123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968
#include <linux/bitrev.h>
#include <linux/completion.h>
#include <linux/crc32.h>
#include <linux/ethtool.h>
#include <linux/ip.h>
#include <linux/udp.h>
#include <net/dsa.h>
#include <net/pkt_cls.h>
#include <net/pkt_sched.h>
#include <net/tcp.h>
#include <net/udp.h>
#include <net/tc_act/tc_gact.h>
#include "stmmac.h"
struct stmmachdr {
__be32 version;
__be64 magic;
u8 id;
} __packed;
#define STMMAC_TEST_PKT_SIZE (sizeof(struct ethhdr) + sizeof(struct iphdr) + \
sizeof(struct stmmachdr))
#define STMMAC_TEST_PKT_MAGIC 0xdeadcafecafedeadULL
#define STMMAC_LB_TIMEOUT msecs_to_jiffies(200)
#define STMMAC_SFT_MAX_LPI (5 * USEC_PER_SEC)
struct stmmac_packet_attrs {
int vlan;
int vlan_id_in;
int vlan_id_out;
unsigned char *src;
const unsigned char *dst;
u32 ip_src;
u32 ip_dst;
int tcp;
int sport;
int dport;
u32 exp_hash;
int dont_wait;
int timeout;
int size;
int max_size;
int remove_sa;
u8 id;
int sarc;
u16 queue_mapping;
u64 timestamp;
};
static u8 stmmac_test_next_id;
static struct sk_buff *stmmac_test_get_udp_skb(struct stmmac_priv *priv,
struct stmmac_packet_attrs *attr)
{
struct sk_buff *skb = NULL;
struct udphdr *uhdr = NULL;
struct tcphdr *thdr = NULL;
struct stmmachdr *shdr;
struct ethhdr *ehdr;
struct iphdr *ihdr;
int iplen, size;
size = attr->size + STMMAC_TEST_PKT_SIZE;
if (attr->vlan) {
size += 4;
if (attr->vlan > 1)
size += 4;
}
if (attr->tcp)
size += sizeof(struct tcphdr);
else
size += sizeof(struct udphdr);
if (attr->max_size && (attr->max_size > size))
size = attr->max_size;
skb = netdev_alloc_skb(priv->dev, size);
if (!skb)
return NULL;
prefetchw(skb->data);
if (attr->vlan > 1)
ehdr = skb_push(skb, ETH_HLEN + 8);
else if (attr->vlan)
ehdr = skb_push(skb, ETH_HLEN + 4);
else if (attr->remove_sa)
ehdr = skb_push(skb, ETH_HLEN - 6);
else
ehdr = skb_push(skb, ETH_HLEN);
skb_reset_mac_header(skb);
skb_set_network_header(skb, skb->len);
ihdr = skb_put(skb, sizeof(*ihdr));
skb_set_transport_header(skb, skb->len);
if (attr->tcp)
thdr = skb_put(skb, sizeof(*thdr));
else
uhdr = skb_put(skb, sizeof(*uhdr));
if (!attr->remove_sa)
eth_zero_addr(ehdr->h_source);
eth_zero_addr(ehdr->h_dest);
if (attr->src && !attr->remove_sa)
ether_addr_copy(ehdr->h_source, attr->src);
if (attr->dst)
ether_addr_copy(ehdr->h_dest, attr->dst);
if (!attr->remove_sa) {
ehdr->h_proto = htons(ETH_P_IP);
} else {
__be16 *ptr = (__be16 *)ehdr;
ptr[3] = htons(ETH_P_IP);
}
if (attr->vlan) {
__be16 *tag, *proto;
if (!attr->remove_sa) {
tag = (void *)ehdr + ETH_HLEN;
proto = (void *)ehdr + (2 * ETH_ALEN);
} else {
tag = (void *)ehdr + ETH_HLEN - 6;
proto = (void *)ehdr + ETH_ALEN;
}
proto[0] = htons(ETH_P_8021Q);
tag[0] = htons(attr->vlan_id_out);
tag[1] = htons(ETH_P_IP);
if (attr->vlan > 1) {
proto[0] = htons(ETH_P_8021AD);
tag[1] = htons(ETH_P_8021Q);
tag[2] = htons(attr->vlan_id_in);
tag[3] = htons(ETH_P_IP);
}
}
if (attr->tcp) {
thdr->source = htons(attr->sport);
thdr->dest = htons(attr->dport);
thdr->doff = sizeof(struct tcphdr) / 4;
thdr->check = 0;
} else {
uhdr->source = htons(attr->sport);
uhdr->dest = htons(attr->dport);
udp_set_len_short(uhdr, sizeof(*shdr) + sizeof(*uhdr) + attr->size);
if (attr->max_size)
udp_set_len_short(uhdr, attr->max_size -
(sizeof(*ihdr) + sizeof(*ehdr)));
uhdr->check = 0;
}
ihdr->ihl = 5;
ihdr->ttl = 32;
ihdr->version = 4;
if (attr->tcp)
ihdr->protocol = IPPROTO_TCP;
else
ihdr->protocol = IPPROTO_UDP;
iplen = sizeof(*ihdr) + sizeof(*shdr) + attr->size;
if (attr->tcp)
iplen += sizeof(*thdr);
else
iplen += sizeof(*uhdr);
if (attr->max_size)
iplen = attr->max_size - sizeof(*ehdr);
ihdr->tot_len = htons(iplen);
ihdr->frag_off = 0;
ihdr->saddr = htonl(attr->ip_src);
ihdr->daddr = htonl(attr->ip_dst);
ihdr->tos = 0;
ihdr->id = 0;
ip_send_check(ihdr);
shdr = skb_put(skb, sizeof(*shdr));
shdr->version = 0;
shdr->magic = cpu_to_be64(STMMAC_TEST_PKT_MAGIC);
attr->id = stmmac_test_next_id;
shdr->id = stmmac_test_next_id++;
if (attr->size)
skb_put(skb, attr->size);
if (attr->max_size && (attr->max_size > skb->len))
skb_put(skb, attr->max_size - skb->len);
skb->csum = 0;
skb->ip_summed = CHECKSUM_PARTIAL;
if (attr->tcp) {
thdr->check = ~tcp_v4_check(skb->len, ihdr->saddr, ihdr->daddr, 0);
skb->csum_start = skb_transport_header(skb) - skb->head;
skb->csum_offset = offsetof(struct tcphdr, check);
} else {
udp4_hwcsum(skb, ihdr->saddr, ihdr->daddr);
}
skb->protocol = htons(ETH_P_IP);
skb->pkt_type = PACKET_HOST;
skb->dev = priv->dev;
if (attr->timestamp)
skb->tstamp = ns_to_ktime(attr->timestamp);
return skb;
}
struct stmmac_test_priv {
struct stmmac_packet_attrs *packet;
struct packet_type pt;
struct completion comp;
__be16 packet_type;
int (*func)(struct sk_buff *skb, struct net_device *ndev,
struct packet_type *pt, struct net_device *orig_ndev);
bool capture_all;
int svlan;
int vlan_id;
int ok;
};
static int stmmac_test_loopback_validate(struct sk_buff *skb,
struct net_device *ndev,
struct packet_type *pt,
struct net_device *orig_ndev)
{
struct stmmac_test_priv *tpriv = pt->af_packet_priv;
const unsigned char *dst = tpriv->packet->dst;
unsigned char *src = tpriv->packet->src;
struct stmmachdr *shdr;
struct ethhdr *ehdr;
struct udphdr *uhdr;
struct tcphdr *thdr;
struct iphdr *ihdr;
skb = skb_share_check(skb, GFP_ATOMIC);
if (!skb)
goto out;
if (skb_linearize(skb))
goto out;
if (skb_headlen(skb) < (STMMAC_TEST_PKT_SIZE - ETH_HLEN))
goto out;
ehdr = (struct ethhdr *)skb_mac_header(skb);
if (dst) {
if (!ether_addr_equal_unaligned(ehdr->h_dest, dst))
goto out;
}
if (tpriv->packet->sarc) {
if (!ether_addr_equal_unaligned(ehdr->h_source, ehdr->h_dest))
goto out;
} else if (src) {
if (!ether_addr_equal_unaligned(ehdr->h_source, src))
goto out;
}
ihdr = ip_hdr(skb);
if (tpriv->svlan)
ihdr = (struct iphdr *)(skb_network_header(skb) + 4);
if (tpriv->packet->tcp) {
if (ihdr->protocol != IPPROTO_TCP)
goto out;
thdr = (struct tcphdr *)((u8 *)ihdr + 4 * ihdr->ihl);
if (thdr->dest != htons(tpriv->packet->dport))
goto out;
shdr = (struct stmmachdr *)((u8 *)thdr + sizeof(*thdr));
} else {
if (ihdr->protocol != IPPROTO_UDP)
goto out;
uhdr = (struct udphdr *)((u8 *)ihdr + 4 * ihdr->ihl);
if (uhdr->dest != htons(tpriv->packet->dport))
goto out;
shdr = (struct stmmachdr *)((u8 *)uhdr + sizeof(*uhdr));
}
if (shdr->magic != cpu_to_be64(STMMAC_TEST_PKT_MAGIC))
goto out;
if (tpriv->packet->exp_hash && !skb->hash)
goto out;
if (tpriv->packet->id != shdr->id)
goto out;
tpriv->ok = true;
complete(&tpriv->comp);
out:
kfree_skb(skb);
return 0;
}
static int stmmac_sft_filter(struct sk_buff *skb, struct net_device *ndev,
struct packet_type *pt,
struct net_device *orig_ndev)
{
struct stmmac_test_priv *tpriv = pt->af_packet_priv;
struct ethhdr *hdr = eth_hdr(skb);
int ret = 0;
if (hdr->h_proto == tpriv->packet_type) {
struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
if (nskb)
ret = tpriv->func(nskb, ndev, pt, orig_ndev);
}
kfree_skb(skb);
return ret;
}
static void stmmac_sft_add_pack(struct packet_type *pt)
{
struct stmmac_test_priv *tpriv = pt->af_packet_priv;
if (netdev_uses_dsa(tpriv->pt.dev) || tpriv->capture_all) {
tpriv->packet_type = tpriv->pt.type;
tpriv->func = tpriv->pt.func;
tpriv->pt.type = htons(ETH_P_ALL);
tpriv->pt.func = stmmac_sft_filter;
tpriv->pt.ignore_outgoing = true;
}
dev_add_pack(pt);
}
static void stmmac_sft_remove_pack(struct packet_type *pt)
{
dev_remove_pack(pt);
}
static int __stmmac_test_loopback(struct stmmac_priv *priv,
struct stmmac_packet_attrs *attr)
{
struct stmmac_test_priv *tpriv;
struct sk_buff *skb = NULL;
int ret = 0;
tpriv = kzalloc_obj(*tpriv);
if (!tpriv)
return -ENOMEM;
tpriv->ok = false;
init_completion(&tpriv->comp);
tpriv->pt.type = htons(ETH_P_IP);
tpriv->pt.func = stmmac_test_loopback_validate;
tpriv->pt.dev = priv->dev;
tpriv->pt.af_packet_priv = tpriv;
tpriv->packet = attr;
if (!attr->dont_wait)
stmmac_sft_add_pack(&tpriv->pt);
skb = stmmac_test_get_udp_skb(priv, attr);
if (!skb) {
ret = -ENOMEM;
goto cleanup;
}
ret = dev_direct_xmit(skb, attr->queue_mapping);
if (ret)
goto cleanup;
if (attr->dont_wait)
goto cleanup;
if (!attr->timeout)
attr->timeout = STMMAC_LB_TIMEOUT;
wait_for_completion_timeout(&tpriv->comp, attr->timeout);
ret = tpriv->ok ? 0 : -ETIMEDOUT;
cleanup:
if (!attr->dont_wait)
stmmac_sft_remove_pack(&tpriv->pt);
kfree(tpriv);
return ret;
}
static int stmmac_test_mac_loopback(struct stmmac_priv *priv)
{
struct stmmac_packet_attrs attr = { };
attr.dst = priv->dev->dev_addr;
return __stmmac_test_loopback(priv, &attr);
}
static int stmmac_test_mmc(struct stmmac_priv *priv)
{
struct stmmac_counters initial, final;
int ret;
memset(&initial, 0, sizeof(initial));
memset(&final, 0, sizeof(final));
if (!priv->dma_cap.rmon)
return -EOPNOTSUPP;
stmmac_mmc_read(priv, priv->mmcaddr, &priv->mmc);
ret = stmmac_test_mac_loopback(priv);
if (ret)
return ret;
stmmac_mmc_read(priv, priv->mmcaddr, &final);
if (final.mmc_tx_framecount_g <= initial.mmc_tx_framecount_g &&
final.mmc_rx_framecount_gb <= initial.mmc_rx_framecount_gb)
return -EINVAL;
return 0;
}
static int stmmac_test_eee(struct stmmac_priv *priv)
{
struct stmmac_extra_stats *initial, *final;
unsigned long timeout, max_duration;
int ret;
if (!priv->dma_cap.eee || !priv->eee_active)
return -EOPNOTSUPP;
if (priv->tx_lpi_timer > STMMAC_SFT_MAX_LPI)
return -EOPNOTSUPP;
initial = kzalloc_obj(*initial);
if (!initial)
return -ENOMEM;
final = kzalloc_obj(*final);
if (!final) {
ret = -ENOMEM;
goto out_free_initial;
}
memcpy(initial, &priv->xstats, sizeof(*initial));
ret = stmmac_test_mac_loopback(priv);
if (ret)
goto out_free_final;
max_duration = usecs_to_jiffies(2 * priv->tx_lpi_timer);
timeout = jiffies + max_duration;
while (!time_after(jiffies, timeout)) {
memcpy(final, &priv->xstats, sizeof(*final));
if (final->irq_tx_path_in_lpi_mode_n >
initial->irq_tx_path_in_lpi_mode_n)
break;
msleep(100);
}
memcpy(final, &priv->xstats, sizeof(*final));
if (final->irq_tx_path_in_lpi_mode_n <=
initial->irq_tx_path_in_lpi_mode_n) {
ret = -ETIMEDOUT;
goto out_free_final;
}
memcpy(initial, &priv->xstats, sizeof(*initial));
ret = stmmac_test_mac_loopback(priv);
if (ret)
goto out_free_final;
timeout = jiffies + max_duration;
while (!time_after(jiffies, timeout)) {
memcpy(final, &priv->xstats, sizeof(*final));
if (final->irq_tx_path_exit_lpi_mode_n >
initial->irq_tx_path_exit_lpi_mode_n)
break;
msleep(100);
}
memcpy(final, &priv->xstats, sizeof(*final));
if (final->irq_tx_path_exit_lpi_mode_n <=
initial->irq_tx_path_exit_lpi_mode_n) {
ret = -ETIMEDOUT;
goto out_free_final;
}
out_free_final:
kfree(final);
out_free_initial:
kfree(initial);
return ret;
}
static int stmmac_filter_check(struct stmmac_priv *priv)
{
if (!(priv->dev->flags & IFF_PROMISC))
return 0;
netdev_warn(priv->dev, "Test can't be run in promiscuous mode!\n");
return -EOPNOTSUPP;
}
static int stmmac_uc_filter_check(struct stmmac_priv *priv)
{
if (netdev_uc_count(priv->dev) + 2 > priv->hw->unicast_filter_entries)
return -EOPNOTSUPP;
return 0;
}
static bool stmmac_hash_check(struct stmmac_priv *priv, unsigned char *addr)
{
int mc_offset = 32 - priv->hw->mcast_bits_log2;
struct netdev_hw_addr *ha;
u32 hash, hash_nr;
hash = bitrev32(~crc32_le(~0, addr, 6)) >> mc_offset;
hash_nr = hash >> 5;
hash = 1 << (hash & 0x1f);
netdev_for_each_mc_addr(ha, priv->dev) {
u32 nr = bitrev32(~crc32_le(~0, ha->addr, ETH_ALEN)) >> mc_offset;
if (((nr >> 5) == hash_nr) && ((1 << (nr & 0x1f)) == hash))
return false;
}
return true;
}
static bool stmmac_perfect_check(struct stmmac_priv *priv, unsigned char *addr)
{
struct netdev_hw_addr *ha;
netdev_for_each_uc_addr(ha, priv->dev) {
if (!memcmp(ha->addr, addr, ETH_ALEN))
return false;
}
return true;
}
static int stmmac_test_hfilt(struct stmmac_priv *priv)
{
unsigned char gd_addr[ETH_ALEN] = {0xf1, 0xee, 0xdd, 0xcc, 0xbb, 0xaa};
unsigned char bd_addr[ETH_ALEN] = {0xf1, 0xff, 0xff, 0xff, 0xff, 0xff};
struct stmmac_packet_attrs attr = { };
int ret, tries = 256;
ret = stmmac_filter_check(priv);
if (ret)
return ret;
if (netdev_mc_count(priv->dev) >= priv->hw->multicast_filter_bins)
return -EOPNOTSUPP;
while (--tries) {
bd_addr[ETH_ALEN - 1] = tries;
if (stmmac_hash_check(priv, bd_addr))
break;
}
if (!tries)
return -EOPNOTSUPP;
ret = dev_mc_add(priv->dev, gd_addr);
if (ret)
return ret;
attr.dst = gd_addr;
ret = __stmmac_test_loopback(priv, &attr);
if (ret)
goto cleanup;
attr.dst = bd_addr;
ret = __stmmac_test_loopback(priv, &attr);
ret = ret ? 0 : -EINVAL;
cleanup:
dev_mc_del(priv->dev, gd_addr);
return ret;
}
static int stmmac_test_pfilt(struct stmmac_priv *priv)
{
unsigned char gd_addr[ETH_ALEN] = {0xf0, 0x01, 0x44, 0x55, 0x66, 0x77};
unsigned char bd_addr[ETH_ALEN] = {0xf0, 0xff, 0xff, 0xff, 0xff, 0xff};
struct stmmac_packet_attrs attr = { };
int ret, tries = 256;
if (stmmac_filter_check(priv))
return -EOPNOTSUPP;
if (stmmac_uc_filter_check(priv))
return -EOPNOTSUPP;
while (--tries) {
bd_addr[ETH_ALEN - 1] = tries;
if (stmmac_perfect_check(priv, bd_addr))
break;
}
if (!tries)
return -EOPNOTSUPP;
ret = dev_uc_add(priv->dev, gd_addr);
if (ret)
return ret;
attr.dst = gd_addr;
ret = __stmmac_test_loopback(priv, &attr);
if (ret)
goto cleanup;
attr.dst = bd_addr;
ret = __stmmac_test_loopback(priv, &attr);
ret = ret ? 0 : -EINVAL;
cleanup:
dev_uc_del(priv->dev, gd_addr);
return ret;
}
static int stmmac_test_mcfilt(struct stmmac_priv *priv)
{
unsigned char uc_addr[ETH_ALEN] = {0xf0, 0xff, 0xff, 0xff, 0xff, 0xff};
unsigned char mc_addr[ETH_ALEN] = {0xf1, 0xff, 0xff, 0xff, 0xff, 0xff};
struct stmmac_packet_attrs attr = { };
int ret, tries = 256;
if (stmmac_filter_check(priv))
return -EOPNOTSUPP;
if (stmmac_uc_filter_check(priv))
return -EOPNOTSUPP;
if (netdev_mc_count(priv->dev) >= priv->hw->multicast_filter_bins)
return -EOPNOTSUPP;
while (--tries) {
mc_addr[ETH_ALEN - 1] = tries;
if (stmmac_hash_check(priv, mc_addr))
break;
}
if (!tries)
return -EOPNOTSUPP;
ret = dev_uc_add(priv->dev, uc_addr);
if (ret)
return ret;
attr.dst = uc_addr;
ret = __stmmac_test_loopback(priv, &attr);
if (ret)
goto cleanup;
attr.dst = mc_addr;
ret = __stmmac_test_loopback(priv, &attr);
ret = ret ? 0 : -EINVAL;
cleanup:
dev_uc_del(priv->dev, uc_addr);
return ret;
}
static int stmmac_test_ucfilt(struct stmmac_priv *priv)
{
unsigned char uc_addr[ETH_ALEN] = {0xf0, 0xff, 0xff, 0xff, 0xff, 0xff};
unsigned char mc_addr[ETH_ALEN] = {0xf1, 0xff, 0xff, 0xff, 0xff, 0xff};
struct stmmac_packet_attrs attr = { };
int ret, tries = 256;
if (stmmac_filter_check(priv))
return -EOPNOTSUPP;
if (stmmac_uc_filter_check(priv))
return -EOPNOTSUPP;
if (netdev_mc_count(priv->dev) >= priv->hw->multicast_filter_bins)
return -EOPNOTSUPP;
while (--tries) {
uc_addr[ETH_ALEN - 1] = tries;
if (stmmac_perfect_check(priv, uc_addr))
break;
}
if (!tries)
return -EOPNOTSUPP;
ret = dev_mc_add(priv->dev, mc_addr);
if (ret)
return ret;
attr.dst = mc_addr;
ret = __stmmac_test_loopback(priv, &attr);
if (ret)
goto cleanup;
attr.dst = uc_addr;
ret = __stmmac_test_loopback(priv, &attr);
ret = ret ? 0 : -EINVAL;
cleanup:
dev_mc_del(priv->dev, mc_addr);
return ret;
}
static int stmmac_test_flowctrl_validate(struct sk_buff *skb,
struct net_device *ndev,
struct packet_type *pt,
struct net_device *orig_ndev)
{
struct stmmac_test_priv *tpriv = pt->af_packet_priv;
struct ethhdr *ehdr;
ehdr = (struct ethhdr *)skb_mac_header(skb);
if (!ether_addr_equal_unaligned(ehdr->h_source, orig_ndev->dev_addr))
goto out;
if (ehdr->h_proto != htons(ETH_P_PAUSE))
goto out;
tpriv->ok = true;
complete(&tpriv->comp);
out:
kfree_skb(skb);
return 0;
}
static int stmmac_test_flowctrl(struct stmmac_priv *priv)
{
unsigned char paddr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x01};
u32 rx_cnt = priv->plat->rx_queues_to_use;
struct mac_device_info *mac = priv->hw;
struct stmmac_test_priv *tpriv;
unsigned int rx_fifo_size;
unsigned int pkt_count;
int i, ret = 0;
if (!(mac->link.caps & MAC_SYM_PAUSE))
return -EOPNOTSUPP;
rx_fifo_size = priv->plat->rx_fifo_size;
if (!rx_fifo_size)
rx_fifo_size = priv->dma_cap.rx_fifo_size;
if (priv->plat->core_type != DWMAC_CORE_MAC100 &&
rx_fifo_size / priv->plat->rx_queues_to_use < 4096)
return -EOPNOTSUPP;
tpriv = kzalloc_obj(*tpriv);
if (!tpriv)
return -ENOMEM;
tpriv->ok = false;
init_completion(&tpriv->comp);
tpriv->pt.type = htons(ETH_P_PAUSE);
tpriv->pt.func = stmmac_test_flowctrl_validate;
tpriv->pt.dev = priv->dev;
tpriv->pt.af_packet_priv = tpriv;
stmmac_sft_add_pack(&tpriv->pt);
pkt_count = rx_fifo_size;
pkt_count /= 1400;
pkt_count *= 2;
for (i = 0; i < rx_cnt; i++)
stmmac_stop_rx(priv, priv->ioaddr, i);
ret = dev_set_promiscuity(priv->dev, 1);
if (ret)
goto cleanup;
ret = dev_mc_add(priv->dev, paddr);
if (ret)
goto cleanup;
for (i = 0; i < pkt_count; i++) {
struct stmmac_packet_attrs attr = { };
attr.dst = priv->dev->dev_addr;
attr.dont_wait = true;
attr.size = 1400;
ret = __stmmac_test_loopback(priv, &attr);
if (ret)
goto cleanup;
if (tpriv->ok)
break;
}
msleep(200);
for (i = 0; i < rx_cnt; i++) {
struct stmmac_channel *ch = &priv->channel[i];
u32 tail;
tail = priv->dma_conf.rx_queue[i].dma_rx_phy +
(priv->dma_conf.dma_rx_size * sizeof(struct dma_desc));
stmmac_set_rx_tail_ptr(priv, priv->ioaddr, tail, i);
stmmac_start_rx(priv, priv->ioaddr, i);
local_bh_disable();
napi_schedule(&ch->rx_napi);
local_bh_enable();
}
wait_for_completion_timeout(&tpriv->comp, STMMAC_LB_TIMEOUT);
ret = tpriv->ok ? 0 : -ETIMEDOUT;
cleanup:
dev_mc_del(priv->dev, paddr);
dev_set_promiscuity(priv->dev, -1);
stmmac_sft_remove_pack(&tpriv->pt);
kfree(tpriv);
return ret;
}
static int stmmac_test_rss(struct stmmac_priv *priv)
{
struct stmmac_packet_attrs attr = { };
if (!priv->dma_cap.rssen || !priv->rss.enable)
return -EOPNOTSUPP;
attr.dst = priv->dev->dev_addr;
attr.exp_hash = true;
attr.sport = 0x321;
attr.dport = 0x123;
return __stmmac_test_loopback(priv, &attr);
}
static int stmmac_test_vlan_validate(struct sk_buff *skb,
struct net_device *ndev,
struct packet_type *pt,
struct net_device *orig_ndev)
{
struct stmmac_test_priv *tpriv = pt->af_packet_priv;
struct stmmachdr *shdr;
struct ethhdr *ehdr;
struct udphdr *uhdr;
struct iphdr *ihdr;
u16 proto;
proto = tpriv->svlan ? ETH_P_8021AD : ETH_P_8021Q;
skb = skb_unclone_full(skb, GFP_ATOMIC);
if (!skb)
goto out;
if (skb_linearize(skb))
goto out;
if (skb_headlen(skb) < (STMMAC_TEST_PKT_SIZE - ETH_HLEN))
goto out;
ehdr = (struct ethhdr *)skb_mac_header(skb);
if (!ether_addr_equal_unaligned(ehdr->h_dest, tpriv->packet->dst))
goto out;
if (tpriv->vlan_id) {
if (skb->vlan_proto != htons(proto))
goto out;
if (skb->vlan_tci != tpriv->vlan_id) {
tpriv->ok = false;
complete(&tpriv->comp);
goto out;
}
}
ihdr = ip_hdr(skb);
if (tpriv->svlan)
ihdr = (struct iphdr *)(skb_network_header(skb) + 4);
if (ihdr->protocol != IPPROTO_UDP)
goto out;
uhdr = (struct udphdr *)((u8 *)ihdr + 4 * ihdr->ihl);
if (uhdr->dest != htons(tpriv->packet->dport))
goto out;
shdr = (struct stmmachdr *)((u8 *)uhdr + sizeof(*uhdr));
if (shdr->magic != cpu_to_be64(STMMAC_TEST_PKT_MAGIC))
goto out;
tpriv->ok = true;
complete(&tpriv->comp);
out:
kfree_skb(skb);
return 0;
}
static int __stmmac_test_vlanfilt(struct stmmac_priv *priv)
{
struct stmmac_packet_attrs attr = { };
struct stmmac_test_priv *tpriv;
struct sk_buff *skb = NULL;
int ret = 0, i;
tpriv = kzalloc_obj(*tpriv);
if (!tpriv)
return -ENOMEM;
tpriv->ok = false;
init_completion(&tpriv->comp);
tpriv->pt.type = htons(ETH_P_IP);
tpriv->pt.func = stmmac_test_vlan_validate;
tpriv->pt.dev = priv->dev;
tpriv->pt.af_packet_priv = tpriv;
tpriv->packet = &attr;
tpriv->capture_all = true;
tpriv->vlan_id = 0x123;
ret = vlan_vid_add(priv->dev, htons(ETH_P_8021Q), tpriv->vlan_id);
if (ret)
goto cleanup;
attr.vlan = 1;
attr.dst = priv->dev->dev_addr;
attr.sport = 9;
attr.dport = 9;
stmmac_sft_add_pack(&tpriv->pt);
for (i = 0; i < 4; i++) {
attr.vlan_id_out = tpriv->vlan_id + i;
skb = stmmac_test_get_udp_skb(priv, &attr);
if (!skb) {
ret = -ENOMEM;
goto vlan_del;
}
ret = dev_direct_xmit(skb, 0);
if (ret)
goto vlan_del;
wait_for_completion_timeout(&tpriv->comp, STMMAC_LB_TIMEOUT);
ret = tpriv->ok ? 0 : -ETIMEDOUT;
if (ret && !i) {
goto vlan_del;
} else if (!ret && i) {
ret = -EINVAL;
goto vlan_del;
} else {
ret = 0;
}
tpriv->ok = false;
}
vlan_del:
stmmac_sft_remove_pack(&tpriv->pt);
vlan_vid_del(priv->dev, htons(ETH_P_8021Q), tpriv->vlan_id);
cleanup:
kfree(tpriv);
return ret;
}
static int stmmac_test_vlanfilt(struct stmmac_priv *priv)
{
if (!priv->dma_cap.vlhash)
return -EOPNOTSUPP;
return __stmmac_test_vlanfilt(priv);
}
static int stmmac_test_vlanfilt_perfect(struct stmmac_priv *priv)
{
int ret, prev_cap = priv->dma_cap.vlhash;
if (!(priv->dev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
return -EOPNOTSUPP;
priv->dma_cap.vlhash = 0;
ret = __stmmac_test_vlanfilt(priv);
priv->dma_cap.vlhash = prev_cap;
return ret;
}
static int __stmmac_test_svlanfilt(struct stmmac_priv *priv)
{
struct stmmac_packet_attrs attr = { };
struct stmmac_test_priv *tpriv;
struct sk_buff *skb = NULL;
int ret = 0, i;
tpriv = kzalloc_obj(*tpriv);
if (!tpriv)
return -ENOMEM;
tpriv->ok = false;
tpriv->svlan = true;
init_completion(&tpriv->comp);
tpriv->pt.type = htons(ETH_P_8021Q);
tpriv->pt.func = stmmac_test_vlan_validate;
tpriv->pt.dev = priv->dev;
tpriv->pt.af_packet_priv = tpriv;
tpriv->packet = &attr;
tpriv->capture_all = true;
tpriv->vlan_id = 0x123;
ret = vlan_vid_add(priv->dev, htons(ETH_P_8021AD), tpriv->vlan_id);
if (ret)
goto cleanup;
attr.vlan = 2;
attr.dst = priv->dev->dev_addr;
attr.sport = 9;
attr.dport = 9;
stmmac_sft_add_pack(&tpriv->pt);
for (i = 0; i < 4; i++) {
attr.vlan_id_out = tpriv->vlan_id + i;
skb = stmmac_test_get_udp_skb(priv, &attr);
if (!skb) {
ret = -ENOMEM;
goto vlan_del;
}
ret = dev_direct_xmit(skb, 0);
if (ret)
goto vlan_del;
wait_for_completion_timeout(&tpriv->comp, STMMAC_LB_TIMEOUT);
ret = tpriv->ok ? 0 : -ETIMEDOUT;
if (ret && !i) {
goto vlan_del;
} else if (!ret && i) {
ret = -EINVAL;
goto vlan_del;
} else {
ret = 0;
}
tpriv->ok = false;
}
vlan_del:
stmmac_sft_remove_pack(&tpriv->pt);
vlan_vid_del(priv->dev, htons(ETH_P_8021AD), tpriv->vlan_id);
cleanup:
kfree(tpriv);
return ret;
}
static int stmmac_test_svlanfilt(struct stmmac_priv *priv)
{
if (!priv->dma_cap.vlhash)
return -EOPNOTSUPP;
return __stmmac_test_svlanfilt(priv);
}
static int stmmac_test_svlanfilt_perfect(struct stmmac_priv *priv)
{
int ret, prev_cap = priv->dma_cap.vlhash;
if (!(priv->dev->features & NETIF_F_HW_VLAN_STAG_FILTER))
return -EOPNOTSUPP;
priv->dma_cap.vlhash = 0;
ret = __stmmac_test_svlanfilt(priv);
priv->dma_cap.vlhash = prev_cap;
return ret;
}
#ifdef CONFIG_NET_CLS_ACT
static int stmmac_test_rxp(struct stmmac_priv *priv)
{
unsigned char addr[ETH_ALEN] = {0xde, 0xad, 0xbe, 0xef, 0x00, 0x00};
struct tc_cls_u32_offload cls_u32 = { };
struct stmmac_packet_attrs attr = { };
struct tc_action **actions;
struct tc_u32_sel *sel;
struct tcf_gact *gact;
struct tcf_exts *exts;
int ret, i, nk = 1;
if (!tc_can_offload(priv->dev))
return -EOPNOTSUPP;
if (!priv->dma_cap.frpsel)
return -EOPNOTSUPP;
sel = kzalloc_flex(*sel, keys, nk);
if (!sel)
return -ENOMEM;
exts = kzalloc_obj(*exts);
if (!exts) {
ret = -ENOMEM;
goto cleanup_sel;
}
actions = kzalloc_objs(*actions, nk);
if (!actions) {
ret = -ENOMEM;
goto cleanup_exts;
}
gact = kzalloc_objs(*gact, nk);
if (!gact) {
ret = -ENOMEM;
goto cleanup_actions;
}
cls_u32.command = TC_CLSU32_NEW_KNODE;
cls_u32.common.chain_index = 0;
cls_u32.common.protocol = htons(ETH_P_ALL);
cls_u32.knode.exts = exts;
cls_u32.knode.sel = sel;
cls_u32.knode.handle = 0x123;
exts->nr_actions = nk;
exts->actions = actions;
for (i = 0; i < nk; i++) {
actions[i] = (struct tc_action *)&gact[i];
gact->tcf_action = TC_ACT_SHOT;
}
sel->nkeys = nk;
sel->offshift = 0;
sel->keys[0].off = 6;
sel->keys[0].val = htonl(0xdeadbeef);
sel->keys[0].mask = ~0x0;
ret = stmmac_tc_setup_cls_u32(priv, priv, &cls_u32);
if (ret)
goto cleanup_act;
attr.dst = priv->dev->dev_addr;
attr.src = addr;
ret = __stmmac_test_loopback(priv, &attr);
ret = ret ? 0 : -EINVAL;
cls_u32.command = TC_CLSU32_DELETE_KNODE;
stmmac_tc_setup_cls_u32(priv, priv, &cls_u32);
cleanup_act:
kfree(gact);
cleanup_actions:
kfree(actions);
cleanup_exts:
kfree(exts);
cleanup_sel:
kfree(sel);
return ret;
}
#else
static int stmmac_test_rxp(struct stmmac_priv *priv)
{
return -EOPNOTSUPP;
}
#endif
static int stmmac_test_desc_sai(struct stmmac_priv *priv)
{
unsigned char src[ETH_ALEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
struct stmmac_packet_attrs attr = { };
int ret;
if (!priv->dma_cap.vlins)
return -EOPNOTSUPP;
attr.remove_sa = true;
attr.sarc = true;
attr.src = src;
attr.dst = priv->dev->dev_addr;
priv->sarc_type = 0x1;
ret = __stmmac_test_loopback(priv, &attr);
priv->sarc_type = 0x0;
return ret;
}
static int stmmac_test_desc_sar(struct stmmac_priv *priv)
{
unsigned char src[ETH_ALEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
struct stmmac_packet_attrs attr = { };
int ret;
if (!priv->dma_cap.vlins)
return -EOPNOTSUPP;
attr.sarc = true;
attr.src = src;
attr.dst = priv->dev->dev_addr;
priv->sarc_type = 0x2;
ret = __stmmac_test_loopback(priv, &attr);
priv->sarc_type = 0x0;
return ret;
}
static int stmmac_test_reg_sai(struct stmmac_priv *priv)
{
unsigned char src[ETH_ALEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
struct stmmac_packet_attrs attr = { };
int ret;
if (!priv->dma_cap.vlins)
return -EOPNOTSUPP;
attr.remove_sa = true;
attr.sarc = true;
attr.src = src;
attr.dst = priv->dev->dev_addr;
if (stmmac_sarc_configure(priv, priv->ioaddr, 0x2))
return -EOPNOTSUPP;
ret = __stmmac_test_loopback(priv, &attr);
stmmac_sarc_configure(priv, priv->ioaddr, 0x0);
return ret;
}
static int stmmac_test_reg_sar(struct stmmac_priv *priv)
{
unsigned char src[ETH_ALEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
struct stmmac_packet_attrs attr = { };
int ret;
if (!priv->dma_cap.vlins)
return -EOPNOTSUPP;
attr.sarc = true;
attr.src = src;
attr.dst = priv->dev->dev_addr;
if (stmmac_sarc_configure(priv, priv->ioaddr, 0x3))
return -EOPNOTSUPP;
ret = __stmmac_test_loopback(priv, &attr);
stmmac_sarc_configure(priv, priv->ioaddr, 0x0);
return ret;
}
static int stmmac_test_vlanoff_common(struct stmmac_priv *priv, bool svlan)
{
struct stmmac_packet_attrs attr = { };
struct stmmac_test_priv *tpriv;
struct sk_buff *skb = NULL;
int ret = 0;
u16 proto;
if (!priv->dma_cap.vlins)
return -EOPNOTSUPP;
tpriv = kzalloc_obj(*tpriv);
if (!tpriv)
return -ENOMEM;
proto = svlan ? ETH_P_8021AD : ETH_P_8021Q;
tpriv->ok = false;
tpriv->svlan = svlan;
init_completion(&tpriv->comp);
tpriv->pt.type = svlan ? htons(ETH_P_8021Q) : htons(ETH_P_IP);
tpriv->pt.func = stmmac_test_vlan_validate;
tpriv->pt.dev = priv->dev;
tpriv->pt.af_packet_priv = tpriv;
tpriv->packet = &attr;
tpriv->vlan_id = 0x123;
tpriv->capture_all = true;
ret = vlan_vid_add(priv->dev, htons(proto), tpriv->vlan_id);
if (ret)
goto cleanup;
attr.dst = priv->dev->dev_addr;
stmmac_sft_add_pack(&tpriv->pt);
skb = stmmac_test_get_udp_skb(priv, &attr);
if (!skb) {
ret = -ENOMEM;
goto vlan_del;
}
__vlan_hwaccel_put_tag(skb, htons(proto), tpriv->vlan_id);
skb->protocol = htons(proto);
ret = dev_direct_xmit(skb, 0);
if (ret)
goto vlan_del;
wait_for_completion_timeout(&tpriv->comp, STMMAC_LB_TIMEOUT);
ret = tpriv->ok ? 0 : -ETIMEDOUT;
vlan_del:
stmmac_sft_remove_pack(&tpriv->pt);
vlan_vid_del(priv->dev, htons(proto), tpriv->vlan_id);
cleanup:
kfree(tpriv);
return ret;
}
static int stmmac_test_vlanoff(struct stmmac_priv *priv)
{
return stmmac_test_vlanoff_common(priv, false);
}
static int stmmac_test_svlanoff(struct stmmac_priv *priv)
{
if (!(priv->dev->features & NETIF_F_HW_VLAN_STAG_TX))
return -EOPNOTSUPP;
return stmmac_test_vlanoff_common(priv, true);
}
#ifdef CONFIG_NET_CLS_ACT
static int __stmmac_test_l3filt(struct stmmac_priv *priv, u32 dst, u32 src,
u32 dst_mask, u32 src_mask)
{
struct flow_dissector_key_ipv4_addrs key, mask;
unsigned long dummy_cookie = 0xdeadbeef;
struct stmmac_packet_attrs attr = { };
struct flow_dissector *dissector;
struct flow_cls_offload *cls;
int ret, old_enable = 0;
struct flow_rule *rule;
if (!tc_can_offload(priv->dev))
return -EOPNOTSUPP;
if (!priv->dma_cap.l3l4fnum)
return -EOPNOTSUPP;
if (priv->rss.enable) {
old_enable = priv->rss.enable;
priv->rss.enable = false;
stmmac_rss_configure(priv, priv->hw, NULL,
priv->plat->rx_queues_to_use);
}
dissector = kzalloc_obj(*dissector);
if (!dissector) {
ret = -ENOMEM;
goto cleanup_rss;
}
dissector->used_keys |= (1ULL << FLOW_DISSECTOR_KEY_IPV4_ADDRS);
dissector->offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] = 0;
cls = kzalloc_obj(*cls);
if (!cls) {
ret = -ENOMEM;
goto cleanup_dissector;
}
cls->common.chain_index = 0;
cls->command = FLOW_CLS_REPLACE;
cls->cookie = dummy_cookie;
rule = kzalloc_flex(*rule, action.entries, 1);
if (!rule) {
ret = -ENOMEM;
goto cleanup_cls;
}
rule->match.dissector = dissector;
rule->match.key = (void *)&key;
rule->match.mask = (void *)&mask;
key.src = htonl(src);
key.dst = htonl(dst);
mask.src = src_mask;
mask.dst = dst_mask;
cls->rule = rule;
rule->action.entries[0].id = FLOW_ACTION_DROP;
rule->action.entries[0].hw_stats = FLOW_ACTION_HW_STATS_ANY;
rule->action.num_entries = 1;
attr.dst = priv->dev->dev_addr;
attr.ip_dst = dst;
attr.ip_src = src;
ret = __stmmac_test_loopback(priv, &attr);
if (ret)
goto cleanup_rule;
ret = stmmac_tc_setup_cls(priv, priv, cls);
if (ret)
goto cleanup_rule;
ret = __stmmac_test_loopback(priv, &attr);
ret = ret ? 0 : -EINVAL;
cls->command = FLOW_CLS_DESTROY;
stmmac_tc_setup_cls(priv, priv, cls);
cleanup_rule:
kfree(rule);
cleanup_cls:
kfree(cls);
cleanup_dissector:
kfree(dissector);
cleanup_rss:
if (old_enable) {
priv->rss.enable = old_enable;
stmmac_rss_configure(priv, priv->hw, &priv->rss,
priv->plat->rx_queues_to_use);
}
return ret;
}
#else
static int __stmmac_test_l3filt(struct stmmac_priv *priv, u32 dst, u32 src,
u32 dst_mask, u32 src_mask)
{
return -EOPNOTSUPP;
}
#endif
static int stmmac_test_l3filt_da(struct stmmac_priv *priv)
{
u32 addr = 0x10203040;
return __stmmac_test_l3filt(priv, addr, 0, ~0, 0);
}
static int stmmac_test_l3filt_sa(struct stmmac_priv *priv)
{
u32 addr = 0x10203040;
return __stmmac_test_l3filt(priv, 0, addr, 0, ~0);
}
#ifdef CONFIG_NET_CLS_ACT
static int __stmmac_test_l4filt(struct stmmac_priv *priv, u32 dst, u32 src,
u32 dst_mask, u32 src_mask, bool udp)
{
struct {
struct flow_dissector_key_basic bkey;
struct flow_dissector_key_ports key;
} __aligned(BITS_PER_LONG / 8) keys = { };
struct {
struct flow_dissector_key_basic bmask;
struct flow_dissector_key_ports mask;
} __aligned(BITS_PER_LONG / 8) masks = { };
unsigned long dummy_cookie = 0xdeadbeef;
struct stmmac_packet_attrs attr = { };
struct flow_dissector *dissector;
struct flow_cls_offload *cls;
int ret, old_enable = 0;
struct flow_rule *rule;
if (!tc_can_offload(priv->dev))
return -EOPNOTSUPP;
if (!priv->dma_cap.l3l4fnum)
return -EOPNOTSUPP;
if (priv->rss.enable) {
old_enable = priv->rss.enable;
priv->rss.enable = false;
stmmac_rss_configure(priv, priv->hw, NULL,
priv->plat->rx_queues_to_use);
}
dissector = kzalloc_obj(*dissector);
if (!dissector) {
ret = -ENOMEM;
goto cleanup_rss;
}
dissector->used_keys |= (1ULL << FLOW_DISSECTOR_KEY_BASIC);
dissector->used_keys |= (1ULL << FLOW_DISSECTOR_KEY_PORTS);
dissector->offset[FLOW_DISSECTOR_KEY_BASIC] = 0;
dissector->offset[FLOW_DISSECTOR_KEY_PORTS] = offsetof(typeof(keys), key);
cls = kzalloc_obj(*cls);
if (!cls) {
ret = -ENOMEM;
goto cleanup_dissector;
}
cls->common.chain_index = 0;
cls->command = FLOW_CLS_REPLACE;
cls->cookie = dummy_cookie;
rule = kzalloc_flex(*rule, action.entries, 1);
if (!rule) {
ret = -ENOMEM;
goto cleanup_cls;
}
rule->match.dissector = dissector;
rule->match.key = (void *)&keys;
rule->match.mask = (void *)&masks;
keys.bkey.ip_proto = udp ? IPPROTO_UDP : IPPROTO_TCP;
keys.key.src = htons(src);
keys.key.dst = htons(dst);
masks.bmask.ip_proto = 0xff;
masks.mask.src = src_mask;
masks.mask.dst = dst_mask;
cls->rule = rule;
rule->action.entries[0].id = FLOW_ACTION_DROP;
rule->action.entries[0].hw_stats = FLOW_ACTION_HW_STATS_ANY;
rule->action.num_entries = 1;
attr.dst = priv->dev->dev_addr;
attr.tcp = !udp;
attr.sport = src;
attr.dport = dst;
attr.ip_dst = 0;
ret = __stmmac_test_loopback(priv, &attr);
if (ret)
goto cleanup_rule;
ret = stmmac_tc_setup_cls(priv, priv, cls);
if (ret)
goto cleanup_rule;
ret = __stmmac_test_loopback(priv, &attr);
ret = ret ? 0 : -EINVAL;
cls->command = FLOW_CLS_DESTROY;
stmmac_tc_setup_cls(priv, priv, cls);
cleanup_rule:
kfree(rule);
cleanup_cls:
kfree(cls);
cleanup_dissector:
kfree(dissector);
cleanup_rss:
if (old_enable) {
priv->rss.enable = old_enable;
stmmac_rss_configure(priv, priv->hw, &priv->rss,
priv->plat->rx_queues_to_use);
}
return ret;
}
#else
static int __stmmac_test_l4filt(struct stmmac_priv *priv, u32 dst, u32 src,
u32 dst_mask, u32 src_mask, bool udp)
{
return -EOPNOTSUPP;
}
#endif
static int stmmac_test_l4filt_da_tcp(struct stmmac_priv *priv)
{
u16 dummy_port = 0x123;
return __stmmac_test_l4filt(priv, dummy_port, 0, ~0, 0, false);
}
static int stmmac_test_l4filt_sa_tcp(struct stmmac_priv *priv)
{
u16 dummy_port = 0x123;
return __stmmac_test_l4filt(priv, 0, dummy_port, 0, ~0, false);
}
static int stmmac_test_l4filt_da_udp(struct stmmac_priv *priv)
{
u16 dummy_port = 0x123;
return __stmmac_test_l4filt(priv, dummy_port, 0, ~0, 0, true);
}
static int stmmac_test_l4filt_sa_udp(struct stmmac_priv *priv)
{
u16 dummy_port = 0x123;
return __stmmac_test_l4filt(priv, 0, dummy_port, 0, ~0, true);
}
static int __stmmac_test_jumbo(struct stmmac_priv *priv, u16 queue)
{
struct stmmac_packet_attrs attr = { };
int size = priv->dma_conf.dma_buf_sz;
if (!dwmac_is_xmac(priv->plat->core_type))
size -= NET_IP_ALIGN;
attr.dst = priv->dev->dev_addr;
attr.max_size = size - ETH_FCS_LEN;
attr.queue_mapping = queue;
return __stmmac_test_loopback(priv, &attr);
}
static int stmmac_test_jumbo(struct stmmac_priv *priv)
{
return __stmmac_test_jumbo(priv, 0);
}
static int stmmac_test_mjumbo(struct stmmac_priv *priv)
{
u32 chan, tx_cnt = priv->plat->tx_queues_to_use;
int ret;
if (tx_cnt <= 1)
return -EOPNOTSUPP;
for (chan = 0; chan < tx_cnt; chan++) {
ret = __stmmac_test_jumbo(priv, chan);
if (ret)
return ret;
}
return 0;
}
static int stmmac_test_sph(struct stmmac_priv *priv)
{
unsigned long cnt_end, cnt_start = priv->xstats.rx_split_hdr_pkt_n;
struct stmmac_packet_attrs attr = { };
int ret;
if (!priv->sph_active)
return -EOPNOTSUPP;
attr.dst = priv->dev->dev_addr;
attr.tcp = false;
ret = __stmmac_test_loopback(priv, &attr);
if (ret)
return ret;
cnt_end = priv->xstats.rx_split_hdr_pkt_n;
if (cnt_end <= cnt_start)
return -EINVAL;
cnt_start = cnt_end;
attr.dst = priv->dev->dev_addr;
attr.tcp = true;
ret = __stmmac_test_loopback(priv, &attr);
if (ret)
return ret;
cnt_end = priv->xstats.rx_split_hdr_pkt_n;
if (cnt_end <= cnt_start)
return -EINVAL;
return 0;
}
static int stmmac_test_tbs(struct stmmac_priv *priv)
{
#define STMMAC_TBS_LT_OFFSET (500 * 1000 * 1000) /* 500 ms*/
struct stmmac_packet_attrs attr = { };
struct tc_etf_qopt_offload qopt;
u64 start_time, curr_time = 0;
unsigned long flags;
int ret, i;
if (!priv->hwts_tx_en)
return -EOPNOTSUPP;
for (i = 0; i < priv->plat->tx_queues_to_use; i++)
if (priv->dma_conf.tx_queue[i].tbs & STMMAC_TBS_AVAIL)
break;
if (i >= priv->plat->tx_queues_to_use)
return -EOPNOTSUPP;
qopt.enable = true;
qopt.queue = i;
ret = stmmac_tc_setup_etf(priv, priv, &qopt);
if (ret)
return ret;
read_lock_irqsave(&priv->ptp_lock, flags);
stmmac_get_systime(priv, priv->ptpaddr, &curr_time, NULL);
read_unlock_irqrestore(&priv->ptp_lock, flags);
if (!curr_time) {
ret = -EOPNOTSUPP;
goto fail_disable;
}
start_time = curr_time;
curr_time += STMMAC_TBS_LT_OFFSET;
attr.dst = priv->dev->dev_addr;
attr.timestamp = curr_time;
attr.timeout = nsecs_to_jiffies(2 * STMMAC_TBS_LT_OFFSET);
attr.queue_mapping = i;
ret = __stmmac_test_loopback(priv, &attr);
if (ret)
goto fail_disable;
read_lock_irqsave(&priv->ptp_lock, flags);
stmmac_get_systime(priv, priv->ptpaddr, &curr_time, NULL);
read_unlock_irqrestore(&priv->ptp_lock, flags);
if ((curr_time - start_time) < STMMAC_TBS_LT_OFFSET)
ret = -EINVAL;
fail_disable:
qopt.enable = false;
stmmac_tc_setup_etf(priv, priv, &qopt);
return ret;
}
static const struct stmmac_test {
char name[ETH_GSTRING_LEN];
int lb;
int (*fn)(struct stmmac_priv *priv);
} stmmac_selftests[] = {
{
.name = "MAC Loopback ",
.fn = stmmac_test_mac_loopback,
}, {
.name = "MMC Counters ",
.fn = stmmac_test_mmc,
}, {
.name = "EEE ",
.fn = stmmac_test_eee,
}, {
.name = "Hash Filter MC ",
.fn = stmmac_test_hfilt,
}, {
.name = "Perfect Filter UC ",
.fn = stmmac_test_pfilt,
}, {
.name = "MC Filter ",
.fn = stmmac_test_mcfilt,
}, {
.name = "UC Filter ",
.fn = stmmac_test_ucfilt,
}, {
.name = "Flow Control ",
.fn = stmmac_test_flowctrl,
}, {
.name = "RSS ",
.fn = stmmac_test_rss,
}, {
.name = "VLAN Filtering ",
.fn = stmmac_test_vlanfilt,
}, {
.name = "VLAN Filtering (perf) ",
.fn = stmmac_test_vlanfilt_perfect,
}, {
.name = "SVLAN Filtering ",
.fn = stmmac_test_svlanfilt,
}, {
.name = "SVLAN Filtering (perf) ",
.fn = stmmac_test_svlanfilt_perfect,
}, {
.name = "Flexible RX Parser ",
.fn = stmmac_test_rxp,
}, {
.name = "SA Insertion (desc) ",
.fn = stmmac_test_desc_sai,
}, {
.name = "SA Replacement (desc) ",
.fn = stmmac_test_desc_sar,
}, {
.name = "SA Insertion (reg) ",
.fn = stmmac_test_reg_sai,
}, {
.name = "SA Replacement (reg) ",
.fn = stmmac_test_reg_sar,
}, {
.name = "VLAN TX Insertion ",
.fn = stmmac_test_vlanoff,
}, {
.name = "SVLAN TX Insertion ",
.fn = stmmac_test_svlanoff,
}, {
.name = "L3 DA Filtering ",
.fn = stmmac_test_l3filt_da,
}, {
.name = "L3 SA Filtering ",
.fn = stmmac_test_l3filt_sa,
}, {
.name = "L4 DA TCP Filtering ",
.fn = stmmac_test_l4filt_da_tcp,
}, {
.name = "L4 SA TCP Filtering ",
.fn = stmmac_test_l4filt_sa_tcp,
}, {
.name = "L4 DA UDP Filtering ",
.fn = stmmac_test_l4filt_da_udp,
}, {
.name = "L4 SA UDP Filtering ",
.fn = stmmac_test_l4filt_sa_udp,
}, {
.name = "Jumbo Frame ",
.fn = stmmac_test_jumbo,
}, {
.name = "Multichannel Jumbo ",
.fn = stmmac_test_mjumbo,
}, {
.name = "Split Header ",
.fn = stmmac_test_sph,
}, {
.name = "TBS (ETF Scheduler) ",
.fn = stmmac_test_tbs,
},
};
void stmmac_selftest_run(struct net_device *dev,
struct ethtool_test *etest, u64 *buf)
{
struct stmmac_priv *priv = netdev_priv(dev);
int count = stmmac_selftest_get_count(priv);
int i, ret;
memset(buf, 0, sizeof(*buf) * count);
stmmac_test_next_id = 0;
if (etest->flags != ETH_TEST_FL_OFFLINE) {
netdev_err(priv->dev, "Only offline tests are supported\n");
etest->flags |= ETH_TEST_FL_FAILED;
return;
} else if (!netif_carrier_ok(dev)) {
netdev_err(priv->dev, "You need valid Link to execute tests\n");
etest->flags |= ETH_TEST_FL_FAILED;
return;
}
msleep(200);
ret = stmmac_set_mac_loopback(priv, priv->ioaddr, true);
if (ret) {
netdev_err(priv->dev, "Loopback is not supported\n");
etest->flags |= ETH_TEST_FL_FAILED;
return;
}
for (i = 0; i < count; i++) {
ret = stmmac_selftests[i].fn(priv);
if (ret && (ret != -EOPNOTSUPP))
etest->flags |= ETH_TEST_FL_FAILED;
buf[i] = ret;
}
stmmac_set_mac_loopback(priv, priv->ioaddr, false);
}
void stmmac_selftest_get_strings(struct stmmac_priv *priv, u8 *data)
{
u8 *p = data;
int i;
for (i = 0; i < stmmac_selftest_get_count(priv); i++) {
snprintf(p, ETH_GSTRING_LEN, "%2d. %s", i + 1,
stmmac_selftests[i].name);
p += ETH_GSTRING_LEN;
}
}
int stmmac_selftest_get_count(struct stmmac_priv *priv)
{
return ARRAY_SIZE(stmmac_selftests);
}