dpaa_eth.c 80 KB

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