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_nsp_eth.h"
52 #include "nfpcore/nfp6000_pcie.h"
57 static const char nfp_driver_name[] = "nfp";
58 const char nfp_driver_version[] = VERMAGIC_STRING;
60 static const struct pci_device_id nfp_pci_device_ids[] = {
61 { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP6000,
62 PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
65 { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP4000,
66 PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
69 { 0, } /* Required last entry. */
71 MODULE_DEVICE_TABLE(pci, nfp_pci_device_ids);
73 static int nfp_pcie_sriov_enable(struct pci_dev *pdev, int num_vfs)
76 struct nfp_pf *pf = pci_get_drvdata(pdev);
79 err = pci_enable_sriov(pdev, num_vfs);
81 dev_warn(&pdev->dev, "Failed to enable PCI sriov: %d\n", err);
85 pf->num_vfs = num_vfs;
87 dev_dbg(&pdev->dev, "Created %d VFs.\n", pf->num_vfs);
94 static int nfp_pcie_sriov_disable(struct pci_dev *pdev)
97 struct nfp_pf *pf = pci_get_drvdata(pdev);
99 /* If the VFs are assigned we cannot shut down SR-IOV without
100 * causing issues, so just leave the hardware available but
103 if (pci_vfs_assigned(pdev)) {
104 dev_warn(&pdev->dev, "Disabling while VFs assigned - VFs will not be deallocated\n");
110 pci_disable_sriov(pdev);
111 dev_dbg(&pdev->dev, "Removed VFs.\n");
116 static int nfp_pcie_sriov_configure(struct pci_dev *pdev, int num_vfs)
119 return nfp_pcie_sriov_disable(pdev);
121 return nfp_pcie_sriov_enable(pdev, num_vfs);
125 * nfp_net_fw_find() - Find the correct firmware image for netdev mode
126 * @pdev: PCI Device structure
127 * @pf: NFP PF Device structure
129 * Return: firmware if found and requested successfully.
131 static const struct firmware *
132 nfp_net_fw_find(struct pci_dev *pdev, struct nfp_pf *pf)
134 const struct firmware *fw = NULL;
135 struct nfp_eth_table_port *port;
136 const char *fw_model;
142 dev_err(&pdev->dev, "Error: can't identify media config\n");
146 fw_model = nfp_hwinfo_lookup(pf->cpp, "assembly.partno");
148 dev_err(&pdev->dev, "Error: can't read part number\n");
152 spc = ARRAY_SIZE(fw_name);
153 spc -= snprintf(fw_name, spc, "netronome/nic_%s", fw_model);
155 for (i = 0; spc > 0 && i < pf->eth_tbl->count; i += j) {
156 port = &pf->eth_tbl->ports[i];
158 while (i + j < pf->eth_tbl->count &&
159 port->speed == port[j].speed)
162 spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc,
163 "_%dx%d", j, port->speed / 1000);
169 spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc, ".nffw");
173 err = request_firmware(&fw, fw_name, &pdev->dev);
177 dev_info(&pdev->dev, "Loading FW image: %s\n", fw_name);
183 * nfp_net_fw_load() - Load the firmware image
184 * @pdev: PCI Device structure
185 * @pf: NFP PF Device structure
186 * @nsp: NFP SP handle
188 * Return: -ERRNO, 0 for no firmware loaded, 1 for firmware loaded
191 nfp_fw_load(struct pci_dev *pdev, struct nfp_pf *pf, struct nfp_nsp *nsp)
193 const struct firmware *fw;
197 interface = nfp_cpp_interface(pf->cpp);
198 if (NFP_CPP_INTERFACE_UNIT_of(interface) != 0) {
199 /* Only Unit 0 should reset or load firmware */
200 dev_info(&pdev->dev, "Firmware will be loaded by partner\n");
204 fw = nfp_net_fw_find(pdev, pf);
208 dev_info(&pdev->dev, "Soft-reset, loading FW image\n");
209 err = nfp_nsp_device_soft_reset(nsp);
211 dev_err(&pdev->dev, "Failed to soft reset the NFP: %d\n",
213 goto exit_release_fw;
216 err = nfp_nsp_load_fw(nsp, fw);
219 dev_err(&pdev->dev, "FW loading failed: %d\n", err);
220 goto exit_release_fw;
223 dev_info(&pdev->dev, "Finished loading FW image\n");
226 release_firmware(fw);
228 return err < 0 ? err : 1;
231 static int nfp_nsp_init(struct pci_dev *pdev, struct nfp_pf *pf)
236 nsp = nfp_nsp_open(pf->cpp);
239 dev_err(&pdev->dev, "Failed to access the NSP: %d\n", err);
243 err = nfp_nsp_wait(nsp);
247 pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
249 err = nfp_fw_load(pdev, pf, nsp);
252 dev_err(&pdev->dev, "Failed to load FW\n");
256 pf->fw_loaded = !!err;
265 static void nfp_fw_unload(struct nfp_pf *pf)
270 nsp = nfp_nsp_open(pf->cpp);
272 nfp_err(pf->cpp, "Reset failed, can't open NSP\n");
276 err = nfp_nsp_device_soft_reset(nsp);
278 dev_warn(&pf->pdev->dev, "Couldn't unload firmware: %d\n", err);
280 dev_info(&pf->pdev->dev, "Firmware safely unloaded\n");
285 static int nfp_pci_probe(struct pci_dev *pdev,
286 const struct pci_device_id *pci_id)
291 err = pci_enable_device(pdev);
295 pci_set_master(pdev);
297 err = dma_set_mask_and_coherent(&pdev->dev,
298 DMA_BIT_MASK(NFP_NET_MAX_DMA_BITS));
300 goto err_pci_disable;
302 err = pci_request_regions(pdev, nfp_driver_name);
304 dev_err(&pdev->dev, "Unable to reserve pci resources.\n");
305 goto err_pci_disable;
308 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
311 goto err_rel_regions;
313 INIT_LIST_HEAD(&pf->ports);
314 pci_set_drvdata(pdev, pf);
317 pf->cpp = nfp_cpp_from_nfp6000_pcie(pdev);
318 if (IS_ERR_OR_NULL(pf->cpp)) {
319 err = PTR_ERR(pf->cpp);
322 goto err_disable_msix;
325 err = nfp_nsp_init(pdev, pf);
329 err = nfp_net_pci_probe(pf);
340 nfp_cpp_free(pf->cpp);
342 pci_set_drvdata(pdev, NULL);
345 pci_release_regions(pdev);
347 pci_disable_device(pdev);
352 static void nfp_pci_remove(struct pci_dev *pdev)
354 struct nfp_pf *pf = pci_get_drvdata(pdev);
356 if (!list_empty(&pf->ports))
357 nfp_net_pci_remove(pf);
359 nfp_pcie_sriov_disable(pdev);
364 pci_set_drvdata(pdev, NULL);
365 nfp_cpp_free(pf->cpp);
369 pci_release_regions(pdev);
370 pci_disable_device(pdev);
373 static struct pci_driver nfp_pci_driver = {
374 .name = nfp_driver_name,
375 .id_table = nfp_pci_device_ids,
376 .probe = nfp_pci_probe,
377 .remove = nfp_pci_remove,
378 .sriov_configure = nfp_pcie_sriov_configure,
381 static int __init nfp_main_init(void)
385 pr_info("%s: NFP PCIe Driver, Copyright (C) 2014-2017 Netronome Systems\n",
388 nfp_net_debugfs_create();
390 err = pci_register_driver(&nfp_pci_driver);
392 goto err_destroy_debugfs;
394 err = pci_register_driver(&nfp_netvf_pci_driver);
401 pci_unregister_driver(&nfp_pci_driver);
403 nfp_net_debugfs_destroy();
407 static void __exit nfp_main_exit(void)
409 pci_unregister_driver(&nfp_netvf_pci_driver);
410 pci_unregister_driver(&nfp_pci_driver);
411 nfp_net_debugfs_destroy();
414 module_init(nfp_main_init);
415 module_exit(nfp_main_exit);
417 MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_1x40.nffw");
418 MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_4x10.nffw");
419 MODULE_FIRMWARE("netronome/nic_AMDA0096-0001_2x10.nffw");
420 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_2x40.nffw");
421 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_4x10_1x40.nffw");
422 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_8x10.nffw");
423 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x10.nffw");
424 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x25.nffw");
426 MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
427 MODULE_LICENSE("GPL");
428 MODULE_DESCRIPTION("The Netronome Flow Processor (NFP) driver.");