xref: /linux/drivers/net/ethernet/wangxun/ngbe/ngbe_main.c (revision 561add0da6d3d07c9bccb0832fb6ed5619167d26)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 - 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 <net/ip.h>
11 #include <linux/phy.h>
12 #include <linux/if_vlan.h>
13 
14 #include "../libwx/wx_type.h"
15 #include "../libwx/wx_hw.h"
16 #include "../libwx/wx_lib.h"
17 #include "ngbe_type.h"
18 #include "ngbe_mdio.h"
19 #include "ngbe_hw.h"
20 #include "ngbe_ethtool.h"
21 
22 char ngbe_driver_name[] = "ngbe";
23 
24 /* ngbe_pci_tbl - PCI Device ID Table
25  *
26  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
27  *   Class, Class Mask, private data (not used) }
28  */
29 static const struct pci_device_id ngbe_pci_tbl[] = {
30 	{ PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860AL_W), 0},
31 	{ PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860A2), 0},
32 	{ PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860A2S), 0},
33 	{ PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860A4), 0},
34 	{ PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860A4S), 0},
35 	{ PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860AL2), 0},
36 	{ PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860AL2S), 0},
37 	{ PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860AL4), 0},
38 	{ PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860AL4S), 0},
39 	{ PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860LC), 0},
40 	{ PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860A1), 0},
41 	{ PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860A1L), 0},
42 	/* required last entry */
43 	{ .device = 0 }
44 };
45 
46 /**
47  *  ngbe_init_type_code - Initialize the shared code
48  *  @wx: pointer to hardware structure
49  **/
50 static void ngbe_init_type_code(struct wx *wx)
51 {
52 	int wol_mask = 0, ncsi_mask = 0;
53 	u16 type_mask = 0, val;
54 
55 	wx->mac.type = wx_mac_em;
56 	type_mask = (u16)(wx->subsystem_device_id & NGBE_OEM_MASK);
57 	ncsi_mask = wx->subsystem_device_id & NGBE_NCSI_MASK;
58 	wol_mask = wx->subsystem_device_id & NGBE_WOL_MASK;
59 
60 	val = rd32(wx, WX_CFG_PORT_ST);
61 	wx->mac_type = (val & BIT(7)) >> 7 ?
62 		       em_mac_type_rgmii :
63 		       em_mac_type_mdi;
64 
65 	wx->wol_hw_supported = (wol_mask == NGBE_WOL_SUP) ? 1 : 0;
66 	wx->ncsi_enabled = (ncsi_mask == NGBE_NCSI_MASK ||
67 			   type_mask == NGBE_SUBID_OCP_CARD) ? 1 : 0;
68 
69 	switch (type_mask) {
70 	case NGBE_SUBID_LY_YT8521S_SFP:
71 	case NGBE_SUBID_LY_M88E1512_SFP:
72 	case NGBE_SUBID_YT8521S_SFP_GPIO:
73 	case NGBE_SUBID_INTERNAL_YT8521S_SFP_GPIO:
74 		wx->gpio_ctrl = 1;
75 		break;
76 	default:
77 		wx->gpio_ctrl = 0;
78 		break;
79 	}
80 }
81 
82 /**
83  * ngbe_init_rss_key - Initialize wx RSS key
84  * @wx: device handle
85  *
86  * Allocates and initializes the RSS key if it is not allocated.
87  **/
88 static inline int ngbe_init_rss_key(struct wx *wx)
89 {
90 	u32 *rss_key;
91 
92 	if (!wx->rss_key) {
93 		rss_key = kzalloc(WX_RSS_KEY_SIZE, GFP_KERNEL);
94 		if (unlikely(!rss_key))
95 			return -ENOMEM;
96 
97 		netdev_rss_key_fill(rss_key, WX_RSS_KEY_SIZE);
98 		wx->rss_key = rss_key;
99 	}
100 
101 	return 0;
102 }
103 
104 /**
105  * ngbe_sw_init - Initialize general software structures
106  * @wx: board private structure to initialize
107  **/
108 static int ngbe_sw_init(struct wx *wx)
109 {
110 	struct pci_dev *pdev = wx->pdev;
111 	u16 msix_count = 0;
112 	int err = 0;
113 
114 	wx->mac.num_rar_entries = NGBE_RAR_ENTRIES;
115 	wx->mac.max_rx_queues = NGBE_MAX_RX_QUEUES;
116 	wx->mac.max_tx_queues = NGBE_MAX_TX_QUEUES;
117 	wx->mac.mcft_size = NGBE_MC_TBL_SIZE;
118 	wx->mac.vft_size = NGBE_SP_VFT_TBL_SIZE;
119 	wx->mac.rx_pb_size = NGBE_RX_PB_SIZE;
120 	wx->mac.tx_pb_size = NGBE_TDB_PB_SZ;
121 
122 	/* PCI config space info */
123 	err = wx_sw_init(wx);
124 	if (err < 0) {
125 		wx_err(wx, "read of internal subsystem device id failed\n");
126 		return err;
127 	}
128 
129 	/* mac type, phy type , oem type */
130 	ngbe_init_type_code(wx);
131 
132 	/* Set common capability flags and settings */
133 	wx->max_q_vectors = NGBE_MAX_MSIX_VECTORS;
134 	err = wx_get_pcie_msix_counts(wx, &msix_count, NGBE_MAX_MSIX_VECTORS);
135 	if (err)
136 		dev_err(&pdev->dev, "Do not support MSI-X\n");
137 	wx->mac.max_msix_vectors = msix_count;
138 
139 	if (ngbe_init_rss_key(wx))
140 		return -ENOMEM;
141 
142 	/* enable itr by default in dynamic mode */
143 	wx->rx_itr_setting = 1;
144 	wx->tx_itr_setting = 1;
145 
146 	/* set default ring sizes */
147 	wx->tx_ring_count = NGBE_DEFAULT_TXD;
148 	wx->rx_ring_count = NGBE_DEFAULT_RXD;
149 
150 	/* set default work limits */
151 	wx->tx_work_limit = NGBE_DEFAULT_TX_WORK;
152 	wx->rx_work_limit = NGBE_DEFAULT_RX_WORK;
153 
154 	return 0;
155 }
156 
157 /**
158  * ngbe_irq_enable - Enable default interrupt generation settings
159  * @wx: board private structure
160  * @queues: enable all queues interrupts
161  **/
162 static void ngbe_irq_enable(struct wx *wx, bool queues)
163 {
164 	u32 mask;
165 
166 	/* enable misc interrupt */
167 	mask = NGBE_PX_MISC_IEN_MASK;
168 
169 	wr32(wx, WX_GPIO_DDR, WX_GPIO_DDR_0);
170 	wr32(wx, WX_GPIO_INTEN, WX_GPIO_INTEN_0 | WX_GPIO_INTEN_1);
171 	wr32(wx, WX_GPIO_INTTYPE_LEVEL, 0x0);
172 	wr32(wx, WX_GPIO_POLARITY, wx->gpio_ctrl ? 0 : 0x3);
173 
174 	wr32(wx, WX_PX_MISC_IEN, mask);
175 
176 	/* mask interrupt */
177 	if (queues)
178 		wx_intr_enable(wx, NGBE_INTR_ALL);
179 	else
180 		wx_intr_enable(wx, NGBE_INTR_MISC(wx));
181 }
182 
183 /**
184  * ngbe_intr - msi/legacy mode Interrupt Handler
185  * @irq: interrupt number
186  * @data: pointer to a network interface device structure
187  **/
188 static irqreturn_t ngbe_intr(int __always_unused irq, void *data)
189 {
190 	struct wx_q_vector *q_vector;
191 	struct wx *wx  = data;
192 	struct pci_dev *pdev;
193 	u32 eicr;
194 
195 	q_vector = wx->q_vector[0];
196 	pdev = wx->pdev;
197 
198 	eicr = wx_misc_isb(wx, WX_ISB_VEC0);
199 	if (!eicr) {
200 		/* shared interrupt alert!
201 		 * the interrupt that we masked before the EICR read.
202 		 */
203 		if (netif_running(wx->netdev))
204 			ngbe_irq_enable(wx, true);
205 		return IRQ_NONE;        /* Not our interrupt */
206 	}
207 	wx->isb_mem[WX_ISB_VEC0] = 0;
208 	if (!(pdev->msi_enabled))
209 		wr32(wx, WX_PX_INTA, 1);
210 
211 	wx->isb_mem[WX_ISB_MISC] = 0;
212 	/* would disable interrupts here but it is auto disabled */
213 	napi_schedule_irqoff(&q_vector->napi);
214 
215 	if (netif_running(wx->netdev))
216 		ngbe_irq_enable(wx, false);
217 
218 	return IRQ_HANDLED;
219 }
220 
221 static irqreturn_t ngbe_msix_other(int __always_unused irq, void *data)
222 {
223 	struct wx *wx = data;
224 
225 	/* re-enable the original interrupt state, no lsc, no queues */
226 	if (netif_running(wx->netdev))
227 		ngbe_irq_enable(wx, false);
228 
229 	return IRQ_HANDLED;
230 }
231 
232 /**
233  * ngbe_request_msix_irqs - Initialize MSI-X interrupts
234  * @wx: board private structure
235  *
236  * ngbe_request_msix_irqs allocates MSI-X vectors and requests
237  * interrupts from the kernel.
238  **/
239 static int ngbe_request_msix_irqs(struct wx *wx)
240 {
241 	struct net_device *netdev = wx->netdev;
242 	int vector, err;
243 
244 	for (vector = 0; vector < wx->num_q_vectors; vector++) {
245 		struct wx_q_vector *q_vector = wx->q_vector[vector];
246 		struct msix_entry *entry = &wx->msix_entries[vector];
247 
248 		if (q_vector->tx.ring && q_vector->rx.ring)
249 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
250 				 "%s-TxRx-%d", netdev->name, entry->entry);
251 		else
252 			/* skip this unused q_vector */
253 			continue;
254 
255 		err = request_irq(entry->vector, wx_msix_clean_rings, 0,
256 				  q_vector->name, q_vector);
257 		if (err) {
258 			wx_err(wx, "request_irq failed for MSIX interrupt %s Error: %d\n",
259 			       q_vector->name, err);
260 			goto free_queue_irqs;
261 		}
262 	}
263 
264 	err = request_irq(wx->msix_entries[vector].vector,
265 			  ngbe_msix_other, 0, netdev->name, wx);
266 
267 	if (err) {
268 		wx_err(wx, "request_irq for msix_other failed: %d\n", err);
269 		goto free_queue_irqs;
270 	}
271 
272 	return 0;
273 
274 free_queue_irqs:
275 	while (vector) {
276 		vector--;
277 		free_irq(wx->msix_entries[vector].vector,
278 			 wx->q_vector[vector]);
279 	}
280 	wx_reset_interrupt_capability(wx);
281 	return err;
282 }
283 
284 /**
285  * ngbe_request_irq - initialize interrupts
286  * @wx: board private structure
287  *
288  * Attempts to configure interrupts using the best available
289  * capabilities of the hardware and kernel.
290  **/
291 static int ngbe_request_irq(struct wx *wx)
292 {
293 	struct net_device *netdev = wx->netdev;
294 	struct pci_dev *pdev = wx->pdev;
295 	int err;
296 
297 	if (pdev->msix_enabled)
298 		err = ngbe_request_msix_irqs(wx);
299 	else if (pdev->msi_enabled)
300 		err = request_irq(pdev->irq, ngbe_intr, 0,
301 				  netdev->name, wx);
302 	else
303 		err = request_irq(pdev->irq, ngbe_intr, IRQF_SHARED,
304 				  netdev->name, wx);
305 
306 	if (err)
307 		wx_err(wx, "request_irq failed, Error %d\n", err);
308 
309 	return err;
310 }
311 
312 static void ngbe_disable_device(struct wx *wx)
313 {
314 	struct net_device *netdev = wx->netdev;
315 	u32 i;
316 
317 	/* disable all enabled rx queues */
318 	for (i = 0; i < wx->num_rx_queues; i++)
319 		/* this call also flushes the previous write */
320 		wx_disable_rx_queue(wx, wx->rx_ring[i]);
321 	/* disable receives */
322 	wx_disable_rx(wx);
323 	wx_napi_disable_all(wx);
324 	netif_tx_stop_all_queues(netdev);
325 	netif_tx_disable(netdev);
326 	if (wx->gpio_ctrl)
327 		ngbe_sfp_modules_txrx_powerctl(wx, false);
328 	wx_irq_disable(wx);
329 	/* disable transmits in the hardware now that interrupts are off */
330 	for (i = 0; i < wx->num_tx_queues; i++) {
331 		u8 reg_idx = wx->tx_ring[i]->reg_idx;
332 
333 		wr32(wx, WX_PX_TR_CFG(reg_idx), WX_PX_TR_CFG_SWFLSH);
334 	}
335 }
336 
337 static void ngbe_down(struct wx *wx)
338 {
339 	phy_stop(wx->phydev);
340 	ngbe_disable_device(wx);
341 	wx_clean_all_tx_rings(wx);
342 	wx_clean_all_rx_rings(wx);
343 }
344 
345 static void ngbe_up(struct wx *wx)
346 {
347 	wx_configure_vectors(wx);
348 
349 	/* make sure to complete pre-operations */
350 	smp_mb__before_atomic();
351 	wx_napi_enable_all(wx);
352 	/* enable transmits */
353 	netif_tx_start_all_queues(wx->netdev);
354 
355 	/* clear any pending interrupts, may auto mask */
356 	rd32(wx, WX_PX_IC(0));
357 	rd32(wx, WX_PX_MISC_IC);
358 	ngbe_irq_enable(wx, true);
359 	if (wx->gpio_ctrl)
360 		ngbe_sfp_modules_txrx_powerctl(wx, true);
361 
362 	phy_start(wx->phydev);
363 }
364 
365 /**
366  * ngbe_open - Called when a network interface is made active
367  * @netdev: network interface device structure
368  *
369  * Returns 0 on success, negative value on failure
370  *
371  * The open entry point is called when a network interface is made
372  * active by the system (IFF_UP).
373  **/
374 static int ngbe_open(struct net_device *netdev)
375 {
376 	struct wx *wx = netdev_priv(netdev);
377 	int err;
378 
379 	wx_control_hw(wx, true);
380 
381 	err = wx_setup_resources(wx);
382 	if (err)
383 		return err;
384 
385 	wx_configure(wx);
386 
387 	err = ngbe_request_irq(wx);
388 	if (err)
389 		goto err_free_resources;
390 
391 	err = ngbe_phy_connect(wx);
392 	if (err)
393 		goto err_free_irq;
394 
395 	err = netif_set_real_num_tx_queues(netdev, wx->num_tx_queues);
396 	if (err)
397 		goto err_dis_phy;
398 
399 	err = netif_set_real_num_rx_queues(netdev, wx->num_rx_queues);
400 	if (err)
401 		goto err_dis_phy;
402 
403 	ngbe_up(wx);
404 
405 	return 0;
406 err_dis_phy:
407 	phy_disconnect(wx->phydev);
408 err_free_irq:
409 	wx_free_irq(wx);
410 err_free_resources:
411 	wx_free_resources(wx);
412 	return err;
413 }
414 
415 /**
416  * ngbe_close - Disables a network interface
417  * @netdev: network interface device structure
418  *
419  * Returns 0, this is not allowed to fail
420  *
421  * The close entry point is called when an interface is de-activated
422  * by the OS.  The hardware is still under the drivers control, but
423  * needs to be disabled.  A global MAC reset is issued to stop the
424  * hardware, and all transmit and receive resources are freed.
425  **/
426 static int ngbe_close(struct net_device *netdev)
427 {
428 	struct wx *wx = netdev_priv(netdev);
429 
430 	ngbe_down(wx);
431 	wx_free_irq(wx);
432 	wx_free_resources(wx);
433 	phy_disconnect(wx->phydev);
434 	wx_control_hw(wx, false);
435 
436 	return 0;
437 }
438 
439 static void ngbe_dev_shutdown(struct pci_dev *pdev, bool *enable_wake)
440 {
441 	struct wx *wx = pci_get_drvdata(pdev);
442 	struct net_device *netdev;
443 	u32 wufc = wx->wol;
444 
445 	netdev = wx->netdev;
446 	rtnl_lock();
447 	netif_device_detach(netdev);
448 
449 	if (netif_running(netdev))
450 		ngbe_close(netdev);
451 	wx_clear_interrupt_scheme(wx);
452 	rtnl_unlock();
453 
454 	if (wufc) {
455 		wx_set_rx_mode(netdev);
456 		wx_configure_rx(wx);
457 		wr32(wx, NGBE_PSR_WKUP_CTL, wufc);
458 	} else {
459 		wr32(wx, NGBE_PSR_WKUP_CTL, 0);
460 	}
461 	pci_wake_from_d3(pdev, !!wufc);
462 	*enable_wake = !!wufc;
463 	wx_control_hw(wx, false);
464 
465 	pci_disable_device(pdev);
466 }
467 
468 static void ngbe_shutdown(struct pci_dev *pdev)
469 {
470 	struct wx *wx = pci_get_drvdata(pdev);
471 	bool wake;
472 
473 	wake = !!wx->wol;
474 
475 	ngbe_dev_shutdown(pdev, &wake);
476 
477 	if (system_state == SYSTEM_POWER_OFF) {
478 		pci_wake_from_d3(pdev, wake);
479 		pci_set_power_state(pdev, PCI_D3hot);
480 	}
481 }
482 
483 static const struct net_device_ops ngbe_netdev_ops = {
484 	.ndo_open               = ngbe_open,
485 	.ndo_stop               = ngbe_close,
486 	.ndo_change_mtu         = wx_change_mtu,
487 	.ndo_start_xmit         = wx_xmit_frame,
488 	.ndo_set_rx_mode        = wx_set_rx_mode,
489 	.ndo_set_features       = wx_set_features,
490 	.ndo_validate_addr      = eth_validate_addr,
491 	.ndo_set_mac_address    = wx_set_mac,
492 	.ndo_get_stats64        = wx_get_stats64,
493 	.ndo_vlan_rx_add_vid    = wx_vlan_rx_add_vid,
494 	.ndo_vlan_rx_kill_vid   = wx_vlan_rx_kill_vid,
495 };
496 
497 /**
498  * ngbe_probe - Device Initialization Routine
499  * @pdev: PCI device information struct
500  * @ent: entry in ngbe_pci_tbl
501  *
502  * Returns 0 on success, negative on failure
503  *
504  * ngbe_probe initializes an wx identified by a pci_dev structure.
505  * The OS initialization, configuring of the wx private structure,
506  * and a hardware reset occur.
507  **/
508 static int ngbe_probe(struct pci_dev *pdev,
509 		      const struct pci_device_id __always_unused *ent)
510 {
511 	struct net_device *netdev;
512 	u32 e2rom_cksum_cap = 0;
513 	struct wx *wx = NULL;
514 	static int func_nums;
515 	u16 e2rom_ver = 0;
516 	u32 etrack_id = 0;
517 	u32 saved_ver = 0;
518 	int err;
519 
520 	err = pci_enable_device_mem(pdev);
521 	if (err)
522 		return err;
523 
524 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
525 	if (err) {
526 		dev_err(&pdev->dev,
527 			"No usable DMA configuration, aborting\n");
528 		goto err_pci_disable_dev;
529 	}
530 
531 	err = pci_request_selected_regions(pdev,
532 					   pci_select_bars(pdev, IORESOURCE_MEM),
533 					   ngbe_driver_name);
534 	if (err) {
535 		dev_err(&pdev->dev,
536 			"pci_request_selected_regions failed %d\n", err);
537 		goto err_pci_disable_dev;
538 	}
539 
540 	pci_set_master(pdev);
541 
542 	netdev = devm_alloc_etherdev_mqs(&pdev->dev,
543 					 sizeof(struct wx),
544 					 NGBE_MAX_TX_QUEUES,
545 					 NGBE_MAX_RX_QUEUES);
546 	if (!netdev) {
547 		err = -ENOMEM;
548 		goto err_pci_release_regions;
549 	}
550 
551 	SET_NETDEV_DEV(netdev, &pdev->dev);
552 
553 	wx = netdev_priv(netdev);
554 	wx->netdev = netdev;
555 	wx->pdev = pdev;
556 	wx->msg_enable = BIT(3) - 1;
557 
558 	wx->hw_addr = devm_ioremap(&pdev->dev,
559 				   pci_resource_start(pdev, 0),
560 				   pci_resource_len(pdev, 0));
561 	if (!wx->hw_addr) {
562 		err = -EIO;
563 		goto err_pci_release_regions;
564 	}
565 
566 	wx->driver_name = ngbe_driver_name;
567 	ngbe_set_ethtool_ops(netdev);
568 	netdev->netdev_ops = &ngbe_netdev_ops;
569 
570 	netdev->features = NETIF_F_SG | NETIF_F_IP_CSUM |
571 			   NETIF_F_TSO | NETIF_F_TSO6 |
572 			   NETIF_F_RXHASH | NETIF_F_RXCSUM;
573 	netdev->features |= NETIF_F_SCTP_CRC | NETIF_F_TSO_MANGLEID;
574 	netdev->vlan_features |= netdev->features;
575 	netdev->features |= NETIF_F_IPV6_CSUM | NETIF_F_VLAN_FEATURES;
576 	/* copy netdev features into list of user selectable features */
577 	netdev->hw_features |= netdev->features | NETIF_F_RXALL;
578 	netdev->hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
579 	netdev->features |= NETIF_F_HIGHDMA;
580 	netdev->hw_features |= NETIF_F_GRO;
581 	netdev->features |= NETIF_F_GRO;
582 
583 	netdev->priv_flags |= IFF_UNICAST_FLT;
584 	netdev->priv_flags |= IFF_SUPP_NOFCS;
585 
586 	netdev->min_mtu = ETH_MIN_MTU;
587 	netdev->max_mtu = WX_MAX_JUMBO_FRAME_SIZE -
588 			  (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
589 
590 	wx->bd_number = func_nums;
591 	/* setup the private structure */
592 	err = ngbe_sw_init(wx);
593 	if (err)
594 		goto err_free_mac_table;
595 
596 	/* check if flash load is done after hw power up */
597 	err = wx_check_flash_load(wx, NGBE_SPI_ILDR_STATUS_PERST);
598 	if (err)
599 		goto err_free_mac_table;
600 	err = wx_check_flash_load(wx, NGBE_SPI_ILDR_STATUS_PWRRST);
601 	if (err)
602 		goto err_free_mac_table;
603 
604 	err = wx_mng_present(wx);
605 	if (err) {
606 		dev_err(&pdev->dev, "Management capability is not present\n");
607 		goto err_free_mac_table;
608 	}
609 
610 	err = ngbe_reset_hw(wx);
611 	if (err) {
612 		dev_err(&pdev->dev, "HW Init failed: %d\n", err);
613 		goto err_free_mac_table;
614 	}
615 
616 	if (wx->bus.func == 0) {
617 		wr32(wx, NGBE_CALSUM_CAP_STATUS, 0x0);
618 		wr32(wx, NGBE_EEPROM_VERSION_STORE_REG, 0x0);
619 	} else {
620 		e2rom_cksum_cap = rd32(wx, NGBE_CALSUM_CAP_STATUS);
621 		saved_ver = rd32(wx, NGBE_EEPROM_VERSION_STORE_REG);
622 	}
623 
624 	wx_init_eeprom_params(wx);
625 	if (wx->bus.func == 0 || e2rom_cksum_cap == 0) {
626 		/* make sure the EEPROM is ready */
627 		err = ngbe_eeprom_chksum_hostif(wx);
628 		if (err) {
629 			dev_err(&pdev->dev, "The EEPROM Checksum Is Not Valid\n");
630 			err = -EIO;
631 			goto err_free_mac_table;
632 		}
633 	}
634 
635 	wx->wol = 0;
636 	if (wx->wol_hw_supported)
637 		wx->wol = NGBE_PSR_WKUP_CTL_MAG;
638 
639 	netdev->wol_enabled = !!(wx->wol);
640 	wr32(wx, NGBE_PSR_WKUP_CTL, wx->wol);
641 	device_set_wakeup_enable(&pdev->dev, wx->wol);
642 
643 	/* Save off EEPROM version number and Option Rom version which
644 	 * together make a unique identify for the eeprom
645 	 */
646 	if (saved_ver) {
647 		etrack_id = saved_ver;
648 	} else {
649 		wx_read_ee_hostif(wx,
650 				  wx->eeprom.sw_region_offset + NGBE_EEPROM_VERSION_H,
651 				  &e2rom_ver);
652 		etrack_id = e2rom_ver << 16;
653 		wx_read_ee_hostif(wx,
654 				  wx->eeprom.sw_region_offset + NGBE_EEPROM_VERSION_L,
655 				  &e2rom_ver);
656 		etrack_id |= e2rom_ver;
657 		wr32(wx, NGBE_EEPROM_VERSION_STORE_REG, etrack_id);
658 	}
659 	snprintf(wx->eeprom_id, sizeof(wx->eeprom_id),
660 		 "0x%08x", etrack_id);
661 
662 	eth_hw_addr_set(netdev, wx->mac.perm_addr);
663 	wx_mac_set_default_filter(wx, wx->mac.perm_addr);
664 
665 	err = wx_init_interrupt_scheme(wx);
666 	if (err)
667 		goto err_free_mac_table;
668 
669 	/* phy Interface Configuration */
670 	err = ngbe_mdio_init(wx);
671 	if (err)
672 		goto err_clear_interrupt_scheme;
673 
674 	err = register_netdev(netdev);
675 	if (err)
676 		goto err_register;
677 
678 	pci_set_drvdata(pdev, wx);
679 
680 	netif_info(wx, probe, netdev,
681 		   "PHY: %s, PBA No: Wang Xun GbE Family Controller\n",
682 		   wx->mac_type == em_mac_type_mdi ? "Internal" : "External");
683 	netif_info(wx, probe, netdev, "%pM\n", netdev->dev_addr);
684 
685 	return 0;
686 
687 err_register:
688 	wx_control_hw(wx, false);
689 err_clear_interrupt_scheme:
690 	wx_clear_interrupt_scheme(wx);
691 err_free_mac_table:
692 	kfree(wx->mac_table);
693 err_pci_release_regions:
694 	pci_release_selected_regions(pdev,
695 				     pci_select_bars(pdev, IORESOURCE_MEM));
696 err_pci_disable_dev:
697 	pci_disable_device(pdev);
698 	return err;
699 }
700 
701 /**
702  * ngbe_remove - Device Removal Routine
703  * @pdev: PCI device information struct
704  *
705  * ngbe_remove is called by the PCI subsystem to alert the driver
706  * that it should release a PCI device.  The could be caused by a
707  * Hot-Plug event, or because the driver is going to be removed from
708  * memory.
709  **/
710 static void ngbe_remove(struct pci_dev *pdev)
711 {
712 	struct wx *wx = pci_get_drvdata(pdev);
713 	struct net_device *netdev;
714 
715 	netdev = wx->netdev;
716 	unregister_netdev(netdev);
717 	pci_release_selected_regions(pdev,
718 				     pci_select_bars(pdev, IORESOURCE_MEM));
719 
720 	kfree(wx->mac_table);
721 	wx_clear_interrupt_scheme(wx);
722 
723 	pci_disable_device(pdev);
724 }
725 
726 static int ngbe_suspend(struct pci_dev *pdev, pm_message_t state)
727 {
728 	bool wake;
729 
730 	ngbe_dev_shutdown(pdev, &wake);
731 	device_set_wakeup_enable(&pdev->dev, wake);
732 
733 	return 0;
734 }
735 
736 static int ngbe_resume(struct pci_dev *pdev)
737 {
738 	struct net_device *netdev;
739 	struct wx *wx;
740 	u32 err;
741 
742 	wx = pci_get_drvdata(pdev);
743 	netdev = wx->netdev;
744 
745 	err = pci_enable_device_mem(pdev);
746 	if (err) {
747 		wx_err(wx, "Cannot enable PCI device from suspend\n");
748 		return err;
749 	}
750 	pci_set_master(pdev);
751 	device_wakeup_disable(&pdev->dev);
752 
753 	ngbe_reset_hw(wx);
754 	rtnl_lock();
755 	err = wx_init_interrupt_scheme(wx);
756 	if (!err && netif_running(netdev))
757 		err = ngbe_open(netdev);
758 	if (!err)
759 		netif_device_attach(netdev);
760 	rtnl_unlock();
761 
762 	return 0;
763 }
764 
765 static struct pci_driver ngbe_driver = {
766 	.name     = ngbe_driver_name,
767 	.id_table = ngbe_pci_tbl,
768 	.probe    = ngbe_probe,
769 	.remove   = ngbe_remove,
770 	.suspend  = ngbe_suspend,
771 	.resume   = ngbe_resume,
772 	.shutdown = ngbe_shutdown,
773 };
774 
775 module_pci_driver(ngbe_driver);
776 
777 MODULE_DEVICE_TABLE(pci, ngbe_pci_tbl);
778 MODULE_AUTHOR("Beijing WangXun Technology Co., Ltd, <software@net-swift.com>");
779 MODULE_DESCRIPTION("WangXun(R) Gigabit PCI Express Network Driver");
780 MODULE_LICENSE("GPL");
781