gianfar.c 92 KB

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