]> git.ipfire.org Git - people/arne_f/kernel.git/blame - net/dsa/switch.c
net: dsa: return after mdb prepare phase
[people/arne_f/kernel.git] / net / dsa / switch.c
CommitLineData
f515f192
VD
1/*
2 * Handling of a single switch chip, part of a switch fabric
3 *
4333d619
VD
4 * Copyright (c) 2017 Savoir-faire Linux Inc.
5 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
f515f192
VD
6 *
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>
1faabf74 15#include <net/switchdev.h>
ea5dd34b
VD
16
17#include "dsa_priv.h"
f515f192 18
1faabf74
VD
19static unsigned int dsa_switch_fastest_ageing_time(struct dsa_switch *ds,
20 unsigned int ageing_time)
21{
22 int i;
23
24 for (i = 0; i < ds->num_ports; ++i) {
25 struct dsa_port *dp = &ds->ports[i];
26
27 if (dp->ageing_time && dp->ageing_time < ageing_time)
28 ageing_time = dp->ageing_time;
29 }
30
31 return ageing_time;
32}
33
34static int dsa_switch_ageing_time(struct dsa_switch *ds,
35 struct dsa_notifier_ageing_time_info *info)
36{
37 unsigned int ageing_time = info->ageing_time;
38 struct switchdev_trans *trans = info->trans;
39
1faabf74
VD
40 if (switchdev_trans_ph_prepare(trans)) {
41 if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
42 return -ERANGE;
43 if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
44 return -ERANGE;
45 return 0;
46 }
47
48 /* Program the fastest ageing time in case of multiple bridges */
49 ageing_time = dsa_switch_fastest_ageing_time(ds, ageing_time);
50
51 if (ds->ops->set_ageing_time)
52 return ds->ops->set_ageing_time(ds, ageing_time);
53
54 return 0;
55}
56
04d3a4c6
VD
57static int dsa_switch_bridge_join(struct dsa_switch *ds,
58 struct dsa_notifier_bridge_info *info)
59{
60 if (ds->index == info->sw_index && ds->ops->port_bridge_join)
61 return ds->ops->port_bridge_join(ds, info->port, info->br);
62
40ef2c93
VD
63 if (ds->index != info->sw_index && ds->ops->crosschip_bridge_join)
64 return ds->ops->crosschip_bridge_join(ds, info->sw_index,
65 info->port, info->br);
04d3a4c6
VD
66
67 return 0;
68}
69
70static int dsa_switch_bridge_leave(struct dsa_switch *ds,
71 struct dsa_notifier_bridge_info *info)
72{
73 if (ds->index == info->sw_index && ds->ops->port_bridge_leave)
74 ds->ops->port_bridge_leave(ds, info->port, info->br);
75
40ef2c93
VD
76 if (ds->index != info->sw_index && ds->ops->crosschip_bridge_leave)
77 ds->ops->crosschip_bridge_leave(ds, info->sw_index, info->port,
78 info->br);
04d3a4c6
VD
79
80 return 0;
81}
82
685fb6a4
VD
83static int dsa_switch_fdb_add(struct dsa_switch *ds,
84 struct dsa_notifier_fdb_info *info)
85{
685fb6a4
VD
86 /* Do not care yet about other switch chips of the fabric */
87 if (ds->index != info->sw_index)
88 return 0;
89
1b6dd556
AS
90 if (!ds->ops->port_fdb_add)
91 return -EOPNOTSUPP;
685fb6a4 92
2acf4e6a
AS
93 return ds->ops->port_fdb_add(ds, info->port, info->addr,
94 info->vid);
685fb6a4
VD
95}
96
97static int dsa_switch_fdb_del(struct dsa_switch *ds,
98 struct dsa_notifier_fdb_info *info)
99{
685fb6a4
VD
100 /* Do not care yet about other switch chips of the fabric */
101 if (ds->index != info->sw_index)
102 return 0;
103
104 if (!ds->ops->port_fdb_del)
105 return -EOPNOTSUPP;
106
2acf4e6a
AS
107 return ds->ops->port_fdb_del(ds, info->port, info->addr,
108 info->vid);
685fb6a4
VD
109}
110
8ae5bcdc
VD
111static int dsa_switch_mdb_add(struct dsa_switch *ds,
112 struct dsa_notifier_mdb_info *info)
113{
114 const struct switchdev_obj_port_mdb *mdb = info->mdb;
115 struct switchdev_trans *trans = info->trans;
a1a6b7ea
VD
116 DECLARE_BITMAP(group, ds->num_ports);
117 int port, err;
8ae5bcdc 118
a1a6b7ea
VD
119 /* Build a mask of Multicast group members */
120 bitmap_zero(group, ds->num_ports);
121 if (ds->index == info->sw_index)
122 set_bit(info->port, group);
123 for (port = 0; port < ds->num_ports; port++)
124 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
125 set_bit(port, group);
8ae5bcdc
VD
126
127 if (switchdev_trans_ph_prepare(trans)) {
128 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
129 return -EOPNOTSUPP;
130
a1a6b7ea
VD
131 for_each_set_bit(port, group, ds->num_ports) {
132 err = ds->ops->port_mdb_prepare(ds, port, mdb, trans);
133 if (err)
134 return err;
135 }
b0b38a1c
VD
136
137 return 0;
8ae5bcdc
VD
138 }
139
a1a6b7ea
VD
140 for_each_set_bit(port, group, ds->num_ports)
141 ds->ops->port_mdb_add(ds, port, mdb, trans);
8ae5bcdc
VD
142
143 return 0;
144}
145
146static int dsa_switch_mdb_del(struct dsa_switch *ds,
147 struct dsa_notifier_mdb_info *info)
148{
149 const struct switchdev_obj_port_mdb *mdb = info->mdb;
150
8ae5bcdc
VD
151 if (!ds->ops->port_mdb_del)
152 return -EOPNOTSUPP;
153
a1a6b7ea
VD
154 if (ds->index == info->sw_index)
155 return ds->ops->port_mdb_del(ds, info->port, mdb);
156
157 return 0;
8ae5bcdc
VD
158}
159
d0c627b8
VD
160static int dsa_switch_vlan_add(struct dsa_switch *ds,
161 struct dsa_notifier_vlan_info *info)
162{
163 const struct switchdev_obj_port_vlan *vlan = info->vlan;
164 struct switchdev_trans *trans = info->trans;
1ca4aa9c
VD
165 DECLARE_BITMAP(members, ds->num_ports);
166 int port, err;
d0c627b8 167
1ca4aa9c
VD
168 /* Build a mask of VLAN members */
169 bitmap_zero(members, ds->num_ports);
170 if (ds->index == info->sw_index)
171 set_bit(info->port, members);
b2f81d30
VD
172 for (port = 0; port < ds->num_ports; port++)
173 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
174 set_bit(port, members);
d0c627b8
VD
175
176 if (switchdev_trans_ph_prepare(trans)) {
177 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
178 return -EOPNOTSUPP;
179
1ca4aa9c
VD
180 for_each_set_bit(port, members, ds->num_ports) {
181 err = ds->ops->port_vlan_prepare(ds, port, vlan, trans);
182 if (err)
183 return err;
184 }
d0c627b8
VD
185 }
186
1ca4aa9c
VD
187 for_each_set_bit(port, members, ds->num_ports)
188 ds->ops->port_vlan_add(ds, port, vlan, trans);
d0c627b8
VD
189
190 return 0;
191}
192
193static int dsa_switch_vlan_del(struct dsa_switch *ds,
194 struct dsa_notifier_vlan_info *info)
195{
196 const struct switchdev_obj_port_vlan *vlan = info->vlan;
197
d0c627b8
VD
198 if (!ds->ops->port_vlan_del)
199 return -EOPNOTSUPP;
200
1ca4aa9c
VD
201 if (ds->index == info->sw_index)
202 return ds->ops->port_vlan_del(ds, info->port, vlan);
203
204 return 0;
d0c627b8
VD
205}
206
f515f192
VD
207static int dsa_switch_event(struct notifier_block *nb,
208 unsigned long event, void *info)
209{
210 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
211 int err;
212
213 switch (event) {
1faabf74
VD
214 case DSA_NOTIFIER_AGEING_TIME:
215 err = dsa_switch_ageing_time(ds, info);
216 break;
04d3a4c6
VD
217 case DSA_NOTIFIER_BRIDGE_JOIN:
218 err = dsa_switch_bridge_join(ds, info);
219 break;
220 case DSA_NOTIFIER_BRIDGE_LEAVE:
221 err = dsa_switch_bridge_leave(ds, info);
222 break;
685fb6a4
VD
223 case DSA_NOTIFIER_FDB_ADD:
224 err = dsa_switch_fdb_add(ds, info);
225 break;
226 case DSA_NOTIFIER_FDB_DEL:
227 err = dsa_switch_fdb_del(ds, info);
228 break;
8ae5bcdc
VD
229 case DSA_NOTIFIER_MDB_ADD:
230 err = dsa_switch_mdb_add(ds, info);
231 break;
232 case DSA_NOTIFIER_MDB_DEL:
233 err = dsa_switch_mdb_del(ds, info);
234 break;
d0c627b8
VD
235 case DSA_NOTIFIER_VLAN_ADD:
236 err = dsa_switch_vlan_add(ds, info);
237 break;
238 case DSA_NOTIFIER_VLAN_DEL:
239 err = dsa_switch_vlan_del(ds, info);
240 break;
f515f192
VD
241 default:
242 err = -EOPNOTSUPP;
243 break;
244 }
245
246 /* Non-switchdev operations cannot be rolled back. If a DSA driver
247 * returns an error during the chained call, switch chips may be in an
248 * inconsistent state.
249 */
250 if (err)
251 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
252 event, err);
253
254 return notifier_from_errno(err);
255}
256
257int dsa_switch_register_notifier(struct dsa_switch *ds)
258{
259 ds->nb.notifier_call = dsa_switch_event;
260
261 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
262}
263
264void dsa_switch_unregister_notifier(struct dsa_switch *ds)
265{
266 int err;
267
268 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
269 if (err)
270 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
271}