xref: /openbmc/linux/drivers/net/ethernet/netronome/nfp/flower/cmsg.c (revision 28efb0046512e8a13ed9f9bdf0d68d10bbfbe9cf)
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)
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, GFP_KERNEL);
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 	if (!skb)
83 		return NULL;
84 
85 	msg = nfp_flower_cmsg_get_data(skb);
86 	memset(msg->reserved, 0, sizeof(msg->reserved));
87 	msg->num_ports = num_ports;
88 
89 	return skb;
90 }
91 
92 void
93 nfp_flower_cmsg_mac_repr_add(struct sk_buff *skb, unsigned int idx,
94 			     unsigned int nbi, unsigned int nbi_port,
95 			     unsigned int phys_port)
96 {
97 	struct nfp_flower_cmsg_mac_repr *msg;
98 
99 	msg = nfp_flower_cmsg_get_data(skb);
100 	msg->ports[idx].idx = idx;
101 	msg->ports[idx].info = nbi & NFP_FLOWER_CMSG_MAC_REPR_NBI;
102 	msg->ports[idx].nbi_port = nbi_port;
103 	msg->ports[idx].phys_port = phys_port;
104 }
105 
106 int nfp_flower_cmsg_portmod(struct nfp_repr *repr, bool carrier_ok)
107 {
108 	struct nfp_flower_cmsg_portmod *msg;
109 	struct sk_buff *skb;
110 
111 	skb = nfp_flower_cmsg_alloc(repr->app, sizeof(*msg),
112 				    NFP_FLOWER_CMSG_TYPE_PORT_MOD);
113 	if (!skb)
114 		return -ENOMEM;
115 
116 	msg = nfp_flower_cmsg_get_data(skb);
117 	msg->portnum = cpu_to_be32(repr->dst->u.port_info.port_id);
118 	msg->reserved = 0;
119 	msg->info = carrier_ok;
120 	msg->mtu = cpu_to_be16(repr->netdev->mtu);
121 
122 	nfp_ctrl_tx(repr->app->ctrl, skb);
123 
124 	return 0;
125 }
126 
127 static void
128 nfp_flower_cmsg_portmod_rx(struct nfp_app *app, struct sk_buff *skb)
129 {
130 	struct nfp_flower_cmsg_portmod *msg;
131 	struct net_device *netdev;
132 	bool link;
133 
134 	msg = nfp_flower_cmsg_get_data(skb);
135 	link = msg->info & NFP_FLOWER_CMSG_PORTMOD_INFO_LINK;
136 
137 	rtnl_lock();
138 	rcu_read_lock();
139 	netdev = nfp_app_repr_get(app, be32_to_cpu(msg->portnum));
140 	rcu_read_unlock();
141 	if (!netdev) {
142 		nfp_flower_cmsg_warn(app, "ctrl msg for unknown port 0x%08x\n",
143 				     be32_to_cpu(msg->portnum));
144 		rtnl_unlock();
145 		return;
146 	}
147 
148 	if (link) {
149 		u16 mtu = be16_to_cpu(msg->mtu);
150 
151 		netif_carrier_on(netdev);
152 
153 		/* An MTU of 0 from the firmware should be ignored */
154 		if (mtu)
155 			dev_set_mtu(netdev, mtu);
156 	} else {
157 		netif_carrier_off(netdev);
158 	}
159 	rtnl_unlock();
160 }
161 
162 static void
163 nfp_flower_cmsg_process_one_rx(struct nfp_app *app, struct sk_buff *skb)
164 {
165 	struct nfp_flower_cmsg_hdr *cmsg_hdr;
166 	enum nfp_flower_cmsg_type_port type;
167 
168 	cmsg_hdr = nfp_flower_cmsg_get_hdr(skb);
169 
170 	if (unlikely(cmsg_hdr->version != NFP_FLOWER_CMSG_VER1)) {
171 		nfp_flower_cmsg_warn(app, "Cannot handle repr control version %u\n",
172 				     cmsg_hdr->version);
173 		goto out;
174 	}
175 
176 	type = cmsg_hdr->type;
177 	switch (type) {
178 	case NFP_FLOWER_CMSG_TYPE_PORT_MOD:
179 		nfp_flower_cmsg_portmod_rx(app, skb);
180 		break;
181 	case NFP_FLOWER_CMSG_TYPE_FLOW_STATS:
182 		nfp_flower_rx_flow_stats(app, skb);
183 		break;
184 	case NFP_FLOWER_CMSG_TYPE_NO_NEIGH:
185 		nfp_tunnel_request_route(app, skb);
186 		break;
187 	case NFP_FLOWER_CMSG_TYPE_ACTIVE_TUNS:
188 		nfp_tunnel_keep_alive(app, skb);
189 		break;
190 	case NFP_FLOWER_CMSG_TYPE_TUN_NEIGH:
191 		/* Acks from the NFP that the route is added - ignore. */
192 		break;
193 	default:
194 		nfp_flower_cmsg_warn(app, "Cannot handle invalid repr control type %u\n",
195 				     type);
196 		goto out;
197 	}
198 
199 	dev_consume_skb_any(skb);
200 	return;
201 out:
202 	dev_kfree_skb_any(skb);
203 }
204 
205 void nfp_flower_cmsg_process_rx(struct work_struct *work)
206 {
207 	struct nfp_flower_priv *priv;
208 	struct sk_buff *skb;
209 
210 	priv = container_of(work, struct nfp_flower_priv, cmsg_work);
211 
212 	while ((skb = skb_dequeue(&priv->cmsg_skbs)))
213 		nfp_flower_cmsg_process_one_rx(priv->app, skb);
214 }
215 
216 void nfp_flower_cmsg_rx(struct nfp_app *app, struct sk_buff *skb)
217 {
218 	struct nfp_flower_priv *priv = app->priv;
219 
220 	skb_queue_tail(&priv->cmsg_skbs, skb);
221 	schedule_work(&priv->cmsg_work);
222 }
223