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