Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
0002 /* Copyright (C) 2015-2018 Netronome Systems, Inc. */
0003 
0004 /*
0005  * nfp_main.c
0006  * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
0007  *          Alejandro Lucero <alejandro.lucero@netronome.com>
0008  *          Jason McMullan <jason.mcmullan@netronome.com>
0009  *          Rolf Neugebauer <rolf.neugebauer@netronome.com>
0010  */
0011 
0012 #include <linux/kernel.h>
0013 #include <linux/module.h>
0014 #include <linux/mutex.h>
0015 #include <linux/pci.h>
0016 #include <linux/firmware.h>
0017 #include <linux/vmalloc.h>
0018 #include <net/devlink.h>
0019 
0020 #include "nfpcore/nfp.h"
0021 #include "nfpcore/nfp_cpp.h"
0022 #include "nfpcore/nfp_dev.h"
0023 #include "nfpcore/nfp_nffw.h"
0024 #include "nfpcore/nfp_nsp.h"
0025 
0026 #include "nfpcore/nfp6000_pcie.h"
0027 
0028 #include "nfp_abi.h"
0029 #include "nfp_app.h"
0030 #include "nfp_main.h"
0031 #include "nfp_net.h"
0032 
0033 static const char nfp_driver_name[] = "nfp";
0034 
0035 static const struct pci_device_id nfp_pci_device_ids[] = {
0036     { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NFP3800,
0037       PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
0038       PCI_ANY_ID, 0, NFP_DEV_NFP3800,
0039     },
0040     { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NFP4000,
0041       PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
0042       PCI_ANY_ID, 0, NFP_DEV_NFP6000,
0043     },
0044     { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NFP5000,
0045       PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
0046       PCI_ANY_ID, 0, NFP_DEV_NFP6000,
0047     },
0048     { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NFP6000,
0049       PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
0050       PCI_ANY_ID, 0, NFP_DEV_NFP6000,
0051     },
0052     { PCI_VENDOR_ID_CORIGINE, PCI_DEVICE_ID_NFP3800,
0053       PCI_VENDOR_ID_CORIGINE, PCI_ANY_ID,
0054       PCI_ANY_ID, 0, NFP_DEV_NFP3800,
0055     },
0056     { PCI_VENDOR_ID_CORIGINE, PCI_DEVICE_ID_NFP4000,
0057       PCI_VENDOR_ID_CORIGINE, PCI_ANY_ID,
0058       PCI_ANY_ID, 0, NFP_DEV_NFP6000,
0059     },
0060     { PCI_VENDOR_ID_CORIGINE, PCI_DEVICE_ID_NFP5000,
0061       PCI_VENDOR_ID_CORIGINE, PCI_ANY_ID,
0062       PCI_ANY_ID, 0, NFP_DEV_NFP6000,
0063     },
0064     { PCI_VENDOR_ID_CORIGINE, PCI_DEVICE_ID_NFP6000,
0065       PCI_VENDOR_ID_CORIGINE, PCI_ANY_ID,
0066       PCI_ANY_ID, 0, NFP_DEV_NFP6000,
0067     },
0068     { 0, } /* Required last entry. */
0069 };
0070 MODULE_DEVICE_TABLE(pci, nfp_pci_device_ids);
0071 
0072 int nfp_pf_rtsym_read_optional(struct nfp_pf *pf, const char *format,
0073                    unsigned int default_val)
0074 {
0075     char name[256];
0076     int err = 0;
0077     u64 val;
0078 
0079     snprintf(name, sizeof(name), format, nfp_cppcore_pcie_unit(pf->cpp));
0080 
0081     val = nfp_rtsym_read_le(pf->rtbl, name, &err);
0082     if (err) {
0083         if (err == -ENOENT)
0084             return default_val;
0085         nfp_err(pf->cpp, "Unable to read symbol %s\n", name);
0086         return err;
0087     }
0088 
0089     return val;
0090 }
0091 
0092 u8 __iomem *
0093 nfp_pf_map_rtsym(struct nfp_pf *pf, const char *name, const char *sym_fmt,
0094          unsigned int min_size, struct nfp_cpp_area **area)
0095 {
0096     char pf_symbol[256];
0097 
0098     snprintf(pf_symbol, sizeof(pf_symbol), sym_fmt,
0099          nfp_cppcore_pcie_unit(pf->cpp));
0100 
0101     return nfp_rtsym_map(pf->rtbl, pf_symbol, name, min_size, area);
0102 }
0103 
0104 /* Callers should hold the devlink instance lock */
0105 int nfp_mbox_cmd(struct nfp_pf *pf, u32 cmd, void *in_data, u64 in_length,
0106          void *out_data, u64 out_length)
0107 {
0108     unsigned long err_at;
0109     u64 max_data_sz;
0110     u32 val = 0;
0111     int n, err;
0112 
0113     if (!pf->mbox)
0114         return -EOPNOTSUPP;
0115 
0116     max_data_sz = nfp_rtsym_size(pf->mbox) - NFP_MBOX_SYM_MIN_SIZE;
0117 
0118     /* Check if cmd field is clear */
0119     err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_CMD, &val);
0120     if (err || val) {
0121         nfp_warn(pf->cpp, "failed to issue command (%u): %u, err: %d\n",
0122              cmd, val, err);
0123         return err ?: -EBUSY;
0124     }
0125 
0126     in_length = min(in_length, max_data_sz);
0127     n = nfp_rtsym_write(pf->cpp, pf->mbox, NFP_MBOX_DATA, in_data,
0128                 in_length);
0129     if (n != in_length)
0130         return -EIO;
0131     /* Write data_len and wipe reserved */
0132     err = nfp_rtsym_writeq(pf->cpp, pf->mbox, NFP_MBOX_DATA_LEN, in_length);
0133     if (err)
0134         return err;
0135 
0136     /* Read back for ordering */
0137     err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_DATA_LEN, &val);
0138     if (err)
0139         return err;
0140 
0141     /* Write cmd and wipe return value */
0142     err = nfp_rtsym_writeq(pf->cpp, pf->mbox, NFP_MBOX_CMD, cmd);
0143     if (err)
0144         return err;
0145 
0146     err_at = jiffies + 5 * HZ;
0147     while (true) {
0148         /* Wait for command to go to 0 (NFP_MBOX_NO_CMD) */
0149         err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_CMD, &val);
0150         if (err)
0151             return err;
0152         if (!val)
0153             break;
0154 
0155         if (time_is_before_eq_jiffies(err_at))
0156             return -ETIMEDOUT;
0157 
0158         msleep(5);
0159     }
0160 
0161     /* Copy output if any (could be error info, do it before reading ret) */
0162     err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_DATA_LEN, &val);
0163     if (err)
0164         return err;
0165 
0166     out_length = min_t(u32, val, min(out_length, max_data_sz));
0167     n = nfp_rtsym_read(pf->cpp, pf->mbox, NFP_MBOX_DATA,
0168                out_data, out_length);
0169     if (n != out_length)
0170         return -EIO;
0171 
0172     /* Check if there is an error */
0173     err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_RET, &val);
0174     if (err)
0175         return err;
0176     if (val)
0177         return -val;
0178 
0179     return out_length;
0180 }
0181 
0182 static bool nfp_board_ready(struct nfp_pf *pf)
0183 {
0184     const char *cp;
0185     long state;
0186     int err;
0187 
0188     cp = nfp_hwinfo_lookup(pf->hwinfo, "board.state");
0189     if (!cp)
0190         return false;
0191 
0192     err = kstrtol(cp, 0, &state);
0193     if (err < 0)
0194         return false;
0195 
0196     return state == 15;
0197 }
0198 
0199 static int nfp_pf_board_state_wait(struct nfp_pf *pf)
0200 {
0201     const unsigned long wait_until = jiffies + 10 * HZ;
0202 
0203     while (!nfp_board_ready(pf)) {
0204         if (time_is_before_eq_jiffies(wait_until)) {
0205             nfp_err(pf->cpp, "NFP board initialization timeout\n");
0206             return -EINVAL;
0207         }
0208 
0209         nfp_info(pf->cpp, "waiting for board initialization\n");
0210         if (msleep_interruptible(500))
0211             return -ERESTARTSYS;
0212 
0213         /* Refresh cached information */
0214         kfree(pf->hwinfo);
0215         pf->hwinfo = nfp_hwinfo_read(pf->cpp);
0216     }
0217 
0218     return 0;
0219 }
0220 
0221 static int nfp_pcie_sriov_read_nfd_limit(struct nfp_pf *pf)
0222 {
0223     int err;
0224 
0225     pf->limit_vfs = nfp_rtsym_read_le(pf->rtbl, "nfd_vf_cfg_max_vfs", &err);
0226     if (err) {
0227         /* For backwards compatibility if symbol not found allow all */
0228         pf->limit_vfs = ~0;
0229         if (err == -ENOENT)
0230             return 0;
0231 
0232         nfp_warn(pf->cpp, "Warning: VF limit read failed: %d\n", err);
0233         return err;
0234     }
0235 
0236     err = pci_sriov_set_totalvfs(pf->pdev, pf->limit_vfs);
0237     if (err)
0238         nfp_warn(pf->cpp, "Failed to set VF count in sysfs: %d\n", err);
0239     return 0;
0240 }
0241 
0242 static int nfp_pcie_sriov_enable(struct pci_dev *pdev, int num_vfs)
0243 {
0244 #ifdef CONFIG_PCI_IOV
0245     struct nfp_pf *pf = pci_get_drvdata(pdev);
0246     struct devlink *devlink;
0247     int err;
0248 
0249     if (num_vfs > pf->limit_vfs) {
0250         nfp_info(pf->cpp, "Firmware limits number of VFs to %u\n",
0251              pf->limit_vfs);
0252         return -EINVAL;
0253     }
0254 
0255     err = pci_enable_sriov(pdev, num_vfs);
0256     if (err) {
0257         dev_warn(&pdev->dev, "Failed to enable PCI SR-IOV: %d\n", err);
0258         return err;
0259     }
0260 
0261     devlink = priv_to_devlink(pf);
0262     devl_lock(devlink);
0263 
0264     err = nfp_app_sriov_enable(pf->app, num_vfs);
0265     if (err) {
0266         dev_warn(&pdev->dev,
0267              "App specific PCI SR-IOV configuration failed: %d\n",
0268              err);
0269         goto err_sriov_disable;
0270     }
0271 
0272     pf->num_vfs = num_vfs;
0273 
0274     dev_dbg(&pdev->dev, "Created %d VFs.\n", pf->num_vfs);
0275 
0276     devl_unlock(devlink);
0277     return num_vfs;
0278 
0279 err_sriov_disable:
0280     devl_unlock(devlink);
0281     pci_disable_sriov(pdev);
0282     return err;
0283 #endif
0284     return 0;
0285 }
0286 
0287 static int nfp_pcie_sriov_disable(struct pci_dev *pdev)
0288 {
0289 #ifdef CONFIG_PCI_IOV
0290     struct nfp_pf *pf = pci_get_drvdata(pdev);
0291     struct devlink *devlink;
0292 
0293     devlink = priv_to_devlink(pf);
0294     devl_lock(devlink);
0295 
0296     /* If the VFs are assigned we cannot shut down SR-IOV without
0297      * causing issues, so just leave the hardware available but
0298      * disabled
0299      */
0300     if (pci_vfs_assigned(pdev)) {
0301         dev_warn(&pdev->dev, "Disabling while VFs assigned - VFs will not be deallocated\n");
0302         devl_unlock(devlink);
0303         return -EPERM;
0304     }
0305 
0306     nfp_app_sriov_disable(pf->app);
0307 
0308     pf->num_vfs = 0;
0309 
0310     devl_unlock(devlink);
0311 
0312     pci_disable_sriov(pdev);
0313     dev_dbg(&pdev->dev, "Removed VFs.\n");
0314 #endif
0315     return 0;
0316 }
0317 
0318 static int nfp_pcie_sriov_configure(struct pci_dev *pdev, int num_vfs)
0319 {
0320     if (!pci_get_drvdata(pdev))
0321         return -ENOENT;
0322 
0323     if (num_vfs == 0)
0324         return nfp_pcie_sriov_disable(pdev);
0325     else
0326         return nfp_pcie_sriov_enable(pdev, num_vfs);
0327 }
0328 
0329 int nfp_flash_update_common(struct nfp_pf *pf, const struct firmware *fw,
0330                 struct netlink_ext_ack *extack)
0331 {
0332     struct device *dev = &pf->pdev->dev;
0333     struct nfp_nsp *nsp;
0334     int err;
0335 
0336     nsp = nfp_nsp_open(pf->cpp);
0337     if (IS_ERR(nsp)) {
0338         err = PTR_ERR(nsp);
0339         if (extack)
0340             NL_SET_ERR_MSG_MOD(extack, "can't access NSP");
0341         else
0342             dev_err(dev, "Failed to access the NSP: %d\n", err);
0343         return err;
0344     }
0345 
0346     err = nfp_nsp_write_flash(nsp, fw);
0347     if (err < 0)
0348         goto exit_close_nsp;
0349     dev_info(dev, "Finished writing flash image\n");
0350     err = 0;
0351 
0352 exit_close_nsp:
0353     nfp_nsp_close(nsp);
0354     return err;
0355 }
0356 
0357 static const struct firmware *
0358 nfp_net_fw_request(struct pci_dev *pdev, struct nfp_pf *pf, const char *name)
0359 {
0360     const struct firmware *fw = NULL;
0361     int err;
0362 
0363     err = request_firmware_direct(&fw, name, &pdev->dev);
0364     nfp_info(pf->cpp, "  %s: %s\n",
0365          name, err ? "not found" : "found");
0366     if (err)
0367         return NULL;
0368 
0369     return fw;
0370 }
0371 
0372 /**
0373  * nfp_net_fw_find() - Find the correct firmware image for netdev mode
0374  * @pdev:   PCI Device structure
0375  * @pf:     NFP PF Device structure
0376  *
0377  * Return: firmware if found and requested successfully.
0378  */
0379 static const struct firmware *
0380 nfp_net_fw_find(struct pci_dev *pdev, struct nfp_pf *pf)
0381 {
0382     struct nfp_eth_table_port *port;
0383     const struct firmware *fw;
0384     const char *fw_model;
0385     char fw_name[256];
0386     const u8 *serial;
0387     u16 interface;
0388     int spc, i, j;
0389 
0390     nfp_info(pf->cpp, "Looking for firmware file in order of priority:\n");
0391 
0392     /* First try to find a firmware image specific for this device */
0393     interface = nfp_cpp_interface(pf->cpp);
0394     nfp_cpp_serial(pf->cpp, &serial);
0395     sprintf(fw_name, "netronome/serial-%pMF-%02x-%02x.nffw",
0396         serial, interface >> 8, interface & 0xff);
0397     fw = nfp_net_fw_request(pdev, pf, fw_name);
0398     if (fw)
0399         return fw;
0400 
0401     /* Then try the PCI name */
0402     sprintf(fw_name, "netronome/pci-%s.nffw", pci_name(pdev));
0403     fw = nfp_net_fw_request(pdev, pf, fw_name);
0404     if (fw)
0405         return fw;
0406 
0407     /* Finally try the card type and media */
0408     if (!pf->eth_tbl) {
0409         dev_err(&pdev->dev, "Error: can't identify media config\n");
0410         return NULL;
0411     }
0412 
0413     fw_model = nfp_hwinfo_lookup(pf->hwinfo, "nffw.partno");
0414     if (!fw_model)
0415         fw_model = nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno");
0416     if (!fw_model) {
0417         dev_err(&pdev->dev, "Error: can't read part number\n");
0418         return NULL;
0419     }
0420 
0421     spc = ARRAY_SIZE(fw_name);
0422     spc -= snprintf(fw_name, spc, "netronome/nic_%s", fw_model);
0423 
0424     for (i = 0; spc > 0 && i < pf->eth_tbl->count; i += j) {
0425         port = &pf->eth_tbl->ports[i];
0426         j = 1;
0427         while (i + j < pf->eth_tbl->count &&
0428                port->speed == port[j].speed)
0429             j++;
0430 
0431         spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc,
0432                 "_%dx%d", j, port->speed / 1000);
0433     }
0434 
0435     if (spc <= 0)
0436         return NULL;
0437 
0438     spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc, ".nffw");
0439     if (spc <= 0)
0440         return NULL;
0441 
0442     return nfp_net_fw_request(pdev, pf, fw_name);
0443 }
0444 
0445 static int
0446 nfp_get_fw_policy_value(struct pci_dev *pdev, struct nfp_nsp *nsp,
0447             const char *key, const char *default_val, int max_val,
0448             int *value)
0449 {
0450     char hwinfo[64];
0451     long hi_val;
0452     int err;
0453 
0454     snprintf(hwinfo, sizeof(hwinfo), key);
0455     err = nfp_nsp_hwinfo_lookup_optional(nsp, hwinfo, sizeof(hwinfo),
0456                          default_val);
0457     if (err)
0458         return err;
0459 
0460     err = kstrtol(hwinfo, 0, &hi_val);
0461     if (err || hi_val < 0 || hi_val > max_val) {
0462         dev_warn(&pdev->dev,
0463              "Invalid value '%s' from '%s', ignoring\n",
0464              hwinfo, key);
0465         err = kstrtol(default_val, 0, &hi_val);
0466     }
0467 
0468     *value = hi_val;
0469     return err;
0470 }
0471 
0472 /**
0473  * nfp_fw_load() - Load the firmware image
0474  * @pdev:       PCI Device structure
0475  * @pf:     NFP PF Device structure
0476  * @nsp:    NFP SP handle
0477  *
0478  * Return: -ERRNO, 0 for no firmware loaded, 1 for firmware loaded
0479  */
0480 static int
0481 nfp_fw_load(struct pci_dev *pdev, struct nfp_pf *pf, struct nfp_nsp *nsp)
0482 {
0483     bool do_reset, fw_loaded = false;
0484     const struct firmware *fw = NULL;
0485     int err, reset, policy, ifcs = 0;
0486     char *token, *ptr;
0487     char hwinfo[64];
0488     u16 interface;
0489 
0490     snprintf(hwinfo, sizeof(hwinfo), "abi_drv_load_ifc");
0491     err = nfp_nsp_hwinfo_lookup_optional(nsp, hwinfo, sizeof(hwinfo),
0492                          NFP_NSP_DRV_LOAD_IFC_DEFAULT);
0493     if (err)
0494         return err;
0495 
0496     interface = nfp_cpp_interface(pf->cpp);
0497     ptr = hwinfo;
0498     while ((token = strsep(&ptr, ","))) {
0499         unsigned long interface_hi;
0500 
0501         err = kstrtoul(token, 0, &interface_hi);
0502         if (err) {
0503             dev_err(&pdev->dev,
0504                 "Failed to parse interface '%s': %d\n",
0505                 token, err);
0506             return err;
0507         }
0508 
0509         ifcs++;
0510         if (interface == interface_hi)
0511             break;
0512     }
0513 
0514     if (!token) {
0515         dev_info(&pdev->dev, "Firmware will be loaded by partner\n");
0516         return 0;
0517     }
0518 
0519     err = nfp_get_fw_policy_value(pdev, nsp, "abi_drv_reset",
0520                       NFP_NSP_DRV_RESET_DEFAULT,
0521                       NFP_NSP_DRV_RESET_NEVER, &reset);
0522     if (err)
0523         return err;
0524 
0525     err = nfp_get_fw_policy_value(pdev, nsp, "app_fw_from_flash",
0526                       NFP_NSP_APP_FW_LOAD_DEFAULT,
0527                       NFP_NSP_APP_FW_LOAD_PREF, &policy);
0528     if (err)
0529         return err;
0530 
0531     fw = nfp_net_fw_find(pdev, pf);
0532     do_reset = reset == NFP_NSP_DRV_RESET_ALWAYS ||
0533            (fw && reset == NFP_NSP_DRV_RESET_DISK);
0534 
0535     if (do_reset) {
0536         dev_info(&pdev->dev, "Soft-resetting the NFP\n");
0537         err = nfp_nsp_device_soft_reset(nsp);
0538         if (err < 0) {
0539             dev_err(&pdev->dev,
0540                 "Failed to soft reset the NFP: %d\n", err);
0541             goto exit_release_fw;
0542         }
0543     }
0544 
0545     if (fw && policy != NFP_NSP_APP_FW_LOAD_FLASH) {
0546         if (nfp_nsp_has_fw_loaded(nsp) && nfp_nsp_fw_loaded(nsp))
0547             goto exit_release_fw;
0548 
0549         err = nfp_nsp_load_fw(nsp, fw);
0550         if (err < 0) {
0551             dev_err(&pdev->dev, "FW loading failed: %d\n",
0552                 err);
0553             goto exit_release_fw;
0554         }
0555         dev_info(&pdev->dev, "Finished loading FW image\n");
0556         fw_loaded = true;
0557     } else if (policy != NFP_NSP_APP_FW_LOAD_DISK &&
0558            nfp_nsp_has_stored_fw_load(nsp)) {
0559 
0560         /* Don't propagate this error to stick with legacy driver
0561          * behavior, failure will be detected later during init.
0562          */
0563         if (!nfp_nsp_load_stored_fw(nsp))
0564             dev_info(&pdev->dev, "Finished loading stored FW image\n");
0565 
0566         /* Don't flag the fw_loaded in this case since other devices
0567          * may reuse the firmware when configured this way
0568          */
0569     } else {
0570         dev_warn(&pdev->dev, "Didn't load firmware, please update flash or reconfigure card\n");
0571     }
0572 
0573 exit_release_fw:
0574     release_firmware(fw);
0575 
0576     /* We don't want to unload firmware when other devices may still be
0577      * dependent on it, which could be the case if there are multiple
0578      * devices that could load firmware.
0579      */
0580     if (fw_loaded && ifcs == 1)
0581         pf->unload_fw_on_remove = true;
0582 
0583     return err < 0 ? err : fw_loaded;
0584 }
0585 
0586 static void
0587 nfp_nsp_init_ports(struct pci_dev *pdev, struct nfp_pf *pf,
0588            struct nfp_nsp *nsp)
0589 {
0590     bool needs_reinit = false;
0591     int i;
0592 
0593     pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
0594     if (!pf->eth_tbl)
0595         return;
0596 
0597     if (!nfp_nsp_has_mac_reinit(nsp))
0598         return;
0599 
0600     for (i = 0; i < pf->eth_tbl->count; i++)
0601         needs_reinit |= pf->eth_tbl->ports[i].override_changed;
0602     if (!needs_reinit)
0603         return;
0604 
0605     kfree(pf->eth_tbl);
0606     if (nfp_nsp_mac_reinit(nsp))
0607         dev_warn(&pdev->dev, "MAC reinit failed\n");
0608 
0609     pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
0610 }
0611 
0612 static int nfp_nsp_init(struct pci_dev *pdev, struct nfp_pf *pf)
0613 {
0614     struct nfp_nsp *nsp;
0615     int err;
0616 
0617     err = nfp_resource_wait(pf->cpp, NFP_RESOURCE_NSP, 30);
0618     if (err)
0619         return err;
0620 
0621     nsp = nfp_nsp_open(pf->cpp);
0622     if (IS_ERR(nsp)) {
0623         err = PTR_ERR(nsp);
0624         dev_err(&pdev->dev, "Failed to access the NSP: %d\n", err);
0625         return err;
0626     }
0627 
0628     err = nfp_nsp_wait(nsp);
0629     if (err < 0)
0630         goto exit_close_nsp;
0631 
0632     nfp_nsp_init_ports(pdev, pf, nsp);
0633 
0634     pf->nspi = __nfp_nsp_identify(nsp);
0635     if (pf->nspi)
0636         dev_info(&pdev->dev, "BSP: %s\n", pf->nspi->version);
0637 
0638     err = nfp_fw_load(pdev, pf, nsp);
0639     if (err < 0) {
0640         kfree(pf->nspi);
0641         kfree(pf->eth_tbl);
0642         dev_err(&pdev->dev, "Failed to load FW\n");
0643         goto exit_close_nsp;
0644     }
0645 
0646     pf->fw_loaded = !!err;
0647     err = 0;
0648 
0649 exit_close_nsp:
0650     nfp_nsp_close(nsp);
0651 
0652     return err;
0653 }
0654 
0655 static void nfp_fw_unload(struct nfp_pf *pf)
0656 {
0657     struct nfp_nsp *nsp;
0658     int err;
0659 
0660     nsp = nfp_nsp_open(pf->cpp);
0661     if (IS_ERR(nsp)) {
0662         nfp_err(pf->cpp, "Reset failed, can't open NSP\n");
0663         return;
0664     }
0665 
0666     err = nfp_nsp_device_soft_reset(nsp);
0667     if (err < 0)
0668         dev_warn(&pf->pdev->dev, "Couldn't unload firmware: %d\n", err);
0669     else
0670         dev_info(&pf->pdev->dev, "Firmware safely unloaded\n");
0671 
0672     nfp_nsp_close(nsp);
0673 }
0674 
0675 static int nfp_pf_find_rtsyms(struct nfp_pf *pf)
0676 {
0677     char pf_symbol[256];
0678     unsigned int pf_id;
0679 
0680     pf_id = nfp_cppcore_pcie_unit(pf->cpp);
0681 
0682     /* Optional per-PCI PF mailbox */
0683     snprintf(pf_symbol, sizeof(pf_symbol), NFP_MBOX_SYM_NAME, pf_id);
0684     pf->mbox = nfp_rtsym_lookup(pf->rtbl, pf_symbol);
0685     if (pf->mbox && nfp_rtsym_size(pf->mbox) < NFP_MBOX_SYM_MIN_SIZE) {
0686         nfp_err(pf->cpp, "PF mailbox symbol too small: %llu < %d\n",
0687             nfp_rtsym_size(pf->mbox), NFP_MBOX_SYM_MIN_SIZE);
0688         return -EINVAL;
0689     }
0690 
0691     return 0;
0692 }
0693 
0694 static int nfp_pci_probe(struct pci_dev *pdev,
0695              const struct pci_device_id *pci_id)
0696 {
0697     const struct nfp_dev_info *dev_info;
0698     struct devlink *devlink;
0699     struct nfp_pf *pf;
0700     int err;
0701 
0702     if ((pdev->vendor == PCI_VENDOR_ID_NETRONOME ||
0703          pdev->vendor == PCI_VENDOR_ID_CORIGINE) &&
0704         (pdev->device == PCI_DEVICE_ID_NFP3800_VF ||
0705          pdev->device == PCI_DEVICE_ID_NFP6000_VF))
0706         dev_warn(&pdev->dev, "Binding NFP VF device to the NFP PF driver, the VF driver is called 'nfp_netvf'\n");
0707 
0708     dev_info = &nfp_dev_info[pci_id->driver_data];
0709 
0710     err = pci_enable_device(pdev);
0711     if (err < 0)
0712         return err;
0713 
0714     pci_set_master(pdev);
0715 
0716     err = dma_set_mask_and_coherent(&pdev->dev, dev_info->dma_mask);
0717     if (err)
0718         goto err_pci_disable;
0719 
0720     err = pci_request_regions(pdev, nfp_driver_name);
0721     if (err < 0) {
0722         dev_err(&pdev->dev, "Unable to reserve pci resources.\n");
0723         goto err_pci_disable;
0724     }
0725 
0726     devlink = devlink_alloc(&nfp_devlink_ops, sizeof(*pf), &pdev->dev);
0727     if (!devlink) {
0728         err = -ENOMEM;
0729         goto err_rel_regions;
0730     }
0731     pf = devlink_priv(devlink);
0732     INIT_LIST_HEAD(&pf->vnics);
0733     INIT_LIST_HEAD(&pf->ports);
0734     pci_set_drvdata(pdev, pf);
0735     pf->pdev = pdev;
0736     pf->dev_info = dev_info;
0737 
0738     pf->wq = alloc_workqueue("nfp-%s", 0, 2, pci_name(pdev));
0739     if (!pf->wq) {
0740         err = -ENOMEM;
0741         goto err_pci_priv_unset;
0742     }
0743 
0744     pf->cpp = nfp_cpp_from_nfp6000_pcie(pdev, dev_info);
0745     if (IS_ERR(pf->cpp)) {
0746         err = PTR_ERR(pf->cpp);
0747         goto err_disable_msix;
0748     }
0749 
0750     err = nfp_resource_table_init(pf->cpp);
0751     if (err)
0752         goto err_cpp_free;
0753 
0754     pf->hwinfo = nfp_hwinfo_read(pf->cpp);
0755 
0756     dev_info(&pdev->dev, "Assembly: %s%s%s-%s CPLD: %s\n",
0757          nfp_hwinfo_lookup(pf->hwinfo, "assembly.vendor"),
0758          nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno"),
0759          nfp_hwinfo_lookup(pf->hwinfo, "assembly.serial"),
0760          nfp_hwinfo_lookup(pf->hwinfo, "assembly.revision"),
0761          nfp_hwinfo_lookup(pf->hwinfo, "cpld.version"));
0762 
0763     err = nfp_pf_board_state_wait(pf);
0764     if (err)
0765         goto err_hwinfo_free;
0766 
0767     err = nfp_nsp_init(pdev, pf);
0768     if (err)
0769         goto err_hwinfo_free;
0770 
0771     pf->mip = nfp_mip_open(pf->cpp);
0772     pf->rtbl = __nfp_rtsym_table_read(pf->cpp, pf->mip);
0773 
0774     err = nfp_pf_find_rtsyms(pf);
0775     if (err)
0776         goto err_fw_unload;
0777 
0778     pf->dump_flag = NFP_DUMP_NSP_DIAG;
0779     pf->dumpspec = nfp_net_dump_load_dumpspec(pf->cpp, pf->rtbl);
0780 
0781     err = nfp_pcie_sriov_read_nfd_limit(pf);
0782     if (err)
0783         goto err_fw_unload;
0784 
0785     pf->num_vfs = pci_num_vf(pdev);
0786     if (pf->num_vfs > pf->limit_vfs) {
0787         dev_err(&pdev->dev,
0788             "Error: %d VFs already enabled, but loaded FW can only support %d\n",
0789             pf->num_vfs, pf->limit_vfs);
0790         err = -EINVAL;
0791         goto err_fw_unload;
0792     }
0793 
0794     err = nfp_net_pci_probe(pf);
0795     if (err)
0796         goto err_fw_unload;
0797 
0798     err = nfp_hwmon_register(pf);
0799     if (err) {
0800         dev_err(&pdev->dev, "Failed to register hwmon info\n");
0801         goto err_net_remove;
0802     }
0803 
0804     return 0;
0805 
0806 err_net_remove:
0807     nfp_net_pci_remove(pf);
0808 err_fw_unload:
0809     kfree(pf->rtbl);
0810     nfp_mip_close(pf->mip);
0811     if (pf->unload_fw_on_remove)
0812         nfp_fw_unload(pf);
0813     kfree(pf->eth_tbl);
0814     kfree(pf->nspi);
0815     vfree(pf->dumpspec);
0816 err_hwinfo_free:
0817     kfree(pf->hwinfo);
0818 err_cpp_free:
0819     nfp_cpp_free(pf->cpp);
0820 err_disable_msix:
0821     destroy_workqueue(pf->wq);
0822 err_pci_priv_unset:
0823     pci_set_drvdata(pdev, NULL);
0824     devlink_free(devlink);
0825 err_rel_regions:
0826     pci_release_regions(pdev);
0827 err_pci_disable:
0828     pci_disable_device(pdev);
0829 
0830     return err;
0831 }
0832 
0833 static void __nfp_pci_shutdown(struct pci_dev *pdev, bool unload_fw)
0834 {
0835     struct nfp_pf *pf;
0836 
0837     pf = pci_get_drvdata(pdev);
0838     if (!pf)
0839         return;
0840 
0841     nfp_hwmon_unregister(pf);
0842 
0843     nfp_pcie_sriov_disable(pdev);
0844 
0845     nfp_net_pci_remove(pf);
0846 
0847     vfree(pf->dumpspec);
0848     kfree(pf->rtbl);
0849     nfp_mip_close(pf->mip);
0850     if (unload_fw && pf->unload_fw_on_remove)
0851         nfp_fw_unload(pf);
0852 
0853     destroy_workqueue(pf->wq);
0854     pci_set_drvdata(pdev, NULL);
0855     kfree(pf->hwinfo);
0856     nfp_cpp_free(pf->cpp);
0857 
0858     kfree(pf->eth_tbl);
0859     kfree(pf->nspi);
0860     devlink_free(priv_to_devlink(pf));
0861     pci_release_regions(pdev);
0862     pci_disable_device(pdev);
0863 }
0864 
0865 static void nfp_pci_remove(struct pci_dev *pdev)
0866 {
0867     __nfp_pci_shutdown(pdev, true);
0868 }
0869 
0870 static void nfp_pci_shutdown(struct pci_dev *pdev)
0871 {
0872     __nfp_pci_shutdown(pdev, false);
0873 }
0874 
0875 static struct pci_driver nfp_pci_driver = {
0876     .name           = nfp_driver_name,
0877     .id_table       = nfp_pci_device_ids,
0878     .probe          = nfp_pci_probe,
0879     .remove         = nfp_pci_remove,
0880     .shutdown       = nfp_pci_shutdown,
0881     .sriov_configure    = nfp_pcie_sriov_configure,
0882 };
0883 
0884 static int __init nfp_main_init(void)
0885 {
0886     int err;
0887 
0888     pr_info("%s: NFP PCIe Driver, Copyright (C) 2014-2020 Netronome Systems\n",
0889         nfp_driver_name);
0890     pr_info("%s: NFP PCIe Driver, Copyright (C) 2021-2022 Corigine Inc.\n",
0891         nfp_driver_name);
0892 
0893     nfp_net_debugfs_create();
0894 
0895     err = pci_register_driver(&nfp_pci_driver);
0896     if (err < 0)
0897         goto err_destroy_debugfs;
0898 
0899     err = pci_register_driver(&nfp_netvf_pci_driver);
0900     if (err)
0901         goto err_unreg_pf;
0902 
0903     return err;
0904 
0905 err_unreg_pf:
0906     pci_unregister_driver(&nfp_pci_driver);
0907 err_destroy_debugfs:
0908     nfp_net_debugfs_destroy();
0909     return err;
0910 }
0911 
0912 static void __exit nfp_main_exit(void)
0913 {
0914     pci_unregister_driver(&nfp_netvf_pci_driver);
0915     pci_unregister_driver(&nfp_pci_driver);
0916     nfp_net_debugfs_destroy();
0917 }
0918 
0919 module_init(nfp_main_init);
0920 module_exit(nfp_main_exit);
0921 
0922 MODULE_FIRMWARE("netronome/nic_AMDA0058-0011_2x40.nffw");
0923 MODULE_FIRMWARE("netronome/nic_AMDA0058-0012_2x40.nffw");
0924 MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_1x40.nffw");
0925 MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_4x10.nffw");
0926 MODULE_FIRMWARE("netronome/nic_AMDA0096-0001_2x10.nffw");
0927 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_2x40.nffw");
0928 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_4x10_1x40.nffw");
0929 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_8x10.nffw");
0930 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x10.nffw");
0931 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x25.nffw");
0932 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_1x10_1x25.nffw");
0933 
0934 MODULE_AUTHOR("Corigine, Inc. <oss-drivers@corigine.com>");
0935 MODULE_LICENSE("GPL");
0936 MODULE_DESCRIPTION("The Network Flow Processor (NFP) driver.");