gianfar.c 94 KB

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