]> git.ipfire.org Git - people/arne_f/kernel.git/blame - net/dsa/switch.c
net: dsa: add FDB notifier
[people/arne_f/kernel.git] / net / dsa / switch.c
CommitLineData
f515f192
VD
1/*
2 * Handling of a single switch chip, part of a switch fabric
3 *
4333d619
VD
4 * Copyright (c) 2017 Savoir-faire Linux Inc.
5 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
f515f192
VD
6 *
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>
1faabf74 15#include <net/switchdev.h>
ea5dd34b
VD
16
17#include "dsa_priv.h"
f515f192 18
1faabf74
VD
19static 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
04d3a4c6
VD
61static 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
40ef2c93
VD
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);
04d3a4c6
VD
70
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
40ef2c93
VD
80 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);
04d3a4c6
VD
83
84 return 0;
85}
86
685fb6a4
VD
87static 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
f515f192
VD
124static int dsa_switch_event(struct notifier_block *nb,
125 unsigned long event, void *info)
126{
127 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
128 int err;
129
130 switch (event) {
1faabf74
VD
131 case DSA_NOTIFIER_AGEING_TIME:
132 err = dsa_switch_ageing_time(ds, info);
133 break;
04d3a4c6
VD
134 case DSA_NOTIFIER_BRIDGE_JOIN:
135 err = dsa_switch_bridge_join(ds, info);
136 break;
137 case DSA_NOTIFIER_BRIDGE_LEAVE:
138 err = dsa_switch_bridge_leave(ds, info);
139 break;
685fb6a4
VD
140 case DSA_NOTIFIER_FDB_ADD:
141 err = dsa_switch_fdb_add(ds, info);
142 break;
143 case DSA_NOTIFIER_FDB_DEL:
144 err = dsa_switch_fdb_del(ds, info);
145 break;
f515f192
VD
146 default:
147 err = -EOPNOTSUPP;
148 break;
149 }
150
151 /* Non-switchdev operations cannot be rolled back. If a DSA driver
152 * returns an error during the chained call, switch chips may be in an
153 * inconsistent state.
154 */
155 if (err)
156 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
157 event, err);
158
159 return notifier_from_errno(err);
160}
161
162int dsa_switch_register_notifier(struct dsa_switch *ds)
163{
164 ds->nb.notifier_call = dsa_switch_event;
165
166 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
167}
168
169void dsa_switch_unregister_notifier(struct dsa_switch *ds)
170{
171 int err;
172
173 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
174 if (err)
175 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
176}