lan966x_vlan.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include "lan966x_main.h"
  3. #define VLANACCESS_CMD_IDLE 0
  4. #define VLANACCESS_CMD_READ 1
  5. #define VLANACCESS_CMD_WRITE 2
  6. #define VLANACCESS_CMD_INIT 3
  7. static int lan966x_vlan_get_status(struct lan966x *lan966x)
  8. {
  9. return lan_rd(lan966x, ANA_VLANACCESS);
  10. }
  11. static int lan966x_vlan_wait_for_completion(struct lan966x *lan966x)
  12. {
  13. u32 val;
  14. return readx_poll_timeout(lan966x_vlan_get_status,
  15. lan966x, val,
  16. (val & ANA_VLANACCESS_VLAN_TBL_CMD) ==
  17. VLANACCESS_CMD_IDLE,
  18. TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
  19. }
  20. static void lan966x_vlan_set_mask(struct lan966x *lan966x, u16 vid)
  21. {
  22. u16 mask = lan966x->vlan_mask[vid];
  23. bool cpu_dis;
  24. cpu_dis = !(mask & BIT(CPU_PORT));
  25. /* Set flags and the VID to configure */
  26. lan_rmw(ANA_VLANTIDX_VLAN_PGID_CPU_DIS_SET(cpu_dis) |
  27. ANA_VLANTIDX_V_INDEX_SET(vid),
  28. ANA_VLANTIDX_VLAN_PGID_CPU_DIS |
  29. ANA_VLANTIDX_V_INDEX,
  30. lan966x, ANA_VLANTIDX);
  31. /* Set the vlan port members mask */
  32. lan_rmw(ANA_VLAN_PORT_MASK_VLAN_PORT_MASK_SET(mask),
  33. ANA_VLAN_PORT_MASK_VLAN_PORT_MASK,
  34. lan966x, ANA_VLAN_PORT_MASK);
  35. /* Issue a write command */
  36. lan_rmw(ANA_VLANACCESS_VLAN_TBL_CMD_SET(VLANACCESS_CMD_WRITE),
  37. ANA_VLANACCESS_VLAN_TBL_CMD,
  38. lan966x, ANA_VLANACCESS);
  39. if (lan966x_vlan_wait_for_completion(lan966x))
  40. dev_err(lan966x->dev, "Vlan set mask failed\n");
  41. }
  42. static void lan966x_vlan_port_add_vlan_mask(struct lan966x_port *port, u16 vid)
  43. {
  44. struct lan966x *lan966x = port->lan966x;
  45. u8 p = port->chip_port;
  46. lan966x->vlan_mask[vid] |= BIT(p);
  47. lan966x_vlan_set_mask(lan966x, vid);
  48. }
  49. static void lan966x_vlan_port_del_vlan_mask(struct lan966x_port *port, u16 vid)
  50. {
  51. struct lan966x *lan966x = port->lan966x;
  52. u8 p = port->chip_port;
  53. lan966x->vlan_mask[vid] &= ~BIT(p);
  54. lan966x_vlan_set_mask(lan966x, vid);
  55. }
  56. static bool lan966x_vlan_port_any_vlan_mask(struct lan966x *lan966x, u16 vid)
  57. {
  58. return !!(lan966x->vlan_mask[vid] & ~BIT(CPU_PORT));
  59. }
  60. static void lan966x_vlan_cpu_add_vlan_mask(struct lan966x *lan966x, u16 vid)
  61. {
  62. lan966x->vlan_mask[vid] |= BIT(CPU_PORT);
  63. lan966x_vlan_set_mask(lan966x, vid);
  64. }
  65. static void lan966x_vlan_cpu_del_vlan_mask(struct lan966x *lan966x, u16 vid)
  66. {
  67. lan966x->vlan_mask[vid] &= ~BIT(CPU_PORT);
  68. lan966x_vlan_set_mask(lan966x, vid);
  69. }
  70. static void lan966x_vlan_cpu_add_cpu_vlan_mask(struct lan966x *lan966x, u16 vid)
  71. {
  72. __set_bit(vid, lan966x->cpu_vlan_mask);
  73. }
  74. static void lan966x_vlan_cpu_del_cpu_vlan_mask(struct lan966x *lan966x, u16 vid)
  75. {
  76. __clear_bit(vid, lan966x->cpu_vlan_mask);
  77. }
  78. bool lan966x_vlan_cpu_member_cpu_vlan_mask(struct lan966x *lan966x, u16 vid)
  79. {
  80. return test_bit(vid, lan966x->cpu_vlan_mask);
  81. }
  82. static u16 lan966x_vlan_port_get_pvid(struct lan966x_port *port)
  83. {
  84. struct lan966x *lan966x = port->lan966x;
  85. if (!(lan966x->bridge_mask & BIT(port->chip_port)))
  86. return HOST_PVID;
  87. return port->vlan_aware ? port->pvid : UNAWARE_PVID;
  88. }
  89. int lan966x_vlan_port_set_vid(struct lan966x_port *port, u16 vid,
  90. bool pvid, bool untagged)
  91. {
  92. struct lan966x *lan966x = port->lan966x;
  93. /* Egress vlan classification */
  94. if (untagged && port->vid != vid) {
  95. if (port->vid) {
  96. dev_err(lan966x->dev,
  97. "Port already has a native VLAN: %d\n",
  98. port->vid);
  99. return -EBUSY;
  100. }
  101. port->vid = vid;
  102. }
  103. /* Default ingress vlan classification */
  104. if (pvid)
  105. port->pvid = vid;
  106. return 0;
  107. }
  108. static void lan966x_vlan_port_remove_vid(struct lan966x_port *port, u16 vid)
  109. {
  110. if (port->pvid == vid)
  111. port->pvid = 0;
  112. if (port->vid == vid)
  113. port->vid = 0;
  114. }
  115. void lan966x_vlan_port_set_vlan_aware(struct lan966x_port *port,
  116. bool vlan_aware)
  117. {
  118. port->vlan_aware = vlan_aware;
  119. }
  120. /* When the interface is in host mode, the interface should not be vlan aware
  121. * but it should insert all the tags that it gets from the network stack.
  122. * The tags are not in the data of the frame but actually in the skb and the ifh
  123. * is configured already to get this tag. So what we need to do is to update the
  124. * rewriter to insert the vlan tag for all frames which have a vlan tag
  125. * different than 0.
  126. */
  127. void lan966x_vlan_port_rew_host(struct lan966x_port *port)
  128. {
  129. struct lan966x *lan966x = port->lan966x;
  130. u32 val;
  131. /* Tag all frames except when VID=0*/
  132. val = REW_TAG_CFG_TAG_CFG_SET(2);
  133. /* Update only some bits in the register */
  134. lan_rmw(val,
  135. REW_TAG_CFG_TAG_CFG,
  136. lan966x, REW_TAG_CFG(port->chip_port));
  137. }
  138. void lan966x_vlan_port_apply(struct lan966x_port *port)
  139. {
  140. struct lan966x *lan966x = port->lan966x;
  141. u16 pvid;
  142. u32 val;
  143. pvid = lan966x_vlan_port_get_pvid(port);
  144. /* Ingress classification (ANA_PORT_VLAN_CFG) */
  145. /* Default vlan to classify for untagged frames (may be zero) */
  146. val = ANA_VLAN_CFG_VLAN_VID_SET(pvid);
  147. if (port->vlan_aware)
  148. val |= ANA_VLAN_CFG_VLAN_AWARE_ENA_SET(1) |
  149. ANA_VLAN_CFG_VLAN_POP_CNT_SET(1);
  150. lan_rmw(val,
  151. ANA_VLAN_CFG_VLAN_VID | ANA_VLAN_CFG_VLAN_AWARE_ENA |
  152. ANA_VLAN_CFG_VLAN_POP_CNT,
  153. lan966x, ANA_VLAN_CFG(port->chip_port));
  154. lan_rmw(DEV_MAC_TAGS_CFG_VLAN_AWR_ENA_SET(port->vlan_aware) |
  155. DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA_SET(port->vlan_aware),
  156. DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
  157. DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA,
  158. lan966x, DEV_MAC_TAGS_CFG(port->chip_port));
  159. /* Drop frames with multicast source address */
  160. val = ANA_DROP_CFG_DROP_MC_SMAC_ENA_SET(1);
  161. if (port->vlan_aware && !pvid)
  162. /* If port is vlan-aware and tagged, drop untagged and priority
  163. * tagged frames.
  164. */
  165. val |= ANA_DROP_CFG_DROP_UNTAGGED_ENA_SET(1) |
  166. ANA_DROP_CFG_DROP_PRIO_S_TAGGED_ENA_SET(1) |
  167. ANA_DROP_CFG_DROP_PRIO_C_TAGGED_ENA_SET(1);
  168. lan_wr(val, lan966x, ANA_DROP_CFG(port->chip_port));
  169. /* Egress configuration (REW_TAG_CFG): VLAN tag type to 8021Q */
  170. val = REW_TAG_CFG_TAG_TPID_CFG_SET(0);
  171. if (port->vlan_aware) {
  172. if (port->vid)
  173. /* Tag all frames except when VID == DEFAULT_VLAN */
  174. val |= REW_TAG_CFG_TAG_CFG_SET(1);
  175. else
  176. val |= REW_TAG_CFG_TAG_CFG_SET(3);
  177. }
  178. /* Update only some bits in the register */
  179. lan_rmw(val,
  180. REW_TAG_CFG_TAG_TPID_CFG | REW_TAG_CFG_TAG_CFG,
  181. lan966x, REW_TAG_CFG(port->chip_port));
  182. /* Set default VLAN and tag type to 8021Q */
  183. lan_rmw(REW_PORT_VLAN_CFG_PORT_TPID_SET(ETH_P_8021Q) |
  184. REW_PORT_VLAN_CFG_PORT_VID_SET(port->vid),
  185. REW_PORT_VLAN_CFG_PORT_TPID |
  186. REW_PORT_VLAN_CFG_PORT_VID,
  187. lan966x, REW_PORT_VLAN_CFG(port->chip_port));
  188. }
  189. void lan966x_vlan_port_add_vlan(struct lan966x_port *port,
  190. u16 vid,
  191. bool pvid,
  192. bool untagged)
  193. {
  194. struct lan966x *lan966x = port->lan966x;
  195. /* If the CPU(br) is already part of the vlan then add the fdb
  196. * entries in MAC table to copy the frames to the CPU(br).
  197. * If the CPU(br) is not part of the vlan then it would
  198. * just drop the frames.
  199. */
  200. if (lan966x_vlan_cpu_member_cpu_vlan_mask(lan966x, vid)) {
  201. lan966x_vlan_cpu_add_vlan_mask(lan966x, vid);
  202. lan966x_fdb_write_entries(lan966x, vid);
  203. lan966x_mdb_write_entries(lan966x, vid);
  204. }
  205. lan966x_vlan_port_set_vid(port, vid, pvid, untagged);
  206. lan966x_vlan_port_add_vlan_mask(port, vid);
  207. lan966x_vlan_port_apply(port);
  208. }
  209. void lan966x_vlan_port_del_vlan(struct lan966x_port *port, u16 vid)
  210. {
  211. struct lan966x *lan966x = port->lan966x;
  212. lan966x_vlan_port_remove_vid(port, vid);
  213. lan966x_vlan_port_del_vlan_mask(port, vid);
  214. lan966x_vlan_port_apply(port);
  215. /* In case there are no other ports in vlan then remove the CPU from
  216. * that vlan but still keep it in the mask because it may be needed
  217. * again then another port gets added in that vlan
  218. */
  219. if (!lan966x_vlan_port_any_vlan_mask(lan966x, vid)) {
  220. lan966x_vlan_cpu_del_vlan_mask(lan966x, vid);
  221. lan966x_fdb_erase_entries(lan966x, vid);
  222. lan966x_mdb_erase_entries(lan966x, vid);
  223. }
  224. }
  225. void lan966x_vlan_cpu_add_vlan(struct lan966x *lan966x, u16 vid)
  226. {
  227. /* Add an entry in the MAC table for the CPU
  228. * Add the CPU part of the vlan only if there is another port in that
  229. * vlan otherwise all the broadcast frames in that vlan will go to CPU
  230. * even if none of the ports are in the vlan and then the CPU will just
  231. * need to discard these frames. It is required to store this
  232. * information so when a front port is added then it would add also the
  233. * CPU port.
  234. */
  235. if (lan966x_vlan_port_any_vlan_mask(lan966x, vid)) {
  236. lan966x_vlan_cpu_add_vlan_mask(lan966x, vid);
  237. lan966x_mdb_write_entries(lan966x, vid);
  238. }
  239. lan966x_vlan_cpu_add_cpu_vlan_mask(lan966x, vid);
  240. lan966x_fdb_write_entries(lan966x, vid);
  241. }
  242. void lan966x_vlan_cpu_del_vlan(struct lan966x *lan966x, u16 vid)
  243. {
  244. /* Remove the CPU part of the vlan */
  245. lan966x_vlan_cpu_del_cpu_vlan_mask(lan966x, vid);
  246. lan966x_vlan_cpu_del_vlan_mask(lan966x, vid);
  247. lan966x_fdb_erase_entries(lan966x, vid);
  248. lan966x_mdb_erase_entries(lan966x, vid);
  249. }
  250. void lan966x_vlan_init(struct lan966x *lan966x)
  251. {
  252. u16 port, vid;
  253. /* Clear VLAN table, by default all ports are members of all VLANS */
  254. lan_rmw(ANA_VLANACCESS_VLAN_TBL_CMD_SET(VLANACCESS_CMD_INIT),
  255. ANA_VLANACCESS_VLAN_TBL_CMD,
  256. lan966x, ANA_VLANACCESS);
  257. lan966x_vlan_wait_for_completion(lan966x);
  258. for (vid = 1; vid < VLAN_N_VID; vid++) {
  259. lan966x->vlan_mask[vid] = 0;
  260. lan966x_vlan_set_mask(lan966x, vid);
  261. }
  262. /* Set all the ports + cpu to be part of HOST_PVID and UNAWARE_PVID */
  263. lan966x->vlan_mask[HOST_PVID] =
  264. GENMASK(lan966x->num_phys_ports - 1, 0) | BIT(CPU_PORT);
  265. lan966x_vlan_set_mask(lan966x, HOST_PVID);
  266. lan966x->vlan_mask[UNAWARE_PVID] =
  267. GENMASK(lan966x->num_phys_ports - 1, 0) | BIT(CPU_PORT);
  268. lan966x_vlan_set_mask(lan966x, UNAWARE_PVID);
  269. lan966x_vlan_cpu_add_cpu_vlan_mask(lan966x, UNAWARE_PVID);
  270. /* Configure the CPU port to be vlan aware */
  271. lan_wr(ANA_VLAN_CFG_VLAN_VID_SET(0) |
  272. ANA_VLAN_CFG_VLAN_AWARE_ENA_SET(1) |
  273. ANA_VLAN_CFG_VLAN_POP_CNT_SET(1),
  274. lan966x, ANA_VLAN_CFG(CPU_PORT));
  275. /* Set vlan ingress filter mask to all ports */
  276. lan_wr(GENMASK(lan966x->num_phys_ports, 0),
  277. lan966x, ANA_VLANMASK);
  278. for (port = 0; port < lan966x->num_phys_ports; port++) {
  279. lan_wr(0, lan966x, REW_PORT_VLAN_CFG(port));
  280. lan_wr(0, lan966x, REW_TAG_CFG(port));
  281. }
  282. }