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_enabled = (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 
444 	netdev = wx->netdev;
445 	netif_device_detach(netdev);
446 
447 	rtnl_lock();
448 	if (netif_running(netdev))
449 		ngbe_down(wx);
450 	rtnl_unlock();
451 	wx_control_hw(wx, false);
452 
453 	pci_disable_device(pdev);
454 }
455 
456 static void ngbe_shutdown(struct pci_dev *pdev)
457 {
458 	struct wx *wx = pci_get_drvdata(pdev);
459 	bool wake;
460 
461 	wake = !!wx->wol;
462 
463 	ngbe_dev_shutdown(pdev, &wake);
464 
465 	if (system_state == SYSTEM_POWER_OFF) {
466 		pci_wake_from_d3(pdev, wake);
467 		pci_set_power_state(pdev, PCI_D3hot);
468 	}
469 }
470 
471 static const struct net_device_ops ngbe_netdev_ops = {
472 	.ndo_open               = ngbe_open,
473 	.ndo_stop               = ngbe_close,
474 	.ndo_change_mtu         = wx_change_mtu,
475 	.ndo_start_xmit         = wx_xmit_frame,
476 	.ndo_set_rx_mode        = wx_set_rx_mode,
477 	.ndo_set_features       = wx_set_features,
478 	.ndo_validate_addr      = eth_validate_addr,
479 	.ndo_set_mac_address    = wx_set_mac,
480 	.ndo_get_stats64        = wx_get_stats64,
481 	.ndo_vlan_rx_add_vid    = wx_vlan_rx_add_vid,
482 	.ndo_vlan_rx_kill_vid   = wx_vlan_rx_kill_vid,
483 };
484 
485 /**
486  * ngbe_probe - Device Initialization Routine
487  * @pdev: PCI device information struct
488  * @ent: entry in ngbe_pci_tbl
489  *
490  * Returns 0 on success, negative on failure
491  *
492  * ngbe_probe initializes an wx identified by a pci_dev structure.
493  * The OS initialization, configuring of the wx private structure,
494  * and a hardware reset occur.
495  **/
496 static int ngbe_probe(struct pci_dev *pdev,
497 		      const struct pci_device_id __always_unused *ent)
498 {
499 	struct net_device *netdev;
500 	u32 e2rom_cksum_cap = 0;
501 	struct wx *wx = NULL;
502 	static int func_nums;
503 	u16 e2rom_ver = 0;
504 	u32 etrack_id = 0;
505 	u32 saved_ver = 0;
506 	int err;
507 
508 	err = pci_enable_device_mem(pdev);
509 	if (err)
510 		return err;
511 
512 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
513 	if (err) {
514 		dev_err(&pdev->dev,
515 			"No usable DMA configuration, aborting\n");
516 		goto err_pci_disable_dev;
517 	}
518 
519 	err = pci_request_selected_regions(pdev,
520 					   pci_select_bars(pdev, IORESOURCE_MEM),
521 					   ngbe_driver_name);
522 	if (err) {
523 		dev_err(&pdev->dev,
524 			"pci_request_selected_regions failed %d\n", err);
525 		goto err_pci_disable_dev;
526 	}
527 
528 	pci_set_master(pdev);
529 
530 	netdev = devm_alloc_etherdev_mqs(&pdev->dev,
531 					 sizeof(struct wx),
532 					 NGBE_MAX_TX_QUEUES,
533 					 NGBE_MAX_RX_QUEUES);
534 	if (!netdev) {
535 		err = -ENOMEM;
536 		goto err_pci_release_regions;
537 	}
538 
539 	SET_NETDEV_DEV(netdev, &pdev->dev);
540 
541 	wx = netdev_priv(netdev);
542 	wx->netdev = netdev;
543 	wx->pdev = pdev;
544 	wx->msg_enable = BIT(3) - 1;
545 
546 	wx->hw_addr = devm_ioremap(&pdev->dev,
547 				   pci_resource_start(pdev, 0),
548 				   pci_resource_len(pdev, 0));
549 	if (!wx->hw_addr) {
550 		err = -EIO;
551 		goto err_pci_release_regions;
552 	}
553 
554 	wx->driver_name = ngbe_driver_name;
555 	ngbe_set_ethtool_ops(netdev);
556 	netdev->netdev_ops = &ngbe_netdev_ops;
557 
558 	netdev->features = NETIF_F_SG | NETIF_F_IP_CSUM |
559 			   NETIF_F_TSO | NETIF_F_TSO6 |
560 			   NETIF_F_RXHASH | NETIF_F_RXCSUM;
561 	netdev->features |= NETIF_F_SCTP_CRC | NETIF_F_TSO_MANGLEID;
562 	netdev->vlan_features |= netdev->features;
563 	netdev->features |= NETIF_F_IPV6_CSUM | NETIF_F_VLAN_FEATURES;
564 	/* copy netdev features into list of user selectable features */
565 	netdev->hw_features |= netdev->features | NETIF_F_RXALL;
566 	netdev->hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
567 	netdev->features |= NETIF_F_HIGHDMA;
568 	netdev->hw_features |= NETIF_F_GRO;
569 	netdev->features |= NETIF_F_GRO;
570 
571 	netdev->priv_flags |= IFF_UNICAST_FLT;
572 	netdev->priv_flags |= IFF_SUPP_NOFCS;
573 
574 	netdev->min_mtu = ETH_MIN_MTU;
575 	netdev->max_mtu = WX_MAX_JUMBO_FRAME_SIZE -
576 			  (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
577 
578 	wx->bd_number = func_nums;
579 	/* setup the private structure */
580 	err = ngbe_sw_init(wx);
581 	if (err)
582 		goto err_free_mac_table;
583 
584 	/* check if flash load is done after hw power up */
585 	err = wx_check_flash_load(wx, NGBE_SPI_ILDR_STATUS_PERST);
586 	if (err)
587 		goto err_free_mac_table;
588 	err = wx_check_flash_load(wx, NGBE_SPI_ILDR_STATUS_PWRRST);
589 	if (err)
590 		goto err_free_mac_table;
591 
592 	err = wx_mng_present(wx);
593 	if (err) {
594 		dev_err(&pdev->dev, "Management capability is not present\n");
595 		goto err_free_mac_table;
596 	}
597 
598 	err = ngbe_reset_hw(wx);
599 	if (err) {
600 		dev_err(&pdev->dev, "HW Init failed: %d\n", err);
601 		goto err_free_mac_table;
602 	}
603 
604 	if (wx->bus.func == 0) {
605 		wr32(wx, NGBE_CALSUM_CAP_STATUS, 0x0);
606 		wr32(wx, NGBE_EEPROM_VERSION_STORE_REG, 0x0);
607 	} else {
608 		e2rom_cksum_cap = rd32(wx, NGBE_CALSUM_CAP_STATUS);
609 		saved_ver = rd32(wx, NGBE_EEPROM_VERSION_STORE_REG);
610 	}
611 
612 	wx_init_eeprom_params(wx);
613 	if (wx->bus.func == 0 || e2rom_cksum_cap == 0) {
614 		/* make sure the EEPROM is ready */
615 		err = ngbe_eeprom_chksum_hostif(wx);
616 		if (err) {
617 			dev_err(&pdev->dev, "The EEPROM Checksum Is Not Valid\n");
618 			err = -EIO;
619 			goto err_free_mac_table;
620 		}
621 	}
622 
623 	wx->wol = 0;
624 	if (wx->wol_enabled)
625 		wx->wol = NGBE_PSR_WKUP_CTL_MAG;
626 
627 	wx->wol_enabled = !!(wx->wol);
628 	wr32(wx, NGBE_PSR_WKUP_CTL, wx->wol);
629 
630 	device_set_wakeup_enable(&pdev->dev, wx->wol);
631 
632 	/* Save off EEPROM version number and Option Rom version which
633 	 * together make a unique identify for the eeprom
634 	 */
635 	if (saved_ver) {
636 		etrack_id = saved_ver;
637 	} else {
638 		wx_read_ee_hostif(wx,
639 				  wx->eeprom.sw_region_offset + NGBE_EEPROM_VERSION_H,
640 				  &e2rom_ver);
641 		etrack_id = e2rom_ver << 16;
642 		wx_read_ee_hostif(wx,
643 				  wx->eeprom.sw_region_offset + NGBE_EEPROM_VERSION_L,
644 				  &e2rom_ver);
645 		etrack_id |= e2rom_ver;
646 		wr32(wx, NGBE_EEPROM_VERSION_STORE_REG, etrack_id);
647 	}
648 	snprintf(wx->eeprom_id, sizeof(wx->eeprom_id),
649 		 "0x%08x", etrack_id);
650 
651 	eth_hw_addr_set(netdev, wx->mac.perm_addr);
652 	wx_mac_set_default_filter(wx, wx->mac.perm_addr);
653 
654 	err = wx_init_interrupt_scheme(wx);
655 	if (err)
656 		goto err_free_mac_table;
657 
658 	/* phy Interface Configuration */
659 	err = ngbe_mdio_init(wx);
660 	if (err)
661 		goto err_clear_interrupt_scheme;
662 
663 	err = register_netdev(netdev);
664 	if (err)
665 		goto err_register;
666 
667 	pci_set_drvdata(pdev, wx);
668 
669 	netif_info(wx, probe, netdev,
670 		   "PHY: %s, PBA No: Wang Xun GbE Family Controller\n",
671 		   wx->mac_type == em_mac_type_mdi ? "Internal" : "External");
672 	netif_info(wx, probe, netdev, "%pM\n", netdev->dev_addr);
673 
674 	return 0;
675 
676 err_register:
677 	wx_control_hw(wx, false);
678 err_clear_interrupt_scheme:
679 	wx_clear_interrupt_scheme(wx);
680 err_free_mac_table:
681 	kfree(wx->mac_table);
682 err_pci_release_regions:
683 	pci_release_selected_regions(pdev,
684 				     pci_select_bars(pdev, IORESOURCE_MEM));
685 err_pci_disable_dev:
686 	pci_disable_device(pdev);
687 	return err;
688 }
689 
690 /**
691  * ngbe_remove - Device Removal Routine
692  * @pdev: PCI device information struct
693  *
694  * ngbe_remove is called by the PCI subsystem to alert the driver
695  * that it should release a PCI device.  The could be caused by a
696  * Hot-Plug event, or because the driver is going to be removed from
697  * memory.
698  **/
699 static void ngbe_remove(struct pci_dev *pdev)
700 {
701 	struct wx *wx = pci_get_drvdata(pdev);
702 	struct net_device *netdev;
703 
704 	netdev = wx->netdev;
705 	unregister_netdev(netdev);
706 	pci_release_selected_regions(pdev,
707 				     pci_select_bars(pdev, IORESOURCE_MEM));
708 
709 	kfree(wx->mac_table);
710 	wx_clear_interrupt_scheme(wx);
711 
712 	pci_disable_device(pdev);
713 }
714 
715 static struct pci_driver ngbe_driver = {
716 	.name     = ngbe_driver_name,
717 	.id_table = ngbe_pci_tbl,
718 	.probe    = ngbe_probe,
719 	.remove   = ngbe_remove,
720 	.shutdown = ngbe_shutdown,
721 };
722 
723 module_pci_driver(ngbe_driver);
724 
725 MODULE_DEVICE_TABLE(pci, ngbe_pci_tbl);
726 MODULE_AUTHOR("Beijing WangXun Technology Co., Ltd, <software@net-swift.com>");
727 MODULE_DESCRIPTION("WangXun(R) Gigabit PCI Express Network Driver");
728 MODULE_LICENSE("GPL");
729