yt921x.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for Motorcomm YT921x Switch
  4. *
  5. * Should work on YT9213/YT9214/YT9215/YT9218, but only tested on YT9215+SGMII,
  6. * be sure to do your own checks before porting to another chip.
  7. *
  8. * Copyright (c) 2025 David Yang
  9. */
  10. #include <linux/dcbnl.h>
  11. #include <linux/etherdevice.h>
  12. #include <linux/if_bridge.h>
  13. #include <linux/if_hsr.h>
  14. #include <linux/if_vlan.h>
  15. #include <linux/iopoll.h>
  16. #include <linux/mdio.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_mdio.h>
  20. #include <linux/of_net.h>
  21. #include <linux/sort.h>
  22. #include <net/dsa.h>
  23. #include <net/dscp.h>
  24. #include <net/ieee8021q.h>
  25. #include "yt921x.h"
  26. struct yt921x_mib_desc {
  27. unsigned int size;
  28. unsigned int offset;
  29. const char *name;
  30. };
  31. #define MIB_DESC(_size, _offset, _name) \
  32. {_size, _offset, _name}
  33. /* Must agree with yt921x_mib
  34. *
  35. * Unstructured fields (name != NULL) will appear in get_ethtool_stats(),
  36. * structured go to their *_stats() methods, but we need their sizes and offsets
  37. * to perform 32bit MIB overflow wraparound.
  38. */
  39. static const struct yt921x_mib_desc yt921x_mib_descs[] = {
  40. MIB_DESC(1, YT921X_MIB_DATA_RX_BROADCAST, NULL),
  41. MIB_DESC(1, YT921X_MIB_DATA_RX_PAUSE, NULL),
  42. MIB_DESC(1, YT921X_MIB_DATA_RX_MULTICAST, NULL),
  43. MIB_DESC(1, YT921X_MIB_DATA_RX_CRC_ERR, NULL),
  44. MIB_DESC(1, YT921X_MIB_DATA_RX_ALIGN_ERR, NULL),
  45. MIB_DESC(1, YT921X_MIB_DATA_RX_UNDERSIZE_ERR, NULL),
  46. MIB_DESC(1, YT921X_MIB_DATA_RX_FRAG_ERR, NULL),
  47. MIB_DESC(1, YT921X_MIB_DATA_RX_PKT_SZ_64, NULL),
  48. MIB_DESC(1, YT921X_MIB_DATA_RX_PKT_SZ_65_TO_127, NULL),
  49. MIB_DESC(1, YT921X_MIB_DATA_RX_PKT_SZ_128_TO_255, NULL),
  50. MIB_DESC(1, YT921X_MIB_DATA_RX_PKT_SZ_256_TO_511, NULL),
  51. MIB_DESC(1, YT921X_MIB_DATA_RX_PKT_SZ_512_TO_1023, NULL),
  52. MIB_DESC(1, YT921X_MIB_DATA_RX_PKT_SZ_1024_TO_1518, NULL),
  53. MIB_DESC(1, YT921X_MIB_DATA_RX_PKT_SZ_1519_TO_MAX, NULL),
  54. MIB_DESC(2, YT921X_MIB_DATA_RX_GOOD_BYTES, NULL),
  55. MIB_DESC(2, YT921X_MIB_DATA_RX_BAD_BYTES, "RxBadBytes"),
  56. MIB_DESC(1, YT921X_MIB_DATA_RX_OVERSIZE_ERR, NULL),
  57. MIB_DESC(1, YT921X_MIB_DATA_RX_DROPPED, NULL),
  58. MIB_DESC(1, YT921X_MIB_DATA_TX_BROADCAST, NULL),
  59. MIB_DESC(1, YT921X_MIB_DATA_TX_PAUSE, NULL),
  60. MIB_DESC(1, YT921X_MIB_DATA_TX_MULTICAST, NULL),
  61. MIB_DESC(1, YT921X_MIB_DATA_TX_UNDERSIZE_ERR, NULL),
  62. MIB_DESC(1, YT921X_MIB_DATA_TX_PKT_SZ_64, NULL),
  63. MIB_DESC(1, YT921X_MIB_DATA_TX_PKT_SZ_65_TO_127, NULL),
  64. MIB_DESC(1, YT921X_MIB_DATA_TX_PKT_SZ_128_TO_255, NULL),
  65. MIB_DESC(1, YT921X_MIB_DATA_TX_PKT_SZ_256_TO_511, NULL),
  66. MIB_DESC(1, YT921X_MIB_DATA_TX_PKT_SZ_512_TO_1023, NULL),
  67. MIB_DESC(1, YT921X_MIB_DATA_TX_PKT_SZ_1024_TO_1518, NULL),
  68. MIB_DESC(1, YT921X_MIB_DATA_TX_PKT_SZ_1519_TO_MAX, NULL),
  69. MIB_DESC(2, YT921X_MIB_DATA_TX_GOOD_BYTES, NULL),
  70. MIB_DESC(1, YT921X_MIB_DATA_TX_COLLISION, NULL),
  71. MIB_DESC(1, YT921X_MIB_DATA_TX_EXCESSIVE_COLLISION, NULL),
  72. MIB_DESC(1, YT921X_MIB_DATA_TX_MULTIPLE_COLLISION, NULL),
  73. MIB_DESC(1, YT921X_MIB_DATA_TX_SINGLE_COLLISION, NULL),
  74. MIB_DESC(1, YT921X_MIB_DATA_TX_PKT, NULL),
  75. MIB_DESC(1, YT921X_MIB_DATA_TX_DEFERRED, NULL),
  76. MIB_DESC(1, YT921X_MIB_DATA_TX_LATE_COLLISION, NULL),
  77. MIB_DESC(1, YT921X_MIB_DATA_RX_OAM, "RxOAM"),
  78. MIB_DESC(1, YT921X_MIB_DATA_TX_OAM, "TxOAM"),
  79. };
  80. struct yt921x_info {
  81. const char *name;
  82. u16 major;
  83. /* Unknown, seems to be plain enumeration */
  84. u8 mode;
  85. u8 extmode;
  86. /* Ports with integral GbE PHYs, not including MCU Port 10 */
  87. u16 internal_mask;
  88. /* TODO: see comments in yt921x_dsa_phylink_get_caps() */
  89. u16 external_mask;
  90. };
  91. #define YT921X_PORT_MASK_INTn(port) BIT(port)
  92. #define YT921X_PORT_MASK_INT0_n(n) GENMASK((n) - 1, 0)
  93. #define YT921X_PORT_MASK_EXT0 BIT(8)
  94. #define YT921X_PORT_MASK_EXT1 BIT(9)
  95. static const struct yt921x_info yt921x_infos[] = {
  96. {
  97. "YT9215SC", YT9215_MAJOR, 1, 0,
  98. YT921X_PORT_MASK_INT0_n(5),
  99. YT921X_PORT_MASK_EXT0 | YT921X_PORT_MASK_EXT1,
  100. },
  101. {
  102. "YT9215S", YT9215_MAJOR, 2, 0,
  103. YT921X_PORT_MASK_INT0_n(5),
  104. YT921X_PORT_MASK_EXT0 | YT921X_PORT_MASK_EXT1,
  105. },
  106. {
  107. "YT9215RB", YT9215_MAJOR, 3, 0,
  108. YT921X_PORT_MASK_INT0_n(5),
  109. YT921X_PORT_MASK_EXT0 | YT921X_PORT_MASK_EXT1,
  110. },
  111. {
  112. "YT9214NB", YT9215_MAJOR, 3, 2,
  113. YT921X_PORT_MASK_INTn(1) | YT921X_PORT_MASK_INTn(3),
  114. YT921X_PORT_MASK_EXT0 | YT921X_PORT_MASK_EXT1,
  115. },
  116. {
  117. "YT9213NB", YT9215_MAJOR, 3, 3,
  118. YT921X_PORT_MASK_INTn(1) | YT921X_PORT_MASK_INTn(3),
  119. YT921X_PORT_MASK_EXT1,
  120. },
  121. {
  122. "YT9218N", YT9218_MAJOR, 0, 0,
  123. YT921X_PORT_MASK_INT0_n(8),
  124. 0,
  125. },
  126. {
  127. "YT9218MB", YT9218_MAJOR, 1, 0,
  128. YT921X_PORT_MASK_INT0_n(8),
  129. YT921X_PORT_MASK_EXT0 | YT921X_PORT_MASK_EXT1,
  130. },
  131. {}
  132. };
  133. #define YT921X_NAME "yt921x"
  134. #define YT921X_VID_UNWARE 4095
  135. #define YT921X_POLL_SLEEP_US 10000
  136. #define YT921X_POLL_TIMEOUT_US 100000
  137. /* The interval should be small enough to avoid overflow of 32bit MIBs.
  138. *
  139. * Until we can read MIBs from stats64 call directly (i.e. sleep
  140. * there), we have to poll stats more frequently then it is actually needed.
  141. * For overflow protection, normally, 100 sec interval should have been OK.
  142. */
  143. #define YT921X_STATS_INTERVAL_JIFFIES (3 * HZ)
  144. struct yt921x_reg_mdio {
  145. struct mii_bus *bus;
  146. int addr;
  147. /* SWITCH_ID_1 / SWITCH_ID_0 of the device
  148. *
  149. * This is a way to multiplex multiple devices on the same MII phyaddr
  150. * and should be configurable in DT. However, MDIO core simply doesn't
  151. * allow multiple devices over one reg addr, so this is a fixed value
  152. * for now until a solution is found.
  153. *
  154. * Keep this because we need switchid to form MII regaddrs anyway.
  155. */
  156. unsigned char switchid;
  157. };
  158. /* TODO: SPI/I2C */
  159. #define to_yt921x_priv(_ds) container_of_const(_ds, struct yt921x_priv, ds)
  160. #define to_device(priv) ((priv)->ds.dev)
  161. static int yt921x_reg_read(struct yt921x_priv *priv, u32 reg, u32 *valp)
  162. {
  163. WARN_ON(!mutex_is_locked(&priv->reg_lock));
  164. return priv->reg_ops->read(priv->reg_ctx, reg, valp);
  165. }
  166. static int yt921x_reg_write(struct yt921x_priv *priv, u32 reg, u32 val)
  167. {
  168. WARN_ON(!mutex_is_locked(&priv->reg_lock));
  169. return priv->reg_ops->write(priv->reg_ctx, reg, val);
  170. }
  171. static int
  172. yt921x_reg_wait(struct yt921x_priv *priv, u32 reg, u32 mask, u32 *valp)
  173. {
  174. u32 val;
  175. int res;
  176. int ret;
  177. ret = read_poll_timeout(yt921x_reg_read, res,
  178. res || (val & mask) == *valp,
  179. YT921X_POLL_SLEEP_US, YT921X_POLL_TIMEOUT_US,
  180. false, priv, reg, &val);
  181. if (ret)
  182. return ret;
  183. if (res)
  184. return res;
  185. *valp = val;
  186. return 0;
  187. }
  188. static int
  189. yt921x_reg_update_bits(struct yt921x_priv *priv, u32 reg, u32 mask, u32 val)
  190. {
  191. int res;
  192. u32 v;
  193. u32 u;
  194. res = yt921x_reg_read(priv, reg, &v);
  195. if (res)
  196. return res;
  197. u = v;
  198. u &= ~mask;
  199. u |= val;
  200. if (u == v)
  201. return 0;
  202. return yt921x_reg_write(priv, reg, u);
  203. }
  204. static int yt921x_reg_set_bits(struct yt921x_priv *priv, u32 reg, u32 mask)
  205. {
  206. return yt921x_reg_update_bits(priv, reg, 0, mask);
  207. }
  208. static int yt921x_reg_clear_bits(struct yt921x_priv *priv, u32 reg, u32 mask)
  209. {
  210. return yt921x_reg_update_bits(priv, reg, mask, 0);
  211. }
  212. static int
  213. yt921x_reg_toggle_bits(struct yt921x_priv *priv, u32 reg, u32 mask, bool set)
  214. {
  215. return yt921x_reg_update_bits(priv, reg, mask, !set ? 0 : mask);
  216. }
  217. /* Some registers, like VLANn_CTRL, should always be written in 64-bit, even if
  218. * you are to write only the lower / upper 32 bits.
  219. *
  220. * There is no such restriction for reading, but we still provide 64-bit read
  221. * wrappers so that we always handle u64 values.
  222. */
  223. static int yt921x_reg64_read(struct yt921x_priv *priv, u32 reg, u64 *valp)
  224. {
  225. u32 lo;
  226. u32 hi;
  227. int res;
  228. res = yt921x_reg_read(priv, reg, &lo);
  229. if (res)
  230. return res;
  231. res = yt921x_reg_read(priv, reg + 4, &hi);
  232. if (res)
  233. return res;
  234. *valp = ((u64)hi << 32) | lo;
  235. return 0;
  236. }
  237. static int yt921x_reg64_write(struct yt921x_priv *priv, u32 reg, u64 val)
  238. {
  239. int res;
  240. res = yt921x_reg_write(priv, reg, (u32)val);
  241. if (res)
  242. return res;
  243. return yt921x_reg_write(priv, reg + 4, (u32)(val >> 32));
  244. }
  245. static int
  246. yt921x_reg64_update_bits(struct yt921x_priv *priv, u32 reg, u64 mask, u64 val)
  247. {
  248. int res;
  249. u64 v;
  250. u64 u;
  251. res = yt921x_reg64_read(priv, reg, &v);
  252. if (res)
  253. return res;
  254. u = v;
  255. u &= ~mask;
  256. u |= val;
  257. if (u == v)
  258. return 0;
  259. return yt921x_reg64_write(priv, reg, u);
  260. }
  261. static int yt921x_reg64_clear_bits(struct yt921x_priv *priv, u32 reg, u64 mask)
  262. {
  263. return yt921x_reg64_update_bits(priv, reg, mask, 0);
  264. }
  265. static int yt921x_reg_mdio_read(void *context, u32 reg, u32 *valp)
  266. {
  267. struct yt921x_reg_mdio *mdio = context;
  268. struct mii_bus *bus = mdio->bus;
  269. int addr = mdio->addr;
  270. u32 reg_addr;
  271. u32 reg_data;
  272. u32 val;
  273. int res;
  274. /* Hold the mdio bus lock to avoid (un)locking for 4 times */
  275. mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  276. reg_addr = YT921X_SMI_SWITCHID(mdio->switchid) | YT921X_SMI_ADDR |
  277. YT921X_SMI_READ;
  278. res = __mdiobus_write(bus, addr, reg_addr, (u16)(reg >> 16));
  279. if (res)
  280. goto end;
  281. res = __mdiobus_write(bus, addr, reg_addr, (u16)reg);
  282. if (res)
  283. goto end;
  284. reg_data = YT921X_SMI_SWITCHID(mdio->switchid) | YT921X_SMI_DATA |
  285. YT921X_SMI_READ;
  286. res = __mdiobus_read(bus, addr, reg_data);
  287. if (res < 0)
  288. goto end;
  289. val = (u16)res;
  290. res = __mdiobus_read(bus, addr, reg_data);
  291. if (res < 0)
  292. goto end;
  293. val = (val << 16) | (u16)res;
  294. *valp = val;
  295. res = 0;
  296. end:
  297. mutex_unlock(&bus->mdio_lock);
  298. return res;
  299. }
  300. static int yt921x_reg_mdio_write(void *context, u32 reg, u32 val)
  301. {
  302. struct yt921x_reg_mdio *mdio = context;
  303. struct mii_bus *bus = mdio->bus;
  304. int addr = mdio->addr;
  305. u32 reg_addr;
  306. u32 reg_data;
  307. int res;
  308. mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  309. reg_addr = YT921X_SMI_SWITCHID(mdio->switchid) | YT921X_SMI_ADDR |
  310. YT921X_SMI_WRITE;
  311. res = __mdiobus_write(bus, addr, reg_addr, (u16)(reg >> 16));
  312. if (res)
  313. goto end;
  314. res = __mdiobus_write(bus, addr, reg_addr, (u16)reg);
  315. if (res)
  316. goto end;
  317. reg_data = YT921X_SMI_SWITCHID(mdio->switchid) | YT921X_SMI_DATA |
  318. YT921X_SMI_WRITE;
  319. res = __mdiobus_write(bus, addr, reg_data, (u16)(val >> 16));
  320. if (res)
  321. goto end;
  322. res = __mdiobus_write(bus, addr, reg_data, (u16)val);
  323. if (res)
  324. goto end;
  325. res = 0;
  326. end:
  327. mutex_unlock(&bus->mdio_lock);
  328. return res;
  329. }
  330. static const struct yt921x_reg_ops yt921x_reg_ops_mdio = {
  331. .read = yt921x_reg_mdio_read,
  332. .write = yt921x_reg_mdio_write,
  333. };
  334. /* TODO: SPI/I2C */
  335. static int yt921x_intif_wait(struct yt921x_priv *priv)
  336. {
  337. u32 val = 0;
  338. return yt921x_reg_wait(priv, YT921X_INT_MBUS_OP, YT921X_MBUS_OP_START,
  339. &val);
  340. }
  341. static int
  342. yt921x_intif_read(struct yt921x_priv *priv, int port, int reg, u16 *valp)
  343. {
  344. struct device *dev = to_device(priv);
  345. u32 mask;
  346. u32 ctrl;
  347. u32 val;
  348. int res;
  349. res = yt921x_intif_wait(priv);
  350. if (res)
  351. return res;
  352. mask = YT921X_MBUS_CTRL_PORT_M | YT921X_MBUS_CTRL_REG_M |
  353. YT921X_MBUS_CTRL_OP_M;
  354. ctrl = YT921X_MBUS_CTRL_PORT(port) | YT921X_MBUS_CTRL_REG(reg) |
  355. YT921X_MBUS_CTRL_READ;
  356. res = yt921x_reg_update_bits(priv, YT921X_INT_MBUS_CTRL, mask, ctrl);
  357. if (res)
  358. return res;
  359. res = yt921x_reg_write(priv, YT921X_INT_MBUS_OP, YT921X_MBUS_OP_START);
  360. if (res)
  361. return res;
  362. res = yt921x_intif_wait(priv);
  363. if (res)
  364. return res;
  365. res = yt921x_reg_read(priv, YT921X_INT_MBUS_DIN, &val);
  366. if (res)
  367. return res;
  368. if ((u16)val != val)
  369. dev_info(dev,
  370. "%s: port %d, reg 0x%x: Expected u16, got 0x%08x\n",
  371. __func__, port, reg, val);
  372. *valp = (u16)val;
  373. return 0;
  374. }
  375. static int
  376. yt921x_intif_write(struct yt921x_priv *priv, int port, int reg, u16 val)
  377. {
  378. u32 mask;
  379. u32 ctrl;
  380. int res;
  381. res = yt921x_intif_wait(priv);
  382. if (res)
  383. return res;
  384. mask = YT921X_MBUS_CTRL_PORT_M | YT921X_MBUS_CTRL_REG_M |
  385. YT921X_MBUS_CTRL_OP_M;
  386. ctrl = YT921X_MBUS_CTRL_PORT(port) | YT921X_MBUS_CTRL_REG(reg) |
  387. YT921X_MBUS_CTRL_WRITE;
  388. res = yt921x_reg_update_bits(priv, YT921X_INT_MBUS_CTRL, mask, ctrl);
  389. if (res)
  390. return res;
  391. res = yt921x_reg_write(priv, YT921X_INT_MBUS_DOUT, val);
  392. if (res)
  393. return res;
  394. res = yt921x_reg_write(priv, YT921X_INT_MBUS_OP, YT921X_MBUS_OP_START);
  395. if (res)
  396. return res;
  397. return yt921x_intif_wait(priv);
  398. }
  399. static int yt921x_mbus_int_read(struct mii_bus *mbus, int port, int reg)
  400. {
  401. struct yt921x_priv *priv = mbus->priv;
  402. u16 val;
  403. int res;
  404. if (port >= YT921X_PORT_NUM)
  405. return U16_MAX;
  406. mutex_lock(&priv->reg_lock);
  407. res = yt921x_intif_read(priv, port, reg, &val);
  408. mutex_unlock(&priv->reg_lock);
  409. if (res)
  410. return res;
  411. return val;
  412. }
  413. static int
  414. yt921x_mbus_int_write(struct mii_bus *mbus, int port, int reg, u16 data)
  415. {
  416. struct yt921x_priv *priv = mbus->priv;
  417. int res;
  418. if (port >= YT921X_PORT_NUM)
  419. return -ENODEV;
  420. mutex_lock(&priv->reg_lock);
  421. res = yt921x_intif_write(priv, port, reg, data);
  422. mutex_unlock(&priv->reg_lock);
  423. return res;
  424. }
  425. static int
  426. yt921x_mbus_int_init(struct yt921x_priv *priv, struct device_node *mnp)
  427. {
  428. struct device *dev = to_device(priv);
  429. struct mii_bus *mbus;
  430. int res;
  431. mbus = devm_mdiobus_alloc(dev);
  432. if (!mbus)
  433. return -ENOMEM;
  434. mbus->name = "YT921x internal MDIO bus";
  435. snprintf(mbus->id, MII_BUS_ID_SIZE, "%s", dev_name(dev));
  436. mbus->priv = priv;
  437. mbus->read = yt921x_mbus_int_read;
  438. mbus->write = yt921x_mbus_int_write;
  439. mbus->parent = dev;
  440. mbus->phy_mask = (u32)~GENMASK(YT921X_PORT_NUM - 1, 0);
  441. res = devm_of_mdiobus_register(dev, mbus, mnp);
  442. if (res)
  443. return res;
  444. priv->mbus_int = mbus;
  445. return 0;
  446. }
  447. static int yt921x_extif_wait(struct yt921x_priv *priv)
  448. {
  449. u32 val = 0;
  450. return yt921x_reg_wait(priv, YT921X_EXT_MBUS_OP, YT921X_MBUS_OP_START,
  451. &val);
  452. }
  453. static int
  454. yt921x_extif_read(struct yt921x_priv *priv, int port, int reg, u16 *valp)
  455. {
  456. struct device *dev = to_device(priv);
  457. u32 mask;
  458. u32 ctrl;
  459. u32 val;
  460. int res;
  461. res = yt921x_extif_wait(priv);
  462. if (res)
  463. return res;
  464. mask = YT921X_MBUS_CTRL_PORT_M | YT921X_MBUS_CTRL_REG_M |
  465. YT921X_MBUS_CTRL_TYPE_M | YT921X_MBUS_CTRL_OP_M;
  466. ctrl = YT921X_MBUS_CTRL_PORT(port) | YT921X_MBUS_CTRL_REG(reg) |
  467. YT921X_MBUS_CTRL_TYPE_C22 | YT921X_MBUS_CTRL_READ;
  468. res = yt921x_reg_update_bits(priv, YT921X_EXT_MBUS_CTRL, mask, ctrl);
  469. if (res)
  470. return res;
  471. res = yt921x_reg_write(priv, YT921X_EXT_MBUS_OP, YT921X_MBUS_OP_START);
  472. if (res)
  473. return res;
  474. res = yt921x_extif_wait(priv);
  475. if (res)
  476. return res;
  477. res = yt921x_reg_read(priv, YT921X_EXT_MBUS_DIN, &val);
  478. if (res)
  479. return res;
  480. if ((u16)val != val)
  481. dev_info(dev,
  482. "%s: port %d, reg 0x%x: Expected u16, got 0x%08x\n",
  483. __func__, port, reg, val);
  484. *valp = (u16)val;
  485. return 0;
  486. }
  487. static int
  488. yt921x_extif_write(struct yt921x_priv *priv, int port, int reg, u16 val)
  489. {
  490. u32 mask;
  491. u32 ctrl;
  492. int res;
  493. res = yt921x_extif_wait(priv);
  494. if (res)
  495. return res;
  496. mask = YT921X_MBUS_CTRL_PORT_M | YT921X_MBUS_CTRL_REG_M |
  497. YT921X_MBUS_CTRL_TYPE_M | YT921X_MBUS_CTRL_OP_M;
  498. ctrl = YT921X_MBUS_CTRL_PORT(port) | YT921X_MBUS_CTRL_REG(reg) |
  499. YT921X_MBUS_CTRL_TYPE_C22 | YT921X_MBUS_CTRL_WRITE;
  500. res = yt921x_reg_update_bits(priv, YT921X_EXT_MBUS_CTRL, mask, ctrl);
  501. if (res)
  502. return res;
  503. res = yt921x_reg_write(priv, YT921X_EXT_MBUS_DOUT, val);
  504. if (res)
  505. return res;
  506. res = yt921x_reg_write(priv, YT921X_EXT_MBUS_OP, YT921X_MBUS_OP_START);
  507. if (res)
  508. return res;
  509. return yt921x_extif_wait(priv);
  510. }
  511. static int yt921x_mbus_ext_read(struct mii_bus *mbus, int port, int reg)
  512. {
  513. struct yt921x_priv *priv = mbus->priv;
  514. u16 val;
  515. int res;
  516. mutex_lock(&priv->reg_lock);
  517. res = yt921x_extif_read(priv, port, reg, &val);
  518. mutex_unlock(&priv->reg_lock);
  519. if (res)
  520. return res;
  521. return val;
  522. }
  523. static int
  524. yt921x_mbus_ext_write(struct mii_bus *mbus, int port, int reg, u16 data)
  525. {
  526. struct yt921x_priv *priv = mbus->priv;
  527. int res;
  528. mutex_lock(&priv->reg_lock);
  529. res = yt921x_extif_write(priv, port, reg, data);
  530. mutex_unlock(&priv->reg_lock);
  531. return res;
  532. }
  533. static int
  534. yt921x_mbus_ext_init(struct yt921x_priv *priv, struct device_node *mnp)
  535. {
  536. struct device *dev = to_device(priv);
  537. struct mii_bus *mbus;
  538. int res;
  539. mbus = devm_mdiobus_alloc(dev);
  540. if (!mbus)
  541. return -ENOMEM;
  542. mbus->name = "YT921x external MDIO bus";
  543. snprintf(mbus->id, MII_BUS_ID_SIZE, "%s@ext", dev_name(dev));
  544. mbus->priv = priv;
  545. /* TODO: c45? */
  546. mbus->read = yt921x_mbus_ext_read;
  547. mbus->write = yt921x_mbus_ext_write;
  548. mbus->parent = dev;
  549. res = devm_of_mdiobus_register(dev, mbus, mnp);
  550. if (res)
  551. return res;
  552. priv->mbus_ext = mbus;
  553. return 0;
  554. }
  555. /* Read and handle overflow of 32bit MIBs. MIB buffer must be zeroed before. */
  556. static int yt921x_read_mib(struct yt921x_priv *priv, int port)
  557. {
  558. struct yt921x_port *pp = &priv->ports[port];
  559. struct device *dev = to_device(priv);
  560. struct yt921x_mib *mib = &pp->mib;
  561. int res = 0;
  562. /* Reading of yt921x_port::mib is not protected by a lock and it's vain
  563. * to keep its consistency, since we have to read registers one by one
  564. * and there is no way to make a snapshot of MIB stats.
  565. *
  566. * Writing (by this function only) is and should be protected by
  567. * reg_lock.
  568. */
  569. for (size_t i = 0; i < ARRAY_SIZE(yt921x_mib_descs); i++) {
  570. const struct yt921x_mib_desc *desc = &yt921x_mib_descs[i];
  571. u32 reg = YT921X_MIBn_DATA0(port) + desc->offset;
  572. u64 *valp = &((u64 *)mib)[i];
  573. u32 val0;
  574. u64 val;
  575. res = yt921x_reg_read(priv, reg, &val0);
  576. if (res)
  577. break;
  578. if (desc->size <= 1) {
  579. u64 old_val = *valp;
  580. val = (old_val & ~(u64)U32_MAX) | val0;
  581. if (val < old_val)
  582. val += 1ull << 32;
  583. } else {
  584. u32 val1;
  585. res = yt921x_reg_read(priv, reg + 4, &val1);
  586. if (res)
  587. break;
  588. val = ((u64)val1 << 32) | val0;
  589. }
  590. WRITE_ONCE(*valp, val);
  591. }
  592. pp->rx_frames = mib->rx_64byte + mib->rx_65_127byte +
  593. mib->rx_128_255byte + mib->rx_256_511byte +
  594. mib->rx_512_1023byte + mib->rx_1024_1518byte +
  595. mib->rx_jumbo;
  596. pp->tx_frames = mib->tx_64byte + mib->tx_65_127byte +
  597. mib->tx_128_255byte + mib->tx_256_511byte +
  598. mib->tx_512_1023byte + mib->tx_1024_1518byte +
  599. mib->tx_jumbo;
  600. if (res)
  601. dev_err(dev, "Failed to %s port %d: %i\n", "read stats for",
  602. port, res);
  603. return res;
  604. }
  605. static void yt921x_poll_mib(struct work_struct *work)
  606. {
  607. struct yt921x_port *pp = container_of_const(work, struct yt921x_port,
  608. mib_read.work);
  609. struct yt921x_priv *priv = (void *)(pp - pp->index) -
  610. offsetof(struct yt921x_priv, ports);
  611. unsigned long delay = YT921X_STATS_INTERVAL_JIFFIES;
  612. int port = pp->index;
  613. int res;
  614. mutex_lock(&priv->reg_lock);
  615. res = yt921x_read_mib(priv, port);
  616. mutex_unlock(&priv->reg_lock);
  617. if (res)
  618. delay *= 4;
  619. schedule_delayed_work(&pp->mib_read, delay);
  620. }
  621. static void
  622. yt921x_dsa_get_strings(struct dsa_switch *ds, int port, u32 stringset,
  623. uint8_t *data)
  624. {
  625. if (stringset != ETH_SS_STATS)
  626. return;
  627. for (size_t i = 0; i < ARRAY_SIZE(yt921x_mib_descs); i++) {
  628. const struct yt921x_mib_desc *desc = &yt921x_mib_descs[i];
  629. if (desc->name)
  630. ethtool_puts(&data, desc->name);
  631. }
  632. }
  633. static void
  634. yt921x_dsa_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data)
  635. {
  636. struct yt921x_priv *priv = to_yt921x_priv(ds);
  637. struct yt921x_port *pp = &priv->ports[port];
  638. struct yt921x_mib *mib = &pp->mib;
  639. size_t j;
  640. mutex_lock(&priv->reg_lock);
  641. yt921x_read_mib(priv, port);
  642. mutex_unlock(&priv->reg_lock);
  643. j = 0;
  644. for (size_t i = 0; i < ARRAY_SIZE(yt921x_mib_descs); i++) {
  645. const struct yt921x_mib_desc *desc = &yt921x_mib_descs[i];
  646. if (!desc->name)
  647. continue;
  648. data[j] = ((u64 *)mib)[i];
  649. j++;
  650. }
  651. }
  652. static int yt921x_dsa_get_sset_count(struct dsa_switch *ds, int port, int sset)
  653. {
  654. int cnt = 0;
  655. if (sset != ETH_SS_STATS)
  656. return 0;
  657. for (size_t i = 0; i < ARRAY_SIZE(yt921x_mib_descs); i++) {
  658. const struct yt921x_mib_desc *desc = &yt921x_mib_descs[i];
  659. if (desc->name)
  660. cnt++;
  661. }
  662. return cnt;
  663. }
  664. static void
  665. yt921x_dsa_get_eth_mac_stats(struct dsa_switch *ds, int port,
  666. struct ethtool_eth_mac_stats *mac_stats)
  667. {
  668. struct yt921x_priv *priv = to_yt921x_priv(ds);
  669. struct yt921x_port *pp = &priv->ports[port];
  670. struct yt921x_mib *mib = &pp->mib;
  671. mutex_lock(&priv->reg_lock);
  672. yt921x_read_mib(priv, port);
  673. mutex_unlock(&priv->reg_lock);
  674. mac_stats->FramesTransmittedOK = pp->tx_frames;
  675. mac_stats->SingleCollisionFrames = mib->tx_single_collisions;
  676. mac_stats->MultipleCollisionFrames = mib->tx_multiple_collisions;
  677. mac_stats->FramesReceivedOK = pp->rx_frames;
  678. mac_stats->FrameCheckSequenceErrors = mib->rx_crc_errors;
  679. mac_stats->AlignmentErrors = mib->rx_alignment_errors;
  680. mac_stats->OctetsTransmittedOK = mib->tx_good_bytes;
  681. mac_stats->FramesWithDeferredXmissions = mib->tx_deferred;
  682. mac_stats->LateCollisions = mib->tx_late_collisions;
  683. mac_stats->FramesAbortedDueToXSColls = mib->tx_aborted_errors;
  684. /* mac_stats->FramesLostDueToIntMACXmitError */
  685. /* mac_stats->CarrierSenseErrors */
  686. mac_stats->OctetsReceivedOK = mib->rx_good_bytes;
  687. /* mac_stats->FramesLostDueToIntMACRcvError */
  688. mac_stats->MulticastFramesXmittedOK = mib->tx_multicast;
  689. mac_stats->BroadcastFramesXmittedOK = mib->tx_broadcast;
  690. /* mac_stats->FramesWithExcessiveDeferral */
  691. mac_stats->MulticastFramesReceivedOK = mib->rx_multicast;
  692. mac_stats->BroadcastFramesReceivedOK = mib->rx_broadcast;
  693. /* mac_stats->InRangeLengthErrors */
  694. /* mac_stats->OutOfRangeLengthField */
  695. mac_stats->FrameTooLongErrors = mib->rx_oversize_errors;
  696. }
  697. static void
  698. yt921x_dsa_get_eth_ctrl_stats(struct dsa_switch *ds, int port,
  699. struct ethtool_eth_ctrl_stats *ctrl_stats)
  700. {
  701. struct yt921x_priv *priv = to_yt921x_priv(ds);
  702. struct yt921x_port *pp = &priv->ports[port];
  703. struct yt921x_mib *mib = &pp->mib;
  704. mutex_lock(&priv->reg_lock);
  705. yt921x_read_mib(priv, port);
  706. mutex_unlock(&priv->reg_lock);
  707. ctrl_stats->MACControlFramesTransmitted = mib->tx_pause;
  708. ctrl_stats->MACControlFramesReceived = mib->rx_pause;
  709. /* ctrl_stats->UnsupportedOpcodesReceived */
  710. }
  711. static const struct ethtool_rmon_hist_range yt921x_rmon_ranges[] = {
  712. { 0, 64 },
  713. { 65, 127 },
  714. { 128, 255 },
  715. { 256, 511 },
  716. { 512, 1023 },
  717. { 1024, 1518 },
  718. { 1519, YT921X_FRAME_SIZE_MAX },
  719. {}
  720. };
  721. static void
  722. yt921x_dsa_get_rmon_stats(struct dsa_switch *ds, int port,
  723. struct ethtool_rmon_stats *rmon_stats,
  724. const struct ethtool_rmon_hist_range **ranges)
  725. {
  726. struct yt921x_priv *priv = to_yt921x_priv(ds);
  727. struct yt921x_port *pp = &priv->ports[port];
  728. struct yt921x_mib *mib = &pp->mib;
  729. mutex_lock(&priv->reg_lock);
  730. yt921x_read_mib(priv, port);
  731. mutex_unlock(&priv->reg_lock);
  732. *ranges = yt921x_rmon_ranges;
  733. rmon_stats->undersize_pkts = mib->rx_undersize_errors;
  734. rmon_stats->oversize_pkts = mib->rx_oversize_errors;
  735. rmon_stats->fragments = mib->rx_alignment_errors;
  736. /* rmon_stats->jabbers */
  737. rmon_stats->hist[0] = mib->rx_64byte;
  738. rmon_stats->hist[1] = mib->rx_65_127byte;
  739. rmon_stats->hist[2] = mib->rx_128_255byte;
  740. rmon_stats->hist[3] = mib->rx_256_511byte;
  741. rmon_stats->hist[4] = mib->rx_512_1023byte;
  742. rmon_stats->hist[5] = mib->rx_1024_1518byte;
  743. rmon_stats->hist[6] = mib->rx_jumbo;
  744. rmon_stats->hist_tx[0] = mib->tx_64byte;
  745. rmon_stats->hist_tx[1] = mib->tx_65_127byte;
  746. rmon_stats->hist_tx[2] = mib->tx_128_255byte;
  747. rmon_stats->hist_tx[3] = mib->tx_256_511byte;
  748. rmon_stats->hist_tx[4] = mib->tx_512_1023byte;
  749. rmon_stats->hist_tx[5] = mib->tx_1024_1518byte;
  750. rmon_stats->hist_tx[6] = mib->tx_jumbo;
  751. }
  752. static void
  753. yt921x_dsa_get_stats64(struct dsa_switch *ds, int port,
  754. struct rtnl_link_stats64 *stats)
  755. {
  756. struct yt921x_priv *priv = to_yt921x_priv(ds);
  757. struct yt921x_port *pp = &priv->ports[port];
  758. struct yt921x_mib *mib = &pp->mib;
  759. stats->rx_length_errors = mib->rx_undersize_errors +
  760. mib->rx_fragment_errors;
  761. stats->rx_over_errors = mib->rx_oversize_errors;
  762. stats->rx_crc_errors = mib->rx_crc_errors;
  763. stats->rx_frame_errors = mib->rx_alignment_errors;
  764. /* stats->rx_fifo_errors */
  765. /* stats->rx_missed_errors */
  766. stats->tx_aborted_errors = mib->tx_aborted_errors;
  767. /* stats->tx_carrier_errors */
  768. stats->tx_fifo_errors = mib->tx_undersize_errors;
  769. /* stats->tx_heartbeat_errors */
  770. stats->tx_window_errors = mib->tx_late_collisions;
  771. stats->rx_packets = pp->rx_frames;
  772. stats->tx_packets = pp->tx_frames;
  773. stats->rx_bytes = mib->rx_good_bytes - ETH_FCS_LEN * stats->rx_packets;
  774. stats->tx_bytes = mib->tx_good_bytes - ETH_FCS_LEN * stats->tx_packets;
  775. stats->rx_errors = stats->rx_length_errors + stats->rx_over_errors +
  776. stats->rx_crc_errors + stats->rx_frame_errors;
  777. stats->tx_errors = stats->tx_aborted_errors + stats->tx_fifo_errors +
  778. stats->tx_window_errors;
  779. stats->rx_dropped = mib->rx_dropped;
  780. /* stats->tx_dropped */
  781. stats->multicast = mib->rx_multicast;
  782. stats->collisions = mib->tx_collisions;
  783. }
  784. static void
  785. yt921x_dsa_get_pause_stats(struct dsa_switch *ds, int port,
  786. struct ethtool_pause_stats *pause_stats)
  787. {
  788. struct yt921x_priv *priv = to_yt921x_priv(ds);
  789. struct yt921x_port *pp = &priv->ports[port];
  790. struct yt921x_mib *mib = &pp->mib;
  791. mutex_lock(&priv->reg_lock);
  792. yt921x_read_mib(priv, port);
  793. mutex_unlock(&priv->reg_lock);
  794. pause_stats->tx_pause_frames = mib->tx_pause;
  795. pause_stats->rx_pause_frames = mib->rx_pause;
  796. }
  797. static int
  798. yt921x_set_eee(struct yt921x_priv *priv, int port, struct ethtool_keee *e)
  799. {
  800. /* Poor datasheet for EEE operations; don't ask if you are confused */
  801. bool enable = e->eee_enabled;
  802. u16 new_mask;
  803. int res;
  804. /* Enable / disable global EEE */
  805. new_mask = priv->eee_ports_mask;
  806. new_mask &= ~BIT(port);
  807. new_mask |= !enable ? 0 : BIT(port);
  808. if (!!new_mask != !!priv->eee_ports_mask) {
  809. res = yt921x_reg_toggle_bits(priv, YT921X_PON_STRAP_FUNC,
  810. YT921X_PON_STRAP_EEE, !!new_mask);
  811. if (res)
  812. return res;
  813. res = yt921x_reg_toggle_bits(priv, YT921X_PON_STRAP_VAL,
  814. YT921X_PON_STRAP_EEE, !!new_mask);
  815. if (res)
  816. return res;
  817. }
  818. priv->eee_ports_mask = new_mask;
  819. /* Enable / disable port EEE */
  820. res = yt921x_reg_toggle_bits(priv, YT921X_EEE_CTRL,
  821. YT921X_EEE_CTRL_ENn(port), enable);
  822. if (res)
  823. return res;
  824. res = yt921x_reg_toggle_bits(priv, YT921X_EEEn_VAL(port),
  825. YT921X_EEE_VAL_DATA, enable);
  826. if (res)
  827. return res;
  828. return 0;
  829. }
  830. static int
  831. yt921x_dsa_set_mac_eee(struct dsa_switch *ds, int port, struct ethtool_keee *e)
  832. {
  833. struct yt921x_priv *priv = to_yt921x_priv(ds);
  834. int res;
  835. mutex_lock(&priv->reg_lock);
  836. res = yt921x_set_eee(priv, port, e);
  837. mutex_unlock(&priv->reg_lock);
  838. return res;
  839. }
  840. static int
  841. yt921x_dsa_port_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
  842. {
  843. /* Only serves as packet filter, since the frame size is always set to
  844. * maximum after reset
  845. */
  846. struct yt921x_priv *priv = to_yt921x_priv(ds);
  847. struct dsa_port *dp = dsa_to_port(ds, port);
  848. int frame_size;
  849. int res;
  850. frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN;
  851. if (dsa_port_is_cpu(dp))
  852. frame_size += YT921X_TAG_LEN;
  853. mutex_lock(&priv->reg_lock);
  854. res = yt921x_reg_update_bits(priv, YT921X_MACn_FRAME(port),
  855. YT921X_MAC_FRAME_SIZE_M,
  856. YT921X_MAC_FRAME_SIZE(frame_size));
  857. mutex_unlock(&priv->reg_lock);
  858. return res;
  859. }
  860. static int yt921x_dsa_port_max_mtu(struct dsa_switch *ds, int port)
  861. {
  862. /* Only called for user ports, exclude tag len here */
  863. return YT921X_FRAME_SIZE_MAX - ETH_HLEN - ETH_FCS_LEN - YT921X_TAG_LEN;
  864. }
  865. static int
  866. yt921x_mirror_del(struct yt921x_priv *priv, int port, bool ingress)
  867. {
  868. u32 mask;
  869. if (ingress)
  870. mask = YT921X_MIRROR_IGR_PORTn(port);
  871. else
  872. mask = YT921X_MIRROR_EGR_PORTn(port);
  873. return yt921x_reg_clear_bits(priv, YT921X_MIRROR, mask);
  874. }
  875. static int
  876. yt921x_mirror_add(struct yt921x_priv *priv, int port, bool ingress,
  877. int to_local_port, struct netlink_ext_ack *extack)
  878. {
  879. u32 srcs;
  880. u32 ctrl;
  881. u32 val;
  882. u32 dst;
  883. int res;
  884. if (ingress)
  885. srcs = YT921X_MIRROR_IGR_PORTn(port);
  886. else
  887. srcs = YT921X_MIRROR_EGR_PORTn(port);
  888. dst = YT921X_MIRROR_PORT(to_local_port);
  889. res = yt921x_reg_read(priv, YT921X_MIRROR, &val);
  890. if (res)
  891. return res;
  892. /* other mirror tasks & different dst port -> conflict */
  893. if ((val & ~srcs & (YT921X_MIRROR_EGR_PORTS_M |
  894. YT921X_MIRROR_IGR_PORTS_M)) &&
  895. (val & YT921X_MIRROR_PORT_M) != dst) {
  896. NL_SET_ERR_MSG_MOD(extack,
  897. "Sniffer port is already configured, delete existing rules & retry");
  898. return -EBUSY;
  899. }
  900. ctrl = val & ~YT921X_MIRROR_PORT_M;
  901. ctrl |= srcs;
  902. ctrl |= dst;
  903. if (ctrl == val)
  904. return 0;
  905. return yt921x_reg_write(priv, YT921X_MIRROR, ctrl);
  906. }
  907. static void
  908. yt921x_dsa_port_mirror_del(struct dsa_switch *ds, int port,
  909. struct dsa_mall_mirror_tc_entry *mirror)
  910. {
  911. struct yt921x_priv *priv = to_yt921x_priv(ds);
  912. struct device *dev = to_device(priv);
  913. int res;
  914. mutex_lock(&priv->reg_lock);
  915. res = yt921x_mirror_del(priv, port, mirror->ingress);
  916. mutex_unlock(&priv->reg_lock);
  917. if (res)
  918. dev_err(dev, "Failed to %s port %d: %i\n", "unmirror",
  919. port, res);
  920. }
  921. static int
  922. yt921x_dsa_port_mirror_add(struct dsa_switch *ds, int port,
  923. struct dsa_mall_mirror_tc_entry *mirror,
  924. bool ingress, struct netlink_ext_ack *extack)
  925. {
  926. struct yt921x_priv *priv = to_yt921x_priv(ds);
  927. int res;
  928. mutex_lock(&priv->reg_lock);
  929. res = yt921x_mirror_add(priv, port, ingress,
  930. mirror->to_local_port, extack);
  931. mutex_unlock(&priv->reg_lock);
  932. return res;
  933. }
  934. static int yt921x_lag_hash(struct yt921x_priv *priv, u32 ctrl, bool unique_lag,
  935. struct netlink_ext_ack *extack)
  936. {
  937. u32 val;
  938. int res;
  939. /* Hash Mode is global. Make sure the same Hash Mode is set to all the
  940. * 2 possible lags.
  941. * If we are the unique LAG we can set whatever hash mode we want.
  942. * To change hash mode it's needed to remove all LAG and change the mode
  943. * with the latest.
  944. */
  945. if (unique_lag) {
  946. res = yt921x_reg_write(priv, YT921X_LAG_HASH, ctrl);
  947. if (res)
  948. return res;
  949. } else {
  950. res = yt921x_reg_read(priv, YT921X_LAG_HASH, &val);
  951. if (res)
  952. return res;
  953. if (val != ctrl) {
  954. NL_SET_ERR_MSG_MOD(extack,
  955. "Mismatched Hash Mode across different lags is not supported");
  956. return -EOPNOTSUPP;
  957. }
  958. }
  959. return 0;
  960. }
  961. static int yt921x_lag_set(struct yt921x_priv *priv, u8 index, u16 ports_mask)
  962. {
  963. unsigned long targets_mask = ports_mask;
  964. unsigned int cnt;
  965. u32 ctrl;
  966. int port;
  967. int res;
  968. cnt = 0;
  969. for_each_set_bit(port, &targets_mask, YT921X_PORT_NUM) {
  970. ctrl = YT921X_LAG_MEMBER_PORT(port);
  971. res = yt921x_reg_write(priv, YT921X_LAG_MEMBERnm(index, cnt),
  972. ctrl);
  973. if (res)
  974. return res;
  975. cnt++;
  976. }
  977. ctrl = YT921X_LAG_GROUP_PORTS(ports_mask) |
  978. YT921X_LAG_GROUP_MEMBER_NUM(cnt);
  979. return yt921x_reg_write(priv, YT921X_LAG_GROUPn(index), ctrl);
  980. }
  981. static int
  982. yt921x_dsa_port_lag_leave(struct dsa_switch *ds, int port, struct dsa_lag lag)
  983. {
  984. struct yt921x_priv *priv = to_yt921x_priv(ds);
  985. struct dsa_port *dp;
  986. u32 ctrl;
  987. int res;
  988. if (!lag.id)
  989. return -EINVAL;
  990. ctrl = 0;
  991. dsa_lag_foreach_port(dp, ds->dst, &lag)
  992. ctrl |= BIT(dp->index);
  993. mutex_lock(&priv->reg_lock);
  994. res = yt921x_lag_set(priv, lag.id - 1, ctrl);
  995. mutex_unlock(&priv->reg_lock);
  996. return res;
  997. }
  998. static int
  999. yt921x_dsa_port_lag_check(struct dsa_switch *ds, struct dsa_lag lag,
  1000. struct netdev_lag_upper_info *info,
  1001. struct netlink_ext_ack *extack)
  1002. {
  1003. unsigned int members;
  1004. struct dsa_port *dp;
  1005. if (!lag.id)
  1006. return -EINVAL;
  1007. members = 0;
  1008. dsa_lag_foreach_port(dp, ds->dst, &lag)
  1009. /* Includes the port joining the LAG */
  1010. members++;
  1011. if (members > YT921X_LAG_PORT_NUM) {
  1012. NL_SET_ERR_MSG_MOD(extack,
  1013. "Cannot offload more than 4 LAG ports");
  1014. return -EOPNOTSUPP;
  1015. }
  1016. if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
  1017. NL_SET_ERR_MSG_MOD(extack,
  1018. "Can only offload LAG using hash TX type");
  1019. return -EOPNOTSUPP;
  1020. }
  1021. if (info->hash_type != NETDEV_LAG_HASH_L2 &&
  1022. info->hash_type != NETDEV_LAG_HASH_L23 &&
  1023. info->hash_type != NETDEV_LAG_HASH_L34) {
  1024. NL_SET_ERR_MSG_MOD(extack,
  1025. "Can only offload L2 or L2+L3 or L3+L4 TX hash");
  1026. return -EOPNOTSUPP;
  1027. }
  1028. return 0;
  1029. }
  1030. static int
  1031. yt921x_dsa_port_lag_join(struct dsa_switch *ds, int port, struct dsa_lag lag,
  1032. struct netdev_lag_upper_info *info,
  1033. struct netlink_ext_ack *extack)
  1034. {
  1035. struct yt921x_priv *priv = to_yt921x_priv(ds);
  1036. struct dsa_port *dp;
  1037. bool unique_lag;
  1038. unsigned int i;
  1039. u32 ctrl;
  1040. int res;
  1041. res = yt921x_dsa_port_lag_check(ds, lag, info, extack);
  1042. if (res)
  1043. return res;
  1044. ctrl = 0;
  1045. switch (info->hash_type) {
  1046. case NETDEV_LAG_HASH_L34:
  1047. ctrl |= YT921X_LAG_HASH_IP_DST;
  1048. ctrl |= YT921X_LAG_HASH_IP_SRC;
  1049. ctrl |= YT921X_LAG_HASH_IP_PROTO;
  1050. ctrl |= YT921X_LAG_HASH_L4_DPORT;
  1051. ctrl |= YT921X_LAG_HASH_L4_SPORT;
  1052. break;
  1053. case NETDEV_LAG_HASH_L23:
  1054. ctrl |= YT921X_LAG_HASH_MAC_DA;
  1055. ctrl |= YT921X_LAG_HASH_MAC_SA;
  1056. ctrl |= YT921X_LAG_HASH_IP_DST;
  1057. ctrl |= YT921X_LAG_HASH_IP_SRC;
  1058. ctrl |= YT921X_LAG_HASH_IP_PROTO;
  1059. break;
  1060. case NETDEV_LAG_HASH_L2:
  1061. ctrl |= YT921X_LAG_HASH_MAC_DA;
  1062. ctrl |= YT921X_LAG_HASH_MAC_SA;
  1063. break;
  1064. default:
  1065. return -EOPNOTSUPP;
  1066. }
  1067. /* Check if we are the unique configured LAG */
  1068. unique_lag = true;
  1069. dsa_lags_foreach_id(i, ds->dst)
  1070. if (i != lag.id && dsa_lag_by_id(ds->dst, i)) {
  1071. unique_lag = false;
  1072. break;
  1073. }
  1074. mutex_lock(&priv->reg_lock);
  1075. do {
  1076. res = yt921x_lag_hash(priv, ctrl, unique_lag, extack);
  1077. if (res)
  1078. break;
  1079. ctrl = 0;
  1080. dsa_lag_foreach_port(dp, ds->dst, &lag)
  1081. ctrl |= BIT(dp->index);
  1082. res = yt921x_lag_set(priv, lag.id - 1, ctrl);
  1083. } while (0);
  1084. mutex_unlock(&priv->reg_lock);
  1085. return res;
  1086. }
  1087. static int yt921x_fdb_wait(struct yt921x_priv *priv, u32 *valp)
  1088. {
  1089. struct device *dev = to_device(priv);
  1090. u32 val = YT921X_FDB_RESULT_DONE;
  1091. int res;
  1092. res = yt921x_reg_wait(priv, YT921X_FDB_RESULT, YT921X_FDB_RESULT_DONE,
  1093. &val);
  1094. if (res) {
  1095. dev_err(dev, "FDB probably stuck\n");
  1096. return res;
  1097. }
  1098. *valp = val;
  1099. return 0;
  1100. }
  1101. static int
  1102. yt921x_fdb_in01(struct yt921x_priv *priv, const unsigned char *addr,
  1103. u16 vid, u32 ctrl1)
  1104. {
  1105. u32 ctrl;
  1106. int res;
  1107. ctrl = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
  1108. res = yt921x_reg_write(priv, YT921X_FDB_IN0, ctrl);
  1109. if (res)
  1110. return res;
  1111. ctrl = ctrl1 | YT921X_FDB_IO1_FID(vid) | (addr[4] << 8) | addr[5];
  1112. return yt921x_reg_write(priv, YT921X_FDB_IN1, ctrl);
  1113. }
  1114. static int
  1115. yt921x_fdb_has(struct yt921x_priv *priv, const unsigned char *addr, u16 vid,
  1116. u16 *indexp)
  1117. {
  1118. u32 ctrl;
  1119. u32 val;
  1120. int res;
  1121. res = yt921x_fdb_in01(priv, addr, vid, 0);
  1122. if (res)
  1123. return res;
  1124. ctrl = 0;
  1125. res = yt921x_reg_write(priv, YT921X_FDB_IN2, ctrl);
  1126. if (res)
  1127. return res;
  1128. ctrl = YT921X_FDB_OP_OP_GET_ONE | YT921X_FDB_OP_START;
  1129. res = yt921x_reg_write(priv, YT921X_FDB_OP, ctrl);
  1130. if (res)
  1131. return res;
  1132. res = yt921x_fdb_wait(priv, &val);
  1133. if (res)
  1134. return res;
  1135. if (val & YT921X_FDB_RESULT_NOTFOUND) {
  1136. *indexp = YT921X_FDB_NUM;
  1137. return 0;
  1138. }
  1139. *indexp = FIELD_GET(YT921X_FDB_RESULT_INDEX_M, val);
  1140. return 0;
  1141. }
  1142. static int
  1143. yt921x_fdb_read(struct yt921x_priv *priv, unsigned char *addr, u16 *vidp,
  1144. u16 *ports_maskp, u16 *indexp, u8 *statusp)
  1145. {
  1146. struct device *dev = to_device(priv);
  1147. u16 index;
  1148. u32 data0;
  1149. u32 data1;
  1150. u32 data2;
  1151. u32 val;
  1152. int res;
  1153. res = yt921x_fdb_wait(priv, &val);
  1154. if (res)
  1155. return res;
  1156. if (val & YT921X_FDB_RESULT_NOTFOUND) {
  1157. *ports_maskp = 0;
  1158. return 0;
  1159. }
  1160. index = FIELD_GET(YT921X_FDB_RESULT_INDEX_M, val);
  1161. res = yt921x_reg_read(priv, YT921X_FDB_OUT1, &data1);
  1162. if (res)
  1163. return res;
  1164. if ((data1 & YT921X_FDB_IO1_STATUS_M) ==
  1165. YT921X_FDB_IO1_STATUS_INVALID) {
  1166. *ports_maskp = 0;
  1167. return 0;
  1168. }
  1169. res = yt921x_reg_read(priv, YT921X_FDB_OUT0, &data0);
  1170. if (res)
  1171. return res;
  1172. res = yt921x_reg_read(priv, YT921X_FDB_OUT2, &data2);
  1173. if (res)
  1174. return res;
  1175. addr[0] = data0 >> 24;
  1176. addr[1] = data0 >> 16;
  1177. addr[2] = data0 >> 8;
  1178. addr[3] = data0;
  1179. addr[4] = data1 >> 8;
  1180. addr[5] = data1;
  1181. *vidp = FIELD_GET(YT921X_FDB_IO1_FID_M, data1);
  1182. *indexp = index;
  1183. *ports_maskp = FIELD_GET(YT921X_FDB_IO2_EGR_PORTS_M, data2);
  1184. *statusp = FIELD_GET(YT921X_FDB_IO1_STATUS_M, data1);
  1185. dev_dbg(dev,
  1186. "%s: index 0x%x, mac %02x:%02x:%02x:%02x:%02x:%02x, vid %d, ports 0x%x, status %d\n",
  1187. __func__, *indexp, addr[0], addr[1], addr[2], addr[3],
  1188. addr[4], addr[5], *vidp, *ports_maskp, *statusp);
  1189. return 0;
  1190. }
  1191. static int
  1192. yt921x_fdb_dump(struct yt921x_priv *priv, u16 ports_mask,
  1193. dsa_fdb_dump_cb_t *cb, void *data)
  1194. {
  1195. unsigned char addr[ETH_ALEN];
  1196. u8 status;
  1197. u16 pmask;
  1198. u16 index;
  1199. u32 ctrl;
  1200. u16 vid;
  1201. int res;
  1202. ctrl = YT921X_FDB_OP_INDEX(0) | YT921X_FDB_OP_MODE_INDEX |
  1203. YT921X_FDB_OP_OP_GET_ONE | YT921X_FDB_OP_START;
  1204. res = yt921x_reg_write(priv, YT921X_FDB_OP, ctrl);
  1205. if (res)
  1206. return res;
  1207. res = yt921x_fdb_read(priv, addr, &vid, &pmask, &index, &status);
  1208. if (res)
  1209. return res;
  1210. if ((pmask & ports_mask) && !is_multicast_ether_addr(addr)) {
  1211. res = cb(addr, vid,
  1212. status == YT921X_FDB_ENTRY_STATUS_STATIC, data);
  1213. if (res)
  1214. return res;
  1215. }
  1216. ctrl = YT921X_FDB_IO2_EGR_PORTS(ports_mask);
  1217. res = yt921x_reg_write(priv, YT921X_FDB_IN2, ctrl);
  1218. if (res)
  1219. return res;
  1220. index = 0;
  1221. do {
  1222. ctrl = YT921X_FDB_OP_INDEX(index) | YT921X_FDB_OP_MODE_INDEX |
  1223. YT921X_FDB_OP_NEXT_TYPE_UCAST_PORT |
  1224. YT921X_FDB_OP_OP_GET_NEXT | YT921X_FDB_OP_START;
  1225. res = yt921x_reg_write(priv, YT921X_FDB_OP, ctrl);
  1226. if (res)
  1227. return res;
  1228. res = yt921x_fdb_read(priv, addr, &vid, &pmask, &index,
  1229. &status);
  1230. if (res)
  1231. return res;
  1232. if (!pmask)
  1233. break;
  1234. if ((pmask & ports_mask) && !is_multicast_ether_addr(addr)) {
  1235. res = cb(addr, vid,
  1236. status == YT921X_FDB_ENTRY_STATUS_STATIC,
  1237. data);
  1238. if (res)
  1239. return res;
  1240. }
  1241. /* Never call GET_NEXT with 4095, otherwise it will hang
  1242. * forever until a reset!
  1243. */
  1244. } while (index < YT921X_FDB_NUM - 1);
  1245. return 0;
  1246. }
  1247. static int
  1248. yt921x_fdb_flush_raw(struct yt921x_priv *priv, u16 ports_mask, u16 vid,
  1249. bool flush_static)
  1250. {
  1251. u32 ctrl;
  1252. u32 val;
  1253. int res;
  1254. if (vid < 4096) {
  1255. ctrl = YT921X_FDB_IO1_FID(vid);
  1256. res = yt921x_reg_write(priv, YT921X_FDB_IN1, ctrl);
  1257. if (res)
  1258. return res;
  1259. }
  1260. ctrl = YT921X_FDB_IO2_EGR_PORTS(ports_mask);
  1261. res = yt921x_reg_write(priv, YT921X_FDB_IN2, ctrl);
  1262. if (res)
  1263. return res;
  1264. ctrl = YT921X_FDB_OP_OP_FLUSH | YT921X_FDB_OP_START;
  1265. if (vid >= 4096)
  1266. ctrl |= YT921X_FDB_OP_FLUSH_PORT;
  1267. else
  1268. ctrl |= YT921X_FDB_OP_FLUSH_PORT_VID;
  1269. if (flush_static)
  1270. ctrl |= YT921X_FDB_OP_FLUSH_STATIC;
  1271. res = yt921x_reg_write(priv, YT921X_FDB_OP, ctrl);
  1272. if (res)
  1273. return res;
  1274. res = yt921x_fdb_wait(priv, &val);
  1275. if (res)
  1276. return res;
  1277. return 0;
  1278. }
  1279. static int
  1280. yt921x_fdb_flush_port(struct yt921x_priv *priv, int port, bool flush_static)
  1281. {
  1282. return yt921x_fdb_flush_raw(priv, BIT(port), 4096, flush_static);
  1283. }
  1284. static int
  1285. yt921x_fdb_add_index_in12(struct yt921x_priv *priv, u16 index, u16 ctrl1,
  1286. u16 ctrl2)
  1287. {
  1288. u32 ctrl;
  1289. u32 val;
  1290. int res;
  1291. res = yt921x_reg_write(priv, YT921X_FDB_IN1, ctrl1);
  1292. if (res)
  1293. return res;
  1294. res = yt921x_reg_write(priv, YT921X_FDB_IN2, ctrl2);
  1295. if (res)
  1296. return res;
  1297. ctrl = YT921X_FDB_OP_INDEX(index) | YT921X_FDB_OP_MODE_INDEX |
  1298. YT921X_FDB_OP_OP_ADD | YT921X_FDB_OP_START;
  1299. res = yt921x_reg_write(priv, YT921X_FDB_OP, ctrl);
  1300. if (res)
  1301. return res;
  1302. return yt921x_fdb_wait(priv, &val);
  1303. }
  1304. static int
  1305. yt921x_fdb_add(struct yt921x_priv *priv, const unsigned char *addr, u16 vid,
  1306. u16 ports_mask)
  1307. {
  1308. u32 ctrl;
  1309. u32 val;
  1310. int res;
  1311. ctrl = YT921X_FDB_IO1_STATUS_STATIC;
  1312. res = yt921x_fdb_in01(priv, addr, vid, ctrl);
  1313. if (res)
  1314. return res;
  1315. ctrl = YT921X_FDB_IO2_EGR_PORTS(ports_mask);
  1316. res = yt921x_reg_write(priv, YT921X_FDB_IN2, ctrl);
  1317. if (res)
  1318. return res;
  1319. ctrl = YT921X_FDB_OP_OP_ADD | YT921X_FDB_OP_START;
  1320. res = yt921x_reg_write(priv, YT921X_FDB_OP, ctrl);
  1321. if (res)
  1322. return res;
  1323. return yt921x_fdb_wait(priv, &val);
  1324. }
  1325. static int
  1326. yt921x_fdb_leave(struct yt921x_priv *priv, const unsigned char *addr,
  1327. u16 vid, u16 ports_mask)
  1328. {
  1329. u16 index;
  1330. u32 ctrl1;
  1331. u32 ctrl2;
  1332. u32 ctrl;
  1333. u32 val2;
  1334. u32 val;
  1335. int res;
  1336. /* Check for presence */
  1337. res = yt921x_fdb_has(priv, addr, vid, &index);
  1338. if (res)
  1339. return res;
  1340. if (index >= YT921X_FDB_NUM)
  1341. return 0;
  1342. /* Check if action required */
  1343. res = yt921x_reg_read(priv, YT921X_FDB_OUT2, &val2);
  1344. if (res)
  1345. return res;
  1346. ctrl2 = val2 & ~YT921X_FDB_IO2_EGR_PORTS(ports_mask);
  1347. if (ctrl2 == val2)
  1348. return 0;
  1349. if (!(ctrl2 & YT921X_FDB_IO2_EGR_PORTS_M)) {
  1350. ctrl = YT921X_FDB_OP_OP_DEL | YT921X_FDB_OP_START;
  1351. res = yt921x_reg_write(priv, YT921X_FDB_OP, ctrl);
  1352. if (res)
  1353. return res;
  1354. return yt921x_fdb_wait(priv, &val);
  1355. }
  1356. res = yt921x_reg_read(priv, YT921X_FDB_OUT1, &ctrl1);
  1357. if (res)
  1358. return res;
  1359. return yt921x_fdb_add_index_in12(priv, index, ctrl1, ctrl2);
  1360. }
  1361. static int
  1362. yt921x_fdb_join(struct yt921x_priv *priv, const unsigned char *addr, u16 vid,
  1363. u16 ports_mask)
  1364. {
  1365. u16 index;
  1366. u32 ctrl1;
  1367. u32 ctrl2;
  1368. u32 val1;
  1369. u32 val2;
  1370. int res;
  1371. /* Check for presence */
  1372. res = yt921x_fdb_has(priv, addr, vid, &index);
  1373. if (res)
  1374. return res;
  1375. if (index >= YT921X_FDB_NUM)
  1376. return yt921x_fdb_add(priv, addr, vid, ports_mask);
  1377. /* Check if action required */
  1378. res = yt921x_reg_read(priv, YT921X_FDB_OUT1, &val1);
  1379. if (res)
  1380. return res;
  1381. res = yt921x_reg_read(priv, YT921X_FDB_OUT2, &val2);
  1382. if (res)
  1383. return res;
  1384. ctrl1 = val1 & ~YT921X_FDB_IO1_STATUS_M;
  1385. ctrl1 |= YT921X_FDB_IO1_STATUS_STATIC;
  1386. ctrl2 = val2 | YT921X_FDB_IO2_EGR_PORTS(ports_mask);
  1387. if (ctrl1 == val1 && ctrl2 == val2)
  1388. return 0;
  1389. return yt921x_fdb_add_index_in12(priv, index, ctrl1, ctrl2);
  1390. }
  1391. static int
  1392. yt921x_dsa_port_fdb_dump(struct dsa_switch *ds, int port,
  1393. dsa_fdb_dump_cb_t *cb, void *data)
  1394. {
  1395. struct yt921x_priv *priv = to_yt921x_priv(ds);
  1396. int res;
  1397. mutex_lock(&priv->reg_lock);
  1398. /* Hardware FDB is shared for fdb and mdb, "bridge fdb show"
  1399. * only wants to see unicast
  1400. */
  1401. res = yt921x_fdb_dump(priv, BIT(port), cb, data);
  1402. mutex_unlock(&priv->reg_lock);
  1403. return res;
  1404. }
  1405. static void yt921x_dsa_port_fast_age(struct dsa_switch *ds, int port)
  1406. {
  1407. struct yt921x_priv *priv = to_yt921x_priv(ds);
  1408. struct device *dev = to_device(priv);
  1409. int res;
  1410. mutex_lock(&priv->reg_lock);
  1411. res = yt921x_fdb_flush_port(priv, port, false);
  1412. mutex_unlock(&priv->reg_lock);
  1413. if (res)
  1414. dev_err(dev, "Failed to %s port %d: %i\n", "clear FDB for",
  1415. port, res);
  1416. }
  1417. static int
  1418. yt921x_dsa_set_ageing_time(struct dsa_switch *ds, unsigned int msecs)
  1419. {
  1420. struct yt921x_priv *priv = to_yt921x_priv(ds);
  1421. u32 ctrl;
  1422. int res;
  1423. /* AGEING reg is set in 5s step */
  1424. ctrl = clamp(msecs / 5000, 1, U16_MAX);
  1425. mutex_lock(&priv->reg_lock);
  1426. res = yt921x_reg_write(priv, YT921X_AGEING, ctrl);
  1427. mutex_unlock(&priv->reg_lock);
  1428. return res;
  1429. }
  1430. static int
  1431. yt921x_dsa_port_fdb_del(struct dsa_switch *ds, int port,
  1432. const unsigned char *addr, u16 vid, struct dsa_db db)
  1433. {
  1434. struct yt921x_priv *priv = to_yt921x_priv(ds);
  1435. int res;
  1436. mutex_lock(&priv->reg_lock);
  1437. res = yt921x_fdb_leave(priv, addr, vid, BIT(port));
  1438. mutex_unlock(&priv->reg_lock);
  1439. return res;
  1440. }
  1441. static int
  1442. yt921x_dsa_port_fdb_add(struct dsa_switch *ds, int port,
  1443. const unsigned char *addr, u16 vid, struct dsa_db db)
  1444. {
  1445. struct yt921x_priv *priv = to_yt921x_priv(ds);
  1446. int res;
  1447. mutex_lock(&priv->reg_lock);
  1448. res = yt921x_fdb_join(priv, addr, vid, BIT(port));
  1449. mutex_unlock(&priv->reg_lock);
  1450. return res;
  1451. }
  1452. static int
  1453. yt921x_dsa_port_mdb_del(struct dsa_switch *ds, int port,
  1454. const struct switchdev_obj_port_mdb *mdb,
  1455. struct dsa_db db)
  1456. {
  1457. struct yt921x_priv *priv = to_yt921x_priv(ds);
  1458. const unsigned char *addr = mdb->addr;
  1459. u16 vid = mdb->vid;
  1460. int res;
  1461. mutex_lock(&priv->reg_lock);
  1462. res = yt921x_fdb_leave(priv, addr, vid, BIT(port));
  1463. mutex_unlock(&priv->reg_lock);
  1464. return res;
  1465. }
  1466. static int
  1467. yt921x_dsa_port_mdb_add(struct dsa_switch *ds, int port,
  1468. const struct switchdev_obj_port_mdb *mdb,
  1469. struct dsa_db db)
  1470. {
  1471. struct yt921x_priv *priv = to_yt921x_priv(ds);
  1472. const unsigned char *addr = mdb->addr;
  1473. u16 vid = mdb->vid;
  1474. int res;
  1475. mutex_lock(&priv->reg_lock);
  1476. res = yt921x_fdb_join(priv, addr, vid, BIT(port));
  1477. mutex_unlock(&priv->reg_lock);
  1478. return res;
  1479. }
  1480. static int
  1481. yt921x_vlan_aware_set(struct yt921x_priv *priv, int port, bool vlan_aware)
  1482. {
  1483. u32 ctrl;
  1484. /* Abuse SVLAN for PCP parsing without polluting the FDB - it just works
  1485. * despite YT921X_VLAN_CTRL_SVLAN_EN never being set
  1486. */
  1487. if (!vlan_aware)
  1488. ctrl = YT921X_PORT_IGR_TPIDn_STAG(0);
  1489. else
  1490. ctrl = YT921X_PORT_IGR_TPIDn_CTAG(0);
  1491. return yt921x_reg_write(priv, YT921X_PORTn_IGR_TPID(port), ctrl);
  1492. }
  1493. static int
  1494. yt921x_port_set_pvid(struct yt921x_priv *priv, int port, u16 vid)
  1495. {
  1496. u32 mask;
  1497. u32 ctrl;
  1498. mask = YT921X_PORT_VLAN_CTRL_CVID_M;
  1499. ctrl = YT921X_PORT_VLAN_CTRL_CVID(vid);
  1500. return yt921x_reg_update_bits(priv, YT921X_PORTn_VLAN_CTRL(port),
  1501. mask, ctrl);
  1502. }
  1503. static int
  1504. yt921x_vlan_filtering(struct yt921x_priv *priv, int port, bool vlan_filtering)
  1505. {
  1506. struct dsa_port *dp = dsa_to_port(&priv->ds, port);
  1507. struct net_device *bdev;
  1508. u16 pvid;
  1509. u32 mask;
  1510. u32 ctrl;
  1511. int res;
  1512. bdev = dsa_port_bridge_dev_get(dp);
  1513. if (!bdev || !vlan_filtering)
  1514. pvid = YT921X_VID_UNWARE;
  1515. else
  1516. br_vlan_get_pvid(bdev, &pvid);
  1517. res = yt921x_port_set_pvid(priv, port, pvid);
  1518. if (res)
  1519. return res;
  1520. mask = YT921X_PORT_VLAN_CTRL1_CVLAN_DROP_TAGGED |
  1521. YT921X_PORT_VLAN_CTRL1_CVLAN_DROP_UNTAGGED;
  1522. ctrl = 0;
  1523. /* Do not drop tagged frames here; let VLAN_IGR_FILTER do it */
  1524. if (vlan_filtering && !pvid)
  1525. ctrl |= YT921X_PORT_VLAN_CTRL1_CVLAN_DROP_UNTAGGED;
  1526. res = yt921x_reg_update_bits(priv, YT921X_PORTn_VLAN_CTRL1(port),
  1527. mask, ctrl);
  1528. if (res)
  1529. return res;
  1530. res = yt921x_reg_toggle_bits(priv, YT921X_VLAN_IGR_FILTER,
  1531. YT921X_VLAN_IGR_FILTER_PORTn(port),
  1532. vlan_filtering);
  1533. if (res)
  1534. return res;
  1535. res = yt921x_vlan_aware_set(priv, port, vlan_filtering);
  1536. if (res)
  1537. return res;
  1538. return 0;
  1539. }
  1540. static int
  1541. yt921x_vlan_del(struct yt921x_priv *priv, int port, u16 vid)
  1542. {
  1543. u64 mask64;
  1544. mask64 = YT921X_VLAN_CTRL_PORTS(port) |
  1545. YT921X_VLAN_CTRL_UNTAG_PORTn(port);
  1546. return yt921x_reg64_clear_bits(priv, YT921X_VLANn_CTRL(vid), mask64);
  1547. }
  1548. static int
  1549. yt921x_vlan_add(struct yt921x_priv *priv, int port, u16 vid, bool untagged)
  1550. {
  1551. u64 mask64;
  1552. u64 ctrl64;
  1553. mask64 = YT921X_VLAN_CTRL_PORTn(port) |
  1554. YT921X_VLAN_CTRL_PORTS(priv->cpu_ports_mask);
  1555. ctrl64 = mask64;
  1556. mask64 |= YT921X_VLAN_CTRL_UNTAG_PORTn(port);
  1557. if (untagged)
  1558. ctrl64 |= YT921X_VLAN_CTRL_UNTAG_PORTn(port);
  1559. return yt921x_reg64_update_bits(priv, YT921X_VLANn_CTRL(vid),
  1560. mask64, ctrl64);
  1561. }
  1562. static int
  1563. yt921x_pvid_clear(struct yt921x_priv *priv, int port)
  1564. {
  1565. struct dsa_port *dp = dsa_to_port(&priv->ds, port);
  1566. bool vlan_filtering;
  1567. u32 mask;
  1568. int res;
  1569. vlan_filtering = dsa_port_is_vlan_filtering(dp);
  1570. res = yt921x_port_set_pvid(priv, port,
  1571. vlan_filtering ? 0 : YT921X_VID_UNWARE);
  1572. if (res)
  1573. return res;
  1574. if (vlan_filtering) {
  1575. mask = YT921X_PORT_VLAN_CTRL1_CVLAN_DROP_UNTAGGED;
  1576. res = yt921x_reg_set_bits(priv, YT921X_PORTn_VLAN_CTRL1(port),
  1577. mask);
  1578. if (res)
  1579. return res;
  1580. }
  1581. return 0;
  1582. }
  1583. static int
  1584. yt921x_pvid_set(struct yt921x_priv *priv, int port, u16 vid)
  1585. {
  1586. struct dsa_port *dp = dsa_to_port(&priv->ds, port);
  1587. bool vlan_filtering;
  1588. u32 mask;
  1589. int res;
  1590. vlan_filtering = dsa_port_is_vlan_filtering(dp);
  1591. if (vlan_filtering) {
  1592. res = yt921x_port_set_pvid(priv, port, vid);
  1593. if (res)
  1594. return res;
  1595. }
  1596. mask = YT921X_PORT_VLAN_CTRL1_CVLAN_DROP_UNTAGGED;
  1597. res = yt921x_reg_clear_bits(priv, YT921X_PORTn_VLAN_CTRL1(port), mask);
  1598. if (res)
  1599. return res;
  1600. return 0;
  1601. }
  1602. static int
  1603. yt921x_dsa_port_vlan_filtering(struct dsa_switch *ds, int port,
  1604. bool vlan_filtering,
  1605. struct netlink_ext_ack *extack)
  1606. {
  1607. struct yt921x_priv *priv = to_yt921x_priv(ds);
  1608. int res;
  1609. if (dsa_is_cpu_port(ds, port))
  1610. return 0;
  1611. mutex_lock(&priv->reg_lock);
  1612. res = yt921x_vlan_filtering(priv, port, vlan_filtering);
  1613. mutex_unlock(&priv->reg_lock);
  1614. return res;
  1615. }
  1616. static int
  1617. yt921x_dsa_port_vlan_del(struct dsa_switch *ds, int port,
  1618. const struct switchdev_obj_port_vlan *vlan)
  1619. {
  1620. struct yt921x_priv *priv = to_yt921x_priv(ds);
  1621. u16 vid = vlan->vid;
  1622. u16 pvid;
  1623. int res;
  1624. if (dsa_is_cpu_port(ds, port))
  1625. return 0;
  1626. mutex_lock(&priv->reg_lock);
  1627. do {
  1628. struct dsa_port *dp = dsa_to_port(ds, port);
  1629. struct net_device *bdev;
  1630. res = yt921x_vlan_del(priv, port, vid);
  1631. if (res)
  1632. break;
  1633. bdev = dsa_port_bridge_dev_get(dp);
  1634. if (bdev) {
  1635. br_vlan_get_pvid(bdev, &pvid);
  1636. if (pvid == vid)
  1637. res = yt921x_pvid_clear(priv, port);
  1638. }
  1639. } while (0);
  1640. mutex_unlock(&priv->reg_lock);
  1641. return res;
  1642. }
  1643. static int
  1644. yt921x_dsa_port_vlan_add(struct dsa_switch *ds, int port,
  1645. const struct switchdev_obj_port_vlan *vlan,
  1646. struct netlink_ext_ack *extack)
  1647. {
  1648. struct yt921x_priv *priv = to_yt921x_priv(ds);
  1649. u16 vid = vlan->vid;
  1650. u16 pvid;
  1651. int res;
  1652. /* CPU port is supposed to be a member of every VLAN; see
  1653. * yt921x_vlan_add() and yt921x_port_setup()
  1654. */
  1655. if (dsa_is_cpu_port(ds, port))
  1656. return 0;
  1657. mutex_lock(&priv->reg_lock);
  1658. do {
  1659. struct dsa_port *dp = dsa_to_port(ds, port);
  1660. struct net_device *bdev;
  1661. res = yt921x_vlan_add(priv, port, vid,
  1662. vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
  1663. if (res)
  1664. break;
  1665. bdev = dsa_port_bridge_dev_get(dp);
  1666. if (bdev) {
  1667. if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
  1668. res = yt921x_pvid_set(priv, port, vid);
  1669. } else {
  1670. br_vlan_get_pvid(bdev, &pvid);
  1671. if (pvid == vid)
  1672. res = yt921x_pvid_clear(priv, port);
  1673. }
  1674. }
  1675. } while (0);
  1676. mutex_unlock(&priv->reg_lock);
  1677. return res;
  1678. }
  1679. static int yt921x_userport_standalone(struct yt921x_priv *priv, int port)
  1680. {
  1681. u32 mask;
  1682. u32 ctrl;
  1683. int res;
  1684. ctrl = ~priv->cpu_ports_mask;
  1685. res = yt921x_reg_write(priv, YT921X_PORTn_ISOLATION(port), ctrl);
  1686. if (res)
  1687. return res;
  1688. /* Turn off FDB learning to prevent FDB pollution */
  1689. mask = YT921X_PORT_LEARN_DIS;
  1690. res = yt921x_reg_set_bits(priv, YT921X_PORTn_LEARN(port), mask);
  1691. if (res)
  1692. return res;
  1693. /* Turn off VLAN awareness */
  1694. res = yt921x_vlan_aware_set(priv, port, false);
  1695. if (res)
  1696. return res;
  1697. /* Unrelated since learning is off and all packets are trapped;
  1698. * set it anyway
  1699. */
  1700. res = yt921x_port_set_pvid(priv, port, YT921X_VID_UNWARE);
  1701. if (res)
  1702. return res;
  1703. return 0;
  1704. }
  1705. static int yt921x_userport_bridge(struct yt921x_priv *priv, int port)
  1706. {
  1707. u32 mask;
  1708. int res;
  1709. mask = YT921X_PORT_LEARN_DIS;
  1710. res = yt921x_reg_clear_bits(priv, YT921X_PORTn_LEARN(port), mask);
  1711. if (res)
  1712. return res;
  1713. return 0;
  1714. }
  1715. static int yt921x_isolate(struct yt921x_priv *priv, int port)
  1716. {
  1717. u32 mask;
  1718. int res;
  1719. mask = BIT(port);
  1720. for (int i = 0; i < YT921X_PORT_NUM; i++) {
  1721. if ((BIT(i) & priv->cpu_ports_mask) || i == port)
  1722. continue;
  1723. res = yt921x_reg_set_bits(priv, YT921X_PORTn_ISOLATION(i),
  1724. mask);
  1725. if (res)
  1726. return res;
  1727. }
  1728. return 0;
  1729. }
  1730. /* Make sure to include the CPU port in ports_mask, or your bridge will
  1731. * not have it.
  1732. */
  1733. static int yt921x_bridge(struct yt921x_priv *priv, u16 ports_mask)
  1734. {
  1735. unsigned long targets_mask = ports_mask & ~priv->cpu_ports_mask;
  1736. u32 isolated_mask;
  1737. u32 ctrl;
  1738. int port;
  1739. int res;
  1740. isolated_mask = 0;
  1741. for_each_set_bit(port, &targets_mask, YT921X_PORT_NUM) {
  1742. struct yt921x_port *pp = &priv->ports[port];
  1743. if (pp->isolated)
  1744. isolated_mask |= BIT(port);
  1745. }
  1746. /* Block from non-cpu bridge ports ... */
  1747. for_each_set_bit(port, &targets_mask, YT921X_PORT_NUM) {
  1748. struct yt921x_port *pp = &priv->ports[port];
  1749. /* to non-bridge ports */
  1750. ctrl = ~ports_mask;
  1751. /* to isolated ports when isolated */
  1752. if (pp->isolated)
  1753. ctrl |= isolated_mask;
  1754. /* to itself when non-hairpin */
  1755. if (!pp->hairpin)
  1756. ctrl |= BIT(port);
  1757. else
  1758. ctrl &= ~BIT(port);
  1759. res = yt921x_reg_write(priv, YT921X_PORTn_ISOLATION(port),
  1760. ctrl);
  1761. if (res)
  1762. return res;
  1763. }
  1764. return 0;
  1765. }
  1766. static int yt921x_bridge_leave(struct yt921x_priv *priv, int port)
  1767. {
  1768. int res;
  1769. res = yt921x_userport_standalone(priv, port);
  1770. if (res)
  1771. return res;
  1772. res = yt921x_isolate(priv, port);
  1773. if (res)
  1774. return res;
  1775. return 0;
  1776. }
  1777. static int
  1778. yt921x_bridge_join(struct yt921x_priv *priv, int port, u16 ports_mask)
  1779. {
  1780. int res;
  1781. res = yt921x_userport_bridge(priv, port);
  1782. if (res)
  1783. return res;
  1784. res = yt921x_bridge(priv, ports_mask);
  1785. if (res)
  1786. return res;
  1787. return 0;
  1788. }
  1789. static u32
  1790. dsa_bridge_ports(struct dsa_switch *ds, const struct net_device *bdev)
  1791. {
  1792. struct dsa_port *dp;
  1793. u32 mask = 0;
  1794. dsa_switch_for_each_user_port(dp, ds)
  1795. if (dsa_port_offloads_bridge_dev(dp, bdev))
  1796. mask |= BIT(dp->index);
  1797. return mask;
  1798. }
  1799. static int
  1800. yt921x_bridge_flags(struct yt921x_priv *priv, int port,
  1801. struct switchdev_brport_flags flags)
  1802. {
  1803. struct yt921x_port *pp = &priv->ports[port];
  1804. bool do_flush;
  1805. u32 mask;
  1806. int res;
  1807. if (flags.mask & BR_LEARNING) {
  1808. bool learning = flags.val & BR_LEARNING;
  1809. mask = YT921X_PORT_LEARN_DIS;
  1810. res = yt921x_reg_toggle_bits(priv, YT921X_PORTn_LEARN(port),
  1811. mask, !learning);
  1812. if (res)
  1813. return res;
  1814. }
  1815. /* BR_FLOOD, BR_MCAST_FLOOD: see the comment where ACT_UNK_ACTn_TRAP
  1816. * is set
  1817. */
  1818. /* BR_BCAST_FLOOD: we can filter bcast, but cannot trap them */
  1819. do_flush = false;
  1820. if (flags.mask & BR_HAIRPIN_MODE) {
  1821. pp->hairpin = flags.val & BR_HAIRPIN_MODE;
  1822. do_flush = true;
  1823. }
  1824. if (flags.mask & BR_ISOLATED) {
  1825. pp->isolated = flags.val & BR_ISOLATED;
  1826. do_flush = true;
  1827. }
  1828. if (do_flush) {
  1829. struct dsa_switch *ds = &priv->ds;
  1830. struct dsa_port *dp = dsa_to_port(ds, port);
  1831. struct net_device *bdev;
  1832. bdev = dsa_port_bridge_dev_get(dp);
  1833. if (bdev) {
  1834. u32 ports_mask;
  1835. ports_mask = dsa_bridge_ports(ds, bdev);
  1836. ports_mask |= priv->cpu_ports_mask;
  1837. res = yt921x_bridge(priv, ports_mask);
  1838. if (res)
  1839. return res;
  1840. }
  1841. }
  1842. return 0;
  1843. }
  1844. static int
  1845. yt921x_dsa_port_pre_bridge_flags(struct dsa_switch *ds, int port,
  1846. struct switchdev_brport_flags flags,
  1847. struct netlink_ext_ack *extack)
  1848. {
  1849. if (flags.mask & ~(BR_HAIRPIN_MODE | BR_LEARNING | BR_FLOOD |
  1850. BR_MCAST_FLOOD | BR_ISOLATED))
  1851. return -EINVAL;
  1852. return 0;
  1853. }
  1854. static int
  1855. yt921x_dsa_port_bridge_flags(struct dsa_switch *ds, int port,
  1856. struct switchdev_brport_flags flags,
  1857. struct netlink_ext_ack *extack)
  1858. {
  1859. struct yt921x_priv *priv = to_yt921x_priv(ds);
  1860. int res;
  1861. if (dsa_is_cpu_port(ds, port))
  1862. return 0;
  1863. mutex_lock(&priv->reg_lock);
  1864. res = yt921x_bridge_flags(priv, port, flags);
  1865. mutex_unlock(&priv->reg_lock);
  1866. return res;
  1867. }
  1868. static void
  1869. yt921x_dsa_port_bridge_leave(struct dsa_switch *ds, int port,
  1870. struct dsa_bridge bridge)
  1871. {
  1872. struct yt921x_priv *priv = to_yt921x_priv(ds);
  1873. struct device *dev = to_device(priv);
  1874. int res;
  1875. if (dsa_is_cpu_port(ds, port))
  1876. return;
  1877. mutex_lock(&priv->reg_lock);
  1878. res = yt921x_bridge_leave(priv, port);
  1879. mutex_unlock(&priv->reg_lock);
  1880. if (res)
  1881. dev_err(dev, "Failed to %s port %d: %i\n", "unbridge",
  1882. port, res);
  1883. }
  1884. static int
  1885. yt921x_dsa_port_bridge_join(struct dsa_switch *ds, int port,
  1886. struct dsa_bridge bridge, bool *tx_fwd_offload,
  1887. struct netlink_ext_ack *extack)
  1888. {
  1889. struct yt921x_priv *priv = to_yt921x_priv(ds);
  1890. u16 ports_mask;
  1891. int res;
  1892. if (dsa_is_cpu_port(ds, port))
  1893. return 0;
  1894. ports_mask = dsa_bridge_ports(ds, bridge.dev);
  1895. ports_mask |= priv->cpu_ports_mask;
  1896. mutex_lock(&priv->reg_lock);
  1897. res = yt921x_bridge_join(priv, port, ports_mask);
  1898. mutex_unlock(&priv->reg_lock);
  1899. return res;
  1900. }
  1901. static int
  1902. yt921x_dsa_port_mst_state_set(struct dsa_switch *ds, int port,
  1903. const struct switchdev_mst_state *st)
  1904. {
  1905. struct yt921x_priv *priv = to_yt921x_priv(ds);
  1906. u32 mask;
  1907. u32 ctrl;
  1908. int res;
  1909. mask = YT921X_STP_PORTn_M(port);
  1910. switch (st->state) {
  1911. case BR_STATE_DISABLED:
  1912. ctrl = YT921X_STP_PORTn_DISABLED(port);
  1913. break;
  1914. case BR_STATE_LISTENING:
  1915. case BR_STATE_LEARNING:
  1916. ctrl = YT921X_STP_PORTn_LEARNING(port);
  1917. break;
  1918. case BR_STATE_FORWARDING:
  1919. default:
  1920. ctrl = YT921X_STP_PORTn_FORWARD(port);
  1921. break;
  1922. case BR_STATE_BLOCKING:
  1923. ctrl = YT921X_STP_PORTn_BLOCKING(port);
  1924. break;
  1925. }
  1926. mutex_lock(&priv->reg_lock);
  1927. res = yt921x_reg_update_bits(priv, YT921X_STPn(st->msti), mask, ctrl);
  1928. mutex_unlock(&priv->reg_lock);
  1929. return res;
  1930. }
  1931. static int
  1932. yt921x_dsa_vlan_msti_set(struct dsa_switch *ds, struct dsa_bridge bridge,
  1933. const struct switchdev_vlan_msti *msti)
  1934. {
  1935. struct yt921x_priv *priv = to_yt921x_priv(ds);
  1936. u64 mask64;
  1937. u64 ctrl64;
  1938. int res;
  1939. if (!msti->vid)
  1940. return -EINVAL;
  1941. if (!msti->msti || msti->msti >= YT921X_MSTI_NUM)
  1942. return -EINVAL;
  1943. mask64 = YT921X_VLAN_CTRL_STP_ID_M;
  1944. ctrl64 = YT921X_VLAN_CTRL_STP_ID(msti->msti);
  1945. mutex_lock(&priv->reg_lock);
  1946. res = yt921x_reg64_update_bits(priv, YT921X_VLANn_CTRL(msti->vid),
  1947. mask64, ctrl64);
  1948. mutex_unlock(&priv->reg_lock);
  1949. return res;
  1950. }
  1951. static void
  1952. yt921x_dsa_port_stp_state_set(struct dsa_switch *ds, int port, u8 state)
  1953. {
  1954. struct yt921x_priv *priv = to_yt921x_priv(ds);
  1955. struct dsa_port *dp = dsa_to_port(ds, port);
  1956. struct device *dev = to_device(priv);
  1957. bool learning;
  1958. u32 mask;
  1959. u32 ctrl;
  1960. int res;
  1961. mask = YT921X_STP_PORTn_M(port);
  1962. learning = false;
  1963. switch (state) {
  1964. case BR_STATE_DISABLED:
  1965. ctrl = YT921X_STP_PORTn_DISABLED(port);
  1966. break;
  1967. case BR_STATE_LISTENING:
  1968. ctrl = YT921X_STP_PORTn_LEARNING(port);
  1969. break;
  1970. case BR_STATE_LEARNING:
  1971. ctrl = YT921X_STP_PORTn_LEARNING(port);
  1972. learning = dp->learning;
  1973. break;
  1974. case BR_STATE_FORWARDING:
  1975. default:
  1976. ctrl = YT921X_STP_PORTn_FORWARD(port);
  1977. learning = dp->learning;
  1978. break;
  1979. case BR_STATE_BLOCKING:
  1980. ctrl = YT921X_STP_PORTn_BLOCKING(port);
  1981. break;
  1982. }
  1983. mutex_lock(&priv->reg_lock);
  1984. do {
  1985. res = yt921x_reg_update_bits(priv, YT921X_STPn(0), mask, ctrl);
  1986. if (res)
  1987. break;
  1988. mask = YT921X_PORT_LEARN_DIS;
  1989. ctrl = !learning ? YT921X_PORT_LEARN_DIS : 0;
  1990. res = yt921x_reg_update_bits(priv, YT921X_PORTn_LEARN(port),
  1991. mask, ctrl);
  1992. } while (0);
  1993. mutex_unlock(&priv->reg_lock);
  1994. if (res)
  1995. dev_err(dev, "Failed to %s port %d: %i\n", "set STP state for",
  1996. port, res);
  1997. }
  1998. static int __maybe_unused
  1999. yt921x_dsa_port_get_default_prio(struct dsa_switch *ds, int port)
  2000. {
  2001. struct yt921x_priv *priv = to_yt921x_priv(ds);
  2002. u32 val;
  2003. int res;
  2004. mutex_lock(&priv->reg_lock);
  2005. res = yt921x_reg_read(priv, YT921X_PORTn_QOS(port), &val);
  2006. mutex_unlock(&priv->reg_lock);
  2007. if (res)
  2008. return res;
  2009. return FIELD_GET(YT921X_PORT_QOS_PRIO_M, val);
  2010. }
  2011. static int __maybe_unused
  2012. yt921x_dsa_port_set_default_prio(struct dsa_switch *ds, int port, u8 prio)
  2013. {
  2014. struct yt921x_priv *priv = to_yt921x_priv(ds);
  2015. u32 mask;
  2016. u32 ctrl;
  2017. int res;
  2018. if (prio >= YT921X_PRIO_NUM)
  2019. return -EINVAL;
  2020. mutex_lock(&priv->reg_lock);
  2021. mask = YT921X_PORT_QOS_PRIO_M | YT921X_PORT_QOS_PRIO_EN;
  2022. ctrl = YT921X_PORT_QOS_PRIO(prio) | YT921X_PORT_QOS_PRIO_EN;
  2023. res = yt921x_reg_update_bits(priv, YT921X_PORTn_QOS(port), mask, ctrl);
  2024. mutex_unlock(&priv->reg_lock);
  2025. return res;
  2026. }
  2027. static int __maybe_unused appprios_cmp(const void *a, const void *b)
  2028. {
  2029. return ((const u8 *)b)[1] - ((const u8 *)a)[1];
  2030. }
  2031. static int __maybe_unused
  2032. yt921x_dsa_port_get_apptrust(struct dsa_switch *ds, int port, u8 *sel,
  2033. int *nselp)
  2034. {
  2035. struct yt921x_priv *priv = to_yt921x_priv(ds);
  2036. u8 appprios[2][2] = {};
  2037. int nsel;
  2038. u32 val;
  2039. int res;
  2040. mutex_lock(&priv->reg_lock);
  2041. res = yt921x_reg_read(priv, YT921X_PORTn_PRIO_ORD(port), &val);
  2042. mutex_unlock(&priv->reg_lock);
  2043. if (res)
  2044. return res;
  2045. appprios[0][0] = IEEE_8021QAZ_APP_SEL_DSCP;
  2046. appprios[0][1] = (val >> (3 * YT921X_APP_SEL_DSCP)) & 7;
  2047. appprios[1][0] = DCB_APP_SEL_PCP;
  2048. appprios[1][1] = (val >> (3 * YT921X_APP_SEL_CVLAN_PCP)) & 7;
  2049. sort(appprios, ARRAY_SIZE(appprios), sizeof(appprios[0]), appprios_cmp,
  2050. NULL);
  2051. nsel = 0;
  2052. for (int i = 0; i < ARRAY_SIZE(appprios) && appprios[i][1]; i++) {
  2053. sel[nsel] = appprios[i][0];
  2054. nsel++;
  2055. }
  2056. *nselp = nsel;
  2057. return 0;
  2058. }
  2059. static int __maybe_unused
  2060. yt921x_dsa_port_set_apptrust(struct dsa_switch *ds, int port, const u8 *sel,
  2061. int nsel)
  2062. {
  2063. struct yt921x_priv *priv = to_yt921x_priv(ds);
  2064. struct device *dev = to_device(priv);
  2065. u32 ctrl;
  2066. int res;
  2067. if (nsel > YT921X_APP_SEL_NUM)
  2068. return -EINVAL;
  2069. ctrl = 0;
  2070. for (int i = 0; i < nsel; i++) {
  2071. switch (sel[i]) {
  2072. case IEEE_8021QAZ_APP_SEL_DSCP:
  2073. ctrl |= YT921X_PORT_PRIO_ORD_APPm(YT921X_APP_SEL_DSCP,
  2074. 7 - i);
  2075. break;
  2076. case DCB_APP_SEL_PCP:
  2077. ctrl |= YT921X_PORT_PRIO_ORD_APPm(YT921X_APP_SEL_CVLAN_PCP,
  2078. 7 - i);
  2079. ctrl |= YT921X_PORT_PRIO_ORD_APPm(YT921X_APP_SEL_SVLAN_PCP,
  2080. 7 - i);
  2081. break;
  2082. default:
  2083. dev_err(dev,
  2084. "Invalid apptrust selector (at %d-th). Supported: dscp, pcp\n",
  2085. i + 1);
  2086. return -EOPNOTSUPP;
  2087. }
  2088. }
  2089. mutex_lock(&priv->reg_lock);
  2090. res = yt921x_reg_write(priv, YT921X_PORTn_PRIO_ORD(port), ctrl);
  2091. mutex_unlock(&priv->reg_lock);
  2092. return res;
  2093. }
  2094. static int yt921x_port_down(struct yt921x_priv *priv, int port)
  2095. {
  2096. u32 mask;
  2097. int res;
  2098. mask = YT921X_PORT_LINK | YT921X_PORT_RX_MAC_EN | YT921X_PORT_TX_MAC_EN;
  2099. res = yt921x_reg_clear_bits(priv, YT921X_PORTn_CTRL(port), mask);
  2100. if (res)
  2101. return res;
  2102. if (yt921x_port_is_external(port)) {
  2103. mask = YT921X_SERDES_LINK;
  2104. res = yt921x_reg_clear_bits(priv, YT921X_SERDESn(port), mask);
  2105. if (res)
  2106. return res;
  2107. mask = YT921X_XMII_LINK;
  2108. res = yt921x_reg_clear_bits(priv, YT921X_XMIIn(port), mask);
  2109. if (res)
  2110. return res;
  2111. }
  2112. return 0;
  2113. }
  2114. static int
  2115. yt921x_port_up(struct yt921x_priv *priv, int port, unsigned int mode,
  2116. phy_interface_t interface, int speed, int duplex,
  2117. bool tx_pause, bool rx_pause)
  2118. {
  2119. u32 mask;
  2120. u32 ctrl;
  2121. int res;
  2122. switch (speed) {
  2123. case SPEED_10:
  2124. ctrl = YT921X_PORT_SPEED_10;
  2125. break;
  2126. case SPEED_100:
  2127. ctrl = YT921X_PORT_SPEED_100;
  2128. break;
  2129. case SPEED_1000:
  2130. ctrl = YT921X_PORT_SPEED_1000;
  2131. break;
  2132. case SPEED_2500:
  2133. ctrl = YT921X_PORT_SPEED_2500;
  2134. break;
  2135. case SPEED_10000:
  2136. ctrl = YT921X_PORT_SPEED_10000;
  2137. break;
  2138. default:
  2139. return -EINVAL;
  2140. }
  2141. if (duplex == DUPLEX_FULL)
  2142. ctrl |= YT921X_PORT_DUPLEX_FULL;
  2143. if (tx_pause)
  2144. ctrl |= YT921X_PORT_TX_PAUSE;
  2145. if (rx_pause)
  2146. ctrl |= YT921X_PORT_RX_PAUSE;
  2147. ctrl |= YT921X_PORT_RX_MAC_EN | YT921X_PORT_TX_MAC_EN;
  2148. res = yt921x_reg_write(priv, YT921X_PORTn_CTRL(port), ctrl);
  2149. if (res)
  2150. return res;
  2151. if (yt921x_port_is_external(port)) {
  2152. mask = YT921X_SERDES_SPEED_M;
  2153. switch (speed) {
  2154. case SPEED_10:
  2155. ctrl = YT921X_SERDES_SPEED_10;
  2156. break;
  2157. case SPEED_100:
  2158. ctrl = YT921X_SERDES_SPEED_100;
  2159. break;
  2160. case SPEED_1000:
  2161. ctrl = YT921X_SERDES_SPEED_1000;
  2162. break;
  2163. case SPEED_2500:
  2164. ctrl = YT921X_SERDES_SPEED_2500;
  2165. break;
  2166. case SPEED_10000:
  2167. ctrl = YT921X_SERDES_SPEED_10000;
  2168. break;
  2169. default:
  2170. return -EINVAL;
  2171. }
  2172. mask |= YT921X_SERDES_DUPLEX_FULL;
  2173. if (duplex == DUPLEX_FULL)
  2174. ctrl |= YT921X_SERDES_DUPLEX_FULL;
  2175. mask |= YT921X_SERDES_TX_PAUSE;
  2176. if (tx_pause)
  2177. ctrl |= YT921X_SERDES_TX_PAUSE;
  2178. mask |= YT921X_SERDES_RX_PAUSE;
  2179. if (rx_pause)
  2180. ctrl |= YT921X_SERDES_RX_PAUSE;
  2181. mask |= YT921X_SERDES_LINK;
  2182. ctrl |= YT921X_SERDES_LINK;
  2183. res = yt921x_reg_update_bits(priv, YT921X_SERDESn(port),
  2184. mask, ctrl);
  2185. if (res)
  2186. return res;
  2187. mask = YT921X_XMII_LINK;
  2188. res = yt921x_reg_set_bits(priv, YT921X_XMIIn(port), mask);
  2189. if (res)
  2190. return res;
  2191. switch (speed) {
  2192. case SPEED_10:
  2193. ctrl = YT921X_MDIO_POLLING_SPEED_10;
  2194. break;
  2195. case SPEED_100:
  2196. ctrl = YT921X_MDIO_POLLING_SPEED_100;
  2197. break;
  2198. case SPEED_1000:
  2199. ctrl = YT921X_MDIO_POLLING_SPEED_1000;
  2200. break;
  2201. case SPEED_2500:
  2202. ctrl = YT921X_MDIO_POLLING_SPEED_2500;
  2203. break;
  2204. case SPEED_10000:
  2205. ctrl = YT921X_MDIO_POLLING_SPEED_10000;
  2206. break;
  2207. default:
  2208. return -EINVAL;
  2209. }
  2210. if (duplex == DUPLEX_FULL)
  2211. ctrl |= YT921X_MDIO_POLLING_DUPLEX_FULL;
  2212. ctrl |= YT921X_MDIO_POLLING_LINK;
  2213. res = yt921x_reg_write(priv, YT921X_MDIO_POLLINGn(port), ctrl);
  2214. if (res)
  2215. return res;
  2216. }
  2217. return 0;
  2218. }
  2219. static int
  2220. yt921x_port_config(struct yt921x_priv *priv, int port, unsigned int mode,
  2221. phy_interface_t interface)
  2222. {
  2223. struct device *dev = to_device(priv);
  2224. u32 mask;
  2225. u32 ctrl;
  2226. int res;
  2227. if (!yt921x_port_is_external(port)) {
  2228. if (interface != PHY_INTERFACE_MODE_INTERNAL) {
  2229. dev_err(dev, "Wrong mode %d on port %d\n",
  2230. interface, port);
  2231. return -EINVAL;
  2232. }
  2233. return 0;
  2234. }
  2235. switch (interface) {
  2236. /* SERDES */
  2237. case PHY_INTERFACE_MODE_SGMII:
  2238. case PHY_INTERFACE_MODE_100BASEX:
  2239. case PHY_INTERFACE_MODE_1000BASEX:
  2240. case PHY_INTERFACE_MODE_2500BASEX:
  2241. mask = YT921X_SERDES_CTRL_PORTn(port);
  2242. res = yt921x_reg_set_bits(priv, YT921X_SERDES_CTRL, mask);
  2243. if (res)
  2244. return res;
  2245. mask = YT921X_XMII_CTRL_PORTn(port);
  2246. res = yt921x_reg_clear_bits(priv, YT921X_XMII_CTRL, mask);
  2247. if (res)
  2248. return res;
  2249. mask = YT921X_SERDES_MODE_M;
  2250. switch (interface) {
  2251. case PHY_INTERFACE_MODE_SGMII:
  2252. ctrl = YT921X_SERDES_MODE_SGMII;
  2253. break;
  2254. case PHY_INTERFACE_MODE_100BASEX:
  2255. ctrl = YT921X_SERDES_MODE_100BASEX;
  2256. break;
  2257. case PHY_INTERFACE_MODE_1000BASEX:
  2258. ctrl = YT921X_SERDES_MODE_1000BASEX;
  2259. break;
  2260. case PHY_INTERFACE_MODE_2500BASEX:
  2261. ctrl = YT921X_SERDES_MODE_2500BASEX;
  2262. break;
  2263. default:
  2264. return -EINVAL;
  2265. }
  2266. res = yt921x_reg_update_bits(priv, YT921X_SERDESn(port),
  2267. mask, ctrl);
  2268. if (res)
  2269. return res;
  2270. break;
  2271. /* add XMII support here */
  2272. default:
  2273. return -EINVAL;
  2274. }
  2275. return 0;
  2276. }
  2277. static void
  2278. yt921x_phylink_mac_link_down(struct phylink_config *config, unsigned int mode,
  2279. phy_interface_t interface)
  2280. {
  2281. struct dsa_port *dp = dsa_phylink_to_port(config);
  2282. struct yt921x_priv *priv = to_yt921x_priv(dp->ds);
  2283. int port = dp->index;
  2284. int res;
  2285. /* No need to sync; port control block is hold until device remove */
  2286. cancel_delayed_work(&priv->ports[port].mib_read);
  2287. mutex_lock(&priv->reg_lock);
  2288. res = yt921x_port_down(priv, port);
  2289. mutex_unlock(&priv->reg_lock);
  2290. if (res)
  2291. dev_err(dp->ds->dev, "Failed to %s port %d: %i\n", "bring down",
  2292. port, res);
  2293. }
  2294. static void
  2295. yt921x_phylink_mac_link_up(struct phylink_config *config,
  2296. struct phy_device *phydev, unsigned int mode,
  2297. phy_interface_t interface, int speed, int duplex,
  2298. bool tx_pause, bool rx_pause)
  2299. {
  2300. struct dsa_port *dp = dsa_phylink_to_port(config);
  2301. struct yt921x_priv *priv = to_yt921x_priv(dp->ds);
  2302. int port = dp->index;
  2303. int res;
  2304. mutex_lock(&priv->reg_lock);
  2305. res = yt921x_port_up(priv, port, mode, interface, speed, duplex,
  2306. tx_pause, rx_pause);
  2307. mutex_unlock(&priv->reg_lock);
  2308. if (res)
  2309. dev_err(dp->ds->dev, "Failed to %s port %d: %i\n", "bring up",
  2310. port, res);
  2311. schedule_delayed_work(&priv->ports[port].mib_read, 0);
  2312. }
  2313. static void
  2314. yt921x_phylink_mac_config(struct phylink_config *config, unsigned int mode,
  2315. const struct phylink_link_state *state)
  2316. {
  2317. struct dsa_port *dp = dsa_phylink_to_port(config);
  2318. struct yt921x_priv *priv = to_yt921x_priv(dp->ds);
  2319. int port = dp->index;
  2320. int res;
  2321. mutex_lock(&priv->reg_lock);
  2322. res = yt921x_port_config(priv, port, mode, state->interface);
  2323. mutex_unlock(&priv->reg_lock);
  2324. if (res)
  2325. dev_err(dp->ds->dev, "Failed to %s port %d: %i\n", "config",
  2326. port, res);
  2327. }
  2328. static void
  2329. yt921x_dsa_phylink_get_caps(struct dsa_switch *ds, int port,
  2330. struct phylink_config *config)
  2331. {
  2332. struct yt921x_priv *priv = to_yt921x_priv(ds);
  2333. const struct yt921x_info *info = priv->info;
  2334. config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
  2335. MAC_10 | MAC_100 | MAC_1000;
  2336. if (info->internal_mask & BIT(port)) {
  2337. /* Port 10 for MCU should probably go here too. But since that
  2338. * is untested yet, turn it down for the moment by letting it
  2339. * fall to the default branch.
  2340. */
  2341. __set_bit(PHY_INTERFACE_MODE_INTERNAL,
  2342. config->supported_interfaces);
  2343. } else if (info->external_mask & BIT(port)) {
  2344. /* TODO: external ports may support SERDES only, XMII only, or
  2345. * SERDES + XMII depending on the chip. However, we can't get
  2346. * the accurate config table due to lack of document, thus
  2347. * we simply declare SERDES + XMII and rely on the correctness
  2348. * of devicetree for now.
  2349. */
  2350. /* SERDES */
  2351. __set_bit(PHY_INTERFACE_MODE_SGMII,
  2352. config->supported_interfaces);
  2353. /* REVSGMII (SGMII in PHY role) should go here, once
  2354. * PHY_INTERFACE_MODE_REVSGMII is introduced.
  2355. */
  2356. __set_bit(PHY_INTERFACE_MODE_100BASEX,
  2357. config->supported_interfaces);
  2358. __set_bit(PHY_INTERFACE_MODE_1000BASEX,
  2359. config->supported_interfaces);
  2360. __set_bit(PHY_INTERFACE_MODE_2500BASEX,
  2361. config->supported_interfaces);
  2362. config->mac_capabilities |= MAC_2500FD;
  2363. /* XMII */
  2364. /* Not tested. To add support for XMII:
  2365. * - Add proper interface modes below
  2366. * - Handle them in yt921x_port_config()
  2367. */
  2368. }
  2369. /* no such port: empty supported_interfaces causes phylink to turn it
  2370. * down
  2371. */
  2372. }
  2373. static int yt921x_port_setup(struct yt921x_priv *priv, int port)
  2374. {
  2375. struct dsa_switch *ds = &priv->ds;
  2376. u32 ctrl;
  2377. int res;
  2378. res = yt921x_userport_standalone(priv, port);
  2379. if (res)
  2380. return res;
  2381. /* Clear prio order (even if DCB is not enabled) to avoid unsolicited
  2382. * priorities
  2383. */
  2384. res = yt921x_reg_write(priv, YT921X_PORTn_PRIO_ORD(port), 0);
  2385. if (res)
  2386. return res;
  2387. if (dsa_is_cpu_port(ds, port)) {
  2388. /* Egress of CPU port is supposed to be completely controlled
  2389. * via tagging, so set to oneway isolated (drop all packets
  2390. * without tag).
  2391. */
  2392. ctrl = ~(u32)0;
  2393. res = yt921x_reg_write(priv, YT921X_PORTn_ISOLATION(port),
  2394. ctrl);
  2395. if (res)
  2396. return res;
  2397. /* To simplify FDB "isolation" simulation, we also disable
  2398. * learning on the CPU port, and let software identify packets
  2399. * towarding CPU (either trapped or a static FDB entry is
  2400. * matched, no matter which bridge that entry is for), which is
  2401. * already done by yt921x_userport_standalone(). As a result,
  2402. * VLAN-awareness becomes unrelated on the CPU port (set to
  2403. * VLAN-unaware by the way).
  2404. */
  2405. }
  2406. return 0;
  2407. }
  2408. static enum dsa_tag_protocol
  2409. yt921x_dsa_get_tag_protocol(struct dsa_switch *ds, int port,
  2410. enum dsa_tag_protocol m)
  2411. {
  2412. return DSA_TAG_PROTO_YT921X;
  2413. }
  2414. static int yt921x_dsa_port_setup(struct dsa_switch *ds, int port)
  2415. {
  2416. struct yt921x_priv *priv = to_yt921x_priv(ds);
  2417. int res;
  2418. mutex_lock(&priv->reg_lock);
  2419. res = yt921x_port_setup(priv, port);
  2420. mutex_unlock(&priv->reg_lock);
  2421. return res;
  2422. }
  2423. /* Not "port" - DSCP mapping is global */
  2424. static int __maybe_unused
  2425. yt921x_dsa_port_get_dscp_prio(struct dsa_switch *ds, int port, u8 dscp)
  2426. {
  2427. struct yt921x_priv *priv = to_yt921x_priv(ds);
  2428. u32 val;
  2429. int res;
  2430. mutex_lock(&priv->reg_lock);
  2431. res = yt921x_reg_read(priv, YT921X_IPM_DSCPn(dscp), &val);
  2432. mutex_unlock(&priv->reg_lock);
  2433. if (res)
  2434. return res;
  2435. return FIELD_GET(YT921X_IPM_PRIO_M, val);
  2436. }
  2437. static int __maybe_unused
  2438. yt921x_dsa_port_del_dscp_prio(struct dsa_switch *ds, int port, u8 dscp, u8 prio)
  2439. {
  2440. struct yt921x_priv *priv = to_yt921x_priv(ds);
  2441. u32 val;
  2442. int res;
  2443. mutex_lock(&priv->reg_lock);
  2444. /* During a "dcb app replace" command, the new app table entry will be
  2445. * added first, then the old one will be deleted. But the hardware only
  2446. * supports one QoS class per DSCP value (duh), so if we blindly delete
  2447. * the app table entry for this DSCP value, we end up deleting the
  2448. * entry with the new priority. Avoid that by checking whether user
  2449. * space wants to delete the priority which is currently configured, or
  2450. * something else which is no longer current.
  2451. */
  2452. res = yt921x_reg_read(priv, YT921X_IPM_DSCPn(dscp), &val);
  2453. if (!res && FIELD_GET(YT921X_IPM_PRIO_M, val) == prio)
  2454. res = yt921x_reg_write(priv, YT921X_IPM_DSCPn(dscp),
  2455. YT921X_IPM_PRIO(IEEE8021Q_TT_BK));
  2456. mutex_unlock(&priv->reg_lock);
  2457. return res;
  2458. }
  2459. static int __maybe_unused
  2460. yt921x_dsa_port_add_dscp_prio(struct dsa_switch *ds, int port, u8 dscp, u8 prio)
  2461. {
  2462. struct yt921x_priv *priv = to_yt921x_priv(ds);
  2463. int res;
  2464. if (prio >= YT921X_PRIO_NUM)
  2465. return -EINVAL;
  2466. mutex_lock(&priv->reg_lock);
  2467. res = yt921x_reg_write(priv, YT921X_IPM_DSCPn(dscp),
  2468. YT921X_IPM_PRIO(prio));
  2469. mutex_unlock(&priv->reg_lock);
  2470. return res;
  2471. }
  2472. static int yt921x_edata_wait(struct yt921x_priv *priv, u32 *valp)
  2473. {
  2474. u32 val = YT921X_EDATA_DATA_IDLE;
  2475. int res;
  2476. res = yt921x_reg_wait(priv, YT921X_EDATA_DATA,
  2477. YT921X_EDATA_DATA_STATUS_M, &val);
  2478. if (res)
  2479. return res;
  2480. *valp = val;
  2481. return 0;
  2482. }
  2483. static int
  2484. yt921x_edata_read_cont(struct yt921x_priv *priv, u8 addr, u8 *valp)
  2485. {
  2486. u32 ctrl;
  2487. u32 val;
  2488. int res;
  2489. ctrl = YT921X_EDATA_CTRL_ADDR(addr) | YT921X_EDATA_CTRL_READ;
  2490. res = yt921x_reg_write(priv, YT921X_EDATA_CTRL, ctrl);
  2491. if (res)
  2492. return res;
  2493. res = yt921x_edata_wait(priv, &val);
  2494. if (res)
  2495. return res;
  2496. *valp = FIELD_GET(YT921X_EDATA_DATA_DATA_M, val);
  2497. return 0;
  2498. }
  2499. static int yt921x_edata_read(struct yt921x_priv *priv, u8 addr, u8 *valp)
  2500. {
  2501. u32 val;
  2502. int res;
  2503. res = yt921x_edata_wait(priv, &val);
  2504. if (res)
  2505. return res;
  2506. return yt921x_edata_read_cont(priv, addr, valp);
  2507. }
  2508. static int yt921x_chip_detect(struct yt921x_priv *priv)
  2509. {
  2510. struct device *dev = to_device(priv);
  2511. const struct yt921x_info *info;
  2512. u8 extmode;
  2513. u32 chipid;
  2514. u32 major;
  2515. u32 mode;
  2516. int res;
  2517. res = yt921x_reg_read(priv, YT921X_CHIP_ID, &chipid);
  2518. if (res)
  2519. return res;
  2520. major = FIELD_GET(YT921X_CHIP_ID_MAJOR, chipid);
  2521. for (info = yt921x_infos; info->name; info++)
  2522. if (info->major == major)
  2523. break;
  2524. if (!info->name) {
  2525. dev_err(dev, "Unexpected chipid 0x%x\n", chipid);
  2526. return -ENODEV;
  2527. }
  2528. res = yt921x_reg_read(priv, YT921X_CHIP_MODE, &mode);
  2529. if (res)
  2530. return res;
  2531. res = yt921x_edata_read(priv, YT921X_EDATA_EXTMODE, &extmode);
  2532. if (res)
  2533. return res;
  2534. for (; info->name; info++)
  2535. if (info->major == major && info->mode == mode &&
  2536. info->extmode == extmode)
  2537. break;
  2538. if (!info->name) {
  2539. dev_err(dev,
  2540. "Unsupported chipid 0x%x with chipmode 0x%x 0x%x\n",
  2541. chipid, mode, extmode);
  2542. return -ENODEV;
  2543. }
  2544. /* Print chipid here since we are interested in lower 16 bits */
  2545. dev_info(dev,
  2546. "Motorcomm %s ethernet switch, chipid: 0x%x, chipmode: 0x%x 0x%x\n",
  2547. info->name, chipid, mode, extmode);
  2548. priv->info = info;
  2549. return 0;
  2550. }
  2551. static int yt921x_chip_reset(struct yt921x_priv *priv)
  2552. {
  2553. struct device *dev = to_device(priv);
  2554. u16 eth_p_tag;
  2555. u32 val;
  2556. int res;
  2557. res = yt921x_chip_detect(priv);
  2558. if (res)
  2559. return res;
  2560. /* Reset */
  2561. res = yt921x_reg_write(priv, YT921X_RST, YT921X_RST_HW);
  2562. if (res)
  2563. return res;
  2564. /* RST_HW is almost same as GPIO hard reset, so we need this delay. */
  2565. fsleep(YT921X_RST_DELAY_US);
  2566. val = 0;
  2567. res = yt921x_reg_wait(priv, YT921X_RST, ~0, &val);
  2568. if (res)
  2569. return res;
  2570. /* Check for tag EtherType; do it after reset in case you messed it up
  2571. * before.
  2572. */
  2573. res = yt921x_reg_read(priv, YT921X_CPU_TAG_TPID, &val);
  2574. if (res)
  2575. return res;
  2576. eth_p_tag = FIELD_GET(YT921X_CPU_TAG_TPID_TPID_M, val);
  2577. if (eth_p_tag != ETH_P_YT921X) {
  2578. dev_err(dev, "Tag type 0x%x != 0x%x\n", eth_p_tag,
  2579. ETH_P_YT921X);
  2580. /* Despite being possible, we choose not to set CPU_TAG_TPID,
  2581. * since there is no way it can be different unless you have the
  2582. * wrong chip.
  2583. */
  2584. return -EINVAL;
  2585. }
  2586. return 0;
  2587. }
  2588. static int yt921x_chip_setup_dsa(struct yt921x_priv *priv)
  2589. {
  2590. struct dsa_switch *ds = &priv->ds;
  2591. unsigned long cpu_ports_mask;
  2592. u64 ctrl64;
  2593. u32 ctrl;
  2594. int port;
  2595. int res;
  2596. /* Enable DSA */
  2597. priv->cpu_ports_mask = dsa_cpu_ports(ds);
  2598. ctrl = YT921X_EXT_CPU_PORT_TAG_EN | YT921X_EXT_CPU_PORT_PORT_EN |
  2599. YT921X_EXT_CPU_PORT_PORT(__ffs(priv->cpu_ports_mask));
  2600. res = yt921x_reg_write(priv, YT921X_EXT_CPU_PORT, ctrl);
  2601. if (res)
  2602. return res;
  2603. /* Setup software switch */
  2604. ctrl = YT921X_CPU_COPY_TO_EXT_CPU;
  2605. res = yt921x_reg_write(priv, YT921X_CPU_COPY, ctrl);
  2606. if (res)
  2607. return res;
  2608. ctrl = GENMASK(10, 0);
  2609. res = yt921x_reg_write(priv, YT921X_FILTER_UNK_UCAST, ctrl);
  2610. if (res)
  2611. return res;
  2612. res = yt921x_reg_write(priv, YT921X_FILTER_UNK_MCAST, ctrl);
  2613. if (res)
  2614. return res;
  2615. /* YT921x does not support native DSA port bridging, so we use port
  2616. * isolation to emulate it. However, be especially careful that port
  2617. * isolation takes _after_ FDB lookups, i.e. if an FDB entry (from
  2618. * another bridge) is matched and the destination port (in another
  2619. * bridge) is blocked, the packet will be dropped instead of flooding to
  2620. * the "bridged" ports, thus we need to trap and handle those packets by
  2621. * software.
  2622. *
  2623. * If there is no more than one bridge, we might be able to drop them
  2624. * directly given some conditions are met, but we trap them in all cases
  2625. * for now.
  2626. */
  2627. ctrl = 0;
  2628. for (int i = 0; i < YT921X_PORT_NUM; i++)
  2629. ctrl |= YT921X_ACT_UNK_ACTn_TRAP(i);
  2630. /* Except for CPU ports, if any packets are sent via CPU ports without
  2631. * tag, they should be dropped.
  2632. */
  2633. cpu_ports_mask = priv->cpu_ports_mask;
  2634. for_each_set_bit(port, &cpu_ports_mask, YT921X_PORT_NUM) {
  2635. ctrl &= ~YT921X_ACT_UNK_ACTn_M(port);
  2636. ctrl |= YT921X_ACT_UNK_ACTn_DROP(port);
  2637. }
  2638. res = yt921x_reg_write(priv, YT921X_ACT_UNK_UCAST, ctrl);
  2639. if (res)
  2640. return res;
  2641. res = yt921x_reg_write(priv, YT921X_ACT_UNK_MCAST, ctrl);
  2642. if (res)
  2643. return res;
  2644. /* Tagged VID 0 should be treated as untagged, which confuses the
  2645. * hardware a lot
  2646. */
  2647. ctrl64 = YT921X_VLAN_CTRL_LEARN_DIS | YT921X_VLAN_CTRL_PORTS_M;
  2648. res = yt921x_reg64_write(priv, YT921X_VLANn_CTRL(0), ctrl64);
  2649. if (res)
  2650. return res;
  2651. return 0;
  2652. }
  2653. static int __maybe_unused yt921x_chip_setup_qos(struct yt921x_priv *priv)
  2654. {
  2655. u32 ctrl;
  2656. int res;
  2657. /* DSCP to internal priorities */
  2658. for (u8 dscp = 0; dscp < DSCP_MAX; dscp++) {
  2659. int prio = ietf_dscp_to_ieee8021q_tt(dscp);
  2660. if (prio < 0)
  2661. return prio;
  2662. res = yt921x_reg_write(priv, YT921X_IPM_DSCPn(dscp),
  2663. YT921X_IPM_PRIO(prio));
  2664. if (res)
  2665. return res;
  2666. }
  2667. /* 802.1Q QoS to internal priorities */
  2668. for (u8 pcp = 0; pcp < 8; pcp++)
  2669. for (u8 dei = 0; dei < 2; dei++) {
  2670. ctrl = YT921X_IPM_PRIO(pcp);
  2671. if (dei)
  2672. /* "Red" almost means drop, so it's not that
  2673. * useful. Note that tc police does not support
  2674. * Three-Color very well
  2675. */
  2676. ctrl |= YT921X_IPM_COLOR_YELLOW;
  2677. for (u8 svlan = 0; svlan < 2; svlan++) {
  2678. u32 reg = YT921X_IPM_PCPn(svlan, dei, pcp);
  2679. res = yt921x_reg_write(priv, reg, ctrl);
  2680. if (res)
  2681. return res;
  2682. }
  2683. }
  2684. return 0;
  2685. }
  2686. static int yt921x_chip_setup(struct yt921x_priv *priv)
  2687. {
  2688. u32 ctrl;
  2689. int res;
  2690. ctrl = YT921X_FUNC_MIB;
  2691. res = yt921x_reg_set_bits(priv, YT921X_FUNC, ctrl);
  2692. if (res)
  2693. return res;
  2694. res = yt921x_chip_setup_dsa(priv);
  2695. if (res)
  2696. return res;
  2697. #if IS_ENABLED(CONFIG_DCB)
  2698. res = yt921x_chip_setup_qos(priv);
  2699. if (res)
  2700. return res;
  2701. #endif
  2702. /* Clear MIB */
  2703. ctrl = YT921X_MIB_CTRL_CLEAN | YT921X_MIB_CTRL_ALL_PORT;
  2704. res = yt921x_reg_write(priv, YT921X_MIB_CTRL, ctrl);
  2705. if (res)
  2706. return res;
  2707. /* Miscellaneous */
  2708. res = yt921x_reg_set_bits(priv, YT921X_SENSOR, YT921X_SENSOR_TEMP);
  2709. if (res)
  2710. return res;
  2711. return 0;
  2712. }
  2713. static int yt921x_dsa_setup(struct dsa_switch *ds)
  2714. {
  2715. struct yt921x_priv *priv = to_yt921x_priv(ds);
  2716. struct device *dev = to_device(priv);
  2717. struct device_node *np = dev->of_node;
  2718. struct device_node *child;
  2719. int res;
  2720. mutex_lock(&priv->reg_lock);
  2721. res = yt921x_chip_reset(priv);
  2722. mutex_unlock(&priv->reg_lock);
  2723. if (res)
  2724. return res;
  2725. /* Register the internal mdio bus. Nodes for internal ports should have
  2726. * proper phy-handle pointing to their PHYs. Not enabling the internal
  2727. * bus is possible, though pretty wired, if internal ports are not used.
  2728. */
  2729. child = of_get_child_by_name(np, "mdio");
  2730. if (child) {
  2731. res = yt921x_mbus_int_init(priv, child);
  2732. of_node_put(child);
  2733. if (res)
  2734. return res;
  2735. }
  2736. /* External mdio bus is optional */
  2737. child = of_get_child_by_name(np, "mdio-external");
  2738. if (child) {
  2739. res = yt921x_mbus_ext_init(priv, child);
  2740. of_node_put(child);
  2741. if (res)
  2742. return res;
  2743. dev_err(dev, "Untested external mdio bus\n");
  2744. return -ENODEV;
  2745. }
  2746. mutex_lock(&priv->reg_lock);
  2747. res = yt921x_chip_setup(priv);
  2748. mutex_unlock(&priv->reg_lock);
  2749. if (res)
  2750. return res;
  2751. return 0;
  2752. }
  2753. static const struct phylink_mac_ops yt921x_phylink_mac_ops = {
  2754. .mac_link_down = yt921x_phylink_mac_link_down,
  2755. .mac_link_up = yt921x_phylink_mac_link_up,
  2756. .mac_config = yt921x_phylink_mac_config,
  2757. };
  2758. static const struct dsa_switch_ops yt921x_dsa_switch_ops = {
  2759. /* mib */
  2760. .get_strings = yt921x_dsa_get_strings,
  2761. .get_ethtool_stats = yt921x_dsa_get_ethtool_stats,
  2762. .get_sset_count = yt921x_dsa_get_sset_count,
  2763. .get_eth_mac_stats = yt921x_dsa_get_eth_mac_stats,
  2764. .get_eth_ctrl_stats = yt921x_dsa_get_eth_ctrl_stats,
  2765. .get_rmon_stats = yt921x_dsa_get_rmon_stats,
  2766. .get_stats64 = yt921x_dsa_get_stats64,
  2767. .get_pause_stats = yt921x_dsa_get_pause_stats,
  2768. /* eee */
  2769. .support_eee = dsa_supports_eee,
  2770. .set_mac_eee = yt921x_dsa_set_mac_eee,
  2771. /* mtu */
  2772. .port_change_mtu = yt921x_dsa_port_change_mtu,
  2773. .port_max_mtu = yt921x_dsa_port_max_mtu,
  2774. /* hsr */
  2775. .port_hsr_leave = dsa_port_simple_hsr_leave,
  2776. .port_hsr_join = dsa_port_simple_hsr_join,
  2777. /* mirror */
  2778. .port_mirror_del = yt921x_dsa_port_mirror_del,
  2779. .port_mirror_add = yt921x_dsa_port_mirror_add,
  2780. /* lag */
  2781. .port_lag_leave = yt921x_dsa_port_lag_leave,
  2782. .port_lag_join = yt921x_dsa_port_lag_join,
  2783. /* fdb */
  2784. .port_fdb_dump = yt921x_dsa_port_fdb_dump,
  2785. .port_fast_age = yt921x_dsa_port_fast_age,
  2786. .set_ageing_time = yt921x_dsa_set_ageing_time,
  2787. .port_fdb_del = yt921x_dsa_port_fdb_del,
  2788. .port_fdb_add = yt921x_dsa_port_fdb_add,
  2789. .port_mdb_del = yt921x_dsa_port_mdb_del,
  2790. .port_mdb_add = yt921x_dsa_port_mdb_add,
  2791. /* vlan */
  2792. .port_vlan_filtering = yt921x_dsa_port_vlan_filtering,
  2793. .port_vlan_del = yt921x_dsa_port_vlan_del,
  2794. .port_vlan_add = yt921x_dsa_port_vlan_add,
  2795. /* bridge */
  2796. .port_pre_bridge_flags = yt921x_dsa_port_pre_bridge_flags,
  2797. .port_bridge_flags = yt921x_dsa_port_bridge_flags,
  2798. .port_bridge_leave = yt921x_dsa_port_bridge_leave,
  2799. .port_bridge_join = yt921x_dsa_port_bridge_join,
  2800. /* mst */
  2801. .port_mst_state_set = yt921x_dsa_port_mst_state_set,
  2802. .vlan_msti_set = yt921x_dsa_vlan_msti_set,
  2803. .port_stp_state_set = yt921x_dsa_port_stp_state_set,
  2804. #if IS_ENABLED(CONFIG_DCB)
  2805. /* dcb */
  2806. .port_get_default_prio = yt921x_dsa_port_get_default_prio,
  2807. .port_set_default_prio = yt921x_dsa_port_set_default_prio,
  2808. .port_get_apptrust = yt921x_dsa_port_get_apptrust,
  2809. .port_set_apptrust = yt921x_dsa_port_set_apptrust,
  2810. #endif
  2811. /* port */
  2812. .get_tag_protocol = yt921x_dsa_get_tag_protocol,
  2813. .phylink_get_caps = yt921x_dsa_phylink_get_caps,
  2814. .port_setup = yt921x_dsa_port_setup,
  2815. #if IS_ENABLED(CONFIG_DCB)
  2816. /* dscp */
  2817. .port_get_dscp_prio = yt921x_dsa_port_get_dscp_prio,
  2818. .port_del_dscp_prio = yt921x_dsa_port_del_dscp_prio,
  2819. .port_add_dscp_prio = yt921x_dsa_port_add_dscp_prio,
  2820. #endif
  2821. /* chip */
  2822. .setup = yt921x_dsa_setup,
  2823. };
  2824. static void yt921x_mdio_shutdown(struct mdio_device *mdiodev)
  2825. {
  2826. struct yt921x_priv *priv = mdiodev_get_drvdata(mdiodev);
  2827. if (!priv)
  2828. return;
  2829. dsa_switch_shutdown(&priv->ds);
  2830. }
  2831. static void yt921x_mdio_remove(struct mdio_device *mdiodev)
  2832. {
  2833. struct yt921x_priv *priv = mdiodev_get_drvdata(mdiodev);
  2834. if (!priv)
  2835. return;
  2836. for (size_t i = ARRAY_SIZE(priv->ports); i-- > 0; ) {
  2837. struct yt921x_port *pp = &priv->ports[i];
  2838. disable_delayed_work_sync(&pp->mib_read);
  2839. }
  2840. dsa_unregister_switch(&priv->ds);
  2841. mutex_destroy(&priv->reg_lock);
  2842. }
  2843. static int yt921x_mdio_probe(struct mdio_device *mdiodev)
  2844. {
  2845. struct device *dev = &mdiodev->dev;
  2846. struct yt921x_reg_mdio *mdio;
  2847. struct yt921x_priv *priv;
  2848. struct dsa_switch *ds;
  2849. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  2850. if (!priv)
  2851. return -ENOMEM;
  2852. mdio = devm_kzalloc(dev, sizeof(*mdio), GFP_KERNEL);
  2853. if (!mdio)
  2854. return -ENOMEM;
  2855. mdio->bus = mdiodev->bus;
  2856. mdio->addr = mdiodev->addr;
  2857. mdio->switchid = 0;
  2858. mutex_init(&priv->reg_lock);
  2859. priv->reg_ops = &yt921x_reg_ops_mdio;
  2860. priv->reg_ctx = mdio;
  2861. for (size_t i = 0; i < ARRAY_SIZE(priv->ports); i++) {
  2862. struct yt921x_port *pp = &priv->ports[i];
  2863. pp->index = i;
  2864. INIT_DELAYED_WORK(&pp->mib_read, yt921x_poll_mib);
  2865. }
  2866. ds = &priv->ds;
  2867. ds->dev = dev;
  2868. ds->assisted_learning_on_cpu_port = true;
  2869. ds->dscp_prio_mapping_is_global = true;
  2870. ds->priv = priv;
  2871. ds->ops = &yt921x_dsa_switch_ops;
  2872. ds->ageing_time_min = 1 * 5000;
  2873. ds->ageing_time_max = U16_MAX * 5000;
  2874. ds->phylink_mac_ops = &yt921x_phylink_mac_ops;
  2875. ds->num_lag_ids = YT921X_LAG_NUM;
  2876. ds->num_ports = YT921X_PORT_NUM;
  2877. mdiodev_set_drvdata(mdiodev, priv);
  2878. return dsa_register_switch(ds);
  2879. }
  2880. static const struct of_device_id yt921x_of_match[] = {
  2881. { .compatible = "motorcomm,yt9215" },
  2882. {}
  2883. };
  2884. MODULE_DEVICE_TABLE(of, yt921x_of_match);
  2885. static struct mdio_driver yt921x_mdio_driver = {
  2886. .probe = yt921x_mdio_probe,
  2887. .remove = yt921x_mdio_remove,
  2888. .shutdown = yt921x_mdio_shutdown,
  2889. .mdiodrv.driver = {
  2890. .name = YT921X_NAME,
  2891. .of_match_table = yt921x_of_match,
  2892. },
  2893. };
  2894. mdio_module_driver(yt921x_mdio_driver);
  2895. MODULE_AUTHOR("David Yang <mmyangfl@gmail.com>");
  2896. MODULE_DESCRIPTION("Driver for Motorcomm YT921x Switch");
  2897. MODULE_LICENSE("GPL");