1 /*
2  * Copyright (C) 2015-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/bitfield.h>
35 #include <linux/netdevice.h>
36 #include <linux/skbuff.h>
37 #include <linux/workqueue.h>
38 #include <net/dst_metadata.h>
39 
40 #include "main.h"
41 #include "../nfp_net.h"
42 #include "../nfp_net_repr.h"
43 #include "./cmsg.h"
44 
45 static struct nfp_flower_cmsg_hdr *
46 nfp_flower_cmsg_get_hdr(struct sk_buff *skb)
47 {
48 	return (struct nfp_flower_cmsg_hdr *)skb->data;
49 }
50 
51 struct sk_buff *
52 nfp_flower_cmsg_alloc(struct nfp_app *app, unsigned int size,
53 		      enum nfp_flower_cmsg_type_port type, gfp_t flag)
54 {
55 	struct nfp_flower_cmsg_hdr *ch;
56 	struct sk_buff *skb;
57 
58 	size += NFP_FLOWER_CMSG_HLEN;
59 
60 	skb = nfp_app_ctrl_msg_alloc(app, size, flag);
61 	if (!skb)
62 		return NULL;
63 
64 	ch = nfp_flower_cmsg_get_hdr(skb);
65 	ch->pad = 0;
66 	ch->version = NFP_FLOWER_CMSG_VER1;
67 	ch->type = type;
68 	skb_put(skb, size);
69 
70 	return skb;
71 }
72 
73 struct sk_buff *
74 nfp_flower_cmsg_mac_repr_start(struct nfp_app *app, unsigned int num_ports)
75 {
76 	struct nfp_flower_cmsg_mac_repr *msg;
77 	struct sk_buff *skb;
78 	unsigned int size;
79 
80 	size = sizeof(*msg) + num_ports * sizeof(msg->ports[0]);
81 	skb = nfp_flower_cmsg_alloc(app, size, NFP_FLOWER_CMSG_TYPE_MAC_REPR,
82 				    GFP_KERNEL);
83 	if (!skb)
84 		return NULL;
85 
86 	msg = nfp_flower_cmsg_get_data(skb);
87 	memset(msg->reserved, 0, sizeof(msg->reserved));
88 	msg->num_ports = num_ports;
89 
90 	return skb;
91 }
92 
93 void
94 nfp_flower_cmsg_mac_repr_add(struct sk_buff *skb, unsigned int idx,
95 			     unsigned int nbi, unsigned int nbi_port,
96 			     unsigned int phys_port)
97 {
98 	struct nfp_flower_cmsg_mac_repr *msg;
99 
100 	msg = nfp_flower_cmsg_get_data(skb);
101 	msg->ports[idx].idx = idx;
102 	msg->ports[idx].info = nbi & NFP_FLOWER_CMSG_MAC_REPR_NBI;
103 	msg->ports[idx].nbi_port = nbi_port;
104 	msg->ports[idx].phys_port = phys_port;
105 }
106 
107 int nfp_flower_cmsg_portmod(struct nfp_repr *repr, bool carrier_ok)
108 {
109 	struct nfp_flower_cmsg_portmod *msg;
110 	struct sk_buff *skb;
111 
112 	skb = nfp_flower_cmsg_alloc(repr->app, sizeof(*msg),
113 				    NFP_FLOWER_CMSG_TYPE_PORT_MOD, GFP_KERNEL);
114 	if (!skb)
115 		return -ENOMEM;
116 
117 	msg = nfp_flower_cmsg_get_data(skb);
118 	msg->portnum = cpu_to_be32(repr->dst->u.port_info.port_id);
119 	msg->reserved = 0;
120 	msg->info = carrier_ok;
121 	msg->mtu = cpu_to_be16(repr->netdev->mtu);
122 
123 	nfp_ctrl_tx(repr->app->ctrl, skb);
124 
125 	return 0;
126 }
127 
128 int nfp_flower_cmsg_portreify(struct nfp_repr *repr, bool exists)
129 {
130 	struct nfp_flower_cmsg_portreify *msg;
131 	struct sk_buff *skb;
132 
133 	skb = nfp_flower_cmsg_alloc(repr->app, sizeof(*msg),
134 				    NFP_FLOWER_CMSG_TYPE_PORT_REIFY,
135 				    GFP_KERNEL);
136 	if (!skb)
137 		return -ENOMEM;
138 
139 	msg = nfp_flower_cmsg_get_data(skb);
140 	msg->portnum = cpu_to_be32(repr->dst->u.port_info.port_id);
141 	msg->reserved = 0;
142 	msg->info = cpu_to_be16(exists);
143 
144 	nfp_ctrl_tx(repr->app->ctrl, skb);
145 
146 	return 0;
147 }
148 
149 static void
150 nfp_flower_cmsg_portmod_rx(struct nfp_app *app, struct sk_buff *skb)
151 {
152 	struct nfp_flower_cmsg_portmod *msg;
153 	struct net_device *netdev;
154 	bool link;
155 
156 	msg = nfp_flower_cmsg_get_data(skb);
157 	link = msg->info & NFP_FLOWER_CMSG_PORTMOD_INFO_LINK;
158 
159 	rtnl_lock();
160 	rcu_read_lock();
161 	netdev = nfp_app_repr_get(app, be32_to_cpu(msg->portnum));
162 	rcu_read_unlock();
163 	if (!netdev) {
164 		nfp_flower_cmsg_warn(app, "ctrl msg for unknown port 0x%08x\n",
165 				     be32_to_cpu(msg->portnum));
166 		rtnl_unlock();
167 		return;
168 	}
169 
170 	if (link) {
171 		u16 mtu = be16_to_cpu(msg->mtu);
172 
173 		netif_carrier_on(netdev);
174 
175 		/* An MTU of 0 from the firmware should be ignored */
176 		if (mtu)
177 			dev_set_mtu(netdev, mtu);
178 	} else {
179 		netif_carrier_off(netdev);
180 	}
181 	rtnl_unlock();
182 }
183 
184 static void
185 nfp_flower_cmsg_portreify_rx(struct nfp_app *app, struct sk_buff *skb)
186 {
187 	struct nfp_flower_priv *priv = app->priv;
188 	struct nfp_flower_cmsg_portreify *msg;
189 	bool exists;
190 
191 	msg = nfp_flower_cmsg_get_data(skb);
192 
193 	rcu_read_lock();
194 	exists = !!nfp_app_repr_get(app, be32_to_cpu(msg->portnum));
195 	rcu_read_unlock();
196 	if (!exists) {
197 		nfp_flower_cmsg_warn(app, "ctrl msg for unknown port 0x%08x\n",
198 				     be32_to_cpu(msg->portnum));
199 		return;
200 	}
201 
202 	atomic_inc(&priv->reify_replies);
203 	wake_up_interruptible(&priv->reify_wait_queue);
204 }
205 
206 static void
207 nfp_flower_cmsg_process_one_rx(struct nfp_app *app, struct sk_buff *skb)
208 {
209 	struct nfp_flower_cmsg_hdr *cmsg_hdr;
210 	enum nfp_flower_cmsg_type_port type;
211 
212 	cmsg_hdr = nfp_flower_cmsg_get_hdr(skb);
213 
214 	type = cmsg_hdr->type;
215 	switch (type) {
216 	case NFP_FLOWER_CMSG_TYPE_PORT_REIFY:
217 		nfp_flower_cmsg_portreify_rx(app, skb);
218 		break;
219 	case NFP_FLOWER_CMSG_TYPE_PORT_MOD:
220 		nfp_flower_cmsg_portmod_rx(app, skb);
221 		break;
222 	case NFP_FLOWER_CMSG_TYPE_NO_NEIGH:
223 		nfp_tunnel_request_route(app, skb);
224 		break;
225 	case NFP_FLOWER_CMSG_TYPE_ACTIVE_TUNS:
226 		nfp_tunnel_keep_alive(app, skb);
227 		break;
228 	case NFP_FLOWER_CMSG_TYPE_TUN_NEIGH:
229 		/* Acks from the NFP that the route is added - ignore. */
230 		break;
231 	default:
232 		nfp_flower_cmsg_warn(app, "Cannot handle invalid repr control type %u\n",
233 				     type);
234 		goto out;
235 	}
236 
237 	dev_consume_skb_any(skb);
238 	return;
239 out:
240 	dev_kfree_skb_any(skb);
241 }
242 
243 void nfp_flower_cmsg_process_rx(struct work_struct *work)
244 {
245 	struct nfp_flower_priv *priv;
246 	struct sk_buff *skb;
247 
248 	priv = container_of(work, struct nfp_flower_priv, cmsg_work);
249 
250 	while ((skb = skb_dequeue(&priv->cmsg_skbs)))
251 		nfp_flower_cmsg_process_one_rx(priv->app, skb);
252 }
253 
254 void nfp_flower_cmsg_rx(struct nfp_app *app, struct sk_buff *skb)
255 {
256 	struct nfp_flower_priv *priv = app->priv;
257 	struct nfp_flower_cmsg_hdr *cmsg_hdr;
258 
259 	cmsg_hdr = nfp_flower_cmsg_get_hdr(skb);
260 
261 	if (unlikely(cmsg_hdr->version != NFP_FLOWER_CMSG_VER1)) {
262 		nfp_flower_cmsg_warn(app, "Cannot handle repr control version %u\n",
263 				     cmsg_hdr->version);
264 		dev_kfree_skb_any(skb);
265 		return;
266 	}
267 
268 	if (cmsg_hdr->type == NFP_FLOWER_CMSG_TYPE_FLOW_STATS) {
269 		/* We need to deal with stats updates from HW asap */
270 		nfp_flower_rx_flow_stats(app, skb);
271 		dev_consume_skb_any(skb);
272 	} else {
273 		skb_queue_tail(&priv->cmsg_skbs, skb);
274 		schedule_work(&priv->cmsg_work);
275 	}
276 }
277