Murali Karicheri | 0e7623b | 2019-04-05 13:31:34 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 2 | /* Copyright 2011-2014 Autronica Fire and Security AS |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 3 | * |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 4 | * Author(s): |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 5 | * 2011-2014 Arvid Brodin, arvid.brodin@alten.se |
Murali Karicheri | 8f4c0e0 | 2020-07-22 10:40:16 -0400 | [diff] [blame] | 6 | * |
| 7 | * Event handling for HSR and PRP devices. |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/netdevice.h> |
Taehee Yoo | de0083c | 2020-06-21 13:46:25 +0000 | [diff] [blame] | 11 | #include <net/rtnetlink.h> |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 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" |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 19 | #include "hsr_slave.h" |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 20 | |
Taehee Yoo | 34a9c36 | 2020-04-30 17:37:02 +0000 | [diff] [blame] | 21 | static 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 Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 31 | static int hsr_netdev_notify(struct notifier_block *nb, unsigned long event, |
| 32 | void *ptr) |
| 33 | { |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 34 | struct hsr_port *port, *master; |
Taehee Yoo | 34a9c36 | 2020-04-30 17:37:02 +0000 | [diff] [blame] | 35 | struct net_device *dev; |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 36 | struct hsr_priv *hsr; |
Taehee Yoo | 34a9c36 | 2020-04-30 17:37:02 +0000 | [diff] [blame] | 37 | LIST_HEAD(list_kill); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 38 | int mtu_max; |
| 39 | int res; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 40 | |
| 41 | dev = netdev_notifier_info_to_dev(ptr); |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 42 | port = hsr_port_get_rtnl(dev); |
Murali Karicheri | 05ca6e6 | 2019-04-05 13:31:28 -0400 | [diff] [blame] | 43 | if (!port) { |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 44 | if (!is_hsr_master(dev)) |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 45 | return NOTIFY_DONE; /* Not an HSR device */ |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 46 | hsr = netdev_priv(dev); |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 47 | port = hsr_port_get_hsr(hsr, HSR_PT_MASTER); |
Murali Karicheri | 05ca6e6 | 2019-04-05 13:31:28 -0400 | [diff] [blame] | 48 | if (!port) { |
Arvid Brodin | 56b08fd | 2015-02-27 21:26:03 +0100 | [diff] [blame] | 49 | /* Resend of notification concerning removed device? */ |
| 50 | return NOTIFY_DONE; |
| 51 | } |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 52 | } else { |
| 53 | hsr = port->hsr; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 54 | } |
| 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 Brodin | e9aae56 | 2014-07-04 23:36:40 +0200 | [diff] [blame] | 60 | hsr_check_carrier_and_operstate(hsr); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 61 | break; |
Taehee Yoo | 4c2d5e3 | 2019-12-22 11:26:39 +0000 | [diff] [blame] | 62 | case NETDEV_CHANGENAME: |
Taehee Yoo | 04b6942 | 2019-12-28 16:28:09 +0000 | [diff] [blame] | 63 | if (is_hsr_master(dev)) |
| 64 | hsr_debugfs_rename(dev); |
Taehee Yoo | 4c2d5e3 | 2019-12-22 11:26:39 +0000 | [diff] [blame] | 65 | break; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 66 | case NETDEV_CHANGEADDR: |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 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. |
| 71 | */ |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 72 | break; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 73 | } |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 74 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 75 | 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 Karicheri | d595b85 | 2019-04-05 13:31:23 -0400 | [diff] [blame] | 79 | call_netdevice_notifiers(NETDEV_CHANGEADDR, |
| 80 | master->dev); |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 81 | } |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 82 | |
| 83 | /* Make sure we recognize frames from ourselves in hsr_rcv() */ |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 84 | port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B); |
Taehee Yoo | 92a3567 | 2019-12-22 11:26:54 +0000 | [diff] [blame] | 85 | res = hsr_create_self_node(hsr, |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 86 | master->dev->dev_addr, |
| 87 | port ? |
| 88 | port->dev->dev_addr : |
| 89 | master->dev->dev_addr); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 90 | if (res) |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 91 | netdev_warn(master->dev, |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 92 | "Could not update HSR node address.\n"); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 93 | break; |
| 94 | case NETDEV_CHANGEMTU: |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 95 | if (port->type == HSR_PT_MASTER) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 96 | break; /* Handled in ndo_change_mtu() */ |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 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; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 100 | break; |
| 101 | case NETDEV_UNREGISTER: |
Taehee Yoo | 34a9c36 | 2020-04-30 17:37:02 +0000 | [diff] [blame] | 102 | if (!is_hsr_master(dev)) { |
| 103 | master = hsr_port_get_hsr(port->hsr, HSR_PT_MASTER); |
Taehee Yoo | e0a4b99 | 2020-02-28 18:02:10 +0000 | [diff] [blame] | 104 | hsr_del_port(port); |
Taehee Yoo | 34a9c36 | 2020-04-30 17:37:02 +0000 | [diff] [blame] | 105 | if (hsr_slave_empty(master->hsr)) { |
Taehee Yoo | de0083c | 2020-06-21 13:46:25 +0000 | [diff] [blame] | 106 | const struct rtnl_link_ops *ops; |
| 107 | |
| 108 | ops = master->dev->rtnl_link_ops; |
| 109 | ops->dellink(master->dev, &list_kill); |
Taehee Yoo | 34a9c36 | 2020-04-30 17:37:02 +0000 | [diff] [blame] | 110 | unregister_netdevice_many(&list_kill); |
| 111 | } |
| 112 | } |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 113 | 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 Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 124 | struct 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 McCollister | dcf0cd1 | 2021-02-09 19:02:11 -0600 | [diff] [blame] | 134 | int 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 | } |
| 143 | EXPORT_SYMBOL(hsr_get_version); |
| 144 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 145 | static struct notifier_block hsr_nb = { |
| 146 | .notifier_call = hsr_netdev_notify, /* Slave event notifications */ |
| 147 | }; |
| 148 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 149 | static int __init hsr_init(void) |
| 150 | { |
| 151 | int res; |
| 152 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 153 | BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_HLEN); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 154 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 155 | register_netdevice_notifier(&hsr_nb); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 156 | res = hsr_netlink_init(); |
| 157 | |
| 158 | return res; |
| 159 | } |
| 160 | |
| 161 | static void __exit hsr_exit(void) |
| 162 | { |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 163 | hsr_netlink_exit(); |
Taehee Yoo | c6c4ccd | 2019-12-22 11:26:27 +0000 | [diff] [blame] | 164 | hsr_debugfs_remove_root(); |
Taehee Yoo | de0083c | 2020-06-21 13:46:25 +0000 | [diff] [blame] | 165 | unregister_netdevice_notifier(&hsr_nb); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | module_init(hsr_init); |
| 169 | module_exit(hsr_exit); |
| 170 | MODULE_LICENSE("GPL"); |