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/pci.h>
45 #include <linux/firmware.h>
46 #include <linux/vermagic.h>
48 #include "nfpcore/nfp.h"
49 #include "nfpcore/nfp_cpp.h"
50 #include "nfpcore/nfp_nffw.h"
51 #include "nfpcore/nfp_nsp.h"
53 #include "nfpcore/nfp6000_pcie.h"
58 static const char nfp_driver_name[] = "nfp";
59 const char nfp_driver_version[] = VERMAGIC_STRING;
61 static const struct pci_device_id nfp_pci_device_ids[] = {
62 { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP6000,
63 PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
66 { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP4000,
67 PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
70 { 0, } /* Required last entry. */
72 MODULE_DEVICE_TABLE(pci, nfp_pci_device_ids);
74 static void nfp_pcie_sriov_read_nfd_limit(struct nfp_pf *pf)
79 pf->limit_vfs = nfp_rtsym_read_le(pf->cpp, "nfd_vf_cfg_max_vfs", &err);
84 /* Allow any setting for backwards compatibility if symbol not found */
86 nfp_warn(pf->cpp, "Warning: VF limit read failed: %d\n", err);
90 static int nfp_pcie_sriov_enable(struct pci_dev *pdev, int num_vfs)
93 struct nfp_pf *pf = pci_get_drvdata(pdev);
96 if (num_vfs > pf->limit_vfs) {
97 nfp_info(pf->cpp, "Firmware limits number of VFs to %u\n",
102 err = pci_enable_sriov(pdev, num_vfs);
104 dev_warn(&pdev->dev, "Failed to enable PCI sriov: %d\n", err);
108 pf->num_vfs = num_vfs;
110 dev_dbg(&pdev->dev, "Created %d VFs.\n", pf->num_vfs);
117 static int nfp_pcie_sriov_disable(struct pci_dev *pdev)
119 #ifdef CONFIG_PCI_IOV
120 struct nfp_pf *pf = pci_get_drvdata(pdev);
122 /* If the VFs are assigned we cannot shut down SR-IOV without
123 * causing issues, so just leave the hardware available but
126 if (pci_vfs_assigned(pdev)) {
127 dev_warn(&pdev->dev, "Disabling while VFs assigned - VFs will not be deallocated\n");
133 pci_disable_sriov(pdev);
134 dev_dbg(&pdev->dev, "Removed VFs.\n");
139 static int nfp_pcie_sriov_configure(struct pci_dev *pdev, int num_vfs)
142 return nfp_pcie_sriov_disable(pdev);
144 return nfp_pcie_sriov_enable(pdev, num_vfs);
148 * nfp_net_fw_find() - Find the correct firmware image for netdev mode
149 * @pdev: PCI Device structure
150 * @pf: NFP PF Device structure
152 * Return: firmware if found and requested successfully.
154 static const struct firmware *
155 nfp_net_fw_find(struct pci_dev *pdev, struct nfp_pf *pf)
157 const struct firmware *fw = NULL;
158 struct nfp_eth_table_port *port;
159 const char *fw_model;
165 dev_err(&pdev->dev, "Error: can't identify media config\n");
169 fw_model = nfp_hwinfo_lookup(pf->cpp, "assembly.partno");
171 dev_err(&pdev->dev, "Error: can't read part number\n");
175 spc = ARRAY_SIZE(fw_name);
176 spc -= snprintf(fw_name, spc, "netronome/nic_%s", fw_model);
178 for (i = 0; spc > 0 && i < pf->eth_tbl->count; i += j) {
179 port = &pf->eth_tbl->ports[i];
181 while (i + j < pf->eth_tbl->count &&
182 port->speed == port[j].speed)
185 spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc,
186 "_%dx%d", j, port->speed / 1000);
192 spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc, ".nffw");
196 err = request_firmware(&fw, fw_name, &pdev->dev);
200 dev_info(&pdev->dev, "Loading FW image: %s\n", fw_name);
206 * nfp_net_fw_load() - Load the firmware image
207 * @pdev: PCI Device structure
208 * @pf: NFP PF Device structure
209 * @nsp: NFP SP handle
211 * Return: -ERRNO, 0 for no firmware loaded, 1 for firmware loaded
214 nfp_fw_load(struct pci_dev *pdev, struct nfp_pf *pf, struct nfp_nsp *nsp)
216 const struct firmware *fw;
220 interface = nfp_cpp_interface(pf->cpp);
221 if (NFP_CPP_INTERFACE_UNIT_of(interface) != 0) {
222 /* Only Unit 0 should reset or load firmware */
223 dev_info(&pdev->dev, "Firmware will be loaded by partner\n");
227 fw = nfp_net_fw_find(pdev, pf);
231 dev_info(&pdev->dev, "Soft-reset, loading FW image\n");
232 err = nfp_nsp_device_soft_reset(nsp);
234 dev_err(&pdev->dev, "Failed to soft reset the NFP: %d\n",
236 goto exit_release_fw;
239 err = nfp_nsp_load_fw(nsp, fw);
242 dev_err(&pdev->dev, "FW loading failed: %d\n", err);
243 goto exit_release_fw;
246 dev_info(&pdev->dev, "Finished loading FW image\n");
249 release_firmware(fw);
251 return err < 0 ? err : 1;
254 static int nfp_nsp_init(struct pci_dev *pdev, struct nfp_pf *pf)
259 nsp = nfp_nsp_open(pf->cpp);
262 dev_err(&pdev->dev, "Failed to access the NSP: %d\n", err);
266 err = nfp_nsp_wait(nsp);
270 pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
272 err = nfp_fw_load(pdev, pf, nsp);
275 dev_err(&pdev->dev, "Failed to load FW\n");
279 pf->fw_loaded = !!err;
288 static void nfp_fw_unload(struct nfp_pf *pf)
293 nsp = nfp_nsp_open(pf->cpp);
295 nfp_err(pf->cpp, "Reset failed, can't open NSP\n");
299 err = nfp_nsp_device_soft_reset(nsp);
301 dev_warn(&pf->pdev->dev, "Couldn't unload firmware: %d\n", err);
303 dev_info(&pf->pdev->dev, "Firmware safely unloaded\n");
308 static int nfp_pci_probe(struct pci_dev *pdev,
309 const struct pci_device_id *pci_id)
314 err = pci_enable_device(pdev);
318 pci_set_master(pdev);
320 err = dma_set_mask_and_coherent(&pdev->dev,
321 DMA_BIT_MASK(NFP_NET_MAX_DMA_BITS));
323 goto err_pci_disable;
325 err = pci_request_regions(pdev, nfp_driver_name);
327 dev_err(&pdev->dev, "Unable to reserve pci resources.\n");
328 goto err_pci_disable;
331 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
334 goto err_rel_regions;
336 INIT_LIST_HEAD(&pf->ports);
337 pci_set_drvdata(pdev, pf);
340 pf->cpp = nfp_cpp_from_nfp6000_pcie(pdev);
341 if (IS_ERR_OR_NULL(pf->cpp)) {
342 err = PTR_ERR(pf->cpp);
345 goto err_disable_msix;
348 dev_info(&pdev->dev, "Assembly: %s%s%s-%s CPLD: %s\n",
349 nfp_hwinfo_lookup(pf->cpp, "assembly.vendor"),
350 nfp_hwinfo_lookup(pf->cpp, "assembly.partno"),
351 nfp_hwinfo_lookup(pf->cpp, "assembly.serial"),
352 nfp_hwinfo_lookup(pf->cpp, "assembly.revision"),
353 nfp_hwinfo_lookup(pf->cpp, "cpld.version"));
355 err = nfp_nsp_init(pdev, pf);
359 nfp_pcie_sriov_read_nfd_limit(pf);
361 err = nfp_net_pci_probe(pf);
372 nfp_cpp_free(pf->cpp);
374 pci_set_drvdata(pdev, NULL);
377 pci_release_regions(pdev);
379 pci_disable_device(pdev);
384 static void nfp_pci_remove(struct pci_dev *pdev)
386 struct nfp_pf *pf = pci_get_drvdata(pdev);
388 nfp_net_pci_remove(pf);
390 nfp_pcie_sriov_disable(pdev);
395 pci_set_drvdata(pdev, NULL);
396 nfp_cpp_free(pf->cpp);
400 pci_release_regions(pdev);
401 pci_disable_device(pdev);
404 static struct pci_driver nfp_pci_driver = {
405 .name = nfp_driver_name,
406 .id_table = nfp_pci_device_ids,
407 .probe = nfp_pci_probe,
408 .remove = nfp_pci_remove,
409 .sriov_configure = nfp_pcie_sriov_configure,
412 static int __init nfp_main_init(void)
416 pr_info("%s: NFP PCIe Driver, Copyright (C) 2014-2017 Netronome Systems\n",
419 nfp_net_debugfs_create();
421 err = pci_register_driver(&nfp_pci_driver);
423 goto err_destroy_debugfs;
425 err = pci_register_driver(&nfp_netvf_pci_driver);
432 pci_unregister_driver(&nfp_pci_driver);
434 nfp_net_debugfs_destroy();
438 static void __exit nfp_main_exit(void)
440 pci_unregister_driver(&nfp_netvf_pci_driver);
441 pci_unregister_driver(&nfp_pci_driver);
442 nfp_net_debugfs_destroy();
445 module_init(nfp_main_init);
446 module_exit(nfp_main_exit);
448 MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_1x40.nffw");
449 MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_4x10.nffw");
450 MODULE_FIRMWARE("netronome/nic_AMDA0096-0001_2x10.nffw");
451 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_2x40.nffw");
452 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_4x10_1x40.nffw");
453 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_8x10.nffw");
454 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x10.nffw");
455 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x25.nffw");
457 MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
458 MODULE_LICENSE("GPL");
459 MODULE_DESCRIPTION("The Netronome Flow Processor (NFP) driver.");