gianfar.c 90 KB

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