Vivien Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Handling of a single switch port |
| 3 | * |
| 4 | * Copyright (c) 2017 Savoir-faire Linux Inc. |
| 5 | * Vivien Didelot <vivien.didelot@savoirfairelinux.com> |
| 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/if_bridge.h> |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 14 | #include <linux/notifier.h> |
Vivien Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 15 | |
| 16 | #include "dsa_priv.h" |
| 17 | |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 18 | static int dsa_port_notify(struct dsa_port *dp, unsigned long e, void *v) |
| 19 | { |
| 20 | struct raw_notifier_head *nh = &dp->ds->dst->nh; |
| 21 | int err; |
| 22 | |
| 23 | err = raw_notifier_call_chain(nh, e, v); |
| 24 | |
| 25 | return notifier_to_errno(err); |
| 26 | } |
| 27 | |
Vivien Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 28 | int dsa_port_set_state(struct dsa_port *dp, u8 state, |
| 29 | struct switchdev_trans *trans) |
| 30 | { |
| 31 | struct dsa_switch *ds = dp->ds; |
| 32 | int port = dp->index; |
| 33 | |
| 34 | if (switchdev_trans_ph_prepare(trans)) |
| 35 | return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP; |
| 36 | |
| 37 | if (ds->ops->port_stp_state_set) |
| 38 | ds->ops->port_stp_state_set(ds, port, state); |
| 39 | |
| 40 | if (ds->ops->port_fast_age) { |
| 41 | /* Fast age FDB entries or flush appropriate forwarding database |
| 42 | * for the given port, if we are moving it from Learning or |
| 43 | * Forwarding state, to Disabled or Blocking or Listening state. |
| 44 | */ |
| 45 | |
| 46 | if ((dp->stp_state == BR_STATE_LEARNING || |
| 47 | dp->stp_state == BR_STATE_FORWARDING) && |
| 48 | (state == BR_STATE_DISABLED || |
| 49 | state == BR_STATE_BLOCKING || |
| 50 | state == BR_STATE_LISTENING)) |
| 51 | ds->ops->port_fast_age(ds, port); |
| 52 | } |
| 53 | |
| 54 | dp->stp_state = state; |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame^] | 59 | static void dsa_port_set_state_now(struct dsa_port *dp, u8 state) |
Vivien Didelot | a40c175 | 2017-05-19 17:00:44 -0400 | [diff] [blame] | 60 | { |
| 61 | int err; |
| 62 | |
| 63 | err = dsa_port_set_state(dp, state, NULL); |
| 64 | if (err) |
| 65 | pr_err("DSA: failed to set STP state %u (%d)\n", state, err); |
| 66 | } |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 67 | |
Vivien Didelot | fb8a6a2 | 2017-09-22 19:01:56 -0400 | [diff] [blame^] | 68 | int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy) |
| 69 | { |
| 70 | u8 stp_state = dp->bridge_dev ? BR_STATE_BLOCKING : BR_STATE_FORWARDING; |
| 71 | struct dsa_switch *ds = dp->ds; |
| 72 | int port = dp->index; |
| 73 | int err; |
| 74 | |
| 75 | if (ds->ops->port_enable) { |
| 76 | err = ds->ops->port_enable(ds, port, phy); |
| 77 | if (err) |
| 78 | return err; |
| 79 | } |
| 80 | |
| 81 | dsa_port_set_state_now(dp, stp_state); |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | void dsa_port_disable(struct dsa_port *dp, struct phy_device *phy) |
| 87 | { |
| 88 | struct dsa_switch *ds = dp->ds; |
| 89 | int port = dp->index; |
| 90 | |
| 91 | dsa_port_set_state_now(dp, BR_STATE_DISABLED); |
| 92 | |
| 93 | if (ds->ops->port_disable) |
| 94 | ds->ops->port_disable(ds, port, phy); |
| 95 | } |
| 96 | |
Vivien Didelot | cfbed32 | 2017-05-19 17:00:45 -0400 | [diff] [blame] | 97 | int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br) |
| 98 | { |
| 99 | struct dsa_notifier_bridge_info info = { |
| 100 | .sw_index = dp->ds->index, |
| 101 | .port = dp->index, |
| 102 | .br = br, |
| 103 | }; |
| 104 | int err; |
| 105 | |
| 106 | /* Here the port is already bridged. Reflect the current configuration |
| 107 | * so that drivers can program their chips accordingly. |
| 108 | */ |
| 109 | dp->bridge_dev = br; |
| 110 | |
| 111 | err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_JOIN, &info); |
| 112 | |
| 113 | /* The bridging is rolled back on error */ |
| 114 | if (err) |
| 115 | dp->bridge_dev = NULL; |
| 116 | |
| 117 | return err; |
| 118 | } |
| 119 | |
| 120 | void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br) |
| 121 | { |
| 122 | struct dsa_notifier_bridge_info info = { |
| 123 | .sw_index = dp->ds->index, |
| 124 | .port = dp->index, |
| 125 | .br = br, |
| 126 | }; |
| 127 | int err; |
| 128 | |
| 129 | /* Here the port is already unbridged. Reflect the current configuration |
| 130 | * so that drivers can program their chips accordingly. |
| 131 | */ |
| 132 | dp->bridge_dev = NULL; |
| 133 | |
| 134 | err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_LEAVE, &info); |
| 135 | if (err) |
| 136 | pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n"); |
| 137 | |
| 138 | /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer, |
| 139 | * so allow it to be in BR_STATE_FORWARDING to be kept functional |
| 140 | */ |
| 141 | dsa_port_set_state_now(dp, BR_STATE_FORWARDING); |
| 142 | } |
Vivien Didelot | 4d61d30 | 2017-05-19 17:00:46 -0400 | [diff] [blame] | 143 | |
| 144 | int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering, |
| 145 | struct switchdev_trans *trans) |
| 146 | { |
| 147 | struct dsa_switch *ds = dp->ds; |
| 148 | |
| 149 | /* bridge skips -EOPNOTSUPP, so skip the prepare phase */ |
| 150 | if (switchdev_trans_ph_prepare(trans)) |
| 151 | return 0; |
| 152 | |
| 153 | if (ds->ops->port_vlan_filtering) |
| 154 | return ds->ops->port_vlan_filtering(ds, dp->index, |
| 155 | vlan_filtering); |
| 156 | |
| 157 | return 0; |
| 158 | } |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 159 | |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 160 | int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock, |
| 161 | struct switchdev_trans *trans) |
| 162 | { |
| 163 | unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock); |
| 164 | unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies); |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 165 | struct dsa_notifier_ageing_time_info info = { |
| 166 | .ageing_time = ageing_time, |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 167 | .trans = trans, |
| 168 | }; |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 169 | |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 170 | if (switchdev_trans_ph_prepare(trans)) |
| 171 | return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info); |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 172 | |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 173 | dp->ageing_time = ageing_time; |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 174 | |
Vivien Didelot | 1faabf7 | 2017-05-19 17:00:52 -0400 | [diff] [blame] | 175 | return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info); |
Vivien Didelot | d87bd94 | 2017-05-19 17:00:47 -0400 | [diff] [blame] | 176 | } |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 177 | |
Arkadi Sharshevsky | 2acf4e6 | 2017-08-06 16:15:41 +0300 | [diff] [blame] | 178 | int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr, |
| 179 | u16 vid) |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 180 | { |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 181 | struct dsa_notifier_fdb_info info = { |
| 182 | .sw_index = dp->ds->index, |
| 183 | .port = dp->index, |
Arkadi Sharshevsky | 2acf4e6 | 2017-08-06 16:15:41 +0300 | [diff] [blame] | 184 | .addr = addr, |
| 185 | .vid = vid, |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 186 | }; |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 187 | |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 188 | return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info); |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 189 | } |
| 190 | |
Arkadi Sharshevsky | 2acf4e6 | 2017-08-06 16:15:41 +0300 | [diff] [blame] | 191 | int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr, |
| 192 | u16 vid) |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 193 | { |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 194 | struct dsa_notifier_fdb_info info = { |
| 195 | .sw_index = dp->ds->index, |
| 196 | .port = dp->index, |
Arkadi Sharshevsky | 2acf4e6 | 2017-08-06 16:15:41 +0300 | [diff] [blame] | 197 | .addr = addr, |
| 198 | .vid = vid, |
| 199 | |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 200 | }; |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 201 | |
Vivien Didelot | 685fb6a | 2017-05-19 17:00:53 -0400 | [diff] [blame] | 202 | return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info); |
Vivien Didelot | d1cffff | 2017-05-19 17:00:48 -0400 | [diff] [blame] | 203 | } |
| 204 | |
Vivien Didelot | de40fc5 | 2017-09-20 19:32:14 -0400 | [diff] [blame] | 205 | int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data) |
| 206 | { |
| 207 | struct dsa_switch *ds = dp->ds; |
| 208 | int port = dp->index; |
| 209 | |
| 210 | if (!ds->ops->port_fdb_dump) |
| 211 | return -EOPNOTSUPP; |
| 212 | |
| 213 | return ds->ops->port_fdb_dump(ds, port, cb, data); |
| 214 | } |
| 215 | |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 216 | int dsa_port_mdb_add(struct dsa_port *dp, |
| 217 | const struct switchdev_obj_port_mdb *mdb, |
| 218 | struct switchdev_trans *trans) |
| 219 | { |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 220 | struct dsa_notifier_mdb_info info = { |
| 221 | .sw_index = dp->ds->index, |
| 222 | .port = dp->index, |
| 223 | .trans = trans, |
| 224 | .mdb = mdb, |
| 225 | }; |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 226 | |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 227 | return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info); |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | int dsa_port_mdb_del(struct dsa_port *dp, |
| 231 | const struct switchdev_obj_port_mdb *mdb) |
| 232 | { |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 233 | struct dsa_notifier_mdb_info info = { |
| 234 | .sw_index = dp->ds->index, |
| 235 | .port = dp->index, |
| 236 | .mdb = mdb, |
| 237 | }; |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 238 | |
Vivien Didelot | 8ae5bcd | 2017-05-19 17:00:54 -0400 | [diff] [blame] | 239 | return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info); |
Vivien Didelot | 3a9afea | 2017-05-19 17:00:49 -0400 | [diff] [blame] | 240 | } |
| 241 | |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 242 | int dsa_port_vlan_add(struct dsa_port *dp, |
| 243 | const struct switchdev_obj_port_vlan *vlan, |
| 244 | struct switchdev_trans *trans) |
| 245 | { |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 246 | struct dsa_notifier_vlan_info info = { |
| 247 | .sw_index = dp->ds->index, |
| 248 | .port = dp->index, |
| 249 | .trans = trans, |
| 250 | .vlan = vlan, |
| 251 | }; |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 252 | |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 253 | return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info); |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | int dsa_port_vlan_del(struct dsa_port *dp, |
| 257 | const struct switchdev_obj_port_vlan *vlan) |
| 258 | { |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 259 | struct dsa_notifier_vlan_info info = { |
| 260 | .sw_index = dp->ds->index, |
| 261 | .port = dp->index, |
| 262 | .vlan = vlan, |
| 263 | }; |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 264 | |
Vivien Didelot | d0c627b | 2017-05-19 17:00:55 -0400 | [diff] [blame] | 265 | return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info); |
Vivien Didelot | 076e713 | 2017-05-19 17:00:50 -0400 | [diff] [blame] | 266 | } |