1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2019 Synopsys, Inc. and/or its affiliates.
4 * stmmac Selftests Support
6 * Author: Jose Abreu <joabreu@synopsys.com>
9 #include <linux/completion.h>
10 #include <linux/ethtool.h>
12 #include <linux/phy.h>
13 #include <linux/udp.h>
24 #define STMMAC_TEST_PKT_SIZE (sizeof(struct ethhdr) + sizeof(struct iphdr) + \
25 sizeof(struct stmmachdr))
26 #define STMMAC_TEST_PKT_MAGIC 0xdeadcafecafedeadULL
27 #define STMMAC_LB_TIMEOUT msecs_to_jiffies(200)
29 struct stmmac_packet_attrs {
48 static u8 stmmac_test_next_id;
50 static struct sk_buff *stmmac_test_get_udp_skb(struct stmmac_priv *priv,
51 struct stmmac_packet_attrs *attr)
53 struct sk_buff *skb = NULL;
54 struct udphdr *uhdr = NULL;
55 struct tcphdr *thdr = NULL;
56 struct stmmachdr *shdr;
61 size = attr->size + STMMAC_TEST_PKT_SIZE;
69 size += sizeof(struct tcphdr);
71 size += sizeof(struct udphdr);
73 skb = netdev_alloc_skb(priv->dev, size);
78 skb_reserve(skb, NET_IP_ALIGN);
81 ehdr = skb_push(skb, ETH_HLEN + 8);
83 ehdr = skb_push(skb, ETH_HLEN + 4);
84 else if (attr->remove_sa)
85 ehdr = skb_push(skb, ETH_HLEN - 6);
87 ehdr = skb_push(skb, ETH_HLEN);
88 skb_reset_mac_header(skb);
90 skb_set_network_header(skb, skb->len);
91 ihdr = skb_put(skb, sizeof(*ihdr));
93 skb_set_transport_header(skb, skb->len);
95 thdr = skb_put(skb, sizeof(*thdr));
97 uhdr = skb_put(skb, sizeof(*uhdr));
100 eth_zero_addr(ehdr->h_source);
101 eth_zero_addr(ehdr->h_dest);
102 if (attr->src && !attr->remove_sa)
103 ether_addr_copy(ehdr->h_source, attr->src);
105 ether_addr_copy(ehdr->h_dest, attr->dst);
107 if (!attr->remove_sa) {
108 ehdr->h_proto = htons(ETH_P_IP);
110 __be16 *ptr = (__be16 *)ehdr;
113 ptr[3] = htons(ETH_P_IP);
119 if (!attr->remove_sa) {
120 tag = (void *)ehdr + ETH_HLEN;
121 proto = (void *)ehdr + (2 * ETH_ALEN);
123 tag = (void *)ehdr + ETH_HLEN - 6;
124 proto = (void *)ehdr + ETH_ALEN;
127 proto[0] = htons(ETH_P_8021Q);
128 tag[0] = htons(attr->vlan_id_out);
129 tag[1] = htons(ETH_P_IP);
130 if (attr->vlan > 1) {
131 proto[0] = htons(ETH_P_8021AD);
132 tag[1] = htons(ETH_P_8021Q);
133 tag[2] = htons(attr->vlan_id_in);
134 tag[3] = htons(ETH_P_IP);
139 thdr->source = htons(attr->sport);
140 thdr->dest = htons(attr->dport);
141 thdr->doff = sizeof(struct tcphdr) / 4;
144 uhdr->source = htons(attr->sport);
145 uhdr->dest = htons(attr->dport);
146 uhdr->len = htons(sizeof(*shdr) + sizeof(*uhdr) + attr->size);
154 ihdr->protocol = IPPROTO_TCP;
156 ihdr->protocol = IPPROTO_UDP;
157 iplen = sizeof(*ihdr) + sizeof(*shdr) + attr->size;
159 iplen += sizeof(*thdr);
161 iplen += sizeof(*uhdr);
162 ihdr->tot_len = htons(iplen);
165 ihdr->daddr = htonl(attr->ip_dst);
170 shdr = skb_put(skb, sizeof(*shdr));
172 shdr->magic = cpu_to_be64(STMMAC_TEST_PKT_MAGIC);
173 attr->id = stmmac_test_next_id;
174 shdr->id = stmmac_test_next_id++;
177 skb_put(skb, attr->size);
180 skb->ip_summed = CHECKSUM_PARTIAL;
182 thdr->check = ~tcp_v4_check(skb->len, ihdr->saddr, ihdr->daddr, 0);
183 skb->csum_start = skb_transport_header(skb) - skb->head;
184 skb->csum_offset = offsetof(struct tcphdr, check);
186 udp4_hwcsum(skb, ihdr->saddr, ihdr->daddr);
189 skb->protocol = htons(ETH_P_IP);
190 skb->pkt_type = PACKET_HOST;
191 skb->dev = priv->dev;
196 struct stmmac_test_priv {
197 struct stmmac_packet_attrs *packet;
198 struct packet_type pt;
199 struct completion comp;
205 static int stmmac_test_loopback_validate(struct sk_buff *skb,
206 struct net_device *ndev,
207 struct packet_type *pt,
208 struct net_device *orig_ndev)
210 struct stmmac_test_priv *tpriv = pt->af_packet_priv;
211 struct stmmachdr *shdr;
217 skb = skb_unshare(skb, GFP_ATOMIC);
221 if (skb_linearize(skb))
223 if (skb_headlen(skb) < (STMMAC_TEST_PKT_SIZE - ETH_HLEN))
226 ehdr = (struct ethhdr *)skb_mac_header(skb);
227 if (tpriv->packet->dst) {
228 if (!ether_addr_equal(ehdr->h_dest, tpriv->packet->dst))
231 if (tpriv->packet->src) {
232 if (!ether_addr_equal(ehdr->h_source, orig_ndev->dev_addr))
237 if (tpriv->double_vlan)
238 ihdr = (struct iphdr *)(skb_network_header(skb) + 4);
240 if (tpriv->packet->tcp) {
241 if (ihdr->protocol != IPPROTO_TCP)
244 thdr = (struct tcphdr *)((u8 *)ihdr + 4 * ihdr->ihl);
245 if (thdr->dest != htons(tpriv->packet->dport))
248 shdr = (struct stmmachdr *)((u8 *)thdr + sizeof(*thdr));
250 if (ihdr->protocol != IPPROTO_UDP)
253 uhdr = (struct udphdr *)((u8 *)ihdr + 4 * ihdr->ihl);
254 if (uhdr->dest != htons(tpriv->packet->dport))
257 shdr = (struct stmmachdr *)((u8 *)uhdr + sizeof(*uhdr));
260 if (shdr->magic != cpu_to_be64(STMMAC_TEST_PKT_MAGIC))
262 if (tpriv->packet->exp_hash && !skb->hash)
264 if (tpriv->packet->id != shdr->id)
268 complete(&tpriv->comp);
274 static int __stmmac_test_loopback(struct stmmac_priv *priv,
275 struct stmmac_packet_attrs *attr)
277 struct stmmac_test_priv *tpriv;
278 struct sk_buff *skb = NULL;
281 tpriv = kzalloc(sizeof(*tpriv), GFP_KERNEL);
286 init_completion(&tpriv->comp);
288 tpriv->pt.type = htons(ETH_P_IP);
289 tpriv->pt.func = stmmac_test_loopback_validate;
290 tpriv->pt.dev = priv->dev;
291 tpriv->pt.af_packet_priv = tpriv;
292 tpriv->packet = attr;
293 dev_add_pack(&tpriv->pt);
295 skb = stmmac_test_get_udp_skb(priv, attr);
301 skb_set_queue_mapping(skb, 0);
302 ret = dev_queue_xmit(skb);
310 attr->timeout = STMMAC_LB_TIMEOUT;
312 wait_for_completion_timeout(&tpriv->comp, attr->timeout);
316 dev_remove_pack(&tpriv->pt);
321 static int stmmac_test_mac_loopback(struct stmmac_priv *priv)
323 struct stmmac_packet_attrs attr = { };
325 attr.dst = priv->dev->dev_addr;
326 return __stmmac_test_loopback(priv, &attr);
329 static int stmmac_test_phy_loopback(struct stmmac_priv *priv)
331 struct stmmac_packet_attrs attr = { };
334 if (!priv->dev->phydev)
337 ret = phy_loopback(priv->dev->phydev, true);
341 attr.dst = priv->dev->dev_addr;
342 ret = __stmmac_test_loopback(priv, &attr);
344 phy_loopback(priv->dev->phydev, false);
348 static int stmmac_test_mmc(struct stmmac_priv *priv)
350 struct stmmac_counters initial, final;
353 memset(&initial, 0, sizeof(initial));
354 memset(&final, 0, sizeof(final));
356 if (!priv->dma_cap.rmon)
359 /* Save previous results into internal struct */
360 stmmac_mmc_read(priv, priv->mmcaddr, &priv->mmc);
362 ret = stmmac_test_mac_loopback(priv);
366 /* These will be loopback results so no need to save them */
367 stmmac_mmc_read(priv, priv->mmcaddr, &final);
370 * The number of MMC counters available depends on HW configuration
371 * so we just use this one to validate the feature. I hope there is
372 * not a version without this counter.
374 if (final.mmc_tx_framecount_g <= initial.mmc_tx_framecount_g)
380 static int stmmac_test_eee(struct stmmac_priv *priv)
382 struct stmmac_extra_stats *initial, *final;
386 if (!priv->dma_cap.eee || !priv->eee_active)
389 initial = kzalloc(sizeof(*initial), GFP_KERNEL);
393 final = kzalloc(sizeof(*final), GFP_KERNEL);
396 goto out_free_initial;
399 memcpy(initial, &priv->xstats, sizeof(*initial));
401 ret = stmmac_test_mac_loopback(priv);
405 /* We have no traffic in the line so, sooner or later it will go LPI */
407 memcpy(final, &priv->xstats, sizeof(*final));
409 if (final->irq_tx_path_in_lpi_mode_n >
410 initial->irq_tx_path_in_lpi_mode_n)
420 if (final->irq_tx_path_in_lpi_mode_n <=
421 initial->irq_tx_path_in_lpi_mode_n) {
426 if (final->irq_tx_path_exit_lpi_mode_n <=
427 initial->irq_tx_path_exit_lpi_mode_n) {
439 static int stmmac_filter_check(struct stmmac_priv *priv)
441 if (!(priv->dev->flags & IFF_PROMISC))
444 netdev_warn(priv->dev, "Test can't be run in promiscuous mode!\n");
448 static int stmmac_test_hfilt(struct stmmac_priv *priv)
450 unsigned char gd_addr[ETH_ALEN] = {0x01, 0x00, 0xcc, 0xcc, 0xdd, 0xdd};
451 unsigned char bd_addr[ETH_ALEN] = {0x09, 0x00, 0xaa, 0xaa, 0xbb, 0xbb};
452 struct stmmac_packet_attrs attr = { };
455 ret = stmmac_filter_check(priv);
459 ret = dev_mc_add(priv->dev, gd_addr);
465 /* Shall receive packet */
466 ret = __stmmac_test_loopback(priv, &attr);
472 /* Shall NOT receive packet */
473 ret = __stmmac_test_loopback(priv, &attr);
477 dev_mc_del(priv->dev, gd_addr);
481 static int stmmac_test_pfilt(struct stmmac_priv *priv)
483 unsigned char gd_addr[ETH_ALEN] = {0x00, 0x01, 0x44, 0x55, 0x66, 0x77};
484 unsigned char bd_addr[ETH_ALEN] = {0x08, 0x00, 0x22, 0x33, 0x44, 0x55};
485 struct stmmac_packet_attrs attr = { };
488 if (stmmac_filter_check(priv))
491 ret = dev_uc_add(priv->dev, gd_addr);
497 /* Shall receive packet */
498 ret = __stmmac_test_loopback(priv, &attr);
504 /* Shall NOT receive packet */
505 ret = __stmmac_test_loopback(priv, &attr);
509 dev_uc_del(priv->dev, gd_addr);
513 static int stmmac_dummy_sync(struct net_device *netdev, const u8 *addr)
518 static void stmmac_test_set_rx_mode(struct net_device *netdev)
520 /* As we are in test mode of ethtool we already own the rtnl lock
521 * so no address will change from user. We can just call the
522 * ndo_set_rx_mode() callback directly */
523 if (netdev->netdev_ops->ndo_set_rx_mode)
524 netdev->netdev_ops->ndo_set_rx_mode(netdev);
527 static int stmmac_test_mcfilt(struct stmmac_priv *priv)
529 unsigned char uc_addr[ETH_ALEN] = {0x00, 0x01, 0x44, 0x55, 0x66, 0x77};
530 unsigned char mc_addr[ETH_ALEN] = {0x01, 0x01, 0x44, 0x55, 0x66, 0x77};
531 struct stmmac_packet_attrs attr = { };
534 if (stmmac_filter_check(priv))
537 /* Remove all MC addresses */
538 __dev_mc_unsync(priv->dev, NULL);
539 stmmac_test_set_rx_mode(priv->dev);
541 ret = dev_uc_add(priv->dev, uc_addr);
547 /* Shall receive packet */
548 ret = __stmmac_test_loopback(priv, &attr);
554 /* Shall NOT receive packet */
555 ret = __stmmac_test_loopback(priv, &attr);
559 dev_uc_del(priv->dev, uc_addr);
560 __dev_mc_sync(priv->dev, stmmac_dummy_sync, NULL);
561 stmmac_test_set_rx_mode(priv->dev);
565 static int stmmac_test_ucfilt(struct stmmac_priv *priv)
567 unsigned char uc_addr[ETH_ALEN] = {0x00, 0x01, 0x44, 0x55, 0x66, 0x77};
568 unsigned char mc_addr[ETH_ALEN] = {0x01, 0x01, 0x44, 0x55, 0x66, 0x77};
569 struct stmmac_packet_attrs attr = { };
572 if (stmmac_filter_check(priv))
575 /* Remove all UC addresses */
576 __dev_uc_unsync(priv->dev, NULL);
577 stmmac_test_set_rx_mode(priv->dev);
579 ret = dev_mc_add(priv->dev, mc_addr);
585 /* Shall receive packet */
586 ret = __stmmac_test_loopback(priv, &attr);
592 /* Shall NOT receive packet */
593 ret = __stmmac_test_loopback(priv, &attr);
597 dev_mc_del(priv->dev, mc_addr);
598 __dev_uc_sync(priv->dev, stmmac_dummy_sync, NULL);
599 stmmac_test_set_rx_mode(priv->dev);
603 static int stmmac_test_flowctrl_validate(struct sk_buff *skb,
604 struct net_device *ndev,
605 struct packet_type *pt,
606 struct net_device *orig_ndev)
608 struct stmmac_test_priv *tpriv = pt->af_packet_priv;
611 ehdr = (struct ethhdr *)skb_mac_header(skb);
612 if (!ether_addr_equal(ehdr->h_source, orig_ndev->dev_addr))
614 if (ehdr->h_proto != htons(ETH_P_PAUSE))
618 complete(&tpriv->comp);
624 static int stmmac_test_flowctrl(struct stmmac_priv *priv)
626 unsigned char paddr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x01};
627 struct phy_device *phydev = priv->dev->phydev;
628 u32 rx_cnt = priv->plat->rx_queues_to_use;
629 struct stmmac_test_priv *tpriv;
630 unsigned int pkt_count;
633 if (!phydev || !phydev->pause)
636 tpriv = kzalloc(sizeof(*tpriv), GFP_KERNEL);
641 init_completion(&tpriv->comp);
642 tpriv->pt.type = htons(ETH_P_PAUSE);
643 tpriv->pt.func = stmmac_test_flowctrl_validate;
644 tpriv->pt.dev = priv->dev;
645 tpriv->pt.af_packet_priv = tpriv;
646 dev_add_pack(&tpriv->pt);
648 /* Compute minimum number of packets to make FIFO full */
649 pkt_count = priv->plat->rx_fifo_size;
651 pkt_count = priv->dma_cap.rx_fifo_size;
655 for (i = 0; i < rx_cnt; i++)
656 stmmac_stop_rx(priv, priv->ioaddr, i);
658 ret = dev_set_promiscuity(priv->dev, 1);
662 ret = dev_mc_add(priv->dev, paddr);
666 for (i = 0; i < pkt_count; i++) {
667 struct stmmac_packet_attrs attr = { };
669 attr.dst = priv->dev->dev_addr;
670 attr.dont_wait = true;
673 ret = __stmmac_test_loopback(priv, &attr);
680 /* Wait for some time in case RX Watchdog is enabled */
683 for (i = 0; i < rx_cnt; i++) {
684 struct stmmac_channel *ch = &priv->channel[i];
686 stmmac_start_rx(priv, priv->ioaddr, i);
688 napi_reschedule(&ch->rx_napi);
692 wait_for_completion_timeout(&tpriv->comp, STMMAC_LB_TIMEOUT);
696 dev_mc_del(priv->dev, paddr);
697 dev_set_promiscuity(priv->dev, -1);
698 dev_remove_pack(&tpriv->pt);
703 #define STMMAC_LOOPBACK_NONE 0
704 #define STMMAC_LOOPBACK_MAC 1
705 #define STMMAC_LOOPBACK_PHY 2
707 static const struct stmmac_test {
708 char name[ETH_GSTRING_LEN];
710 int (*fn)(struct stmmac_priv *priv);
711 } stmmac_selftests[] = {
713 .name = "MAC Loopback ",
714 .lb = STMMAC_LOOPBACK_MAC,
715 .fn = stmmac_test_mac_loopback,
717 .name = "PHY Loopback ",
718 .lb = STMMAC_LOOPBACK_NONE, /* Test will handle it */
719 .fn = stmmac_test_phy_loopback,
721 .name = "MMC Counters ",
722 .lb = STMMAC_LOOPBACK_PHY,
723 .fn = stmmac_test_mmc,
726 .lb = STMMAC_LOOPBACK_PHY,
727 .fn = stmmac_test_eee,
729 .name = "Hash Filter MC ",
730 .lb = STMMAC_LOOPBACK_PHY,
731 .fn = stmmac_test_hfilt,
733 .name = "Perfect Filter UC ",
734 .lb = STMMAC_LOOPBACK_PHY,
735 .fn = stmmac_test_pfilt,
737 .name = "MC Filter ",
738 .lb = STMMAC_LOOPBACK_PHY,
739 .fn = stmmac_test_mcfilt,
741 .name = "UC Filter ",
742 .lb = STMMAC_LOOPBACK_PHY,
743 .fn = stmmac_test_ucfilt,
745 .name = "Flow Control ",
746 .lb = STMMAC_LOOPBACK_PHY,
747 .fn = stmmac_test_flowctrl,
751 void stmmac_selftest_run(struct net_device *dev,
752 struct ethtool_test *etest, u64 *buf)
754 struct stmmac_priv *priv = netdev_priv(dev);
755 int count = stmmac_selftest_get_count(priv);
756 int carrier = netif_carrier_ok(dev);
759 memset(buf, 0, sizeof(*buf) * count);
760 stmmac_test_next_id = 0;
762 if (etest->flags != ETH_TEST_FL_OFFLINE) {
763 netdev_err(priv->dev, "Only offline tests are supported\n");
764 etest->flags |= ETH_TEST_FL_FAILED;
766 } else if (!carrier) {
767 netdev_err(priv->dev, "You need valid Link to execute tests\n");
768 etest->flags |= ETH_TEST_FL_FAILED;
772 /* We don't want extra traffic */
773 netif_carrier_off(dev);
775 /* Wait for queues drain */
778 for (i = 0; i < count; i++) {
781 switch (stmmac_selftests[i].lb) {
782 case STMMAC_LOOPBACK_PHY:
785 ret = phy_loopback(dev->phydev, true);
789 case STMMAC_LOOPBACK_MAC:
790 ret = stmmac_set_mac_loopback(priv, priv->ioaddr, true);
792 case STMMAC_LOOPBACK_NONE:
800 * First tests will always be MAC / PHY loobpack. If any of
801 * them is not supported we abort earlier.
804 netdev_err(priv->dev, "Loopback is not supported\n");
805 etest->flags |= ETH_TEST_FL_FAILED;
809 ret = stmmac_selftests[i].fn(priv);
810 if (ret && (ret != -EOPNOTSUPP))
811 etest->flags |= ETH_TEST_FL_FAILED;
814 switch (stmmac_selftests[i].lb) {
815 case STMMAC_LOOPBACK_PHY:
818 ret = phy_loopback(dev->phydev, false);
822 case STMMAC_LOOPBACK_MAC:
823 stmmac_set_mac_loopback(priv, priv->ioaddr, false);
830 /* Restart everything */
832 netif_carrier_on(dev);
835 void stmmac_selftest_get_strings(struct stmmac_priv *priv, u8 *data)
840 for (i = 0; i < stmmac_selftest_get_count(priv); i++) {
841 snprintf(p, ETH_GSTRING_LEN, "%2d. %s", i + 1,
842 stmmac_selftests[i].name);
843 p += ETH_GSTRING_LEN;
847 int stmmac_selftest_get_count(struct stmmac_priv *priv)
849 return ARRAY_SIZE(stmmac_selftests);