gianfar.c 87 KB

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