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/etherdevice.h>
10 #include <linux/phylink.h>
11 #include <net/ip.h>
12 #include <linux/if_vlan.h>
13 
14 #include "../libwx/wx_type.h"
15 #include "../libwx/wx_lib.h"
16 #include "../libwx/wx_hw.h"
17 #include "txgbe_type.h"
18 #include "txgbe_hw.h"
19 #include "txgbe_phy.h"
20 #include "txgbe_ethtool.h"
21 
22 char txgbe_driver_name[] = "txgbe";
23 
24 /* txgbe_pci_tbl - PCI Device ID Table
25  *
26  * Wildcard entries (PCI_ANY_ID) should come last
27  * Last entry must be all 0s
28  *
29  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
30  *   Class, Class Mask, private data (not used) }
31  */
32 static const struct pci_device_id txgbe_pci_tbl[] = {
33 	{ PCI_VDEVICE(WANGXUN, TXGBE_DEV_ID_SP1000), 0},
34 	{ PCI_VDEVICE(WANGXUN, TXGBE_DEV_ID_WX1820), 0},
35 	/* required last entry */
36 	{ .device = 0 }
37 };
38 
39 #define DEFAULT_DEBUG_LEVEL_SHIFT 3
40 
41 static void txgbe_check_minimum_link(struct wx *wx)
42 {
43 	struct pci_dev *pdev;
44 
45 	pdev = wx->pdev;
46 	pcie_print_link_status(pdev);
47 }
48 
49 /**
50  * txgbe_enumerate_functions - Get the number of ports this device has
51  * @wx: wx structure
52  *
53  * This function enumerates the phsyical functions co-located on a single slot,
54  * in order to determine how many ports a device has. This is most useful in
55  * determining the required GT/s of PCIe bandwidth necessary for optimal
56  * performance.
57  **/
58 static int txgbe_enumerate_functions(struct wx *wx)
59 {
60 	struct pci_dev *entry, *pdev = wx->pdev;
61 	int physfns = 0;
62 
63 	list_for_each_entry(entry, &pdev->bus->devices, bus_list) {
64 		/* When the devices on the bus don't all match our device ID,
65 		 * we can't reliably determine the correct number of
66 		 * functions. This can occur if a function has been direct
67 		 * attached to a virtual machine using VT-d.
68 		 */
69 		if (entry->vendor != pdev->vendor ||
70 		    entry->device != pdev->device)
71 			return -EINVAL;
72 
73 		physfns++;
74 	}
75 
76 	return physfns;
77 }
78 
79 /**
80  * txgbe_irq_enable - Enable default interrupt generation settings
81  * @wx: pointer to private structure
82  * @queues: enable irqs for queues
83  **/
84 static void txgbe_irq_enable(struct wx *wx, bool queues)
85 {
86 	wr32(wx, WX_PX_MISC_IEN, TXGBE_PX_MISC_IEN_MASK);
87 
88 	/* unmask interrupt */
89 	wx_intr_enable(wx, TXGBE_INTR_MISC(wx));
90 	if (queues)
91 		wx_intr_enable(wx, TXGBE_INTR_QALL(wx));
92 }
93 
94 /**
95  * txgbe_intr - msi/legacy mode Interrupt Handler
96  * @irq: interrupt number
97  * @data: pointer to a network interface device structure
98  **/
99 static irqreturn_t txgbe_intr(int __always_unused irq, void *data)
100 {
101 	struct wx_q_vector *q_vector;
102 	struct wx *wx  = data;
103 	struct pci_dev *pdev;
104 	u32 eicr;
105 
106 	q_vector = wx->q_vector[0];
107 	pdev = wx->pdev;
108 
109 	eicr = wx_misc_isb(wx, WX_ISB_VEC0);
110 	if (!eicr) {
111 		/* shared interrupt alert!
112 		 * the interrupt that we masked before the ICR read.
113 		 */
114 		if (netif_running(wx->netdev))
115 			txgbe_irq_enable(wx, true);
116 		return IRQ_NONE;        /* Not our interrupt */
117 	}
118 	wx->isb_mem[WX_ISB_VEC0] = 0;
119 	if (!(pdev->msi_enabled))
120 		wr32(wx, WX_PX_INTA, 1);
121 
122 	wx->isb_mem[WX_ISB_MISC] = 0;
123 	/* would disable interrupts here but it is auto disabled */
124 	napi_schedule_irqoff(&q_vector->napi);
125 
126 	/* re-enable link(maybe) and non-queue interrupts, no flush.
127 	 * txgbe_poll will re-enable the queue interrupts
128 	 */
129 	if (netif_running(wx->netdev))
130 		txgbe_irq_enable(wx, false);
131 
132 	return IRQ_HANDLED;
133 }
134 
135 /**
136  * txgbe_request_msix_irqs - Initialize MSI-X interrupts
137  * @wx: board private structure
138  *
139  * Allocate MSI-X vectors and request interrupts from the kernel.
140  **/
141 static int txgbe_request_msix_irqs(struct wx *wx)
142 {
143 	struct net_device *netdev = wx->netdev;
144 	int vector, err;
145 
146 	for (vector = 0; vector < wx->num_q_vectors; vector++) {
147 		struct wx_q_vector *q_vector = wx->q_vector[vector];
148 		struct msix_entry *entry = &wx->msix_entries[vector];
149 
150 		if (q_vector->tx.ring && q_vector->rx.ring)
151 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
152 				 "%s-TxRx-%d", netdev->name, entry->entry);
153 		else
154 			/* skip this unused q_vector */
155 			continue;
156 
157 		err = request_irq(entry->vector, wx_msix_clean_rings, 0,
158 				  q_vector->name, q_vector);
159 		if (err) {
160 			wx_err(wx, "request_irq failed for MSIX interrupt %s Error: %d\n",
161 			       q_vector->name, err);
162 			goto free_queue_irqs;
163 		}
164 	}
165 
166 	return 0;
167 
168 free_queue_irqs:
169 	while (vector) {
170 		vector--;
171 		free_irq(wx->msix_entries[vector].vector,
172 			 wx->q_vector[vector]);
173 	}
174 	wx_reset_interrupt_capability(wx);
175 	return err;
176 }
177 
178 /**
179  * txgbe_request_irq - initialize interrupts
180  * @wx: board private structure
181  *
182  * Attempt to configure interrupts using the best available
183  * capabilities of the hardware and kernel.
184  **/
185 static int txgbe_request_irq(struct wx *wx)
186 {
187 	struct net_device *netdev = wx->netdev;
188 	struct pci_dev *pdev = wx->pdev;
189 	int err;
190 
191 	if (pdev->msix_enabled)
192 		err = txgbe_request_msix_irqs(wx);
193 	else if (pdev->msi_enabled)
194 		err = request_irq(wx->pdev->irq, &txgbe_intr, 0,
195 				  netdev->name, wx);
196 	else
197 		err = request_irq(wx->pdev->irq, &txgbe_intr, IRQF_SHARED,
198 				  netdev->name, wx);
199 
200 	if (err)
201 		wx_err(wx, "request_irq failed, Error %d\n", err);
202 
203 	return err;
204 }
205 
206 static void txgbe_up_complete(struct wx *wx)
207 {
208 	struct net_device *netdev = wx->netdev;
209 	struct txgbe *txgbe;
210 
211 	wx_control_hw(wx, true);
212 	wx_configure_vectors(wx);
213 
214 	/* make sure to complete pre-operations */
215 	smp_mb__before_atomic();
216 	wx_napi_enable_all(wx);
217 
218 	txgbe = netdev_to_txgbe(netdev);
219 	phylink_start(txgbe->phylink);
220 
221 	/* clear any pending interrupts, may auto mask */
222 	rd32(wx, WX_PX_IC(0));
223 	rd32(wx, WX_PX_IC(1));
224 	rd32(wx, WX_PX_MISC_IC);
225 	txgbe_irq_enable(wx, true);
226 
227 	/* enable transmits */
228 	netif_tx_start_all_queues(netdev);
229 }
230 
231 static void txgbe_reset(struct wx *wx)
232 {
233 	struct net_device *netdev = wx->netdev;
234 	u8 old_addr[ETH_ALEN];
235 	int err;
236 
237 	err = txgbe_reset_hw(wx);
238 	if (err != 0)
239 		wx_err(wx, "Hardware Error: %d\n", err);
240 
241 	wx_start_hw(wx);
242 	/* do not flush user set addresses */
243 	memcpy(old_addr, &wx->mac_table[0].addr, netdev->addr_len);
244 	wx_flush_sw_mac_table(wx);
245 	wx_mac_set_default_filter(wx, old_addr);
246 }
247 
248 static void txgbe_disable_device(struct wx *wx)
249 {
250 	struct net_device *netdev = wx->netdev;
251 	u32 i;
252 
253 	wx_disable_pcie_master(wx);
254 	/* disable receives */
255 	wx_disable_rx(wx);
256 
257 	/* disable all enabled rx queues */
258 	for (i = 0; i < wx->num_rx_queues; i++)
259 		/* this call also flushes the previous write */
260 		wx_disable_rx_queue(wx, wx->rx_ring[i]);
261 
262 	netif_tx_stop_all_queues(netdev);
263 	netif_tx_disable(netdev);
264 
265 	wx_irq_disable(wx);
266 	wx_napi_disable_all(wx);
267 
268 	if (wx->bus.func < 2)
269 		wr32m(wx, TXGBE_MIS_PRB_CTL, TXGBE_MIS_PRB_CTL_LAN_UP(wx->bus.func), 0);
270 	else
271 		wx_err(wx, "%s: invalid bus lan id %d\n",
272 		       __func__, wx->bus.func);
273 
274 	if (!(((wx->subsystem_device_id & WX_NCSI_MASK) == WX_NCSI_SUP) ||
275 	      ((wx->subsystem_device_id & WX_WOL_MASK) == WX_WOL_SUP))) {
276 		/* disable mac transmiter */
277 		wr32m(wx, WX_MAC_TX_CFG, WX_MAC_TX_CFG_TE, 0);
278 	}
279 
280 	/* disable transmits in the hardware now that interrupts are off */
281 	for (i = 0; i < wx->num_tx_queues; i++) {
282 		u8 reg_idx = wx->tx_ring[i]->reg_idx;
283 
284 		wr32(wx, WX_PX_TR_CFG(reg_idx), WX_PX_TR_CFG_SWFLSH);
285 	}
286 
287 	/* Disable the Tx DMA engine */
288 	wr32m(wx, WX_TDM_CTL, WX_TDM_CTL_TE, 0);
289 }
290 
291 static void txgbe_down(struct wx *wx)
292 {
293 	struct txgbe *txgbe = netdev_to_txgbe(wx->netdev);
294 
295 	txgbe_disable_device(wx);
296 	txgbe_reset(wx);
297 	phylink_stop(txgbe->phylink);
298 
299 	wx_clean_all_tx_rings(wx);
300 	wx_clean_all_rx_rings(wx);
301 }
302 
303 /**
304  * txgbe_sw_init - Initialize general software structures (struct wx)
305  * @wx: board private structure to initialize
306  **/
307 static int txgbe_sw_init(struct wx *wx)
308 {
309 	u16 msix_count = 0;
310 	int err;
311 
312 	wx->mac.num_rar_entries = TXGBE_SP_RAR_ENTRIES;
313 	wx->mac.max_tx_queues = TXGBE_SP_MAX_TX_QUEUES;
314 	wx->mac.max_rx_queues = TXGBE_SP_MAX_RX_QUEUES;
315 	wx->mac.mcft_size = TXGBE_SP_MC_TBL_SIZE;
316 	wx->mac.vft_size = TXGBE_SP_VFT_TBL_SIZE;
317 	wx->mac.rx_pb_size = TXGBE_SP_RX_PB_SIZE;
318 	wx->mac.tx_pb_size = TXGBE_SP_TDB_PB_SZ;
319 
320 	/* PCI config space info */
321 	err = wx_sw_init(wx);
322 	if (err < 0) {
323 		wx_err(wx, "read of internal subsystem device id failed\n");
324 		return err;
325 	}
326 
327 	switch (wx->device_id) {
328 	case TXGBE_DEV_ID_SP1000:
329 	case TXGBE_DEV_ID_WX1820:
330 		wx->mac.type = wx_mac_sp;
331 		break;
332 	default:
333 		wx->mac.type = wx_mac_unknown;
334 		break;
335 	}
336 
337 	/* Set common capability flags and settings */
338 	wx->max_q_vectors = TXGBE_MAX_MSIX_VECTORS;
339 	err = wx_get_pcie_msix_counts(wx, &msix_count, TXGBE_MAX_MSIX_VECTORS);
340 	if (err)
341 		wx_err(wx, "Do not support MSI-X\n");
342 	wx->mac.max_msix_vectors = msix_count;
343 
344 	/* enable itr by default in dynamic mode */
345 	wx->rx_itr_setting = 1;
346 	wx->tx_itr_setting = 1;
347 
348 	/* set default ring sizes */
349 	wx->tx_ring_count = TXGBE_DEFAULT_TXD;
350 	wx->rx_ring_count = TXGBE_DEFAULT_RXD;
351 
352 	/* set default work limits */
353 	wx->tx_work_limit = TXGBE_DEFAULT_TX_WORK;
354 	wx->rx_work_limit = TXGBE_DEFAULT_RX_WORK;
355 
356 	return 0;
357 }
358 
359 /**
360  * txgbe_open - Called when a network interface is made active
361  * @netdev: network interface device structure
362  *
363  * Returns 0 on success, negative value on failure
364  *
365  * The open entry point is called when a network interface is made
366  * active by the system (IFF_UP).
367  **/
368 static int txgbe_open(struct net_device *netdev)
369 {
370 	struct wx *wx = netdev_priv(netdev);
371 	int err;
372 
373 	err = wx_setup_resources(wx);
374 	if (err)
375 		goto err_reset;
376 
377 	wx_configure(wx);
378 
379 	err = txgbe_request_irq(wx);
380 	if (err)
381 		goto err_free_isb;
382 
383 	/* Notify the stack of the actual queue counts. */
384 	err = netif_set_real_num_tx_queues(netdev, wx->num_tx_queues);
385 	if (err)
386 		goto err_free_irq;
387 
388 	err = netif_set_real_num_rx_queues(netdev, wx->num_rx_queues);
389 	if (err)
390 		goto err_free_irq;
391 
392 	txgbe_up_complete(wx);
393 
394 	return 0;
395 
396 err_free_irq:
397 	wx_free_irq(wx);
398 err_free_isb:
399 	wx_free_isb_resources(wx);
400 err_reset:
401 	txgbe_reset(wx);
402 
403 	return err;
404 }
405 
406 /**
407  * txgbe_close_suspend - actions necessary to both suspend and close flows
408  * @wx: the private wx struct
409  *
410  * This function should contain the necessary work common to both suspending
411  * and closing of the device.
412  */
413 static void txgbe_close_suspend(struct wx *wx)
414 {
415 	txgbe_disable_device(wx);
416 	wx_free_resources(wx);
417 }
418 
419 /**
420  * txgbe_close - Disables a network interface
421  * @netdev: network interface device structure
422  *
423  * Returns 0, this is not allowed to fail
424  *
425  * The close entry point is called when an interface is de-activated
426  * by the OS.  The hardware is still under the drivers control, but
427  * needs to be disabled.  A global MAC reset is issued to stop the
428  * hardware, and all transmit and receive resources are freed.
429  **/
430 static int txgbe_close(struct net_device *netdev)
431 {
432 	struct wx *wx = netdev_priv(netdev);
433 
434 	txgbe_down(wx);
435 	wx_free_irq(wx);
436 	wx_free_resources(wx);
437 	wx_control_hw(wx, false);
438 
439 	return 0;
440 }
441 
442 static void txgbe_dev_shutdown(struct pci_dev *pdev)
443 {
444 	struct wx *wx = pci_get_drvdata(pdev);
445 	struct net_device *netdev;
446 
447 	netdev = wx->netdev;
448 	netif_device_detach(netdev);
449 
450 	rtnl_lock();
451 	if (netif_running(netdev))
452 		txgbe_close_suspend(wx);
453 	rtnl_unlock();
454 
455 	wx_control_hw(wx, false);
456 
457 	pci_disable_device(pdev);
458 }
459 
460 static void txgbe_shutdown(struct pci_dev *pdev)
461 {
462 	txgbe_dev_shutdown(pdev);
463 
464 	if (system_state == SYSTEM_POWER_OFF) {
465 		pci_wake_from_d3(pdev, false);
466 		pci_set_power_state(pdev, PCI_D3hot);
467 	}
468 }
469 
470 static const struct net_device_ops txgbe_netdev_ops = {
471 	.ndo_open               = txgbe_open,
472 	.ndo_stop               = txgbe_close,
473 	.ndo_change_mtu         = wx_change_mtu,
474 	.ndo_start_xmit         = wx_xmit_frame,
475 	.ndo_set_rx_mode        = wx_set_rx_mode,
476 	.ndo_set_features       = wx_set_features,
477 	.ndo_validate_addr      = eth_validate_addr,
478 	.ndo_set_mac_address    = wx_set_mac,
479 	.ndo_get_stats64        = wx_get_stats64,
480 	.ndo_vlan_rx_add_vid    = wx_vlan_rx_add_vid,
481 	.ndo_vlan_rx_kill_vid   = wx_vlan_rx_kill_vid,
482 };
483 
484 /**
485  * txgbe_probe - Device Initialization Routine
486  * @pdev: PCI device information struct
487  * @ent: entry in txgbe_pci_tbl
488  *
489  * Returns 0 on success, negative on failure
490  *
491  * txgbe_probe initializes an adapter identified by a pci_dev structure.
492  * The OS initialization, configuring of the wx private structure,
493  * and a hardware reset occur.
494  **/
495 static int txgbe_probe(struct pci_dev *pdev,
496 		       const struct pci_device_id __always_unused *ent)
497 {
498 	struct net_device *netdev;
499 	int err, expected_gts;
500 	struct wx *wx = NULL;
501 	struct txgbe *txgbe;
502 
503 	u16 eeprom_verh = 0, eeprom_verl = 0, offset = 0;
504 	u16 eeprom_cfg_blkh = 0, eeprom_cfg_blkl = 0;
505 	u16 build = 0, major = 0, patch = 0;
506 	u8 part_str[TXGBE_PBANUM_LENGTH];
507 	u32 etrack_id = 0;
508 
509 	err = pci_enable_device_mem(pdev);
510 	if (err)
511 		return err;
512 
513 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
514 	if (err) {
515 		dev_err(&pdev->dev,
516 			"No usable DMA configuration, aborting\n");
517 		goto err_pci_disable_dev;
518 	}
519 
520 	err = pci_request_selected_regions(pdev,
521 					   pci_select_bars(pdev, IORESOURCE_MEM),
522 					   txgbe_driver_name);
523 	if (err) {
524 		dev_err(&pdev->dev,
525 			"pci_request_selected_regions failed 0x%x\n", err);
526 		goto err_pci_disable_dev;
527 	}
528 
529 	pci_set_master(pdev);
530 
531 	netdev = devm_alloc_etherdev_mqs(&pdev->dev,
532 					 sizeof(struct wx),
533 					 TXGBE_MAX_TX_QUEUES,
534 					 TXGBE_MAX_RX_QUEUES);
535 	if (!netdev) {
536 		err = -ENOMEM;
537 		goto err_pci_release_regions;
538 	}
539 
540 	SET_NETDEV_DEV(netdev, &pdev->dev);
541 
542 	wx = netdev_priv(netdev);
543 	wx->netdev = netdev;
544 	wx->pdev = pdev;
545 
546 	wx->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
547 
548 	wx->hw_addr = devm_ioremap(&pdev->dev,
549 				   pci_resource_start(pdev, 0),
550 				   pci_resource_len(pdev, 0));
551 	if (!wx->hw_addr) {
552 		err = -EIO;
553 		goto err_pci_release_regions;
554 	}
555 
556 	wx->driver_name = txgbe_driver_name;
557 	txgbe_set_ethtool_ops(netdev);
558 	netdev->netdev_ops = &txgbe_netdev_ops;
559 
560 	/* setup the private structure */
561 	err = txgbe_sw_init(wx);
562 	if (err)
563 		goto err_free_mac_table;
564 
565 	/* check if flash load is done after hw power up */
566 	err = wx_check_flash_load(wx, TXGBE_SPI_ILDR_STATUS_PERST);
567 	if (err)
568 		goto err_free_mac_table;
569 	err = wx_check_flash_load(wx, TXGBE_SPI_ILDR_STATUS_PWRRST);
570 	if (err)
571 		goto err_free_mac_table;
572 
573 	err = wx_mng_present(wx);
574 	if (err) {
575 		dev_err(&pdev->dev, "Management capability is not present\n");
576 		goto err_free_mac_table;
577 	}
578 
579 	err = txgbe_reset_hw(wx);
580 	if (err) {
581 		dev_err(&pdev->dev, "HW Init failed: %d\n", err);
582 		goto err_free_mac_table;
583 	}
584 
585 	netdev->features = NETIF_F_SG |
586 			   NETIF_F_TSO |
587 			   NETIF_F_TSO6 |
588 			   NETIF_F_RXHASH |
589 			   NETIF_F_RXCSUM |
590 			   NETIF_F_HW_CSUM;
591 
592 	netdev->gso_partial_features =  NETIF_F_GSO_ENCAP_ALL;
593 	netdev->features |= netdev->gso_partial_features;
594 	netdev->features |= NETIF_F_SCTP_CRC;
595 	netdev->vlan_features |= netdev->features | NETIF_F_TSO_MANGLEID;
596 	netdev->hw_enc_features |= netdev->vlan_features;
597 	netdev->features |= NETIF_F_VLAN_FEATURES;
598 	/* copy netdev features into list of user selectable features */
599 	netdev->hw_features |= netdev->features | NETIF_F_RXALL;
600 	netdev->hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
601 	netdev->features |= NETIF_F_HIGHDMA;
602 	netdev->hw_features |= NETIF_F_GRO;
603 	netdev->features |= NETIF_F_GRO;
604 
605 	netdev->priv_flags |= IFF_UNICAST_FLT;
606 	netdev->priv_flags |= IFF_SUPP_NOFCS;
607 
608 	netdev->min_mtu = ETH_MIN_MTU;
609 	netdev->max_mtu = WX_MAX_JUMBO_FRAME_SIZE -
610 			  (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
611 
612 	/* make sure the EEPROM is good */
613 	err = txgbe_validate_eeprom_checksum(wx, NULL);
614 	if (err != 0) {
615 		dev_err(&pdev->dev, "The EEPROM Checksum Is Not Valid\n");
616 		wr32(wx, WX_MIS_RST, WX_MIS_RST_SW_RST);
617 		err = -EIO;
618 		goto err_free_mac_table;
619 	}
620 
621 	eth_hw_addr_set(netdev, wx->mac.perm_addr);
622 	wx_mac_set_default_filter(wx, wx->mac.perm_addr);
623 
624 	err = wx_init_interrupt_scheme(wx);
625 	if (err)
626 		goto err_free_mac_table;
627 
628 	/* Save off EEPROM version number and Option Rom version which
629 	 * together make a unique identify for the eeprom
630 	 */
631 	wx_read_ee_hostif(wx,
632 			  wx->eeprom.sw_region_offset + TXGBE_EEPROM_VERSION_H,
633 			  &eeprom_verh);
634 	wx_read_ee_hostif(wx,
635 			  wx->eeprom.sw_region_offset + TXGBE_EEPROM_VERSION_L,
636 			  &eeprom_verl);
637 	etrack_id = (eeprom_verh << 16) | eeprom_verl;
638 
639 	wx_read_ee_hostif(wx,
640 			  wx->eeprom.sw_region_offset + TXGBE_ISCSI_BOOT_CONFIG,
641 			  &offset);
642 
643 	/* Make sure offset to SCSI block is valid */
644 	if (!(offset == 0x0) && !(offset == 0xffff)) {
645 		wx_read_ee_hostif(wx, offset + 0x84, &eeprom_cfg_blkh);
646 		wx_read_ee_hostif(wx, offset + 0x83, &eeprom_cfg_blkl);
647 
648 		/* Only display Option Rom if exist */
649 		if (eeprom_cfg_blkl && eeprom_cfg_blkh) {
650 			major = eeprom_cfg_blkl >> 8;
651 			build = (eeprom_cfg_blkl << 8) | (eeprom_cfg_blkh >> 8);
652 			patch = eeprom_cfg_blkh & 0x00ff;
653 
654 			snprintf(wx->eeprom_id, sizeof(wx->eeprom_id),
655 				 "0x%08x, %d.%d.%d", etrack_id, major, build,
656 				 patch);
657 		} else {
658 			snprintf(wx->eeprom_id, sizeof(wx->eeprom_id),
659 				 "0x%08x", etrack_id);
660 		}
661 	} else {
662 		snprintf(wx->eeprom_id, sizeof(wx->eeprom_id),
663 			 "0x%08x", etrack_id);
664 	}
665 
666 	txgbe = devm_kzalloc(&pdev->dev, sizeof(*txgbe), GFP_KERNEL);
667 	if (!txgbe) {
668 		err = -ENOMEM;
669 		goto err_release_hw;
670 	}
671 
672 	txgbe->wx = wx;
673 	wx->priv = txgbe;
674 
675 	err = txgbe_init_phy(txgbe);
676 	if (err)
677 		goto err_release_hw;
678 
679 	err = register_netdev(netdev);
680 	if (err)
681 		goto err_remove_phy;
682 
683 	pci_set_drvdata(pdev, wx);
684 
685 	netif_tx_stop_all_queues(netdev);
686 
687 	/* calculate the expected PCIe bandwidth required for optimal
688 	 * performance. Note that some older parts will never have enough
689 	 * bandwidth due to being older generation PCIe parts. We clamp these
690 	 * parts to ensure that no warning is displayed, as this could confuse
691 	 * users otherwise.
692 	 */
693 	expected_gts = txgbe_enumerate_functions(wx) * 10;
694 
695 	/* don't check link if we failed to enumerate functions */
696 	if (expected_gts > 0)
697 		txgbe_check_minimum_link(wx);
698 	else
699 		dev_warn(&pdev->dev, "Failed to enumerate PF devices.\n");
700 
701 	/* First try to read PBA as a string */
702 	err = txgbe_read_pba_string(wx, part_str, TXGBE_PBANUM_LENGTH);
703 	if (err)
704 		strncpy(part_str, "Unknown", TXGBE_PBANUM_LENGTH);
705 
706 	netif_info(wx, probe, netdev, "%pM\n", netdev->dev_addr);
707 
708 	return 0;
709 
710 err_remove_phy:
711 	txgbe_remove_phy(txgbe);
712 err_release_hw:
713 	wx_clear_interrupt_scheme(wx);
714 	wx_control_hw(wx, false);
715 err_free_mac_table:
716 	kfree(wx->mac_table);
717 err_pci_release_regions:
718 	pci_release_selected_regions(pdev,
719 				     pci_select_bars(pdev, IORESOURCE_MEM));
720 err_pci_disable_dev:
721 	pci_disable_device(pdev);
722 	return err;
723 }
724 
725 /**
726  * txgbe_remove - Device Removal Routine
727  * @pdev: PCI device information struct
728  *
729  * txgbe_remove is called by the PCI subsystem to alert the driver
730  * that it should release a PCI device.  The could be caused by a
731  * Hot-Plug event, or because the driver is going to be removed from
732  * memory.
733  **/
734 static void txgbe_remove(struct pci_dev *pdev)
735 {
736 	struct wx *wx = pci_get_drvdata(pdev);
737 	struct txgbe *txgbe = wx->priv;
738 	struct net_device *netdev;
739 
740 	netdev = wx->netdev;
741 	unregister_netdev(netdev);
742 
743 	txgbe_remove_phy(txgbe);
744 
745 	pci_release_selected_regions(pdev,
746 				     pci_select_bars(pdev, IORESOURCE_MEM));
747 
748 	kfree(wx->mac_table);
749 	wx_clear_interrupt_scheme(wx);
750 
751 	pci_disable_device(pdev);
752 }
753 
754 static struct pci_driver txgbe_driver = {
755 	.name     = txgbe_driver_name,
756 	.id_table = txgbe_pci_tbl,
757 	.probe    = txgbe_probe,
758 	.remove   = txgbe_remove,
759 	.shutdown = txgbe_shutdown,
760 };
761 
762 module_pci_driver(txgbe_driver);
763 
764 MODULE_DEVICE_TABLE(pci, txgbe_pci_tbl);
765 MODULE_AUTHOR("Beijing WangXun Technology Co., Ltd, <software@trustnetic.com>");
766 MODULE_DESCRIPTION("WangXun(R) 10 Gigabit PCI Express Network Driver");
767 MODULE_LICENSE("GPL");
768