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) /* sunvnet.c: Sun LDOM Virtual Network Driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2016-2017 Oracle. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/ethtool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/highmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/if_vlan.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/icmpv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <net/icmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <net/route.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <asm/vio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <asm/ldc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include "sunvnet_common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) /* length of time before we decide the hardware is borked,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * and dev->tx_timeout() should be called to fix the problem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define VNET_TX_TIMEOUT			(5 * HZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define DRV_MODULE_NAME		"sunvnet"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define DRV_MODULE_VERSION	"2.0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define DRV_MODULE_RELDATE	"February 3, 2017"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static char version[] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	DRV_MODULE_NAME " " DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) MODULE_DESCRIPTION("Sun LDOM virtual network driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) MODULE_VERSION(DRV_MODULE_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) /* Ordered from largest major to lowest */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static struct vio_version vnet_versions[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	{ .major = 1, .minor = 8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	{ .major = 1, .minor = 7 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	{ .major = 1, .minor = 6 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	{ .major = 1, .minor = 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static void vnet_get_drvinfo(struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 			     struct ethtool_drvinfo *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) static u32 vnet_get_msglevel(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	struct vnet *vp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	return vp->msg_enable;
^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 vnet_set_msglevel(struct net_device *dev, u32 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct vnet *vp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	vp->msg_enable = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) static const struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	const char string[ETH_GSTRING_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) } ethtool_stats_keys[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	{ "rx_packets" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	{ "tx_packets" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	{ "rx_bytes" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	{ "tx_bytes" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	{ "rx_errors" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	{ "tx_errors" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	{ "rx_dropped" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	{ "tx_dropped" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	{ "multicast" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	{ "rx_length_errors" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	{ "rx_frame_errors" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	{ "rx_missed_errors" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	{ "tx_carrier_errors" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	{ "nports" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static int vnet_get_sset_count(struct net_device *dev, int sset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	struct vnet *vp = (struct vnet *)netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	switch (sset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	case ETH_SS_STATS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		return ARRAY_SIZE(ethtool_stats_keys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			+ (NUM_VNET_PORT_STATS * vp->nports);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	}
^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) static void vnet_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	struct vnet *vp = (struct vnet *)netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	struct vnet_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	char *p = (char *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	switch (stringset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	case ETH_SS_STATS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		p += sizeof(ethtool_stats_keys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		list_for_each_entry_rcu(port, &vp->port_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			snprintf(p, ETH_GSTRING_LEN, "p%u.%s-%pM",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 				 port->q_index, port->switch_port ? "s" : "q",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 				 port->raddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			p += ETH_GSTRING_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			snprintf(p, ETH_GSTRING_LEN, "p%u.rx_packets",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 				 port->q_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			p += ETH_GSTRING_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			snprintf(p, ETH_GSTRING_LEN, "p%u.tx_packets",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 				 port->q_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			p += ETH_GSTRING_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			snprintf(p, ETH_GSTRING_LEN, "p%u.rx_bytes",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 				 port->q_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			p += ETH_GSTRING_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			snprintf(p, ETH_GSTRING_LEN, "p%u.tx_bytes",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 				 port->q_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			p += ETH_GSTRING_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			snprintf(p, ETH_GSTRING_LEN, "p%u.event_up",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 				 port->q_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			p += ETH_GSTRING_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			snprintf(p, ETH_GSTRING_LEN, "p%u.event_reset",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 				 port->q_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			p += ETH_GSTRING_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	}
^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) static void vnet_get_ethtool_stats(struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 				   struct ethtool_stats *estats, u64 *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	struct vnet *vp = (struct vnet *)netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	struct vnet_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	data[i++] = dev->stats.rx_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	data[i++] = dev->stats.tx_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	data[i++] = dev->stats.rx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	data[i++] = dev->stats.tx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	data[i++] = dev->stats.rx_errors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	data[i++] = dev->stats.tx_errors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	data[i++] = dev->stats.rx_dropped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	data[i++] = dev->stats.tx_dropped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	data[i++] = dev->stats.multicast;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	data[i++] = dev->stats.rx_length_errors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	data[i++] = dev->stats.rx_frame_errors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	data[i++] = dev->stats.rx_missed_errors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	data[i++] = dev->stats.tx_carrier_errors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	data[i++] = vp->nports;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	list_for_each_entry_rcu(port, &vp->port_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		data[i++] = port->q_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		data[i++] = port->stats.rx_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		data[i++] = port->stats.tx_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		data[i++] = port->stats.rx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		data[i++] = port->stats.tx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		data[i++] = port->stats.event_up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		data[i++] = port->stats.event_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static const struct ethtool_ops vnet_ethtool_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	.get_drvinfo		= vnet_get_drvinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	.get_msglevel		= vnet_get_msglevel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	.set_msglevel		= vnet_set_msglevel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	.get_link		= ethtool_op_get_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	.get_sset_count		= vnet_get_sset_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	.get_strings		= vnet_get_strings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	.get_ethtool_stats	= vnet_get_ethtool_stats,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static LIST_HEAD(vnet_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static DEFINE_MUTEX(vnet_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static struct vnet_port *__tx_port_find(struct vnet *vp, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	unsigned int hash = vnet_hashfn(skb->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	struct hlist_head *hp = &vp->port_hash[hash];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	struct vnet_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	hlist_for_each_entry_rcu(port, hp, hash) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		if (!sunvnet_port_is_up_common(port))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		if (ether_addr_equal(port->raddr, skb->data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			return port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	list_for_each_entry_rcu(port, &vp->port_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		if (!port->switch_port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		if (!sunvnet_port_is_up_common(port))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		return port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) /* func arg to vnet_start_xmit_common() to get the proper tx port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static struct vnet_port *vnet_tx_port_find(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 					   struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	struct vnet *vp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	return __tx_port_find(vp, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) static u16 vnet_select_queue(struct net_device *dev, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			     struct net_device *sb_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	struct vnet *vp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	struct vnet_port *port = __tx_port_find(vp, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	if (!port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	return port->q_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) /* Wrappers to common functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static netdev_tx_t vnet_start_xmit(struct sk_buff *skb, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	return sunvnet_start_xmit_common(skb, dev, vnet_tx_port_find);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static void vnet_set_rx_mode(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	struct vnet *vp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	return sunvnet_set_rx_mode_common(dev, vp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) #ifdef CONFIG_NET_POLL_CONTROLLER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) static void vnet_poll_controller(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	struct vnet *vp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	return sunvnet_poll_controller_common(dev, vp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) static const struct net_device_ops vnet_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	.ndo_open		= sunvnet_open_common,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	.ndo_stop		= sunvnet_close_common,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	.ndo_set_rx_mode	= vnet_set_rx_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	.ndo_set_mac_address	= sunvnet_set_mac_addr_common,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	.ndo_validate_addr	= eth_validate_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	.ndo_tx_timeout		= sunvnet_tx_timeout_common,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	.ndo_start_xmit		= vnet_start_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	.ndo_select_queue	= vnet_select_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) #ifdef CONFIG_NET_POLL_CONTROLLER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	.ndo_poll_controller	= vnet_poll_controller,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) static struct vnet *vnet_new(const u64 *local_mac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			     struct vio_dev *vdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	struct vnet *vp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	int err, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	dev = alloc_etherdev_mqs(sizeof(*vp), VNET_MAX_TXQS, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	dev->needed_headroom = VNET_PACKET_SKIP + 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	dev->needed_tailroom = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	for (i = 0; i < ETH_ALEN; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		dev->dev_addr[i] = (*local_mac >> (5 - i) * 8) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	vp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	spin_lock_init(&vp->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	vp->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	INIT_LIST_HEAD(&vp->port_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	for (i = 0; i < VNET_PORT_HASH_SIZE; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		INIT_HLIST_HEAD(&vp->port_hash[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	INIT_LIST_HEAD(&vp->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	vp->local_mac = *local_mac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	dev->netdev_ops = &vnet_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	dev->ethtool_ops = &vnet_ethtool_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	dev->watchdog_timeo = VNET_TX_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	dev->hw_features = NETIF_F_TSO | NETIF_F_GSO | NETIF_F_ALL_TSO |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 			   NETIF_F_HW_CSUM | NETIF_F_SG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	dev->features = dev->hw_features;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	/* MTU range: 68 - 65535 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	dev->min_mtu = ETH_MIN_MTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	dev->max_mtu = VNET_MAX_MTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	SET_NETDEV_DEV(dev, &vdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	err = register_netdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		pr_err("Cannot register net device, aborting\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		goto err_out_free_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	netdev_info(dev, "Sun LDOM vnet %pM\n", dev->dev_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	list_add(&vp->list, &vnet_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	return vp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) err_out_free_dev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	free_netdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) static struct vnet *vnet_find_or_create(const u64 *local_mac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 					struct vio_dev *vdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	struct vnet *iter, *vp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	mutex_lock(&vnet_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	vp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	list_for_each_entry(iter, &vnet_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		if (iter->local_mac == *local_mac) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 			vp = iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	if (!vp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		vp = vnet_new(local_mac, vdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	mutex_unlock(&vnet_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	return vp;
^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) static void vnet_cleanup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	struct vnet *vp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	mutex_lock(&vnet_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	while (!list_empty(&vnet_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		vp = list_first_entry(&vnet_list, struct vnet, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		list_del(&vp->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		dev = vp->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		/* vio_unregister_driver() should have cleaned up port_list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		BUG_ON(!list_empty(&vp->port_list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		unregister_netdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		free_netdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	mutex_unlock(&vnet_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) static const char *local_mac_prop = "local-mac-address";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) static struct vnet *vnet_find_parent(struct mdesc_handle *hp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 				     u64 port_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 				     struct vio_dev *vdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	const u64 *local_mac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	u64 a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	mdesc_for_each_arc(a, hp, port_node, MDESC_ARC_TYPE_BACK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		u64 target = mdesc_arc_target(hp, a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		name = mdesc_get_property(hp, target, "name", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		if (!name || strcmp(name, "network"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		local_mac = mdesc_get_property(hp, target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 					       local_mac_prop, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		if (local_mac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	if (!local_mac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	return vnet_find_or_create(local_mac, vdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) static struct ldc_channel_config vnet_ldc_cfg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	.event		= sunvnet_event_common,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	.mtu		= 64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	.mode		= LDC_MODE_UNRELIABLE,
^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) static struct vio_driver_ops vnet_vio_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	.send_attr		= sunvnet_send_attr_common,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	.handle_attr		= sunvnet_handle_attr_common,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	.handshake_complete	= sunvnet_handshake_complete_common,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) const char *remote_macaddr_prop = "remote-mac-address";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) static int vnet_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	struct mdesc_handle *hp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	struct vnet_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	struct vnet *vp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	const u64 *rmac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	int len, i, err, switch_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	hp = mdesc_grab();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	vp = vnet_find_parent(hp, vdev->mp, vdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	if (IS_ERR(vp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		pr_err("Cannot find port parent vnet\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		err = PTR_ERR(vp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		goto err_out_put_mdesc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	rmac = mdesc_get_property(hp, vdev->mp, remote_macaddr_prop, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	if (!rmac) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		pr_err("Port lacks %s property\n", remote_macaddr_prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		goto err_out_put_mdesc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	port = kzalloc(sizeof(*port), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	if (!port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		goto err_out_put_mdesc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	for (i = 0; i < ETH_ALEN; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		port->raddr[i] = (*rmac >> (5 - i) * 8) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	port->vp = vp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	err = vio_driver_init(&port->vio, vdev, VDEV_NETWORK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 			      vnet_versions, ARRAY_SIZE(vnet_versions),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 			      &vnet_vio_ops, vp->dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		goto err_out_free_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	err = vio_ldc_alloc(&port->vio, &vnet_ldc_cfg, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		goto err_out_free_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	netif_napi_add(port->vp->dev, &port->napi, sunvnet_poll_common,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		       NAPI_POLL_WEIGHT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	INIT_HLIST_NODE(&port->hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	INIT_LIST_HEAD(&port->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	switch_port = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	if (mdesc_get_property(hp, vdev->mp, "switch-port", NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		switch_port = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	port->switch_port = switch_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	port->tso = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	port->tsolen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	spin_lock_irqsave(&vp->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	if (switch_port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		list_add_rcu(&port->list, &vp->port_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		list_add_tail_rcu(&port->list, &vp->port_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	hlist_add_head_rcu(&port->hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 			   &vp->port_hash[vnet_hashfn(port->raddr)]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	sunvnet_port_add_txq_common(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	spin_unlock_irqrestore(&vp->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	dev_set_drvdata(&vdev->dev, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	pr_info("%s: PORT ( remote-mac %pM%s )\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		vp->dev->name, port->raddr, switch_port ? " switch-port" : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	timer_setup(&port->clean_timer, sunvnet_clean_timer_expire_common, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	napi_enable(&port->napi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	vio_port_up(&port->vio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	mdesc_release(hp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) err_out_free_port:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	kfree(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) err_out_put_mdesc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	mdesc_release(hp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) static int vnet_port_remove(struct vio_dev *vdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	struct vnet_port *port = dev_get_drvdata(&vdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	if (port) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 		del_timer_sync(&port->vio.timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		napi_disable(&port->napi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		list_del_rcu(&port->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		hlist_del_rcu(&port->hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		del_timer_sync(&port->clean_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		sunvnet_port_rm_txq_common(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		netif_napi_del(&port->napi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 		sunvnet_port_free_tx_bufs_common(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 		vio_ldc_free(&port->vio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 		dev_set_drvdata(&vdev->dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		kfree(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) static const struct vio_device_id vnet_port_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 		.type = "vnet-port",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) MODULE_DEVICE_TABLE(vio, vnet_port_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) static struct vio_driver vnet_port_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	.id_table	= vnet_port_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	.probe		= vnet_port_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	.remove		= vnet_port_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	.name		= "vnet_port",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) static int __init vnet_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	pr_info("%s\n", version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	return vio_register_driver(&vnet_port_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) static void __exit vnet_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	vio_unregister_driver(&vnet_port_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	vnet_cleanup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) module_init(vnet_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) module_exit(vnet_exit);