1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/prefetch.h>
34#include <linux/ip.h>
35#include <linux/udp.h>
36#include <net/udp.h>
37#include "en.h"
38
39enum {
40 MLX5E_ST_LINK_STATE,
41 MLX5E_ST_LINK_SPEED,
42 MLX5E_ST_HEALTH_INFO,
43#ifdef CONFIG_INET
44 MLX5E_ST_LOOPBACK,
45#endif
46 MLX5E_ST_NUM,
47};
48
49const char mlx5e_self_tests[MLX5E_ST_NUM][ETH_GSTRING_LEN] = {
50 "Link Test",
51 "Speed Test",
52 "Health Test",
53#ifdef CONFIG_INET
54 "Loopback Test",
55#endif
56};
57
58int mlx5e_self_test_num(struct mlx5e_priv *priv)
59{
60 return ARRAY_SIZE(mlx5e_self_tests);
61}
62
63static int mlx5e_test_health_info(struct mlx5e_priv *priv)
64{
65 struct mlx5_core_health *health = &priv->mdev->priv.health;
66
67 return health->sick ? 1 : 0;
68}
69
70static int mlx5e_test_link_state(struct mlx5e_priv *priv)
71{
72 u8 port_state;
73
74 if (!netif_carrier_ok(priv->netdev))
75 return 1;
76
77 port_state = mlx5_query_vport_state(priv->mdev, MLX5_QUERY_VPORT_STATE_IN_OP_MOD_VNIC_VPORT, 0);
78 return port_state == VPORT_STATE_UP ? 0 : 1;
79}
80
81static int mlx5e_test_link_speed(struct mlx5e_priv *priv)
82{
83 u32 out[MLX5_ST_SZ_DW(ptys_reg)];
84 u32 eth_proto_oper;
85 int i;
86
87 if (!netif_carrier_ok(priv->netdev))
88 return 1;
89
90 if (mlx5_query_port_ptys(priv->mdev, out, sizeof(out), MLX5_PTYS_EN, 1))
91 return 1;
92
93 eth_proto_oper = MLX5_GET(ptys_reg, out, eth_proto_oper);
94 for (i = 0; i < MLX5E_LINK_MODES_NUMBER; i++) {
95 if (eth_proto_oper & MLX5E_PROT_MASK(i))
96 return 0;
97 }
98 return 1;
99}
100
101#ifdef CONFIG_INET
102
103#define MLX5E_TEST_PKT_SIZE (MLX5_MPWRQ_SMALL_PACKET_THRESHOLD - NET_IP_ALIGN)
104static const char mlx5e_test_text[ETH_GSTRING_LEN] = "MLX5E SELF TEST";
105#define MLX5E_TEST_MAGIC 0x5AEED15C001ULL
106
107struct mlx5ehdr {
108 __be32 version;
109 __be64 magic;
110 char text[ETH_GSTRING_LEN];
111};
112
113static struct sk_buff *mlx5e_test_get_udp_skb(struct mlx5e_priv *priv)
114{
115 struct sk_buff *skb = NULL;
116 struct mlx5ehdr *mlxh;
117 struct ethhdr *ethh;
118 struct udphdr *udph;
119 struct iphdr *iph;
120 int datalen, iplen;
121
122 datalen = MLX5E_TEST_PKT_SIZE -
123 (sizeof(*ethh) + sizeof(*iph) + sizeof(*udph));
124
125 skb = netdev_alloc_skb(priv->netdev, MLX5E_TEST_PKT_SIZE);
126 if (!skb) {
127 netdev_err(priv->netdev, "\tFailed to alloc loopback skb\n");
128 return NULL;
129 }
130
131 prefetchw(skb->data);
132 skb_reserve(skb, NET_IP_ALIGN);
133
134
135 ethh = skb_push(skb, ETH_HLEN);
136 skb_reset_mac_header(skb);
137
138 skb_set_network_header(skb, skb->len);
139 iph = skb_put(skb, sizeof(struct iphdr));
140
141 skb_set_transport_header(skb, skb->len);
142 udph = skb_put(skb, sizeof(struct udphdr));
143
144
145 ether_addr_copy(ethh->h_dest, priv->netdev->dev_addr);
146 eth_zero_addr(ethh->h_source);
147 ethh->h_proto = htons(ETH_P_IP);
148
149
150 udph->source = htons(9);
151 udph->dest = htons(9);
152 udph->len = htons(datalen + sizeof(struct udphdr));
153 udph->check = 0;
154
155
156 iph->ihl = 5;
157 iph->ttl = 32;
158 iph->version = 4;
159 iph->protocol = IPPROTO_UDP;
160 iplen = sizeof(struct iphdr) + sizeof(struct udphdr) + datalen;
161 iph->tot_len = htons(iplen);
162 iph->frag_off = 0;
163 iph->saddr = 0;
164 iph->daddr = 0;
165 iph->tos = 0;
166 iph->id = 0;
167 ip_send_check(iph);
168
169
170 mlxh = skb_put(skb, sizeof(*mlxh));
171 mlxh->version = 0;
172 mlxh->magic = cpu_to_be64(MLX5E_TEST_MAGIC);
173 strlcpy(mlxh->text, mlx5e_test_text, sizeof(mlxh->text));
174 datalen -= sizeof(*mlxh);
175 skb_put_zero(skb, datalen);
176
177 skb->csum = 0;
178 skb->ip_summed = CHECKSUM_PARTIAL;
179 udp4_hwcsum(skb, iph->saddr, iph->daddr);
180
181 skb->protocol = htons(ETH_P_IP);
182 skb->pkt_type = PACKET_HOST;
183 skb->dev = priv->netdev;
184
185 return skb;
186}
187
188struct mlx5e_lbt_priv {
189 struct packet_type pt;
190 struct completion comp;
191 bool loopback_ok;
192 bool local_lb;
193};
194
195static int
196mlx5e_test_loopback_validate(struct sk_buff *skb,
197 struct net_device *ndev,
198 struct packet_type *pt,
199 struct net_device *orig_ndev)
200{
201 struct mlx5e_lbt_priv *lbtp = pt->af_packet_priv;
202 struct mlx5ehdr *mlxh;
203 struct ethhdr *ethh;
204 struct udphdr *udph;
205 struct iphdr *iph;
206
207
208 if (MLX5E_TEST_PKT_SIZE - ETH_HLEN > skb_headlen(skb))
209 goto out;
210
211 ethh = (struct ethhdr *)skb_mac_header(skb);
212 if (!ether_addr_equal(ethh->h_dest, orig_ndev->dev_addr))
213 goto out;
214
215 iph = ip_hdr(skb);
216 if (iph->protocol != IPPROTO_UDP)
217 goto out;
218
219 udph = udp_hdr(skb);
220 if (udph->dest != htons(9))
221 goto out;
222
223 mlxh = (struct mlx5ehdr *)((char *)udph + sizeof(*udph));
224 if (mlxh->magic != cpu_to_be64(MLX5E_TEST_MAGIC))
225 goto out;
226
227
228 lbtp->loopback_ok = true;
229 complete(&lbtp->comp);
230out:
231 kfree_skb(skb);
232 return 0;
233}
234
235static int mlx5e_test_loopback_setup(struct mlx5e_priv *priv,
236 struct mlx5e_lbt_priv *lbtp)
237{
238 int err = 0;
239
240
241 if (MLX5_CAP_GEN(priv->mdev, disable_local_lb)) {
242 mlx5_nic_vport_query_local_lb(priv->mdev, &lbtp->local_lb);
243 if (!lbtp->local_lb)
244 mlx5_nic_vport_update_local_lb(priv->mdev, true);
245 }
246
247 err = mlx5e_refresh_tirs(priv, true);
248 if (err)
249 return err;
250
251 lbtp->loopback_ok = false;
252 init_completion(&lbtp->comp);
253
254 lbtp->pt.type = htons(ETH_P_IP);
255 lbtp->pt.func = mlx5e_test_loopback_validate;
256 lbtp->pt.dev = priv->netdev;
257 lbtp->pt.af_packet_priv = lbtp;
258 dev_add_pack(&lbtp->pt);
259 return err;
260}
261
262static void mlx5e_test_loopback_cleanup(struct mlx5e_priv *priv,
263 struct mlx5e_lbt_priv *lbtp)
264{
265 if (MLX5_CAP_GEN(priv->mdev, disable_local_lb)) {
266 if (!lbtp->local_lb)
267 mlx5_nic_vport_update_local_lb(priv->mdev, false);
268 }
269
270 dev_remove_pack(&lbtp->pt);
271 mlx5e_refresh_tirs(priv, false);
272}
273
274#define MLX5E_LB_VERIFY_TIMEOUT (msecs_to_jiffies(200))
275static int mlx5e_test_loopback(struct mlx5e_priv *priv)
276{
277 struct mlx5e_lbt_priv *lbtp;
278 struct sk_buff *skb = NULL;
279 int err;
280
281 if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) {
282 netdev_err(priv->netdev,
283 "\tCan't perform loobpack test while device is down\n");
284 return -ENODEV;
285 }
286
287 lbtp = kzalloc(sizeof(*lbtp), GFP_KERNEL);
288 if (!lbtp)
289 return -ENOMEM;
290 lbtp->loopback_ok = false;
291
292 err = mlx5e_test_loopback_setup(priv, lbtp);
293 if (err)
294 goto out;
295
296 skb = mlx5e_test_get_udp_skb(priv);
297 if (!skb) {
298 err = -ENOMEM;
299 goto cleanup;
300 }
301
302 skb_set_queue_mapping(skb, 0);
303 err = dev_queue_xmit(skb);
304 if (err) {
305 netdev_err(priv->netdev,
306 "\tFailed to xmit loopback packet err(%d)\n",
307 err);
308 goto cleanup;
309 }
310
311 wait_for_completion_timeout(&lbtp->comp, MLX5E_LB_VERIFY_TIMEOUT);
312 err = !lbtp->loopback_ok;
313
314cleanup:
315 mlx5e_test_loopback_cleanup(priv, lbtp);
316out:
317 kfree(lbtp);
318 return err;
319}
320#endif
321
322static int (*mlx5e_st_func[MLX5E_ST_NUM])(struct mlx5e_priv *) = {
323 mlx5e_test_link_state,
324 mlx5e_test_link_speed,
325 mlx5e_test_health_info,
326#ifdef CONFIG_INET
327 mlx5e_test_loopback,
328#endif
329};
330
331void mlx5e_self_test(struct net_device *ndev, struct ethtool_test *etest,
332 u64 *buf)
333{
334 struct mlx5e_priv *priv = netdev_priv(ndev);
335 int i;
336
337 memset(buf, 0, sizeof(u64) * MLX5E_ST_NUM);
338
339 mutex_lock(&priv->state_lock);
340 netdev_info(ndev, "Self test begin..\n");
341
342 for (i = 0; i < MLX5E_ST_NUM; i++) {
343 netdev_info(ndev, "\t[%d] %s start..\n",
344 i, mlx5e_self_tests[i]);
345 buf[i] = mlx5e_st_func[i](priv);
346 netdev_info(ndev, "\t[%d] %s end: result(%lld)\n",
347 i, mlx5e_self_tests[i], buf[i]);
348 }
349
350 mutex_unlock(&priv->state_lock);
351
352 for (i = 0; i < MLX5E_ST_NUM; i++) {
353 if (buf[i]) {
354 etest->flags |= ETH_TEST_FL_FAILED;
355 break;
356 }
357 }
358 netdev_info(ndev, "Self test out: status flags(0x%x)\n",
359 etest->flags);
360}
361