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 #include <linux/bug.h> 35 #include <linux/skbuff.h> 36 #include <linux/slab.h> 37 38 #include "nfpcore/nfp_cpp.h" 39 #include "nfpcore/nfp_nffw.h" 40 #include "nfp_app.h" 41 #include "nfp_main.h" 42 #include "nfp_net.h" 43 #include "nfp_net_repr.h" 44 45 static const struct nfp_app_type *apps[] = { 46 &app_nic, 47 &app_bpf, 48 #ifdef CONFIG_NFP_APP_FLOWER 49 &app_flower, 50 #endif 51 }; 52 53 struct nfp_app *nfp_app_from_netdev(struct net_device *netdev) 54 { 55 if (nfp_netdev_is_nfp_net(netdev)) { 56 struct nfp_net *nn = netdev_priv(netdev); 57 58 return nn->app; 59 } 60 61 if (nfp_netdev_is_nfp_repr(netdev)) { 62 struct nfp_repr *repr = netdev_priv(netdev); 63 64 return repr->app; 65 } 66 67 WARN(1, "Unknown netdev type for nfp_app\n"); 68 69 return NULL; 70 } 71 72 const char *nfp_app_mip_name(struct nfp_app *app) 73 { 74 if (!app || !app->pf->mip) 75 return ""; 76 return nfp_mip_name(app->pf->mip); 77 } 78 79 struct sk_buff * 80 nfp_app_ctrl_msg_alloc(struct nfp_app *app, unsigned int size, gfp_t priority) 81 { 82 struct sk_buff *skb; 83 84 if (nfp_app_ctrl_has_meta(app)) 85 size += 8; 86 87 skb = alloc_skb(size, priority); 88 if (!skb) 89 return NULL; 90 91 if (nfp_app_ctrl_has_meta(app)) 92 skb_reserve(skb, 8); 93 94 return skb; 95 } 96 97 struct nfp_reprs * 98 nfp_app_reprs_set(struct nfp_app *app, enum nfp_repr_type type, 99 struct nfp_reprs *reprs) 100 { 101 struct nfp_reprs *old; 102 103 old = rcu_dereference_protected(app->reprs[type], 104 lockdep_is_held(&app->pf->lock)); 105 if (reprs && old) { 106 old = ERR_PTR(-EBUSY); 107 goto exit_unlock; 108 } 109 110 rcu_assign_pointer(app->reprs[type], reprs); 111 112 exit_unlock: 113 return old; 114 } 115 116 struct nfp_app *nfp_app_alloc(struct nfp_pf *pf, enum nfp_app_id id) 117 { 118 struct nfp_app *app; 119 unsigned int i; 120 121 for (i = 0; i < ARRAY_SIZE(apps); i++) 122 if (apps[i]->id == id) 123 break; 124 if (i == ARRAY_SIZE(apps)) { 125 nfp_err(pf->cpp, "failed to find app with ID 0x%02hhx\n", id); 126 return ERR_PTR(-EINVAL); 127 } 128 129 if (WARN_ON(!apps[i]->name || !apps[i]->vnic_alloc)) 130 return ERR_PTR(-EINVAL); 131 132 app = kzalloc(sizeof(*app), GFP_KERNEL); 133 if (!app) 134 return ERR_PTR(-ENOMEM); 135 136 app->pf = pf; 137 app->cpp = pf->cpp; 138 app->pdev = pf->pdev; 139 app->type = apps[i]; 140 141 return app; 142 } 143 144 void nfp_app_free(struct nfp_app *app) 145 { 146 kfree(app); 147 } 148