xref: /linux/drivers/net/ethernet/stmicro/stmmac/stmmac_selftests.c (revision f2c53ea949c5048f96b3dbb5a5ee7131ce4ff2de)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2019 Synopsys, Inc. and/or its affiliates.
4  * stmmac Selftests Support
5  *
6  * Author: Jose Abreu <joabreu@synopsys.com>
7  */
8 
9 #include <linux/bitrev.h>
10 #include <linux/completion.h>
11 #include <linux/crc32.h>
12 #include <linux/ethtool.h>
13 #include <linux/ip.h>
14 #include <linux/udp.h>
15 #include <net/dsa.h>
16 #include <net/pkt_cls.h>
17 #include <net/pkt_sched.h>
18 #include <net/tcp.h>
19 #include <net/udp.h>
20 #include <net/tc_act/tc_gact.h>
21 #include "stmmac.h"
22 
23 struct stmmachdr {
24 	__be32 version;
25 	__be64 magic;
26 	u8 id;
27 } __packed;
28 
29 #define STMMAC_TEST_PKT_SIZE (sizeof(struct ethhdr) + sizeof(struct iphdr) + \
30 			      sizeof(struct stmmachdr))
31 #define STMMAC_TEST_PKT_MAGIC	0xdeadcafecafedeadULL
32 #define STMMAC_LB_TIMEOUT	msecs_to_jiffies(200)
33 #define STMMAC_SFT_MAX_LPI	(5 * USEC_PER_SEC)
34 
35 struct stmmac_packet_attrs {
36 	int vlan;
37 	int vlan_id_in;
38 	int vlan_id_out;
39 	unsigned char *src;
40 	const unsigned char *dst;
41 	u32 ip_src;
42 	u32 ip_dst;
43 	int tcp;
44 	int sport;
45 	int dport;
46 	u32 exp_hash;
47 	int dont_wait;
48 	int timeout;
49 	int size;
50 	int max_size;
51 	int remove_sa;
52 	u8 id;
53 	int sarc;
54 	u16 queue_mapping;
55 	u64 timestamp;
56 };
57 
58 static u8 stmmac_test_next_id;
59 
stmmac_test_get_udp_skb(struct stmmac_priv * priv,struct stmmac_packet_attrs * attr)60 static struct sk_buff *stmmac_test_get_udp_skb(struct stmmac_priv *priv,
61 					       struct stmmac_packet_attrs *attr)
62 {
63 	struct sk_buff *skb = NULL;
64 	struct udphdr *uhdr = NULL;
65 	struct tcphdr *thdr = NULL;
66 	struct stmmachdr *shdr;
67 	struct ethhdr *ehdr;
68 	struct iphdr *ihdr;
69 	int iplen, size;
70 
71 	size = attr->size + STMMAC_TEST_PKT_SIZE;
72 	if (attr->vlan) {
73 		size += 4;
74 		if (attr->vlan > 1)
75 			size += 4;
76 	}
77 
78 	if (attr->tcp)
79 		size += sizeof(struct tcphdr);
80 	else
81 		size += sizeof(struct udphdr);
82 
83 	if (attr->max_size && (attr->max_size > size))
84 		size = attr->max_size;
85 
86 	skb = netdev_alloc_skb(priv->dev, size);
87 	if (!skb)
88 		return NULL;
89 
90 	prefetchw(skb->data);
91 
92 	if (attr->vlan > 1)
93 		ehdr = skb_push(skb, ETH_HLEN + 8);
94 	else if (attr->vlan)
95 		ehdr = skb_push(skb, ETH_HLEN + 4);
96 	else if (attr->remove_sa)
97 		ehdr = skb_push(skb, ETH_HLEN - 6);
98 	else
99 		ehdr = skb_push(skb, ETH_HLEN);
100 	skb_reset_mac_header(skb);
101 
102 	skb_set_network_header(skb, skb->len);
103 	ihdr = skb_put(skb, sizeof(*ihdr));
104 
105 	skb_set_transport_header(skb, skb->len);
106 	if (attr->tcp)
107 		thdr = skb_put(skb, sizeof(*thdr));
108 	else
109 		uhdr = skb_put(skb, sizeof(*uhdr));
110 
111 	if (!attr->remove_sa)
112 		eth_zero_addr(ehdr->h_source);
113 	eth_zero_addr(ehdr->h_dest);
114 	if (attr->src && !attr->remove_sa)
115 		ether_addr_copy(ehdr->h_source, attr->src);
116 	if (attr->dst)
117 		ether_addr_copy(ehdr->h_dest, attr->dst);
118 
119 	if (!attr->remove_sa) {
120 		ehdr->h_proto = htons(ETH_P_IP);
121 	} else {
122 		__be16 *ptr = (__be16 *)ehdr;
123 
124 		/* HACK */
125 		ptr[3] = htons(ETH_P_IP);
126 	}
127 
128 	if (attr->vlan) {
129 		__be16 *tag, *proto;
130 
131 		if (!attr->remove_sa) {
132 			tag = (void *)ehdr + ETH_HLEN;
133 			proto = (void *)ehdr + (2 * ETH_ALEN);
134 		} else {
135 			tag = (void *)ehdr + ETH_HLEN - 6;
136 			proto = (void *)ehdr + ETH_ALEN;
137 		}
138 
139 		proto[0] = htons(ETH_P_8021Q);
140 		tag[0] = htons(attr->vlan_id_out);
141 		tag[1] = htons(ETH_P_IP);
142 		if (attr->vlan > 1) {
143 			proto[0] = htons(ETH_P_8021AD);
144 			tag[1] = htons(ETH_P_8021Q);
145 			tag[2] = htons(attr->vlan_id_in);
146 			tag[3] = htons(ETH_P_IP);
147 		}
148 	}
149 
150 	if (attr->tcp) {
151 		thdr->source = htons(attr->sport);
152 		thdr->dest = htons(attr->dport);
153 		thdr->doff = sizeof(struct tcphdr) / 4;
154 		thdr->check = 0;
155 	} else {
156 		uhdr->source = htons(attr->sport);
157 		uhdr->dest = htons(attr->dport);
158 		udp_set_len_short(uhdr, sizeof(*shdr) + sizeof(*uhdr) + attr->size);
159 		if (attr->max_size)
160 			udp_set_len_short(uhdr, attr->max_size -
161 					  (sizeof(*ihdr) + sizeof(*ehdr)));
162 		uhdr->check = 0;
163 	}
164 
165 	ihdr->ihl = 5;
166 	ihdr->ttl = 32;
167 	ihdr->version = 4;
168 	if (attr->tcp)
169 		ihdr->protocol = IPPROTO_TCP;
170 	else
171 		ihdr->protocol = IPPROTO_UDP;
172 	iplen = sizeof(*ihdr) + sizeof(*shdr) + attr->size;
173 	if (attr->tcp)
174 		iplen += sizeof(*thdr);
175 	else
176 		iplen += sizeof(*uhdr);
177 
178 	if (attr->max_size)
179 		iplen = attr->max_size - sizeof(*ehdr);
180 
181 	ihdr->tot_len = htons(iplen);
182 	ihdr->frag_off = 0;
183 	ihdr->saddr = htonl(attr->ip_src);
184 	ihdr->daddr = htonl(attr->ip_dst);
185 	ihdr->tos = 0;
186 	ihdr->id = 0;
187 	ip_send_check(ihdr);
188 
189 	shdr = skb_put(skb, sizeof(*shdr));
190 	shdr->version = 0;
191 	shdr->magic = cpu_to_be64(STMMAC_TEST_PKT_MAGIC);
192 	attr->id = stmmac_test_next_id;
193 	shdr->id = stmmac_test_next_id++;
194 
195 	if (attr->size)
196 		skb_put(skb, attr->size);
197 	if (attr->max_size && (attr->max_size > skb->len))
198 		skb_put(skb, attr->max_size - skb->len);
199 
200 	skb->csum = 0;
201 	skb->ip_summed = CHECKSUM_PARTIAL;
202 	if (attr->tcp) {
203 		thdr->check = ~tcp_v4_check(skb->len, ihdr->saddr, ihdr->daddr, 0);
204 		skb->csum_start = skb_transport_header(skb) - skb->head;
205 		skb->csum_offset = offsetof(struct tcphdr, check);
206 	} else {
207 		udp4_hwcsum(skb, ihdr->saddr, ihdr->daddr);
208 	}
209 
210 	skb->protocol = htons(ETH_P_IP);
211 	skb->pkt_type = PACKET_HOST;
212 	skb->dev = priv->dev;
213 
214 	if (attr->timestamp)
215 		skb->tstamp = ns_to_ktime(attr->timestamp);
216 
217 	return skb;
218 }
219 
stmmac_test_get_arp_skb(struct stmmac_priv * priv,struct stmmac_packet_attrs * attr)220 static struct sk_buff *stmmac_test_get_arp_skb(struct stmmac_priv *priv,
221 					       struct stmmac_packet_attrs *attr)
222 {
223 	__be32 ip_src = htonl(attr->ip_src);
224 	__be32 ip_dst = htonl(attr->ip_dst);
225 	struct sk_buff *skb = NULL;
226 
227 	skb = arp_create(ARPOP_REQUEST, ETH_P_ARP, ip_dst, priv->dev, ip_src,
228 			 NULL, attr->src, attr->dst);
229 	if (!skb)
230 		return NULL;
231 
232 	skb->pkt_type = PACKET_HOST;
233 	skb->dev = priv->dev;
234 
235 	return skb;
236 }
237 
238 struct stmmac_test_priv {
239 	struct stmmac_packet_attrs *packet;
240 	struct packet_type pt;
241 	struct completion comp;
242 	__be16 packet_type;
243 	int (*func)(struct sk_buff *skb, struct net_device *ndev,
244 		    struct packet_type *pt, struct net_device *orig_ndev);
245 	bool capture_all;
246 	int double_vlan;
247 	int vlan_id;
248 	int ok;
249 };
250 
stmmac_test_loopback_validate(struct sk_buff * skb,struct net_device * ndev,struct packet_type * pt,struct net_device * orig_ndev)251 static int stmmac_test_loopback_validate(struct sk_buff *skb,
252 					 struct net_device *ndev,
253 					 struct packet_type *pt,
254 					 struct net_device *orig_ndev)
255 {
256 	struct stmmac_test_priv *tpriv = pt->af_packet_priv;
257 	const unsigned char *dst = tpriv->packet->dst;
258 	unsigned char *src = tpriv->packet->src;
259 	struct stmmachdr *shdr;
260 	struct ethhdr *ehdr;
261 	struct udphdr *uhdr;
262 	struct tcphdr *thdr;
263 	struct iphdr *ihdr;
264 
265 	skb = skb_unshare(skb, GFP_ATOMIC);
266 	if (!skb)
267 		goto out;
268 
269 	if (skb_linearize(skb))
270 		goto out;
271 	if (skb_headlen(skb) < (STMMAC_TEST_PKT_SIZE - ETH_HLEN))
272 		goto out;
273 
274 	ehdr = (struct ethhdr *)skb_mac_header(skb);
275 	if (dst) {
276 		if (!ether_addr_equal_unaligned(ehdr->h_dest, dst))
277 			goto out;
278 	}
279 	if (tpriv->packet->sarc) {
280 		if (!ether_addr_equal_unaligned(ehdr->h_source, ehdr->h_dest))
281 			goto out;
282 	} else if (src) {
283 		if (!ether_addr_equal_unaligned(ehdr->h_source, src))
284 			goto out;
285 	}
286 
287 	ihdr = ip_hdr(skb);
288 	if (tpriv->double_vlan)
289 		ihdr = (struct iphdr *)(skb_network_header(skb) + 4);
290 
291 	if (tpriv->packet->tcp) {
292 		if (ihdr->protocol != IPPROTO_TCP)
293 			goto out;
294 
295 		thdr = (struct tcphdr *)((u8 *)ihdr + 4 * ihdr->ihl);
296 		if (thdr->dest != htons(tpriv->packet->dport))
297 			goto out;
298 
299 		shdr = (struct stmmachdr *)((u8 *)thdr + sizeof(*thdr));
300 	} else {
301 		if (ihdr->protocol != IPPROTO_UDP)
302 			goto out;
303 
304 		uhdr = (struct udphdr *)((u8 *)ihdr + 4 * ihdr->ihl);
305 		if (uhdr->dest != htons(tpriv->packet->dport))
306 			goto out;
307 
308 		shdr = (struct stmmachdr *)((u8 *)uhdr + sizeof(*uhdr));
309 	}
310 
311 	if (shdr->magic != cpu_to_be64(STMMAC_TEST_PKT_MAGIC))
312 		goto out;
313 	if (tpriv->packet->exp_hash && !skb->hash)
314 		goto out;
315 	if (tpriv->packet->id != shdr->id)
316 		goto out;
317 
318 	tpriv->ok = true;
319 	complete(&tpriv->comp);
320 out:
321 	kfree_skb(skb);
322 	return 0;
323 }
324 
stmmac_sft_filter(struct sk_buff * skb,struct net_device * ndev,struct packet_type * pt,struct net_device * orig_ndev)325 static int stmmac_sft_filter(struct sk_buff *skb, struct net_device *ndev,
326 			     struct packet_type *pt,
327 			     struct net_device *orig_ndev)
328 {
329 	struct stmmac_test_priv *tpriv = pt->af_packet_priv;
330 	struct ethhdr *hdr = eth_hdr(skb);
331 	int ret = 0;
332 
333 	if (hdr->h_proto == tpriv->packet_type) {
334 		struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
335 
336 		if (nskb)
337 			ret = tpriv->func(nskb, ndev, pt, orig_ndev);
338 	}
339 
340 	kfree_skb(skb);
341 	return ret;
342 }
343 
stmmac_sft_add_pack(struct packet_type * pt)344 static void stmmac_sft_add_pack(struct packet_type *pt)
345 {
346 	struct stmmac_test_priv *tpriv = pt->af_packet_priv;
347 
348 	if (netdev_uses_dsa(tpriv->pt.dev) || tpriv->capture_all) {
349 		tpriv->packet_type = tpriv->pt.type;
350 		tpriv->func = tpriv->pt.func;
351 
352 		/* DSA conduit will report ETH_P_XDSA, so our packet handler
353 		 * won't match. Let's register a ETH_P_ALL match and filter
354 		 * manually in stmmac_sft_filter. This is also useful for
355 		 * VLAN tests, to capture packets otherwise marked as
356 		 * OTHERHOST.
357 		 */
358 		tpriv->pt.type = htons(ETH_P_ALL);
359 		tpriv->pt.func = stmmac_sft_filter;
360 		tpriv->pt.ignore_outgoing = true;
361 	}
362 
363 	dev_add_pack(pt);
364 }
365 
stmmac_sft_remove_pack(struct packet_type * pt)366 static void stmmac_sft_remove_pack(struct packet_type *pt)
367 {
368 	dev_remove_pack(pt);
369 }
370 
__stmmac_test_loopback(struct stmmac_priv * priv,struct stmmac_packet_attrs * attr)371 static int __stmmac_test_loopback(struct stmmac_priv *priv,
372 				  struct stmmac_packet_attrs *attr)
373 {
374 	struct stmmac_test_priv *tpriv;
375 	struct sk_buff *skb = NULL;
376 	int ret = 0;
377 
378 	tpriv = kzalloc_obj(*tpriv);
379 	if (!tpriv)
380 		return -ENOMEM;
381 
382 	tpriv->ok = false;
383 	init_completion(&tpriv->comp);
384 
385 	tpriv->pt.type = htons(ETH_P_IP);
386 	tpriv->pt.func = stmmac_test_loopback_validate;
387 	tpriv->pt.dev = priv->dev;
388 	tpriv->pt.af_packet_priv = tpriv;
389 	tpriv->packet = attr;
390 
391 	if (!attr->dont_wait)
392 		stmmac_sft_add_pack(&tpriv->pt);
393 
394 	skb = stmmac_test_get_udp_skb(priv, attr);
395 	if (!skb) {
396 		ret = -ENOMEM;
397 		goto cleanup;
398 	}
399 
400 	ret = dev_direct_xmit(skb, attr->queue_mapping);
401 	if (ret)
402 		goto cleanup;
403 
404 	if (attr->dont_wait)
405 		goto cleanup;
406 
407 	if (!attr->timeout)
408 		attr->timeout = STMMAC_LB_TIMEOUT;
409 
410 	wait_for_completion_timeout(&tpriv->comp, attr->timeout);
411 	ret = tpriv->ok ? 0 : -ETIMEDOUT;
412 
413 cleanup:
414 	if (!attr->dont_wait)
415 		stmmac_sft_remove_pack(&tpriv->pt);
416 	kfree(tpriv);
417 	return ret;
418 }
419 
stmmac_test_mac_loopback(struct stmmac_priv * priv)420 static int stmmac_test_mac_loopback(struct stmmac_priv *priv)
421 {
422 	struct stmmac_packet_attrs attr = { };
423 
424 	attr.dst = priv->dev->dev_addr;
425 	return __stmmac_test_loopback(priv, &attr);
426 }
427 
stmmac_test_mmc(struct stmmac_priv * priv)428 static int stmmac_test_mmc(struct stmmac_priv *priv)
429 {
430 	struct stmmac_counters initial, final;
431 	int ret;
432 
433 	memset(&initial, 0, sizeof(initial));
434 	memset(&final, 0, sizeof(final));
435 
436 	if (!priv->dma_cap.rmon)
437 		return -EOPNOTSUPP;
438 
439 	/* Save previous results into internal struct */
440 	stmmac_mmc_read(priv, priv->mmcaddr, &priv->mmc);
441 
442 	ret = stmmac_test_mac_loopback(priv);
443 	if (ret)
444 		return ret;
445 
446 	/* These will be loopback results so no need to save them */
447 	stmmac_mmc_read(priv, priv->mmcaddr, &final);
448 
449 	/*
450 	 * The number of MMC counters available depends on HW configuration,
451 	 * and there doesn't seem to be a way to enumerate the implemented
452 	 * counters.
453 	 *
454 	 * Let's check a hand-picked set of counters, knowing that :
455 	 *  - Starfive JH7110 doesn't implement mmc_tx_framecount_g
456 	 *  - Amlogic SM1 doesn't implement any mmc_tx_*
457 	 *
458 	 */
459 	if (final.mmc_tx_framecount_g <= initial.mmc_tx_framecount_g &&
460 	    final.mmc_rx_framecount_gb <= initial.mmc_rx_framecount_gb)
461 		return -EINVAL;
462 
463 	return 0;
464 }
465 
stmmac_test_eee(struct stmmac_priv * priv)466 static int stmmac_test_eee(struct stmmac_priv *priv)
467 {
468 	struct stmmac_extra_stats *initial, *final;
469 	unsigned long timeout, max_duration;
470 	int ret;
471 
472 	if (!priv->dma_cap.eee || !priv->eee_active)
473 		return -EOPNOTSUPP;
474 
475 	/* Bail out if the configured LPI timer is too long */
476 	if (priv->tx_lpi_timer > STMMAC_SFT_MAX_LPI)
477 		return -EOPNOTSUPP;
478 
479 	initial = kzalloc_obj(*initial);
480 	if (!initial)
481 		return -ENOMEM;
482 
483 	final = kzalloc_obj(*final);
484 	if (!final) {
485 		ret = -ENOMEM;
486 		goto out_free_initial;
487 	}
488 
489 	/* Snapshot stats, we want to count the in_lpi events. We may enter
490 	 * LPI just after the packet was sent.
491 	 */
492 	memcpy(initial, &priv->xstats, sizeof(*initial));
493 
494 	/* Send a frame, then wait to enter LPI */
495 	ret = stmmac_test_mac_loopback(priv);
496 	if (ret)
497 		goto out_free_final;
498 
499 	max_duration = usecs_to_jiffies(2 * priv->tx_lpi_timer);
500 
501 	/* We have no traffic in the line so, sooner or later it will go LPI */
502 	timeout = jiffies + max_duration;
503 	while (!time_after(jiffies, timeout)) {
504 		memcpy(final, &priv->xstats, sizeof(*final));
505 
506 		if (final->irq_tx_path_in_lpi_mode_n >
507 		    initial->irq_tx_path_in_lpi_mode_n)
508 			break;
509 		msleep(100);
510 	}
511 
512 	memcpy(final, &priv->xstats, sizeof(*final));
513 	if (final->irq_tx_path_in_lpi_mode_n <=
514 	    initial->irq_tx_path_in_lpi_mode_n) {
515 		ret = -ETIMEDOUT;
516 		goto out_free_final;
517 	}
518 
519 	/* Re-snapshot, as we want to measure exit_lpi events. We should be
520 	 * in LPI right now.
521 	 */
522 	memcpy(initial, &priv->xstats, sizeof(*initial));
523 
524 	/* TX something so we go out of LPI */
525 	ret = stmmac_test_mac_loopback(priv);
526 	if (ret)
527 		goto out_free_final;
528 
529 	/* Wait for the exit LPI interrupt */
530 	timeout = jiffies + max_duration;
531 	while (!time_after(jiffies, timeout)) {
532 		memcpy(final, &priv->xstats, sizeof(*final));
533 
534 		if (final->irq_tx_path_exit_lpi_mode_n >
535 		    initial->irq_tx_path_exit_lpi_mode_n)
536 			break;
537 		msleep(100);
538 	}
539 
540 	memcpy(final, &priv->xstats, sizeof(*final));
541 	if (final->irq_tx_path_exit_lpi_mode_n <=
542 	    initial->irq_tx_path_exit_lpi_mode_n) {
543 		ret = -ETIMEDOUT;
544 		goto out_free_final;
545 	}
546 
547 out_free_final:
548 	kfree(final);
549 out_free_initial:
550 	kfree(initial);
551 	return ret;
552 }
553 
stmmac_filter_check(struct stmmac_priv * priv)554 static int stmmac_filter_check(struct stmmac_priv *priv)
555 {
556 	if (!(priv->dev->flags & IFF_PROMISC))
557 		return 0;
558 
559 	netdev_warn(priv->dev, "Test can't be run in promiscuous mode!\n");
560 	return -EOPNOTSUPP;
561 }
562 
stmmac_uc_filter_check(struct stmmac_priv * priv)563 static int stmmac_uc_filter_check(struct stmmac_priv *priv)
564 {
565 	/* For tests involving the UC filter, we need at least one empty
566 	 * slot in the UC filter. The UC filters contains netdev_uc_count() + 1
567 	 * entries: The dev->uc list + one entry for the HW address.
568 	 *
569 	 * Having an empty slot therefore means netdev_uc_count() + 2 entries
570 	 * can fit in the filter
571 	 */
572 	if (netdev_uc_count(priv->dev) + 2 > priv->hw->unicast_filter_entries)
573 		return -EOPNOTSUPP;
574 
575 	return 0;
576 }
577 
stmmac_hash_check(struct stmmac_priv * priv,unsigned char * addr)578 static bool stmmac_hash_check(struct stmmac_priv *priv, unsigned char *addr)
579 {
580 	int mc_offset = 32 - priv->hw->mcast_bits_log2;
581 	struct netdev_hw_addr *ha;
582 	u32 hash, hash_nr;
583 
584 	/* First compute the hash for desired addr */
585 	hash = bitrev32(~crc32_le(~0, addr, 6)) >> mc_offset;
586 	hash_nr = hash >> 5;
587 	hash = 1 << (hash & 0x1f);
588 
589 	/* Now, check if it collides with any existing one */
590 	netdev_for_each_mc_addr(ha, priv->dev) {
591 		u32 nr = bitrev32(~crc32_le(~0, ha->addr, ETH_ALEN)) >> mc_offset;
592 		if (((nr >> 5) == hash_nr) && ((1 << (nr & 0x1f)) == hash))
593 			return false;
594 	}
595 
596 	/* No collisions, address is good to go */
597 	return true;
598 }
599 
stmmac_perfect_check(struct stmmac_priv * priv,unsigned char * addr)600 static bool stmmac_perfect_check(struct stmmac_priv *priv, unsigned char *addr)
601 {
602 	struct netdev_hw_addr *ha;
603 
604 	/* Check if it collides with any existing one */
605 	netdev_for_each_uc_addr(ha, priv->dev) {
606 		if (!memcmp(ha->addr, addr, ETH_ALEN))
607 			return false;
608 	}
609 
610 	/* No collisions, address is good to go */
611 	return true;
612 }
613 
stmmac_test_hfilt(struct stmmac_priv * priv)614 static int stmmac_test_hfilt(struct stmmac_priv *priv)
615 {
616 	unsigned char gd_addr[ETH_ALEN] = {0xf1, 0xee, 0xdd, 0xcc, 0xbb, 0xaa};
617 	unsigned char bd_addr[ETH_ALEN] = {0xf1, 0xff, 0xff, 0xff, 0xff, 0xff};
618 	struct stmmac_packet_attrs attr = { };
619 	int ret, tries = 256;
620 
621 	ret = stmmac_filter_check(priv);
622 	if (ret)
623 		return ret;
624 
625 	if (netdev_mc_count(priv->dev) >= priv->hw->multicast_filter_bins)
626 		return -EOPNOTSUPP;
627 
628 	while (--tries) {
629 		/* We only need to check the bd_addr for collisions */
630 		bd_addr[ETH_ALEN - 1] = tries;
631 		if (stmmac_hash_check(priv, bd_addr))
632 			break;
633 	}
634 
635 	if (!tries)
636 		return -EOPNOTSUPP;
637 
638 	ret = dev_mc_add(priv->dev, gd_addr);
639 	if (ret)
640 		return ret;
641 
642 	attr.dst = gd_addr;
643 
644 	/* Shall receive packet */
645 	ret = __stmmac_test_loopback(priv, &attr);
646 	if (ret)
647 		goto cleanup;
648 
649 	attr.dst = bd_addr;
650 
651 	/* Shall NOT receive packet */
652 	ret = __stmmac_test_loopback(priv, &attr);
653 	ret = ret ? 0 : -EINVAL;
654 
655 cleanup:
656 	dev_mc_del(priv->dev, gd_addr);
657 	return ret;
658 }
659 
stmmac_test_pfilt(struct stmmac_priv * priv)660 static int stmmac_test_pfilt(struct stmmac_priv *priv)
661 {
662 	unsigned char gd_addr[ETH_ALEN] = {0xf0, 0x01, 0x44, 0x55, 0x66, 0x77};
663 	unsigned char bd_addr[ETH_ALEN] = {0xf0, 0xff, 0xff, 0xff, 0xff, 0xff};
664 	struct stmmac_packet_attrs attr = { };
665 	int ret, tries = 256;
666 
667 	if (stmmac_filter_check(priv))
668 		return -EOPNOTSUPP;
669 	if (stmmac_uc_filter_check(priv))
670 		return -EOPNOTSUPP;
671 
672 	while (--tries) {
673 		/* We only need to check the bd_addr for collisions */
674 		bd_addr[ETH_ALEN - 1] = tries;
675 		if (stmmac_perfect_check(priv, bd_addr))
676 			break;
677 	}
678 
679 	if (!tries)
680 		return -EOPNOTSUPP;
681 
682 	ret = dev_uc_add(priv->dev, gd_addr);
683 	if (ret)
684 		return ret;
685 
686 	attr.dst = gd_addr;
687 
688 	/* Shall receive packet */
689 	ret = __stmmac_test_loopback(priv, &attr);
690 	if (ret)
691 		goto cleanup;
692 
693 	attr.dst = bd_addr;
694 
695 	/* Shall NOT receive packet */
696 	ret = __stmmac_test_loopback(priv, &attr);
697 	ret = ret ? 0 : -EINVAL;
698 
699 cleanup:
700 	dev_uc_del(priv->dev, gd_addr);
701 	return ret;
702 }
703 
stmmac_test_mcfilt(struct stmmac_priv * priv)704 static int stmmac_test_mcfilt(struct stmmac_priv *priv)
705 {
706 	unsigned char uc_addr[ETH_ALEN] = {0xf0, 0xff, 0xff, 0xff, 0xff, 0xff};
707 	unsigned char mc_addr[ETH_ALEN] = {0xf1, 0xff, 0xff, 0xff, 0xff, 0xff};
708 	struct stmmac_packet_attrs attr = { };
709 	int ret, tries = 256;
710 
711 	if (stmmac_filter_check(priv))
712 		return -EOPNOTSUPP;
713 	if (stmmac_uc_filter_check(priv))
714 		return -EOPNOTSUPP;
715 	if (netdev_mc_count(priv->dev) >= priv->hw->multicast_filter_bins)
716 		return -EOPNOTSUPP;
717 
718 	while (--tries) {
719 		/* We only need to check the mc_addr for collisions */
720 		mc_addr[ETH_ALEN - 1] = tries;
721 		if (stmmac_hash_check(priv, mc_addr))
722 			break;
723 	}
724 
725 	if (!tries)
726 		return -EOPNOTSUPP;
727 
728 	ret = dev_uc_add(priv->dev, uc_addr);
729 	if (ret)
730 		return ret;
731 
732 	attr.dst = uc_addr;
733 
734 	/* Shall receive packet */
735 	ret = __stmmac_test_loopback(priv, &attr);
736 	if (ret)
737 		goto cleanup;
738 
739 	attr.dst = mc_addr;
740 
741 	/* Shall NOT receive packet */
742 	ret = __stmmac_test_loopback(priv, &attr);
743 	ret = ret ? 0 : -EINVAL;
744 
745 cleanup:
746 	dev_uc_del(priv->dev, uc_addr);
747 	return ret;
748 }
749 
stmmac_test_ucfilt(struct stmmac_priv * priv)750 static int stmmac_test_ucfilt(struct stmmac_priv *priv)
751 {
752 	unsigned char uc_addr[ETH_ALEN] = {0xf0, 0xff, 0xff, 0xff, 0xff, 0xff};
753 	unsigned char mc_addr[ETH_ALEN] = {0xf1, 0xff, 0xff, 0xff, 0xff, 0xff};
754 	struct stmmac_packet_attrs attr = { };
755 	int ret, tries = 256;
756 
757 	if (stmmac_filter_check(priv))
758 		return -EOPNOTSUPP;
759 	if (stmmac_uc_filter_check(priv))
760 		return -EOPNOTSUPP;
761 	if (netdev_mc_count(priv->dev) >= priv->hw->multicast_filter_bins)
762 		return -EOPNOTSUPP;
763 
764 	while (--tries) {
765 		/* We only need to check the uc_addr for collisions */
766 		uc_addr[ETH_ALEN - 1] = tries;
767 		if (stmmac_perfect_check(priv, uc_addr))
768 			break;
769 	}
770 
771 	if (!tries)
772 		return -EOPNOTSUPP;
773 
774 	ret = dev_mc_add(priv->dev, mc_addr);
775 	if (ret)
776 		return ret;
777 
778 	attr.dst = mc_addr;
779 
780 	/* Shall receive packet */
781 	ret = __stmmac_test_loopback(priv, &attr);
782 	if (ret)
783 		goto cleanup;
784 
785 	attr.dst = uc_addr;
786 
787 	/* Shall NOT receive packet */
788 	ret = __stmmac_test_loopback(priv, &attr);
789 	ret = ret ? 0 : -EINVAL;
790 
791 cleanup:
792 	dev_mc_del(priv->dev, mc_addr);
793 	return ret;
794 }
795 
stmmac_test_flowctrl_validate(struct sk_buff * skb,struct net_device * ndev,struct packet_type * pt,struct net_device * orig_ndev)796 static int stmmac_test_flowctrl_validate(struct sk_buff *skb,
797 					 struct net_device *ndev,
798 					 struct packet_type *pt,
799 					 struct net_device *orig_ndev)
800 {
801 	struct stmmac_test_priv *tpriv = pt->af_packet_priv;
802 	struct ethhdr *ehdr;
803 
804 	ehdr = (struct ethhdr *)skb_mac_header(skb);
805 	if (!ether_addr_equal_unaligned(ehdr->h_source, orig_ndev->dev_addr))
806 		goto out;
807 	if (ehdr->h_proto != htons(ETH_P_PAUSE))
808 		goto out;
809 
810 	tpriv->ok = true;
811 	complete(&tpriv->comp);
812 out:
813 	kfree_skb(skb);
814 	return 0;
815 }
816 
stmmac_test_flowctrl(struct stmmac_priv * priv)817 static int stmmac_test_flowctrl(struct stmmac_priv *priv)
818 {
819 	unsigned char paddr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x01};
820 	u32 rx_cnt = priv->plat->rx_queues_to_use;
821 	struct mac_device_info *mac = priv->hw;
822 	struct stmmac_test_priv *tpriv;
823 	unsigned int rx_fifo_size;
824 	unsigned int pkt_count;
825 	int i, ret = 0;
826 
827 	if (!(mac->link.caps & MAC_SYM_PAUSE))
828 		return -EOPNOTSUPP;
829 
830 	rx_fifo_size = priv->plat->rx_fifo_size;
831 	if (!rx_fifo_size)
832 		rx_fifo_size = priv->dma_cap.rx_fifo_size;
833 
834 	/* No pause frame is emitted if we don't have at least 4096 bytes per
835 	 * queue, except on dwmac100.
836 	 */
837 	if (priv->plat->core_type != DWMAC_CORE_MAC100 &&
838 	    rx_fifo_size / priv->plat->rx_queues_to_use < 4096)
839 		return -EOPNOTSUPP;
840 
841 	tpriv = kzalloc_obj(*tpriv);
842 	if (!tpriv)
843 		return -ENOMEM;
844 
845 	tpriv->ok = false;
846 	init_completion(&tpriv->comp);
847 	tpriv->pt.type = htons(ETH_P_PAUSE);
848 	tpriv->pt.func = stmmac_test_flowctrl_validate;
849 	tpriv->pt.dev = priv->dev;
850 	tpriv->pt.af_packet_priv = tpriv;
851 	stmmac_sft_add_pack(&tpriv->pt);
852 
853 	/* Compute minimum number of packets to make FIFO full */
854 	pkt_count = rx_fifo_size;
855 	pkt_count /= 1400;
856 	pkt_count *= 2;
857 
858 	for (i = 0; i < rx_cnt; i++)
859 		stmmac_stop_rx(priv, priv->ioaddr, i);
860 
861 	ret = dev_set_promiscuity(priv->dev, 1);
862 	if (ret)
863 		goto cleanup;
864 
865 	ret = dev_mc_add(priv->dev, paddr);
866 	if (ret)
867 		goto cleanup;
868 
869 	for (i = 0; i < pkt_count; i++) {
870 		struct stmmac_packet_attrs attr = { };
871 
872 		attr.dst = priv->dev->dev_addr;
873 		attr.dont_wait = true;
874 		attr.size = 1400;
875 
876 		ret = __stmmac_test_loopback(priv, &attr);
877 		if (ret)
878 			goto cleanup;
879 		if (tpriv->ok)
880 			break;
881 	}
882 
883 	/* Wait for some time in case RX Watchdog is enabled */
884 	msleep(200);
885 
886 	for (i = 0; i < rx_cnt; i++) {
887 		struct stmmac_channel *ch = &priv->channel[i];
888 		u32 tail;
889 
890 		tail = priv->dma_conf.rx_queue[i].dma_rx_phy +
891 			(priv->dma_conf.dma_rx_size * sizeof(struct dma_desc));
892 
893 		stmmac_set_rx_tail_ptr(priv, priv->ioaddr, tail, i);
894 		stmmac_start_rx(priv, priv->ioaddr, i);
895 
896 		local_bh_disable();
897 		napi_schedule(&ch->rx_napi);
898 		local_bh_enable();
899 	}
900 
901 	wait_for_completion_timeout(&tpriv->comp, STMMAC_LB_TIMEOUT);
902 	ret = tpriv->ok ? 0 : -ETIMEDOUT;
903 
904 cleanup:
905 	dev_mc_del(priv->dev, paddr);
906 	dev_set_promiscuity(priv->dev, -1);
907 	stmmac_sft_remove_pack(&tpriv->pt);
908 	kfree(tpriv);
909 	return ret;
910 }
911 
stmmac_test_rss(struct stmmac_priv * priv)912 static int stmmac_test_rss(struct stmmac_priv *priv)
913 {
914 	struct stmmac_packet_attrs attr = { };
915 
916 	if (!priv->dma_cap.rssen || !priv->rss.enable)
917 		return -EOPNOTSUPP;
918 
919 	attr.dst = priv->dev->dev_addr;
920 	attr.exp_hash = true;
921 	attr.sport = 0x321;
922 	attr.dport = 0x123;
923 
924 	return __stmmac_test_loopback(priv, &attr);
925 }
926 
stmmac_test_vlan_validate(struct sk_buff * skb,struct net_device * ndev,struct packet_type * pt,struct net_device * orig_ndev)927 static int stmmac_test_vlan_validate(struct sk_buff *skb,
928 				     struct net_device *ndev,
929 				     struct packet_type *pt,
930 				     struct net_device *orig_ndev)
931 {
932 	struct stmmac_test_priv *tpriv = pt->af_packet_priv;
933 	struct stmmachdr *shdr;
934 	struct ethhdr *ehdr;
935 	struct udphdr *uhdr;
936 	struct iphdr *ihdr;
937 	u16 proto;
938 
939 	proto = tpriv->double_vlan ? ETH_P_8021AD : ETH_P_8021Q;
940 
941 	skb = skb_unshare(skb, GFP_ATOMIC);
942 	if (!skb)
943 		goto out;
944 
945 	if (skb_linearize(skb))
946 		goto out;
947 	if (skb_headlen(skb) < (STMMAC_TEST_PKT_SIZE - ETH_HLEN))
948 		goto out;
949 
950 	ehdr = (struct ethhdr *)skb_mac_header(skb);
951 	if (!ether_addr_equal_unaligned(ehdr->h_dest, tpriv->packet->dst))
952 		goto out;
953 
954 	if (tpriv->vlan_id) {
955 		if (skb->vlan_proto != htons(proto))
956 			goto out;
957 		if (skb->vlan_tci != tpriv->vlan_id) {
958 			/* Means filter did not work. */
959 			tpriv->ok = false;
960 			complete(&tpriv->comp);
961 			goto out;
962 		}
963 	}
964 
965 	ihdr = ip_hdr(skb);
966 	if (tpriv->double_vlan)
967 		ihdr = (struct iphdr *)(skb_network_header(skb) + 4);
968 	if (ihdr->protocol != IPPROTO_UDP)
969 		goto out;
970 
971 	uhdr = (struct udphdr *)((u8 *)ihdr + 4 * ihdr->ihl);
972 	if (uhdr->dest != htons(tpriv->packet->dport))
973 		goto out;
974 
975 	shdr = (struct stmmachdr *)((u8 *)uhdr + sizeof(*uhdr));
976 	if (shdr->magic != cpu_to_be64(STMMAC_TEST_PKT_MAGIC))
977 		goto out;
978 
979 	tpriv->ok = true;
980 	complete(&tpriv->comp);
981 
982 out:
983 	kfree_skb(skb);
984 	return 0;
985 }
986 
__stmmac_test_vlanfilt(struct stmmac_priv * priv)987 static int __stmmac_test_vlanfilt(struct stmmac_priv *priv)
988 {
989 	struct stmmac_packet_attrs attr = { };
990 	struct stmmac_test_priv *tpriv;
991 	struct sk_buff *skb = NULL;
992 	int ret = 0, i;
993 
994 	tpriv = kzalloc_obj(*tpriv);
995 	if (!tpriv)
996 		return -ENOMEM;
997 
998 	tpriv->ok = false;
999 	init_completion(&tpriv->comp);
1000 
1001 	tpriv->pt.type = htons(ETH_P_IP);
1002 	tpriv->pt.func = stmmac_test_vlan_validate;
1003 	tpriv->pt.dev = priv->dev;
1004 	tpriv->pt.af_packet_priv = tpriv;
1005 	tpriv->packet = &attr;
1006 	tpriv->capture_all = true;
1007 
1008 	/*
1009 	 * As we use HASH filtering, false positives may appear. This is a
1010 	 * specially chosen ID so that adjacent IDs (+4) have different
1011 	 * HASH values.
1012 	 */
1013 	tpriv->vlan_id = 0x123;
1014 
1015 	ret = vlan_vid_add(priv->dev, htons(ETH_P_8021Q), tpriv->vlan_id);
1016 	if (ret)
1017 		goto cleanup;
1018 
1019 	attr.vlan = 1;
1020 	attr.dst = priv->dev->dev_addr;
1021 	attr.sport = 9;
1022 	attr.dport = 9;
1023 
1024 	stmmac_sft_add_pack(&tpriv->pt);
1025 
1026 	for (i = 0; i < 4; i++) {
1027 		attr.vlan_id_out = tpriv->vlan_id + i;
1028 
1029 		skb = stmmac_test_get_udp_skb(priv, &attr);
1030 		if (!skb) {
1031 			ret = -ENOMEM;
1032 			goto vlan_del;
1033 		}
1034 
1035 		ret = dev_direct_xmit(skb, 0);
1036 		if (ret)
1037 			goto vlan_del;
1038 
1039 		wait_for_completion_timeout(&tpriv->comp, STMMAC_LB_TIMEOUT);
1040 		ret = tpriv->ok ? 0 : -ETIMEDOUT;
1041 		if (ret && !i) {
1042 			goto vlan_del;
1043 		} else if (!ret && i) {
1044 			ret = -EINVAL;
1045 			goto vlan_del;
1046 		} else {
1047 			ret = 0;
1048 		}
1049 
1050 		tpriv->ok = false;
1051 	}
1052 
1053 vlan_del:
1054 	stmmac_sft_remove_pack(&tpriv->pt);
1055 	vlan_vid_del(priv->dev, htons(ETH_P_8021Q), tpriv->vlan_id);
1056 cleanup:
1057 	kfree(tpriv);
1058 	return ret;
1059 }
1060 
stmmac_test_vlanfilt(struct stmmac_priv * priv)1061 static int stmmac_test_vlanfilt(struct stmmac_priv *priv)
1062 {
1063 	if (!priv->dma_cap.vlhash)
1064 		return -EOPNOTSUPP;
1065 
1066 	return __stmmac_test_vlanfilt(priv);
1067 }
1068 
stmmac_test_vlanfilt_perfect(struct stmmac_priv * priv)1069 static int stmmac_test_vlanfilt_perfect(struct stmmac_priv *priv)
1070 {
1071 	int ret, prev_cap = priv->dma_cap.vlhash;
1072 
1073 	if (!(priv->dev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
1074 		return -EOPNOTSUPP;
1075 
1076 	priv->dma_cap.vlhash = 0;
1077 	ret = __stmmac_test_vlanfilt(priv);
1078 	priv->dma_cap.vlhash = prev_cap;
1079 
1080 	return ret;
1081 }
1082 
__stmmac_test_dvlanfilt(struct stmmac_priv * priv)1083 static int __stmmac_test_dvlanfilt(struct stmmac_priv *priv)
1084 {
1085 	struct stmmac_packet_attrs attr = { };
1086 	struct stmmac_test_priv *tpriv;
1087 	struct sk_buff *skb = NULL;
1088 	int ret = 0, i;
1089 
1090 	tpriv = kzalloc_obj(*tpriv);
1091 	if (!tpriv)
1092 		return -ENOMEM;
1093 
1094 	tpriv->ok = false;
1095 	tpriv->double_vlan = true;
1096 	init_completion(&tpriv->comp);
1097 
1098 	tpriv->pt.type = htons(ETH_P_8021Q);
1099 	tpriv->pt.func = stmmac_test_vlan_validate;
1100 	tpriv->pt.dev = priv->dev;
1101 	tpriv->pt.af_packet_priv = tpriv;
1102 	tpriv->packet = &attr;
1103 	tpriv->capture_all = true;
1104 
1105 	/*
1106 	 * As we use HASH filtering, false positives may appear. This is a
1107 	 * specially chosen ID so that adjacent IDs (+4) have different
1108 	 * HASH values.
1109 	 */
1110 	tpriv->vlan_id = 0x123;
1111 
1112 	ret = vlan_vid_add(priv->dev, htons(ETH_P_8021AD), tpriv->vlan_id);
1113 	if (ret)
1114 		goto cleanup;
1115 
1116 	attr.vlan = 2;
1117 	attr.dst = priv->dev->dev_addr;
1118 	attr.sport = 9;
1119 	attr.dport = 9;
1120 
1121 	stmmac_sft_add_pack(&tpriv->pt);
1122 
1123 	for (i = 0; i < 4; i++) {
1124 		attr.vlan_id_out = tpriv->vlan_id + i;
1125 
1126 		skb = stmmac_test_get_udp_skb(priv, &attr);
1127 		if (!skb) {
1128 			ret = -ENOMEM;
1129 			goto vlan_del;
1130 		}
1131 
1132 		ret = dev_direct_xmit(skb, 0);
1133 		if (ret)
1134 			goto vlan_del;
1135 
1136 		wait_for_completion_timeout(&tpriv->comp, STMMAC_LB_TIMEOUT);
1137 		ret = tpriv->ok ? 0 : -ETIMEDOUT;
1138 		if (ret && !i) {
1139 			goto vlan_del;
1140 		} else if (!ret && i) {
1141 			ret = -EINVAL;
1142 			goto vlan_del;
1143 		} else {
1144 			ret = 0;
1145 		}
1146 
1147 		tpriv->ok = false;
1148 	}
1149 
1150 vlan_del:
1151 	stmmac_sft_remove_pack(&tpriv->pt);
1152 	vlan_vid_del(priv->dev, htons(ETH_P_8021AD), tpriv->vlan_id);
1153 cleanup:
1154 	kfree(tpriv);
1155 	return ret;
1156 }
1157 
stmmac_test_dvlanfilt(struct stmmac_priv * priv)1158 static int stmmac_test_dvlanfilt(struct stmmac_priv *priv)
1159 {
1160 	if (!priv->dma_cap.vlhash)
1161 		return -EOPNOTSUPP;
1162 
1163 	return __stmmac_test_dvlanfilt(priv);
1164 }
1165 
stmmac_test_dvlanfilt_perfect(struct stmmac_priv * priv)1166 static int stmmac_test_dvlanfilt_perfect(struct stmmac_priv *priv)
1167 {
1168 	int ret, prev_cap = priv->dma_cap.vlhash;
1169 
1170 	if (!(priv->dev->features & NETIF_F_HW_VLAN_STAG_FILTER))
1171 		return -EOPNOTSUPP;
1172 
1173 	priv->dma_cap.vlhash = 0;
1174 	ret = __stmmac_test_dvlanfilt(priv);
1175 	priv->dma_cap.vlhash = prev_cap;
1176 
1177 	return ret;
1178 }
1179 
1180 #ifdef CONFIG_NET_CLS_ACT
stmmac_test_rxp(struct stmmac_priv * priv)1181 static int stmmac_test_rxp(struct stmmac_priv *priv)
1182 {
1183 	unsigned char addr[ETH_ALEN] = {0xde, 0xad, 0xbe, 0xef, 0x00, 0x00};
1184 	struct tc_cls_u32_offload cls_u32 = { };
1185 	struct stmmac_packet_attrs attr = { };
1186 	struct tc_action **actions;
1187 	struct tc_u32_sel *sel;
1188 	struct tcf_gact *gact;
1189 	struct tcf_exts *exts;
1190 	int ret, i, nk = 1;
1191 
1192 	if (!tc_can_offload(priv->dev))
1193 		return -EOPNOTSUPP;
1194 	if (!priv->dma_cap.frpsel)
1195 		return -EOPNOTSUPP;
1196 
1197 	sel = kzalloc_flex(*sel, keys, nk);
1198 	if (!sel)
1199 		return -ENOMEM;
1200 
1201 	exts = kzalloc_obj(*exts);
1202 	if (!exts) {
1203 		ret = -ENOMEM;
1204 		goto cleanup_sel;
1205 	}
1206 
1207 	actions = kzalloc_objs(*actions, nk);
1208 	if (!actions) {
1209 		ret = -ENOMEM;
1210 		goto cleanup_exts;
1211 	}
1212 
1213 	gact = kzalloc_objs(*gact, nk);
1214 	if (!gact) {
1215 		ret = -ENOMEM;
1216 		goto cleanup_actions;
1217 	}
1218 
1219 	cls_u32.command = TC_CLSU32_NEW_KNODE;
1220 	cls_u32.common.chain_index = 0;
1221 	cls_u32.common.protocol = htons(ETH_P_ALL);
1222 	cls_u32.knode.exts = exts;
1223 	cls_u32.knode.sel = sel;
1224 	cls_u32.knode.handle = 0x123;
1225 
1226 	exts->nr_actions = nk;
1227 	exts->actions = actions;
1228 	for (i = 0; i < nk; i++) {
1229 		actions[i] = (struct tc_action *)&gact[i];
1230 		gact->tcf_action = TC_ACT_SHOT;
1231 	}
1232 
1233 	sel->nkeys = nk;
1234 	sel->offshift = 0;
1235 	sel->keys[0].off = 6;
1236 	sel->keys[0].val = htonl(0xdeadbeef);
1237 	sel->keys[0].mask = ~0x0;
1238 
1239 	ret = stmmac_tc_setup_cls_u32(priv, priv, &cls_u32);
1240 	if (ret)
1241 		goto cleanup_act;
1242 
1243 	attr.dst = priv->dev->dev_addr;
1244 	attr.src = addr;
1245 
1246 	ret = __stmmac_test_loopback(priv, &attr);
1247 	ret = ret ? 0 : -EINVAL; /* Shall NOT receive packet */
1248 
1249 	cls_u32.command = TC_CLSU32_DELETE_KNODE;
1250 	stmmac_tc_setup_cls_u32(priv, priv, &cls_u32);
1251 
1252 cleanup_act:
1253 	kfree(gact);
1254 cleanup_actions:
1255 	kfree(actions);
1256 cleanup_exts:
1257 	kfree(exts);
1258 cleanup_sel:
1259 	kfree(sel);
1260 	return ret;
1261 }
1262 #else
stmmac_test_rxp(struct stmmac_priv * priv)1263 static int stmmac_test_rxp(struct stmmac_priv *priv)
1264 {
1265 	return -EOPNOTSUPP;
1266 }
1267 #endif
1268 
stmmac_test_desc_sai(struct stmmac_priv * priv)1269 static int stmmac_test_desc_sai(struct stmmac_priv *priv)
1270 {
1271 	unsigned char src[ETH_ALEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1272 	struct stmmac_packet_attrs attr = { };
1273 	int ret;
1274 
1275 	if (!priv->dma_cap.vlins)
1276 		return -EOPNOTSUPP;
1277 
1278 	attr.remove_sa = true;
1279 	attr.sarc = true;
1280 	attr.src = src;
1281 	attr.dst = priv->dev->dev_addr;
1282 
1283 	priv->sarc_type = 0x1;
1284 
1285 	ret = __stmmac_test_loopback(priv, &attr);
1286 
1287 	priv->sarc_type = 0x0;
1288 	return ret;
1289 }
1290 
stmmac_test_desc_sar(struct stmmac_priv * priv)1291 static int stmmac_test_desc_sar(struct stmmac_priv *priv)
1292 {
1293 	unsigned char src[ETH_ALEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1294 	struct stmmac_packet_attrs attr = { };
1295 	int ret;
1296 
1297 	if (!priv->dma_cap.vlins)
1298 		return -EOPNOTSUPP;
1299 
1300 	attr.sarc = true;
1301 	attr.src = src;
1302 	attr.dst = priv->dev->dev_addr;
1303 
1304 	priv->sarc_type = 0x2;
1305 
1306 	ret = __stmmac_test_loopback(priv, &attr);
1307 
1308 	priv->sarc_type = 0x0;
1309 	return ret;
1310 }
1311 
stmmac_test_reg_sai(struct stmmac_priv * priv)1312 static int stmmac_test_reg_sai(struct stmmac_priv *priv)
1313 {
1314 	unsigned char src[ETH_ALEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1315 	struct stmmac_packet_attrs attr = { };
1316 	int ret;
1317 
1318 	if (!priv->dma_cap.vlins)
1319 		return -EOPNOTSUPP;
1320 
1321 	attr.remove_sa = true;
1322 	attr.sarc = true;
1323 	attr.src = src;
1324 	attr.dst = priv->dev->dev_addr;
1325 
1326 	if (stmmac_sarc_configure(priv, priv->ioaddr, 0x2))
1327 		return -EOPNOTSUPP;
1328 
1329 	ret = __stmmac_test_loopback(priv, &attr);
1330 
1331 	stmmac_sarc_configure(priv, priv->ioaddr, 0x0);
1332 	return ret;
1333 }
1334 
stmmac_test_reg_sar(struct stmmac_priv * priv)1335 static int stmmac_test_reg_sar(struct stmmac_priv *priv)
1336 {
1337 	unsigned char src[ETH_ALEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1338 	struct stmmac_packet_attrs attr = { };
1339 	int ret;
1340 
1341 	if (!priv->dma_cap.vlins)
1342 		return -EOPNOTSUPP;
1343 
1344 	attr.sarc = true;
1345 	attr.src = src;
1346 	attr.dst = priv->dev->dev_addr;
1347 
1348 	if (stmmac_sarc_configure(priv, priv->ioaddr, 0x3))
1349 		return -EOPNOTSUPP;
1350 
1351 	ret = __stmmac_test_loopback(priv, &attr);
1352 
1353 	stmmac_sarc_configure(priv, priv->ioaddr, 0x0);
1354 	return ret;
1355 }
1356 
stmmac_test_vlanoff_common(struct stmmac_priv * priv,bool svlan)1357 static int stmmac_test_vlanoff_common(struct stmmac_priv *priv, bool svlan)
1358 {
1359 	struct stmmac_packet_attrs attr = { };
1360 	struct stmmac_test_priv *tpriv;
1361 	struct sk_buff *skb = NULL;
1362 	int ret = 0;
1363 	u16 proto;
1364 
1365 	if (!priv->dma_cap.vlins)
1366 		return -EOPNOTSUPP;
1367 
1368 	tpriv = kzalloc_obj(*tpriv);
1369 	if (!tpriv)
1370 		return -ENOMEM;
1371 
1372 	proto = svlan ? ETH_P_8021AD : ETH_P_8021Q;
1373 
1374 	tpriv->ok = false;
1375 	tpriv->double_vlan = svlan;
1376 	init_completion(&tpriv->comp);
1377 
1378 	tpriv->pt.type = svlan ? htons(ETH_P_8021Q) : htons(ETH_P_IP);
1379 	tpriv->pt.func = stmmac_test_vlan_validate;
1380 	tpriv->pt.dev = priv->dev;
1381 	tpriv->pt.af_packet_priv = tpriv;
1382 	tpriv->packet = &attr;
1383 	tpriv->vlan_id = 0x123;
1384 	tpriv->capture_all = true;
1385 
1386 	ret = vlan_vid_add(priv->dev, htons(proto), tpriv->vlan_id);
1387 	if (ret)
1388 		goto cleanup;
1389 
1390 	attr.dst = priv->dev->dev_addr;
1391 
1392 	stmmac_sft_add_pack(&tpriv->pt);
1393 
1394 	skb = stmmac_test_get_udp_skb(priv, &attr);
1395 	if (!skb) {
1396 		ret = -ENOMEM;
1397 		goto vlan_del;
1398 	}
1399 
1400 	__vlan_hwaccel_put_tag(skb, htons(proto), tpriv->vlan_id);
1401 	skb->protocol = htons(proto);
1402 
1403 	ret = dev_direct_xmit(skb, 0);
1404 	if (ret)
1405 		goto vlan_del;
1406 
1407 	wait_for_completion_timeout(&tpriv->comp, STMMAC_LB_TIMEOUT);
1408 	ret = tpriv->ok ? 0 : -ETIMEDOUT;
1409 
1410 vlan_del:
1411 	stmmac_sft_remove_pack(&tpriv->pt);
1412 	vlan_vid_del(priv->dev, htons(proto), tpriv->vlan_id);
1413 cleanup:
1414 	kfree(tpriv);
1415 	return ret;
1416 }
1417 
stmmac_test_vlanoff(struct stmmac_priv * priv)1418 static int stmmac_test_vlanoff(struct stmmac_priv *priv)
1419 {
1420 	return stmmac_test_vlanoff_common(priv, false);
1421 }
1422 
stmmac_test_svlanoff(struct stmmac_priv * priv)1423 static int stmmac_test_svlanoff(struct stmmac_priv *priv)
1424 {
1425 	if (!(priv->dev->features & NETIF_F_HW_VLAN_STAG_TX))
1426 		return -EOPNOTSUPP;
1427 	return stmmac_test_vlanoff_common(priv, true);
1428 }
1429 
1430 #ifdef CONFIG_NET_CLS_ACT
__stmmac_test_l3filt(struct stmmac_priv * priv,u32 dst,u32 src,u32 dst_mask,u32 src_mask)1431 static int __stmmac_test_l3filt(struct stmmac_priv *priv, u32 dst, u32 src,
1432 				u32 dst_mask, u32 src_mask)
1433 {
1434 	struct flow_dissector_key_ipv4_addrs key, mask;
1435 	unsigned long dummy_cookie = 0xdeadbeef;
1436 	struct stmmac_packet_attrs attr = { };
1437 	struct flow_dissector *dissector;
1438 	struct flow_cls_offload *cls;
1439 	int ret, old_enable = 0;
1440 	struct flow_rule *rule;
1441 
1442 	if (!tc_can_offload(priv->dev))
1443 		return -EOPNOTSUPP;
1444 	if (!priv->dma_cap.l3l4fnum)
1445 		return -EOPNOTSUPP;
1446 	if (priv->rss.enable) {
1447 		old_enable = priv->rss.enable;
1448 		priv->rss.enable = false;
1449 		stmmac_rss_configure(priv, priv->hw, NULL,
1450 				     priv->plat->rx_queues_to_use);
1451 	}
1452 
1453 	dissector = kzalloc_obj(*dissector);
1454 	if (!dissector) {
1455 		ret = -ENOMEM;
1456 		goto cleanup_rss;
1457 	}
1458 
1459 	dissector->used_keys |= (1ULL << FLOW_DISSECTOR_KEY_IPV4_ADDRS);
1460 	dissector->offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] = 0;
1461 
1462 	cls = kzalloc_obj(*cls);
1463 	if (!cls) {
1464 		ret = -ENOMEM;
1465 		goto cleanup_dissector;
1466 	}
1467 
1468 	cls->common.chain_index = 0;
1469 	cls->command = FLOW_CLS_REPLACE;
1470 	cls->cookie = dummy_cookie;
1471 
1472 	rule = kzalloc_flex(*rule, action.entries, 1);
1473 	if (!rule) {
1474 		ret = -ENOMEM;
1475 		goto cleanup_cls;
1476 	}
1477 
1478 	rule->match.dissector = dissector;
1479 	rule->match.key = (void *)&key;
1480 	rule->match.mask = (void *)&mask;
1481 
1482 	key.src = htonl(src);
1483 	key.dst = htonl(dst);
1484 	mask.src = src_mask;
1485 	mask.dst = dst_mask;
1486 
1487 	cls->rule = rule;
1488 
1489 	rule->action.entries[0].id = FLOW_ACTION_DROP;
1490 	rule->action.entries[0].hw_stats = FLOW_ACTION_HW_STATS_ANY;
1491 	rule->action.num_entries = 1;
1492 
1493 	attr.dst = priv->dev->dev_addr;
1494 	attr.ip_dst = dst;
1495 	attr.ip_src = src;
1496 
1497 	/* Shall receive packet */
1498 	ret = __stmmac_test_loopback(priv, &attr);
1499 	if (ret)
1500 		goto cleanup_rule;
1501 
1502 	ret = stmmac_tc_setup_cls(priv, priv, cls);
1503 	if (ret)
1504 		goto cleanup_rule;
1505 
1506 	/* Shall NOT receive packet */
1507 	ret = __stmmac_test_loopback(priv, &attr);
1508 	ret = ret ? 0 : -EINVAL;
1509 
1510 	cls->command = FLOW_CLS_DESTROY;
1511 	stmmac_tc_setup_cls(priv, priv, cls);
1512 cleanup_rule:
1513 	kfree(rule);
1514 cleanup_cls:
1515 	kfree(cls);
1516 cleanup_dissector:
1517 	kfree(dissector);
1518 cleanup_rss:
1519 	if (old_enable) {
1520 		priv->rss.enable = old_enable;
1521 		stmmac_rss_configure(priv, priv->hw, &priv->rss,
1522 				     priv->plat->rx_queues_to_use);
1523 	}
1524 
1525 	return ret;
1526 }
1527 #else
__stmmac_test_l3filt(struct stmmac_priv * priv,u32 dst,u32 src,u32 dst_mask,u32 src_mask)1528 static int __stmmac_test_l3filt(struct stmmac_priv *priv, u32 dst, u32 src,
1529 				u32 dst_mask, u32 src_mask)
1530 {
1531 	return -EOPNOTSUPP;
1532 }
1533 #endif
1534 
stmmac_test_l3filt_da(struct stmmac_priv * priv)1535 static int stmmac_test_l3filt_da(struct stmmac_priv *priv)
1536 {
1537 	u32 addr = 0x10203040;
1538 
1539 	return __stmmac_test_l3filt(priv, addr, 0, ~0, 0);
1540 }
1541 
stmmac_test_l3filt_sa(struct stmmac_priv * priv)1542 static int stmmac_test_l3filt_sa(struct stmmac_priv *priv)
1543 {
1544 	u32 addr = 0x10203040;
1545 
1546 	return __stmmac_test_l3filt(priv, 0, addr, 0, ~0);
1547 }
1548 
1549 #ifdef CONFIG_NET_CLS_ACT
__stmmac_test_l4filt(struct stmmac_priv * priv,u32 dst,u32 src,u32 dst_mask,u32 src_mask,bool udp)1550 static int __stmmac_test_l4filt(struct stmmac_priv *priv, u32 dst, u32 src,
1551 				u32 dst_mask, u32 src_mask, bool udp)
1552 {
1553 	struct {
1554 		struct flow_dissector_key_basic bkey;
1555 		struct flow_dissector_key_ports key;
1556 	} __aligned(BITS_PER_LONG / 8) keys = { };
1557 	struct {
1558 		struct flow_dissector_key_basic bmask;
1559 		struct flow_dissector_key_ports mask;
1560 	} __aligned(BITS_PER_LONG / 8) masks = { };
1561 	unsigned long dummy_cookie = 0xdeadbeef;
1562 	struct stmmac_packet_attrs attr = { };
1563 	struct flow_dissector *dissector;
1564 	struct flow_cls_offload *cls;
1565 	int ret, old_enable = 0;
1566 	struct flow_rule *rule;
1567 
1568 	if (!tc_can_offload(priv->dev))
1569 		return -EOPNOTSUPP;
1570 	if (!priv->dma_cap.l3l4fnum)
1571 		return -EOPNOTSUPP;
1572 	if (priv->rss.enable) {
1573 		old_enable = priv->rss.enable;
1574 		priv->rss.enable = false;
1575 		stmmac_rss_configure(priv, priv->hw, NULL,
1576 				     priv->plat->rx_queues_to_use);
1577 	}
1578 
1579 	dissector = kzalloc_obj(*dissector);
1580 	if (!dissector) {
1581 		ret = -ENOMEM;
1582 		goto cleanup_rss;
1583 	}
1584 
1585 	dissector->used_keys |= (1ULL << FLOW_DISSECTOR_KEY_BASIC);
1586 	dissector->used_keys |= (1ULL << FLOW_DISSECTOR_KEY_PORTS);
1587 	dissector->offset[FLOW_DISSECTOR_KEY_BASIC] = 0;
1588 	dissector->offset[FLOW_DISSECTOR_KEY_PORTS] = offsetof(typeof(keys), key);
1589 
1590 	cls = kzalloc_obj(*cls);
1591 	if (!cls) {
1592 		ret = -ENOMEM;
1593 		goto cleanup_dissector;
1594 	}
1595 
1596 	cls->common.chain_index = 0;
1597 	cls->command = FLOW_CLS_REPLACE;
1598 	cls->cookie = dummy_cookie;
1599 
1600 	rule = kzalloc_flex(*rule, action.entries, 1);
1601 	if (!rule) {
1602 		ret = -ENOMEM;
1603 		goto cleanup_cls;
1604 	}
1605 
1606 	rule->match.dissector = dissector;
1607 	rule->match.key = (void *)&keys;
1608 	rule->match.mask = (void *)&masks;
1609 
1610 	keys.bkey.ip_proto = udp ? IPPROTO_UDP : IPPROTO_TCP;
1611 	keys.key.src = htons(src);
1612 	keys.key.dst = htons(dst);
1613 	/* Match the full IP proto field */
1614 	masks.bmask.ip_proto = 0xff;
1615 	masks.mask.src = src_mask;
1616 	masks.mask.dst = dst_mask;
1617 
1618 	cls->rule = rule;
1619 
1620 	rule->action.entries[0].id = FLOW_ACTION_DROP;
1621 	rule->action.entries[0].hw_stats = FLOW_ACTION_HW_STATS_ANY;
1622 	rule->action.num_entries = 1;
1623 
1624 	attr.dst = priv->dev->dev_addr;
1625 	attr.tcp = !udp;
1626 	attr.sport = src;
1627 	attr.dport = dst;
1628 	attr.ip_dst = 0;
1629 
1630 	/* Shall receive packet */
1631 	ret = __stmmac_test_loopback(priv, &attr);
1632 	if (ret)
1633 		goto cleanup_rule;
1634 
1635 	ret = stmmac_tc_setup_cls(priv, priv, cls);
1636 	if (ret)
1637 		goto cleanup_rule;
1638 
1639 	/* Shall NOT receive packet */
1640 	ret = __stmmac_test_loopback(priv, &attr);
1641 	ret = ret ? 0 : -EINVAL;
1642 
1643 	cls->command = FLOW_CLS_DESTROY;
1644 	stmmac_tc_setup_cls(priv, priv, cls);
1645 cleanup_rule:
1646 	kfree(rule);
1647 cleanup_cls:
1648 	kfree(cls);
1649 cleanup_dissector:
1650 	kfree(dissector);
1651 cleanup_rss:
1652 	if (old_enable) {
1653 		priv->rss.enable = old_enable;
1654 		stmmac_rss_configure(priv, priv->hw, &priv->rss,
1655 				     priv->plat->rx_queues_to_use);
1656 	}
1657 
1658 	return ret;
1659 }
1660 #else
__stmmac_test_l4filt(struct stmmac_priv * priv,u32 dst,u32 src,u32 dst_mask,u32 src_mask,bool udp)1661 static int __stmmac_test_l4filt(struct stmmac_priv *priv, u32 dst, u32 src,
1662 				u32 dst_mask, u32 src_mask, bool udp)
1663 {
1664 	return -EOPNOTSUPP;
1665 }
1666 #endif
1667 
stmmac_test_l4filt_da_tcp(struct stmmac_priv * priv)1668 static int stmmac_test_l4filt_da_tcp(struct stmmac_priv *priv)
1669 {
1670 	u16 dummy_port = 0x123;
1671 
1672 	return __stmmac_test_l4filt(priv, dummy_port, 0, ~0, 0, false);
1673 }
1674 
stmmac_test_l4filt_sa_tcp(struct stmmac_priv * priv)1675 static int stmmac_test_l4filt_sa_tcp(struct stmmac_priv *priv)
1676 {
1677 	u16 dummy_port = 0x123;
1678 
1679 	return __stmmac_test_l4filt(priv, 0, dummy_port, 0, ~0, false);
1680 }
1681 
stmmac_test_l4filt_da_udp(struct stmmac_priv * priv)1682 static int stmmac_test_l4filt_da_udp(struct stmmac_priv *priv)
1683 {
1684 	u16 dummy_port = 0x123;
1685 
1686 	return __stmmac_test_l4filt(priv, dummy_port, 0, ~0, 0, true);
1687 }
1688 
stmmac_test_l4filt_sa_udp(struct stmmac_priv * priv)1689 static int stmmac_test_l4filt_sa_udp(struct stmmac_priv *priv)
1690 {
1691 	u16 dummy_port = 0x123;
1692 
1693 	return __stmmac_test_l4filt(priv, 0, dummy_port, 0, ~0, true);
1694 }
1695 
stmmac_test_arp_validate(struct sk_buff * skb,struct net_device * ndev,struct packet_type * pt,struct net_device * orig_ndev)1696 static int stmmac_test_arp_validate(struct sk_buff *skb,
1697 				    struct net_device *ndev,
1698 				    struct packet_type *pt,
1699 				    struct net_device *orig_ndev)
1700 {
1701 	struct stmmac_test_priv *tpriv = pt->af_packet_priv;
1702 	struct ethhdr *ehdr;
1703 	struct arphdr *ahdr;
1704 
1705 	ehdr = (struct ethhdr *)skb_mac_header(skb);
1706 	if (!ether_addr_equal_unaligned(ehdr->h_dest, tpriv->packet->src))
1707 		goto out;
1708 
1709 	ahdr = arp_hdr(skb);
1710 	if (ahdr->ar_op != htons(ARPOP_REPLY))
1711 		goto out;
1712 
1713 	tpriv->ok = true;
1714 	complete(&tpriv->comp);
1715 out:
1716 	kfree_skb(skb);
1717 	return 0;
1718 }
1719 
stmmac_test_arpoffload(struct stmmac_priv * priv)1720 static int stmmac_test_arpoffload(struct stmmac_priv *priv)
1721 {
1722 	unsigned char src[ETH_ALEN] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
1723 	unsigned char dst[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1724 	struct stmmac_packet_attrs attr = { };
1725 	struct stmmac_test_priv *tpriv;
1726 	struct sk_buff *skb = NULL;
1727 	u32 ip_addr = 0xdeadcafe;
1728 	u32 ip_src = 0xdeadbeef;
1729 	int ret;
1730 
1731 	if (!priv->dma_cap.arpoffsel)
1732 		return -EOPNOTSUPP;
1733 
1734 	tpriv = kzalloc_obj(*tpriv);
1735 	if (!tpriv)
1736 		return -ENOMEM;
1737 
1738 	tpriv->ok = false;
1739 	init_completion(&tpriv->comp);
1740 
1741 	tpriv->pt.type = htons(ETH_P_ARP);
1742 	tpriv->pt.func = stmmac_test_arp_validate;
1743 	tpriv->pt.dev = priv->dev;
1744 	tpriv->pt.af_packet_priv = tpriv;
1745 	tpriv->packet = &attr;
1746 	dev_add_pack(&tpriv->pt);
1747 
1748 	attr.src = src;
1749 	attr.ip_src = ip_src;
1750 	attr.dst = dst;
1751 	attr.ip_dst = ip_addr;
1752 
1753 	skb = stmmac_test_get_arp_skb(priv, &attr);
1754 	if (!skb) {
1755 		ret = -ENOMEM;
1756 		goto cleanup;
1757 	}
1758 
1759 	ret = stmmac_set_arp_offload(priv, priv->hw, true, ip_addr);
1760 	if (ret) {
1761 		kfree_skb(skb);
1762 		goto cleanup;
1763 	}
1764 
1765 	ret = dev_set_promiscuity(priv->dev, 1);
1766 	if (ret) {
1767 		kfree_skb(skb);
1768 		goto cleanup;
1769 	}
1770 
1771 	ret = dev_direct_xmit(skb, 0);
1772 	if (ret)
1773 		goto cleanup_promisc;
1774 
1775 	wait_for_completion_timeout(&tpriv->comp, STMMAC_LB_TIMEOUT);
1776 	ret = tpriv->ok ? 0 : -ETIMEDOUT;
1777 
1778 cleanup_promisc:
1779 	dev_set_promiscuity(priv->dev, -1);
1780 cleanup:
1781 	stmmac_set_arp_offload(priv, priv->hw, false, 0x0);
1782 	dev_remove_pack(&tpriv->pt);
1783 	kfree(tpriv);
1784 	return ret;
1785 }
1786 
__stmmac_test_jumbo(struct stmmac_priv * priv,u16 queue)1787 static int __stmmac_test_jumbo(struct stmmac_priv *priv, u16 queue)
1788 {
1789 	struct stmmac_packet_attrs attr = { };
1790 	int size = priv->dma_conf.dma_buf_sz;
1791 
1792 	if (!dwmac_is_xmac(priv->plat->core_type))
1793 		size -= NET_IP_ALIGN;
1794 
1795 	attr.dst = priv->dev->dev_addr;
1796 	attr.max_size = size - ETH_FCS_LEN;
1797 	attr.queue_mapping = queue;
1798 
1799 	return __stmmac_test_loopback(priv, &attr);
1800 }
1801 
stmmac_test_jumbo(struct stmmac_priv * priv)1802 static int stmmac_test_jumbo(struct stmmac_priv *priv)
1803 {
1804 	return __stmmac_test_jumbo(priv, 0);
1805 }
1806 
stmmac_test_mjumbo(struct stmmac_priv * priv)1807 static int stmmac_test_mjumbo(struct stmmac_priv *priv)
1808 {
1809 	u32 chan, tx_cnt = priv->plat->tx_queues_to_use;
1810 	int ret;
1811 
1812 	if (tx_cnt <= 1)
1813 		return -EOPNOTSUPP;
1814 
1815 	for (chan = 0; chan < tx_cnt; chan++) {
1816 		ret = __stmmac_test_jumbo(priv, chan);
1817 		if (ret)
1818 			return ret;
1819 	}
1820 
1821 	return 0;
1822 }
1823 
stmmac_test_sph(struct stmmac_priv * priv)1824 static int stmmac_test_sph(struct stmmac_priv *priv)
1825 {
1826 	unsigned long cnt_end, cnt_start = priv->xstats.rx_split_hdr_pkt_n;
1827 	struct stmmac_packet_attrs attr = { };
1828 	int ret;
1829 
1830 	if (!priv->sph_active)
1831 		return -EOPNOTSUPP;
1832 
1833 	/* Check for UDP first */
1834 	attr.dst = priv->dev->dev_addr;
1835 	attr.tcp = false;
1836 
1837 	ret = __stmmac_test_loopback(priv, &attr);
1838 	if (ret)
1839 		return ret;
1840 
1841 	cnt_end = priv->xstats.rx_split_hdr_pkt_n;
1842 	if (cnt_end <= cnt_start)
1843 		return -EINVAL;
1844 
1845 	/* Check for TCP now */
1846 	cnt_start = cnt_end;
1847 
1848 	attr.dst = priv->dev->dev_addr;
1849 	attr.tcp = true;
1850 
1851 	ret = __stmmac_test_loopback(priv, &attr);
1852 	if (ret)
1853 		return ret;
1854 
1855 	cnt_end = priv->xstats.rx_split_hdr_pkt_n;
1856 	if (cnt_end <= cnt_start)
1857 		return -EINVAL;
1858 
1859 	return 0;
1860 }
1861 
stmmac_test_tbs(struct stmmac_priv * priv)1862 static int stmmac_test_tbs(struct stmmac_priv *priv)
1863 {
1864 #define STMMAC_TBS_LT_OFFSET		(500 * 1000 * 1000) /* 500 ms*/
1865 	struct stmmac_packet_attrs attr = { };
1866 	struct tc_etf_qopt_offload qopt;
1867 	u64 start_time, curr_time = 0;
1868 	unsigned long flags;
1869 	int ret, i;
1870 
1871 	if (!priv->hwts_tx_en)
1872 		return -EOPNOTSUPP;
1873 
1874 	/* Find first TBS enabled Queue, if any */
1875 	for (i = 0; i < priv->plat->tx_queues_to_use; i++)
1876 		if (priv->dma_conf.tx_queue[i].tbs & STMMAC_TBS_AVAIL)
1877 			break;
1878 
1879 	if (i >= priv->plat->tx_queues_to_use)
1880 		return -EOPNOTSUPP;
1881 
1882 	qopt.enable = true;
1883 	qopt.queue = i;
1884 
1885 	ret = stmmac_tc_setup_etf(priv, priv, &qopt);
1886 	if (ret)
1887 		return ret;
1888 
1889 	read_lock_irqsave(&priv->ptp_lock, flags);
1890 	stmmac_get_systime(priv, priv->ptpaddr, &curr_time);
1891 	read_unlock_irqrestore(&priv->ptp_lock, flags);
1892 
1893 	if (!curr_time) {
1894 		ret = -EOPNOTSUPP;
1895 		goto fail_disable;
1896 	}
1897 
1898 	start_time = curr_time;
1899 	curr_time += STMMAC_TBS_LT_OFFSET;
1900 
1901 	attr.dst = priv->dev->dev_addr;
1902 	attr.timestamp = curr_time;
1903 	attr.timeout = nsecs_to_jiffies(2 * STMMAC_TBS_LT_OFFSET);
1904 	attr.queue_mapping = i;
1905 
1906 	ret = __stmmac_test_loopback(priv, &attr);
1907 	if (ret)
1908 		goto fail_disable;
1909 
1910 	/* Check if expected time has elapsed */
1911 	read_lock_irqsave(&priv->ptp_lock, flags);
1912 	stmmac_get_systime(priv, priv->ptpaddr, &curr_time);
1913 	read_unlock_irqrestore(&priv->ptp_lock, flags);
1914 
1915 	if ((curr_time - start_time) < STMMAC_TBS_LT_OFFSET)
1916 		ret = -EINVAL;
1917 
1918 fail_disable:
1919 	qopt.enable = false;
1920 	stmmac_tc_setup_etf(priv, priv, &qopt);
1921 	return ret;
1922 }
1923 
1924 static const struct stmmac_test {
1925 	char name[ETH_GSTRING_LEN];
1926 	int lb;
1927 	int (*fn)(struct stmmac_priv *priv);
1928 } stmmac_selftests[] = {
1929 	{
1930 		.name = "MAC Loopback               ",
1931 		.fn = stmmac_test_mac_loopback,
1932 	}, {
1933 		.name = "MMC Counters               ",
1934 		.fn = stmmac_test_mmc,
1935 	}, {
1936 		.name = "EEE                        ",
1937 		.fn = stmmac_test_eee,
1938 	}, {
1939 		.name = "Hash Filter MC             ",
1940 		.fn = stmmac_test_hfilt,
1941 	}, {
1942 		.name = "Perfect Filter UC          ",
1943 		.fn = stmmac_test_pfilt,
1944 	}, {
1945 		.name = "MC Filter                  ",
1946 		.fn = stmmac_test_mcfilt,
1947 	}, {
1948 		.name = "UC Filter                  ",
1949 		.fn = stmmac_test_ucfilt,
1950 	}, {
1951 		.name = "Flow Control               ",
1952 		.fn = stmmac_test_flowctrl,
1953 	}, {
1954 		.name = "RSS                        ",
1955 		.fn = stmmac_test_rss,
1956 	}, {
1957 		.name = "VLAN Filtering             ",
1958 		.fn = stmmac_test_vlanfilt,
1959 	}, {
1960 		.name = "VLAN Filtering (perf)      ",
1961 		.fn = stmmac_test_vlanfilt_perfect,
1962 	}, {
1963 		.name = "Double VLAN Filter         ",
1964 		.fn = stmmac_test_dvlanfilt,
1965 	}, {
1966 		.name = "Double VLAN Filter (perf)  ",
1967 		.fn = stmmac_test_dvlanfilt_perfect,
1968 	}, {
1969 		.name = "Flexible RX Parser         ",
1970 		.fn = stmmac_test_rxp,
1971 	}, {
1972 		.name = "SA Insertion (desc)        ",
1973 		.fn = stmmac_test_desc_sai,
1974 	}, {
1975 		.name = "SA Replacement (desc)      ",
1976 		.fn = stmmac_test_desc_sar,
1977 	}, {
1978 		.name = "SA Insertion (reg)         ",
1979 		.fn = stmmac_test_reg_sai,
1980 	}, {
1981 		.name = "SA Replacement (reg)       ",
1982 		.fn = stmmac_test_reg_sar,
1983 	}, {
1984 		.name = "VLAN TX Insertion          ",
1985 		.fn = stmmac_test_vlanoff,
1986 	}, {
1987 		.name = "SVLAN TX Insertion         ",
1988 		.fn = stmmac_test_svlanoff,
1989 	}, {
1990 		.name = "L3 DA Filtering            ",
1991 		.fn = stmmac_test_l3filt_da,
1992 	}, {
1993 		.name = "L3 SA Filtering            ",
1994 		.fn = stmmac_test_l3filt_sa,
1995 	}, {
1996 		.name = "L4 DA TCP Filtering        ",
1997 		.fn = stmmac_test_l4filt_da_tcp,
1998 	}, {
1999 		.name = "L4 SA TCP Filtering        ",
2000 		.fn = stmmac_test_l4filt_sa_tcp,
2001 	}, {
2002 		.name = "L4 DA UDP Filtering        ",
2003 		.fn = stmmac_test_l4filt_da_udp,
2004 	}, {
2005 		.name = "L4 SA UDP Filtering        ",
2006 		.fn = stmmac_test_l4filt_sa_udp,
2007 	}, {
2008 		.name = "ARP Offload                ",
2009 		.fn = stmmac_test_arpoffload,
2010 	}, {
2011 		.name = "Jumbo Frame                ",
2012 		.fn = stmmac_test_jumbo,
2013 	}, {
2014 		.name = "Multichannel Jumbo         ",
2015 		.fn = stmmac_test_mjumbo,
2016 	}, {
2017 		.name = "Split Header               ",
2018 		.fn = stmmac_test_sph,
2019 	}, {
2020 		.name = "TBS (ETF Scheduler)        ",
2021 		.fn = stmmac_test_tbs,
2022 	},
2023 };
2024 
stmmac_selftest_run(struct net_device * dev,struct ethtool_test * etest,u64 * buf)2025 void stmmac_selftest_run(struct net_device *dev,
2026 			 struct ethtool_test *etest, u64 *buf)
2027 {
2028 	struct stmmac_priv *priv = netdev_priv(dev);
2029 	int count = stmmac_selftest_get_count(priv);
2030 	int i, ret;
2031 
2032 	memset(buf, 0, sizeof(*buf) * count);
2033 	stmmac_test_next_id = 0;
2034 
2035 	if (etest->flags != ETH_TEST_FL_OFFLINE) {
2036 		netdev_err(priv->dev, "Only offline tests are supported\n");
2037 		etest->flags |= ETH_TEST_FL_FAILED;
2038 		return;
2039 	} else if (!netif_carrier_ok(dev)) {
2040 		netdev_err(priv->dev, "You need valid Link to execute tests\n");
2041 		etest->flags |= ETH_TEST_FL_FAILED;
2042 		return;
2043 	}
2044 
2045 	/* Wait for queues drain */
2046 	msleep(200);
2047 
2048 	ret = stmmac_set_mac_loopback(priv, priv->ioaddr, true);
2049 	if (ret) {
2050 		netdev_err(priv->dev, "Loopback is not supported\n");
2051 		etest->flags |= ETH_TEST_FL_FAILED;
2052 		return;
2053 	}
2054 
2055 	for (i = 0; i < count; i++) {
2056 		ret = stmmac_selftests[i].fn(priv);
2057 		if (ret && (ret != -EOPNOTSUPP))
2058 			etest->flags |= ETH_TEST_FL_FAILED;
2059 		buf[i] = ret;
2060 	}
2061 
2062 	stmmac_set_mac_loopback(priv, priv->ioaddr, false);
2063 }
2064 
stmmac_selftest_get_strings(struct stmmac_priv * priv,u8 * data)2065 void stmmac_selftest_get_strings(struct stmmac_priv *priv, u8 *data)
2066 {
2067 	u8 *p = data;
2068 	int i;
2069 
2070 	for (i = 0; i < stmmac_selftest_get_count(priv); i++) {
2071 		snprintf(p, ETH_GSTRING_LEN, "%2d. %s", i + 1,
2072 			 stmmac_selftests[i].name);
2073 		p += ETH_GSTRING_LEN;
2074 	}
2075 }
2076 
stmmac_selftest_get_count(struct stmmac_priv * priv)2077 int stmmac_selftest_get_count(struct stmmac_priv *priv)
2078 {
2079 	return ARRAY_SIZE(stmmac_selftests);
2080 }
2081