xref: /linux/drivers/net/ethernet/wangxun/txgbe/txgbe_main.c (revision c8b8b8190a80b591aa73c27c70a668799f8db547)
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_ethtool.h"
22 
23 char txgbe_driver_name[] = "txgbe";
24 
25 /* txgbe_pci_tbl - PCI Device ID Table
26  *
27  * Wildcard entries (PCI_ANY_ID) should come last
28  * Last entry must be all 0s
29  *
30  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
31  *   Class, Class Mask, private data (not used) }
32  */
33 static const struct pci_device_id txgbe_pci_tbl[] = {
34 	{ PCI_VDEVICE(WANGXUN, TXGBE_DEV_ID_SP1000), 0},
35 	{ PCI_VDEVICE(WANGXUN, TXGBE_DEV_ID_WX1820), 0},
36 	/* required last entry */
37 	{ .device = 0 }
38 };
39 
40 #define DEFAULT_DEBUG_LEVEL_SHIFT 3
41 
42 static void txgbe_check_minimum_link(struct wx *wx)
43 {
44 	struct pci_dev *pdev;
45 
46 	pdev = wx->pdev;
47 	pcie_print_link_status(pdev);
48 }
49 
50 /**
51  * txgbe_enumerate_functions - Get the number of ports this device has
52  * @wx: wx structure
53  *
54  * This function enumerates the phsyical functions co-located on a single slot,
55  * in order to determine how many ports a device has. This is most useful in
56  * determining the required GT/s of PCIe bandwidth necessary for optimal
57  * performance.
58  **/
59 static int txgbe_enumerate_functions(struct wx *wx)
60 {
61 	struct pci_dev *entry, *pdev = wx->pdev;
62 	int physfns = 0;
63 
64 	list_for_each_entry(entry, &pdev->bus->devices, bus_list) {
65 		/* When the devices on the bus don't all match our device ID,
66 		 * we can't reliably determine the correct number of
67 		 * functions. This can occur if a function has been direct
68 		 * attached to a virtual machine using VT-d.
69 		 */
70 		if (entry->vendor != pdev->vendor ||
71 		    entry->device != pdev->device)
72 			return -EINVAL;
73 
74 		physfns++;
75 	}
76 
77 	return physfns;
78 }
79 
80 static void txgbe_up_complete(struct wx *wx)
81 {
82 	struct net_device *netdev = wx->netdev;
83 
84 	txgbe_reinit_gpio_intr(wx);
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 
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 
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 
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 
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  **/
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  **/
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 	/* enable itr by default in dynamic mode */
261 	wx->rx_itr_setting = 1;
262 	wx->tx_itr_setting = 1;
263 
264 	/* set default ring sizes */
265 	wx->tx_ring_count = TXGBE_DEFAULT_TXD;
266 	wx->rx_ring_count = TXGBE_DEFAULT_RXD;
267 
268 	/* set default work limits */
269 	wx->tx_work_limit = TXGBE_DEFAULT_TX_WORK;
270 	wx->rx_work_limit = TXGBE_DEFAULT_RX_WORK;
271 
272 	wx->do_reset = txgbe_do_reset;
273 
274 	return 0;
275 }
276 
277 /**
278  * txgbe_open - Called when a network interface is made active
279  * @netdev: network interface device structure
280  *
281  * Returns 0 on success, negative value on failure
282  *
283  * The open entry point is called when a network interface is made
284  * active by the system (IFF_UP).
285  **/
286 static int txgbe_open(struct net_device *netdev)
287 {
288 	struct wx *wx = netdev_priv(netdev);
289 	int err;
290 
291 	err = wx_setup_resources(wx);
292 	if (err)
293 		goto err_reset;
294 
295 	wx_configure(wx);
296 
297 	err = txgbe_request_queue_irqs(wx);
298 	if (err)
299 		goto err_free_resources;
300 
301 	/* Notify the stack of the actual queue counts. */
302 	err = netif_set_real_num_tx_queues(netdev, wx->num_tx_queues);
303 	if (err)
304 		goto err_free_irq;
305 
306 	err = netif_set_real_num_rx_queues(netdev, wx->num_rx_queues);
307 	if (err)
308 		goto err_free_irq;
309 
310 	txgbe_up_complete(wx);
311 
312 	return 0;
313 
314 err_free_irq:
315 	wx_free_irq(wx);
316 err_free_resources:
317 	wx_free_resources(wx);
318 err_reset:
319 	txgbe_reset(wx);
320 
321 	return err;
322 }
323 
324 /**
325  * txgbe_close_suspend - actions necessary to both suspend and close flows
326  * @wx: the private wx struct
327  *
328  * This function should contain the necessary work common to both suspending
329  * and closing of the device.
330  */
331 static void txgbe_close_suspend(struct wx *wx)
332 {
333 	txgbe_disable_device(wx);
334 	wx_free_resources(wx);
335 }
336 
337 /**
338  * txgbe_close - Disables a network interface
339  * @netdev: network interface device structure
340  *
341  * Returns 0, this is not allowed to fail
342  *
343  * The close entry point is called when an interface is de-activated
344  * by the OS.  The hardware is still under the drivers control, but
345  * needs to be disabled.  A global MAC reset is issued to stop the
346  * hardware, and all transmit and receive resources are freed.
347  **/
348 static int txgbe_close(struct net_device *netdev)
349 {
350 	struct wx *wx = netdev_priv(netdev);
351 
352 	txgbe_down(wx);
353 	wx_free_irq(wx);
354 	wx_free_resources(wx);
355 	wx_control_hw(wx, false);
356 
357 	return 0;
358 }
359 
360 static void txgbe_dev_shutdown(struct pci_dev *pdev)
361 {
362 	struct wx *wx = pci_get_drvdata(pdev);
363 	struct net_device *netdev;
364 
365 	netdev = wx->netdev;
366 	netif_device_detach(netdev);
367 
368 	rtnl_lock();
369 	if (netif_running(netdev))
370 		txgbe_close_suspend(wx);
371 	rtnl_unlock();
372 
373 	wx_control_hw(wx, false);
374 
375 	pci_disable_device(pdev);
376 }
377 
378 static void txgbe_shutdown(struct pci_dev *pdev)
379 {
380 	txgbe_dev_shutdown(pdev);
381 
382 	if (system_state == SYSTEM_POWER_OFF) {
383 		pci_wake_from_d3(pdev, false);
384 		pci_set_power_state(pdev, PCI_D3hot);
385 	}
386 }
387 
388 /**
389  * txgbe_setup_tc - routine to configure net_device for multiple traffic
390  * classes.
391  *
392  * @dev: net device to configure
393  * @tc: number of traffic classes to enable
394  */
395 int txgbe_setup_tc(struct net_device *dev, u8 tc)
396 {
397 	struct wx *wx = netdev_priv(dev);
398 	struct txgbe *txgbe = wx->priv;
399 
400 	/* Hardware has to reinitialize queues and interrupts to
401 	 * match packet buffer alignment. Unfortunately, the
402 	 * hardware is not flexible enough to do this dynamically.
403 	 */
404 	if (netif_running(dev))
405 		txgbe_close(dev);
406 	else
407 		txgbe_reset(wx);
408 
409 	txgbe_free_misc_irq(txgbe);
410 	wx_clear_interrupt_scheme(wx);
411 
412 	if (tc)
413 		netdev_set_num_tc(dev, tc);
414 	else
415 		netdev_reset_tc(dev);
416 
417 	wx_init_interrupt_scheme(wx);
418 	txgbe_setup_misc_irq(txgbe);
419 
420 	if (netif_running(dev))
421 		txgbe_open(dev);
422 
423 	return 0;
424 }
425 
426 static void txgbe_reinit_locked(struct wx *wx)
427 {
428 	int err = 0;
429 
430 	netif_trans_update(wx->netdev);
431 
432 	err = wx_set_state_reset(wx);
433 	if (err) {
434 		wx_err(wx, "wait device reset timeout\n");
435 		return;
436 	}
437 
438 	txgbe_down(wx);
439 	txgbe_up(wx);
440 
441 	clear_bit(WX_STATE_RESETTING, wx->state);
442 }
443 
444 void txgbe_do_reset(struct net_device *netdev)
445 {
446 	struct wx *wx = netdev_priv(netdev);
447 
448 	if (netif_running(netdev))
449 		txgbe_reinit_locked(wx);
450 	else
451 		txgbe_reset(wx);
452 }
453 
454 static const struct net_device_ops txgbe_netdev_ops = {
455 	.ndo_open               = txgbe_open,
456 	.ndo_stop               = txgbe_close,
457 	.ndo_change_mtu         = wx_change_mtu,
458 	.ndo_start_xmit         = wx_xmit_frame,
459 	.ndo_set_rx_mode        = wx_set_rx_mode,
460 	.ndo_set_features       = wx_set_features,
461 	.ndo_fix_features       = wx_fix_features,
462 	.ndo_validate_addr      = eth_validate_addr,
463 	.ndo_set_mac_address    = wx_set_mac,
464 	.ndo_get_stats64        = wx_get_stats64,
465 	.ndo_vlan_rx_add_vid    = wx_vlan_rx_add_vid,
466 	.ndo_vlan_rx_kill_vid   = wx_vlan_rx_kill_vid,
467 };
468 
469 /**
470  * txgbe_probe - Device Initialization Routine
471  * @pdev: PCI device information struct
472  * @ent: entry in txgbe_pci_tbl
473  *
474  * Returns 0 on success, negative on failure
475  *
476  * txgbe_probe initializes an adapter identified by a pci_dev structure.
477  * The OS initialization, configuring of the wx private structure,
478  * and a hardware reset occur.
479  **/
480 static int txgbe_probe(struct pci_dev *pdev,
481 		       const struct pci_device_id __always_unused *ent)
482 {
483 	struct net_device *netdev;
484 	int err, expected_gts;
485 	struct wx *wx = NULL;
486 	struct txgbe *txgbe;
487 
488 	u16 eeprom_verh = 0, eeprom_verl = 0, offset = 0;
489 	u16 eeprom_cfg_blkh = 0, eeprom_cfg_blkl = 0;
490 	u16 build = 0, major = 0, patch = 0;
491 	u32 etrack_id = 0;
492 
493 	err = pci_enable_device_mem(pdev);
494 	if (err)
495 		return err;
496 
497 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
498 	if (err) {
499 		dev_err(&pdev->dev,
500 			"No usable DMA configuration, aborting\n");
501 		goto err_pci_disable_dev;
502 	}
503 
504 	err = pci_request_selected_regions(pdev,
505 					   pci_select_bars(pdev, IORESOURCE_MEM),
506 					   txgbe_driver_name);
507 	if (err) {
508 		dev_err(&pdev->dev,
509 			"pci_request_selected_regions failed 0x%x\n", err);
510 		goto err_pci_disable_dev;
511 	}
512 
513 	pci_set_master(pdev);
514 
515 	netdev = devm_alloc_etherdev_mqs(&pdev->dev,
516 					 sizeof(struct wx),
517 					 TXGBE_MAX_TX_QUEUES,
518 					 TXGBE_MAX_RX_QUEUES);
519 	if (!netdev) {
520 		err = -ENOMEM;
521 		goto err_pci_release_regions;
522 	}
523 
524 	SET_NETDEV_DEV(netdev, &pdev->dev);
525 
526 	wx = netdev_priv(netdev);
527 	wx->netdev = netdev;
528 	wx->pdev = pdev;
529 
530 	wx->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
531 
532 	wx->hw_addr = devm_ioremap(&pdev->dev,
533 				   pci_resource_start(pdev, 0),
534 				   pci_resource_len(pdev, 0));
535 	if (!wx->hw_addr) {
536 		err = -EIO;
537 		goto err_pci_release_regions;
538 	}
539 
540 	wx->driver_name = txgbe_driver_name;
541 	txgbe_set_ethtool_ops(netdev);
542 	netdev->netdev_ops = &txgbe_netdev_ops;
543 
544 	/* setup the private structure */
545 	err = txgbe_sw_init(wx);
546 	if (err)
547 		goto err_free_mac_table;
548 
549 	/* check if flash load is done after hw power up */
550 	err = wx_check_flash_load(wx, TXGBE_SPI_ILDR_STATUS_PERST);
551 	if (err)
552 		goto err_free_mac_table;
553 	err = wx_check_flash_load(wx, TXGBE_SPI_ILDR_STATUS_PWRRST);
554 	if (err)
555 		goto err_free_mac_table;
556 
557 	err = wx_mng_present(wx);
558 	if (err) {
559 		dev_err(&pdev->dev, "Management capability is not present\n");
560 		goto err_free_mac_table;
561 	}
562 
563 	err = txgbe_reset_hw(wx);
564 	if (err) {
565 		dev_err(&pdev->dev, "HW Init failed: %d\n", err);
566 		goto err_free_mac_table;
567 	}
568 
569 	netdev->features = NETIF_F_SG |
570 			   NETIF_F_TSO |
571 			   NETIF_F_TSO6 |
572 			   NETIF_F_RXHASH |
573 			   NETIF_F_RXCSUM |
574 			   NETIF_F_HW_CSUM;
575 
576 	netdev->gso_partial_features =  NETIF_F_GSO_ENCAP_ALL;
577 	netdev->features |= netdev->gso_partial_features;
578 	netdev->features |= NETIF_F_SCTP_CRC;
579 	netdev->vlan_features |= netdev->features | NETIF_F_TSO_MANGLEID;
580 	netdev->hw_enc_features |= netdev->vlan_features;
581 	netdev->features |= NETIF_F_VLAN_FEATURES;
582 	/* copy netdev features into list of user selectable features */
583 	netdev->hw_features |= netdev->features | NETIF_F_RXALL;
584 	netdev->hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
585 	netdev->features |= NETIF_F_HIGHDMA;
586 	netdev->hw_features |= NETIF_F_GRO;
587 	netdev->features |= NETIF_F_GRO;
588 
589 	netdev->priv_flags |= IFF_UNICAST_FLT;
590 	netdev->priv_flags |= IFF_SUPP_NOFCS;
591 	netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
592 
593 	netdev->min_mtu = ETH_MIN_MTU;
594 	netdev->max_mtu = WX_MAX_JUMBO_FRAME_SIZE -
595 			  (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
596 
597 	/* make sure the EEPROM is good */
598 	err = txgbe_validate_eeprom_checksum(wx, NULL);
599 	if (err != 0) {
600 		dev_err(&pdev->dev, "The EEPROM Checksum Is Not Valid\n");
601 		wr32(wx, WX_MIS_RST, WX_MIS_RST_SW_RST);
602 		err = -EIO;
603 		goto err_free_mac_table;
604 	}
605 
606 	eth_hw_addr_set(netdev, wx->mac.perm_addr);
607 	wx_mac_set_default_filter(wx, wx->mac.perm_addr);
608 
609 	err = wx_init_interrupt_scheme(wx);
610 	if (err)
611 		goto err_free_mac_table;
612 
613 	/* Save off EEPROM version number and Option Rom version which
614 	 * together make a unique identify for the eeprom
615 	 */
616 	wx_read_ee_hostif(wx,
617 			  wx->eeprom.sw_region_offset + TXGBE_EEPROM_VERSION_H,
618 			  &eeprom_verh);
619 	wx_read_ee_hostif(wx,
620 			  wx->eeprom.sw_region_offset + TXGBE_EEPROM_VERSION_L,
621 			  &eeprom_verl);
622 	etrack_id = (eeprom_verh << 16) | eeprom_verl;
623 
624 	wx_read_ee_hostif(wx,
625 			  wx->eeprom.sw_region_offset + TXGBE_ISCSI_BOOT_CONFIG,
626 			  &offset);
627 
628 	/* Make sure offset to SCSI block is valid */
629 	if (!(offset == 0x0) && !(offset == 0xffff)) {
630 		wx_read_ee_hostif(wx, offset + 0x84, &eeprom_cfg_blkh);
631 		wx_read_ee_hostif(wx, offset + 0x83, &eeprom_cfg_blkl);
632 
633 		/* Only display Option Rom if exist */
634 		if (eeprom_cfg_blkl && eeprom_cfg_blkh) {
635 			major = eeprom_cfg_blkl >> 8;
636 			build = (eeprom_cfg_blkl << 8) | (eeprom_cfg_blkh >> 8);
637 			patch = eeprom_cfg_blkh & 0x00ff;
638 
639 			snprintf(wx->eeprom_id, sizeof(wx->eeprom_id),
640 				 "0x%08x, %d.%d.%d", etrack_id, major, build,
641 				 patch);
642 		} else {
643 			snprintf(wx->eeprom_id, sizeof(wx->eeprom_id),
644 				 "0x%08x", etrack_id);
645 		}
646 	} else {
647 		snprintf(wx->eeprom_id, sizeof(wx->eeprom_id),
648 			 "0x%08x", etrack_id);
649 	}
650 
651 	if (etrack_id < 0x20010)
652 		dev_warn(&pdev->dev, "Please upgrade the firmware to 0x20010 or above.\n");
653 
654 	txgbe = devm_kzalloc(&pdev->dev, sizeof(*txgbe), GFP_KERNEL);
655 	if (!txgbe) {
656 		err = -ENOMEM;
657 		goto err_release_hw;
658 	}
659 
660 	txgbe->wx = wx;
661 	wx->priv = txgbe;
662 
663 	err = txgbe_setup_misc_irq(txgbe);
664 	if (err)
665 		goto err_release_hw;
666 
667 	err = txgbe_init_phy(txgbe);
668 	if (err)
669 		goto err_free_misc_irq;
670 
671 	err = register_netdev(netdev);
672 	if (err)
673 		goto err_remove_phy;
674 
675 	pci_set_drvdata(pdev, wx);
676 
677 	netif_tx_stop_all_queues(netdev);
678 
679 	/* calculate the expected PCIe bandwidth required for optimal
680 	 * performance. Note that some older parts will never have enough
681 	 * bandwidth due to being older generation PCIe parts. We clamp these
682 	 * parts to ensure that no warning is displayed, as this could confuse
683 	 * users otherwise.
684 	 */
685 	expected_gts = txgbe_enumerate_functions(wx) * 10;
686 
687 	/* don't check link if we failed to enumerate functions */
688 	if (expected_gts > 0)
689 		txgbe_check_minimum_link(wx);
690 	else
691 		dev_warn(&pdev->dev, "Failed to enumerate PF devices.\n");
692 
693 	return 0;
694 
695 err_remove_phy:
696 	txgbe_remove_phy(txgbe);
697 err_free_misc_irq:
698 	txgbe_free_misc_irq(txgbe);
699 err_release_hw:
700 	wx_clear_interrupt_scheme(wx);
701 	wx_control_hw(wx, false);
702 err_free_mac_table:
703 	kfree(wx->mac_table);
704 err_pci_release_regions:
705 	pci_release_selected_regions(pdev,
706 				     pci_select_bars(pdev, IORESOURCE_MEM));
707 err_pci_disable_dev:
708 	pci_disable_device(pdev);
709 	return err;
710 }
711 
712 /**
713  * txgbe_remove - Device Removal Routine
714  * @pdev: PCI device information struct
715  *
716  * txgbe_remove is called by the PCI subsystem to alert the driver
717  * that it should release a PCI device.  The could be caused by a
718  * Hot-Plug event, or because the driver is going to be removed from
719  * memory.
720  **/
721 static void txgbe_remove(struct pci_dev *pdev)
722 {
723 	struct wx *wx = pci_get_drvdata(pdev);
724 	struct txgbe *txgbe = wx->priv;
725 	struct net_device *netdev;
726 
727 	netdev = wx->netdev;
728 	unregister_netdev(netdev);
729 
730 	txgbe_remove_phy(txgbe);
731 	txgbe_free_misc_irq(txgbe);
732 	wx_free_isb_resources(wx);
733 
734 	pci_release_selected_regions(pdev,
735 				     pci_select_bars(pdev, IORESOURCE_MEM));
736 
737 	kfree(wx->rss_key);
738 	kfree(wx->mac_table);
739 	wx_clear_interrupt_scheme(wx);
740 
741 	pci_disable_device(pdev);
742 }
743 
744 static struct pci_driver txgbe_driver = {
745 	.name     = txgbe_driver_name,
746 	.id_table = txgbe_pci_tbl,
747 	.probe    = txgbe_probe,
748 	.remove   = txgbe_remove,
749 	.shutdown = txgbe_shutdown,
750 };
751 
752 module_pci_driver(txgbe_driver);
753 
754 MODULE_DEVICE_TABLE(pci, txgbe_pci_tbl);
755 MODULE_AUTHOR("Beijing WangXun Technology Co., Ltd, <software@trustnetic.com>");
756 MODULE_DESCRIPTION("WangXun(R) 10 Gigabit PCI Express Network Driver");
757 MODULE_LICENSE("GPL");
758