1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2015-2018 Netronome Systems, Inc. */
6 * Netronome network device driver: Main entry point
7 * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
8 * Alejandro Lucero <alejandro.lucero@netronome.com>
9 * Jason McMullan <jason.mcmullan@netronome.com>
10 * Rolf Neugebauer <rolf.neugebauer@netronome.com>
13 #include <linux/etherdevice.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/lockdep.h>
17 #include <linux/pci.h>
18 #include <linux/pci_regs.h>
19 #include <linux/msi.h>
20 #include <linux/random.h>
21 #include <linux/rtnetlink.h>
23 #include "nfpcore/nfp.h"
24 #include "nfpcore/nfp_cpp.h"
25 #include "nfpcore/nfp_dev.h"
26 #include "nfpcore/nfp_nffw.h"
27 #include "nfpcore/nfp_nsp.h"
28 #include "nfpcore/nfp6000_pcie.h"
30 #include "nfp_net_ctrl.h"
31 #include "nfp_net_sriov.h"
36 #define NFP_PF_CSR_SLICE_SIZE (32 * 1024)
39 * nfp_net_get_mac_addr() - Get the MAC address.
41 * @netdev: net_device to set MAC address on
42 * @port: NFP port structure
44 * First try to get the MAC address from NSP ETH table. If that
45 * fails generate a random address.
48 nfp_net_get_mac_addr(struct nfp_pf *pf, struct net_device *netdev,
49 struct nfp_port *port)
51 struct nfp_eth_table_port *eth_port;
53 eth_port = __nfp_port_get_eth_port(port);
55 eth_hw_addr_random(netdev);
59 eth_hw_addr_set(netdev, eth_port->mac_addr);
60 ether_addr_copy(netdev->perm_addr, eth_port->mac_addr);
63 static struct nfp_eth_table_port *
64 nfp_net_find_port(struct nfp_eth_table *eth_tbl, unsigned int index)
68 for (i = 0; eth_tbl && i < eth_tbl->count; i++)
69 if (eth_tbl->ports[i].index == index)
70 return ð_tbl->ports[i];
75 static int nfp_net_pf_get_num_ports(struct nfp_pf *pf)
77 return nfp_pf_rtsym_read_optional(pf, "nfd_cfg_pf%u_num_ports", 1);
80 static int nfp_net_pf_get_app_id(struct nfp_pf *pf)
82 return nfp_pf_rtsym_read_optional(pf, "_pf%u_net_app_id",
86 static void nfp_net_pf_free_vnic(struct nfp_pf *pf, struct nfp_net *nn)
88 if (nfp_net_is_data_vnic(nn))
89 nfp_app_vnic_free(pf->app, nn);
90 nfp_port_free(nn->port);
91 list_del(&nn->vnic_list);
96 static void nfp_net_pf_free_vnics(struct nfp_pf *pf)
98 struct nfp_net *nn, *next;
100 list_for_each_entry_safe(nn, next, &pf->vnics, vnic_list)
101 if (nfp_net_is_data_vnic(nn))
102 nfp_net_pf_free_vnic(pf, nn);
105 static struct nfp_net *
106 nfp_net_pf_alloc_vnic(struct nfp_pf *pf, bool needs_netdev,
107 void __iomem *ctrl_bar, void __iomem *qc_bar,
108 int stride, unsigned int id)
110 u32 tx_base, rx_base, n_tx_rings, n_rx_rings;
114 tx_base = readl(ctrl_bar + NFP_NET_CFG_START_TXQ);
115 rx_base = readl(ctrl_bar + NFP_NET_CFG_START_RXQ);
116 n_tx_rings = readl(ctrl_bar + NFP_NET_CFG_MAX_TXRINGS);
117 n_rx_rings = readl(ctrl_bar + NFP_NET_CFG_MAX_RXRINGS);
119 /* Allocate and initialise the vNIC */
120 nn = nfp_net_alloc(pf->pdev, pf->dev_info, ctrl_bar, needs_netdev,
121 n_tx_rings, n_rx_rings);
126 nn->tx_bar = qc_bar + tx_base * NFP_QCP_QUEUE_ADDR_SZ;
127 nn->rx_bar = qc_bar + rx_base * NFP_QCP_QUEUE_ADDR_SZ;
129 nn->stride_rx = stride;
130 nn->stride_tx = stride;
133 err = nfp_app_vnic_alloc(pf->app, nn, id);
141 list_add_tail(&nn->vnic_list, &pf->vnics);
147 nfp_net_pf_init_vnic(struct nfp_pf *pf, struct nfp_net *nn, unsigned int id)
154 err = nfp_devlink_port_register(pf->app, nn->port);
159 err = nfp_net_init(nn);
161 goto err_devlink_port_clean;
163 nfp_net_debugfs_vnic_add(nn, pf->ddir);
166 nfp_devlink_port_type_eth_set(nn->port);
170 if (nfp_net_is_data_vnic(nn)) {
171 err = nfp_app_vnic_init(pf->app, nn);
173 goto err_devlink_port_type_clean;
178 err_devlink_port_type_clean:
180 nfp_devlink_port_type_clear(nn->port);
181 nfp_net_debugfs_dir_clean(&nn->debugfs_dir);
183 err_devlink_port_clean:
185 nfp_devlink_port_unregister(nn->port);
190 nfp_net_pf_alloc_vnics(struct nfp_pf *pf, void __iomem *ctrl_bar,
191 void __iomem *qc_bar, int stride)
197 for (i = 0; i < pf->max_data_vnics; i++) {
198 nn = nfp_net_pf_alloc_vnic(pf, true, ctrl_bar, qc_bar,
205 ctrl_bar += NFP_PF_CSR_SLICE_SIZE;
207 /* Kill the vNIC if app init marked it as invalid */
208 if (nn->port && nn->port->type == NFP_PORT_INVALID)
209 nfp_net_pf_free_vnic(pf, nn);
212 if (list_empty(&pf->vnics))
218 nfp_net_pf_free_vnics(pf);
222 static void nfp_net_pf_clean_vnic(struct nfp_pf *pf, struct nfp_net *nn)
224 if (nfp_net_is_data_vnic(nn))
225 nfp_app_vnic_clean(pf->app, nn);
227 nfp_devlink_port_type_clear(nn->port);
228 nfp_net_debugfs_dir_clean(&nn->debugfs_dir);
231 nfp_devlink_port_unregister(nn->port);
234 static int nfp_net_pf_alloc_irqs(struct nfp_pf *pf)
236 unsigned int wanted_irqs, num_irqs, vnics_left, irqs_left;
239 /* Get MSI-X vectors */
241 list_for_each_entry(nn, &pf->vnics, vnic_list)
242 wanted_irqs += NFP_NET_NON_Q_VECTORS + nn->dp.num_r_vecs;
243 pf->irq_entries = kcalloc(wanted_irqs, sizeof(*pf->irq_entries),
245 if (!pf->irq_entries)
248 num_irqs = nfp_net_irqs_alloc(pf->pdev, pf->irq_entries,
249 NFP_NET_MIN_VNIC_IRQS * pf->num_vnics,
252 nfp_warn(pf->cpp, "Unable to allocate MSI-X vectors\n");
253 kfree(pf->irq_entries);
257 /* Distribute IRQs to vNICs */
258 irqs_left = num_irqs;
259 vnics_left = pf->num_vnics;
260 list_for_each_entry(nn, &pf->vnics, vnic_list) {
263 n = min(NFP_NET_NON_Q_VECTORS + nn->dp.num_r_vecs,
264 DIV_ROUND_UP(irqs_left, vnics_left));
265 nfp_net_irqs_assign(nn, &pf->irq_entries[num_irqs - irqs_left],
274 static void nfp_net_pf_free_irqs(struct nfp_pf *pf)
276 nfp_net_irqs_disable(pf->pdev);
277 kfree(pf->irq_entries);
280 static int nfp_net_pf_init_vnics(struct nfp_pf *pf)
286 /* Finish vNIC init and register */
288 list_for_each_entry(nn, &pf->vnics, vnic_list) {
289 if (!nfp_net_is_data_vnic(nn))
291 err = nfp_net_pf_init_vnic(pf, nn, id);
293 goto err_prev_deinit;
301 list_for_each_entry_continue_reverse(nn, &pf->vnics, vnic_list)
302 if (nfp_net_is_data_vnic(nn))
303 nfp_net_pf_clean_vnic(pf, nn);
308 nfp_net_pf_app_init(struct nfp_pf *pf, u8 __iomem *qc_bar, unsigned int stride)
310 struct devlink *devlink = priv_to_devlink(pf);
311 u8 __iomem *ctrl_bar;
314 pf->app = nfp_app_alloc(pf, nfp_net_pf_get_app_id(pf));
316 return PTR_ERR(pf->app);
319 err = nfp_app_init(pf->app);
320 devl_unlock(devlink);
324 if (!nfp_app_needs_ctrl_vnic(pf->app))
327 ctrl_bar = nfp_pf_map_rtsym(pf, "net.ctrl", "_pf%u_net_ctrl_bar",
328 NFP_PF_CSR_SLICE_SIZE, &pf->ctrl_vnic_bar);
329 if (IS_ERR(ctrl_bar)) {
330 nfp_err(pf->cpp, "Failed to find ctrl vNIC memory symbol\n");
331 err = PTR_ERR(ctrl_bar);
335 pf->ctrl_vnic = nfp_net_pf_alloc_vnic(pf, false, ctrl_bar, qc_bar,
337 if (IS_ERR(pf->ctrl_vnic)) {
338 err = PTR_ERR(pf->ctrl_vnic);
345 nfp_cpp_area_release_free(pf->ctrl_vnic_bar);
348 nfp_app_clean(pf->app);
349 devl_unlock(devlink);
351 nfp_app_free(pf->app);
356 static void nfp_net_pf_app_clean(struct nfp_pf *pf)
358 struct devlink *devlink = priv_to_devlink(pf);
361 nfp_net_pf_free_vnic(pf, pf->ctrl_vnic);
362 nfp_cpp_area_release_free(pf->ctrl_vnic_bar);
366 nfp_app_clean(pf->app);
367 devl_unlock(devlink);
369 nfp_app_free(pf->app);
373 static int nfp_net_pf_app_start_ctrl(struct nfp_pf *pf)
379 err = nfp_net_pf_init_vnic(pf, pf->ctrl_vnic, 0);
383 err = nfp_ctrl_open(pf->ctrl_vnic);
390 nfp_net_pf_clean_vnic(pf, pf->ctrl_vnic);
394 static void nfp_net_pf_app_stop_ctrl(struct nfp_pf *pf)
398 nfp_ctrl_close(pf->ctrl_vnic);
399 nfp_net_pf_clean_vnic(pf, pf->ctrl_vnic);
402 static int nfp_net_pf_app_start(struct nfp_pf *pf)
406 err = nfp_net_pf_app_start_ctrl(pf);
410 err = nfp_app_start(pf->app, pf->ctrl_vnic);
415 err = nfp_app_sriov_enable(pf->app, pf->num_vfs);
423 nfp_app_stop(pf->app);
425 nfp_net_pf_app_stop_ctrl(pf);
429 static void nfp_net_pf_app_stop(struct nfp_pf *pf)
432 nfp_app_sriov_disable(pf->app);
433 nfp_app_stop(pf->app);
434 nfp_net_pf_app_stop_ctrl(pf);
437 static void nfp_net_pci_unmap_mem(struct nfp_pf *pf)
439 if (pf->vfcfg_tbl2_area)
440 nfp_cpp_area_release_free(pf->vfcfg_tbl2_area);
442 nfp_cpp_area_release_free(pf->vf_cfg_bar);
443 if (pf->mac_stats_bar)
444 nfp_cpp_area_release_free(pf->mac_stats_bar);
445 nfp_cpp_area_release_free(pf->qc_area);
446 nfp_cpp_area_release_free(pf->data_vnic_bar);
449 static int nfp_net_pci_map_mem(struct nfp_pf *pf)
451 u32 min_size, cpp_id;
455 min_size = pf->max_data_vnics * NFP_PF_CSR_SLICE_SIZE;
456 mem = nfp_pf_map_rtsym(pf, "net.bar0", "_pf%d_net_bar0",
457 min_size, &pf->data_vnic_bar);
459 nfp_err(pf->cpp, "Failed to find data vNIC memory symbol\n");
464 min_size = NFP_MAC_STATS_SIZE * (pf->eth_tbl->max_index + 1);
465 pf->mac_stats_mem = nfp_rtsym_map(pf->rtbl, "_mac_stats",
466 "net.macstats", min_size,
468 if (IS_ERR(pf->mac_stats_mem)) {
469 if (PTR_ERR(pf->mac_stats_mem) != -ENOENT) {
470 err = PTR_ERR(pf->mac_stats_mem);
473 pf->mac_stats_mem = NULL;
477 pf->vf_cfg_mem = nfp_pf_map_rtsym(pf, "net.vfcfg", "_pf%d_net_vf_bar",
478 NFP_NET_CFG_BAR_SZ * pf->limit_vfs,
480 if (IS_ERR(pf->vf_cfg_mem)) {
481 if (PTR_ERR(pf->vf_cfg_mem) != -ENOENT) {
482 err = PTR_ERR(pf->vf_cfg_mem);
483 goto err_unmap_mac_stats;
485 pf->vf_cfg_mem = NULL;
488 min_size = NFP_NET_VF_CFG_SZ * pf->limit_vfs + NFP_NET_VF_CFG_MB_SZ;
489 pf->vfcfg_tbl2 = nfp_pf_map_rtsym(pf, "net.vfcfg_tbl2",
491 min_size, &pf->vfcfg_tbl2_area);
492 if (IS_ERR(pf->vfcfg_tbl2)) {
493 if (PTR_ERR(pf->vfcfg_tbl2) != -ENOENT) {
494 err = PTR_ERR(pf->vfcfg_tbl2);
495 goto err_unmap_vf_cfg;
497 pf->vfcfg_tbl2 = NULL;
500 cpp_id = NFP_CPP_ISLAND_ID(0, NFP_CPP_ACTION_RW, 0, 0);
501 mem = nfp_cpp_map_area(pf->cpp, "net.qc", cpp_id,
502 nfp_qcp_queue_offset(pf->dev_info, 0),
503 pf->dev_info->qc_area_sz, &pf->qc_area);
505 nfp_err(pf->cpp, "Failed to map Queue Controller area.\n");
507 goto err_unmap_vfcfg_tbl2;
512 err_unmap_vfcfg_tbl2:
513 if (pf->vfcfg_tbl2_area)
514 nfp_cpp_area_release_free(pf->vfcfg_tbl2_area);
517 nfp_cpp_area_release_free(pf->vf_cfg_bar);
519 if (pf->mac_stats_bar)
520 nfp_cpp_area_release_free(pf->mac_stats_bar);
522 nfp_cpp_area_release_free(pf->data_vnic_bar);
527 nfp_net_eth_port_update(struct nfp_cpp *cpp, struct nfp_port *port,
528 struct nfp_eth_table *eth_table)
530 struct nfp_eth_table_port *eth_port;
534 eth_port = nfp_net_find_port(eth_table, port->eth_id);
536 set_bit(NFP_PORT_CHANGED, &port->flags);
537 nfp_warn(cpp, "Warning: port #%d not present after reconfig\n",
541 if (eth_port->override_changed) {
542 nfp_warn(cpp, "Port #%d config changed, unregistering. Driver reload required before port will be operational again.\n", port->eth_id);
543 port->type = NFP_PORT_INVALID;
546 memcpy(port->eth_port, eth_port, sizeof(*eth_port));
551 int nfp_net_refresh_port_table_sync(struct nfp_pf *pf)
553 struct devlink *devlink = priv_to_devlink(pf);
554 struct nfp_eth_table *eth_table;
555 struct nfp_net *nn, *next;
556 struct nfp_port *port;
559 devl_assert_locked(devlink);
561 /* Check for nfp_net_pci_remove() racing against us */
562 if (list_empty(&pf->vnics))
565 /* Update state of all ports */
567 list_for_each_entry(port, &pf->ports, port_list)
568 clear_bit(NFP_PORT_CHANGED, &port->flags);
570 eth_table = nfp_eth_read_ports(pf->cpp);
572 list_for_each_entry(port, &pf->ports, port_list)
573 if (__nfp_port_get_eth_port(port))
574 set_bit(NFP_PORT_CHANGED, &port->flags);
576 nfp_err(pf->cpp, "Error refreshing port config!\n");
580 list_for_each_entry(port, &pf->ports, port_list)
581 if (__nfp_port_get_eth_port(port))
582 nfp_net_eth_port_update(pf->cpp, port, eth_table);
587 /* Resync repr state. This may cause reprs to be removed. */
588 err = nfp_reprs_resync_phys_ports(pf->app);
592 /* Shoot off the ports which became invalid */
593 list_for_each_entry_safe(nn, next, &pf->vnics, vnic_list) {
594 if (!nn->port || nn->port->type != NFP_PORT_INVALID)
597 nfp_net_pf_clean_vnic(pf, nn);
598 nfp_net_pf_free_vnic(pf, nn);
604 static void nfp_net_refresh_vnics(struct work_struct *work)
606 struct nfp_pf *pf = container_of(work, struct nfp_pf,
608 struct devlink *devlink = priv_to_devlink(pf);
611 nfp_net_refresh_port_table_sync(pf);
612 devl_unlock(devlink);
615 void nfp_net_refresh_port_table(struct nfp_port *port)
617 struct nfp_pf *pf = port->app->pf;
619 set_bit(NFP_PORT_CHANGED, &port->flags);
621 queue_work(pf->wq, &pf->port_refresh_work);
624 int nfp_net_refresh_eth_port(struct nfp_port *port)
626 struct nfp_cpp *cpp = port->app->cpp;
627 struct nfp_eth_table *eth_table;
630 clear_bit(NFP_PORT_CHANGED, &port->flags);
632 eth_table = nfp_eth_read_ports(cpp);
634 set_bit(NFP_PORT_CHANGED, &port->flags);
635 nfp_err(cpp, "Error refreshing port state table!\n");
639 ret = nfp_net_eth_port_update(cpp, port, eth_table);
647 * PCI device functions
649 int nfp_net_pci_probe(struct nfp_pf *pf)
651 struct devlink *devlink = priv_to_devlink(pf);
652 struct nfp_net_fw_version fw_ver;
653 u8 __iomem *ctrl_bar, *qc_bar;
657 INIT_WORK(&pf->port_refresh_work, nfp_net_refresh_vnics);
660 nfp_err(pf->cpp, "No %s, giving up.\n",
661 pf->fw_loaded ? "symbol table" : "firmware found");
665 pf->max_data_vnics = nfp_net_pf_get_num_ports(pf);
666 if ((int)pf->max_data_vnics < 0)
667 return pf->max_data_vnics;
669 err = nfp_net_pci_map_mem(pf);
673 ctrl_bar = nfp_cpp_area_iomem(pf->data_vnic_bar);
674 qc_bar = nfp_cpp_area_iomem(pf->qc_area);
675 if (!ctrl_bar || !qc_bar) {
680 nfp_net_get_fw_version(&fw_ver, ctrl_bar);
681 if (fw_ver.extend & NFP_NET_CFG_VERSION_RESERVED_MASK ||
682 fw_ver.class != NFP_NET_CFG_VERSION_CLASS_GENERIC) {
683 nfp_err(pf->cpp, "Unknown Firmware ABI %d.%d.%d.%d\n",
684 fw_ver.extend, fw_ver.class,
685 fw_ver.major, fw_ver.minor);
690 /* Determine stride */
691 if (nfp_net_fw_ver_eq(&fw_ver, 0, 0, 0, 1)) {
693 nfp_warn(pf->cpp, "OBSOLETE Firmware detected - VF isolation not available\n");
695 switch (fw_ver.major) {
700 nfp_err(pf->cpp, "Unsupported Firmware ABI %d.%d.%d.%d\n",
701 fw_ver.extend, fw_ver.class,
702 fw_ver.major, fw_ver.minor);
708 err = nfp_net_pf_app_init(pf, qc_bar, stride);
712 err = nfp_shared_buf_register(pf);
714 goto err_devlink_unreg;
716 err = nfp_devlink_params_register(pf);
718 goto err_shared_buf_unreg;
721 pf->ddir = nfp_net_debugfs_device_add(pf->pdev);
723 /* Allocate the vnics and do basic init */
724 err = nfp_net_pf_alloc_vnics(pf, ctrl_bar, qc_bar, stride);
728 err = nfp_net_pf_alloc_irqs(pf);
732 err = nfp_net_pf_app_start(pf);
736 err = nfp_net_pf_init_vnics(pf);
740 devl_unlock(devlink);
741 devlink_register(devlink);
746 nfp_net_pf_app_stop(pf);
748 nfp_net_pf_free_irqs(pf);
750 nfp_net_pf_free_vnics(pf);
752 nfp_net_debugfs_dir_clean(&pf->ddir);
753 devl_unlock(devlink);
754 nfp_devlink_params_unregister(pf);
755 err_shared_buf_unreg:
756 nfp_shared_buf_unregister(pf);
758 cancel_work_sync(&pf->port_refresh_work);
759 nfp_net_pf_app_clean(pf);
761 nfp_net_pci_unmap_mem(pf);
765 void nfp_net_pci_remove(struct nfp_pf *pf)
767 struct devlink *devlink = priv_to_devlink(pf);
768 struct nfp_net *nn, *next;
770 devlink_unregister(priv_to_devlink(pf));
772 list_for_each_entry_safe(nn, next, &pf->vnics, vnic_list) {
773 if (!nfp_net_is_data_vnic(nn))
775 nfp_net_pf_clean_vnic(pf, nn);
776 nfp_net_pf_free_vnic(pf, nn);
779 nfp_net_pf_app_stop(pf);
780 /* stop app first, to avoid double free of ctrl vNIC's ddir */
781 nfp_net_debugfs_dir_clean(&pf->ddir);
783 devl_unlock(devlink);
785 nfp_devlink_params_unregister(pf);
786 nfp_shared_buf_unregister(pf);
788 nfp_net_pf_free_irqs(pf);
789 nfp_net_pf_app_clean(pf);
790 nfp_net_pci_unmap_mem(pf);
792 cancel_work_sync(&pf->port_refresh_work);