lan966x_lag.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include <linux/if_bridge.h>
  3. #include "lan966x_main.h"
  4. static void lan966x_lag_set_aggr_pgids(struct lan966x *lan966x)
  5. {
  6. u32 visited = GENMASK(lan966x->num_phys_ports - 1, 0);
  7. int p, lag, i;
  8. /* Reset destination and aggregation PGIDS */
  9. for (p = 0; p < lan966x->num_phys_ports; ++p)
  10. lan_wr(ANA_PGID_PGID_SET(BIT(p)),
  11. lan966x, ANA_PGID(p));
  12. for (p = PGID_AGGR; p < PGID_SRC; ++p)
  13. lan_wr(ANA_PGID_PGID_SET(visited),
  14. lan966x, ANA_PGID(p));
  15. /* The visited ports bitmask holds the list of ports offloading any
  16. * bonding interface. Initially we mark all these ports as unvisited,
  17. * then every time we visit a port in this bitmask, we know that it is
  18. * the lowest numbered port, i.e. the one whose logical ID == physical
  19. * port ID == LAG ID. So we mark as visited all further ports in the
  20. * bitmask that are offloading the same bonding interface. This way,
  21. * we set up the aggregation PGIDs only once per bonding interface.
  22. */
  23. for (p = 0; p < lan966x->num_phys_ports; ++p) {
  24. struct lan966x_port *port = lan966x->ports[p];
  25. if (!port || !port->bond)
  26. continue;
  27. visited &= ~BIT(p);
  28. }
  29. /* Now, set PGIDs for each active LAG */
  30. for (lag = 0; lag < lan966x->num_phys_ports; ++lag) {
  31. struct lan966x_port *port = lan966x->ports[lag];
  32. int num_active_ports = 0;
  33. struct net_device *bond;
  34. unsigned long bond_mask;
  35. u8 aggr_idx[16];
  36. if (!port || !port->bond || (visited & BIT(lag)))
  37. continue;
  38. bond = port->bond;
  39. bond_mask = lan966x_lag_get_mask(lan966x, bond);
  40. for_each_set_bit(p, &bond_mask, lan966x->num_phys_ports) {
  41. struct lan966x_port *port = lan966x->ports[p];
  42. if (!port)
  43. continue;
  44. lan_wr(ANA_PGID_PGID_SET(bond_mask),
  45. lan966x, ANA_PGID(p));
  46. if (port->lag_tx_active)
  47. aggr_idx[num_active_ports++] = p;
  48. }
  49. for (i = PGID_AGGR; i < PGID_SRC; ++i) {
  50. u32 ac;
  51. ac = lan_rd(lan966x, ANA_PGID(i));
  52. ac &= ~bond_mask;
  53. /* Don't do division by zero if there was no active
  54. * port. Just make all aggregation codes zero.
  55. */
  56. if (num_active_ports)
  57. ac |= BIT(aggr_idx[i % num_active_ports]);
  58. lan_wr(ANA_PGID_PGID_SET(ac),
  59. lan966x, ANA_PGID(i));
  60. }
  61. /* Mark all ports in the same LAG as visited to avoid applying
  62. * the same config again.
  63. */
  64. for (p = lag; p < lan966x->num_phys_ports; p++) {
  65. struct lan966x_port *port = lan966x->ports[p];
  66. if (!port)
  67. continue;
  68. if (port->bond == bond)
  69. visited |= BIT(p);
  70. }
  71. }
  72. }
  73. static void lan966x_lag_set_port_ids(struct lan966x *lan966x)
  74. {
  75. struct lan966x_port *port;
  76. u32 bond_mask;
  77. u32 lag_id;
  78. int p;
  79. for (p = 0; p < lan966x->num_phys_ports; ++p) {
  80. port = lan966x->ports[p];
  81. if (!port)
  82. continue;
  83. lag_id = port->chip_port;
  84. bond_mask = lan966x_lag_get_mask(lan966x, port->bond);
  85. if (bond_mask)
  86. lag_id = __ffs(bond_mask);
  87. lan_rmw(ANA_PORT_CFG_PORTID_VAL_SET(lag_id),
  88. ANA_PORT_CFG_PORTID_VAL,
  89. lan966x, ANA_PORT_CFG(port->chip_port));
  90. }
  91. }
  92. static void lan966x_lag_update_ids(struct lan966x *lan966x)
  93. {
  94. lan966x_lag_set_port_ids(lan966x);
  95. lan966x_update_fwd_mask(lan966x);
  96. lan966x_lag_set_aggr_pgids(lan966x);
  97. }
  98. int lan966x_lag_port_join(struct lan966x_port *port,
  99. struct net_device *brport_dev,
  100. struct net_device *bond,
  101. struct netlink_ext_ack *extack)
  102. {
  103. struct lan966x *lan966x = port->lan966x;
  104. struct net_device *dev = port->dev;
  105. u32 lag_id = -1;
  106. u32 bond_mask;
  107. int err;
  108. bond_mask = lan966x_lag_get_mask(lan966x, bond);
  109. if (bond_mask)
  110. lag_id = __ffs(bond_mask);
  111. port->bond = bond;
  112. lan966x_lag_update_ids(lan966x);
  113. err = switchdev_bridge_port_offload(brport_dev, dev, port,
  114. &lan966x_switchdev_nb,
  115. &lan966x_switchdev_blocking_nb,
  116. false, extack);
  117. if (err)
  118. goto out;
  119. lan966x_port_stp_state_set(port, br_port_get_stp_state(brport_dev));
  120. if (lan966x_lag_first_port(port->bond, port->dev) &&
  121. lag_id != -1)
  122. lan966x_mac_lag_replace_port_entry(lan966x,
  123. lan966x->ports[lag_id],
  124. port);
  125. return 0;
  126. out:
  127. port->bond = NULL;
  128. lan966x_lag_update_ids(lan966x);
  129. return err;
  130. }
  131. void lan966x_lag_port_leave(struct lan966x_port *port, struct net_device *bond)
  132. {
  133. struct lan966x *lan966x = port->lan966x;
  134. u32 bond_mask;
  135. u32 lag_id;
  136. if (lan966x_lag_first_port(port->bond, port->dev)) {
  137. bond_mask = lan966x_lag_get_mask(lan966x, port->bond);
  138. bond_mask &= ~BIT(port->chip_port);
  139. if (bond_mask) {
  140. lag_id = __ffs(bond_mask);
  141. lan966x_mac_lag_replace_port_entry(lan966x, port,
  142. lan966x->ports[lag_id]);
  143. } else {
  144. lan966x_mac_lag_remove_port_entry(lan966x, port);
  145. }
  146. }
  147. port->bond = NULL;
  148. lan966x_lag_update_ids(lan966x);
  149. lan966x_port_stp_state_set(port, BR_STATE_FORWARDING);
  150. }
  151. static bool lan966x_lag_port_check_hash_types(struct lan966x *lan966x,
  152. enum netdev_lag_hash hash_type)
  153. {
  154. int p;
  155. for (p = 0; p < lan966x->num_phys_ports; ++p) {
  156. struct lan966x_port *port = lan966x->ports[p];
  157. if (!port || !port->bond)
  158. continue;
  159. if (port->hash_type != hash_type)
  160. return false;
  161. }
  162. return true;
  163. }
  164. int lan966x_lag_port_prechangeupper(struct net_device *dev,
  165. struct netdev_notifier_changeupper_info *info)
  166. {
  167. struct lan966x_port *port = netdev_priv(dev);
  168. struct lan966x *lan966x = port->lan966x;
  169. struct netdev_lag_upper_info *lui;
  170. struct netlink_ext_ack *extack;
  171. extack = netdev_notifier_info_to_extack(&info->info);
  172. lui = info->upper_info;
  173. if (!lui) {
  174. port->hash_type = NETDEV_LAG_HASH_NONE;
  175. return NOTIFY_DONE;
  176. }
  177. if (lui->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
  178. NL_SET_ERR_MSG_MOD(extack,
  179. "LAG device using unsupported Tx type");
  180. return -EINVAL;
  181. }
  182. if (!lan966x_lag_port_check_hash_types(lan966x, lui->hash_type)) {
  183. NL_SET_ERR_MSG_MOD(extack,
  184. "LAG devices can have only the same hash_type");
  185. return -EINVAL;
  186. }
  187. switch (lui->hash_type) {
  188. case NETDEV_LAG_HASH_L2:
  189. lan_wr(ANA_AGGR_CFG_AC_DMAC_ENA_SET(1) |
  190. ANA_AGGR_CFG_AC_SMAC_ENA_SET(1),
  191. lan966x, ANA_AGGR_CFG);
  192. break;
  193. case NETDEV_LAG_HASH_L34:
  194. lan_wr(ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA_SET(1) |
  195. ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA_SET(1) |
  196. ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA_SET(1),
  197. lan966x, ANA_AGGR_CFG);
  198. break;
  199. case NETDEV_LAG_HASH_L23:
  200. lan_wr(ANA_AGGR_CFG_AC_DMAC_ENA_SET(1) |
  201. ANA_AGGR_CFG_AC_SMAC_ENA_SET(1) |
  202. ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA_SET(1) |
  203. ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA_SET(1),
  204. lan966x, ANA_AGGR_CFG);
  205. break;
  206. default:
  207. NL_SET_ERR_MSG_MOD(extack,
  208. "LAG device using unsupported hash type");
  209. return -EINVAL;
  210. }
  211. port->hash_type = lui->hash_type;
  212. return NOTIFY_OK;
  213. }
  214. int lan966x_lag_port_changelowerstate(struct net_device *dev,
  215. struct netdev_notifier_changelowerstate_info *info)
  216. {
  217. struct netdev_lag_lower_state_info *lag = info->lower_state_info;
  218. struct lan966x_port *port = netdev_priv(dev);
  219. struct lan966x *lan966x = port->lan966x;
  220. bool is_active;
  221. if (!port->bond)
  222. return NOTIFY_DONE;
  223. is_active = lag->link_up && lag->tx_enabled;
  224. if (port->lag_tx_active == is_active)
  225. return NOTIFY_DONE;
  226. port->lag_tx_active = is_active;
  227. lan966x_lag_set_aggr_pgids(lan966x);
  228. return NOTIFY_OK;
  229. }
  230. int lan966x_lag_netdev_prechangeupper(struct net_device *dev,
  231. struct netdev_notifier_changeupper_info *info)
  232. {
  233. struct lan966x_port *port;
  234. struct net_device *lower;
  235. struct list_head *iter;
  236. int err;
  237. netdev_for_each_lower_dev(dev, lower, iter) {
  238. if (!lan966x_netdevice_check(lower))
  239. continue;
  240. port = netdev_priv(lower);
  241. if (port->bond != dev)
  242. continue;
  243. err = lan966x_port_prechangeupper(lower, dev, info);
  244. if (err)
  245. return err;
  246. }
  247. return NOTIFY_DONE;
  248. }
  249. int lan966x_lag_netdev_changeupper(struct net_device *dev,
  250. struct netdev_notifier_changeupper_info *info)
  251. {
  252. struct lan966x_port *port;
  253. struct net_device *lower;
  254. struct list_head *iter;
  255. int err;
  256. netdev_for_each_lower_dev(dev, lower, iter) {
  257. if (!lan966x_netdevice_check(lower))
  258. continue;
  259. port = netdev_priv(lower);
  260. if (port->bond != dev)
  261. continue;
  262. err = lan966x_port_changeupper(lower, dev, info);
  263. if (err)
  264. return err;
  265. }
  266. return NOTIFY_DONE;
  267. }
  268. bool lan966x_lag_first_port(struct net_device *lag, struct net_device *dev)
  269. {
  270. struct lan966x_port *port = netdev_priv(dev);
  271. struct lan966x *lan966x = port->lan966x;
  272. unsigned long bond_mask;
  273. if (port->bond != lag)
  274. return false;
  275. bond_mask = lan966x_lag_get_mask(lan966x, lag);
  276. if (bond_mask && port->chip_port == __ffs(bond_mask))
  277. return true;
  278. return false;
  279. }
  280. u32 lan966x_lag_get_mask(struct lan966x *lan966x, struct net_device *bond)
  281. {
  282. struct lan966x_port *port;
  283. u32 mask = 0;
  284. int p;
  285. if (!bond)
  286. return mask;
  287. for (p = 0; p < lan966x->num_phys_ports; p++) {
  288. port = lan966x->ports[p];
  289. if (!port)
  290. continue;
  291. if (port->bond == bond)
  292. mask |= BIT(p);
  293. }
  294. return mask;
  295. }