fec_main.c 87 KB

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