10e7623bdSMurali Karicheri // SPDX-License-Identifier: GPL-2.0
270ebe4a4SArvid Brodin /* Copyright 2011-2014 Autronica Fire and Security AS
3f421436aSArvid Brodin *
4f421436aSArvid Brodin * Author(s):
570ebe4a4SArvid Brodin * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
6f421436aSArvid Brodin * This file contains device methods for creating, using and destroying
78f4c0e01SMurali Karicheri * virtual HSR or PRP devices.
8f421436aSArvid Brodin */
9f421436aSArvid Brodin
10f421436aSArvid Brodin #include <linux/netdevice.h>
11f421436aSArvid Brodin #include <linux/skbuff.h>
12f421436aSArvid Brodin #include <linux/etherdevice.h>
13f421436aSArvid Brodin #include <linux/rtnetlink.h>
14f421436aSArvid Brodin #include <linux/pkt_sched.h>
15f421436aSArvid Brodin #include "hsr_device.h"
1681ba6afdSArvid Brodin #include "hsr_slave.h"
17f421436aSArvid Brodin #include "hsr_framereg.h"
18f421436aSArvid Brodin #include "hsr_main.h"
19f266a683SArvid Brodin #include "hsr_forward.h"
20f421436aSArvid Brodin
is_admin_up(struct net_device * dev)21f421436aSArvid Brodin static bool is_admin_up(struct net_device *dev)
22f421436aSArvid Brodin {
23f421436aSArvid Brodin return dev && (dev->flags & IFF_UP);
24f421436aSArvid Brodin }
25f421436aSArvid Brodin
is_slave_up(struct net_device * dev)26f421436aSArvid Brodin static bool is_slave_up(struct net_device *dev)
27f421436aSArvid Brodin {
28f421436aSArvid Brodin return dev && is_admin_up(dev) && netif_oper_up(dev);
29f421436aSArvid Brodin }
30f421436aSArvid Brodin
__hsr_set_operstate(struct net_device * dev,int transition)31f421436aSArvid Brodin static void __hsr_set_operstate(struct net_device *dev, int transition)
32f421436aSArvid Brodin {
33fd888e85SSebastian Andrzej Siewior write_lock(&dev_base_lock);
341c2b0f08SEric Dumazet if (READ_ONCE(dev->operstate) != transition) {
351c2b0f08SEric Dumazet WRITE_ONCE(dev->operstate, transition);
36fd888e85SSebastian Andrzej Siewior write_unlock(&dev_base_lock);
37f421436aSArvid Brodin netdev_state_change(dev);
38f421436aSArvid Brodin } else {
39fd888e85SSebastian Andrzej Siewior write_unlock(&dev_base_lock);
40f421436aSArvid Brodin }
41f421436aSArvid Brodin }
42f421436aSArvid Brodin
hsr_set_operstate(struct hsr_port * master,bool has_carrier)43c5a75911SArvid Brodin static void hsr_set_operstate(struct hsr_port *master, bool has_carrier)
44f421436aSArvid Brodin {
45c5a75911SArvid Brodin if (!is_admin_up(master->dev)) {
46c5a75911SArvid Brodin __hsr_set_operstate(master->dev, IF_OPER_DOWN);
47f421436aSArvid Brodin return;
48f421436aSArvid Brodin }
49f421436aSArvid Brodin
50e9aae56eSArvid Brodin if (has_carrier)
51c5a75911SArvid Brodin __hsr_set_operstate(master->dev, IF_OPER_UP);
52f421436aSArvid Brodin else
53c5a75911SArvid Brodin __hsr_set_operstate(master->dev, IF_OPER_LOWERLAYERDOWN);
54f421436aSArvid Brodin }
55f421436aSArvid Brodin
hsr_check_carrier(struct hsr_port * master)56c5a75911SArvid Brodin static bool hsr_check_carrier(struct hsr_port *master)
57f421436aSArvid Brodin {
58c5a75911SArvid Brodin struct hsr_port *port;
59e9aae56eSArvid Brodin
6081390d0cSTaehee Yoo ASSERT_RTNL();
61c5a75911SArvid Brodin
6281390d0cSTaehee Yoo hsr_for_each_port(master->hsr, port) {
635670342cSMurali Karicheri if (port->type != HSR_PT_MASTER && is_slave_up(port->dev)) {
64c5a75911SArvid Brodin netif_carrier_on(master->dev);
6581390d0cSTaehee Yoo return true;
6681390d0cSTaehee Yoo }
6781390d0cSTaehee Yoo }
6881390d0cSTaehee Yoo
69c5a75911SArvid Brodin netif_carrier_off(master->dev);
70e9aae56eSArvid Brodin
7181390d0cSTaehee Yoo return false;
72f421436aSArvid Brodin }
73f421436aSArvid Brodin
hsr_check_announce(struct net_device * hsr_dev)74d183f297SLukasz Majewski static void hsr_check_announce(struct net_device *hsr_dev)
75f421436aSArvid Brodin {
7670ebe4a4SArvid Brodin struct hsr_priv *hsr;
77f421436aSArvid Brodin
7870ebe4a4SArvid Brodin hsr = netdev_priv(hsr_dev);
79d183f297SLukasz Majewski if (netif_running(hsr_dev) && netif_oper_up(hsr_dev)) {
80d183f297SLukasz Majewski /* Enable announce timer and start sending supervisory frames */
81d183f297SLukasz Majewski if (!timer_pending(&hsr->announce_timer)) {
8270ebe4a4SArvid Brodin hsr->announce_count = 0;
83d183f297SLukasz Majewski mod_timer(&hsr->announce_timer, jiffies +
84d183f297SLukasz Majewski msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL));
85f421436aSArvid Brodin }
86d183f297SLukasz Majewski } else {
87d183f297SLukasz Majewski /* Deactivate the announce timer */
88d183f297SLukasz Majewski timer_delete(&hsr->announce_timer);
89d183f297SLukasz Majewski }
90f421436aSArvid Brodin }
91f421436aSArvid Brodin
hsr_check_carrier_and_operstate(struct hsr_priv * hsr)92e9aae56eSArvid Brodin void hsr_check_carrier_and_operstate(struct hsr_priv *hsr)
93e9aae56eSArvid Brodin {
94c5a75911SArvid Brodin struct hsr_port *master;
95e9aae56eSArvid Brodin bool has_carrier;
96e9aae56eSArvid Brodin
97c5a75911SArvid Brodin master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
98e9aae56eSArvid Brodin /* netif_stacked_transfer_operstate() cannot be used here since
99e9aae56eSArvid Brodin * it doesn't set IF_OPER_LOWERLAYERDOWN (?)
100e9aae56eSArvid Brodin */
101c5a75911SArvid Brodin has_carrier = hsr_check_carrier(master);
102c5a75911SArvid Brodin hsr_set_operstate(master, has_carrier);
103d183f297SLukasz Majewski hsr_check_announce(master->dev);
104e9aae56eSArvid Brodin }
105e9aae56eSArvid Brodin
hsr_get_max_mtu(struct hsr_priv * hsr)10670ebe4a4SArvid Brodin int hsr_get_max_mtu(struct hsr_priv *hsr)
107f421436aSArvid Brodin {
10851f3c605SArvid Brodin unsigned int mtu_max;
109c5a75911SArvid Brodin struct hsr_port *port;
110f421436aSArvid Brodin
11151f3c605SArvid Brodin mtu_max = ETH_DATA_LEN;
112c5a75911SArvid Brodin hsr_for_each_port(hsr, port)
113c5a75911SArvid Brodin if (port->type != HSR_PT_MASTER)
114c5a75911SArvid Brodin mtu_max = min(port->dev->mtu, mtu_max);
115f421436aSArvid Brodin
11651f3c605SArvid Brodin if (mtu_max < HSR_HLEN)
11751f3c605SArvid Brodin return 0;
11870ebe4a4SArvid Brodin return mtu_max - HSR_HLEN;
119f421436aSArvid Brodin }
120f421436aSArvid Brodin
hsr_dev_change_mtu(struct net_device * dev,int new_mtu)121f421436aSArvid Brodin static int hsr_dev_change_mtu(struct net_device *dev, int new_mtu)
122f421436aSArvid Brodin {
12370ebe4a4SArvid Brodin struct hsr_priv *hsr;
124f421436aSArvid Brodin
12570ebe4a4SArvid Brodin hsr = netdev_priv(dev);
126f421436aSArvid Brodin
12770ebe4a4SArvid Brodin if (new_mtu > hsr_get_max_mtu(hsr)) {
128f30e4720STaehee Yoo netdev_info(dev, "A HSR master's MTU cannot be greater than the smallest MTU of its slaves minus the HSR Tag length (%d octets).\n",
12970ebe4a4SArvid Brodin HSR_HLEN);
130f421436aSArvid Brodin return -EINVAL;
131f421436aSArvid Brodin }
132f421436aSArvid Brodin
133f421436aSArvid Brodin dev->mtu = new_mtu;
134f421436aSArvid Brodin
135f421436aSArvid Brodin return 0;
136f421436aSArvid Brodin }
137f421436aSArvid Brodin
hsr_dev_open(struct net_device * dev)138f421436aSArvid Brodin static int hsr_dev_open(struct net_device *dev)
139f421436aSArvid Brodin {
14070ebe4a4SArvid Brodin struct hsr_priv *hsr;
141c5a75911SArvid Brodin struct hsr_port *port;
142c5a75911SArvid Brodin char designation;
143f421436aSArvid Brodin
14470ebe4a4SArvid Brodin hsr = netdev_priv(dev);
145c5a75911SArvid Brodin designation = '\0';
146f421436aSArvid Brodin
147c5a75911SArvid Brodin hsr_for_each_port(hsr, port) {
148c5a75911SArvid Brodin if (port->type == HSR_PT_MASTER)
149c5a75911SArvid Brodin continue;
150c5a75911SArvid Brodin switch (port->type) {
151c5a75911SArvid Brodin case HSR_PT_SLAVE_A:
152c5a75911SArvid Brodin designation = 'A';
153c5a75911SArvid Brodin break;
154c5a75911SArvid Brodin case HSR_PT_SLAVE_B:
155c5a75911SArvid Brodin designation = 'B';
156c5a75911SArvid Brodin break;
157c5a75911SArvid Brodin default:
158c5a75911SArvid Brodin designation = '?';
159f421436aSArvid Brodin }
160c5a75911SArvid Brodin if (!is_slave_up(port->dev))
161c5a75911SArvid Brodin netdev_warn(dev, "Slave %c (%s) is not up; please bring it up to get a fully working HSR network\n",
162c5a75911SArvid Brodin designation, port->dev->name);
163c5a75911SArvid Brodin }
164c5a75911SArvid Brodin
165c5a75911SArvid Brodin if (designation == '\0')
166c5a75911SArvid Brodin netdev_warn(dev, "No slave devices configured\n");
167f421436aSArvid Brodin
168f421436aSArvid Brodin return 0;
169f421436aSArvid Brodin }
170f421436aSArvid Brodin
hsr_dev_close(struct net_device * dev)171f421436aSArvid Brodin static int hsr_dev_close(struct net_device *dev)
172f421436aSArvid Brodin {
173c5a75911SArvid Brodin /* Nothing to do here. */
174f421436aSArvid Brodin return 0;
175f421436aSArvid Brodin }
176f421436aSArvid Brodin
hsr_features_recompute(struct hsr_priv * hsr,netdev_features_t features)1771cc1eb52SArvid Brodin static netdev_features_t hsr_features_recompute(struct hsr_priv *hsr,
1781cc1eb52SArvid Brodin netdev_features_t features)
1791cc1eb52SArvid Brodin {
1801cc1eb52SArvid Brodin netdev_features_t mask;
1811cc1eb52SArvid Brodin struct hsr_port *port;
1821cc1eb52SArvid Brodin
1831cc1eb52SArvid Brodin mask = features;
1841cc1eb52SArvid Brodin
1851cc1eb52SArvid Brodin /* Mask out all features that, if supported by one device, should be
1861cc1eb52SArvid Brodin * enabled for all devices (see NETIF_F_ONE_FOR_ALL).
1871cc1eb52SArvid Brodin *
1881cc1eb52SArvid Brodin * Anything that's off in mask will not be enabled - so only things
1891cc1eb52SArvid Brodin * that were in features originally, and also is in NETIF_F_ONE_FOR_ALL,
1901cc1eb52SArvid Brodin * may become enabled.
1911cc1eb52SArvid Brodin */
1921cc1eb52SArvid Brodin features &= ~NETIF_F_ONE_FOR_ALL;
1931cc1eb52SArvid Brodin hsr_for_each_port(hsr, port)
1941cc1eb52SArvid Brodin features = netdev_increment_features(features,
1951cc1eb52SArvid Brodin port->dev->features,
1961cc1eb52SArvid Brodin mask);
1971cc1eb52SArvid Brodin
1981cc1eb52SArvid Brodin return features;
1991cc1eb52SArvid Brodin }
2001cc1eb52SArvid Brodin
hsr_fix_features(struct net_device * dev,netdev_features_t features)2011cc1eb52SArvid Brodin static netdev_features_t hsr_fix_features(struct net_device *dev,
2021cc1eb52SArvid Brodin netdev_features_t features)
2031cc1eb52SArvid Brodin {
2041cc1eb52SArvid Brodin struct hsr_priv *hsr = netdev_priv(dev);
2051cc1eb52SArvid Brodin
2061cc1eb52SArvid Brodin return hsr_features_recompute(hsr, features);
2071cc1eb52SArvid Brodin }
2081cc1eb52SArvid Brodin
hsr_dev_xmit(struct sk_buff * skb,struct net_device * dev)20942deace2SLuc Van Oostenryck static netdev_tx_t hsr_dev_xmit(struct sk_buff *skb, struct net_device *dev)
210f421436aSArvid Brodin {
211f266a683SArvid Brodin struct hsr_priv *hsr = netdev_priv(dev);
212c5a75911SArvid Brodin struct hsr_port *master;
213f421436aSArvid Brodin
214c5a75911SArvid Brodin master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
215df95467bSTaehee Yoo if (master) {
216f266a683SArvid Brodin skb->dev = master->dev;
2179d680392SKurt Kanzenbach skb_reset_mac_header(skb);
21848b491a5SGeorge McCollister skb_reset_mac_len(skb);
21906afd2c3SSebastian Andrzej Siewior spin_lock_bh(&hsr->seqnr_lock);
220f266a683SArvid Brodin hsr_forward_skb(skb, master);
22106afd2c3SSebastian Andrzej Siewior spin_unlock_bh(&hsr->seqnr_lock);
222df95467bSTaehee Yoo } else {
223625788b5SEric Dumazet dev_core_stats_tx_dropped_inc(dev);
224df95467bSTaehee Yoo dev_kfree_skb_any(skb);
225df95467bSTaehee Yoo }
226f421436aSArvid Brodin return NETDEV_TX_OK;
227f421436aSArvid Brodin }
228f421436aSArvid Brodin
229f421436aSArvid Brodin static const struct header_ops hsr_header_ops = {
230f266a683SArvid Brodin .create = eth_header,
231f421436aSArvid Brodin .parse = eth_header_parse,
232f421436aSArvid Brodin };
233f421436aSArvid Brodin
hsr_init_skb(struct hsr_port * master)23478be9217SGeorge McCollister static struct sk_buff *hsr_init_skb(struct hsr_port *master)
235f421436aSArvid Brodin {
236121c33b0SMurali Karicheri struct hsr_priv *hsr = master->hsr;
237f421436aSArvid Brodin struct sk_buff *skb;
238f421436aSArvid Brodin int hlen, tlen;
239c5a75911SArvid Brodin
240c5a75911SArvid Brodin hlen = LL_RESERVED_SPACE(master->dev);
241c5a75911SArvid Brodin tlen = master->dev->needed_tailroom;
242c643ff03SMurali Karicheri /* skb size is same for PRP/HSR frames, only difference
243c643ff03SMurali Karicheri * being, for PRP it is a trailer and for HSR it is a
244c643ff03SMurali Karicheri * header
245c643ff03SMurali Karicheri */
24678be9217SGeorge McCollister skb = dev_alloc_skb(sizeof(struct hsr_sup_tag) +
247ee1c2797SPeter Heise sizeof(struct hsr_sup_payload) + hlen + tlen);
248f421436aSArvid Brodin
24905ca6e64SMurali Karicheri if (!skb)
250121c33b0SMurali Karicheri return skb;
251f421436aSArvid Brodin
252f421436aSArvid Brodin skb_reserve(skb, hlen);
253c5a75911SArvid Brodin skb->dev = master->dev;
254f421436aSArvid Brodin skb->priority = TC_PRIO_CONTROL;
255f421436aSArvid Brodin
256*fd7259c1SEric Dumazet skb_reset_network_header(skb);
257*fd7259c1SEric Dumazet skb_reset_transport_header(skb);
25878be9217SGeorge McCollister if (dev_hard_header(skb, skb->dev, ETH_P_PRP,
259121c33b0SMurali Karicheri hsr->sup_multicast_addr,
260f266a683SArvid Brodin skb->dev->dev_addr, skb->len) <= 0)
261f421436aSArvid Brodin goto out;
262121c33b0SMurali Karicheri
263f266a683SArvid Brodin skb_reset_mac_header(skb);
26448b491a5SGeorge McCollister skb_reset_mac_len(skb);
265f421436aSArvid Brodin
266121c33b0SMurali Karicheri return skb;
267121c33b0SMurali Karicheri out:
268121c33b0SMurali Karicheri kfree_skb(skb);
269121c33b0SMurali Karicheri
270121c33b0SMurali Karicheri return NULL;
271121c33b0SMurali Karicheri }
272121c33b0SMurali Karicheri
send_hsr_supervision_frame(struct hsr_port * master,unsigned long * interval)273121c33b0SMurali Karicheri static void send_hsr_supervision_frame(struct hsr_port *master,
27428e458e0SMurali Karicheri unsigned long *interval)
275121c33b0SMurali Karicheri {
27628e458e0SMurali Karicheri struct hsr_priv *hsr = master->hsr;
27728e458e0SMurali Karicheri __u8 type = HSR_TLV_LIFE_CHECK;
278121c33b0SMurali Karicheri struct hsr_sup_payload *hsr_sp;
279121c33b0SMurali Karicheri struct hsr_sup_tag *hsr_stag;
280121c33b0SMurali Karicheri struct sk_buff *skb;
281121c33b0SMurali Karicheri
28228e458e0SMurali Karicheri *interval = msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
28328e458e0SMurali Karicheri if (hsr->announce_count < 3 && hsr->prot_version == 0) {
28428e458e0SMurali Karicheri type = HSR_TLV_ANNOUNCE;
28528e458e0SMurali Karicheri *interval = msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL);
28628e458e0SMurali Karicheri hsr->announce_count++;
28728e458e0SMurali Karicheri }
28828e458e0SMurali Karicheri
28978be9217SGeorge McCollister skb = hsr_init_skb(master);
290121c33b0SMurali Karicheri if (!skb) {
291923dea2aSNikita Zhandarovich netdev_warn_once(master->dev, "HSR: Could not send supervision frame\n");
292121c33b0SMurali Karicheri return;
293121c33b0SMurali Karicheri }
294121c33b0SMurali Karicheri
295b952f4dfSyuan linyu hsr_stag = skb_put(skb, sizeof(struct hsr_sup_tag));
29628e458e0SMurali Karicheri set_hsr_stag_path(hsr_stag, (hsr->prot_version ? 0x0 : 0xf));
29728e458e0SMurali Karicheri set_hsr_stag_HSR_ver(hsr_stag, hsr->prot_version);
298f421436aSArvid Brodin
299ee1c2797SPeter Heise /* From HSRv1 on we have separate supervision sequence numbers. */
300d5c7652eSSebastian Andrzej Siewior spin_lock_bh(&hsr->seqnr_lock);
30128e458e0SMurali Karicheri if (hsr->prot_version > 0) {
30228e458e0SMurali Karicheri hsr_stag->sequence_nr = htons(hsr->sup_sequence_nr);
30328e458e0SMurali Karicheri hsr->sup_sequence_nr++;
304ee1c2797SPeter Heise } else {
30528e458e0SMurali Karicheri hsr_stag->sequence_nr = htons(hsr->sequence_nr);
30628e458e0SMurali Karicheri hsr->sequence_nr++;
307ee1c2797SPeter Heise }
308f421436aSArvid Brodin
309eafaa88bSAndreas Oetken hsr_stag->tlv.HSR_TLV_type = type;
310ee1c2797SPeter Heise /* TODO: Why 12 in HSRv0? */
311eafaa88bSAndreas Oetken hsr_stag->tlv.HSR_TLV_length = hsr->prot_version ?
31228e458e0SMurali Karicheri sizeof(struct hsr_sup_payload) : 12;
313f421436aSArvid Brodin
314f421436aSArvid Brodin /* Payload: MacAddressA */
315b952f4dfSyuan linyu hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload));
316b1b4aa91SMurali Karicheri ether_addr_copy(hsr_sp->macaddress_A, master->dev->dev_addr);
317f421436aSArvid Brodin
31806afd2c3SSebastian Andrzej Siewior if (skb_put_padto(skb, ETH_ZLEN)) {
31906afd2c3SSebastian Andrzej Siewior spin_unlock_bh(&hsr->seqnr_lock);
320414e7d76SFlorian Fainelli return;
32106afd2c3SSebastian Andrzej Siewior }
322ee1c2797SPeter Heise
323f266a683SArvid Brodin hsr_forward_skb(skb, master);
32406afd2c3SSebastian Andrzej Siewior spin_unlock_bh(&hsr->seqnr_lock);
325121c33b0SMurali Karicheri return;
326f421436aSArvid Brodin }
327f421436aSArvid Brodin
send_prp_supervision_frame(struct hsr_port * master,unsigned long * interval)328c643ff03SMurali Karicheri static void send_prp_supervision_frame(struct hsr_port *master,
329c643ff03SMurali Karicheri unsigned long *interval)
330c643ff03SMurali Karicheri {
331c643ff03SMurali Karicheri struct hsr_priv *hsr = master->hsr;
332c643ff03SMurali Karicheri struct hsr_sup_payload *hsr_sp;
333c643ff03SMurali Karicheri struct hsr_sup_tag *hsr_stag;
334c643ff03SMurali Karicheri struct sk_buff *skb;
335c643ff03SMurali Karicheri
33678be9217SGeorge McCollister skb = hsr_init_skb(master);
337c643ff03SMurali Karicheri if (!skb) {
338923dea2aSNikita Zhandarovich netdev_warn_once(master->dev, "PRP: Could not send supervision frame\n");
339c643ff03SMurali Karicheri return;
340c643ff03SMurali Karicheri }
341c643ff03SMurali Karicheri
342c643ff03SMurali Karicheri *interval = msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
343c643ff03SMurali Karicheri hsr_stag = skb_put(skb, sizeof(struct hsr_sup_tag));
344c643ff03SMurali Karicheri set_hsr_stag_path(hsr_stag, (hsr->prot_version ? 0x0 : 0xf));
345c643ff03SMurali Karicheri set_hsr_stag_HSR_ver(hsr_stag, (hsr->prot_version ? 1 : 0));
346c643ff03SMurali Karicheri
347c643ff03SMurali Karicheri /* From HSRv1 on we have separate supervision sequence numbers. */
348d5c7652eSSebastian Andrzej Siewior spin_lock_bh(&hsr->seqnr_lock);
349c643ff03SMurali Karicheri hsr_stag->sequence_nr = htons(hsr->sup_sequence_nr);
350c643ff03SMurali Karicheri hsr->sup_sequence_nr++;
351eafaa88bSAndreas Oetken hsr_stag->tlv.HSR_TLV_type = PRP_TLV_LIFE_CHECK_DD;
352eafaa88bSAndreas Oetken hsr_stag->tlv.HSR_TLV_length = sizeof(struct hsr_sup_payload);
353c643ff03SMurali Karicheri
354c643ff03SMurali Karicheri /* Payload: MacAddressA */
355c643ff03SMurali Karicheri hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload));
356c643ff03SMurali Karicheri ether_addr_copy(hsr_sp->macaddress_A, master->dev->dev_addr);
357c643ff03SMurali Karicheri
35878be9217SGeorge McCollister if (skb_put_padto(skb, ETH_ZLEN)) {
359d5c7652eSSebastian Andrzej Siewior spin_unlock_bh(&hsr->seqnr_lock);
360c643ff03SMurali Karicheri return;
361c643ff03SMurali Karicheri }
362c643ff03SMurali Karicheri
363c643ff03SMurali Karicheri hsr_forward_skb(skb, master);
36406afd2c3SSebastian Andrzej Siewior spin_unlock_bh(&hsr->seqnr_lock);
365c643ff03SMurali Karicheri }
366c643ff03SMurali Karicheri
367f421436aSArvid Brodin /* Announce (supervision frame) timer function
368f421436aSArvid Brodin */
hsr_announce(struct timer_list * t)369dda436b7SKees Cook static void hsr_announce(struct timer_list *t)
370f421436aSArvid Brodin {
37170ebe4a4SArvid Brodin struct hsr_priv *hsr;
372c5a75911SArvid Brodin struct hsr_port *master;
3731e027960SEric Dumazet unsigned long interval;
374f421436aSArvid Brodin
375dda436b7SKees Cook hsr = from_timer(hsr, t, announce_timer);
376f266a683SArvid Brodin
377f266a683SArvid Brodin rcu_read_lock();
378c5a75911SArvid Brodin master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
37928e458e0SMurali Karicheri hsr->proto_ops->send_sv_frame(master, &interval);
380f421436aSArvid Brodin
381c5a75911SArvid Brodin if (is_admin_up(master->dev))
3821e027960SEric Dumazet mod_timer(&hsr->announce_timer, jiffies + interval);
383f266a683SArvid Brodin
384f266a683SArvid Brodin rcu_read_unlock();
385f421436aSArvid Brodin }
386f421436aSArvid Brodin
hsr_del_ports(struct hsr_priv * hsr)387de0083c7STaehee Yoo void hsr_del_ports(struct hsr_priv *hsr)
388e0a4b997STaehee Yoo {
389e0a4b997STaehee Yoo struct hsr_port *port;
390e0a4b997STaehee Yoo
391e0a4b997STaehee Yoo port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
392e0a4b997STaehee Yoo if (port)
393e0a4b997STaehee Yoo hsr_del_port(port);
394e0a4b997STaehee Yoo
395e0a4b997STaehee Yoo port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
396e0a4b997STaehee Yoo if (port)
397e0a4b997STaehee Yoo hsr_del_port(port);
398e0a4b997STaehee Yoo
399e0a4b997STaehee Yoo port = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
400e0a4b997STaehee Yoo if (port)
401e0a4b997STaehee Yoo hsr_del_port(port);
402e0a4b997STaehee Yoo }
403e0a4b997STaehee Yoo
404f421436aSArvid Brodin static const struct net_device_ops hsr_device_ops = {
405f421436aSArvid Brodin .ndo_change_mtu = hsr_dev_change_mtu,
406f421436aSArvid Brodin .ndo_open = hsr_dev_open,
407f421436aSArvid Brodin .ndo_stop = hsr_dev_close,
408f421436aSArvid Brodin .ndo_start_xmit = hsr_dev_xmit,
4091cc1eb52SArvid Brodin .ndo_fix_features = hsr_fix_features,
410f421436aSArvid Brodin };
411f421436aSArvid Brodin
4124c3477dcSArvid Brodin static struct device_type hsr_type = {
4134c3477dcSArvid Brodin .name = "hsr",
4144c3477dcSArvid Brodin };
415f421436aSArvid Brodin
41628e458e0SMurali Karicheri static struct hsr_proto_ops hsr_ops = {
41728e458e0SMurali Karicheri .send_sv_frame = send_hsr_supervision_frame,
418fa4dc895SMurali Karicheri .create_tagged_frame = hsr_create_tagged_frame,
419fa4dc895SMurali Karicheri .get_untagged_frame = hsr_get_untagged_frame,
420dcf0cd1cSGeorge McCollister .drop_frame = hsr_drop_frame,
421fa4dc895SMurali Karicheri .fill_frame_info = hsr_fill_frame_info,
422451d8123SMurali Karicheri .invalid_dan_ingress_frame = hsr_invalid_dan_ingress_frame,
42328e458e0SMurali Karicheri };
42428e458e0SMurali Karicheri
425fa4dc895SMurali Karicheri static struct hsr_proto_ops prp_ops = {
426c643ff03SMurali Karicheri .send_sv_frame = send_prp_supervision_frame,
427451d8123SMurali Karicheri .create_tagged_frame = prp_create_tagged_frame,
428451d8123SMurali Karicheri .get_untagged_frame = prp_get_untagged_frame,
429451d8123SMurali Karicheri .drop_frame = prp_drop_frame,
430451d8123SMurali Karicheri .fill_frame_info = prp_fill_frame_info,
431451d8123SMurali Karicheri .handle_san_frame = prp_handle_san_frame,
432451d8123SMurali Karicheri .update_san_info = prp_update_san_info,
433c643ff03SMurali Karicheri };
434c643ff03SMurali Karicheri
hsr_dev_setup(struct net_device * dev)435f421436aSArvid Brodin void hsr_dev_setup(struct net_device *dev)
436f421436aSArvid Brodin {
437d4af4c0aSTobias Klauser eth_hw_addr_random(dev);
438f421436aSArvid Brodin
439f421436aSArvid Brodin ether_setup(dev);
440b3e3893eSJarod Wilson dev->min_mtu = 0;
441f421436aSArvid Brodin dev->header_ops = &hsr_header_ops;
442f421436aSArvid Brodin dev->netdev_ops = &hsr_device_ops;
4434c3477dcSArvid Brodin SET_NETDEV_DEVTYPE(dev, &hsr_type);
444d5c7652eSSebastian Andrzej Siewior dev->priv_flags |= IFF_NO_QUEUE | IFF_DISABLE_NETPOLL;
445f421436aSArvid Brodin
446cf124db5SDavid S. Miller dev->needs_free_netdev = true;
4471cc1eb52SArvid Brodin
4481cc1eb52SArvid Brodin dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
4491cc1eb52SArvid Brodin NETIF_F_GSO_MASK | NETIF_F_HW_CSUM |
4501cc1eb52SArvid Brodin NETIF_F_HW_VLAN_CTAG_TX;
4511cc1eb52SArvid Brodin
4521cc1eb52SArvid Brodin dev->features = dev->hw_features;
4531cc1eb52SArvid Brodin
4541cc1eb52SArvid Brodin /* Prevent recursive tx locking */
4551cc1eb52SArvid Brodin dev->features |= NETIF_F_LLTX;
4561cc1eb52SArvid Brodin /* VLAN on top of HSR needs testing and probably some work on
4571cc1eb52SArvid Brodin * hsr_header_create() etc.
4581cc1eb52SArvid Brodin */
4591cc1eb52SArvid Brodin dev->features |= NETIF_F_VLAN_CHALLENGED;
4604c3477dcSArvid Brodin /* Not sure about this. Taken from bridge code. netdev_features.h says
4614c3477dcSArvid Brodin * it means "Does not change network namespaces".
4624c3477dcSArvid Brodin */
4634c3477dcSArvid Brodin dev->features |= NETIF_F_NETNS_LOCAL;
464f421436aSArvid Brodin }
465f421436aSArvid Brodin
466f421436aSArvid Brodin /* Return true if dev is a HSR master; return false otherwise.
467f421436aSArvid Brodin */
is_hsr_master(struct net_device * dev)468dcf0cd1cSGeorge McCollister bool is_hsr_master(struct net_device *dev)
469f421436aSArvid Brodin {
470f421436aSArvid Brodin return (dev->netdev_ops->ndo_start_xmit == hsr_dev_xmit);
471f421436aSArvid Brodin }
472dcf0cd1cSGeorge McCollister EXPORT_SYMBOL(is_hsr_master);
473f421436aSArvid Brodin
474f421436aSArvid Brodin /* Default multicast address for HSR Supervision frames */
475e83abe37SJoe Perches static const unsigned char def_multicast_addr[ETH_ALEN] __aligned(2) = {
476f421436aSArvid Brodin 0x01, 0x15, 0x4e, 0x00, 0x01, 0x00
477f421436aSArvid Brodin };
478f421436aSArvid Brodin
hsr_dev_finalize(struct net_device * hsr_dev,struct net_device * slave[2],unsigned char multicast_spec,u8 protocol_version,struct netlink_ext_ack * extack)479f421436aSArvid Brodin int hsr_dev_finalize(struct net_device *hsr_dev, struct net_device *slave[2],
48013eeb5feSTaehee Yoo unsigned char multicast_spec, u8 protocol_version,
48113eeb5feSTaehee Yoo struct netlink_ext_ack *extack)
482f421436aSArvid Brodin {
483ccfc9df1STaehee Yoo bool unregister = false;
48470ebe4a4SArvid Brodin struct hsr_priv *hsr;
485e012764cSSebastian Andrzej Siewior int res;
486f421436aSArvid Brodin
48770ebe4a4SArvid Brodin hsr = netdev_priv(hsr_dev);
488c5a75911SArvid Brodin INIT_LIST_HEAD(&hsr->ports);
489e012764cSSebastian Andrzej Siewior INIT_LIST_HEAD(&hsr->node_db);
49092a35678STaehee Yoo spin_lock_init(&hsr->list_lock);
49151f3c605SArvid Brodin
492e35b8d7dSJakub Kicinski eth_hw_addr_set(hsr_dev, slave[0]->dev_addr);
49351f3c605SArvid Brodin
494c643ff03SMurali Karicheri /* initialize protocol specific functions */
495451d8123SMurali Karicheri if (protocol_version == PRP_V1) {
496451d8123SMurali Karicheri /* For PRP, lan_id has most significant 3 bits holding
497451d8123SMurali Karicheri * the net_id of PRP_LAN_ID
498451d8123SMurali Karicheri */
499451d8123SMurali Karicheri hsr->net_id = PRP_LAN_ID << 1;
500c643ff03SMurali Karicheri hsr->proto_ops = &prp_ops;
501451d8123SMurali Karicheri } else {
50228e458e0SMurali Karicheri hsr->proto_ops = &hsr_ops;
503451d8123SMurali Karicheri }
504c643ff03SMurali Karicheri
50551f3c605SArvid Brodin /* Make sure we recognize frames from ourselves in hsr_rcv() */
50692a35678STaehee Yoo res = hsr_create_self_node(hsr, hsr_dev->dev_addr,
50751f3c605SArvid Brodin slave[1]->dev_addr);
50851f3c605SArvid Brodin if (res < 0)
50951f3c605SArvid Brodin return res;
51051f3c605SArvid Brodin
51170ebe4a4SArvid Brodin spin_lock_init(&hsr->seqnr_lock);
512f421436aSArvid Brodin /* Overflow soon to find bugs easier: */
513f266a683SArvid Brodin hsr->sequence_nr = HSR_SEQNR_START;
514ee1c2797SPeter Heise hsr->sup_sequence_nr = HSR_SUP_SEQNR_START;
515f421436aSArvid Brodin
516dda436b7SKees Cook timer_setup(&hsr->announce_timer, hsr_announce, 0);
517dda436b7SKees Cook timer_setup(&hsr->prune_timer, hsr_prune_nodes, 0);
518abff7162SArvid Brodin
51970ebe4a4SArvid Brodin ether_addr_copy(hsr->sup_multicast_addr, def_multicast_addr);
52070ebe4a4SArvid Brodin hsr->sup_multicast_addr[ETH_ALEN - 1] = multicast_spec;
521f421436aSArvid Brodin
522b1b4aa91SMurali Karicheri hsr->prot_version = protocol_version;
523ee1c2797SPeter Heise
524f421436aSArvid Brodin /* Make sure the 1st call to netif_carrier_on() gets through */
525f421436aSArvid Brodin netif_carrier_off(hsr_dev);
526f421436aSArvid Brodin
52713eeb5feSTaehee Yoo res = hsr_add_port(hsr, hsr_dev, HSR_PT_MASTER, extack);
528f421436aSArvid Brodin if (res)
5291d19e2d5STaehee Yoo goto err_add_master;
53051f3c605SArvid Brodin
531e748d0fdSRavi Gunasekaran /* HSR forwarding offload supported in lower device? */
532e748d0fdSRavi Gunasekaran if ((slave[0]->features & NETIF_F_HW_HSR_FWD) &&
533e748d0fdSRavi Gunasekaran (slave[1]->features & NETIF_F_HW_HSR_FWD))
534e748d0fdSRavi Gunasekaran hsr->fwd_offloaded = true;
535e748d0fdSRavi Gunasekaran
536c5a75911SArvid Brodin res = register_netdevice(hsr_dev);
53751f3c605SArvid Brodin if (res)
5381d19e2d5STaehee Yoo goto err_unregister;
539c5a75911SArvid Brodin
540ccfc9df1STaehee Yoo unregister = true;
541ccfc9df1STaehee Yoo
54213eeb5feSTaehee Yoo res = hsr_add_port(hsr, slave[0], HSR_PT_SLAVE_A, extack);
543c5a75911SArvid Brodin if (res)
544ccfc9df1STaehee Yoo goto err_unregister;
5451d19e2d5STaehee Yoo
54613eeb5feSTaehee Yoo res = hsr_add_port(hsr, slave[1], HSR_PT_SLAVE_B, extack);
547c5a75911SArvid Brodin if (res)
548ccfc9df1STaehee Yoo goto err_unregister;
549f421436aSArvid Brodin
5501d19e2d5STaehee Yoo hsr_debugfs_init(hsr, hsr_dev);
55115db6e0dSMuhammad Falak R Wani mod_timer(&hsr->prune_timer, jiffies + msecs_to_jiffies(PRUNE_PERIOD));
552abff7162SArvid Brodin
553f421436aSArvid Brodin return 0;
554c5a75911SArvid Brodin
5551d19e2d5STaehee Yoo err_unregister:
556e0a4b997STaehee Yoo hsr_del_ports(hsr);
5571d19e2d5STaehee Yoo err_add_master:
55892a35678STaehee Yoo hsr_del_self_node(hsr);
559c5a75911SArvid Brodin
560ccfc9df1STaehee Yoo if (unregister)
561ccfc9df1STaehee Yoo unregister_netdevice(hsr_dev);
562c5a75911SArvid Brodin return res;
563f421436aSArvid Brodin }
564