gianfar.c 79 KB

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