fec_main.c 93 KB

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