blob: 29608d087a7c56a9be475fca32a69f52de985f00 [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>
Vivien Didelot1faabf72017-05-19 17:00:52 -040015#include <net/switchdev.h>
Vivien Didelotea5dd342017-05-17 15:46:03 -040016
17#include "dsa_priv.h"
Vivien Didelotf515f192017-02-03 13:20:20 -050018
Vivien Didelot1faabf72017-05-19 17:00:52 -040019static 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
Vivien Didelot1faabf72017-05-19 17:00:52 -040040 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
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050057static 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
Vivien Didelot40ef2c92017-03-30 17:37:14 -040063 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);
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050066
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
Vivien Didelot40ef2c92017-03-30 17:37:14 -040076 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);
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050079
80 return 0;
81}
82
Vivien Didelot685fb6a2017-05-19 17:00:53 -040083static int dsa_switch_fdb_add(struct dsa_switch *ds,
84 struct dsa_notifier_fdb_info *info)
85{
Vivien Didelot685fb6a2017-05-19 17:00:53 -040086 /* Do not care yet about other switch chips of the fabric */
87 if (ds->index != info->sw_index)
88 return 0;
89
Arkadi Sharshevsky1b6dd552017-08-06 16:15:40 +030090 if (!ds->ops->port_fdb_add)
91 return -EOPNOTSUPP;
Vivien Didelot685fb6a2017-05-19 17:00:53 -040092
Arkadi Sharshevsky2acf4e62017-08-06 16:15:41 +030093 return ds->ops->port_fdb_add(ds, info->port, info->addr,
94 info->vid);
Vivien Didelot685fb6a2017-05-19 17:00:53 -040095}
96
97static int dsa_switch_fdb_del(struct dsa_switch *ds,
98 struct dsa_notifier_fdb_info *info)
99{
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400100 /* 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
Arkadi Sharshevsky2acf4e62017-08-06 16:15:41 +0300107 return ds->ops->port_fdb_del(ds, info->port, info->addr,
108 info->vid);
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400109}
110
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400111static 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;
Vivien Didelota1a6b7e2017-06-15 16:14:48 -0400116 DECLARE_BITMAP(group, ds->num_ports);
117 int port, err;
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400118
Vivien Didelota1a6b7e2017-06-15 16:14:48 -0400119 /* 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++)
Andrew Lunnae451022017-11-09 23:11:02 +0100124 if (dsa_is_dsa_port(ds, port))
Vivien Didelota1a6b7e2017-06-15 16:14:48 -0400125 set_bit(port, group);
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400126
127 if (switchdev_trans_ph_prepare(trans)) {
128 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
129 return -EOPNOTSUPP;
130
Vivien Didelota1a6b7e2017-06-15 16:14:48 -0400131 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 }
Vivien Didelotb0b38a12017-11-08 10:49:56 -0500136
137 return 0;
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400138 }
139
Vivien Didelota1a6b7e2017-06-15 16:14:48 -0400140 for_each_set_bit(port, group, ds->num_ports)
141 ds->ops->port_mdb_add(ds, port, mdb, trans);
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400142
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
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400151 if (!ds->ops->port_mdb_del)
152 return -EOPNOTSUPP;
153
Vivien Didelota1a6b7e2017-06-15 16:14:48 -0400154 if (ds->index == info->sw_index)
155 return ds->ops->port_mdb_del(ds, info->port, mdb);
156
157 return 0;
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400158}
159
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400160static 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;
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400165 DECLARE_BITMAP(members, ds->num_ports);
166 int port, err;
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400167
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400168 /* 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);
Vivien Didelotb2f81d32017-06-07 18:12:15 -0400172 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);
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400175
176 if (switchdev_trans_ph_prepare(trans)) {
177 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
178 return -EOPNOTSUPP;
179
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400180 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 }
Vivien Didelot2118df92017-11-08 10:50:10 -0500185
186 return 0;
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400187 }
188
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400189 for_each_set_bit(port, members, ds->num_ports)
190 ds->ops->port_vlan_add(ds, port, vlan, trans);
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400191
192 return 0;
193}
194
195static int dsa_switch_vlan_del(struct dsa_switch *ds,
196 struct dsa_notifier_vlan_info *info)
197{
198 const struct switchdev_obj_port_vlan *vlan = info->vlan;
199
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400200 if (!ds->ops->port_vlan_del)
201 return -EOPNOTSUPP;
202
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400203 if (ds->index == info->sw_index)
204 return ds->ops->port_vlan_del(ds, info->port, vlan);
205
206 return 0;
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400207}
208
Vivien Didelotf515f192017-02-03 13:20:20 -0500209static int dsa_switch_event(struct notifier_block *nb,
210 unsigned long event, void *info)
211{
212 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
213 int err;
214
215 switch (event) {
Vivien Didelot1faabf72017-05-19 17:00:52 -0400216 case DSA_NOTIFIER_AGEING_TIME:
217 err = dsa_switch_ageing_time(ds, info);
218 break;
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500219 case DSA_NOTIFIER_BRIDGE_JOIN:
220 err = dsa_switch_bridge_join(ds, info);
221 break;
222 case DSA_NOTIFIER_BRIDGE_LEAVE:
223 err = dsa_switch_bridge_leave(ds, info);
224 break;
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400225 case DSA_NOTIFIER_FDB_ADD:
226 err = dsa_switch_fdb_add(ds, info);
227 break;
228 case DSA_NOTIFIER_FDB_DEL:
229 err = dsa_switch_fdb_del(ds, info);
230 break;
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400231 case DSA_NOTIFIER_MDB_ADD:
232 err = dsa_switch_mdb_add(ds, info);
233 break;
234 case DSA_NOTIFIER_MDB_DEL:
235 err = dsa_switch_mdb_del(ds, info);
236 break;
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400237 case DSA_NOTIFIER_VLAN_ADD:
238 err = dsa_switch_vlan_add(ds, info);
239 break;
240 case DSA_NOTIFIER_VLAN_DEL:
241 err = dsa_switch_vlan_del(ds, info);
242 break;
Vivien Didelotf515f192017-02-03 13:20:20 -0500243 default:
244 err = -EOPNOTSUPP;
245 break;
246 }
247
248 /* Non-switchdev operations cannot be rolled back. If a DSA driver
249 * returns an error during the chained call, switch chips may be in an
250 * inconsistent state.
251 */
252 if (err)
253 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
254 event, err);
255
256 return notifier_from_errno(err);
257}
258
259int dsa_switch_register_notifier(struct dsa_switch *ds)
260{
261 ds->nb.notifier_call = dsa_switch_event;
262
263 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
264}
265
266void dsa_switch_unregister_notifier(struct dsa_switch *ds)
267{
268 int err;
269
270 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
271 if (err)
272 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
273}