fec_main.c 94 KB

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