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 | * This file contains device methods for creating, using and destroying |
| 12 | * virtual HSR devices. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/netdevice.h> |
| 16 | #include <linux/skbuff.h> |
| 17 | #include <linux/etherdevice.h> |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 18 | #include <linux/rtnetlink.h> |
| 19 | #include <linux/pkt_sched.h> |
| 20 | #include "hsr_device.h" |
Arvid Brodin | 81ba6af | 2014-07-04 23:35:24 +0200 | [diff] [blame] | 21 | #include "hsr_slave.h" |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 22 | #include "hsr_framereg.h" |
| 23 | #include "hsr_main.h" |
| 24 | |
| 25 | |
| 26 | static bool is_admin_up(struct net_device *dev) |
| 27 | { |
| 28 | return dev && (dev->flags & IFF_UP); |
| 29 | } |
| 30 | |
| 31 | static bool is_slave_up(struct net_device *dev) |
| 32 | { |
| 33 | return dev && is_admin_up(dev) && netif_oper_up(dev); |
| 34 | } |
| 35 | |
| 36 | static void __hsr_set_operstate(struct net_device *dev, int transition) |
| 37 | { |
| 38 | write_lock_bh(&dev_base_lock); |
| 39 | if (dev->operstate != transition) { |
| 40 | dev->operstate = transition; |
| 41 | write_unlock_bh(&dev_base_lock); |
| 42 | netdev_state_change(dev); |
| 43 | } else { |
| 44 | write_unlock_bh(&dev_base_lock); |
| 45 | } |
| 46 | } |
| 47 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 48 | static void hsr_set_operstate(struct hsr_port *master, bool has_carrier) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 49 | { |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 50 | if (!is_admin_up(master->dev)) { |
| 51 | __hsr_set_operstate(master->dev, IF_OPER_DOWN); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 52 | return; |
| 53 | } |
| 54 | |
Arvid Brodin | e9aae56 | 2014-07-04 23:36:40 +0200 | [diff] [blame] | 55 | if (has_carrier) |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 56 | __hsr_set_operstate(master->dev, IF_OPER_UP); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 57 | else |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 58 | __hsr_set_operstate(master->dev, IF_OPER_LOWERLAYERDOWN); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 61 | static bool hsr_check_carrier(struct hsr_port *master) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 62 | { |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 63 | struct hsr_port *port; |
Arvid Brodin | e9aae56 | 2014-07-04 23:36:40 +0200 | [diff] [blame] | 64 | bool has_carrier; |
| 65 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 66 | has_carrier = false; |
| 67 | |
| 68 | rcu_read_lock(); |
| 69 | hsr_for_each_port(master->hsr, port) |
| 70 | if ((port->type != HSR_PT_MASTER) && is_slave_up(port->dev)) { |
| 71 | has_carrier = true; |
| 72 | break; |
| 73 | } |
| 74 | rcu_read_unlock(); |
Arvid Brodin | e9aae56 | 2014-07-04 23:36:40 +0200 | [diff] [blame] | 75 | |
| 76 | if (has_carrier) |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 77 | netif_carrier_on(master->dev); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 78 | else |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 79 | netif_carrier_off(master->dev); |
Arvid Brodin | e9aae56 | 2014-07-04 23:36:40 +0200 | [diff] [blame] | 80 | |
| 81 | return has_carrier; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | |
Arvid Brodin | e9aae56 | 2014-07-04 23:36:40 +0200 | [diff] [blame] | 85 | static void hsr_check_announce(struct net_device *hsr_dev, |
| 86 | unsigned char old_operstate) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 87 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 88 | struct hsr_priv *hsr; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 89 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 90 | hsr = netdev_priv(hsr_dev); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 91 | |
| 92 | if ((hsr_dev->operstate == IF_OPER_UP) && (old_operstate != IF_OPER_UP)) { |
| 93 | /* Went up */ |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 94 | hsr->announce_count = 0; |
| 95 | hsr->announce_timer.expires = jiffies + |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 96 | msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL); |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 97 | add_timer(&hsr->announce_timer); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | if ((hsr_dev->operstate != IF_OPER_UP) && (old_operstate == IF_OPER_UP)) |
| 101 | /* Went down */ |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 102 | del_timer(&hsr->announce_timer); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Arvid Brodin | e9aae56 | 2014-07-04 23:36:40 +0200 | [diff] [blame] | 105 | void hsr_check_carrier_and_operstate(struct hsr_priv *hsr) |
| 106 | { |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 107 | struct hsr_port *master; |
Arvid Brodin | e9aae56 | 2014-07-04 23:36:40 +0200 | [diff] [blame] | 108 | unsigned char old_operstate; |
| 109 | bool has_carrier; |
| 110 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 111 | master = hsr_port_get_hsr(hsr, HSR_PT_MASTER); |
Arvid Brodin | e9aae56 | 2014-07-04 23:36:40 +0200 | [diff] [blame] | 112 | /* netif_stacked_transfer_operstate() cannot be used here since |
| 113 | * it doesn't set IF_OPER_LOWERLAYERDOWN (?) |
| 114 | */ |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 115 | old_operstate = master->dev->operstate; |
| 116 | has_carrier = hsr_check_carrier(master); |
| 117 | hsr_set_operstate(master, has_carrier); |
| 118 | hsr_check_announce(master->dev, old_operstate); |
Arvid Brodin | e9aae56 | 2014-07-04 23:36:40 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 121 | int hsr_get_max_mtu(struct hsr_priv *hsr) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 122 | { |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 123 | unsigned int mtu_max; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 124 | struct hsr_port *port; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 125 | |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 126 | mtu_max = ETH_DATA_LEN; |
| 127 | rcu_read_lock(); |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 128 | hsr_for_each_port(hsr, port) |
| 129 | if (port->type != HSR_PT_MASTER) |
| 130 | mtu_max = min(port->dev->mtu, mtu_max); |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 131 | rcu_read_unlock(); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 132 | |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 133 | if (mtu_max < HSR_HLEN) |
| 134 | return 0; |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 135 | return mtu_max - HSR_HLEN; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 138 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 139 | static int hsr_dev_change_mtu(struct net_device *dev, int new_mtu) |
| 140 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 141 | struct hsr_priv *hsr; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 142 | struct hsr_port *master; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 143 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 144 | hsr = netdev_priv(dev); |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 145 | master = hsr_port_get_hsr(hsr, HSR_PT_MASTER); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 146 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 147 | if (new_mtu > hsr_get_max_mtu(hsr)) { |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 148 | netdev_info(master->dev, "A HSR master's MTU cannot be greater than the smallest MTU of its slaves minus the HSR Tag length (%d octets).\n", |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 149 | HSR_HLEN); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 150 | return -EINVAL; |
| 151 | } |
| 152 | |
| 153 | dev->mtu = new_mtu; |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static int hsr_dev_open(struct net_device *dev) |
| 159 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 160 | struct hsr_priv *hsr; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 161 | struct hsr_port *port; |
| 162 | char designation; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 163 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 164 | hsr = netdev_priv(dev); |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 165 | designation = '\0'; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 166 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 167 | rcu_read_lock(); |
| 168 | hsr_for_each_port(hsr, port) { |
| 169 | if (port->type == HSR_PT_MASTER) |
| 170 | continue; |
| 171 | switch (port->type) { |
| 172 | case HSR_PT_SLAVE_A: |
| 173 | designation = 'A'; |
| 174 | break; |
| 175 | case HSR_PT_SLAVE_B: |
| 176 | designation = 'B'; |
| 177 | break; |
| 178 | default: |
| 179 | designation = '?'; |
| 180 | } |
| 181 | if (!is_slave_up(port->dev)) |
| 182 | netdev_warn(dev, "Slave %c (%s) is not up; please bring it up to get a fully working HSR network\n", |
| 183 | designation, port->dev->name); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 184 | } |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 185 | rcu_read_unlock(); |
| 186 | |
| 187 | if (designation == '\0') |
| 188 | netdev_warn(dev, "No slave devices configured\n"); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 193 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 194 | static int hsr_dev_close(struct net_device *dev) |
| 195 | { |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 196 | /* Nothing to do here. */ |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 201 | static void hsr_fill_tag(struct hsr_ethhdr *hsr_ethhdr, struct hsr_priv *hsr) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 202 | { |
| 203 | unsigned long irqflags; |
| 204 | |
| 205 | /* IEC 62439-1:2010, p 48, says the 4-bit "path" field can take values |
| 206 | * between 0001-1001 ("ring identifier", for regular HSR frames), |
| 207 | * or 1111 ("HSR management", supervision frames). Unfortunately, the |
| 208 | * spec writers forgot to explain what a "ring identifier" is, or |
| 209 | * how it is used. So we just set this to 0001 for regular frames, |
| 210 | * and 1111 for supervision frames. |
| 211 | */ |
| 212 | set_hsr_tag_path(&hsr_ethhdr->hsr_tag, 0x1); |
| 213 | |
| 214 | /* IEC 62439-1:2010, p 12: "The link service data unit in an Ethernet |
| 215 | * frame is the content of the frame located between the Length/Type |
| 216 | * field and the Frame Check Sequence." |
| 217 | * |
| 218 | * IEC 62439-3, p 48, specifies the "original LPDU" to include the |
| 219 | * original "LT" field (what "LT" means is not explained anywhere as |
| 220 | * far as I can see - perhaps "Length/Type"?). So LSDU_size might |
| 221 | * equal original length + 2. |
| 222 | * Also, the fact that this field is not used anywhere (might be used |
| 223 | * by a RedBox connecting HSR and PRP nets?) means I cannot test its |
| 224 | * correctness. Instead of guessing, I set this to 0 here, to make any |
| 225 | * problems immediately apparent. Anyone using this driver with PRP/HSR |
| 226 | * RedBoxes might need to fix this... |
| 227 | */ |
| 228 | set_hsr_tag_LSDU_size(&hsr_ethhdr->hsr_tag, 0); |
| 229 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 230 | spin_lock_irqsave(&hsr->seqnr_lock, irqflags); |
| 231 | hsr_ethhdr->hsr_tag.sequence_nr = htons(hsr->sequence_nr); |
| 232 | hsr->sequence_nr++; |
| 233 | spin_unlock_irqrestore(&hsr->seqnr_lock, irqflags); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 234 | |
| 235 | hsr_ethhdr->hsr_tag.encap_proto = hsr_ethhdr->ethhdr.h_proto; |
| 236 | |
| 237 | hsr_ethhdr->ethhdr.h_proto = htons(ETH_P_PRP); |
| 238 | } |
| 239 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 240 | static int slave_xmit(struct hsr_priv *hsr, struct sk_buff *skb, |
| 241 | enum hsr_port_type type) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 242 | { |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 243 | struct hsr_port *port; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 244 | struct hsr_ethhdr *hsr_ethhdr; |
| 245 | |
| 246 | hsr_ethhdr = (struct hsr_ethhdr *) skb->data; |
| 247 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 248 | rcu_read_lock(); |
| 249 | port = hsr_port_get_hsr(hsr, type); |
| 250 | if (!port) { |
| 251 | rcu_read_unlock(); |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 252 | return NET_XMIT_DROP; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 253 | } |
| 254 | skb->dev = port->dev; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 255 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 256 | hsr_addr_subst_dest(port->hsr, &hsr_ethhdr->ethhdr, port); |
| 257 | rcu_read_unlock(); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 258 | |
| 259 | /* Address substitution (IEC62439-3 pp 26, 50): replace mac |
| 260 | * address of outgoing frame with that of the outgoing slave's. |
| 261 | */ |
Joe Perches | e83abe3 | 2014-02-18 10:37:20 -0800 | [diff] [blame] | 262 | ether_addr_copy(hsr_ethhdr->ethhdr.h_source, skb->dev->dev_addr); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 263 | |
| 264 | return dev_queue_xmit(skb); |
| 265 | } |
| 266 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 267 | static int hsr_dev_xmit(struct sk_buff *skb, struct net_device *dev) |
| 268 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 269 | struct hsr_priv *hsr; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 270 | struct hsr_port *master; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 271 | struct hsr_ethhdr *hsr_ethhdr; |
| 272 | struct sk_buff *skb2; |
| 273 | int res1, res2; |
| 274 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 275 | hsr = netdev_priv(dev); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 276 | hsr_ethhdr = (struct hsr_ethhdr *) skb->data; |
| 277 | |
| 278 | if ((skb->protocol != htons(ETH_P_PRP)) || |
| 279 | (hsr_ethhdr->ethhdr.h_proto != htons(ETH_P_PRP))) { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 280 | hsr_fill_tag(hsr_ethhdr, hsr); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 281 | skb->protocol = htons(ETH_P_PRP); |
| 282 | } |
| 283 | |
| 284 | skb2 = pskb_copy(skb, GFP_ATOMIC); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 285 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 286 | res1 = slave_xmit(hsr, skb, HSR_PT_SLAVE_A); |
| 287 | if (skb2) |
| 288 | res2 = slave_xmit(hsr, skb2, HSR_PT_SLAVE_B); |
| 289 | else |
| 290 | res2 = NET_XMIT_DROP; |
| 291 | |
| 292 | rcu_read_lock(); |
| 293 | master = hsr_port_get_hsr(hsr, HSR_PT_MASTER); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 294 | if (likely(res1 == NET_XMIT_SUCCESS || res1 == NET_XMIT_CN || |
| 295 | res2 == NET_XMIT_SUCCESS || res2 == NET_XMIT_CN)) { |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 296 | master->dev->stats.tx_packets++; |
| 297 | master->dev->stats.tx_bytes += skb->len; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 298 | } else { |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 299 | master->dev->stats.tx_dropped++; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 300 | } |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 301 | rcu_read_unlock(); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 302 | |
| 303 | return NETDEV_TX_OK; |
| 304 | } |
| 305 | |
| 306 | |
| 307 | static int hsr_header_create(struct sk_buff *skb, struct net_device *dev, |
| 308 | unsigned short type, const void *daddr, |
| 309 | const void *saddr, unsigned int len) |
| 310 | { |
| 311 | int res; |
| 312 | |
| 313 | /* Make room for the HSR tag now. We will fill it in later (in |
| 314 | * hsr_dev_xmit) |
| 315 | */ |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 316 | if (skb_headroom(skb) < HSR_HLEN + ETH_HLEN) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 317 | return -ENOBUFS; |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 318 | skb_push(skb, HSR_HLEN); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 319 | |
| 320 | /* To allow VLAN/HSR combos we should probably use |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 321 | * res = dev_hard_header(skb, dev, type, daddr, saddr, len + HSR_HLEN); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 322 | * here instead. It would require other changes too, though - e.g. |
| 323 | * separate headers for each slave etc... |
| 324 | */ |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 325 | res = eth_header(skb, dev, type, daddr, saddr, len + HSR_HLEN); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 326 | if (res <= 0) |
| 327 | return res; |
| 328 | skb_reset_mac_header(skb); |
| 329 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 330 | return res + HSR_HLEN; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | |
| 334 | static const struct header_ops hsr_header_ops = { |
| 335 | .create = hsr_header_create, |
| 336 | .parse = eth_header_parse, |
| 337 | }; |
| 338 | |
| 339 | |
| 340 | /* HSR:2010 supervision frames should be padded so that the whole frame, |
| 341 | * including headers and FCS, is 64 bytes (without VLAN). |
| 342 | */ |
| 343 | static int hsr_pad(int size) |
| 344 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 345 | const int min_size = ETH_ZLEN - HSR_HLEN - ETH_HLEN; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 346 | |
| 347 | if (size >= min_size) |
| 348 | return size; |
| 349 | return min_size; |
| 350 | } |
| 351 | |
| 352 | static void send_hsr_supervision_frame(struct net_device *hsr_dev, u8 type) |
| 353 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 354 | struct hsr_priv *hsr; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 355 | struct hsr_port *master; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 356 | struct sk_buff *skb; |
| 357 | int hlen, tlen; |
| 358 | struct hsr_sup_tag *hsr_stag; |
| 359 | struct hsr_sup_payload *hsr_sp; |
| 360 | unsigned long irqflags; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 361 | int res; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 362 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 363 | hsr = netdev_priv(hsr_dev); |
| 364 | master = hsr_port_get_hsr(hsr, HSR_PT_MASTER); |
| 365 | |
| 366 | hlen = LL_RESERVED_SPACE(master->dev); |
| 367 | tlen = master->dev->needed_tailroom; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 368 | skb = alloc_skb(hsr_pad(sizeof(struct hsr_sup_payload)) + hlen + tlen, |
| 369 | GFP_ATOMIC); |
| 370 | |
| 371 | if (skb == NULL) |
| 372 | return; |
| 373 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 374 | skb_reserve(skb, hlen); |
| 375 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 376 | skb->dev = master->dev; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 377 | skb->protocol = htons(ETH_P_PRP); |
| 378 | skb->priority = TC_PRIO_CONTROL; |
| 379 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 380 | res = dev_hard_header(skb, skb->dev, ETH_P_PRP, |
| 381 | hsr->sup_multicast_addr, |
| 382 | skb->dev->dev_addr, skb->len); |
| 383 | if (res <= 0) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 384 | goto out; |
| 385 | |
| 386 | skb_pull(skb, sizeof(struct ethhdr)); |
| 387 | hsr_stag = (typeof(hsr_stag)) skb->data; |
| 388 | |
| 389 | set_hsr_stag_path(hsr_stag, 0xf); |
| 390 | set_hsr_stag_HSR_Ver(hsr_stag, 0); |
| 391 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 392 | spin_lock_irqsave(&hsr->seqnr_lock, irqflags); |
| 393 | hsr_stag->sequence_nr = htons(hsr->sequence_nr); |
| 394 | hsr->sequence_nr++; |
| 395 | spin_unlock_irqrestore(&hsr->seqnr_lock, irqflags); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 396 | |
| 397 | hsr_stag->HSR_TLV_Type = type; |
| 398 | hsr_stag->HSR_TLV_Length = 12; |
| 399 | |
| 400 | skb_push(skb, sizeof(struct ethhdr)); |
| 401 | |
| 402 | /* Payload: MacAddressA */ |
| 403 | hsr_sp = (typeof(hsr_sp)) skb_put(skb, sizeof(*hsr_sp)); |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 404 | ether_addr_copy(hsr_sp->MacAddressA, master->dev->dev_addr); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 405 | |
| 406 | dev_queue_xmit(skb); |
| 407 | return; |
| 408 | |
| 409 | out: |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 410 | WARN_ON_ONCE("HSR: Could not send supervision frame\n"); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 411 | kfree_skb(skb); |
| 412 | } |
| 413 | |
| 414 | |
| 415 | /* Announce (supervision frame) timer function |
| 416 | */ |
| 417 | static void hsr_announce(unsigned long data) |
| 418 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 419 | struct hsr_priv *hsr; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 420 | struct hsr_port *master; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 421 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 422 | hsr = (struct hsr_priv *) data; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 423 | master = hsr_port_get_hsr(hsr, HSR_PT_MASTER); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 424 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 425 | if (hsr->announce_count < 3) { |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 426 | send_hsr_supervision_frame(master->dev, HSR_TLV_ANNOUNCE); |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 427 | hsr->announce_count++; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 428 | } else { |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 429 | send_hsr_supervision_frame(master->dev, HSR_TLV_LIFE_CHECK); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 430 | } |
| 431 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 432 | if (hsr->announce_count < 3) |
| 433 | hsr->announce_timer.expires = jiffies + |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 434 | msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL); |
| 435 | else |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 436 | hsr->announce_timer.expires = jiffies + |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 437 | msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL); |
| 438 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 439 | if (is_admin_up(master->dev)) |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 440 | add_timer(&hsr->announce_timer); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 444 | /* According to comments in the declaration of struct net_device, this function |
| 445 | * is "Called from unregister, can be used to call free_netdev". Ok then... |
| 446 | */ |
| 447 | static void hsr_dev_destroy(struct net_device *hsr_dev) |
| 448 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 449 | struct hsr_priv *hsr; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 450 | struct hsr_port *port; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 451 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 452 | hsr = netdev_priv(hsr_dev); |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 453 | hsr_for_each_port(hsr, port) |
| 454 | hsr_del_port(port); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 455 | |
Arvid Brodin | abff716 | 2014-07-04 23:35:47 +0200 | [diff] [blame] | 456 | del_timer_sync(&hsr->prune_timer); |
| 457 | del_timer_sync(&hsr->announce_timer); |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 458 | |
| 459 | synchronize_rcu(); |
| 460 | free_netdev(hsr_dev); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | static const struct net_device_ops hsr_device_ops = { |
| 464 | .ndo_change_mtu = hsr_dev_change_mtu, |
| 465 | .ndo_open = hsr_dev_open, |
| 466 | .ndo_stop = hsr_dev_close, |
| 467 | .ndo_start_xmit = hsr_dev_xmit, |
| 468 | }; |
| 469 | |
| 470 | |
| 471 | void hsr_dev_setup(struct net_device *dev) |
| 472 | { |
| 473 | random_ether_addr(dev->dev_addr); |
| 474 | |
| 475 | ether_setup(dev); |
| 476 | dev->header_ops = &hsr_header_ops; |
| 477 | dev->netdev_ops = &hsr_device_ops; |
| 478 | dev->tx_queue_len = 0; |
| 479 | |
| 480 | dev->destructor = hsr_dev_destroy; |
| 481 | } |
| 482 | |
| 483 | |
| 484 | /* Return true if dev is a HSR master; return false otherwise. |
| 485 | */ |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 486 | inline bool is_hsr_master(struct net_device *dev) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 487 | { |
| 488 | return (dev->netdev_ops->ndo_start_xmit == hsr_dev_xmit); |
| 489 | } |
| 490 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 491 | /* Default multicast address for HSR Supervision frames */ |
Joe Perches | e83abe3 | 2014-02-18 10:37:20 -0800 | [diff] [blame] | 492 | static const unsigned char def_multicast_addr[ETH_ALEN] __aligned(2) = { |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 493 | 0x01, 0x15, 0x4e, 0x00, 0x01, 0x00 |
| 494 | }; |
| 495 | |
| 496 | int hsr_dev_finalize(struct net_device *hsr_dev, struct net_device *slave[2], |
| 497 | unsigned char multicast_spec) |
| 498 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 499 | struct hsr_priv *hsr; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 500 | struct hsr_port *port; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 501 | int res; |
| 502 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 503 | hsr = netdev_priv(hsr_dev); |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 504 | INIT_LIST_HEAD(&hsr->ports); |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 505 | INIT_LIST_HEAD(&hsr->node_db); |
| 506 | INIT_LIST_HEAD(&hsr->self_node_db); |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 507 | |
| 508 | ether_addr_copy(hsr_dev->dev_addr, slave[0]->dev_addr); |
| 509 | |
| 510 | /* Make sure we recognize frames from ourselves in hsr_rcv() */ |
| 511 | res = hsr_create_self_node(&hsr->self_node_db, hsr_dev->dev_addr, |
| 512 | slave[1]->dev_addr); |
| 513 | if (res < 0) |
| 514 | return res; |
| 515 | |
| 516 | hsr_dev->features = slave[0]->features & slave[1]->features; |
| 517 | /* Prevent recursive tx locking */ |
| 518 | hsr_dev->features |= NETIF_F_LLTX; |
| 519 | /* VLAN on top of HSR needs testing and probably some work on |
| 520 | * hsr_header_create() etc. |
| 521 | */ |
| 522 | hsr_dev->features |= NETIF_F_VLAN_CHALLENGED; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 523 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 524 | spin_lock_init(&hsr->seqnr_lock); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 525 | /* Overflow soon to find bugs easier: */ |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 526 | hsr->sequence_nr = USHRT_MAX - 1024; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 527 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 528 | init_timer(&hsr->announce_timer); |
| 529 | hsr->announce_timer.function = hsr_announce; |
| 530 | hsr->announce_timer.data = (unsigned long) hsr; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 531 | |
Arvid Brodin | abff716 | 2014-07-04 23:35:47 +0200 | [diff] [blame] | 532 | init_timer(&hsr->prune_timer); |
| 533 | hsr->prune_timer.function = hsr_prune_nodes; |
| 534 | hsr->prune_timer.data = (unsigned long) hsr; |
| 535 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 536 | ether_addr_copy(hsr->sup_multicast_addr, def_multicast_addr); |
| 537 | hsr->sup_multicast_addr[ETH_ALEN - 1] = multicast_spec; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 538 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 539 | /* FIXME: should I modify the value of these? |
| 540 | * |
| 541 | * - hsr_dev->flags - i.e. |
| 542 | * IFF_MASTER/SLAVE? |
| 543 | * - hsr_dev->priv_flags - i.e. |
| 544 | * IFF_EBRIDGE? |
| 545 | * IFF_TX_SKB_SHARING? |
| 546 | * IFF_HSR_MASTER/SLAVE? |
| 547 | */ |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 548 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 549 | /* Make sure the 1st call to netif_carrier_on() gets through */ |
| 550 | netif_carrier_off(hsr_dev); |
| 551 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 552 | res = hsr_add_port(hsr, hsr_dev, HSR_PT_MASTER); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 553 | if (res) |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 554 | return res; |
| 555 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 556 | res = register_netdevice(hsr_dev); |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 557 | if (res) |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 558 | goto fail; |
| 559 | |
| 560 | res = hsr_add_port(hsr, slave[0], HSR_PT_SLAVE_A); |
| 561 | if (res) |
| 562 | goto fail; |
| 563 | res = hsr_add_port(hsr, slave[1], HSR_PT_SLAVE_B); |
| 564 | if (res) |
| 565 | goto fail; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 566 | |
Arvid Brodin | abff716 | 2014-07-04 23:35:47 +0200 | [diff] [blame] | 567 | hsr->prune_timer.expires = jiffies + msecs_to_jiffies(PRUNE_PERIOD); |
| 568 | add_timer(&hsr->prune_timer); |
| 569 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 570 | return 0; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame^] | 571 | |
| 572 | fail: |
| 573 | hsr_for_each_port(hsr, port) |
| 574 | hsr_del_port(port); |
| 575 | |
| 576 | return res; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 577 | } |