sunvnet.c 10.4 KB
Newer Older
1 2
/* sunvnet.c: Sun LDOM Virtual Network Driver.
 *
3
 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
4
 * Copyright (C) 2016 Oracle. All rights reserved.
5 6
 */

7 8
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

9 10 11 12 13 14 15 16 17
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/netdevice.h>
#include <linux/ethtool.h>
#include <linux/etherdevice.h>
18
#include <linux/mutex.h>
19
#include <linux/highmem.h>
20
#include <linux/if_vlan.h>
21

22 23 24 25
#if IS_ENABLED(CONFIG_IPV6)
#include <linux/icmpv6.h>
#endif

26
#include <net/ip.h>
27 28 29
#include <net/icmp.h>
#include <net/route.h>

30 31 32
#include <asm/vio.h>
#include <asm/ldc.h>

33 34 35 36 37 38
#include "sunvnet_common.h"

/* length of time before we decide the hardware is borked,
 * and dev->tx_timeout() should be called to fix the problem
 */
#define VNET_TX_TIMEOUT			(5 * HZ)
39 40 41 42 43

#define DRV_MODULE_NAME		"sunvnet"
#define DRV_MODULE_VERSION	"1.0"
#define DRV_MODULE_RELDATE	"June 25, 2007"

B
Bill Pemberton 已提交
44
static char version[] =
45 46 47 48 49 50 51 52
	DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
MODULE_DESCRIPTION("Sun LDOM virtual network driver");
MODULE_LICENSE("GPL");
MODULE_VERSION(DRV_MODULE_VERSION);

/* Ordered from largest major to lowest */
static struct vio_version vnet_versions[] = {
53 54
	{ .major = 1, .minor = 8 },
	{ .major = 1, .minor = 7 },
55
	{ .major = 1, .minor = 6 },
56 57 58 59 60 61
	{ .major = 1, .minor = 0 },
};

static void vnet_get_drvinfo(struct net_device *dev,
			     struct ethtool_drvinfo *info)
{
62 63
	strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
	strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
64 65 66 67 68
}

static u32 vnet_get_msglevel(struct net_device *dev)
{
	struct vnet *vp = netdev_priv(dev);
69

70 71 72 73 74 75
	return vp->msg_enable;
}

static void vnet_set_msglevel(struct net_device *dev, u32 value)
{
	struct vnet *vp = netdev_priv(dev);
76

77 78 79 80 81 82 83 84 85 86
	vp->msg_enable = value;
}

static const struct ethtool_ops vnet_ethtool_ops = {
	.get_drvinfo		= vnet_get_drvinfo,
	.get_msglevel		= vnet_get_msglevel,
	.set_msglevel		= vnet_set_msglevel,
	.get_link		= ethtool_op_get_link,
};

87 88 89
static LIST_HEAD(vnet_list);
static DEFINE_MUTEX(vnet_list_mutex);

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
static struct vnet_port *__tx_port_find(struct vnet *vp, struct sk_buff *skb)
{
	unsigned int hash = vnet_hashfn(skb->data);
	struct hlist_head *hp = &vp->port_hash[hash];
	struct vnet_port *port;

	hlist_for_each_entry_rcu(port, hp, hash) {
		if (!sunvnet_port_is_up_common(port))
			continue;
		if (ether_addr_equal(port->raddr, skb->data))
			return port;
	}
	list_for_each_entry_rcu(port, &vp->port_list, list) {
		if (!port->switch_port)
			continue;
		if (!sunvnet_port_is_up_common(port))
			continue;
		return port;
	}
	return NULL;
}

/* func arg to vnet_start_xmit_common() to get the proper tx port */
static struct vnet_port *vnet_tx_port_find(struct sk_buff *skb,
					   struct net_device *dev)
{
	struct vnet *vp = netdev_priv(dev);

	return __tx_port_find(vp, skb);
}

static u16 vnet_select_queue(struct net_device *dev, struct sk_buff *skb,
			     void *accel_priv, select_queue_fallback_t fallback)
{
	struct vnet *vp = netdev_priv(dev);
	struct vnet_port *port = __tx_port_find(vp, skb);

	if (!port)
		return 0;

	return port->q_index;
}

/* Wrappers to common functions */
static int vnet_start_xmit(struct sk_buff *skb, struct net_device *dev)
{
	return sunvnet_start_xmit_common(skb, dev, vnet_tx_port_find);
}

static void vnet_set_rx_mode(struct net_device *dev)
{
	struct vnet *vp = netdev_priv(dev);

	return sunvnet_set_rx_mode_common(dev, vp);
}

#ifdef CONFIG_NET_POLL_CONTROLLER
static void vnet_poll_controller(struct net_device *dev)
{
	struct vnet *vp = netdev_priv(dev);

	return sunvnet_poll_controller_common(dev, vp);
}
#endif

155
static const struct net_device_ops vnet_ops = {
156 157
	.ndo_open		= sunvnet_open_common,
	.ndo_stop		= sunvnet_close_common,
158
	.ndo_set_rx_mode	= vnet_set_rx_mode,
159
	.ndo_set_mac_address	= sunvnet_set_mac_addr_common,
160
	.ndo_validate_addr	= eth_validate_addr,
161 162
	.ndo_tx_timeout		= sunvnet_tx_timeout_common,
	.ndo_change_mtu		= sunvnet_change_mtu_common,
163 164
	.ndo_start_xmit		= vnet_start_xmit,
	.ndo_select_queue	= vnet_select_queue,
S
Sowmini Varadhan 已提交
165
#ifdef CONFIG_NET_POLL_CONTROLLER
166
	.ndo_poll_controller	= vnet_poll_controller,
S
Sowmini Varadhan 已提交
167
#endif
168 169
};

170 171
static struct vnet *vnet_new(const u64 *local_mac,
			     struct vio_dev *vdev)
172 173 174 175 176
{
	struct net_device *dev;
	struct vnet *vp;
	int err, i;

177
	dev = alloc_etherdev_mqs(sizeof(*vp), VNET_MAX_TXQS, 1);
178
	if (!dev)
179
		return ERR_PTR(-ENOMEM);
180 181
	dev->needed_headroom = VNET_PACKET_SKIP + 8;
	dev->needed_tailroom = 8;
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

	for (i = 0; i < ETH_ALEN; i++)
		dev->dev_addr[i] = (*local_mac >> (5 - i) * 8) & 0xff;

	vp = netdev_priv(dev);

	spin_lock_init(&vp->lock);
	vp->dev = dev;

	INIT_LIST_HEAD(&vp->port_list);
	for (i = 0; i < VNET_PORT_HASH_SIZE; i++)
		INIT_HLIST_HEAD(&vp->port_hash[i]);
	INIT_LIST_HEAD(&vp->list);
	vp->local_mac = *local_mac;

197
	dev->netdev_ops = &vnet_ops;
198 199 200
	dev->ethtool_ops = &vnet_ethtool_ops;
	dev->watchdog_timeo = VNET_TX_TIMEOUT;

D
David L Stevens 已提交
201
	dev->hw_features = NETIF_F_TSO | NETIF_F_GSO | NETIF_F_GSO_SOFTWARE |
D
David L Stevens 已提交
202
			   NETIF_F_HW_CSUM | NETIF_F_SG;
203 204
	dev->features = dev->hw_features;

205 206
	SET_NETDEV_DEV(dev, &vdev->dev);

207 208
	err = register_netdev(dev);
	if (err) {
209
		pr_err("Cannot register net device, aborting\n");
210 211 212
		goto err_out_free_dev;
	}

213
	netdev_info(dev, "Sun LDOM vnet %pM\n", dev->dev_addr);
214 215 216 217 218 219 220 221 222 223 224

	list_add(&vp->list, &vnet_list);

	return vp;

err_out_free_dev:
	free_netdev(dev);

	return ERR_PTR(err);
}

225 226
static struct vnet *vnet_find_or_create(const u64 *local_mac,
					struct vio_dev *vdev)
227 228 229 230 231 232 233 234 235 236 237 238
{
	struct vnet *iter, *vp;

	mutex_lock(&vnet_list_mutex);
	vp = NULL;
	list_for_each_entry(iter, &vnet_list, list) {
		if (iter->local_mac == *local_mac) {
			vp = iter;
			break;
		}
	}
	if (!vp)
239
		vp = vnet_new(local_mac, vdev);
240 241 242 243 244
	mutex_unlock(&vnet_list_mutex);

	return vp;
}

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
static void vnet_cleanup(void)
{
	struct vnet *vp;
	struct net_device *dev;

	mutex_lock(&vnet_list_mutex);
	while (!list_empty(&vnet_list)) {
		vp = list_first_entry(&vnet_list, struct vnet, list);
		list_del(&vp->list);
		dev = vp->dev;
		/* vio_unregister_driver() should have cleaned up port_list */
		BUG_ON(!list_empty(&vp->port_list));
		unregister_netdev(dev);
		free_netdev(dev);
	}
	mutex_unlock(&vnet_list_mutex);
}

263 264
static const char *local_mac_prop = "local-mac-address";

B
Bill Pemberton 已提交
265
static struct vnet *vnet_find_parent(struct mdesc_handle *hp,
266 267
				     u64 port_node,
				     struct vio_dev *vdev)
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
{
	const u64 *local_mac = NULL;
	u64 a;

	mdesc_for_each_arc(a, hp, port_node, MDESC_ARC_TYPE_BACK) {
		u64 target = mdesc_arc_target(hp, a);
		const char *name;

		name = mdesc_get_property(hp, target, "name", NULL);
		if (!name || strcmp(name, "network"))
			continue;

		local_mac = mdesc_get_property(hp, target,
					       local_mac_prop, NULL);
		if (local_mac)
			break;
	}
	if (!local_mac)
		return ERR_PTR(-ENODEV);

288
	return vnet_find_or_create(local_mac, vdev);
289 290
}

291
static struct ldc_channel_config vnet_ldc_cfg = {
292
	.event		= sunvnet_event_common,
293 294 295 296 297
	.mtu		= 64,
	.mode		= LDC_MODE_UNRELIABLE,
};

static struct vio_driver_ops vnet_vio_ops = {
298 299 300
	.send_attr		= sunvnet_send_attr_common,
	.handle_attr		= sunvnet_handle_attr_common,
	.handshake_complete	= sunvnet_handshake_complete_common,
301 302
};

B
Bill Pemberton 已提交
303
static void print_version(void)
304
{
305
	printk_once(KERN_INFO "%s", version);
306 307
}

308 309
const char *remote_macaddr_prop = "remote-mac-address";

310
static int vnet_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
311
{
312
	struct mdesc_handle *hp;
313 314 315 316 317 318
	struct vnet_port *port;
	unsigned long flags;
	struct vnet *vp;
	const u64 *rmac;
	int len, i, err, switch_port;

319
	print_version();
320

321 322
	hp = mdesc_grab();

323
	vp = vnet_find_parent(hp, vdev->mp, vdev);
324
	if (IS_ERR(vp)) {
325
		pr_err("Cannot find port parent vnet\n");
326 327 328 329
		err = PTR_ERR(vp);
		goto err_out_put_mdesc;
	}

330 331
	rmac = mdesc_get_property(hp, vdev->mp, remote_macaddr_prop, &len);
	err = -ENODEV;
332
	if (!rmac) {
333
		pr_err("Port lacks %s property\n", remote_macaddr_prop);
334
		goto err_out_put_mdesc;
335 336 337
	}

	port = kzalloc(sizeof(*port), GFP_KERNEL);
338
	err = -ENOMEM;
339
	if (!port)
340
		goto err_out_put_mdesc;
341 342 343 344 345 346

	for (i = 0; i < ETH_ALEN; i++)
		port->raddr[i] = (*rmac >> (5 - i) * 8) & 0xff;

	port->vp = vp;

347
	err = vio_driver_init(&port->vio, vdev, VDEV_NETWORK,
348 349 350 351 352 353 354 355 356
			      vnet_versions, ARRAY_SIZE(vnet_versions),
			      &vnet_vio_ops, vp->dev->name);
	if (err)
		goto err_out_free_port;

	err = vio_ldc_alloc(&port->vio, &vnet_ldc_cfg, port);
	if (err)
		goto err_out_free_port;

357 358
	netif_napi_add(port->vp->dev, &port->napi, sunvnet_poll_common,
		       NAPI_POLL_WEIGHT);
S
Sowmini Varadhan 已提交
359

360 361 362 363
	INIT_HLIST_NODE(&port->hash);
	INIT_LIST_HEAD(&port->list);

	switch_port = 0;
364
	if (mdesc_get_property(hp, vdev->mp, "switch-port", NULL))
365
		switch_port = 1;
366
	port->switch_port = switch_port;
D
David L Stevens 已提交
367 368
	port->tso = true;
	port->tsolen = 0;
369 370 371

	spin_lock_irqsave(&vp->lock, flags);
	if (switch_port)
372
		list_add_rcu(&port->list, &vp->port_list);
373
	else
374 375 376
		list_add_tail_rcu(&port->list, &vp->port_list);
	hlist_add_head_rcu(&port->hash,
			   &vp->port_hash[vnet_hashfn(port->raddr)]);
377
	sunvnet_port_add_txq_common(port);
378 379 380 381
	spin_unlock_irqrestore(&vp->lock, flags);

	dev_set_drvdata(&vdev->dev, port);

382 383
	pr_info("%s: PORT ( remote-mac %pM%s )\n",
		vp->dev->name, port->raddr, switch_port ? " switch-port" : "");
384

385
	setup_timer(&port->clean_timer, sunvnet_clean_timer_expire_common,
386 387
		    (unsigned long)port);

S
Sowmini Varadhan 已提交
388
	napi_enable(&port->napi);
389 390
	vio_port_up(&port->vio);

391 392
	mdesc_release(hp);

393 394 395 396 397
	return 0;

err_out_free_port:
	kfree(port);

398 399
err_out_put_mdesc:
	mdesc_release(hp);
400 401 402 403 404 405 406 407 408 409
	return err;
}

static int vnet_port_remove(struct vio_dev *vdev)
{
	struct vnet_port *port = dev_get_drvdata(&vdev->dev);

	if (port) {
		del_timer_sync(&port->vio.timer);

S
Sowmini Varadhan 已提交
410
		napi_disable(&port->napi);
411

412 413 414 415 416
		list_del_rcu(&port->list);
		hlist_del_rcu(&port->hash);

		synchronize_rcu();
		del_timer_sync(&port->clean_timer);
417
		sunvnet_port_rm_txq_common(port);
S
Sowmini Varadhan 已提交
418
		netif_napi_del(&port->napi);
419
		sunvnet_port_free_tx_bufs_common(port);
420 421 422 423 424 425 426 427 428
		vio_ldc_free(&port->vio);

		dev_set_drvdata(&vdev->dev, NULL);

		kfree(port);
	}
	return 0;
}

429
static const struct vio_device_id vnet_port_match[] = {
430 431 432 433 434
	{
		.type = "vnet-port",
	},
	{},
};
435
MODULE_DEVICE_TABLE(vio, vnet_port_match);
436 437 438 439 440

static struct vio_driver vnet_port_driver = {
	.id_table	= vnet_port_match,
	.probe		= vnet_port_probe,
	.remove		= vnet_port_remove,
441
	.name		= "vnet_port",
442 443 444 445
};

static int __init vnet_init(void)
{
446
	return vio_register_driver(&vnet_port_driver);
447 448 449 450 451
}

static void __exit vnet_exit(void)
{
	vio_unregister_driver(&vnet_port_driver);
452
	vnet_cleanup();
453 454 455 456
}

module_init(vnet_init);
module_exit(vnet_exit);