1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2015 - 2022 Beijing WangXun Technology Co., Ltd. */ 3 4 #include <linux/types.h> 5 #include <linux/module.h> 6 #include <linux/pci.h> 7 #include <linux/netdevice.h> 8 #include <linux/string.h> 9 #include <linux/aer.h> 10 #include <linux/etherdevice.h> 11 #include <net/ip.h> 12 13 #include "../libwx/wx_type.h" 14 #include "../libwx/wx_hw.h" 15 #include "txgbe_type.h" 16 #include "txgbe_hw.h" 17 #include "txgbe.h" 18 19 char txgbe_driver_name[] = "txgbe"; 20 21 /* txgbe_pci_tbl - PCI Device ID Table 22 * 23 * Wildcard entries (PCI_ANY_ID) should come last 24 * Last entry must be all 0s 25 * 26 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID, 27 * Class, Class Mask, private data (not used) } 28 */ 29 static const struct pci_device_id txgbe_pci_tbl[] = { 30 { PCI_VDEVICE(WANGXUN, TXGBE_DEV_ID_SP1000), 0}, 31 { PCI_VDEVICE(WANGXUN, TXGBE_DEV_ID_WX1820), 0}, 32 /* required last entry */ 33 { .device = 0 } 34 }; 35 36 #define DEFAULT_DEBUG_LEVEL_SHIFT 3 37 38 static void txgbe_check_minimum_link(struct txgbe_adapter *adapter) 39 { 40 struct pci_dev *pdev; 41 42 pdev = adapter->pdev; 43 pcie_print_link_status(pdev); 44 } 45 46 /** 47 * txgbe_enumerate_functions - Get the number of ports this device has 48 * @adapter: adapter structure 49 * 50 * This function enumerates the phsyical functions co-located on a single slot, 51 * in order to determine how many ports a device has. This is most useful in 52 * determining the required GT/s of PCIe bandwidth necessary for optimal 53 * performance. 54 **/ 55 static int txgbe_enumerate_functions(struct txgbe_adapter *adapter) 56 { 57 struct pci_dev *entry, *pdev = adapter->pdev; 58 int physfns = 0; 59 60 list_for_each_entry(entry, &pdev->bus->devices, bus_list) { 61 /* When the devices on the bus don't all match our device ID, 62 * we can't reliably determine the correct number of 63 * functions. This can occur if a function has been direct 64 * attached to a virtual machine using VT-d. 65 */ 66 if (entry->vendor != pdev->vendor || 67 entry->device != pdev->device) 68 return -EINVAL; 69 70 physfns++; 71 } 72 73 return physfns; 74 } 75 76 static void txgbe_sync_mac_table(struct txgbe_adapter *adapter) 77 { 78 struct txgbe_hw *hw = &adapter->hw; 79 struct wx_hw *wxhw = &hw->wxhw; 80 int i; 81 82 for (i = 0; i < wxhw->mac.num_rar_entries; i++) { 83 if (adapter->mac_table[i].state & TXGBE_MAC_STATE_MODIFIED) { 84 if (adapter->mac_table[i].state & TXGBE_MAC_STATE_IN_USE) { 85 wx_set_rar(wxhw, i, 86 adapter->mac_table[i].addr, 87 adapter->mac_table[i].pools, 88 WX_PSR_MAC_SWC_AD_H_AV); 89 } else { 90 wx_clear_rar(wxhw, i); 91 } 92 adapter->mac_table[i].state &= ~(TXGBE_MAC_STATE_MODIFIED); 93 } 94 } 95 } 96 97 /* this function destroys the first RAR entry */ 98 static void txgbe_mac_set_default_filter(struct txgbe_adapter *adapter, 99 u8 *addr) 100 { 101 struct wx_hw *wxhw = &adapter->hw.wxhw; 102 103 memcpy(&adapter->mac_table[0].addr, addr, ETH_ALEN); 104 adapter->mac_table[0].pools = 1ULL; 105 adapter->mac_table[0].state = (TXGBE_MAC_STATE_DEFAULT | 106 TXGBE_MAC_STATE_IN_USE); 107 wx_set_rar(wxhw, 0, adapter->mac_table[0].addr, 108 adapter->mac_table[0].pools, 109 WX_PSR_MAC_SWC_AD_H_AV); 110 } 111 112 static void txgbe_flush_sw_mac_table(struct txgbe_adapter *adapter) 113 { 114 struct wx_hw *wxhw = &adapter->hw.wxhw; 115 u32 i; 116 117 for (i = 0; i < wxhw->mac.num_rar_entries; i++) { 118 adapter->mac_table[i].state |= TXGBE_MAC_STATE_MODIFIED; 119 adapter->mac_table[i].state &= ~TXGBE_MAC_STATE_IN_USE; 120 memset(adapter->mac_table[i].addr, 0, ETH_ALEN); 121 adapter->mac_table[i].pools = 0; 122 } 123 txgbe_sync_mac_table(adapter); 124 } 125 126 static int txgbe_del_mac_filter(struct txgbe_adapter *adapter, u8 *addr, u16 pool) 127 { 128 struct wx_hw *wxhw = &adapter->hw.wxhw; 129 u32 i; 130 131 if (is_zero_ether_addr(addr)) 132 return -EINVAL; 133 134 /* search table for addr, if found, set to 0 and sync */ 135 for (i = 0; i < wxhw->mac.num_rar_entries; i++) { 136 if (ether_addr_equal(addr, adapter->mac_table[i].addr)) { 137 if (adapter->mac_table[i].pools & (1ULL << pool)) { 138 adapter->mac_table[i].state |= TXGBE_MAC_STATE_MODIFIED; 139 adapter->mac_table[i].state &= ~TXGBE_MAC_STATE_IN_USE; 140 adapter->mac_table[i].pools &= ~(1ULL << pool); 141 txgbe_sync_mac_table(adapter); 142 } 143 return 0; 144 } 145 146 if (adapter->mac_table[i].pools != (1 << pool)) 147 continue; 148 if (!ether_addr_equal(addr, adapter->mac_table[i].addr)) 149 continue; 150 151 adapter->mac_table[i].state |= TXGBE_MAC_STATE_MODIFIED; 152 adapter->mac_table[i].state &= ~TXGBE_MAC_STATE_IN_USE; 153 memset(adapter->mac_table[i].addr, 0, ETH_ALEN); 154 adapter->mac_table[i].pools = 0; 155 txgbe_sync_mac_table(adapter); 156 return 0; 157 } 158 return -ENOMEM; 159 } 160 161 static void txgbe_up_complete(struct txgbe_adapter *adapter) 162 { 163 struct txgbe_hw *hw = &adapter->hw; 164 struct wx_hw *wxhw = &hw->wxhw; 165 166 wx_control_hw(wxhw, true); 167 } 168 169 static void txgbe_reset(struct txgbe_adapter *adapter) 170 { 171 struct net_device *netdev = adapter->netdev; 172 struct txgbe_hw *hw = &adapter->hw; 173 u8 old_addr[ETH_ALEN]; 174 int err; 175 176 err = txgbe_reset_hw(hw); 177 if (err != 0) 178 dev_err(&adapter->pdev->dev, "Hardware Error: %d\n", err); 179 180 /* do not flush user set addresses */ 181 memcpy(old_addr, &adapter->mac_table[0].addr, netdev->addr_len); 182 txgbe_flush_sw_mac_table(adapter); 183 txgbe_mac_set_default_filter(adapter, old_addr); 184 } 185 186 static void txgbe_disable_device(struct txgbe_adapter *adapter) 187 { 188 struct net_device *netdev = adapter->netdev; 189 struct wx_hw *wxhw = &adapter->hw.wxhw; 190 191 wx_disable_pcie_master(wxhw); 192 /* disable receives */ 193 wx_disable_rx(wxhw); 194 195 netif_carrier_off(netdev); 196 netif_tx_disable(netdev); 197 198 if (wxhw->bus.func < 2) 199 wr32m(wxhw, TXGBE_MIS_PRB_CTL, TXGBE_MIS_PRB_CTL_LAN_UP(wxhw->bus.func), 0); 200 else 201 dev_err(&adapter->pdev->dev, 202 "%s: invalid bus lan id %d\n", 203 __func__, wxhw->bus.func); 204 205 if (!(((wxhw->subsystem_device_id & WX_NCSI_MASK) == WX_NCSI_SUP) || 206 ((wxhw->subsystem_device_id & WX_WOL_MASK) == WX_WOL_SUP))) { 207 /* disable mac transmiter */ 208 wr32m(wxhw, WX_MAC_TX_CFG, WX_MAC_TX_CFG_TE, 0); 209 } 210 211 /* Disable the Tx DMA engine */ 212 wr32m(wxhw, WX_TDM_CTL, WX_TDM_CTL_TE, 0); 213 } 214 215 static void txgbe_down(struct txgbe_adapter *adapter) 216 { 217 txgbe_disable_device(adapter); 218 txgbe_reset(adapter); 219 } 220 221 /** 222 * txgbe_sw_init - Initialize general software structures (struct txgbe_adapter) 223 * @adapter: board private structure to initialize 224 **/ 225 static int txgbe_sw_init(struct txgbe_adapter *adapter) 226 { 227 struct pci_dev *pdev = adapter->pdev; 228 struct txgbe_hw *hw = &adapter->hw; 229 struct wx_hw *wxhw = &hw->wxhw; 230 int err; 231 232 wxhw->hw_addr = adapter->io_addr; 233 wxhw->pdev = pdev; 234 235 /* PCI config space info */ 236 err = wx_sw_init(wxhw); 237 if (err < 0) { 238 netif_err(adapter, probe, adapter->netdev, 239 "read of internal subsystem device id failed\n"); 240 return err; 241 } 242 243 switch (wxhw->device_id) { 244 case TXGBE_DEV_ID_SP1000: 245 case TXGBE_DEV_ID_WX1820: 246 wxhw->mac.type = wx_mac_sp; 247 break; 248 default: 249 wxhw->mac.type = wx_mac_unknown; 250 break; 251 } 252 253 wxhw->mac.num_rar_entries = TXGBE_SP_RAR_ENTRIES; 254 wxhw->mac.max_tx_queues = TXGBE_SP_MAX_TX_QUEUES; 255 wxhw->mac.max_rx_queues = TXGBE_SP_MAX_RX_QUEUES; 256 wxhw->mac.mcft_size = TXGBE_SP_MC_TBL_SIZE; 257 258 adapter->mac_table = kcalloc(wxhw->mac.num_rar_entries, 259 sizeof(struct txgbe_mac_addr), 260 GFP_KERNEL); 261 if (!adapter->mac_table) { 262 netif_err(adapter, probe, adapter->netdev, 263 "mac_table allocation failed\n"); 264 return -ENOMEM; 265 } 266 267 return 0; 268 } 269 270 /** 271 * txgbe_open - Called when a network interface is made active 272 * @netdev: network interface device structure 273 * 274 * Returns 0 on success, negative value on failure 275 * 276 * The open entry point is called when a network interface is made 277 * active by the system (IFF_UP). 278 **/ 279 static int txgbe_open(struct net_device *netdev) 280 { 281 struct txgbe_adapter *adapter = netdev_priv(netdev); 282 283 txgbe_up_complete(adapter); 284 285 return 0; 286 } 287 288 /** 289 * txgbe_close_suspend - actions necessary to both suspend and close flows 290 * @adapter: the private adapter struct 291 * 292 * This function should contain the necessary work common to both suspending 293 * and closing of the device. 294 */ 295 static void txgbe_close_suspend(struct txgbe_adapter *adapter) 296 { 297 txgbe_disable_device(adapter); 298 } 299 300 /** 301 * txgbe_close - Disables a network interface 302 * @netdev: network interface device structure 303 * 304 * Returns 0, this is not allowed to fail 305 * 306 * The close entry point is called when an interface is de-activated 307 * by the OS. The hardware is still under the drivers control, but 308 * needs to be disabled. A global MAC reset is issued to stop the 309 * hardware, and all transmit and receive resources are freed. 310 **/ 311 static int txgbe_close(struct net_device *netdev) 312 { 313 struct txgbe_adapter *adapter = netdev_priv(netdev); 314 315 txgbe_down(adapter); 316 wx_control_hw(&adapter->hw.wxhw, false); 317 318 return 0; 319 } 320 321 static void txgbe_dev_shutdown(struct pci_dev *pdev, bool *enable_wake) 322 { 323 struct txgbe_adapter *adapter = pci_get_drvdata(pdev); 324 struct net_device *netdev = adapter->netdev; 325 struct txgbe_hw *hw = &adapter->hw; 326 struct wx_hw *wxhw = &hw->wxhw; 327 328 netif_device_detach(netdev); 329 330 rtnl_lock(); 331 if (netif_running(netdev)) 332 txgbe_close_suspend(adapter); 333 rtnl_unlock(); 334 335 wx_control_hw(wxhw, false); 336 337 pci_disable_device(pdev); 338 } 339 340 static void txgbe_shutdown(struct pci_dev *pdev) 341 { 342 bool wake; 343 344 txgbe_dev_shutdown(pdev, &wake); 345 346 if (system_state == SYSTEM_POWER_OFF) { 347 pci_wake_from_d3(pdev, wake); 348 pci_set_power_state(pdev, PCI_D3hot); 349 } 350 } 351 352 static netdev_tx_t txgbe_xmit_frame(struct sk_buff *skb, 353 struct net_device *netdev) 354 { 355 return NETDEV_TX_OK; 356 } 357 358 /** 359 * txgbe_set_mac - Change the Ethernet Address of the NIC 360 * @netdev: network interface device structure 361 * @p: pointer to an address structure 362 * 363 * Returns 0 on success, negative on failure 364 **/ 365 static int txgbe_set_mac(struct net_device *netdev, void *p) 366 { 367 struct txgbe_adapter *adapter = netdev_priv(netdev); 368 struct wx_hw *wxhw = &adapter->hw.wxhw; 369 struct sockaddr *addr = p; 370 int retval; 371 372 retval = eth_prepare_mac_addr_change(netdev, addr); 373 if (retval) 374 return retval; 375 376 txgbe_del_mac_filter(adapter, wxhw->mac.addr, 0); 377 eth_hw_addr_set(netdev, addr->sa_data); 378 memcpy(wxhw->mac.addr, addr->sa_data, netdev->addr_len); 379 380 txgbe_mac_set_default_filter(adapter, wxhw->mac.addr); 381 382 return 0; 383 } 384 385 static const struct net_device_ops txgbe_netdev_ops = { 386 .ndo_open = txgbe_open, 387 .ndo_stop = txgbe_close, 388 .ndo_start_xmit = txgbe_xmit_frame, 389 .ndo_validate_addr = eth_validate_addr, 390 .ndo_set_mac_address = txgbe_set_mac, 391 }; 392 393 /** 394 * txgbe_probe - Device Initialization Routine 395 * @pdev: PCI device information struct 396 * @ent: entry in txgbe_pci_tbl 397 * 398 * Returns 0 on success, negative on failure 399 * 400 * txgbe_probe initializes an adapter identified by a pci_dev structure. 401 * The OS initialization, configuring of the adapter private structure, 402 * and a hardware reset occur. 403 **/ 404 static int txgbe_probe(struct pci_dev *pdev, 405 const struct pci_device_id __always_unused *ent) 406 { 407 struct txgbe_adapter *adapter = NULL; 408 struct txgbe_hw *hw = NULL; 409 struct wx_hw *wxhw = NULL; 410 struct net_device *netdev; 411 int err, expected_gts; 412 413 u16 eeprom_verh = 0, eeprom_verl = 0, offset = 0; 414 u16 eeprom_cfg_blkh = 0, eeprom_cfg_blkl = 0; 415 u16 build = 0, major = 0, patch = 0; 416 u8 part_str[TXGBE_PBANUM_LENGTH]; 417 u32 etrack_id = 0; 418 419 err = pci_enable_device_mem(pdev); 420 if (err) 421 return err; 422 423 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 424 if (err) { 425 dev_err(&pdev->dev, 426 "No usable DMA configuration, aborting\n"); 427 goto err_pci_disable_dev; 428 } 429 430 err = pci_request_selected_regions(pdev, 431 pci_select_bars(pdev, IORESOURCE_MEM), 432 txgbe_driver_name); 433 if (err) { 434 dev_err(&pdev->dev, 435 "pci_request_selected_regions failed 0x%x\n", err); 436 goto err_pci_disable_dev; 437 } 438 439 pci_enable_pcie_error_reporting(pdev); 440 pci_set_master(pdev); 441 442 netdev = devm_alloc_etherdev_mqs(&pdev->dev, 443 sizeof(struct txgbe_adapter), 444 TXGBE_MAX_TX_QUEUES, 445 TXGBE_MAX_RX_QUEUES); 446 if (!netdev) { 447 err = -ENOMEM; 448 goto err_pci_release_regions; 449 } 450 451 SET_NETDEV_DEV(netdev, &pdev->dev); 452 453 adapter = netdev_priv(netdev); 454 adapter->netdev = netdev; 455 adapter->pdev = pdev; 456 hw = &adapter->hw; 457 wxhw = &hw->wxhw; 458 adapter->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1; 459 460 adapter->io_addr = devm_ioremap(&pdev->dev, 461 pci_resource_start(pdev, 0), 462 pci_resource_len(pdev, 0)); 463 if (!adapter->io_addr) { 464 err = -EIO; 465 goto err_pci_release_regions; 466 } 467 468 netdev->netdev_ops = &txgbe_netdev_ops; 469 470 /* setup the private structure */ 471 err = txgbe_sw_init(adapter); 472 if (err) 473 goto err_free_mac_table; 474 475 /* check if flash load is done after hw power up */ 476 err = wx_check_flash_load(wxhw, TXGBE_SPI_ILDR_STATUS_PERST); 477 if (err) 478 goto err_free_mac_table; 479 err = wx_check_flash_load(wxhw, TXGBE_SPI_ILDR_STATUS_PWRRST); 480 if (err) 481 goto err_free_mac_table; 482 483 err = wx_mng_present(wxhw); 484 if (err) { 485 dev_err(&pdev->dev, "Management capability is not present\n"); 486 goto err_free_mac_table; 487 } 488 489 err = txgbe_reset_hw(hw); 490 if (err) { 491 dev_err(&pdev->dev, "HW Init failed: %d\n", err); 492 goto err_free_mac_table; 493 } 494 495 netdev->features |= NETIF_F_HIGHDMA; 496 497 /* make sure the EEPROM is good */ 498 err = txgbe_validate_eeprom_checksum(hw, NULL); 499 if (err != 0) { 500 dev_err(&pdev->dev, "The EEPROM Checksum Is Not Valid\n"); 501 wr32(wxhw, WX_MIS_RST, WX_MIS_RST_SW_RST); 502 err = -EIO; 503 goto err_free_mac_table; 504 } 505 506 eth_hw_addr_set(netdev, wxhw->mac.perm_addr); 507 txgbe_mac_set_default_filter(adapter, wxhw->mac.perm_addr); 508 509 /* Save off EEPROM version number and Option Rom version which 510 * together make a unique identify for the eeprom 511 */ 512 wx_read_ee_hostif(wxhw, 513 wxhw->eeprom.sw_region_offset + TXGBE_EEPROM_VERSION_H, 514 &eeprom_verh); 515 wx_read_ee_hostif(wxhw, 516 wxhw->eeprom.sw_region_offset + TXGBE_EEPROM_VERSION_L, 517 &eeprom_verl); 518 etrack_id = (eeprom_verh << 16) | eeprom_verl; 519 520 wx_read_ee_hostif(wxhw, 521 wxhw->eeprom.sw_region_offset + TXGBE_ISCSI_BOOT_CONFIG, 522 &offset); 523 524 /* Make sure offset to SCSI block is valid */ 525 if (!(offset == 0x0) && !(offset == 0xffff)) { 526 wx_read_ee_hostif(wxhw, offset + 0x84, &eeprom_cfg_blkh); 527 wx_read_ee_hostif(wxhw, offset + 0x83, &eeprom_cfg_blkl); 528 529 /* Only display Option Rom if exist */ 530 if (eeprom_cfg_blkl && eeprom_cfg_blkh) { 531 major = eeprom_cfg_blkl >> 8; 532 build = (eeprom_cfg_blkl << 8) | (eeprom_cfg_blkh >> 8); 533 patch = eeprom_cfg_blkh & 0x00ff; 534 535 snprintf(adapter->eeprom_id, sizeof(adapter->eeprom_id), 536 "0x%08x, %d.%d.%d", etrack_id, major, build, 537 patch); 538 } else { 539 snprintf(adapter->eeprom_id, sizeof(adapter->eeprom_id), 540 "0x%08x", etrack_id); 541 } 542 } else { 543 snprintf(adapter->eeprom_id, sizeof(adapter->eeprom_id), 544 "0x%08x", etrack_id); 545 } 546 547 err = register_netdev(netdev); 548 if (err) 549 goto err_release_hw; 550 551 pci_set_drvdata(pdev, adapter); 552 553 /* calculate the expected PCIe bandwidth required for optimal 554 * performance. Note that some older parts will never have enough 555 * bandwidth due to being older generation PCIe parts. We clamp these 556 * parts to ensure that no warning is displayed, as this could confuse 557 * users otherwise. 558 */ 559 expected_gts = txgbe_enumerate_functions(adapter) * 10; 560 561 /* don't check link if we failed to enumerate functions */ 562 if (expected_gts > 0) 563 txgbe_check_minimum_link(adapter); 564 else 565 dev_warn(&pdev->dev, "Failed to enumerate PF devices.\n"); 566 567 /* First try to read PBA as a string */ 568 err = txgbe_read_pba_string(hw, part_str, TXGBE_PBANUM_LENGTH); 569 if (err) 570 strncpy(part_str, "Unknown", TXGBE_PBANUM_LENGTH); 571 572 netif_info(adapter, probe, netdev, "%pM\n", netdev->dev_addr); 573 574 return 0; 575 576 err_release_hw: 577 wx_control_hw(wxhw, false); 578 err_free_mac_table: 579 kfree(adapter->mac_table); 580 err_pci_release_regions: 581 pci_disable_pcie_error_reporting(pdev); 582 pci_release_selected_regions(pdev, 583 pci_select_bars(pdev, IORESOURCE_MEM)); 584 err_pci_disable_dev: 585 pci_disable_device(pdev); 586 return err; 587 } 588 589 /** 590 * txgbe_remove - Device Removal Routine 591 * @pdev: PCI device information struct 592 * 593 * txgbe_remove is called by the PCI subsystem to alert the driver 594 * that it should release a PCI device. The could be caused by a 595 * Hot-Plug event, or because the driver is going to be removed from 596 * memory. 597 **/ 598 static void txgbe_remove(struct pci_dev *pdev) 599 { 600 struct txgbe_adapter *adapter = pci_get_drvdata(pdev); 601 struct net_device *netdev; 602 603 netdev = adapter->netdev; 604 unregister_netdev(netdev); 605 606 pci_release_selected_regions(pdev, 607 pci_select_bars(pdev, IORESOURCE_MEM)); 608 609 kfree(adapter->mac_table); 610 611 pci_disable_pcie_error_reporting(pdev); 612 613 pci_disable_device(pdev); 614 } 615 616 static struct pci_driver txgbe_driver = { 617 .name = txgbe_driver_name, 618 .id_table = txgbe_pci_tbl, 619 .probe = txgbe_probe, 620 .remove = txgbe_remove, 621 .shutdown = txgbe_shutdown, 622 }; 623 624 module_pci_driver(txgbe_driver); 625 626 MODULE_DEVICE_TABLE(pci, txgbe_pci_tbl); 627 MODULE_AUTHOR("Beijing WangXun Technology Co., Ltd, <software@trustnetic.com>"); 628 MODULE_DESCRIPTION("WangXun(R) 10 Gigabit PCI Express Network Driver"); 629 MODULE_LICENSE("GPL"); 630