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