gianfar.c 87 KB

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