blob: b9988a662ee1ac14c3ad8b96ea18a2dca92f5254 [file] [log] [blame]
Murali Karicheri0e7623b2019-04-05 13:31:34 -04001// SPDX-License-Identifier: GPL-2.0
Arvid Brodin70ebe4a2014-07-04 23:34:38 +02002/* Copyright 2011-2014 Autronica Fire and Security AS
Arvid Brodinf4214362013-10-30 21:10:47 +01003 *
Arvid Brodinf4214362013-10-30 21:10:47 +01004 * Author(s):
Arvid Brodin70ebe4a2014-07-04 23:34:38 +02005 * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
Arvid Brodinf4214362013-10-30 21:10:47 +01006 */
7
8#include <linux/netdevice.h>
9#include <linux/rculist.h>
10#include <linux/timer.h>
11#include <linux/etherdevice.h>
12#include "hsr_main.h"
13#include "hsr_device.h"
14#include "hsr_netlink.h"
15#include "hsr_framereg.h"
Arvid Brodin51f3c602014-07-04 23:37:27 +020016#include "hsr_slave.h"
Arvid Brodinf4214362013-10-30 21:10:47 +010017
Arvid Brodinf4214362013-10-30 21:10:47 +010018static int hsr_netdev_notify(struct notifier_block *nb, unsigned long event,
19 void *ptr)
20{
Arvid Brodinc5a75912014-07-04 23:38:05 +020021 struct net_device *dev;
22 struct hsr_port *port, *master;
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020023 struct hsr_priv *hsr;
Arvid Brodinf4214362013-10-30 21:10:47 +010024 int mtu_max;
25 int res;
Arvid Brodinf4214362013-10-30 21:10:47 +010026
27 dev = netdev_notifier_info_to_dev(ptr);
Arvid Brodinc5a75912014-07-04 23:38:05 +020028 port = hsr_port_get_rtnl(dev);
Murali Karicheri05ca6e62019-04-05 13:31:28 -040029 if (!port) {
Arvid Brodinf4214362013-10-30 21:10:47 +010030 if (!is_hsr_master(dev))
Arvid Brodinc5a75912014-07-04 23:38:05 +020031 return NOTIFY_DONE; /* Not an HSR device */
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020032 hsr = netdev_priv(dev);
Arvid Brodinc5a75912014-07-04 23:38:05 +020033 port = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
Murali Karicheri05ca6e62019-04-05 13:31:28 -040034 if (!port) {
Arvid Brodin56b08fd2015-02-27 21:26:03 +010035 /* Resend of notification concerning removed device? */
36 return NOTIFY_DONE;
37 }
Arvid Brodinc5a75912014-07-04 23:38:05 +020038 } else {
39 hsr = port->hsr;
Arvid Brodinf4214362013-10-30 21:10:47 +010040 }
41
42 switch (event) {
43 case NETDEV_UP: /* Administrative state DOWN */
44 case NETDEV_DOWN: /* Administrative state UP */
45 case NETDEV_CHANGE: /* Link (carrier) state changes */
Arvid Brodine9aae562014-07-04 23:36:40 +020046 hsr_check_carrier_and_operstate(hsr);
Arvid Brodinf4214362013-10-30 21:10:47 +010047 break;
48 case NETDEV_CHANGEADDR:
Arvid Brodinc5a75912014-07-04 23:38:05 +020049 if (port->type == HSR_PT_MASTER) {
50 /* This should not happen since there's no
51 * ndo_set_mac_address() for HSR devices - i.e. not
52 * supported.
53 */
Arvid Brodinf4214362013-10-30 21:10:47 +010054 break;
Arvid Brodinc5a75912014-07-04 23:38:05 +020055 }
Arvid Brodinf4214362013-10-30 21:10:47 +010056
Arvid Brodinc5a75912014-07-04 23:38:05 +020057 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
58
59 if (port->type == HSR_PT_SLAVE_A) {
60 ether_addr_copy(master->dev->dev_addr, dev->dev_addr);
Murali Karicherid595b852019-04-05 13:31:23 -040061 call_netdevice_notifiers(NETDEV_CHANGEADDR,
62 master->dev);
Arvid Brodin51f3c602014-07-04 23:37:27 +020063 }
Arvid Brodinf4214362013-10-30 21:10:47 +010064
65 /* Make sure we recognize frames from ourselves in hsr_rcv() */
Arvid Brodinc5a75912014-07-04 23:38:05 +020066 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020067 res = hsr_create_self_node(&hsr->self_node_db,
Arvid Brodinc5a75912014-07-04 23:38:05 +020068 master->dev->dev_addr,
69 port ?
70 port->dev->dev_addr :
71 master->dev->dev_addr);
Arvid Brodinf4214362013-10-30 21:10:47 +010072 if (res)
Arvid Brodinc5a75912014-07-04 23:38:05 +020073 netdev_warn(master->dev,
Arvid Brodinf4214362013-10-30 21:10:47 +010074 "Could not update HSR node address.\n");
Arvid Brodinf4214362013-10-30 21:10:47 +010075 break;
76 case NETDEV_CHANGEMTU:
Arvid Brodinc5a75912014-07-04 23:38:05 +020077 if (port->type == HSR_PT_MASTER)
Arvid Brodinf4214362013-10-30 21:10:47 +010078 break; /* Handled in ndo_change_mtu() */
Arvid Brodinc5a75912014-07-04 23:38:05 +020079 mtu_max = hsr_get_max_mtu(port->hsr);
80 master = hsr_port_get_hsr(port->hsr, HSR_PT_MASTER);
81 master->dev->mtu = mtu_max;
Arvid Brodinf4214362013-10-30 21:10:47 +010082 break;
83 case NETDEV_UNREGISTER:
Arvid Brodinc5a75912014-07-04 23:38:05 +020084 hsr_del_port(port);
Arvid Brodinf4214362013-10-30 21:10:47 +010085 break;
86 case NETDEV_PRE_TYPE_CHANGE:
87 /* HSR works only on Ethernet devices. Refuse slave to change
88 * its type.
89 */
90 return NOTIFY_BAD;
91 }
92
93 return NOTIFY_DONE;
94}
95
Arvid Brodinc5a75912014-07-04 23:38:05 +020096struct hsr_port *hsr_port_get_hsr(struct hsr_priv *hsr, enum hsr_port_type pt)
97{
98 struct hsr_port *port;
99
100 hsr_for_each_port(hsr, port)
101 if (port->type == pt)
102 return port;
103 return NULL;
104}
105
Arvid Brodinf4214362013-10-30 21:10:47 +0100106static struct notifier_block hsr_nb = {
107 .notifier_call = hsr_netdev_notify, /* Slave event notifications */
108};
109
Arvid Brodinf4214362013-10-30 21:10:47 +0100110static int __init hsr_init(void)
111{
112 int res;
113
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200114 BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_HLEN);
Arvid Brodinf4214362013-10-30 21:10:47 +0100115
Arvid Brodinf4214362013-10-30 21:10:47 +0100116 register_netdevice_notifier(&hsr_nb);
Arvid Brodinf4214362013-10-30 21:10:47 +0100117 res = hsr_netlink_init();
118
119 return res;
120}
121
122static void __exit hsr_exit(void)
123{
124 unregister_netdevice_notifier(&hsr_nb);
Arvid Brodinf4214362013-10-30 21:10:47 +0100125 hsr_netlink_exit();
Arvid Brodinf4214362013-10-30 21:10:47 +0100126}
127
128module_init(hsr_init);
129module_exit(hsr_exit);
130MODULE_LICENSE("GPL");