xref: /openbmc/linux/drivers/net/ethernet/netronome/nfp/flower/main.c (revision 9dae47aba0a055f761176d9297371d5bb24289ec)
1 /*
2  * Copyright (C) 2017 Netronome Systems, Inc.
3  *
4  * This software is dual licensed under the GNU General License Version 2,
5  * June 1991 as shown in the file COPYING in the top-level directory of this
6  * source tree or the BSD 2-Clause License provided below.  You have the
7  * option to license this software under the complete terms of either license.
8  *
9  * The BSD 2-Clause License:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      1. Redistributions of source code must retain the above
16  *         copyright notice, this list of conditions and the following
17  *         disclaimer.
18  *
19  *      2. Redistributions in binary form must reproduce the above
20  *         copyright notice, this list of conditions and the following
21  *         disclaimer in the documentation and/or other materials
22  *         provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 #include <linux/etherdevice.h>
35 #include <linux/lockdep.h>
36 #include <linux/pci.h>
37 #include <linux/skbuff.h>
38 #include <linux/vmalloc.h>
39 #include <net/devlink.h>
40 #include <net/dst_metadata.h>
41 
42 #include "main.h"
43 #include "../nfpcore/nfp_cpp.h"
44 #include "../nfpcore/nfp_nffw.h"
45 #include "../nfpcore/nfp_nsp.h"
46 #include "../nfp_app.h"
47 #include "../nfp_main.h"
48 #include "../nfp_net.h"
49 #include "../nfp_net_repr.h"
50 #include "../nfp_port.h"
51 #include "./cmsg.h"
52 
53 #define NFP_FLOWER_ALLOWED_VER 0x0001000000010000UL
54 
55 static const char *nfp_flower_extra_cap(struct nfp_app *app, struct nfp_net *nn)
56 {
57 	return "FLOWER";
58 }
59 
60 static enum devlink_eswitch_mode eswitch_mode_get(struct nfp_app *app)
61 {
62 	return DEVLINK_ESWITCH_MODE_SWITCHDEV;
63 }
64 
65 static enum nfp_repr_type
66 nfp_flower_repr_get_type_and_port(struct nfp_app *app, u32 port_id, u8 *port)
67 {
68 	switch (FIELD_GET(NFP_FLOWER_CMSG_PORT_TYPE, port_id)) {
69 	case NFP_FLOWER_CMSG_PORT_TYPE_PHYS_PORT:
70 		*port = FIELD_GET(NFP_FLOWER_CMSG_PORT_PHYS_PORT_NUM,
71 				  port_id);
72 		return NFP_REPR_TYPE_PHYS_PORT;
73 
74 	case NFP_FLOWER_CMSG_PORT_TYPE_PCIE_PORT:
75 		*port = FIELD_GET(NFP_FLOWER_CMSG_PORT_VNIC, port_id);
76 		if (FIELD_GET(NFP_FLOWER_CMSG_PORT_VNIC_TYPE, port_id) ==
77 		    NFP_FLOWER_CMSG_PORT_VNIC_TYPE_PF)
78 			return NFP_REPR_TYPE_PF;
79 		else
80 			return NFP_REPR_TYPE_VF;
81 	}
82 
83 	return NFP_FLOWER_CMSG_PORT_TYPE_UNSPEC;
84 }
85 
86 static struct net_device *
87 nfp_flower_repr_get(struct nfp_app *app, u32 port_id)
88 {
89 	enum nfp_repr_type repr_type;
90 	struct nfp_reprs *reprs;
91 	u8 port = 0;
92 
93 	repr_type = nfp_flower_repr_get_type_and_port(app, port_id, &port);
94 
95 	reprs = rcu_dereference(app->reprs[repr_type]);
96 	if (!reprs)
97 		return NULL;
98 
99 	if (port >= reprs->num_reprs)
100 		return NULL;
101 
102 	return reprs->reprs[port];
103 }
104 
105 static int
106 nfp_flower_reprs_reify(struct nfp_app *app, enum nfp_repr_type type,
107 		       bool exists)
108 {
109 	struct nfp_reprs *reprs;
110 	int i, err, count = 0;
111 
112 	reprs = rcu_dereference_protected(app->reprs[type],
113 					  lockdep_is_held(&app->pf->lock));
114 	if (!reprs)
115 		return 0;
116 
117 	for (i = 0; i < reprs->num_reprs; i++)
118 		if (reprs->reprs[i]) {
119 			struct nfp_repr *repr = netdev_priv(reprs->reprs[i]);
120 
121 			err = nfp_flower_cmsg_portreify(repr, exists);
122 			if (err)
123 				return err;
124 			count++;
125 		}
126 
127 	return count;
128 }
129 
130 static int
131 nfp_flower_wait_repr_reify(struct nfp_app *app, atomic_t *replies, int tot_repl)
132 {
133 	struct nfp_flower_priv *priv = app->priv;
134 	int err;
135 
136 	if (!tot_repl)
137 		return 0;
138 
139 	lockdep_assert_held(&app->pf->lock);
140 	err = wait_event_interruptible_timeout(priv->reify_wait_queue,
141 					       atomic_read(replies) >= tot_repl,
142 					       msecs_to_jiffies(10));
143 	if (err <= 0) {
144 		nfp_warn(app->cpp, "Not all reprs responded to reify\n");
145 		return -EIO;
146 	}
147 
148 	return 0;
149 }
150 
151 static int
152 nfp_flower_repr_netdev_open(struct nfp_app *app, struct nfp_repr *repr)
153 {
154 	int err;
155 
156 	err = nfp_flower_cmsg_portmod(repr, true);
157 	if (err)
158 		return err;
159 
160 	netif_tx_wake_all_queues(repr->netdev);
161 
162 	return 0;
163 }
164 
165 static int
166 nfp_flower_repr_netdev_stop(struct nfp_app *app, struct nfp_repr *repr)
167 {
168 	netif_tx_disable(repr->netdev);
169 
170 	return nfp_flower_cmsg_portmod(repr, false);
171 }
172 
173 static int
174 nfp_flower_repr_netdev_init(struct nfp_app *app, struct net_device *netdev)
175 {
176 	return tc_setup_cb_egdev_register(netdev,
177 					  nfp_flower_setup_tc_egress_cb,
178 					  netdev_priv(netdev));
179 }
180 
181 static void
182 nfp_flower_repr_netdev_clean(struct nfp_app *app, struct net_device *netdev)
183 {
184 	tc_setup_cb_egdev_unregister(netdev, nfp_flower_setup_tc_egress_cb,
185 				     netdev_priv(netdev));
186 }
187 
188 static void
189 nfp_flower_repr_netdev_preclean(struct nfp_app *app, struct net_device *netdev)
190 {
191 	struct nfp_repr *repr = netdev_priv(netdev);
192 	struct nfp_flower_priv *priv = app->priv;
193 	atomic_t *replies = &priv->reify_replies;
194 	int err;
195 
196 	atomic_set(replies, 0);
197 	err = nfp_flower_cmsg_portreify(repr, false);
198 	if (err) {
199 		nfp_warn(app->cpp, "Failed to notify firmware about repr destruction\n");
200 		return;
201 	}
202 
203 	nfp_flower_wait_repr_reify(app, replies, 1);
204 }
205 
206 static void nfp_flower_sriov_disable(struct nfp_app *app)
207 {
208 	struct nfp_flower_priv *priv = app->priv;
209 
210 	if (!priv->nn)
211 		return;
212 
213 	nfp_reprs_clean_and_free_by_type(app, NFP_REPR_TYPE_VF);
214 }
215 
216 static int
217 nfp_flower_spawn_vnic_reprs(struct nfp_app *app,
218 			    enum nfp_flower_cmsg_port_vnic_type vnic_type,
219 			    enum nfp_repr_type repr_type, unsigned int cnt)
220 {
221 	u8 nfp_pcie = nfp_cppcore_pcie_unit(app->pf->cpp);
222 	struct nfp_flower_priv *priv = app->priv;
223 	atomic_t *replies = &priv->reify_replies;
224 	enum nfp_port_type port_type;
225 	struct nfp_reprs *reprs;
226 	int i, err, reify_cnt;
227 	const u8 queue = 0;
228 
229 	port_type = repr_type == NFP_REPR_TYPE_PF ? NFP_PORT_PF_PORT :
230 						    NFP_PORT_VF_PORT;
231 
232 	reprs = nfp_reprs_alloc(cnt);
233 	if (!reprs)
234 		return -ENOMEM;
235 
236 	for (i = 0; i < cnt; i++) {
237 		struct nfp_port *port;
238 		u32 port_id;
239 
240 		reprs->reprs[i] = nfp_repr_alloc(app);
241 		if (!reprs->reprs[i]) {
242 			err = -ENOMEM;
243 			goto err_reprs_clean;
244 		}
245 
246 		/* For now we only support 1 PF */
247 		WARN_ON(repr_type == NFP_REPR_TYPE_PF && i);
248 
249 		port = nfp_port_alloc(app, port_type, reprs->reprs[i]);
250 		if (repr_type == NFP_REPR_TYPE_PF) {
251 			port->pf_id = i;
252 			port->vnic = priv->nn->dp.ctrl_bar;
253 		} else {
254 			port->pf_id = 0;
255 			port->vf_id = i;
256 			port->vnic =
257 				app->pf->vf_cfg_mem + i * NFP_NET_CFG_BAR_SZ;
258 		}
259 
260 		eth_hw_addr_random(reprs->reprs[i]);
261 
262 		port_id = nfp_flower_cmsg_pcie_port(nfp_pcie, vnic_type,
263 						    i, queue);
264 		err = nfp_repr_init(app, reprs->reprs[i],
265 				    port_id, port, priv->nn->dp.netdev);
266 		if (err) {
267 			nfp_port_free(port);
268 			goto err_reprs_clean;
269 		}
270 
271 		nfp_info(app->cpp, "%s%d Representor(%s) created\n",
272 			 repr_type == NFP_REPR_TYPE_PF ? "PF" : "VF", i,
273 			 reprs->reprs[i]->name);
274 	}
275 
276 	nfp_app_reprs_set(app, repr_type, reprs);
277 
278 	atomic_set(replies, 0);
279 	reify_cnt = nfp_flower_reprs_reify(app, repr_type, true);
280 	if (reify_cnt < 0) {
281 		err = reify_cnt;
282 		nfp_warn(app->cpp, "Failed to notify firmware about repr creation\n");
283 		goto err_reprs_remove;
284 	}
285 
286 	err = nfp_flower_wait_repr_reify(app, replies, reify_cnt);
287 	if (err)
288 		goto err_reprs_remove;
289 
290 	return 0;
291 err_reprs_remove:
292 	reprs = nfp_app_reprs_set(app, repr_type, NULL);
293 err_reprs_clean:
294 	nfp_reprs_clean_and_free(reprs);
295 	return err;
296 }
297 
298 static int nfp_flower_sriov_enable(struct nfp_app *app, int num_vfs)
299 {
300 	struct nfp_flower_priv *priv = app->priv;
301 
302 	if (!priv->nn)
303 		return 0;
304 
305 	return nfp_flower_spawn_vnic_reprs(app,
306 					   NFP_FLOWER_CMSG_PORT_VNIC_TYPE_VF,
307 					   NFP_REPR_TYPE_VF, num_vfs);
308 }
309 
310 static int
311 nfp_flower_spawn_phy_reprs(struct nfp_app *app, struct nfp_flower_priv *priv)
312 {
313 	struct nfp_eth_table *eth_tbl = app->pf->eth_tbl;
314 	atomic_t *replies = &priv->reify_replies;
315 	struct sk_buff *ctrl_skb;
316 	struct nfp_reprs *reprs;
317 	int err, reify_cnt;
318 	unsigned int i;
319 
320 	ctrl_skb = nfp_flower_cmsg_mac_repr_start(app, eth_tbl->count);
321 	if (!ctrl_skb)
322 		return -ENOMEM;
323 
324 	reprs = nfp_reprs_alloc(eth_tbl->max_index + 1);
325 	if (!reprs) {
326 		err = -ENOMEM;
327 		goto err_free_ctrl_skb;
328 	}
329 
330 	for (i = 0; i < eth_tbl->count; i++) {
331 		unsigned int phys_port = eth_tbl->ports[i].index;
332 		struct nfp_port *port;
333 		u32 cmsg_port_id;
334 
335 		reprs->reprs[phys_port] = nfp_repr_alloc(app);
336 		if (!reprs->reprs[phys_port]) {
337 			err = -ENOMEM;
338 			goto err_reprs_clean;
339 		}
340 
341 		port = nfp_port_alloc(app, NFP_PORT_PHYS_PORT,
342 				      reprs->reprs[phys_port]);
343 		if (IS_ERR(port)) {
344 			err = PTR_ERR(port);
345 			goto err_reprs_clean;
346 		}
347 		err = nfp_port_init_phy_port(app->pf, app, port, i);
348 		if (err) {
349 			nfp_port_free(port);
350 			goto err_reprs_clean;
351 		}
352 
353 		SET_NETDEV_DEV(reprs->reprs[phys_port], &priv->nn->pdev->dev);
354 		nfp_net_get_mac_addr(app->pf, port);
355 
356 		cmsg_port_id = nfp_flower_cmsg_phys_port(phys_port);
357 		err = nfp_repr_init(app, reprs->reprs[phys_port],
358 				    cmsg_port_id, port, priv->nn->dp.netdev);
359 		if (err) {
360 			nfp_port_free(port);
361 			goto err_reprs_clean;
362 		}
363 
364 		nfp_flower_cmsg_mac_repr_add(ctrl_skb, i,
365 					     eth_tbl->ports[i].nbi,
366 					     eth_tbl->ports[i].base,
367 					     phys_port);
368 
369 		nfp_info(app->cpp, "Phys Port %d Representor(%s) created\n",
370 			 phys_port, reprs->reprs[phys_port]->name);
371 	}
372 
373 	nfp_app_reprs_set(app, NFP_REPR_TYPE_PHYS_PORT, reprs);
374 
375 	/* The REIFY/MAC_REPR control messages should be sent after the MAC
376 	 * representors are registered using nfp_app_reprs_set().  This is
377 	 * because the firmware may respond with control messages for the
378 	 * MAC representors, f.e. to provide the driver with information
379 	 * about their state, and without registration the driver will drop
380 	 * any such messages.
381 	 */
382 	atomic_set(replies, 0);
383 	reify_cnt = nfp_flower_reprs_reify(app, NFP_REPR_TYPE_PHYS_PORT, true);
384 	if (reify_cnt < 0) {
385 		err = reify_cnt;
386 		nfp_warn(app->cpp, "Failed to notify firmware about repr creation\n");
387 		goto err_reprs_remove;
388 	}
389 
390 	err = nfp_flower_wait_repr_reify(app, replies, reify_cnt);
391 	if (err)
392 		goto err_reprs_remove;
393 
394 	nfp_ctrl_tx(app->ctrl, ctrl_skb);
395 
396 	return 0;
397 err_reprs_remove:
398 	reprs = nfp_app_reprs_set(app, NFP_REPR_TYPE_PHYS_PORT, NULL);
399 err_reprs_clean:
400 	nfp_reprs_clean_and_free(reprs);
401 err_free_ctrl_skb:
402 	kfree_skb(ctrl_skb);
403 	return err;
404 }
405 
406 static int nfp_flower_vnic_alloc(struct nfp_app *app, struct nfp_net *nn,
407 				 unsigned int id)
408 {
409 	if (id > 0) {
410 		nfp_warn(app->cpp, "FlowerNIC doesn't support more than one data vNIC\n");
411 		goto err_invalid_port;
412 	}
413 
414 	eth_hw_addr_random(nn->dp.netdev);
415 	netif_keep_dst(nn->dp.netdev);
416 
417 	return 0;
418 
419 err_invalid_port:
420 	nn->port = nfp_port_alloc(app, NFP_PORT_INVALID, nn->dp.netdev);
421 	return PTR_ERR_OR_ZERO(nn->port);
422 }
423 
424 static void nfp_flower_vnic_clean(struct nfp_app *app, struct nfp_net *nn)
425 {
426 	struct nfp_flower_priv *priv = app->priv;
427 
428 	if (app->pf->num_vfs)
429 		nfp_reprs_clean_and_free_by_type(app, NFP_REPR_TYPE_VF);
430 	nfp_reprs_clean_and_free_by_type(app, NFP_REPR_TYPE_PF);
431 	nfp_reprs_clean_and_free_by_type(app, NFP_REPR_TYPE_PHYS_PORT);
432 
433 	priv->nn = NULL;
434 }
435 
436 static int nfp_flower_vnic_init(struct nfp_app *app, struct nfp_net *nn)
437 {
438 	struct nfp_flower_priv *priv = app->priv;
439 	int err;
440 
441 	priv->nn = nn;
442 
443 	err = nfp_flower_spawn_phy_reprs(app, app->priv);
444 	if (err)
445 		goto err_clear_nn;
446 
447 	err = nfp_flower_spawn_vnic_reprs(app,
448 					  NFP_FLOWER_CMSG_PORT_VNIC_TYPE_PF,
449 					  NFP_REPR_TYPE_PF, 1);
450 	if (err)
451 		goto err_destroy_reprs_phy;
452 
453 	if (app->pf->num_vfs) {
454 		err = nfp_flower_spawn_vnic_reprs(app,
455 						  NFP_FLOWER_CMSG_PORT_VNIC_TYPE_VF,
456 						  NFP_REPR_TYPE_VF,
457 						  app->pf->num_vfs);
458 		if (err)
459 			goto err_destroy_reprs_pf;
460 	}
461 
462 	return 0;
463 
464 err_destroy_reprs_pf:
465 	nfp_reprs_clean_and_free_by_type(app, NFP_REPR_TYPE_PF);
466 err_destroy_reprs_phy:
467 	nfp_reprs_clean_and_free_by_type(app, NFP_REPR_TYPE_PHYS_PORT);
468 err_clear_nn:
469 	priv->nn = NULL;
470 	return err;
471 }
472 
473 static int nfp_flower_init(struct nfp_app *app)
474 {
475 	const struct nfp_pf *pf = app->pf;
476 	struct nfp_flower_priv *app_priv;
477 	u64 version, features;
478 	int err;
479 
480 	if (!pf->eth_tbl) {
481 		nfp_warn(app->cpp, "FlowerNIC requires eth table\n");
482 		return -EINVAL;
483 	}
484 
485 	if (!pf->mac_stats_bar) {
486 		nfp_warn(app->cpp, "FlowerNIC requires mac_stats BAR\n");
487 		return -EINVAL;
488 	}
489 
490 	if (!pf->vf_cfg_bar) {
491 		nfp_warn(app->cpp, "FlowerNIC requires vf_cfg BAR\n");
492 		return -EINVAL;
493 	}
494 
495 	version = nfp_rtsym_read_le(app->pf->rtbl, "hw_flower_version", &err);
496 	if (err) {
497 		nfp_warn(app->cpp, "FlowerNIC requires hw_flower_version memory symbol\n");
498 		return err;
499 	}
500 
501 	/* We need to ensure hardware has enough flower capabilities. */
502 	if (version != NFP_FLOWER_ALLOWED_VER) {
503 		nfp_warn(app->cpp, "FlowerNIC: unsupported firmware version\n");
504 		return -EINVAL;
505 	}
506 
507 	app_priv = vzalloc(sizeof(struct nfp_flower_priv));
508 	if (!app_priv)
509 		return -ENOMEM;
510 
511 	app->priv = app_priv;
512 	app_priv->app = app;
513 	skb_queue_head_init(&app_priv->cmsg_skbs);
514 	INIT_WORK(&app_priv->cmsg_work, nfp_flower_cmsg_process_rx);
515 	init_waitqueue_head(&app_priv->reify_wait_queue);
516 
517 	err = nfp_flower_metadata_init(app);
518 	if (err)
519 		goto err_free_app_priv;
520 
521 	/* Extract the extra features supported by the firmware. */
522 	features = nfp_rtsym_read_le(app->pf->rtbl,
523 				     "_abi_flower_extra_features", &err);
524 	if (err)
525 		app_priv->flower_ext_feats = 0;
526 	else
527 		app_priv->flower_ext_feats = features;
528 
529 	return 0;
530 
531 err_free_app_priv:
532 	vfree(app->priv);
533 	return err;
534 }
535 
536 static void nfp_flower_clean(struct nfp_app *app)
537 {
538 	struct nfp_flower_priv *app_priv = app->priv;
539 
540 	skb_queue_purge(&app_priv->cmsg_skbs);
541 	flush_work(&app_priv->cmsg_work);
542 
543 	nfp_flower_metadata_cleanup(app);
544 	vfree(app->priv);
545 	app->priv = NULL;
546 }
547 
548 static int nfp_flower_start(struct nfp_app *app)
549 {
550 	return nfp_tunnel_config_start(app);
551 }
552 
553 static void nfp_flower_stop(struct nfp_app *app)
554 {
555 	nfp_tunnel_config_stop(app);
556 }
557 
558 const struct nfp_app_type app_flower = {
559 	.id		= NFP_APP_FLOWER_NIC,
560 	.name		= "flower",
561 	.ctrl_has_meta	= true,
562 
563 	.extra_cap	= nfp_flower_extra_cap,
564 
565 	.init		= nfp_flower_init,
566 	.clean		= nfp_flower_clean,
567 
568 	.vnic_alloc	= nfp_flower_vnic_alloc,
569 	.vnic_init	= nfp_flower_vnic_init,
570 	.vnic_clean	= nfp_flower_vnic_clean,
571 
572 	.repr_init	= nfp_flower_repr_netdev_init,
573 	.repr_preclean	= nfp_flower_repr_netdev_preclean,
574 	.repr_clean	= nfp_flower_repr_netdev_clean,
575 
576 	.repr_open	= nfp_flower_repr_netdev_open,
577 	.repr_stop	= nfp_flower_repr_netdev_stop,
578 
579 	.start		= nfp_flower_start,
580 	.stop		= nfp_flower_stop,
581 
582 	.ctrl_msg_rx	= nfp_flower_cmsg_rx,
583 
584 	.sriov_enable	= nfp_flower_sriov_enable,
585 	.sriov_disable	= nfp_flower_sriov_disable,
586 
587 	.eswitch_mode_get  = eswitch_mode_get,
588 	.repr_get	= nfp_flower_repr_get,
589 
590 	.setup_tc	= nfp_flower_setup_tc,
591 };
592