gianfar.c 86 KB

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