Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Handling of a single switch chip, part of a switch fabric |
| 4 | * |
Vivien Didelot | 4333d61 | 2017-03-28 15:10:36 -0400 | [diff] [blame] | 5 | * Copyright (c) 2017 Savoir-faire Linux Inc. |
| 6 | * Vivien Didelot <vivien.didelot@savoirfairelinux.com> |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
Vladimir Oltean | d371b7c | 2019-04-28 21:45:46 +0300 | [diff] [blame] | 9 | #include <linux/if_bridge.h> |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 10 | #include <linux/netdevice.h> |
| 11 | #include <linux/notifier.h> |
Florian Fainelli | 061f6a5 | 2019-02-20 14:35:39 -0800 | [diff] [blame] | 12 | #include <linux/if_vlan.h> |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 13 | #include <net/switchdev.h> |
Vivien Didelot | ea5dd34 | 2017-05-17 15:46:03 -0400 | [diff] [blame] | 14 | |
| 15 | #include "dsa_priv.h" |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 16 | |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 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) { |
Vivien Didelot | 68bb8ea | 2019-10-21 16:51:15 -0400 | [diff] [blame] | 23 | struct dsa_port *dp = dsa_to_port(ds, i); |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 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 | struct switchdev_trans *trans = info->trans; |
| 37 | |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 38 | if (switchdev_trans_ph_prepare(trans)) { |
| 39 | if (ds->ageing_time_min && ageing_time < ds->ageing_time_min) |
| 40 | return -ERANGE; |
| 41 | if (ds->ageing_time_max && ageing_time > ds->ageing_time_max) |
| 42 | return -ERANGE; |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | /* Program the fastest ageing time in case of multiple bridges */ |
| 47 | ageing_time = dsa_switch_fastest_ageing_time(ds, ageing_time); |
| 48 | |
| 49 | if (ds->ops->set_ageing_time) |
| 50 | return ds->ops->set_ageing_time(ds, ageing_time); |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 55 | static int dsa_switch_bridge_join(struct dsa_switch *ds, |
| 56 | struct dsa_notifier_bridge_info *info) |
| 57 | { |
| 58 | if (ds->index == info->sw_index && ds->ops->port_bridge_join) |
| 59 | return ds->ops->port_bridge_join(ds, info->port, info->br); |
| 60 | |
Vivien Didelot | 40ef2c9 | 2017-03-30 17:37:14 -0400 | [diff] [blame] | 61 | if (ds->index != info->sw_index && ds->ops->crosschip_bridge_join) |
| 62 | return ds->ops->crosschip_bridge_join(ds, info->sw_index, |
| 63 | info->port, info->br); |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | static int dsa_switch_bridge_leave(struct dsa_switch *ds, |
| 69 | struct dsa_notifier_bridge_info *info) |
| 70 | { |
Vladimir Oltean | d371b7c | 2019-04-28 21:45:46 +0300 | [diff] [blame] | 71 | bool unset_vlan_filtering = br_vlan_enabled(info->br); |
| 72 | int err, i; |
| 73 | |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 74 | if (ds->index == info->sw_index && ds->ops->port_bridge_leave) |
| 75 | ds->ops->port_bridge_leave(ds, info->port, info->br); |
| 76 | |
Vivien Didelot | 40ef2c9 | 2017-03-30 17:37:14 -0400 | [diff] [blame] | 77 | if (ds->index != info->sw_index && ds->ops->crosschip_bridge_leave) |
| 78 | ds->ops->crosschip_bridge_leave(ds, info->sw_index, info->port, |
| 79 | info->br); |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 80 | |
Vladimir Oltean | d371b7c | 2019-04-28 21:45:46 +0300 | [diff] [blame] | 81 | /* If the bridge was vlan_filtering, the bridge core doesn't trigger an |
| 82 | * event for changing vlan_filtering setting upon slave ports leaving |
| 83 | * it. That is a good thing, because that lets us handle it and also |
| 84 | * handle the case where the switch's vlan_filtering setting is global |
| 85 | * (not per port). When that happens, the correct moment to trigger the |
| 86 | * vlan_filtering callback is only when the last port left this bridge. |
| 87 | */ |
| 88 | if (unset_vlan_filtering && ds->vlan_filtering_is_global) { |
| 89 | for (i = 0; i < ds->num_ports; i++) { |
| 90 | if (i == info->port) |
| 91 | continue; |
| 92 | if (dsa_to_port(ds, i)->bridge_dev == info->br) { |
| 93 | unset_vlan_filtering = false; |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | if (unset_vlan_filtering) { |
| 99 | struct switchdev_trans trans = {0}; |
| 100 | |
Vivien Didelot | 68bb8ea | 2019-10-21 16:51:15 -0400 | [diff] [blame] | 101 | err = dsa_port_vlan_filtering(dsa_to_port(ds, info->port), |
Vladimir Oltean | d371b7c | 2019-04-28 21:45:46 +0300 | [diff] [blame] | 102 | false, &trans); |
| 103 | if (err && err != EOPNOTSUPP) |
| 104 | return err; |
| 105 | } |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 109 | static int dsa_switch_fdb_add(struct dsa_switch *ds, |
| 110 | struct dsa_notifier_fdb_info *info) |
| 111 | { |
Vivien Didelot | 3169241 | 2017-11-30 12:56:43 -0500 | [diff] [blame] | 112 | int port = dsa_towards_port(ds, info->sw_index, info->port); |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 113 | |
Arkadi Sharshevsky | 1b6dd55 | 2017-08-06 16:15:40 +0300 | [diff] [blame] | 114 | if (!ds->ops->port_fdb_add) |
| 115 | return -EOPNOTSUPP; |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 116 | |
Vivien Didelot | 3169241 | 2017-11-30 12:56:43 -0500 | [diff] [blame] | 117 | return ds->ops->port_fdb_add(ds, port, info->addr, info->vid); |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static int dsa_switch_fdb_del(struct dsa_switch *ds, |
| 121 | struct dsa_notifier_fdb_info *info) |
| 122 | { |
Vivien Didelot | 3169241 | 2017-11-30 12:56:43 -0500 | [diff] [blame] | 123 | int port = dsa_towards_port(ds, info->sw_index, info->port); |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 124 | |
| 125 | if (!ds->ops->port_fdb_del) |
| 126 | return -EOPNOTSUPP; |
| 127 | |
Vivien Didelot | 3169241 | 2017-11-30 12:56:43 -0500 | [diff] [blame] | 128 | return ds->ops->port_fdb_del(ds, port, info->addr, info->vid); |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 129 | } |
| 130 | |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 131 | static bool dsa_switch_mdb_match(struct dsa_switch *ds, int port, |
| 132 | struct dsa_notifier_mdb_info *info) |
| 133 | { |
| 134 | if (ds->index == info->sw_index && port == info->port) |
| 135 | return true; |
| 136 | |
| 137 | if (dsa_is_dsa_port(ds, port)) |
| 138 | return true; |
| 139 | |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | static int dsa_switch_mdb_prepare(struct dsa_switch *ds, |
| 144 | struct dsa_notifier_mdb_info *info) |
Vivien Didelot | e6db98d | 2017-11-30 11:24:00 -0500 | [diff] [blame] | 145 | { |
| 146 | int port, err; |
| 147 | |
| 148 | if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add) |
| 149 | return -EOPNOTSUPP; |
| 150 | |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 151 | for (port = 0; port < ds->num_ports; port++) { |
| 152 | if (dsa_switch_mdb_match(ds, port, info)) { |
| 153 | err = ds->ops->port_mdb_prepare(ds, port, info->mdb); |
| 154 | if (err) |
| 155 | return err; |
| 156 | } |
Vivien Didelot | e6db98d | 2017-11-30 11:24:00 -0500 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 162 | static int dsa_switch_mdb_add(struct dsa_switch *ds, |
| 163 | struct dsa_notifier_mdb_info *info) |
| 164 | { |
Vivien Didelot | e6db98d | 2017-11-30 11:24:00 -0500 | [diff] [blame] | 165 | int port; |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 166 | |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 167 | if (switchdev_trans_ph_prepare(info->trans)) |
| 168 | return dsa_switch_mdb_prepare(ds, info); |
| 169 | |
| 170 | if (!ds->ops->port_mdb_add) |
| 171 | return 0; |
| 172 | |
Vivien Didelot | a1a6b7e | 2017-06-15 16:14:48 -0400 | [diff] [blame] | 173 | for (port = 0; port < ds->num_ports; port++) |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 174 | if (dsa_switch_mdb_match(ds, port, info)) |
| 175 | ds->ops->port_mdb_add(ds, port, info->mdb); |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int dsa_switch_mdb_del(struct dsa_switch *ds, |
| 181 | struct dsa_notifier_mdb_info *info) |
| 182 | { |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 183 | if (!ds->ops->port_mdb_del) |
| 184 | return -EOPNOTSUPP; |
| 185 | |
Vivien Didelot | a1a6b7e | 2017-06-15 16:14:48 -0400 | [diff] [blame] | 186 | if (ds->index == info->sw_index) |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 187 | return ds->ops->port_mdb_del(ds, info->port, info->mdb); |
Vivien Didelot | a1a6b7e | 2017-06-15 16:14:48 -0400 | [diff] [blame] | 188 | |
| 189 | return 0; |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 190 | } |
| 191 | |
Florian Fainelli | 061f6a5 | 2019-02-20 14:35:39 -0800 | [diff] [blame] | 192 | static int dsa_port_vlan_device_check(struct net_device *vlan_dev, |
| 193 | int vlan_dev_vid, |
| 194 | void *arg) |
| 195 | { |
| 196 | struct switchdev_obj_port_vlan *vlan = arg; |
| 197 | u16 vid; |
| 198 | |
| 199 | for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) { |
| 200 | if (vid == vlan_dev_vid) |
| 201 | return -EBUSY; |
| 202 | } |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | static int dsa_port_vlan_check(struct dsa_switch *ds, int port, |
| 208 | const struct switchdev_obj_port_vlan *vlan) |
| 209 | { |
| 210 | const struct dsa_port *dp = dsa_to_port(ds, port); |
| 211 | int err = 0; |
| 212 | |
| 213 | /* Device is not bridged, let it proceed with the VLAN device |
| 214 | * creation. |
| 215 | */ |
| 216 | if (!dp->bridge_dev) |
| 217 | return err; |
| 218 | |
Vladimir Oltean | 85478d7 | 2019-04-28 21:45:42 +0300 | [diff] [blame] | 219 | /* dsa_slave_vlan_rx_{add,kill}_vid() cannot use the prepare phase and |
Florian Fainelli | 061f6a5 | 2019-02-20 14:35:39 -0800 | [diff] [blame] | 220 | * already checks whether there is an overlapping bridge VLAN entry |
| 221 | * with the same VID, so here we only need to check that if we are |
| 222 | * adding a bridge VLAN entry there is not an overlapping VLAN device |
| 223 | * claiming that VID. |
| 224 | */ |
| 225 | return vlan_for_each(dp->slave, dsa_port_vlan_device_check, |
| 226 | (void *)vlan); |
| 227 | } |
| 228 | |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 229 | static bool dsa_switch_vlan_match(struct dsa_switch *ds, int port, |
| 230 | struct dsa_notifier_vlan_info *info) |
| 231 | { |
| 232 | if (ds->index == info->sw_index && port == info->port) |
| 233 | return true; |
| 234 | |
Vivien Didelot | 7e1741b | 2019-08-25 13:25:19 -0400 | [diff] [blame] | 235 | if (dsa_is_dsa_port(ds, port)) |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 236 | return true; |
| 237 | |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | static int dsa_switch_vlan_prepare(struct dsa_switch *ds, |
| 242 | struct dsa_notifier_vlan_info *info) |
Vivien Didelot | 9c428c5 | 2017-11-30 11:23:59 -0500 | [diff] [blame] | 243 | { |
| 244 | int port, err; |
| 245 | |
| 246 | if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add) |
| 247 | return -EOPNOTSUPP; |
| 248 | |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 249 | for (port = 0; port < ds->num_ports; port++) { |
| 250 | if (dsa_switch_vlan_match(ds, port, info)) { |
| 251 | err = dsa_port_vlan_check(ds, port, info->vlan); |
| 252 | if (err) |
| 253 | return err; |
Florian Fainelli | 061f6a5 | 2019-02-20 14:35:39 -0800 | [diff] [blame] | 254 | |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 255 | err = ds->ops->port_vlan_prepare(ds, port, info->vlan); |
| 256 | if (err) |
| 257 | return err; |
| 258 | } |
Vivien Didelot | 9c428c5 | 2017-11-30 11:23:59 -0500 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 264 | static int dsa_switch_vlan_add(struct dsa_switch *ds, |
| 265 | struct dsa_notifier_vlan_info *info) |
| 266 | { |
Vivien Didelot | 9c428c5 | 2017-11-30 11:23:59 -0500 | [diff] [blame] | 267 | int port; |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 268 | |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 269 | if (switchdev_trans_ph_prepare(info->trans)) |
| 270 | return dsa_switch_vlan_prepare(ds, info); |
| 271 | |
| 272 | if (!ds->ops->port_vlan_add) |
| 273 | return 0; |
| 274 | |
Vivien Didelot | b2f81d3 | 2017-06-07 18:12:15 -0400 | [diff] [blame] | 275 | for (port = 0; port < ds->num_ports; port++) |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 276 | if (dsa_switch_vlan_match(ds, port, info)) |
| 277 | ds->ops->port_vlan_add(ds, port, info->vlan); |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static int dsa_switch_vlan_del(struct dsa_switch *ds, |
| 283 | struct dsa_notifier_vlan_info *info) |
| 284 | { |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 285 | if (!ds->ops->port_vlan_del) |
| 286 | return -EOPNOTSUPP; |
| 287 | |
Vivien Didelot | 1ca4aa9 | 2017-06-07 18:12:14 -0400 | [diff] [blame] | 288 | if (ds->index == info->sw_index) |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 289 | return ds->ops->port_vlan_del(ds, info->port, info->vlan); |
Vivien Didelot | 1ca4aa9 | 2017-06-07 18:12:14 -0400 | [diff] [blame] | 290 | |
Vivien Didelot | 7e1741b | 2019-08-25 13:25:19 -0400 | [diff] [blame] | 291 | /* Do not deprogram the DSA links as they may be used as conduit |
| 292 | * for other VLAN members in the fabric. |
| 293 | */ |
Vivien Didelot | 1ca4aa9 | 2017-06-07 18:12:14 -0400 | [diff] [blame] | 294 | return 0; |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 295 | } |
| 296 | |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 297 | static int dsa_switch_event(struct notifier_block *nb, |
| 298 | unsigned long event, void *info) |
| 299 | { |
| 300 | struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb); |
| 301 | int err; |
| 302 | |
| 303 | switch (event) { |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 304 | case DSA_NOTIFIER_AGEING_TIME: |
| 305 | err = dsa_switch_ageing_time(ds, info); |
| 306 | break; |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 307 | case DSA_NOTIFIER_BRIDGE_JOIN: |
| 308 | err = dsa_switch_bridge_join(ds, info); |
| 309 | break; |
| 310 | case DSA_NOTIFIER_BRIDGE_LEAVE: |
| 311 | err = dsa_switch_bridge_leave(ds, info); |
| 312 | break; |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 313 | case DSA_NOTIFIER_FDB_ADD: |
| 314 | err = dsa_switch_fdb_add(ds, info); |
| 315 | break; |
| 316 | case DSA_NOTIFIER_FDB_DEL: |
| 317 | err = dsa_switch_fdb_del(ds, info); |
| 318 | break; |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 319 | case DSA_NOTIFIER_MDB_ADD: |
| 320 | err = dsa_switch_mdb_add(ds, info); |
| 321 | break; |
| 322 | case DSA_NOTIFIER_MDB_DEL: |
| 323 | err = dsa_switch_mdb_del(ds, info); |
| 324 | break; |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 325 | case DSA_NOTIFIER_VLAN_ADD: |
| 326 | err = dsa_switch_vlan_add(ds, info); |
| 327 | break; |
| 328 | case DSA_NOTIFIER_VLAN_DEL: |
| 329 | err = dsa_switch_vlan_del(ds, info); |
| 330 | break; |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 331 | default: |
| 332 | err = -EOPNOTSUPP; |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | /* Non-switchdev operations cannot be rolled back. If a DSA driver |
| 337 | * returns an error during the chained call, switch chips may be in an |
| 338 | * inconsistent state. |
| 339 | */ |
| 340 | if (err) |
| 341 | dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n", |
| 342 | event, err); |
| 343 | |
| 344 | return notifier_from_errno(err); |
| 345 | } |
| 346 | |
| 347 | int dsa_switch_register_notifier(struct dsa_switch *ds) |
| 348 | { |
| 349 | ds->nb.notifier_call = dsa_switch_event; |
| 350 | |
| 351 | return raw_notifier_chain_register(&ds->dst->nh, &ds->nb); |
| 352 | } |
| 353 | |
| 354 | void dsa_switch_unregister_notifier(struct dsa_switch *ds) |
| 355 | { |
| 356 | int err; |
| 357 | |
| 358 | err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb); |
| 359 | if (err) |
| 360 | dev_err(ds->dev, "failed to unregister notifier (%d)\n", err); |
| 361 | } |