dpaa_eth.c 76 KB

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