1 /* 2 * Huawei HiNIC PCI Express Linux driver 3 * Copyright(c) 2017 Huawei Technologies Co., Ltd 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms and conditions of the GNU General Public License, 7 * version 2, as published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * for more details. 13 * 14 */ 15 16 #include <linux/kernel.h> 17 #include <linux/module.h> 18 #include <linux/moduleparam.h> 19 #include <linux/pci.h> 20 #include <linux/device.h> 21 #include <linux/errno.h> 22 #include <linux/types.h> 23 #include <linux/etherdevice.h> 24 #include <linux/netdevice.h> 25 #include <linux/slab.h> 26 #include <linux/if_vlan.h> 27 #include <linux/semaphore.h> 28 #include <linux/workqueue.h> 29 #include <net/ip.h> 30 #include <linux/bitops.h> 31 #include <linux/bitmap.h> 32 #include <linux/delay.h> 33 #include <linux/err.h> 34 35 #include "hinic_hw_qp.h" 36 #include "hinic_hw_dev.h" 37 #include "hinic_port.h" 38 #include "hinic_tx.h" 39 #include "hinic_rx.h" 40 #include "hinic_dev.h" 41 42 MODULE_AUTHOR("Huawei Technologies CO., Ltd"); 43 MODULE_DESCRIPTION("Huawei Intelligent NIC driver"); 44 MODULE_LICENSE("GPL"); 45 46 static unsigned int tx_weight = 64; 47 module_param(tx_weight, uint, 0644); 48 MODULE_PARM_DESC(tx_weight, "Number Tx packets for NAPI budget (default=64)"); 49 50 static unsigned int rx_weight = 64; 51 module_param(rx_weight, uint, 0644); 52 MODULE_PARM_DESC(rx_weight, "Number Rx packets for NAPI budget (default=64)"); 53 54 #define HINIC_DEV_ID_QUAD_PORT_25GE 0x1822 55 #define HINIC_DEV_ID_DUAL_PORT_25GE 0x0200 56 #define HINIC_DEV_ID_DUAL_PORT_100GE 0x0201 57 58 #define HINIC_WQ_NAME "hinic_dev" 59 60 #define MSG_ENABLE_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \ 61 NETIF_MSG_IFUP | \ 62 NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR) 63 64 #define VLAN_BITMAP_SIZE(nic_dev) (ALIGN(VLAN_N_VID, 8) / 8) 65 66 #define work_to_rx_mode_work(work) \ 67 container_of(work, struct hinic_rx_mode_work, work) 68 69 #define rx_mode_work_to_nic_dev(rx_mode_work) \ 70 container_of(rx_mode_work, struct hinic_dev, rx_mode_work) 71 72 static int change_mac_addr(struct net_device *netdev, const u8 *addr); 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_open(struct net_device *netdev) 375 { 376 struct hinic_dev *nic_dev = netdev_priv(netdev); 377 enum hinic_port_link_state link_state; 378 int err, ret, num_qps; 379 380 if (!(nic_dev->flags & HINIC_INTF_UP)) { 381 err = hinic_hwdev_ifup(nic_dev->hwdev); 382 if (err) { 383 netif_err(nic_dev, drv, netdev, 384 "Failed - HW interface up\n"); 385 return err; 386 } 387 } 388 389 err = create_txqs(nic_dev); 390 if (err) { 391 netif_err(nic_dev, drv, netdev, 392 "Failed to create Tx queues\n"); 393 goto err_create_txqs; 394 } 395 396 err = create_rxqs(nic_dev); 397 if (err) { 398 netif_err(nic_dev, drv, netdev, 399 "Failed to create Rx queues\n"); 400 goto err_create_rxqs; 401 } 402 403 num_qps = hinic_hwdev_num_qps(nic_dev->hwdev); 404 netif_set_real_num_tx_queues(netdev, num_qps); 405 netif_set_real_num_rx_queues(netdev, num_qps); 406 407 err = hinic_port_set_state(nic_dev, HINIC_PORT_ENABLE); 408 if (err) { 409 netif_err(nic_dev, drv, netdev, 410 "Failed to set port state\n"); 411 goto err_port_state; 412 } 413 414 err = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_ENABLE); 415 if (err) { 416 netif_err(nic_dev, drv, netdev, 417 "Failed to set func port state\n"); 418 goto err_func_port_state; 419 } 420 421 /* Wait up to 3 sec between port enable to link state */ 422 msleep(3000); 423 424 down(&nic_dev->mgmt_lock); 425 426 err = hinic_port_link_state(nic_dev, &link_state); 427 if (err) { 428 netif_err(nic_dev, drv, netdev, "Failed to get link state\n"); 429 goto err_port_link; 430 } 431 432 if (link_state == HINIC_LINK_STATE_UP) 433 nic_dev->flags |= HINIC_LINK_UP; 434 435 nic_dev->flags |= HINIC_INTF_UP; 436 437 if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) == 438 (HINIC_LINK_UP | HINIC_INTF_UP)) { 439 netif_info(nic_dev, drv, netdev, "link + intf UP\n"); 440 netif_carrier_on(netdev); 441 netif_tx_wake_all_queues(netdev); 442 } 443 444 up(&nic_dev->mgmt_lock); 445 446 netif_info(nic_dev, drv, netdev, "HINIC_INTF is UP\n"); 447 return 0; 448 449 err_port_link: 450 up(&nic_dev->mgmt_lock); 451 ret = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE); 452 if (ret) 453 netif_warn(nic_dev, drv, netdev, 454 "Failed to revert func port state\n"); 455 456 err_func_port_state: 457 ret = hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE); 458 if (ret) 459 netif_warn(nic_dev, drv, netdev, 460 "Failed to revert port state\n"); 461 462 err_port_state: 463 free_rxqs(nic_dev); 464 465 err_create_rxqs: 466 free_txqs(nic_dev); 467 468 err_create_txqs: 469 if (!(nic_dev->flags & HINIC_INTF_UP)) 470 hinic_hwdev_ifdown(nic_dev->hwdev); 471 return err; 472 } 473 474 static int hinic_close(struct net_device *netdev) 475 { 476 struct hinic_dev *nic_dev = netdev_priv(netdev); 477 unsigned int flags; 478 int err; 479 480 down(&nic_dev->mgmt_lock); 481 482 flags = nic_dev->flags; 483 nic_dev->flags &= ~HINIC_INTF_UP; 484 485 netif_carrier_off(netdev); 486 netif_tx_disable(netdev); 487 488 update_nic_stats(nic_dev); 489 490 up(&nic_dev->mgmt_lock); 491 492 err = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE); 493 if (err) { 494 netif_err(nic_dev, drv, netdev, 495 "Failed to set func port state\n"); 496 nic_dev->flags |= (flags & HINIC_INTF_UP); 497 return err; 498 } 499 500 err = hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE); 501 if (err) { 502 netif_err(nic_dev, drv, netdev, "Failed to set port state\n"); 503 nic_dev->flags |= (flags & HINIC_INTF_UP); 504 return err; 505 } 506 507 free_rxqs(nic_dev); 508 free_txqs(nic_dev); 509 510 if (flags & HINIC_INTF_UP) 511 hinic_hwdev_ifdown(nic_dev->hwdev); 512 513 netif_info(nic_dev, drv, netdev, "HINIC_INTF is DOWN\n"); 514 return 0; 515 } 516 517 static int hinic_change_mtu(struct net_device *netdev, int new_mtu) 518 { 519 struct hinic_dev *nic_dev = netdev_priv(netdev); 520 int err; 521 522 netif_info(nic_dev, drv, netdev, "set_mtu = %d\n", new_mtu); 523 524 err = hinic_port_set_mtu(nic_dev, new_mtu); 525 if (err) 526 netif_err(nic_dev, drv, netdev, "Failed to set port mtu\n"); 527 else 528 netdev->mtu = new_mtu; 529 530 return err; 531 } 532 533 /** 534 * change_mac_addr - change the main mac address of network device 535 * @netdev: network device 536 * @addr: mac address to set 537 * 538 * Return 0 - Success, negative - Failure 539 **/ 540 static int change_mac_addr(struct net_device *netdev, const u8 *addr) 541 { 542 struct hinic_dev *nic_dev = netdev_priv(netdev); 543 u16 vid = 0; 544 int err; 545 546 if (!is_valid_ether_addr(addr)) 547 return -EADDRNOTAVAIL; 548 549 netif_info(nic_dev, drv, netdev, "change mac addr = %02x %02x %02x %02x %02x %02x\n", 550 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); 551 552 down(&nic_dev->mgmt_lock); 553 554 do { 555 err = hinic_port_del_mac(nic_dev, netdev->dev_addr, vid); 556 if (err) { 557 netif_err(nic_dev, drv, netdev, 558 "Failed to delete mac\n"); 559 break; 560 } 561 562 err = hinic_port_add_mac(nic_dev, addr, vid); 563 if (err) { 564 netif_err(nic_dev, drv, netdev, "Failed to add mac\n"); 565 break; 566 } 567 568 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1); 569 } while (vid != VLAN_N_VID); 570 571 up(&nic_dev->mgmt_lock); 572 return err; 573 } 574 575 static int hinic_set_mac_addr(struct net_device *netdev, void *addr) 576 { 577 unsigned char new_mac[ETH_ALEN]; 578 struct sockaddr *saddr = addr; 579 int err; 580 581 memcpy(new_mac, saddr->sa_data, ETH_ALEN); 582 583 err = change_mac_addr(netdev, new_mac); 584 if (!err) 585 memcpy(netdev->dev_addr, new_mac, ETH_ALEN); 586 587 return err; 588 } 589 590 /** 591 * add_mac_addr - add mac address to network device 592 * @netdev: network device 593 * @addr: mac address to add 594 * 595 * Return 0 - Success, negative - Failure 596 **/ 597 static int add_mac_addr(struct net_device *netdev, const u8 *addr) 598 { 599 struct hinic_dev *nic_dev = netdev_priv(netdev); 600 u16 vid = 0; 601 int err; 602 603 if (!is_valid_ether_addr(addr)) 604 return -EADDRNOTAVAIL; 605 606 netif_info(nic_dev, drv, netdev, "set mac addr = %02x %02x %02x %02x %02x %02x\n", 607 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); 608 609 down(&nic_dev->mgmt_lock); 610 611 do { 612 err = hinic_port_add_mac(nic_dev, addr, vid); 613 if (err) { 614 netif_err(nic_dev, drv, netdev, "Failed to add mac\n"); 615 break; 616 } 617 618 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1); 619 } while (vid != VLAN_N_VID); 620 621 up(&nic_dev->mgmt_lock); 622 return err; 623 } 624 625 /** 626 * remove_mac_addr - remove mac address from network device 627 * @netdev: network device 628 * @addr: mac address to remove 629 * 630 * Return 0 - Success, negative - Failure 631 **/ 632 static int remove_mac_addr(struct net_device *netdev, const u8 *addr) 633 { 634 struct hinic_dev *nic_dev = netdev_priv(netdev); 635 u16 vid = 0; 636 int err; 637 638 if (!is_valid_ether_addr(addr)) 639 return -EADDRNOTAVAIL; 640 641 netif_info(nic_dev, drv, netdev, "remove mac addr = %02x %02x %02x %02x %02x %02x\n", 642 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); 643 644 down(&nic_dev->mgmt_lock); 645 646 do { 647 err = hinic_port_del_mac(nic_dev, addr, vid); 648 if (err) { 649 netif_err(nic_dev, drv, netdev, 650 "Failed to delete mac\n"); 651 break; 652 } 653 654 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1); 655 } while (vid != VLAN_N_VID); 656 657 up(&nic_dev->mgmt_lock); 658 return err; 659 } 660 661 static int hinic_vlan_rx_add_vid(struct net_device *netdev, 662 __always_unused __be16 proto, u16 vid) 663 { 664 struct hinic_dev *nic_dev = netdev_priv(netdev); 665 int ret, err; 666 667 netif_info(nic_dev, drv, netdev, "add vid = %d\n", vid); 668 669 down(&nic_dev->mgmt_lock); 670 671 err = hinic_port_add_vlan(nic_dev, vid); 672 if (err) { 673 netif_err(nic_dev, drv, netdev, "Failed to add vlan\n"); 674 goto err_vlan_add; 675 } 676 677 err = hinic_port_add_mac(nic_dev, netdev->dev_addr, vid); 678 if (err) { 679 netif_err(nic_dev, drv, netdev, "Failed to set mac\n"); 680 goto err_add_mac; 681 } 682 683 bitmap_set(nic_dev->vlan_bitmap, vid, 1); 684 685 up(&nic_dev->mgmt_lock); 686 return 0; 687 688 err_add_mac: 689 ret = hinic_port_del_vlan(nic_dev, vid); 690 if (ret) 691 netif_err(nic_dev, drv, netdev, 692 "Failed to revert by removing vlan\n"); 693 694 err_vlan_add: 695 up(&nic_dev->mgmt_lock); 696 return err; 697 } 698 699 static int hinic_vlan_rx_kill_vid(struct net_device *netdev, 700 __always_unused __be16 proto, u16 vid) 701 { 702 struct hinic_dev *nic_dev = netdev_priv(netdev); 703 int err; 704 705 netif_info(nic_dev, drv, netdev, "remove vid = %d\n", vid); 706 707 down(&nic_dev->mgmt_lock); 708 709 err = hinic_port_del_vlan(nic_dev, vid); 710 if (err) { 711 netif_err(nic_dev, drv, netdev, "Failed to delete vlan\n"); 712 goto err_del_vlan; 713 } 714 715 bitmap_clear(nic_dev->vlan_bitmap, vid, 1); 716 717 up(&nic_dev->mgmt_lock); 718 return 0; 719 720 err_del_vlan: 721 up(&nic_dev->mgmt_lock); 722 return err; 723 } 724 725 static void set_rx_mode(struct work_struct *work) 726 { 727 struct hinic_rx_mode_work *rx_mode_work = work_to_rx_mode_work(work); 728 struct hinic_dev *nic_dev = rx_mode_work_to_nic_dev(rx_mode_work); 729 730 netif_info(nic_dev, drv, nic_dev->netdev, "set rx mode work\n"); 731 732 hinic_port_set_rx_mode(nic_dev, rx_mode_work->rx_mode); 733 734 __dev_uc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr); 735 __dev_mc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr); 736 } 737 738 static void hinic_set_rx_mode(struct net_device *netdev) 739 { 740 struct hinic_dev *nic_dev = netdev_priv(netdev); 741 struct hinic_rx_mode_work *rx_mode_work; 742 u32 rx_mode; 743 744 rx_mode_work = &nic_dev->rx_mode_work; 745 746 rx_mode = HINIC_RX_MODE_UC | 747 HINIC_RX_MODE_MC | 748 HINIC_RX_MODE_BC; 749 750 if (netdev->flags & IFF_PROMISC) 751 rx_mode |= HINIC_RX_MODE_PROMISC; 752 else if (netdev->flags & IFF_ALLMULTI) 753 rx_mode |= HINIC_RX_MODE_MC_ALL; 754 755 rx_mode_work->rx_mode = rx_mode; 756 757 queue_work(nic_dev->workq, &rx_mode_work->work); 758 } 759 760 static void hinic_tx_timeout(struct net_device *netdev) 761 { 762 struct hinic_dev *nic_dev = netdev_priv(netdev); 763 764 netif_err(nic_dev, drv, netdev, "Tx timeout\n"); 765 } 766 767 static void hinic_get_stats64(struct net_device *netdev, 768 struct rtnl_link_stats64 *stats) 769 { 770 struct hinic_dev *nic_dev = netdev_priv(netdev); 771 struct hinic_rxq_stats *nic_rx_stats; 772 struct hinic_txq_stats *nic_tx_stats; 773 774 nic_rx_stats = &nic_dev->rx_stats; 775 nic_tx_stats = &nic_dev->tx_stats; 776 777 down(&nic_dev->mgmt_lock); 778 779 if (nic_dev->flags & HINIC_INTF_UP) 780 update_nic_stats(nic_dev); 781 782 up(&nic_dev->mgmt_lock); 783 784 stats->rx_bytes = nic_rx_stats->bytes; 785 stats->rx_packets = nic_rx_stats->pkts; 786 787 stats->tx_bytes = nic_tx_stats->bytes; 788 stats->tx_packets = nic_tx_stats->pkts; 789 stats->tx_errors = nic_tx_stats->tx_dropped; 790 } 791 792 #ifdef CONFIG_NET_POLL_CONTROLLER 793 static void hinic_netpoll(struct net_device *netdev) 794 { 795 struct hinic_dev *nic_dev = netdev_priv(netdev); 796 int i, num_qps; 797 798 num_qps = hinic_hwdev_num_qps(nic_dev->hwdev); 799 for (i = 0; i < num_qps; i++) { 800 struct hinic_txq *txq = &nic_dev->txqs[i]; 801 struct hinic_rxq *rxq = &nic_dev->rxqs[i]; 802 803 napi_schedule(&txq->napi); 804 napi_schedule(&rxq->napi); 805 } 806 } 807 #endif 808 809 static const struct net_device_ops hinic_netdev_ops = { 810 .ndo_open = hinic_open, 811 .ndo_stop = hinic_close, 812 .ndo_change_mtu = hinic_change_mtu, 813 .ndo_set_mac_address = hinic_set_mac_addr, 814 .ndo_validate_addr = eth_validate_addr, 815 .ndo_vlan_rx_add_vid = hinic_vlan_rx_add_vid, 816 .ndo_vlan_rx_kill_vid = hinic_vlan_rx_kill_vid, 817 .ndo_set_rx_mode = hinic_set_rx_mode, 818 .ndo_start_xmit = hinic_xmit_frame, 819 .ndo_tx_timeout = hinic_tx_timeout, 820 .ndo_get_stats64 = hinic_get_stats64, 821 #ifdef CONFIG_NET_POLL_CONTROLLER 822 .ndo_poll_controller = hinic_netpoll, 823 #endif 824 }; 825 826 static void netdev_features_init(struct net_device *netdev) 827 { 828 netdev->hw_features = NETIF_F_SG | NETIF_F_HIGHDMA; 829 830 netdev->vlan_features = netdev->hw_features; 831 832 netdev->features = netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER; 833 } 834 835 /** 836 * link_status_event_handler - link event handler 837 * @handle: nic device for the handler 838 * @buf_in: input buffer 839 * @in_size: input size 840 * @buf_in: output buffer 841 * @out_size: returned output size 842 * 843 * Return 0 - Success, negative - Failure 844 **/ 845 static void link_status_event_handler(void *handle, void *buf_in, u16 in_size, 846 void *buf_out, u16 *out_size) 847 { 848 struct hinic_port_link_status *link_status, *ret_link_status; 849 struct hinic_dev *nic_dev = handle; 850 851 link_status = buf_in; 852 853 if (link_status->link == HINIC_LINK_STATE_UP) { 854 down(&nic_dev->mgmt_lock); 855 856 nic_dev->flags |= HINIC_LINK_UP; 857 858 if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) == 859 (HINIC_LINK_UP | HINIC_INTF_UP)) { 860 netif_carrier_on(nic_dev->netdev); 861 netif_tx_wake_all_queues(nic_dev->netdev); 862 } 863 864 up(&nic_dev->mgmt_lock); 865 866 netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is UP\n"); 867 } else { 868 down(&nic_dev->mgmt_lock); 869 870 nic_dev->flags &= ~HINIC_LINK_UP; 871 872 netif_carrier_off(nic_dev->netdev); 873 netif_tx_disable(nic_dev->netdev); 874 875 up(&nic_dev->mgmt_lock); 876 877 netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is DOWN\n"); 878 } 879 880 ret_link_status = buf_out; 881 ret_link_status->status = 0; 882 883 *out_size = sizeof(*ret_link_status); 884 } 885 886 /** 887 * nic_dev_init - Initialize the NIC device 888 * @pdev: the NIC pci device 889 * 890 * Return 0 - Success, negative - Failure 891 **/ 892 static int nic_dev_init(struct pci_dev *pdev) 893 { 894 struct hinic_rx_mode_work *rx_mode_work; 895 struct hinic_txq_stats *tx_stats; 896 struct hinic_rxq_stats *rx_stats; 897 struct hinic_dev *nic_dev; 898 struct net_device *netdev; 899 struct hinic_hwdev *hwdev; 900 int err, num_qps; 901 902 hwdev = hinic_init_hwdev(pdev); 903 if (IS_ERR(hwdev)) { 904 dev_err(&pdev->dev, "Failed to initialize HW device\n"); 905 return PTR_ERR(hwdev); 906 } 907 908 num_qps = hinic_hwdev_num_qps(hwdev); 909 if (num_qps <= 0) { 910 dev_err(&pdev->dev, "Invalid number of QPS\n"); 911 err = -EINVAL; 912 goto err_num_qps; 913 } 914 915 netdev = alloc_etherdev_mq(sizeof(*nic_dev), num_qps); 916 if (!netdev) { 917 dev_err(&pdev->dev, "Failed to allocate Ethernet device\n"); 918 err = -ENOMEM; 919 goto err_alloc_etherdev; 920 } 921 922 netdev->netdev_ops = &hinic_netdev_ops; 923 netdev->ethtool_ops = &hinic_ethtool_ops; 924 netdev->max_mtu = ETH_MAX_MTU; 925 926 nic_dev = netdev_priv(netdev); 927 nic_dev->netdev = netdev; 928 nic_dev->hwdev = hwdev; 929 nic_dev->msg_enable = MSG_ENABLE_DEFAULT; 930 nic_dev->flags = 0; 931 nic_dev->txqs = NULL; 932 nic_dev->rxqs = NULL; 933 nic_dev->tx_weight = tx_weight; 934 nic_dev->rx_weight = rx_weight; 935 936 sema_init(&nic_dev->mgmt_lock, 1); 937 938 tx_stats = &nic_dev->tx_stats; 939 rx_stats = &nic_dev->rx_stats; 940 941 u64_stats_init(&tx_stats->syncp); 942 u64_stats_init(&rx_stats->syncp); 943 944 nic_dev->vlan_bitmap = devm_kzalloc(&pdev->dev, 945 VLAN_BITMAP_SIZE(nic_dev), 946 GFP_KERNEL); 947 if (!nic_dev->vlan_bitmap) { 948 err = -ENOMEM; 949 goto err_vlan_bitmap; 950 } 951 952 nic_dev->workq = create_singlethread_workqueue(HINIC_WQ_NAME); 953 if (!nic_dev->workq) { 954 err = -ENOMEM; 955 goto err_workq; 956 } 957 958 pci_set_drvdata(pdev, netdev); 959 960 err = hinic_port_get_mac(nic_dev, netdev->dev_addr); 961 if (err) 962 dev_warn(&pdev->dev, "Failed to get mac address\n"); 963 964 err = hinic_port_add_mac(nic_dev, netdev->dev_addr, 0); 965 if (err) { 966 dev_err(&pdev->dev, "Failed to add mac\n"); 967 goto err_add_mac; 968 } 969 970 err = hinic_port_set_mtu(nic_dev, netdev->mtu); 971 if (err) { 972 dev_err(&pdev->dev, "Failed to set mtu\n"); 973 goto err_set_mtu; 974 } 975 976 rx_mode_work = &nic_dev->rx_mode_work; 977 INIT_WORK(&rx_mode_work->work, set_rx_mode); 978 979 netdev_features_init(netdev); 980 981 netif_carrier_off(netdev); 982 983 hinic_hwdev_cb_register(nic_dev->hwdev, HINIC_MGMT_MSG_CMD_LINK_STATUS, 984 nic_dev, link_status_event_handler); 985 986 err = register_netdev(netdev); 987 if (err) { 988 dev_err(&pdev->dev, "Failed to register netdev\n"); 989 goto err_reg_netdev; 990 } 991 992 return 0; 993 994 err_reg_netdev: 995 hinic_hwdev_cb_unregister(nic_dev->hwdev, 996 HINIC_MGMT_MSG_CMD_LINK_STATUS); 997 cancel_work_sync(&rx_mode_work->work); 998 999 err_set_mtu: 1000 err_add_mac: 1001 pci_set_drvdata(pdev, NULL); 1002 destroy_workqueue(nic_dev->workq); 1003 1004 err_workq: 1005 err_vlan_bitmap: 1006 free_netdev(netdev); 1007 1008 err_alloc_etherdev: 1009 err_num_qps: 1010 hinic_free_hwdev(hwdev); 1011 return err; 1012 } 1013 1014 static int hinic_probe(struct pci_dev *pdev, 1015 const struct pci_device_id *id) 1016 { 1017 int err = pci_enable_device(pdev); 1018 1019 if (err) { 1020 dev_err(&pdev->dev, "Failed to enable PCI device\n"); 1021 return err; 1022 } 1023 1024 err = pci_request_regions(pdev, HINIC_DRV_NAME); 1025 if (err) { 1026 dev_err(&pdev->dev, "Failed to request PCI regions\n"); 1027 goto err_pci_regions; 1028 } 1029 1030 pci_set_master(pdev); 1031 1032 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64)); 1033 if (err) { 1034 dev_warn(&pdev->dev, "Couldn't set 64-bit DMA mask\n"); 1035 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); 1036 if (err) { 1037 dev_err(&pdev->dev, "Failed to set DMA mask\n"); 1038 goto err_dma_mask; 1039 } 1040 } 1041 1042 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)); 1043 if (err) { 1044 dev_warn(&pdev->dev, 1045 "Couldn't set 64-bit consistent DMA mask\n"); 1046 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)); 1047 if (err) { 1048 dev_err(&pdev->dev, 1049 "Failed to set consistent DMA mask\n"); 1050 goto err_dma_consistent_mask; 1051 } 1052 } 1053 1054 err = nic_dev_init(pdev); 1055 if (err) { 1056 dev_err(&pdev->dev, "Failed to initialize NIC device\n"); 1057 goto err_nic_dev_init; 1058 } 1059 1060 dev_info(&pdev->dev, "HiNIC driver - probed\n"); 1061 return 0; 1062 1063 err_nic_dev_init: 1064 err_dma_consistent_mask: 1065 err_dma_mask: 1066 pci_release_regions(pdev); 1067 1068 err_pci_regions: 1069 pci_disable_device(pdev); 1070 return err; 1071 } 1072 1073 static void hinic_remove(struct pci_dev *pdev) 1074 { 1075 struct net_device *netdev = pci_get_drvdata(pdev); 1076 struct hinic_dev *nic_dev = netdev_priv(netdev); 1077 struct hinic_rx_mode_work *rx_mode_work; 1078 1079 unregister_netdev(netdev); 1080 1081 hinic_hwdev_cb_unregister(nic_dev->hwdev, 1082 HINIC_MGMT_MSG_CMD_LINK_STATUS); 1083 1084 rx_mode_work = &nic_dev->rx_mode_work; 1085 cancel_work_sync(&rx_mode_work->work); 1086 1087 pci_set_drvdata(pdev, NULL); 1088 1089 destroy_workqueue(nic_dev->workq); 1090 1091 hinic_free_hwdev(nic_dev->hwdev); 1092 1093 free_netdev(netdev); 1094 1095 pci_release_regions(pdev); 1096 pci_disable_device(pdev); 1097 1098 dev_info(&pdev->dev, "HiNIC driver - removed\n"); 1099 } 1100 1101 static const struct pci_device_id hinic_pci_table[] = { 1102 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_QUAD_PORT_25GE), 0}, 1103 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_25GE), 0}, 1104 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_100GE), 0}, 1105 { 0, 0} 1106 }; 1107 MODULE_DEVICE_TABLE(pci, hinic_pci_table); 1108 1109 static struct pci_driver hinic_driver = { 1110 .name = HINIC_DRV_NAME, 1111 .id_table = hinic_pci_table, 1112 .probe = hinic_probe, 1113 .remove = hinic_remove, 1114 }; 1115 1116 module_pci_driver(hinic_driver); 1117