blob: f7e284f23b1f3ef941106cb01336eba026b26f73 [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
Murali Karicheri8f4c0e02020-07-22 10:40:16 -04006 *
7 * Event handling for HSR and PRP devices.
Arvid Brodinf4214362013-10-30 21:10:47 +01008 */
9
10#include <linux/netdevice.h>
Taehee Yoode0083c2020-06-21 13:46:25 +000011#include <net/rtnetlink.h>
Arvid Brodinf4214362013-10-30 21:10:47 +010012#include <linux/rculist.h>
13#include <linux/timer.h>
14#include <linux/etherdevice.h>
15#include "hsr_main.h"
16#include "hsr_device.h"
17#include "hsr_netlink.h"
18#include "hsr_framereg.h"
Arvid Brodin51f3c602014-07-04 23:37:27 +020019#include "hsr_slave.h"
Arvid Brodinf4214362013-10-30 21:10:47 +010020
Taehee Yoo34a9c362020-04-30 17:37:02 +000021static bool hsr_slave_empty(struct hsr_priv *hsr)
22{
23 struct hsr_port *port;
24
25 hsr_for_each_port(hsr, port)
26 if (port->type != HSR_PT_MASTER)
27 return false;
28 return true;
29}
30
Arvid Brodinf4214362013-10-30 21:10:47 +010031static int hsr_netdev_notify(struct notifier_block *nb, unsigned long event,
32 void *ptr)
33{
Arvid Brodinc5a75912014-07-04 23:38:05 +020034 struct hsr_port *port, *master;
Taehee Yoo34a9c362020-04-30 17:37:02 +000035 struct net_device *dev;
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020036 struct hsr_priv *hsr;
Taehee Yoo34a9c362020-04-30 17:37:02 +000037 LIST_HEAD(list_kill);
Arvid Brodinf4214362013-10-30 21:10:47 +010038 int mtu_max;
39 int res;
Arvid Brodinf4214362013-10-30 21:10:47 +010040
41 dev = netdev_notifier_info_to_dev(ptr);
Arvid Brodinc5a75912014-07-04 23:38:05 +020042 port = hsr_port_get_rtnl(dev);
Murali Karicheri05ca6e62019-04-05 13:31:28 -040043 if (!port) {
Arvid Brodinf4214362013-10-30 21:10:47 +010044 if (!is_hsr_master(dev))
Arvid Brodinc5a75912014-07-04 23:38:05 +020045 return NOTIFY_DONE; /* Not an HSR device */
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020046 hsr = netdev_priv(dev);
Arvid Brodinc5a75912014-07-04 23:38:05 +020047 port = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
Murali Karicheri05ca6e62019-04-05 13:31:28 -040048 if (!port) {
Arvid Brodin56b08fd2015-02-27 21:26:03 +010049 /* Resend of notification concerning removed device? */
50 return NOTIFY_DONE;
51 }
Arvid Brodinc5a75912014-07-04 23:38:05 +020052 } else {
53 hsr = port->hsr;
Arvid Brodinf4214362013-10-30 21:10:47 +010054 }
55
56 switch (event) {
57 case NETDEV_UP: /* Administrative state DOWN */
58 case NETDEV_DOWN: /* Administrative state UP */
59 case NETDEV_CHANGE: /* Link (carrier) state changes */
Arvid Brodine9aae562014-07-04 23:36:40 +020060 hsr_check_carrier_and_operstate(hsr);
Arvid Brodinf4214362013-10-30 21:10:47 +010061 break;
Taehee Yoo4c2d5e32019-12-22 11:26:39 +000062 case NETDEV_CHANGENAME:
Taehee Yoo04b69422019-12-28 16:28:09 +000063 if (is_hsr_master(dev))
64 hsr_debugfs_rename(dev);
Taehee Yoo4c2d5e32019-12-22 11:26:39 +000065 break;
Arvid Brodinf4214362013-10-30 21:10:47 +010066 case NETDEV_CHANGEADDR:
Arvid Brodinc5a75912014-07-04 23:38:05 +020067 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
70 * supported.
71 */
Arvid Brodinf4214362013-10-30 21:10:47 +010072 break;
Arvid Brodinc5a75912014-07-04 23:38:05 +020073 }
Arvid Brodinf4214362013-10-30 21:10:47 +010074
Arvid Brodinc5a75912014-07-04 23:38:05 +020075 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
76
77 if (port->type == HSR_PT_SLAVE_A) {
78 ether_addr_copy(master->dev->dev_addr, dev->dev_addr);
Murali Karicherid595b852019-04-05 13:31:23 -040079 call_netdevice_notifiers(NETDEV_CHANGEADDR,
80 master->dev);
Arvid Brodin51f3c602014-07-04 23:37:27 +020081 }
Arvid Brodinf4214362013-10-30 21:10:47 +010082
83 /* Make sure we recognize frames from ourselves in hsr_rcv() */
Arvid Brodinc5a75912014-07-04 23:38:05 +020084 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
Taehee Yoo92a35672019-12-22 11:26:54 +000085 res = hsr_create_self_node(hsr,
Arvid Brodinc5a75912014-07-04 23:38:05 +020086 master->dev->dev_addr,
87 port ?
88 port->dev->dev_addr :
89 master->dev->dev_addr);
Arvid Brodinf4214362013-10-30 21:10:47 +010090 if (res)
Arvid Brodinc5a75912014-07-04 23:38:05 +020091 netdev_warn(master->dev,
Arvid Brodinf4214362013-10-30 21:10:47 +010092 "Could not update HSR node address.\n");
Arvid Brodinf4214362013-10-30 21:10:47 +010093 break;
94 case NETDEV_CHANGEMTU:
Arvid Brodinc5a75912014-07-04 23:38:05 +020095 if (port->type == HSR_PT_MASTER)
Arvid Brodinf4214362013-10-30 21:10:47 +010096 break; /* Handled in ndo_change_mtu() */
Arvid Brodinc5a75912014-07-04 23:38:05 +020097 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;
Arvid Brodinf4214362013-10-30 21:10:47 +0100100 break;
101 case NETDEV_UNREGISTER:
Taehee Yoo34a9c362020-04-30 17:37:02 +0000102 if (!is_hsr_master(dev)) {
103 master = hsr_port_get_hsr(port->hsr, HSR_PT_MASTER);
Taehee Yooe0a4b992020-02-28 18:02:10 +0000104 hsr_del_port(port);
Taehee Yoo34a9c362020-04-30 17:37:02 +0000105 if (hsr_slave_empty(master->hsr)) {
Taehee Yoode0083c2020-06-21 13:46:25 +0000106 const struct rtnl_link_ops *ops;
107
108 ops = master->dev->rtnl_link_ops;
109 ops->dellink(master->dev, &list_kill);
Taehee Yoo34a9c362020-04-30 17:37:02 +0000110 unregister_netdevice_many(&list_kill);
111 }
112 }
Arvid Brodinf4214362013-10-30 21:10:47 +0100113 break;
114 case NETDEV_PRE_TYPE_CHANGE:
115 /* HSR works only on Ethernet devices. Refuse slave to change
116 * its type.
117 */
118 return NOTIFY_BAD;
119 }
120
121 return NOTIFY_DONE;
122}
123
Arvid Brodinc5a75912014-07-04 23:38:05 +0200124struct hsr_port *hsr_port_get_hsr(struct hsr_priv *hsr, enum hsr_port_type pt)
125{
126 struct hsr_port *port;
127
128 hsr_for_each_port(hsr, port)
129 if (port->type == pt)
130 return port;
131 return NULL;
132}
133
George McCollisterdcf0cd12021-02-09 19:02:11 -0600134int hsr_get_version(struct net_device *dev, enum hsr_version *ver)
135{
136 struct hsr_priv *hsr;
137
138 hsr = netdev_priv(dev);
139 *ver = hsr->prot_version;
140
141 return 0;
142}
143EXPORT_SYMBOL(hsr_get_version);
144
Arvid Brodinf4214362013-10-30 21:10:47 +0100145static struct notifier_block hsr_nb = {
146 .notifier_call = hsr_netdev_notify, /* Slave event notifications */
147};
148
Arvid Brodinf4214362013-10-30 21:10:47 +0100149static int __init hsr_init(void)
150{
151 int res;
152
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200153 BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_HLEN);
Arvid Brodinf4214362013-10-30 21:10:47 +0100154
Arvid Brodinf4214362013-10-30 21:10:47 +0100155 register_netdevice_notifier(&hsr_nb);
Arvid Brodinf4214362013-10-30 21:10:47 +0100156 res = hsr_netlink_init();
157
158 return res;
159}
160
161static void __exit hsr_exit(void)
162{
Arvid Brodinf4214362013-10-30 21:10:47 +0100163 hsr_netlink_exit();
Taehee Yooc6c4ccd2019-12-22 11:26:27 +0000164 hsr_debugfs_remove_root();
Taehee Yoode0083c2020-06-21 13:46:25 +0000165 unregister_netdevice_notifier(&hsr_nb);
Arvid Brodinf4214362013-10-30 21:10:47 +0100166}
167
168module_init(hsr_init);
169module_exit(hsr_exit);
170MODULE_LICENSE("GPL");