dpaa_eth.c 77 KB

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