9474a4eed8ce5f710c002c99e500fc246949687e
[sfrench/cifs-2.6.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 <linux/vmalloc.h>
49 #include <net/devlink.h>
50
51 #include "nfpcore/nfp.h"
52 #include "nfpcore/nfp_cpp.h"
53 #include "nfpcore/nfp_nffw.h"
54 #include "nfpcore/nfp_nsp.h"
55
56 #include "nfpcore/nfp6000_pcie.h"
57
58 #include "nfp_abi.h"
59 #include "nfp_app.h"
60 #include "nfp_main.h"
61 #include "nfp_net.h"
62
63 static const char nfp_driver_name[] = "nfp";
64 const char nfp_driver_version[] = VERMAGIC_STRING;
65
66 static const struct pci_device_id nfp_pci_device_ids[] = {
67         { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP6000,
68           PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
69           PCI_ANY_ID, 0,
70         },
71         { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP5000,
72           PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
73           PCI_ANY_ID, 0,
74         },
75         { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP4000,
76           PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
77           PCI_ANY_ID, 0,
78         },
79         { 0, } /* Required last entry. */
80 };
81 MODULE_DEVICE_TABLE(pci, nfp_pci_device_ids);
82
83 int nfp_pf_rtsym_read_optional(struct nfp_pf *pf, const char *format,
84                                unsigned int default_val)
85 {
86         char name[256];
87         int err = 0;
88         u64 val;
89
90         snprintf(name, sizeof(name), format, nfp_cppcore_pcie_unit(pf->cpp));
91
92         val = nfp_rtsym_read_le(pf->rtbl, name, &err);
93         if (err) {
94                 if (err == -ENOENT)
95                         return default_val;
96                 nfp_err(pf->cpp, "Unable to read symbol %s\n", name);
97                 return err;
98         }
99
100         return val;
101 }
102
103 u8 __iomem *
104 nfp_pf_map_rtsym(struct nfp_pf *pf, const char *name, const char *sym_fmt,
105                  unsigned int min_size, struct nfp_cpp_area **area)
106 {
107         char pf_symbol[256];
108
109         snprintf(pf_symbol, sizeof(pf_symbol), sym_fmt,
110                  nfp_cppcore_pcie_unit(pf->cpp));
111
112         return nfp_rtsym_map(pf->rtbl, pf_symbol, name, min_size, area);
113 }
114
115 /* Callers should hold the devlink instance lock */
116 int nfp_mbox_cmd(struct nfp_pf *pf, u32 cmd, void *in_data, u64 in_length,
117                  void *out_data, u64 out_length)
118 {
119         unsigned long err_at;
120         u64 max_data_sz;
121         u32 val = 0;
122         int n, err;
123
124         if (!pf->mbox)
125                 return -EOPNOTSUPP;
126
127         max_data_sz = nfp_rtsym_size(pf->mbox) - NFP_MBOX_SYM_MIN_SIZE;
128
129         /* Check if cmd field is clear */
130         err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_CMD, &val);
131         if (err || val) {
132                 nfp_warn(pf->cpp, "failed to issue command (%u): %u, err: %d\n",
133                          cmd, val, err);
134                 return err ?: -EBUSY;
135         }
136
137         in_length = min(in_length, max_data_sz);
138         n = nfp_rtsym_write(pf->cpp, pf->mbox, NFP_MBOX_DATA, in_data,
139                             in_length);
140         if (n != in_length)
141                 return -EIO;
142         /* Write data_len and wipe reserved */
143         err = nfp_rtsym_writeq(pf->cpp, pf->mbox, NFP_MBOX_DATA_LEN, in_length);
144         if (err)
145                 return err;
146
147         /* Read back for ordering */
148         err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_DATA_LEN, &val);
149         if (err)
150                 return err;
151
152         /* Write cmd and wipe return value */
153         err = nfp_rtsym_writeq(pf->cpp, pf->mbox, NFP_MBOX_CMD, cmd);
154         if (err)
155                 return err;
156
157         err_at = jiffies + 5 * HZ;
158         while (true) {
159                 /* Wait for command to go to 0 (NFP_MBOX_NO_CMD) */
160                 err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_CMD, &val);
161                 if (err)
162                         return err;
163                 if (!val)
164                         break;
165
166                 if (time_is_before_eq_jiffies(err_at))
167                         return -ETIMEDOUT;
168
169                 msleep(5);
170         }
171
172         /* Copy output if any (could be error info, do it before reading ret) */
173         err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_DATA_LEN, &val);
174         if (err)
175                 return err;
176
177         out_length = min_t(u32, val, min(out_length, max_data_sz));
178         n = nfp_rtsym_read(pf->cpp, pf->mbox, NFP_MBOX_DATA,
179                            out_data, out_length);
180         if (n != out_length)
181                 return -EIO;
182
183         /* Check if there is an error */
184         err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_RET, &val);
185         if (err)
186                 return err;
187         if (val)
188                 return -val;
189
190         return out_length;
191 }
192
193 static bool nfp_board_ready(struct nfp_pf *pf)
194 {
195         const char *cp;
196         long state;
197         int err;
198
199         cp = nfp_hwinfo_lookup(pf->hwinfo, "board.state");
200         if (!cp)
201                 return false;
202
203         err = kstrtol(cp, 0, &state);
204         if (err < 0)
205                 return false;
206
207         return state == 15;
208 }
209
210 static int nfp_pf_board_state_wait(struct nfp_pf *pf)
211 {
212         const unsigned long wait_until = jiffies + 10 * HZ;
213
214         while (!nfp_board_ready(pf)) {
215                 if (time_is_before_eq_jiffies(wait_until)) {
216                         nfp_err(pf->cpp, "NFP board initialization timeout\n");
217                         return -EINVAL;
218                 }
219
220                 nfp_info(pf->cpp, "waiting for board initialization\n");
221                 if (msleep_interruptible(500))
222                         return -ERESTARTSYS;
223
224                 /* Refresh cached information */
225                 kfree(pf->hwinfo);
226                 pf->hwinfo = nfp_hwinfo_read(pf->cpp);
227         }
228
229         return 0;
230 }
231
232 static int nfp_pcie_sriov_read_nfd_limit(struct nfp_pf *pf)
233 {
234         int err;
235
236         pf->limit_vfs = nfp_rtsym_read_le(pf->rtbl, "nfd_vf_cfg_max_vfs", &err);
237         if (err) {
238                 /* For backwards compatibility if symbol not found allow all */
239                 pf->limit_vfs = ~0;
240                 if (err == -ENOENT)
241                         return 0;
242
243                 nfp_warn(pf->cpp, "Warning: VF limit read failed: %d\n", err);
244                 return err;
245         }
246
247         err = pci_sriov_set_totalvfs(pf->pdev, pf->limit_vfs);
248         if (err)
249                 nfp_warn(pf->cpp, "Failed to set VF count in sysfs: %d\n", err);
250         return 0;
251 }
252
253 static int nfp_pcie_sriov_enable(struct pci_dev *pdev, int num_vfs)
254 {
255 #ifdef CONFIG_PCI_IOV
256         struct nfp_pf *pf = pci_get_drvdata(pdev);
257         int err;
258
259         if (num_vfs > pf->limit_vfs) {
260                 nfp_info(pf->cpp, "Firmware limits number of VFs to %u\n",
261                          pf->limit_vfs);
262                 return -EINVAL;
263         }
264
265         err = pci_enable_sriov(pdev, num_vfs);
266         if (err) {
267                 dev_warn(&pdev->dev, "Failed to enable PCI SR-IOV: %d\n", err);
268                 return err;
269         }
270
271         mutex_lock(&pf->lock);
272
273         err = nfp_app_sriov_enable(pf->app, num_vfs);
274         if (err) {
275                 dev_warn(&pdev->dev,
276                          "App specific PCI SR-IOV configuration failed: %d\n",
277                          err);
278                 goto err_sriov_disable;
279         }
280
281         pf->num_vfs = num_vfs;
282
283         dev_dbg(&pdev->dev, "Created %d VFs.\n", pf->num_vfs);
284
285         mutex_unlock(&pf->lock);
286         return num_vfs;
287
288 err_sriov_disable:
289         mutex_unlock(&pf->lock);
290         pci_disable_sriov(pdev);
291         return err;
292 #endif
293         return 0;
294 }
295
296 static int nfp_pcie_sriov_disable(struct pci_dev *pdev)
297 {
298 #ifdef CONFIG_PCI_IOV
299         struct nfp_pf *pf = pci_get_drvdata(pdev);
300
301         mutex_lock(&pf->lock);
302
303         /* If the VFs are assigned we cannot shut down SR-IOV without
304          * causing issues, so just leave the hardware available but
305          * disabled
306          */
307         if (pci_vfs_assigned(pdev)) {
308                 dev_warn(&pdev->dev, "Disabling while VFs assigned - VFs will not be deallocated\n");
309                 mutex_unlock(&pf->lock);
310                 return -EPERM;
311         }
312
313         nfp_app_sriov_disable(pf->app);
314
315         pf->num_vfs = 0;
316
317         mutex_unlock(&pf->lock);
318
319         pci_disable_sriov(pdev);
320         dev_dbg(&pdev->dev, "Removed VFs.\n");
321 #endif
322         return 0;
323 }
324
325 static int nfp_pcie_sriov_configure(struct pci_dev *pdev, int num_vfs)
326 {
327         if (num_vfs == 0)
328                 return nfp_pcie_sriov_disable(pdev);
329         else
330                 return nfp_pcie_sriov_enable(pdev, num_vfs);
331 }
332
333 static const struct firmware *
334 nfp_net_fw_request(struct pci_dev *pdev, struct nfp_pf *pf, const char *name)
335 {
336         const struct firmware *fw = NULL;
337         int err;
338
339         err = request_firmware_direct(&fw, name, &pdev->dev);
340         nfp_info(pf->cpp, "  %s: %s\n",
341                  name, err ? "not found" : "found, loading...");
342         if (err)
343                 return NULL;
344
345         return fw;
346 }
347
348 /**
349  * nfp_net_fw_find() - Find the correct firmware image for netdev mode
350  * @pdev:       PCI Device structure
351  * @pf:         NFP PF Device structure
352  *
353  * Return: firmware if found and requested successfully.
354  */
355 static const struct firmware *
356 nfp_net_fw_find(struct pci_dev *pdev, struct nfp_pf *pf)
357 {
358         struct nfp_eth_table_port *port;
359         const struct firmware *fw;
360         const char *fw_model;
361         char fw_name[256];
362         const u8 *serial;
363         u16 interface;
364         int spc, i, j;
365
366         nfp_info(pf->cpp, "Looking for firmware file in order of priority:\n");
367
368         /* First try to find a firmware image specific for this device */
369         interface = nfp_cpp_interface(pf->cpp);
370         nfp_cpp_serial(pf->cpp, &serial);
371         sprintf(fw_name, "netronome/serial-%pMF-%02hhx-%02hhx.nffw",
372                 serial, interface >> 8, interface & 0xff);
373         fw = nfp_net_fw_request(pdev, pf, fw_name);
374         if (fw)
375                 return fw;
376
377         /* Then try the PCI name */
378         sprintf(fw_name, "netronome/pci-%s.nffw", pci_name(pdev));
379         fw = nfp_net_fw_request(pdev, pf, fw_name);
380         if (fw)
381                 return fw;
382
383         /* Finally try the card type and media */
384         if (!pf->eth_tbl) {
385                 dev_err(&pdev->dev, "Error: can't identify media config\n");
386                 return NULL;
387         }
388
389         fw_model = nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno");
390         if (!fw_model) {
391                 dev_err(&pdev->dev, "Error: can't read part number\n");
392                 return NULL;
393         }
394
395         spc = ARRAY_SIZE(fw_name);
396         spc -= snprintf(fw_name, spc, "netronome/nic_%s", fw_model);
397
398         for (i = 0; spc > 0 && i < pf->eth_tbl->count; i += j) {
399                 port = &pf->eth_tbl->ports[i];
400                 j = 1;
401                 while (i + j < pf->eth_tbl->count &&
402                        port->speed == port[j].speed)
403                         j++;
404
405                 spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc,
406                                 "_%dx%d", j, port->speed / 1000);
407         }
408
409         if (spc <= 0)
410                 return NULL;
411
412         spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc, ".nffw");
413         if (spc <= 0)
414                 return NULL;
415
416         return nfp_net_fw_request(pdev, pf, fw_name);
417 }
418
419 /**
420  * nfp_net_fw_load() - Load the firmware image
421  * @pdev:       PCI Device structure
422  * @pf:         NFP PF Device structure
423  * @nsp:        NFP SP handle
424  *
425  * Return: -ERRNO, 0 for no firmware loaded, 1 for firmware loaded
426  */
427 static int
428 nfp_fw_load(struct pci_dev *pdev, struct nfp_pf *pf, struct nfp_nsp *nsp)
429 {
430         const struct firmware *fw;
431         u16 interface;
432         int err;
433
434         interface = nfp_cpp_interface(pf->cpp);
435         if (NFP_CPP_INTERFACE_UNIT_of(interface) != 0) {
436                 /* Only Unit 0 should reset or load firmware */
437                 dev_info(&pdev->dev, "Firmware will be loaded by partner\n");
438                 return 0;
439         }
440
441         fw = nfp_net_fw_find(pdev, pf);
442         if (!fw) {
443                 if (nfp_nsp_has_stored_fw_load(nsp))
444                         nfp_nsp_load_stored_fw(nsp);
445                 return 0;
446         }
447
448         dev_info(&pdev->dev, "Soft-reset, loading FW image\n");
449         err = nfp_nsp_device_soft_reset(nsp);
450         if (err < 0) {
451                 dev_err(&pdev->dev, "Failed to soft reset the NFP: %d\n",
452                         err);
453                 goto exit_release_fw;
454         }
455
456         err = nfp_nsp_load_fw(nsp, fw);
457         if (err < 0) {
458                 dev_err(&pdev->dev, "FW loading failed: %d\n", err);
459                 goto exit_release_fw;
460         }
461
462         dev_info(&pdev->dev, "Finished loading FW image\n");
463
464 exit_release_fw:
465         release_firmware(fw);
466
467         return err < 0 ? err : 1;
468 }
469
470 static void
471 nfp_nsp_init_ports(struct pci_dev *pdev, struct nfp_pf *pf,
472                    struct nfp_nsp *nsp)
473 {
474         bool needs_reinit = false;
475         int i;
476
477         pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
478         if (!pf->eth_tbl)
479                 return;
480
481         if (!nfp_nsp_has_mac_reinit(nsp))
482                 return;
483
484         for (i = 0; i < pf->eth_tbl->count; i++)
485                 needs_reinit |= pf->eth_tbl->ports[i].override_changed;
486         if (!needs_reinit)
487                 return;
488
489         kfree(pf->eth_tbl);
490         if (nfp_nsp_mac_reinit(nsp))
491                 dev_warn(&pdev->dev, "MAC reinit failed\n");
492
493         pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
494 }
495
496 static int nfp_nsp_init(struct pci_dev *pdev, struct nfp_pf *pf)
497 {
498         struct nfp_nsp *nsp;
499         int err;
500
501         err = nfp_resource_wait(pf->cpp, NFP_RESOURCE_NSP, 30);
502         if (err)
503                 return err;
504
505         nsp = nfp_nsp_open(pf->cpp);
506         if (IS_ERR(nsp)) {
507                 err = PTR_ERR(nsp);
508                 dev_err(&pdev->dev, "Failed to access the NSP: %d\n", err);
509                 return err;
510         }
511
512         err = nfp_nsp_wait(nsp);
513         if (err < 0)
514                 goto exit_close_nsp;
515
516         nfp_nsp_init_ports(pdev, pf, nsp);
517
518         pf->nspi = __nfp_nsp_identify(nsp);
519         if (pf->nspi)
520                 dev_info(&pdev->dev, "BSP: %s\n", pf->nspi->version);
521
522         err = nfp_fw_load(pdev, pf, nsp);
523         if (err < 0) {
524                 kfree(pf->nspi);
525                 kfree(pf->eth_tbl);
526                 dev_err(&pdev->dev, "Failed to load FW\n");
527                 goto exit_close_nsp;
528         }
529
530         pf->fw_loaded = !!err;
531         err = 0;
532
533 exit_close_nsp:
534         nfp_nsp_close(nsp);
535
536         return err;
537 }
538
539 static void nfp_fw_unload(struct nfp_pf *pf)
540 {
541         struct nfp_nsp *nsp;
542         int err;
543
544         nsp = nfp_nsp_open(pf->cpp);
545         if (IS_ERR(nsp)) {
546                 nfp_err(pf->cpp, "Reset failed, can't open NSP\n");
547                 return;
548         }
549
550         err = nfp_nsp_device_soft_reset(nsp);
551         if (err < 0)
552                 dev_warn(&pf->pdev->dev, "Couldn't unload firmware: %d\n", err);
553         else
554                 dev_info(&pf->pdev->dev, "Firmware safely unloaded\n");
555
556         nfp_nsp_close(nsp);
557 }
558
559 static int nfp_pf_find_rtsyms(struct nfp_pf *pf)
560 {
561         char pf_symbol[256];
562         unsigned int pf_id;
563
564         pf_id = nfp_cppcore_pcie_unit(pf->cpp);
565
566         /* Optional per-PCI PF mailbox */
567         snprintf(pf_symbol, sizeof(pf_symbol), NFP_MBOX_SYM_NAME, pf_id);
568         pf->mbox = nfp_rtsym_lookup(pf->rtbl, pf_symbol);
569         if (pf->mbox && nfp_rtsym_size(pf->mbox) < NFP_MBOX_SYM_MIN_SIZE) {
570                 nfp_err(pf->cpp, "PF mailbox symbol too small: %llu < %d\n",
571                         nfp_rtsym_size(pf->mbox), NFP_MBOX_SYM_MIN_SIZE);
572                 return -EINVAL;
573         }
574
575         return 0;
576 }
577
578 static int nfp_pci_probe(struct pci_dev *pdev,
579                          const struct pci_device_id *pci_id)
580 {
581         struct devlink *devlink;
582         struct nfp_pf *pf;
583         int err;
584
585         err = pci_enable_device(pdev);
586         if (err < 0)
587                 return err;
588
589         pci_set_master(pdev);
590
591         err = dma_set_mask_and_coherent(&pdev->dev,
592                                         DMA_BIT_MASK(NFP_NET_MAX_DMA_BITS));
593         if (err)
594                 goto err_pci_disable;
595
596         err = pci_request_regions(pdev, nfp_driver_name);
597         if (err < 0) {
598                 dev_err(&pdev->dev, "Unable to reserve pci resources.\n");
599                 goto err_pci_disable;
600         }
601
602         devlink = devlink_alloc(&nfp_devlink_ops, sizeof(*pf));
603         if (!devlink) {
604                 err = -ENOMEM;
605                 goto err_rel_regions;
606         }
607         pf = devlink_priv(devlink);
608         INIT_LIST_HEAD(&pf->vnics);
609         INIT_LIST_HEAD(&pf->ports);
610         mutex_init(&pf->lock);
611         pci_set_drvdata(pdev, pf);
612         pf->pdev = pdev;
613
614         pf->wq = alloc_workqueue("nfp-%s", 0, 2, pci_name(pdev));
615         if (!pf->wq) {
616                 err = -ENOMEM;
617                 goto err_pci_priv_unset;
618         }
619
620         pf->cpp = nfp_cpp_from_nfp6000_pcie(pdev);
621         if (IS_ERR_OR_NULL(pf->cpp)) {
622                 err = PTR_ERR(pf->cpp);
623                 if (err >= 0)
624                         err = -ENOMEM;
625                 goto err_disable_msix;
626         }
627
628         err = nfp_resource_table_init(pf->cpp);
629         if (err)
630                 goto err_cpp_free;
631
632         pf->hwinfo = nfp_hwinfo_read(pf->cpp);
633
634         dev_info(&pdev->dev, "Assembly: %s%s%s-%s CPLD: %s\n",
635                  nfp_hwinfo_lookup(pf->hwinfo, "assembly.vendor"),
636                  nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno"),
637                  nfp_hwinfo_lookup(pf->hwinfo, "assembly.serial"),
638                  nfp_hwinfo_lookup(pf->hwinfo, "assembly.revision"),
639                  nfp_hwinfo_lookup(pf->hwinfo, "cpld.version"));
640
641         err = nfp_pf_board_state_wait(pf);
642         if (err)
643                 goto err_hwinfo_free;
644
645         err = nfp_nsp_init(pdev, pf);
646         if (err)
647                 goto err_hwinfo_free;
648
649         pf->mip = nfp_mip_open(pf->cpp);
650         pf->rtbl = __nfp_rtsym_table_read(pf->cpp, pf->mip);
651
652         err = nfp_pf_find_rtsyms(pf);
653         if (err)
654                 goto err_fw_unload;
655
656         pf->dump_flag = NFP_DUMP_NSP_DIAG;
657         pf->dumpspec = nfp_net_dump_load_dumpspec(pf->cpp, pf->rtbl);
658
659         err = nfp_pcie_sriov_read_nfd_limit(pf);
660         if (err)
661                 goto err_fw_unload;
662
663         pf->num_vfs = pci_num_vf(pdev);
664         if (pf->num_vfs > pf->limit_vfs) {
665                 dev_err(&pdev->dev,
666                         "Error: %d VFs already enabled, but loaded FW can only support %d\n",
667                         pf->num_vfs, pf->limit_vfs);
668                 err = -EINVAL;
669                 goto err_fw_unload;
670         }
671
672         err = nfp_net_pci_probe(pf);
673         if (err)
674                 goto err_fw_unload;
675
676         err = nfp_hwmon_register(pf);
677         if (err) {
678                 dev_err(&pdev->dev, "Failed to register hwmon info\n");
679                 goto err_net_remove;
680         }
681
682         return 0;
683
684 err_net_remove:
685         nfp_net_pci_remove(pf);
686 err_fw_unload:
687         kfree(pf->rtbl);
688         nfp_mip_close(pf->mip);
689         if (pf->fw_loaded)
690                 nfp_fw_unload(pf);
691         kfree(pf->eth_tbl);
692         kfree(pf->nspi);
693         vfree(pf->dumpspec);
694 err_hwinfo_free:
695         kfree(pf->hwinfo);
696 err_cpp_free:
697         nfp_cpp_free(pf->cpp);
698 err_disable_msix:
699         destroy_workqueue(pf->wq);
700 err_pci_priv_unset:
701         pci_set_drvdata(pdev, NULL);
702         mutex_destroy(&pf->lock);
703         devlink_free(devlink);
704 err_rel_regions:
705         pci_release_regions(pdev);
706 err_pci_disable:
707         pci_disable_device(pdev);
708
709         return err;
710 }
711
712 static void nfp_pci_remove(struct pci_dev *pdev)
713 {
714         struct nfp_pf *pf = pci_get_drvdata(pdev);
715
716         nfp_hwmon_unregister(pf);
717
718         nfp_pcie_sriov_disable(pdev);
719
720         nfp_net_pci_remove(pf);
721
722         vfree(pf->dumpspec);
723         kfree(pf->rtbl);
724         nfp_mip_close(pf->mip);
725         if (pf->fw_loaded)
726                 nfp_fw_unload(pf);
727
728         destroy_workqueue(pf->wq);
729         pci_set_drvdata(pdev, NULL);
730         kfree(pf->hwinfo);
731         nfp_cpp_free(pf->cpp);
732
733         kfree(pf->eth_tbl);
734         kfree(pf->nspi);
735         mutex_destroy(&pf->lock);
736         devlink_free(priv_to_devlink(pf));
737         pci_release_regions(pdev);
738         pci_disable_device(pdev);
739 }
740
741 static struct pci_driver nfp_pci_driver = {
742         .name                   = nfp_driver_name,
743         .id_table               = nfp_pci_device_ids,
744         .probe                  = nfp_pci_probe,
745         .remove                 = nfp_pci_remove,
746         .sriov_configure        = nfp_pcie_sriov_configure,
747 };
748
749 static int __init nfp_main_init(void)
750 {
751         int err;
752
753         pr_info("%s: NFP PCIe Driver, Copyright (C) 2014-2017 Netronome Systems\n",
754                 nfp_driver_name);
755
756         nfp_net_debugfs_create();
757
758         err = pci_register_driver(&nfp_pci_driver);
759         if (err < 0)
760                 goto err_destroy_debugfs;
761
762         err = pci_register_driver(&nfp_netvf_pci_driver);
763         if (err)
764                 goto err_unreg_pf;
765
766         return err;
767
768 err_unreg_pf:
769         pci_unregister_driver(&nfp_pci_driver);
770 err_destroy_debugfs:
771         nfp_net_debugfs_destroy();
772         return err;
773 }
774
775 static void __exit nfp_main_exit(void)
776 {
777         pci_unregister_driver(&nfp_netvf_pci_driver);
778         pci_unregister_driver(&nfp_pci_driver);
779         nfp_net_debugfs_destroy();
780 }
781
782 module_init(nfp_main_init);
783 module_exit(nfp_main_exit);
784
785 MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_1x40.nffw");
786 MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_4x10.nffw");
787 MODULE_FIRMWARE("netronome/nic_AMDA0096-0001_2x10.nffw");
788 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_2x40.nffw");
789 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_4x10_1x40.nffw");
790 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_8x10.nffw");
791 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x10.nffw");
792 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x25.nffw");
793 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_1x10_1x25.nffw");
794
795 MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
796 MODULE_LICENSE("GPL");
797 MODULE_DESCRIPTION("The Netronome Flow Processor (NFP) driver.");
798 MODULE_VERSION(UTS_RELEASE);