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 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 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
);
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
);
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
,
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
++) {
126 if (dsa_to_port(ds
, i
)->bridge_dev
== info
->br
) {
127 unset_vlan_filtering
= false;
132 if (unset_vlan_filtering
) {
133 struct switchdev_trans trans
= {0};
135 err
= dsa_port_vlan_filtering(dsa_to_port(ds
, info
->port
),
137 if (err
&& err
!= EOPNOTSUPP
)
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
)
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
)
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
)
171 if (dsa_is_dsa_port(ds
, port
))
177 static int dsa_switch_mdb_prepare(struct dsa_switch
*ds
,
178 struct dsa_notifier_mdb_info
*info
)
182 if (!ds
->ops
->port_mdb_prepare
|| !ds
->ops
->port_mdb_add
)
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
);
196 static int dsa_switch_mdb_add(struct dsa_switch
*ds
,
197 struct dsa_notifier_mdb_info
*info
)
201 if (switchdev_trans_ph_prepare(info
->trans
))
202 return dsa_switch_mdb_prepare(ds
, info
);
204 if (!ds
->ops
->port_mdb_add
)
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
);
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
)
220 if (ds
->index
== info
->sw_index
)
221 return ds
->ops
->port_mdb_del(ds
, info
->port
, info
->mdb
);
226 static int dsa_port_vlan_device_check(struct net_device
*vlan_dev
,
230 struct switchdev_obj_port_vlan
*vlan
= arg
;
233 for (vid
= vlan
->vid_begin
; vid
<= vlan
->vid_end
; ++vid
) {
234 if (vid
== vlan_dev_vid
)
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
);
247 /* Device is not bridged, let it proceed with the VLAN device
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
259 return vlan_for_each(dp
->slave
, dsa_port_vlan_device_check
,
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
)
269 if (dsa_is_dsa_port(ds
, port
))
275 static int dsa_switch_vlan_prepare(struct dsa_switch
*ds
,
276 struct dsa_notifier_vlan_info
*info
)
280 if (!ds
->ops
->port_vlan_prepare
|| !ds
->ops
->port_vlan_add
)
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
);
289 err
= ds
->ops
->port_vlan_prepare(ds
, port
, info
->vlan
);
298 static int dsa_switch_vlan_add(struct dsa_switch
*ds
,
299 struct dsa_notifier_vlan_info
*info
)
303 if (switchdev_trans_ph_prepare(info
->trans
))
304 return dsa_switch_vlan_prepare(ds
, info
);
306 if (!ds
->ops
->port_vlan_add
)
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
);
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
)
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.
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
);
338 case DSA_NOTIFIER_AGEING_TIME
:
339 err
= dsa_switch_ageing_time(ds
, info
);
341 case DSA_NOTIFIER_BRIDGE_JOIN
:
342 err
= dsa_switch_bridge_join(ds
, info
);
344 case DSA_NOTIFIER_BRIDGE_LEAVE
:
345 err
= dsa_switch_bridge_leave(ds
, info
);
347 case DSA_NOTIFIER_FDB_ADD
:
348 err
= dsa_switch_fdb_add(ds
, info
);
350 case DSA_NOTIFIER_FDB_DEL
:
351 err
= dsa_switch_fdb_del(ds
, info
);
353 case DSA_NOTIFIER_MDB_ADD
:
354 err
= dsa_switch_mdb_add(ds
, info
);
356 case DSA_NOTIFIER_MDB_DEL
:
357 err
= dsa_switch_mdb_del(ds
, info
);
359 case DSA_NOTIFIER_VLAN_ADD
:
360 err
= dsa_switch_vlan_add(ds
, info
);
362 case DSA_NOTIFIER_VLAN_DEL
:
363 err
= dsa_switch_vlan_del(ds
, info
);
365 case DSA_NOTIFIER_MTU
:
366 err
= dsa_switch_mtu(ds
, info
);
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.
378 dev_dbg(ds
->dev
, "breaking chain for DSA event %lu (%d)\n",
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
)
395 err
= raw_notifier_chain_unregister(&ds
->dst
->nh
, &ds
->nb
);
397 dev_err(ds
->dev
, "failed to unregister notifier (%d)\n", err
);