libnvdimm, namespace: allow creation of multiple pmem-namespaces per region
[cascardo/linux.git] / drivers / net / ethernet / netronome / nfp / nfp_netvf_main.c
1 /*
2  * Copyright (C) 2015 Netronome Systems, Inc.
3  *
4  * This software is dual licensed under the GNU General License Version 2,
5  * June 1991 as shown in the file COPYING in the top-level directory of this
6  * source tree or the BSD 2-Clause License provided below.  You have the
7  * option to license this software under the complete terms of either license.
8  *
9  * The BSD 2-Clause License:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      1. Redistributions of source code must retain the above
16  *         copyright notice, this list of conditions and the following
17  *         disclaimer.
18  *
19  *      2. Redistributions in binary form must reproduce the above
20  *         copyright notice, this list of conditions and the following
21  *         disclaimer in the documentation and/or other materials
22  *         provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33
34 /*
35  * nfp_netvf_main.c
36  * Netronome virtual function network device driver: Main entry point
37  * Author: Jason McMullan <jason.mcmullan@netronome.com>
38  *         Rolf Neugebauer <rolf.neugebauer@netronome.com>
39  */
40
41 #include <linux/version.h>
42 #include <linux/module.h>
43 #include <linux/kernel.h>
44 #include <linux/init.h>
45 #include <linux/etherdevice.h>
46
47 #include "nfp_net_ctrl.h"
48 #include "nfp_net.h"
49
50 const char nfp_net_driver_name[] = "nfp_netvf";
51 const char nfp_net_driver_version[] = "0.1";
52 #define PCI_DEVICE_NFP6000VF            0x6003
53 static const struct pci_device_id nfp_netvf_pci_device_ids[] = {
54         { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_NFP6000VF,
55           PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
56           PCI_ANY_ID, 0,
57         },
58         { 0, } /* Required last entry. */
59 };
60 MODULE_DEVICE_TABLE(pci, nfp_netvf_pci_device_ids);
61
62 static void nfp_netvf_get_mac_addr(struct nfp_net *nn)
63 {
64         u8 mac_addr[ETH_ALEN];
65
66         put_unaligned_be32(nn_readl(nn, NFP_NET_CFG_MACADDR + 0), &mac_addr[0]);
67         /* We can't do readw for NFP-3200 compatibility */
68         put_unaligned_be16(nn_readl(nn, NFP_NET_CFG_MACADDR + 4) >> 16,
69                            &mac_addr[4]);
70
71         if (!is_valid_ether_addr(mac_addr)) {
72                 eth_hw_addr_random(nn->netdev);
73                 return;
74         }
75
76         ether_addr_copy(nn->netdev->dev_addr, mac_addr);
77         ether_addr_copy(nn->netdev->perm_addr, mac_addr);
78 }
79
80 static int nfp_netvf_pci_probe(struct pci_dev *pdev,
81                                const struct pci_device_id *pci_id)
82 {
83         struct nfp_net_fw_version fw_ver;
84         int max_tx_rings, max_rx_rings;
85         u32 tx_bar_off, rx_bar_off;
86         u32 tx_bar_sz, rx_bar_sz;
87         int tx_bar_no, rx_bar_no;
88         u8 __iomem *ctrl_bar;
89         struct nfp_net *nn;
90         int is_nfp3200;
91         u32 startq;
92         int stride;
93         int err;
94
95         err = pci_enable_device_mem(pdev);
96         if (err)
97                 return err;
98
99         err = pci_request_regions(pdev, nfp_net_driver_name);
100         if (err) {
101                 dev_err(&pdev->dev, "Unable to allocate device memory.\n");
102                 goto err_pci_disable;
103         }
104
105         switch (pdev->device) {
106         case PCI_DEVICE_NFP6000VF:
107                 is_nfp3200 = 0;
108                 break;
109         default:
110                 err = -ENODEV;
111                 goto err_pci_regions;
112         }
113
114         pci_set_master(pdev);
115
116         err = dma_set_mask_and_coherent(&pdev->dev,
117                                         DMA_BIT_MASK(NFP_NET_MAX_DMA_BITS));
118         if (err)
119                 goto err_pci_regions;
120
121         /* Map the Control BAR.
122          *
123          * Irrespective of the advertised BAR size we only map the
124          * first NFP_NET_CFG_BAR_SZ of the BAR.  This keeps the code
125          * the identical for PF and VF drivers.
126          */
127         ctrl_bar = ioremap_nocache(pci_resource_start(pdev, NFP_NET_CTRL_BAR),
128                                    NFP_NET_CFG_BAR_SZ);
129         if (!ctrl_bar) {
130                 dev_err(&pdev->dev,
131                         "Failed to map resource %d\n", NFP_NET_CTRL_BAR);
132                 err = -EIO;
133                 goto err_pci_regions;
134         }
135
136         nfp_net_get_fw_version(&fw_ver, ctrl_bar);
137         if (fw_ver.class != NFP_NET_CFG_VERSION_CLASS_GENERIC) {
138                 dev_err(&pdev->dev, "Unknown Firmware ABI %d.%d.%d.%d\n",
139                         fw_ver.resv, fw_ver.class, fw_ver.major, fw_ver.minor);
140                 err = -EINVAL;
141                 goto err_ctrl_unmap;
142         }
143
144         /* Determine stride */
145         if (nfp_net_fw_ver_eq(&fw_ver, 0, 0, 0, 0) ||
146             nfp_net_fw_ver_eq(&fw_ver, 0, 0, 0, 1) ||
147             nfp_net_fw_ver_eq(&fw_ver, 0, 0, 0x12, 0x48)) {
148                 stride = 2;
149                 tx_bar_no = NFP_NET_Q0_BAR;
150                 rx_bar_no = NFP_NET_Q1_BAR;
151                 dev_warn(&pdev->dev, "OBSOLETE Firmware detected - VF isolation not available\n");
152         } else {
153                 switch (fw_ver.major) {
154                 case 1 ... 3:
155                         if (is_nfp3200) {
156                                 stride = 2;
157                                 tx_bar_no = NFP_NET_Q0_BAR;
158                                 rx_bar_no = NFP_NET_Q1_BAR;
159                         } else {
160                                 stride = 4;
161                                 tx_bar_no = NFP_NET_Q0_BAR;
162                                 rx_bar_no = tx_bar_no;
163                         }
164                         break;
165                 default:
166                         dev_err(&pdev->dev, "Unsupported Firmware ABI %d.%d.%d.%d\n",
167                                 fw_ver.resv, fw_ver.class,
168                                 fw_ver.major, fw_ver.minor);
169                         err = -EINVAL;
170                         goto err_ctrl_unmap;
171                 }
172         }
173
174         /* Find out how many rings are supported */
175         max_tx_rings = readl(ctrl_bar + NFP_NET_CFG_MAX_TXRINGS);
176         max_rx_rings = readl(ctrl_bar + NFP_NET_CFG_MAX_RXRINGS);
177
178         tx_bar_sz = NFP_QCP_QUEUE_ADDR_SZ * max_tx_rings * stride;
179         rx_bar_sz = NFP_QCP_QUEUE_ADDR_SZ * max_rx_rings * stride;
180
181         /* Sanity checks */
182         if (tx_bar_sz > pci_resource_len(pdev, tx_bar_no)) {
183                 dev_err(&pdev->dev,
184                         "TX BAR too small for number of TX rings. Adjusting\n");
185                 tx_bar_sz = pci_resource_len(pdev, tx_bar_no);
186                 max_tx_rings = (tx_bar_sz / NFP_QCP_QUEUE_ADDR_SZ) / 2;
187         }
188         if (rx_bar_sz > pci_resource_len(pdev, rx_bar_no)) {
189                 dev_err(&pdev->dev,
190                         "RX BAR too small for number of RX rings. Adjusting\n");
191                 rx_bar_sz = pci_resource_len(pdev, rx_bar_no);
192                 max_rx_rings = (rx_bar_sz / NFP_QCP_QUEUE_ADDR_SZ) / 2;
193         }
194
195         /* XXX Implement a workaround for THB-350 here.  Ideally, we
196          * have a different PCI ID for A rev VFs.
197          */
198         switch (pdev->device) {
199         case PCI_DEVICE_NFP6000VF:
200                 startq = readl(ctrl_bar + NFP_NET_CFG_START_TXQ);
201                 tx_bar_off = NFP_PCIE_QUEUE(startq);
202                 startq = readl(ctrl_bar + NFP_NET_CFG_START_RXQ);
203                 rx_bar_off = NFP_PCIE_QUEUE(startq);
204                 break;
205         default:
206                 err = -ENODEV;
207                 goto err_ctrl_unmap;
208         }
209
210         /* Allocate and initialise the netdev */
211         nn = nfp_net_netdev_alloc(pdev, max_tx_rings, max_rx_rings);
212         if (IS_ERR(nn)) {
213                 err = PTR_ERR(nn);
214                 goto err_ctrl_unmap;
215         }
216
217         nn->fw_ver = fw_ver;
218         nn->ctrl_bar = ctrl_bar;
219         nn->is_vf = 1;
220         nn->is_nfp3200 = is_nfp3200;
221         nn->stride_tx = stride;
222         nn->stride_rx = stride;
223
224         if (rx_bar_no == tx_bar_no) {
225                 u32 bar_off, bar_sz;
226                 resource_size_t map_addr;
227
228                 /* Make a single overlapping BAR mapping */
229                 if (tx_bar_off < rx_bar_off)
230                         bar_off = tx_bar_off;
231                 else
232                         bar_off = rx_bar_off;
233
234                 if ((tx_bar_off + tx_bar_sz) > (rx_bar_off + rx_bar_sz))
235                         bar_sz = (tx_bar_off + tx_bar_sz) - bar_off;
236                 else
237                         bar_sz = (rx_bar_off + rx_bar_sz) - bar_off;
238
239                 map_addr = pci_resource_start(pdev, tx_bar_no) + bar_off;
240                 nn->q_bar = ioremap_nocache(map_addr, bar_sz);
241                 if (!nn->q_bar) {
242                         nn_err(nn, "Failed to map resource %d\n", tx_bar_no);
243                         err = -EIO;
244                         goto err_netdev_free;
245                 }
246
247                 /* TX queues */
248                 nn->tx_bar = nn->q_bar + (tx_bar_off - bar_off);
249                 /* RX queues */
250                 nn->rx_bar = nn->q_bar + (rx_bar_off - bar_off);
251         } else {
252                 resource_size_t map_addr;
253
254                 /* TX queues */
255                 map_addr = pci_resource_start(pdev, tx_bar_no) + tx_bar_off;
256                 nn->tx_bar = ioremap_nocache(map_addr, tx_bar_sz);
257                 if (!nn->tx_bar) {
258                         nn_err(nn, "Failed to map resource %d\n", tx_bar_no);
259                         err = -EIO;
260                         goto err_netdev_free;
261                 }
262
263                 /* RX queues */
264                 map_addr = pci_resource_start(pdev, rx_bar_no) + rx_bar_off;
265                 nn->rx_bar = ioremap_nocache(map_addr, rx_bar_sz);
266                 if (!nn->rx_bar) {
267                         nn_err(nn, "Failed to map resource %d\n", rx_bar_no);
268                         err = -EIO;
269                         goto err_unmap_tx;
270                 }
271         }
272
273         nfp_netvf_get_mac_addr(nn);
274
275         err = nfp_net_irqs_alloc(nn);
276         if (!err) {
277                 nn_warn(nn, "Unable to allocate MSI-X Vectors. Exiting\n");
278                 err = -EIO;
279                 goto err_unmap_rx;
280         }
281
282         /* Get ME clock frequency from ctrl BAR
283          * XXX for now frequency is hardcoded until we figure out how
284          * to get the value from nfp-hwinfo into ctrl bar
285          */
286         nn->me_freq_mhz = 1200;
287
288         err = nfp_net_netdev_init(nn->netdev);
289         if (err)
290                 goto err_irqs_disable;
291
292         pci_set_drvdata(pdev, nn);
293
294         nfp_net_info(nn);
295         nfp_net_debugfs_adapter_add(nn);
296
297         return 0;
298
299 err_irqs_disable:
300         nfp_net_irqs_disable(nn);
301 err_unmap_rx:
302         if (!nn->q_bar)
303                 iounmap(nn->rx_bar);
304 err_unmap_tx:
305         if (!nn->q_bar)
306                 iounmap(nn->tx_bar);
307         else
308                 iounmap(nn->q_bar);
309 err_netdev_free:
310         pci_set_drvdata(pdev, NULL);
311         nfp_net_netdev_free(nn);
312 err_ctrl_unmap:
313         iounmap(ctrl_bar);
314 err_pci_regions:
315         pci_release_regions(pdev);
316 err_pci_disable:
317         pci_disable_device(pdev);
318         return err;
319 }
320
321 static void nfp_netvf_pci_remove(struct pci_dev *pdev)
322 {
323         struct nfp_net *nn = pci_get_drvdata(pdev);
324
325         /* Note, the order is slightly different from above as we need
326          * to keep the nn pointer around till we have freed everything.
327          */
328         nfp_net_debugfs_adapter_del(nn);
329
330         nfp_net_netdev_clean(nn->netdev);
331
332         nfp_net_irqs_disable(nn);
333
334         if (!nn->q_bar) {
335                 iounmap(nn->rx_bar);
336                 iounmap(nn->tx_bar);
337         } else {
338                 iounmap(nn->q_bar);
339         }
340         iounmap(nn->ctrl_bar);
341
342         pci_set_drvdata(pdev, NULL);
343
344         nfp_net_netdev_free(nn);
345
346         pci_release_regions(pdev);
347         pci_disable_device(pdev);
348 }
349
350 static struct pci_driver nfp_netvf_pci_driver = {
351         .name        = nfp_net_driver_name,
352         .id_table    = nfp_netvf_pci_device_ids,
353         .probe       = nfp_netvf_pci_probe,
354         .remove      = nfp_netvf_pci_remove,
355 };
356
357 static int __init nfp_netvf_init(void)
358 {
359         int err;
360
361         pr_info("%s: NFP VF Network driver, Copyright (C) 2014-2015 Netronome Systems\n",
362                 nfp_net_driver_name);
363
364         nfp_net_debugfs_create();
365         err = pci_register_driver(&nfp_netvf_pci_driver);
366         if (err) {
367                 nfp_net_debugfs_destroy();
368                 return err;
369         }
370
371         return 0;
372 }
373
374 static void __exit nfp_netvf_exit(void)
375 {
376         pci_unregister_driver(&nfp_netvf_pci_driver);
377         nfp_net_debugfs_destroy();
378 }
379
380 module_init(nfp_netvf_init);
381 module_exit(nfp_netvf_exit);
382
383 MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
384 MODULE_LICENSE("GPL");
385 MODULE_DESCRIPTION("NFP VF network device driver");