blob: f235ae1e9777681c30a43747e2d40ae435f25a76 [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{
86 const struct switchdev_obj_port_fdb *fdb = info->fdb;
87 struct switchdev_trans *trans = info->trans;
88
89 /* Do not care yet about other switch chips of the fabric */
90 if (ds->index != info->sw_index)
91 return 0;
92
93 if (switchdev_trans_ph_prepare(trans)) {
94 if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add)
95 return -EOPNOTSUPP;
96
97 return ds->ops->port_fdb_prepare(ds, info->port, fdb, trans);
98 }
99
100 ds->ops->port_fdb_add(ds, info->port, fdb, trans);
101
102 return 0;
103}
104
105static int dsa_switch_fdb_del(struct dsa_switch *ds,
106 struct dsa_notifier_fdb_info *info)
107{
108 const struct switchdev_obj_port_fdb *fdb = info->fdb;
109
110 /* Do not care yet about other switch chips of the fabric */
111 if (ds->index != info->sw_index)
112 return 0;
113
114 if (!ds->ops->port_fdb_del)
115 return -EOPNOTSUPP;
116
117 return ds->ops->port_fdb_del(ds, info->port, fdb);
118}
119
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400120static int dsa_switch_mdb_add(struct dsa_switch *ds,
121 struct dsa_notifier_mdb_info *info)
122{
123 const struct switchdev_obj_port_mdb *mdb = info->mdb;
124 struct switchdev_trans *trans = info->trans;
125
126 /* Do not care yet about other switch chips of the fabric */
127 if (ds->index != info->sw_index)
128 return 0;
129
130 if (switchdev_trans_ph_prepare(trans)) {
131 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
132 return -EOPNOTSUPP;
133
134 return ds->ops->port_mdb_prepare(ds, info->port, mdb, trans);
135 }
136
137 ds->ops->port_mdb_add(ds, info->port, mdb, trans);
138
139 return 0;
140}
141
142static int dsa_switch_mdb_del(struct dsa_switch *ds,
143 struct dsa_notifier_mdb_info *info)
144{
145 const struct switchdev_obj_port_mdb *mdb = info->mdb;
146
147 /* Do not care yet about other switch chips of the fabric */
148 if (ds->index != info->sw_index)
149 return 0;
150
151 if (!ds->ops->port_mdb_del)
152 return -EOPNOTSUPP;
153
154 return ds->ops->port_mdb_del(ds, info->port, mdb);
155}
156
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400157static int dsa_switch_vlan_add(struct dsa_switch *ds,
158 struct dsa_notifier_vlan_info *info)
159{
160 const struct switchdev_obj_port_vlan *vlan = info->vlan;
161 struct switchdev_trans *trans = info->trans;
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400162 DECLARE_BITMAP(members, ds->num_ports);
163 int port, err;
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400164
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400165 /* Build a mask of VLAN members */
166 bitmap_zero(members, ds->num_ports);
167 if (ds->index == info->sw_index)
168 set_bit(info->port, members);
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400169
170 if (switchdev_trans_ph_prepare(trans)) {
171 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
172 return -EOPNOTSUPP;
173
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400174 for_each_set_bit(port, members, ds->num_ports) {
175 err = ds->ops->port_vlan_prepare(ds, port, vlan, trans);
176 if (err)
177 return err;
178 }
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400179 }
180
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400181 for_each_set_bit(port, members, ds->num_ports)
182 ds->ops->port_vlan_add(ds, port, vlan, trans);
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400183
184 return 0;
185}
186
187static int dsa_switch_vlan_del(struct dsa_switch *ds,
188 struct dsa_notifier_vlan_info *info)
189{
190 const struct switchdev_obj_port_vlan *vlan = info->vlan;
191
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400192 if (!ds->ops->port_vlan_del)
193 return -EOPNOTSUPP;
194
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400195 if (ds->index == info->sw_index)
196 return ds->ops->port_vlan_del(ds, info->port, vlan);
197
198 return 0;
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400199}
200
Vivien Didelotf515f192017-02-03 13:20:20 -0500201static int dsa_switch_event(struct notifier_block *nb,
202 unsigned long event, void *info)
203{
204 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
205 int err;
206
207 switch (event) {
Vivien Didelot1faabf72017-05-19 17:00:52 -0400208 case DSA_NOTIFIER_AGEING_TIME:
209 err = dsa_switch_ageing_time(ds, info);
210 break;
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500211 case DSA_NOTIFIER_BRIDGE_JOIN:
212 err = dsa_switch_bridge_join(ds, info);
213 break;
214 case DSA_NOTIFIER_BRIDGE_LEAVE:
215 err = dsa_switch_bridge_leave(ds, info);
216 break;
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400217 case DSA_NOTIFIER_FDB_ADD:
218 err = dsa_switch_fdb_add(ds, info);
219 break;
220 case DSA_NOTIFIER_FDB_DEL:
221 err = dsa_switch_fdb_del(ds, info);
222 break;
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400223 case DSA_NOTIFIER_MDB_ADD:
224 err = dsa_switch_mdb_add(ds, info);
225 break;
226 case DSA_NOTIFIER_MDB_DEL:
227 err = dsa_switch_mdb_del(ds, info);
228 break;
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400229 case DSA_NOTIFIER_VLAN_ADD:
230 err = dsa_switch_vlan_add(ds, info);
231 break;
232 case DSA_NOTIFIER_VLAN_DEL:
233 err = dsa_switch_vlan_del(ds, info);
234 break;
Vivien Didelotf515f192017-02-03 13:20:20 -0500235 default:
236 err = -EOPNOTSUPP;
237 break;
238 }
239
240 /* Non-switchdev operations cannot be rolled back. If a DSA driver
241 * returns an error during the chained call, switch chips may be in an
242 * inconsistent state.
243 */
244 if (err)
245 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
246 event, err);
247
248 return notifier_from_errno(err);
249}
250
251int dsa_switch_register_notifier(struct dsa_switch *ds)
252{
253 ds->nb.notifier_call = dsa_switch_event;
254
255 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
256}
257
258void dsa_switch_unregister_notifier(struct dsa_switch *ds)
259{
260 int err;
261
262 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
263 if (err)
264 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
265}