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