blob: c2ae54924a0fb97e03b2f36c12eac96f3a103538 [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 * 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 Brodinf4214362013-10-30 21:10:47 +010018#include <linux/rtnetlink.h>
19#include <linux/pkt_sched.h>
20#include "hsr_device.h"
Arvid Brodin81ba6af2014-07-04 23:35:24 +020021#include "hsr_slave.h"
Arvid Brodinf4214362013-10-30 21:10:47 +010022#include "hsr_framereg.h"
23#include "hsr_main.h"
24
25
26static bool is_admin_up(struct net_device *dev)
27{
28 return dev && (dev->flags & IFF_UP);
29}
30
31static bool is_slave_up(struct net_device *dev)
32{
33 return dev && is_admin_up(dev) && netif_oper_up(dev);
34}
35
36static 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 Brodinc5a75912014-07-04 23:38:05 +020048static void hsr_set_operstate(struct hsr_port *master, bool has_carrier)
Arvid Brodinf4214362013-10-30 21:10:47 +010049{
Arvid Brodinc5a75912014-07-04 23:38:05 +020050 if (!is_admin_up(master->dev)) {
51 __hsr_set_operstate(master->dev, IF_OPER_DOWN);
Arvid Brodinf4214362013-10-30 21:10:47 +010052 return;
53 }
54
Arvid Brodine9aae562014-07-04 23:36:40 +020055 if (has_carrier)
Arvid Brodinc5a75912014-07-04 23:38:05 +020056 __hsr_set_operstate(master->dev, IF_OPER_UP);
Arvid Brodinf4214362013-10-30 21:10:47 +010057 else
Arvid Brodinc5a75912014-07-04 23:38:05 +020058 __hsr_set_operstate(master->dev, IF_OPER_LOWERLAYERDOWN);
Arvid Brodinf4214362013-10-30 21:10:47 +010059}
60
Arvid Brodinc5a75912014-07-04 23:38:05 +020061static bool hsr_check_carrier(struct hsr_port *master)
Arvid Brodinf4214362013-10-30 21:10:47 +010062{
Arvid Brodinc5a75912014-07-04 23:38:05 +020063 struct hsr_port *port;
Arvid Brodine9aae562014-07-04 23:36:40 +020064 bool has_carrier;
65
Arvid Brodinc5a75912014-07-04 23:38:05 +020066 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 Brodine9aae562014-07-04 23:36:40 +020075
76 if (has_carrier)
Arvid Brodinc5a75912014-07-04 23:38:05 +020077 netif_carrier_on(master->dev);
Arvid Brodinf4214362013-10-30 21:10:47 +010078 else
Arvid Brodinc5a75912014-07-04 23:38:05 +020079 netif_carrier_off(master->dev);
Arvid Brodine9aae562014-07-04 23:36:40 +020080
81 return has_carrier;
Arvid Brodinf4214362013-10-30 21:10:47 +010082}
83
84
Arvid Brodine9aae562014-07-04 23:36:40 +020085static void hsr_check_announce(struct net_device *hsr_dev,
86 unsigned char old_operstate)
Arvid Brodinf4214362013-10-30 21:10:47 +010087{
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020088 struct hsr_priv *hsr;
Arvid Brodinf4214362013-10-30 21:10:47 +010089
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020090 hsr = netdev_priv(hsr_dev);
Arvid Brodinf4214362013-10-30 21:10:47 +010091
92 if ((hsr_dev->operstate == IF_OPER_UP) && (old_operstate != IF_OPER_UP)) {
93 /* Went up */
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020094 hsr->announce_count = 0;
95 hsr->announce_timer.expires = jiffies +
Arvid Brodinf4214362013-10-30 21:10:47 +010096 msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL);
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020097 add_timer(&hsr->announce_timer);
Arvid Brodinf4214362013-10-30 21:10:47 +010098 }
99
100 if ((hsr_dev->operstate != IF_OPER_UP) && (old_operstate == IF_OPER_UP))
101 /* Went down */
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200102 del_timer(&hsr->announce_timer);
Arvid Brodinf4214362013-10-30 21:10:47 +0100103}
104
Arvid Brodine9aae562014-07-04 23:36:40 +0200105void hsr_check_carrier_and_operstate(struct hsr_priv *hsr)
106{
Arvid Brodinc5a75912014-07-04 23:38:05 +0200107 struct hsr_port *master;
Arvid Brodine9aae562014-07-04 23:36:40 +0200108 unsigned char old_operstate;
109 bool has_carrier;
110
Arvid Brodinc5a75912014-07-04 23:38:05 +0200111 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
Arvid Brodine9aae562014-07-04 23:36:40 +0200112 /* netif_stacked_transfer_operstate() cannot be used here since
113 * it doesn't set IF_OPER_LOWERLAYERDOWN (?)
114 */
Arvid Brodinc5a75912014-07-04 23:38:05 +0200115 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 Brodine9aae562014-07-04 23:36:40 +0200119}
120
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200121int hsr_get_max_mtu(struct hsr_priv *hsr)
Arvid Brodinf4214362013-10-30 21:10:47 +0100122{
Arvid Brodin51f3c602014-07-04 23:37:27 +0200123 unsigned int mtu_max;
Arvid Brodinc5a75912014-07-04 23:38:05 +0200124 struct hsr_port *port;
Arvid Brodinf4214362013-10-30 21:10:47 +0100125
Arvid Brodin51f3c602014-07-04 23:37:27 +0200126 mtu_max = ETH_DATA_LEN;
127 rcu_read_lock();
Arvid Brodinc5a75912014-07-04 23:38:05 +0200128 hsr_for_each_port(hsr, port)
129 if (port->type != HSR_PT_MASTER)
130 mtu_max = min(port->dev->mtu, mtu_max);
Arvid Brodin51f3c602014-07-04 23:37:27 +0200131 rcu_read_unlock();
Arvid Brodinf4214362013-10-30 21:10:47 +0100132
Arvid Brodin51f3c602014-07-04 23:37:27 +0200133 if (mtu_max < HSR_HLEN)
134 return 0;
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200135 return mtu_max - HSR_HLEN;
Arvid Brodinf4214362013-10-30 21:10:47 +0100136}
137
Arvid Brodin51f3c602014-07-04 23:37:27 +0200138
Arvid Brodinf4214362013-10-30 21:10:47 +0100139static int hsr_dev_change_mtu(struct net_device *dev, int new_mtu)
140{
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200141 struct hsr_priv *hsr;
Arvid Brodinc5a75912014-07-04 23:38:05 +0200142 struct hsr_port *master;
Arvid Brodinf4214362013-10-30 21:10:47 +0100143
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200144 hsr = netdev_priv(dev);
Arvid Brodinc5a75912014-07-04 23:38:05 +0200145 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
Arvid Brodinf4214362013-10-30 21:10:47 +0100146
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200147 if (new_mtu > hsr_get_max_mtu(hsr)) {
Arvid Brodinc5a75912014-07-04 23:38:05 +0200148 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 Brodin70ebe4a2014-07-04 23:34:38 +0200149 HSR_HLEN);
Arvid Brodinf4214362013-10-30 21:10:47 +0100150 return -EINVAL;
151 }
152
153 dev->mtu = new_mtu;
154
155 return 0;
156}
157
158static int hsr_dev_open(struct net_device *dev)
159{
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200160 struct hsr_priv *hsr;
Arvid Brodinc5a75912014-07-04 23:38:05 +0200161 struct hsr_port *port;
162 char designation;
Arvid Brodinf4214362013-10-30 21:10:47 +0100163
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200164 hsr = netdev_priv(dev);
Arvid Brodinc5a75912014-07-04 23:38:05 +0200165 designation = '\0';
Arvid Brodinf4214362013-10-30 21:10:47 +0100166
Arvid Brodinc5a75912014-07-04 23:38:05 +0200167 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 Brodinf4214362013-10-30 21:10:47 +0100184 }
Arvid Brodinc5a75912014-07-04 23:38:05 +0200185 rcu_read_unlock();
186
187 if (designation == '\0')
188 netdev_warn(dev, "No slave devices configured\n");
Arvid Brodinf4214362013-10-30 21:10:47 +0100189
190 return 0;
191}
192
Arvid Brodinc5a75912014-07-04 23:38:05 +0200193
Arvid Brodinf4214362013-10-30 21:10:47 +0100194static int hsr_dev_close(struct net_device *dev)
195{
Arvid Brodinc5a75912014-07-04 23:38:05 +0200196 /* Nothing to do here. */
Arvid Brodinf4214362013-10-30 21:10:47 +0100197 return 0;
198}
199
200
Arvid Brodin1cc1eb52014-07-04 23:38:57 +0200201static netdev_features_t hsr_features_recompute(struct hsr_priv *hsr,
202 netdev_features_t features)
203{
204 netdev_features_t mask;
205 struct hsr_port *port;
206
207 mask = features;
208
209 /* Mask out all features that, if supported by one device, should be
210 * enabled for all devices (see NETIF_F_ONE_FOR_ALL).
211 *
212 * Anything that's off in mask will not be enabled - so only things
213 * that were in features originally, and also is in NETIF_F_ONE_FOR_ALL,
214 * may become enabled.
215 */
216 features &= ~NETIF_F_ONE_FOR_ALL;
217 hsr_for_each_port(hsr, port)
218 features = netdev_increment_features(features,
219 port->dev->features,
220 mask);
221
222 return features;
223}
224
225static netdev_features_t hsr_fix_features(struct net_device *dev,
226 netdev_features_t features)
227{
228 struct hsr_priv *hsr = netdev_priv(dev);
229
230 return hsr_features_recompute(hsr, features);
231}
232
233
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200234static void hsr_fill_tag(struct hsr_ethhdr *hsr_ethhdr, struct hsr_priv *hsr)
Arvid Brodinf4214362013-10-30 21:10:47 +0100235{
236 unsigned long irqflags;
237
238 /* IEC 62439-1:2010, p 48, says the 4-bit "path" field can take values
239 * between 0001-1001 ("ring identifier", for regular HSR frames),
240 * or 1111 ("HSR management", supervision frames). Unfortunately, the
241 * spec writers forgot to explain what a "ring identifier" is, or
242 * how it is used. So we just set this to 0001 for regular frames,
243 * and 1111 for supervision frames.
244 */
245 set_hsr_tag_path(&hsr_ethhdr->hsr_tag, 0x1);
246
247 /* IEC 62439-1:2010, p 12: "The link service data unit in an Ethernet
248 * frame is the content of the frame located between the Length/Type
249 * field and the Frame Check Sequence."
250 *
251 * IEC 62439-3, p 48, specifies the "original LPDU" to include the
252 * original "LT" field (what "LT" means is not explained anywhere as
253 * far as I can see - perhaps "Length/Type"?). So LSDU_size might
254 * equal original length + 2.
255 * Also, the fact that this field is not used anywhere (might be used
256 * by a RedBox connecting HSR and PRP nets?) means I cannot test its
257 * correctness. Instead of guessing, I set this to 0 here, to make any
258 * problems immediately apparent. Anyone using this driver with PRP/HSR
259 * RedBoxes might need to fix this...
260 */
261 set_hsr_tag_LSDU_size(&hsr_ethhdr->hsr_tag, 0);
262
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200263 spin_lock_irqsave(&hsr->seqnr_lock, irqflags);
264 hsr_ethhdr->hsr_tag.sequence_nr = htons(hsr->sequence_nr);
265 hsr->sequence_nr++;
266 spin_unlock_irqrestore(&hsr->seqnr_lock, irqflags);
Arvid Brodinf4214362013-10-30 21:10:47 +0100267
268 hsr_ethhdr->hsr_tag.encap_proto = hsr_ethhdr->ethhdr.h_proto;
269
270 hsr_ethhdr->ethhdr.h_proto = htons(ETH_P_PRP);
271}
272
Arvid Brodinc5a75912014-07-04 23:38:05 +0200273static int slave_xmit(struct hsr_priv *hsr, struct sk_buff *skb,
274 enum hsr_port_type type)
Arvid Brodinf4214362013-10-30 21:10:47 +0100275{
Arvid Brodinc5a75912014-07-04 23:38:05 +0200276 struct hsr_port *port;
Arvid Brodinf4214362013-10-30 21:10:47 +0100277 struct hsr_ethhdr *hsr_ethhdr;
278
279 hsr_ethhdr = (struct hsr_ethhdr *) skb->data;
280
Arvid Brodinc5a75912014-07-04 23:38:05 +0200281 rcu_read_lock();
282 port = hsr_port_get_hsr(hsr, type);
283 if (!port) {
284 rcu_read_unlock();
Arvid Brodin51f3c602014-07-04 23:37:27 +0200285 return NET_XMIT_DROP;
Arvid Brodinc5a75912014-07-04 23:38:05 +0200286 }
287 skb->dev = port->dev;
Arvid Brodinf4214362013-10-30 21:10:47 +0100288
Arvid Brodinc5a75912014-07-04 23:38:05 +0200289 hsr_addr_subst_dest(port->hsr, &hsr_ethhdr->ethhdr, port);
290 rcu_read_unlock();
Arvid Brodinf4214362013-10-30 21:10:47 +0100291
292 /* Address substitution (IEC62439-3 pp 26, 50): replace mac
293 * address of outgoing frame with that of the outgoing slave's.
294 */
Joe Perchese83abe32014-02-18 10:37:20 -0800295 ether_addr_copy(hsr_ethhdr->ethhdr.h_source, skb->dev->dev_addr);
Arvid Brodinf4214362013-10-30 21:10:47 +0100296
297 return dev_queue_xmit(skb);
298}
299
Arvid Brodinf4214362013-10-30 21:10:47 +0100300static int hsr_dev_xmit(struct sk_buff *skb, struct net_device *dev)
301{
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200302 struct hsr_priv *hsr;
Arvid Brodinc5a75912014-07-04 23:38:05 +0200303 struct hsr_port *master;
Arvid Brodinf4214362013-10-30 21:10:47 +0100304 struct hsr_ethhdr *hsr_ethhdr;
305 struct sk_buff *skb2;
306 int res1, res2;
307
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200308 hsr = netdev_priv(dev);
Arvid Brodinf4214362013-10-30 21:10:47 +0100309 hsr_ethhdr = (struct hsr_ethhdr *) skb->data;
310
311 if ((skb->protocol != htons(ETH_P_PRP)) ||
312 (hsr_ethhdr->ethhdr.h_proto != htons(ETH_P_PRP))) {
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200313 hsr_fill_tag(hsr_ethhdr, hsr);
Arvid Brodinf4214362013-10-30 21:10:47 +0100314 skb->protocol = htons(ETH_P_PRP);
315 }
316
317 skb2 = pskb_copy(skb, GFP_ATOMIC);
Arvid Brodinf4214362013-10-30 21:10:47 +0100318
Arvid Brodinc5a75912014-07-04 23:38:05 +0200319 res1 = slave_xmit(hsr, skb, HSR_PT_SLAVE_A);
320 if (skb2)
321 res2 = slave_xmit(hsr, skb2, HSR_PT_SLAVE_B);
322 else
323 res2 = NET_XMIT_DROP;
324
325 rcu_read_lock();
326 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
Arvid Brodinf4214362013-10-30 21:10:47 +0100327 if (likely(res1 == NET_XMIT_SUCCESS || res1 == NET_XMIT_CN ||
328 res2 == NET_XMIT_SUCCESS || res2 == NET_XMIT_CN)) {
Arvid Brodinc5a75912014-07-04 23:38:05 +0200329 master->dev->stats.tx_packets++;
330 master->dev->stats.tx_bytes += skb->len;
Arvid Brodinf4214362013-10-30 21:10:47 +0100331 } else {
Arvid Brodinc5a75912014-07-04 23:38:05 +0200332 master->dev->stats.tx_dropped++;
Arvid Brodinf4214362013-10-30 21:10:47 +0100333 }
Arvid Brodinc5a75912014-07-04 23:38:05 +0200334 rcu_read_unlock();
Arvid Brodinf4214362013-10-30 21:10:47 +0100335
336 return NETDEV_TX_OK;
337}
338
339
340static int hsr_header_create(struct sk_buff *skb, struct net_device *dev,
341 unsigned short type, const void *daddr,
342 const void *saddr, unsigned int len)
343{
344 int res;
345
346 /* Make room for the HSR tag now. We will fill it in later (in
347 * hsr_dev_xmit)
348 */
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200349 if (skb_headroom(skb) < HSR_HLEN + ETH_HLEN)
Arvid Brodinf4214362013-10-30 21:10:47 +0100350 return -ENOBUFS;
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200351 skb_push(skb, HSR_HLEN);
Arvid Brodinf4214362013-10-30 21:10:47 +0100352
353 /* To allow VLAN/HSR combos we should probably use
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200354 * res = dev_hard_header(skb, dev, type, daddr, saddr, len + HSR_HLEN);
Arvid Brodinf4214362013-10-30 21:10:47 +0100355 * here instead. It would require other changes too, though - e.g.
356 * separate headers for each slave etc...
357 */
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200358 res = eth_header(skb, dev, type, daddr, saddr, len + HSR_HLEN);
Arvid Brodinf4214362013-10-30 21:10:47 +0100359 if (res <= 0)
360 return res;
361 skb_reset_mac_header(skb);
362
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200363 return res + HSR_HLEN;
Arvid Brodinf4214362013-10-30 21:10:47 +0100364}
365
366
367static const struct header_ops hsr_header_ops = {
368 .create = hsr_header_create,
369 .parse = eth_header_parse,
370};
371
372
373/* HSR:2010 supervision frames should be padded so that the whole frame,
374 * including headers and FCS, is 64 bytes (without VLAN).
375 */
376static int hsr_pad(int size)
377{
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200378 const int min_size = ETH_ZLEN - HSR_HLEN - ETH_HLEN;
Arvid Brodinf4214362013-10-30 21:10:47 +0100379
380 if (size >= min_size)
381 return size;
382 return min_size;
383}
384
385static void send_hsr_supervision_frame(struct net_device *hsr_dev, u8 type)
386{
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200387 struct hsr_priv *hsr;
Arvid Brodinc5a75912014-07-04 23:38:05 +0200388 struct hsr_port *master;
Arvid Brodinf4214362013-10-30 21:10:47 +0100389 struct sk_buff *skb;
390 int hlen, tlen;
391 struct hsr_sup_tag *hsr_stag;
392 struct hsr_sup_payload *hsr_sp;
393 unsigned long irqflags;
Arvid Brodinc5a75912014-07-04 23:38:05 +0200394 int res;
Arvid Brodinf4214362013-10-30 21:10:47 +0100395
Arvid Brodinc5a75912014-07-04 23:38:05 +0200396 hsr = netdev_priv(hsr_dev);
397 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
398
399 hlen = LL_RESERVED_SPACE(master->dev);
400 tlen = master->dev->needed_tailroom;
Arvid Brodinf4214362013-10-30 21:10:47 +0100401 skb = alloc_skb(hsr_pad(sizeof(struct hsr_sup_payload)) + hlen + tlen,
402 GFP_ATOMIC);
403
404 if (skb == NULL)
405 return;
406
Arvid Brodinf4214362013-10-30 21:10:47 +0100407 skb_reserve(skb, hlen);
408
Arvid Brodinc5a75912014-07-04 23:38:05 +0200409 skb->dev = master->dev;
Arvid Brodinf4214362013-10-30 21:10:47 +0100410 skb->protocol = htons(ETH_P_PRP);
411 skb->priority = TC_PRIO_CONTROL;
412
Arvid Brodinc5a75912014-07-04 23:38:05 +0200413 res = dev_hard_header(skb, skb->dev, ETH_P_PRP,
414 hsr->sup_multicast_addr,
415 skb->dev->dev_addr, skb->len);
416 if (res <= 0)
Arvid Brodinf4214362013-10-30 21:10:47 +0100417 goto out;
418
419 skb_pull(skb, sizeof(struct ethhdr));
420 hsr_stag = (typeof(hsr_stag)) skb->data;
421
422 set_hsr_stag_path(hsr_stag, 0xf);
423 set_hsr_stag_HSR_Ver(hsr_stag, 0);
424
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200425 spin_lock_irqsave(&hsr->seqnr_lock, irqflags);
426 hsr_stag->sequence_nr = htons(hsr->sequence_nr);
427 hsr->sequence_nr++;
428 spin_unlock_irqrestore(&hsr->seqnr_lock, irqflags);
Arvid Brodinf4214362013-10-30 21:10:47 +0100429
430 hsr_stag->HSR_TLV_Type = type;
431 hsr_stag->HSR_TLV_Length = 12;
432
433 skb_push(skb, sizeof(struct ethhdr));
434
435 /* Payload: MacAddressA */
436 hsr_sp = (typeof(hsr_sp)) skb_put(skb, sizeof(*hsr_sp));
Arvid Brodinc5a75912014-07-04 23:38:05 +0200437 ether_addr_copy(hsr_sp->MacAddressA, master->dev->dev_addr);
Arvid Brodinf4214362013-10-30 21:10:47 +0100438
439 dev_queue_xmit(skb);
440 return;
441
442out:
Arvid Brodinc5a75912014-07-04 23:38:05 +0200443 WARN_ON_ONCE("HSR: Could not send supervision frame\n");
Arvid Brodinf4214362013-10-30 21:10:47 +0100444 kfree_skb(skb);
445}
446
447
448/* Announce (supervision frame) timer function
449 */
450static void hsr_announce(unsigned long data)
451{
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200452 struct hsr_priv *hsr;
Arvid Brodinc5a75912014-07-04 23:38:05 +0200453 struct hsr_port *master;
Arvid Brodinf4214362013-10-30 21:10:47 +0100454
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200455 hsr = (struct hsr_priv *) data;
Arvid Brodinc5a75912014-07-04 23:38:05 +0200456 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
Arvid Brodinf4214362013-10-30 21:10:47 +0100457
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200458 if (hsr->announce_count < 3) {
Arvid Brodinc5a75912014-07-04 23:38:05 +0200459 send_hsr_supervision_frame(master->dev, HSR_TLV_ANNOUNCE);
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200460 hsr->announce_count++;
Arvid Brodinf4214362013-10-30 21:10:47 +0100461 } else {
Arvid Brodinc5a75912014-07-04 23:38:05 +0200462 send_hsr_supervision_frame(master->dev, HSR_TLV_LIFE_CHECK);
Arvid Brodinf4214362013-10-30 21:10:47 +0100463 }
464
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200465 if (hsr->announce_count < 3)
466 hsr->announce_timer.expires = jiffies +
Arvid Brodinf4214362013-10-30 21:10:47 +0100467 msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL);
468 else
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200469 hsr->announce_timer.expires = jiffies +
Arvid Brodinf4214362013-10-30 21:10:47 +0100470 msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
471
Arvid Brodinc5a75912014-07-04 23:38:05 +0200472 if (is_admin_up(master->dev))
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200473 add_timer(&hsr->announce_timer);
Arvid Brodinf4214362013-10-30 21:10:47 +0100474}
475
476
Arvid Brodinf4214362013-10-30 21:10:47 +0100477/* According to comments in the declaration of struct net_device, this function
478 * is "Called from unregister, can be used to call free_netdev". Ok then...
479 */
480static void hsr_dev_destroy(struct net_device *hsr_dev)
481{
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200482 struct hsr_priv *hsr;
Arvid Brodinc5a75912014-07-04 23:38:05 +0200483 struct hsr_port *port;
Arvid Brodinf4214362013-10-30 21:10:47 +0100484
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200485 hsr = netdev_priv(hsr_dev);
Arvid Brodinc5a75912014-07-04 23:38:05 +0200486 hsr_for_each_port(hsr, port)
487 hsr_del_port(port);
Arvid Brodinf4214362013-10-30 21:10:47 +0100488
Arvid Brodinabff7162014-07-04 23:35:47 +0200489 del_timer_sync(&hsr->prune_timer);
490 del_timer_sync(&hsr->announce_timer);
Arvid Brodinc5a75912014-07-04 23:38:05 +0200491
492 synchronize_rcu();
493 free_netdev(hsr_dev);
Arvid Brodinf4214362013-10-30 21:10:47 +0100494}
495
496static const struct net_device_ops hsr_device_ops = {
497 .ndo_change_mtu = hsr_dev_change_mtu,
498 .ndo_open = hsr_dev_open,
499 .ndo_stop = hsr_dev_close,
500 .ndo_start_xmit = hsr_dev_xmit,
Arvid Brodin1cc1eb52014-07-04 23:38:57 +0200501 .ndo_fix_features = hsr_fix_features,
Arvid Brodinf4214362013-10-30 21:10:47 +0100502};
503
Arvid Brodin4c3477d2014-07-04 23:39:42 +0200504static struct device_type hsr_type = {
505 .name = "hsr",
506};
Arvid Brodinf4214362013-10-30 21:10:47 +0100507
508void hsr_dev_setup(struct net_device *dev)
509{
510 random_ether_addr(dev->dev_addr);
511
512 ether_setup(dev);
Arvid Brodin4c3477d2014-07-04 23:39:42 +0200513 dev->header_ops = &hsr_header_ops;
514 dev->netdev_ops = &hsr_device_ops;
515 SET_NETDEV_DEVTYPE(dev, &hsr_type);
516 dev->tx_queue_len = 0;
Arvid Brodinf4214362013-10-30 21:10:47 +0100517
518 dev->destructor = hsr_dev_destroy;
Arvid Brodin1cc1eb52014-07-04 23:38:57 +0200519
520 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
521 NETIF_F_GSO_MASK | NETIF_F_HW_CSUM |
522 NETIF_F_HW_VLAN_CTAG_TX;
523
524 dev->features = dev->hw_features;
525
526 /* Prevent recursive tx locking */
527 dev->features |= NETIF_F_LLTX;
528 /* VLAN on top of HSR needs testing and probably some work on
529 * hsr_header_create() etc.
530 */
531 dev->features |= NETIF_F_VLAN_CHALLENGED;
Arvid Brodin4c3477d2014-07-04 23:39:42 +0200532 /* Not sure about this. Taken from bridge code. netdev_features.h says
533 * it means "Does not change network namespaces".
534 */
535 dev->features |= NETIF_F_NETNS_LOCAL;
Arvid Brodinf4214362013-10-30 21:10:47 +0100536}
537
538
539/* Return true if dev is a HSR master; return false otherwise.
540 */
Arvid Brodinc5a75912014-07-04 23:38:05 +0200541inline bool is_hsr_master(struct net_device *dev)
Arvid Brodinf4214362013-10-30 21:10:47 +0100542{
543 return (dev->netdev_ops->ndo_start_xmit == hsr_dev_xmit);
544}
545
Arvid Brodinf4214362013-10-30 21:10:47 +0100546/* Default multicast address for HSR Supervision frames */
Joe Perchese83abe32014-02-18 10:37:20 -0800547static const unsigned char def_multicast_addr[ETH_ALEN] __aligned(2) = {
Arvid Brodinf4214362013-10-30 21:10:47 +0100548 0x01, 0x15, 0x4e, 0x00, 0x01, 0x00
549};
550
551int hsr_dev_finalize(struct net_device *hsr_dev, struct net_device *slave[2],
552 unsigned char multicast_spec)
553{
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200554 struct hsr_priv *hsr;
Arvid Brodinc5a75912014-07-04 23:38:05 +0200555 struct hsr_port *port;
Arvid Brodinf4214362013-10-30 21:10:47 +0100556 int res;
557
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200558 hsr = netdev_priv(hsr_dev);
Arvid Brodinc5a75912014-07-04 23:38:05 +0200559 INIT_LIST_HEAD(&hsr->ports);
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200560 INIT_LIST_HEAD(&hsr->node_db);
561 INIT_LIST_HEAD(&hsr->self_node_db);
Arvid Brodin51f3c602014-07-04 23:37:27 +0200562
563 ether_addr_copy(hsr_dev->dev_addr, slave[0]->dev_addr);
564
565 /* Make sure we recognize frames from ourselves in hsr_rcv() */
566 res = hsr_create_self_node(&hsr->self_node_db, hsr_dev->dev_addr,
567 slave[1]->dev_addr);
568 if (res < 0)
569 return res;
570
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200571 spin_lock_init(&hsr->seqnr_lock);
Arvid Brodinf4214362013-10-30 21:10:47 +0100572 /* Overflow soon to find bugs easier: */
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200573 hsr->sequence_nr = USHRT_MAX - 1024;
Arvid Brodinf4214362013-10-30 21:10:47 +0100574
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200575 init_timer(&hsr->announce_timer);
576 hsr->announce_timer.function = hsr_announce;
577 hsr->announce_timer.data = (unsigned long) hsr;
Arvid Brodinf4214362013-10-30 21:10:47 +0100578
Arvid Brodinabff7162014-07-04 23:35:47 +0200579 init_timer(&hsr->prune_timer);
580 hsr->prune_timer.function = hsr_prune_nodes;
581 hsr->prune_timer.data = (unsigned long) hsr;
582
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200583 ether_addr_copy(hsr->sup_multicast_addr, def_multicast_addr);
584 hsr->sup_multicast_addr[ETH_ALEN - 1] = multicast_spec;
Arvid Brodinf4214362013-10-30 21:10:47 +0100585
Arvid Brodinc5a75912014-07-04 23:38:05 +0200586 /* FIXME: should I modify the value of these?
587 *
588 * - hsr_dev->flags - i.e.
589 * IFF_MASTER/SLAVE?
590 * - hsr_dev->priv_flags - i.e.
591 * IFF_EBRIDGE?
592 * IFF_TX_SKB_SHARING?
593 * IFF_HSR_MASTER/SLAVE?
594 */
Arvid Brodinf4214362013-10-30 21:10:47 +0100595
Arvid Brodinf4214362013-10-30 21:10:47 +0100596 /* Make sure the 1st call to netif_carrier_on() gets through */
597 netif_carrier_off(hsr_dev);
598
Arvid Brodinc5a75912014-07-04 23:38:05 +0200599 res = hsr_add_port(hsr, hsr_dev, HSR_PT_MASTER);
Arvid Brodinf4214362013-10-30 21:10:47 +0100600 if (res)
Arvid Brodin51f3c602014-07-04 23:37:27 +0200601 return res;
602
Arvid Brodinc5a75912014-07-04 23:38:05 +0200603 res = register_netdevice(hsr_dev);
Arvid Brodin51f3c602014-07-04 23:37:27 +0200604 if (res)
Arvid Brodinc5a75912014-07-04 23:38:05 +0200605 goto fail;
606
607 res = hsr_add_port(hsr, slave[0], HSR_PT_SLAVE_A);
608 if (res)
609 goto fail;
610 res = hsr_add_port(hsr, slave[1], HSR_PT_SLAVE_B);
611 if (res)
612 goto fail;
Arvid Brodinf4214362013-10-30 21:10:47 +0100613
Arvid Brodinabff7162014-07-04 23:35:47 +0200614 hsr->prune_timer.expires = jiffies + msecs_to_jiffies(PRUNE_PERIOD);
615 add_timer(&hsr->prune_timer);
616
Arvid Brodinf4214362013-10-30 21:10:47 +0100617 return 0;
Arvid Brodinc5a75912014-07-04 23:38:05 +0200618
619fail:
620 hsr_for_each_port(hsr, port)
621 hsr_del_port(port);
622
623 return res;
Arvid Brodinf4214362013-10-30 21:10:47 +0100624}