gianfar.c 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795
  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 phy_device *phydev = dev->phydev;
  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 (!phydev)
  847. return -ENODEV;
  848. return phy_mii_ioctl(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 device_node *np = ofdev->dev.of_node;
  1088. struct net_device *dev = NULL;
  1089. struct gfar_private *priv = NULL;
  1090. int err = 0, i;
  1091. err = gfar_of_init(ofdev, &dev);
  1092. if (err)
  1093. return err;
  1094. priv = netdev_priv(dev);
  1095. priv->ndev = dev;
  1096. priv->ofdev = ofdev;
  1097. priv->dev = &ofdev->dev;
  1098. SET_NETDEV_DEV(dev, &ofdev->dev);
  1099. INIT_WORK(&priv->reset_task, gfar_reset_task);
  1100. platform_set_drvdata(ofdev, priv);
  1101. gfar_detect_errata(priv);
  1102. /* Set the dev->base_addr to the gfar reg region */
  1103. dev->base_addr = (unsigned long) priv->gfargrp[0].regs;
  1104. /* Fill in the dev structure */
  1105. dev->watchdog_timeo = TX_TIMEOUT;
  1106. dev->mtu = 1500;
  1107. dev->netdev_ops = &gfar_netdev_ops;
  1108. dev->ethtool_ops = &gfar_ethtool_ops;
  1109. /* Register for napi ...We are registering NAPI for each grp */
  1110. for (i = 0; i < priv->num_grps; i++) {
  1111. if (priv->poll_mode == GFAR_SQ_POLLING) {
  1112. netif_napi_add(dev, &priv->gfargrp[i].napi_rx,
  1113. gfar_poll_rx_sq, GFAR_DEV_WEIGHT);
  1114. netif_tx_napi_add(dev, &priv->gfargrp[i].napi_tx,
  1115. gfar_poll_tx_sq, 2);
  1116. } else {
  1117. netif_napi_add(dev, &priv->gfargrp[i].napi_rx,
  1118. gfar_poll_rx, GFAR_DEV_WEIGHT);
  1119. netif_tx_napi_add(dev, &priv->gfargrp[i].napi_tx,
  1120. gfar_poll_tx, 2);
  1121. }
  1122. }
  1123. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
  1124. dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG |
  1125. NETIF_F_RXCSUM;
  1126. dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG |
  1127. NETIF_F_RXCSUM | NETIF_F_HIGHDMA;
  1128. }
  1129. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
  1130. dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX |
  1131. NETIF_F_HW_VLAN_CTAG_RX;
  1132. dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
  1133. }
  1134. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1135. gfar_init_addr_hash_table(priv);
  1136. /* Insert receive time stamps into padding alignment bytes */
  1137. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
  1138. priv->padding = 8;
  1139. if (dev->features & NETIF_F_IP_CSUM ||
  1140. priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
  1141. dev->needed_headroom = GMAC_FCB_LEN;
  1142. /* Initializing some of the rx/tx queue level parameters */
  1143. for (i = 0; i < priv->num_tx_queues; i++) {
  1144. priv->tx_queue[i]->tx_ring_size = DEFAULT_TX_RING_SIZE;
  1145. priv->tx_queue[i]->num_txbdfree = DEFAULT_TX_RING_SIZE;
  1146. priv->tx_queue[i]->txcoalescing = DEFAULT_TX_COALESCE;
  1147. priv->tx_queue[i]->txic = DEFAULT_TXIC;
  1148. }
  1149. for (i = 0; i < priv->num_rx_queues; i++) {
  1150. priv->rx_queue[i]->rx_ring_size = DEFAULT_RX_RING_SIZE;
  1151. priv->rx_queue[i]->rxcoalescing = DEFAULT_RX_COALESCE;
  1152. priv->rx_queue[i]->rxic = DEFAULT_RXIC;
  1153. }
  1154. /* Always enable rx filer if available */
  1155. priv->rx_filer_enable =
  1156. (priv->device_flags & FSL_GIANFAR_DEV_HAS_RX_FILER) ? 1 : 0;
  1157. /* Enable most messages by default */
  1158. priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
  1159. /* use pritority h/w tx queue scheduling for single queue devices */
  1160. if (priv->num_tx_queues == 1)
  1161. priv->prio_sched_en = 1;
  1162. set_bit(GFAR_DOWN, &priv->state);
  1163. gfar_hw_init(priv);
  1164. /* Carrier starts down, phylib will bring it up */
  1165. netif_carrier_off(dev);
  1166. err = register_netdev(dev);
  1167. if (err) {
  1168. pr_err("%s: Cannot register net device, aborting\n", dev->name);
  1169. goto register_fail;
  1170. }
  1171. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET)
  1172. priv->wol_supported |= GFAR_WOL_MAGIC;
  1173. if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_WAKE_ON_FILER) &&
  1174. priv->rx_filer_enable)
  1175. priv->wol_supported |= GFAR_WOL_FILER_UCAST;
  1176. device_set_wakeup_capable(&ofdev->dev, priv->wol_supported);
  1177. /* fill out IRQ number and name fields */
  1178. for (i = 0; i < priv->num_grps; i++) {
  1179. struct gfar_priv_grp *grp = &priv->gfargrp[i];
  1180. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  1181. sprintf(gfar_irq(grp, TX)->name, "%s%s%c%s",
  1182. dev->name, "_g", '0' + i, "_tx");
  1183. sprintf(gfar_irq(grp, RX)->name, "%s%s%c%s",
  1184. dev->name, "_g", '0' + i, "_rx");
  1185. sprintf(gfar_irq(grp, ER)->name, "%s%s%c%s",
  1186. dev->name, "_g", '0' + i, "_er");
  1187. } else
  1188. strcpy(gfar_irq(grp, TX)->name, dev->name);
  1189. }
  1190. /* Initialize the filer table */
  1191. gfar_init_filer_table(priv);
  1192. /* Print out the device info */
  1193. netdev_info(dev, "mac: %pM\n", dev->dev_addr);
  1194. /* Even more device info helps when determining which kernel
  1195. * provided which set of benchmarks.
  1196. */
  1197. netdev_info(dev, "Running with NAPI enabled\n");
  1198. for (i = 0; i < priv->num_rx_queues; i++)
  1199. netdev_info(dev, "RX BD ring size for Q[%d]: %d\n",
  1200. i, priv->rx_queue[i]->rx_ring_size);
  1201. for (i = 0; i < priv->num_tx_queues; i++)
  1202. netdev_info(dev, "TX BD ring size for Q[%d]: %d\n",
  1203. i, priv->tx_queue[i]->tx_ring_size);
  1204. return 0;
  1205. register_fail:
  1206. if (of_phy_is_fixed_link(np))
  1207. of_phy_deregister_fixed_link(np);
  1208. unmap_group_regs(priv);
  1209. gfar_free_rx_queues(priv);
  1210. gfar_free_tx_queues(priv);
  1211. of_node_put(priv->phy_node);
  1212. of_node_put(priv->tbi_node);
  1213. free_gfar_dev(priv);
  1214. return err;
  1215. }
  1216. static int gfar_remove(struct platform_device *ofdev)
  1217. {
  1218. struct gfar_private *priv = platform_get_drvdata(ofdev);
  1219. struct device_node *np = ofdev->dev.of_node;
  1220. of_node_put(priv->phy_node);
  1221. of_node_put(priv->tbi_node);
  1222. unregister_netdev(priv->ndev);
  1223. if (of_phy_is_fixed_link(np))
  1224. of_phy_deregister_fixed_link(np);
  1225. unmap_group_regs(priv);
  1226. gfar_free_rx_queues(priv);
  1227. gfar_free_tx_queues(priv);
  1228. free_gfar_dev(priv);
  1229. return 0;
  1230. }
  1231. #ifdef CONFIG_PM
  1232. static void __gfar_filer_disable(struct gfar_private *priv)
  1233. {
  1234. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1235. u32 temp;
  1236. temp = gfar_read(&regs->rctrl);
  1237. temp &= ~(RCTRL_FILREN | RCTRL_PRSDEP_INIT);
  1238. gfar_write(&regs->rctrl, temp);
  1239. }
  1240. static void __gfar_filer_enable(struct gfar_private *priv)
  1241. {
  1242. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1243. u32 temp;
  1244. temp = gfar_read(&regs->rctrl);
  1245. temp |= RCTRL_FILREN | RCTRL_PRSDEP_INIT;
  1246. gfar_write(&regs->rctrl, temp);
  1247. }
  1248. /* Filer rules implementing wol capabilities */
  1249. static void gfar_filer_config_wol(struct gfar_private *priv)
  1250. {
  1251. unsigned int i;
  1252. u32 rqfcr;
  1253. __gfar_filer_disable(priv);
  1254. /* clear the filer table, reject any packet by default */
  1255. rqfcr = RQFCR_RJE | RQFCR_CMP_MATCH;
  1256. for (i = 0; i <= MAX_FILER_IDX; i++)
  1257. gfar_write_filer(priv, i, rqfcr, 0);
  1258. i = 0;
  1259. if (priv->wol_opts & GFAR_WOL_FILER_UCAST) {
  1260. /* unicast packet, accept it */
  1261. struct net_device *ndev = priv->ndev;
  1262. /* get the default rx queue index */
  1263. u8 qindex = (u8)priv->gfargrp[0].rx_queue->qindex;
  1264. u32 dest_mac_addr = (ndev->dev_addr[0] << 16) |
  1265. (ndev->dev_addr[1] << 8) |
  1266. ndev->dev_addr[2];
  1267. rqfcr = (qindex << 10) | RQFCR_AND |
  1268. RQFCR_CMP_EXACT | RQFCR_PID_DAH;
  1269. gfar_write_filer(priv, i++, rqfcr, dest_mac_addr);
  1270. dest_mac_addr = (ndev->dev_addr[3] << 16) |
  1271. (ndev->dev_addr[4] << 8) |
  1272. ndev->dev_addr[5];
  1273. rqfcr = (qindex << 10) | RQFCR_GPI |
  1274. RQFCR_CMP_EXACT | RQFCR_PID_DAL;
  1275. gfar_write_filer(priv, i++, rqfcr, dest_mac_addr);
  1276. }
  1277. __gfar_filer_enable(priv);
  1278. }
  1279. static void gfar_filer_restore_table(struct gfar_private *priv)
  1280. {
  1281. u32 rqfcr, rqfpr;
  1282. unsigned int i;
  1283. __gfar_filer_disable(priv);
  1284. for (i = 0; i <= MAX_FILER_IDX; i++) {
  1285. rqfcr = priv->ftp_rqfcr[i];
  1286. rqfpr = priv->ftp_rqfpr[i];
  1287. gfar_write_filer(priv, i, rqfcr, rqfpr);
  1288. }
  1289. __gfar_filer_enable(priv);
  1290. }
  1291. /* gfar_start() for Rx only and with the FGPI filer interrupt enabled */
  1292. static void gfar_start_wol_filer(struct gfar_private *priv)
  1293. {
  1294. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1295. u32 tempval;
  1296. int i = 0;
  1297. /* Enable Rx hw queues */
  1298. gfar_write(&regs->rqueue, priv->rqueue);
  1299. /* Initialize DMACTRL to have WWR and WOP */
  1300. tempval = gfar_read(&regs->dmactrl);
  1301. tempval |= DMACTRL_INIT_SETTINGS;
  1302. gfar_write(&regs->dmactrl, tempval);
  1303. /* Make sure we aren't stopped */
  1304. tempval = gfar_read(&regs->dmactrl);
  1305. tempval &= ~DMACTRL_GRS;
  1306. gfar_write(&regs->dmactrl, tempval);
  1307. for (i = 0; i < priv->num_grps; i++) {
  1308. regs = priv->gfargrp[i].regs;
  1309. /* Clear RHLT, so that the DMA starts polling now */
  1310. gfar_write(&regs->rstat, priv->gfargrp[i].rstat);
  1311. /* enable the Filer General Purpose Interrupt */
  1312. gfar_write(&regs->imask, IMASK_FGPI);
  1313. }
  1314. /* Enable Rx DMA */
  1315. tempval = gfar_read(&regs->maccfg1);
  1316. tempval |= MACCFG1_RX_EN;
  1317. gfar_write(&regs->maccfg1, tempval);
  1318. }
  1319. static int gfar_suspend(struct device *dev)
  1320. {
  1321. struct gfar_private *priv = dev_get_drvdata(dev);
  1322. struct net_device *ndev = priv->ndev;
  1323. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1324. u32 tempval;
  1325. u16 wol = priv->wol_opts;
  1326. if (!netif_running(ndev))
  1327. return 0;
  1328. disable_napi(priv);
  1329. netif_tx_lock(ndev);
  1330. netif_device_detach(ndev);
  1331. netif_tx_unlock(ndev);
  1332. gfar_halt(priv);
  1333. if (wol & GFAR_WOL_MAGIC) {
  1334. /* Enable interrupt on Magic Packet */
  1335. gfar_write(&regs->imask, IMASK_MAG);
  1336. /* Enable Magic Packet mode */
  1337. tempval = gfar_read(&regs->maccfg2);
  1338. tempval |= MACCFG2_MPEN;
  1339. gfar_write(&regs->maccfg2, tempval);
  1340. /* re-enable the Rx block */
  1341. tempval = gfar_read(&regs->maccfg1);
  1342. tempval |= MACCFG1_RX_EN;
  1343. gfar_write(&regs->maccfg1, tempval);
  1344. } else if (wol & GFAR_WOL_FILER_UCAST) {
  1345. gfar_filer_config_wol(priv);
  1346. gfar_start_wol_filer(priv);
  1347. } else {
  1348. phy_stop(ndev->phydev);
  1349. }
  1350. return 0;
  1351. }
  1352. static int gfar_resume(struct device *dev)
  1353. {
  1354. struct gfar_private *priv = dev_get_drvdata(dev);
  1355. struct net_device *ndev = priv->ndev;
  1356. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1357. u32 tempval;
  1358. u16 wol = priv->wol_opts;
  1359. if (!netif_running(ndev))
  1360. return 0;
  1361. if (wol & GFAR_WOL_MAGIC) {
  1362. /* Disable Magic Packet mode */
  1363. tempval = gfar_read(&regs->maccfg2);
  1364. tempval &= ~MACCFG2_MPEN;
  1365. gfar_write(&regs->maccfg2, tempval);
  1366. } else if (wol & GFAR_WOL_FILER_UCAST) {
  1367. /* need to stop rx only, tx is already down */
  1368. gfar_halt(priv);
  1369. gfar_filer_restore_table(priv);
  1370. } else {
  1371. phy_start(ndev->phydev);
  1372. }
  1373. gfar_start(priv);
  1374. netif_device_attach(ndev);
  1375. enable_napi(priv);
  1376. return 0;
  1377. }
  1378. static int gfar_restore(struct device *dev)
  1379. {
  1380. struct gfar_private *priv = dev_get_drvdata(dev);
  1381. struct net_device *ndev = priv->ndev;
  1382. if (!netif_running(ndev)) {
  1383. netif_device_attach(ndev);
  1384. return 0;
  1385. }
  1386. gfar_init_bds(ndev);
  1387. gfar_mac_reset(priv);
  1388. gfar_init_tx_rx_base(priv);
  1389. gfar_start(priv);
  1390. priv->oldlink = 0;
  1391. priv->oldspeed = 0;
  1392. priv->oldduplex = -1;
  1393. if (ndev->phydev)
  1394. phy_start(ndev->phydev);
  1395. netif_device_attach(ndev);
  1396. enable_napi(priv);
  1397. return 0;
  1398. }
  1399. static struct dev_pm_ops gfar_pm_ops = {
  1400. .suspend = gfar_suspend,
  1401. .resume = gfar_resume,
  1402. .freeze = gfar_suspend,
  1403. .thaw = gfar_resume,
  1404. .restore = gfar_restore,
  1405. };
  1406. #define GFAR_PM_OPS (&gfar_pm_ops)
  1407. #else
  1408. #define GFAR_PM_OPS NULL
  1409. #endif
  1410. /* Reads the controller's registers to determine what interface
  1411. * connects it to the PHY.
  1412. */
  1413. static phy_interface_t gfar_get_interface(struct net_device *dev)
  1414. {
  1415. struct gfar_private *priv = netdev_priv(dev);
  1416. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1417. u32 ecntrl;
  1418. ecntrl = gfar_read(&regs->ecntrl);
  1419. if (ecntrl & ECNTRL_SGMII_MODE)
  1420. return PHY_INTERFACE_MODE_SGMII;
  1421. if (ecntrl & ECNTRL_TBI_MODE) {
  1422. if (ecntrl & ECNTRL_REDUCED_MODE)
  1423. return PHY_INTERFACE_MODE_RTBI;
  1424. else
  1425. return PHY_INTERFACE_MODE_TBI;
  1426. }
  1427. if (ecntrl & ECNTRL_REDUCED_MODE) {
  1428. if (ecntrl & ECNTRL_REDUCED_MII_MODE) {
  1429. return PHY_INTERFACE_MODE_RMII;
  1430. }
  1431. else {
  1432. phy_interface_t interface = priv->interface;
  1433. /* This isn't autodetected right now, so it must
  1434. * be set by the device tree or platform code.
  1435. */
  1436. if (interface == PHY_INTERFACE_MODE_RGMII_ID)
  1437. return PHY_INTERFACE_MODE_RGMII_ID;
  1438. return PHY_INTERFACE_MODE_RGMII;
  1439. }
  1440. }
  1441. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
  1442. return PHY_INTERFACE_MODE_GMII;
  1443. return PHY_INTERFACE_MODE_MII;
  1444. }
  1445. /* Initializes driver's PHY state, and attaches to the PHY.
  1446. * Returns 0 on success.
  1447. */
  1448. static int init_phy(struct net_device *dev)
  1449. {
  1450. struct gfar_private *priv = netdev_priv(dev);
  1451. uint gigabit_support =
  1452. priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
  1453. GFAR_SUPPORTED_GBIT : 0;
  1454. phy_interface_t interface;
  1455. struct phy_device *phydev;
  1456. priv->oldlink = 0;
  1457. priv->oldspeed = 0;
  1458. priv->oldduplex = -1;
  1459. interface = gfar_get_interface(dev);
  1460. phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
  1461. interface);
  1462. if (!phydev) {
  1463. dev_err(&dev->dev, "could not attach to PHY\n");
  1464. return -ENODEV;
  1465. }
  1466. if (interface == PHY_INTERFACE_MODE_SGMII)
  1467. gfar_configure_serdes(dev);
  1468. /* Remove any features not supported by the controller */
  1469. phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
  1470. phydev->advertising = phydev->supported;
  1471. /* Add support for flow control, but don't advertise it by default */
  1472. phydev->supported |= (SUPPORTED_Pause | SUPPORTED_Asym_Pause);
  1473. return 0;
  1474. }
  1475. /* Initialize TBI PHY interface for communicating with the
  1476. * SERDES lynx PHY on the chip. We communicate with this PHY
  1477. * through the MDIO bus on each controller, treating it as a
  1478. * "normal" PHY at the address found in the TBIPA register. We assume
  1479. * that the TBIPA register is valid. Either the MDIO bus code will set
  1480. * it to a value that doesn't conflict with other PHYs on the bus, or the
  1481. * value doesn't matter, as there are no other PHYs on the bus.
  1482. */
  1483. static void gfar_configure_serdes(struct net_device *dev)
  1484. {
  1485. struct gfar_private *priv = netdev_priv(dev);
  1486. struct phy_device *tbiphy;
  1487. if (!priv->tbi_node) {
  1488. dev_warn(&dev->dev, "error: SGMII mode requires that the "
  1489. "device tree specify a tbi-handle\n");
  1490. return;
  1491. }
  1492. tbiphy = of_phy_find_device(priv->tbi_node);
  1493. if (!tbiphy) {
  1494. dev_err(&dev->dev, "error: Could not get TBI device\n");
  1495. return;
  1496. }
  1497. /* If the link is already up, we must already be ok, and don't need to
  1498. * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
  1499. * everything for us? Resetting it takes the link down and requires
  1500. * several seconds for it to come back.
  1501. */
  1502. if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS) {
  1503. put_device(&tbiphy->mdio.dev);
  1504. return;
  1505. }
  1506. /* Single clk mode, mii mode off(for serdes communication) */
  1507. phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
  1508. phy_write(tbiphy, MII_ADVERTISE,
  1509. ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
  1510. ADVERTISE_1000XPSE_ASYM);
  1511. phy_write(tbiphy, MII_BMCR,
  1512. BMCR_ANENABLE | BMCR_ANRESTART | BMCR_FULLDPLX |
  1513. BMCR_SPEED1000);
  1514. put_device(&tbiphy->mdio.dev);
  1515. }
  1516. static int __gfar_is_rx_idle(struct gfar_private *priv)
  1517. {
  1518. u32 res;
  1519. /* Normaly TSEC should not hang on GRS commands, so we should
  1520. * actually wait for IEVENT_GRSC flag.
  1521. */
  1522. if (!gfar_has_errata(priv, GFAR_ERRATA_A002))
  1523. return 0;
  1524. /* Read the eTSEC register at offset 0xD1C. If bits 7-14 are
  1525. * the same as bits 23-30, the eTSEC Rx is assumed to be idle
  1526. * and the Rx can be safely reset.
  1527. */
  1528. res = gfar_read((void __iomem *)priv->gfargrp[0].regs + 0xd1c);
  1529. res &= 0x7f807f80;
  1530. if ((res & 0xffff) == (res >> 16))
  1531. return 1;
  1532. return 0;
  1533. }
  1534. /* Halt the receive and transmit queues */
  1535. static void gfar_halt_nodisable(struct gfar_private *priv)
  1536. {
  1537. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1538. u32 tempval;
  1539. unsigned int timeout;
  1540. int stopped;
  1541. gfar_ints_disable(priv);
  1542. if (gfar_is_dma_stopped(priv))
  1543. return;
  1544. /* Stop the DMA, and wait for it to stop */
  1545. tempval = gfar_read(&regs->dmactrl);
  1546. tempval |= (DMACTRL_GRS | DMACTRL_GTS);
  1547. gfar_write(&regs->dmactrl, tempval);
  1548. retry:
  1549. timeout = 1000;
  1550. while (!(stopped = gfar_is_dma_stopped(priv)) && timeout) {
  1551. cpu_relax();
  1552. timeout--;
  1553. }
  1554. if (!timeout)
  1555. stopped = gfar_is_dma_stopped(priv);
  1556. if (!stopped && !gfar_is_rx_dma_stopped(priv) &&
  1557. !__gfar_is_rx_idle(priv))
  1558. goto retry;
  1559. }
  1560. /* Halt the receive and transmit queues */
  1561. void gfar_halt(struct gfar_private *priv)
  1562. {
  1563. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1564. u32 tempval;
  1565. /* Dissable the Rx/Tx hw queues */
  1566. gfar_write(&regs->rqueue, 0);
  1567. gfar_write(&regs->tqueue, 0);
  1568. mdelay(10);
  1569. gfar_halt_nodisable(priv);
  1570. /* Disable Rx/Tx DMA */
  1571. tempval = gfar_read(&regs->maccfg1);
  1572. tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
  1573. gfar_write(&regs->maccfg1, tempval);
  1574. }
  1575. void stop_gfar(struct net_device *dev)
  1576. {
  1577. struct gfar_private *priv = netdev_priv(dev);
  1578. netif_tx_stop_all_queues(dev);
  1579. smp_mb__before_atomic();
  1580. set_bit(GFAR_DOWN, &priv->state);
  1581. smp_mb__after_atomic();
  1582. disable_napi(priv);
  1583. /* disable ints and gracefully shut down Rx/Tx DMA */
  1584. gfar_halt(priv);
  1585. phy_stop(dev->phydev);
  1586. free_skb_resources(priv);
  1587. }
  1588. static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue)
  1589. {
  1590. struct txbd8 *txbdp;
  1591. struct gfar_private *priv = netdev_priv(tx_queue->dev);
  1592. int i, j;
  1593. txbdp = tx_queue->tx_bd_base;
  1594. for (i = 0; i < tx_queue->tx_ring_size; i++) {
  1595. if (!tx_queue->tx_skbuff[i])
  1596. continue;
  1597. dma_unmap_single(priv->dev, be32_to_cpu(txbdp->bufPtr),
  1598. be16_to_cpu(txbdp->length), DMA_TO_DEVICE);
  1599. txbdp->lstatus = 0;
  1600. for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags;
  1601. j++) {
  1602. txbdp++;
  1603. dma_unmap_page(priv->dev, be32_to_cpu(txbdp->bufPtr),
  1604. be16_to_cpu(txbdp->length),
  1605. DMA_TO_DEVICE);
  1606. }
  1607. txbdp++;
  1608. dev_kfree_skb_any(tx_queue->tx_skbuff[i]);
  1609. tx_queue->tx_skbuff[i] = NULL;
  1610. }
  1611. kfree(tx_queue->tx_skbuff);
  1612. tx_queue->tx_skbuff = NULL;
  1613. }
  1614. static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue)
  1615. {
  1616. int i;
  1617. struct rxbd8 *rxbdp = rx_queue->rx_bd_base;
  1618. if (rx_queue->skb)
  1619. dev_kfree_skb(rx_queue->skb);
  1620. for (i = 0; i < rx_queue->rx_ring_size; i++) {
  1621. struct gfar_rx_buff *rxb = &rx_queue->rx_buff[i];
  1622. rxbdp->lstatus = 0;
  1623. rxbdp->bufPtr = 0;
  1624. rxbdp++;
  1625. if (!rxb->page)
  1626. continue;
  1627. dma_unmap_page(rx_queue->dev, rxb->dma,
  1628. PAGE_SIZE, DMA_FROM_DEVICE);
  1629. __free_page(rxb->page);
  1630. rxb->page = NULL;
  1631. }
  1632. kfree(rx_queue->rx_buff);
  1633. rx_queue->rx_buff = NULL;
  1634. }
  1635. /* If there are any tx skbs or rx skbs still around, free them.
  1636. * Then free tx_skbuff and rx_skbuff
  1637. */
  1638. static void free_skb_resources(struct gfar_private *priv)
  1639. {
  1640. struct gfar_priv_tx_q *tx_queue = NULL;
  1641. struct gfar_priv_rx_q *rx_queue = NULL;
  1642. int i;
  1643. /* Go through all the buffer descriptors and free their data buffers */
  1644. for (i = 0; i < priv->num_tx_queues; i++) {
  1645. struct netdev_queue *txq;
  1646. tx_queue = priv->tx_queue[i];
  1647. txq = netdev_get_tx_queue(tx_queue->dev, tx_queue->qindex);
  1648. if (tx_queue->tx_skbuff)
  1649. free_skb_tx_queue(tx_queue);
  1650. netdev_tx_reset_queue(txq);
  1651. }
  1652. for (i = 0; i < priv->num_rx_queues; i++) {
  1653. rx_queue = priv->rx_queue[i];
  1654. if (rx_queue->rx_buff)
  1655. free_skb_rx_queue(rx_queue);
  1656. }
  1657. dma_free_coherent(priv->dev,
  1658. sizeof(struct txbd8) * priv->total_tx_ring_size +
  1659. sizeof(struct rxbd8) * priv->total_rx_ring_size,
  1660. priv->tx_queue[0]->tx_bd_base,
  1661. priv->tx_queue[0]->tx_bd_dma_base);
  1662. }
  1663. void gfar_start(struct gfar_private *priv)
  1664. {
  1665. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1666. u32 tempval;
  1667. int i = 0;
  1668. /* Enable Rx/Tx hw queues */
  1669. gfar_write(&regs->rqueue, priv->rqueue);
  1670. gfar_write(&regs->tqueue, priv->tqueue);
  1671. /* Initialize DMACTRL to have WWR and WOP */
  1672. tempval = gfar_read(&regs->dmactrl);
  1673. tempval |= DMACTRL_INIT_SETTINGS;
  1674. gfar_write(&regs->dmactrl, tempval);
  1675. /* Make sure we aren't stopped */
  1676. tempval = gfar_read(&regs->dmactrl);
  1677. tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
  1678. gfar_write(&regs->dmactrl, tempval);
  1679. for (i = 0; i < priv->num_grps; i++) {
  1680. regs = priv->gfargrp[i].regs;
  1681. /* Clear THLT/RHLT, so that the DMA starts polling now */
  1682. gfar_write(&regs->tstat, priv->gfargrp[i].tstat);
  1683. gfar_write(&regs->rstat, priv->gfargrp[i].rstat);
  1684. }
  1685. /* Enable Rx/Tx DMA */
  1686. tempval = gfar_read(&regs->maccfg1);
  1687. tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
  1688. gfar_write(&regs->maccfg1, tempval);
  1689. gfar_ints_enable(priv);
  1690. netif_trans_update(priv->ndev); /* prevent tx timeout */
  1691. }
  1692. static void free_grp_irqs(struct gfar_priv_grp *grp)
  1693. {
  1694. free_irq(gfar_irq(grp, TX)->irq, grp);
  1695. free_irq(gfar_irq(grp, RX)->irq, grp);
  1696. free_irq(gfar_irq(grp, ER)->irq, grp);
  1697. }
  1698. static int register_grp_irqs(struct gfar_priv_grp *grp)
  1699. {
  1700. struct gfar_private *priv = grp->priv;
  1701. struct net_device *dev = priv->ndev;
  1702. int err;
  1703. /* If the device has multiple interrupts, register for
  1704. * them. Otherwise, only register for the one
  1705. */
  1706. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  1707. /* Install our interrupt handlers for Error,
  1708. * Transmit, and Receive
  1709. */
  1710. err = request_irq(gfar_irq(grp, ER)->irq, gfar_error, 0,
  1711. gfar_irq(grp, ER)->name, grp);
  1712. if (err < 0) {
  1713. netif_err(priv, intr, dev, "Can't get IRQ %d\n",
  1714. gfar_irq(grp, ER)->irq);
  1715. goto err_irq_fail;
  1716. }
  1717. enable_irq_wake(gfar_irq(grp, ER)->irq);
  1718. err = request_irq(gfar_irq(grp, TX)->irq, gfar_transmit, 0,
  1719. gfar_irq(grp, TX)->name, grp);
  1720. if (err < 0) {
  1721. netif_err(priv, intr, dev, "Can't get IRQ %d\n",
  1722. gfar_irq(grp, TX)->irq);
  1723. goto tx_irq_fail;
  1724. }
  1725. err = request_irq(gfar_irq(grp, RX)->irq, gfar_receive, 0,
  1726. gfar_irq(grp, RX)->name, grp);
  1727. if (err < 0) {
  1728. netif_err(priv, intr, dev, "Can't get IRQ %d\n",
  1729. gfar_irq(grp, RX)->irq);
  1730. goto rx_irq_fail;
  1731. }
  1732. enable_irq_wake(gfar_irq(grp, RX)->irq);
  1733. } else {
  1734. err = request_irq(gfar_irq(grp, TX)->irq, gfar_interrupt, 0,
  1735. gfar_irq(grp, TX)->name, grp);
  1736. if (err < 0) {
  1737. netif_err(priv, intr, dev, "Can't get IRQ %d\n",
  1738. gfar_irq(grp, TX)->irq);
  1739. goto err_irq_fail;
  1740. }
  1741. enable_irq_wake(gfar_irq(grp, TX)->irq);
  1742. }
  1743. return 0;
  1744. rx_irq_fail:
  1745. free_irq(gfar_irq(grp, TX)->irq, grp);
  1746. tx_irq_fail:
  1747. free_irq(gfar_irq(grp, ER)->irq, grp);
  1748. err_irq_fail:
  1749. return err;
  1750. }
  1751. static void gfar_free_irq(struct gfar_private *priv)
  1752. {
  1753. int i;
  1754. /* Free the IRQs */
  1755. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  1756. for (i = 0; i < priv->num_grps; i++)
  1757. free_grp_irqs(&priv->gfargrp[i]);
  1758. } else {
  1759. for (i = 0; i < priv->num_grps; i++)
  1760. free_irq(gfar_irq(&priv->gfargrp[i], TX)->irq,
  1761. &priv->gfargrp[i]);
  1762. }
  1763. }
  1764. static int gfar_request_irq(struct gfar_private *priv)
  1765. {
  1766. int err, i, j;
  1767. for (i = 0; i < priv->num_grps; i++) {
  1768. err = register_grp_irqs(&priv->gfargrp[i]);
  1769. if (err) {
  1770. for (j = 0; j < i; j++)
  1771. free_grp_irqs(&priv->gfargrp[j]);
  1772. return err;
  1773. }
  1774. }
  1775. return 0;
  1776. }
  1777. /* Bring the controller up and running */
  1778. int startup_gfar(struct net_device *ndev)
  1779. {
  1780. struct gfar_private *priv = netdev_priv(ndev);
  1781. int err;
  1782. gfar_mac_reset(priv);
  1783. err = gfar_alloc_skb_resources(ndev);
  1784. if (err)
  1785. return err;
  1786. gfar_init_tx_rx_base(priv);
  1787. smp_mb__before_atomic();
  1788. clear_bit(GFAR_DOWN, &priv->state);
  1789. smp_mb__after_atomic();
  1790. /* Start Rx/Tx DMA and enable the interrupts */
  1791. gfar_start(priv);
  1792. /* force link state update after mac reset */
  1793. priv->oldlink = 0;
  1794. priv->oldspeed = 0;
  1795. priv->oldduplex = -1;
  1796. phy_start(ndev->phydev);
  1797. enable_napi(priv);
  1798. netif_tx_wake_all_queues(ndev);
  1799. return 0;
  1800. }
  1801. /* Called when something needs to use the ethernet device
  1802. * Returns 0 for success.
  1803. */
  1804. static int gfar_enet_open(struct net_device *dev)
  1805. {
  1806. struct gfar_private *priv = netdev_priv(dev);
  1807. int err;
  1808. err = init_phy(dev);
  1809. if (err)
  1810. return err;
  1811. err = gfar_request_irq(priv);
  1812. if (err)
  1813. return err;
  1814. err = startup_gfar(dev);
  1815. if (err)
  1816. return err;
  1817. return err;
  1818. }
  1819. static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
  1820. {
  1821. struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN);
  1822. memset(fcb, 0, GMAC_FCB_LEN);
  1823. return fcb;
  1824. }
  1825. static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb,
  1826. int fcb_length)
  1827. {
  1828. /* If we're here, it's a IP packet with a TCP or UDP
  1829. * payload. We set it to checksum, using a pseudo-header
  1830. * we provide
  1831. */
  1832. u8 flags = TXFCB_DEFAULT;
  1833. /* Tell the controller what the protocol is
  1834. * And provide the already calculated phcs
  1835. */
  1836. if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
  1837. flags |= TXFCB_UDP;
  1838. fcb->phcs = (__force __be16)(udp_hdr(skb)->check);
  1839. } else
  1840. fcb->phcs = (__force __be16)(tcp_hdr(skb)->check);
  1841. /* l3os is the distance between the start of the
  1842. * frame (skb->data) and the start of the IP hdr.
  1843. * l4os is the distance between the start of the
  1844. * l3 hdr and the l4 hdr
  1845. */
  1846. fcb->l3os = (u8)(skb_network_offset(skb) - fcb_length);
  1847. fcb->l4os = skb_network_header_len(skb);
  1848. fcb->flags = flags;
  1849. }
  1850. static inline void gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
  1851. {
  1852. fcb->flags |= TXFCB_VLN;
  1853. fcb->vlctl = cpu_to_be16(skb_vlan_tag_get(skb));
  1854. }
  1855. static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
  1856. struct txbd8 *base, int ring_size)
  1857. {
  1858. struct txbd8 *new_bd = bdp + stride;
  1859. return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
  1860. }
  1861. static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
  1862. int ring_size)
  1863. {
  1864. return skip_txbd(bdp, 1, base, ring_size);
  1865. }
  1866. /* eTSEC12: csum generation not supported for some fcb offsets */
  1867. static inline bool gfar_csum_errata_12(struct gfar_private *priv,
  1868. unsigned long fcb_addr)
  1869. {
  1870. return (gfar_has_errata(priv, GFAR_ERRATA_12) &&
  1871. (fcb_addr % 0x20) > 0x18);
  1872. }
  1873. /* eTSEC76: csum generation for frames larger than 2500 may
  1874. * cause excess delays before start of transmission
  1875. */
  1876. static inline bool gfar_csum_errata_76(struct gfar_private *priv,
  1877. unsigned int len)
  1878. {
  1879. return (gfar_has_errata(priv, GFAR_ERRATA_76) &&
  1880. (len > 2500));
  1881. }
  1882. /* This is called by the kernel when a frame is ready for transmission.
  1883. * It is pointed to by the dev->hard_start_xmit function pointer
  1884. */
  1885. static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1886. {
  1887. struct gfar_private *priv = netdev_priv(dev);
  1888. struct gfar_priv_tx_q *tx_queue = NULL;
  1889. struct netdev_queue *txq;
  1890. struct gfar __iomem *regs = NULL;
  1891. struct txfcb *fcb = NULL;
  1892. struct txbd8 *txbdp, *txbdp_start, *base, *txbdp_tstamp = NULL;
  1893. u32 lstatus;
  1894. skb_frag_t *frag;
  1895. int i, rq = 0;
  1896. int do_tstamp, do_csum, do_vlan;
  1897. u32 bufaddr;
  1898. unsigned int nr_frags, nr_txbds, bytes_sent, fcb_len = 0;
  1899. rq = skb->queue_mapping;
  1900. tx_queue = priv->tx_queue[rq];
  1901. txq = netdev_get_tx_queue(dev, rq);
  1902. base = tx_queue->tx_bd_base;
  1903. regs = tx_queue->grp->regs;
  1904. do_csum = (CHECKSUM_PARTIAL == skb->ip_summed);
  1905. do_vlan = skb_vlan_tag_present(skb);
  1906. do_tstamp = (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  1907. priv->hwts_tx_en;
  1908. if (do_csum || do_vlan)
  1909. fcb_len = GMAC_FCB_LEN;
  1910. /* check if time stamp should be generated */
  1911. if (unlikely(do_tstamp))
  1912. fcb_len = GMAC_FCB_LEN + GMAC_TXPAL_LEN;
  1913. /* make space for additional header when fcb is needed */
  1914. if (fcb_len && unlikely(skb_headroom(skb) < fcb_len)) {
  1915. struct sk_buff *skb_new;
  1916. skb_new = skb_realloc_headroom(skb, fcb_len);
  1917. if (!skb_new) {
  1918. dev->stats.tx_errors++;
  1919. dev_kfree_skb_any(skb);
  1920. return NETDEV_TX_OK;
  1921. }
  1922. if (skb->sk)
  1923. skb_set_owner_w(skb_new, skb->sk);
  1924. dev_consume_skb_any(skb);
  1925. skb = skb_new;
  1926. }
  1927. /* total number of fragments in the SKB */
  1928. nr_frags = skb_shinfo(skb)->nr_frags;
  1929. /* calculate the required number of TxBDs for this skb */
  1930. if (unlikely(do_tstamp))
  1931. nr_txbds = nr_frags + 2;
  1932. else
  1933. nr_txbds = nr_frags + 1;
  1934. /* check if there is space to queue this packet */
  1935. if (nr_txbds > tx_queue->num_txbdfree) {
  1936. /* no space, stop the queue */
  1937. netif_tx_stop_queue(txq);
  1938. dev->stats.tx_fifo_errors++;
  1939. return NETDEV_TX_BUSY;
  1940. }
  1941. /* Update transmit stats */
  1942. bytes_sent = skb->len;
  1943. tx_queue->stats.tx_bytes += bytes_sent;
  1944. /* keep Tx bytes on wire for BQL accounting */
  1945. GFAR_CB(skb)->bytes_sent = bytes_sent;
  1946. tx_queue->stats.tx_packets++;
  1947. txbdp = txbdp_start = tx_queue->cur_tx;
  1948. lstatus = be32_to_cpu(txbdp->lstatus);
  1949. /* Add TxPAL between FCB and frame if required */
  1950. if (unlikely(do_tstamp)) {
  1951. skb_push(skb, GMAC_TXPAL_LEN);
  1952. memset(skb->data, 0, GMAC_TXPAL_LEN);
  1953. }
  1954. /* Add TxFCB if required */
  1955. if (fcb_len) {
  1956. fcb = gfar_add_fcb(skb);
  1957. lstatus |= BD_LFLAG(TXBD_TOE);
  1958. }
  1959. /* Set up checksumming */
  1960. if (do_csum) {
  1961. gfar_tx_checksum(skb, fcb, fcb_len);
  1962. if (unlikely(gfar_csum_errata_12(priv, (unsigned long)fcb)) ||
  1963. unlikely(gfar_csum_errata_76(priv, skb->len))) {
  1964. __skb_pull(skb, GMAC_FCB_LEN);
  1965. skb_checksum_help(skb);
  1966. if (do_vlan || do_tstamp) {
  1967. /* put back a new fcb for vlan/tstamp TOE */
  1968. fcb = gfar_add_fcb(skb);
  1969. } else {
  1970. /* Tx TOE not used */
  1971. lstatus &= ~(BD_LFLAG(TXBD_TOE));
  1972. fcb = NULL;
  1973. }
  1974. }
  1975. }
  1976. if (do_vlan)
  1977. gfar_tx_vlan(skb, fcb);
  1978. bufaddr = dma_map_single(priv->dev, skb->data, skb_headlen(skb),
  1979. DMA_TO_DEVICE);
  1980. if (unlikely(dma_mapping_error(priv->dev, bufaddr)))
  1981. goto dma_map_err;
  1982. txbdp_start->bufPtr = cpu_to_be32(bufaddr);
  1983. /* Time stamp insertion requires one additional TxBD */
  1984. if (unlikely(do_tstamp))
  1985. txbdp_tstamp = txbdp = next_txbd(txbdp, base,
  1986. tx_queue->tx_ring_size);
  1987. if (likely(!nr_frags)) {
  1988. if (likely(!do_tstamp))
  1989. lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
  1990. } else {
  1991. u32 lstatus_start = lstatus;
  1992. /* Place the fragment addresses and lengths into the TxBDs */
  1993. frag = &skb_shinfo(skb)->frags[0];
  1994. for (i = 0; i < nr_frags; i++, frag++) {
  1995. unsigned int size;
  1996. /* Point at the next BD, wrapping as needed */
  1997. txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
  1998. size = skb_frag_size(frag);
  1999. lstatus = be32_to_cpu(txbdp->lstatus) | size |
  2000. BD_LFLAG(TXBD_READY);
  2001. /* Handle the last BD specially */
  2002. if (i == nr_frags - 1)
  2003. lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
  2004. bufaddr = skb_frag_dma_map(priv->dev, frag, 0,
  2005. size, DMA_TO_DEVICE);
  2006. if (unlikely(dma_mapping_error(priv->dev, bufaddr)))
  2007. goto dma_map_err;
  2008. /* set the TxBD length and buffer pointer */
  2009. txbdp->bufPtr = cpu_to_be32(bufaddr);
  2010. txbdp->lstatus = cpu_to_be32(lstatus);
  2011. }
  2012. lstatus = lstatus_start;
  2013. }
  2014. /* If time stamping is requested one additional TxBD must be set up. The
  2015. * first TxBD points to the FCB and must have a data length of
  2016. * GMAC_FCB_LEN. The second TxBD points to the actual frame data with
  2017. * the full frame length.
  2018. */
  2019. if (unlikely(do_tstamp)) {
  2020. u32 lstatus_ts = be32_to_cpu(txbdp_tstamp->lstatus);
  2021. bufaddr = be32_to_cpu(txbdp_start->bufPtr);
  2022. bufaddr += fcb_len;
  2023. lstatus_ts |= BD_LFLAG(TXBD_READY) |
  2024. (skb_headlen(skb) - fcb_len);
  2025. if (!nr_frags)
  2026. lstatus_ts |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
  2027. txbdp_tstamp->bufPtr = cpu_to_be32(bufaddr);
  2028. txbdp_tstamp->lstatus = cpu_to_be32(lstatus_ts);
  2029. lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | GMAC_FCB_LEN;
  2030. /* Setup tx hardware time stamping */
  2031. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  2032. fcb->ptp = 1;
  2033. } else {
  2034. lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
  2035. }
  2036. netdev_tx_sent_queue(txq, bytes_sent);
  2037. gfar_wmb();
  2038. txbdp_start->lstatus = cpu_to_be32(lstatus);
  2039. gfar_wmb(); /* force lstatus write before tx_skbuff */
  2040. tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb;
  2041. /* Update the current skb pointer to the next entry we will use
  2042. * (wrapping if necessary)
  2043. */
  2044. tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) &
  2045. TX_RING_MOD_MASK(tx_queue->tx_ring_size);
  2046. tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
  2047. /* We can work in parallel with gfar_clean_tx_ring(), except
  2048. * when modifying num_txbdfree. Note that we didn't grab the lock
  2049. * when we were reading the num_txbdfree and checking for available
  2050. * space, that's because outside of this function it can only grow.
  2051. */
  2052. spin_lock_bh(&tx_queue->txlock);
  2053. /* reduce TxBD free count */
  2054. tx_queue->num_txbdfree -= (nr_txbds);
  2055. spin_unlock_bh(&tx_queue->txlock);
  2056. /* If the next BD still needs to be cleaned up, then the bds
  2057. * are full. We need to tell the kernel to stop sending us stuff.
  2058. */
  2059. if (!tx_queue->num_txbdfree) {
  2060. netif_tx_stop_queue(txq);
  2061. dev->stats.tx_fifo_errors++;
  2062. }
  2063. /* Tell the DMA to go go go */
  2064. gfar_write(&regs->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex);
  2065. return NETDEV_TX_OK;
  2066. dma_map_err:
  2067. txbdp = next_txbd(txbdp_start, base, tx_queue->tx_ring_size);
  2068. if (do_tstamp)
  2069. txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
  2070. for (i = 0; i < nr_frags; i++) {
  2071. lstatus = be32_to_cpu(txbdp->lstatus);
  2072. if (!(lstatus & BD_LFLAG(TXBD_READY)))
  2073. break;
  2074. lstatus &= ~BD_LFLAG(TXBD_READY);
  2075. txbdp->lstatus = cpu_to_be32(lstatus);
  2076. bufaddr = be32_to_cpu(txbdp->bufPtr);
  2077. dma_unmap_page(priv->dev, bufaddr, be16_to_cpu(txbdp->length),
  2078. DMA_TO_DEVICE);
  2079. txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
  2080. }
  2081. gfar_wmb();
  2082. dev_kfree_skb_any(skb);
  2083. return NETDEV_TX_OK;
  2084. }
  2085. /* Stops the kernel queue, and halts the controller */
  2086. static int gfar_close(struct net_device *dev)
  2087. {
  2088. struct gfar_private *priv = netdev_priv(dev);
  2089. cancel_work_sync(&priv->reset_task);
  2090. stop_gfar(dev);
  2091. /* Disconnect from the PHY */
  2092. phy_disconnect(dev->phydev);
  2093. gfar_free_irq(priv);
  2094. return 0;
  2095. }
  2096. /* Changes the mac address if the controller is not running. */
  2097. static int gfar_set_mac_address(struct net_device *dev)
  2098. {
  2099. gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
  2100. return 0;
  2101. }
  2102. static int gfar_change_mtu(struct net_device *dev, int new_mtu)
  2103. {
  2104. struct gfar_private *priv = netdev_priv(dev);
  2105. int frame_size = new_mtu + ETH_HLEN;
  2106. if ((frame_size < 64) || (frame_size > GFAR_JUMBO_FRAME_SIZE)) {
  2107. netif_err(priv, drv, dev, "Invalid MTU setting\n");
  2108. return -EINVAL;
  2109. }
  2110. while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
  2111. cpu_relax();
  2112. if (dev->flags & IFF_UP)
  2113. stop_gfar(dev);
  2114. dev->mtu = new_mtu;
  2115. if (dev->flags & IFF_UP)
  2116. startup_gfar(dev);
  2117. clear_bit_unlock(GFAR_RESETTING, &priv->state);
  2118. return 0;
  2119. }
  2120. void reset_gfar(struct net_device *ndev)
  2121. {
  2122. struct gfar_private *priv = netdev_priv(ndev);
  2123. while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
  2124. cpu_relax();
  2125. stop_gfar(ndev);
  2126. startup_gfar(ndev);
  2127. clear_bit_unlock(GFAR_RESETTING, &priv->state);
  2128. }
  2129. /* gfar_reset_task gets scheduled when a packet has not been
  2130. * transmitted after a set amount of time.
  2131. * For now, assume that clearing out all the structures, and
  2132. * starting over will fix the problem.
  2133. */
  2134. static void gfar_reset_task(struct work_struct *work)
  2135. {
  2136. struct gfar_private *priv = container_of(work, struct gfar_private,
  2137. reset_task);
  2138. reset_gfar(priv->ndev);
  2139. }
  2140. static void gfar_timeout(struct net_device *dev)
  2141. {
  2142. struct gfar_private *priv = netdev_priv(dev);
  2143. dev->stats.tx_errors++;
  2144. schedule_work(&priv->reset_task);
  2145. }
  2146. /* Interrupt Handler for Transmit complete */
  2147. static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
  2148. {
  2149. struct net_device *dev = tx_queue->dev;
  2150. struct netdev_queue *txq;
  2151. struct gfar_private *priv = netdev_priv(dev);
  2152. struct txbd8 *bdp, *next = NULL;
  2153. struct txbd8 *lbdp = NULL;
  2154. struct txbd8 *base = tx_queue->tx_bd_base;
  2155. struct sk_buff *skb;
  2156. int skb_dirtytx;
  2157. int tx_ring_size = tx_queue->tx_ring_size;
  2158. int frags = 0, nr_txbds = 0;
  2159. int i;
  2160. int howmany = 0;
  2161. int tqi = tx_queue->qindex;
  2162. unsigned int bytes_sent = 0;
  2163. u32 lstatus;
  2164. size_t buflen;
  2165. txq = netdev_get_tx_queue(dev, tqi);
  2166. bdp = tx_queue->dirty_tx;
  2167. skb_dirtytx = tx_queue->skb_dirtytx;
  2168. while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
  2169. frags = skb_shinfo(skb)->nr_frags;
  2170. /* When time stamping, one additional TxBD must be freed.
  2171. * Also, we need to dma_unmap_single() the TxPAL.
  2172. */
  2173. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
  2174. nr_txbds = frags + 2;
  2175. else
  2176. nr_txbds = frags + 1;
  2177. lbdp = skip_txbd(bdp, nr_txbds - 1, base, tx_ring_size);
  2178. lstatus = be32_to_cpu(lbdp->lstatus);
  2179. /* Only clean completed frames */
  2180. if ((lstatus & BD_LFLAG(TXBD_READY)) &&
  2181. (lstatus & BD_LENGTH_MASK))
  2182. break;
  2183. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
  2184. next = next_txbd(bdp, base, tx_ring_size);
  2185. buflen = be16_to_cpu(next->length) +
  2186. GMAC_FCB_LEN + GMAC_TXPAL_LEN;
  2187. } else
  2188. buflen = be16_to_cpu(bdp->length);
  2189. dma_unmap_single(priv->dev, be32_to_cpu(bdp->bufPtr),
  2190. buflen, DMA_TO_DEVICE);
  2191. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
  2192. struct skb_shared_hwtstamps shhwtstamps;
  2193. u64 *ns = (u64 *)(((uintptr_t)skb->data + 0x10) &
  2194. ~0x7UL);
  2195. memset(&shhwtstamps, 0, sizeof(shhwtstamps));
  2196. shhwtstamps.hwtstamp = ns_to_ktime(be64_to_cpu(*ns));
  2197. skb_pull(skb, GMAC_FCB_LEN + GMAC_TXPAL_LEN);
  2198. skb_tstamp_tx(skb, &shhwtstamps);
  2199. gfar_clear_txbd_status(bdp);
  2200. bdp = next;
  2201. }
  2202. gfar_clear_txbd_status(bdp);
  2203. bdp = next_txbd(bdp, base, tx_ring_size);
  2204. for (i = 0; i < frags; i++) {
  2205. dma_unmap_page(priv->dev, be32_to_cpu(bdp->bufPtr),
  2206. be16_to_cpu(bdp->length),
  2207. DMA_TO_DEVICE);
  2208. gfar_clear_txbd_status(bdp);
  2209. bdp = next_txbd(bdp, base, tx_ring_size);
  2210. }
  2211. bytes_sent += GFAR_CB(skb)->bytes_sent;
  2212. dev_kfree_skb_any(skb);
  2213. tx_queue->tx_skbuff[skb_dirtytx] = NULL;
  2214. skb_dirtytx = (skb_dirtytx + 1) &
  2215. TX_RING_MOD_MASK(tx_ring_size);
  2216. howmany++;
  2217. spin_lock(&tx_queue->txlock);
  2218. tx_queue->num_txbdfree += nr_txbds;
  2219. spin_unlock(&tx_queue->txlock);
  2220. }
  2221. /* If we freed a buffer, we can restart transmission, if necessary */
  2222. if (tx_queue->num_txbdfree &&
  2223. netif_tx_queue_stopped(txq) &&
  2224. !(test_bit(GFAR_DOWN, &priv->state)))
  2225. netif_wake_subqueue(priv->ndev, tqi);
  2226. /* Update dirty indicators */
  2227. tx_queue->skb_dirtytx = skb_dirtytx;
  2228. tx_queue->dirty_tx = bdp;
  2229. netdev_tx_completed_queue(txq, howmany, bytes_sent);
  2230. }
  2231. static bool gfar_new_page(struct gfar_priv_rx_q *rxq, struct gfar_rx_buff *rxb)
  2232. {
  2233. struct page *page;
  2234. dma_addr_t addr;
  2235. page = dev_alloc_page();
  2236. if (unlikely(!page))
  2237. return false;
  2238. addr = dma_map_page(rxq->dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE);
  2239. if (unlikely(dma_mapping_error(rxq->dev, addr))) {
  2240. __free_page(page);
  2241. return false;
  2242. }
  2243. rxb->dma = addr;
  2244. rxb->page = page;
  2245. rxb->page_offset = 0;
  2246. return true;
  2247. }
  2248. static void gfar_rx_alloc_err(struct gfar_priv_rx_q *rx_queue)
  2249. {
  2250. struct gfar_private *priv = netdev_priv(rx_queue->ndev);
  2251. struct gfar_extra_stats *estats = &priv->extra_stats;
  2252. netdev_err(rx_queue->ndev, "Can't alloc RX buffers\n");
  2253. atomic64_inc(&estats->rx_alloc_err);
  2254. }
  2255. static void gfar_alloc_rx_buffs(struct gfar_priv_rx_q *rx_queue,
  2256. int alloc_cnt)
  2257. {
  2258. struct rxbd8 *bdp;
  2259. struct gfar_rx_buff *rxb;
  2260. int i;
  2261. i = rx_queue->next_to_use;
  2262. bdp = &rx_queue->rx_bd_base[i];
  2263. rxb = &rx_queue->rx_buff[i];
  2264. while (alloc_cnt--) {
  2265. /* try reuse page */
  2266. if (unlikely(!rxb->page)) {
  2267. if (unlikely(!gfar_new_page(rx_queue, rxb))) {
  2268. gfar_rx_alloc_err(rx_queue);
  2269. break;
  2270. }
  2271. }
  2272. /* Setup the new RxBD */
  2273. gfar_init_rxbdp(rx_queue, bdp,
  2274. rxb->dma + rxb->page_offset + RXBUF_ALIGNMENT);
  2275. /* Update to the next pointer */
  2276. bdp++;
  2277. rxb++;
  2278. if (unlikely(++i == rx_queue->rx_ring_size)) {
  2279. i = 0;
  2280. bdp = rx_queue->rx_bd_base;
  2281. rxb = rx_queue->rx_buff;
  2282. }
  2283. }
  2284. rx_queue->next_to_use = i;
  2285. rx_queue->next_to_alloc = i;
  2286. }
  2287. static void count_errors(u32 lstatus, struct net_device *ndev)
  2288. {
  2289. struct gfar_private *priv = netdev_priv(ndev);
  2290. struct net_device_stats *stats = &ndev->stats;
  2291. struct gfar_extra_stats *estats = &priv->extra_stats;
  2292. /* If the packet was truncated, none of the other errors matter */
  2293. if (lstatus & BD_LFLAG(RXBD_TRUNCATED)) {
  2294. stats->rx_length_errors++;
  2295. atomic64_inc(&estats->rx_trunc);
  2296. return;
  2297. }
  2298. /* Count the errors, if there were any */
  2299. if (lstatus & BD_LFLAG(RXBD_LARGE | RXBD_SHORT)) {
  2300. stats->rx_length_errors++;
  2301. if (lstatus & BD_LFLAG(RXBD_LARGE))
  2302. atomic64_inc(&estats->rx_large);
  2303. else
  2304. atomic64_inc(&estats->rx_short);
  2305. }
  2306. if (lstatus & BD_LFLAG(RXBD_NONOCTET)) {
  2307. stats->rx_frame_errors++;
  2308. atomic64_inc(&estats->rx_nonoctet);
  2309. }
  2310. if (lstatus & BD_LFLAG(RXBD_CRCERR)) {
  2311. atomic64_inc(&estats->rx_crcerr);
  2312. stats->rx_crc_errors++;
  2313. }
  2314. if (lstatus & BD_LFLAG(RXBD_OVERRUN)) {
  2315. atomic64_inc(&estats->rx_overrun);
  2316. stats->rx_over_errors++;
  2317. }
  2318. }
  2319. irqreturn_t gfar_receive(int irq, void *grp_id)
  2320. {
  2321. struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
  2322. unsigned long flags;
  2323. u32 imask, ievent;
  2324. ievent = gfar_read(&grp->regs->ievent);
  2325. if (unlikely(ievent & IEVENT_FGPI)) {
  2326. gfar_write(&grp->regs->ievent, IEVENT_FGPI);
  2327. return IRQ_HANDLED;
  2328. }
  2329. if (likely(napi_schedule_prep(&grp->napi_rx))) {
  2330. spin_lock_irqsave(&grp->grplock, flags);
  2331. imask = gfar_read(&grp->regs->imask);
  2332. imask &= IMASK_RX_DISABLED;
  2333. gfar_write(&grp->regs->imask, imask);
  2334. spin_unlock_irqrestore(&grp->grplock, flags);
  2335. __napi_schedule(&grp->napi_rx);
  2336. } else {
  2337. /* Clear IEVENT, so interrupts aren't called again
  2338. * because of the packets that have already arrived.
  2339. */
  2340. gfar_write(&grp->regs->ievent, IEVENT_RX_MASK);
  2341. }
  2342. return IRQ_HANDLED;
  2343. }
  2344. /* Interrupt Handler for Transmit complete */
  2345. static irqreturn_t gfar_transmit(int irq, void *grp_id)
  2346. {
  2347. struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
  2348. unsigned long flags;
  2349. u32 imask;
  2350. if (likely(napi_schedule_prep(&grp->napi_tx))) {
  2351. spin_lock_irqsave(&grp->grplock, flags);
  2352. imask = gfar_read(&grp->regs->imask);
  2353. imask &= IMASK_TX_DISABLED;
  2354. gfar_write(&grp->regs->imask, imask);
  2355. spin_unlock_irqrestore(&grp->grplock, flags);
  2356. __napi_schedule(&grp->napi_tx);
  2357. } else {
  2358. /* Clear IEVENT, so interrupts aren't called again
  2359. * because of the packets that have already arrived.
  2360. */
  2361. gfar_write(&grp->regs->ievent, IEVENT_TX_MASK);
  2362. }
  2363. return IRQ_HANDLED;
  2364. }
  2365. static bool gfar_add_rx_frag(struct gfar_rx_buff *rxb, u32 lstatus,
  2366. struct sk_buff *skb, bool first)
  2367. {
  2368. unsigned int size = lstatus & BD_LENGTH_MASK;
  2369. struct page *page = rxb->page;
  2370. bool last = !!(lstatus & BD_LFLAG(RXBD_LAST));
  2371. /* Remove the FCS from the packet length */
  2372. if (last)
  2373. size -= ETH_FCS_LEN;
  2374. if (likely(first)) {
  2375. skb_put(skb, size);
  2376. } else {
  2377. /* the last fragments' length contains the full frame length */
  2378. if (last)
  2379. size -= skb->len;
  2380. /* in case the last fragment consisted only of the FCS */
  2381. if (size > 0)
  2382. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
  2383. rxb->page_offset + RXBUF_ALIGNMENT,
  2384. size, GFAR_RXB_TRUESIZE);
  2385. }
  2386. /* try reuse page */
  2387. if (unlikely(page_count(page) != 1 || page_is_pfmemalloc(page)))
  2388. return false;
  2389. /* change offset to the other half */
  2390. rxb->page_offset ^= GFAR_RXB_TRUESIZE;
  2391. page_ref_inc(page);
  2392. return true;
  2393. }
  2394. static void gfar_reuse_rx_page(struct gfar_priv_rx_q *rxq,
  2395. struct gfar_rx_buff *old_rxb)
  2396. {
  2397. struct gfar_rx_buff *new_rxb;
  2398. u16 nta = rxq->next_to_alloc;
  2399. new_rxb = &rxq->rx_buff[nta];
  2400. /* find next buf that can reuse a page */
  2401. nta++;
  2402. rxq->next_to_alloc = (nta < rxq->rx_ring_size) ? nta : 0;
  2403. /* copy page reference */
  2404. *new_rxb = *old_rxb;
  2405. /* sync for use by the device */
  2406. dma_sync_single_range_for_device(rxq->dev, old_rxb->dma,
  2407. old_rxb->page_offset,
  2408. GFAR_RXB_TRUESIZE, DMA_FROM_DEVICE);
  2409. }
  2410. static struct sk_buff *gfar_get_next_rxbuff(struct gfar_priv_rx_q *rx_queue,
  2411. u32 lstatus, struct sk_buff *skb)
  2412. {
  2413. struct gfar_rx_buff *rxb = &rx_queue->rx_buff[rx_queue->next_to_clean];
  2414. struct page *page = rxb->page;
  2415. bool first = false;
  2416. if (likely(!skb)) {
  2417. void *buff_addr = page_address(page) + rxb->page_offset;
  2418. skb = build_skb(buff_addr, GFAR_SKBFRAG_SIZE);
  2419. if (unlikely(!skb)) {
  2420. gfar_rx_alloc_err(rx_queue);
  2421. return NULL;
  2422. }
  2423. skb_reserve(skb, RXBUF_ALIGNMENT);
  2424. first = true;
  2425. }
  2426. dma_sync_single_range_for_cpu(rx_queue->dev, rxb->dma, rxb->page_offset,
  2427. GFAR_RXB_TRUESIZE, DMA_FROM_DEVICE);
  2428. if (gfar_add_rx_frag(rxb, lstatus, skb, first)) {
  2429. /* reuse the free half of the page */
  2430. gfar_reuse_rx_page(rx_queue, rxb);
  2431. } else {
  2432. /* page cannot be reused, unmap it */
  2433. dma_unmap_page(rx_queue->dev, rxb->dma,
  2434. PAGE_SIZE, DMA_FROM_DEVICE);
  2435. }
  2436. /* clear rxb content */
  2437. rxb->page = NULL;
  2438. return skb;
  2439. }
  2440. static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
  2441. {
  2442. /* If valid headers were found, and valid sums
  2443. * were verified, then we tell the kernel that no
  2444. * checksumming is necessary. Otherwise, it is [FIXME]
  2445. */
  2446. if ((be16_to_cpu(fcb->flags) & RXFCB_CSUM_MASK) ==
  2447. (RXFCB_CIP | RXFCB_CTU))
  2448. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2449. else
  2450. skb_checksum_none_assert(skb);
  2451. }
  2452. /* gfar_process_frame() -- handle one incoming packet if skb isn't NULL. */
  2453. static void gfar_process_frame(struct net_device *ndev, struct sk_buff *skb)
  2454. {
  2455. struct gfar_private *priv = netdev_priv(ndev);
  2456. struct rxfcb *fcb = NULL;
  2457. /* fcb is at the beginning if exists */
  2458. fcb = (struct rxfcb *)skb->data;
  2459. /* Remove the FCB from the skb
  2460. * Remove the padded bytes, if there are any
  2461. */
  2462. if (priv->uses_rxfcb)
  2463. skb_pull(skb, GMAC_FCB_LEN);
  2464. /* Get receive timestamp from the skb */
  2465. if (priv->hwts_rx_en) {
  2466. struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
  2467. u64 *ns = (u64 *) skb->data;
  2468. memset(shhwtstamps, 0, sizeof(*shhwtstamps));
  2469. shhwtstamps->hwtstamp = ns_to_ktime(be64_to_cpu(*ns));
  2470. }
  2471. if (priv->padding)
  2472. skb_pull(skb, priv->padding);
  2473. if (ndev->features & NETIF_F_RXCSUM)
  2474. gfar_rx_checksum(skb, fcb);
  2475. /* Tell the skb what kind of packet this is */
  2476. skb->protocol = eth_type_trans(skb, ndev);
  2477. /* There's need to check for NETIF_F_HW_VLAN_CTAG_RX here.
  2478. * Even if vlan rx accel is disabled, on some chips
  2479. * RXFCB_VLN is pseudo randomly set.
  2480. */
  2481. if (ndev->features & NETIF_F_HW_VLAN_CTAG_RX &&
  2482. be16_to_cpu(fcb->flags) & RXFCB_VLN)
  2483. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  2484. be16_to_cpu(fcb->vlctl));
  2485. }
  2486. /* gfar_clean_rx_ring() -- Processes each frame in the rx ring
  2487. * until the budget/quota has been reached. Returns the number
  2488. * of frames handled
  2489. */
  2490. int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit)
  2491. {
  2492. struct net_device *ndev = rx_queue->ndev;
  2493. struct gfar_private *priv = netdev_priv(ndev);
  2494. struct rxbd8 *bdp;
  2495. int i, howmany = 0;
  2496. struct sk_buff *skb = rx_queue->skb;
  2497. int cleaned_cnt = gfar_rxbd_unused(rx_queue);
  2498. unsigned int total_bytes = 0, total_pkts = 0;
  2499. /* Get the first full descriptor */
  2500. i = rx_queue->next_to_clean;
  2501. while (rx_work_limit--) {
  2502. u32 lstatus;
  2503. if (cleaned_cnt >= GFAR_RX_BUFF_ALLOC) {
  2504. gfar_alloc_rx_buffs(rx_queue, cleaned_cnt);
  2505. cleaned_cnt = 0;
  2506. }
  2507. bdp = &rx_queue->rx_bd_base[i];
  2508. lstatus = be32_to_cpu(bdp->lstatus);
  2509. if (lstatus & BD_LFLAG(RXBD_EMPTY))
  2510. break;
  2511. /* order rx buffer descriptor reads */
  2512. rmb();
  2513. /* fetch next to clean buffer from the ring */
  2514. skb = gfar_get_next_rxbuff(rx_queue, lstatus, skb);
  2515. if (unlikely(!skb))
  2516. break;
  2517. cleaned_cnt++;
  2518. howmany++;
  2519. if (unlikely(++i == rx_queue->rx_ring_size))
  2520. i = 0;
  2521. rx_queue->next_to_clean = i;
  2522. /* fetch next buffer if not the last in frame */
  2523. if (!(lstatus & BD_LFLAG(RXBD_LAST)))
  2524. continue;
  2525. if (unlikely(lstatus & BD_LFLAG(RXBD_ERR))) {
  2526. count_errors(lstatus, ndev);
  2527. /* discard faulty buffer */
  2528. dev_kfree_skb(skb);
  2529. skb = NULL;
  2530. rx_queue->stats.rx_dropped++;
  2531. continue;
  2532. }
  2533. /* Increment the number of packets */
  2534. total_pkts++;
  2535. total_bytes += skb->len;
  2536. skb_record_rx_queue(skb, rx_queue->qindex);
  2537. gfar_process_frame(ndev, skb);
  2538. /* Send the packet up the stack */
  2539. napi_gro_receive(&rx_queue->grp->napi_rx, skb);
  2540. skb = NULL;
  2541. }
  2542. /* Store incomplete frames for completion */
  2543. rx_queue->skb = skb;
  2544. rx_queue->stats.rx_packets += total_pkts;
  2545. rx_queue->stats.rx_bytes += total_bytes;
  2546. if (cleaned_cnt)
  2547. gfar_alloc_rx_buffs(rx_queue, cleaned_cnt);
  2548. /* Update Last Free RxBD pointer for LFC */
  2549. if (unlikely(priv->tx_actual_en)) {
  2550. u32 bdp_dma = gfar_rxbd_dma_lastfree(rx_queue);
  2551. gfar_write(rx_queue->rfbptr, bdp_dma);
  2552. }
  2553. return howmany;
  2554. }
  2555. static int gfar_poll_rx_sq(struct napi_struct *napi, int budget)
  2556. {
  2557. struct gfar_priv_grp *gfargrp =
  2558. container_of(napi, struct gfar_priv_grp, napi_rx);
  2559. struct gfar __iomem *regs = gfargrp->regs;
  2560. struct gfar_priv_rx_q *rx_queue = gfargrp->rx_queue;
  2561. int work_done = 0;
  2562. /* Clear IEVENT, so interrupts aren't called again
  2563. * because of the packets that have already arrived
  2564. */
  2565. gfar_write(&regs->ievent, IEVENT_RX_MASK);
  2566. work_done = gfar_clean_rx_ring(rx_queue, budget);
  2567. if (work_done < budget) {
  2568. u32 imask;
  2569. napi_complete(napi);
  2570. /* Clear the halt bit in RSTAT */
  2571. gfar_write(&regs->rstat, gfargrp->rstat);
  2572. spin_lock_irq(&gfargrp->grplock);
  2573. imask = gfar_read(&regs->imask);
  2574. imask |= IMASK_RX_DEFAULT;
  2575. gfar_write(&regs->imask, imask);
  2576. spin_unlock_irq(&gfargrp->grplock);
  2577. }
  2578. return work_done;
  2579. }
  2580. static int gfar_poll_tx_sq(struct napi_struct *napi, int budget)
  2581. {
  2582. struct gfar_priv_grp *gfargrp =
  2583. container_of(napi, struct gfar_priv_grp, napi_tx);
  2584. struct gfar __iomem *regs = gfargrp->regs;
  2585. struct gfar_priv_tx_q *tx_queue = gfargrp->tx_queue;
  2586. u32 imask;
  2587. /* Clear IEVENT, so interrupts aren't called again
  2588. * because of the packets that have already arrived
  2589. */
  2590. gfar_write(&regs->ievent, IEVENT_TX_MASK);
  2591. /* run Tx cleanup to completion */
  2592. if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx])
  2593. gfar_clean_tx_ring(tx_queue);
  2594. napi_complete(napi);
  2595. spin_lock_irq(&gfargrp->grplock);
  2596. imask = gfar_read(&regs->imask);
  2597. imask |= IMASK_TX_DEFAULT;
  2598. gfar_write(&regs->imask, imask);
  2599. spin_unlock_irq(&gfargrp->grplock);
  2600. return 0;
  2601. }
  2602. static int gfar_poll_rx(struct napi_struct *napi, int budget)
  2603. {
  2604. struct gfar_priv_grp *gfargrp =
  2605. container_of(napi, struct gfar_priv_grp, napi_rx);
  2606. struct gfar_private *priv = gfargrp->priv;
  2607. struct gfar __iomem *regs = gfargrp->regs;
  2608. struct gfar_priv_rx_q *rx_queue = NULL;
  2609. int work_done = 0, work_done_per_q = 0;
  2610. int i, budget_per_q = 0;
  2611. unsigned long rstat_rxf;
  2612. int num_act_queues;
  2613. /* Clear IEVENT, so interrupts aren't called again
  2614. * because of the packets that have already arrived
  2615. */
  2616. gfar_write(&regs->ievent, IEVENT_RX_MASK);
  2617. rstat_rxf = gfar_read(&regs->rstat) & RSTAT_RXF_MASK;
  2618. num_act_queues = bitmap_weight(&rstat_rxf, MAX_RX_QS);
  2619. if (num_act_queues)
  2620. budget_per_q = budget/num_act_queues;
  2621. for_each_set_bit(i, &gfargrp->rx_bit_map, priv->num_rx_queues) {
  2622. /* skip queue if not active */
  2623. if (!(rstat_rxf & (RSTAT_CLEAR_RXF0 >> i)))
  2624. continue;
  2625. rx_queue = priv->rx_queue[i];
  2626. work_done_per_q =
  2627. gfar_clean_rx_ring(rx_queue, budget_per_q);
  2628. work_done += work_done_per_q;
  2629. /* finished processing this queue */
  2630. if (work_done_per_q < budget_per_q) {
  2631. /* clear active queue hw indication */
  2632. gfar_write(&regs->rstat,
  2633. RSTAT_CLEAR_RXF0 >> i);
  2634. num_act_queues--;
  2635. if (!num_act_queues)
  2636. break;
  2637. }
  2638. }
  2639. if (!num_act_queues) {
  2640. u32 imask;
  2641. napi_complete(napi);
  2642. /* Clear the halt bit in RSTAT */
  2643. gfar_write(&regs->rstat, gfargrp->rstat);
  2644. spin_lock_irq(&gfargrp->grplock);
  2645. imask = gfar_read(&regs->imask);
  2646. imask |= IMASK_RX_DEFAULT;
  2647. gfar_write(&regs->imask, imask);
  2648. spin_unlock_irq(&gfargrp->grplock);
  2649. }
  2650. return work_done;
  2651. }
  2652. static int gfar_poll_tx(struct napi_struct *napi, int budget)
  2653. {
  2654. struct gfar_priv_grp *gfargrp =
  2655. container_of(napi, struct gfar_priv_grp, napi_tx);
  2656. struct gfar_private *priv = gfargrp->priv;
  2657. struct gfar __iomem *regs = gfargrp->regs;
  2658. struct gfar_priv_tx_q *tx_queue = NULL;
  2659. int has_tx_work = 0;
  2660. int i;
  2661. /* Clear IEVENT, so interrupts aren't called again
  2662. * because of the packets that have already arrived
  2663. */
  2664. gfar_write(&regs->ievent, IEVENT_TX_MASK);
  2665. for_each_set_bit(i, &gfargrp->tx_bit_map, priv->num_tx_queues) {
  2666. tx_queue = priv->tx_queue[i];
  2667. /* run Tx cleanup to completion */
  2668. if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx]) {
  2669. gfar_clean_tx_ring(tx_queue);
  2670. has_tx_work = 1;
  2671. }
  2672. }
  2673. if (!has_tx_work) {
  2674. u32 imask;
  2675. napi_complete(napi);
  2676. spin_lock_irq(&gfargrp->grplock);
  2677. imask = gfar_read(&regs->imask);
  2678. imask |= IMASK_TX_DEFAULT;
  2679. gfar_write(&regs->imask, imask);
  2680. spin_unlock_irq(&gfargrp->grplock);
  2681. }
  2682. return 0;
  2683. }
  2684. #ifdef CONFIG_NET_POLL_CONTROLLER
  2685. /* Polling 'interrupt' - used by things like netconsole to send skbs
  2686. * without having to re-enable interrupts. It's not called while
  2687. * the interrupt routine is executing.
  2688. */
  2689. static void gfar_netpoll(struct net_device *dev)
  2690. {
  2691. struct gfar_private *priv = netdev_priv(dev);
  2692. int i;
  2693. /* If the device has multiple interrupts, run tx/rx */
  2694. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  2695. for (i = 0; i < priv->num_grps; i++) {
  2696. struct gfar_priv_grp *grp = &priv->gfargrp[i];
  2697. disable_irq(gfar_irq(grp, TX)->irq);
  2698. disable_irq(gfar_irq(grp, RX)->irq);
  2699. disable_irq(gfar_irq(grp, ER)->irq);
  2700. gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
  2701. enable_irq(gfar_irq(grp, ER)->irq);
  2702. enable_irq(gfar_irq(grp, RX)->irq);
  2703. enable_irq(gfar_irq(grp, TX)->irq);
  2704. }
  2705. } else {
  2706. for (i = 0; i < priv->num_grps; i++) {
  2707. struct gfar_priv_grp *grp = &priv->gfargrp[i];
  2708. disable_irq(gfar_irq(grp, TX)->irq);
  2709. gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
  2710. enable_irq(gfar_irq(grp, TX)->irq);
  2711. }
  2712. }
  2713. }
  2714. #endif
  2715. /* The interrupt handler for devices with one interrupt */
  2716. static irqreturn_t gfar_interrupt(int irq, void *grp_id)
  2717. {
  2718. struct gfar_priv_grp *gfargrp = grp_id;
  2719. /* Save ievent for future reference */
  2720. u32 events = gfar_read(&gfargrp->regs->ievent);
  2721. /* Check for reception */
  2722. if (events & IEVENT_RX_MASK)
  2723. gfar_receive(irq, grp_id);
  2724. /* Check for transmit completion */
  2725. if (events & IEVENT_TX_MASK)
  2726. gfar_transmit(irq, grp_id);
  2727. /* Check for errors */
  2728. if (events & IEVENT_ERR_MASK)
  2729. gfar_error(irq, grp_id);
  2730. return IRQ_HANDLED;
  2731. }
  2732. /* Called every time the controller might need to be made
  2733. * aware of new link state. The PHY code conveys this
  2734. * information through variables in the phydev structure, and this
  2735. * function converts those variables into the appropriate
  2736. * register values, and can bring down the device if needed.
  2737. */
  2738. static void adjust_link(struct net_device *dev)
  2739. {
  2740. struct gfar_private *priv = netdev_priv(dev);
  2741. struct phy_device *phydev = dev->phydev;
  2742. if (unlikely(phydev->link != priv->oldlink ||
  2743. (phydev->link && (phydev->duplex != priv->oldduplex ||
  2744. phydev->speed != priv->oldspeed))))
  2745. gfar_update_link_state(priv);
  2746. }
  2747. /* Update the hash table based on the current list of multicast
  2748. * addresses we subscribe to. Also, change the promiscuity of
  2749. * the device based on the flags (this function is called
  2750. * whenever dev->flags is changed
  2751. */
  2752. static void gfar_set_multi(struct net_device *dev)
  2753. {
  2754. struct netdev_hw_addr *ha;
  2755. struct gfar_private *priv = netdev_priv(dev);
  2756. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  2757. u32 tempval;
  2758. if (dev->flags & IFF_PROMISC) {
  2759. /* Set RCTRL to PROM */
  2760. tempval = gfar_read(&regs->rctrl);
  2761. tempval |= RCTRL_PROM;
  2762. gfar_write(&regs->rctrl, tempval);
  2763. } else {
  2764. /* Set RCTRL to not PROM */
  2765. tempval = gfar_read(&regs->rctrl);
  2766. tempval &= ~(RCTRL_PROM);
  2767. gfar_write(&regs->rctrl, tempval);
  2768. }
  2769. if (dev->flags & IFF_ALLMULTI) {
  2770. /* Set the hash to rx all multicast frames */
  2771. gfar_write(&regs->igaddr0, 0xffffffff);
  2772. gfar_write(&regs->igaddr1, 0xffffffff);
  2773. gfar_write(&regs->igaddr2, 0xffffffff);
  2774. gfar_write(&regs->igaddr3, 0xffffffff);
  2775. gfar_write(&regs->igaddr4, 0xffffffff);
  2776. gfar_write(&regs->igaddr5, 0xffffffff);
  2777. gfar_write(&regs->igaddr6, 0xffffffff);
  2778. gfar_write(&regs->igaddr7, 0xffffffff);
  2779. gfar_write(&regs->gaddr0, 0xffffffff);
  2780. gfar_write(&regs->gaddr1, 0xffffffff);
  2781. gfar_write(&regs->gaddr2, 0xffffffff);
  2782. gfar_write(&regs->gaddr3, 0xffffffff);
  2783. gfar_write(&regs->gaddr4, 0xffffffff);
  2784. gfar_write(&regs->gaddr5, 0xffffffff);
  2785. gfar_write(&regs->gaddr6, 0xffffffff);
  2786. gfar_write(&regs->gaddr7, 0xffffffff);
  2787. } else {
  2788. int em_num;
  2789. int idx;
  2790. /* zero out the hash */
  2791. gfar_write(&regs->igaddr0, 0x0);
  2792. gfar_write(&regs->igaddr1, 0x0);
  2793. gfar_write(&regs->igaddr2, 0x0);
  2794. gfar_write(&regs->igaddr3, 0x0);
  2795. gfar_write(&regs->igaddr4, 0x0);
  2796. gfar_write(&regs->igaddr5, 0x0);
  2797. gfar_write(&regs->igaddr6, 0x0);
  2798. gfar_write(&regs->igaddr7, 0x0);
  2799. gfar_write(&regs->gaddr0, 0x0);
  2800. gfar_write(&regs->gaddr1, 0x0);
  2801. gfar_write(&regs->gaddr2, 0x0);
  2802. gfar_write(&regs->gaddr3, 0x0);
  2803. gfar_write(&regs->gaddr4, 0x0);
  2804. gfar_write(&regs->gaddr5, 0x0);
  2805. gfar_write(&regs->gaddr6, 0x0);
  2806. gfar_write(&regs->gaddr7, 0x0);
  2807. /* If we have extended hash tables, we need to
  2808. * clear the exact match registers to prepare for
  2809. * setting them
  2810. */
  2811. if (priv->extended_hash) {
  2812. em_num = GFAR_EM_NUM + 1;
  2813. gfar_clear_exact_match(dev);
  2814. idx = 1;
  2815. } else {
  2816. idx = 0;
  2817. em_num = 0;
  2818. }
  2819. if (netdev_mc_empty(dev))
  2820. return;
  2821. /* Parse the list, and set the appropriate bits */
  2822. netdev_for_each_mc_addr(ha, dev) {
  2823. if (idx < em_num) {
  2824. gfar_set_mac_for_addr(dev, idx, ha->addr);
  2825. idx++;
  2826. } else
  2827. gfar_set_hash_for_addr(dev, ha->addr);
  2828. }
  2829. }
  2830. }
  2831. /* Clears each of the exact match registers to zero, so they
  2832. * don't interfere with normal reception
  2833. */
  2834. static void gfar_clear_exact_match(struct net_device *dev)
  2835. {
  2836. int idx;
  2837. static const u8 zero_arr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
  2838. for (idx = 1; idx < GFAR_EM_NUM + 1; idx++)
  2839. gfar_set_mac_for_addr(dev, idx, zero_arr);
  2840. }
  2841. /* Set the appropriate hash bit for the given addr */
  2842. /* The algorithm works like so:
  2843. * 1) Take the Destination Address (ie the multicast address), and
  2844. * do a CRC on it (little endian), and reverse the bits of the
  2845. * result.
  2846. * 2) Use the 8 most significant bits as a hash into a 256-entry
  2847. * table. The table is controlled through 8 32-bit registers:
  2848. * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
  2849. * gaddr7. This means that the 3 most significant bits in the
  2850. * hash index which gaddr register to use, and the 5 other bits
  2851. * indicate which bit (assuming an IBM numbering scheme, which
  2852. * for PowerPC (tm) is usually the case) in the register holds
  2853. * the entry.
  2854. */
  2855. static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
  2856. {
  2857. u32 tempval;
  2858. struct gfar_private *priv = netdev_priv(dev);
  2859. u32 result = ether_crc(ETH_ALEN, addr);
  2860. int width = priv->hash_width;
  2861. u8 whichbit = (result >> (32 - width)) & 0x1f;
  2862. u8 whichreg = result >> (32 - width + 5);
  2863. u32 value = (1 << (31-whichbit));
  2864. tempval = gfar_read(priv->hash_regs[whichreg]);
  2865. tempval |= value;
  2866. gfar_write(priv->hash_regs[whichreg], tempval);
  2867. }
  2868. /* There are multiple MAC Address register pairs on some controllers
  2869. * This function sets the numth pair to a given address
  2870. */
  2871. static void gfar_set_mac_for_addr(struct net_device *dev, int num,
  2872. const u8 *addr)
  2873. {
  2874. struct gfar_private *priv = netdev_priv(dev);
  2875. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  2876. u32 tempval;
  2877. u32 __iomem *macptr = &regs->macstnaddr1;
  2878. macptr += num*2;
  2879. /* For a station address of 0x12345678ABCD in transmission
  2880. * order (BE), MACnADDR1 is set to 0xCDAB7856 and
  2881. * MACnADDR2 is set to 0x34120000.
  2882. */
  2883. tempval = (addr[5] << 24) | (addr[4] << 16) |
  2884. (addr[3] << 8) | addr[2];
  2885. gfar_write(macptr, tempval);
  2886. tempval = (addr[1] << 24) | (addr[0] << 16);
  2887. gfar_write(macptr+1, tempval);
  2888. }
  2889. /* GFAR error interrupt handler */
  2890. static irqreturn_t gfar_error(int irq, void *grp_id)
  2891. {
  2892. struct gfar_priv_grp *gfargrp = grp_id;
  2893. struct gfar __iomem *regs = gfargrp->regs;
  2894. struct gfar_private *priv= gfargrp->priv;
  2895. struct net_device *dev = priv->ndev;
  2896. /* Save ievent for future reference */
  2897. u32 events = gfar_read(&regs->ievent);
  2898. /* Clear IEVENT */
  2899. gfar_write(&regs->ievent, events & IEVENT_ERR_MASK);
  2900. /* Magic Packet is not an error. */
  2901. if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
  2902. (events & IEVENT_MAG))
  2903. events &= ~IEVENT_MAG;
  2904. /* Hmm... */
  2905. if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
  2906. netdev_dbg(dev,
  2907. "error interrupt (ievent=0x%08x imask=0x%08x)\n",
  2908. events, gfar_read(&regs->imask));
  2909. /* Update the error counters */
  2910. if (events & IEVENT_TXE) {
  2911. dev->stats.tx_errors++;
  2912. if (events & IEVENT_LC)
  2913. dev->stats.tx_window_errors++;
  2914. if (events & IEVENT_CRL)
  2915. dev->stats.tx_aborted_errors++;
  2916. if (events & IEVENT_XFUN) {
  2917. netif_dbg(priv, tx_err, dev,
  2918. "TX FIFO underrun, packet dropped\n");
  2919. dev->stats.tx_dropped++;
  2920. atomic64_inc(&priv->extra_stats.tx_underrun);
  2921. schedule_work(&priv->reset_task);
  2922. }
  2923. netif_dbg(priv, tx_err, dev, "Transmit Error\n");
  2924. }
  2925. if (events & IEVENT_BSY) {
  2926. dev->stats.rx_over_errors++;
  2927. atomic64_inc(&priv->extra_stats.rx_bsy);
  2928. netif_dbg(priv, rx_err, dev, "busy error (rstat: %x)\n",
  2929. gfar_read(&regs->rstat));
  2930. }
  2931. if (events & IEVENT_BABR) {
  2932. dev->stats.rx_errors++;
  2933. atomic64_inc(&priv->extra_stats.rx_babr);
  2934. netif_dbg(priv, rx_err, dev, "babbling RX error\n");
  2935. }
  2936. if (events & IEVENT_EBERR) {
  2937. atomic64_inc(&priv->extra_stats.eberr);
  2938. netif_dbg(priv, rx_err, dev, "bus error\n");
  2939. }
  2940. if (events & IEVENT_RXC)
  2941. netif_dbg(priv, rx_status, dev, "control frame\n");
  2942. if (events & IEVENT_BABT) {
  2943. atomic64_inc(&priv->extra_stats.tx_babt);
  2944. netif_dbg(priv, tx_err, dev, "babbling TX error\n");
  2945. }
  2946. return IRQ_HANDLED;
  2947. }
  2948. static u32 gfar_get_flowctrl_cfg(struct gfar_private *priv)
  2949. {
  2950. struct net_device *ndev = priv->ndev;
  2951. struct phy_device *phydev = ndev->phydev;
  2952. u32 val = 0;
  2953. if (!phydev->duplex)
  2954. return val;
  2955. if (!priv->pause_aneg_en) {
  2956. if (priv->tx_pause_en)
  2957. val |= MACCFG1_TX_FLOW;
  2958. if (priv->rx_pause_en)
  2959. val |= MACCFG1_RX_FLOW;
  2960. } else {
  2961. u16 lcl_adv, rmt_adv;
  2962. u8 flowctrl;
  2963. /* get link partner capabilities */
  2964. rmt_adv = 0;
  2965. if (phydev->pause)
  2966. rmt_adv = LPA_PAUSE_CAP;
  2967. if (phydev->asym_pause)
  2968. rmt_adv |= LPA_PAUSE_ASYM;
  2969. lcl_adv = 0;
  2970. if (phydev->advertising & ADVERTISED_Pause)
  2971. lcl_adv |= ADVERTISE_PAUSE_CAP;
  2972. if (phydev->advertising & ADVERTISED_Asym_Pause)
  2973. lcl_adv |= ADVERTISE_PAUSE_ASYM;
  2974. flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
  2975. if (flowctrl & FLOW_CTRL_TX)
  2976. val |= MACCFG1_TX_FLOW;
  2977. if (flowctrl & FLOW_CTRL_RX)
  2978. val |= MACCFG1_RX_FLOW;
  2979. }
  2980. return val;
  2981. }
  2982. static noinline void gfar_update_link_state(struct gfar_private *priv)
  2983. {
  2984. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  2985. struct net_device *ndev = priv->ndev;
  2986. struct phy_device *phydev = ndev->phydev;
  2987. struct gfar_priv_rx_q *rx_queue = NULL;
  2988. int i;
  2989. if (unlikely(test_bit(GFAR_RESETTING, &priv->state)))
  2990. return;
  2991. if (phydev->link) {
  2992. u32 tempval1 = gfar_read(&regs->maccfg1);
  2993. u32 tempval = gfar_read(&regs->maccfg2);
  2994. u32 ecntrl = gfar_read(&regs->ecntrl);
  2995. u32 tx_flow_oldval = (tempval1 & MACCFG1_TX_FLOW);
  2996. if (phydev->duplex != priv->oldduplex) {
  2997. if (!(phydev->duplex))
  2998. tempval &= ~(MACCFG2_FULL_DUPLEX);
  2999. else
  3000. tempval |= MACCFG2_FULL_DUPLEX;
  3001. priv->oldduplex = phydev->duplex;
  3002. }
  3003. if (phydev->speed != priv->oldspeed) {
  3004. switch (phydev->speed) {
  3005. case 1000:
  3006. tempval =
  3007. ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
  3008. ecntrl &= ~(ECNTRL_R100);
  3009. break;
  3010. case 100:
  3011. case 10:
  3012. tempval =
  3013. ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
  3014. /* Reduced mode distinguishes
  3015. * between 10 and 100
  3016. */
  3017. if (phydev->speed == SPEED_100)
  3018. ecntrl |= ECNTRL_R100;
  3019. else
  3020. ecntrl &= ~(ECNTRL_R100);
  3021. break;
  3022. default:
  3023. netif_warn(priv, link, priv->ndev,
  3024. "Ack! Speed (%d) is not 10/100/1000!\n",
  3025. phydev->speed);
  3026. break;
  3027. }
  3028. priv->oldspeed = phydev->speed;
  3029. }
  3030. tempval1 &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
  3031. tempval1 |= gfar_get_flowctrl_cfg(priv);
  3032. /* Turn last free buffer recording on */
  3033. if ((tempval1 & MACCFG1_TX_FLOW) && !tx_flow_oldval) {
  3034. for (i = 0; i < priv->num_rx_queues; i++) {
  3035. u32 bdp_dma;
  3036. rx_queue = priv->rx_queue[i];
  3037. bdp_dma = gfar_rxbd_dma_lastfree(rx_queue);
  3038. gfar_write(rx_queue->rfbptr, bdp_dma);
  3039. }
  3040. priv->tx_actual_en = 1;
  3041. }
  3042. if (unlikely(!(tempval1 & MACCFG1_TX_FLOW) && tx_flow_oldval))
  3043. priv->tx_actual_en = 0;
  3044. gfar_write(&regs->maccfg1, tempval1);
  3045. gfar_write(&regs->maccfg2, tempval);
  3046. gfar_write(&regs->ecntrl, ecntrl);
  3047. if (!priv->oldlink)
  3048. priv->oldlink = 1;
  3049. } else if (priv->oldlink) {
  3050. priv->oldlink = 0;
  3051. priv->oldspeed = 0;
  3052. priv->oldduplex = -1;
  3053. }
  3054. if (netif_msg_link(priv))
  3055. phy_print_status(phydev);
  3056. }
  3057. static const struct of_device_id gfar_match[] =
  3058. {
  3059. {
  3060. .type = "network",
  3061. .compatible = "gianfar",
  3062. },
  3063. {
  3064. .compatible = "fsl,etsec2",
  3065. },
  3066. {},
  3067. };
  3068. MODULE_DEVICE_TABLE(of, gfar_match);
  3069. /* Structure for a device driver */
  3070. static struct platform_driver gfar_driver = {
  3071. .driver = {
  3072. .name = "fsl-gianfar",
  3073. .pm = GFAR_PM_OPS,
  3074. .of_match_table = gfar_match,
  3075. },
  3076. .probe = gfar_probe,
  3077. .remove = gfar_remove,
  3078. };
  3079. module_platform_driver(gfar_driver);