blob: 540770ecc8b08c7146b67cfc252e1f35f2080b16 [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 Didelotf515f192017-02-03 13:20:20 -050087static int dsa_switch_event(struct notifier_block *nb,
88 unsigned long event, void *info)
89{
90 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
91 int err;
92
93 switch (event) {
Vivien Didelot1faabf72017-05-19 17:00:52 -040094 case DSA_NOTIFIER_AGEING_TIME:
95 err = dsa_switch_ageing_time(ds, info);
96 break;
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050097 case DSA_NOTIFIER_BRIDGE_JOIN:
98 err = dsa_switch_bridge_join(ds, info);
99 break;
100 case DSA_NOTIFIER_BRIDGE_LEAVE:
101 err = dsa_switch_bridge_leave(ds, info);
102 break;
Vivien Didelotf515f192017-02-03 13:20:20 -0500103 default:
104 err = -EOPNOTSUPP;
105 break;
106 }
107
108 /* Non-switchdev operations cannot be rolled back. If a DSA driver
109 * returns an error during the chained call, switch chips may be in an
110 * inconsistent state.
111 */
112 if (err)
113 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
114 event, err);
115
116 return notifier_from_errno(err);
117}
118
119int dsa_switch_register_notifier(struct dsa_switch *ds)
120{
121 ds->nb.notifier_call = dsa_switch_event;
122
123 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
124}
125
126void dsa_switch_unregister_notifier(struct dsa_switch *ds)
127{
128 int err;
129
130 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
131 if (err)
132 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
133}