1 /* 2 * Copyright (C) 2015-2017 Netronome Systems, Inc. 3 * 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. 8 * 9 * The BSD 2-Clause License: 10 * 11 * Redistribution and use in source and binary forms, with or 12 * without modification, are permitted provided that the following 13 * conditions are met: 14 * 15 * 1. Redistributions of source code must retain the above 16 * copyright notice, this list of conditions and the following 17 * disclaimer. 18 * 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. 23 * 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 31 * SOFTWARE. 32 */ 33 34 /* 35 * nfp_main.c 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> 40 */ 41 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> 49 50 #include "nfpcore/nfp.h" 51 #include "nfpcore/nfp_cpp.h" 52 #include "nfpcore/nfp_nffw.h" 53 #include "nfpcore/nfp_nsp.h" 54 55 #include "nfpcore/nfp6000_pcie.h" 56 57 #include "nfp_app.h" 58 #include "nfp_main.h" 59 #include "nfp_net.h" 60 61 static const char nfp_driver_name[] = "nfp"; 62 const char nfp_driver_version[] = VERMAGIC_STRING; 63 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, 67 PCI_ANY_ID, 0, 68 }, 69 { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP4000, 70 PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID, 71 PCI_ANY_ID, 0, 72 }, 73 { 0, } /* Required last entry. */ 74 }; 75 MODULE_DEVICE_TABLE(pci, nfp_pci_device_ids); 76 77 static bool nfp_board_ready(struct nfp_pf *pf) 78 { 79 const char *cp; 80 long state; 81 int err; 82 83 cp = nfp_hwinfo_lookup(pf->hwinfo, "board.state"); 84 if (!cp) 85 return false; 86 87 err = kstrtol(cp, 0, &state); 88 if (err < 0) 89 return false; 90 91 return state == 15; 92 } 93 94 static int nfp_pf_board_state_wait(struct nfp_pf *pf) 95 { 96 const unsigned long wait_until = jiffies + 10 * HZ; 97 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"); 101 return -EINVAL; 102 } 103 104 nfp_info(pf->cpp, "waiting for board initialization\n"); 105 if (msleep_interruptible(500)) 106 return -ERESTARTSYS; 107 108 /* Refresh cached information */ 109 kfree(pf->hwinfo); 110 pf->hwinfo = nfp_hwinfo_read(pf->cpp); 111 } 112 113 return 0; 114 } 115 116 static int nfp_pcie_sriov_read_nfd_limit(struct nfp_pf *pf) 117 { 118 int err; 119 120 pf->limit_vfs = nfp_rtsym_read_le(pf->rtbl, "nfd_vf_cfg_max_vfs", &err); 121 if (!err) 122 return pci_sriov_set_totalvfs(pf->pdev, pf->limit_vfs); 123 124 pf->limit_vfs = ~0; 125 pci_sriov_set_totalvfs(pf->pdev, 0); /* 0 is unset */ 126 /* Allow any setting for backwards compatibility if symbol not found */ 127 if (err == -ENOENT) 128 return 0; 129 130 nfp_warn(pf->cpp, "Warning: VF limit read failed: %d\n", err); 131 return err; 132 } 133 134 static int nfp_pcie_sriov_enable(struct pci_dev *pdev, int num_vfs) 135 { 136 #ifdef CONFIG_PCI_IOV 137 struct nfp_pf *pf = pci_get_drvdata(pdev); 138 int err; 139 140 if (num_vfs > pf->limit_vfs) { 141 nfp_info(pf->cpp, "Firmware limits number of VFs to %u\n", 142 pf->limit_vfs); 143 return -EINVAL; 144 } 145 146 err = pci_enable_sriov(pdev, num_vfs); 147 if (err) { 148 dev_warn(&pdev->dev, "Failed to enable PCI SR-IOV: %d\n", err); 149 return err; 150 } 151 152 mutex_lock(&pf->lock); 153 154 err = nfp_app_sriov_enable(pf->app, num_vfs); 155 if (err) { 156 dev_warn(&pdev->dev, 157 "App specific PCI SR-IOV configuration failed: %d\n", 158 err); 159 goto err_sriov_disable; 160 } 161 162 pf->num_vfs = num_vfs; 163 164 dev_dbg(&pdev->dev, "Created %d VFs.\n", pf->num_vfs); 165 166 mutex_unlock(&pf->lock); 167 return num_vfs; 168 169 err_sriov_disable: 170 mutex_unlock(&pf->lock); 171 pci_disable_sriov(pdev); 172 return err; 173 #endif 174 return 0; 175 } 176 177 static int nfp_pcie_sriov_disable(struct pci_dev *pdev) 178 { 179 #ifdef CONFIG_PCI_IOV 180 struct nfp_pf *pf = pci_get_drvdata(pdev); 181 182 mutex_lock(&pf->lock); 183 184 /* If the VFs are assigned we cannot shut down SR-IOV without 185 * causing issues, so just leave the hardware available but 186 * disabled 187 */ 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); 191 return -EPERM; 192 } 193 194 nfp_app_sriov_disable(pf->app); 195 196 pf->num_vfs = 0; 197 198 mutex_unlock(&pf->lock); 199 200 pci_disable_sriov(pdev); 201 dev_dbg(&pdev->dev, "Removed VFs.\n"); 202 #endif 203 return 0; 204 } 205 206 static int nfp_pcie_sriov_configure(struct pci_dev *pdev, int num_vfs) 207 { 208 if (num_vfs == 0) 209 return nfp_pcie_sriov_disable(pdev); 210 else 211 return nfp_pcie_sriov_enable(pdev, num_vfs); 212 } 213 214 static const struct firmware * 215 nfp_net_fw_request(struct pci_dev *pdev, struct nfp_pf *pf, const char *name) 216 { 217 const struct firmware *fw = NULL; 218 int err; 219 220 err = request_firmware_direct(&fw, name, &pdev->dev); 221 nfp_info(pf->cpp, " %s: %s\n", 222 name, err ? "not found" : "found, loading..."); 223 if (err) 224 return NULL; 225 226 return fw; 227 } 228 229 /** 230 * nfp_net_fw_find() - Find the correct firmware image for netdev mode 231 * @pdev: PCI Device structure 232 * @pf: NFP PF Device structure 233 * 234 * Return: firmware if found and requested successfully. 235 */ 236 static const struct firmware * 237 nfp_net_fw_find(struct pci_dev *pdev, struct nfp_pf *pf) 238 { 239 struct nfp_eth_table_port *port; 240 const struct firmware *fw; 241 const char *fw_model; 242 char fw_name[256]; 243 const u8 *serial; 244 u16 interface; 245 int spc, i, j; 246 247 nfp_info(pf->cpp, "Looking for firmware file in order of priority:\n"); 248 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); 255 if (fw) 256 return fw; 257 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); 261 if (fw) 262 return fw; 263 264 /* Finally try the card type and media */ 265 if (!pf->eth_tbl) { 266 dev_err(&pdev->dev, "Error: can't identify media config\n"); 267 return NULL; 268 } 269 270 fw_model = nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno"); 271 if (!fw_model) { 272 dev_err(&pdev->dev, "Error: can't read part number\n"); 273 return NULL; 274 } 275 276 spc = ARRAY_SIZE(fw_name); 277 spc -= snprintf(fw_name, spc, "netronome/nic_%s", fw_model); 278 279 for (i = 0; spc > 0 && i < pf->eth_tbl->count; i += j) { 280 port = &pf->eth_tbl->ports[i]; 281 j = 1; 282 while (i + j < pf->eth_tbl->count && 283 port->speed == port[j].speed) 284 j++; 285 286 spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc, 287 "_%dx%d", j, port->speed / 1000); 288 } 289 290 if (spc <= 0) 291 return NULL; 292 293 spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc, ".nffw"); 294 if (spc <= 0) 295 return NULL; 296 297 return nfp_net_fw_request(pdev, pf, fw_name); 298 } 299 300 /** 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 305 * 306 * Return: -ERRNO, 0 for no firmware loaded, 1 for firmware loaded 307 */ 308 static int 309 nfp_fw_load(struct pci_dev *pdev, struct nfp_pf *pf, struct nfp_nsp *nsp) 310 { 311 const struct firmware *fw; 312 u16 interface; 313 int err; 314 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"); 319 return 0; 320 } 321 322 fw = nfp_net_fw_find(pdev, pf); 323 if (!fw) 324 return 0; 325 326 dev_info(&pdev->dev, "Soft-reset, loading FW image\n"); 327 err = nfp_nsp_device_soft_reset(nsp); 328 if (err < 0) { 329 dev_err(&pdev->dev, "Failed to soft reset the NFP: %d\n", 330 err); 331 goto exit_release_fw; 332 } 333 334 err = nfp_nsp_load_fw(nsp, fw); 335 336 if (err < 0) { 337 dev_err(&pdev->dev, "FW loading failed: %d\n", err); 338 goto exit_release_fw; 339 } 340 341 dev_info(&pdev->dev, "Finished loading FW image\n"); 342 343 exit_release_fw: 344 release_firmware(fw); 345 346 return err < 0 ? err : 1; 347 } 348 349 static void 350 nfp_nsp_init_ports(struct pci_dev *pdev, struct nfp_pf *pf, 351 struct nfp_nsp *nsp) 352 { 353 bool needs_reinit = false; 354 int i; 355 356 pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp); 357 if (!pf->eth_tbl) 358 return; 359 360 if (!nfp_nsp_has_mac_reinit(nsp)) 361 return; 362 363 for (i = 0; i < pf->eth_tbl->count; i++) 364 needs_reinit |= pf->eth_tbl->ports[i].override_changed; 365 if (!needs_reinit) 366 return; 367 368 kfree(pf->eth_tbl); 369 if (nfp_nsp_mac_reinit(nsp)) 370 dev_warn(&pdev->dev, "MAC reinit failed\n"); 371 372 pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp); 373 } 374 375 static int nfp_nsp_init(struct pci_dev *pdev, struct nfp_pf *pf) 376 { 377 struct nfp_nsp *nsp; 378 int err; 379 380 err = nfp_resource_wait(pf->cpp, NFP_RESOURCE_NSP, 30); 381 if (err) 382 return err; 383 384 nsp = nfp_nsp_open(pf->cpp); 385 if (IS_ERR(nsp)) { 386 err = PTR_ERR(nsp); 387 dev_err(&pdev->dev, "Failed to access the NSP: %d\n", err); 388 return err; 389 } 390 391 err = nfp_nsp_wait(nsp); 392 if (err < 0) 393 goto exit_close_nsp; 394 395 nfp_nsp_init_ports(pdev, pf, nsp); 396 397 pf->nspi = __nfp_nsp_identify(nsp); 398 if (pf->nspi) 399 dev_info(&pdev->dev, "BSP: %s\n", pf->nspi->version); 400 401 err = nfp_fw_load(pdev, pf, nsp); 402 if (err < 0) { 403 kfree(pf->nspi); 404 kfree(pf->eth_tbl); 405 dev_err(&pdev->dev, "Failed to load FW\n"); 406 goto exit_close_nsp; 407 } 408 409 pf->fw_loaded = !!err; 410 err = 0; 411 412 exit_close_nsp: 413 nfp_nsp_close(nsp); 414 415 return err; 416 } 417 418 static void nfp_fw_unload(struct nfp_pf *pf) 419 { 420 struct nfp_nsp *nsp; 421 int err; 422 423 nsp = nfp_nsp_open(pf->cpp); 424 if (IS_ERR(nsp)) { 425 nfp_err(pf->cpp, "Reset failed, can't open NSP\n"); 426 return; 427 } 428 429 err = nfp_nsp_device_soft_reset(nsp); 430 if (err < 0) 431 dev_warn(&pf->pdev->dev, "Couldn't unload firmware: %d\n", err); 432 else 433 dev_info(&pf->pdev->dev, "Firmware safely unloaded\n"); 434 435 nfp_nsp_close(nsp); 436 } 437 438 static int nfp_pci_probe(struct pci_dev *pdev, 439 const struct pci_device_id *pci_id) 440 { 441 struct devlink *devlink; 442 struct nfp_pf *pf; 443 int err; 444 445 err = pci_enable_device(pdev); 446 if (err < 0) 447 return err; 448 449 pci_set_master(pdev); 450 451 err = dma_set_mask_and_coherent(&pdev->dev, 452 DMA_BIT_MASK(NFP_NET_MAX_DMA_BITS)); 453 if (err) 454 goto err_pci_disable; 455 456 err = pci_request_regions(pdev, nfp_driver_name); 457 if (err < 0) { 458 dev_err(&pdev->dev, "Unable to reserve pci resources.\n"); 459 goto err_pci_disable; 460 } 461 462 devlink = devlink_alloc(&nfp_devlink_ops, sizeof(*pf)); 463 if (!devlink) { 464 err = -ENOMEM; 465 goto err_rel_regions; 466 } 467 pf = devlink_priv(devlink); 468 INIT_LIST_HEAD(&pf->vnics); 469 INIT_LIST_HEAD(&pf->ports); 470 mutex_init(&pf->lock); 471 pci_set_drvdata(pdev, pf); 472 pf->pdev = pdev; 473 474 pf->wq = alloc_workqueue("nfp-%s", 0, 2, pci_name(pdev)); 475 if (!pf->wq) { 476 err = -ENOMEM; 477 goto err_pci_priv_unset; 478 } 479 480 pf->cpp = nfp_cpp_from_nfp6000_pcie(pdev); 481 if (IS_ERR_OR_NULL(pf->cpp)) { 482 err = PTR_ERR(pf->cpp); 483 if (err >= 0) 484 err = -ENOMEM; 485 goto err_disable_msix; 486 } 487 488 pf->hwinfo = nfp_hwinfo_read(pf->cpp); 489 490 dev_info(&pdev->dev, "Assembly: %s%s%s-%s CPLD: %s\n", 491 nfp_hwinfo_lookup(pf->hwinfo, "assembly.vendor"), 492 nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno"), 493 nfp_hwinfo_lookup(pf->hwinfo, "assembly.serial"), 494 nfp_hwinfo_lookup(pf->hwinfo, "assembly.revision"), 495 nfp_hwinfo_lookup(pf->hwinfo, "cpld.version")); 496 497 err = nfp_pf_board_state_wait(pf); 498 if (err) 499 goto err_hwinfo_free; 500 501 err = devlink_register(devlink, &pdev->dev); 502 if (err) 503 goto err_hwinfo_free; 504 505 err = nfp_nsp_init(pdev, pf); 506 if (err) 507 goto err_devlink_unreg; 508 509 pf->mip = nfp_mip_open(pf->cpp); 510 pf->rtbl = __nfp_rtsym_table_read(pf->cpp, pf->mip); 511 512 err = nfp_pcie_sriov_read_nfd_limit(pf); 513 if (err) 514 goto err_fw_unload; 515 516 pf->num_vfs = pci_num_vf(pdev); 517 if (pf->num_vfs > pf->limit_vfs) { 518 dev_err(&pdev->dev, 519 "Error: %d VFs already enabled, but loaded FW can only support %d\n", 520 pf->num_vfs, pf->limit_vfs); 521 goto err_fw_unload; 522 } 523 524 err = nfp_net_pci_probe(pf); 525 if (err) 526 goto err_sriov_unlimit; 527 528 err = nfp_hwmon_register(pf); 529 if (err) { 530 dev_err(&pdev->dev, "Failed to register hwmon info\n"); 531 goto err_net_remove; 532 } 533 534 return 0; 535 536 err_net_remove: 537 nfp_net_pci_remove(pf); 538 err_sriov_unlimit: 539 pci_sriov_set_totalvfs(pf->pdev, 0); 540 err_fw_unload: 541 kfree(pf->rtbl); 542 nfp_mip_close(pf->mip); 543 if (pf->fw_loaded) 544 nfp_fw_unload(pf); 545 kfree(pf->eth_tbl); 546 kfree(pf->nspi); 547 err_devlink_unreg: 548 devlink_unregister(devlink); 549 err_hwinfo_free: 550 kfree(pf->hwinfo); 551 nfp_cpp_free(pf->cpp); 552 err_disable_msix: 553 destroy_workqueue(pf->wq); 554 err_pci_priv_unset: 555 pci_set_drvdata(pdev, NULL); 556 mutex_destroy(&pf->lock); 557 devlink_free(devlink); 558 err_rel_regions: 559 pci_release_regions(pdev); 560 err_pci_disable: 561 pci_disable_device(pdev); 562 563 return err; 564 } 565 566 static void nfp_pci_remove(struct pci_dev *pdev) 567 { 568 struct nfp_pf *pf = pci_get_drvdata(pdev); 569 struct devlink *devlink; 570 571 nfp_hwmon_unregister(pf); 572 573 devlink = priv_to_devlink(pf); 574 575 nfp_net_pci_remove(pf); 576 577 nfp_pcie_sriov_disable(pdev); 578 pci_sriov_set_totalvfs(pf->pdev, 0); 579 580 devlink_unregister(devlink); 581 582 kfree(pf->rtbl); 583 nfp_mip_close(pf->mip); 584 if (pf->fw_loaded) 585 nfp_fw_unload(pf); 586 587 destroy_workqueue(pf->wq); 588 pci_set_drvdata(pdev, NULL); 589 kfree(pf->hwinfo); 590 nfp_cpp_free(pf->cpp); 591 592 kfree(pf->eth_tbl); 593 kfree(pf->nspi); 594 mutex_destroy(&pf->lock); 595 devlink_free(devlink); 596 pci_release_regions(pdev); 597 pci_disable_device(pdev); 598 } 599 600 static struct pci_driver nfp_pci_driver = { 601 .name = nfp_driver_name, 602 .id_table = nfp_pci_device_ids, 603 .probe = nfp_pci_probe, 604 .remove = nfp_pci_remove, 605 .sriov_configure = nfp_pcie_sriov_configure, 606 }; 607 608 static int __init nfp_main_init(void) 609 { 610 int err; 611 612 pr_info("%s: NFP PCIe Driver, Copyright (C) 2014-2017 Netronome Systems\n", 613 nfp_driver_name); 614 615 nfp_net_debugfs_create(); 616 617 err = pci_register_driver(&nfp_pci_driver); 618 if (err < 0) 619 goto err_destroy_debugfs; 620 621 err = pci_register_driver(&nfp_netvf_pci_driver); 622 if (err) 623 goto err_unreg_pf; 624 625 return err; 626 627 err_unreg_pf: 628 pci_unregister_driver(&nfp_pci_driver); 629 err_destroy_debugfs: 630 nfp_net_debugfs_destroy(); 631 return err; 632 } 633 634 static void __exit nfp_main_exit(void) 635 { 636 pci_unregister_driver(&nfp_netvf_pci_driver); 637 pci_unregister_driver(&nfp_pci_driver); 638 nfp_net_debugfs_destroy(); 639 } 640 641 module_init(nfp_main_init); 642 module_exit(nfp_main_exit); 643 644 MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_1x40.nffw"); 645 MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_4x10.nffw"); 646 MODULE_FIRMWARE("netronome/nic_AMDA0096-0001_2x10.nffw"); 647 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_2x40.nffw"); 648 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_4x10_1x40.nffw"); 649 MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_8x10.nffw"); 650 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x10.nffw"); 651 MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x25.nffw"); 652 653 MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>"); 654 MODULE_LICENSE("GPL"); 655 MODULE_DESCRIPTION("The Netronome Flow Processor (NFP) driver."); 656