gianfar.c 86 KB

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