GNU Linux-libre 4.19.286-gnu1
[releases.git] / net / dsa / switch.c
1 /*
2  * Handling of a single switch chip, part of a switch fabric
3  *
4  * Copyright (c) 2017 Savoir-faire Linux Inc.
5  *      Vivien Didelot <vivien.didelot@savoirfairelinux.com>
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>
15 #include <net/switchdev.h>
16
17 #include "dsa_priv.h"
18
19 static 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
34 static 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
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
57 static 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
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);
66
67         return 0;
68 }
69
70 static 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
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);
79
80         return 0;
81 }
82
83 static int dsa_switch_fdb_add(struct dsa_switch *ds,
84                               struct dsa_notifier_fdb_info *info)
85 {
86         int port = dsa_towards_port(ds, info->sw_index, info->port);
87
88         if (!ds->ops->port_fdb_add)
89                 return -EOPNOTSUPP;
90
91         return ds->ops->port_fdb_add(ds, port, info->addr, info->vid);
92 }
93
94 static int dsa_switch_fdb_del(struct dsa_switch *ds,
95                               struct dsa_notifier_fdb_info *info)
96 {
97         int port = dsa_towards_port(ds, info->sw_index, info->port);
98
99         if (!ds->ops->port_fdb_del)
100                 return -EOPNOTSUPP;
101
102         return ds->ops->port_fdb_del(ds, port, info->addr, info->vid);
103 }
104
105 static int
106 dsa_switch_mdb_prepare_bitmap(struct dsa_switch *ds,
107                               const struct switchdev_obj_port_mdb *mdb,
108                               const unsigned long *bitmap)
109 {
110         int port, err;
111
112         if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
113                 return -EOPNOTSUPP;
114
115         for_each_set_bit(port, bitmap, ds->num_ports) {
116                 err = ds->ops->port_mdb_prepare(ds, port, mdb);
117                 if (err)
118                         return err;
119         }
120
121         return 0;
122 }
123
124 static void dsa_switch_mdb_add_bitmap(struct dsa_switch *ds,
125                                       const struct switchdev_obj_port_mdb *mdb,
126                                       const unsigned long *bitmap)
127 {
128         int port;
129
130         if (!ds->ops->port_mdb_add)
131                 return;
132
133         for_each_set_bit(port, bitmap, ds->num_ports)
134                 ds->ops->port_mdb_add(ds, port, mdb);
135 }
136
137 static int dsa_switch_mdb_add(struct dsa_switch *ds,
138                               struct dsa_notifier_mdb_info *info)
139 {
140         const struct switchdev_obj_port_mdb *mdb = info->mdb;
141         struct switchdev_trans *trans = info->trans;
142         int port;
143
144         /* Build a mask of Multicast group members */
145         bitmap_zero(ds->bitmap, ds->num_ports);
146         if (ds->index == info->sw_index)
147                 set_bit(info->port, ds->bitmap);
148         for (port = 0; port < ds->num_ports; port++)
149                 if (dsa_is_dsa_port(ds, port))
150                         set_bit(port, ds->bitmap);
151
152         if (switchdev_trans_ph_prepare(trans))
153                 return dsa_switch_mdb_prepare_bitmap(ds, mdb, ds->bitmap);
154
155         dsa_switch_mdb_add_bitmap(ds, mdb, ds->bitmap);
156
157         return 0;
158 }
159
160 static int dsa_switch_mdb_del(struct dsa_switch *ds,
161                               struct dsa_notifier_mdb_info *info)
162 {
163         const struct switchdev_obj_port_mdb *mdb = info->mdb;
164
165         if (!ds->ops->port_mdb_del)
166                 return -EOPNOTSUPP;
167
168         if (ds->index == info->sw_index)
169                 return ds->ops->port_mdb_del(ds, info->port, mdb);
170
171         return 0;
172 }
173
174 static int
175 dsa_switch_vlan_prepare_bitmap(struct dsa_switch *ds,
176                                const struct switchdev_obj_port_vlan *vlan,
177                                const unsigned long *bitmap)
178 {
179         int port, err;
180
181         if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
182                 return -EOPNOTSUPP;
183
184         for_each_set_bit(port, bitmap, ds->num_ports) {
185                 err = ds->ops->port_vlan_prepare(ds, port, vlan);
186                 if (err)
187                         return err;
188         }
189
190         return 0;
191 }
192
193 static void
194 dsa_switch_vlan_add_bitmap(struct dsa_switch *ds,
195                            const struct switchdev_obj_port_vlan *vlan,
196                            const unsigned long *bitmap)
197 {
198         int port;
199
200         for_each_set_bit(port, bitmap, ds->num_ports)
201                 ds->ops->port_vlan_add(ds, port, vlan);
202 }
203
204 static int dsa_switch_vlan_add(struct dsa_switch *ds,
205                                struct dsa_notifier_vlan_info *info)
206 {
207         const struct switchdev_obj_port_vlan *vlan = info->vlan;
208         struct switchdev_trans *trans = info->trans;
209         int port;
210
211         /* Build a mask of VLAN members */
212         bitmap_zero(ds->bitmap, ds->num_ports);
213         if (ds->index == info->sw_index)
214                 set_bit(info->port, ds->bitmap);
215         for (port = 0; port < ds->num_ports; port++)
216                 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
217                         set_bit(port, ds->bitmap);
218
219         if (switchdev_trans_ph_prepare(trans))
220                 return dsa_switch_vlan_prepare_bitmap(ds, vlan, ds->bitmap);
221
222         dsa_switch_vlan_add_bitmap(ds, vlan, ds->bitmap);
223
224         return 0;
225 }
226
227 static int dsa_switch_vlan_del(struct dsa_switch *ds,
228                                struct dsa_notifier_vlan_info *info)
229 {
230         const struct switchdev_obj_port_vlan *vlan = info->vlan;
231
232         if (!ds->ops->port_vlan_del)
233                 return -EOPNOTSUPP;
234
235         if (ds->index == info->sw_index)
236                 return ds->ops->port_vlan_del(ds, info->port, vlan);
237
238         return 0;
239 }
240
241 static int dsa_switch_event(struct notifier_block *nb,
242                             unsigned long event, void *info)
243 {
244         struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
245         int err;
246
247         switch (event) {
248         case DSA_NOTIFIER_AGEING_TIME:
249                 err = dsa_switch_ageing_time(ds, info);
250                 break;
251         case DSA_NOTIFIER_BRIDGE_JOIN:
252                 err = dsa_switch_bridge_join(ds, info);
253                 break;
254         case DSA_NOTIFIER_BRIDGE_LEAVE:
255                 err = dsa_switch_bridge_leave(ds, info);
256                 break;
257         case DSA_NOTIFIER_FDB_ADD:
258                 err = dsa_switch_fdb_add(ds, info);
259                 break;
260         case DSA_NOTIFIER_FDB_DEL:
261                 err = dsa_switch_fdb_del(ds, info);
262                 break;
263         case DSA_NOTIFIER_MDB_ADD:
264                 err = dsa_switch_mdb_add(ds, info);
265                 break;
266         case DSA_NOTIFIER_MDB_DEL:
267                 err = dsa_switch_mdb_del(ds, info);
268                 break;
269         case DSA_NOTIFIER_VLAN_ADD:
270                 err = dsa_switch_vlan_add(ds, info);
271                 break;
272         case DSA_NOTIFIER_VLAN_DEL:
273                 err = dsa_switch_vlan_del(ds, info);
274                 break;
275         default:
276                 err = -EOPNOTSUPP;
277                 break;
278         }
279
280         /* Non-switchdev operations cannot be rolled back. If a DSA driver
281          * returns an error during the chained call, switch chips may be in an
282          * inconsistent state.
283          */
284         if (err)
285                 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
286                         event, err);
287
288         return notifier_from_errno(err);
289 }
290
291 int dsa_switch_register_notifier(struct dsa_switch *ds)
292 {
293         ds->nb.notifier_call = dsa_switch_event;
294
295         return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
296 }
297
298 void dsa_switch_unregister_notifier(struct dsa_switch *ds)
299 {
300         int err;
301
302         err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
303         if (err)
304                 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
305 }