GNU Linux-libre 4.14.328-gnu1
[releases.git] / drivers / net / ethernet / netronome / nfp / nfp_main.c
1 /*
2  * Copyright (C) 2015-2017 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_main.c
36  * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
37  *          Alejandro Lucero <alejandro.lucero@netronome.com>
38  *          Jason McMullan <jason.mcmullan@netronome.com>
39  *          Rolf Neugebauer <rolf.neugebauer@netronome.com>
40  */
41
42 #include <linux/kernel.h>
43 #include <linux/module.h>
44 #include <linux/mutex.h>
45 #include <linux/pci.h>
46 #include <linux/firmware.h>
47 #include <linux/vermagic.h>
48 #include <net/devlink.h>
49
50 #include "nfpcore/nfp.h"
51 #include "nfpcore/nfp_cpp.h"
52 #include "nfpcore/nfp_nffw.h"
53 #include "nfpcore/nfp_nsp.h"
54
55 #include "nfpcore/nfp6000_pcie.h"
56
57 #include "nfp_app.h"
58 #include "nfp_main.h"
59 #include "nfp_net.h"
60
61 static const char nfp_driver_name[] = "nfp";
62 const char nfp_driver_version[] = VERMAGIC_STRING;
63
64 static const struct pci_device_id nfp_pci_device_ids[] = {
65         { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP6000,
66           PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
67           PCI_ANY_ID, 0,
68         },
69         { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP4000,
70           PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
71           PCI_ANY_ID, 0,
72         },
73         { 0, } /* Required last entry. */
74 };
75 MODULE_DEVICE_TABLE(pci, nfp_pci_device_ids);
76
77 static bool nfp_board_ready(struct nfp_pf *pf)
78 {
79         const char *cp;
80         long state;
81         int err;
82
83         cp = nfp_hwinfo_lookup(pf->hwinfo, "board.state");
84         if (!cp)
85                 return false;
86
87         err = kstrtol(cp, 0, &state);
88         if (err < 0)
89                 return false;
90
91         return state == 15;
92 }
93
94 static int nfp_pf_board_state_wait(struct nfp_pf *pf)
95 {
96         const unsigned long wait_until = jiffies + 10 * HZ;
97
98         while (!nfp_board_ready(pf)) {
99                 if (time_is_before_eq_jiffies(wait_until)) {
100                         nfp_err(pf->cpp, "NFP board initialization timeout\n");
101                         return -EINVAL;
102                 }
103
104                 nfp_info(pf->cpp, "waiting for board initialization\n");
105                 if (msleep_interruptible(500))
106                         return -ERESTARTSYS;
107
108                 /* Refresh cached information */
109                 kfree(pf->hwinfo);
110                 pf->hwinfo = nfp_hwinfo_read(pf->cpp);
111         }
112
113         return 0;
114 }
115
116 static int nfp_pcie_sriov_read_nfd_limit(struct nfp_pf *pf)
117 {
118         int err;
119
120         pf->limit_vfs = nfp_rtsym_read_le(pf->rtbl, "nfd_vf_cfg_max_vfs", &err);
121         if (!err)
122                 return pci_sriov_set_totalvfs(pf->pdev, pf->limit_vfs);
123
124         pf->limit_vfs = ~0;
125         pci_sriov_set_totalvfs(pf->pdev, 0); /* 0 is unset */
126         /* Allow any setting for backwards compatibility if symbol not found */
127         if (err == -ENOENT)
128                 return 0;
129
130         nfp_warn(pf->cpp, "Warning: VF limit read failed: %d\n", err);
131         return err;
132 }
133
134 static int nfp_pcie_sriov_enable(struct pci_dev *pdev, int num_vfs)
135 {
136 #ifdef CONFIG_PCI_IOV
137         struct nfp_pf *pf = pci_get_drvdata(pdev);
138         int err;
139
140         if (num_vfs > pf->limit_vfs) {
141                 nfp_info(pf->cpp, "Firmware limits number of VFs to %u\n",
142                          pf->limit_vfs);
143                 return -EINVAL;
144         }
145
146         err = pci_enable_sriov(pdev, num_vfs);
147         if (err) {
148                 dev_warn(&pdev->dev, "Failed to enable PCI SR-IOV: %d\n", err);
149                 return err;
150         }
151
152         mutex_lock(&pf->lock);
153
154         err = nfp_app_sriov_enable(pf->app, num_vfs);
155         if (err) {
156                 dev_warn(&pdev->dev,
157                          "App specific PCI SR-IOV configuration failed: %d\n",
158                          err);
159                 goto err_sriov_disable;
160         }
161
162         pf->num_vfs = num_vfs;
163
164         dev_dbg(&pdev->dev, "Created %d VFs.\n", pf->num_vfs);
165
166         mutex_unlock(&pf->lock);
167         return num_vfs;
168
169 err_sriov_disable:
170         mutex_unlock(&pf->lock);
171         pci_disable_sriov(pdev);
172         return err;
173 #endif
174         return 0;
175 }
176
177 static int nfp_pcie_sriov_disable(struct pci_dev *pdev)
178 {
179 #ifdef CONFIG_PCI_IOV
180         struct nfp_pf *pf = pci_get_drvdata(pdev);
181
182         mutex_lock(&pf->lock);
183
184         /* If the VFs are assigned we cannot shut down SR-IOV without
185          * causing issues, so just leave the hardware available but
186          * disabled
187          */
188         if (pci_vfs_assigned(pdev)) {
189                 dev_warn(&pdev->dev, "Disabling while VFs assigned - VFs will not be deallocated\n");
190                 mutex_unlock(&pf->lock);
191                 return -EPERM;
192         }
193
194         nfp_app_sriov_disable(pf->app);
195
196         pf->num_vfs = 0;
197
198         mutex_unlock(&pf->lock);
199
200         pci_disable_sriov(pdev);
201         dev_dbg(&pdev->dev, "Removed VFs.\n");
202 #endif
203         return 0;
204 }
205
206 static int nfp_pcie_sriov_configure(struct pci_dev *pdev, int num_vfs)
207 {
208         if (num_vfs == 0)
209                 return nfp_pcie_sriov_disable(pdev);
210         else
211                 return nfp_pcie_sriov_enable(pdev, num_vfs);
212 }
213
214 static const struct firmware *
215 nfp_net_fw_request(struct pci_dev *pdev, struct nfp_pf *pf, const char *name)
216 {
217         const struct firmware *fw = NULL;
218         int err;
219
220         err = reject_firmware_direct(&fw, name, &pdev->dev);
221         nfp_info(pf->cpp, "  %s: %s\n",
222                  name, err ? "not found" : "found, loading...");
223         if (err)
224                 return NULL;
225
226         return fw;
227 }
228
229 /**
230  * nfp_net_fw_find() - Find the correct firmware image for netdev mode
231  * @pdev:       PCI Device structure
232  * @pf:         NFP PF Device structure
233  *
234  * Return: firmware if found and requested successfully.
235  */
236 static const struct firmware *
237 nfp_net_fw_find(struct pci_dev *pdev, struct nfp_pf *pf)
238 {
239         struct nfp_eth_table_port *port;
240         const struct firmware *fw;
241         const char *fw_model;
242         char fw_name[256];
243         const u8 *serial;
244         u16 interface;
245         int spc, i, j;
246
247         nfp_info(pf->cpp, "Looking for firmware file in order of priority:\n");
248
249         /* First try to find a firmware image specific for this device */
250         interface = nfp_cpp_interface(pf->cpp);
251         nfp_cpp_serial(pf->cpp, &serial);
252         sprintf(fw_name, "/*(DEBLOBBED)*/",
253                 serial, interface >> 8, interface & 0xff);
254         fw = nfp_net_fw_request(pdev, pf, fw_name);
255         if (fw)
256                 return fw;
257
258         /* Then try the PCI name */
259         sprintf(fw_name, "/*(DEBLOBBED)*/", pci_name(pdev));
260         fw = nfp_net_fw_request(pdev, pf, fw_name);
261         if (fw)
262                 return fw;
263
264         /* Finally try the card type and media */
265         if (!pf->eth_tbl) {
266                 dev_err(&pdev->dev, "Error: can't identify media config\n");
267                 return NULL;
268         }
269
270         fw_model = nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno");
271         if (!fw_model) {
272                 dev_err(&pdev->dev, "Error: can't read part number\n");
273                 return NULL;
274         }
275
276         spc = ARRAY_SIZE(fw_name);
277         spc -= snprintf(fw_name, spc, "/*(DEBLOBBED)*/", fw_model);
278
279         for (i = 0; spc > 0 && i < pf->eth_tbl->count; i += j) {
280                 port = &pf->eth_tbl->ports[i];
281                 j = 1;
282                 while (i + j < pf->eth_tbl->count &&
283                        port->speed == port[j].speed)
284                         j++;
285
286                 /*(DEBLOBBED)*/;
287         }
288
289         if (spc <= 0)
290                 return NULL;
291
292         /*(DEBLOBBED)*/;
293         if (spc <= 0)
294                 return NULL;
295
296         return nfp_net_fw_request(pdev, pf, fw_name);
297 }
298
299 /**
300  * nfp_net_fw_load() - Load the firmware image
301  * @pdev:       PCI Device structure
302  * @pf:         NFP PF Device structure
303  * @nsp:        NFP SP handle
304  *
305  * Return: -ERRNO, 0 for no firmware loaded, 1 for firmware loaded
306  */
307 static int
308 nfp_fw_load(struct pci_dev *pdev, struct nfp_pf *pf, struct nfp_nsp *nsp)
309 {
310         const struct firmware *fw;
311         u16 interface;
312         int err;
313
314         interface = nfp_cpp_interface(pf->cpp);
315         if (NFP_CPP_INTERFACE_UNIT_of(interface) != 0) {
316                 /* Only Unit 0 should reset or load firmware */
317                 dev_info(&pdev->dev, "Firmware will be loaded by partner\n");
318                 return 0;
319         }
320
321         fw = nfp_net_fw_find(pdev, pf);
322         if (!fw)
323                 return 0;
324
325         dev_info(&pdev->dev, "Soft-reset, loading FW image\n");
326         err = nfp_nsp_device_soft_reset(nsp);
327         if (err < 0) {
328                 dev_err(&pdev->dev, "Failed to soft reset the NFP: %d\n",
329                         err);
330                 goto exit_release_fw;
331         }
332
333         err = nfp_nsp_load_fw(nsp, fw);
334
335         if (err < 0) {
336                 dev_err(&pdev->dev, "FW loading failed: %d\n", err);
337                 goto exit_release_fw;
338         }
339
340         dev_info(&pdev->dev, "Finished loading FW image\n");
341
342 exit_release_fw:
343         release_firmware(fw);
344
345         return err < 0 ? err : 1;
346 }
347
348 static int nfp_nsp_init(struct pci_dev *pdev, struct nfp_pf *pf)
349 {
350         struct nfp_nsp *nsp;
351         int err;
352
353         err = nfp_resource_wait(pf->cpp, NFP_RESOURCE_NSP, 30);
354         if (err)
355                 return err;
356
357         nsp = nfp_nsp_open(pf->cpp);
358         if (IS_ERR(nsp)) {
359                 err = PTR_ERR(nsp);
360                 dev_err(&pdev->dev, "Failed to access the NSP: %d\n", err);
361                 return err;
362         }
363
364         err = nfp_nsp_wait(nsp);
365         if (err < 0)
366                 goto exit_close_nsp;
367
368         pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
369
370         pf->nspi = __nfp_nsp_identify(nsp);
371         if (pf->nspi)
372                 dev_info(&pdev->dev, "BSP: %s\n", pf->nspi->version);
373
374         err = nfp_fw_load(pdev, pf, nsp);
375         if (err < 0) {
376                 kfree(pf->nspi);
377                 kfree(pf->eth_tbl);
378                 dev_err(&pdev->dev, "Failed to load FW\n");
379                 goto exit_close_nsp;
380         }
381
382         pf->fw_loaded = !!err;
383         err = 0;
384
385 exit_close_nsp:
386         nfp_nsp_close(nsp);
387
388         return err;
389 }
390
391 static void nfp_fw_unload(struct nfp_pf *pf)
392 {
393         struct nfp_nsp *nsp;
394         int err;
395
396         nsp = nfp_nsp_open(pf->cpp);
397         if (IS_ERR(nsp)) {
398                 nfp_err(pf->cpp, "Reset failed, can't open NSP\n");
399                 return;
400         }
401
402         err = nfp_nsp_device_soft_reset(nsp);
403         if (err < 0)
404                 dev_warn(&pf->pdev->dev, "Couldn't unload firmware: %d\n", err);
405         else
406                 dev_info(&pf->pdev->dev, "Firmware safely unloaded\n");
407
408         nfp_nsp_close(nsp);
409 }
410
411 static int nfp_pci_probe(struct pci_dev *pdev,
412                          const struct pci_device_id *pci_id)
413 {
414         struct devlink *devlink;
415         struct nfp_pf *pf;
416         int err;
417
418         err = pci_enable_device(pdev);
419         if (err < 0)
420                 return err;
421
422         pci_set_master(pdev);
423
424         err = dma_set_mask_and_coherent(&pdev->dev,
425                                         DMA_BIT_MASK(NFP_NET_MAX_DMA_BITS));
426         if (err)
427                 goto err_pci_disable;
428
429         err = pci_request_regions(pdev, nfp_driver_name);
430         if (err < 0) {
431                 dev_err(&pdev->dev, "Unable to reserve pci resources.\n");
432                 goto err_pci_disable;
433         }
434
435         devlink = devlink_alloc(&nfp_devlink_ops, sizeof(*pf));
436         if (!devlink) {
437                 err = -ENOMEM;
438                 goto err_rel_regions;
439         }
440         pf = devlink_priv(devlink);
441         INIT_LIST_HEAD(&pf->vnics);
442         INIT_LIST_HEAD(&pf->ports);
443         mutex_init(&pf->lock);
444         pci_set_drvdata(pdev, pf);
445         pf->pdev = pdev;
446
447         pf->wq = alloc_workqueue("nfp-%s", 0, 2, pci_name(pdev));
448         if (!pf->wq) {
449                 err = -ENOMEM;
450                 goto err_pci_priv_unset;
451         }
452
453         pf->cpp = nfp_cpp_from_nfp6000_pcie(pdev);
454         if (IS_ERR_OR_NULL(pf->cpp)) {
455                 err = PTR_ERR(pf->cpp);
456                 if (err >= 0)
457                         err = -ENOMEM;
458                 goto err_disable_msix;
459         }
460
461         pf->hwinfo = nfp_hwinfo_read(pf->cpp);
462
463         dev_info(&pdev->dev, "Assembly: %s%s%s-%s CPLD: %s\n",
464                  nfp_hwinfo_lookup(pf->hwinfo, "assembly.vendor"),
465                  nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno"),
466                  nfp_hwinfo_lookup(pf->hwinfo, "assembly.serial"),
467                  nfp_hwinfo_lookup(pf->hwinfo, "assembly.revision"),
468                  nfp_hwinfo_lookup(pf->hwinfo, "cpld.version"));
469
470         err = nfp_pf_board_state_wait(pf);
471         if (err)
472                 goto err_hwinfo_free;
473
474         err = devlink_register(devlink, &pdev->dev);
475         if (err)
476                 goto err_hwinfo_free;
477
478         err = nfp_nsp_init(pdev, pf);
479         if (err)
480                 goto err_devlink_unreg;
481
482         pf->mip = nfp_mip_open(pf->cpp);
483         pf->rtbl = __nfp_rtsym_table_read(pf->cpp, pf->mip);
484
485         err = nfp_pcie_sriov_read_nfd_limit(pf);
486         if (err)
487                 goto err_fw_unload;
488
489         pf->num_vfs = pci_num_vf(pdev);
490         if (pf->num_vfs > pf->limit_vfs) {
491                 dev_err(&pdev->dev,
492                         "Error: %d VFs already enabled, but loaded FW can only support %d\n",
493                         pf->num_vfs, pf->limit_vfs);
494                 err = -EINVAL;
495                 goto err_fw_unload;
496         }
497
498         err = nfp_net_pci_probe(pf);
499         if (err)
500                 goto err_sriov_unlimit;
501
502         err = nfp_hwmon_register(pf);
503         if (err) {
504                 dev_err(&pdev->dev, "Failed to register hwmon info\n");
505                 goto err_net_remove;
506         }
507
508         return 0;
509
510 err_net_remove:
511         nfp_net_pci_remove(pf);
512 err_sriov_unlimit:
513         pci_sriov_set_totalvfs(pf->pdev, 0);
514 err_fw_unload:
515         kfree(pf->rtbl);
516         nfp_mip_close(pf->mip);
517         if (pf->fw_loaded)
518                 nfp_fw_unload(pf);
519         kfree(pf->eth_tbl);
520         kfree(pf->nspi);
521 err_devlink_unreg:
522         devlink_unregister(devlink);
523 err_hwinfo_free:
524         kfree(pf->hwinfo);
525         nfp_cpp_free(pf->cpp);
526 err_disable_msix:
527         destroy_workqueue(pf->wq);
528 err_pci_priv_unset:
529         pci_set_drvdata(pdev, NULL);
530         mutex_destroy(&pf->lock);
531         devlink_free(devlink);
532 err_rel_regions:
533         pci_release_regions(pdev);
534 err_pci_disable:
535         pci_disable_device(pdev);
536
537         return err;
538 }
539
540 static void nfp_pci_remove(struct pci_dev *pdev)
541 {
542         struct nfp_pf *pf = pci_get_drvdata(pdev);
543         struct devlink *devlink;
544
545         nfp_hwmon_unregister(pf);
546
547         devlink = priv_to_devlink(pf);
548
549         nfp_net_pci_remove(pf);
550
551         nfp_pcie_sriov_disable(pdev);
552         pci_sriov_set_totalvfs(pf->pdev, 0);
553
554         devlink_unregister(devlink);
555
556         kfree(pf->rtbl);
557         nfp_mip_close(pf->mip);
558         if (pf->fw_loaded)
559                 nfp_fw_unload(pf);
560
561         destroy_workqueue(pf->wq);
562         pci_set_drvdata(pdev, NULL);
563         kfree(pf->hwinfo);
564         nfp_cpp_free(pf->cpp);
565
566         kfree(pf->eth_tbl);
567         kfree(pf->nspi);
568         mutex_destroy(&pf->lock);
569         devlink_free(devlink);
570         pci_release_regions(pdev);
571         pci_disable_device(pdev);
572 }
573
574 static struct pci_driver nfp_pci_driver = {
575         .name                   = nfp_driver_name,
576         .id_table               = nfp_pci_device_ids,
577         .probe                  = nfp_pci_probe,
578         .remove                 = nfp_pci_remove,
579         .sriov_configure        = nfp_pcie_sriov_configure,
580 };
581
582 static int __init nfp_main_init(void)
583 {
584         int err;
585
586         pr_info("%s: NFP PCIe Driver, Copyright (C) 2014-2017 Netronome Systems\n",
587                 nfp_driver_name);
588
589         nfp_net_debugfs_create();
590
591         err = pci_register_driver(&nfp_pci_driver);
592         if (err < 0)
593                 goto err_destroy_debugfs;
594
595         err = pci_register_driver(&nfp_netvf_pci_driver);
596         if (err)
597                 goto err_unreg_pf;
598
599         return err;
600
601 err_unreg_pf:
602         pci_unregister_driver(&nfp_pci_driver);
603 err_destroy_debugfs:
604         nfp_net_debugfs_destroy();
605         return err;
606 }
607
608 static void __exit nfp_main_exit(void)
609 {
610         pci_unregister_driver(&nfp_netvf_pci_driver);
611         pci_unregister_driver(&nfp_pci_driver);
612         nfp_net_debugfs_destroy();
613 }
614
615 module_init(nfp_main_init);
616 module_exit(nfp_main_exit);
617
618 /*(DEBLOBBED)*/
619
620 MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
621 MODULE_LICENSE("GPL");
622 MODULE_DESCRIPTION("The Netronome Flow Processor (NFP) driver.");