dsa.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <net/dsa.h>
  3. #include <linux/if_bridge.h>
  4. #include <asm/mach-rtl838x/mach-rtl83xx.h>
  5. #include "rtl83xx.h"
  6. extern struct rtl83xx_soc_info soc_info;
  7. static void rtl83xx_init_stats(struct rtl838x_switch_priv *priv)
  8. {
  9. mutex_lock(&priv->reg_mutex);
  10. /* Enable statistics module: all counters plus debug.
  11. * On RTL839x all counters are enabled by default
  12. */
  13. if (priv->family_id == RTL8380_FAMILY_ID)
  14. sw_w32_mask(0, 3, RTL838X_STAT_CTRL);
  15. /* Reset statistics counters */
  16. sw_w32_mask(0, 1, priv->r->stat_rst);
  17. mutex_unlock(&priv->reg_mutex);
  18. }
  19. static void rtl83xx_enable_phy_polling(struct rtl838x_switch_priv *priv)
  20. {
  21. int i;
  22. u64 v = 0;
  23. msleep(1000);
  24. /* Enable all ports with a PHY, including the SFP-ports */
  25. for (i = 0; i < priv->cpu_port; i++) {
  26. if (priv->ports[i].phy)
  27. v |= BIT_ULL(i);
  28. }
  29. pr_info("%s: %16llx\n", __func__, v);
  30. priv->r->set_port_reg_le(v, priv->r->smi_poll_ctrl);
  31. /* PHY update complete, there is no global PHY polling enable bit on the 9300 */
  32. if (priv->family_id == RTL8390_FAMILY_ID)
  33. sw_w32_mask(0, BIT(7), RTL839X_SMI_GLB_CTRL);
  34. else if(priv->family_id == RTL9300_FAMILY_ID)
  35. sw_w32_mask(0, 0x8000, RTL838X_SMI_GLB_CTRL);
  36. }
  37. const struct rtl83xx_mib_desc rtl83xx_mib[] = {
  38. MIB_DESC(2, 0xf8, "ifInOctets"),
  39. MIB_DESC(2, 0xf0, "ifOutOctets"),
  40. MIB_DESC(1, 0xec, "dot1dTpPortInDiscards"),
  41. MIB_DESC(1, 0xe8, "ifInUcastPkts"),
  42. MIB_DESC(1, 0xe4, "ifInMulticastPkts"),
  43. MIB_DESC(1, 0xe0, "ifInBroadcastPkts"),
  44. MIB_DESC(1, 0xdc, "ifOutUcastPkts"),
  45. MIB_DESC(1, 0xd8, "ifOutMulticastPkts"),
  46. MIB_DESC(1, 0xd4, "ifOutBroadcastPkts"),
  47. MIB_DESC(1, 0xd0, "ifOutDiscards"),
  48. MIB_DESC(1, 0xcc, ".3SingleCollisionFrames"),
  49. MIB_DESC(1, 0xc8, ".3MultipleCollisionFrames"),
  50. MIB_DESC(1, 0xc4, ".3DeferredTransmissions"),
  51. MIB_DESC(1, 0xc0, ".3LateCollisions"),
  52. MIB_DESC(1, 0xbc, ".3ExcessiveCollisions"),
  53. MIB_DESC(1, 0xb8, ".3SymbolErrors"),
  54. MIB_DESC(1, 0xb4, ".3ControlInUnknownOpcodes"),
  55. MIB_DESC(1, 0xb0, ".3InPauseFrames"),
  56. MIB_DESC(1, 0xac, ".3OutPauseFrames"),
  57. MIB_DESC(1, 0xa8, "DropEvents"),
  58. MIB_DESC(1, 0xa4, "tx_BroadcastPkts"),
  59. MIB_DESC(1, 0xa0, "tx_MulticastPkts"),
  60. MIB_DESC(1, 0x9c, "CRCAlignErrors"),
  61. MIB_DESC(1, 0x98, "tx_UndersizePkts"),
  62. MIB_DESC(1, 0x94, "rx_UndersizePkts"),
  63. MIB_DESC(1, 0x90, "rx_UndersizedropPkts"),
  64. MIB_DESC(1, 0x8c, "tx_OversizePkts"),
  65. MIB_DESC(1, 0x88, "rx_OversizePkts"),
  66. MIB_DESC(1, 0x84, "Fragments"),
  67. MIB_DESC(1, 0x80, "Jabbers"),
  68. MIB_DESC(1, 0x7c, "Collisions"),
  69. MIB_DESC(1, 0x78, "tx_Pkts64Octets"),
  70. MIB_DESC(1, 0x74, "rx_Pkts64Octets"),
  71. MIB_DESC(1, 0x70, "tx_Pkts65to127Octets"),
  72. MIB_DESC(1, 0x6c, "rx_Pkts65to127Octets"),
  73. MIB_DESC(1, 0x68, "tx_Pkts128to255Octets"),
  74. MIB_DESC(1, 0x64, "rx_Pkts128to255Octets"),
  75. MIB_DESC(1, 0x60, "tx_Pkts256to511Octets"),
  76. MIB_DESC(1, 0x5c, "rx_Pkts256to511Octets"),
  77. MIB_DESC(1, 0x58, "tx_Pkts512to1023Octets"),
  78. MIB_DESC(1, 0x54, "rx_Pkts512to1023Octets"),
  79. MIB_DESC(1, 0x50, "tx_Pkts1024to1518Octets"),
  80. MIB_DESC(1, 0x4c, "rx_StatsPkts1024to1518Octets"),
  81. MIB_DESC(1, 0x48, "tx_Pkts1519toMaxOctets"),
  82. MIB_DESC(1, 0x44, "rx_Pkts1519toMaxOctets"),
  83. MIB_DESC(1, 0x40, "rxMacDiscards")
  84. };
  85. /* DSA callbacks */
  86. static enum dsa_tag_protocol rtl83xx_get_tag_protocol(struct dsa_switch *ds,
  87. int port,
  88. enum dsa_tag_protocol mprot)
  89. {
  90. /* The switch does not tag the frames, instead internally the header
  91. * structure for each packet is tagged accordingly.
  92. */
  93. return DSA_TAG_PROTO_TRAILER;
  94. }
  95. /*
  96. * Initialize all VLANS
  97. */
  98. static void rtl83xx_vlan_setup(struct rtl838x_switch_priv *priv)
  99. {
  100. struct rtl838x_vlan_info info;
  101. int i;
  102. pr_info("In %s\n", __func__);
  103. priv->r->vlan_profile_setup(0);
  104. priv->r->vlan_profile_setup(1);
  105. pr_info("UNKNOWN_MC_PMASK: %016llx\n", priv->r->read_mcast_pmask(UNKNOWN_MC_PMASK));
  106. priv->r->vlan_profile_dump(0);
  107. info.fid = 0; // Default Forwarding ID / MSTI
  108. info.hash_uc_fid = false; // Do not build the L2 lookup hash with FID, but VID
  109. info.hash_mc_fid = false; // Do the same for Multicast packets
  110. info.profile_id = 0; // Use default Vlan Profile 0
  111. info.tagged_ports = 0; // Initially no port members
  112. if (priv->family_id == RTL9310_FAMILY_ID) {
  113. info.if_id = 0;
  114. info.multicast_grp_mask = 0;
  115. info.l2_tunnel_list_id = -1;
  116. }
  117. // Initialize all vlans 0-4095
  118. for (i = 0; i < MAX_VLANS; i ++)
  119. priv->r->vlan_set_tagged(i, &info);
  120. // reset PVIDs; defaults to 1 on reset
  121. for (i = 0; i <= priv->ds->num_ports; i++) {
  122. priv->r->vlan_port_pvid_set(i, PBVLAN_TYPE_INNER, 0);
  123. priv->r->vlan_port_pvid_set(i, PBVLAN_TYPE_OUTER, 0);
  124. priv->r->vlan_port_pvidmode_set(i, PBVLAN_TYPE_INNER, PBVLAN_MODE_UNTAG_AND_PRITAG);
  125. priv->r->vlan_port_pvidmode_set(i, PBVLAN_TYPE_OUTER, PBVLAN_MODE_UNTAG_AND_PRITAG);
  126. }
  127. // Set forwarding action based on inner VLAN tag
  128. for (i = 0; i < priv->cpu_port; i++)
  129. priv->r->vlan_fwd_on_inner(i, true);
  130. }
  131. static void rtl83xx_setup_bpdu_traps(struct rtl838x_switch_priv *priv)
  132. {
  133. int i;
  134. for (i = 0; i < priv->cpu_port; i++)
  135. priv->r->set_receive_management_action(i, BPDU, COPY2CPU);
  136. }
  137. static int rtl83xx_setup(struct dsa_switch *ds)
  138. {
  139. int i;
  140. struct rtl838x_switch_priv *priv = ds->priv;
  141. u64 port_bitmap = BIT_ULL(priv->cpu_port);
  142. pr_debug("%s called\n", __func__);
  143. /* Disable MAC polling the PHY so that we can start configuration */
  144. priv->r->set_port_reg_le(0ULL, priv->r->smi_poll_ctrl);
  145. for (i = 0; i < ds->num_ports; i++)
  146. priv->ports[i].enable = false;
  147. priv->ports[priv->cpu_port].enable = true;
  148. /* Isolate ports from each other: traffic only CPU <-> port */
  149. /* Setting bit j in register RTL838X_PORT_ISO_CTRL(i) allows
  150. * traffic from source port i to destination port j
  151. */
  152. for (i = 0; i < priv->cpu_port; i++) {
  153. if (priv->ports[i].phy) {
  154. priv->r->set_port_reg_be(BIT_ULL(priv->cpu_port) | BIT_ULL(i),
  155. priv->r->port_iso_ctrl(i));
  156. port_bitmap |= BIT_ULL(i);
  157. }
  158. }
  159. priv->r->set_port_reg_be(port_bitmap, priv->r->port_iso_ctrl(priv->cpu_port));
  160. if (priv->family_id == RTL8380_FAMILY_ID)
  161. rtl838x_print_matrix();
  162. else
  163. rtl839x_print_matrix();
  164. rtl83xx_init_stats(priv);
  165. rtl83xx_vlan_setup(priv);
  166. rtl83xx_setup_bpdu_traps(priv);
  167. ds->configure_vlan_while_not_filtering = true;
  168. priv->r->l2_learning_setup();
  169. /* Enable MAC Polling PHY again */
  170. rtl83xx_enable_phy_polling(priv);
  171. pr_debug("Please wait until PHY is settled\n");
  172. msleep(1000);
  173. priv->r->pie_init(priv);
  174. return 0;
  175. }
  176. static int rtl93xx_setup(struct dsa_switch *ds)
  177. {
  178. int i;
  179. struct rtl838x_switch_priv *priv = ds->priv;
  180. u32 port_bitmap = BIT(priv->cpu_port);
  181. pr_info("%s called\n", __func__);
  182. /* Disable MAC polling the PHY so that we can start configuration */
  183. if (priv->family_id == RTL9300_FAMILY_ID)
  184. sw_w32(0, RTL930X_SMI_POLL_CTRL);
  185. if (priv->family_id == RTL9310_FAMILY_ID) {
  186. sw_w32(0, RTL931X_SMI_PORT_POLLING_CTRL);
  187. sw_w32(0, RTL931X_SMI_PORT_POLLING_CTRL + 4);
  188. }
  189. // Disable all ports except CPU port
  190. for (i = 0; i < ds->num_ports; i++)
  191. priv->ports[i].enable = false;
  192. priv->ports[priv->cpu_port].enable = true;
  193. for (i = 0; i < priv->cpu_port; i++) {
  194. if (priv->ports[i].phy) {
  195. priv->r->traffic_set(i, BIT_ULL(priv->cpu_port) | BIT_ULL(i));
  196. port_bitmap |= BIT_ULL(i);
  197. }
  198. }
  199. priv->r->traffic_set(priv->cpu_port, port_bitmap);
  200. rtl930x_print_matrix();
  201. // TODO: Initialize statistics
  202. rtl83xx_vlan_setup(priv);
  203. ds->configure_vlan_while_not_filtering = true;
  204. priv->r->l2_learning_setup();
  205. rtl83xx_enable_phy_polling(priv);
  206. priv->r->pie_init(priv);
  207. return 0;
  208. }
  209. static int rtl93xx_get_sds(struct phy_device *phydev)
  210. {
  211. struct device *dev = &phydev->mdio.dev;
  212. struct device_node *dn;
  213. u32 sds_num;
  214. if (!dev)
  215. return -1;
  216. if (dev->of_node) {
  217. dn = dev->of_node;
  218. if (of_property_read_u32(dn, "sds", &sds_num))
  219. sds_num = -1;
  220. } else {
  221. dev_err(dev, "No DT node.\n");
  222. return -1;
  223. }
  224. return sds_num;
  225. }
  226. static void rtl83xx_phylink_validate(struct dsa_switch *ds, int port,
  227. unsigned long *supported,
  228. struct phylink_link_state *state)
  229. {
  230. struct rtl838x_switch_priv *priv = ds->priv;
  231. __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
  232. pr_debug("In %s port %d, state is %d", __func__, port, state->interface);
  233. if (!phy_interface_mode_is_rgmii(state->interface) &&
  234. state->interface != PHY_INTERFACE_MODE_NA &&
  235. state->interface != PHY_INTERFACE_MODE_1000BASEX &&
  236. state->interface != PHY_INTERFACE_MODE_MII &&
  237. state->interface != PHY_INTERFACE_MODE_REVMII &&
  238. state->interface != PHY_INTERFACE_MODE_GMII &&
  239. state->interface != PHY_INTERFACE_MODE_QSGMII &&
  240. state->interface != PHY_INTERFACE_MODE_INTERNAL &&
  241. state->interface != PHY_INTERFACE_MODE_SGMII) {
  242. bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
  243. dev_err(ds->dev,
  244. "Unsupported interface: %d for port %d\n",
  245. state->interface, port);
  246. return;
  247. }
  248. /* Allow all the expected bits */
  249. phylink_set(mask, Autoneg);
  250. phylink_set_port_modes(mask);
  251. phylink_set(mask, Pause);
  252. phylink_set(mask, Asym_Pause);
  253. /* With the exclusion of MII and Reverse MII, we support Gigabit,
  254. * including Half duplex
  255. */
  256. if (state->interface != PHY_INTERFACE_MODE_MII &&
  257. state->interface != PHY_INTERFACE_MODE_REVMII) {
  258. phylink_set(mask, 1000baseT_Full);
  259. phylink_set(mask, 1000baseT_Half);
  260. }
  261. /* On both the 8380 and 8382, ports 24-27 are SFP ports */
  262. if (port >= 24 && port <= 27 && priv->family_id == RTL8380_FAMILY_ID)
  263. phylink_set(mask, 1000baseX_Full);
  264. /* On the RTL839x family of SoCs, ports 48 to 51 are SFP ports */
  265. if (port >= 48 && port <= 51 && priv->family_id == RTL8390_FAMILY_ID)
  266. phylink_set(mask, 1000baseX_Full);
  267. phylink_set(mask, 10baseT_Half);
  268. phylink_set(mask, 10baseT_Full);
  269. phylink_set(mask, 100baseT_Half);
  270. phylink_set(mask, 100baseT_Full);
  271. bitmap_and(supported, supported, mask,
  272. __ETHTOOL_LINK_MODE_MASK_NBITS);
  273. bitmap_and(state->advertising, state->advertising, mask,
  274. __ETHTOOL_LINK_MODE_MASK_NBITS);
  275. }
  276. static void rtl93xx_phylink_validate(struct dsa_switch *ds, int port,
  277. unsigned long *supported,
  278. struct phylink_link_state *state)
  279. {
  280. struct rtl838x_switch_priv *priv = ds->priv;
  281. __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
  282. pr_debug("In %s port %d, state is %d (%s)", __func__, port, state->interface,
  283. phy_modes(state->interface));
  284. if (!phy_interface_mode_is_rgmii(state->interface) &&
  285. state->interface != PHY_INTERFACE_MODE_NA &&
  286. state->interface != PHY_INTERFACE_MODE_1000BASEX &&
  287. state->interface != PHY_INTERFACE_MODE_MII &&
  288. state->interface != PHY_INTERFACE_MODE_REVMII &&
  289. state->interface != PHY_INTERFACE_MODE_GMII &&
  290. state->interface != PHY_INTERFACE_MODE_QSGMII &&
  291. state->interface != PHY_INTERFACE_MODE_XGMII &&
  292. state->interface != PHY_INTERFACE_MODE_HSGMII &&
  293. state->interface != PHY_INTERFACE_MODE_10GBASER &&
  294. state->interface != PHY_INTERFACE_MODE_10GKR &&
  295. state->interface != PHY_INTERFACE_MODE_USXGMII &&
  296. state->interface != PHY_INTERFACE_MODE_INTERNAL &&
  297. state->interface != PHY_INTERFACE_MODE_SGMII) {
  298. bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
  299. dev_err(ds->dev,
  300. "Unsupported interface: %d for port %d\n",
  301. state->interface, port);
  302. return;
  303. }
  304. /* Allow all the expected bits */
  305. phylink_set(mask, Autoneg);
  306. phylink_set_port_modes(mask);
  307. phylink_set(mask, Pause);
  308. phylink_set(mask, Asym_Pause);
  309. /* With the exclusion of MII and Reverse MII, we support Gigabit,
  310. * including Half duplex
  311. */
  312. if (state->interface != PHY_INTERFACE_MODE_MII &&
  313. state->interface != PHY_INTERFACE_MODE_REVMII) {
  314. phylink_set(mask, 1000baseT_Full);
  315. phylink_set(mask, 1000baseT_Half);
  316. }
  317. // Internal phys of the RTL93xx family provide 10G
  318. if (priv->ports[port].phy_is_integrated
  319. && state->interface == PHY_INTERFACE_MODE_1000BASEX) {
  320. phylink_set(mask, 1000baseX_Full);
  321. } else if (priv->ports[port].phy_is_integrated) {
  322. phylink_set(mask, 1000baseX_Full);
  323. phylink_set(mask, 10000baseKR_Full);
  324. phylink_set(mask, 10000baseSR_Full);
  325. phylink_set(mask, 10000baseCR_Full);
  326. }
  327. if (state->interface == PHY_INTERFACE_MODE_INTERNAL) {
  328. phylink_set(mask, 1000baseX_Full);
  329. phylink_set(mask, 1000baseT_Full);
  330. phylink_set(mask, 10000baseKR_Full);
  331. phylink_set(mask, 10000baseT_Full);
  332. phylink_set(mask, 10000baseSR_Full);
  333. phylink_set(mask, 10000baseCR_Full);
  334. }
  335. if (state->interface == PHY_INTERFACE_MODE_USXGMII)
  336. phylink_set(mask, 10000baseT_Full);
  337. phylink_set(mask, 10baseT_Half);
  338. phylink_set(mask, 10baseT_Full);
  339. phylink_set(mask, 100baseT_Half);
  340. phylink_set(mask, 100baseT_Full);
  341. bitmap_and(supported, supported, mask,
  342. __ETHTOOL_LINK_MODE_MASK_NBITS);
  343. bitmap_and(state->advertising, state->advertising, mask,
  344. __ETHTOOL_LINK_MODE_MASK_NBITS);
  345. pr_debug("%s leaving supported: %*pb", __func__, __ETHTOOL_LINK_MODE_MASK_NBITS, supported);
  346. }
  347. static int rtl83xx_phylink_mac_link_state(struct dsa_switch *ds, int port,
  348. struct phylink_link_state *state)
  349. {
  350. struct rtl838x_switch_priv *priv = ds->priv;
  351. u64 speed;
  352. u64 link;
  353. if (port < 0 || port > priv->cpu_port)
  354. return -EINVAL;
  355. state->link = 0;
  356. link = priv->r->get_port_reg_le(priv->r->mac_link_sts);
  357. if (link & BIT_ULL(port))
  358. state->link = 1;
  359. pr_debug("%s: link state port %d: %llx\n", __func__, port, link & BIT_ULL(port));
  360. state->duplex = 0;
  361. if (priv->r->get_port_reg_le(priv->r->mac_link_dup_sts) & BIT_ULL(port))
  362. state->duplex = 1;
  363. speed = priv->r->get_port_reg_le(priv->r->mac_link_spd_sts(port));
  364. speed >>= (port % 16) << 1;
  365. switch (speed & 0x3) {
  366. case 0:
  367. state->speed = SPEED_10;
  368. break;
  369. case 1:
  370. state->speed = SPEED_100;
  371. break;
  372. case 2:
  373. state->speed = SPEED_1000;
  374. break;
  375. case 3:
  376. if (priv->family_id == RTL9300_FAMILY_ID
  377. && (port == 24 || port == 26)) /* Internal serdes */
  378. state->speed = SPEED_2500;
  379. else
  380. state->speed = SPEED_100; /* Is in fact 500Mbit */
  381. }
  382. state->pause &= (MLO_PAUSE_RX | MLO_PAUSE_TX);
  383. if (priv->r->get_port_reg_le(priv->r->mac_rx_pause_sts) & BIT_ULL(port))
  384. state->pause |= MLO_PAUSE_RX;
  385. if (priv->r->get_port_reg_le(priv->r->mac_tx_pause_sts) & BIT_ULL(port))
  386. state->pause |= MLO_PAUSE_TX;
  387. return 1;
  388. }
  389. static int rtl93xx_phylink_mac_link_state(struct dsa_switch *ds, int port,
  390. struct phylink_link_state *state)
  391. {
  392. struct rtl838x_switch_priv *priv = ds->priv;
  393. u64 speed;
  394. u64 link;
  395. if (port < 0 || port > priv->cpu_port)
  396. return -EINVAL;
  397. /*
  398. * On the RTL9300 for at least the RTL8226B PHY, the MAC-side link
  399. * state needs to be read twice in order to read a correct result.
  400. * This would not be necessary for ports connected e.g. to RTL8218D
  401. * PHYs.
  402. */
  403. state->link = 0;
  404. link = priv->r->get_port_reg_le(priv->r->mac_link_sts);
  405. link = priv->r->get_port_reg_le(priv->r->mac_link_sts);
  406. if (link & BIT_ULL(port))
  407. state->link = 1;
  408. pr_debug("%s: link state port %d: %llx, media %08x\n", __func__, port,
  409. link & BIT_ULL(port), sw_r32(RTL930X_MAC_LINK_MEDIA_STS));
  410. state->duplex = 0;
  411. if (priv->r->get_port_reg_le(priv->r->mac_link_dup_sts) & BIT_ULL(port))
  412. state->duplex = 1;
  413. speed = priv->r->get_port_reg_le(priv->r->mac_link_spd_sts(port));
  414. speed >>= (port % 8) << 2;
  415. switch (speed & 0xf) {
  416. case 0:
  417. state->speed = SPEED_10;
  418. break;
  419. case 1:
  420. state->speed = SPEED_100;
  421. break;
  422. case 2:
  423. case 7:
  424. state->speed = SPEED_1000;
  425. break;
  426. case 4:
  427. state->speed = SPEED_10000;
  428. break;
  429. case 5:
  430. case 8:
  431. state->speed = SPEED_2500;
  432. break;
  433. case 6:
  434. state->speed = SPEED_5000;
  435. break;
  436. default:
  437. pr_err("%s: unknown speed: %d\n", __func__, (u32)speed & 0xf);
  438. }
  439. if (priv->family_id == RTL9310_FAMILY_ID
  440. && (port >= 52 || port <= 55)) { /* Internal serdes */
  441. state->speed = SPEED_10000;
  442. state->link = 1;
  443. state->duplex = 1;
  444. }
  445. pr_debug("%s: speed is: %d %d\n", __func__, (u32)speed & 0xf, state->speed);
  446. state->pause &= (MLO_PAUSE_RX | MLO_PAUSE_TX);
  447. if (priv->r->get_port_reg_le(priv->r->mac_rx_pause_sts) & BIT_ULL(port))
  448. state->pause |= MLO_PAUSE_RX;
  449. if (priv->r->get_port_reg_le(priv->r->mac_tx_pause_sts) & BIT_ULL(port))
  450. state->pause |= MLO_PAUSE_TX;
  451. return 1;
  452. }
  453. static void rtl83xx_config_interface(int port, phy_interface_t interface)
  454. {
  455. u32 old, int_shift, sds_shift;
  456. switch (port) {
  457. case 24:
  458. int_shift = 0;
  459. sds_shift = 5;
  460. break;
  461. case 26:
  462. int_shift = 3;
  463. sds_shift = 0;
  464. break;
  465. default:
  466. return;
  467. }
  468. old = sw_r32(RTL838X_SDS_MODE_SEL);
  469. switch (interface) {
  470. case PHY_INTERFACE_MODE_1000BASEX:
  471. if ((old >> sds_shift & 0x1f) == 4)
  472. return;
  473. sw_w32_mask(0x7 << int_shift, 1 << int_shift, RTL838X_INT_MODE_CTRL);
  474. sw_w32_mask(0x1f << sds_shift, 4 << sds_shift, RTL838X_SDS_MODE_SEL);
  475. break;
  476. case PHY_INTERFACE_MODE_SGMII:
  477. if ((old >> sds_shift & 0x1f) == 2)
  478. return;
  479. sw_w32_mask(0x7 << int_shift, 2 << int_shift, RTL838X_INT_MODE_CTRL);
  480. sw_w32_mask(0x1f << sds_shift, 2 << sds_shift, RTL838X_SDS_MODE_SEL);
  481. break;
  482. default:
  483. return;
  484. }
  485. pr_debug("configured port %d for interface %s\n", port, phy_modes(interface));
  486. }
  487. static void rtl83xx_phylink_mac_config(struct dsa_switch *ds, int port,
  488. unsigned int mode,
  489. const struct phylink_link_state *state)
  490. {
  491. struct rtl838x_switch_priv *priv = ds->priv;
  492. u32 reg;
  493. int speed_bit = priv->family_id == RTL8380_FAMILY_ID ? 4 : 3;
  494. pr_debug("%s port %d, mode %x\n", __func__, port, mode);
  495. if (port == priv->cpu_port) {
  496. /* Set Speed, duplex, flow control
  497. * FORCE_EN | LINK_EN | NWAY_EN | DUP_SEL
  498. * | SPD_SEL = 0b10 | FORCE_FC_EN | PHY_MASTER_SLV_MANUAL_EN
  499. * | MEDIA_SEL
  500. */
  501. if (priv->family_id == RTL8380_FAMILY_ID) {
  502. sw_w32(0x6192F, priv->r->mac_force_mode_ctrl(priv->cpu_port));
  503. /* allow CRC errors on CPU-port */
  504. sw_w32_mask(0, 0x8, RTL838X_MAC_PORT_CTRL(priv->cpu_port));
  505. } else {
  506. sw_w32_mask(0, 3, priv->r->mac_force_mode_ctrl(priv->cpu_port));
  507. }
  508. return;
  509. }
  510. reg = sw_r32(priv->r->mac_force_mode_ctrl(port));
  511. /* Auto-Negotiation does not work for MAC in RTL8390 */
  512. if (priv->family_id == RTL8380_FAMILY_ID) {
  513. if (mode == MLO_AN_PHY || phylink_autoneg_inband(mode)) {
  514. pr_debug("PHY autonegotiates\n");
  515. reg |= RTL838X_NWAY_EN;
  516. sw_w32(reg, priv->r->mac_force_mode_ctrl(port));
  517. rtl83xx_config_interface(port, state->interface);
  518. return;
  519. }
  520. }
  521. if (mode != MLO_AN_FIXED)
  522. pr_debug("Fixed state.\n");
  523. /* Clear id_mode_dis bit, and the existing port mode, let
  524. * RGMII_MODE_EN bet set by mac_link_{up,down} */
  525. if (priv->family_id == RTL8380_FAMILY_ID) {
  526. reg &= ~(RTL838X_RX_PAUSE_EN | RTL838X_TX_PAUSE_EN);
  527. if (state->pause & MLO_PAUSE_TXRX_MASK) {
  528. if (state->pause & MLO_PAUSE_TX)
  529. reg |= RTL838X_TX_PAUSE_EN;
  530. reg |= RTL838X_RX_PAUSE_EN;
  531. }
  532. } else if (priv->family_id == RTL8390_FAMILY_ID) {
  533. reg &= ~(RTL839X_RX_PAUSE_EN | RTL839X_TX_PAUSE_EN);
  534. if (state->pause & MLO_PAUSE_TXRX_MASK) {
  535. if (state->pause & MLO_PAUSE_TX)
  536. reg |= RTL839X_TX_PAUSE_EN;
  537. reg |= RTL839X_RX_PAUSE_EN;
  538. }
  539. }
  540. reg &= ~(3 << speed_bit);
  541. switch (state->speed) {
  542. case SPEED_1000:
  543. reg |= 2 << speed_bit;
  544. break;
  545. case SPEED_100:
  546. reg |= 1 << speed_bit;
  547. break;
  548. default:
  549. break; // Ignore, including 10MBit which has a speed value of 0
  550. }
  551. if (priv->family_id == RTL8380_FAMILY_ID) {
  552. reg &= ~(RTL838X_DUPLEX_MODE | RTL838X_FORCE_LINK_EN);
  553. if (state->link)
  554. reg |= RTL838X_FORCE_LINK_EN;
  555. if (state->duplex == RTL838X_DUPLEX_MODE)
  556. reg |= RTL838X_DUPLEX_MODE;
  557. } else if (priv->family_id == RTL8390_FAMILY_ID) {
  558. reg &= ~(RTL839X_DUPLEX_MODE | RTL839X_FORCE_LINK_EN);
  559. if (state->link)
  560. reg |= RTL839X_FORCE_LINK_EN;
  561. if (state->duplex == RTL839X_DUPLEX_MODE)
  562. reg |= RTL839X_DUPLEX_MODE;
  563. }
  564. // LAG members must use DUPLEX and we need to enable the link
  565. if (priv->lagmembers & BIT_ULL(port)) {
  566. switch(priv->family_id) {
  567. case RTL8380_FAMILY_ID:
  568. reg |= (RTL838X_DUPLEX_MODE | RTL838X_FORCE_LINK_EN);
  569. break;
  570. case RTL8390_FAMILY_ID:
  571. reg |= (RTL839X_DUPLEX_MODE | RTL839X_FORCE_LINK_EN);
  572. break;
  573. }
  574. }
  575. // Disable AN
  576. if (priv->family_id == RTL8380_FAMILY_ID)
  577. reg &= ~RTL838X_NWAY_EN;
  578. sw_w32(reg, priv->r->mac_force_mode_ctrl(port));
  579. }
  580. static void rtl931x_phylink_mac_config(struct dsa_switch *ds, int port,
  581. unsigned int mode,
  582. const struct phylink_link_state *state)
  583. {
  584. struct rtl838x_switch_priv *priv = ds->priv;
  585. int sds_num;
  586. u32 reg, band;
  587. sds_num = priv->ports[port].sds_num;
  588. pr_info("%s: speed %d sds_num %d\n", __func__, state->speed, sds_num);
  589. switch (state->interface) {
  590. case PHY_INTERFACE_MODE_HSGMII:
  591. pr_info("%s setting mode PHY_INTERFACE_MODE_HSGMII\n", __func__);
  592. band = rtl931x_sds_cmu_band_get(sds_num, PHY_INTERFACE_MODE_HSGMII);
  593. rtl931x_sds_init(sds_num, PHY_INTERFACE_MODE_HSGMII);
  594. band = rtl931x_sds_cmu_band_set(sds_num, true, 62, PHY_INTERFACE_MODE_HSGMII);
  595. break;
  596. case PHY_INTERFACE_MODE_1000BASEX:
  597. band = rtl931x_sds_cmu_band_get(sds_num, PHY_INTERFACE_MODE_1000BASEX);
  598. rtl931x_sds_init(sds_num, PHY_INTERFACE_MODE_1000BASEX);
  599. break;
  600. case PHY_INTERFACE_MODE_XGMII:
  601. band = rtl931x_sds_cmu_band_get(sds_num, PHY_INTERFACE_MODE_XGMII);
  602. rtl931x_sds_init(sds_num, PHY_INTERFACE_MODE_XGMII);
  603. break;
  604. case PHY_INTERFACE_MODE_10GBASER:
  605. case PHY_INTERFACE_MODE_10GKR:
  606. band = rtl931x_sds_cmu_band_get(sds_num, PHY_INTERFACE_MODE_10GBASER);
  607. rtl931x_sds_init(sds_num, PHY_INTERFACE_MODE_10GBASER);
  608. break;
  609. case PHY_INTERFACE_MODE_USXGMII:
  610. // Translates to MII_USXGMII_10GSXGMII
  611. band = rtl931x_sds_cmu_band_get(sds_num, PHY_INTERFACE_MODE_USXGMII);
  612. rtl931x_sds_init(sds_num, PHY_INTERFACE_MODE_USXGMII);
  613. break;
  614. case PHY_INTERFACE_MODE_SGMII:
  615. pr_info("%s setting mode PHY_INTERFACE_MODE_SGMII\n", __func__);
  616. band = rtl931x_sds_cmu_band_get(sds_num, PHY_INTERFACE_MODE_SGMII);
  617. rtl931x_sds_init(sds_num, PHY_INTERFACE_MODE_SGMII);
  618. band = rtl931x_sds_cmu_band_set(sds_num, true, 62, PHY_INTERFACE_MODE_SGMII);
  619. break;
  620. case PHY_INTERFACE_MODE_QSGMII:
  621. band = rtl931x_sds_cmu_band_get(sds_num, PHY_INTERFACE_MODE_QSGMII);
  622. rtl931x_sds_init(sds_num, PHY_INTERFACE_MODE_QSGMII);
  623. break;
  624. default:
  625. pr_err("%s: unknown serdes mode: %s\n",
  626. __func__, phy_modes(state->interface));
  627. return;
  628. }
  629. reg = sw_r32(priv->r->mac_force_mode_ctrl(port));
  630. pr_info("%s reading FORCE_MODE_CTRL: %08x\n", __func__, reg);
  631. reg &= ~(RTL931X_DUPLEX_MODE | RTL931X_FORCE_EN | RTL931X_FORCE_LINK_EN);
  632. reg &= ~(0xf << 12);
  633. reg |= 0x2 << 12; // Set SMI speed to 0x2
  634. reg |= RTL931X_TX_PAUSE_EN | RTL931X_RX_PAUSE_EN;
  635. if (priv->lagmembers & BIT_ULL(port))
  636. reg |= RTL931X_DUPLEX_MODE;
  637. if (state->duplex == DUPLEX_FULL)
  638. reg |= RTL931X_DUPLEX_MODE;
  639. sw_w32(reg, priv->r->mac_force_mode_ctrl(port));
  640. }
  641. static void rtl93xx_phylink_mac_config(struct dsa_switch *ds, int port,
  642. unsigned int mode,
  643. const struct phylink_link_state *state)
  644. {
  645. struct rtl838x_switch_priv *priv = ds->priv;
  646. int sds_num, sds_mode;
  647. u32 reg;
  648. pr_info("%s port %d, mode %x, phy-mode: %s, speed %d, link %d\n", __func__,
  649. port, mode, phy_modes(state->interface), state->speed, state->link);
  650. // Nothing to be done for the CPU-port
  651. if (port == priv->cpu_port)
  652. return;
  653. if (priv->family_id == RTL9310_FAMILY_ID)
  654. return rtl931x_phylink_mac_config(ds, port, mode, state);
  655. sds_num = priv->ports[port].sds_num;
  656. pr_info("%s SDS is %d\n", __func__, sds_num);
  657. if (sds_num >= 0) {
  658. switch (state->interface) {
  659. case PHY_INTERFACE_MODE_HSGMII:
  660. sds_mode = 0x12;
  661. break;
  662. case PHY_INTERFACE_MODE_1000BASEX:
  663. sds_mode = 0x04;
  664. break;
  665. case PHY_INTERFACE_MODE_XGMII:
  666. sds_mode = 0x10;
  667. break;
  668. case PHY_INTERFACE_MODE_10GBASER:
  669. case PHY_INTERFACE_MODE_10GKR:
  670. sds_mode = 0x1b; // 10G 1000X Auto
  671. break;
  672. case PHY_INTERFACE_MODE_USXGMII:
  673. sds_mode = 0x0d;
  674. break;
  675. default:
  676. pr_err("%s: unknown serdes mode: %s\n",
  677. __func__, phy_modes(state->interface));
  678. return;
  679. }
  680. rtl9300_sds_rst(sds_num, sds_mode);
  681. }
  682. reg = sw_r32(priv->r->mac_force_mode_ctrl(port));
  683. reg &= ~(0xf << 3);
  684. switch (state->speed) {
  685. case SPEED_10000:
  686. reg |= 4 << 3;
  687. break;
  688. case SPEED_5000:
  689. reg |= 6 << 3;
  690. break;
  691. case SPEED_2500:
  692. reg |= 5 << 3;
  693. break;
  694. case SPEED_1000:
  695. reg |= 2 << 3;
  696. break;
  697. default:
  698. reg |= 2 << 3;
  699. break;
  700. }
  701. if (state->link)
  702. reg |= RTL930X_FORCE_LINK_EN;
  703. if (priv->lagmembers & BIT_ULL(port))
  704. reg |= RTL930X_DUPLEX_MODE | RTL930X_FORCE_LINK_EN;
  705. if (state->duplex == DUPLEX_FULL)
  706. reg |= RTL930X_DUPLEX_MODE;
  707. if (priv->ports[port].phy_is_integrated)
  708. reg &= ~RTL930X_FORCE_EN; // Clear MAC_FORCE_EN to allow SDS-MAC link
  709. else
  710. reg |= RTL930X_FORCE_EN;
  711. sw_w32(reg, priv->r->mac_force_mode_ctrl(port));
  712. }
  713. static void rtl83xx_phylink_mac_link_down(struct dsa_switch *ds, int port,
  714. unsigned int mode,
  715. phy_interface_t interface)
  716. {
  717. struct rtl838x_switch_priv *priv = ds->priv;
  718. u32 v;
  719. /* Stop TX/RX to port */
  720. sw_w32_mask(0x3, 0, priv->r->mac_port_ctrl(port));
  721. // No longer force link
  722. if (priv->family_id == RTL9300_FAMILY_ID)
  723. v = RTL930X_FORCE_EN | RTL930X_FORCE_LINK_EN;
  724. else if (priv->family_id == RTL9310_FAMILY_ID)
  725. v = RTL931X_FORCE_EN | RTL931X_FORCE_LINK_EN;
  726. sw_w32_mask(v, 0, priv->r->mac_port_ctrl(port));
  727. }
  728. static void rtl93xx_phylink_mac_link_down(struct dsa_switch *ds, int port,
  729. unsigned int mode,
  730. phy_interface_t interface)
  731. {
  732. struct rtl838x_switch_priv *priv = ds->priv;
  733. /* Stop TX/RX to port */
  734. sw_w32_mask(0x3, 0, priv->r->mac_port_ctrl(port));
  735. // No longer force link
  736. sw_w32_mask(3, 0, priv->r->mac_force_mode_ctrl(port));
  737. }
  738. static void rtl83xx_phylink_mac_link_up(struct dsa_switch *ds, int port,
  739. unsigned int mode,
  740. phy_interface_t interface,
  741. struct phy_device *phydev,
  742. int speed, int duplex,
  743. bool tx_pause, bool rx_pause)
  744. {
  745. struct rtl838x_switch_priv *priv = ds->priv;
  746. /* Restart TX/RX to port */
  747. sw_w32_mask(0, 0x3, priv->r->mac_port_ctrl(port));
  748. // TODO: Set speed/duplex/pauses
  749. }
  750. static void rtl93xx_phylink_mac_link_up(struct dsa_switch *ds, int port,
  751. unsigned int mode,
  752. phy_interface_t interface,
  753. struct phy_device *phydev,
  754. int speed, int duplex,
  755. bool tx_pause, bool rx_pause)
  756. {
  757. struct rtl838x_switch_priv *priv = ds->priv;
  758. /* Restart TX/RX to port */
  759. sw_w32_mask(0, 0x3, priv->r->mac_port_ctrl(port));
  760. // TODO: Set speed/duplex/pauses
  761. }
  762. static void rtl83xx_get_strings(struct dsa_switch *ds,
  763. int port, u32 stringset, u8 *data)
  764. {
  765. int i;
  766. if (stringset != ETH_SS_STATS)
  767. return;
  768. for (i = 0; i < ARRAY_SIZE(rtl83xx_mib); i++)
  769. strncpy(data + i * ETH_GSTRING_LEN, rtl83xx_mib[i].name,
  770. ETH_GSTRING_LEN);
  771. }
  772. static void rtl83xx_get_ethtool_stats(struct dsa_switch *ds, int port,
  773. uint64_t *data)
  774. {
  775. struct rtl838x_switch_priv *priv = ds->priv;
  776. const struct rtl83xx_mib_desc *mib;
  777. int i;
  778. u64 h;
  779. for (i = 0; i < ARRAY_SIZE(rtl83xx_mib); i++) {
  780. mib = &rtl83xx_mib[i];
  781. data[i] = sw_r32(priv->r->stat_port_std_mib + (port << 8) + 252 - mib->offset);
  782. if (mib->size == 2) {
  783. h = sw_r32(priv->r->stat_port_std_mib + (port << 8) + 248 - mib->offset);
  784. data[i] |= h << 32;
  785. }
  786. }
  787. }
  788. static int rtl83xx_get_sset_count(struct dsa_switch *ds, int port, int sset)
  789. {
  790. if (sset != ETH_SS_STATS)
  791. return 0;
  792. return ARRAY_SIZE(rtl83xx_mib);
  793. }
  794. static int rtl83xx_mc_group_alloc(struct rtl838x_switch_priv *priv, int port)
  795. {
  796. int mc_group = find_first_zero_bit(priv->mc_group_bm, MAX_MC_GROUPS - 1);
  797. u64 portmask;
  798. if (mc_group >= MAX_MC_GROUPS - 1)
  799. return -1;
  800. if (priv->is_lagmember[port]) {
  801. pr_info("%s: %d is lag slave. ignore\n", __func__, port);
  802. return 0;
  803. }
  804. set_bit(mc_group, priv->mc_group_bm);
  805. mc_group++; // We cannot use group 0, as this is used for lookup miss flooding
  806. portmask = BIT_ULL(port) | BIT_ULL(priv->cpu_port);
  807. priv->r->write_mcast_pmask(mc_group, portmask);
  808. return mc_group;
  809. }
  810. static u64 rtl83xx_mc_group_add_port(struct rtl838x_switch_priv *priv, int mc_group, int port)
  811. {
  812. u64 portmask = priv->r->read_mcast_pmask(mc_group);
  813. pr_debug("%s: %d\n", __func__, port);
  814. if (priv->is_lagmember[port]) {
  815. pr_info("%s: %d is lag slave. ignore\n", __func__, port);
  816. return portmask;
  817. }
  818. portmask |= BIT_ULL(port);
  819. priv->r->write_mcast_pmask(mc_group, portmask);
  820. return portmask;
  821. }
  822. static u64 rtl83xx_mc_group_del_port(struct rtl838x_switch_priv *priv, int mc_group, int port)
  823. {
  824. u64 portmask = priv->r->read_mcast_pmask(mc_group);
  825. pr_debug("%s: %d\n", __func__, port);
  826. if (priv->is_lagmember[port]) {
  827. pr_info("%s: %d is lag slave. ignore\n", __func__, port);
  828. return portmask;
  829. }
  830. priv->r->write_mcast_pmask(mc_group, portmask);
  831. if (portmask == BIT_ULL(priv->cpu_port)) {
  832. portmask &= ~BIT_ULL(priv->cpu_port);
  833. priv->r->write_mcast_pmask(mc_group, portmask);
  834. clear_bit(mc_group, priv->mc_group_bm);
  835. }
  836. return portmask;
  837. }
  838. static void store_mcgroups(struct rtl838x_switch_priv *priv, int port)
  839. {
  840. int mc_group;
  841. for (mc_group = 0; mc_group < MAX_MC_GROUPS; mc_group++) {
  842. u64 portmask = priv->r->read_mcast_pmask(mc_group);
  843. if (portmask & BIT_ULL(port)) {
  844. priv->mc_group_saves[mc_group] = port;
  845. rtl83xx_mc_group_del_port(priv, mc_group, port);
  846. }
  847. }
  848. }
  849. static void load_mcgroups(struct rtl838x_switch_priv *priv, int port)
  850. {
  851. int mc_group;
  852. for (mc_group = 0; mc_group < MAX_MC_GROUPS; mc_group++) {
  853. if (priv->mc_group_saves[mc_group] == port) {
  854. rtl83xx_mc_group_add_port(priv, mc_group, port);
  855. priv->mc_group_saves[mc_group] = -1;
  856. }
  857. }
  858. }
  859. static int rtl83xx_port_enable(struct dsa_switch *ds, int port,
  860. struct phy_device *phydev)
  861. {
  862. struct rtl838x_switch_priv *priv = ds->priv;
  863. u64 v;
  864. pr_debug("%s: %x %d", __func__, (u32) priv, port);
  865. priv->ports[port].enable = true;
  866. /* enable inner tagging on egress, do not keep any tags */
  867. if (priv->family_id == RTL9310_FAMILY_ID)
  868. sw_w32(BIT(4), priv->r->vlan_port_tag_sts_ctrl + (port << 2));
  869. else
  870. sw_w32(1, priv->r->vlan_port_tag_sts_ctrl + (port << 2));
  871. if (dsa_is_cpu_port(ds, port))
  872. return 0;
  873. /* add port to switch mask of CPU_PORT */
  874. priv->r->traffic_enable(priv->cpu_port, port);
  875. load_mcgroups(priv, port);
  876. if (priv->is_lagmember[port]) {
  877. pr_debug("%s: %d is lag slave. ignore\n", __func__, port);
  878. return 0;
  879. }
  880. /* add all other ports in the same bridge to switch mask of port */
  881. v = priv->r->traffic_get(port);
  882. v |= priv->ports[port].pm;
  883. priv->r->traffic_set(port, v);
  884. // TODO: Figure out if this is necessary
  885. if (priv->family_id == RTL9300_FAMILY_ID) {
  886. sw_w32_mask(0, BIT(port), RTL930X_L2_PORT_SABLK_CTRL);
  887. sw_w32_mask(0, BIT(port), RTL930X_L2_PORT_DABLK_CTRL);
  888. }
  889. priv->ports[port].sds_num = rtl93xx_get_sds(phydev);
  890. return 0;
  891. }
  892. static void rtl83xx_port_disable(struct dsa_switch *ds, int port)
  893. {
  894. struct rtl838x_switch_priv *priv = ds->priv;
  895. u64 v;
  896. pr_debug("%s %x: %d", __func__, (u32)priv, port);
  897. /* you can only disable user ports */
  898. if (!dsa_is_user_port(ds, port))
  899. return;
  900. // BUG: This does not work on RTL931X
  901. /* remove port from switch mask of CPU_PORT */
  902. priv->r->traffic_disable(priv->cpu_port, port);
  903. store_mcgroups(priv, port);
  904. /* remove all other ports in the same bridge from switch mask of port */
  905. v = priv->r->traffic_get(port);
  906. v &= ~priv->ports[port].pm;
  907. priv->r->traffic_set(port, v);
  908. priv->ports[port].enable = false;
  909. }
  910. static int rtl83xx_set_mac_eee(struct dsa_switch *ds, int port,
  911. struct ethtool_eee *e)
  912. {
  913. struct rtl838x_switch_priv *priv = ds->priv;
  914. if (e->eee_enabled && !priv->eee_enabled) {
  915. pr_info("Globally enabling EEE\n");
  916. priv->r->init_eee(priv, true);
  917. }
  918. priv->r->port_eee_set(priv, port, e->eee_enabled);
  919. if (e->eee_enabled)
  920. pr_info("Enabled EEE for port %d\n", port);
  921. else
  922. pr_info("Disabled EEE for port %d\n", port);
  923. return 0;
  924. }
  925. static int rtl83xx_get_mac_eee(struct dsa_switch *ds, int port,
  926. struct ethtool_eee *e)
  927. {
  928. struct rtl838x_switch_priv *priv = ds->priv;
  929. e->supported = SUPPORTED_100baseT_Full | SUPPORTED_1000baseT_Full;
  930. priv->r->eee_port_ability(priv, e, port);
  931. e->eee_enabled = priv->ports[port].eee_enabled;
  932. e->eee_active = !!(e->advertised & e->lp_advertised);
  933. return 0;
  934. }
  935. static int rtl93xx_get_mac_eee(struct dsa_switch *ds, int port,
  936. struct ethtool_eee *e)
  937. {
  938. struct rtl838x_switch_priv *priv = ds->priv;
  939. e->supported = SUPPORTED_100baseT_Full | SUPPORTED_1000baseT_Full
  940. | SUPPORTED_2500baseX_Full;
  941. priv->r->eee_port_ability(priv, e, port);
  942. e->eee_enabled = priv->ports[port].eee_enabled;
  943. e->eee_active = !!(e->advertised & e->lp_advertised);
  944. return 0;
  945. }
  946. static int rtl83xx_set_ageing_time(struct dsa_switch *ds, unsigned int msec)
  947. {
  948. struct rtl838x_switch_priv *priv = ds->priv;
  949. priv->r->set_ageing_time(msec);
  950. return 0;
  951. }
  952. static int rtl83xx_port_bridge_join(struct dsa_switch *ds, int port,
  953. struct net_device *bridge)
  954. {
  955. struct rtl838x_switch_priv *priv = ds->priv;
  956. u64 port_bitmap = BIT_ULL(priv->cpu_port), v;
  957. int i;
  958. pr_debug("%s %x: %d %llx", __func__, (u32)priv, port, port_bitmap);
  959. if (priv->is_lagmember[port]) {
  960. pr_debug("%s: %d is lag slave. ignore\n", __func__, port);
  961. return 0;
  962. }
  963. mutex_lock(&priv->reg_mutex);
  964. for (i = 0; i < ds->num_ports; i++) {
  965. /* Add this port to the port matrix of the other ports in the
  966. * same bridge. If the port is disabled, port matrix is kept
  967. * and not being setup until the port becomes enabled.
  968. */
  969. if (dsa_is_user_port(ds, i) && !priv->is_lagmember[i] && i != port) {
  970. if (dsa_to_port(ds, i)->bridge_dev != bridge)
  971. continue;
  972. if (priv->ports[i].enable)
  973. priv->r->traffic_enable(i, port);
  974. priv->ports[i].pm |= BIT_ULL(port);
  975. port_bitmap |= BIT_ULL(i);
  976. }
  977. }
  978. load_mcgroups(priv, port);
  979. /* Add all other ports to this port matrix. */
  980. if (priv->ports[port].enable) {
  981. priv->r->traffic_enable(priv->cpu_port, port);
  982. v = priv->r->traffic_get(port);
  983. v |= port_bitmap;
  984. priv->r->traffic_set(port, v);
  985. }
  986. priv->ports[port].pm |= port_bitmap;
  987. mutex_unlock(&priv->reg_mutex);
  988. return 0;
  989. }
  990. static void rtl83xx_port_bridge_leave(struct dsa_switch *ds, int port,
  991. struct net_device *bridge)
  992. {
  993. struct rtl838x_switch_priv *priv = ds->priv;
  994. u64 port_bitmap = BIT_ULL(priv->cpu_port), v;
  995. int i;
  996. pr_debug("%s %x: %d", __func__, (u32)priv, port);
  997. mutex_lock(&priv->reg_mutex);
  998. for (i = 0; i < ds->num_ports; i++) {
  999. /* Remove this port from the port matrix of the other ports
  1000. * in the same bridge. If the port is disabled, port matrix
  1001. * is kept and not being setup until the port becomes enabled.
  1002. * And the other port's port matrix cannot be broken when the
  1003. * other port is still a VLAN-aware port.
  1004. */
  1005. if (dsa_is_user_port(ds, i) && i != port) {
  1006. if (dsa_to_port(ds, i)->bridge_dev != bridge)
  1007. continue;
  1008. if (priv->ports[i].enable)
  1009. priv->r->traffic_disable(i, port);
  1010. priv->ports[i].pm |= BIT_ULL(port);
  1011. port_bitmap &= ~BIT_ULL(i);
  1012. }
  1013. }
  1014. store_mcgroups(priv, port);
  1015. /* Add all other ports to this port matrix. */
  1016. if (priv->ports[port].enable) {
  1017. v = priv->r->traffic_get(port);
  1018. v |= port_bitmap;
  1019. priv->r->traffic_set(port, v);
  1020. }
  1021. priv->ports[port].pm &= ~port_bitmap;
  1022. mutex_unlock(&priv->reg_mutex);
  1023. }
  1024. void rtl83xx_port_stp_state_set(struct dsa_switch *ds, int port, u8 state)
  1025. {
  1026. u32 msti = 0;
  1027. u32 port_state[4];
  1028. int index, bit;
  1029. int pos = port;
  1030. struct rtl838x_switch_priv *priv = ds->priv;
  1031. int n = priv->port_width << 1;
  1032. /* Ports above or equal CPU port can never be configured */
  1033. if (port >= priv->cpu_port)
  1034. return;
  1035. mutex_lock(&priv->reg_mutex);
  1036. /* For the RTL839x and following, the bits are left-aligned, 838x and 930x
  1037. * have 64 bit fields, 839x and 931x have 128 bit fields
  1038. */
  1039. if (priv->family_id == RTL8390_FAMILY_ID)
  1040. pos += 12;
  1041. if (priv->family_id == RTL9300_FAMILY_ID)
  1042. pos += 3;
  1043. if (priv->family_id == RTL9310_FAMILY_ID)
  1044. pos += 8;
  1045. index = n - (pos >> 4) - 1;
  1046. bit = (pos << 1) % 32;
  1047. priv->r->stp_get(priv, msti, port_state);
  1048. pr_debug("Current state, port %d: %d\n", port, (port_state[index] >> bit) & 3);
  1049. port_state[index] &= ~(3 << bit);
  1050. switch (state) {
  1051. case BR_STATE_DISABLED: /* 0 */
  1052. port_state[index] |= (0 << bit);
  1053. break;
  1054. case BR_STATE_BLOCKING: /* 4 */
  1055. case BR_STATE_LISTENING: /* 1 */
  1056. port_state[index] |= (1 << bit);
  1057. break;
  1058. case BR_STATE_LEARNING: /* 2 */
  1059. port_state[index] |= (2 << bit);
  1060. break;
  1061. case BR_STATE_FORWARDING: /* 3*/
  1062. port_state[index] |= (3 << bit);
  1063. default:
  1064. break;
  1065. }
  1066. priv->r->stp_set(priv, msti, port_state);
  1067. mutex_unlock(&priv->reg_mutex);
  1068. }
  1069. void rtl83xx_fast_age(struct dsa_switch *ds, int port)
  1070. {
  1071. struct rtl838x_switch_priv *priv = ds->priv;
  1072. int s = priv->family_id == RTL8390_FAMILY_ID ? 2 : 0;
  1073. pr_debug("FAST AGE port %d\n", port);
  1074. mutex_lock(&priv->reg_mutex);
  1075. /* RTL838X_L2_TBL_FLUSH_CTRL register bits, 839x has 1 bit larger
  1076. * port fields:
  1077. * 0-4: Replacing port
  1078. * 5-9: Flushed/replaced port
  1079. * 10-21: FVID
  1080. * 22: Entry types: 1: dynamic, 0: also static
  1081. * 23: Match flush port
  1082. * 24: Match FVID
  1083. * 25: Flush (0) or replace (1) L2 entries
  1084. * 26: Status of action (1: Start, 0: Done)
  1085. */
  1086. sw_w32(1 << (26 + s) | 1 << (23 + s) | port << (5 + (s / 2)), priv->r->l2_tbl_flush_ctrl);
  1087. do { } while (sw_r32(priv->r->l2_tbl_flush_ctrl) & BIT(26 + s));
  1088. mutex_unlock(&priv->reg_mutex);
  1089. }
  1090. void rtl931x_fast_age(struct dsa_switch *ds, int port)
  1091. {
  1092. struct rtl838x_switch_priv *priv = ds->priv;
  1093. pr_info("%s port %d\n", __func__, port);
  1094. mutex_lock(&priv->reg_mutex);
  1095. sw_w32(port << 11, RTL931X_L2_TBL_FLUSH_CTRL + 4);
  1096. sw_w32(BIT(24) | BIT(28), RTL931X_L2_TBL_FLUSH_CTRL);
  1097. do { } while (sw_r32(RTL931X_L2_TBL_FLUSH_CTRL) & BIT (28));
  1098. mutex_unlock(&priv->reg_mutex);
  1099. }
  1100. void rtl930x_fast_age(struct dsa_switch *ds, int port)
  1101. {
  1102. struct rtl838x_switch_priv *priv = ds->priv;
  1103. if (priv->family_id == RTL9310_FAMILY_ID)
  1104. return rtl931x_fast_age(ds, port);
  1105. pr_debug("FAST AGE port %d\n", port);
  1106. mutex_lock(&priv->reg_mutex);
  1107. sw_w32(port << 11, RTL930X_L2_TBL_FLUSH_CTRL + 4);
  1108. sw_w32(BIT(26) | BIT(30), RTL930X_L2_TBL_FLUSH_CTRL);
  1109. do { } while (sw_r32(priv->r->l2_tbl_flush_ctrl) & BIT(30));
  1110. mutex_unlock(&priv->reg_mutex);
  1111. }
  1112. static int rtl83xx_vlan_filtering(struct dsa_switch *ds, int port,
  1113. bool vlan_filtering,
  1114. struct switchdev_trans *trans)
  1115. {
  1116. struct rtl838x_switch_priv *priv = ds->priv;
  1117. pr_debug("%s: port %d\n", __func__, port);
  1118. mutex_lock(&priv->reg_mutex);
  1119. if (vlan_filtering) {
  1120. /* Enable ingress and egress filtering
  1121. * The VLAN_PORT_IGR_FILTER register uses 2 bits for each port to define
  1122. * the filter action:
  1123. * 0: Always Forward
  1124. * 1: Drop packet
  1125. * 2: Trap packet to CPU port
  1126. * The Egress filter used 1 bit per state (0: DISABLED, 1: ENABLED)
  1127. */
  1128. if (port != priv->cpu_port)
  1129. priv->r->set_vlan_igr_filter(port, IGR_DROP);
  1130. priv->r->set_vlan_egr_filter(port, EGR_ENABLE);
  1131. } else {
  1132. /* Disable ingress and egress filtering */
  1133. if (port != priv->cpu_port)
  1134. priv->r->set_vlan_igr_filter(port, IGR_FORWARD);
  1135. priv->r->set_vlan_egr_filter(port, EGR_DISABLE);
  1136. }
  1137. /* Do we need to do something to the CPU-Port, too? */
  1138. mutex_unlock(&priv->reg_mutex);
  1139. return 0;
  1140. }
  1141. static int rtl83xx_vlan_prepare(struct dsa_switch *ds, int port,
  1142. const struct switchdev_obj_port_vlan *vlan)
  1143. {
  1144. struct rtl838x_vlan_info info;
  1145. struct rtl838x_switch_priv *priv = ds->priv;
  1146. priv->r->vlan_tables_read(0, &info);
  1147. pr_debug("VLAN 0: Tagged ports %llx, untag %llx, profile %d, MC# %d, UC# %d, FID %x\n",
  1148. info.tagged_ports, info.untagged_ports, info.profile_id,
  1149. info.hash_mc_fid, info.hash_uc_fid, info.fid);
  1150. priv->r->vlan_tables_read(1, &info);
  1151. pr_debug("VLAN 1: Tagged ports %llx, untag %llx, profile %d, MC# %d, UC# %d, FID %x\n",
  1152. info.tagged_ports, info.untagged_ports, info.profile_id,
  1153. info.hash_mc_fid, info.hash_uc_fid, info.fid);
  1154. priv->r->vlan_set_untagged(1, info.untagged_ports);
  1155. pr_debug("SET: Untagged ports, VLAN %d: %llx\n", 1, info.untagged_ports);
  1156. priv->r->vlan_set_tagged(1, &info);
  1157. pr_debug("SET: Tagged ports, VLAN %d: %llx\n", 1, info.tagged_ports);
  1158. mutex_unlock(&priv->reg_mutex);
  1159. return 0;
  1160. }
  1161. static void rtl83xx_vlan_add(struct dsa_switch *ds, int port,
  1162. const struct switchdev_obj_port_vlan *vlan)
  1163. {
  1164. struct rtl838x_vlan_info info;
  1165. struct rtl838x_switch_priv *priv = ds->priv;
  1166. int v;
  1167. pr_debug("%s port %d, vid_end %d, vid_end %d, flags %x\n", __func__,
  1168. port, vlan->vid_begin, vlan->vid_end, vlan->flags);
  1169. if (vlan->vid_begin > 4095 || vlan->vid_end > 4095) {
  1170. dev_err(priv->dev, "VLAN out of range: %d - %d",
  1171. vlan->vid_begin, vlan->vid_end);
  1172. return;
  1173. }
  1174. mutex_lock(&priv->reg_mutex);
  1175. if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
  1176. for (v = vlan->vid_begin; v <= vlan->vid_end; v++) {
  1177. if (!v)
  1178. continue;
  1179. /* Set both inner and outer PVID of the port */
  1180. priv->r->vlan_port_pvid_set(port, PBVLAN_TYPE_INNER, v);
  1181. priv->r->vlan_port_pvid_set(port, PBVLAN_TYPE_OUTER, v);
  1182. priv->r->vlan_port_pvidmode_set(port, PBVLAN_TYPE_INNER,
  1183. PBVLAN_MODE_UNTAG_AND_PRITAG);
  1184. priv->r->vlan_port_pvidmode_set(port, PBVLAN_TYPE_OUTER,
  1185. PBVLAN_MODE_UNTAG_AND_PRITAG);
  1186. priv->ports[port].pvid = vlan->vid_end;
  1187. }
  1188. }
  1189. for (v = vlan->vid_begin; v <= vlan->vid_end; v++) {
  1190. /* Get port memberships of this vlan */
  1191. priv->r->vlan_tables_read(v, &info);
  1192. /* new VLAN? */
  1193. if (!info.tagged_ports) {
  1194. info.fid = 0;
  1195. info.hash_mc_fid = false;
  1196. info.hash_uc_fid = false;
  1197. info.profile_id = 0;
  1198. }
  1199. /* sanitize untagged_ports - must be a subset */
  1200. if (info.untagged_ports & ~info.tagged_ports)
  1201. info.untagged_ports = 0;
  1202. info.tagged_ports |= BIT_ULL(port);
  1203. if (vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED)
  1204. info.untagged_ports |= BIT_ULL(port);
  1205. priv->r->vlan_set_untagged(v, info.untagged_ports);
  1206. pr_debug("Untagged ports, VLAN %d: %llx\n", v, info.untagged_ports);
  1207. priv->r->vlan_set_tagged(v, &info);
  1208. pr_debug("Tagged ports, VLAN %d: %llx\n", v, info.tagged_ports);
  1209. }
  1210. mutex_unlock(&priv->reg_mutex);
  1211. }
  1212. static int rtl83xx_vlan_del(struct dsa_switch *ds, int port,
  1213. const struct switchdev_obj_port_vlan *vlan)
  1214. {
  1215. struct rtl838x_vlan_info info;
  1216. struct rtl838x_switch_priv *priv = ds->priv;
  1217. int v;
  1218. u16 pvid;
  1219. pr_debug("%s: port %d, vid_end %d, vid_end %d, flags %x\n", __func__,
  1220. port, vlan->vid_begin, vlan->vid_end, vlan->flags);
  1221. if (vlan->vid_begin > 4095 || vlan->vid_end > 4095) {
  1222. dev_err(priv->dev, "VLAN out of range: %d - %d",
  1223. vlan->vid_begin, vlan->vid_end);
  1224. return -ENOTSUPP;
  1225. }
  1226. mutex_lock(&priv->reg_mutex);
  1227. pvid = priv->ports[port].pvid;
  1228. for (v = vlan->vid_begin; v <= vlan->vid_end; v++) {
  1229. /* Reset to default if removing the current PVID */
  1230. if (v == pvid) {
  1231. priv->r->vlan_port_pvid_set(port, PBVLAN_TYPE_INNER, 0);
  1232. priv->r->vlan_port_pvid_set(port, PBVLAN_TYPE_OUTER, 0);
  1233. priv->r->vlan_port_pvidmode_set(port, PBVLAN_TYPE_INNER,
  1234. PBVLAN_MODE_UNTAG_AND_PRITAG);
  1235. priv->r->vlan_port_pvidmode_set(port, PBVLAN_TYPE_OUTER,
  1236. PBVLAN_MODE_UNTAG_AND_PRITAG);
  1237. }
  1238. /* Get port memberships of this vlan */
  1239. priv->r->vlan_tables_read(v, &info);
  1240. /* remove port from both tables */
  1241. info.untagged_ports &= (~BIT_ULL(port));
  1242. info.tagged_ports &= (~BIT_ULL(port));
  1243. priv->r->vlan_set_untagged(v, info.untagged_ports);
  1244. pr_debug("Untagged ports, VLAN %d: %llx\n", v, info.untagged_ports);
  1245. priv->r->vlan_set_tagged(v, &info);
  1246. pr_debug("Tagged ports, VLAN %d: %llx\n", v, info.tagged_ports);
  1247. }
  1248. mutex_unlock(&priv->reg_mutex);
  1249. return 0;
  1250. }
  1251. static void dump_l2_entry(struct rtl838x_l2_entry *e)
  1252. {
  1253. pr_info("MAC: %02x:%02x:%02x:%02x:%02x:%02x vid: %d, rvid: %d, port: %d, valid: %d\n",
  1254. e->mac[0], e->mac[1], e->mac[2], e->mac[3], e->mac[4], e->mac[5],
  1255. e->vid, e->rvid, e->port, e->valid);
  1256. if (e->type != L2_MULTICAST) {
  1257. pr_info("Type: %d, is_static: %d, is_ip_mc: %d, is_ipv6_mc: %d, block_da: %d\n",
  1258. e->type, e->is_static, e->is_ip_mc, e->is_ipv6_mc, e->block_da);
  1259. pr_info(" block_sa: %d, susp: %d, nh: %d, age: %d, is_trunk: %d, trunk: %d\n",
  1260. e->block_sa, e->suspended, e->next_hop, e->age, e->is_trunk, e->trunk);
  1261. }
  1262. if (e->type == L2_MULTICAST)
  1263. pr_info(" L2_MULTICAST mc_portmask_index: %d\n", e->mc_portmask_index);
  1264. if (e->is_ip_mc || e->is_ipv6_mc)
  1265. pr_info(" mc_portmask_index: %d, mc_gip: %d, mc_sip: %d\n",
  1266. e->mc_portmask_index, e->mc_gip, e->mc_sip);
  1267. pr_info(" stack_dev: %d\n", e->stack_dev);
  1268. if (e->next_hop)
  1269. pr_info(" nh_route_id: %d\n", e->nh_route_id);
  1270. }
  1271. static void rtl83xx_setup_l2_uc_entry(struct rtl838x_l2_entry *e, int port, int vid, u64 mac)
  1272. {
  1273. e->is_ip_mc = e->is_ipv6_mc = false;
  1274. e->valid = true;
  1275. e->age = 3;
  1276. e->port = port,
  1277. e->vid = vid;
  1278. u64_to_ether_addr(mac, e->mac);
  1279. }
  1280. static void rtl83xx_setup_l2_mc_entry(struct rtl838x_switch_priv *priv,
  1281. struct rtl838x_l2_entry *e, int vid, u64 mac, int mc_group)
  1282. {
  1283. e->is_ip_mc = e->is_ipv6_mc = false;
  1284. e->valid = true;
  1285. e->mc_portmask_index = mc_group;
  1286. e->type = L2_MULTICAST;
  1287. e->rvid = e->vid = vid;
  1288. pr_debug("%s: vid: %d, rvid: %d\n", __func__, e->vid, e->rvid);
  1289. u64_to_ether_addr(mac, e->mac);
  1290. }
  1291. /*
  1292. * Uses the seed to identify a hash bucket in the L2 using the derived hash key and then loops
  1293. * over the entries in the bucket until either a matching entry is found or an empty slot
  1294. * Returns the filled in rtl838x_l2_entry and the index in the bucket when an entry was found
  1295. * when an empty slot was found and must exist is false, the index of the slot is returned
  1296. * when no slots are available returns -1
  1297. */
  1298. static int rtl83xx_find_l2_hash_entry(struct rtl838x_switch_priv *priv, u64 seed,
  1299. bool must_exist, struct rtl838x_l2_entry *e)
  1300. {
  1301. int i, idx = -1;
  1302. u32 key = priv->r->l2_hash_key(priv, seed);
  1303. u64 entry;
  1304. pr_debug("%s: using key %x, for seed %016llx\n", __func__, key, seed);
  1305. // Loop over all entries in the hash-bucket and over the second block on 93xx SoCs
  1306. for (i = 0; i < priv->l2_bucket_size; i++) {
  1307. entry = priv->r->read_l2_entry_using_hash(key, i, e);
  1308. pr_debug("valid %d, mac %016llx\n", e->valid, ether_addr_to_u64(&e->mac[0]));
  1309. if (must_exist && !e->valid)
  1310. continue;
  1311. if (!e->valid || ((entry & 0x0fffffffffffffffULL) == seed)) {
  1312. idx = i > 3 ? ((key >> 14) & 0xffff) | i >> 1 : ((key << 2) | i) & 0xffff;
  1313. break;
  1314. }
  1315. }
  1316. return idx;
  1317. }
  1318. /*
  1319. * Uses the seed to identify an entry in the CAM by looping over all its entries
  1320. * Returns the filled in rtl838x_l2_entry and the index in the CAM when an entry was found
  1321. * when an empty slot was found the index of the slot is returned
  1322. * when no slots are available returns -1
  1323. */
  1324. static int rtl83xx_find_l2_cam_entry(struct rtl838x_switch_priv *priv, u64 seed,
  1325. bool must_exist, struct rtl838x_l2_entry *e)
  1326. {
  1327. int i, idx = -1;
  1328. u64 entry;
  1329. for (i = 0; i < 64; i++) {
  1330. entry = priv->r->read_cam(i, e);
  1331. if (!must_exist && !e->valid) {
  1332. if (idx < 0) /* First empty entry? */
  1333. idx = i;
  1334. break;
  1335. } else if ((entry & 0x0fffffffffffffffULL) == seed) {
  1336. pr_debug("Found entry in CAM\n");
  1337. idx = i;
  1338. break;
  1339. }
  1340. }
  1341. return idx;
  1342. }
  1343. static int rtl83xx_port_fdb_add(struct dsa_switch *ds, int port,
  1344. const unsigned char *addr, u16 vid)
  1345. {
  1346. struct rtl838x_switch_priv *priv = ds->priv;
  1347. u64 mac = ether_addr_to_u64(addr);
  1348. struct rtl838x_l2_entry e;
  1349. int err = 0, idx;
  1350. u64 seed = priv->r->l2_hash_seed(mac, vid);
  1351. if (priv->is_lagmember[port]) {
  1352. pr_debug("%s: %d is lag slave. ignore\n", __func__, port);
  1353. return 0;
  1354. }
  1355. mutex_lock(&priv->reg_mutex);
  1356. idx = rtl83xx_find_l2_hash_entry(priv, seed, false, &e);
  1357. // Found an existing or empty entry
  1358. if (idx >= 0) {
  1359. rtl83xx_setup_l2_uc_entry(&e, port, vid, mac);
  1360. priv->r->write_l2_entry_using_hash(idx >> 2, idx & 0x3, &e);
  1361. goto out;
  1362. }
  1363. // Hash buckets full, try CAM
  1364. rtl83xx_find_l2_cam_entry(priv, seed, false, &e);
  1365. if (idx >= 0) {
  1366. rtl83xx_setup_l2_uc_entry(&e, port, vid, mac);
  1367. priv->r->write_cam(idx, &e);
  1368. goto out;
  1369. }
  1370. err = -ENOTSUPP;
  1371. out:
  1372. mutex_unlock(&priv->reg_mutex);
  1373. return err;
  1374. }
  1375. static int rtl83xx_port_fdb_del(struct dsa_switch *ds, int port,
  1376. const unsigned char *addr, u16 vid)
  1377. {
  1378. struct rtl838x_switch_priv *priv = ds->priv;
  1379. u64 mac = ether_addr_to_u64(addr);
  1380. struct rtl838x_l2_entry e;
  1381. int err = 0, idx;
  1382. u64 seed = priv->r->l2_hash_seed(mac, vid);
  1383. pr_info("In %s, mac %llx, vid: %d\n", __func__, mac, vid);
  1384. mutex_lock(&priv->reg_mutex);
  1385. idx = rtl83xx_find_l2_hash_entry(priv, seed, true, &e);
  1386. pr_info("Found entry index %d, key %d and bucket %d\n", idx, idx >> 2, idx & 3);
  1387. if (idx >= 0) {
  1388. e.valid = false;
  1389. dump_l2_entry(&e);
  1390. priv->r->write_l2_entry_using_hash(idx >> 2, idx & 0x3, &e);
  1391. goto out;
  1392. }
  1393. /* Check CAM for spillover from hash buckets */
  1394. rtl83xx_find_l2_cam_entry(priv, seed, true, &e);
  1395. if (idx >= 0) {
  1396. e.valid = false;
  1397. priv->r->write_cam(idx, &e);
  1398. goto out;
  1399. }
  1400. err = -ENOENT;
  1401. out:
  1402. mutex_unlock(&priv->reg_mutex);
  1403. return err;
  1404. }
  1405. static int rtl83xx_port_fdb_dump(struct dsa_switch *ds, int port,
  1406. dsa_fdb_dump_cb_t *cb, void *data)
  1407. {
  1408. struct rtl838x_l2_entry e;
  1409. struct rtl838x_switch_priv *priv = ds->priv;
  1410. int i;
  1411. u32 fid, pkey;
  1412. u64 mac;
  1413. mutex_lock(&priv->reg_mutex);
  1414. for (i = 0; i < priv->fib_entries; i++) {
  1415. priv->r->read_l2_entry_using_hash(i >> 2, i & 0x3, &e);
  1416. if (!e.valid)
  1417. continue;
  1418. if (e.port == port || e.port == RTL930X_PORT_IGNORE) {
  1419. u64 seed;
  1420. u32 key;
  1421. fid = ((i >> 2) & 0x3ff) | (e.rvid & ~0x3ff);
  1422. mac = ether_addr_to_u64(&e.mac[0]);
  1423. pkey = priv->r->l2_hash_key(priv, priv->r->l2_hash_seed(mac, fid));
  1424. fid = (pkey & 0x3ff) | (fid & ~0x3ff);
  1425. pr_info("-> index %d, key %x, bucket %d, dmac %016llx, fid: %x rvid: %x\n",
  1426. i, i >> 2, i & 0x3, mac, fid, e.rvid);
  1427. dump_l2_entry(&e);
  1428. seed = priv->r->l2_hash_seed(mac, e.rvid);
  1429. key = priv->r->l2_hash_key(priv, seed);
  1430. pr_info("seed: %016llx, key based on rvid: %08x\n", seed, key);
  1431. cb(e.mac, e.vid, e.is_static, data);
  1432. }
  1433. if (e.type == L2_MULTICAST) {
  1434. u64 portmask = priv->r->read_mcast_pmask(e.mc_portmask_index);
  1435. if (portmask & BIT_ULL(port)) {
  1436. dump_l2_entry(&e);
  1437. pr_info(" PM: %016llx\n", portmask);
  1438. }
  1439. }
  1440. }
  1441. for (i = 0; i < 64; i++) {
  1442. priv->r->read_cam(i, &e);
  1443. if (!e.valid)
  1444. continue;
  1445. if (e.port == port)
  1446. cb(e.mac, e.vid, e.is_static, data);
  1447. }
  1448. mutex_unlock(&priv->reg_mutex);
  1449. return 0;
  1450. }
  1451. static int rtl83xx_port_mdb_prepare(struct dsa_switch *ds, int port,
  1452. const struct switchdev_obj_port_mdb *mdb)
  1453. {
  1454. struct rtl838x_switch_priv *priv = ds->priv;
  1455. if (priv->id >= 0x9300)
  1456. return -EOPNOTSUPP;
  1457. return 0;
  1458. }
  1459. static void rtl83xx_port_mdb_add(struct dsa_switch *ds, int port,
  1460. const struct switchdev_obj_port_mdb *mdb)
  1461. {
  1462. struct rtl838x_switch_priv *priv = ds->priv;
  1463. u64 mac = ether_addr_to_u64(mdb->addr);
  1464. struct rtl838x_l2_entry e;
  1465. int err = 0, idx;
  1466. int vid = mdb->vid;
  1467. u64 seed = priv->r->l2_hash_seed(mac, vid);
  1468. int mc_group;
  1469. pr_debug("In %s port %d, mac %llx, vid: %d\n", __func__, port, mac, vid);
  1470. if (priv->is_lagmember[port]) {
  1471. pr_debug("%s: %d is lag slave. ignore\n", __func__, port);
  1472. return;
  1473. }
  1474. mutex_lock(&priv->reg_mutex);
  1475. idx = rtl83xx_find_l2_hash_entry(priv, seed, false, &e);
  1476. // Found an existing or empty entry
  1477. if (idx >= 0) {
  1478. if (e.valid) {
  1479. pr_debug("Found an existing entry %016llx, mc_group %d\n",
  1480. ether_addr_to_u64(e.mac), e.mc_portmask_index);
  1481. rtl83xx_mc_group_add_port(priv, e.mc_portmask_index, port);
  1482. } else {
  1483. pr_debug("New entry for seed %016llx\n", seed);
  1484. mc_group = rtl83xx_mc_group_alloc(priv, port);
  1485. if (mc_group < 0) {
  1486. err = -ENOTSUPP;
  1487. goto out;
  1488. }
  1489. rtl83xx_setup_l2_mc_entry(priv, &e, vid, mac, mc_group);
  1490. priv->r->write_l2_entry_using_hash(idx >> 2, idx & 0x3, &e);
  1491. }
  1492. goto out;
  1493. }
  1494. // Hash buckets full, try CAM
  1495. rtl83xx_find_l2_cam_entry(priv, seed, false, &e);
  1496. if (idx >= 0) {
  1497. if (e.valid) {
  1498. pr_debug("Found existing CAM entry %016llx, mc_group %d\n",
  1499. ether_addr_to_u64(e.mac), e.mc_portmask_index);
  1500. rtl83xx_mc_group_add_port(priv, e.mc_portmask_index, port);
  1501. } else {
  1502. pr_debug("New entry\n");
  1503. mc_group = rtl83xx_mc_group_alloc(priv, port);
  1504. if (mc_group < 0) {
  1505. err = -ENOTSUPP;
  1506. goto out;
  1507. }
  1508. rtl83xx_setup_l2_mc_entry(priv, &e, vid, mac, mc_group);
  1509. priv->r->write_cam(idx, &e);
  1510. }
  1511. goto out;
  1512. }
  1513. err = -ENOTSUPP;
  1514. out:
  1515. mutex_unlock(&priv->reg_mutex);
  1516. if (err)
  1517. dev_err(ds->dev, "failed to add MDB entry\n");
  1518. }
  1519. int rtl83xx_port_mdb_del(struct dsa_switch *ds, int port,
  1520. const struct switchdev_obj_port_mdb *mdb)
  1521. {
  1522. struct rtl838x_switch_priv *priv = ds->priv;
  1523. u64 mac = ether_addr_to_u64(mdb->addr);
  1524. struct rtl838x_l2_entry e;
  1525. int err = 0, idx;
  1526. int vid = mdb->vid;
  1527. u64 seed = priv->r->l2_hash_seed(mac, vid);
  1528. u64 portmask;
  1529. pr_debug("In %s, port %d, mac %llx, vid: %d\n", __func__, port, mac, vid);
  1530. if (priv->is_lagmember[port]) {
  1531. pr_info("%s: %d is lag slave. ignore\n", __func__, port);
  1532. return 0;
  1533. }
  1534. mutex_lock(&priv->reg_mutex);
  1535. idx = rtl83xx_find_l2_hash_entry(priv, seed, true, &e);
  1536. pr_debug("Found entry index %d, key %d and bucket %d\n", idx, idx >> 2, idx & 3);
  1537. if (idx >= 0) {
  1538. portmask = rtl83xx_mc_group_del_port(priv, e.mc_portmask_index, port);
  1539. if (!portmask) {
  1540. e.valid = false;
  1541. // dump_l2_entry(&e);
  1542. priv->r->write_l2_entry_using_hash(idx >> 2, idx & 0x3, &e);
  1543. }
  1544. goto out;
  1545. }
  1546. /* Check CAM for spillover from hash buckets */
  1547. rtl83xx_find_l2_cam_entry(priv, seed, true, &e);
  1548. if (idx >= 0) {
  1549. portmask = rtl83xx_mc_group_del_port(priv, e.mc_portmask_index, port);
  1550. if (!portmask) {
  1551. e.valid = false;
  1552. // dump_l2_entry(&e);
  1553. priv->r->write_cam(idx, &e);
  1554. }
  1555. goto out;
  1556. }
  1557. // TODO: Re-enable with a newer kernel: err = -ENOENT;
  1558. out:
  1559. mutex_unlock(&priv->reg_mutex);
  1560. return err;
  1561. }
  1562. static int rtl83xx_port_mirror_add(struct dsa_switch *ds, int port,
  1563. struct dsa_mall_mirror_tc_entry *mirror,
  1564. bool ingress)
  1565. {
  1566. /* We support 4 mirror groups, one destination port per group */
  1567. int group;
  1568. struct rtl838x_switch_priv *priv = ds->priv;
  1569. int ctrl_reg, dpm_reg, spm_reg;
  1570. pr_debug("In %s\n", __func__);
  1571. for (group = 0; group < 4; group++) {
  1572. if (priv->mirror_group_ports[group] == mirror->to_local_port)
  1573. break;
  1574. }
  1575. if (group >= 4) {
  1576. for (group = 0; group < 4; group++) {
  1577. if (priv->mirror_group_ports[group] < 0)
  1578. break;
  1579. }
  1580. }
  1581. if (group >= 4)
  1582. return -ENOSPC;
  1583. ctrl_reg = priv->r->mir_ctrl + group * 4;
  1584. dpm_reg = priv->r->mir_dpm + group * 4 * priv->port_width;
  1585. spm_reg = priv->r->mir_spm + group * 4 * priv->port_width;
  1586. pr_debug("Using group %d\n", group);
  1587. mutex_lock(&priv->reg_mutex);
  1588. if (priv->family_id == RTL8380_FAMILY_ID) {
  1589. /* Enable mirroring to port across VLANs (bit 11) */
  1590. sw_w32(1 << 11 | (mirror->to_local_port << 4) | 1, ctrl_reg);
  1591. } else {
  1592. /* Enable mirroring to destination port */
  1593. sw_w32((mirror->to_local_port << 4) | 1, ctrl_reg);
  1594. }
  1595. if (ingress && (priv->r->get_port_reg_be(spm_reg) & (1ULL << port))) {
  1596. mutex_unlock(&priv->reg_mutex);
  1597. return -EEXIST;
  1598. }
  1599. if ((!ingress) && (priv->r->get_port_reg_be(dpm_reg) & (1ULL << port))) {
  1600. mutex_unlock(&priv->reg_mutex);
  1601. return -EEXIST;
  1602. }
  1603. if (ingress)
  1604. priv->r->mask_port_reg_be(0, 1ULL << port, spm_reg);
  1605. else
  1606. priv->r->mask_port_reg_be(0, 1ULL << port, dpm_reg);
  1607. priv->mirror_group_ports[group] = mirror->to_local_port;
  1608. mutex_unlock(&priv->reg_mutex);
  1609. return 0;
  1610. }
  1611. static void rtl83xx_port_mirror_del(struct dsa_switch *ds, int port,
  1612. struct dsa_mall_mirror_tc_entry *mirror)
  1613. {
  1614. int group = 0;
  1615. struct rtl838x_switch_priv *priv = ds->priv;
  1616. int ctrl_reg, dpm_reg, spm_reg;
  1617. pr_debug("In %s\n", __func__);
  1618. for (group = 0; group < 4; group++) {
  1619. if (priv->mirror_group_ports[group] == mirror->to_local_port)
  1620. break;
  1621. }
  1622. if (group >= 4)
  1623. return;
  1624. ctrl_reg = priv->r->mir_ctrl + group * 4;
  1625. dpm_reg = priv->r->mir_dpm + group * 4 * priv->port_width;
  1626. spm_reg = priv->r->mir_spm + group * 4 * priv->port_width;
  1627. mutex_lock(&priv->reg_mutex);
  1628. if (mirror->ingress) {
  1629. /* Ingress, clear source port matrix */
  1630. priv->r->mask_port_reg_be(1ULL << port, 0, spm_reg);
  1631. } else {
  1632. /* Egress, clear destination port matrix */
  1633. priv->r->mask_port_reg_be(1ULL << port, 0, dpm_reg);
  1634. }
  1635. if (!(sw_r32(spm_reg) || sw_r32(dpm_reg))) {
  1636. priv->mirror_group_ports[group] = -1;
  1637. sw_w32(0, ctrl_reg);
  1638. }
  1639. mutex_unlock(&priv->reg_mutex);
  1640. }
  1641. static int rtl83xx_port_pre_bridge_flags(struct dsa_switch *ds, int port, unsigned long flags, struct netlink_ext_ack *extack)
  1642. {
  1643. struct rtl838x_switch_priv *priv = ds->priv;
  1644. unsigned long features = 0;
  1645. pr_debug("%s: %d %lX\n", __func__, port, flags);
  1646. if (priv->r->enable_learning)
  1647. features |= BR_LEARNING;
  1648. if (priv->r->enable_flood)
  1649. features |= BR_FLOOD;
  1650. if (priv->r->enable_mcast_flood)
  1651. features |= BR_MCAST_FLOOD;
  1652. if (priv->r->enable_bcast_flood)
  1653. features |= BR_BCAST_FLOOD;
  1654. if (flags & ~(features))
  1655. return -EINVAL;
  1656. return 0;
  1657. }
  1658. static int rtl83xx_port_bridge_flags(struct dsa_switch *ds, int port, unsigned long flags, struct netlink_ext_ack *extack)
  1659. {
  1660. struct rtl838x_switch_priv *priv = ds->priv;
  1661. pr_debug("%s: %d %lX\n", __func__, port, flags);
  1662. if (priv->r->enable_learning)
  1663. priv->r->enable_learning(port, !!(flags & BR_LEARNING));
  1664. if (priv->r->enable_flood)
  1665. priv->r->enable_flood(port, !!(flags & BR_FLOOD));
  1666. if (priv->r->enable_mcast_flood)
  1667. priv->r->enable_mcast_flood(port, !!(flags & BR_MCAST_FLOOD));
  1668. if (priv->r->enable_bcast_flood)
  1669. priv->r->enable_bcast_flood(port, !!(flags & BR_BCAST_FLOOD));
  1670. return 0;
  1671. }
  1672. static bool rtl83xx_lag_can_offload(struct dsa_switch *ds,
  1673. struct net_device *lag,
  1674. struct netdev_lag_upper_info *info)
  1675. {
  1676. int id;
  1677. id = dsa_lag_id(ds->dst, lag);
  1678. if (id < 0 || id >= ds->num_lag_ids)
  1679. return false;
  1680. if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
  1681. return false;
  1682. }
  1683. if (info->hash_type != NETDEV_LAG_HASH_L2 && info->hash_type != NETDEV_LAG_HASH_L23)
  1684. return false;
  1685. return true;
  1686. }
  1687. static int rtl83xx_port_lag_change(struct dsa_switch *ds, int port)
  1688. {
  1689. struct rtl838x_switch_priv *priv = ds->priv;
  1690. pr_debug("%s: %d\n", __func__, port);
  1691. // Nothing to be done...
  1692. return 0;
  1693. }
  1694. static int rtl83xx_port_lag_join(struct dsa_switch *ds, int port,
  1695. struct net_device *lag,
  1696. struct netdev_lag_upper_info *info)
  1697. {
  1698. struct rtl838x_switch_priv *priv = ds->priv;
  1699. int i, err = 0;
  1700. if (!rtl83xx_lag_can_offload(ds, lag, info))
  1701. return -EOPNOTSUPP;
  1702. mutex_lock(&priv->reg_mutex);
  1703. for (i = 0; i < priv->n_lags; i++) {
  1704. if ((!priv->lag_devs[i]) || (priv->lag_devs[i] == lag))
  1705. break;
  1706. }
  1707. if (port >= priv->cpu_port) {
  1708. err = -EINVAL;
  1709. goto out;
  1710. }
  1711. pr_info("port_lag_join: group %d, port %d\n",i, port);
  1712. if (!priv->lag_devs[i])
  1713. priv->lag_devs[i] = lag;
  1714. if (priv->lag_primary[i]==-1) {
  1715. priv->lag_primary[i]=port;
  1716. } else
  1717. priv->is_lagmember[port] = 1;
  1718. priv->lagmembers |= (1ULL << port);
  1719. pr_debug("lag_members = %llX\n", priv->lagmembers);
  1720. err = rtl83xx_lag_add(priv->ds, i, port, info);
  1721. if (err) {
  1722. err = -EINVAL;
  1723. goto out;
  1724. }
  1725. out:
  1726. mutex_unlock(&priv->reg_mutex);
  1727. return err;
  1728. }
  1729. static int rtl83xx_port_lag_leave(struct dsa_switch *ds, int port,
  1730. struct net_device *lag)
  1731. {
  1732. int i, group = -1, err;
  1733. struct rtl838x_switch_priv *priv = ds->priv;
  1734. mutex_lock(&priv->reg_mutex);
  1735. for (i=0;i<priv->n_lags;i++) {
  1736. if (priv->lags_port_members[i] & BIT_ULL(port)) {
  1737. group = i;
  1738. break;
  1739. }
  1740. }
  1741. if (group == -1) {
  1742. pr_info("port_lag_leave: port %d is not a member\n", port);
  1743. err = -EINVAL;
  1744. goto out;
  1745. }
  1746. if (port >= priv->cpu_port) {
  1747. err = -EINVAL;
  1748. goto out;
  1749. }
  1750. pr_info("port_lag_del: group %d, port %d\n",group, port);
  1751. priv->lagmembers &=~ (1ULL << port);
  1752. priv->lag_primary[i] = -1;
  1753. priv->is_lagmember[port] = 0;
  1754. pr_debug("lag_members = %llX\n", priv->lagmembers);
  1755. err = rtl83xx_lag_del(priv->ds, group, port);
  1756. if (err) {
  1757. err = -EINVAL;
  1758. goto out;
  1759. }
  1760. if (!priv->lags_port_members[i])
  1761. priv->lag_devs[i] = NULL;
  1762. out:
  1763. mutex_unlock(&priv->reg_mutex);
  1764. return 0;
  1765. }
  1766. int dsa_phy_read(struct dsa_switch *ds, int phy_addr, int phy_reg)
  1767. {
  1768. u32 val;
  1769. u32 offset = 0;
  1770. struct rtl838x_switch_priv *priv = ds->priv;
  1771. if (phy_addr >= 24 && phy_addr <= 27
  1772. && priv->ports[24].phy == PHY_RTL838X_SDS) {
  1773. if (phy_addr == 26)
  1774. offset = 0x100;
  1775. val = sw_r32(RTL838X_SDS4_FIB_REG0 + offset + (phy_reg << 2)) & 0xffff;
  1776. return val;
  1777. }
  1778. read_phy(phy_addr, 0, phy_reg, &val);
  1779. return val;
  1780. }
  1781. int dsa_phy_write(struct dsa_switch *ds, int phy_addr, int phy_reg, u16 val)
  1782. {
  1783. u32 offset = 0;
  1784. struct rtl838x_switch_priv *priv = ds->priv;
  1785. if (phy_addr >= 24 && phy_addr <= 27
  1786. && priv->ports[24].phy == PHY_RTL838X_SDS) {
  1787. if (phy_addr == 26)
  1788. offset = 0x100;
  1789. sw_w32(val, RTL838X_SDS4_FIB_REG0 + offset + (phy_reg << 2));
  1790. return 0;
  1791. }
  1792. return write_phy(phy_addr, 0, phy_reg, val);
  1793. }
  1794. const struct dsa_switch_ops rtl83xx_switch_ops = {
  1795. .get_tag_protocol = rtl83xx_get_tag_protocol,
  1796. .setup = rtl83xx_setup,
  1797. .phy_read = dsa_phy_read,
  1798. .phy_write = dsa_phy_write,
  1799. .phylink_validate = rtl83xx_phylink_validate,
  1800. .phylink_mac_link_state = rtl83xx_phylink_mac_link_state,
  1801. .phylink_mac_config = rtl83xx_phylink_mac_config,
  1802. .phylink_mac_link_down = rtl83xx_phylink_mac_link_down,
  1803. .phylink_mac_link_up = rtl83xx_phylink_mac_link_up,
  1804. .get_strings = rtl83xx_get_strings,
  1805. .get_ethtool_stats = rtl83xx_get_ethtool_stats,
  1806. .get_sset_count = rtl83xx_get_sset_count,
  1807. .port_enable = rtl83xx_port_enable,
  1808. .port_disable = rtl83xx_port_disable,
  1809. .get_mac_eee = rtl83xx_get_mac_eee,
  1810. .set_mac_eee = rtl83xx_set_mac_eee,
  1811. .set_ageing_time = rtl83xx_set_ageing_time,
  1812. .port_bridge_join = rtl83xx_port_bridge_join,
  1813. .port_bridge_leave = rtl83xx_port_bridge_leave,
  1814. .port_stp_state_set = rtl83xx_port_stp_state_set,
  1815. .port_fast_age = rtl83xx_fast_age,
  1816. .port_vlan_filtering = rtl83xx_vlan_filtering,
  1817. .port_vlan_prepare = rtl83xx_vlan_prepare,
  1818. .port_vlan_add = rtl83xx_vlan_add,
  1819. .port_vlan_del = rtl83xx_vlan_del,
  1820. .port_fdb_add = rtl83xx_port_fdb_add,
  1821. .port_fdb_del = rtl83xx_port_fdb_del,
  1822. .port_fdb_dump = rtl83xx_port_fdb_dump,
  1823. .port_mdb_prepare = rtl83xx_port_mdb_prepare,
  1824. .port_mdb_add = rtl83xx_port_mdb_add,
  1825. .port_mdb_del = rtl83xx_port_mdb_del,
  1826. .port_mirror_add = rtl83xx_port_mirror_add,
  1827. .port_mirror_del = rtl83xx_port_mirror_del,
  1828. .port_lag_change = rtl83xx_port_lag_change,
  1829. .port_lag_join = rtl83xx_port_lag_join,
  1830. .port_lag_leave = rtl83xx_port_lag_leave,
  1831. .port_pre_bridge_flags = rtl83xx_port_pre_bridge_flags,
  1832. .port_bridge_flags = rtl83xx_port_bridge_flags,
  1833. };
  1834. const struct dsa_switch_ops rtl930x_switch_ops = {
  1835. .get_tag_protocol = rtl83xx_get_tag_protocol,
  1836. .setup = rtl93xx_setup,
  1837. .phy_read = dsa_phy_read,
  1838. .phy_write = dsa_phy_write,
  1839. .phylink_validate = rtl93xx_phylink_validate,
  1840. .phylink_mac_link_state = rtl93xx_phylink_mac_link_state,
  1841. .phylink_mac_config = rtl93xx_phylink_mac_config,
  1842. .phylink_mac_link_down = rtl93xx_phylink_mac_link_down,
  1843. .phylink_mac_link_up = rtl93xx_phylink_mac_link_up,
  1844. .get_strings = rtl83xx_get_strings,
  1845. .get_ethtool_stats = rtl83xx_get_ethtool_stats,
  1846. .get_sset_count = rtl83xx_get_sset_count,
  1847. .port_enable = rtl83xx_port_enable,
  1848. .port_disable = rtl83xx_port_disable,
  1849. .get_mac_eee = rtl93xx_get_mac_eee,
  1850. .set_mac_eee = rtl83xx_set_mac_eee,
  1851. .set_ageing_time = rtl83xx_set_ageing_time,
  1852. .port_bridge_join = rtl83xx_port_bridge_join,
  1853. .port_bridge_leave = rtl83xx_port_bridge_leave,
  1854. .port_stp_state_set = rtl83xx_port_stp_state_set,
  1855. .port_fast_age = rtl930x_fast_age,
  1856. .port_vlan_filtering = rtl83xx_vlan_filtering,
  1857. .port_vlan_prepare = rtl83xx_vlan_prepare,
  1858. .port_vlan_add = rtl83xx_vlan_add,
  1859. .port_vlan_del = rtl83xx_vlan_del,
  1860. .port_fdb_add = rtl83xx_port_fdb_add,
  1861. .port_fdb_del = rtl83xx_port_fdb_del,
  1862. .port_fdb_dump = rtl83xx_port_fdb_dump,
  1863. .port_mdb_prepare = rtl83xx_port_mdb_prepare,
  1864. .port_mdb_add = rtl83xx_port_mdb_add,
  1865. .port_mdb_del = rtl83xx_port_mdb_del,
  1866. .port_lag_change = rtl83xx_port_lag_change,
  1867. .port_lag_join = rtl83xx_port_lag_join,
  1868. .port_lag_leave = rtl83xx_port_lag_leave,
  1869. .port_pre_bridge_flags = rtl83xx_port_pre_bridge_flags,
  1870. .port_bridge_flags = rtl83xx_port_bridge_flags,
  1871. };