stmmac_selftests.c 45 KB

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