cpsw.c 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730
  1. /*
  2. * Texas Instruments Ethernet Switch Driver
  3. *
  4. * Copyright (C) 2012 Texas Instruments
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation version 2.
  9. *
  10. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  11. * kind, whether express or implied; without even the implied warranty
  12. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/io.h>
  17. #include <linux/clk.h>
  18. #include <linux/timer.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/irqreturn.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/if_ether.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/net_tstamp.h>
  27. #include <linux/phy.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/delay.h>
  30. #include <linux/pm_runtime.h>
  31. #include <linux/gpio/consumer.h>
  32. #include <linux/of.h>
  33. #include <linux/of_mdio.h>
  34. #include <linux/of_net.h>
  35. #include <linux/of_device.h>
  36. #include <linux/if_vlan.h>
  37. #include <linux/kmemleak.h>
  38. #include <linux/sys_soc.h>
  39. #include <linux/pinctrl/consumer.h>
  40. #include <net/pkt_cls.h>
  41. #include "cpsw.h"
  42. #include "cpsw_ale.h"
  43. #include "cpts.h"
  44. #include "davinci_cpdma.h"
  45. #include <net/pkt_sched.h>
  46. #define CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_WOL | \
  47. NETIF_MSG_DRV | NETIF_MSG_LINK | \
  48. NETIF_MSG_IFUP | NETIF_MSG_INTR | \
  49. NETIF_MSG_PROBE | NETIF_MSG_TIMER | \
  50. NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | \
  51. NETIF_MSG_TX_ERR | NETIF_MSG_TX_DONE | \
  52. NETIF_MSG_PKTDATA | NETIF_MSG_TX_QUEUED | \
  53. NETIF_MSG_RX_STATUS)
  54. #define cpsw_info(priv, type, format, ...) \
  55. do { \
  56. if (netif_msg_##type(priv) && net_ratelimit()) \
  57. dev_info(priv->dev, format, ## __VA_ARGS__); \
  58. } while (0)
  59. #define cpsw_err(priv, type, format, ...) \
  60. do { \
  61. if (netif_msg_##type(priv) && net_ratelimit()) \
  62. dev_err(priv->dev, format, ## __VA_ARGS__); \
  63. } while (0)
  64. #define cpsw_dbg(priv, type, format, ...) \
  65. do { \
  66. if (netif_msg_##type(priv) && net_ratelimit()) \
  67. dev_dbg(priv->dev, format, ## __VA_ARGS__); \
  68. } while (0)
  69. #define cpsw_notice(priv, type, format, ...) \
  70. do { \
  71. if (netif_msg_##type(priv) && net_ratelimit()) \
  72. dev_notice(priv->dev, format, ## __VA_ARGS__); \
  73. } while (0)
  74. #define ALE_ALL_PORTS 0x7
  75. #define CPSW_MAJOR_VERSION(reg) (reg >> 8 & 0x7)
  76. #define CPSW_MINOR_VERSION(reg) (reg & 0xff)
  77. #define CPSW_RTL_VERSION(reg) ((reg >> 11) & 0x1f)
  78. #define CPSW_VERSION_1 0x19010a
  79. #define CPSW_VERSION_2 0x19010c
  80. #define CPSW_VERSION_3 0x19010f
  81. #define CPSW_VERSION_4 0x190112
  82. #define HOST_PORT_NUM 0
  83. #define CPSW_ALE_PORTS_NUM 3
  84. #define SLIVER_SIZE 0x40
  85. #define CPSW1_HOST_PORT_OFFSET 0x028
  86. #define CPSW1_SLAVE_OFFSET 0x050
  87. #define CPSW1_SLAVE_SIZE 0x040
  88. #define CPSW1_CPDMA_OFFSET 0x100
  89. #define CPSW1_STATERAM_OFFSET 0x200
  90. #define CPSW1_HW_STATS 0x400
  91. #define CPSW1_CPTS_OFFSET 0x500
  92. #define CPSW1_ALE_OFFSET 0x600
  93. #define CPSW1_SLIVER_OFFSET 0x700
  94. #define CPSW2_HOST_PORT_OFFSET 0x108
  95. #define CPSW2_SLAVE_OFFSET 0x200
  96. #define CPSW2_SLAVE_SIZE 0x100
  97. #define CPSW2_CPDMA_OFFSET 0x800
  98. #define CPSW2_HW_STATS 0x900
  99. #define CPSW2_STATERAM_OFFSET 0xa00
  100. #define CPSW2_CPTS_OFFSET 0xc00
  101. #define CPSW2_ALE_OFFSET 0xd00
  102. #define CPSW2_SLIVER_OFFSET 0xd80
  103. #define CPSW2_BD_OFFSET 0x2000
  104. #define CPDMA_RXTHRESH 0x0c0
  105. #define CPDMA_RXFREE 0x0e0
  106. #define CPDMA_TXHDP 0x00
  107. #define CPDMA_RXHDP 0x20
  108. #define CPDMA_TXCP 0x40
  109. #define CPDMA_RXCP 0x60
  110. #define CPSW_POLL_WEIGHT 64
  111. #define CPSW_RX_VLAN_ENCAP_HDR_SIZE 4
  112. #define CPSW_MIN_PACKET_SIZE (VLAN_ETH_ZLEN)
  113. #define CPSW_MAX_PACKET_SIZE (VLAN_ETH_FRAME_LEN +\
  114. ETH_FCS_LEN +\
  115. CPSW_RX_VLAN_ENCAP_HDR_SIZE)
  116. #define RX_PRIORITY_MAPPING 0x76543210
  117. #define TX_PRIORITY_MAPPING 0x33221100
  118. #define CPDMA_TX_PRIORITY_MAP 0x76543210
  119. #define CPSW_VLAN_AWARE BIT(1)
  120. #define CPSW_RX_VLAN_ENCAP BIT(2)
  121. #define CPSW_ALE_VLAN_AWARE 1
  122. #define CPSW_FIFO_NORMAL_MODE (0 << 16)
  123. #define CPSW_FIFO_DUAL_MAC_MODE (1 << 16)
  124. #define CPSW_FIFO_RATE_LIMIT_MODE (2 << 16)
  125. #define CPSW_INTPACEEN (0x3f << 16)
  126. #define CPSW_INTPRESCALE_MASK (0x7FF << 0)
  127. #define CPSW_CMINTMAX_CNT 63
  128. #define CPSW_CMINTMIN_CNT 2
  129. #define CPSW_CMINTMAX_INTVL (1000 / CPSW_CMINTMIN_CNT)
  130. #define CPSW_CMINTMIN_INTVL ((1000 / CPSW_CMINTMAX_CNT) + 1)
  131. #define cpsw_slave_index(cpsw, priv) \
  132. ((cpsw->data.dual_emac) ? priv->emac_port : \
  133. cpsw->data.active_slave)
  134. #define IRQ_NUM 2
  135. #define CPSW_MAX_QUEUES 8
  136. #define CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT 256
  137. #define CPSW_FIFO_QUEUE_TYPE_SHIFT 16
  138. #define CPSW_FIFO_SHAPE_EN_SHIFT 16
  139. #define CPSW_FIFO_RATE_EN_SHIFT 20
  140. #define CPSW_TC_NUM 4
  141. #define CPSW_FIFO_SHAPERS_NUM (CPSW_TC_NUM - 1)
  142. #define CPSW_PCT_MASK 0x7f
  143. #define CPSW_RX_VLAN_ENCAP_HDR_PRIO_SHIFT 29
  144. #define CPSW_RX_VLAN_ENCAP_HDR_PRIO_MSK GENMASK(2, 0)
  145. #define CPSW_RX_VLAN_ENCAP_HDR_VID_SHIFT 16
  146. #define CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_SHIFT 8
  147. #define CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_MSK GENMASK(1, 0)
  148. enum {
  149. CPSW_RX_VLAN_ENCAP_HDR_PKT_VLAN_TAG = 0,
  150. CPSW_RX_VLAN_ENCAP_HDR_PKT_RESERV,
  151. CPSW_RX_VLAN_ENCAP_HDR_PKT_PRIO_TAG,
  152. CPSW_RX_VLAN_ENCAP_HDR_PKT_UNTAG,
  153. };
  154. static int debug_level;
  155. module_param(debug_level, int, 0);
  156. MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
  157. static int ale_ageout = 10;
  158. module_param(ale_ageout, int, 0);
  159. MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
  160. static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
  161. module_param(rx_packet_max, int, 0);
  162. MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
  163. static int descs_pool_size = CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT;
  164. module_param(descs_pool_size, int, 0444);
  165. MODULE_PARM_DESC(descs_pool_size, "Number of CPDMA CPPI descriptors in pool");
  166. struct cpsw_wr_regs {
  167. u32 id_ver;
  168. u32 soft_reset;
  169. u32 control;
  170. u32 int_control;
  171. u32 rx_thresh_en;
  172. u32 rx_en;
  173. u32 tx_en;
  174. u32 misc_en;
  175. u32 mem_allign1[8];
  176. u32 rx_thresh_stat;
  177. u32 rx_stat;
  178. u32 tx_stat;
  179. u32 misc_stat;
  180. u32 mem_allign2[8];
  181. u32 rx_imax;
  182. u32 tx_imax;
  183. };
  184. struct cpsw_ss_regs {
  185. u32 id_ver;
  186. u32 control;
  187. u32 soft_reset;
  188. u32 stat_port_en;
  189. u32 ptype;
  190. u32 soft_idle;
  191. u32 thru_rate;
  192. u32 gap_thresh;
  193. u32 tx_start_wds;
  194. u32 flow_control;
  195. u32 vlan_ltype;
  196. u32 ts_ltype;
  197. u32 dlr_ltype;
  198. };
  199. /* CPSW_PORT_V1 */
  200. #define CPSW1_MAX_BLKS 0x00 /* Maximum FIFO Blocks */
  201. #define CPSW1_BLK_CNT 0x04 /* FIFO Block Usage Count (Read Only) */
  202. #define CPSW1_TX_IN_CTL 0x08 /* Transmit FIFO Control */
  203. #define CPSW1_PORT_VLAN 0x0c /* VLAN Register */
  204. #define CPSW1_TX_PRI_MAP 0x10 /* Tx Header Priority to Switch Pri Mapping */
  205. #define CPSW1_TS_CTL 0x14 /* Time Sync Control */
  206. #define CPSW1_TS_SEQ_LTYPE 0x18 /* Time Sync Sequence ID Offset and Msg Type */
  207. #define CPSW1_TS_VLAN 0x1c /* Time Sync VLAN1 and VLAN2 */
  208. /* CPSW_PORT_V2 */
  209. #define CPSW2_CONTROL 0x00 /* Control Register */
  210. #define CPSW2_MAX_BLKS 0x08 /* Maximum FIFO Blocks */
  211. #define CPSW2_BLK_CNT 0x0c /* FIFO Block Usage Count (Read Only) */
  212. #define CPSW2_TX_IN_CTL 0x10 /* Transmit FIFO Control */
  213. #define CPSW2_PORT_VLAN 0x14 /* VLAN Register */
  214. #define CPSW2_TX_PRI_MAP 0x18 /* Tx Header Priority to Switch Pri Mapping */
  215. #define CPSW2_TS_SEQ_MTYPE 0x1c /* Time Sync Sequence ID Offset and Msg Type */
  216. /* CPSW_PORT_V1 and V2 */
  217. #define SA_LO 0x20 /* CPGMAC_SL Source Address Low */
  218. #define SA_HI 0x24 /* CPGMAC_SL Source Address High */
  219. #define SEND_PERCENT 0x28 /* Transmit Queue Send Percentages */
  220. /* CPSW_PORT_V2 only */
  221. #define RX_DSCP_PRI_MAP0 0x30 /* Rx DSCP Priority to Rx Packet Mapping */
  222. #define RX_DSCP_PRI_MAP1 0x34 /* Rx DSCP Priority to Rx Packet Mapping */
  223. #define RX_DSCP_PRI_MAP2 0x38 /* Rx DSCP Priority to Rx Packet Mapping */
  224. #define RX_DSCP_PRI_MAP3 0x3c /* Rx DSCP Priority to Rx Packet Mapping */
  225. #define RX_DSCP_PRI_MAP4 0x40 /* Rx DSCP Priority to Rx Packet Mapping */
  226. #define RX_DSCP_PRI_MAP5 0x44 /* Rx DSCP Priority to Rx Packet Mapping */
  227. #define RX_DSCP_PRI_MAP6 0x48 /* Rx DSCP Priority to Rx Packet Mapping */
  228. #define RX_DSCP_PRI_MAP7 0x4c /* Rx DSCP Priority to Rx Packet Mapping */
  229. /* Bit definitions for the CPSW2_CONTROL register */
  230. #define PASS_PRI_TAGGED BIT(24) /* Pass Priority Tagged */
  231. #define VLAN_LTYPE2_EN BIT(21) /* VLAN LTYPE 2 enable */
  232. #define VLAN_LTYPE1_EN BIT(20) /* VLAN LTYPE 1 enable */
  233. #define DSCP_PRI_EN BIT(16) /* DSCP Priority Enable */
  234. #define TS_107 BIT(15) /* Tyme Sync Dest IP Address 107 */
  235. #define TS_320 BIT(14) /* Time Sync Dest Port 320 enable */
  236. #define TS_319 BIT(13) /* Time Sync Dest Port 319 enable */
  237. #define TS_132 BIT(12) /* Time Sync Dest IP Addr 132 enable */
  238. #define TS_131 BIT(11) /* Time Sync Dest IP Addr 131 enable */
  239. #define TS_130 BIT(10) /* Time Sync Dest IP Addr 130 enable */
  240. #define TS_129 BIT(9) /* Time Sync Dest IP Addr 129 enable */
  241. #define TS_TTL_NONZERO BIT(8) /* Time Sync Time To Live Non-zero enable */
  242. #define TS_ANNEX_F_EN BIT(6) /* Time Sync Annex F enable */
  243. #define TS_ANNEX_D_EN BIT(4) /* Time Sync Annex D enable */
  244. #define TS_LTYPE2_EN BIT(3) /* Time Sync LTYPE 2 enable */
  245. #define TS_LTYPE1_EN BIT(2) /* Time Sync LTYPE 1 enable */
  246. #define TS_TX_EN BIT(1) /* Time Sync Transmit Enable */
  247. #define TS_RX_EN BIT(0) /* Time Sync Receive Enable */
  248. #define CTRL_V2_TS_BITS \
  249. (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
  250. TS_TTL_NONZERO | TS_ANNEX_D_EN | TS_LTYPE1_EN)
  251. #define CTRL_V2_ALL_TS_MASK (CTRL_V2_TS_BITS | TS_TX_EN | TS_RX_EN)
  252. #define CTRL_V2_TX_TS_BITS (CTRL_V2_TS_BITS | TS_TX_EN)
  253. #define CTRL_V2_RX_TS_BITS (CTRL_V2_TS_BITS | TS_RX_EN)
  254. #define CTRL_V3_TS_BITS \
  255. (TS_107 | TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
  256. TS_TTL_NONZERO | TS_ANNEX_F_EN | TS_ANNEX_D_EN |\
  257. TS_LTYPE1_EN)
  258. #define CTRL_V3_ALL_TS_MASK (CTRL_V3_TS_BITS | TS_TX_EN | TS_RX_EN)
  259. #define CTRL_V3_TX_TS_BITS (CTRL_V3_TS_BITS | TS_TX_EN)
  260. #define CTRL_V3_RX_TS_BITS (CTRL_V3_TS_BITS | TS_RX_EN)
  261. /* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */
  262. #define TS_SEQ_ID_OFFSET_SHIFT (16) /* Time Sync Sequence ID Offset */
  263. #define TS_SEQ_ID_OFFSET_MASK (0x3f)
  264. #define TS_MSG_TYPE_EN_SHIFT (0) /* Time Sync Message Type Enable */
  265. #define TS_MSG_TYPE_EN_MASK (0xffff)
  266. /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
  267. #define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3))
  268. /* Bit definitions for the CPSW1_TS_CTL register */
  269. #define CPSW_V1_TS_RX_EN BIT(0)
  270. #define CPSW_V1_TS_TX_EN BIT(4)
  271. #define CPSW_V1_MSG_TYPE_OFS 16
  272. /* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */
  273. #define CPSW_V1_SEQ_ID_OFS_SHIFT 16
  274. #define CPSW_MAX_BLKS_TX 15
  275. #define CPSW_MAX_BLKS_TX_SHIFT 4
  276. #define CPSW_MAX_BLKS_RX 5
  277. struct cpsw_host_regs {
  278. u32 max_blks;
  279. u32 blk_cnt;
  280. u32 tx_in_ctl;
  281. u32 port_vlan;
  282. u32 tx_pri_map;
  283. u32 cpdma_tx_pri_map;
  284. u32 cpdma_rx_chan_map;
  285. };
  286. struct cpsw_sliver_regs {
  287. u32 id_ver;
  288. u32 mac_control;
  289. u32 mac_status;
  290. u32 soft_reset;
  291. u32 rx_maxlen;
  292. u32 __reserved_0;
  293. u32 rx_pause;
  294. u32 tx_pause;
  295. u32 __reserved_1;
  296. u32 rx_pri_map;
  297. };
  298. struct cpsw_hw_stats {
  299. u32 rxgoodframes;
  300. u32 rxbroadcastframes;
  301. u32 rxmulticastframes;
  302. u32 rxpauseframes;
  303. u32 rxcrcerrors;
  304. u32 rxaligncodeerrors;
  305. u32 rxoversizedframes;
  306. u32 rxjabberframes;
  307. u32 rxundersizedframes;
  308. u32 rxfragments;
  309. u32 __pad_0[2];
  310. u32 rxoctets;
  311. u32 txgoodframes;
  312. u32 txbroadcastframes;
  313. u32 txmulticastframes;
  314. u32 txpauseframes;
  315. u32 txdeferredframes;
  316. u32 txcollisionframes;
  317. u32 txsinglecollframes;
  318. u32 txmultcollframes;
  319. u32 txexcessivecollisions;
  320. u32 txlatecollisions;
  321. u32 txunderrun;
  322. u32 txcarriersenseerrors;
  323. u32 txoctets;
  324. u32 octetframes64;
  325. u32 octetframes65t127;
  326. u32 octetframes128t255;
  327. u32 octetframes256t511;
  328. u32 octetframes512t1023;
  329. u32 octetframes1024tup;
  330. u32 netoctets;
  331. u32 rxsofoverruns;
  332. u32 rxmofoverruns;
  333. u32 rxdmaoverruns;
  334. };
  335. struct cpsw_slave_data {
  336. struct device_node *phy_node;
  337. char phy_id[MII_BUS_ID_SIZE];
  338. int phy_if;
  339. u8 mac_addr[ETH_ALEN];
  340. u16 dual_emac_res_vlan; /* Reserved VLAN for DualEMAC */
  341. };
  342. struct cpsw_platform_data {
  343. struct cpsw_slave_data *slave_data;
  344. u32 ss_reg_ofs; /* Subsystem control register offset */
  345. u32 channels; /* number of cpdma channels (symmetric) */
  346. u32 slaves; /* number of slave cpgmac ports */
  347. u32 active_slave; /* time stamping, ethtool and SIOCGMIIPHY slave */
  348. u32 ale_entries; /* ale table size */
  349. u32 bd_ram_size; /*buffer descriptor ram size */
  350. u32 mac_control; /* Mac control register */
  351. u16 default_vlan; /* Def VLAN for ALE lookup in VLAN aware mode*/
  352. bool dual_emac; /* Enable Dual EMAC mode */
  353. };
  354. struct cpsw_slave {
  355. void __iomem *regs;
  356. struct cpsw_sliver_regs __iomem *sliver;
  357. int slave_num;
  358. u32 mac_control;
  359. struct cpsw_slave_data *data;
  360. struct phy_device *phy;
  361. struct net_device *ndev;
  362. u32 port_vlan;
  363. };
  364. static inline u32 slave_read(struct cpsw_slave *slave, u32 offset)
  365. {
  366. return readl_relaxed(slave->regs + offset);
  367. }
  368. static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset)
  369. {
  370. writel_relaxed(val, slave->regs + offset);
  371. }
  372. struct cpsw_vector {
  373. struct cpdma_chan *ch;
  374. int budget;
  375. };
  376. struct cpsw_common {
  377. struct device *dev;
  378. struct cpsw_platform_data data;
  379. struct napi_struct napi_rx;
  380. struct napi_struct napi_tx;
  381. struct cpsw_ss_regs __iomem *regs;
  382. struct cpsw_wr_regs __iomem *wr_regs;
  383. u8 __iomem *hw_stats;
  384. struct cpsw_host_regs __iomem *host_port_regs;
  385. u32 version;
  386. u32 coal_intvl;
  387. u32 bus_freq_mhz;
  388. int rx_packet_max;
  389. struct cpsw_slave *slaves;
  390. struct cpdma_ctlr *dma;
  391. struct cpsw_vector txv[CPSW_MAX_QUEUES];
  392. struct cpsw_vector rxv[CPSW_MAX_QUEUES];
  393. struct cpsw_ale *ale;
  394. bool quirk_irq;
  395. bool rx_irq_disabled;
  396. bool tx_irq_disabled;
  397. u32 irqs_table[IRQ_NUM];
  398. struct cpts *cpts;
  399. int rx_ch_num, tx_ch_num;
  400. int speed;
  401. int usage_count;
  402. };
  403. struct cpsw_priv {
  404. struct net_device *ndev;
  405. struct device *dev;
  406. u32 msg_enable;
  407. u8 mac_addr[ETH_ALEN];
  408. bool rx_pause;
  409. bool tx_pause;
  410. bool mqprio_hw;
  411. int fifo_bw[CPSW_TC_NUM];
  412. int shp_cfg_speed;
  413. u32 emac_port;
  414. struct cpsw_common *cpsw;
  415. };
  416. struct cpsw_stats {
  417. char stat_string[ETH_GSTRING_LEN];
  418. int type;
  419. int sizeof_stat;
  420. int stat_offset;
  421. };
  422. enum {
  423. CPSW_STATS,
  424. CPDMA_RX_STATS,
  425. CPDMA_TX_STATS,
  426. };
  427. #define CPSW_STAT(m) CPSW_STATS, \
  428. sizeof(((struct cpsw_hw_stats *)0)->m), \
  429. offsetof(struct cpsw_hw_stats, m)
  430. #define CPDMA_RX_STAT(m) CPDMA_RX_STATS, \
  431. sizeof(((struct cpdma_chan_stats *)0)->m), \
  432. offsetof(struct cpdma_chan_stats, m)
  433. #define CPDMA_TX_STAT(m) CPDMA_TX_STATS, \
  434. sizeof(((struct cpdma_chan_stats *)0)->m), \
  435. offsetof(struct cpdma_chan_stats, m)
  436. static const struct cpsw_stats cpsw_gstrings_stats[] = {
  437. { "Good Rx Frames", CPSW_STAT(rxgoodframes) },
  438. { "Broadcast Rx Frames", CPSW_STAT(rxbroadcastframes) },
  439. { "Multicast Rx Frames", CPSW_STAT(rxmulticastframes) },
  440. { "Pause Rx Frames", CPSW_STAT(rxpauseframes) },
  441. { "Rx CRC Errors", CPSW_STAT(rxcrcerrors) },
  442. { "Rx Align/Code Errors", CPSW_STAT(rxaligncodeerrors) },
  443. { "Oversize Rx Frames", CPSW_STAT(rxoversizedframes) },
  444. { "Rx Jabbers", CPSW_STAT(rxjabberframes) },
  445. { "Undersize (Short) Rx Frames", CPSW_STAT(rxundersizedframes) },
  446. { "Rx Fragments", CPSW_STAT(rxfragments) },
  447. { "Rx Octets", CPSW_STAT(rxoctets) },
  448. { "Good Tx Frames", CPSW_STAT(txgoodframes) },
  449. { "Broadcast Tx Frames", CPSW_STAT(txbroadcastframes) },
  450. { "Multicast Tx Frames", CPSW_STAT(txmulticastframes) },
  451. { "Pause Tx Frames", CPSW_STAT(txpauseframes) },
  452. { "Deferred Tx Frames", CPSW_STAT(txdeferredframes) },
  453. { "Collisions", CPSW_STAT(txcollisionframes) },
  454. { "Single Collision Tx Frames", CPSW_STAT(txsinglecollframes) },
  455. { "Multiple Collision Tx Frames", CPSW_STAT(txmultcollframes) },
  456. { "Excessive Collisions", CPSW_STAT(txexcessivecollisions) },
  457. { "Late Collisions", CPSW_STAT(txlatecollisions) },
  458. { "Tx Underrun", CPSW_STAT(txunderrun) },
  459. { "Carrier Sense Errors", CPSW_STAT(txcarriersenseerrors) },
  460. { "Tx Octets", CPSW_STAT(txoctets) },
  461. { "Rx + Tx 64 Octet Frames", CPSW_STAT(octetframes64) },
  462. { "Rx + Tx 65-127 Octet Frames", CPSW_STAT(octetframes65t127) },
  463. { "Rx + Tx 128-255 Octet Frames", CPSW_STAT(octetframes128t255) },
  464. { "Rx + Tx 256-511 Octet Frames", CPSW_STAT(octetframes256t511) },
  465. { "Rx + Tx 512-1023 Octet Frames", CPSW_STAT(octetframes512t1023) },
  466. { "Rx + Tx 1024-Up Octet Frames", CPSW_STAT(octetframes1024tup) },
  467. { "Net Octets", CPSW_STAT(netoctets) },
  468. { "Rx Start of Frame Overruns", CPSW_STAT(rxsofoverruns) },
  469. { "Rx Middle of Frame Overruns", CPSW_STAT(rxmofoverruns) },
  470. { "Rx DMA Overruns", CPSW_STAT(rxdmaoverruns) },
  471. };
  472. static const struct cpsw_stats cpsw_gstrings_ch_stats[] = {
  473. { "head_enqueue", CPDMA_RX_STAT(head_enqueue) },
  474. { "tail_enqueue", CPDMA_RX_STAT(tail_enqueue) },
  475. { "pad_enqueue", CPDMA_RX_STAT(pad_enqueue) },
  476. { "misqueued", CPDMA_RX_STAT(misqueued) },
  477. { "desc_alloc_fail", CPDMA_RX_STAT(desc_alloc_fail) },
  478. { "pad_alloc_fail", CPDMA_RX_STAT(pad_alloc_fail) },
  479. { "runt_receive_buf", CPDMA_RX_STAT(runt_receive_buff) },
  480. { "runt_transmit_buf", CPDMA_RX_STAT(runt_transmit_buff) },
  481. { "empty_dequeue", CPDMA_RX_STAT(empty_dequeue) },
  482. { "busy_dequeue", CPDMA_RX_STAT(busy_dequeue) },
  483. { "good_dequeue", CPDMA_RX_STAT(good_dequeue) },
  484. { "requeue", CPDMA_RX_STAT(requeue) },
  485. { "teardown_dequeue", CPDMA_RX_STAT(teardown_dequeue) },
  486. };
  487. #define CPSW_STATS_COMMON_LEN ARRAY_SIZE(cpsw_gstrings_stats)
  488. #define CPSW_STATS_CH_LEN ARRAY_SIZE(cpsw_gstrings_ch_stats)
  489. #define ndev_to_cpsw(ndev) (((struct cpsw_priv *)netdev_priv(ndev))->cpsw)
  490. #define napi_to_cpsw(napi) container_of(napi, struct cpsw_common, napi)
  491. #define for_each_slave(priv, func, arg...) \
  492. do { \
  493. struct cpsw_slave *slave; \
  494. struct cpsw_common *cpsw = (priv)->cpsw; \
  495. int n; \
  496. if (cpsw->data.dual_emac) \
  497. (func)((cpsw)->slaves + priv->emac_port, ##arg);\
  498. else \
  499. for (n = cpsw->data.slaves, \
  500. slave = cpsw->slaves; \
  501. n; n--) \
  502. (func)(slave++, ##arg); \
  503. } while (0)
  504. static inline int cpsw_get_slave_port(u32 slave_num)
  505. {
  506. return slave_num + 1;
  507. }
  508. static void cpsw_add_mcast(struct cpsw_priv *priv, u8 *addr)
  509. {
  510. struct cpsw_common *cpsw = priv->cpsw;
  511. if (cpsw->data.dual_emac) {
  512. struct cpsw_slave *slave = cpsw->slaves + priv->emac_port;
  513. int slave_port = cpsw_get_slave_port(slave->slave_num);
  514. cpsw_ale_add_mcast(cpsw->ale, addr,
  515. 1 << slave_port | ALE_PORT_HOST,
  516. ALE_VLAN, slave->port_vlan, 0);
  517. return;
  518. }
  519. cpsw_ale_add_mcast(cpsw->ale, addr, ALE_ALL_PORTS, 0, 0, 0);
  520. }
  521. static void cpsw_set_promiscious(struct net_device *ndev, bool enable)
  522. {
  523. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  524. struct cpsw_ale *ale = cpsw->ale;
  525. int i;
  526. if (cpsw->data.dual_emac) {
  527. bool flag = false;
  528. /* Enabling promiscuous mode for one interface will be
  529. * common for both the interface as the interface shares
  530. * the same hardware resource.
  531. */
  532. for (i = 0; i < cpsw->data.slaves; i++)
  533. if (cpsw->slaves[i].ndev->flags & IFF_PROMISC)
  534. flag = true;
  535. if (!enable && flag) {
  536. enable = true;
  537. dev_err(&ndev->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n");
  538. }
  539. if (enable) {
  540. /* Enable Bypass */
  541. cpsw_ale_control_set(ale, 0, ALE_BYPASS, 1);
  542. dev_dbg(&ndev->dev, "promiscuity enabled\n");
  543. } else {
  544. /* Disable Bypass */
  545. cpsw_ale_control_set(ale, 0, ALE_BYPASS, 0);
  546. dev_dbg(&ndev->dev, "promiscuity disabled\n");
  547. }
  548. } else {
  549. if (enable) {
  550. unsigned long timeout = jiffies + HZ;
  551. /* Disable Learn for all ports (host is port 0 and slaves are port 1 and up */
  552. for (i = 0; i <= cpsw->data.slaves; i++) {
  553. cpsw_ale_control_set(ale, i,
  554. ALE_PORT_NOLEARN, 1);
  555. cpsw_ale_control_set(ale, i,
  556. ALE_PORT_NO_SA_UPDATE, 1);
  557. }
  558. /* Clear All Untouched entries */
  559. cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
  560. do {
  561. cpu_relax();
  562. if (cpsw_ale_control_get(ale, 0, ALE_AGEOUT))
  563. break;
  564. } while (time_after(timeout, jiffies));
  565. cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
  566. /* Clear all mcast from ALE */
  567. cpsw_ale_flush_multicast(ale, ALE_ALL_PORTS, -1);
  568. __dev_mc_unsync(ndev, NULL);
  569. /* Flood All Unicast Packets to Host port */
  570. cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 1);
  571. dev_dbg(&ndev->dev, "promiscuity enabled\n");
  572. } else {
  573. /* Don't Flood All Unicast Packets to Host port */
  574. cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 0);
  575. /* Enable Learn for all ports (host is port 0 and slaves are port 1 and up */
  576. for (i = 0; i <= cpsw->data.slaves; i++) {
  577. cpsw_ale_control_set(ale, i,
  578. ALE_PORT_NOLEARN, 0);
  579. cpsw_ale_control_set(ale, i,
  580. ALE_PORT_NO_SA_UPDATE, 0);
  581. }
  582. dev_dbg(&ndev->dev, "promiscuity disabled\n");
  583. }
  584. }
  585. }
  586. static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
  587. {
  588. struct cpsw_priv *priv = netdev_priv(ndev);
  589. struct cpsw_common *cpsw = priv->cpsw;
  590. int vid;
  591. if (cpsw->data.dual_emac)
  592. vid = cpsw->slaves[priv->emac_port].port_vlan;
  593. else
  594. vid = cpsw->data.default_vlan;
  595. if (ndev->flags & IFF_PROMISC) {
  596. /* Enable promiscuous mode */
  597. cpsw_set_promiscious(ndev, true);
  598. cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI);
  599. return;
  600. } else {
  601. /* Disable promiscuous mode */
  602. cpsw_set_promiscious(ndev, false);
  603. }
  604. /* Restore allmulti on vlans if necessary */
  605. cpsw_ale_set_allmulti(cpsw->ale, priv->ndev->flags & IFF_ALLMULTI);
  606. /* Clear all mcast from ALE */
  607. cpsw_ale_flush_multicast(cpsw->ale, ALE_ALL_PORTS, vid);
  608. if (!netdev_mc_empty(ndev)) {
  609. struct netdev_hw_addr *ha;
  610. /* program multicast address list into ALE register */
  611. netdev_for_each_mc_addr(ha, ndev) {
  612. cpsw_add_mcast(priv, ha->addr);
  613. }
  614. }
  615. }
  616. static void cpsw_intr_enable(struct cpsw_common *cpsw)
  617. {
  618. writel_relaxed(0xFF, &cpsw->wr_regs->tx_en);
  619. writel_relaxed(0xFF, &cpsw->wr_regs->rx_en);
  620. cpdma_ctlr_int_ctrl(cpsw->dma, true);
  621. return;
  622. }
  623. static void cpsw_intr_disable(struct cpsw_common *cpsw)
  624. {
  625. writel_relaxed(0, &cpsw->wr_regs->tx_en);
  626. writel_relaxed(0, &cpsw->wr_regs->rx_en);
  627. cpdma_ctlr_int_ctrl(cpsw->dma, false);
  628. return;
  629. }
  630. static void cpsw_tx_handler(void *token, int len, int status)
  631. {
  632. struct netdev_queue *txq;
  633. struct sk_buff *skb = token;
  634. struct net_device *ndev = skb->dev;
  635. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  636. /* Check whether the queue is stopped due to stalled tx dma, if the
  637. * queue is stopped then start the queue as we have free desc for tx
  638. */
  639. txq = netdev_get_tx_queue(ndev, skb_get_queue_mapping(skb));
  640. if (unlikely(netif_tx_queue_stopped(txq)))
  641. netif_tx_wake_queue(txq);
  642. cpts_tx_timestamp(cpsw->cpts, skb);
  643. ndev->stats.tx_packets++;
  644. ndev->stats.tx_bytes += len;
  645. dev_kfree_skb_any(skb);
  646. }
  647. static void cpsw_rx_vlan_encap(struct sk_buff *skb)
  648. {
  649. struct cpsw_priv *priv = netdev_priv(skb->dev);
  650. struct cpsw_common *cpsw = priv->cpsw;
  651. u32 rx_vlan_encap_hdr = *((u32 *)skb->data);
  652. u16 vtag, vid, prio, pkt_type;
  653. /* Remove VLAN header encapsulation word */
  654. skb_pull(skb, CPSW_RX_VLAN_ENCAP_HDR_SIZE);
  655. pkt_type = (rx_vlan_encap_hdr >>
  656. CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_SHIFT) &
  657. CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_MSK;
  658. /* Ignore unknown & Priority-tagged packets*/
  659. if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_RESERV ||
  660. pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_PRIO_TAG)
  661. return;
  662. vid = (rx_vlan_encap_hdr >>
  663. CPSW_RX_VLAN_ENCAP_HDR_VID_SHIFT) &
  664. VLAN_VID_MASK;
  665. /* Ignore vid 0 and pass packet as is */
  666. if (!vid)
  667. return;
  668. /* Ignore default vlans in dual mac mode */
  669. if (cpsw->data.dual_emac &&
  670. vid == cpsw->slaves[priv->emac_port].port_vlan)
  671. return;
  672. prio = (rx_vlan_encap_hdr >>
  673. CPSW_RX_VLAN_ENCAP_HDR_PRIO_SHIFT) &
  674. CPSW_RX_VLAN_ENCAP_HDR_PRIO_MSK;
  675. vtag = (prio << VLAN_PRIO_SHIFT) | vid;
  676. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
  677. /* strip vlan tag for VLAN-tagged packet */
  678. if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_VLAN_TAG) {
  679. memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
  680. skb_pull(skb, VLAN_HLEN);
  681. }
  682. }
  683. static void cpsw_rx_handler(void *token, int len, int status)
  684. {
  685. struct cpdma_chan *ch;
  686. struct sk_buff *skb = token;
  687. struct sk_buff *new_skb;
  688. struct net_device *ndev = skb->dev;
  689. int ret = 0, port;
  690. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  691. if (cpsw->data.dual_emac) {
  692. port = CPDMA_RX_SOURCE_PORT(status);
  693. if (port) {
  694. ndev = cpsw->slaves[--port].ndev;
  695. skb->dev = ndev;
  696. }
  697. }
  698. if (unlikely(status < 0) || unlikely(!netif_running(ndev))) {
  699. /* In dual emac mode check for all interfaces */
  700. if (cpsw->data.dual_emac && cpsw->usage_count &&
  701. (status >= 0)) {
  702. /* The packet received is for the interface which
  703. * is already down and the other interface is up
  704. * and running, instead of freeing which results
  705. * in reducing of the number of rx descriptor in
  706. * DMA engine, requeue skb back to cpdma.
  707. */
  708. new_skb = skb;
  709. goto requeue;
  710. }
  711. /* the interface is going down, skbs are purged */
  712. dev_kfree_skb_any(skb);
  713. return;
  714. }
  715. new_skb = netdev_alloc_skb_ip_align(ndev, cpsw->rx_packet_max);
  716. if (new_skb) {
  717. skb_copy_queue_mapping(new_skb, skb);
  718. skb_put(skb, len);
  719. if (status & CPDMA_RX_VLAN_ENCAP)
  720. cpsw_rx_vlan_encap(skb);
  721. cpts_rx_timestamp(cpsw->cpts, skb);
  722. skb->protocol = eth_type_trans(skb, ndev);
  723. netif_receive_skb(skb);
  724. ndev->stats.rx_bytes += len;
  725. ndev->stats.rx_packets++;
  726. kmemleak_not_leak(new_skb);
  727. } else {
  728. ndev->stats.rx_dropped++;
  729. new_skb = skb;
  730. }
  731. requeue:
  732. if (netif_dormant(ndev)) {
  733. dev_kfree_skb_any(new_skb);
  734. return;
  735. }
  736. ch = cpsw->rxv[skb_get_queue_mapping(new_skb)].ch;
  737. ret = cpdma_chan_submit(ch, new_skb, new_skb->data,
  738. skb_tailroom(new_skb), 0);
  739. if (WARN_ON(ret < 0))
  740. dev_kfree_skb_any(new_skb);
  741. }
  742. static void cpsw_split_res(struct net_device *ndev)
  743. {
  744. struct cpsw_priv *priv = netdev_priv(ndev);
  745. u32 consumed_rate = 0, bigest_rate = 0;
  746. struct cpsw_common *cpsw = priv->cpsw;
  747. struct cpsw_vector *txv = cpsw->txv;
  748. int i, ch_weight, rlim_ch_num = 0;
  749. int budget, bigest_rate_ch = 0;
  750. u32 ch_rate, max_rate;
  751. int ch_budget = 0;
  752. for (i = 0; i < cpsw->tx_ch_num; i++) {
  753. ch_rate = cpdma_chan_get_rate(txv[i].ch);
  754. if (!ch_rate)
  755. continue;
  756. rlim_ch_num++;
  757. consumed_rate += ch_rate;
  758. }
  759. if (cpsw->tx_ch_num == rlim_ch_num) {
  760. max_rate = consumed_rate;
  761. } else if (!rlim_ch_num) {
  762. ch_budget = CPSW_POLL_WEIGHT / cpsw->tx_ch_num;
  763. bigest_rate = 0;
  764. max_rate = consumed_rate;
  765. } else {
  766. max_rate = cpsw->speed * 1000;
  767. /* if max_rate is less then expected due to reduced link speed,
  768. * split proportionally according next potential max speed
  769. */
  770. if (max_rate < consumed_rate)
  771. max_rate *= 10;
  772. if (max_rate < consumed_rate)
  773. max_rate *= 10;
  774. ch_budget = (consumed_rate * CPSW_POLL_WEIGHT) / max_rate;
  775. ch_budget = (CPSW_POLL_WEIGHT - ch_budget) /
  776. (cpsw->tx_ch_num - rlim_ch_num);
  777. bigest_rate = (max_rate - consumed_rate) /
  778. (cpsw->tx_ch_num - rlim_ch_num);
  779. }
  780. /* split tx weight/budget */
  781. budget = CPSW_POLL_WEIGHT;
  782. for (i = 0; i < cpsw->tx_ch_num; i++) {
  783. ch_rate = cpdma_chan_get_rate(txv[i].ch);
  784. if (ch_rate) {
  785. txv[i].budget = (ch_rate * CPSW_POLL_WEIGHT) / max_rate;
  786. if (!txv[i].budget)
  787. txv[i].budget++;
  788. if (ch_rate > bigest_rate) {
  789. bigest_rate_ch = i;
  790. bigest_rate = ch_rate;
  791. }
  792. ch_weight = (ch_rate * 100) / max_rate;
  793. if (!ch_weight)
  794. ch_weight++;
  795. cpdma_chan_set_weight(cpsw->txv[i].ch, ch_weight);
  796. } else {
  797. txv[i].budget = ch_budget;
  798. if (!bigest_rate_ch)
  799. bigest_rate_ch = i;
  800. cpdma_chan_set_weight(cpsw->txv[i].ch, 0);
  801. }
  802. budget -= txv[i].budget;
  803. }
  804. if (budget)
  805. txv[bigest_rate_ch].budget += budget;
  806. /* split rx budget */
  807. budget = CPSW_POLL_WEIGHT;
  808. ch_budget = budget / cpsw->rx_ch_num;
  809. for (i = 0; i < cpsw->rx_ch_num; i++) {
  810. cpsw->rxv[i].budget = ch_budget;
  811. budget -= ch_budget;
  812. }
  813. if (budget)
  814. cpsw->rxv[0].budget += budget;
  815. }
  816. static irqreturn_t cpsw_tx_interrupt(int irq, void *dev_id)
  817. {
  818. struct cpsw_common *cpsw = dev_id;
  819. writel(0, &cpsw->wr_regs->tx_en);
  820. cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_TX);
  821. if (cpsw->quirk_irq) {
  822. disable_irq_nosync(cpsw->irqs_table[1]);
  823. cpsw->tx_irq_disabled = true;
  824. }
  825. napi_schedule(&cpsw->napi_tx);
  826. return IRQ_HANDLED;
  827. }
  828. static irqreturn_t cpsw_rx_interrupt(int irq, void *dev_id)
  829. {
  830. struct cpsw_common *cpsw = dev_id;
  831. writel(0, &cpsw->wr_regs->rx_en);
  832. cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_RX);
  833. if (cpsw->quirk_irq) {
  834. disable_irq_nosync(cpsw->irqs_table[0]);
  835. cpsw->rx_irq_disabled = true;
  836. }
  837. napi_schedule(&cpsw->napi_rx);
  838. return IRQ_HANDLED;
  839. }
  840. static int cpsw_tx_mq_poll(struct napi_struct *napi_tx, int budget)
  841. {
  842. u32 ch_map;
  843. int num_tx, cur_budget, ch;
  844. struct cpsw_common *cpsw = napi_to_cpsw(napi_tx);
  845. struct cpsw_vector *txv;
  846. /* process every unprocessed channel */
  847. ch_map = cpdma_ctrl_txchs_state(cpsw->dma);
  848. for (ch = 0, num_tx = 0; ch_map & 0xff; ch_map <<= 1, ch++) {
  849. if (!(ch_map & 0x80))
  850. continue;
  851. txv = &cpsw->txv[ch];
  852. if (unlikely(txv->budget > budget - num_tx))
  853. cur_budget = budget - num_tx;
  854. else
  855. cur_budget = txv->budget;
  856. num_tx += cpdma_chan_process(txv->ch, cur_budget);
  857. if (num_tx >= budget)
  858. break;
  859. }
  860. if (num_tx < budget) {
  861. napi_complete(napi_tx);
  862. writel(0xff, &cpsw->wr_regs->tx_en);
  863. }
  864. return num_tx;
  865. }
  866. static int cpsw_tx_poll(struct napi_struct *napi_tx, int budget)
  867. {
  868. struct cpsw_common *cpsw = napi_to_cpsw(napi_tx);
  869. int num_tx;
  870. num_tx = cpdma_chan_process(cpsw->txv[0].ch, budget);
  871. if (num_tx < budget) {
  872. napi_complete(napi_tx);
  873. writel(0xff, &cpsw->wr_regs->tx_en);
  874. if (cpsw->tx_irq_disabled) {
  875. cpsw->tx_irq_disabled = false;
  876. enable_irq(cpsw->irqs_table[1]);
  877. }
  878. }
  879. return num_tx;
  880. }
  881. static int cpsw_rx_mq_poll(struct napi_struct *napi_rx, int budget)
  882. {
  883. u32 ch_map;
  884. int num_rx, cur_budget, ch;
  885. struct cpsw_common *cpsw = napi_to_cpsw(napi_rx);
  886. struct cpsw_vector *rxv;
  887. /* process every unprocessed channel */
  888. ch_map = cpdma_ctrl_rxchs_state(cpsw->dma);
  889. for (ch = 0, num_rx = 0; ch_map; ch_map >>= 1, ch++) {
  890. if (!(ch_map & 0x01))
  891. continue;
  892. rxv = &cpsw->rxv[ch];
  893. if (unlikely(rxv->budget > budget - num_rx))
  894. cur_budget = budget - num_rx;
  895. else
  896. cur_budget = rxv->budget;
  897. num_rx += cpdma_chan_process(rxv->ch, cur_budget);
  898. if (num_rx >= budget)
  899. break;
  900. }
  901. if (num_rx < budget) {
  902. napi_complete_done(napi_rx, num_rx);
  903. writel(0xff, &cpsw->wr_regs->rx_en);
  904. }
  905. return num_rx;
  906. }
  907. static int cpsw_rx_poll(struct napi_struct *napi_rx, int budget)
  908. {
  909. struct cpsw_common *cpsw = napi_to_cpsw(napi_rx);
  910. int num_rx;
  911. num_rx = cpdma_chan_process(cpsw->rxv[0].ch, budget);
  912. if (num_rx < budget) {
  913. napi_complete_done(napi_rx, num_rx);
  914. writel(0xff, &cpsw->wr_regs->rx_en);
  915. if (cpsw->rx_irq_disabled) {
  916. cpsw->rx_irq_disabled = false;
  917. enable_irq(cpsw->irqs_table[0]);
  918. }
  919. }
  920. return num_rx;
  921. }
  922. static inline void soft_reset(const char *module, void __iomem *reg)
  923. {
  924. unsigned long timeout = jiffies + HZ;
  925. writel_relaxed(1, reg);
  926. do {
  927. cpu_relax();
  928. } while ((readl_relaxed(reg) & 1) && time_after(timeout, jiffies));
  929. WARN(readl_relaxed(reg) & 1, "failed to soft-reset %s\n", module);
  930. }
  931. static void cpsw_set_slave_mac(struct cpsw_slave *slave,
  932. struct cpsw_priv *priv)
  933. {
  934. slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
  935. slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
  936. }
  937. static bool cpsw_shp_is_off(struct cpsw_priv *priv)
  938. {
  939. struct cpsw_common *cpsw = priv->cpsw;
  940. struct cpsw_slave *slave;
  941. u32 shift, mask, val;
  942. val = readl_relaxed(&cpsw->regs->ptype);
  943. slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
  944. shift = CPSW_FIFO_SHAPE_EN_SHIFT + 3 * slave->slave_num;
  945. mask = 7 << shift;
  946. val = val & mask;
  947. return !val;
  948. }
  949. static void cpsw_fifo_shp_on(struct cpsw_priv *priv, int fifo, int on)
  950. {
  951. struct cpsw_common *cpsw = priv->cpsw;
  952. struct cpsw_slave *slave;
  953. u32 shift, mask, val;
  954. val = readl_relaxed(&cpsw->regs->ptype);
  955. slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
  956. shift = CPSW_FIFO_SHAPE_EN_SHIFT + 3 * slave->slave_num;
  957. mask = (1 << --fifo) << shift;
  958. val = on ? val | mask : val & ~mask;
  959. writel_relaxed(val, &cpsw->regs->ptype);
  960. }
  961. static void _cpsw_adjust_link(struct cpsw_slave *slave,
  962. struct cpsw_priv *priv, bool *link)
  963. {
  964. struct phy_device *phy = slave->phy;
  965. u32 mac_control = 0;
  966. u32 slave_port;
  967. struct cpsw_common *cpsw = priv->cpsw;
  968. if (!phy)
  969. return;
  970. slave_port = cpsw_get_slave_port(slave->slave_num);
  971. if (phy->link) {
  972. mac_control = cpsw->data.mac_control;
  973. /* enable forwarding */
  974. cpsw_ale_control_set(cpsw->ale, slave_port,
  975. ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
  976. if (phy->speed == 1000)
  977. mac_control |= BIT(7); /* GIGABITEN */
  978. if (phy->duplex)
  979. mac_control |= BIT(0); /* FULLDUPLEXEN */
  980. /* set speed_in input in case RMII mode is used in 100Mbps */
  981. if (phy->speed == 100)
  982. mac_control |= BIT(15);
  983. /* in band mode only works in 10Mbps RGMII mode */
  984. else if ((phy->speed == 10) && phy_interface_is_rgmii(phy))
  985. mac_control |= BIT(18); /* In Band mode */
  986. if (priv->rx_pause)
  987. mac_control |= BIT(3);
  988. if (priv->tx_pause)
  989. mac_control |= BIT(4);
  990. *link = true;
  991. if (priv->shp_cfg_speed &&
  992. priv->shp_cfg_speed != slave->phy->speed &&
  993. !cpsw_shp_is_off(priv))
  994. dev_warn(priv->dev,
  995. "Speed was changed, CBS shaper speeds are changed!");
  996. } else {
  997. mac_control = 0;
  998. /* disable forwarding */
  999. cpsw_ale_control_set(cpsw->ale, slave_port,
  1000. ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
  1001. }
  1002. if (mac_control != slave->mac_control) {
  1003. phy_print_status(phy);
  1004. writel_relaxed(mac_control, &slave->sliver->mac_control);
  1005. }
  1006. slave->mac_control = mac_control;
  1007. }
  1008. static int cpsw_get_common_speed(struct cpsw_common *cpsw)
  1009. {
  1010. int i, speed;
  1011. for (i = 0, speed = 0; i < cpsw->data.slaves; i++)
  1012. if (cpsw->slaves[i].phy && cpsw->slaves[i].phy->link)
  1013. speed += cpsw->slaves[i].phy->speed;
  1014. return speed;
  1015. }
  1016. static int cpsw_need_resplit(struct cpsw_common *cpsw)
  1017. {
  1018. int i, rlim_ch_num;
  1019. int speed, ch_rate;
  1020. /* re-split resources only in case speed was changed */
  1021. speed = cpsw_get_common_speed(cpsw);
  1022. if (speed == cpsw->speed || !speed)
  1023. return 0;
  1024. cpsw->speed = speed;
  1025. for (i = 0, rlim_ch_num = 0; i < cpsw->tx_ch_num; i++) {
  1026. ch_rate = cpdma_chan_get_rate(cpsw->txv[i].ch);
  1027. if (!ch_rate)
  1028. break;
  1029. rlim_ch_num++;
  1030. }
  1031. /* cases not dependent on speed */
  1032. if (!rlim_ch_num || rlim_ch_num == cpsw->tx_ch_num)
  1033. return 0;
  1034. return 1;
  1035. }
  1036. static void cpsw_adjust_link(struct net_device *ndev)
  1037. {
  1038. struct cpsw_priv *priv = netdev_priv(ndev);
  1039. struct cpsw_common *cpsw = priv->cpsw;
  1040. bool link = false;
  1041. for_each_slave(priv, _cpsw_adjust_link, priv, &link);
  1042. if (link) {
  1043. if (cpsw_need_resplit(cpsw))
  1044. cpsw_split_res(ndev);
  1045. netif_carrier_on(ndev);
  1046. if (netif_running(ndev))
  1047. netif_tx_wake_all_queues(ndev);
  1048. } else {
  1049. netif_carrier_off(ndev);
  1050. netif_tx_stop_all_queues(ndev);
  1051. }
  1052. }
  1053. static int cpsw_get_coalesce(struct net_device *ndev,
  1054. struct ethtool_coalesce *coal)
  1055. {
  1056. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  1057. coal->rx_coalesce_usecs = cpsw->coal_intvl;
  1058. return 0;
  1059. }
  1060. static int cpsw_set_coalesce(struct net_device *ndev,
  1061. struct ethtool_coalesce *coal)
  1062. {
  1063. struct cpsw_priv *priv = netdev_priv(ndev);
  1064. u32 int_ctrl;
  1065. u32 num_interrupts = 0;
  1066. u32 prescale = 0;
  1067. u32 addnl_dvdr = 1;
  1068. u32 coal_intvl = 0;
  1069. struct cpsw_common *cpsw = priv->cpsw;
  1070. coal_intvl = coal->rx_coalesce_usecs;
  1071. int_ctrl = readl(&cpsw->wr_regs->int_control);
  1072. prescale = cpsw->bus_freq_mhz * 4;
  1073. if (!coal->rx_coalesce_usecs) {
  1074. int_ctrl &= ~(CPSW_INTPRESCALE_MASK | CPSW_INTPACEEN);
  1075. goto update_return;
  1076. }
  1077. if (coal_intvl < CPSW_CMINTMIN_INTVL)
  1078. coal_intvl = CPSW_CMINTMIN_INTVL;
  1079. if (coal_intvl > CPSW_CMINTMAX_INTVL) {
  1080. /* Interrupt pacer works with 4us Pulse, we can
  1081. * throttle further by dilating the 4us pulse.
  1082. */
  1083. addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale;
  1084. if (addnl_dvdr > 1) {
  1085. prescale *= addnl_dvdr;
  1086. if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr))
  1087. coal_intvl = (CPSW_CMINTMAX_INTVL
  1088. * addnl_dvdr);
  1089. } else {
  1090. addnl_dvdr = 1;
  1091. coal_intvl = CPSW_CMINTMAX_INTVL;
  1092. }
  1093. }
  1094. num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
  1095. writel(num_interrupts, &cpsw->wr_regs->rx_imax);
  1096. writel(num_interrupts, &cpsw->wr_regs->tx_imax);
  1097. int_ctrl |= CPSW_INTPACEEN;
  1098. int_ctrl &= (~CPSW_INTPRESCALE_MASK);
  1099. int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK);
  1100. update_return:
  1101. writel(int_ctrl, &cpsw->wr_regs->int_control);
  1102. cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl);
  1103. cpsw->coal_intvl = coal_intvl;
  1104. return 0;
  1105. }
  1106. static int cpsw_get_sset_count(struct net_device *ndev, int sset)
  1107. {
  1108. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  1109. switch (sset) {
  1110. case ETH_SS_STATS:
  1111. return (CPSW_STATS_COMMON_LEN +
  1112. (cpsw->rx_ch_num + cpsw->tx_ch_num) *
  1113. CPSW_STATS_CH_LEN);
  1114. default:
  1115. return -EOPNOTSUPP;
  1116. }
  1117. }
  1118. static void cpsw_add_ch_strings(u8 **p, int ch_num, int rx_dir)
  1119. {
  1120. int ch_stats_len;
  1121. int line;
  1122. int i;
  1123. ch_stats_len = CPSW_STATS_CH_LEN * ch_num;
  1124. for (i = 0; i < ch_stats_len; i++) {
  1125. line = i % CPSW_STATS_CH_LEN;
  1126. snprintf(*p, ETH_GSTRING_LEN,
  1127. "%s DMA chan %ld: %s", rx_dir ? "Rx" : "Tx",
  1128. (long)(i / CPSW_STATS_CH_LEN),
  1129. cpsw_gstrings_ch_stats[line].stat_string);
  1130. *p += ETH_GSTRING_LEN;
  1131. }
  1132. }
  1133. static void cpsw_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
  1134. {
  1135. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  1136. u8 *p = data;
  1137. int i;
  1138. switch (stringset) {
  1139. case ETH_SS_STATS:
  1140. for (i = 0; i < CPSW_STATS_COMMON_LEN; i++) {
  1141. memcpy(p, cpsw_gstrings_stats[i].stat_string,
  1142. ETH_GSTRING_LEN);
  1143. p += ETH_GSTRING_LEN;
  1144. }
  1145. cpsw_add_ch_strings(&p, cpsw->rx_ch_num, 1);
  1146. cpsw_add_ch_strings(&p, cpsw->tx_ch_num, 0);
  1147. break;
  1148. }
  1149. }
  1150. static void cpsw_get_ethtool_stats(struct net_device *ndev,
  1151. struct ethtool_stats *stats, u64 *data)
  1152. {
  1153. u8 *p;
  1154. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  1155. struct cpdma_chan_stats ch_stats;
  1156. int i, l, ch;
  1157. /* Collect Davinci CPDMA stats for Rx and Tx Channel */
  1158. for (l = 0; l < CPSW_STATS_COMMON_LEN; l++)
  1159. data[l] = readl(cpsw->hw_stats +
  1160. cpsw_gstrings_stats[l].stat_offset);
  1161. for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
  1162. cpdma_chan_get_stats(cpsw->rxv[ch].ch, &ch_stats);
  1163. for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
  1164. p = (u8 *)&ch_stats +
  1165. cpsw_gstrings_ch_stats[i].stat_offset;
  1166. data[l] = *(u32 *)p;
  1167. }
  1168. }
  1169. for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
  1170. cpdma_chan_get_stats(cpsw->txv[ch].ch, &ch_stats);
  1171. for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
  1172. p = (u8 *)&ch_stats +
  1173. cpsw_gstrings_ch_stats[i].stat_offset;
  1174. data[l] = *(u32 *)p;
  1175. }
  1176. }
  1177. }
  1178. static inline int cpsw_tx_packet_submit(struct cpsw_priv *priv,
  1179. struct sk_buff *skb,
  1180. struct cpdma_chan *txch)
  1181. {
  1182. struct cpsw_common *cpsw = priv->cpsw;
  1183. skb_tx_timestamp(skb);
  1184. return cpdma_chan_submit(txch, skb, skb->data, skb->len,
  1185. priv->emac_port + cpsw->data.dual_emac);
  1186. }
  1187. static inline void cpsw_add_dual_emac_def_ale_entries(
  1188. struct cpsw_priv *priv, struct cpsw_slave *slave,
  1189. u32 slave_port)
  1190. {
  1191. struct cpsw_common *cpsw = priv->cpsw;
  1192. u32 port_mask = 1 << slave_port | ALE_PORT_HOST;
  1193. if (cpsw->version == CPSW_VERSION_1)
  1194. slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
  1195. else
  1196. slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
  1197. cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask,
  1198. port_mask, port_mask, 0);
  1199. cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
  1200. port_mask, ALE_VLAN, slave->port_vlan, 0);
  1201. cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
  1202. HOST_PORT_NUM, ALE_VLAN |
  1203. ALE_SECURE, slave->port_vlan);
  1204. cpsw_ale_control_set(cpsw->ale, slave_port,
  1205. ALE_PORT_DROP_UNKNOWN_VLAN, 1);
  1206. }
  1207. static void soft_reset_slave(struct cpsw_slave *slave)
  1208. {
  1209. char name[32];
  1210. snprintf(name, sizeof(name), "slave-%d", slave->slave_num);
  1211. soft_reset(name, &slave->sliver->soft_reset);
  1212. }
  1213. static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
  1214. {
  1215. u32 slave_port;
  1216. struct phy_device *phy;
  1217. struct cpsw_common *cpsw = priv->cpsw;
  1218. soft_reset_slave(slave);
  1219. /* setup priority mapping */
  1220. writel_relaxed(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
  1221. switch (cpsw->version) {
  1222. case CPSW_VERSION_1:
  1223. slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
  1224. /* Increase RX FIFO size to 5 for supporting fullduplex
  1225. * flow control mode
  1226. */
  1227. slave_write(slave,
  1228. (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
  1229. CPSW_MAX_BLKS_RX, CPSW1_MAX_BLKS);
  1230. break;
  1231. case CPSW_VERSION_2:
  1232. case CPSW_VERSION_3:
  1233. case CPSW_VERSION_4:
  1234. slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
  1235. /* Increase RX FIFO size to 5 for supporting fullduplex
  1236. * flow control mode
  1237. */
  1238. slave_write(slave,
  1239. (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
  1240. CPSW_MAX_BLKS_RX, CPSW2_MAX_BLKS);
  1241. break;
  1242. }
  1243. /* setup max packet size, and mac address */
  1244. writel_relaxed(cpsw->rx_packet_max, &slave->sliver->rx_maxlen);
  1245. cpsw_set_slave_mac(slave, priv);
  1246. slave->mac_control = 0; /* no link yet */
  1247. slave_port = cpsw_get_slave_port(slave->slave_num);
  1248. if (cpsw->data.dual_emac)
  1249. cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
  1250. else
  1251. cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
  1252. 1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
  1253. if (slave->data->phy_node) {
  1254. phy = of_phy_connect(priv->ndev, slave->data->phy_node,
  1255. &cpsw_adjust_link, 0, slave->data->phy_if);
  1256. if (!phy) {
  1257. dev_err(priv->dev, "phy \"%pOF\" not found on slave %d\n",
  1258. slave->data->phy_node,
  1259. slave->slave_num);
  1260. return;
  1261. }
  1262. } else {
  1263. phy = phy_connect(priv->ndev, slave->data->phy_id,
  1264. &cpsw_adjust_link, slave->data->phy_if);
  1265. if (IS_ERR(phy)) {
  1266. dev_err(priv->dev,
  1267. "phy \"%s\" not found on slave %d, err %ld\n",
  1268. slave->data->phy_id, slave->slave_num,
  1269. PTR_ERR(phy));
  1270. return;
  1271. }
  1272. }
  1273. slave->phy = phy;
  1274. phy_attached_info(slave->phy);
  1275. phy_start(slave->phy);
  1276. /* Configure GMII_SEL register */
  1277. cpsw_phy_sel(cpsw->dev, slave->phy->interface, slave->slave_num);
  1278. }
  1279. static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
  1280. {
  1281. struct cpsw_common *cpsw = priv->cpsw;
  1282. const int vlan = cpsw->data.default_vlan;
  1283. u32 reg;
  1284. int i;
  1285. int unreg_mcast_mask;
  1286. reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
  1287. CPSW2_PORT_VLAN;
  1288. writel(vlan, &cpsw->host_port_regs->port_vlan);
  1289. for (i = 0; i < cpsw->data.slaves; i++)
  1290. slave_write(cpsw->slaves + i, vlan, reg);
  1291. if (priv->ndev->flags & IFF_ALLMULTI)
  1292. unreg_mcast_mask = ALE_ALL_PORTS;
  1293. else
  1294. unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
  1295. cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS,
  1296. ALE_ALL_PORTS, ALE_ALL_PORTS,
  1297. unreg_mcast_mask);
  1298. }
  1299. static void cpsw_init_host_port(struct cpsw_priv *priv)
  1300. {
  1301. u32 fifo_mode;
  1302. u32 control_reg;
  1303. struct cpsw_common *cpsw = priv->cpsw;
  1304. /* soft reset the controller and initialize ale */
  1305. soft_reset("cpsw", &cpsw->regs->soft_reset);
  1306. cpsw_ale_start(cpsw->ale);
  1307. /* switch to vlan unaware mode */
  1308. cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE,
  1309. CPSW_ALE_VLAN_AWARE);
  1310. control_reg = readl(&cpsw->regs->control);
  1311. control_reg |= CPSW_VLAN_AWARE | CPSW_RX_VLAN_ENCAP;
  1312. writel(control_reg, &cpsw->regs->control);
  1313. fifo_mode = (cpsw->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
  1314. CPSW_FIFO_NORMAL_MODE;
  1315. writel(fifo_mode, &cpsw->host_port_regs->tx_in_ctl);
  1316. /* setup host port priority mapping */
  1317. writel_relaxed(CPDMA_TX_PRIORITY_MAP,
  1318. &cpsw->host_port_regs->cpdma_tx_pri_map);
  1319. writel_relaxed(0, &cpsw->host_port_regs->cpdma_rx_chan_map);
  1320. cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
  1321. ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
  1322. if (!cpsw->data.dual_emac) {
  1323. cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
  1324. 0, 0);
  1325. cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
  1326. ALE_PORT_HOST, 0, 0, ALE_MCAST_FWD_2);
  1327. }
  1328. }
  1329. static int cpsw_fill_rx_channels(struct cpsw_priv *priv)
  1330. {
  1331. struct cpsw_common *cpsw = priv->cpsw;
  1332. struct sk_buff *skb;
  1333. int ch_buf_num;
  1334. int ch, i, ret;
  1335. for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
  1336. ch_buf_num = cpdma_chan_get_rx_buf_num(cpsw->rxv[ch].ch);
  1337. for (i = 0; i < ch_buf_num; i++) {
  1338. skb = __netdev_alloc_skb_ip_align(priv->ndev,
  1339. cpsw->rx_packet_max,
  1340. GFP_KERNEL);
  1341. if (!skb) {
  1342. cpsw_err(priv, ifup, "cannot allocate skb\n");
  1343. return -ENOMEM;
  1344. }
  1345. skb_set_queue_mapping(skb, ch);
  1346. ret = cpdma_chan_submit(cpsw->rxv[ch].ch, skb,
  1347. skb->data, skb_tailroom(skb),
  1348. 0);
  1349. if (ret < 0) {
  1350. cpsw_err(priv, ifup,
  1351. "cannot submit skb to channel %d rx, error %d\n",
  1352. ch, ret);
  1353. kfree_skb(skb);
  1354. return ret;
  1355. }
  1356. kmemleak_not_leak(skb);
  1357. }
  1358. cpsw_info(priv, ifup, "ch %d rx, submitted %d descriptors\n",
  1359. ch, ch_buf_num);
  1360. }
  1361. return 0;
  1362. }
  1363. static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_common *cpsw)
  1364. {
  1365. u32 slave_port;
  1366. slave_port = cpsw_get_slave_port(slave->slave_num);
  1367. if (!slave->phy)
  1368. return;
  1369. phy_stop(slave->phy);
  1370. phy_disconnect(slave->phy);
  1371. slave->phy = NULL;
  1372. cpsw_ale_control_set(cpsw->ale, slave_port,
  1373. ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
  1374. soft_reset_slave(slave);
  1375. }
  1376. static int cpsw_tc_to_fifo(int tc, int num_tc)
  1377. {
  1378. if (tc == num_tc - 1)
  1379. return 0;
  1380. return CPSW_FIFO_SHAPERS_NUM - tc;
  1381. }
  1382. static int cpsw_set_fifo_bw(struct cpsw_priv *priv, int fifo, int bw)
  1383. {
  1384. struct cpsw_common *cpsw = priv->cpsw;
  1385. u32 val = 0, send_pct, shift;
  1386. struct cpsw_slave *slave;
  1387. int pct = 0, i;
  1388. if (bw > priv->shp_cfg_speed * 1000)
  1389. goto err;
  1390. /* shaping has to stay enabled for highest fifos linearly
  1391. * and fifo bw no more then interface can allow
  1392. */
  1393. slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
  1394. send_pct = slave_read(slave, SEND_PERCENT);
  1395. for (i = CPSW_FIFO_SHAPERS_NUM; i > 0; i--) {
  1396. if (!bw) {
  1397. if (i >= fifo || !priv->fifo_bw[i])
  1398. continue;
  1399. dev_warn(priv->dev, "Prev FIFO%d is shaped", i);
  1400. continue;
  1401. }
  1402. if (!priv->fifo_bw[i] && i > fifo) {
  1403. dev_err(priv->dev, "Upper FIFO%d is not shaped", i);
  1404. return -EINVAL;
  1405. }
  1406. shift = (i - 1) * 8;
  1407. if (i == fifo) {
  1408. send_pct &= ~(CPSW_PCT_MASK << shift);
  1409. val = DIV_ROUND_UP(bw, priv->shp_cfg_speed * 10);
  1410. if (!val)
  1411. val = 1;
  1412. send_pct |= val << shift;
  1413. pct += val;
  1414. continue;
  1415. }
  1416. if (priv->fifo_bw[i])
  1417. pct += (send_pct >> shift) & CPSW_PCT_MASK;
  1418. }
  1419. if (pct >= 100)
  1420. goto err;
  1421. slave_write(slave, send_pct, SEND_PERCENT);
  1422. priv->fifo_bw[fifo] = bw;
  1423. dev_warn(priv->dev, "set FIFO%d bw = %d\n", fifo,
  1424. DIV_ROUND_CLOSEST(val * priv->shp_cfg_speed, 100));
  1425. return 0;
  1426. err:
  1427. dev_err(priv->dev, "Bandwidth doesn't fit in tc configuration");
  1428. return -EINVAL;
  1429. }
  1430. static int cpsw_set_fifo_rlimit(struct cpsw_priv *priv, int fifo, int bw)
  1431. {
  1432. struct cpsw_common *cpsw = priv->cpsw;
  1433. struct cpsw_slave *slave;
  1434. u32 tx_in_ctl_rg, val;
  1435. int ret;
  1436. ret = cpsw_set_fifo_bw(priv, fifo, bw);
  1437. if (ret)
  1438. return ret;
  1439. slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
  1440. tx_in_ctl_rg = cpsw->version == CPSW_VERSION_1 ?
  1441. CPSW1_TX_IN_CTL : CPSW2_TX_IN_CTL;
  1442. if (!bw)
  1443. cpsw_fifo_shp_on(priv, fifo, bw);
  1444. val = slave_read(slave, tx_in_ctl_rg);
  1445. if (cpsw_shp_is_off(priv)) {
  1446. /* disable FIFOs rate limited queues */
  1447. val &= ~(0xf << CPSW_FIFO_RATE_EN_SHIFT);
  1448. /* set type of FIFO queues to normal priority mode */
  1449. val &= ~(3 << CPSW_FIFO_QUEUE_TYPE_SHIFT);
  1450. /* set type of FIFO queues to be rate limited */
  1451. if (bw)
  1452. val |= 2 << CPSW_FIFO_QUEUE_TYPE_SHIFT;
  1453. else
  1454. priv->shp_cfg_speed = 0;
  1455. }
  1456. /* toggle a FIFO rate limited queue */
  1457. if (bw)
  1458. val |= BIT(fifo + CPSW_FIFO_RATE_EN_SHIFT);
  1459. else
  1460. val &= ~BIT(fifo + CPSW_FIFO_RATE_EN_SHIFT);
  1461. slave_write(slave, val, tx_in_ctl_rg);
  1462. /* FIFO transmit shape enable */
  1463. cpsw_fifo_shp_on(priv, fifo, bw);
  1464. return 0;
  1465. }
  1466. /* Defaults:
  1467. * class A - prio 3
  1468. * class B - prio 2
  1469. * shaping for class A should be set first
  1470. */
  1471. static int cpsw_set_cbs(struct net_device *ndev,
  1472. struct tc_cbs_qopt_offload *qopt)
  1473. {
  1474. struct cpsw_priv *priv = netdev_priv(ndev);
  1475. struct cpsw_common *cpsw = priv->cpsw;
  1476. struct cpsw_slave *slave;
  1477. int prev_speed = 0;
  1478. int tc, ret, fifo;
  1479. u32 bw = 0;
  1480. tc = netdev_txq_to_tc(priv->ndev, qopt->queue);
  1481. /* enable channels in backward order, as highest FIFOs must be rate
  1482. * limited first and for compliance with CPDMA rate limited channels
  1483. * that also used in bacward order. FIFO0 cannot be rate limited.
  1484. */
  1485. fifo = cpsw_tc_to_fifo(tc, ndev->num_tc);
  1486. if (!fifo) {
  1487. dev_err(priv->dev, "Last tc%d can't be rate limited", tc);
  1488. return -EINVAL;
  1489. }
  1490. /* do nothing, it's disabled anyway */
  1491. if (!qopt->enable && !priv->fifo_bw[fifo])
  1492. return 0;
  1493. /* shapers can be set if link speed is known */
  1494. slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
  1495. if (slave->phy && slave->phy->link) {
  1496. if (priv->shp_cfg_speed &&
  1497. priv->shp_cfg_speed != slave->phy->speed)
  1498. prev_speed = priv->shp_cfg_speed;
  1499. priv->shp_cfg_speed = slave->phy->speed;
  1500. }
  1501. if (!priv->shp_cfg_speed) {
  1502. dev_err(priv->dev, "Link speed is not known");
  1503. return -1;
  1504. }
  1505. ret = pm_runtime_get_sync(cpsw->dev);
  1506. if (ret < 0) {
  1507. pm_runtime_put_noidle(cpsw->dev);
  1508. return ret;
  1509. }
  1510. bw = qopt->enable ? qopt->idleslope : 0;
  1511. ret = cpsw_set_fifo_rlimit(priv, fifo, bw);
  1512. if (ret) {
  1513. priv->shp_cfg_speed = prev_speed;
  1514. prev_speed = 0;
  1515. }
  1516. if (bw && prev_speed)
  1517. dev_warn(priv->dev,
  1518. "Speed was changed, CBS shaper speeds are changed!");
  1519. pm_runtime_put_sync(cpsw->dev);
  1520. return ret;
  1521. }
  1522. static void cpsw_cbs_resume(struct cpsw_slave *slave, struct cpsw_priv *priv)
  1523. {
  1524. int fifo, bw;
  1525. for (fifo = CPSW_FIFO_SHAPERS_NUM; fifo > 0; fifo--) {
  1526. bw = priv->fifo_bw[fifo];
  1527. if (!bw)
  1528. continue;
  1529. cpsw_set_fifo_rlimit(priv, fifo, bw);
  1530. }
  1531. }
  1532. static void cpsw_mqprio_resume(struct cpsw_slave *slave, struct cpsw_priv *priv)
  1533. {
  1534. struct cpsw_common *cpsw = priv->cpsw;
  1535. u32 tx_prio_map = 0;
  1536. int i, tc, fifo;
  1537. u32 tx_prio_rg;
  1538. if (!priv->mqprio_hw)
  1539. return;
  1540. for (i = 0; i < 8; i++) {
  1541. tc = netdev_get_prio_tc_map(priv->ndev, i);
  1542. fifo = CPSW_FIFO_SHAPERS_NUM - tc;
  1543. tx_prio_map |= fifo << (4 * i);
  1544. }
  1545. tx_prio_rg = cpsw->version == CPSW_VERSION_1 ?
  1546. CPSW1_TX_PRI_MAP : CPSW2_TX_PRI_MAP;
  1547. slave_write(slave, tx_prio_map, tx_prio_rg);
  1548. }
  1549. /* restore resources after port reset */
  1550. static void cpsw_restore(struct cpsw_priv *priv)
  1551. {
  1552. /* restore MQPRIO offload */
  1553. for_each_slave(priv, cpsw_mqprio_resume, priv);
  1554. /* restore CBS offload */
  1555. for_each_slave(priv, cpsw_cbs_resume, priv);
  1556. }
  1557. static int cpsw_ndo_open(struct net_device *ndev)
  1558. {
  1559. struct cpsw_priv *priv = netdev_priv(ndev);
  1560. struct cpsw_common *cpsw = priv->cpsw;
  1561. int ret;
  1562. u32 reg;
  1563. ret = pm_runtime_get_sync(cpsw->dev);
  1564. if (ret < 0) {
  1565. pm_runtime_put_noidle(cpsw->dev);
  1566. return ret;
  1567. }
  1568. netif_carrier_off(ndev);
  1569. /* Notify the stack of the actual queue counts. */
  1570. ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num);
  1571. if (ret) {
  1572. dev_err(priv->dev, "cannot set real number of tx queues\n");
  1573. goto err_cleanup;
  1574. }
  1575. ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num);
  1576. if (ret) {
  1577. dev_err(priv->dev, "cannot set real number of rx queues\n");
  1578. goto err_cleanup;
  1579. }
  1580. reg = cpsw->version;
  1581. dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
  1582. CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
  1583. CPSW_RTL_VERSION(reg));
  1584. /* Initialize host and slave ports */
  1585. if (!cpsw->usage_count)
  1586. cpsw_init_host_port(priv);
  1587. for_each_slave(priv, cpsw_slave_open, priv);
  1588. /* Add default VLAN */
  1589. if (!cpsw->data.dual_emac)
  1590. cpsw_add_default_vlan(priv);
  1591. else
  1592. cpsw_ale_add_vlan(cpsw->ale, cpsw->data.default_vlan,
  1593. ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
  1594. /* initialize shared resources for every ndev */
  1595. if (!cpsw->usage_count) {
  1596. /* disable priority elevation */
  1597. writel_relaxed(0, &cpsw->regs->ptype);
  1598. /* enable statistics collection only on all ports */
  1599. writel_relaxed(0x7, &cpsw->regs->stat_port_en);
  1600. /* Enable internal fifo flow control */
  1601. writel(0x7, &cpsw->regs->flow_control);
  1602. napi_enable(&cpsw->napi_rx);
  1603. napi_enable(&cpsw->napi_tx);
  1604. if (cpsw->tx_irq_disabled) {
  1605. cpsw->tx_irq_disabled = false;
  1606. enable_irq(cpsw->irqs_table[1]);
  1607. }
  1608. if (cpsw->rx_irq_disabled) {
  1609. cpsw->rx_irq_disabled = false;
  1610. enable_irq(cpsw->irqs_table[0]);
  1611. }
  1612. ret = cpsw_fill_rx_channels(priv);
  1613. if (ret < 0)
  1614. goto err_cleanup;
  1615. if (cpts_register(cpsw->cpts))
  1616. dev_err(priv->dev, "error registering cpts device\n");
  1617. }
  1618. cpsw_restore(priv);
  1619. /* Enable Interrupt pacing if configured */
  1620. if (cpsw->coal_intvl != 0) {
  1621. struct ethtool_coalesce coal;
  1622. coal.rx_coalesce_usecs = cpsw->coal_intvl;
  1623. cpsw_set_coalesce(ndev, &coal);
  1624. }
  1625. cpdma_ctlr_start(cpsw->dma);
  1626. cpsw_intr_enable(cpsw);
  1627. cpsw->usage_count++;
  1628. return 0;
  1629. err_cleanup:
  1630. cpdma_ctlr_stop(cpsw->dma);
  1631. for_each_slave(priv, cpsw_slave_stop, cpsw);
  1632. pm_runtime_put_sync(cpsw->dev);
  1633. netif_carrier_off(priv->ndev);
  1634. return ret;
  1635. }
  1636. static int cpsw_ndo_stop(struct net_device *ndev)
  1637. {
  1638. struct cpsw_priv *priv = netdev_priv(ndev);
  1639. struct cpsw_common *cpsw = priv->cpsw;
  1640. cpsw_info(priv, ifdown, "shutting down cpsw device\n");
  1641. netif_tx_stop_all_queues(priv->ndev);
  1642. netif_carrier_off(priv->ndev);
  1643. if (cpsw->usage_count <= 1) {
  1644. napi_disable(&cpsw->napi_rx);
  1645. napi_disable(&cpsw->napi_tx);
  1646. cpts_unregister(cpsw->cpts);
  1647. cpsw_intr_disable(cpsw);
  1648. cpdma_ctlr_stop(cpsw->dma);
  1649. cpsw_ale_stop(cpsw->ale);
  1650. }
  1651. for_each_slave(priv, cpsw_slave_stop, cpsw);
  1652. if (cpsw_need_resplit(cpsw))
  1653. cpsw_split_res(ndev);
  1654. cpsw->usage_count--;
  1655. pm_runtime_put_sync(cpsw->dev);
  1656. return 0;
  1657. }
  1658. static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
  1659. struct net_device *ndev)
  1660. {
  1661. struct cpsw_priv *priv = netdev_priv(ndev);
  1662. struct cpsw_common *cpsw = priv->cpsw;
  1663. struct cpts *cpts = cpsw->cpts;
  1664. struct netdev_queue *txq;
  1665. struct cpdma_chan *txch;
  1666. int ret, q_idx;
  1667. if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
  1668. cpsw_err(priv, tx_err, "packet pad failed\n");
  1669. ndev->stats.tx_dropped++;
  1670. return NET_XMIT_DROP;
  1671. }
  1672. if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
  1673. cpts_is_tx_enabled(cpts) && cpts_can_timestamp(cpts, skb))
  1674. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1675. q_idx = skb_get_queue_mapping(skb);
  1676. if (q_idx >= cpsw->tx_ch_num)
  1677. q_idx = q_idx % cpsw->tx_ch_num;
  1678. txch = cpsw->txv[q_idx].ch;
  1679. txq = netdev_get_tx_queue(ndev, q_idx);
  1680. ret = cpsw_tx_packet_submit(priv, skb, txch);
  1681. if (unlikely(ret != 0)) {
  1682. cpsw_err(priv, tx_err, "desc submit failed\n");
  1683. goto fail;
  1684. }
  1685. /* If there is no more tx desc left free then we need to
  1686. * tell the kernel to stop sending us tx frames.
  1687. */
  1688. if (unlikely(!cpdma_check_free_tx_desc(txch))) {
  1689. netif_tx_stop_queue(txq);
  1690. /* Barrier, so that stop_queue visible to other cpus */
  1691. smp_mb__after_atomic();
  1692. if (cpdma_check_free_tx_desc(txch))
  1693. netif_tx_wake_queue(txq);
  1694. }
  1695. return NETDEV_TX_OK;
  1696. fail:
  1697. ndev->stats.tx_dropped++;
  1698. netif_tx_stop_queue(txq);
  1699. /* Barrier, so that stop_queue visible to other cpus */
  1700. smp_mb__after_atomic();
  1701. if (cpdma_check_free_tx_desc(txch))
  1702. netif_tx_wake_queue(txq);
  1703. return NETDEV_TX_BUSY;
  1704. }
  1705. #if IS_ENABLED(CONFIG_TI_CPTS)
  1706. static void cpsw_hwtstamp_v1(struct cpsw_common *cpsw)
  1707. {
  1708. struct cpsw_slave *slave = &cpsw->slaves[cpsw->data.active_slave];
  1709. u32 ts_en, seq_id;
  1710. if (!cpts_is_tx_enabled(cpsw->cpts) &&
  1711. !cpts_is_rx_enabled(cpsw->cpts)) {
  1712. slave_write(slave, 0, CPSW1_TS_CTL);
  1713. return;
  1714. }
  1715. seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
  1716. ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
  1717. if (cpts_is_tx_enabled(cpsw->cpts))
  1718. ts_en |= CPSW_V1_TS_TX_EN;
  1719. if (cpts_is_rx_enabled(cpsw->cpts))
  1720. ts_en |= CPSW_V1_TS_RX_EN;
  1721. slave_write(slave, ts_en, CPSW1_TS_CTL);
  1722. slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
  1723. }
  1724. static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
  1725. {
  1726. struct cpsw_slave *slave;
  1727. struct cpsw_common *cpsw = priv->cpsw;
  1728. u32 ctrl, mtype;
  1729. slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
  1730. ctrl = slave_read(slave, CPSW2_CONTROL);
  1731. switch (cpsw->version) {
  1732. case CPSW_VERSION_2:
  1733. ctrl &= ~CTRL_V2_ALL_TS_MASK;
  1734. if (cpts_is_tx_enabled(cpsw->cpts))
  1735. ctrl |= CTRL_V2_TX_TS_BITS;
  1736. if (cpts_is_rx_enabled(cpsw->cpts))
  1737. ctrl |= CTRL_V2_RX_TS_BITS;
  1738. break;
  1739. case CPSW_VERSION_3:
  1740. default:
  1741. ctrl &= ~CTRL_V3_ALL_TS_MASK;
  1742. if (cpts_is_tx_enabled(cpsw->cpts))
  1743. ctrl |= CTRL_V3_TX_TS_BITS;
  1744. if (cpts_is_rx_enabled(cpsw->cpts))
  1745. ctrl |= CTRL_V3_RX_TS_BITS;
  1746. break;
  1747. }
  1748. mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
  1749. slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
  1750. slave_write(slave, ctrl, CPSW2_CONTROL);
  1751. writel_relaxed(ETH_P_1588, &cpsw->regs->ts_ltype);
  1752. }
  1753. static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
  1754. {
  1755. struct cpsw_priv *priv = netdev_priv(dev);
  1756. struct hwtstamp_config cfg;
  1757. struct cpsw_common *cpsw = priv->cpsw;
  1758. struct cpts *cpts = cpsw->cpts;
  1759. if (cpsw->version != CPSW_VERSION_1 &&
  1760. cpsw->version != CPSW_VERSION_2 &&
  1761. cpsw->version != CPSW_VERSION_3)
  1762. return -EOPNOTSUPP;
  1763. if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
  1764. return -EFAULT;
  1765. /* reserved for future extensions */
  1766. if (cfg.flags)
  1767. return -EINVAL;
  1768. if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON)
  1769. return -ERANGE;
  1770. switch (cfg.rx_filter) {
  1771. case HWTSTAMP_FILTER_NONE:
  1772. cpts_rx_enable(cpts, 0);
  1773. break;
  1774. case HWTSTAMP_FILTER_ALL:
  1775. case HWTSTAMP_FILTER_NTP_ALL:
  1776. return -ERANGE;
  1777. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  1778. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  1779. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  1780. cpts_rx_enable(cpts, HWTSTAMP_FILTER_PTP_V1_L4_EVENT);
  1781. cfg.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
  1782. break;
  1783. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  1784. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  1785. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  1786. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  1787. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  1788. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  1789. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  1790. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  1791. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  1792. cpts_rx_enable(cpts, HWTSTAMP_FILTER_PTP_V2_EVENT);
  1793. cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  1794. break;
  1795. default:
  1796. return -ERANGE;
  1797. }
  1798. cpts_tx_enable(cpts, cfg.tx_type == HWTSTAMP_TX_ON);
  1799. switch (cpsw->version) {
  1800. case CPSW_VERSION_1:
  1801. cpsw_hwtstamp_v1(cpsw);
  1802. break;
  1803. case CPSW_VERSION_2:
  1804. case CPSW_VERSION_3:
  1805. cpsw_hwtstamp_v2(priv);
  1806. break;
  1807. default:
  1808. WARN_ON(1);
  1809. }
  1810. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  1811. }
  1812. static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
  1813. {
  1814. struct cpsw_common *cpsw = ndev_to_cpsw(dev);
  1815. struct cpts *cpts = cpsw->cpts;
  1816. struct hwtstamp_config cfg;
  1817. if (cpsw->version != CPSW_VERSION_1 &&
  1818. cpsw->version != CPSW_VERSION_2 &&
  1819. cpsw->version != CPSW_VERSION_3)
  1820. return -EOPNOTSUPP;
  1821. cfg.flags = 0;
  1822. cfg.tx_type = cpts_is_tx_enabled(cpts) ?
  1823. HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
  1824. cfg.rx_filter = (cpts_is_rx_enabled(cpts) ?
  1825. cpts->rx_enable : HWTSTAMP_FILTER_NONE);
  1826. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  1827. }
  1828. #else
  1829. static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
  1830. {
  1831. return -EOPNOTSUPP;
  1832. }
  1833. static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
  1834. {
  1835. return -EOPNOTSUPP;
  1836. }
  1837. #endif /*CONFIG_TI_CPTS*/
  1838. static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
  1839. {
  1840. struct cpsw_priv *priv = netdev_priv(dev);
  1841. struct cpsw_common *cpsw = priv->cpsw;
  1842. int slave_no = cpsw_slave_index(cpsw, priv);
  1843. if (!netif_running(dev))
  1844. return -EINVAL;
  1845. switch (cmd) {
  1846. case SIOCSHWTSTAMP:
  1847. return cpsw_hwtstamp_set(dev, req);
  1848. case SIOCGHWTSTAMP:
  1849. return cpsw_hwtstamp_get(dev, req);
  1850. }
  1851. if (!cpsw->slaves[slave_no].phy)
  1852. return -EOPNOTSUPP;
  1853. return phy_mii_ioctl(cpsw->slaves[slave_no].phy, req, cmd);
  1854. }
  1855. static void cpsw_ndo_tx_timeout(struct net_device *ndev)
  1856. {
  1857. struct cpsw_priv *priv = netdev_priv(ndev);
  1858. struct cpsw_common *cpsw = priv->cpsw;
  1859. int ch;
  1860. cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
  1861. ndev->stats.tx_errors++;
  1862. cpsw_intr_disable(cpsw);
  1863. for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
  1864. cpdma_chan_stop(cpsw->txv[ch].ch);
  1865. cpdma_chan_start(cpsw->txv[ch].ch);
  1866. }
  1867. cpsw_intr_enable(cpsw);
  1868. netif_trans_update(ndev);
  1869. netif_tx_wake_all_queues(ndev);
  1870. }
  1871. static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
  1872. {
  1873. struct cpsw_priv *priv = netdev_priv(ndev);
  1874. struct sockaddr *addr = (struct sockaddr *)p;
  1875. struct cpsw_common *cpsw = priv->cpsw;
  1876. int flags = 0;
  1877. u16 vid = 0;
  1878. int ret;
  1879. if (!is_valid_ether_addr(addr->sa_data))
  1880. return -EADDRNOTAVAIL;
  1881. ret = pm_runtime_get_sync(cpsw->dev);
  1882. if (ret < 0) {
  1883. pm_runtime_put_noidle(cpsw->dev);
  1884. return ret;
  1885. }
  1886. if (cpsw->data.dual_emac) {
  1887. vid = cpsw->slaves[priv->emac_port].port_vlan;
  1888. flags = ALE_VLAN;
  1889. }
  1890. cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
  1891. flags, vid);
  1892. cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
  1893. flags, vid);
  1894. memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
  1895. memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
  1896. for_each_slave(priv, cpsw_set_slave_mac, priv);
  1897. pm_runtime_put(cpsw->dev);
  1898. return 0;
  1899. }
  1900. #ifdef CONFIG_NET_POLL_CONTROLLER
  1901. static void cpsw_ndo_poll_controller(struct net_device *ndev)
  1902. {
  1903. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  1904. cpsw_intr_disable(cpsw);
  1905. cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
  1906. cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
  1907. cpsw_intr_enable(cpsw);
  1908. }
  1909. #endif
  1910. static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
  1911. unsigned short vid)
  1912. {
  1913. int ret;
  1914. int unreg_mcast_mask = 0;
  1915. u32 port_mask;
  1916. struct cpsw_common *cpsw = priv->cpsw;
  1917. if (cpsw->data.dual_emac) {
  1918. port_mask = (1 << (priv->emac_port + 1)) | ALE_PORT_HOST;
  1919. if (priv->ndev->flags & IFF_ALLMULTI)
  1920. unreg_mcast_mask = port_mask;
  1921. } else {
  1922. port_mask = ALE_ALL_PORTS;
  1923. if (priv->ndev->flags & IFF_ALLMULTI)
  1924. unreg_mcast_mask = ALE_ALL_PORTS;
  1925. else
  1926. unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
  1927. }
  1928. ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
  1929. unreg_mcast_mask);
  1930. if (ret != 0)
  1931. return ret;
  1932. ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
  1933. HOST_PORT_NUM, ALE_VLAN, vid);
  1934. if (ret != 0)
  1935. goto clean_vid;
  1936. ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
  1937. port_mask, ALE_VLAN, vid, 0);
  1938. if (ret != 0)
  1939. goto clean_vlan_ucast;
  1940. return 0;
  1941. clean_vlan_ucast:
  1942. cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
  1943. HOST_PORT_NUM, ALE_VLAN, vid);
  1944. clean_vid:
  1945. cpsw_ale_del_vlan(cpsw->ale, vid, 0);
  1946. return ret;
  1947. }
  1948. static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
  1949. __be16 proto, u16 vid)
  1950. {
  1951. struct cpsw_priv *priv = netdev_priv(ndev);
  1952. struct cpsw_common *cpsw = priv->cpsw;
  1953. int ret;
  1954. if (vid == cpsw->data.default_vlan)
  1955. return 0;
  1956. ret = pm_runtime_get_sync(cpsw->dev);
  1957. if (ret < 0) {
  1958. pm_runtime_put_noidle(cpsw->dev);
  1959. return ret;
  1960. }
  1961. if (cpsw->data.dual_emac) {
  1962. /* In dual EMAC, reserved VLAN id should not be used for
  1963. * creating VLAN interfaces as this can break the dual
  1964. * EMAC port separation
  1965. */
  1966. int i;
  1967. for (i = 0; i < cpsw->data.slaves; i++) {
  1968. if (vid == cpsw->slaves[i].port_vlan) {
  1969. ret = -EINVAL;
  1970. goto err;
  1971. }
  1972. }
  1973. }
  1974. dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
  1975. ret = cpsw_add_vlan_ale_entry(priv, vid);
  1976. err:
  1977. pm_runtime_put(cpsw->dev);
  1978. return ret;
  1979. }
  1980. static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
  1981. __be16 proto, u16 vid)
  1982. {
  1983. struct cpsw_priv *priv = netdev_priv(ndev);
  1984. struct cpsw_common *cpsw = priv->cpsw;
  1985. int ret;
  1986. if (vid == cpsw->data.default_vlan)
  1987. return 0;
  1988. ret = pm_runtime_get_sync(cpsw->dev);
  1989. if (ret < 0) {
  1990. pm_runtime_put_noidle(cpsw->dev);
  1991. return ret;
  1992. }
  1993. if (cpsw->data.dual_emac) {
  1994. int i;
  1995. for (i = 0; i < cpsw->data.slaves; i++) {
  1996. if (vid == cpsw->slaves[i].port_vlan)
  1997. goto err;
  1998. }
  1999. }
  2000. dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
  2001. ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
  2002. ret |= cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
  2003. HOST_PORT_NUM, ALE_VLAN, vid);
  2004. ret |= cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
  2005. 0, ALE_VLAN, vid);
  2006. err:
  2007. pm_runtime_put(cpsw->dev);
  2008. return ret;
  2009. }
  2010. static int cpsw_ndo_set_tx_maxrate(struct net_device *ndev, int queue, u32 rate)
  2011. {
  2012. struct cpsw_priv *priv = netdev_priv(ndev);
  2013. struct cpsw_common *cpsw = priv->cpsw;
  2014. struct cpsw_slave *slave;
  2015. u32 min_rate;
  2016. u32 ch_rate;
  2017. int i, ret;
  2018. ch_rate = netdev_get_tx_queue(ndev, queue)->tx_maxrate;
  2019. if (ch_rate == rate)
  2020. return 0;
  2021. ch_rate = rate * 1000;
  2022. min_rate = cpdma_chan_get_min_rate(cpsw->dma);
  2023. if ((ch_rate < min_rate && ch_rate)) {
  2024. dev_err(priv->dev, "The channel rate cannot be less than %dMbps",
  2025. min_rate);
  2026. return -EINVAL;
  2027. }
  2028. if (rate > cpsw->speed) {
  2029. dev_err(priv->dev, "The channel rate cannot be more than 2Gbps");
  2030. return -EINVAL;
  2031. }
  2032. ret = pm_runtime_get_sync(cpsw->dev);
  2033. if (ret < 0) {
  2034. pm_runtime_put_noidle(cpsw->dev);
  2035. return ret;
  2036. }
  2037. ret = cpdma_chan_set_rate(cpsw->txv[queue].ch, ch_rate);
  2038. pm_runtime_put(cpsw->dev);
  2039. if (ret)
  2040. return ret;
  2041. /* update rates for slaves tx queues */
  2042. for (i = 0; i < cpsw->data.slaves; i++) {
  2043. slave = &cpsw->slaves[i];
  2044. if (!slave->ndev)
  2045. continue;
  2046. netdev_get_tx_queue(slave->ndev, queue)->tx_maxrate = rate;
  2047. }
  2048. cpsw_split_res(ndev);
  2049. return ret;
  2050. }
  2051. static int cpsw_set_mqprio(struct net_device *ndev, void *type_data)
  2052. {
  2053. struct tc_mqprio_qopt_offload *mqprio = type_data;
  2054. struct cpsw_priv *priv = netdev_priv(ndev);
  2055. struct cpsw_common *cpsw = priv->cpsw;
  2056. int fifo, num_tc, count, offset;
  2057. struct cpsw_slave *slave;
  2058. u32 tx_prio_map = 0;
  2059. int i, tc, ret;
  2060. num_tc = mqprio->qopt.num_tc;
  2061. if (num_tc > CPSW_TC_NUM)
  2062. return -EINVAL;
  2063. if (mqprio->mode != TC_MQPRIO_MODE_DCB)
  2064. return -EINVAL;
  2065. ret = pm_runtime_get_sync(cpsw->dev);
  2066. if (ret < 0) {
  2067. pm_runtime_put_noidle(cpsw->dev);
  2068. return ret;
  2069. }
  2070. if (num_tc) {
  2071. for (i = 0; i < 8; i++) {
  2072. tc = mqprio->qopt.prio_tc_map[i];
  2073. fifo = cpsw_tc_to_fifo(tc, num_tc);
  2074. tx_prio_map |= fifo << (4 * i);
  2075. }
  2076. netdev_set_num_tc(ndev, num_tc);
  2077. for (i = 0; i < num_tc; i++) {
  2078. count = mqprio->qopt.count[i];
  2079. offset = mqprio->qopt.offset[i];
  2080. netdev_set_tc_queue(ndev, i, count, offset);
  2081. }
  2082. }
  2083. if (!mqprio->qopt.hw) {
  2084. /* restore default configuration */
  2085. netdev_reset_tc(ndev);
  2086. tx_prio_map = TX_PRIORITY_MAPPING;
  2087. }
  2088. priv->mqprio_hw = mqprio->qopt.hw;
  2089. offset = cpsw->version == CPSW_VERSION_1 ?
  2090. CPSW1_TX_PRI_MAP : CPSW2_TX_PRI_MAP;
  2091. slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
  2092. slave_write(slave, tx_prio_map, offset);
  2093. pm_runtime_put_sync(cpsw->dev);
  2094. return 0;
  2095. }
  2096. static int cpsw_ndo_setup_tc(struct net_device *ndev, enum tc_setup_type type,
  2097. void *type_data)
  2098. {
  2099. switch (type) {
  2100. case TC_SETUP_QDISC_CBS:
  2101. return cpsw_set_cbs(ndev, type_data);
  2102. case TC_SETUP_QDISC_MQPRIO:
  2103. return cpsw_set_mqprio(ndev, type_data);
  2104. default:
  2105. return -EOPNOTSUPP;
  2106. }
  2107. }
  2108. static const struct net_device_ops cpsw_netdev_ops = {
  2109. .ndo_open = cpsw_ndo_open,
  2110. .ndo_stop = cpsw_ndo_stop,
  2111. .ndo_start_xmit = cpsw_ndo_start_xmit,
  2112. .ndo_set_mac_address = cpsw_ndo_set_mac_address,
  2113. .ndo_do_ioctl = cpsw_ndo_ioctl,
  2114. .ndo_validate_addr = eth_validate_addr,
  2115. .ndo_tx_timeout = cpsw_ndo_tx_timeout,
  2116. .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
  2117. .ndo_set_tx_maxrate = cpsw_ndo_set_tx_maxrate,
  2118. #ifdef CONFIG_NET_POLL_CONTROLLER
  2119. .ndo_poll_controller = cpsw_ndo_poll_controller,
  2120. #endif
  2121. .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid,
  2122. .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid,
  2123. .ndo_setup_tc = cpsw_ndo_setup_tc,
  2124. };
  2125. static int cpsw_get_regs_len(struct net_device *ndev)
  2126. {
  2127. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  2128. return cpsw->data.ale_entries * ALE_ENTRY_WORDS * sizeof(u32);
  2129. }
  2130. static void cpsw_get_regs(struct net_device *ndev,
  2131. struct ethtool_regs *regs, void *p)
  2132. {
  2133. u32 *reg = p;
  2134. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  2135. /* update CPSW IP version */
  2136. regs->version = cpsw->version;
  2137. cpsw_ale_dump(cpsw->ale, reg);
  2138. }
  2139. static void cpsw_get_drvinfo(struct net_device *ndev,
  2140. struct ethtool_drvinfo *info)
  2141. {
  2142. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  2143. struct platform_device *pdev = to_platform_device(cpsw->dev);
  2144. strlcpy(info->driver, "cpsw", sizeof(info->driver));
  2145. strlcpy(info->version, "1.0", sizeof(info->version));
  2146. strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
  2147. }
  2148. static u32 cpsw_get_msglevel(struct net_device *ndev)
  2149. {
  2150. struct cpsw_priv *priv = netdev_priv(ndev);
  2151. return priv->msg_enable;
  2152. }
  2153. static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
  2154. {
  2155. struct cpsw_priv *priv = netdev_priv(ndev);
  2156. priv->msg_enable = value;
  2157. }
  2158. #if IS_ENABLED(CONFIG_TI_CPTS)
  2159. static int cpsw_get_ts_info(struct net_device *ndev,
  2160. struct ethtool_ts_info *info)
  2161. {
  2162. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  2163. info->so_timestamping =
  2164. SOF_TIMESTAMPING_TX_HARDWARE |
  2165. SOF_TIMESTAMPING_TX_SOFTWARE |
  2166. SOF_TIMESTAMPING_RX_HARDWARE |
  2167. SOF_TIMESTAMPING_RX_SOFTWARE |
  2168. SOF_TIMESTAMPING_SOFTWARE |
  2169. SOF_TIMESTAMPING_RAW_HARDWARE;
  2170. info->phc_index = cpsw->cpts->phc_index;
  2171. info->tx_types =
  2172. (1 << HWTSTAMP_TX_OFF) |
  2173. (1 << HWTSTAMP_TX_ON);
  2174. info->rx_filters =
  2175. (1 << HWTSTAMP_FILTER_NONE) |
  2176. (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
  2177. (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
  2178. return 0;
  2179. }
  2180. #else
  2181. static int cpsw_get_ts_info(struct net_device *ndev,
  2182. struct ethtool_ts_info *info)
  2183. {
  2184. info->so_timestamping =
  2185. SOF_TIMESTAMPING_TX_SOFTWARE |
  2186. SOF_TIMESTAMPING_RX_SOFTWARE |
  2187. SOF_TIMESTAMPING_SOFTWARE;
  2188. info->phc_index = -1;
  2189. info->tx_types = 0;
  2190. info->rx_filters = 0;
  2191. return 0;
  2192. }
  2193. #endif
  2194. static int cpsw_get_link_ksettings(struct net_device *ndev,
  2195. struct ethtool_link_ksettings *ecmd)
  2196. {
  2197. struct cpsw_priv *priv = netdev_priv(ndev);
  2198. struct cpsw_common *cpsw = priv->cpsw;
  2199. int slave_no = cpsw_slave_index(cpsw, priv);
  2200. if (!cpsw->slaves[slave_no].phy)
  2201. return -EOPNOTSUPP;
  2202. phy_ethtool_ksettings_get(cpsw->slaves[slave_no].phy, ecmd);
  2203. return 0;
  2204. }
  2205. static int cpsw_set_link_ksettings(struct net_device *ndev,
  2206. const struct ethtool_link_ksettings *ecmd)
  2207. {
  2208. struct cpsw_priv *priv = netdev_priv(ndev);
  2209. struct cpsw_common *cpsw = priv->cpsw;
  2210. int slave_no = cpsw_slave_index(cpsw, priv);
  2211. if (cpsw->slaves[slave_no].phy)
  2212. return phy_ethtool_ksettings_set(cpsw->slaves[slave_no].phy,
  2213. ecmd);
  2214. else
  2215. return -EOPNOTSUPP;
  2216. }
  2217. static void cpsw_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  2218. {
  2219. struct cpsw_priv *priv = netdev_priv(ndev);
  2220. struct cpsw_common *cpsw = priv->cpsw;
  2221. int slave_no = cpsw_slave_index(cpsw, priv);
  2222. wol->supported = 0;
  2223. wol->wolopts = 0;
  2224. if (cpsw->slaves[slave_no].phy)
  2225. phy_ethtool_get_wol(cpsw->slaves[slave_no].phy, wol);
  2226. }
  2227. static int cpsw_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  2228. {
  2229. struct cpsw_priv *priv = netdev_priv(ndev);
  2230. struct cpsw_common *cpsw = priv->cpsw;
  2231. int slave_no = cpsw_slave_index(cpsw, priv);
  2232. if (cpsw->slaves[slave_no].phy)
  2233. return phy_ethtool_set_wol(cpsw->slaves[slave_no].phy, wol);
  2234. else
  2235. return -EOPNOTSUPP;
  2236. }
  2237. static void cpsw_get_pauseparam(struct net_device *ndev,
  2238. struct ethtool_pauseparam *pause)
  2239. {
  2240. struct cpsw_priv *priv = netdev_priv(ndev);
  2241. pause->autoneg = AUTONEG_DISABLE;
  2242. pause->rx_pause = priv->rx_pause ? true : false;
  2243. pause->tx_pause = priv->tx_pause ? true : false;
  2244. }
  2245. static int cpsw_set_pauseparam(struct net_device *ndev,
  2246. struct ethtool_pauseparam *pause)
  2247. {
  2248. struct cpsw_priv *priv = netdev_priv(ndev);
  2249. bool link;
  2250. priv->rx_pause = pause->rx_pause ? true : false;
  2251. priv->tx_pause = pause->tx_pause ? true : false;
  2252. for_each_slave(priv, _cpsw_adjust_link, priv, &link);
  2253. return 0;
  2254. }
  2255. static int cpsw_ethtool_op_begin(struct net_device *ndev)
  2256. {
  2257. struct cpsw_priv *priv = netdev_priv(ndev);
  2258. struct cpsw_common *cpsw = priv->cpsw;
  2259. int ret;
  2260. ret = pm_runtime_get_sync(cpsw->dev);
  2261. if (ret < 0) {
  2262. cpsw_err(priv, drv, "ethtool begin failed %d\n", ret);
  2263. pm_runtime_put_noidle(cpsw->dev);
  2264. }
  2265. return ret;
  2266. }
  2267. static void cpsw_ethtool_op_complete(struct net_device *ndev)
  2268. {
  2269. struct cpsw_priv *priv = netdev_priv(ndev);
  2270. int ret;
  2271. ret = pm_runtime_put(priv->cpsw->dev);
  2272. if (ret < 0)
  2273. cpsw_err(priv, drv, "ethtool complete failed %d\n", ret);
  2274. }
  2275. static void cpsw_get_channels(struct net_device *ndev,
  2276. struct ethtool_channels *ch)
  2277. {
  2278. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  2279. ch->max_rx = cpsw->quirk_irq ? 1 : CPSW_MAX_QUEUES;
  2280. ch->max_tx = cpsw->quirk_irq ? 1 : CPSW_MAX_QUEUES;
  2281. ch->max_combined = 0;
  2282. ch->max_other = 0;
  2283. ch->other_count = 0;
  2284. ch->rx_count = cpsw->rx_ch_num;
  2285. ch->tx_count = cpsw->tx_ch_num;
  2286. ch->combined_count = 0;
  2287. }
  2288. static int cpsw_check_ch_settings(struct cpsw_common *cpsw,
  2289. struct ethtool_channels *ch)
  2290. {
  2291. if (cpsw->quirk_irq) {
  2292. dev_err(cpsw->dev, "Maximum one tx/rx queue is allowed");
  2293. return -EOPNOTSUPP;
  2294. }
  2295. if (ch->combined_count)
  2296. return -EINVAL;
  2297. /* verify we have at least one channel in each direction */
  2298. if (!ch->rx_count || !ch->tx_count)
  2299. return -EINVAL;
  2300. if (ch->rx_count > cpsw->data.channels ||
  2301. ch->tx_count > cpsw->data.channels)
  2302. return -EINVAL;
  2303. return 0;
  2304. }
  2305. static int cpsw_update_channels_res(struct cpsw_priv *priv, int ch_num, int rx)
  2306. {
  2307. struct cpsw_common *cpsw = priv->cpsw;
  2308. void (*handler)(void *, int, int);
  2309. struct netdev_queue *queue;
  2310. struct cpsw_vector *vec;
  2311. int ret, *ch, vch;
  2312. if (rx) {
  2313. ch = &cpsw->rx_ch_num;
  2314. vec = cpsw->rxv;
  2315. handler = cpsw_rx_handler;
  2316. } else {
  2317. ch = &cpsw->tx_ch_num;
  2318. vec = cpsw->txv;
  2319. handler = cpsw_tx_handler;
  2320. }
  2321. while (*ch < ch_num) {
  2322. vch = rx ? *ch : 7 - *ch;
  2323. vec[*ch].ch = cpdma_chan_create(cpsw->dma, vch, handler, rx);
  2324. queue = netdev_get_tx_queue(priv->ndev, *ch);
  2325. queue->tx_maxrate = 0;
  2326. if (IS_ERR(vec[*ch].ch))
  2327. return PTR_ERR(vec[*ch].ch);
  2328. if (!vec[*ch].ch)
  2329. return -EINVAL;
  2330. cpsw_info(priv, ifup, "created new %d %s channel\n", *ch,
  2331. (rx ? "rx" : "tx"));
  2332. (*ch)++;
  2333. }
  2334. while (*ch > ch_num) {
  2335. (*ch)--;
  2336. ret = cpdma_chan_destroy(vec[*ch].ch);
  2337. if (ret)
  2338. return ret;
  2339. cpsw_info(priv, ifup, "destroyed %d %s channel\n", *ch,
  2340. (rx ? "rx" : "tx"));
  2341. }
  2342. return 0;
  2343. }
  2344. static int cpsw_update_channels(struct cpsw_priv *priv,
  2345. struct ethtool_channels *ch)
  2346. {
  2347. int ret;
  2348. ret = cpsw_update_channels_res(priv, ch->rx_count, 1);
  2349. if (ret)
  2350. return ret;
  2351. ret = cpsw_update_channels_res(priv, ch->tx_count, 0);
  2352. if (ret)
  2353. return ret;
  2354. return 0;
  2355. }
  2356. static void cpsw_suspend_data_pass(struct net_device *ndev)
  2357. {
  2358. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  2359. struct cpsw_slave *slave;
  2360. int i;
  2361. /* Disable NAPI scheduling */
  2362. cpsw_intr_disable(cpsw);
  2363. /* Stop all transmit queues for every network device.
  2364. * Disable re-using rx descriptors with dormant_on.
  2365. */
  2366. for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
  2367. if (!(slave->ndev && netif_running(slave->ndev)))
  2368. continue;
  2369. netif_tx_stop_all_queues(slave->ndev);
  2370. netif_dormant_on(slave->ndev);
  2371. }
  2372. /* Handle rest of tx packets and stop cpdma channels */
  2373. cpdma_ctlr_stop(cpsw->dma);
  2374. }
  2375. static int cpsw_resume_data_pass(struct net_device *ndev)
  2376. {
  2377. struct cpsw_priv *priv = netdev_priv(ndev);
  2378. struct cpsw_common *cpsw = priv->cpsw;
  2379. struct cpsw_slave *slave;
  2380. int i, ret;
  2381. /* Allow rx packets handling */
  2382. for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++)
  2383. if (slave->ndev && netif_running(slave->ndev))
  2384. netif_dormant_off(slave->ndev);
  2385. /* After this receive is started */
  2386. if (cpsw->usage_count) {
  2387. ret = cpsw_fill_rx_channels(priv);
  2388. if (ret)
  2389. return ret;
  2390. cpdma_ctlr_start(cpsw->dma);
  2391. cpsw_intr_enable(cpsw);
  2392. }
  2393. /* Resume transmit for every affected interface */
  2394. for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++)
  2395. if (slave->ndev && netif_running(slave->ndev))
  2396. netif_tx_start_all_queues(slave->ndev);
  2397. return 0;
  2398. }
  2399. static int cpsw_set_channels(struct net_device *ndev,
  2400. struct ethtool_channels *chs)
  2401. {
  2402. struct cpsw_priv *priv = netdev_priv(ndev);
  2403. struct cpsw_common *cpsw = priv->cpsw;
  2404. struct cpsw_slave *slave;
  2405. int i, ret;
  2406. ret = cpsw_check_ch_settings(cpsw, chs);
  2407. if (ret < 0)
  2408. return ret;
  2409. cpsw_suspend_data_pass(ndev);
  2410. ret = cpsw_update_channels(priv, chs);
  2411. if (ret)
  2412. goto err;
  2413. for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
  2414. if (!(slave->ndev && netif_running(slave->ndev)))
  2415. continue;
  2416. /* Inform stack about new count of queues */
  2417. ret = netif_set_real_num_tx_queues(slave->ndev,
  2418. cpsw->tx_ch_num);
  2419. if (ret) {
  2420. dev_err(priv->dev, "cannot set real number of tx queues\n");
  2421. goto err;
  2422. }
  2423. ret = netif_set_real_num_rx_queues(slave->ndev,
  2424. cpsw->rx_ch_num);
  2425. if (ret) {
  2426. dev_err(priv->dev, "cannot set real number of rx queues\n");
  2427. goto err;
  2428. }
  2429. }
  2430. if (cpsw->usage_count)
  2431. cpsw_split_res(ndev);
  2432. ret = cpsw_resume_data_pass(ndev);
  2433. if (!ret)
  2434. return 0;
  2435. err:
  2436. dev_err(priv->dev, "cannot update channels number, closing device\n");
  2437. dev_close(ndev);
  2438. return ret;
  2439. }
  2440. static int cpsw_get_eee(struct net_device *ndev, struct ethtool_eee *edata)
  2441. {
  2442. struct cpsw_priv *priv = netdev_priv(ndev);
  2443. struct cpsw_common *cpsw = priv->cpsw;
  2444. int slave_no = cpsw_slave_index(cpsw, priv);
  2445. if (cpsw->slaves[slave_no].phy)
  2446. return phy_ethtool_get_eee(cpsw->slaves[slave_no].phy, edata);
  2447. else
  2448. return -EOPNOTSUPP;
  2449. }
  2450. static int cpsw_set_eee(struct net_device *ndev, struct ethtool_eee *edata)
  2451. {
  2452. struct cpsw_priv *priv = netdev_priv(ndev);
  2453. struct cpsw_common *cpsw = priv->cpsw;
  2454. int slave_no = cpsw_slave_index(cpsw, priv);
  2455. if (cpsw->slaves[slave_no].phy)
  2456. return phy_ethtool_set_eee(cpsw->slaves[slave_no].phy, edata);
  2457. else
  2458. return -EOPNOTSUPP;
  2459. }
  2460. static int cpsw_nway_reset(struct net_device *ndev)
  2461. {
  2462. struct cpsw_priv *priv = netdev_priv(ndev);
  2463. struct cpsw_common *cpsw = priv->cpsw;
  2464. int slave_no = cpsw_slave_index(cpsw, priv);
  2465. if (cpsw->slaves[slave_no].phy)
  2466. return genphy_restart_aneg(cpsw->slaves[slave_no].phy);
  2467. else
  2468. return -EOPNOTSUPP;
  2469. }
  2470. static void cpsw_get_ringparam(struct net_device *ndev,
  2471. struct ethtool_ringparam *ering)
  2472. {
  2473. struct cpsw_priv *priv = netdev_priv(ndev);
  2474. struct cpsw_common *cpsw = priv->cpsw;
  2475. /* not supported */
  2476. ering->tx_max_pending = descs_pool_size - CPSW_MAX_QUEUES;
  2477. ering->tx_pending = cpdma_get_num_tx_descs(cpsw->dma);
  2478. ering->rx_max_pending = descs_pool_size - CPSW_MAX_QUEUES;
  2479. ering->rx_pending = cpdma_get_num_rx_descs(cpsw->dma);
  2480. }
  2481. static int cpsw_set_ringparam(struct net_device *ndev,
  2482. struct ethtool_ringparam *ering)
  2483. {
  2484. struct cpsw_priv *priv = netdev_priv(ndev);
  2485. struct cpsw_common *cpsw = priv->cpsw;
  2486. int ret;
  2487. /* ignore ering->tx_pending - only rx_pending adjustment is supported */
  2488. if (ering->rx_mini_pending || ering->rx_jumbo_pending ||
  2489. ering->rx_pending < CPSW_MAX_QUEUES ||
  2490. ering->rx_pending > (descs_pool_size - CPSW_MAX_QUEUES))
  2491. return -EINVAL;
  2492. if (ering->rx_pending == cpdma_get_num_rx_descs(cpsw->dma))
  2493. return 0;
  2494. cpsw_suspend_data_pass(ndev);
  2495. cpdma_set_num_rx_descs(cpsw->dma, ering->rx_pending);
  2496. if (cpsw->usage_count)
  2497. cpdma_chan_split_pool(cpsw->dma);
  2498. ret = cpsw_resume_data_pass(ndev);
  2499. if (!ret)
  2500. return 0;
  2501. dev_err(&ndev->dev, "cannot set ring params, closing device\n");
  2502. dev_close(ndev);
  2503. return ret;
  2504. }
  2505. static const struct ethtool_ops cpsw_ethtool_ops = {
  2506. .get_drvinfo = cpsw_get_drvinfo,
  2507. .get_msglevel = cpsw_get_msglevel,
  2508. .set_msglevel = cpsw_set_msglevel,
  2509. .get_link = ethtool_op_get_link,
  2510. .get_ts_info = cpsw_get_ts_info,
  2511. .get_coalesce = cpsw_get_coalesce,
  2512. .set_coalesce = cpsw_set_coalesce,
  2513. .get_sset_count = cpsw_get_sset_count,
  2514. .get_strings = cpsw_get_strings,
  2515. .get_ethtool_stats = cpsw_get_ethtool_stats,
  2516. .get_pauseparam = cpsw_get_pauseparam,
  2517. .set_pauseparam = cpsw_set_pauseparam,
  2518. .get_wol = cpsw_get_wol,
  2519. .set_wol = cpsw_set_wol,
  2520. .get_regs_len = cpsw_get_regs_len,
  2521. .get_regs = cpsw_get_regs,
  2522. .begin = cpsw_ethtool_op_begin,
  2523. .complete = cpsw_ethtool_op_complete,
  2524. .get_channels = cpsw_get_channels,
  2525. .set_channels = cpsw_set_channels,
  2526. .get_link_ksettings = cpsw_get_link_ksettings,
  2527. .set_link_ksettings = cpsw_set_link_ksettings,
  2528. .get_eee = cpsw_get_eee,
  2529. .set_eee = cpsw_set_eee,
  2530. .nway_reset = cpsw_nway_reset,
  2531. .get_ringparam = cpsw_get_ringparam,
  2532. .set_ringparam = cpsw_set_ringparam,
  2533. };
  2534. static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_common *cpsw,
  2535. u32 slave_reg_ofs, u32 sliver_reg_ofs)
  2536. {
  2537. void __iomem *regs = cpsw->regs;
  2538. int slave_num = slave->slave_num;
  2539. struct cpsw_slave_data *data = cpsw->data.slave_data + slave_num;
  2540. slave->data = data;
  2541. slave->regs = regs + slave_reg_ofs;
  2542. slave->sliver = regs + sliver_reg_ofs;
  2543. slave->port_vlan = data->dual_emac_res_vlan;
  2544. }
  2545. static int cpsw_probe_dt(struct cpsw_platform_data *data,
  2546. struct platform_device *pdev)
  2547. {
  2548. struct device_node *node = pdev->dev.of_node;
  2549. struct device_node *slave_node;
  2550. int i = 0, ret;
  2551. u32 prop;
  2552. if (!node)
  2553. return -EINVAL;
  2554. if (of_property_read_u32(node, "slaves", &prop)) {
  2555. dev_err(&pdev->dev, "Missing slaves property in the DT.\n");
  2556. return -EINVAL;
  2557. }
  2558. data->slaves = prop;
  2559. if (of_property_read_u32(node, "active_slave", &prop)) {
  2560. dev_err(&pdev->dev, "Missing active_slave property in the DT.\n");
  2561. return -EINVAL;
  2562. }
  2563. data->active_slave = prop;
  2564. data->slave_data = devm_kcalloc(&pdev->dev,
  2565. data->slaves,
  2566. sizeof(struct cpsw_slave_data),
  2567. GFP_KERNEL);
  2568. if (!data->slave_data)
  2569. return -ENOMEM;
  2570. if (of_property_read_u32(node, "cpdma_channels", &prop)) {
  2571. dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n");
  2572. return -EINVAL;
  2573. }
  2574. data->channels = prop;
  2575. if (of_property_read_u32(node, "ale_entries", &prop)) {
  2576. dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n");
  2577. return -EINVAL;
  2578. }
  2579. data->ale_entries = prop;
  2580. if (of_property_read_u32(node, "bd_ram_size", &prop)) {
  2581. dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n");
  2582. return -EINVAL;
  2583. }
  2584. data->bd_ram_size = prop;
  2585. if (of_property_read_u32(node, "mac_control", &prop)) {
  2586. dev_err(&pdev->dev, "Missing mac_control property in the DT.\n");
  2587. return -EINVAL;
  2588. }
  2589. data->mac_control = prop;
  2590. if (of_property_read_bool(node, "dual_emac"))
  2591. data->dual_emac = 1;
  2592. /*
  2593. * Populate all the child nodes here...
  2594. */
  2595. ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
  2596. /* We do not want to force this, as in some cases may not have child */
  2597. if (ret)
  2598. dev_warn(&pdev->dev, "Doesn't have any child node\n");
  2599. for_each_available_child_of_node(node, slave_node) {
  2600. struct cpsw_slave_data *slave_data = data->slave_data + i;
  2601. const void *mac_addr = NULL;
  2602. int lenp;
  2603. const __be32 *parp;
  2604. /* This is no slave child node, continue */
  2605. if (strcmp(slave_node->name, "slave"))
  2606. continue;
  2607. slave_data->phy_node = of_parse_phandle(slave_node,
  2608. "phy-handle", 0);
  2609. parp = of_get_property(slave_node, "phy_id", &lenp);
  2610. if (slave_data->phy_node) {
  2611. dev_dbg(&pdev->dev,
  2612. "slave[%d] using phy-handle=\"%pOF\"\n",
  2613. i, slave_data->phy_node);
  2614. } else if (of_phy_is_fixed_link(slave_node)) {
  2615. /* In the case of a fixed PHY, the DT node associated
  2616. * to the PHY is the Ethernet MAC DT node.
  2617. */
  2618. ret = of_phy_register_fixed_link(slave_node);
  2619. if (ret) {
  2620. if (ret != -EPROBE_DEFER)
  2621. dev_err(&pdev->dev, "failed to register fixed-link phy: %d\n", ret);
  2622. return ret;
  2623. }
  2624. slave_data->phy_node = of_node_get(slave_node);
  2625. } else if (parp) {
  2626. u32 phyid;
  2627. struct device_node *mdio_node;
  2628. struct platform_device *mdio;
  2629. if (lenp != (sizeof(__be32) * 2)) {
  2630. dev_err(&pdev->dev, "Invalid slave[%d] phy_id property\n", i);
  2631. goto no_phy_slave;
  2632. }
  2633. mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
  2634. phyid = be32_to_cpup(parp+1);
  2635. mdio = of_find_device_by_node(mdio_node);
  2636. of_node_put(mdio_node);
  2637. if (!mdio) {
  2638. dev_err(&pdev->dev, "Missing mdio platform device\n");
  2639. return -EINVAL;
  2640. }
  2641. snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
  2642. PHY_ID_FMT, mdio->name, phyid);
  2643. put_device(&mdio->dev);
  2644. } else {
  2645. dev_err(&pdev->dev,
  2646. "No slave[%d] phy_id, phy-handle, or fixed-link property\n",
  2647. i);
  2648. goto no_phy_slave;
  2649. }
  2650. slave_data->phy_if = of_get_phy_mode(slave_node);
  2651. if (slave_data->phy_if < 0) {
  2652. dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n",
  2653. i);
  2654. return slave_data->phy_if;
  2655. }
  2656. no_phy_slave:
  2657. mac_addr = of_get_mac_address(slave_node);
  2658. if (mac_addr) {
  2659. memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
  2660. } else {
  2661. ret = ti_cm_get_macid(&pdev->dev, i,
  2662. slave_data->mac_addr);
  2663. if (ret)
  2664. return ret;
  2665. }
  2666. if (data->dual_emac) {
  2667. if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
  2668. &prop)) {
  2669. dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n");
  2670. slave_data->dual_emac_res_vlan = i+1;
  2671. dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n",
  2672. slave_data->dual_emac_res_vlan, i);
  2673. } else {
  2674. slave_data->dual_emac_res_vlan = prop;
  2675. }
  2676. }
  2677. i++;
  2678. if (i == data->slaves)
  2679. break;
  2680. }
  2681. return 0;
  2682. }
  2683. static void cpsw_remove_dt(struct platform_device *pdev)
  2684. {
  2685. struct net_device *ndev = platform_get_drvdata(pdev);
  2686. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  2687. struct cpsw_platform_data *data = &cpsw->data;
  2688. struct device_node *node = pdev->dev.of_node;
  2689. struct device_node *slave_node;
  2690. int i = 0;
  2691. for_each_available_child_of_node(node, slave_node) {
  2692. struct cpsw_slave_data *slave_data = &data->slave_data[i];
  2693. if (strcmp(slave_node->name, "slave"))
  2694. continue;
  2695. if (of_phy_is_fixed_link(slave_node))
  2696. of_phy_deregister_fixed_link(slave_node);
  2697. of_node_put(slave_data->phy_node);
  2698. i++;
  2699. if (i == data->slaves)
  2700. break;
  2701. }
  2702. of_platform_depopulate(&pdev->dev);
  2703. }
  2704. static int cpsw_probe_dual_emac(struct cpsw_priv *priv)
  2705. {
  2706. struct cpsw_common *cpsw = priv->cpsw;
  2707. struct cpsw_platform_data *data = &cpsw->data;
  2708. struct net_device *ndev;
  2709. struct cpsw_priv *priv_sl2;
  2710. int ret = 0;
  2711. ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
  2712. if (!ndev) {
  2713. dev_err(cpsw->dev, "cpsw: error allocating net_device\n");
  2714. return -ENOMEM;
  2715. }
  2716. priv_sl2 = netdev_priv(ndev);
  2717. priv_sl2->cpsw = cpsw;
  2718. priv_sl2->ndev = ndev;
  2719. priv_sl2->dev = &ndev->dev;
  2720. priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
  2721. if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
  2722. memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
  2723. ETH_ALEN);
  2724. dev_info(cpsw->dev, "cpsw: Detected MACID = %pM\n",
  2725. priv_sl2->mac_addr);
  2726. } else {
  2727. eth_random_addr(priv_sl2->mac_addr);
  2728. dev_info(cpsw->dev, "cpsw: Random MACID = %pM\n",
  2729. priv_sl2->mac_addr);
  2730. }
  2731. memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
  2732. priv_sl2->emac_port = 1;
  2733. cpsw->slaves[1].ndev = ndev;
  2734. ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX;
  2735. ndev->netdev_ops = &cpsw_netdev_ops;
  2736. ndev->ethtool_ops = &cpsw_ethtool_ops;
  2737. /* register the network device */
  2738. SET_NETDEV_DEV(ndev, cpsw->dev);
  2739. ret = register_netdev(ndev);
  2740. if (ret) {
  2741. dev_err(cpsw->dev, "cpsw: error registering net device\n");
  2742. free_netdev(ndev);
  2743. ret = -ENODEV;
  2744. }
  2745. return ret;
  2746. }
  2747. static const struct of_device_id cpsw_of_mtable[] = {
  2748. { .compatible = "ti,cpsw"},
  2749. { .compatible = "ti,am335x-cpsw"},
  2750. { .compatible = "ti,am4372-cpsw"},
  2751. { .compatible = "ti,dra7-cpsw"},
  2752. { /* sentinel */ },
  2753. };
  2754. MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
  2755. static const struct soc_device_attribute cpsw_soc_devices[] = {
  2756. { .family = "AM33xx", .revision = "ES1.0"},
  2757. { /* sentinel */ }
  2758. };
  2759. static int cpsw_probe(struct platform_device *pdev)
  2760. {
  2761. struct clk *clk;
  2762. struct cpsw_platform_data *data;
  2763. struct net_device *ndev;
  2764. struct cpsw_priv *priv;
  2765. struct cpdma_params dma_params;
  2766. struct cpsw_ale_params ale_params;
  2767. void __iomem *ss_regs;
  2768. void __iomem *cpts_regs;
  2769. struct resource *res, *ss_res;
  2770. struct gpio_descs *mode;
  2771. u32 slave_offset, sliver_offset, slave_size;
  2772. const struct soc_device_attribute *soc;
  2773. struct cpsw_common *cpsw;
  2774. int ret = 0, i, ch;
  2775. int irq;
  2776. cpsw = devm_kzalloc(&pdev->dev, sizeof(struct cpsw_common), GFP_KERNEL);
  2777. if (!cpsw)
  2778. return -ENOMEM;
  2779. cpsw->dev = &pdev->dev;
  2780. ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
  2781. if (!ndev) {
  2782. dev_err(&pdev->dev, "error allocating net_device\n");
  2783. return -ENOMEM;
  2784. }
  2785. platform_set_drvdata(pdev, ndev);
  2786. priv = netdev_priv(ndev);
  2787. priv->cpsw = cpsw;
  2788. priv->ndev = ndev;
  2789. priv->dev = &ndev->dev;
  2790. priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
  2791. cpsw->rx_packet_max = max(rx_packet_max, 128);
  2792. mode = devm_gpiod_get_array_optional(&pdev->dev, "mode", GPIOD_OUT_LOW);
  2793. if (IS_ERR(mode)) {
  2794. ret = PTR_ERR(mode);
  2795. dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
  2796. goto clean_ndev_ret;
  2797. }
  2798. /*
  2799. * This may be required here for child devices.
  2800. */
  2801. pm_runtime_enable(&pdev->dev);
  2802. /* Select default pin state */
  2803. pinctrl_pm_select_default_state(&pdev->dev);
  2804. /* Need to enable clocks with runtime PM api to access module
  2805. * registers
  2806. */
  2807. ret = pm_runtime_get_sync(&pdev->dev);
  2808. if (ret < 0) {
  2809. pm_runtime_put_noidle(&pdev->dev);
  2810. goto clean_runtime_disable_ret;
  2811. }
  2812. ret = cpsw_probe_dt(&cpsw->data, pdev);
  2813. if (ret)
  2814. goto clean_dt_ret;
  2815. data = &cpsw->data;
  2816. cpsw->rx_ch_num = 1;
  2817. cpsw->tx_ch_num = 1;
  2818. if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
  2819. memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
  2820. dev_info(&pdev->dev, "Detected MACID = %pM\n", priv->mac_addr);
  2821. } else {
  2822. eth_random_addr(priv->mac_addr);
  2823. dev_info(&pdev->dev, "Random MACID = %pM\n", priv->mac_addr);
  2824. }
  2825. memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
  2826. cpsw->slaves = devm_kcalloc(&pdev->dev,
  2827. data->slaves, sizeof(struct cpsw_slave),
  2828. GFP_KERNEL);
  2829. if (!cpsw->slaves) {
  2830. ret = -ENOMEM;
  2831. goto clean_dt_ret;
  2832. }
  2833. for (i = 0; i < data->slaves; i++)
  2834. cpsw->slaves[i].slave_num = i;
  2835. cpsw->slaves[0].ndev = ndev;
  2836. priv->emac_port = 0;
  2837. clk = devm_clk_get(&pdev->dev, "fck");
  2838. if (IS_ERR(clk)) {
  2839. dev_err(priv->dev, "fck is not found\n");
  2840. ret = -ENODEV;
  2841. goto clean_dt_ret;
  2842. }
  2843. cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
  2844. ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2845. ss_regs = devm_ioremap_resource(&pdev->dev, ss_res);
  2846. if (IS_ERR(ss_regs)) {
  2847. ret = PTR_ERR(ss_regs);
  2848. goto clean_dt_ret;
  2849. }
  2850. cpsw->regs = ss_regs;
  2851. cpsw->version = readl(&cpsw->regs->id_ver);
  2852. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  2853. cpsw->wr_regs = devm_ioremap_resource(&pdev->dev, res);
  2854. if (IS_ERR(cpsw->wr_regs)) {
  2855. ret = PTR_ERR(cpsw->wr_regs);
  2856. goto clean_dt_ret;
  2857. }
  2858. memset(&dma_params, 0, sizeof(dma_params));
  2859. memset(&ale_params, 0, sizeof(ale_params));
  2860. switch (cpsw->version) {
  2861. case CPSW_VERSION_1:
  2862. cpsw->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
  2863. cpts_regs = ss_regs + CPSW1_CPTS_OFFSET;
  2864. cpsw->hw_stats = ss_regs + CPSW1_HW_STATS;
  2865. dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
  2866. dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
  2867. ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
  2868. slave_offset = CPSW1_SLAVE_OFFSET;
  2869. slave_size = CPSW1_SLAVE_SIZE;
  2870. sliver_offset = CPSW1_SLIVER_OFFSET;
  2871. dma_params.desc_mem_phys = 0;
  2872. break;
  2873. case CPSW_VERSION_2:
  2874. case CPSW_VERSION_3:
  2875. case CPSW_VERSION_4:
  2876. cpsw->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
  2877. cpts_regs = ss_regs + CPSW2_CPTS_OFFSET;
  2878. cpsw->hw_stats = ss_regs + CPSW2_HW_STATS;
  2879. dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
  2880. dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
  2881. ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
  2882. slave_offset = CPSW2_SLAVE_OFFSET;
  2883. slave_size = CPSW2_SLAVE_SIZE;
  2884. sliver_offset = CPSW2_SLIVER_OFFSET;
  2885. dma_params.desc_mem_phys =
  2886. (u32 __force) ss_res->start + CPSW2_BD_OFFSET;
  2887. break;
  2888. default:
  2889. dev_err(priv->dev, "unknown version 0x%08x\n", cpsw->version);
  2890. ret = -ENODEV;
  2891. goto clean_dt_ret;
  2892. }
  2893. for (i = 0; i < cpsw->data.slaves; i++) {
  2894. struct cpsw_slave *slave = &cpsw->slaves[i];
  2895. cpsw_slave_init(slave, cpsw, slave_offset, sliver_offset);
  2896. slave_offset += slave_size;
  2897. sliver_offset += SLIVER_SIZE;
  2898. }
  2899. dma_params.dev = &pdev->dev;
  2900. dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
  2901. dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
  2902. dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
  2903. dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
  2904. dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
  2905. dma_params.num_chan = data->channels;
  2906. dma_params.has_soft_reset = true;
  2907. dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
  2908. dma_params.desc_mem_size = data->bd_ram_size;
  2909. dma_params.desc_align = 16;
  2910. dma_params.has_ext_regs = true;
  2911. dma_params.desc_hw_addr = dma_params.desc_mem_phys;
  2912. dma_params.bus_freq_mhz = cpsw->bus_freq_mhz;
  2913. dma_params.descs_pool_size = descs_pool_size;
  2914. cpsw->dma = cpdma_ctlr_create(&dma_params);
  2915. if (!cpsw->dma) {
  2916. dev_err(priv->dev, "error initializing dma\n");
  2917. ret = -ENOMEM;
  2918. goto clean_dt_ret;
  2919. }
  2920. soc = soc_device_match(cpsw_soc_devices);
  2921. if (soc)
  2922. cpsw->quirk_irq = 1;
  2923. ch = cpsw->quirk_irq ? 0 : 7;
  2924. cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, ch, cpsw_tx_handler, 0);
  2925. if (IS_ERR(cpsw->txv[0].ch)) {
  2926. dev_err(priv->dev, "error initializing tx dma channel\n");
  2927. ret = PTR_ERR(cpsw->txv[0].ch);
  2928. goto clean_dma_ret;
  2929. }
  2930. cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1);
  2931. if (IS_ERR(cpsw->rxv[0].ch)) {
  2932. dev_err(priv->dev, "error initializing rx dma channel\n");
  2933. ret = PTR_ERR(cpsw->rxv[0].ch);
  2934. goto clean_dma_ret;
  2935. }
  2936. ale_params.dev = &pdev->dev;
  2937. ale_params.ale_ageout = ale_ageout;
  2938. ale_params.ale_entries = data->ale_entries;
  2939. ale_params.ale_ports = CPSW_ALE_PORTS_NUM;
  2940. cpsw->ale = cpsw_ale_create(&ale_params);
  2941. if (!cpsw->ale) {
  2942. dev_err(priv->dev, "error initializing ale engine\n");
  2943. ret = -ENODEV;
  2944. goto clean_dma_ret;
  2945. }
  2946. cpsw->cpts = cpts_create(cpsw->dev, cpts_regs, cpsw->dev->of_node);
  2947. if (IS_ERR(cpsw->cpts)) {
  2948. ret = PTR_ERR(cpsw->cpts);
  2949. goto clean_dma_ret;
  2950. }
  2951. ndev->irq = platform_get_irq(pdev, 1);
  2952. if (ndev->irq < 0) {
  2953. dev_err(priv->dev, "error getting irq resource\n");
  2954. ret = ndev->irq;
  2955. goto clean_dma_ret;
  2956. }
  2957. ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX;
  2958. ndev->netdev_ops = &cpsw_netdev_ops;
  2959. ndev->ethtool_ops = &cpsw_ethtool_ops;
  2960. netif_napi_add(ndev, &cpsw->napi_rx,
  2961. cpsw->quirk_irq ? cpsw_rx_poll : cpsw_rx_mq_poll,
  2962. CPSW_POLL_WEIGHT);
  2963. netif_tx_napi_add(ndev, &cpsw->napi_tx,
  2964. cpsw->quirk_irq ? cpsw_tx_poll : cpsw_tx_mq_poll,
  2965. CPSW_POLL_WEIGHT);
  2966. cpsw_split_res(ndev);
  2967. /* register the network device */
  2968. SET_NETDEV_DEV(ndev, &pdev->dev);
  2969. ret = register_netdev(ndev);
  2970. if (ret) {
  2971. dev_err(priv->dev, "error registering net device\n");
  2972. ret = -ENODEV;
  2973. goto clean_dma_ret;
  2974. }
  2975. if (cpsw->data.dual_emac) {
  2976. ret = cpsw_probe_dual_emac(priv);
  2977. if (ret) {
  2978. cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
  2979. goto clean_unregister_netdev_ret;
  2980. }
  2981. }
  2982. /* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
  2983. * MISC IRQs which are always kept disabled with this driver so
  2984. * we will not request them.
  2985. *
  2986. * If anyone wants to implement support for those, make sure to
  2987. * first request and append them to irqs_table array.
  2988. */
  2989. /* RX IRQ */
  2990. irq = platform_get_irq(pdev, 1);
  2991. if (irq < 0) {
  2992. ret = irq;
  2993. goto clean_dma_ret;
  2994. }
  2995. cpsw->irqs_table[0] = irq;
  2996. ret = devm_request_irq(&pdev->dev, irq, cpsw_rx_interrupt,
  2997. 0, dev_name(&pdev->dev), cpsw);
  2998. if (ret < 0) {
  2999. dev_err(priv->dev, "error attaching irq (%d)\n", ret);
  3000. goto clean_dma_ret;
  3001. }
  3002. /* TX IRQ */
  3003. irq = platform_get_irq(pdev, 2);
  3004. if (irq < 0) {
  3005. ret = irq;
  3006. goto clean_dma_ret;
  3007. }
  3008. cpsw->irqs_table[1] = irq;
  3009. ret = devm_request_irq(&pdev->dev, irq, cpsw_tx_interrupt,
  3010. 0, dev_name(&pdev->dev), cpsw);
  3011. if (ret < 0) {
  3012. dev_err(priv->dev, "error attaching irq (%d)\n", ret);
  3013. goto clean_dma_ret;
  3014. }
  3015. cpsw_notice(priv, probe,
  3016. "initialized device (regs %pa, irq %d, pool size %d)\n",
  3017. &ss_res->start, ndev->irq, dma_params.descs_pool_size);
  3018. pm_runtime_put(&pdev->dev);
  3019. return 0;
  3020. clean_unregister_netdev_ret:
  3021. unregister_netdev(ndev);
  3022. clean_dma_ret:
  3023. cpdma_ctlr_destroy(cpsw->dma);
  3024. clean_dt_ret:
  3025. cpsw_remove_dt(pdev);
  3026. pm_runtime_put_sync(&pdev->dev);
  3027. clean_runtime_disable_ret:
  3028. pm_runtime_disable(&pdev->dev);
  3029. clean_ndev_ret:
  3030. free_netdev(priv->ndev);
  3031. return ret;
  3032. }
  3033. static int cpsw_remove(struct platform_device *pdev)
  3034. {
  3035. struct net_device *ndev = platform_get_drvdata(pdev);
  3036. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  3037. int ret;
  3038. ret = pm_runtime_get_sync(&pdev->dev);
  3039. if (ret < 0) {
  3040. pm_runtime_put_noidle(&pdev->dev);
  3041. return ret;
  3042. }
  3043. if (cpsw->data.dual_emac)
  3044. unregister_netdev(cpsw->slaves[1].ndev);
  3045. unregister_netdev(ndev);
  3046. cpts_release(cpsw->cpts);
  3047. cpdma_ctlr_destroy(cpsw->dma);
  3048. cpsw_remove_dt(pdev);
  3049. pm_runtime_put_sync(&pdev->dev);
  3050. pm_runtime_disable(&pdev->dev);
  3051. if (cpsw->data.dual_emac)
  3052. free_netdev(cpsw->slaves[1].ndev);
  3053. free_netdev(ndev);
  3054. return 0;
  3055. }
  3056. #ifdef CONFIG_PM_SLEEP
  3057. static int cpsw_suspend(struct device *dev)
  3058. {
  3059. struct platform_device *pdev = to_platform_device(dev);
  3060. struct net_device *ndev = platform_get_drvdata(pdev);
  3061. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  3062. if (cpsw->data.dual_emac) {
  3063. int i;
  3064. for (i = 0; i < cpsw->data.slaves; i++) {
  3065. if (netif_running(cpsw->slaves[i].ndev))
  3066. cpsw_ndo_stop(cpsw->slaves[i].ndev);
  3067. }
  3068. } else {
  3069. if (netif_running(ndev))
  3070. cpsw_ndo_stop(ndev);
  3071. }
  3072. /* Select sleep pin state */
  3073. pinctrl_pm_select_sleep_state(dev);
  3074. return 0;
  3075. }
  3076. static int cpsw_resume(struct device *dev)
  3077. {
  3078. struct platform_device *pdev = to_platform_device(dev);
  3079. struct net_device *ndev = platform_get_drvdata(pdev);
  3080. struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  3081. /* Select default pin state */
  3082. pinctrl_pm_select_default_state(dev);
  3083. /* shut up ASSERT_RTNL() warning in netif_set_real_num_tx/rx_queues */
  3084. rtnl_lock();
  3085. if (cpsw->data.dual_emac) {
  3086. int i;
  3087. for (i = 0; i < cpsw->data.slaves; i++) {
  3088. if (netif_running(cpsw->slaves[i].ndev))
  3089. cpsw_ndo_open(cpsw->slaves[i].ndev);
  3090. }
  3091. } else {
  3092. if (netif_running(ndev))
  3093. cpsw_ndo_open(ndev);
  3094. }
  3095. rtnl_unlock();
  3096. return 0;
  3097. }
  3098. #endif
  3099. static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
  3100. static struct platform_driver cpsw_driver = {
  3101. .driver = {
  3102. .name = "cpsw",
  3103. .pm = &cpsw_pm_ops,
  3104. .of_match_table = cpsw_of_mtable,
  3105. },
  3106. .probe = cpsw_probe,
  3107. .remove = cpsw_remove,
  3108. };
  3109. module_platform_driver(cpsw_driver);
  3110. MODULE_LICENSE("GPL");
  3111. MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
  3112. MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
  3113. MODULE_DESCRIPTION("TI CPSW Ethernet driver");