Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 1 | /* Copyright 2011-2014 Autronica Fire and Security AS |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify it |
| 4 | * under the terms of the GNU General Public License as published by the Free |
| 5 | * Software Foundation; either version 2 of the License, or (at your option) |
| 6 | * any later version. |
| 7 | * |
| 8 | * Author(s): |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 9 | * 2011-2014 Arvid Brodin, arvid.brodin@alten.se |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/netdevice.h> |
| 13 | #include <linux/rculist.h> |
| 14 | #include <linux/timer.h> |
| 15 | #include <linux/etherdevice.h> |
| 16 | #include "hsr_main.h" |
| 17 | #include "hsr_device.h" |
| 18 | #include "hsr_netlink.h" |
| 19 | #include "hsr_framereg.h" |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 20 | #include "hsr_slave.h" |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 21 | |
| 22 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 23 | static int hsr_netdev_notify(struct notifier_block *nb, unsigned long event, |
| 24 | void *ptr) |
| 25 | { |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 26 | struct net_device *dev; |
| 27 | struct hsr_port *port, *master; |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 28 | struct hsr_priv *hsr; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 29 | int mtu_max; |
| 30 | int res; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 31 | |
| 32 | dev = netdev_notifier_info_to_dev(ptr); |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 33 | port = hsr_port_get_rtnl(dev); |
| 34 | if (port == NULL) { |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 35 | if (!is_hsr_master(dev)) |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 36 | return NOTIFY_DONE; /* Not an HSR device */ |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 37 | hsr = netdev_priv(dev); |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 38 | port = hsr_port_get_hsr(hsr, HSR_PT_MASTER); |
Arvid Brodin | 56b08fd | 2015-02-27 21:26:03 +0100 | [diff] [blame] | 39 | if (port == NULL) { |
| 40 | /* Resend of notification concerning removed device? */ |
| 41 | return NOTIFY_DONE; |
| 42 | } |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 43 | } else { |
| 44 | hsr = port->hsr; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | switch (event) { |
| 48 | case NETDEV_UP: /* Administrative state DOWN */ |
| 49 | case NETDEV_DOWN: /* Administrative state UP */ |
| 50 | case NETDEV_CHANGE: /* Link (carrier) state changes */ |
Arvid Brodin | e9aae56 | 2014-07-04 23:36:40 +0200 | [diff] [blame] | 51 | hsr_check_carrier_and_operstate(hsr); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 52 | break; |
| 53 | case NETDEV_CHANGEADDR: |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 54 | if (port->type == HSR_PT_MASTER) { |
| 55 | /* This should not happen since there's no |
| 56 | * ndo_set_mac_address() for HSR devices - i.e. not |
| 57 | * supported. |
| 58 | */ |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 59 | break; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 60 | } |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 61 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 62 | master = hsr_port_get_hsr(hsr, HSR_PT_MASTER); |
| 63 | |
| 64 | if (port->type == HSR_PT_SLAVE_A) { |
| 65 | ether_addr_copy(master->dev->dev_addr, dev->dev_addr); |
| 66 | call_netdevice_notifiers(NETDEV_CHANGEADDR, master->dev); |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 67 | } |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 68 | |
| 69 | /* Make sure we recognize frames from ourselves in hsr_rcv() */ |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 70 | port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B); |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 71 | res = hsr_create_self_node(&hsr->self_node_db, |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 72 | master->dev->dev_addr, |
| 73 | port ? |
| 74 | port->dev->dev_addr : |
| 75 | master->dev->dev_addr); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 76 | if (res) |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 77 | netdev_warn(master->dev, |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 78 | "Could not update HSR node address.\n"); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 79 | break; |
| 80 | case NETDEV_CHANGEMTU: |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 81 | if (port->type == HSR_PT_MASTER) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 82 | break; /* Handled in ndo_change_mtu() */ |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 83 | mtu_max = hsr_get_max_mtu(port->hsr); |
| 84 | master = hsr_port_get_hsr(port->hsr, HSR_PT_MASTER); |
| 85 | master->dev->mtu = mtu_max; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 86 | break; |
| 87 | case NETDEV_UNREGISTER: |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 88 | hsr_del_port(port); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 89 | break; |
| 90 | case NETDEV_PRE_TYPE_CHANGE: |
| 91 | /* HSR works only on Ethernet devices. Refuse slave to change |
| 92 | * its type. |
| 93 | */ |
| 94 | return NOTIFY_BAD; |
| 95 | } |
| 96 | |
| 97 | return NOTIFY_DONE; |
| 98 | } |
| 99 | |
| 100 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 101 | struct hsr_port *hsr_port_get_hsr(struct hsr_priv *hsr, enum hsr_port_type pt) |
| 102 | { |
| 103 | struct hsr_port *port; |
| 104 | |
| 105 | hsr_for_each_port(hsr, port) |
| 106 | if (port->type == pt) |
| 107 | return port; |
| 108 | return NULL; |
| 109 | } |
| 110 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 111 | static struct notifier_block hsr_nb = { |
| 112 | .notifier_call = hsr_netdev_notify, /* Slave event notifications */ |
| 113 | }; |
| 114 | |
| 115 | |
| 116 | static int __init hsr_init(void) |
| 117 | { |
| 118 | int res; |
| 119 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 120 | BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_HLEN); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 121 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 122 | register_netdevice_notifier(&hsr_nb); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 123 | res = hsr_netlink_init(); |
| 124 | |
| 125 | return res; |
| 126 | } |
| 127 | |
| 128 | static void __exit hsr_exit(void) |
| 129 | { |
| 130 | unregister_netdevice_notifier(&hsr_nb); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 131 | hsr_netlink_exit(); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | module_init(hsr_init); |
| 135 | module_exit(hsr_exit); |
| 136 | MODULE_LICENSE("GPL"); |