1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2018 Netronome Systems, Inc. */
3 
4 #include <linux/bitfield.h>
5 #include <linux/bitmap.h>
6 #include <linux/etherdevice.h>
7 #include <linux/lockdep.h>
8 #include <linux/netdevice.h>
9 #include <linux/rcupdate.h>
10 #include <linux/rtnetlink.h>
11 #include <linux/slab.h>
12 
13 #include "../nfpcore/nfp.h"
14 #include "../nfpcore/nfp_cpp.h"
15 #include "../nfpcore/nfp_nsp.h"
16 #include "../nfp_app.h"
17 #include "../nfp_main.h"
18 #include "../nfp_net.h"
19 #include "../nfp_net_repr.h"
20 #include "../nfp_port.h"
21 #include "main.h"
22 
23 static u32 nfp_abm_portid(enum nfp_repr_type rtype, unsigned int id)
24 {
25 	return FIELD_PREP(NFP_ABM_PORTID_TYPE, rtype) |
26 	       FIELD_PREP(NFP_ABM_PORTID_ID, id);
27 }
28 
29 static int
30 nfp_abm_setup_tc(struct nfp_app *app, struct net_device *netdev,
31 		 enum tc_setup_type type, void *type_data)
32 {
33 	struct nfp_repr *repr = netdev_priv(netdev);
34 	struct nfp_port *port;
35 
36 	port = nfp_port_from_netdev(netdev);
37 	if (!port || port->type != NFP_PORT_PF_PORT)
38 		return -EOPNOTSUPP;
39 
40 	switch (type) {
41 	case TC_SETUP_ROOT_QDISC:
42 		return nfp_abm_setup_root(netdev, repr->app_priv, type_data);
43 	case TC_SETUP_QDISC_MQ:
44 		return nfp_abm_setup_tc_mq(netdev, repr->app_priv, type_data);
45 	case TC_SETUP_QDISC_RED:
46 		return nfp_abm_setup_tc_red(netdev, repr->app_priv, type_data);
47 	case TC_SETUP_QDISC_GRED:
48 		return nfp_abm_setup_tc_gred(netdev, repr->app_priv, type_data);
49 	default:
50 		return -EOPNOTSUPP;
51 	}
52 }
53 
54 static struct net_device *nfp_abm_repr_get(struct nfp_app *app, u32 port_id)
55 {
56 	enum nfp_repr_type rtype;
57 	struct nfp_reprs *reprs;
58 	u8 port;
59 
60 	rtype = FIELD_GET(NFP_ABM_PORTID_TYPE, port_id);
61 	port = FIELD_GET(NFP_ABM_PORTID_ID, port_id);
62 
63 	reprs = rcu_dereference(app->reprs[rtype]);
64 	if (!reprs)
65 		return NULL;
66 
67 	if (port >= reprs->num_reprs)
68 		return NULL;
69 
70 	return rcu_dereference(reprs->reprs[port]);
71 }
72 
73 static int
74 nfp_abm_spawn_repr(struct nfp_app *app, struct nfp_abm_link *alink,
75 		   enum nfp_port_type ptype)
76 {
77 	struct net_device *netdev;
78 	enum nfp_repr_type rtype;
79 	struct nfp_reprs *reprs;
80 	struct nfp_repr *repr;
81 	struct nfp_port *port;
82 	unsigned int txqs;
83 	int err;
84 
85 	if (ptype == NFP_PORT_PHYS_PORT) {
86 		rtype = NFP_REPR_TYPE_PHYS_PORT;
87 		txqs = 1;
88 	} else {
89 		rtype = NFP_REPR_TYPE_PF;
90 		txqs = alink->vnic->max_rx_rings;
91 	}
92 
93 	netdev = nfp_repr_alloc_mqs(app, txqs, 1);
94 	if (!netdev)
95 		return -ENOMEM;
96 	repr = netdev_priv(netdev);
97 	repr->app_priv = alink;
98 
99 	port = nfp_port_alloc(app, ptype, netdev);
100 	if (IS_ERR(port)) {
101 		err = PTR_ERR(port);
102 		goto err_free_repr;
103 	}
104 
105 	if (ptype == NFP_PORT_PHYS_PORT) {
106 		port->eth_forced = true;
107 		err = nfp_port_init_phy_port(app->pf, app, port, alink->id);
108 		if (err)
109 			goto err_free_port;
110 	} else {
111 		port->pf_id = alink->abm->pf_id;
112 		port->pf_split = app->pf->max_data_vnics > 1;
113 		port->pf_split_id = alink->id;
114 		port->vnic = alink->vnic->dp.ctrl_bar;
115 	}
116 
117 	SET_NETDEV_DEV(netdev, &alink->vnic->pdev->dev);
118 	eth_hw_addr_random(netdev);
119 
120 	err = nfp_repr_init(app, netdev, nfp_abm_portid(rtype, alink->id),
121 			    port, alink->vnic->dp.netdev);
122 	if (err)
123 		goto err_free_port;
124 
125 	reprs = nfp_reprs_get_locked(app, rtype);
126 	WARN(nfp_repr_get_locked(app, reprs, alink->id), "duplicate repr");
127 	rcu_assign_pointer(reprs->reprs[alink->id], netdev);
128 
129 	nfp_info(app->cpp, "%s Port %d Representor(%s) created\n",
130 		 ptype == NFP_PORT_PF_PORT ? "PCIe" : "Phys",
131 		 alink->id, netdev->name);
132 
133 	return 0;
134 
135 err_free_port:
136 	nfp_port_free(port);
137 err_free_repr:
138 	nfp_repr_free(netdev);
139 	return err;
140 }
141 
142 static void
143 nfp_abm_kill_repr(struct nfp_app *app, struct nfp_abm_link *alink,
144 		  enum nfp_repr_type rtype)
145 {
146 	struct net_device *netdev;
147 	struct nfp_reprs *reprs;
148 
149 	reprs = nfp_reprs_get_locked(app, rtype);
150 	netdev = nfp_repr_get_locked(app, reprs, alink->id);
151 	if (!netdev)
152 		return;
153 	rcu_assign_pointer(reprs->reprs[alink->id], NULL);
154 	synchronize_rcu();
155 	/* Cast to make sure nfp_repr_clean_and_free() takes a nfp_repr */
156 	nfp_repr_clean_and_free((struct nfp_repr *)netdev_priv(netdev));
157 }
158 
159 static void
160 nfp_abm_kill_reprs(struct nfp_abm *abm, struct nfp_abm_link *alink)
161 {
162 	nfp_abm_kill_repr(abm->app, alink, NFP_REPR_TYPE_PF);
163 	nfp_abm_kill_repr(abm->app, alink, NFP_REPR_TYPE_PHYS_PORT);
164 }
165 
166 static void nfp_abm_kill_reprs_all(struct nfp_abm *abm)
167 {
168 	struct nfp_pf *pf = abm->app->pf;
169 	struct nfp_net *nn;
170 
171 	list_for_each_entry(nn, &pf->vnics, vnic_list)
172 		nfp_abm_kill_reprs(abm, (struct nfp_abm_link *)nn->app_priv);
173 }
174 
175 static enum devlink_eswitch_mode nfp_abm_eswitch_mode_get(struct nfp_app *app)
176 {
177 	struct nfp_abm *abm = app->priv;
178 
179 	return abm->eswitch_mode;
180 }
181 
182 static int nfp_abm_eswitch_set_legacy(struct nfp_abm *abm)
183 {
184 	nfp_abm_kill_reprs_all(abm);
185 	nfp_abm_ctrl_qm_disable(abm);
186 
187 	abm->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY;
188 	return 0;
189 }
190 
191 static void nfp_abm_eswitch_clean_up(struct nfp_abm *abm)
192 {
193 	if (abm->eswitch_mode != DEVLINK_ESWITCH_MODE_LEGACY)
194 		WARN_ON(nfp_abm_eswitch_set_legacy(abm));
195 }
196 
197 static int nfp_abm_eswitch_set_switchdev(struct nfp_abm *abm)
198 {
199 	struct nfp_app *app = abm->app;
200 	struct nfp_pf *pf = app->pf;
201 	struct nfp_net *nn;
202 	int err;
203 
204 	err = nfp_abm_ctrl_qm_enable(abm);
205 	if (err)
206 		return err;
207 
208 	list_for_each_entry(nn, &pf->vnics, vnic_list) {
209 		struct nfp_abm_link *alink = nn->app_priv;
210 
211 		err = nfp_abm_spawn_repr(app, alink, NFP_PORT_PHYS_PORT);
212 		if (err)
213 			goto err_kill_all_reprs;
214 
215 		err = nfp_abm_spawn_repr(app, alink, NFP_PORT_PF_PORT);
216 		if (err)
217 			goto err_kill_all_reprs;
218 	}
219 
220 	abm->eswitch_mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
221 	return 0;
222 
223 err_kill_all_reprs:
224 	nfp_abm_kill_reprs_all(abm);
225 	nfp_abm_ctrl_qm_disable(abm);
226 	return err;
227 }
228 
229 static int nfp_abm_eswitch_mode_set(struct nfp_app *app, u16 mode)
230 {
231 	struct nfp_abm *abm = app->priv;
232 
233 	if (abm->eswitch_mode == mode)
234 		return 0;
235 
236 	switch (mode) {
237 	case DEVLINK_ESWITCH_MODE_LEGACY:
238 		return nfp_abm_eswitch_set_legacy(abm);
239 	case DEVLINK_ESWITCH_MODE_SWITCHDEV:
240 		return nfp_abm_eswitch_set_switchdev(abm);
241 	default:
242 		return -EINVAL;
243 	}
244 }
245 
246 static void
247 nfp_abm_vnic_set_mac(struct nfp_pf *pf, struct nfp_abm *abm, struct nfp_net *nn,
248 		     unsigned int id)
249 {
250 	struct nfp_eth_table_port *eth_port = &pf->eth_tbl->ports[id];
251 	u8 mac_addr[ETH_ALEN];
252 	struct nfp_nsp *nsp;
253 	char hwinfo[32];
254 	int err;
255 
256 	if (id > pf->eth_tbl->count) {
257 		nfp_warn(pf->cpp, "No entry for persistent MAC address\n");
258 		eth_hw_addr_random(nn->dp.netdev);
259 		return;
260 	}
261 
262 	snprintf(hwinfo, sizeof(hwinfo), "eth%u.mac.pf%u",
263 		 eth_port->eth_index, abm->pf_id);
264 
265 	nsp = nfp_nsp_open(pf->cpp);
266 	if (IS_ERR(nsp)) {
267 		nfp_warn(pf->cpp, "Failed to access the NSP for persistent MAC address: %ld\n",
268 			 PTR_ERR(nsp));
269 		eth_hw_addr_random(nn->dp.netdev);
270 		return;
271 	}
272 
273 	if (!nfp_nsp_has_hwinfo_lookup(nsp)) {
274 		nfp_warn(pf->cpp, "NSP doesn't support PF MAC generation\n");
275 		eth_hw_addr_random(nn->dp.netdev);
276 		return;
277 	}
278 
279 	err = nfp_nsp_hwinfo_lookup(nsp, hwinfo, sizeof(hwinfo));
280 	nfp_nsp_close(nsp);
281 	if (err) {
282 		nfp_warn(pf->cpp, "Reading persistent MAC address failed: %d\n",
283 			 err);
284 		eth_hw_addr_random(nn->dp.netdev);
285 		return;
286 	}
287 
288 	if (sscanf(hwinfo, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
289 		   &mac_addr[0], &mac_addr[1], &mac_addr[2],
290 		   &mac_addr[3], &mac_addr[4], &mac_addr[5]) != 6) {
291 		nfp_warn(pf->cpp, "Can't parse persistent MAC address (%s)\n",
292 			 hwinfo);
293 		eth_hw_addr_random(nn->dp.netdev);
294 		return;
295 	}
296 
297 	ether_addr_copy(nn->dp.netdev->dev_addr, mac_addr);
298 	ether_addr_copy(nn->dp.netdev->perm_addr, mac_addr);
299 }
300 
301 static int
302 nfp_abm_vnic_alloc(struct nfp_app *app, struct nfp_net *nn, unsigned int id)
303 {
304 	struct nfp_eth_table_port *eth_port = &app->pf->eth_tbl->ports[id];
305 	struct nfp_abm *abm = app->priv;
306 	struct nfp_abm_link *alink;
307 	int err;
308 
309 	alink = kzalloc(sizeof(*alink), GFP_KERNEL);
310 	if (!alink)
311 		return -ENOMEM;
312 	nn->app_priv = alink;
313 	alink->abm = abm;
314 	alink->vnic = nn;
315 	alink->id = id;
316 	alink->total_queues = alink->vnic->max_rx_rings;
317 
318 	/* This is a multi-host app, make sure MAC/PHY is up, but don't
319 	 * make the MAC/PHY state follow the state of any of the ports.
320 	 */
321 	err = nfp_eth_set_configured(app->cpp, eth_port->index, true);
322 	if (err < 0)
323 		goto err_free_alink;
324 
325 	netif_keep_dst(nn->dp.netdev);
326 
327 	nfp_abm_vnic_set_mac(app->pf, abm, nn, id);
328 	nfp_abm_ctrl_read_params(alink);
329 	INIT_RADIX_TREE(&alink->qdiscs, GFP_KERNEL);
330 
331 	return 0;
332 
333 err_free_alink:
334 	kfree(alink);
335 	return err;
336 }
337 
338 static void nfp_abm_vnic_free(struct nfp_app *app, struct nfp_net *nn)
339 {
340 	struct nfp_abm_link *alink = nn->app_priv;
341 
342 	nfp_abm_kill_reprs(alink->abm, alink);
343 	WARN(!radix_tree_empty(&alink->qdiscs), "left over qdiscs\n");
344 	kfree(alink);
345 }
346 
347 static u64 *
348 nfp_abm_port_get_stats(struct nfp_app *app, struct nfp_port *port, u64 *data)
349 {
350 	struct nfp_repr *repr = netdev_priv(port->netdev);
351 	struct nfp_abm_link *alink;
352 	unsigned int i;
353 
354 	if (port->type != NFP_PORT_PF_PORT)
355 		return data;
356 	alink = repr->app_priv;
357 	for (i = 0; i < alink->vnic->dp.num_r_vecs; i++) {
358 		*data++ = nfp_abm_ctrl_stat_non_sto(alink, i);
359 		*data++ = nfp_abm_ctrl_stat_sto(alink, i);
360 	}
361 	return data;
362 }
363 
364 static int
365 nfp_abm_port_get_stats_count(struct nfp_app *app, struct nfp_port *port)
366 {
367 	struct nfp_repr *repr = netdev_priv(port->netdev);
368 	struct nfp_abm_link *alink;
369 
370 	if (port->type != NFP_PORT_PF_PORT)
371 		return 0;
372 	alink = repr->app_priv;
373 	return alink->vnic->dp.num_r_vecs * 2;
374 }
375 
376 static u8 *
377 nfp_abm_port_get_stats_strings(struct nfp_app *app, struct nfp_port *port,
378 			       u8 *data)
379 {
380 	struct nfp_repr *repr = netdev_priv(port->netdev);
381 	struct nfp_abm_link *alink;
382 	unsigned int i;
383 
384 	if (port->type != NFP_PORT_PF_PORT)
385 		return data;
386 	alink = repr->app_priv;
387 	for (i = 0; i < alink->vnic->dp.num_r_vecs; i++) {
388 		data = nfp_pr_et(data, "q%u_no_wait", i);
389 		data = nfp_pr_et(data, "q%u_delayed", i);
390 	}
391 	return data;
392 }
393 
394 static int nfp_abm_init(struct nfp_app *app)
395 {
396 	struct nfp_pf *pf = app->pf;
397 	struct nfp_reprs *reprs;
398 	struct nfp_abm *abm;
399 	unsigned int i;
400 	int err;
401 
402 	if (!pf->eth_tbl) {
403 		nfp_err(pf->cpp, "ABM NIC requires ETH table\n");
404 		return -EINVAL;
405 	}
406 	if (pf->max_data_vnics != pf->eth_tbl->count) {
407 		nfp_err(pf->cpp, "ETH entries don't match vNICs (%d vs %d)\n",
408 			pf->max_data_vnics, pf->eth_tbl->count);
409 		return -EINVAL;
410 	}
411 	if (!pf->mac_stats_bar) {
412 		nfp_warn(app->cpp, "ABM NIC requires mac_stats symbol\n");
413 		return -EINVAL;
414 	}
415 
416 	abm = kzalloc(sizeof(*abm), GFP_KERNEL);
417 	if (!abm)
418 		return -ENOMEM;
419 	app->priv = abm;
420 	abm->app = app;
421 
422 	err = nfp_abm_ctrl_find_addrs(abm);
423 	if (err)
424 		goto err_free_abm;
425 
426 	err = -ENOMEM;
427 	abm->num_thresholds = array_size(abm->num_bands, NFP_NET_MAX_RX_RINGS);
428 	abm->threshold_undef = bitmap_zalloc(abm->num_thresholds, GFP_KERNEL);
429 	if (!abm->threshold_undef)
430 		goto err_free_abm;
431 
432 	abm->thresholds = kvcalloc(abm->num_thresholds,
433 				   sizeof(*abm->thresholds), GFP_KERNEL);
434 	if (!abm->thresholds)
435 		goto err_free_thresh_umap;
436 	for (i = 0; i < abm->num_bands * NFP_NET_MAX_RX_RINGS; i++)
437 		__nfp_abm_ctrl_set_q_lvl(abm, i, NFP_ABM_LVL_INFINITY);
438 
439 	/* We start in legacy mode, make sure advanced queuing is disabled */
440 	err = nfp_abm_ctrl_qm_disable(abm);
441 	if (err)
442 		goto err_free_thresh;
443 
444 	err = -ENOMEM;
445 	reprs = nfp_reprs_alloc(pf->max_data_vnics);
446 	if (!reprs)
447 		goto err_free_thresh;
448 	RCU_INIT_POINTER(app->reprs[NFP_REPR_TYPE_PHYS_PORT], reprs);
449 
450 	reprs = nfp_reprs_alloc(pf->max_data_vnics);
451 	if (!reprs)
452 		goto err_free_phys;
453 	RCU_INIT_POINTER(app->reprs[NFP_REPR_TYPE_PF], reprs);
454 
455 	return 0;
456 
457 err_free_phys:
458 	nfp_reprs_clean_and_free_by_type(app, NFP_REPR_TYPE_PHYS_PORT);
459 err_free_thresh:
460 	kvfree(abm->thresholds);
461 err_free_thresh_umap:
462 	bitmap_free(abm->threshold_undef);
463 err_free_abm:
464 	kfree(abm);
465 	app->priv = NULL;
466 	return err;
467 }
468 
469 static void nfp_abm_clean(struct nfp_app *app)
470 {
471 	struct nfp_abm *abm = app->priv;
472 
473 	nfp_abm_eswitch_clean_up(abm);
474 	nfp_reprs_clean_and_free_by_type(app, NFP_REPR_TYPE_PF);
475 	nfp_reprs_clean_and_free_by_type(app, NFP_REPR_TYPE_PHYS_PORT);
476 	bitmap_free(abm->threshold_undef);
477 	kvfree(abm->thresholds);
478 	kfree(abm);
479 	app->priv = NULL;
480 }
481 
482 const struct nfp_app_type app_abm = {
483 	.id		= NFP_APP_ACTIVE_BUFFER_MGMT_NIC,
484 	.name		= "abm",
485 
486 	.init		= nfp_abm_init,
487 	.clean		= nfp_abm_clean,
488 
489 	.vnic_alloc	= nfp_abm_vnic_alloc,
490 	.vnic_free	= nfp_abm_vnic_free,
491 
492 	.port_get_stats		= nfp_abm_port_get_stats,
493 	.port_get_stats_count	= nfp_abm_port_get_stats_count,
494 	.port_get_stats_strings	= nfp_abm_port_get_stats_strings,
495 
496 	.setup_tc	= nfp_abm_setup_tc,
497 
498 	.eswitch_mode_get	= nfp_abm_eswitch_mode_get,
499 	.eswitch_mode_set	= nfp_abm_eswitch_mode_set,
500 
501 	.repr_get	= nfp_abm_repr_get,
502 };
503