blob: f517d6d7efa23c2d610f4a075098e473f0c49fc8 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Handling of a single switch chip, part of a switch fabric
4 *
5 * Copyright (c) 2017 Savoir-faire Linux Inc.
6 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
7 */
8
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>
14
15#include "dsa_priv.h"
16
17static unsigned int dsa_switch_fastest_ageing_time(struct dsa_switch *ds,
18 unsigned int ageing_time)
19{
20 int i;
21
22 for (i = 0; i < ds->num_ports; ++i) {
23 struct dsa_port *dp = &ds->ports[i];
24
25 if (dp->ageing_time && dp->ageing_time < ageing_time)
26 ageing_time = dp->ageing_time;
27 }
28
29 return ageing_time;
30}
31
32static int dsa_switch_ageing_time(struct dsa_switch *ds,
33 struct dsa_notifier_ageing_time_info *info)
34{
35 unsigned int ageing_time = info->ageing_time;
36 struct switchdev_trans *trans = info->trans;
37
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;
44 }
45
46 /* Program the fastest ageing time in case of multiple bridges */
47 ageing_time = dsa_switch_fastest_ageing_time(ds, ageing_time);
48
49 if (ds->ops->set_ageing_time)
50 return ds->ops->set_ageing_time(ds, ageing_time);
51
52 return 0;
53}
54
55static int dsa_switch_bridge_join(struct dsa_switch *ds,
56 struct dsa_notifier_bridge_info *info)
57{
58 if (ds->index == info->sw_index && ds->ops->port_bridge_join)
59 return ds->ops->port_bridge_join(ds, info->port, info->br);
60
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);
64
65 return 0;
66}
67
68static int dsa_switch_sync_vlan_filtering(struct dsa_switch *ds,
69 struct dsa_notifier_bridge_info *info)
70{
71 bool unset_vlan_filtering = br_vlan_enabled(info->br);
72 int err, i;
73
74 /* If the bridge was vlan_filtering, the bridge core doesn't trigger an
75 * event for changing vlan_filtering setting upon slave ports leaving
76 * it. That is a good thing, because that lets us handle it and also
77 * handle the case where the switch's vlan_filtering setting is global
78 * (not per port). When that happens, the correct moment to trigger the
79 * vlan_filtering callback is only when the last port left this bridge.
80 */
81 if (unset_vlan_filtering && ds->vlan_filtering_is_global) {
82 for (i = 0; i < ds->num_ports; i++) {
83 if (i == info->port)
84 continue;
85 if (dsa_to_port(ds, i)->bridge_dev == info->br) {
86 unset_vlan_filtering = false;
87 break;
88 }
89 }
90 }
91 if (unset_vlan_filtering) {
92 struct switchdev_trans trans = {0};
93
94 err = dsa_port_vlan_filtering(&ds->ports[info->port],
95 false, &trans);
96 if (err && err != EOPNOTSUPP)
97 return err;
98 }
99
100 return 0;
101}
102
103static int dsa_switch_bridge_leave(struct dsa_switch *ds,
104 struct dsa_notifier_bridge_info *info)
105{
106 int err;
107
108 if (ds->index == info->sw_index && ds->ops->port_bridge_leave)
109 ds->ops->port_bridge_leave(ds, info->port, info->br);
110
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);
114
115 if (ds->index == info->sw_index) {
116 err = dsa_switch_sync_vlan_filtering(ds, info);
117 if (err)
118 return err;
119 }
120
121 return 0;
122}
123
124static int dsa_switch_fdb_add(struct dsa_switch *ds,
125 struct dsa_notifier_fdb_info *info)
126{
127 int port = dsa_towards_port(ds, info->sw_index, info->port);
128
129 if (!ds->ops->port_fdb_add)
130 return -EOPNOTSUPP;
131
132 return ds->ops->port_fdb_add(ds, port, info->addr, info->vid);
133}
134
135static int dsa_switch_fdb_del(struct dsa_switch *ds,
136 struct dsa_notifier_fdb_info *info)
137{
138 int port = dsa_towards_port(ds, info->sw_index, info->port);
139
140 if (!ds->ops->port_fdb_del)
141 return -EOPNOTSUPP;
142
143 return ds->ops->port_fdb_del(ds, port, info->addr, info->vid);
144}
145
146static bool dsa_switch_mdb_match(struct dsa_switch *ds, int port,
147 struct dsa_notifier_mdb_info *info)
148{
149 if (ds->index == info->sw_index && port == info->port)
150 return true;
151
152 if (dsa_is_dsa_port(ds, port))
153 return true;
154
155 return false;
156}
157
158static int dsa_switch_mdb_prepare(struct dsa_switch *ds,
159 struct dsa_notifier_mdb_info *info)
160{
161 int port, err;
162
163 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
164 return -EOPNOTSUPP;
165
166 for (port = 0; port < ds->num_ports; port++) {
167 if (dsa_switch_mdb_match(ds, port, info)) {
168 err = ds->ops->port_mdb_prepare(ds, port, info->mdb);
169 if (err)
170 return err;
171 }
172 }
173
174 return 0;
175}
176
177static int dsa_switch_mdb_add(struct dsa_switch *ds,
178 struct dsa_notifier_mdb_info *info)
179{
180 int port;
181
182 if (switchdev_trans_ph_prepare(info->trans))
183 return dsa_switch_mdb_prepare(ds, info);
184
185 if (!ds->ops->port_mdb_add)
186 return 0;
187
188 for (port = 0; port < ds->num_ports; port++)
189 if (dsa_switch_mdb_match(ds, port, info))
190 ds->ops->port_mdb_add(ds, port, info->mdb);
191
192 return 0;
193}
194
195static int dsa_switch_mdb_del(struct dsa_switch *ds,
196 struct dsa_notifier_mdb_info *info)
197{
198 if (!ds->ops->port_mdb_del)
199 return -EOPNOTSUPP;
200
201 if (ds->index == info->sw_index)
202 return ds->ops->port_mdb_del(ds, info->port, info->mdb);
203
204 return 0;
205}
206
207static int dsa_port_vlan_device_check(struct net_device *vlan_dev,
208 int vlan_dev_vid,
209 void *arg)
210{
211 struct switchdev_obj_port_vlan *vlan = arg;
212 u16 vid;
213
214 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
215 if (vid == vlan_dev_vid)
216 return -EBUSY;
217 }
218
219 return 0;
220}
221
222static int dsa_port_vlan_check(struct dsa_switch *ds, int port,
223 const struct switchdev_obj_port_vlan *vlan)
224{
225 const struct dsa_port *dp = dsa_to_port(ds, port);
226 int err = 0;
227
228 /* Device is not bridged, let it proceed with the VLAN device
229 * creation.
230 */
231 if (!dp->bridge_dev)
232 return err;
233
234 /* dsa_slave_vlan_rx_{add,kill}_vid() cannot use the prepare phase and
235 * already checks whether there is an overlapping bridge VLAN entry
236 * with the same VID, so here we only need to check that if we are
237 * adding a bridge VLAN entry there is not an overlapping VLAN device
238 * claiming that VID.
239 */
240 return vlan_for_each(dp->slave, dsa_port_vlan_device_check,
241 (void *)vlan);
242}
243
244static bool dsa_switch_vlan_match(struct dsa_switch *ds, int port,
245 struct dsa_notifier_vlan_info *info)
246{
247 if (ds->index == info->sw_index && port == info->port)
248 return true;
249
250 if (dsa_is_dsa_port(ds, port))
251 return true;
252
253 return false;
254}
255
256static int dsa_switch_vlan_prepare(struct dsa_switch *ds,
257 struct dsa_notifier_vlan_info *info)
258{
259 int port, err;
260
261 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
262 return -EOPNOTSUPP;
263
264 for (port = 0; port < ds->num_ports; port++) {
265 if (dsa_switch_vlan_match(ds, port, info)) {
266 err = dsa_port_vlan_check(ds, port, info->vlan);
267 if (err)
268 return err;
269
270 err = ds->ops->port_vlan_prepare(ds, port, info->vlan);
271 if (err)
272 return err;
273 }
274 }
275
276 return 0;
277}
278
279static int dsa_switch_vlan_add(struct dsa_switch *ds,
280 struct dsa_notifier_vlan_info *info)
281{
282 int port;
283
284 if (switchdev_trans_ph_prepare(info->trans))
285 return dsa_switch_vlan_prepare(ds, info);
286
287 if (!ds->ops->port_vlan_add)
288 return 0;
289
290 for (port = 0; port < ds->num_ports; port++)
291 if (dsa_switch_vlan_match(ds, port, info))
292 ds->ops->port_vlan_add(ds, port, info->vlan);
293
294 return 0;
295}
296
297static int dsa_switch_vlan_del(struct dsa_switch *ds,
298 struct dsa_notifier_vlan_info *info)
299{
300 if (!ds->ops->port_vlan_del)
301 return -EOPNOTSUPP;
302
303 if (ds->index == info->sw_index)
304 return ds->ops->port_vlan_del(ds, info->port, info->vlan);
305
306 /* Do not deprogram the DSA links as they may be used as conduit
307 * for other VLAN members in the fabric.
308 */
309 return 0;
310}
311
312static int dsa_switch_event(struct notifier_block *nb,
313 unsigned long event, void *info)
314{
315 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
316 int err;
317
318 switch (event) {
319 case DSA_NOTIFIER_AGEING_TIME:
320 err = dsa_switch_ageing_time(ds, info);
321 break;
322 case DSA_NOTIFIER_BRIDGE_JOIN:
323 err = dsa_switch_bridge_join(ds, info);
324 break;
325 case DSA_NOTIFIER_BRIDGE_LEAVE:
326 err = dsa_switch_bridge_leave(ds, info);
327 break;
328 case DSA_NOTIFIER_FDB_ADD:
329 err = dsa_switch_fdb_add(ds, info);
330 break;
331 case DSA_NOTIFIER_FDB_DEL:
332 err = dsa_switch_fdb_del(ds, info);
333 break;
334 case DSA_NOTIFIER_MDB_ADD:
335 err = dsa_switch_mdb_add(ds, info);
336 break;
337 case DSA_NOTIFIER_MDB_DEL:
338 err = dsa_switch_mdb_del(ds, info);
339 break;
340 case DSA_NOTIFIER_VLAN_ADD:
341 err = dsa_switch_vlan_add(ds, info);
342 break;
343 case DSA_NOTIFIER_VLAN_DEL:
344 err = dsa_switch_vlan_del(ds, info);
345 break;
346 default:
347 err = -EOPNOTSUPP;
348 break;
349 }
350
351 /* Non-switchdev operations cannot be rolled back. If a DSA driver
352 * returns an error during the chained call, switch chips may be in an
353 * inconsistent state.
354 */
355 if (err)
356 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
357 event, err);
358
359 return notifier_from_errno(err);
360}
361
362int dsa_switch_register_notifier(struct dsa_switch *ds)
363{
364 ds->nb.notifier_call = dsa_switch_event;
365
366 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
367}
368
369void dsa_switch_unregister_notifier(struct dsa_switch *ds)
370{
371 int err;
372
373 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
374 if (err)
375 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
376}