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 <linux/bitops.h> 22 #include <linux/bitmap.h> 23 #include <linux/delay.h> 24 #include <linux/err.h> 25 26 #include "hinic_hw_qp.h" 27 #include "hinic_hw_dev.h" 28 #include "hinic_port.h" 29 #include "hinic_tx.h" 30 #include "hinic_rx.h" 31 #include "hinic_dev.h" 32 33 MODULE_AUTHOR("Huawei Technologies CO., Ltd"); 34 MODULE_DESCRIPTION("Huawei Intelligent NIC driver"); 35 MODULE_LICENSE("GPL"); 36 37 static unsigned int tx_weight = 64; 38 module_param(tx_weight, uint, 0644); 39 MODULE_PARM_DESC(tx_weight, "Number Tx packets for NAPI budget (default=64)"); 40 41 static unsigned int rx_weight = 64; 42 module_param(rx_weight, uint, 0644); 43 MODULE_PARM_DESC(rx_weight, "Number Rx packets for NAPI budget (default=64)"); 44 45 #define HINIC_DEV_ID_QUAD_PORT_25GE 0x1822 46 #define HINIC_DEV_ID_DUAL_PORT_100GE 0x0200 47 #define HINIC_DEV_ID_DUAL_PORT_100GE_MEZZ 0x0205 48 #define HINIC_DEV_ID_QUAD_PORT_25GE_MEZZ 0x0210 49 50 #define HINIC_WQ_NAME "hinic_dev" 51 52 #define MSG_ENABLE_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \ 53 NETIF_MSG_IFUP | \ 54 NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR) 55 56 #define HINIC_LRO_MAX_WQE_NUM_DEFAULT 8 57 58 #define HINIC_LRO_RX_TIMER_DEFAULT 16 59 60 #define VLAN_BITMAP_SIZE(nic_dev) (ALIGN(VLAN_N_VID, 8) / 8) 61 62 #define work_to_rx_mode_work(work) \ 63 container_of(work, struct hinic_rx_mode_work, work) 64 65 #define rx_mode_work_to_nic_dev(rx_mode_work) \ 66 container_of(rx_mode_work, struct hinic_dev, rx_mode_work) 67 68 static int change_mac_addr(struct net_device *netdev, const u8 *addr); 69 70 static int set_features(struct hinic_dev *nic_dev, 71 netdev_features_t pre_features, 72 netdev_features_t features, bool force_change); 73 74 static void set_link_speed(struct ethtool_link_ksettings *link_ksettings, 75 enum hinic_speed speed) 76 { 77 switch (speed) { 78 case HINIC_SPEED_10MB_LINK: 79 link_ksettings->base.speed = SPEED_10; 80 break; 81 82 case HINIC_SPEED_100MB_LINK: 83 link_ksettings->base.speed = SPEED_100; 84 break; 85 86 case HINIC_SPEED_1000MB_LINK: 87 link_ksettings->base.speed = SPEED_1000; 88 break; 89 90 case HINIC_SPEED_10GB_LINK: 91 link_ksettings->base.speed = SPEED_10000; 92 break; 93 94 case HINIC_SPEED_25GB_LINK: 95 link_ksettings->base.speed = SPEED_25000; 96 break; 97 98 case HINIC_SPEED_40GB_LINK: 99 link_ksettings->base.speed = SPEED_40000; 100 break; 101 102 case HINIC_SPEED_100GB_LINK: 103 link_ksettings->base.speed = SPEED_100000; 104 break; 105 106 default: 107 link_ksettings->base.speed = SPEED_UNKNOWN; 108 break; 109 } 110 } 111 112 static int hinic_get_link_ksettings(struct net_device *netdev, 113 struct ethtool_link_ksettings 114 *link_ksettings) 115 { 116 struct hinic_dev *nic_dev = netdev_priv(netdev); 117 enum hinic_port_link_state link_state; 118 struct hinic_port_cap port_cap; 119 int err; 120 121 ethtool_link_ksettings_zero_link_mode(link_ksettings, advertising); 122 ethtool_link_ksettings_add_link_mode(link_ksettings, supported, 123 Autoneg); 124 125 link_ksettings->base.speed = SPEED_UNKNOWN; 126 link_ksettings->base.autoneg = AUTONEG_DISABLE; 127 link_ksettings->base.duplex = DUPLEX_UNKNOWN; 128 129 err = hinic_port_get_cap(nic_dev, &port_cap); 130 if (err) { 131 netif_err(nic_dev, drv, netdev, 132 "Failed to get port capabilities\n"); 133 return err; 134 } 135 136 err = hinic_port_link_state(nic_dev, &link_state); 137 if (err) { 138 netif_err(nic_dev, drv, netdev, 139 "Failed to get port link state\n"); 140 return err; 141 } 142 143 if (link_state != HINIC_LINK_STATE_UP) { 144 netif_info(nic_dev, drv, netdev, "No link\n"); 145 return err; 146 } 147 148 set_link_speed(link_ksettings, port_cap.speed); 149 150 if (!!(port_cap.autoneg_cap & HINIC_AUTONEG_SUPPORTED)) 151 ethtool_link_ksettings_add_link_mode(link_ksettings, 152 advertising, Autoneg); 153 154 if (port_cap.autoneg_state == HINIC_AUTONEG_ACTIVE) 155 link_ksettings->base.autoneg = AUTONEG_ENABLE; 156 157 link_ksettings->base.duplex = (port_cap.duplex == HINIC_DUPLEX_FULL) ? 158 DUPLEX_FULL : DUPLEX_HALF; 159 return 0; 160 } 161 162 static void hinic_get_drvinfo(struct net_device *netdev, 163 struct ethtool_drvinfo *info) 164 { 165 struct hinic_dev *nic_dev = netdev_priv(netdev); 166 struct hinic_hwdev *hwdev = nic_dev->hwdev; 167 struct hinic_hwif *hwif = hwdev->hwif; 168 169 strlcpy(info->driver, HINIC_DRV_NAME, sizeof(info->driver)); 170 strlcpy(info->bus_info, pci_name(hwif->pdev), sizeof(info->bus_info)); 171 } 172 173 static void hinic_get_ringparam(struct net_device *netdev, 174 struct ethtool_ringparam *ring) 175 { 176 ring->rx_max_pending = HINIC_RQ_DEPTH; 177 ring->tx_max_pending = HINIC_SQ_DEPTH; 178 ring->rx_pending = HINIC_RQ_DEPTH; 179 ring->tx_pending = HINIC_SQ_DEPTH; 180 } 181 182 static void hinic_get_channels(struct net_device *netdev, 183 struct ethtool_channels *channels) 184 { 185 struct hinic_dev *nic_dev = netdev_priv(netdev); 186 struct hinic_hwdev *hwdev = nic_dev->hwdev; 187 188 channels->max_rx = hwdev->nic_cap.max_qps; 189 channels->max_tx = hwdev->nic_cap.max_qps; 190 channels->max_other = 0; 191 channels->max_combined = 0; 192 channels->rx_count = hinic_hwdev_num_qps(hwdev); 193 channels->tx_count = hinic_hwdev_num_qps(hwdev); 194 channels->other_count = 0; 195 channels->combined_count = 0; 196 } 197 198 static const struct ethtool_ops hinic_ethtool_ops = { 199 .get_link_ksettings = hinic_get_link_ksettings, 200 .get_drvinfo = hinic_get_drvinfo, 201 .get_link = ethtool_op_get_link, 202 .get_ringparam = hinic_get_ringparam, 203 .get_channels = hinic_get_channels, 204 }; 205 206 static void update_rx_stats(struct hinic_dev *nic_dev, struct hinic_rxq *rxq) 207 { 208 struct hinic_rxq_stats *nic_rx_stats = &nic_dev->rx_stats; 209 struct hinic_rxq_stats rx_stats; 210 211 u64_stats_init(&rx_stats.syncp); 212 213 hinic_rxq_get_stats(rxq, &rx_stats); 214 215 u64_stats_update_begin(&nic_rx_stats->syncp); 216 nic_rx_stats->bytes += rx_stats.bytes; 217 nic_rx_stats->pkts += rx_stats.pkts; 218 u64_stats_update_end(&nic_rx_stats->syncp); 219 220 hinic_rxq_clean_stats(rxq); 221 } 222 223 static void update_tx_stats(struct hinic_dev *nic_dev, struct hinic_txq *txq) 224 { 225 struct hinic_txq_stats *nic_tx_stats = &nic_dev->tx_stats; 226 struct hinic_txq_stats tx_stats; 227 228 u64_stats_init(&tx_stats.syncp); 229 230 hinic_txq_get_stats(txq, &tx_stats); 231 232 u64_stats_update_begin(&nic_tx_stats->syncp); 233 nic_tx_stats->bytes += tx_stats.bytes; 234 nic_tx_stats->pkts += tx_stats.pkts; 235 nic_tx_stats->tx_busy += tx_stats.tx_busy; 236 nic_tx_stats->tx_wake += tx_stats.tx_wake; 237 nic_tx_stats->tx_dropped += tx_stats.tx_dropped; 238 u64_stats_update_end(&nic_tx_stats->syncp); 239 240 hinic_txq_clean_stats(txq); 241 } 242 243 static void update_nic_stats(struct hinic_dev *nic_dev) 244 { 245 int i, num_qps = hinic_hwdev_num_qps(nic_dev->hwdev); 246 247 for (i = 0; i < num_qps; i++) 248 update_rx_stats(nic_dev, &nic_dev->rxqs[i]); 249 250 for (i = 0; i < num_qps; i++) 251 update_tx_stats(nic_dev, &nic_dev->txqs[i]); 252 } 253 254 /** 255 * create_txqs - Create the Logical Tx Queues of specific NIC device 256 * @nic_dev: the specific NIC device 257 * 258 * Return 0 - Success, negative - Failure 259 **/ 260 static int create_txqs(struct hinic_dev *nic_dev) 261 { 262 int err, i, j, num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev); 263 struct net_device *netdev = nic_dev->netdev; 264 size_t txq_size; 265 266 if (nic_dev->txqs) 267 return -EINVAL; 268 269 txq_size = num_txqs * sizeof(*nic_dev->txqs); 270 nic_dev->txqs = devm_kzalloc(&netdev->dev, txq_size, GFP_KERNEL); 271 if (!nic_dev->txqs) 272 return -ENOMEM; 273 274 for (i = 0; i < num_txqs; i++) { 275 struct hinic_sq *sq = hinic_hwdev_get_sq(nic_dev->hwdev, i); 276 277 err = hinic_init_txq(&nic_dev->txqs[i], sq, netdev); 278 if (err) { 279 netif_err(nic_dev, drv, netdev, 280 "Failed to init Txq\n"); 281 goto err_init_txq; 282 } 283 } 284 285 return 0; 286 287 err_init_txq: 288 for (j = 0; j < i; j++) 289 hinic_clean_txq(&nic_dev->txqs[j]); 290 291 devm_kfree(&netdev->dev, nic_dev->txqs); 292 return err; 293 } 294 295 /** 296 * free_txqs - Free the Logical Tx Queues of specific NIC device 297 * @nic_dev: the specific NIC device 298 **/ 299 static void free_txqs(struct hinic_dev *nic_dev) 300 { 301 int i, num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev); 302 struct net_device *netdev = nic_dev->netdev; 303 304 if (!nic_dev->txqs) 305 return; 306 307 for (i = 0; i < num_txqs; i++) 308 hinic_clean_txq(&nic_dev->txqs[i]); 309 310 devm_kfree(&netdev->dev, nic_dev->txqs); 311 nic_dev->txqs = NULL; 312 } 313 314 /** 315 * create_txqs - Create the Logical Rx Queues of specific NIC device 316 * @nic_dev: the specific NIC device 317 * 318 * Return 0 - Success, negative - Failure 319 **/ 320 static int create_rxqs(struct hinic_dev *nic_dev) 321 { 322 int err, i, j, num_rxqs = hinic_hwdev_num_qps(nic_dev->hwdev); 323 struct net_device *netdev = nic_dev->netdev; 324 size_t rxq_size; 325 326 if (nic_dev->rxqs) 327 return -EINVAL; 328 329 rxq_size = num_rxqs * sizeof(*nic_dev->rxqs); 330 nic_dev->rxqs = devm_kzalloc(&netdev->dev, rxq_size, GFP_KERNEL); 331 if (!nic_dev->rxqs) 332 return -ENOMEM; 333 334 for (i = 0; i < num_rxqs; i++) { 335 struct hinic_rq *rq = hinic_hwdev_get_rq(nic_dev->hwdev, i); 336 337 err = hinic_init_rxq(&nic_dev->rxqs[i], rq, netdev); 338 if (err) { 339 netif_err(nic_dev, drv, netdev, 340 "Failed to init rxq\n"); 341 goto err_init_rxq; 342 } 343 } 344 345 return 0; 346 347 err_init_rxq: 348 for (j = 0; j < i; j++) 349 hinic_clean_rxq(&nic_dev->rxqs[j]); 350 351 devm_kfree(&netdev->dev, nic_dev->rxqs); 352 return err; 353 } 354 355 /** 356 * free_txqs - Free the Logical Rx Queues of specific NIC device 357 * @nic_dev: the specific NIC device 358 **/ 359 static void free_rxqs(struct hinic_dev *nic_dev) 360 { 361 int i, num_rxqs = hinic_hwdev_num_qps(nic_dev->hwdev); 362 struct net_device *netdev = nic_dev->netdev; 363 364 if (!nic_dev->rxqs) 365 return; 366 367 for (i = 0; i < num_rxqs; i++) 368 hinic_clean_rxq(&nic_dev->rxqs[i]); 369 370 devm_kfree(&netdev->dev, nic_dev->rxqs); 371 nic_dev->rxqs = NULL; 372 } 373 374 static int hinic_configure_max_qnum(struct hinic_dev *nic_dev) 375 { 376 int err; 377 378 err = hinic_set_max_qnum(nic_dev, nic_dev->hwdev->nic_cap.max_qps); 379 if (err) 380 return err; 381 382 return 0; 383 } 384 385 static int hinic_open(struct net_device *netdev) 386 { 387 struct hinic_dev *nic_dev = netdev_priv(netdev); 388 enum hinic_port_link_state link_state; 389 int err, ret, num_qps; 390 391 if (!(nic_dev->flags & HINIC_INTF_UP)) { 392 err = hinic_hwdev_ifup(nic_dev->hwdev); 393 if (err) { 394 netif_err(nic_dev, drv, netdev, 395 "Failed - HW interface up\n"); 396 return err; 397 } 398 } 399 400 err = create_txqs(nic_dev); 401 if (err) { 402 netif_err(nic_dev, drv, netdev, 403 "Failed to create Tx queues\n"); 404 goto err_create_txqs; 405 } 406 407 err = create_rxqs(nic_dev); 408 if (err) { 409 netif_err(nic_dev, drv, netdev, 410 "Failed to create Rx queues\n"); 411 goto err_create_rxqs; 412 } 413 414 err = hinic_configure_max_qnum(nic_dev); 415 if (err) { 416 netif_err(nic_dev, drv, nic_dev->netdev, 417 "Failed to configure the maximum number of queues\n"); 418 goto err_port_state; 419 } 420 421 num_qps = hinic_hwdev_num_qps(nic_dev->hwdev); 422 netif_set_real_num_tx_queues(netdev, num_qps); 423 netif_set_real_num_rx_queues(netdev, num_qps); 424 425 err = hinic_port_set_state(nic_dev, HINIC_PORT_ENABLE); 426 if (err) { 427 netif_err(nic_dev, drv, netdev, 428 "Failed to set port state\n"); 429 goto err_port_state; 430 } 431 432 err = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_ENABLE); 433 if (err) { 434 netif_err(nic_dev, drv, netdev, 435 "Failed to set func port state\n"); 436 goto err_func_port_state; 437 } 438 439 /* Wait up to 3 sec between port enable to link state */ 440 msleep(3000); 441 442 down(&nic_dev->mgmt_lock); 443 444 err = hinic_port_link_state(nic_dev, &link_state); 445 if (err) { 446 netif_err(nic_dev, drv, netdev, "Failed to get link state\n"); 447 goto err_port_link; 448 } 449 450 if (link_state == HINIC_LINK_STATE_UP) 451 nic_dev->flags |= HINIC_LINK_UP; 452 453 nic_dev->flags |= HINIC_INTF_UP; 454 455 if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) == 456 (HINIC_LINK_UP | HINIC_INTF_UP)) { 457 netif_info(nic_dev, drv, netdev, "link + intf UP\n"); 458 netif_carrier_on(netdev); 459 netif_tx_wake_all_queues(netdev); 460 } 461 462 up(&nic_dev->mgmt_lock); 463 464 netif_info(nic_dev, drv, netdev, "HINIC_INTF is UP\n"); 465 return 0; 466 467 err_port_link: 468 up(&nic_dev->mgmt_lock); 469 ret = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE); 470 if (ret) 471 netif_warn(nic_dev, drv, netdev, 472 "Failed to revert func port state\n"); 473 474 err_func_port_state: 475 ret = hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE); 476 if (ret) 477 netif_warn(nic_dev, drv, netdev, 478 "Failed to revert port state\n"); 479 480 err_port_state: 481 free_rxqs(nic_dev); 482 483 err_create_rxqs: 484 free_txqs(nic_dev); 485 486 err_create_txqs: 487 if (!(nic_dev->flags & HINIC_INTF_UP)) 488 hinic_hwdev_ifdown(nic_dev->hwdev); 489 return err; 490 } 491 492 static int hinic_close(struct net_device *netdev) 493 { 494 struct hinic_dev *nic_dev = netdev_priv(netdev); 495 unsigned int flags; 496 int err; 497 498 down(&nic_dev->mgmt_lock); 499 500 flags = nic_dev->flags; 501 nic_dev->flags &= ~HINIC_INTF_UP; 502 503 netif_carrier_off(netdev); 504 netif_tx_disable(netdev); 505 506 update_nic_stats(nic_dev); 507 508 up(&nic_dev->mgmt_lock); 509 510 err = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE); 511 if (err) { 512 netif_err(nic_dev, drv, netdev, 513 "Failed to set func port state\n"); 514 nic_dev->flags |= (flags & HINIC_INTF_UP); 515 return err; 516 } 517 518 err = hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE); 519 if (err) { 520 netif_err(nic_dev, drv, netdev, "Failed to set port state\n"); 521 nic_dev->flags |= (flags & HINIC_INTF_UP); 522 return err; 523 } 524 525 free_rxqs(nic_dev); 526 free_txqs(nic_dev); 527 528 if (flags & HINIC_INTF_UP) 529 hinic_hwdev_ifdown(nic_dev->hwdev); 530 531 netif_info(nic_dev, drv, netdev, "HINIC_INTF is DOWN\n"); 532 return 0; 533 } 534 535 static int hinic_change_mtu(struct net_device *netdev, int new_mtu) 536 { 537 struct hinic_dev *nic_dev = netdev_priv(netdev); 538 int err; 539 540 netif_info(nic_dev, drv, netdev, "set_mtu = %d\n", new_mtu); 541 542 err = hinic_port_set_mtu(nic_dev, new_mtu); 543 if (err) 544 netif_err(nic_dev, drv, netdev, "Failed to set port mtu\n"); 545 else 546 netdev->mtu = new_mtu; 547 548 return err; 549 } 550 551 /** 552 * change_mac_addr - change the main mac address of network device 553 * @netdev: network device 554 * @addr: mac address to set 555 * 556 * Return 0 - Success, negative - Failure 557 **/ 558 static int change_mac_addr(struct net_device *netdev, const u8 *addr) 559 { 560 struct hinic_dev *nic_dev = netdev_priv(netdev); 561 u16 vid = 0; 562 int err; 563 564 if (!is_valid_ether_addr(addr)) 565 return -EADDRNOTAVAIL; 566 567 netif_info(nic_dev, drv, netdev, "change mac addr = %02x %02x %02x %02x %02x %02x\n", 568 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); 569 570 down(&nic_dev->mgmt_lock); 571 572 do { 573 err = hinic_port_del_mac(nic_dev, netdev->dev_addr, vid); 574 if (err) { 575 netif_err(nic_dev, drv, netdev, 576 "Failed to delete mac\n"); 577 break; 578 } 579 580 err = hinic_port_add_mac(nic_dev, addr, vid); 581 if (err) { 582 netif_err(nic_dev, drv, netdev, "Failed to add mac\n"); 583 break; 584 } 585 586 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1); 587 } while (vid != VLAN_N_VID); 588 589 up(&nic_dev->mgmt_lock); 590 return err; 591 } 592 593 static int hinic_set_mac_addr(struct net_device *netdev, void *addr) 594 { 595 unsigned char new_mac[ETH_ALEN]; 596 struct sockaddr *saddr = addr; 597 int err; 598 599 memcpy(new_mac, saddr->sa_data, ETH_ALEN); 600 601 err = change_mac_addr(netdev, new_mac); 602 if (!err) 603 memcpy(netdev->dev_addr, new_mac, ETH_ALEN); 604 605 return err; 606 } 607 608 /** 609 * add_mac_addr - add mac address to network device 610 * @netdev: network device 611 * @addr: mac address to add 612 * 613 * Return 0 - Success, negative - Failure 614 **/ 615 static int add_mac_addr(struct net_device *netdev, const u8 *addr) 616 { 617 struct hinic_dev *nic_dev = netdev_priv(netdev); 618 u16 vid = 0; 619 int err; 620 621 netif_info(nic_dev, drv, netdev, "set 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_add_mac(nic_dev, addr, vid); 628 if (err) { 629 netif_err(nic_dev, drv, netdev, "Failed to add mac\n"); 630 break; 631 } 632 633 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1); 634 } while (vid != VLAN_N_VID); 635 636 up(&nic_dev->mgmt_lock); 637 return err; 638 } 639 640 /** 641 * remove_mac_addr - remove mac address from network device 642 * @netdev: network device 643 * @addr: mac address to remove 644 * 645 * Return 0 - Success, negative - Failure 646 **/ 647 static int remove_mac_addr(struct net_device *netdev, const u8 *addr) 648 { 649 struct hinic_dev *nic_dev = netdev_priv(netdev); 650 u16 vid = 0; 651 int err; 652 653 if (!is_valid_ether_addr(addr)) 654 return -EADDRNOTAVAIL; 655 656 netif_info(nic_dev, drv, netdev, "remove mac addr = %02x %02x %02x %02x %02x %02x\n", 657 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); 658 659 down(&nic_dev->mgmt_lock); 660 661 do { 662 err = hinic_port_del_mac(nic_dev, addr, vid); 663 if (err) { 664 netif_err(nic_dev, drv, netdev, 665 "Failed to delete mac\n"); 666 break; 667 } 668 669 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1); 670 } while (vid != VLAN_N_VID); 671 672 up(&nic_dev->mgmt_lock); 673 return err; 674 } 675 676 static int hinic_vlan_rx_add_vid(struct net_device *netdev, 677 __always_unused __be16 proto, u16 vid) 678 { 679 struct hinic_dev *nic_dev = netdev_priv(netdev); 680 int ret, err; 681 682 netif_info(nic_dev, drv, netdev, "add vid = %d\n", vid); 683 684 down(&nic_dev->mgmt_lock); 685 686 err = hinic_port_add_vlan(nic_dev, vid); 687 if (err) { 688 netif_err(nic_dev, drv, netdev, "Failed to add vlan\n"); 689 goto err_vlan_add; 690 } 691 692 err = hinic_port_add_mac(nic_dev, netdev->dev_addr, vid); 693 if (err) { 694 netif_err(nic_dev, drv, netdev, "Failed to set mac\n"); 695 goto err_add_mac; 696 } 697 698 bitmap_set(nic_dev->vlan_bitmap, vid, 1); 699 700 up(&nic_dev->mgmt_lock); 701 return 0; 702 703 err_add_mac: 704 ret = hinic_port_del_vlan(nic_dev, vid); 705 if (ret) 706 netif_err(nic_dev, drv, netdev, 707 "Failed to revert by removing vlan\n"); 708 709 err_vlan_add: 710 up(&nic_dev->mgmt_lock); 711 return err; 712 } 713 714 static int hinic_vlan_rx_kill_vid(struct net_device *netdev, 715 __always_unused __be16 proto, u16 vid) 716 { 717 struct hinic_dev *nic_dev = netdev_priv(netdev); 718 int err; 719 720 netif_info(nic_dev, drv, netdev, "remove vid = %d\n", vid); 721 722 down(&nic_dev->mgmt_lock); 723 724 err = hinic_port_del_vlan(nic_dev, vid); 725 if (err) { 726 netif_err(nic_dev, drv, netdev, "Failed to delete vlan\n"); 727 goto err_del_vlan; 728 } 729 730 bitmap_clear(nic_dev->vlan_bitmap, vid, 1); 731 732 up(&nic_dev->mgmt_lock); 733 return 0; 734 735 err_del_vlan: 736 up(&nic_dev->mgmt_lock); 737 return err; 738 } 739 740 static void set_rx_mode(struct work_struct *work) 741 { 742 struct hinic_rx_mode_work *rx_mode_work = work_to_rx_mode_work(work); 743 struct hinic_dev *nic_dev = rx_mode_work_to_nic_dev(rx_mode_work); 744 745 netif_info(nic_dev, drv, nic_dev->netdev, "set rx mode work\n"); 746 747 hinic_port_set_rx_mode(nic_dev, rx_mode_work->rx_mode); 748 749 __dev_uc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr); 750 __dev_mc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr); 751 } 752 753 static void hinic_set_rx_mode(struct net_device *netdev) 754 { 755 struct hinic_dev *nic_dev = netdev_priv(netdev); 756 struct hinic_rx_mode_work *rx_mode_work; 757 u32 rx_mode; 758 759 rx_mode_work = &nic_dev->rx_mode_work; 760 761 rx_mode = HINIC_RX_MODE_UC | 762 HINIC_RX_MODE_MC | 763 HINIC_RX_MODE_BC; 764 765 if (netdev->flags & IFF_PROMISC) 766 rx_mode |= HINIC_RX_MODE_PROMISC; 767 else if (netdev->flags & IFF_ALLMULTI) 768 rx_mode |= HINIC_RX_MODE_MC_ALL; 769 770 rx_mode_work->rx_mode = rx_mode; 771 772 queue_work(nic_dev->workq, &rx_mode_work->work); 773 } 774 775 static void hinic_tx_timeout(struct net_device *netdev) 776 { 777 struct hinic_dev *nic_dev = netdev_priv(netdev); 778 779 netif_err(nic_dev, drv, netdev, "Tx timeout\n"); 780 } 781 782 static void hinic_get_stats64(struct net_device *netdev, 783 struct rtnl_link_stats64 *stats) 784 { 785 struct hinic_dev *nic_dev = netdev_priv(netdev); 786 struct hinic_rxq_stats *nic_rx_stats; 787 struct hinic_txq_stats *nic_tx_stats; 788 789 nic_rx_stats = &nic_dev->rx_stats; 790 nic_tx_stats = &nic_dev->tx_stats; 791 792 down(&nic_dev->mgmt_lock); 793 794 if (nic_dev->flags & HINIC_INTF_UP) 795 update_nic_stats(nic_dev); 796 797 up(&nic_dev->mgmt_lock); 798 799 stats->rx_bytes = nic_rx_stats->bytes; 800 stats->rx_packets = nic_rx_stats->pkts; 801 802 stats->tx_bytes = nic_tx_stats->bytes; 803 stats->tx_packets = nic_tx_stats->pkts; 804 stats->tx_errors = nic_tx_stats->tx_dropped; 805 } 806 807 static int hinic_set_features(struct net_device *netdev, 808 netdev_features_t features) 809 { 810 struct hinic_dev *nic_dev = netdev_priv(netdev); 811 812 return set_features(nic_dev, nic_dev->netdev->features, 813 features, false); 814 } 815 816 static netdev_features_t hinic_fix_features(struct net_device *netdev, 817 netdev_features_t features) 818 { 819 struct hinic_dev *nic_dev = netdev_priv(netdev); 820 821 /* If Rx checksum is disabled, then LRO should also be disabled */ 822 if (!(features & NETIF_F_RXCSUM)) { 823 netif_info(nic_dev, drv, netdev, "disabling LRO as RXCSUM is off\n"); 824 features &= ~NETIF_F_LRO; 825 } 826 827 return features; 828 } 829 830 static const struct net_device_ops hinic_netdev_ops = { 831 .ndo_open = hinic_open, 832 .ndo_stop = hinic_close, 833 .ndo_change_mtu = hinic_change_mtu, 834 .ndo_set_mac_address = hinic_set_mac_addr, 835 .ndo_validate_addr = eth_validate_addr, 836 .ndo_vlan_rx_add_vid = hinic_vlan_rx_add_vid, 837 .ndo_vlan_rx_kill_vid = hinic_vlan_rx_kill_vid, 838 .ndo_set_rx_mode = hinic_set_rx_mode, 839 .ndo_start_xmit = hinic_xmit_frame, 840 .ndo_tx_timeout = hinic_tx_timeout, 841 .ndo_get_stats64 = hinic_get_stats64, 842 .ndo_fix_features = hinic_fix_features, 843 .ndo_set_features = hinic_set_features, 844 845 }; 846 847 static void netdev_features_init(struct net_device *netdev) 848 { 849 netdev->hw_features = NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_IP_CSUM | 850 NETIF_F_IPV6_CSUM | NETIF_F_TSO | NETIF_F_TSO6 | 851 NETIF_F_RXCSUM | NETIF_F_LRO; 852 853 netdev->vlan_features = netdev->hw_features; 854 855 netdev->features = netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER; 856 } 857 858 /** 859 * link_status_event_handler - link event handler 860 * @handle: nic device for the handler 861 * @buf_in: input buffer 862 * @in_size: input size 863 * @buf_in: output buffer 864 * @out_size: returned output size 865 * 866 * Return 0 - Success, negative - Failure 867 **/ 868 static void link_status_event_handler(void *handle, void *buf_in, u16 in_size, 869 void *buf_out, u16 *out_size) 870 { 871 struct hinic_port_link_status *link_status, *ret_link_status; 872 struct hinic_dev *nic_dev = handle; 873 874 link_status = buf_in; 875 876 if (link_status->link == HINIC_LINK_STATE_UP) { 877 down(&nic_dev->mgmt_lock); 878 879 nic_dev->flags |= HINIC_LINK_UP; 880 881 if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) == 882 (HINIC_LINK_UP | HINIC_INTF_UP)) { 883 netif_carrier_on(nic_dev->netdev); 884 netif_tx_wake_all_queues(nic_dev->netdev); 885 } 886 887 up(&nic_dev->mgmt_lock); 888 889 netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is UP\n"); 890 } else { 891 down(&nic_dev->mgmt_lock); 892 893 nic_dev->flags &= ~HINIC_LINK_UP; 894 895 netif_carrier_off(nic_dev->netdev); 896 netif_tx_disable(nic_dev->netdev); 897 898 up(&nic_dev->mgmt_lock); 899 900 netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is DOWN\n"); 901 } 902 903 ret_link_status = buf_out; 904 ret_link_status->status = 0; 905 906 *out_size = sizeof(*ret_link_status); 907 } 908 909 static int set_features(struct hinic_dev *nic_dev, 910 netdev_features_t pre_features, 911 netdev_features_t features, bool force_change) 912 { 913 netdev_features_t changed = force_change ? ~0 : pre_features ^ features; 914 u32 csum_en = HINIC_RX_CSUM_OFFLOAD_EN; 915 int err = 0; 916 917 if (changed & NETIF_F_TSO) 918 err = hinic_port_set_tso(nic_dev, (features & NETIF_F_TSO) ? 919 HINIC_TSO_ENABLE : HINIC_TSO_DISABLE); 920 921 if (changed & NETIF_F_RXCSUM) 922 err = hinic_set_rx_csum_offload(nic_dev, csum_en); 923 924 if (changed & NETIF_F_LRO) { 925 err = hinic_set_rx_lro_state(nic_dev, 926 !!(features & NETIF_F_LRO), 927 HINIC_LRO_RX_TIMER_DEFAULT, 928 HINIC_LRO_MAX_WQE_NUM_DEFAULT); 929 } 930 931 return err; 932 } 933 934 /** 935 * nic_dev_init - Initialize the NIC device 936 * @pdev: the NIC pci device 937 * 938 * Return 0 - Success, negative - Failure 939 **/ 940 static int nic_dev_init(struct pci_dev *pdev) 941 { 942 struct hinic_rx_mode_work *rx_mode_work; 943 struct hinic_txq_stats *tx_stats; 944 struct hinic_rxq_stats *rx_stats; 945 struct hinic_dev *nic_dev; 946 struct net_device *netdev; 947 struct hinic_hwdev *hwdev; 948 int err, num_qps; 949 950 hwdev = hinic_init_hwdev(pdev); 951 if (IS_ERR(hwdev)) { 952 dev_err(&pdev->dev, "Failed to initialize HW device\n"); 953 return PTR_ERR(hwdev); 954 } 955 956 num_qps = hinic_hwdev_num_qps(hwdev); 957 if (num_qps <= 0) { 958 dev_err(&pdev->dev, "Invalid number of QPS\n"); 959 err = -EINVAL; 960 goto err_num_qps; 961 } 962 963 netdev = alloc_etherdev_mq(sizeof(*nic_dev), num_qps); 964 if (!netdev) { 965 dev_err(&pdev->dev, "Failed to allocate Ethernet device\n"); 966 err = -ENOMEM; 967 goto err_alloc_etherdev; 968 } 969 970 netdev->netdev_ops = &hinic_netdev_ops; 971 netdev->ethtool_ops = &hinic_ethtool_ops; 972 netdev->max_mtu = ETH_MAX_MTU; 973 974 nic_dev = netdev_priv(netdev); 975 nic_dev->netdev = netdev; 976 nic_dev->hwdev = hwdev; 977 nic_dev->msg_enable = MSG_ENABLE_DEFAULT; 978 nic_dev->flags = 0; 979 nic_dev->txqs = NULL; 980 nic_dev->rxqs = NULL; 981 nic_dev->tx_weight = tx_weight; 982 nic_dev->rx_weight = rx_weight; 983 984 sema_init(&nic_dev->mgmt_lock, 1); 985 986 tx_stats = &nic_dev->tx_stats; 987 rx_stats = &nic_dev->rx_stats; 988 989 u64_stats_init(&tx_stats->syncp); 990 u64_stats_init(&rx_stats->syncp); 991 992 nic_dev->vlan_bitmap = devm_kzalloc(&pdev->dev, 993 VLAN_BITMAP_SIZE(nic_dev), 994 GFP_KERNEL); 995 if (!nic_dev->vlan_bitmap) { 996 err = -ENOMEM; 997 goto err_vlan_bitmap; 998 } 999 1000 nic_dev->workq = create_singlethread_workqueue(HINIC_WQ_NAME); 1001 if (!nic_dev->workq) { 1002 err = -ENOMEM; 1003 goto err_workq; 1004 } 1005 1006 pci_set_drvdata(pdev, netdev); 1007 1008 err = hinic_port_get_mac(nic_dev, netdev->dev_addr); 1009 if (err) 1010 dev_warn(&pdev->dev, "Failed to get mac address\n"); 1011 1012 err = hinic_port_add_mac(nic_dev, netdev->dev_addr, 0); 1013 if (err) { 1014 dev_err(&pdev->dev, "Failed to add mac\n"); 1015 goto err_add_mac; 1016 } 1017 1018 err = hinic_port_set_mtu(nic_dev, netdev->mtu); 1019 if (err) { 1020 dev_err(&pdev->dev, "Failed to set mtu\n"); 1021 goto err_set_mtu; 1022 } 1023 1024 rx_mode_work = &nic_dev->rx_mode_work; 1025 INIT_WORK(&rx_mode_work->work, set_rx_mode); 1026 1027 netdev_features_init(netdev); 1028 1029 netif_carrier_off(netdev); 1030 1031 hinic_hwdev_cb_register(nic_dev->hwdev, HINIC_MGMT_MSG_CMD_LINK_STATUS, 1032 nic_dev, link_status_event_handler); 1033 1034 err = set_features(nic_dev, 0, nic_dev->netdev->features, true); 1035 if (err) 1036 goto err_set_features; 1037 1038 SET_NETDEV_DEV(netdev, &pdev->dev); 1039 1040 err = register_netdev(netdev); 1041 if (err) { 1042 dev_err(&pdev->dev, "Failed to register netdev\n"); 1043 goto err_reg_netdev; 1044 } 1045 1046 return 0; 1047 1048 err_reg_netdev: 1049 err_set_features: 1050 hinic_hwdev_cb_unregister(nic_dev->hwdev, 1051 HINIC_MGMT_MSG_CMD_LINK_STATUS); 1052 cancel_work_sync(&rx_mode_work->work); 1053 1054 err_set_mtu: 1055 err_add_mac: 1056 pci_set_drvdata(pdev, NULL); 1057 destroy_workqueue(nic_dev->workq); 1058 1059 err_workq: 1060 err_vlan_bitmap: 1061 free_netdev(netdev); 1062 1063 err_alloc_etherdev: 1064 err_num_qps: 1065 hinic_free_hwdev(hwdev); 1066 return err; 1067 } 1068 1069 static int hinic_probe(struct pci_dev *pdev, 1070 const struct pci_device_id *id) 1071 { 1072 int err = pci_enable_device(pdev); 1073 1074 if (err) { 1075 dev_err(&pdev->dev, "Failed to enable PCI device\n"); 1076 return err; 1077 } 1078 1079 err = pci_request_regions(pdev, HINIC_DRV_NAME); 1080 if (err) { 1081 dev_err(&pdev->dev, "Failed to request PCI regions\n"); 1082 goto err_pci_regions; 1083 } 1084 1085 pci_set_master(pdev); 1086 1087 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64)); 1088 if (err) { 1089 dev_warn(&pdev->dev, "Couldn't set 64-bit DMA mask\n"); 1090 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); 1091 if (err) { 1092 dev_err(&pdev->dev, "Failed to set DMA mask\n"); 1093 goto err_dma_mask; 1094 } 1095 } 1096 1097 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)); 1098 if (err) { 1099 dev_warn(&pdev->dev, 1100 "Couldn't set 64-bit consistent DMA mask\n"); 1101 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)); 1102 if (err) { 1103 dev_err(&pdev->dev, 1104 "Failed to set consistent DMA mask\n"); 1105 goto err_dma_consistent_mask; 1106 } 1107 } 1108 1109 err = nic_dev_init(pdev); 1110 if (err) { 1111 dev_err(&pdev->dev, "Failed to initialize NIC device\n"); 1112 goto err_nic_dev_init; 1113 } 1114 1115 dev_info(&pdev->dev, "HiNIC driver - probed\n"); 1116 return 0; 1117 1118 err_nic_dev_init: 1119 err_dma_consistent_mask: 1120 err_dma_mask: 1121 pci_release_regions(pdev); 1122 1123 err_pci_regions: 1124 pci_disable_device(pdev); 1125 return err; 1126 } 1127 1128 static void hinic_remove(struct pci_dev *pdev) 1129 { 1130 struct net_device *netdev = pci_get_drvdata(pdev); 1131 struct hinic_dev *nic_dev = netdev_priv(netdev); 1132 struct hinic_rx_mode_work *rx_mode_work; 1133 1134 unregister_netdev(netdev); 1135 1136 hinic_hwdev_cb_unregister(nic_dev->hwdev, 1137 HINIC_MGMT_MSG_CMD_LINK_STATUS); 1138 1139 rx_mode_work = &nic_dev->rx_mode_work; 1140 cancel_work_sync(&rx_mode_work->work); 1141 1142 pci_set_drvdata(pdev, NULL); 1143 1144 destroy_workqueue(nic_dev->workq); 1145 1146 hinic_free_hwdev(nic_dev->hwdev); 1147 1148 free_netdev(netdev); 1149 1150 pci_release_regions(pdev); 1151 pci_disable_device(pdev); 1152 1153 dev_info(&pdev->dev, "HiNIC driver - removed\n"); 1154 } 1155 1156 static void hinic_shutdown(struct pci_dev *pdev) 1157 { 1158 pci_disable_device(pdev); 1159 } 1160 1161 static const struct pci_device_id hinic_pci_table[] = { 1162 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_QUAD_PORT_25GE), 0}, 1163 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_100GE), 0}, 1164 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_100GE_MEZZ), 0}, 1165 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_QUAD_PORT_25GE_MEZZ), 0}, 1166 { 0, 0} 1167 }; 1168 MODULE_DEVICE_TABLE(pci, hinic_pci_table); 1169 1170 static struct pci_driver hinic_driver = { 1171 .name = HINIC_DRV_NAME, 1172 .id_table = hinic_pci_table, 1173 .probe = hinic_probe, 1174 .remove = hinic_remove, 1175 .shutdown = hinic_shutdown, 1176 }; 1177 1178 module_pci_driver(hinic_driver); 1179