2 * Handling of a single switch chip, part of a switch fabric
4 * Copyright (c) 2017 Savoir-faire Linux Inc.
5 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
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.
13 #include <linux/netdevice.h>
14 #include <linux/notifier.h>
15 #include <net/switchdev.h>
19 static unsigned int dsa_switch_fastest_ageing_time(struct dsa_switch *ds,
20 unsigned int ageing_time)
24 for (i = 0; i < ds->num_ports; ++i) {
25 struct dsa_port *dp = &ds->ports[i];
27 if (dp->ageing_time && dp->ageing_time < ageing_time)
28 ageing_time = dp->ageing_time;
34 static int dsa_switch_ageing_time(struct dsa_switch *ds,
35 struct dsa_notifier_ageing_time_info *info)
37 unsigned int ageing_time = info->ageing_time;
38 struct switchdev_trans *trans = info->trans;
40 if (switchdev_trans_ph_prepare(trans)) {
41 if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
43 if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
48 /* Program the fastest ageing time in case of multiple bridges */
49 ageing_time = dsa_switch_fastest_ageing_time(ds, ageing_time);
51 if (ds->ops->set_ageing_time)
52 return ds->ops->set_ageing_time(ds, ageing_time);
57 static int dsa_switch_bridge_join(struct dsa_switch *ds,
58 struct dsa_notifier_bridge_info *info)
60 if (ds->index == info->sw_index && ds->ops->port_bridge_join)
61 return ds->ops->port_bridge_join(ds, info->port, info->br);
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);
70 static int dsa_switch_bridge_leave(struct dsa_switch *ds,
71 struct dsa_notifier_bridge_info *info)
73 if (ds->index == info->sw_index && ds->ops->port_bridge_leave)
74 ds->ops->port_bridge_leave(ds, info->port, info->br);
76 if (ds->index != info->sw_index && ds->ops->crosschip_bridge_leave)
77 ds->ops->crosschip_bridge_leave(ds, info->sw_index, info->port,
83 static int dsa_switch_fdb_add(struct dsa_switch *ds,
84 struct dsa_notifier_fdb_info *info)
86 const struct switchdev_obj_port_fdb *fdb = info->fdb;
87 struct switchdev_trans *trans = info->trans;
89 /* Do not care yet about other switch chips of the fabric */
90 if (ds->index != info->sw_index)
93 if (switchdev_trans_ph_prepare(trans)) {
94 if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add)
97 return ds->ops->port_fdb_prepare(ds, info->port, fdb->addr,
101 ds->ops->port_fdb_add(ds, info->port, fdb->addr, fdb->vid);
106 static int dsa_switch_fdb_del(struct dsa_switch *ds,
107 struct dsa_notifier_fdb_info *info)
109 const struct switchdev_obj_port_fdb *fdb = info->fdb;
111 /* Do not care yet about other switch chips of the fabric */
112 if (ds->index != info->sw_index)
115 if (!ds->ops->port_fdb_del)
118 return ds->ops->port_fdb_del(ds, info->port, fdb->addr,
122 static int dsa_switch_mdb_add(struct dsa_switch *ds,
123 struct dsa_notifier_mdb_info *info)
125 const struct switchdev_obj_port_mdb *mdb = info->mdb;
126 struct switchdev_trans *trans = info->trans;
127 DECLARE_BITMAP(group, ds->num_ports);
130 /* Build a mask of Multicast group members */
131 bitmap_zero(group, ds->num_ports);
132 if (ds->index == info->sw_index)
133 set_bit(info->port, group);
134 for (port = 0; port < ds->num_ports; port++)
135 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
136 set_bit(port, group);
138 if (switchdev_trans_ph_prepare(trans)) {
139 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
142 for_each_set_bit(port, group, ds->num_ports) {
143 err = ds->ops->port_mdb_prepare(ds, port, mdb, trans);
149 for_each_set_bit(port, group, ds->num_ports)
150 ds->ops->port_mdb_add(ds, port, mdb, trans);
155 static int dsa_switch_mdb_del(struct dsa_switch *ds,
156 struct dsa_notifier_mdb_info *info)
158 const struct switchdev_obj_port_mdb *mdb = info->mdb;
160 if (!ds->ops->port_mdb_del)
163 if (ds->index == info->sw_index)
164 return ds->ops->port_mdb_del(ds, info->port, mdb);
169 static int dsa_switch_vlan_add(struct dsa_switch *ds,
170 struct dsa_notifier_vlan_info *info)
172 const struct switchdev_obj_port_vlan *vlan = info->vlan;
173 struct switchdev_trans *trans = info->trans;
174 DECLARE_BITMAP(members, ds->num_ports);
177 /* Build a mask of VLAN members */
178 bitmap_zero(members, ds->num_ports);
179 if (ds->index == info->sw_index)
180 set_bit(info->port, members);
181 for (port = 0; port < ds->num_ports; port++)
182 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
183 set_bit(port, members);
185 if (switchdev_trans_ph_prepare(trans)) {
186 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
189 for_each_set_bit(port, members, ds->num_ports) {
190 err = ds->ops->port_vlan_prepare(ds, port, vlan, trans);
196 for_each_set_bit(port, members, ds->num_ports)
197 ds->ops->port_vlan_add(ds, port, vlan, trans);
202 static int dsa_switch_vlan_del(struct dsa_switch *ds,
203 struct dsa_notifier_vlan_info *info)
205 const struct switchdev_obj_port_vlan *vlan = info->vlan;
207 if (!ds->ops->port_vlan_del)
210 if (ds->index == info->sw_index)
211 return ds->ops->port_vlan_del(ds, info->port, vlan);
216 static int dsa_switch_event(struct notifier_block *nb,
217 unsigned long event, void *info)
219 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
223 case DSA_NOTIFIER_AGEING_TIME:
224 err = dsa_switch_ageing_time(ds, info);
226 case DSA_NOTIFIER_BRIDGE_JOIN:
227 err = dsa_switch_bridge_join(ds, info);
229 case DSA_NOTIFIER_BRIDGE_LEAVE:
230 err = dsa_switch_bridge_leave(ds, info);
232 case DSA_NOTIFIER_FDB_ADD:
233 err = dsa_switch_fdb_add(ds, info);
235 case DSA_NOTIFIER_FDB_DEL:
236 err = dsa_switch_fdb_del(ds, info);
238 case DSA_NOTIFIER_MDB_ADD:
239 err = dsa_switch_mdb_add(ds, info);
241 case DSA_NOTIFIER_MDB_DEL:
242 err = dsa_switch_mdb_del(ds, info);
244 case DSA_NOTIFIER_VLAN_ADD:
245 err = dsa_switch_vlan_add(ds, info);
247 case DSA_NOTIFIER_VLAN_DEL:
248 err = dsa_switch_vlan_del(ds, info);
255 /* Non-switchdev operations cannot be rolled back. If a DSA driver
256 * returns an error during the chained call, switch chips may be in an
257 * inconsistent state.
260 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
263 return notifier_from_errno(err);
266 int dsa_switch_register_notifier(struct dsa_switch *ds)
268 ds->nb.notifier_call = dsa_switch_event;
270 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
273 void dsa_switch_unregister_notifier(struct dsa_switch *ds)
277 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
279 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);