gianfar.c 98 KB

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