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