blob: f477053308d2948ada9bae7f87c4559bb2bb8f75 [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 Didelotea5dd342017-05-17 15:46:03 -040015
16#include "dsa_priv.h"
Vivien Didelotf515f192017-02-03 13:20:20 -050017
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050018static int dsa_switch_bridge_join(struct dsa_switch *ds,
19 struct dsa_notifier_bridge_info *info)
20{
21 if (ds->index == info->sw_index && ds->ops->port_bridge_join)
22 return ds->ops->port_bridge_join(ds, info->port, info->br);
23
Vivien Didelot40ef2c92017-03-30 17:37:14 -040024 if (ds->index != info->sw_index && ds->ops->crosschip_bridge_join)
25 return ds->ops->crosschip_bridge_join(ds, info->sw_index,
26 info->port, info->br);
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050027
28 return 0;
29}
30
31static int dsa_switch_bridge_leave(struct dsa_switch *ds,
32 struct dsa_notifier_bridge_info *info)
33{
34 if (ds->index == info->sw_index && ds->ops->port_bridge_leave)
35 ds->ops->port_bridge_leave(ds, info->port, info->br);
36
Vivien Didelot40ef2c92017-03-30 17:37:14 -040037 if (ds->index != info->sw_index && ds->ops->crosschip_bridge_leave)
38 ds->ops->crosschip_bridge_leave(ds, info->sw_index, info->port,
39 info->br);
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050040
41 return 0;
42}
43
Vivien Didelotf515f192017-02-03 13:20:20 -050044static int dsa_switch_event(struct notifier_block *nb,
45 unsigned long event, void *info)
46{
47 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
48 int err;
49
50 switch (event) {
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050051 case DSA_NOTIFIER_BRIDGE_JOIN:
52 err = dsa_switch_bridge_join(ds, info);
53 break;
54 case DSA_NOTIFIER_BRIDGE_LEAVE:
55 err = dsa_switch_bridge_leave(ds, info);
56 break;
Vivien Didelotf515f192017-02-03 13:20:20 -050057 default:
58 err = -EOPNOTSUPP;
59 break;
60 }
61
62 /* Non-switchdev operations cannot be rolled back. If a DSA driver
63 * returns an error during the chained call, switch chips may be in an
64 * inconsistent state.
65 */
66 if (err)
67 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
68 event, err);
69
70 return notifier_from_errno(err);
71}
72
73int dsa_switch_register_notifier(struct dsa_switch *ds)
74{
75 ds->nb.notifier_call = dsa_switch_event;
76
77 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
78}
79
80void dsa_switch_unregister_notifier(struct dsa_switch *ds)
81{
82 int err;
83
84 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
85 if (err)
86 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
87}