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