License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-2.6/btrfs-unstable.git] / drivers / net / ethernet / netronome / nfp / nfp_app.h
blobaf640b5c2108f387cb90a4ccd37dd9f662ac5742
1 /*
2 * Copyright (C) 2017 Netronome Systems, Inc.
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.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
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.
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.
34 #ifndef _NFP_APP_H
35 #define _NFP_APP_H 1
37 #include <net/devlink.h>
39 #include "nfp_net_repr.h"
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;
52 enum nfp_app_id {
53 NFP_APP_CORE_NIC = 0x1,
54 NFP_APP_BPF_NIC = 0x2,
55 NFP_APP_FLOWER_NIC = 0x3,
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;
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
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
89 struct nfp_app_type {
90 enum nfp_app_id id;
91 const char *name;
93 bool ctrl_has_meta;
95 int (*init)(struct nfp_app *app);
96 void (*clean)(struct nfp_app *app);
98 const char *(*extra_cap)(struct nfp_app *app, struct nfp_net *nn);
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);
106 int (*repr_open)(struct nfp_app *app, struct nfp_repr *repr);
107 int (*repr_stop)(struct nfp_app *app, struct nfp_repr *repr);
109 int (*start)(struct nfp_app *app);
110 void (*stop)(struct nfp_app *app);
112 void (*ctrl_msg_rx)(struct nfp_app *app, struct sk_buff *skb);
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);
120 int (*sriov_enable)(struct nfp_app *app, int num_vfs);
121 void (*sriov_disable)(struct nfp_app *app);
123 enum devlink_eswitch_mode (*eswitch_mode_get)(struct nfp_app *app);
124 struct net_device *(*repr_get)(struct nfp_app *app, u32 id);
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
137 struct nfp_app {
138 struct pci_dev *pdev;
139 struct nfp_pf *pf;
140 struct nfp_cpp *cpp;
142 struct nfp_net *ctrl;
143 struct nfp_reprs __rcu *reprs[NFP_REPR_TYPE_MAX + 1];
145 const struct nfp_app_type *type;
146 void *priv;
149 bool nfp_ctrl_tx(struct nfp_net *nn, struct sk_buff *skb);
151 static inline int nfp_app_init(struct nfp_app *app)
153 if (!app->type->init)
154 return 0;
155 return app->type->init(app);
158 static inline void nfp_app_clean(struct nfp_app *app)
160 if (app->type->clean)
161 app->type->clean(app);
164 static inline int nfp_app_vnic_alloc(struct nfp_app *app, struct nfp_net *nn,
165 unsigned int id)
167 return app->type->vnic_alloc(app, nn, id);
170 static inline void nfp_app_vnic_free(struct nfp_app *app, struct nfp_net *nn)
172 if (app->type->vnic_free)
173 app->type->vnic_free(app, nn);
176 static inline int nfp_app_vnic_init(struct nfp_app *app, struct nfp_net *nn)
178 if (!app->type->vnic_init)
179 return 0;
180 return app->type->vnic_init(app, nn);
183 static inline void nfp_app_vnic_clean(struct nfp_app *app, struct nfp_net *nn)
185 if (app->type->vnic_clean)
186 app->type->vnic_clean(app, nn);
189 static inline int nfp_app_repr_open(struct nfp_app *app, struct nfp_repr *repr)
191 if (!app->type->repr_open)
192 return -EINVAL;
193 return app->type->repr_open(app, repr);
196 static inline int nfp_app_repr_stop(struct nfp_app *app, struct nfp_repr *repr)
198 if (!app->type->repr_stop)
199 return -EINVAL;
200 return app->type->repr_stop(app, repr);
203 static inline int nfp_app_start(struct nfp_app *app, struct nfp_net *ctrl)
205 app->ctrl = ctrl;
206 if (!app->type->start)
207 return 0;
208 return app->type->start(app);
211 static inline void nfp_app_stop(struct nfp_app *app)
213 if (!app->type->stop)
214 return;
215 app->type->stop(app);
218 static inline const char *nfp_app_name(struct nfp_app *app)
220 if (!app)
221 return "";
222 return app->type->name;
225 static inline bool nfp_app_needs_ctrl_vnic(struct nfp_app *app)
227 return app && app->type->ctrl_msg_rx;
230 static inline bool nfp_app_ctrl_has_meta(struct nfp_app *app)
232 return app->type->ctrl_has_meta;
235 static inline const char *nfp_app_extra_cap(struct nfp_app *app,
236 struct nfp_net *nn)
238 if (!app || !app->type->extra_cap)
239 return "";
240 return app->type->extra_cap(app, nn);
243 static inline bool nfp_app_has_tc(struct nfp_app *app)
245 return app && app->type->setup_tc;
248 static inline bool nfp_app_tc_busy(struct nfp_app *app, struct nfp_net *nn)
250 if (!app || !app->type->tc_busy)
251 return false;
252 return app->type->tc_busy(app, nn);
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)
259 if (!app || !app->type->setup_tc)
260 return -EOPNOTSUPP;
261 return app->type->setup_tc(app, netdev, type, type_data);
264 static inline int nfp_app_xdp_offload(struct nfp_app *app, struct nfp_net *nn,
265 struct bpf_prog *prog)
267 if (!app || !app->type->xdp_offload)
268 return -EOPNOTSUPP;
269 return app->type->xdp_offload(app, nn, prog);
272 static inline bool nfp_app_ctrl_tx(struct nfp_app *app, struct sk_buff *skb)
274 return nfp_ctrl_tx(app->ctrl, skb);
277 static inline void nfp_app_ctrl_rx(struct nfp_app *app, struct sk_buff *skb)
279 app->type->ctrl_msg_rx(app, skb);
282 static inline int nfp_app_eswitch_mode_get(struct nfp_app *app, u16 *mode)
284 if (!app->type->eswitch_mode_get)
285 return -EOPNOTSUPP;
287 *mode = app->type->eswitch_mode_get(app);
289 return 0;
292 static inline int nfp_app_sriov_enable(struct nfp_app *app, int num_vfs)
294 if (!app || !app->type->sriov_enable)
295 return -EOPNOTSUPP;
296 return app->type->sriov_enable(app, num_vfs);
299 static inline void nfp_app_sriov_disable(struct nfp_app *app)
301 if (app && app->type->sriov_disable)
302 app->type->sriov_disable(app);
305 static inline struct net_device *nfp_app_repr_get(struct nfp_app *app, u32 id)
307 if (unlikely(!app || !app->type->repr_get))
308 return NULL;
310 return app->type->repr_get(app, id);
313 struct nfp_app *nfp_app_from_netdev(struct net_device *netdev);
315 struct nfp_reprs *
316 nfp_app_reprs_set(struct nfp_app *app, enum nfp_repr_type type,
317 struct nfp_reprs *reprs);
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);
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);
326 /* Callbacks shared between apps */
328 int nfp_app_nic_vnic_alloc(struct nfp_app *app, struct nfp_net *nn,
329 unsigned int id);
331 #endif