blob: f3c32ff552b35de163a235b29470952adb5f03ad [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
Vladimir Olteanbfcb8132020-03-27 21:55:42 +020055static bool dsa_switch_mtu_match(struct dsa_switch *ds, int port,
56 struct dsa_notifier_mtu_info *info)
57{
58 if (ds->index == info->sw_index)
59 return (port == info->port) || dsa_is_dsa_port(ds, port);
60
61 if (!info->propagate_upstream)
62 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{
92 if (ds->index == info->sw_index && ds->ops->port_bridge_join)
93 return ds->ops->port_bridge_join(ds, info->port, info->br);
94
Vivien Didelot40ef2c92017-03-30 17:37:14 -040095 if (ds->index != info->sw_index && ds->ops->crosschip_bridge_join)
96 return ds->ops->crosschip_bridge_join(ds, info->sw_index,
97 info->port, info->br);
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050098
99 return 0;
100}
101
102static int dsa_switch_bridge_leave(struct dsa_switch *ds,
103 struct dsa_notifier_bridge_info *info)
104{
Vladimir Olteand371b7c2019-04-28 21:45:46 +0300105 bool unset_vlan_filtering = br_vlan_enabled(info->br);
106 int err, i;
107
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500108 if (ds->index == info->sw_index && ds->ops->port_bridge_leave)
109 ds->ops->port_bridge_leave(ds, info->port, info->br);
110
Vivien Didelot40ef2c92017-03-30 17:37:14 -0400111 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);
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500114
Vladimir Olteand371b7c2019-04-28 21:45:46 +0300115 /* If the bridge was vlan_filtering, the bridge core doesn't trigger an
116 * event for changing vlan_filtering setting upon slave ports leaving
117 * it. That is a good thing, because that lets us handle it and also
118 * handle the case where the switch's vlan_filtering setting is global
119 * (not per port). When that happens, the correct moment to trigger the
120 * vlan_filtering callback is only when the last port left this bridge.
121 */
122 if (unset_vlan_filtering && ds->vlan_filtering_is_global) {
123 for (i = 0; i < ds->num_ports; i++) {
124 if (i == info->port)
125 continue;
126 if (dsa_to_port(ds, i)->bridge_dev == info->br) {
127 unset_vlan_filtering = false;
128 break;
129 }
130 }
131 }
132 if (unset_vlan_filtering) {
133 struct switchdev_trans trans = {0};
134
Vivien Didelot68bb8ea2019-10-21 16:51:15 -0400135 err = dsa_port_vlan_filtering(dsa_to_port(ds, info->port),
Vladimir Olteand371b7c2019-04-28 21:45:46 +0300136 false, &trans);
137 if (err && err != EOPNOTSUPP)
138 return err;
139 }
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500140 return 0;
141}
142
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400143static int dsa_switch_fdb_add(struct dsa_switch *ds,
144 struct dsa_notifier_fdb_info *info)
145{
Vivien Didelot31692412017-11-30 12:56:43 -0500146 int port = dsa_towards_port(ds, info->sw_index, info->port);
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400147
Arkadi Sharshevsky1b6dd552017-08-06 16:15:40 +0300148 if (!ds->ops->port_fdb_add)
149 return -EOPNOTSUPP;
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400150
Vivien Didelot31692412017-11-30 12:56:43 -0500151 return ds->ops->port_fdb_add(ds, port, info->addr, info->vid);
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400152}
153
154static int dsa_switch_fdb_del(struct dsa_switch *ds,
155 struct dsa_notifier_fdb_info *info)
156{
Vivien Didelot31692412017-11-30 12:56:43 -0500157 int port = dsa_towards_port(ds, info->sw_index, info->port);
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400158
159 if (!ds->ops->port_fdb_del)
160 return -EOPNOTSUPP;
161
Vivien Didelot31692412017-11-30 12:56:43 -0500162 return ds->ops->port_fdb_del(ds, port, info->addr, info->vid);
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400163}
164
Vivien Didelote65d45c2019-08-25 13:25:15 -0400165static bool dsa_switch_mdb_match(struct dsa_switch *ds, int port,
166 struct dsa_notifier_mdb_info *info)
167{
168 if (ds->index == info->sw_index && port == info->port)
169 return true;
170
171 if (dsa_is_dsa_port(ds, port))
172 return true;
173
174 return false;
175}
176
177static int dsa_switch_mdb_prepare(struct dsa_switch *ds,
178 struct dsa_notifier_mdb_info *info)
Vivien Didelote6db98d2017-11-30 11:24:00 -0500179{
180 int port, err;
181
182 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
183 return -EOPNOTSUPP;
184
Vivien Didelote65d45c2019-08-25 13:25:15 -0400185 for (port = 0; port < ds->num_ports; port++) {
186 if (dsa_switch_mdb_match(ds, port, info)) {
187 err = ds->ops->port_mdb_prepare(ds, port, info->mdb);
188 if (err)
189 return err;
190 }
Vivien Didelote6db98d2017-11-30 11:24:00 -0500191 }
192
193 return 0;
194}
195
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400196static int dsa_switch_mdb_add(struct dsa_switch *ds,
197 struct dsa_notifier_mdb_info *info)
198{
Vivien Didelote6db98d2017-11-30 11:24:00 -0500199 int port;
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400200
Vivien Didelote65d45c2019-08-25 13:25:15 -0400201 if (switchdev_trans_ph_prepare(info->trans))
202 return dsa_switch_mdb_prepare(ds, info);
203
204 if (!ds->ops->port_mdb_add)
205 return 0;
206
Vivien Didelota1a6b7e2017-06-15 16:14:48 -0400207 for (port = 0; port < ds->num_ports; port++)
Vivien Didelote65d45c2019-08-25 13:25:15 -0400208 if (dsa_switch_mdb_match(ds, port, info))
209 ds->ops->port_mdb_add(ds, port, info->mdb);
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400210
211 return 0;
212}
213
214static int dsa_switch_mdb_del(struct dsa_switch *ds,
215 struct dsa_notifier_mdb_info *info)
216{
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400217 if (!ds->ops->port_mdb_del)
218 return -EOPNOTSUPP;
219
Vivien Didelota1a6b7e2017-06-15 16:14:48 -0400220 if (ds->index == info->sw_index)
Vivien Didelote65d45c2019-08-25 13:25:15 -0400221 return ds->ops->port_mdb_del(ds, info->port, info->mdb);
Vivien Didelota1a6b7e2017-06-15 16:14:48 -0400222
223 return 0;
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400224}
225
Florian Fainelli061f6a52019-02-20 14:35:39 -0800226static int dsa_port_vlan_device_check(struct net_device *vlan_dev,
227 int vlan_dev_vid,
228 void *arg)
229{
230 struct switchdev_obj_port_vlan *vlan = arg;
231 u16 vid;
232
233 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
234 if (vid == vlan_dev_vid)
235 return -EBUSY;
236 }
237
238 return 0;
239}
240
241static int dsa_port_vlan_check(struct dsa_switch *ds, int port,
242 const struct switchdev_obj_port_vlan *vlan)
243{
244 const struct dsa_port *dp = dsa_to_port(ds, port);
245 int err = 0;
246
247 /* Device is not bridged, let it proceed with the VLAN device
248 * creation.
249 */
250 if (!dp->bridge_dev)
251 return err;
252
Vladimir Oltean85478d72019-04-28 21:45:42 +0300253 /* dsa_slave_vlan_rx_{add,kill}_vid() cannot use the prepare phase and
Florian Fainelli061f6a52019-02-20 14:35:39 -0800254 * already checks whether there is an overlapping bridge VLAN entry
255 * with the same VID, so here we only need to check that if we are
256 * adding a bridge VLAN entry there is not an overlapping VLAN device
257 * claiming that VID.
258 */
259 return vlan_for_each(dp->slave, dsa_port_vlan_device_check,
260 (void *)vlan);
261}
262
Vivien Didelote65d45c2019-08-25 13:25:15 -0400263static bool dsa_switch_vlan_match(struct dsa_switch *ds, int port,
264 struct dsa_notifier_vlan_info *info)
265{
266 if (ds->index == info->sw_index && port == info->port)
267 return true;
268
Vivien Didelot7e1741b2019-08-25 13:25:19 -0400269 if (dsa_is_dsa_port(ds, port))
Vivien Didelote65d45c2019-08-25 13:25:15 -0400270 return true;
271
272 return false;
273}
274
275static int dsa_switch_vlan_prepare(struct dsa_switch *ds,
276 struct dsa_notifier_vlan_info *info)
Vivien Didelot9c428c52017-11-30 11:23:59 -0500277{
278 int port, err;
279
280 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
281 return -EOPNOTSUPP;
282
Vivien Didelote65d45c2019-08-25 13:25:15 -0400283 for (port = 0; port < ds->num_ports; port++) {
284 if (dsa_switch_vlan_match(ds, port, info)) {
285 err = dsa_port_vlan_check(ds, port, info->vlan);
286 if (err)
287 return err;
Florian Fainelli061f6a52019-02-20 14:35:39 -0800288
Vivien Didelote65d45c2019-08-25 13:25:15 -0400289 err = ds->ops->port_vlan_prepare(ds, port, info->vlan);
290 if (err)
291 return err;
292 }
Vivien Didelot9c428c52017-11-30 11:23:59 -0500293 }
294
295 return 0;
296}
297
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400298static int dsa_switch_vlan_add(struct dsa_switch *ds,
299 struct dsa_notifier_vlan_info *info)
300{
Vivien Didelot9c428c52017-11-30 11:23:59 -0500301 int port;
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400302
Vivien Didelote65d45c2019-08-25 13:25:15 -0400303 if (switchdev_trans_ph_prepare(info->trans))
304 return dsa_switch_vlan_prepare(ds, info);
305
306 if (!ds->ops->port_vlan_add)
307 return 0;
308
Vivien Didelotb2f81d32017-06-07 18:12:15 -0400309 for (port = 0; port < ds->num_ports; port++)
Vivien Didelote65d45c2019-08-25 13:25:15 -0400310 if (dsa_switch_vlan_match(ds, port, info))
311 ds->ops->port_vlan_add(ds, port, info->vlan);
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400312
313 return 0;
314}
315
316static int dsa_switch_vlan_del(struct dsa_switch *ds,
317 struct dsa_notifier_vlan_info *info)
318{
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400319 if (!ds->ops->port_vlan_del)
320 return -EOPNOTSUPP;
321
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400322 if (ds->index == info->sw_index)
Vivien Didelote65d45c2019-08-25 13:25:15 -0400323 return ds->ops->port_vlan_del(ds, info->port, info->vlan);
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400324
Vivien Didelot7e1741b2019-08-25 13:25:19 -0400325 /* Do not deprogram the DSA links as they may be used as conduit
326 * for other VLAN members in the fabric.
327 */
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400328 return 0;
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400329}
330
Vivien Didelotf515f192017-02-03 13:20:20 -0500331static int dsa_switch_event(struct notifier_block *nb,
332 unsigned long event, void *info)
333{
334 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
335 int err;
336
337 switch (event) {
Vivien Didelot1faabf72017-05-19 17:00:52 -0400338 case DSA_NOTIFIER_AGEING_TIME:
339 err = dsa_switch_ageing_time(ds, info);
340 break;
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500341 case DSA_NOTIFIER_BRIDGE_JOIN:
342 err = dsa_switch_bridge_join(ds, info);
343 break;
344 case DSA_NOTIFIER_BRIDGE_LEAVE:
345 err = dsa_switch_bridge_leave(ds, info);
346 break;
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400347 case DSA_NOTIFIER_FDB_ADD:
348 err = dsa_switch_fdb_add(ds, info);
349 break;
350 case DSA_NOTIFIER_FDB_DEL:
351 err = dsa_switch_fdb_del(ds, info);
352 break;
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400353 case DSA_NOTIFIER_MDB_ADD:
354 err = dsa_switch_mdb_add(ds, info);
355 break;
356 case DSA_NOTIFIER_MDB_DEL:
357 err = dsa_switch_mdb_del(ds, info);
358 break;
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400359 case DSA_NOTIFIER_VLAN_ADD:
360 err = dsa_switch_vlan_add(ds, info);
361 break;
362 case DSA_NOTIFIER_VLAN_DEL:
363 err = dsa_switch_vlan_del(ds, info);
364 break;
Vladimir Olteanbfcb8132020-03-27 21:55:42 +0200365 case DSA_NOTIFIER_MTU:
366 err = dsa_switch_mtu(ds, info);
367 break;
Vivien Didelotf515f192017-02-03 13:20:20 -0500368 default:
369 err = -EOPNOTSUPP;
370 break;
371 }
372
373 /* Non-switchdev operations cannot be rolled back. If a DSA driver
374 * returns an error during the chained call, switch chips may be in an
375 * inconsistent state.
376 */
377 if (err)
378 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
379 event, err);
380
381 return notifier_from_errno(err);
382}
383
384int dsa_switch_register_notifier(struct dsa_switch *ds)
385{
386 ds->nb.notifier_call = dsa_switch_event;
387
388 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
389}
390
391void dsa_switch_unregister_notifier(struct dsa_switch *ds)
392{
393 int err;
394
395 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
396 if (err)
397 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
398}