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 /* Do not care yet about other switch chips of the fabric */
41 if (ds->index != info->sw_index)
44 if (switchdev_trans_ph_prepare(trans)) {
45 if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
47 if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
52 /* Program the fastest ageing time in case of multiple bridges */
53 ageing_time = dsa_switch_fastest_ageing_time(ds, ageing_time);
55 if (ds->ops->set_ageing_time)
56 return ds->ops->set_ageing_time(ds, ageing_time);
61 static int dsa_switch_bridge_join(struct dsa_switch *ds,
62 struct dsa_notifier_bridge_info *info)
64 if (ds->index == info->sw_index && ds->ops->port_bridge_join)
65 return ds->ops->port_bridge_join(ds, info->port, info->br);
67 if (ds->index != info->sw_index && ds->ops->crosschip_bridge_join)
68 return ds->ops->crosschip_bridge_join(ds, info->sw_index,
69 info->port, info->br);
74 static int dsa_switch_bridge_leave(struct dsa_switch *ds,
75 struct dsa_notifier_bridge_info *info)
77 if (ds->index == info->sw_index && ds->ops->port_bridge_leave)
78 ds->ops->port_bridge_leave(ds, info->port, info->br);
80 if (ds->index != info->sw_index && ds->ops->crosschip_bridge_leave)
81 ds->ops->crosschip_bridge_leave(ds, info->sw_index, info->port,
87 static int dsa_switch_fdb_add(struct dsa_switch *ds,
88 struct dsa_notifier_fdb_info *info)
90 const struct switchdev_obj_port_fdb *fdb = info->fdb;
91 struct switchdev_trans *trans = info->trans;
93 /* Do not care yet about other switch chips of the fabric */
94 if (ds->index != info->sw_index)
97 if (switchdev_trans_ph_prepare(trans)) {
98 if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add)
101 return ds->ops->port_fdb_prepare(ds, info->port, fdb, trans);
104 ds->ops->port_fdb_add(ds, info->port, fdb, trans);
109 static int dsa_switch_fdb_del(struct dsa_switch *ds,
110 struct dsa_notifier_fdb_info *info)
112 const struct switchdev_obj_port_fdb *fdb = info->fdb;
114 /* Do not care yet about other switch chips of the fabric */
115 if (ds->index != info->sw_index)
118 if (!ds->ops->port_fdb_del)
121 return ds->ops->port_fdb_del(ds, info->port, fdb);
124 static int dsa_switch_mdb_add(struct dsa_switch *ds,
125 struct dsa_notifier_mdb_info *info)
127 const struct switchdev_obj_port_mdb *mdb = info->mdb;
128 struct switchdev_trans *trans = info->trans;
130 /* Do not care yet about other switch chips of the fabric */
131 if (ds->index != info->sw_index)
134 if (switchdev_trans_ph_prepare(trans)) {
135 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
138 return ds->ops->port_mdb_prepare(ds, info->port, mdb, trans);
141 ds->ops->port_mdb_add(ds, info->port, mdb, trans);
146 static int dsa_switch_mdb_del(struct dsa_switch *ds,
147 struct dsa_notifier_mdb_info *info)
149 const struct switchdev_obj_port_mdb *mdb = info->mdb;
151 /* Do not care yet about other switch chips of the fabric */
152 if (ds->index != info->sw_index)
155 if (!ds->ops->port_mdb_del)
158 return ds->ops->port_mdb_del(ds, info->port, mdb);
161 static int dsa_switch_vlan_add(struct dsa_switch *ds,
162 struct dsa_notifier_vlan_info *info)
164 const struct switchdev_obj_port_vlan *vlan = info->vlan;
165 struct switchdev_trans *trans = info->trans;
167 /* Do not care yet about other switch chips of the fabric */
168 if (ds->index != info->sw_index)
171 if (switchdev_trans_ph_prepare(trans)) {
172 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
175 return ds->ops->port_vlan_prepare(ds, info->port, vlan, trans);
178 ds->ops->port_vlan_add(ds, info->port, vlan, trans);
183 static int dsa_switch_vlan_del(struct dsa_switch *ds,
184 struct dsa_notifier_vlan_info *info)
186 const struct switchdev_obj_port_vlan *vlan = info->vlan;
188 /* Do not care yet about other switch chips of the fabric */
189 if (ds->index != info->sw_index)
192 if (!ds->ops->port_vlan_del)
195 return ds->ops->port_vlan_del(ds, info->port, vlan);
198 static int dsa_switch_event(struct notifier_block *nb,
199 unsigned long event, void *info)
201 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
205 case DSA_NOTIFIER_AGEING_TIME:
206 err = dsa_switch_ageing_time(ds, info);
208 case DSA_NOTIFIER_BRIDGE_JOIN:
209 err = dsa_switch_bridge_join(ds, info);
211 case DSA_NOTIFIER_BRIDGE_LEAVE:
212 err = dsa_switch_bridge_leave(ds, info);
214 case DSA_NOTIFIER_FDB_ADD:
215 err = dsa_switch_fdb_add(ds, info);
217 case DSA_NOTIFIER_FDB_DEL:
218 err = dsa_switch_fdb_del(ds, info);
220 case DSA_NOTIFIER_MDB_ADD:
221 err = dsa_switch_mdb_add(ds, info);
223 case DSA_NOTIFIER_MDB_DEL:
224 err = dsa_switch_mdb_del(ds, info);
226 case DSA_NOTIFIER_VLAN_ADD:
227 err = dsa_switch_vlan_add(ds, info);
229 case DSA_NOTIFIER_VLAN_DEL:
230 err = dsa_switch_vlan_del(ds, info);
237 /* Non-switchdev operations cannot be rolled back. If a DSA driver
238 * returns an error during the chained call, switch chips may be in an
239 * inconsistent state.
242 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
245 return notifier_from_errno(err);
248 int dsa_switch_register_notifier(struct dsa_switch *ds)
250 ds->nb.notifier_call = dsa_switch_event;
252 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
255 void dsa_switch_unregister_notifier(struct dsa_switch *ds)
259 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
261 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);