Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / net / hsr / hsr_main.c
blob2fd1976e5b1c3865773f41e765e45bd5f2b5f296
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2011-2014 Autronica Fire and Security AS
4 * Author(s):
5 * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
7 * Event handling for HSR and PRP devices.
8 */
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>
15 #include "hsr_main.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)
27 return false;
28 return true;
31 static int hsr_netdev_notify(struct notifier_block *nb, unsigned long event,
32 void *ptr)
34 struct hsr_port *port, *master;
35 struct net_device *dev;
36 struct hsr_priv *hsr;
37 LIST_HEAD(list_kill);
38 int mtu_max;
39 int res;
41 dev = netdev_notifier_info_to_dev(ptr);
42 port = hsr_port_get_rtnl(dev);
43 if (!port) {
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);
48 if (!port) {
49 /* Resend of notification concerning removed device? */
50 return NOTIFY_DONE;
52 } else {
53 hsr = port->hsr;
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 */
60 hsr_check_carrier_and_operstate(hsr);
61 break;
62 case NETDEV_CHANGENAME:
63 if (is_hsr_master(dev))
64 hsr_debugfs_rename(dev);
65 break;
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
70 * supported.
72 break;
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,
80 master->dev);
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,
87 port ?
88 port->dev->dev_addr :
89 master->dev->dev_addr);
90 if (res)
91 netdev_warn(master->dev,
92 "Could not update HSR node address.\n");
93 break;
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;
100 break;
101 case NETDEV_UNREGISTER:
102 if (!is_hsr_master(dev)) {
103 master = hsr_port_get_hsr(port->hsr, HSR_PT_MASTER);
104 hsr_del_port(port);
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);
113 break;
114 case NETDEV_PRE_TYPE_CHANGE:
115 /* HSR works only on Ethernet devices. Refuse slave to change
116 * its type.
118 return NOTIFY_BAD;
121 return NOTIFY_DONE;
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)
130 return port;
131 return NULL;
134 static struct notifier_block hsr_nb = {
135 .notifier_call = hsr_netdev_notify, /* Slave event notifications */
138 static int __init hsr_init(void)
140 int res;
142 BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_HLEN);
144 register_netdevice_notifier(&hsr_nb);
145 res = hsr_netlink_init();
147 return res;
150 static void __exit hsr_exit(void)
152 hsr_netlink_exit();
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");