1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Handling of a single switch chip, part of a switch fabric
5 * Copyright (c) 2017 Savoir-faire Linux Inc.
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>
17 static unsigned int dsa_switch_fastest_ageing_time(struct dsa_switch *ds,
18 unsigned int ageing_time)
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;
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)
41 if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
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);
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)
64 if (dsa_is_dsa_port(ds, port) || dsa_is_cpu_port(ds, port))
70 static int dsa_switch_mtu(struct dsa_switch *ds,
71 struct dsa_notifier_mtu_info *info)
75 if (!ds->ops->port_change_mtu)
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);
89 static int dsa_switch_bridge_join(struct dsa_switch *ds,
90 struct dsa_notifier_bridge_info *info)
92 struct dsa_switch_tree *dst = ds->dst;
94 if (dst->index == info->tree_index && ds->index == info->sw_index &&
95 ds->ops->port_bridge_join)
96 return ds->ops->port_bridge_join(ds, info->port, info->br);
98 if ((dst->index != info->tree_index || ds->index != info->sw_index) &&
99 ds->ops->crosschip_bridge_join)
100 return ds->ops->crosschip_bridge_join(ds, info->tree_index,
102 info->port, info->br);
107 static int dsa_switch_bridge_leave(struct dsa_switch *ds,
108 struct dsa_notifier_bridge_info *info)
110 bool unset_vlan_filtering = br_vlan_enabled(info->br);
111 struct dsa_switch_tree *dst = ds->dst;
114 if (dst->index == info->tree_index && ds->index == info->sw_index &&
115 ds->ops->port_bridge_join)
116 ds->ops->port_bridge_leave(ds, info->port, info->br);
118 if ((dst->index != info->tree_index || ds->index != info->sw_index) &&
119 ds->ops->crosschip_bridge_join)
120 ds->ops->crosschip_bridge_leave(ds, info->tree_index,
121 info->sw_index, info->port,
124 /* If the bridge was vlan_filtering, the bridge core doesn't trigger an
125 * event for changing vlan_filtering setting upon slave ports leaving
126 * it. That is a good thing, because that lets us handle it and also
127 * handle the case where the switch's vlan_filtering setting is global
128 * (not per port). When that happens, the correct moment to trigger the
129 * vlan_filtering callback is only when the last port left this bridge.
131 if (unset_vlan_filtering && ds->vlan_filtering_is_global) {
132 for (i = 0; i < ds->num_ports; i++) {
135 if (dsa_to_port(ds, i)->bridge_dev == info->br) {
136 unset_vlan_filtering = false;
141 if (unset_vlan_filtering) {
142 struct switchdev_trans trans = {0};
144 err = dsa_port_vlan_filtering(dsa_to_port(ds, info->port),
146 if (err && err != EOPNOTSUPP)
152 static int dsa_switch_fdb_add(struct dsa_switch *ds,
153 struct dsa_notifier_fdb_info *info)
155 int port = dsa_towards_port(ds, info->sw_index, info->port);
157 if (!ds->ops->port_fdb_add)
160 return ds->ops->port_fdb_add(ds, port, info->addr, info->vid);
163 static int dsa_switch_fdb_del(struct dsa_switch *ds,
164 struct dsa_notifier_fdb_info *info)
166 int port = dsa_towards_port(ds, info->sw_index, info->port);
168 if (!ds->ops->port_fdb_del)
171 return ds->ops->port_fdb_del(ds, port, info->addr, info->vid);
174 static bool dsa_switch_mdb_match(struct dsa_switch *ds, int port,
175 struct dsa_notifier_mdb_info *info)
177 if (ds->index == info->sw_index && port == info->port)
180 if (dsa_is_dsa_port(ds, port))
186 static int dsa_switch_mdb_prepare(struct dsa_switch *ds,
187 struct dsa_notifier_mdb_info *info)
191 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
194 for (port = 0; port < ds->num_ports; port++) {
195 if (dsa_switch_mdb_match(ds, port, info)) {
196 err = ds->ops->port_mdb_prepare(ds, port, info->mdb);
205 static int dsa_switch_mdb_add(struct dsa_switch *ds,
206 struct dsa_notifier_mdb_info *info)
210 if (switchdev_trans_ph_prepare(info->trans))
211 return dsa_switch_mdb_prepare(ds, info);
213 if (!ds->ops->port_mdb_add)
216 for (port = 0; port < ds->num_ports; port++)
217 if (dsa_switch_mdb_match(ds, port, info))
218 ds->ops->port_mdb_add(ds, port, info->mdb);
223 static int dsa_switch_mdb_del(struct dsa_switch *ds,
224 struct dsa_notifier_mdb_info *info)
226 if (!ds->ops->port_mdb_del)
229 if (ds->index == info->sw_index)
230 return ds->ops->port_mdb_del(ds, info->port, info->mdb);
235 static int dsa_port_vlan_device_check(struct net_device *vlan_dev,
239 struct switchdev_obj_port_vlan *vlan = arg;
242 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
243 if (vid == vlan_dev_vid)
250 static int dsa_port_vlan_check(struct dsa_switch *ds, int port,
251 const struct switchdev_obj_port_vlan *vlan)
253 const struct dsa_port *dp = dsa_to_port(ds, port);
256 /* Device is not bridged, let it proceed with the VLAN device
262 /* dsa_slave_vlan_rx_{add,kill}_vid() cannot use the prepare phase and
263 * already checks whether there is an overlapping bridge VLAN entry
264 * with the same VID, so here we only need to check that if we are
265 * adding a bridge VLAN entry there is not an overlapping VLAN device
268 return vlan_for_each(dp->slave, dsa_port_vlan_device_check,
272 static bool dsa_switch_vlan_match(struct dsa_switch *ds, int port,
273 struct dsa_notifier_vlan_info *info)
275 if (ds->index == info->sw_index && port == info->port)
278 if (dsa_is_dsa_port(ds, port))
284 static int dsa_switch_vlan_prepare(struct dsa_switch *ds,
285 struct dsa_notifier_vlan_info *info)
289 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
292 for (port = 0; port < ds->num_ports; port++) {
293 if (dsa_switch_vlan_match(ds, port, info)) {
294 err = dsa_port_vlan_check(ds, port, info->vlan);
298 err = ds->ops->port_vlan_prepare(ds, port, info->vlan);
307 static int dsa_switch_vlan_add(struct dsa_switch *ds,
308 struct dsa_notifier_vlan_info *info)
312 if (switchdev_trans_ph_prepare(info->trans))
313 return dsa_switch_vlan_prepare(ds, info);
315 if (!ds->ops->port_vlan_add)
318 for (port = 0; port < ds->num_ports; port++)
319 if (dsa_switch_vlan_match(ds, port, info))
320 ds->ops->port_vlan_add(ds, port, info->vlan);
325 static int dsa_switch_vlan_del(struct dsa_switch *ds,
326 struct dsa_notifier_vlan_info *info)
328 if (!ds->ops->port_vlan_del)
331 if (ds->index == info->sw_index)
332 return ds->ops->port_vlan_del(ds, info->port, info->vlan);
334 /* Do not deprogram the DSA links as they may be used as conduit
335 * for other VLAN members in the fabric.
340 static int dsa_switch_event(struct notifier_block *nb,
341 unsigned long event, void *info)
343 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
347 case DSA_NOTIFIER_AGEING_TIME:
348 err = dsa_switch_ageing_time(ds, info);
350 case DSA_NOTIFIER_BRIDGE_JOIN:
351 err = dsa_switch_bridge_join(ds, info);
353 case DSA_NOTIFIER_BRIDGE_LEAVE:
354 err = dsa_switch_bridge_leave(ds, info);
356 case DSA_NOTIFIER_FDB_ADD:
357 err = dsa_switch_fdb_add(ds, info);
359 case DSA_NOTIFIER_FDB_DEL:
360 err = dsa_switch_fdb_del(ds, info);
362 case DSA_NOTIFIER_MDB_ADD:
363 err = dsa_switch_mdb_add(ds, info);
365 case DSA_NOTIFIER_MDB_DEL:
366 err = dsa_switch_mdb_del(ds, info);
368 case DSA_NOTIFIER_VLAN_ADD:
369 err = dsa_switch_vlan_add(ds, info);
371 case DSA_NOTIFIER_VLAN_DEL:
372 err = dsa_switch_vlan_del(ds, info);
374 case DSA_NOTIFIER_MTU:
375 err = dsa_switch_mtu(ds, info);
382 /* Non-switchdev operations cannot be rolled back. If a DSA driver
383 * returns an error during the chained call, switch chips may be in an
384 * inconsistent state.
387 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
390 return notifier_from_errno(err);
393 int dsa_switch_register_notifier(struct dsa_switch *ds)
395 ds->nb.notifier_call = dsa_switch_event;
397 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
400 void dsa_switch_unregister_notifier(struct dsa_switch *ds)
404 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
406 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);