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 | { |
| 86 | const struct switchdev_obj_port_fdb *fdb = info->fdb; |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 87 | |
| 88 | /* Do not care yet about other switch chips of the fabric */ |
| 89 | if (ds->index != info->sw_index) |
| 90 | return 0; |
| 91 | |
Arkadi Sharshevsky | 1b6dd55 | 2017-08-06 16:15:40 +0300 | [diff] [blame^] | 92 | if (!ds->ops->port_fdb_add) |
| 93 | return -EOPNOTSUPP; |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 94 | |
Arkadi Sharshevsky | 1b6dd55 | 2017-08-06 16:15:40 +0300 | [diff] [blame^] | 95 | return ds->ops->port_fdb_add(ds, info->port, fdb->addr, fdb->vid); |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static int dsa_switch_fdb_del(struct dsa_switch *ds, |
| 99 | struct dsa_notifier_fdb_info *info) |
| 100 | { |
| 101 | const struct switchdev_obj_port_fdb *fdb = info->fdb; |
| 102 | |
| 103 | /* Do not care yet about other switch chips of the fabric */ |
| 104 | if (ds->index != info->sw_index) |
| 105 | return 0; |
| 106 | |
| 107 | if (!ds->ops->port_fdb_del) |
| 108 | return -EOPNOTSUPP; |
| 109 | |
Arkadi Sharshevsky | 6c2c1dc | 2017-08-06 16:15:39 +0300 | [diff] [blame] | 110 | return ds->ops->port_fdb_del(ds, info->port, fdb->addr, |
| 111 | fdb->vid); |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 112 | } |
| 113 | |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 114 | static int dsa_switch_mdb_add(struct dsa_switch *ds, |
| 115 | struct dsa_notifier_mdb_info *info) |
| 116 | { |
| 117 | const struct switchdev_obj_port_mdb *mdb = info->mdb; |
| 118 | struct switchdev_trans *trans = info->trans; |
Vivien Didelot | a1a6b7e | 2017-06-15 16:14:48 -0400 | [diff] [blame] | 119 | DECLARE_BITMAP(group, ds->num_ports); |
| 120 | int port, err; |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 121 | |
Vivien Didelot | a1a6b7e | 2017-06-15 16:14:48 -0400 | [diff] [blame] | 122 | /* Build a mask of Multicast group members */ |
| 123 | bitmap_zero(group, ds->num_ports); |
| 124 | if (ds->index == info->sw_index) |
| 125 | set_bit(info->port, group); |
| 126 | for (port = 0; port < ds->num_ports; port++) |
| 127 | if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) |
| 128 | set_bit(port, group); |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 129 | |
| 130 | if (switchdev_trans_ph_prepare(trans)) { |
| 131 | if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add) |
| 132 | return -EOPNOTSUPP; |
| 133 | |
Vivien Didelot | a1a6b7e | 2017-06-15 16:14:48 -0400 | [diff] [blame] | 134 | for_each_set_bit(port, group, ds->num_ports) { |
| 135 | err = ds->ops->port_mdb_prepare(ds, port, mdb, trans); |
| 136 | if (err) |
| 137 | return err; |
| 138 | } |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 139 | } |
| 140 | |
Vivien Didelot | a1a6b7e | 2017-06-15 16:14:48 -0400 | [diff] [blame] | 141 | for_each_set_bit(port, group, ds->num_ports) |
| 142 | ds->ops->port_mdb_add(ds, port, mdb, trans); |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | static int dsa_switch_mdb_del(struct dsa_switch *ds, |
| 148 | struct dsa_notifier_mdb_info *info) |
| 149 | { |
| 150 | const struct switchdev_obj_port_mdb *mdb = info->mdb; |
| 151 | |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 152 | if (!ds->ops->port_mdb_del) |
| 153 | return -EOPNOTSUPP; |
| 154 | |
Vivien Didelot | a1a6b7e | 2017-06-15 16:14:48 -0400 | [diff] [blame] | 155 | if (ds->index == info->sw_index) |
| 156 | return ds->ops->port_mdb_del(ds, info->port, mdb); |
| 157 | |
| 158 | return 0; |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 159 | } |
| 160 | |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 161 | static int dsa_switch_vlan_add(struct dsa_switch *ds, |
| 162 | struct dsa_notifier_vlan_info *info) |
| 163 | { |
| 164 | const struct switchdev_obj_port_vlan *vlan = info->vlan; |
| 165 | struct switchdev_trans *trans = info->trans; |
Vivien Didelot | 1ca4aa9 | 2017-06-07 18:12:14 -0400 | [diff] [blame] | 166 | DECLARE_BITMAP(members, ds->num_ports); |
| 167 | int port, err; |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 168 | |
Vivien Didelot | 1ca4aa9 | 2017-06-07 18:12:14 -0400 | [diff] [blame] | 169 | /* Build a mask of VLAN members */ |
| 170 | bitmap_zero(members, ds->num_ports); |
| 171 | if (ds->index == info->sw_index) |
| 172 | set_bit(info->port, members); |
Vivien Didelot | b2f81d3 | 2017-06-07 18:12:15 -0400 | [diff] [blame] | 173 | for (port = 0; port < ds->num_ports; port++) |
| 174 | if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) |
| 175 | set_bit(port, members); |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 176 | |
| 177 | if (switchdev_trans_ph_prepare(trans)) { |
| 178 | if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add) |
| 179 | return -EOPNOTSUPP; |
| 180 | |
Vivien Didelot | 1ca4aa9 | 2017-06-07 18:12:14 -0400 | [diff] [blame] | 181 | for_each_set_bit(port, members, ds->num_ports) { |
| 182 | err = ds->ops->port_vlan_prepare(ds, port, vlan, trans); |
| 183 | if (err) |
| 184 | return err; |
| 185 | } |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 186 | } |
| 187 | |
Vivien Didelot | 1ca4aa9 | 2017-06-07 18:12:14 -0400 | [diff] [blame] | 188 | for_each_set_bit(port, members, ds->num_ports) |
| 189 | ds->ops->port_vlan_add(ds, port, vlan, trans); |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static int dsa_switch_vlan_del(struct dsa_switch *ds, |
| 195 | struct dsa_notifier_vlan_info *info) |
| 196 | { |
| 197 | const struct switchdev_obj_port_vlan *vlan = info->vlan; |
| 198 | |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 199 | if (!ds->ops->port_vlan_del) |
| 200 | return -EOPNOTSUPP; |
| 201 | |
Vivien Didelot | 1ca4aa9 | 2017-06-07 18:12:14 -0400 | [diff] [blame] | 202 | if (ds->index == info->sw_index) |
| 203 | return ds->ops->port_vlan_del(ds, info->port, vlan); |
| 204 | |
| 205 | return 0; |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 206 | } |
| 207 | |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 208 | static int dsa_switch_event(struct notifier_block *nb, |
| 209 | unsigned long event, void *info) |
| 210 | { |
| 211 | struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb); |
| 212 | int err; |
| 213 | |
| 214 | switch (event) { |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 215 | case DSA_NOTIFIER_AGEING_TIME: |
| 216 | err = dsa_switch_ageing_time(ds, info); |
| 217 | break; |
Vivien Didelot | 04d3a4c | 2017-02-03 13:20:21 -0500 | [diff] [blame] | 218 | case DSA_NOTIFIER_BRIDGE_JOIN: |
| 219 | err = dsa_switch_bridge_join(ds, info); |
| 220 | break; |
| 221 | case DSA_NOTIFIER_BRIDGE_LEAVE: |
| 222 | err = dsa_switch_bridge_leave(ds, info); |
| 223 | break; |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 224 | case DSA_NOTIFIER_FDB_ADD: |
| 225 | err = dsa_switch_fdb_add(ds, info); |
| 226 | break; |
| 227 | case DSA_NOTIFIER_FDB_DEL: |
| 228 | err = dsa_switch_fdb_del(ds, info); |
| 229 | break; |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 230 | case DSA_NOTIFIER_MDB_ADD: |
| 231 | err = dsa_switch_mdb_add(ds, info); |
| 232 | break; |
| 233 | case DSA_NOTIFIER_MDB_DEL: |
| 234 | err = dsa_switch_mdb_del(ds, info); |
| 235 | break; |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 236 | case DSA_NOTIFIER_VLAN_ADD: |
| 237 | err = dsa_switch_vlan_add(ds, info); |
| 238 | break; |
| 239 | case DSA_NOTIFIER_VLAN_DEL: |
| 240 | err = dsa_switch_vlan_del(ds, info); |
| 241 | break; |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 242 | default: |
| 243 | err = -EOPNOTSUPP; |
| 244 | break; |
| 245 | } |
| 246 | |
| 247 | /* Non-switchdev operations cannot be rolled back. If a DSA driver |
| 248 | * returns an error during the chained call, switch chips may be in an |
| 249 | * inconsistent state. |
| 250 | */ |
| 251 | if (err) |
| 252 | dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n", |
| 253 | event, err); |
| 254 | |
| 255 | return notifier_from_errno(err); |
| 256 | } |
| 257 | |
| 258 | int dsa_switch_register_notifier(struct dsa_switch *ds) |
| 259 | { |
| 260 | ds->nb.notifier_call = dsa_switch_event; |
| 261 | |
| 262 | return raw_notifier_chain_register(&ds->dst->nh, &ds->nb); |
| 263 | } |
| 264 | |
| 265 | void dsa_switch_unregister_notifier(struct dsa_switch *ds) |
| 266 | { |
| 267 | int err; |
| 268 | |
| 269 | err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb); |
| 270 | if (err) |
| 271 | dev_err(ds->dev, "failed to unregister notifier (%d)\n", err); |
| 272 | } |