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