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