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