gianfar.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* drivers/net/ethernet/freescale/gianfar.c
  3. *
  4. * Gianfar Ethernet Driver
  5. * This driver is designed for the non-CPM ethernet controllers
  6. * on the 85xx and 83xx family of integrated processors
  7. * Based on 8260_io/fcc_enet.c
  8. *
  9. * Author: Andy Fleming
  10. * Maintainer: Kumar Gala
  11. * Modifier: Sandeep Gopalpet <[email protected]>
  12. *
  13. * Copyright 2002-2009, 2011-2013 Freescale Semiconductor, Inc.
  14. * Copyright 2007 MontaVista Software, Inc.
  15. *
  16. * Gianfar: AKA Lambda Draconis, "Dragon"
  17. * RA 11 31 24.2
  18. * Dec +69 19 52
  19. * V 3.84
  20. * B-V +1.62
  21. *
  22. * Theory of operation
  23. *
  24. * The driver is initialized through of_device. Configuration information
  25. * is therefore conveyed through an OF-style device tree.
  26. *
  27. * The Gianfar Ethernet Controller uses a ring of buffer
  28. * descriptors. The beginning is indicated by a register
  29. * pointing to the physical address of the start of the ring.
  30. * The end is determined by a "wrap" bit being set in the
  31. * last descriptor of the ring.
  32. *
  33. * When a packet is received, the RXF bit in the
  34. * IEVENT register is set, triggering an interrupt when the
  35. * corresponding bit in the IMASK register is also set (if
  36. * interrupt coalescing is active, then the interrupt may not
  37. * happen immediately, but will wait until either a set number
  38. * of frames or amount of time have passed). In NAPI, the
  39. * interrupt handler will signal there is work to be done, and
  40. * exit. This method will start at the last known empty
  41. * descriptor, and process every subsequent descriptor until there
  42. * are none left with data (NAPI will stop after a set number of
  43. * packets to give time to other tasks, but will eventually
  44. * process all the packets). The data arrives inside a
  45. * pre-allocated skb, and so after the skb is passed up to the
  46. * stack, a new skb must be allocated, and the address field in
  47. * the buffer descriptor must be updated to indicate this new
  48. * skb.
  49. *
  50. * When the kernel requests that a packet be transmitted, the
  51. * driver starts where it left off last time, and points the
  52. * descriptor at the buffer which was passed in. The driver
  53. * then informs the DMA engine that there are packets ready to
  54. * be transmitted. Once the controller is finished transmitting
  55. * the packet, an interrupt may be triggered (under the same
  56. * conditions as for reception, but depending on the TXF bit).
  57. * The driver then cleans up the buffer.
  58. */
  59. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  60. #include <linux/kernel.h>
  61. #include <linux/string.h>
  62. #include <linux/errno.h>
  63. #include <linux/unistd.h>
  64. #include <linux/slab.h>
  65. #include <linux/interrupt.h>
  66. #include <linux/delay.h>
  67. #include <linux/netdevice.h>
  68. #include <linux/etherdevice.h>
  69. #include <linux/skbuff.h>
  70. #include <linux/if_vlan.h>
  71. #include <linux/spinlock.h>
  72. #include <linux/mm.h>
  73. #include <linux/of_address.h>
  74. #include <linux/of_irq.h>
  75. #include <linux/of_mdio.h>
  76. #include <linux/of_platform.h>
  77. #include <linux/ip.h>
  78. #include <linux/tcp.h>
  79. #include <linux/udp.h>
  80. #include <linux/in.h>
  81. #include <linux/net_tstamp.h>
  82. #include <asm/io.h>
  83. #ifdef CONFIG_PPC
  84. #include <asm/reg.h>
  85. #include <asm/mpc85xx.h>
  86. #endif
  87. #include <asm/irq.h>
  88. #include <linux/uaccess.h>
  89. #include <linux/module.h>
  90. #include <linux/dma-mapping.h>
  91. #include <linux/crc32.h>
  92. #include <linux/mii.h>
  93. #include <linux/phy.h>
  94. #include <linux/phy_fixed.h>
  95. #include <linux/of.h>
  96. #include <linux/of_net.h>
  97. #include "gianfar.h"
  98. #define TX_TIMEOUT (5*HZ)
  99. MODULE_AUTHOR("Freescale Semiconductor, Inc");
  100. MODULE_DESCRIPTION("Gianfar Ethernet Driver");
  101. MODULE_LICENSE("GPL");
  102. static void gfar_init_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
  103. dma_addr_t buf)
  104. {
  105. u32 lstatus;
  106. bdp->bufPtr = cpu_to_be32(buf);
  107. lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT);
  108. if (bdp == rx_queue->rx_bd_base + rx_queue->rx_ring_size - 1)
  109. lstatus |= BD_LFLAG(RXBD_WRAP);
  110. gfar_wmb();
  111. bdp->lstatus = cpu_to_be32(lstatus);
  112. }
  113. static void gfar_init_tx_rx_base(struct gfar_private *priv)
  114. {
  115. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  116. u32 __iomem *baddr;
  117. int i;
  118. baddr = &regs->tbase0;
  119. for (i = 0; i < priv->num_tx_queues; i++) {
  120. gfar_write(baddr, priv->tx_queue[i]->tx_bd_dma_base);
  121. baddr += 2;
  122. }
  123. baddr = &regs->rbase0;
  124. for (i = 0; i < priv->num_rx_queues; i++) {
  125. gfar_write(baddr, priv->rx_queue[i]->rx_bd_dma_base);
  126. baddr += 2;
  127. }
  128. }
  129. static void gfar_init_rqprm(struct gfar_private *priv)
  130. {
  131. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  132. u32 __iomem *baddr;
  133. int i;
  134. baddr = &regs->rqprm0;
  135. for (i = 0; i < priv->num_rx_queues; i++) {
  136. gfar_write(baddr, priv->rx_queue[i]->rx_ring_size |
  137. (DEFAULT_RX_LFC_THR << FBTHR_SHIFT));
  138. baddr++;
  139. }
  140. }
  141. static void gfar_rx_offload_en(struct gfar_private *priv)
  142. {
  143. /* set this when rx hw offload (TOE) functions are being used */
  144. priv->uses_rxfcb = 0;
  145. if (priv->ndev->features & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX))
  146. priv->uses_rxfcb = 1;
  147. if (priv->hwts_rx_en || priv->rx_filer_enable)
  148. priv->uses_rxfcb = 1;
  149. }
  150. static void gfar_mac_rx_config(struct gfar_private *priv)
  151. {
  152. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  153. u32 rctrl = 0;
  154. if (priv->rx_filer_enable) {
  155. rctrl |= RCTRL_FILREN | RCTRL_PRSDEP_INIT;
  156. /* Program the RIR0 reg with the required distribution */
  157. gfar_write(&regs->rir0, DEFAULT_2RXQ_RIR0);
  158. }
  159. /* Restore PROMISC mode */
  160. if (priv->ndev->flags & IFF_PROMISC)
  161. rctrl |= RCTRL_PROM;
  162. if (priv->ndev->features & NETIF_F_RXCSUM)
  163. rctrl |= RCTRL_CHECKSUMMING;
  164. if (priv->extended_hash)
  165. rctrl |= RCTRL_EXTHASH | RCTRL_EMEN;
  166. if (priv->padding) {
  167. rctrl &= ~RCTRL_PAL_MASK;
  168. rctrl |= RCTRL_PADDING(priv->padding);
  169. }
  170. /* Enable HW time stamping if requested from user space */
  171. if (priv->hwts_rx_en)
  172. rctrl |= RCTRL_PRSDEP_INIT | RCTRL_TS_ENABLE;
  173. if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_RX)
  174. rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT;
  175. /* Clear the LFC bit */
  176. gfar_write(&regs->rctrl, rctrl);
  177. /* Init flow control threshold values */
  178. gfar_init_rqprm(priv);
  179. gfar_write(&regs->ptv, DEFAULT_LFC_PTVVAL);
  180. rctrl |= RCTRL_LFC;
  181. /* Init rctrl based on our settings */
  182. gfar_write(&regs->rctrl, rctrl);
  183. }
  184. static void gfar_mac_tx_config(struct gfar_private *priv)
  185. {
  186. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  187. u32 tctrl = 0;
  188. if (priv->ndev->features & NETIF_F_IP_CSUM)
  189. tctrl |= TCTRL_INIT_CSUM;
  190. if (priv->prio_sched_en)
  191. tctrl |= TCTRL_TXSCHED_PRIO;
  192. else {
  193. tctrl |= TCTRL_TXSCHED_WRRS;
  194. gfar_write(&regs->tr03wt, DEFAULT_WRRS_WEIGHT);
  195. gfar_write(&regs->tr47wt, DEFAULT_WRRS_WEIGHT);
  196. }
  197. if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_TX)
  198. tctrl |= TCTRL_VLINS;
  199. gfar_write(&regs->tctrl, tctrl);
  200. }
  201. static void gfar_configure_coalescing(struct gfar_private *priv,
  202. unsigned long tx_mask, unsigned long rx_mask)
  203. {
  204. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  205. u32 __iomem *baddr;
  206. if (priv->mode == MQ_MG_MODE) {
  207. int i = 0;
  208. baddr = &regs->txic0;
  209. for_each_set_bit(i, &tx_mask, priv->num_tx_queues) {
  210. gfar_write(baddr + i, 0);
  211. if (likely(priv->tx_queue[i]->txcoalescing))
  212. gfar_write(baddr + i, priv->tx_queue[i]->txic);
  213. }
  214. baddr = &regs->rxic0;
  215. for_each_set_bit(i, &rx_mask, priv->num_rx_queues) {
  216. gfar_write(baddr + i, 0);
  217. if (likely(priv->rx_queue[i]->rxcoalescing))
  218. gfar_write(baddr + i, priv->rx_queue[i]->rxic);
  219. }
  220. } else {
  221. /* Backward compatible case -- even if we enable
  222. * multiple queues, there's only single reg to program
  223. */
  224. gfar_write(&regs->txic, 0);
  225. if (likely(priv->tx_queue[0]->txcoalescing))
  226. gfar_write(&regs->txic, priv->tx_queue[0]->txic);
  227. gfar_write(&regs->rxic, 0);
  228. if (unlikely(priv->rx_queue[0]->rxcoalescing))
  229. gfar_write(&regs->rxic, priv->rx_queue[0]->rxic);
  230. }
  231. }
  232. static void gfar_configure_coalescing_all(struct gfar_private *priv)
  233. {
  234. gfar_configure_coalescing(priv, 0xFF, 0xFF);
  235. }
  236. static void gfar_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  237. {
  238. struct gfar_private *priv = netdev_priv(dev);
  239. int i;
  240. for (i = 0; i < priv->num_rx_queues; i++) {
  241. stats->rx_packets += priv->rx_queue[i]->stats.rx_packets;
  242. stats->rx_bytes += priv->rx_queue[i]->stats.rx_bytes;
  243. stats->rx_dropped += priv->rx_queue[i]->stats.rx_dropped;
  244. }
  245. for (i = 0; i < priv->num_tx_queues; i++) {
  246. stats->tx_bytes += priv->tx_queue[i]->stats.tx_bytes;
  247. stats->tx_packets += priv->tx_queue[i]->stats.tx_packets;
  248. }
  249. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
  250. struct rmon_mib __iomem *rmon = &priv->gfargrp[0].regs->rmon;
  251. unsigned long flags;
  252. u32 rdrp, car, car_before;
  253. u64 rdrp_offset;
  254. spin_lock_irqsave(&priv->rmon_overflow.lock, flags);
  255. car = gfar_read(&rmon->car1) & CAR1_C1RDR;
  256. do {
  257. car_before = car;
  258. rdrp = gfar_read(&rmon->rdrp);
  259. car = gfar_read(&rmon->car1) & CAR1_C1RDR;
  260. } while (car != car_before);
  261. if (car) {
  262. priv->rmon_overflow.rdrp++;
  263. gfar_write(&rmon->car1, car);
  264. }
  265. rdrp_offset = priv->rmon_overflow.rdrp;
  266. spin_unlock_irqrestore(&priv->rmon_overflow.lock, flags);
  267. stats->rx_missed_errors = rdrp + (rdrp_offset << 16);
  268. }
  269. }
  270. /* Set the appropriate hash bit for the given addr */
  271. /* The algorithm works like so:
  272. * 1) Take the Destination Address (ie the multicast address), and
  273. * do a CRC on it (little endian), and reverse the bits of the
  274. * result.
  275. * 2) Use the 8 most significant bits as a hash into a 256-entry
  276. * table. The table is controlled through 8 32-bit registers:
  277. * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
  278. * gaddr7. This means that the 3 most significant bits in the
  279. * hash index which gaddr register to use, and the 5 other bits
  280. * indicate which bit (assuming an IBM numbering scheme, which
  281. * for PowerPC (tm) is usually the case) in the register holds
  282. * the entry.
  283. */
  284. static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
  285. {
  286. u32 tempval;
  287. struct gfar_private *priv = netdev_priv(dev);
  288. u32 result = ether_crc(ETH_ALEN, addr);
  289. int width = priv->hash_width;
  290. u8 whichbit = (result >> (32 - width)) & 0x1f;
  291. u8 whichreg = result >> (32 - width + 5);
  292. u32 value = (1 << (31-whichbit));
  293. tempval = gfar_read(priv->hash_regs[whichreg]);
  294. tempval |= value;
  295. gfar_write(priv->hash_regs[whichreg], tempval);
  296. }
  297. /* There are multiple MAC Address register pairs on some controllers
  298. * This function sets the numth pair to a given address
  299. */
  300. static void gfar_set_mac_for_addr(struct net_device *dev, int num,
  301. const u8 *addr)
  302. {
  303. struct gfar_private *priv = netdev_priv(dev);
  304. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  305. u32 tempval;
  306. u32 __iomem *macptr = &regs->macstnaddr1;
  307. macptr += num*2;
  308. /* For a station address of 0x12345678ABCD in transmission
  309. * order (BE), MACnADDR1 is set to 0xCDAB7856 and
  310. * MACnADDR2 is set to 0x34120000.
  311. */
  312. tempval = (addr[5] << 24) | (addr[4] << 16) |
  313. (addr[3] << 8) | addr[2];
  314. gfar_write(macptr, tempval);
  315. tempval = (addr[1] << 24) | (addr[0] << 16);
  316. gfar_write(macptr+1, tempval);
  317. }
  318. static int gfar_set_mac_addr(struct net_device *dev, void *p)
  319. {
  320. int ret;
  321. ret = eth_mac_addr(dev, p);
  322. if (ret)
  323. return ret;
  324. gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
  325. return 0;
  326. }
  327. static void gfar_ints_disable(struct gfar_private *priv)
  328. {
  329. int i;
  330. for (i = 0; i < priv->num_grps; i++) {
  331. struct gfar __iomem *regs = priv->gfargrp[i].regs;
  332. /* Clear IEVENT */
  333. gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
  334. /* Initialize IMASK */
  335. gfar_write(&regs->imask, IMASK_INIT_CLEAR);
  336. }
  337. }
  338. static void gfar_ints_enable(struct gfar_private *priv)
  339. {
  340. int i;
  341. for (i = 0; i < priv->num_grps; i++) {
  342. struct gfar __iomem *regs = priv->gfargrp[i].regs;
  343. /* Unmask the interrupts we look for */
  344. gfar_write(&regs->imask,
  345. IMASK_DEFAULT | priv->rmon_overflow.imask);
  346. }
  347. }
  348. static int gfar_alloc_tx_queues(struct gfar_private *priv)
  349. {
  350. int i;
  351. for (i = 0; i < priv->num_tx_queues; i++) {
  352. priv->tx_queue[i] = kzalloc(sizeof(struct gfar_priv_tx_q),
  353. GFP_KERNEL);
  354. if (!priv->tx_queue[i])
  355. return -ENOMEM;
  356. priv->tx_queue[i]->tx_skbuff = NULL;
  357. priv->tx_queue[i]->qindex = i;
  358. priv->tx_queue[i]->dev = priv->ndev;
  359. spin_lock_init(&(priv->tx_queue[i]->txlock));
  360. }
  361. return 0;
  362. }
  363. static int gfar_alloc_rx_queues(struct gfar_private *priv)
  364. {
  365. int i;
  366. for (i = 0; i < priv->num_rx_queues; i++) {
  367. priv->rx_queue[i] = kzalloc(sizeof(struct gfar_priv_rx_q),
  368. GFP_KERNEL);
  369. if (!priv->rx_queue[i])
  370. return -ENOMEM;
  371. priv->rx_queue[i]->qindex = i;
  372. priv->rx_queue[i]->ndev = priv->ndev;
  373. }
  374. return 0;
  375. }
  376. static void gfar_free_tx_queues(struct gfar_private *priv)
  377. {
  378. int i;
  379. for (i = 0; i < priv->num_tx_queues; i++)
  380. kfree(priv->tx_queue[i]);
  381. }
  382. static void gfar_free_rx_queues(struct gfar_private *priv)
  383. {
  384. int i;
  385. for (i = 0; i < priv->num_rx_queues; i++)
  386. kfree(priv->rx_queue[i]);
  387. }
  388. static void unmap_group_regs(struct gfar_private *priv)
  389. {
  390. int i;
  391. for (i = 0; i < MAXGROUPS; i++)
  392. if (priv->gfargrp[i].regs)
  393. iounmap(priv->gfargrp[i].regs);
  394. }
  395. static void free_gfar_dev(struct gfar_private *priv)
  396. {
  397. int i, j;
  398. for (i = 0; i < priv->num_grps; i++)
  399. for (j = 0; j < GFAR_NUM_IRQS; j++) {
  400. kfree(priv->gfargrp[i].irqinfo[j]);
  401. priv->gfargrp[i].irqinfo[j] = NULL;
  402. }
  403. free_netdev(priv->ndev);
  404. }
  405. static void disable_napi(struct gfar_private *priv)
  406. {
  407. int i;
  408. for (i = 0; i < priv->num_grps; i++) {
  409. napi_disable(&priv->gfargrp[i].napi_rx);
  410. napi_disable(&priv->gfargrp[i].napi_tx);
  411. }
  412. }
  413. static void enable_napi(struct gfar_private *priv)
  414. {
  415. int i;
  416. for (i = 0; i < priv->num_grps; i++) {
  417. napi_enable(&priv->gfargrp[i].napi_rx);
  418. napi_enable(&priv->gfargrp[i].napi_tx);
  419. }
  420. }
  421. static int gfar_parse_group(struct device_node *np,
  422. struct gfar_private *priv, const char *model)
  423. {
  424. struct gfar_priv_grp *grp = &priv->gfargrp[priv->num_grps];
  425. int i;
  426. for (i = 0; i < GFAR_NUM_IRQS; i++) {
  427. grp->irqinfo[i] = kzalloc(sizeof(struct gfar_irqinfo),
  428. GFP_KERNEL);
  429. if (!grp->irqinfo[i])
  430. return -ENOMEM;
  431. }
  432. grp->regs = of_iomap(np, 0);
  433. if (!grp->regs)
  434. return -ENOMEM;
  435. gfar_irq(grp, TX)->irq = irq_of_parse_and_map(np, 0);
  436. /* If we aren't the FEC we have multiple interrupts */
  437. if (model && strcasecmp(model, "FEC")) {
  438. gfar_irq(grp, RX)->irq = irq_of_parse_and_map(np, 1);
  439. gfar_irq(grp, ER)->irq = irq_of_parse_and_map(np, 2);
  440. if (!gfar_irq(grp, TX)->irq ||
  441. !gfar_irq(grp, RX)->irq ||
  442. !gfar_irq(grp, ER)->irq)
  443. return -EINVAL;
  444. }
  445. grp->priv = priv;
  446. spin_lock_init(&grp->grplock);
  447. if (priv->mode == MQ_MG_MODE) {
  448. /* One Q per interrupt group: Q0 to G0, Q1 to G1 */
  449. grp->rx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
  450. grp->tx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
  451. } else {
  452. grp->rx_bit_map = 0xFF;
  453. grp->tx_bit_map = 0xFF;
  454. }
  455. /* bit_map's MSB is q0 (from q0 to q7) but, for_each_set_bit parses
  456. * right to left, so we need to revert the 8 bits to get the q index
  457. */
  458. grp->rx_bit_map = bitrev8(grp->rx_bit_map);
  459. grp->tx_bit_map = bitrev8(grp->tx_bit_map);
  460. /* Calculate RSTAT, TSTAT, RQUEUE and TQUEUE values,
  461. * also assign queues to groups
  462. */
  463. for_each_set_bit(i, &grp->rx_bit_map, priv->num_rx_queues) {
  464. if (!grp->rx_queue)
  465. grp->rx_queue = priv->rx_queue[i];
  466. grp->num_rx_queues++;
  467. grp->rstat |= (RSTAT_CLEAR_RHALT >> i);
  468. priv->rqueue |= ((RQUEUE_EN0 | RQUEUE_EX0) >> i);
  469. priv->rx_queue[i]->grp = grp;
  470. }
  471. for_each_set_bit(i, &grp->tx_bit_map, priv->num_tx_queues) {
  472. if (!grp->tx_queue)
  473. grp->tx_queue = priv->tx_queue[i];
  474. grp->num_tx_queues++;
  475. grp->tstat |= (TSTAT_CLEAR_THALT >> i);
  476. priv->tqueue |= (TQUEUE_EN0 >> i);
  477. priv->tx_queue[i]->grp = grp;
  478. }
  479. priv->num_grps++;
  480. return 0;
  481. }
  482. static int gfar_of_group_count(struct device_node *np)
  483. {
  484. struct device_node *child;
  485. int num = 0;
  486. for_each_available_child_of_node(np, child)
  487. if (of_node_name_eq(child, "queue-group"))
  488. num++;
  489. return num;
  490. }
  491. /* Reads the controller's registers to determine what interface
  492. * connects it to the PHY.
  493. */
  494. static phy_interface_t gfar_get_interface(struct net_device *dev)
  495. {
  496. struct gfar_private *priv = netdev_priv(dev);
  497. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  498. u32 ecntrl;
  499. ecntrl = gfar_read(&regs->ecntrl);
  500. if (ecntrl & ECNTRL_SGMII_MODE)
  501. return PHY_INTERFACE_MODE_SGMII;
  502. if (ecntrl & ECNTRL_TBI_MODE) {
  503. if (ecntrl & ECNTRL_REDUCED_MODE)
  504. return PHY_INTERFACE_MODE_RTBI;
  505. else
  506. return PHY_INTERFACE_MODE_TBI;
  507. }
  508. if (ecntrl & ECNTRL_REDUCED_MODE) {
  509. if (ecntrl & ECNTRL_REDUCED_MII_MODE) {
  510. return PHY_INTERFACE_MODE_RMII;
  511. }
  512. else {
  513. phy_interface_t interface = priv->interface;
  514. /* This isn't autodetected right now, so it must
  515. * be set by the device tree or platform code.
  516. */
  517. if (interface == PHY_INTERFACE_MODE_RGMII_ID)
  518. return PHY_INTERFACE_MODE_RGMII_ID;
  519. return PHY_INTERFACE_MODE_RGMII;
  520. }
  521. }
  522. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
  523. return PHY_INTERFACE_MODE_GMII;
  524. return PHY_INTERFACE_MODE_MII;
  525. }
  526. static int gfar_of_init(struct platform_device *ofdev, struct net_device **pdev)
  527. {
  528. const char *model;
  529. int err = 0, i;
  530. phy_interface_t interface;
  531. struct net_device *dev = NULL;
  532. struct gfar_private *priv = NULL;
  533. struct device_node *np = ofdev->dev.of_node;
  534. struct device_node *child = NULL;
  535. u32 stash_len = 0;
  536. u32 stash_idx = 0;
  537. unsigned int num_tx_qs, num_rx_qs;
  538. unsigned short mode;
  539. if (!np)
  540. return -ENODEV;
  541. if (of_device_is_compatible(np, "fsl,etsec2"))
  542. mode = MQ_MG_MODE;
  543. else
  544. mode = SQ_SG_MODE;
  545. if (mode == SQ_SG_MODE) {
  546. num_tx_qs = 1;
  547. num_rx_qs = 1;
  548. } else { /* MQ_MG_MODE */
  549. /* get the actual number of supported groups */
  550. unsigned int num_grps = gfar_of_group_count(np);
  551. if (num_grps == 0 || num_grps > MAXGROUPS) {
  552. dev_err(&ofdev->dev, "Invalid # of int groups(%d)\n",
  553. num_grps);
  554. pr_err("Cannot do alloc_etherdev, aborting\n");
  555. return -EINVAL;
  556. }
  557. num_tx_qs = num_grps; /* one txq per int group */
  558. num_rx_qs = num_grps; /* one rxq per int group */
  559. }
  560. if (num_tx_qs > MAX_TX_QS) {
  561. pr_err("num_tx_qs(=%d) greater than MAX_TX_QS(=%d)\n",
  562. num_tx_qs, MAX_TX_QS);
  563. pr_err("Cannot do alloc_etherdev, aborting\n");
  564. return -EINVAL;
  565. }
  566. if (num_rx_qs > MAX_RX_QS) {
  567. pr_err("num_rx_qs(=%d) greater than MAX_RX_QS(=%d)\n",
  568. num_rx_qs, MAX_RX_QS);
  569. pr_err("Cannot do alloc_etherdev, aborting\n");
  570. return -EINVAL;
  571. }
  572. *pdev = alloc_etherdev_mq(sizeof(*priv), num_tx_qs);
  573. dev = *pdev;
  574. if (NULL == dev)
  575. return -ENOMEM;
  576. priv = netdev_priv(dev);
  577. priv->ndev = dev;
  578. priv->mode = mode;
  579. priv->num_tx_queues = num_tx_qs;
  580. netif_set_real_num_rx_queues(dev, num_rx_qs);
  581. priv->num_rx_queues = num_rx_qs;
  582. err = gfar_alloc_tx_queues(priv);
  583. if (err)
  584. goto tx_alloc_failed;
  585. err = gfar_alloc_rx_queues(priv);
  586. if (err)
  587. goto rx_alloc_failed;
  588. err = of_property_read_string(np, "model", &model);
  589. if (err) {
  590. pr_err("Device model property missing, aborting\n");
  591. goto rx_alloc_failed;
  592. }
  593. /* Init Rx queue filer rule set linked list */
  594. INIT_LIST_HEAD(&priv->rx_list.list);
  595. priv->rx_list.count = 0;
  596. mutex_init(&priv->rx_queue_access);
  597. for (i = 0; i < MAXGROUPS; i++)
  598. priv->gfargrp[i].regs = NULL;
  599. /* Parse and initialize group specific information */
  600. if (priv->mode == MQ_MG_MODE) {
  601. for_each_available_child_of_node(np, child) {
  602. if (!of_node_name_eq(child, "queue-group"))
  603. continue;
  604. err = gfar_parse_group(child, priv, model);
  605. if (err) {
  606. of_node_put(child);
  607. goto err_grp_init;
  608. }
  609. }
  610. } else { /* SQ_SG_MODE */
  611. err = gfar_parse_group(np, priv, model);
  612. if (err)
  613. goto err_grp_init;
  614. }
  615. if (of_property_read_bool(np, "bd-stash")) {
  616. priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING;
  617. priv->bd_stash_en = 1;
  618. }
  619. err = of_property_read_u32(np, "rx-stash-len", &stash_len);
  620. if (err == 0)
  621. priv->rx_stash_size = stash_len;
  622. err = of_property_read_u32(np, "rx-stash-idx", &stash_idx);
  623. if (err == 0)
  624. priv->rx_stash_index = stash_idx;
  625. if (stash_len || stash_idx)
  626. priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING;
  627. err = of_get_ethdev_address(np, dev);
  628. if (err) {
  629. eth_hw_addr_random(dev);
  630. dev_info(&ofdev->dev, "Using random MAC address: %pM\n", dev->dev_addr);
  631. }
  632. if (model && !strcasecmp(model, "TSEC"))
  633. priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
  634. FSL_GIANFAR_DEV_HAS_COALESCE |
  635. FSL_GIANFAR_DEV_HAS_RMON |
  636. FSL_GIANFAR_DEV_HAS_MULTI_INTR;
  637. if (model && !strcasecmp(model, "eTSEC"))
  638. priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
  639. FSL_GIANFAR_DEV_HAS_COALESCE |
  640. FSL_GIANFAR_DEV_HAS_RMON |
  641. FSL_GIANFAR_DEV_HAS_MULTI_INTR |
  642. FSL_GIANFAR_DEV_HAS_CSUM |
  643. FSL_GIANFAR_DEV_HAS_VLAN |
  644. FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
  645. FSL_GIANFAR_DEV_HAS_EXTENDED_HASH |
  646. FSL_GIANFAR_DEV_HAS_TIMER |
  647. FSL_GIANFAR_DEV_HAS_RX_FILER;
  648. /* Use PHY connection type from the DT node if one is specified there.
  649. * rgmii-id really needs to be specified. Other types can be
  650. * detected by hardware
  651. */
  652. err = of_get_phy_mode(np, &interface);
  653. if (!err)
  654. priv->interface = interface;
  655. else
  656. priv->interface = gfar_get_interface(dev);
  657. if (of_find_property(np, "fsl,magic-packet", NULL))
  658. priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
  659. if (of_get_property(np, "fsl,wake-on-filer", NULL))
  660. priv->device_flags |= FSL_GIANFAR_DEV_HAS_WAKE_ON_FILER;
  661. priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
  662. /* In the case of a fixed PHY, the DT node associated
  663. * to the PHY is the Ethernet MAC DT node.
  664. */
  665. if (!priv->phy_node && of_phy_is_fixed_link(np)) {
  666. err = of_phy_register_fixed_link(np);
  667. if (err)
  668. goto err_grp_init;
  669. priv->phy_node = of_node_get(np);
  670. }
  671. /* Find the TBI PHY. If it's not there, we don't support SGMII */
  672. priv->tbi_node = of_parse_phandle(np, "tbi-handle", 0);
  673. return 0;
  674. err_grp_init:
  675. unmap_group_regs(priv);
  676. rx_alloc_failed:
  677. gfar_free_rx_queues(priv);
  678. tx_alloc_failed:
  679. gfar_free_tx_queues(priv);
  680. free_gfar_dev(priv);
  681. return err;
  682. }
  683. static u32 cluster_entry_per_class(struct gfar_private *priv, u32 rqfar,
  684. u32 class)
  685. {
  686. u32 rqfpr = FPR_FILER_MASK;
  687. u32 rqfcr = 0x0;
  688. rqfar--;
  689. rqfcr = RQFCR_CLE | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
  690. priv->ftp_rqfpr[rqfar] = rqfpr;
  691. priv->ftp_rqfcr[rqfar] = rqfcr;
  692. gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
  693. rqfar--;
  694. rqfcr = RQFCR_CMP_NOMATCH;
  695. priv->ftp_rqfpr[rqfar] = rqfpr;
  696. priv->ftp_rqfcr[rqfar] = rqfcr;
  697. gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
  698. rqfar--;
  699. rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND;
  700. rqfpr = class;
  701. priv->ftp_rqfcr[rqfar] = rqfcr;
  702. priv->ftp_rqfpr[rqfar] = rqfpr;
  703. gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
  704. rqfar--;
  705. rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_MASK | RQFCR_AND;
  706. rqfpr = class;
  707. priv->ftp_rqfcr[rqfar] = rqfcr;
  708. priv->ftp_rqfpr[rqfar] = rqfpr;
  709. gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
  710. return rqfar;
  711. }
  712. static void gfar_init_filer_table(struct gfar_private *priv)
  713. {
  714. int i = 0x0;
  715. u32 rqfar = MAX_FILER_IDX;
  716. u32 rqfcr = 0x0;
  717. u32 rqfpr = FPR_FILER_MASK;
  718. /* Default rule */
  719. rqfcr = RQFCR_CMP_MATCH;
  720. priv->ftp_rqfcr[rqfar] = rqfcr;
  721. priv->ftp_rqfpr[rqfar] = rqfpr;
  722. gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
  723. rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6);
  724. rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_UDP);
  725. rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_TCP);
  726. rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4);
  727. rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_UDP);
  728. rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_TCP);
  729. /* cur_filer_idx indicated the first non-masked rule */
  730. priv->cur_filer_idx = rqfar;
  731. /* Rest are masked rules */
  732. rqfcr = RQFCR_CMP_NOMATCH;
  733. for (i = 0; i < rqfar; i++) {
  734. priv->ftp_rqfcr[i] = rqfcr;
  735. priv->ftp_rqfpr[i] = rqfpr;
  736. gfar_write_filer(priv, i, rqfcr, rqfpr);
  737. }
  738. }
  739. #ifdef CONFIG_PPC
  740. static void __gfar_detect_errata_83xx(struct gfar_private *priv)
  741. {
  742. unsigned int pvr = mfspr(SPRN_PVR);
  743. unsigned int svr = mfspr(SPRN_SVR);
  744. unsigned int mod = (svr >> 16) & 0xfff6; /* w/o E suffix */
  745. unsigned int rev = svr & 0xffff;
  746. /* MPC8313 Rev 2.0 and higher; All MPC837x */
  747. if ((pvr == 0x80850010 && mod == 0x80b0 && rev >= 0x0020) ||
  748. (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
  749. priv->errata |= GFAR_ERRATA_74;
  750. /* MPC8313 and MPC837x all rev */
  751. if ((pvr == 0x80850010 && mod == 0x80b0) ||
  752. (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
  753. priv->errata |= GFAR_ERRATA_76;
  754. /* MPC8313 Rev < 2.0 */
  755. if (pvr == 0x80850010 && mod == 0x80b0 && rev < 0x0020)
  756. priv->errata |= GFAR_ERRATA_12;
  757. }
  758. static void __gfar_detect_errata_85xx(struct gfar_private *priv)
  759. {
  760. unsigned int svr = mfspr(SPRN_SVR);
  761. if ((SVR_SOC_VER(svr) == SVR_8548) && (SVR_REV(svr) == 0x20))
  762. priv->errata |= GFAR_ERRATA_12;
  763. /* P2020/P1010 Rev 1; MPC8548 Rev 2 */
  764. if (((SVR_SOC_VER(svr) == SVR_P2020) && (SVR_REV(svr) < 0x20)) ||
  765. ((SVR_SOC_VER(svr) == SVR_P2010) && (SVR_REV(svr) < 0x20)) ||
  766. ((SVR_SOC_VER(svr) == SVR_8548) && (SVR_REV(svr) < 0x31)))
  767. priv->errata |= GFAR_ERRATA_76; /* aka eTSEC 20 */
  768. }
  769. #endif
  770. static void gfar_detect_errata(struct gfar_private *priv)
  771. {
  772. struct device *dev = &priv->ofdev->dev;
  773. /* no plans to fix */
  774. priv->errata |= GFAR_ERRATA_A002;
  775. #ifdef CONFIG_PPC
  776. if (pvr_version_is(PVR_VER_E500V1) || pvr_version_is(PVR_VER_E500V2))
  777. __gfar_detect_errata_85xx(priv);
  778. else /* non-mpc85xx parts, i.e. e300 core based */
  779. __gfar_detect_errata_83xx(priv);
  780. #endif
  781. if (priv->errata)
  782. dev_info(dev, "enabled errata workarounds, flags: 0x%x\n",
  783. priv->errata);
  784. }
  785. static void gfar_init_addr_hash_table(struct gfar_private *priv)
  786. {
  787. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  788. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
  789. priv->extended_hash = 1;
  790. priv->hash_width = 9;
  791. priv->hash_regs[0] = &regs->igaddr0;
  792. priv->hash_regs[1] = &regs->igaddr1;
  793. priv->hash_regs[2] = &regs->igaddr2;
  794. priv->hash_regs[3] = &regs->igaddr3;
  795. priv->hash_regs[4] = &regs->igaddr4;
  796. priv->hash_regs[5] = &regs->igaddr5;
  797. priv->hash_regs[6] = &regs->igaddr6;
  798. priv->hash_regs[7] = &regs->igaddr7;
  799. priv->hash_regs[8] = &regs->gaddr0;
  800. priv->hash_regs[9] = &regs->gaddr1;
  801. priv->hash_regs[10] = &regs->gaddr2;
  802. priv->hash_regs[11] = &regs->gaddr3;
  803. priv->hash_regs[12] = &regs->gaddr4;
  804. priv->hash_regs[13] = &regs->gaddr5;
  805. priv->hash_regs[14] = &regs->gaddr6;
  806. priv->hash_regs[15] = &regs->gaddr7;
  807. } else {
  808. priv->extended_hash = 0;
  809. priv->hash_width = 8;
  810. priv->hash_regs[0] = &regs->gaddr0;
  811. priv->hash_regs[1] = &regs->gaddr1;
  812. priv->hash_regs[2] = &regs->gaddr2;
  813. priv->hash_regs[3] = &regs->gaddr3;
  814. priv->hash_regs[4] = &regs->gaddr4;
  815. priv->hash_regs[5] = &regs->gaddr5;
  816. priv->hash_regs[6] = &regs->gaddr6;
  817. priv->hash_regs[7] = &regs->gaddr7;
  818. }
  819. }
  820. static int __gfar_is_rx_idle(struct gfar_private *priv)
  821. {
  822. u32 res;
  823. /* Normaly TSEC should not hang on GRS commands, so we should
  824. * actually wait for IEVENT_GRSC flag.
  825. */
  826. if (!gfar_has_errata(priv, GFAR_ERRATA_A002))
  827. return 0;
  828. /* Read the eTSEC register at offset 0xD1C. If bits 7-14 are
  829. * the same as bits 23-30, the eTSEC Rx is assumed to be idle
  830. * and the Rx can be safely reset.
  831. */
  832. res = gfar_read((void __iomem *)priv->gfargrp[0].regs + 0xd1c);
  833. res &= 0x7f807f80;
  834. if ((res & 0xffff) == (res >> 16))
  835. return 1;
  836. return 0;
  837. }
  838. /* Halt the receive and transmit queues */
  839. static void gfar_halt_nodisable(struct gfar_private *priv)
  840. {
  841. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  842. u32 tempval;
  843. unsigned int timeout;
  844. int stopped;
  845. gfar_ints_disable(priv);
  846. if (gfar_is_dma_stopped(priv))
  847. return;
  848. /* Stop the DMA, and wait for it to stop */
  849. tempval = gfar_read(&regs->dmactrl);
  850. tempval |= (DMACTRL_GRS | DMACTRL_GTS);
  851. gfar_write(&regs->dmactrl, tempval);
  852. retry:
  853. timeout = 1000;
  854. while (!(stopped = gfar_is_dma_stopped(priv)) && timeout) {
  855. cpu_relax();
  856. timeout--;
  857. }
  858. if (!timeout)
  859. stopped = gfar_is_dma_stopped(priv);
  860. if (!stopped && !gfar_is_rx_dma_stopped(priv) &&
  861. !__gfar_is_rx_idle(priv))
  862. goto retry;
  863. }
  864. /* Halt the receive and transmit queues */
  865. static void gfar_halt(struct gfar_private *priv)
  866. {
  867. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  868. u32 tempval;
  869. /* Dissable the Rx/Tx hw queues */
  870. gfar_write(&regs->rqueue, 0);
  871. gfar_write(&regs->tqueue, 0);
  872. mdelay(10);
  873. gfar_halt_nodisable(priv);
  874. /* Disable Rx/Tx DMA */
  875. tempval = gfar_read(&regs->maccfg1);
  876. tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
  877. gfar_write(&regs->maccfg1, tempval);
  878. }
  879. static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue)
  880. {
  881. struct txbd8 *txbdp;
  882. struct gfar_private *priv = netdev_priv(tx_queue->dev);
  883. int i, j;
  884. txbdp = tx_queue->tx_bd_base;
  885. for (i = 0; i < tx_queue->tx_ring_size; i++) {
  886. if (!tx_queue->tx_skbuff[i])
  887. continue;
  888. dma_unmap_single(priv->dev, be32_to_cpu(txbdp->bufPtr),
  889. be16_to_cpu(txbdp->length), DMA_TO_DEVICE);
  890. txbdp->lstatus = 0;
  891. for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags;
  892. j++) {
  893. txbdp++;
  894. dma_unmap_page(priv->dev, be32_to_cpu(txbdp->bufPtr),
  895. be16_to_cpu(txbdp->length),
  896. DMA_TO_DEVICE);
  897. }
  898. txbdp++;
  899. dev_kfree_skb_any(tx_queue->tx_skbuff[i]);
  900. tx_queue->tx_skbuff[i] = NULL;
  901. }
  902. kfree(tx_queue->tx_skbuff);
  903. tx_queue->tx_skbuff = NULL;
  904. }
  905. static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue)
  906. {
  907. int i;
  908. struct rxbd8 *rxbdp = rx_queue->rx_bd_base;
  909. dev_kfree_skb(rx_queue->skb);
  910. for (i = 0; i < rx_queue->rx_ring_size; i++) {
  911. struct gfar_rx_buff *rxb = &rx_queue->rx_buff[i];
  912. rxbdp->lstatus = 0;
  913. rxbdp->bufPtr = 0;
  914. rxbdp++;
  915. if (!rxb->page)
  916. continue;
  917. dma_unmap_page(rx_queue->dev, rxb->dma,
  918. PAGE_SIZE, DMA_FROM_DEVICE);
  919. __free_page(rxb->page);
  920. rxb->page = NULL;
  921. }
  922. kfree(rx_queue->rx_buff);
  923. rx_queue->rx_buff = NULL;
  924. }
  925. /* If there are any tx skbs or rx skbs still around, free them.
  926. * Then free tx_skbuff and rx_skbuff
  927. */
  928. static void free_skb_resources(struct gfar_private *priv)
  929. {
  930. struct gfar_priv_tx_q *tx_queue = NULL;
  931. struct gfar_priv_rx_q *rx_queue = NULL;
  932. int i;
  933. /* Go through all the buffer descriptors and free their data buffers */
  934. for (i = 0; i < priv->num_tx_queues; i++) {
  935. struct netdev_queue *txq;
  936. tx_queue = priv->tx_queue[i];
  937. txq = netdev_get_tx_queue(tx_queue->dev, tx_queue->qindex);
  938. if (tx_queue->tx_skbuff)
  939. free_skb_tx_queue(tx_queue);
  940. netdev_tx_reset_queue(txq);
  941. }
  942. for (i = 0; i < priv->num_rx_queues; i++) {
  943. rx_queue = priv->rx_queue[i];
  944. if (rx_queue->rx_buff)
  945. free_skb_rx_queue(rx_queue);
  946. }
  947. dma_free_coherent(priv->dev,
  948. sizeof(struct txbd8) * priv->total_tx_ring_size +
  949. sizeof(struct rxbd8) * priv->total_rx_ring_size,
  950. priv->tx_queue[0]->tx_bd_base,
  951. priv->tx_queue[0]->tx_bd_dma_base);
  952. }
  953. void stop_gfar(struct net_device *dev)
  954. {
  955. struct gfar_private *priv = netdev_priv(dev);
  956. netif_tx_stop_all_queues(dev);
  957. smp_mb__before_atomic();
  958. set_bit(GFAR_DOWN, &priv->state);
  959. smp_mb__after_atomic();
  960. disable_napi(priv);
  961. /* disable ints and gracefully shut down Rx/Tx DMA */
  962. gfar_halt(priv);
  963. phy_stop(dev->phydev);
  964. free_skb_resources(priv);
  965. }
  966. static void gfar_start(struct gfar_private *priv)
  967. {
  968. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  969. u32 tempval;
  970. int i = 0;
  971. /* Enable Rx/Tx hw queues */
  972. gfar_write(&regs->rqueue, priv->rqueue);
  973. gfar_write(&regs->tqueue, priv->tqueue);
  974. /* Initialize DMACTRL to have WWR and WOP */
  975. tempval = gfar_read(&regs->dmactrl);
  976. tempval |= DMACTRL_INIT_SETTINGS;
  977. gfar_write(&regs->dmactrl, tempval);
  978. /* Make sure we aren't stopped */
  979. tempval = gfar_read(&regs->dmactrl);
  980. tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
  981. gfar_write(&regs->dmactrl, tempval);
  982. for (i = 0; i < priv->num_grps; i++) {
  983. regs = priv->gfargrp[i].regs;
  984. /* Clear THLT/RHLT, so that the DMA starts polling now */
  985. gfar_write(&regs->tstat, priv->gfargrp[i].tstat);
  986. gfar_write(&regs->rstat, priv->gfargrp[i].rstat);
  987. }
  988. /* Enable Rx/Tx DMA */
  989. tempval = gfar_read(&regs->maccfg1);
  990. tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
  991. gfar_write(&regs->maccfg1, tempval);
  992. gfar_ints_enable(priv);
  993. netif_trans_update(priv->ndev); /* prevent tx timeout */
  994. }
  995. static bool gfar_new_page(struct gfar_priv_rx_q *rxq, struct gfar_rx_buff *rxb)
  996. {
  997. struct page *page;
  998. dma_addr_t addr;
  999. page = dev_alloc_page();
  1000. if (unlikely(!page))
  1001. return false;
  1002. addr = dma_map_page(rxq->dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE);
  1003. if (unlikely(dma_mapping_error(rxq->dev, addr))) {
  1004. __free_page(page);
  1005. return false;
  1006. }
  1007. rxb->dma = addr;
  1008. rxb->page = page;
  1009. rxb->page_offset = 0;
  1010. return true;
  1011. }
  1012. static void gfar_rx_alloc_err(struct gfar_priv_rx_q *rx_queue)
  1013. {
  1014. struct gfar_private *priv = netdev_priv(rx_queue->ndev);
  1015. struct gfar_extra_stats *estats = &priv->extra_stats;
  1016. netdev_err(rx_queue->ndev, "Can't alloc RX buffers\n");
  1017. atomic64_inc(&estats->rx_alloc_err);
  1018. }
  1019. static void gfar_alloc_rx_buffs(struct gfar_priv_rx_q *rx_queue,
  1020. int alloc_cnt)
  1021. {
  1022. struct rxbd8 *bdp;
  1023. struct gfar_rx_buff *rxb;
  1024. int i;
  1025. i = rx_queue->next_to_use;
  1026. bdp = &rx_queue->rx_bd_base[i];
  1027. rxb = &rx_queue->rx_buff[i];
  1028. while (alloc_cnt--) {
  1029. /* try reuse page */
  1030. if (unlikely(!rxb->page)) {
  1031. if (unlikely(!gfar_new_page(rx_queue, rxb))) {
  1032. gfar_rx_alloc_err(rx_queue);
  1033. break;
  1034. }
  1035. }
  1036. /* Setup the new RxBD */
  1037. gfar_init_rxbdp(rx_queue, bdp,
  1038. rxb->dma + rxb->page_offset + RXBUF_ALIGNMENT);
  1039. /* Update to the next pointer */
  1040. bdp++;
  1041. rxb++;
  1042. if (unlikely(++i == rx_queue->rx_ring_size)) {
  1043. i = 0;
  1044. bdp = rx_queue->rx_bd_base;
  1045. rxb = rx_queue->rx_buff;
  1046. }
  1047. }
  1048. rx_queue->next_to_use = i;
  1049. rx_queue->next_to_alloc = i;
  1050. }
  1051. static void gfar_init_bds(struct net_device *ndev)
  1052. {
  1053. struct gfar_private *priv = netdev_priv(ndev);
  1054. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1055. struct gfar_priv_tx_q *tx_queue = NULL;
  1056. struct gfar_priv_rx_q *rx_queue = NULL;
  1057. struct txbd8 *txbdp;
  1058. u32 __iomem *rfbptr;
  1059. int i, j;
  1060. for (i = 0; i < priv->num_tx_queues; i++) {
  1061. tx_queue = priv->tx_queue[i];
  1062. /* Initialize some variables in our dev structure */
  1063. tx_queue->num_txbdfree = tx_queue->tx_ring_size;
  1064. tx_queue->dirty_tx = tx_queue->tx_bd_base;
  1065. tx_queue->cur_tx = tx_queue->tx_bd_base;
  1066. tx_queue->skb_curtx = 0;
  1067. tx_queue->skb_dirtytx = 0;
  1068. /* Initialize Transmit Descriptor Ring */
  1069. txbdp = tx_queue->tx_bd_base;
  1070. for (j = 0; j < tx_queue->tx_ring_size; j++) {
  1071. txbdp->lstatus = 0;
  1072. txbdp->bufPtr = 0;
  1073. txbdp++;
  1074. }
  1075. /* Set the last descriptor in the ring to indicate wrap */
  1076. txbdp--;
  1077. txbdp->status = cpu_to_be16(be16_to_cpu(txbdp->status) |
  1078. TXBD_WRAP);
  1079. }
  1080. rfbptr = &regs->rfbptr0;
  1081. for (i = 0; i < priv->num_rx_queues; i++) {
  1082. rx_queue = priv->rx_queue[i];
  1083. rx_queue->next_to_clean = 0;
  1084. rx_queue->next_to_use = 0;
  1085. rx_queue->next_to_alloc = 0;
  1086. /* make sure next_to_clean != next_to_use after this
  1087. * by leaving at least 1 unused descriptor
  1088. */
  1089. gfar_alloc_rx_buffs(rx_queue, gfar_rxbd_unused(rx_queue));
  1090. rx_queue->rfbptr = rfbptr;
  1091. rfbptr += 2;
  1092. }
  1093. }
  1094. static int gfar_alloc_skb_resources(struct net_device *ndev)
  1095. {
  1096. void *vaddr;
  1097. dma_addr_t addr;
  1098. int i, j;
  1099. struct gfar_private *priv = netdev_priv(ndev);
  1100. struct device *dev = priv->dev;
  1101. struct gfar_priv_tx_q *tx_queue = NULL;
  1102. struct gfar_priv_rx_q *rx_queue = NULL;
  1103. priv->total_tx_ring_size = 0;
  1104. for (i = 0; i < priv->num_tx_queues; i++)
  1105. priv->total_tx_ring_size += priv->tx_queue[i]->tx_ring_size;
  1106. priv->total_rx_ring_size = 0;
  1107. for (i = 0; i < priv->num_rx_queues; i++)
  1108. priv->total_rx_ring_size += priv->rx_queue[i]->rx_ring_size;
  1109. /* Allocate memory for the buffer descriptors */
  1110. vaddr = dma_alloc_coherent(dev,
  1111. (priv->total_tx_ring_size *
  1112. sizeof(struct txbd8)) +
  1113. (priv->total_rx_ring_size *
  1114. sizeof(struct rxbd8)),
  1115. &addr, GFP_KERNEL);
  1116. if (!vaddr)
  1117. return -ENOMEM;
  1118. for (i = 0; i < priv->num_tx_queues; i++) {
  1119. tx_queue = priv->tx_queue[i];
  1120. tx_queue->tx_bd_base = vaddr;
  1121. tx_queue->tx_bd_dma_base = addr;
  1122. tx_queue->dev = ndev;
  1123. /* enet DMA only understands physical addresses */
  1124. addr += sizeof(struct txbd8) * tx_queue->tx_ring_size;
  1125. vaddr += sizeof(struct txbd8) * tx_queue->tx_ring_size;
  1126. }
  1127. /* Start the rx descriptor ring where the tx ring leaves off */
  1128. for (i = 0; i < priv->num_rx_queues; i++) {
  1129. rx_queue = priv->rx_queue[i];
  1130. rx_queue->rx_bd_base = vaddr;
  1131. rx_queue->rx_bd_dma_base = addr;
  1132. rx_queue->ndev = ndev;
  1133. rx_queue->dev = dev;
  1134. addr += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
  1135. vaddr += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
  1136. }
  1137. /* Setup the skbuff rings */
  1138. for (i = 0; i < priv->num_tx_queues; i++) {
  1139. tx_queue = priv->tx_queue[i];
  1140. tx_queue->tx_skbuff =
  1141. kmalloc_array(tx_queue->tx_ring_size,
  1142. sizeof(*tx_queue->tx_skbuff),
  1143. GFP_KERNEL);
  1144. if (!tx_queue->tx_skbuff)
  1145. goto cleanup;
  1146. for (j = 0; j < tx_queue->tx_ring_size; j++)
  1147. tx_queue->tx_skbuff[j] = NULL;
  1148. }
  1149. for (i = 0; i < priv->num_rx_queues; i++) {
  1150. rx_queue = priv->rx_queue[i];
  1151. rx_queue->rx_buff = kcalloc(rx_queue->rx_ring_size,
  1152. sizeof(*rx_queue->rx_buff),
  1153. GFP_KERNEL);
  1154. if (!rx_queue->rx_buff)
  1155. goto cleanup;
  1156. }
  1157. gfar_init_bds(ndev);
  1158. return 0;
  1159. cleanup:
  1160. free_skb_resources(priv);
  1161. return -ENOMEM;
  1162. }
  1163. /* Bring the controller up and running */
  1164. int startup_gfar(struct net_device *ndev)
  1165. {
  1166. struct gfar_private *priv = netdev_priv(ndev);
  1167. int err;
  1168. gfar_mac_reset(priv);
  1169. err = gfar_alloc_skb_resources(ndev);
  1170. if (err)
  1171. return err;
  1172. gfar_init_tx_rx_base(priv);
  1173. smp_mb__before_atomic();
  1174. clear_bit(GFAR_DOWN, &priv->state);
  1175. smp_mb__after_atomic();
  1176. /* Start Rx/Tx DMA and enable the interrupts */
  1177. gfar_start(priv);
  1178. /* force link state update after mac reset */
  1179. priv->oldlink = 0;
  1180. priv->oldspeed = 0;
  1181. priv->oldduplex = -1;
  1182. phy_start(ndev->phydev);
  1183. enable_napi(priv);
  1184. netif_tx_wake_all_queues(ndev);
  1185. return 0;
  1186. }
  1187. static u32 gfar_get_flowctrl_cfg(struct gfar_private *priv)
  1188. {
  1189. struct net_device *ndev = priv->ndev;
  1190. struct phy_device *phydev = ndev->phydev;
  1191. u32 val = 0;
  1192. if (!phydev->duplex)
  1193. return val;
  1194. if (!priv->pause_aneg_en) {
  1195. if (priv->tx_pause_en)
  1196. val |= MACCFG1_TX_FLOW;
  1197. if (priv->rx_pause_en)
  1198. val |= MACCFG1_RX_FLOW;
  1199. } else {
  1200. u16 lcl_adv, rmt_adv;
  1201. u8 flowctrl;
  1202. /* get link partner capabilities */
  1203. rmt_adv = 0;
  1204. if (phydev->pause)
  1205. rmt_adv = LPA_PAUSE_CAP;
  1206. if (phydev->asym_pause)
  1207. rmt_adv |= LPA_PAUSE_ASYM;
  1208. lcl_adv = linkmode_adv_to_lcl_adv_t(phydev->advertising);
  1209. flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
  1210. if (flowctrl & FLOW_CTRL_TX)
  1211. val |= MACCFG1_TX_FLOW;
  1212. if (flowctrl & FLOW_CTRL_RX)
  1213. val |= MACCFG1_RX_FLOW;
  1214. }
  1215. return val;
  1216. }
  1217. static noinline void gfar_update_link_state(struct gfar_private *priv)
  1218. {
  1219. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1220. struct net_device *ndev = priv->ndev;
  1221. struct phy_device *phydev = ndev->phydev;
  1222. struct gfar_priv_rx_q *rx_queue = NULL;
  1223. int i;
  1224. if (unlikely(test_bit(GFAR_RESETTING, &priv->state)))
  1225. return;
  1226. if (phydev->link) {
  1227. u32 tempval1 = gfar_read(&regs->maccfg1);
  1228. u32 tempval = gfar_read(&regs->maccfg2);
  1229. u32 ecntrl = gfar_read(&regs->ecntrl);
  1230. u32 tx_flow_oldval = (tempval1 & MACCFG1_TX_FLOW);
  1231. if (phydev->duplex != priv->oldduplex) {
  1232. if (!(phydev->duplex))
  1233. tempval &= ~(MACCFG2_FULL_DUPLEX);
  1234. else
  1235. tempval |= MACCFG2_FULL_DUPLEX;
  1236. priv->oldduplex = phydev->duplex;
  1237. }
  1238. if (phydev->speed != priv->oldspeed) {
  1239. switch (phydev->speed) {
  1240. case 1000:
  1241. tempval =
  1242. ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
  1243. ecntrl &= ~(ECNTRL_R100);
  1244. break;
  1245. case 100:
  1246. case 10:
  1247. tempval =
  1248. ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
  1249. /* Reduced mode distinguishes
  1250. * between 10 and 100
  1251. */
  1252. if (phydev->speed == SPEED_100)
  1253. ecntrl |= ECNTRL_R100;
  1254. else
  1255. ecntrl &= ~(ECNTRL_R100);
  1256. break;
  1257. default:
  1258. netif_warn(priv, link, priv->ndev,
  1259. "Ack! Speed (%d) is not 10/100/1000!\n",
  1260. phydev->speed);
  1261. break;
  1262. }
  1263. priv->oldspeed = phydev->speed;
  1264. }
  1265. tempval1 &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
  1266. tempval1 |= gfar_get_flowctrl_cfg(priv);
  1267. /* Turn last free buffer recording on */
  1268. if ((tempval1 & MACCFG1_TX_FLOW) && !tx_flow_oldval) {
  1269. for (i = 0; i < priv->num_rx_queues; i++) {
  1270. u32 bdp_dma;
  1271. rx_queue = priv->rx_queue[i];
  1272. bdp_dma = gfar_rxbd_dma_lastfree(rx_queue);
  1273. gfar_write(rx_queue->rfbptr, bdp_dma);
  1274. }
  1275. priv->tx_actual_en = 1;
  1276. }
  1277. if (unlikely(!(tempval1 & MACCFG1_TX_FLOW) && tx_flow_oldval))
  1278. priv->tx_actual_en = 0;
  1279. gfar_write(&regs->maccfg1, tempval1);
  1280. gfar_write(&regs->maccfg2, tempval);
  1281. gfar_write(&regs->ecntrl, ecntrl);
  1282. if (!priv->oldlink)
  1283. priv->oldlink = 1;
  1284. } else if (priv->oldlink) {
  1285. priv->oldlink = 0;
  1286. priv->oldspeed = 0;
  1287. priv->oldduplex = -1;
  1288. }
  1289. if (netif_msg_link(priv))
  1290. phy_print_status(phydev);
  1291. }
  1292. /* Called every time the controller might need to be made
  1293. * aware of new link state. The PHY code conveys this
  1294. * information through variables in the phydev structure, and this
  1295. * function converts those variables into the appropriate
  1296. * register values, and can bring down the device if needed.
  1297. */
  1298. static void adjust_link(struct net_device *dev)
  1299. {
  1300. struct gfar_private *priv = netdev_priv(dev);
  1301. struct phy_device *phydev = dev->phydev;
  1302. if (unlikely(phydev->link != priv->oldlink ||
  1303. (phydev->link && (phydev->duplex != priv->oldduplex ||
  1304. phydev->speed != priv->oldspeed))))
  1305. gfar_update_link_state(priv);
  1306. }
  1307. /* Initialize TBI PHY interface for communicating with the
  1308. * SERDES lynx PHY on the chip. We communicate with this PHY
  1309. * through the MDIO bus on each controller, treating it as a
  1310. * "normal" PHY at the address found in the TBIPA register. We assume
  1311. * that the TBIPA register is valid. Either the MDIO bus code will set
  1312. * it to a value that doesn't conflict with other PHYs on the bus, or the
  1313. * value doesn't matter, as there are no other PHYs on the bus.
  1314. */
  1315. static void gfar_configure_serdes(struct net_device *dev)
  1316. {
  1317. struct gfar_private *priv = netdev_priv(dev);
  1318. struct phy_device *tbiphy;
  1319. if (!priv->tbi_node) {
  1320. dev_warn(&dev->dev, "error: SGMII mode requires that the "
  1321. "device tree specify a tbi-handle\n");
  1322. return;
  1323. }
  1324. tbiphy = of_phy_find_device(priv->tbi_node);
  1325. if (!tbiphy) {
  1326. dev_err(&dev->dev, "error: Could not get TBI device\n");
  1327. return;
  1328. }
  1329. /* If the link is already up, we must already be ok, and don't need to
  1330. * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
  1331. * everything for us? Resetting it takes the link down and requires
  1332. * several seconds for it to come back.
  1333. */
  1334. if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS) {
  1335. put_device(&tbiphy->mdio.dev);
  1336. return;
  1337. }
  1338. /* Single clk mode, mii mode off(for serdes communication) */
  1339. phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
  1340. phy_write(tbiphy, MII_ADVERTISE,
  1341. ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
  1342. ADVERTISE_1000XPSE_ASYM);
  1343. phy_write(tbiphy, MII_BMCR,
  1344. BMCR_ANENABLE | BMCR_ANRESTART | BMCR_FULLDPLX |
  1345. BMCR_SPEED1000);
  1346. put_device(&tbiphy->mdio.dev);
  1347. }
  1348. /* Initializes driver's PHY state, and attaches to the PHY.
  1349. * Returns 0 on success.
  1350. */
  1351. static int init_phy(struct net_device *dev)
  1352. {
  1353. __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
  1354. struct gfar_private *priv = netdev_priv(dev);
  1355. phy_interface_t interface = priv->interface;
  1356. struct phy_device *phydev;
  1357. struct ethtool_eee edata;
  1358. linkmode_set_bit_array(phy_10_100_features_array,
  1359. ARRAY_SIZE(phy_10_100_features_array),
  1360. mask);
  1361. linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, mask);
  1362. linkmode_set_bit(ETHTOOL_LINK_MODE_MII_BIT, mask);
  1363. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
  1364. linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, mask);
  1365. priv->oldlink = 0;
  1366. priv->oldspeed = 0;
  1367. priv->oldduplex = -1;
  1368. phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
  1369. interface);
  1370. if (!phydev) {
  1371. dev_err(&dev->dev, "could not attach to PHY\n");
  1372. return -ENODEV;
  1373. }
  1374. if (interface == PHY_INTERFACE_MODE_SGMII)
  1375. gfar_configure_serdes(dev);
  1376. /* Remove any features not supported by the controller */
  1377. linkmode_and(phydev->supported, phydev->supported, mask);
  1378. linkmode_copy(phydev->advertising, phydev->supported);
  1379. /* Add support for flow control */
  1380. phy_support_asym_pause(phydev);
  1381. /* disable EEE autoneg, EEE not supported by eTSEC */
  1382. memset(&edata, 0, sizeof(struct ethtool_eee));
  1383. phy_ethtool_set_eee(phydev, &edata);
  1384. return 0;
  1385. }
  1386. static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
  1387. {
  1388. struct txfcb *fcb = skb_push(skb, GMAC_FCB_LEN);
  1389. memset(fcb, 0, GMAC_FCB_LEN);
  1390. return fcb;
  1391. }
  1392. static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb,
  1393. int fcb_length)
  1394. {
  1395. /* If we're here, it's a IP packet with a TCP or UDP
  1396. * payload. We set it to checksum, using a pseudo-header
  1397. * we provide
  1398. */
  1399. u8 flags = TXFCB_DEFAULT;
  1400. /* Tell the controller what the protocol is
  1401. * And provide the already calculated phcs
  1402. */
  1403. if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
  1404. flags |= TXFCB_UDP;
  1405. fcb->phcs = (__force __be16)(udp_hdr(skb)->check);
  1406. } else
  1407. fcb->phcs = (__force __be16)(tcp_hdr(skb)->check);
  1408. /* l3os is the distance between the start of the
  1409. * frame (skb->data) and the start of the IP hdr.
  1410. * l4os is the distance between the start of the
  1411. * l3 hdr and the l4 hdr
  1412. */
  1413. fcb->l3os = (u8)(skb_network_offset(skb) - fcb_length);
  1414. fcb->l4os = skb_network_header_len(skb);
  1415. fcb->flags = flags;
  1416. }
  1417. static inline void gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
  1418. {
  1419. fcb->flags |= TXFCB_VLN;
  1420. fcb->vlctl = cpu_to_be16(skb_vlan_tag_get(skb));
  1421. }
  1422. static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
  1423. struct txbd8 *base, int ring_size)
  1424. {
  1425. struct txbd8 *new_bd = bdp + stride;
  1426. return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
  1427. }
  1428. static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
  1429. int ring_size)
  1430. {
  1431. return skip_txbd(bdp, 1, base, ring_size);
  1432. }
  1433. /* eTSEC12: csum generation not supported for some fcb offsets */
  1434. static inline bool gfar_csum_errata_12(struct gfar_private *priv,
  1435. unsigned long fcb_addr)
  1436. {
  1437. return (gfar_has_errata(priv, GFAR_ERRATA_12) &&
  1438. (fcb_addr % 0x20) > 0x18);
  1439. }
  1440. /* eTSEC76: csum generation for frames larger than 2500 may
  1441. * cause excess delays before start of transmission
  1442. */
  1443. static inline bool gfar_csum_errata_76(struct gfar_private *priv,
  1444. unsigned int len)
  1445. {
  1446. return (gfar_has_errata(priv, GFAR_ERRATA_76) &&
  1447. (len > 2500));
  1448. }
  1449. /* This is called by the kernel when a frame is ready for transmission.
  1450. * It is pointed to by the dev->hard_start_xmit function pointer
  1451. */
  1452. static netdev_tx_t gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1453. {
  1454. struct gfar_private *priv = netdev_priv(dev);
  1455. struct gfar_priv_tx_q *tx_queue = NULL;
  1456. struct netdev_queue *txq;
  1457. struct gfar __iomem *regs = NULL;
  1458. struct txfcb *fcb = NULL;
  1459. struct txbd8 *txbdp, *txbdp_start, *base, *txbdp_tstamp = NULL;
  1460. u32 lstatus;
  1461. skb_frag_t *frag;
  1462. int i, rq = 0;
  1463. int do_tstamp, do_csum, do_vlan;
  1464. u32 bufaddr;
  1465. unsigned int nr_frags, nr_txbds, bytes_sent, fcb_len = 0;
  1466. rq = skb->queue_mapping;
  1467. tx_queue = priv->tx_queue[rq];
  1468. txq = netdev_get_tx_queue(dev, rq);
  1469. base = tx_queue->tx_bd_base;
  1470. regs = tx_queue->grp->regs;
  1471. do_csum = (CHECKSUM_PARTIAL == skb->ip_summed);
  1472. do_vlan = skb_vlan_tag_present(skb);
  1473. do_tstamp = (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  1474. priv->hwts_tx_en;
  1475. if (do_csum || do_vlan)
  1476. fcb_len = GMAC_FCB_LEN;
  1477. /* check if time stamp should be generated */
  1478. if (unlikely(do_tstamp))
  1479. fcb_len = GMAC_FCB_LEN + GMAC_TXPAL_LEN;
  1480. /* make space for additional header when fcb is needed */
  1481. if (fcb_len) {
  1482. if (unlikely(skb_cow_head(skb, fcb_len))) {
  1483. dev->stats.tx_errors++;
  1484. dev_kfree_skb_any(skb);
  1485. return NETDEV_TX_OK;
  1486. }
  1487. }
  1488. /* total number of fragments in the SKB */
  1489. nr_frags = skb_shinfo(skb)->nr_frags;
  1490. /* calculate the required number of TxBDs for this skb */
  1491. if (unlikely(do_tstamp))
  1492. nr_txbds = nr_frags + 2;
  1493. else
  1494. nr_txbds = nr_frags + 1;
  1495. /* check if there is space to queue this packet */
  1496. if (nr_txbds > tx_queue->num_txbdfree) {
  1497. /* no space, stop the queue */
  1498. netif_tx_stop_queue(txq);
  1499. dev->stats.tx_fifo_errors++;
  1500. return NETDEV_TX_BUSY;
  1501. }
  1502. /* Update transmit stats */
  1503. bytes_sent = skb->len;
  1504. tx_queue->stats.tx_bytes += bytes_sent;
  1505. /* keep Tx bytes on wire for BQL accounting */
  1506. GFAR_CB(skb)->bytes_sent = bytes_sent;
  1507. tx_queue->stats.tx_packets++;
  1508. txbdp = txbdp_start = tx_queue->cur_tx;
  1509. lstatus = be32_to_cpu(txbdp->lstatus);
  1510. /* Add TxPAL between FCB and frame if required */
  1511. if (unlikely(do_tstamp)) {
  1512. skb_push(skb, GMAC_TXPAL_LEN);
  1513. memset(skb->data, 0, GMAC_TXPAL_LEN);
  1514. }
  1515. /* Add TxFCB if required */
  1516. if (fcb_len) {
  1517. fcb = gfar_add_fcb(skb);
  1518. lstatus |= BD_LFLAG(TXBD_TOE);
  1519. }
  1520. /* Set up checksumming */
  1521. if (do_csum) {
  1522. gfar_tx_checksum(skb, fcb, fcb_len);
  1523. if (unlikely(gfar_csum_errata_12(priv, (unsigned long)fcb)) ||
  1524. unlikely(gfar_csum_errata_76(priv, skb->len))) {
  1525. __skb_pull(skb, GMAC_FCB_LEN);
  1526. skb_checksum_help(skb);
  1527. if (do_vlan || do_tstamp) {
  1528. /* put back a new fcb for vlan/tstamp TOE */
  1529. fcb = gfar_add_fcb(skb);
  1530. } else {
  1531. /* Tx TOE not used */
  1532. lstatus &= ~(BD_LFLAG(TXBD_TOE));
  1533. fcb = NULL;
  1534. }
  1535. }
  1536. }
  1537. if (do_vlan)
  1538. gfar_tx_vlan(skb, fcb);
  1539. bufaddr = dma_map_single(priv->dev, skb->data, skb_headlen(skb),
  1540. DMA_TO_DEVICE);
  1541. if (unlikely(dma_mapping_error(priv->dev, bufaddr)))
  1542. goto dma_map_err;
  1543. txbdp_start->bufPtr = cpu_to_be32(bufaddr);
  1544. /* Time stamp insertion requires one additional TxBD */
  1545. if (unlikely(do_tstamp))
  1546. txbdp_tstamp = txbdp = next_txbd(txbdp, base,
  1547. tx_queue->tx_ring_size);
  1548. if (likely(!nr_frags)) {
  1549. if (likely(!do_tstamp))
  1550. lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
  1551. } else {
  1552. u32 lstatus_start = lstatus;
  1553. /* Place the fragment addresses and lengths into the TxBDs */
  1554. frag = &skb_shinfo(skb)->frags[0];
  1555. for (i = 0; i < nr_frags; i++, frag++) {
  1556. unsigned int size;
  1557. /* Point at the next BD, wrapping as needed */
  1558. txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
  1559. size = skb_frag_size(frag);
  1560. lstatus = be32_to_cpu(txbdp->lstatus) | size |
  1561. BD_LFLAG(TXBD_READY);
  1562. /* Handle the last BD specially */
  1563. if (i == nr_frags - 1)
  1564. lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
  1565. bufaddr = skb_frag_dma_map(priv->dev, frag, 0,
  1566. size, DMA_TO_DEVICE);
  1567. if (unlikely(dma_mapping_error(priv->dev, bufaddr)))
  1568. goto dma_map_err;
  1569. /* set the TxBD length and buffer pointer */
  1570. txbdp->bufPtr = cpu_to_be32(bufaddr);
  1571. txbdp->lstatus = cpu_to_be32(lstatus);
  1572. }
  1573. lstatus = lstatus_start;
  1574. }
  1575. /* If time stamping is requested one additional TxBD must be set up. The
  1576. * first TxBD points to the FCB and must have a data length of
  1577. * GMAC_FCB_LEN. The second TxBD points to the actual frame data with
  1578. * the full frame length.
  1579. */
  1580. if (unlikely(do_tstamp)) {
  1581. u32 lstatus_ts = be32_to_cpu(txbdp_tstamp->lstatus);
  1582. bufaddr = be32_to_cpu(txbdp_start->bufPtr);
  1583. bufaddr += fcb_len;
  1584. lstatus_ts |= BD_LFLAG(TXBD_READY) |
  1585. (skb_headlen(skb) - fcb_len);
  1586. if (!nr_frags)
  1587. lstatus_ts |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
  1588. txbdp_tstamp->bufPtr = cpu_to_be32(bufaddr);
  1589. txbdp_tstamp->lstatus = cpu_to_be32(lstatus_ts);
  1590. lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | GMAC_FCB_LEN;
  1591. /* Setup tx hardware time stamping */
  1592. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1593. fcb->ptp = 1;
  1594. } else {
  1595. lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
  1596. }
  1597. skb_tx_timestamp(skb);
  1598. netdev_tx_sent_queue(txq, bytes_sent);
  1599. gfar_wmb();
  1600. txbdp_start->lstatus = cpu_to_be32(lstatus);
  1601. gfar_wmb(); /* force lstatus write before tx_skbuff */
  1602. tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb;
  1603. /* Update the current skb pointer to the next entry we will use
  1604. * (wrapping if necessary)
  1605. */
  1606. tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) &
  1607. TX_RING_MOD_MASK(tx_queue->tx_ring_size);
  1608. tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
  1609. /* We can work in parallel with gfar_clean_tx_ring(), except
  1610. * when modifying num_txbdfree. Note that we didn't grab the lock
  1611. * when we were reading the num_txbdfree and checking for available
  1612. * space, that's because outside of this function it can only grow.
  1613. */
  1614. spin_lock_bh(&tx_queue->txlock);
  1615. /* reduce TxBD free count */
  1616. tx_queue->num_txbdfree -= (nr_txbds);
  1617. spin_unlock_bh(&tx_queue->txlock);
  1618. /* If the next BD still needs to be cleaned up, then the bds
  1619. * are full. We need to tell the kernel to stop sending us stuff.
  1620. */
  1621. if (!tx_queue->num_txbdfree) {
  1622. netif_tx_stop_queue(txq);
  1623. dev->stats.tx_fifo_errors++;
  1624. }
  1625. /* Tell the DMA to go go go */
  1626. gfar_write(&regs->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex);
  1627. return NETDEV_TX_OK;
  1628. dma_map_err:
  1629. txbdp = next_txbd(txbdp_start, base, tx_queue->tx_ring_size);
  1630. if (do_tstamp)
  1631. txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
  1632. for (i = 0; i < nr_frags; i++) {
  1633. lstatus = be32_to_cpu(txbdp->lstatus);
  1634. if (!(lstatus & BD_LFLAG(TXBD_READY)))
  1635. break;
  1636. lstatus &= ~BD_LFLAG(TXBD_READY);
  1637. txbdp->lstatus = cpu_to_be32(lstatus);
  1638. bufaddr = be32_to_cpu(txbdp->bufPtr);
  1639. dma_unmap_page(priv->dev, bufaddr, be16_to_cpu(txbdp->length),
  1640. DMA_TO_DEVICE);
  1641. txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
  1642. }
  1643. gfar_wmb();
  1644. dev_kfree_skb_any(skb);
  1645. return NETDEV_TX_OK;
  1646. }
  1647. /* Changes the mac address if the controller is not running. */
  1648. static int gfar_set_mac_address(struct net_device *dev)
  1649. {
  1650. gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
  1651. return 0;
  1652. }
  1653. static int gfar_change_mtu(struct net_device *dev, int new_mtu)
  1654. {
  1655. struct gfar_private *priv = netdev_priv(dev);
  1656. while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
  1657. cpu_relax();
  1658. if (dev->flags & IFF_UP)
  1659. stop_gfar(dev);
  1660. dev->mtu = new_mtu;
  1661. if (dev->flags & IFF_UP)
  1662. startup_gfar(dev);
  1663. clear_bit_unlock(GFAR_RESETTING, &priv->state);
  1664. return 0;
  1665. }
  1666. static void reset_gfar(struct net_device *ndev)
  1667. {
  1668. struct gfar_private *priv = netdev_priv(ndev);
  1669. while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
  1670. cpu_relax();
  1671. stop_gfar(ndev);
  1672. startup_gfar(ndev);
  1673. clear_bit_unlock(GFAR_RESETTING, &priv->state);
  1674. }
  1675. /* gfar_reset_task gets scheduled when a packet has not been
  1676. * transmitted after a set amount of time.
  1677. * For now, assume that clearing out all the structures, and
  1678. * starting over will fix the problem.
  1679. */
  1680. static void gfar_reset_task(struct work_struct *work)
  1681. {
  1682. struct gfar_private *priv = container_of(work, struct gfar_private,
  1683. reset_task);
  1684. reset_gfar(priv->ndev);
  1685. }
  1686. static void gfar_timeout(struct net_device *dev, unsigned int txqueue)
  1687. {
  1688. struct gfar_private *priv = netdev_priv(dev);
  1689. dev->stats.tx_errors++;
  1690. schedule_work(&priv->reset_task);
  1691. }
  1692. static int gfar_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
  1693. {
  1694. struct hwtstamp_config config;
  1695. struct gfar_private *priv = netdev_priv(netdev);
  1696. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  1697. return -EFAULT;
  1698. switch (config.tx_type) {
  1699. case HWTSTAMP_TX_OFF:
  1700. priv->hwts_tx_en = 0;
  1701. break;
  1702. case HWTSTAMP_TX_ON:
  1703. if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
  1704. return -ERANGE;
  1705. priv->hwts_tx_en = 1;
  1706. break;
  1707. default:
  1708. return -ERANGE;
  1709. }
  1710. switch (config.rx_filter) {
  1711. case HWTSTAMP_FILTER_NONE:
  1712. if (priv->hwts_rx_en) {
  1713. priv->hwts_rx_en = 0;
  1714. reset_gfar(netdev);
  1715. }
  1716. break;
  1717. default:
  1718. if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
  1719. return -ERANGE;
  1720. if (!priv->hwts_rx_en) {
  1721. priv->hwts_rx_en = 1;
  1722. reset_gfar(netdev);
  1723. }
  1724. config.rx_filter = HWTSTAMP_FILTER_ALL;
  1725. break;
  1726. }
  1727. return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
  1728. -EFAULT : 0;
  1729. }
  1730. static int gfar_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr)
  1731. {
  1732. struct hwtstamp_config config;
  1733. struct gfar_private *priv = netdev_priv(netdev);
  1734. config.flags = 0;
  1735. config.tx_type = priv->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
  1736. config.rx_filter = (priv->hwts_rx_en ?
  1737. HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE);
  1738. return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
  1739. -EFAULT : 0;
  1740. }
  1741. static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1742. {
  1743. struct phy_device *phydev = dev->phydev;
  1744. if (!netif_running(dev))
  1745. return -EINVAL;
  1746. if (cmd == SIOCSHWTSTAMP)
  1747. return gfar_hwtstamp_set(dev, rq);
  1748. if (cmd == SIOCGHWTSTAMP)
  1749. return gfar_hwtstamp_get(dev, rq);
  1750. if (!phydev)
  1751. return -ENODEV;
  1752. return phy_mii_ioctl(phydev, rq, cmd);
  1753. }
  1754. /* Interrupt Handler for Transmit complete */
  1755. static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
  1756. {
  1757. struct net_device *dev = tx_queue->dev;
  1758. struct netdev_queue *txq;
  1759. struct gfar_private *priv = netdev_priv(dev);
  1760. struct txbd8 *bdp, *next = NULL;
  1761. struct txbd8 *lbdp = NULL;
  1762. struct txbd8 *base = tx_queue->tx_bd_base;
  1763. struct sk_buff *skb;
  1764. int skb_dirtytx;
  1765. int tx_ring_size = tx_queue->tx_ring_size;
  1766. int frags = 0, nr_txbds = 0;
  1767. int i;
  1768. int howmany = 0;
  1769. int tqi = tx_queue->qindex;
  1770. unsigned int bytes_sent = 0;
  1771. u32 lstatus;
  1772. size_t buflen;
  1773. txq = netdev_get_tx_queue(dev, tqi);
  1774. bdp = tx_queue->dirty_tx;
  1775. skb_dirtytx = tx_queue->skb_dirtytx;
  1776. while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
  1777. bool do_tstamp;
  1778. do_tstamp = (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  1779. priv->hwts_tx_en;
  1780. frags = skb_shinfo(skb)->nr_frags;
  1781. /* When time stamping, one additional TxBD must be freed.
  1782. * Also, we need to dma_unmap_single() the TxPAL.
  1783. */
  1784. if (unlikely(do_tstamp))
  1785. nr_txbds = frags + 2;
  1786. else
  1787. nr_txbds = frags + 1;
  1788. lbdp = skip_txbd(bdp, nr_txbds - 1, base, tx_ring_size);
  1789. lstatus = be32_to_cpu(lbdp->lstatus);
  1790. /* Only clean completed frames */
  1791. if ((lstatus & BD_LFLAG(TXBD_READY)) &&
  1792. (lstatus & BD_LENGTH_MASK))
  1793. break;
  1794. if (unlikely(do_tstamp)) {
  1795. next = next_txbd(bdp, base, tx_ring_size);
  1796. buflen = be16_to_cpu(next->length) +
  1797. GMAC_FCB_LEN + GMAC_TXPAL_LEN;
  1798. } else
  1799. buflen = be16_to_cpu(bdp->length);
  1800. dma_unmap_single(priv->dev, be32_to_cpu(bdp->bufPtr),
  1801. buflen, DMA_TO_DEVICE);
  1802. if (unlikely(do_tstamp)) {
  1803. struct skb_shared_hwtstamps shhwtstamps;
  1804. u64 *ns = (u64 *)(((uintptr_t)skb->data + 0x10) &
  1805. ~0x7UL);
  1806. memset(&shhwtstamps, 0, sizeof(shhwtstamps));
  1807. shhwtstamps.hwtstamp = ns_to_ktime(be64_to_cpu(*ns));
  1808. skb_pull(skb, GMAC_FCB_LEN + GMAC_TXPAL_LEN);
  1809. skb_tstamp_tx(skb, &shhwtstamps);
  1810. gfar_clear_txbd_status(bdp);
  1811. bdp = next;
  1812. }
  1813. gfar_clear_txbd_status(bdp);
  1814. bdp = next_txbd(bdp, base, tx_ring_size);
  1815. for (i = 0; i < frags; i++) {
  1816. dma_unmap_page(priv->dev, be32_to_cpu(bdp->bufPtr),
  1817. be16_to_cpu(bdp->length),
  1818. DMA_TO_DEVICE);
  1819. gfar_clear_txbd_status(bdp);
  1820. bdp = next_txbd(bdp, base, tx_ring_size);
  1821. }
  1822. bytes_sent += GFAR_CB(skb)->bytes_sent;
  1823. dev_kfree_skb_any(skb);
  1824. tx_queue->tx_skbuff[skb_dirtytx] = NULL;
  1825. skb_dirtytx = (skb_dirtytx + 1) &
  1826. TX_RING_MOD_MASK(tx_ring_size);
  1827. howmany++;
  1828. spin_lock(&tx_queue->txlock);
  1829. tx_queue->num_txbdfree += nr_txbds;
  1830. spin_unlock(&tx_queue->txlock);
  1831. }
  1832. /* If we freed a buffer, we can restart transmission, if necessary */
  1833. if (tx_queue->num_txbdfree &&
  1834. netif_tx_queue_stopped(txq) &&
  1835. !(test_bit(GFAR_DOWN, &priv->state)))
  1836. netif_wake_subqueue(priv->ndev, tqi);
  1837. /* Update dirty indicators */
  1838. tx_queue->skb_dirtytx = skb_dirtytx;
  1839. tx_queue->dirty_tx = bdp;
  1840. netdev_tx_completed_queue(txq, howmany, bytes_sent);
  1841. }
  1842. static void count_errors(u32 lstatus, struct net_device *ndev)
  1843. {
  1844. struct gfar_private *priv = netdev_priv(ndev);
  1845. struct net_device_stats *stats = &ndev->stats;
  1846. struct gfar_extra_stats *estats = &priv->extra_stats;
  1847. /* If the packet was truncated, none of the other errors matter */
  1848. if (lstatus & BD_LFLAG(RXBD_TRUNCATED)) {
  1849. stats->rx_length_errors++;
  1850. atomic64_inc(&estats->rx_trunc);
  1851. return;
  1852. }
  1853. /* Count the errors, if there were any */
  1854. if (lstatus & BD_LFLAG(RXBD_LARGE | RXBD_SHORT)) {
  1855. stats->rx_length_errors++;
  1856. if (lstatus & BD_LFLAG(RXBD_LARGE))
  1857. atomic64_inc(&estats->rx_large);
  1858. else
  1859. atomic64_inc(&estats->rx_short);
  1860. }
  1861. if (lstatus & BD_LFLAG(RXBD_NONOCTET)) {
  1862. stats->rx_frame_errors++;
  1863. atomic64_inc(&estats->rx_nonoctet);
  1864. }
  1865. if (lstatus & BD_LFLAG(RXBD_CRCERR)) {
  1866. atomic64_inc(&estats->rx_crcerr);
  1867. stats->rx_crc_errors++;
  1868. }
  1869. if (lstatus & BD_LFLAG(RXBD_OVERRUN)) {
  1870. atomic64_inc(&estats->rx_overrun);
  1871. stats->rx_over_errors++;
  1872. }
  1873. }
  1874. static irqreturn_t gfar_receive(int irq, void *grp_id)
  1875. {
  1876. struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
  1877. unsigned long flags;
  1878. u32 imask, ievent;
  1879. ievent = gfar_read(&grp->regs->ievent);
  1880. if (unlikely(ievent & IEVENT_FGPI)) {
  1881. gfar_write(&grp->regs->ievent, IEVENT_FGPI);
  1882. return IRQ_HANDLED;
  1883. }
  1884. if (likely(napi_schedule_prep(&grp->napi_rx))) {
  1885. spin_lock_irqsave(&grp->grplock, flags);
  1886. imask = gfar_read(&grp->regs->imask);
  1887. imask &= IMASK_RX_DISABLED | grp->priv->rmon_overflow.imask;
  1888. gfar_write(&grp->regs->imask, imask);
  1889. spin_unlock_irqrestore(&grp->grplock, flags);
  1890. __napi_schedule(&grp->napi_rx);
  1891. } else {
  1892. /* Clear IEVENT, so interrupts aren't called again
  1893. * because of the packets that have already arrived.
  1894. */
  1895. gfar_write(&grp->regs->ievent, IEVENT_RX_MASK);
  1896. }
  1897. return IRQ_HANDLED;
  1898. }
  1899. /* Interrupt Handler for Transmit complete */
  1900. static irqreturn_t gfar_transmit(int irq, void *grp_id)
  1901. {
  1902. struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
  1903. unsigned long flags;
  1904. u32 imask;
  1905. if (likely(napi_schedule_prep(&grp->napi_tx))) {
  1906. spin_lock_irqsave(&grp->grplock, flags);
  1907. imask = gfar_read(&grp->regs->imask);
  1908. imask &= IMASK_TX_DISABLED | grp->priv->rmon_overflow.imask;
  1909. gfar_write(&grp->regs->imask, imask);
  1910. spin_unlock_irqrestore(&grp->grplock, flags);
  1911. __napi_schedule(&grp->napi_tx);
  1912. } else {
  1913. /* Clear IEVENT, so interrupts aren't called again
  1914. * because of the packets that have already arrived.
  1915. */
  1916. gfar_write(&grp->regs->ievent, IEVENT_TX_MASK);
  1917. }
  1918. return IRQ_HANDLED;
  1919. }
  1920. static bool gfar_add_rx_frag(struct gfar_rx_buff *rxb, u32 lstatus,
  1921. struct sk_buff *skb, bool first)
  1922. {
  1923. int size = lstatus & BD_LENGTH_MASK;
  1924. struct page *page = rxb->page;
  1925. if (likely(first)) {
  1926. skb_put(skb, size);
  1927. } else {
  1928. /* the last fragments' length contains the full frame length */
  1929. if (lstatus & BD_LFLAG(RXBD_LAST))
  1930. size -= skb->len;
  1931. WARN(size < 0, "gianfar: rx fragment size underflow");
  1932. if (size < 0)
  1933. return false;
  1934. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
  1935. rxb->page_offset + RXBUF_ALIGNMENT,
  1936. size, GFAR_RXB_TRUESIZE);
  1937. }
  1938. /* try reuse page */
  1939. if (unlikely(page_count(page) != 1 || page_is_pfmemalloc(page)))
  1940. return false;
  1941. /* change offset to the other half */
  1942. rxb->page_offset ^= GFAR_RXB_TRUESIZE;
  1943. page_ref_inc(page);
  1944. return true;
  1945. }
  1946. static void gfar_reuse_rx_page(struct gfar_priv_rx_q *rxq,
  1947. struct gfar_rx_buff *old_rxb)
  1948. {
  1949. struct gfar_rx_buff *new_rxb;
  1950. u16 nta = rxq->next_to_alloc;
  1951. new_rxb = &rxq->rx_buff[nta];
  1952. /* find next buf that can reuse a page */
  1953. nta++;
  1954. rxq->next_to_alloc = (nta < rxq->rx_ring_size) ? nta : 0;
  1955. /* copy page reference */
  1956. *new_rxb = *old_rxb;
  1957. /* sync for use by the device */
  1958. dma_sync_single_range_for_device(rxq->dev, old_rxb->dma,
  1959. old_rxb->page_offset,
  1960. GFAR_RXB_TRUESIZE, DMA_FROM_DEVICE);
  1961. }
  1962. static struct sk_buff *gfar_get_next_rxbuff(struct gfar_priv_rx_q *rx_queue,
  1963. u32 lstatus, struct sk_buff *skb)
  1964. {
  1965. struct gfar_rx_buff *rxb = &rx_queue->rx_buff[rx_queue->next_to_clean];
  1966. struct page *page = rxb->page;
  1967. bool first = false;
  1968. if (likely(!skb)) {
  1969. void *buff_addr = page_address(page) + rxb->page_offset;
  1970. skb = build_skb(buff_addr, GFAR_SKBFRAG_SIZE);
  1971. if (unlikely(!skb)) {
  1972. gfar_rx_alloc_err(rx_queue);
  1973. return NULL;
  1974. }
  1975. skb_reserve(skb, RXBUF_ALIGNMENT);
  1976. first = true;
  1977. }
  1978. dma_sync_single_range_for_cpu(rx_queue->dev, rxb->dma, rxb->page_offset,
  1979. GFAR_RXB_TRUESIZE, DMA_FROM_DEVICE);
  1980. if (gfar_add_rx_frag(rxb, lstatus, skb, first)) {
  1981. /* reuse the free half of the page */
  1982. gfar_reuse_rx_page(rx_queue, rxb);
  1983. } else {
  1984. /* page cannot be reused, unmap it */
  1985. dma_unmap_page(rx_queue->dev, rxb->dma,
  1986. PAGE_SIZE, DMA_FROM_DEVICE);
  1987. }
  1988. /* clear rxb content */
  1989. rxb->page = NULL;
  1990. return skb;
  1991. }
  1992. static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
  1993. {
  1994. /* If valid headers were found, and valid sums
  1995. * were verified, then we tell the kernel that no
  1996. * checksumming is necessary. Otherwise, it is [FIXME]
  1997. */
  1998. if ((be16_to_cpu(fcb->flags) & RXFCB_CSUM_MASK) ==
  1999. (RXFCB_CIP | RXFCB_CTU))
  2000. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2001. else
  2002. skb_checksum_none_assert(skb);
  2003. }
  2004. /* gfar_process_frame() -- handle one incoming packet if skb isn't NULL. */
  2005. static void gfar_process_frame(struct net_device *ndev, struct sk_buff *skb)
  2006. {
  2007. struct gfar_private *priv = netdev_priv(ndev);
  2008. struct rxfcb *fcb = NULL;
  2009. /* fcb is at the beginning if exists */
  2010. fcb = (struct rxfcb *)skb->data;
  2011. /* Remove the FCB from the skb
  2012. * Remove the padded bytes, if there are any
  2013. */
  2014. if (priv->uses_rxfcb)
  2015. skb_pull(skb, GMAC_FCB_LEN);
  2016. /* Get receive timestamp from the skb */
  2017. if (priv->hwts_rx_en) {
  2018. struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
  2019. u64 *ns = (u64 *) skb->data;
  2020. memset(shhwtstamps, 0, sizeof(*shhwtstamps));
  2021. shhwtstamps->hwtstamp = ns_to_ktime(be64_to_cpu(*ns));
  2022. }
  2023. if (priv->padding)
  2024. skb_pull(skb, priv->padding);
  2025. /* Trim off the FCS */
  2026. pskb_trim(skb, skb->len - ETH_FCS_LEN);
  2027. if (ndev->features & NETIF_F_RXCSUM)
  2028. gfar_rx_checksum(skb, fcb);
  2029. /* There's need to check for NETIF_F_HW_VLAN_CTAG_RX here.
  2030. * Even if vlan rx accel is disabled, on some chips
  2031. * RXFCB_VLN is pseudo randomly set.
  2032. */
  2033. if (ndev->features & NETIF_F_HW_VLAN_CTAG_RX &&
  2034. be16_to_cpu(fcb->flags) & RXFCB_VLN)
  2035. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  2036. be16_to_cpu(fcb->vlctl));
  2037. }
  2038. /* gfar_clean_rx_ring() -- Processes each frame in the rx ring
  2039. * until the budget/quota has been reached. Returns the number
  2040. * of frames handled
  2041. */
  2042. static int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue,
  2043. int rx_work_limit)
  2044. {
  2045. struct net_device *ndev = rx_queue->ndev;
  2046. struct gfar_private *priv = netdev_priv(ndev);
  2047. struct rxbd8 *bdp;
  2048. int i, howmany = 0;
  2049. struct sk_buff *skb = rx_queue->skb;
  2050. int cleaned_cnt = gfar_rxbd_unused(rx_queue);
  2051. unsigned int total_bytes = 0, total_pkts = 0;
  2052. /* Get the first full descriptor */
  2053. i = rx_queue->next_to_clean;
  2054. while (rx_work_limit--) {
  2055. u32 lstatus;
  2056. if (cleaned_cnt >= GFAR_RX_BUFF_ALLOC) {
  2057. gfar_alloc_rx_buffs(rx_queue, cleaned_cnt);
  2058. cleaned_cnt = 0;
  2059. }
  2060. bdp = &rx_queue->rx_bd_base[i];
  2061. lstatus = be32_to_cpu(bdp->lstatus);
  2062. if (lstatus & BD_LFLAG(RXBD_EMPTY))
  2063. break;
  2064. /* lost RXBD_LAST descriptor due to overrun */
  2065. if (skb &&
  2066. (lstatus & BD_LFLAG(RXBD_FIRST))) {
  2067. /* discard faulty buffer */
  2068. dev_kfree_skb(skb);
  2069. skb = NULL;
  2070. rx_queue->stats.rx_dropped++;
  2071. /* can continue normally */
  2072. }
  2073. /* order rx buffer descriptor reads */
  2074. rmb();
  2075. /* fetch next to clean buffer from the ring */
  2076. skb = gfar_get_next_rxbuff(rx_queue, lstatus, skb);
  2077. if (unlikely(!skb))
  2078. break;
  2079. cleaned_cnt++;
  2080. howmany++;
  2081. if (unlikely(++i == rx_queue->rx_ring_size))
  2082. i = 0;
  2083. rx_queue->next_to_clean = i;
  2084. /* fetch next buffer if not the last in frame */
  2085. if (!(lstatus & BD_LFLAG(RXBD_LAST)))
  2086. continue;
  2087. if (unlikely(lstatus & BD_LFLAG(RXBD_ERR))) {
  2088. count_errors(lstatus, ndev);
  2089. /* discard faulty buffer */
  2090. dev_kfree_skb(skb);
  2091. skb = NULL;
  2092. rx_queue->stats.rx_dropped++;
  2093. continue;
  2094. }
  2095. gfar_process_frame(ndev, skb);
  2096. /* Increment the number of packets */
  2097. total_pkts++;
  2098. total_bytes += skb->len;
  2099. skb_record_rx_queue(skb, rx_queue->qindex);
  2100. skb->protocol = eth_type_trans(skb, ndev);
  2101. /* Send the packet up the stack */
  2102. napi_gro_receive(&rx_queue->grp->napi_rx, skb);
  2103. skb = NULL;
  2104. }
  2105. /* Store incomplete frames for completion */
  2106. rx_queue->skb = skb;
  2107. rx_queue->stats.rx_packets += total_pkts;
  2108. rx_queue->stats.rx_bytes += total_bytes;
  2109. if (cleaned_cnt)
  2110. gfar_alloc_rx_buffs(rx_queue, cleaned_cnt);
  2111. /* Update Last Free RxBD pointer for LFC */
  2112. if (unlikely(priv->tx_actual_en)) {
  2113. u32 bdp_dma = gfar_rxbd_dma_lastfree(rx_queue);
  2114. gfar_write(rx_queue->rfbptr, bdp_dma);
  2115. }
  2116. return howmany;
  2117. }
  2118. static int gfar_poll_rx_sq(struct napi_struct *napi, int budget)
  2119. {
  2120. struct gfar_priv_grp *gfargrp =
  2121. container_of(napi, struct gfar_priv_grp, napi_rx);
  2122. struct gfar __iomem *regs = gfargrp->regs;
  2123. struct gfar_priv_rx_q *rx_queue = gfargrp->rx_queue;
  2124. int work_done = 0;
  2125. /* Clear IEVENT, so interrupts aren't called again
  2126. * because of the packets that have already arrived
  2127. */
  2128. gfar_write(&regs->ievent, IEVENT_RX_MASK);
  2129. work_done = gfar_clean_rx_ring(rx_queue, budget);
  2130. if (work_done < budget) {
  2131. u32 imask;
  2132. napi_complete_done(napi, work_done);
  2133. /* Clear the halt bit in RSTAT */
  2134. gfar_write(&regs->rstat, gfargrp->rstat);
  2135. spin_lock_irq(&gfargrp->grplock);
  2136. imask = gfar_read(&regs->imask);
  2137. imask |= IMASK_RX_DEFAULT;
  2138. gfar_write(&regs->imask, imask);
  2139. spin_unlock_irq(&gfargrp->grplock);
  2140. }
  2141. return work_done;
  2142. }
  2143. static int gfar_poll_tx_sq(struct napi_struct *napi, int budget)
  2144. {
  2145. struct gfar_priv_grp *gfargrp =
  2146. container_of(napi, struct gfar_priv_grp, napi_tx);
  2147. struct gfar __iomem *regs = gfargrp->regs;
  2148. struct gfar_priv_tx_q *tx_queue = gfargrp->tx_queue;
  2149. u32 imask;
  2150. /* Clear IEVENT, so interrupts aren't called again
  2151. * because of the packets that have already arrived
  2152. */
  2153. gfar_write(&regs->ievent, IEVENT_TX_MASK);
  2154. /* run Tx cleanup to completion */
  2155. if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx])
  2156. gfar_clean_tx_ring(tx_queue);
  2157. napi_complete(napi);
  2158. spin_lock_irq(&gfargrp->grplock);
  2159. imask = gfar_read(&regs->imask);
  2160. imask |= IMASK_TX_DEFAULT;
  2161. gfar_write(&regs->imask, imask);
  2162. spin_unlock_irq(&gfargrp->grplock);
  2163. return 0;
  2164. }
  2165. /* GFAR error interrupt handler */
  2166. static irqreturn_t gfar_error(int irq, void *grp_id)
  2167. {
  2168. struct gfar_priv_grp *gfargrp = grp_id;
  2169. struct gfar __iomem *regs = gfargrp->regs;
  2170. struct gfar_private *priv= gfargrp->priv;
  2171. struct net_device *dev = priv->ndev;
  2172. /* Save ievent for future reference */
  2173. u32 events = gfar_read(&regs->ievent);
  2174. /* Clear IEVENT */
  2175. gfar_write(&regs->ievent, events & IEVENT_ERR_MASK);
  2176. /* Magic Packet is not an error. */
  2177. if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
  2178. (events & IEVENT_MAG))
  2179. events &= ~IEVENT_MAG;
  2180. /* Hmm... */
  2181. if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
  2182. netdev_dbg(dev,
  2183. "error interrupt (ievent=0x%08x imask=0x%08x)\n",
  2184. events, gfar_read(&regs->imask));
  2185. /* Update the error counters */
  2186. if (events & IEVENT_TXE) {
  2187. dev->stats.tx_errors++;
  2188. if (events & IEVENT_LC)
  2189. dev->stats.tx_window_errors++;
  2190. if (events & IEVENT_CRL)
  2191. dev->stats.tx_aborted_errors++;
  2192. if (events & IEVENT_XFUN) {
  2193. netif_dbg(priv, tx_err, dev,
  2194. "TX FIFO underrun, packet dropped\n");
  2195. dev->stats.tx_dropped++;
  2196. atomic64_inc(&priv->extra_stats.tx_underrun);
  2197. schedule_work(&priv->reset_task);
  2198. }
  2199. netif_dbg(priv, tx_err, dev, "Transmit Error\n");
  2200. }
  2201. if (events & IEVENT_MSRO) {
  2202. struct rmon_mib __iomem *rmon = &regs->rmon;
  2203. u32 car;
  2204. spin_lock(&priv->rmon_overflow.lock);
  2205. car = gfar_read(&rmon->car1) & CAR1_C1RDR;
  2206. if (car) {
  2207. priv->rmon_overflow.rdrp++;
  2208. gfar_write(&rmon->car1, car);
  2209. }
  2210. spin_unlock(&priv->rmon_overflow.lock);
  2211. }
  2212. if (events & IEVENT_BSY) {
  2213. dev->stats.rx_over_errors++;
  2214. atomic64_inc(&priv->extra_stats.rx_bsy);
  2215. netif_dbg(priv, rx_err, dev, "busy error (rstat: %x)\n",
  2216. gfar_read(&regs->rstat));
  2217. }
  2218. if (events & IEVENT_BABR) {
  2219. dev->stats.rx_errors++;
  2220. atomic64_inc(&priv->extra_stats.rx_babr);
  2221. netif_dbg(priv, rx_err, dev, "babbling RX error\n");
  2222. }
  2223. if (events & IEVENT_EBERR) {
  2224. atomic64_inc(&priv->extra_stats.eberr);
  2225. netif_dbg(priv, rx_err, dev, "bus error\n");
  2226. }
  2227. if (events & IEVENT_RXC)
  2228. netif_dbg(priv, rx_status, dev, "control frame\n");
  2229. if (events & IEVENT_BABT) {
  2230. atomic64_inc(&priv->extra_stats.tx_babt);
  2231. netif_dbg(priv, tx_err, dev, "babbling TX error\n");
  2232. }
  2233. return IRQ_HANDLED;
  2234. }
  2235. /* The interrupt handler for devices with one interrupt */
  2236. static irqreturn_t gfar_interrupt(int irq, void *grp_id)
  2237. {
  2238. struct gfar_priv_grp *gfargrp = grp_id;
  2239. /* Save ievent for future reference */
  2240. u32 events = gfar_read(&gfargrp->regs->ievent);
  2241. /* Check for reception */
  2242. if (events & IEVENT_RX_MASK)
  2243. gfar_receive(irq, grp_id);
  2244. /* Check for transmit completion */
  2245. if (events & IEVENT_TX_MASK)
  2246. gfar_transmit(irq, grp_id);
  2247. /* Check for errors */
  2248. if (events & IEVENT_ERR_MASK)
  2249. gfar_error(irq, grp_id);
  2250. return IRQ_HANDLED;
  2251. }
  2252. #ifdef CONFIG_NET_POLL_CONTROLLER
  2253. /* Polling 'interrupt' - used by things like netconsole to send skbs
  2254. * without having to re-enable interrupts. It's not called while
  2255. * the interrupt routine is executing.
  2256. */
  2257. static void gfar_netpoll(struct net_device *dev)
  2258. {
  2259. struct gfar_private *priv = netdev_priv(dev);
  2260. int i;
  2261. /* If the device has multiple interrupts, run tx/rx */
  2262. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  2263. for (i = 0; i < priv->num_grps; i++) {
  2264. struct gfar_priv_grp *grp = &priv->gfargrp[i];
  2265. disable_irq(gfar_irq(grp, TX)->irq);
  2266. disable_irq(gfar_irq(grp, RX)->irq);
  2267. disable_irq(gfar_irq(grp, ER)->irq);
  2268. gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
  2269. enable_irq(gfar_irq(grp, ER)->irq);
  2270. enable_irq(gfar_irq(grp, RX)->irq);
  2271. enable_irq(gfar_irq(grp, TX)->irq);
  2272. }
  2273. } else {
  2274. for (i = 0; i < priv->num_grps; i++) {
  2275. struct gfar_priv_grp *grp = &priv->gfargrp[i];
  2276. disable_irq(gfar_irq(grp, TX)->irq);
  2277. gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
  2278. enable_irq(gfar_irq(grp, TX)->irq);
  2279. }
  2280. }
  2281. }
  2282. #endif
  2283. static void free_grp_irqs(struct gfar_priv_grp *grp)
  2284. {
  2285. free_irq(gfar_irq(grp, TX)->irq, grp);
  2286. free_irq(gfar_irq(grp, RX)->irq, grp);
  2287. free_irq(gfar_irq(grp, ER)->irq, grp);
  2288. }
  2289. static int register_grp_irqs(struct gfar_priv_grp *grp)
  2290. {
  2291. struct gfar_private *priv = grp->priv;
  2292. struct net_device *dev = priv->ndev;
  2293. int err;
  2294. /* If the device has multiple interrupts, register for
  2295. * them. Otherwise, only register for the one
  2296. */
  2297. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  2298. /* Install our interrupt handlers for Error,
  2299. * Transmit, and Receive
  2300. */
  2301. err = request_irq(gfar_irq(grp, ER)->irq, gfar_error, 0,
  2302. gfar_irq(grp, ER)->name, grp);
  2303. if (err < 0) {
  2304. netif_err(priv, intr, dev, "Can't get IRQ %d\n",
  2305. gfar_irq(grp, ER)->irq);
  2306. goto err_irq_fail;
  2307. }
  2308. enable_irq_wake(gfar_irq(grp, ER)->irq);
  2309. err = request_irq(gfar_irq(grp, TX)->irq, gfar_transmit, 0,
  2310. gfar_irq(grp, TX)->name, grp);
  2311. if (err < 0) {
  2312. netif_err(priv, intr, dev, "Can't get IRQ %d\n",
  2313. gfar_irq(grp, TX)->irq);
  2314. goto tx_irq_fail;
  2315. }
  2316. err = request_irq(gfar_irq(grp, RX)->irq, gfar_receive, 0,
  2317. gfar_irq(grp, RX)->name, grp);
  2318. if (err < 0) {
  2319. netif_err(priv, intr, dev, "Can't get IRQ %d\n",
  2320. gfar_irq(grp, RX)->irq);
  2321. goto rx_irq_fail;
  2322. }
  2323. enable_irq_wake(gfar_irq(grp, RX)->irq);
  2324. } else {
  2325. err = request_irq(gfar_irq(grp, TX)->irq, gfar_interrupt, 0,
  2326. gfar_irq(grp, TX)->name, grp);
  2327. if (err < 0) {
  2328. netif_err(priv, intr, dev, "Can't get IRQ %d\n",
  2329. gfar_irq(grp, TX)->irq);
  2330. goto err_irq_fail;
  2331. }
  2332. enable_irq_wake(gfar_irq(grp, TX)->irq);
  2333. }
  2334. return 0;
  2335. rx_irq_fail:
  2336. free_irq(gfar_irq(grp, TX)->irq, grp);
  2337. tx_irq_fail:
  2338. free_irq(gfar_irq(grp, ER)->irq, grp);
  2339. err_irq_fail:
  2340. return err;
  2341. }
  2342. static void gfar_free_irq(struct gfar_private *priv)
  2343. {
  2344. int i;
  2345. /* Free the IRQs */
  2346. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  2347. for (i = 0; i < priv->num_grps; i++)
  2348. free_grp_irqs(&priv->gfargrp[i]);
  2349. } else {
  2350. for (i = 0; i < priv->num_grps; i++)
  2351. free_irq(gfar_irq(&priv->gfargrp[i], TX)->irq,
  2352. &priv->gfargrp[i]);
  2353. }
  2354. }
  2355. static int gfar_request_irq(struct gfar_private *priv)
  2356. {
  2357. int err, i, j;
  2358. for (i = 0; i < priv->num_grps; i++) {
  2359. err = register_grp_irqs(&priv->gfargrp[i]);
  2360. if (err) {
  2361. for (j = 0; j < i; j++)
  2362. free_grp_irqs(&priv->gfargrp[j]);
  2363. return err;
  2364. }
  2365. }
  2366. return 0;
  2367. }
  2368. /* Called when something needs to use the ethernet device
  2369. * Returns 0 for success.
  2370. */
  2371. static int gfar_enet_open(struct net_device *dev)
  2372. {
  2373. struct gfar_private *priv = netdev_priv(dev);
  2374. int err;
  2375. err = init_phy(dev);
  2376. if (err)
  2377. return err;
  2378. err = gfar_request_irq(priv);
  2379. if (err)
  2380. return err;
  2381. err = startup_gfar(dev);
  2382. if (err)
  2383. return err;
  2384. return err;
  2385. }
  2386. /* Stops the kernel queue, and halts the controller */
  2387. static int gfar_close(struct net_device *dev)
  2388. {
  2389. struct gfar_private *priv = netdev_priv(dev);
  2390. cancel_work_sync(&priv->reset_task);
  2391. stop_gfar(dev);
  2392. /* Disconnect from the PHY */
  2393. phy_disconnect(dev->phydev);
  2394. gfar_free_irq(priv);
  2395. return 0;
  2396. }
  2397. /* Clears each of the exact match registers to zero, so they
  2398. * don't interfere with normal reception
  2399. */
  2400. static void gfar_clear_exact_match(struct net_device *dev)
  2401. {
  2402. int idx;
  2403. static const u8 zero_arr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
  2404. for (idx = 1; idx < GFAR_EM_NUM + 1; idx++)
  2405. gfar_set_mac_for_addr(dev, idx, zero_arr);
  2406. }
  2407. /* Update the hash table based on the current list of multicast
  2408. * addresses we subscribe to. Also, change the promiscuity of
  2409. * the device based on the flags (this function is called
  2410. * whenever dev->flags is changed
  2411. */
  2412. static void gfar_set_multi(struct net_device *dev)
  2413. {
  2414. struct netdev_hw_addr *ha;
  2415. struct gfar_private *priv = netdev_priv(dev);
  2416. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  2417. u32 tempval;
  2418. if (dev->flags & IFF_PROMISC) {
  2419. /* Set RCTRL to PROM */
  2420. tempval = gfar_read(&regs->rctrl);
  2421. tempval |= RCTRL_PROM;
  2422. gfar_write(&regs->rctrl, tempval);
  2423. } else {
  2424. /* Set RCTRL to not PROM */
  2425. tempval = gfar_read(&regs->rctrl);
  2426. tempval &= ~(RCTRL_PROM);
  2427. gfar_write(&regs->rctrl, tempval);
  2428. }
  2429. if (dev->flags & IFF_ALLMULTI) {
  2430. /* Set the hash to rx all multicast frames */
  2431. gfar_write(&regs->igaddr0, 0xffffffff);
  2432. gfar_write(&regs->igaddr1, 0xffffffff);
  2433. gfar_write(&regs->igaddr2, 0xffffffff);
  2434. gfar_write(&regs->igaddr3, 0xffffffff);
  2435. gfar_write(&regs->igaddr4, 0xffffffff);
  2436. gfar_write(&regs->igaddr5, 0xffffffff);
  2437. gfar_write(&regs->igaddr6, 0xffffffff);
  2438. gfar_write(&regs->igaddr7, 0xffffffff);
  2439. gfar_write(&regs->gaddr0, 0xffffffff);
  2440. gfar_write(&regs->gaddr1, 0xffffffff);
  2441. gfar_write(&regs->gaddr2, 0xffffffff);
  2442. gfar_write(&regs->gaddr3, 0xffffffff);
  2443. gfar_write(&regs->gaddr4, 0xffffffff);
  2444. gfar_write(&regs->gaddr5, 0xffffffff);
  2445. gfar_write(&regs->gaddr6, 0xffffffff);
  2446. gfar_write(&regs->gaddr7, 0xffffffff);
  2447. } else {
  2448. int em_num;
  2449. int idx;
  2450. /* zero out the hash */
  2451. gfar_write(&regs->igaddr0, 0x0);
  2452. gfar_write(&regs->igaddr1, 0x0);
  2453. gfar_write(&regs->igaddr2, 0x0);
  2454. gfar_write(&regs->igaddr3, 0x0);
  2455. gfar_write(&regs->igaddr4, 0x0);
  2456. gfar_write(&regs->igaddr5, 0x0);
  2457. gfar_write(&regs->igaddr6, 0x0);
  2458. gfar_write(&regs->igaddr7, 0x0);
  2459. gfar_write(&regs->gaddr0, 0x0);
  2460. gfar_write(&regs->gaddr1, 0x0);
  2461. gfar_write(&regs->gaddr2, 0x0);
  2462. gfar_write(&regs->gaddr3, 0x0);
  2463. gfar_write(&regs->gaddr4, 0x0);
  2464. gfar_write(&regs->gaddr5, 0x0);
  2465. gfar_write(&regs->gaddr6, 0x0);
  2466. gfar_write(&regs->gaddr7, 0x0);
  2467. /* If we have extended hash tables, we need to
  2468. * clear the exact match registers to prepare for
  2469. * setting them
  2470. */
  2471. if (priv->extended_hash) {
  2472. em_num = GFAR_EM_NUM + 1;
  2473. gfar_clear_exact_match(dev);
  2474. idx = 1;
  2475. } else {
  2476. idx = 0;
  2477. em_num = 0;
  2478. }
  2479. if (netdev_mc_empty(dev))
  2480. return;
  2481. /* Parse the list, and set the appropriate bits */
  2482. netdev_for_each_mc_addr(ha, dev) {
  2483. if (idx < em_num) {
  2484. gfar_set_mac_for_addr(dev, idx, ha->addr);
  2485. idx++;
  2486. } else
  2487. gfar_set_hash_for_addr(dev, ha->addr);
  2488. }
  2489. }
  2490. }
  2491. void gfar_mac_reset(struct gfar_private *priv)
  2492. {
  2493. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  2494. u32 tempval;
  2495. /* Reset MAC layer */
  2496. gfar_write(&regs->maccfg1, MACCFG1_SOFT_RESET);
  2497. /* We need to delay at least 3 TX clocks */
  2498. udelay(3);
  2499. /* the soft reset bit is not self-resetting, so we need to
  2500. * clear it before resuming normal operation
  2501. */
  2502. gfar_write(&regs->maccfg1, 0);
  2503. udelay(3);
  2504. gfar_rx_offload_en(priv);
  2505. /* Initialize the max receive frame/buffer lengths */
  2506. gfar_write(&regs->maxfrm, GFAR_JUMBO_FRAME_SIZE);
  2507. gfar_write(&regs->mrblr, GFAR_RXB_SIZE);
  2508. /* Initialize the Minimum Frame Length Register */
  2509. gfar_write(&regs->minflr, MINFLR_INIT_SETTINGS);
  2510. /* Initialize MACCFG2. */
  2511. tempval = MACCFG2_INIT_SETTINGS;
  2512. /* eTSEC74 erratum: Rx frames of length MAXFRM or MAXFRM-1
  2513. * are marked as truncated. Avoid this by MACCFG2[Huge Frame]=1,
  2514. * and by checking RxBD[LG] and discarding larger than MAXFRM.
  2515. */
  2516. if (gfar_has_errata(priv, GFAR_ERRATA_74))
  2517. tempval |= MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK;
  2518. gfar_write(&regs->maccfg2, tempval);
  2519. /* Clear mac addr hash registers */
  2520. gfar_write(&regs->igaddr0, 0);
  2521. gfar_write(&regs->igaddr1, 0);
  2522. gfar_write(&regs->igaddr2, 0);
  2523. gfar_write(&regs->igaddr3, 0);
  2524. gfar_write(&regs->igaddr4, 0);
  2525. gfar_write(&regs->igaddr5, 0);
  2526. gfar_write(&regs->igaddr6, 0);
  2527. gfar_write(&regs->igaddr7, 0);
  2528. gfar_write(&regs->gaddr0, 0);
  2529. gfar_write(&regs->gaddr1, 0);
  2530. gfar_write(&regs->gaddr2, 0);
  2531. gfar_write(&regs->gaddr3, 0);
  2532. gfar_write(&regs->gaddr4, 0);
  2533. gfar_write(&regs->gaddr5, 0);
  2534. gfar_write(&regs->gaddr6, 0);
  2535. gfar_write(&regs->gaddr7, 0);
  2536. if (priv->extended_hash)
  2537. gfar_clear_exact_match(priv->ndev);
  2538. gfar_mac_rx_config(priv);
  2539. gfar_mac_tx_config(priv);
  2540. gfar_set_mac_address(priv->ndev);
  2541. gfar_set_multi(priv->ndev);
  2542. /* clear ievent and imask before configuring coalescing */
  2543. gfar_ints_disable(priv);
  2544. /* Configure the coalescing support */
  2545. gfar_configure_coalescing_all(priv);
  2546. }
  2547. static void gfar_hw_init(struct gfar_private *priv)
  2548. {
  2549. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  2550. u32 attrs;
  2551. /* Stop the DMA engine now, in case it was running before
  2552. * (The firmware could have used it, and left it running).
  2553. */
  2554. gfar_halt(priv);
  2555. gfar_mac_reset(priv);
  2556. /* Zero out the rmon mib registers if it has them */
  2557. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
  2558. memset_io(&regs->rmon, 0, offsetof(struct rmon_mib, car1));
  2559. /* Mask off the CAM interrupts */
  2560. gfar_write(&regs->rmon.cam1, 0xffffffff);
  2561. gfar_write(&regs->rmon.cam2, 0xffffffff);
  2562. /* Clear the CAR registers (w1c style) */
  2563. gfar_write(&regs->rmon.car1, 0xffffffff);
  2564. gfar_write(&regs->rmon.car2, 0xffffffff);
  2565. }
  2566. /* Initialize ECNTRL */
  2567. gfar_write(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
  2568. /* Set the extraction length and index */
  2569. attrs = ATTRELI_EL(priv->rx_stash_size) |
  2570. ATTRELI_EI(priv->rx_stash_index);
  2571. gfar_write(&regs->attreli, attrs);
  2572. /* Start with defaults, and add stashing
  2573. * depending on driver parameters
  2574. */
  2575. attrs = ATTR_INIT_SETTINGS;
  2576. if (priv->bd_stash_en)
  2577. attrs |= ATTR_BDSTASH;
  2578. if (priv->rx_stash_size != 0)
  2579. attrs |= ATTR_BUFSTASH;
  2580. gfar_write(&regs->attr, attrs);
  2581. /* FIFO configs */
  2582. gfar_write(&regs->fifo_tx_thr, DEFAULT_FIFO_TX_THR);
  2583. gfar_write(&regs->fifo_tx_starve, DEFAULT_FIFO_TX_STARVE);
  2584. gfar_write(&regs->fifo_tx_starve_shutoff, DEFAULT_FIFO_TX_STARVE_OFF);
  2585. /* Program the interrupt steering regs, only for MG devices */
  2586. if (priv->num_grps > 1)
  2587. gfar_write_isrg(priv);
  2588. }
  2589. static const struct net_device_ops gfar_netdev_ops = {
  2590. .ndo_open = gfar_enet_open,
  2591. .ndo_start_xmit = gfar_start_xmit,
  2592. .ndo_stop = gfar_close,
  2593. .ndo_change_mtu = gfar_change_mtu,
  2594. .ndo_set_features = gfar_set_features,
  2595. .ndo_set_rx_mode = gfar_set_multi,
  2596. .ndo_tx_timeout = gfar_timeout,
  2597. .ndo_eth_ioctl = gfar_ioctl,
  2598. .ndo_get_stats64 = gfar_get_stats64,
  2599. .ndo_change_carrier = fixed_phy_change_carrier,
  2600. .ndo_set_mac_address = gfar_set_mac_addr,
  2601. .ndo_validate_addr = eth_validate_addr,
  2602. #ifdef CONFIG_NET_POLL_CONTROLLER
  2603. .ndo_poll_controller = gfar_netpoll,
  2604. #endif
  2605. };
  2606. /* Set up the ethernet device structure, private data,
  2607. * and anything else we need before we start
  2608. */
  2609. static int gfar_probe(struct platform_device *ofdev)
  2610. {
  2611. struct device_node *np = ofdev->dev.of_node;
  2612. struct net_device *dev = NULL;
  2613. struct gfar_private *priv = NULL;
  2614. int err = 0, i;
  2615. err = gfar_of_init(ofdev, &dev);
  2616. if (err)
  2617. return err;
  2618. priv = netdev_priv(dev);
  2619. priv->ndev = dev;
  2620. priv->ofdev = ofdev;
  2621. priv->dev = &ofdev->dev;
  2622. SET_NETDEV_DEV(dev, &ofdev->dev);
  2623. INIT_WORK(&priv->reset_task, gfar_reset_task);
  2624. platform_set_drvdata(ofdev, priv);
  2625. gfar_detect_errata(priv);
  2626. /* Set the dev->base_addr to the gfar reg region */
  2627. dev->base_addr = (unsigned long) priv->gfargrp[0].regs;
  2628. /* Fill in the dev structure */
  2629. dev->watchdog_timeo = TX_TIMEOUT;
  2630. /* MTU range: 50 - 9586 */
  2631. dev->mtu = 1500;
  2632. dev->min_mtu = 50;
  2633. dev->max_mtu = GFAR_JUMBO_FRAME_SIZE - ETH_HLEN;
  2634. dev->netdev_ops = &gfar_netdev_ops;
  2635. dev->ethtool_ops = &gfar_ethtool_ops;
  2636. /* Register for napi ...We are registering NAPI for each grp */
  2637. for (i = 0; i < priv->num_grps; i++) {
  2638. netif_napi_add(dev, &priv->gfargrp[i].napi_rx,
  2639. gfar_poll_rx_sq);
  2640. netif_napi_add_tx_weight(dev, &priv->gfargrp[i].napi_tx,
  2641. gfar_poll_tx_sq, 2);
  2642. }
  2643. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
  2644. dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG |
  2645. NETIF_F_RXCSUM;
  2646. dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG |
  2647. NETIF_F_RXCSUM | NETIF_F_HIGHDMA;
  2648. }
  2649. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
  2650. dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX |
  2651. NETIF_F_HW_VLAN_CTAG_RX;
  2652. dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
  2653. }
  2654. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  2655. gfar_init_addr_hash_table(priv);
  2656. /* Insert receive time stamps into padding alignment bytes, and
  2657. * plus 2 bytes padding to ensure the cpu alignment.
  2658. */
  2659. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
  2660. priv->padding = 8 + DEFAULT_PADDING;
  2661. if (dev->features & NETIF_F_IP_CSUM ||
  2662. priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
  2663. dev->needed_headroom = GMAC_FCB_LEN + GMAC_TXPAL_LEN;
  2664. /* Initializing some of the rx/tx queue level parameters */
  2665. for (i = 0; i < priv->num_tx_queues; i++) {
  2666. priv->tx_queue[i]->tx_ring_size = DEFAULT_TX_RING_SIZE;
  2667. priv->tx_queue[i]->num_txbdfree = DEFAULT_TX_RING_SIZE;
  2668. priv->tx_queue[i]->txcoalescing = DEFAULT_TX_COALESCE;
  2669. priv->tx_queue[i]->txic = DEFAULT_TXIC;
  2670. }
  2671. for (i = 0; i < priv->num_rx_queues; i++) {
  2672. priv->rx_queue[i]->rx_ring_size = DEFAULT_RX_RING_SIZE;
  2673. priv->rx_queue[i]->rxcoalescing = DEFAULT_RX_COALESCE;
  2674. priv->rx_queue[i]->rxic = DEFAULT_RXIC;
  2675. }
  2676. /* Always enable rx filer if available */
  2677. priv->rx_filer_enable =
  2678. (priv->device_flags & FSL_GIANFAR_DEV_HAS_RX_FILER) ? 1 : 0;
  2679. /* Enable most messages by default */
  2680. priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
  2681. /* use pritority h/w tx queue scheduling for single queue devices */
  2682. if (priv->num_tx_queues == 1)
  2683. priv->prio_sched_en = 1;
  2684. set_bit(GFAR_DOWN, &priv->state);
  2685. gfar_hw_init(priv);
  2686. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
  2687. struct rmon_mib __iomem *rmon = &priv->gfargrp[0].regs->rmon;
  2688. spin_lock_init(&priv->rmon_overflow.lock);
  2689. priv->rmon_overflow.imask = IMASK_MSRO;
  2690. gfar_write(&rmon->cam1, gfar_read(&rmon->cam1) & ~CAM1_M1RDR);
  2691. }
  2692. /* Carrier starts down, phylib will bring it up */
  2693. netif_carrier_off(dev);
  2694. err = register_netdev(dev);
  2695. if (err) {
  2696. pr_err("%s: Cannot register net device, aborting\n", dev->name);
  2697. goto register_fail;
  2698. }
  2699. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET)
  2700. priv->wol_supported |= GFAR_WOL_MAGIC;
  2701. if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_WAKE_ON_FILER) &&
  2702. priv->rx_filer_enable)
  2703. priv->wol_supported |= GFAR_WOL_FILER_UCAST;
  2704. device_set_wakeup_capable(&ofdev->dev, priv->wol_supported);
  2705. /* fill out IRQ number and name fields */
  2706. for (i = 0; i < priv->num_grps; i++) {
  2707. struct gfar_priv_grp *grp = &priv->gfargrp[i];
  2708. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  2709. sprintf(gfar_irq(grp, TX)->name, "%s%s%c%s",
  2710. dev->name, "_g", '0' + i, "_tx");
  2711. sprintf(gfar_irq(grp, RX)->name, "%s%s%c%s",
  2712. dev->name, "_g", '0' + i, "_rx");
  2713. sprintf(gfar_irq(grp, ER)->name, "%s%s%c%s",
  2714. dev->name, "_g", '0' + i, "_er");
  2715. } else
  2716. strcpy(gfar_irq(grp, TX)->name, dev->name);
  2717. }
  2718. /* Initialize the filer table */
  2719. gfar_init_filer_table(priv);
  2720. /* Print out the device info */
  2721. netdev_info(dev, "mac: %pM\n", dev->dev_addr);
  2722. /* Even more device info helps when determining which kernel
  2723. * provided which set of benchmarks.
  2724. */
  2725. netdev_info(dev, "Running with NAPI enabled\n");
  2726. for (i = 0; i < priv->num_rx_queues; i++)
  2727. netdev_info(dev, "RX BD ring size for Q[%d]: %d\n",
  2728. i, priv->rx_queue[i]->rx_ring_size);
  2729. for (i = 0; i < priv->num_tx_queues; i++)
  2730. netdev_info(dev, "TX BD ring size for Q[%d]: %d\n",
  2731. i, priv->tx_queue[i]->tx_ring_size);
  2732. return 0;
  2733. register_fail:
  2734. if (of_phy_is_fixed_link(np))
  2735. of_phy_deregister_fixed_link(np);
  2736. unmap_group_regs(priv);
  2737. gfar_free_rx_queues(priv);
  2738. gfar_free_tx_queues(priv);
  2739. of_node_put(priv->phy_node);
  2740. of_node_put(priv->tbi_node);
  2741. free_gfar_dev(priv);
  2742. return err;
  2743. }
  2744. static int gfar_remove(struct platform_device *ofdev)
  2745. {
  2746. struct gfar_private *priv = platform_get_drvdata(ofdev);
  2747. struct device_node *np = ofdev->dev.of_node;
  2748. of_node_put(priv->phy_node);
  2749. of_node_put(priv->tbi_node);
  2750. unregister_netdev(priv->ndev);
  2751. if (of_phy_is_fixed_link(np))
  2752. of_phy_deregister_fixed_link(np);
  2753. unmap_group_regs(priv);
  2754. gfar_free_rx_queues(priv);
  2755. gfar_free_tx_queues(priv);
  2756. free_gfar_dev(priv);
  2757. return 0;
  2758. }
  2759. #ifdef CONFIG_PM
  2760. static void __gfar_filer_disable(struct gfar_private *priv)
  2761. {
  2762. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  2763. u32 temp;
  2764. temp = gfar_read(&regs->rctrl);
  2765. temp &= ~(RCTRL_FILREN | RCTRL_PRSDEP_INIT);
  2766. gfar_write(&regs->rctrl, temp);
  2767. }
  2768. static void __gfar_filer_enable(struct gfar_private *priv)
  2769. {
  2770. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  2771. u32 temp;
  2772. temp = gfar_read(&regs->rctrl);
  2773. temp |= RCTRL_FILREN | RCTRL_PRSDEP_INIT;
  2774. gfar_write(&regs->rctrl, temp);
  2775. }
  2776. /* Filer rules implementing wol capabilities */
  2777. static void gfar_filer_config_wol(struct gfar_private *priv)
  2778. {
  2779. unsigned int i;
  2780. u32 rqfcr;
  2781. __gfar_filer_disable(priv);
  2782. /* clear the filer table, reject any packet by default */
  2783. rqfcr = RQFCR_RJE | RQFCR_CMP_MATCH;
  2784. for (i = 0; i <= MAX_FILER_IDX; i++)
  2785. gfar_write_filer(priv, i, rqfcr, 0);
  2786. i = 0;
  2787. if (priv->wol_opts & GFAR_WOL_FILER_UCAST) {
  2788. /* unicast packet, accept it */
  2789. struct net_device *ndev = priv->ndev;
  2790. /* get the default rx queue index */
  2791. u8 qindex = (u8)priv->gfargrp[0].rx_queue->qindex;
  2792. u32 dest_mac_addr = (ndev->dev_addr[0] << 16) |
  2793. (ndev->dev_addr[1] << 8) |
  2794. ndev->dev_addr[2];
  2795. rqfcr = (qindex << 10) | RQFCR_AND |
  2796. RQFCR_CMP_EXACT | RQFCR_PID_DAH;
  2797. gfar_write_filer(priv, i++, rqfcr, dest_mac_addr);
  2798. dest_mac_addr = (ndev->dev_addr[3] << 16) |
  2799. (ndev->dev_addr[4] << 8) |
  2800. ndev->dev_addr[5];
  2801. rqfcr = (qindex << 10) | RQFCR_GPI |
  2802. RQFCR_CMP_EXACT | RQFCR_PID_DAL;
  2803. gfar_write_filer(priv, i++, rqfcr, dest_mac_addr);
  2804. }
  2805. __gfar_filer_enable(priv);
  2806. }
  2807. static void gfar_filer_restore_table(struct gfar_private *priv)
  2808. {
  2809. u32 rqfcr, rqfpr;
  2810. unsigned int i;
  2811. __gfar_filer_disable(priv);
  2812. for (i = 0; i <= MAX_FILER_IDX; i++) {
  2813. rqfcr = priv->ftp_rqfcr[i];
  2814. rqfpr = priv->ftp_rqfpr[i];
  2815. gfar_write_filer(priv, i, rqfcr, rqfpr);
  2816. }
  2817. __gfar_filer_enable(priv);
  2818. }
  2819. /* gfar_start() for Rx only and with the FGPI filer interrupt enabled */
  2820. static void gfar_start_wol_filer(struct gfar_private *priv)
  2821. {
  2822. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  2823. u32 tempval;
  2824. int i = 0;
  2825. /* Enable Rx hw queues */
  2826. gfar_write(&regs->rqueue, priv->rqueue);
  2827. /* Initialize DMACTRL to have WWR and WOP */
  2828. tempval = gfar_read(&regs->dmactrl);
  2829. tempval |= DMACTRL_INIT_SETTINGS;
  2830. gfar_write(&regs->dmactrl, tempval);
  2831. /* Make sure we aren't stopped */
  2832. tempval = gfar_read(&regs->dmactrl);
  2833. tempval &= ~DMACTRL_GRS;
  2834. gfar_write(&regs->dmactrl, tempval);
  2835. for (i = 0; i < priv->num_grps; i++) {
  2836. regs = priv->gfargrp[i].regs;
  2837. /* Clear RHLT, so that the DMA starts polling now */
  2838. gfar_write(&regs->rstat, priv->gfargrp[i].rstat);
  2839. /* enable the Filer General Purpose Interrupt */
  2840. gfar_write(&regs->imask, IMASK_FGPI);
  2841. }
  2842. /* Enable Rx DMA */
  2843. tempval = gfar_read(&regs->maccfg1);
  2844. tempval |= MACCFG1_RX_EN;
  2845. gfar_write(&regs->maccfg1, tempval);
  2846. }
  2847. static int gfar_suspend(struct device *dev)
  2848. {
  2849. struct gfar_private *priv = dev_get_drvdata(dev);
  2850. struct net_device *ndev = priv->ndev;
  2851. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  2852. u32 tempval;
  2853. u16 wol = priv->wol_opts;
  2854. if (!netif_running(ndev))
  2855. return 0;
  2856. disable_napi(priv);
  2857. netif_tx_lock(ndev);
  2858. netif_device_detach(ndev);
  2859. netif_tx_unlock(ndev);
  2860. gfar_halt(priv);
  2861. if (wol & GFAR_WOL_MAGIC) {
  2862. /* Enable interrupt on Magic Packet */
  2863. gfar_write(&regs->imask, IMASK_MAG);
  2864. /* Enable Magic Packet mode */
  2865. tempval = gfar_read(&regs->maccfg2);
  2866. tempval |= MACCFG2_MPEN;
  2867. gfar_write(&regs->maccfg2, tempval);
  2868. /* re-enable the Rx block */
  2869. tempval = gfar_read(&regs->maccfg1);
  2870. tempval |= MACCFG1_RX_EN;
  2871. gfar_write(&regs->maccfg1, tempval);
  2872. } else if (wol & GFAR_WOL_FILER_UCAST) {
  2873. gfar_filer_config_wol(priv);
  2874. gfar_start_wol_filer(priv);
  2875. } else {
  2876. phy_stop(ndev->phydev);
  2877. }
  2878. return 0;
  2879. }
  2880. static int gfar_resume(struct device *dev)
  2881. {
  2882. struct gfar_private *priv = dev_get_drvdata(dev);
  2883. struct net_device *ndev = priv->ndev;
  2884. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  2885. u32 tempval;
  2886. u16 wol = priv->wol_opts;
  2887. if (!netif_running(ndev))
  2888. return 0;
  2889. if (wol & GFAR_WOL_MAGIC) {
  2890. /* Disable Magic Packet mode */
  2891. tempval = gfar_read(&regs->maccfg2);
  2892. tempval &= ~MACCFG2_MPEN;
  2893. gfar_write(&regs->maccfg2, tempval);
  2894. } else if (wol & GFAR_WOL_FILER_UCAST) {
  2895. /* need to stop rx only, tx is already down */
  2896. gfar_halt(priv);
  2897. gfar_filer_restore_table(priv);
  2898. } else {
  2899. phy_start(ndev->phydev);
  2900. }
  2901. gfar_start(priv);
  2902. netif_device_attach(ndev);
  2903. enable_napi(priv);
  2904. return 0;
  2905. }
  2906. static int gfar_restore(struct device *dev)
  2907. {
  2908. struct gfar_private *priv = dev_get_drvdata(dev);
  2909. struct net_device *ndev = priv->ndev;
  2910. if (!netif_running(ndev)) {
  2911. netif_device_attach(ndev);
  2912. return 0;
  2913. }
  2914. gfar_init_bds(ndev);
  2915. gfar_mac_reset(priv);
  2916. gfar_init_tx_rx_base(priv);
  2917. gfar_start(priv);
  2918. priv->oldlink = 0;
  2919. priv->oldspeed = 0;
  2920. priv->oldduplex = -1;
  2921. if (ndev->phydev)
  2922. phy_start(ndev->phydev);
  2923. netif_device_attach(ndev);
  2924. enable_napi(priv);
  2925. return 0;
  2926. }
  2927. static const struct dev_pm_ops gfar_pm_ops = {
  2928. .suspend = gfar_suspend,
  2929. .resume = gfar_resume,
  2930. .freeze = gfar_suspend,
  2931. .thaw = gfar_resume,
  2932. .restore = gfar_restore,
  2933. };
  2934. #define GFAR_PM_OPS (&gfar_pm_ops)
  2935. #else
  2936. #define GFAR_PM_OPS NULL
  2937. #endif
  2938. static const struct of_device_id gfar_match[] =
  2939. {
  2940. {
  2941. .type = "network",
  2942. .compatible = "gianfar",
  2943. },
  2944. {
  2945. .compatible = "fsl,etsec2",
  2946. },
  2947. {},
  2948. };
  2949. MODULE_DEVICE_TABLE(of, gfar_match);
  2950. /* Structure for a device driver */
  2951. static struct platform_driver gfar_driver = {
  2952. .driver = {
  2953. .name = "fsl-gianfar",
  2954. .pm = GFAR_PM_OPS,
  2955. .of_match_table = gfar_match,
  2956. },
  2957. .probe = gfar_probe,
  2958. .remove = gfar_remove,
  2959. };
  2960. module_platform_driver(gfar_driver);