1 // SPDX-License-Identifier: GPL-2.0+ 2 3 #include <linux/if_bridge.h> 4 #include <net/switchdev.h> 5 6 #include "lan966x_main.h" 7 8 static struct notifier_block lan966x_netdevice_nb __read_mostly; 9 static struct notifier_block lan966x_switchdev_nb __read_mostly; 10 static struct notifier_block lan966x_switchdev_blocking_nb __read_mostly; 11 12 static void lan966x_update_fwd_mask(struct lan966x *lan966x) 13 { 14 int i; 15 16 for (i = 0; i < lan966x->num_phys_ports; i++) { 17 struct lan966x_port *port = lan966x->ports[i]; 18 unsigned long mask = 0; 19 20 if (port && lan966x->bridge_fwd_mask & BIT(i)) 21 mask = lan966x->bridge_fwd_mask & ~BIT(i); 22 23 mask |= BIT(CPU_PORT); 24 25 lan_wr(ANA_PGID_PGID_SET(mask), 26 lan966x, ANA_PGID(PGID_SRC + i)); 27 } 28 } 29 30 static void lan966x_port_stp_state_set(struct lan966x_port *port, u8 state) 31 { 32 struct lan966x *lan966x = port->lan966x; 33 bool learn_ena = false; 34 35 if (state == BR_STATE_FORWARDING || state == BR_STATE_LEARNING) 36 learn_ena = true; 37 38 if (state == BR_STATE_FORWARDING) 39 lan966x->bridge_fwd_mask |= BIT(port->chip_port); 40 else 41 lan966x->bridge_fwd_mask &= ~BIT(port->chip_port); 42 43 lan_rmw(ANA_PORT_CFG_LEARN_ENA_SET(learn_ena), 44 ANA_PORT_CFG_LEARN_ENA, 45 lan966x, ANA_PORT_CFG(port->chip_port)); 46 47 lan966x_update_fwd_mask(lan966x); 48 } 49 50 static void lan966x_port_ageing_set(struct lan966x_port *port, 51 unsigned long ageing_clock_t) 52 { 53 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t); 54 u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000; 55 56 lan966x_mac_set_ageing(port->lan966x, ageing_time); 57 } 58 59 static int lan966x_port_attr_set(struct net_device *dev, const void *ctx, 60 const struct switchdev_attr *attr, 61 struct netlink_ext_ack *extack) 62 { 63 struct lan966x_port *port = netdev_priv(dev); 64 int err = 0; 65 66 if (ctx && ctx != port) 67 return 0; 68 69 switch (attr->id) { 70 case SWITCHDEV_ATTR_ID_PORT_STP_STATE: 71 lan966x_port_stp_state_set(port, attr->u.stp_state); 72 break; 73 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME: 74 lan966x_port_ageing_set(port, attr->u.ageing_time); 75 break; 76 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING: 77 lan966x_vlan_port_set_vlan_aware(port, attr->u.vlan_filtering); 78 lan966x_vlan_port_apply(port); 79 break; 80 default: 81 err = -EOPNOTSUPP; 82 break; 83 } 84 85 return err; 86 } 87 88 static int lan966x_port_bridge_join(struct lan966x_port *port, 89 struct net_device *bridge, 90 struct netlink_ext_ack *extack) 91 { 92 struct lan966x *lan966x = port->lan966x; 93 struct net_device *dev = port->dev; 94 int err; 95 96 if (!lan966x->bridge_mask) { 97 lan966x->bridge = bridge; 98 } else { 99 if (lan966x->bridge != bridge) { 100 NL_SET_ERR_MSG_MOD(extack, "Not allow to add port to different bridge"); 101 return -ENODEV; 102 } 103 } 104 105 err = switchdev_bridge_port_offload(dev, dev, port, 106 &lan966x_switchdev_nb, 107 &lan966x_switchdev_blocking_nb, 108 false, extack); 109 if (err) 110 return err; 111 112 lan966x->bridge_mask |= BIT(port->chip_port); 113 114 return 0; 115 } 116 117 static void lan966x_port_bridge_leave(struct lan966x_port *port, 118 struct net_device *bridge) 119 { 120 struct lan966x *lan966x = port->lan966x; 121 122 lan966x->bridge_mask &= ~BIT(port->chip_port); 123 124 if (!lan966x->bridge_mask) 125 lan966x->bridge = NULL; 126 127 /* Set the port back to host mode */ 128 lan966x_vlan_port_set_vlan_aware(port, false); 129 lan966x_vlan_port_set_vid(port, HOST_PVID, false, false); 130 lan966x_vlan_port_apply(port); 131 } 132 133 static int lan966x_port_changeupper(struct net_device *dev, 134 struct netdev_notifier_changeupper_info *info) 135 { 136 struct lan966x_port *port = netdev_priv(dev); 137 struct netlink_ext_ack *extack; 138 int err = 0; 139 140 extack = netdev_notifier_info_to_extack(&info->info); 141 142 if (netif_is_bridge_master(info->upper_dev)) { 143 if (info->linking) 144 err = lan966x_port_bridge_join(port, info->upper_dev, 145 extack); 146 else 147 lan966x_port_bridge_leave(port, info->upper_dev); 148 } 149 150 return err; 151 } 152 153 static int lan966x_port_prechangeupper(struct net_device *dev, 154 struct netdev_notifier_changeupper_info *info) 155 { 156 struct lan966x_port *port = netdev_priv(dev); 157 158 if (netif_is_bridge_master(info->upper_dev) && !info->linking) 159 switchdev_bridge_port_unoffload(port->dev, port, 160 &lan966x_switchdev_nb, 161 &lan966x_switchdev_blocking_nb); 162 163 return NOTIFY_DONE; 164 } 165 166 static int lan966x_foreign_bridging_check(struct net_device *bridge, 167 struct netlink_ext_ack *extack) 168 { 169 struct lan966x *lan966x = NULL; 170 bool has_foreign = false; 171 struct net_device *dev; 172 struct list_head *iter; 173 174 if (!netif_is_bridge_master(bridge)) 175 return 0; 176 177 netdev_for_each_lower_dev(bridge, dev, iter) { 178 if (lan966x_netdevice_check(dev)) { 179 struct lan966x_port *port = netdev_priv(dev); 180 181 if (lan966x) { 182 /* Bridge already has at least one port of a 183 * lan966x switch inside it, check that it's 184 * the same instance of the driver. 185 */ 186 if (port->lan966x != lan966x) { 187 NL_SET_ERR_MSG_MOD(extack, 188 "Bridging between multiple lan966x switches disallowed"); 189 return -EINVAL; 190 } 191 } else { 192 /* This is the first lan966x port inside this 193 * bridge 194 */ 195 lan966x = port->lan966x; 196 } 197 } else { 198 has_foreign = true; 199 } 200 201 if (lan966x && has_foreign) { 202 NL_SET_ERR_MSG_MOD(extack, 203 "Bridging lan966x ports with foreign interfaces disallowed"); 204 return -EINVAL; 205 } 206 } 207 208 return 0; 209 } 210 211 static int lan966x_bridge_check(struct net_device *dev, 212 struct netdev_notifier_changeupper_info *info) 213 { 214 return lan966x_foreign_bridging_check(info->upper_dev, 215 info->info.extack); 216 } 217 218 static int lan966x_netdevice_port_event(struct net_device *dev, 219 struct notifier_block *nb, 220 unsigned long event, void *ptr) 221 { 222 int err = 0; 223 224 if (!lan966x_netdevice_check(dev)) { 225 if (event == NETDEV_CHANGEUPPER) 226 return lan966x_bridge_check(dev, ptr); 227 return 0; 228 } 229 230 switch (event) { 231 case NETDEV_PRECHANGEUPPER: 232 err = lan966x_port_prechangeupper(dev, ptr); 233 break; 234 case NETDEV_CHANGEUPPER: 235 err = lan966x_bridge_check(dev, ptr); 236 if (err) 237 return err; 238 239 err = lan966x_port_changeupper(dev, ptr); 240 break; 241 } 242 243 return err; 244 } 245 246 static int lan966x_netdevice_event(struct notifier_block *nb, 247 unsigned long event, void *ptr) 248 { 249 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 250 int ret; 251 252 ret = lan966x_netdevice_port_event(dev, nb, event, ptr); 253 254 return notifier_from_errno(ret); 255 } 256 257 static int lan966x_switchdev_event(struct notifier_block *nb, 258 unsigned long event, void *ptr) 259 { 260 struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 261 int err; 262 263 switch (event) { 264 case SWITCHDEV_PORT_ATTR_SET: 265 err = switchdev_handle_port_attr_set(dev, ptr, 266 lan966x_netdevice_check, 267 lan966x_port_attr_set); 268 return notifier_from_errno(err); 269 } 270 271 return NOTIFY_DONE; 272 } 273 274 static int lan966x_handle_port_vlan_add(struct lan966x_port *port, 275 const struct switchdev_obj *obj) 276 { 277 const struct switchdev_obj_port_vlan *v = SWITCHDEV_OBJ_PORT_VLAN(obj); 278 struct lan966x *lan966x = port->lan966x; 279 280 /* When adding a port to a vlan, we get a callback for the port but 281 * also for the bridge. When get the callback for the bridge just bail 282 * out. Then when the bridge is added to the vlan, then we get a 283 * callback here but in this case the flags has set: 284 * BRIDGE_VLAN_INFO_BRENTRY. In this case it means that the CPU 285 * port is added to the vlan, so the broadcast frames and unicast frames 286 * with dmac of the bridge should be foward to CPU. 287 */ 288 if (netif_is_bridge_master(obj->orig_dev) && 289 !(v->flags & BRIDGE_VLAN_INFO_BRENTRY)) 290 return 0; 291 292 if (!netif_is_bridge_master(obj->orig_dev)) 293 lan966x_vlan_port_add_vlan(port, v->vid, 294 v->flags & BRIDGE_VLAN_INFO_PVID, 295 v->flags & BRIDGE_VLAN_INFO_UNTAGGED); 296 else 297 lan966x_vlan_cpu_add_vlan(lan966x, v->vid); 298 299 return 0; 300 } 301 302 static int lan966x_handle_port_obj_add(struct net_device *dev, const void *ctx, 303 const struct switchdev_obj *obj, 304 struct netlink_ext_ack *extack) 305 { 306 struct lan966x_port *port = netdev_priv(dev); 307 int err; 308 309 if (ctx && ctx != port) 310 return 0; 311 312 switch (obj->id) { 313 case SWITCHDEV_OBJ_ID_PORT_VLAN: 314 err = lan966x_handle_port_vlan_add(port, obj); 315 break; 316 default: 317 err = -EOPNOTSUPP; 318 break; 319 } 320 321 return err; 322 } 323 324 static int lan966x_handle_port_vlan_del(struct lan966x_port *port, 325 const struct switchdev_obj *obj) 326 { 327 const struct switchdev_obj_port_vlan *v = SWITCHDEV_OBJ_PORT_VLAN(obj); 328 struct lan966x *lan966x = port->lan966x; 329 330 if (!netif_is_bridge_master(obj->orig_dev)) 331 lan966x_vlan_port_del_vlan(port, v->vid); 332 else 333 lan966x_vlan_cpu_del_vlan(lan966x, v->vid); 334 335 return 0; 336 } 337 338 static int lan966x_handle_port_obj_del(struct net_device *dev, const void *ctx, 339 const struct switchdev_obj *obj) 340 { 341 struct lan966x_port *port = netdev_priv(dev); 342 int err; 343 344 if (ctx && ctx != port) 345 return 0; 346 347 switch (obj->id) { 348 case SWITCHDEV_OBJ_ID_PORT_VLAN: 349 err = lan966x_handle_port_vlan_del(port, obj); 350 break; 351 default: 352 err = -EOPNOTSUPP; 353 break; 354 } 355 356 return err; 357 } 358 359 static int lan966x_switchdev_blocking_event(struct notifier_block *nb, 360 unsigned long event, 361 void *ptr) 362 { 363 struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 364 int err; 365 366 switch (event) { 367 case SWITCHDEV_PORT_OBJ_ADD: 368 err = switchdev_handle_port_obj_add(dev, ptr, 369 lan966x_netdevice_check, 370 lan966x_handle_port_obj_add); 371 return notifier_from_errno(err); 372 case SWITCHDEV_PORT_OBJ_DEL: 373 err = switchdev_handle_port_obj_del(dev, ptr, 374 lan966x_netdevice_check, 375 lan966x_handle_port_obj_del); 376 return notifier_from_errno(err); 377 case SWITCHDEV_PORT_ATTR_SET: 378 err = switchdev_handle_port_attr_set(dev, ptr, 379 lan966x_netdevice_check, 380 lan966x_port_attr_set); 381 return notifier_from_errno(err); 382 } 383 384 return NOTIFY_DONE; 385 } 386 387 static struct notifier_block lan966x_netdevice_nb __read_mostly = { 388 .notifier_call = lan966x_netdevice_event, 389 }; 390 391 static struct notifier_block lan966x_switchdev_nb __read_mostly = { 392 .notifier_call = lan966x_switchdev_event, 393 }; 394 395 static struct notifier_block lan966x_switchdev_blocking_nb __read_mostly = { 396 .notifier_call = lan966x_switchdev_blocking_event, 397 }; 398 399 void lan966x_register_notifier_blocks(void) 400 { 401 register_netdevice_notifier(&lan966x_netdevice_nb); 402 register_switchdev_notifier(&lan966x_switchdev_nb); 403 register_switchdev_blocking_notifier(&lan966x_switchdev_blocking_nb); 404 } 405 406 void lan966x_unregister_notifier_blocks(void) 407 { 408 unregister_switchdev_blocking_notifier(&lan966x_switchdev_blocking_nb); 409 unregister_switchdev_notifier(&lan966x_switchdev_nb); 410 unregister_netdevice_notifier(&lan966x_netdevice_nb); 411 } 412