gianfar.c 84 KB

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