qede: Add basic Network driver
[linux-2.6-microblaze.git] / drivers / net / ethernet / qlogic / qede / qede_main.c
1 /* QLogic qede NIC Driver
2 * Copyright (c) 2015 QLogic Corporation
3 *
4 * This software is available under the terms of the GNU General Public License
5 * (GPL) Version 2, available from the file COPYING in the main directory of
6 * this source tree.
7 */
8
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 #include <linux/version.h>
12 #include <linux/device.h>
13 #include <linux/netdevice.h>
14 #include <linux/etherdevice.h>
15 #include <linux/skbuff.h>
16 #include <linux/errno.h>
17 #include <linux/list.h>
18 #include <linux/string.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/interrupt.h>
21 #include <asm/byteorder.h>
22 #include <asm/param.h>
23 #include <linux/io.h>
24 #include <linux/netdev_features.h>
25 #include <linux/udp.h>
26 #include <linux/tcp.h>
27 #include <net/vxlan.h>
28 #include <linux/ip.h>
29 #include <net/ipv6.h>
30 #include <net/tcp.h>
31 #include <linux/if_ether.h>
32 #include <linux/if_vlan.h>
33 #include <linux/pkt_sched.h>
34 #include <linux/ethtool.h>
35 #include <linux/in.h>
36 #include <linux/random.h>
37 #include <net/ip6_checksum.h>
38 #include <linux/bitops.h>
39
40 #include "qede.h"
41
42 static const char version[] = "QLogic QL4xxx 40G/100G Ethernet Driver qede "
43                               DRV_MODULE_VERSION "\n";
44
45 MODULE_DESCRIPTION("QLogic 40G/100G Ethernet Driver");
46 MODULE_LICENSE("GPL");
47 MODULE_VERSION(DRV_MODULE_VERSION);
48
49 static uint debug;
50 module_param(debug, uint, 0);
51 MODULE_PARM_DESC(debug, " Default debug msglevel");
52
53 static const struct qed_eth_ops *qed_ops;
54
55 #define CHIP_NUM_57980S_40              0x1634
56 #define CHIP_NUM_57980S_10              0x1635
57 #define CHIP_NUM_57980S_MF              0x1636
58 #define CHIP_NUM_57980S_100             0x1644
59 #define CHIP_NUM_57980S_50              0x1654
60 #define CHIP_NUM_57980S_25              0x1656
61
62 #ifndef PCI_DEVICE_ID_NX2_57980E
63 #define PCI_DEVICE_ID_57980S_40         CHIP_NUM_57980S_40
64 #define PCI_DEVICE_ID_57980S_10         CHIP_NUM_57980S_10
65 #define PCI_DEVICE_ID_57980S_MF         CHIP_NUM_57980S_MF
66 #define PCI_DEVICE_ID_57980S_100        CHIP_NUM_57980S_100
67 #define PCI_DEVICE_ID_57980S_50         CHIP_NUM_57980S_50
68 #define PCI_DEVICE_ID_57980S_25         CHIP_NUM_57980S_25
69 #endif
70
71 static const struct pci_device_id qede_pci_tbl[] = {
72         { PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_40), 0 },
73         { PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_10), 0 },
74         { PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_MF), 0 },
75         { PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_100), 0 },
76         { PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_50), 0 },
77         { PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_25), 0 },
78         { 0 }
79 };
80
81 MODULE_DEVICE_TABLE(pci, qede_pci_tbl);
82
83 static int qede_probe(struct pci_dev *pdev, const struct pci_device_id *id);
84
85 #define TX_TIMEOUT              (5 * HZ)
86
87 static void qede_remove(struct pci_dev *pdev);
88
89 static struct pci_driver qede_pci_driver = {
90         .name = "qede",
91         .id_table = qede_pci_tbl,
92         .probe = qede_probe,
93         .remove = qede_remove,
94 };
95
96 static
97 int __init qede_init(void)
98 {
99         int ret;
100         u32 qed_ver;
101
102         pr_notice("qede_init: %s\n", version);
103
104         qed_ver = qed_get_protocol_version(QED_PROTOCOL_ETH);
105         if (qed_ver !=  QEDE_ETH_INTERFACE_VERSION) {
106                 pr_notice("Version mismatch [%08x != %08x]\n",
107                           qed_ver,
108                           QEDE_ETH_INTERFACE_VERSION);
109                 return -EINVAL;
110         }
111
112         qed_ops = qed_get_eth_ops(QEDE_ETH_INTERFACE_VERSION);
113         if (!qed_ops) {
114                 pr_notice("Failed to get qed ethtool operations\n");
115                 return -EINVAL;
116         }
117
118         ret = pci_register_driver(&qede_pci_driver);
119         if (ret) {
120                 pr_notice("Failed to register driver\n");
121                 qed_put_eth_ops();
122                 return -EINVAL;
123         }
124
125         return 0;
126 }
127
128 static void __exit qede_cleanup(void)
129 {
130         pr_notice("qede_cleanup called\n");
131
132         pci_unregister_driver(&qede_pci_driver);
133         qed_put_eth_ops();
134 }
135
136 module_init(qede_init);
137 module_exit(qede_cleanup);
138
139 /* -------------------------------------------------------------------------
140  * START OF PROBE / REMOVE
141  * -------------------------------------------------------------------------
142  */
143
144 static struct qede_dev *qede_alloc_etherdev(struct qed_dev *cdev,
145                                             struct pci_dev *pdev,
146                                             struct qed_dev_eth_info *info,
147                                             u32 dp_module,
148                                             u8 dp_level)
149 {
150         struct net_device *ndev;
151         struct qede_dev *edev;
152
153         ndev = alloc_etherdev_mqs(sizeof(*edev),
154                                   info->num_queues,
155                                   info->num_queues);
156         if (!ndev) {
157                 pr_err("etherdev allocation failed\n");
158                 return NULL;
159         }
160
161         edev = netdev_priv(ndev);
162         edev->ndev = ndev;
163         edev->cdev = cdev;
164         edev->pdev = pdev;
165         edev->dp_module = dp_module;
166         edev->dp_level = dp_level;
167         edev->ops = qed_ops;
168
169         DP_INFO(edev, "Allocated netdev with 64 tx queues and 64 rx queues\n");
170
171         SET_NETDEV_DEV(ndev, &pdev->dev);
172
173         memcpy(&edev->dev_info, info, sizeof(*info));
174
175         edev->num_tc = edev->dev_info.num_tc;
176
177         return edev;
178 }
179
180 static void qede_init_ndev(struct qede_dev *edev)
181 {
182         struct net_device *ndev = edev->ndev;
183         struct pci_dev *pdev = edev->pdev;
184         u32 hw_features;
185
186         pci_set_drvdata(pdev, ndev);
187
188         ndev->mem_start = edev->dev_info.common.pci_mem_start;
189         ndev->base_addr = ndev->mem_start;
190         ndev->mem_end = edev->dev_info.common.pci_mem_end;
191         ndev->irq = edev->dev_info.common.pci_irq;
192
193         ndev->watchdog_timeo = TX_TIMEOUT;
194
195         /* user-changeble features */
196         hw_features = NETIF_F_GRO | NETIF_F_SG |
197                       NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
198                       NETIF_F_TSO | NETIF_F_TSO6;
199
200         ndev->vlan_features = hw_features | NETIF_F_RXHASH | NETIF_F_RXCSUM |
201                               NETIF_F_HIGHDMA;
202         ndev->features = hw_features | NETIF_F_RXHASH | NETIF_F_RXCSUM |
203                          NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HIGHDMA |
204                          NETIF_F_HW_VLAN_CTAG_TX;
205
206         ndev->hw_features = hw_features;
207
208         /* Set network device HW mac */
209         ether_addr_copy(edev->ndev->dev_addr, edev->dev_info.common.hw_mac);
210 }
211
212 /* This function converts from 32b param to two params of level and module
213  * Input 32b decoding:
214  * b31 - enable all NOTICE prints. NOTICE prints are for deviation from the
215  * 'happy' flow, e.g. memory allocation failed.
216  * b30 - enable all INFO prints. INFO prints are for major steps in the flow
217  * and provide important parameters.
218  * b29-b0 - per-module bitmap, where each bit enables VERBOSE prints of that
219  * module. VERBOSE prints are for tracking the specific flow in low level.
220  *
221  * Notice that the level should be that of the lowest required logs.
222  */
223 static void qede_config_debug(uint debug, u32 *p_dp_module, u8 *p_dp_level)
224 {
225         *p_dp_level = QED_LEVEL_NOTICE;
226         *p_dp_module = 0;
227
228         if (debug & QED_LOG_VERBOSE_MASK) {
229                 *p_dp_level = QED_LEVEL_VERBOSE;
230                 *p_dp_module = (debug & 0x3FFFFFFF);
231         } else if (debug & QED_LOG_INFO_MASK) {
232                 *p_dp_level = QED_LEVEL_INFO;
233         } else if (debug & QED_LOG_NOTICE_MASK) {
234                 *p_dp_level = QED_LEVEL_NOTICE;
235         }
236 }
237
238 static void qede_update_pf_params(struct qed_dev *cdev)
239 {
240         struct qed_pf_params pf_params;
241
242         /* 16 rx + 16 tx */
243         memset(&pf_params, 0, sizeof(struct qed_pf_params));
244         pf_params.eth_pf_params.num_cons = 32;
245         qed_ops->common->update_pf_params(cdev, &pf_params);
246 }
247
248 enum qede_probe_mode {
249         QEDE_PROBE_NORMAL,
250 };
251
252 static int __qede_probe(struct pci_dev *pdev, u32 dp_module, u8 dp_level,
253                         enum qede_probe_mode mode)
254 {
255         struct qed_slowpath_params params;
256         struct qed_dev_eth_info dev_info;
257         struct qede_dev *edev;
258         struct qed_dev *cdev;
259         int rc;
260
261         if (unlikely(dp_level & QED_LEVEL_INFO))
262                 pr_notice("Starting qede probe\n");
263
264         cdev = qed_ops->common->probe(pdev, QED_PROTOCOL_ETH,
265                                       dp_module, dp_level);
266         if (!cdev) {
267                 rc = -ENODEV;
268                 goto err0;
269         }
270
271         qede_update_pf_params(cdev);
272
273         /* Start the Slowpath-process */
274         memset(&params, 0, sizeof(struct qed_slowpath_params));
275         params.int_mode = QED_INT_MODE_MSIX;
276         params.drv_major = QEDE_MAJOR_VERSION;
277         params.drv_minor = QEDE_MINOR_VERSION;
278         params.drv_rev = QEDE_REVISION_VERSION;
279         params.drv_eng = QEDE_ENGINEERING_VERSION;
280         strlcpy(params.name, "qede LAN", QED_DRV_VER_STR_SIZE);
281         rc = qed_ops->common->slowpath_start(cdev, &params);
282         if (rc) {
283                 pr_notice("Cannot start slowpath\n");
284                 goto err1;
285         }
286
287         /* Learn information crucial for qede to progress */
288         rc = qed_ops->fill_dev_info(cdev, &dev_info);
289         if (rc)
290                 goto err2;
291
292         edev = qede_alloc_etherdev(cdev, pdev, &dev_info, dp_module,
293                                    dp_level);
294         if (!edev) {
295                 rc = -ENOMEM;
296                 goto err2;
297         }
298
299         qede_init_ndev(edev);
300
301         edev->ops->common->set_id(cdev, edev->ndev->name, DRV_MODULE_VERSION);
302
303         DP_INFO(edev, "Ending successfully qede probe\n");
304
305         return 0;
306
307 err2:
308         qed_ops->common->slowpath_stop(cdev);
309 err1:
310         qed_ops->common->remove(cdev);
311 err0:
312         return rc;
313 }
314
315 static int qede_probe(struct pci_dev *pdev, const struct pci_device_id *id)
316 {
317         u32 dp_module = 0;
318         u8 dp_level = 0;
319
320         qede_config_debug(debug, &dp_module, &dp_level);
321
322         return __qede_probe(pdev, dp_module, dp_level,
323                             QEDE_PROBE_NORMAL);
324 }
325
326 enum qede_remove_mode {
327         QEDE_REMOVE_NORMAL,
328 };
329
330 static void __qede_remove(struct pci_dev *pdev, enum qede_remove_mode mode)
331 {
332         struct net_device *ndev = pci_get_drvdata(pdev);
333         struct qede_dev *edev = netdev_priv(ndev);
334         struct qed_dev *cdev = edev->cdev;
335
336         DP_INFO(edev, "Starting qede_remove\n");
337
338         edev->ops->common->set_power_state(cdev, PCI_D0);
339
340         pci_set_drvdata(pdev, NULL);
341
342         free_netdev(ndev);
343
344         /* Use global ops since we've freed edev */
345         qed_ops->common->slowpath_stop(cdev);
346         qed_ops->common->remove(cdev);
347
348         pr_notice("Ending successfully qede_remove\n");
349 }
350
351 static void qede_remove(struct pci_dev *pdev)
352 {
353         __qede_remove(pdev, QEDE_REMOVE_NORMAL);
354 }