xref: /linux/drivers/net/ethernet/netronome/nfp/nfp_app.h (revision c4c8f39a57bf5057fc51a848d42b7e348ecfa31d)
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  * @vnic_alloc:	allocate vNICs (assign port types, etc.)
82  * @vnic_free:	free up app's vNIC state
83  * @vnic_init:	vNIC netdev was registered
84  * @vnic_clean:	vNIC netdev about to be unregistered
85  * @repr_init:	representor about to be registered
86  * @repr_preclean:	representor about to unregistered, executed before app
87  *			reference to the it is removed
88  * @repr_clean:	representor about to be unregistered
89  * @repr_open:	representor netdev open callback
90  * @repr_stop:	representor netdev stop callback
91  * @check_mtu:	MTU change request on a netdev (verify it is valid)
92  * @repr_change_mtu:	MTU change request on repr (make and verify change)
93  * @start:	start application logic
94  * @stop:	stop application logic
95  * @ctrl_msg_rx:    control message handler
96  * @setup_tc:	setup TC ndo
97  * @bpf:	BPF ndo offload-related calls
98  * @xdp_offload:    offload an XDP program
99  * @eswitch_mode_get:    get SR-IOV eswitch mode
100  * @sriov_enable: app-specific sriov initialisation
101  * @sriov_disable: app-specific sriov clean-up
102  * @repr_get:	get representor netdev
103  */
104 struct nfp_app_type {
105 	enum nfp_app_id id;
106 	const char *name;
107 
108 	u32 ctrl_cap_mask;
109 	bool ctrl_has_meta;
110 
111 	int (*init)(struct nfp_app *app);
112 	void (*clean)(struct nfp_app *app);
113 
114 	const char *(*extra_cap)(struct nfp_app *app, struct nfp_net *nn);
115 
116 	int (*vnic_alloc)(struct nfp_app *app, struct nfp_net *nn,
117 			  unsigned int id);
118 	void (*vnic_free)(struct nfp_app *app, struct nfp_net *nn);
119 	int (*vnic_init)(struct nfp_app *app, struct nfp_net *nn);
120 	void (*vnic_clean)(struct nfp_app *app, struct nfp_net *nn);
121 
122 	int (*repr_init)(struct nfp_app *app, struct net_device *netdev);
123 	void (*repr_preclean)(struct nfp_app *app, struct net_device *netdev);
124 	void (*repr_clean)(struct nfp_app *app, struct net_device *netdev);
125 
126 	int (*repr_open)(struct nfp_app *app, struct nfp_repr *repr);
127 	int (*repr_stop)(struct nfp_app *app, struct nfp_repr *repr);
128 
129 	int (*check_mtu)(struct nfp_app *app, struct net_device *netdev,
130 			 int new_mtu);
131 	int (*repr_change_mtu)(struct nfp_app *app, struct net_device *netdev,
132 			       int new_mtu);
133 
134 	int (*start)(struct nfp_app *app);
135 	void (*stop)(struct nfp_app *app);
136 
137 	void (*ctrl_msg_rx)(struct nfp_app *app, struct sk_buff *skb);
138 
139 	int (*setup_tc)(struct nfp_app *app, struct net_device *netdev,
140 			enum tc_setup_type type, void *type_data);
141 	int (*bpf)(struct nfp_app *app, struct nfp_net *nn,
142 		   struct netdev_bpf *xdp);
143 	int (*xdp_offload)(struct nfp_app *app, struct nfp_net *nn,
144 			   struct bpf_prog *prog,
145 			   struct netlink_ext_ack *extack);
146 
147 	int (*sriov_enable)(struct nfp_app *app, int num_vfs);
148 	void (*sriov_disable)(struct nfp_app *app);
149 
150 	enum devlink_eswitch_mode (*eswitch_mode_get)(struct nfp_app *app);
151 	struct net_device *(*repr_get)(struct nfp_app *app, u32 id);
152 };
153 
154 /**
155  * struct nfp_app - NFP application container
156  * @pdev:	backpointer to PCI device
157  * @pf:		backpointer to NFP PF structure
158  * @cpp:	pointer to the CPP handle
159  * @ctrl:	pointer to ctrl vNIC struct
160  * @reprs:	array of pointers to representors
161  * @type:	pointer to const application ops and info
162  * @priv:	app-specific priv data
163  */
164 struct nfp_app {
165 	struct pci_dev *pdev;
166 	struct nfp_pf *pf;
167 	struct nfp_cpp *cpp;
168 
169 	struct nfp_net *ctrl;
170 	struct nfp_reprs __rcu *reprs[NFP_REPR_TYPE_MAX + 1];
171 
172 	const struct nfp_app_type *type;
173 	void *priv;
174 };
175 
176 bool __nfp_ctrl_tx(struct nfp_net *nn, struct sk_buff *skb);
177 bool nfp_ctrl_tx(struct nfp_net *nn, struct sk_buff *skb);
178 
179 static inline int nfp_app_init(struct nfp_app *app)
180 {
181 	if (!app->type->init)
182 		return 0;
183 	return app->type->init(app);
184 }
185 
186 static inline void nfp_app_clean(struct nfp_app *app)
187 {
188 	if (app->type->clean)
189 		app->type->clean(app);
190 }
191 
192 static inline int nfp_app_vnic_alloc(struct nfp_app *app, struct nfp_net *nn,
193 				     unsigned int id)
194 {
195 	return app->type->vnic_alloc(app, nn, id);
196 }
197 
198 static inline void nfp_app_vnic_free(struct nfp_app *app, struct nfp_net *nn)
199 {
200 	if (app->type->vnic_free)
201 		app->type->vnic_free(app, nn);
202 }
203 
204 static inline int nfp_app_vnic_init(struct nfp_app *app, struct nfp_net *nn)
205 {
206 	if (!app->type->vnic_init)
207 		return 0;
208 	return app->type->vnic_init(app, nn);
209 }
210 
211 static inline void nfp_app_vnic_clean(struct nfp_app *app, struct nfp_net *nn)
212 {
213 	if (app->type->vnic_clean)
214 		app->type->vnic_clean(app, nn);
215 }
216 
217 static inline int nfp_app_repr_open(struct nfp_app *app, struct nfp_repr *repr)
218 {
219 	if (!app->type->repr_open)
220 		return -EINVAL;
221 	return app->type->repr_open(app, repr);
222 }
223 
224 static inline int nfp_app_repr_stop(struct nfp_app *app, struct nfp_repr *repr)
225 {
226 	if (!app->type->repr_stop)
227 		return -EINVAL;
228 	return app->type->repr_stop(app, repr);
229 }
230 
231 static inline int
232 nfp_app_repr_init(struct nfp_app *app, struct net_device *netdev)
233 {
234 	if (!app->type->repr_init)
235 		return 0;
236 	return app->type->repr_init(app, netdev);
237 }
238 
239 static inline void
240 nfp_app_repr_preclean(struct nfp_app *app, struct net_device *netdev)
241 {
242 	if (app->type->repr_preclean)
243 		app->type->repr_preclean(app, netdev);
244 }
245 
246 static inline void
247 nfp_app_repr_clean(struct nfp_app *app, struct net_device *netdev)
248 {
249 	if (app->type->repr_clean)
250 		app->type->repr_clean(app, netdev);
251 }
252 
253 static inline int
254 nfp_app_check_mtu(struct nfp_app *app, struct net_device *netdev, int new_mtu)
255 {
256 	if (!app || !app->type->check_mtu)
257 		return 0;
258 	return app->type->check_mtu(app, netdev, new_mtu);
259 }
260 
261 static inline int
262 nfp_app_repr_change_mtu(struct nfp_app *app, struct net_device *netdev,
263 			int new_mtu)
264 {
265 	if (!app || !app->type->repr_change_mtu)
266 		return 0;
267 	return app->type->repr_change_mtu(app, netdev, new_mtu);
268 }
269 
270 static inline int nfp_app_start(struct nfp_app *app, struct nfp_net *ctrl)
271 {
272 	app->ctrl = ctrl;
273 	if (!app->type->start)
274 		return 0;
275 	return app->type->start(app);
276 }
277 
278 static inline void nfp_app_stop(struct nfp_app *app)
279 {
280 	if (!app->type->stop)
281 		return;
282 	app->type->stop(app);
283 }
284 
285 static inline const char *nfp_app_name(struct nfp_app *app)
286 {
287 	if (!app)
288 		return "";
289 	return app->type->name;
290 }
291 
292 static inline bool nfp_app_needs_ctrl_vnic(struct nfp_app *app)
293 {
294 	return app && app->type->ctrl_msg_rx;
295 }
296 
297 static inline bool nfp_app_ctrl_has_meta(struct nfp_app *app)
298 {
299 	return app->type->ctrl_has_meta;
300 }
301 
302 static inline const char *nfp_app_extra_cap(struct nfp_app *app,
303 					    struct nfp_net *nn)
304 {
305 	if (!app || !app->type->extra_cap)
306 		return "";
307 	return app->type->extra_cap(app, nn);
308 }
309 
310 static inline bool nfp_app_has_tc(struct nfp_app *app)
311 {
312 	return app && app->type->setup_tc;
313 }
314 
315 static inline int nfp_app_setup_tc(struct nfp_app *app,
316 				   struct net_device *netdev,
317 				   enum tc_setup_type type, void *type_data)
318 {
319 	if (!app || !app->type->setup_tc)
320 		return -EOPNOTSUPP;
321 	return app->type->setup_tc(app, netdev, type, type_data);
322 }
323 
324 static inline int nfp_app_bpf(struct nfp_app *app, struct nfp_net *nn,
325 			      struct netdev_bpf *bpf)
326 {
327 	if (!app || !app->type->bpf)
328 		return -EINVAL;
329 	return app->type->bpf(app, nn, bpf);
330 }
331 
332 static inline int nfp_app_xdp_offload(struct nfp_app *app, struct nfp_net *nn,
333 				      struct bpf_prog *prog,
334 				      struct netlink_ext_ack *extack)
335 {
336 	if (!app || !app->type->xdp_offload)
337 		return -EOPNOTSUPP;
338 	return app->type->xdp_offload(app, nn, prog, extack);
339 }
340 
341 static inline bool __nfp_app_ctrl_tx(struct nfp_app *app, struct sk_buff *skb)
342 {
343 	trace_devlink_hwmsg(priv_to_devlink(app->pf), false, 0,
344 			    skb->data, skb->len);
345 
346 	return __nfp_ctrl_tx(app->ctrl, skb);
347 }
348 
349 static inline bool nfp_app_ctrl_tx(struct nfp_app *app, struct sk_buff *skb)
350 {
351 	trace_devlink_hwmsg(priv_to_devlink(app->pf), false, 0,
352 			    skb->data, skb->len);
353 
354 	return nfp_ctrl_tx(app->ctrl, skb);
355 }
356 
357 static inline void nfp_app_ctrl_rx(struct nfp_app *app, struct sk_buff *skb)
358 {
359 	trace_devlink_hwmsg(priv_to_devlink(app->pf), true, 0,
360 			    skb->data, skb->len);
361 
362 	app->type->ctrl_msg_rx(app, skb);
363 }
364 
365 static inline int nfp_app_eswitch_mode_get(struct nfp_app *app, u16 *mode)
366 {
367 	if (!app->type->eswitch_mode_get)
368 		return -EOPNOTSUPP;
369 
370 	*mode = app->type->eswitch_mode_get(app);
371 
372 	return 0;
373 }
374 
375 static inline int nfp_app_sriov_enable(struct nfp_app *app, int num_vfs)
376 {
377 	if (!app || !app->type->sriov_enable)
378 		return -EOPNOTSUPP;
379 	return app->type->sriov_enable(app, num_vfs);
380 }
381 
382 static inline void nfp_app_sriov_disable(struct nfp_app *app)
383 {
384 	if (app && app->type->sriov_disable)
385 		app->type->sriov_disable(app);
386 }
387 
388 static inline struct net_device *nfp_app_repr_get(struct nfp_app *app, u32 id)
389 {
390 	if (unlikely(!app || !app->type->repr_get))
391 		return NULL;
392 
393 	return app->type->repr_get(app, id);
394 }
395 
396 struct nfp_app *nfp_app_from_netdev(struct net_device *netdev);
397 
398 struct nfp_reprs *
399 nfp_reprs_get_locked(struct nfp_app *app, enum nfp_repr_type type);
400 struct nfp_reprs *
401 nfp_app_reprs_set(struct nfp_app *app, enum nfp_repr_type type,
402 		  struct nfp_reprs *reprs);
403 
404 const char *nfp_app_mip_name(struct nfp_app *app);
405 struct sk_buff *
406 nfp_app_ctrl_msg_alloc(struct nfp_app *app, unsigned int size, gfp_t priority);
407 
408 struct nfp_app *nfp_app_alloc(struct nfp_pf *pf, enum nfp_app_id id);
409 void nfp_app_free(struct nfp_app *app);
410 
411 /* Callbacks shared between apps */
412 
413 int nfp_app_nic_vnic_alloc(struct nfp_app *app, struct nfp_net *nn,
414 			   unsigned int id);
415 
416 #endif
417