linux/drivers/net/ethernet/netronome/nfp/nfp_main.c
<<
>>
Prefs
   1// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
   2/* Copyright (C) 2015-2018 Netronome Systems, Inc. */
   3
   4/*
   5 * nfp_main.c
   6 * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
   7 *          Alejandro Lucero <alejandro.lucero@netronome.com>
   8 *          Jason McMullan <jason.mcmullan@netronome.com>
   9 *          Rolf Neugebauer <rolf.neugebauer@netronome.com>
  10 */
  11
  12#include <linux/kernel.h>
  13#include <linux/module.h>
  14#include <linux/mutex.h>
  15#include <linux/pci.h>
  16#include <linux/firmware.h>
  17#include <linux/vmalloc.h>
  18#include <net/devlink.h>
  19
  20#include "nfpcore/nfp.h"
  21#include "nfpcore/nfp_cpp.h"
  22#include "nfpcore/nfp_nffw.h"
  23#include "nfpcore/nfp_nsp.h"
  24
  25#include "nfpcore/nfp6000_pcie.h"
  26
  27#include "nfp_abi.h"
  28#include "nfp_app.h"
  29#include "nfp_main.h"
  30#include "nfp_net.h"
  31
  32static const char nfp_driver_name[] = "nfp";
  33
  34static const struct pci_device_id nfp_pci_device_ids[] = {
  35        { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP6000,
  36          PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
  37          PCI_ANY_ID, 0,
  38        },
  39        { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP5000,
  40          PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
  41          PCI_ANY_ID, 0,
  42        },
  43        { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP4000,
  44          PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
  45          PCI_ANY_ID, 0,
  46        },
  47        { 0, } /* Required last entry. */
  48};
  49MODULE_DEVICE_TABLE(pci, nfp_pci_device_ids);
  50
  51int nfp_pf_rtsym_read_optional(struct nfp_pf *pf, const char *format,
  52                               unsigned int default_val)
  53{
  54        char name[256];
  55        int err = 0;
  56        u64 val;
  57
  58        snprintf(name, sizeof(name), format, nfp_cppcore_pcie_unit(pf->cpp));
  59
  60        val = nfp_rtsym_read_le(pf->rtbl, name, &err);
  61        if (err) {
  62                if (err == -ENOENT)
  63                        return default_val;
  64                nfp_err(pf->cpp, "Unable to read symbol %s\n", name);
  65                return err;
  66        }
  67
  68        return val;
  69}
  70
  71u8 __iomem *
  72nfp_pf_map_rtsym(struct nfp_pf *pf, const char *name, const char *sym_fmt,
  73                 unsigned int min_size, struct nfp_cpp_area **area)
  74{
  75        char pf_symbol[256];
  76
  77        snprintf(pf_symbol, sizeof(pf_symbol), sym_fmt,
  78                 nfp_cppcore_pcie_unit(pf->cpp));
  79
  80        return nfp_rtsym_map(pf->rtbl, pf_symbol, name, min_size, area);
  81}
  82
  83/* Callers should hold the devlink instance lock */
  84int nfp_mbox_cmd(struct nfp_pf *pf, u32 cmd, void *in_data, u64 in_length,
  85                 void *out_data, u64 out_length)
  86{
  87        unsigned long err_at;
  88        u64 max_data_sz;
  89        u32 val = 0;
  90        int n, err;
  91
  92        if (!pf->mbox)
  93                return -EOPNOTSUPP;
  94
  95        max_data_sz = nfp_rtsym_size(pf->mbox) - NFP_MBOX_SYM_MIN_SIZE;
  96
  97        /* Check if cmd field is clear */
  98        err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_CMD, &val);
  99        if (err || val) {
 100                nfp_warn(pf->cpp, "failed to issue command (%u): %u, err: %d\n",
 101                         cmd, val, err);
 102                return err ?: -EBUSY;
 103        }
 104
 105        in_length = min(in_length, max_data_sz);
 106        n = nfp_rtsym_write(pf->cpp, pf->mbox, NFP_MBOX_DATA, in_data,
 107                            in_length);
 108        if (n != in_length)
 109                return -EIO;
 110        /* Write data_len and wipe reserved */
 111        err = nfp_rtsym_writeq(pf->cpp, pf->mbox, NFP_MBOX_DATA_LEN, in_length);
 112        if (err)
 113                return err;
 114
 115        /* Read back for ordering */
 116        err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_DATA_LEN, &val);
 117        if (err)
 118                return err;
 119
 120        /* Write cmd and wipe return value */
 121        err = nfp_rtsym_writeq(pf->cpp, pf->mbox, NFP_MBOX_CMD, cmd);
 122        if (err)
 123                return err;
 124
 125        err_at = jiffies + 5 * HZ;
 126        while (true) {
 127                /* Wait for command to go to 0 (NFP_MBOX_NO_CMD) */
 128                err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_CMD, &val);
 129                if (err)
 130                        return err;
 131                if (!val)
 132                        break;
 133
 134                if (time_is_before_eq_jiffies(err_at))
 135                        return -ETIMEDOUT;
 136
 137                msleep(5);
 138        }
 139
 140        /* Copy output if any (could be error info, do it before reading ret) */
 141        err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_DATA_LEN, &val);
 142        if (err)
 143                return err;
 144
 145        out_length = min_t(u32, val, min(out_length, max_data_sz));
 146        n = nfp_rtsym_read(pf->cpp, pf->mbox, NFP_MBOX_DATA,
 147                           out_data, out_length);
 148        if (n != out_length)
 149                return -EIO;
 150
 151        /* Check if there is an error */
 152        err = nfp_rtsym_readl(pf->cpp, pf->mbox, NFP_MBOX_RET, &val);
 153        if (err)
 154                return err;
 155        if (val)
 156                return -val;
 157
 158        return out_length;
 159}
 160
 161static bool nfp_board_ready(struct nfp_pf *pf)
 162{
 163        const char *cp;
 164        long state;
 165        int err;
 166
 167        cp = nfp_hwinfo_lookup(pf->hwinfo, "board.state");
 168        if (!cp)
 169                return false;
 170
 171        err = kstrtol(cp, 0, &state);
 172        if (err < 0)
 173                return false;
 174
 175        return state == 15;
 176}
 177
 178static int nfp_pf_board_state_wait(struct nfp_pf *pf)
 179{
 180        const unsigned long wait_until = jiffies + 10 * HZ;
 181
 182        while (!nfp_board_ready(pf)) {
 183                if (time_is_before_eq_jiffies(wait_until)) {
 184                        nfp_err(pf->cpp, "NFP board initialization timeout\n");
 185                        return -EINVAL;
 186                }
 187
 188                nfp_info(pf->cpp, "waiting for board initialization\n");
 189                if (msleep_interruptible(500))
 190                        return -ERESTARTSYS;
 191
 192                /* Refresh cached information */
 193                kfree(pf->hwinfo);
 194                pf->hwinfo = nfp_hwinfo_read(pf->cpp);
 195        }
 196
 197        return 0;
 198}
 199
 200static int nfp_pcie_sriov_read_nfd_limit(struct nfp_pf *pf)
 201{
 202        int err;
 203
 204        pf->limit_vfs = nfp_rtsym_read_le(pf->rtbl, "nfd_vf_cfg_max_vfs", &err);
 205        if (err) {
 206                /* For backwards compatibility if symbol not found allow all */
 207                pf->limit_vfs = ~0;
 208                if (err == -ENOENT)
 209                        return 0;
 210
 211                nfp_warn(pf->cpp, "Warning: VF limit read failed: %d\n", err);
 212                return err;
 213        }
 214
 215        err = pci_sriov_set_totalvfs(pf->pdev, pf->limit_vfs);
 216        if (err)
 217                nfp_warn(pf->cpp, "Failed to set VF count in sysfs: %d\n", err);
 218        return 0;
 219}
 220
 221static int nfp_pcie_sriov_enable(struct pci_dev *pdev, int num_vfs)
 222{
 223#ifdef CONFIG_PCI_IOV
 224        struct nfp_pf *pf = pci_get_drvdata(pdev);
 225        int err;
 226
 227        if (num_vfs > pf->limit_vfs) {
 228                nfp_info(pf->cpp, "Firmware limits number of VFs to %u\n",
 229                         pf->limit_vfs);
 230                return -EINVAL;
 231        }
 232
 233        err = pci_enable_sriov(pdev, num_vfs);
 234        if (err) {
 235                dev_warn(&pdev->dev, "Failed to enable PCI SR-IOV: %d\n", err);
 236                return err;
 237        }
 238
 239        mutex_lock(&pf->lock);
 240
 241        err = nfp_app_sriov_enable(pf->app, num_vfs);
 242        if (err) {
 243                dev_warn(&pdev->dev,
 244                         "App specific PCI SR-IOV configuration failed: %d\n",
 245                         err);
 246                goto err_sriov_disable;
 247        }
 248
 249        pf->num_vfs = num_vfs;
 250
 251        dev_dbg(&pdev->dev, "Created %d VFs.\n", pf->num_vfs);
 252
 253        mutex_unlock(&pf->lock);
 254        return num_vfs;
 255
 256err_sriov_disable:
 257        mutex_unlock(&pf->lock);
 258        pci_disable_sriov(pdev);
 259        return err;
 260#endif
 261        return 0;
 262}
 263
 264static int nfp_pcie_sriov_disable(struct pci_dev *pdev)
 265{
 266#ifdef CONFIG_PCI_IOV
 267        struct nfp_pf *pf = pci_get_drvdata(pdev);
 268
 269        mutex_lock(&pf->lock);
 270
 271        /* If the VFs are assigned we cannot shut down SR-IOV without
 272         * causing issues, so just leave the hardware available but
 273         * disabled
 274         */
 275        if (pci_vfs_assigned(pdev)) {
 276                dev_warn(&pdev->dev, "Disabling while VFs assigned - VFs will not be deallocated\n");
 277                mutex_unlock(&pf->lock);
 278                return -EPERM;
 279        }
 280
 281        nfp_app_sriov_disable(pf->app);
 282
 283        pf->num_vfs = 0;
 284
 285        mutex_unlock(&pf->lock);
 286
 287        pci_disable_sriov(pdev);
 288        dev_dbg(&pdev->dev, "Removed VFs.\n");
 289#endif
 290        return 0;
 291}
 292
 293static int nfp_pcie_sriov_configure(struct pci_dev *pdev, int num_vfs)
 294{
 295        if (!pci_get_drvdata(pdev))
 296                return -ENOENT;
 297
 298        if (num_vfs == 0)
 299                return nfp_pcie_sriov_disable(pdev);
 300        else
 301                return nfp_pcie_sriov_enable(pdev, num_vfs);
 302}
 303
 304int nfp_flash_update_common(struct nfp_pf *pf, const struct firmware *fw,
 305                            struct netlink_ext_ack *extack)
 306{
 307        struct device *dev = &pf->pdev->dev;
 308        struct nfp_nsp *nsp;
 309        int err;
 310
 311        nsp = nfp_nsp_open(pf->cpp);
 312        if (IS_ERR(nsp)) {
 313                err = PTR_ERR(nsp);
 314                if (extack)
 315                        NL_SET_ERR_MSG_MOD(extack, "can't access NSP");
 316                else
 317                        dev_err(dev, "Failed to access the NSP: %d\n", err);
 318                return err;
 319        }
 320
 321        err = nfp_nsp_write_flash(nsp, fw);
 322        if (err < 0)
 323                goto exit_close_nsp;
 324        dev_info(dev, "Finished writing flash image\n");
 325        err = 0;
 326
 327exit_close_nsp:
 328        nfp_nsp_close(nsp);
 329        return err;
 330}
 331
 332static const struct firmware *
 333nfp_net_fw_request(struct pci_dev *pdev, struct nfp_pf *pf, const char *name)
 334{
 335        const struct firmware *fw = NULL;
 336        int err;
 337
 338        err = request_firmware_direct(&fw, name, &pdev->dev);
 339        nfp_info(pf->cpp, "  %s: %s\n",
 340                 name, err ? "not found" : "found");
 341        if (err)
 342                return NULL;
 343
 344        return fw;
 345}
 346
 347/**
 348 * nfp_net_fw_find() - Find the correct firmware image for netdev mode
 349 * @pdev:       PCI Device structure
 350 * @pf:         NFP PF Device structure
 351 *
 352 * Return: firmware if found and requested successfully.
 353 */
 354static const struct firmware *
 355nfp_net_fw_find(struct pci_dev *pdev, struct nfp_pf *pf)
 356{
 357        struct nfp_eth_table_port *port;
 358        const struct firmware *fw;
 359        const char *fw_model;
 360        char fw_name[256];
 361        const u8 *serial;
 362        u16 interface;
 363        int spc, i, j;
 364
 365        nfp_info(pf->cpp, "Looking for firmware file in order of priority:\n");
 366
 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);
 373        if (fw)
 374                return fw;
 375
 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);
 379        if (fw)
 380                return fw;
 381
 382        /* Finally try the card type and media */
 383        if (!pf->eth_tbl) {
 384                dev_err(&pdev->dev, "Error: can't identify media config\n");
 385                return NULL;
 386        }
 387
 388        fw_model = nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno");
 389        if (!fw_model) {
 390                dev_err(&pdev->dev, "Error: can't read part number\n");
 391                return NULL;
 392        }
 393
 394        spc = ARRAY_SIZE(fw_name);
 395        spc -= snprintf(fw_name, spc, "netronome/nic_%s", fw_model);
 396
 397        for (i = 0; spc > 0 && i < pf->eth_tbl->count; i += j) {
 398                port = &pf->eth_tbl->ports[i];
 399                j = 1;
 400                while (i + j < pf->eth_tbl->count &&
 401                       port->speed == port[j].speed)
 402                        j++;
 403
 404                spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc,
 405                                "_%dx%d", j, port->speed / 1000);
 406        }
 407
 408        if (spc <= 0)
 409                return NULL;
 410
 411        spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc, ".nffw");
 412        if (spc <= 0)
 413                return NULL;
 414
 415        return nfp_net_fw_request(pdev, pf, fw_name);
 416}
 417
 418static int
 419nfp_get_fw_policy_value(struct pci_dev *pdev, struct nfp_nsp *nsp,
 420                        const char *key, const char *default_val, int max_val,
 421                        int *value)
 422{
 423        char hwinfo[64];
 424        long hi_val;
 425        int err;
 426
 427        snprintf(hwinfo, sizeof(hwinfo), key);
 428        err = nfp_nsp_hwinfo_lookup_optional(nsp, hwinfo, sizeof(hwinfo),
 429                                             default_val);
 430        if (err)
 431                return err;
 432
 433        err = kstrtol(hwinfo, 0, &hi_val);
 434        if (err || hi_val < 0 || hi_val > max_val) {
 435                dev_warn(&pdev->dev,
 436                         "Invalid value '%s' from '%s', ignoring\n",
 437                         hwinfo, key);
 438                err = kstrtol(default_val, 0, &hi_val);
 439        }
 440
 441        *value = hi_val;
 442        return err;
 443}
 444
 445/**
 446 * nfp_fw_load() - Load the firmware image
 447 * @pdev:       PCI Device structure
 448 * @pf:         NFP PF Device structure
 449 * @nsp:        NFP SP handle
 450 *
 451 * Return: -ERRNO, 0 for no firmware loaded, 1 for firmware loaded
 452 */
 453static int
 454nfp_fw_load(struct pci_dev *pdev, struct nfp_pf *pf, struct nfp_nsp *nsp)
 455{
 456        bool do_reset, fw_loaded = false;
 457        const struct firmware *fw = NULL;
 458        int err, reset, policy, ifcs = 0;
 459        char *token, *ptr;
 460        char hwinfo[64];
 461        u16 interface;
 462
 463        snprintf(hwinfo, sizeof(hwinfo), "abi_drv_load_ifc");
 464        err = nfp_nsp_hwinfo_lookup_optional(nsp, hwinfo, sizeof(hwinfo),
 465                                             NFP_NSP_DRV_LOAD_IFC_DEFAULT);
 466        if (err)
 467                return err;
 468
 469        interface = nfp_cpp_interface(pf->cpp);
 470        ptr = hwinfo;
 471        while ((token = strsep(&ptr, ","))) {
 472                unsigned long interface_hi;
 473
 474                err = kstrtoul(token, 0, &interface_hi);
 475                if (err) {
 476                        dev_err(&pdev->dev,
 477                                "Failed to parse interface '%s': %d\n",
 478                                token, err);
 479                        return err;
 480                }
 481
 482                ifcs++;
 483                if (interface == interface_hi)
 484                        break;
 485        }
 486
 487        if (!token) {
 488                dev_info(&pdev->dev, "Firmware will be loaded by partner\n");
 489                return 0;
 490        }
 491
 492        err = nfp_get_fw_policy_value(pdev, nsp, "abi_drv_reset",
 493                                      NFP_NSP_DRV_RESET_DEFAULT,
 494                                      NFP_NSP_DRV_RESET_NEVER, &reset);
 495        if (err)
 496                return err;
 497
 498        err = nfp_get_fw_policy_value(pdev, nsp, "app_fw_from_flash",
 499                                      NFP_NSP_APP_FW_LOAD_DEFAULT,
 500                                      NFP_NSP_APP_FW_LOAD_PREF, &policy);
 501        if (err)
 502                return err;
 503
 504        fw = nfp_net_fw_find(pdev, pf);
 505        do_reset = reset == NFP_NSP_DRV_RESET_ALWAYS ||
 506                   (fw && reset == NFP_NSP_DRV_RESET_DISK);
 507
 508        if (do_reset) {
 509                dev_info(&pdev->dev, "Soft-resetting the NFP\n");
 510                err = nfp_nsp_device_soft_reset(nsp);
 511                if (err < 0) {
 512                        dev_err(&pdev->dev,
 513                                "Failed to soft reset the NFP: %d\n", err);
 514                        goto exit_release_fw;
 515                }
 516        }
 517
 518        if (fw && policy != NFP_NSP_APP_FW_LOAD_FLASH) {
 519                if (nfp_nsp_has_fw_loaded(nsp) && nfp_nsp_fw_loaded(nsp))
 520                        goto exit_release_fw;
 521
 522                err = nfp_nsp_load_fw(nsp, fw);
 523                if (err < 0) {
 524                        dev_err(&pdev->dev, "FW loading failed: %d\n",
 525                                err);
 526                        goto exit_release_fw;
 527                }
 528                dev_info(&pdev->dev, "Finished loading FW image\n");
 529                fw_loaded = true;
 530        } else if (policy != NFP_NSP_APP_FW_LOAD_DISK &&
 531                   nfp_nsp_has_stored_fw_load(nsp)) {
 532
 533                /* Don't propagate this error to stick with legacy driver
 534                 * behavior, failure will be detected later during init.
 535                 */
 536                if (!nfp_nsp_load_stored_fw(nsp))
 537                        dev_info(&pdev->dev, "Finished loading stored FW image\n");
 538
 539                /* Don't flag the fw_loaded in this case since other devices
 540                 * may reuse the firmware when configured this way
 541                 */
 542        } else {
 543                dev_warn(&pdev->dev, "Didn't load firmware, please update flash or reconfigure card\n");
 544        }
 545
 546exit_release_fw:
 547        release_firmware(fw);
 548
 549        /* We don't want to unload firmware when other devices may still be
 550         * dependent on it, which could be the case if there are multiple
 551         * devices that could load firmware.
 552         */
 553        if (fw_loaded && ifcs == 1)
 554                pf->unload_fw_on_remove = true;
 555
 556        return err < 0 ? err : fw_loaded;
 557}
 558
 559static void
 560nfp_nsp_init_ports(struct pci_dev *pdev, struct nfp_pf *pf,
 561                   struct nfp_nsp *nsp)
 562{
 563        bool needs_reinit = false;
 564        int i;
 565
 566        pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
 567        if (!pf->eth_tbl)
 568                return;
 569
 570        if (!nfp_nsp_has_mac_reinit(nsp))
 571                return;
 572
 573        for (i = 0; i < pf->eth_tbl->count; i++)
 574                needs_reinit |= pf->eth_tbl->ports[i].override_changed;
 575        if (!needs_reinit)
 576                return;
 577
 578        kfree(pf->eth_tbl);
 579        if (nfp_nsp_mac_reinit(nsp))
 580                dev_warn(&pdev->dev, "MAC reinit failed\n");
 581
 582        pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
 583}
 584
 585static int nfp_nsp_init(struct pci_dev *pdev, struct nfp_pf *pf)
 586{
 587        struct nfp_nsp *nsp;
 588        int err;
 589
 590        err = nfp_resource_wait(pf->cpp, NFP_RESOURCE_NSP, 30);
 591        if (err)
 592                return err;
 593
 594        nsp = nfp_nsp_open(pf->cpp);
 595        if (IS_ERR(nsp)) {
 596                err = PTR_ERR(nsp);
 597                dev_err(&pdev->dev, "Failed to access the NSP: %d\n", err);
 598                return err;
 599        }
 600
 601        err = nfp_nsp_wait(nsp);
 602        if (err < 0)
 603                goto exit_close_nsp;
 604
 605        nfp_nsp_init_ports(pdev, pf, nsp);
 606
 607        pf->nspi = __nfp_nsp_identify(nsp);
 608        if (pf->nspi)
 609                dev_info(&pdev->dev, "BSP: %s\n", pf->nspi->version);
 610
 611        err = nfp_fw_load(pdev, pf, nsp);
 612        if (err < 0) {
 613                kfree(pf->nspi);
 614                kfree(pf->eth_tbl);
 615                dev_err(&pdev->dev, "Failed to load FW\n");
 616                goto exit_close_nsp;
 617        }
 618
 619        pf->fw_loaded = !!err;
 620        err = 0;
 621
 622exit_close_nsp:
 623        nfp_nsp_close(nsp);
 624
 625        return err;
 626}
 627
 628static void nfp_fw_unload(struct nfp_pf *pf)
 629{
 630        struct nfp_nsp *nsp;
 631        int err;
 632
 633        nsp = nfp_nsp_open(pf->cpp);
 634        if (IS_ERR(nsp)) {
 635                nfp_err(pf->cpp, "Reset failed, can't open NSP\n");
 636                return;
 637        }
 638
 639        err = nfp_nsp_device_soft_reset(nsp);
 640        if (err < 0)
 641                dev_warn(&pf->pdev->dev, "Couldn't unload firmware: %d\n", err);
 642        else
 643                dev_info(&pf->pdev->dev, "Firmware safely unloaded\n");
 644
 645        nfp_nsp_close(nsp);
 646}
 647
 648static int nfp_pf_find_rtsyms(struct nfp_pf *pf)
 649{
 650        char pf_symbol[256];
 651        unsigned int pf_id;
 652
 653        pf_id = nfp_cppcore_pcie_unit(pf->cpp);
 654
 655        /* Optional per-PCI PF mailbox */
 656        snprintf(pf_symbol, sizeof(pf_symbol), NFP_MBOX_SYM_NAME, pf_id);
 657        pf->mbox = nfp_rtsym_lookup(pf->rtbl, pf_symbol);
 658        if (pf->mbox && nfp_rtsym_size(pf->mbox) < NFP_MBOX_SYM_MIN_SIZE) {
 659                nfp_err(pf->cpp, "PF mailbox symbol too small: %llu < %d\n",
 660                        nfp_rtsym_size(pf->mbox), NFP_MBOX_SYM_MIN_SIZE);
 661                return -EINVAL;
 662        }
 663
 664        return 0;
 665}
 666
 667static int nfp_pci_probe(struct pci_dev *pdev,
 668                         const struct pci_device_id *pci_id)
 669{
 670        struct devlink *devlink;
 671        struct nfp_pf *pf;
 672        int err;
 673
 674        if (pdev->vendor == PCI_VENDOR_ID_NETRONOME &&
 675            pdev->device == PCI_DEVICE_ID_NETRONOME_NFP6000_VF)
 676                dev_warn(&pdev->dev, "Binding NFP VF device to the NFP PF driver, the VF driver is called 'nfp_netvf'\n");
 677
 678        err = pci_enable_device(pdev);
 679        if (err < 0)
 680                return err;
 681
 682        pci_set_master(pdev);
 683
 684        err = dma_set_mask_and_coherent(&pdev->dev,
 685                                        DMA_BIT_MASK(NFP_NET_MAX_DMA_BITS));
 686        if (err)
 687                goto err_pci_disable;
 688
 689        err = pci_request_regions(pdev, nfp_driver_name);
 690        if (err < 0) {
 691                dev_err(&pdev->dev, "Unable to reserve pci resources.\n");
 692                goto err_pci_disable;
 693        }
 694
 695        devlink = devlink_alloc(&nfp_devlink_ops, sizeof(*pf), &pdev->dev);
 696        if (!devlink) {
 697                err = -ENOMEM;
 698                goto err_rel_regions;
 699        }
 700        pf = devlink_priv(devlink);
 701        INIT_LIST_HEAD(&pf->vnics);
 702        INIT_LIST_HEAD(&pf->ports);
 703        mutex_init(&pf->lock);
 704        pci_set_drvdata(pdev, pf);
 705        pf->pdev = pdev;
 706
 707        pf->wq = alloc_workqueue("nfp-%s", 0, 2, pci_name(pdev));
 708        if (!pf->wq) {
 709                err = -ENOMEM;
 710                goto err_pci_priv_unset;
 711        }
 712
 713        pf->cpp = nfp_cpp_from_nfp6000_pcie(pdev);
 714        if (IS_ERR(pf->cpp)) {
 715                err = PTR_ERR(pf->cpp);
 716                goto err_disable_msix;
 717        }
 718
 719        err = nfp_resource_table_init(pf->cpp);
 720        if (err)
 721                goto err_cpp_free;
 722
 723        pf->hwinfo = nfp_hwinfo_read(pf->cpp);
 724
 725        dev_info(&pdev->dev, "Assembly: %s%s%s-%s CPLD: %s\n",
 726                 nfp_hwinfo_lookup(pf->hwinfo, "assembly.vendor"),
 727                 nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno"),
 728                 nfp_hwinfo_lookup(pf->hwinfo, "assembly.serial"),
 729                 nfp_hwinfo_lookup(pf->hwinfo, "assembly.revision"),
 730                 nfp_hwinfo_lookup(pf->hwinfo, "cpld.version"));
 731
 732        err = nfp_pf_board_state_wait(pf);
 733        if (err)
 734                goto err_hwinfo_free;
 735
 736        err = nfp_nsp_init(pdev, pf);
 737        if (err)
 738                goto err_hwinfo_free;
 739
 740        pf->mip = nfp_mip_open(pf->cpp);
 741        pf->rtbl = __nfp_rtsym_table_read(pf->cpp, pf->mip);
 742
 743        err = nfp_pf_find_rtsyms(pf);
 744        if (err)
 745                goto err_fw_unload;
 746
 747        pf->dump_flag = NFP_DUMP_NSP_DIAG;
 748        pf->dumpspec = nfp_net_dump_load_dumpspec(pf->cpp, pf->rtbl);
 749
 750        err = nfp_pcie_sriov_read_nfd_limit(pf);
 751        if (err)
 752                goto err_fw_unload;
 753
 754        pf->num_vfs = pci_num_vf(pdev);
 755        if (pf->num_vfs > pf->limit_vfs) {
 756                dev_err(&pdev->dev,
 757                        "Error: %d VFs already enabled, but loaded FW can only support %d\n",
 758                        pf->num_vfs, pf->limit_vfs);
 759                err = -EINVAL;
 760                goto err_fw_unload;
 761        }
 762
 763        err = nfp_net_pci_probe(pf);
 764        if (err)
 765                goto err_fw_unload;
 766
 767        err = nfp_hwmon_register(pf);
 768        if (err) {
 769                dev_err(&pdev->dev, "Failed to register hwmon info\n");
 770                goto err_net_remove;
 771        }
 772
 773        return 0;
 774
 775err_net_remove:
 776        nfp_net_pci_remove(pf);
 777err_fw_unload:
 778        kfree(pf->rtbl);
 779        nfp_mip_close(pf->mip);
 780        if (pf->unload_fw_on_remove)
 781                nfp_fw_unload(pf);
 782        kfree(pf->eth_tbl);
 783        kfree(pf->nspi);
 784        vfree(pf->dumpspec);
 785err_hwinfo_free:
 786        kfree(pf->hwinfo);
 787err_cpp_free:
 788        nfp_cpp_free(pf->cpp);
 789err_disable_msix:
 790        destroy_workqueue(pf->wq);
 791err_pci_priv_unset:
 792        pci_set_drvdata(pdev, NULL);
 793        mutex_destroy(&pf->lock);
 794        devlink_free(devlink);
 795err_rel_regions:
 796        pci_release_regions(pdev);
 797err_pci_disable:
 798        pci_disable_device(pdev);
 799
 800        return err;
 801}
 802
 803static void __nfp_pci_shutdown(struct pci_dev *pdev, bool unload_fw)
 804{
 805        struct nfp_pf *pf;
 806
 807        pf = pci_get_drvdata(pdev);
 808        if (!pf)
 809                return;
 810
 811        nfp_hwmon_unregister(pf);
 812
 813        nfp_pcie_sriov_disable(pdev);
 814
 815        nfp_net_pci_remove(pf);
 816
 817        vfree(pf->dumpspec);
 818        kfree(pf->rtbl);
 819        nfp_mip_close(pf->mip);
 820        if (unload_fw && pf->unload_fw_on_remove)
 821                nfp_fw_unload(pf);
 822
 823        destroy_workqueue(pf->wq);
 824        pci_set_drvdata(pdev, NULL);
 825        kfree(pf->hwinfo);
 826        nfp_cpp_free(pf->cpp);
 827
 828        kfree(pf->eth_tbl);
 829        kfree(pf->nspi);
 830        mutex_destroy(&pf->lock);
 831        devlink_free(priv_to_devlink(pf));
 832        pci_release_regions(pdev);
 833        pci_disable_device(pdev);
 834}
 835
 836static void nfp_pci_remove(struct pci_dev *pdev)
 837{
 838        __nfp_pci_shutdown(pdev, true);
 839}
 840
 841static void nfp_pci_shutdown(struct pci_dev *pdev)
 842{
 843        __nfp_pci_shutdown(pdev, false);
 844}
 845
 846static struct pci_driver nfp_pci_driver = {
 847        .name                   = nfp_driver_name,
 848        .id_table               = nfp_pci_device_ids,
 849        .probe                  = nfp_pci_probe,
 850        .remove                 = nfp_pci_remove,
 851        .shutdown               = nfp_pci_shutdown,
 852        .sriov_configure        = nfp_pcie_sriov_configure,
 853};
 854
 855static int __init nfp_main_init(void)
 856{
 857        int err;
 858
 859        pr_info("%s: NFP PCIe Driver, Copyright (C) 2014-2017 Netronome Systems\n",
 860                nfp_driver_name);
 861
 862        nfp_net_debugfs_create();
 863
 864        err = pci_register_driver(&nfp_pci_driver);
 865        if (err < 0)
 866                goto err_destroy_debugfs;
 867
 868        err = pci_register_driver(&nfp_netvf_pci_driver);
 869        if (err)
 870                goto err_unreg_pf;
 871
 872        return err;
 873
 874err_unreg_pf:
 875        pci_unregister_driver(&nfp_pci_driver);
 876err_destroy_debugfs:
 877        nfp_net_debugfs_destroy();
 878        return err;
 879}
 880
 881static void __exit nfp_main_exit(void)
 882{
 883        pci_unregister_driver(&nfp_netvf_pci_driver);
 884        pci_unregister_driver(&nfp_pci_driver);
 885        nfp_net_debugfs_destroy();
 886}
 887
 888module_init(nfp_main_init);
 889module_exit(nfp_main_exit);
 890
 891MODULE_FIRMWARE("netronome/nic_AMDA0058-0011_2x40.nffw");
 892MODULE_FIRMWARE("netronome/nic_AMDA0058-0012_2x40.nffw");
 893MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_1x40.nffw");
 894MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_4x10.nffw");
 895MODULE_FIRMWARE("netronome/nic_AMDA0096-0001_2x10.nffw");
 896MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_2x40.nffw");
 897MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_4x10_1x40.nffw");
 898MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_8x10.nffw");
 899MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x10.nffw");
 900MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x25.nffw");
 901MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_1x10_1x25.nffw");
 902
 903MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
 904MODULE_LICENSE("GPL");
 905MODULE_DESCRIPTION("The Netronome Flow Processor (NFP) driver.");
 906