2 * Handling of a single switch port
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/if_bridge.h>
14 #include <linux/notifier.h>
18 static int dsa_port_notify(struct dsa_port *dp, unsigned long e, void *v)
20 struct raw_notifier_head *nh = &dp->ds->dst->nh;
23 err = raw_notifier_call_chain(nh, e, v);
25 return notifier_to_errno(err);
28 int dsa_port_set_state(struct dsa_port *dp, u8 state,
29 struct switchdev_trans *trans)
31 struct dsa_switch *ds = dp->ds;
34 if (switchdev_trans_ph_prepare(trans))
35 return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP;
37 if (ds->ops->port_stp_state_set)
38 ds->ops->port_stp_state_set(ds, port, state);
40 if (ds->ops->port_fast_age) {
41 /* Fast age FDB entries or flush appropriate forwarding database
42 * for the given port, if we are moving it from Learning or
43 * Forwarding state, to Disabled or Blocking or Listening state.
46 if ((dp->stp_state == BR_STATE_LEARNING ||
47 dp->stp_state == BR_STATE_FORWARDING) &&
48 (state == BR_STATE_DISABLED ||
49 state == BR_STATE_BLOCKING ||
50 state == BR_STATE_LISTENING))
51 ds->ops->port_fast_age(ds, port);
54 dp->stp_state = state;
59 void dsa_port_set_state_now(struct dsa_port *dp, u8 state)
63 err = dsa_port_set_state(dp, state, NULL);
65 pr_err("DSA: failed to set STP state %u (%d)\n", state, err);
68 int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br)
70 struct dsa_notifier_bridge_info info = {
71 .sw_index = dp->ds->index,
77 /* Here the port is already bridged. Reflect the current configuration
78 * so that drivers can program their chips accordingly.
82 err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_JOIN, &info);
84 /* The bridging is rolled back on error */
86 dp->bridge_dev = NULL;
91 void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
93 struct dsa_notifier_bridge_info info = {
94 .sw_index = dp->ds->index,
100 /* Here the port is already unbridged. Reflect the current configuration
101 * so that drivers can program their chips accordingly.
103 dp->bridge_dev = NULL;
105 err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_LEAVE, &info);
107 pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
109 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
110 * so allow it to be in BR_STATE_FORWARDING to be kept functional
112 dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
115 int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
116 struct switchdev_trans *trans)
118 struct dsa_switch *ds = dp->ds;
120 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
121 if (switchdev_trans_ph_prepare(trans))
124 if (ds->ops->port_vlan_filtering)
125 return ds->ops->port_vlan_filtering(ds, dp->index,
131 static unsigned int dsa_fastest_ageing_time(struct dsa_switch *ds,
132 unsigned int ageing_time)
136 for (i = 0; i < ds->num_ports; ++i) {
137 struct dsa_port *dp = &ds->ports[i];
139 if (dp->ageing_time && dp->ageing_time < ageing_time)
140 ageing_time = dp->ageing_time;
146 int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock,
147 struct switchdev_trans *trans)
149 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
150 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
151 struct dsa_switch *ds = dp->ds;
153 if (switchdev_trans_ph_prepare(trans)) {
154 if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
156 if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
161 /* Keep the fastest ageing time in case of multiple bridges */
162 dp->ageing_time = ageing_time;
163 ageing_time = dsa_fastest_ageing_time(ds, ageing_time);
165 if (ds->ops->set_ageing_time)
166 return ds->ops->set_ageing_time(ds, ageing_time);
171 int dsa_port_fdb_add(struct dsa_port *dp,
172 const struct switchdev_obj_port_fdb *fdb,
173 struct switchdev_trans *trans)
175 struct dsa_switch *ds = dp->ds;
177 if (switchdev_trans_ph_prepare(trans)) {
178 if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add)
181 return ds->ops->port_fdb_prepare(ds, dp->index, fdb, trans);
184 ds->ops->port_fdb_add(ds, dp->index, fdb, trans);
189 int dsa_port_fdb_del(struct dsa_port *dp,
190 const struct switchdev_obj_port_fdb *fdb)
192 struct dsa_switch *ds = dp->ds;
194 if (ds->ops->port_fdb_del)
197 return ds->ops->port_fdb_del(ds, dp->index, fdb);
200 int dsa_port_fdb_dump(struct dsa_port *dp, struct switchdev_obj_port_fdb *fdb,
201 switchdev_obj_dump_cb_t *cb)
203 struct dsa_switch *ds = dp->ds;
205 if (ds->ops->port_fdb_dump)
206 return ds->ops->port_fdb_dump(ds, dp->index, fdb, cb);
211 int dsa_port_mdb_add(struct dsa_port *dp,
212 const struct switchdev_obj_port_mdb *mdb,
213 struct switchdev_trans *trans)
215 struct dsa_switch *ds = dp->ds;
217 if (switchdev_trans_ph_prepare(trans)) {
218 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
221 return ds->ops->port_mdb_prepare(ds, dp->index, mdb, trans);
224 ds->ops->port_mdb_add(ds, dp->index, mdb, trans);
229 int dsa_port_mdb_del(struct dsa_port *dp,
230 const struct switchdev_obj_port_mdb *mdb)
232 struct dsa_switch *ds = dp->ds;
234 if (ds->ops->port_mdb_del)
235 return ds->ops->port_mdb_del(ds, dp->index, mdb);
240 int dsa_port_mdb_dump(struct dsa_port *dp, struct switchdev_obj_port_mdb *mdb,
241 switchdev_obj_dump_cb_t *cb)
243 struct dsa_switch *ds = dp->ds;
245 if (ds->ops->port_mdb_dump)
246 return ds->ops->port_mdb_dump(ds, dp->index, mdb, cb);