netvsc_drv.c 14.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (c) 2009, Microsoft Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307 USA.
 *
 * Authors:
18
 *   Haiyang Zhang <haiyangz@microsoft.com>
19 20
 *   Hank Janssen  <hjanssen@microsoft.com>
 */
21 22
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

23 24 25 26 27 28 29 30 31 32 33
#include <linux/init.h>
#include <linux/module.h>
#include <linux/highmem.h>
#include <linux/device.h>
#include <linux/io.h>
#include <linux/delay.h>
#include <linux/netdevice.h>
#include <linux/inetdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/in.h>
34
#include <linux/slab.h>
35 36
#include <linux/dmi.h>
#include <linux/pci.h>
37 38 39 40
#include <net/arp.h>
#include <net/route.h>
#include <net/sock.h>
#include <net/pkt_sched.h>
K
K. Y. Srinivasan 已提交
41
#include "hv_api.h"
G
Greg Kroah-Hartman 已提交
42
#include "logging.h"
43
#include "version_info.h"
G
Greg Kroah-Hartman 已提交
44
#include "vmbus.h"
45
#include "netvsc_api.h"
46 47

struct net_device_context {
48
	/* point back to our device context */
49
	struct hv_device *device_ctx;
50
	unsigned long avail;
51
	struct work_struct work;
52 53 54
};


55 56 57 58
#define PACKET_PAGES_LOWATER  8
/* Need this many pages to handle worst case fragmented packet */
#define PACKET_PAGES_HIWATER  (MAX_SKB_FRAGS + 2)

59
static int ring_size = 128;
S
Stephen Hemminger 已提交
60 61
module_param(ring_size, int, S_IRUGO);
MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
62

63
/* The one and only one */
64
static struct  netvsc_driver g_netvsc_drv;
65

66 67 68
/* no-op so the netdev core doesn't return -EINVAL when modifying the the
 * multicast address list in SIOCADDMULTI. hv is setup to get all multicast
 * when it calls RndisFilterOnOpen() */
69
static void netvsc_set_multicast_list(struct net_device *net)
70 71 72 73 74 75
{
}

static int netvsc_open(struct net_device *net)
{
	struct net_device_context *net_device_ctx = netdev_priv(net);
76
	struct hv_device *device_obj = net_device_ctx->device_ctx;
77
	int ret = 0;
78

79
	if (netif_carrier_ok(net)) {
80
		/* Open up the device */
81
		ret = rndis_filter_open(device_obj);
82
		if (ret != 0) {
83 84
			netdev_err(net, "unable to open device (ret %d).\n",
				   ret);
85 86 87 88
			return ret;
		}

		netif_start_queue(net);
89
	} else {
90
		netdev_err(net, "unable to open device...link is down.\n");
91 92 93 94 95 96 97 98
	}

	return ret;
}

static int netvsc_close(struct net_device *net)
{
	struct net_device_context *net_device_ctx = netdev_priv(net);
99
	struct hv_device *device_obj = net_device_ctx->device_ctx;
100
	int ret;
101 102 103

	netif_stop_queue(net);

104
	ret = rndis_filter_close(device_obj);
105
	if (ret != 0)
106
		netdev_err(net, "unable to close device (ret %d).\n", ret);
107 108 109 110 111 112

	return ret;
}

static void netvsc_xmit_completion(void *context)
{
113
	struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
114
	struct sk_buff *skb = (struct sk_buff *)
115
		(unsigned long)packet->completion.send.send_completion_tid;
116 117 118

	kfree(packet);

119
	if (skb) {
120
		struct net_device *net = skb->dev;
121 122
		struct net_device_context *net_device_ctx = netdev_priv(net);
		unsigned int num_pages = skb_shinfo(skb)->nr_frags + 2;
123

124
		dev_kfree_skb_any(skb);
125

126 127
		net_device_ctx->avail += num_pages;
		if (net_device_ctx->avail >= PACKET_PAGES_HIWATER)
128
 			netif_wake_queue(net);
129 130 131
	}
}

132
static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
133 134
{
	struct net_device_context *net_device_ctx = netdev_priv(net);
135 136
	struct netvsc_driver *net_drv_obj =
		drv_to_netvscdrv(net_device_ctx->device_ctx->device.driver);
137
	struct hv_netvsc_packet *packet;
138
	int ret;
139
	unsigned int i, num_pages;
140

141 142
	/* Add 1 for skb->data and additional one for RNDIS */
	num_pages = skb_shinfo(skb)->nr_frags + 1 + 1;
143 144
	if (num_pages > net_device_ctx->avail)
		return NETDEV_TX_BUSY;
145

146
	/* Allocate a netvsc packet based on # of frags. */
147
	packet = kzalloc(sizeof(struct hv_netvsc_packet) +
148
			 (num_pages * sizeof(struct hv_page_buffer)) +
149
			 net_drv_obj->req_ext_size, GFP_ATOMIC);
150
	if (!packet) {
151
		/* out of memory, silently drop packet */
152
		netdev_err(net, "unable to allocate hv_netvsc_packet\n");
153 154 155 156

		dev_kfree_skb(skb);
		net->stats.tx_dropped++;
		return NETDEV_TX_OK;
157 158
	}

159
	packet->extension = (void *)(unsigned long)packet +
160
				sizeof(struct hv_netvsc_packet) +
161
				    (num_pages * sizeof(struct hv_page_buffer));
162

163
	/* Setup the rndis header */
164
	packet->page_buf_cnt = num_pages;
165

166 167
	/* TODO: Flush all write buffers/ memory fence ??? */
	/* wmb(); */
168

169
	/* Initialize it from the skb */
170
	packet->total_data_buflen	= skb->len;
171

172
	/* Start filling in the page buffers starting after RNDIS buffer. */
173 174
	packet->page_buf[1].pfn = virt_to_phys(skb->data) >> PAGE_SHIFT;
	packet->page_buf[1].offset
175
		= (unsigned long)skb->data & (PAGE_SIZE - 1);
176
	packet->page_buf[1].len = skb_headlen(skb);
177 178 179 180 181

	/* Additional fragments are after SKB data */
	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
		skb_frag_t *f = &skb_shinfo(skb)->frags[i];

182 183 184
		packet->page_buf[i+2].pfn = page_to_pfn(f->page);
		packet->page_buf[i+2].offset = f->page_offset;
		packet->page_buf[i+2].len = f->size;
185 186
	}

187
	/* Set the completion routine */
188 189 190
	packet->completion.send.send_completion = netvsc_xmit_completion;
	packet->completion.send.send_completion_ctx = packet;
	packet->completion.send.send_completion_tid = (unsigned long)skb;
191

192
	ret = net_drv_obj->send(net_device_ctx->device_ctx,
193 194
				  packet);
	if (ret == 0) {
195 196
		net->stats.tx_bytes += skb->len;
		net->stats.tx_packets++;
197

198 199
		net_device_ctx->avail -= num_pages;
		if (net_device_ctx->avail < PACKET_PAGES_LOWATER)
200 201 202
			netif_stop_queue(net);
	} else {
		/* we are shutting down or bus overloaded, just drop packet */
203
		net->stats.tx_dropped++;
204
		netvsc_xmit_completion(packet);
205 206
	}

207
	return NETDEV_TX_OK;
208 209
}

210
/*
211 212 213 214
 * netvsc_linkstatus_callback - Link up/down notification
 */
static void netvsc_linkstatus_callback(struct hv_device *device_obj,
				       unsigned int status)
215
{
216
	struct net_device *net = dev_get_drvdata(&device_obj->device);
217
	struct net_device_context *ndev_ctx;
218

219
	if (!net) {
220 221
		netdev_err(net, "got link status but net device "
				"not initialized yet\n");
222 223 224
		return;
	}

225
	if (status == 1) {
226 227
		netif_carrier_on(net);
		netif_wake_queue(net);
228
		netif_notify_peers(net);
229 230
		ndev_ctx = netdev_priv(net);
		schedule_work(&ndev_ctx->work);
231
	} else {
232 233 234 235 236
		netif_carrier_off(net);
		netif_stop_queue(net);
	}
}

237 238 239
/*
 * netvsc_recv_callback -  Callback when we receive a packet from the
 * "wire" on the specified device.
240 241 242
 */
static int netvsc_recv_callback(struct hv_device *device_obj,
				struct hv_netvsc_packet *packet)
243
{
244
	struct net_device *net = dev_get_drvdata(&device_obj->device);
245 246
	struct sk_buff *skb;
	void *data;
247
	int i;
248 249
	unsigned long flags;

250
	if (!net) {
251 252
		netdev_err(net, "got receive callback but net device"
			" not initialized yet\n");
253 254 255
		return 0;
	}

256
	/* Allocate a skb - TODO direct I/O to pages? */
257
	skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
258 259 260 261
	if (unlikely(!skb)) {
		++net->stats.rx_dropped;
		return 0;
	}
262

263
	/* for kmap_atomic */
264 265
	local_irq_save(flags);

266 267 268 269
	/*
	 * Copy to skb. This copy is needed here since the memory pointed by
	 * hv_netvsc_packet cannot be deallocated
	 */
270
	for (i = 0; i < packet->page_buf_cnt; i++) {
271
		data = kmap_atomic(pfn_to_page(packet->page_buf[i].pfn),
272 273
					       KM_IRQ1);
		data = (void *)(unsigned long)data +
274
				packet->page_buf[i].offset;
275

276 277
		memcpy(skb_put(skb, packet->page_buf[i].len), data,
		       packet->page_buf[i].len);
278 279

		kunmap_atomic((void *)((unsigned long)data -
280
				       packet->page_buf[i].offset), KM_IRQ1);
281 282 283 284 285 286 287
	}

	local_irq_restore(flags);

	skb->protocol = eth_type_trans(skb, net);
	skb->ip_summed = CHECKSUM_NONE;

288 289 290
	net->stats.rx_packets++;
	net->stats.rx_bytes += skb->len;

291 292
	/*
	 * Pass the skb back up. Network stack will deallocate the skb when it
293 294
	 * is done.
	 * TODO - use NAPI?
295
	 */
296
	netif_rx(skb);
297 298 299 300

	return 0;
}

301 302 303 304 305 306 307 308 309 310 311 312 313
static void netvsc_get_drvinfo(struct net_device *net,
			       struct ethtool_drvinfo *info)
{
	strcpy(info->driver, "hv_netvsc");
	strcpy(info->version, HV_DRV_VERSION);
	strcpy(info->fw_version, "N/A");
}

static const struct ethtool_ops ethtool_ops = {
	.get_drvinfo	= netvsc_get_drvinfo,
	.get_link	= ethtool_op_get_link,
};

314 315 316 317 318
static const struct net_device_ops device_ops = {
	.ndo_open =			netvsc_open,
	.ndo_stop =			netvsc_close,
	.ndo_start_xmit =		netvsc_start_xmit,
	.ndo_set_multicast_list =	netvsc_set_multicast_list,
319 320 321
	.ndo_change_mtu =		eth_change_mtu,
	.ndo_validate_addr =		eth_validate_addr,
	.ndo_set_mac_address =		eth_mac_addr,
322 323
};

324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
/*
 * Send GARP packet to network peers after migrations.
 * After Quick Migration, the network is not immediately operational in the
 * current context when receiving RNDIS_STATUS_MEDIA_CONNECT event. So, add
 * another netif_notify_peers() into a scheduled work, otherwise GARP packet
 * will not be sent after quick migration, and cause network disconnection.
 */
static void netvsc_send_garp(struct work_struct *w)
{
	struct net_device_context *ndev_ctx;
	struct net_device *net;

	msleep(20);
	ndev_ctx = container_of(w, struct net_device_context, work);
	net = dev_get_drvdata(&ndev_ctx->device_ctx->device);
	netif_notify_peers(net);
}


343
static int netvsc_probe(struct hv_device *dev)
344
{
345
	struct netvsc_driver *net_drv_obj =
346
		drv_to_netvscdrv(dev->device.driver);
347 348 349 350 351
	struct net_device *net = NULL;
	struct net_device_context *net_device_ctx;
	struct netvsc_device_info device_info;
	int ret;

352
	if (!net_drv_obj->base.dev_add)
353 354
		return -1;

355
	net = alloc_etherdev(sizeof(struct net_device_context));
356 357 358 359 360 361 362
	if (!net)
		return -1;

	/* Set initial state */
	netif_carrier_off(net);

	net_device_ctx = netdev_priv(net);
363
	net_device_ctx->device_ctx = dev;
364
	net_device_ctx->avail = ring_size;
365
	dev_set_drvdata(&dev->device, net);
366
	INIT_WORK(&net_device_ctx->work, netvsc_send_garp);
367 368

	/* Notify the netvsc driver of the new device */
369
	ret = net_drv_obj->base.dev_add(dev, &device_info);
370 371
	if (ret != 0) {
		free_netdev(net);
372
		dev_set_drvdata(&dev->device, NULL);
373

374
		netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
375 376 377 378 379 380 381 382 383 384 385 386
		return ret;
	}

	/*
	 * If carrier is still off ie we did not get a link status callback,
	 * update it if necessary
	 */
	/*
	 * FIXME: We should use a atomic or test/set instead to avoid getting
	 * out of sync with the device's link status
	 */
	if (!netif_carrier_ok(net))
387
		if (!device_info.link_state)
388 389
			netif_carrier_on(net);

390
	memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
391 392 393

	net->netdev_ops = &device_ops;

394
	/* TODO: Add GSO and Checksum offload */
395
	net->hw_features = NETIF_F_SG;
396 397
	net->features = NETIF_F_SG;

398
	SET_ETHTOOL_OPS(net, &ethtool_ops);
399
	SET_NETDEV_DEV(net, &dev->device);
400 401 402 403

	ret = register_netdev(net);
	if (ret != 0) {
		/* Remove the device and release the resource */
404
		net_drv_obj->base.dev_rm(dev);
405 406 407 408 409 410 411 412
		free_netdev(net);
	}

	return ret;
}

static int netvsc_remove(struct device *device)
{
413 414
	struct netvsc_driver *net_drv_obj =
		drv_to_netvscdrv(device->driver);
415 416
	struct hv_device *device_obj = device_to_hv_device(device);
	struct net_device *net = dev_get_drvdata(&device_obj->device);
417 418 419
	int ret;

	if (net == NULL) {
420
		dev_err(device, "No net device to remove\n");
421 422 423
		return 0;
	}

424
	if (!net_drv_obj->base.dev_rm)
425 426 427 428 429 430 431 432 433 434 435 436
		return -1;

	/* Stop outbound asap */
	netif_stop_queue(net);
	/* netif_carrier_off(net); */

	unregister_netdev(net);

	/*
	 * Call to the vsc driver to let it know that the device is being
	 * removed
	 */
437
	ret = net_drv_obj->base.dev_rm(device_obj);
438 439
	if (ret != 0) {
		/* TODO: */
440
		netdev_err(net, "unable to remove vsc device (ret %d)\n", ret);
441 442 443 444 445 446
	}

	free_netdev(net);
	return ret;
}

447 448 449
static int netvsc_drv_exit_cb(struct device *dev, void *data)
{
	struct device **curr = (struct device **)data;
450

451
	*curr = dev;
452 453
	/* stop iterating */
	return 1;
454 455
}

456
static void netvsc_drv_exit(void)
457
{
458 459
	struct netvsc_driver *netvsc_drv_obj = &g_netvsc_drv;
	struct hv_driver *drv = &g_netvsc_drv.base;
460
	struct device *current_dev;
461
	int ret;
462

463
	while (1) {
464 465
		current_dev = NULL;

466
		/* Get the device */
467
		ret = driver_for_each_device(&drv->driver, NULL,
468
					     &current_dev, netvsc_drv_exit_cb);
469

470 471 472
		if (current_dev == NULL)
			break;

473
		/* Initiate removal from the top-down */
474 475
		dev_err(current_dev, "unregistering device (%s)...\n",
			dev_name(current_dev));
476 477 478 479

		device_unregister(current_dev);
	}

480 481
	if (netvsc_drv_obj->base.cleanup)
		netvsc_drv_obj->base.cleanup(&netvsc_drv_obj->base);
482

483
	vmbus_child_driver_unregister(&drv->driver);
484 485 486 487

	return;
}

488
static int netvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
489
{
490 491
	struct netvsc_driver *net_drv_obj = &g_netvsc_drv;
	struct hv_driver *drv = &g_netvsc_drv.base;
492 493
	int ret;

494 495 496
	net_drv_obj->ring_buf_size = ring_size * PAGE_SIZE;
	net_drv_obj->recv_cb = netvsc_recv_callback;
	net_drv_obj->link_status_change = netvsc_linkstatus_callback;
497 498

	/* Callback to client driver to complete the initialization */
499
	drv_init(&net_drv_obj->base);
500

501
	drv->driver.name = net_drv_obj->base.name;
502

503
	drv->probe = netvsc_probe;
504
	drv->driver.remove = netvsc_remove;
505 506

	/* The driver belongs to vmbus */
507
	ret = vmbus_child_driver_register(&drv->driver);
508 509 510 511

	return ret;
}

512 513 514 515 516 517 518 519 520 521 522 523 524 525
static const struct dmi_system_id __initconst
hv_netvsc_dmi_table[] __maybe_unused  = {
	{
		.ident = "Hyper-V",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Virtual Machine"),
			DMI_MATCH(DMI_BOARD_NAME, "Virtual Machine"),
		},
	},
	{ },
};
MODULE_DEVICE_TABLE(dmi, hv_netvsc_dmi_table);

526 527
static int __init netvsc_init(void)
{
528
	pr_info("initializing....");
529

530 531 532
	if (!dmi_check_system(hv_netvsc_dmi_table))
		return -ENODEV;

533
	return netvsc_drv_init(netvsc_initialize);
534 535 536 537 538 539 540
}

static void __exit netvsc_exit(void)
{
	netvsc_drv_exit();
}

541 542 543 544 545 546 547
static const struct pci_device_id __initconst
hv_netvsc_pci_table[] __maybe_unused = {
	{ PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
	{ 0 }
};
MODULE_DEVICE_TABLE(pci, hv_netvsc_pci_table);

548 549
MODULE_LICENSE("GPL");
MODULE_VERSION(HV_DRV_VERSION);
550
MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
551 552 553

module_init(netvsc_init);
module_exit(netvsc_exit);