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 "../nfpcore/nfp_cpp.h"
42 #include "../nfp_net.h"
43 #include "../nfp_net_repr.h"
44 #include "./cmsg.h"
45
46 #define nfp_flower_cmsg_warn(app, fmt, args...) \
47 do { \
48 if (net_ratelimit()) \
49 nfp_warn((app)->cpp, fmt, ## args); \
50 } while (0)
51
52 static struct nfp_flower_cmsg_hdr *
nfp_flower_cmsg_get_hdr(struct sk_buff * skb)53 nfp_flower_cmsg_get_hdr(struct sk_buff *skb)
54 {
55 return (struct nfp_flower_cmsg_hdr *)skb->data;
56 }
57
58 struct sk_buff *
nfp_flower_cmsg_alloc(struct nfp_app * app,unsigned int size,enum nfp_flower_cmsg_type_port type)59 nfp_flower_cmsg_alloc(struct nfp_app *app, unsigned int size,
60 enum nfp_flower_cmsg_type_port type)
61 {
62 struct nfp_flower_cmsg_hdr *ch;
63 struct sk_buff *skb;
64
65 size += NFP_FLOWER_CMSG_HLEN;
66
67 skb = nfp_app_ctrl_msg_alloc(app, size, GFP_KERNEL);
68 if (!skb)
69 return NULL;
70
71 ch = nfp_flower_cmsg_get_hdr(skb);
72 ch->pad = 0;
73 ch->version = NFP_FLOWER_CMSG_VER1;
74 ch->type = type;
75 skb_put(skb, size);
76
77 return skb;
78 }
79
80 struct sk_buff *
nfp_flower_cmsg_mac_repr_start(struct nfp_app * app,unsigned int num_ports)81 nfp_flower_cmsg_mac_repr_start(struct nfp_app *app, unsigned int num_ports)
82 {
83 struct nfp_flower_cmsg_mac_repr *msg;
84 struct sk_buff *skb;
85 unsigned int size;
86
87 size = sizeof(*msg) + num_ports * sizeof(msg->ports[0]);
88 skb = nfp_flower_cmsg_alloc(app, size, NFP_FLOWER_CMSG_TYPE_MAC_REPR);
89 if (!skb)
90 return NULL;
91
92 msg = nfp_flower_cmsg_get_data(skb);
93 memset(msg->reserved, 0, sizeof(msg->reserved));
94 msg->num_ports = num_ports;
95
96 return skb;
97 }
98
99 void
nfp_flower_cmsg_mac_repr_add(struct sk_buff * skb,unsigned int idx,unsigned int nbi,unsigned int nbi_port,unsigned int phys_port)100 nfp_flower_cmsg_mac_repr_add(struct sk_buff *skb, unsigned int idx,
101 unsigned int nbi, unsigned int nbi_port,
102 unsigned int phys_port)
103 {
104 struct nfp_flower_cmsg_mac_repr *msg;
105
106 msg = nfp_flower_cmsg_get_data(skb);
107 msg->ports[idx].idx = idx;
108 msg->ports[idx].info = nbi & NFP_FLOWER_CMSG_MAC_REPR_NBI;
109 msg->ports[idx].nbi_port = nbi_port;
110 msg->ports[idx].phys_port = phys_port;
111 }
112
nfp_flower_cmsg_portmod(struct nfp_repr * repr,bool carrier_ok)113 int nfp_flower_cmsg_portmod(struct nfp_repr *repr, bool carrier_ok)
114 {
115 struct nfp_flower_cmsg_portmod *msg;
116 struct sk_buff *skb;
117
118 skb = nfp_flower_cmsg_alloc(repr->app, sizeof(*msg),
119 NFP_FLOWER_CMSG_TYPE_PORT_MOD);
120 if (!skb)
121 return -ENOMEM;
122
123 msg = nfp_flower_cmsg_get_data(skb);
124 msg->portnum = cpu_to_be32(repr->dst->u.port_info.port_id);
125 msg->reserved = 0;
126 msg->info = carrier_ok;
127 msg->mtu = cpu_to_be16(repr->netdev->mtu);
128
129 nfp_ctrl_tx(repr->app->ctrl, skb);
130
131 return 0;
132 }
133
134 static void
nfp_flower_cmsg_portmod_rx(struct nfp_app * app,struct sk_buff * skb)135 nfp_flower_cmsg_portmod_rx(struct nfp_app *app, struct sk_buff *skb)
136 {
137 struct nfp_flower_cmsg_portmod *msg;
138 struct net_device *netdev;
139 bool link;
140
141 msg = nfp_flower_cmsg_get_data(skb);
142 link = msg->info & NFP_FLOWER_CMSG_PORTMOD_INFO_LINK;
143
144 rtnl_lock();
145 rcu_read_lock();
146 netdev = nfp_app_repr_get(app, be32_to_cpu(msg->portnum));
147 rcu_read_unlock();
148 if (!netdev) {
149 nfp_flower_cmsg_warn(app, "ctrl msg for unknown port 0x%08x\n",
150 be32_to_cpu(msg->portnum));
151 rtnl_unlock();
152 return;
153 }
154
155 if (link) {
156 u16 mtu = be16_to_cpu(msg->mtu);
157
158 netif_carrier_on(netdev);
159
160 /* An MTU of 0 from the firmware should be ignored */
161 if (mtu)
162 dev_set_mtu(netdev, mtu);
163 } else {
164 netif_carrier_off(netdev);
165 }
166 rtnl_unlock();
167 }
168
169 static void
nfp_flower_cmsg_process_one_rx(struct nfp_app * app,struct sk_buff * skb)170 nfp_flower_cmsg_process_one_rx(struct nfp_app *app, struct sk_buff *skb)
171 {
172 struct nfp_flower_cmsg_hdr *cmsg_hdr;
173 enum nfp_flower_cmsg_type_port type;
174
175 cmsg_hdr = nfp_flower_cmsg_get_hdr(skb);
176
177 if (unlikely(cmsg_hdr->version != NFP_FLOWER_CMSG_VER1)) {
178 nfp_flower_cmsg_warn(app, "Cannot handle repr control version %u\n",
179 cmsg_hdr->version);
180 goto out;
181 }
182
183 type = cmsg_hdr->type;
184 switch (type) {
185 case NFP_FLOWER_CMSG_TYPE_PORT_MOD:
186 nfp_flower_cmsg_portmod_rx(app, skb);
187 break;
188 case NFP_FLOWER_CMSG_TYPE_FLOW_STATS:
189 nfp_flower_rx_flow_stats(app, skb);
190 break;
191 default:
192 nfp_flower_cmsg_warn(app, "Cannot handle invalid repr control type %u\n",
193 type);
194 goto out;
195 }
196
197 dev_consume_skb_any(skb);
198 return;
199 out:
200 dev_kfree_skb_any(skb);
201 }
202
nfp_flower_cmsg_process_rx(struct work_struct * work)203 void nfp_flower_cmsg_process_rx(struct work_struct *work)
204 {
205 struct nfp_flower_priv *priv;
206 struct sk_buff *skb;
207
208 priv = container_of(work, struct nfp_flower_priv, cmsg_work);
209
210 while ((skb = skb_dequeue(&priv->cmsg_skbs)))
211 nfp_flower_cmsg_process_one_rx(priv->app, skb);
212 }
213
nfp_flower_cmsg_rx(struct nfp_app * app,struct sk_buff * skb)214 void nfp_flower_cmsg_rx(struct nfp_app *app, struct sk_buff *skb)
215 {
216 struct nfp_flower_priv *priv = app->priv;
217
218 skb_queue_tail(&priv->cmsg_skbs, skb);
219 schedule_work(&priv->cmsg_work);
220 }
221