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 <linux/vmalloc.h>
49 #include <net/devlink.h>
51 #include "nfpcore/nfp.h"
52 #include "nfpcore/nfp_cpp.h"
53 #include "nfpcore/nfp_nffw.h"
54 #include "nfpcore/nfp_nsp.h"
56 #include "nfpcore/nfp6000_pcie.h"
63 static const char nfp_driver_name[] = "nfp";
64 const char nfp_driver_version[] = VERMAGIC_STRING;
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,
71 { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP4000,
72 PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
75 { 0, } /* Required last entry. */
77 MODULE_DEVICE_TABLE(pci, nfp_pci_device_ids);
79 int nfp_pf_rtsym_read_optional(struct nfp_pf *pf, const char *format,
80 unsigned int default_val)
86 snprintf(name, sizeof(name), format, nfp_cppcore_pcie_unit(pf->cpp));
88 val = nfp_rtsym_read_le(pf->rtbl, name, &err);
92 nfp_err(pf->cpp, "Unable to read symbol %s\n", name);
100 nfp_pf_map_rtsym(struct nfp_pf *pf, const char *name, const char *sym_fmt,
101 unsigned int min_size, struct nfp_cpp_area **area)
105 snprintf(pf_symbol, sizeof(pf_symbol), sym_fmt,
106 nfp_cppcore_pcie_unit(pf->cpp));
108 return nfp_rtsym_map(pf->rtbl, pf_symbol, name, min_size, area);
111 /* Callers should hold the devlink instance lock */
112 int nfp_mbox_cmd(struct nfp_pf *pf, u32 cmd, void *in_data, u64 in_length,
113 void *out_data, u64 out_length)
115 unsigned long long addr;
116 unsigned long err_at;
125 cpp_id = NFP_CPP_ISLAND_ID(pf->mbox->target, NFP_CPP_ACTION_RW, 0,
127 addr = pf->mbox->addr;
128 max_data_sz = pf->mbox->size - NFP_MBOX_SYM_MIN_SIZE;
130 /* Check if cmd field is clear */
131 err = nfp_cpp_readl(pf->cpp, cpp_id, addr + NFP_MBOX_CMD, &val);
133 nfp_warn(pf->cpp, "failed to issue command (%u): %u, err: %d\n",
135 return err ?: -EBUSY;
138 in_length = min(in_length, max_data_sz);
139 n = nfp_cpp_write(pf->cpp, cpp_id, addr + NFP_MBOX_DATA,
143 /* Write data_len and wipe reserved */
144 err = nfp_cpp_writeq(pf->cpp, cpp_id, addr + NFP_MBOX_DATA_LEN,
149 /* Read back for ordering */
150 err = nfp_cpp_readl(pf->cpp, cpp_id, addr + NFP_MBOX_DATA_LEN, &val);
154 /* Write cmd and wipe return value */
155 err = nfp_cpp_writeq(pf->cpp, cpp_id, addr + NFP_MBOX_CMD, cmd);
159 err_at = jiffies + 5 * HZ;
161 /* Wait for command to go to 0 (NFP_MBOX_NO_CMD) */
162 err = nfp_cpp_readl(pf->cpp, cpp_id, addr + NFP_MBOX_CMD, &val);
168 if (time_is_before_eq_jiffies(err_at))
174 /* Copy output if any (could be error info, do it before reading ret) */
175 err = nfp_cpp_readl(pf->cpp, cpp_id, addr + NFP_MBOX_DATA_LEN, &val);
179 out_length = min_t(u32, val, min(out_length, max_data_sz));
180 n = nfp_cpp_read(pf->cpp, cpp_id, addr + NFP_MBOX_DATA,
181 out_data, out_length);
185 /* Check if there is an error */
186 err = nfp_cpp_readl(pf->cpp, cpp_id, addr + NFP_MBOX_RET, &val);
195 static bool nfp_board_ready(struct nfp_pf *pf)
201 cp = nfp_hwinfo_lookup(pf->hwinfo, "board.state");
205 err = kstrtol(cp, 0, &state);
212 static int nfp_pf_board_state_wait(struct nfp_pf *pf)
214 const unsigned long wait_until = jiffies + 10 * HZ;
216 while (!nfp_board_ready(pf)) {
217 if (time_is_before_eq_jiffies(wait_until)) {
218 nfp_err(pf->cpp, "NFP board initialization timeout\n");
222 nfp_info(pf->cpp, "waiting for board initialization\n");
223 if (msleep_interruptible(500))
226 /* Refresh cached information */
228 pf->hwinfo = nfp_hwinfo_read(pf->cpp);
234 static int nfp_pcie_sriov_read_nfd_limit(struct nfp_pf *pf)
238 pf->limit_vfs = nfp_rtsym_read_le(pf->rtbl, "nfd_vf_cfg_max_vfs", &err);
240 return pci_sriov_set_totalvfs(pf->pdev, pf->limit_vfs);
243 pci_sriov_set_totalvfs(pf->pdev, 0); /* 0 is unset */
244 /* Allow any setting for backwards compatibility if symbol not found */
248 nfp_warn(pf->cpp, "Warning: VF limit read failed: %d\n", err);
252 static int nfp_pcie_sriov_enable(struct pci_dev *pdev, int num_vfs)
254 #ifdef CONFIG_PCI_IOV
255 struct nfp_pf *pf = pci_get_drvdata(pdev);
258 if (num_vfs > pf->limit_vfs) {
259 nfp_info(pf->cpp, "Firmware limits number of VFs to %u\n",
264 err = pci_enable_sriov(pdev, num_vfs);
266 dev_warn(&pdev->dev, "Failed to enable PCI SR-IOV: %d\n", err);
270 mutex_lock(&pf->lock);
272 err = nfp_app_sriov_enable(pf->app, num_vfs);
275 "App specific PCI SR-IOV configuration failed: %d\n",
277 goto err_sriov_disable;
280 pf->num_vfs = num_vfs;
282 dev_dbg(&pdev->dev, "Created %d VFs.\n", pf->num_vfs);
284 mutex_unlock(&pf->lock);
288 mutex_unlock(&pf->lock);
289 pci_disable_sriov(pdev);
295 static int nfp_pcie_sriov_disable(struct pci_dev *pdev)
297 #ifdef CONFIG_PCI_IOV
298 struct nfp_pf *pf = pci_get_drvdata(pdev);
300 mutex_lock(&pf->lock);
302 /* If the VFs are assigned we cannot shut down SR-IOV without
303 * causing issues, so just leave the hardware available but
306 if (pci_vfs_assigned(pdev)) {
307 dev_warn(&pdev->dev, "Disabling while VFs assigned - VFs will not be deallocated\n");
308 mutex_unlock(&pf->lock);
312 nfp_app_sriov_disable(pf->app);
316 mutex_unlock(&pf->lock);
318 pci_disable_sriov(pdev);
319 dev_dbg(&pdev->dev, "Removed VFs.\n");
324 static int nfp_pcie_sriov_configure(struct pci_dev *pdev, int num_vfs)
327 return nfp_pcie_sriov_disable(pdev);
329 return nfp_pcie_sriov_enable(pdev, num_vfs);
332 static const struct firmware *
333 nfp_net_fw_request(struct pci_dev *pdev, struct nfp_pf *pf, const char *name)
335 const struct firmware *fw = NULL;
338 err = request_firmware_direct(&fw, name, &pdev->dev);
339 nfp_info(pf->cpp, " %s: %s\n",
340 name, err ? "not found" : "found, loading...");
348 * nfp_net_fw_find() - Find the correct firmware image for netdev mode
349 * @pdev: PCI Device structure
350 * @pf: NFP PF Device structure
352 * Return: firmware if found and requested successfully.
354 static const struct firmware *
355 nfp_net_fw_find(struct pci_dev *pdev, struct nfp_pf *pf)
357 struct nfp_eth_table_port *port;
358 const struct firmware *fw;
359 const char *fw_model;
365 nfp_info(pf->cpp, "Looking for firmware file in order of priority:\n");
367 /* First try to find a firmware image specific for this device */
368 interface = nfp_cpp_interface(pf->cpp);
369 nfp_cpp_serial(pf->cpp, &serial);
370 sprintf(fw_name, "netronome/serial-%pMF-%02hhx-%02hhx.nffw",
371 serial, interface >> 8, interface & 0xff);
372 fw = nfp_net_fw_request(pdev, pf, fw_name);
376 /* Then try the PCI name */
377 sprintf(fw_name, "netronome/pci-%s.nffw", pci_name(pdev));
378 fw = nfp_net_fw_request(pdev, pf, fw_name);
382 /* Finally try the card type and media */
384 dev_err(&pdev->dev, "Error: can't identify media config\n");
388 fw_model = nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno");
390 dev_err(&pdev->dev, "Error: can't read part number\n");
394 spc = ARRAY_SIZE(fw_name);
395 spc -= snprintf(fw_name, spc, "netronome/nic_%s", fw_model);
397 for (i = 0; spc > 0 && i < pf->eth_tbl->count; i += j) {
398 port = &pf->eth_tbl->ports[i];
400 while (i + j < pf->eth_tbl->count &&
401 port->speed == port[j].speed)
404 spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc,
405 "_%dx%d", j, port->speed / 1000);
411 spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc, ".nffw");
415 return nfp_net_fw_request(pdev, pf, fw_name);
419 * nfp_net_fw_load() - Load the firmware image
420 * @pdev: PCI Device structure
421 * @pf: NFP PF Device structure
422 * @nsp: NFP SP handle
424 * Return: -ERRNO, 0 for no firmware loaded, 1 for firmware loaded
427 nfp_fw_load(struct pci_dev *pdev, struct nfp_pf *pf, struct nfp_nsp *nsp)
429 const struct firmware *fw;
433 interface = nfp_cpp_interface(pf->cpp);
434 if (NFP_CPP_INTERFACE_UNIT_of(interface) != 0) {
435 /* Only Unit 0 should reset or load firmware */
436 dev_info(&pdev->dev, "Firmware will be loaded by partner\n");
440 fw = nfp_net_fw_find(pdev, pf);
444 dev_info(&pdev->dev, "Soft-reset, loading FW image\n");
445 err = nfp_nsp_device_soft_reset(nsp);
447 dev_err(&pdev->dev, "Failed to soft reset the NFP: %d\n",
449 goto exit_release_fw;
452 err = nfp_nsp_load_fw(nsp, fw);
455 dev_err(&pdev->dev, "FW loading failed: %d\n", err);
456 goto exit_release_fw;
459 dev_info(&pdev->dev, "Finished loading FW image\n");
462 release_firmware(fw);
464 return err < 0 ? err : 1;
468 nfp_nsp_init_ports(struct pci_dev *pdev, struct nfp_pf *pf,
471 bool needs_reinit = false;
474 pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
478 if (!nfp_nsp_has_mac_reinit(nsp))
481 for (i = 0; i < pf->eth_tbl->count; i++)
482 needs_reinit |= pf->eth_tbl->ports[i].override_changed;
487 if (nfp_nsp_mac_reinit(nsp))
488 dev_warn(&pdev->dev, "MAC reinit failed\n");
490 pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
493 static int nfp_nsp_init(struct pci_dev *pdev, struct nfp_pf *pf)
498 err = nfp_resource_wait(pf->cpp, NFP_RESOURCE_NSP, 30);
502 nsp = nfp_nsp_open(pf->cpp);
505 dev_err(&pdev->dev, "Failed to access the NSP: %d\n", err);
509 err = nfp_nsp_wait(nsp);
513 nfp_nsp_init_ports(pdev, pf, nsp);
515 pf->nspi = __nfp_nsp_identify(nsp);
517 dev_info(&pdev->dev, "BSP: %s\n", pf->nspi->version);
519 err = nfp_fw_load(pdev, pf, nsp);
523 dev_err(&pdev->dev, "Failed to load FW\n");
527 pf->fw_loaded = !!err;
536 static void nfp_fw_unload(struct nfp_pf *pf)
541 nsp = nfp_nsp_open(pf->cpp);
543 nfp_err(pf->cpp, "Reset failed, can't open NSP\n");
547 err = nfp_nsp_device_soft_reset(nsp);
549 dev_warn(&pf->pdev->dev, "Couldn't unload firmware: %d\n", err);
551 dev_info(&pf->pdev->dev, "Firmware safely unloaded\n");
556 static int nfp_pf_find_rtsyms(struct nfp_pf *pf)
561 pf_id = nfp_cppcore_pcie_unit(pf->cpp);
563 /* Optional per-PCI PF mailbox */
564 snprintf(pf_symbol, sizeof(pf_symbol), NFP_MBOX_SYM_NAME, pf_id);
565 pf->mbox = nfp_rtsym_lookup(pf->rtbl, pf_symbol);
566 if (pf->mbox && pf->mbox->size < NFP_MBOX_SYM_MIN_SIZE) {
567 nfp_err(pf->cpp, "PF mailbox symbol too small: %llu < %d\n",
568 pf->mbox->size, NFP_MBOX_SYM_MIN_SIZE);
575 static int nfp_pci_probe(struct pci_dev *pdev,
576 const struct pci_device_id *pci_id)
578 struct devlink *devlink;
582 err = pci_enable_device(pdev);
586 pci_set_master(pdev);
588 err = dma_set_mask_and_coherent(&pdev->dev,
589 DMA_BIT_MASK(NFP_NET_MAX_DMA_BITS));
591 goto err_pci_disable;
593 err = pci_request_regions(pdev, nfp_driver_name);
595 dev_err(&pdev->dev, "Unable to reserve pci resources.\n");
596 goto err_pci_disable;
599 devlink = devlink_alloc(&nfp_devlink_ops, sizeof(*pf));
602 goto err_rel_regions;
604 pf = devlink_priv(devlink);
605 INIT_LIST_HEAD(&pf->vnics);
606 INIT_LIST_HEAD(&pf->ports);
607 mutex_init(&pf->lock);
608 pci_set_drvdata(pdev, pf);
611 pf->wq = alloc_workqueue("nfp-%s", 0, 2, pci_name(pdev));
614 goto err_pci_priv_unset;
617 pf->cpp = nfp_cpp_from_nfp6000_pcie(pdev);
618 if (IS_ERR_OR_NULL(pf->cpp)) {
619 err = PTR_ERR(pf->cpp);
622 goto err_disable_msix;
625 err = nfp_resource_table_init(pf->cpp);
629 pf->hwinfo = nfp_hwinfo_read(pf->cpp);
631 dev_info(&pdev->dev, "Assembly: %s%s%s-%s CPLD: %s\n",
632 nfp_hwinfo_lookup(pf->hwinfo, "assembly.vendor"),
633 nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno"),
634 nfp_hwinfo_lookup(pf->hwinfo, "assembly.serial"),
635 nfp_hwinfo_lookup(pf->hwinfo, "assembly.revision"),
636 nfp_hwinfo_lookup(pf->hwinfo, "cpld.version"));
638 err = nfp_pf_board_state_wait(pf);
640 goto err_hwinfo_free;
642 err = nfp_nsp_init(pdev, pf);
644 goto err_hwinfo_free;
646 pf->mip = nfp_mip_open(pf->cpp);
647 pf->rtbl = __nfp_rtsym_table_read(pf->cpp, pf->mip);
649 err = nfp_pf_find_rtsyms(pf);
653 pf->dump_flag = NFP_DUMP_NSP_DIAG;
654 pf->dumpspec = nfp_net_dump_load_dumpspec(pf->cpp, pf->rtbl);
656 err = nfp_pcie_sriov_read_nfd_limit(pf);
660 pf->num_vfs = pci_num_vf(pdev);
661 if (pf->num_vfs > pf->limit_vfs) {
663 "Error: %d VFs already enabled, but loaded FW can only support %d\n",
664 pf->num_vfs, pf->limit_vfs);
669 err = nfp_net_pci_probe(pf);
671 goto err_sriov_unlimit;
673 err = nfp_hwmon_register(pf);
675 dev_err(&pdev->dev, "Failed to register hwmon info\n");
682 nfp_net_pci_remove(pf);
684 pci_sriov_set_totalvfs(pf->pdev, 0);
687 nfp_mip_close(pf->mip);
696 nfp_cpp_free(pf->cpp);
698 destroy_workqueue(pf->wq);
700 pci_set_drvdata(pdev, NULL);
701 mutex_destroy(&pf->lock);
702 devlink_free(devlink);
704 pci_release_regions(pdev);
706 pci_disable_device(pdev);
711 static void nfp_pci_remove(struct pci_dev *pdev)
713 struct nfp_pf *pf = pci_get_drvdata(pdev);
715 nfp_hwmon_unregister(pf);
717 nfp_pcie_sriov_disable(pdev);
718 pci_sriov_set_totalvfs(pf->pdev, 0);
720 nfp_net_pci_remove(pf);
724 nfp_mip_close(pf->mip);
728 destroy_workqueue(pf->wq);
729 pci_set_drvdata(pdev, NULL);
731 nfp_cpp_free(pf->cpp);
735 mutex_destroy(&pf->lock);
736 devlink_free(priv_to_devlink(pf));
737 pci_release_regions(pdev);
738 pci_disable_device(pdev);
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,
749 static int __init nfp_main_init(void)
753 pr_info("%s: NFP PCIe Driver, Copyright (C) 2014-2017 Netronome Systems\n",
756 nfp_net_debugfs_create();
758 err = pci_register_driver(&nfp_pci_driver);
760 goto err_destroy_debugfs;
762 err = pci_register_driver(&nfp_netvf_pci_driver);
769 pci_unregister_driver(&nfp_pci_driver);
771 nfp_net_debugfs_destroy();
775 static void __exit nfp_main_exit(void)
777 pci_unregister_driver(&nfp_netvf_pci_driver);
778 pci_unregister_driver(&nfp_pci_driver);
779 nfp_net_debugfs_destroy();
782 module_init(nfp_main_init);
783 module_exit(nfp_main_exit);
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");
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);