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/aer.h>
10 #include <linux/etherdevice.h>
11
12 #include "ngbe.h"
13 char ngbe_driver_name[] = "ngbe";
14
15 /* ngbe_pci_tbl - PCI Device ID Table
16 *
17 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
18 * Class, Class Mask, private data (not used) }
19 */
20 static const struct pci_device_id ngbe_pci_tbl[] = {
21 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860AL_W), 0},
22 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860A2), 0},
23 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860A2S), 0},
24 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860A4), 0},
25 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860A4S), 0},
26 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860AL2), 0},
27 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860AL2S), 0},
28 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860AL4), 0},
29 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860AL4S), 0},
30 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860LC), 0},
31 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860A1), 0},
32 { PCI_VDEVICE(WANGXUN, NGBE_DEV_ID_EM_WX1860A1L), 0},
33 /* required last entry */
34 { .device = 0 }
35 };
36
ngbe_dev_shutdown(struct pci_dev * pdev,bool * enable_wake)37 static void ngbe_dev_shutdown(struct pci_dev *pdev, bool *enable_wake)
38 {
39 struct ngbe_adapter *adapter = pci_get_drvdata(pdev);
40 struct net_device *netdev = adapter->netdev;
41
42 netif_device_detach(netdev);
43
44 pci_disable_device(pdev);
45 }
46
ngbe_shutdown(struct pci_dev * pdev)47 static void ngbe_shutdown(struct pci_dev *pdev)
48 {
49 bool wake;
50
51 ngbe_dev_shutdown(pdev, &wake);
52
53 if (system_state == SYSTEM_POWER_OFF) {
54 pci_wake_from_d3(pdev, wake);
55 pci_set_power_state(pdev, PCI_D3hot);
56 }
57 }
58
59 /**
60 * ngbe_probe - Device Initialization Routine
61 * @pdev: PCI device information struct
62 * @ent: entry in ngbe_pci_tbl
63 *
64 * Returns 0 on success, negative on failure
65 *
66 * ngbe_probe initializes an adapter identified by a pci_dev structure.
67 * The OS initialization, configuring of the adapter private structure,
68 * and a hardware reset occur.
69 **/
ngbe_probe(struct pci_dev * pdev,const struct pci_device_id __always_unused * ent)70 static int ngbe_probe(struct pci_dev *pdev,
71 const struct pci_device_id __always_unused *ent)
72 {
73 struct ngbe_adapter *adapter = NULL;
74 struct net_device *netdev;
75 int err;
76
77 err = pci_enable_device_mem(pdev);
78 if (err)
79 return err;
80
81 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
82 if (err) {
83 dev_err(&pdev->dev,
84 "No usable DMA configuration, aborting\n");
85 goto err_pci_disable_dev;
86 }
87
88 err = pci_request_selected_regions(pdev,
89 pci_select_bars(pdev, IORESOURCE_MEM),
90 ngbe_driver_name);
91 if (err) {
92 dev_err(&pdev->dev,
93 "pci_request_selected_regions failed %d\n", err);
94 goto err_pci_disable_dev;
95 }
96
97 pci_enable_pcie_error_reporting(pdev);
98 pci_set_master(pdev);
99
100 netdev = devm_alloc_etherdev_mqs(&pdev->dev,
101 sizeof(struct ngbe_adapter),
102 NGBE_MAX_TX_QUEUES,
103 NGBE_MAX_RX_QUEUES);
104 if (!netdev) {
105 err = -ENOMEM;
106 goto err_pci_release_regions;
107 }
108
109 SET_NETDEV_DEV(netdev, &pdev->dev);
110
111 adapter = netdev_priv(netdev);
112 adapter->netdev = netdev;
113 adapter->pdev = pdev;
114
115 adapter->io_addr = devm_ioremap(&pdev->dev,
116 pci_resource_start(pdev, 0),
117 pci_resource_len(pdev, 0));
118 if (!adapter->io_addr) {
119 err = -EIO;
120 goto err_pci_release_regions;
121 }
122
123 netdev->features |= NETIF_F_HIGHDMA;
124
125 pci_set_drvdata(pdev, adapter);
126
127 return 0;
128
129 err_pci_release_regions:
130 pci_disable_pcie_error_reporting(pdev);
131 pci_release_selected_regions(pdev,
132 pci_select_bars(pdev, IORESOURCE_MEM));
133 err_pci_disable_dev:
134 pci_disable_device(pdev);
135 return err;
136 }
137
138 /**
139 * ngbe_remove - Device Removal Routine
140 * @pdev: PCI device information struct
141 *
142 * ngbe_remove is called by the PCI subsystem to alert the driver
143 * that it should release a PCI device. The could be caused by a
144 * Hot-Plug event, or because the driver is going to be removed from
145 * memory.
146 **/
ngbe_remove(struct pci_dev * pdev)147 static void ngbe_remove(struct pci_dev *pdev)
148 {
149 pci_release_selected_regions(pdev,
150 pci_select_bars(pdev, IORESOURCE_MEM));
151
152 pci_disable_pcie_error_reporting(pdev);
153
154 pci_disable_device(pdev);
155 }
156
157 static struct pci_driver ngbe_driver = {
158 .name = ngbe_driver_name,
159 .id_table = ngbe_pci_tbl,
160 .probe = ngbe_probe,
161 .remove = ngbe_remove,
162 .shutdown = ngbe_shutdown,
163 };
164
165 module_pci_driver(ngbe_driver);
166
167 MODULE_DEVICE_TABLE(pci, ngbe_pci_tbl);
168 MODULE_AUTHOR("Beijing WangXun Technology Co., Ltd, <software@net-swift.com>");
169 MODULE_DESCRIPTION("WangXun(R) Gigabit PCI Express Network Driver");
170 MODULE_LICENSE("GPL");
171