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
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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
37 #include <net/devlink.h>
39 #include "nfp_net_repr.h"
54 NFP_APP_CORE_NIC = 0x1,
55 NFP_APP_BPF_NIC = 0x2,
56 NFP_APP_FLOWER_NIC = 0x3,
59 extern const struct nfp_app_type app_nic;
60 extern const struct nfp_app_type app_bpf;
61 extern const struct nfp_app_type app_flower;
64 * struct nfp_app_type - application definition
66 * @name: application name
67 * @ctrl_has_meta: control messages have prepend of type:5/port:CTRL
70 * @init: perform basic app checks and init
71 * @clean: clean app state
72 * @extra_cap: extra capabilities string
73 * @vnic_init: init vNICs (assign port types, etc.)
74 * @vnic_clean: clean up app's vNIC state
75 * @repr_open: representor netdev open callback
76 * @repr_stop: representor netdev stop callback
77 * @start: start application logic
78 * @stop: stop application logic
79 * @ctrl_msg_rx: control message handler
80 * @setup_tc: setup TC ndo
81 * @tc_busy: TC HW offload busy (rules loaded)
82 * @xdp_offload: offload an XDP program
83 * @eswitch_mode_get: get SR-IOV eswitch mode
84 * @sriov_enable: app-specific sriov initialisation
85 * @sriov_disable: app-specific sriov clean-up
86 * @repr_get: get representor netdev
94 int (*init)(struct nfp_app *app);
95 void (*clean)(struct nfp_app *app);
97 const char *(*extra_cap)(struct nfp_app *app, struct nfp_net *nn);
99 int (*vnic_init)(struct nfp_app *app, struct nfp_net *nn,
101 void (*vnic_clean)(struct nfp_app *app, struct nfp_net *nn);
103 int (*repr_open)(struct nfp_app *app, struct nfp_repr *repr);
104 int (*repr_stop)(struct nfp_app *app, struct nfp_repr *repr);
106 int (*start)(struct nfp_app *app);
107 void (*stop)(struct nfp_app *app);
109 void (*ctrl_msg_rx)(struct nfp_app *app, struct sk_buff *skb);
111 int (*setup_tc)(struct nfp_app *app, struct net_device *netdev,
112 u32 handle, __be16 proto, struct tc_to_netdev *tc);
113 bool (*tc_busy)(struct nfp_app *app, struct nfp_net *nn);
114 int (*xdp_offload)(struct nfp_app *app, struct nfp_net *nn,
115 struct bpf_prog *prog);
117 int (*sriov_enable)(struct nfp_app *app, int num_vfs);
118 void (*sriov_disable)(struct nfp_app *app);
120 enum devlink_eswitch_mode (*eswitch_mode_get)(struct nfp_app *app);
121 struct net_device *(*repr_get)(struct nfp_app *app, u32 id);
125 * struct nfp_app - NFP application container
126 * @pdev: backpointer to PCI device
127 * @pf: backpointer to NFP PF structure
128 * @cpp: pointer to the CPP handle
129 * @ctrl: pointer to ctrl vNIC struct
130 * @reprs: array of pointers to representors
131 * @type: pointer to const application ops and info
132 * @priv: app-specific priv data
135 struct pci_dev *pdev;
139 struct nfp_net *ctrl;
140 struct nfp_reprs __rcu *reprs[NFP_REPR_TYPE_MAX + 1];
142 const struct nfp_app_type *type;
146 bool nfp_ctrl_tx(struct nfp_net *nn, struct sk_buff *skb);
148 static inline int nfp_app_init(struct nfp_app *app)
150 if (!app->type->init)
152 return app->type->init(app);
155 static inline void nfp_app_clean(struct nfp_app *app)
157 if (app->type->clean)
158 app->type->clean(app);
161 static inline int nfp_app_vnic_init(struct nfp_app *app, struct nfp_net *nn,
164 return app->type->vnic_init(app, nn, id);
167 static inline void nfp_app_vnic_clean(struct nfp_app *app, struct nfp_net *nn)
169 if (app->type->vnic_clean)
170 app->type->vnic_clean(app, nn);
173 static inline int nfp_app_repr_open(struct nfp_app *app, struct nfp_repr *repr)
175 if (!app->type->repr_open)
177 return app->type->repr_open(app, repr);
180 static inline int nfp_app_repr_stop(struct nfp_app *app, struct nfp_repr *repr)
182 if (!app->type->repr_stop)
184 return app->type->repr_stop(app, repr);
187 static inline int nfp_app_start(struct nfp_app *app, struct nfp_net *ctrl)
190 if (!app->type->start)
192 return app->type->start(app);
195 static inline void nfp_app_stop(struct nfp_app *app)
197 if (!app->type->stop)
199 app->type->stop(app);
202 static inline const char *nfp_app_name(struct nfp_app *app)
206 return app->type->name;
209 static inline bool nfp_app_needs_ctrl_vnic(struct nfp_app *app)
211 return app && app->type->ctrl_msg_rx;
214 static inline bool nfp_app_ctrl_has_meta(struct nfp_app *app)
216 return app->type->ctrl_has_meta;
219 static inline const char *nfp_app_extra_cap(struct nfp_app *app,
222 if (!app || !app->type->extra_cap)
224 return app->type->extra_cap(app, nn);
227 static inline bool nfp_app_has_tc(struct nfp_app *app)
229 return app && app->type->setup_tc;
232 static inline bool nfp_app_tc_busy(struct nfp_app *app, struct nfp_net *nn)
234 if (!app || !app->type->tc_busy)
236 return app->type->tc_busy(app, nn);
239 static inline int nfp_app_setup_tc(struct nfp_app *app,
240 struct net_device *netdev,
241 u32 handle, __be16 proto,
242 struct tc_to_netdev *tc)
244 if (!app || !app->type->setup_tc)
246 return app->type->setup_tc(app, netdev, handle, proto, tc);
249 static inline int nfp_app_xdp_offload(struct nfp_app *app, struct nfp_net *nn,
250 struct bpf_prog *prog)
252 if (!app || !app->type->xdp_offload)
254 return app->type->xdp_offload(app, nn, prog);
257 static inline bool nfp_app_ctrl_tx(struct nfp_app *app, struct sk_buff *skb)
259 return nfp_ctrl_tx(app->ctrl, skb);
262 static inline void nfp_app_ctrl_rx(struct nfp_app *app, struct sk_buff *skb)
264 app->type->ctrl_msg_rx(app, skb);
267 static inline int nfp_app_eswitch_mode_get(struct nfp_app *app, u16 *mode)
269 if (!app->type->eswitch_mode_get)
272 *mode = app->type->eswitch_mode_get(app);
277 static inline int nfp_app_sriov_enable(struct nfp_app *app, int num_vfs)
279 if (!app || !app->type->sriov_enable)
281 return app->type->sriov_enable(app, num_vfs);
284 static inline void nfp_app_sriov_disable(struct nfp_app *app)
286 if (app && app->type->sriov_disable)
287 app->type->sriov_disable(app);
290 static inline struct net_device *nfp_app_repr_get(struct nfp_app *app, u32 id)
292 if (unlikely(!app || !app->type->repr_get))
295 return app->type->repr_get(app, id);
299 nfp_app_reprs_set(struct nfp_app *app, enum nfp_repr_type type,
300 struct nfp_reprs *reprs);
302 const char *nfp_app_mip_name(struct nfp_app *app);
304 nfp_app_ctrl_msg_alloc(struct nfp_app *app, unsigned int size, gfp_t priority);
306 struct nfp_app *nfp_app_alloc(struct nfp_pf *pf, enum nfp_app_id id);
307 void nfp_app_free(struct nfp_app *app);
309 /* Callbacks shared between apps */
311 int nfp_app_nic_vnic_init(struct nfp_app *app, struct nfp_net *nn,