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
= &ds
->ports
[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(&ds
->ports
[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
);
132 dsa_switch_mdb_prepare_bitmap(struct dsa_switch
*ds
,
133 const struct switchdev_obj_port_mdb
*mdb
,
134 const unsigned long *bitmap
)
138 if (!ds
->ops
->port_mdb_prepare
|| !ds
->ops
->port_mdb_add
)
141 for_each_set_bit(port
, bitmap
, ds
->num_ports
) {
142 err
= ds
->ops
->port_mdb_prepare(ds
, port
, mdb
);
150 static void dsa_switch_mdb_add_bitmap(struct dsa_switch
*ds
,
151 const struct switchdev_obj_port_mdb
*mdb
,
152 const unsigned long *bitmap
)
156 for_each_set_bit(port
, bitmap
, ds
->num_ports
)
157 ds
->ops
->port_mdb_add(ds
, port
, mdb
);
160 static int dsa_switch_mdb_add(struct dsa_switch
*ds
,
161 struct dsa_notifier_mdb_info
*info
)
163 const struct switchdev_obj_port_mdb
*mdb
= info
->mdb
;
164 struct switchdev_trans
*trans
= info
->trans
;
167 /* Build a mask of Multicast group members */
168 bitmap_zero(ds
->bitmap
, ds
->num_ports
);
169 if (ds
->index
== info
->sw_index
)
170 set_bit(info
->port
, ds
->bitmap
);
171 for (port
= 0; port
< ds
->num_ports
; port
++)
172 if (dsa_is_dsa_port(ds
, port
))
173 set_bit(port
, ds
->bitmap
);
175 if (switchdev_trans_ph_prepare(trans
))
176 return dsa_switch_mdb_prepare_bitmap(ds
, mdb
, ds
->bitmap
);
178 dsa_switch_mdb_add_bitmap(ds
, mdb
, ds
->bitmap
);
183 static int dsa_switch_mdb_del(struct dsa_switch
*ds
,
184 struct dsa_notifier_mdb_info
*info
)
186 const struct switchdev_obj_port_mdb
*mdb
= info
->mdb
;
188 if (!ds
->ops
->port_mdb_del
)
191 if (ds
->index
== info
->sw_index
)
192 return ds
->ops
->port_mdb_del(ds
, info
->port
, mdb
);
197 static int dsa_port_vlan_device_check(struct net_device
*vlan_dev
,
201 struct switchdev_obj_port_vlan
*vlan
= arg
;
204 for (vid
= vlan
->vid_begin
; vid
<= vlan
->vid_end
; ++vid
) {
205 if (vid
== vlan_dev_vid
)
212 static int dsa_port_vlan_check(struct dsa_switch
*ds
, int port
,
213 const struct switchdev_obj_port_vlan
*vlan
)
215 const struct dsa_port
*dp
= dsa_to_port(ds
, port
);
218 /* Device is not bridged, let it proceed with the VLAN device
224 /* dsa_slave_vlan_rx_{add,kill}_vid() cannot use the prepare phase and
225 * already checks whether there is an overlapping bridge VLAN entry
226 * with the same VID, so here we only need to check that if we are
227 * adding a bridge VLAN entry there is not an overlapping VLAN device
230 return vlan_for_each(dp
->slave
, dsa_port_vlan_device_check
,
235 dsa_switch_vlan_prepare_bitmap(struct dsa_switch
*ds
,
236 const struct switchdev_obj_port_vlan
*vlan
,
237 const unsigned long *bitmap
)
241 if (!ds
->ops
->port_vlan_prepare
|| !ds
->ops
->port_vlan_add
)
244 for_each_set_bit(port
, bitmap
, ds
->num_ports
) {
245 err
= dsa_port_vlan_check(ds
, port
, vlan
);
249 err
= ds
->ops
->port_vlan_prepare(ds
, port
, vlan
);
258 dsa_switch_vlan_add_bitmap(struct dsa_switch
*ds
,
259 const struct switchdev_obj_port_vlan
*vlan
,
260 const unsigned long *bitmap
)
264 for_each_set_bit(port
, bitmap
, ds
->num_ports
)
265 ds
->ops
->port_vlan_add(ds
, port
, vlan
);
268 static int dsa_switch_vlan_add(struct dsa_switch
*ds
,
269 struct dsa_notifier_vlan_info
*info
)
271 const struct switchdev_obj_port_vlan
*vlan
= info
->vlan
;
272 struct switchdev_trans
*trans
= info
->trans
;
275 /* Build a mask of VLAN members */
276 bitmap_zero(ds
->bitmap
, ds
->num_ports
);
277 if (ds
->index
== info
->sw_index
)
278 set_bit(info
->port
, ds
->bitmap
);
279 for (port
= 0; port
< ds
->num_ports
; port
++)
280 if (dsa_is_cpu_port(ds
, port
) || dsa_is_dsa_port(ds
, port
))
281 set_bit(port
, ds
->bitmap
);
283 if (switchdev_trans_ph_prepare(trans
))
284 return dsa_switch_vlan_prepare_bitmap(ds
, vlan
, ds
->bitmap
);
286 dsa_switch_vlan_add_bitmap(ds
, vlan
, ds
->bitmap
);
291 static int dsa_switch_vlan_del(struct dsa_switch
*ds
,
292 struct dsa_notifier_vlan_info
*info
)
294 const struct switchdev_obj_port_vlan
*vlan
= info
->vlan
;
296 if (!ds
->ops
->port_vlan_del
)
299 if (ds
->index
== info
->sw_index
)
300 return ds
->ops
->port_vlan_del(ds
, info
->port
, vlan
);
305 static int dsa_switch_event(struct notifier_block
*nb
,
306 unsigned long event
, void *info
)
308 struct dsa_switch
*ds
= container_of(nb
, struct dsa_switch
, nb
);
312 case DSA_NOTIFIER_AGEING_TIME
:
313 err
= dsa_switch_ageing_time(ds
, info
);
315 case DSA_NOTIFIER_BRIDGE_JOIN
:
316 err
= dsa_switch_bridge_join(ds
, info
);
318 case DSA_NOTIFIER_BRIDGE_LEAVE
:
319 err
= dsa_switch_bridge_leave(ds
, info
);
321 case DSA_NOTIFIER_FDB_ADD
:
322 err
= dsa_switch_fdb_add(ds
, info
);
324 case DSA_NOTIFIER_FDB_DEL
:
325 err
= dsa_switch_fdb_del(ds
, info
);
327 case DSA_NOTIFIER_MDB_ADD
:
328 err
= dsa_switch_mdb_add(ds
, info
);
330 case DSA_NOTIFIER_MDB_DEL
:
331 err
= dsa_switch_mdb_del(ds
, info
);
333 case DSA_NOTIFIER_VLAN_ADD
:
334 err
= dsa_switch_vlan_add(ds
, info
);
336 case DSA_NOTIFIER_VLAN_DEL
:
337 err
= dsa_switch_vlan_del(ds
, info
);
344 /* Non-switchdev operations cannot be rolled back. If a DSA driver
345 * returns an error during the chained call, switch chips may be in an
346 * inconsistent state.
349 dev_dbg(ds
->dev
, "breaking chain for DSA event %lu (%d)\n",
352 return notifier_from_errno(err
);
355 int dsa_switch_register_notifier(struct dsa_switch
*ds
)
357 ds
->nb
.notifier_call
= dsa_switch_event
;
359 return raw_notifier_chain_register(&ds
->dst
->nh
, &ds
->nb
);
362 void dsa_switch_unregister_notifier(struct dsa_switch
*ds
)
366 err
= raw_notifier_chain_unregister(&ds
->dst
->nh
, &ds
->nb
);
368 dev_err(ds
->dev
, "failed to unregister notifier (%d)\n", err
);