gianfar.c 86 KB

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