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 "nfp_net_repr.h"
40 
41 struct bpf_prog;
42 struct net_device;
43 struct pci_dev;
44 struct sk_buff;
45 struct sk_buff;
46 struct nfp_app;
47 struct nfp_cpp;
48 struct nfp_pf;
49 struct nfp_repr;
50 struct nfp_net;
51 
52 enum nfp_app_id {
53 	NFP_APP_CORE_NIC	= 0x1,
54 	NFP_APP_BPF_NIC		= 0x2,
55 	NFP_APP_FLOWER_NIC	= 0x3,
56 };
57 
58 extern const struct nfp_app_type app_nic;
59 extern const struct nfp_app_type app_bpf;
60 extern const struct nfp_app_type app_flower;
61 
62 /**
63  * struct nfp_app_type - application definition
64  * @id:		application ID
65  * @name:	application name
66  * @ctrl_has_meta:  control messages have prepend of type:5/port:CTRL
67  *
68  * Callbacks
69  * @init:	perform basic app checks and init
70  * @clean:	clean app state
71  * @extra_cap:	extra capabilities string
72  * @vnic_alloc:	allocate vNICs (assign port types, etc.)
73  * @vnic_free:	free up app's vNIC state
74  * @vnic_init:	vNIC netdev was registered
75  * @vnic_clean:	vNIC netdev about to be unregistered
76  * @repr_open:	representor netdev open callback
77  * @repr_stop:	representor netdev stop callback
78  * @start:	start application logic
79  * @stop:	stop application logic
80  * @ctrl_msg_rx:    control message handler
81  * @setup_tc:	setup TC ndo
82  * @tc_busy:	TC HW offload busy (rules loaded)
83  * @xdp_offload:    offload an XDP program
84  * @eswitch_mode_get:    get SR-IOV eswitch mode
85  * @sriov_enable: app-specific sriov initialisation
86  * @sriov_disable: app-specific sriov clean-up
87  * @repr_get:	get representor netdev
88  */
89 struct nfp_app_type {
90 	enum nfp_app_id id;
91 	const char *name;
92 
93 	bool ctrl_has_meta;
94 
95 	int (*init)(struct nfp_app *app);
96 	void (*clean)(struct nfp_app *app);
97 
98 	const char *(*extra_cap)(struct nfp_app *app, struct nfp_net *nn);
99 
100 	int (*vnic_alloc)(struct nfp_app *app, struct nfp_net *nn,
101 			  unsigned int id);
102 	void (*vnic_free)(struct nfp_app *app, struct nfp_net *nn);
103 	int (*vnic_init)(struct nfp_app *app, struct nfp_net *nn);
104 	void (*vnic_clean)(struct nfp_app *app, struct nfp_net *nn);
105 
106 	int (*repr_open)(struct nfp_app *app, struct nfp_repr *repr);
107 	int (*repr_stop)(struct nfp_app *app, struct nfp_repr *repr);
108 
109 	int (*start)(struct nfp_app *app);
110 	void (*stop)(struct nfp_app *app);
111 
112 	void (*ctrl_msg_rx)(struct nfp_app *app, struct sk_buff *skb);
113 
114 	int (*setup_tc)(struct nfp_app *app, struct net_device *netdev,
115 			enum tc_setup_type type, void *type_data);
116 	bool (*tc_busy)(struct nfp_app *app, struct nfp_net *nn);
117 	int (*xdp_offload)(struct nfp_app *app, struct nfp_net *nn,
118 			   struct bpf_prog *prog);
119 
120 	int (*sriov_enable)(struct nfp_app *app, int num_vfs);
121 	void (*sriov_disable)(struct nfp_app *app);
122 
123 	enum devlink_eswitch_mode (*eswitch_mode_get)(struct nfp_app *app);
124 	struct net_device *(*repr_get)(struct nfp_app *app, u32 id);
125 };
126 
127 /**
128  * struct nfp_app - NFP application container
129  * @pdev:	backpointer to PCI device
130  * @pf:		backpointer to NFP PF structure
131  * @cpp:	pointer to the CPP handle
132  * @ctrl:	pointer to ctrl vNIC struct
133  * @reprs:	array of pointers to representors
134  * @type:	pointer to const application ops and info
135  * @priv:	app-specific priv data
136  */
137 struct nfp_app {
138 	struct pci_dev *pdev;
139 	struct nfp_pf *pf;
140 	struct nfp_cpp *cpp;
141 
142 	struct nfp_net *ctrl;
143 	struct nfp_reprs __rcu *reprs[NFP_REPR_TYPE_MAX + 1];
144 
145 	const struct nfp_app_type *type;
146 	void *priv;
147 };
148 
149 bool nfp_ctrl_tx(struct nfp_net *nn, struct sk_buff *skb);
150 
151 static inline int nfp_app_init(struct nfp_app *app)
152 {
153 	if (!app->type->init)
154 		return 0;
155 	return app->type->init(app);
156 }
157 
158 static inline void nfp_app_clean(struct nfp_app *app)
159 {
160 	if (app->type->clean)
161 		app->type->clean(app);
162 }
163 
164 static inline int nfp_app_vnic_alloc(struct nfp_app *app, struct nfp_net *nn,
165 				     unsigned int id)
166 {
167 	return app->type->vnic_alloc(app, nn, id);
168 }
169 
170 static inline void nfp_app_vnic_free(struct nfp_app *app, struct nfp_net *nn)
171 {
172 	if (app->type->vnic_free)
173 		app->type->vnic_free(app, nn);
174 }
175 
176 static inline int nfp_app_vnic_init(struct nfp_app *app, struct nfp_net *nn)
177 {
178 	if (!app->type->vnic_init)
179 		return 0;
180 	return app->type->vnic_init(app, nn);
181 }
182 
183 static inline void nfp_app_vnic_clean(struct nfp_app *app, struct nfp_net *nn)
184 {
185 	if (app->type->vnic_clean)
186 		app->type->vnic_clean(app, nn);
187 }
188 
189 static inline int nfp_app_repr_open(struct nfp_app *app, struct nfp_repr *repr)
190 {
191 	if (!app->type->repr_open)
192 		return -EINVAL;
193 	return app->type->repr_open(app, repr);
194 }
195 
196 static inline int nfp_app_repr_stop(struct nfp_app *app, struct nfp_repr *repr)
197 {
198 	if (!app->type->repr_stop)
199 		return -EINVAL;
200 	return app->type->repr_stop(app, repr);
201 }
202 
203 static inline int nfp_app_start(struct nfp_app *app, struct nfp_net *ctrl)
204 {
205 	app->ctrl = ctrl;
206 	if (!app->type->start)
207 		return 0;
208 	return app->type->start(app);
209 }
210 
211 static inline void nfp_app_stop(struct nfp_app *app)
212 {
213 	if (!app->type->stop)
214 		return;
215 	app->type->stop(app);
216 }
217 
218 static inline const char *nfp_app_name(struct nfp_app *app)
219 {
220 	if (!app)
221 		return "";
222 	return app->type->name;
223 }
224 
225 static inline bool nfp_app_needs_ctrl_vnic(struct nfp_app *app)
226 {
227 	return app && app->type->ctrl_msg_rx;
228 }
229 
230 static inline bool nfp_app_ctrl_has_meta(struct nfp_app *app)
231 {
232 	return app->type->ctrl_has_meta;
233 }
234 
235 static inline const char *nfp_app_extra_cap(struct nfp_app *app,
236 					    struct nfp_net *nn)
237 {
238 	if (!app || !app->type->extra_cap)
239 		return "";
240 	return app->type->extra_cap(app, nn);
241 }
242 
243 static inline bool nfp_app_has_tc(struct nfp_app *app)
244 {
245 	return app && app->type->setup_tc;
246 }
247 
248 static inline bool nfp_app_tc_busy(struct nfp_app *app, struct nfp_net *nn)
249 {
250 	if (!app || !app->type->tc_busy)
251 		return false;
252 	return app->type->tc_busy(app, nn);
253 }
254 
255 static inline int nfp_app_setup_tc(struct nfp_app *app,
256 				   struct net_device *netdev,
257 				   enum tc_setup_type type, void *type_data)
258 {
259 	if (!app || !app->type->setup_tc)
260 		return -EOPNOTSUPP;
261 	return app->type->setup_tc(app, netdev, type, type_data);
262 }
263 
264 static inline int nfp_app_xdp_offload(struct nfp_app *app, struct nfp_net *nn,
265 				      struct bpf_prog *prog)
266 {
267 	if (!app || !app->type->xdp_offload)
268 		return -EOPNOTSUPP;
269 	return app->type->xdp_offload(app, nn, prog);
270 }
271 
272 static inline bool nfp_app_ctrl_tx(struct nfp_app *app, struct sk_buff *skb)
273 {
274 	return nfp_ctrl_tx(app->ctrl, skb);
275 }
276 
277 static inline void nfp_app_ctrl_rx(struct nfp_app *app, struct sk_buff *skb)
278 {
279 	app->type->ctrl_msg_rx(app, skb);
280 }
281 
282 static inline int nfp_app_eswitch_mode_get(struct nfp_app *app, u16 *mode)
283 {
284 	if (!app->type->eswitch_mode_get)
285 		return -EOPNOTSUPP;
286 
287 	*mode = app->type->eswitch_mode_get(app);
288 
289 	return 0;
290 }
291 
292 static inline int nfp_app_sriov_enable(struct nfp_app *app, int num_vfs)
293 {
294 	if (!app || !app->type->sriov_enable)
295 		return -EOPNOTSUPP;
296 	return app->type->sriov_enable(app, num_vfs);
297 }
298 
299 static inline void nfp_app_sriov_disable(struct nfp_app *app)
300 {
301 	if (app && app->type->sriov_disable)
302 		app->type->sriov_disable(app);
303 }
304 
305 static inline struct net_device *nfp_app_repr_get(struct nfp_app *app, u32 id)
306 {
307 	if (unlikely(!app || !app->type->repr_get))
308 		return NULL;
309 
310 	return app->type->repr_get(app, id);
311 }
312 
313 struct nfp_app *nfp_app_from_netdev(struct net_device *netdev);
314 
315 struct nfp_reprs *
316 nfp_app_reprs_set(struct nfp_app *app, enum nfp_repr_type type,
317 		  struct nfp_reprs *reprs);
318 
319 const char *nfp_app_mip_name(struct nfp_app *app);
320 struct sk_buff *
321 nfp_app_ctrl_msg_alloc(struct nfp_app *app, unsigned int size, gfp_t priority);
322 
323 struct nfp_app *nfp_app_alloc(struct nfp_pf *pf, enum nfp_app_id id);
324 void nfp_app_free(struct nfp_app *app);
325 
326 /* Callbacks shared between apps */
327 
328 int nfp_app_nic_vnic_alloc(struct nfp_app *app, struct nfp_net *nn,
329 			   unsigned int id);
330 
331 #endif
332