blob: fd1a1c6bf9cf2c8acaa49db14f9080097d218815 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Vivien Didelotf515f192017-02-03 13:20:20 -05002/*
3 * Handling of a single switch chip, part of a switch fabric
4 *
Vivien Didelot4333d612017-03-28 15:10:36 -04005 * Copyright (c) 2017 Savoir-faire Linux Inc.
6 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
Vivien Didelotf515f192017-02-03 13:20:20 -05007 */
8
Vladimir Olteand371b7c2019-04-28 21:45:46 +03009#include <linux/if_bridge.h>
Vivien Didelotf515f192017-02-03 13:20:20 -050010#include <linux/netdevice.h>
11#include <linux/notifier.h>
Florian Fainelli061f6a52019-02-20 14:35:39 -080012#include <linux/if_vlan.h>
Vivien Didelot1faabf72017-05-19 17:00:52 -040013#include <net/switchdev.h>
Vivien Didelotea5dd342017-05-17 15:46:03 -040014
15#include "dsa_priv.h"
Vivien Didelotf515f192017-02-03 13:20:20 -050016
Vivien Didelot1faabf72017-05-19 17:00:52 -040017static 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) {
Vivien Didelot68bb8ea2019-10-21 16:51:15 -040023 struct dsa_port *dp = dsa_to_port(ds, i);
Vivien Didelot1faabf72017-05-19 17:00:52 -040024
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;
Vivien Didelot1faabf72017-05-19 17:00:52 -040036
Vladimir Oltean77b61362021-01-09 02:01:51 +020037 if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
38 return -ERANGE;
39
40 if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
41 return -ERANGE;
Vivien Didelot1faabf72017-05-19 17:00:52 -040042
43 /* Program the fastest ageing time in case of multiple bridges */
44 ageing_time = dsa_switch_fastest_ageing_time(ds, ageing_time);
45
46 if (ds->ops->set_ageing_time)
47 return ds->ops->set_ageing_time(ds, ageing_time);
48
49 return 0;
50}
51
Vladimir Olteanbfcb8132020-03-27 21:55:42 +020052static bool dsa_switch_mtu_match(struct dsa_switch *ds, int port,
53 struct dsa_notifier_mtu_info *info)
54{
Vladimir Oltean88faba22021-06-21 19:42:18 +030055 if (ds->index == info->sw_index && port == info->port)
56 return true;
Vladimir Olteanbfcb8132020-03-27 21:55:42 +020057
Vladimir Oltean88faba22021-06-21 19:42:18 +030058 /* Do not propagate to other switches in the tree if the notifier was
59 * targeted for a single switch.
60 */
61 if (info->targeted_match)
Vladimir Olteanbfcb8132020-03-27 21:55:42 +020062 return false;
63
64 if (dsa_is_dsa_port(ds, port) || dsa_is_cpu_port(ds, port))
65 return true;
66
67 return false;
68}
69
70static int dsa_switch_mtu(struct dsa_switch *ds,
71 struct dsa_notifier_mtu_info *info)
72{
73 int port, ret;
74
75 if (!ds->ops->port_change_mtu)
76 return -EOPNOTSUPP;
77
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);
81 if (ret)
82 return ret;
83 }
84 }
85
86 return 0;
87}
88
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050089static int dsa_switch_bridge_join(struct dsa_switch *ds,
90 struct dsa_notifier_bridge_info *info)
91{
Vladimir Olteanf66a6a62020-05-10 19:37:41 +030092 struct dsa_switch_tree *dst = ds->dst;
Vladimir Olteane19cc132021-07-19 20:14:51 +030093 int err;
Vladimir Olteanf66a6a62020-05-10 19:37:41 +030094
95 if (dst->index == info->tree_index && ds->index == info->sw_index &&
Vladimir Olteane19cc132021-07-19 20:14:51 +030096 ds->ops->port_bridge_join) {
97 err = ds->ops->port_bridge_join(ds, info->port, info->br);
98 if (err)
99 return err;
100 }
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500101
Vladimir Olteanf66a6a62020-05-10 19:37:41 +0300102 if ((dst->index != info->tree_index || ds->index != info->sw_index) &&
Vladimir Olteane19cc132021-07-19 20:14:51 +0300103 ds->ops->crosschip_bridge_join) {
104 err = ds->ops->crosschip_bridge_join(ds, info->tree_index,
105 info->sw_index,
106 info->port, info->br);
107 if (err)
108 return err;
109 }
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500110
Vladimir Olteane19cc132021-07-19 20:14:51 +0300111 return dsa_tag_8021q_bridge_join(ds, info);
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500112}
113
114static int dsa_switch_bridge_leave(struct dsa_switch *ds,
115 struct dsa_notifier_bridge_info *info)
116{
Vladimir Olteand371b7c2019-04-28 21:45:46 +0300117 bool unset_vlan_filtering = br_vlan_enabled(info->br);
Vladimir Olteanf66a6a62020-05-10 19:37:41 +0300118 struct dsa_switch_tree *dst = ds->dst;
Vladimir Oltean89153ed2021-02-13 22:43:19 +0200119 struct netlink_ext_ack extack = {0};
Vladimir Oltean479dc492021-03-24 11:56:39 +0200120 int err, port;
Vladimir Olteand371b7c2019-04-28 21:45:46 +0300121
Vladimir Olteanf66a6a62020-05-10 19:37:41 +0300122 if (dst->index == info->tree_index && ds->index == info->sw_index &&
Vladimir Olteanbcb99282021-07-13 12:40:21 +0300123 ds->ops->port_bridge_leave)
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500124 ds->ops->port_bridge_leave(ds, info->port, info->br);
125
Vladimir Olteanf66a6a62020-05-10 19:37:41 +0300126 if ((dst->index != info->tree_index || ds->index != info->sw_index) &&
Vladimir Olteanbcb99282021-07-13 12:40:21 +0300127 ds->ops->crosschip_bridge_leave)
Vladimir Olteanf66a6a62020-05-10 19:37:41 +0300128 ds->ops->crosschip_bridge_leave(ds, info->tree_index,
129 info->sw_index, info->port,
Vivien Didelot40ef2c92017-03-30 17:37:14 -0400130 info->br);
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500131
Vladimir Olteand371b7c2019-04-28 21:45:46 +0300132 /* If the bridge was vlan_filtering, the bridge core doesn't trigger an
133 * event for changing vlan_filtering setting upon slave ports leaving
134 * it. That is a good thing, because that lets us handle it and also
135 * handle the case where the switch's vlan_filtering setting is global
136 * (not per port). When that happens, the correct moment to trigger the
Vladimir Oltean479dc492021-03-24 11:56:39 +0200137 * vlan_filtering callback is only when the last port leaves the last
138 * VLAN-aware bridge.
Vladimir Olteand371b7c2019-04-28 21:45:46 +0300139 */
140 if (unset_vlan_filtering && ds->vlan_filtering_is_global) {
Vladimir Oltean479dc492021-03-24 11:56:39 +0200141 for (port = 0; port < ds->num_ports; port++) {
142 struct net_device *bridge_dev;
143
144 bridge_dev = dsa_to_port(ds, port)->bridge_dev;
145
146 if (bridge_dev && br_vlan_enabled(bridge_dev)) {
Vladimir Olteand371b7c2019-04-28 21:45:46 +0300147 unset_vlan_filtering = false;
148 break;
149 }
150 }
151 }
152 if (unset_vlan_filtering) {
Vladimir Oltean2e554a72020-10-03 01:06:46 +0300153 err = dsa_port_vlan_filtering(dsa_to_port(ds, info->port),
Vladimir Oltean89153ed2021-02-13 22:43:19 +0200154 false, &extack);
155 if (extack._msg)
156 dev_err(ds->dev, "port %d: %s\n", info->port,
157 extack._msg);
Vladimir Olteand371b7c2019-04-28 21:45:46 +0300158 if (err && err != EOPNOTSUPP)
159 return err;
160 }
Vladimir Olteane19cc132021-07-19 20:14:51 +0300161
162 return dsa_tag_8021q_bridge_leave(ds, info);
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500163}
164
Vladimir Olteanb8e997c2021-06-29 17:06:49 +0300165/* Matches for all upstream-facing ports (the CPU port and all upstream-facing
166 * DSA links) that sit between the targeted port on which the notifier was
167 * emitted and its dedicated CPU port.
168 */
169static bool dsa_switch_host_address_match(struct dsa_switch *ds, int port,
170 int info_sw_index, int info_port)
171{
172 struct dsa_port *targeted_dp, *cpu_dp;
173 struct dsa_switch *targeted_ds;
174
175 targeted_ds = dsa_switch_find(ds->dst->index, info_sw_index);
176 targeted_dp = dsa_to_port(targeted_ds, info_port);
177 cpu_dp = targeted_dp->cpu_dp;
178
179 if (dsa_switch_is_upstream_of(ds, targeted_ds))
180 return port == dsa_towards_port(ds, cpu_dp->ds->index,
181 cpu_dp->index);
182
183 return false;
184}
185
Vladimir Oltean161ca592021-06-29 17:06:50 +0300186static struct dsa_mac_addr *dsa_mac_addr_find(struct list_head *addr_list,
187 const unsigned char *addr,
188 u16 vid)
189{
190 struct dsa_mac_addr *a;
191
192 list_for_each_entry(a, addr_list, list)
193 if (ether_addr_equal(a->addr, addr) && a->vid == vid)
194 return a;
195
196 return NULL;
197}
198
199static int dsa_switch_do_mdb_add(struct dsa_switch *ds, int port,
200 const struct switchdev_obj_port_mdb *mdb)
201{
202 struct dsa_port *dp = dsa_to_port(ds, port);
203 struct dsa_mac_addr *a;
204 int err;
205
206 /* No need to bother with refcounting for user ports */
207 if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
208 return ds->ops->port_mdb_add(ds, port, mdb);
209
210 a = dsa_mac_addr_find(&dp->mdbs, mdb->addr, mdb->vid);
211 if (a) {
212 refcount_inc(&a->refcount);
213 return 0;
214 }
215
216 a = kzalloc(sizeof(*a), GFP_KERNEL);
217 if (!a)
218 return -ENOMEM;
219
220 err = ds->ops->port_mdb_add(ds, port, mdb);
221 if (err) {
222 kfree(a);
223 return err;
224 }
225
226 ether_addr_copy(a->addr, mdb->addr);
227 a->vid = mdb->vid;
228 refcount_set(&a->refcount, 1);
229 list_add_tail(&a->list, &dp->mdbs);
230
231 return 0;
232}
233
234static int dsa_switch_do_mdb_del(struct dsa_switch *ds, int port,
235 const struct switchdev_obj_port_mdb *mdb)
236{
237 struct dsa_port *dp = dsa_to_port(ds, port);
238 struct dsa_mac_addr *a;
239 int err;
240
241 /* No need to bother with refcounting for user ports */
242 if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
243 return ds->ops->port_mdb_del(ds, port, mdb);
244
245 a = dsa_mac_addr_find(&dp->mdbs, mdb->addr, mdb->vid);
246 if (!a)
247 return -ENOENT;
248
249 if (!refcount_dec_and_test(&a->refcount))
250 return 0;
251
252 err = ds->ops->port_mdb_del(ds, port, mdb);
253 if (err) {
254 refcount_inc(&a->refcount);
255 return err;
256 }
257
258 list_del(&a->list);
259 kfree(a);
260
261 return 0;
262}
263
Vladimir Oltean3f6e32f2021-06-29 17:06:52 +0300264static int dsa_switch_do_fdb_add(struct dsa_switch *ds, int port,
265 const unsigned char *addr, u16 vid)
266{
267 struct dsa_port *dp = dsa_to_port(ds, port);
268 struct dsa_mac_addr *a;
269 int err;
270
271 /* No need to bother with refcounting for user ports */
272 if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
273 return ds->ops->port_fdb_add(ds, port, addr, vid);
274
275 a = dsa_mac_addr_find(&dp->fdbs, addr, vid);
276 if (a) {
277 refcount_inc(&a->refcount);
278 return 0;
279 }
280
281 a = kzalloc(sizeof(*a), GFP_KERNEL);
282 if (!a)
283 return -ENOMEM;
284
285 err = ds->ops->port_fdb_add(ds, port, addr, vid);
286 if (err) {
287 kfree(a);
288 return err;
289 }
290
291 ether_addr_copy(a->addr, addr);
292 a->vid = vid;
293 refcount_set(&a->refcount, 1);
294 list_add_tail(&a->list, &dp->fdbs);
295
296 return 0;
297}
298
299static int dsa_switch_do_fdb_del(struct dsa_switch *ds, int port,
300 const unsigned char *addr, u16 vid)
301{
302 struct dsa_port *dp = dsa_to_port(ds, port);
303 struct dsa_mac_addr *a;
304 int err;
305
306 /* No need to bother with refcounting for user ports */
307 if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
308 return ds->ops->port_fdb_del(ds, port, addr, vid);
309
310 a = dsa_mac_addr_find(&dp->fdbs, addr, vid);
311 if (!a)
312 return -ENOENT;
313
314 if (!refcount_dec_and_test(&a->refcount))
315 return 0;
316
317 err = ds->ops->port_fdb_del(ds, port, addr, vid);
318 if (err) {
319 refcount_inc(&a->refcount);
320 return err;
321 }
322
323 list_del(&a->list);
324 kfree(a);
325
326 return 0;
327}
328
Vladimir Oltean3dc80af2021-06-29 17:06:51 +0300329static int dsa_switch_host_fdb_add(struct dsa_switch *ds,
330 struct dsa_notifier_fdb_info *info)
331{
332 int err = 0;
333 int port;
334
335 if (!ds->ops->port_fdb_add)
336 return -EOPNOTSUPP;
337
338 for (port = 0; port < ds->num_ports; port++) {
339 if (dsa_switch_host_address_match(ds, port, info->sw_index,
340 info->port)) {
Vladimir Oltean3f6e32f2021-06-29 17:06:52 +0300341 err = dsa_switch_do_fdb_add(ds, port, info->addr,
Vladimir Oltean3dc80af2021-06-29 17:06:51 +0300342 info->vid);
343 if (err)
344 break;
345 }
346 }
347
348 return err;
349}
350
351static int dsa_switch_host_fdb_del(struct dsa_switch *ds,
352 struct dsa_notifier_fdb_info *info)
353{
Vladimir Oltean3f6e32f2021-06-29 17:06:52 +0300354 int err = 0;
355 int port;
356
Vladimir Oltean3dc80af2021-06-29 17:06:51 +0300357 if (!ds->ops->port_fdb_del)
358 return -EOPNOTSUPP;
359
Vladimir Oltean3f6e32f2021-06-29 17:06:52 +0300360 for (port = 0; port < ds->num_ports; port++) {
361 if (dsa_switch_host_address_match(ds, port, info->sw_index,
362 info->port)) {
363 err = dsa_switch_do_fdb_del(ds, port, info->addr,
364 info->vid);
365 if (err)
366 break;
367 }
368 }
Vladimir Oltean3dc80af2021-06-29 17:06:51 +0300369
Vladimir Oltean3f6e32f2021-06-29 17:06:52 +0300370 return err;
Vladimir Oltean3dc80af2021-06-29 17:06:51 +0300371}
372
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400373static int dsa_switch_fdb_add(struct dsa_switch *ds,
374 struct dsa_notifier_fdb_info *info)
375{
Vivien Didelot31692412017-11-30 12:56:43 -0500376 int port = dsa_towards_port(ds, info->sw_index, info->port);
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400377
Arkadi Sharshevsky1b6dd552017-08-06 16:15:40 +0300378 if (!ds->ops->port_fdb_add)
379 return -EOPNOTSUPP;
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400380
Vladimir Oltean3f6e32f2021-06-29 17:06:52 +0300381 return dsa_switch_do_fdb_add(ds, port, info->addr, info->vid);
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400382}
383
384static int dsa_switch_fdb_del(struct dsa_switch *ds,
385 struct dsa_notifier_fdb_info *info)
386{
Vivien Didelot31692412017-11-30 12:56:43 -0500387 int port = dsa_towards_port(ds, info->sw_index, info->port);
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400388
389 if (!ds->ops->port_fdb_del)
390 return -EOPNOTSUPP;
391
Vladimir Oltean3f6e32f2021-06-29 17:06:52 +0300392 return dsa_switch_do_fdb_del(ds, port, info->addr, info->vid);
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400393}
394
George McCollister18596f52021-02-09 19:02:12 -0600395static int dsa_switch_hsr_join(struct dsa_switch *ds,
396 struct dsa_notifier_hsr_info *info)
397{
398 if (ds->index == info->sw_index && ds->ops->port_hsr_join)
399 return ds->ops->port_hsr_join(ds, info->port, info->hsr);
400
401 return -EOPNOTSUPP;
402}
403
404static int dsa_switch_hsr_leave(struct dsa_switch *ds,
405 struct dsa_notifier_hsr_info *info)
406{
407 if (ds->index == info->sw_index && ds->ops->port_hsr_leave)
408 return ds->ops->port_hsr_leave(ds, info->port, info->hsr);
409
410 return -EOPNOTSUPP;
411}
412
Tobias Waldekranz058102a2021-01-13 09:42:53 +0100413static int dsa_switch_lag_change(struct dsa_switch *ds,
414 struct dsa_notifier_lag_info *info)
415{
416 if (ds->index == info->sw_index && ds->ops->port_lag_change)
417 return ds->ops->port_lag_change(ds, info->port);
418
419 if (ds->index != info->sw_index && ds->ops->crosschip_lag_change)
420 return ds->ops->crosschip_lag_change(ds, info->sw_index,
421 info->port);
422
423 return 0;
424}
425
426static int dsa_switch_lag_join(struct dsa_switch *ds,
427 struct dsa_notifier_lag_info *info)
428{
429 if (ds->index == info->sw_index && ds->ops->port_lag_join)
430 return ds->ops->port_lag_join(ds, info->port, info->lag,
431 info->info);
432
433 if (ds->index != info->sw_index && ds->ops->crosschip_lag_join)
434 return ds->ops->crosschip_lag_join(ds, info->sw_index,
435 info->port, info->lag,
436 info->info);
437
Vladimir Olteanb71d0982021-06-29 23:32:15 +0300438 return -EOPNOTSUPP;
Tobias Waldekranz058102a2021-01-13 09:42:53 +0100439}
440
441static int dsa_switch_lag_leave(struct dsa_switch *ds,
442 struct dsa_notifier_lag_info *info)
443{
444 if (ds->index == info->sw_index && ds->ops->port_lag_leave)
445 return ds->ops->port_lag_leave(ds, info->port, info->lag);
446
447 if (ds->index != info->sw_index && ds->ops->crosschip_lag_leave)
448 return ds->ops->crosschip_lag_leave(ds, info->sw_index,
449 info->port, info->lag);
450
Vladimir Olteanb71d0982021-06-29 23:32:15 +0300451 return -EOPNOTSUPP;
Tobias Waldekranz058102a2021-01-13 09:42:53 +0100452}
453
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200454static int dsa_switch_mdb_add(struct dsa_switch *ds,
455 struct dsa_notifier_mdb_info *info)
Vivien Didelote6db98d2017-11-30 11:24:00 -0500456{
Vladimir Olteanabd49532021-06-21 19:42:16 +0300457 int port = dsa_towards_port(ds, info->sw_index, info->port);
Vivien Didelote6db98d2017-11-30 11:24:00 -0500458
Vladimir Olteana52b2da2021-01-09 02:01:52 +0200459 if (!ds->ops->port_mdb_add)
Vivien Didelote6db98d2017-11-30 11:24:00 -0500460 return -EOPNOTSUPP;
461
Vladimir Oltean161ca592021-06-29 17:06:50 +0300462 return dsa_switch_do_mdb_add(ds, port, info->mdb);
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400463}
464
465static int dsa_switch_mdb_del(struct dsa_switch *ds,
466 struct dsa_notifier_mdb_info *info)
467{
Vladimir Oltean161ca592021-06-29 17:06:50 +0300468 int port = dsa_towards_port(ds, info->sw_index, info->port);
469
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400470 if (!ds->ops->port_mdb_del)
471 return -EOPNOTSUPP;
472
Vladimir Oltean161ca592021-06-29 17:06:50 +0300473 return dsa_switch_do_mdb_del(ds, port, info->mdb);
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400474}
475
Vladimir Olteanb8e997c2021-06-29 17:06:49 +0300476static int dsa_switch_host_mdb_add(struct dsa_switch *ds,
477 struct dsa_notifier_mdb_info *info)
478{
479 int err = 0;
480 int port;
481
482 if (!ds->ops->port_mdb_add)
483 return -EOPNOTSUPP;
484
485 for (port = 0; port < ds->num_ports; port++) {
486 if (dsa_switch_host_address_match(ds, port, info->sw_index,
487 info->port)) {
Vladimir Oltean161ca592021-06-29 17:06:50 +0300488 err = dsa_switch_do_mdb_add(ds, port, info->mdb);
Vladimir Olteanb8e997c2021-06-29 17:06:49 +0300489 if (err)
490 break;
491 }
492 }
493
494 return err;
495}
496
497static int dsa_switch_host_mdb_del(struct dsa_switch *ds,
498 struct dsa_notifier_mdb_info *info)
499{
Vladimir Oltean161ca592021-06-29 17:06:50 +0300500 int err = 0;
501 int port;
502
Vladimir Olteanb8e997c2021-06-29 17:06:49 +0300503 if (!ds->ops->port_mdb_del)
504 return -EOPNOTSUPP;
505
Vladimir Oltean161ca592021-06-29 17:06:50 +0300506 for (port = 0; port < ds->num_ports; port++) {
507 if (dsa_switch_host_address_match(ds, port, info->sw_index,
508 info->port)) {
509 err = dsa_switch_do_mdb_del(ds, port, info->mdb);
510 if (err)
511 break;
512 }
513 }
Vladimir Olteanb8e997c2021-06-29 17:06:49 +0300514
Vladimir Oltean161ca592021-06-29 17:06:50 +0300515 return err;
Vladimir Olteanb8e997c2021-06-29 17:06:49 +0300516}
517
Vivien Didelote65d45c2019-08-25 13:25:15 -0400518static bool dsa_switch_vlan_match(struct dsa_switch *ds, int port,
519 struct dsa_notifier_vlan_info *info)
520{
521 if (ds->index == info->sw_index && port == info->port)
522 return true;
523
Vivien Didelot7e1741b2019-08-25 13:25:19 -0400524 if (dsa_is_dsa_port(ds, port))
Vivien Didelote65d45c2019-08-25 13:25:15 -0400525 return true;
526
527 return false;
528}
529
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200530static int dsa_switch_vlan_add(struct dsa_switch *ds,
531 struct dsa_notifier_vlan_info *info)
Vivien Didelot9c428c52017-11-30 11:23:59 -0500532{
533 int port, err;
534
Vladimir Oltean1958d582021-01-09 02:01:53 +0200535 if (!ds->ops->port_vlan_add)
Vivien Didelot9c428c52017-11-30 11:23:59 -0500536 return -EOPNOTSUPP;
537
Vivien Didelote65d45c2019-08-25 13:25:15 -0400538 for (port = 0; port < ds->num_ports; port++) {
539 if (dsa_switch_vlan_match(ds, port, info)) {
Vladimir Oltean31046a52021-02-13 22:43:18 +0200540 err = ds->ops->port_vlan_add(ds, port, info->vlan,
541 info->extack);
Vivien Didelote65d45c2019-08-25 13:25:15 -0400542 if (err)
543 return err;
544 }
Vivien Didelot9c428c52017-11-30 11:23:59 -0500545 }
546
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400547 return 0;
548}
549
550static int dsa_switch_vlan_del(struct dsa_switch *ds,
551 struct dsa_notifier_vlan_info *info)
552{
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400553 if (!ds->ops->port_vlan_del)
554 return -EOPNOTSUPP;
555
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400556 if (ds->index == info->sw_index)
Vivien Didelote65d45c2019-08-25 13:25:15 -0400557 return ds->ops->port_vlan_del(ds, info->port, info->vlan);
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400558
Vivien Didelot7e1741b2019-08-25 13:25:19 -0400559 /* Do not deprogram the DSA links as they may be used as conduit
560 * for other VLAN members in the fabric.
561 */
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400562 return 0;
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400563}
564
Vladimir Oltean53da0eb2021-01-29 03:00:06 +0200565static int dsa_switch_change_tag_proto(struct dsa_switch *ds,
566 struct dsa_notifier_tag_proto_info *info)
567{
568 const struct dsa_device_ops *tag_ops = info->tag_ops;
569 int port, err;
570
571 if (!ds->ops->change_tag_protocol)
572 return -EOPNOTSUPP;
573
574 ASSERT_RTNL();
575
576 for (port = 0; port < ds->num_ports; port++) {
Tobias Waldekranz21e0b502021-04-20 20:53:09 +0200577 if (!dsa_is_cpu_port(ds, port))
578 continue;
Vladimir Oltean53da0eb2021-01-29 03:00:06 +0200579
Tobias Waldekranz21e0b502021-04-20 20:53:09 +0200580 err = ds->ops->change_tag_protocol(ds, port, tag_ops->proto);
581 if (err)
582 return err;
583
584 dsa_port_set_tag_protocol(dsa_to_port(ds, port), tag_ops);
Vladimir Oltean53da0eb2021-01-29 03:00:06 +0200585 }
586
587 /* Now that changing the tag protocol can no longer fail, let's update
588 * the remaining bits which are "duplicated for faster access", and the
589 * bits that depend on the tagger, such as the MTU.
590 */
591 for (port = 0; port < ds->num_ports; port++) {
592 if (dsa_is_user_port(ds, port)) {
593 struct net_device *slave;
594
595 slave = dsa_to_port(ds, port)->slave;
596 dsa_slave_setup_tagger(slave);
597
598 /* rtnl_mutex is held in dsa_tree_change_tag_proto */
599 dsa_slave_change_mtu(slave, slave->mtu);
600 }
601 }
602
603 return 0;
604}
605
Horatiu Vulturc595c432021-02-16 22:42:04 +0100606static int dsa_switch_mrp_add(struct dsa_switch *ds,
607 struct dsa_notifier_mrp_info *info)
608{
Horatiu Vulturc595c432021-02-16 22:42:04 +0100609 if (!ds->ops->port_mrp_add)
610 return -EOPNOTSUPP;
611
Vladimir Olteanf9bcdc362021-06-21 19:42:19 +0300612 if (ds->index == info->sw_index)
613 return ds->ops->port_mrp_add(ds, info->port, info->mrp);
Horatiu Vulturc595c432021-02-16 22:42:04 +0100614
Vladimir Olteanf9bcdc362021-06-21 19:42:19 +0300615 return 0;
Horatiu Vulturc595c432021-02-16 22:42:04 +0100616}
617
618static int dsa_switch_mrp_del(struct dsa_switch *ds,
619 struct dsa_notifier_mrp_info *info)
620{
621 if (!ds->ops->port_mrp_del)
622 return -EOPNOTSUPP;
623
624 if (ds->index == info->sw_index)
625 return ds->ops->port_mrp_del(ds, info->port, info->mrp);
626
627 return 0;
628}
629
Horatiu Vulturc595c432021-02-16 22:42:04 +0100630static int
631dsa_switch_mrp_add_ring_role(struct dsa_switch *ds,
632 struct dsa_notifier_mrp_ring_role_info *info)
633{
Horatiu Vulturc595c432021-02-16 22:42:04 +0100634 if (!ds->ops->port_mrp_add)
635 return -EOPNOTSUPP;
636
Vladimir Olteanf9bcdc362021-06-21 19:42:19 +0300637 if (ds->index == info->sw_index)
638 return ds->ops->port_mrp_add_ring_role(ds, info->port,
639 info->mrp);
Horatiu Vulturc595c432021-02-16 22:42:04 +0100640
Vladimir Olteanf9bcdc362021-06-21 19:42:19 +0300641 return 0;
Horatiu Vulturc595c432021-02-16 22:42:04 +0100642}
643
644static int
645dsa_switch_mrp_del_ring_role(struct dsa_switch *ds,
646 struct dsa_notifier_mrp_ring_role_info *info)
647{
648 if (!ds->ops->port_mrp_del)
649 return -EOPNOTSUPP;
650
651 if (ds->index == info->sw_index)
652 return ds->ops->port_mrp_del_ring_role(ds, info->port,
653 info->mrp);
654
655 return 0;
656}
657
Vivien Didelotf515f192017-02-03 13:20:20 -0500658static int dsa_switch_event(struct notifier_block *nb,
659 unsigned long event, void *info)
660{
661 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
662 int err;
663
664 switch (event) {
Vivien Didelot1faabf72017-05-19 17:00:52 -0400665 case DSA_NOTIFIER_AGEING_TIME:
666 err = dsa_switch_ageing_time(ds, info);
667 break;
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500668 case DSA_NOTIFIER_BRIDGE_JOIN:
669 err = dsa_switch_bridge_join(ds, info);
670 break;
671 case DSA_NOTIFIER_BRIDGE_LEAVE:
672 err = dsa_switch_bridge_leave(ds, info);
673 break;
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400674 case DSA_NOTIFIER_FDB_ADD:
675 err = dsa_switch_fdb_add(ds, info);
676 break;
677 case DSA_NOTIFIER_FDB_DEL:
678 err = dsa_switch_fdb_del(ds, info);
679 break;
Vladimir Oltean3dc80af2021-06-29 17:06:51 +0300680 case DSA_NOTIFIER_HOST_FDB_ADD:
681 err = dsa_switch_host_fdb_add(ds, info);
682 break;
683 case DSA_NOTIFIER_HOST_FDB_DEL:
684 err = dsa_switch_host_fdb_del(ds, info);
685 break;
George McCollister18596f52021-02-09 19:02:12 -0600686 case DSA_NOTIFIER_HSR_JOIN:
687 err = dsa_switch_hsr_join(ds, info);
688 break;
689 case DSA_NOTIFIER_HSR_LEAVE:
690 err = dsa_switch_hsr_leave(ds, info);
691 break;
Tobias Waldekranz058102a2021-01-13 09:42:53 +0100692 case DSA_NOTIFIER_LAG_CHANGE:
693 err = dsa_switch_lag_change(ds, info);
694 break;
695 case DSA_NOTIFIER_LAG_JOIN:
696 err = dsa_switch_lag_join(ds, info);
697 break;
698 case DSA_NOTIFIER_LAG_LEAVE:
699 err = dsa_switch_lag_leave(ds, info);
700 break;
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400701 case DSA_NOTIFIER_MDB_ADD:
702 err = dsa_switch_mdb_add(ds, info);
703 break;
704 case DSA_NOTIFIER_MDB_DEL:
705 err = dsa_switch_mdb_del(ds, info);
706 break;
Vladimir Olteanb8e997c2021-06-29 17:06:49 +0300707 case DSA_NOTIFIER_HOST_MDB_ADD:
708 err = dsa_switch_host_mdb_add(ds, info);
709 break;
710 case DSA_NOTIFIER_HOST_MDB_DEL:
711 err = dsa_switch_host_mdb_del(ds, info);
712 break;
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400713 case DSA_NOTIFIER_VLAN_ADD:
714 err = dsa_switch_vlan_add(ds, info);
715 break;
716 case DSA_NOTIFIER_VLAN_DEL:
717 err = dsa_switch_vlan_del(ds, info);
718 break;
Vladimir Olteanbfcb8132020-03-27 21:55:42 +0200719 case DSA_NOTIFIER_MTU:
720 err = dsa_switch_mtu(ds, info);
721 break;
Vladimir Oltean53da0eb2021-01-29 03:00:06 +0200722 case DSA_NOTIFIER_TAG_PROTO:
723 err = dsa_switch_change_tag_proto(ds, info);
724 break;
Horatiu Vulturc595c432021-02-16 22:42:04 +0100725 case DSA_NOTIFIER_MRP_ADD:
726 err = dsa_switch_mrp_add(ds, info);
727 break;
728 case DSA_NOTIFIER_MRP_DEL:
729 err = dsa_switch_mrp_del(ds, info);
730 break;
731 case DSA_NOTIFIER_MRP_ADD_RING_ROLE:
732 err = dsa_switch_mrp_add_ring_role(ds, info);
733 break;
734 case DSA_NOTIFIER_MRP_DEL_RING_ROLE:
735 err = dsa_switch_mrp_del_ring_role(ds, info);
736 break;
Vladimir Olteanc64b9c02021-07-19 20:14:52 +0300737 case DSA_NOTIFIER_TAG_8021Q_VLAN_ADD:
738 err = dsa_switch_tag_8021q_vlan_add(ds, info);
739 break;
740 case DSA_NOTIFIER_TAG_8021Q_VLAN_DEL:
741 err = dsa_switch_tag_8021q_vlan_del(ds, info);
742 break;
Vivien Didelotf515f192017-02-03 13:20:20 -0500743 default:
744 err = -EOPNOTSUPP;
745 break;
746 }
747
Vivien Didelotf515f192017-02-03 13:20:20 -0500748 if (err)
749 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
750 event, err);
751
752 return notifier_from_errno(err);
753}
754
755int dsa_switch_register_notifier(struct dsa_switch *ds)
756{
757 ds->nb.notifier_call = dsa_switch_event;
758
759 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
760}
761
762void dsa_switch_unregister_notifier(struct dsa_switch *ds)
763{
764 int err;
765
766 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
767 if (err)
768 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
769}