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