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 | 89153ed | 2021-02-13 22:43:19 +0200 | [diff] [blame] | 109 | struct netlink_ext_ack extack = {0}; |
Vladimir Oltean | 479dc49 | 2021-03-24 11:56:39 +0200 | [diff] [blame] | 110 | int err, port; |
Vladimir Oltean | d371b7c | 2019-04-28 21:45:46 +0300 | [diff] [blame] | 111 | |
Vladimir Oltean | f66a6a6 | 2020-05-10 19:37:41 +0300 | [diff] [blame] | 112 | if (dst->index == info->tree_index && ds->index == info->sw_index && |
| 113 | ds->ops->port_bridge_join) |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 114 | ds->ops->port_bridge_leave(ds, info->port, info->br); |
| 115 | |
Vladimir Oltean | f66a6a6 | 2020-05-10 19:37:41 +0300 | [diff] [blame] | 116 | if ((dst->index != info->tree_index || ds->index != info->sw_index) && |
| 117 | ds->ops->crosschip_bridge_join) |
| 118 | ds->ops->crosschip_bridge_leave(ds, info->tree_index, |
| 119 | info->sw_index, info->port, |
Vivien Didelot | 40ef2c9 | 2017-03-30 17:37:14 -0400 | [diff] [blame] | 120 | info->br); |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 121 | |
Vladimir Oltean | d371b7c | 2019-04-28 21:45:46 +0300 | [diff] [blame] | 122 | /* If the bridge was vlan_filtering, the bridge core doesn't trigger an |
| 123 | * event for changing vlan_filtering setting upon slave ports leaving |
| 124 | * it. That is a good thing, because that lets us handle it and also |
| 125 | * handle the case where the switch's vlan_filtering setting is global |
| 126 | * (not per port). When that happens, the correct moment to trigger the |
Vladimir Oltean | 479dc49 | 2021-03-24 11:56:39 +0200 | [diff] [blame] | 127 | * vlan_filtering callback is only when the last port leaves the last |
| 128 | * VLAN-aware bridge. |
Vladimir Oltean | d371b7c | 2019-04-28 21:45:46 +0300 | [diff] [blame] | 129 | */ |
| 130 | if (unset_vlan_filtering && ds->vlan_filtering_is_global) { |
Vladimir Oltean | 479dc49 | 2021-03-24 11:56:39 +0200 | [diff] [blame] | 131 | for (port = 0; port < ds->num_ports; port++) { |
| 132 | struct net_device *bridge_dev; |
| 133 | |
| 134 | bridge_dev = dsa_to_port(ds, port)->bridge_dev; |
| 135 | |
| 136 | if (bridge_dev && br_vlan_enabled(bridge_dev)) { |
Vladimir Oltean | d371b7c | 2019-04-28 21:45:46 +0300 | [diff] [blame] | 137 | unset_vlan_filtering = false; |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | if (unset_vlan_filtering) { |
Vladimir Oltean | 2e554a7 | 2020-10-03 01:06:46 +0300 | [diff] [blame] | 143 | err = dsa_port_vlan_filtering(dsa_to_port(ds, info->port), |
Vladimir Oltean | 89153ed | 2021-02-13 22:43:19 +0200 | [diff] [blame] | 144 | false, &extack); |
| 145 | if (extack._msg) |
| 146 | dev_err(ds->dev, "port %d: %s\n", info->port, |
| 147 | extack._msg); |
Vladimir Oltean | d371b7c | 2019-04-28 21:45:46 +0300 | [diff] [blame] | 148 | if (err && err != EOPNOTSUPP) |
| 149 | return err; |
| 150 | } |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 154 | static int dsa_switch_fdb_add(struct dsa_switch *ds, |
| 155 | struct dsa_notifier_fdb_info *info) |
| 156 | { |
Vivien Didelot | 3169241 | 2017-11-30 12:56:43 -0500 | [diff] [blame] | 157 | int port = dsa_towards_port(ds, info->sw_index, info->port); |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 158 | |
Arkadi Sharshevsky | 1b6dd55 | 2017-08-06 16:15:40 +0300 | [diff] [blame] | 159 | if (!ds->ops->port_fdb_add) |
| 160 | return -EOPNOTSUPP; |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 161 | |
Vivien Didelot | 3169241 | 2017-11-30 12:56:43 -0500 | [diff] [blame] | 162 | return ds->ops->port_fdb_add(ds, port, info->addr, info->vid); |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static int dsa_switch_fdb_del(struct dsa_switch *ds, |
| 166 | struct dsa_notifier_fdb_info *info) |
| 167 | { |
Vivien Didelot | 3169241 | 2017-11-30 12:56:43 -0500 | [diff] [blame] | 168 | int port = dsa_towards_port(ds, info->sw_index, info->port); |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 169 | |
| 170 | if (!ds->ops->port_fdb_del) |
| 171 | return -EOPNOTSUPP; |
| 172 | |
Vivien Didelot | 3169241 | 2017-11-30 12:56:43 -0500 | [diff] [blame] | 173 | return ds->ops->port_fdb_del(ds, port, info->addr, info->vid); |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 174 | } |
| 175 | |
George McCollister | 18596f5 | 2021-02-09 19:02:12 -0600 | [diff] [blame] | 176 | static int dsa_switch_hsr_join(struct dsa_switch *ds, |
| 177 | struct dsa_notifier_hsr_info *info) |
| 178 | { |
| 179 | if (ds->index == info->sw_index && ds->ops->port_hsr_join) |
| 180 | return ds->ops->port_hsr_join(ds, info->port, info->hsr); |
| 181 | |
| 182 | return -EOPNOTSUPP; |
| 183 | } |
| 184 | |
| 185 | static int dsa_switch_hsr_leave(struct dsa_switch *ds, |
| 186 | struct dsa_notifier_hsr_info *info) |
| 187 | { |
| 188 | if (ds->index == info->sw_index && ds->ops->port_hsr_leave) |
| 189 | return ds->ops->port_hsr_leave(ds, info->port, info->hsr); |
| 190 | |
| 191 | return -EOPNOTSUPP; |
| 192 | } |
| 193 | |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 194 | static int dsa_switch_lag_change(struct dsa_switch *ds, |
| 195 | struct dsa_notifier_lag_info *info) |
| 196 | { |
| 197 | if (ds->index == info->sw_index && ds->ops->port_lag_change) |
| 198 | return ds->ops->port_lag_change(ds, info->port); |
| 199 | |
| 200 | if (ds->index != info->sw_index && ds->ops->crosschip_lag_change) |
| 201 | return ds->ops->crosschip_lag_change(ds, info->sw_index, |
| 202 | info->port); |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | static int dsa_switch_lag_join(struct dsa_switch *ds, |
| 208 | struct dsa_notifier_lag_info *info) |
| 209 | { |
| 210 | if (ds->index == info->sw_index && ds->ops->port_lag_join) |
| 211 | return ds->ops->port_lag_join(ds, info->port, info->lag, |
| 212 | info->info); |
| 213 | |
| 214 | if (ds->index != info->sw_index && ds->ops->crosschip_lag_join) |
| 215 | return ds->ops->crosschip_lag_join(ds, info->sw_index, |
| 216 | info->port, info->lag, |
| 217 | info->info); |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static int dsa_switch_lag_leave(struct dsa_switch *ds, |
| 223 | struct dsa_notifier_lag_info *info) |
| 224 | { |
| 225 | if (ds->index == info->sw_index && ds->ops->port_lag_leave) |
| 226 | return ds->ops->port_lag_leave(ds, info->port, info->lag); |
| 227 | |
| 228 | if (ds->index != info->sw_index && ds->ops->crosschip_lag_leave) |
| 229 | return ds->ops->crosschip_lag_leave(ds, info->sw_index, |
| 230 | info->port, info->lag); |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
Vladimir Oltean | ffb68fc | 2021-01-09 02:01:48 +0200 | [diff] [blame] | 235 | static int dsa_switch_mdb_add(struct dsa_switch *ds, |
| 236 | struct dsa_notifier_mdb_info *info) |
Vivien Didelot | e6db98d | 2017-11-30 11:24:00 -0500 | [diff] [blame] | 237 | { |
Vladimir Oltean | abd4953 | 2021-06-21 19:42:16 +0300 | [diff] [blame^] | 238 | int port = dsa_towards_port(ds, info->sw_index, info->port); |
Vivien Didelot | e6db98d | 2017-11-30 11:24:00 -0500 | [diff] [blame] | 239 | |
Vladimir Oltean | a52b2da | 2021-01-09 02:01:52 +0200 | [diff] [blame] | 240 | if (!ds->ops->port_mdb_add) |
Vivien Didelot | e6db98d | 2017-11-30 11:24:00 -0500 | [diff] [blame] | 241 | return -EOPNOTSUPP; |
| 242 | |
Vladimir Oltean | abd4953 | 2021-06-21 19:42:16 +0300 | [diff] [blame^] | 243 | return ds->ops->port_mdb_add(ds, port, info->mdb); |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static int dsa_switch_mdb_del(struct dsa_switch *ds, |
| 247 | struct dsa_notifier_mdb_info *info) |
| 248 | { |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 249 | if (!ds->ops->port_mdb_del) |
| 250 | return -EOPNOTSUPP; |
| 251 | |
Vivien Didelot | a1a6b7e | 2017-06-15 16:14:48 -0400 | [diff] [blame] | 252 | if (ds->index == info->sw_index) |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 253 | return ds->ops->port_mdb_del(ds, info->port, info->mdb); |
Vivien Didelot | a1a6b7e | 2017-06-15 16:14:48 -0400 | [diff] [blame] | 254 | |
| 255 | return 0; |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 256 | } |
| 257 | |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 258 | static bool dsa_switch_vlan_match(struct dsa_switch *ds, int port, |
| 259 | struct dsa_notifier_vlan_info *info) |
| 260 | { |
| 261 | if (ds->index == info->sw_index && port == info->port) |
| 262 | return true; |
| 263 | |
Vivien Didelot | 7e1741b | 2019-08-25 13:25:19 -0400 | [diff] [blame] | 264 | if (dsa_is_dsa_port(ds, port)) |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 265 | return true; |
| 266 | |
| 267 | return false; |
| 268 | } |
| 269 | |
Vladimir Oltean | ffb68fc | 2021-01-09 02:01:48 +0200 | [diff] [blame] | 270 | static int dsa_switch_vlan_add(struct dsa_switch *ds, |
| 271 | struct dsa_notifier_vlan_info *info) |
Vivien Didelot | 9c428c5 | 2017-11-30 11:23:59 -0500 | [diff] [blame] | 272 | { |
| 273 | int port, err; |
| 274 | |
Vladimir Oltean | 1958d58 | 2021-01-09 02:01:53 +0200 | [diff] [blame] | 275 | if (!ds->ops->port_vlan_add) |
Vivien Didelot | 9c428c5 | 2017-11-30 11:23:59 -0500 | [diff] [blame] | 276 | return -EOPNOTSUPP; |
| 277 | |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 278 | for (port = 0; port < ds->num_ports; port++) { |
| 279 | if (dsa_switch_vlan_match(ds, port, info)) { |
Vladimir Oltean | 31046a5 | 2021-02-13 22:43:18 +0200 | [diff] [blame] | 280 | err = ds->ops->port_vlan_add(ds, port, info->vlan, |
| 281 | info->extack); |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 282 | if (err) |
| 283 | return err; |
| 284 | } |
Vivien Didelot | 9c428c5 | 2017-11-30 11:23:59 -0500 | [diff] [blame] | 285 | } |
| 286 | |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | static int dsa_switch_vlan_del(struct dsa_switch *ds, |
| 291 | struct dsa_notifier_vlan_info *info) |
| 292 | { |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 293 | if (!ds->ops->port_vlan_del) |
| 294 | return -EOPNOTSUPP; |
| 295 | |
Vivien Didelot | 1ca4aa9 | 2017-06-07 18:12:14 -0400 | [diff] [blame] | 296 | if (ds->index == info->sw_index) |
Vivien Didelot | e65d45c | 2019-08-25 13:25:15 -0400 | [diff] [blame] | 297 | return ds->ops->port_vlan_del(ds, info->port, info->vlan); |
Vivien Didelot | 1ca4aa9 | 2017-06-07 18:12:14 -0400 | [diff] [blame] | 298 | |
Vivien Didelot | 7e1741b | 2019-08-25 13:25:19 -0400 | [diff] [blame] | 299 | /* Do not deprogram the DSA links as they may be used as conduit |
| 300 | * for other VLAN members in the fabric. |
| 301 | */ |
Vivien Didelot | 1ca4aa9 | 2017-06-07 18:12:14 -0400 | [diff] [blame] | 302 | return 0; |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 303 | } |
| 304 | |
Vladimir Oltean | 53da0eb | 2021-01-29 03:00:06 +0200 | [diff] [blame] | 305 | static int dsa_switch_change_tag_proto(struct dsa_switch *ds, |
| 306 | struct dsa_notifier_tag_proto_info *info) |
| 307 | { |
| 308 | const struct dsa_device_ops *tag_ops = info->tag_ops; |
| 309 | int port, err; |
| 310 | |
| 311 | if (!ds->ops->change_tag_protocol) |
| 312 | return -EOPNOTSUPP; |
| 313 | |
| 314 | ASSERT_RTNL(); |
| 315 | |
| 316 | for (port = 0; port < ds->num_ports; port++) { |
Tobias Waldekranz | 21e0b50 | 2021-04-20 20:53:09 +0200 | [diff] [blame] | 317 | if (!dsa_is_cpu_port(ds, port)) |
| 318 | continue; |
Vladimir Oltean | 53da0eb | 2021-01-29 03:00:06 +0200 | [diff] [blame] | 319 | |
Tobias Waldekranz | 21e0b50 | 2021-04-20 20:53:09 +0200 | [diff] [blame] | 320 | err = ds->ops->change_tag_protocol(ds, port, tag_ops->proto); |
| 321 | if (err) |
| 322 | return err; |
| 323 | |
| 324 | dsa_port_set_tag_protocol(dsa_to_port(ds, port), tag_ops); |
Vladimir Oltean | 53da0eb | 2021-01-29 03:00:06 +0200 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /* Now that changing the tag protocol can no longer fail, let's update |
| 328 | * the remaining bits which are "duplicated for faster access", and the |
| 329 | * bits that depend on the tagger, such as the MTU. |
| 330 | */ |
| 331 | for (port = 0; port < ds->num_ports; port++) { |
| 332 | if (dsa_is_user_port(ds, port)) { |
| 333 | struct net_device *slave; |
| 334 | |
| 335 | slave = dsa_to_port(ds, port)->slave; |
| 336 | dsa_slave_setup_tagger(slave); |
| 337 | |
| 338 | /* rtnl_mutex is held in dsa_tree_change_tag_proto */ |
| 339 | dsa_slave_change_mtu(slave, slave->mtu); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
Horatiu Vultur | c595c43 | 2021-02-16 22:42:04 +0100 | [diff] [blame] | 346 | static bool dsa_switch_mrp_match(struct dsa_switch *ds, int port, |
| 347 | struct dsa_notifier_mrp_info *info) |
| 348 | { |
| 349 | if (ds->index == info->sw_index && port == info->port) |
| 350 | return true; |
| 351 | |
| 352 | if (dsa_is_dsa_port(ds, port)) |
| 353 | return true; |
| 354 | |
| 355 | return false; |
| 356 | } |
| 357 | |
| 358 | static int dsa_switch_mrp_add(struct dsa_switch *ds, |
| 359 | struct dsa_notifier_mrp_info *info) |
| 360 | { |
| 361 | int err = 0; |
| 362 | int port; |
| 363 | |
| 364 | if (!ds->ops->port_mrp_add) |
| 365 | return -EOPNOTSUPP; |
| 366 | |
| 367 | for (port = 0; port < ds->num_ports; port++) { |
| 368 | if (dsa_switch_mrp_match(ds, port, info)) { |
| 369 | err = ds->ops->port_mrp_add(ds, port, info->mrp); |
| 370 | if (err) |
| 371 | break; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | return err; |
| 376 | } |
| 377 | |
| 378 | static int dsa_switch_mrp_del(struct dsa_switch *ds, |
| 379 | struct dsa_notifier_mrp_info *info) |
| 380 | { |
| 381 | if (!ds->ops->port_mrp_del) |
| 382 | return -EOPNOTSUPP; |
| 383 | |
| 384 | if (ds->index == info->sw_index) |
| 385 | return ds->ops->port_mrp_del(ds, info->port, info->mrp); |
| 386 | |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | static bool |
| 391 | dsa_switch_mrp_ring_role_match(struct dsa_switch *ds, int port, |
| 392 | struct dsa_notifier_mrp_ring_role_info *info) |
| 393 | { |
| 394 | if (ds->index == info->sw_index && port == info->port) |
| 395 | return true; |
| 396 | |
| 397 | if (dsa_is_dsa_port(ds, port)) |
| 398 | return true; |
| 399 | |
| 400 | return false; |
| 401 | } |
| 402 | |
| 403 | static int |
| 404 | dsa_switch_mrp_add_ring_role(struct dsa_switch *ds, |
| 405 | struct dsa_notifier_mrp_ring_role_info *info) |
| 406 | { |
| 407 | int err = 0; |
| 408 | int port; |
| 409 | |
| 410 | if (!ds->ops->port_mrp_add) |
| 411 | return -EOPNOTSUPP; |
| 412 | |
| 413 | for (port = 0; port < ds->num_ports; port++) { |
| 414 | if (dsa_switch_mrp_ring_role_match(ds, port, info)) { |
| 415 | err = ds->ops->port_mrp_add_ring_role(ds, port, |
| 416 | info->mrp); |
| 417 | if (err) |
| 418 | break; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | return err; |
| 423 | } |
| 424 | |
| 425 | static int |
| 426 | dsa_switch_mrp_del_ring_role(struct dsa_switch *ds, |
| 427 | struct dsa_notifier_mrp_ring_role_info *info) |
| 428 | { |
| 429 | if (!ds->ops->port_mrp_del) |
| 430 | return -EOPNOTSUPP; |
| 431 | |
| 432 | if (ds->index == info->sw_index) |
| 433 | return ds->ops->port_mrp_del_ring_role(ds, info->port, |
| 434 | info->mrp); |
| 435 | |
| 436 | return 0; |
| 437 | } |
| 438 | |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 439 | static int dsa_switch_event(struct notifier_block *nb, |
| 440 | unsigned long event, void *info) |
| 441 | { |
| 442 | struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb); |
| 443 | int err; |
| 444 | |
| 445 | switch (event) { |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 446 | case DSA_NOTIFIER_AGEING_TIME: |
| 447 | err = dsa_switch_ageing_time(ds, info); |
| 448 | break; |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 449 | case DSA_NOTIFIER_BRIDGE_JOIN: |
| 450 | err = dsa_switch_bridge_join(ds, info); |
| 451 | break; |
| 452 | case DSA_NOTIFIER_BRIDGE_LEAVE: |
| 453 | err = dsa_switch_bridge_leave(ds, info); |
| 454 | break; |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 455 | case DSA_NOTIFIER_FDB_ADD: |
| 456 | err = dsa_switch_fdb_add(ds, info); |
| 457 | break; |
| 458 | case DSA_NOTIFIER_FDB_DEL: |
| 459 | err = dsa_switch_fdb_del(ds, info); |
| 460 | break; |
George McCollister | 18596f5 | 2021-02-09 19:02:12 -0600 | [diff] [blame] | 461 | case DSA_NOTIFIER_HSR_JOIN: |
| 462 | err = dsa_switch_hsr_join(ds, info); |
| 463 | break; |
| 464 | case DSA_NOTIFIER_HSR_LEAVE: |
| 465 | err = dsa_switch_hsr_leave(ds, info); |
| 466 | break; |
Tobias Waldekranz | 058102a | 2021-01-13 09:42:53 +0100 | [diff] [blame] | 467 | case DSA_NOTIFIER_LAG_CHANGE: |
| 468 | err = dsa_switch_lag_change(ds, info); |
| 469 | break; |
| 470 | case DSA_NOTIFIER_LAG_JOIN: |
| 471 | err = dsa_switch_lag_join(ds, info); |
| 472 | break; |
| 473 | case DSA_NOTIFIER_LAG_LEAVE: |
| 474 | err = dsa_switch_lag_leave(ds, info); |
| 475 | break; |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 476 | case DSA_NOTIFIER_MDB_ADD: |
| 477 | err = dsa_switch_mdb_add(ds, info); |
| 478 | break; |
| 479 | case DSA_NOTIFIER_MDB_DEL: |
| 480 | err = dsa_switch_mdb_del(ds, info); |
| 481 | break; |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 482 | case DSA_NOTIFIER_VLAN_ADD: |
| 483 | err = dsa_switch_vlan_add(ds, info); |
| 484 | break; |
| 485 | case DSA_NOTIFIER_VLAN_DEL: |
| 486 | err = dsa_switch_vlan_del(ds, info); |
| 487 | break; |
Vladimir Oltean | bfcb813 | 2020-03-27 21:55:42 +0200 | [diff] [blame] | 488 | case DSA_NOTIFIER_MTU: |
| 489 | err = dsa_switch_mtu(ds, info); |
| 490 | break; |
Vladimir Oltean | 53da0eb | 2021-01-29 03:00:06 +0200 | [diff] [blame] | 491 | case DSA_NOTIFIER_TAG_PROTO: |
| 492 | err = dsa_switch_change_tag_proto(ds, info); |
| 493 | break; |
Horatiu Vultur | c595c43 | 2021-02-16 22:42:04 +0100 | [diff] [blame] | 494 | case DSA_NOTIFIER_MRP_ADD: |
| 495 | err = dsa_switch_mrp_add(ds, info); |
| 496 | break; |
| 497 | case DSA_NOTIFIER_MRP_DEL: |
| 498 | err = dsa_switch_mrp_del(ds, info); |
| 499 | break; |
| 500 | case DSA_NOTIFIER_MRP_ADD_RING_ROLE: |
| 501 | err = dsa_switch_mrp_add_ring_role(ds, info); |
| 502 | break; |
| 503 | case DSA_NOTIFIER_MRP_DEL_RING_ROLE: |
| 504 | err = dsa_switch_mrp_del_ring_role(ds, info); |
| 505 | break; |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 506 | default: |
| 507 | err = -EOPNOTSUPP; |
| 508 | break; |
| 509 | } |
| 510 | |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 511 | if (err) |
| 512 | dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n", |
| 513 | event, err); |
| 514 | |
| 515 | return notifier_from_errno(err); |
| 516 | } |
| 517 | |
| 518 | int dsa_switch_register_notifier(struct dsa_switch *ds) |
| 519 | { |
| 520 | ds->nb.notifier_call = dsa_switch_event; |
| 521 | |
| 522 | return raw_notifier_chain_register(&ds->dst->nh, &ds->nb); |
| 523 | } |
| 524 | |
| 525 | void dsa_switch_unregister_notifier(struct dsa_switch *ds) |
| 526 | { |
| 527 | int err; |
| 528 | |
| 529 | err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb); |
| 530 | if (err) |
| 531 | dev_err(ds->dev, "failed to unregister notifier (%d)\n", err); |
| 532 | } |