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