fec_main.c 96 KB

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