gianfar.c 86 KB

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