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