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