1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2015-2018 Netronome Systems, Inc. */
6 * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
7 * Alejandro Lucero <alejandro.lucero@netronome.com>
8 * Jason McMullan <jason.mcmullan@netronome.com>
9 * Rolf Neugebauer <rolf.neugebauer@netronome.com>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/pci.h>
16 #include <linux/firmware.h>
17 #include <linux/vermagic.h>
18 #include <linux/vmalloc.h>
19 #include <net/devlink.h>
21 #include "nfpcore/nfp.h"
22 #include "nfpcore/nfp_cpp.h"
23 #include "nfpcore/nfp_nffw.h"
24 #include "nfpcore/nfp_nsp.h"
26 #include "nfpcore/nfp6000_pcie.h"
33 static const char nfp_driver_name[] = "nfp";
34 const char nfp_driver_version[] = VERMAGIC_STRING;
36 static const struct pci_device_id nfp_pci_device_ids[] = {
37 { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP6000,
38 PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
41 { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP5000,
42 PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
45 { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP4000,
46 PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
49 { 0, } /* Required last entry. */
51 MODULE_DEVICE_TABLE(pci, nfp_pci_device_ids);
53 int nfp_pf_rtsym_read_optional(struct nfp_pf *pf, const char *format,
54 unsigned int default_val)
60 snprintf(name, sizeof(name), format, nfp_cppcore_pcie_unit(pf->cpp));
62 val = nfp_rtsym_read_le(pf->rtbl, name, &err);
66 nfp_err(pf->cpp, "Unable to read symbol %s\n", name);
74 nfp_pf_map_rtsym(struct nfp_pf *pf, const char *name, const char *sym_fmt,
75 unsigned int min_size, struct nfp_cpp_area **area)
79 snprintf(pf_symbol, sizeof(pf_symbol), sym_fmt,
80 nfp_cppcore_pcie_unit(pf->cpp));
82 return nfp_rtsym_map(pf->rtbl, pf_symbol, name, min_size, area);
85 /* Callers should hold the devlink instance lock */
86 int nfp_mbox_cmd(struct nfp_pf *pf, u32 cmd, void *in_data, u64 in_length,
87 void *out_data, u64 out_length)
97 max_data_sz = nfp_rtsym_size(pf->mbox) - NFP_MBOX_SYM_MIN_SIZE;
99 /* Check if cmd field is clear */
100 err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_CMD, &val);
102 nfp_warn(pf->cpp, "failed to issue command (%u): %u, err: %d\n",
104 return err ?: -EBUSY;
107 in_length = min(in_length, max_data_sz);
108 n = nfp_rtsym_write(pf->cpp, pf->mbox, NFP_MBOX_DATA, in_data,
112 /* Write data_len and wipe reserved */
113 err = nfp_rtsym_writeq(pf->cpp, pf->mbox, NFP_MBOX_DATA_LEN, in_length);
117 /* Read back for ordering */
118 err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_DATA_LEN, &val);
122 /* Write cmd and wipe return value */
123 err = nfp_rtsym_writeq(pf->cpp, pf->mbox, NFP_MBOX_CMD, cmd);
127 err_at = jiffies + 5 * HZ;
129 /* Wait for command to go to 0 (NFP_MBOX_NO_CMD) */
130 err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_CMD, &val);
136 if (time_is_before_eq_jiffies(err_at))
142 /* Copy output if any (could be error info, do it before reading ret) */
143 err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_DATA_LEN, &val);
147 out_length = min_t(u32, val, min(out_length, max_data_sz));
148 n = nfp_rtsym_read(pf->cpp, pf->mbox, NFP_MBOX_DATA,
149 out_data, out_length);
153 /* Check if there is an error */
154 err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_RET, &val);
163 static bool nfp_board_ready(struct nfp_pf *pf)
169 cp = nfp_hwinfo_lookup(pf->hwinfo, "board.state");
173 err = kstrtol(cp, 0, &state);
180 static int nfp_pf_board_state_wait(struct nfp_pf *pf)
182 const unsigned long wait_until = jiffies + 10 * HZ;
184 while (!nfp_board_ready(pf)) {
185 if (time_is_before_eq_jiffies(wait_until)) {
186 nfp_err(pf->cpp, "NFP board initialization timeout\n");
190 nfp_info(pf->cpp, "waiting for board initialization\n");
191 if (msleep_interruptible(500))
194 /* Refresh cached information */
196 pf->hwinfo = nfp_hwinfo_read(pf->cpp);
202 static int nfp_pcie_sriov_read_nfd_limit(struct nfp_pf *pf)
206 pf->limit_vfs = nfp_rtsym_read_le(pf->rtbl, "nfd_vf_cfg_max_vfs", &err);
208 /* For backwards compatibility if symbol not found allow all */
213 nfp_warn(pf->cpp, "Warning: VF limit read failed: %d\n", err);
217 err = pci_sriov_set_totalvfs(pf->pdev, pf->limit_vfs);
219 nfp_warn(pf->cpp, "Failed to set VF count in sysfs: %d\n", err);
223 static int nfp_pcie_sriov_enable(struct pci_dev *pdev, int num_vfs)
225 #ifdef CONFIG_PCI_IOV
226 struct nfp_pf *pf = pci_get_drvdata(pdev);
229 if (num_vfs > pf->limit_vfs) {
230 nfp_info(pf->cpp, "Firmware limits number of VFs to %u\n",
235 err = pci_enable_sriov(pdev, num_vfs);
237 dev_warn(&pdev->dev, "Failed to enable PCI SR-IOV: %d\n", err);
241 mutex_lock(&pf->lock);
243 err = nfp_app_sriov_enable(pf->app, num_vfs);
246 "App specific PCI SR-IOV configuration failed: %d\n",
248 goto err_sriov_disable;
251 pf->num_vfs = num_vfs;
253 dev_dbg(&pdev->dev, "Created %d VFs.\n", pf->num_vfs);
255 mutex_unlock(&pf->lock);
259 mutex_unlock(&pf->lock);
260 pci_disable_sriov(pdev);
266 static int nfp_pcie_sriov_disable(struct pci_dev *pdev)
268 #ifdef CONFIG_PCI_IOV
269 struct nfp_pf *pf = pci_get_drvdata(pdev);
271 mutex_lock(&pf->lock);
273 /* If the VFs are assigned we cannot shut down SR-IOV without
274 * causing issues, so just leave the hardware available but
277 if (pci_vfs_assigned(pdev)) {
278 dev_warn(&pdev->dev, "Disabling while VFs assigned - VFs will not be deallocated\n");
279 mutex_unlock(&pf->lock);
283 nfp_app_sriov_disable(pf->app);
287 mutex_unlock(&pf->lock);
289 pci_disable_sriov(pdev);
290 dev_dbg(&pdev->dev, "Removed VFs.\n");
295 static int nfp_pcie_sriov_configure(struct pci_dev *pdev, int num_vfs)
297 if (!pci_get_drvdata(pdev))
301 return nfp_pcie_sriov_disable(pdev);
303 return nfp_pcie_sriov_enable(pdev, num_vfs);
306 int nfp_flash_update_common(struct nfp_pf *pf, const char *path,
307 struct netlink_ext_ack *extack)
309 struct device *dev = &pf->pdev->dev;
310 const struct firmware *fw;
314 nsp = nfp_nsp_open(pf->cpp);
318 NL_SET_ERR_MSG_MOD(extack, "can't access NSP");
320 dev_err(dev, "Failed to access the NSP: %d\n", err);
324 err = reject_firmware_direct(&fw, path, dev);
326 NL_SET_ERR_MSG_MOD(extack,
327 "unable to read flash file from disk");
331 dev_info(dev, "Please be patient while writing flash image: %s\n",
334 err = nfp_nsp_write_flash(nsp, fw);
336 goto exit_release_fw;
337 dev_info(dev, "Finished writing flash image\n");
341 release_firmware(fw);
347 static const struct firmware *
348 nfp_net_fw_request(struct pci_dev *pdev, struct nfp_pf *pf, const char *name)
350 const struct firmware *fw = NULL;
353 err = reject_firmware_direct(&fw, name, &pdev->dev);
354 nfp_info(pf->cpp, " %s: %s\n",
355 name, err ? "not found" : "found, loading...");
363 * nfp_net_fw_find() - Find the correct firmware image for netdev mode
364 * @pdev: PCI Device structure
365 * @pf: NFP PF Device structure
367 * Return: firmware if found and requested successfully.
369 static const struct firmware *
370 nfp_net_fw_find(struct pci_dev *pdev, struct nfp_pf *pf)
372 struct nfp_eth_table_port *port;
373 const struct firmware *fw;
374 const char *fw_model;
380 nfp_info(pf->cpp, "Looking for firmware file in order of priority:\n");
382 /* First try to find a firmware image specific for this device */
383 interface = nfp_cpp_interface(pf->cpp);
384 nfp_cpp_serial(pf->cpp, &serial);
385 sprintf(fw_name, "/*(DEBLOBBED)*/",
386 serial, interface >> 8, interface & 0xff);
387 fw = nfp_net_fw_request(pdev, pf, fw_name);
391 /* Then try the PCI name */
392 sprintf(fw_name, "/*(DEBLOBBED)*/", pci_name(pdev));
393 fw = nfp_net_fw_request(pdev, pf, fw_name);
397 /* Finally try the card type and media */
399 dev_err(&pdev->dev, "Error: can't identify media config\n");
403 fw_model = nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno");
405 dev_err(&pdev->dev, "Error: can't read part number\n");
409 spc = ARRAY_SIZE(fw_name);
410 spc -= snprintf(fw_name, spc, "/*(DEBLOBBED)*/", fw_model);
412 for (i = 0; spc > 0 && i < pf->eth_tbl->count; i += j) {
413 port = &pf->eth_tbl->ports[i];
415 while (i + j < pf->eth_tbl->count &&
416 port->speed == port[j].speed)
429 return nfp_net_fw_request(pdev, pf, fw_name);
433 * nfp_net_fw_load() - Load the firmware image
434 * @pdev: PCI Device structure
435 * @pf: NFP PF Device structure
436 * @nsp: NFP SP handle
438 * Return: -ERRNO, 0 for no firmware loaded, 1 for firmware loaded
441 nfp_fw_load(struct pci_dev *pdev, struct nfp_pf *pf, struct nfp_nsp *nsp)
443 const struct firmware *fw;
447 interface = nfp_cpp_interface(pf->cpp);
448 if (NFP_CPP_INTERFACE_UNIT_of(interface) != 0) {
449 /* Only Unit 0 should reset or load firmware */
450 dev_info(&pdev->dev, "Firmware will be loaded by partner\n");
454 fw = nfp_net_fw_find(pdev, pf);
456 if (nfp_nsp_has_stored_fw_load(nsp))
457 nfp_nsp_load_stored_fw(nsp);
461 dev_info(&pdev->dev, "Soft-reset, loading FW image\n");
462 err = nfp_nsp_device_soft_reset(nsp);
464 dev_err(&pdev->dev, "Failed to soft reset the NFP: %d\n",
466 goto exit_release_fw;
469 err = nfp_nsp_load_fw(nsp, fw);
471 dev_err(&pdev->dev, "FW loading failed: %d\n", err);
472 goto exit_release_fw;
475 dev_info(&pdev->dev, "Finished loading FW image\n");
478 release_firmware(fw);
480 return err < 0 ? err : 1;
484 nfp_nsp_init_ports(struct pci_dev *pdev, struct nfp_pf *pf,
487 bool needs_reinit = false;
490 pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
494 if (!nfp_nsp_has_mac_reinit(nsp))
497 for (i = 0; i < pf->eth_tbl->count; i++)
498 needs_reinit |= pf->eth_tbl->ports[i].override_changed;
503 if (nfp_nsp_mac_reinit(nsp))
504 dev_warn(&pdev->dev, "MAC reinit failed\n");
506 pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
509 static int nfp_nsp_init(struct pci_dev *pdev, struct nfp_pf *pf)
514 err = nfp_resource_wait(pf->cpp, NFP_RESOURCE_NSP, 30);
518 nsp = nfp_nsp_open(pf->cpp);
521 dev_err(&pdev->dev, "Failed to access the NSP: %d\n", err);
525 err = nfp_nsp_wait(nsp);
529 nfp_nsp_init_ports(pdev, pf, nsp);
531 pf->nspi = __nfp_nsp_identify(nsp);
533 dev_info(&pdev->dev, "BSP: %s\n", pf->nspi->version);
535 err = nfp_fw_load(pdev, pf, nsp);
539 dev_err(&pdev->dev, "Failed to load FW\n");
543 pf->fw_loaded = !!err;
552 static void nfp_fw_unload(struct nfp_pf *pf)
557 nsp = nfp_nsp_open(pf->cpp);
559 nfp_err(pf->cpp, "Reset failed, can't open NSP\n");
563 err = nfp_nsp_device_soft_reset(nsp);
565 dev_warn(&pf->pdev->dev, "Couldn't unload firmware: %d\n", err);
567 dev_info(&pf->pdev->dev, "Firmware safely unloaded\n");
572 static int nfp_pf_find_rtsyms(struct nfp_pf *pf)
577 pf_id = nfp_cppcore_pcie_unit(pf->cpp);
579 /* Optional per-PCI PF mailbox */
580 snprintf(pf_symbol, sizeof(pf_symbol), NFP_MBOX_SYM_NAME, pf_id);
581 pf->mbox = nfp_rtsym_lookup(pf->rtbl, pf_symbol);
582 if (pf->mbox && nfp_rtsym_size(pf->mbox) < NFP_MBOX_SYM_MIN_SIZE) {
583 nfp_err(pf->cpp, "PF mailbox symbol too small: %llu < %d\n",
584 nfp_rtsym_size(pf->mbox), NFP_MBOX_SYM_MIN_SIZE);
591 static int nfp_pci_probe(struct pci_dev *pdev,
592 const struct pci_device_id *pci_id)
594 struct devlink *devlink;
598 if (pdev->vendor == PCI_VENDOR_ID_NETRONOME &&
599 pdev->device == PCI_DEVICE_ID_NETRONOME_NFP6000_VF)
600 dev_warn(&pdev->dev, "Binding NFP VF device to the NFP PF driver, the VF driver is called 'nfp_netvf'\n");
602 err = pci_enable_device(pdev);
606 pci_set_master(pdev);
608 err = dma_set_mask_and_coherent(&pdev->dev,
609 DMA_BIT_MASK(NFP_NET_MAX_DMA_BITS));
611 goto err_pci_disable;
613 err = pci_request_regions(pdev, nfp_driver_name);
615 dev_err(&pdev->dev, "Unable to reserve pci resources.\n");
616 goto err_pci_disable;
619 devlink = devlink_alloc(&nfp_devlink_ops, sizeof(*pf));
622 goto err_rel_regions;
624 pf = devlink_priv(devlink);
625 INIT_LIST_HEAD(&pf->vnics);
626 INIT_LIST_HEAD(&pf->ports);
627 mutex_init(&pf->lock);
628 pci_set_drvdata(pdev, pf);
631 pf->wq = alloc_workqueue("nfp-%s", 0, 2, pci_name(pdev));
634 goto err_pci_priv_unset;
637 pf->cpp = nfp_cpp_from_nfp6000_pcie(pdev);
638 if (IS_ERR_OR_NULL(pf->cpp)) {
639 err = PTR_ERR(pf->cpp);
642 goto err_disable_msix;
645 err = nfp_resource_table_init(pf->cpp);
649 pf->hwinfo = nfp_hwinfo_read(pf->cpp);
651 dev_info(&pdev->dev, "Assembly: %s%s%s-%s CPLD: %s\n",
652 nfp_hwinfo_lookup(pf->hwinfo, "assembly.vendor"),
653 nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno"),
654 nfp_hwinfo_lookup(pf->hwinfo, "assembly.serial"),
655 nfp_hwinfo_lookup(pf->hwinfo, "assembly.revision"),
656 nfp_hwinfo_lookup(pf->hwinfo, "cpld.version"));
658 err = nfp_pf_board_state_wait(pf);
660 goto err_hwinfo_free;
662 err = nfp_nsp_init(pdev, pf);
664 goto err_hwinfo_free;
666 pf->mip = nfp_mip_open(pf->cpp);
667 pf->rtbl = __nfp_rtsym_table_read(pf->cpp, pf->mip);
669 err = nfp_pf_find_rtsyms(pf);
673 pf->dump_flag = NFP_DUMP_NSP_DIAG;
674 pf->dumpspec = nfp_net_dump_load_dumpspec(pf->cpp, pf->rtbl);
676 err = nfp_pcie_sriov_read_nfd_limit(pf);
680 pf->num_vfs = pci_num_vf(pdev);
681 if (pf->num_vfs > pf->limit_vfs) {
683 "Error: %d VFs already enabled, but loaded FW can only support %d\n",
684 pf->num_vfs, pf->limit_vfs);
689 err = nfp_net_pci_probe(pf);
693 err = nfp_hwmon_register(pf);
695 dev_err(&pdev->dev, "Failed to register hwmon info\n");
702 nfp_net_pci_remove(pf);
705 nfp_mip_close(pf->mip);
714 nfp_cpp_free(pf->cpp);
716 destroy_workqueue(pf->wq);
718 pci_set_drvdata(pdev, NULL);
719 mutex_destroy(&pf->lock);
720 devlink_free(devlink);
722 pci_release_regions(pdev);
724 pci_disable_device(pdev);
729 static void __nfp_pci_shutdown(struct pci_dev *pdev, bool unload_fw)
733 pf = pci_get_drvdata(pdev);
737 nfp_hwmon_unregister(pf);
739 nfp_pcie_sriov_disable(pdev);
741 nfp_net_pci_remove(pf);
745 nfp_mip_close(pf->mip);
746 if (unload_fw && pf->fw_loaded)
749 destroy_workqueue(pf->wq);
750 pci_set_drvdata(pdev, NULL);
752 nfp_cpp_free(pf->cpp);
756 mutex_destroy(&pf->lock);
757 devlink_free(priv_to_devlink(pf));
758 pci_release_regions(pdev);
759 pci_disable_device(pdev);
762 static void nfp_pci_remove(struct pci_dev *pdev)
764 __nfp_pci_shutdown(pdev, true);
767 static void nfp_pci_shutdown(struct pci_dev *pdev)
769 __nfp_pci_shutdown(pdev, false);
772 static struct pci_driver nfp_pci_driver = {
773 .name = nfp_driver_name,
774 .id_table = nfp_pci_device_ids,
775 .probe = nfp_pci_probe,
776 .remove = nfp_pci_remove,
777 .shutdown = nfp_pci_shutdown,
778 .sriov_configure = nfp_pcie_sriov_configure,
781 static int __init nfp_main_init(void)
785 pr_info("%s: NFP PCIe Driver, Copyright (C) 2014-2017 Netronome Systems\n",
788 nfp_net_debugfs_create();
790 err = pci_register_driver(&nfp_pci_driver);
792 goto err_destroy_debugfs;
794 err = pci_register_driver(&nfp_netvf_pci_driver);
801 pci_unregister_driver(&nfp_pci_driver);
803 nfp_net_debugfs_destroy();
807 static void __exit nfp_main_exit(void)
809 pci_unregister_driver(&nfp_netvf_pci_driver);
810 pci_unregister_driver(&nfp_pci_driver);
811 nfp_net_debugfs_destroy();
814 module_init(nfp_main_init);
815 module_exit(nfp_main_exit);
819 MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
820 MODULE_LICENSE("GPL");
821 MODULE_DESCRIPTION("The Netronome Flow Processor (NFP) driver.");
822 MODULE_VERSION(UTS_RELEASE);