^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Copyright 2011-2014 Autronica Fire and Security AS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Author(s):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * This file contains device methods for creating, using and destroying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * virtual HSR or PRP devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/pkt_sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "hsr_device.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "hsr_slave.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "hsr_framereg.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "hsr_main.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "hsr_forward.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static bool is_admin_up(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) return dev && (dev->flags & IFF_UP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static bool is_slave_up(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) return dev && is_admin_up(dev) && netif_oper_up(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static void __hsr_set_operstate(struct net_device *dev, int transition)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) write_lock_bh(&dev_base_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (dev->operstate != transition) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) dev->operstate = transition;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) write_unlock_bh(&dev_base_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) netdev_state_change(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) write_unlock_bh(&dev_base_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static void hsr_set_operstate(struct hsr_port *master, bool has_carrier)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (!is_admin_up(master->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) __hsr_set_operstate(master->dev, IF_OPER_DOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (has_carrier)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) __hsr_set_operstate(master->dev, IF_OPER_UP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) __hsr_set_operstate(master->dev, IF_OPER_LOWERLAYERDOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static bool hsr_check_carrier(struct hsr_port *master)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct hsr_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) hsr_for_each_port(master->hsr, port) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (port->type != HSR_PT_MASTER && is_slave_up(port->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) netif_carrier_on(master->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) netif_carrier_off(master->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static void hsr_check_announce(struct net_device *hsr_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) unsigned char old_operstate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct hsr_priv *hsr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) hsr = netdev_priv(hsr_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (hsr_dev->operstate == IF_OPER_UP && old_operstate != IF_OPER_UP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) /* Went up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) hsr->announce_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) mod_timer(&hsr->announce_timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) jiffies + msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (hsr_dev->operstate != IF_OPER_UP && old_operstate == IF_OPER_UP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /* Went down */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) del_timer(&hsr->announce_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) void hsr_check_carrier_and_operstate(struct hsr_priv *hsr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct hsr_port *master;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) unsigned char old_operstate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) bool has_carrier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /* netif_stacked_transfer_operstate() cannot be used here since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * it doesn't set IF_OPER_LOWERLAYERDOWN (?)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) old_operstate = master->dev->operstate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) has_carrier = hsr_check_carrier(master);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) hsr_set_operstate(master, has_carrier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) hsr_check_announce(master->dev, old_operstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) int hsr_get_max_mtu(struct hsr_priv *hsr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) unsigned int mtu_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct hsr_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) mtu_max = ETH_DATA_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) hsr_for_each_port(hsr, port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (port->type != HSR_PT_MASTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) mtu_max = min(port->dev->mtu, mtu_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (mtu_max < HSR_HLEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return mtu_max - HSR_HLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static int hsr_dev_change_mtu(struct net_device *dev, int new_mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct hsr_priv *hsr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) hsr = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (new_mtu > hsr_get_max_mtu(hsr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 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",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) HSR_HLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) dev->mtu = new_mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static int hsr_dev_open(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) struct hsr_priv *hsr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) struct hsr_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) char designation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) hsr = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) designation = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) hsr_for_each_port(hsr, port) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (port->type == HSR_PT_MASTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) switch (port->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) case HSR_PT_SLAVE_A:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) designation = 'A';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) case HSR_PT_SLAVE_B:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) designation = 'B';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) designation = '?';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (!is_slave_up(port->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) netdev_warn(dev, "Slave %c (%s) is not up; please bring it up to get a fully working HSR network\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) designation, port->dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (designation == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) netdev_warn(dev, "No slave devices configured\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static int hsr_dev_close(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /* Nothing to do here. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static netdev_features_t hsr_features_recompute(struct hsr_priv *hsr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) netdev_features_t features)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) netdev_features_t mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) struct hsr_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) mask = features;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /* Mask out all features that, if supported by one device, should be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * enabled for all devices (see NETIF_F_ONE_FOR_ALL).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * Anything that's off in mask will not be enabled - so only things
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * that were in features originally, and also is in NETIF_F_ONE_FOR_ALL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * may become enabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) features &= ~NETIF_F_ONE_FOR_ALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) hsr_for_each_port(hsr, port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) features = netdev_increment_features(features,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) port->dev->features,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return features;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static netdev_features_t hsr_fix_features(struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) netdev_features_t features)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) struct hsr_priv *hsr = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return hsr_features_recompute(hsr, features);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) static netdev_tx_t hsr_dev_xmit(struct sk_buff *skb, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct hsr_priv *hsr = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct hsr_port *master;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (master) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) skb->dev = master->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) skb_reset_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) skb_reset_mac_len(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) hsr_forward_skb(skb, master);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) atomic_long_inc(&dev->tx_dropped);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) dev_kfree_skb_any(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static const struct header_ops hsr_header_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) .create = eth_header,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) .parse = eth_header_parse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static struct sk_buff *hsr_init_skb(struct hsr_port *master, u16 proto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) struct hsr_priv *hsr = master->hsr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) int hlen, tlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) hlen = LL_RESERVED_SPACE(master->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) tlen = master->dev->needed_tailroom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) /* skb size is same for PRP/HSR frames, only difference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * being, for PRP it is a trailer and for HSR it is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) skb = dev_alloc_skb(sizeof(struct hsr_tag) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) sizeof(struct hsr_sup_tag) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) sizeof(struct hsr_sup_payload) + hlen + tlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) skb_reserve(skb, hlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) skb->dev = master->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) skb->protocol = htons(proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) skb->priority = TC_PRIO_CONTROL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (dev_hard_header(skb, skb->dev, proto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) hsr->sup_multicast_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) skb->dev->dev_addr, skb->len) <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) skb_reset_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) skb_reset_mac_len(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) skb_reset_transport_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) return skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) static void send_hsr_supervision_frame(struct hsr_port *master,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) unsigned long *interval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) struct hsr_priv *hsr = master->hsr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) __u8 type = HSR_TLV_LIFE_CHECK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) struct hsr_tag *hsr_tag = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) struct hsr_sup_payload *hsr_sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) struct hsr_sup_tag *hsr_stag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) unsigned long irqflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) u16 proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) *interval = msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (hsr->announce_count < 3 && hsr->prot_version == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) type = HSR_TLV_ANNOUNCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) *interval = msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) hsr->announce_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (!hsr->prot_version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) proto = ETH_P_PRP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) proto = ETH_P_HSR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) skb = hsr_init_skb(master, proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) WARN_ONCE(1, "HSR: Could not send supervision frame\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (hsr->prot_version > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) hsr_tag = skb_put(skb, sizeof(struct hsr_tag));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) hsr_tag->encap_proto = htons(ETH_P_PRP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) set_hsr_tag_LSDU_size(hsr_tag, HSR_V1_SUP_LSDUSIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) hsr_stag = skb_put(skb, sizeof(struct hsr_sup_tag));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) set_hsr_stag_path(hsr_stag, (hsr->prot_version ? 0x0 : 0xf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) set_hsr_stag_HSR_ver(hsr_stag, hsr->prot_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) /* From HSRv1 on we have separate supervision sequence numbers. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) spin_lock_irqsave(&master->hsr->seqnr_lock, irqflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (hsr->prot_version > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) hsr_stag->sequence_nr = htons(hsr->sup_sequence_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) hsr->sup_sequence_nr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) hsr_tag->sequence_nr = htons(hsr->sequence_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) hsr->sequence_nr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) hsr_stag->sequence_nr = htons(hsr->sequence_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) hsr->sequence_nr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) spin_unlock_irqrestore(&master->hsr->seqnr_lock, irqflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) hsr_stag->HSR_TLV_type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) /* TODO: Why 12 in HSRv0? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) hsr_stag->HSR_TLV_length = hsr->prot_version ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) sizeof(struct hsr_sup_payload) : 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) /* Payload: MacAddressA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) ether_addr_copy(hsr_sp->macaddress_A, master->dev->dev_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (skb_put_padto(skb, ETH_ZLEN + HSR_HLEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) hsr_forward_skb(skb, master);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) static void send_prp_supervision_frame(struct hsr_port *master,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) unsigned long *interval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) struct hsr_priv *hsr = master->hsr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) struct hsr_sup_payload *hsr_sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) struct hsr_sup_tag *hsr_stag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) unsigned long irqflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) struct prp_rct *rct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) u8 *tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) skb = hsr_init_skb(master, ETH_P_PRP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) WARN_ONCE(1, "PRP: Could not send supervision frame\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) *interval = msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) hsr_stag = skb_put(skb, sizeof(struct hsr_sup_tag));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) set_hsr_stag_path(hsr_stag, (hsr->prot_version ? 0x0 : 0xf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) set_hsr_stag_HSR_ver(hsr_stag, (hsr->prot_version ? 1 : 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) /* From HSRv1 on we have separate supervision sequence numbers. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) spin_lock_irqsave(&master->hsr->seqnr_lock, irqflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) hsr_stag->sequence_nr = htons(hsr->sup_sequence_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) hsr->sup_sequence_nr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) hsr_stag->HSR_TLV_type = PRP_TLV_LIFE_CHECK_DD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) hsr_stag->HSR_TLV_length = sizeof(struct hsr_sup_payload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) /* Payload: MacAddressA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) ether_addr_copy(hsr_sp->macaddress_A, master->dev->dev_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (skb_put_padto(skb, ETH_ZLEN + HSR_HLEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) spin_unlock_irqrestore(&master->hsr->seqnr_lock, irqflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) tail = skb_tail_pointer(skb) - HSR_HLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) rct = (struct prp_rct *)tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) rct->PRP_suffix = htons(ETH_P_PRP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) set_prp_LSDU_size(rct, HSR_V1_SUP_LSDUSIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) rct->sequence_nr = htons(hsr->sequence_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) hsr->sequence_nr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) spin_unlock_irqrestore(&master->hsr->seqnr_lock, irqflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) hsr_forward_skb(skb, master);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) /* Announce (supervision frame) timer function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) static void hsr_announce(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) struct hsr_priv *hsr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) struct hsr_port *master;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) unsigned long interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) hsr = from_timer(hsr, t, announce_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) hsr->proto_ops->send_sv_frame(master, &interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) if (is_admin_up(master->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) mod_timer(&hsr->announce_timer, jiffies + interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) void hsr_del_ports(struct hsr_priv *hsr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) struct hsr_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) if (port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) hsr_del_port(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) if (port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) hsr_del_port(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) port = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) if (port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) hsr_del_port(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) static const struct net_device_ops hsr_device_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) .ndo_change_mtu = hsr_dev_change_mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) .ndo_open = hsr_dev_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) .ndo_stop = hsr_dev_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) .ndo_start_xmit = hsr_dev_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) .ndo_fix_features = hsr_fix_features,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) static struct device_type hsr_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) .name = "hsr",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) static struct hsr_proto_ops hsr_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) .send_sv_frame = send_hsr_supervision_frame,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) .create_tagged_frame = hsr_create_tagged_frame,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) .get_untagged_frame = hsr_get_untagged_frame,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) .fill_frame_info = hsr_fill_frame_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) .invalid_dan_ingress_frame = hsr_invalid_dan_ingress_frame,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) static struct hsr_proto_ops prp_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) .send_sv_frame = send_prp_supervision_frame,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) .create_tagged_frame = prp_create_tagged_frame,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) .get_untagged_frame = prp_get_untagged_frame,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) .drop_frame = prp_drop_frame,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) .fill_frame_info = prp_fill_frame_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) .handle_san_frame = prp_handle_san_frame,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) .update_san_info = prp_update_san_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) void hsr_dev_setup(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) eth_hw_addr_random(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) ether_setup(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) dev->min_mtu = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) dev->header_ops = &hsr_header_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) dev->netdev_ops = &hsr_device_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) SET_NETDEV_DEVTYPE(dev, &hsr_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) dev->priv_flags |= IFF_NO_QUEUE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) dev->needs_free_netdev = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) NETIF_F_GSO_MASK | NETIF_F_HW_CSUM |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) NETIF_F_HW_VLAN_CTAG_TX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) dev->features = dev->hw_features;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) /* Prevent recursive tx locking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) dev->features |= NETIF_F_LLTX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) /* VLAN on top of HSR needs testing and probably some work on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) * hsr_header_create() etc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) dev->features |= NETIF_F_VLAN_CHALLENGED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) /* Not sure about this. Taken from bridge code. netdev_features.h says
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) * it means "Does not change network namespaces".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) dev->features |= NETIF_F_NETNS_LOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) /* Return true if dev is a HSR master; return false otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) inline bool is_hsr_master(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) return (dev->netdev_ops->ndo_start_xmit == hsr_dev_xmit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) /* Default multicast address for HSR Supervision frames */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) static const unsigned char def_multicast_addr[ETH_ALEN] __aligned(2) = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 0x01, 0x15, 0x4e, 0x00, 0x01, 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) int hsr_dev_finalize(struct net_device *hsr_dev, struct net_device *slave[2],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) unsigned char multicast_spec, u8 protocol_version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) bool unregister = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) struct hsr_priv *hsr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) hsr = netdev_priv(hsr_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) INIT_LIST_HEAD(&hsr->ports);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) INIT_LIST_HEAD(&hsr->node_db);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) INIT_LIST_HEAD(&hsr->self_node_db);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) spin_lock_init(&hsr->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) ether_addr_copy(hsr_dev->dev_addr, slave[0]->dev_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) /* initialize protocol specific functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) if (protocol_version == PRP_V1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) /* For PRP, lan_id has most significant 3 bits holding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) * the net_id of PRP_LAN_ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) hsr->net_id = PRP_LAN_ID << 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) hsr->proto_ops = &prp_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) hsr->proto_ops = &hsr_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) /* Make sure we recognize frames from ourselves in hsr_rcv() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) res = hsr_create_self_node(hsr, hsr_dev->dev_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) slave[1]->dev_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) if (res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) spin_lock_init(&hsr->seqnr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) /* Overflow soon to find bugs easier: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) hsr->sequence_nr = HSR_SEQNR_START;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) hsr->sup_sequence_nr = HSR_SUP_SEQNR_START;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) timer_setup(&hsr->announce_timer, hsr_announce, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) timer_setup(&hsr->prune_timer, hsr_prune_nodes, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) ether_addr_copy(hsr->sup_multicast_addr, def_multicast_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) hsr->sup_multicast_addr[ETH_ALEN - 1] = multicast_spec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) hsr->prot_version = protocol_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) /* FIXME: should I modify the value of these?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) * - hsr_dev->flags - i.e.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) * IFF_MASTER/SLAVE?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) * - hsr_dev->priv_flags - i.e.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) * IFF_EBRIDGE?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) * IFF_TX_SKB_SHARING?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) * IFF_HSR_MASTER/SLAVE?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) /* Make sure the 1st call to netif_carrier_on() gets through */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) netif_carrier_off(hsr_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) res = hsr_add_port(hsr, hsr_dev, HSR_PT_MASTER, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) goto err_add_master;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) res = register_netdevice(hsr_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) goto err_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) unregister = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) res = hsr_add_port(hsr, slave[0], HSR_PT_SLAVE_A, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) goto err_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) res = hsr_add_port(hsr, slave[1], HSR_PT_SLAVE_B, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) goto err_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) hsr_debugfs_init(hsr, hsr_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) mod_timer(&hsr->prune_timer, jiffies + msecs_to_jiffies(PRUNE_PERIOD));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) err_unregister:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) hsr_del_ports(hsr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) err_add_master:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) hsr_del_self_node(hsr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) if (unregister)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) unregister_netdevice(hsr_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) }