blob: e1fae969aa737cc88556e5c5b0fb7f336571c3ce [file] [log] [blame]
Vivien Didelotf515f192017-02-03 13:20:20 -05001/*
2 * Handling of a single switch chip, part of a switch fabric
3 *
Vivien Didelot4333d612017-03-28 15:10:36 -04004 * Copyright (c) 2017 Savoir-faire Linux Inc.
5 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
Vivien Didelotf515f192017-02-03 13:20:20 -05006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13#include <linux/netdevice.h>
14#include <linux/notifier.h>
Florian Fainelli061f6a52019-02-20 14:35:39 -080015#include <linux/if_vlan.h>
Vivien Didelot1faabf72017-05-19 17:00:52 -040016#include <net/switchdev.h>
Vivien Didelotea5dd342017-05-17 15:46:03 -040017
18#include "dsa_priv.h"
Vivien Didelotf515f192017-02-03 13:20:20 -050019
Vivien Didelot1faabf72017-05-19 17:00:52 -040020static unsigned int dsa_switch_fastest_ageing_time(struct dsa_switch *ds,
21 unsigned int ageing_time)
22{
23 int i;
24
25 for (i = 0; i < ds->num_ports; ++i) {
26 struct dsa_port *dp = &ds->ports[i];
27
28 if (dp->ageing_time && dp->ageing_time < ageing_time)
29 ageing_time = dp->ageing_time;
30 }
31
32 return ageing_time;
33}
34
35static int dsa_switch_ageing_time(struct dsa_switch *ds,
36 struct dsa_notifier_ageing_time_info *info)
37{
38 unsigned int ageing_time = info->ageing_time;
39 struct switchdev_trans *trans = info->trans;
40
Vivien Didelot1faabf72017-05-19 17:00:52 -040041 if (switchdev_trans_ph_prepare(trans)) {
42 if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
43 return -ERANGE;
44 if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
45 return -ERANGE;
46 return 0;
47 }
48
49 /* Program the fastest ageing time in case of multiple bridges */
50 ageing_time = dsa_switch_fastest_ageing_time(ds, ageing_time);
51
52 if (ds->ops->set_ageing_time)
53 return ds->ops->set_ageing_time(ds, ageing_time);
54
55 return 0;
56}
57
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050058static int dsa_switch_bridge_join(struct dsa_switch *ds,
59 struct dsa_notifier_bridge_info *info)
60{
61 if (ds->index == info->sw_index && ds->ops->port_bridge_join)
62 return ds->ops->port_bridge_join(ds, info->port, info->br);
63
Vivien Didelot40ef2c92017-03-30 17:37:14 -040064 if (ds->index != info->sw_index && ds->ops->crosschip_bridge_join)
65 return ds->ops->crosschip_bridge_join(ds, info->sw_index,
66 info->port, info->br);
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050067
68 return 0;
69}
70
71static int dsa_switch_bridge_leave(struct dsa_switch *ds,
72 struct dsa_notifier_bridge_info *info)
73{
74 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
81 return 0;
82}
83
Vivien Didelot685fb6a2017-05-19 17:00:53 -040084static int dsa_switch_fdb_add(struct dsa_switch *ds,
85 struct dsa_notifier_fdb_info *info)
86{
Vivien Didelot31692412017-11-30 12:56:43 -050087 int port = dsa_towards_port(ds, info->sw_index, info->port);
Vivien Didelot685fb6a2017-05-19 17:00:53 -040088
Arkadi Sharshevsky1b6dd552017-08-06 16:15:40 +030089 if (!ds->ops->port_fdb_add)
90 return -EOPNOTSUPP;
Vivien Didelot685fb6a2017-05-19 17:00:53 -040091
Vivien Didelot31692412017-11-30 12:56:43 -050092 return ds->ops->port_fdb_add(ds, port, info->addr, info->vid);
Vivien Didelot685fb6a2017-05-19 17:00:53 -040093}
94
95static int dsa_switch_fdb_del(struct dsa_switch *ds,
96 struct dsa_notifier_fdb_info *info)
97{
Vivien Didelot31692412017-11-30 12:56:43 -050098 int port = dsa_towards_port(ds, info->sw_index, info->port);
Vivien Didelot685fb6a2017-05-19 17:00:53 -040099
100 if (!ds->ops->port_fdb_del)
101 return -EOPNOTSUPP;
102
Vivien Didelot31692412017-11-30 12:56:43 -0500103 return ds->ops->port_fdb_del(ds, port, info->addr, info->vid);
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400104}
105
Vivien Didelote6db98d2017-11-30 11:24:00 -0500106static int
107dsa_switch_mdb_prepare_bitmap(struct dsa_switch *ds,
108 const struct switchdev_obj_port_mdb *mdb,
109 const unsigned long *bitmap)
110{
111 int port, err;
112
113 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
114 return -EOPNOTSUPP;
115
116 for_each_set_bit(port, bitmap, ds->num_ports) {
117 err = ds->ops->port_mdb_prepare(ds, port, mdb);
118 if (err)
119 return err;
120 }
121
122 return 0;
123}
124
125static void dsa_switch_mdb_add_bitmap(struct dsa_switch *ds,
126 const struct switchdev_obj_port_mdb *mdb,
127 const unsigned long *bitmap)
128{
129 int port;
130
131 for_each_set_bit(port, bitmap, ds->num_ports)
132 ds->ops->port_mdb_add(ds, port, mdb);
133}
134
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400135static int dsa_switch_mdb_add(struct dsa_switch *ds,
136 struct dsa_notifier_mdb_info *info)
137{
138 const struct switchdev_obj_port_mdb *mdb = info->mdb;
139 struct switchdev_trans *trans = info->trans;
Vivien Didelote6db98d2017-11-30 11:24:00 -0500140 int port;
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400141
Vivien Didelota1a6b7e2017-06-15 16:14:48 -0400142 /* Build a mask of Multicast group members */
Salvatore Mesoraca0015b802018-07-16 21:10:34 -0700143 bitmap_zero(ds->bitmap, ds->num_ports);
Vivien Didelota1a6b7e2017-06-15 16:14:48 -0400144 if (ds->index == info->sw_index)
Salvatore Mesoraca0015b802018-07-16 21:10:34 -0700145 set_bit(info->port, ds->bitmap);
Vivien Didelota1a6b7e2017-06-15 16:14:48 -0400146 for (port = 0; port < ds->num_ports; port++)
Andrew Lunnae451022017-11-09 23:11:02 +0100147 if (dsa_is_dsa_port(ds, port))
Salvatore Mesoraca0015b802018-07-16 21:10:34 -0700148 set_bit(port, ds->bitmap);
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400149
Vivien Didelote6db98d2017-11-30 11:24:00 -0500150 if (switchdev_trans_ph_prepare(trans))
Salvatore Mesoraca0015b802018-07-16 21:10:34 -0700151 return dsa_switch_mdb_prepare_bitmap(ds, mdb, ds->bitmap);
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400152
Salvatore Mesoraca0015b802018-07-16 21:10:34 -0700153 dsa_switch_mdb_add_bitmap(ds, mdb, ds->bitmap);
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400154
155 return 0;
156}
157
158static int dsa_switch_mdb_del(struct dsa_switch *ds,
159 struct dsa_notifier_mdb_info *info)
160{
161 const struct switchdev_obj_port_mdb *mdb = info->mdb;
162
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400163 if (!ds->ops->port_mdb_del)
164 return -EOPNOTSUPP;
165
Vivien Didelota1a6b7e2017-06-15 16:14:48 -0400166 if (ds->index == info->sw_index)
167 return ds->ops->port_mdb_del(ds, info->port, mdb);
168
169 return 0;
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400170}
171
Florian Fainelli061f6a52019-02-20 14:35:39 -0800172static int dsa_port_vlan_device_check(struct net_device *vlan_dev,
173 int vlan_dev_vid,
174 void *arg)
175{
176 struct switchdev_obj_port_vlan *vlan = arg;
177 u16 vid;
178
179 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
180 if (vid == vlan_dev_vid)
181 return -EBUSY;
182 }
183
184 return 0;
185}
186
187static int dsa_port_vlan_check(struct dsa_switch *ds, int port,
188 const struct switchdev_obj_port_vlan *vlan)
189{
190 const struct dsa_port *dp = dsa_to_port(ds, port);
191 int err = 0;
192
193 /* Device is not bridged, let it proceed with the VLAN device
194 * creation.
195 */
196 if (!dp->bridge_dev)
197 return err;
198
199 /* dsa_slave_vlan_rx_{add,kill}_vid() cannot use the prepare pharse and
200 * already checks whether there is an overlapping bridge VLAN entry
201 * with the same VID, so here we only need to check that if we are
202 * adding a bridge VLAN entry there is not an overlapping VLAN device
203 * claiming that VID.
204 */
205 return vlan_for_each(dp->slave, dsa_port_vlan_device_check,
206 (void *)vlan);
207}
208
Vivien Didelot9c428c52017-11-30 11:23:59 -0500209static int
210dsa_switch_vlan_prepare_bitmap(struct dsa_switch *ds,
211 const struct switchdev_obj_port_vlan *vlan,
212 const unsigned long *bitmap)
213{
214 int port, err;
215
216 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
217 return -EOPNOTSUPP;
218
219 for_each_set_bit(port, bitmap, ds->num_ports) {
Florian Fainelli061f6a52019-02-20 14:35:39 -0800220 err = dsa_port_vlan_check(ds, port, vlan);
221 if (err)
222 return err;
223
Vivien Didelot9c428c52017-11-30 11:23:59 -0500224 err = ds->ops->port_vlan_prepare(ds, port, vlan);
225 if (err)
226 return err;
227 }
228
229 return 0;
230}
231
232static void
233dsa_switch_vlan_add_bitmap(struct dsa_switch *ds,
234 const struct switchdev_obj_port_vlan *vlan,
235 const unsigned long *bitmap)
236{
237 int port;
238
239 for_each_set_bit(port, bitmap, ds->num_ports)
240 ds->ops->port_vlan_add(ds, port, vlan);
241}
242
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400243static int dsa_switch_vlan_add(struct dsa_switch *ds,
244 struct dsa_notifier_vlan_info *info)
245{
246 const struct switchdev_obj_port_vlan *vlan = info->vlan;
247 struct switchdev_trans *trans = info->trans;
Vivien Didelot9c428c52017-11-30 11:23:59 -0500248 int port;
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400249
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400250 /* Build a mask of VLAN members */
Salvatore Mesoraca0015b802018-07-16 21:10:34 -0700251 bitmap_zero(ds->bitmap, ds->num_ports);
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400252 if (ds->index == info->sw_index)
Salvatore Mesoraca0015b802018-07-16 21:10:34 -0700253 set_bit(info->port, ds->bitmap);
Vivien Didelotb2f81d32017-06-07 18:12:15 -0400254 for (port = 0; port < ds->num_ports; port++)
255 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
Salvatore Mesoraca0015b802018-07-16 21:10:34 -0700256 set_bit(port, ds->bitmap);
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400257
Vivien Didelot9c428c52017-11-30 11:23:59 -0500258 if (switchdev_trans_ph_prepare(trans))
Salvatore Mesoraca0015b802018-07-16 21:10:34 -0700259 return dsa_switch_vlan_prepare_bitmap(ds, vlan, ds->bitmap);
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400260
Salvatore Mesoraca0015b802018-07-16 21:10:34 -0700261 dsa_switch_vlan_add_bitmap(ds, vlan, ds->bitmap);
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400262
263 return 0;
264}
265
266static int dsa_switch_vlan_del(struct dsa_switch *ds,
267 struct dsa_notifier_vlan_info *info)
268{
269 const struct switchdev_obj_port_vlan *vlan = info->vlan;
270
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400271 if (!ds->ops->port_vlan_del)
272 return -EOPNOTSUPP;
273
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400274 if (ds->index == info->sw_index)
275 return ds->ops->port_vlan_del(ds, info->port, vlan);
276
277 return 0;
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400278}
279
Vivien Didelotf515f192017-02-03 13:20:20 -0500280static int dsa_switch_event(struct notifier_block *nb,
281 unsigned long event, void *info)
282{
283 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
284 int err;
285
286 switch (event) {
Vivien Didelot1faabf72017-05-19 17:00:52 -0400287 case DSA_NOTIFIER_AGEING_TIME:
288 err = dsa_switch_ageing_time(ds, info);
289 break;
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500290 case DSA_NOTIFIER_BRIDGE_JOIN:
291 err = dsa_switch_bridge_join(ds, info);
292 break;
293 case DSA_NOTIFIER_BRIDGE_LEAVE:
294 err = dsa_switch_bridge_leave(ds, info);
295 break;
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400296 case DSA_NOTIFIER_FDB_ADD:
297 err = dsa_switch_fdb_add(ds, info);
298 break;
299 case DSA_NOTIFIER_FDB_DEL:
300 err = dsa_switch_fdb_del(ds, info);
301 break;
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400302 case DSA_NOTIFIER_MDB_ADD:
303 err = dsa_switch_mdb_add(ds, info);
304 break;
305 case DSA_NOTIFIER_MDB_DEL:
306 err = dsa_switch_mdb_del(ds, info);
307 break;
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400308 case DSA_NOTIFIER_VLAN_ADD:
309 err = dsa_switch_vlan_add(ds, info);
310 break;
311 case DSA_NOTIFIER_VLAN_DEL:
312 err = dsa_switch_vlan_del(ds, info);
313 break;
Vivien Didelotf515f192017-02-03 13:20:20 -0500314 default:
315 err = -EOPNOTSUPP;
316 break;
317 }
318
319 /* Non-switchdev operations cannot be rolled back. If a DSA driver
320 * returns an error during the chained call, switch chips may be in an
321 * inconsistent state.
322 */
323 if (err)
324 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
325 event, err);
326
327 return notifier_from_errno(err);
328}
329
330int dsa_switch_register_notifier(struct dsa_switch *ds)
331{
332 ds->nb.notifier_call = dsa_switch_event;
333
334 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
335}
336
337void dsa_switch_unregister_notifier(struct dsa_switch *ds)
338{
339 int err;
340
341 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
342 if (err)
343 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
344}