gianfar.c 86 KB

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