Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^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)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Event handling for HSR and 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 <net/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/rculist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include "hsr_main.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include "hsr_device.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "hsr_netlink.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "hsr_framereg.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include "hsr_slave.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static bool hsr_slave_empty(struct hsr_priv *hsr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	struct hsr_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	hsr_for_each_port(hsr, port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 		if (port->type != HSR_PT_MASTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	return true;
^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 int hsr_netdev_notify(struct notifier_block *nb, unsigned long event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 			     void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct hsr_port *port, *master;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct hsr_priv *hsr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	LIST_HEAD(list_kill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	int mtu_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	dev = netdev_notifier_info_to_dev(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	port = hsr_port_get_rtnl(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	if (!port) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		if (!is_hsr_master(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 			return NOTIFY_DONE;	/* Not an HSR device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		hsr = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		port = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		if (!port) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 			/* Resend of notification concerning removed device? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 			return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		hsr = port->hsr;
^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) 	switch (event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	case NETDEV_UP:		/* Administrative state DOWN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	case NETDEV_DOWN:	/* Administrative state UP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	case NETDEV_CHANGE:	/* Link (carrier) state changes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		hsr_check_carrier_and_operstate(hsr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	case NETDEV_CHANGENAME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		if (is_hsr_master(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 			hsr_debugfs_rename(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	case NETDEV_CHANGEADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		if (port->type == HSR_PT_MASTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			/* This should not happen since there's no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			 * ndo_set_mac_address() for HSR devices - i.e. not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 			 * supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		if (port->type == HSR_PT_SLAVE_A) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			ether_addr_copy(master->dev->dev_addr, dev->dev_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			call_netdevice_notifiers(NETDEV_CHANGEADDR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 						 master->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		/* Make sure we recognize frames from ourselves in hsr_rcv() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		res = hsr_create_self_node(hsr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 					   master->dev->dev_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 					   port ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 						port->dev->dev_addr :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 						master->dev->dev_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			netdev_warn(master->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 				    "Could not update HSR node address.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	case NETDEV_CHANGEMTU:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		if (port->type == HSR_PT_MASTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			break; /* Handled in ndo_change_mtu() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		mtu_max = hsr_get_max_mtu(port->hsr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		master = hsr_port_get_hsr(port->hsr, HSR_PT_MASTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		master->dev->mtu = mtu_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	case NETDEV_UNREGISTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		if (!is_hsr_master(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			master = hsr_port_get_hsr(port->hsr, HSR_PT_MASTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			hsr_del_port(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			if (hsr_slave_empty(master->hsr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 				const struct rtnl_link_ops *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 				ops = master->dev->rtnl_link_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 				ops->dellink(master->dev, &list_kill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 				unregister_netdevice_many(&list_kill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	case NETDEV_PRE_TYPE_CHANGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		/* HSR works only on Ethernet devices. Refuse slave to change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		 * its type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		return NOTIFY_BAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	return NOTIFY_DONE;
^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) struct hsr_port *hsr_port_get_hsr(struct hsr_priv *hsr, enum hsr_port_type pt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	struct hsr_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	hsr_for_each_port(hsr, port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		if (port->type == pt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			return port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static struct notifier_block hsr_nb = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	.notifier_call = hsr_netdev_notify,	/* Slave event notifications */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static int __init hsr_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_HLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	register_netdevice_notifier(&hsr_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	res = hsr_netlink_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static void __exit hsr_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	hsr_netlink_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	hsr_debugfs_remove_root();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	unregister_netdevice_notifier(&hsr_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) module_init(hsr_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) module_exit(hsr_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) MODULE_LICENSE("GPL");