dsa.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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_print_matrix(void)
  8. {
  9. unsigned volatile int *ptr8;
  10. volatile u64 *ptr9;
  11. int i;
  12. if (soc_info.family == RTL8380_FAMILY_ID) {
  13. ptr8 = RTL838X_SW_BASE + RTL838X_PORT_ISO_CTRL(0);
  14. for (i = 0; i < 28; i += 8)
  15. pr_debug("> %8x %8x %8x %8x %8x %8x %8x %8x\n",
  16. ptr8[i + 0], ptr8[i + 1], ptr8[i + 2], ptr8[i + 3],
  17. ptr8[i + 4], ptr8[i + 5], ptr8[i + 6], ptr8[i + 7]);
  18. pr_debug("CPU_PORT> %8x\n", ptr8[28]);
  19. } else {
  20. ptr9 = RTL838X_SW_BASE + RTL839X_PORT_ISO_CTRL(0);
  21. for (i = 0; i < 52; i += 4)
  22. pr_debug("> %16llx %16llx %16llx %16llx\n",
  23. ptr9[i + 0], ptr9[i + 1], ptr9[i + 2], ptr9[i + 3]);
  24. pr_debug("CPU_PORT> %16llx\n", ptr9[52]);
  25. }
  26. }
  27. static void rtl83xx_init_stats(struct rtl838x_switch_priv *priv)
  28. {
  29. mutex_lock(&priv->reg_mutex);
  30. /* Enable statistics module: all counters plus debug.
  31. * On RTL839x all counters are enabled by default
  32. */
  33. if (priv->family_id == RTL8380_FAMILY_ID)
  34. sw_w32_mask(0, 3, RTL838X_STAT_CTRL);
  35. /* Reset statistics counters */
  36. sw_w32_mask(0, 1, priv->r->stat_rst);
  37. mutex_unlock(&priv->reg_mutex);
  38. }
  39. static void rtl83xx_write_cam(int idx, u32 *r)
  40. {
  41. u32 cmd = BIT(16) /* Execute cmd */
  42. | BIT(15) /* Read */
  43. | BIT(13) /* Table type 0b01 */
  44. | (idx & 0x3f);
  45. sw_w32(r[0], RTL838X_TBL_ACCESS_L2_DATA(0));
  46. sw_w32(r[1], RTL838X_TBL_ACCESS_L2_DATA(1));
  47. sw_w32(r[2], RTL838X_TBL_ACCESS_L2_DATA(2));
  48. sw_w32(cmd, RTL838X_TBL_ACCESS_L2_CTRL);
  49. do { } while (sw_r32(RTL838X_TBL_ACCESS_L2_CTRL) & BIT(16));
  50. }
  51. static u64 rtl83xx_hash_key(struct rtl838x_switch_priv *priv, u64 mac, u32 vid)
  52. {
  53. if (priv->family_id == RTL8380_FAMILY_ID)
  54. return rtl838x_hash(priv, mac << 12 | vid);
  55. else
  56. return rtl839x_hash(priv, mac << 12 | vid);
  57. }
  58. static void rtl83xx_write_hash(int idx, u32 *r)
  59. {
  60. u32 cmd = BIT(16) /* Execute cmd */
  61. | 0 << 15 /* Write */
  62. | 0 << 13 /* Table type 0b00 */
  63. | (idx & 0x1fff);
  64. sw_w32(0, RTL838X_TBL_ACCESS_L2_DATA(0));
  65. sw_w32(0, RTL838X_TBL_ACCESS_L2_DATA(1));
  66. sw_w32(0, RTL838X_TBL_ACCESS_L2_DATA(2));
  67. sw_w32(cmd, RTL838X_TBL_ACCESS_L2_CTRL);
  68. do { } while (sw_r32(RTL838X_TBL_ACCESS_L2_CTRL) & BIT(16));
  69. }
  70. static void rtl83xx_enable_phy_polling(struct rtl838x_switch_priv *priv)
  71. {
  72. int i;
  73. u64 v = 0;
  74. msleep(1000);
  75. /* Enable all ports with a PHY, including the SFP-ports */
  76. for (i = 0; i < priv->cpu_port; i++) {
  77. if (priv->ports[i].phy)
  78. v |= BIT(i);
  79. }
  80. pr_debug("%s: %16llx\n", __func__, v);
  81. priv->r->set_port_reg_le(v, priv->r->smi_poll_ctrl);
  82. /* PHY update complete */
  83. if (priv->family_id == RTL8390_FAMILY_ID)
  84. sw_w32_mask(0, BIT(7), RTL839X_SMI_GLB_CTRL);
  85. else
  86. sw_w32_mask(0, 0x8000, RTL838X_SMI_GLB_CTRL);
  87. }
  88. const struct rtl83xx_mib_desc rtl83xx_mib[] = {
  89. MIB_DESC(2, 0xf8, "ifInOctets"),
  90. MIB_DESC(2, 0xf0, "ifOutOctets"),
  91. MIB_DESC(1, 0xec, "dot1dTpPortInDiscards"),
  92. MIB_DESC(1, 0xe8, "ifInUcastPkts"),
  93. MIB_DESC(1, 0xe4, "ifInMulticastPkts"),
  94. MIB_DESC(1, 0xe0, "ifInBroadcastPkts"),
  95. MIB_DESC(1, 0xdc, "ifOutUcastPkts"),
  96. MIB_DESC(1, 0xd8, "ifOutMulticastPkts"),
  97. MIB_DESC(1, 0xd4, "ifOutBroadcastPkts"),
  98. MIB_DESC(1, 0xd0, "ifOutDiscards"),
  99. MIB_DESC(1, 0xcc, ".3SingleCollisionFrames"),
  100. MIB_DESC(1, 0xc8, ".3MultipleCollisionFrames"),
  101. MIB_DESC(1, 0xc4, ".3DeferredTransmissions"),
  102. MIB_DESC(1, 0xc0, ".3LateCollisions"),
  103. MIB_DESC(1, 0xbc, ".3ExcessiveCollisions"),
  104. MIB_DESC(1, 0xb8, ".3SymbolErrors"),
  105. MIB_DESC(1, 0xb4, ".3ControlInUnknownOpcodes"),
  106. MIB_DESC(1, 0xb0, ".3InPauseFrames"),
  107. MIB_DESC(1, 0xac, ".3OutPauseFrames"),
  108. MIB_DESC(1, 0xa8, "DropEvents"),
  109. MIB_DESC(1, 0xa4, "tx_BroadcastPkts"),
  110. MIB_DESC(1, 0xa0, "tx_MulticastPkts"),
  111. MIB_DESC(1, 0x9c, "CRCAlignErrors"),
  112. MIB_DESC(1, 0x98, "tx_UndersizePkts"),
  113. MIB_DESC(1, 0x94, "rx_UndersizePkts"),
  114. MIB_DESC(1, 0x90, "rx_UndersizedropPkts"),
  115. MIB_DESC(1, 0x8c, "tx_OversizePkts"),
  116. MIB_DESC(1, 0x88, "rx_OversizePkts"),
  117. MIB_DESC(1, 0x84, "Fragments"),
  118. MIB_DESC(1, 0x80, "Jabbers"),
  119. MIB_DESC(1, 0x7c, "Collisions"),
  120. MIB_DESC(1, 0x78, "tx_Pkts64Octets"),
  121. MIB_DESC(1, 0x74, "rx_Pkts64Octets"),
  122. MIB_DESC(1, 0x70, "tx_Pkts65to127Octets"),
  123. MIB_DESC(1, 0x6c, "rx_Pkts65to127Octets"),
  124. MIB_DESC(1, 0x68, "tx_Pkts128to255Octets"),
  125. MIB_DESC(1, 0x64, "rx_Pkts128to255Octets"),
  126. MIB_DESC(1, 0x60, "tx_Pkts256to511Octets"),
  127. MIB_DESC(1, 0x5c, "rx_Pkts256to511Octets"),
  128. MIB_DESC(1, 0x58, "tx_Pkts512to1023Octets"),
  129. MIB_DESC(1, 0x54, "rx_Pkts512to1023Octets"),
  130. MIB_DESC(1, 0x50, "tx_Pkts1024to1518Octets"),
  131. MIB_DESC(1, 0x4c, "rx_StatsPkts1024to1518Octets"),
  132. MIB_DESC(1, 0x48, "tx_Pkts1519toMaxOctets"),
  133. MIB_DESC(1, 0x44, "rx_Pkts1519toMaxOctets"),
  134. MIB_DESC(1, 0x40, "rxMacDiscards")
  135. };
  136. /* DSA callbacks */
  137. static enum dsa_tag_protocol rtl83xx_get_tag_protocol(struct dsa_switch *ds, int port)
  138. {
  139. /* The switch does not tag the frames, instead internally the header
  140. * structure for each packet is tagged accordingly.
  141. */
  142. return DSA_TAG_PROTO_TRAILER;
  143. }
  144. static int rtl83xx_setup(struct dsa_switch *ds)
  145. {
  146. int i;
  147. struct rtl838x_switch_priv *priv = ds->priv;
  148. u64 port_bitmap = BIT_ULL(priv->cpu_port);
  149. pr_debug("%s called\n", __func__);
  150. /* Disable MAC polling the PHY so that we can start configuration */
  151. priv->r->set_port_reg_le(0ULL, priv->r->smi_poll_ctrl);
  152. for (i = 0; i < ds->num_ports; i++)
  153. priv->ports[i].enable = false;
  154. priv->ports[priv->cpu_port].enable = true;
  155. /* Isolate ports from each other: traffic only CPU <-> port */
  156. /* Setting bit j in register RTL838X_PORT_ISO_CTRL(i) allows
  157. * traffic from source port i to destination port j
  158. */
  159. for (i = 0; i < priv->cpu_port; i++) {
  160. if (priv->ports[i].phy) {
  161. priv->r->set_port_reg_be(BIT_ULL(priv->cpu_port) | BIT(i),
  162. priv->r->port_iso_ctrl(i));
  163. port_bitmap |= BIT_ULL(i);
  164. }
  165. }
  166. priv->r->set_port_reg_be(port_bitmap, priv->r->port_iso_ctrl(priv->cpu_port));
  167. rtl83xx_print_matrix();
  168. rtl83xx_init_stats(priv);
  169. ds->configure_vlan_while_not_filtering = true;
  170. /* Enable MAC Polling PHY again */
  171. rtl83xx_enable_phy_polling(priv);
  172. pr_debug("Please wait until PHY is settled\n");
  173. msleep(1000);
  174. return 0;
  175. }
  176. static void rtl83xx_phylink_validate(struct dsa_switch *ds, int port,
  177. unsigned long *supported,
  178. struct phylink_link_state *state)
  179. {
  180. struct rtl838x_switch_priv *priv = ds->priv;
  181. __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
  182. pr_debug("In %s port %d", __func__, port);
  183. if (!phy_interface_mode_is_rgmii(state->interface) &&
  184. state->interface != PHY_INTERFACE_MODE_1000BASEX &&
  185. state->interface != PHY_INTERFACE_MODE_MII &&
  186. state->interface != PHY_INTERFACE_MODE_REVMII &&
  187. state->interface != PHY_INTERFACE_MODE_GMII &&
  188. state->interface != PHY_INTERFACE_MODE_QSGMII &&
  189. state->interface != PHY_INTERFACE_MODE_INTERNAL &&
  190. state->interface != PHY_INTERFACE_MODE_SGMII) {
  191. bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
  192. dev_err(ds->dev,
  193. "Unsupported interface: %d for port %d\n",
  194. state->interface, port);
  195. return;
  196. }
  197. /* Allow all the expected bits */
  198. phylink_set(mask, Autoneg);
  199. phylink_set_port_modes(mask);
  200. phylink_set(mask, Pause);
  201. phylink_set(mask, Asym_Pause);
  202. /* With the exclusion of MII and Reverse MII, we support Gigabit,
  203. * including Half duplex
  204. */
  205. if (state->interface != PHY_INTERFACE_MODE_MII &&
  206. state->interface != PHY_INTERFACE_MODE_REVMII) {
  207. phylink_set(mask, 1000baseT_Full);
  208. phylink_set(mask, 1000baseT_Half);
  209. }
  210. /* On both the 8380 and 8382, ports 24-27 are SFP ports */
  211. if (port >= 24 && port <= 27 && priv->family_id == RTL8380_FAMILY_ID)
  212. phylink_set(mask, 1000baseX_Full);
  213. phylink_set(mask, 10baseT_Half);
  214. phylink_set(mask, 10baseT_Full);
  215. phylink_set(mask, 100baseT_Half);
  216. phylink_set(mask, 100baseT_Full);
  217. bitmap_and(supported, supported, mask,
  218. __ETHTOOL_LINK_MODE_MASK_NBITS);
  219. bitmap_and(state->advertising, state->advertising, mask,
  220. __ETHTOOL_LINK_MODE_MASK_NBITS);
  221. }
  222. static int rtl83xx_phylink_mac_link_state(struct dsa_switch *ds, int port,
  223. struct phylink_link_state *state)
  224. {
  225. struct rtl838x_switch_priv *priv = ds->priv;
  226. u64 speed;
  227. if (port < 0 || port > priv->cpu_port)
  228. return -EINVAL;
  229. state->link = 0;
  230. if (priv->r->get_port_reg_le(priv->r->mac_link_sts) & BIT_ULL(port))
  231. state->link = 1;
  232. state->duplex = 0;
  233. if (priv->r->get_port_reg_le(priv->r->mac_link_dup_sts) & BIT_ULL(port))
  234. state->duplex = 1;
  235. speed = priv->r->get_port_reg_le(priv->r->mac_link_spd_sts(port));
  236. speed >>= (port % 16) << 1;
  237. switch (speed & 0x3) {
  238. case 0:
  239. state->speed = SPEED_10;
  240. break;
  241. case 1:
  242. state->speed = SPEED_100;
  243. break;
  244. case 2:
  245. state->speed = SPEED_1000;
  246. break;
  247. case 3:
  248. if (port == 24 || port == 26) /* Internal serdes */
  249. state->speed = SPEED_2500;
  250. else
  251. state->speed = SPEED_100; /* Is in fact 500Mbit */
  252. }
  253. state->pause &= (MLO_PAUSE_RX | MLO_PAUSE_TX);
  254. if (priv->r->get_port_reg_le(priv->r->mac_rx_pause_sts) & BIT_ULL(port))
  255. state->pause |= MLO_PAUSE_RX;
  256. if (priv->r->get_port_reg_le(priv->r->mac_tx_pause_sts) & BIT_ULL(port))
  257. state->pause |= MLO_PAUSE_TX;
  258. return 1;
  259. }
  260. static void rtl83xx_phylink_mac_config(struct dsa_switch *ds, int port,
  261. unsigned int mode,
  262. const struct phylink_link_state *state)
  263. {
  264. struct rtl838x_switch_priv *priv = ds->priv;
  265. u32 reg;
  266. int speed_bit = priv->family_id == RTL8380_FAMILY_ID ? 4 : 3;
  267. pr_debug("%s port %d, mode %x\n", __func__, port, mode);
  268. if (port == priv->cpu_port) {
  269. /* Set Speed, duplex, flow control
  270. * FORCE_EN | LINK_EN | NWAY_EN | DUP_SEL
  271. * | SPD_SEL = 0b10 | FORCE_FC_EN | PHY_MASTER_SLV_MANUAL_EN
  272. * | MEDIA_SEL
  273. */
  274. if (priv->family_id == RTL8380_FAMILY_ID) {
  275. sw_w32(0x6192F, priv->r->mac_force_mode_ctrl(priv->cpu_port));
  276. /* allow CRC errors on CPU-port */
  277. sw_w32_mask(0, 0x8, RTL838X_MAC_PORT_CTRL(priv->cpu_port));
  278. } else {
  279. sw_w32_mask(0, 3, priv->r->mac_force_mode_ctrl(priv->cpu_port));
  280. }
  281. return;
  282. }
  283. reg = sw_r32(priv->r->mac_force_mode_ctrl(port));
  284. /* Auto-Negotiation does not work for MAC in RTL8390 */
  285. if (priv->family_id == RTL8380_FAMILY_ID) {
  286. if (mode == MLO_AN_PHY) {
  287. pr_debug("PHY autonegotiates\n");
  288. reg |= BIT(2);
  289. sw_w32(reg, priv->r->mac_force_mode_ctrl(port));
  290. return;
  291. }
  292. }
  293. if (mode != MLO_AN_FIXED)
  294. pr_debug("Fixed state.\n");
  295. if (priv->family_id == RTL8380_FAMILY_ID) {
  296. /* Clear id_mode_dis bit, and the existing port mode, let
  297. * RGMII_MODE_EN bet set by mac_link_{up,down}
  298. */
  299. reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
  300. if (state->pause & MLO_PAUSE_TXRX_MASK) {
  301. if (state->pause & MLO_PAUSE_TX)
  302. reg |= TX_PAUSE_EN;
  303. reg |= RX_PAUSE_EN;
  304. }
  305. }
  306. reg &= ~(3 << speed_bit);
  307. switch (state->speed) {
  308. case SPEED_1000:
  309. reg |= 2 << speed_bit;
  310. break;
  311. case SPEED_100:
  312. reg |= 1 << speed_bit;
  313. break;
  314. }
  315. if (priv->family_id == RTL8380_FAMILY_ID) {
  316. reg &= ~(DUPLEX_FULL | FORCE_LINK_EN);
  317. if (state->link)
  318. reg |= FORCE_LINK_EN;
  319. if (state->duplex == DUPLEX_FULL)
  320. reg |= DUPLX_MODE;
  321. }
  322. // Disable AN
  323. if (priv->family_id == RTL8380_FAMILY_ID)
  324. reg &= ~BIT(2);
  325. sw_w32(reg, priv->r->mac_force_mode_ctrl(port));
  326. }
  327. static void rtl83xx_phylink_mac_link_down(struct dsa_switch *ds, int port,
  328. unsigned int mode,
  329. phy_interface_t interface)
  330. {
  331. struct rtl838x_switch_priv *priv = ds->priv;
  332. /* Stop TX/RX to port */
  333. sw_w32_mask(0x3, 0, priv->r->mac_port_ctrl(port));
  334. }
  335. static void rtl83xx_phylink_mac_link_up(struct dsa_switch *ds, int port,
  336. unsigned int mode,
  337. phy_interface_t interface,
  338. struct phy_device *phydev)
  339. {
  340. struct rtl838x_switch_priv *priv = ds->priv;
  341. /* Restart TX/RX to port */
  342. sw_w32_mask(0, 0x3, priv->r->mac_port_ctrl(port));
  343. }
  344. static void rtl83xx_get_strings(struct dsa_switch *ds,
  345. int port, u32 stringset, u8 *data)
  346. {
  347. int i;
  348. if (stringset != ETH_SS_STATS)
  349. return;
  350. for (i = 0; i < ARRAY_SIZE(rtl83xx_mib); i++)
  351. strncpy(data + i * ETH_GSTRING_LEN, rtl83xx_mib[i].name,
  352. ETH_GSTRING_LEN);
  353. }
  354. static void rtl83xx_get_ethtool_stats(struct dsa_switch *ds, int port,
  355. uint64_t *data)
  356. {
  357. struct rtl838x_switch_priv *priv = ds->priv;
  358. const struct rtl83xx_mib_desc *mib;
  359. int i;
  360. u64 high;
  361. for (i = 0; i < ARRAY_SIZE(rtl83xx_mib); i++) {
  362. mib = &rtl83xx_mib[i];
  363. data[i] = sw_r32(priv->r->stat_port_std_mib(port) + 252 - mib->offset);
  364. if (mib->size == 2) {
  365. high = sw_r32(priv->r->stat_port_std_mib(port) + 252 - mib->offset - 4);
  366. data[i] |= high << 32;
  367. }
  368. }
  369. }
  370. static int rtl83xx_get_sset_count(struct dsa_switch *ds, int port, int sset)
  371. {
  372. if (sset != ETH_SS_STATS)
  373. return 0;
  374. return ARRAY_SIZE(rtl83xx_mib);
  375. }
  376. static int rtl83xx_port_enable(struct dsa_switch *ds, int port,
  377. struct phy_device *phydev)
  378. {
  379. struct rtl838x_switch_priv *priv = ds->priv;
  380. pr_debug("%s: %x %d", __func__, (u32) priv, port);
  381. priv->ports[port].enable = true;
  382. /* enable inner tagging on egress, do not keep any tags */
  383. sw_w32(1, priv->r->vlan_port_tag_sts_ctrl(port));
  384. if (dsa_is_cpu_port(ds, port))
  385. return 0;
  386. /* add port to switch mask of CPU_PORT */
  387. priv->r->mask_port_reg_be(0ULL, BIT_ULL(port), priv->r->port_iso_ctrl(priv->cpu_port));
  388. /* add all other ports in the same bridge to switch mask of port */
  389. priv->r->mask_port_reg_be(0ULL, priv->ports[port].pm, priv->r->port_iso_ctrl(port));
  390. return 0;
  391. }
  392. static void rtl83xx_port_disable(struct dsa_switch *ds, int port)
  393. {
  394. struct rtl838x_switch_priv *priv = ds->priv;
  395. pr_debug("%s %x: %d", __func__, (u32)priv, port);
  396. /* you can only disable user ports */
  397. if (!dsa_is_user_port(ds, port))
  398. return;
  399. /* remove port from switch mask of CPU_PORT */
  400. priv->r->mask_port_reg_be(BIT_ULL(port), 0, priv->r->port_iso_ctrl(priv->cpu_port));
  401. /* remove all other ports in the same bridge from switch mask of port */
  402. priv->r->mask_port_reg_be(priv->ports[port].pm, 0LL, priv->r->port_iso_ctrl(port));
  403. priv->ports[port].enable = false;
  404. }
  405. static int rtl83xx_get_mac_eee(struct dsa_switch *ds, int port,
  406. struct ethtool_eee *e)
  407. {
  408. struct rtl838x_switch_priv *priv = ds->priv;
  409. pr_debug("%s: port %d", __func__, port);
  410. e->supported = SUPPORTED_100baseT_Full | SUPPORTED_1000baseT_Full;
  411. if (sw_r32(priv->r->mac_force_mode_ctrl(port)) & BIT(9))
  412. e->advertised |= ADVERTISED_100baseT_Full;
  413. if (sw_r32(priv->r->mac_force_mode_ctrl(port)) & BIT(10))
  414. e->advertised |= ADVERTISED_1000baseT_Full;
  415. e->eee_enabled = priv->ports[port].eee_enabled;
  416. pr_debug("enabled: %d, active %x\n", e->eee_enabled, e->advertised);
  417. if (sw_r32(RTL838X_MAC_EEE_ABLTY) & BIT(port)) {
  418. e->lp_advertised = ADVERTISED_100baseT_Full;
  419. e->lp_advertised |= ADVERTISED_1000baseT_Full;
  420. }
  421. e->eee_active = !!(e->advertised & e->lp_advertised);
  422. pr_debug("active: %d, lp %x\n", e->eee_active, e->lp_advertised);
  423. return 0;
  424. }
  425. static int rtl83xx_set_mac_eee(struct dsa_switch *ds, int port,
  426. struct ethtool_eee *e)
  427. {
  428. struct rtl838x_switch_priv *priv = ds->priv;
  429. pr_debug("%s: port %d", __func__, port);
  430. if (e->eee_enabled) {
  431. pr_debug("Globally enabling EEE\n");
  432. sw_w32_mask(0x4, 0, RTL838X_SMI_GLB_CTRL);
  433. }
  434. if (e->eee_enabled) {
  435. pr_debug("Enabling EEE for MAC %d\n", port);
  436. sw_w32_mask(0, 3 << 9, priv->r->mac_force_mode_ctrl(port));
  437. sw_w32_mask(0, BIT(port), RTL838X_EEE_PORT_TX_EN);
  438. sw_w32_mask(0, BIT(port), RTL838X_EEE_PORT_RX_EN);
  439. priv->ports[port].eee_enabled = true;
  440. e->eee_enabled = true;
  441. } else {
  442. pr_debug("Disabling EEE for MAC %d\n", port);
  443. sw_w32_mask(3 << 9, 0, priv->r->mac_force_mode_ctrl(port));
  444. sw_w32_mask(BIT(port), 0, RTL838X_EEE_PORT_TX_EN);
  445. sw_w32_mask(BIT(port), 0, RTL838X_EEE_PORT_RX_EN);
  446. priv->ports[port].eee_enabled = false;
  447. e->eee_enabled = false;
  448. }
  449. return 0;
  450. }
  451. /*
  452. * Set Switch L2 Aging time, t is time in milliseconds
  453. * t = 0: aging is disabled
  454. */
  455. static int rtl83xx_set_l2aging(struct dsa_switch *ds, u32 t)
  456. {
  457. struct rtl838x_switch_priv *priv = ds->priv;
  458. int t_max = priv->family_id == RTL8380_FAMILY_ID ? 0x7fffff : 0x1FFFFF;
  459. /* Convert time in mseconds to internal value */
  460. if (t > 0x10000000) { /* Set to maximum */
  461. t = t_max;
  462. } else {
  463. if (priv->family_id == RTL8380_FAMILY_ID)
  464. t = ((t * 625) / 1000 + 127) / 128;
  465. else
  466. t = (t * 5 + 2) / 3;
  467. }
  468. sw_w32(t, priv->r->l2_ctrl_1);
  469. return 0;
  470. }
  471. static int rtl83xx_port_bridge_join(struct dsa_switch *ds, int port,
  472. struct net_device *bridge)
  473. {
  474. struct rtl838x_switch_priv *priv = ds->priv;
  475. u64 port_bitmap = BIT_ULL(priv->cpu_port);
  476. int i;
  477. pr_debug("%s %x: %d %llx", __func__, (u32)priv, port, port_bitmap);
  478. mutex_lock(&priv->reg_mutex);
  479. for (i = 0; i < ds->num_ports; i++) {
  480. /* Add this port to the port matrix of the other ports in the
  481. * same bridge. If the port is disabled, port matrix is kept
  482. * and not being setup until the port becomes enabled.
  483. */
  484. if (dsa_is_user_port(ds, i) && i != port) {
  485. if (dsa_to_port(ds, i)->bridge_dev != bridge)
  486. continue;
  487. if (priv->ports[i].enable)
  488. priv->r->mask_port_reg_be(0, BIT_ULL(port),
  489. priv->r->port_iso_ctrl(i));
  490. priv->ports[i].pm |= BIT_ULL(port);
  491. port_bitmap |= BIT_ULL(i);
  492. }
  493. }
  494. /* Add all other ports to this port matrix. */
  495. if (priv->ports[port].enable) {
  496. priv->r->mask_port_reg_be(0, BIT_ULL(port),
  497. priv->r->port_iso_ctrl(priv->cpu_port));
  498. priv->r->mask_port_reg_be(0, port_bitmap,
  499. priv->r->port_iso_ctrl(port));
  500. }
  501. priv->ports[port].pm |= port_bitmap;
  502. mutex_unlock(&priv->reg_mutex);
  503. return 0;
  504. }
  505. static void rtl83xx_port_bridge_leave(struct dsa_switch *ds, int port,
  506. struct net_device *bridge)
  507. {
  508. struct rtl838x_switch_priv *priv = ds->priv;
  509. u64 port_bitmap = BIT_ULL(priv->cpu_port);
  510. int i;
  511. pr_debug("%s %x: %d", __func__, (u32)priv, port);
  512. mutex_lock(&priv->reg_mutex);
  513. for (i = 0; i < ds->num_ports; i++) {
  514. /* Remove this port from the port matrix of the other ports
  515. * in the same bridge. If the port is disabled, port matrix
  516. * is kept and not being setup until the port becomes enabled.
  517. * And the other port's port matrix cannot be broken when the
  518. * other port is still a VLAN-aware port.
  519. */
  520. if (dsa_is_user_port(ds, i) && i != port) {
  521. if (dsa_to_port(ds, i)->bridge_dev != bridge)
  522. continue;
  523. if (priv->ports[i].enable)
  524. priv->r->mask_port_reg_be(BIT_ULL(port), 0,
  525. priv->r->port_iso_ctrl(i));
  526. priv->ports[i].pm &= ~BIT_ULL(port);
  527. port_bitmap &= ~BIT_ULL(i);
  528. }
  529. }
  530. /* Add all other ports to this port matrix. */
  531. if (priv->ports[port].enable)
  532. priv->r->mask_port_reg_be(0, port_bitmap, priv->r->port_iso_ctrl(port));
  533. priv->ports[port].pm &= ~port_bitmap;
  534. mutex_unlock(&priv->reg_mutex);
  535. }
  536. static void rtl83xx_port_stp_state_set(struct dsa_switch *ds, int port,
  537. u8 state)
  538. {
  539. u32 cmd, msti = 0;
  540. u32 port_state[4];
  541. int index, bit, i;
  542. int pos = port;
  543. struct rtl838x_switch_priv *priv = ds->priv;
  544. int n = priv->family_id == RTL8380_FAMILY_ID ? 2 : 4;
  545. pr_debug("%s: port %d state %2x\n", __func__, port, state);
  546. /* CPU PORT can only be configured on RTL838x */
  547. if (port >= priv->cpu_port || port > 51)
  548. return;
  549. mutex_lock(&priv->reg_mutex);
  550. /* For the RTL839x, the bits are left-aligned in the 128 bit field */
  551. if (priv->family_id == RTL8390_FAMILY_ID)
  552. pos += 12;
  553. index = n - (pos >> 4) - 1;
  554. bit = (pos << 1) % 32;
  555. if (priv->family_id == RTL8380_FAMILY_ID) {
  556. cmd = BIT(15) /* Execute cmd */
  557. | BIT(14) /* Read */
  558. | 2 << 12 /* Table type 0b10 */
  559. | (msti & 0xfff);
  560. } else {
  561. cmd = BIT(16) /* Execute cmd */
  562. | 0 << 15 /* Read */
  563. | 5 << 12 /* Table type 0b101 */
  564. | (msti & 0xfff);
  565. }
  566. priv->r->exec_tbl0_cmd(cmd);
  567. for (i = 0; i < n; i++)
  568. port_state[i] = sw_r32(priv->r->tbl_access_data_0(i));
  569. pr_debug("Current state, port %d: %d\n", port, (port_state[index] >> bit) & 3);
  570. port_state[index] &= ~(3 << bit);
  571. switch (state) {
  572. case BR_STATE_DISABLED: /* 0 */
  573. port_state[index] |= (0 << bit);
  574. break;
  575. case BR_STATE_BLOCKING: /* 4 */
  576. case BR_STATE_LISTENING: /* 1 */
  577. port_state[index] |= (1 << bit);
  578. break;
  579. case BR_STATE_LEARNING: /* 2 */
  580. port_state[index] |= (2 << bit);
  581. break;
  582. case BR_STATE_FORWARDING: /* 3*/
  583. port_state[index] |= (3 << bit);
  584. default:
  585. break;
  586. }
  587. if (priv->family_id == RTL8380_FAMILY_ID) {
  588. cmd = BIT(15) /* Execute cmd */
  589. | 0 << 14 /* Write */
  590. | 2 << 12 /* Table type 0b10 */
  591. | (msti & 0xfff);
  592. } else {
  593. cmd = 1 << 16 /* Execute cmd */
  594. | BIT(15) /* Write */
  595. | 5 << 12 /* Table type 0b101 */
  596. | (msti & 0xfff);
  597. }
  598. for (i = 0; i < n; i++)
  599. sw_w32(port_state[i], priv->r->tbl_access_data_0(i));
  600. priv->r->exec_tbl0_cmd(cmd);
  601. mutex_unlock(&priv->reg_mutex);
  602. }
  603. static void rtl83xx_fast_age(struct dsa_switch *ds, int port)
  604. {
  605. struct rtl838x_switch_priv *priv = ds->priv;
  606. int s = priv->family_id == RTL8390_FAMILY_ID ? 2 : 0;
  607. pr_debug("FAST AGE port %d\n", port);
  608. mutex_lock(&priv->reg_mutex);
  609. /* RTL838X_L2_TBL_FLUSH_CTRL register bits, 839x has 1 bit larger
  610. * port fields:
  611. * 0-4: Replacing port
  612. * 5-9: Flushed/replaced port
  613. * 10-21: FVID
  614. * 22: Entry types: 1: dynamic, 0: also static
  615. * 23: Match flush port
  616. * 24: Match FVID
  617. * 25: Flush (0) or replace (1) L2 entries
  618. * 26: Status of action (1: Start, 0: Done)
  619. */
  620. sw_w32(1 << (26 + s) | 1 << (23 + s) | port << (5 + (s / 2)), priv->r->l2_tbl_flush_ctrl);
  621. do { } while (sw_r32(priv->r->l2_tbl_flush_ctrl) & (1 << (26 + s)));
  622. mutex_unlock(&priv->reg_mutex);
  623. }
  624. static int rtl83xx_vlan_filtering(struct dsa_switch *ds, int port,
  625. bool vlan_filtering)
  626. {
  627. struct rtl838x_switch_priv *priv = ds->priv;
  628. pr_debug("%s: port %d\n", __func__, port);
  629. mutex_lock(&priv->reg_mutex);
  630. if (vlan_filtering) {
  631. /* Enable ingress and egress filtering */
  632. if (port != priv->cpu_port)
  633. sw_w32_mask(0b10 << ((port % 16) << 1), 0b01 << ((port % 16) << 1),
  634. priv->r->vlan_port_igr_filter(port));
  635. sw_w32_mask(0, 1 << (port % 32), priv->r->vlan_port_egr_filter(port));
  636. } else {
  637. /* Disable ingress and egress filtering */
  638. if (port != priv->cpu_port)
  639. sw_w32_mask(0b11 << ((port % 16) << 1), 0,
  640. priv->r->vlan_port_igr_filter(port));
  641. sw_w32_mask(1 << (port % 32), 0, priv->r->vlan_port_egr_filter(port));
  642. }
  643. /* Do we need to do something to the CPU-Port, too? */
  644. mutex_unlock(&priv->reg_mutex);
  645. return 0;
  646. }
  647. static int rtl83xx_vlan_prepare(struct dsa_switch *ds, int port,
  648. const struct switchdev_obj_port_vlan *vlan)
  649. {
  650. struct rtl838x_vlan_info info;
  651. struct rtl838x_switch_priv *priv = ds->priv;
  652. pr_debug("%s: port %d\n", __func__, port);
  653. mutex_lock(&priv->reg_mutex);
  654. if (priv->family_id == RTL8380_FAMILY_ID)
  655. rtl838x_vlan_profile_dump(0);
  656. else
  657. rtl839x_vlan_profile_dump(0);
  658. priv->r->vlan_tables_read(0, &info);
  659. pr_debug("Tagged ports %llx, untag %llx, prof %x, MC# %d, UC# %d, FID %x\n",
  660. info.tagged_ports, info.untagged_ports, info.profile_id,
  661. info.hash_mc_fid, info.hash_uc_fid, info.fid);
  662. mutex_unlock(&priv->reg_mutex);
  663. return 0;
  664. }
  665. static void rtl83xx_vlan_add(struct dsa_switch *ds, int port,
  666. const struct switchdev_obj_port_vlan *vlan)
  667. {
  668. struct rtl838x_vlan_info info = {};
  669. struct rtl838x_switch_priv *priv = ds->priv;
  670. int v;
  671. pr_debug("%s port %d, vid_end %d, vid_end %d, flags %x\n", __func__,
  672. port, vlan->vid_begin, vlan->vid_end, vlan->flags);
  673. if (vlan->vid_begin > 4095 || vlan->vid_end > 4095) {
  674. dev_err(priv->dev, "VLAN out of range: %d - %d",
  675. vlan->vid_begin, vlan->vid_end);
  676. return;
  677. }
  678. mutex_lock(&priv->reg_mutex);
  679. if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
  680. /* Set both inner and outer PVID of the port */
  681. sw_w32((vlan->vid_end << 16) | vlan->vid_end << 2, priv->r->vlan_port_pb(port));
  682. priv->ports[port].pvid = vlan->vid_end;
  683. }
  684. for (v = vlan->vid_begin; v <= vlan->vid_end; v++) {
  685. /* Get port memberships of this vlan */
  686. priv->r->vlan_tables_read(v, &info);
  687. /* new VLAN? */
  688. if (!info.tagged_ports) {
  689. info.fid = 0;
  690. info.hash_mc_fid = false;
  691. info.hash_uc_fid = false;
  692. info.profile_id = 0;
  693. }
  694. /* sanitize untagged_ports - must be a subset */
  695. if (info.untagged_ports & ~info.tagged_ports)
  696. info.untagged_ports = 0;
  697. info.tagged_ports |= BIT_ULL(port);
  698. if (vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED)
  699. info.untagged_ports |= BIT_ULL(port);
  700. priv->r->vlan_set_untagged(v, info.untagged_ports);
  701. pr_debug("Untagged ports, VLAN %d: %llx\n", v, info.untagged_ports);
  702. priv->r->vlan_set_tagged(v, &info);
  703. pr_debug("Tagged ports, VLAN %d: %llx\n", v, info.tagged_ports);
  704. }
  705. mutex_unlock(&priv->reg_mutex);
  706. }
  707. static int rtl83xx_vlan_del(struct dsa_switch *ds, int port,
  708. const struct switchdev_obj_port_vlan *vlan)
  709. {
  710. struct rtl838x_vlan_info info;
  711. struct rtl838x_switch_priv *priv = ds->priv;
  712. int v;
  713. u16 pvid;
  714. pr_debug("%s: port %d, vid_end %d, vid_end %d, flags %x\n", __func__,
  715. port, vlan->vid_begin, vlan->vid_end, vlan->flags);
  716. if (vlan->vid_begin > 4095 || vlan->vid_end > 4095) {
  717. dev_err(priv->dev, "VLAN out of range: %d - %d",
  718. vlan->vid_begin, vlan->vid_end);
  719. return -ENOTSUPP;
  720. }
  721. mutex_lock(&priv->reg_mutex);
  722. pvid = priv->ports[port].pvid;
  723. for (v = vlan->vid_begin; v <= vlan->vid_end; v++) {
  724. /* Reset to default if removing the current PVID */
  725. if (v == pvid)
  726. sw_w32(0, priv->r->vlan_port_pb(port));
  727. /* Get port memberships of this vlan */
  728. priv->r->vlan_tables_read(v, &info);
  729. /* remove port from both tables */
  730. info.untagged_ports &= (~BIT_ULL(port));
  731. /* always leave vid 1 */
  732. if (v != 1)
  733. info.tagged_ports &= (~BIT_ULL(port));
  734. priv->r->vlan_set_untagged(v, info.untagged_ports);
  735. pr_debug("Untagged ports, VLAN %d: %llx\n", v, info.untagged_ports);
  736. priv->r->vlan_set_tagged(v, &info);
  737. pr_debug("Tagged ports, VLAN %d: %llx\n", v, info.tagged_ports);
  738. }
  739. mutex_unlock(&priv->reg_mutex);
  740. return 0;
  741. }
  742. static int rtl83xx_port_fdb_add(struct dsa_switch *ds, int port,
  743. const unsigned char *addr, u16 vid)
  744. {
  745. struct rtl838x_switch_priv *priv = ds->priv;
  746. u64 mac = ether_addr_to_u64(addr);
  747. u32 key = rtl83xx_hash_key(priv, mac, vid);
  748. struct rtl838x_l2_entry e;
  749. u32 r[3];
  750. u64 entry;
  751. int idx = -1, err = 0, i;
  752. mutex_lock(&priv->reg_mutex);
  753. for (i = 0; i < 4; i++) {
  754. entry = priv->r->read_l2_entry_using_hash(key, i, &e);
  755. if (!e.valid) {
  756. idx = (key << 2) | i;
  757. break;
  758. }
  759. if ((entry & 0x0fffffffffffffffULL) == ((mac << 12) | vid)) {
  760. idx = (key << 2) | i;
  761. break;
  762. }
  763. }
  764. if (idx >= 0) {
  765. r[0] = 3 << 17 | port << 12; // Aging and port
  766. r[0] |= vid;
  767. r[1] = mac >> 16;
  768. r[2] = (mac & 0xffff) << 12; /* rvid = 0 */
  769. rtl83xx_write_hash(idx, r);
  770. goto out;
  771. }
  772. /* Hash buckets full, try CAM */
  773. for (i = 0; i < 64; i++) {
  774. entry = priv->r->read_cam(i, &e);
  775. if (!e.valid) {
  776. if (idx < 0) /* First empty entry? */
  777. idx = i;
  778. break;
  779. } else if ((entry & 0x0fffffffffffffffULL) == ((mac << 12) | vid)) {
  780. pr_debug("Found entry in CAM\n");
  781. idx = i;
  782. break;
  783. }
  784. }
  785. if (idx >= 0) {
  786. r[0] = 3 << 17 | port << 12; // Aging
  787. r[0] |= vid;
  788. r[1] = mac >> 16;
  789. r[2] = (mac & 0xffff) << 12; /* rvid = 0 */
  790. rtl83xx_write_cam(idx, r);
  791. goto out;
  792. }
  793. err = -ENOTSUPP;
  794. out:
  795. mutex_unlock(&priv->reg_mutex);
  796. return err;
  797. }
  798. static int rtl83xx_port_fdb_del(struct dsa_switch *ds, int port,
  799. const unsigned char *addr, u16 vid)
  800. {
  801. struct rtl838x_switch_priv *priv = ds->priv;
  802. u64 mac = ether_addr_to_u64(addr);
  803. u32 key = rtl83xx_hash_key(priv, mac, vid);
  804. struct rtl838x_l2_entry e;
  805. u32 r[3];
  806. u64 entry;
  807. int idx = -1, err = 0, i;
  808. pr_debug("In %s, mac %llx, vid: %d, key: %x\n", __func__, mac, vid, key);
  809. mutex_lock(&priv->reg_mutex);
  810. for (i = 0; i < 4; i++) {
  811. entry = priv->r->read_l2_entry_using_hash(key, i, &e);
  812. if (!e.valid)
  813. continue;
  814. if ((entry & 0x0fffffffffffffffULL) == ((mac << 12) | vid)) {
  815. idx = (key << 2) | i;
  816. break;
  817. }
  818. }
  819. if (idx >= 0) {
  820. r[0] = r[1] = r[2] = 0;
  821. rtl83xx_write_hash(idx, r);
  822. goto out;
  823. }
  824. /* Check CAM for spillover from hash buckets */
  825. for (i = 0; i < 64; i++) {
  826. entry = priv->r->read_cam(i, &e);
  827. if ((entry & 0x0fffffffffffffffULL) == ((mac << 12) | vid)) {
  828. idx = i;
  829. break;
  830. }
  831. }
  832. if (idx >= 0) {
  833. r[0] = r[1] = r[2] = 0;
  834. rtl83xx_write_cam(idx, r);
  835. goto out;
  836. }
  837. err = -ENOENT;
  838. out:
  839. mutex_unlock(&priv->reg_mutex);
  840. return err;
  841. }
  842. static int rtl83xx_port_fdb_dump(struct dsa_switch *ds, int port,
  843. dsa_fdb_dump_cb_t *cb, void *data)
  844. {
  845. struct rtl838x_l2_entry e;
  846. struct rtl838x_switch_priv *priv = ds->priv;
  847. int i;
  848. u32 fid;
  849. u32 pkey;
  850. u64 mac;
  851. mutex_lock(&priv->reg_mutex);
  852. for (i = 0; i < priv->fib_entries; i++) {
  853. priv->r->read_l2_entry_using_hash(i >> 2, i & 0x3, &e);
  854. if (!e.valid)
  855. continue;
  856. if (e.port == port) {
  857. fid = (i & 0x3ff) | (e.rvid & ~0x3ff);
  858. mac = ether_addr_to_u64(&e.mac[0]);
  859. pkey = rtl838x_hash(priv, mac << 12 | fid);
  860. fid = (pkey & 0x3ff) | (fid & ~0x3ff);
  861. pr_debug("-> mac %016llx, fid: %d\n", mac, fid);
  862. cb(e.mac, e.vid, e.is_static, data);
  863. }
  864. }
  865. for (i = 0; i < 64; i++) {
  866. priv->r->read_cam(i, &e);
  867. if (!e.valid)
  868. continue;
  869. if (e.port == port)
  870. cb(e.mac, e.vid, e.is_static, data);
  871. }
  872. mutex_unlock(&priv->reg_mutex);
  873. return 0;
  874. }
  875. static int rtl83xx_port_mirror_add(struct dsa_switch *ds, int port,
  876. struct dsa_mall_mirror_tc_entry *mirror,
  877. bool ingress)
  878. {
  879. /* We support 4 mirror groups, one destination port per group */
  880. int group;
  881. struct rtl838x_switch_priv *priv = ds->priv;
  882. pr_debug("In %s\n", __func__);
  883. for (group = 0; group < 4; group++) {
  884. if (priv->mirror_group_ports[group] == mirror->to_local_port)
  885. break;
  886. }
  887. if (group >= 4) {
  888. for (group = 0; group < 4; group++) {
  889. if (priv->mirror_group_ports[group] < 0)
  890. break;
  891. }
  892. }
  893. if (group >= 4)
  894. return -ENOSPC;
  895. pr_debug("Using group %d\n", group);
  896. mutex_lock(&priv->reg_mutex);
  897. if (priv->family_id == RTL8380_FAMILY_ID) {
  898. /* Enable mirroring to port across VLANs (bit 11) */
  899. sw_w32(1 << 11 | (mirror->to_local_port << 4) | 1, RTL838X_MIR_CTRL(group));
  900. } else {
  901. /* Enable mirroring to destination port */
  902. sw_w32((mirror->to_local_port << 4) | 1, RTL839X_MIR_CTRL(group));
  903. }
  904. if (ingress && (priv->r->get_port_reg_be(priv->r->mir_spm(group)) & (1ULL << port))) {
  905. mutex_unlock(&priv->reg_mutex);
  906. return -EEXIST;
  907. }
  908. if ((!ingress) && (priv->r->get_port_reg_be(priv->r->mir_dpm(group)) & (1ULL << port))) {
  909. mutex_unlock(&priv->reg_mutex);
  910. return -EEXIST;
  911. }
  912. if (ingress)
  913. priv->r->mask_port_reg_be(0, 1ULL << port, priv->r->mir_spm(group));
  914. else
  915. priv->r->mask_port_reg_be(0, 1ULL << port, priv->r->mir_dpm(group));
  916. priv->mirror_group_ports[group] = mirror->to_local_port;
  917. mutex_unlock(&priv->reg_mutex);
  918. return 0;
  919. }
  920. static void rtl83xx_port_mirror_del(struct dsa_switch *ds, int port,
  921. struct dsa_mall_mirror_tc_entry *mirror)
  922. {
  923. int group = 0;
  924. struct rtl838x_switch_priv *priv = ds->priv;
  925. pr_debug("In %s\n", __func__);
  926. for (group = 0; group < 4; group++) {
  927. if (priv->mirror_group_ports[group] == mirror->to_local_port)
  928. break;
  929. }
  930. if (group >= 4)
  931. return;
  932. mutex_lock(&priv->reg_mutex);
  933. if (mirror->ingress) {
  934. /* Ingress, clear source port matrix */
  935. priv->r->mask_port_reg_be(1ULL << port, 0, priv->r->mir_spm(group));
  936. } else {
  937. /* Egress, clear destination port matrix */
  938. priv->r->mask_port_reg_be(1ULL << port, 0, priv->r->mir_dpm(group));
  939. }
  940. if (!(sw_r32(priv->r->mir_spm(group)) || sw_r32(priv->r->mir_dpm(group)))) {
  941. priv->mirror_group_ports[group] = -1;
  942. sw_w32(0, priv->r->mir_ctrl(group));
  943. }
  944. mutex_unlock(&priv->reg_mutex);
  945. }
  946. const struct dsa_switch_ops rtl83xx_switch_ops = {
  947. .get_tag_protocol = rtl83xx_get_tag_protocol,
  948. .setup = rtl83xx_setup,
  949. .phy_read = rtl83xx_dsa_phy_read,
  950. .phy_write = rtl83xx_dsa_phy_write,
  951. .phylink_validate = rtl83xx_phylink_validate,
  952. .phylink_mac_link_state = rtl83xx_phylink_mac_link_state,
  953. .phylink_mac_config = rtl83xx_phylink_mac_config,
  954. .phylink_mac_link_down = rtl83xx_phylink_mac_link_down,
  955. .phylink_mac_link_up = rtl83xx_phylink_mac_link_up,
  956. .get_strings = rtl83xx_get_strings,
  957. .get_ethtool_stats = rtl83xx_get_ethtool_stats,
  958. .get_sset_count = rtl83xx_get_sset_count,
  959. .port_enable = rtl83xx_port_enable,
  960. .port_disable = rtl83xx_port_disable,
  961. .get_mac_eee = rtl83xx_get_mac_eee,
  962. .set_mac_eee = rtl83xx_set_mac_eee,
  963. .set_ageing_time = rtl83xx_set_l2aging,
  964. .port_bridge_join = rtl83xx_port_bridge_join,
  965. .port_bridge_leave = rtl83xx_port_bridge_leave,
  966. .port_stp_state_set = rtl83xx_port_stp_state_set,
  967. .port_fast_age = rtl83xx_fast_age,
  968. .port_vlan_filtering = rtl83xx_vlan_filtering,
  969. .port_vlan_prepare = rtl83xx_vlan_prepare,
  970. .port_vlan_add = rtl83xx_vlan_add,
  971. .port_vlan_del = rtl83xx_vlan_del,
  972. .port_fdb_add = rtl83xx_port_fdb_add,
  973. .port_fdb_del = rtl83xx_port_fdb_del,
  974. .port_fdb_dump = rtl83xx_port_fdb_dump,
  975. .port_mirror_add = rtl83xx_port_mirror_add,
  976. .port_mirror_del = rtl83xx_port_mirror_del,
  977. };