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