xref: /linux/drivers/net/ethernet/netronome/nfp/abm/main.c (revision 14780c34)
196de2506SJakub Kicinski // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
296de2506SJakub Kicinski /* Copyright (C) 2018 Netronome Systems, Inc. */
3c4c8f39aSJakub Kicinski 
4d05d902eSJakub Kicinski #include <linux/bitfield.h>
56666f545SJakub Kicinski #include <linux/bitmap.h>
6cc54dc28SJakub Kicinski #include <linux/etherdevice.h>
7d05d902eSJakub Kicinski #include <linux/lockdep.h>
8d05d902eSJakub Kicinski #include <linux/netdevice.h>
9d05d902eSJakub Kicinski #include <linux/rcupdate.h>
10bd3b5d46SJakub Kicinski #include <linux/rtnetlink.h>
11d05d902eSJakub Kicinski #include <linux/slab.h>
12cc54dc28SJakub Kicinski 
13cc54dc28SJakub Kicinski #include "../nfpcore/nfp.h"
14c4c8f39aSJakub Kicinski #include "../nfpcore/nfp_cpp.h"
15c4c8f39aSJakub Kicinski #include "../nfpcore/nfp_nsp.h"
16c4c8f39aSJakub Kicinski #include "../nfp_app.h"
17c4c8f39aSJakub Kicinski #include "../nfp_main.h"
18cc54dc28SJakub Kicinski #include "../nfp_net.h"
19d05d902eSJakub Kicinski #include "../nfp_net_repr.h"
20cc54dc28SJakub Kicinski #include "../nfp_port.h"
21c4c8f39aSJakub Kicinski #include "main.h"
22c4c8f39aSJakub Kicinski 
23d05d902eSJakub Kicinski static u32 nfp_abm_portid(enum nfp_repr_type rtype, unsigned int id)
24d05d902eSJakub Kicinski {
25d05d902eSJakub Kicinski 	return FIELD_PREP(NFP_ABM_PORTID_TYPE, rtype) |
26d05d902eSJakub Kicinski 	       FIELD_PREP(NFP_ABM_PORTID_ID, id);
27d05d902eSJakub Kicinski }
28d05d902eSJakub Kicinski 
29674cb229SJakub Kicinski static int
308c8e6406SJakub Kicinski nfp_abm_setup_tc(struct nfp_app *app, struct net_device *netdev,
318c8e6406SJakub Kicinski 		 enum tc_setup_type type, void *type_data)
328c8e6406SJakub Kicinski {
338c8e6406SJakub Kicinski 	struct nfp_repr *repr = netdev_priv(netdev);
348c8e6406SJakub Kicinski 	struct nfp_port *port;
358c8e6406SJakub Kicinski 
368c8e6406SJakub Kicinski 	port = nfp_port_from_netdev(netdev);
378c8e6406SJakub Kicinski 	if (!port || port->type != NFP_PORT_PF_PORT)
388c8e6406SJakub Kicinski 		return -EOPNOTSUPP;
398c8e6406SJakub Kicinski 
408c8e6406SJakub Kicinski 	switch (type) {
4118531258SJakub Kicinski 	case TC_SETUP_ROOT_QDISC:
4218531258SJakub Kicinski 		return nfp_abm_setup_root(netdev, repr->app_priv, type_data);
43674cb229SJakub Kicinski 	case TC_SETUP_QDISC_MQ:
44674cb229SJakub Kicinski 		return nfp_abm_setup_tc_mq(netdev, repr->app_priv, type_data);
458c8e6406SJakub Kicinski 	case TC_SETUP_QDISC_RED:
468c8e6406SJakub Kicinski 		return nfp_abm_setup_tc_red(netdev, repr->app_priv, type_data);
47f3d63720SJakub Kicinski 	case TC_SETUP_QDISC_GRED:
48f3d63720SJakub Kicinski 		return nfp_abm_setup_tc_gred(netdev, repr->app_priv, type_data);
498c8e6406SJakub Kicinski 	default:
508c8e6406SJakub Kicinski 		return -EOPNOTSUPP;
518c8e6406SJakub Kicinski 	}
528c8e6406SJakub Kicinski }
538c8e6406SJakub Kicinski 
54d05d902eSJakub Kicinski static struct net_device *nfp_abm_repr_get(struct nfp_app *app, u32 port_id)
55d05d902eSJakub Kicinski {
56d05d902eSJakub Kicinski 	enum nfp_repr_type rtype;
57d05d902eSJakub Kicinski 	struct nfp_reprs *reprs;
58d05d902eSJakub Kicinski 	u8 port;
59d05d902eSJakub Kicinski 
60d05d902eSJakub Kicinski 	rtype = FIELD_GET(NFP_ABM_PORTID_TYPE, port_id);
61d05d902eSJakub Kicinski 	port = FIELD_GET(NFP_ABM_PORTID_ID, port_id);
62d05d902eSJakub Kicinski 
63d05d902eSJakub Kicinski 	reprs = rcu_dereference(app->reprs[rtype]);
64d05d902eSJakub Kicinski 	if (!reprs)
65d05d902eSJakub Kicinski 		return NULL;
66d05d902eSJakub Kicinski 
67d05d902eSJakub Kicinski 	if (port >= reprs->num_reprs)
68d05d902eSJakub Kicinski 		return NULL;
69d05d902eSJakub Kicinski 
70d05d902eSJakub Kicinski 	return rcu_dereference(reprs->reprs[port]);
71d05d902eSJakub Kicinski }
72d05d902eSJakub Kicinski 
73d05d902eSJakub Kicinski static int
74d05d902eSJakub Kicinski nfp_abm_spawn_repr(struct nfp_app *app, struct nfp_abm_link *alink,
75d05d902eSJakub Kicinski 		   enum nfp_port_type ptype)
76d05d902eSJakub Kicinski {
77d05d902eSJakub Kicinski 	struct net_device *netdev;
78d05d902eSJakub Kicinski 	enum nfp_repr_type rtype;
79d05d902eSJakub Kicinski 	struct nfp_reprs *reprs;
80d05d902eSJakub Kicinski 	struct nfp_repr *repr;
81d05d902eSJakub Kicinski 	struct nfp_port *port;
822ef3c253SJakub Kicinski 	unsigned int txqs;
83d05d902eSJakub Kicinski 	int err;
84d05d902eSJakub Kicinski 
852ef3c253SJakub Kicinski 	if (ptype == NFP_PORT_PHYS_PORT) {
86d05d902eSJakub Kicinski 		rtype = NFP_REPR_TYPE_PHYS_PORT;
872ef3c253SJakub Kicinski 		txqs = 1;
882ef3c253SJakub Kicinski 	} else {
89d05d902eSJakub Kicinski 		rtype = NFP_REPR_TYPE_PF;
902ef3c253SJakub Kicinski 		txqs = alink->vnic->max_rx_rings;
912ef3c253SJakub Kicinski 	}
92d05d902eSJakub Kicinski 
932ef3c253SJakub Kicinski 	netdev = nfp_repr_alloc_mqs(app, txqs, 1);
94d05d902eSJakub Kicinski 	if (!netdev)
95d05d902eSJakub Kicinski 		return -ENOMEM;
96d05d902eSJakub Kicinski 	repr = netdev_priv(netdev);
97d05d902eSJakub Kicinski 	repr->app_priv = alink;
98d05d902eSJakub Kicinski 
99d05d902eSJakub Kicinski 	port = nfp_port_alloc(app, ptype, netdev);
100d05d902eSJakub Kicinski 	if (IS_ERR(port)) {
101d05d902eSJakub Kicinski 		err = PTR_ERR(port);
102d05d902eSJakub Kicinski 		goto err_free_repr;
103d05d902eSJakub Kicinski 	}
104d05d902eSJakub Kicinski 
105d05d902eSJakub Kicinski 	if (ptype == NFP_PORT_PHYS_PORT) {
1061f700367SJakub Kicinski 		port->eth_forced = true;
107d05d902eSJakub Kicinski 		err = nfp_port_init_phy_port(app->pf, app, port, alink->id);
108d05d902eSJakub Kicinski 		if (err)
109d05d902eSJakub Kicinski 			goto err_free_port;
110d05d902eSJakub Kicinski 	} else {
111d05d902eSJakub Kicinski 		port->pf_id = alink->abm->pf_id;
112290f54dbSJakub Kicinski 		port->pf_split = app->pf->max_data_vnics > 1;
113290f54dbSJakub Kicinski 		port->pf_split_id = alink->id;
114d05d902eSJakub Kicinski 		port->vnic = alink->vnic->dp.ctrl_bar;
115d05d902eSJakub Kicinski 	}
116d05d902eSJakub Kicinski 
117d05d902eSJakub Kicinski 	SET_NETDEV_DEV(netdev, &alink->vnic->pdev->dev);
118d05d902eSJakub Kicinski 	eth_hw_addr_random(netdev);
119d05d902eSJakub Kicinski 
120d05d902eSJakub Kicinski 	err = nfp_repr_init(app, netdev, nfp_abm_portid(rtype, alink->id),
121d05d902eSJakub Kicinski 			    port, alink->vnic->dp.netdev);
122d05d902eSJakub Kicinski 	if (err)
123d05d902eSJakub Kicinski 		goto err_free_port;
124d05d902eSJakub Kicinski 
125d05d902eSJakub Kicinski 	reprs = nfp_reprs_get_locked(app, rtype);
126d05d902eSJakub Kicinski 	WARN(nfp_repr_get_locked(app, reprs, alink->id), "duplicate repr");
127d05d902eSJakub Kicinski 	rcu_assign_pointer(reprs->reprs[alink->id], netdev);
128d05d902eSJakub Kicinski 
129d05d902eSJakub Kicinski 	nfp_info(app->cpp, "%s Port %d Representor(%s) created\n",
130d05d902eSJakub Kicinski 		 ptype == NFP_PORT_PF_PORT ? "PCIe" : "Phys",
131d05d902eSJakub Kicinski 		 alink->id, netdev->name);
132d05d902eSJakub Kicinski 
133d05d902eSJakub Kicinski 	return 0;
134d05d902eSJakub Kicinski 
135d05d902eSJakub Kicinski err_free_port:
136d05d902eSJakub Kicinski 	nfp_port_free(port);
137d05d902eSJakub Kicinski err_free_repr:
138d05d902eSJakub Kicinski 	nfp_repr_free(netdev);
139d05d902eSJakub Kicinski 	return err;
140d05d902eSJakub Kicinski }
141d05d902eSJakub Kicinski 
142d05d902eSJakub Kicinski static void
143d05d902eSJakub Kicinski nfp_abm_kill_repr(struct nfp_app *app, struct nfp_abm_link *alink,
144d05d902eSJakub Kicinski 		  enum nfp_repr_type rtype)
145d05d902eSJakub Kicinski {
146d05d902eSJakub Kicinski 	struct net_device *netdev;
147d05d902eSJakub Kicinski 	struct nfp_reprs *reprs;
148d05d902eSJakub Kicinski 
149d05d902eSJakub Kicinski 	reprs = nfp_reprs_get_locked(app, rtype);
150d05d902eSJakub Kicinski 	netdev = nfp_repr_get_locked(app, reprs, alink->id);
151d05d902eSJakub Kicinski 	if (!netdev)
152d05d902eSJakub Kicinski 		return;
153d05d902eSJakub Kicinski 	rcu_assign_pointer(reprs->reprs[alink->id], NULL);
154d05d902eSJakub Kicinski 	synchronize_rcu();
155d05d902eSJakub Kicinski 	/* Cast to make sure nfp_repr_clean_and_free() takes a nfp_repr */
156d05d902eSJakub Kicinski 	nfp_repr_clean_and_free((struct nfp_repr *)netdev_priv(netdev));
157d05d902eSJakub Kicinski }
158d05d902eSJakub Kicinski 
159d05d902eSJakub Kicinski static void
160d05d902eSJakub Kicinski nfp_abm_kill_reprs(struct nfp_abm *abm, struct nfp_abm_link *alink)
161d05d902eSJakub Kicinski {
162d05d902eSJakub Kicinski 	nfp_abm_kill_repr(abm->app, alink, NFP_REPR_TYPE_PF);
163d05d902eSJakub Kicinski 	nfp_abm_kill_repr(abm->app, alink, NFP_REPR_TYPE_PHYS_PORT);
164d05d902eSJakub Kicinski }
165d05d902eSJakub Kicinski 
166d05d902eSJakub Kicinski static void nfp_abm_kill_reprs_all(struct nfp_abm *abm)
167d05d902eSJakub Kicinski {
168d05d902eSJakub Kicinski 	struct nfp_pf *pf = abm->app->pf;
169d05d902eSJakub Kicinski 	struct nfp_net *nn;
170d05d902eSJakub Kicinski 
171d05d902eSJakub Kicinski 	list_for_each_entry(nn, &pf->vnics, vnic_list)
172d05d902eSJakub Kicinski 		nfp_abm_kill_reprs(abm, (struct nfp_abm_link *)nn->app_priv);
173d05d902eSJakub Kicinski }
174d05d902eSJakub Kicinski 
175d05d902eSJakub Kicinski static enum devlink_eswitch_mode nfp_abm_eswitch_mode_get(struct nfp_app *app)
176d05d902eSJakub Kicinski {
177d05d902eSJakub Kicinski 	struct nfp_abm *abm = app->priv;
178d05d902eSJakub Kicinski 
179d05d902eSJakub Kicinski 	return abm->eswitch_mode;
180d05d902eSJakub Kicinski }
181d05d902eSJakub Kicinski 
182d05d902eSJakub Kicinski static int nfp_abm_eswitch_set_legacy(struct nfp_abm *abm)
183d05d902eSJakub Kicinski {
184d05d902eSJakub Kicinski 	nfp_abm_kill_reprs_all(abm);
185055ee0d6SJakub Kicinski 	nfp_abm_ctrl_qm_disable(abm);
186d05d902eSJakub Kicinski 
187d05d902eSJakub Kicinski 	abm->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY;
188d05d902eSJakub Kicinski 	return 0;
189d05d902eSJakub Kicinski }
190d05d902eSJakub Kicinski 
191d05d902eSJakub Kicinski static void nfp_abm_eswitch_clean_up(struct nfp_abm *abm)
192d05d902eSJakub Kicinski {
193d05d902eSJakub Kicinski 	if (abm->eswitch_mode != DEVLINK_ESWITCH_MODE_LEGACY)
194d05d902eSJakub Kicinski 		WARN_ON(nfp_abm_eswitch_set_legacy(abm));
195d05d902eSJakub Kicinski }
196d05d902eSJakub Kicinski 
197d05d902eSJakub Kicinski static int nfp_abm_eswitch_set_switchdev(struct nfp_abm *abm)
198d05d902eSJakub Kicinski {
199d05d902eSJakub Kicinski 	struct nfp_app *app = abm->app;
200d05d902eSJakub Kicinski 	struct nfp_pf *pf = app->pf;
201d05d902eSJakub Kicinski 	struct nfp_net *nn;
202d05d902eSJakub Kicinski 	int err;
203d05d902eSJakub Kicinski 
204055ee0d6SJakub Kicinski 	err = nfp_abm_ctrl_qm_enable(abm);
205055ee0d6SJakub Kicinski 	if (err)
206055ee0d6SJakub Kicinski 		return err;
207055ee0d6SJakub Kicinski 
208d05d902eSJakub Kicinski 	list_for_each_entry(nn, &pf->vnics, vnic_list) {
209d05d902eSJakub Kicinski 		struct nfp_abm_link *alink = nn->app_priv;
210d05d902eSJakub Kicinski 
211d05d902eSJakub Kicinski 		err = nfp_abm_spawn_repr(app, alink, NFP_PORT_PHYS_PORT);
212d05d902eSJakub Kicinski 		if (err)
213d05d902eSJakub Kicinski 			goto err_kill_all_reprs;
214d05d902eSJakub Kicinski 
215d05d902eSJakub Kicinski 		err = nfp_abm_spawn_repr(app, alink, NFP_PORT_PF_PORT);
216d05d902eSJakub Kicinski 		if (err)
217d05d902eSJakub Kicinski 			goto err_kill_all_reprs;
218d05d902eSJakub Kicinski 	}
219d05d902eSJakub Kicinski 
220d05d902eSJakub Kicinski 	abm->eswitch_mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
221d05d902eSJakub Kicinski 	return 0;
222d05d902eSJakub Kicinski 
223d05d902eSJakub Kicinski err_kill_all_reprs:
224d05d902eSJakub Kicinski 	nfp_abm_kill_reprs_all(abm);
225055ee0d6SJakub Kicinski 	nfp_abm_ctrl_qm_disable(abm);
226d05d902eSJakub Kicinski 	return err;
227d05d902eSJakub Kicinski }
228d05d902eSJakub Kicinski 
229d05d902eSJakub Kicinski static int nfp_abm_eswitch_mode_set(struct nfp_app *app, u16 mode)
230d05d902eSJakub Kicinski {
231d05d902eSJakub Kicinski 	struct nfp_abm *abm = app->priv;
232d05d902eSJakub Kicinski 
233d05d902eSJakub Kicinski 	if (abm->eswitch_mode == mode)
234d05d902eSJakub Kicinski 		return 0;
235d05d902eSJakub Kicinski 
236d05d902eSJakub Kicinski 	switch (mode) {
237d05d902eSJakub Kicinski 	case DEVLINK_ESWITCH_MODE_LEGACY:
238d05d902eSJakub Kicinski 		return nfp_abm_eswitch_set_legacy(abm);
239d05d902eSJakub Kicinski 	case DEVLINK_ESWITCH_MODE_SWITCHDEV:
240d05d902eSJakub Kicinski 		return nfp_abm_eswitch_set_switchdev(abm);
241d05d902eSJakub Kicinski 	default:
242d05d902eSJakub Kicinski 		return -EINVAL;
243d05d902eSJakub Kicinski 	}
244d05d902eSJakub Kicinski }
245d05d902eSJakub Kicinski 
246cc54dc28SJakub Kicinski static void
247cc54dc28SJakub Kicinski nfp_abm_vnic_set_mac(struct nfp_pf *pf, struct nfp_abm *abm, struct nfp_net *nn,
248cc54dc28SJakub Kicinski 		     unsigned int id)
249cc54dc28SJakub Kicinski {
250cc54dc28SJakub Kicinski 	struct nfp_eth_table_port *eth_port = &pf->eth_tbl->ports[id];
251cc54dc28SJakub Kicinski 	u8 mac_addr[ETH_ALEN];
252f6e71efdSJakub Kicinski 	struct nfp_nsp *nsp;
253f6e71efdSJakub Kicinski 	char hwinfo[32];
254f6e71efdSJakub Kicinski 	int err;
255cc54dc28SJakub Kicinski 
256cc54dc28SJakub Kicinski 	if (id > pf->eth_tbl->count) {
257cc54dc28SJakub Kicinski 		nfp_warn(pf->cpp, "No entry for persistent MAC address\n");
258cc54dc28SJakub Kicinski 		eth_hw_addr_random(nn->dp.netdev);
259cc54dc28SJakub Kicinski 		return;
260cc54dc28SJakub Kicinski 	}
261cc54dc28SJakub Kicinski 
262f6e71efdSJakub Kicinski 	snprintf(hwinfo, sizeof(hwinfo), "eth%u.mac.pf%u",
263cc54dc28SJakub Kicinski 		 eth_port->eth_index, abm->pf_id);
264cc54dc28SJakub Kicinski 
265f6e71efdSJakub Kicinski 	nsp = nfp_nsp_open(pf->cpp);
266f6e71efdSJakub Kicinski 	if (IS_ERR(nsp)) {
267f6e71efdSJakub Kicinski 		nfp_warn(pf->cpp, "Failed to access the NSP for persistent MAC address: %ld\n",
268f6e71efdSJakub Kicinski 			 PTR_ERR(nsp));
269cc54dc28SJakub Kicinski 		eth_hw_addr_random(nn->dp.netdev);
270cc54dc28SJakub Kicinski 		return;
271cc54dc28SJakub Kicinski 	}
272cc54dc28SJakub Kicinski 
273f6e71efdSJakub Kicinski 	if (!nfp_nsp_has_hwinfo_lookup(nsp)) {
274f6e71efdSJakub Kicinski 		nfp_warn(pf->cpp, "NSP doesn't support PF MAC generation\n");
275f6e71efdSJakub Kicinski 		eth_hw_addr_random(nn->dp.netdev);
276f6e71efdSJakub Kicinski 		return;
277f6e71efdSJakub Kicinski 	}
278f6e71efdSJakub Kicinski 
279f6e71efdSJakub Kicinski 	err = nfp_nsp_hwinfo_lookup(nsp, hwinfo, sizeof(hwinfo));
280f6e71efdSJakub Kicinski 	nfp_nsp_close(nsp);
281f6e71efdSJakub Kicinski 	if (err) {
282f6e71efdSJakub Kicinski 		nfp_warn(pf->cpp, "Reading persistent MAC address failed: %d\n",
283f6e71efdSJakub Kicinski 			 err);
284f6e71efdSJakub Kicinski 		eth_hw_addr_random(nn->dp.netdev);
285f6e71efdSJakub Kicinski 		return;
286f6e71efdSJakub Kicinski 	}
287f6e71efdSJakub Kicinski 
288f6e71efdSJakub Kicinski 	if (sscanf(hwinfo, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
289cc54dc28SJakub Kicinski 		   &mac_addr[0], &mac_addr[1], &mac_addr[2],
290cc54dc28SJakub Kicinski 		   &mac_addr[3], &mac_addr[4], &mac_addr[5]) != 6) {
291cc54dc28SJakub Kicinski 		nfp_warn(pf->cpp, "Can't parse persistent MAC address (%s)\n",
292f6e71efdSJakub Kicinski 			 hwinfo);
293cc54dc28SJakub Kicinski 		eth_hw_addr_random(nn->dp.netdev);
294cc54dc28SJakub Kicinski 		return;
295cc54dc28SJakub Kicinski 	}
296cc54dc28SJakub Kicinski 
297cc54dc28SJakub Kicinski 	ether_addr_copy(nn->dp.netdev->dev_addr, mac_addr);
298cc54dc28SJakub Kicinski 	ether_addr_copy(nn->dp.netdev->perm_addr, mac_addr);
299cc54dc28SJakub Kicinski }
300cc54dc28SJakub Kicinski 
301cc54dc28SJakub Kicinski static int
302cc54dc28SJakub Kicinski nfp_abm_vnic_alloc(struct nfp_app *app, struct nfp_net *nn, unsigned int id)
303cc54dc28SJakub Kicinski {
3041f700367SJakub Kicinski 	struct nfp_eth_table_port *eth_port = &app->pf->eth_tbl->ports[id];
305cc54dc28SJakub Kicinski 	struct nfp_abm *abm = app->priv;
306cc54dc28SJakub Kicinski 	struct nfp_abm_link *alink;
3071f700367SJakub Kicinski 	int err;
308cc54dc28SJakub Kicinski 
309cc54dc28SJakub Kicinski 	alink = kzalloc(sizeof(*alink), GFP_KERNEL);
310cc54dc28SJakub Kicinski 	if (!alink)
311cc54dc28SJakub Kicinski 		return -ENOMEM;
312cc54dc28SJakub Kicinski 	nn->app_priv = alink;
313cc54dc28SJakub Kicinski 	alink->abm = abm;
314cc54dc28SJakub Kicinski 	alink->vnic = nn;
315cc54dc28SJakub Kicinski 	alink->id = id;
316674cb229SJakub Kicinski 	alink->total_queues = alink->vnic->max_rx_rings;
317cc54dc28SJakub Kicinski 
318*14780c34SJakub Kicinski 	err = nfp_abm_ctrl_read_params(alink);
319*14780c34SJakub Kicinski 	if (err)
320*14780c34SJakub Kicinski 		goto err_free_alink;
321*14780c34SJakub Kicinski 
3221f700367SJakub Kicinski 	/* This is a multi-host app, make sure MAC/PHY is up, but don't
3231f700367SJakub Kicinski 	 * make the MAC/PHY state follow the state of any of the ports.
3241f700367SJakub Kicinski 	 */
3251f700367SJakub Kicinski 	err = nfp_eth_set_configured(app->cpp, eth_port->index, true);
3261f700367SJakub Kicinski 	if (err < 0)
327bd3b5d46SJakub Kicinski 		goto err_free_alink;
3281f700367SJakub Kicinski 
329d05d902eSJakub Kicinski 	netif_keep_dst(nn->dp.netdev);
330cc54dc28SJakub Kicinski 
331cc54dc28SJakub Kicinski 	nfp_abm_vnic_set_mac(app->pf, abm, nn, id);
3324f5681d0SJakub Kicinski 	INIT_RADIX_TREE(&alink->qdiscs, GFP_KERNEL);
333cc54dc28SJakub Kicinski 
334cc54dc28SJakub Kicinski 	return 0;
3351f700367SJakub Kicinski 
3361f700367SJakub Kicinski err_free_alink:
3371f700367SJakub Kicinski 	kfree(alink);
3381f700367SJakub Kicinski 	return err;
339cc54dc28SJakub Kicinski }
340cc54dc28SJakub Kicinski 
341cc54dc28SJakub Kicinski static void nfp_abm_vnic_free(struct nfp_app *app, struct nfp_net *nn)
342cc54dc28SJakub Kicinski {
343cc54dc28SJakub Kicinski 	struct nfp_abm_link *alink = nn->app_priv;
344cc54dc28SJakub Kicinski 
345d05d902eSJakub Kicinski 	nfp_abm_kill_reprs(alink->abm, alink);
3464f5681d0SJakub Kicinski 	WARN(!radix_tree_empty(&alink->qdiscs), "left over qdiscs\n");
347cc54dc28SJakub Kicinski 	kfree(alink);
348cc54dc28SJakub Kicinski }
349cc54dc28SJakub Kicinski 
3500a8b7019SJakub Kicinski static u64 *
3510a8b7019SJakub Kicinski nfp_abm_port_get_stats(struct nfp_app *app, struct nfp_port *port, u64 *data)
3520a8b7019SJakub Kicinski {
3530a8b7019SJakub Kicinski 	struct nfp_repr *repr = netdev_priv(port->netdev);
3540a8b7019SJakub Kicinski 	struct nfp_abm_link *alink;
3550a8b7019SJakub Kicinski 	unsigned int i;
3560a8b7019SJakub Kicinski 
3570a8b7019SJakub Kicinski 	if (port->type != NFP_PORT_PF_PORT)
3580a8b7019SJakub Kicinski 		return data;
3590a8b7019SJakub Kicinski 	alink = repr->app_priv;
3600a8b7019SJakub Kicinski 	for (i = 0; i < alink->vnic->dp.num_r_vecs; i++) {
3610a8b7019SJakub Kicinski 		*data++ = nfp_abm_ctrl_stat_non_sto(alink, i);
3620a8b7019SJakub Kicinski 		*data++ = nfp_abm_ctrl_stat_sto(alink, i);
3630a8b7019SJakub Kicinski 	}
3640a8b7019SJakub Kicinski 	return data;
3650a8b7019SJakub Kicinski }
3660a8b7019SJakub Kicinski 
3670a8b7019SJakub Kicinski static int
3680a8b7019SJakub Kicinski nfp_abm_port_get_stats_count(struct nfp_app *app, struct nfp_port *port)
3690a8b7019SJakub Kicinski {
3700a8b7019SJakub Kicinski 	struct nfp_repr *repr = netdev_priv(port->netdev);
3710a8b7019SJakub Kicinski 	struct nfp_abm_link *alink;
3720a8b7019SJakub Kicinski 
3730a8b7019SJakub Kicinski 	if (port->type != NFP_PORT_PF_PORT)
3740a8b7019SJakub Kicinski 		return 0;
3750a8b7019SJakub Kicinski 	alink = repr->app_priv;
3760a8b7019SJakub Kicinski 	return alink->vnic->dp.num_r_vecs * 2;
3770a8b7019SJakub Kicinski }
3780a8b7019SJakub Kicinski 
3790a8b7019SJakub Kicinski static u8 *
3800a8b7019SJakub Kicinski nfp_abm_port_get_stats_strings(struct nfp_app *app, struct nfp_port *port,
3810a8b7019SJakub Kicinski 			       u8 *data)
3820a8b7019SJakub Kicinski {
3830a8b7019SJakub Kicinski 	struct nfp_repr *repr = netdev_priv(port->netdev);
3840a8b7019SJakub Kicinski 	struct nfp_abm_link *alink;
3850a8b7019SJakub Kicinski 	unsigned int i;
3860a8b7019SJakub Kicinski 
3870a8b7019SJakub Kicinski 	if (port->type != NFP_PORT_PF_PORT)
3880a8b7019SJakub Kicinski 		return data;
3890a8b7019SJakub Kicinski 	alink = repr->app_priv;
3900a8b7019SJakub Kicinski 	for (i = 0; i < alink->vnic->dp.num_r_vecs; i++) {
3910a8b7019SJakub Kicinski 		data = nfp_pr_et(data, "q%u_no_wait", i);
3920a8b7019SJakub Kicinski 		data = nfp_pr_et(data, "q%u_delayed", i);
3930a8b7019SJakub Kicinski 	}
3940a8b7019SJakub Kicinski 	return data;
3950a8b7019SJakub Kicinski }
3960a8b7019SJakub Kicinski 
397c4c8f39aSJakub Kicinski static int nfp_abm_init(struct nfp_app *app)
398c4c8f39aSJakub Kicinski {
399c4c8f39aSJakub Kicinski 	struct nfp_pf *pf = app->pf;
400d05d902eSJakub Kicinski 	struct nfp_reprs *reprs;
401c4c8f39aSJakub Kicinski 	struct nfp_abm *abm;
4026666f545SJakub Kicinski 	unsigned int i;
403cc54dc28SJakub Kicinski 	int err;
404c4c8f39aSJakub Kicinski 
405c4c8f39aSJakub Kicinski 	if (!pf->eth_tbl) {
406c4c8f39aSJakub Kicinski 		nfp_err(pf->cpp, "ABM NIC requires ETH table\n");
407c4c8f39aSJakub Kicinski 		return -EINVAL;
408c4c8f39aSJakub Kicinski 	}
409c4c8f39aSJakub Kicinski 	if (pf->max_data_vnics != pf->eth_tbl->count) {
410c4c8f39aSJakub Kicinski 		nfp_err(pf->cpp, "ETH entries don't match vNICs (%d vs %d)\n",
411c4c8f39aSJakub Kicinski 			pf->max_data_vnics, pf->eth_tbl->count);
412c4c8f39aSJakub Kicinski 		return -EINVAL;
413c4c8f39aSJakub Kicinski 	}
414c4c8f39aSJakub Kicinski 	if (!pf->mac_stats_bar) {
415c4c8f39aSJakub Kicinski 		nfp_warn(app->cpp, "ABM NIC requires mac_stats symbol\n");
416c4c8f39aSJakub Kicinski 		return -EINVAL;
417c4c8f39aSJakub Kicinski 	}
418c4c8f39aSJakub Kicinski 
419c4c8f39aSJakub Kicinski 	abm = kzalloc(sizeof(*abm), GFP_KERNEL);
420c4c8f39aSJakub Kicinski 	if (!abm)
421c4c8f39aSJakub Kicinski 		return -ENOMEM;
422c4c8f39aSJakub Kicinski 	app->priv = abm;
423c4c8f39aSJakub Kicinski 	abm->app = app;
424c4c8f39aSJakub Kicinski 
425cc54dc28SJakub Kicinski 	err = nfp_abm_ctrl_find_addrs(abm);
426cc54dc28SJakub Kicinski 	if (err)
427cc54dc28SJakub Kicinski 		goto err_free_abm;
428cc54dc28SJakub Kicinski 
4296666f545SJakub Kicinski 	err = -ENOMEM;
43068e98642SJakub Kicinski 	abm->num_thresholds = array_size(abm->num_bands, NFP_NET_MAX_RX_RINGS);
4316666f545SJakub Kicinski 	abm->threshold_undef = bitmap_zalloc(abm->num_thresholds, GFP_KERNEL);
4326666f545SJakub Kicinski 	if (!abm->threshold_undef)
4336666f545SJakub Kicinski 		goto err_free_abm;
4346666f545SJakub Kicinski 
4356666f545SJakub Kicinski 	abm->thresholds = kvcalloc(abm->num_thresholds,
4366666f545SJakub Kicinski 				   sizeof(*abm->thresholds), GFP_KERNEL);
4376666f545SJakub Kicinski 	if (!abm->thresholds)
4386666f545SJakub Kicinski 		goto err_free_thresh_umap;
43968e98642SJakub Kicinski 	for (i = 0; i < abm->num_bands * NFP_NET_MAX_RX_RINGS; i++)
4406666f545SJakub Kicinski 		__nfp_abm_ctrl_set_q_lvl(abm, i, NFP_ABM_LVL_INFINITY);
4416666f545SJakub Kicinski 
442055ee0d6SJakub Kicinski 	/* We start in legacy mode, make sure advanced queuing is disabled */
443055ee0d6SJakub Kicinski 	err = nfp_abm_ctrl_qm_disable(abm);
444055ee0d6SJakub Kicinski 	if (err)
4456666f545SJakub Kicinski 		goto err_free_thresh;
446055ee0d6SJakub Kicinski 
447d05d902eSJakub Kicinski 	err = -ENOMEM;
448d05d902eSJakub Kicinski 	reprs = nfp_reprs_alloc(pf->max_data_vnics);
449d05d902eSJakub Kicinski 	if (!reprs)
4506666f545SJakub Kicinski 		goto err_free_thresh;
451d05d902eSJakub Kicinski 	RCU_INIT_POINTER(app->reprs[NFP_REPR_TYPE_PHYS_PORT], reprs);
452d05d902eSJakub Kicinski 
453d05d902eSJakub Kicinski 	reprs = nfp_reprs_alloc(pf->max_data_vnics);
454d05d902eSJakub Kicinski 	if (!reprs)
455d05d902eSJakub Kicinski 		goto err_free_phys;
456d05d902eSJakub Kicinski 	RCU_INIT_POINTER(app->reprs[NFP_REPR_TYPE_PF], reprs);
457d05d902eSJakub Kicinski 
458c4c8f39aSJakub Kicinski 	return 0;
459cc54dc28SJakub Kicinski 
460d05d902eSJakub Kicinski err_free_phys:
461d05d902eSJakub Kicinski 	nfp_reprs_clean_and_free_by_type(app, NFP_REPR_TYPE_PHYS_PORT);
4626666f545SJakub Kicinski err_free_thresh:
4636666f545SJakub Kicinski 	kvfree(abm->thresholds);
4646666f545SJakub Kicinski err_free_thresh_umap:
4656666f545SJakub Kicinski 	bitmap_free(abm->threshold_undef);
466cc54dc28SJakub Kicinski err_free_abm:
467cc54dc28SJakub Kicinski 	kfree(abm);
468cc54dc28SJakub Kicinski 	app->priv = NULL;
469cc54dc28SJakub Kicinski 	return err;
470c4c8f39aSJakub Kicinski }
471c4c8f39aSJakub Kicinski 
472c4c8f39aSJakub Kicinski static void nfp_abm_clean(struct nfp_app *app)
473c4c8f39aSJakub Kicinski {
474c4c8f39aSJakub Kicinski 	struct nfp_abm *abm = app->priv;
475c4c8f39aSJakub Kicinski 
476d05d902eSJakub Kicinski 	nfp_abm_eswitch_clean_up(abm);
477d05d902eSJakub Kicinski 	nfp_reprs_clean_and_free_by_type(app, NFP_REPR_TYPE_PF);
478d05d902eSJakub Kicinski 	nfp_reprs_clean_and_free_by_type(app, NFP_REPR_TYPE_PHYS_PORT);
4796666f545SJakub Kicinski 	bitmap_free(abm->threshold_undef);
4806666f545SJakub Kicinski 	kvfree(abm->thresholds);
481c4c8f39aSJakub Kicinski 	kfree(abm);
482c4c8f39aSJakub Kicinski 	app->priv = NULL;
483c4c8f39aSJakub Kicinski }
484c4c8f39aSJakub Kicinski 
485c4c8f39aSJakub Kicinski const struct nfp_app_type app_abm = {
486c4c8f39aSJakub Kicinski 	.id		= NFP_APP_ACTIVE_BUFFER_MGMT_NIC,
487c4c8f39aSJakub Kicinski 	.name		= "abm",
488c4c8f39aSJakub Kicinski 
489c4c8f39aSJakub Kicinski 	.init		= nfp_abm_init,
490c4c8f39aSJakub Kicinski 	.clean		= nfp_abm_clean,
491c4c8f39aSJakub Kicinski 
492cc54dc28SJakub Kicinski 	.vnic_alloc	= nfp_abm_vnic_alloc,
493cc54dc28SJakub Kicinski 	.vnic_free	= nfp_abm_vnic_free,
494d05d902eSJakub Kicinski 
4950a8b7019SJakub Kicinski 	.port_get_stats		= nfp_abm_port_get_stats,
4960a8b7019SJakub Kicinski 	.port_get_stats_count	= nfp_abm_port_get_stats_count,
4970a8b7019SJakub Kicinski 	.port_get_stats_strings	= nfp_abm_port_get_stats_strings,
4980a8b7019SJakub Kicinski 
4998c8e6406SJakub Kicinski 	.setup_tc	= nfp_abm_setup_tc,
5008c8e6406SJakub Kicinski 
501d05d902eSJakub Kicinski 	.eswitch_mode_get	= nfp_abm_eswitch_mode_get,
502d05d902eSJakub Kicinski 	.eswitch_mode_set	= nfp_abm_eswitch_mode_set,
503d05d902eSJakub Kicinski 
504d05d902eSJakub Kicinski 	.repr_get	= nfp_abm_repr_get,
505c4c8f39aSJakub Kicinski };
506