fec_main.c 94 KB

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