fec_main.c 93 KB

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