2 * Copyright (C) 2015-2017 Netronome Systems, Inc.
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.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
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>
41 #include "../nfp_net.h"
42 #include "../nfp_net_repr.h"
45 static struct nfp_flower_cmsg_hdr *
46 nfp_flower_cmsg_get_hdr(struct sk_buff *skb)
48 return (struct nfp_flower_cmsg_hdr *)skb->data;
52 nfp_flower_cmsg_alloc(struct nfp_app *app, unsigned int size,
53 enum nfp_flower_cmsg_type_port type, gfp_t flag)
55 struct nfp_flower_cmsg_hdr *ch;
58 size += NFP_FLOWER_CMSG_HLEN;
60 skb = nfp_app_ctrl_msg_alloc(app, size, flag);
64 ch = nfp_flower_cmsg_get_hdr(skb);
66 ch->version = NFP_FLOWER_CMSG_VER1;
74 nfp_flower_cmsg_mac_repr_start(struct nfp_app *app, unsigned int num_ports)
76 struct nfp_flower_cmsg_mac_repr *msg;
80 size = sizeof(*msg) + num_ports * sizeof(msg->ports[0]);
81 skb = nfp_flower_cmsg_alloc(app, size, NFP_FLOWER_CMSG_TYPE_MAC_REPR,
86 msg = nfp_flower_cmsg_get_data(skb);
87 memset(msg->reserved, 0, sizeof(msg->reserved));
88 msg->num_ports = num_ports;
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)
98 struct nfp_flower_cmsg_mac_repr *msg;
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;
107 int nfp_flower_cmsg_portmod(struct nfp_repr *repr, bool carrier_ok,
108 unsigned int mtu, bool mtu_only)
110 struct nfp_flower_cmsg_portmod *msg;
113 skb = nfp_flower_cmsg_alloc(repr->app, sizeof(*msg),
114 NFP_FLOWER_CMSG_TYPE_PORT_MOD, GFP_KERNEL);
118 msg = nfp_flower_cmsg_get_data(skb);
119 msg->portnum = cpu_to_be32(repr->dst->u.port_info.port_id);
121 msg->info = carrier_ok;
124 msg->info |= NFP_FLOWER_CMSG_PORTMOD_MTU_CHANGE_ONLY;
126 msg->mtu = cpu_to_be16(mtu);
128 nfp_ctrl_tx(repr->app->ctrl, skb);
133 int nfp_flower_cmsg_portreify(struct nfp_repr *repr, bool exists)
135 struct nfp_flower_cmsg_portreify *msg;
138 skb = nfp_flower_cmsg_alloc(repr->app, sizeof(*msg),
139 NFP_FLOWER_CMSG_TYPE_PORT_REIFY,
144 msg = nfp_flower_cmsg_get_data(skb);
145 msg->portnum = cpu_to_be32(repr->dst->u.port_info.port_id);
147 msg->info = cpu_to_be16(exists);
149 nfp_ctrl_tx(repr->app->ctrl, skb);
155 nfp_flower_process_mtu_ack(struct nfp_app *app, struct sk_buff *skb)
157 struct nfp_flower_priv *app_priv = app->priv;
158 struct nfp_flower_cmsg_portmod *msg;
160 msg = nfp_flower_cmsg_get_data(skb);
162 if (!(msg->info & NFP_FLOWER_CMSG_PORTMOD_MTU_CHANGE_ONLY))
165 spin_lock_bh(&app_priv->mtu_conf.lock);
166 if (!app_priv->mtu_conf.requested_val ||
167 app_priv->mtu_conf.portnum != be32_to_cpu(msg->portnum) ||
168 be16_to_cpu(msg->mtu) != app_priv->mtu_conf.requested_val) {
169 /* Not an ack for requested MTU change. */
170 spin_unlock_bh(&app_priv->mtu_conf.lock);
174 app_priv->mtu_conf.ack = true;
175 app_priv->mtu_conf.requested_val = 0;
176 wake_up(&app_priv->mtu_conf.wait_q);
177 spin_unlock_bh(&app_priv->mtu_conf.lock);
183 nfp_flower_cmsg_portmod_rx(struct nfp_app *app, struct sk_buff *skb)
185 struct nfp_flower_cmsg_portmod *msg;
186 struct net_device *netdev;
189 msg = nfp_flower_cmsg_get_data(skb);
190 link = msg->info & NFP_FLOWER_CMSG_PORTMOD_INFO_LINK;
194 netdev = nfp_app_repr_get(app, be32_to_cpu(msg->portnum));
197 nfp_flower_cmsg_warn(app, "ctrl msg for unknown port 0x%08x\n",
198 be32_to_cpu(msg->portnum));
204 u16 mtu = be16_to_cpu(msg->mtu);
206 netif_carrier_on(netdev);
208 /* An MTU of 0 from the firmware should be ignored */
210 dev_set_mtu(netdev, mtu);
212 netif_carrier_off(netdev);
218 nfp_flower_cmsg_portreify_rx(struct nfp_app *app, struct sk_buff *skb)
220 struct nfp_flower_priv *priv = app->priv;
221 struct nfp_flower_cmsg_portreify *msg;
224 msg = nfp_flower_cmsg_get_data(skb);
227 exists = !!nfp_app_repr_get(app, be32_to_cpu(msg->portnum));
230 nfp_flower_cmsg_warn(app, "ctrl msg for unknown port 0x%08x\n",
231 be32_to_cpu(msg->portnum));
235 atomic_inc(&priv->reify_replies);
236 wake_up_interruptible(&priv->reify_wait_queue);
240 nfp_flower_cmsg_process_one_rx(struct nfp_app *app, struct sk_buff *skb)
242 struct nfp_flower_cmsg_hdr *cmsg_hdr;
243 enum nfp_flower_cmsg_type_port type;
245 cmsg_hdr = nfp_flower_cmsg_get_hdr(skb);
247 type = cmsg_hdr->type;
249 case NFP_FLOWER_CMSG_TYPE_PORT_REIFY:
250 nfp_flower_cmsg_portreify_rx(app, skb);
252 case NFP_FLOWER_CMSG_TYPE_PORT_MOD:
253 nfp_flower_cmsg_portmod_rx(app, skb);
255 case NFP_FLOWER_CMSG_TYPE_NO_NEIGH:
256 nfp_tunnel_request_route(app, skb);
258 case NFP_FLOWER_CMSG_TYPE_ACTIVE_TUNS:
259 nfp_tunnel_keep_alive(app, skb);
262 nfp_flower_cmsg_warn(app, "Cannot handle invalid repr control type %u\n",
267 dev_consume_skb_any(skb);
270 dev_kfree_skb_any(skb);
273 void nfp_flower_cmsg_process_rx(struct work_struct *work)
275 struct sk_buff_head cmsg_joined;
276 struct nfp_flower_priv *priv;
279 priv = container_of(work, struct nfp_flower_priv, cmsg_work);
280 skb_queue_head_init(&cmsg_joined);
282 spin_lock_bh(&priv->cmsg_skbs_high.lock);
283 skb_queue_splice_tail_init(&priv->cmsg_skbs_high, &cmsg_joined);
284 spin_unlock_bh(&priv->cmsg_skbs_high.lock);
286 spin_lock_bh(&priv->cmsg_skbs_low.lock);
287 skb_queue_splice_tail_init(&priv->cmsg_skbs_low, &cmsg_joined);
288 spin_unlock_bh(&priv->cmsg_skbs_low.lock);
290 while ((skb = __skb_dequeue(&cmsg_joined)))
291 nfp_flower_cmsg_process_one_rx(priv->app, skb);
295 nfp_flower_queue_ctl_msg(struct nfp_app *app, struct sk_buff *skb, int type)
297 struct nfp_flower_priv *priv = app->priv;
298 struct sk_buff_head *skb_head;
300 if (type == NFP_FLOWER_CMSG_TYPE_PORT_REIFY ||
301 type == NFP_FLOWER_CMSG_TYPE_PORT_MOD)
302 skb_head = &priv->cmsg_skbs_high;
304 skb_head = &priv->cmsg_skbs_low;
306 if (skb_queue_len(skb_head) >= NFP_FLOWER_WORKQ_MAX_SKBS) {
307 nfp_flower_cmsg_warn(app, "Dropping queued control messages\n");
308 dev_kfree_skb_any(skb);
312 skb_queue_tail(skb_head, skb);
313 schedule_work(&priv->cmsg_work);
316 void nfp_flower_cmsg_rx(struct nfp_app *app, struct sk_buff *skb)
318 struct nfp_flower_cmsg_hdr *cmsg_hdr;
320 cmsg_hdr = nfp_flower_cmsg_get_hdr(skb);
322 if (unlikely(cmsg_hdr->version != NFP_FLOWER_CMSG_VER1)) {
323 nfp_flower_cmsg_warn(app, "Cannot handle repr control version %u\n",
325 dev_kfree_skb_any(skb);
329 if (cmsg_hdr->type == NFP_FLOWER_CMSG_TYPE_FLOW_STATS) {
330 /* We need to deal with stats updates from HW asap */
331 nfp_flower_rx_flow_stats(app, skb);
332 dev_consume_skb_any(skb);
333 } else if (cmsg_hdr->type == NFP_FLOWER_CMSG_TYPE_PORT_MOD &&
334 nfp_flower_process_mtu_ack(app, skb)) {
335 /* Handle MTU acks outside wq to prevent RTNL conflict. */
336 dev_consume_skb_any(skb);
337 } else if (cmsg_hdr->type == NFP_FLOWER_CMSG_TYPE_TUN_NEIGH) {
338 /* Acks from the NFP that the route is added - ignore. */
339 dev_consume_skb_any(skb);
341 nfp_flower_queue_ctl_msg(app, skb, cmsg_hdr->type);