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; |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 36 | |
Vladimir Oltean | 77b6136 | 2021-01-09 02:01:51 +0200 | [diff] [blame] | 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; |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 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 | |
Vladimir Oltean | bfcb813 | 2020-03-27 21:55:42 +0200 | [diff] [blame] | 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 | |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 86 | static int dsa_switch_bridge_join(struct dsa_switch *ds, |
| 87 | struct dsa_notifier_bridge_info *info) |
| 88 | { |
Vladimir Oltean | f66a6a6 | 2020-05-10 19:37:41 +0300 | [diff] [blame] | 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) |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 93 | return ds->ops->port_bridge_join(ds, info->port, info->br); |
| 94 | |
Vladimir Oltean | f66a6a6 | 2020-05-10 19:37:41 +0300 | [diff] [blame] | 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, |
Vivien Didelot | 40ef2c9 | 2017-03-30 17:37:14 -0400 | [diff] [blame] | 99 | info->port, info->br); |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 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 | { |
Vladimir Oltean | d371b7c | 2019-04-28 21:45:46 +0300 | [diff] [blame] | 107 | bool unset_vlan_filtering = br_vlan_enabled(info->br); |
Vladimir Oltean | f66a6a6 | 2020-05-10 19:37:41 +0300 | [diff] [blame] | 108 | struct dsa_switch_tree *dst = ds->dst; |
Vladimir Oltean | d371b7c | 2019-04-28 21:45:46 +0300 | [diff] [blame] | 109 | int err, i; |
| 110 | |
Vladimir Oltean | f66a6a6 | 2020-05-10 19:37:41 +0300 | [diff] [blame] | 111 | if (dst->index == info->tree_index && ds->index == info->sw_index && |
| 112 | ds->ops->port_bridge_join) |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 113 | ds->ops->port_bridge_leave(ds, info->port, info->br); |
| 114 | |
Vladimir Oltean | f66a6a6 | 2020-05-10 19:37:41 +0300 | [diff] [blame] | 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, |
Vivien Didelot | 40ef2c9 | 2017-03-30 17:37:14 -0400 | [diff] [blame] | 119 | info->br); |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 120 | |
Vladimir Oltean | d371b7c | 2019-04-28 21:45:46 +0300 | [diff] [blame] | 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) { |
Vladimir Oltean | 2e554a7 | 2020-10-03 01:06:46 +0300 | [diff] [blame] | 139 | err = dsa_port_vlan_filtering(dsa_to_port(ds, info->port), |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 140 | false); |
Vladimir Oltean | d371b7c | 2019-04-28 21:45:46 +0300 | [diff] [blame] | 141 | if (err && err != EOPNOTSUPP) |
| 142 | return err; |
| 143 | } |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 144 | return 0; |
| 145 | } |
| 146 | |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 147 | static int dsa_switch_fdb_add(struct dsa_switch *ds, |
| 148 | struct dsa_notifier_fdb_info *info) |
| 149 | { |
Vivien Didelot | 3169241 | 2017-11-30 12:56:43 -0500 | [diff] [blame] | 150 | int port = dsa_towards_port(ds, info->sw_index, info->port); |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 151 | |
Arkadi Sharshevsky | 1b6dd55 | 2017-08-06 16:15:40 +0300 | [diff] [blame] | 152 | if (!ds->ops->port_fdb_add) |
| 153 | return -EOPNOTSUPP; |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 154 | |
Vivien Didelot | 3169241 | 2017-11-30 12:56:43 -0500 | [diff] [blame] | 155 | return ds->ops->port_fdb_add(ds, port, info->addr, info->vid); |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static int dsa_switch_fdb_del(struct dsa_switch *ds, |
| 159 | struct dsa_notifier_fdb_info *info) |
| 160 | { |
Vivien Didelot | 3169241 | 2017-11-30 12:56:43 -0500 | [diff] [blame] | 161 | int port = dsa_towards_port(ds, info->sw_index, info->port); |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 162 | |
| 163 | if (!ds->ops->port_fdb_del) |
| 164 | return -EOPNOTSUPP; |
| 165 | |
Vivien Didelot | 3169241 | 2017-11-30 12:56:43 -0500 | [diff] [blame] | 166 | return ds->ops->port_fdb_del(ds, port, info->addr, info->vid); |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 167 | } |
| 168 | |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 169 | static bool dsa_switch_mdb_match(struct dsa_switch *ds, int port, |
| 170 | struct dsa_notifier_mdb_info *info) |
| 171 | { |
| 172 | if (ds->index == info->sw_index && port == info->port) |
| 173 | return true; |
| 174 | |
| 175 | if (dsa_is_dsa_port(ds, port)) |
| 176 | return true; |
| 177 | |
| 178 | return false; |
| 179 | } |
| 180 | |
Vladimir Oltean | ffb68fc | 2021-01-09 02:01:48 +0200 | [diff] [blame] | 181 | static int dsa_switch_mdb_add(struct dsa_switch *ds, |
| 182 | struct dsa_notifier_mdb_info *info) |
Vivien Didelot | e6db98d | 2017-11-30 11:24:00 -0500 | [diff] [blame] | 183 | { |
Vladimir Oltean | a52b2da | 2021-01-09 02:01:52 +0200 | [diff] [blame^] | 184 | int err = 0; |
| 185 | int port; |
Vivien Didelot | e6db98d | 2017-11-30 11:24:00 -0500 | [diff] [blame] | 186 | |
Vladimir Oltean | a52b2da | 2021-01-09 02:01:52 +0200 | [diff] [blame^] | 187 | if (!ds->ops->port_mdb_add) |
Vivien Didelot | e6db98d | 2017-11-30 11:24:00 -0500 | [diff] [blame] | 188 | return -EOPNOTSUPP; |
| 189 | |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 190 | for (port = 0; port < ds->num_ports; port++) { |
| 191 | if (dsa_switch_mdb_match(ds, port, info)) { |
Vladimir Oltean | a52b2da | 2021-01-09 02:01:52 +0200 | [diff] [blame^] | 192 | err = ds->ops->port_mdb_add(ds, port, info->mdb); |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 193 | if (err) |
Vladimir Oltean | a52b2da | 2021-01-09 02:01:52 +0200 | [diff] [blame^] | 194 | break; |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 195 | } |
Vivien Didelot | e6db98d | 2017-11-30 11:24:00 -0500 | [diff] [blame] | 196 | } |
| 197 | |
Vladimir Oltean | a52b2da | 2021-01-09 02:01:52 +0200 | [diff] [blame^] | 198 | return err; |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | static int dsa_switch_mdb_del(struct dsa_switch *ds, |
| 202 | struct dsa_notifier_mdb_info *info) |
| 203 | { |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 204 | if (!ds->ops->port_mdb_del) |
| 205 | return -EOPNOTSUPP; |
| 206 | |
Vivien Didelot | a1a6b7e | 2017-06-15 16:14:48 -0400 | [diff] [blame] | 207 | if (ds->index == info->sw_index) |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 208 | return ds->ops->port_mdb_del(ds, info->port, info->mdb); |
Vivien Didelot | a1a6b7e | 2017-06-15 16:14:48 -0400 | [diff] [blame] | 209 | |
| 210 | return 0; |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 211 | } |
| 212 | |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 213 | static bool dsa_switch_vlan_match(struct dsa_switch *ds, int port, |
| 214 | struct dsa_notifier_vlan_info *info) |
| 215 | { |
| 216 | if (ds->index == info->sw_index && port == info->port) |
| 217 | return true; |
| 218 | |
Vivien Didelot | 7e1741b | 2019-08-25 13:25:19 -0400 | [diff] [blame] | 219 | if (dsa_is_dsa_port(ds, port)) |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 220 | return true; |
| 221 | |
| 222 | return false; |
| 223 | } |
| 224 | |
Vladimir Oltean | ffb68fc | 2021-01-09 02:01:48 +0200 | [diff] [blame] | 225 | static int dsa_switch_vlan_add(struct dsa_switch *ds, |
| 226 | struct dsa_notifier_vlan_info *info) |
Vivien Didelot | 9c428c5 | 2017-11-30 11:23:59 -0500 | [diff] [blame] | 227 | { |
| 228 | int port, err; |
| 229 | |
| 230 | if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add) |
| 231 | return -EOPNOTSUPP; |
| 232 | |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 233 | for (port = 0; port < ds->num_ports; port++) { |
| 234 | if (dsa_switch_vlan_match(ds, port, info)) { |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 235 | err = ds->ops->port_vlan_prepare(ds, port, info->vlan); |
| 236 | if (err) |
| 237 | return err; |
| 238 | } |
Vivien Didelot | 9c428c5 | 2017-11-30 11:23:59 -0500 | [diff] [blame] | 239 | } |
| 240 | |
Vivien Didelot | b2f81d3 | 2017-06-07 18:12:15 -0400 | [diff] [blame] | 241 | for (port = 0; port < ds->num_ports; port++) |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 242 | if (dsa_switch_vlan_match(ds, port, info)) |
| 243 | ds->ops->port_vlan_add(ds, port, info->vlan); |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static int dsa_switch_vlan_del(struct dsa_switch *ds, |
| 249 | struct dsa_notifier_vlan_info *info) |
| 250 | { |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 251 | if (!ds->ops->port_vlan_del) |
| 252 | return -EOPNOTSUPP; |
| 253 | |
Vivien Didelot | 1ca4aa9 | 2017-06-07 18:12:14 -0400 | [diff] [blame] | 254 | if (ds->index == info->sw_index) |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 255 | return ds->ops->port_vlan_del(ds, info->port, info->vlan); |
Vivien Didelot | 1ca4aa9 | 2017-06-07 18:12:14 -0400 | [diff] [blame] | 256 | |
Vivien Didelot | 7e1741b | 2019-08-25 13:25:19 -0400 | [diff] [blame] | 257 | /* Do not deprogram the DSA links as they may be used as conduit |
| 258 | * for other VLAN members in the fabric. |
| 259 | */ |
Vivien Didelot | 1ca4aa9 | 2017-06-07 18:12:14 -0400 | [diff] [blame] | 260 | return 0; |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 261 | } |
| 262 | |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 263 | static int dsa_switch_event(struct notifier_block *nb, |
| 264 | unsigned long event, void *info) |
| 265 | { |
| 266 | struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb); |
| 267 | int err; |
| 268 | |
| 269 | switch (event) { |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 270 | case DSA_NOTIFIER_AGEING_TIME: |
| 271 | err = dsa_switch_ageing_time(ds, info); |
| 272 | break; |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 273 | case DSA_NOTIFIER_BRIDGE_JOIN: |
| 274 | err = dsa_switch_bridge_join(ds, info); |
| 275 | break; |
| 276 | case DSA_NOTIFIER_BRIDGE_LEAVE: |
| 277 | err = dsa_switch_bridge_leave(ds, info); |
| 278 | break; |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 279 | case DSA_NOTIFIER_FDB_ADD: |
| 280 | err = dsa_switch_fdb_add(ds, info); |
| 281 | break; |
| 282 | case DSA_NOTIFIER_FDB_DEL: |
| 283 | err = dsa_switch_fdb_del(ds, info); |
| 284 | break; |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 285 | case DSA_NOTIFIER_MDB_ADD: |
| 286 | err = dsa_switch_mdb_add(ds, info); |
| 287 | break; |
| 288 | case DSA_NOTIFIER_MDB_DEL: |
| 289 | err = dsa_switch_mdb_del(ds, info); |
| 290 | break; |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 291 | case DSA_NOTIFIER_VLAN_ADD: |
| 292 | err = dsa_switch_vlan_add(ds, info); |
| 293 | break; |
| 294 | case DSA_NOTIFIER_VLAN_DEL: |
| 295 | err = dsa_switch_vlan_del(ds, info); |
| 296 | break; |
Vladimir Oltean | bfcb813 | 2020-03-27 21:55:42 +0200 | [diff] [blame] | 297 | case DSA_NOTIFIER_MTU: |
| 298 | err = dsa_switch_mtu(ds, info); |
| 299 | break; |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 300 | default: |
| 301 | err = -EOPNOTSUPP; |
| 302 | break; |
| 303 | } |
| 304 | |
| 305 | /* Non-switchdev operations cannot be rolled back. If a DSA driver |
| 306 | * returns an error during the chained call, switch chips may be in an |
| 307 | * inconsistent state. |
| 308 | */ |
| 309 | if (err) |
| 310 | dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n", |
| 311 | event, err); |
| 312 | |
| 313 | return notifier_from_errno(err); |
| 314 | } |
| 315 | |
| 316 | int dsa_switch_register_notifier(struct dsa_switch *ds) |
| 317 | { |
| 318 | ds->nb.notifier_call = dsa_switch_event; |
| 319 | |
| 320 | return raw_notifier_chain_register(&ds->dst->nh, &ds->nb); |
| 321 | } |
| 322 | |
| 323 | void dsa_switch_unregister_notifier(struct dsa_switch *ds) |
| 324 | { |
| 325 | int err; |
| 326 | |
| 327 | err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb); |
| 328 | if (err) |
| 329 | dev_err(ds->dev, "failed to unregister notifier (%d)\n", err); |
| 330 | } |