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