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 |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 6 | */ |
| 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 Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 16 | #include "hsr_slave.h" |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 17 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 18 | static int hsr_netdev_notify(struct notifier_block *nb, unsigned long event, |
| 19 | void *ptr) |
| 20 | { |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 21 | struct net_device *dev; |
| 22 | struct hsr_port *port, *master; |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 23 | struct hsr_priv *hsr; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 24 | int mtu_max; |
| 25 | int res; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 26 | |
| 27 | dev = netdev_notifier_info_to_dev(ptr); |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 28 | port = hsr_port_get_rtnl(dev); |
Murali Karicheri | 05ca6e6 | 2019-04-05 13:31:28 -0400 | [diff] [blame] | 29 | if (!port) { |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 30 | if (!is_hsr_master(dev)) |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 31 | return NOTIFY_DONE; /* Not an HSR device */ |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 32 | hsr = netdev_priv(dev); |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 33 | port = hsr_port_get_hsr(hsr, HSR_PT_MASTER); |
Murali Karicheri | 05ca6e6 | 2019-04-05 13:31:28 -0400 | [diff] [blame] | 34 | if (!port) { |
Arvid Brodin | 56b08fd | 2015-02-27 21:26:03 +0100 | [diff] [blame] | 35 | /* Resend of notification concerning removed device? */ |
| 36 | return NOTIFY_DONE; |
| 37 | } |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 38 | } else { |
| 39 | hsr = port->hsr; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 40 | } |
| 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 Brodin | e9aae56 | 2014-07-04 23:36:40 +0200 | [diff] [blame] | 46 | hsr_check_carrier_and_operstate(hsr); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 47 | break; |
| 48 | case NETDEV_CHANGEADDR: |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 49 | 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 Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 54 | break; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 55 | } |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 56 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 57 | 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 Karicheri | d595b85 | 2019-04-05 13:31:23 -0400 | [diff] [blame] | 61 | call_netdevice_notifiers(NETDEV_CHANGEADDR, |
| 62 | master->dev); |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 63 | } |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 64 | |
| 65 | /* Make sure we recognize frames from ourselves in hsr_rcv() */ |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 66 | port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B); |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 67 | res = hsr_create_self_node(&hsr->self_node_db, |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 68 | master->dev->dev_addr, |
| 69 | port ? |
| 70 | port->dev->dev_addr : |
| 71 | master->dev->dev_addr); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 72 | if (res) |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 73 | netdev_warn(master->dev, |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 74 | "Could not update HSR node address.\n"); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 75 | break; |
| 76 | case NETDEV_CHANGEMTU: |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 77 | if (port->type == HSR_PT_MASTER) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 78 | break; /* Handled in ndo_change_mtu() */ |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 79 | 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 Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 82 | break; |
| 83 | case NETDEV_UNREGISTER: |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 84 | hsr_del_port(port); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 85 | 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 Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 96 | struct 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 Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 106 | static struct notifier_block hsr_nb = { |
| 107 | .notifier_call = hsr_netdev_notify, /* Slave event notifications */ |
| 108 | }; |
| 109 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 110 | static int __init hsr_init(void) |
| 111 | { |
| 112 | int res; |
| 113 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 114 | BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_HLEN); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 115 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 116 | register_netdevice_notifier(&hsr_nb); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 117 | res = hsr_netlink_init(); |
| 118 | |
| 119 | return res; |
| 120 | } |
| 121 | |
| 122 | static void __exit hsr_exit(void) |
| 123 | { |
| 124 | unregister_netdevice_notifier(&hsr_nb); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 125 | hsr_netlink_exit(); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | module_init(hsr_init); |
| 129 | module_exit(hsr_exit); |
| 130 | MODULE_LICENSE("GPL"); |