xref: /linux/drivers/net/ethernet/netronome/nfp/nfp_app.h (revision b7d3826c2ed6c3e626e7ae796c5df2c0d2551c6a)
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 #ifndef _NFP_APP_H
35 #define _NFP_APP_H 1
36 
37 #include <net/devlink.h>
38 
39 #include <trace/events/devlink.h>
40 
41 #include "nfp_net_repr.h"
42 
43 #define NFP_APP_CTRL_MTU_MAX	U32_MAX
44 
45 struct bpf_prog;
46 struct net_device;
47 struct netdev_bpf;
48 struct netlink_ext_ack;
49 struct pci_dev;
50 struct sk_buff;
51 struct sk_buff;
52 struct nfp_app;
53 struct nfp_cpp;
54 struct nfp_pf;
55 struct nfp_repr;
56 struct nfp_net;
57 
58 enum nfp_app_id {
59 	NFP_APP_CORE_NIC	= 0x1,
60 	NFP_APP_BPF_NIC		= 0x2,
61 	NFP_APP_FLOWER_NIC	= 0x3,
62 	NFP_APP_ACTIVE_BUFFER_MGMT_NIC = 0x4,
63 };
64 
65 extern const struct nfp_app_type app_nic;
66 extern const struct nfp_app_type app_bpf;
67 extern const struct nfp_app_type app_flower;
68 extern const struct nfp_app_type app_abm;
69 
70 /**
71  * struct nfp_app_type - application definition
72  * @id:		application ID
73  * @name:	application name
74  * @ctrl_cap_mask:  ctrl vNIC capability mask, allows disabling features like
75  *		    IRQMOD which are on by default but counter-productive for
76  *		    control messages which are often latency-sensitive
77  * @ctrl_has_meta:  control messages have prepend of type:5/port:CTRL
78  *
79  * Callbacks
80  * @init:	perform basic app checks and init
81  * @clean:	clean app state
82  * @extra_cap:	extra capabilities string
83  * @ndo_init:	vNIC and repr netdev .ndo_init
84  * @ndo_uninit:	vNIC and repr netdev .ndo_unint
85  * @vnic_alloc:	allocate vNICs (assign port types, etc.)
86  * @vnic_free:	free up app's vNIC state
87  * @vnic_init:	vNIC netdev was registered
88  * @vnic_clean:	vNIC netdev about to be unregistered
89  * @repr_init:	representor about to be registered
90  * @repr_preclean:	representor about to unregistered, executed before app
91  *			reference to the it is removed
92  * @repr_clean:	representor about to be unregistered
93  * @repr_open:	representor netdev open callback
94  * @repr_stop:	representor netdev stop callback
95  * @check_mtu:	MTU change request on a netdev (verify it is valid)
96  * @repr_change_mtu:	MTU change request on repr (make and verify change)
97  * @port_get_stats:		get extra ethtool statistics for a port
98  * @port_get_stats_count:	get count of extra statistics for a port
99  * @port_get_stats_strings:	get strings for extra statistics
100  * @start:	start application logic
101  * @stop:	stop application logic
102  * @ctrl_msg_rx:    control message handler
103  * @ctrl_msg_rx_raw:	handler for control messages from data queues
104  * @setup_tc:	setup TC ndo
105  * @bpf:	BPF ndo offload-related calls
106  * @xdp_offload:    offload an XDP program
107  * @eswitch_mode_get:    get SR-IOV eswitch mode
108  * @eswitch_mode_set:    set SR-IOV eswitch mode (under pf->lock)
109  * @sriov_enable: app-specific sriov initialisation
110  * @sriov_disable: app-specific sriov clean-up
111  * @repr_get:	get representor netdev
112  */
113 struct nfp_app_type {
114 	enum nfp_app_id id;
115 	const char *name;
116 
117 	u32 ctrl_cap_mask;
118 	bool ctrl_has_meta;
119 
120 	int (*init)(struct nfp_app *app);
121 	void (*clean)(struct nfp_app *app);
122 
123 	const char *(*extra_cap)(struct nfp_app *app, struct nfp_net *nn);
124 
125 	int (*ndo_init)(struct nfp_app *app, struct net_device *netdev);
126 	void (*ndo_uninit)(struct nfp_app *app, struct net_device *netdev);
127 
128 	int (*vnic_alloc)(struct nfp_app *app, struct nfp_net *nn,
129 			  unsigned int id);
130 	void (*vnic_free)(struct nfp_app *app, struct nfp_net *nn);
131 	int (*vnic_init)(struct nfp_app *app, struct nfp_net *nn);
132 	void (*vnic_clean)(struct nfp_app *app, struct nfp_net *nn);
133 
134 	int (*repr_init)(struct nfp_app *app, struct net_device *netdev);
135 	void (*repr_preclean)(struct nfp_app *app, struct net_device *netdev);
136 	void (*repr_clean)(struct nfp_app *app, struct net_device *netdev);
137 
138 	int (*repr_open)(struct nfp_app *app, struct nfp_repr *repr);
139 	int (*repr_stop)(struct nfp_app *app, struct nfp_repr *repr);
140 
141 	int (*check_mtu)(struct nfp_app *app, struct net_device *netdev,
142 			 int new_mtu);
143 	int (*repr_change_mtu)(struct nfp_app *app, struct net_device *netdev,
144 			       int new_mtu);
145 
146 	u64 *(*port_get_stats)(struct nfp_app *app,
147 			       struct nfp_port *port, u64 *data);
148 	int (*port_get_stats_count)(struct nfp_app *app, struct nfp_port *port);
149 	u8 *(*port_get_stats_strings)(struct nfp_app *app,
150 				      struct nfp_port *port, u8 *data);
151 
152 	int (*start)(struct nfp_app *app);
153 	void (*stop)(struct nfp_app *app);
154 
155 	void (*ctrl_msg_rx)(struct nfp_app *app, struct sk_buff *skb);
156 	void (*ctrl_msg_rx_raw)(struct nfp_app *app, const void *data,
157 				unsigned int len);
158 
159 	int (*setup_tc)(struct nfp_app *app, struct net_device *netdev,
160 			enum tc_setup_type type, void *type_data);
161 	int (*bpf)(struct nfp_app *app, struct nfp_net *nn,
162 		   struct netdev_bpf *xdp);
163 	int (*xdp_offload)(struct nfp_app *app, struct nfp_net *nn,
164 			   struct bpf_prog *prog,
165 			   struct netlink_ext_ack *extack);
166 
167 	int (*sriov_enable)(struct nfp_app *app, int num_vfs);
168 	void (*sriov_disable)(struct nfp_app *app);
169 
170 	enum devlink_eswitch_mode (*eswitch_mode_get)(struct nfp_app *app);
171 	int (*eswitch_mode_set)(struct nfp_app *app, u16 mode);
172 	struct net_device *(*repr_get)(struct nfp_app *app, u32 id);
173 };
174 
175 /**
176  * struct nfp_app - NFP application container
177  * @pdev:	backpointer to PCI device
178  * @pf:		backpointer to NFP PF structure
179  * @cpp:	pointer to the CPP handle
180  * @ctrl:	pointer to ctrl vNIC struct
181  * @reprs:	array of pointers to representors
182  * @type:	pointer to const application ops and info
183  * @ctrl_mtu:	MTU to set on the control vNIC (set in .init())
184  * @priv:	app-specific priv data
185  */
186 struct nfp_app {
187 	struct pci_dev *pdev;
188 	struct nfp_pf *pf;
189 	struct nfp_cpp *cpp;
190 
191 	struct nfp_net *ctrl;
192 	struct nfp_reprs __rcu *reprs[NFP_REPR_TYPE_MAX + 1];
193 
194 	const struct nfp_app_type *type;
195 	unsigned int ctrl_mtu;
196 	void *priv;
197 };
198 
199 bool __nfp_ctrl_tx(struct nfp_net *nn, struct sk_buff *skb);
200 bool nfp_ctrl_tx(struct nfp_net *nn, struct sk_buff *skb);
201 
202 static inline int nfp_app_init(struct nfp_app *app)
203 {
204 	if (!app->type->init)
205 		return 0;
206 	return app->type->init(app);
207 }
208 
209 static inline void nfp_app_clean(struct nfp_app *app)
210 {
211 	if (app->type->clean)
212 		app->type->clean(app);
213 }
214 
215 int nfp_app_ndo_init(struct net_device *netdev);
216 void nfp_app_ndo_uninit(struct net_device *netdev);
217 
218 static inline int nfp_app_vnic_alloc(struct nfp_app *app, struct nfp_net *nn,
219 				     unsigned int id)
220 {
221 	return app->type->vnic_alloc(app, nn, id);
222 }
223 
224 static inline void nfp_app_vnic_free(struct nfp_app *app, struct nfp_net *nn)
225 {
226 	if (app->type->vnic_free)
227 		app->type->vnic_free(app, nn);
228 }
229 
230 static inline int nfp_app_vnic_init(struct nfp_app *app, struct nfp_net *nn)
231 {
232 	if (!app->type->vnic_init)
233 		return 0;
234 	return app->type->vnic_init(app, nn);
235 }
236 
237 static inline void nfp_app_vnic_clean(struct nfp_app *app, struct nfp_net *nn)
238 {
239 	if (app->type->vnic_clean)
240 		app->type->vnic_clean(app, nn);
241 }
242 
243 static inline int nfp_app_repr_open(struct nfp_app *app, struct nfp_repr *repr)
244 {
245 	if (!app->type->repr_open)
246 		return -EINVAL;
247 	return app->type->repr_open(app, repr);
248 }
249 
250 static inline int nfp_app_repr_stop(struct nfp_app *app, struct nfp_repr *repr)
251 {
252 	if (!app->type->repr_stop)
253 		return -EINVAL;
254 	return app->type->repr_stop(app, repr);
255 }
256 
257 static inline int
258 nfp_app_repr_init(struct nfp_app *app, struct net_device *netdev)
259 {
260 	if (!app->type->repr_init)
261 		return 0;
262 	return app->type->repr_init(app, netdev);
263 }
264 
265 static inline void
266 nfp_app_repr_preclean(struct nfp_app *app, struct net_device *netdev)
267 {
268 	if (app->type->repr_preclean)
269 		app->type->repr_preclean(app, netdev);
270 }
271 
272 static inline void
273 nfp_app_repr_clean(struct nfp_app *app, struct net_device *netdev)
274 {
275 	if (app->type->repr_clean)
276 		app->type->repr_clean(app, netdev);
277 }
278 
279 static inline int
280 nfp_app_check_mtu(struct nfp_app *app, struct net_device *netdev, int new_mtu)
281 {
282 	if (!app || !app->type->check_mtu)
283 		return 0;
284 	return app->type->check_mtu(app, netdev, new_mtu);
285 }
286 
287 static inline int
288 nfp_app_repr_change_mtu(struct nfp_app *app, struct net_device *netdev,
289 			int new_mtu)
290 {
291 	if (!app || !app->type->repr_change_mtu)
292 		return 0;
293 	return app->type->repr_change_mtu(app, netdev, new_mtu);
294 }
295 
296 static inline int nfp_app_start(struct nfp_app *app, struct nfp_net *ctrl)
297 {
298 	app->ctrl = ctrl;
299 	if (!app->type->start)
300 		return 0;
301 	return app->type->start(app);
302 }
303 
304 static inline void nfp_app_stop(struct nfp_app *app)
305 {
306 	if (!app->type->stop)
307 		return;
308 	app->type->stop(app);
309 }
310 
311 static inline const char *nfp_app_name(struct nfp_app *app)
312 {
313 	if (!app)
314 		return "";
315 	return app->type->name;
316 }
317 
318 static inline bool nfp_app_needs_ctrl_vnic(struct nfp_app *app)
319 {
320 	return app && app->type->ctrl_msg_rx;
321 }
322 
323 static inline bool nfp_app_ctrl_has_meta(struct nfp_app *app)
324 {
325 	return app->type->ctrl_has_meta;
326 }
327 
328 static inline bool nfp_app_ctrl_uses_data_vnics(struct nfp_app *app)
329 {
330 	return app && app->type->ctrl_msg_rx_raw;
331 }
332 
333 static inline const char *nfp_app_extra_cap(struct nfp_app *app,
334 					    struct nfp_net *nn)
335 {
336 	if (!app || !app->type->extra_cap)
337 		return "";
338 	return app->type->extra_cap(app, nn);
339 }
340 
341 static inline bool nfp_app_has_tc(struct nfp_app *app)
342 {
343 	return app && app->type->setup_tc;
344 }
345 
346 static inline int nfp_app_setup_tc(struct nfp_app *app,
347 				   struct net_device *netdev,
348 				   enum tc_setup_type type, void *type_data)
349 {
350 	if (!app || !app->type->setup_tc)
351 		return -EOPNOTSUPP;
352 	return app->type->setup_tc(app, netdev, type, type_data);
353 }
354 
355 static inline int nfp_app_bpf(struct nfp_app *app, struct nfp_net *nn,
356 			      struct netdev_bpf *bpf)
357 {
358 	if (!app || !app->type->bpf)
359 		return -EINVAL;
360 	return app->type->bpf(app, nn, bpf);
361 }
362 
363 static inline int nfp_app_xdp_offload(struct nfp_app *app, struct nfp_net *nn,
364 				      struct bpf_prog *prog,
365 				      struct netlink_ext_ack *extack)
366 {
367 	if (!app || !app->type->xdp_offload)
368 		return -EOPNOTSUPP;
369 	return app->type->xdp_offload(app, nn, prog, extack);
370 }
371 
372 static inline bool __nfp_app_ctrl_tx(struct nfp_app *app, struct sk_buff *skb)
373 {
374 	trace_devlink_hwmsg(priv_to_devlink(app->pf), false, 0,
375 			    skb->data, skb->len);
376 
377 	return __nfp_ctrl_tx(app->ctrl, skb);
378 }
379 
380 static inline bool nfp_app_ctrl_tx(struct nfp_app *app, struct sk_buff *skb)
381 {
382 	trace_devlink_hwmsg(priv_to_devlink(app->pf), false, 0,
383 			    skb->data, skb->len);
384 
385 	return nfp_ctrl_tx(app->ctrl, skb);
386 }
387 
388 static inline void nfp_app_ctrl_rx(struct nfp_app *app, struct sk_buff *skb)
389 {
390 	trace_devlink_hwmsg(priv_to_devlink(app->pf), true, 0,
391 			    skb->data, skb->len);
392 
393 	app->type->ctrl_msg_rx(app, skb);
394 }
395 
396 static inline void
397 nfp_app_ctrl_rx_raw(struct nfp_app *app, const void *data, unsigned int len)
398 {
399 	if (!app || !app->type->ctrl_msg_rx_raw)
400 		return;
401 
402 	trace_devlink_hwmsg(priv_to_devlink(app->pf), true, 0, data, len);
403 	app->type->ctrl_msg_rx_raw(app, data, len);
404 }
405 
406 static inline int nfp_app_eswitch_mode_get(struct nfp_app *app, u16 *mode)
407 {
408 	if (!app->type->eswitch_mode_get)
409 		return -EOPNOTSUPP;
410 
411 	*mode = app->type->eswitch_mode_get(app);
412 
413 	return 0;
414 }
415 
416 static inline int nfp_app_eswitch_mode_set(struct nfp_app *app, u16 mode)
417 {
418 	if (!app->type->eswitch_mode_set)
419 		return -EOPNOTSUPP;
420 	return app->type->eswitch_mode_set(app, mode);
421 }
422 
423 static inline int nfp_app_sriov_enable(struct nfp_app *app, int num_vfs)
424 {
425 	if (!app || !app->type->sriov_enable)
426 		return -EOPNOTSUPP;
427 	return app->type->sriov_enable(app, num_vfs);
428 }
429 
430 static inline void nfp_app_sriov_disable(struct nfp_app *app)
431 {
432 	if (app && app->type->sriov_disable)
433 		app->type->sriov_disable(app);
434 }
435 
436 static inline struct net_device *nfp_app_repr_get(struct nfp_app *app, u32 id)
437 {
438 	if (unlikely(!app || !app->type->repr_get))
439 		return NULL;
440 
441 	return app->type->repr_get(app, id);
442 }
443 
444 struct nfp_app *nfp_app_from_netdev(struct net_device *netdev);
445 
446 u64 *nfp_app_port_get_stats(struct nfp_port *port, u64 *data);
447 int nfp_app_port_get_stats_count(struct nfp_port *port);
448 u8 *nfp_app_port_get_stats_strings(struct nfp_port *port, u8 *data);
449 
450 struct nfp_reprs *
451 nfp_reprs_get_locked(struct nfp_app *app, enum nfp_repr_type type);
452 struct nfp_reprs *
453 nfp_app_reprs_set(struct nfp_app *app, enum nfp_repr_type type,
454 		  struct nfp_reprs *reprs);
455 
456 const char *nfp_app_mip_name(struct nfp_app *app);
457 struct sk_buff *
458 nfp_app_ctrl_msg_alloc(struct nfp_app *app, unsigned int size, gfp_t priority);
459 
460 struct nfp_app *nfp_app_alloc(struct nfp_pf *pf, enum nfp_app_id id);
461 void nfp_app_free(struct nfp_app *app);
462 
463 /* Callbacks shared between apps */
464 
465 int nfp_app_nic_vnic_alloc(struct nfp_app *app, struct nfp_net *nn,
466 			   unsigned int id);
467 int nfp_app_nic_vnic_init_phy_port(struct nfp_pf *pf, struct nfp_app *app,
468 				   struct nfp_net *nn, unsigned int id);
469 
470 #endif
471