1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2019 - 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 "ngbe_type.h" 16 #include "ngbe_hw.h" 17 #include "ngbe.h" 18 char ngbe_driver_name[] = "ngbe"; 19 20 /* ngbe_pci_tbl - PCI Device ID Table 21 * 22 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID, 23 * Class, Class Mask, private data (not used) } 24 */ 25 static const struct pci_device_id ngbe_pci_tbl[] = { 26 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860AL_W), 0}, 27 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860A2), 0}, 28 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860A2S), 0}, 29 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860A4), 0}, 30 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860A4S), 0}, 31 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860AL2), 0}, 32 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860AL2S), 0}, 33 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860AL4), 0}, 34 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860AL4S), 0}, 35 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860LC), 0}, 36 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860A1), 0}, 37 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860A1L), 0}, 38 /* required last entry */ 39 { .device = 0 } 40 }; 41 42 static void ngbe_mac_set_default_filter(struct ngbe_adapter *adapter, u8 *addr) 43 { 44 struct ngbe_hw *hw = &adapter->hw; 45 46 memcpy(&adapter->mac_table[0].addr, addr, ETH_ALEN); 47 adapter->mac_table[0].pools = 1ULL; 48 adapter->mac_table[0].state = (NGBE_MAC_STATE_DEFAULT | 49 NGBE_MAC_STATE_IN_USE); 50 wx_set_rar(&hw->wxhw, 0, adapter->mac_table[0].addr, 51 adapter->mac_table[0].pools, 52 WX_PSR_MAC_SWC_AD_H_AV); 53 } 54 55 /** 56 * ngbe_init_type_code - Initialize the shared code 57 * @hw: pointer to hardware structure 58 **/ 59 static void ngbe_init_type_code(struct ngbe_hw *hw) 60 { 61 int wol_mask = 0, ncsi_mask = 0; 62 struct wx_hw *wxhw = &hw->wxhw; 63 u16 type_mask = 0; 64 65 wxhw->mac.type = wx_mac_em; 66 type_mask = (u16)(wxhw->subsystem_device_id & NGBE_OEM_MASK); 67 ncsi_mask = wxhw->subsystem_device_id & NGBE_NCSI_MASK; 68 wol_mask = wxhw->subsystem_device_id & NGBE_WOL_MASK; 69 70 switch (type_mask) { 71 case NGBE_SUBID_M88E1512_SFP: 72 case NGBE_SUBID_LY_M88E1512_SFP: 73 hw->phy.type = ngbe_phy_m88e1512_sfi; 74 break; 75 case NGBE_SUBID_M88E1512_RJ45: 76 hw->phy.type = ngbe_phy_m88e1512; 77 break; 78 case NGBE_SUBID_M88E1512_MIX: 79 hw->phy.type = ngbe_phy_m88e1512_unknown; 80 break; 81 case NGBE_SUBID_YT8521S_SFP: 82 case NGBE_SUBID_YT8521S_SFP_GPIO: 83 case NGBE_SUBID_LY_YT8521S_SFP: 84 hw->phy.type = ngbe_phy_yt8521s_sfi; 85 break; 86 case NGBE_SUBID_INTERNAL_YT8521S_SFP: 87 case NGBE_SUBID_INTERNAL_YT8521S_SFP_GPIO: 88 hw->phy.type = ngbe_phy_internal_yt8521s_sfi; 89 break; 90 case NGBE_SUBID_RGMII_FPGA: 91 case NGBE_SUBID_OCP_CARD: 92 fallthrough; 93 default: 94 hw->phy.type = ngbe_phy_internal; 95 break; 96 } 97 98 if (hw->phy.type == ngbe_phy_internal || 99 hw->phy.type == ngbe_phy_internal_yt8521s_sfi) 100 hw->mac_type = ngbe_mac_type_mdi; 101 else 102 hw->mac_type = ngbe_mac_type_rgmii; 103 104 hw->wol_enabled = (wol_mask == NGBE_WOL_SUP) ? 1 : 0; 105 hw->ncsi_enabled = (ncsi_mask == NGBE_NCSI_MASK || 106 type_mask == NGBE_SUBID_OCP_CARD) ? 1 : 0; 107 108 switch (type_mask) { 109 case NGBE_SUBID_LY_YT8521S_SFP: 110 case NGBE_SUBID_LY_M88E1512_SFP: 111 case NGBE_SUBID_YT8521S_SFP_GPIO: 112 case NGBE_SUBID_INTERNAL_YT8521S_SFP_GPIO: 113 hw->gpio_ctrl = 1; 114 break; 115 default: 116 hw->gpio_ctrl = 0; 117 break; 118 } 119 } 120 121 /** 122 * ngbe_init_rss_key - Initialize adapter RSS key 123 * @adapter: device handle 124 * 125 * Allocates and initializes the RSS key if it is not allocated. 126 **/ 127 static inline int ngbe_init_rss_key(struct ngbe_adapter *adapter) 128 { 129 u32 *rss_key; 130 131 if (!adapter->rss_key) { 132 rss_key = kzalloc(NGBE_RSS_KEY_SIZE, GFP_KERNEL); 133 if (unlikely(!rss_key)) 134 return -ENOMEM; 135 136 netdev_rss_key_fill(rss_key, NGBE_RSS_KEY_SIZE); 137 adapter->rss_key = rss_key; 138 } 139 140 return 0; 141 } 142 143 /** 144 * ngbe_sw_init - Initialize general software structures 145 * @adapter: board private structure to initialize 146 **/ 147 static int ngbe_sw_init(struct ngbe_adapter *adapter) 148 { 149 struct pci_dev *pdev = adapter->pdev; 150 struct ngbe_hw *hw = &adapter->hw; 151 struct wx_hw *wxhw = &hw->wxhw; 152 u16 msix_count = 0; 153 int err = 0; 154 155 wxhw->hw_addr = adapter->io_addr; 156 wxhw->pdev = pdev; 157 158 /* PCI config space info */ 159 err = wx_sw_init(wxhw); 160 if (err < 0) { 161 netif_err(adapter, probe, adapter->netdev, 162 "Read of internal subsystem device id failed\n"); 163 return err; 164 } 165 166 /* mac type, phy type , oem type */ 167 ngbe_init_type_code(hw); 168 169 wxhw->mac.max_rx_queues = NGBE_MAX_RX_QUEUES; 170 wxhw->mac.max_tx_queues = NGBE_MAX_TX_QUEUES; 171 wxhw->mac.num_rar_entries = NGBE_RAR_ENTRIES; 172 /* Set common capability flags and settings */ 173 adapter->max_q_vectors = NGBE_MAX_MSIX_VECTORS; 174 175 err = wx_get_pcie_msix_counts(wxhw, &msix_count, NGBE_MAX_MSIX_VECTORS); 176 if (err) 177 dev_err(&pdev->dev, "Do not support MSI-X\n"); 178 wxhw->mac.max_msix_vectors = msix_count; 179 180 adapter->mac_table = kcalloc(wxhw->mac.num_rar_entries, 181 sizeof(struct ngbe_mac_addr), 182 GFP_KERNEL); 183 if (!adapter->mac_table) { 184 dev_err(&pdev->dev, "mac_table allocation failed: %d\n", err); 185 return -ENOMEM; 186 } 187 188 if (ngbe_init_rss_key(adapter)) 189 return -ENOMEM; 190 191 /* enable itr by default in dynamic mode */ 192 adapter->rx_itr_setting = 1; 193 adapter->tx_itr_setting = 1; 194 195 /* set default ring sizes */ 196 adapter->tx_ring_count = NGBE_DEFAULT_TXD; 197 adapter->rx_ring_count = NGBE_DEFAULT_RXD; 198 199 /* set default work limits */ 200 adapter->tx_work_limit = NGBE_DEFAULT_TX_WORK; 201 adapter->rx_work_limit = NGBE_DEFAULT_RX_WORK; 202 203 return 0; 204 } 205 206 static void ngbe_down(struct ngbe_adapter *adapter) 207 { 208 netif_carrier_off(adapter->netdev); 209 netif_tx_disable(adapter->netdev); 210 }; 211 212 /** 213 * ngbe_open - Called when a network interface is made active 214 * @netdev: network interface device structure 215 * 216 * Returns 0 on success, negative value on failure 217 * 218 * The open entry point is called when a network interface is made 219 * active by the system (IFF_UP). 220 **/ 221 static int ngbe_open(struct net_device *netdev) 222 { 223 struct ngbe_adapter *adapter = netdev_priv(netdev); 224 struct ngbe_hw *hw = &adapter->hw; 225 struct wx_hw *wxhw = &hw->wxhw; 226 227 wx_control_hw(wxhw, true); 228 229 return 0; 230 } 231 232 /** 233 * ngbe_close - Disables a network interface 234 * @netdev: network interface device structure 235 * 236 * Returns 0, this is not allowed to fail 237 * 238 * The close entry point is called when an interface is de-activated 239 * by the OS. The hardware is still under the drivers control, but 240 * needs to be disabled. A global MAC reset is issued to stop the 241 * hardware, and all transmit and receive resources are freed. 242 **/ 243 static int ngbe_close(struct net_device *netdev) 244 { 245 struct ngbe_adapter *adapter = netdev_priv(netdev); 246 247 ngbe_down(adapter); 248 wx_control_hw(&adapter->hw.wxhw, false); 249 250 return 0; 251 } 252 253 static netdev_tx_t ngbe_xmit_frame(struct sk_buff *skb, 254 struct net_device *netdev) 255 { 256 return NETDEV_TX_OK; 257 } 258 259 /** 260 * ngbe_set_mac - Change the Ethernet Address of the NIC 261 * @netdev: network interface device structure 262 * @p: pointer to an address structure 263 * 264 * Returns 0 on success, negative on failure 265 **/ 266 static int ngbe_set_mac(struct net_device *netdev, void *p) 267 { 268 struct ngbe_adapter *adapter = netdev_priv(netdev); 269 struct wx_hw *wxhw = &adapter->hw.wxhw; 270 struct sockaddr *addr = p; 271 272 if (!is_valid_ether_addr(addr->sa_data)) 273 return -EADDRNOTAVAIL; 274 275 eth_hw_addr_set(netdev, addr->sa_data); 276 memcpy(wxhw->mac.addr, addr->sa_data, netdev->addr_len); 277 278 ngbe_mac_set_default_filter(adapter, wxhw->mac.addr); 279 280 return 0; 281 } 282 283 static void ngbe_dev_shutdown(struct pci_dev *pdev, bool *enable_wake) 284 { 285 struct ngbe_adapter *adapter = pci_get_drvdata(pdev); 286 struct net_device *netdev = adapter->netdev; 287 288 netif_device_detach(netdev); 289 290 rtnl_lock(); 291 if (netif_running(netdev)) 292 ngbe_down(adapter); 293 rtnl_unlock(); 294 wx_control_hw(&adapter->hw.wxhw, false); 295 296 pci_disable_device(pdev); 297 } 298 299 static void ngbe_shutdown(struct pci_dev *pdev) 300 { 301 struct ngbe_adapter *adapter = pci_get_drvdata(pdev); 302 bool wake; 303 304 wake = !!adapter->wol; 305 306 ngbe_dev_shutdown(pdev, &wake); 307 308 if (system_state == SYSTEM_POWER_OFF) { 309 pci_wake_from_d3(pdev, wake); 310 pci_set_power_state(pdev, PCI_D3hot); 311 } 312 } 313 314 static const struct net_device_ops ngbe_netdev_ops = { 315 .ndo_open = ngbe_open, 316 .ndo_stop = ngbe_close, 317 .ndo_start_xmit = ngbe_xmit_frame, 318 .ndo_validate_addr = eth_validate_addr, 319 .ndo_set_mac_address = ngbe_set_mac, 320 }; 321 322 /** 323 * ngbe_probe - Device Initialization Routine 324 * @pdev: PCI device information struct 325 * @ent: entry in ngbe_pci_tbl 326 * 327 * Returns 0 on success, negative on failure 328 * 329 * ngbe_probe initializes an adapter identified by a pci_dev structure. 330 * The OS initialization, configuring of the adapter private structure, 331 * and a hardware reset occur. 332 **/ 333 static int ngbe_probe(struct pci_dev *pdev, 334 const struct pci_device_id __always_unused *ent) 335 { 336 struct ngbe_adapter *adapter = NULL; 337 struct ngbe_hw *hw = NULL; 338 struct wx_hw *wxhw = NULL; 339 struct net_device *netdev; 340 u32 e2rom_cksum_cap = 0; 341 static int func_nums; 342 u16 e2rom_ver = 0; 343 u32 etrack_id = 0; 344 u32 saved_ver = 0; 345 int err; 346 347 err = pci_enable_device_mem(pdev); 348 if (err) 349 return err; 350 351 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 352 if (err) { 353 dev_err(&pdev->dev, 354 "No usable DMA configuration, aborting\n"); 355 goto err_pci_disable_dev; 356 } 357 358 err = pci_request_selected_regions(pdev, 359 pci_select_bars(pdev, IORESOURCE_MEM), 360 ngbe_driver_name); 361 if (err) { 362 dev_err(&pdev->dev, 363 "pci_request_selected_regions failed %d\n", err); 364 goto err_pci_disable_dev; 365 } 366 367 pci_enable_pcie_error_reporting(pdev); 368 pci_set_master(pdev); 369 370 netdev = devm_alloc_etherdev_mqs(&pdev->dev, 371 sizeof(struct ngbe_adapter), 372 NGBE_MAX_TX_QUEUES, 373 NGBE_MAX_RX_QUEUES); 374 if (!netdev) { 375 err = -ENOMEM; 376 goto err_pci_release_regions; 377 } 378 379 SET_NETDEV_DEV(netdev, &pdev->dev); 380 381 adapter = netdev_priv(netdev); 382 adapter->netdev = netdev; 383 adapter->pdev = pdev; 384 hw = &adapter->hw; 385 wxhw = &hw->wxhw; 386 adapter->msg_enable = BIT(3) - 1; 387 388 adapter->io_addr = devm_ioremap(&pdev->dev, 389 pci_resource_start(pdev, 0), 390 pci_resource_len(pdev, 0)); 391 if (!adapter->io_addr) { 392 err = -EIO; 393 goto err_pci_release_regions; 394 } 395 396 netdev->netdev_ops = &ngbe_netdev_ops; 397 398 netdev->features |= NETIF_F_HIGHDMA; 399 400 adapter->bd_number = func_nums; 401 /* setup the private structure */ 402 err = ngbe_sw_init(adapter); 403 if (err) 404 goto err_free_mac_table; 405 406 /* check if flash load is done after hw power up */ 407 err = wx_check_flash_load(wxhw, NGBE_SPI_ILDR_STATUS_PERST); 408 if (err) 409 goto err_free_mac_table; 410 err = wx_check_flash_load(wxhw, NGBE_SPI_ILDR_STATUS_PWRRST); 411 if (err) 412 goto err_free_mac_table; 413 414 err = wx_mng_present(wxhw); 415 if (err) { 416 dev_err(&pdev->dev, "Management capability is not present\n"); 417 goto err_free_mac_table; 418 } 419 420 err = ngbe_reset_hw(hw); 421 if (err) { 422 dev_err(&pdev->dev, "HW Init failed: %d\n", err); 423 goto err_free_mac_table; 424 } 425 426 if (wxhw->bus.func == 0) { 427 wr32(wxhw, NGBE_CALSUM_CAP_STATUS, 0x0); 428 wr32(wxhw, NGBE_EEPROM_VERSION_STORE_REG, 0x0); 429 } else { 430 e2rom_cksum_cap = rd32(wxhw, NGBE_CALSUM_CAP_STATUS); 431 saved_ver = rd32(wxhw, NGBE_EEPROM_VERSION_STORE_REG); 432 } 433 434 wx_init_eeprom_params(wxhw); 435 if (wxhw->bus.func == 0 || e2rom_cksum_cap == 0) { 436 /* make sure the EEPROM is ready */ 437 err = ngbe_eeprom_chksum_hostif(hw); 438 if (err) { 439 dev_err(&pdev->dev, "The EEPROM Checksum Is Not Valid\n"); 440 err = -EIO; 441 goto err_free_mac_table; 442 } 443 } 444 445 adapter->wol = 0; 446 if (hw->wol_enabled) 447 adapter->wol = NGBE_PSR_WKUP_CTL_MAG; 448 449 hw->wol_enabled = !!(adapter->wol); 450 wr32(wxhw, NGBE_PSR_WKUP_CTL, adapter->wol); 451 452 device_set_wakeup_enable(&pdev->dev, adapter->wol); 453 454 /* Save off EEPROM version number and Option Rom version which 455 * together make a unique identify for the eeprom 456 */ 457 if (saved_ver) { 458 etrack_id = saved_ver; 459 } else { 460 wx_read_ee_hostif(wxhw, 461 wxhw->eeprom.sw_region_offset + NGBE_EEPROM_VERSION_H, 462 &e2rom_ver); 463 etrack_id = e2rom_ver << 16; 464 wx_read_ee_hostif(wxhw, 465 wxhw->eeprom.sw_region_offset + NGBE_EEPROM_VERSION_L, 466 &e2rom_ver); 467 etrack_id |= e2rom_ver; 468 wr32(wxhw, NGBE_EEPROM_VERSION_STORE_REG, etrack_id); 469 } 470 471 eth_hw_addr_set(netdev, wxhw->mac.perm_addr); 472 ngbe_mac_set_default_filter(adapter, wxhw->mac.perm_addr); 473 474 err = register_netdev(netdev); 475 if (err) 476 goto err_register; 477 478 pci_set_drvdata(pdev, adapter); 479 480 netif_info(adapter, probe, netdev, 481 "PHY: %s, PBA No: Wang Xun GbE Family Controller\n", 482 hw->phy.type == ngbe_phy_internal ? "Internal" : "External"); 483 netif_info(adapter, probe, netdev, "%pM\n", netdev->dev_addr); 484 485 return 0; 486 487 err_register: 488 wx_control_hw(wxhw, false); 489 err_free_mac_table: 490 kfree(adapter->mac_table); 491 err_pci_release_regions: 492 pci_disable_pcie_error_reporting(pdev); 493 pci_release_selected_regions(pdev, 494 pci_select_bars(pdev, IORESOURCE_MEM)); 495 err_pci_disable_dev: 496 pci_disable_device(pdev); 497 return err; 498 } 499 500 /** 501 * ngbe_remove - Device Removal Routine 502 * @pdev: PCI device information struct 503 * 504 * ngbe_remove is called by the PCI subsystem to alert the driver 505 * that it should release a PCI device. The could be caused by a 506 * Hot-Plug event, or because the driver is going to be removed from 507 * memory. 508 **/ 509 static void ngbe_remove(struct pci_dev *pdev) 510 { 511 struct ngbe_adapter *adapter = pci_get_drvdata(pdev); 512 struct net_device *netdev; 513 514 netdev = adapter->netdev; 515 unregister_netdev(netdev); 516 pci_release_selected_regions(pdev, 517 pci_select_bars(pdev, IORESOURCE_MEM)); 518 519 kfree(adapter->mac_table); 520 pci_disable_pcie_error_reporting(pdev); 521 522 pci_disable_device(pdev); 523 } 524 525 static struct pci_driver ngbe_driver = { 526 .name = ngbe_driver_name, 527 .id_table = ngbe_pci_tbl, 528 .probe = ngbe_probe, 529 .remove = ngbe_remove, 530 .shutdown = ngbe_shutdown, 531 }; 532 533 module_pci_driver(ngbe_driver); 534 535 MODULE_DEVICE_TABLE(pci, ngbe_pci_tbl); 536 MODULE_AUTHOR("Beijing WangXun Technology Co., Ltd, <software@net-swift.com>"); 537 MODULE_DESCRIPTION("WangXun(R) Gigabit PCI Express Network Driver"); 538 MODULE_LICENSE("GPL"); 539