1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
3 
4 #include <linux/bug.h>
5 #include <linux/lockdep.h>
6 #include <linux/rcupdate.h>
7 #include <linux/skbuff.h>
8 #include <linux/slab.h>
9 
10 #include "nfpcore/nfp_cpp.h"
11 #include "nfpcore/nfp_nffw.h"
12 #include "nfp_app.h"
13 #include "nfp_main.h"
14 #include "nfp_net.h"
15 #include "nfp_net_repr.h"
16 #include "nfp_port.h"
17 
18 static const struct nfp_app_type *apps[] = {
19 	[NFP_APP_CORE_NIC]	= &app_nic,
20 #ifdef CONFIG_BPF_SYSCALL
21 	[NFP_APP_BPF_NIC]	= &app_bpf,
22 #else
23 	[NFP_APP_BPF_NIC]	= &app_nic,
24 #endif
25 #ifdef CONFIG_NFP_APP_FLOWER
26 	[NFP_APP_FLOWER_NIC]	= &app_flower,
27 #endif
28 #ifdef CONFIG_NFP_APP_ABM_NIC
29 	[NFP_APP_ACTIVE_BUFFER_MGMT_NIC] = &app_abm,
30 #endif
31 };
32 
33 void nfp_check_rhashtable_empty(void *ptr, void *arg)
34 {
35 	WARN_ON_ONCE(1);
36 }
37 
38 struct nfp_app *nfp_app_from_netdev(struct net_device *netdev)
39 {
40 	if (nfp_netdev_is_nfp_net(netdev)) {
41 		struct nfp_net *nn = netdev_priv(netdev);
42 
43 		return nn->app;
44 	}
45 
46 	if (nfp_netdev_is_nfp_repr(netdev)) {
47 		struct nfp_repr *repr = netdev_priv(netdev);
48 
49 		return repr->app;
50 	}
51 
52 	WARN(1, "Unknown netdev type for nfp_app\n");
53 
54 	return NULL;
55 }
56 
57 const char *nfp_app_mip_name(struct nfp_app *app)
58 {
59 	if (!app || !app->pf->mip)
60 		return "";
61 	return nfp_mip_name(app->pf->mip);
62 }
63 
64 int nfp_app_ndo_init(struct net_device *netdev)
65 {
66 	struct nfp_app *app = nfp_app_from_netdev(netdev);
67 
68 	if (!app || !app->type->ndo_init)
69 		return 0;
70 	return app->type->ndo_init(app, netdev);
71 }
72 
73 void nfp_app_ndo_uninit(struct net_device *netdev)
74 {
75 	struct nfp_app *app = nfp_app_from_netdev(netdev);
76 
77 	if (app && app->type->ndo_uninit)
78 		app->type->ndo_uninit(app, netdev);
79 }
80 
81 u64 *nfp_app_port_get_stats(struct nfp_port *port, u64 *data)
82 {
83 	if (!port || !port->app || !port->app->type->port_get_stats)
84 		return data;
85 	return port->app->type->port_get_stats(port->app, port, data);
86 }
87 
88 int nfp_app_port_get_stats_count(struct nfp_port *port)
89 {
90 	if (!port || !port->app || !port->app->type->port_get_stats_count)
91 		return 0;
92 	return port->app->type->port_get_stats_count(port->app, port);
93 }
94 
95 u8 *nfp_app_port_get_stats_strings(struct nfp_port *port, u8 *data)
96 {
97 	if (!port || !port->app || !port->app->type->port_get_stats_strings)
98 		return data;
99 	return port->app->type->port_get_stats_strings(port->app, port, data);
100 }
101 
102 struct sk_buff *
103 nfp_app_ctrl_msg_alloc(struct nfp_app *app, unsigned int size, gfp_t priority)
104 {
105 	struct sk_buff *skb;
106 
107 	if (nfp_app_ctrl_has_meta(app))
108 		size += 8;
109 
110 	skb = alloc_skb(size, priority);
111 	if (!skb)
112 		return NULL;
113 
114 	if (nfp_app_ctrl_has_meta(app))
115 		skb_reserve(skb, 8);
116 
117 	return skb;
118 }
119 
120 struct nfp_reprs *
121 nfp_reprs_get_locked(struct nfp_app *app, enum nfp_repr_type type)
122 {
123 	return rcu_dereference_protected(app->reprs[type],
124 					 lockdep_is_held(&app->pf->lock));
125 }
126 
127 struct nfp_reprs *
128 nfp_app_reprs_set(struct nfp_app *app, enum nfp_repr_type type,
129 		  struct nfp_reprs *reprs)
130 {
131 	struct nfp_reprs *old;
132 
133 	old = nfp_reprs_get_locked(app, type);
134 	rcu_assign_pointer(app->reprs[type], reprs);
135 
136 	return old;
137 }
138 
139 struct nfp_app *nfp_app_alloc(struct nfp_pf *pf, enum nfp_app_id id)
140 {
141 	struct nfp_app *app;
142 
143 	if (id >= ARRAY_SIZE(apps) || !apps[id]) {
144 		nfp_err(pf->cpp, "unknown FW app ID 0x%02hhx, driver too old or support for FW not built in\n", id);
145 		return ERR_PTR(-EINVAL);
146 	}
147 
148 	if (WARN_ON(!apps[id]->name || !apps[id]->vnic_alloc))
149 		return ERR_PTR(-EINVAL);
150 	if (WARN_ON(!apps[id]->ctrl_msg_rx && apps[id]->ctrl_msg_rx_raw))
151 		return ERR_PTR(-EINVAL);
152 
153 	app = kzalloc(sizeof(*app), GFP_KERNEL);
154 	if (!app)
155 		return ERR_PTR(-ENOMEM);
156 
157 	app->pf = pf;
158 	app->cpp = pf->cpp;
159 	app->pdev = pf->pdev;
160 	app->type = apps[id];
161 
162 	return app;
163 }
164 
165 void nfp_app_free(struct nfp_app *app)
166 {
167 	kfree(app);
168 }
169