2 * Copyright (C) 2015-2017 Netronome Systems, Inc.
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.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
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>
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>
50 #include "nfpcore/nfp.h"
51 #include "nfpcore/nfp_cpp.h"
52 #include "nfpcore/nfp_nffw.h"
53 #include "nfpcore/nfp_nsp.h"
55 #include "nfpcore/nfp6000_pcie.h"
61 static const char nfp_driver_name[] = "nfp";
62 const char nfp_driver_version[] = VERMAGIC_STRING;
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,
69 { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP4000,
70 PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
73 { 0, } /* Required last entry. */
75 MODULE_DEVICE_TABLE(pci, nfp_pci_device_ids);
77 static bool nfp_board_ready(struct nfp_pf *pf)
83 cp = nfp_hwinfo_lookup(pf->hwinfo, "board.state");
87 err = kstrtol(cp, 0, &state);
94 static int nfp_pf_board_state_wait(struct nfp_pf *pf)
96 const unsigned long wait_until = jiffies + 10 * HZ;
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");
104 nfp_info(pf->cpp, "waiting for board initialization\n");
105 if (msleep_interruptible(500))
108 /* Refresh cached information */
110 pf->hwinfo = nfp_hwinfo_read(pf->cpp);
116 static int nfp_pcie_sriov_read_nfd_limit(struct nfp_pf *pf)
120 pf->limit_vfs = nfp_rtsym_read_le(pf->rtbl, "nfd_vf_cfg_max_vfs", &err);
122 return pci_sriov_set_totalvfs(pf->pdev, pf->limit_vfs);
125 pci_sriov_set_totalvfs(pf->pdev, 0); /* 0 is unset */
126 /* Allow any setting for backwards compatibility if symbol not found */
130 nfp_warn(pf->cpp, "Warning: VF limit read failed: %d\n", err);
134 static int nfp_pcie_sriov_enable(struct pci_dev *pdev, int num_vfs)
136 #ifdef CONFIG_PCI_IOV
137 struct nfp_pf *pf = pci_get_drvdata(pdev);
140 if (num_vfs > pf->limit_vfs) {
141 nfp_info(pf->cpp, "Firmware limits number of VFs to %u\n",
146 err = pci_enable_sriov(pdev, num_vfs);
148 dev_warn(&pdev->dev, "Failed to enable PCI SR-IOV: %d\n", err);
152 mutex_lock(&pf->lock);
154 err = nfp_app_sriov_enable(pf->app, num_vfs);
157 "App specific PCI SR-IOV configuration failed: %d\n",
159 goto err_sriov_disable;
162 pf->num_vfs = num_vfs;
164 dev_dbg(&pdev->dev, "Created %d VFs.\n", pf->num_vfs);
166 mutex_unlock(&pf->lock);
170 mutex_unlock(&pf->lock);
171 pci_disable_sriov(pdev);
177 static int nfp_pcie_sriov_disable(struct pci_dev *pdev)
179 #ifdef CONFIG_PCI_IOV
180 struct nfp_pf *pf = pci_get_drvdata(pdev);
182 mutex_lock(&pf->lock);
184 /* If the VFs are assigned we cannot shut down SR-IOV without
185 * causing issues, so just leave the hardware available but
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);
194 nfp_app_sriov_disable(pf->app);
198 mutex_unlock(&pf->lock);
200 pci_disable_sriov(pdev);
201 dev_dbg(&pdev->dev, "Removed VFs.\n");
206 static int nfp_pcie_sriov_configure(struct pci_dev *pdev, int num_vfs)
209 return nfp_pcie_sriov_disable(pdev);
211 return nfp_pcie_sriov_enable(pdev, num_vfs);
214 static const struct firmware *
215 nfp_net_fw_request(struct pci_dev *pdev, struct nfp_pf *pf, const char *name)
217 const struct firmware *fw = NULL;
220 err = reject_firmware_direct(&fw, name, &pdev->dev);
221 nfp_info(pf->cpp, " %s: %s\n",
222 name, err ? "not found" : "found, loading...");
230 * nfp_net_fw_find() - Find the correct firmware image for netdev mode
231 * @pdev: PCI Device structure
232 * @pf: NFP PF Device structure
234 * Return: firmware if found and requested successfully.
236 static const struct firmware *
237 nfp_net_fw_find(struct pci_dev *pdev, struct nfp_pf *pf)
239 struct nfp_eth_table_port *port;
240 const struct firmware *fw;
241 const char *fw_model;
247 nfp_info(pf->cpp, "Looking for firmware file in order of priority:\n");
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);
258 /* Then try the PCI name */
259 sprintf(fw_name, "/*(DEBLOBBED)*/", pci_name(pdev));
260 fw = nfp_net_fw_request(pdev, pf, fw_name);
264 /* Finally try the card type and media */
266 dev_err(&pdev->dev, "Error: can't identify media config\n");
270 fw_model = nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno");
272 dev_err(&pdev->dev, "Error: can't read part number\n");
276 spc = ARRAY_SIZE(fw_name);
277 spc -= snprintf(fw_name, spc, "/*(DEBLOBBED)*/", fw_model);
279 for (i = 0; spc > 0 && i < pf->eth_tbl->count; i += j) {
280 port = &pf->eth_tbl->ports[i];
282 while (i + j < pf->eth_tbl->count &&
283 port->speed == port[j].speed)
296 return nfp_net_fw_request(pdev, pf, fw_name);
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
305 * Return: -ERRNO, 0 for no firmware loaded, 1 for firmware loaded
308 nfp_fw_load(struct pci_dev *pdev, struct nfp_pf *pf, struct nfp_nsp *nsp)
310 const struct firmware *fw;
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");
321 fw = nfp_net_fw_find(pdev, pf);
325 dev_info(&pdev->dev, "Soft-reset, loading FW image\n");
326 err = nfp_nsp_device_soft_reset(nsp);
328 dev_err(&pdev->dev, "Failed to soft reset the NFP: %d\n",
330 goto exit_release_fw;
333 err = nfp_nsp_load_fw(nsp, fw);
336 dev_err(&pdev->dev, "FW loading failed: %d\n", err);
337 goto exit_release_fw;
340 dev_info(&pdev->dev, "Finished loading FW image\n");
343 release_firmware(fw);
345 return err < 0 ? err : 1;
348 static int nfp_nsp_init(struct pci_dev *pdev, struct nfp_pf *pf)
353 err = nfp_resource_wait(pf->cpp, NFP_RESOURCE_NSP, 30);
357 nsp = nfp_nsp_open(pf->cpp);
360 dev_err(&pdev->dev, "Failed to access the NSP: %d\n", err);
364 err = nfp_nsp_wait(nsp);
368 pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
370 pf->nspi = __nfp_nsp_identify(nsp);
372 dev_info(&pdev->dev, "BSP: %s\n", pf->nspi->version);
374 err = nfp_fw_load(pdev, pf, nsp);
378 dev_err(&pdev->dev, "Failed to load FW\n");
382 pf->fw_loaded = !!err;
391 static void nfp_fw_unload(struct nfp_pf *pf)
396 nsp = nfp_nsp_open(pf->cpp);
398 nfp_err(pf->cpp, "Reset failed, can't open NSP\n");
402 err = nfp_nsp_device_soft_reset(nsp);
404 dev_warn(&pf->pdev->dev, "Couldn't unload firmware: %d\n", err);
406 dev_info(&pf->pdev->dev, "Firmware safely unloaded\n");
411 static int nfp_pci_probe(struct pci_dev *pdev,
412 const struct pci_device_id *pci_id)
414 struct devlink *devlink;
418 err = pci_enable_device(pdev);
422 pci_set_master(pdev);
424 err = dma_set_mask_and_coherent(&pdev->dev,
425 DMA_BIT_MASK(NFP_NET_MAX_DMA_BITS));
427 goto err_pci_disable;
429 err = pci_request_regions(pdev, nfp_driver_name);
431 dev_err(&pdev->dev, "Unable to reserve pci resources.\n");
432 goto err_pci_disable;
435 devlink = devlink_alloc(&nfp_devlink_ops, sizeof(*pf));
438 goto err_rel_regions;
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);
447 pf->wq = alloc_workqueue("nfp-%s", 0, 2, pci_name(pdev));
450 goto err_pci_priv_unset;
453 pf->cpp = nfp_cpp_from_nfp6000_pcie(pdev);
454 if (IS_ERR_OR_NULL(pf->cpp)) {
455 err = PTR_ERR(pf->cpp);
458 goto err_disable_msix;
461 pf->hwinfo = nfp_hwinfo_read(pf->cpp);
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"));
470 err = nfp_pf_board_state_wait(pf);
472 goto err_hwinfo_free;
474 err = devlink_register(devlink, &pdev->dev);
476 goto err_hwinfo_free;
478 err = nfp_nsp_init(pdev, pf);
480 goto err_devlink_unreg;
482 pf->mip = nfp_mip_open(pf->cpp);
483 pf->rtbl = __nfp_rtsym_table_read(pf->cpp, pf->mip);
485 err = nfp_pcie_sriov_read_nfd_limit(pf);
489 pf->num_vfs = pci_num_vf(pdev);
490 if (pf->num_vfs > pf->limit_vfs) {
492 "Error: %d VFs already enabled, but loaded FW can only support %d\n",
493 pf->num_vfs, pf->limit_vfs);
498 err = nfp_net_pci_probe(pf);
500 goto err_sriov_unlimit;
502 err = nfp_hwmon_register(pf);
504 dev_err(&pdev->dev, "Failed to register hwmon info\n");
511 nfp_net_pci_remove(pf);
513 pci_sriov_set_totalvfs(pf->pdev, 0);
516 nfp_mip_close(pf->mip);
522 devlink_unregister(devlink);
525 nfp_cpp_free(pf->cpp);
527 destroy_workqueue(pf->wq);
529 pci_set_drvdata(pdev, NULL);
530 mutex_destroy(&pf->lock);
531 devlink_free(devlink);
533 pci_release_regions(pdev);
535 pci_disable_device(pdev);
540 static void nfp_pci_remove(struct pci_dev *pdev)
542 struct nfp_pf *pf = pci_get_drvdata(pdev);
543 struct devlink *devlink;
545 nfp_hwmon_unregister(pf);
547 devlink = priv_to_devlink(pf);
549 nfp_net_pci_remove(pf);
551 nfp_pcie_sriov_disable(pdev);
552 pci_sriov_set_totalvfs(pf->pdev, 0);
554 devlink_unregister(devlink);
557 nfp_mip_close(pf->mip);
561 destroy_workqueue(pf->wq);
562 pci_set_drvdata(pdev, NULL);
564 nfp_cpp_free(pf->cpp);
568 mutex_destroy(&pf->lock);
569 devlink_free(devlink);
570 pci_release_regions(pdev);
571 pci_disable_device(pdev);
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,
582 static int __init nfp_main_init(void)
586 pr_info("%s: NFP PCIe Driver, Copyright (C) 2014-2017 Netronome Systems\n",
589 nfp_net_debugfs_create();
591 err = pci_register_driver(&nfp_pci_driver);
593 goto err_destroy_debugfs;
595 err = pci_register_driver(&nfp_netvf_pci_driver);
602 pci_unregister_driver(&nfp_pci_driver);
604 nfp_net_debugfs_destroy();
608 static void __exit nfp_main_exit(void)
610 pci_unregister_driver(&nfp_netvf_pci_driver);
611 pci_unregister_driver(&nfp_pci_driver);
612 nfp_net_debugfs_destroy();
615 module_init(nfp_main_init);
616 module_exit(nfp_main_exit);
620 MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
621 MODULE_LICENSE("GPL");
622 MODULE_DESCRIPTION("The Netronome Flow Processor (NFP) driver.");