xref: /linux/drivers/net/ethernet/mellanox/mlx5/core/en_selftest.c (revision cfaaa7d010d1fc58f9717fcc8591201e741d2d49)
1 /*
2  * Copyright (c) 2016, Mellanox Technologies, Ltd.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #include <linux/ip.h>
34 #include <linux/udp.h>
35 #include <net/udp.h>
36 #include "en.h"
37 #include "en/port.h"
38 #include "eswitch.h"
39 #include "lib/mlx5.h"
40 
mlx5e_test_health_info(struct mlx5e_priv * priv)41 static int mlx5e_test_health_info(struct mlx5e_priv *priv)
42 {
43 	struct mlx5_core_health *health = &priv->mdev->priv.health;
44 
45 	return health->fatal_error ? 1 : 0;
46 }
47 
mlx5e_test_link_state(struct mlx5e_priv * priv)48 static int mlx5e_test_link_state(struct mlx5e_priv *priv)
49 {
50 	u8 port_state;
51 
52 	if (!netif_carrier_ok(priv->netdev))
53 		return 1;
54 
55 	port_state = mlx5_query_vport_state(priv->mdev, MLX5_VPORT_STATE_OP_MOD_VNIC_VPORT, 0);
56 	return port_state == VPORT_STATE_UP ? 0 : 1;
57 }
58 
mlx5e_test_link_speed(struct mlx5e_priv * priv)59 static int mlx5e_test_link_speed(struct mlx5e_priv *priv)
60 {
61 	u32 speed;
62 
63 	if (!netif_carrier_ok(priv->netdev))
64 		return 1;
65 
66 	return mlx5e_port_linkspeed(priv->mdev, &speed);
67 }
68 
69 struct mlx5ehdr {
70 	__be32 version;
71 	__be64 magic;
72 };
73 
74 #ifdef CONFIG_INET
75 /* loopback test */
76 #define MLX5E_TEST_PKT_SIZE (sizeof(struct ethhdr) + sizeof(struct iphdr) +\
77 			     sizeof(struct udphdr) + sizeof(struct mlx5ehdr))
78 #define MLX5E_TEST_MAGIC 0x5AEED15C001ULL
79 
mlx5e_test_get_udp_skb(struct mlx5e_priv * priv)80 static struct sk_buff *mlx5e_test_get_udp_skb(struct mlx5e_priv *priv)
81 {
82 	struct sk_buff *skb = NULL;
83 	struct mlx5ehdr *mlxh;
84 	struct ethhdr *ethh;
85 	struct udphdr *udph;
86 	struct iphdr *iph;
87 	int    iplen;
88 
89 	skb = netdev_alloc_skb(priv->netdev, MLX5E_TEST_PKT_SIZE);
90 	if (!skb) {
91 		netdev_err(priv->netdev, "\tFailed to alloc loopback skb\n");
92 		return NULL;
93 	}
94 
95 	net_prefetchw(skb->data);
96 	skb_reserve(skb, NET_IP_ALIGN);
97 
98 	/*  Reserve for ethernet and IP header  */
99 	ethh = skb_push(skb, ETH_HLEN);
100 	skb_reset_mac_header(skb);
101 
102 	skb_set_network_header(skb, skb->len);
103 	iph = skb_put(skb, sizeof(struct iphdr));
104 
105 	skb_set_transport_header(skb, skb->len);
106 	udph = skb_put(skb, sizeof(struct udphdr));
107 
108 	/* Fill ETH header */
109 	ether_addr_copy(ethh->h_dest, priv->netdev->dev_addr);
110 	eth_zero_addr(ethh->h_source);
111 	ethh->h_proto = htons(ETH_P_IP);
112 
113 	/* Fill UDP header */
114 	udph->source = htons(9);
115 	udph->dest = htons(9); /* Discard Protocol */
116 	udph->len = htons(sizeof(struct mlx5ehdr) + sizeof(struct udphdr));
117 	udph->check = 0;
118 
119 	/* Fill IP header */
120 	iph->ihl = 5;
121 	iph->ttl = 32;
122 	iph->version = 4;
123 	iph->protocol = IPPROTO_UDP;
124 	iplen = sizeof(struct iphdr) + sizeof(struct udphdr) +
125 		sizeof(struct mlx5ehdr);
126 	iph->tot_len = htons(iplen);
127 	iph->frag_off = 0;
128 	iph->saddr = 0;
129 	iph->daddr = 0;
130 	iph->tos = 0;
131 	iph->id = 0;
132 	ip_send_check(iph);
133 
134 	/* Fill test header and data */
135 	mlxh = skb_put(skb, sizeof(*mlxh));
136 	mlxh->version = 0;
137 	mlxh->magic = cpu_to_be64(MLX5E_TEST_MAGIC);
138 
139 	skb->csum = 0;
140 	skb->ip_summed = CHECKSUM_PARTIAL;
141 	udp4_hwcsum(skb, iph->saddr, iph->daddr);
142 
143 	skb->protocol = htons(ETH_P_IP);
144 	skb->pkt_type = PACKET_HOST;
145 	skb->dev = priv->netdev;
146 
147 	return skb;
148 }
149 
150 struct mlx5e_lbt_priv {
151 	struct packet_type pt;
152 	struct completion comp;
153 	bool loopback_ok;
154 	bool local_lb;
155 };
156 
157 static int
mlx5e_test_loopback_validate(struct sk_buff * skb,struct net_device * ndev,struct packet_type * pt,struct net_device * orig_ndev)158 mlx5e_test_loopback_validate(struct sk_buff *skb,
159 			     struct net_device *ndev,
160 			     struct packet_type *pt,
161 			     struct net_device *orig_ndev)
162 {
163 	struct mlx5e_lbt_priv *lbtp = pt->af_packet_priv;
164 	struct mlx5ehdr *mlxh;
165 	struct ethhdr *ethh;
166 	struct udphdr *udph;
167 	struct iphdr *iph;
168 
169 	/* We are only going to peek, no need to clone the SKB */
170 	if (MLX5E_TEST_PKT_SIZE - ETH_HLEN > skb_headlen(skb))
171 		goto out;
172 
173 	ethh = (struct ethhdr *)skb_mac_header(skb);
174 	if (!ether_addr_equal(ethh->h_dest, orig_ndev->dev_addr))
175 		goto out;
176 
177 	iph = ip_hdr(skb);
178 	if (iph->protocol != IPPROTO_UDP)
179 		goto out;
180 
181 	/* Don't assume skb_transport_header() was set */
182 	udph = (struct udphdr *)((u8 *)iph + 4 * iph->ihl);
183 	if (udph->dest != htons(9))
184 		goto out;
185 
186 	mlxh = (struct mlx5ehdr *)((char *)udph + sizeof(*udph));
187 	if (mlxh->magic != cpu_to_be64(MLX5E_TEST_MAGIC))
188 		goto out; /* so close ! */
189 
190 	/* bingo */
191 	lbtp->loopback_ok = true;
192 	complete(&lbtp->comp);
193 out:
194 	kfree_skb(skb);
195 	return 0;
196 }
197 
mlx5e_test_loopback_setup(struct mlx5e_priv * priv,struct mlx5e_lbt_priv * lbtp)198 static int mlx5e_test_loopback_setup(struct mlx5e_priv *priv,
199 				     struct mlx5e_lbt_priv *lbtp)
200 {
201 	int err = 0;
202 
203 	/* Temporarily enable local_lb */
204 	err = mlx5_nic_vport_query_local_lb(priv->mdev, &lbtp->local_lb);
205 	if (err)
206 		return err;
207 
208 	if (!lbtp->local_lb) {
209 		err = mlx5_nic_vport_update_local_lb(priv->mdev, true);
210 		if (err)
211 			return err;
212 	}
213 
214 	err = mlx5e_refresh_tirs(priv, true, false);
215 	if (err)
216 		goto out;
217 
218 	lbtp->loopback_ok = false;
219 	init_completion(&lbtp->comp);
220 
221 	lbtp->pt.type = htons(ETH_P_IP);
222 	lbtp->pt.func = mlx5e_test_loopback_validate;
223 	lbtp->pt.dev = priv->netdev;
224 	lbtp->pt.af_packet_priv = lbtp;
225 	dev_add_pack(&lbtp->pt);
226 
227 	return 0;
228 
229 out:
230 	if (!lbtp->local_lb)
231 		mlx5_nic_vport_update_local_lb(priv->mdev, false);
232 
233 	return err;
234 }
235 
mlx5e_test_loopback_cleanup(struct mlx5e_priv * priv,struct mlx5e_lbt_priv * lbtp)236 static void mlx5e_test_loopback_cleanup(struct mlx5e_priv *priv,
237 					struct mlx5e_lbt_priv *lbtp)
238 {
239 	if (!lbtp->local_lb)
240 		mlx5_nic_vport_update_local_lb(priv->mdev, false);
241 
242 	dev_remove_pack(&lbtp->pt);
243 	mlx5e_refresh_tirs(priv, false, false);
244 }
245 
mlx5e_cond_loopback(struct mlx5e_priv * priv)246 static int mlx5e_cond_loopback(struct mlx5e_priv *priv)
247 {
248 	if (is_mdev_switchdev_mode(priv->mdev))
249 		return -EOPNOTSUPP;
250 
251 	if (mlx5_get_sd(priv->mdev))
252 		return -EOPNOTSUPP;
253 
254 	return 0;
255 }
256 
257 #define MLX5E_LB_VERIFY_TIMEOUT (msecs_to_jiffies(200))
mlx5e_test_loopback(struct mlx5e_priv * priv)258 static int mlx5e_test_loopback(struct mlx5e_priv *priv)
259 {
260 	struct mlx5e_lbt_priv *lbtp;
261 	struct sk_buff *skb = NULL;
262 	int err;
263 
264 	if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) {
265 		netdev_err(priv->netdev,
266 			   "\tCan't perform loopback test while device is down\n");
267 		return -ENODEV;
268 	}
269 
270 	lbtp = kzalloc(sizeof(*lbtp), GFP_KERNEL);
271 	if (!lbtp)
272 		return -ENOMEM;
273 	lbtp->loopback_ok = false;
274 
275 	err = mlx5e_test_loopback_setup(priv, lbtp);
276 	if (err)
277 		goto out;
278 
279 	skb = mlx5e_test_get_udp_skb(priv);
280 	if (!skb) {
281 		err = -ENOMEM;
282 		goto cleanup;
283 	}
284 
285 	skb_set_queue_mapping(skb, 0);
286 	err = dev_queue_xmit(skb);
287 	if (err) {
288 		netdev_err(priv->netdev,
289 			   "\tFailed to xmit loopback packet err(%d)\n",
290 			   err);
291 		goto cleanup;
292 	}
293 
294 	wait_for_completion_timeout(&lbtp->comp, MLX5E_LB_VERIFY_TIMEOUT);
295 	err = !lbtp->loopback_ok;
296 
297 cleanup:
298 	mlx5e_test_loopback_cleanup(priv, lbtp);
299 out:
300 	kfree(lbtp);
301 	return err;
302 }
303 #endif
304 
305 typedef int (*mlx5e_st_func)(struct mlx5e_priv *);
306 
307 struct mlx5e_st {
308 	char name[ETH_GSTRING_LEN];
309 	mlx5e_st_func st_func;
310 	mlx5e_st_func cond_func;
311 };
312 
313 static struct mlx5e_st mlx5e_sts[] = {
314 	{ "Link Test", mlx5e_test_link_state },
315 	{ "Speed Test", mlx5e_test_link_speed },
316 	{ "Health Test", mlx5e_test_health_info },
317 #ifdef CONFIG_INET
318 	{ "Loopback Test", mlx5e_test_loopback, mlx5e_cond_loopback },
319 #endif
320 };
321 
322 #define MLX5E_ST_NUM ARRAY_SIZE(mlx5e_sts)
323 
mlx5e_self_test(struct net_device * ndev,struct ethtool_test * etest,u64 * buf)324 void mlx5e_self_test(struct net_device *ndev, struct ethtool_test *etest,
325 		     u64 *buf)
326 {
327 	struct mlx5e_priv *priv = netdev_priv(ndev);
328 	int i, count = 0;
329 
330 	mutex_lock(&priv->state_lock);
331 	netdev_info(ndev, "Self test begin..\n");
332 
333 	for (i = 0; i < MLX5E_ST_NUM; i++) {
334 		struct mlx5e_st st = mlx5e_sts[i];
335 
336 		if (st.cond_func && st.cond_func(priv))
337 			continue;
338 		netdev_info(ndev, "\t[%d] %s start..\n", i, st.name);
339 		buf[count] = st.st_func(priv);
340 		netdev_info(ndev, "\t[%d] %s end: result(%lld)\n", i, st.name, buf[count]);
341 		count++;
342 	}
343 
344 	mutex_unlock(&priv->state_lock);
345 
346 	for (i = 0; i < count; i++) {
347 		if (buf[i]) {
348 			etest->flags |= ETH_TEST_FL_FAILED;
349 			break;
350 		}
351 	}
352 	netdev_info(ndev, "Self test out: status flags(0x%x)\n",
353 		    etest->flags);
354 }
355 
mlx5e_self_test_fill_strings(struct mlx5e_priv * priv,u8 * data)356 int mlx5e_self_test_fill_strings(struct mlx5e_priv *priv, u8 *data)
357 {
358 	int i, count = 0;
359 
360 	for (i = 0; i < MLX5E_ST_NUM; i++) {
361 		struct mlx5e_st st = mlx5e_sts[i];
362 
363 		if (st.cond_func && st.cond_func(priv))
364 			continue;
365 		if (data)
366 			ethtool_puts(&data, st.name);
367 		count++;
368 	}
369 	return count;
370 }
371 
mlx5e_self_test_num(struct mlx5e_priv * priv)372 int mlx5e_self_test_num(struct mlx5e_priv *priv)
373 {
374 	return mlx5e_self_test_fill_strings(priv, NULL);
375 }
376