196de2506SJakub Kicinski // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
296de2506SJakub Kicinski /* Copyright (C) 2015-2018 Netronome Systems, Inc. */
363461a02SJakub Kicinski
463461a02SJakub Kicinski /*
563461a02SJakub Kicinski * nfp_net_main.c
663461a02SJakub Kicinski * Netronome network device driver: Main entry point
763461a02SJakub Kicinski * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
863461a02SJakub Kicinski * Alejandro Lucero <alejandro.lucero@netronome.com>
963461a02SJakub Kicinski * Jason McMullan <jason.mcmullan@netronome.com>
1063461a02SJakub Kicinski * Rolf Neugebauer <rolf.neugebauer@netronome.com>
1163461a02SJakub Kicinski */
1263461a02SJakub Kicinski
1363461a02SJakub Kicinski #include <linux/etherdevice.h>
1463461a02SJakub Kicinski #include <linux/kernel.h>
1563461a02SJakub Kicinski #include <linux/init.h>
16ec8b1fbeSJakub Kicinski #include <linux/lockdep.h>
1763461a02SJakub Kicinski #include <linux/pci.h>
1863461a02SJakub Kicinski #include <linux/pci_regs.h>
1963461a02SJakub Kicinski #include <linux/random.h>
20172f638cSJakub Kicinski #include <linux/rtnetlink.h>
2163461a02SJakub Kicinski
2263461a02SJakub Kicinski #include "nfpcore/nfp.h"
2363461a02SJakub Kicinski #include "nfpcore/nfp_cpp.h"
249423d24bSJakub Kicinski #include "nfpcore/nfp_dev.h"
2563461a02SJakub Kicinski #include "nfpcore/nfp_nffw.h"
26ce22f5a2SJakub Kicinski #include "nfpcore/nfp_nsp.h"
2763461a02SJakub Kicinski #include "nfpcore/nfp6000_pcie.h"
287ac9ebd5SJakub Kicinski #include "nfp_app.h"
2963461a02SJakub Kicinski #include "nfp_net_ctrl.h"
3025528d90SPablo Cascón #include "nfp_net_sriov.h"
3163461a02SJakub Kicinski #include "nfp_net.h"
3263461a02SJakub Kicinski #include "nfp_main.h"
33eb488c26SJakub Kicinski #include "nfp_port.h"
3463461a02SJakub Kicinski
3563461a02SJakub Kicinski #define NFP_PF_CSR_SLICE_SIZE (32 * 1024)
3663461a02SJakub Kicinski
3763461a02SJakub Kicinski /**
38b9de0077SJakub Kicinski * nfp_net_get_mac_addr() - Get the MAC address.
399baa4885SJakub Kicinski * @pf: NFP PF handle
40c55ca688SJakub Kicinski * @netdev: net_device to set MAC address on
4193da7d96SSimon Horman * @port: NFP port structure
42b9de0077SJakub Kicinski *
43b9de0077SJakub Kicinski * First try to get the MAC address from NSP ETH table. If that
44cb2cda48SJakub Kicinski * fails generate a random address.
45b9de0077SJakub Kicinski */
46c55ca688SJakub Kicinski void
nfp_net_get_mac_addr(struct nfp_pf * pf,struct net_device * netdev,struct nfp_port * port)47c55ca688SJakub Kicinski nfp_net_get_mac_addr(struct nfp_pf *pf, struct net_device *netdev,
48c55ca688SJakub Kicinski struct nfp_port *port)
4963461a02SJakub Kicinski {
50eb488c26SJakub Kicinski struct nfp_eth_table_port *eth_port;
5163461a02SJakub Kicinski
5293da7d96SSimon Horman eth_port = __nfp_port_get_eth_port(port);
53cb2cda48SJakub Kicinski if (!eth_port) {
54c55ca688SJakub Kicinski eth_hw_addr_random(netdev);
55cb2cda48SJakub Kicinski return;
56cb2cda48SJakub Kicinski }
57cb2cda48SJakub Kicinski
58f3956ebbSJakub Kicinski eth_hw_addr_set(netdev, eth_port->mac_addr);
59c55ca688SJakub Kicinski ether_addr_copy(netdev->perm_addr, eth_port->mac_addr);
6063461a02SJakub Kicinski }
6163461a02SJakub Kicinski
622eb333c4SJakub Kicinski static struct nfp_eth_table_port *
nfp_net_find_port(struct nfp_eth_table * eth_tbl,unsigned int index)632eb333c4SJakub Kicinski nfp_net_find_port(struct nfp_eth_table *eth_tbl, unsigned int index)
6463461a02SJakub Kicinski {
6563461a02SJakub Kicinski int i;
6663461a02SJakub Kicinski
6790fdc561SJakub Kicinski for (i = 0; eth_tbl && i < eth_tbl->count; i++)
682eb333c4SJakub Kicinski if (eth_tbl->ports[i].index == index)
6990fdc561SJakub Kicinski return ð_tbl->ports[i];
7063461a02SJakub Kicinski
71b9de0077SJakub Kicinski return NULL;
7263461a02SJakub Kicinski }
7363461a02SJakub Kicinski
nfp_net_pf_get_num_ports(struct nfp_pf * pf)7469394af5SJakub Kicinski static int nfp_net_pf_get_num_ports(struct nfp_pf *pf)
7569394af5SJakub Kicinski {
768f6196f6SJakub Kicinski return nfp_pf_rtsym_read_optional(pf, "nfd_cfg_pf%u_num_ports", 1);
7769394af5SJakub Kicinski }
7869394af5SJakub Kicinski
nfp_net_pf_free_vnic(struct nfp_pf * pf,struct nfp_net * nn)799140b30dSJakub Kicinski static void nfp_net_pf_free_vnic(struct nfp_pf *pf, struct nfp_net *nn)
809140b30dSJakub Kicinski {
81c496291cSJakub Kicinski if (nfp_net_is_data_vnic(nn))
82c496291cSJakub Kicinski nfp_app_vnic_free(pf->app, nn);
83eb488c26SJakub Kicinski nfp_port_free(nn->port);
849140b30dSJakub Kicinski list_del(&nn->vnic_list);
859140b30dSJakub Kicinski pf->num_vnics--;
869140b30dSJakub Kicinski nfp_net_free(nn);
879140b30dSJakub Kicinski }
889140b30dSJakub Kicinski
nfp_net_pf_free_vnics(struct nfp_pf * pf)89d4e7f092SJakub Kicinski static void nfp_net_pf_free_vnics(struct nfp_pf *pf)
9063461a02SJakub Kicinski {
9102082701SJakub Kicinski struct nfp_net *nn, *next;
9263461a02SJakub Kicinski
9302082701SJakub Kicinski list_for_each_entry_safe(nn, next, &pf->vnics, vnic_list)
9402082701SJakub Kicinski if (nfp_net_is_data_vnic(nn))
959140b30dSJakub Kicinski nfp_net_pf_free_vnic(pf, nn);
9663461a02SJakub Kicinski }
9763461a02SJakub Kicinski
9863461a02SJakub Kicinski static struct nfp_net *
nfp_net_pf_alloc_vnic(struct nfp_pf * pf,bool needs_netdev,void __iomem * ctrl_bar,void __iomem * qc_bar,int stride,unsigned int id)99a7b1ad08SJakub Kicinski nfp_net_pf_alloc_vnic(struct nfp_pf *pf, bool needs_netdev,
10073e253f0SJakub Kicinski void __iomem *ctrl_bar, void __iomem *qc_bar,
1012eb333c4SJakub Kicinski int stride, unsigned int id)
10263461a02SJakub Kicinski {
10373e253f0SJakub Kicinski u32 tx_base, rx_base, n_tx_rings, n_rx_rings;
10463461a02SJakub Kicinski struct nfp_net *nn;
1058aa0cb00SJakub Kicinski int err;
10663461a02SJakub Kicinski
10773e253f0SJakub Kicinski tx_base = readl(ctrl_bar + NFP_NET_CFG_START_TXQ);
10873e253f0SJakub Kicinski rx_base = readl(ctrl_bar + NFP_NET_CFG_START_RXQ);
10963461a02SJakub Kicinski n_tx_rings = readl(ctrl_bar + NFP_NET_CFG_MAX_TXRINGS);
11063461a02SJakub Kicinski n_rx_rings = readl(ctrl_bar + NFP_NET_CFG_MAX_RXRINGS);
11163461a02SJakub Kicinski
112d4e7f092SJakub Kicinski /* Allocate and initialise the vNIC */
1139423d24bSJakub Kicinski nn = nfp_net_alloc(pf->pdev, pf->dev_info, ctrl_bar, needs_netdev,
114e38f5d11SJakub Kicinski n_tx_rings, n_rx_rings);
11563461a02SJakub Kicinski if (IS_ERR(nn))
11663461a02SJakub Kicinski return nn;
11763461a02SJakub Kicinski
1187ac9ebd5SJakub Kicinski nn->app = pf->app;
11973e253f0SJakub Kicinski nn->tx_bar = qc_bar + tx_base * NFP_QCP_QUEUE_ADDR_SZ;
12073e253f0SJakub Kicinski nn->rx_bar = qc_bar + rx_base * NFP_QCP_QUEUE_ADDR_SZ;
12179c12a75SJakub Kicinski nn->dp.is_vf = 0;
12263461a02SJakub Kicinski nn->stride_rx = stride;
12363461a02SJakub Kicinski nn->stride_tx = stride;
124eb488c26SJakub Kicinski
12502082701SJakub Kicinski if (needs_netdev) {
126c496291cSJakub Kicinski err = nfp_app_vnic_alloc(pf->app, nn, id);
1278aa0cb00SJakub Kicinski if (err) {
128eb488c26SJakub Kicinski nfp_net_free(nn);
1298aa0cb00SJakub Kicinski return ERR_PTR(err);
130eb488c26SJakub Kicinski }
13102082701SJakub Kicinski }
132d88b0a23SJakub Kicinski
133d88b0a23SJakub Kicinski pf->num_vnics++;
134d88b0a23SJakub Kicinski list_add_tail(&nn->vnic_list, &pf->vnics);
13563461a02SJakub Kicinski
13663461a02SJakub Kicinski return nn;
13763461a02SJakub Kicinski }
13863461a02SJakub Kicinski
13963461a02SJakub Kicinski static int
nfp_net_pf_init_vnic(struct nfp_pf * pf,struct nfp_net * nn,unsigned int id)140d4e7f092SJakub Kicinski nfp_net_pf_init_vnic(struct nfp_pf *pf, struct nfp_net *nn, unsigned int id)
14163461a02SJakub Kicinski {
14263461a02SJakub Kicinski int err;
14363461a02SJakub Kicinski
14451c1df83SJakub Kicinski nn->id = id;
14551c1df83SJakub Kicinski
14653e7a08fSJakub Kicinski if (nn->port) {
14753e7a08fSJakub Kicinski err = nfp_devlink_port_register(pf->app, nn->port);
14853e7a08fSJakub Kicinski if (err)
149335bc0ddSJiri Pirko return err;
15053e7a08fSJakub Kicinski }
15153e7a08fSJakub Kicinski
152335bc0ddSJiri Pirko err = nfp_net_init(nn);
153335bc0ddSJiri Pirko if (err)
154335bc0ddSJiri Pirko goto err_devlink_port_clean;
155335bc0ddSJiri Pirko
156335bc0ddSJiri Pirko nfp_net_debugfs_vnic_add(nn, pf->ddir);
157335bc0ddSJiri Pirko
15863461a02SJakub Kicinski nfp_net_info(nn);
15963461a02SJakub Kicinski
160c496291cSJakub Kicinski if (nfp_net_is_data_vnic(nn)) {
161c496291cSJakub Kicinski err = nfp_app_vnic_init(pf->app, nn);
162c496291cSJakub Kicinski if (err)
163ac73d4bfSJiri Pirko goto err_debugfs_vnic_clean;
164c496291cSJakub Kicinski }
165c496291cSJakub Kicinski
16663461a02SJakub Kicinski return 0;
16753e7a08fSJakub Kicinski
168ac73d4bfSJiri Pirko err_debugfs_vnic_clean:
16953e7a08fSJakub Kicinski nfp_net_debugfs_dir_clean(&nn->debugfs_dir);
17053e7a08fSJakub Kicinski nfp_net_clean(nn);
171335bc0ddSJiri Pirko err_devlink_port_clean:
172335bc0ddSJiri Pirko if (nn->port)
173335bc0ddSJiri Pirko nfp_devlink_port_unregister(nn->port);
17453e7a08fSJakub Kicinski return err;
17563461a02SJakub Kicinski }
17663461a02SJakub Kicinski
17763461a02SJakub Kicinski static int
nfp_net_pf_alloc_vnics(struct nfp_pf * pf,void __iomem * ctrl_bar,void __iomem * qc_bar,int stride)178d4e7f092SJakub Kicinski nfp_net_pf_alloc_vnics(struct nfp_pf *pf, void __iomem *ctrl_bar,
17921537bc7SJakub Kicinski void __iomem *qc_bar, int stride)
18063461a02SJakub Kicinski {
18163461a02SJakub Kicinski struct nfp_net *nn;
18263461a02SJakub Kicinski unsigned int i;
18363461a02SJakub Kicinski int err;
18463461a02SJakub Kicinski
185d4e7f092SJakub Kicinski for (i = 0; i < pf->max_data_vnics; i++) {
18673e253f0SJakub Kicinski nn = nfp_net_pf_alloc_vnic(pf, true, ctrl_bar, qc_bar,
18721537bc7SJakub Kicinski stride, i);
18863461a02SJakub Kicinski if (IS_ERR(nn)) {
18963461a02SJakub Kicinski err = PTR_ERR(nn);
19063461a02SJakub Kicinski goto err_free_prev;
19163461a02SJakub Kicinski }
19263461a02SJakub Kicinski
19362fad9e6SYinjun Zhang if (nn->port)
19462fad9e6SYinjun Zhang nn->port->link_cb = nfp_net_refresh_port_table;
19562fad9e6SYinjun Zhang
19663461a02SJakub Kicinski ctrl_bar += NFP_PF_CSR_SLICE_SIZE;
197d88b0a23SJakub Kicinski
1988aa0cb00SJakub Kicinski /* Kill the vNIC if app init marked it as invalid */
19947e25277Szhong jiang if (nn->port && nn->port->type == NFP_PORT_INVALID)
200d88b0a23SJakub Kicinski nfp_net_pf_free_vnic(pf, nn);
20163461a02SJakub Kicinski }
20263461a02SJakub Kicinski
203d4e7f092SJakub Kicinski if (list_empty(&pf->vnics))
204b9de0077SJakub Kicinski return -ENODEV;
205b9de0077SJakub Kicinski
20663461a02SJakub Kicinski return 0;
20763461a02SJakub Kicinski
20863461a02SJakub Kicinski err_free_prev:
209d4e7f092SJakub Kicinski nfp_net_pf_free_vnics(pf);
21063461a02SJakub Kicinski return err;
21163461a02SJakub Kicinski }
21263461a02SJakub Kicinski
nfp_net_pf_clean_vnic(struct nfp_pf * pf,struct nfp_net * nn)21371f8a116SJakub Kicinski static void nfp_net_pf_clean_vnic(struct nfp_pf *pf, struct nfp_net *nn)
21471f8a116SJakub Kicinski {
215c496291cSJakub Kicinski if (nfp_net_is_data_vnic(nn))
216c496291cSJakub Kicinski nfp_app_vnic_clean(pf->app, nn);
21771f8a116SJakub Kicinski nfp_net_debugfs_dir_clean(&nn->debugfs_dir);
21871f8a116SJakub Kicinski nfp_net_clean(nn);
219335bc0ddSJiri Pirko if (nn->port)
220335bc0ddSJiri Pirko nfp_devlink_port_unregister(nn->port);
22171f8a116SJakub Kicinski }
22271f8a116SJakub Kicinski
nfp_net_pf_alloc_irqs(struct nfp_pf * pf)2236d4b0d8eSJakub Kicinski static int nfp_net_pf_alloc_irqs(struct nfp_pf *pf)
22463461a02SJakub Kicinski {
2256d4b0d8eSJakub Kicinski unsigned int wanted_irqs, num_irqs, vnics_left, irqs_left;
22663461a02SJakub Kicinski struct nfp_net *nn;
22763461a02SJakub Kicinski
22863461a02SJakub Kicinski /* Get MSI-X vectors */
22963461a02SJakub Kicinski wanted_irqs = 0;
230d4e7f092SJakub Kicinski list_for_each_entry(nn, &pf->vnics, vnic_list)
23179c12a75SJakub Kicinski wanted_irqs += NFP_NET_NON_Q_VECTORS + nn->dp.num_r_vecs;
23263461a02SJakub Kicinski pf->irq_entries = kcalloc(wanted_irqs, sizeof(*pf->irq_entries),
23363461a02SJakub Kicinski GFP_KERNEL);
2346d4b0d8eSJakub Kicinski if (!pf->irq_entries)
2356d4b0d8eSJakub Kicinski return -ENOMEM;
23663461a02SJakub Kicinski
23763461a02SJakub Kicinski num_irqs = nfp_net_irqs_alloc(pf->pdev, pf->irq_entries,
238d4e7f092SJakub Kicinski NFP_NET_MIN_VNIC_IRQS * pf->num_vnics,
23963461a02SJakub Kicinski wanted_irqs);
24063461a02SJakub Kicinski if (!num_irqs) {
2416d4b0d8eSJakub Kicinski nfp_warn(pf->cpp, "Unable to allocate MSI-X vectors\n");
2426d4b0d8eSJakub Kicinski kfree(pf->irq_entries);
2436d4b0d8eSJakub Kicinski return -ENOMEM;
24463461a02SJakub Kicinski }
24563461a02SJakub Kicinski
246d4e7f092SJakub Kicinski /* Distribute IRQs to vNICs */
24763461a02SJakub Kicinski irqs_left = num_irqs;
248d4e7f092SJakub Kicinski vnics_left = pf->num_vnics;
249d4e7f092SJakub Kicinski list_for_each_entry(nn, &pf->vnics, vnic_list) {
25063461a02SJakub Kicinski unsigned int n;
25163461a02SJakub Kicinski
2522c7e41c0SJakub Kicinski n = min(NFP_NET_NON_Q_VECTORS + nn->dp.num_r_vecs,
2532c7e41c0SJakub Kicinski DIV_ROUND_UP(irqs_left, vnics_left));
25463461a02SJakub Kicinski nfp_net_irqs_assign(nn, &pf->irq_entries[num_irqs - irqs_left],
25563461a02SJakub Kicinski n);
25663461a02SJakub Kicinski irqs_left -= n;
257d4e7f092SJakub Kicinski vnics_left--;
25863461a02SJakub Kicinski }
25963461a02SJakub Kicinski
2606d4b0d8eSJakub Kicinski return 0;
2616d4b0d8eSJakub Kicinski }
2626d4b0d8eSJakub Kicinski
nfp_net_pf_free_irqs(struct nfp_pf * pf)2636d4b0d8eSJakub Kicinski static void nfp_net_pf_free_irqs(struct nfp_pf *pf)
2646d4b0d8eSJakub Kicinski {
2656d4b0d8eSJakub Kicinski nfp_net_irqs_disable(pf->pdev);
2666d4b0d8eSJakub Kicinski kfree(pf->irq_entries);
2676d4b0d8eSJakub Kicinski }
2686d4b0d8eSJakub Kicinski
nfp_net_pf_init_vnics(struct nfp_pf * pf)2696d4b0d8eSJakub Kicinski static int nfp_net_pf_init_vnics(struct nfp_pf *pf)
2706d4b0d8eSJakub Kicinski {
2716d4b0d8eSJakub Kicinski struct nfp_net *nn;
2726d4b0d8eSJakub Kicinski unsigned int id;
2736d4b0d8eSJakub Kicinski int err;
2746d4b0d8eSJakub Kicinski
275d4e7f092SJakub Kicinski /* Finish vNIC init and register */
27663461a02SJakub Kicinski id = 0;
277d4e7f092SJakub Kicinski list_for_each_entry(nn, &pf->vnics, vnic_list) {
27802082701SJakub Kicinski if (!nfp_net_is_data_vnic(nn))
27902082701SJakub Kicinski continue;
280d4e7f092SJakub Kicinski err = nfp_net_pf_init_vnic(pf, nn, id);
28163461a02SJakub Kicinski if (err)
28263461a02SJakub Kicinski goto err_prev_deinit;
28363461a02SJakub Kicinski
28463461a02SJakub Kicinski id++;
28563461a02SJakub Kicinski }
28663461a02SJakub Kicinski
28763461a02SJakub Kicinski return 0;
28863461a02SJakub Kicinski
28963461a02SJakub Kicinski err_prev_deinit:
29071f8a116SJakub Kicinski list_for_each_entry_continue_reverse(nn, &pf->vnics, vnic_list)
29102082701SJakub Kicinski if (nfp_net_is_data_vnic(nn))
29271f8a116SJakub Kicinski nfp_net_pf_clean_vnic(pf, nn);
29363461a02SJakub Kicinski return err;
29463461a02SJakub Kicinski }
29563461a02SJakub Kicinski
29602082701SJakub Kicinski static int
nfp_net_pf_app_init(struct nfp_pf * pf,u8 __iomem * qc_bar,unsigned int stride)29702082701SJakub Kicinski nfp_net_pf_app_init(struct nfp_pf *pf, u8 __iomem *qc_bar, unsigned int stride)
2987ac9ebd5SJakub Kicinski {
299162cca42SJakub Kicinski struct devlink *devlink = priv_to_devlink(pf);
30002082701SJakub Kicinski u8 __iomem *ctrl_bar;
3018aa0cb00SJakub Kicinski int err;
3027ac9ebd5SJakub Kicinski
3038aa0cb00SJakub Kicinski pf->app = nfp_app_alloc(pf, nfp_net_pf_get_app_id(pf));
3048aa0cb00SJakub Kicinski if (IS_ERR(pf->app))
3058aa0cb00SJakub Kicinski return PTR_ERR(pf->app);
3068aa0cb00SJakub Kicinski
307162cca42SJakub Kicinski devl_lock(devlink);
3088aa0cb00SJakub Kicinski err = nfp_app_init(pf->app);
309162cca42SJakub Kicinski devl_unlock(devlink);
3108aa0cb00SJakub Kicinski if (err)
3118aa0cb00SJakub Kicinski goto err_free;
3128aa0cb00SJakub Kicinski
31302082701SJakub Kicinski if (!nfp_app_needs_ctrl_vnic(pf->app))
3148aa0cb00SJakub Kicinski return 0;
3158aa0cb00SJakub Kicinski
3168f6196f6SJakub Kicinski ctrl_bar = nfp_pf_map_rtsym(pf, "net.ctrl", "_pf%u_net_ctrl_bar",
3178f6196f6SJakub Kicinski NFP_PF_CSR_SLICE_SIZE, &pf->ctrl_vnic_bar);
31802082701SJakub Kicinski if (IS_ERR(ctrl_bar)) {
319825b18abSJakub Kicinski nfp_err(pf->cpp, "Failed to find ctrl vNIC memory symbol\n");
32002082701SJakub Kicinski err = PTR_ERR(ctrl_bar);
3219ce6bbbbSJakub Kicinski goto err_app_clean;
32202082701SJakub Kicinski }
32302082701SJakub Kicinski
32402082701SJakub Kicinski pf->ctrl_vnic = nfp_net_pf_alloc_vnic(pf, false, ctrl_bar, qc_bar,
32502082701SJakub Kicinski stride, 0);
32602082701SJakub Kicinski if (IS_ERR(pf->ctrl_vnic)) {
32702082701SJakub Kicinski err = PTR_ERR(pf->ctrl_vnic);
32802082701SJakub Kicinski goto err_unmap;
32902082701SJakub Kicinski }
33002082701SJakub Kicinski
33102082701SJakub Kicinski return 0;
33202082701SJakub Kicinski
33302082701SJakub Kicinski err_unmap:
33402082701SJakub Kicinski nfp_cpp_area_release_free(pf->ctrl_vnic_bar);
3359ce6bbbbSJakub Kicinski err_app_clean:
336162cca42SJakub Kicinski devl_lock(devlink);
3379ce6bbbbSJakub Kicinski nfp_app_clean(pf->app);
338162cca42SJakub Kicinski devl_unlock(devlink);
3398aa0cb00SJakub Kicinski err_free:
3408aa0cb00SJakub Kicinski nfp_app_free(pf->app);
3419ce6bbbbSJakub Kicinski pf->app = NULL;
3428aa0cb00SJakub Kicinski return err;
3437ac9ebd5SJakub Kicinski }
3447ac9ebd5SJakub Kicinski
nfp_net_pf_app_clean(struct nfp_pf * pf)3457ac9ebd5SJakub Kicinski static void nfp_net_pf_app_clean(struct nfp_pf *pf)
3467ac9ebd5SJakub Kicinski {
347162cca42SJakub Kicinski struct devlink *devlink = priv_to_devlink(pf);
348162cca42SJakub Kicinski
34902082701SJakub Kicinski if (pf->ctrl_vnic) {
35002082701SJakub Kicinski nfp_net_pf_free_vnic(pf, pf->ctrl_vnic);
35102082701SJakub Kicinski nfp_cpp_area_release_free(pf->ctrl_vnic_bar);
35202082701SJakub Kicinski }
353bcc93a23SJakub Kicinski
354162cca42SJakub Kicinski devl_lock(devlink);
3559ce6bbbbSJakub Kicinski nfp_app_clean(pf->app);
356162cca42SJakub Kicinski devl_unlock(devlink);
357bcc93a23SJakub Kicinski
3587ac9ebd5SJakub Kicinski nfp_app_free(pf->app);
3591851f93fSSimon Horman pf->app = NULL;
3607ac9ebd5SJakub Kicinski }
3617ac9ebd5SJakub Kicinski
nfp_net_pf_app_start_ctrl(struct nfp_pf * pf)36202082701SJakub Kicinski static int nfp_net_pf_app_start_ctrl(struct nfp_pf *pf)
36302082701SJakub Kicinski {
36402082701SJakub Kicinski int err;
36502082701SJakub Kicinski
36602082701SJakub Kicinski if (!pf->ctrl_vnic)
36702082701SJakub Kicinski return 0;
36802082701SJakub Kicinski err = nfp_net_pf_init_vnic(pf, pf->ctrl_vnic, 0);
36902082701SJakub Kicinski if (err)
37002082701SJakub Kicinski return err;
37102082701SJakub Kicinski
37202082701SJakub Kicinski err = nfp_ctrl_open(pf->ctrl_vnic);
37302082701SJakub Kicinski if (err)
37402082701SJakub Kicinski goto err_clean_ctrl;
37502082701SJakub Kicinski
37602082701SJakub Kicinski return 0;
37702082701SJakub Kicinski
37802082701SJakub Kicinski err_clean_ctrl:
37902082701SJakub Kicinski nfp_net_pf_clean_vnic(pf, pf->ctrl_vnic);
38002082701SJakub Kicinski return err;
38102082701SJakub Kicinski }
38202082701SJakub Kicinski
nfp_net_pf_app_stop_ctrl(struct nfp_pf * pf)38302082701SJakub Kicinski static void nfp_net_pf_app_stop_ctrl(struct nfp_pf *pf)
38402082701SJakub Kicinski {
38502082701SJakub Kicinski if (!pf->ctrl_vnic)
38602082701SJakub Kicinski return;
38702082701SJakub Kicinski nfp_ctrl_close(pf->ctrl_vnic);
38802082701SJakub Kicinski nfp_net_pf_clean_vnic(pf, pf->ctrl_vnic);
38902082701SJakub Kicinski }
39002082701SJakub Kicinski
nfp_net_pf_app_start(struct nfp_pf * pf)39102082701SJakub Kicinski static int nfp_net_pf_app_start(struct nfp_pf *pf)
39202082701SJakub Kicinski {
39302082701SJakub Kicinski int err;
39402082701SJakub Kicinski
3959d8b17beSJakub Kicinski err = nfp_net_pf_app_start_ctrl(pf);
39602082701SJakub Kicinski if (err)
397e7562597SDan Carpenter return err;
39802082701SJakub Kicinski
3999d8b17beSJakub Kicinski err = nfp_app_start(pf->app, pf->ctrl_vnic);
4009d8b17beSJakub Kicinski if (err)
4019d8b17beSJakub Kicinski goto err_ctrl_stop;
4029d8b17beSJakub Kicinski
403e3f28473SJakub Kicinski if (pf->num_vfs) {
404e3f28473SJakub Kicinski err = nfp_app_sriov_enable(pf->app, pf->num_vfs);
405e3f28473SJakub Kicinski if (err)
406e3f28473SJakub Kicinski goto err_app_stop;
407e3f28473SJakub Kicinski }
408e3f28473SJakub Kicinski
40902082701SJakub Kicinski return 0;
41002082701SJakub Kicinski
411e3f28473SJakub Kicinski err_app_stop:
412e3f28473SJakub Kicinski nfp_app_stop(pf->app);
4139d8b17beSJakub Kicinski err_ctrl_stop:
4149d8b17beSJakub Kicinski nfp_net_pf_app_stop_ctrl(pf);
41502082701SJakub Kicinski return err;
41602082701SJakub Kicinski }
41702082701SJakub Kicinski
nfp_net_pf_app_stop(struct nfp_pf * pf)41802082701SJakub Kicinski static void nfp_net_pf_app_stop(struct nfp_pf *pf)
41902082701SJakub Kicinski {
420e3f28473SJakub Kicinski if (pf->num_vfs)
421e3f28473SJakub Kicinski nfp_app_sriov_disable(pf->app);
42202082701SJakub Kicinski nfp_app_stop(pf->app);
4239d8b17beSJakub Kicinski nfp_net_pf_app_stop_ctrl(pf);
42402082701SJakub Kicinski }
42502082701SJakub Kicinski
nfp_net_pci_unmap_mem(struct nfp_pf * pf)426a5950182SSimon Horman static void nfp_net_pci_unmap_mem(struct nfp_pf *pf)
427a5950182SSimon Horman {
42825528d90SPablo Cascón if (pf->vfcfg_tbl2_area)
42925528d90SPablo Cascón nfp_cpp_area_release_free(pf->vfcfg_tbl2_area);
430a5950182SSimon Horman if (pf->vf_cfg_bar)
431a5950182SSimon Horman nfp_cpp_area_release_free(pf->vf_cfg_bar);
432a5950182SSimon Horman if (pf->mac_stats_bar)
433a5950182SSimon Horman nfp_cpp_area_release_free(pf->mac_stats_bar);
434a5950182SSimon Horman nfp_cpp_area_release_free(pf->qc_area);
435a5950182SSimon Horman nfp_cpp_area_release_free(pf->data_vnic_bar);
436a5950182SSimon Horman }
437a5950182SSimon Horman
nfp_net_pci_map_mem(struct nfp_pf * pf)438a5950182SSimon Horman static int nfp_net_pci_map_mem(struct nfp_pf *pf)
439a5950182SSimon Horman {
4408f6d6052SJakub Kicinski u32 min_size, cpp_id;
441a5950182SSimon Horman u8 __iomem *mem;
442a5950182SSimon Horman int err;
443a5950182SSimon Horman
444f8473024SJakub Kicinski min_size = pf->max_data_vnics * NFP_PF_CSR_SLICE_SIZE;
4458f6196f6SJakub Kicinski mem = nfp_pf_map_rtsym(pf, "net.bar0", "_pf%d_net_bar0",
446f8473024SJakub Kicinski min_size, &pf->data_vnic_bar);
447a5950182SSimon Horman if (IS_ERR(mem)) {
448a5950182SSimon Horman nfp_err(pf->cpp, "Failed to find data vNIC memory symbol\n");
449d557ee6bSJakub Kicinski return PTR_ERR(mem);
450a5950182SSimon Horman }
451a5950182SSimon Horman
452c55ca688SJakub Kicinski if (pf->eth_tbl) {
453f8473024SJakub Kicinski min_size = NFP_MAC_STATS_SIZE * (pf->eth_tbl->max_index + 1);
454f8473024SJakub Kicinski pf->mac_stats_mem = nfp_rtsym_map(pf->rtbl, "_mac_stats",
455f8473024SJakub Kicinski "net.macstats", min_size,
456a5950182SSimon Horman &pf->mac_stats_bar);
457a5950182SSimon Horman if (IS_ERR(pf->mac_stats_mem)) {
458a5950182SSimon Horman if (PTR_ERR(pf->mac_stats_mem) != -ENOENT) {
459a5950182SSimon Horman err = PTR_ERR(pf->mac_stats_mem);
460a5950182SSimon Horman goto err_unmap_ctrl;
461a5950182SSimon Horman }
462a5950182SSimon Horman pf->mac_stats_mem = NULL;
463a5950182SSimon Horman }
464c55ca688SJakub Kicinski }
465a5950182SSimon Horman
4668f6196f6SJakub Kicinski pf->vf_cfg_mem = nfp_pf_map_rtsym(pf, "net.vfcfg", "_pf%d_net_vf_bar",
4678f6196f6SJakub Kicinski NFP_NET_CFG_BAR_SZ * pf->limit_vfs,
4688f6196f6SJakub Kicinski &pf->vf_cfg_bar);
469a5950182SSimon Horman if (IS_ERR(pf->vf_cfg_mem)) {
470a5950182SSimon Horman if (PTR_ERR(pf->vf_cfg_mem) != -ENOENT) {
471a5950182SSimon Horman err = PTR_ERR(pf->vf_cfg_mem);
472a5950182SSimon Horman goto err_unmap_mac_stats;
473a5950182SSimon Horman }
474a5950182SSimon Horman pf->vf_cfg_mem = NULL;
475a5950182SSimon Horman }
476a5950182SSimon Horman
47725528d90SPablo Cascón min_size = NFP_NET_VF_CFG_SZ * pf->limit_vfs + NFP_NET_VF_CFG_MB_SZ;
4788f6196f6SJakub Kicinski pf->vfcfg_tbl2 = nfp_pf_map_rtsym(pf, "net.vfcfg_tbl2",
47925528d90SPablo Cascón "_pf%d_net_vf_cfg2",
48025528d90SPablo Cascón min_size, &pf->vfcfg_tbl2_area);
48125528d90SPablo Cascón if (IS_ERR(pf->vfcfg_tbl2)) {
48225528d90SPablo Cascón if (PTR_ERR(pf->vfcfg_tbl2) != -ENOENT) {
48325528d90SPablo Cascón err = PTR_ERR(pf->vfcfg_tbl2);
48425528d90SPablo Cascón goto err_unmap_vf_cfg;
48525528d90SPablo Cascón }
48625528d90SPablo Cascón pf->vfcfg_tbl2 = NULL;
48725528d90SPablo Cascón }
48825528d90SPablo Cascón
4898f6d6052SJakub Kicinski cpp_id = NFP_CPP_ISLAND_ID(0, NFP_CPP_ACTION_RW, 0, 0);
490e900db70SJakub Kicinski mem = nfp_cpp_map_area(pf->cpp, "net.qc", cpp_id,
491e900db70SJakub Kicinski nfp_qcp_queue_offset(pf->dev_info, 0),
492e900db70SJakub Kicinski pf->dev_info->qc_area_sz, &pf->qc_area);
493a5950182SSimon Horman if (IS_ERR(mem)) {
494a5950182SSimon Horman nfp_err(pf->cpp, "Failed to map Queue Controller area.\n");
495a5950182SSimon Horman err = PTR_ERR(mem);
49625528d90SPablo Cascón goto err_unmap_vfcfg_tbl2;
497a5950182SSimon Horman }
498a5950182SSimon Horman
499a5950182SSimon Horman return 0;
500a5950182SSimon Horman
50125528d90SPablo Cascón err_unmap_vfcfg_tbl2:
50225528d90SPablo Cascón if (pf->vfcfg_tbl2_area)
50325528d90SPablo Cascón nfp_cpp_area_release_free(pf->vfcfg_tbl2_area);
504a5950182SSimon Horman err_unmap_vf_cfg:
505a5950182SSimon Horman if (pf->vf_cfg_bar)
506a5950182SSimon Horman nfp_cpp_area_release_free(pf->vf_cfg_bar);
507a5950182SSimon Horman err_unmap_mac_stats:
508a5950182SSimon Horman if (pf->mac_stats_bar)
509a5950182SSimon Horman nfp_cpp_area_release_free(pf->mac_stats_bar);
510a5950182SSimon Horman err_unmap_ctrl:
511a5950182SSimon Horman nfp_cpp_area_release_free(pf->data_vnic_bar);
512a5950182SSimon Horman return err;
513a5950182SSimon Horman }
514a5950182SSimon Horman
51562fad9e6SYinjun Zhang static const unsigned int lr_to_speed[] = {
51662fad9e6SYinjun Zhang [NFP_NET_CFG_STS_LINK_RATE_UNSUPPORTED] = 0,
51762fad9e6SYinjun Zhang [NFP_NET_CFG_STS_LINK_RATE_UNKNOWN] = SPEED_UNKNOWN,
51862fad9e6SYinjun Zhang [NFP_NET_CFG_STS_LINK_RATE_1G] = SPEED_1000,
51962fad9e6SYinjun Zhang [NFP_NET_CFG_STS_LINK_RATE_10G] = SPEED_10000,
52062fad9e6SYinjun Zhang [NFP_NET_CFG_STS_LINK_RATE_25G] = SPEED_25000,
52162fad9e6SYinjun Zhang [NFP_NET_CFG_STS_LINK_RATE_40G] = SPEED_40000,
52262fad9e6SYinjun Zhang [NFP_NET_CFG_STS_LINK_RATE_50G] = SPEED_50000,
52362fad9e6SYinjun Zhang [NFP_NET_CFG_STS_LINK_RATE_100G] = SPEED_100000,
52462fad9e6SYinjun Zhang };
52562fad9e6SYinjun Zhang
nfp_net_lr2speed(unsigned int linkrate)52662fad9e6SYinjun Zhang unsigned int nfp_net_lr2speed(unsigned int linkrate)
52762fad9e6SYinjun Zhang {
52862fad9e6SYinjun Zhang if (linkrate < ARRAY_SIZE(lr_to_speed))
52962fad9e6SYinjun Zhang return lr_to_speed[linkrate];
53062fad9e6SYinjun Zhang
53162fad9e6SYinjun Zhang return SPEED_UNKNOWN;
53262fad9e6SYinjun Zhang }
53362fad9e6SYinjun Zhang
nfp_net_speed2lr(unsigned int speed)53462fad9e6SYinjun Zhang unsigned int nfp_net_speed2lr(unsigned int speed)
53562fad9e6SYinjun Zhang {
53662fad9e6SYinjun Zhang int i;
53762fad9e6SYinjun Zhang
53862fad9e6SYinjun Zhang for (i = 0; i < ARRAY_SIZE(lr_to_speed); i++) {
53962fad9e6SYinjun Zhang if (speed == lr_to_speed[i])
54062fad9e6SYinjun Zhang return i;
54162fad9e6SYinjun Zhang }
54262fad9e6SYinjun Zhang
54362fad9e6SYinjun Zhang return NFP_NET_CFG_STS_LINK_RATE_UNKNOWN;
54462fad9e6SYinjun Zhang }
54562fad9e6SYinjun Zhang
nfp_net_notify_port_speed(struct nfp_port * port)54662fad9e6SYinjun Zhang static void nfp_net_notify_port_speed(struct nfp_port *port)
54762fad9e6SYinjun Zhang {
54862fad9e6SYinjun Zhang struct net_device *netdev = port->netdev;
54962fad9e6SYinjun Zhang struct nfp_net *nn;
55062fad9e6SYinjun Zhang u16 sts;
55162fad9e6SYinjun Zhang
55262fad9e6SYinjun Zhang if (!nfp_netdev_is_nfp_net(netdev))
55362fad9e6SYinjun Zhang return;
55462fad9e6SYinjun Zhang
55562fad9e6SYinjun Zhang nn = netdev_priv(netdev);
55662fad9e6SYinjun Zhang sts = nn_readw(nn, NFP_NET_CFG_STS);
55762fad9e6SYinjun Zhang
55862fad9e6SYinjun Zhang if (!(sts & NFP_NET_CFG_STS_LINK)) {
55962fad9e6SYinjun Zhang nn_writew(nn, NFP_NET_CFG_STS_NSP_LINK_RATE, NFP_NET_CFG_STS_LINK_RATE_UNKNOWN);
56062fad9e6SYinjun Zhang return;
56162fad9e6SYinjun Zhang }
56262fad9e6SYinjun Zhang
56362fad9e6SYinjun Zhang nn_writew(nn, NFP_NET_CFG_STS_NSP_LINK_RATE, nfp_net_speed2lr(port->eth_port->speed));
56462fad9e6SYinjun Zhang }
56562fad9e6SYinjun Zhang
5663d4ed1e7SJakub Kicinski static int
nfp_net_eth_port_update(struct nfp_cpp * cpp,struct nfp_port * port,struct nfp_eth_table * eth_table)5673d4ed1e7SJakub Kicinski nfp_net_eth_port_update(struct nfp_cpp *cpp, struct nfp_port *port,
5683d4ed1e7SJakub Kicinski struct nfp_eth_table *eth_table)
5693d4ed1e7SJakub Kicinski {
5703d4ed1e7SJakub Kicinski struct nfp_eth_table_port *eth_port;
5713d4ed1e7SJakub Kicinski
5723d4ed1e7SJakub Kicinski ASSERT_RTNL();
5733d4ed1e7SJakub Kicinski
5743d4ed1e7SJakub Kicinski eth_port = nfp_net_find_port(eth_table, port->eth_id);
5753d4ed1e7SJakub Kicinski if (!eth_port) {
57646b25031SJakub Kicinski set_bit(NFP_PORT_CHANGED, &port->flags);
5773d4ed1e7SJakub Kicinski nfp_warn(cpp, "Warning: port #%d not present after reconfig\n",
5783d4ed1e7SJakub Kicinski port->eth_id);
5793d4ed1e7SJakub Kicinski return -EIO;
5803d4ed1e7SJakub Kicinski }
5813d4ed1e7SJakub Kicinski if (eth_port->override_changed) {
5827717c319SJakub Kicinski nfp_warn(cpp, "Port #%d config changed, unregistering. Driver reload required before port will be operational again.\n", port->eth_id);
5833d4ed1e7SJakub Kicinski port->type = NFP_PORT_INVALID;
5843d4ed1e7SJakub Kicinski }
5853d4ed1e7SJakub Kicinski
5863d4ed1e7SJakub Kicinski memcpy(port->eth_port, eth_port, sizeof(*eth_port));
58762fad9e6SYinjun Zhang nfp_net_notify_port_speed(port);
5883d4ed1e7SJakub Kicinski
5893d4ed1e7SJakub Kicinski return 0;
5903d4ed1e7SJakub Kicinski }
5913d4ed1e7SJakub Kicinski
nfp_net_refresh_port_table_sync(struct nfp_pf * pf)592ec8b1fbeSJakub Kicinski int nfp_net_refresh_port_table_sync(struct nfp_pf *pf)
593172f638cSJakub Kicinski {
594162cca42SJakub Kicinski struct devlink *devlink = priv_to_devlink(pf);
59590fdc561SJakub Kicinski struct nfp_eth_table *eth_table;
596172f638cSJakub Kicinski struct nfp_net *nn, *next;
5973eb3b74aSJakub Kicinski struct nfp_port *port;
5985fa27d59SDirk van der Merwe int err;
599172f638cSJakub Kicinski
600162cca42SJakub Kicinski devl_assert_locked(devlink);
601172f638cSJakub Kicinski
602172f638cSJakub Kicinski /* Check for nfp_net_pci_remove() racing against us */
603d4e7f092SJakub Kicinski if (list_empty(&pf->vnics))
604ec8b1fbeSJakub Kicinski return 0;
605172f638cSJakub Kicinski
6066d4f8cbaSJakub Kicinski /* Update state of all ports */
6076d4f8cbaSJakub Kicinski rtnl_lock();
6083eb3b74aSJakub Kicinski list_for_each_entry(port, &pf->ports, port_list)
6093eb3b74aSJakub Kicinski clear_bit(NFP_PORT_CHANGED, &port->flags);
61090fdc561SJakub Kicinski
61190fdc561SJakub Kicinski eth_table = nfp_eth_read_ports(pf->cpp);
61290fdc561SJakub Kicinski if (!eth_table) {
61346b25031SJakub Kicinski list_for_each_entry(port, &pf->ports, port_list)
61446b25031SJakub Kicinski if (__nfp_port_get_eth_port(port))
61546b25031SJakub Kicinski set_bit(NFP_PORT_CHANGED, &port->flags);
6166d4f8cbaSJakub Kicinski rtnl_unlock();
61790fdc561SJakub Kicinski nfp_err(pf->cpp, "Error refreshing port config!\n");
618ec8b1fbeSJakub Kicinski return -EIO;
61990fdc561SJakub Kicinski }
62090fdc561SJakub Kicinski
6213eb3b74aSJakub Kicinski list_for_each_entry(port, &pf->ports, port_list)
6223eb3b74aSJakub Kicinski if (__nfp_port_get_eth_port(port))
6233eb3b74aSJakub Kicinski nfp_net_eth_port_update(pf->cpp, port, eth_table);
62490fdc561SJakub Kicinski rtnl_unlock();
62590fdc561SJakub Kicinski
6263d4ed1e7SJakub Kicinski kfree(eth_table);
62790fdc561SJakub Kicinski
6285fa27d59SDirk van der Merwe /* Resync repr state. This may cause reprs to be removed. */
6295fa27d59SDirk van der Merwe err = nfp_reprs_resync_phys_ports(pf->app);
6305fa27d59SDirk van der Merwe if (err)
6315fa27d59SDirk van der Merwe return err;
6325fa27d59SDirk van der Merwe
6336d4f8cbaSJakub Kicinski /* Shoot off the ports which became invalid */
634d4e7f092SJakub Kicinski list_for_each_entry_safe(nn, next, &pf->vnics, vnic_list) {
635eb488c26SJakub Kicinski if (!nn->port || nn->port->type != NFP_PORT_INVALID)
636172f638cSJakub Kicinski continue;
637172f638cSJakub Kicinski
63871f8a116SJakub Kicinski nfp_net_pf_clean_vnic(pf, nn);
6399140b30dSJakub Kicinski nfp_net_pf_free_vnic(pf, nn);
640172f638cSJakub Kicinski }
641172f638cSJakub Kicinski
642ec8b1fbeSJakub Kicinski return 0;
643ec8b1fbeSJakub Kicinski }
644ec8b1fbeSJakub Kicinski
nfp_net_refresh_vnics(struct work_struct * work)645ec8b1fbeSJakub Kicinski static void nfp_net_refresh_vnics(struct work_struct *work)
646ec8b1fbeSJakub Kicinski {
647ec8b1fbeSJakub Kicinski struct nfp_pf *pf = container_of(work, struct nfp_pf,
648ec8b1fbeSJakub Kicinski port_refresh_work);
649162cca42SJakub Kicinski struct devlink *devlink = priv_to_devlink(pf);
650ec8b1fbeSJakub Kicinski
651162cca42SJakub Kicinski devl_lock(devlink);
652ec8b1fbeSJakub Kicinski nfp_net_refresh_port_table_sync(pf);
653162cca42SJakub Kicinski devl_unlock(devlink);
654172f638cSJakub Kicinski }
655172f638cSJakub Kicinski
nfp_net_refresh_port_table(struct nfp_port * port)656eb488c26SJakub Kicinski void nfp_net_refresh_port_table(struct nfp_port *port)
657172f638cSJakub Kicinski {
658eb488c26SJakub Kicinski struct nfp_pf *pf = port->app->pf;
659172f638cSJakub Kicinski
6601f60a581SJakub Kicinski set_bit(NFP_PORT_CHANGED, &port->flags);
6611f60a581SJakub Kicinski
6626d48ceb2SJakub Kicinski queue_work(pf->wq, &pf->port_refresh_work);
663172f638cSJakub Kicinski }
664172f638cSJakub Kicinski
nfp_net_refresh_eth_port(struct nfp_port * port)665eb488c26SJakub Kicinski int nfp_net_refresh_eth_port(struct nfp_port *port)
66690fdc561SJakub Kicinski {
667eb488c26SJakub Kicinski struct nfp_cpp *cpp = port->app->cpp;
66890fdc561SJakub Kicinski struct nfp_eth_table *eth_table;
6693d4ed1e7SJakub Kicinski int ret;
67090fdc561SJakub Kicinski
6716d4f8cbaSJakub Kicinski clear_bit(NFP_PORT_CHANGED, &port->flags);
6726d4f8cbaSJakub Kicinski
673eb488c26SJakub Kicinski eth_table = nfp_eth_read_ports(cpp);
67490fdc561SJakub Kicinski if (!eth_table) {
67546b25031SJakub Kicinski set_bit(NFP_PORT_CHANGED, &port->flags);
676eb488c26SJakub Kicinski nfp_err(cpp, "Error refreshing port state table!\n");
67790fdc561SJakub Kicinski return -EIO;
67890fdc561SJakub Kicinski }
67990fdc561SJakub Kicinski
6803d4ed1e7SJakub Kicinski ret = nfp_net_eth_port_update(cpp, port, eth_table);
68190fdc561SJakub Kicinski
68290fdc561SJakub Kicinski kfree(eth_table);
68390fdc561SJakub Kicinski
6843d4ed1e7SJakub Kicinski return ret;
68590fdc561SJakub Kicinski }
68690fdc561SJakub Kicinski
68763461a02SJakub Kicinski /*
68863461a02SJakub Kicinski * PCI device functions
68963461a02SJakub Kicinski */
nfp_net_pci_probe(struct nfp_pf * pf)69063461a02SJakub Kicinski int nfp_net_pci_probe(struct nfp_pf *pf)
69163461a02SJakub Kicinski {
692bcc93a23SJakub Kicinski struct devlink *devlink = priv_to_devlink(pf);
69363461a02SJakub Kicinski struct nfp_net_fw_version fw_ver;
69473e253f0SJakub Kicinski u8 __iomem *ctrl_bar, *qc_bar;
69563461a02SJakub Kicinski int stride;
69663461a02SJakub Kicinski int err;
69763461a02SJakub Kicinski
698d4e7f092SJakub Kicinski INIT_WORK(&pf->port_refresh_work, nfp_net_refresh_vnics);
699d12537dfSJakub Kicinski
700d557ee6bSJakub Kicinski if (!pf->rtbl) {
701d557ee6bSJakub Kicinski nfp_err(pf->cpp, "No %s, giving up.\n",
702d557ee6bSJakub Kicinski pf->fw_loaded ? "symbol table" : "firmware found");
70326a8985fSJakub Kicinski return -EINVAL;
704d557ee6bSJakub Kicinski }
705d557ee6bSJakub Kicinski
706d4e7f092SJakub Kicinski pf->max_data_vnics = nfp_net_pf_get_num_ports(pf);
707bcc93a23SJakub Kicinski if ((int)pf->max_data_vnics < 0)
708bcc93a23SJakub Kicinski return pf->max_data_vnics;
70963461a02SJakub Kicinski
710a5950182SSimon Horman err = nfp_net_pci_map_mem(pf);
711a5950182SSimon Horman if (err)
712bcc93a23SJakub Kicinski return err;
713a5950182SSimon Horman
714a5950182SSimon Horman ctrl_bar = nfp_cpp_area_iomem(pf->data_vnic_bar);
715a5950182SSimon Horman qc_bar = nfp_cpp_area_iomem(pf->qc_area);
716a5950182SSimon Horman if (!ctrl_bar || !qc_bar) {
717a5950182SSimon Horman err = -EIO;
718a5950182SSimon Horman goto err_unmap;
719d12537dfSJakub Kicinski }
72063461a02SJakub Kicinski
72163461a02SJakub Kicinski nfp_net_get_fw_version(&fw_ver, ctrl_bar);
722d9e3c299SJakub Kicinski if (fw_ver.extend & NFP_NET_CFG_VERSION_RESERVED_MASK ||
723d9e3c299SJakub Kicinski fw_ver.class != NFP_NET_CFG_VERSION_CLASS_GENERIC) {
72463461a02SJakub Kicinski nfp_err(pf->cpp, "Unknown Firmware ABI %d.%d.%d.%d\n",
725d9e3c299SJakub Kicinski fw_ver.extend, fw_ver.class,
726d9e3c299SJakub Kicinski fw_ver.major, fw_ver.minor);
72763461a02SJakub Kicinski err = -EINVAL;
728a5950182SSimon Horman goto err_unmap;
72963461a02SJakub Kicinski }
73063461a02SJakub Kicinski
73163461a02SJakub Kicinski /* Determine stride */
73263461a02SJakub Kicinski if (nfp_net_fw_ver_eq(&fw_ver, 0, 0, 0, 1)) {
73363461a02SJakub Kicinski stride = 2;
73463461a02SJakub Kicinski nfp_warn(pf->cpp, "OBSOLETE Firmware detected - VF isolation not available\n");
73563461a02SJakub Kicinski } else {
73663461a02SJakub Kicinski switch (fw_ver.major) {
737f9380629SJakub Kicinski case 1 ... 5:
73863461a02SJakub Kicinski stride = 4;
73963461a02SJakub Kicinski break;
74063461a02SJakub Kicinski default:
74163461a02SJakub Kicinski nfp_err(pf->cpp, "Unsupported Firmware ABI %d.%d.%d.%d\n",
742d9e3c299SJakub Kicinski fw_ver.extend, fw_ver.class,
74363461a02SJakub Kicinski fw_ver.major, fw_ver.minor);
74463461a02SJakub Kicinski err = -EINVAL;
745a5950182SSimon Horman goto err_unmap;
74663461a02SJakub Kicinski }
74763461a02SJakub Kicinski }
74863461a02SJakub Kicinski
74902082701SJakub Kicinski err = nfp_net_pf_app_init(pf, qc_bar, stride);
7507ac9ebd5SJakub Kicinski if (err)
751a5950182SSimon Horman goto err_unmap;
7527ac9ebd5SJakub Kicinski
753a0d163f4SJakub Kicinski err = nfp_shared_buf_register(pf);
754a0d163f4SJakub Kicinski if (err)
755a0d163f4SJakub Kicinski goto err_devlink_unreg;
756a0d163f4SJakub Kicinski
757*075935f0SJiri Pirko devl_lock(devlink);
758165c3c9fSDirk van der Merwe err = nfp_devlink_params_register(pf);
759165c3c9fSDirk van der Merwe if (err)
760165c3c9fSDirk van der Merwe goto err_shared_buf_unreg;
761165c3c9fSDirk van der Merwe
76263461a02SJakub Kicinski pf->ddir = nfp_net_debugfs_device_add(pf->pdev);
76363461a02SJakub Kicinski
7646d4b0d8eSJakub Kicinski /* Allocate the vnics and do basic init */
7656d4b0d8eSJakub Kicinski err = nfp_net_pf_alloc_vnics(pf, ctrl_bar, qc_bar, stride);
76663461a02SJakub Kicinski if (err)
76763461a02SJakub Kicinski goto err_clean_ddir;
76863461a02SJakub Kicinski
7692b88354dSYinjun Zhang err = nfp_net_pf_alloc_irqs(pf);
7702b88354dSYinjun Zhang if (err)
771b1e4f11eSYinjun Zhang goto err_free_vnics;
7722b88354dSYinjun Zhang
7739d8b17beSJakub Kicinski err = nfp_net_pf_app_start(pf);
7746d4b0d8eSJakub Kicinski if (err)
7756d4b0d8eSJakub Kicinski goto err_free_irqs;
7766d4b0d8eSJakub Kicinski
77702082701SJakub Kicinski err = nfp_net_pf_init_vnics(pf);
77802082701SJakub Kicinski if (err)
77902082701SJakub Kicinski goto err_stop_app;
78002082701SJakub Kicinski
781162cca42SJakub Kicinski devl_unlock(devlink);
7824f2a81c4SLeon Romanovsky devlink_register(devlink);
783d12537dfSJakub Kicinski
78463461a02SJakub Kicinski return 0;
78563461a02SJakub Kicinski
78602082701SJakub Kicinski err_stop_app:
7879d8b17beSJakub Kicinski nfp_net_pf_app_stop(pf);
7886d4b0d8eSJakub Kicinski err_free_irqs:
7896d4b0d8eSJakub Kicinski nfp_net_pf_free_irqs(pf);
7906d4b0d8eSJakub Kicinski err_free_vnics:
7916d4b0d8eSJakub Kicinski nfp_net_pf_free_vnics(pf);
79263461a02SJakub Kicinski err_clean_ddir:
79363461a02SJakub Kicinski nfp_net_debugfs_dir_clean(&pf->ddir);
794165c3c9fSDirk van der Merwe nfp_devlink_params_unregister(pf);
795165c3c9fSDirk van der Merwe err_shared_buf_unreg:
796*075935f0SJiri Pirko devl_unlock(devlink);
797a0d163f4SJakub Kicinski nfp_shared_buf_unregister(pf);
798a0d163f4SJakub Kicinski err_devlink_unreg:
799bcc93a23SJakub Kicinski cancel_work_sync(&pf->port_refresh_work);
8007ac9ebd5SJakub Kicinski nfp_net_pf_app_clean(pf);
801a5950182SSimon Horman err_unmap:
802a5950182SSimon Horman nfp_net_pci_unmap_mem(pf);
80363461a02SJakub Kicinski return err;
80463461a02SJakub Kicinski }
80563461a02SJakub Kicinski
nfp_net_pci_remove(struct nfp_pf * pf)80663461a02SJakub Kicinski void nfp_net_pci_remove(struct nfp_pf *pf)
80763461a02SJakub Kicinski {
808162cca42SJakub Kicinski struct devlink *devlink = priv_to_devlink(pf);
809e1a2599dSJakub Kicinski struct nfp_net *nn, *next;
81063461a02SJakub Kicinski
8114f2a81c4SLeon Romanovsky devlink_unregister(priv_to_devlink(pf));
812162cca42SJakub Kicinski devl_lock(devlink);
813e1a2599dSJakub Kicinski list_for_each_entry_safe(nn, next, &pf->vnics, vnic_list) {
814e1a2599dSJakub Kicinski if (!nfp_net_is_data_vnic(nn))
815e1a2599dSJakub Kicinski continue;
81671f8a116SJakub Kicinski nfp_net_pf_clean_vnic(pf, nn);
817e1a2599dSJakub Kicinski nfp_net_pf_free_vnic(pf, nn);
818e1a2599dSJakub Kicinski }
81963461a02SJakub Kicinski
820e1a2599dSJakub Kicinski nfp_net_pf_app_stop(pf);
821e1a2599dSJakub Kicinski /* stop app first, to avoid double free of ctrl vNIC's ddir */
822e1a2599dSJakub Kicinski nfp_net_debugfs_dir_clean(&pf->ddir);
82363461a02SJakub Kicinski
824*075935f0SJiri Pirko nfp_devlink_params_unregister(pf);
825*075935f0SJiri Pirko
826162cca42SJakub Kicinski devl_unlock(devlink);
827bcc93a23SJakub Kicinski
828a0d163f4SJakub Kicinski nfp_shared_buf_unregister(pf);
829bcc93a23SJakub Kicinski
830e1a2599dSJakub Kicinski nfp_net_pf_free_irqs(pf);
831e1a2599dSJakub Kicinski nfp_net_pf_app_clean(pf);
832e1a2599dSJakub Kicinski nfp_net_pci_unmap_mem(pf);
833e1a2599dSJakub Kicinski
834172f638cSJakub Kicinski cancel_work_sync(&pf->port_refresh_work);
83563461a02SJakub Kicinski }
836