dpaa_eth.c 77 KB

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