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.
6 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
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 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
);
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
);
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
,
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
++) {
92 if (dsa_to_port(ds
, i
)->bridge_dev
== info
->br
) {
93 unset_vlan_filtering
= false;
98 if (unset_vlan_filtering
) {
99 struct switchdev_trans trans
= {0};
101 err
= dsa_port_vlan_filtering(dsa_to_port(ds
, info
->port
),
103 if (err
&& err
!= EOPNOTSUPP
)
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
)
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
)
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
)
137 if (dsa_is_dsa_port(ds
, port
))
143 static int dsa_switch_mdb_prepare(struct dsa_switch
*ds
,
144 struct dsa_notifier_mdb_info
*info
)
148 if (!ds
->ops
->port_mdb_prepare
|| !ds
->ops
->port_mdb_add
)
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
);
162 static int dsa_switch_mdb_add(struct dsa_switch
*ds
,
163 struct dsa_notifier_mdb_info
*info
)
167 if (switchdev_trans_ph_prepare(info
->trans
))
168 return dsa_switch_mdb_prepare(ds
, info
);
170 if (!ds
->ops
->port_mdb_add
)
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
);
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
)
186 if (ds
->index
== info
->sw_index
)
187 return ds
->ops
->port_mdb_del(ds
, info
->port
, info
->mdb
);
192 static int dsa_port_vlan_device_check(struct net_device
*vlan_dev
,
196 struct switchdev_obj_port_vlan
*vlan
= arg
;
199 for (vid
= vlan
->vid_begin
; vid
<= vlan
->vid_end
; ++vid
) {
200 if (vid
== vlan_dev_vid
)
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
);
213 /* Device is not bridged, let it proceed with the VLAN device
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
225 return vlan_for_each(dp
->slave
, dsa_port_vlan_device_check
,
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
)
235 if (dsa_is_dsa_port(ds
, port
))
241 static int dsa_switch_vlan_prepare(struct dsa_switch
*ds
,
242 struct dsa_notifier_vlan_info
*info
)
246 if (!ds
->ops
->port_vlan_prepare
|| !ds
->ops
->port_vlan_add
)
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
);
255 err
= ds
->ops
->port_vlan_prepare(ds
, port
, info
->vlan
);
264 static int dsa_switch_vlan_add(struct dsa_switch
*ds
,
265 struct dsa_notifier_vlan_info
*info
)
269 if (switchdev_trans_ph_prepare(info
->trans
))
270 return dsa_switch_vlan_prepare(ds
, info
);
272 if (!ds
->ops
->port_vlan_add
)
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
);
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
)
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.
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
);
304 case DSA_NOTIFIER_AGEING_TIME
:
305 err
= dsa_switch_ageing_time(ds
, info
);
307 case DSA_NOTIFIER_BRIDGE_JOIN
:
308 err
= dsa_switch_bridge_join(ds
, info
);
310 case DSA_NOTIFIER_BRIDGE_LEAVE
:
311 err
= dsa_switch_bridge_leave(ds
, info
);
313 case DSA_NOTIFIER_FDB_ADD
:
314 err
= dsa_switch_fdb_add(ds
, info
);
316 case DSA_NOTIFIER_FDB_DEL
:
317 err
= dsa_switch_fdb_del(ds
, info
);
319 case DSA_NOTIFIER_MDB_ADD
:
320 err
= dsa_switch_mdb_add(ds
, info
);
322 case DSA_NOTIFIER_MDB_DEL
:
323 err
= dsa_switch_mdb_del(ds
, info
);
325 case DSA_NOTIFIER_VLAN_ADD
:
326 err
= dsa_switch_vlan_add(ds
, info
);
328 case DSA_NOTIFIER_VLAN_DEL
:
329 err
= dsa_switch_vlan_del(ds
, info
);
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.
341 dev_dbg(ds
->dev
, "breaking chain for DSA event %lu (%d)\n",
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
)
358 err
= raw_notifier_chain_unregister(&ds
->dst
->nh
, &ds
->nb
);
360 dev_err(ds
->dev
, "failed to unregister notifier (%d)\n", err
);