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