gianfar.c 86 KB

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