blob: 779d28b65417a6e62b687d8f5ea36d6be285f417 [file] [log] [blame]
Arvid Brodin70ebe4a2014-07-04 23:34:38 +02001/* Copyright 2011-2014 Autronica Fire and Security AS
Arvid Brodinf4214362013-10-30 21:10:47 +01002 *
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 Brodin70ebe4a2014-07-04 23:34:38 +02009 * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
Arvid Brodinf4214362013-10-30 21:10:47 +010010 */
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 Brodin51f3c602014-07-04 23:37:27 +020020#include "hsr_slave.h"
Arvid Brodinf4214362013-10-30 21:10:47 +010021
22
Arvid Brodinf4214362013-10-30 21:10:47 +010023static int hsr_netdev_notify(struct notifier_block *nb, unsigned long event,
24 void *ptr)
25{
Arvid Brodinc5a75912014-07-04 23:38:05 +020026 struct net_device *dev;
27 struct hsr_port *port, *master;
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020028 struct hsr_priv *hsr;
Arvid Brodinf4214362013-10-30 21:10:47 +010029 int mtu_max;
30 int res;
Arvid Brodinf4214362013-10-30 21:10:47 +010031
32 dev = netdev_notifier_info_to_dev(ptr);
Arvid Brodinc5a75912014-07-04 23:38:05 +020033 port = hsr_port_get_rtnl(dev);
34 if (port == NULL) {
Arvid Brodinf4214362013-10-30 21:10:47 +010035 if (!is_hsr_master(dev))
Arvid Brodinc5a75912014-07-04 23:38:05 +020036 return NOTIFY_DONE; /* Not an HSR device */
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020037 hsr = netdev_priv(dev);
Arvid Brodinc5a75912014-07-04 23:38:05 +020038 port = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
39 } else {
40 hsr = port->hsr;
Arvid Brodinf4214362013-10-30 21:10:47 +010041 }
42
43 switch (event) {
44 case NETDEV_UP: /* Administrative state DOWN */
45 case NETDEV_DOWN: /* Administrative state UP */
46 case NETDEV_CHANGE: /* Link (carrier) state changes */
Arvid Brodine9aae562014-07-04 23:36:40 +020047 hsr_check_carrier_and_operstate(hsr);
Arvid Brodinf4214362013-10-30 21:10:47 +010048 break;
49 case NETDEV_CHANGEADDR:
Arvid Brodinc5a75912014-07-04 23:38:05 +020050 if (port->type == HSR_PT_MASTER) {
51 /* This should not happen since there's no
52 * ndo_set_mac_address() for HSR devices - i.e. not
53 * supported.
54 */
Arvid Brodinf4214362013-10-30 21:10:47 +010055 break;
Arvid Brodinc5a75912014-07-04 23:38:05 +020056 }
Arvid Brodinf4214362013-10-30 21:10:47 +010057
Arvid Brodinc5a75912014-07-04 23:38:05 +020058 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
59
60 if (port->type == HSR_PT_SLAVE_A) {
61 ether_addr_copy(master->dev->dev_addr, dev->dev_addr);
62 call_netdevice_notifiers(NETDEV_CHANGEADDR, master->dev);
Arvid Brodin51f3c602014-07-04 23:37:27 +020063 }
Arvid Brodinf4214362013-10-30 21:10:47 +010064
65 /* Make sure we recognize frames from ourselves in hsr_rcv() */
Arvid Brodinc5a75912014-07-04 23:38:05 +020066 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020067 res = hsr_create_self_node(&hsr->self_node_db,
Arvid Brodinc5a75912014-07-04 23:38:05 +020068 master->dev->dev_addr,
69 port ?
70 port->dev->dev_addr :
71 master->dev->dev_addr);
Arvid Brodinf4214362013-10-30 21:10:47 +010072 if (res)
Arvid Brodinc5a75912014-07-04 23:38:05 +020073 netdev_warn(master->dev,
Arvid Brodinf4214362013-10-30 21:10:47 +010074 "Could not update HSR node address.\n");
Arvid Brodinf4214362013-10-30 21:10:47 +010075 break;
76 case NETDEV_CHANGEMTU:
Arvid Brodinc5a75912014-07-04 23:38:05 +020077 if (port->type == HSR_PT_MASTER)
Arvid Brodinf4214362013-10-30 21:10:47 +010078 break; /* Handled in ndo_change_mtu() */
Arvid Brodinc5a75912014-07-04 23:38:05 +020079 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 Brodinf4214362013-10-30 21:10:47 +010082 break;
83 case NETDEV_UNREGISTER:
Arvid Brodinc5a75912014-07-04 23:38:05 +020084 hsr_del_port(port);
Arvid Brodinf4214362013-10-30 21:10:47 +010085 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
96
Arvid Brodinc5a75912014-07-04 23:38:05 +020097struct hsr_port *hsr_port_get_hsr(struct hsr_priv *hsr, enum hsr_port_type pt)
98{
99 struct hsr_port *port;
100
101 hsr_for_each_port(hsr, port)
102 if (port->type == pt)
103 return port;
104 return NULL;
105}
106
Arvid Brodinf4214362013-10-30 21:10:47 +0100107static struct notifier_block hsr_nb = {
108 .notifier_call = hsr_netdev_notify, /* Slave event notifications */
109};
110
111
112static int __init hsr_init(void)
113{
114 int res;
115
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200116 BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_HLEN);
Arvid Brodinf4214362013-10-30 21:10:47 +0100117
Arvid Brodinf4214362013-10-30 21:10:47 +0100118 register_netdevice_notifier(&hsr_nb);
Arvid Brodinf4214362013-10-30 21:10:47 +0100119 res = hsr_netlink_init();
120
121 return res;
122}
123
124static void __exit hsr_exit(void)
125{
126 unregister_netdevice_notifier(&hsr_nb);
Arvid Brodinf4214362013-10-30 21:10:47 +0100127 hsr_netlink_exit();
Arvid Brodinf4214362013-10-30 21:10:47 +0100128}
129
130module_init(hsr_init);
131module_exit(hsr_exit);
132MODULE_LICENSE("GPL");