1 /*
2 * Copyright (C) 2017-2018 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/lockdep.h>
36 #include <linux/rcupdate.h>
37 #include <linux/skbuff.h>
38 #include <linux/slab.h>
39
40 #include "nfpcore/nfp_cpp.h"
41 #include "nfpcore/nfp_nffw.h"
42 #include "nfp_app.h"
43 #include "nfp_main.h"
44 #include "nfp_net.h"
45 #include "nfp_net_repr.h"
46 #include "nfp_port.h"
47
48 static const struct nfp_app_type *apps[] = {
49 [NFP_APP_CORE_NIC] = &app_nic,
50 #ifdef CONFIG_BPF_SYSCALL
51 [NFP_APP_BPF_NIC] = &app_bpf,
52 #else
53 [NFP_APP_BPF_NIC] = &app_nic,
54 #endif
55 #ifdef CONFIG_NFP_APP_FLOWER
56 [NFP_APP_FLOWER_NIC] = &app_flower,
57 #endif
58 #ifdef CONFIG_NFP_APP_ABM_NIC
59 [NFP_APP_ACTIVE_BUFFER_MGMT_NIC] = &app_abm,
60 #endif
61 };
62
nfp_app_from_netdev(struct net_device * netdev)63 struct nfp_app *nfp_app_from_netdev(struct net_device *netdev)
64 {
65 if (nfp_netdev_is_nfp_net(netdev)) {
66 struct nfp_net *nn = netdev_priv(netdev);
67
68 return nn->app;
69 }
70
71 if (nfp_netdev_is_nfp_repr(netdev)) {
72 struct nfp_repr *repr = netdev_priv(netdev);
73
74 return repr->app;
75 }
76
77 WARN(1, "Unknown netdev type for nfp_app\n");
78
79 return NULL;
80 }
81
nfp_app_mip_name(struct nfp_app * app)82 const char *nfp_app_mip_name(struct nfp_app *app)
83 {
84 if (!app || !app->pf->mip)
85 return "";
86 return nfp_mip_name(app->pf->mip);
87 }
88
nfp_app_ndo_init(struct net_device * netdev)89 int nfp_app_ndo_init(struct net_device *netdev)
90 {
91 struct nfp_app *app = nfp_app_from_netdev(netdev);
92
93 if (!app || !app->type->ndo_init)
94 return 0;
95 return app->type->ndo_init(app, netdev);
96 }
97
nfp_app_ndo_uninit(struct net_device * netdev)98 void nfp_app_ndo_uninit(struct net_device *netdev)
99 {
100 struct nfp_app *app = nfp_app_from_netdev(netdev);
101
102 if (app && app->type->ndo_uninit)
103 app->type->ndo_uninit(app, netdev);
104 }
105
nfp_app_port_get_stats(struct nfp_port * port,u64 * data)106 u64 *nfp_app_port_get_stats(struct nfp_port *port, u64 *data)
107 {
108 if (!port || !port->app || !port->app->type->port_get_stats)
109 return data;
110 return port->app->type->port_get_stats(port->app, port, data);
111 }
112
nfp_app_port_get_stats_count(struct nfp_port * port)113 int nfp_app_port_get_stats_count(struct nfp_port *port)
114 {
115 if (!port || !port->app || !port->app->type->port_get_stats_count)
116 return 0;
117 return port->app->type->port_get_stats_count(port->app, port);
118 }
119
nfp_app_port_get_stats_strings(struct nfp_port * port,u8 * data)120 u8 *nfp_app_port_get_stats_strings(struct nfp_port *port, u8 *data)
121 {
122 if (!port || !port->app || !port->app->type->port_get_stats_strings)
123 return data;
124 return port->app->type->port_get_stats_strings(port->app, port, data);
125 }
126
127 struct sk_buff *
nfp_app_ctrl_msg_alloc(struct nfp_app * app,unsigned int size,gfp_t priority)128 nfp_app_ctrl_msg_alloc(struct nfp_app *app, unsigned int size, gfp_t priority)
129 {
130 struct sk_buff *skb;
131
132 if (nfp_app_ctrl_has_meta(app))
133 size += 8;
134
135 skb = alloc_skb(size, priority);
136 if (!skb)
137 return NULL;
138
139 if (nfp_app_ctrl_has_meta(app))
140 skb_reserve(skb, 8);
141
142 return skb;
143 }
144
145 struct nfp_reprs *
nfp_reprs_get_locked(struct nfp_app * app,enum nfp_repr_type type)146 nfp_reprs_get_locked(struct nfp_app *app, enum nfp_repr_type type)
147 {
148 return rcu_dereference_protected(app->reprs[type],
149 lockdep_is_held(&app->pf->lock));
150 }
151
152 struct nfp_reprs *
nfp_app_reprs_set(struct nfp_app * app,enum nfp_repr_type type,struct nfp_reprs * reprs)153 nfp_app_reprs_set(struct nfp_app *app, enum nfp_repr_type type,
154 struct nfp_reprs *reprs)
155 {
156 struct nfp_reprs *old;
157
158 old = nfp_reprs_get_locked(app, type);
159 rcu_assign_pointer(app->reprs[type], reprs);
160
161 return old;
162 }
163
nfp_app_alloc(struct nfp_pf * pf,enum nfp_app_id id)164 struct nfp_app *nfp_app_alloc(struct nfp_pf *pf, enum nfp_app_id id)
165 {
166 struct nfp_app *app;
167
168 if (id >= ARRAY_SIZE(apps) || !apps[id]) {
169 nfp_err(pf->cpp, "unknown FW app ID 0x%02hhx, driver too old or support for FW not built in\n", id);
170 return ERR_PTR(-EINVAL);
171 }
172
173 if (WARN_ON(!apps[id]->name || !apps[id]->vnic_alloc))
174 return ERR_PTR(-EINVAL);
175 if (WARN_ON(!apps[id]->ctrl_msg_rx && apps[id]->ctrl_msg_rx_raw))
176 return ERR_PTR(-EINVAL);
177
178 app = kzalloc(sizeof(*app), GFP_KERNEL);
179 if (!app)
180 return ERR_PTR(-ENOMEM);
181
182 app->pf = pf;
183 app->cpp = pf->cpp;
184 app->pdev = pf->pdev;
185 app->type = apps[id];
186
187 return app;
188 }
189
nfp_app_free(struct nfp_app * app)190 void nfp_app_free(struct nfp_app *app)
191 {
192 kfree(app);
193 }
194