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 int dsa_switch_event(struct notifier_block *nb, 301 unsigned long event, void *info) 302 { 303 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb); 304 int err; 305 306 switch (event) { 307 case DSA_NOTIFIER_AGEING_TIME: 308 err = dsa_switch_ageing_time(ds, info); 309 break; 310 case DSA_NOTIFIER_BRIDGE_JOIN: 311 err = dsa_switch_bridge_join(ds, info); 312 break; 313 case DSA_NOTIFIER_BRIDGE_LEAVE: 314 err = dsa_switch_bridge_leave(ds, info); 315 break; 316 case DSA_NOTIFIER_FDB_ADD: 317 err = dsa_switch_fdb_add(ds, info); 318 break; 319 case DSA_NOTIFIER_FDB_DEL: 320 err = dsa_switch_fdb_del(ds, info); 321 break; 322 case DSA_NOTIFIER_LAG_CHANGE: 323 err = dsa_switch_lag_change(ds, info); 324 break; 325 case DSA_NOTIFIER_LAG_JOIN: 326 err = dsa_switch_lag_join(ds, info); 327 break; 328 case DSA_NOTIFIER_LAG_LEAVE: 329 err = dsa_switch_lag_leave(ds, info); 330 break; 331 case DSA_NOTIFIER_MDB_ADD: 332 err = dsa_switch_mdb_add(ds, info); 333 break; 334 case DSA_NOTIFIER_MDB_DEL: 335 err = dsa_switch_mdb_del(ds, info); 336 break; 337 case DSA_NOTIFIER_VLAN_ADD: 338 err = dsa_switch_vlan_add(ds, info); 339 break; 340 case DSA_NOTIFIER_VLAN_DEL: 341 err = dsa_switch_vlan_del(ds, info); 342 break; 343 case DSA_NOTIFIER_MTU: 344 err = dsa_switch_mtu(ds, info); 345 break; 346 default: 347 err = -EOPNOTSUPP; 348 break; 349 } 350 351 if (err) 352 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n", 353 event, err); 354 355 return notifier_from_errno(err); 356 } 357 358 int dsa_switch_register_notifier(struct dsa_switch *ds) 359 { 360 ds->nb.notifier_call = dsa_switch_event; 361 362 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb); 363 } 364 365 void dsa_switch_unregister_notifier(struct dsa_switch *ds) 366 { 367 int err; 368 369 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb); 370 if (err) 371 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err); 372 } 373