fec_main.c 93 KB

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