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