dpaa_eth.c 79 KB

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