xref: /linux/drivers/net/ethernet/wangxun/ngbe/ngbe_main.c (revision 59fff63cc2b75dcfe08f9eeb4b2187d73e53843d)
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 	wx_update_stats(wx);
337 }
338 
339 static void ngbe_down(struct wx *wx)
340 {
341 	phy_stop(wx->phydev);
342 	ngbe_disable_device(wx);
343 	wx_clean_all_tx_rings(wx);
344 	wx_clean_all_rx_rings(wx);
345 }
346 
347 static void ngbe_up(struct wx *wx)
348 {
349 	wx_configure_vectors(wx);
350 
351 	/* make sure to complete pre-operations */
352 	smp_mb__before_atomic();
353 	wx_napi_enable_all(wx);
354 	/* enable transmits */
355 	netif_tx_start_all_queues(wx->netdev);
356 
357 	/* clear any pending interrupts, may auto mask */
358 	rd32(wx, WX_PX_IC(0));
359 	rd32(wx, WX_PX_MISC_IC);
360 	ngbe_irq_enable(wx, true);
361 	if (wx->gpio_ctrl)
362 		ngbe_sfp_modules_txrx_powerctl(wx, true);
363 
364 	phy_start(wx->phydev);
365 }
366 
367 /**
368  * ngbe_open - Called when a network interface is made active
369  * @netdev: network interface device structure
370  *
371  * Returns 0 on success, negative value on failure
372  *
373  * The open entry point is called when a network interface is made
374  * active by the system (IFF_UP).
375  **/
376 static int ngbe_open(struct net_device *netdev)
377 {
378 	struct wx *wx = netdev_priv(netdev);
379 	int err;
380 
381 	wx_control_hw(wx, true);
382 
383 	err = wx_setup_resources(wx);
384 	if (err)
385 		return err;
386 
387 	wx_configure(wx);
388 
389 	err = ngbe_request_irq(wx);
390 	if (err)
391 		goto err_free_resources;
392 
393 	err = ngbe_phy_connect(wx);
394 	if (err)
395 		goto err_free_irq;
396 
397 	err = netif_set_real_num_tx_queues(netdev, wx->num_tx_queues);
398 	if (err)
399 		goto err_dis_phy;
400 
401 	err = netif_set_real_num_rx_queues(netdev, wx->num_rx_queues);
402 	if (err)
403 		goto err_dis_phy;
404 
405 	ngbe_up(wx);
406 
407 	return 0;
408 err_dis_phy:
409 	phy_disconnect(wx->phydev);
410 err_free_irq:
411 	wx_free_irq(wx);
412 err_free_resources:
413 	wx_free_resources(wx);
414 	return err;
415 }
416 
417 /**
418  * ngbe_close - Disables a network interface
419  * @netdev: network interface device structure
420  *
421  * Returns 0, this is not allowed to fail
422  *
423  * The close entry point is called when an interface is de-activated
424  * by the OS.  The hardware is still under the drivers control, but
425  * needs to be disabled.  A global MAC reset is issued to stop the
426  * hardware, and all transmit and receive resources are freed.
427  **/
428 static int ngbe_close(struct net_device *netdev)
429 {
430 	struct wx *wx = netdev_priv(netdev);
431 
432 	ngbe_down(wx);
433 	wx_free_irq(wx);
434 	wx_free_resources(wx);
435 	phy_disconnect(wx->phydev);
436 	wx_control_hw(wx, false);
437 
438 	return 0;
439 }
440 
441 static void ngbe_dev_shutdown(struct pci_dev *pdev, bool *enable_wake)
442 {
443 	struct wx *wx = pci_get_drvdata(pdev);
444 	struct net_device *netdev;
445 	u32 wufc = wx->wol;
446 
447 	netdev = wx->netdev;
448 	rtnl_lock();
449 	netif_device_detach(netdev);
450 
451 	if (netif_running(netdev))
452 		ngbe_close(netdev);
453 	wx_clear_interrupt_scheme(wx);
454 	rtnl_unlock();
455 
456 	if (wufc) {
457 		wx_set_rx_mode(netdev);
458 		wx_configure_rx(wx);
459 		wr32(wx, NGBE_PSR_WKUP_CTL, wufc);
460 	} else {
461 		wr32(wx, NGBE_PSR_WKUP_CTL, 0);
462 	}
463 	pci_wake_from_d3(pdev, !!wufc);
464 	*enable_wake = !!wufc;
465 	wx_control_hw(wx, false);
466 
467 	pci_disable_device(pdev);
468 }
469 
470 static void ngbe_shutdown(struct pci_dev *pdev)
471 {
472 	struct wx *wx = pci_get_drvdata(pdev);
473 	bool wake;
474 
475 	wake = !!wx->wol;
476 
477 	ngbe_dev_shutdown(pdev, &wake);
478 
479 	if (system_state == SYSTEM_POWER_OFF) {
480 		pci_wake_from_d3(pdev, wake);
481 		pci_set_power_state(pdev, PCI_D3hot);
482 	}
483 }
484 
485 static const struct net_device_ops ngbe_netdev_ops = {
486 	.ndo_open               = ngbe_open,
487 	.ndo_stop               = ngbe_close,
488 	.ndo_change_mtu         = wx_change_mtu,
489 	.ndo_start_xmit         = wx_xmit_frame,
490 	.ndo_set_rx_mode        = wx_set_rx_mode,
491 	.ndo_set_features       = wx_set_features,
492 	.ndo_validate_addr      = eth_validate_addr,
493 	.ndo_set_mac_address    = wx_set_mac,
494 	.ndo_get_stats64        = wx_get_stats64,
495 	.ndo_vlan_rx_add_vid    = wx_vlan_rx_add_vid,
496 	.ndo_vlan_rx_kill_vid   = wx_vlan_rx_kill_vid,
497 };
498 
499 /**
500  * ngbe_probe - Device Initialization Routine
501  * @pdev: PCI device information struct
502  * @ent: entry in ngbe_pci_tbl
503  *
504  * Returns 0 on success, negative on failure
505  *
506  * ngbe_probe initializes an wx identified by a pci_dev structure.
507  * The OS initialization, configuring of the wx private structure,
508  * and a hardware reset occur.
509  **/
510 static int ngbe_probe(struct pci_dev *pdev,
511 		      const struct pci_device_id __always_unused *ent)
512 {
513 	struct net_device *netdev;
514 	u32 e2rom_cksum_cap = 0;
515 	struct wx *wx = NULL;
516 	static int func_nums;
517 	u16 e2rom_ver = 0;
518 	u32 etrack_id = 0;
519 	u32 saved_ver = 0;
520 	int err;
521 
522 	err = pci_enable_device_mem(pdev);
523 	if (err)
524 		return err;
525 
526 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
527 	if (err) {
528 		dev_err(&pdev->dev,
529 			"No usable DMA configuration, aborting\n");
530 		goto err_pci_disable_dev;
531 	}
532 
533 	err = pci_request_selected_regions(pdev,
534 					   pci_select_bars(pdev, IORESOURCE_MEM),
535 					   ngbe_driver_name);
536 	if (err) {
537 		dev_err(&pdev->dev,
538 			"pci_request_selected_regions failed %d\n", err);
539 		goto err_pci_disable_dev;
540 	}
541 
542 	pci_set_master(pdev);
543 
544 	netdev = devm_alloc_etherdev_mqs(&pdev->dev,
545 					 sizeof(struct wx),
546 					 NGBE_MAX_TX_QUEUES,
547 					 NGBE_MAX_RX_QUEUES);
548 	if (!netdev) {
549 		err = -ENOMEM;
550 		goto err_pci_release_regions;
551 	}
552 
553 	SET_NETDEV_DEV(netdev, &pdev->dev);
554 
555 	wx = netdev_priv(netdev);
556 	wx->netdev = netdev;
557 	wx->pdev = pdev;
558 	wx->msg_enable = BIT(3) - 1;
559 
560 	wx->hw_addr = devm_ioremap(&pdev->dev,
561 				   pci_resource_start(pdev, 0),
562 				   pci_resource_len(pdev, 0));
563 	if (!wx->hw_addr) {
564 		err = -EIO;
565 		goto err_pci_release_regions;
566 	}
567 
568 	wx->driver_name = ngbe_driver_name;
569 	ngbe_set_ethtool_ops(netdev);
570 	netdev->netdev_ops = &ngbe_netdev_ops;
571 
572 	netdev->features = NETIF_F_SG | NETIF_F_IP_CSUM |
573 			   NETIF_F_TSO | NETIF_F_TSO6 |
574 			   NETIF_F_RXHASH | NETIF_F_RXCSUM;
575 	netdev->features |= NETIF_F_SCTP_CRC | NETIF_F_TSO_MANGLEID;
576 	netdev->vlan_features |= netdev->features;
577 	netdev->features |= NETIF_F_IPV6_CSUM | NETIF_F_VLAN_FEATURES;
578 	/* copy netdev features into list of user selectable features */
579 	netdev->hw_features |= netdev->features | NETIF_F_RXALL;
580 	netdev->hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
581 	netdev->features |= NETIF_F_HIGHDMA;
582 	netdev->hw_features |= NETIF_F_GRO;
583 	netdev->features |= NETIF_F_GRO;
584 
585 	netdev->priv_flags |= IFF_UNICAST_FLT;
586 	netdev->priv_flags |= IFF_SUPP_NOFCS;
587 
588 	netdev->min_mtu = ETH_MIN_MTU;
589 	netdev->max_mtu = WX_MAX_JUMBO_FRAME_SIZE -
590 			  (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
591 
592 	wx->bd_number = func_nums;
593 	/* setup the private structure */
594 	err = ngbe_sw_init(wx);
595 	if (err)
596 		goto err_free_mac_table;
597 
598 	/* check if flash load is done after hw power up */
599 	err = wx_check_flash_load(wx, NGBE_SPI_ILDR_STATUS_PERST);
600 	if (err)
601 		goto err_free_mac_table;
602 	err = wx_check_flash_load(wx, NGBE_SPI_ILDR_STATUS_PWRRST);
603 	if (err)
604 		goto err_free_mac_table;
605 
606 	err = wx_mng_present(wx);
607 	if (err) {
608 		dev_err(&pdev->dev, "Management capability is not present\n");
609 		goto err_free_mac_table;
610 	}
611 
612 	err = ngbe_reset_hw(wx);
613 	if (err) {
614 		dev_err(&pdev->dev, "HW Init failed: %d\n", err);
615 		goto err_free_mac_table;
616 	}
617 
618 	if (wx->bus.func == 0) {
619 		wr32(wx, NGBE_CALSUM_CAP_STATUS, 0x0);
620 		wr32(wx, NGBE_EEPROM_VERSION_STORE_REG, 0x0);
621 	} else {
622 		e2rom_cksum_cap = rd32(wx, NGBE_CALSUM_CAP_STATUS);
623 		saved_ver = rd32(wx, NGBE_EEPROM_VERSION_STORE_REG);
624 	}
625 
626 	wx_init_eeprom_params(wx);
627 	if (wx->bus.func == 0 || e2rom_cksum_cap == 0) {
628 		/* make sure the EEPROM is ready */
629 		err = ngbe_eeprom_chksum_hostif(wx);
630 		if (err) {
631 			dev_err(&pdev->dev, "The EEPROM Checksum Is Not Valid\n");
632 			err = -EIO;
633 			goto err_free_mac_table;
634 		}
635 	}
636 
637 	wx->wol = 0;
638 	if (wx->wol_hw_supported)
639 		wx->wol = NGBE_PSR_WKUP_CTL_MAG;
640 
641 	netdev->wol_enabled = !!(wx->wol);
642 	wr32(wx, NGBE_PSR_WKUP_CTL, wx->wol);
643 	device_set_wakeup_enable(&pdev->dev, wx->wol);
644 
645 	/* Save off EEPROM version number and Option Rom version which
646 	 * together make a unique identify for the eeprom
647 	 */
648 	if (saved_ver) {
649 		etrack_id = saved_ver;
650 	} else {
651 		wx_read_ee_hostif(wx,
652 				  wx->eeprom.sw_region_offset + NGBE_EEPROM_VERSION_H,
653 				  &e2rom_ver);
654 		etrack_id = e2rom_ver << 16;
655 		wx_read_ee_hostif(wx,
656 				  wx->eeprom.sw_region_offset + NGBE_EEPROM_VERSION_L,
657 				  &e2rom_ver);
658 		etrack_id |= e2rom_ver;
659 		wr32(wx, NGBE_EEPROM_VERSION_STORE_REG, etrack_id);
660 	}
661 	snprintf(wx->eeprom_id, sizeof(wx->eeprom_id),
662 		 "0x%08x", etrack_id);
663 
664 	eth_hw_addr_set(netdev, wx->mac.perm_addr);
665 	wx_mac_set_default_filter(wx, wx->mac.perm_addr);
666 
667 	err = wx_init_interrupt_scheme(wx);
668 	if (err)
669 		goto err_free_mac_table;
670 
671 	/* phy Interface Configuration */
672 	err = ngbe_mdio_init(wx);
673 	if (err)
674 		goto err_clear_interrupt_scheme;
675 
676 	err = register_netdev(netdev);
677 	if (err)
678 		goto err_register;
679 
680 	pci_set_drvdata(pdev, wx);
681 
682 	return 0;
683 
684 err_register:
685 	wx_control_hw(wx, false);
686 err_clear_interrupt_scheme:
687 	wx_clear_interrupt_scheme(wx);
688 err_free_mac_table:
689 	kfree(wx->mac_table);
690 err_pci_release_regions:
691 	pci_release_selected_regions(pdev,
692 				     pci_select_bars(pdev, IORESOURCE_MEM));
693 err_pci_disable_dev:
694 	pci_disable_device(pdev);
695 	return err;
696 }
697 
698 /**
699  * ngbe_remove - Device Removal Routine
700  * @pdev: PCI device information struct
701  *
702  * ngbe_remove is called by the PCI subsystem to alert the driver
703  * that it should release a PCI device.  The could be caused by a
704  * Hot-Plug event, or because the driver is going to be removed from
705  * memory.
706  **/
707 static void ngbe_remove(struct pci_dev *pdev)
708 {
709 	struct wx *wx = pci_get_drvdata(pdev);
710 	struct net_device *netdev;
711 
712 	netdev = wx->netdev;
713 	unregister_netdev(netdev);
714 	pci_release_selected_regions(pdev,
715 				     pci_select_bars(pdev, IORESOURCE_MEM));
716 
717 	kfree(wx->mac_table);
718 	wx_clear_interrupt_scheme(wx);
719 
720 	pci_disable_device(pdev);
721 }
722 
723 static int ngbe_suspend(struct pci_dev *pdev, pm_message_t state)
724 {
725 	bool wake;
726 
727 	ngbe_dev_shutdown(pdev, &wake);
728 	device_set_wakeup_enable(&pdev->dev, wake);
729 
730 	return 0;
731 }
732 
733 static int ngbe_resume(struct pci_dev *pdev)
734 {
735 	struct net_device *netdev;
736 	struct wx *wx;
737 	u32 err;
738 
739 	wx = pci_get_drvdata(pdev);
740 	netdev = wx->netdev;
741 
742 	err = pci_enable_device_mem(pdev);
743 	if (err) {
744 		wx_err(wx, "Cannot enable PCI device from suspend\n");
745 		return err;
746 	}
747 	pci_set_master(pdev);
748 	device_wakeup_disable(&pdev->dev);
749 
750 	ngbe_reset_hw(wx);
751 	rtnl_lock();
752 	err = wx_init_interrupt_scheme(wx);
753 	if (!err && netif_running(netdev))
754 		err = ngbe_open(netdev);
755 	if (!err)
756 		netif_device_attach(netdev);
757 	rtnl_unlock();
758 
759 	return 0;
760 }
761 
762 static struct pci_driver ngbe_driver = {
763 	.name     = ngbe_driver_name,
764 	.id_table = ngbe_pci_tbl,
765 	.probe    = ngbe_probe,
766 	.remove   = ngbe_remove,
767 	.suspend  = ngbe_suspend,
768 	.resume   = ngbe_resume,
769 	.shutdown = ngbe_shutdown,
770 };
771 
772 module_pci_driver(ngbe_driver);
773 
774 MODULE_DEVICE_TABLE(pci, ngbe_pci_tbl);
775 MODULE_AUTHOR("Beijing WangXun Technology Co., Ltd, <software@net-swift.com>");
776 MODULE_DESCRIPTION("WangXun(R) Gigabit PCI Express Network Driver");
777 MODULE_LICENSE("GPL");
778