gianfar.c 90 KB

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