core.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100
  1. /*
  2. * drivers/net/ibm_newemac/core.c
  3. *
  4. * Driver for PowerPC 4xx on-chip ethernet controller.
  5. *
  6. * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
  7. * <benh@kernel.crashing.org>
  8. *
  9. * Based on the arch/ppc version of the driver:
  10. *
  11. * Copyright (c) 2004, 2005 Zultys Technologies.
  12. * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
  13. *
  14. * Based on original work by
  15. * Matt Porter <mporter@kernel.crashing.org>
  16. * (c) 2003 Benjamin Herrenschmidt <benh@kernel.crashing.org>
  17. * Armin Kuster <akuster@mvista.com>
  18. * Johnnie Peters <jpeters@mvista.com>
  19. *
  20. * This program is free software; you can redistribute it and/or modify it
  21. * under the terms of the GNU General Public License as published by the
  22. * Free Software Foundation; either version 2 of the License, or (at your
  23. * option) any later version.
  24. *
  25. */
  26. #include <linux/module.h>
  27. #include <linux/sched.h>
  28. #include <linux/string.h>
  29. #include <linux/errno.h>
  30. #include <linux/delay.h>
  31. #include <linux/types.h>
  32. #include <linux/pci.h>
  33. #include <linux/etherdevice.h>
  34. #include <linux/skbuff.h>
  35. #include <linux/crc32.h>
  36. #include <linux/ethtool.h>
  37. #include <linux/mii.h>
  38. #include <linux/bitops.h>
  39. #include <linux/workqueue.h>
  40. #include <linux/of.h>
  41. #include <linux/slab.h>
  42. #include <asm/processor.h>
  43. #include <asm/io.h>
  44. #include <asm/dma.h>
  45. #include <asm/uaccess.h>
  46. #include <asm/dcr.h>
  47. #include <asm/dcr-regs.h>
  48. #include "core.h"
  49. /*
  50. * Lack of dma_unmap_???? calls is intentional.
  51. *
  52. * API-correct usage requires additional support state information to be
  53. * maintained for every RX and TX buffer descriptor (BD). Unfortunately, due to
  54. * EMAC design (e.g. TX buffer passed from network stack can be split into
  55. * several BDs, dma_map_single/dma_map_page can be used to map particular BD),
  56. * maintaining such information will add additional overhead.
  57. * Current DMA API implementation for 4xx processors only ensures cache coherency
  58. * and dma_unmap_???? routines are empty and are likely to stay this way.
  59. * I decided to omit dma_unmap_??? calls because I don't want to add additional
  60. * complexity just for the sake of following some abstract API, when it doesn't
  61. * add any real benefit to the driver. I understand that this decision maybe
  62. * controversial, but I really tried to make code API-correct and efficient
  63. * at the same time and didn't come up with code I liked :(. --ebs
  64. */
  65. #define DRV_NAME "emac"
  66. #define DRV_VERSION "3.54"
  67. #define DRV_DESC "PPC 4xx OCP EMAC driver"
  68. MODULE_DESCRIPTION(DRV_DESC);
  69. MODULE_AUTHOR
  70. ("Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>");
  71. MODULE_LICENSE("GPL");
  72. /*
  73. * PPC64 doesn't (yet) have a cacheable_memcpy
  74. */
  75. #ifdef CONFIG_PPC64
  76. #define cacheable_memcpy(d,s,n) memcpy((d),(s),(n))
  77. #endif
  78. /* minimum number of free TX descriptors required to wake up TX process */
  79. #define EMAC_TX_WAKEUP_THRESH (NUM_TX_BUFF / 4)
  80. /* If packet size is less than this number, we allocate small skb and copy packet
  81. * contents into it instead of just sending original big skb up
  82. */
  83. #define EMAC_RX_COPY_THRESH CONFIG_IBM_NEW_EMAC_RX_COPY_THRESHOLD
  84. /* Since multiple EMACs share MDIO lines in various ways, we need
  85. * to avoid re-using the same PHY ID in cases where the arch didn't
  86. * setup precise phy_map entries
  87. *
  88. * XXX This is something that needs to be reworked as we can have multiple
  89. * EMAC "sets" (multiple ASICs containing several EMACs) though we can
  90. * probably require in that case to have explicit PHY IDs in the device-tree
  91. */
  92. static u32 busy_phy_map;
  93. static DEFINE_MUTEX(emac_phy_map_lock);
  94. /* This is the wait queue used to wait on any event related to probe, that
  95. * is discovery of MALs, other EMACs, ZMII/RGMIIs, etc...
  96. */
  97. static DECLARE_WAIT_QUEUE_HEAD(emac_probe_wait);
  98. /* Having stable interface names is a doomed idea. However, it would be nice
  99. * if we didn't have completely random interface names at boot too :-) It's
  100. * just a matter of making everybody's life easier. Since we are doing
  101. * threaded probing, it's a bit harder though. The base idea here is that
  102. * we make up a list of all emacs in the device-tree before we register the
  103. * driver. Every emac will then wait for the previous one in the list to
  104. * initialize before itself. We should also keep that list ordered by
  105. * cell_index.
  106. * That list is only 4 entries long, meaning that additional EMACs don't
  107. * get ordering guarantees unless EMAC_BOOT_LIST_SIZE is increased.
  108. */
  109. #define EMAC_BOOT_LIST_SIZE 4
  110. static struct device_node *emac_boot_list[EMAC_BOOT_LIST_SIZE];
  111. /* How long should I wait for dependent devices ? */
  112. #define EMAC_PROBE_DEP_TIMEOUT (HZ * 5)
  113. /* I don't want to litter system log with timeout errors
  114. * when we have brain-damaged PHY.
  115. */
  116. static inline void emac_report_timeout_error(struct emac_instance *dev,
  117. const char *error)
  118. {
  119. if (emac_has_feature(dev, EMAC_FTR_440GX_PHY_CLK_FIX |
  120. EMAC_FTR_460EX_PHY_CLK_FIX |
  121. EMAC_FTR_440EP_PHY_CLK_FIX))
  122. DBG(dev, "%s" NL, error);
  123. else if (net_ratelimit())
  124. printk(KERN_ERR "%s: %s\n", dev->ofdev->dev.of_node->full_name,
  125. error);
  126. }
  127. /* EMAC PHY clock workaround:
  128. * 440EP/440GR has more sane SDR0_MFR register implementation than 440GX,
  129. * which allows controlling each EMAC clock
  130. */
  131. static inline void emac_rx_clk_tx(struct emac_instance *dev)
  132. {
  133. #ifdef CONFIG_PPC_DCR_NATIVE
  134. if (emac_has_feature(dev, EMAC_FTR_440EP_PHY_CLK_FIX))
  135. dcri_clrset(SDR0, SDR0_MFR,
  136. 0, SDR0_MFR_ECS >> dev->cell_index);
  137. #endif
  138. }
  139. static inline void emac_rx_clk_default(struct emac_instance *dev)
  140. {
  141. #ifdef CONFIG_PPC_DCR_NATIVE
  142. if (emac_has_feature(dev, EMAC_FTR_440EP_PHY_CLK_FIX))
  143. dcri_clrset(SDR0, SDR0_MFR,
  144. SDR0_MFR_ECS >> dev->cell_index, 0);
  145. #endif
  146. }
  147. /* PHY polling intervals */
  148. #define PHY_POLL_LINK_ON HZ
  149. #define PHY_POLL_LINK_OFF (HZ / 5)
  150. /* Graceful stop timeouts in us.
  151. * We should allow up to 1 frame time (full-duplex, ignoring collisions)
  152. */
  153. #define STOP_TIMEOUT_10 1230
  154. #define STOP_TIMEOUT_100 124
  155. #define STOP_TIMEOUT_1000 13
  156. #define STOP_TIMEOUT_1000_JUMBO 73
  157. static unsigned char default_mcast_addr[] = {
  158. 0x01, 0x80, 0xC2, 0x00, 0x00, 0x01
  159. };
  160. /* Please, keep in sync with struct ibm_emac_stats/ibm_emac_error_stats */
  161. static const char emac_stats_keys[EMAC_ETHTOOL_STATS_COUNT][ETH_GSTRING_LEN] = {
  162. "rx_packets", "rx_bytes", "tx_packets", "tx_bytes", "rx_packets_csum",
  163. "tx_packets_csum", "tx_undo", "rx_dropped_stack", "rx_dropped_oom",
  164. "rx_dropped_error", "rx_dropped_resize", "rx_dropped_mtu",
  165. "rx_stopped", "rx_bd_errors", "rx_bd_overrun", "rx_bd_bad_packet",
  166. "rx_bd_runt_packet", "rx_bd_short_event", "rx_bd_alignment_error",
  167. "rx_bd_bad_fcs", "rx_bd_packet_too_long", "rx_bd_out_of_range",
  168. "rx_bd_in_range", "rx_parity", "rx_fifo_overrun", "rx_overrun",
  169. "rx_bad_packet", "rx_runt_packet", "rx_short_event",
  170. "rx_alignment_error", "rx_bad_fcs", "rx_packet_too_long",
  171. "rx_out_of_range", "rx_in_range", "tx_dropped", "tx_bd_errors",
  172. "tx_bd_bad_fcs", "tx_bd_carrier_loss", "tx_bd_excessive_deferral",
  173. "tx_bd_excessive_collisions", "tx_bd_late_collision",
  174. "tx_bd_multple_collisions", "tx_bd_single_collision",
  175. "tx_bd_underrun", "tx_bd_sqe", "tx_parity", "tx_underrun", "tx_sqe",
  176. "tx_errors"
  177. };
  178. static irqreturn_t emac_irq(int irq, void *dev_instance);
  179. static void emac_clean_tx_ring(struct emac_instance *dev);
  180. static void __emac_set_multicast_list(struct emac_instance *dev);
  181. static inline int emac_phy_supports_gige(int phy_mode)
  182. {
  183. return phy_mode == PHY_MODE_GMII ||
  184. phy_mode == PHY_MODE_RGMII ||
  185. phy_mode == PHY_MODE_SGMII ||
  186. phy_mode == PHY_MODE_TBI ||
  187. phy_mode == PHY_MODE_RTBI;
  188. }
  189. static inline int emac_phy_gpcs(int phy_mode)
  190. {
  191. return phy_mode == PHY_MODE_SGMII ||
  192. phy_mode == PHY_MODE_TBI ||
  193. phy_mode == PHY_MODE_RTBI;
  194. }
  195. static inline void emac_tx_enable(struct emac_instance *dev)
  196. {
  197. struct emac_regs __iomem *p = dev->emacp;
  198. u32 r;
  199. DBG(dev, "tx_enable" NL);
  200. r = in_be32(&p->mr0);
  201. if (!(r & EMAC_MR0_TXE))
  202. out_be32(&p->mr0, r | EMAC_MR0_TXE);
  203. }
  204. static void emac_tx_disable(struct emac_instance *dev)
  205. {
  206. struct emac_regs __iomem *p = dev->emacp;
  207. u32 r;
  208. DBG(dev, "tx_disable" NL);
  209. r = in_be32(&p->mr0);
  210. if (r & EMAC_MR0_TXE) {
  211. int n = dev->stop_timeout;
  212. out_be32(&p->mr0, r & ~EMAC_MR0_TXE);
  213. while (!(in_be32(&p->mr0) & EMAC_MR0_TXI) && n) {
  214. udelay(1);
  215. --n;
  216. }
  217. if (unlikely(!n))
  218. emac_report_timeout_error(dev, "TX disable timeout");
  219. }
  220. }
  221. static void emac_rx_enable(struct emac_instance *dev)
  222. {
  223. struct emac_regs __iomem *p = dev->emacp;
  224. u32 r;
  225. if (unlikely(test_bit(MAL_COMMAC_RX_STOPPED, &dev->commac.flags)))
  226. goto out;
  227. DBG(dev, "rx_enable" NL);
  228. r = in_be32(&p->mr0);
  229. if (!(r & EMAC_MR0_RXE)) {
  230. if (unlikely(!(r & EMAC_MR0_RXI))) {
  231. /* Wait if previous async disable is still in progress */
  232. int n = dev->stop_timeout;
  233. while (!(r = in_be32(&p->mr0) & EMAC_MR0_RXI) && n) {
  234. udelay(1);
  235. --n;
  236. }
  237. if (unlikely(!n))
  238. emac_report_timeout_error(dev,
  239. "RX disable timeout");
  240. }
  241. out_be32(&p->mr0, r | EMAC_MR0_RXE);
  242. }
  243. out:
  244. ;
  245. }
  246. static void emac_rx_disable(struct emac_instance *dev)
  247. {
  248. struct emac_regs __iomem *p = dev->emacp;
  249. u32 r;
  250. DBG(dev, "rx_disable" NL);
  251. r = in_be32(&p->mr0);
  252. if (r & EMAC_MR0_RXE) {
  253. int n = dev->stop_timeout;
  254. out_be32(&p->mr0, r & ~EMAC_MR0_RXE);
  255. while (!(in_be32(&p->mr0) & EMAC_MR0_RXI) && n) {
  256. udelay(1);
  257. --n;
  258. }
  259. if (unlikely(!n))
  260. emac_report_timeout_error(dev, "RX disable timeout");
  261. }
  262. }
  263. static inline void emac_netif_stop(struct emac_instance *dev)
  264. {
  265. netif_tx_lock_bh(dev->ndev);
  266. netif_addr_lock(dev->ndev);
  267. dev->no_mcast = 1;
  268. netif_addr_unlock(dev->ndev);
  269. netif_tx_unlock_bh(dev->ndev);
  270. dev->ndev->trans_start = jiffies; /* prevent tx timeout */
  271. mal_poll_disable(dev->mal, &dev->commac);
  272. netif_tx_disable(dev->ndev);
  273. }
  274. static inline void emac_netif_start(struct emac_instance *dev)
  275. {
  276. netif_tx_lock_bh(dev->ndev);
  277. netif_addr_lock(dev->ndev);
  278. dev->no_mcast = 0;
  279. if (dev->mcast_pending && netif_running(dev->ndev))
  280. __emac_set_multicast_list(dev);
  281. netif_addr_unlock(dev->ndev);
  282. netif_tx_unlock_bh(dev->ndev);
  283. netif_wake_queue(dev->ndev);
  284. /* NOTE: unconditional netif_wake_queue is only appropriate
  285. * so long as all callers are assured to have free tx slots
  286. * (taken from tg3... though the case where that is wrong is
  287. * not terribly harmful)
  288. */
  289. mal_poll_enable(dev->mal, &dev->commac);
  290. }
  291. static inline void emac_rx_disable_async(struct emac_instance *dev)
  292. {
  293. struct emac_regs __iomem *p = dev->emacp;
  294. u32 r;
  295. DBG(dev, "rx_disable_async" NL);
  296. r = in_be32(&p->mr0);
  297. if (r & EMAC_MR0_RXE)
  298. out_be32(&p->mr0, r & ~EMAC_MR0_RXE);
  299. }
  300. static int emac_reset(struct emac_instance *dev)
  301. {
  302. struct emac_regs __iomem *p = dev->emacp;
  303. int n = 20;
  304. DBG(dev, "reset" NL);
  305. if (!dev->reset_failed) {
  306. /* 40x erratum suggests stopping RX channel before reset,
  307. * we stop TX as well
  308. */
  309. emac_rx_disable(dev);
  310. emac_tx_disable(dev);
  311. }
  312. #ifdef CONFIG_PPC_DCR_NATIVE
  313. /* Enable internal clock source */
  314. if (emac_has_feature(dev, EMAC_FTR_460EX_PHY_CLK_FIX))
  315. dcri_clrset(SDR0, SDR0_ETH_CFG,
  316. 0, SDR0_ETH_CFG_ECS << dev->cell_index);
  317. #endif
  318. out_be32(&p->mr0, EMAC_MR0_SRST);
  319. while ((in_be32(&p->mr0) & EMAC_MR0_SRST) && n)
  320. --n;
  321. #ifdef CONFIG_PPC_DCR_NATIVE
  322. /* Enable external clock source */
  323. if (emac_has_feature(dev, EMAC_FTR_460EX_PHY_CLK_FIX))
  324. dcri_clrset(SDR0, SDR0_ETH_CFG,
  325. SDR0_ETH_CFG_ECS << dev->cell_index, 0);
  326. #endif
  327. if (n) {
  328. dev->reset_failed = 0;
  329. return 0;
  330. } else {
  331. emac_report_timeout_error(dev, "reset timeout");
  332. dev->reset_failed = 1;
  333. return -ETIMEDOUT;
  334. }
  335. }
  336. static void emac_hash_mc(struct emac_instance *dev)
  337. {
  338. const int regs = EMAC_XAHT_REGS(dev);
  339. u32 *gaht_base = emac_gaht_base(dev);
  340. u32 gaht_temp[regs];
  341. struct netdev_hw_addr *ha;
  342. int i;
  343. DBG(dev, "hash_mc %d" NL, netdev_mc_count(dev->ndev));
  344. memset(gaht_temp, 0, sizeof (gaht_temp));
  345. netdev_for_each_mc_addr(ha, dev->ndev) {
  346. int slot, reg, mask;
  347. DBG2(dev, "mc %pM" NL, ha->addr);
  348. slot = EMAC_XAHT_CRC_TO_SLOT(dev,
  349. ether_crc(ETH_ALEN, ha->addr));
  350. reg = EMAC_XAHT_SLOT_TO_REG(dev, slot);
  351. mask = EMAC_XAHT_SLOT_TO_MASK(dev, slot);
  352. gaht_temp[reg] |= mask;
  353. }
  354. for (i = 0; i < regs; i++)
  355. out_be32(gaht_base + i, gaht_temp[i]);
  356. }
  357. static inline u32 emac_iff2rmr(struct net_device *ndev)
  358. {
  359. struct emac_instance *dev = netdev_priv(ndev);
  360. u32 r;
  361. r = EMAC_RMR_SP | EMAC_RMR_SFCS | EMAC_RMR_IAE | EMAC_RMR_BAE;
  362. if (emac_has_feature(dev, EMAC_FTR_EMAC4))
  363. r |= EMAC4_RMR_BASE;
  364. else
  365. r |= EMAC_RMR_BASE;
  366. if (ndev->flags & IFF_PROMISC)
  367. r |= EMAC_RMR_PME;
  368. else if (ndev->flags & IFF_ALLMULTI ||
  369. (netdev_mc_count(ndev) > EMAC_XAHT_SLOTS(dev)))
  370. r |= EMAC_RMR_PMME;
  371. else if (!netdev_mc_empty(ndev))
  372. r |= EMAC_RMR_MAE;
  373. return r;
  374. }
  375. static u32 __emac_calc_base_mr1(struct emac_instance *dev, int tx_size, int rx_size)
  376. {
  377. u32 ret = EMAC_MR1_VLE | EMAC_MR1_IST | EMAC_MR1_TR0_MULT;
  378. DBG2(dev, "__emac_calc_base_mr1" NL);
  379. switch(tx_size) {
  380. case 2048:
  381. ret |= EMAC_MR1_TFS_2K;
  382. break;
  383. default:
  384. printk(KERN_WARNING "%s: Unknown Tx FIFO size %d\n",
  385. dev->ndev->name, tx_size);
  386. }
  387. switch(rx_size) {
  388. case 16384:
  389. ret |= EMAC_MR1_RFS_16K;
  390. break;
  391. case 4096:
  392. ret |= EMAC_MR1_RFS_4K;
  393. break;
  394. default:
  395. printk(KERN_WARNING "%s: Unknown Rx FIFO size %d\n",
  396. dev->ndev->name, rx_size);
  397. }
  398. return ret;
  399. }
  400. static u32 __emac4_calc_base_mr1(struct emac_instance *dev, int tx_size, int rx_size)
  401. {
  402. u32 ret = EMAC_MR1_VLE | EMAC_MR1_IST | EMAC4_MR1_TR |
  403. EMAC4_MR1_OBCI(dev->opb_bus_freq / 1000000);
  404. DBG2(dev, "__emac4_calc_base_mr1" NL);
  405. switch(tx_size) {
  406. case 16384:
  407. ret |= EMAC4_MR1_TFS_16K;
  408. break;
  409. case 4096:
  410. ret |= EMAC4_MR1_TFS_4K;
  411. break;
  412. case 2048:
  413. ret |= EMAC4_MR1_TFS_2K;
  414. break;
  415. default:
  416. printk(KERN_WARNING "%s: Unknown Tx FIFO size %d\n",
  417. dev->ndev->name, tx_size);
  418. }
  419. switch(rx_size) {
  420. case 16384:
  421. ret |= EMAC4_MR1_RFS_16K;
  422. break;
  423. case 4096:
  424. ret |= EMAC4_MR1_RFS_4K;
  425. break;
  426. case 2048:
  427. ret |= EMAC4_MR1_RFS_2K;
  428. break;
  429. default:
  430. printk(KERN_WARNING "%s: Unknown Rx FIFO size %d\n",
  431. dev->ndev->name, rx_size);
  432. }
  433. return ret;
  434. }
  435. static u32 emac_calc_base_mr1(struct emac_instance *dev, int tx_size, int rx_size)
  436. {
  437. return emac_has_feature(dev, EMAC_FTR_EMAC4) ?
  438. __emac4_calc_base_mr1(dev, tx_size, rx_size) :
  439. __emac_calc_base_mr1(dev, tx_size, rx_size);
  440. }
  441. static inline u32 emac_calc_trtr(struct emac_instance *dev, unsigned int size)
  442. {
  443. if (emac_has_feature(dev, EMAC_FTR_EMAC4))
  444. return ((size >> 6) - 1) << EMAC_TRTR_SHIFT_EMAC4;
  445. else
  446. return ((size >> 6) - 1) << EMAC_TRTR_SHIFT;
  447. }
  448. static inline u32 emac_calc_rwmr(struct emac_instance *dev,
  449. unsigned int low, unsigned int high)
  450. {
  451. if (emac_has_feature(dev, EMAC_FTR_EMAC4))
  452. return (low << 22) | ( (high & 0x3ff) << 6);
  453. else
  454. return (low << 23) | ( (high & 0x1ff) << 7);
  455. }
  456. static int emac_configure(struct emac_instance *dev)
  457. {
  458. struct emac_regs __iomem *p = dev->emacp;
  459. struct net_device *ndev = dev->ndev;
  460. int tx_size, rx_size, link = netif_carrier_ok(dev->ndev);
  461. u32 r, mr1 = 0;
  462. DBG(dev, "configure" NL);
  463. if (!link) {
  464. out_be32(&p->mr1, in_be32(&p->mr1)
  465. | EMAC_MR1_FDE | EMAC_MR1_ILE);
  466. udelay(100);
  467. } else if (emac_reset(dev) < 0)
  468. return -ETIMEDOUT;
  469. if (emac_has_feature(dev, EMAC_FTR_HAS_TAH))
  470. tah_reset(dev->tah_dev);
  471. DBG(dev, " link = %d duplex = %d, pause = %d, asym_pause = %d\n",
  472. link, dev->phy.duplex, dev->phy.pause, dev->phy.asym_pause);
  473. /* Default fifo sizes */
  474. tx_size = dev->tx_fifo_size;
  475. rx_size = dev->rx_fifo_size;
  476. /* No link, force loopback */
  477. if (!link)
  478. mr1 = EMAC_MR1_FDE | EMAC_MR1_ILE;
  479. /* Check for full duplex */
  480. else if (dev->phy.duplex == DUPLEX_FULL)
  481. mr1 |= EMAC_MR1_FDE | EMAC_MR1_MWSW_001;
  482. /* Adjust fifo sizes, mr1 and timeouts based on link speed */
  483. dev->stop_timeout = STOP_TIMEOUT_10;
  484. switch (dev->phy.speed) {
  485. case SPEED_1000:
  486. if (emac_phy_gpcs(dev->phy.mode)) {
  487. mr1 |= EMAC_MR1_MF_1000GPCS | EMAC_MR1_MF_IPPA(
  488. (dev->phy.gpcs_address != 0xffffffff) ?
  489. dev->phy.gpcs_address : dev->phy.address);
  490. /* Put some arbitrary OUI, Manuf & Rev IDs so we can
  491. * identify this GPCS PHY later.
  492. */
  493. out_be32(&p->u1.emac4.ipcr, 0xdeadbeef);
  494. } else
  495. mr1 |= EMAC_MR1_MF_1000;
  496. /* Extended fifo sizes */
  497. tx_size = dev->tx_fifo_size_gige;
  498. rx_size = dev->rx_fifo_size_gige;
  499. if (dev->ndev->mtu > ETH_DATA_LEN) {
  500. if (emac_has_feature(dev, EMAC_FTR_EMAC4))
  501. mr1 |= EMAC4_MR1_JPSM;
  502. else
  503. mr1 |= EMAC_MR1_JPSM;
  504. dev->stop_timeout = STOP_TIMEOUT_1000_JUMBO;
  505. } else
  506. dev->stop_timeout = STOP_TIMEOUT_1000;
  507. break;
  508. case SPEED_100:
  509. mr1 |= EMAC_MR1_MF_100;
  510. dev->stop_timeout = STOP_TIMEOUT_100;
  511. break;
  512. default: /* make gcc happy */
  513. break;
  514. }
  515. if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII))
  516. rgmii_set_speed(dev->rgmii_dev, dev->rgmii_port,
  517. dev->phy.speed);
  518. if (emac_has_feature(dev, EMAC_FTR_HAS_ZMII))
  519. zmii_set_speed(dev->zmii_dev, dev->zmii_port, dev->phy.speed);
  520. /* on 40x erratum forces us to NOT use integrated flow control,
  521. * let's hope it works on 44x ;)
  522. */
  523. if (!emac_has_feature(dev, EMAC_FTR_NO_FLOW_CONTROL_40x) &&
  524. dev->phy.duplex == DUPLEX_FULL) {
  525. if (dev->phy.pause)
  526. mr1 |= EMAC_MR1_EIFC | EMAC_MR1_APP;
  527. else if (dev->phy.asym_pause)
  528. mr1 |= EMAC_MR1_APP;
  529. }
  530. /* Add base settings & fifo sizes & program MR1 */
  531. mr1 |= emac_calc_base_mr1(dev, tx_size, rx_size);
  532. out_be32(&p->mr1, mr1);
  533. /* Set individual MAC address */
  534. out_be32(&p->iahr, (ndev->dev_addr[0] << 8) | ndev->dev_addr[1]);
  535. out_be32(&p->ialr, (ndev->dev_addr[2] << 24) |
  536. (ndev->dev_addr[3] << 16) | (ndev->dev_addr[4] << 8) |
  537. ndev->dev_addr[5]);
  538. /* VLAN Tag Protocol ID */
  539. out_be32(&p->vtpid, 0x8100);
  540. /* Receive mode register */
  541. r = emac_iff2rmr(ndev);
  542. if (r & EMAC_RMR_MAE)
  543. emac_hash_mc(dev);
  544. out_be32(&p->rmr, r);
  545. /* FIFOs thresholds */
  546. if (emac_has_feature(dev, EMAC_FTR_EMAC4))
  547. r = EMAC4_TMR1((dev->mal_burst_size / dev->fifo_entry_size) + 1,
  548. tx_size / 2 / dev->fifo_entry_size);
  549. else
  550. r = EMAC_TMR1((dev->mal_burst_size / dev->fifo_entry_size) + 1,
  551. tx_size / 2 / dev->fifo_entry_size);
  552. out_be32(&p->tmr1, r);
  553. out_be32(&p->trtr, emac_calc_trtr(dev, tx_size / 2));
  554. /* PAUSE frame is sent when RX FIFO reaches its high-water mark,
  555. there should be still enough space in FIFO to allow the our link
  556. partner time to process this frame and also time to send PAUSE
  557. frame itself.
  558. Here is the worst case scenario for the RX FIFO "headroom"
  559. (from "The Switch Book") (100Mbps, without preamble, inter-frame gap):
  560. 1) One maximum-length frame on TX 1522 bytes
  561. 2) One PAUSE frame time 64 bytes
  562. 3) PAUSE frame decode time allowance 64 bytes
  563. 4) One maximum-length frame on RX 1522 bytes
  564. 5) Round-trip propagation delay of the link (100Mb) 15 bytes
  565. ----------
  566. 3187 bytes
  567. I chose to set high-water mark to RX_FIFO_SIZE / 4 (1024 bytes)
  568. low-water mark to RX_FIFO_SIZE / 8 (512 bytes)
  569. */
  570. r = emac_calc_rwmr(dev, rx_size / 8 / dev->fifo_entry_size,
  571. rx_size / 4 / dev->fifo_entry_size);
  572. out_be32(&p->rwmr, r);
  573. /* Set PAUSE timer to the maximum */
  574. out_be32(&p->ptr, 0xffff);
  575. /* IRQ sources */
  576. r = EMAC_ISR_OVR | EMAC_ISR_BP | EMAC_ISR_SE |
  577. EMAC_ISR_ALE | EMAC_ISR_BFCS | EMAC_ISR_PTLE | EMAC_ISR_ORE |
  578. EMAC_ISR_IRE | EMAC_ISR_TE;
  579. if (emac_has_feature(dev, EMAC_FTR_EMAC4))
  580. r |= EMAC4_ISR_TXPE | EMAC4_ISR_RXPE /* | EMAC4_ISR_TXUE |
  581. EMAC4_ISR_RXOE | */;
  582. out_be32(&p->iser, r);
  583. /* We need to take GPCS PHY out of isolate mode after EMAC reset */
  584. if (emac_phy_gpcs(dev->phy.mode)) {
  585. if (dev->phy.gpcs_address != 0xffffffff)
  586. emac_mii_reset_gpcs(&dev->phy);
  587. else
  588. emac_mii_reset_phy(&dev->phy);
  589. }
  590. return 0;
  591. }
  592. static void emac_reinitialize(struct emac_instance *dev)
  593. {
  594. DBG(dev, "reinitialize" NL);
  595. emac_netif_stop(dev);
  596. if (!emac_configure(dev)) {
  597. emac_tx_enable(dev);
  598. emac_rx_enable(dev);
  599. }
  600. emac_netif_start(dev);
  601. }
  602. static void emac_full_tx_reset(struct emac_instance *dev)
  603. {
  604. DBG(dev, "full_tx_reset" NL);
  605. emac_tx_disable(dev);
  606. mal_disable_tx_channel(dev->mal, dev->mal_tx_chan);
  607. emac_clean_tx_ring(dev);
  608. dev->tx_cnt = dev->tx_slot = dev->ack_slot = 0;
  609. emac_configure(dev);
  610. mal_enable_tx_channel(dev->mal, dev->mal_tx_chan);
  611. emac_tx_enable(dev);
  612. emac_rx_enable(dev);
  613. }
  614. static void emac_reset_work(struct work_struct *work)
  615. {
  616. struct emac_instance *dev = container_of(work, struct emac_instance, reset_work);
  617. DBG(dev, "reset_work" NL);
  618. mutex_lock(&dev->link_lock);
  619. if (dev->opened) {
  620. emac_netif_stop(dev);
  621. emac_full_tx_reset(dev);
  622. emac_netif_start(dev);
  623. }
  624. mutex_unlock(&dev->link_lock);
  625. }
  626. static void emac_tx_timeout(struct net_device *ndev)
  627. {
  628. struct emac_instance *dev = netdev_priv(ndev);
  629. DBG(dev, "tx_timeout" NL);
  630. schedule_work(&dev->reset_work);
  631. }
  632. static inline int emac_phy_done(struct emac_instance *dev, u32 stacr)
  633. {
  634. int done = !!(stacr & EMAC_STACR_OC);
  635. if (emac_has_feature(dev, EMAC_FTR_STACR_OC_INVERT))
  636. done = !done;
  637. return done;
  638. };
  639. static int __emac_mdio_read(struct emac_instance *dev, u8 id, u8 reg)
  640. {
  641. struct emac_regs __iomem *p = dev->emacp;
  642. u32 r = 0;
  643. int n, err = -ETIMEDOUT;
  644. mutex_lock(&dev->mdio_lock);
  645. DBG2(dev, "mdio_read(%02x,%02x)" NL, id, reg);
  646. /* Enable proper MDIO port */
  647. if (emac_has_feature(dev, EMAC_FTR_HAS_ZMII))
  648. zmii_get_mdio(dev->zmii_dev, dev->zmii_port);
  649. if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII))
  650. rgmii_get_mdio(dev->rgmii_dev, dev->rgmii_port);
  651. /* Wait for management interface to become idle */
  652. n = 20;
  653. while (!emac_phy_done(dev, in_be32(&p->stacr))) {
  654. udelay(1);
  655. if (!--n) {
  656. DBG2(dev, " -> timeout wait idle\n");
  657. goto bail;
  658. }
  659. }
  660. /* Issue read command */
  661. if (emac_has_feature(dev, EMAC_FTR_EMAC4))
  662. r = EMAC4_STACR_BASE(dev->opb_bus_freq);
  663. else
  664. r = EMAC_STACR_BASE(dev->opb_bus_freq);
  665. if (emac_has_feature(dev, EMAC_FTR_STACR_OC_INVERT))
  666. r |= EMAC_STACR_OC;
  667. if (emac_has_feature(dev, EMAC_FTR_HAS_NEW_STACR))
  668. r |= EMACX_STACR_STAC_READ;
  669. else
  670. r |= EMAC_STACR_STAC_READ;
  671. r |= (reg & EMAC_STACR_PRA_MASK)
  672. | ((id & EMAC_STACR_PCDA_MASK) << EMAC_STACR_PCDA_SHIFT);
  673. out_be32(&p->stacr, r);
  674. /* Wait for read to complete */
  675. n = 200;
  676. while (!emac_phy_done(dev, (r = in_be32(&p->stacr)))) {
  677. udelay(1);
  678. if (!--n) {
  679. DBG2(dev, " -> timeout wait complete\n");
  680. goto bail;
  681. }
  682. }
  683. if (unlikely(r & EMAC_STACR_PHYE)) {
  684. DBG(dev, "mdio_read(%02x, %02x) failed" NL, id, reg);
  685. err = -EREMOTEIO;
  686. goto bail;
  687. }
  688. r = ((r >> EMAC_STACR_PHYD_SHIFT) & EMAC_STACR_PHYD_MASK);
  689. DBG2(dev, "mdio_read -> %04x" NL, r);
  690. err = 0;
  691. bail:
  692. if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII))
  693. rgmii_put_mdio(dev->rgmii_dev, dev->rgmii_port);
  694. if (emac_has_feature(dev, EMAC_FTR_HAS_ZMII))
  695. zmii_put_mdio(dev->zmii_dev, dev->zmii_port);
  696. mutex_unlock(&dev->mdio_lock);
  697. return err == 0 ? r : err;
  698. }
  699. static void __emac_mdio_write(struct emac_instance *dev, u8 id, u8 reg,
  700. u16 val)
  701. {
  702. struct emac_regs __iomem *p = dev->emacp;
  703. u32 r = 0;
  704. int n, err = -ETIMEDOUT;
  705. mutex_lock(&dev->mdio_lock);
  706. DBG2(dev, "mdio_write(%02x,%02x,%04x)" NL, id, reg, val);
  707. /* Enable proper MDIO port */
  708. if (emac_has_feature(dev, EMAC_FTR_HAS_ZMII))
  709. zmii_get_mdio(dev->zmii_dev, dev->zmii_port);
  710. if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII))
  711. rgmii_get_mdio(dev->rgmii_dev, dev->rgmii_port);
  712. /* Wait for management interface to be idle */
  713. n = 20;
  714. while (!emac_phy_done(dev, in_be32(&p->stacr))) {
  715. udelay(1);
  716. if (!--n) {
  717. DBG2(dev, " -> timeout wait idle\n");
  718. goto bail;
  719. }
  720. }
  721. /* Issue write command */
  722. if (emac_has_feature(dev, EMAC_FTR_EMAC4))
  723. r = EMAC4_STACR_BASE(dev->opb_bus_freq);
  724. else
  725. r = EMAC_STACR_BASE(dev->opb_bus_freq);
  726. if (emac_has_feature(dev, EMAC_FTR_STACR_OC_INVERT))
  727. r |= EMAC_STACR_OC;
  728. if (emac_has_feature(dev, EMAC_FTR_HAS_NEW_STACR))
  729. r |= EMACX_STACR_STAC_WRITE;
  730. else
  731. r |= EMAC_STACR_STAC_WRITE;
  732. r |= (reg & EMAC_STACR_PRA_MASK) |
  733. ((id & EMAC_STACR_PCDA_MASK) << EMAC_STACR_PCDA_SHIFT) |
  734. (val << EMAC_STACR_PHYD_SHIFT);
  735. out_be32(&p->stacr, r);
  736. /* Wait for write to complete */
  737. n = 200;
  738. while (!emac_phy_done(dev, in_be32(&p->stacr))) {
  739. udelay(1);
  740. if (!--n) {
  741. DBG2(dev, " -> timeout wait complete\n");
  742. goto bail;
  743. }
  744. }
  745. err = 0;
  746. bail:
  747. if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII))
  748. rgmii_put_mdio(dev->rgmii_dev, dev->rgmii_port);
  749. if (emac_has_feature(dev, EMAC_FTR_HAS_ZMII))
  750. zmii_put_mdio(dev->zmii_dev, dev->zmii_port);
  751. mutex_unlock(&dev->mdio_lock);
  752. }
  753. static int emac_mdio_read(struct net_device *ndev, int id, int reg)
  754. {
  755. struct emac_instance *dev = netdev_priv(ndev);
  756. int res;
  757. res = __emac_mdio_read((dev->mdio_instance &&
  758. dev->phy.gpcs_address != id) ?
  759. dev->mdio_instance : dev,
  760. (u8) id, (u8) reg);
  761. return res;
  762. }
  763. static void emac_mdio_write(struct net_device *ndev, int id, int reg, int val)
  764. {
  765. struct emac_instance *dev = netdev_priv(ndev);
  766. __emac_mdio_write((dev->mdio_instance &&
  767. dev->phy.gpcs_address != id) ?
  768. dev->mdio_instance : dev,
  769. (u8) id, (u8) reg, (u16) val);
  770. }
  771. /* Tx lock BH */
  772. static void __emac_set_multicast_list(struct emac_instance *dev)
  773. {
  774. struct emac_regs __iomem *p = dev->emacp;
  775. u32 rmr = emac_iff2rmr(dev->ndev);
  776. DBG(dev, "__multicast %08x" NL, rmr);
  777. /* I decided to relax register access rules here to avoid
  778. * full EMAC reset.
  779. *
  780. * There is a real problem with EMAC4 core if we use MWSW_001 bit
  781. * in MR1 register and do a full EMAC reset.
  782. * One TX BD status update is delayed and, after EMAC reset, it
  783. * never happens, resulting in TX hung (it'll be recovered by TX
  784. * timeout handler eventually, but this is just gross).
  785. * So we either have to do full TX reset or try to cheat here :)
  786. *
  787. * The only required change is to RX mode register, so I *think* all
  788. * we need is just to stop RX channel. This seems to work on all
  789. * tested SoCs. --ebs
  790. *
  791. * If we need the full reset, we might just trigger the workqueue
  792. * and do it async... a bit nasty but should work --BenH
  793. */
  794. dev->mcast_pending = 0;
  795. emac_rx_disable(dev);
  796. if (rmr & EMAC_RMR_MAE)
  797. emac_hash_mc(dev);
  798. out_be32(&p->rmr, rmr);
  799. emac_rx_enable(dev);
  800. }
  801. /* Tx lock BH */
  802. static void emac_set_multicast_list(struct net_device *ndev)
  803. {
  804. struct emac_instance *dev = netdev_priv(ndev);
  805. DBG(dev, "multicast" NL);
  806. BUG_ON(!netif_running(dev->ndev));
  807. if (dev->no_mcast) {
  808. dev->mcast_pending = 1;
  809. return;
  810. }
  811. __emac_set_multicast_list(dev);
  812. }
  813. static int emac_resize_rx_ring(struct emac_instance *dev, int new_mtu)
  814. {
  815. int rx_sync_size = emac_rx_sync_size(new_mtu);
  816. int rx_skb_size = emac_rx_skb_size(new_mtu);
  817. int i, ret = 0;
  818. mutex_lock(&dev->link_lock);
  819. emac_netif_stop(dev);
  820. emac_rx_disable(dev);
  821. mal_disable_rx_channel(dev->mal, dev->mal_rx_chan);
  822. if (dev->rx_sg_skb) {
  823. ++dev->estats.rx_dropped_resize;
  824. dev_kfree_skb(dev->rx_sg_skb);
  825. dev->rx_sg_skb = NULL;
  826. }
  827. /* Make a first pass over RX ring and mark BDs ready, dropping
  828. * non-processed packets on the way. We need this as a separate pass
  829. * to simplify error recovery in the case of allocation failure later.
  830. */
  831. for (i = 0; i < NUM_RX_BUFF; ++i) {
  832. if (dev->rx_desc[i].ctrl & MAL_RX_CTRL_FIRST)
  833. ++dev->estats.rx_dropped_resize;
  834. dev->rx_desc[i].data_len = 0;
  835. dev->rx_desc[i].ctrl = MAL_RX_CTRL_EMPTY |
  836. (i == (NUM_RX_BUFF - 1) ? MAL_RX_CTRL_WRAP : 0);
  837. }
  838. /* Reallocate RX ring only if bigger skb buffers are required */
  839. if (rx_skb_size <= dev->rx_skb_size)
  840. goto skip;
  841. /* Second pass, allocate new skbs */
  842. for (i = 0; i < NUM_RX_BUFF; ++i) {
  843. struct sk_buff *skb = alloc_skb(rx_skb_size, GFP_ATOMIC);
  844. if (!skb) {
  845. ret = -ENOMEM;
  846. goto oom;
  847. }
  848. BUG_ON(!dev->rx_skb[i]);
  849. dev_kfree_skb(dev->rx_skb[i]);
  850. skb_reserve(skb, EMAC_RX_SKB_HEADROOM + 2);
  851. dev->rx_desc[i].data_ptr =
  852. dma_map_single(&dev->ofdev->dev, skb->data - 2, rx_sync_size,
  853. DMA_FROM_DEVICE) + 2;
  854. dev->rx_skb[i] = skb;
  855. }
  856. skip:
  857. /* Check if we need to change "Jumbo" bit in MR1 */
  858. if ((new_mtu > ETH_DATA_LEN) ^ (dev->ndev->mtu > ETH_DATA_LEN)) {
  859. /* This is to prevent starting RX channel in emac_rx_enable() */
  860. set_bit(MAL_COMMAC_RX_STOPPED, &dev->commac.flags);
  861. dev->ndev->mtu = new_mtu;
  862. emac_full_tx_reset(dev);
  863. }
  864. mal_set_rcbs(dev->mal, dev->mal_rx_chan, emac_rx_size(new_mtu));
  865. oom:
  866. /* Restart RX */
  867. clear_bit(MAL_COMMAC_RX_STOPPED, &dev->commac.flags);
  868. dev->rx_slot = 0;
  869. mal_enable_rx_channel(dev->mal, dev->mal_rx_chan);
  870. emac_rx_enable(dev);
  871. emac_netif_start(dev);
  872. mutex_unlock(&dev->link_lock);
  873. return ret;
  874. }
  875. /* Process ctx, rtnl_lock semaphore */
  876. static int emac_change_mtu(struct net_device *ndev, int new_mtu)
  877. {
  878. struct emac_instance *dev = netdev_priv(ndev);
  879. int ret = 0;
  880. if (new_mtu < EMAC_MIN_MTU || new_mtu > dev->max_mtu)
  881. return -EINVAL;
  882. DBG(dev, "change_mtu(%d)" NL, new_mtu);
  883. if (netif_running(ndev)) {
  884. /* Check if we really need to reinitialize RX ring */
  885. if (emac_rx_skb_size(ndev->mtu) != emac_rx_skb_size(new_mtu))
  886. ret = emac_resize_rx_ring(dev, new_mtu);
  887. }
  888. if (!ret) {
  889. ndev->mtu = new_mtu;
  890. dev->rx_skb_size = emac_rx_skb_size(new_mtu);
  891. dev->rx_sync_size = emac_rx_sync_size(new_mtu);
  892. }
  893. return ret;
  894. }
  895. static void emac_clean_tx_ring(struct emac_instance *dev)
  896. {
  897. int i;
  898. for (i = 0; i < NUM_TX_BUFF; ++i) {
  899. if (dev->tx_skb[i]) {
  900. dev_kfree_skb(dev->tx_skb[i]);
  901. dev->tx_skb[i] = NULL;
  902. if (dev->tx_desc[i].ctrl & MAL_TX_CTRL_READY)
  903. ++dev->estats.tx_dropped;
  904. }
  905. dev->tx_desc[i].ctrl = 0;
  906. dev->tx_desc[i].data_ptr = 0;
  907. }
  908. }
  909. static void emac_clean_rx_ring(struct emac_instance *dev)
  910. {
  911. int i;
  912. for (i = 0; i < NUM_RX_BUFF; ++i)
  913. if (dev->rx_skb[i]) {
  914. dev->rx_desc[i].ctrl = 0;
  915. dev_kfree_skb(dev->rx_skb[i]);
  916. dev->rx_skb[i] = NULL;
  917. dev->rx_desc[i].data_ptr = 0;
  918. }
  919. if (dev->rx_sg_skb) {
  920. dev_kfree_skb(dev->rx_sg_skb);
  921. dev->rx_sg_skb = NULL;
  922. }
  923. }
  924. static inline int emac_alloc_rx_skb(struct emac_instance *dev, int slot,
  925. gfp_t flags)
  926. {
  927. struct sk_buff *skb = alloc_skb(dev->rx_skb_size, flags);
  928. if (unlikely(!skb))
  929. return -ENOMEM;
  930. dev->rx_skb[slot] = skb;
  931. dev->rx_desc[slot].data_len = 0;
  932. skb_reserve(skb, EMAC_RX_SKB_HEADROOM + 2);
  933. dev->rx_desc[slot].data_ptr =
  934. dma_map_single(&dev->ofdev->dev, skb->data - 2, dev->rx_sync_size,
  935. DMA_FROM_DEVICE) + 2;
  936. wmb();
  937. dev->rx_desc[slot].ctrl = MAL_RX_CTRL_EMPTY |
  938. (slot == (NUM_RX_BUFF - 1) ? MAL_RX_CTRL_WRAP : 0);
  939. return 0;
  940. }
  941. static void emac_print_link_status(struct emac_instance *dev)
  942. {
  943. if (netif_carrier_ok(dev->ndev))
  944. printk(KERN_INFO "%s: link is up, %d %s%s\n",
  945. dev->ndev->name, dev->phy.speed,
  946. dev->phy.duplex == DUPLEX_FULL ? "FDX" : "HDX",
  947. dev->phy.pause ? ", pause enabled" :
  948. dev->phy.asym_pause ? ", asymmetric pause enabled" : "");
  949. else
  950. printk(KERN_INFO "%s: link is down\n", dev->ndev->name);
  951. }
  952. /* Process ctx, rtnl_lock semaphore */
  953. static int emac_open(struct net_device *ndev)
  954. {
  955. struct emac_instance *dev = netdev_priv(ndev);
  956. int err, i;
  957. DBG(dev, "open" NL);
  958. /* Setup error IRQ handler */
  959. err = request_irq(dev->emac_irq, emac_irq, 0, "EMAC", dev);
  960. if (err) {
  961. printk(KERN_ERR "%s: failed to request IRQ %d\n",
  962. ndev->name, dev->emac_irq);
  963. return err;
  964. }
  965. /* Allocate RX ring */
  966. for (i = 0; i < NUM_RX_BUFF; ++i)
  967. if (emac_alloc_rx_skb(dev, i, GFP_KERNEL)) {
  968. printk(KERN_ERR "%s: failed to allocate RX ring\n",
  969. ndev->name);
  970. goto oom;
  971. }
  972. dev->tx_cnt = dev->tx_slot = dev->ack_slot = dev->rx_slot = 0;
  973. clear_bit(MAL_COMMAC_RX_STOPPED, &dev->commac.flags);
  974. dev->rx_sg_skb = NULL;
  975. mutex_lock(&dev->link_lock);
  976. dev->opened = 1;
  977. /* Start PHY polling now.
  978. */
  979. if (dev->phy.address >= 0) {
  980. int link_poll_interval;
  981. if (dev->phy.def->ops->poll_link(&dev->phy)) {
  982. dev->phy.def->ops->read_link(&dev->phy);
  983. emac_rx_clk_default(dev);
  984. netif_carrier_on(dev->ndev);
  985. link_poll_interval = PHY_POLL_LINK_ON;
  986. } else {
  987. emac_rx_clk_tx(dev);
  988. netif_carrier_off(dev->ndev);
  989. link_poll_interval = PHY_POLL_LINK_OFF;
  990. }
  991. dev->link_polling = 1;
  992. wmb();
  993. schedule_delayed_work(&dev->link_work, link_poll_interval);
  994. emac_print_link_status(dev);
  995. } else
  996. netif_carrier_on(dev->ndev);
  997. /* Required for Pause packet support in EMAC */
  998. dev_mc_add_global(ndev, default_mcast_addr);
  999. emac_configure(dev);
  1000. mal_poll_add(dev->mal, &dev->commac);
  1001. mal_enable_tx_channel(dev->mal, dev->mal_tx_chan);
  1002. mal_set_rcbs(dev->mal, dev->mal_rx_chan, emac_rx_size(ndev->mtu));
  1003. mal_enable_rx_channel(dev->mal, dev->mal_rx_chan);
  1004. emac_tx_enable(dev);
  1005. emac_rx_enable(dev);
  1006. emac_netif_start(dev);
  1007. mutex_unlock(&dev->link_lock);
  1008. return 0;
  1009. oom:
  1010. emac_clean_rx_ring(dev);
  1011. free_irq(dev->emac_irq, dev);
  1012. return -ENOMEM;
  1013. }
  1014. /* BHs disabled */
  1015. #if 0
  1016. static int emac_link_differs(struct emac_instance *dev)
  1017. {
  1018. u32 r = in_be32(&dev->emacp->mr1);
  1019. int duplex = r & EMAC_MR1_FDE ? DUPLEX_FULL : DUPLEX_HALF;
  1020. int speed, pause, asym_pause;
  1021. if (r & EMAC_MR1_MF_1000)
  1022. speed = SPEED_1000;
  1023. else if (r & EMAC_MR1_MF_100)
  1024. speed = SPEED_100;
  1025. else
  1026. speed = SPEED_10;
  1027. switch (r & (EMAC_MR1_EIFC | EMAC_MR1_APP)) {
  1028. case (EMAC_MR1_EIFC | EMAC_MR1_APP):
  1029. pause = 1;
  1030. asym_pause = 0;
  1031. break;
  1032. case EMAC_MR1_APP:
  1033. pause = 0;
  1034. asym_pause = 1;
  1035. break;
  1036. default:
  1037. pause = asym_pause = 0;
  1038. }
  1039. return speed != dev->phy.speed || duplex != dev->phy.duplex ||
  1040. pause != dev->phy.pause || asym_pause != dev->phy.asym_pause;
  1041. }
  1042. #endif
  1043. static void emac_link_timer(struct work_struct *work)
  1044. {
  1045. struct emac_instance *dev =
  1046. container_of(to_delayed_work(work),
  1047. struct emac_instance, link_work);
  1048. int link_poll_interval;
  1049. mutex_lock(&dev->link_lock);
  1050. DBG2(dev, "link timer" NL);
  1051. if (!dev->opened)
  1052. goto bail;
  1053. if (dev->phy.def->ops->poll_link(&dev->phy)) {
  1054. if (!netif_carrier_ok(dev->ndev)) {
  1055. emac_rx_clk_default(dev);
  1056. /* Get new link parameters */
  1057. dev->phy.def->ops->read_link(&dev->phy);
  1058. netif_carrier_on(dev->ndev);
  1059. emac_netif_stop(dev);
  1060. emac_full_tx_reset(dev);
  1061. emac_netif_start(dev);
  1062. emac_print_link_status(dev);
  1063. }
  1064. link_poll_interval = PHY_POLL_LINK_ON;
  1065. } else {
  1066. if (netif_carrier_ok(dev->ndev)) {
  1067. emac_rx_clk_tx(dev);
  1068. netif_carrier_off(dev->ndev);
  1069. netif_tx_disable(dev->ndev);
  1070. emac_reinitialize(dev);
  1071. emac_print_link_status(dev);
  1072. }
  1073. link_poll_interval = PHY_POLL_LINK_OFF;
  1074. }
  1075. schedule_delayed_work(&dev->link_work, link_poll_interval);
  1076. bail:
  1077. mutex_unlock(&dev->link_lock);
  1078. }
  1079. static void emac_force_link_update(struct emac_instance *dev)
  1080. {
  1081. netif_carrier_off(dev->ndev);
  1082. smp_rmb();
  1083. if (dev->link_polling) {
  1084. cancel_delayed_work_sync(&dev->link_work);
  1085. if (dev->link_polling)
  1086. schedule_delayed_work(&dev->link_work, PHY_POLL_LINK_OFF);
  1087. }
  1088. }
  1089. /* Process ctx, rtnl_lock semaphore */
  1090. static int emac_close(struct net_device *ndev)
  1091. {
  1092. struct emac_instance *dev = netdev_priv(ndev);
  1093. DBG(dev, "close" NL);
  1094. if (dev->phy.address >= 0) {
  1095. dev->link_polling = 0;
  1096. cancel_delayed_work_sync(&dev->link_work);
  1097. }
  1098. mutex_lock(&dev->link_lock);
  1099. emac_netif_stop(dev);
  1100. dev->opened = 0;
  1101. mutex_unlock(&dev->link_lock);
  1102. emac_rx_disable(dev);
  1103. emac_tx_disable(dev);
  1104. mal_disable_rx_channel(dev->mal, dev->mal_rx_chan);
  1105. mal_disable_tx_channel(dev->mal, dev->mal_tx_chan);
  1106. mal_poll_del(dev->mal, &dev->commac);
  1107. emac_clean_tx_ring(dev);
  1108. emac_clean_rx_ring(dev);
  1109. free_irq(dev->emac_irq, dev);
  1110. netif_carrier_off(ndev);
  1111. return 0;
  1112. }
  1113. static inline u16 emac_tx_csum(struct emac_instance *dev,
  1114. struct sk_buff *skb)
  1115. {
  1116. if (emac_has_feature(dev, EMAC_FTR_HAS_TAH) &&
  1117. (skb->ip_summed == CHECKSUM_PARTIAL)) {
  1118. ++dev->stats.tx_packets_csum;
  1119. return EMAC_TX_CTRL_TAH_CSUM;
  1120. }
  1121. return 0;
  1122. }
  1123. static inline int emac_xmit_finish(struct emac_instance *dev, int len)
  1124. {
  1125. struct emac_regs __iomem *p = dev->emacp;
  1126. struct net_device *ndev = dev->ndev;
  1127. /* Send the packet out. If the if makes a significant perf
  1128. * difference, then we can store the TMR0 value in "dev"
  1129. * instead
  1130. */
  1131. if (emac_has_feature(dev, EMAC_FTR_EMAC4))
  1132. out_be32(&p->tmr0, EMAC4_TMR0_XMIT);
  1133. else
  1134. out_be32(&p->tmr0, EMAC_TMR0_XMIT);
  1135. if (unlikely(++dev->tx_cnt == NUM_TX_BUFF)) {
  1136. netif_stop_queue(ndev);
  1137. DBG2(dev, "stopped TX queue" NL);
  1138. }
  1139. ndev->trans_start = jiffies;
  1140. ++dev->stats.tx_packets;
  1141. dev->stats.tx_bytes += len;
  1142. return NETDEV_TX_OK;
  1143. }
  1144. /* Tx lock BH */
  1145. static int emac_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  1146. {
  1147. struct emac_instance *dev = netdev_priv(ndev);
  1148. unsigned int len = skb->len;
  1149. int slot;
  1150. u16 ctrl = EMAC_TX_CTRL_GFCS | EMAC_TX_CTRL_GP | MAL_TX_CTRL_READY |
  1151. MAL_TX_CTRL_LAST | emac_tx_csum(dev, skb);
  1152. slot = dev->tx_slot++;
  1153. if (dev->tx_slot == NUM_TX_BUFF) {
  1154. dev->tx_slot = 0;
  1155. ctrl |= MAL_TX_CTRL_WRAP;
  1156. }
  1157. DBG2(dev, "xmit(%u) %d" NL, len, slot);
  1158. dev->tx_skb[slot] = skb;
  1159. dev->tx_desc[slot].data_ptr = dma_map_single(&dev->ofdev->dev,
  1160. skb->data, len,
  1161. DMA_TO_DEVICE);
  1162. dev->tx_desc[slot].data_len = (u16) len;
  1163. wmb();
  1164. dev->tx_desc[slot].ctrl = ctrl;
  1165. return emac_xmit_finish(dev, len);
  1166. }
  1167. static inline int emac_xmit_split(struct emac_instance *dev, int slot,
  1168. u32 pd, int len, int last, u16 base_ctrl)
  1169. {
  1170. while (1) {
  1171. u16 ctrl = base_ctrl;
  1172. int chunk = min(len, MAL_MAX_TX_SIZE);
  1173. len -= chunk;
  1174. slot = (slot + 1) % NUM_TX_BUFF;
  1175. if (last && !len)
  1176. ctrl |= MAL_TX_CTRL_LAST;
  1177. if (slot == NUM_TX_BUFF - 1)
  1178. ctrl |= MAL_TX_CTRL_WRAP;
  1179. dev->tx_skb[slot] = NULL;
  1180. dev->tx_desc[slot].data_ptr = pd;
  1181. dev->tx_desc[slot].data_len = (u16) chunk;
  1182. dev->tx_desc[slot].ctrl = ctrl;
  1183. ++dev->tx_cnt;
  1184. if (!len)
  1185. break;
  1186. pd += chunk;
  1187. }
  1188. return slot;
  1189. }
  1190. /* Tx lock BH disabled (SG version for TAH equipped EMACs) */
  1191. static int emac_start_xmit_sg(struct sk_buff *skb, struct net_device *ndev)
  1192. {
  1193. struct emac_instance *dev = netdev_priv(ndev);
  1194. int nr_frags = skb_shinfo(skb)->nr_frags;
  1195. int len = skb->len, chunk;
  1196. int slot, i;
  1197. u16 ctrl;
  1198. u32 pd;
  1199. /* This is common "fast" path */
  1200. if (likely(!nr_frags && len <= MAL_MAX_TX_SIZE))
  1201. return emac_start_xmit(skb, ndev);
  1202. len -= skb->data_len;
  1203. /* Note, this is only an *estimation*, we can still run out of empty
  1204. * slots because of the additional fragmentation into
  1205. * MAL_MAX_TX_SIZE-sized chunks
  1206. */
  1207. if (unlikely(dev->tx_cnt + nr_frags + mal_tx_chunks(len) > NUM_TX_BUFF))
  1208. goto stop_queue;
  1209. ctrl = EMAC_TX_CTRL_GFCS | EMAC_TX_CTRL_GP | MAL_TX_CTRL_READY |
  1210. emac_tx_csum(dev, skb);
  1211. slot = dev->tx_slot;
  1212. /* skb data */
  1213. dev->tx_skb[slot] = NULL;
  1214. chunk = min(len, MAL_MAX_TX_SIZE);
  1215. dev->tx_desc[slot].data_ptr = pd =
  1216. dma_map_single(&dev->ofdev->dev, skb->data, len, DMA_TO_DEVICE);
  1217. dev->tx_desc[slot].data_len = (u16) chunk;
  1218. len -= chunk;
  1219. if (unlikely(len))
  1220. slot = emac_xmit_split(dev, slot, pd + chunk, len, !nr_frags,
  1221. ctrl);
  1222. /* skb fragments */
  1223. for (i = 0; i < nr_frags; ++i) {
  1224. struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
  1225. len = frag->size;
  1226. if (unlikely(dev->tx_cnt + mal_tx_chunks(len) >= NUM_TX_BUFF))
  1227. goto undo_frame;
  1228. pd = dma_map_page(&dev->ofdev->dev, frag->page, frag->page_offset, len,
  1229. DMA_TO_DEVICE);
  1230. slot = emac_xmit_split(dev, slot, pd, len, i == nr_frags - 1,
  1231. ctrl);
  1232. }
  1233. DBG2(dev, "xmit_sg(%u) %d - %d" NL, skb->len, dev->tx_slot, slot);
  1234. /* Attach skb to the last slot so we don't release it too early */
  1235. dev->tx_skb[slot] = skb;
  1236. /* Send the packet out */
  1237. if (dev->tx_slot == NUM_TX_BUFF - 1)
  1238. ctrl |= MAL_TX_CTRL_WRAP;
  1239. wmb();
  1240. dev->tx_desc[dev->tx_slot].ctrl = ctrl;
  1241. dev->tx_slot = (slot + 1) % NUM_TX_BUFF;
  1242. return emac_xmit_finish(dev, skb->len);
  1243. undo_frame:
  1244. /* Well, too bad. Our previous estimation was overly optimistic.
  1245. * Undo everything.
  1246. */
  1247. while (slot != dev->tx_slot) {
  1248. dev->tx_desc[slot].ctrl = 0;
  1249. --dev->tx_cnt;
  1250. if (--slot < 0)
  1251. slot = NUM_TX_BUFF - 1;
  1252. }
  1253. ++dev->estats.tx_undo;
  1254. stop_queue:
  1255. netif_stop_queue(ndev);
  1256. DBG2(dev, "stopped TX queue" NL);
  1257. return NETDEV_TX_BUSY;
  1258. }
  1259. /* Tx lock BHs */
  1260. static void emac_parse_tx_error(struct emac_instance *dev, u16 ctrl)
  1261. {
  1262. struct emac_error_stats *st = &dev->estats;
  1263. DBG(dev, "BD TX error %04x" NL, ctrl);
  1264. ++st->tx_bd_errors;
  1265. if (ctrl & EMAC_TX_ST_BFCS)
  1266. ++st->tx_bd_bad_fcs;
  1267. if (ctrl & EMAC_TX_ST_LCS)
  1268. ++st->tx_bd_carrier_loss;
  1269. if (ctrl & EMAC_TX_ST_ED)
  1270. ++st->tx_bd_excessive_deferral;
  1271. if (ctrl & EMAC_TX_ST_EC)
  1272. ++st->tx_bd_excessive_collisions;
  1273. if (ctrl & EMAC_TX_ST_LC)
  1274. ++st->tx_bd_late_collision;
  1275. if (ctrl & EMAC_TX_ST_MC)
  1276. ++st->tx_bd_multple_collisions;
  1277. if (ctrl & EMAC_TX_ST_SC)
  1278. ++st->tx_bd_single_collision;
  1279. if (ctrl & EMAC_TX_ST_UR)
  1280. ++st->tx_bd_underrun;
  1281. if (ctrl & EMAC_TX_ST_SQE)
  1282. ++st->tx_bd_sqe;
  1283. }
  1284. static void emac_poll_tx(void *param)
  1285. {
  1286. struct emac_instance *dev = param;
  1287. u32 bad_mask;
  1288. DBG2(dev, "poll_tx, %d %d" NL, dev->tx_cnt, dev->ack_slot);
  1289. if (emac_has_feature(dev, EMAC_FTR_HAS_TAH))
  1290. bad_mask = EMAC_IS_BAD_TX_TAH;
  1291. else
  1292. bad_mask = EMAC_IS_BAD_TX;
  1293. netif_tx_lock_bh(dev->ndev);
  1294. if (dev->tx_cnt) {
  1295. u16 ctrl;
  1296. int slot = dev->ack_slot, n = 0;
  1297. again:
  1298. ctrl = dev->tx_desc[slot].ctrl;
  1299. if (!(ctrl & MAL_TX_CTRL_READY)) {
  1300. struct sk_buff *skb = dev->tx_skb[slot];
  1301. ++n;
  1302. if (skb) {
  1303. dev_kfree_skb(skb);
  1304. dev->tx_skb[slot] = NULL;
  1305. }
  1306. slot = (slot + 1) % NUM_TX_BUFF;
  1307. if (unlikely(ctrl & bad_mask))
  1308. emac_parse_tx_error(dev, ctrl);
  1309. if (--dev->tx_cnt)
  1310. goto again;
  1311. }
  1312. if (n) {
  1313. dev->ack_slot = slot;
  1314. if (netif_queue_stopped(dev->ndev) &&
  1315. dev->tx_cnt < EMAC_TX_WAKEUP_THRESH)
  1316. netif_wake_queue(dev->ndev);
  1317. DBG2(dev, "tx %d pkts" NL, n);
  1318. }
  1319. }
  1320. netif_tx_unlock_bh(dev->ndev);
  1321. }
  1322. static inline void emac_recycle_rx_skb(struct emac_instance *dev, int slot,
  1323. int len)
  1324. {
  1325. struct sk_buff *skb = dev->rx_skb[slot];
  1326. DBG2(dev, "recycle %d %d" NL, slot, len);
  1327. if (len)
  1328. dma_map_single(&dev->ofdev->dev, skb->data - 2,
  1329. EMAC_DMA_ALIGN(len + 2), DMA_FROM_DEVICE);
  1330. dev->rx_desc[slot].data_len = 0;
  1331. wmb();
  1332. dev->rx_desc[slot].ctrl = MAL_RX_CTRL_EMPTY |
  1333. (slot == (NUM_RX_BUFF - 1) ? MAL_RX_CTRL_WRAP : 0);
  1334. }
  1335. static void emac_parse_rx_error(struct emac_instance *dev, u16 ctrl)
  1336. {
  1337. struct emac_error_stats *st = &dev->estats;
  1338. DBG(dev, "BD RX error %04x" NL, ctrl);
  1339. ++st->rx_bd_errors;
  1340. if (ctrl & EMAC_RX_ST_OE)
  1341. ++st->rx_bd_overrun;
  1342. if (ctrl & EMAC_RX_ST_BP)
  1343. ++st->rx_bd_bad_packet;
  1344. if (ctrl & EMAC_RX_ST_RP)
  1345. ++st->rx_bd_runt_packet;
  1346. if (ctrl & EMAC_RX_ST_SE)
  1347. ++st->rx_bd_short_event;
  1348. if (ctrl & EMAC_RX_ST_AE)
  1349. ++st->rx_bd_alignment_error;
  1350. if (ctrl & EMAC_RX_ST_BFCS)
  1351. ++st->rx_bd_bad_fcs;
  1352. if (ctrl & EMAC_RX_ST_PTL)
  1353. ++st->rx_bd_packet_too_long;
  1354. if (ctrl & EMAC_RX_ST_ORE)
  1355. ++st->rx_bd_out_of_range;
  1356. if (ctrl & EMAC_RX_ST_IRE)
  1357. ++st->rx_bd_in_range;
  1358. }
  1359. static inline void emac_rx_csum(struct emac_instance *dev,
  1360. struct sk_buff *skb, u16 ctrl)
  1361. {
  1362. #ifdef CONFIG_IBM_NEW_EMAC_TAH
  1363. if (!ctrl && dev->tah_dev) {
  1364. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1365. ++dev->stats.rx_packets_csum;
  1366. }
  1367. #endif
  1368. }
  1369. static inline int emac_rx_sg_append(struct emac_instance *dev, int slot)
  1370. {
  1371. if (likely(dev->rx_sg_skb != NULL)) {
  1372. int len = dev->rx_desc[slot].data_len;
  1373. int tot_len = dev->rx_sg_skb->len + len;
  1374. if (unlikely(tot_len + 2 > dev->rx_skb_size)) {
  1375. ++dev->estats.rx_dropped_mtu;
  1376. dev_kfree_skb(dev->rx_sg_skb);
  1377. dev->rx_sg_skb = NULL;
  1378. } else {
  1379. cacheable_memcpy(skb_tail_pointer(dev->rx_sg_skb),
  1380. dev->rx_skb[slot]->data, len);
  1381. skb_put(dev->rx_sg_skb, len);
  1382. emac_recycle_rx_skb(dev, slot, len);
  1383. return 0;
  1384. }
  1385. }
  1386. emac_recycle_rx_skb(dev, slot, 0);
  1387. return -1;
  1388. }
  1389. /* NAPI poll context */
  1390. static int emac_poll_rx(void *param, int budget)
  1391. {
  1392. struct emac_instance *dev = param;
  1393. int slot = dev->rx_slot, received = 0;
  1394. DBG2(dev, "poll_rx(%d)" NL, budget);
  1395. again:
  1396. while (budget > 0) {
  1397. int len;
  1398. struct sk_buff *skb;
  1399. u16 ctrl = dev->rx_desc[slot].ctrl;
  1400. if (ctrl & MAL_RX_CTRL_EMPTY)
  1401. break;
  1402. skb = dev->rx_skb[slot];
  1403. mb();
  1404. len = dev->rx_desc[slot].data_len;
  1405. if (unlikely(!MAL_IS_SINGLE_RX(ctrl)))
  1406. goto sg;
  1407. ctrl &= EMAC_BAD_RX_MASK;
  1408. if (unlikely(ctrl && ctrl != EMAC_RX_TAH_BAD_CSUM)) {
  1409. emac_parse_rx_error(dev, ctrl);
  1410. ++dev->estats.rx_dropped_error;
  1411. emac_recycle_rx_skb(dev, slot, 0);
  1412. len = 0;
  1413. goto next;
  1414. }
  1415. if (len < ETH_HLEN) {
  1416. ++dev->estats.rx_dropped_stack;
  1417. emac_recycle_rx_skb(dev, slot, len);
  1418. goto next;
  1419. }
  1420. if (len && len < EMAC_RX_COPY_THRESH) {
  1421. struct sk_buff *copy_skb =
  1422. alloc_skb(len + EMAC_RX_SKB_HEADROOM + 2, GFP_ATOMIC);
  1423. if (unlikely(!copy_skb))
  1424. goto oom;
  1425. skb_reserve(copy_skb, EMAC_RX_SKB_HEADROOM + 2);
  1426. cacheable_memcpy(copy_skb->data - 2, skb->data - 2,
  1427. len + 2);
  1428. emac_recycle_rx_skb(dev, slot, len);
  1429. skb = copy_skb;
  1430. } else if (unlikely(emac_alloc_rx_skb(dev, slot, GFP_ATOMIC)))
  1431. goto oom;
  1432. skb_put(skb, len);
  1433. push_packet:
  1434. skb->protocol = eth_type_trans(skb, dev->ndev);
  1435. emac_rx_csum(dev, skb, ctrl);
  1436. if (unlikely(netif_receive_skb(skb) == NET_RX_DROP))
  1437. ++dev->estats.rx_dropped_stack;
  1438. next:
  1439. ++dev->stats.rx_packets;
  1440. skip:
  1441. dev->stats.rx_bytes += len;
  1442. slot = (slot + 1) % NUM_RX_BUFF;
  1443. --budget;
  1444. ++received;
  1445. continue;
  1446. sg:
  1447. if (ctrl & MAL_RX_CTRL_FIRST) {
  1448. BUG_ON(dev->rx_sg_skb);
  1449. if (unlikely(emac_alloc_rx_skb(dev, slot, GFP_ATOMIC))) {
  1450. DBG(dev, "rx OOM %d" NL, slot);
  1451. ++dev->estats.rx_dropped_oom;
  1452. emac_recycle_rx_skb(dev, slot, 0);
  1453. } else {
  1454. dev->rx_sg_skb = skb;
  1455. skb_put(skb, len);
  1456. }
  1457. } else if (!emac_rx_sg_append(dev, slot) &&
  1458. (ctrl & MAL_RX_CTRL_LAST)) {
  1459. skb = dev->rx_sg_skb;
  1460. dev->rx_sg_skb = NULL;
  1461. ctrl &= EMAC_BAD_RX_MASK;
  1462. if (unlikely(ctrl && ctrl != EMAC_RX_TAH_BAD_CSUM)) {
  1463. emac_parse_rx_error(dev, ctrl);
  1464. ++dev->estats.rx_dropped_error;
  1465. dev_kfree_skb(skb);
  1466. len = 0;
  1467. } else
  1468. goto push_packet;
  1469. }
  1470. goto skip;
  1471. oom:
  1472. DBG(dev, "rx OOM %d" NL, slot);
  1473. /* Drop the packet and recycle skb */
  1474. ++dev->estats.rx_dropped_oom;
  1475. emac_recycle_rx_skb(dev, slot, 0);
  1476. goto next;
  1477. }
  1478. if (received) {
  1479. DBG2(dev, "rx %d BDs" NL, received);
  1480. dev->rx_slot = slot;
  1481. }
  1482. if (unlikely(budget && test_bit(MAL_COMMAC_RX_STOPPED, &dev->commac.flags))) {
  1483. mb();
  1484. if (!(dev->rx_desc[slot].ctrl & MAL_RX_CTRL_EMPTY)) {
  1485. DBG2(dev, "rx restart" NL);
  1486. received = 0;
  1487. goto again;
  1488. }
  1489. if (dev->rx_sg_skb) {
  1490. DBG2(dev, "dropping partial rx packet" NL);
  1491. ++dev->estats.rx_dropped_error;
  1492. dev_kfree_skb(dev->rx_sg_skb);
  1493. dev->rx_sg_skb = NULL;
  1494. }
  1495. clear_bit(MAL_COMMAC_RX_STOPPED, &dev->commac.flags);
  1496. mal_enable_rx_channel(dev->mal, dev->mal_rx_chan);
  1497. emac_rx_enable(dev);
  1498. dev->rx_slot = 0;
  1499. }
  1500. return received;
  1501. }
  1502. /* NAPI poll context */
  1503. static int emac_peek_rx(void *param)
  1504. {
  1505. struct emac_instance *dev = param;
  1506. return !(dev->rx_desc[dev->rx_slot].ctrl & MAL_RX_CTRL_EMPTY);
  1507. }
  1508. /* NAPI poll context */
  1509. static int emac_peek_rx_sg(void *param)
  1510. {
  1511. struct emac_instance *dev = param;
  1512. int slot = dev->rx_slot;
  1513. while (1) {
  1514. u16 ctrl = dev->rx_desc[slot].ctrl;
  1515. if (ctrl & MAL_RX_CTRL_EMPTY)
  1516. return 0;
  1517. else if (ctrl & MAL_RX_CTRL_LAST)
  1518. return 1;
  1519. slot = (slot + 1) % NUM_RX_BUFF;
  1520. /* I'm just being paranoid here :) */
  1521. if (unlikely(slot == dev->rx_slot))
  1522. return 0;
  1523. }
  1524. }
  1525. /* Hard IRQ */
  1526. static void emac_rxde(void *param)
  1527. {
  1528. struct emac_instance *dev = param;
  1529. ++dev->estats.rx_stopped;
  1530. emac_rx_disable_async(dev);
  1531. }
  1532. /* Hard IRQ */
  1533. static irqreturn_t emac_irq(int irq, void *dev_instance)
  1534. {
  1535. struct emac_instance *dev = dev_instance;
  1536. struct emac_regs __iomem *p = dev->emacp;
  1537. struct emac_error_stats *st = &dev->estats;
  1538. u32 isr;
  1539. spin_lock(&dev->lock);
  1540. isr = in_be32(&p->isr);
  1541. out_be32(&p->isr, isr);
  1542. DBG(dev, "isr = %08x" NL, isr);
  1543. if (isr & EMAC4_ISR_TXPE)
  1544. ++st->tx_parity;
  1545. if (isr & EMAC4_ISR_RXPE)
  1546. ++st->rx_parity;
  1547. if (isr & EMAC4_ISR_TXUE)
  1548. ++st->tx_underrun;
  1549. if (isr & EMAC4_ISR_RXOE)
  1550. ++st->rx_fifo_overrun;
  1551. if (isr & EMAC_ISR_OVR)
  1552. ++st->rx_overrun;
  1553. if (isr & EMAC_ISR_BP)
  1554. ++st->rx_bad_packet;
  1555. if (isr & EMAC_ISR_RP)
  1556. ++st->rx_runt_packet;
  1557. if (isr & EMAC_ISR_SE)
  1558. ++st->rx_short_event;
  1559. if (isr & EMAC_ISR_ALE)
  1560. ++st->rx_alignment_error;
  1561. if (isr & EMAC_ISR_BFCS)
  1562. ++st->rx_bad_fcs;
  1563. if (isr & EMAC_ISR_PTLE)
  1564. ++st->rx_packet_too_long;
  1565. if (isr & EMAC_ISR_ORE)
  1566. ++st->rx_out_of_range;
  1567. if (isr & EMAC_ISR_IRE)
  1568. ++st->rx_in_range;
  1569. if (isr & EMAC_ISR_SQE)
  1570. ++st->tx_sqe;
  1571. if (isr & EMAC_ISR_TE)
  1572. ++st->tx_errors;
  1573. spin_unlock(&dev->lock);
  1574. return IRQ_HANDLED;
  1575. }
  1576. static struct net_device_stats *emac_stats(struct net_device *ndev)
  1577. {
  1578. struct emac_instance *dev = netdev_priv(ndev);
  1579. struct emac_stats *st = &dev->stats;
  1580. struct emac_error_stats *est = &dev->estats;
  1581. struct net_device_stats *nst = &dev->nstats;
  1582. unsigned long flags;
  1583. DBG2(dev, "stats" NL);
  1584. /* Compute "legacy" statistics */
  1585. spin_lock_irqsave(&dev->lock, flags);
  1586. nst->rx_packets = (unsigned long)st->rx_packets;
  1587. nst->rx_bytes = (unsigned long)st->rx_bytes;
  1588. nst->tx_packets = (unsigned long)st->tx_packets;
  1589. nst->tx_bytes = (unsigned long)st->tx_bytes;
  1590. nst->rx_dropped = (unsigned long)(est->rx_dropped_oom +
  1591. est->rx_dropped_error +
  1592. est->rx_dropped_resize +
  1593. est->rx_dropped_mtu);
  1594. nst->tx_dropped = (unsigned long)est->tx_dropped;
  1595. nst->rx_errors = (unsigned long)est->rx_bd_errors;
  1596. nst->rx_fifo_errors = (unsigned long)(est->rx_bd_overrun +
  1597. est->rx_fifo_overrun +
  1598. est->rx_overrun);
  1599. nst->rx_frame_errors = (unsigned long)(est->rx_bd_alignment_error +
  1600. est->rx_alignment_error);
  1601. nst->rx_crc_errors = (unsigned long)(est->rx_bd_bad_fcs +
  1602. est->rx_bad_fcs);
  1603. nst->rx_length_errors = (unsigned long)(est->rx_bd_runt_packet +
  1604. est->rx_bd_short_event +
  1605. est->rx_bd_packet_too_long +
  1606. est->rx_bd_out_of_range +
  1607. est->rx_bd_in_range +
  1608. est->rx_runt_packet +
  1609. est->rx_short_event +
  1610. est->rx_packet_too_long +
  1611. est->rx_out_of_range +
  1612. est->rx_in_range);
  1613. nst->tx_errors = (unsigned long)(est->tx_bd_errors + est->tx_errors);
  1614. nst->tx_fifo_errors = (unsigned long)(est->tx_bd_underrun +
  1615. est->tx_underrun);
  1616. nst->tx_carrier_errors = (unsigned long)est->tx_bd_carrier_loss;
  1617. nst->collisions = (unsigned long)(est->tx_bd_excessive_deferral +
  1618. est->tx_bd_excessive_collisions +
  1619. est->tx_bd_late_collision +
  1620. est->tx_bd_multple_collisions);
  1621. spin_unlock_irqrestore(&dev->lock, flags);
  1622. return nst;
  1623. }
  1624. static struct mal_commac_ops emac_commac_ops = {
  1625. .poll_tx = &emac_poll_tx,
  1626. .poll_rx = &emac_poll_rx,
  1627. .peek_rx = &emac_peek_rx,
  1628. .rxde = &emac_rxde,
  1629. };
  1630. static struct mal_commac_ops emac_commac_sg_ops = {
  1631. .poll_tx = &emac_poll_tx,
  1632. .poll_rx = &emac_poll_rx,
  1633. .peek_rx = &emac_peek_rx_sg,
  1634. .rxde = &emac_rxde,
  1635. };
  1636. /* Ethtool support */
  1637. static int emac_ethtool_get_settings(struct net_device *ndev,
  1638. struct ethtool_cmd *cmd)
  1639. {
  1640. struct emac_instance *dev = netdev_priv(ndev);
  1641. cmd->supported = dev->phy.features;
  1642. cmd->port = PORT_MII;
  1643. cmd->phy_address = dev->phy.address;
  1644. cmd->transceiver =
  1645. dev->phy.address >= 0 ? XCVR_EXTERNAL : XCVR_INTERNAL;
  1646. mutex_lock(&dev->link_lock);
  1647. cmd->advertising = dev->phy.advertising;
  1648. cmd->autoneg = dev->phy.autoneg;
  1649. cmd->speed = dev->phy.speed;
  1650. cmd->duplex = dev->phy.duplex;
  1651. mutex_unlock(&dev->link_lock);
  1652. return 0;
  1653. }
  1654. static int emac_ethtool_set_settings(struct net_device *ndev,
  1655. struct ethtool_cmd *cmd)
  1656. {
  1657. struct emac_instance *dev = netdev_priv(ndev);
  1658. u32 f = dev->phy.features;
  1659. DBG(dev, "set_settings(%d, %d, %d, 0x%08x)" NL,
  1660. cmd->autoneg, cmd->speed, cmd->duplex, cmd->advertising);
  1661. /* Basic sanity checks */
  1662. if (dev->phy.address < 0)
  1663. return -EOPNOTSUPP;
  1664. if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
  1665. return -EINVAL;
  1666. if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
  1667. return -EINVAL;
  1668. if (cmd->duplex != DUPLEX_HALF && cmd->duplex != DUPLEX_FULL)
  1669. return -EINVAL;
  1670. if (cmd->autoneg == AUTONEG_DISABLE) {
  1671. switch (cmd->speed) {
  1672. case SPEED_10:
  1673. if (cmd->duplex == DUPLEX_HALF &&
  1674. !(f & SUPPORTED_10baseT_Half))
  1675. return -EINVAL;
  1676. if (cmd->duplex == DUPLEX_FULL &&
  1677. !(f & SUPPORTED_10baseT_Full))
  1678. return -EINVAL;
  1679. break;
  1680. case SPEED_100:
  1681. if (cmd->duplex == DUPLEX_HALF &&
  1682. !(f & SUPPORTED_100baseT_Half))
  1683. return -EINVAL;
  1684. if (cmd->duplex == DUPLEX_FULL &&
  1685. !(f & SUPPORTED_100baseT_Full))
  1686. return -EINVAL;
  1687. break;
  1688. case SPEED_1000:
  1689. if (cmd->duplex == DUPLEX_HALF &&
  1690. !(f & SUPPORTED_1000baseT_Half))
  1691. return -EINVAL;
  1692. if (cmd->duplex == DUPLEX_FULL &&
  1693. !(f & SUPPORTED_1000baseT_Full))
  1694. return -EINVAL;
  1695. break;
  1696. default:
  1697. return -EINVAL;
  1698. }
  1699. mutex_lock(&dev->link_lock);
  1700. dev->phy.def->ops->setup_forced(&dev->phy, cmd->speed,
  1701. cmd->duplex);
  1702. mutex_unlock(&dev->link_lock);
  1703. } else {
  1704. if (!(f & SUPPORTED_Autoneg))
  1705. return -EINVAL;
  1706. mutex_lock(&dev->link_lock);
  1707. dev->phy.def->ops->setup_aneg(&dev->phy,
  1708. (cmd->advertising & f) |
  1709. (dev->phy.advertising &
  1710. (ADVERTISED_Pause |
  1711. ADVERTISED_Asym_Pause)));
  1712. mutex_unlock(&dev->link_lock);
  1713. }
  1714. emac_force_link_update(dev);
  1715. return 0;
  1716. }
  1717. static void emac_ethtool_get_ringparam(struct net_device *ndev,
  1718. struct ethtool_ringparam *rp)
  1719. {
  1720. rp->rx_max_pending = rp->rx_pending = NUM_RX_BUFF;
  1721. rp->tx_max_pending = rp->tx_pending = NUM_TX_BUFF;
  1722. }
  1723. static void emac_ethtool_get_pauseparam(struct net_device *ndev,
  1724. struct ethtool_pauseparam *pp)
  1725. {
  1726. struct emac_instance *dev = netdev_priv(ndev);
  1727. mutex_lock(&dev->link_lock);
  1728. if ((dev->phy.features & SUPPORTED_Autoneg) &&
  1729. (dev->phy.advertising & (ADVERTISED_Pause | ADVERTISED_Asym_Pause)))
  1730. pp->autoneg = 1;
  1731. if (dev->phy.duplex == DUPLEX_FULL) {
  1732. if (dev->phy.pause)
  1733. pp->rx_pause = pp->tx_pause = 1;
  1734. else if (dev->phy.asym_pause)
  1735. pp->tx_pause = 1;
  1736. }
  1737. mutex_unlock(&dev->link_lock);
  1738. }
  1739. static int emac_get_regs_len(struct emac_instance *dev)
  1740. {
  1741. if (emac_has_feature(dev, EMAC_FTR_EMAC4))
  1742. return sizeof(struct emac_ethtool_regs_subhdr) +
  1743. EMAC4_ETHTOOL_REGS_SIZE(dev);
  1744. else
  1745. return sizeof(struct emac_ethtool_regs_subhdr) +
  1746. EMAC_ETHTOOL_REGS_SIZE(dev);
  1747. }
  1748. static int emac_ethtool_get_regs_len(struct net_device *ndev)
  1749. {
  1750. struct emac_instance *dev = netdev_priv(ndev);
  1751. int size;
  1752. size = sizeof(struct emac_ethtool_regs_hdr) +
  1753. emac_get_regs_len(dev) + mal_get_regs_len(dev->mal);
  1754. if (emac_has_feature(dev, EMAC_FTR_HAS_ZMII))
  1755. size += zmii_get_regs_len(dev->zmii_dev);
  1756. if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII))
  1757. size += rgmii_get_regs_len(dev->rgmii_dev);
  1758. if (emac_has_feature(dev, EMAC_FTR_HAS_TAH))
  1759. size += tah_get_regs_len(dev->tah_dev);
  1760. return size;
  1761. }
  1762. static void *emac_dump_regs(struct emac_instance *dev, void *buf)
  1763. {
  1764. struct emac_ethtool_regs_subhdr *hdr = buf;
  1765. hdr->index = dev->cell_index;
  1766. if (emac_has_feature(dev, EMAC_FTR_EMAC4)) {
  1767. hdr->version = EMAC4_ETHTOOL_REGS_VER;
  1768. memcpy_fromio(hdr + 1, dev->emacp, EMAC4_ETHTOOL_REGS_SIZE(dev));
  1769. return (void *)(hdr + 1) + EMAC4_ETHTOOL_REGS_SIZE(dev);
  1770. } else {
  1771. hdr->version = EMAC_ETHTOOL_REGS_VER;
  1772. memcpy_fromio(hdr + 1, dev->emacp, EMAC_ETHTOOL_REGS_SIZE(dev));
  1773. return (void *)(hdr + 1) + EMAC_ETHTOOL_REGS_SIZE(dev);
  1774. }
  1775. }
  1776. static void emac_ethtool_get_regs(struct net_device *ndev,
  1777. struct ethtool_regs *regs, void *buf)
  1778. {
  1779. struct emac_instance *dev = netdev_priv(ndev);
  1780. struct emac_ethtool_regs_hdr *hdr = buf;
  1781. hdr->components = 0;
  1782. buf = hdr + 1;
  1783. buf = mal_dump_regs(dev->mal, buf);
  1784. buf = emac_dump_regs(dev, buf);
  1785. if (emac_has_feature(dev, EMAC_FTR_HAS_ZMII)) {
  1786. hdr->components |= EMAC_ETHTOOL_REGS_ZMII;
  1787. buf = zmii_dump_regs(dev->zmii_dev, buf);
  1788. }
  1789. if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII)) {
  1790. hdr->components |= EMAC_ETHTOOL_REGS_RGMII;
  1791. buf = rgmii_dump_regs(dev->rgmii_dev, buf);
  1792. }
  1793. if (emac_has_feature(dev, EMAC_FTR_HAS_TAH)) {
  1794. hdr->components |= EMAC_ETHTOOL_REGS_TAH;
  1795. buf = tah_dump_regs(dev->tah_dev, buf);
  1796. }
  1797. }
  1798. static int emac_ethtool_nway_reset(struct net_device *ndev)
  1799. {
  1800. struct emac_instance *dev = netdev_priv(ndev);
  1801. int res = 0;
  1802. DBG(dev, "nway_reset" NL);
  1803. if (dev->phy.address < 0)
  1804. return -EOPNOTSUPP;
  1805. mutex_lock(&dev->link_lock);
  1806. if (!dev->phy.autoneg) {
  1807. res = -EINVAL;
  1808. goto out;
  1809. }
  1810. dev->phy.def->ops->setup_aneg(&dev->phy, dev->phy.advertising);
  1811. out:
  1812. mutex_unlock(&dev->link_lock);
  1813. emac_force_link_update(dev);
  1814. return res;
  1815. }
  1816. static int emac_ethtool_get_sset_count(struct net_device *ndev, int stringset)
  1817. {
  1818. if (stringset == ETH_SS_STATS)
  1819. return EMAC_ETHTOOL_STATS_COUNT;
  1820. else
  1821. return -EINVAL;
  1822. }
  1823. static void emac_ethtool_get_strings(struct net_device *ndev, u32 stringset,
  1824. u8 * buf)
  1825. {
  1826. if (stringset == ETH_SS_STATS)
  1827. memcpy(buf, &emac_stats_keys, sizeof(emac_stats_keys));
  1828. }
  1829. static void emac_ethtool_get_ethtool_stats(struct net_device *ndev,
  1830. struct ethtool_stats *estats,
  1831. u64 * tmp_stats)
  1832. {
  1833. struct emac_instance *dev = netdev_priv(ndev);
  1834. memcpy(tmp_stats, &dev->stats, sizeof(dev->stats));
  1835. tmp_stats += sizeof(dev->stats) / sizeof(u64);
  1836. memcpy(tmp_stats, &dev->estats, sizeof(dev->estats));
  1837. }
  1838. static void emac_ethtool_get_drvinfo(struct net_device *ndev,
  1839. struct ethtool_drvinfo *info)
  1840. {
  1841. struct emac_instance *dev = netdev_priv(ndev);
  1842. strcpy(info->driver, "ibm_emac");
  1843. strcpy(info->version, DRV_VERSION);
  1844. info->fw_version[0] = '\0';
  1845. sprintf(info->bus_info, "PPC 4xx EMAC-%d %s",
  1846. dev->cell_index, dev->ofdev->dev.of_node->full_name);
  1847. info->regdump_len = emac_ethtool_get_regs_len(ndev);
  1848. }
  1849. static const struct ethtool_ops emac_ethtool_ops = {
  1850. .get_settings = emac_ethtool_get_settings,
  1851. .set_settings = emac_ethtool_set_settings,
  1852. .get_drvinfo = emac_ethtool_get_drvinfo,
  1853. .get_regs_len = emac_ethtool_get_regs_len,
  1854. .get_regs = emac_ethtool_get_regs,
  1855. .nway_reset = emac_ethtool_nway_reset,
  1856. .get_ringparam = emac_ethtool_get_ringparam,
  1857. .get_pauseparam = emac_ethtool_get_pauseparam,
  1858. .get_strings = emac_ethtool_get_strings,
  1859. .get_sset_count = emac_ethtool_get_sset_count,
  1860. .get_ethtool_stats = emac_ethtool_get_ethtool_stats,
  1861. .get_link = ethtool_op_get_link,
  1862. };
  1863. static int emac_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
  1864. {
  1865. struct emac_instance *dev = netdev_priv(ndev);
  1866. struct mii_ioctl_data *data = if_mii(rq);
  1867. DBG(dev, "ioctl %08x" NL, cmd);
  1868. if (dev->phy.address < 0)
  1869. return -EOPNOTSUPP;
  1870. switch (cmd) {
  1871. case SIOCGMIIPHY:
  1872. data->phy_id = dev->phy.address;
  1873. /* Fall through */
  1874. case SIOCGMIIREG:
  1875. data->val_out = emac_mdio_read(ndev, dev->phy.address,
  1876. data->reg_num);
  1877. return 0;
  1878. case SIOCSMIIREG:
  1879. emac_mdio_write(ndev, dev->phy.address, data->reg_num,
  1880. data->val_in);
  1881. return 0;
  1882. default:
  1883. return -EOPNOTSUPP;
  1884. }
  1885. }
  1886. struct emac_depentry {
  1887. u32 phandle;
  1888. struct device_node *node;
  1889. struct platform_device *ofdev;
  1890. void *drvdata;
  1891. };
  1892. #define EMAC_DEP_MAL_IDX 0
  1893. #define EMAC_DEP_ZMII_IDX 1
  1894. #define EMAC_DEP_RGMII_IDX 2
  1895. #define EMAC_DEP_TAH_IDX 3
  1896. #define EMAC_DEP_MDIO_IDX 4
  1897. #define EMAC_DEP_PREV_IDX 5
  1898. #define EMAC_DEP_COUNT 6
  1899. static int __devinit emac_check_deps(struct emac_instance *dev,
  1900. struct emac_depentry *deps)
  1901. {
  1902. int i, there = 0;
  1903. struct device_node *np;
  1904. for (i = 0; i < EMAC_DEP_COUNT; i++) {
  1905. /* no dependency on that item, allright */
  1906. if (deps[i].phandle == 0) {
  1907. there++;
  1908. continue;
  1909. }
  1910. /* special case for blist as the dependency might go away */
  1911. if (i == EMAC_DEP_PREV_IDX) {
  1912. np = *(dev->blist - 1);
  1913. if (np == NULL) {
  1914. deps[i].phandle = 0;
  1915. there++;
  1916. continue;
  1917. }
  1918. if (deps[i].node == NULL)
  1919. deps[i].node = of_node_get(np);
  1920. }
  1921. if (deps[i].node == NULL)
  1922. deps[i].node = of_find_node_by_phandle(deps[i].phandle);
  1923. if (deps[i].node == NULL)
  1924. continue;
  1925. if (deps[i].ofdev == NULL)
  1926. deps[i].ofdev = of_find_device_by_node(deps[i].node);
  1927. if (deps[i].ofdev == NULL)
  1928. continue;
  1929. if (deps[i].drvdata == NULL)
  1930. deps[i].drvdata = dev_get_drvdata(&deps[i].ofdev->dev);
  1931. if (deps[i].drvdata != NULL)
  1932. there++;
  1933. }
  1934. return there == EMAC_DEP_COUNT;
  1935. }
  1936. static void emac_put_deps(struct emac_instance *dev)
  1937. {
  1938. if (dev->mal_dev)
  1939. of_dev_put(dev->mal_dev);
  1940. if (dev->zmii_dev)
  1941. of_dev_put(dev->zmii_dev);
  1942. if (dev->rgmii_dev)
  1943. of_dev_put(dev->rgmii_dev);
  1944. if (dev->mdio_dev)
  1945. of_dev_put(dev->mdio_dev);
  1946. if (dev->tah_dev)
  1947. of_dev_put(dev->tah_dev);
  1948. }
  1949. static int __devinit emac_of_bus_notify(struct notifier_block *nb,
  1950. unsigned long action, void *data)
  1951. {
  1952. /* We are only intereted in device addition */
  1953. if (action == BUS_NOTIFY_BOUND_DRIVER)
  1954. wake_up_all(&emac_probe_wait);
  1955. return 0;
  1956. }
  1957. static struct notifier_block emac_of_bus_notifier __devinitdata = {
  1958. .notifier_call = emac_of_bus_notify
  1959. };
  1960. static int __devinit emac_wait_deps(struct emac_instance *dev)
  1961. {
  1962. struct emac_depentry deps[EMAC_DEP_COUNT];
  1963. int i, err;
  1964. memset(&deps, 0, sizeof(deps));
  1965. deps[EMAC_DEP_MAL_IDX].phandle = dev->mal_ph;
  1966. deps[EMAC_DEP_ZMII_IDX].phandle = dev->zmii_ph;
  1967. deps[EMAC_DEP_RGMII_IDX].phandle = dev->rgmii_ph;
  1968. if (dev->tah_ph)
  1969. deps[EMAC_DEP_TAH_IDX].phandle = dev->tah_ph;
  1970. if (dev->mdio_ph)
  1971. deps[EMAC_DEP_MDIO_IDX].phandle = dev->mdio_ph;
  1972. if (dev->blist && dev->blist > emac_boot_list)
  1973. deps[EMAC_DEP_PREV_IDX].phandle = 0xffffffffu;
  1974. bus_register_notifier(&platform_bus_type, &emac_of_bus_notifier);
  1975. wait_event_timeout(emac_probe_wait,
  1976. emac_check_deps(dev, deps),
  1977. EMAC_PROBE_DEP_TIMEOUT);
  1978. bus_unregister_notifier(&platform_bus_type, &emac_of_bus_notifier);
  1979. err = emac_check_deps(dev, deps) ? 0 : -ENODEV;
  1980. for (i = 0; i < EMAC_DEP_COUNT; i++) {
  1981. if (deps[i].node)
  1982. of_node_put(deps[i].node);
  1983. if (err && deps[i].ofdev)
  1984. of_dev_put(deps[i].ofdev);
  1985. }
  1986. if (err == 0) {
  1987. dev->mal_dev = deps[EMAC_DEP_MAL_IDX].ofdev;
  1988. dev->zmii_dev = deps[EMAC_DEP_ZMII_IDX].ofdev;
  1989. dev->rgmii_dev = deps[EMAC_DEP_RGMII_IDX].ofdev;
  1990. dev->tah_dev = deps[EMAC_DEP_TAH_IDX].ofdev;
  1991. dev->mdio_dev = deps[EMAC_DEP_MDIO_IDX].ofdev;
  1992. }
  1993. if (deps[EMAC_DEP_PREV_IDX].ofdev)
  1994. of_dev_put(deps[EMAC_DEP_PREV_IDX].ofdev);
  1995. return err;
  1996. }
  1997. static int __devinit emac_read_uint_prop(struct device_node *np, const char *name,
  1998. u32 *val, int fatal)
  1999. {
  2000. int len;
  2001. const u32 *prop = of_get_property(np, name, &len);
  2002. if (prop == NULL || len < sizeof(u32)) {
  2003. if (fatal)
  2004. printk(KERN_ERR "%s: missing %s property\n",
  2005. np->full_name, name);
  2006. return -ENODEV;
  2007. }
  2008. *val = *prop;
  2009. return 0;
  2010. }
  2011. static int __devinit emac_init_phy(struct emac_instance *dev)
  2012. {
  2013. struct device_node *np = dev->ofdev->dev.of_node;
  2014. struct net_device *ndev = dev->ndev;
  2015. u32 phy_map, adv;
  2016. int i;
  2017. dev->phy.dev = ndev;
  2018. dev->phy.mode = dev->phy_mode;
  2019. /* PHY-less configuration.
  2020. * XXX I probably should move these settings to the dev tree
  2021. */
  2022. if (dev->phy_address == 0xffffffff && dev->phy_map == 0xffffffff) {
  2023. emac_reset(dev);
  2024. /* PHY-less configuration.
  2025. * XXX I probably should move these settings to the dev tree
  2026. */
  2027. dev->phy.address = -1;
  2028. dev->phy.features = SUPPORTED_MII;
  2029. if (emac_phy_supports_gige(dev->phy_mode))
  2030. dev->phy.features |= SUPPORTED_1000baseT_Full;
  2031. else
  2032. dev->phy.features |= SUPPORTED_100baseT_Full;
  2033. dev->phy.pause = 1;
  2034. return 0;
  2035. }
  2036. mutex_lock(&emac_phy_map_lock);
  2037. phy_map = dev->phy_map | busy_phy_map;
  2038. DBG(dev, "PHY maps %08x %08x" NL, dev->phy_map, busy_phy_map);
  2039. dev->phy.mdio_read = emac_mdio_read;
  2040. dev->phy.mdio_write = emac_mdio_write;
  2041. /* Enable internal clock source */
  2042. #ifdef CONFIG_PPC_DCR_NATIVE
  2043. if (emac_has_feature(dev, EMAC_FTR_440GX_PHY_CLK_FIX))
  2044. dcri_clrset(SDR0, SDR0_MFR, 0, SDR0_MFR_ECS);
  2045. #endif
  2046. /* PHY clock workaround */
  2047. emac_rx_clk_tx(dev);
  2048. /* Enable internal clock source on 440GX*/
  2049. #ifdef CONFIG_PPC_DCR_NATIVE
  2050. if (emac_has_feature(dev, EMAC_FTR_440GX_PHY_CLK_FIX))
  2051. dcri_clrset(SDR0, SDR0_MFR, 0, SDR0_MFR_ECS);
  2052. #endif
  2053. /* Configure EMAC with defaults so we can at least use MDIO
  2054. * This is needed mostly for 440GX
  2055. */
  2056. if (emac_phy_gpcs(dev->phy.mode)) {
  2057. /* XXX
  2058. * Make GPCS PHY address equal to EMAC index.
  2059. * We probably should take into account busy_phy_map
  2060. * and/or phy_map here.
  2061. *
  2062. * Note that the busy_phy_map is currently global
  2063. * while it should probably be per-ASIC...
  2064. */
  2065. dev->phy.gpcs_address = dev->gpcs_address;
  2066. if (dev->phy.gpcs_address == 0xffffffff)
  2067. dev->phy.address = dev->cell_index;
  2068. }
  2069. emac_configure(dev);
  2070. if (dev->phy_address != 0xffffffff)
  2071. phy_map = ~(1 << dev->phy_address);
  2072. for (i = 0; i < 0x20; phy_map >>= 1, ++i)
  2073. if (!(phy_map & 1)) {
  2074. int r;
  2075. busy_phy_map |= 1 << i;
  2076. /* Quick check if there is a PHY at the address */
  2077. r = emac_mdio_read(dev->ndev, i, MII_BMCR);
  2078. if (r == 0xffff || r < 0)
  2079. continue;
  2080. if (!emac_mii_phy_probe(&dev->phy, i))
  2081. break;
  2082. }
  2083. /* Enable external clock source */
  2084. #ifdef CONFIG_PPC_DCR_NATIVE
  2085. if (emac_has_feature(dev, EMAC_FTR_440GX_PHY_CLK_FIX))
  2086. dcri_clrset(SDR0, SDR0_MFR, SDR0_MFR_ECS, 0);
  2087. #endif
  2088. mutex_unlock(&emac_phy_map_lock);
  2089. if (i == 0x20) {
  2090. printk(KERN_WARNING "%s: can't find PHY!\n", np->full_name);
  2091. return -ENXIO;
  2092. }
  2093. /* Init PHY */
  2094. if (dev->phy.def->ops->init)
  2095. dev->phy.def->ops->init(&dev->phy);
  2096. /* Disable any PHY features not supported by the platform */
  2097. dev->phy.def->features &= ~dev->phy_feat_exc;
  2098. /* Setup initial link parameters */
  2099. if (dev->phy.features & SUPPORTED_Autoneg) {
  2100. adv = dev->phy.features;
  2101. if (!emac_has_feature(dev, EMAC_FTR_NO_FLOW_CONTROL_40x))
  2102. adv |= ADVERTISED_Pause | ADVERTISED_Asym_Pause;
  2103. /* Restart autonegotiation */
  2104. dev->phy.def->ops->setup_aneg(&dev->phy, adv);
  2105. } else {
  2106. u32 f = dev->phy.def->features;
  2107. int speed = SPEED_10, fd = DUPLEX_HALF;
  2108. /* Select highest supported speed/duplex */
  2109. if (f & SUPPORTED_1000baseT_Full) {
  2110. speed = SPEED_1000;
  2111. fd = DUPLEX_FULL;
  2112. } else if (f & SUPPORTED_1000baseT_Half)
  2113. speed = SPEED_1000;
  2114. else if (f & SUPPORTED_100baseT_Full) {
  2115. speed = SPEED_100;
  2116. fd = DUPLEX_FULL;
  2117. } else if (f & SUPPORTED_100baseT_Half)
  2118. speed = SPEED_100;
  2119. else if (f & SUPPORTED_10baseT_Full)
  2120. fd = DUPLEX_FULL;
  2121. /* Force link parameters */
  2122. dev->phy.def->ops->setup_forced(&dev->phy, speed, fd);
  2123. }
  2124. return 0;
  2125. }
  2126. static int __devinit emac_init_config(struct emac_instance *dev)
  2127. {
  2128. struct device_node *np = dev->ofdev->dev.of_node;
  2129. const void *p;
  2130. unsigned int plen;
  2131. const char *pm, *phy_modes[] = {
  2132. [PHY_MODE_NA] = "",
  2133. [PHY_MODE_MII] = "mii",
  2134. [PHY_MODE_RMII] = "rmii",
  2135. [PHY_MODE_SMII] = "smii",
  2136. [PHY_MODE_RGMII] = "rgmii",
  2137. [PHY_MODE_TBI] = "tbi",
  2138. [PHY_MODE_GMII] = "gmii",
  2139. [PHY_MODE_RTBI] = "rtbi",
  2140. [PHY_MODE_SGMII] = "sgmii",
  2141. };
  2142. /* Read config from device-tree */
  2143. if (emac_read_uint_prop(np, "mal-device", &dev->mal_ph, 1))
  2144. return -ENXIO;
  2145. if (emac_read_uint_prop(np, "mal-tx-channel", &dev->mal_tx_chan, 1))
  2146. return -ENXIO;
  2147. if (emac_read_uint_prop(np, "mal-rx-channel", &dev->mal_rx_chan, 1))
  2148. return -ENXIO;
  2149. if (emac_read_uint_prop(np, "cell-index", &dev->cell_index, 1))
  2150. return -ENXIO;
  2151. if (emac_read_uint_prop(np, "max-frame-size", &dev->max_mtu, 0))
  2152. dev->max_mtu = 1500;
  2153. if (emac_read_uint_prop(np, "rx-fifo-size", &dev->rx_fifo_size, 0))
  2154. dev->rx_fifo_size = 2048;
  2155. if (emac_read_uint_prop(np, "tx-fifo-size", &dev->tx_fifo_size, 0))
  2156. dev->tx_fifo_size = 2048;
  2157. if (emac_read_uint_prop(np, "rx-fifo-size-gige", &dev->rx_fifo_size_gige, 0))
  2158. dev->rx_fifo_size_gige = dev->rx_fifo_size;
  2159. if (emac_read_uint_prop(np, "tx-fifo-size-gige", &dev->tx_fifo_size_gige, 0))
  2160. dev->tx_fifo_size_gige = dev->tx_fifo_size;
  2161. if (emac_read_uint_prop(np, "phy-address", &dev->phy_address, 0))
  2162. dev->phy_address = 0xffffffff;
  2163. if (emac_read_uint_prop(np, "phy-map", &dev->phy_map, 0))
  2164. dev->phy_map = 0xffffffff;
  2165. if (emac_read_uint_prop(np, "gpcs-address", &dev->gpcs_address, 0))
  2166. dev->gpcs_address = 0xffffffff;
  2167. if (emac_read_uint_prop(np->parent, "clock-frequency", &dev->opb_bus_freq, 1))
  2168. return -ENXIO;
  2169. if (emac_read_uint_prop(np, "tah-device", &dev->tah_ph, 0))
  2170. dev->tah_ph = 0;
  2171. if (emac_read_uint_prop(np, "tah-channel", &dev->tah_port, 0))
  2172. dev->tah_port = 0;
  2173. if (emac_read_uint_prop(np, "mdio-device", &dev->mdio_ph, 0))
  2174. dev->mdio_ph = 0;
  2175. if (emac_read_uint_prop(np, "zmii-device", &dev->zmii_ph, 0))
  2176. dev->zmii_ph = 0;
  2177. if (emac_read_uint_prop(np, "zmii-channel", &dev->zmii_port, 0))
  2178. dev->zmii_port = 0xffffffff;
  2179. if (emac_read_uint_prop(np, "rgmii-device", &dev->rgmii_ph, 0))
  2180. dev->rgmii_ph = 0;
  2181. if (emac_read_uint_prop(np, "rgmii-channel", &dev->rgmii_port, 0))
  2182. dev->rgmii_port = 0xffffffff;
  2183. if (emac_read_uint_prop(np, "fifo-entry-size", &dev->fifo_entry_size, 0))
  2184. dev->fifo_entry_size = 16;
  2185. if (emac_read_uint_prop(np, "mal-burst-size", &dev->mal_burst_size, 0))
  2186. dev->mal_burst_size = 256;
  2187. /* PHY mode needs some decoding */
  2188. dev->phy_mode = PHY_MODE_NA;
  2189. pm = of_get_property(np, "phy-mode", &plen);
  2190. if (pm != NULL) {
  2191. int i;
  2192. for (i = 0; i < ARRAY_SIZE(phy_modes); i++)
  2193. if (!strcasecmp(pm, phy_modes[i])) {
  2194. dev->phy_mode = i;
  2195. break;
  2196. }
  2197. }
  2198. /* Backward compat with non-final DT */
  2199. if (dev->phy_mode == PHY_MODE_NA && pm != NULL && plen == 4) {
  2200. u32 nmode = *(const u32 *)pm;
  2201. if (nmode > PHY_MODE_NA && nmode <= PHY_MODE_SGMII)
  2202. dev->phy_mode = nmode;
  2203. }
  2204. /* Check EMAC version */
  2205. if (of_device_is_compatible(np, "ibm,emac4sync")) {
  2206. dev->features |= (EMAC_FTR_EMAC4 | EMAC_FTR_EMAC4SYNC);
  2207. if (of_device_is_compatible(np, "ibm,emac-460ex") ||
  2208. of_device_is_compatible(np, "ibm,emac-460gt"))
  2209. dev->features |= EMAC_FTR_460EX_PHY_CLK_FIX;
  2210. if (of_device_is_compatible(np, "ibm,emac-405ex") ||
  2211. of_device_is_compatible(np, "ibm,emac-405exr"))
  2212. dev->features |= EMAC_FTR_440EP_PHY_CLK_FIX;
  2213. } else if (of_device_is_compatible(np, "ibm,emac4")) {
  2214. dev->features |= EMAC_FTR_EMAC4;
  2215. if (of_device_is_compatible(np, "ibm,emac-440gx"))
  2216. dev->features |= EMAC_FTR_440GX_PHY_CLK_FIX;
  2217. } else {
  2218. if (of_device_is_compatible(np, "ibm,emac-440ep") ||
  2219. of_device_is_compatible(np, "ibm,emac-440gr"))
  2220. dev->features |= EMAC_FTR_440EP_PHY_CLK_FIX;
  2221. if (of_device_is_compatible(np, "ibm,emac-405ez")) {
  2222. #ifdef CONFIG_IBM_NEW_EMAC_NO_FLOW_CTRL
  2223. dev->features |= EMAC_FTR_NO_FLOW_CONTROL_40x;
  2224. #else
  2225. printk(KERN_ERR "%s: Flow control not disabled!\n",
  2226. np->full_name);
  2227. return -ENXIO;
  2228. #endif
  2229. }
  2230. }
  2231. /* Fixup some feature bits based on the device tree */
  2232. if (of_get_property(np, "has-inverted-stacr-oc", NULL))
  2233. dev->features |= EMAC_FTR_STACR_OC_INVERT;
  2234. if (of_get_property(np, "has-new-stacr-staopc", NULL))
  2235. dev->features |= EMAC_FTR_HAS_NEW_STACR;
  2236. /* CAB lacks the appropriate properties */
  2237. if (of_device_is_compatible(np, "ibm,emac-axon"))
  2238. dev->features |= EMAC_FTR_HAS_NEW_STACR |
  2239. EMAC_FTR_STACR_OC_INVERT;
  2240. /* Enable TAH/ZMII/RGMII features as found */
  2241. if (dev->tah_ph != 0) {
  2242. #ifdef CONFIG_IBM_NEW_EMAC_TAH
  2243. dev->features |= EMAC_FTR_HAS_TAH;
  2244. #else
  2245. printk(KERN_ERR "%s: TAH support not enabled !\n",
  2246. np->full_name);
  2247. return -ENXIO;
  2248. #endif
  2249. }
  2250. if (dev->zmii_ph != 0) {
  2251. #ifdef CONFIG_IBM_NEW_EMAC_ZMII
  2252. dev->features |= EMAC_FTR_HAS_ZMII;
  2253. #else
  2254. printk(KERN_ERR "%s: ZMII support not enabled !\n",
  2255. np->full_name);
  2256. return -ENXIO;
  2257. #endif
  2258. }
  2259. if (dev->rgmii_ph != 0) {
  2260. #ifdef CONFIG_IBM_NEW_EMAC_RGMII
  2261. dev->features |= EMAC_FTR_HAS_RGMII;
  2262. #else
  2263. printk(KERN_ERR "%s: RGMII support not enabled !\n",
  2264. np->full_name);
  2265. return -ENXIO;
  2266. #endif
  2267. }
  2268. /* Read MAC-address */
  2269. p = of_get_property(np, "local-mac-address", NULL);
  2270. if (p == NULL) {
  2271. printk(KERN_ERR "%s: Can't find local-mac-address property\n",
  2272. np->full_name);
  2273. return -ENXIO;
  2274. }
  2275. memcpy(dev->ndev->dev_addr, p, 6);
  2276. /* IAHT and GAHT filter parameterization */
  2277. if (emac_has_feature(dev, EMAC_FTR_EMAC4SYNC)) {
  2278. dev->xaht_slots_shift = EMAC4SYNC_XAHT_SLOTS_SHIFT;
  2279. dev->xaht_width_shift = EMAC4SYNC_XAHT_WIDTH_SHIFT;
  2280. } else {
  2281. dev->xaht_slots_shift = EMAC4_XAHT_SLOTS_SHIFT;
  2282. dev->xaht_width_shift = EMAC4_XAHT_WIDTH_SHIFT;
  2283. }
  2284. DBG(dev, "features : 0x%08x / 0x%08x\n", dev->features, EMAC_FTRS_POSSIBLE);
  2285. DBG(dev, "tx_fifo_size : %d (%d gige)\n", dev->tx_fifo_size, dev->tx_fifo_size_gige);
  2286. DBG(dev, "rx_fifo_size : %d (%d gige)\n", dev->rx_fifo_size, dev->rx_fifo_size_gige);
  2287. DBG(dev, "max_mtu : %d\n", dev->max_mtu);
  2288. DBG(dev, "OPB freq : %d\n", dev->opb_bus_freq);
  2289. return 0;
  2290. }
  2291. static const struct net_device_ops emac_netdev_ops = {
  2292. .ndo_open = emac_open,
  2293. .ndo_stop = emac_close,
  2294. .ndo_get_stats = emac_stats,
  2295. .ndo_set_multicast_list = emac_set_multicast_list,
  2296. .ndo_do_ioctl = emac_ioctl,
  2297. .ndo_tx_timeout = emac_tx_timeout,
  2298. .ndo_validate_addr = eth_validate_addr,
  2299. .ndo_set_mac_address = eth_mac_addr,
  2300. .ndo_start_xmit = emac_start_xmit,
  2301. .ndo_change_mtu = eth_change_mtu,
  2302. };
  2303. static const struct net_device_ops emac_gige_netdev_ops = {
  2304. .ndo_open = emac_open,
  2305. .ndo_stop = emac_close,
  2306. .ndo_get_stats = emac_stats,
  2307. .ndo_set_multicast_list = emac_set_multicast_list,
  2308. .ndo_do_ioctl = emac_ioctl,
  2309. .ndo_tx_timeout = emac_tx_timeout,
  2310. .ndo_validate_addr = eth_validate_addr,
  2311. .ndo_set_mac_address = eth_mac_addr,
  2312. .ndo_start_xmit = emac_start_xmit_sg,
  2313. .ndo_change_mtu = emac_change_mtu,
  2314. };
  2315. static int __devinit emac_probe(struct platform_device *ofdev)
  2316. {
  2317. struct net_device *ndev;
  2318. struct emac_instance *dev;
  2319. struct device_node *np = ofdev->dev.of_node;
  2320. struct device_node **blist = NULL;
  2321. int err, i;
  2322. /* Skip unused/unwired EMACS. We leave the check for an unused
  2323. * property here for now, but new flat device trees should set a
  2324. * status property to "disabled" instead.
  2325. */
  2326. if (of_get_property(np, "unused", NULL) || !of_device_is_available(np))
  2327. return -ENODEV;
  2328. /* Find ourselves in the bootlist if we are there */
  2329. for (i = 0; i < EMAC_BOOT_LIST_SIZE; i++)
  2330. if (emac_boot_list[i] == np)
  2331. blist = &emac_boot_list[i];
  2332. /* Allocate our net_device structure */
  2333. err = -ENOMEM;
  2334. ndev = alloc_etherdev(sizeof(struct emac_instance));
  2335. if (!ndev) {
  2336. printk(KERN_ERR "%s: could not allocate ethernet device!\n",
  2337. np->full_name);
  2338. goto err_gone;
  2339. }
  2340. dev = netdev_priv(ndev);
  2341. dev->ndev = ndev;
  2342. dev->ofdev = ofdev;
  2343. dev->blist = blist;
  2344. SET_NETDEV_DEV(ndev, &ofdev->dev);
  2345. /* Initialize some embedded data structures */
  2346. mutex_init(&dev->mdio_lock);
  2347. mutex_init(&dev->link_lock);
  2348. spin_lock_init(&dev->lock);
  2349. INIT_WORK(&dev->reset_work, emac_reset_work);
  2350. /* Init various config data based on device-tree */
  2351. err = emac_init_config(dev);
  2352. if (err != 0)
  2353. goto err_free;
  2354. /* Get interrupts. EMAC irq is mandatory, WOL irq is optional */
  2355. dev->emac_irq = irq_of_parse_and_map(np, 0);
  2356. dev->wol_irq = irq_of_parse_and_map(np, 1);
  2357. if (dev->emac_irq == NO_IRQ) {
  2358. printk(KERN_ERR "%s: Can't map main interrupt\n", np->full_name);
  2359. goto err_free;
  2360. }
  2361. ndev->irq = dev->emac_irq;
  2362. /* Map EMAC regs */
  2363. if (of_address_to_resource(np, 0, &dev->rsrc_regs)) {
  2364. printk(KERN_ERR "%s: Can't get registers address\n",
  2365. np->full_name);
  2366. goto err_irq_unmap;
  2367. }
  2368. // TODO : request_mem_region
  2369. dev->emacp = ioremap(dev->rsrc_regs.start,
  2370. dev->rsrc_regs.end - dev->rsrc_regs.start + 1);
  2371. if (dev->emacp == NULL) {
  2372. printk(KERN_ERR "%s: Can't map device registers!\n",
  2373. np->full_name);
  2374. err = -ENOMEM;
  2375. goto err_irq_unmap;
  2376. }
  2377. /* Wait for dependent devices */
  2378. err = emac_wait_deps(dev);
  2379. if (err) {
  2380. printk(KERN_ERR
  2381. "%s: Timeout waiting for dependent devices\n",
  2382. np->full_name);
  2383. /* display more info about what's missing ? */
  2384. goto err_reg_unmap;
  2385. }
  2386. dev->mal = dev_get_drvdata(&dev->mal_dev->dev);
  2387. if (dev->mdio_dev != NULL)
  2388. dev->mdio_instance = dev_get_drvdata(&dev->mdio_dev->dev);
  2389. /* Register with MAL */
  2390. dev->commac.ops = &emac_commac_ops;
  2391. dev->commac.dev = dev;
  2392. dev->commac.tx_chan_mask = MAL_CHAN_MASK(dev->mal_tx_chan);
  2393. dev->commac.rx_chan_mask = MAL_CHAN_MASK(dev->mal_rx_chan);
  2394. err = mal_register_commac(dev->mal, &dev->commac);
  2395. if (err) {
  2396. printk(KERN_ERR "%s: failed to register with mal %s!\n",
  2397. np->full_name, dev->mal_dev->dev.of_node->full_name);
  2398. goto err_rel_deps;
  2399. }
  2400. dev->rx_skb_size = emac_rx_skb_size(ndev->mtu);
  2401. dev->rx_sync_size = emac_rx_sync_size(ndev->mtu);
  2402. /* Get pointers to BD rings */
  2403. dev->tx_desc =
  2404. dev->mal->bd_virt + mal_tx_bd_offset(dev->mal, dev->mal_tx_chan);
  2405. dev->rx_desc =
  2406. dev->mal->bd_virt + mal_rx_bd_offset(dev->mal, dev->mal_rx_chan);
  2407. DBG(dev, "tx_desc %p" NL, dev->tx_desc);
  2408. DBG(dev, "rx_desc %p" NL, dev->rx_desc);
  2409. /* Clean rings */
  2410. memset(dev->tx_desc, 0, NUM_TX_BUFF * sizeof(struct mal_descriptor));
  2411. memset(dev->rx_desc, 0, NUM_RX_BUFF * sizeof(struct mal_descriptor));
  2412. memset(dev->tx_skb, 0, NUM_TX_BUFF * sizeof(struct sk_buff *));
  2413. memset(dev->rx_skb, 0, NUM_RX_BUFF * sizeof(struct sk_buff *));
  2414. /* Attach to ZMII, if needed */
  2415. if (emac_has_feature(dev, EMAC_FTR_HAS_ZMII) &&
  2416. (err = zmii_attach(dev->zmii_dev, dev->zmii_port, &dev->phy_mode)) != 0)
  2417. goto err_unreg_commac;
  2418. /* Attach to RGMII, if needed */
  2419. if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII) &&
  2420. (err = rgmii_attach(dev->rgmii_dev, dev->rgmii_port, dev->phy_mode)) != 0)
  2421. goto err_detach_zmii;
  2422. /* Attach to TAH, if needed */
  2423. if (emac_has_feature(dev, EMAC_FTR_HAS_TAH) &&
  2424. (err = tah_attach(dev->tah_dev, dev->tah_port)) != 0)
  2425. goto err_detach_rgmii;
  2426. /* Set some link defaults before we can find out real parameters */
  2427. dev->phy.speed = SPEED_100;
  2428. dev->phy.duplex = DUPLEX_FULL;
  2429. dev->phy.autoneg = AUTONEG_DISABLE;
  2430. dev->phy.pause = dev->phy.asym_pause = 0;
  2431. dev->stop_timeout = STOP_TIMEOUT_100;
  2432. INIT_DELAYED_WORK(&dev->link_work, emac_link_timer);
  2433. /* Find PHY if any */
  2434. err = emac_init_phy(dev);
  2435. if (err != 0)
  2436. goto err_detach_tah;
  2437. if (dev->tah_dev) {
  2438. ndev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG;
  2439. ndev->features |= ndev->hw_features | NETIF_F_RXCSUM;
  2440. }
  2441. ndev->watchdog_timeo = 5 * HZ;
  2442. if (emac_phy_supports_gige(dev->phy_mode)) {
  2443. ndev->netdev_ops = &emac_gige_netdev_ops;
  2444. dev->commac.ops = &emac_commac_sg_ops;
  2445. } else
  2446. ndev->netdev_ops = &emac_netdev_ops;
  2447. SET_ETHTOOL_OPS(ndev, &emac_ethtool_ops);
  2448. netif_carrier_off(ndev);
  2449. err = register_netdev(ndev);
  2450. if (err) {
  2451. printk(KERN_ERR "%s: failed to register net device (%d)!\n",
  2452. np->full_name, err);
  2453. goto err_detach_tah;
  2454. }
  2455. /* Set our drvdata last as we don't want them visible until we are
  2456. * fully initialized
  2457. */
  2458. wmb();
  2459. dev_set_drvdata(&ofdev->dev, dev);
  2460. /* There's a new kid in town ! Let's tell everybody */
  2461. wake_up_all(&emac_probe_wait);
  2462. printk(KERN_INFO "%s: EMAC-%d %s, MAC %pM\n",
  2463. ndev->name, dev->cell_index, np->full_name, ndev->dev_addr);
  2464. if (dev->phy_mode == PHY_MODE_SGMII)
  2465. printk(KERN_NOTICE "%s: in SGMII mode\n", ndev->name);
  2466. if (dev->phy.address >= 0)
  2467. printk("%s: found %s PHY (0x%02x)\n", ndev->name,
  2468. dev->phy.def->name, dev->phy.address);
  2469. emac_dbg_register(dev);
  2470. /* Life is good */
  2471. return 0;
  2472. /* I have a bad feeling about this ... */
  2473. err_detach_tah:
  2474. if (emac_has_feature(dev, EMAC_FTR_HAS_TAH))
  2475. tah_detach(dev->tah_dev, dev->tah_port);
  2476. err_detach_rgmii:
  2477. if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII))
  2478. rgmii_detach(dev->rgmii_dev, dev->rgmii_port);
  2479. err_detach_zmii:
  2480. if (emac_has_feature(dev, EMAC_FTR_HAS_ZMII))
  2481. zmii_detach(dev->zmii_dev, dev->zmii_port);
  2482. err_unreg_commac:
  2483. mal_unregister_commac(dev->mal, &dev->commac);
  2484. err_rel_deps:
  2485. emac_put_deps(dev);
  2486. err_reg_unmap:
  2487. iounmap(dev->emacp);
  2488. err_irq_unmap:
  2489. if (dev->wol_irq != NO_IRQ)
  2490. irq_dispose_mapping(dev->wol_irq);
  2491. if (dev->emac_irq != NO_IRQ)
  2492. irq_dispose_mapping(dev->emac_irq);
  2493. err_free:
  2494. free_netdev(ndev);
  2495. err_gone:
  2496. /* if we were on the bootlist, remove us as we won't show up and
  2497. * wake up all waiters to notify them in case they were waiting
  2498. * on us
  2499. */
  2500. if (blist) {
  2501. *blist = NULL;
  2502. wake_up_all(&emac_probe_wait);
  2503. }
  2504. return err;
  2505. }
  2506. static int __devexit emac_remove(struct platform_device *ofdev)
  2507. {
  2508. struct emac_instance *dev = dev_get_drvdata(&ofdev->dev);
  2509. DBG(dev, "remove" NL);
  2510. dev_set_drvdata(&ofdev->dev, NULL);
  2511. unregister_netdev(dev->ndev);
  2512. cancel_work_sync(&dev->reset_work);
  2513. if (emac_has_feature(dev, EMAC_FTR_HAS_TAH))
  2514. tah_detach(dev->tah_dev, dev->tah_port);
  2515. if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII))
  2516. rgmii_detach(dev->rgmii_dev, dev->rgmii_port);
  2517. if (emac_has_feature(dev, EMAC_FTR_HAS_ZMII))
  2518. zmii_detach(dev->zmii_dev, dev->zmii_port);
  2519. mal_unregister_commac(dev->mal, &dev->commac);
  2520. emac_put_deps(dev);
  2521. emac_dbg_unregister(dev);
  2522. iounmap(dev->emacp);
  2523. if (dev->wol_irq != NO_IRQ)
  2524. irq_dispose_mapping(dev->wol_irq);
  2525. if (dev->emac_irq != NO_IRQ)
  2526. irq_dispose_mapping(dev->emac_irq);
  2527. free_netdev(dev->ndev);
  2528. return 0;
  2529. }
  2530. /* XXX Features in here should be replaced by properties... */
  2531. static struct of_device_id emac_match[] =
  2532. {
  2533. {
  2534. .type = "network",
  2535. .compatible = "ibm,emac",
  2536. },
  2537. {
  2538. .type = "network",
  2539. .compatible = "ibm,emac4",
  2540. },
  2541. {
  2542. .type = "network",
  2543. .compatible = "ibm,emac4sync",
  2544. },
  2545. {},
  2546. };
  2547. MODULE_DEVICE_TABLE(of, emac_match);
  2548. static struct platform_driver emac_driver = {
  2549. .driver = {
  2550. .name = "emac",
  2551. .owner = THIS_MODULE,
  2552. .of_match_table = emac_match,
  2553. },
  2554. .probe = emac_probe,
  2555. .remove = emac_remove,
  2556. };
  2557. static void __init emac_make_bootlist(void)
  2558. {
  2559. struct device_node *np = NULL;
  2560. int j, max, i = 0, k;
  2561. int cell_indices[EMAC_BOOT_LIST_SIZE];
  2562. /* Collect EMACs */
  2563. while((np = of_find_all_nodes(np)) != NULL) {
  2564. const u32 *idx;
  2565. if (of_match_node(emac_match, np) == NULL)
  2566. continue;
  2567. if (of_get_property(np, "unused", NULL))
  2568. continue;
  2569. idx = of_get_property(np, "cell-index", NULL);
  2570. if (idx == NULL)
  2571. continue;
  2572. cell_indices[i] = *idx;
  2573. emac_boot_list[i++] = of_node_get(np);
  2574. if (i >= EMAC_BOOT_LIST_SIZE) {
  2575. of_node_put(np);
  2576. break;
  2577. }
  2578. }
  2579. max = i;
  2580. /* Bubble sort them (doh, what a creative algorithm :-) */
  2581. for (i = 0; max > 1 && (i < (max - 1)); i++)
  2582. for (j = i; j < max; j++) {
  2583. if (cell_indices[i] > cell_indices[j]) {
  2584. np = emac_boot_list[i];
  2585. emac_boot_list[i] = emac_boot_list[j];
  2586. emac_boot_list[j] = np;
  2587. k = cell_indices[i];
  2588. cell_indices[i] = cell_indices[j];
  2589. cell_indices[j] = k;
  2590. }
  2591. }
  2592. }
  2593. static int __init emac_init(void)
  2594. {
  2595. int rc;
  2596. printk(KERN_INFO DRV_DESC ", version " DRV_VERSION "\n");
  2597. /* Init debug stuff */
  2598. emac_init_debug();
  2599. /* Build EMAC boot list */
  2600. emac_make_bootlist();
  2601. /* Init submodules */
  2602. rc = mal_init();
  2603. if (rc)
  2604. goto err;
  2605. rc = zmii_init();
  2606. if (rc)
  2607. goto err_mal;
  2608. rc = rgmii_init();
  2609. if (rc)
  2610. goto err_zmii;
  2611. rc = tah_init();
  2612. if (rc)
  2613. goto err_rgmii;
  2614. rc = platform_driver_register(&emac_driver);
  2615. if (rc)
  2616. goto err_tah;
  2617. return 0;
  2618. err_tah:
  2619. tah_exit();
  2620. err_rgmii:
  2621. rgmii_exit();
  2622. err_zmii:
  2623. zmii_exit();
  2624. err_mal:
  2625. mal_exit();
  2626. err:
  2627. return rc;
  2628. }
  2629. static void __exit emac_exit(void)
  2630. {
  2631. int i;
  2632. platform_driver_unregister(&emac_driver);
  2633. tah_exit();
  2634. rgmii_exit();
  2635. zmii_exit();
  2636. mal_exit();
  2637. emac_fini_debug();
  2638. /* Destroy EMAC boot list */
  2639. for (i = 0; i < EMAC_BOOT_LIST_SIZE; i++)
  2640. if (emac_boot_list[i])
  2641. of_node_put(emac_boot_list[i]);
  2642. }
  2643. module_init(emac_init);
  2644. module_exit(emac_exit);