Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Handling of a single switch chip, part of a switch fabric |
| 3 | * |
Vivien Didelot | 4333d61 | 2017-03-28 15:10:36 -0400 | [diff] [blame] | 4 | * Copyright (c) 2017 Savoir-faire Linux Inc. |
| 5 | * Vivien Didelot <vivien.didelot@savoirfairelinux.com> |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/netdevice.h> |
| 14 | #include <linux/notifier.h> |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 15 | #include <net/switchdev.h> |
Vivien Didelot | ea5dd34 | 2017-05-17 15:46:03 -0400 | [diff] [blame] | 16 | |
| 17 | #include "dsa_priv.h" |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 18 | |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 19 | static unsigned int dsa_switch_fastest_ageing_time(struct dsa_switch *ds, |
| 20 | unsigned int ageing_time) |
| 21 | { |
| 22 | int i; |
| 23 | |
| 24 | for (i = 0; i < ds->num_ports; ++i) { |
| 25 | struct dsa_port *dp = &ds->ports[i]; |
| 26 | |
| 27 | if (dp->ageing_time && dp->ageing_time < ageing_time) |
| 28 | ageing_time = dp->ageing_time; |
| 29 | } |
| 30 | |
| 31 | return ageing_time; |
| 32 | } |
| 33 | |
| 34 | static int dsa_switch_ageing_time(struct dsa_switch *ds, |
| 35 | struct dsa_notifier_ageing_time_info *info) |
| 36 | { |
| 37 | unsigned int ageing_time = info->ageing_time; |
| 38 | struct switchdev_trans *trans = info->trans; |
| 39 | |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 40 | if (switchdev_trans_ph_prepare(trans)) { |
| 41 | if (ds->ageing_time_min && ageing_time < ds->ageing_time_min) |
| 42 | return -ERANGE; |
| 43 | if (ds->ageing_time_max && ageing_time > ds->ageing_time_max) |
| 44 | return -ERANGE; |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | /* Program the fastest ageing time in case of multiple bridges */ |
| 49 | ageing_time = dsa_switch_fastest_ageing_time(ds, ageing_time); |
| 50 | |
| 51 | if (ds->ops->set_ageing_time) |
| 52 | return ds->ops->set_ageing_time(ds, ageing_time); |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 57 | static int dsa_switch_bridge_join(struct dsa_switch *ds, |
| 58 | struct dsa_notifier_bridge_info *info) |
| 59 | { |
| 60 | if (ds->index == info->sw_index && ds->ops->port_bridge_join) |
| 61 | return ds->ops->port_bridge_join(ds, info->port, info->br); |
| 62 | |
Vivien Didelot | 40ef2c9 | 2017-03-30 17:37:14 -0400 | [diff] [blame] | 63 | if (ds->index != info->sw_index && ds->ops->crosschip_bridge_join) |
| 64 | return ds->ops->crosschip_bridge_join(ds, info->sw_index, |
| 65 | info->port, info->br); |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int dsa_switch_bridge_leave(struct dsa_switch *ds, |
| 71 | struct dsa_notifier_bridge_info *info) |
| 72 | { |
| 73 | if (ds->index == info->sw_index && ds->ops->port_bridge_leave) |
| 74 | ds->ops->port_bridge_leave(ds, info->port, info->br); |
| 75 | |
Vivien Didelot | 40ef2c9 | 2017-03-30 17:37:14 -0400 | [diff] [blame] | 76 | if (ds->index != info->sw_index && ds->ops->crosschip_bridge_leave) |
| 77 | ds->ops->crosschip_bridge_leave(ds, info->sw_index, info->port, |
| 78 | info->br); |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 83 | static int dsa_switch_fdb_add(struct dsa_switch *ds, |
| 84 | struct dsa_notifier_fdb_info *info) |
| 85 | { |
Vivien Didelot | 3169241 | 2017-11-30 12:56:43 -0500 | [diff] [blame] | 86 | int port = dsa_towards_port(ds, info->sw_index, info->port); |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 87 | |
Arkadi Sharshevsky | 1b6dd55 | 2017-08-06 16:15:40 +0300 | [diff] [blame] | 88 | if (!ds->ops->port_fdb_add) |
| 89 | return -EOPNOTSUPP; |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 90 | |
Vivien Didelot | 3169241 | 2017-11-30 12:56:43 -0500 | [diff] [blame] | 91 | return ds->ops->port_fdb_add(ds, port, info->addr, info->vid); |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static int dsa_switch_fdb_del(struct dsa_switch *ds, |
| 95 | struct dsa_notifier_fdb_info *info) |
| 96 | { |
Vivien Didelot | 3169241 | 2017-11-30 12:56:43 -0500 | [diff] [blame] | 97 | int port = dsa_towards_port(ds, info->sw_index, info->port); |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 98 | |
| 99 | if (!ds->ops->port_fdb_del) |
| 100 | return -EOPNOTSUPP; |
| 101 | |
Vivien Didelot | 3169241 | 2017-11-30 12:56:43 -0500 | [diff] [blame] | 102 | return ds->ops->port_fdb_del(ds, port, info->addr, info->vid); |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 103 | } |
| 104 | |
Vivien Didelot | e6db98d | 2017-11-30 11:24:00 -0500 | [diff] [blame] | 105 | static int |
| 106 | dsa_switch_mdb_prepare_bitmap(struct dsa_switch *ds, |
| 107 | const struct switchdev_obj_port_mdb *mdb, |
| 108 | const unsigned long *bitmap) |
| 109 | { |
| 110 | int port, err; |
| 111 | |
| 112 | if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add) |
| 113 | return -EOPNOTSUPP; |
| 114 | |
| 115 | for_each_set_bit(port, bitmap, ds->num_ports) { |
| 116 | err = ds->ops->port_mdb_prepare(ds, port, mdb); |
| 117 | if (err) |
| 118 | return err; |
| 119 | } |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static void dsa_switch_mdb_add_bitmap(struct dsa_switch *ds, |
| 125 | const struct switchdev_obj_port_mdb *mdb, |
| 126 | const unsigned long *bitmap) |
| 127 | { |
| 128 | int port; |
| 129 | |
| 130 | for_each_set_bit(port, bitmap, ds->num_ports) |
| 131 | ds->ops->port_mdb_add(ds, port, mdb); |
| 132 | } |
| 133 | |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 134 | static int dsa_switch_mdb_add(struct dsa_switch *ds, |
| 135 | struct dsa_notifier_mdb_info *info) |
| 136 | { |
| 137 | const struct switchdev_obj_port_mdb *mdb = info->mdb; |
| 138 | struct switchdev_trans *trans = info->trans; |
Vivien Didelot | a1a6b7e | 2017-06-15 16:14:48 -0400 | [diff] [blame] | 139 | DECLARE_BITMAP(group, ds->num_ports); |
Vivien Didelot | e6db98d | 2017-11-30 11:24:00 -0500 | [diff] [blame] | 140 | int port; |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 141 | |
Vivien Didelot | a1a6b7e | 2017-06-15 16:14:48 -0400 | [diff] [blame] | 142 | /* Build a mask of Multicast group members */ |
| 143 | bitmap_zero(group, ds->num_ports); |
| 144 | if (ds->index == info->sw_index) |
| 145 | set_bit(info->port, group); |
| 146 | for (port = 0; port < ds->num_ports; port++) |
Andrew Lunn | ae45102 | 2017-11-09 23:11:02 +0100 | [diff] [blame] | 147 | if (dsa_is_dsa_port(ds, port)) |
Vivien Didelot | a1a6b7e | 2017-06-15 16:14:48 -0400 | [diff] [blame] | 148 | set_bit(port, group); |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 149 | |
Vivien Didelot | e6db98d | 2017-11-30 11:24:00 -0500 | [diff] [blame] | 150 | if (switchdev_trans_ph_prepare(trans)) |
| 151 | return dsa_switch_mdb_prepare_bitmap(ds, mdb, group); |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 152 | |
Vivien Didelot | e6db98d | 2017-11-30 11:24:00 -0500 | [diff] [blame] | 153 | dsa_switch_mdb_add_bitmap(ds, mdb, group); |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static int dsa_switch_mdb_del(struct dsa_switch *ds, |
| 159 | struct dsa_notifier_mdb_info *info) |
| 160 | { |
| 161 | const struct switchdev_obj_port_mdb *mdb = info->mdb; |
| 162 | |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 163 | if (!ds->ops->port_mdb_del) |
| 164 | return -EOPNOTSUPP; |
| 165 | |
Vivien Didelot | a1a6b7e | 2017-06-15 16:14:48 -0400 | [diff] [blame] | 166 | if (ds->index == info->sw_index) |
| 167 | return ds->ops->port_mdb_del(ds, info->port, mdb); |
| 168 | |
| 169 | return 0; |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 170 | } |
| 171 | |
Vivien Didelot | 9c428c5 | 2017-11-30 11:23:59 -0500 | [diff] [blame] | 172 | static int |
| 173 | dsa_switch_vlan_prepare_bitmap(struct dsa_switch *ds, |
| 174 | const struct switchdev_obj_port_vlan *vlan, |
| 175 | const unsigned long *bitmap) |
| 176 | { |
| 177 | int port, err; |
| 178 | |
| 179 | if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add) |
| 180 | return -EOPNOTSUPP; |
| 181 | |
| 182 | for_each_set_bit(port, bitmap, ds->num_ports) { |
| 183 | err = ds->ops->port_vlan_prepare(ds, port, vlan); |
| 184 | if (err) |
| 185 | return err; |
| 186 | } |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | static void |
| 192 | dsa_switch_vlan_add_bitmap(struct dsa_switch *ds, |
| 193 | const struct switchdev_obj_port_vlan *vlan, |
| 194 | const unsigned long *bitmap) |
| 195 | { |
| 196 | int port; |
| 197 | |
| 198 | for_each_set_bit(port, bitmap, ds->num_ports) |
| 199 | ds->ops->port_vlan_add(ds, port, vlan); |
| 200 | } |
| 201 | |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 202 | static int dsa_switch_vlan_add(struct dsa_switch *ds, |
| 203 | struct dsa_notifier_vlan_info *info) |
| 204 | { |
| 205 | const struct switchdev_obj_port_vlan *vlan = info->vlan; |
| 206 | struct switchdev_trans *trans = info->trans; |
Vivien Didelot | 1ca4aa9 | 2017-06-07 18:12:14 -0400 | [diff] [blame] | 207 | DECLARE_BITMAP(members, ds->num_ports); |
Vivien Didelot | 9c428c5 | 2017-11-30 11:23:59 -0500 | [diff] [blame] | 208 | int port; |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 209 | |
Vivien Didelot | 1ca4aa9 | 2017-06-07 18:12:14 -0400 | [diff] [blame] | 210 | /* Build a mask of VLAN members */ |
| 211 | bitmap_zero(members, ds->num_ports); |
| 212 | if (ds->index == info->sw_index) |
| 213 | set_bit(info->port, members); |
Vivien Didelot | b2f81d3 | 2017-06-07 18:12:15 -0400 | [diff] [blame] | 214 | for (port = 0; port < ds->num_ports; port++) |
| 215 | if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) |
| 216 | set_bit(port, members); |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 217 | |
Vivien Didelot | 9c428c5 | 2017-11-30 11:23:59 -0500 | [diff] [blame] | 218 | if (switchdev_trans_ph_prepare(trans)) |
| 219 | return dsa_switch_vlan_prepare_bitmap(ds, vlan, members); |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 220 | |
Vivien Didelot | 9c428c5 | 2017-11-30 11:23:59 -0500 | [diff] [blame] | 221 | dsa_switch_vlan_add_bitmap(ds, vlan, members); |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static int dsa_switch_vlan_del(struct dsa_switch *ds, |
| 227 | struct dsa_notifier_vlan_info *info) |
| 228 | { |
| 229 | const struct switchdev_obj_port_vlan *vlan = info->vlan; |
| 230 | |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 231 | if (!ds->ops->port_vlan_del) |
| 232 | return -EOPNOTSUPP; |
| 233 | |
Vivien Didelot | 1ca4aa9 | 2017-06-07 18:12:14 -0400 | [diff] [blame] | 234 | if (ds->index == info->sw_index) |
| 235 | return ds->ops->port_vlan_del(ds, info->port, vlan); |
| 236 | |
| 237 | return 0; |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 238 | } |
| 239 | |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 240 | static int dsa_switch_event(struct notifier_block *nb, |
| 241 | unsigned long event, void *info) |
| 242 | { |
| 243 | struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb); |
| 244 | int err; |
| 245 | |
| 246 | switch (event) { |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 247 | case DSA_NOTIFIER_AGEING_TIME: |
| 248 | err = dsa_switch_ageing_time(ds, info); |
| 249 | break; |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 250 | case DSA_NOTIFIER_BRIDGE_JOIN: |
| 251 | err = dsa_switch_bridge_join(ds, info); |
| 252 | break; |
| 253 | case DSA_NOTIFIER_BRIDGE_LEAVE: |
| 254 | err = dsa_switch_bridge_leave(ds, info); |
| 255 | break; |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 256 | case DSA_NOTIFIER_FDB_ADD: |
| 257 | err = dsa_switch_fdb_add(ds, info); |
| 258 | break; |
| 259 | case DSA_NOTIFIER_FDB_DEL: |
| 260 | err = dsa_switch_fdb_del(ds, info); |
| 261 | break; |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 262 | case DSA_NOTIFIER_MDB_ADD: |
| 263 | err = dsa_switch_mdb_add(ds, info); |
| 264 | break; |
| 265 | case DSA_NOTIFIER_MDB_DEL: |
| 266 | err = dsa_switch_mdb_del(ds, info); |
| 267 | break; |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 268 | case DSA_NOTIFIER_VLAN_ADD: |
| 269 | err = dsa_switch_vlan_add(ds, info); |
| 270 | break; |
| 271 | case DSA_NOTIFIER_VLAN_DEL: |
| 272 | err = dsa_switch_vlan_del(ds, info); |
| 273 | break; |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 274 | default: |
| 275 | err = -EOPNOTSUPP; |
| 276 | break; |
| 277 | } |
| 278 | |
| 279 | /* Non-switchdev operations cannot be rolled back. If a DSA driver |
| 280 | * returns an error during the chained call, switch chips may be in an |
| 281 | * inconsistent state. |
| 282 | */ |
| 283 | if (err) |
| 284 | dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n", |
| 285 | event, err); |
| 286 | |
| 287 | return notifier_from_errno(err); |
| 288 | } |
| 289 | |
| 290 | int dsa_switch_register_notifier(struct dsa_switch *ds) |
| 291 | { |
| 292 | ds->nb.notifier_call = dsa_switch_event; |
| 293 | |
| 294 | return raw_notifier_chain_register(&ds->dst->nh, &ds->nb); |
| 295 | } |
| 296 | |
| 297 | void dsa_switch_unregister_notifier(struct dsa_switch *ds) |
| 298 | { |
| 299 | int err; |
| 300 | |
| 301 | err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb); |
| 302 | if (err) |
| 303 | dev_err(ds->dev, "failed to unregister notifier (%d)\n", err); |
| 304 | } |