]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/netronome/nfp/nfp_app.h
ff2d43615808093270ff9b00e628ab7423c953ec
[karo-tx-linux.git] / drivers / net / ethernet / netronome / nfp / nfp_app.h
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 tc_to_netdev;
46 struct sk_buff;
47 struct nfp_app;
48 struct nfp_cpp;
49 struct nfp_pf;
50 struct nfp_net;
51
52 enum nfp_app_id {
53         NFP_APP_CORE_NIC        = 0x1,
54         NFP_APP_BPF_NIC         = 0x2,
55 };
56
57 extern const struct nfp_app_type app_nic;
58 extern const struct nfp_app_type app_bpf;
59
60 /**
61  * struct nfp_app_type - application definition
62  * @id:         application ID
63  * @name:       application name
64  * @ctrl_has_meta:  control messages have prepend of type:5/port:CTRL
65  *
66  * Callbacks
67  * @init:       perform basic app checks
68  * @extra_cap:  extra capabilities string
69  * @vnic_init:  init vNICs (assign port types, etc.)
70  * @vnic_clean: clean up app's vNIC state
71  * @start:      start application logic
72  * @stop:       stop application logic
73  * @ctrl_msg_rx:    control message handler
74  * @setup_tc:   setup TC ndo
75  * @tc_busy:    TC HW offload busy (rules loaded)
76  * @xdp_offload:    offload an XDP program
77  * @eswitch_mode_get:    get SR-IOV eswitch mode
78  * @sriov_enable: app-specific sriov initialisation
79  * @sriov_disable: app-specific sriov clean-up
80  * @repr_get:   get representor netdev
81  */
82 struct nfp_app_type {
83         enum nfp_app_id id;
84         const char *name;
85
86         bool ctrl_has_meta;
87
88         int (*init)(struct nfp_app *app);
89
90         const char *(*extra_cap)(struct nfp_app *app, struct nfp_net *nn);
91
92         int (*vnic_init)(struct nfp_app *app, struct nfp_net *nn,
93                          unsigned int id);
94         void (*vnic_clean)(struct nfp_app *app, struct nfp_net *nn);
95
96         int (*start)(struct nfp_app *app);
97         void (*stop)(struct nfp_app *app);
98
99         void (*ctrl_msg_rx)(struct nfp_app *app, struct sk_buff *skb);
100
101         int (*setup_tc)(struct nfp_app *app, struct net_device *netdev,
102                         u32 handle, __be16 proto, struct tc_to_netdev *tc);
103         bool (*tc_busy)(struct nfp_app *app, struct nfp_net *nn);
104         int (*xdp_offload)(struct nfp_app *app, struct nfp_net *nn,
105                            struct bpf_prog *prog);
106
107         int (*sriov_enable)(struct nfp_app *app, int num_vfs);
108         void (*sriov_disable)(struct nfp_app *app);
109
110         enum devlink_eswitch_mode (*eswitch_mode_get)(struct nfp_app *app);
111         struct net_device *(*repr_get)(struct nfp_app *app, u32 id);
112 };
113
114 /**
115  * struct nfp_app - NFP application container
116  * @pdev:       backpointer to PCI device
117  * @pf:         backpointer to NFP PF structure
118  * @cpp:        pointer to the CPP handle
119  * @ctrl:       pointer to ctrl vNIC struct
120  * @reprs:      array of pointers to representors
121  * @type:       pointer to const application ops and info
122  */
123 struct nfp_app {
124         struct pci_dev *pdev;
125         struct nfp_pf *pf;
126         struct nfp_cpp *cpp;
127
128         struct nfp_net *ctrl;
129         struct nfp_reprs __rcu *reprs[NFP_REPR_TYPE_MAX + 1];
130
131         const struct nfp_app_type *type;
132 };
133
134 bool nfp_ctrl_tx(struct nfp_net *nn, struct sk_buff *skb);
135
136 static inline int nfp_app_init(struct nfp_app *app)
137 {
138         if (!app->type->init)
139                 return 0;
140         return app->type->init(app);
141 }
142
143 static inline int nfp_app_vnic_init(struct nfp_app *app, struct nfp_net *nn,
144                                     unsigned int id)
145 {
146         return app->type->vnic_init(app, nn, id);
147 }
148
149 static inline void nfp_app_vnic_clean(struct nfp_app *app, struct nfp_net *nn)
150 {
151         if (app->type->vnic_clean)
152                 app->type->vnic_clean(app, nn);
153 }
154
155 static inline int nfp_app_start(struct nfp_app *app, struct nfp_net *ctrl)
156 {
157         app->ctrl = ctrl;
158         if (!app->type->start)
159                 return 0;
160         return app->type->start(app);
161 }
162
163 static inline void nfp_app_stop(struct nfp_app *app)
164 {
165         if (!app->type->stop)
166                 return;
167         app->type->stop(app);
168 }
169
170 static inline const char *nfp_app_name(struct nfp_app *app)
171 {
172         if (!app)
173                 return "";
174         return app->type->name;
175 }
176
177 static inline bool nfp_app_needs_ctrl_vnic(struct nfp_app *app)
178 {
179         return app && app->type->ctrl_msg_rx;
180 }
181
182 static inline bool nfp_app_ctrl_has_meta(struct nfp_app *app)
183 {
184         return app->type->ctrl_has_meta;
185 }
186
187 static inline const char *nfp_app_extra_cap(struct nfp_app *app,
188                                             struct nfp_net *nn)
189 {
190         if (!app || !app->type->extra_cap)
191                 return "";
192         return app->type->extra_cap(app, nn);
193 }
194
195 static inline bool nfp_app_has_tc(struct nfp_app *app)
196 {
197         return app && app->type->setup_tc;
198 }
199
200 static inline bool nfp_app_tc_busy(struct nfp_app *app, struct nfp_net *nn)
201 {
202         if (!app || !app->type->tc_busy)
203                 return false;
204         return app->type->tc_busy(app, nn);
205 }
206
207 static inline int nfp_app_setup_tc(struct nfp_app *app,
208                                    struct net_device *netdev,
209                                    u32 handle, __be16 proto,
210                                    struct tc_to_netdev *tc)
211 {
212         if (!app || !app->type->setup_tc)
213                 return -EOPNOTSUPP;
214         return app->type->setup_tc(app, netdev, handle, proto, tc);
215 }
216
217 static inline int nfp_app_xdp_offload(struct nfp_app *app, struct nfp_net *nn,
218                                       struct bpf_prog *prog)
219 {
220         if (!app || !app->type->xdp_offload)
221                 return -EOPNOTSUPP;
222         return app->type->xdp_offload(app, nn, prog);
223 }
224
225 static inline bool nfp_app_ctrl_tx(struct nfp_app *app, struct sk_buff *skb)
226 {
227         return nfp_ctrl_tx(app->ctrl, skb);
228 }
229
230 static inline void nfp_app_ctrl_rx(struct nfp_app *app, struct sk_buff *skb)
231 {
232         app->type->ctrl_msg_rx(app, skb);
233 }
234
235 static inline int nfp_app_eswitch_mode_get(struct nfp_app *app, u16 *mode)
236 {
237         if (!app->type->eswitch_mode_get)
238                 return -EOPNOTSUPP;
239
240         *mode = app->type->eswitch_mode_get(app);
241
242         return 0;
243 }
244
245 static inline int nfp_app_sriov_enable(struct nfp_app *app, int num_vfs)
246 {
247         if (!app || !app->type->sriov_enable)
248                 return -EOPNOTSUPP;
249         return app->type->sriov_enable(app, num_vfs);
250 }
251
252 static inline void nfp_app_sriov_disable(struct nfp_app *app)
253 {
254         if (app && app->type->sriov_disable)
255                 app->type->sriov_disable(app);
256 }
257
258 static inline struct net_device *nfp_app_repr_get(struct nfp_app *app, u32 id)
259 {
260         if (unlikely(!app || !app->type->repr_get))
261                 return NULL;
262
263         return app->type->repr_get(app, id);
264 }
265
266 struct nfp_reprs *
267 nfp_app_reprs_set(struct nfp_app *app, enum nfp_repr_type type,
268                   struct nfp_reprs *reprs);
269
270 const char *nfp_app_mip_name(struct nfp_app *app);
271 struct sk_buff *nfp_app_ctrl_msg_alloc(struct nfp_app *app, unsigned int size);
272
273 struct nfp_app *nfp_app_alloc(struct nfp_pf *pf, enum nfp_app_id id);
274 void nfp_app_free(struct nfp_app *app);
275
276 /* Callbacks shared between apps */
277
278 int nfp_app_nic_vnic_init(struct nfp_app *app, struct nfp_net *nn,
279                           unsigned int id);
280
281 #endif