fec_main.c 93 KB

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