blob: 5f5e19c5e43ae562cfc16ebe167f0ac5bcef9c3e [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{
55 if (ds->index == info->sw_index)
56 return (port == info->port) || dsa_is_dsa_port(ds, port);
57
58 if (!info->propagate_upstream)
59 return false;
60
61 if (dsa_is_dsa_port(ds, port) || dsa_is_cpu_port(ds, port))
62 return true;
63
64 return false;
65}
66
67static int dsa_switch_mtu(struct dsa_switch *ds,
68 struct dsa_notifier_mtu_info *info)
69{
70 int port, ret;
71
72 if (!ds->ops->port_change_mtu)
73 return -EOPNOTSUPP;
74
75 for (port = 0; port < ds->num_ports; port++) {
76 if (dsa_switch_mtu_match(ds, port, info)) {
77 ret = ds->ops->port_change_mtu(ds, port, info->mtu);
78 if (ret)
79 return ret;
80 }
81 }
82
83 return 0;
84}
85
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050086static int dsa_switch_bridge_join(struct dsa_switch *ds,
87 struct dsa_notifier_bridge_info *info)
88{
Vladimir Olteanf66a6a62020-05-10 19:37:41 +030089 struct dsa_switch_tree *dst = ds->dst;
90
91 if (dst->index == info->tree_index && ds->index == info->sw_index &&
92 ds->ops->port_bridge_join)
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050093 return ds->ops->port_bridge_join(ds, info->port, info->br);
94
Vladimir Olteanf66a6a62020-05-10 19:37:41 +030095 if ((dst->index != info->tree_index || ds->index != info->sw_index) &&
96 ds->ops->crosschip_bridge_join)
97 return ds->ops->crosschip_bridge_join(ds, info->tree_index,
98 info->sw_index,
Vivien Didelot40ef2c92017-03-30 17:37:14 -040099 info->port, info->br);
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500100
101 return 0;
102}
103
104static int dsa_switch_bridge_leave(struct dsa_switch *ds,
105 struct dsa_notifier_bridge_info *info)
106{
Vladimir Olteand371b7c2019-04-28 21:45:46 +0300107 bool unset_vlan_filtering = br_vlan_enabled(info->br);
Vladimir Olteanf66a6a62020-05-10 19:37:41 +0300108 struct dsa_switch_tree *dst = ds->dst;
Vladimir Olteand371b7c2019-04-28 21:45:46 +0300109 int err, i;
110
Vladimir Olteanf66a6a62020-05-10 19:37:41 +0300111 if (dst->index == info->tree_index && ds->index == info->sw_index &&
112 ds->ops->port_bridge_join)
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500113 ds->ops->port_bridge_leave(ds, info->port, info->br);
114
Vladimir Olteanf66a6a62020-05-10 19:37:41 +0300115 if ((dst->index != info->tree_index || ds->index != info->sw_index) &&
116 ds->ops->crosschip_bridge_join)
117 ds->ops->crosschip_bridge_leave(ds, info->tree_index,
118 info->sw_index, info->port,
Vivien Didelot40ef2c92017-03-30 17:37:14 -0400119 info->br);
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500120
Vladimir Olteand371b7c2019-04-28 21:45:46 +0300121 /* If the bridge was vlan_filtering, the bridge core doesn't trigger an
122 * event for changing vlan_filtering setting upon slave ports leaving
123 * it. That is a good thing, because that lets us handle it and also
124 * handle the case where the switch's vlan_filtering setting is global
125 * (not per port). When that happens, the correct moment to trigger the
126 * vlan_filtering callback is only when the last port left this bridge.
127 */
128 if (unset_vlan_filtering && ds->vlan_filtering_is_global) {
129 for (i = 0; i < ds->num_ports; i++) {
130 if (i == info->port)
131 continue;
132 if (dsa_to_port(ds, i)->bridge_dev == info->br) {
133 unset_vlan_filtering = false;
134 break;
135 }
136 }
137 }
138 if (unset_vlan_filtering) {
Vladimir Oltean2e554a72020-10-03 01:06:46 +0300139 err = dsa_port_vlan_filtering(dsa_to_port(ds, info->port),
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200140 false);
Vladimir Olteand371b7c2019-04-28 21:45:46 +0300141 if (err && err != EOPNOTSUPP)
142 return err;
143 }
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500144 return 0;
145}
146
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400147static int dsa_switch_fdb_add(struct dsa_switch *ds,
148 struct dsa_notifier_fdb_info *info)
149{
Vivien Didelot31692412017-11-30 12:56:43 -0500150 int port = dsa_towards_port(ds, info->sw_index, info->port);
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400151
Arkadi Sharshevsky1b6dd552017-08-06 16:15:40 +0300152 if (!ds->ops->port_fdb_add)
153 return -EOPNOTSUPP;
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400154
Vivien Didelot31692412017-11-30 12:56:43 -0500155 return ds->ops->port_fdb_add(ds, port, info->addr, info->vid);
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400156}
157
158static int dsa_switch_fdb_del(struct dsa_switch *ds,
159 struct dsa_notifier_fdb_info *info)
160{
Vivien Didelot31692412017-11-30 12:56:43 -0500161 int port = dsa_towards_port(ds, info->sw_index, info->port);
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400162
163 if (!ds->ops->port_fdb_del)
164 return -EOPNOTSUPP;
165
Vivien Didelot31692412017-11-30 12:56:43 -0500166 return ds->ops->port_fdb_del(ds, port, info->addr, info->vid);
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400167}
168
Vivien Didelote65d45c2019-08-25 13:25:15 -0400169static bool dsa_switch_mdb_match(struct dsa_switch *ds, int port,
170 struct dsa_notifier_mdb_info *info)
171{
172 if (ds->index == info->sw_index && port == info->port)
173 return true;
174
175 if (dsa_is_dsa_port(ds, port))
176 return true;
177
178 return false;
179}
180
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200181static int dsa_switch_mdb_add(struct dsa_switch *ds,
182 struct dsa_notifier_mdb_info *info)
Vivien Didelote6db98d2017-11-30 11:24:00 -0500183{
Vladimir Olteana52b2da2021-01-09 02:01:52 +0200184 int err = 0;
185 int port;
Vivien Didelote6db98d2017-11-30 11:24:00 -0500186
Vladimir Olteana52b2da2021-01-09 02:01:52 +0200187 if (!ds->ops->port_mdb_add)
Vivien Didelote6db98d2017-11-30 11:24:00 -0500188 return -EOPNOTSUPP;
189
Vivien Didelote65d45c2019-08-25 13:25:15 -0400190 for (port = 0; port < ds->num_ports; port++) {
191 if (dsa_switch_mdb_match(ds, port, info)) {
Vladimir Olteana52b2da2021-01-09 02:01:52 +0200192 err = ds->ops->port_mdb_add(ds, port, info->mdb);
Vivien Didelote65d45c2019-08-25 13:25:15 -0400193 if (err)
Vladimir Olteana52b2da2021-01-09 02:01:52 +0200194 break;
Vivien Didelote65d45c2019-08-25 13:25:15 -0400195 }
Vivien Didelote6db98d2017-11-30 11:24:00 -0500196 }
197
Vladimir Olteana52b2da2021-01-09 02:01:52 +0200198 return err;
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400199}
200
201static int dsa_switch_mdb_del(struct dsa_switch *ds,
202 struct dsa_notifier_mdb_info *info)
203{
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400204 if (!ds->ops->port_mdb_del)
205 return -EOPNOTSUPP;
206
Vivien Didelota1a6b7e2017-06-15 16:14:48 -0400207 if (ds->index == info->sw_index)
Vivien Didelote65d45c2019-08-25 13:25:15 -0400208 return ds->ops->port_mdb_del(ds, info->port, info->mdb);
Vivien Didelota1a6b7e2017-06-15 16:14:48 -0400209
210 return 0;
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400211}
212
Vivien Didelote65d45c2019-08-25 13:25:15 -0400213static bool dsa_switch_vlan_match(struct dsa_switch *ds, int port,
214 struct dsa_notifier_vlan_info *info)
215{
216 if (ds->index == info->sw_index && port == info->port)
217 return true;
218
Vivien Didelot7e1741b2019-08-25 13:25:19 -0400219 if (dsa_is_dsa_port(ds, port))
Vivien Didelote65d45c2019-08-25 13:25:15 -0400220 return true;
221
222 return false;
223}
224
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200225static int dsa_switch_vlan_add(struct dsa_switch *ds,
226 struct dsa_notifier_vlan_info *info)
Vivien Didelot9c428c52017-11-30 11:23:59 -0500227{
228 int port, err;
229
230 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
231 return -EOPNOTSUPP;
232
Vivien Didelote65d45c2019-08-25 13:25:15 -0400233 for (port = 0; port < ds->num_ports; port++) {
234 if (dsa_switch_vlan_match(ds, port, info)) {
Vivien Didelote65d45c2019-08-25 13:25:15 -0400235 err = ds->ops->port_vlan_prepare(ds, port, info->vlan);
236 if (err)
237 return err;
238 }
Vivien Didelot9c428c52017-11-30 11:23:59 -0500239 }
240
Vivien Didelotb2f81d32017-06-07 18:12:15 -0400241 for (port = 0; port < ds->num_ports; port++)
Vivien Didelote65d45c2019-08-25 13:25:15 -0400242 if (dsa_switch_vlan_match(ds, port, info))
243 ds->ops->port_vlan_add(ds, port, info->vlan);
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400244
245 return 0;
246}
247
248static int dsa_switch_vlan_del(struct dsa_switch *ds,
249 struct dsa_notifier_vlan_info *info)
250{
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400251 if (!ds->ops->port_vlan_del)
252 return -EOPNOTSUPP;
253
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400254 if (ds->index == info->sw_index)
Vivien Didelote65d45c2019-08-25 13:25:15 -0400255 return ds->ops->port_vlan_del(ds, info->port, info->vlan);
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400256
Vivien Didelot7e1741b2019-08-25 13:25:19 -0400257 /* Do not deprogram the DSA links as they may be used as conduit
258 * for other VLAN members in the fabric.
259 */
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400260 return 0;
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400261}
262
Vivien Didelotf515f192017-02-03 13:20:20 -0500263static int dsa_switch_event(struct notifier_block *nb,
264 unsigned long event, void *info)
265{
266 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
267 int err;
268
269 switch (event) {
Vivien Didelot1faabf72017-05-19 17:00:52 -0400270 case DSA_NOTIFIER_AGEING_TIME:
271 err = dsa_switch_ageing_time(ds, info);
272 break;
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500273 case DSA_NOTIFIER_BRIDGE_JOIN:
274 err = dsa_switch_bridge_join(ds, info);
275 break;
276 case DSA_NOTIFIER_BRIDGE_LEAVE:
277 err = dsa_switch_bridge_leave(ds, info);
278 break;
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400279 case DSA_NOTIFIER_FDB_ADD:
280 err = dsa_switch_fdb_add(ds, info);
281 break;
282 case DSA_NOTIFIER_FDB_DEL:
283 err = dsa_switch_fdb_del(ds, info);
284 break;
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400285 case DSA_NOTIFIER_MDB_ADD:
286 err = dsa_switch_mdb_add(ds, info);
287 break;
288 case DSA_NOTIFIER_MDB_DEL:
289 err = dsa_switch_mdb_del(ds, info);
290 break;
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400291 case DSA_NOTIFIER_VLAN_ADD:
292 err = dsa_switch_vlan_add(ds, info);
293 break;
294 case DSA_NOTIFIER_VLAN_DEL:
295 err = dsa_switch_vlan_del(ds, info);
296 break;
Vladimir Olteanbfcb8132020-03-27 21:55:42 +0200297 case DSA_NOTIFIER_MTU:
298 err = dsa_switch_mtu(ds, info);
299 break;
Vivien Didelotf515f192017-02-03 13:20:20 -0500300 default:
301 err = -EOPNOTSUPP;
302 break;
303 }
304
305 /* Non-switchdev operations cannot be rolled back. If a DSA driver
306 * returns an error during the chained call, switch chips may be in an
307 * inconsistent state.
308 */
309 if (err)
310 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
311 event, err);
312
313 return notifier_from_errno(err);
314}
315
316int dsa_switch_register_notifier(struct dsa_switch *ds)
317{
318 ds->nb.notifier_call = dsa_switch_event;
319
320 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
321}
322
323void dsa_switch_unregister_notifier(struct dsa_switch *ds)
324{
325 int err;
326
327 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
328 if (err)
329 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
330}