1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2011-2014 Autronica Fire and Security AS
5 * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
7 * Event handling for HSR and PRP devices.
10 #include <linux/netdevice.h>
11 #include <net/rtnetlink.h>
12 #include <linux/rculist.h>
13 #include <linux/timer.h>
14 #include <linux/etherdevice.h>
16 #include "hsr_device.h"
17 #include "hsr_netlink.h"
18 #include "hsr_framereg.h"
19 #include "hsr_slave.h"
21 static bool hsr_slave_empty(struct hsr_priv
*hsr
)
23 struct hsr_port
*port
;
25 hsr_for_each_port(hsr
, port
)
26 if (port
->type
!= HSR_PT_MASTER
)
31 static int hsr_netdev_notify(struct notifier_block
*nb
, unsigned long event
,
34 struct hsr_port
*port
, *master
;
35 struct net_device
*dev
;
41 dev
= netdev_notifier_info_to_dev(ptr
);
42 port
= hsr_port_get_rtnl(dev
);
44 if (!is_hsr_master(dev
))
45 return NOTIFY_DONE
; /* Not an HSR device */
46 hsr
= netdev_priv(dev
);
47 port
= hsr_port_get_hsr(hsr
, HSR_PT_MASTER
);
49 /* Resend of notification concerning removed device? */
57 case NETDEV_UP
: /* Administrative state DOWN */
58 case NETDEV_DOWN
: /* Administrative state UP */
59 case NETDEV_CHANGE
: /* Link (carrier) state changes */
60 hsr_check_carrier_and_operstate(hsr
);
62 case NETDEV_CHANGENAME
:
63 if (is_hsr_master(dev
))
64 hsr_debugfs_rename(dev
);
66 case NETDEV_CHANGEADDR
:
67 if (port
->type
== HSR_PT_MASTER
) {
68 /* This should not happen since there's no
69 * ndo_set_mac_address() for HSR devices - i.e. not
75 master
= hsr_port_get_hsr(hsr
, HSR_PT_MASTER
);
77 if (port
->type
== HSR_PT_SLAVE_A
) {
78 ether_addr_copy(master
->dev
->dev_addr
, dev
->dev_addr
);
79 call_netdevice_notifiers(NETDEV_CHANGEADDR
,
83 /* Make sure we recognize frames from ourselves in hsr_rcv() */
84 port
= hsr_port_get_hsr(hsr
, HSR_PT_SLAVE_B
);
85 res
= hsr_create_self_node(hsr
,
86 master
->dev
->dev_addr
,
89 master
->dev
->dev_addr
);
91 netdev_warn(master
->dev
,
92 "Could not update HSR node address.\n");
94 case NETDEV_CHANGEMTU
:
95 if (port
->type
== HSR_PT_MASTER
)
96 break; /* Handled in ndo_change_mtu() */
97 mtu_max
= hsr_get_max_mtu(port
->hsr
);
98 master
= hsr_port_get_hsr(port
->hsr
, HSR_PT_MASTER
);
99 master
->dev
->mtu
= mtu_max
;
101 case NETDEV_UNREGISTER
:
102 if (!is_hsr_master(dev
)) {
103 master
= hsr_port_get_hsr(port
->hsr
, HSR_PT_MASTER
);
105 if (hsr_slave_empty(master
->hsr
)) {
106 const struct rtnl_link_ops
*ops
;
108 ops
= master
->dev
->rtnl_link_ops
;
109 ops
->dellink(master
->dev
, &list_kill
);
110 unregister_netdevice_many(&list_kill
);
114 case NETDEV_PRE_TYPE_CHANGE
:
115 /* HSR works only on Ethernet devices. Refuse slave to change
124 struct hsr_port
*hsr_port_get_hsr(struct hsr_priv
*hsr
, enum hsr_port_type pt
)
126 struct hsr_port
*port
;
128 hsr_for_each_port(hsr
, port
)
129 if (port
->type
== pt
)
134 static struct notifier_block hsr_nb
= {
135 .notifier_call
= hsr_netdev_notify
, /* Slave event notifications */
138 static int __init
hsr_init(void)
142 BUILD_BUG_ON(sizeof(struct hsr_tag
) != HSR_HLEN
);
144 register_netdevice_notifier(&hsr_nb
);
145 res
= hsr_netlink_init();
150 static void __exit
hsr_exit(void)
153 hsr_debugfs_remove_root();
154 unregister_netdevice_notifier(&hsr_nb
);
157 module_init(hsr_init
);
158 module_exit(hsr_exit
);
159 MODULE_LICENSE("GPL");