xref: /linux/drivers/net/ethernet/netronome/nfp/abm/main.c (revision f3956ebb)
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 
nfp_abm_portid(enum nfp_repr_type rtype,unsigned int id)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
nfp_abm_setup_tc(struct nfp_app * app,struct net_device * netdev,enum tc_setup_type type,void * type_data)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);
49174ab544SJakub Kicinski 	case TC_SETUP_BLOCK:
50174ab544SJakub Kicinski 		return nfp_abm_setup_cls_block(netdev, repr, type_data);
518c8e6406SJakub Kicinski 	default:
528c8e6406SJakub Kicinski 		return -EOPNOTSUPP;
538c8e6406SJakub Kicinski 	}
548c8e6406SJakub Kicinski }
558c8e6406SJakub Kicinski 
5627f54b58SJohn Hurley static struct net_device *
nfp_abm_repr_get(struct nfp_app * app,u32 port_id,bool * redir_egress)5727f54b58SJohn Hurley nfp_abm_repr_get(struct nfp_app *app, u32 port_id, bool *redir_egress)
58d05d902eSJakub Kicinski {
59d05d902eSJakub Kicinski 	enum nfp_repr_type rtype;
60d05d902eSJakub Kicinski 	struct nfp_reprs *reprs;
61d05d902eSJakub Kicinski 	u8 port;
62d05d902eSJakub Kicinski 
63d05d902eSJakub Kicinski 	rtype = FIELD_GET(NFP_ABM_PORTID_TYPE, port_id);
64d05d902eSJakub Kicinski 	port = FIELD_GET(NFP_ABM_PORTID_ID, port_id);
65d05d902eSJakub Kicinski 
66d05d902eSJakub Kicinski 	reprs = rcu_dereference(app->reprs[rtype]);
67d05d902eSJakub Kicinski 	if (!reprs)
68d05d902eSJakub Kicinski 		return NULL;
69d05d902eSJakub Kicinski 
70d05d902eSJakub Kicinski 	if (port >= reprs->num_reprs)
71d05d902eSJakub Kicinski 		return NULL;
72d05d902eSJakub Kicinski 
73d05d902eSJakub Kicinski 	return rcu_dereference(reprs->reprs[port]);
74d05d902eSJakub Kicinski }
75d05d902eSJakub Kicinski 
76d05d902eSJakub Kicinski static int
nfp_abm_spawn_repr(struct nfp_app * app,struct nfp_abm_link * alink,enum nfp_port_type ptype)77d05d902eSJakub Kicinski nfp_abm_spawn_repr(struct nfp_app *app, struct nfp_abm_link *alink,
78d05d902eSJakub Kicinski 		   enum nfp_port_type ptype)
79d05d902eSJakub Kicinski {
80d05d902eSJakub Kicinski 	struct net_device *netdev;
81d05d902eSJakub Kicinski 	enum nfp_repr_type rtype;
82d05d902eSJakub Kicinski 	struct nfp_reprs *reprs;
83d05d902eSJakub Kicinski 	struct nfp_repr *repr;
84d05d902eSJakub Kicinski 	struct nfp_port *port;
852ef3c253SJakub Kicinski 	unsigned int txqs;
86d05d902eSJakub Kicinski 	int err;
87d05d902eSJakub Kicinski 
882ef3c253SJakub Kicinski 	if (ptype == NFP_PORT_PHYS_PORT) {
89d05d902eSJakub Kicinski 		rtype = NFP_REPR_TYPE_PHYS_PORT;
902ef3c253SJakub Kicinski 		txqs = 1;
912ef3c253SJakub Kicinski 	} else {
92d05d902eSJakub Kicinski 		rtype = NFP_REPR_TYPE_PF;
932ef3c253SJakub Kicinski 		txqs = alink->vnic->max_rx_rings;
942ef3c253SJakub Kicinski 	}
95d05d902eSJakub Kicinski 
962ef3c253SJakub Kicinski 	netdev = nfp_repr_alloc_mqs(app, txqs, 1);
97d05d902eSJakub Kicinski 	if (!netdev)
98d05d902eSJakub Kicinski 		return -ENOMEM;
99d05d902eSJakub Kicinski 	repr = netdev_priv(netdev);
100d05d902eSJakub Kicinski 	repr->app_priv = alink;
101d05d902eSJakub Kicinski 
102d05d902eSJakub Kicinski 	port = nfp_port_alloc(app, ptype, netdev);
103d05d902eSJakub Kicinski 	if (IS_ERR(port)) {
104d05d902eSJakub Kicinski 		err = PTR_ERR(port);
105d05d902eSJakub Kicinski 		goto err_free_repr;
106d05d902eSJakub Kicinski 	}
107d05d902eSJakub Kicinski 
108d05d902eSJakub Kicinski 	if (ptype == NFP_PORT_PHYS_PORT) {
1091f700367SJakub Kicinski 		port->eth_forced = true;
110d05d902eSJakub Kicinski 		err = nfp_port_init_phy_port(app->pf, app, port, alink->id);
111d05d902eSJakub Kicinski 		if (err)
112d05d902eSJakub Kicinski 			goto err_free_port;
113d05d902eSJakub Kicinski 	} else {
114d05d902eSJakub Kicinski 		port->pf_id = alink->abm->pf_id;
115290f54dbSJakub Kicinski 		port->pf_split = app->pf->max_data_vnics > 1;
116290f54dbSJakub Kicinski 		port->pf_split_id = alink->id;
117d05d902eSJakub Kicinski 		port->vnic = alink->vnic->dp.ctrl_bar;
118d05d902eSJakub Kicinski 	}
119d05d902eSJakub Kicinski 
120d05d902eSJakub Kicinski 	SET_NETDEV_DEV(netdev, &alink->vnic->pdev->dev);
121d05d902eSJakub Kicinski 	eth_hw_addr_random(netdev);
122d05d902eSJakub Kicinski 
123d05d902eSJakub Kicinski 	err = nfp_repr_init(app, netdev, nfp_abm_portid(rtype, alink->id),
124d05d902eSJakub Kicinski 			    port, alink->vnic->dp.netdev);
125d05d902eSJakub Kicinski 	if (err)
126d05d902eSJakub Kicinski 		goto err_free_port;
127d05d902eSJakub Kicinski 
128d05d902eSJakub Kicinski 	reprs = nfp_reprs_get_locked(app, rtype);
129d05d902eSJakub Kicinski 	WARN(nfp_repr_get_locked(app, reprs, alink->id), "duplicate repr");
13071844facSJakub Kicinski 	rtnl_lock();
131d05d902eSJakub Kicinski 	rcu_assign_pointer(reprs->reprs[alink->id], netdev);
13271844facSJakub Kicinski 	rtnl_unlock();
133d05d902eSJakub Kicinski 
134d05d902eSJakub Kicinski 	nfp_info(app->cpp, "%s Port %d Representor(%s) created\n",
135d05d902eSJakub Kicinski 		 ptype == NFP_PORT_PF_PORT ? "PCIe" : "Phys",
136d05d902eSJakub Kicinski 		 alink->id, netdev->name);
137d05d902eSJakub Kicinski 
138d05d902eSJakub Kicinski 	return 0;
139d05d902eSJakub Kicinski 
140d05d902eSJakub Kicinski err_free_port:
141d05d902eSJakub Kicinski 	nfp_port_free(port);
142d05d902eSJakub Kicinski err_free_repr:
143d05d902eSJakub Kicinski 	nfp_repr_free(netdev);
144d05d902eSJakub Kicinski 	return err;
145d05d902eSJakub Kicinski }
146d05d902eSJakub Kicinski 
147d05d902eSJakub Kicinski static void
nfp_abm_kill_repr(struct nfp_app * app,struct nfp_abm_link * alink,enum nfp_repr_type rtype)148d05d902eSJakub Kicinski nfp_abm_kill_repr(struct nfp_app *app, struct nfp_abm_link *alink,
149d05d902eSJakub Kicinski 		  enum nfp_repr_type rtype)
150d05d902eSJakub Kicinski {
151d05d902eSJakub Kicinski 	struct net_device *netdev;
152d05d902eSJakub Kicinski 	struct nfp_reprs *reprs;
153d05d902eSJakub Kicinski 
154d05d902eSJakub Kicinski 	reprs = nfp_reprs_get_locked(app, rtype);
155d05d902eSJakub Kicinski 	netdev = nfp_repr_get_locked(app, reprs, alink->id);
156d05d902eSJakub Kicinski 	if (!netdev)
157d05d902eSJakub Kicinski 		return;
15871844facSJakub Kicinski 	rtnl_lock();
159d05d902eSJakub Kicinski 	rcu_assign_pointer(reprs->reprs[alink->id], NULL);
16071844facSJakub Kicinski 	rtnl_unlock();
161d05d902eSJakub Kicinski 	synchronize_rcu();
162d05d902eSJakub Kicinski 	/* Cast to make sure nfp_repr_clean_and_free() takes a nfp_repr */
163d05d902eSJakub Kicinski 	nfp_repr_clean_and_free((struct nfp_repr *)netdev_priv(netdev));
164d05d902eSJakub Kicinski }
165d05d902eSJakub Kicinski 
166d05d902eSJakub Kicinski static void
nfp_abm_kill_reprs(struct nfp_abm * abm,struct nfp_abm_link * alink)167d05d902eSJakub Kicinski nfp_abm_kill_reprs(struct nfp_abm *abm, struct nfp_abm_link *alink)
168d05d902eSJakub Kicinski {
169d05d902eSJakub Kicinski 	nfp_abm_kill_repr(abm->app, alink, NFP_REPR_TYPE_PF);
170d05d902eSJakub Kicinski 	nfp_abm_kill_repr(abm->app, alink, NFP_REPR_TYPE_PHYS_PORT);
171d05d902eSJakub Kicinski }
172d05d902eSJakub Kicinski 
nfp_abm_kill_reprs_all(struct nfp_abm * abm)173d05d902eSJakub Kicinski static void nfp_abm_kill_reprs_all(struct nfp_abm *abm)
174d05d902eSJakub Kicinski {
175d05d902eSJakub Kicinski 	struct nfp_pf *pf = abm->app->pf;
176d05d902eSJakub Kicinski 	struct nfp_net *nn;
177d05d902eSJakub Kicinski 
178d05d902eSJakub Kicinski 	list_for_each_entry(nn, &pf->vnics, vnic_list)
179d05d902eSJakub Kicinski 		nfp_abm_kill_reprs(abm, (struct nfp_abm_link *)nn->app_priv);
180d05d902eSJakub Kicinski }
181d05d902eSJakub Kicinski 
nfp_abm_eswitch_mode_get(struct nfp_app * app)182d05d902eSJakub Kicinski static enum devlink_eswitch_mode nfp_abm_eswitch_mode_get(struct nfp_app *app)
183d05d902eSJakub Kicinski {
184d05d902eSJakub Kicinski 	struct nfp_abm *abm = app->priv;
185d05d902eSJakub Kicinski 
186d05d902eSJakub Kicinski 	return abm->eswitch_mode;
187d05d902eSJakub Kicinski }
188d05d902eSJakub Kicinski 
nfp_abm_eswitch_set_legacy(struct nfp_abm * abm)189d05d902eSJakub Kicinski static int nfp_abm_eswitch_set_legacy(struct nfp_abm *abm)
190d05d902eSJakub Kicinski {
191d05d902eSJakub Kicinski 	nfp_abm_kill_reprs_all(abm);
192055ee0d6SJakub Kicinski 	nfp_abm_ctrl_qm_disable(abm);
193d05d902eSJakub Kicinski 
194d05d902eSJakub Kicinski 	abm->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY;
195d05d902eSJakub Kicinski 	return 0;
196d05d902eSJakub Kicinski }
197d05d902eSJakub Kicinski 
nfp_abm_eswitch_clean_up(struct nfp_abm * abm)198d05d902eSJakub Kicinski static void nfp_abm_eswitch_clean_up(struct nfp_abm *abm)
199d05d902eSJakub Kicinski {
200d05d902eSJakub Kicinski 	if (abm->eswitch_mode != DEVLINK_ESWITCH_MODE_LEGACY)
201d05d902eSJakub Kicinski 		WARN_ON(nfp_abm_eswitch_set_legacy(abm));
202d05d902eSJakub Kicinski }
203d05d902eSJakub Kicinski 
nfp_abm_eswitch_set_switchdev(struct nfp_abm * abm)204d05d902eSJakub Kicinski static int nfp_abm_eswitch_set_switchdev(struct nfp_abm *abm)
205d05d902eSJakub Kicinski {
206d05d902eSJakub Kicinski 	struct nfp_app *app = abm->app;
207d05d902eSJakub Kicinski 	struct nfp_pf *pf = app->pf;
208d05d902eSJakub Kicinski 	struct nfp_net *nn;
209d05d902eSJakub Kicinski 	int err;
210d05d902eSJakub Kicinski 
211036b9e7cSJakub Kicinski 	if (!abm->red_support)
212036b9e7cSJakub Kicinski 		return -EOPNOTSUPP;
213036b9e7cSJakub Kicinski 
214055ee0d6SJakub Kicinski 	err = nfp_abm_ctrl_qm_enable(abm);
215055ee0d6SJakub Kicinski 	if (err)
216055ee0d6SJakub Kicinski 		return err;
217055ee0d6SJakub Kicinski 
218d05d902eSJakub Kicinski 	list_for_each_entry(nn, &pf->vnics, vnic_list) {
219d05d902eSJakub Kicinski 		struct nfp_abm_link *alink = nn->app_priv;
220d05d902eSJakub Kicinski 
221d05d902eSJakub Kicinski 		err = nfp_abm_spawn_repr(app, alink, NFP_PORT_PHYS_PORT);
222d05d902eSJakub Kicinski 		if (err)
223d05d902eSJakub Kicinski 			goto err_kill_all_reprs;
224d05d902eSJakub Kicinski 
225d05d902eSJakub Kicinski 		err = nfp_abm_spawn_repr(app, alink, NFP_PORT_PF_PORT);
226d05d902eSJakub Kicinski 		if (err)
227d05d902eSJakub Kicinski 			goto err_kill_all_reprs;
228d05d902eSJakub Kicinski 	}
229d05d902eSJakub Kicinski 
230d05d902eSJakub Kicinski 	abm->eswitch_mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
231d05d902eSJakub Kicinski 	return 0;
232d05d902eSJakub Kicinski 
233d05d902eSJakub Kicinski err_kill_all_reprs:
234d05d902eSJakub Kicinski 	nfp_abm_kill_reprs_all(abm);
235055ee0d6SJakub Kicinski 	nfp_abm_ctrl_qm_disable(abm);
236d05d902eSJakub Kicinski 	return err;
237d05d902eSJakub Kicinski }
238d05d902eSJakub Kicinski 
nfp_abm_eswitch_mode_set(struct nfp_app * app,u16 mode)239d05d902eSJakub Kicinski static int nfp_abm_eswitch_mode_set(struct nfp_app *app, u16 mode)
240d05d902eSJakub Kicinski {
241d05d902eSJakub Kicinski 	struct nfp_abm *abm = app->priv;
242d05d902eSJakub Kicinski 
243d05d902eSJakub Kicinski 	if (abm->eswitch_mode == mode)
244d05d902eSJakub Kicinski 		return 0;
245d05d902eSJakub Kicinski 
246d05d902eSJakub Kicinski 	switch (mode) {
247d05d902eSJakub Kicinski 	case DEVLINK_ESWITCH_MODE_LEGACY:
248d05d902eSJakub Kicinski 		return nfp_abm_eswitch_set_legacy(abm);
249d05d902eSJakub Kicinski 	case DEVLINK_ESWITCH_MODE_SWITCHDEV:
250d05d902eSJakub Kicinski 		return nfp_abm_eswitch_set_switchdev(abm);
251d05d902eSJakub Kicinski 	default:
252d05d902eSJakub Kicinski 		return -EINVAL;
253d05d902eSJakub Kicinski 	}
254d05d902eSJakub Kicinski }
255d05d902eSJakub Kicinski 
256cc54dc28SJakub Kicinski static void
nfp_abm_vnic_set_mac(struct nfp_pf * pf,struct nfp_abm * abm,struct nfp_net * nn,unsigned int id)257cc54dc28SJakub Kicinski nfp_abm_vnic_set_mac(struct nfp_pf *pf, struct nfp_abm *abm, struct nfp_net *nn,
258cc54dc28SJakub Kicinski 		     unsigned int id)
259cc54dc28SJakub Kicinski {
260cc54dc28SJakub Kicinski 	struct nfp_eth_table_port *eth_port = &pf->eth_tbl->ports[id];
261cc54dc28SJakub Kicinski 	u8 mac_addr[ETH_ALEN];
262f6e71efdSJakub Kicinski 	struct nfp_nsp *nsp;
263f6e71efdSJakub Kicinski 	char hwinfo[32];
264f6e71efdSJakub Kicinski 	int err;
265cc54dc28SJakub Kicinski 
266cc54dc28SJakub Kicinski 	if (id > pf->eth_tbl->count) {
267cc54dc28SJakub Kicinski 		nfp_warn(pf->cpp, "No entry for persistent MAC address\n");
268cc54dc28SJakub Kicinski 		eth_hw_addr_random(nn->dp.netdev);
269cc54dc28SJakub Kicinski 		return;
270cc54dc28SJakub Kicinski 	}
271cc54dc28SJakub Kicinski 
272f6e71efdSJakub Kicinski 	snprintf(hwinfo, sizeof(hwinfo), "eth%u.mac.pf%u",
273cc54dc28SJakub Kicinski 		 eth_port->eth_index, abm->pf_id);
274cc54dc28SJakub Kicinski 
275f6e71efdSJakub Kicinski 	nsp = nfp_nsp_open(pf->cpp);
276f6e71efdSJakub Kicinski 	if (IS_ERR(nsp)) {
277f6e71efdSJakub Kicinski 		nfp_warn(pf->cpp, "Failed to access the NSP for persistent MAC address: %ld\n",
278f6e71efdSJakub Kicinski 			 PTR_ERR(nsp));
279cc54dc28SJakub Kicinski 		eth_hw_addr_random(nn->dp.netdev);
280cc54dc28SJakub Kicinski 		return;
281cc54dc28SJakub Kicinski 	}
282cc54dc28SJakub Kicinski 
283f6e71efdSJakub Kicinski 	if (!nfp_nsp_has_hwinfo_lookup(nsp)) {
284f6e71efdSJakub Kicinski 		nfp_warn(pf->cpp, "NSP doesn't support PF MAC generation\n");
285f6e71efdSJakub Kicinski 		eth_hw_addr_random(nn->dp.netdev);
286bd4af432SQiushi Wu 		nfp_nsp_close(nsp);
287f6e71efdSJakub Kicinski 		return;
288f6e71efdSJakub Kicinski 	}
289f6e71efdSJakub Kicinski 
290f6e71efdSJakub Kicinski 	err = nfp_nsp_hwinfo_lookup(nsp, hwinfo, sizeof(hwinfo));
291f6e71efdSJakub Kicinski 	nfp_nsp_close(nsp);
292f6e71efdSJakub Kicinski 	if (err) {
293f6e71efdSJakub Kicinski 		nfp_warn(pf->cpp, "Reading persistent MAC address failed: %d\n",
294f6e71efdSJakub Kicinski 			 err);
295f6e71efdSJakub Kicinski 		eth_hw_addr_random(nn->dp.netdev);
296f6e71efdSJakub Kicinski 		return;
297f6e71efdSJakub Kicinski 	}
298f6e71efdSJakub Kicinski 
299f6e71efdSJakub Kicinski 	if (sscanf(hwinfo, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
300cc54dc28SJakub Kicinski 		   &mac_addr[0], &mac_addr[1], &mac_addr[2],
301cc54dc28SJakub Kicinski 		   &mac_addr[3], &mac_addr[4], &mac_addr[5]) != 6) {
302cc54dc28SJakub Kicinski 		nfp_warn(pf->cpp, "Can't parse persistent MAC address (%s)\n",
303f6e71efdSJakub Kicinski 			 hwinfo);
304cc54dc28SJakub Kicinski 		eth_hw_addr_random(nn->dp.netdev);
305cc54dc28SJakub Kicinski 		return;
306cc54dc28SJakub Kicinski 	}
307cc54dc28SJakub Kicinski 
308*f3956ebbSJakub Kicinski 	eth_hw_addr_set(nn->dp.netdev, mac_addr);
309cc54dc28SJakub Kicinski 	ether_addr_copy(nn->dp.netdev->perm_addr, mac_addr);
310cc54dc28SJakub Kicinski }
311cc54dc28SJakub Kicinski 
312cc54dc28SJakub Kicinski static int
nfp_abm_vnic_alloc(struct nfp_app * app,struct nfp_net * nn,unsigned int id)313cc54dc28SJakub Kicinski nfp_abm_vnic_alloc(struct nfp_app *app, struct nfp_net *nn, unsigned int id)
314cc54dc28SJakub Kicinski {
3151f700367SJakub Kicinski 	struct nfp_eth_table_port *eth_port = &app->pf->eth_tbl->ports[id];
316cc54dc28SJakub Kicinski 	struct nfp_abm *abm = app->priv;
317cc54dc28SJakub Kicinski 	struct nfp_abm_link *alink;
3181f700367SJakub Kicinski 	int err;
319cc54dc28SJakub Kicinski 
320cc54dc28SJakub Kicinski 	alink = kzalloc(sizeof(*alink), GFP_KERNEL);
321cc54dc28SJakub Kicinski 	if (!alink)
322cc54dc28SJakub Kicinski 		return -ENOMEM;
323cc54dc28SJakub Kicinski 	nn->app_priv = alink;
324cc54dc28SJakub Kicinski 	alink->abm = abm;
325cc54dc28SJakub Kicinski 	alink->vnic = nn;
326cc54dc28SJakub Kicinski 	alink->id = id;
327674cb229SJakub Kicinski 	alink->total_queues = alink->vnic->max_rx_rings;
328cc54dc28SJakub Kicinski 
329174ab544SJakub Kicinski 	INIT_LIST_HEAD(&alink->dscp_map);
330174ab544SJakub Kicinski 
33114780c34SJakub Kicinski 	err = nfp_abm_ctrl_read_params(alink);
33214780c34SJakub Kicinski 	if (err)
33314780c34SJakub Kicinski 		goto err_free_alink;
33414780c34SJakub Kicinski 
335174ab544SJakub Kicinski 	alink->prio_map = kzalloc(abm->prio_map_len, GFP_KERNEL);
3365099dea0SWei Yongjun 	if (!alink->prio_map) {
3375099dea0SWei Yongjun 		err = -ENOMEM;
338174ab544SJakub Kicinski 		goto err_free_alink;
3395099dea0SWei Yongjun 	}
340174ab544SJakub Kicinski 
3411f700367SJakub Kicinski 	/* This is a multi-host app, make sure MAC/PHY is up, but don't
3421f700367SJakub Kicinski 	 * make the MAC/PHY state follow the state of any of the ports.
3431f700367SJakub Kicinski 	 */
3441f700367SJakub Kicinski 	err = nfp_eth_set_configured(app->cpp, eth_port->index, true);
3451f700367SJakub Kicinski 	if (err < 0)
346174ab544SJakub Kicinski 		goto err_free_priomap;
3471f700367SJakub Kicinski 
348d05d902eSJakub Kicinski 	netif_keep_dst(nn->dp.netdev);
349cc54dc28SJakub Kicinski 
350cc54dc28SJakub Kicinski 	nfp_abm_vnic_set_mac(app->pf, abm, nn, id);
3514f5681d0SJakub Kicinski 	INIT_RADIX_TREE(&alink->qdiscs, GFP_KERNEL);
352cc54dc28SJakub Kicinski 
353cc54dc28SJakub Kicinski 	return 0;
3541f700367SJakub Kicinski 
355174ab544SJakub Kicinski err_free_priomap:
356174ab544SJakub Kicinski 	kfree(alink->prio_map);
3571f700367SJakub Kicinski err_free_alink:
3581f700367SJakub Kicinski 	kfree(alink);
3591f700367SJakub Kicinski 	return err;
360cc54dc28SJakub Kicinski }
361cc54dc28SJakub Kicinski 
nfp_abm_vnic_free(struct nfp_app * app,struct nfp_net * nn)362cc54dc28SJakub Kicinski static void nfp_abm_vnic_free(struct nfp_app *app, struct nfp_net *nn)
363cc54dc28SJakub Kicinski {
364cc54dc28SJakub Kicinski 	struct nfp_abm_link *alink = nn->app_priv;
365cc54dc28SJakub Kicinski 
366d05d902eSJakub Kicinski 	nfp_abm_kill_reprs(alink->abm, alink);
3674f5681d0SJakub Kicinski 	WARN(!radix_tree_empty(&alink->qdiscs), "left over qdiscs\n");
368174ab544SJakub Kicinski 	kfree(alink->prio_map);
369cc54dc28SJakub Kicinski 	kfree(alink);
370cc54dc28SJakub Kicinski }
371cc54dc28SJakub Kicinski 
nfp_abm_vnic_init(struct nfp_app * app,struct nfp_net * nn)372174ab544SJakub Kicinski static int nfp_abm_vnic_init(struct nfp_app *app, struct nfp_net *nn)
373174ab544SJakub Kicinski {
374174ab544SJakub Kicinski 	struct nfp_abm_link *alink = nn->app_priv;
375174ab544SJakub Kicinski 
376174ab544SJakub Kicinski 	if (nfp_abm_has_prio(alink->abm))
377174ab544SJakub Kicinski 		return nfp_abm_ctrl_prio_map_update(alink, alink->prio_map);
378174ab544SJakub Kicinski 	return 0;
379174ab544SJakub Kicinski }
380174ab544SJakub Kicinski 
3810a8b7019SJakub Kicinski static u64 *
nfp_abm_port_get_stats(struct nfp_app * app,struct nfp_port * port,u64 * data)3820a8b7019SJakub Kicinski nfp_abm_port_get_stats(struct nfp_app *app, struct nfp_port *port, u64 *data)
3830a8b7019SJakub Kicinski {
3840a8b7019SJakub Kicinski 	struct nfp_repr *repr = netdev_priv(port->netdev);
3850a8b7019SJakub Kicinski 	struct nfp_abm_link *alink;
3860a8b7019SJakub Kicinski 	unsigned int i;
3870a8b7019SJakub Kicinski 
3880a8b7019SJakub Kicinski 	if (port->type != NFP_PORT_PF_PORT)
3890a8b7019SJakub Kicinski 		return data;
3900a8b7019SJakub Kicinski 	alink = repr->app_priv;
3910a8b7019SJakub Kicinski 	for (i = 0; i < alink->vnic->dp.num_r_vecs; i++) {
3920a8b7019SJakub Kicinski 		*data++ = nfp_abm_ctrl_stat_non_sto(alink, i);
3930a8b7019SJakub Kicinski 		*data++ = nfp_abm_ctrl_stat_sto(alink, i);
3940a8b7019SJakub Kicinski 	}
3950a8b7019SJakub Kicinski 	return data;
3960a8b7019SJakub Kicinski }
3970a8b7019SJakub Kicinski 
3980a8b7019SJakub Kicinski static int
nfp_abm_port_get_stats_count(struct nfp_app * app,struct nfp_port * port)3990a8b7019SJakub Kicinski nfp_abm_port_get_stats_count(struct nfp_app *app, struct nfp_port *port)
4000a8b7019SJakub Kicinski {
4010a8b7019SJakub Kicinski 	struct nfp_repr *repr = netdev_priv(port->netdev);
4020a8b7019SJakub Kicinski 	struct nfp_abm_link *alink;
4030a8b7019SJakub Kicinski 
4040a8b7019SJakub Kicinski 	if (port->type != NFP_PORT_PF_PORT)
4050a8b7019SJakub Kicinski 		return 0;
4060a8b7019SJakub Kicinski 	alink = repr->app_priv;
4070a8b7019SJakub Kicinski 	return alink->vnic->dp.num_r_vecs * 2;
4080a8b7019SJakub Kicinski }
4090a8b7019SJakub Kicinski 
4100a8b7019SJakub Kicinski static u8 *
nfp_abm_port_get_stats_strings(struct nfp_app * app,struct nfp_port * port,u8 * data)4110a8b7019SJakub Kicinski nfp_abm_port_get_stats_strings(struct nfp_app *app, struct nfp_port *port,
4120a8b7019SJakub Kicinski 			       u8 *data)
4130a8b7019SJakub Kicinski {
4140a8b7019SJakub Kicinski 	struct nfp_repr *repr = netdev_priv(port->netdev);
4150a8b7019SJakub Kicinski 	struct nfp_abm_link *alink;
4160a8b7019SJakub Kicinski 	unsigned int i;
4170a8b7019SJakub Kicinski 
4180a8b7019SJakub Kicinski 	if (port->type != NFP_PORT_PF_PORT)
4190a8b7019SJakub Kicinski 		return data;
4200a8b7019SJakub Kicinski 	alink = repr->app_priv;
4210a8b7019SJakub Kicinski 	for (i = 0; i < alink->vnic->dp.num_r_vecs; i++) {
4226a143a7cSAlexander Duyck 		ethtool_sprintf(&data, "q%u_no_wait", i);
4236a143a7cSAlexander Duyck 		ethtool_sprintf(&data, "q%u_delayed", i);
4240a8b7019SJakub Kicinski 	}
4250a8b7019SJakub Kicinski 	return data;
4260a8b7019SJakub Kicinski }
4270a8b7019SJakub Kicinski 
nfp_abm_fw_init_reset(struct nfp_abm * abm)428036b9e7cSJakub Kicinski static int nfp_abm_fw_init_reset(struct nfp_abm *abm)
429036b9e7cSJakub Kicinski {
430036b9e7cSJakub Kicinski 	unsigned int i;
431036b9e7cSJakub Kicinski 
432036b9e7cSJakub Kicinski 	if (!abm->red_support)
433036b9e7cSJakub Kicinski 		return 0;
434036b9e7cSJakub Kicinski 
435036b9e7cSJakub Kicinski 	for (i = 0; i < abm->num_bands * NFP_NET_MAX_RX_RINGS; i++) {
436036b9e7cSJakub Kicinski 		__nfp_abm_ctrl_set_q_lvl(abm, i, NFP_ABM_LVL_INFINITY);
437036b9e7cSJakub Kicinski 		__nfp_abm_ctrl_set_q_act(abm, i, NFP_ABM_ACT_DROP);
438036b9e7cSJakub Kicinski 	}
439036b9e7cSJakub Kicinski 
440036b9e7cSJakub Kicinski 	return nfp_abm_ctrl_qm_disable(abm);
441036b9e7cSJakub Kicinski }
442036b9e7cSJakub Kicinski 
nfp_abm_init(struct nfp_app * app)443c4c8f39aSJakub Kicinski static int nfp_abm_init(struct nfp_app *app)
444c4c8f39aSJakub Kicinski {
445c4c8f39aSJakub Kicinski 	struct nfp_pf *pf = app->pf;
446d05d902eSJakub Kicinski 	struct nfp_reprs *reprs;
447c4c8f39aSJakub Kicinski 	struct nfp_abm *abm;
448cc54dc28SJakub Kicinski 	int err;
449c4c8f39aSJakub Kicinski 
450c4c8f39aSJakub Kicinski 	if (!pf->eth_tbl) {
451c4c8f39aSJakub Kicinski 		nfp_err(pf->cpp, "ABM NIC requires ETH table\n");
452c4c8f39aSJakub Kicinski 		return -EINVAL;
453c4c8f39aSJakub Kicinski 	}
454c4c8f39aSJakub Kicinski 	if (pf->max_data_vnics != pf->eth_tbl->count) {
455c4c8f39aSJakub Kicinski 		nfp_err(pf->cpp, "ETH entries don't match vNICs (%d vs %d)\n",
456c4c8f39aSJakub Kicinski 			pf->max_data_vnics, pf->eth_tbl->count);
457c4c8f39aSJakub Kicinski 		return -EINVAL;
458c4c8f39aSJakub Kicinski 	}
459c4c8f39aSJakub Kicinski 	if (!pf->mac_stats_bar) {
460c4c8f39aSJakub Kicinski 		nfp_warn(app->cpp, "ABM NIC requires mac_stats symbol\n");
461c4c8f39aSJakub Kicinski 		return -EINVAL;
462c4c8f39aSJakub Kicinski 	}
463c4c8f39aSJakub Kicinski 
464c4c8f39aSJakub Kicinski 	abm = kzalloc(sizeof(*abm), GFP_KERNEL);
465c4c8f39aSJakub Kicinski 	if (!abm)
466c4c8f39aSJakub Kicinski 		return -ENOMEM;
467c4c8f39aSJakub Kicinski 	app->priv = abm;
468c4c8f39aSJakub Kicinski 	abm->app = app;
469c4c8f39aSJakub Kicinski 
470cc54dc28SJakub Kicinski 	err = nfp_abm_ctrl_find_addrs(abm);
471cc54dc28SJakub Kicinski 	if (err)
472cc54dc28SJakub Kicinski 		goto err_free_abm;
473cc54dc28SJakub Kicinski 
4746666f545SJakub Kicinski 	err = -ENOMEM;
47568e98642SJakub Kicinski 	abm->num_thresholds = array_size(abm->num_bands, NFP_NET_MAX_RX_RINGS);
4766666f545SJakub Kicinski 	abm->threshold_undef = bitmap_zalloc(abm->num_thresholds, GFP_KERNEL);
4776666f545SJakub Kicinski 	if (!abm->threshold_undef)
4786666f545SJakub Kicinski 		goto err_free_abm;
4796666f545SJakub Kicinski 
4806666f545SJakub Kicinski 	abm->thresholds = kvcalloc(abm->num_thresholds,
4816666f545SJakub Kicinski 				   sizeof(*abm->thresholds), GFP_KERNEL);
4826666f545SJakub Kicinski 	if (!abm->thresholds)
4836666f545SJakub Kicinski 		goto err_free_thresh_umap;
4846666f545SJakub Kicinski 
485340a4864SJakub Kicinski 	abm->actions = kvcalloc(abm->num_thresholds, sizeof(*abm->actions),
486340a4864SJakub Kicinski 				GFP_KERNEL);
487340a4864SJakub Kicinski 	if (!abm->actions)
488340a4864SJakub Kicinski 		goto err_free_thresh;
489340a4864SJakub Kicinski 
490055ee0d6SJakub Kicinski 	/* We start in legacy mode, make sure advanced queuing is disabled */
491036b9e7cSJakub Kicinski 	err = nfp_abm_fw_init_reset(abm);
492055ee0d6SJakub Kicinski 	if (err)
493340a4864SJakub Kicinski 		goto err_free_act;
494055ee0d6SJakub Kicinski 
495d05d902eSJakub Kicinski 	err = -ENOMEM;
496d05d902eSJakub Kicinski 	reprs = nfp_reprs_alloc(pf->max_data_vnics);
497d05d902eSJakub Kicinski 	if (!reprs)
498340a4864SJakub Kicinski 		goto err_free_act;
499d05d902eSJakub Kicinski 	RCU_INIT_POINTER(app->reprs[NFP_REPR_TYPE_PHYS_PORT], reprs);
500d05d902eSJakub Kicinski 
501d05d902eSJakub Kicinski 	reprs = nfp_reprs_alloc(pf->max_data_vnics);
502d05d902eSJakub Kicinski 	if (!reprs)
503d05d902eSJakub Kicinski 		goto err_free_phys;
504d05d902eSJakub Kicinski 	RCU_INIT_POINTER(app->reprs[NFP_REPR_TYPE_PF], reprs);
505d05d902eSJakub Kicinski 
506c4c8f39aSJakub Kicinski 	return 0;
507cc54dc28SJakub Kicinski 
508d05d902eSJakub Kicinski err_free_phys:
509d05d902eSJakub Kicinski 	nfp_reprs_clean_and_free_by_type(app, NFP_REPR_TYPE_PHYS_PORT);
510340a4864SJakub Kicinski err_free_act:
511340a4864SJakub Kicinski 	kvfree(abm->actions);
5126666f545SJakub Kicinski err_free_thresh:
5136666f545SJakub Kicinski 	kvfree(abm->thresholds);
5146666f545SJakub Kicinski err_free_thresh_umap:
5156666f545SJakub Kicinski 	bitmap_free(abm->threshold_undef);
516cc54dc28SJakub Kicinski err_free_abm:
517cc54dc28SJakub Kicinski 	kfree(abm);
518cc54dc28SJakub Kicinski 	app->priv = NULL;
519cc54dc28SJakub Kicinski 	return err;
520c4c8f39aSJakub Kicinski }
521c4c8f39aSJakub Kicinski 
nfp_abm_clean(struct nfp_app * app)522c4c8f39aSJakub Kicinski static void nfp_abm_clean(struct nfp_app *app)
523c4c8f39aSJakub Kicinski {
524c4c8f39aSJakub Kicinski 	struct nfp_abm *abm = app->priv;
525c4c8f39aSJakub Kicinski 
526d05d902eSJakub Kicinski 	nfp_abm_eswitch_clean_up(abm);
527d05d902eSJakub Kicinski 	nfp_reprs_clean_and_free_by_type(app, NFP_REPR_TYPE_PF);
528d05d902eSJakub Kicinski 	nfp_reprs_clean_and_free_by_type(app, NFP_REPR_TYPE_PHYS_PORT);
5296666f545SJakub Kicinski 	bitmap_free(abm->threshold_undef);
530340a4864SJakub Kicinski 	kvfree(abm->actions);
5316666f545SJakub Kicinski 	kvfree(abm->thresholds);
532c4c8f39aSJakub Kicinski 	kfree(abm);
533c4c8f39aSJakub Kicinski 	app->priv = NULL;
534c4c8f39aSJakub Kicinski }
535c4c8f39aSJakub Kicinski 
536c4c8f39aSJakub Kicinski const struct nfp_app_type app_abm = {
537c4c8f39aSJakub Kicinski 	.id		= NFP_APP_ACTIVE_BUFFER_MGMT_NIC,
538c4c8f39aSJakub Kicinski 	.name		= "abm",
539c4c8f39aSJakub Kicinski 
540c4c8f39aSJakub Kicinski 	.init		= nfp_abm_init,
541c4c8f39aSJakub Kicinski 	.clean		= nfp_abm_clean,
542c4c8f39aSJakub Kicinski 
543cc54dc28SJakub Kicinski 	.vnic_alloc	= nfp_abm_vnic_alloc,
544cc54dc28SJakub Kicinski 	.vnic_free	= nfp_abm_vnic_free,
545174ab544SJakub Kicinski 	.vnic_init	= nfp_abm_vnic_init,
546d05d902eSJakub Kicinski 
5470a8b7019SJakub Kicinski 	.port_get_stats		= nfp_abm_port_get_stats,
5480a8b7019SJakub Kicinski 	.port_get_stats_count	= nfp_abm_port_get_stats_count,
5490a8b7019SJakub Kicinski 	.port_get_stats_strings	= nfp_abm_port_get_stats_strings,
5500a8b7019SJakub Kicinski 
5518c8e6406SJakub Kicinski 	.setup_tc	= nfp_abm_setup_tc,
5528c8e6406SJakub Kicinski 
553d05d902eSJakub Kicinski 	.eswitch_mode_get	= nfp_abm_eswitch_mode_get,
554d05d902eSJakub Kicinski 	.eswitch_mode_set	= nfp_abm_eswitch_mode_set,
555d05d902eSJakub Kicinski 
55627f54b58SJohn Hurley 	.dev_get	= nfp_abm_repr_get,
557c4c8f39aSJakub Kicinski };
558