Linux 5.7.7
[linux/fpc-iii.git] / net / dsa / switch.c
blobf3c32ff552b35de163a235b29470952adb5f03ad
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 bool dsa_switch_mtu_match(struct dsa_switch *ds, int port,
56 struct dsa_notifier_mtu_info *info)
58 if (ds->index == info->sw_index)
59 return (port == info->port) || dsa_is_dsa_port(ds, port);
61 if (!info->propagate_upstream)
62 return false;
64 if (dsa_is_dsa_port(ds, port) || dsa_is_cpu_port(ds, port))
65 return true;
67 return false;
70 static int dsa_switch_mtu(struct dsa_switch *ds,
71 struct dsa_notifier_mtu_info *info)
73 int port, ret;
75 if (!ds->ops->port_change_mtu)
76 return -EOPNOTSUPP;
78 for (port = 0; port < ds->num_ports; port++) {
79 if (dsa_switch_mtu_match(ds, port, info)) {
80 ret = ds->ops->port_change_mtu(ds, port, info->mtu);
81 if (ret)
82 return ret;
86 return 0;
89 static int dsa_switch_bridge_join(struct dsa_switch *ds,
90 struct dsa_notifier_bridge_info *info)
92 if (ds->index == info->sw_index && ds->ops->port_bridge_join)
93 return ds->ops->port_bridge_join(ds, info->port, info->br);
95 if (ds->index != info->sw_index && ds->ops->crosschip_bridge_join)
96 return ds->ops->crosschip_bridge_join(ds, info->sw_index,
97 info->port, info->br);
99 return 0;
102 static int dsa_switch_bridge_leave(struct dsa_switch *ds,
103 struct dsa_notifier_bridge_info *info)
105 bool unset_vlan_filtering = br_vlan_enabled(info->br);
106 int err, i;
108 if (ds->index == info->sw_index && ds->ops->port_bridge_leave)
109 ds->ops->port_bridge_leave(ds, info->port, info->br);
111 if (ds->index != info->sw_index && ds->ops->crosschip_bridge_leave)
112 ds->ops->crosschip_bridge_leave(ds, info->sw_index, info->port,
113 info->br);
115 /* If the bridge was vlan_filtering, the bridge core doesn't trigger an
116 * event for changing vlan_filtering setting upon slave ports leaving
117 * it. That is a good thing, because that lets us handle it and also
118 * handle the case where the switch's vlan_filtering setting is global
119 * (not per port). When that happens, the correct moment to trigger the
120 * vlan_filtering callback is only when the last port left this bridge.
122 if (unset_vlan_filtering && ds->vlan_filtering_is_global) {
123 for (i = 0; i < ds->num_ports; i++) {
124 if (i == info->port)
125 continue;
126 if (dsa_to_port(ds, i)->bridge_dev == info->br) {
127 unset_vlan_filtering = false;
128 break;
132 if (unset_vlan_filtering) {
133 struct switchdev_trans trans = {0};
135 err = dsa_port_vlan_filtering(dsa_to_port(ds, info->port),
136 false, &trans);
137 if (err && err != EOPNOTSUPP)
138 return err;
140 return 0;
143 static int dsa_switch_fdb_add(struct dsa_switch *ds,
144 struct dsa_notifier_fdb_info *info)
146 int port = dsa_towards_port(ds, info->sw_index, info->port);
148 if (!ds->ops->port_fdb_add)
149 return -EOPNOTSUPP;
151 return ds->ops->port_fdb_add(ds, port, info->addr, info->vid);
154 static int dsa_switch_fdb_del(struct dsa_switch *ds,
155 struct dsa_notifier_fdb_info *info)
157 int port = dsa_towards_port(ds, info->sw_index, info->port);
159 if (!ds->ops->port_fdb_del)
160 return -EOPNOTSUPP;
162 return ds->ops->port_fdb_del(ds, port, info->addr, info->vid);
165 static bool dsa_switch_mdb_match(struct dsa_switch *ds, int port,
166 struct dsa_notifier_mdb_info *info)
168 if (ds->index == info->sw_index && port == info->port)
169 return true;
171 if (dsa_is_dsa_port(ds, port))
172 return true;
174 return false;
177 static int dsa_switch_mdb_prepare(struct dsa_switch *ds,
178 struct dsa_notifier_mdb_info *info)
180 int port, err;
182 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
183 return -EOPNOTSUPP;
185 for (port = 0; port < ds->num_ports; port++) {
186 if (dsa_switch_mdb_match(ds, port, info)) {
187 err = ds->ops->port_mdb_prepare(ds, port, info->mdb);
188 if (err)
189 return err;
193 return 0;
196 static int dsa_switch_mdb_add(struct dsa_switch *ds,
197 struct dsa_notifier_mdb_info *info)
199 int port;
201 if (switchdev_trans_ph_prepare(info->trans))
202 return dsa_switch_mdb_prepare(ds, info);
204 if (!ds->ops->port_mdb_add)
205 return 0;
207 for (port = 0; port < ds->num_ports; port++)
208 if (dsa_switch_mdb_match(ds, port, info))
209 ds->ops->port_mdb_add(ds, port, info->mdb);
211 return 0;
214 static int dsa_switch_mdb_del(struct dsa_switch *ds,
215 struct dsa_notifier_mdb_info *info)
217 if (!ds->ops->port_mdb_del)
218 return -EOPNOTSUPP;
220 if (ds->index == info->sw_index)
221 return ds->ops->port_mdb_del(ds, info->port, info->mdb);
223 return 0;
226 static int dsa_port_vlan_device_check(struct net_device *vlan_dev,
227 int vlan_dev_vid,
228 void *arg)
230 struct switchdev_obj_port_vlan *vlan = arg;
231 u16 vid;
233 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
234 if (vid == vlan_dev_vid)
235 return -EBUSY;
238 return 0;
241 static int dsa_port_vlan_check(struct dsa_switch *ds, int port,
242 const struct switchdev_obj_port_vlan *vlan)
244 const struct dsa_port *dp = dsa_to_port(ds, port);
245 int err = 0;
247 /* Device is not bridged, let it proceed with the VLAN device
248 * creation.
250 if (!dp->bridge_dev)
251 return err;
253 /* dsa_slave_vlan_rx_{add,kill}_vid() cannot use the prepare phase and
254 * already checks whether there is an overlapping bridge VLAN entry
255 * with the same VID, so here we only need to check that if we are
256 * adding a bridge VLAN entry there is not an overlapping VLAN device
257 * claiming that VID.
259 return vlan_for_each(dp->slave, dsa_port_vlan_device_check,
260 (void *)vlan);
263 static bool dsa_switch_vlan_match(struct dsa_switch *ds, int port,
264 struct dsa_notifier_vlan_info *info)
266 if (ds->index == info->sw_index && port == info->port)
267 return true;
269 if (dsa_is_dsa_port(ds, port))
270 return true;
272 return false;
275 static int dsa_switch_vlan_prepare(struct dsa_switch *ds,
276 struct dsa_notifier_vlan_info *info)
278 int port, err;
280 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
281 return -EOPNOTSUPP;
283 for (port = 0; port < ds->num_ports; port++) {
284 if (dsa_switch_vlan_match(ds, port, info)) {
285 err = dsa_port_vlan_check(ds, port, info->vlan);
286 if (err)
287 return err;
289 err = ds->ops->port_vlan_prepare(ds, port, info->vlan);
290 if (err)
291 return err;
295 return 0;
298 static int dsa_switch_vlan_add(struct dsa_switch *ds,
299 struct dsa_notifier_vlan_info *info)
301 int port;
303 if (switchdev_trans_ph_prepare(info->trans))
304 return dsa_switch_vlan_prepare(ds, info);
306 if (!ds->ops->port_vlan_add)
307 return 0;
309 for (port = 0; port < ds->num_ports; port++)
310 if (dsa_switch_vlan_match(ds, port, info))
311 ds->ops->port_vlan_add(ds, port, info->vlan);
313 return 0;
316 static int dsa_switch_vlan_del(struct dsa_switch *ds,
317 struct dsa_notifier_vlan_info *info)
319 if (!ds->ops->port_vlan_del)
320 return -EOPNOTSUPP;
322 if (ds->index == info->sw_index)
323 return ds->ops->port_vlan_del(ds, info->port, info->vlan);
325 /* Do not deprogram the DSA links as they may be used as conduit
326 * for other VLAN members in the fabric.
328 return 0;
331 static int dsa_switch_event(struct notifier_block *nb,
332 unsigned long event, void *info)
334 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
335 int err;
337 switch (event) {
338 case DSA_NOTIFIER_AGEING_TIME:
339 err = dsa_switch_ageing_time(ds, info);
340 break;
341 case DSA_NOTIFIER_BRIDGE_JOIN:
342 err = dsa_switch_bridge_join(ds, info);
343 break;
344 case DSA_NOTIFIER_BRIDGE_LEAVE:
345 err = dsa_switch_bridge_leave(ds, info);
346 break;
347 case DSA_NOTIFIER_FDB_ADD:
348 err = dsa_switch_fdb_add(ds, info);
349 break;
350 case DSA_NOTIFIER_FDB_DEL:
351 err = dsa_switch_fdb_del(ds, info);
352 break;
353 case DSA_NOTIFIER_MDB_ADD:
354 err = dsa_switch_mdb_add(ds, info);
355 break;
356 case DSA_NOTIFIER_MDB_DEL:
357 err = dsa_switch_mdb_del(ds, info);
358 break;
359 case DSA_NOTIFIER_VLAN_ADD:
360 err = dsa_switch_vlan_add(ds, info);
361 break;
362 case DSA_NOTIFIER_VLAN_DEL:
363 err = dsa_switch_vlan_del(ds, info);
364 break;
365 case DSA_NOTIFIER_MTU:
366 err = dsa_switch_mtu(ds, info);
367 break;
368 default:
369 err = -EOPNOTSUPP;
370 break;
373 /* Non-switchdev operations cannot be rolled back. If a DSA driver
374 * returns an error during the chained call, switch chips may be in an
375 * inconsistent state.
377 if (err)
378 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
379 event, err);
381 return notifier_from_errno(err);
384 int dsa_switch_register_notifier(struct dsa_switch *ds)
386 ds->nb.notifier_call = dsa_switch_event;
388 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
391 void dsa_switch_unregister_notifier(struct dsa_switch *ds)
393 int err;
395 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
396 if (err)
397 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);