linux/drivers/net/ethernet/huawei/hinic/hinic_main.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Huawei HiNIC PCI Express Linux driver
   4 * Copyright(c) 2017 Huawei Technologies Co., Ltd
   5 */
   6
   7#include <linux/kernel.h>
   8#include <linux/module.h>
   9#include <linux/moduleparam.h>
  10#include <linux/pci.h>
  11#include <linux/device.h>
  12#include <linux/errno.h>
  13#include <linux/types.h>
  14#include <linux/etherdevice.h>
  15#include <linux/netdevice.h>
  16#include <linux/slab.h>
  17#include <linux/if_vlan.h>
  18#include <linux/semaphore.h>
  19#include <linux/workqueue.h>
  20#include <net/ip.h>
  21#include <net/devlink.h>
  22#include <linux/bitops.h>
  23#include <linux/bitmap.h>
  24#include <linux/delay.h>
  25#include <linux/err.h>
  26
  27#include "hinic_debugfs.h"
  28#include "hinic_hw_qp.h"
  29#include "hinic_hw_dev.h"
  30#include "hinic_devlink.h"
  31#include "hinic_port.h"
  32#include "hinic_tx.h"
  33#include "hinic_rx.h"
  34#include "hinic_dev.h"
  35#include "hinic_sriov.h"
  36
  37MODULE_AUTHOR("Huawei Technologies CO., Ltd");
  38MODULE_DESCRIPTION("Huawei Intelligent NIC driver");
  39MODULE_LICENSE("GPL");
  40
  41static unsigned int tx_weight = 64;
  42module_param(tx_weight, uint, 0644);
  43MODULE_PARM_DESC(tx_weight, "Number Tx packets for NAPI budget (default=64)");
  44
  45static unsigned int rx_weight = 64;
  46module_param(rx_weight, uint, 0644);
  47MODULE_PARM_DESC(rx_weight, "Number Rx packets for NAPI budget (default=64)");
  48
  49#define HINIC_DEV_ID_QUAD_PORT_25GE         0x1822
  50#define HINIC_DEV_ID_DUAL_PORT_100GE        0x0200
  51#define HINIC_DEV_ID_DUAL_PORT_100GE_MEZZ   0x0205
  52#define HINIC_DEV_ID_QUAD_PORT_25GE_MEZZ    0x0210
  53#define HINIC_DEV_ID_VF    0x375e
  54
  55#define HINIC_WQ_NAME                   "hinic_dev"
  56
  57#define MSG_ENABLE_DEFAULT              (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
  58                                         NETIF_MSG_IFUP |                  \
  59                                         NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR)
  60
  61#define HINIC_LRO_MAX_WQE_NUM_DEFAULT   8
  62
  63#define HINIC_LRO_RX_TIMER_DEFAULT      16
  64
  65#define VLAN_BITMAP_SIZE(nic_dev)       (ALIGN(VLAN_N_VID, 8) / 8)
  66
  67#define work_to_rx_mode_work(work)      \
  68                container_of(work, struct hinic_rx_mode_work, work)
  69
  70#define rx_mode_work_to_nic_dev(rx_mode_work) \
  71                container_of(rx_mode_work, struct hinic_dev, rx_mode_work)
  72
  73#define HINIC_WAIT_SRIOV_CFG_TIMEOUT    15000
  74
  75#define HINIC_DEAULT_TXRX_MSIX_PENDING_LIMIT            2
  76#define HINIC_DEAULT_TXRX_MSIX_COALESC_TIMER_CFG        32
  77#define HINIC_DEAULT_TXRX_MSIX_RESEND_TIMER_CFG         7
  78
  79static int change_mac_addr(struct net_device *netdev, const u8 *addr);
  80
  81static int set_features(struct hinic_dev *nic_dev,
  82                        netdev_features_t pre_features,
  83                        netdev_features_t features, bool force_change);
  84
  85static void update_rx_stats(struct hinic_dev *nic_dev, struct hinic_rxq *rxq)
  86{
  87        struct hinic_rxq_stats *nic_rx_stats = &nic_dev->rx_stats;
  88        struct hinic_rxq_stats rx_stats;
  89
  90        u64_stats_init(&rx_stats.syncp);
  91
  92        hinic_rxq_get_stats(rxq, &rx_stats);
  93
  94        u64_stats_update_begin(&nic_rx_stats->syncp);
  95        nic_rx_stats->bytes += rx_stats.bytes;
  96        nic_rx_stats->pkts  += rx_stats.pkts;
  97        nic_rx_stats->errors += rx_stats.errors;
  98        nic_rx_stats->csum_errors += rx_stats.csum_errors;
  99        nic_rx_stats->other_errors += rx_stats.other_errors;
 100        u64_stats_update_end(&nic_rx_stats->syncp);
 101
 102        hinic_rxq_clean_stats(rxq);
 103}
 104
 105static void update_tx_stats(struct hinic_dev *nic_dev, struct hinic_txq *txq)
 106{
 107        struct hinic_txq_stats *nic_tx_stats = &nic_dev->tx_stats;
 108        struct hinic_txq_stats tx_stats;
 109
 110        u64_stats_init(&tx_stats.syncp);
 111
 112        hinic_txq_get_stats(txq, &tx_stats);
 113
 114        u64_stats_update_begin(&nic_tx_stats->syncp);
 115        nic_tx_stats->bytes += tx_stats.bytes;
 116        nic_tx_stats->pkts += tx_stats.pkts;
 117        nic_tx_stats->tx_busy += tx_stats.tx_busy;
 118        nic_tx_stats->tx_wake += tx_stats.tx_wake;
 119        nic_tx_stats->tx_dropped += tx_stats.tx_dropped;
 120        nic_tx_stats->big_frags_pkts += tx_stats.big_frags_pkts;
 121        u64_stats_update_end(&nic_tx_stats->syncp);
 122
 123        hinic_txq_clean_stats(txq);
 124}
 125
 126static void update_nic_stats(struct hinic_dev *nic_dev)
 127{
 128        int i, num_qps = hinic_hwdev_num_qps(nic_dev->hwdev);
 129
 130        for (i = 0; i < num_qps; i++)
 131                update_rx_stats(nic_dev, &nic_dev->rxqs[i]);
 132
 133        for (i = 0; i < num_qps; i++)
 134                update_tx_stats(nic_dev, &nic_dev->txqs[i]);
 135}
 136
 137/**
 138 * create_txqs - Create the Logical Tx Queues of specific NIC device
 139 * @nic_dev: the specific NIC device
 140 *
 141 * Return 0 - Success, negative - Failure
 142 **/
 143static int create_txqs(struct hinic_dev *nic_dev)
 144{
 145        int err, i, j, num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
 146        struct net_device *netdev = nic_dev->netdev;
 147        size_t txq_size;
 148
 149        if (nic_dev->txqs)
 150                return -EINVAL;
 151
 152        txq_size = num_txqs * sizeof(*nic_dev->txqs);
 153        nic_dev->txqs = devm_kzalloc(&netdev->dev, txq_size, GFP_KERNEL);
 154        if (!nic_dev->txqs)
 155                return -ENOMEM;
 156
 157        hinic_sq_dbgfs_init(nic_dev);
 158
 159        for (i = 0; i < num_txqs; i++) {
 160                struct hinic_sq *sq = hinic_hwdev_get_sq(nic_dev->hwdev, i);
 161
 162                err = hinic_init_txq(&nic_dev->txqs[i], sq, netdev);
 163                if (err) {
 164                        netif_err(nic_dev, drv, netdev,
 165                                  "Failed to init Txq\n");
 166                        goto err_init_txq;
 167                }
 168
 169                err = hinic_sq_debug_add(nic_dev, i);
 170                if (err) {
 171                        netif_err(nic_dev, drv, netdev,
 172                                  "Failed to add SQ%d debug\n", i);
 173                        goto err_add_sq_dbg;
 174                }
 175
 176        }
 177
 178        return 0;
 179
 180err_add_sq_dbg:
 181        hinic_clean_txq(&nic_dev->txqs[i]);
 182err_init_txq:
 183        for (j = 0; j < i; j++) {
 184                hinic_sq_debug_rem(nic_dev->txqs[j].sq);
 185                hinic_clean_txq(&nic_dev->txqs[j]);
 186        }
 187
 188        hinic_sq_dbgfs_uninit(nic_dev);
 189
 190        devm_kfree(&netdev->dev, nic_dev->txqs);
 191        return err;
 192}
 193
 194static void enable_txqs_napi(struct hinic_dev *nic_dev)
 195{
 196        int num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
 197        int i;
 198
 199        for (i = 0; i < num_txqs; i++)
 200                napi_enable(&nic_dev->txqs[i].napi);
 201}
 202
 203static void disable_txqs_napi(struct hinic_dev *nic_dev)
 204{
 205        int num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
 206        int i;
 207
 208        for (i = 0; i < num_txqs; i++)
 209                napi_disable(&nic_dev->txqs[i].napi);
 210}
 211
 212/**
 213 * free_txqs - Free the Logical Tx Queues of specific NIC device
 214 * @nic_dev: the specific NIC device
 215 **/
 216static void free_txqs(struct hinic_dev *nic_dev)
 217{
 218        int i, num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
 219        struct net_device *netdev = nic_dev->netdev;
 220
 221        if (!nic_dev->txqs)
 222                return;
 223
 224        for (i = 0; i < num_txqs; i++) {
 225                hinic_sq_debug_rem(nic_dev->txqs[i].sq);
 226                hinic_clean_txq(&nic_dev->txqs[i]);
 227        }
 228
 229        hinic_sq_dbgfs_uninit(nic_dev);
 230
 231        devm_kfree(&netdev->dev, nic_dev->txqs);
 232        nic_dev->txqs = NULL;
 233}
 234
 235/**
 236 * create_txqs - Create the Logical Rx Queues of specific NIC device
 237 * @nic_dev: the specific NIC device
 238 *
 239 * Return 0 - Success, negative - Failure
 240 **/
 241static int create_rxqs(struct hinic_dev *nic_dev)
 242{
 243        int err, i, j, num_rxqs = hinic_hwdev_num_qps(nic_dev->hwdev);
 244        struct net_device *netdev = nic_dev->netdev;
 245        size_t rxq_size;
 246
 247        if (nic_dev->rxqs)
 248                return -EINVAL;
 249
 250        rxq_size = num_rxqs * sizeof(*nic_dev->rxqs);
 251        nic_dev->rxqs = devm_kzalloc(&netdev->dev, rxq_size, GFP_KERNEL);
 252        if (!nic_dev->rxqs)
 253                return -ENOMEM;
 254
 255        hinic_rq_dbgfs_init(nic_dev);
 256
 257        for (i = 0; i < num_rxqs; i++) {
 258                struct hinic_rq *rq = hinic_hwdev_get_rq(nic_dev->hwdev, i);
 259
 260                err = hinic_init_rxq(&nic_dev->rxqs[i], rq, netdev);
 261                if (err) {
 262                        netif_err(nic_dev, drv, netdev,
 263                                  "Failed to init rxq\n");
 264                        goto err_init_rxq;
 265                }
 266
 267                err = hinic_rq_debug_add(nic_dev, i);
 268                if (err) {
 269                        netif_err(nic_dev, drv, netdev,
 270                                  "Failed to add RQ%d debug\n", i);
 271                        goto err_add_rq_dbg;
 272                }
 273        }
 274
 275        return 0;
 276
 277err_add_rq_dbg:
 278        hinic_clean_rxq(&nic_dev->rxqs[i]);
 279err_init_rxq:
 280        for (j = 0; j < i; j++) {
 281                hinic_rq_debug_rem(nic_dev->rxqs[j].rq);
 282                hinic_clean_rxq(&nic_dev->rxqs[j]);
 283        }
 284
 285        hinic_rq_dbgfs_uninit(nic_dev);
 286
 287        devm_kfree(&netdev->dev, nic_dev->rxqs);
 288        return err;
 289}
 290
 291/**
 292 * free_txqs - Free the Logical Rx Queues of specific NIC device
 293 * @nic_dev: the specific NIC device
 294 **/
 295static void free_rxqs(struct hinic_dev *nic_dev)
 296{
 297        int i, num_rxqs = hinic_hwdev_num_qps(nic_dev->hwdev);
 298        struct net_device *netdev = nic_dev->netdev;
 299
 300        if (!nic_dev->rxqs)
 301                return;
 302
 303        for (i = 0; i < num_rxqs; i++) {
 304                hinic_rq_debug_rem(nic_dev->rxqs[i].rq);
 305                hinic_clean_rxq(&nic_dev->rxqs[i]);
 306        }
 307
 308        hinic_rq_dbgfs_uninit(nic_dev);
 309
 310        devm_kfree(&netdev->dev, nic_dev->rxqs);
 311        nic_dev->rxqs = NULL;
 312}
 313
 314static int hinic_configure_max_qnum(struct hinic_dev *nic_dev)
 315{
 316        return hinic_set_max_qnum(nic_dev, nic_dev->hwdev->nic_cap.max_qps);
 317}
 318
 319static int hinic_rss_init(struct hinic_dev *nic_dev)
 320{
 321        u8 default_rss_key[HINIC_RSS_KEY_SIZE];
 322        u8 tmpl_idx = nic_dev->rss_tmpl_idx;
 323        u32 *indir_tbl;
 324        int err, i;
 325
 326        indir_tbl = kcalloc(HINIC_RSS_INDIR_SIZE, sizeof(u32), GFP_KERNEL);
 327        if (!indir_tbl)
 328                return -ENOMEM;
 329
 330        netdev_rss_key_fill(default_rss_key, sizeof(default_rss_key));
 331        for (i = 0; i < HINIC_RSS_INDIR_SIZE; i++)
 332                indir_tbl[i] = ethtool_rxfh_indir_default(i, nic_dev->num_rss);
 333
 334        err = hinic_rss_set_template_tbl(nic_dev, tmpl_idx, default_rss_key);
 335        if (err)
 336                goto out;
 337
 338        err = hinic_rss_set_indir_tbl(nic_dev, tmpl_idx, indir_tbl);
 339        if (err)
 340                goto out;
 341
 342        err = hinic_set_rss_type(nic_dev, tmpl_idx, nic_dev->rss_type);
 343        if (err)
 344                goto out;
 345
 346        err = hinic_rss_set_hash_engine(nic_dev, tmpl_idx,
 347                                        nic_dev->rss_hash_engine);
 348        if (err)
 349                goto out;
 350
 351        err = hinic_rss_cfg(nic_dev, 1, tmpl_idx);
 352        if (err)
 353                goto out;
 354
 355out:
 356        kfree(indir_tbl);
 357        return err;
 358}
 359
 360static void hinic_rss_deinit(struct hinic_dev *nic_dev)
 361{
 362        hinic_rss_cfg(nic_dev, 0, nic_dev->rss_tmpl_idx);
 363}
 364
 365static void hinic_init_rss_parameters(struct hinic_dev *nic_dev)
 366{
 367        nic_dev->rss_hash_engine = HINIC_RSS_HASH_ENGINE_TYPE_XOR;
 368        nic_dev->rss_type.tcp_ipv6_ext = 1;
 369        nic_dev->rss_type.ipv6_ext = 1;
 370        nic_dev->rss_type.tcp_ipv6 = 1;
 371        nic_dev->rss_type.ipv6 = 1;
 372        nic_dev->rss_type.tcp_ipv4 = 1;
 373        nic_dev->rss_type.ipv4 = 1;
 374        nic_dev->rss_type.udp_ipv6 = 1;
 375        nic_dev->rss_type.udp_ipv4 = 1;
 376}
 377
 378static void hinic_enable_rss(struct hinic_dev *nic_dev)
 379{
 380        struct net_device *netdev = nic_dev->netdev;
 381        struct hinic_hwdev *hwdev = nic_dev->hwdev;
 382        struct hinic_hwif *hwif = hwdev->hwif;
 383        struct pci_dev *pdev = hwif->pdev;
 384        int i, node, err = 0;
 385        u16 num_cpus = 0;
 386
 387        if (nic_dev->max_qps <= 1) {
 388                nic_dev->flags &= ~HINIC_RSS_ENABLE;
 389                nic_dev->rss_limit = nic_dev->max_qps;
 390                nic_dev->num_qps = nic_dev->max_qps;
 391                nic_dev->num_rss = nic_dev->max_qps;
 392
 393                return;
 394        }
 395
 396        err = hinic_rss_template_alloc(nic_dev, &nic_dev->rss_tmpl_idx);
 397        if (err) {
 398                netif_err(nic_dev, drv, netdev,
 399                          "Failed to alloc tmpl_idx for rss, can't enable rss for this function\n");
 400                nic_dev->flags &= ~HINIC_RSS_ENABLE;
 401                nic_dev->max_qps = 1;
 402                nic_dev->rss_limit = nic_dev->max_qps;
 403                nic_dev->num_qps = nic_dev->max_qps;
 404                nic_dev->num_rss = nic_dev->max_qps;
 405
 406                return;
 407        }
 408
 409        nic_dev->flags |= HINIC_RSS_ENABLE;
 410
 411        for (i = 0; i < num_online_cpus(); i++) {
 412                node = cpu_to_node(i);
 413                if (node == dev_to_node(&pdev->dev))
 414                        num_cpus++;
 415        }
 416
 417        if (!num_cpus)
 418                num_cpus = num_online_cpus();
 419
 420        nic_dev->num_qps = hinic_hwdev_num_qps(hwdev);
 421        nic_dev->num_qps = min_t(u16, nic_dev->num_qps, num_cpus);
 422
 423        nic_dev->rss_limit = nic_dev->num_qps;
 424        nic_dev->num_rss = nic_dev->num_qps;
 425
 426        hinic_init_rss_parameters(nic_dev);
 427        err = hinic_rss_init(nic_dev);
 428        if (err)
 429                netif_err(nic_dev, drv, netdev, "Failed to init rss\n");
 430}
 431
 432int hinic_open(struct net_device *netdev)
 433{
 434        struct hinic_dev *nic_dev = netdev_priv(netdev);
 435        enum hinic_port_link_state link_state;
 436        int err, ret;
 437
 438        if (!(nic_dev->flags & HINIC_INTF_UP)) {
 439                err = hinic_hwdev_ifup(nic_dev->hwdev, nic_dev->sq_depth,
 440                                       nic_dev->rq_depth);
 441                if (err) {
 442                        netif_err(nic_dev, drv, netdev,
 443                                  "Failed - HW interface up\n");
 444                        return err;
 445                }
 446        }
 447
 448        err = create_txqs(nic_dev);
 449        if (err) {
 450                netif_err(nic_dev, drv, netdev,
 451                          "Failed to create Tx queues\n");
 452                goto err_create_txqs;
 453        }
 454
 455        enable_txqs_napi(nic_dev);
 456
 457        err = create_rxqs(nic_dev);
 458        if (err) {
 459                netif_err(nic_dev, drv, netdev,
 460                          "Failed to create Rx queues\n");
 461                goto err_create_rxqs;
 462        }
 463
 464        hinic_enable_rss(nic_dev);
 465
 466        err = hinic_configure_max_qnum(nic_dev);
 467        if (err) {
 468                netif_err(nic_dev, drv, nic_dev->netdev,
 469                          "Failed to configure the maximum number of queues\n");
 470                goto err_port_state;
 471        }
 472
 473        netif_set_real_num_tx_queues(netdev, nic_dev->num_qps);
 474        netif_set_real_num_rx_queues(netdev, nic_dev->num_qps);
 475
 476        err = hinic_port_set_state(nic_dev, HINIC_PORT_ENABLE);
 477        if (err) {
 478                netif_err(nic_dev, drv, netdev,
 479                          "Failed to set port state\n");
 480                goto err_port_state;
 481        }
 482
 483        err = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_ENABLE);
 484        if (err) {
 485                netif_err(nic_dev, drv, netdev,
 486                          "Failed to set func port state\n");
 487                goto err_func_port_state;
 488        }
 489
 490        down(&nic_dev->mgmt_lock);
 491
 492        err = hinic_port_link_state(nic_dev, &link_state);
 493        if (err) {
 494                netif_err(nic_dev, drv, netdev, "Failed to get link state\n");
 495                goto err_port_link;
 496        }
 497
 498        if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
 499                hinic_notify_all_vfs_link_changed(nic_dev->hwdev, link_state);
 500
 501        if (link_state == HINIC_LINK_STATE_UP) {
 502                nic_dev->flags |= HINIC_LINK_UP;
 503                nic_dev->cable_unplugged = false;
 504                nic_dev->module_unrecognized = false;
 505        }
 506
 507        nic_dev->flags |= HINIC_INTF_UP;
 508
 509        if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) ==
 510            (HINIC_LINK_UP | HINIC_INTF_UP)) {
 511                netif_info(nic_dev, drv, netdev, "link + intf UP\n");
 512                netif_carrier_on(netdev);
 513                netif_tx_wake_all_queues(netdev);
 514        }
 515
 516        up(&nic_dev->mgmt_lock);
 517
 518        netif_info(nic_dev, drv, netdev, "HINIC_INTF is UP\n");
 519        return 0;
 520
 521err_port_link:
 522        up(&nic_dev->mgmt_lock);
 523        ret = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE);
 524        if (ret)
 525                netif_warn(nic_dev, drv, netdev,
 526                           "Failed to revert func port state\n");
 527
 528err_func_port_state:
 529        ret = hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE);
 530        if (ret)
 531                netif_warn(nic_dev, drv, netdev,
 532                           "Failed to revert port state\n");
 533err_port_state:
 534        free_rxqs(nic_dev);
 535        if (nic_dev->flags & HINIC_RSS_ENABLE) {
 536                hinic_rss_deinit(nic_dev);
 537                hinic_rss_template_free(nic_dev, nic_dev->rss_tmpl_idx);
 538        }
 539
 540err_create_rxqs:
 541        disable_txqs_napi(nic_dev);
 542        free_txqs(nic_dev);
 543
 544err_create_txqs:
 545        if (!(nic_dev->flags & HINIC_INTF_UP))
 546                hinic_hwdev_ifdown(nic_dev->hwdev);
 547        return err;
 548}
 549
 550int hinic_close(struct net_device *netdev)
 551{
 552        struct hinic_dev *nic_dev = netdev_priv(netdev);
 553        unsigned int flags;
 554
 555        /* Disable txq napi firstly to aviod rewaking txq in free_tx_poll */
 556        disable_txqs_napi(nic_dev);
 557
 558        down(&nic_dev->mgmt_lock);
 559
 560        flags = nic_dev->flags;
 561        nic_dev->flags &= ~HINIC_INTF_UP;
 562
 563        netif_carrier_off(netdev);
 564        netif_tx_disable(netdev);
 565
 566        update_nic_stats(nic_dev);
 567
 568        up(&nic_dev->mgmt_lock);
 569
 570        if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
 571                hinic_notify_all_vfs_link_changed(nic_dev->hwdev, 0);
 572
 573        hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE);
 574
 575        hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE);
 576
 577        if (nic_dev->flags & HINIC_RSS_ENABLE) {
 578                hinic_rss_deinit(nic_dev);
 579                hinic_rss_template_free(nic_dev, nic_dev->rss_tmpl_idx);
 580        }
 581
 582        free_rxqs(nic_dev);
 583        free_txqs(nic_dev);
 584
 585        if (flags & HINIC_INTF_UP)
 586                hinic_hwdev_ifdown(nic_dev->hwdev);
 587
 588        netif_info(nic_dev, drv, netdev, "HINIC_INTF is DOWN\n");
 589        return 0;
 590}
 591
 592static int hinic_change_mtu(struct net_device *netdev, int new_mtu)
 593{
 594        struct hinic_dev *nic_dev = netdev_priv(netdev);
 595        int err;
 596
 597        netif_info(nic_dev, drv, netdev, "set_mtu = %d\n", new_mtu);
 598
 599        err = hinic_port_set_mtu(nic_dev, new_mtu);
 600        if (err)
 601                netif_err(nic_dev, drv, netdev, "Failed to set port mtu\n");
 602        else
 603                netdev->mtu = new_mtu;
 604
 605        return err;
 606}
 607
 608/**
 609 * change_mac_addr - change the main mac address of network device
 610 * @netdev: network device
 611 * @addr: mac address to set
 612 *
 613 * Return 0 - Success, negative - Failure
 614 **/
 615static int change_mac_addr(struct net_device *netdev, const u8 *addr)
 616{
 617        struct hinic_dev *nic_dev = netdev_priv(netdev);
 618        u16 vid = 0;
 619        int err;
 620
 621        if (!is_valid_ether_addr(addr))
 622                return -EADDRNOTAVAIL;
 623
 624        netif_info(nic_dev, drv, netdev, "change mac addr = %02x %02x %02x %02x %02x %02x\n",
 625                   addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
 626
 627        down(&nic_dev->mgmt_lock);
 628
 629        do {
 630                err = hinic_port_del_mac(nic_dev, netdev->dev_addr, vid);
 631                if (err) {
 632                        netif_err(nic_dev, drv, netdev,
 633                                  "Failed to delete mac\n");
 634                        break;
 635                }
 636
 637                err = hinic_port_add_mac(nic_dev, addr, vid);
 638                if (err) {
 639                        netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
 640                        break;
 641                }
 642
 643                vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
 644        } while (vid != VLAN_N_VID);
 645
 646        up(&nic_dev->mgmt_lock);
 647        return err;
 648}
 649
 650static int hinic_set_mac_addr(struct net_device *netdev, void *addr)
 651{
 652        unsigned char new_mac[ETH_ALEN];
 653        struct sockaddr *saddr = addr;
 654        int err;
 655
 656        memcpy(new_mac, saddr->sa_data, ETH_ALEN);
 657
 658        err = change_mac_addr(netdev, new_mac);
 659        if (!err)
 660                memcpy(netdev->dev_addr, new_mac, ETH_ALEN);
 661
 662        return err;
 663}
 664
 665/**
 666 * add_mac_addr - add mac address to network device
 667 * @netdev: network device
 668 * @addr: mac address to add
 669 *
 670 * Return 0 - Success, negative - Failure
 671 **/
 672static int add_mac_addr(struct net_device *netdev, const u8 *addr)
 673{
 674        struct hinic_dev *nic_dev = netdev_priv(netdev);
 675        u16 vid = 0;
 676        int err;
 677
 678        netif_info(nic_dev, drv, netdev, "set mac addr = %02x %02x %02x %02x %02x %02x\n",
 679                   addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
 680
 681        down(&nic_dev->mgmt_lock);
 682
 683        do {
 684                err = hinic_port_add_mac(nic_dev, addr, vid);
 685                if (err) {
 686                        netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
 687                        break;
 688                }
 689
 690                vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
 691        } while (vid != VLAN_N_VID);
 692
 693        up(&nic_dev->mgmt_lock);
 694        return err;
 695}
 696
 697/**
 698 * remove_mac_addr - remove mac address from network device
 699 * @netdev: network device
 700 * @addr: mac address to remove
 701 *
 702 * Return 0 - Success, negative - Failure
 703 **/
 704static int remove_mac_addr(struct net_device *netdev, const u8 *addr)
 705{
 706        struct hinic_dev *nic_dev = netdev_priv(netdev);
 707        u16 vid = 0;
 708        int err;
 709
 710        if (!is_valid_ether_addr(addr))
 711                return -EADDRNOTAVAIL;
 712
 713        netif_info(nic_dev, drv, netdev, "remove mac addr = %02x %02x %02x %02x %02x %02x\n",
 714                   addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
 715
 716        down(&nic_dev->mgmt_lock);
 717
 718        do {
 719                err = hinic_port_del_mac(nic_dev, addr, vid);
 720                if (err) {
 721                        netif_err(nic_dev, drv, netdev,
 722                                  "Failed to delete mac\n");
 723                        break;
 724                }
 725
 726                vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
 727        } while (vid != VLAN_N_VID);
 728
 729        up(&nic_dev->mgmt_lock);
 730        return err;
 731}
 732
 733static int hinic_vlan_rx_add_vid(struct net_device *netdev,
 734                                 __always_unused __be16 proto, u16 vid)
 735{
 736        struct hinic_dev *nic_dev = netdev_priv(netdev);
 737        int ret, err;
 738
 739        netif_info(nic_dev, drv, netdev, "add vid = %d\n", vid);
 740
 741        down(&nic_dev->mgmt_lock);
 742
 743        err = hinic_port_add_vlan(nic_dev, vid);
 744        if (err) {
 745                netif_err(nic_dev, drv, netdev, "Failed to add vlan\n");
 746                goto err_vlan_add;
 747        }
 748
 749        err = hinic_port_add_mac(nic_dev, netdev->dev_addr, vid);
 750        if (err && err != HINIC_PF_SET_VF_ALREADY) {
 751                netif_err(nic_dev, drv, netdev, "Failed to set mac\n");
 752                goto err_add_mac;
 753        }
 754
 755        bitmap_set(nic_dev->vlan_bitmap, vid, 1);
 756
 757        up(&nic_dev->mgmt_lock);
 758        return 0;
 759
 760err_add_mac:
 761        ret = hinic_port_del_vlan(nic_dev, vid);
 762        if (ret)
 763                netif_err(nic_dev, drv, netdev,
 764                          "Failed to revert by removing vlan\n");
 765
 766err_vlan_add:
 767        up(&nic_dev->mgmt_lock);
 768        return err;
 769}
 770
 771static int hinic_vlan_rx_kill_vid(struct net_device *netdev,
 772                                  __always_unused __be16 proto, u16 vid)
 773{
 774        struct hinic_dev *nic_dev = netdev_priv(netdev);
 775        int err;
 776
 777        netif_info(nic_dev, drv, netdev, "remove vid = %d\n", vid);
 778
 779        down(&nic_dev->mgmt_lock);
 780
 781        err = hinic_port_del_vlan(nic_dev, vid);
 782        if (err) {
 783                netif_err(nic_dev, drv, netdev, "Failed to delete vlan\n");
 784                goto err_del_vlan;
 785        }
 786
 787        bitmap_clear(nic_dev->vlan_bitmap, vid, 1);
 788
 789        up(&nic_dev->mgmt_lock);
 790        return 0;
 791
 792err_del_vlan:
 793        up(&nic_dev->mgmt_lock);
 794        return err;
 795}
 796
 797static void set_rx_mode(struct work_struct *work)
 798{
 799        struct hinic_rx_mode_work *rx_mode_work = work_to_rx_mode_work(work);
 800        struct hinic_dev *nic_dev = rx_mode_work_to_nic_dev(rx_mode_work);
 801
 802        hinic_port_set_rx_mode(nic_dev, rx_mode_work->rx_mode);
 803
 804        __dev_uc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
 805        __dev_mc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
 806}
 807
 808static void hinic_set_rx_mode(struct net_device *netdev)
 809{
 810        struct hinic_dev *nic_dev = netdev_priv(netdev);
 811        struct hinic_rx_mode_work *rx_mode_work;
 812        u32 rx_mode;
 813
 814        rx_mode_work = &nic_dev->rx_mode_work;
 815
 816        rx_mode = HINIC_RX_MODE_UC |
 817                  HINIC_RX_MODE_MC |
 818                  HINIC_RX_MODE_BC;
 819
 820        if (netdev->flags & IFF_PROMISC) {
 821                if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
 822                        rx_mode |= HINIC_RX_MODE_PROMISC;
 823        } else if (netdev->flags & IFF_ALLMULTI) {
 824                rx_mode |= HINIC_RX_MODE_MC_ALL;
 825        }
 826
 827        rx_mode_work->rx_mode = rx_mode;
 828
 829        queue_work(nic_dev->workq, &rx_mode_work->work);
 830}
 831
 832static void hinic_tx_timeout(struct net_device *netdev, unsigned int txqueue)
 833{
 834        struct hinic_dev *nic_dev = netdev_priv(netdev);
 835        u16 sw_pi, hw_ci, sw_ci;
 836        struct hinic_sq *sq;
 837        u16 num_sqs, q_id;
 838
 839        num_sqs = hinic_hwdev_num_qps(nic_dev->hwdev);
 840
 841        netif_err(nic_dev, drv, netdev, "Tx timeout\n");
 842
 843        for (q_id = 0; q_id < num_sqs; q_id++) {
 844                if (!netif_xmit_stopped(netdev_get_tx_queue(netdev, q_id)))
 845                        continue;
 846
 847                sq = hinic_hwdev_get_sq(nic_dev->hwdev, q_id);
 848                sw_pi = atomic_read(&sq->wq->prod_idx) & sq->wq->mask;
 849                hw_ci = be16_to_cpu(*(u16 *)(sq->hw_ci_addr)) & sq->wq->mask;
 850                sw_ci = atomic_read(&sq->wq->cons_idx) & sq->wq->mask;
 851                netif_err(nic_dev, drv, netdev, "Txq%d: sw_pi: %d, hw_ci: %d, sw_ci: %d, napi->state: 0x%lx\n",
 852                          q_id, sw_pi, hw_ci, sw_ci,
 853                          nic_dev->txqs[q_id].napi.state);
 854        }
 855}
 856
 857static void hinic_get_stats64(struct net_device *netdev,
 858                              struct rtnl_link_stats64 *stats)
 859{
 860        struct hinic_dev *nic_dev = netdev_priv(netdev);
 861        struct hinic_rxq_stats *nic_rx_stats;
 862        struct hinic_txq_stats *nic_tx_stats;
 863
 864        nic_rx_stats = &nic_dev->rx_stats;
 865        nic_tx_stats = &nic_dev->tx_stats;
 866
 867        down(&nic_dev->mgmt_lock);
 868
 869        if (nic_dev->flags & HINIC_INTF_UP)
 870                update_nic_stats(nic_dev);
 871
 872        up(&nic_dev->mgmt_lock);
 873
 874        stats->rx_bytes   = nic_rx_stats->bytes;
 875        stats->rx_packets = nic_rx_stats->pkts;
 876        stats->rx_errors  = nic_rx_stats->errors;
 877
 878        stats->tx_bytes   = nic_tx_stats->bytes;
 879        stats->tx_packets = nic_tx_stats->pkts;
 880        stats->tx_errors  = nic_tx_stats->tx_dropped;
 881}
 882
 883static int hinic_set_features(struct net_device *netdev,
 884                              netdev_features_t features)
 885{
 886        struct hinic_dev *nic_dev = netdev_priv(netdev);
 887
 888        return set_features(nic_dev, nic_dev->netdev->features,
 889                            features, false);
 890}
 891
 892static netdev_features_t hinic_fix_features(struct net_device *netdev,
 893                                            netdev_features_t features)
 894{
 895        struct hinic_dev *nic_dev = netdev_priv(netdev);
 896
 897        /* If Rx checksum is disabled, then LRO should also be disabled */
 898        if (!(features & NETIF_F_RXCSUM)) {
 899                netif_info(nic_dev, drv, netdev, "disabling LRO as RXCSUM is off\n");
 900                features &= ~NETIF_F_LRO;
 901        }
 902
 903        return features;
 904}
 905
 906static const struct net_device_ops hinic_netdev_ops = {
 907        .ndo_open = hinic_open,
 908        .ndo_stop = hinic_close,
 909        .ndo_change_mtu = hinic_change_mtu,
 910        .ndo_set_mac_address = hinic_set_mac_addr,
 911        .ndo_validate_addr = eth_validate_addr,
 912        .ndo_vlan_rx_add_vid = hinic_vlan_rx_add_vid,
 913        .ndo_vlan_rx_kill_vid = hinic_vlan_rx_kill_vid,
 914        .ndo_set_rx_mode = hinic_set_rx_mode,
 915        .ndo_start_xmit = hinic_xmit_frame,
 916        .ndo_tx_timeout = hinic_tx_timeout,
 917        .ndo_get_stats64 = hinic_get_stats64,
 918        .ndo_fix_features = hinic_fix_features,
 919        .ndo_set_features = hinic_set_features,
 920        .ndo_set_vf_mac = hinic_ndo_set_vf_mac,
 921        .ndo_set_vf_vlan = hinic_ndo_set_vf_vlan,
 922        .ndo_get_vf_config = hinic_ndo_get_vf_config,
 923        .ndo_set_vf_trust = hinic_ndo_set_vf_trust,
 924        .ndo_set_vf_rate = hinic_ndo_set_vf_bw,
 925        .ndo_set_vf_spoofchk = hinic_ndo_set_vf_spoofchk,
 926        .ndo_set_vf_link_state = hinic_ndo_set_vf_link_state,
 927};
 928
 929static const struct net_device_ops hinicvf_netdev_ops = {
 930        .ndo_open = hinic_open,
 931        .ndo_stop = hinic_close,
 932        .ndo_change_mtu = hinic_change_mtu,
 933        .ndo_set_mac_address = hinic_set_mac_addr,
 934        .ndo_validate_addr = eth_validate_addr,
 935        .ndo_vlan_rx_add_vid = hinic_vlan_rx_add_vid,
 936        .ndo_vlan_rx_kill_vid = hinic_vlan_rx_kill_vid,
 937        .ndo_set_rx_mode = hinic_set_rx_mode,
 938        .ndo_start_xmit = hinic_xmit_frame,
 939        .ndo_tx_timeout = hinic_tx_timeout,
 940        .ndo_get_stats64 = hinic_get_stats64,
 941        .ndo_fix_features = hinic_fix_features,
 942        .ndo_set_features = hinic_set_features,
 943};
 944
 945static void netdev_features_init(struct net_device *netdev)
 946{
 947        netdev->hw_features = NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_IP_CSUM |
 948                              NETIF_F_IPV6_CSUM | NETIF_F_TSO | NETIF_F_TSO6 |
 949                              NETIF_F_RXCSUM | NETIF_F_LRO |
 950                              NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
 951                              NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_UDP_TUNNEL_CSUM;
 952
 953        netdev->vlan_features = netdev->hw_features;
 954
 955        netdev->features = netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
 956
 957        netdev->hw_enc_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SCTP_CRC |
 958                                  NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN |
 959                                  NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_UDP_TUNNEL;
 960}
 961
 962static void hinic_refresh_nic_cfg(struct hinic_dev *nic_dev)
 963{
 964        struct hinic_nic_cfg *nic_cfg = &nic_dev->hwdev->func_to_io.nic_cfg;
 965        struct hinic_pause_config pause_info = {0};
 966        struct hinic_port_cap port_cap = {0};
 967
 968        if (hinic_port_get_cap(nic_dev, &port_cap))
 969                return;
 970
 971        mutex_lock(&nic_cfg->cfg_mutex);
 972        if (nic_cfg->pause_set || !port_cap.autoneg_state) {
 973                nic_cfg->auto_neg = port_cap.autoneg_state;
 974                pause_info.auto_neg = nic_cfg->auto_neg;
 975                pause_info.rx_pause = nic_cfg->rx_pause;
 976                pause_info.tx_pause = nic_cfg->tx_pause;
 977                hinic_set_hw_pause_info(nic_dev->hwdev, &pause_info);
 978        }
 979        mutex_unlock(&nic_cfg->cfg_mutex);
 980}
 981
 982/**
 983 * link_status_event_handler - link event handler
 984 * @handle: nic device for the handler
 985 * @buf_in: input buffer
 986 * @in_size: input size
 987 * @buf_out: output buffer
 988 * @out_size: returned output size
 989 *
 990 * Return 0 - Success, negative - Failure
 991 **/
 992static void link_status_event_handler(void *handle, void *buf_in, u16 in_size,
 993                                      void *buf_out, u16 *out_size)
 994{
 995        struct hinic_port_link_status *link_status, *ret_link_status;
 996        struct hinic_dev *nic_dev = handle;
 997
 998        link_status = buf_in;
 999
1000        if (link_status->link == HINIC_LINK_STATE_UP) {
1001                down(&nic_dev->mgmt_lock);
1002
1003                nic_dev->flags |= HINIC_LINK_UP;
1004                nic_dev->cable_unplugged = false;
1005                nic_dev->module_unrecognized = false;
1006
1007                if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) ==
1008                    (HINIC_LINK_UP | HINIC_INTF_UP)) {
1009                        netif_carrier_on(nic_dev->netdev);
1010                        netif_tx_wake_all_queues(nic_dev->netdev);
1011                }
1012
1013                up(&nic_dev->mgmt_lock);
1014
1015                if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
1016                        hinic_refresh_nic_cfg(nic_dev);
1017
1018                netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is UP\n");
1019        } else {
1020                down(&nic_dev->mgmt_lock);
1021
1022                nic_dev->flags &= ~HINIC_LINK_UP;
1023
1024                netif_carrier_off(nic_dev->netdev);
1025                netif_tx_disable(nic_dev->netdev);
1026
1027                up(&nic_dev->mgmt_lock);
1028
1029                netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is DOWN\n");
1030        }
1031
1032        if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
1033                hinic_notify_all_vfs_link_changed(nic_dev->hwdev,
1034                                                  link_status->link);
1035
1036        ret_link_status = buf_out;
1037        ret_link_status->status = 0;
1038
1039        *out_size = sizeof(*ret_link_status);
1040}
1041
1042static void cable_plug_event(void *handle,
1043                             void *buf_in, u16 in_size,
1044                             void *buf_out, u16 *out_size)
1045{
1046        struct hinic_cable_plug_event *plug_event = buf_in;
1047        struct hinic_dev *nic_dev = handle;
1048
1049        nic_dev->cable_unplugged = plug_event->plugged ? false : true;
1050
1051        *out_size = sizeof(*plug_event);
1052        plug_event = buf_out;
1053        plug_event->status = 0;
1054}
1055
1056static void link_err_event(void *handle,
1057                           void *buf_in, u16 in_size,
1058                           void *buf_out, u16 *out_size)
1059{
1060        struct hinic_link_err_event *link_err = buf_in;
1061        struct hinic_dev *nic_dev = handle;
1062
1063        if (link_err->err_type >= LINK_ERR_NUM)
1064                netif_info(nic_dev, link, nic_dev->netdev,
1065                           "Link failed, Unknown error type: 0x%x\n",
1066                           link_err->err_type);
1067        else
1068                nic_dev->module_unrecognized = true;
1069
1070        *out_size = sizeof(*link_err);
1071        link_err = buf_out;
1072        link_err->status = 0;
1073}
1074
1075static int set_features(struct hinic_dev *nic_dev,
1076                        netdev_features_t pre_features,
1077                        netdev_features_t features, bool force_change)
1078{
1079        netdev_features_t changed = force_change ? ~0 : pre_features ^ features;
1080        u32 csum_en = HINIC_RX_CSUM_OFFLOAD_EN;
1081        netdev_features_t failed_features = 0;
1082        int ret = 0;
1083        int err = 0;
1084
1085        if (changed & NETIF_F_TSO) {
1086                ret = hinic_port_set_tso(nic_dev, (features & NETIF_F_TSO) ?
1087                                         HINIC_TSO_ENABLE : HINIC_TSO_DISABLE);
1088                if (ret) {
1089                        err = ret;
1090                        failed_features |= NETIF_F_TSO;
1091                }
1092        }
1093
1094        if (changed & NETIF_F_RXCSUM) {
1095                ret = hinic_set_rx_csum_offload(nic_dev, csum_en);
1096                if (ret) {
1097                        err = ret;
1098                        failed_features |= NETIF_F_RXCSUM;
1099                }
1100        }
1101
1102        if (changed & NETIF_F_LRO) {
1103                ret = hinic_set_rx_lro_state(nic_dev,
1104                                             !!(features & NETIF_F_LRO),
1105                                             HINIC_LRO_RX_TIMER_DEFAULT,
1106                                             HINIC_LRO_MAX_WQE_NUM_DEFAULT);
1107                if (ret) {
1108                        err = ret;
1109                        failed_features |= NETIF_F_LRO;
1110                }
1111        }
1112
1113        if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
1114                ret = hinic_set_rx_vlan_offload(nic_dev,
1115                                                !!(features &
1116                                                   NETIF_F_HW_VLAN_CTAG_RX));
1117                if (ret) {
1118                        err = ret;
1119                        failed_features |= NETIF_F_HW_VLAN_CTAG_RX;
1120                }
1121        }
1122
1123        if (err) {
1124                nic_dev->netdev->features = features ^ failed_features;
1125                return -EIO;
1126        }
1127
1128        return 0;
1129}
1130
1131static int hinic_init_intr_coalesce(struct hinic_dev *nic_dev)
1132{
1133        u64 size;
1134        u16 i;
1135
1136        size = sizeof(struct hinic_intr_coal_info) * nic_dev->max_qps;
1137        nic_dev->rx_intr_coalesce = kzalloc(size, GFP_KERNEL);
1138        if (!nic_dev->rx_intr_coalesce)
1139                return -ENOMEM;
1140        nic_dev->tx_intr_coalesce = kzalloc(size, GFP_KERNEL);
1141        if (!nic_dev->tx_intr_coalesce) {
1142                kfree(nic_dev->rx_intr_coalesce);
1143                return -ENOMEM;
1144        }
1145
1146        for (i = 0; i < nic_dev->max_qps; i++) {
1147                nic_dev->rx_intr_coalesce[i].pending_limt =
1148                        HINIC_DEAULT_TXRX_MSIX_PENDING_LIMIT;
1149                nic_dev->rx_intr_coalesce[i].coalesce_timer_cfg =
1150                        HINIC_DEAULT_TXRX_MSIX_COALESC_TIMER_CFG;
1151                nic_dev->rx_intr_coalesce[i].resend_timer_cfg =
1152                        HINIC_DEAULT_TXRX_MSIX_RESEND_TIMER_CFG;
1153                nic_dev->tx_intr_coalesce[i].pending_limt =
1154                        HINIC_DEAULT_TXRX_MSIX_PENDING_LIMIT;
1155                nic_dev->tx_intr_coalesce[i].coalesce_timer_cfg =
1156                        HINIC_DEAULT_TXRX_MSIX_COALESC_TIMER_CFG;
1157                nic_dev->tx_intr_coalesce[i].resend_timer_cfg =
1158                        HINIC_DEAULT_TXRX_MSIX_RESEND_TIMER_CFG;
1159        }
1160
1161        return 0;
1162}
1163
1164static void hinic_free_intr_coalesce(struct hinic_dev *nic_dev)
1165{
1166        kfree(nic_dev->tx_intr_coalesce);
1167        kfree(nic_dev->rx_intr_coalesce);
1168}
1169
1170/**
1171 * nic_dev_init - Initialize the NIC device
1172 * @pdev: the NIC pci device
1173 *
1174 * Return 0 - Success, negative - Failure
1175 **/
1176static int nic_dev_init(struct pci_dev *pdev)
1177{
1178        struct hinic_rx_mode_work *rx_mode_work;
1179        struct hinic_txq_stats *tx_stats;
1180        struct hinic_rxq_stats *rx_stats;
1181        struct hinic_dev *nic_dev;
1182        struct net_device *netdev;
1183        struct hinic_hwdev *hwdev;
1184        struct devlink *devlink;
1185        int err, num_qps;
1186
1187        devlink = hinic_devlink_alloc();
1188        if (!devlink) {
1189                dev_err(&pdev->dev, "Hinic devlink alloc failed\n");
1190                return -ENOMEM;
1191        }
1192
1193        hwdev = hinic_init_hwdev(pdev, devlink);
1194        if (IS_ERR(hwdev)) {
1195                dev_err(&pdev->dev, "Failed to initialize HW device\n");
1196                hinic_devlink_free(devlink);
1197                return PTR_ERR(hwdev);
1198        }
1199
1200        num_qps = hinic_hwdev_num_qps(hwdev);
1201        if (num_qps <= 0) {
1202                dev_err(&pdev->dev, "Invalid number of QPS\n");
1203                err = -EINVAL;
1204                goto err_num_qps;
1205        }
1206
1207        netdev = alloc_etherdev_mq(sizeof(*nic_dev), num_qps);
1208        if (!netdev) {
1209                dev_err(&pdev->dev, "Failed to allocate Ethernet device\n");
1210                err = -ENOMEM;
1211                goto err_alloc_etherdev;
1212        }
1213
1214        if (!HINIC_IS_VF(hwdev->hwif))
1215                netdev->netdev_ops = &hinic_netdev_ops;
1216        else
1217                netdev->netdev_ops = &hinicvf_netdev_ops;
1218
1219        netdev->max_mtu = ETH_MAX_MTU;
1220
1221        nic_dev = netdev_priv(netdev);
1222        nic_dev->netdev = netdev;
1223        nic_dev->hwdev  = hwdev;
1224        nic_dev->msg_enable = MSG_ENABLE_DEFAULT;
1225        nic_dev->flags = 0;
1226        nic_dev->txqs = NULL;
1227        nic_dev->rxqs = NULL;
1228        nic_dev->tx_weight = tx_weight;
1229        nic_dev->rx_weight = rx_weight;
1230        nic_dev->sq_depth = HINIC_SQ_DEPTH;
1231        nic_dev->rq_depth = HINIC_RQ_DEPTH;
1232        nic_dev->sriov_info.hwdev = hwdev;
1233        nic_dev->sriov_info.pdev = pdev;
1234        nic_dev->max_qps = num_qps;
1235        nic_dev->devlink = devlink;
1236
1237        hinic_set_ethtool_ops(netdev);
1238
1239        sema_init(&nic_dev->mgmt_lock, 1);
1240
1241        tx_stats = &nic_dev->tx_stats;
1242        rx_stats = &nic_dev->rx_stats;
1243
1244        u64_stats_init(&tx_stats->syncp);
1245        u64_stats_init(&rx_stats->syncp);
1246
1247        nic_dev->vlan_bitmap = devm_kzalloc(&pdev->dev,
1248                                            VLAN_BITMAP_SIZE(nic_dev),
1249                                            GFP_KERNEL);
1250        if (!nic_dev->vlan_bitmap) {
1251                err = -ENOMEM;
1252                goto err_vlan_bitmap;
1253        }
1254
1255        nic_dev->workq = create_singlethread_workqueue(HINIC_WQ_NAME);
1256        if (!nic_dev->workq) {
1257                err = -ENOMEM;
1258                goto err_workq;
1259        }
1260
1261        pci_set_drvdata(pdev, netdev);
1262
1263        err = hinic_port_get_mac(nic_dev, netdev->dev_addr);
1264        if (err) {
1265                dev_err(&pdev->dev, "Failed to get mac address\n");
1266                goto err_get_mac;
1267        }
1268
1269        if (!is_valid_ether_addr(netdev->dev_addr)) {
1270                if (!HINIC_IS_VF(nic_dev->hwdev->hwif)) {
1271                        dev_err(&pdev->dev, "Invalid MAC address\n");
1272                        err = -EIO;
1273                        goto err_add_mac;
1274                }
1275
1276                dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
1277                         netdev->dev_addr);
1278                eth_hw_addr_random(netdev);
1279        }
1280
1281        err = hinic_port_add_mac(nic_dev, netdev->dev_addr, 0);
1282        if (err && err != HINIC_PF_SET_VF_ALREADY) {
1283                dev_err(&pdev->dev, "Failed to add mac\n");
1284                goto err_add_mac;
1285        }
1286
1287        err = hinic_port_set_mtu(nic_dev, netdev->mtu);
1288        if (err) {
1289                dev_err(&pdev->dev, "Failed to set mtu\n");
1290                goto err_set_mtu;
1291        }
1292
1293        rx_mode_work = &nic_dev->rx_mode_work;
1294        INIT_WORK(&rx_mode_work->work, set_rx_mode);
1295
1296        netdev_features_init(netdev);
1297
1298        netif_carrier_off(netdev);
1299
1300        hinic_hwdev_cb_register(nic_dev->hwdev, HINIC_MGMT_MSG_CMD_LINK_STATUS,
1301                                nic_dev, link_status_event_handler);
1302        hinic_hwdev_cb_register(nic_dev->hwdev,
1303                                HINIC_MGMT_MSG_CMD_CABLE_PLUG_EVENT,
1304                                nic_dev, cable_plug_event);
1305        hinic_hwdev_cb_register(nic_dev->hwdev,
1306                                HINIC_MGMT_MSG_CMD_LINK_ERR_EVENT,
1307                                nic_dev, link_err_event);
1308
1309        err = set_features(nic_dev, 0, nic_dev->netdev->features, true);
1310        if (err)
1311                goto err_set_features;
1312
1313        /* enable pause and disable pfc by default */
1314        err = hinic_dcb_set_pfc(nic_dev->hwdev, 0, 0);
1315        if (err)
1316                goto err_set_pfc;
1317
1318        SET_NETDEV_DEV(netdev, &pdev->dev);
1319
1320        err = hinic_init_intr_coalesce(nic_dev);
1321        if (err) {
1322                dev_err(&pdev->dev, "Failed to init_intr_coalesce\n");
1323                goto err_init_intr;
1324        }
1325
1326        hinic_dbg_init(nic_dev);
1327
1328        hinic_func_tbl_dbgfs_init(nic_dev);
1329
1330        err = hinic_func_table_debug_add(nic_dev);
1331        if (err) {
1332                dev_err(&pdev->dev, "Failed to add func_table debug\n");
1333                goto err_add_func_table_dbg;
1334        }
1335
1336        err = register_netdev(netdev);
1337        if (err) {
1338                dev_err(&pdev->dev, "Failed to register netdev\n");
1339                goto err_reg_netdev;
1340        }
1341
1342        return 0;
1343
1344err_reg_netdev:
1345        hinic_func_table_debug_rem(nic_dev);
1346err_add_func_table_dbg:
1347        hinic_func_tbl_dbgfs_uninit(nic_dev);
1348        hinic_dbg_uninit(nic_dev);
1349        hinic_free_intr_coalesce(nic_dev);
1350err_init_intr:
1351err_set_pfc:
1352err_set_features:
1353        hinic_hwdev_cb_unregister(nic_dev->hwdev,
1354                                  HINIC_MGMT_MSG_CMD_LINK_ERR_EVENT);
1355        hinic_hwdev_cb_unregister(nic_dev->hwdev,
1356                                  HINIC_MGMT_MSG_CMD_CABLE_PLUG_EVENT);
1357        hinic_hwdev_cb_unregister(nic_dev->hwdev,
1358                                  HINIC_MGMT_MSG_CMD_LINK_STATUS);
1359        cancel_work_sync(&rx_mode_work->work);
1360
1361err_set_mtu:
1362        hinic_port_del_mac(nic_dev, netdev->dev_addr, 0);
1363err_add_mac:
1364err_get_mac:
1365        pci_set_drvdata(pdev, NULL);
1366        destroy_workqueue(nic_dev->workq);
1367err_workq:
1368err_vlan_bitmap:
1369        free_netdev(netdev);
1370
1371err_alloc_etherdev:
1372err_num_qps:
1373        hinic_free_hwdev(hwdev);
1374        hinic_devlink_free(devlink);
1375        return err;
1376}
1377
1378static int hinic_probe(struct pci_dev *pdev,
1379                       const struct pci_device_id *id)
1380{
1381        int err = pci_enable_device(pdev);
1382
1383        if (err) {
1384                dev_err(&pdev->dev, "Failed to enable PCI device\n");
1385                return err;
1386        }
1387
1388        err = pci_request_regions(pdev, HINIC_DRV_NAME);
1389        if (err) {
1390                dev_err(&pdev->dev, "Failed to request PCI regions\n");
1391                goto err_pci_regions;
1392        }
1393
1394        pci_set_master(pdev);
1395
1396        err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1397        if (err) {
1398                dev_warn(&pdev->dev, "Couldn't set 64-bit DMA mask\n");
1399                err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1400                if (err) {
1401                        dev_err(&pdev->dev, "Failed to set DMA mask\n");
1402                        goto err_dma_mask;
1403                }
1404        }
1405
1406        err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1407        if (err) {
1408                dev_warn(&pdev->dev,
1409                         "Couldn't set 64-bit consistent DMA mask\n");
1410                err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1411                if (err) {
1412                        dev_err(&pdev->dev,
1413                                "Failed to set consistent DMA mask\n");
1414                        goto err_dma_consistent_mask;
1415                }
1416        }
1417
1418        err = nic_dev_init(pdev);
1419        if (err) {
1420                dev_err(&pdev->dev, "Failed to initialize NIC device\n");
1421                goto err_nic_dev_init;
1422        }
1423
1424        dev_info(&pdev->dev, "HiNIC driver - probed\n");
1425        return 0;
1426
1427err_nic_dev_init:
1428err_dma_consistent_mask:
1429err_dma_mask:
1430        pci_release_regions(pdev);
1431
1432err_pci_regions:
1433        pci_disable_device(pdev);
1434        return err;
1435}
1436
1437#define HINIC_WAIT_SRIOV_CFG_TIMEOUT    15000
1438
1439static void wait_sriov_cfg_complete(struct hinic_dev *nic_dev)
1440{
1441        struct hinic_sriov_info *sriov_info = &nic_dev->sriov_info;
1442        u32 loop_cnt = 0;
1443
1444        set_bit(HINIC_FUNC_REMOVE, &sriov_info->state);
1445        usleep_range(9900, 10000);
1446
1447        while (loop_cnt < HINIC_WAIT_SRIOV_CFG_TIMEOUT) {
1448                if (!test_bit(HINIC_SRIOV_ENABLE, &sriov_info->state) &&
1449                    !test_bit(HINIC_SRIOV_DISABLE, &sriov_info->state))
1450                        return;
1451
1452                usleep_range(9900, 10000);
1453                loop_cnt++;
1454        }
1455}
1456
1457static void hinic_remove(struct pci_dev *pdev)
1458{
1459        struct net_device *netdev = pci_get_drvdata(pdev);
1460        struct hinic_dev *nic_dev = netdev_priv(netdev);
1461        struct devlink *devlink = nic_dev->devlink;
1462        struct hinic_rx_mode_work *rx_mode_work;
1463
1464        if (!HINIC_IS_VF(nic_dev->hwdev->hwif)) {
1465                wait_sriov_cfg_complete(nic_dev);
1466                hinic_pci_sriov_disable(pdev);
1467        }
1468
1469        unregister_netdev(netdev);
1470
1471        hinic_func_table_debug_rem(nic_dev);
1472
1473        hinic_func_tbl_dbgfs_uninit(nic_dev);
1474
1475        hinic_dbg_uninit(nic_dev);
1476
1477        hinic_free_intr_coalesce(nic_dev);
1478
1479        hinic_port_del_mac(nic_dev, netdev->dev_addr, 0);
1480
1481        hinic_hwdev_cb_unregister(nic_dev->hwdev,
1482                                  HINIC_MGMT_MSG_CMD_LINK_ERR_EVENT);
1483        hinic_hwdev_cb_unregister(nic_dev->hwdev,
1484                                  HINIC_MGMT_MSG_CMD_CABLE_PLUG_EVENT);
1485        hinic_hwdev_cb_unregister(nic_dev->hwdev,
1486                                  HINIC_MGMT_MSG_CMD_LINK_STATUS);
1487
1488        rx_mode_work = &nic_dev->rx_mode_work;
1489        cancel_work_sync(&rx_mode_work->work);
1490
1491        pci_set_drvdata(pdev, NULL);
1492
1493        destroy_workqueue(nic_dev->workq);
1494
1495        hinic_free_hwdev(nic_dev->hwdev);
1496
1497        free_netdev(netdev);
1498
1499        hinic_devlink_free(devlink);
1500
1501        pci_release_regions(pdev);
1502        pci_disable_device(pdev);
1503
1504        dev_info(&pdev->dev, "HiNIC driver - removed\n");
1505}
1506
1507static void hinic_shutdown(struct pci_dev *pdev)
1508{
1509        pci_disable_device(pdev);
1510}
1511
1512static const struct pci_device_id hinic_pci_table[] = {
1513        { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_QUAD_PORT_25GE), 0},
1514        { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_100GE), 0},
1515        { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_100GE_MEZZ), 0},
1516        { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_QUAD_PORT_25GE_MEZZ), 0},
1517        { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_VF), 0},
1518        { 0, 0}
1519};
1520MODULE_DEVICE_TABLE(pci, hinic_pci_table);
1521
1522static struct pci_driver hinic_driver = {
1523        .name           = HINIC_DRV_NAME,
1524        .id_table       = hinic_pci_table,
1525        .probe          = hinic_probe,
1526        .remove         = hinic_remove,
1527        .shutdown       = hinic_shutdown,
1528        .sriov_configure = hinic_pci_sriov_configure,
1529};
1530
1531static int __init hinic_module_init(void)
1532{
1533        hinic_dbg_register_debugfs(HINIC_DRV_NAME);
1534        return pci_register_driver(&hinic_driver);
1535}
1536
1537static void __exit hinic_module_exit(void)
1538{
1539        pci_unregister_driver(&hinic_driver);
1540        hinic_dbg_unregister_debugfs();
1541}
1542
1543module_init(hinic_module_init);
1544module_exit(hinic_module_exit);
1545