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