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 = request_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, "netronome/serial-%pMF-%02hhx-%02hhx.nffw",
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, "netronome/pci-%s.nffw", 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, "netronome/nic_%s", 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)
286 spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc,
287 "_%dx%d", j, port->speed / 1000);
293 spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc, ".nffw");
297 return nfp_net_fw_request(pdev, pf, fw_name);
301 * nfp_net_fw_load() - Load the firmware image
302 * @pdev: PCI Device structure
303 * @pf: NFP PF Device structure
304 * @nsp: NFP SP handle
306 * Return: -ERRNO, 0 for no firmware loaded, 1 for firmware loaded
309 nfp_fw_load(struct pci_dev *pdev, struct nfp_pf *pf, struct nfp_nsp *nsp)
311 const struct firmware *fw;
315 interface = nfp_cpp_interface(pf->cpp);
316 if (NFP_CPP_INTERFACE_UNIT_of(interface) != 0) {
317 /* Only Unit 0 should reset or load firmware */
318 dev_info(&pdev->dev, "Firmware will be loaded by partner\n");
322 fw = nfp_net_fw_find(pdev, pf);
326 dev_info(&pdev->dev, "Soft-reset, loading FW image\n");
327 err = nfp_nsp_device_soft_reset(nsp);
329 dev_err(&pdev->dev, "Failed to soft reset the NFP: %d\n",
331 goto exit_release_fw;
334 err = nfp_nsp_load_fw(nsp, fw);
337 dev_err(&pdev->dev, "FW loading failed: %d\n", err);
338 goto exit_release_fw;
341 dev_info(&pdev->dev, "Finished loading FW image\n");
344 release_firmware(fw);
346 return err < 0 ? err : 1;
349 static int nfp_nsp_init(struct pci_dev *pdev, struct nfp_pf *pf)
354 err = nfp_resource_wait(pf->cpp, NFP_RESOURCE_NSP, 30);
358 nsp = nfp_nsp_open(pf->cpp);
361 dev_err(&pdev->dev, "Failed to access the NSP: %d\n", err);
365 err = nfp_nsp_wait(nsp);
369 pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
371 pf->nspi = __nfp_nsp_identify(nsp);
373 dev_info(&pdev->dev, "BSP: %s\n", pf->nspi->version);
375 err = nfp_fw_load(pdev, pf, nsp);
379 dev_err(&pdev->dev, "Failed to load FW\n");
383 pf->fw_loaded = !!err;
392 static void nfp_fw_unload(struct nfp_pf *pf)
397 nsp = nfp_nsp_open(pf->cpp);
399 nfp_err(pf->cpp, "Reset failed, can't open NSP\n");
403 err = nfp_nsp_device_soft_reset(nsp);
405 dev_warn(&pf->pdev->dev, "Couldn't unload firmware: %d\n", err);
407 dev_info(&pf->pdev->dev, "Firmware safely unloaded\n");
412 static int nfp_pci_probe(struct pci_dev *pdev,
413 const struct pci_device_id *pci_id)
415 struct devlink *devlink;
419 err = pci_enable_device(pdev);
423 pci_set_master(pdev);
425 err = dma_set_mask_and_coherent(&pdev->dev,
426 DMA_BIT_MASK(NFP_NET_MAX_DMA_BITS));
428 goto err_pci_disable;
430 err = pci_request_regions(pdev, nfp_driver_name);
432 dev_err(&pdev->dev, "Unable to reserve pci resources.\n");
433 goto err_pci_disable;
436 devlink = devlink_alloc(&nfp_devlink_ops, sizeof(*pf));
439 goto err_rel_regions;
441 pf = devlink_priv(devlink);
442 INIT_LIST_HEAD(&pf->vnics);
443 INIT_LIST_HEAD(&pf->ports);
444 mutex_init(&pf->lock);
445 pci_set_drvdata(pdev, pf);
448 pf->wq = alloc_workqueue("nfp-%s", 0, 2, pci_name(pdev));
451 goto err_pci_priv_unset;
454 pf->cpp = nfp_cpp_from_nfp6000_pcie(pdev);
455 if (IS_ERR_OR_NULL(pf->cpp)) {
456 err = PTR_ERR(pf->cpp);
459 goto err_disable_msix;
462 pf->hwinfo = nfp_hwinfo_read(pf->cpp);
464 dev_info(&pdev->dev, "Assembly: %s%s%s-%s CPLD: %s\n",
465 nfp_hwinfo_lookup(pf->hwinfo, "assembly.vendor"),
466 nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno"),
467 nfp_hwinfo_lookup(pf->hwinfo, "assembly.serial"),
468 nfp_hwinfo_lookup(pf->hwinfo, "assembly.revision"),
469 nfp_hwinfo_lookup(pf->hwinfo, "cpld.version"));
471 err = nfp_pf_board_state_wait(pf);
473 goto err_hwinfo_free;
475 err = devlink_register(devlink, &pdev->dev);
477 goto err_hwinfo_free;
479 err = nfp_nsp_init(pdev, pf);
481 goto err_devlink_unreg;
483 pf->mip = nfp_mip_open(pf->cpp);
484 pf->rtbl = __nfp_rtsym_table_read(pf->cpp, pf->mip);
486 err = nfp_pcie_sriov_read_nfd_limit(pf);
490 pf->num_vfs = pci_num_vf(pdev);
491 if (pf->num_vfs > pf->limit_vfs) {
493 "Error: %d VFs already enabled, but loaded FW can only support %d\n",
494 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);
618 MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_1x40.nffw");
619 MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_4x10.nffw");
620 MODULE_FIRMWARE("netronome/nic_AMDA0096-0001_2x10.nffw");
621 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_2x40.nffw");
622 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_4x10_1x40.nffw");
623 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_8x10.nffw");
624 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x10.nffw");
625 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x25.nffw");
627 MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
628 MODULE_LICENSE("GPL");
629 MODULE_DESCRIPTION("The Netronome Flow Processor (NFP) driver.");