dpaa_eth.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766
  1. /* Copyright 2008 - 2016 Freescale Semiconductor Inc.
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions are met:
  5. * * Redistributions of source code must retain the above copyright
  6. * notice, this list of conditions and the following disclaimer.
  7. * * Redistributions in binary form must reproduce the above copyright
  8. * notice, this list of conditions and the following disclaimer in the
  9. * documentation and/or other materials provided with the distribution.
  10. * * Neither the name of Freescale Semiconductor nor the
  11. * names of its contributors may be used to endorse or promote products
  12. * derived from this software without specific prior written permission.
  13. *
  14. * ALTERNATIVELY, this software may be distributed under the terms of the
  15. * GNU General Public License ("GPL") as published by the Free Software
  16. * Foundation, either version 2 of that License or (at your option) any
  17. * later version.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
  20. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  31. #include <linux/init.h>
  32. #include <linux/module.h>
  33. #include <linux/of_platform.h>
  34. #include <linux/of_mdio.h>
  35. #include <linux/of_net.h>
  36. #include <linux/io.h>
  37. #include <linux/if_arp.h>
  38. #include <linux/if_vlan.h>
  39. #include <linux/icmp.h>
  40. #include <linux/ip.h>
  41. #include <linux/ipv6.h>
  42. #include <linux/udp.h>
  43. #include <linux/tcp.h>
  44. #include <linux/net.h>
  45. #include <linux/skbuff.h>
  46. #include <linux/etherdevice.h>
  47. #include <linux/if_ether.h>
  48. #include <linux/highmem.h>
  49. #include <linux/percpu.h>
  50. #include <linux/dma-mapping.h>
  51. #include <linux/sort.h>
  52. #include <soc/fsl/bman.h>
  53. #include <soc/fsl/qman.h>
  54. #include "fman.h"
  55. #include "fman_port.h"
  56. #include "mac.h"
  57. #include "dpaa_eth.h"
  58. /* CREATE_TRACE_POINTS only needs to be defined once. Other dpaa files
  59. * using trace events only need to #include <trace/events/sched.h>
  60. */
  61. #define CREATE_TRACE_POINTS
  62. #include "dpaa_eth_trace.h"
  63. static int debug = -1;
  64. module_param(debug, int, 0444);
  65. MODULE_PARM_DESC(debug, "Module/Driver verbosity level (0=none,...,16=all)");
  66. static u16 tx_timeout = 1000;
  67. module_param(tx_timeout, ushort, 0444);
  68. MODULE_PARM_DESC(tx_timeout, "The Tx timeout in ms");
  69. #define FM_FD_STAT_RX_ERRORS \
  70. (FM_FD_ERR_DMA | FM_FD_ERR_PHYSICAL | \
  71. FM_FD_ERR_SIZE | FM_FD_ERR_CLS_DISCARD | \
  72. FM_FD_ERR_EXTRACTION | FM_FD_ERR_NO_SCHEME | \
  73. FM_FD_ERR_PRS_TIMEOUT | FM_FD_ERR_PRS_ILL_INSTRUCT | \
  74. FM_FD_ERR_PRS_HDR_ERR)
  75. #define FM_FD_STAT_TX_ERRORS \
  76. (FM_FD_ERR_UNSUPPORTED_FORMAT | \
  77. FM_FD_ERR_LENGTH | FM_FD_ERR_DMA)
  78. #define DPAA_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
  79. NETIF_MSG_LINK | NETIF_MSG_IFUP | \
  80. NETIF_MSG_IFDOWN)
  81. #define DPAA_INGRESS_CS_THRESHOLD 0x10000000
  82. /* Ingress congestion threshold on FMan ports
  83. * The size in bytes of the ingress tail-drop threshold on FMan ports.
  84. * Traffic piling up above this value will be rejected by QMan and discarded
  85. * by FMan.
  86. */
  87. /* Size in bytes of the FQ taildrop threshold */
  88. #define DPAA_FQ_TD 0x200000
  89. #define DPAA_CS_THRESHOLD_1G 0x06000000
  90. /* Egress congestion threshold on 1G ports, range 0x1000 .. 0x10000000
  91. * The size in bytes of the egress Congestion State notification threshold on
  92. * 1G ports. The 1G dTSECs can quite easily be flooded by cores doing Tx in a
  93. * tight loop (e.g. by sending UDP datagrams at "while(1) speed"),
  94. * and the larger the frame size, the more acute the problem.
  95. * So we have to find a balance between these factors:
  96. * - avoiding the device staying congested for a prolonged time (risking
  97. * the netdev watchdog to fire - see also the tx_timeout module param);
  98. * - affecting performance of protocols such as TCP, which otherwise
  99. * behave well under the congestion notification mechanism;
  100. * - preventing the Tx cores from tightly-looping (as if the congestion
  101. * threshold was too low to be effective);
  102. * - running out of memory if the CS threshold is set too high.
  103. */
  104. #define DPAA_CS_THRESHOLD_10G 0x10000000
  105. /* The size in bytes of the egress Congestion State notification threshold on
  106. * 10G ports, range 0x1000 .. 0x10000000
  107. */
  108. /* Largest value that the FQD's OAL field can hold */
  109. #define FSL_QMAN_MAX_OAL 127
  110. /* Default alignment for start of data in an Rx FD */
  111. #define DPAA_FD_DATA_ALIGNMENT 16
  112. /* Values for the L3R field of the FM Parse Results
  113. */
  114. /* L3 Type field: First IP Present IPv4 */
  115. #define FM_L3_PARSE_RESULT_IPV4 0x8000
  116. /* L3 Type field: First IP Present IPv6 */
  117. #define FM_L3_PARSE_RESULT_IPV6 0x4000
  118. /* Values for the L4R field of the FM Parse Results */
  119. /* L4 Type field: UDP */
  120. #define FM_L4_PARSE_RESULT_UDP 0x40
  121. /* L4 Type field: TCP */
  122. #define FM_L4_PARSE_RESULT_TCP 0x20
  123. #define DPAA_SGT_MAX_ENTRIES 16 /* maximum number of entries in SG Table */
  124. #define DPAA_BUFF_RELEASE_MAX 8 /* maximum number of buffers released at once */
  125. #define FSL_DPAA_BPID_INV 0xff
  126. #define FSL_DPAA_ETH_MAX_BUF_COUNT 128
  127. #define FSL_DPAA_ETH_REFILL_THRESHOLD 80
  128. #define DPAA_TX_PRIV_DATA_SIZE 16
  129. #define DPAA_PARSE_RESULTS_SIZE sizeof(struct fman_prs_result)
  130. #define DPAA_TIME_STAMP_SIZE 8
  131. #define DPAA_HASH_RESULTS_SIZE 8
  132. #define DPAA_RX_PRIV_DATA_SIZE (u16)(DPAA_TX_PRIV_DATA_SIZE + \
  133. dpaa_rx_extra_headroom)
  134. #define DPAA_ETH_RX_QUEUES 128
  135. #define DPAA_ENQUEUE_RETRIES 100000
  136. enum port_type {RX, TX};
  137. struct fm_port_fqs {
  138. struct dpaa_fq *tx_defq;
  139. struct dpaa_fq *tx_errq;
  140. struct dpaa_fq *rx_defq;
  141. struct dpaa_fq *rx_errq;
  142. };
  143. /* All the dpa bps in use at any moment */
  144. static struct dpaa_bp *dpaa_bp_array[BM_MAX_NUM_OF_POOLS];
  145. /* The raw buffer size must be cacheline aligned */
  146. #define DPAA_BP_RAW_SIZE 4096
  147. /* When using more than one buffer pool, the raw sizes are as follows:
  148. * 1 bp: 4KB
  149. * 2 bp: 2KB, 4KB
  150. * 3 bp: 1KB, 2KB, 4KB
  151. * 4 bp: 1KB, 2KB, 4KB, 8KB
  152. */
  153. static inline size_t bpool_buffer_raw_size(u8 index, u8 cnt)
  154. {
  155. size_t res = DPAA_BP_RAW_SIZE / 4;
  156. u8 i;
  157. for (i = (cnt < 3) ? cnt : 3; i < 3 + index; i++)
  158. res *= 2;
  159. return res;
  160. }
  161. /* FMan-DMA requires 16-byte alignment for Rx buffers, but SKB_DATA_ALIGN is
  162. * even stronger (SMP_CACHE_BYTES-aligned), so we just get away with that,
  163. * via SKB_WITH_OVERHEAD(). We can't rely on netdev_alloc_frag() giving us
  164. * half-page-aligned buffers, so we reserve some more space for start-of-buffer
  165. * alignment.
  166. */
  167. #define dpaa_bp_size(raw_size) SKB_WITH_OVERHEAD((raw_size) - SMP_CACHE_BYTES)
  168. static int dpaa_max_frm;
  169. static int dpaa_rx_extra_headroom;
  170. #define dpaa_get_max_mtu() \
  171. (dpaa_max_frm - (VLAN_ETH_HLEN + ETH_FCS_LEN))
  172. static int dpaa_netdev_init(struct net_device *net_dev,
  173. const struct net_device_ops *dpaa_ops,
  174. u16 tx_timeout)
  175. {
  176. struct dpaa_priv *priv = netdev_priv(net_dev);
  177. struct device *dev = net_dev->dev.parent;
  178. struct dpaa_percpu_priv *percpu_priv;
  179. const u8 *mac_addr;
  180. int i, err;
  181. /* Although we access another CPU's private data here
  182. * we do it at initialization so it is safe
  183. */
  184. for_each_possible_cpu(i) {
  185. percpu_priv = per_cpu_ptr(priv->percpu_priv, i);
  186. percpu_priv->net_dev = net_dev;
  187. }
  188. net_dev->netdev_ops = dpaa_ops;
  189. mac_addr = priv->mac_dev->addr;
  190. net_dev->mem_start = priv->mac_dev->res->start;
  191. net_dev->mem_end = priv->mac_dev->res->end;
  192. net_dev->min_mtu = ETH_MIN_MTU;
  193. net_dev->max_mtu = dpaa_get_max_mtu();
  194. net_dev->hw_features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  195. NETIF_F_LLTX);
  196. net_dev->hw_features |= NETIF_F_SG | NETIF_F_HIGHDMA;
  197. /* The kernels enables GSO automatically, if we declare NETIF_F_SG.
  198. * For conformity, we'll still declare GSO explicitly.
  199. */
  200. net_dev->features |= NETIF_F_GSO;
  201. net_dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  202. /* we do not want shared skbs on TX */
  203. net_dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  204. net_dev->features |= net_dev->hw_features;
  205. net_dev->vlan_features = net_dev->features;
  206. memcpy(net_dev->perm_addr, mac_addr, net_dev->addr_len);
  207. memcpy(net_dev->dev_addr, mac_addr, net_dev->addr_len);
  208. net_dev->ethtool_ops = &dpaa_ethtool_ops;
  209. net_dev->needed_headroom = priv->tx_headroom;
  210. net_dev->watchdog_timeo = msecs_to_jiffies(tx_timeout);
  211. /* start without the RUNNING flag, phylib controls it later */
  212. netif_carrier_off(net_dev);
  213. err = register_netdev(net_dev);
  214. if (err < 0) {
  215. dev_err(dev, "register_netdev() = %d\n", err);
  216. return err;
  217. }
  218. return 0;
  219. }
  220. static int dpaa_stop(struct net_device *net_dev)
  221. {
  222. struct mac_device *mac_dev;
  223. struct dpaa_priv *priv;
  224. int i, err, error;
  225. priv = netdev_priv(net_dev);
  226. mac_dev = priv->mac_dev;
  227. netif_tx_stop_all_queues(net_dev);
  228. /* Allow the Fman (Tx) port to process in-flight frames before we
  229. * try switching it off.
  230. */
  231. usleep_range(5000, 10000);
  232. err = mac_dev->stop(mac_dev);
  233. if (err < 0)
  234. netif_err(priv, ifdown, net_dev, "mac_dev->stop() = %d\n",
  235. err);
  236. for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++) {
  237. error = fman_port_disable(mac_dev->port[i]);
  238. if (error)
  239. err = error;
  240. }
  241. if (net_dev->phydev)
  242. phy_disconnect(net_dev->phydev);
  243. net_dev->phydev = NULL;
  244. return err;
  245. }
  246. static void dpaa_tx_timeout(struct net_device *net_dev)
  247. {
  248. struct dpaa_percpu_priv *percpu_priv;
  249. const struct dpaa_priv *priv;
  250. priv = netdev_priv(net_dev);
  251. percpu_priv = this_cpu_ptr(priv->percpu_priv);
  252. netif_crit(priv, timer, net_dev, "Transmit timeout latency: %u ms\n",
  253. jiffies_to_msecs(jiffies - dev_trans_start(net_dev)));
  254. percpu_priv->stats.tx_errors++;
  255. }
  256. /* Calculates the statistics for the given device by adding the statistics
  257. * collected by each CPU.
  258. */
  259. static void dpaa_get_stats64(struct net_device *net_dev,
  260. struct rtnl_link_stats64 *s)
  261. {
  262. int numstats = sizeof(struct rtnl_link_stats64) / sizeof(u64);
  263. struct dpaa_priv *priv = netdev_priv(net_dev);
  264. struct dpaa_percpu_priv *percpu_priv;
  265. u64 *netstats = (u64 *)s;
  266. u64 *cpustats;
  267. int i, j;
  268. for_each_possible_cpu(i) {
  269. percpu_priv = per_cpu_ptr(priv->percpu_priv, i);
  270. cpustats = (u64 *)&percpu_priv->stats;
  271. /* add stats from all CPUs */
  272. for (j = 0; j < numstats; j++)
  273. netstats[j] += cpustats[j];
  274. }
  275. }
  276. static struct mac_device *dpaa_mac_dev_get(struct platform_device *pdev)
  277. {
  278. struct platform_device *of_dev;
  279. struct dpaa_eth_data *eth_data;
  280. struct device *dpaa_dev, *dev;
  281. struct device_node *mac_node;
  282. struct mac_device *mac_dev;
  283. dpaa_dev = &pdev->dev;
  284. eth_data = dpaa_dev->platform_data;
  285. if (!eth_data)
  286. return ERR_PTR(-ENODEV);
  287. mac_node = eth_data->mac_node;
  288. of_dev = of_find_device_by_node(mac_node);
  289. if (!of_dev) {
  290. dev_err(dpaa_dev, "of_find_device_by_node(%s) failed\n",
  291. mac_node->full_name);
  292. of_node_put(mac_node);
  293. return ERR_PTR(-EINVAL);
  294. }
  295. of_node_put(mac_node);
  296. dev = &of_dev->dev;
  297. mac_dev = dev_get_drvdata(dev);
  298. if (!mac_dev) {
  299. dev_err(dpaa_dev, "dev_get_drvdata(%s) failed\n",
  300. dev_name(dev));
  301. return ERR_PTR(-EINVAL);
  302. }
  303. return mac_dev;
  304. }
  305. static int dpaa_set_mac_address(struct net_device *net_dev, void *addr)
  306. {
  307. const struct dpaa_priv *priv;
  308. struct mac_device *mac_dev;
  309. struct sockaddr old_addr;
  310. int err;
  311. priv = netdev_priv(net_dev);
  312. memcpy(old_addr.sa_data, net_dev->dev_addr, ETH_ALEN);
  313. err = eth_mac_addr(net_dev, addr);
  314. if (err < 0) {
  315. netif_err(priv, drv, net_dev, "eth_mac_addr() = %d\n", err);
  316. return err;
  317. }
  318. mac_dev = priv->mac_dev;
  319. err = mac_dev->change_addr(mac_dev->fman_mac,
  320. (enet_addr_t *)net_dev->dev_addr);
  321. if (err < 0) {
  322. netif_err(priv, drv, net_dev, "mac_dev->change_addr() = %d\n",
  323. err);
  324. /* reverting to previous address */
  325. eth_mac_addr(net_dev, &old_addr);
  326. return err;
  327. }
  328. return 0;
  329. }
  330. static void dpaa_set_rx_mode(struct net_device *net_dev)
  331. {
  332. const struct dpaa_priv *priv;
  333. int err;
  334. priv = netdev_priv(net_dev);
  335. if (!!(net_dev->flags & IFF_PROMISC) != priv->mac_dev->promisc) {
  336. priv->mac_dev->promisc = !priv->mac_dev->promisc;
  337. err = priv->mac_dev->set_promisc(priv->mac_dev->fman_mac,
  338. priv->mac_dev->promisc);
  339. if (err < 0)
  340. netif_err(priv, drv, net_dev,
  341. "mac_dev->set_promisc() = %d\n",
  342. err);
  343. }
  344. err = priv->mac_dev->set_multi(net_dev, priv->mac_dev);
  345. if (err < 0)
  346. netif_err(priv, drv, net_dev, "mac_dev->set_multi() = %d\n",
  347. err);
  348. }
  349. static struct dpaa_bp *dpaa_bpid2pool(int bpid)
  350. {
  351. if (WARN_ON(bpid < 0 || bpid >= BM_MAX_NUM_OF_POOLS))
  352. return NULL;
  353. return dpaa_bp_array[bpid];
  354. }
  355. /* checks if this bpool is already allocated */
  356. static bool dpaa_bpid2pool_use(int bpid)
  357. {
  358. if (dpaa_bpid2pool(bpid)) {
  359. atomic_inc(&dpaa_bp_array[bpid]->refs);
  360. return true;
  361. }
  362. return false;
  363. }
  364. /* called only once per bpid by dpaa_bp_alloc_pool() */
  365. static void dpaa_bpid2pool_map(int bpid, struct dpaa_bp *dpaa_bp)
  366. {
  367. dpaa_bp_array[bpid] = dpaa_bp;
  368. atomic_set(&dpaa_bp->refs, 1);
  369. }
  370. static int dpaa_bp_alloc_pool(struct dpaa_bp *dpaa_bp)
  371. {
  372. int err;
  373. if (dpaa_bp->size == 0 || dpaa_bp->config_count == 0) {
  374. pr_err("%s: Buffer pool is not properly initialized! Missing size or initial number of buffers\n",
  375. __func__);
  376. return -EINVAL;
  377. }
  378. /* If the pool is already specified, we only create one per bpid */
  379. if (dpaa_bp->bpid != FSL_DPAA_BPID_INV &&
  380. dpaa_bpid2pool_use(dpaa_bp->bpid))
  381. return 0;
  382. if (dpaa_bp->bpid == FSL_DPAA_BPID_INV) {
  383. dpaa_bp->pool = bman_new_pool();
  384. if (!dpaa_bp->pool) {
  385. pr_err("%s: bman_new_pool() failed\n",
  386. __func__);
  387. return -ENODEV;
  388. }
  389. dpaa_bp->bpid = (u8)bman_get_bpid(dpaa_bp->pool);
  390. }
  391. if (dpaa_bp->seed_cb) {
  392. err = dpaa_bp->seed_cb(dpaa_bp);
  393. if (err)
  394. goto pool_seed_failed;
  395. }
  396. dpaa_bpid2pool_map(dpaa_bp->bpid, dpaa_bp);
  397. return 0;
  398. pool_seed_failed:
  399. pr_err("%s: pool seeding failed\n", __func__);
  400. bman_free_pool(dpaa_bp->pool);
  401. return err;
  402. }
  403. /* remove and free all the buffers from the given buffer pool */
  404. static void dpaa_bp_drain(struct dpaa_bp *bp)
  405. {
  406. u8 num = 8;
  407. int ret;
  408. do {
  409. struct bm_buffer bmb[8];
  410. int i;
  411. ret = bman_acquire(bp->pool, bmb, num);
  412. if (ret < 0) {
  413. if (num == 8) {
  414. /* we have less than 8 buffers left;
  415. * drain them one by one
  416. */
  417. num = 1;
  418. ret = 1;
  419. continue;
  420. } else {
  421. /* Pool is fully drained */
  422. break;
  423. }
  424. }
  425. if (bp->free_buf_cb)
  426. for (i = 0; i < num; i++)
  427. bp->free_buf_cb(bp, &bmb[i]);
  428. } while (ret > 0);
  429. }
  430. static void dpaa_bp_free(struct dpaa_bp *dpaa_bp)
  431. {
  432. struct dpaa_bp *bp = dpaa_bpid2pool(dpaa_bp->bpid);
  433. /* the mapping between bpid and dpaa_bp is done very late in the
  434. * allocation procedure; if something failed before the mapping, the bp
  435. * was not configured, therefore we don't need the below instructions
  436. */
  437. if (!bp)
  438. return;
  439. if (!atomic_dec_and_test(&bp->refs))
  440. return;
  441. if (bp->free_buf_cb)
  442. dpaa_bp_drain(bp);
  443. dpaa_bp_array[bp->bpid] = NULL;
  444. bman_free_pool(bp->pool);
  445. }
  446. static void dpaa_bps_free(struct dpaa_priv *priv)
  447. {
  448. int i;
  449. for (i = 0; i < DPAA_BPS_NUM; i++)
  450. dpaa_bp_free(priv->dpaa_bps[i]);
  451. }
  452. /* Use multiple WQs for FQ assignment:
  453. * - Tx Confirmation queues go to WQ1.
  454. * - Rx Error and Tx Error queues go to WQ2 (giving them a better chance
  455. * to be scheduled, in case there are many more FQs in WQ3).
  456. * - Rx Default and Tx queues go to WQ3 (no differentiation between
  457. * Rx and Tx traffic).
  458. * This ensures that Tx-confirmed buffers are timely released. In particular,
  459. * it avoids congestion on the Tx Confirm FQs, which can pile up PFDRs if they
  460. * are greatly outnumbered by other FQs in the system, while
  461. * dequeue scheduling is round-robin.
  462. */
  463. static inline void dpaa_assign_wq(struct dpaa_fq *fq)
  464. {
  465. switch (fq->fq_type) {
  466. case FQ_TYPE_TX_CONFIRM:
  467. case FQ_TYPE_TX_CONF_MQ:
  468. fq->wq = 1;
  469. break;
  470. case FQ_TYPE_RX_ERROR:
  471. case FQ_TYPE_TX_ERROR:
  472. fq->wq = 2;
  473. break;
  474. case FQ_TYPE_RX_DEFAULT:
  475. case FQ_TYPE_TX:
  476. fq->wq = 3;
  477. break;
  478. default:
  479. WARN(1, "Invalid FQ type %d for FQID %d!\n",
  480. fq->fq_type, fq->fqid);
  481. }
  482. }
  483. static struct dpaa_fq *dpaa_fq_alloc(struct device *dev,
  484. u32 start, u32 count,
  485. struct list_head *list,
  486. enum dpaa_fq_type fq_type)
  487. {
  488. struct dpaa_fq *dpaa_fq;
  489. int i;
  490. dpaa_fq = devm_kzalloc(dev, sizeof(*dpaa_fq) * count,
  491. GFP_KERNEL);
  492. if (!dpaa_fq)
  493. return NULL;
  494. for (i = 0; i < count; i++) {
  495. dpaa_fq[i].fq_type = fq_type;
  496. dpaa_fq[i].fqid = start ? start + i : 0;
  497. list_add_tail(&dpaa_fq[i].list, list);
  498. }
  499. for (i = 0; i < count; i++)
  500. dpaa_assign_wq(dpaa_fq + i);
  501. return dpaa_fq;
  502. }
  503. static int dpaa_alloc_all_fqs(struct device *dev, struct list_head *list,
  504. struct fm_port_fqs *port_fqs)
  505. {
  506. struct dpaa_fq *dpaa_fq;
  507. dpaa_fq = dpaa_fq_alloc(dev, 0, 1, list, FQ_TYPE_RX_ERROR);
  508. if (!dpaa_fq)
  509. goto fq_alloc_failed;
  510. port_fqs->rx_errq = &dpaa_fq[0];
  511. dpaa_fq = dpaa_fq_alloc(dev, 0, 1, list, FQ_TYPE_RX_DEFAULT);
  512. if (!dpaa_fq)
  513. goto fq_alloc_failed;
  514. port_fqs->rx_defq = &dpaa_fq[0];
  515. if (!dpaa_fq_alloc(dev, 0, DPAA_ETH_TXQ_NUM, list, FQ_TYPE_TX_CONF_MQ))
  516. goto fq_alloc_failed;
  517. dpaa_fq = dpaa_fq_alloc(dev, 0, 1, list, FQ_TYPE_TX_ERROR);
  518. if (!dpaa_fq)
  519. goto fq_alloc_failed;
  520. port_fqs->tx_errq = &dpaa_fq[0];
  521. dpaa_fq = dpaa_fq_alloc(dev, 0, 1, list, FQ_TYPE_TX_CONFIRM);
  522. if (!dpaa_fq)
  523. goto fq_alloc_failed;
  524. port_fqs->tx_defq = &dpaa_fq[0];
  525. if (!dpaa_fq_alloc(dev, 0, DPAA_ETH_TXQ_NUM, list, FQ_TYPE_TX))
  526. goto fq_alloc_failed;
  527. return 0;
  528. fq_alloc_failed:
  529. dev_err(dev, "dpaa_fq_alloc() failed\n");
  530. return -ENOMEM;
  531. }
  532. static u32 rx_pool_channel;
  533. static DEFINE_SPINLOCK(rx_pool_channel_init);
  534. static int dpaa_get_channel(void)
  535. {
  536. spin_lock(&rx_pool_channel_init);
  537. if (!rx_pool_channel) {
  538. u32 pool;
  539. int ret;
  540. ret = qman_alloc_pool(&pool);
  541. if (!ret)
  542. rx_pool_channel = pool;
  543. }
  544. spin_unlock(&rx_pool_channel_init);
  545. if (!rx_pool_channel)
  546. return -ENOMEM;
  547. return rx_pool_channel;
  548. }
  549. static void dpaa_release_channel(void)
  550. {
  551. qman_release_pool(rx_pool_channel);
  552. }
  553. static void dpaa_eth_add_channel(u16 channel)
  554. {
  555. u32 pool = QM_SDQCR_CHANNELS_POOL_CONV(channel);
  556. const cpumask_t *cpus = qman_affine_cpus();
  557. struct qman_portal *portal;
  558. int cpu;
  559. for_each_cpu(cpu, cpus) {
  560. portal = qman_get_affine_portal(cpu);
  561. qman_p_static_dequeue_add(portal, pool);
  562. }
  563. }
  564. /* Congestion group state change notification callback.
  565. * Stops the device's egress queues while they are congested and
  566. * wakes them upon exiting congested state.
  567. * Also updates some CGR-related stats.
  568. */
  569. static void dpaa_eth_cgscn(struct qman_portal *qm, struct qman_cgr *cgr,
  570. int congested)
  571. {
  572. struct dpaa_priv *priv = (struct dpaa_priv *)container_of(cgr,
  573. struct dpaa_priv, cgr_data.cgr);
  574. if (congested) {
  575. priv->cgr_data.congestion_start_jiffies = jiffies;
  576. netif_tx_stop_all_queues(priv->net_dev);
  577. priv->cgr_data.cgr_congested_count++;
  578. } else {
  579. priv->cgr_data.congested_jiffies +=
  580. (jiffies - priv->cgr_data.congestion_start_jiffies);
  581. netif_tx_wake_all_queues(priv->net_dev);
  582. }
  583. }
  584. static int dpaa_eth_cgr_init(struct dpaa_priv *priv)
  585. {
  586. struct qm_mcc_initcgr initcgr;
  587. u32 cs_th;
  588. int err;
  589. err = qman_alloc_cgrid(&priv->cgr_data.cgr.cgrid);
  590. if (err < 0) {
  591. if (netif_msg_drv(priv))
  592. pr_err("%s: Error %d allocating CGR ID\n",
  593. __func__, err);
  594. goto out_error;
  595. }
  596. priv->cgr_data.cgr.cb = dpaa_eth_cgscn;
  597. /* Enable Congestion State Change Notifications and CS taildrop */
  598. memset(&initcgr, 0, sizeof(initcgr));
  599. initcgr.we_mask = cpu_to_be16(QM_CGR_WE_CSCN_EN | QM_CGR_WE_CS_THRES);
  600. initcgr.cgr.cscn_en = QM_CGR_EN;
  601. /* Set different thresholds based on the MAC speed.
  602. * This may turn suboptimal if the MAC is reconfigured at a speed
  603. * lower than its max, e.g. if a dTSEC later negotiates a 100Mbps link.
  604. * In such cases, we ought to reconfigure the threshold, too.
  605. */
  606. if (priv->mac_dev->if_support & SUPPORTED_10000baseT_Full)
  607. cs_th = DPAA_CS_THRESHOLD_10G;
  608. else
  609. cs_th = DPAA_CS_THRESHOLD_1G;
  610. qm_cgr_cs_thres_set64(&initcgr.cgr.cs_thres, cs_th, 1);
  611. initcgr.we_mask |= cpu_to_be16(QM_CGR_WE_CSTD_EN);
  612. initcgr.cgr.cstd_en = QM_CGR_EN;
  613. err = qman_create_cgr(&priv->cgr_data.cgr, QMAN_CGR_FLAG_USE_INIT,
  614. &initcgr);
  615. if (err < 0) {
  616. if (netif_msg_drv(priv))
  617. pr_err("%s: Error %d creating CGR with ID %d\n",
  618. __func__, err, priv->cgr_data.cgr.cgrid);
  619. qman_release_cgrid(priv->cgr_data.cgr.cgrid);
  620. goto out_error;
  621. }
  622. if (netif_msg_drv(priv))
  623. pr_debug("Created CGR %d for netdev with hwaddr %pM on QMan channel %d\n",
  624. priv->cgr_data.cgr.cgrid, priv->mac_dev->addr,
  625. priv->cgr_data.cgr.chan);
  626. out_error:
  627. return err;
  628. }
  629. static inline void dpaa_setup_ingress(const struct dpaa_priv *priv,
  630. struct dpaa_fq *fq,
  631. const struct qman_fq *template)
  632. {
  633. fq->fq_base = *template;
  634. fq->net_dev = priv->net_dev;
  635. fq->flags = QMAN_FQ_FLAG_NO_ENQUEUE;
  636. fq->channel = priv->channel;
  637. }
  638. static inline void dpaa_setup_egress(const struct dpaa_priv *priv,
  639. struct dpaa_fq *fq,
  640. struct fman_port *port,
  641. const struct qman_fq *template)
  642. {
  643. fq->fq_base = *template;
  644. fq->net_dev = priv->net_dev;
  645. if (port) {
  646. fq->flags = QMAN_FQ_FLAG_TO_DCPORTAL;
  647. fq->channel = (u16)fman_port_get_qman_channel_id(port);
  648. } else {
  649. fq->flags = QMAN_FQ_FLAG_NO_MODIFY;
  650. }
  651. }
  652. static void dpaa_fq_setup(struct dpaa_priv *priv,
  653. const struct dpaa_fq_cbs *fq_cbs,
  654. struct fman_port *tx_port)
  655. {
  656. int egress_cnt = 0, conf_cnt = 0, num_portals = 0, cpu;
  657. const cpumask_t *affine_cpus = qman_affine_cpus();
  658. u16 portals[NR_CPUS];
  659. struct dpaa_fq *fq;
  660. for_each_cpu(cpu, affine_cpus)
  661. portals[num_portals++] = qman_affine_channel(cpu);
  662. if (num_portals == 0)
  663. dev_err(priv->net_dev->dev.parent,
  664. "No Qman software (affine) channels found");
  665. /* Initialize each FQ in the list */
  666. list_for_each_entry(fq, &priv->dpaa_fq_list, list) {
  667. switch (fq->fq_type) {
  668. case FQ_TYPE_RX_DEFAULT:
  669. dpaa_setup_ingress(priv, fq, &fq_cbs->rx_defq);
  670. break;
  671. case FQ_TYPE_RX_ERROR:
  672. dpaa_setup_ingress(priv, fq, &fq_cbs->rx_errq);
  673. break;
  674. case FQ_TYPE_TX:
  675. dpaa_setup_egress(priv, fq, tx_port,
  676. &fq_cbs->egress_ern);
  677. /* If we have more Tx queues than the number of cores,
  678. * just ignore the extra ones.
  679. */
  680. if (egress_cnt < DPAA_ETH_TXQ_NUM)
  681. priv->egress_fqs[egress_cnt++] = &fq->fq_base;
  682. break;
  683. case FQ_TYPE_TX_CONF_MQ:
  684. priv->conf_fqs[conf_cnt++] = &fq->fq_base;
  685. /* fall through */
  686. case FQ_TYPE_TX_CONFIRM:
  687. dpaa_setup_ingress(priv, fq, &fq_cbs->tx_defq);
  688. break;
  689. case FQ_TYPE_TX_ERROR:
  690. dpaa_setup_ingress(priv, fq, &fq_cbs->tx_errq);
  691. break;
  692. default:
  693. dev_warn(priv->net_dev->dev.parent,
  694. "Unknown FQ type detected!\n");
  695. break;
  696. }
  697. }
  698. /* Make sure all CPUs receive a corresponding Tx queue. */
  699. while (egress_cnt < DPAA_ETH_TXQ_NUM) {
  700. list_for_each_entry(fq, &priv->dpaa_fq_list, list) {
  701. if (fq->fq_type != FQ_TYPE_TX)
  702. continue;
  703. priv->egress_fqs[egress_cnt++] = &fq->fq_base;
  704. if (egress_cnt == DPAA_ETH_TXQ_NUM)
  705. break;
  706. }
  707. }
  708. }
  709. static inline int dpaa_tx_fq_to_id(const struct dpaa_priv *priv,
  710. struct qman_fq *tx_fq)
  711. {
  712. int i;
  713. for (i = 0; i < DPAA_ETH_TXQ_NUM; i++)
  714. if (priv->egress_fqs[i] == tx_fq)
  715. return i;
  716. return -EINVAL;
  717. }
  718. static int dpaa_fq_init(struct dpaa_fq *dpaa_fq, bool td_enable)
  719. {
  720. const struct dpaa_priv *priv;
  721. struct qman_fq *confq = NULL;
  722. struct qm_mcc_initfq initfq;
  723. struct device *dev;
  724. struct qman_fq *fq;
  725. int queue_id;
  726. int err;
  727. priv = netdev_priv(dpaa_fq->net_dev);
  728. dev = dpaa_fq->net_dev->dev.parent;
  729. if (dpaa_fq->fqid == 0)
  730. dpaa_fq->flags |= QMAN_FQ_FLAG_DYNAMIC_FQID;
  731. dpaa_fq->init = !(dpaa_fq->flags & QMAN_FQ_FLAG_NO_MODIFY);
  732. err = qman_create_fq(dpaa_fq->fqid, dpaa_fq->flags, &dpaa_fq->fq_base);
  733. if (err) {
  734. dev_err(dev, "qman_create_fq() failed\n");
  735. return err;
  736. }
  737. fq = &dpaa_fq->fq_base;
  738. if (dpaa_fq->init) {
  739. memset(&initfq, 0, sizeof(initfq));
  740. initfq.we_mask = cpu_to_be16(QM_INITFQ_WE_FQCTRL);
  741. /* Note: we may get to keep an empty FQ in cache */
  742. initfq.fqd.fq_ctrl = cpu_to_be16(QM_FQCTRL_PREFERINCACHE);
  743. /* Try to reduce the number of portal interrupts for
  744. * Tx Confirmation FQs.
  745. */
  746. if (dpaa_fq->fq_type == FQ_TYPE_TX_CONFIRM)
  747. initfq.fqd.fq_ctrl |= cpu_to_be16(QM_FQCTRL_HOLDACTIVE);
  748. /* FQ placement */
  749. initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_DESTWQ);
  750. qm_fqd_set_destwq(&initfq.fqd, dpaa_fq->channel, dpaa_fq->wq);
  751. /* Put all egress queues in a congestion group of their own.
  752. * Sensu stricto, the Tx confirmation queues are Rx FQs,
  753. * rather than Tx - but they nonetheless account for the
  754. * memory footprint on behalf of egress traffic. We therefore
  755. * place them in the netdev's CGR, along with the Tx FQs.
  756. */
  757. if (dpaa_fq->fq_type == FQ_TYPE_TX ||
  758. dpaa_fq->fq_type == FQ_TYPE_TX_CONFIRM ||
  759. dpaa_fq->fq_type == FQ_TYPE_TX_CONF_MQ) {
  760. initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_CGID);
  761. initfq.fqd.fq_ctrl |= cpu_to_be16(QM_FQCTRL_CGE);
  762. initfq.fqd.cgid = (u8)priv->cgr_data.cgr.cgrid;
  763. /* Set a fixed overhead accounting, in an attempt to
  764. * reduce the impact of fixed-size skb shells and the
  765. * driver's needed headroom on system memory. This is
  766. * especially the case when the egress traffic is
  767. * composed of small datagrams.
  768. * Unfortunately, QMan's OAL value is capped to an
  769. * insufficient value, but even that is better than
  770. * no overhead accounting at all.
  771. */
  772. initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_OAC);
  773. qm_fqd_set_oac(&initfq.fqd, QM_OAC_CG);
  774. qm_fqd_set_oal(&initfq.fqd,
  775. min(sizeof(struct sk_buff) +
  776. priv->tx_headroom,
  777. (size_t)FSL_QMAN_MAX_OAL));
  778. }
  779. if (td_enable) {
  780. initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_TDTHRESH);
  781. qm_fqd_set_taildrop(&initfq.fqd, DPAA_FQ_TD, 1);
  782. initfq.fqd.fq_ctrl = cpu_to_be16(QM_FQCTRL_TDE);
  783. }
  784. if (dpaa_fq->fq_type == FQ_TYPE_TX) {
  785. queue_id = dpaa_tx_fq_to_id(priv, &dpaa_fq->fq_base);
  786. if (queue_id >= 0)
  787. confq = priv->conf_fqs[queue_id];
  788. if (confq) {
  789. initfq.we_mask |=
  790. cpu_to_be16(QM_INITFQ_WE_CONTEXTA);
  791. /* ContextA: OVOM=1(use contextA2 bits instead of ICAD)
  792. * A2V=1 (contextA A2 field is valid)
  793. * A0V=1 (contextA A0 field is valid)
  794. * B0V=1 (contextB field is valid)
  795. * ContextA A2: EBD=1 (deallocate buffers inside FMan)
  796. * ContextB B0(ASPID): 0 (absolute Virtual Storage ID)
  797. */
  798. qm_fqd_context_a_set64(&initfq.fqd,
  799. 0x1e00000080000000ULL);
  800. }
  801. }
  802. /* Put all the ingress queues in our "ingress CGR". */
  803. if (priv->use_ingress_cgr &&
  804. (dpaa_fq->fq_type == FQ_TYPE_RX_DEFAULT ||
  805. dpaa_fq->fq_type == FQ_TYPE_RX_ERROR)) {
  806. initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_CGID);
  807. initfq.fqd.fq_ctrl |= cpu_to_be16(QM_FQCTRL_CGE);
  808. initfq.fqd.cgid = (u8)priv->ingress_cgr.cgrid;
  809. /* Set a fixed overhead accounting, just like for the
  810. * egress CGR.
  811. */
  812. initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_OAC);
  813. qm_fqd_set_oac(&initfq.fqd, QM_OAC_CG);
  814. qm_fqd_set_oal(&initfq.fqd,
  815. min(sizeof(struct sk_buff) +
  816. priv->tx_headroom,
  817. (size_t)FSL_QMAN_MAX_OAL));
  818. }
  819. /* Initialization common to all ingress queues */
  820. if (dpaa_fq->flags & QMAN_FQ_FLAG_NO_ENQUEUE) {
  821. initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_CONTEXTA);
  822. initfq.fqd.fq_ctrl |= cpu_to_be16(QM_FQCTRL_HOLDACTIVE);
  823. initfq.fqd.context_a.stashing.exclusive =
  824. QM_STASHING_EXCL_DATA | QM_STASHING_EXCL_CTX |
  825. QM_STASHING_EXCL_ANNOTATION;
  826. qm_fqd_set_stashing(&initfq.fqd, 1, 2,
  827. DIV_ROUND_UP(sizeof(struct qman_fq),
  828. 64));
  829. }
  830. err = qman_init_fq(fq, QMAN_INITFQ_FLAG_SCHED, &initfq);
  831. if (err < 0) {
  832. dev_err(dev, "qman_init_fq(%u) = %d\n",
  833. qman_fq_fqid(fq), err);
  834. qman_destroy_fq(fq);
  835. return err;
  836. }
  837. }
  838. dpaa_fq->fqid = qman_fq_fqid(fq);
  839. return 0;
  840. }
  841. static int dpaa_fq_free_entry(struct device *dev, struct qman_fq *fq)
  842. {
  843. const struct dpaa_priv *priv;
  844. struct dpaa_fq *dpaa_fq;
  845. int err, error;
  846. err = 0;
  847. dpaa_fq = container_of(fq, struct dpaa_fq, fq_base);
  848. priv = netdev_priv(dpaa_fq->net_dev);
  849. if (dpaa_fq->init) {
  850. err = qman_retire_fq(fq, NULL);
  851. if (err < 0 && netif_msg_drv(priv))
  852. dev_err(dev, "qman_retire_fq(%u) = %d\n",
  853. qman_fq_fqid(fq), err);
  854. error = qman_oos_fq(fq);
  855. if (error < 0 && netif_msg_drv(priv)) {
  856. dev_err(dev, "qman_oos_fq(%u) = %d\n",
  857. qman_fq_fqid(fq), error);
  858. if (err >= 0)
  859. err = error;
  860. }
  861. }
  862. qman_destroy_fq(fq);
  863. list_del(&dpaa_fq->list);
  864. return err;
  865. }
  866. static int dpaa_fq_free(struct device *dev, struct list_head *list)
  867. {
  868. struct dpaa_fq *dpaa_fq, *tmp;
  869. int err, error;
  870. err = 0;
  871. list_for_each_entry_safe(dpaa_fq, tmp, list, list) {
  872. error = dpaa_fq_free_entry(dev, (struct qman_fq *)dpaa_fq);
  873. if (error < 0 && err >= 0)
  874. err = error;
  875. }
  876. return err;
  877. }
  878. static void dpaa_eth_init_tx_port(struct fman_port *port, struct dpaa_fq *errq,
  879. struct dpaa_fq *defq,
  880. struct dpaa_buffer_layout *buf_layout)
  881. {
  882. struct fman_buffer_prefix_content buf_prefix_content;
  883. struct fman_port_params params;
  884. int err;
  885. memset(&params, 0, sizeof(params));
  886. memset(&buf_prefix_content, 0, sizeof(buf_prefix_content));
  887. buf_prefix_content.priv_data_size = buf_layout->priv_data_size;
  888. buf_prefix_content.pass_prs_result = true;
  889. buf_prefix_content.pass_hash_result = true;
  890. buf_prefix_content.pass_time_stamp = false;
  891. buf_prefix_content.data_align = DPAA_FD_DATA_ALIGNMENT;
  892. params.specific_params.non_rx_params.err_fqid = errq->fqid;
  893. params.specific_params.non_rx_params.dflt_fqid = defq->fqid;
  894. err = fman_port_config(port, &params);
  895. if (err)
  896. pr_err("%s: fman_port_config failed\n", __func__);
  897. err = fman_port_cfg_buf_prefix_content(port, &buf_prefix_content);
  898. if (err)
  899. pr_err("%s: fman_port_cfg_buf_prefix_content failed\n",
  900. __func__);
  901. err = fman_port_init(port);
  902. if (err)
  903. pr_err("%s: fm_port_init failed\n", __func__);
  904. }
  905. static void dpaa_eth_init_rx_port(struct fman_port *port, struct dpaa_bp **bps,
  906. size_t count, struct dpaa_fq *errq,
  907. struct dpaa_fq *defq,
  908. struct dpaa_buffer_layout *buf_layout)
  909. {
  910. struct fman_buffer_prefix_content buf_prefix_content;
  911. struct fman_port_rx_params *rx_p;
  912. struct fman_port_params params;
  913. int i, err;
  914. memset(&params, 0, sizeof(params));
  915. memset(&buf_prefix_content, 0, sizeof(buf_prefix_content));
  916. buf_prefix_content.priv_data_size = buf_layout->priv_data_size;
  917. buf_prefix_content.pass_prs_result = true;
  918. buf_prefix_content.pass_hash_result = true;
  919. buf_prefix_content.pass_time_stamp = false;
  920. buf_prefix_content.data_align = DPAA_FD_DATA_ALIGNMENT;
  921. rx_p = &params.specific_params.rx_params;
  922. rx_p->err_fqid = errq->fqid;
  923. rx_p->dflt_fqid = defq->fqid;
  924. count = min(ARRAY_SIZE(rx_p->ext_buf_pools.ext_buf_pool), count);
  925. rx_p->ext_buf_pools.num_of_pools_used = (u8)count;
  926. for (i = 0; i < count; i++) {
  927. rx_p->ext_buf_pools.ext_buf_pool[i].id = bps[i]->bpid;
  928. rx_p->ext_buf_pools.ext_buf_pool[i].size = (u16)bps[i]->size;
  929. }
  930. err = fman_port_config(port, &params);
  931. if (err)
  932. pr_err("%s: fman_port_config failed\n", __func__);
  933. err = fman_port_cfg_buf_prefix_content(port, &buf_prefix_content);
  934. if (err)
  935. pr_err("%s: fman_port_cfg_buf_prefix_content failed\n",
  936. __func__);
  937. err = fman_port_init(port);
  938. if (err)
  939. pr_err("%s: fm_port_init failed\n", __func__);
  940. }
  941. static void dpaa_eth_init_ports(struct mac_device *mac_dev,
  942. struct dpaa_bp **bps, size_t count,
  943. struct fm_port_fqs *port_fqs,
  944. struct dpaa_buffer_layout *buf_layout,
  945. struct device *dev)
  946. {
  947. struct fman_port *rxport = mac_dev->port[RX];
  948. struct fman_port *txport = mac_dev->port[TX];
  949. dpaa_eth_init_tx_port(txport, port_fqs->tx_errq,
  950. port_fqs->tx_defq, &buf_layout[TX]);
  951. dpaa_eth_init_rx_port(rxport, bps, count, port_fqs->rx_errq,
  952. port_fqs->rx_defq, &buf_layout[RX]);
  953. }
  954. static int dpaa_bman_release(const struct dpaa_bp *dpaa_bp,
  955. struct bm_buffer *bmb, int cnt)
  956. {
  957. int err;
  958. err = bman_release(dpaa_bp->pool, bmb, cnt);
  959. /* Should never occur, address anyway to avoid leaking the buffers */
  960. if (unlikely(WARN_ON(err)) && dpaa_bp->free_buf_cb)
  961. while (cnt-- > 0)
  962. dpaa_bp->free_buf_cb(dpaa_bp, &bmb[cnt]);
  963. return cnt;
  964. }
  965. static void dpaa_release_sgt_members(struct qm_sg_entry *sgt)
  966. {
  967. struct bm_buffer bmb[DPAA_BUFF_RELEASE_MAX];
  968. struct dpaa_bp *dpaa_bp;
  969. int i = 0, j;
  970. memset(bmb, 0, sizeof(bmb));
  971. do {
  972. dpaa_bp = dpaa_bpid2pool(sgt[i].bpid);
  973. if (!dpaa_bp)
  974. return;
  975. j = 0;
  976. do {
  977. WARN_ON(qm_sg_entry_is_ext(&sgt[i]));
  978. bm_buffer_set64(&bmb[j], qm_sg_entry_get64(&sgt[i]));
  979. j++; i++;
  980. } while (j < ARRAY_SIZE(bmb) &&
  981. !qm_sg_entry_is_final(&sgt[i - 1]) &&
  982. sgt[i - 1].bpid == sgt[i].bpid);
  983. dpaa_bman_release(dpaa_bp, bmb, j);
  984. } while (!qm_sg_entry_is_final(&sgt[i - 1]));
  985. }
  986. static void dpaa_fd_release(const struct net_device *net_dev,
  987. const struct qm_fd *fd)
  988. {
  989. struct qm_sg_entry *sgt;
  990. struct dpaa_bp *dpaa_bp;
  991. struct bm_buffer bmb;
  992. dma_addr_t addr;
  993. void *vaddr;
  994. bmb.data = 0;
  995. bm_buffer_set64(&bmb, qm_fd_addr(fd));
  996. dpaa_bp = dpaa_bpid2pool(fd->bpid);
  997. if (!dpaa_bp)
  998. return;
  999. if (qm_fd_get_format(fd) == qm_fd_sg) {
  1000. vaddr = phys_to_virt(qm_fd_addr(fd));
  1001. sgt = vaddr + qm_fd_get_offset(fd);
  1002. dma_unmap_single(dpaa_bp->dev, qm_fd_addr(fd), dpaa_bp->size,
  1003. DMA_FROM_DEVICE);
  1004. dpaa_release_sgt_members(sgt);
  1005. addr = dma_map_single(dpaa_bp->dev, vaddr, dpaa_bp->size,
  1006. DMA_FROM_DEVICE);
  1007. if (dma_mapping_error(dpaa_bp->dev, addr)) {
  1008. dev_err(dpaa_bp->dev, "DMA mapping failed");
  1009. return;
  1010. }
  1011. bm_buffer_set64(&bmb, addr);
  1012. }
  1013. dpaa_bman_release(dpaa_bp, &bmb, 1);
  1014. }
  1015. static void count_ern(struct dpaa_percpu_priv *percpu_priv,
  1016. const union qm_mr_entry *msg)
  1017. {
  1018. switch (msg->ern.rc & QM_MR_RC_MASK) {
  1019. case QM_MR_RC_CGR_TAILDROP:
  1020. percpu_priv->ern_cnt.cg_tdrop++;
  1021. break;
  1022. case QM_MR_RC_WRED:
  1023. percpu_priv->ern_cnt.wred++;
  1024. break;
  1025. case QM_MR_RC_ERROR:
  1026. percpu_priv->ern_cnt.err_cond++;
  1027. break;
  1028. case QM_MR_RC_ORPWINDOW_EARLY:
  1029. percpu_priv->ern_cnt.early_window++;
  1030. break;
  1031. case QM_MR_RC_ORPWINDOW_LATE:
  1032. percpu_priv->ern_cnt.late_window++;
  1033. break;
  1034. case QM_MR_RC_FQ_TAILDROP:
  1035. percpu_priv->ern_cnt.fq_tdrop++;
  1036. break;
  1037. case QM_MR_RC_ORPWINDOW_RETIRED:
  1038. percpu_priv->ern_cnt.fq_retired++;
  1039. break;
  1040. case QM_MR_RC_ORP_ZERO:
  1041. percpu_priv->ern_cnt.orp_zero++;
  1042. break;
  1043. }
  1044. }
  1045. /* Turn on HW checksum computation for this outgoing frame.
  1046. * If the current protocol is not something we support in this regard
  1047. * (or if the stack has already computed the SW checksum), we do nothing.
  1048. *
  1049. * Returns 0 if all goes well (or HW csum doesn't apply), and a negative value
  1050. * otherwise.
  1051. *
  1052. * Note that this function may modify the fd->cmd field and the skb data buffer
  1053. * (the Parse Results area).
  1054. */
  1055. static int dpaa_enable_tx_csum(struct dpaa_priv *priv,
  1056. struct sk_buff *skb,
  1057. struct qm_fd *fd,
  1058. char *parse_results)
  1059. {
  1060. struct fman_prs_result *parse_result;
  1061. u16 ethertype = ntohs(skb->protocol);
  1062. struct ipv6hdr *ipv6h = NULL;
  1063. struct iphdr *iph;
  1064. int retval = 0;
  1065. u8 l4_proto;
  1066. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1067. return 0;
  1068. /* Note: L3 csum seems to be already computed in sw, but we can't choose
  1069. * L4 alone from the FM configuration anyway.
  1070. */
  1071. /* Fill in some fields of the Parse Results array, so the FMan
  1072. * can find them as if they came from the FMan Parser.
  1073. */
  1074. parse_result = (struct fman_prs_result *)parse_results;
  1075. /* If we're dealing with VLAN, get the real Ethernet type */
  1076. if (ethertype == ETH_P_8021Q) {
  1077. /* We can't always assume the MAC header is set correctly
  1078. * by the stack, so reset to beginning of skb->data
  1079. */
  1080. skb_reset_mac_header(skb);
  1081. ethertype = ntohs(vlan_eth_hdr(skb)->h_vlan_encapsulated_proto);
  1082. }
  1083. /* Fill in the relevant L3 parse result fields
  1084. * and read the L4 protocol type
  1085. */
  1086. switch (ethertype) {
  1087. case ETH_P_IP:
  1088. parse_result->l3r = cpu_to_be16(FM_L3_PARSE_RESULT_IPV4);
  1089. iph = ip_hdr(skb);
  1090. WARN_ON(!iph);
  1091. l4_proto = iph->protocol;
  1092. break;
  1093. case ETH_P_IPV6:
  1094. parse_result->l3r = cpu_to_be16(FM_L3_PARSE_RESULT_IPV6);
  1095. ipv6h = ipv6_hdr(skb);
  1096. WARN_ON(!ipv6h);
  1097. l4_proto = ipv6h->nexthdr;
  1098. break;
  1099. default:
  1100. /* We shouldn't even be here */
  1101. if (net_ratelimit())
  1102. netif_alert(priv, tx_err, priv->net_dev,
  1103. "Can't compute HW csum for L3 proto 0x%x\n",
  1104. ntohs(skb->protocol));
  1105. retval = -EIO;
  1106. goto return_error;
  1107. }
  1108. /* Fill in the relevant L4 parse result fields */
  1109. switch (l4_proto) {
  1110. case IPPROTO_UDP:
  1111. parse_result->l4r = FM_L4_PARSE_RESULT_UDP;
  1112. break;
  1113. case IPPROTO_TCP:
  1114. parse_result->l4r = FM_L4_PARSE_RESULT_TCP;
  1115. break;
  1116. default:
  1117. if (net_ratelimit())
  1118. netif_alert(priv, tx_err, priv->net_dev,
  1119. "Can't compute HW csum for L4 proto 0x%x\n",
  1120. l4_proto);
  1121. retval = -EIO;
  1122. goto return_error;
  1123. }
  1124. /* At index 0 is IPOffset_1 as defined in the Parse Results */
  1125. parse_result->ip_off[0] = (u8)skb_network_offset(skb);
  1126. parse_result->l4_off = (u8)skb_transport_offset(skb);
  1127. /* Enable L3 (and L4, if TCP or UDP) HW checksum. */
  1128. fd->cmd |= cpu_to_be32(FM_FD_CMD_RPD | FM_FD_CMD_DTC);
  1129. /* On P1023 and similar platforms fd->cmd interpretation could
  1130. * be disabled by setting CONTEXT_A bit ICMD; currently this bit
  1131. * is not set so we do not need to check; in the future, if/when
  1132. * using context_a we need to check this bit
  1133. */
  1134. return_error:
  1135. return retval;
  1136. }
  1137. static int dpaa_bp_add_8_bufs(const struct dpaa_bp *dpaa_bp)
  1138. {
  1139. struct device *dev = dpaa_bp->dev;
  1140. struct bm_buffer bmb[8];
  1141. dma_addr_t addr;
  1142. void *new_buf;
  1143. u8 i;
  1144. for (i = 0; i < 8; i++) {
  1145. new_buf = netdev_alloc_frag(dpaa_bp->raw_size);
  1146. if (unlikely(!new_buf)) {
  1147. dev_err(dev, "netdev_alloc_frag() failed, size %zu\n",
  1148. dpaa_bp->raw_size);
  1149. goto release_previous_buffs;
  1150. }
  1151. new_buf = PTR_ALIGN(new_buf, SMP_CACHE_BYTES);
  1152. addr = dma_map_single(dev, new_buf,
  1153. dpaa_bp->size, DMA_FROM_DEVICE);
  1154. if (unlikely(dma_mapping_error(dev, addr))) {
  1155. dev_err(dpaa_bp->dev, "DMA map failed");
  1156. goto release_previous_buffs;
  1157. }
  1158. bmb[i].data = 0;
  1159. bm_buffer_set64(&bmb[i], addr);
  1160. }
  1161. release_bufs:
  1162. return dpaa_bman_release(dpaa_bp, bmb, i);
  1163. release_previous_buffs:
  1164. WARN_ONCE(1, "dpaa_eth: failed to add buffers on Rx\n");
  1165. bm_buffer_set64(&bmb[i], 0);
  1166. /* Avoid releasing a completely null buffer; bman_release() requires
  1167. * at least one buffer.
  1168. */
  1169. if (likely(i))
  1170. goto release_bufs;
  1171. return 0;
  1172. }
  1173. static int dpaa_bp_seed(struct dpaa_bp *dpaa_bp)
  1174. {
  1175. int i;
  1176. /* Give each CPU an allotment of "config_count" buffers */
  1177. for_each_possible_cpu(i) {
  1178. int *count_ptr = per_cpu_ptr(dpaa_bp->percpu_count, i);
  1179. int j;
  1180. /* Although we access another CPU's counters here
  1181. * we do it at boot time so it is safe
  1182. */
  1183. for (j = 0; j < dpaa_bp->config_count; j += 8)
  1184. *count_ptr += dpaa_bp_add_8_bufs(dpaa_bp);
  1185. }
  1186. return 0;
  1187. }
  1188. /* Add buffers/(pages) for Rx processing whenever bpool count falls below
  1189. * REFILL_THRESHOLD.
  1190. */
  1191. static int dpaa_eth_refill_bpool(struct dpaa_bp *dpaa_bp, int *countptr)
  1192. {
  1193. int count = *countptr;
  1194. int new_bufs;
  1195. if (unlikely(count < FSL_DPAA_ETH_REFILL_THRESHOLD)) {
  1196. do {
  1197. new_bufs = dpaa_bp_add_8_bufs(dpaa_bp);
  1198. if (unlikely(!new_bufs)) {
  1199. /* Avoid looping forever if we've temporarily
  1200. * run out of memory. We'll try again at the
  1201. * next NAPI cycle.
  1202. */
  1203. break;
  1204. }
  1205. count += new_bufs;
  1206. } while (count < FSL_DPAA_ETH_MAX_BUF_COUNT);
  1207. *countptr = count;
  1208. if (unlikely(count < FSL_DPAA_ETH_MAX_BUF_COUNT))
  1209. return -ENOMEM;
  1210. }
  1211. return 0;
  1212. }
  1213. static int dpaa_eth_refill_bpools(struct dpaa_priv *priv)
  1214. {
  1215. struct dpaa_bp *dpaa_bp;
  1216. int *countptr;
  1217. int res, i;
  1218. for (i = 0; i < DPAA_BPS_NUM; i++) {
  1219. dpaa_bp = priv->dpaa_bps[i];
  1220. if (!dpaa_bp)
  1221. return -EINVAL;
  1222. countptr = this_cpu_ptr(dpaa_bp->percpu_count);
  1223. res = dpaa_eth_refill_bpool(dpaa_bp, countptr);
  1224. if (res)
  1225. return res;
  1226. }
  1227. return 0;
  1228. }
  1229. /* Cleanup function for outgoing frame descriptors that were built on Tx path,
  1230. * either contiguous frames or scatter/gather ones.
  1231. * Skb freeing is not handled here.
  1232. *
  1233. * This function may be called on error paths in the Tx function, so guard
  1234. * against cases when not all fd relevant fields were filled in.
  1235. *
  1236. * Return the skb backpointer, since for S/G frames the buffer containing it
  1237. * gets freed here.
  1238. */
  1239. static struct sk_buff *dpaa_cleanup_tx_fd(const struct dpaa_priv *priv,
  1240. const struct qm_fd *fd)
  1241. {
  1242. const enum dma_data_direction dma_dir = DMA_TO_DEVICE;
  1243. struct device *dev = priv->net_dev->dev.parent;
  1244. dma_addr_t addr = qm_fd_addr(fd);
  1245. const struct qm_sg_entry *sgt;
  1246. struct sk_buff **skbh, *skb;
  1247. int nr_frags, i;
  1248. skbh = (struct sk_buff **)phys_to_virt(addr);
  1249. skb = *skbh;
  1250. if (unlikely(qm_fd_get_format(fd) == qm_fd_sg)) {
  1251. nr_frags = skb_shinfo(skb)->nr_frags;
  1252. dma_unmap_single(dev, addr, qm_fd_get_offset(fd) +
  1253. sizeof(struct qm_sg_entry) * (1 + nr_frags),
  1254. dma_dir);
  1255. /* The sgt buffer has been allocated with netdev_alloc_frag(),
  1256. * it's from lowmem.
  1257. */
  1258. sgt = phys_to_virt(addr + qm_fd_get_offset(fd));
  1259. /* sgt[0] is from lowmem, was dma_map_single()-ed */
  1260. dma_unmap_single(dev, qm_sg_addr(&sgt[0]),
  1261. qm_sg_entry_get_len(&sgt[0]), dma_dir);
  1262. /* remaining pages were mapped with skb_frag_dma_map() */
  1263. for (i = 1; i < nr_frags; i++) {
  1264. WARN_ON(qm_sg_entry_is_ext(&sgt[i]));
  1265. dma_unmap_page(dev, qm_sg_addr(&sgt[i]),
  1266. qm_sg_entry_get_len(&sgt[i]), dma_dir);
  1267. }
  1268. /* Free the page frag that we allocated on Tx */
  1269. skb_free_frag(phys_to_virt(addr));
  1270. } else {
  1271. dma_unmap_single(dev, addr,
  1272. skb_tail_pointer(skb) - (u8 *)skbh, dma_dir);
  1273. }
  1274. return skb;
  1275. }
  1276. /* Build a linear skb around the received buffer.
  1277. * We are guaranteed there is enough room at the end of the data buffer to
  1278. * accommodate the shared info area of the skb.
  1279. */
  1280. static struct sk_buff *contig_fd_to_skb(const struct dpaa_priv *priv,
  1281. const struct qm_fd *fd)
  1282. {
  1283. ssize_t fd_off = qm_fd_get_offset(fd);
  1284. dma_addr_t addr = qm_fd_addr(fd);
  1285. struct dpaa_bp *dpaa_bp;
  1286. struct sk_buff *skb;
  1287. void *vaddr;
  1288. vaddr = phys_to_virt(addr);
  1289. WARN_ON(!IS_ALIGNED((unsigned long)vaddr, SMP_CACHE_BYTES));
  1290. dpaa_bp = dpaa_bpid2pool(fd->bpid);
  1291. if (!dpaa_bp)
  1292. goto free_buffer;
  1293. skb = build_skb(vaddr, dpaa_bp->size +
  1294. SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
  1295. if (unlikely(!skb)) {
  1296. WARN_ONCE(1, "Build skb failure on Rx\n");
  1297. goto free_buffer;
  1298. }
  1299. WARN_ON(fd_off != priv->rx_headroom);
  1300. skb_reserve(skb, fd_off);
  1301. skb_put(skb, qm_fd_get_length(fd));
  1302. skb->ip_summed = CHECKSUM_NONE;
  1303. return skb;
  1304. free_buffer:
  1305. skb_free_frag(vaddr);
  1306. return NULL;
  1307. }
  1308. /* Build an skb with the data of the first S/G entry in the linear portion and
  1309. * the rest of the frame as skb fragments.
  1310. *
  1311. * The page fragment holding the S/G Table is recycled here.
  1312. */
  1313. static struct sk_buff *sg_fd_to_skb(const struct dpaa_priv *priv,
  1314. const struct qm_fd *fd)
  1315. {
  1316. ssize_t fd_off = qm_fd_get_offset(fd);
  1317. dma_addr_t addr = qm_fd_addr(fd);
  1318. const struct qm_sg_entry *sgt;
  1319. struct page *page, *head_page;
  1320. struct dpaa_bp *dpaa_bp;
  1321. void *vaddr, *sg_vaddr;
  1322. int frag_off, frag_len;
  1323. struct sk_buff *skb;
  1324. dma_addr_t sg_addr;
  1325. int page_offset;
  1326. unsigned int sz;
  1327. int *count_ptr;
  1328. int i;
  1329. vaddr = phys_to_virt(addr);
  1330. WARN_ON(!IS_ALIGNED((unsigned long)vaddr, SMP_CACHE_BYTES));
  1331. /* Iterate through the SGT entries and add data buffers to the skb */
  1332. sgt = vaddr + fd_off;
  1333. for (i = 0; i < DPAA_SGT_MAX_ENTRIES; i++) {
  1334. /* Extension bit is not supported */
  1335. WARN_ON(qm_sg_entry_is_ext(&sgt[i]));
  1336. sg_addr = qm_sg_addr(&sgt[i]);
  1337. sg_vaddr = phys_to_virt(sg_addr);
  1338. WARN_ON(!IS_ALIGNED((unsigned long)sg_vaddr,
  1339. SMP_CACHE_BYTES));
  1340. /* We may use multiple Rx pools */
  1341. dpaa_bp = dpaa_bpid2pool(sgt[i].bpid);
  1342. if (!dpaa_bp)
  1343. goto free_buffers;
  1344. count_ptr = this_cpu_ptr(dpaa_bp->percpu_count);
  1345. dma_unmap_single(dpaa_bp->dev, sg_addr, dpaa_bp->size,
  1346. DMA_FROM_DEVICE);
  1347. if (i == 0) {
  1348. sz = dpaa_bp->size +
  1349. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  1350. skb = build_skb(sg_vaddr, sz);
  1351. if (WARN_ON(unlikely(!skb)))
  1352. goto free_buffers;
  1353. skb->ip_summed = CHECKSUM_NONE;
  1354. /* Make sure forwarded skbs will have enough space
  1355. * on Tx, if extra headers are added.
  1356. */
  1357. WARN_ON(fd_off != priv->rx_headroom);
  1358. skb_reserve(skb, fd_off);
  1359. skb_put(skb, qm_sg_entry_get_len(&sgt[i]));
  1360. } else {
  1361. /* Not the first S/G entry; all data from buffer will
  1362. * be added in an skb fragment; fragment index is offset
  1363. * by one since first S/G entry was incorporated in the
  1364. * linear part of the skb.
  1365. *
  1366. * Caution: 'page' may be a tail page.
  1367. */
  1368. page = virt_to_page(sg_vaddr);
  1369. head_page = virt_to_head_page(sg_vaddr);
  1370. /* Compute offset in (possibly tail) page */
  1371. page_offset = ((unsigned long)sg_vaddr &
  1372. (PAGE_SIZE - 1)) +
  1373. (page_address(page) - page_address(head_page));
  1374. /* page_offset only refers to the beginning of sgt[i];
  1375. * but the buffer itself may have an internal offset.
  1376. */
  1377. frag_off = qm_sg_entry_get_off(&sgt[i]) + page_offset;
  1378. frag_len = qm_sg_entry_get_len(&sgt[i]);
  1379. /* skb_add_rx_frag() does no checking on the page; if
  1380. * we pass it a tail page, we'll end up with
  1381. * bad page accounting and eventually with segafults.
  1382. */
  1383. skb_add_rx_frag(skb, i - 1, head_page, frag_off,
  1384. frag_len, dpaa_bp->size);
  1385. }
  1386. /* Update the pool count for the current {cpu x bpool} */
  1387. (*count_ptr)--;
  1388. if (qm_sg_entry_is_final(&sgt[i]))
  1389. break;
  1390. }
  1391. WARN_ONCE(i == DPAA_SGT_MAX_ENTRIES, "No final bit on SGT\n");
  1392. /* free the SG table buffer */
  1393. skb_free_frag(vaddr);
  1394. return skb;
  1395. free_buffers:
  1396. /* compensate sw bpool counter changes */
  1397. for (i--; i >= 0; i--) {
  1398. dpaa_bp = dpaa_bpid2pool(sgt[i].bpid);
  1399. if (dpaa_bp) {
  1400. count_ptr = this_cpu_ptr(dpaa_bp->percpu_count);
  1401. (*count_ptr)++;
  1402. }
  1403. }
  1404. /* free all the SG entries */
  1405. for (i = 0; i < DPAA_SGT_MAX_ENTRIES ; i++) {
  1406. sg_addr = qm_sg_addr(&sgt[i]);
  1407. sg_vaddr = phys_to_virt(sg_addr);
  1408. skb_free_frag(sg_vaddr);
  1409. dpaa_bp = dpaa_bpid2pool(sgt[i].bpid);
  1410. if (dpaa_bp) {
  1411. count_ptr = this_cpu_ptr(dpaa_bp->percpu_count);
  1412. (*count_ptr)--;
  1413. }
  1414. if (qm_sg_entry_is_final(&sgt[i]))
  1415. break;
  1416. }
  1417. /* free the SGT fragment */
  1418. skb_free_frag(vaddr);
  1419. return NULL;
  1420. }
  1421. static int skb_to_contig_fd(struct dpaa_priv *priv,
  1422. struct sk_buff *skb, struct qm_fd *fd,
  1423. int *offset)
  1424. {
  1425. struct net_device *net_dev = priv->net_dev;
  1426. struct device *dev = net_dev->dev.parent;
  1427. enum dma_data_direction dma_dir;
  1428. unsigned char *buffer_start;
  1429. struct sk_buff **skbh;
  1430. dma_addr_t addr;
  1431. int err;
  1432. /* We are guaranteed to have at least tx_headroom bytes
  1433. * available, so just use that for offset.
  1434. */
  1435. fd->bpid = FSL_DPAA_BPID_INV;
  1436. buffer_start = skb->data - priv->tx_headroom;
  1437. dma_dir = DMA_TO_DEVICE;
  1438. skbh = (struct sk_buff **)buffer_start;
  1439. *skbh = skb;
  1440. /* Enable L3/L4 hardware checksum computation.
  1441. *
  1442. * We must do this before dma_map_single(DMA_TO_DEVICE), because we may
  1443. * need to write into the skb.
  1444. */
  1445. err = dpaa_enable_tx_csum(priv, skb, fd,
  1446. ((char *)skbh) + DPAA_TX_PRIV_DATA_SIZE);
  1447. if (unlikely(err < 0)) {
  1448. if (net_ratelimit())
  1449. netif_err(priv, tx_err, net_dev, "HW csum error: %d\n",
  1450. err);
  1451. return err;
  1452. }
  1453. /* Fill in the rest of the FD fields */
  1454. qm_fd_set_contig(fd, priv->tx_headroom, skb->len);
  1455. fd->cmd |= cpu_to_be32(FM_FD_CMD_FCO);
  1456. /* Map the entire buffer size that may be seen by FMan, but no more */
  1457. addr = dma_map_single(dev, skbh,
  1458. skb_tail_pointer(skb) - buffer_start, dma_dir);
  1459. if (unlikely(dma_mapping_error(dev, addr))) {
  1460. if (net_ratelimit())
  1461. netif_err(priv, tx_err, net_dev, "dma_map_single() failed\n");
  1462. return -EINVAL;
  1463. }
  1464. qm_fd_addr_set64(fd, addr);
  1465. return 0;
  1466. }
  1467. static int skb_to_sg_fd(struct dpaa_priv *priv,
  1468. struct sk_buff *skb, struct qm_fd *fd)
  1469. {
  1470. const enum dma_data_direction dma_dir = DMA_TO_DEVICE;
  1471. const int nr_frags = skb_shinfo(skb)->nr_frags;
  1472. struct net_device *net_dev = priv->net_dev;
  1473. struct device *dev = net_dev->dev.parent;
  1474. struct qm_sg_entry *sgt;
  1475. struct sk_buff **skbh;
  1476. int i, j, err, sz;
  1477. void *buffer_start;
  1478. skb_frag_t *frag;
  1479. dma_addr_t addr;
  1480. size_t frag_len;
  1481. void *sgt_buf;
  1482. /* get a page frag to store the SGTable */
  1483. sz = SKB_DATA_ALIGN(priv->tx_headroom +
  1484. sizeof(struct qm_sg_entry) * (1 + nr_frags));
  1485. sgt_buf = netdev_alloc_frag(sz);
  1486. if (unlikely(!sgt_buf)) {
  1487. netdev_err(net_dev, "netdev_alloc_frag() failed for size %d\n",
  1488. sz);
  1489. return -ENOMEM;
  1490. }
  1491. /* Enable L3/L4 hardware checksum computation.
  1492. *
  1493. * We must do this before dma_map_single(DMA_TO_DEVICE), because we may
  1494. * need to write into the skb.
  1495. */
  1496. err = dpaa_enable_tx_csum(priv, skb, fd,
  1497. sgt_buf + DPAA_TX_PRIV_DATA_SIZE);
  1498. if (unlikely(err < 0)) {
  1499. if (net_ratelimit())
  1500. netif_err(priv, tx_err, net_dev, "HW csum error: %d\n",
  1501. err);
  1502. goto csum_failed;
  1503. }
  1504. sgt = (struct qm_sg_entry *)(sgt_buf + priv->tx_headroom);
  1505. qm_sg_entry_set_len(&sgt[0], skb_headlen(skb));
  1506. sgt[0].bpid = FSL_DPAA_BPID_INV;
  1507. sgt[0].offset = 0;
  1508. addr = dma_map_single(dev, skb->data,
  1509. skb_headlen(skb), dma_dir);
  1510. if (unlikely(dma_mapping_error(dev, addr))) {
  1511. dev_err(dev, "DMA mapping failed");
  1512. err = -EINVAL;
  1513. goto sg0_map_failed;
  1514. }
  1515. qm_sg_entry_set64(&sgt[0], addr);
  1516. /* populate the rest of SGT entries */
  1517. frag = &skb_shinfo(skb)->frags[0];
  1518. frag_len = frag->size;
  1519. for (i = 1; i <= nr_frags; i++, frag++) {
  1520. WARN_ON(!skb_frag_page(frag));
  1521. addr = skb_frag_dma_map(dev, frag, 0,
  1522. frag_len, dma_dir);
  1523. if (unlikely(dma_mapping_error(dev, addr))) {
  1524. dev_err(dev, "DMA mapping failed");
  1525. err = -EINVAL;
  1526. goto sg_map_failed;
  1527. }
  1528. qm_sg_entry_set_len(&sgt[i], frag_len);
  1529. sgt[i].bpid = FSL_DPAA_BPID_INV;
  1530. sgt[i].offset = 0;
  1531. /* keep the offset in the address */
  1532. qm_sg_entry_set64(&sgt[i], addr);
  1533. frag_len = frag->size;
  1534. }
  1535. qm_sg_entry_set_f(&sgt[i - 1], frag_len);
  1536. qm_fd_set_sg(fd, priv->tx_headroom, skb->len);
  1537. /* DMA map the SGT page */
  1538. buffer_start = (void *)sgt - priv->tx_headroom;
  1539. skbh = (struct sk_buff **)buffer_start;
  1540. *skbh = skb;
  1541. addr = dma_map_single(dev, buffer_start, priv->tx_headroom +
  1542. sizeof(struct qm_sg_entry) * (1 + nr_frags),
  1543. dma_dir);
  1544. if (unlikely(dma_mapping_error(dev, addr))) {
  1545. dev_err(dev, "DMA mapping failed");
  1546. err = -EINVAL;
  1547. goto sgt_map_failed;
  1548. }
  1549. fd->bpid = FSL_DPAA_BPID_INV;
  1550. fd->cmd |= cpu_to_be32(FM_FD_CMD_FCO);
  1551. qm_fd_addr_set64(fd, addr);
  1552. return 0;
  1553. sgt_map_failed:
  1554. sg_map_failed:
  1555. for (j = 0; j < i; j++)
  1556. dma_unmap_page(dev, qm_sg_addr(&sgt[j]),
  1557. qm_sg_entry_get_len(&sgt[j]), dma_dir);
  1558. sg0_map_failed:
  1559. csum_failed:
  1560. skb_free_frag(sgt_buf);
  1561. return err;
  1562. }
  1563. static inline int dpaa_xmit(struct dpaa_priv *priv,
  1564. struct rtnl_link_stats64 *percpu_stats,
  1565. int queue,
  1566. struct qm_fd *fd)
  1567. {
  1568. struct qman_fq *egress_fq;
  1569. int err, i;
  1570. egress_fq = priv->egress_fqs[queue];
  1571. if (fd->bpid == FSL_DPAA_BPID_INV)
  1572. fd->cmd |= cpu_to_be32(qman_fq_fqid(priv->conf_fqs[queue]));
  1573. /* Trace this Tx fd */
  1574. trace_dpaa_tx_fd(priv->net_dev, egress_fq, fd);
  1575. for (i = 0; i < DPAA_ENQUEUE_RETRIES; i++) {
  1576. err = qman_enqueue(egress_fq, fd);
  1577. if (err != -EBUSY)
  1578. break;
  1579. }
  1580. if (unlikely(err < 0)) {
  1581. percpu_stats->tx_errors++;
  1582. percpu_stats->tx_fifo_errors++;
  1583. return err;
  1584. }
  1585. percpu_stats->tx_packets++;
  1586. percpu_stats->tx_bytes += qm_fd_get_length(fd);
  1587. return 0;
  1588. }
  1589. static int dpaa_start_xmit(struct sk_buff *skb, struct net_device *net_dev)
  1590. {
  1591. const int queue_mapping = skb_get_queue_mapping(skb);
  1592. bool nonlinear = skb_is_nonlinear(skb);
  1593. struct rtnl_link_stats64 *percpu_stats;
  1594. struct dpaa_percpu_priv *percpu_priv;
  1595. struct dpaa_priv *priv;
  1596. struct qm_fd fd;
  1597. int offset = 0;
  1598. int err = 0;
  1599. priv = netdev_priv(net_dev);
  1600. percpu_priv = this_cpu_ptr(priv->percpu_priv);
  1601. percpu_stats = &percpu_priv->stats;
  1602. qm_fd_clear_fd(&fd);
  1603. if (!nonlinear) {
  1604. /* We're going to store the skb backpointer at the beginning
  1605. * of the data buffer, so we need a privately owned skb
  1606. *
  1607. * We've made sure skb is not shared in dev->priv_flags,
  1608. * we need to verify the skb head is not cloned
  1609. */
  1610. if (skb_cow_head(skb, priv->tx_headroom))
  1611. goto enomem;
  1612. WARN_ON(skb_is_nonlinear(skb));
  1613. }
  1614. /* MAX_SKB_FRAGS is equal or larger than our dpaa_SGT_MAX_ENTRIES;
  1615. * make sure we don't feed FMan with more fragments than it supports.
  1616. */
  1617. if (nonlinear &&
  1618. likely(skb_shinfo(skb)->nr_frags < DPAA_SGT_MAX_ENTRIES)) {
  1619. /* Just create a S/G fd based on the skb */
  1620. err = skb_to_sg_fd(priv, skb, &fd);
  1621. percpu_priv->tx_frag_skbuffs++;
  1622. } else {
  1623. /* If the egress skb contains more fragments than we support
  1624. * we have no choice but to linearize it ourselves.
  1625. */
  1626. if (unlikely(nonlinear) && __skb_linearize(skb))
  1627. goto enomem;
  1628. /* Finally, create a contig FD from this skb */
  1629. err = skb_to_contig_fd(priv, skb, &fd, &offset);
  1630. }
  1631. if (unlikely(err < 0))
  1632. goto skb_to_fd_failed;
  1633. if (likely(dpaa_xmit(priv, percpu_stats, queue_mapping, &fd) == 0))
  1634. return NETDEV_TX_OK;
  1635. dpaa_cleanup_tx_fd(priv, &fd);
  1636. skb_to_fd_failed:
  1637. enomem:
  1638. percpu_stats->tx_errors++;
  1639. dev_kfree_skb(skb);
  1640. return NETDEV_TX_OK;
  1641. }
  1642. static void dpaa_rx_error(struct net_device *net_dev,
  1643. const struct dpaa_priv *priv,
  1644. struct dpaa_percpu_priv *percpu_priv,
  1645. const struct qm_fd *fd,
  1646. u32 fqid)
  1647. {
  1648. if (net_ratelimit())
  1649. netif_err(priv, hw, net_dev, "Err FD status = 0x%08x\n",
  1650. be32_to_cpu(fd->status) & FM_FD_STAT_RX_ERRORS);
  1651. percpu_priv->stats.rx_errors++;
  1652. if (be32_to_cpu(fd->status) & FM_FD_ERR_DMA)
  1653. percpu_priv->rx_errors.dme++;
  1654. if (be32_to_cpu(fd->status) & FM_FD_ERR_PHYSICAL)
  1655. percpu_priv->rx_errors.fpe++;
  1656. if (be32_to_cpu(fd->status) & FM_FD_ERR_SIZE)
  1657. percpu_priv->rx_errors.fse++;
  1658. if (be32_to_cpu(fd->status) & FM_FD_ERR_PRS_HDR_ERR)
  1659. percpu_priv->rx_errors.phe++;
  1660. dpaa_fd_release(net_dev, fd);
  1661. }
  1662. static void dpaa_tx_error(struct net_device *net_dev,
  1663. const struct dpaa_priv *priv,
  1664. struct dpaa_percpu_priv *percpu_priv,
  1665. const struct qm_fd *fd,
  1666. u32 fqid)
  1667. {
  1668. struct sk_buff *skb;
  1669. if (net_ratelimit())
  1670. netif_warn(priv, hw, net_dev, "FD status = 0x%08x\n",
  1671. be32_to_cpu(fd->status) & FM_FD_STAT_TX_ERRORS);
  1672. percpu_priv->stats.tx_errors++;
  1673. skb = dpaa_cleanup_tx_fd(priv, fd);
  1674. dev_kfree_skb(skb);
  1675. }
  1676. static int dpaa_eth_poll(struct napi_struct *napi, int budget)
  1677. {
  1678. struct dpaa_napi_portal *np =
  1679. container_of(napi, struct dpaa_napi_portal, napi);
  1680. int cleaned = qman_p_poll_dqrr(np->p, budget);
  1681. if (cleaned < budget) {
  1682. napi_complete_done(napi, cleaned);
  1683. qman_p_irqsource_add(np->p, QM_PIRQ_DQRI);
  1684. } else if (np->down) {
  1685. qman_p_irqsource_add(np->p, QM_PIRQ_DQRI);
  1686. }
  1687. return cleaned;
  1688. }
  1689. static void dpaa_tx_conf(struct net_device *net_dev,
  1690. const struct dpaa_priv *priv,
  1691. struct dpaa_percpu_priv *percpu_priv,
  1692. const struct qm_fd *fd,
  1693. u32 fqid)
  1694. {
  1695. struct sk_buff *skb;
  1696. if (unlikely(be32_to_cpu(fd->status) & FM_FD_STAT_TX_ERRORS)) {
  1697. if (net_ratelimit())
  1698. netif_warn(priv, hw, net_dev, "FD status = 0x%08x\n",
  1699. be32_to_cpu(fd->status) &
  1700. FM_FD_STAT_TX_ERRORS);
  1701. percpu_priv->stats.tx_errors++;
  1702. }
  1703. percpu_priv->tx_confirm++;
  1704. skb = dpaa_cleanup_tx_fd(priv, fd);
  1705. consume_skb(skb);
  1706. }
  1707. static inline int dpaa_eth_napi_schedule(struct dpaa_percpu_priv *percpu_priv,
  1708. struct qman_portal *portal)
  1709. {
  1710. if (unlikely(in_irq() || !in_serving_softirq())) {
  1711. /* Disable QMan IRQ and invoke NAPI */
  1712. qman_p_irqsource_remove(portal, QM_PIRQ_DQRI);
  1713. percpu_priv->np.p = portal;
  1714. napi_schedule(&percpu_priv->np.napi);
  1715. percpu_priv->in_interrupt++;
  1716. return 1;
  1717. }
  1718. return 0;
  1719. }
  1720. static enum qman_cb_dqrr_result rx_error_dqrr(struct qman_portal *portal,
  1721. struct qman_fq *fq,
  1722. const struct qm_dqrr_entry *dq)
  1723. {
  1724. struct dpaa_fq *dpaa_fq = container_of(fq, struct dpaa_fq, fq_base);
  1725. struct dpaa_percpu_priv *percpu_priv;
  1726. struct net_device *net_dev;
  1727. struct dpaa_bp *dpaa_bp;
  1728. struct dpaa_priv *priv;
  1729. net_dev = dpaa_fq->net_dev;
  1730. priv = netdev_priv(net_dev);
  1731. dpaa_bp = dpaa_bpid2pool(dq->fd.bpid);
  1732. if (!dpaa_bp)
  1733. return qman_cb_dqrr_consume;
  1734. percpu_priv = this_cpu_ptr(priv->percpu_priv);
  1735. if (dpaa_eth_napi_schedule(percpu_priv, portal))
  1736. return qman_cb_dqrr_stop;
  1737. if (dpaa_eth_refill_bpools(priv))
  1738. /* Unable to refill the buffer pool due to insufficient
  1739. * system memory. Just release the frame back into the pool,
  1740. * otherwise we'll soon end up with an empty buffer pool.
  1741. */
  1742. dpaa_fd_release(net_dev, &dq->fd);
  1743. else
  1744. dpaa_rx_error(net_dev, priv, percpu_priv, &dq->fd, fq->fqid);
  1745. return qman_cb_dqrr_consume;
  1746. }
  1747. static enum qman_cb_dqrr_result rx_default_dqrr(struct qman_portal *portal,
  1748. struct qman_fq *fq,
  1749. const struct qm_dqrr_entry *dq)
  1750. {
  1751. struct rtnl_link_stats64 *percpu_stats;
  1752. struct dpaa_percpu_priv *percpu_priv;
  1753. const struct qm_fd *fd = &dq->fd;
  1754. dma_addr_t addr = qm_fd_addr(fd);
  1755. enum qm_fd_format fd_format;
  1756. struct net_device *net_dev;
  1757. u32 fd_status = fd->status;
  1758. struct dpaa_bp *dpaa_bp;
  1759. struct dpaa_priv *priv;
  1760. unsigned int skb_len;
  1761. struct sk_buff *skb;
  1762. int *count_ptr;
  1763. fd_status = be32_to_cpu(fd->status);
  1764. fd_format = qm_fd_get_format(fd);
  1765. net_dev = ((struct dpaa_fq *)fq)->net_dev;
  1766. priv = netdev_priv(net_dev);
  1767. dpaa_bp = dpaa_bpid2pool(dq->fd.bpid);
  1768. if (!dpaa_bp)
  1769. return qman_cb_dqrr_consume;
  1770. /* Trace the Rx fd */
  1771. trace_dpaa_rx_fd(net_dev, fq, &dq->fd);
  1772. percpu_priv = this_cpu_ptr(priv->percpu_priv);
  1773. percpu_stats = &percpu_priv->stats;
  1774. if (unlikely(dpaa_eth_napi_schedule(percpu_priv, portal)))
  1775. return qman_cb_dqrr_stop;
  1776. /* Make sure we didn't run out of buffers */
  1777. if (unlikely(dpaa_eth_refill_bpools(priv))) {
  1778. /* Unable to refill the buffer pool due to insufficient
  1779. * system memory. Just release the frame back into the pool,
  1780. * otherwise we'll soon end up with an empty buffer pool.
  1781. */
  1782. dpaa_fd_release(net_dev, &dq->fd);
  1783. return qman_cb_dqrr_consume;
  1784. }
  1785. if (unlikely(fd_status & FM_FD_STAT_RX_ERRORS) != 0) {
  1786. if (net_ratelimit())
  1787. netif_warn(priv, hw, net_dev, "FD status = 0x%08x\n",
  1788. fd_status & FM_FD_STAT_RX_ERRORS);
  1789. percpu_stats->rx_errors++;
  1790. dpaa_fd_release(net_dev, fd);
  1791. return qman_cb_dqrr_consume;
  1792. }
  1793. dpaa_bp = dpaa_bpid2pool(fd->bpid);
  1794. if (!dpaa_bp)
  1795. return qman_cb_dqrr_consume;
  1796. dma_unmap_single(dpaa_bp->dev, addr, dpaa_bp->size, DMA_FROM_DEVICE);
  1797. /* prefetch the first 64 bytes of the frame or the SGT start */
  1798. prefetch(phys_to_virt(addr) + qm_fd_get_offset(fd));
  1799. fd_format = qm_fd_get_format(fd);
  1800. /* The only FD types that we may receive are contig and S/G */
  1801. WARN_ON((fd_format != qm_fd_contig) && (fd_format != qm_fd_sg));
  1802. /* Account for either the contig buffer or the SGT buffer (depending on
  1803. * which case we were in) having been removed from the pool.
  1804. */
  1805. count_ptr = this_cpu_ptr(dpaa_bp->percpu_count);
  1806. (*count_ptr)--;
  1807. if (likely(fd_format == qm_fd_contig))
  1808. skb = contig_fd_to_skb(priv, fd);
  1809. else
  1810. skb = sg_fd_to_skb(priv, fd);
  1811. if (!skb)
  1812. return qman_cb_dqrr_consume;
  1813. skb->protocol = eth_type_trans(skb, net_dev);
  1814. skb_len = skb->len;
  1815. if (unlikely(netif_receive_skb(skb) == NET_RX_DROP))
  1816. return qman_cb_dqrr_consume;
  1817. percpu_stats->rx_packets++;
  1818. percpu_stats->rx_bytes += skb_len;
  1819. return qman_cb_dqrr_consume;
  1820. }
  1821. static enum qman_cb_dqrr_result conf_error_dqrr(struct qman_portal *portal,
  1822. struct qman_fq *fq,
  1823. const struct qm_dqrr_entry *dq)
  1824. {
  1825. struct dpaa_percpu_priv *percpu_priv;
  1826. struct net_device *net_dev;
  1827. struct dpaa_priv *priv;
  1828. net_dev = ((struct dpaa_fq *)fq)->net_dev;
  1829. priv = netdev_priv(net_dev);
  1830. percpu_priv = this_cpu_ptr(priv->percpu_priv);
  1831. if (dpaa_eth_napi_schedule(percpu_priv, portal))
  1832. return qman_cb_dqrr_stop;
  1833. dpaa_tx_error(net_dev, priv, percpu_priv, &dq->fd, fq->fqid);
  1834. return qman_cb_dqrr_consume;
  1835. }
  1836. static enum qman_cb_dqrr_result conf_dflt_dqrr(struct qman_portal *portal,
  1837. struct qman_fq *fq,
  1838. const struct qm_dqrr_entry *dq)
  1839. {
  1840. struct dpaa_percpu_priv *percpu_priv;
  1841. struct net_device *net_dev;
  1842. struct dpaa_priv *priv;
  1843. net_dev = ((struct dpaa_fq *)fq)->net_dev;
  1844. priv = netdev_priv(net_dev);
  1845. /* Trace the fd */
  1846. trace_dpaa_tx_conf_fd(net_dev, fq, &dq->fd);
  1847. percpu_priv = this_cpu_ptr(priv->percpu_priv);
  1848. if (dpaa_eth_napi_schedule(percpu_priv, portal))
  1849. return qman_cb_dqrr_stop;
  1850. dpaa_tx_conf(net_dev, priv, percpu_priv, &dq->fd, fq->fqid);
  1851. return qman_cb_dqrr_consume;
  1852. }
  1853. static void egress_ern(struct qman_portal *portal,
  1854. struct qman_fq *fq,
  1855. const union qm_mr_entry *msg)
  1856. {
  1857. const struct qm_fd *fd = &msg->ern.fd;
  1858. struct dpaa_percpu_priv *percpu_priv;
  1859. const struct dpaa_priv *priv;
  1860. struct net_device *net_dev;
  1861. struct sk_buff *skb;
  1862. net_dev = ((struct dpaa_fq *)fq)->net_dev;
  1863. priv = netdev_priv(net_dev);
  1864. percpu_priv = this_cpu_ptr(priv->percpu_priv);
  1865. percpu_priv->stats.tx_dropped++;
  1866. percpu_priv->stats.tx_fifo_errors++;
  1867. count_ern(percpu_priv, msg);
  1868. skb = dpaa_cleanup_tx_fd(priv, fd);
  1869. dev_kfree_skb_any(skb);
  1870. }
  1871. static const struct dpaa_fq_cbs dpaa_fq_cbs = {
  1872. .rx_defq = { .cb = { .dqrr = rx_default_dqrr } },
  1873. .tx_defq = { .cb = { .dqrr = conf_dflt_dqrr } },
  1874. .rx_errq = { .cb = { .dqrr = rx_error_dqrr } },
  1875. .tx_errq = { .cb = { .dqrr = conf_error_dqrr } },
  1876. .egress_ern = { .cb = { .ern = egress_ern } }
  1877. };
  1878. static void dpaa_eth_napi_enable(struct dpaa_priv *priv)
  1879. {
  1880. struct dpaa_percpu_priv *percpu_priv;
  1881. int i;
  1882. for_each_possible_cpu(i) {
  1883. percpu_priv = per_cpu_ptr(priv->percpu_priv, i);
  1884. percpu_priv->np.down = 0;
  1885. napi_enable(&percpu_priv->np.napi);
  1886. }
  1887. }
  1888. static void dpaa_eth_napi_disable(struct dpaa_priv *priv)
  1889. {
  1890. struct dpaa_percpu_priv *percpu_priv;
  1891. int i;
  1892. for_each_possible_cpu(i) {
  1893. percpu_priv = per_cpu_ptr(priv->percpu_priv, i);
  1894. percpu_priv->np.down = 1;
  1895. napi_disable(&percpu_priv->np.napi);
  1896. }
  1897. }
  1898. static int dpaa_open(struct net_device *net_dev)
  1899. {
  1900. struct mac_device *mac_dev;
  1901. struct dpaa_priv *priv;
  1902. int err, i;
  1903. priv = netdev_priv(net_dev);
  1904. mac_dev = priv->mac_dev;
  1905. dpaa_eth_napi_enable(priv);
  1906. net_dev->phydev = mac_dev->init_phy(net_dev, priv->mac_dev);
  1907. if (!net_dev->phydev) {
  1908. netif_err(priv, ifup, net_dev, "init_phy() failed\n");
  1909. err = -ENODEV;
  1910. goto phy_init_failed;
  1911. }
  1912. for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++) {
  1913. err = fman_port_enable(mac_dev->port[i]);
  1914. if (err)
  1915. goto mac_start_failed;
  1916. }
  1917. err = priv->mac_dev->start(mac_dev);
  1918. if (err < 0) {
  1919. netif_err(priv, ifup, net_dev, "mac_dev->start() = %d\n", err);
  1920. goto mac_start_failed;
  1921. }
  1922. netif_tx_start_all_queues(net_dev);
  1923. return 0;
  1924. mac_start_failed:
  1925. for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++)
  1926. fman_port_disable(mac_dev->port[i]);
  1927. phy_init_failed:
  1928. dpaa_eth_napi_disable(priv);
  1929. return err;
  1930. }
  1931. static int dpaa_eth_stop(struct net_device *net_dev)
  1932. {
  1933. struct dpaa_priv *priv;
  1934. int err;
  1935. err = dpaa_stop(net_dev);
  1936. priv = netdev_priv(net_dev);
  1937. dpaa_eth_napi_disable(priv);
  1938. return err;
  1939. }
  1940. static int dpaa_ioctl(struct net_device *net_dev, struct ifreq *rq, int cmd)
  1941. {
  1942. if (!net_dev->phydev)
  1943. return -EINVAL;
  1944. return phy_mii_ioctl(net_dev->phydev, rq, cmd);
  1945. }
  1946. static const struct net_device_ops dpaa_ops = {
  1947. .ndo_open = dpaa_open,
  1948. .ndo_start_xmit = dpaa_start_xmit,
  1949. .ndo_stop = dpaa_eth_stop,
  1950. .ndo_tx_timeout = dpaa_tx_timeout,
  1951. .ndo_get_stats64 = dpaa_get_stats64,
  1952. .ndo_set_mac_address = dpaa_set_mac_address,
  1953. .ndo_validate_addr = eth_validate_addr,
  1954. .ndo_set_rx_mode = dpaa_set_rx_mode,
  1955. .ndo_do_ioctl = dpaa_ioctl,
  1956. };
  1957. static int dpaa_napi_add(struct net_device *net_dev)
  1958. {
  1959. struct dpaa_priv *priv = netdev_priv(net_dev);
  1960. struct dpaa_percpu_priv *percpu_priv;
  1961. int cpu;
  1962. for_each_possible_cpu(cpu) {
  1963. percpu_priv = per_cpu_ptr(priv->percpu_priv, cpu);
  1964. netif_napi_add(net_dev, &percpu_priv->np.napi,
  1965. dpaa_eth_poll, NAPI_POLL_WEIGHT);
  1966. }
  1967. return 0;
  1968. }
  1969. static void dpaa_napi_del(struct net_device *net_dev)
  1970. {
  1971. struct dpaa_priv *priv = netdev_priv(net_dev);
  1972. struct dpaa_percpu_priv *percpu_priv;
  1973. int cpu;
  1974. for_each_possible_cpu(cpu) {
  1975. percpu_priv = per_cpu_ptr(priv->percpu_priv, cpu);
  1976. netif_napi_del(&percpu_priv->np.napi);
  1977. }
  1978. }
  1979. static inline void dpaa_bp_free_pf(const struct dpaa_bp *bp,
  1980. struct bm_buffer *bmb)
  1981. {
  1982. dma_addr_t addr = bm_buf_addr(bmb);
  1983. dma_unmap_single(bp->dev, addr, bp->size, DMA_FROM_DEVICE);
  1984. skb_free_frag(phys_to_virt(addr));
  1985. }
  1986. /* Alloc the dpaa_bp struct and configure default values */
  1987. static struct dpaa_bp *dpaa_bp_alloc(struct device *dev)
  1988. {
  1989. struct dpaa_bp *dpaa_bp;
  1990. dpaa_bp = devm_kzalloc(dev, sizeof(*dpaa_bp), GFP_KERNEL);
  1991. if (!dpaa_bp)
  1992. return ERR_PTR(-ENOMEM);
  1993. dpaa_bp->bpid = FSL_DPAA_BPID_INV;
  1994. dpaa_bp->percpu_count = devm_alloc_percpu(dev, *dpaa_bp->percpu_count);
  1995. dpaa_bp->config_count = FSL_DPAA_ETH_MAX_BUF_COUNT;
  1996. dpaa_bp->seed_cb = dpaa_bp_seed;
  1997. dpaa_bp->free_buf_cb = dpaa_bp_free_pf;
  1998. return dpaa_bp;
  1999. }
  2000. /* Place all ingress FQs (Rx Default, Rx Error) in a dedicated CGR.
  2001. * We won't be sending congestion notifications to FMan; for now, we just use
  2002. * this CGR to generate enqueue rejections to FMan in order to drop the frames
  2003. * before they reach our ingress queues and eat up memory.
  2004. */
  2005. static int dpaa_ingress_cgr_init(struct dpaa_priv *priv)
  2006. {
  2007. struct qm_mcc_initcgr initcgr;
  2008. u32 cs_th;
  2009. int err;
  2010. err = qman_alloc_cgrid(&priv->ingress_cgr.cgrid);
  2011. if (err < 0) {
  2012. if (netif_msg_drv(priv))
  2013. pr_err("Error %d allocating CGR ID\n", err);
  2014. goto out_error;
  2015. }
  2016. /* Enable CS TD, but disable Congestion State Change Notifications. */
  2017. memset(&initcgr, 0, sizeof(initcgr));
  2018. initcgr.we_mask = cpu_to_be16(QM_CGR_WE_CS_THRES);
  2019. initcgr.cgr.cscn_en = QM_CGR_EN;
  2020. cs_th = DPAA_INGRESS_CS_THRESHOLD;
  2021. qm_cgr_cs_thres_set64(&initcgr.cgr.cs_thres, cs_th, 1);
  2022. initcgr.we_mask |= cpu_to_be16(QM_CGR_WE_CSTD_EN);
  2023. initcgr.cgr.cstd_en = QM_CGR_EN;
  2024. /* This CGR will be associated with the SWP affined to the current CPU.
  2025. * However, we'll place all our ingress FQs in it.
  2026. */
  2027. err = qman_create_cgr(&priv->ingress_cgr, QMAN_CGR_FLAG_USE_INIT,
  2028. &initcgr);
  2029. if (err < 0) {
  2030. if (netif_msg_drv(priv))
  2031. pr_err("Error %d creating ingress CGR with ID %d\n",
  2032. err, priv->ingress_cgr.cgrid);
  2033. qman_release_cgrid(priv->ingress_cgr.cgrid);
  2034. goto out_error;
  2035. }
  2036. if (netif_msg_drv(priv))
  2037. pr_debug("Created ingress CGR %d for netdev with hwaddr %pM\n",
  2038. priv->ingress_cgr.cgrid, priv->mac_dev->addr);
  2039. priv->use_ingress_cgr = true;
  2040. out_error:
  2041. return err;
  2042. }
  2043. static const struct of_device_id dpaa_match[];
  2044. static inline u16 dpaa_get_headroom(struct dpaa_buffer_layout *bl)
  2045. {
  2046. u16 headroom;
  2047. /* The frame headroom must accommodate:
  2048. * - the driver private data area
  2049. * - parse results, hash results, timestamp if selected
  2050. * If either hash results or time stamp are selected, both will
  2051. * be copied to/from the frame headroom, as TS is located between PR and
  2052. * HR in the IC and IC copy size has a granularity of 16bytes
  2053. * (see description of FMBM_RICP and FMBM_TICP registers in DPAARM)
  2054. *
  2055. * Also make sure the headroom is a multiple of data_align bytes
  2056. */
  2057. headroom = (u16)(bl->priv_data_size + DPAA_PARSE_RESULTS_SIZE +
  2058. DPAA_TIME_STAMP_SIZE + DPAA_HASH_RESULTS_SIZE);
  2059. return DPAA_FD_DATA_ALIGNMENT ? ALIGN(headroom,
  2060. DPAA_FD_DATA_ALIGNMENT) :
  2061. headroom;
  2062. }
  2063. static int dpaa_eth_probe(struct platform_device *pdev)
  2064. {
  2065. struct dpaa_bp *dpaa_bps[DPAA_BPS_NUM] = {NULL};
  2066. struct dpaa_percpu_priv *percpu_priv;
  2067. struct net_device *net_dev = NULL;
  2068. struct dpaa_fq *dpaa_fq, *tmp;
  2069. struct dpaa_priv *priv = NULL;
  2070. struct fm_port_fqs port_fqs;
  2071. struct mac_device *mac_dev;
  2072. int err = 0, i, channel;
  2073. struct device *dev;
  2074. dev = &pdev->dev;
  2075. /* Allocate this early, so we can store relevant information in
  2076. * the private area
  2077. */
  2078. net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA_ETH_TXQ_NUM);
  2079. if (!net_dev) {
  2080. dev_err(dev, "alloc_etherdev_mq() failed\n");
  2081. goto alloc_etherdev_mq_failed;
  2082. }
  2083. /* Do this here, so we can be verbose early */
  2084. SET_NETDEV_DEV(net_dev, dev);
  2085. dev_set_drvdata(dev, net_dev);
  2086. priv = netdev_priv(net_dev);
  2087. priv->net_dev = net_dev;
  2088. priv->msg_enable = netif_msg_init(debug, DPAA_MSG_DEFAULT);
  2089. mac_dev = dpaa_mac_dev_get(pdev);
  2090. if (IS_ERR(mac_dev)) {
  2091. dev_err(dev, "dpaa_mac_dev_get() failed\n");
  2092. err = PTR_ERR(mac_dev);
  2093. goto mac_probe_failed;
  2094. }
  2095. /* If fsl_fm_max_frm is set to a higher value than the all-common 1500,
  2096. * we choose conservatively and let the user explicitly set a higher
  2097. * MTU via ifconfig. Otherwise, the user may end up with different MTUs
  2098. * in the same LAN.
  2099. * If on the other hand fsl_fm_max_frm has been chosen below 1500,
  2100. * start with the maximum allowed.
  2101. */
  2102. net_dev->mtu = min(dpaa_get_max_mtu(), ETH_DATA_LEN);
  2103. netdev_dbg(net_dev, "Setting initial MTU on net device: %d\n",
  2104. net_dev->mtu);
  2105. priv->buf_layout[RX].priv_data_size = DPAA_RX_PRIV_DATA_SIZE; /* Rx */
  2106. priv->buf_layout[TX].priv_data_size = DPAA_TX_PRIV_DATA_SIZE; /* Tx */
  2107. /* device used for DMA mapping */
  2108. arch_setup_dma_ops(dev, 0, 0, NULL, false);
  2109. err = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(40));
  2110. if (err) {
  2111. dev_err(dev, "dma_coerce_mask_and_coherent() failed\n");
  2112. goto dev_mask_failed;
  2113. }
  2114. /* bp init */
  2115. for (i = 0; i < DPAA_BPS_NUM; i++) {
  2116. int err;
  2117. dpaa_bps[i] = dpaa_bp_alloc(dev);
  2118. if (IS_ERR(dpaa_bps[i]))
  2119. return PTR_ERR(dpaa_bps[i]);
  2120. /* the raw size of the buffers used for reception */
  2121. dpaa_bps[i]->raw_size = bpool_buffer_raw_size(i, DPAA_BPS_NUM);
  2122. /* avoid runtime computations by keeping the usable size here */
  2123. dpaa_bps[i]->size = dpaa_bp_size(dpaa_bps[i]->raw_size);
  2124. dpaa_bps[i]->dev = dev;
  2125. err = dpaa_bp_alloc_pool(dpaa_bps[i]);
  2126. if (err < 0) {
  2127. dpaa_bps_free(priv);
  2128. priv->dpaa_bps[i] = NULL;
  2129. goto bp_create_failed;
  2130. }
  2131. priv->dpaa_bps[i] = dpaa_bps[i];
  2132. }
  2133. INIT_LIST_HEAD(&priv->dpaa_fq_list);
  2134. memset(&port_fqs, 0, sizeof(port_fqs));
  2135. err = dpaa_alloc_all_fqs(dev, &priv->dpaa_fq_list, &port_fqs);
  2136. if (err < 0) {
  2137. dev_err(dev, "dpaa_alloc_all_fqs() failed\n");
  2138. goto fq_probe_failed;
  2139. }
  2140. priv->mac_dev = mac_dev;
  2141. channel = dpaa_get_channel();
  2142. if (channel < 0) {
  2143. dev_err(dev, "dpaa_get_channel() failed\n");
  2144. err = channel;
  2145. goto get_channel_failed;
  2146. }
  2147. priv->channel = (u16)channel;
  2148. /* Start a thread that will walk the CPUs with affine portals
  2149. * and add this pool channel to each's dequeue mask.
  2150. */
  2151. dpaa_eth_add_channel(priv->channel);
  2152. dpaa_fq_setup(priv, &dpaa_fq_cbs, priv->mac_dev->port[TX]);
  2153. /* Create a congestion group for this netdev, with
  2154. * dynamically-allocated CGR ID.
  2155. * Must be executed after probing the MAC, but before
  2156. * assigning the egress FQs to the CGRs.
  2157. */
  2158. err = dpaa_eth_cgr_init(priv);
  2159. if (err < 0) {
  2160. dev_err(dev, "Error initializing CGR\n");
  2161. goto tx_cgr_init_failed;
  2162. }
  2163. err = dpaa_ingress_cgr_init(priv);
  2164. if (err < 0) {
  2165. dev_err(dev, "Error initializing ingress CGR\n");
  2166. goto rx_cgr_init_failed;
  2167. }
  2168. /* Add the FQs to the interface, and make them active */
  2169. list_for_each_entry_safe(dpaa_fq, tmp, &priv->dpaa_fq_list, list) {
  2170. err = dpaa_fq_init(dpaa_fq, false);
  2171. if (err < 0)
  2172. goto fq_alloc_failed;
  2173. }
  2174. priv->tx_headroom = dpaa_get_headroom(&priv->buf_layout[TX]);
  2175. priv->rx_headroom = dpaa_get_headroom(&priv->buf_layout[RX]);
  2176. /* All real interfaces need their ports initialized */
  2177. dpaa_eth_init_ports(mac_dev, dpaa_bps, DPAA_BPS_NUM, &port_fqs,
  2178. &priv->buf_layout[0], dev);
  2179. priv->percpu_priv = devm_alloc_percpu(dev, *priv->percpu_priv);
  2180. if (!priv->percpu_priv) {
  2181. dev_err(dev, "devm_alloc_percpu() failed\n");
  2182. err = -ENOMEM;
  2183. goto alloc_percpu_failed;
  2184. }
  2185. for_each_possible_cpu(i) {
  2186. percpu_priv = per_cpu_ptr(priv->percpu_priv, i);
  2187. memset(percpu_priv, 0, sizeof(*percpu_priv));
  2188. }
  2189. /* Initialize NAPI */
  2190. err = dpaa_napi_add(net_dev);
  2191. if (err < 0)
  2192. goto napi_add_failed;
  2193. err = dpaa_netdev_init(net_dev, &dpaa_ops, tx_timeout);
  2194. if (err < 0)
  2195. goto netdev_init_failed;
  2196. dpaa_eth_sysfs_init(&net_dev->dev);
  2197. netif_info(priv, probe, net_dev, "Probed interface %s\n",
  2198. net_dev->name);
  2199. return 0;
  2200. netdev_init_failed:
  2201. napi_add_failed:
  2202. dpaa_napi_del(net_dev);
  2203. alloc_percpu_failed:
  2204. dpaa_fq_free(dev, &priv->dpaa_fq_list);
  2205. fq_alloc_failed:
  2206. qman_delete_cgr_safe(&priv->ingress_cgr);
  2207. qman_release_cgrid(priv->ingress_cgr.cgrid);
  2208. rx_cgr_init_failed:
  2209. qman_delete_cgr_safe(&priv->cgr_data.cgr);
  2210. qman_release_cgrid(priv->cgr_data.cgr.cgrid);
  2211. tx_cgr_init_failed:
  2212. get_channel_failed:
  2213. dpaa_bps_free(priv);
  2214. bp_create_failed:
  2215. fq_probe_failed:
  2216. dev_mask_failed:
  2217. mac_probe_failed:
  2218. dev_set_drvdata(dev, NULL);
  2219. free_netdev(net_dev);
  2220. alloc_etherdev_mq_failed:
  2221. for (i = 0; i < DPAA_BPS_NUM && dpaa_bps[i]; i++) {
  2222. if (atomic_read(&dpaa_bps[i]->refs) == 0)
  2223. devm_kfree(dev, dpaa_bps[i]);
  2224. }
  2225. return err;
  2226. }
  2227. static int dpaa_remove(struct platform_device *pdev)
  2228. {
  2229. struct net_device *net_dev;
  2230. struct dpaa_priv *priv;
  2231. struct device *dev;
  2232. int err;
  2233. dev = &pdev->dev;
  2234. net_dev = dev_get_drvdata(dev);
  2235. priv = netdev_priv(net_dev);
  2236. dpaa_eth_sysfs_remove(dev);
  2237. dev_set_drvdata(dev, NULL);
  2238. unregister_netdev(net_dev);
  2239. err = dpaa_fq_free(dev, &priv->dpaa_fq_list);
  2240. qman_delete_cgr_safe(&priv->ingress_cgr);
  2241. qman_release_cgrid(priv->ingress_cgr.cgrid);
  2242. qman_delete_cgr_safe(&priv->cgr_data.cgr);
  2243. qman_release_cgrid(priv->cgr_data.cgr.cgrid);
  2244. dpaa_napi_del(net_dev);
  2245. dpaa_bps_free(priv);
  2246. free_netdev(net_dev);
  2247. return err;
  2248. }
  2249. static struct platform_device_id dpaa_devtype[] = {
  2250. {
  2251. .name = "dpaa-ethernet",
  2252. .driver_data = 0,
  2253. }, {
  2254. }
  2255. };
  2256. MODULE_DEVICE_TABLE(platform, dpaa_devtype);
  2257. static struct platform_driver dpaa_driver = {
  2258. .driver = {
  2259. .name = KBUILD_MODNAME,
  2260. },
  2261. .id_table = dpaa_devtype,
  2262. .probe = dpaa_eth_probe,
  2263. .remove = dpaa_remove
  2264. };
  2265. static int __init dpaa_load(void)
  2266. {
  2267. int err;
  2268. pr_debug("FSL DPAA Ethernet driver\n");
  2269. /* initialize dpaa_eth mirror values */
  2270. dpaa_rx_extra_headroom = fman_get_rx_extra_headroom();
  2271. dpaa_max_frm = fman_get_max_frm();
  2272. err = platform_driver_register(&dpaa_driver);
  2273. if (err < 0)
  2274. pr_err("Error, platform_driver_register() = %d\n", err);
  2275. return err;
  2276. }
  2277. module_init(dpaa_load);
  2278. static void __exit dpaa_unload(void)
  2279. {
  2280. platform_driver_unregister(&dpaa_driver);
  2281. /* Only one channel is used and needs to be released after all
  2282. * interfaces are removed
  2283. */
  2284. dpaa_release_channel();
  2285. }
  2286. module_exit(dpaa_unload);
  2287. MODULE_LICENSE("Dual BSD/GPL");
  2288. MODULE_DESCRIPTION("FSL DPAA Ethernet driver");