gianfar.c 90 KB

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