gianfar.c 93 KB

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