gianfar.c 94 KB

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