dm writecache: fix incorrect flush sequence when doing SSD mode commit
[linux/fpc-iii.git] / net / dsa / switch.c
blobdf4abe897ed61cef1a06c354cfcedfcc071deb23
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Handling of a single switch chip, part of a switch fabric
5 * Copyright (c) 2017 Savoir-faire Linux Inc.
6 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
7 */
9 #include <linux/if_bridge.h>
10 #include <linux/netdevice.h>
11 #include <linux/notifier.h>
12 #include <linux/if_vlan.h>
13 #include <net/switchdev.h>
15 #include "dsa_priv.h"
17 static unsigned int dsa_switch_fastest_ageing_time(struct dsa_switch *ds,
18 unsigned int ageing_time)
20 int i;
22 for (i = 0; i < ds->num_ports; ++i) {
23 struct dsa_port *dp = dsa_to_port(ds, i);
25 if (dp->ageing_time && dp->ageing_time < ageing_time)
26 ageing_time = dp->ageing_time;
29 return ageing_time;
32 static int dsa_switch_ageing_time(struct dsa_switch *ds,
33 struct dsa_notifier_ageing_time_info *info)
35 unsigned int ageing_time = info->ageing_time;
36 struct switchdev_trans *trans = info->trans;
38 if (switchdev_trans_ph_prepare(trans)) {
39 if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
40 return -ERANGE;
41 if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
42 return -ERANGE;
43 return 0;
46 /* Program the fastest ageing time in case of multiple bridges */
47 ageing_time = dsa_switch_fastest_ageing_time(ds, ageing_time);
49 if (ds->ops->set_ageing_time)
50 return ds->ops->set_ageing_time(ds, ageing_time);
52 return 0;
55 static int dsa_switch_bridge_join(struct dsa_switch *ds,
56 struct dsa_notifier_bridge_info *info)
58 if (ds->index == info->sw_index && ds->ops->port_bridge_join)
59 return ds->ops->port_bridge_join(ds, info->port, info->br);
61 if (ds->index != info->sw_index && ds->ops->crosschip_bridge_join)
62 return ds->ops->crosschip_bridge_join(ds, info->sw_index,
63 info->port, info->br);
65 return 0;
68 static int dsa_switch_bridge_leave(struct dsa_switch *ds,
69 struct dsa_notifier_bridge_info *info)
71 bool unset_vlan_filtering = br_vlan_enabled(info->br);
72 int err, i;
74 if (ds->index == info->sw_index && ds->ops->port_bridge_leave)
75 ds->ops->port_bridge_leave(ds, info->port, info->br);
77 if (ds->index != info->sw_index && ds->ops->crosschip_bridge_leave)
78 ds->ops->crosschip_bridge_leave(ds, info->sw_index, info->port,
79 info->br);
81 /* If the bridge was vlan_filtering, the bridge core doesn't trigger an
82 * event for changing vlan_filtering setting upon slave ports leaving
83 * it. That is a good thing, because that lets us handle it and also
84 * handle the case where the switch's vlan_filtering setting is global
85 * (not per port). When that happens, the correct moment to trigger the
86 * vlan_filtering callback is only when the last port left this bridge.
88 if (unset_vlan_filtering && ds->vlan_filtering_is_global) {
89 for (i = 0; i < ds->num_ports; i++) {
90 if (i == info->port)
91 continue;
92 if (dsa_to_port(ds, i)->bridge_dev == info->br) {
93 unset_vlan_filtering = false;
94 break;
98 if (unset_vlan_filtering) {
99 struct switchdev_trans trans = {0};
101 err = dsa_port_vlan_filtering(dsa_to_port(ds, info->port),
102 false, &trans);
103 if (err && err != EOPNOTSUPP)
104 return err;
106 return 0;
109 static int dsa_switch_fdb_add(struct dsa_switch *ds,
110 struct dsa_notifier_fdb_info *info)
112 int port = dsa_towards_port(ds, info->sw_index, info->port);
114 if (!ds->ops->port_fdb_add)
115 return -EOPNOTSUPP;
117 return ds->ops->port_fdb_add(ds, port, info->addr, info->vid);
120 static int dsa_switch_fdb_del(struct dsa_switch *ds,
121 struct dsa_notifier_fdb_info *info)
123 int port = dsa_towards_port(ds, info->sw_index, info->port);
125 if (!ds->ops->port_fdb_del)
126 return -EOPNOTSUPP;
128 return ds->ops->port_fdb_del(ds, port, info->addr, info->vid);
131 static bool dsa_switch_mdb_match(struct dsa_switch *ds, int port,
132 struct dsa_notifier_mdb_info *info)
134 if (ds->index == info->sw_index && port == info->port)
135 return true;
137 if (dsa_is_dsa_port(ds, port))
138 return true;
140 return false;
143 static int dsa_switch_mdb_prepare(struct dsa_switch *ds,
144 struct dsa_notifier_mdb_info *info)
146 int port, err;
148 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
149 return -EOPNOTSUPP;
151 for (port = 0; port < ds->num_ports; port++) {
152 if (dsa_switch_mdb_match(ds, port, info)) {
153 err = ds->ops->port_mdb_prepare(ds, port, info->mdb);
154 if (err)
155 return err;
159 return 0;
162 static int dsa_switch_mdb_add(struct dsa_switch *ds,
163 struct dsa_notifier_mdb_info *info)
165 int port;
167 if (switchdev_trans_ph_prepare(info->trans))
168 return dsa_switch_mdb_prepare(ds, info);
170 if (!ds->ops->port_mdb_add)
171 return 0;
173 for (port = 0; port < ds->num_ports; port++)
174 if (dsa_switch_mdb_match(ds, port, info))
175 ds->ops->port_mdb_add(ds, port, info->mdb);
177 return 0;
180 static int dsa_switch_mdb_del(struct dsa_switch *ds,
181 struct dsa_notifier_mdb_info *info)
183 if (!ds->ops->port_mdb_del)
184 return -EOPNOTSUPP;
186 if (ds->index == info->sw_index)
187 return ds->ops->port_mdb_del(ds, info->port, info->mdb);
189 return 0;
192 static int dsa_port_vlan_device_check(struct net_device *vlan_dev,
193 int vlan_dev_vid,
194 void *arg)
196 struct switchdev_obj_port_vlan *vlan = arg;
197 u16 vid;
199 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
200 if (vid == vlan_dev_vid)
201 return -EBUSY;
204 return 0;
207 static int dsa_port_vlan_check(struct dsa_switch *ds, int port,
208 const struct switchdev_obj_port_vlan *vlan)
210 const struct dsa_port *dp = dsa_to_port(ds, port);
211 int err = 0;
213 /* Device is not bridged, let it proceed with the VLAN device
214 * creation.
216 if (!dp->bridge_dev)
217 return err;
219 /* dsa_slave_vlan_rx_{add,kill}_vid() cannot use the prepare phase and
220 * already checks whether there is an overlapping bridge VLAN entry
221 * with the same VID, so here we only need to check that if we are
222 * adding a bridge VLAN entry there is not an overlapping VLAN device
223 * claiming that VID.
225 return vlan_for_each(dp->slave, dsa_port_vlan_device_check,
226 (void *)vlan);
229 static bool dsa_switch_vlan_match(struct dsa_switch *ds, int port,
230 struct dsa_notifier_vlan_info *info)
232 if (ds->index == info->sw_index && port == info->port)
233 return true;
235 if (dsa_is_dsa_port(ds, port))
236 return true;
238 return false;
241 static int dsa_switch_vlan_prepare(struct dsa_switch *ds,
242 struct dsa_notifier_vlan_info *info)
244 int port, err;
246 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
247 return -EOPNOTSUPP;
249 for (port = 0; port < ds->num_ports; port++) {
250 if (dsa_switch_vlan_match(ds, port, info)) {
251 err = dsa_port_vlan_check(ds, port, info->vlan);
252 if (err)
253 return err;
255 err = ds->ops->port_vlan_prepare(ds, port, info->vlan);
256 if (err)
257 return err;
261 return 0;
264 static int dsa_switch_vlan_add(struct dsa_switch *ds,
265 struct dsa_notifier_vlan_info *info)
267 int port;
269 if (switchdev_trans_ph_prepare(info->trans))
270 return dsa_switch_vlan_prepare(ds, info);
272 if (!ds->ops->port_vlan_add)
273 return 0;
275 for (port = 0; port < ds->num_ports; port++)
276 if (dsa_switch_vlan_match(ds, port, info))
277 ds->ops->port_vlan_add(ds, port, info->vlan);
279 return 0;
282 static int dsa_switch_vlan_del(struct dsa_switch *ds,
283 struct dsa_notifier_vlan_info *info)
285 if (!ds->ops->port_vlan_del)
286 return -EOPNOTSUPP;
288 if (ds->index == info->sw_index)
289 return ds->ops->port_vlan_del(ds, info->port, info->vlan);
291 /* Do not deprogram the DSA links as they may be used as conduit
292 * for other VLAN members in the fabric.
294 return 0;
297 static int dsa_switch_event(struct notifier_block *nb,
298 unsigned long event, void *info)
300 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
301 int err;
303 switch (event) {
304 case DSA_NOTIFIER_AGEING_TIME:
305 err = dsa_switch_ageing_time(ds, info);
306 break;
307 case DSA_NOTIFIER_BRIDGE_JOIN:
308 err = dsa_switch_bridge_join(ds, info);
309 break;
310 case DSA_NOTIFIER_BRIDGE_LEAVE:
311 err = dsa_switch_bridge_leave(ds, info);
312 break;
313 case DSA_NOTIFIER_FDB_ADD:
314 err = dsa_switch_fdb_add(ds, info);
315 break;
316 case DSA_NOTIFIER_FDB_DEL:
317 err = dsa_switch_fdb_del(ds, info);
318 break;
319 case DSA_NOTIFIER_MDB_ADD:
320 err = dsa_switch_mdb_add(ds, info);
321 break;
322 case DSA_NOTIFIER_MDB_DEL:
323 err = dsa_switch_mdb_del(ds, info);
324 break;
325 case DSA_NOTIFIER_VLAN_ADD:
326 err = dsa_switch_vlan_add(ds, info);
327 break;
328 case DSA_NOTIFIER_VLAN_DEL:
329 err = dsa_switch_vlan_del(ds, info);
330 break;
331 default:
332 err = -EOPNOTSUPP;
333 break;
336 /* Non-switchdev operations cannot be rolled back. If a DSA driver
337 * returns an error during the chained call, switch chips may be in an
338 * inconsistent state.
340 if (err)
341 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
342 event, err);
344 return notifier_from_errno(err);
347 int dsa_switch_register_notifier(struct dsa_switch *ds)
349 ds->nb.notifier_call = dsa_switch_event;
351 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
354 void dsa_switch_unregister_notifier(struct dsa_switch *ds)
356 int err;
358 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
359 if (err)
360 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);