Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (c) 2019 Synopsys, Inc. and/or its affiliates.
0004  * stmmac Selftests Support
0005  *
0006  * Author: Jose Abreu <joabreu@synopsys.com>
0007  */
0008 
0009 #include <linux/bitrev.h>
0010 #include <linux/completion.h>
0011 #include <linux/crc32.h>
0012 #include <linux/ethtool.h>
0013 #include <linux/ip.h>
0014 #include <linux/phy.h>
0015 #include <linux/udp.h>
0016 #include <net/pkt_cls.h>
0017 #include <net/pkt_sched.h>
0018 #include <net/tcp.h>
0019 #include <net/udp.h>
0020 #include <net/tc_act/tc_gact.h>
0021 #include "stmmac.h"
0022 
0023 struct stmmachdr {
0024     __be32 version;
0025     __be64 magic;
0026     u8 id;
0027 } __packed;
0028 
0029 #define STMMAC_TEST_PKT_SIZE (sizeof(struct ethhdr) + sizeof(struct iphdr) + \
0030                   sizeof(struct stmmachdr))
0031 #define STMMAC_TEST_PKT_MAGIC   0xdeadcafecafedeadULL
0032 #define STMMAC_LB_TIMEOUT   msecs_to_jiffies(200)
0033 
0034 struct stmmac_packet_attrs {
0035     int vlan;
0036     int vlan_id_in;
0037     int vlan_id_out;
0038     unsigned char *src;
0039     const unsigned char *dst;
0040     u32 ip_src;
0041     u32 ip_dst;
0042     int tcp;
0043     int sport;
0044     int dport;
0045     u32 exp_hash;
0046     int dont_wait;
0047     int timeout;
0048     int size;
0049     int max_size;
0050     int remove_sa;
0051     u8 id;
0052     int sarc;
0053     u16 queue_mapping;
0054     u64 timestamp;
0055 };
0056 
0057 static u8 stmmac_test_next_id;
0058 
0059 static struct sk_buff *stmmac_test_get_udp_skb(struct stmmac_priv *priv,
0060                            struct stmmac_packet_attrs *attr)
0061 {
0062     struct sk_buff *skb = NULL;
0063     struct udphdr *uhdr = NULL;
0064     struct tcphdr *thdr = NULL;
0065     struct stmmachdr *shdr;
0066     struct ethhdr *ehdr;
0067     struct iphdr *ihdr;
0068     int iplen, size;
0069 
0070     size = attr->size + STMMAC_TEST_PKT_SIZE;
0071     if (attr->vlan) {
0072         size += 4;
0073         if (attr->vlan > 1)
0074             size += 4;
0075     }
0076 
0077     if (attr->tcp)
0078         size += sizeof(struct tcphdr);
0079     else
0080         size += sizeof(struct udphdr);
0081 
0082     if (attr->max_size && (attr->max_size > size))
0083         size = attr->max_size;
0084 
0085     skb = netdev_alloc_skb(priv->dev, size);
0086     if (!skb)
0087         return NULL;
0088 
0089     prefetchw(skb->data);
0090 
0091     if (attr->vlan > 1)
0092         ehdr = skb_push(skb, ETH_HLEN + 8);
0093     else if (attr->vlan)
0094         ehdr = skb_push(skb, ETH_HLEN + 4);
0095     else if (attr->remove_sa)
0096         ehdr = skb_push(skb, ETH_HLEN - 6);
0097     else
0098         ehdr = skb_push(skb, ETH_HLEN);
0099     skb_reset_mac_header(skb);
0100 
0101     skb_set_network_header(skb, skb->len);
0102     ihdr = skb_put(skb, sizeof(*ihdr));
0103 
0104     skb_set_transport_header(skb, skb->len);
0105     if (attr->tcp)
0106         thdr = skb_put(skb, sizeof(*thdr));
0107     else
0108         uhdr = skb_put(skb, sizeof(*uhdr));
0109 
0110     if (!attr->remove_sa)
0111         eth_zero_addr(ehdr->h_source);
0112     eth_zero_addr(ehdr->h_dest);
0113     if (attr->src && !attr->remove_sa)
0114         ether_addr_copy(ehdr->h_source, attr->src);
0115     if (attr->dst)
0116         ether_addr_copy(ehdr->h_dest, attr->dst);
0117 
0118     if (!attr->remove_sa) {
0119         ehdr->h_proto = htons(ETH_P_IP);
0120     } else {
0121         __be16 *ptr = (__be16 *)ehdr;
0122 
0123         /* HACK */
0124         ptr[3] = htons(ETH_P_IP);
0125     }
0126 
0127     if (attr->vlan) {
0128         __be16 *tag, *proto;
0129 
0130         if (!attr->remove_sa) {
0131             tag = (void *)ehdr + ETH_HLEN;
0132             proto = (void *)ehdr + (2 * ETH_ALEN);
0133         } else {
0134             tag = (void *)ehdr + ETH_HLEN - 6;
0135             proto = (void *)ehdr + ETH_ALEN;
0136         }
0137 
0138         proto[0] = htons(ETH_P_8021Q);
0139         tag[0] = htons(attr->vlan_id_out);
0140         tag[1] = htons(ETH_P_IP);
0141         if (attr->vlan > 1) {
0142             proto[0] = htons(ETH_P_8021AD);
0143             tag[1] = htons(ETH_P_8021Q);
0144             tag[2] = htons(attr->vlan_id_in);
0145             tag[3] = htons(ETH_P_IP);
0146         }
0147     }
0148 
0149     if (attr->tcp) {
0150         thdr->source = htons(attr->sport);
0151         thdr->dest = htons(attr->dport);
0152         thdr->doff = sizeof(struct tcphdr) / 4;
0153         thdr->check = 0;
0154     } else {
0155         uhdr->source = htons(attr->sport);
0156         uhdr->dest = htons(attr->dport);
0157         uhdr->len = htons(sizeof(*shdr) + sizeof(*uhdr) + attr->size);
0158         if (attr->max_size)
0159             uhdr->len = htons(attr->max_size -
0160                       (sizeof(*ihdr) + sizeof(*ehdr)));
0161         uhdr->check = 0;
0162     }
0163 
0164     ihdr->ihl = 5;
0165     ihdr->ttl = 32;
0166     ihdr->version = 4;
0167     if (attr->tcp)
0168         ihdr->protocol = IPPROTO_TCP;
0169     else
0170         ihdr->protocol = IPPROTO_UDP;
0171     iplen = sizeof(*ihdr) + sizeof(*shdr) + attr->size;
0172     if (attr->tcp)
0173         iplen += sizeof(*thdr);
0174     else
0175         iplen += sizeof(*uhdr);
0176 
0177     if (attr->max_size)
0178         iplen = attr->max_size - sizeof(*ehdr);
0179 
0180     ihdr->tot_len = htons(iplen);
0181     ihdr->frag_off = 0;
0182     ihdr->saddr = htonl(attr->ip_src);
0183     ihdr->daddr = htonl(attr->ip_dst);
0184     ihdr->tos = 0;
0185     ihdr->id = 0;
0186     ip_send_check(ihdr);
0187 
0188     shdr = skb_put(skb, sizeof(*shdr));
0189     shdr->version = 0;
0190     shdr->magic = cpu_to_be64(STMMAC_TEST_PKT_MAGIC);
0191     attr->id = stmmac_test_next_id;
0192     shdr->id = stmmac_test_next_id++;
0193 
0194     if (attr->size)
0195         skb_put(skb, attr->size);
0196     if (attr->max_size && (attr->max_size > skb->len))
0197         skb_put(skb, attr->max_size - skb->len);
0198 
0199     skb->csum = 0;
0200     skb->ip_summed = CHECKSUM_PARTIAL;
0201     if (attr->tcp) {
0202         thdr->check = ~tcp_v4_check(skb->len, ihdr->saddr, ihdr->daddr, 0);
0203         skb->csum_start = skb_transport_header(skb) - skb->head;
0204         skb->csum_offset = offsetof(struct tcphdr, check);
0205     } else {
0206         udp4_hwcsum(skb, ihdr->saddr, ihdr->daddr);
0207     }
0208 
0209     skb->protocol = htons(ETH_P_IP);
0210     skb->pkt_type = PACKET_HOST;
0211     skb->dev = priv->dev;
0212 
0213     if (attr->timestamp)
0214         skb->tstamp = ns_to_ktime(attr->timestamp);
0215 
0216     return skb;
0217 }
0218 
0219 static struct sk_buff *stmmac_test_get_arp_skb(struct stmmac_priv *priv,
0220                            struct stmmac_packet_attrs *attr)
0221 {
0222     __be32 ip_src = htonl(attr->ip_src);
0223     __be32 ip_dst = htonl(attr->ip_dst);
0224     struct sk_buff *skb = NULL;
0225 
0226     skb = arp_create(ARPOP_REQUEST, ETH_P_ARP, ip_dst, priv->dev, ip_src,
0227              NULL, attr->src, attr->dst);
0228     if (!skb)
0229         return NULL;
0230 
0231     skb->pkt_type = PACKET_HOST;
0232     skb->dev = priv->dev;
0233 
0234     return skb;
0235 }
0236 
0237 struct stmmac_test_priv {
0238     struct stmmac_packet_attrs *packet;
0239     struct packet_type pt;
0240     struct completion comp;
0241     int double_vlan;
0242     int vlan_id;
0243     int ok;
0244 };
0245 
0246 static int stmmac_test_loopback_validate(struct sk_buff *skb,
0247                      struct net_device *ndev,
0248                      struct packet_type *pt,
0249                      struct net_device *orig_ndev)
0250 {
0251     struct stmmac_test_priv *tpriv = pt->af_packet_priv;
0252     const unsigned char *dst = tpriv->packet->dst;
0253     unsigned char *src = tpriv->packet->src;
0254     struct stmmachdr *shdr;
0255     struct ethhdr *ehdr;
0256     struct udphdr *uhdr;
0257     struct tcphdr *thdr;
0258     struct iphdr *ihdr;
0259 
0260     skb = skb_unshare(skb, GFP_ATOMIC);
0261     if (!skb)
0262         goto out;
0263 
0264     if (skb_linearize(skb))
0265         goto out;
0266     if (skb_headlen(skb) < (STMMAC_TEST_PKT_SIZE - ETH_HLEN))
0267         goto out;
0268 
0269     ehdr = (struct ethhdr *)skb_mac_header(skb);
0270     if (dst) {
0271         if (!ether_addr_equal_unaligned(ehdr->h_dest, dst))
0272             goto out;
0273     }
0274     if (tpriv->packet->sarc) {
0275         if (!ether_addr_equal_unaligned(ehdr->h_source, ehdr->h_dest))
0276             goto out;
0277     } else if (src) {
0278         if (!ether_addr_equal_unaligned(ehdr->h_source, src))
0279             goto out;
0280     }
0281 
0282     ihdr = ip_hdr(skb);
0283     if (tpriv->double_vlan)
0284         ihdr = (struct iphdr *)(skb_network_header(skb) + 4);
0285 
0286     if (tpriv->packet->tcp) {
0287         if (ihdr->protocol != IPPROTO_TCP)
0288             goto out;
0289 
0290         thdr = (struct tcphdr *)((u8 *)ihdr + 4 * ihdr->ihl);
0291         if (thdr->dest != htons(tpriv->packet->dport))
0292             goto out;
0293 
0294         shdr = (struct stmmachdr *)((u8 *)thdr + sizeof(*thdr));
0295     } else {
0296         if (ihdr->protocol != IPPROTO_UDP)
0297             goto out;
0298 
0299         uhdr = (struct udphdr *)((u8 *)ihdr + 4 * ihdr->ihl);
0300         if (uhdr->dest != htons(tpriv->packet->dport))
0301             goto out;
0302 
0303         shdr = (struct stmmachdr *)((u8 *)uhdr + sizeof(*uhdr));
0304     }
0305 
0306     if (shdr->magic != cpu_to_be64(STMMAC_TEST_PKT_MAGIC))
0307         goto out;
0308     if (tpriv->packet->exp_hash && !skb->hash)
0309         goto out;
0310     if (tpriv->packet->id != shdr->id)
0311         goto out;
0312 
0313     tpriv->ok = true;
0314     complete(&tpriv->comp);
0315 out:
0316     kfree_skb(skb);
0317     return 0;
0318 }
0319 
0320 static int __stmmac_test_loopback(struct stmmac_priv *priv,
0321                   struct stmmac_packet_attrs *attr)
0322 {
0323     struct stmmac_test_priv *tpriv;
0324     struct sk_buff *skb = NULL;
0325     int ret = 0;
0326 
0327     tpriv = kzalloc(sizeof(*tpriv), GFP_KERNEL);
0328     if (!tpriv)
0329         return -ENOMEM;
0330 
0331     tpriv->ok = false;
0332     init_completion(&tpriv->comp);
0333 
0334     tpriv->pt.type = htons(ETH_P_IP);
0335     tpriv->pt.func = stmmac_test_loopback_validate;
0336     tpriv->pt.dev = priv->dev;
0337     tpriv->pt.af_packet_priv = tpriv;
0338     tpriv->packet = attr;
0339 
0340     if (!attr->dont_wait)
0341         dev_add_pack(&tpriv->pt);
0342 
0343     skb = stmmac_test_get_udp_skb(priv, attr);
0344     if (!skb) {
0345         ret = -ENOMEM;
0346         goto cleanup;
0347     }
0348 
0349     ret = dev_direct_xmit(skb, attr->queue_mapping);
0350     if (ret)
0351         goto cleanup;
0352 
0353     if (attr->dont_wait)
0354         goto cleanup;
0355 
0356     if (!attr->timeout)
0357         attr->timeout = STMMAC_LB_TIMEOUT;
0358 
0359     wait_for_completion_timeout(&tpriv->comp, attr->timeout);
0360     ret = tpriv->ok ? 0 : -ETIMEDOUT;
0361 
0362 cleanup:
0363     if (!attr->dont_wait)
0364         dev_remove_pack(&tpriv->pt);
0365     kfree(tpriv);
0366     return ret;
0367 }
0368 
0369 static int stmmac_test_mac_loopback(struct stmmac_priv *priv)
0370 {
0371     struct stmmac_packet_attrs attr = { };
0372 
0373     attr.dst = priv->dev->dev_addr;
0374     return __stmmac_test_loopback(priv, &attr);
0375 }
0376 
0377 static int stmmac_test_phy_loopback(struct stmmac_priv *priv)
0378 {
0379     struct stmmac_packet_attrs attr = { };
0380     int ret;
0381 
0382     if (!priv->dev->phydev)
0383         return -EOPNOTSUPP;
0384 
0385     ret = phy_loopback(priv->dev->phydev, true);
0386     if (ret)
0387         return ret;
0388 
0389     attr.dst = priv->dev->dev_addr;
0390     ret = __stmmac_test_loopback(priv, &attr);
0391 
0392     phy_loopback(priv->dev->phydev, false);
0393     return ret;
0394 }
0395 
0396 static int stmmac_test_mmc(struct stmmac_priv *priv)
0397 {
0398     struct stmmac_counters initial, final;
0399     int ret;
0400 
0401     memset(&initial, 0, sizeof(initial));
0402     memset(&final, 0, sizeof(final));
0403 
0404     if (!priv->dma_cap.rmon)
0405         return -EOPNOTSUPP;
0406 
0407     /* Save previous results into internal struct */
0408     stmmac_mmc_read(priv, priv->mmcaddr, &priv->mmc);
0409 
0410     ret = stmmac_test_mac_loopback(priv);
0411     if (ret)
0412         return ret;
0413 
0414     /* These will be loopback results so no need to save them */
0415     stmmac_mmc_read(priv, priv->mmcaddr, &final);
0416 
0417     /*
0418      * The number of MMC counters available depends on HW configuration
0419      * so we just use this one to validate the feature. I hope there is
0420      * not a version without this counter.
0421      */
0422     if (final.mmc_tx_framecount_g <= initial.mmc_tx_framecount_g)
0423         return -EINVAL;
0424 
0425     return 0;
0426 }
0427 
0428 static int stmmac_test_eee(struct stmmac_priv *priv)
0429 {
0430     struct stmmac_extra_stats *initial, *final;
0431     int retries = 10;
0432     int ret;
0433 
0434     if (!priv->dma_cap.eee || !priv->eee_active)
0435         return -EOPNOTSUPP;
0436 
0437     initial = kzalloc(sizeof(*initial), GFP_KERNEL);
0438     if (!initial)
0439         return -ENOMEM;
0440 
0441     final = kzalloc(sizeof(*final), GFP_KERNEL);
0442     if (!final) {
0443         ret = -ENOMEM;
0444         goto out_free_initial;
0445     }
0446 
0447     memcpy(initial, &priv->xstats, sizeof(*initial));
0448 
0449     ret = stmmac_test_mac_loopback(priv);
0450     if (ret)
0451         goto out_free_final;
0452 
0453     /* We have no traffic in the line so, sooner or later it will go LPI */
0454     while (--retries) {
0455         memcpy(final, &priv->xstats, sizeof(*final));
0456 
0457         if (final->irq_tx_path_in_lpi_mode_n >
0458             initial->irq_tx_path_in_lpi_mode_n)
0459             break;
0460         msleep(100);
0461     }
0462 
0463     if (!retries) {
0464         ret = -ETIMEDOUT;
0465         goto out_free_final;
0466     }
0467 
0468     if (final->irq_tx_path_in_lpi_mode_n <=
0469         initial->irq_tx_path_in_lpi_mode_n) {
0470         ret = -EINVAL;
0471         goto out_free_final;
0472     }
0473 
0474     if (final->irq_tx_path_exit_lpi_mode_n <=
0475         initial->irq_tx_path_exit_lpi_mode_n) {
0476         ret = -EINVAL;
0477         goto out_free_final;
0478     }
0479 
0480 out_free_final:
0481     kfree(final);
0482 out_free_initial:
0483     kfree(initial);
0484     return ret;
0485 }
0486 
0487 static int stmmac_filter_check(struct stmmac_priv *priv)
0488 {
0489     if (!(priv->dev->flags & IFF_PROMISC))
0490         return 0;
0491 
0492     netdev_warn(priv->dev, "Test can't be run in promiscuous mode!\n");
0493     return -EOPNOTSUPP;
0494 }
0495 
0496 static bool stmmac_hash_check(struct stmmac_priv *priv, unsigned char *addr)
0497 {
0498     int mc_offset = 32 - priv->hw->mcast_bits_log2;
0499     struct netdev_hw_addr *ha;
0500     u32 hash, hash_nr;
0501 
0502     /* First compute the hash for desired addr */
0503     hash = bitrev32(~crc32_le(~0, addr, 6)) >> mc_offset;
0504     hash_nr = hash >> 5;
0505     hash = 1 << (hash & 0x1f);
0506 
0507     /* Now, check if it collides with any existing one */
0508     netdev_for_each_mc_addr(ha, priv->dev) {
0509         u32 nr = bitrev32(~crc32_le(~0, ha->addr, ETH_ALEN)) >> mc_offset;
0510         if (((nr >> 5) == hash_nr) && ((1 << (nr & 0x1f)) == hash))
0511             return false;
0512     }
0513 
0514     /* No collisions, address is good to go */
0515     return true;
0516 }
0517 
0518 static bool stmmac_perfect_check(struct stmmac_priv *priv, unsigned char *addr)
0519 {
0520     struct netdev_hw_addr *ha;
0521 
0522     /* Check if it collides with any existing one */
0523     netdev_for_each_uc_addr(ha, priv->dev) {
0524         if (!memcmp(ha->addr, addr, ETH_ALEN))
0525             return false;
0526     }
0527 
0528     /* No collisions, address is good to go */
0529     return true;
0530 }
0531 
0532 static int stmmac_test_hfilt(struct stmmac_priv *priv)
0533 {
0534     unsigned char gd_addr[ETH_ALEN] = {0xf1, 0xee, 0xdd, 0xcc, 0xbb, 0xaa};
0535     unsigned char bd_addr[ETH_ALEN] = {0xf1, 0xff, 0xff, 0xff, 0xff, 0xff};
0536     struct stmmac_packet_attrs attr = { };
0537     int ret, tries = 256;
0538 
0539     ret = stmmac_filter_check(priv);
0540     if (ret)
0541         return ret;
0542 
0543     if (netdev_mc_count(priv->dev) >= priv->hw->multicast_filter_bins)
0544         return -EOPNOTSUPP;
0545 
0546     while (--tries) {
0547         /* We only need to check the bd_addr for collisions */
0548         bd_addr[ETH_ALEN - 1] = tries;
0549         if (stmmac_hash_check(priv, bd_addr))
0550             break;
0551     }
0552 
0553     if (!tries)
0554         return -EOPNOTSUPP;
0555 
0556     ret = dev_mc_add(priv->dev, gd_addr);
0557     if (ret)
0558         return ret;
0559 
0560     attr.dst = gd_addr;
0561 
0562     /* Shall receive packet */
0563     ret = __stmmac_test_loopback(priv, &attr);
0564     if (ret)
0565         goto cleanup;
0566 
0567     attr.dst = bd_addr;
0568 
0569     /* Shall NOT receive packet */
0570     ret = __stmmac_test_loopback(priv, &attr);
0571     ret = ret ? 0 : -EINVAL;
0572 
0573 cleanup:
0574     dev_mc_del(priv->dev, gd_addr);
0575     return ret;
0576 }
0577 
0578 static int stmmac_test_pfilt(struct stmmac_priv *priv)
0579 {
0580     unsigned char gd_addr[ETH_ALEN] = {0xf0, 0x01, 0x44, 0x55, 0x66, 0x77};
0581     unsigned char bd_addr[ETH_ALEN] = {0xf0, 0xff, 0xff, 0xff, 0xff, 0xff};
0582     struct stmmac_packet_attrs attr = { };
0583     int ret, tries = 256;
0584 
0585     if (stmmac_filter_check(priv))
0586         return -EOPNOTSUPP;
0587     if (netdev_uc_count(priv->dev) >= priv->hw->unicast_filter_entries)
0588         return -EOPNOTSUPP;
0589 
0590     while (--tries) {
0591         /* We only need to check the bd_addr for collisions */
0592         bd_addr[ETH_ALEN - 1] = tries;
0593         if (stmmac_perfect_check(priv, bd_addr))
0594             break;
0595     }
0596 
0597     if (!tries)
0598         return -EOPNOTSUPP;
0599 
0600     ret = dev_uc_add(priv->dev, gd_addr);
0601     if (ret)
0602         return ret;
0603 
0604     attr.dst = gd_addr;
0605 
0606     /* Shall receive packet */
0607     ret = __stmmac_test_loopback(priv, &attr);
0608     if (ret)
0609         goto cleanup;
0610 
0611     attr.dst = bd_addr;
0612 
0613     /* Shall NOT receive packet */
0614     ret = __stmmac_test_loopback(priv, &attr);
0615     ret = ret ? 0 : -EINVAL;
0616 
0617 cleanup:
0618     dev_uc_del(priv->dev, gd_addr);
0619     return ret;
0620 }
0621 
0622 static int stmmac_test_mcfilt(struct stmmac_priv *priv)
0623 {
0624     unsigned char uc_addr[ETH_ALEN] = {0xf0, 0xff, 0xff, 0xff, 0xff, 0xff};
0625     unsigned char mc_addr[ETH_ALEN] = {0xf1, 0xff, 0xff, 0xff, 0xff, 0xff};
0626     struct stmmac_packet_attrs attr = { };
0627     int ret, tries = 256;
0628 
0629     if (stmmac_filter_check(priv))
0630         return -EOPNOTSUPP;
0631     if (netdev_uc_count(priv->dev) >= priv->hw->unicast_filter_entries)
0632         return -EOPNOTSUPP;
0633     if (netdev_mc_count(priv->dev) >= priv->hw->multicast_filter_bins)
0634         return -EOPNOTSUPP;
0635 
0636     while (--tries) {
0637         /* We only need to check the mc_addr for collisions */
0638         mc_addr[ETH_ALEN - 1] = tries;
0639         if (stmmac_hash_check(priv, mc_addr))
0640             break;
0641     }
0642 
0643     if (!tries)
0644         return -EOPNOTSUPP;
0645 
0646     ret = dev_uc_add(priv->dev, uc_addr);
0647     if (ret)
0648         return ret;
0649 
0650     attr.dst = uc_addr;
0651 
0652     /* Shall receive packet */
0653     ret = __stmmac_test_loopback(priv, &attr);
0654     if (ret)
0655         goto cleanup;
0656 
0657     attr.dst = mc_addr;
0658 
0659     /* Shall NOT receive packet */
0660     ret = __stmmac_test_loopback(priv, &attr);
0661     ret = ret ? 0 : -EINVAL;
0662 
0663 cleanup:
0664     dev_uc_del(priv->dev, uc_addr);
0665     return ret;
0666 }
0667 
0668 static int stmmac_test_ucfilt(struct stmmac_priv *priv)
0669 {
0670     unsigned char uc_addr[ETH_ALEN] = {0xf0, 0xff, 0xff, 0xff, 0xff, 0xff};
0671     unsigned char mc_addr[ETH_ALEN] = {0xf1, 0xff, 0xff, 0xff, 0xff, 0xff};
0672     struct stmmac_packet_attrs attr = { };
0673     int ret, tries = 256;
0674 
0675     if (stmmac_filter_check(priv))
0676         return -EOPNOTSUPP;
0677     if (netdev_uc_count(priv->dev) >= priv->hw->unicast_filter_entries)
0678         return -EOPNOTSUPP;
0679     if (netdev_mc_count(priv->dev) >= priv->hw->multicast_filter_bins)
0680         return -EOPNOTSUPP;
0681 
0682     while (--tries) {
0683         /* We only need to check the uc_addr for collisions */
0684         uc_addr[ETH_ALEN - 1] = tries;
0685         if (stmmac_perfect_check(priv, uc_addr))
0686             break;
0687     }
0688 
0689     if (!tries)
0690         return -EOPNOTSUPP;
0691 
0692     ret = dev_mc_add(priv->dev, mc_addr);
0693     if (ret)
0694         return ret;
0695 
0696     attr.dst = mc_addr;
0697 
0698     /* Shall receive packet */
0699     ret = __stmmac_test_loopback(priv, &attr);
0700     if (ret)
0701         goto cleanup;
0702 
0703     attr.dst = uc_addr;
0704 
0705     /* Shall NOT receive packet */
0706     ret = __stmmac_test_loopback(priv, &attr);
0707     ret = ret ? 0 : -EINVAL;
0708 
0709 cleanup:
0710     dev_mc_del(priv->dev, mc_addr);
0711     return ret;
0712 }
0713 
0714 static int stmmac_test_flowctrl_validate(struct sk_buff *skb,
0715                      struct net_device *ndev,
0716                      struct packet_type *pt,
0717                      struct net_device *orig_ndev)
0718 {
0719     struct stmmac_test_priv *tpriv = pt->af_packet_priv;
0720     struct ethhdr *ehdr;
0721 
0722     ehdr = (struct ethhdr *)skb_mac_header(skb);
0723     if (!ether_addr_equal_unaligned(ehdr->h_source, orig_ndev->dev_addr))
0724         goto out;
0725     if (ehdr->h_proto != htons(ETH_P_PAUSE))
0726         goto out;
0727 
0728     tpriv->ok = true;
0729     complete(&tpriv->comp);
0730 out:
0731     kfree_skb(skb);
0732     return 0;
0733 }
0734 
0735 static int stmmac_test_flowctrl(struct stmmac_priv *priv)
0736 {
0737     unsigned char paddr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x01};
0738     struct phy_device *phydev = priv->dev->phydev;
0739     u32 rx_cnt = priv->plat->rx_queues_to_use;
0740     struct stmmac_test_priv *tpriv;
0741     unsigned int pkt_count;
0742     int i, ret = 0;
0743 
0744     if (!phydev || (!phydev->pause && !phydev->asym_pause))
0745         return -EOPNOTSUPP;
0746 
0747     tpriv = kzalloc(sizeof(*tpriv), GFP_KERNEL);
0748     if (!tpriv)
0749         return -ENOMEM;
0750 
0751     tpriv->ok = false;
0752     init_completion(&tpriv->comp);
0753     tpriv->pt.type = htons(ETH_P_PAUSE);
0754     tpriv->pt.func = stmmac_test_flowctrl_validate;
0755     tpriv->pt.dev = priv->dev;
0756     tpriv->pt.af_packet_priv = tpriv;
0757     dev_add_pack(&tpriv->pt);
0758 
0759     /* Compute minimum number of packets to make FIFO full */
0760     pkt_count = priv->plat->rx_fifo_size;
0761     if (!pkt_count)
0762         pkt_count = priv->dma_cap.rx_fifo_size;
0763     pkt_count /= 1400;
0764     pkt_count *= 2;
0765 
0766     for (i = 0; i < rx_cnt; i++)
0767         stmmac_stop_rx(priv, priv->ioaddr, i);
0768 
0769     ret = dev_set_promiscuity(priv->dev, 1);
0770     if (ret)
0771         goto cleanup;
0772 
0773     ret = dev_mc_add(priv->dev, paddr);
0774     if (ret)
0775         goto cleanup;
0776 
0777     for (i = 0; i < pkt_count; i++) {
0778         struct stmmac_packet_attrs attr = { };
0779 
0780         attr.dst = priv->dev->dev_addr;
0781         attr.dont_wait = true;
0782         attr.size = 1400;
0783 
0784         ret = __stmmac_test_loopback(priv, &attr);
0785         if (ret)
0786             goto cleanup;
0787         if (tpriv->ok)
0788             break;
0789     }
0790 
0791     /* Wait for some time in case RX Watchdog is enabled */
0792     msleep(200);
0793 
0794     for (i = 0; i < rx_cnt; i++) {
0795         struct stmmac_channel *ch = &priv->channel[i];
0796         u32 tail;
0797 
0798         tail = priv->dma_conf.rx_queue[i].dma_rx_phy +
0799             (priv->dma_conf.dma_rx_size * sizeof(struct dma_desc));
0800 
0801         stmmac_set_rx_tail_ptr(priv, priv->ioaddr, tail, i);
0802         stmmac_start_rx(priv, priv->ioaddr, i);
0803 
0804         local_bh_disable();
0805         napi_reschedule(&ch->rx_napi);
0806         local_bh_enable();
0807     }
0808 
0809     wait_for_completion_timeout(&tpriv->comp, STMMAC_LB_TIMEOUT);
0810     ret = tpriv->ok ? 0 : -ETIMEDOUT;
0811 
0812 cleanup:
0813     dev_mc_del(priv->dev, paddr);
0814     dev_set_promiscuity(priv->dev, -1);
0815     dev_remove_pack(&tpriv->pt);
0816     kfree(tpriv);
0817     return ret;
0818 }
0819 
0820 static int stmmac_test_rss(struct stmmac_priv *priv)
0821 {
0822     struct stmmac_packet_attrs attr = { };
0823 
0824     if (!priv->dma_cap.rssen || !priv->rss.enable)
0825         return -EOPNOTSUPP;
0826 
0827     attr.dst = priv->dev->dev_addr;
0828     attr.exp_hash = true;
0829     attr.sport = 0x321;
0830     attr.dport = 0x123;
0831 
0832     return __stmmac_test_loopback(priv, &attr);
0833 }
0834 
0835 static int stmmac_test_vlan_validate(struct sk_buff *skb,
0836                      struct net_device *ndev,
0837                      struct packet_type *pt,
0838                      struct net_device *orig_ndev)
0839 {
0840     struct stmmac_test_priv *tpriv = pt->af_packet_priv;
0841     struct stmmachdr *shdr;
0842     struct ethhdr *ehdr;
0843     struct udphdr *uhdr;
0844     struct iphdr *ihdr;
0845     u16 proto;
0846 
0847     proto = tpriv->double_vlan ? ETH_P_8021AD : ETH_P_8021Q;
0848 
0849     skb = skb_unshare(skb, GFP_ATOMIC);
0850     if (!skb)
0851         goto out;
0852 
0853     if (skb_linearize(skb))
0854         goto out;
0855     if (skb_headlen(skb) < (STMMAC_TEST_PKT_SIZE - ETH_HLEN))
0856         goto out;
0857     if (tpriv->vlan_id) {
0858         if (skb->vlan_proto != htons(proto))
0859             goto out;
0860         if (skb->vlan_tci != tpriv->vlan_id) {
0861             /* Means filter did not work. */
0862             tpriv->ok = false;
0863             complete(&tpriv->comp);
0864             goto out;
0865         }
0866     }
0867 
0868     ehdr = (struct ethhdr *)skb_mac_header(skb);
0869     if (!ether_addr_equal_unaligned(ehdr->h_dest, tpriv->packet->dst))
0870         goto out;
0871 
0872     ihdr = ip_hdr(skb);
0873     if (tpriv->double_vlan)
0874         ihdr = (struct iphdr *)(skb_network_header(skb) + 4);
0875     if (ihdr->protocol != IPPROTO_UDP)
0876         goto out;
0877 
0878     uhdr = (struct udphdr *)((u8 *)ihdr + 4 * ihdr->ihl);
0879     if (uhdr->dest != htons(tpriv->packet->dport))
0880         goto out;
0881 
0882     shdr = (struct stmmachdr *)((u8 *)uhdr + sizeof(*uhdr));
0883     if (shdr->magic != cpu_to_be64(STMMAC_TEST_PKT_MAGIC))
0884         goto out;
0885 
0886     tpriv->ok = true;
0887     complete(&tpriv->comp);
0888 
0889 out:
0890     kfree_skb(skb);
0891     return 0;
0892 }
0893 
0894 static int __stmmac_test_vlanfilt(struct stmmac_priv *priv)
0895 {
0896     struct stmmac_packet_attrs attr = { };
0897     struct stmmac_test_priv *tpriv;
0898     struct sk_buff *skb = NULL;
0899     int ret = 0, i;
0900 
0901     tpriv = kzalloc(sizeof(*tpriv), GFP_KERNEL);
0902     if (!tpriv)
0903         return -ENOMEM;
0904 
0905     tpriv->ok = false;
0906     init_completion(&tpriv->comp);
0907 
0908     tpriv->pt.type = htons(ETH_P_IP);
0909     tpriv->pt.func = stmmac_test_vlan_validate;
0910     tpriv->pt.dev = priv->dev;
0911     tpriv->pt.af_packet_priv = tpriv;
0912     tpriv->packet = &attr;
0913 
0914     /*
0915      * As we use HASH filtering, false positives may appear. This is a
0916      * specially chosen ID so that adjacent IDs (+4) have different
0917      * HASH values.
0918      */
0919     tpriv->vlan_id = 0x123;
0920     dev_add_pack(&tpriv->pt);
0921 
0922     ret = vlan_vid_add(priv->dev, htons(ETH_P_8021Q), tpriv->vlan_id);
0923     if (ret)
0924         goto cleanup;
0925 
0926     for (i = 0; i < 4; i++) {
0927         attr.vlan = 1;
0928         attr.vlan_id_out = tpriv->vlan_id + i;
0929         attr.dst = priv->dev->dev_addr;
0930         attr.sport = 9;
0931         attr.dport = 9;
0932 
0933         skb = stmmac_test_get_udp_skb(priv, &attr);
0934         if (!skb) {
0935             ret = -ENOMEM;
0936             goto vlan_del;
0937         }
0938 
0939         ret = dev_direct_xmit(skb, 0);
0940         if (ret)
0941             goto vlan_del;
0942 
0943         wait_for_completion_timeout(&tpriv->comp, STMMAC_LB_TIMEOUT);
0944         ret = tpriv->ok ? 0 : -ETIMEDOUT;
0945         if (ret && !i) {
0946             goto vlan_del;
0947         } else if (!ret && i) {
0948             ret = -EINVAL;
0949             goto vlan_del;
0950         } else {
0951             ret = 0;
0952         }
0953 
0954         tpriv->ok = false;
0955     }
0956 
0957 vlan_del:
0958     vlan_vid_del(priv->dev, htons(ETH_P_8021Q), tpriv->vlan_id);
0959 cleanup:
0960     dev_remove_pack(&tpriv->pt);
0961     kfree(tpriv);
0962     return ret;
0963 }
0964 
0965 static int stmmac_test_vlanfilt(struct stmmac_priv *priv)
0966 {
0967     if (!priv->dma_cap.vlhash)
0968         return -EOPNOTSUPP;
0969 
0970     return __stmmac_test_vlanfilt(priv);
0971 }
0972 
0973 static int stmmac_test_vlanfilt_perfect(struct stmmac_priv *priv)
0974 {
0975     int ret, prev_cap = priv->dma_cap.vlhash;
0976 
0977     if (!(priv->dev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
0978         return -EOPNOTSUPP;
0979 
0980     priv->dma_cap.vlhash = 0;
0981     ret = __stmmac_test_vlanfilt(priv);
0982     priv->dma_cap.vlhash = prev_cap;
0983 
0984     return ret;
0985 }
0986 
0987 static int __stmmac_test_dvlanfilt(struct stmmac_priv *priv)
0988 {
0989     struct stmmac_packet_attrs attr = { };
0990     struct stmmac_test_priv *tpriv;
0991     struct sk_buff *skb = NULL;
0992     int ret = 0, i;
0993 
0994     tpriv = kzalloc(sizeof(*tpriv), GFP_KERNEL);
0995     if (!tpriv)
0996         return -ENOMEM;
0997 
0998     tpriv->ok = false;
0999     tpriv->double_vlan = true;
1000     init_completion(&tpriv->comp);
1001 
1002     tpriv->pt.type = htons(ETH_P_8021Q);
1003     tpriv->pt.func = stmmac_test_vlan_validate;
1004     tpriv->pt.dev = priv->dev;
1005     tpriv->pt.af_packet_priv = tpriv;
1006     tpriv->packet = &attr;
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     dev_add_pack(&tpriv->pt);
1015 
1016     ret = vlan_vid_add(priv->dev, htons(ETH_P_8021AD), tpriv->vlan_id);
1017     if (ret)
1018         goto cleanup;
1019 
1020     for (i = 0; i < 4; i++) {
1021         attr.vlan = 2;
1022         attr.vlan_id_out = tpriv->vlan_id + i;
1023         attr.dst = priv->dev->dev_addr;
1024         attr.sport = 9;
1025         attr.dport = 9;
1026 
1027         skb = stmmac_test_get_udp_skb(priv, &attr);
1028         if (!skb) {
1029             ret = -ENOMEM;
1030             goto vlan_del;
1031         }
1032 
1033         ret = dev_direct_xmit(skb, 0);
1034         if (ret)
1035             goto vlan_del;
1036 
1037         wait_for_completion_timeout(&tpriv->comp, STMMAC_LB_TIMEOUT);
1038         ret = tpriv->ok ? 0 : -ETIMEDOUT;
1039         if (ret && !i) {
1040             goto vlan_del;
1041         } else if (!ret && i) {
1042             ret = -EINVAL;
1043             goto vlan_del;
1044         } else {
1045             ret = 0;
1046         }
1047 
1048         tpriv->ok = false;
1049     }
1050 
1051 vlan_del:
1052     vlan_vid_del(priv->dev, htons(ETH_P_8021AD), tpriv->vlan_id);
1053 cleanup:
1054     dev_remove_pack(&tpriv->pt);
1055     kfree(tpriv);
1056     return ret;
1057 }
1058 
1059 static int stmmac_test_dvlanfilt(struct stmmac_priv *priv)
1060 {
1061     if (!priv->dma_cap.vlhash)
1062         return -EOPNOTSUPP;
1063 
1064     return __stmmac_test_dvlanfilt(priv);
1065 }
1066 
1067 static int stmmac_test_dvlanfilt_perfect(struct stmmac_priv *priv)
1068 {
1069     int ret, prev_cap = priv->dma_cap.vlhash;
1070 
1071     if (!(priv->dev->features & NETIF_F_HW_VLAN_STAG_FILTER))
1072         return -EOPNOTSUPP;
1073 
1074     priv->dma_cap.vlhash = 0;
1075     ret = __stmmac_test_dvlanfilt(priv);
1076     priv->dma_cap.vlhash = prev_cap;
1077 
1078     return ret;
1079 }
1080 
1081 #ifdef CONFIG_NET_CLS_ACT
1082 static int stmmac_test_rxp(struct stmmac_priv *priv)
1083 {
1084     unsigned char addr[ETH_ALEN] = {0xde, 0xad, 0xbe, 0xef, 0x00, 0x00};
1085     struct tc_cls_u32_offload cls_u32 = { };
1086     struct stmmac_packet_attrs attr = { };
1087     struct tc_action **actions;
1088     struct tc_u32_sel *sel;
1089     struct tcf_gact *gact;
1090     struct tcf_exts *exts;
1091     int ret, i, nk = 1;
1092 
1093     if (!tc_can_offload(priv->dev))
1094         return -EOPNOTSUPP;
1095     if (!priv->dma_cap.frpsel)
1096         return -EOPNOTSUPP;
1097 
1098     sel = kzalloc(struct_size(sel, keys, nk), GFP_KERNEL);
1099     if (!sel)
1100         return -ENOMEM;
1101 
1102     exts = kzalloc(sizeof(*exts), GFP_KERNEL);
1103     if (!exts) {
1104         ret = -ENOMEM;
1105         goto cleanup_sel;
1106     }
1107 
1108     actions = kcalloc(nk, sizeof(*actions), GFP_KERNEL);
1109     if (!actions) {
1110         ret = -ENOMEM;
1111         goto cleanup_exts;
1112     }
1113 
1114     gact = kcalloc(nk, sizeof(*gact), GFP_KERNEL);
1115     if (!gact) {
1116         ret = -ENOMEM;
1117         goto cleanup_actions;
1118     }
1119 
1120     cls_u32.command = TC_CLSU32_NEW_KNODE;
1121     cls_u32.common.chain_index = 0;
1122     cls_u32.common.protocol = htons(ETH_P_ALL);
1123     cls_u32.knode.exts = exts;
1124     cls_u32.knode.sel = sel;
1125     cls_u32.knode.handle = 0x123;
1126 
1127     exts->nr_actions = nk;
1128     exts->actions = actions;
1129     for (i = 0; i < nk; i++) {
1130         actions[i] = (struct tc_action *)&gact[i];
1131         gact->tcf_action = TC_ACT_SHOT;
1132     }
1133 
1134     sel->nkeys = nk;
1135     sel->offshift = 0;
1136     sel->keys[0].off = 6;
1137     sel->keys[0].val = htonl(0xdeadbeef);
1138     sel->keys[0].mask = ~0x0;
1139 
1140     ret = stmmac_tc_setup_cls_u32(priv, priv, &cls_u32);
1141     if (ret)
1142         goto cleanup_act;
1143 
1144     attr.dst = priv->dev->dev_addr;
1145     attr.src = addr;
1146 
1147     ret = __stmmac_test_loopback(priv, &attr);
1148     ret = ret ? 0 : -EINVAL; /* Shall NOT receive packet */
1149 
1150     cls_u32.command = TC_CLSU32_DELETE_KNODE;
1151     stmmac_tc_setup_cls_u32(priv, priv, &cls_u32);
1152 
1153 cleanup_act:
1154     kfree(gact);
1155 cleanup_actions:
1156     kfree(actions);
1157 cleanup_exts:
1158     kfree(exts);
1159 cleanup_sel:
1160     kfree(sel);
1161     return ret;
1162 }
1163 #else
1164 static int stmmac_test_rxp(struct stmmac_priv *priv)
1165 {
1166     return -EOPNOTSUPP;
1167 }
1168 #endif
1169 
1170 static int stmmac_test_desc_sai(struct stmmac_priv *priv)
1171 {
1172     unsigned char src[ETH_ALEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1173     struct stmmac_packet_attrs attr = { };
1174     int ret;
1175 
1176     if (!priv->dma_cap.vlins)
1177         return -EOPNOTSUPP;
1178 
1179     attr.remove_sa = true;
1180     attr.sarc = true;
1181     attr.src = src;
1182     attr.dst = priv->dev->dev_addr;
1183 
1184     priv->sarc_type = 0x1;
1185 
1186     ret = __stmmac_test_loopback(priv, &attr);
1187 
1188     priv->sarc_type = 0x0;
1189     return ret;
1190 }
1191 
1192 static int stmmac_test_desc_sar(struct stmmac_priv *priv)
1193 {
1194     unsigned char src[ETH_ALEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1195     struct stmmac_packet_attrs attr = { };
1196     int ret;
1197 
1198     if (!priv->dma_cap.vlins)
1199         return -EOPNOTSUPP;
1200 
1201     attr.sarc = true;
1202     attr.src = src;
1203     attr.dst = priv->dev->dev_addr;
1204 
1205     priv->sarc_type = 0x2;
1206 
1207     ret = __stmmac_test_loopback(priv, &attr);
1208 
1209     priv->sarc_type = 0x0;
1210     return ret;
1211 }
1212 
1213 static int stmmac_test_reg_sai(struct stmmac_priv *priv)
1214 {
1215     unsigned char src[ETH_ALEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1216     struct stmmac_packet_attrs attr = { };
1217     int ret;
1218 
1219     if (!priv->dma_cap.vlins)
1220         return -EOPNOTSUPP;
1221 
1222     attr.remove_sa = true;
1223     attr.sarc = true;
1224     attr.src = src;
1225     attr.dst = priv->dev->dev_addr;
1226 
1227     if (stmmac_sarc_configure(priv, priv->ioaddr, 0x2))
1228         return -EOPNOTSUPP;
1229 
1230     ret = __stmmac_test_loopback(priv, &attr);
1231 
1232     stmmac_sarc_configure(priv, priv->ioaddr, 0x0);
1233     return ret;
1234 }
1235 
1236 static int stmmac_test_reg_sar(struct stmmac_priv *priv)
1237 {
1238     unsigned char src[ETH_ALEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1239     struct stmmac_packet_attrs attr = { };
1240     int ret;
1241 
1242     if (!priv->dma_cap.vlins)
1243         return -EOPNOTSUPP;
1244 
1245     attr.sarc = true;
1246     attr.src = src;
1247     attr.dst = priv->dev->dev_addr;
1248 
1249     if (stmmac_sarc_configure(priv, priv->ioaddr, 0x3))
1250         return -EOPNOTSUPP;
1251 
1252     ret = __stmmac_test_loopback(priv, &attr);
1253 
1254     stmmac_sarc_configure(priv, priv->ioaddr, 0x0);
1255     return ret;
1256 }
1257 
1258 static int stmmac_test_vlanoff_common(struct stmmac_priv *priv, bool svlan)
1259 {
1260     struct stmmac_packet_attrs attr = { };
1261     struct stmmac_test_priv *tpriv;
1262     struct sk_buff *skb = NULL;
1263     int ret = 0;
1264     u16 proto;
1265 
1266     if (!priv->dma_cap.vlins)
1267         return -EOPNOTSUPP;
1268 
1269     tpriv = kzalloc(sizeof(*tpriv), GFP_KERNEL);
1270     if (!tpriv)
1271         return -ENOMEM;
1272 
1273     proto = svlan ? ETH_P_8021AD : ETH_P_8021Q;
1274 
1275     tpriv->ok = false;
1276     tpriv->double_vlan = svlan;
1277     init_completion(&tpriv->comp);
1278 
1279     tpriv->pt.type = svlan ? htons(ETH_P_8021Q) : htons(ETH_P_IP);
1280     tpriv->pt.func = stmmac_test_vlan_validate;
1281     tpriv->pt.dev = priv->dev;
1282     tpriv->pt.af_packet_priv = tpriv;
1283     tpriv->packet = &attr;
1284     tpriv->vlan_id = 0x123;
1285     dev_add_pack(&tpriv->pt);
1286 
1287     ret = vlan_vid_add(priv->dev, htons(proto), tpriv->vlan_id);
1288     if (ret)
1289         goto cleanup;
1290 
1291     attr.dst = priv->dev->dev_addr;
1292 
1293     skb = stmmac_test_get_udp_skb(priv, &attr);
1294     if (!skb) {
1295         ret = -ENOMEM;
1296         goto vlan_del;
1297     }
1298 
1299     __vlan_hwaccel_put_tag(skb, htons(proto), tpriv->vlan_id);
1300     skb->protocol = htons(proto);
1301 
1302     ret = dev_direct_xmit(skb, 0);
1303     if (ret)
1304         goto vlan_del;
1305 
1306     wait_for_completion_timeout(&tpriv->comp, STMMAC_LB_TIMEOUT);
1307     ret = tpriv->ok ? 0 : -ETIMEDOUT;
1308 
1309 vlan_del:
1310     vlan_vid_del(priv->dev, htons(proto), tpriv->vlan_id);
1311 cleanup:
1312     dev_remove_pack(&tpriv->pt);
1313     kfree(tpriv);
1314     return ret;
1315 }
1316 
1317 static int stmmac_test_vlanoff(struct stmmac_priv *priv)
1318 {
1319     return stmmac_test_vlanoff_common(priv, false);
1320 }
1321 
1322 static int stmmac_test_svlanoff(struct stmmac_priv *priv)
1323 {
1324     if (!priv->dma_cap.dvlan)
1325         return -EOPNOTSUPP;
1326     return stmmac_test_vlanoff_common(priv, true);
1327 }
1328 
1329 #ifdef CONFIG_NET_CLS_ACT
1330 static int __stmmac_test_l3filt(struct stmmac_priv *priv, u32 dst, u32 src,
1331                 u32 dst_mask, u32 src_mask)
1332 {
1333     struct flow_dissector_key_ipv4_addrs key, mask;
1334     unsigned long dummy_cookie = 0xdeadbeef;
1335     struct stmmac_packet_attrs attr = { };
1336     struct flow_dissector *dissector;
1337     struct flow_cls_offload *cls;
1338     int ret, old_enable = 0;
1339     struct flow_rule *rule;
1340 
1341     if (!tc_can_offload(priv->dev))
1342         return -EOPNOTSUPP;
1343     if (!priv->dma_cap.l3l4fnum)
1344         return -EOPNOTSUPP;
1345     if (priv->rss.enable) {
1346         old_enable = priv->rss.enable;
1347         priv->rss.enable = false;
1348         stmmac_rss_configure(priv, priv->hw, NULL,
1349                      priv->plat->rx_queues_to_use);
1350     }
1351 
1352     dissector = kzalloc(sizeof(*dissector), GFP_KERNEL);
1353     if (!dissector) {
1354         ret = -ENOMEM;
1355         goto cleanup_rss;
1356     }
1357 
1358     dissector->used_keys |= (1 << FLOW_DISSECTOR_KEY_IPV4_ADDRS);
1359     dissector->offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] = 0;
1360 
1361     cls = kzalloc(sizeof(*cls), GFP_KERNEL);
1362     if (!cls) {
1363         ret = -ENOMEM;
1364         goto cleanup_dissector;
1365     }
1366 
1367     cls->common.chain_index = 0;
1368     cls->command = FLOW_CLS_REPLACE;
1369     cls->cookie = dummy_cookie;
1370 
1371     rule = kzalloc(struct_size(rule, action.entries, 1), GFP_KERNEL);
1372     if (!rule) {
1373         ret = -ENOMEM;
1374         goto cleanup_cls;
1375     }
1376 
1377     rule->match.dissector = dissector;
1378     rule->match.key = (void *)&key;
1379     rule->match.mask = (void *)&mask;
1380 
1381     key.src = htonl(src);
1382     key.dst = htonl(dst);
1383     mask.src = src_mask;
1384     mask.dst = dst_mask;
1385 
1386     cls->rule = rule;
1387 
1388     rule->action.entries[0].id = FLOW_ACTION_DROP;
1389     rule->action.entries[0].hw_stats = FLOW_ACTION_HW_STATS_ANY;
1390     rule->action.num_entries = 1;
1391 
1392     attr.dst = priv->dev->dev_addr;
1393     attr.ip_dst = dst;
1394     attr.ip_src = src;
1395 
1396     /* Shall receive packet */
1397     ret = __stmmac_test_loopback(priv, &attr);
1398     if (ret)
1399         goto cleanup_rule;
1400 
1401     ret = stmmac_tc_setup_cls(priv, priv, cls);
1402     if (ret)
1403         goto cleanup_rule;
1404 
1405     /* Shall NOT receive packet */
1406     ret = __stmmac_test_loopback(priv, &attr);
1407     ret = ret ? 0 : -EINVAL;
1408 
1409     cls->command = FLOW_CLS_DESTROY;
1410     stmmac_tc_setup_cls(priv, priv, cls);
1411 cleanup_rule:
1412     kfree(rule);
1413 cleanup_cls:
1414     kfree(cls);
1415 cleanup_dissector:
1416     kfree(dissector);
1417 cleanup_rss:
1418     if (old_enable) {
1419         priv->rss.enable = old_enable;
1420         stmmac_rss_configure(priv, priv->hw, &priv->rss,
1421                      priv->plat->rx_queues_to_use);
1422     }
1423 
1424     return ret;
1425 }
1426 #else
1427 static int __stmmac_test_l3filt(struct stmmac_priv *priv, u32 dst, u32 src,
1428                 u32 dst_mask, u32 src_mask)
1429 {
1430     return -EOPNOTSUPP;
1431 }
1432 #endif
1433 
1434 static int stmmac_test_l3filt_da(struct stmmac_priv *priv)
1435 {
1436     u32 addr = 0x10203040;
1437 
1438     return __stmmac_test_l3filt(priv, addr, 0, ~0, 0);
1439 }
1440 
1441 static int stmmac_test_l3filt_sa(struct stmmac_priv *priv)
1442 {
1443     u32 addr = 0x10203040;
1444 
1445     return __stmmac_test_l3filt(priv, 0, addr, 0, ~0);
1446 }
1447 
1448 #ifdef CONFIG_NET_CLS_ACT
1449 static int __stmmac_test_l4filt(struct stmmac_priv *priv, u32 dst, u32 src,
1450                 u32 dst_mask, u32 src_mask, bool udp)
1451 {
1452     struct {
1453         struct flow_dissector_key_basic bkey;
1454         struct flow_dissector_key_ports key;
1455     } __aligned(BITS_PER_LONG / 8) keys;
1456     struct {
1457         struct flow_dissector_key_basic bmask;
1458         struct flow_dissector_key_ports mask;
1459     } __aligned(BITS_PER_LONG / 8) masks;
1460     unsigned long dummy_cookie = 0xdeadbeef;
1461     struct stmmac_packet_attrs attr = { };
1462     struct flow_dissector *dissector;
1463     struct flow_cls_offload *cls;
1464     int ret, old_enable = 0;
1465     struct flow_rule *rule;
1466 
1467     if (!tc_can_offload(priv->dev))
1468         return -EOPNOTSUPP;
1469     if (!priv->dma_cap.l3l4fnum)
1470         return -EOPNOTSUPP;
1471     if (priv->rss.enable) {
1472         old_enable = priv->rss.enable;
1473         priv->rss.enable = false;
1474         stmmac_rss_configure(priv, priv->hw, NULL,
1475                      priv->plat->rx_queues_to_use);
1476     }
1477 
1478     dissector = kzalloc(sizeof(*dissector), GFP_KERNEL);
1479     if (!dissector) {
1480         ret = -ENOMEM;
1481         goto cleanup_rss;
1482     }
1483 
1484     dissector->used_keys |= (1 << FLOW_DISSECTOR_KEY_BASIC);
1485     dissector->used_keys |= (1 << FLOW_DISSECTOR_KEY_PORTS);
1486     dissector->offset[FLOW_DISSECTOR_KEY_BASIC] = 0;
1487     dissector->offset[FLOW_DISSECTOR_KEY_PORTS] = offsetof(typeof(keys), key);
1488 
1489     cls = kzalloc(sizeof(*cls), GFP_KERNEL);
1490     if (!cls) {
1491         ret = -ENOMEM;
1492         goto cleanup_dissector;
1493     }
1494 
1495     cls->common.chain_index = 0;
1496     cls->command = FLOW_CLS_REPLACE;
1497     cls->cookie = dummy_cookie;
1498 
1499     rule = kzalloc(struct_size(rule, action.entries, 1), GFP_KERNEL);
1500     if (!rule) {
1501         ret = -ENOMEM;
1502         goto cleanup_cls;
1503     }
1504 
1505     rule->match.dissector = dissector;
1506     rule->match.key = (void *)&keys;
1507     rule->match.mask = (void *)&masks;
1508 
1509     keys.bkey.ip_proto = udp ? IPPROTO_UDP : IPPROTO_TCP;
1510     keys.key.src = htons(src);
1511     keys.key.dst = htons(dst);
1512     masks.mask.src = src_mask;
1513     masks.mask.dst = dst_mask;
1514 
1515     cls->rule = rule;
1516 
1517     rule->action.entries[0].id = FLOW_ACTION_DROP;
1518     rule->action.entries[0].hw_stats = FLOW_ACTION_HW_STATS_ANY;
1519     rule->action.num_entries = 1;
1520 
1521     attr.dst = priv->dev->dev_addr;
1522     attr.tcp = !udp;
1523     attr.sport = src;
1524     attr.dport = dst;
1525     attr.ip_dst = 0;
1526 
1527     /* Shall receive packet */
1528     ret = __stmmac_test_loopback(priv, &attr);
1529     if (ret)
1530         goto cleanup_rule;
1531 
1532     ret = stmmac_tc_setup_cls(priv, priv, cls);
1533     if (ret)
1534         goto cleanup_rule;
1535 
1536     /* Shall NOT receive packet */
1537     ret = __stmmac_test_loopback(priv, &attr);
1538     ret = ret ? 0 : -EINVAL;
1539 
1540     cls->command = FLOW_CLS_DESTROY;
1541     stmmac_tc_setup_cls(priv, priv, cls);
1542 cleanup_rule:
1543     kfree(rule);
1544 cleanup_cls:
1545     kfree(cls);
1546 cleanup_dissector:
1547     kfree(dissector);
1548 cleanup_rss:
1549     if (old_enable) {
1550         priv->rss.enable = old_enable;
1551         stmmac_rss_configure(priv, priv->hw, &priv->rss,
1552                      priv->plat->rx_queues_to_use);
1553     }
1554 
1555     return ret;
1556 }
1557 #else
1558 static int __stmmac_test_l4filt(struct stmmac_priv *priv, u32 dst, u32 src,
1559                 u32 dst_mask, u32 src_mask, bool udp)
1560 {
1561     return -EOPNOTSUPP;
1562 }
1563 #endif
1564 
1565 static int stmmac_test_l4filt_da_tcp(struct stmmac_priv *priv)
1566 {
1567     u16 dummy_port = 0x123;
1568 
1569     return __stmmac_test_l4filt(priv, dummy_port, 0, ~0, 0, false);
1570 }
1571 
1572 static int stmmac_test_l4filt_sa_tcp(struct stmmac_priv *priv)
1573 {
1574     u16 dummy_port = 0x123;
1575 
1576     return __stmmac_test_l4filt(priv, 0, dummy_port, 0, ~0, false);
1577 }
1578 
1579 static int stmmac_test_l4filt_da_udp(struct stmmac_priv *priv)
1580 {
1581     u16 dummy_port = 0x123;
1582 
1583     return __stmmac_test_l4filt(priv, dummy_port, 0, ~0, 0, true);
1584 }
1585 
1586 static int stmmac_test_l4filt_sa_udp(struct stmmac_priv *priv)
1587 {
1588     u16 dummy_port = 0x123;
1589 
1590     return __stmmac_test_l4filt(priv, 0, dummy_port, 0, ~0, true);
1591 }
1592 
1593 static int stmmac_test_arp_validate(struct sk_buff *skb,
1594                     struct net_device *ndev,
1595                     struct packet_type *pt,
1596                     struct net_device *orig_ndev)
1597 {
1598     struct stmmac_test_priv *tpriv = pt->af_packet_priv;
1599     struct ethhdr *ehdr;
1600     struct arphdr *ahdr;
1601 
1602     ehdr = (struct ethhdr *)skb_mac_header(skb);
1603     if (!ether_addr_equal_unaligned(ehdr->h_dest, tpriv->packet->src))
1604         goto out;
1605 
1606     ahdr = arp_hdr(skb);
1607     if (ahdr->ar_op != htons(ARPOP_REPLY))
1608         goto out;
1609 
1610     tpriv->ok = true;
1611     complete(&tpriv->comp);
1612 out:
1613     kfree_skb(skb);
1614     return 0;
1615 }
1616 
1617 static int stmmac_test_arpoffload(struct stmmac_priv *priv)
1618 {
1619     unsigned char src[ETH_ALEN] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
1620     unsigned char dst[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1621     struct stmmac_packet_attrs attr = { };
1622     struct stmmac_test_priv *tpriv;
1623     struct sk_buff *skb = NULL;
1624     u32 ip_addr = 0xdeadcafe;
1625     u32 ip_src = 0xdeadbeef;
1626     int ret;
1627 
1628     if (!priv->dma_cap.arpoffsel)
1629         return -EOPNOTSUPP;
1630 
1631     tpriv = kzalloc(sizeof(*tpriv), GFP_KERNEL);
1632     if (!tpriv)
1633         return -ENOMEM;
1634 
1635     tpriv->ok = false;
1636     init_completion(&tpriv->comp);
1637 
1638     tpriv->pt.type = htons(ETH_P_ARP);
1639     tpriv->pt.func = stmmac_test_arp_validate;
1640     tpriv->pt.dev = priv->dev;
1641     tpriv->pt.af_packet_priv = tpriv;
1642     tpriv->packet = &attr;
1643     dev_add_pack(&tpriv->pt);
1644 
1645     attr.src = src;
1646     attr.ip_src = ip_src;
1647     attr.dst = dst;
1648     attr.ip_dst = ip_addr;
1649 
1650     skb = stmmac_test_get_arp_skb(priv, &attr);
1651     if (!skb) {
1652         ret = -ENOMEM;
1653         goto cleanup;
1654     }
1655 
1656     ret = stmmac_set_arp_offload(priv, priv->hw, true, ip_addr);
1657     if (ret)
1658         goto cleanup;
1659 
1660     ret = dev_set_promiscuity(priv->dev, 1);
1661     if (ret)
1662         goto cleanup;
1663 
1664     ret = dev_direct_xmit(skb, 0);
1665     if (ret)
1666         goto cleanup_promisc;
1667 
1668     wait_for_completion_timeout(&tpriv->comp, STMMAC_LB_TIMEOUT);
1669     ret = tpriv->ok ? 0 : -ETIMEDOUT;
1670 
1671 cleanup_promisc:
1672     dev_set_promiscuity(priv->dev, -1);
1673 cleanup:
1674     stmmac_set_arp_offload(priv, priv->hw, false, 0x0);
1675     dev_remove_pack(&tpriv->pt);
1676     kfree(tpriv);
1677     return ret;
1678 }
1679 
1680 static int __stmmac_test_jumbo(struct stmmac_priv *priv, u16 queue)
1681 {
1682     struct stmmac_packet_attrs attr = { };
1683     int size = priv->dma_conf.dma_buf_sz;
1684 
1685     attr.dst = priv->dev->dev_addr;
1686     attr.max_size = size - ETH_FCS_LEN;
1687     attr.queue_mapping = queue;
1688 
1689     return __stmmac_test_loopback(priv, &attr);
1690 }
1691 
1692 static int stmmac_test_jumbo(struct stmmac_priv *priv)
1693 {
1694     return __stmmac_test_jumbo(priv, 0);
1695 }
1696 
1697 static int stmmac_test_mjumbo(struct stmmac_priv *priv)
1698 {
1699     u32 chan, tx_cnt = priv->plat->tx_queues_to_use;
1700     int ret;
1701 
1702     if (tx_cnt <= 1)
1703         return -EOPNOTSUPP;
1704 
1705     for (chan = 0; chan < tx_cnt; chan++) {
1706         ret = __stmmac_test_jumbo(priv, chan);
1707         if (ret)
1708             return ret;
1709     }
1710 
1711     return 0;
1712 }
1713 
1714 static int stmmac_test_sph(struct stmmac_priv *priv)
1715 {
1716     unsigned long cnt_end, cnt_start = priv->xstats.rx_split_hdr_pkt_n;
1717     struct stmmac_packet_attrs attr = { };
1718     int ret;
1719 
1720     if (!priv->sph)
1721         return -EOPNOTSUPP;
1722 
1723     /* Check for UDP first */
1724     attr.dst = priv->dev->dev_addr;
1725     attr.tcp = false;
1726 
1727     ret = __stmmac_test_loopback(priv, &attr);
1728     if (ret)
1729         return ret;
1730 
1731     cnt_end = priv->xstats.rx_split_hdr_pkt_n;
1732     if (cnt_end <= cnt_start)
1733         return -EINVAL;
1734 
1735     /* Check for TCP now */
1736     cnt_start = cnt_end;
1737 
1738     attr.dst = priv->dev->dev_addr;
1739     attr.tcp = true;
1740 
1741     ret = __stmmac_test_loopback(priv, &attr);
1742     if (ret)
1743         return ret;
1744 
1745     cnt_end = priv->xstats.rx_split_hdr_pkt_n;
1746     if (cnt_end <= cnt_start)
1747         return -EINVAL;
1748 
1749     return 0;
1750 }
1751 
1752 static int stmmac_test_tbs(struct stmmac_priv *priv)
1753 {
1754 #define STMMAC_TBS_LT_OFFSET        (500 * 1000 * 1000) /* 500 ms*/
1755     struct stmmac_packet_attrs attr = { };
1756     struct tc_etf_qopt_offload qopt;
1757     u64 start_time, curr_time = 0;
1758     unsigned long flags;
1759     int ret, i;
1760 
1761     if (!priv->hwts_tx_en)
1762         return -EOPNOTSUPP;
1763 
1764     /* Find first TBS enabled Queue, if any */
1765     for (i = 0; i < priv->plat->tx_queues_to_use; i++)
1766         if (priv->dma_conf.tx_queue[i].tbs & STMMAC_TBS_AVAIL)
1767             break;
1768 
1769     if (i >= priv->plat->tx_queues_to_use)
1770         return -EOPNOTSUPP;
1771 
1772     qopt.enable = true;
1773     qopt.queue = i;
1774 
1775     ret = stmmac_tc_setup_etf(priv, priv, &qopt);
1776     if (ret)
1777         return ret;
1778 
1779     read_lock_irqsave(&priv->ptp_lock, flags);
1780     stmmac_get_systime(priv, priv->ptpaddr, &curr_time);
1781     read_unlock_irqrestore(&priv->ptp_lock, flags);
1782 
1783     if (!curr_time) {
1784         ret = -EOPNOTSUPP;
1785         goto fail_disable;
1786     }
1787 
1788     start_time = curr_time;
1789     curr_time += STMMAC_TBS_LT_OFFSET;
1790 
1791     attr.dst = priv->dev->dev_addr;
1792     attr.timestamp = curr_time;
1793     attr.timeout = nsecs_to_jiffies(2 * STMMAC_TBS_LT_OFFSET);
1794     attr.queue_mapping = i;
1795 
1796     ret = __stmmac_test_loopback(priv, &attr);
1797     if (ret)
1798         goto fail_disable;
1799 
1800     /* Check if expected time has elapsed */
1801     read_lock_irqsave(&priv->ptp_lock, flags);
1802     stmmac_get_systime(priv, priv->ptpaddr, &curr_time);
1803     read_unlock_irqrestore(&priv->ptp_lock, flags);
1804 
1805     if ((curr_time - start_time) < STMMAC_TBS_LT_OFFSET)
1806         ret = -EINVAL;
1807 
1808 fail_disable:
1809     qopt.enable = false;
1810     stmmac_tc_setup_etf(priv, priv, &qopt);
1811     return ret;
1812 }
1813 
1814 #define STMMAC_LOOPBACK_NONE    0
1815 #define STMMAC_LOOPBACK_MAC 1
1816 #define STMMAC_LOOPBACK_PHY 2
1817 
1818 static const struct stmmac_test {
1819     char name[ETH_GSTRING_LEN];
1820     int lb;
1821     int (*fn)(struct stmmac_priv *priv);
1822 } stmmac_selftests[] = {
1823     {
1824         .name = "MAC Loopback               ",
1825         .lb = STMMAC_LOOPBACK_MAC,
1826         .fn = stmmac_test_mac_loopback,
1827     }, {
1828         .name = "PHY Loopback               ",
1829         .lb = STMMAC_LOOPBACK_NONE, /* Test will handle it */
1830         .fn = stmmac_test_phy_loopback,
1831     }, {
1832         .name = "MMC Counters               ",
1833         .lb = STMMAC_LOOPBACK_PHY,
1834         .fn = stmmac_test_mmc,
1835     }, {
1836         .name = "EEE                        ",
1837         .lb = STMMAC_LOOPBACK_PHY,
1838         .fn = stmmac_test_eee,
1839     }, {
1840         .name = "Hash Filter MC             ",
1841         .lb = STMMAC_LOOPBACK_PHY,
1842         .fn = stmmac_test_hfilt,
1843     }, {
1844         .name = "Perfect Filter UC          ",
1845         .lb = STMMAC_LOOPBACK_PHY,
1846         .fn = stmmac_test_pfilt,
1847     }, {
1848         .name = "MC Filter                  ",
1849         .lb = STMMAC_LOOPBACK_PHY,
1850         .fn = stmmac_test_mcfilt,
1851     }, {
1852         .name = "UC Filter                  ",
1853         .lb = STMMAC_LOOPBACK_PHY,
1854         .fn = stmmac_test_ucfilt,
1855     }, {
1856         .name = "Flow Control               ",
1857         .lb = STMMAC_LOOPBACK_PHY,
1858         .fn = stmmac_test_flowctrl,
1859     }, {
1860         .name = "RSS                        ",
1861         .lb = STMMAC_LOOPBACK_PHY,
1862         .fn = stmmac_test_rss,
1863     }, {
1864         .name = "VLAN Filtering             ",
1865         .lb = STMMAC_LOOPBACK_PHY,
1866         .fn = stmmac_test_vlanfilt,
1867     }, {
1868         .name = "VLAN Filtering (perf)      ",
1869         .lb = STMMAC_LOOPBACK_PHY,
1870         .fn = stmmac_test_vlanfilt_perfect,
1871     }, {
1872         .name = "Double VLAN Filter         ",
1873         .lb = STMMAC_LOOPBACK_PHY,
1874         .fn = stmmac_test_dvlanfilt,
1875     }, {
1876         .name = "Double VLAN Filter (perf)  ",
1877         .lb = STMMAC_LOOPBACK_PHY,
1878         .fn = stmmac_test_dvlanfilt_perfect,
1879     }, {
1880         .name = "Flexible RX Parser         ",
1881         .lb = STMMAC_LOOPBACK_PHY,
1882         .fn = stmmac_test_rxp,
1883     }, {
1884         .name = "SA Insertion (desc)        ",
1885         .lb = STMMAC_LOOPBACK_PHY,
1886         .fn = stmmac_test_desc_sai,
1887     }, {
1888         .name = "SA Replacement (desc)      ",
1889         .lb = STMMAC_LOOPBACK_PHY,
1890         .fn = stmmac_test_desc_sar,
1891     }, {
1892         .name = "SA Insertion (reg)         ",
1893         .lb = STMMAC_LOOPBACK_PHY,
1894         .fn = stmmac_test_reg_sai,
1895     }, {
1896         .name = "SA Replacement (reg)       ",
1897         .lb = STMMAC_LOOPBACK_PHY,
1898         .fn = stmmac_test_reg_sar,
1899     }, {
1900         .name = "VLAN TX Insertion          ",
1901         .lb = STMMAC_LOOPBACK_PHY,
1902         .fn = stmmac_test_vlanoff,
1903     }, {
1904         .name = "SVLAN TX Insertion         ",
1905         .lb = STMMAC_LOOPBACK_PHY,
1906         .fn = stmmac_test_svlanoff,
1907     }, {
1908         .name = "L3 DA Filtering            ",
1909         .lb = STMMAC_LOOPBACK_PHY,
1910         .fn = stmmac_test_l3filt_da,
1911     }, {
1912         .name = "L3 SA Filtering            ",
1913         .lb = STMMAC_LOOPBACK_PHY,
1914         .fn = stmmac_test_l3filt_sa,
1915     }, {
1916         .name = "L4 DA TCP Filtering        ",
1917         .lb = STMMAC_LOOPBACK_PHY,
1918         .fn = stmmac_test_l4filt_da_tcp,
1919     }, {
1920         .name = "L4 SA TCP Filtering        ",
1921         .lb = STMMAC_LOOPBACK_PHY,
1922         .fn = stmmac_test_l4filt_sa_tcp,
1923     }, {
1924         .name = "L4 DA UDP Filtering        ",
1925         .lb = STMMAC_LOOPBACK_PHY,
1926         .fn = stmmac_test_l4filt_da_udp,
1927     }, {
1928         .name = "L4 SA UDP Filtering        ",
1929         .lb = STMMAC_LOOPBACK_PHY,
1930         .fn = stmmac_test_l4filt_sa_udp,
1931     }, {
1932         .name = "ARP Offload                ",
1933         .lb = STMMAC_LOOPBACK_PHY,
1934         .fn = stmmac_test_arpoffload,
1935     }, {
1936         .name = "Jumbo Frame                ",
1937         .lb = STMMAC_LOOPBACK_PHY,
1938         .fn = stmmac_test_jumbo,
1939     }, {
1940         .name = "Multichannel Jumbo         ",
1941         .lb = STMMAC_LOOPBACK_PHY,
1942         .fn = stmmac_test_mjumbo,
1943     }, {
1944         .name = "Split Header               ",
1945         .lb = STMMAC_LOOPBACK_PHY,
1946         .fn = stmmac_test_sph,
1947     }, {
1948         .name = "TBS (ETF Scheduler)        ",
1949         .lb = STMMAC_LOOPBACK_PHY,
1950         .fn = stmmac_test_tbs,
1951     },
1952 };
1953 
1954 void stmmac_selftest_run(struct net_device *dev,
1955              struct ethtool_test *etest, u64 *buf)
1956 {
1957     struct stmmac_priv *priv = netdev_priv(dev);
1958     int count = stmmac_selftest_get_count(priv);
1959     int i, ret;
1960 
1961     memset(buf, 0, sizeof(*buf) * count);
1962     stmmac_test_next_id = 0;
1963 
1964     if (etest->flags != ETH_TEST_FL_OFFLINE) {
1965         netdev_err(priv->dev, "Only offline tests are supported\n");
1966         etest->flags |= ETH_TEST_FL_FAILED;
1967         return;
1968     } else if (!netif_carrier_ok(dev)) {
1969         netdev_err(priv->dev, "You need valid Link to execute tests\n");
1970         etest->flags |= ETH_TEST_FL_FAILED;
1971         return;
1972     }
1973 
1974     /* Wait for queues drain */
1975     msleep(200);
1976 
1977     for (i = 0; i < count; i++) {
1978         ret = 0;
1979 
1980         switch (stmmac_selftests[i].lb) {
1981         case STMMAC_LOOPBACK_PHY:
1982             ret = -EOPNOTSUPP;
1983             if (dev->phydev)
1984                 ret = phy_loopback(dev->phydev, true);
1985             if (!ret)
1986                 break;
1987             fallthrough;
1988         case STMMAC_LOOPBACK_MAC:
1989             ret = stmmac_set_mac_loopback(priv, priv->ioaddr, true);
1990             break;
1991         case STMMAC_LOOPBACK_NONE:
1992             break;
1993         default:
1994             ret = -EOPNOTSUPP;
1995             break;
1996         }
1997 
1998         /*
1999          * First tests will always be MAC / PHY loobpack. If any of
2000          * them is not supported we abort earlier.
2001          */
2002         if (ret) {
2003             netdev_err(priv->dev, "Loopback is not supported\n");
2004             etest->flags |= ETH_TEST_FL_FAILED;
2005             break;
2006         }
2007 
2008         ret = stmmac_selftests[i].fn(priv);
2009         if (ret && (ret != -EOPNOTSUPP))
2010             etest->flags |= ETH_TEST_FL_FAILED;
2011         buf[i] = ret;
2012 
2013         switch (stmmac_selftests[i].lb) {
2014         case STMMAC_LOOPBACK_PHY:
2015             ret = -EOPNOTSUPP;
2016             if (dev->phydev)
2017                 ret = phy_loopback(dev->phydev, false);
2018             if (!ret)
2019                 break;
2020             fallthrough;
2021         case STMMAC_LOOPBACK_MAC:
2022             stmmac_set_mac_loopback(priv, priv->ioaddr, false);
2023             break;
2024         default:
2025             break;
2026         }
2027     }
2028 }
2029 
2030 void stmmac_selftest_get_strings(struct stmmac_priv *priv, u8 *data)
2031 {
2032     u8 *p = data;
2033     int i;
2034 
2035     for (i = 0; i < stmmac_selftest_get_count(priv); i++) {
2036         snprintf(p, ETH_GSTRING_LEN, "%2d. %s", i + 1,
2037              stmmac_selftests[i].name);
2038         p += ETH_GSTRING_LEN;
2039     }
2040 }
2041 
2042 int stmmac_selftest_get_count(struct stmmac_priv *priv)
2043 {
2044     return ARRAY_SIZE(stmmac_selftests);
2045 }