1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Handling of a single switch chip, part of a switch fabric 4 * 5 * Copyright (c) 2017 Savoir-faire Linux Inc. 6 * Vivien Didelot <vivien.didelot@savoirfairelinux.com> 7 */ 8 9 #include <linux/if_bridge.h> 10 #include <linux/netdevice.h> 11 #include <linux/notifier.h> 12 #include <linux/if_vlan.h> 13 #include <net/switchdev.h> 14 15 #include "dsa_priv.h" 16 17 static unsigned int dsa_switch_fastest_ageing_time(struct dsa_switch *ds, 18 unsigned int ageing_time) 19 { 20 int i; 21 22 for (i = 0; i < ds->num_ports; ++i) { 23 struct dsa_port *dp = dsa_to_port(ds, i); 24 25 if (dp->ageing_time && dp->ageing_time < ageing_time) 26 ageing_time = dp->ageing_time; 27 } 28 29 return ageing_time; 30 } 31 32 static int dsa_switch_ageing_time(struct dsa_switch *ds, 33 struct dsa_notifier_ageing_time_info *info) 34 { 35 unsigned int ageing_time = info->ageing_time; 36 37 if (ds->ageing_time_min && ageing_time < ds->ageing_time_min) 38 return -ERANGE; 39 40 if (ds->ageing_time_max && ageing_time > ds->ageing_time_max) 41 return -ERANGE; 42 43 /* Program the fastest ageing time in case of multiple bridges */ 44 ageing_time = dsa_switch_fastest_ageing_time(ds, ageing_time); 45 46 if (ds->ops->set_ageing_time) 47 return ds->ops->set_ageing_time(ds, ageing_time); 48 49 return 0; 50 } 51 52 static bool dsa_switch_mtu_match(struct dsa_switch *ds, int port, 53 struct dsa_notifier_mtu_info *info) 54 { 55 if (ds->index == info->sw_index) 56 return (port == info->port) || dsa_is_dsa_port(ds, port); 57 58 if (!info->propagate_upstream) 59 return false; 60 61 if (dsa_is_dsa_port(ds, port) || dsa_is_cpu_port(ds, port)) 62 return true; 63 64 return false; 65 } 66 67 static int dsa_switch_mtu(struct dsa_switch *ds, 68 struct dsa_notifier_mtu_info *info) 69 { 70 int port, ret; 71 72 if (!ds->ops->port_change_mtu) 73 return -EOPNOTSUPP; 74 75 for (port = 0; port < ds->num_ports; port++) { 76 if (dsa_switch_mtu_match(ds, port, info)) { 77 ret = ds->ops->port_change_mtu(ds, port, info->mtu); 78 if (ret) 79 return ret; 80 } 81 } 82 83 return 0; 84 } 85 86 static int dsa_switch_bridge_join(struct dsa_switch *ds, 87 struct dsa_notifier_bridge_info *info) 88 { 89 struct dsa_switch_tree *dst = ds->dst; 90 91 if (dst->index == info->tree_index && ds->index == info->sw_index && 92 ds->ops->port_bridge_join) 93 return ds->ops->port_bridge_join(ds, info->port, info->br); 94 95 if ((dst->index != info->tree_index || ds->index != info->sw_index) && 96 ds->ops->crosschip_bridge_join) 97 return ds->ops->crosschip_bridge_join(ds, info->tree_index, 98 info->sw_index, 99 info->port, info->br); 100 101 return 0; 102 } 103 104 static int dsa_switch_bridge_leave(struct dsa_switch *ds, 105 struct dsa_notifier_bridge_info *info) 106 { 107 bool unset_vlan_filtering = br_vlan_enabled(info->br); 108 struct dsa_switch_tree *dst = ds->dst; 109 int err, i; 110 111 if (dst->index == info->tree_index && ds->index == info->sw_index && 112 ds->ops->port_bridge_join) 113 ds->ops->port_bridge_leave(ds, info->port, info->br); 114 115 if ((dst->index != info->tree_index || ds->index != info->sw_index) && 116 ds->ops->crosschip_bridge_join) 117 ds->ops->crosschip_bridge_leave(ds, info->tree_index, 118 info->sw_index, info->port, 119 info->br); 120 121 /* If the bridge was vlan_filtering, the bridge core doesn't trigger an 122 * event for changing vlan_filtering setting upon slave ports leaving 123 * it. That is a good thing, because that lets us handle it and also 124 * handle the case where the switch's vlan_filtering setting is global 125 * (not per port). When that happens, the correct moment to trigger the 126 * vlan_filtering callback is only when the last port left this bridge. 127 */ 128 if (unset_vlan_filtering && ds->vlan_filtering_is_global) { 129 for (i = 0; i < ds->num_ports; i++) { 130 if (i == info->port) 131 continue; 132 if (dsa_to_port(ds, i)->bridge_dev == info->br) { 133 unset_vlan_filtering = false; 134 break; 135 } 136 } 137 } 138 if (unset_vlan_filtering) { 139 err = dsa_port_vlan_filtering(dsa_to_port(ds, info->port), 140 false); 141 if (err && err != EOPNOTSUPP) 142 return err; 143 } 144 return 0; 145 } 146 147 static int dsa_switch_fdb_add(struct dsa_switch *ds, 148 struct dsa_notifier_fdb_info *info) 149 { 150 int port = dsa_towards_port(ds, info->sw_index, info->port); 151 152 if (!ds->ops->port_fdb_add) 153 return -EOPNOTSUPP; 154 155 return ds->ops->port_fdb_add(ds, port, info->addr, info->vid); 156 } 157 158 static int dsa_switch_fdb_del(struct dsa_switch *ds, 159 struct dsa_notifier_fdb_info *info) 160 { 161 int port = dsa_towards_port(ds, info->sw_index, info->port); 162 163 if (!ds->ops->port_fdb_del) 164 return -EOPNOTSUPP; 165 166 return ds->ops->port_fdb_del(ds, port, info->addr, info->vid); 167 } 168 169 static int dsa_switch_lag_change(struct dsa_switch *ds, 170 struct dsa_notifier_lag_info *info) 171 { 172 if (ds->index == info->sw_index && ds->ops->port_lag_change) 173 return ds->ops->port_lag_change(ds, info->port); 174 175 if (ds->index != info->sw_index && ds->ops->crosschip_lag_change) 176 return ds->ops->crosschip_lag_change(ds, info->sw_index, 177 info->port); 178 179 return 0; 180 } 181 182 static int dsa_switch_lag_join(struct dsa_switch *ds, 183 struct dsa_notifier_lag_info *info) 184 { 185 if (ds->index == info->sw_index && ds->ops->port_lag_join) 186 return ds->ops->port_lag_join(ds, info->port, info->lag, 187 info->info); 188 189 if (ds->index != info->sw_index && ds->ops->crosschip_lag_join) 190 return ds->ops->crosschip_lag_join(ds, info->sw_index, 191 info->port, info->lag, 192 info->info); 193 194 return 0; 195 } 196 197 static int dsa_switch_lag_leave(struct dsa_switch *ds, 198 struct dsa_notifier_lag_info *info) 199 { 200 if (ds->index == info->sw_index && ds->ops->port_lag_leave) 201 return ds->ops->port_lag_leave(ds, info->port, info->lag); 202 203 if (ds->index != info->sw_index && ds->ops->crosschip_lag_leave) 204 return ds->ops->crosschip_lag_leave(ds, info->sw_index, 205 info->port, info->lag); 206 207 return 0; 208 } 209 210 static bool dsa_switch_mdb_match(struct dsa_switch *ds, int port, 211 struct dsa_notifier_mdb_info *info) 212 { 213 if (ds->index == info->sw_index && port == info->port) 214 return true; 215 216 if (dsa_is_dsa_port(ds, port)) 217 return true; 218 219 return false; 220 } 221 222 static int dsa_switch_mdb_add(struct dsa_switch *ds, 223 struct dsa_notifier_mdb_info *info) 224 { 225 int err = 0; 226 int port; 227 228 if (!ds->ops->port_mdb_add) 229 return -EOPNOTSUPP; 230 231 for (port = 0; port < ds->num_ports; port++) { 232 if (dsa_switch_mdb_match(ds, port, info)) { 233 err = ds->ops->port_mdb_add(ds, port, info->mdb); 234 if (err) 235 break; 236 } 237 } 238 239 return err; 240 } 241 242 static int dsa_switch_mdb_del(struct dsa_switch *ds, 243 struct dsa_notifier_mdb_info *info) 244 { 245 if (!ds->ops->port_mdb_del) 246 return -EOPNOTSUPP; 247 248 if (ds->index == info->sw_index) 249 return ds->ops->port_mdb_del(ds, info->port, info->mdb); 250 251 return 0; 252 } 253 254 static bool dsa_switch_vlan_match(struct dsa_switch *ds, int port, 255 struct dsa_notifier_vlan_info *info) 256 { 257 if (ds->index == info->sw_index && port == info->port) 258 return true; 259 260 if (dsa_is_dsa_port(ds, port)) 261 return true; 262 263 return false; 264 } 265 266 static int dsa_switch_vlan_add(struct dsa_switch *ds, 267 struct dsa_notifier_vlan_info *info) 268 { 269 int port, err; 270 271 if (!ds->ops->port_vlan_add) 272 return -EOPNOTSUPP; 273 274 for (port = 0; port < ds->num_ports; port++) { 275 if (dsa_switch_vlan_match(ds, port, info)) { 276 err = ds->ops->port_vlan_add(ds, port, info->vlan); 277 if (err) 278 return err; 279 } 280 } 281 282 return 0; 283 } 284 285 static int dsa_switch_vlan_del(struct dsa_switch *ds, 286 struct dsa_notifier_vlan_info *info) 287 { 288 if (!ds->ops->port_vlan_del) 289 return -EOPNOTSUPP; 290 291 if (ds->index == info->sw_index) 292 return ds->ops->port_vlan_del(ds, info->port, info->vlan); 293 294 /* Do not deprogram the DSA links as they may be used as conduit 295 * for other VLAN members in the fabric. 296 */ 297 return 0; 298 } 299 300 static bool dsa_switch_tag_proto_match(struct dsa_switch *ds, int port, 301 struct dsa_notifier_tag_proto_info *info) 302 { 303 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) 304 return true; 305 306 return false; 307 } 308 309 static int dsa_switch_change_tag_proto(struct dsa_switch *ds, 310 struct dsa_notifier_tag_proto_info *info) 311 { 312 const struct dsa_device_ops *tag_ops = info->tag_ops; 313 int port, err; 314 315 if (!ds->ops->change_tag_protocol) 316 return -EOPNOTSUPP; 317 318 ASSERT_RTNL(); 319 320 for (port = 0; port < ds->num_ports; port++) { 321 if (dsa_switch_tag_proto_match(ds, port, info)) { 322 err = ds->ops->change_tag_protocol(ds, port, 323 tag_ops->proto); 324 if (err) 325 return err; 326 327 if (dsa_is_cpu_port(ds, port)) 328 dsa_port_set_tag_protocol(dsa_to_port(ds, port), 329 tag_ops); 330 } 331 } 332 333 /* Now that changing the tag protocol can no longer fail, let's update 334 * the remaining bits which are "duplicated for faster access", and the 335 * bits that depend on the tagger, such as the MTU. 336 */ 337 for (port = 0; port < ds->num_ports; port++) { 338 if (dsa_is_user_port(ds, port)) { 339 struct net_device *slave; 340 341 slave = dsa_to_port(ds, port)->slave; 342 dsa_slave_setup_tagger(slave); 343 344 /* rtnl_mutex is held in dsa_tree_change_tag_proto */ 345 dsa_slave_change_mtu(slave, slave->mtu); 346 } 347 } 348 349 return 0; 350 } 351 352 static int dsa_switch_event(struct notifier_block *nb, 353 unsigned long event, void *info) 354 { 355 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb); 356 int err; 357 358 switch (event) { 359 case DSA_NOTIFIER_AGEING_TIME: 360 err = dsa_switch_ageing_time(ds, info); 361 break; 362 case DSA_NOTIFIER_BRIDGE_JOIN: 363 err = dsa_switch_bridge_join(ds, info); 364 break; 365 case DSA_NOTIFIER_BRIDGE_LEAVE: 366 err = dsa_switch_bridge_leave(ds, info); 367 break; 368 case DSA_NOTIFIER_FDB_ADD: 369 err = dsa_switch_fdb_add(ds, info); 370 break; 371 case DSA_NOTIFIER_FDB_DEL: 372 err = dsa_switch_fdb_del(ds, info); 373 break; 374 case DSA_NOTIFIER_LAG_CHANGE: 375 err = dsa_switch_lag_change(ds, info); 376 break; 377 case DSA_NOTIFIER_LAG_JOIN: 378 err = dsa_switch_lag_join(ds, info); 379 break; 380 case DSA_NOTIFIER_LAG_LEAVE: 381 err = dsa_switch_lag_leave(ds, info); 382 break; 383 case DSA_NOTIFIER_MDB_ADD: 384 err = dsa_switch_mdb_add(ds, info); 385 break; 386 case DSA_NOTIFIER_MDB_DEL: 387 err = dsa_switch_mdb_del(ds, info); 388 break; 389 case DSA_NOTIFIER_VLAN_ADD: 390 err = dsa_switch_vlan_add(ds, info); 391 break; 392 case DSA_NOTIFIER_VLAN_DEL: 393 err = dsa_switch_vlan_del(ds, info); 394 break; 395 case DSA_NOTIFIER_MTU: 396 err = dsa_switch_mtu(ds, info); 397 break; 398 case DSA_NOTIFIER_TAG_PROTO: 399 err = dsa_switch_change_tag_proto(ds, info); 400 break; 401 default: 402 err = -EOPNOTSUPP; 403 break; 404 } 405 406 if (err) 407 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n", 408 event, err); 409 410 return notifier_from_errno(err); 411 } 412 413 int dsa_switch_register_notifier(struct dsa_switch *ds) 414 { 415 ds->nb.notifier_call = dsa_switch_event; 416 417 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb); 418 } 419 420 void dsa_switch_unregister_notifier(struct dsa_switch *ds) 421 { 422 int err; 423 424 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb); 425 if (err) 426 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err); 427 } 428