gianfar.c 92 KB

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