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 	wx_update_stats(wx);
291 }
292 
293 static void txgbe_down(struct wx *wx)
294 {
295 	struct txgbe *txgbe = netdev_to_txgbe(wx->netdev);
296 
297 	txgbe_disable_device(wx);
298 	txgbe_reset(wx);
299 	phylink_stop(txgbe->phylink);
300 
301 	wx_clean_all_tx_rings(wx);
302 	wx_clean_all_rx_rings(wx);
303 }
304 
305 /**
306  *  txgbe_init_type_code - Initialize the shared code
307  *  @wx: pointer to hardware structure
308  **/
309 static void txgbe_init_type_code(struct wx *wx)
310 {
311 	u8 device_type = wx->subsystem_device_id & 0xF0;
312 
313 	switch (wx->device_id) {
314 	case TXGBE_DEV_ID_SP1000:
315 	case TXGBE_DEV_ID_WX1820:
316 		wx->mac.type = wx_mac_sp;
317 		break;
318 	default:
319 		wx->mac.type = wx_mac_unknown;
320 		break;
321 	}
322 
323 	switch (device_type) {
324 	case TXGBE_ID_SFP:
325 		wx->media_type = sp_media_fiber;
326 		break;
327 	case TXGBE_ID_XAUI:
328 	case TXGBE_ID_SGMII:
329 		wx->media_type = sp_media_copper;
330 		break;
331 	case TXGBE_ID_KR_KX_KX4:
332 	case TXGBE_ID_MAC_XAUI:
333 	case TXGBE_ID_MAC_SGMII:
334 		wx->media_type = sp_media_backplane;
335 		break;
336 	case TXGBE_ID_SFI_XAUI:
337 		if (wx->bus.func == 0)
338 			wx->media_type = sp_media_fiber;
339 		else
340 			wx->media_type = sp_media_copper;
341 		break;
342 	default:
343 		wx->media_type = sp_media_unknown;
344 		break;
345 	}
346 }
347 
348 /**
349  * txgbe_sw_init - Initialize general software structures (struct wx)
350  * @wx: board private structure to initialize
351  **/
352 static int txgbe_sw_init(struct wx *wx)
353 {
354 	u16 msix_count = 0;
355 	int err;
356 
357 	wx->mac.num_rar_entries = TXGBE_SP_RAR_ENTRIES;
358 	wx->mac.max_tx_queues = TXGBE_SP_MAX_TX_QUEUES;
359 	wx->mac.max_rx_queues = TXGBE_SP_MAX_RX_QUEUES;
360 	wx->mac.mcft_size = TXGBE_SP_MC_TBL_SIZE;
361 	wx->mac.vft_size = TXGBE_SP_VFT_TBL_SIZE;
362 	wx->mac.rx_pb_size = TXGBE_SP_RX_PB_SIZE;
363 	wx->mac.tx_pb_size = TXGBE_SP_TDB_PB_SZ;
364 
365 	/* PCI config space info */
366 	err = wx_sw_init(wx);
367 	if (err < 0)
368 		return err;
369 
370 	txgbe_init_type_code(wx);
371 
372 	/* Set common capability flags and settings */
373 	wx->max_q_vectors = TXGBE_MAX_MSIX_VECTORS;
374 	err = wx_get_pcie_msix_counts(wx, &msix_count, TXGBE_MAX_MSIX_VECTORS);
375 	if (err)
376 		wx_err(wx, "Do not support MSI-X\n");
377 	wx->mac.max_msix_vectors = msix_count;
378 
379 	/* enable itr by default in dynamic mode */
380 	wx->rx_itr_setting = 1;
381 	wx->tx_itr_setting = 1;
382 
383 	/* set default ring sizes */
384 	wx->tx_ring_count = TXGBE_DEFAULT_TXD;
385 	wx->rx_ring_count = TXGBE_DEFAULT_RXD;
386 
387 	/* set default work limits */
388 	wx->tx_work_limit = TXGBE_DEFAULT_TX_WORK;
389 	wx->rx_work_limit = TXGBE_DEFAULT_RX_WORK;
390 
391 	return 0;
392 }
393 
394 /**
395  * txgbe_open - Called when a network interface is made active
396  * @netdev: network interface device structure
397  *
398  * Returns 0 on success, negative value on failure
399  *
400  * The open entry point is called when a network interface is made
401  * active by the system (IFF_UP).
402  **/
403 static int txgbe_open(struct net_device *netdev)
404 {
405 	struct wx *wx = netdev_priv(netdev);
406 	int err;
407 
408 	err = wx_setup_resources(wx);
409 	if (err)
410 		goto err_reset;
411 
412 	wx_configure(wx);
413 
414 	err = txgbe_request_irq(wx);
415 	if (err)
416 		goto err_free_isb;
417 
418 	/* Notify the stack of the actual queue counts. */
419 	err = netif_set_real_num_tx_queues(netdev, wx->num_tx_queues);
420 	if (err)
421 		goto err_free_irq;
422 
423 	err = netif_set_real_num_rx_queues(netdev, wx->num_rx_queues);
424 	if (err)
425 		goto err_free_irq;
426 
427 	txgbe_up_complete(wx);
428 
429 	return 0;
430 
431 err_free_irq:
432 	wx_free_irq(wx);
433 err_free_isb:
434 	wx_free_isb_resources(wx);
435 err_reset:
436 	txgbe_reset(wx);
437 
438 	return err;
439 }
440 
441 /**
442  * txgbe_close_suspend - actions necessary to both suspend and close flows
443  * @wx: the private wx struct
444  *
445  * This function should contain the necessary work common to both suspending
446  * and closing of the device.
447  */
448 static void txgbe_close_suspend(struct wx *wx)
449 {
450 	txgbe_disable_device(wx);
451 	wx_free_resources(wx);
452 }
453 
454 /**
455  * txgbe_close - Disables a network interface
456  * @netdev: network interface device structure
457  *
458  * Returns 0, this is not allowed to fail
459  *
460  * The close entry point is called when an interface is de-activated
461  * by the OS.  The hardware is still under the drivers control, but
462  * needs to be disabled.  A global MAC reset is issued to stop the
463  * hardware, and all transmit and receive resources are freed.
464  **/
465 static int txgbe_close(struct net_device *netdev)
466 {
467 	struct wx *wx = netdev_priv(netdev);
468 
469 	txgbe_down(wx);
470 	wx_free_irq(wx);
471 	wx_free_resources(wx);
472 	wx_control_hw(wx, false);
473 
474 	return 0;
475 }
476 
477 static void txgbe_dev_shutdown(struct pci_dev *pdev)
478 {
479 	struct wx *wx = pci_get_drvdata(pdev);
480 	struct net_device *netdev;
481 
482 	netdev = wx->netdev;
483 	netif_device_detach(netdev);
484 
485 	rtnl_lock();
486 	if (netif_running(netdev))
487 		txgbe_close_suspend(wx);
488 	rtnl_unlock();
489 
490 	wx_control_hw(wx, false);
491 
492 	pci_disable_device(pdev);
493 }
494 
495 static void txgbe_shutdown(struct pci_dev *pdev)
496 {
497 	txgbe_dev_shutdown(pdev);
498 
499 	if (system_state == SYSTEM_POWER_OFF) {
500 		pci_wake_from_d3(pdev, false);
501 		pci_set_power_state(pdev, PCI_D3hot);
502 	}
503 }
504 
505 static const struct net_device_ops txgbe_netdev_ops = {
506 	.ndo_open               = txgbe_open,
507 	.ndo_stop               = txgbe_close,
508 	.ndo_change_mtu         = wx_change_mtu,
509 	.ndo_start_xmit         = wx_xmit_frame,
510 	.ndo_set_rx_mode        = wx_set_rx_mode,
511 	.ndo_set_features       = wx_set_features,
512 	.ndo_validate_addr      = eth_validate_addr,
513 	.ndo_set_mac_address    = wx_set_mac,
514 	.ndo_get_stats64        = wx_get_stats64,
515 	.ndo_vlan_rx_add_vid    = wx_vlan_rx_add_vid,
516 	.ndo_vlan_rx_kill_vid   = wx_vlan_rx_kill_vid,
517 };
518 
519 /**
520  * txgbe_probe - Device Initialization Routine
521  * @pdev: PCI device information struct
522  * @ent: entry in txgbe_pci_tbl
523  *
524  * Returns 0 on success, negative on failure
525  *
526  * txgbe_probe initializes an adapter identified by a pci_dev structure.
527  * The OS initialization, configuring of the wx private structure,
528  * and a hardware reset occur.
529  **/
530 static int txgbe_probe(struct pci_dev *pdev,
531 		       const struct pci_device_id __always_unused *ent)
532 {
533 	struct net_device *netdev;
534 	int err, expected_gts;
535 	struct wx *wx = NULL;
536 	struct txgbe *txgbe;
537 
538 	u16 eeprom_verh = 0, eeprom_verl = 0, offset = 0;
539 	u16 eeprom_cfg_blkh = 0, eeprom_cfg_blkl = 0;
540 	u16 build = 0, major = 0, patch = 0;
541 	u32 etrack_id = 0;
542 
543 	err = pci_enable_device_mem(pdev);
544 	if (err)
545 		return err;
546 
547 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
548 	if (err) {
549 		dev_err(&pdev->dev,
550 			"No usable DMA configuration, aborting\n");
551 		goto err_pci_disable_dev;
552 	}
553 
554 	err = pci_request_selected_regions(pdev,
555 					   pci_select_bars(pdev, IORESOURCE_MEM),
556 					   txgbe_driver_name);
557 	if (err) {
558 		dev_err(&pdev->dev,
559 			"pci_request_selected_regions failed 0x%x\n", err);
560 		goto err_pci_disable_dev;
561 	}
562 
563 	pci_set_master(pdev);
564 
565 	netdev = devm_alloc_etherdev_mqs(&pdev->dev,
566 					 sizeof(struct wx),
567 					 TXGBE_MAX_TX_QUEUES,
568 					 TXGBE_MAX_RX_QUEUES);
569 	if (!netdev) {
570 		err = -ENOMEM;
571 		goto err_pci_release_regions;
572 	}
573 
574 	SET_NETDEV_DEV(netdev, &pdev->dev);
575 
576 	wx = netdev_priv(netdev);
577 	wx->netdev = netdev;
578 	wx->pdev = pdev;
579 
580 	wx->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
581 
582 	wx->hw_addr = devm_ioremap(&pdev->dev,
583 				   pci_resource_start(pdev, 0),
584 				   pci_resource_len(pdev, 0));
585 	if (!wx->hw_addr) {
586 		err = -EIO;
587 		goto err_pci_release_regions;
588 	}
589 
590 	wx->driver_name = txgbe_driver_name;
591 	txgbe_set_ethtool_ops(netdev);
592 	netdev->netdev_ops = &txgbe_netdev_ops;
593 
594 	/* setup the private structure */
595 	err = txgbe_sw_init(wx);
596 	if (err)
597 		goto err_free_mac_table;
598 
599 	/* check if flash load is done after hw power up */
600 	err = wx_check_flash_load(wx, TXGBE_SPI_ILDR_STATUS_PERST);
601 	if (err)
602 		goto err_free_mac_table;
603 	err = wx_check_flash_load(wx, TXGBE_SPI_ILDR_STATUS_PWRRST);
604 	if (err)
605 		goto err_free_mac_table;
606 
607 	err = wx_mng_present(wx);
608 	if (err) {
609 		dev_err(&pdev->dev, "Management capability is not present\n");
610 		goto err_free_mac_table;
611 	}
612 
613 	err = txgbe_reset_hw(wx);
614 	if (err) {
615 		dev_err(&pdev->dev, "HW Init failed: %d\n", err);
616 		goto err_free_mac_table;
617 	}
618 
619 	netdev->features = NETIF_F_SG |
620 			   NETIF_F_TSO |
621 			   NETIF_F_TSO6 |
622 			   NETIF_F_RXHASH |
623 			   NETIF_F_RXCSUM |
624 			   NETIF_F_HW_CSUM;
625 
626 	netdev->gso_partial_features =  NETIF_F_GSO_ENCAP_ALL;
627 	netdev->features |= netdev->gso_partial_features;
628 	netdev->features |= NETIF_F_SCTP_CRC;
629 	netdev->vlan_features |= netdev->features | NETIF_F_TSO_MANGLEID;
630 	netdev->hw_enc_features |= netdev->vlan_features;
631 	netdev->features |= NETIF_F_VLAN_FEATURES;
632 	/* copy netdev features into list of user selectable features */
633 	netdev->hw_features |= netdev->features | NETIF_F_RXALL;
634 	netdev->hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
635 	netdev->features |= NETIF_F_HIGHDMA;
636 	netdev->hw_features |= NETIF_F_GRO;
637 	netdev->features |= NETIF_F_GRO;
638 
639 	netdev->priv_flags |= IFF_UNICAST_FLT;
640 	netdev->priv_flags |= IFF_SUPP_NOFCS;
641 
642 	netdev->min_mtu = ETH_MIN_MTU;
643 	netdev->max_mtu = WX_MAX_JUMBO_FRAME_SIZE -
644 			  (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
645 
646 	/* make sure the EEPROM is good */
647 	err = txgbe_validate_eeprom_checksum(wx, NULL);
648 	if (err != 0) {
649 		dev_err(&pdev->dev, "The EEPROM Checksum Is Not Valid\n");
650 		wr32(wx, WX_MIS_RST, WX_MIS_RST_SW_RST);
651 		err = -EIO;
652 		goto err_free_mac_table;
653 	}
654 
655 	eth_hw_addr_set(netdev, wx->mac.perm_addr);
656 	wx_mac_set_default_filter(wx, wx->mac.perm_addr);
657 
658 	err = wx_init_interrupt_scheme(wx);
659 	if (err)
660 		goto err_free_mac_table;
661 
662 	/* Save off EEPROM version number and Option Rom version which
663 	 * together make a unique identify for the eeprom
664 	 */
665 	wx_read_ee_hostif(wx,
666 			  wx->eeprom.sw_region_offset + TXGBE_EEPROM_VERSION_H,
667 			  &eeprom_verh);
668 	wx_read_ee_hostif(wx,
669 			  wx->eeprom.sw_region_offset + TXGBE_EEPROM_VERSION_L,
670 			  &eeprom_verl);
671 	etrack_id = (eeprom_verh << 16) | eeprom_verl;
672 
673 	wx_read_ee_hostif(wx,
674 			  wx->eeprom.sw_region_offset + TXGBE_ISCSI_BOOT_CONFIG,
675 			  &offset);
676 
677 	/* Make sure offset to SCSI block is valid */
678 	if (!(offset == 0x0) && !(offset == 0xffff)) {
679 		wx_read_ee_hostif(wx, offset + 0x84, &eeprom_cfg_blkh);
680 		wx_read_ee_hostif(wx, offset + 0x83, &eeprom_cfg_blkl);
681 
682 		/* Only display Option Rom if exist */
683 		if (eeprom_cfg_blkl && eeprom_cfg_blkh) {
684 			major = eeprom_cfg_blkl >> 8;
685 			build = (eeprom_cfg_blkl << 8) | (eeprom_cfg_blkh >> 8);
686 			patch = eeprom_cfg_blkh & 0x00ff;
687 
688 			snprintf(wx->eeprom_id, sizeof(wx->eeprom_id),
689 				 "0x%08x, %d.%d.%d", etrack_id, major, build,
690 				 patch);
691 		} else {
692 			snprintf(wx->eeprom_id, sizeof(wx->eeprom_id),
693 				 "0x%08x", etrack_id);
694 		}
695 	} else {
696 		snprintf(wx->eeprom_id, sizeof(wx->eeprom_id),
697 			 "0x%08x", etrack_id);
698 	}
699 
700 	if (etrack_id < 0x20010)
701 		dev_warn(&pdev->dev, "Please upgrade the firmware to 0x20010 or above.\n");
702 
703 	txgbe = devm_kzalloc(&pdev->dev, sizeof(*txgbe), GFP_KERNEL);
704 	if (!txgbe) {
705 		err = -ENOMEM;
706 		goto err_release_hw;
707 	}
708 
709 	txgbe->wx = wx;
710 	wx->priv = txgbe;
711 
712 	err = txgbe_init_phy(txgbe);
713 	if (err)
714 		goto err_release_hw;
715 
716 	err = register_netdev(netdev);
717 	if (err)
718 		goto err_remove_phy;
719 
720 	pci_set_drvdata(pdev, wx);
721 
722 	netif_tx_stop_all_queues(netdev);
723 
724 	/* calculate the expected PCIe bandwidth required for optimal
725 	 * performance. Note that some older parts will never have enough
726 	 * bandwidth due to being older generation PCIe parts. We clamp these
727 	 * parts to ensure that no warning is displayed, as this could confuse
728 	 * users otherwise.
729 	 */
730 	expected_gts = txgbe_enumerate_functions(wx) * 10;
731 
732 	/* don't check link if we failed to enumerate functions */
733 	if (expected_gts > 0)
734 		txgbe_check_minimum_link(wx);
735 	else
736 		dev_warn(&pdev->dev, "Failed to enumerate PF devices.\n");
737 
738 	return 0;
739 
740 err_remove_phy:
741 	txgbe_remove_phy(txgbe);
742 err_release_hw:
743 	wx_clear_interrupt_scheme(wx);
744 	wx_control_hw(wx, false);
745 err_free_mac_table:
746 	kfree(wx->mac_table);
747 err_pci_release_regions:
748 	pci_release_selected_regions(pdev,
749 				     pci_select_bars(pdev, IORESOURCE_MEM));
750 err_pci_disable_dev:
751 	pci_disable_device(pdev);
752 	return err;
753 }
754 
755 /**
756  * txgbe_remove - Device Removal Routine
757  * @pdev: PCI device information struct
758  *
759  * txgbe_remove is called by the PCI subsystem to alert the driver
760  * that it should release a PCI device.  The could be caused by a
761  * Hot-Plug event, or because the driver is going to be removed from
762  * memory.
763  **/
764 static void txgbe_remove(struct pci_dev *pdev)
765 {
766 	struct wx *wx = pci_get_drvdata(pdev);
767 	struct txgbe *txgbe = wx->priv;
768 	struct net_device *netdev;
769 
770 	netdev = wx->netdev;
771 	unregister_netdev(netdev);
772 
773 	txgbe_remove_phy(txgbe);
774 
775 	pci_release_selected_regions(pdev,
776 				     pci_select_bars(pdev, IORESOURCE_MEM));
777 
778 	kfree(wx->mac_table);
779 	wx_clear_interrupt_scheme(wx);
780 
781 	pci_disable_device(pdev);
782 }
783 
784 static struct pci_driver txgbe_driver = {
785 	.name     = txgbe_driver_name,
786 	.id_table = txgbe_pci_tbl,
787 	.probe    = txgbe_probe,
788 	.remove   = txgbe_remove,
789 	.shutdown = txgbe_shutdown,
790 };
791 
792 module_pci_driver(txgbe_driver);
793 
794 MODULE_DEVICE_TABLE(pci, txgbe_pci_tbl);
795 MODULE_AUTHOR("Beijing WangXun Technology Co., Ltd, <software@trustnetic.com>");
796 MODULE_DESCRIPTION("WangXun(R) 10 Gigabit PCI Express Network Driver");
797 MODULE_LICENSE("GPL");
798