fec_main.c 95 KB

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