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_net_main.c 36 * Netronome network device driver: Main entry point 37 * Authors: Jakub Kicinski <jakub.kicinski@netronome.com> 38 * Alejandro Lucero <alejandro.lucero@netronome.com> 39 * Jason McMullan <jason.mcmullan@netronome.com> 40 * Rolf Neugebauer <rolf.neugebauer@netronome.com> 41 */ 42 43 #include <linux/etherdevice.h> 44 #include <linux/kernel.h> 45 #include <linux/init.h> 46 #include <linux/lockdep.h> 47 #include <linux/pci.h> 48 #include <linux/pci_regs.h> 49 #include <linux/msi.h> 50 #include <linux/random.h> 51 #include <linux/rtnetlink.h> 52 53 #include "nfpcore/nfp.h" 54 #include "nfpcore/nfp_cpp.h" 55 #include "nfpcore/nfp_nffw.h" 56 #include "nfpcore/nfp_nsp.h" 57 #include "nfpcore/nfp6000_pcie.h" 58 #include "nfp_app.h" 59 #include "nfp_net_ctrl.h" 60 #include "nfp_net_sriov.h" 61 #include "nfp_net.h" 62 #include "nfp_main.h" 63 #include "nfp_port.h" 64 65 #define NFP_PF_CSR_SLICE_SIZE (32 * 1024) 66 67 /** 68 * nfp_net_get_mac_addr() - Get the MAC address. 69 * @pf: NFP PF handle 70 * @port: NFP port structure 71 * 72 * First try to get the MAC address from NSP ETH table. If that 73 * fails generate a random address. 74 */ 75 void nfp_net_get_mac_addr(struct nfp_pf *pf, struct nfp_port *port) 76 { 77 struct nfp_eth_table_port *eth_port; 78 79 eth_port = __nfp_port_get_eth_port(port); 80 if (!eth_port) { 81 eth_hw_addr_random(port->netdev); 82 return; 83 } 84 85 ether_addr_copy(port->netdev->dev_addr, eth_port->mac_addr); 86 ether_addr_copy(port->netdev->perm_addr, eth_port->mac_addr); 87 } 88 89 static struct nfp_eth_table_port * 90 nfp_net_find_port(struct nfp_eth_table *eth_tbl, unsigned int index) 91 { 92 int i; 93 94 for (i = 0; eth_tbl && i < eth_tbl->count; i++) 95 if (eth_tbl->ports[i].index == index) 96 return ð_tbl->ports[i]; 97 98 return NULL; 99 } 100 101 static int 102 nfp_net_pf_rtsym_read_optional(struct nfp_pf *pf, const char *format, 103 unsigned int default_val) 104 { 105 char name[256]; 106 int err = 0; 107 u64 val; 108 109 snprintf(name, sizeof(name), format, nfp_cppcore_pcie_unit(pf->cpp)); 110 111 val = nfp_rtsym_read_le(pf->rtbl, name, &err); 112 if (err) { 113 if (err == -ENOENT) 114 return default_val; 115 nfp_err(pf->cpp, "Unable to read symbol %s\n", name); 116 return err; 117 } 118 119 return val; 120 } 121 122 static int nfp_net_pf_get_num_ports(struct nfp_pf *pf) 123 { 124 return nfp_net_pf_rtsym_read_optional(pf, "nfd_cfg_pf%u_num_ports", 1); 125 } 126 127 static int nfp_net_pf_get_app_id(struct nfp_pf *pf) 128 { 129 return nfp_net_pf_rtsym_read_optional(pf, "_pf%u_net_app_id", 130 NFP_APP_CORE_NIC); 131 } 132 133 static u8 __iomem * 134 nfp_net_pf_map_rtsym(struct nfp_pf *pf, const char *name, const char *sym_fmt, 135 unsigned int min_size, struct nfp_cpp_area **area) 136 { 137 char pf_symbol[256]; 138 139 snprintf(pf_symbol, sizeof(pf_symbol), sym_fmt, 140 nfp_cppcore_pcie_unit(pf->cpp)); 141 142 return nfp_rtsym_map(pf->rtbl, pf_symbol, name, min_size, area); 143 } 144 145 static void nfp_net_pf_free_vnic(struct nfp_pf *pf, struct nfp_net *nn) 146 { 147 if (nfp_net_is_data_vnic(nn)) 148 nfp_app_vnic_free(pf->app, nn); 149 nfp_port_free(nn->port); 150 list_del(&nn->vnic_list); 151 pf->num_vnics--; 152 nfp_net_free(nn); 153 } 154 155 static void nfp_net_pf_free_vnics(struct nfp_pf *pf) 156 { 157 struct nfp_net *nn, *next; 158 159 list_for_each_entry_safe(nn, next, &pf->vnics, vnic_list) 160 if (nfp_net_is_data_vnic(nn)) 161 nfp_net_pf_free_vnic(pf, nn); 162 } 163 164 static struct nfp_net * 165 nfp_net_pf_alloc_vnic(struct nfp_pf *pf, bool needs_netdev, 166 void __iomem *ctrl_bar, void __iomem *qc_bar, 167 int stride, unsigned int id) 168 { 169 u32 tx_base, rx_base, n_tx_rings, n_rx_rings; 170 struct nfp_net *nn; 171 int err; 172 173 tx_base = readl(ctrl_bar + NFP_NET_CFG_START_TXQ); 174 rx_base = readl(ctrl_bar + NFP_NET_CFG_START_RXQ); 175 n_tx_rings = readl(ctrl_bar + NFP_NET_CFG_MAX_TXRINGS); 176 n_rx_rings = readl(ctrl_bar + NFP_NET_CFG_MAX_RXRINGS); 177 178 /* Allocate and initialise the vNIC */ 179 nn = nfp_net_alloc(pf->pdev, needs_netdev, n_tx_rings, n_rx_rings); 180 if (IS_ERR(nn)) 181 return nn; 182 183 nn->app = pf->app; 184 nfp_net_get_fw_version(&nn->fw_ver, ctrl_bar); 185 nn->dp.ctrl_bar = ctrl_bar; 186 nn->tx_bar = qc_bar + tx_base * NFP_QCP_QUEUE_ADDR_SZ; 187 nn->rx_bar = qc_bar + rx_base * NFP_QCP_QUEUE_ADDR_SZ; 188 nn->dp.is_vf = 0; 189 nn->stride_rx = stride; 190 nn->stride_tx = stride; 191 192 if (needs_netdev) { 193 err = nfp_app_vnic_alloc(pf->app, nn, id); 194 if (err) { 195 nfp_net_free(nn); 196 return ERR_PTR(err); 197 } 198 } 199 200 pf->num_vnics++; 201 list_add_tail(&nn->vnic_list, &pf->vnics); 202 203 return nn; 204 } 205 206 static int 207 nfp_net_pf_init_vnic(struct nfp_pf *pf, struct nfp_net *nn, unsigned int id) 208 { 209 int err; 210 211 /* Get ME clock frequency from ctrl BAR 212 * XXX for now frequency is hardcoded until we figure out how 213 * to get the value from nfp-hwinfo into ctrl bar 214 */ 215 nn->me_freq_mhz = 1200; 216 217 err = nfp_net_init(nn); 218 if (err) 219 return err; 220 221 nfp_net_debugfs_vnic_add(nn, pf->ddir, id); 222 223 if (nn->port) { 224 err = nfp_devlink_port_register(pf->app, nn->port); 225 if (err) 226 goto err_dfs_clean; 227 } 228 229 nfp_net_info(nn); 230 231 if (nfp_net_is_data_vnic(nn)) { 232 err = nfp_app_vnic_init(pf->app, nn); 233 if (err) 234 goto err_devlink_port_clean; 235 } 236 237 return 0; 238 239 err_devlink_port_clean: 240 if (nn->port) 241 nfp_devlink_port_unregister(nn->port); 242 err_dfs_clean: 243 nfp_net_debugfs_dir_clean(&nn->debugfs_dir); 244 nfp_net_clean(nn); 245 return err; 246 } 247 248 static int 249 nfp_net_pf_alloc_vnics(struct nfp_pf *pf, void __iomem *ctrl_bar, 250 void __iomem *qc_bar, int stride) 251 { 252 struct nfp_net *nn; 253 unsigned int i; 254 int err; 255 256 for (i = 0; i < pf->max_data_vnics; i++) { 257 nn = nfp_net_pf_alloc_vnic(pf, true, ctrl_bar, qc_bar, 258 stride, i); 259 if (IS_ERR(nn)) { 260 err = PTR_ERR(nn); 261 goto err_free_prev; 262 } 263 264 ctrl_bar += NFP_PF_CSR_SLICE_SIZE; 265 266 /* Kill the vNIC if app init marked it as invalid */ 267 if (nn->port && nn->port->type == NFP_PORT_INVALID) { 268 nfp_net_pf_free_vnic(pf, nn); 269 continue; 270 } 271 } 272 273 if (list_empty(&pf->vnics)) 274 return -ENODEV; 275 276 return 0; 277 278 err_free_prev: 279 nfp_net_pf_free_vnics(pf); 280 return err; 281 } 282 283 static void nfp_net_pf_clean_vnic(struct nfp_pf *pf, struct nfp_net *nn) 284 { 285 if (nfp_net_is_data_vnic(nn)) 286 nfp_app_vnic_clean(pf->app, nn); 287 if (nn->port) 288 nfp_devlink_port_unregister(nn->port); 289 nfp_net_debugfs_dir_clean(&nn->debugfs_dir); 290 nfp_net_clean(nn); 291 } 292 293 static int nfp_net_pf_alloc_irqs(struct nfp_pf *pf) 294 { 295 unsigned int wanted_irqs, num_irqs, vnics_left, irqs_left; 296 struct nfp_net *nn; 297 298 /* Get MSI-X vectors */ 299 wanted_irqs = 0; 300 list_for_each_entry(nn, &pf->vnics, vnic_list) 301 wanted_irqs += NFP_NET_NON_Q_VECTORS + nn->dp.num_r_vecs; 302 pf->irq_entries = kcalloc(wanted_irqs, sizeof(*pf->irq_entries), 303 GFP_KERNEL); 304 if (!pf->irq_entries) 305 return -ENOMEM; 306 307 num_irqs = nfp_net_irqs_alloc(pf->pdev, pf->irq_entries, 308 NFP_NET_MIN_VNIC_IRQS * pf->num_vnics, 309 wanted_irqs); 310 if (!num_irqs) { 311 nfp_warn(pf->cpp, "Unable to allocate MSI-X vectors\n"); 312 kfree(pf->irq_entries); 313 return -ENOMEM; 314 } 315 316 /* Distribute IRQs to vNICs */ 317 irqs_left = num_irqs; 318 vnics_left = pf->num_vnics; 319 list_for_each_entry(nn, &pf->vnics, vnic_list) { 320 unsigned int n; 321 322 n = min(NFP_NET_NON_Q_VECTORS + nn->dp.num_r_vecs, 323 DIV_ROUND_UP(irqs_left, vnics_left)); 324 nfp_net_irqs_assign(nn, &pf->irq_entries[num_irqs - irqs_left], 325 n); 326 irqs_left -= n; 327 vnics_left--; 328 } 329 330 return 0; 331 } 332 333 static void nfp_net_pf_free_irqs(struct nfp_pf *pf) 334 { 335 nfp_net_irqs_disable(pf->pdev); 336 kfree(pf->irq_entries); 337 } 338 339 static int nfp_net_pf_init_vnics(struct nfp_pf *pf) 340 { 341 struct nfp_net *nn; 342 unsigned int id; 343 int err; 344 345 /* Finish vNIC init and register */ 346 id = 0; 347 list_for_each_entry(nn, &pf->vnics, vnic_list) { 348 if (!nfp_net_is_data_vnic(nn)) 349 continue; 350 err = nfp_net_pf_init_vnic(pf, nn, id); 351 if (err) 352 goto err_prev_deinit; 353 354 id++; 355 } 356 357 return 0; 358 359 err_prev_deinit: 360 list_for_each_entry_continue_reverse(nn, &pf->vnics, vnic_list) 361 if (nfp_net_is_data_vnic(nn)) 362 nfp_net_pf_clean_vnic(pf, nn); 363 return err; 364 } 365 366 static int 367 nfp_net_pf_app_init(struct nfp_pf *pf, u8 __iomem *qc_bar, unsigned int stride) 368 { 369 u8 __iomem *ctrl_bar; 370 int err; 371 372 pf->app = nfp_app_alloc(pf, nfp_net_pf_get_app_id(pf)); 373 if (IS_ERR(pf->app)) 374 return PTR_ERR(pf->app); 375 376 err = nfp_app_init(pf->app); 377 if (err) 378 goto err_free; 379 380 if (!nfp_app_needs_ctrl_vnic(pf->app)) 381 return 0; 382 383 ctrl_bar = nfp_net_pf_map_rtsym(pf, "net.ctrl", "_pf%u_net_ctrl_bar", 384 NFP_PF_CSR_SLICE_SIZE, 385 &pf->ctrl_vnic_bar); 386 if (IS_ERR(ctrl_bar)) { 387 nfp_err(pf->cpp, "Failed to find ctrl vNIC memory symbol\n"); 388 err = PTR_ERR(ctrl_bar); 389 goto err_app_clean; 390 } 391 392 pf->ctrl_vnic = nfp_net_pf_alloc_vnic(pf, false, ctrl_bar, qc_bar, 393 stride, 0); 394 if (IS_ERR(pf->ctrl_vnic)) { 395 err = PTR_ERR(pf->ctrl_vnic); 396 goto err_unmap; 397 } 398 399 return 0; 400 401 err_unmap: 402 nfp_cpp_area_release_free(pf->ctrl_vnic_bar); 403 err_app_clean: 404 nfp_app_clean(pf->app); 405 err_free: 406 nfp_app_free(pf->app); 407 pf->app = NULL; 408 return err; 409 } 410 411 static void nfp_net_pf_app_clean(struct nfp_pf *pf) 412 { 413 if (pf->ctrl_vnic) { 414 nfp_net_pf_free_vnic(pf, pf->ctrl_vnic); 415 nfp_cpp_area_release_free(pf->ctrl_vnic_bar); 416 } 417 nfp_app_clean(pf->app); 418 nfp_app_free(pf->app); 419 pf->app = NULL; 420 } 421 422 static int nfp_net_pf_app_start_ctrl(struct nfp_pf *pf) 423 { 424 int err; 425 426 if (!pf->ctrl_vnic) 427 return 0; 428 err = nfp_net_pf_init_vnic(pf, pf->ctrl_vnic, 0); 429 if (err) 430 return err; 431 432 err = nfp_ctrl_open(pf->ctrl_vnic); 433 if (err) 434 goto err_clean_ctrl; 435 436 return 0; 437 438 err_clean_ctrl: 439 nfp_net_pf_clean_vnic(pf, pf->ctrl_vnic); 440 return err; 441 } 442 443 static void nfp_net_pf_app_stop_ctrl(struct nfp_pf *pf) 444 { 445 if (!pf->ctrl_vnic) 446 return; 447 nfp_ctrl_close(pf->ctrl_vnic); 448 nfp_net_pf_clean_vnic(pf, pf->ctrl_vnic); 449 } 450 451 static int nfp_net_pf_app_start(struct nfp_pf *pf) 452 { 453 int err; 454 455 err = nfp_net_pf_app_start_ctrl(pf); 456 if (err) 457 return err; 458 459 err = nfp_app_start(pf->app, pf->ctrl_vnic); 460 if (err) 461 goto err_ctrl_stop; 462 463 if (pf->num_vfs) { 464 err = nfp_app_sriov_enable(pf->app, pf->num_vfs); 465 if (err) 466 goto err_app_stop; 467 } 468 469 return 0; 470 471 err_app_stop: 472 nfp_app_stop(pf->app); 473 err_ctrl_stop: 474 nfp_net_pf_app_stop_ctrl(pf); 475 return err; 476 } 477 478 static void nfp_net_pf_app_stop(struct nfp_pf *pf) 479 { 480 if (pf->num_vfs) 481 nfp_app_sriov_disable(pf->app); 482 nfp_app_stop(pf->app); 483 nfp_net_pf_app_stop_ctrl(pf); 484 } 485 486 static void nfp_net_pci_unmap_mem(struct nfp_pf *pf) 487 { 488 if (pf->vfcfg_tbl2_area) 489 nfp_cpp_area_release_free(pf->vfcfg_tbl2_area); 490 if (pf->vf_cfg_bar) 491 nfp_cpp_area_release_free(pf->vf_cfg_bar); 492 if (pf->mac_stats_bar) 493 nfp_cpp_area_release_free(pf->mac_stats_bar); 494 nfp_cpp_area_release_free(pf->qc_area); 495 nfp_cpp_area_release_free(pf->data_vnic_bar); 496 } 497 498 static int nfp_net_pci_map_mem(struct nfp_pf *pf) 499 { 500 u8 __iomem *mem; 501 u32 min_size; 502 int err; 503 504 min_size = pf->max_data_vnics * NFP_PF_CSR_SLICE_SIZE; 505 mem = nfp_net_pf_map_rtsym(pf, "net.bar0", "_pf%d_net_bar0", 506 min_size, &pf->data_vnic_bar); 507 if (IS_ERR(mem)) { 508 nfp_err(pf->cpp, "Failed to find data vNIC memory symbol\n"); 509 return PTR_ERR(mem); 510 } 511 512 min_size = NFP_MAC_STATS_SIZE * (pf->eth_tbl->max_index + 1); 513 pf->mac_stats_mem = nfp_rtsym_map(pf->rtbl, "_mac_stats", 514 "net.macstats", min_size, 515 &pf->mac_stats_bar); 516 if (IS_ERR(pf->mac_stats_mem)) { 517 if (PTR_ERR(pf->mac_stats_mem) != -ENOENT) { 518 err = PTR_ERR(pf->mac_stats_mem); 519 goto err_unmap_ctrl; 520 } 521 pf->mac_stats_mem = NULL; 522 } 523 524 pf->vf_cfg_mem = nfp_net_pf_map_rtsym(pf, "net.vfcfg", 525 "_pf%d_net_vf_bar", 526 NFP_NET_CFG_BAR_SZ * 527 pf->limit_vfs, &pf->vf_cfg_bar); 528 if (IS_ERR(pf->vf_cfg_mem)) { 529 if (PTR_ERR(pf->vf_cfg_mem) != -ENOENT) { 530 err = PTR_ERR(pf->vf_cfg_mem); 531 goto err_unmap_mac_stats; 532 } 533 pf->vf_cfg_mem = NULL; 534 } 535 536 min_size = NFP_NET_VF_CFG_SZ * pf->limit_vfs + NFP_NET_VF_CFG_MB_SZ; 537 pf->vfcfg_tbl2 = nfp_net_pf_map_rtsym(pf, "net.vfcfg_tbl2", 538 "_pf%d_net_vf_cfg2", 539 min_size, &pf->vfcfg_tbl2_area); 540 if (IS_ERR(pf->vfcfg_tbl2)) { 541 if (PTR_ERR(pf->vfcfg_tbl2) != -ENOENT) { 542 err = PTR_ERR(pf->vfcfg_tbl2); 543 goto err_unmap_vf_cfg; 544 } 545 pf->vfcfg_tbl2 = NULL; 546 } 547 548 mem = nfp_cpp_map_area(pf->cpp, "net.qc", 0, 0, 549 NFP_PCIE_QUEUE(0), NFP_QCP_QUEUE_AREA_SZ, 550 &pf->qc_area); 551 if (IS_ERR(mem)) { 552 nfp_err(pf->cpp, "Failed to map Queue Controller area.\n"); 553 err = PTR_ERR(mem); 554 goto err_unmap_vfcfg_tbl2; 555 } 556 557 return 0; 558 559 err_unmap_vfcfg_tbl2: 560 if (pf->vfcfg_tbl2_area) 561 nfp_cpp_area_release_free(pf->vfcfg_tbl2_area); 562 err_unmap_vf_cfg: 563 if (pf->vf_cfg_bar) 564 nfp_cpp_area_release_free(pf->vf_cfg_bar); 565 err_unmap_mac_stats: 566 if (pf->mac_stats_bar) 567 nfp_cpp_area_release_free(pf->mac_stats_bar); 568 err_unmap_ctrl: 569 nfp_cpp_area_release_free(pf->data_vnic_bar); 570 return err; 571 } 572 573 static void nfp_net_pci_remove_finish(struct nfp_pf *pf) 574 { 575 nfp_net_pf_app_stop(pf); 576 /* stop app first, to avoid double free of ctrl vNIC's ddir */ 577 nfp_net_debugfs_dir_clean(&pf->ddir); 578 579 nfp_net_pf_free_irqs(pf); 580 nfp_net_pf_app_clean(pf); 581 nfp_net_pci_unmap_mem(pf); 582 } 583 584 static int 585 nfp_net_eth_port_update(struct nfp_cpp *cpp, struct nfp_port *port, 586 struct nfp_eth_table *eth_table) 587 { 588 struct nfp_eth_table_port *eth_port; 589 590 ASSERT_RTNL(); 591 592 eth_port = nfp_net_find_port(eth_table, port->eth_id); 593 if (!eth_port) { 594 set_bit(NFP_PORT_CHANGED, &port->flags); 595 nfp_warn(cpp, "Warning: port #%d not present after reconfig\n", 596 port->eth_id); 597 return -EIO; 598 } 599 if (eth_port->override_changed) { 600 nfp_warn(cpp, "Port #%d config changed, unregistering. Driver reload required before port will be operational again.\n", port->eth_id); 601 port->type = NFP_PORT_INVALID; 602 } 603 604 memcpy(port->eth_port, eth_port, sizeof(*eth_port)); 605 606 return 0; 607 } 608 609 int nfp_net_refresh_port_table_sync(struct nfp_pf *pf) 610 { 611 struct nfp_eth_table *eth_table; 612 struct nfp_net *nn, *next; 613 struct nfp_port *port; 614 int err; 615 616 lockdep_assert_held(&pf->lock); 617 618 /* Check for nfp_net_pci_remove() racing against us */ 619 if (list_empty(&pf->vnics)) 620 return 0; 621 622 /* Update state of all ports */ 623 rtnl_lock(); 624 list_for_each_entry(port, &pf->ports, port_list) 625 clear_bit(NFP_PORT_CHANGED, &port->flags); 626 627 eth_table = nfp_eth_read_ports(pf->cpp); 628 if (!eth_table) { 629 list_for_each_entry(port, &pf->ports, port_list) 630 if (__nfp_port_get_eth_port(port)) 631 set_bit(NFP_PORT_CHANGED, &port->flags); 632 rtnl_unlock(); 633 nfp_err(pf->cpp, "Error refreshing port config!\n"); 634 return -EIO; 635 } 636 637 list_for_each_entry(port, &pf->ports, port_list) 638 if (__nfp_port_get_eth_port(port)) 639 nfp_net_eth_port_update(pf->cpp, port, eth_table); 640 rtnl_unlock(); 641 642 kfree(eth_table); 643 644 /* Resync repr state. This may cause reprs to be removed. */ 645 err = nfp_reprs_resync_phys_ports(pf->app); 646 if (err) 647 return err; 648 649 /* Shoot off the ports which became invalid */ 650 list_for_each_entry_safe(nn, next, &pf->vnics, vnic_list) { 651 if (!nn->port || nn->port->type != NFP_PORT_INVALID) 652 continue; 653 654 nfp_net_pf_clean_vnic(pf, nn); 655 nfp_net_pf_free_vnic(pf, nn); 656 } 657 658 if (list_empty(&pf->vnics)) 659 nfp_net_pci_remove_finish(pf); 660 661 return 0; 662 } 663 664 static void nfp_net_refresh_vnics(struct work_struct *work) 665 { 666 struct nfp_pf *pf = container_of(work, struct nfp_pf, 667 port_refresh_work); 668 669 mutex_lock(&pf->lock); 670 nfp_net_refresh_port_table_sync(pf); 671 mutex_unlock(&pf->lock); 672 } 673 674 void nfp_net_refresh_port_table(struct nfp_port *port) 675 { 676 struct nfp_pf *pf = port->app->pf; 677 678 set_bit(NFP_PORT_CHANGED, &port->flags); 679 680 queue_work(pf->wq, &pf->port_refresh_work); 681 } 682 683 int nfp_net_refresh_eth_port(struct nfp_port *port) 684 { 685 struct nfp_cpp *cpp = port->app->cpp; 686 struct nfp_eth_table *eth_table; 687 int ret; 688 689 clear_bit(NFP_PORT_CHANGED, &port->flags); 690 691 eth_table = nfp_eth_read_ports(cpp); 692 if (!eth_table) { 693 set_bit(NFP_PORT_CHANGED, &port->flags); 694 nfp_err(cpp, "Error refreshing port state table!\n"); 695 return -EIO; 696 } 697 698 ret = nfp_net_eth_port_update(cpp, port, eth_table); 699 700 kfree(eth_table); 701 702 return ret; 703 } 704 705 /* 706 * PCI device functions 707 */ 708 int nfp_net_pci_probe(struct nfp_pf *pf) 709 { 710 struct nfp_net_fw_version fw_ver; 711 u8 __iomem *ctrl_bar, *qc_bar; 712 int stride; 713 int err; 714 715 INIT_WORK(&pf->port_refresh_work, nfp_net_refresh_vnics); 716 717 if (!pf->rtbl) { 718 nfp_err(pf->cpp, "No %s, giving up.\n", 719 pf->fw_loaded ? "symbol table" : "firmware found"); 720 return -EINVAL; 721 } 722 723 mutex_lock(&pf->lock); 724 pf->max_data_vnics = nfp_net_pf_get_num_ports(pf); 725 if ((int)pf->max_data_vnics < 0) { 726 err = pf->max_data_vnics; 727 goto err_unlock; 728 } 729 730 err = nfp_net_pci_map_mem(pf); 731 if (err) 732 goto err_unlock; 733 734 ctrl_bar = nfp_cpp_area_iomem(pf->data_vnic_bar); 735 qc_bar = nfp_cpp_area_iomem(pf->qc_area); 736 if (!ctrl_bar || !qc_bar) { 737 err = -EIO; 738 goto err_unmap; 739 } 740 741 nfp_net_get_fw_version(&fw_ver, ctrl_bar); 742 if (fw_ver.resv || fw_ver.class != NFP_NET_CFG_VERSION_CLASS_GENERIC) { 743 nfp_err(pf->cpp, "Unknown Firmware ABI %d.%d.%d.%d\n", 744 fw_ver.resv, fw_ver.class, fw_ver.major, fw_ver.minor); 745 err = -EINVAL; 746 goto err_unmap; 747 } 748 749 /* Determine stride */ 750 if (nfp_net_fw_ver_eq(&fw_ver, 0, 0, 0, 1)) { 751 stride = 2; 752 nfp_warn(pf->cpp, "OBSOLETE Firmware detected - VF isolation not available\n"); 753 } else { 754 switch (fw_ver.major) { 755 case 1 ... 5: 756 stride = 4; 757 break; 758 default: 759 nfp_err(pf->cpp, "Unsupported Firmware ABI %d.%d.%d.%d\n", 760 fw_ver.resv, fw_ver.class, 761 fw_ver.major, fw_ver.minor); 762 err = -EINVAL; 763 goto err_unmap; 764 } 765 } 766 767 err = nfp_net_pf_app_init(pf, qc_bar, stride); 768 if (err) 769 goto err_unmap; 770 771 pf->ddir = nfp_net_debugfs_device_add(pf->pdev); 772 773 /* Allocate the vnics and do basic init */ 774 err = nfp_net_pf_alloc_vnics(pf, ctrl_bar, qc_bar, stride); 775 if (err) 776 goto err_clean_ddir; 777 778 err = nfp_net_pf_alloc_irqs(pf); 779 if (err) 780 goto err_free_vnics; 781 782 err = nfp_net_pf_app_start(pf); 783 if (err) 784 goto err_free_irqs; 785 786 err = nfp_net_pf_init_vnics(pf); 787 if (err) 788 goto err_stop_app; 789 790 mutex_unlock(&pf->lock); 791 792 return 0; 793 794 err_stop_app: 795 nfp_net_pf_app_stop(pf); 796 err_free_irqs: 797 nfp_net_pf_free_irqs(pf); 798 err_free_vnics: 799 nfp_net_pf_free_vnics(pf); 800 err_clean_ddir: 801 nfp_net_debugfs_dir_clean(&pf->ddir); 802 nfp_net_pf_app_clean(pf); 803 err_unmap: 804 nfp_net_pci_unmap_mem(pf); 805 err_unlock: 806 mutex_unlock(&pf->lock); 807 cancel_work_sync(&pf->port_refresh_work); 808 return err; 809 } 810 811 void nfp_net_pci_remove(struct nfp_pf *pf) 812 { 813 struct nfp_net *nn; 814 815 mutex_lock(&pf->lock); 816 if (list_empty(&pf->vnics)) 817 goto out; 818 819 list_for_each_entry(nn, &pf->vnics, vnic_list) 820 if (nfp_net_is_data_vnic(nn)) 821 nfp_net_pf_clean_vnic(pf, nn); 822 823 nfp_net_pf_free_vnics(pf); 824 825 nfp_net_pci_remove_finish(pf); 826 out: 827 mutex_unlock(&pf->lock); 828 829 cancel_work_sync(&pf->port_refresh_work); 830 } 831