blob: b7e8e45869fc503df00a411fcdea0c797076dc92 [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
40 /* Do not care yet about other switch chips of the fabric */
41 if (ds->index != info->sw_index)
42 return 0;
43
44 if (switchdev_trans_ph_prepare(trans)) {
45 if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
46 return -ERANGE;
47 if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
48 return -ERANGE;
49 return 0;
50 }
51
52 /* Program the fastest ageing time in case of multiple bridges */
53 ageing_time = dsa_switch_fastest_ageing_time(ds, ageing_time);
54
55 if (ds->ops->set_ageing_time)
56 return ds->ops->set_ageing_time(ds, ageing_time);
57
58 return 0;
59}
60
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050061static int dsa_switch_bridge_join(struct dsa_switch *ds,
62 struct dsa_notifier_bridge_info *info)
63{
64 if (ds->index == info->sw_index && ds->ops->port_bridge_join)
65 return ds->ops->port_bridge_join(ds, info->port, info->br);
66
Vivien Didelot40ef2c92017-03-30 17:37:14 -040067 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);
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050070
71 return 0;
72}
73
74static int dsa_switch_bridge_leave(struct dsa_switch *ds,
75 struct dsa_notifier_bridge_info *info)
76{
77 if (ds->index == info->sw_index && ds->ops->port_bridge_leave)
78 ds->ops->port_bridge_leave(ds, info->port, info->br);
79
Vivien Didelot40ef2c92017-03-30 17:37:14 -040080 if (ds->index != info->sw_index && ds->ops->crosschip_bridge_leave)
81 ds->ops->crosschip_bridge_leave(ds, info->sw_index, info->port,
82 info->br);
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050083
84 return 0;
85}
86
Vivien Didelot685fb6a2017-05-19 17:00:53 -040087static int dsa_switch_fdb_add(struct dsa_switch *ds,
88 struct dsa_notifier_fdb_info *info)
89{
90 const struct switchdev_obj_port_fdb *fdb = info->fdb;
91 struct switchdev_trans *trans = info->trans;
92
93 /* Do not care yet about other switch chips of the fabric */
94 if (ds->index != info->sw_index)
95 return 0;
96
97 if (switchdev_trans_ph_prepare(trans)) {
98 if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add)
99 return -EOPNOTSUPP;
100
101 return ds->ops->port_fdb_prepare(ds, info->port, fdb, trans);
102 }
103
104 ds->ops->port_fdb_add(ds, info->port, fdb, trans);
105
106 return 0;
107}
108
109static int dsa_switch_fdb_del(struct dsa_switch *ds,
110 struct dsa_notifier_fdb_info *info)
111{
112 const struct switchdev_obj_port_fdb *fdb = info->fdb;
113
114 /* Do not care yet about other switch chips of the fabric */
115 if (ds->index != info->sw_index)
116 return 0;
117
118 if (!ds->ops->port_fdb_del)
119 return -EOPNOTSUPP;
120
121 return ds->ops->port_fdb_del(ds, info->port, fdb);
122}
123
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400124static int dsa_switch_mdb_add(struct dsa_switch *ds,
125 struct dsa_notifier_mdb_info *info)
126{
127 const struct switchdev_obj_port_mdb *mdb = info->mdb;
128 struct switchdev_trans *trans = info->trans;
129
130 /* Do not care yet about other switch chips of the fabric */
131 if (ds->index != info->sw_index)
132 return 0;
133
134 if (switchdev_trans_ph_prepare(trans)) {
135 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
136 return -EOPNOTSUPP;
137
138 return ds->ops->port_mdb_prepare(ds, info->port, mdb, trans);
139 }
140
141 ds->ops->port_mdb_add(ds, info->port, mdb, trans);
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
151 /* Do not care yet about other switch chips of the fabric */
152 if (ds->index != info->sw_index)
153 return 0;
154
155 if (!ds->ops->port_mdb_del)
156 return -EOPNOTSUPP;
157
158 return ds->ops->port_mdb_del(ds, info->port, mdb);
159}
160
Vivien Didelotf515f192017-02-03 13:20:20 -0500161static int dsa_switch_event(struct notifier_block *nb,
162 unsigned long event, void *info)
163{
164 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
165 int err;
166
167 switch (event) {
Vivien Didelot1faabf72017-05-19 17:00:52 -0400168 case DSA_NOTIFIER_AGEING_TIME:
169 err = dsa_switch_ageing_time(ds, info);
170 break;
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500171 case DSA_NOTIFIER_BRIDGE_JOIN:
172 err = dsa_switch_bridge_join(ds, info);
173 break;
174 case DSA_NOTIFIER_BRIDGE_LEAVE:
175 err = dsa_switch_bridge_leave(ds, info);
176 break;
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400177 case DSA_NOTIFIER_FDB_ADD:
178 err = dsa_switch_fdb_add(ds, info);
179 break;
180 case DSA_NOTIFIER_FDB_DEL:
181 err = dsa_switch_fdb_del(ds, info);
182 break;
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400183 case DSA_NOTIFIER_MDB_ADD:
184 err = dsa_switch_mdb_add(ds, info);
185 break;
186 case DSA_NOTIFIER_MDB_DEL:
187 err = dsa_switch_mdb_del(ds, info);
188 break;
Vivien Didelotf515f192017-02-03 13:20:20 -0500189 default:
190 err = -EOPNOTSUPP;
191 break;
192 }
193
194 /* Non-switchdev operations cannot be rolled back. If a DSA driver
195 * returns an error during the chained call, switch chips may be in an
196 * inconsistent state.
197 */
198 if (err)
199 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
200 event, err);
201
202 return notifier_from_errno(err);
203}
204
205int dsa_switch_register_notifier(struct dsa_switch *ds)
206{
207 ds->nb.notifier_call = dsa_switch_event;
208
209 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
210}
211
212void dsa_switch_unregister_notifier(struct dsa_switch *ds)
213{
214 int err;
215
216 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
217 if (err)
218 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
219}