gianfar.c 97 KB

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