gianfar.c 87 KB

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