xref: /linux/drivers/net/ethernet/sun/sunvnet.c (revision 9410645520e9b820069761f3450ef6661418e279)
1c861ef83SShannon Nelson // SPDX-License-Identifier: GPL-2.0
2e689cf4aSJeff Kirsher /* sunvnet.c: Sun LDOM Virtual Network Driver.
3e689cf4aSJeff Kirsher  *
4e689cf4aSJeff Kirsher  * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
50f512c84SShannon Nelson  * Copyright (C) 2016-2017 Oracle. All rights reserved.
6e689cf4aSJeff Kirsher  */
7e689cf4aSJeff Kirsher 
8e689cf4aSJeff Kirsher #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9e689cf4aSJeff Kirsher 
10e689cf4aSJeff Kirsher #include <linux/module.h>
11e689cf4aSJeff Kirsher #include <linux/kernel.h>
12e689cf4aSJeff Kirsher #include <linux/types.h>
13e689cf4aSJeff Kirsher #include <linux/slab.h>
14e689cf4aSJeff Kirsher #include <linux/delay.h>
15e689cf4aSJeff Kirsher #include <linux/init.h>
16e689cf4aSJeff Kirsher #include <linux/netdevice.h>
17e689cf4aSJeff Kirsher #include <linux/ethtool.h>
18e689cf4aSJeff Kirsher #include <linux/etherdevice.h>
19e689cf4aSJeff Kirsher #include <linux/mutex.h>
20da38c564SDavid L Stevens #include <linux/highmem.h>
21e4defc77SDavid L Stevens #include <linux/if_vlan.h>
22e689cf4aSJeff Kirsher 
23a2b78e9bSDavid L Stevens #if IS_ENABLED(CONFIG_IPV6)
24a2b78e9bSDavid L Stevens #include <linux/icmpv6.h>
25a2b78e9bSDavid L Stevens #endif
26a2b78e9bSDavid L Stevens 
276d0ba919SDavid L Stevens #include <net/ip.h>
28a2b78e9bSDavid L Stevens #include <net/icmp.h>
29a2b78e9bSDavid L Stevens #include <net/route.h>
30a2b78e9bSDavid L Stevens 
31e689cf4aSJeff Kirsher #include <asm/vio.h>
32e689cf4aSJeff Kirsher #include <asm/ldc.h>
33e689cf4aSJeff Kirsher 
3431762eaaSAaron Young #include "sunvnet_common.h"
3531762eaaSAaron Young 
3631762eaaSAaron Young /* length of time before we decide the hardware is borked,
3731762eaaSAaron Young  * and dev->tx_timeout() should be called to fix the problem
3831762eaaSAaron Young  */
3931762eaaSAaron Young #define VNET_TX_TIMEOUT			(5 * HZ)
40e689cf4aSJeff Kirsher 
41e689cf4aSJeff Kirsher #define DRV_MODULE_NAME		"sunvnet"
42f2f3e210SShannon Nelson #define DRV_MODULE_VERSION	"2.0"
43f2f3e210SShannon Nelson #define DRV_MODULE_RELDATE	"February 3, 2017"
44e689cf4aSJeff Kirsher 
45f73d12bdSBill Pemberton static char version[] =
46f2f3e210SShannon Nelson 	DRV_MODULE_NAME " " DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
476a57a219SAhelenia Ziemiańska MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
48e689cf4aSJeff Kirsher MODULE_DESCRIPTION("Sun LDOM virtual network driver");
49e689cf4aSJeff Kirsher MODULE_LICENSE("GPL");
50e689cf4aSJeff Kirsher MODULE_VERSION(DRV_MODULE_VERSION);
51e689cf4aSJeff Kirsher 
52e689cf4aSJeff Kirsher /* Ordered from largest major to lowest */
53e689cf4aSJeff Kirsher static struct vio_version vnet_versions[] = {
546d0ba919SDavid L Stevens 	{ .major = 1, .minor = 8 },
556d0ba919SDavid L Stevens 	{ .major = 1, .minor = 7 },
56e4defc77SDavid L Stevens 	{ .major = 1, .minor = 6 },
57e689cf4aSJeff Kirsher 	{ .major = 1, .minor = 0 },
58e689cf4aSJeff Kirsher };
59e689cf4aSJeff Kirsher 
vnet_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)60e689cf4aSJeff Kirsher static void vnet_get_drvinfo(struct net_device *dev,
61e689cf4aSJeff Kirsher 			     struct ethtool_drvinfo *info)
62e689cf4aSJeff Kirsher {
63f029c781SWolfram Sang 	strscpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
64f029c781SWolfram Sang 	strscpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
65e689cf4aSJeff Kirsher }
66e689cf4aSJeff Kirsher 
vnet_get_msglevel(struct net_device * dev)67e689cf4aSJeff Kirsher static u32 vnet_get_msglevel(struct net_device *dev)
68e689cf4aSJeff Kirsher {
69e689cf4aSJeff Kirsher 	struct vnet *vp = netdev_priv(dev);
70dc153f85SAaron Young 
71e689cf4aSJeff Kirsher 	return vp->msg_enable;
72e689cf4aSJeff Kirsher }
73e689cf4aSJeff Kirsher 
vnet_set_msglevel(struct net_device * dev,u32 value)74e689cf4aSJeff Kirsher static void vnet_set_msglevel(struct net_device *dev, u32 value)
75e689cf4aSJeff Kirsher {
76e689cf4aSJeff Kirsher 	struct vnet *vp = netdev_priv(dev);
77dc153f85SAaron Young 
78e689cf4aSJeff Kirsher 	vp->msg_enable = value;
79e689cf4aSJeff Kirsher }
80e689cf4aSJeff Kirsher 
810f512c84SShannon Nelson static const struct {
820f512c84SShannon Nelson 	const char string[ETH_GSTRING_LEN];
830f512c84SShannon Nelson } ethtool_stats_keys[] = {
840f512c84SShannon Nelson 	{ "rx_packets" },
850f512c84SShannon Nelson 	{ "tx_packets" },
860f512c84SShannon Nelson 	{ "rx_bytes" },
870f512c84SShannon Nelson 	{ "tx_bytes" },
880f512c84SShannon Nelson 	{ "rx_errors" },
890f512c84SShannon Nelson 	{ "tx_errors" },
900f512c84SShannon Nelson 	{ "rx_dropped" },
910f512c84SShannon Nelson 	{ "tx_dropped" },
920f512c84SShannon Nelson 	{ "multicast" },
930f512c84SShannon Nelson 	{ "rx_length_errors" },
940f512c84SShannon Nelson 	{ "rx_frame_errors" },
950f512c84SShannon Nelson 	{ "rx_missed_errors" },
960f512c84SShannon Nelson 	{ "tx_carrier_errors" },
970f512c84SShannon Nelson 	{ "nports" },
980f512c84SShannon Nelson };
990f512c84SShannon Nelson 
vnet_get_sset_count(struct net_device * dev,int sset)1000f512c84SShannon Nelson static int vnet_get_sset_count(struct net_device *dev, int sset)
1010f512c84SShannon Nelson {
1020f512c84SShannon Nelson 	struct vnet *vp = (struct vnet *)netdev_priv(dev);
1030f512c84SShannon Nelson 
1040f512c84SShannon Nelson 	switch (sset) {
1050f512c84SShannon Nelson 	case ETH_SS_STATS:
1060f512c84SShannon Nelson 		return ARRAY_SIZE(ethtool_stats_keys)
1070f512c84SShannon Nelson 			+ (NUM_VNET_PORT_STATS * vp->nports);
1080f512c84SShannon Nelson 	default:
1090f512c84SShannon Nelson 		return -EOPNOTSUPP;
1100f512c84SShannon Nelson 	}
1110f512c84SShannon Nelson }
1120f512c84SShannon Nelson 
vnet_get_strings(struct net_device * dev,u32 stringset,u8 * buf)1130f512c84SShannon Nelson static void vnet_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
1140f512c84SShannon Nelson {
1150f512c84SShannon Nelson 	struct vnet *vp = (struct vnet *)netdev_priv(dev);
1160f512c84SShannon Nelson 	struct vnet_port *port;
1170f512c84SShannon Nelson 
1180f512c84SShannon Nelson 	switch (stringset) {
1190f512c84SShannon Nelson 	case ETH_SS_STATS:
1200f512c84SShannon Nelson 		memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
121*f547e956SRosen Penev 		buf += sizeof(ethtool_stats_keys);
1220f512c84SShannon Nelson 
1230f512c84SShannon Nelson 		rcu_read_lock();
1240f512c84SShannon Nelson 		list_for_each_entry_rcu(port, &vp->port_list, list) {
125*f547e956SRosen Penev 			ethtool_sprintf(&buf, "p%u.%s-%pM", port->q_index,
126*f547e956SRosen Penev 					port->switch_port ? "s" : "q",
1270f512c84SShannon Nelson 					port->raddr);
128*f547e956SRosen Penev 			ethtool_sprintf(&buf, "p%u.rx_packets", port->q_index);
129*f547e956SRosen Penev 			ethtool_sprintf(&buf, "p%u.tx_packets", port->q_index);
130*f547e956SRosen Penev 			ethtool_sprintf(&buf, "p%u.rx_bytes", port->q_index);
131*f547e956SRosen Penev 			ethtool_sprintf(&buf, "p%u.tx_bytes", port->q_index);
132*f547e956SRosen Penev 			ethtool_sprintf(&buf, "p%u.event_up", port->q_index);
133*f547e956SRosen Penev 			ethtool_sprintf(&buf, "p%u.event_reset", port->q_index);
1340f512c84SShannon Nelson 		}
1350f512c84SShannon Nelson 		rcu_read_unlock();
1360f512c84SShannon Nelson 		break;
1370f512c84SShannon Nelson 	default:
1380f512c84SShannon Nelson 		WARN_ON(1);
1390f512c84SShannon Nelson 		break;
1400f512c84SShannon Nelson 	}
1410f512c84SShannon Nelson }
1420f512c84SShannon Nelson 
vnet_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * estats,u64 * data)1430f512c84SShannon Nelson static void vnet_get_ethtool_stats(struct net_device *dev,
1440f512c84SShannon Nelson 				   struct ethtool_stats *estats, u64 *data)
1450f512c84SShannon Nelson {
1460f512c84SShannon Nelson 	struct vnet *vp = (struct vnet *)netdev_priv(dev);
1470f512c84SShannon Nelson 	struct vnet_port *port;
1480f512c84SShannon Nelson 	int i = 0;
1490f512c84SShannon Nelson 
1500f512c84SShannon Nelson 	data[i++] = dev->stats.rx_packets;
1510f512c84SShannon Nelson 	data[i++] = dev->stats.tx_packets;
1520f512c84SShannon Nelson 	data[i++] = dev->stats.rx_bytes;
1530f512c84SShannon Nelson 	data[i++] = dev->stats.tx_bytes;
1540f512c84SShannon Nelson 	data[i++] = dev->stats.rx_errors;
1550f512c84SShannon Nelson 	data[i++] = dev->stats.tx_errors;
1560f512c84SShannon Nelson 	data[i++] = dev->stats.rx_dropped;
1570f512c84SShannon Nelson 	data[i++] = dev->stats.tx_dropped;
1580f512c84SShannon Nelson 	data[i++] = dev->stats.multicast;
1590f512c84SShannon Nelson 	data[i++] = dev->stats.rx_length_errors;
1600f512c84SShannon Nelson 	data[i++] = dev->stats.rx_frame_errors;
1610f512c84SShannon Nelson 	data[i++] = dev->stats.rx_missed_errors;
1620f512c84SShannon Nelson 	data[i++] = dev->stats.tx_carrier_errors;
1630f512c84SShannon Nelson 	data[i++] = vp->nports;
1640f512c84SShannon Nelson 
1650f512c84SShannon Nelson 	rcu_read_lock();
1660f512c84SShannon Nelson 	list_for_each_entry_rcu(port, &vp->port_list, list) {
1670f512c84SShannon Nelson 		data[i++] = port->q_index;
1680f512c84SShannon Nelson 		data[i++] = port->stats.rx_packets;
1690f512c84SShannon Nelson 		data[i++] = port->stats.tx_packets;
1700f512c84SShannon Nelson 		data[i++] = port->stats.rx_bytes;
1710f512c84SShannon Nelson 		data[i++] = port->stats.tx_bytes;
1720f512c84SShannon Nelson 		data[i++] = port->stats.event_up;
1730f512c84SShannon Nelson 		data[i++] = port->stats.event_reset;
1740f512c84SShannon Nelson 	}
1750f512c84SShannon Nelson 	rcu_read_unlock();
1760f512c84SShannon Nelson }
1770f512c84SShannon Nelson 
178e689cf4aSJeff Kirsher static const struct ethtool_ops vnet_ethtool_ops = {
179e689cf4aSJeff Kirsher 	.get_drvinfo		= vnet_get_drvinfo,
180e689cf4aSJeff Kirsher 	.get_msglevel		= vnet_get_msglevel,
181e689cf4aSJeff Kirsher 	.set_msglevel		= vnet_set_msglevel,
182e689cf4aSJeff Kirsher 	.get_link		= ethtool_op_get_link,
1830f512c84SShannon Nelson 	.get_sset_count		= vnet_get_sset_count,
1840f512c84SShannon Nelson 	.get_strings		= vnet_get_strings,
1850f512c84SShannon Nelson 	.get_ethtool_stats	= vnet_get_ethtool_stats,
186e689cf4aSJeff Kirsher };
187e689cf4aSJeff Kirsher 
188e689cf4aSJeff Kirsher static LIST_HEAD(vnet_list);
189e689cf4aSJeff Kirsher static DEFINE_MUTEX(vnet_list_mutex);
190e689cf4aSJeff Kirsher 
__tx_port_find(struct vnet * vp,struct sk_buff * skb)19167d0719fSAaron Young static struct vnet_port *__tx_port_find(struct vnet *vp, struct sk_buff *skb)
19267d0719fSAaron Young {
19367d0719fSAaron Young 	unsigned int hash = vnet_hashfn(skb->data);
19467d0719fSAaron Young 	struct hlist_head *hp = &vp->port_hash[hash];
19567d0719fSAaron Young 	struct vnet_port *port;
19667d0719fSAaron Young 
19767d0719fSAaron Young 	hlist_for_each_entry_rcu(port, hp, hash) {
19867d0719fSAaron Young 		if (!sunvnet_port_is_up_common(port))
19967d0719fSAaron Young 			continue;
20067d0719fSAaron Young 		if (ether_addr_equal(port->raddr, skb->data))
20167d0719fSAaron Young 			return port;
20267d0719fSAaron Young 	}
20367d0719fSAaron Young 	list_for_each_entry_rcu(port, &vp->port_list, list) {
20467d0719fSAaron Young 		if (!port->switch_port)
20567d0719fSAaron Young 			continue;
20667d0719fSAaron Young 		if (!sunvnet_port_is_up_common(port))
20767d0719fSAaron Young 			continue;
20867d0719fSAaron Young 		return port;
20967d0719fSAaron Young 	}
21067d0719fSAaron Young 	return NULL;
21167d0719fSAaron Young }
21267d0719fSAaron Young 
21367d0719fSAaron Young /* func arg to vnet_start_xmit_common() to get the proper tx port */
vnet_tx_port_find(struct sk_buff * skb,struct net_device * dev)21467d0719fSAaron Young static struct vnet_port *vnet_tx_port_find(struct sk_buff *skb,
21567d0719fSAaron Young 					   struct net_device *dev)
21667d0719fSAaron Young {
21767d0719fSAaron Young 	struct vnet *vp = netdev_priv(dev);
21867d0719fSAaron Young 
21967d0719fSAaron Young 	return __tx_port_find(vp, skb);
22067d0719fSAaron Young }
22167d0719fSAaron Young 
vnet_select_queue(struct net_device * dev,struct sk_buff * skb,struct net_device * sb_dev)22267d0719fSAaron Young static u16 vnet_select_queue(struct net_device *dev, struct sk_buff *skb,
223a350ecceSPaolo Abeni 			     struct net_device *sb_dev)
22467d0719fSAaron Young {
22567d0719fSAaron Young 	struct vnet *vp = netdev_priv(dev);
22667d0719fSAaron Young 	struct vnet_port *port = __tx_port_find(vp, skb);
22767d0719fSAaron Young 
22867d0719fSAaron Young 	if (!port)
22967d0719fSAaron Young 		return 0;
23067d0719fSAaron Young 
23167d0719fSAaron Young 	return port->q_index;
23267d0719fSAaron Young }
23367d0719fSAaron Young 
23467d0719fSAaron Young /* Wrappers to common functions */
vnet_start_xmit(struct sk_buff * skb,struct net_device * dev)2350e0cc31fSYueHaibing static netdev_tx_t vnet_start_xmit(struct sk_buff *skb, struct net_device *dev)
23667d0719fSAaron Young {
23767d0719fSAaron Young 	return sunvnet_start_xmit_common(skb, dev, vnet_tx_port_find);
23867d0719fSAaron Young }
23967d0719fSAaron Young 
vnet_set_rx_mode(struct net_device * dev)24067d0719fSAaron Young static void vnet_set_rx_mode(struct net_device *dev)
24167d0719fSAaron Young {
24267d0719fSAaron Young 	struct vnet *vp = netdev_priv(dev);
24367d0719fSAaron Young 
24467d0719fSAaron Young 	return sunvnet_set_rx_mode_common(dev, vp);
24567d0719fSAaron Young }
24667d0719fSAaron Young 
24767d0719fSAaron Young #ifdef CONFIG_NET_POLL_CONTROLLER
vnet_poll_controller(struct net_device * dev)24867d0719fSAaron Young static void vnet_poll_controller(struct net_device *dev)
24967d0719fSAaron Young {
25067d0719fSAaron Young 	struct vnet *vp = netdev_priv(dev);
25167d0719fSAaron Young 
25267d0719fSAaron Young 	return sunvnet_poll_controller_common(dev, vp);
25367d0719fSAaron Young }
25467d0719fSAaron Young #endif
25567d0719fSAaron Young 
256e689cf4aSJeff Kirsher static const struct net_device_ops vnet_ops = {
25731762eaaSAaron Young 	.ndo_open		= sunvnet_open_common,
25831762eaaSAaron Young 	.ndo_stop		= sunvnet_close_common,
25967d0719fSAaron Young 	.ndo_set_rx_mode	= vnet_set_rx_mode,
26031762eaaSAaron Young 	.ndo_set_mac_address	= sunvnet_set_mac_addr_common,
261e689cf4aSJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
26231762eaaSAaron Young 	.ndo_tx_timeout		= sunvnet_tx_timeout_common,
26367d0719fSAaron Young 	.ndo_start_xmit		= vnet_start_xmit,
26467d0719fSAaron Young 	.ndo_select_queue	= vnet_select_queue,
26569088822SSowmini Varadhan #ifdef CONFIG_NET_POLL_CONTROLLER
26667d0719fSAaron Young 	.ndo_poll_controller	= vnet_poll_controller,
26769088822SSowmini Varadhan #endif
268e689cf4aSJeff Kirsher };
269e689cf4aSJeff Kirsher 
vnet_new(const u64 * local_mac,struct vio_dev * vdev)2704c5d283aSSowmini Varadhan static struct vnet *vnet_new(const u64 *local_mac,
2714c5d283aSSowmini Varadhan 			     struct vio_dev *vdev)
272e689cf4aSJeff Kirsher {
273e689cf4aSJeff Kirsher 	struct net_device *dev;
2744abd7cffSJakub Kicinski 	u8 addr[ETH_ALEN];
275e689cf4aSJeff Kirsher 	struct vnet *vp;
276e689cf4aSJeff Kirsher 	int err, i;
277e689cf4aSJeff Kirsher 
278d51bffd1SSowmini Varadhan 	dev = alloc_etherdev_mqs(sizeof(*vp), VNET_MAX_TXQS, 1);
27941de8d4cSJoe Perches 	if (!dev)
280e689cf4aSJeff Kirsher 		return ERR_PTR(-ENOMEM);
2818e845f4cSDavid L Stevens 	dev->needed_headroom = VNET_PACKET_SKIP + 8;
2828e845f4cSDavid L Stevens 	dev->needed_tailroom = 8;
283e689cf4aSJeff Kirsher 
284e689cf4aSJeff Kirsher 	for (i = 0; i < ETH_ALEN; i++)
2854abd7cffSJakub Kicinski 		addr[i] = (*local_mac >> (5 - i) * 8) & 0xff;
2864abd7cffSJakub Kicinski 	eth_hw_addr_set(dev, addr);
287e689cf4aSJeff Kirsher 
288e689cf4aSJeff Kirsher 	vp = netdev_priv(dev);
289e689cf4aSJeff Kirsher 
290e689cf4aSJeff Kirsher 	spin_lock_init(&vp->lock);
291e689cf4aSJeff Kirsher 	vp->dev = dev;
292e689cf4aSJeff Kirsher 
293e689cf4aSJeff Kirsher 	INIT_LIST_HEAD(&vp->port_list);
294e689cf4aSJeff Kirsher 	for (i = 0; i < VNET_PORT_HASH_SIZE; i++)
295e689cf4aSJeff Kirsher 		INIT_HLIST_HEAD(&vp->port_hash[i]);
296e689cf4aSJeff Kirsher 	INIT_LIST_HEAD(&vp->list);
297e689cf4aSJeff Kirsher 	vp->local_mac = *local_mac;
298e689cf4aSJeff Kirsher 
299e689cf4aSJeff Kirsher 	dev->netdev_ops = &vnet_ops;
300e689cf4aSJeff Kirsher 	dev->ethtool_ops = &vnet_ethtool_ops;
301e689cf4aSJeff Kirsher 	dev->watchdog_timeo = VNET_TX_TIMEOUT;
302e689cf4aSJeff Kirsher 
303cf55612aSCathy Zhou 	dev->hw_features = NETIF_F_TSO | NETIF_F_GSO | NETIF_F_ALL_TSO |
30498524e04SShannon Nelson 			   NETIF_F_HW_CSUM | NETIF_F_SG;
305da38c564SDavid L Stevens 	dev->features = dev->hw_features;
306da38c564SDavid L Stevens 
307540bfe30SJarod Wilson 	/* MTU range: 68 - 65535 */
308540bfe30SJarod Wilson 	dev->min_mtu = ETH_MIN_MTU;
309540bfe30SJarod Wilson 	dev->max_mtu = VNET_MAX_MTU;
310540bfe30SJarod Wilson 
3114c5d283aSSowmini Varadhan 	SET_NETDEV_DEV(dev, &vdev->dev);
3124c5d283aSSowmini Varadhan 
313e689cf4aSJeff Kirsher 	err = register_netdev(dev);
314e689cf4aSJeff Kirsher 	if (err) {
315e689cf4aSJeff Kirsher 		pr_err("Cannot register net device, aborting\n");
316e689cf4aSJeff Kirsher 		goto err_out_free_dev;
317e689cf4aSJeff Kirsher 	}
318e689cf4aSJeff Kirsher 
319e689cf4aSJeff Kirsher 	netdev_info(dev, "Sun LDOM vnet %pM\n", dev->dev_addr);
320e689cf4aSJeff Kirsher 
321e689cf4aSJeff Kirsher 	list_add(&vp->list, &vnet_list);
322e689cf4aSJeff Kirsher 
323e689cf4aSJeff Kirsher 	return vp;
324e689cf4aSJeff Kirsher 
325e689cf4aSJeff Kirsher err_out_free_dev:
326e689cf4aSJeff Kirsher 	free_netdev(dev);
327e689cf4aSJeff Kirsher 
328e689cf4aSJeff Kirsher 	return ERR_PTR(err);
329e689cf4aSJeff Kirsher }
330e689cf4aSJeff Kirsher 
vnet_find_or_create(const u64 * local_mac,struct vio_dev * vdev)3314c5d283aSSowmini Varadhan static struct vnet *vnet_find_or_create(const u64 *local_mac,
3324c5d283aSSowmini Varadhan 					struct vio_dev *vdev)
333e689cf4aSJeff Kirsher {
334e689cf4aSJeff Kirsher 	struct vnet *iter, *vp;
335e689cf4aSJeff Kirsher 
336e689cf4aSJeff Kirsher 	mutex_lock(&vnet_list_mutex);
337e689cf4aSJeff Kirsher 	vp = NULL;
338e689cf4aSJeff Kirsher 	list_for_each_entry(iter, &vnet_list, list) {
339e689cf4aSJeff Kirsher 		if (iter->local_mac == *local_mac) {
340e689cf4aSJeff Kirsher 			vp = iter;
341e689cf4aSJeff Kirsher 			break;
342e689cf4aSJeff Kirsher 		}
343e689cf4aSJeff Kirsher 	}
344e689cf4aSJeff Kirsher 	if (!vp)
3454c5d283aSSowmini Varadhan 		vp = vnet_new(local_mac, vdev);
346e689cf4aSJeff Kirsher 	mutex_unlock(&vnet_list_mutex);
347e689cf4aSJeff Kirsher 
348e689cf4aSJeff Kirsher 	return vp;
349e689cf4aSJeff Kirsher }
350e689cf4aSJeff Kirsher 
vnet_cleanup(void)351a4b70a07SSowmini Varadhan static void vnet_cleanup(void)
352a4b70a07SSowmini Varadhan {
353a4b70a07SSowmini Varadhan 	struct vnet *vp;
354a4b70a07SSowmini Varadhan 	struct net_device *dev;
355a4b70a07SSowmini Varadhan 
356a4b70a07SSowmini Varadhan 	mutex_lock(&vnet_list_mutex);
357a4b70a07SSowmini Varadhan 	while (!list_empty(&vnet_list)) {
358a4b70a07SSowmini Varadhan 		vp = list_first_entry(&vnet_list, struct vnet, list);
359a4b70a07SSowmini Varadhan 		list_del(&vp->list);
360a4b70a07SSowmini Varadhan 		dev = vp->dev;
361a4b70a07SSowmini Varadhan 		/* vio_unregister_driver() should have cleaned up port_list */
362a4b70a07SSowmini Varadhan 		BUG_ON(!list_empty(&vp->port_list));
363a4b70a07SSowmini Varadhan 		unregister_netdev(dev);
364a4b70a07SSowmini Varadhan 		free_netdev(dev);
365a4b70a07SSowmini Varadhan 	}
366a4b70a07SSowmini Varadhan 	mutex_unlock(&vnet_list_mutex);
367a4b70a07SSowmini Varadhan }
368a4b70a07SSowmini Varadhan 
369e689cf4aSJeff Kirsher static const char *local_mac_prop = "local-mac-address";
370e689cf4aSJeff Kirsher 
vnet_find_parent(struct mdesc_handle * hp,u64 port_node,struct vio_dev * vdev)371f73d12bdSBill Pemberton static struct vnet *vnet_find_parent(struct mdesc_handle *hp,
3724c5d283aSSowmini Varadhan 				     u64 port_node,
3734c5d283aSSowmini Varadhan 				     struct vio_dev *vdev)
374e689cf4aSJeff Kirsher {
375e689cf4aSJeff Kirsher 	const u64 *local_mac = NULL;
376e689cf4aSJeff Kirsher 	u64 a;
377e689cf4aSJeff Kirsher 
378e689cf4aSJeff Kirsher 	mdesc_for_each_arc(a, hp, port_node, MDESC_ARC_TYPE_BACK) {
379e689cf4aSJeff Kirsher 		u64 target = mdesc_arc_target(hp, a);
380e689cf4aSJeff Kirsher 		const char *name;
381e689cf4aSJeff Kirsher 
382e689cf4aSJeff Kirsher 		name = mdesc_get_property(hp, target, "name", NULL);
383e689cf4aSJeff Kirsher 		if (!name || strcmp(name, "network"))
384e689cf4aSJeff Kirsher 			continue;
385e689cf4aSJeff Kirsher 
386e689cf4aSJeff Kirsher 		local_mac = mdesc_get_property(hp, target,
387e689cf4aSJeff Kirsher 					       local_mac_prop, NULL);
388e689cf4aSJeff Kirsher 		if (local_mac)
389e689cf4aSJeff Kirsher 			break;
390e689cf4aSJeff Kirsher 	}
391e689cf4aSJeff Kirsher 	if (!local_mac)
392e689cf4aSJeff Kirsher 		return ERR_PTR(-ENODEV);
393e689cf4aSJeff Kirsher 
3944c5d283aSSowmini Varadhan 	return vnet_find_or_create(local_mac, vdev);
395e689cf4aSJeff Kirsher }
396e689cf4aSJeff Kirsher 
397e689cf4aSJeff Kirsher static struct ldc_channel_config vnet_ldc_cfg = {
39831762eaaSAaron Young 	.event		= sunvnet_event_common,
399e689cf4aSJeff Kirsher 	.mtu		= 64,
400e689cf4aSJeff Kirsher 	.mode		= LDC_MODE_UNRELIABLE,
401e689cf4aSJeff Kirsher };
402e689cf4aSJeff Kirsher 
403e689cf4aSJeff Kirsher static struct vio_driver_ops vnet_vio_ops = {
40431762eaaSAaron Young 	.send_attr		= sunvnet_send_attr_common,
40531762eaaSAaron Young 	.handle_attr		= sunvnet_handle_attr_common,
40631762eaaSAaron Young 	.handshake_complete	= sunvnet_handshake_complete_common,
407e689cf4aSJeff Kirsher };
408e689cf4aSJeff Kirsher 
409e689cf4aSJeff Kirsher const char *remote_macaddr_prop = "remote-mac-address";
410e689cf4aSJeff Kirsher 
vnet_port_probe(struct vio_dev * vdev,const struct vio_device_id * id)4111dd06ae8SGreg Kroah-Hartman static int vnet_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
412e689cf4aSJeff Kirsher {
413e689cf4aSJeff Kirsher 	struct mdesc_handle *hp;
414e689cf4aSJeff Kirsher 	struct vnet_port *port;
415e689cf4aSJeff Kirsher 	unsigned long flags;
416e689cf4aSJeff Kirsher 	struct vnet *vp;
417e689cf4aSJeff Kirsher 	const u64 *rmac;
418e689cf4aSJeff Kirsher 	int len, i, err, switch_port;
419e689cf4aSJeff Kirsher 
420e689cf4aSJeff Kirsher 	hp = mdesc_grab();
421e689cf4aSJeff Kirsher 
42290de546dSLiang He 	if (!hp)
42390de546dSLiang He 		return -ENODEV;
42490de546dSLiang He 
4254c5d283aSSowmini Varadhan 	vp = vnet_find_parent(hp, vdev->mp, vdev);
426e689cf4aSJeff Kirsher 	if (IS_ERR(vp)) {
427e689cf4aSJeff Kirsher 		pr_err("Cannot find port parent vnet\n");
428e689cf4aSJeff Kirsher 		err = PTR_ERR(vp);
429e689cf4aSJeff Kirsher 		goto err_out_put_mdesc;
430e689cf4aSJeff Kirsher 	}
431e689cf4aSJeff Kirsher 
432e689cf4aSJeff Kirsher 	rmac = mdesc_get_property(hp, vdev->mp, remote_macaddr_prop, &len);
433e689cf4aSJeff Kirsher 	err = -ENODEV;
434e689cf4aSJeff Kirsher 	if (!rmac) {
435e689cf4aSJeff Kirsher 		pr_err("Port lacks %s property\n", remote_macaddr_prop);
436e689cf4aSJeff Kirsher 		goto err_out_put_mdesc;
437e689cf4aSJeff Kirsher 	}
438e689cf4aSJeff Kirsher 
439e689cf4aSJeff Kirsher 	port = kzalloc(sizeof(*port), GFP_KERNEL);
440e689cf4aSJeff Kirsher 	err = -ENOMEM;
441e404decbSJoe Perches 	if (!port)
442e689cf4aSJeff Kirsher 		goto err_out_put_mdesc;
443e689cf4aSJeff Kirsher 
444e689cf4aSJeff Kirsher 	for (i = 0; i < ETH_ALEN; i++)
445e689cf4aSJeff Kirsher 		port->raddr[i] = (*rmac >> (5 - i) * 8) & 0xff;
446e689cf4aSJeff Kirsher 
447e689cf4aSJeff Kirsher 	port->vp = vp;
448e689cf4aSJeff Kirsher 
449e689cf4aSJeff Kirsher 	err = vio_driver_init(&port->vio, vdev, VDEV_NETWORK,
450e689cf4aSJeff Kirsher 			      vnet_versions, ARRAY_SIZE(vnet_versions),
451e689cf4aSJeff Kirsher 			      &vnet_vio_ops, vp->dev->name);
452e689cf4aSJeff Kirsher 	if (err)
453e689cf4aSJeff Kirsher 		goto err_out_free_port;
454e689cf4aSJeff Kirsher 
455e689cf4aSJeff Kirsher 	err = vio_ldc_alloc(&port->vio, &vnet_ldc_cfg, port);
456e689cf4aSJeff Kirsher 	if (err)
457e689cf4aSJeff Kirsher 		goto err_out_free_port;
458e689cf4aSJeff Kirsher 
459b48b89f9SJakub Kicinski 	netif_napi_add(port->vp->dev, &port->napi, sunvnet_poll_common);
46069088822SSowmini Varadhan 
461e689cf4aSJeff Kirsher 	INIT_HLIST_NODE(&port->hash);
462e689cf4aSJeff Kirsher 	INIT_LIST_HEAD(&port->list);
463e689cf4aSJeff Kirsher 
464e689cf4aSJeff Kirsher 	switch_port = 0;
465dc153f85SAaron Young 	if (mdesc_get_property(hp, vdev->mp, "switch-port", NULL))
466e689cf4aSJeff Kirsher 		switch_port = 1;
467e689cf4aSJeff Kirsher 	port->switch_port = switch_port;
468368e36edSDavid L Stevens 	port->tso = true;
469368e36edSDavid L Stevens 	port->tsolen = 0;
470e689cf4aSJeff Kirsher 
471e689cf4aSJeff Kirsher 	spin_lock_irqsave(&vp->lock, flags);
472e689cf4aSJeff Kirsher 	if (switch_port)
4732a968dd8SSowmini Varadhan 		list_add_rcu(&port->list, &vp->port_list);
474e689cf4aSJeff Kirsher 	else
4752a968dd8SSowmini Varadhan 		list_add_tail_rcu(&port->list, &vp->port_list);
4762a968dd8SSowmini Varadhan 	hlist_add_head_rcu(&port->hash,
4772a968dd8SSowmini Varadhan 			   &vp->port_hash[vnet_hashfn(port->raddr)]);
47831762eaaSAaron Young 	sunvnet_port_add_txq_common(port);
479e689cf4aSJeff Kirsher 	spin_unlock_irqrestore(&vp->lock, flags);
480e689cf4aSJeff Kirsher 
481e689cf4aSJeff Kirsher 	dev_set_drvdata(&vdev->dev, port);
482e689cf4aSJeff Kirsher 
483e689cf4aSJeff Kirsher 	pr_info("%s: PORT ( remote-mac %pM%s )\n",
484e689cf4aSJeff Kirsher 		vp->dev->name, port->raddr, switch_port ? " switch-port" : "");
485e689cf4aSJeff Kirsher 
4860822c5d9SKees Cook 	timer_setup(&port->clean_timer, sunvnet_clean_timer_expire_common, 0);
4878e845f4cSDavid L Stevens 
48869088822SSowmini Varadhan 	napi_enable(&port->napi);
489e689cf4aSJeff Kirsher 	vio_port_up(&port->vio);
490e689cf4aSJeff Kirsher 
491e689cf4aSJeff Kirsher 	mdesc_release(hp);
492e689cf4aSJeff Kirsher 
493e689cf4aSJeff Kirsher 	return 0;
494e689cf4aSJeff Kirsher 
495e689cf4aSJeff Kirsher err_out_free_port:
496e689cf4aSJeff Kirsher 	kfree(port);
497e689cf4aSJeff Kirsher 
498e689cf4aSJeff Kirsher err_out_put_mdesc:
499e689cf4aSJeff Kirsher 	mdesc_release(hp);
500e689cf4aSJeff Kirsher 	return err;
501e689cf4aSJeff Kirsher }
502e689cf4aSJeff Kirsher 
vnet_port_remove(struct vio_dev * vdev)5031553573cSUwe Kleine-König static void vnet_port_remove(struct vio_dev *vdev)
504e689cf4aSJeff Kirsher {
505e689cf4aSJeff Kirsher 	struct vnet_port *port = dev_get_drvdata(&vdev->dev);
506e689cf4aSJeff Kirsher 
507e689cf4aSJeff Kirsher 	if (port) {
508e689cf4aSJeff Kirsher 		del_timer_sync(&port->vio.timer);
509e689cf4aSJeff Kirsher 
51069088822SSowmini Varadhan 		napi_disable(&port->napi);
511e689cf4aSJeff Kirsher 
5122a968dd8SSowmini Varadhan 		list_del_rcu(&port->list);
5132a968dd8SSowmini Varadhan 		hlist_del_rcu(&port->hash);
5142a968dd8SSowmini Varadhan 
5152a968dd8SSowmini Varadhan 		synchronize_rcu();
516292a089dSSteven Rostedt (Google) 		timer_shutdown_sync(&port->clean_timer);
51731762eaaSAaron Young 		sunvnet_port_rm_txq_common(port);
51869088822SSowmini Varadhan 		netif_napi_del(&port->napi);
51931762eaaSAaron Young 		sunvnet_port_free_tx_bufs_common(port);
520e689cf4aSJeff Kirsher 		vio_ldc_free(&port->vio);
521e689cf4aSJeff Kirsher 
522e689cf4aSJeff Kirsher 		dev_set_drvdata(&vdev->dev, NULL);
523e689cf4aSJeff Kirsher 
524e689cf4aSJeff Kirsher 		kfree(port);
525e689cf4aSJeff Kirsher 	}
526e689cf4aSJeff Kirsher }
527e689cf4aSJeff Kirsher 
528e689cf4aSJeff Kirsher static const struct vio_device_id vnet_port_match[] = {
529e689cf4aSJeff Kirsher 	{
530e689cf4aSJeff Kirsher 		.type = "vnet-port",
531e689cf4aSJeff Kirsher 	},
532e689cf4aSJeff Kirsher 	{},
533e689cf4aSJeff Kirsher };
534e689cf4aSJeff Kirsher MODULE_DEVICE_TABLE(vio, vnet_port_match);
535e689cf4aSJeff Kirsher 
536e689cf4aSJeff Kirsher static struct vio_driver vnet_port_driver = {
537e689cf4aSJeff Kirsher 	.id_table	= vnet_port_match,
538e689cf4aSJeff Kirsher 	.probe		= vnet_port_probe,
539e689cf4aSJeff Kirsher 	.remove		= vnet_port_remove,
540e689cf4aSJeff Kirsher 	.name		= "vnet_port",
541e689cf4aSJeff Kirsher };
542e689cf4aSJeff Kirsher 
vnet_init(void)543e689cf4aSJeff Kirsher static int __init vnet_init(void)
544e689cf4aSJeff Kirsher {
545f2f3e210SShannon Nelson 	pr_info("%s\n", version);
546e689cf4aSJeff Kirsher 	return vio_register_driver(&vnet_port_driver);
547e689cf4aSJeff Kirsher }
548e689cf4aSJeff Kirsher 
vnet_exit(void)549e689cf4aSJeff Kirsher static void __exit vnet_exit(void)
550e689cf4aSJeff Kirsher {
551e689cf4aSJeff Kirsher 	vio_unregister_driver(&vnet_port_driver);
552a4b70a07SSowmini Varadhan 	vnet_cleanup();
553e689cf4aSJeff Kirsher }
554e689cf4aSJeff Kirsher 
555e689cf4aSJeff Kirsher module_init(vnet_init);
556e689cf4aSJeff Kirsher module_exit(vnet_exit);
557