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