dpaa_eth.c 73 KB

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