fec_main.c 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731
  1. /*
  2. * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
  3. * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
  4. *
  5. * Right now, I am very wasteful with the buffers. I allocate memory
  6. * pages and then divide them into 2K frame buffers. This way I know I
  7. * have buffers large enough to hold one frame within one buffer descriptor.
  8. * Once I get this working, I will use 64 or 128 byte CPM buffers, which
  9. * will be much more memory efficient and will easily handle lots of
  10. * small packets.
  11. *
  12. * Much better multiple PHY support by Magnus Damm.
  13. * Copyright (c) 2000 Ericsson Radio Systems AB.
  14. *
  15. * Support for FEC controller of ColdFire processors.
  16. * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com)
  17. *
  18. * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be)
  19. * Copyright (c) 2004-2006 Macq Electronique SA.
  20. *
  21. * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/string.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/errno.h>
  29. #include <linux/ioport.h>
  30. #include <linux/slab.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/delay.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/etherdevice.h>
  35. #include <linux/skbuff.h>
  36. #include <linux/in.h>
  37. #include <linux/ip.h>
  38. #include <net/ip.h>
  39. #include <net/tso.h>
  40. #include <linux/tcp.h>
  41. #include <linux/udp.h>
  42. #include <linux/icmp.h>
  43. #include <linux/spinlock.h>
  44. #include <linux/workqueue.h>
  45. #include <linux/bitops.h>
  46. #include <linux/io.h>
  47. #include <linux/irq.h>
  48. #include <linux/clk.h>
  49. #include <linux/platform_device.h>
  50. #include <linux/mdio.h>
  51. #include <linux/phy.h>
  52. #include <linux/fec.h>
  53. #include <linux/of.h>
  54. #include <linux/of_device.h>
  55. #include <linux/of_gpio.h>
  56. #include <linux/of_mdio.h>
  57. #include <linux/of_net.h>
  58. #include <linux/regulator/consumer.h>
  59. #include <linux/if_vlan.h>
  60. #include <linux/pinctrl/consumer.h>
  61. #include <linux/prefetch.h>
  62. #include <soc/imx/cpuidle.h>
  63. #include <asm/cacheflush.h>
  64. #include "fec.h"
  65. static void set_multicast_list(struct net_device *ndev);
  66. static void fec_enet_itr_coal_init(struct net_device *ndev);
  67. #define DRIVER_NAME "fec"
  68. #define FEC_ENET_GET_QUQUE(_x) ((_x == 0) ? 1 : ((_x == 1) ? 2 : 0))
  69. /* Pause frame feild and FIFO threshold */
  70. #define FEC_ENET_FCE (1 << 5)
  71. #define FEC_ENET_RSEM_V 0x84
  72. #define FEC_ENET_RSFL_V 16
  73. #define FEC_ENET_RAEM_V 0x8
  74. #define FEC_ENET_RAFL_V 0x8
  75. #define FEC_ENET_OPD_V 0xFFF0
  76. #define FEC_MDIO_PM_TIMEOUT 100 /* ms */
  77. static struct platform_device_id fec_devtype[] = {
  78. {
  79. /* keep it for coldfire */
  80. .name = DRIVER_NAME,
  81. .driver_data = 0,
  82. }, {
  83. .name = "imx25-fec",
  84. .driver_data = FEC_QUIRK_USE_GASKET | FEC_QUIRK_MIB_CLEAR,
  85. }, {
  86. .name = "imx27-fec",
  87. .driver_data = FEC_QUIRK_MIB_CLEAR,
  88. }, {
  89. .name = "imx28-fec",
  90. .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME |
  91. FEC_QUIRK_SINGLE_MDIO | FEC_QUIRK_HAS_RACC,
  92. }, {
  93. .name = "imx6q-fec",
  94. .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
  95. FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
  96. FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358 |
  97. FEC_QUIRK_HAS_RACC,
  98. }, {
  99. .name = "mvf600-fec",
  100. .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_RACC,
  101. }, {
  102. .name = "imx6sx-fec",
  103. .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
  104. FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
  105. FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
  106. FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
  107. FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE,
  108. }, {
  109. .name = "imx6ul-fec",
  110. .driver_data = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
  111. FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
  112. FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR007885 |
  113. FEC_QUIRK_BUG_CAPTURE | FEC_QUIRK_HAS_RACC |
  114. FEC_QUIRK_HAS_COALESCE,
  115. }, {
  116. /* sentinel */
  117. }
  118. };
  119. MODULE_DEVICE_TABLE(platform, fec_devtype);
  120. enum imx_fec_type {
  121. IMX25_FEC = 1, /* runs on i.mx25/50/53 */
  122. IMX27_FEC, /* runs on i.mx27/35/51 */
  123. IMX28_FEC,
  124. IMX6Q_FEC,
  125. MVF600_FEC,
  126. IMX6SX_FEC,
  127. IMX6UL_FEC,
  128. };
  129. static const struct of_device_id fec_dt_ids[] = {
  130. { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
  131. { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
  132. { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
  133. { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
  134. { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
  135. { .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], },
  136. { .compatible = "fsl,imx6ul-fec", .data = &fec_devtype[IMX6UL_FEC], },
  137. { /* sentinel */ }
  138. };
  139. MODULE_DEVICE_TABLE(of, fec_dt_ids);
  140. static unsigned char macaddr[ETH_ALEN];
  141. module_param_array(macaddr, byte, NULL, 0);
  142. MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
  143. #if defined(CONFIG_M5272)
  144. /*
  145. * Some hardware gets it MAC address out of local flash memory.
  146. * if this is non-zero then assume it is the address to get MAC from.
  147. */
  148. #if defined(CONFIG_NETtel)
  149. #define FEC_FLASHMAC 0xf0006006
  150. #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
  151. #define FEC_FLASHMAC 0xf0006000
  152. #elif defined(CONFIG_CANCam)
  153. #define FEC_FLASHMAC 0xf0020000
  154. #elif defined (CONFIG_M5272C3)
  155. #define FEC_FLASHMAC (0xffe04000 + 4)
  156. #elif defined(CONFIG_MOD5272)
  157. #define FEC_FLASHMAC 0xffc0406b
  158. #else
  159. #define FEC_FLASHMAC 0
  160. #endif
  161. #endif /* CONFIG_M5272 */
  162. /* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
  163. *
  164. * 2048 byte skbufs are allocated. However, alignment requirements
  165. * varies between FEC variants. Worst case is 64, so round down by 64.
  166. */
  167. #define PKT_MAXBUF_SIZE (round_down(2048 - 64, 64))
  168. #define PKT_MINBUF_SIZE 64
  169. /* FEC receive acceleration */
  170. #define FEC_RACC_IPDIS (1 << 1)
  171. #define FEC_RACC_PRODIS (1 << 2)
  172. #define FEC_RACC_SHIFT16 BIT(7)
  173. #define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
  174. /* MIB Control Register */
  175. #define FEC_MIB_CTRLSTAT_DISABLE BIT(31)
  176. /*
  177. * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
  178. * size bits. Other FEC hardware does not, so we need to take that into
  179. * account when setting it.
  180. */
  181. #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
  182. defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \
  183. defined(CONFIG_ARM64)
  184. #define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
  185. #else
  186. #define OPT_FRAME_SIZE 0
  187. #endif
  188. /* FEC MII MMFR bits definition */
  189. #define FEC_MMFR_ST (1 << 30)
  190. #define FEC_MMFR_OP_READ (2 << 28)
  191. #define FEC_MMFR_OP_WRITE (1 << 28)
  192. #define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
  193. #define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
  194. #define FEC_MMFR_TA (2 << 16)
  195. #define FEC_MMFR_DATA(v) (v & 0xffff)
  196. /* FEC ECR bits definition */
  197. #define FEC_ECR_MAGICEN (1 << 2)
  198. #define FEC_ECR_SLEEP (1 << 3)
  199. #define FEC_MII_TIMEOUT 30000 /* us */
  200. /* Transmitter timeout */
  201. #define TX_TIMEOUT (2 * HZ)
  202. #define FEC_PAUSE_FLAG_AUTONEG 0x1
  203. #define FEC_PAUSE_FLAG_ENABLE 0x2
  204. #define FEC_WOL_HAS_MAGIC_PACKET (0x1 << 0)
  205. #define FEC_WOL_FLAG_ENABLE (0x1 << 1)
  206. #define FEC_WOL_FLAG_SLEEP_ON (0x1 << 2)
  207. #define COPYBREAK_DEFAULT 256
  208. /* Max number of allowed TCP segments for software TSO */
  209. #define FEC_MAX_TSO_SEGS 100
  210. #define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
  211. #define IS_TSO_HEADER(txq, addr) \
  212. ((addr >= txq->tso_hdrs_dma) && \
  213. (addr < txq->tso_hdrs_dma + txq->bd.ring_size * TSO_HEADER_SIZE))
  214. static int mii_cnt;
  215. static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
  216. struct bufdesc_prop *bd)
  217. {
  218. return (bdp >= bd->last) ? bd->base
  219. : (struct bufdesc *)(((void *)bdp) + bd->dsize);
  220. }
  221. static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
  222. struct bufdesc_prop *bd)
  223. {
  224. return (bdp <= bd->base) ? bd->last
  225. : (struct bufdesc *)(((void *)bdp) - bd->dsize);
  226. }
  227. static int fec_enet_get_bd_index(struct bufdesc *bdp,
  228. struct bufdesc_prop *bd)
  229. {
  230. return ((const char *)bdp - (const char *)bd->base) >> bd->dsize_log2;
  231. }
  232. static int fec_enet_get_free_txdesc_num(struct fec_enet_priv_tx_q *txq)
  233. {
  234. int entries;
  235. entries = (((const char *)txq->dirty_tx -
  236. (const char *)txq->bd.cur) >> txq->bd.dsize_log2) - 1;
  237. return entries >= 0 ? entries : entries + txq->bd.ring_size;
  238. }
  239. static void swap_buffer(void *bufaddr, int len)
  240. {
  241. int i;
  242. unsigned int *buf = bufaddr;
  243. for (i = 0; i < len; i += 4, buf++)
  244. swab32s(buf);
  245. }
  246. static void swap_buffer2(void *dst_buf, void *src_buf, int len)
  247. {
  248. int i;
  249. unsigned int *src = src_buf;
  250. unsigned int *dst = dst_buf;
  251. for (i = 0; i < len; i += 4, src++, dst++)
  252. *dst = swab32p(src);
  253. }
  254. static void fec_dump(struct net_device *ndev)
  255. {
  256. struct fec_enet_private *fep = netdev_priv(ndev);
  257. struct bufdesc *bdp;
  258. struct fec_enet_priv_tx_q *txq;
  259. int index = 0;
  260. netdev_info(ndev, "TX ring dump\n");
  261. pr_info("Nr SC addr len SKB\n");
  262. txq = fep->tx_queue[0];
  263. bdp = txq->bd.base;
  264. do {
  265. pr_info("%3u %c%c 0x%04x 0x%08x %4u %p\n",
  266. index,
  267. bdp == txq->bd.cur ? 'S' : ' ',
  268. bdp == txq->dirty_tx ? 'H' : ' ',
  269. fec16_to_cpu(bdp->cbd_sc),
  270. fec32_to_cpu(bdp->cbd_bufaddr),
  271. fec16_to_cpu(bdp->cbd_datlen),
  272. txq->tx_skbuff[index]);
  273. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  274. index++;
  275. } while (bdp != txq->bd.base);
  276. }
  277. static inline bool is_ipv4_pkt(struct sk_buff *skb)
  278. {
  279. return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
  280. }
  281. static int
  282. fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
  283. {
  284. /* Only run for packets requiring a checksum. */
  285. if (skb->ip_summed != CHECKSUM_PARTIAL)
  286. return 0;
  287. if (unlikely(skb_cow_head(skb, 0)))
  288. return -1;
  289. if (is_ipv4_pkt(skb))
  290. ip_hdr(skb)->check = 0;
  291. *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
  292. return 0;
  293. }
  294. static struct bufdesc *
  295. fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
  296. struct sk_buff *skb,
  297. struct net_device *ndev)
  298. {
  299. struct fec_enet_private *fep = netdev_priv(ndev);
  300. struct bufdesc *bdp = txq->bd.cur;
  301. struct bufdesc_ex *ebdp;
  302. int nr_frags = skb_shinfo(skb)->nr_frags;
  303. int frag, frag_len;
  304. unsigned short status;
  305. unsigned int estatus = 0;
  306. skb_frag_t *this_frag;
  307. unsigned int index;
  308. void *bufaddr;
  309. dma_addr_t addr;
  310. int i;
  311. for (frag = 0; frag < nr_frags; frag++) {
  312. this_frag = &skb_shinfo(skb)->frags[frag];
  313. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  314. ebdp = (struct bufdesc_ex *)bdp;
  315. status = fec16_to_cpu(bdp->cbd_sc);
  316. status &= ~BD_ENET_TX_STATS;
  317. status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
  318. frag_len = skb_shinfo(skb)->frags[frag].size;
  319. /* Handle the last BD specially */
  320. if (frag == nr_frags - 1) {
  321. status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
  322. if (fep->bufdesc_ex) {
  323. estatus |= BD_ENET_TX_INT;
  324. if (unlikely(skb_shinfo(skb)->tx_flags &
  325. SKBTX_HW_TSTAMP && fep->hwts_tx_en))
  326. estatus |= BD_ENET_TX_TS;
  327. }
  328. }
  329. if (fep->bufdesc_ex) {
  330. if (fep->quirks & FEC_QUIRK_HAS_AVB)
  331. estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
  332. if (skb->ip_summed == CHECKSUM_PARTIAL)
  333. estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
  334. ebdp->cbd_bdu = 0;
  335. ebdp->cbd_esc = cpu_to_fec32(estatus);
  336. }
  337. bufaddr = page_address(this_frag->page.p) + this_frag->page_offset;
  338. index = fec_enet_get_bd_index(bdp, &txq->bd);
  339. if (((unsigned long) bufaddr) & fep->tx_align ||
  340. fep->quirks & FEC_QUIRK_SWAP_FRAME) {
  341. memcpy(txq->tx_bounce[index], bufaddr, frag_len);
  342. bufaddr = txq->tx_bounce[index];
  343. if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
  344. swap_buffer(bufaddr, frag_len);
  345. }
  346. addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
  347. DMA_TO_DEVICE);
  348. if (dma_mapping_error(&fep->pdev->dev, addr)) {
  349. if (net_ratelimit())
  350. netdev_err(ndev, "Tx DMA memory map failed\n");
  351. goto dma_mapping_error;
  352. }
  353. bdp->cbd_bufaddr = cpu_to_fec32(addr);
  354. bdp->cbd_datlen = cpu_to_fec16(frag_len);
  355. /* Make sure the updates to rest of the descriptor are
  356. * performed before transferring ownership.
  357. */
  358. wmb();
  359. bdp->cbd_sc = cpu_to_fec16(status);
  360. }
  361. return bdp;
  362. dma_mapping_error:
  363. bdp = txq->bd.cur;
  364. for (i = 0; i < frag; i++) {
  365. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  366. dma_unmap_single(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr),
  367. fec16_to_cpu(bdp->cbd_datlen), DMA_TO_DEVICE);
  368. }
  369. return ERR_PTR(-ENOMEM);
  370. }
  371. static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
  372. struct sk_buff *skb, struct net_device *ndev)
  373. {
  374. struct fec_enet_private *fep = netdev_priv(ndev);
  375. int nr_frags = skb_shinfo(skb)->nr_frags;
  376. struct bufdesc *bdp, *last_bdp;
  377. void *bufaddr;
  378. dma_addr_t addr;
  379. unsigned short status;
  380. unsigned short buflen;
  381. unsigned int estatus = 0;
  382. unsigned int index;
  383. int entries_free;
  384. entries_free = fec_enet_get_free_txdesc_num(txq);
  385. if (entries_free < MAX_SKB_FRAGS + 1) {
  386. dev_kfree_skb_any(skb);
  387. if (net_ratelimit())
  388. netdev_err(ndev, "NOT enough BD for SG!\n");
  389. return NETDEV_TX_OK;
  390. }
  391. /* Protocol checksum off-load for TCP and UDP. */
  392. if (fec_enet_clear_csum(skb, ndev)) {
  393. dev_kfree_skb_any(skb);
  394. return NETDEV_TX_OK;
  395. }
  396. /* Fill in a Tx ring entry */
  397. bdp = txq->bd.cur;
  398. last_bdp = bdp;
  399. status = fec16_to_cpu(bdp->cbd_sc);
  400. status &= ~BD_ENET_TX_STATS;
  401. /* Set buffer length and buffer pointer */
  402. bufaddr = skb->data;
  403. buflen = skb_headlen(skb);
  404. index = fec_enet_get_bd_index(bdp, &txq->bd);
  405. if (((unsigned long) bufaddr) & fep->tx_align ||
  406. fep->quirks & FEC_QUIRK_SWAP_FRAME) {
  407. memcpy(txq->tx_bounce[index], skb->data, buflen);
  408. bufaddr = txq->tx_bounce[index];
  409. if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
  410. swap_buffer(bufaddr, buflen);
  411. }
  412. /* Push the data cache so the CPM does not get stale memory data. */
  413. addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
  414. if (dma_mapping_error(&fep->pdev->dev, addr)) {
  415. dev_kfree_skb_any(skb);
  416. if (net_ratelimit())
  417. netdev_err(ndev, "Tx DMA memory map failed\n");
  418. return NETDEV_TX_OK;
  419. }
  420. if (nr_frags) {
  421. last_bdp = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
  422. if (IS_ERR(last_bdp)) {
  423. dma_unmap_single(&fep->pdev->dev, addr,
  424. buflen, DMA_TO_DEVICE);
  425. dev_kfree_skb_any(skb);
  426. return NETDEV_TX_OK;
  427. }
  428. } else {
  429. status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
  430. if (fep->bufdesc_ex) {
  431. estatus = BD_ENET_TX_INT;
  432. if (unlikely(skb_shinfo(skb)->tx_flags &
  433. SKBTX_HW_TSTAMP && fep->hwts_tx_en))
  434. estatus |= BD_ENET_TX_TS;
  435. }
  436. }
  437. bdp->cbd_bufaddr = cpu_to_fec32(addr);
  438. bdp->cbd_datlen = cpu_to_fec16(buflen);
  439. if (fep->bufdesc_ex) {
  440. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  441. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
  442. fep->hwts_tx_en))
  443. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  444. if (fep->quirks & FEC_QUIRK_HAS_AVB)
  445. estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
  446. if (skb->ip_summed == CHECKSUM_PARTIAL)
  447. estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
  448. ebdp->cbd_bdu = 0;
  449. ebdp->cbd_esc = cpu_to_fec32(estatus);
  450. }
  451. index = fec_enet_get_bd_index(last_bdp, &txq->bd);
  452. /* Save skb pointer */
  453. txq->tx_skbuff[index] = skb;
  454. /* Make sure the updates to rest of the descriptor are performed before
  455. * transferring ownership.
  456. */
  457. wmb();
  458. /* Send it on its way. Tell FEC it's ready, interrupt when done,
  459. * it's the last BD of the frame, and to put the CRC on the end.
  460. */
  461. status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
  462. bdp->cbd_sc = cpu_to_fec16(status);
  463. /* If this was the last BD in the ring, start at the beginning again. */
  464. bdp = fec_enet_get_nextdesc(last_bdp, &txq->bd);
  465. skb_tx_timestamp(skb);
  466. /* Make sure the update to bdp and tx_skbuff are performed before
  467. * txq->bd.cur.
  468. */
  469. wmb();
  470. txq->bd.cur = bdp;
  471. /* Trigger transmission start */
  472. writel(0, txq->bd.reg_desc_active);
  473. return 0;
  474. }
  475. static int
  476. fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
  477. struct net_device *ndev,
  478. struct bufdesc *bdp, int index, char *data,
  479. int size, bool last_tcp, bool is_last)
  480. {
  481. struct fec_enet_private *fep = netdev_priv(ndev);
  482. struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
  483. unsigned short status;
  484. unsigned int estatus = 0;
  485. dma_addr_t addr;
  486. status = fec16_to_cpu(bdp->cbd_sc);
  487. status &= ~BD_ENET_TX_STATS;
  488. status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
  489. if (((unsigned long) data) & fep->tx_align ||
  490. fep->quirks & FEC_QUIRK_SWAP_FRAME) {
  491. memcpy(txq->tx_bounce[index], data, size);
  492. data = txq->tx_bounce[index];
  493. if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
  494. swap_buffer(data, size);
  495. }
  496. addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
  497. if (dma_mapping_error(&fep->pdev->dev, addr)) {
  498. dev_kfree_skb_any(skb);
  499. if (net_ratelimit())
  500. netdev_err(ndev, "Tx DMA memory map failed\n");
  501. return NETDEV_TX_BUSY;
  502. }
  503. bdp->cbd_datlen = cpu_to_fec16(size);
  504. bdp->cbd_bufaddr = cpu_to_fec32(addr);
  505. if (fep->bufdesc_ex) {
  506. if (fep->quirks & FEC_QUIRK_HAS_AVB)
  507. estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
  508. if (skb->ip_summed == CHECKSUM_PARTIAL)
  509. estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
  510. ebdp->cbd_bdu = 0;
  511. ebdp->cbd_esc = cpu_to_fec32(estatus);
  512. }
  513. /* Handle the last BD specially */
  514. if (last_tcp)
  515. status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
  516. if (is_last) {
  517. status |= BD_ENET_TX_INTR;
  518. if (fep->bufdesc_ex)
  519. ebdp->cbd_esc |= cpu_to_fec32(BD_ENET_TX_INT);
  520. }
  521. bdp->cbd_sc = cpu_to_fec16(status);
  522. return 0;
  523. }
  524. static int
  525. fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
  526. struct sk_buff *skb, struct net_device *ndev,
  527. struct bufdesc *bdp, int index)
  528. {
  529. struct fec_enet_private *fep = netdev_priv(ndev);
  530. int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  531. struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
  532. void *bufaddr;
  533. unsigned long dmabuf;
  534. unsigned short status;
  535. unsigned int estatus = 0;
  536. status = fec16_to_cpu(bdp->cbd_sc);
  537. status &= ~BD_ENET_TX_STATS;
  538. status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
  539. bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
  540. dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
  541. if (((unsigned long)bufaddr) & fep->tx_align ||
  542. fep->quirks & FEC_QUIRK_SWAP_FRAME) {
  543. memcpy(txq->tx_bounce[index], skb->data, hdr_len);
  544. bufaddr = txq->tx_bounce[index];
  545. if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
  546. swap_buffer(bufaddr, hdr_len);
  547. dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
  548. hdr_len, DMA_TO_DEVICE);
  549. if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
  550. dev_kfree_skb_any(skb);
  551. if (net_ratelimit())
  552. netdev_err(ndev, "Tx DMA memory map failed\n");
  553. return NETDEV_TX_BUSY;
  554. }
  555. }
  556. bdp->cbd_bufaddr = cpu_to_fec32(dmabuf);
  557. bdp->cbd_datlen = cpu_to_fec16(hdr_len);
  558. if (fep->bufdesc_ex) {
  559. if (fep->quirks & FEC_QUIRK_HAS_AVB)
  560. estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
  561. if (skb->ip_summed == CHECKSUM_PARTIAL)
  562. estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
  563. ebdp->cbd_bdu = 0;
  564. ebdp->cbd_esc = cpu_to_fec32(estatus);
  565. }
  566. bdp->cbd_sc = cpu_to_fec16(status);
  567. return 0;
  568. }
  569. static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
  570. struct sk_buff *skb,
  571. struct net_device *ndev)
  572. {
  573. struct fec_enet_private *fep = netdev_priv(ndev);
  574. int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  575. int total_len, data_left;
  576. struct bufdesc *bdp = txq->bd.cur;
  577. struct tso_t tso;
  578. unsigned int index = 0;
  579. int ret;
  580. if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(txq)) {
  581. dev_kfree_skb_any(skb);
  582. if (net_ratelimit())
  583. netdev_err(ndev, "NOT enough BD for TSO!\n");
  584. return NETDEV_TX_OK;
  585. }
  586. /* Protocol checksum off-load for TCP and UDP. */
  587. if (fec_enet_clear_csum(skb, ndev)) {
  588. dev_kfree_skb_any(skb);
  589. return NETDEV_TX_OK;
  590. }
  591. /* Initialize the TSO handler, and prepare the first payload */
  592. tso_start(skb, &tso);
  593. total_len = skb->len - hdr_len;
  594. while (total_len > 0) {
  595. char *hdr;
  596. index = fec_enet_get_bd_index(bdp, &txq->bd);
  597. data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
  598. total_len -= data_left;
  599. /* prepare packet headers: MAC + IP + TCP */
  600. hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
  601. tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
  602. ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
  603. if (ret)
  604. goto err_release;
  605. while (data_left > 0) {
  606. int size;
  607. size = min_t(int, tso.size, data_left);
  608. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  609. index = fec_enet_get_bd_index(bdp, &txq->bd);
  610. ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
  611. bdp, index,
  612. tso.data, size,
  613. size == data_left,
  614. total_len == 0);
  615. if (ret)
  616. goto err_release;
  617. data_left -= size;
  618. tso_build_data(skb, &tso, size);
  619. }
  620. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  621. }
  622. /* Save skb pointer */
  623. txq->tx_skbuff[index] = skb;
  624. skb_tx_timestamp(skb);
  625. txq->bd.cur = bdp;
  626. /* Trigger transmission start */
  627. if (!(fep->quirks & FEC_QUIRK_ERR007885) ||
  628. !readl(txq->bd.reg_desc_active) ||
  629. !readl(txq->bd.reg_desc_active) ||
  630. !readl(txq->bd.reg_desc_active) ||
  631. !readl(txq->bd.reg_desc_active))
  632. writel(0, txq->bd.reg_desc_active);
  633. return 0;
  634. err_release:
  635. /* TODO: Release all used data descriptors for TSO */
  636. return ret;
  637. }
  638. static netdev_tx_t
  639. fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  640. {
  641. struct fec_enet_private *fep = netdev_priv(ndev);
  642. int entries_free;
  643. unsigned short queue;
  644. struct fec_enet_priv_tx_q *txq;
  645. struct netdev_queue *nq;
  646. int ret;
  647. queue = skb_get_queue_mapping(skb);
  648. txq = fep->tx_queue[queue];
  649. nq = netdev_get_tx_queue(ndev, queue);
  650. if (skb_is_gso(skb))
  651. ret = fec_enet_txq_submit_tso(txq, skb, ndev);
  652. else
  653. ret = fec_enet_txq_submit_skb(txq, skb, ndev);
  654. if (ret)
  655. return ret;
  656. entries_free = fec_enet_get_free_txdesc_num(txq);
  657. if (entries_free <= txq->tx_stop_threshold)
  658. netif_tx_stop_queue(nq);
  659. return NETDEV_TX_OK;
  660. }
  661. /* Init RX & TX buffer descriptors
  662. */
  663. static void fec_enet_bd_init(struct net_device *dev)
  664. {
  665. struct fec_enet_private *fep = netdev_priv(dev);
  666. struct fec_enet_priv_tx_q *txq;
  667. struct fec_enet_priv_rx_q *rxq;
  668. struct bufdesc *bdp;
  669. unsigned int i;
  670. unsigned int q;
  671. for (q = 0; q < fep->num_rx_queues; q++) {
  672. /* Initialize the receive buffer descriptors. */
  673. rxq = fep->rx_queue[q];
  674. bdp = rxq->bd.base;
  675. for (i = 0; i < rxq->bd.ring_size; i++) {
  676. /* Initialize the BD for every fragment in the page. */
  677. if (bdp->cbd_bufaddr)
  678. bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
  679. else
  680. bdp->cbd_sc = cpu_to_fec16(0);
  681. bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
  682. }
  683. /* Set the last buffer to wrap */
  684. bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
  685. bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
  686. rxq->bd.cur = rxq->bd.base;
  687. }
  688. for (q = 0; q < fep->num_tx_queues; q++) {
  689. /* ...and the same for transmit */
  690. txq = fep->tx_queue[q];
  691. bdp = txq->bd.base;
  692. txq->bd.cur = bdp;
  693. for (i = 0; i < txq->bd.ring_size; i++) {
  694. /* Initialize the BD for every fragment in the page. */
  695. bdp->cbd_sc = cpu_to_fec16(0);
  696. if (bdp->cbd_bufaddr &&
  697. !IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
  698. dma_unmap_single(&fep->pdev->dev,
  699. fec32_to_cpu(bdp->cbd_bufaddr),
  700. fec16_to_cpu(bdp->cbd_datlen),
  701. DMA_TO_DEVICE);
  702. if (txq->tx_skbuff[i]) {
  703. dev_kfree_skb_any(txq->tx_skbuff[i]);
  704. txq->tx_skbuff[i] = NULL;
  705. }
  706. bdp->cbd_bufaddr = cpu_to_fec32(0);
  707. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  708. }
  709. /* Set the last buffer to wrap */
  710. bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
  711. bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
  712. txq->dirty_tx = bdp;
  713. }
  714. }
  715. static void fec_enet_active_rxring(struct net_device *ndev)
  716. {
  717. struct fec_enet_private *fep = netdev_priv(ndev);
  718. int i;
  719. for (i = 0; i < fep->num_rx_queues; i++)
  720. writel(0, fep->rx_queue[i]->bd.reg_desc_active);
  721. }
  722. static void fec_enet_enable_ring(struct net_device *ndev)
  723. {
  724. struct fec_enet_private *fep = netdev_priv(ndev);
  725. struct fec_enet_priv_tx_q *txq;
  726. struct fec_enet_priv_rx_q *rxq;
  727. int i;
  728. for (i = 0; i < fep->num_rx_queues; i++) {
  729. rxq = fep->rx_queue[i];
  730. writel(rxq->bd.dma, fep->hwp + FEC_R_DES_START(i));
  731. writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_R_BUFF_SIZE(i));
  732. /* enable DMA1/2 */
  733. if (i)
  734. writel(RCMR_MATCHEN | RCMR_CMP(i),
  735. fep->hwp + FEC_RCMR(i));
  736. }
  737. for (i = 0; i < fep->num_tx_queues; i++) {
  738. txq = fep->tx_queue[i];
  739. writel(txq->bd.dma, fep->hwp + FEC_X_DES_START(i));
  740. /* enable DMA1/2 */
  741. if (i)
  742. writel(DMA_CLASS_EN | IDLE_SLOPE(i),
  743. fep->hwp + FEC_DMA_CFG(i));
  744. }
  745. }
  746. static void fec_enet_reset_skb(struct net_device *ndev)
  747. {
  748. struct fec_enet_private *fep = netdev_priv(ndev);
  749. struct fec_enet_priv_tx_q *txq;
  750. int i, j;
  751. for (i = 0; i < fep->num_tx_queues; i++) {
  752. txq = fep->tx_queue[i];
  753. for (j = 0; j < txq->bd.ring_size; j++) {
  754. if (txq->tx_skbuff[j]) {
  755. dev_kfree_skb_any(txq->tx_skbuff[j]);
  756. txq->tx_skbuff[j] = NULL;
  757. }
  758. }
  759. }
  760. }
  761. /*
  762. * This function is called to start or restart the FEC during a link
  763. * change, transmit timeout, or to reconfigure the FEC. The network
  764. * packet processing for this device must be stopped before this call.
  765. */
  766. static void
  767. fec_restart(struct net_device *ndev)
  768. {
  769. struct fec_enet_private *fep = netdev_priv(ndev);
  770. u32 val;
  771. u32 temp_mac[2];
  772. u32 rcntl = OPT_FRAME_SIZE | 0x04;
  773. u32 ecntl = 0x2; /* ETHEREN */
  774. /* Whack a reset. We should wait for this.
  775. * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
  776. * instead of reset MAC itself.
  777. */
  778. if (fep->quirks & FEC_QUIRK_HAS_AVB) {
  779. writel(0, fep->hwp + FEC_ECNTRL);
  780. } else {
  781. writel(1, fep->hwp + FEC_ECNTRL);
  782. udelay(10);
  783. }
  784. /*
  785. * enet-mac reset will reset mac address registers too,
  786. * so need to reconfigure it.
  787. */
  788. memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
  789. writel((__force u32)cpu_to_be32(temp_mac[0]),
  790. fep->hwp + FEC_ADDR_LOW);
  791. writel((__force u32)cpu_to_be32(temp_mac[1]),
  792. fep->hwp + FEC_ADDR_HIGH);
  793. /* Clear any outstanding interrupt. */
  794. writel(0xffffffff, fep->hwp + FEC_IEVENT);
  795. fec_enet_bd_init(ndev);
  796. fec_enet_enable_ring(ndev);
  797. /* Reset tx SKB buffers. */
  798. fec_enet_reset_skb(ndev);
  799. /* Enable MII mode */
  800. if (fep->full_duplex == DUPLEX_FULL) {
  801. /* FD enable */
  802. writel(0x04, fep->hwp + FEC_X_CNTRL);
  803. } else {
  804. /* No Rcv on Xmit */
  805. rcntl |= 0x02;
  806. writel(0x0, fep->hwp + FEC_X_CNTRL);
  807. }
  808. /* Set MII speed */
  809. writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
  810. #if !defined(CONFIG_M5272)
  811. if (fep->quirks & FEC_QUIRK_HAS_RACC) {
  812. val = readl(fep->hwp + FEC_RACC);
  813. /* align IP header */
  814. val |= FEC_RACC_SHIFT16;
  815. if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
  816. /* set RX checksum */
  817. val |= FEC_RACC_OPTIONS;
  818. else
  819. val &= ~FEC_RACC_OPTIONS;
  820. writel(val, fep->hwp + FEC_RACC);
  821. writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_FTRL);
  822. }
  823. #endif
  824. /*
  825. * The phy interface and speed need to get configured
  826. * differently on enet-mac.
  827. */
  828. if (fep->quirks & FEC_QUIRK_ENET_MAC) {
  829. /* Enable flow control and length check */
  830. rcntl |= 0x40000000 | 0x00000020;
  831. /* RGMII, RMII or MII */
  832. if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII ||
  833. fep->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
  834. fep->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
  835. fep->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
  836. rcntl |= (1 << 6);
  837. else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
  838. rcntl |= (1 << 8);
  839. else
  840. rcntl &= ~(1 << 8);
  841. /* 1G, 100M or 10M */
  842. if (ndev->phydev) {
  843. if (ndev->phydev->speed == SPEED_1000)
  844. ecntl |= (1 << 5);
  845. else if (ndev->phydev->speed == SPEED_100)
  846. rcntl &= ~(1 << 9);
  847. else
  848. rcntl |= (1 << 9);
  849. }
  850. } else {
  851. #ifdef FEC_MIIGSK_ENR
  852. if (fep->quirks & FEC_QUIRK_USE_GASKET) {
  853. u32 cfgr;
  854. /* disable the gasket and wait */
  855. writel(0, fep->hwp + FEC_MIIGSK_ENR);
  856. while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
  857. udelay(1);
  858. /*
  859. * configure the gasket:
  860. * RMII, 50 MHz, no loopback, no echo
  861. * MII, 25 MHz, no loopback, no echo
  862. */
  863. cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
  864. ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
  865. if (ndev->phydev && ndev->phydev->speed == SPEED_10)
  866. cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
  867. writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
  868. /* re-enable the gasket */
  869. writel(2, fep->hwp + FEC_MIIGSK_ENR);
  870. }
  871. #endif
  872. }
  873. #if !defined(CONFIG_M5272)
  874. /* enable pause frame*/
  875. if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
  876. ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
  877. ndev->phydev && ndev->phydev->pause)) {
  878. rcntl |= FEC_ENET_FCE;
  879. /* set FIFO threshold parameter to reduce overrun */
  880. writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
  881. writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
  882. writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
  883. writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
  884. /* OPD */
  885. writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
  886. } else {
  887. rcntl &= ~FEC_ENET_FCE;
  888. }
  889. #endif /* !defined(CONFIG_M5272) */
  890. writel(rcntl, fep->hwp + FEC_R_CNTRL);
  891. /* Setup multicast filter. */
  892. set_multicast_list(ndev);
  893. #ifndef CONFIG_M5272
  894. writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
  895. writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
  896. #endif
  897. if (fep->quirks & FEC_QUIRK_ENET_MAC) {
  898. /* enable ENET endian swap */
  899. ecntl |= (1 << 8);
  900. /* enable ENET store and forward mode */
  901. writel(1 << 8, fep->hwp + FEC_X_WMRK);
  902. }
  903. if (fep->bufdesc_ex)
  904. ecntl |= (1 << 4);
  905. #ifndef CONFIG_M5272
  906. /* Enable the MIB statistic event counters */
  907. writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
  908. #endif
  909. /* And last, enable the transmit and receive processing */
  910. writel(ecntl, fep->hwp + FEC_ECNTRL);
  911. fec_enet_active_rxring(ndev);
  912. if (fep->bufdesc_ex)
  913. fec_ptp_start_cyclecounter(ndev);
  914. /* Enable interrupts we wish to service */
  915. if (fep->link)
  916. writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
  917. else
  918. writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
  919. /* Init the interrupt coalescing */
  920. fec_enet_itr_coal_init(ndev);
  921. }
  922. static void
  923. fec_stop(struct net_device *ndev)
  924. {
  925. struct fec_enet_private *fep = netdev_priv(ndev);
  926. struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
  927. u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
  928. u32 val;
  929. /* We cannot expect a graceful transmit stop without link !!! */
  930. if (fep->link) {
  931. writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
  932. udelay(10);
  933. if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
  934. netdev_err(ndev, "Graceful transmit stop did not complete!\n");
  935. }
  936. /* Whack a reset. We should wait for this.
  937. * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
  938. * instead of reset MAC itself.
  939. */
  940. if (!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
  941. if (fep->quirks & FEC_QUIRK_HAS_AVB) {
  942. writel(0, fep->hwp + FEC_ECNTRL);
  943. } else {
  944. writel(1, fep->hwp + FEC_ECNTRL);
  945. udelay(10);
  946. }
  947. writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
  948. } else {
  949. writel(FEC_DEFAULT_IMASK | FEC_ENET_WAKEUP, fep->hwp + FEC_IMASK);
  950. val = readl(fep->hwp + FEC_ECNTRL);
  951. val |= (FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
  952. writel(val, fep->hwp + FEC_ECNTRL);
  953. if (pdata && pdata->sleep_mode_enable)
  954. pdata->sleep_mode_enable(true);
  955. }
  956. writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
  957. /* We have to keep ENET enabled to have MII interrupt stay working */
  958. if (fep->quirks & FEC_QUIRK_ENET_MAC &&
  959. !(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
  960. writel(2, fep->hwp + FEC_ECNTRL);
  961. writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
  962. }
  963. }
  964. static void
  965. fec_timeout(struct net_device *ndev)
  966. {
  967. struct fec_enet_private *fep = netdev_priv(ndev);
  968. fec_dump(ndev);
  969. ndev->stats.tx_errors++;
  970. schedule_work(&fep->tx_timeout_work);
  971. }
  972. static void fec_enet_timeout_work(struct work_struct *work)
  973. {
  974. struct fec_enet_private *fep =
  975. container_of(work, struct fec_enet_private, tx_timeout_work);
  976. struct net_device *ndev = fep->netdev;
  977. rtnl_lock();
  978. if (netif_device_present(ndev) || netif_running(ndev)) {
  979. napi_disable(&fep->napi);
  980. netif_tx_lock_bh(ndev);
  981. fec_restart(ndev);
  982. netif_wake_queue(ndev);
  983. netif_tx_unlock_bh(ndev);
  984. napi_enable(&fep->napi);
  985. }
  986. rtnl_unlock();
  987. }
  988. static void
  989. fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
  990. struct skb_shared_hwtstamps *hwtstamps)
  991. {
  992. unsigned long flags;
  993. u64 ns;
  994. spin_lock_irqsave(&fep->tmreg_lock, flags);
  995. ns = timecounter_cyc2time(&fep->tc, ts);
  996. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  997. memset(hwtstamps, 0, sizeof(*hwtstamps));
  998. hwtstamps->hwtstamp = ns_to_ktime(ns);
  999. }
  1000. static void
  1001. fec_enet_tx_queue(struct net_device *ndev, u16 queue_id)
  1002. {
  1003. struct fec_enet_private *fep;
  1004. struct bufdesc *bdp;
  1005. unsigned short status;
  1006. struct sk_buff *skb;
  1007. struct fec_enet_priv_tx_q *txq;
  1008. struct netdev_queue *nq;
  1009. int index = 0;
  1010. int entries_free;
  1011. fep = netdev_priv(ndev);
  1012. queue_id = FEC_ENET_GET_QUQUE(queue_id);
  1013. txq = fep->tx_queue[queue_id];
  1014. /* get next bdp of dirty_tx */
  1015. nq = netdev_get_tx_queue(ndev, queue_id);
  1016. bdp = txq->dirty_tx;
  1017. /* get next bdp of dirty_tx */
  1018. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  1019. while (bdp != READ_ONCE(txq->bd.cur)) {
  1020. /* Order the load of bd.cur and cbd_sc */
  1021. rmb();
  1022. status = fec16_to_cpu(READ_ONCE(bdp->cbd_sc));
  1023. if (status & BD_ENET_TX_READY)
  1024. break;
  1025. index = fec_enet_get_bd_index(bdp, &txq->bd);
  1026. skb = txq->tx_skbuff[index];
  1027. txq->tx_skbuff[index] = NULL;
  1028. if (!IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
  1029. dma_unmap_single(&fep->pdev->dev,
  1030. fec32_to_cpu(bdp->cbd_bufaddr),
  1031. fec16_to_cpu(bdp->cbd_datlen),
  1032. DMA_TO_DEVICE);
  1033. bdp->cbd_bufaddr = cpu_to_fec32(0);
  1034. if (!skb)
  1035. goto skb_done;
  1036. /* Check for errors. */
  1037. if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
  1038. BD_ENET_TX_RL | BD_ENET_TX_UN |
  1039. BD_ENET_TX_CSL)) {
  1040. ndev->stats.tx_errors++;
  1041. if (status & BD_ENET_TX_HB) /* No heartbeat */
  1042. ndev->stats.tx_heartbeat_errors++;
  1043. if (status & BD_ENET_TX_LC) /* Late collision */
  1044. ndev->stats.tx_window_errors++;
  1045. if (status & BD_ENET_TX_RL) /* Retrans limit */
  1046. ndev->stats.tx_aborted_errors++;
  1047. if (status & BD_ENET_TX_UN) /* Underrun */
  1048. ndev->stats.tx_fifo_errors++;
  1049. if (status & BD_ENET_TX_CSL) /* Carrier lost */
  1050. ndev->stats.tx_carrier_errors++;
  1051. } else {
  1052. ndev->stats.tx_packets++;
  1053. ndev->stats.tx_bytes += skb->len;
  1054. }
  1055. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) &&
  1056. fep->bufdesc_ex) {
  1057. struct skb_shared_hwtstamps shhwtstamps;
  1058. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  1059. fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), &shhwtstamps);
  1060. skb_tstamp_tx(skb, &shhwtstamps);
  1061. }
  1062. /* Deferred means some collisions occurred during transmit,
  1063. * but we eventually sent the packet OK.
  1064. */
  1065. if (status & BD_ENET_TX_DEF)
  1066. ndev->stats.collisions++;
  1067. /* Free the sk buffer associated with this last transmit */
  1068. dev_kfree_skb_any(skb);
  1069. skb_done:
  1070. /* Make sure the update to bdp and tx_skbuff are performed
  1071. * before dirty_tx
  1072. */
  1073. wmb();
  1074. txq->dirty_tx = bdp;
  1075. /* Update pointer to next buffer descriptor to be transmitted */
  1076. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  1077. /* Since we have freed up a buffer, the ring is no longer full
  1078. */
  1079. if (netif_queue_stopped(ndev)) {
  1080. entries_free = fec_enet_get_free_txdesc_num(txq);
  1081. if (entries_free >= txq->tx_wake_threshold)
  1082. netif_tx_wake_queue(nq);
  1083. }
  1084. }
  1085. /* ERR006358: Keep the transmitter going */
  1086. if (bdp != txq->bd.cur &&
  1087. readl(txq->bd.reg_desc_active) == 0)
  1088. writel(0, txq->bd.reg_desc_active);
  1089. }
  1090. static void
  1091. fec_enet_tx(struct net_device *ndev)
  1092. {
  1093. struct fec_enet_private *fep = netdev_priv(ndev);
  1094. u16 queue_id;
  1095. /* First process class A queue, then Class B and Best Effort queue */
  1096. for_each_set_bit(queue_id, &fep->work_tx, FEC_ENET_MAX_TX_QS) {
  1097. clear_bit(queue_id, &fep->work_tx);
  1098. fec_enet_tx_queue(ndev, queue_id);
  1099. }
  1100. return;
  1101. }
  1102. static int
  1103. fec_enet_new_rxbdp(struct net_device *ndev, struct bufdesc *bdp, struct sk_buff *skb)
  1104. {
  1105. struct fec_enet_private *fep = netdev_priv(ndev);
  1106. int off;
  1107. off = ((unsigned long)skb->data) & fep->rx_align;
  1108. if (off)
  1109. skb_reserve(skb, fep->rx_align + 1 - off);
  1110. bdp->cbd_bufaddr = cpu_to_fec32(dma_map_single(&fep->pdev->dev, skb->data, FEC_ENET_RX_FRSIZE - fep->rx_align, DMA_FROM_DEVICE));
  1111. if (dma_mapping_error(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr))) {
  1112. if (net_ratelimit())
  1113. netdev_err(ndev, "Rx DMA memory map failed\n");
  1114. return -ENOMEM;
  1115. }
  1116. return 0;
  1117. }
  1118. static bool fec_enet_copybreak(struct net_device *ndev, struct sk_buff **skb,
  1119. struct bufdesc *bdp, u32 length, bool swap)
  1120. {
  1121. struct fec_enet_private *fep = netdev_priv(ndev);
  1122. struct sk_buff *new_skb;
  1123. if (length > fep->rx_copybreak)
  1124. return false;
  1125. new_skb = netdev_alloc_skb(ndev, length);
  1126. if (!new_skb)
  1127. return false;
  1128. dma_sync_single_for_cpu(&fep->pdev->dev,
  1129. fec32_to_cpu(bdp->cbd_bufaddr),
  1130. FEC_ENET_RX_FRSIZE - fep->rx_align,
  1131. DMA_FROM_DEVICE);
  1132. if (!swap)
  1133. memcpy(new_skb->data, (*skb)->data, length);
  1134. else
  1135. swap_buffer2(new_skb->data, (*skb)->data, length);
  1136. *skb = new_skb;
  1137. return true;
  1138. }
  1139. /* During a receive, the bd_rx.cur points to the current incoming buffer.
  1140. * When we update through the ring, if the next incoming buffer has
  1141. * not been given to the system, we just set the empty indicator,
  1142. * effectively tossing the packet.
  1143. */
  1144. static int
  1145. fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
  1146. {
  1147. struct fec_enet_private *fep = netdev_priv(ndev);
  1148. struct fec_enet_priv_rx_q *rxq;
  1149. struct bufdesc *bdp;
  1150. unsigned short status;
  1151. struct sk_buff *skb_new = NULL;
  1152. struct sk_buff *skb;
  1153. ushort pkt_len;
  1154. __u8 *data;
  1155. int pkt_received = 0;
  1156. struct bufdesc_ex *ebdp = NULL;
  1157. bool vlan_packet_rcvd = false;
  1158. u16 vlan_tag;
  1159. int index = 0;
  1160. bool is_copybreak;
  1161. bool need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME;
  1162. #ifdef CONFIG_M532x
  1163. flush_cache_all();
  1164. #endif
  1165. queue_id = FEC_ENET_GET_QUQUE(queue_id);
  1166. rxq = fep->rx_queue[queue_id];
  1167. /* First, grab all of the stats for the incoming packet.
  1168. * These get messed up if we get called due to a busy condition.
  1169. */
  1170. bdp = rxq->bd.cur;
  1171. while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) {
  1172. if (pkt_received >= budget)
  1173. break;
  1174. pkt_received++;
  1175. writel(FEC_ENET_RXF, fep->hwp + FEC_IEVENT);
  1176. /* Check for errors. */
  1177. status ^= BD_ENET_RX_LAST;
  1178. if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
  1179. BD_ENET_RX_CR | BD_ENET_RX_OV | BD_ENET_RX_LAST |
  1180. BD_ENET_RX_CL)) {
  1181. ndev->stats.rx_errors++;
  1182. if (status & BD_ENET_RX_OV) {
  1183. /* FIFO overrun */
  1184. ndev->stats.rx_fifo_errors++;
  1185. goto rx_processing_done;
  1186. }
  1187. if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH
  1188. | BD_ENET_RX_LAST)) {
  1189. /* Frame too long or too short. */
  1190. ndev->stats.rx_length_errors++;
  1191. if (status & BD_ENET_RX_LAST)
  1192. netdev_err(ndev, "rcv is not +last\n");
  1193. }
  1194. if (status & BD_ENET_RX_CR) /* CRC Error */
  1195. ndev->stats.rx_crc_errors++;
  1196. /* Report late collisions as a frame error. */
  1197. if (status & (BD_ENET_RX_NO | BD_ENET_RX_CL))
  1198. ndev->stats.rx_frame_errors++;
  1199. goto rx_processing_done;
  1200. }
  1201. /* Process the incoming frame. */
  1202. ndev->stats.rx_packets++;
  1203. pkt_len = fec16_to_cpu(bdp->cbd_datlen);
  1204. ndev->stats.rx_bytes += pkt_len;
  1205. index = fec_enet_get_bd_index(bdp, &rxq->bd);
  1206. skb = rxq->rx_skbuff[index];
  1207. /* The packet length includes FCS, but we don't want to
  1208. * include that when passing upstream as it messes up
  1209. * bridging applications.
  1210. */
  1211. is_copybreak = fec_enet_copybreak(ndev, &skb, bdp, pkt_len - 4,
  1212. need_swap);
  1213. if (!is_copybreak) {
  1214. skb_new = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
  1215. if (unlikely(!skb_new)) {
  1216. ndev->stats.rx_dropped++;
  1217. goto rx_processing_done;
  1218. }
  1219. dma_unmap_single(&fep->pdev->dev,
  1220. fec32_to_cpu(bdp->cbd_bufaddr),
  1221. FEC_ENET_RX_FRSIZE - fep->rx_align,
  1222. DMA_FROM_DEVICE);
  1223. }
  1224. prefetch(skb->data - NET_IP_ALIGN);
  1225. skb_put(skb, pkt_len - 4);
  1226. data = skb->data;
  1227. if (!is_copybreak && need_swap)
  1228. swap_buffer(data, pkt_len);
  1229. #if !defined(CONFIG_M5272)
  1230. if (fep->quirks & FEC_QUIRK_HAS_RACC)
  1231. data = skb_pull_inline(skb, 2);
  1232. #endif
  1233. /* Extract the enhanced buffer descriptor */
  1234. ebdp = NULL;
  1235. if (fep->bufdesc_ex)
  1236. ebdp = (struct bufdesc_ex *)bdp;
  1237. /* If this is a VLAN packet remove the VLAN Tag */
  1238. vlan_packet_rcvd = false;
  1239. if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
  1240. fep->bufdesc_ex &&
  1241. (ebdp->cbd_esc & cpu_to_fec32(BD_ENET_RX_VLAN))) {
  1242. /* Push and remove the vlan tag */
  1243. struct vlan_hdr *vlan_header =
  1244. (struct vlan_hdr *) (data + ETH_HLEN);
  1245. vlan_tag = ntohs(vlan_header->h_vlan_TCI);
  1246. vlan_packet_rcvd = true;
  1247. memmove(skb->data + VLAN_HLEN, data, ETH_ALEN * 2);
  1248. skb_pull(skb, VLAN_HLEN);
  1249. }
  1250. skb->protocol = eth_type_trans(skb, ndev);
  1251. /* Get receive timestamp from the skb */
  1252. if (fep->hwts_rx_en && fep->bufdesc_ex)
  1253. fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts),
  1254. skb_hwtstamps(skb));
  1255. if (fep->bufdesc_ex &&
  1256. (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) {
  1257. if (!(ebdp->cbd_esc & cpu_to_fec32(FLAG_RX_CSUM_ERROR))) {
  1258. /* don't check it */
  1259. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1260. } else {
  1261. skb_checksum_none_assert(skb);
  1262. }
  1263. }
  1264. /* Handle received VLAN packets */
  1265. if (vlan_packet_rcvd)
  1266. __vlan_hwaccel_put_tag(skb,
  1267. htons(ETH_P_8021Q),
  1268. vlan_tag);
  1269. napi_gro_receive(&fep->napi, skb);
  1270. if (is_copybreak) {
  1271. dma_sync_single_for_device(&fep->pdev->dev,
  1272. fec32_to_cpu(bdp->cbd_bufaddr),
  1273. FEC_ENET_RX_FRSIZE - fep->rx_align,
  1274. DMA_FROM_DEVICE);
  1275. } else {
  1276. rxq->rx_skbuff[index] = skb_new;
  1277. fec_enet_new_rxbdp(ndev, bdp, skb_new);
  1278. }
  1279. rx_processing_done:
  1280. /* Clear the status flags for this buffer */
  1281. status &= ~BD_ENET_RX_STATS;
  1282. /* Mark the buffer empty */
  1283. status |= BD_ENET_RX_EMPTY;
  1284. if (fep->bufdesc_ex) {
  1285. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  1286. ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
  1287. ebdp->cbd_prot = 0;
  1288. ebdp->cbd_bdu = 0;
  1289. }
  1290. /* Make sure the updates to rest of the descriptor are
  1291. * performed before transferring ownership.
  1292. */
  1293. wmb();
  1294. bdp->cbd_sc = cpu_to_fec16(status);
  1295. /* Update BD pointer to next entry */
  1296. bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
  1297. /* Doing this here will keep the FEC running while we process
  1298. * incoming frames. On a heavily loaded network, we should be
  1299. * able to keep up at the expense of system resources.
  1300. */
  1301. writel(0, rxq->bd.reg_desc_active);
  1302. }
  1303. rxq->bd.cur = bdp;
  1304. return pkt_received;
  1305. }
  1306. static int
  1307. fec_enet_rx(struct net_device *ndev, int budget)
  1308. {
  1309. int pkt_received = 0;
  1310. u16 queue_id;
  1311. struct fec_enet_private *fep = netdev_priv(ndev);
  1312. for_each_set_bit(queue_id, &fep->work_rx, FEC_ENET_MAX_RX_QS) {
  1313. int ret;
  1314. ret = fec_enet_rx_queue(ndev,
  1315. budget - pkt_received, queue_id);
  1316. if (ret < budget - pkt_received)
  1317. clear_bit(queue_id, &fep->work_rx);
  1318. pkt_received += ret;
  1319. }
  1320. return pkt_received;
  1321. }
  1322. static bool
  1323. fec_enet_collect_events(struct fec_enet_private *fep, uint int_events)
  1324. {
  1325. if (int_events == 0)
  1326. return false;
  1327. if (int_events & FEC_ENET_RXF_0)
  1328. fep->work_rx |= (1 << 2);
  1329. if (int_events & FEC_ENET_RXF_1)
  1330. fep->work_rx |= (1 << 0);
  1331. if (int_events & FEC_ENET_RXF_2)
  1332. fep->work_rx |= (1 << 1);
  1333. if (int_events & FEC_ENET_TXF_0)
  1334. fep->work_tx |= (1 << 2);
  1335. if (int_events & FEC_ENET_TXF_1)
  1336. fep->work_tx |= (1 << 0);
  1337. if (int_events & FEC_ENET_TXF_2)
  1338. fep->work_tx |= (1 << 1);
  1339. return true;
  1340. }
  1341. static irqreturn_t
  1342. fec_enet_interrupt(int irq, void *dev_id)
  1343. {
  1344. struct net_device *ndev = dev_id;
  1345. struct fec_enet_private *fep = netdev_priv(ndev);
  1346. uint int_events;
  1347. irqreturn_t ret = IRQ_NONE;
  1348. int_events = readl(fep->hwp + FEC_IEVENT);
  1349. writel(int_events, fep->hwp + FEC_IEVENT);
  1350. fec_enet_collect_events(fep, int_events);
  1351. if ((fep->work_tx || fep->work_rx) && fep->link) {
  1352. ret = IRQ_HANDLED;
  1353. if (napi_schedule_prep(&fep->napi)) {
  1354. /* Disable the NAPI interrupts */
  1355. writel(FEC_NAPI_IMASK, fep->hwp + FEC_IMASK);
  1356. __napi_schedule(&fep->napi);
  1357. }
  1358. }
  1359. if (int_events & FEC_ENET_MII) {
  1360. ret = IRQ_HANDLED;
  1361. complete(&fep->mdio_done);
  1362. }
  1363. return ret;
  1364. }
  1365. static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
  1366. {
  1367. struct net_device *ndev = napi->dev;
  1368. struct fec_enet_private *fep = netdev_priv(ndev);
  1369. int pkts;
  1370. pkts = fec_enet_rx(ndev, budget);
  1371. fec_enet_tx(ndev);
  1372. if (pkts < budget) {
  1373. napi_complete_done(napi, pkts);
  1374. writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
  1375. }
  1376. return pkts;
  1377. }
  1378. /* ------------------------------------------------------------------------- */
  1379. static void fec_get_mac(struct net_device *ndev)
  1380. {
  1381. struct fec_enet_private *fep = netdev_priv(ndev);
  1382. struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
  1383. unsigned char *iap, tmpaddr[ETH_ALEN];
  1384. /*
  1385. * try to get mac address in following order:
  1386. *
  1387. * 1) module parameter via kernel command line in form
  1388. * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
  1389. */
  1390. iap = macaddr;
  1391. /*
  1392. * 2) from device tree data
  1393. */
  1394. if (!is_valid_ether_addr(iap)) {
  1395. struct device_node *np = fep->pdev->dev.of_node;
  1396. if (np) {
  1397. const char *mac = of_get_mac_address(np);
  1398. if (mac)
  1399. iap = (unsigned char *) mac;
  1400. }
  1401. }
  1402. /*
  1403. * 3) from flash or fuse (via platform data)
  1404. */
  1405. if (!is_valid_ether_addr(iap)) {
  1406. #ifdef CONFIG_M5272
  1407. if (FEC_FLASHMAC)
  1408. iap = (unsigned char *)FEC_FLASHMAC;
  1409. #else
  1410. if (pdata)
  1411. iap = (unsigned char *)&pdata->mac;
  1412. #endif
  1413. }
  1414. /*
  1415. * 4) FEC mac registers set by bootloader
  1416. */
  1417. if (!is_valid_ether_addr(iap)) {
  1418. *((__be32 *) &tmpaddr[0]) =
  1419. cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
  1420. *((__be16 *) &tmpaddr[4]) =
  1421. cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
  1422. iap = &tmpaddr[0];
  1423. }
  1424. /*
  1425. * 5) random mac address
  1426. */
  1427. if (!is_valid_ether_addr(iap)) {
  1428. /* Report it and use a random ethernet address instead */
  1429. netdev_err(ndev, "Invalid MAC address: %pM\n", iap);
  1430. eth_hw_addr_random(ndev);
  1431. netdev_info(ndev, "Using random MAC address: %pM\n",
  1432. ndev->dev_addr);
  1433. return;
  1434. }
  1435. memcpy(ndev->dev_addr, iap, ETH_ALEN);
  1436. /* Adjust MAC if using macaddr */
  1437. if (iap == macaddr)
  1438. ndev->dev_addr[ETH_ALEN-1] = macaddr[ETH_ALEN-1] + fep->dev_id;
  1439. }
  1440. /* ------------------------------------------------------------------------- */
  1441. /*
  1442. * Phy section
  1443. */
  1444. static void fec_enet_adjust_link(struct net_device *ndev)
  1445. {
  1446. struct fec_enet_private *fep = netdev_priv(ndev);
  1447. struct phy_device *phy_dev = ndev->phydev;
  1448. int status_change = 0;
  1449. /* Prevent a state halted on mii error */
  1450. if (fep->mii_timeout && phy_dev->state == PHY_HALTED) {
  1451. phy_dev->state = PHY_RESUMING;
  1452. return;
  1453. }
  1454. /*
  1455. * If the netdev is down, or is going down, we're not interested
  1456. * in link state events, so just mark our idea of the link as down
  1457. * and ignore the event.
  1458. */
  1459. if (!netif_running(ndev) || !netif_device_present(ndev)) {
  1460. fep->link = 0;
  1461. } else if (phy_dev->link) {
  1462. if (!fep->link) {
  1463. fep->link = phy_dev->link;
  1464. status_change = 1;
  1465. }
  1466. if (fep->full_duplex != phy_dev->duplex) {
  1467. fep->full_duplex = phy_dev->duplex;
  1468. status_change = 1;
  1469. }
  1470. if (phy_dev->speed != fep->speed) {
  1471. fep->speed = phy_dev->speed;
  1472. status_change = 1;
  1473. }
  1474. /* if any of the above changed restart the FEC */
  1475. if (status_change) {
  1476. napi_disable(&fep->napi);
  1477. netif_tx_lock_bh(ndev);
  1478. fec_restart(ndev);
  1479. netif_wake_queue(ndev);
  1480. netif_tx_unlock_bh(ndev);
  1481. napi_enable(&fep->napi);
  1482. }
  1483. } else {
  1484. if (fep->link) {
  1485. napi_disable(&fep->napi);
  1486. netif_tx_lock_bh(ndev);
  1487. fec_stop(ndev);
  1488. netif_tx_unlock_bh(ndev);
  1489. napi_enable(&fep->napi);
  1490. fep->link = phy_dev->link;
  1491. status_change = 1;
  1492. }
  1493. }
  1494. if (status_change)
  1495. phy_print_status(phy_dev);
  1496. }
  1497. static int fec_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  1498. {
  1499. struct fec_enet_private *fep = bus->priv;
  1500. struct device *dev = &fep->pdev->dev;
  1501. unsigned long time_left;
  1502. int ret = 0;
  1503. ret = pm_runtime_get_sync(dev);
  1504. if (ret < 0)
  1505. return ret;
  1506. fep->mii_timeout = 0;
  1507. reinit_completion(&fep->mdio_done);
  1508. /* start a read op */
  1509. writel(FEC_MMFR_ST | FEC_MMFR_OP_READ |
  1510. FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
  1511. FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
  1512. /* wait for end of transfer */
  1513. time_left = wait_for_completion_timeout(&fep->mdio_done,
  1514. usecs_to_jiffies(FEC_MII_TIMEOUT));
  1515. if (time_left == 0) {
  1516. fep->mii_timeout = 1;
  1517. netdev_err(fep->netdev, "MDIO read timeout\n");
  1518. ret = -ETIMEDOUT;
  1519. goto out;
  1520. }
  1521. ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
  1522. out:
  1523. pm_runtime_mark_last_busy(dev);
  1524. pm_runtime_put_autosuspend(dev);
  1525. return ret;
  1526. }
  1527. static int fec_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
  1528. u16 value)
  1529. {
  1530. struct fec_enet_private *fep = bus->priv;
  1531. struct device *dev = &fep->pdev->dev;
  1532. unsigned long time_left;
  1533. int ret;
  1534. ret = pm_runtime_get_sync(dev);
  1535. if (ret < 0)
  1536. return ret;
  1537. else
  1538. ret = 0;
  1539. fep->mii_timeout = 0;
  1540. reinit_completion(&fep->mdio_done);
  1541. /* start a write op */
  1542. writel(FEC_MMFR_ST | FEC_MMFR_OP_WRITE |
  1543. FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(regnum) |
  1544. FEC_MMFR_TA | FEC_MMFR_DATA(value),
  1545. fep->hwp + FEC_MII_DATA);
  1546. /* wait for end of transfer */
  1547. time_left = wait_for_completion_timeout(&fep->mdio_done,
  1548. usecs_to_jiffies(FEC_MII_TIMEOUT));
  1549. if (time_left == 0) {
  1550. fep->mii_timeout = 1;
  1551. netdev_err(fep->netdev, "MDIO write timeout\n");
  1552. ret = -ETIMEDOUT;
  1553. }
  1554. pm_runtime_mark_last_busy(dev);
  1555. pm_runtime_put_autosuspend(dev);
  1556. return ret;
  1557. }
  1558. static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
  1559. {
  1560. struct fec_enet_private *fep = netdev_priv(ndev);
  1561. int ret;
  1562. if (enable) {
  1563. ret = clk_prepare_enable(fep->clk_ahb);
  1564. if (ret)
  1565. return ret;
  1566. ret = clk_prepare_enable(fep->clk_enet_out);
  1567. if (ret)
  1568. goto failed_clk_enet_out;
  1569. if (fep->clk_ptp) {
  1570. mutex_lock(&fep->ptp_clk_mutex);
  1571. ret = clk_prepare_enable(fep->clk_ptp);
  1572. if (ret) {
  1573. mutex_unlock(&fep->ptp_clk_mutex);
  1574. goto failed_clk_ptp;
  1575. } else {
  1576. fep->ptp_clk_on = true;
  1577. }
  1578. mutex_unlock(&fep->ptp_clk_mutex);
  1579. }
  1580. ret = clk_prepare_enable(fep->clk_ref);
  1581. if (ret)
  1582. goto failed_clk_ref;
  1583. phy_reset_after_clk_enable(ndev->phydev);
  1584. } else {
  1585. clk_disable_unprepare(fep->clk_ahb);
  1586. clk_disable_unprepare(fep->clk_enet_out);
  1587. if (fep->clk_ptp) {
  1588. mutex_lock(&fep->ptp_clk_mutex);
  1589. clk_disable_unprepare(fep->clk_ptp);
  1590. fep->ptp_clk_on = false;
  1591. mutex_unlock(&fep->ptp_clk_mutex);
  1592. }
  1593. clk_disable_unprepare(fep->clk_ref);
  1594. }
  1595. return 0;
  1596. failed_clk_ref:
  1597. if (fep->clk_ref)
  1598. clk_disable_unprepare(fep->clk_ref);
  1599. failed_clk_ptp:
  1600. if (fep->clk_enet_out)
  1601. clk_disable_unprepare(fep->clk_enet_out);
  1602. failed_clk_enet_out:
  1603. clk_disable_unprepare(fep->clk_ahb);
  1604. return ret;
  1605. }
  1606. static int fec_enet_mii_probe(struct net_device *ndev)
  1607. {
  1608. struct fec_enet_private *fep = netdev_priv(ndev);
  1609. struct phy_device *phy_dev = NULL;
  1610. char mdio_bus_id[MII_BUS_ID_SIZE];
  1611. char phy_name[MII_BUS_ID_SIZE + 3];
  1612. int phy_id;
  1613. int dev_id = fep->dev_id;
  1614. if (fep->phy_node) {
  1615. phy_dev = of_phy_connect(ndev, fep->phy_node,
  1616. &fec_enet_adjust_link, 0,
  1617. fep->phy_interface);
  1618. if (!phy_dev) {
  1619. netdev_err(ndev, "Unable to connect to phy\n");
  1620. return -ENODEV;
  1621. }
  1622. } else {
  1623. /* check for attached phy */
  1624. for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
  1625. if (!mdiobus_is_registered_device(fep->mii_bus, phy_id))
  1626. continue;
  1627. if (dev_id--)
  1628. continue;
  1629. strlcpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE);
  1630. break;
  1631. }
  1632. if (phy_id >= PHY_MAX_ADDR) {
  1633. netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
  1634. strlcpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE);
  1635. phy_id = 0;
  1636. }
  1637. snprintf(phy_name, sizeof(phy_name),
  1638. PHY_ID_FMT, mdio_bus_id, phy_id);
  1639. phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link,
  1640. fep->phy_interface);
  1641. }
  1642. if (IS_ERR(phy_dev)) {
  1643. netdev_err(ndev, "could not attach to PHY\n");
  1644. return PTR_ERR(phy_dev);
  1645. }
  1646. /* mask with MAC supported features */
  1647. if (fep->quirks & FEC_QUIRK_HAS_GBIT) {
  1648. phy_dev->supported &= PHY_GBIT_FEATURES;
  1649. phy_dev->supported &= ~SUPPORTED_1000baseT_Half;
  1650. #if !defined(CONFIG_M5272)
  1651. phy_dev->supported |= SUPPORTED_Pause;
  1652. #endif
  1653. }
  1654. else
  1655. phy_dev->supported &= PHY_BASIC_FEATURES;
  1656. phy_dev->advertising = phy_dev->supported;
  1657. fep->link = 0;
  1658. fep->full_duplex = 0;
  1659. phy_attached_info(phy_dev);
  1660. return 0;
  1661. }
  1662. static int fec_enet_mii_init(struct platform_device *pdev)
  1663. {
  1664. static struct mii_bus *fec0_mii_bus;
  1665. struct net_device *ndev = platform_get_drvdata(pdev);
  1666. struct fec_enet_private *fep = netdev_priv(ndev);
  1667. struct device_node *node;
  1668. int err = -ENXIO;
  1669. u32 mii_speed, holdtime;
  1670. /*
  1671. * The i.MX28 dual fec interfaces are not equal.
  1672. * Here are the differences:
  1673. *
  1674. * - fec0 supports MII & RMII modes while fec1 only supports RMII
  1675. * - fec0 acts as the 1588 time master while fec1 is slave
  1676. * - external phys can only be configured by fec0
  1677. *
  1678. * That is to say fec1 can not work independently. It only works
  1679. * when fec0 is working. The reason behind this design is that the
  1680. * second interface is added primarily for Switch mode.
  1681. *
  1682. * Because of the last point above, both phys are attached on fec0
  1683. * mdio interface in board design, and need to be configured by
  1684. * fec0 mii_bus.
  1685. */
  1686. if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) {
  1687. /* fec1 uses fec0 mii_bus */
  1688. if (mii_cnt && fec0_mii_bus) {
  1689. fep->mii_bus = fec0_mii_bus;
  1690. mii_cnt++;
  1691. return 0;
  1692. }
  1693. return -ENOENT;
  1694. }
  1695. fep->mii_timeout = 0;
  1696. /*
  1697. * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
  1698. *
  1699. * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
  1700. * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
  1701. * Reference Manual has an error on this, and gets fixed on i.MX6Q
  1702. * document.
  1703. */
  1704. mii_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 5000000);
  1705. if (fep->quirks & FEC_QUIRK_ENET_MAC)
  1706. mii_speed--;
  1707. if (mii_speed > 63) {
  1708. dev_err(&pdev->dev,
  1709. "fec clock (%lu) too fast to get right mii speed\n",
  1710. clk_get_rate(fep->clk_ipg));
  1711. err = -EINVAL;
  1712. goto err_out;
  1713. }
  1714. /*
  1715. * The i.MX28 and i.MX6 types have another filed in the MSCR (aka
  1716. * MII_SPEED) register that defines the MDIO output hold time. Earlier
  1717. * versions are RAZ there, so just ignore the difference and write the
  1718. * register always.
  1719. * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
  1720. * HOLDTIME + 1 is the number of clk cycles the fec is holding the
  1721. * output.
  1722. * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
  1723. * Given that ceil(clkrate / 5000000) <= 64, the calculation for
  1724. * holdtime cannot result in a value greater than 3.
  1725. */
  1726. holdtime = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 100000000) - 1;
  1727. fep->phy_speed = mii_speed << 1 | holdtime << 8;
  1728. writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
  1729. fep->mii_bus = mdiobus_alloc();
  1730. if (fep->mii_bus == NULL) {
  1731. err = -ENOMEM;
  1732. goto err_out;
  1733. }
  1734. fep->mii_bus->name = "fec_enet_mii_bus";
  1735. fep->mii_bus->read = fec_enet_mdio_read;
  1736. fep->mii_bus->write = fec_enet_mdio_write;
  1737. snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  1738. pdev->name, fep->dev_id + 1);
  1739. fep->mii_bus->priv = fep;
  1740. fep->mii_bus->parent = &pdev->dev;
  1741. node = of_get_child_by_name(pdev->dev.of_node, "mdio");
  1742. if (node) {
  1743. err = of_mdiobus_register(fep->mii_bus, node);
  1744. of_node_put(node);
  1745. } else {
  1746. err = mdiobus_register(fep->mii_bus);
  1747. }
  1748. if (err)
  1749. goto err_out_free_mdiobus;
  1750. mii_cnt++;
  1751. /* save fec0 mii_bus */
  1752. if (fep->quirks & FEC_QUIRK_SINGLE_MDIO)
  1753. fec0_mii_bus = fep->mii_bus;
  1754. return 0;
  1755. err_out_free_mdiobus:
  1756. mdiobus_free(fep->mii_bus);
  1757. err_out:
  1758. return err;
  1759. }
  1760. static void fec_enet_mii_remove(struct fec_enet_private *fep)
  1761. {
  1762. if (--mii_cnt == 0) {
  1763. mdiobus_unregister(fep->mii_bus);
  1764. mdiobus_free(fep->mii_bus);
  1765. }
  1766. }
  1767. static void fec_enet_get_drvinfo(struct net_device *ndev,
  1768. struct ethtool_drvinfo *info)
  1769. {
  1770. struct fec_enet_private *fep = netdev_priv(ndev);
  1771. strlcpy(info->driver, fep->pdev->dev.driver->name,
  1772. sizeof(info->driver));
  1773. strlcpy(info->version, "Revision: 1.0", sizeof(info->version));
  1774. strlcpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
  1775. }
  1776. static int fec_enet_get_regs_len(struct net_device *ndev)
  1777. {
  1778. struct fec_enet_private *fep = netdev_priv(ndev);
  1779. struct resource *r;
  1780. int s = 0;
  1781. r = platform_get_resource(fep->pdev, IORESOURCE_MEM, 0);
  1782. if (r)
  1783. s = resource_size(r);
  1784. return s;
  1785. }
  1786. /* List of registers that can be safety be read to dump them with ethtool */
  1787. #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
  1788. defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \
  1789. defined(CONFIG_ARM64)
  1790. static u32 fec_enet_register_offset[] = {
  1791. FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0,
  1792. FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL,
  1793. FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_TXIC1,
  1794. FEC_TXIC2, FEC_RXIC0, FEC_RXIC1, FEC_RXIC2, FEC_HASH_TABLE_HIGH,
  1795. FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW,
  1796. FEC_X_WMRK, FEC_R_BOUND, FEC_R_FSTART, FEC_R_DES_START_1,
  1797. FEC_X_DES_START_1, FEC_R_BUFF_SIZE_1, FEC_R_DES_START_2,
  1798. FEC_X_DES_START_2, FEC_R_BUFF_SIZE_2, FEC_R_DES_START_0,
  1799. FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM,
  1800. FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, FEC_RCMR_1, FEC_RCMR_2,
  1801. FEC_DMA_CFG_1, FEC_DMA_CFG_2, FEC_R_DES_ACTIVE_1, FEC_X_DES_ACTIVE_1,
  1802. FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_2, FEC_QOS_SCHEME,
  1803. RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT,
  1804. RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG,
  1805. RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255,
  1806. RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047,
  1807. RMON_T_P_GTE2048, RMON_T_OCTETS,
  1808. IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF,
  1809. IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE,
  1810. IEEE_T_FDXFC, IEEE_T_OCTETS_OK,
  1811. RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN,
  1812. RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB,
  1813. RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255,
  1814. RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047,
  1815. RMON_R_P_GTE2048, RMON_R_OCTETS,
  1816. IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR,
  1817. IEEE_R_FDXFC, IEEE_R_OCTETS_OK
  1818. };
  1819. #else
  1820. static u32 fec_enet_register_offset[] = {
  1821. FEC_ECNTRL, FEC_IEVENT, FEC_IMASK, FEC_IVEC, FEC_R_DES_ACTIVE_0,
  1822. FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_0,
  1823. FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2, FEC_MII_DATA, FEC_MII_SPEED,
  1824. FEC_R_BOUND, FEC_R_FSTART, FEC_X_WMRK, FEC_X_FSTART, FEC_R_CNTRL,
  1825. FEC_MAX_FRM_LEN, FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH,
  1826. FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, FEC_R_DES_START_0,
  1827. FEC_R_DES_START_1, FEC_R_DES_START_2, FEC_X_DES_START_0,
  1828. FEC_X_DES_START_1, FEC_X_DES_START_2, FEC_R_BUFF_SIZE_0,
  1829. FEC_R_BUFF_SIZE_1, FEC_R_BUFF_SIZE_2
  1830. };
  1831. #endif
  1832. static void fec_enet_get_regs(struct net_device *ndev,
  1833. struct ethtool_regs *regs, void *regbuf)
  1834. {
  1835. struct fec_enet_private *fep = netdev_priv(ndev);
  1836. u32 __iomem *theregs = (u32 __iomem *)fep->hwp;
  1837. u32 *buf = (u32 *)regbuf;
  1838. u32 i, off;
  1839. memset(buf, 0, regs->len);
  1840. for (i = 0; i < ARRAY_SIZE(fec_enet_register_offset); i++) {
  1841. off = fec_enet_register_offset[i] / 4;
  1842. buf[off] = readl(&theregs[off]);
  1843. }
  1844. }
  1845. static int fec_enet_get_ts_info(struct net_device *ndev,
  1846. struct ethtool_ts_info *info)
  1847. {
  1848. struct fec_enet_private *fep = netdev_priv(ndev);
  1849. if (fep->bufdesc_ex) {
  1850. info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
  1851. SOF_TIMESTAMPING_RX_SOFTWARE |
  1852. SOF_TIMESTAMPING_SOFTWARE |
  1853. SOF_TIMESTAMPING_TX_HARDWARE |
  1854. SOF_TIMESTAMPING_RX_HARDWARE |
  1855. SOF_TIMESTAMPING_RAW_HARDWARE;
  1856. if (fep->ptp_clock)
  1857. info->phc_index = ptp_clock_index(fep->ptp_clock);
  1858. else
  1859. info->phc_index = -1;
  1860. info->tx_types = (1 << HWTSTAMP_TX_OFF) |
  1861. (1 << HWTSTAMP_TX_ON);
  1862. info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
  1863. (1 << HWTSTAMP_FILTER_ALL);
  1864. return 0;
  1865. } else {
  1866. return ethtool_op_get_ts_info(ndev, info);
  1867. }
  1868. }
  1869. #if !defined(CONFIG_M5272)
  1870. static void fec_enet_get_pauseparam(struct net_device *ndev,
  1871. struct ethtool_pauseparam *pause)
  1872. {
  1873. struct fec_enet_private *fep = netdev_priv(ndev);
  1874. pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
  1875. pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
  1876. pause->rx_pause = pause->tx_pause;
  1877. }
  1878. static int fec_enet_set_pauseparam(struct net_device *ndev,
  1879. struct ethtool_pauseparam *pause)
  1880. {
  1881. struct fec_enet_private *fep = netdev_priv(ndev);
  1882. if (!ndev->phydev)
  1883. return -ENODEV;
  1884. if (pause->tx_pause != pause->rx_pause) {
  1885. netdev_info(ndev,
  1886. "hardware only support enable/disable both tx and rx");
  1887. return -EINVAL;
  1888. }
  1889. fep->pause_flag = 0;
  1890. /* tx pause must be same as rx pause */
  1891. fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
  1892. fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
  1893. if (pause->rx_pause || pause->autoneg) {
  1894. ndev->phydev->supported |= ADVERTISED_Pause;
  1895. ndev->phydev->advertising |= ADVERTISED_Pause;
  1896. } else {
  1897. ndev->phydev->supported &= ~ADVERTISED_Pause;
  1898. ndev->phydev->advertising &= ~ADVERTISED_Pause;
  1899. }
  1900. if (pause->autoneg) {
  1901. if (netif_running(ndev))
  1902. fec_stop(ndev);
  1903. phy_start_aneg(ndev->phydev);
  1904. }
  1905. if (netif_running(ndev)) {
  1906. napi_disable(&fep->napi);
  1907. netif_tx_lock_bh(ndev);
  1908. fec_restart(ndev);
  1909. netif_wake_queue(ndev);
  1910. netif_tx_unlock_bh(ndev);
  1911. napi_enable(&fep->napi);
  1912. }
  1913. return 0;
  1914. }
  1915. static const struct fec_stat {
  1916. char name[ETH_GSTRING_LEN];
  1917. u16 offset;
  1918. } fec_stats[] = {
  1919. /* RMON TX */
  1920. { "tx_dropped", RMON_T_DROP },
  1921. { "tx_packets", RMON_T_PACKETS },
  1922. { "tx_broadcast", RMON_T_BC_PKT },
  1923. { "tx_multicast", RMON_T_MC_PKT },
  1924. { "tx_crc_errors", RMON_T_CRC_ALIGN },
  1925. { "tx_undersize", RMON_T_UNDERSIZE },
  1926. { "tx_oversize", RMON_T_OVERSIZE },
  1927. { "tx_fragment", RMON_T_FRAG },
  1928. { "tx_jabber", RMON_T_JAB },
  1929. { "tx_collision", RMON_T_COL },
  1930. { "tx_64byte", RMON_T_P64 },
  1931. { "tx_65to127byte", RMON_T_P65TO127 },
  1932. { "tx_128to255byte", RMON_T_P128TO255 },
  1933. { "tx_256to511byte", RMON_T_P256TO511 },
  1934. { "tx_512to1023byte", RMON_T_P512TO1023 },
  1935. { "tx_1024to2047byte", RMON_T_P1024TO2047 },
  1936. { "tx_GTE2048byte", RMON_T_P_GTE2048 },
  1937. { "tx_octets", RMON_T_OCTETS },
  1938. /* IEEE TX */
  1939. { "IEEE_tx_drop", IEEE_T_DROP },
  1940. { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
  1941. { "IEEE_tx_1col", IEEE_T_1COL },
  1942. { "IEEE_tx_mcol", IEEE_T_MCOL },
  1943. { "IEEE_tx_def", IEEE_T_DEF },
  1944. { "IEEE_tx_lcol", IEEE_T_LCOL },
  1945. { "IEEE_tx_excol", IEEE_T_EXCOL },
  1946. { "IEEE_tx_macerr", IEEE_T_MACERR },
  1947. { "IEEE_tx_cserr", IEEE_T_CSERR },
  1948. { "IEEE_tx_sqe", IEEE_T_SQE },
  1949. { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
  1950. { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
  1951. /* RMON RX */
  1952. { "rx_packets", RMON_R_PACKETS },
  1953. { "rx_broadcast", RMON_R_BC_PKT },
  1954. { "rx_multicast", RMON_R_MC_PKT },
  1955. { "rx_crc_errors", RMON_R_CRC_ALIGN },
  1956. { "rx_undersize", RMON_R_UNDERSIZE },
  1957. { "rx_oversize", RMON_R_OVERSIZE },
  1958. { "rx_fragment", RMON_R_FRAG },
  1959. { "rx_jabber", RMON_R_JAB },
  1960. { "rx_64byte", RMON_R_P64 },
  1961. { "rx_65to127byte", RMON_R_P65TO127 },
  1962. { "rx_128to255byte", RMON_R_P128TO255 },
  1963. { "rx_256to511byte", RMON_R_P256TO511 },
  1964. { "rx_512to1023byte", RMON_R_P512TO1023 },
  1965. { "rx_1024to2047byte", RMON_R_P1024TO2047 },
  1966. { "rx_GTE2048byte", RMON_R_P_GTE2048 },
  1967. { "rx_octets", RMON_R_OCTETS },
  1968. /* IEEE RX */
  1969. { "IEEE_rx_drop", IEEE_R_DROP },
  1970. { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
  1971. { "IEEE_rx_crc", IEEE_R_CRC },
  1972. { "IEEE_rx_align", IEEE_R_ALIGN },
  1973. { "IEEE_rx_macerr", IEEE_R_MACERR },
  1974. { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
  1975. { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
  1976. };
  1977. #define FEC_STATS_SIZE (ARRAY_SIZE(fec_stats) * sizeof(u64))
  1978. static void fec_enet_update_ethtool_stats(struct net_device *dev)
  1979. {
  1980. struct fec_enet_private *fep = netdev_priv(dev);
  1981. int i;
  1982. for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
  1983. fep->ethtool_stats[i] = readl(fep->hwp + fec_stats[i].offset);
  1984. }
  1985. static void fec_enet_get_ethtool_stats(struct net_device *dev,
  1986. struct ethtool_stats *stats, u64 *data)
  1987. {
  1988. struct fec_enet_private *fep = netdev_priv(dev);
  1989. if (netif_running(dev))
  1990. fec_enet_update_ethtool_stats(dev);
  1991. memcpy(data, fep->ethtool_stats, FEC_STATS_SIZE);
  1992. }
  1993. static void fec_enet_get_strings(struct net_device *netdev,
  1994. u32 stringset, u8 *data)
  1995. {
  1996. int i;
  1997. switch (stringset) {
  1998. case ETH_SS_STATS:
  1999. for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
  2000. memcpy(data + i * ETH_GSTRING_LEN,
  2001. fec_stats[i].name, ETH_GSTRING_LEN);
  2002. break;
  2003. }
  2004. }
  2005. static int fec_enet_get_sset_count(struct net_device *dev, int sset)
  2006. {
  2007. switch (sset) {
  2008. case ETH_SS_STATS:
  2009. return ARRAY_SIZE(fec_stats);
  2010. default:
  2011. return -EOPNOTSUPP;
  2012. }
  2013. }
  2014. static void fec_enet_clear_ethtool_stats(struct net_device *dev)
  2015. {
  2016. struct fec_enet_private *fep = netdev_priv(dev);
  2017. int i;
  2018. /* Disable MIB statistics counters */
  2019. writel(FEC_MIB_CTRLSTAT_DISABLE, fep->hwp + FEC_MIB_CTRLSTAT);
  2020. for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
  2021. writel(0, fep->hwp + fec_stats[i].offset);
  2022. /* Don't disable MIB statistics counters */
  2023. writel(0, fep->hwp + FEC_MIB_CTRLSTAT);
  2024. }
  2025. #else /* !defined(CONFIG_M5272) */
  2026. #define FEC_STATS_SIZE 0
  2027. static inline void fec_enet_update_ethtool_stats(struct net_device *dev)
  2028. {
  2029. }
  2030. static inline void fec_enet_clear_ethtool_stats(struct net_device *dev)
  2031. {
  2032. }
  2033. #endif /* !defined(CONFIG_M5272) */
  2034. /* ITR clock source is enet system clock (clk_ahb).
  2035. * TCTT unit is cycle_ns * 64 cycle
  2036. * So, the ICTT value = X us / (cycle_ns * 64)
  2037. */
  2038. static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
  2039. {
  2040. struct fec_enet_private *fep = netdev_priv(ndev);
  2041. return us * (fep->itr_clk_rate / 64000) / 1000;
  2042. }
  2043. /* Set threshold for interrupt coalescing */
  2044. static void fec_enet_itr_coal_set(struct net_device *ndev)
  2045. {
  2046. struct fec_enet_private *fep = netdev_priv(ndev);
  2047. int rx_itr, tx_itr;
  2048. /* Must be greater than zero to avoid unpredictable behavior */
  2049. if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
  2050. !fep->tx_time_itr || !fep->tx_pkts_itr)
  2051. return;
  2052. /* Select enet system clock as Interrupt Coalescing
  2053. * timer Clock Source
  2054. */
  2055. rx_itr = FEC_ITR_CLK_SEL;
  2056. tx_itr = FEC_ITR_CLK_SEL;
  2057. /* set ICFT and ICTT */
  2058. rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
  2059. rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
  2060. tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
  2061. tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
  2062. rx_itr |= FEC_ITR_EN;
  2063. tx_itr |= FEC_ITR_EN;
  2064. writel(tx_itr, fep->hwp + FEC_TXIC0);
  2065. writel(rx_itr, fep->hwp + FEC_RXIC0);
  2066. if (fep->quirks & FEC_QUIRK_HAS_AVB) {
  2067. writel(tx_itr, fep->hwp + FEC_TXIC1);
  2068. writel(rx_itr, fep->hwp + FEC_RXIC1);
  2069. writel(tx_itr, fep->hwp + FEC_TXIC2);
  2070. writel(rx_itr, fep->hwp + FEC_RXIC2);
  2071. }
  2072. }
  2073. static int
  2074. fec_enet_get_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
  2075. {
  2076. struct fec_enet_private *fep = netdev_priv(ndev);
  2077. if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
  2078. return -EOPNOTSUPP;
  2079. ec->rx_coalesce_usecs = fep->rx_time_itr;
  2080. ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
  2081. ec->tx_coalesce_usecs = fep->tx_time_itr;
  2082. ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
  2083. return 0;
  2084. }
  2085. static int
  2086. fec_enet_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *ec)
  2087. {
  2088. struct fec_enet_private *fep = netdev_priv(ndev);
  2089. unsigned int cycle;
  2090. if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
  2091. return -EOPNOTSUPP;
  2092. if (ec->rx_max_coalesced_frames > 255) {
  2093. pr_err("Rx coalesced frames exceed hardware limitation\n");
  2094. return -EINVAL;
  2095. }
  2096. if (ec->tx_max_coalesced_frames > 255) {
  2097. pr_err("Tx coalesced frame exceed hardware limitation\n");
  2098. return -EINVAL;
  2099. }
  2100. cycle = fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr);
  2101. if (cycle > 0xFFFF) {
  2102. pr_err("Rx coalesced usec exceed hardware limitation\n");
  2103. return -EINVAL;
  2104. }
  2105. cycle = fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr);
  2106. if (cycle > 0xFFFF) {
  2107. pr_err("Rx coalesced usec exceed hardware limitation\n");
  2108. return -EINVAL;
  2109. }
  2110. fep->rx_time_itr = ec->rx_coalesce_usecs;
  2111. fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
  2112. fep->tx_time_itr = ec->tx_coalesce_usecs;
  2113. fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
  2114. fec_enet_itr_coal_set(ndev);
  2115. return 0;
  2116. }
  2117. static void fec_enet_itr_coal_init(struct net_device *ndev)
  2118. {
  2119. struct ethtool_coalesce ec;
  2120. ec.rx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
  2121. ec.rx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
  2122. ec.tx_coalesce_usecs = FEC_ITR_ICTT_DEFAULT;
  2123. ec.tx_max_coalesced_frames = FEC_ITR_ICFT_DEFAULT;
  2124. fec_enet_set_coalesce(ndev, &ec);
  2125. }
  2126. static int fec_enet_get_tunable(struct net_device *netdev,
  2127. const struct ethtool_tunable *tuna,
  2128. void *data)
  2129. {
  2130. struct fec_enet_private *fep = netdev_priv(netdev);
  2131. int ret = 0;
  2132. switch (tuna->id) {
  2133. case ETHTOOL_RX_COPYBREAK:
  2134. *(u32 *)data = fep->rx_copybreak;
  2135. break;
  2136. default:
  2137. ret = -EINVAL;
  2138. break;
  2139. }
  2140. return ret;
  2141. }
  2142. static int fec_enet_set_tunable(struct net_device *netdev,
  2143. const struct ethtool_tunable *tuna,
  2144. const void *data)
  2145. {
  2146. struct fec_enet_private *fep = netdev_priv(netdev);
  2147. int ret = 0;
  2148. switch (tuna->id) {
  2149. case ETHTOOL_RX_COPYBREAK:
  2150. fep->rx_copybreak = *(u32 *)data;
  2151. break;
  2152. default:
  2153. ret = -EINVAL;
  2154. break;
  2155. }
  2156. return ret;
  2157. }
  2158. static void
  2159. fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  2160. {
  2161. struct fec_enet_private *fep = netdev_priv(ndev);
  2162. if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) {
  2163. wol->supported = WAKE_MAGIC;
  2164. wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0;
  2165. } else {
  2166. wol->supported = wol->wolopts = 0;
  2167. }
  2168. }
  2169. static int
  2170. fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  2171. {
  2172. struct fec_enet_private *fep = netdev_priv(ndev);
  2173. if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET))
  2174. return -EINVAL;
  2175. if (wol->wolopts & ~WAKE_MAGIC)
  2176. return -EINVAL;
  2177. device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC);
  2178. if (device_may_wakeup(&ndev->dev)) {
  2179. fep->wol_flag |= FEC_WOL_FLAG_ENABLE;
  2180. if (fep->irq[0] > 0)
  2181. enable_irq_wake(fep->irq[0]);
  2182. } else {
  2183. fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE);
  2184. if (fep->irq[0] > 0)
  2185. disable_irq_wake(fep->irq[0]);
  2186. }
  2187. return 0;
  2188. }
  2189. static const struct ethtool_ops fec_enet_ethtool_ops = {
  2190. .get_drvinfo = fec_enet_get_drvinfo,
  2191. .get_regs_len = fec_enet_get_regs_len,
  2192. .get_regs = fec_enet_get_regs,
  2193. .nway_reset = phy_ethtool_nway_reset,
  2194. .get_link = ethtool_op_get_link,
  2195. .get_coalesce = fec_enet_get_coalesce,
  2196. .set_coalesce = fec_enet_set_coalesce,
  2197. #ifndef CONFIG_M5272
  2198. .get_pauseparam = fec_enet_get_pauseparam,
  2199. .set_pauseparam = fec_enet_set_pauseparam,
  2200. .get_strings = fec_enet_get_strings,
  2201. .get_ethtool_stats = fec_enet_get_ethtool_stats,
  2202. .get_sset_count = fec_enet_get_sset_count,
  2203. #endif
  2204. .get_ts_info = fec_enet_get_ts_info,
  2205. .get_tunable = fec_enet_get_tunable,
  2206. .set_tunable = fec_enet_set_tunable,
  2207. .get_wol = fec_enet_get_wol,
  2208. .set_wol = fec_enet_set_wol,
  2209. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  2210. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  2211. };
  2212. static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
  2213. {
  2214. struct fec_enet_private *fep = netdev_priv(ndev);
  2215. struct phy_device *phydev = ndev->phydev;
  2216. if (!netif_running(ndev))
  2217. return -EINVAL;
  2218. if (!phydev)
  2219. return -ENODEV;
  2220. if (fep->bufdesc_ex) {
  2221. if (cmd == SIOCSHWTSTAMP)
  2222. return fec_ptp_set(ndev, rq);
  2223. if (cmd == SIOCGHWTSTAMP)
  2224. return fec_ptp_get(ndev, rq);
  2225. }
  2226. return phy_mii_ioctl(phydev, rq, cmd);
  2227. }
  2228. static void fec_enet_free_buffers(struct net_device *ndev)
  2229. {
  2230. struct fec_enet_private *fep = netdev_priv(ndev);
  2231. unsigned int i;
  2232. struct sk_buff *skb;
  2233. struct bufdesc *bdp;
  2234. struct fec_enet_priv_tx_q *txq;
  2235. struct fec_enet_priv_rx_q *rxq;
  2236. unsigned int q;
  2237. for (q = 0; q < fep->num_rx_queues; q++) {
  2238. rxq = fep->rx_queue[q];
  2239. bdp = rxq->bd.base;
  2240. for (i = 0; i < rxq->bd.ring_size; i++) {
  2241. skb = rxq->rx_skbuff[i];
  2242. rxq->rx_skbuff[i] = NULL;
  2243. if (skb) {
  2244. dma_unmap_single(&fep->pdev->dev,
  2245. fec32_to_cpu(bdp->cbd_bufaddr),
  2246. FEC_ENET_RX_FRSIZE - fep->rx_align,
  2247. DMA_FROM_DEVICE);
  2248. dev_kfree_skb(skb);
  2249. }
  2250. bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
  2251. }
  2252. }
  2253. for (q = 0; q < fep->num_tx_queues; q++) {
  2254. txq = fep->tx_queue[q];
  2255. bdp = txq->bd.base;
  2256. for (i = 0; i < txq->bd.ring_size; i++) {
  2257. kfree(txq->tx_bounce[i]);
  2258. txq->tx_bounce[i] = NULL;
  2259. skb = txq->tx_skbuff[i];
  2260. txq->tx_skbuff[i] = NULL;
  2261. dev_kfree_skb(skb);
  2262. }
  2263. }
  2264. }
  2265. static void fec_enet_free_queue(struct net_device *ndev)
  2266. {
  2267. struct fec_enet_private *fep = netdev_priv(ndev);
  2268. int i;
  2269. struct fec_enet_priv_tx_q *txq;
  2270. for (i = 0; i < fep->num_tx_queues; i++)
  2271. if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
  2272. txq = fep->tx_queue[i];
  2273. dma_free_coherent(&fep->pdev->dev,
  2274. txq->bd.ring_size * TSO_HEADER_SIZE,
  2275. txq->tso_hdrs,
  2276. txq->tso_hdrs_dma);
  2277. }
  2278. for (i = 0; i < fep->num_rx_queues; i++)
  2279. kfree(fep->rx_queue[i]);
  2280. for (i = 0; i < fep->num_tx_queues; i++)
  2281. kfree(fep->tx_queue[i]);
  2282. }
  2283. static int fec_enet_alloc_queue(struct net_device *ndev)
  2284. {
  2285. struct fec_enet_private *fep = netdev_priv(ndev);
  2286. int i;
  2287. int ret = 0;
  2288. struct fec_enet_priv_tx_q *txq;
  2289. for (i = 0; i < fep->num_tx_queues; i++) {
  2290. txq = kzalloc(sizeof(*txq), GFP_KERNEL);
  2291. if (!txq) {
  2292. ret = -ENOMEM;
  2293. goto alloc_failed;
  2294. }
  2295. fep->tx_queue[i] = txq;
  2296. txq->bd.ring_size = TX_RING_SIZE;
  2297. fep->total_tx_ring_size += fep->tx_queue[i]->bd.ring_size;
  2298. txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
  2299. txq->tx_wake_threshold =
  2300. (txq->bd.ring_size - txq->tx_stop_threshold) / 2;
  2301. txq->tso_hdrs = dma_alloc_coherent(&fep->pdev->dev,
  2302. txq->bd.ring_size * TSO_HEADER_SIZE,
  2303. &txq->tso_hdrs_dma,
  2304. GFP_KERNEL);
  2305. if (!txq->tso_hdrs) {
  2306. ret = -ENOMEM;
  2307. goto alloc_failed;
  2308. }
  2309. }
  2310. for (i = 0; i < fep->num_rx_queues; i++) {
  2311. fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
  2312. GFP_KERNEL);
  2313. if (!fep->rx_queue[i]) {
  2314. ret = -ENOMEM;
  2315. goto alloc_failed;
  2316. }
  2317. fep->rx_queue[i]->bd.ring_size = RX_RING_SIZE;
  2318. fep->total_rx_ring_size += fep->rx_queue[i]->bd.ring_size;
  2319. }
  2320. return ret;
  2321. alloc_failed:
  2322. fec_enet_free_queue(ndev);
  2323. return ret;
  2324. }
  2325. static int
  2326. fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
  2327. {
  2328. struct fec_enet_private *fep = netdev_priv(ndev);
  2329. unsigned int i;
  2330. struct sk_buff *skb;
  2331. struct bufdesc *bdp;
  2332. struct fec_enet_priv_rx_q *rxq;
  2333. rxq = fep->rx_queue[queue];
  2334. bdp = rxq->bd.base;
  2335. for (i = 0; i < rxq->bd.ring_size; i++) {
  2336. skb = netdev_alloc_skb(ndev, FEC_ENET_RX_FRSIZE);
  2337. if (!skb)
  2338. goto err_alloc;
  2339. if (fec_enet_new_rxbdp(ndev, bdp, skb)) {
  2340. dev_kfree_skb(skb);
  2341. goto err_alloc;
  2342. }
  2343. rxq->rx_skbuff[i] = skb;
  2344. bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
  2345. if (fep->bufdesc_ex) {
  2346. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  2347. ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
  2348. }
  2349. bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
  2350. }
  2351. /* Set the last buffer to wrap. */
  2352. bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
  2353. bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
  2354. return 0;
  2355. err_alloc:
  2356. fec_enet_free_buffers(ndev);
  2357. return -ENOMEM;
  2358. }
  2359. static int
  2360. fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
  2361. {
  2362. struct fec_enet_private *fep = netdev_priv(ndev);
  2363. unsigned int i;
  2364. struct bufdesc *bdp;
  2365. struct fec_enet_priv_tx_q *txq;
  2366. txq = fep->tx_queue[queue];
  2367. bdp = txq->bd.base;
  2368. for (i = 0; i < txq->bd.ring_size; i++) {
  2369. txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
  2370. if (!txq->tx_bounce[i])
  2371. goto err_alloc;
  2372. bdp->cbd_sc = cpu_to_fec16(0);
  2373. bdp->cbd_bufaddr = cpu_to_fec32(0);
  2374. if (fep->bufdesc_ex) {
  2375. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  2376. ebdp->cbd_esc = cpu_to_fec32(BD_ENET_TX_INT);
  2377. }
  2378. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  2379. }
  2380. /* Set the last buffer to wrap. */
  2381. bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
  2382. bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
  2383. return 0;
  2384. err_alloc:
  2385. fec_enet_free_buffers(ndev);
  2386. return -ENOMEM;
  2387. }
  2388. static int fec_enet_alloc_buffers(struct net_device *ndev)
  2389. {
  2390. struct fec_enet_private *fep = netdev_priv(ndev);
  2391. unsigned int i;
  2392. for (i = 0; i < fep->num_rx_queues; i++)
  2393. if (fec_enet_alloc_rxq_buffers(ndev, i))
  2394. return -ENOMEM;
  2395. for (i = 0; i < fep->num_tx_queues; i++)
  2396. if (fec_enet_alloc_txq_buffers(ndev, i))
  2397. return -ENOMEM;
  2398. return 0;
  2399. }
  2400. static int
  2401. fec_enet_open(struct net_device *ndev)
  2402. {
  2403. struct fec_enet_private *fep = netdev_priv(ndev);
  2404. int ret;
  2405. bool reset_again;
  2406. ret = pm_runtime_get_sync(&fep->pdev->dev);
  2407. if (ret < 0)
  2408. return ret;
  2409. pinctrl_pm_select_default_state(&fep->pdev->dev);
  2410. ret = fec_enet_clk_enable(ndev, true);
  2411. if (ret)
  2412. goto clk_enable;
  2413. /* During the first fec_enet_open call the PHY isn't probed at this
  2414. * point. Therefore the phy_reset_after_clk_enable() call within
  2415. * fec_enet_clk_enable() fails. As we need this reset in order to be
  2416. * sure the PHY is working correctly we check if we need to reset again
  2417. * later when the PHY is probed
  2418. */
  2419. if (ndev->phydev && ndev->phydev->drv)
  2420. reset_again = false;
  2421. else
  2422. reset_again = true;
  2423. /* I should reset the ring buffers here, but I don't yet know
  2424. * a simple way to do that.
  2425. */
  2426. ret = fec_enet_alloc_buffers(ndev);
  2427. if (ret)
  2428. goto err_enet_alloc;
  2429. /* Init MAC prior to mii bus probe */
  2430. fec_restart(ndev);
  2431. /* Probe and connect to PHY when open the interface */
  2432. ret = fec_enet_mii_probe(ndev);
  2433. if (ret)
  2434. goto err_enet_mii_probe;
  2435. /* Call phy_reset_after_clk_enable() again if it failed during
  2436. * phy_reset_after_clk_enable() before because the PHY wasn't probed.
  2437. */
  2438. if (reset_again)
  2439. phy_reset_after_clk_enable(ndev->phydev);
  2440. if (fep->quirks & FEC_QUIRK_ERR006687)
  2441. imx6q_cpuidle_fec_irqs_used();
  2442. napi_enable(&fep->napi);
  2443. phy_start(ndev->phydev);
  2444. netif_tx_start_all_queues(ndev);
  2445. device_set_wakeup_enable(&ndev->dev, fep->wol_flag &
  2446. FEC_WOL_FLAG_ENABLE);
  2447. return 0;
  2448. err_enet_mii_probe:
  2449. fec_enet_free_buffers(ndev);
  2450. err_enet_alloc:
  2451. fec_enet_clk_enable(ndev, false);
  2452. clk_enable:
  2453. pm_runtime_mark_last_busy(&fep->pdev->dev);
  2454. pm_runtime_put_autosuspend(&fep->pdev->dev);
  2455. pinctrl_pm_select_sleep_state(&fep->pdev->dev);
  2456. return ret;
  2457. }
  2458. static int
  2459. fec_enet_close(struct net_device *ndev)
  2460. {
  2461. struct fec_enet_private *fep = netdev_priv(ndev);
  2462. phy_stop(ndev->phydev);
  2463. if (netif_device_present(ndev)) {
  2464. napi_disable(&fep->napi);
  2465. netif_tx_disable(ndev);
  2466. fec_stop(ndev);
  2467. }
  2468. phy_disconnect(ndev->phydev);
  2469. if (fep->quirks & FEC_QUIRK_ERR006687)
  2470. imx6q_cpuidle_fec_irqs_unused();
  2471. fec_enet_update_ethtool_stats(ndev);
  2472. fec_enet_clk_enable(ndev, false);
  2473. pinctrl_pm_select_sleep_state(&fep->pdev->dev);
  2474. pm_runtime_mark_last_busy(&fep->pdev->dev);
  2475. pm_runtime_put_autosuspend(&fep->pdev->dev);
  2476. fec_enet_free_buffers(ndev);
  2477. return 0;
  2478. }
  2479. /* Set or clear the multicast filter for this adaptor.
  2480. * Skeleton taken from sunlance driver.
  2481. * The CPM Ethernet implementation allows Multicast as well as individual
  2482. * MAC address filtering. Some of the drivers check to make sure it is
  2483. * a group multicast address, and discard those that are not. I guess I
  2484. * will do the same for now, but just remove the test if you want
  2485. * individual filtering as well (do the upper net layers want or support
  2486. * this kind of feature?).
  2487. */
  2488. #define FEC_HASH_BITS 6 /* #bits in hash */
  2489. #define CRC32_POLY 0xEDB88320
  2490. static void set_multicast_list(struct net_device *ndev)
  2491. {
  2492. struct fec_enet_private *fep = netdev_priv(ndev);
  2493. struct netdev_hw_addr *ha;
  2494. unsigned int i, bit, data, crc, tmp;
  2495. unsigned char hash;
  2496. unsigned int hash_high = 0, hash_low = 0;
  2497. if (ndev->flags & IFF_PROMISC) {
  2498. tmp = readl(fep->hwp + FEC_R_CNTRL);
  2499. tmp |= 0x8;
  2500. writel(tmp, fep->hwp + FEC_R_CNTRL);
  2501. return;
  2502. }
  2503. tmp = readl(fep->hwp + FEC_R_CNTRL);
  2504. tmp &= ~0x8;
  2505. writel(tmp, fep->hwp + FEC_R_CNTRL);
  2506. if (ndev->flags & IFF_ALLMULTI) {
  2507. /* Catch all multicast addresses, so set the
  2508. * filter to all 1's
  2509. */
  2510. writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  2511. writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  2512. return;
  2513. }
  2514. /* Add the addresses in hash register */
  2515. netdev_for_each_mc_addr(ha, ndev) {
  2516. /* calculate crc32 value of mac address */
  2517. crc = 0xffffffff;
  2518. for (i = 0; i < ndev->addr_len; i++) {
  2519. data = ha->addr[i];
  2520. for (bit = 0; bit < 8; bit++, data >>= 1) {
  2521. crc = (crc >> 1) ^
  2522. (((crc ^ data) & 1) ? CRC32_POLY : 0);
  2523. }
  2524. }
  2525. /* only upper 6 bits (FEC_HASH_BITS) are used
  2526. * which point to specific bit in the hash registers
  2527. */
  2528. hash = (crc >> (32 - FEC_HASH_BITS)) & 0x3f;
  2529. if (hash > 31)
  2530. hash_high |= 1 << (hash - 32);
  2531. else
  2532. hash_low |= 1 << hash;
  2533. }
  2534. writel(hash_high, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  2535. writel(hash_low, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  2536. }
  2537. /* Set a MAC change in hardware. */
  2538. static int
  2539. fec_set_mac_address(struct net_device *ndev, void *p)
  2540. {
  2541. struct fec_enet_private *fep = netdev_priv(ndev);
  2542. struct sockaddr *addr = p;
  2543. if (addr) {
  2544. if (!is_valid_ether_addr(addr->sa_data))
  2545. return -EADDRNOTAVAIL;
  2546. memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
  2547. }
  2548. /* Add netif status check here to avoid system hang in below case:
  2549. * ifconfig ethx down; ifconfig ethx hw ether xx:xx:xx:xx:xx:xx;
  2550. * After ethx down, fec all clocks are gated off and then register
  2551. * access causes system hang.
  2552. */
  2553. if (!netif_running(ndev))
  2554. return 0;
  2555. writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
  2556. (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
  2557. fep->hwp + FEC_ADDR_LOW);
  2558. writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
  2559. fep->hwp + FEC_ADDR_HIGH);
  2560. return 0;
  2561. }
  2562. #ifdef CONFIG_NET_POLL_CONTROLLER
  2563. /**
  2564. * fec_poll_controller - FEC Poll controller function
  2565. * @dev: The FEC network adapter
  2566. *
  2567. * Polled functionality used by netconsole and others in non interrupt mode
  2568. *
  2569. */
  2570. static void fec_poll_controller(struct net_device *dev)
  2571. {
  2572. int i;
  2573. struct fec_enet_private *fep = netdev_priv(dev);
  2574. for (i = 0; i < FEC_IRQ_NUM; i++) {
  2575. if (fep->irq[i] > 0) {
  2576. disable_irq(fep->irq[i]);
  2577. fec_enet_interrupt(fep->irq[i], dev);
  2578. enable_irq(fep->irq[i]);
  2579. }
  2580. }
  2581. }
  2582. #endif
  2583. static inline void fec_enet_set_netdev_features(struct net_device *netdev,
  2584. netdev_features_t features)
  2585. {
  2586. struct fec_enet_private *fep = netdev_priv(netdev);
  2587. netdev_features_t changed = features ^ netdev->features;
  2588. netdev->features = features;
  2589. /* Receive checksum has been changed */
  2590. if (changed & NETIF_F_RXCSUM) {
  2591. if (features & NETIF_F_RXCSUM)
  2592. fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
  2593. else
  2594. fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
  2595. }
  2596. }
  2597. static int fec_set_features(struct net_device *netdev,
  2598. netdev_features_t features)
  2599. {
  2600. struct fec_enet_private *fep = netdev_priv(netdev);
  2601. netdev_features_t changed = features ^ netdev->features;
  2602. if (netif_running(netdev) && changed & NETIF_F_RXCSUM) {
  2603. napi_disable(&fep->napi);
  2604. netif_tx_lock_bh(netdev);
  2605. fec_stop(netdev);
  2606. fec_enet_set_netdev_features(netdev, features);
  2607. fec_restart(netdev);
  2608. netif_tx_wake_all_queues(netdev);
  2609. netif_tx_unlock_bh(netdev);
  2610. napi_enable(&fep->napi);
  2611. } else {
  2612. fec_enet_set_netdev_features(netdev, features);
  2613. }
  2614. return 0;
  2615. }
  2616. static const struct net_device_ops fec_netdev_ops = {
  2617. .ndo_open = fec_enet_open,
  2618. .ndo_stop = fec_enet_close,
  2619. .ndo_start_xmit = fec_enet_start_xmit,
  2620. .ndo_set_rx_mode = set_multicast_list,
  2621. .ndo_validate_addr = eth_validate_addr,
  2622. .ndo_tx_timeout = fec_timeout,
  2623. .ndo_set_mac_address = fec_set_mac_address,
  2624. .ndo_do_ioctl = fec_enet_ioctl,
  2625. #ifdef CONFIG_NET_POLL_CONTROLLER
  2626. .ndo_poll_controller = fec_poll_controller,
  2627. #endif
  2628. .ndo_set_features = fec_set_features,
  2629. };
  2630. static const unsigned short offset_des_active_rxq[] = {
  2631. FEC_R_DES_ACTIVE_0, FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2
  2632. };
  2633. static const unsigned short offset_des_active_txq[] = {
  2634. FEC_X_DES_ACTIVE_0, FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2
  2635. };
  2636. /*
  2637. * XXX: We need to clean up on failure exits here.
  2638. *
  2639. */
  2640. static int fec_enet_init(struct net_device *ndev)
  2641. {
  2642. struct fec_enet_private *fep = netdev_priv(ndev);
  2643. struct bufdesc *cbd_base;
  2644. dma_addr_t bd_dma;
  2645. int bd_size;
  2646. unsigned int i;
  2647. unsigned dsize = fep->bufdesc_ex ? sizeof(struct bufdesc_ex) :
  2648. sizeof(struct bufdesc);
  2649. unsigned dsize_log2 = __fls(dsize);
  2650. WARN_ON(dsize != (1 << dsize_log2));
  2651. #if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
  2652. fep->rx_align = 0xf;
  2653. fep->tx_align = 0xf;
  2654. #else
  2655. fep->rx_align = 0x3;
  2656. fep->tx_align = 0x3;
  2657. #endif
  2658. fec_enet_alloc_queue(ndev);
  2659. bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) * dsize;
  2660. /* Allocate memory for buffer descriptors. */
  2661. cbd_base = dmam_alloc_coherent(&fep->pdev->dev, bd_size, &bd_dma,
  2662. GFP_KERNEL);
  2663. if (!cbd_base) {
  2664. return -ENOMEM;
  2665. }
  2666. memset(cbd_base, 0, bd_size);
  2667. /* Get the Ethernet address */
  2668. fec_get_mac(ndev);
  2669. /* make sure MAC we just acquired is programmed into the hw */
  2670. fec_set_mac_address(ndev, NULL);
  2671. /* Set receive and transmit descriptor base. */
  2672. for (i = 0; i < fep->num_rx_queues; i++) {
  2673. struct fec_enet_priv_rx_q *rxq = fep->rx_queue[i];
  2674. unsigned size = dsize * rxq->bd.ring_size;
  2675. rxq->bd.qid = i;
  2676. rxq->bd.base = cbd_base;
  2677. rxq->bd.cur = cbd_base;
  2678. rxq->bd.dma = bd_dma;
  2679. rxq->bd.dsize = dsize;
  2680. rxq->bd.dsize_log2 = dsize_log2;
  2681. rxq->bd.reg_desc_active = fep->hwp + offset_des_active_rxq[i];
  2682. bd_dma += size;
  2683. cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
  2684. rxq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
  2685. }
  2686. for (i = 0; i < fep->num_tx_queues; i++) {
  2687. struct fec_enet_priv_tx_q *txq = fep->tx_queue[i];
  2688. unsigned size = dsize * txq->bd.ring_size;
  2689. txq->bd.qid = i;
  2690. txq->bd.base = cbd_base;
  2691. txq->bd.cur = cbd_base;
  2692. txq->bd.dma = bd_dma;
  2693. txq->bd.dsize = dsize;
  2694. txq->bd.dsize_log2 = dsize_log2;
  2695. txq->bd.reg_desc_active = fep->hwp + offset_des_active_txq[i];
  2696. bd_dma += size;
  2697. cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
  2698. txq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
  2699. }
  2700. /* The FEC Ethernet specific entries in the device structure */
  2701. ndev->watchdog_timeo = TX_TIMEOUT;
  2702. ndev->netdev_ops = &fec_netdev_ops;
  2703. ndev->ethtool_ops = &fec_enet_ethtool_ops;
  2704. writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
  2705. netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi, NAPI_POLL_WEIGHT);
  2706. if (fep->quirks & FEC_QUIRK_HAS_VLAN)
  2707. /* enable hw VLAN support */
  2708. ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
  2709. if (fep->quirks & FEC_QUIRK_HAS_CSUM) {
  2710. ndev->gso_max_segs = FEC_MAX_TSO_SEGS;
  2711. /* enable hw accelerator */
  2712. ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
  2713. | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
  2714. fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
  2715. }
  2716. if (fep->quirks & FEC_QUIRK_HAS_AVB) {
  2717. fep->tx_align = 0;
  2718. fep->rx_align = 0x3f;
  2719. }
  2720. ndev->hw_features = ndev->features;
  2721. fec_restart(ndev);
  2722. if (fep->quirks & FEC_QUIRK_MIB_CLEAR)
  2723. fec_enet_clear_ethtool_stats(ndev);
  2724. else
  2725. fec_enet_update_ethtool_stats(ndev);
  2726. return 0;
  2727. }
  2728. #ifdef CONFIG_OF
  2729. static int fec_reset_phy(struct platform_device *pdev)
  2730. {
  2731. int err, phy_reset;
  2732. bool active_high = false;
  2733. int msec = 1, phy_post_delay = 0;
  2734. struct device_node *np = pdev->dev.of_node;
  2735. if (!np)
  2736. return 0;
  2737. err = of_property_read_u32(np, "phy-reset-duration", &msec);
  2738. /* A sane reset duration should not be longer than 1s */
  2739. if (!err && msec > 1000)
  2740. msec = 1;
  2741. phy_reset = of_get_named_gpio(np, "phy-reset-gpios", 0);
  2742. if (phy_reset == -EPROBE_DEFER)
  2743. return phy_reset;
  2744. else if (!gpio_is_valid(phy_reset))
  2745. return 0;
  2746. err = of_property_read_u32(np, "phy-reset-post-delay", &phy_post_delay);
  2747. /* valid reset duration should be less than 1s */
  2748. if (!err && phy_post_delay > 1000)
  2749. return -EINVAL;
  2750. active_high = of_property_read_bool(np, "phy-reset-active-high");
  2751. err = devm_gpio_request_one(&pdev->dev, phy_reset,
  2752. active_high ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
  2753. "phy-reset");
  2754. if (err) {
  2755. dev_err(&pdev->dev, "failed to get phy-reset-gpios: %d\n", err);
  2756. return err;
  2757. }
  2758. if (msec > 20)
  2759. msleep(msec);
  2760. else
  2761. usleep_range(msec * 1000, msec * 1000 + 1000);
  2762. gpio_set_value_cansleep(phy_reset, !active_high);
  2763. if (!phy_post_delay)
  2764. return 0;
  2765. if (phy_post_delay > 20)
  2766. msleep(phy_post_delay);
  2767. else
  2768. usleep_range(phy_post_delay * 1000,
  2769. phy_post_delay * 1000 + 1000);
  2770. return 0;
  2771. }
  2772. #else /* CONFIG_OF */
  2773. static int fec_reset_phy(struct platform_device *pdev)
  2774. {
  2775. /*
  2776. * In case of platform probe, the reset has been done
  2777. * by machine code.
  2778. */
  2779. return 0;
  2780. }
  2781. #endif /* CONFIG_OF */
  2782. static void
  2783. fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
  2784. {
  2785. struct device_node *np = pdev->dev.of_node;
  2786. *num_tx = *num_rx = 1;
  2787. if (!np || !of_device_is_available(np))
  2788. return;
  2789. /* parse the num of tx and rx queues */
  2790. of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
  2791. of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
  2792. if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
  2793. dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
  2794. *num_tx);
  2795. *num_tx = 1;
  2796. return;
  2797. }
  2798. if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
  2799. dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
  2800. *num_rx);
  2801. *num_rx = 1;
  2802. return;
  2803. }
  2804. }
  2805. static int fec_enet_get_irq_cnt(struct platform_device *pdev)
  2806. {
  2807. int irq_cnt = platform_irq_count(pdev);
  2808. if (irq_cnt > FEC_IRQ_NUM)
  2809. irq_cnt = FEC_IRQ_NUM; /* last for pps */
  2810. else if (irq_cnt == 2)
  2811. irq_cnt = 1; /* last for pps */
  2812. else if (irq_cnt <= 0)
  2813. irq_cnt = 1; /* At least 1 irq is needed */
  2814. return irq_cnt;
  2815. }
  2816. static int
  2817. fec_probe(struct platform_device *pdev)
  2818. {
  2819. struct fec_enet_private *fep;
  2820. struct fec_platform_data *pdata;
  2821. struct net_device *ndev;
  2822. int i, irq, ret = 0;
  2823. struct resource *r;
  2824. const struct of_device_id *of_id;
  2825. static int dev_id;
  2826. struct device_node *np = pdev->dev.of_node, *phy_node;
  2827. int num_tx_qs;
  2828. int num_rx_qs;
  2829. char irq_name[8];
  2830. int irq_cnt;
  2831. fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
  2832. /* Init network device */
  2833. ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private) +
  2834. FEC_STATS_SIZE, num_tx_qs, num_rx_qs);
  2835. if (!ndev)
  2836. return -ENOMEM;
  2837. SET_NETDEV_DEV(ndev, &pdev->dev);
  2838. /* setup board info structure */
  2839. fep = netdev_priv(ndev);
  2840. of_id = of_match_device(fec_dt_ids, &pdev->dev);
  2841. if (of_id)
  2842. pdev->id_entry = of_id->data;
  2843. fep->quirks = pdev->id_entry->driver_data;
  2844. fep->netdev = ndev;
  2845. fep->num_rx_queues = num_rx_qs;
  2846. fep->num_tx_queues = num_tx_qs;
  2847. #if !defined(CONFIG_M5272)
  2848. /* default enable pause frame auto negotiation */
  2849. if (fep->quirks & FEC_QUIRK_HAS_GBIT)
  2850. fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
  2851. #endif
  2852. /* Select default pin state */
  2853. pinctrl_pm_select_default_state(&pdev->dev);
  2854. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2855. fep->hwp = devm_ioremap_resource(&pdev->dev, r);
  2856. if (IS_ERR(fep->hwp)) {
  2857. ret = PTR_ERR(fep->hwp);
  2858. goto failed_ioremap;
  2859. }
  2860. fep->pdev = pdev;
  2861. fep->dev_id = dev_id++;
  2862. platform_set_drvdata(pdev, ndev);
  2863. if ((of_machine_is_compatible("fsl,imx6q") ||
  2864. of_machine_is_compatible("fsl,imx6dl")) &&
  2865. !of_property_read_bool(np, "fsl,err006687-workaround-present"))
  2866. fep->quirks |= FEC_QUIRK_ERR006687;
  2867. if (of_get_property(np, "fsl,magic-packet", NULL))
  2868. fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET;
  2869. phy_node = of_parse_phandle(np, "phy-handle", 0);
  2870. if (!phy_node && of_phy_is_fixed_link(np)) {
  2871. ret = of_phy_register_fixed_link(np);
  2872. if (ret < 0) {
  2873. dev_err(&pdev->dev,
  2874. "broken fixed-link specification\n");
  2875. goto failed_phy;
  2876. }
  2877. phy_node = of_node_get(np);
  2878. }
  2879. fep->phy_node = phy_node;
  2880. ret = of_get_phy_mode(pdev->dev.of_node);
  2881. if (ret < 0) {
  2882. pdata = dev_get_platdata(&pdev->dev);
  2883. if (pdata)
  2884. fep->phy_interface = pdata->phy;
  2885. else
  2886. fep->phy_interface = PHY_INTERFACE_MODE_MII;
  2887. } else {
  2888. fep->phy_interface = ret;
  2889. }
  2890. fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  2891. if (IS_ERR(fep->clk_ipg)) {
  2892. ret = PTR_ERR(fep->clk_ipg);
  2893. goto failed_clk;
  2894. }
  2895. fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  2896. if (IS_ERR(fep->clk_ahb)) {
  2897. ret = PTR_ERR(fep->clk_ahb);
  2898. goto failed_clk;
  2899. }
  2900. fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
  2901. /* enet_out is optional, depends on board */
  2902. fep->clk_enet_out = devm_clk_get(&pdev->dev, "enet_out");
  2903. if (IS_ERR(fep->clk_enet_out))
  2904. fep->clk_enet_out = NULL;
  2905. fep->ptp_clk_on = false;
  2906. mutex_init(&fep->ptp_clk_mutex);
  2907. /* clk_ref is optional, depends on board */
  2908. fep->clk_ref = devm_clk_get(&pdev->dev, "enet_clk_ref");
  2909. if (IS_ERR(fep->clk_ref))
  2910. fep->clk_ref = NULL;
  2911. fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX;
  2912. fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
  2913. if (IS_ERR(fep->clk_ptp)) {
  2914. fep->clk_ptp = NULL;
  2915. fep->bufdesc_ex = false;
  2916. }
  2917. ret = fec_enet_clk_enable(ndev, true);
  2918. if (ret)
  2919. goto failed_clk;
  2920. ret = clk_prepare_enable(fep->clk_ipg);
  2921. if (ret)
  2922. goto failed_clk_ipg;
  2923. fep->reg_phy = devm_regulator_get(&pdev->dev, "phy");
  2924. if (!IS_ERR(fep->reg_phy)) {
  2925. ret = regulator_enable(fep->reg_phy);
  2926. if (ret) {
  2927. dev_err(&pdev->dev,
  2928. "Failed to enable phy regulator: %d\n", ret);
  2929. clk_disable_unprepare(fep->clk_ipg);
  2930. goto failed_regulator;
  2931. }
  2932. } else {
  2933. if (PTR_ERR(fep->reg_phy) == -EPROBE_DEFER) {
  2934. ret = -EPROBE_DEFER;
  2935. goto failed_regulator;
  2936. }
  2937. fep->reg_phy = NULL;
  2938. }
  2939. pm_runtime_set_autosuspend_delay(&pdev->dev, FEC_MDIO_PM_TIMEOUT);
  2940. pm_runtime_use_autosuspend(&pdev->dev);
  2941. pm_runtime_get_noresume(&pdev->dev);
  2942. pm_runtime_set_active(&pdev->dev);
  2943. pm_runtime_enable(&pdev->dev);
  2944. ret = fec_reset_phy(pdev);
  2945. if (ret)
  2946. goto failed_reset;
  2947. irq_cnt = fec_enet_get_irq_cnt(pdev);
  2948. if (fep->bufdesc_ex)
  2949. fec_ptp_init(pdev, irq_cnt);
  2950. ret = fec_enet_init(ndev);
  2951. if (ret)
  2952. goto failed_init;
  2953. for (i = 0; i < irq_cnt; i++) {
  2954. sprintf(irq_name, "int%d", i);
  2955. irq = platform_get_irq_byname(pdev, irq_name);
  2956. if (irq < 0)
  2957. irq = platform_get_irq(pdev, i);
  2958. if (irq < 0) {
  2959. ret = irq;
  2960. goto failed_irq;
  2961. }
  2962. ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
  2963. 0, pdev->name, ndev);
  2964. if (ret)
  2965. goto failed_irq;
  2966. fep->irq[i] = irq;
  2967. }
  2968. init_completion(&fep->mdio_done);
  2969. ret = fec_enet_mii_init(pdev);
  2970. if (ret)
  2971. goto failed_mii_init;
  2972. /* Carrier starts down, phylib will bring it up */
  2973. netif_carrier_off(ndev);
  2974. fec_enet_clk_enable(ndev, false);
  2975. pinctrl_pm_select_sleep_state(&pdev->dev);
  2976. ret = register_netdev(ndev);
  2977. if (ret)
  2978. goto failed_register;
  2979. device_init_wakeup(&ndev->dev, fep->wol_flag &
  2980. FEC_WOL_HAS_MAGIC_PACKET);
  2981. if (fep->bufdesc_ex && fep->ptp_clock)
  2982. netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
  2983. fep->rx_copybreak = COPYBREAK_DEFAULT;
  2984. INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
  2985. pm_runtime_mark_last_busy(&pdev->dev);
  2986. pm_runtime_put_autosuspend(&pdev->dev);
  2987. return 0;
  2988. failed_register:
  2989. fec_enet_mii_remove(fep);
  2990. failed_mii_init:
  2991. failed_irq:
  2992. failed_init:
  2993. fec_ptp_stop(pdev);
  2994. if (fep->reg_phy)
  2995. regulator_disable(fep->reg_phy);
  2996. failed_reset:
  2997. pm_runtime_put(&pdev->dev);
  2998. pm_runtime_disable(&pdev->dev);
  2999. failed_regulator:
  3000. failed_clk_ipg:
  3001. fec_enet_clk_enable(ndev, false);
  3002. failed_clk:
  3003. if (of_phy_is_fixed_link(np))
  3004. of_phy_deregister_fixed_link(np);
  3005. of_node_put(phy_node);
  3006. failed_phy:
  3007. dev_id--;
  3008. failed_ioremap:
  3009. free_netdev(ndev);
  3010. return ret;
  3011. }
  3012. static int
  3013. fec_drv_remove(struct platform_device *pdev)
  3014. {
  3015. struct net_device *ndev = platform_get_drvdata(pdev);
  3016. struct fec_enet_private *fep = netdev_priv(ndev);
  3017. struct device_node *np = pdev->dev.of_node;
  3018. cancel_work_sync(&fep->tx_timeout_work);
  3019. fec_ptp_stop(pdev);
  3020. unregister_netdev(ndev);
  3021. fec_enet_mii_remove(fep);
  3022. if (fep->reg_phy)
  3023. regulator_disable(fep->reg_phy);
  3024. pm_runtime_put(&pdev->dev);
  3025. pm_runtime_disable(&pdev->dev);
  3026. if (of_phy_is_fixed_link(np))
  3027. of_phy_deregister_fixed_link(np);
  3028. of_node_put(fep->phy_node);
  3029. free_netdev(ndev);
  3030. return 0;
  3031. }
  3032. static int __maybe_unused fec_suspend(struct device *dev)
  3033. {
  3034. struct net_device *ndev = dev_get_drvdata(dev);
  3035. struct fec_enet_private *fep = netdev_priv(ndev);
  3036. rtnl_lock();
  3037. if (netif_running(ndev)) {
  3038. if (fep->wol_flag & FEC_WOL_FLAG_ENABLE)
  3039. fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON;
  3040. phy_stop(ndev->phydev);
  3041. napi_disable(&fep->napi);
  3042. netif_tx_lock_bh(ndev);
  3043. netif_device_detach(ndev);
  3044. netif_tx_unlock_bh(ndev);
  3045. fec_stop(ndev);
  3046. fec_enet_clk_enable(ndev, false);
  3047. if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
  3048. pinctrl_pm_select_sleep_state(&fep->pdev->dev);
  3049. }
  3050. rtnl_unlock();
  3051. if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
  3052. regulator_disable(fep->reg_phy);
  3053. /* SOC supply clock to phy, when clock is disabled, phy link down
  3054. * SOC control phy regulator, when regulator is disabled, phy link down
  3055. */
  3056. if (fep->clk_enet_out || fep->reg_phy)
  3057. fep->link = 0;
  3058. return 0;
  3059. }
  3060. static int __maybe_unused fec_resume(struct device *dev)
  3061. {
  3062. struct net_device *ndev = dev_get_drvdata(dev);
  3063. struct fec_enet_private *fep = netdev_priv(ndev);
  3064. struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
  3065. int ret;
  3066. int val;
  3067. if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
  3068. ret = regulator_enable(fep->reg_phy);
  3069. if (ret)
  3070. return ret;
  3071. }
  3072. rtnl_lock();
  3073. if (netif_running(ndev)) {
  3074. ret = fec_enet_clk_enable(ndev, true);
  3075. if (ret) {
  3076. rtnl_unlock();
  3077. goto failed_clk;
  3078. }
  3079. if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) {
  3080. if (pdata && pdata->sleep_mode_enable)
  3081. pdata->sleep_mode_enable(false);
  3082. val = readl(fep->hwp + FEC_ECNTRL);
  3083. val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
  3084. writel(val, fep->hwp + FEC_ECNTRL);
  3085. fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON;
  3086. } else {
  3087. pinctrl_pm_select_default_state(&fep->pdev->dev);
  3088. }
  3089. fec_restart(ndev);
  3090. netif_tx_lock_bh(ndev);
  3091. netif_device_attach(ndev);
  3092. netif_tx_unlock_bh(ndev);
  3093. napi_enable(&fep->napi);
  3094. phy_start(ndev->phydev);
  3095. }
  3096. rtnl_unlock();
  3097. return 0;
  3098. failed_clk:
  3099. if (fep->reg_phy)
  3100. regulator_disable(fep->reg_phy);
  3101. return ret;
  3102. }
  3103. static int __maybe_unused fec_runtime_suspend(struct device *dev)
  3104. {
  3105. struct net_device *ndev = dev_get_drvdata(dev);
  3106. struct fec_enet_private *fep = netdev_priv(ndev);
  3107. clk_disable_unprepare(fep->clk_ipg);
  3108. return 0;
  3109. }
  3110. static int __maybe_unused fec_runtime_resume(struct device *dev)
  3111. {
  3112. struct net_device *ndev = dev_get_drvdata(dev);
  3113. struct fec_enet_private *fep = netdev_priv(ndev);
  3114. return clk_prepare_enable(fep->clk_ipg);
  3115. }
  3116. static const struct dev_pm_ops fec_pm_ops = {
  3117. SET_SYSTEM_SLEEP_PM_OPS(fec_suspend, fec_resume)
  3118. SET_RUNTIME_PM_OPS(fec_runtime_suspend, fec_runtime_resume, NULL)
  3119. };
  3120. static struct platform_driver fec_driver = {
  3121. .driver = {
  3122. .name = DRIVER_NAME,
  3123. .pm = &fec_pm_ops,
  3124. .of_match_table = fec_dt_ids,
  3125. },
  3126. .id_table = fec_devtype,
  3127. .probe = fec_probe,
  3128. .remove = fec_drv_remove,
  3129. };
  3130. module_platform_driver(fec_driver);
  3131. MODULE_ALIAS("platform:"DRIVER_NAME);
  3132. MODULE_LICENSE("GPL");