blob: df4abe897ed61cef1a06c354cfcedfcc071deb23 [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;
36 struct switchdev_trans *trans = info->trans;
37
Vivien Didelot1faabf72017-05-19 17:00:52 -040038 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
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050055static 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
Vivien Didelot40ef2c92017-03-30 17:37:14 -040061 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);
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050064
65 return 0;
66}
67
68static int dsa_switch_bridge_leave(struct dsa_switch *ds,
69 struct dsa_notifier_bridge_info *info)
70{
Vladimir Olteand371b7c2019-04-28 21:45:46 +030071 bool unset_vlan_filtering = br_vlan_enabled(info->br);
72 int err, i;
73
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050074 if (ds->index == info->sw_index && ds->ops->port_bridge_leave)
75 ds->ops->port_bridge_leave(ds, info->port, info->br);
76
Vivien Didelot40ef2c92017-03-30 17:37:14 -040077 if (ds->index != info->sw_index && ds->ops->crosschip_bridge_leave)
78 ds->ops->crosschip_bridge_leave(ds, info->sw_index, info->port,
79 info->br);
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050080
Vladimir Olteand371b7c2019-04-28 21:45:46 +030081 /* 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.
87 */
88 if (unset_vlan_filtering && ds->vlan_filtering_is_global) {
89 for (i = 0; i < ds->num_ports; i++) {
90 if (i == info->port)
91 continue;
92 if (dsa_to_port(ds, i)->bridge_dev == info->br) {
93 unset_vlan_filtering = false;
94 break;
95 }
96 }
97 }
98 if (unset_vlan_filtering) {
99 struct switchdev_trans trans = {0};
100
Vivien Didelot68bb8ea2019-10-21 16:51:15 -0400101 err = dsa_port_vlan_filtering(dsa_to_port(ds, info->port),
Vladimir Olteand371b7c2019-04-28 21:45:46 +0300102 false, &trans);
103 if (err && err != EOPNOTSUPP)
104 return err;
105 }
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500106 return 0;
107}
108
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400109static int dsa_switch_fdb_add(struct dsa_switch *ds,
110 struct dsa_notifier_fdb_info *info)
111{
Vivien Didelot31692412017-11-30 12:56:43 -0500112 int port = dsa_towards_port(ds, info->sw_index, info->port);
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400113
Arkadi Sharshevsky1b6dd552017-08-06 16:15:40 +0300114 if (!ds->ops->port_fdb_add)
115 return -EOPNOTSUPP;
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400116
Vivien Didelot31692412017-11-30 12:56:43 -0500117 return ds->ops->port_fdb_add(ds, port, info->addr, info->vid);
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400118}
119
120static int dsa_switch_fdb_del(struct dsa_switch *ds,
121 struct dsa_notifier_fdb_info *info)
122{
Vivien Didelot31692412017-11-30 12:56:43 -0500123 int port = dsa_towards_port(ds, info->sw_index, info->port);
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400124
125 if (!ds->ops->port_fdb_del)
126 return -EOPNOTSUPP;
127
Vivien Didelot31692412017-11-30 12:56:43 -0500128 return ds->ops->port_fdb_del(ds, port, info->addr, info->vid);
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400129}
130
Vivien Didelote65d45c2019-08-25 13:25:15 -0400131static bool dsa_switch_mdb_match(struct dsa_switch *ds, int port,
132 struct dsa_notifier_mdb_info *info)
133{
134 if (ds->index == info->sw_index && port == info->port)
135 return true;
136
137 if (dsa_is_dsa_port(ds, port))
138 return true;
139
140 return false;
141}
142
143static int dsa_switch_mdb_prepare(struct dsa_switch *ds,
144 struct dsa_notifier_mdb_info *info)
Vivien Didelote6db98d2017-11-30 11:24:00 -0500145{
146 int port, err;
147
148 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
149 return -EOPNOTSUPP;
150
Vivien Didelote65d45c2019-08-25 13:25:15 -0400151 for (port = 0; port < ds->num_ports; port++) {
152 if (dsa_switch_mdb_match(ds, port, info)) {
153 err = ds->ops->port_mdb_prepare(ds, port, info->mdb);
154 if (err)
155 return err;
156 }
Vivien Didelote6db98d2017-11-30 11:24:00 -0500157 }
158
159 return 0;
160}
161
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400162static int dsa_switch_mdb_add(struct dsa_switch *ds,
163 struct dsa_notifier_mdb_info *info)
164{
Vivien Didelote6db98d2017-11-30 11:24:00 -0500165 int port;
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400166
Vivien Didelote65d45c2019-08-25 13:25:15 -0400167 if (switchdev_trans_ph_prepare(info->trans))
168 return dsa_switch_mdb_prepare(ds, info);
169
170 if (!ds->ops->port_mdb_add)
171 return 0;
172
Vivien Didelota1a6b7e2017-06-15 16:14:48 -0400173 for (port = 0; port < ds->num_ports; port++)
Vivien Didelote65d45c2019-08-25 13:25:15 -0400174 if (dsa_switch_mdb_match(ds, port, info))
175 ds->ops->port_mdb_add(ds, port, info->mdb);
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400176
177 return 0;
178}
179
180static int dsa_switch_mdb_del(struct dsa_switch *ds,
181 struct dsa_notifier_mdb_info *info)
182{
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400183 if (!ds->ops->port_mdb_del)
184 return -EOPNOTSUPP;
185
Vivien Didelota1a6b7e2017-06-15 16:14:48 -0400186 if (ds->index == info->sw_index)
Vivien Didelote65d45c2019-08-25 13:25:15 -0400187 return ds->ops->port_mdb_del(ds, info->port, info->mdb);
Vivien Didelota1a6b7e2017-06-15 16:14:48 -0400188
189 return 0;
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400190}
191
Florian Fainelli061f6a52019-02-20 14:35:39 -0800192static int dsa_port_vlan_device_check(struct net_device *vlan_dev,
193 int vlan_dev_vid,
194 void *arg)
195{
196 struct switchdev_obj_port_vlan *vlan = arg;
197 u16 vid;
198
199 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
200 if (vid == vlan_dev_vid)
201 return -EBUSY;
202 }
203
204 return 0;
205}
206
207static int dsa_port_vlan_check(struct dsa_switch *ds, int port,
208 const struct switchdev_obj_port_vlan *vlan)
209{
210 const struct dsa_port *dp = dsa_to_port(ds, port);
211 int err = 0;
212
213 /* Device is not bridged, let it proceed with the VLAN device
214 * creation.
215 */
216 if (!dp->bridge_dev)
217 return err;
218
Vladimir Oltean85478d72019-04-28 21:45:42 +0300219 /* dsa_slave_vlan_rx_{add,kill}_vid() cannot use the prepare phase and
Florian Fainelli061f6a52019-02-20 14:35:39 -0800220 * already checks whether there is an overlapping bridge VLAN entry
221 * with the same VID, so here we only need to check that if we are
222 * adding a bridge VLAN entry there is not an overlapping VLAN device
223 * claiming that VID.
224 */
225 return vlan_for_each(dp->slave, dsa_port_vlan_device_check,
226 (void *)vlan);
227}
228
Vivien Didelote65d45c2019-08-25 13:25:15 -0400229static bool dsa_switch_vlan_match(struct dsa_switch *ds, int port,
230 struct dsa_notifier_vlan_info *info)
231{
232 if (ds->index == info->sw_index && port == info->port)
233 return true;
234
Vivien Didelot7e1741b2019-08-25 13:25:19 -0400235 if (dsa_is_dsa_port(ds, port))
Vivien Didelote65d45c2019-08-25 13:25:15 -0400236 return true;
237
238 return false;
239}
240
241static int dsa_switch_vlan_prepare(struct dsa_switch *ds,
242 struct dsa_notifier_vlan_info *info)
Vivien Didelot9c428c52017-11-30 11:23:59 -0500243{
244 int port, err;
245
246 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
247 return -EOPNOTSUPP;
248
Vivien Didelote65d45c2019-08-25 13:25:15 -0400249 for (port = 0; port < ds->num_ports; port++) {
250 if (dsa_switch_vlan_match(ds, port, info)) {
251 err = dsa_port_vlan_check(ds, port, info->vlan);
252 if (err)
253 return err;
Florian Fainelli061f6a52019-02-20 14:35:39 -0800254
Vivien Didelote65d45c2019-08-25 13:25:15 -0400255 err = ds->ops->port_vlan_prepare(ds, port, info->vlan);
256 if (err)
257 return err;
258 }
Vivien Didelot9c428c52017-11-30 11:23:59 -0500259 }
260
261 return 0;
262}
263
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400264static int dsa_switch_vlan_add(struct dsa_switch *ds,
265 struct dsa_notifier_vlan_info *info)
266{
Vivien Didelot9c428c52017-11-30 11:23:59 -0500267 int port;
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400268
Vivien Didelote65d45c2019-08-25 13:25:15 -0400269 if (switchdev_trans_ph_prepare(info->trans))
270 return dsa_switch_vlan_prepare(ds, info);
271
272 if (!ds->ops->port_vlan_add)
273 return 0;
274
Vivien Didelotb2f81d32017-06-07 18:12:15 -0400275 for (port = 0; port < ds->num_ports; port++)
Vivien Didelote65d45c2019-08-25 13:25:15 -0400276 if (dsa_switch_vlan_match(ds, port, info))
277 ds->ops->port_vlan_add(ds, port, info->vlan);
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400278
279 return 0;
280}
281
282static int dsa_switch_vlan_del(struct dsa_switch *ds,
283 struct dsa_notifier_vlan_info *info)
284{
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400285 if (!ds->ops->port_vlan_del)
286 return -EOPNOTSUPP;
287
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400288 if (ds->index == info->sw_index)
Vivien Didelote65d45c2019-08-25 13:25:15 -0400289 return ds->ops->port_vlan_del(ds, info->port, info->vlan);
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400290
Vivien Didelot7e1741b2019-08-25 13:25:19 -0400291 /* Do not deprogram the DSA links as they may be used as conduit
292 * for other VLAN members in the fabric.
293 */
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400294 return 0;
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400295}
296
Vivien Didelotf515f192017-02-03 13:20:20 -0500297static int dsa_switch_event(struct notifier_block *nb,
298 unsigned long event, void *info)
299{
300 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
301 int err;
302
303 switch (event) {
Vivien Didelot1faabf72017-05-19 17:00:52 -0400304 case DSA_NOTIFIER_AGEING_TIME:
305 err = dsa_switch_ageing_time(ds, info);
306 break;
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500307 case DSA_NOTIFIER_BRIDGE_JOIN:
308 err = dsa_switch_bridge_join(ds, info);
309 break;
310 case DSA_NOTIFIER_BRIDGE_LEAVE:
311 err = dsa_switch_bridge_leave(ds, info);
312 break;
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400313 case DSA_NOTIFIER_FDB_ADD:
314 err = dsa_switch_fdb_add(ds, info);
315 break;
316 case DSA_NOTIFIER_FDB_DEL:
317 err = dsa_switch_fdb_del(ds, info);
318 break;
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400319 case DSA_NOTIFIER_MDB_ADD:
320 err = dsa_switch_mdb_add(ds, info);
321 break;
322 case DSA_NOTIFIER_MDB_DEL:
323 err = dsa_switch_mdb_del(ds, info);
324 break;
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400325 case DSA_NOTIFIER_VLAN_ADD:
326 err = dsa_switch_vlan_add(ds, info);
327 break;
328 case DSA_NOTIFIER_VLAN_DEL:
329 err = dsa_switch_vlan_del(ds, info);
330 break;
Vivien Didelotf515f192017-02-03 13:20:20 -0500331 default:
332 err = -EOPNOTSUPP;
333 break;
334 }
335
336 /* Non-switchdev operations cannot be rolled back. If a DSA driver
337 * returns an error during the chained call, switch chips may be in an
338 * inconsistent state.
339 */
340 if (err)
341 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
342 event, err);
343
344 return notifier_from_errno(err);
345}
346
347int dsa_switch_register_notifier(struct dsa_switch *ds)
348{
349 ds->nb.notifier_call = dsa_switch_event;
350
351 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
352}
353
354void dsa_switch_unregister_notifier(struct dsa_switch *ds)
355{
356 int err;
357
358 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
359 if (err)
360 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
361}