netvsc.c 22.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * 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
14
 * this program; if not, see <http://www.gnu.org/licenses/>.
15 16
 *
 * Authors:
17
 *   Haiyang Zhang <haiyangz@microsoft.com>
18 19
 *   Hank Janssen  <hjanssen@microsoft.com>
 */
20 21
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

22
#include <linux/kernel.h>
23 24
#include <linux/sched.h>
#include <linux/wait.h>
25
#include <linux/mm.h>
26
#include <linux/delay.h>
27
#include <linux/io.h>
28
#include <linux/slab.h>
29
#include <linux/netdevice.h>
30
#include <linux/if_ether.h>
31

32
#include "hyperv_net.h"
33 34


35
static struct netvsc_device *alloc_net_device(struct hv_device *device)
36
{
37
	struct netvsc_device *net_device;
38
	struct net_device *ndev = hv_get_drvdata(device);
39

40 41
	net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
	if (!net_device)
42 43
		return NULL;

44
	init_waitqueue_head(&net_device->wait_drain);
45
	net_device->start_remove = false;
46
	net_device->destroy = false;
47
	net_device->dev = device;
48
	net_device->ndev = ndev;
49

50
	hv_set_drvdata(device, net_device);
51
	return net_device;
52 53
}

54
static struct netvsc_device *get_outbound_net_device(struct hv_device *device)
55
{
56
	struct netvsc_device *net_device;
57

58
	net_device = hv_get_drvdata(device);
59
	if (net_device && net_device->destroy)
60
		net_device = NULL;
61

62
	return net_device;
63 64
}

65
static struct netvsc_device *get_inbound_net_device(struct hv_device *device)
66
{
67
	struct netvsc_device *net_device;
68

69
	net_device = hv_get_drvdata(device);
70 71 72 73 74 75

	if (!net_device)
		goto get_in_err;

	if (net_device->destroy &&
		atomic_read(&net_device->num_outstanding_sends) == 0)
76
		net_device = NULL;
77

78
get_in_err:
79
	return net_device;
80 81 82
}


83 84 85 86
static int netvsc_destroy_recv_buf(struct netvsc_device *net_device)
{
	struct nvsp_message *revoke_packet;
	int ret = 0;
87
	struct net_device *ndev = net_device->ndev;
88 89 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

	/*
	 * If we got a section count, it means we received a
	 * SendReceiveBufferComplete msg (ie sent
	 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
	 * to send a revoke msg here
	 */
	if (net_device->recv_section_cnt) {
		/* Send the revoke receive buffer */
		revoke_packet = &net_device->revoke_packet;
		memset(revoke_packet, 0, sizeof(struct nvsp_message));

		revoke_packet->hdr.msg_type =
			NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
		revoke_packet->msg.v1_msg.
		revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;

		ret = vmbus_sendpacket(net_device->dev->channel,
				       revoke_packet,
				       sizeof(struct nvsp_message),
				       (unsigned long)revoke_packet,
				       VM_PKT_DATA_INBAND, 0);
		/*
		 * If we failed here, we might as well return and
		 * have a leak rather than continue and a bugchk
		 */
		if (ret != 0) {
115
			netdev_err(ndev, "unable to send "
116
				"revoke receive buffer to netvsp\n");
117
			return ret;
118 119 120 121 122 123 124 125 126 127 128 129
		}
	}

	/* Teardown the gpadl on the vsp end */
	if (net_device->recv_buf_gpadl_handle) {
		ret = vmbus_teardown_gpadl(net_device->dev->channel,
			   net_device->recv_buf_gpadl_handle);

		/* If we failed here, we might as well return and have a leak
		 * rather than continue and a bugchk
		 */
		if (ret != 0) {
130
			netdev_err(ndev,
131
				   "unable to teardown receive buffer's gpadl\n");
132
			return ret;
133 134 135 136 137 138
		}
		net_device->recv_buf_gpadl_handle = 0;
	}

	if (net_device->recv_buf) {
		/* Free up the receive buffer */
139
		vfree(net_device->recv_buf);
140 141 142 143 144 145 146 147 148 149 150 151
		net_device->recv_buf = NULL;
	}

	if (net_device->recv_section) {
		net_device->recv_section_cnt = 0;
		kfree(net_device->recv_section);
		net_device->recv_section = NULL;
	}

	return ret;
}

152
static int netvsc_init_recv_buf(struct hv_device *device)
153
{
154
	int ret = 0;
155
	int t;
156 157
	struct netvsc_device *net_device;
	struct nvsp_message *init_packet;
158
	struct net_device *ndev;
159

160
	net_device = get_outbound_net_device(device);
161
	if (!net_device)
162
		return -ENODEV;
163
	ndev = net_device->ndev;
164

165
	net_device->recv_buf = vzalloc(net_device->recv_buf_size);
166
	if (!net_device->recv_buf) {
167
		netdev_err(ndev, "unable to allocate receive "
168
			"buffer of size %d\n", net_device->recv_buf_size);
169
		ret = -ENOMEM;
170
		goto cleanup;
171 172
	}

173 174 175 176 177
	/*
	 * Establish the gpadl handle for this buffer on this
	 * channel.  Note: This call uses the vmbus connection rather
	 * than the channel to establish the gpadl handle.
	 */
178 179 180
	ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
				    net_device->recv_buf_size,
				    &net_device->recv_buf_gpadl_handle);
181
	if (ret != 0) {
182
		netdev_err(ndev,
183
			"unable to establish receive buffer's gpadl\n");
184
		goto cleanup;
185 186 187
	}


188
	/* Notify the NetVsp of the gpadl handle */
189
	init_packet = &net_device->channel_init_pkt;
190

191
	memset(init_packet, 0, sizeof(struct nvsp_message));
192

193 194 195 196 197
	init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
	init_packet->msg.v1_msg.send_recv_buf.
		gpadl_handle = net_device->recv_buf_gpadl_handle;
	init_packet->msg.v1_msg.
		send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
198

199
	/* Send the gpadl notification request */
200
	ret = vmbus_sendpacket(device->channel, init_packet,
201
			       sizeof(struct nvsp_message),
202
			       (unsigned long)init_packet,
203
			       VM_PKT_DATA_INBAND,
204
			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
205
	if (ret != 0) {
206
		netdev_err(ndev,
207
			"unable to send receive buffer's gpadl to netvsp\n");
208
		goto cleanup;
209 210
	}

211
	t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
212
	BUG_ON(t == 0);
213

214

215
	/* Check the response */
216 217
	if (init_packet->msg.v1_msg.
	    send_recv_buf_complete.status != NVSP_STAT_SUCCESS) {
218
		netdev_err(ndev, "Unable to complete receive buffer "
219
			   "initialization with NetVsp - status %d\n",
220 221
			   init_packet->msg.v1_msg.
			   send_recv_buf_complete.status);
222
		ret = -EINVAL;
223
		goto cleanup;
224 225
	}

226
	/* Parse the response */
227

228 229
	net_device->recv_section_cnt = init_packet->msg.
		v1_msg.send_recv_buf_complete.num_sections;
230

231 232 233 234 235
	net_device->recv_section = kmemdup(
		init_packet->msg.v1_msg.send_recv_buf_complete.sections,
		net_device->recv_section_cnt *
		sizeof(struct nvsp_1_receive_buffer_section),
		GFP_KERNEL);
236
	if (net_device->recv_section == NULL) {
237
		ret = -EINVAL;
238
		goto cleanup;
239 240
	}

241 242 243 244
	/*
	 * For 1st release, there should only be 1 section that represents the
	 * entire receive buffer
	 */
245 246
	if (net_device->recv_section_cnt != 1 ||
	    net_device->recv_section->offset != 0) {
247
		ret = -EINVAL;
248
		goto cleanup;
249 250
	}

251
	goto exit;
252

253
cleanup:
254
	netvsc_destroy_recv_buf(net_device);
255

256
exit:
257 258 259 260
	return ret;
}


261 262 263 264 265
/* Negotiate NVSP protocol version */
static int negotiate_nvsp_ver(struct hv_device *device,
			      struct netvsc_device *net_device,
			      struct nvsp_message *init_packet,
			      u32 nvsp_ver)
266
{
267
	int ret, t;
268

269
	memset(init_packet, 0, sizeof(struct nvsp_message));
270
	init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
271 272
	init_packet->msg.init_msg.init.min_protocol_ver = nvsp_ver;
	init_packet->msg.init_msg.init.max_protocol_ver = nvsp_ver;
273

274
	/* Send the init request */
275
	ret = vmbus_sendpacket(device->channel, init_packet,
276
			       sizeof(struct nvsp_message),
277
			       (unsigned long)init_packet,
278
			       VM_PKT_DATA_INBAND,
279
			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
280

281
	if (ret != 0)
282
		return ret;
283

284
	t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
285

286 287
	if (t == 0)
		return -ETIMEDOUT;
288

289
	if (init_packet->msg.init_msg.init_complete.status !=
290 291
	    NVSP_STAT_SUCCESS)
		return -EINVAL;
292

293
	if (nvsp_ver == NVSP_PROTOCOL_VERSION_1)
294 295 296 297 298
		return 0;

	/* NVSPv2 only: Send NDIS config */
	memset(init_packet, 0, sizeof(struct nvsp_message));
	init_packet->hdr.msg_type = NVSP_MSG2_TYPE_SEND_NDIS_CONFIG;
299
	init_packet->msg.v2_msg.send_ndis_config.mtu = net_device->ndev->mtu;
300
	init_packet->msg.v2_msg.send_ndis_config.capability.ieee8021q = 1;
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316

	ret = vmbus_sendpacket(device->channel, init_packet,
				sizeof(struct nvsp_message),
				(unsigned long)init_packet,
				VM_PKT_DATA_INBAND, 0);

	return ret;
}

static int netvsc_connect_vsp(struct hv_device *device)
{
	int ret;
	struct netvsc_device *net_device;
	struct nvsp_message *init_packet;
	int ndis_version;
	struct net_device *ndev;
317 318 319
	u32 ver_list[] = { NVSP_PROTOCOL_VERSION_1, NVSP_PROTOCOL_VERSION_2,
		NVSP_PROTOCOL_VERSION_4, NVSP_PROTOCOL_VERSION_5 };
	int i, num_ver = 4; /* number of different NVSP versions */
320 321 322 323 324 325 326 327 328

	net_device = get_outbound_net_device(device);
	if (!net_device)
		return -ENODEV;
	ndev = net_device->ndev;

	init_packet = &net_device->channel_init_pkt;

	/* Negotiate the latest NVSP protocol supported */
329 330 331 332 333 334 335 336
	for (i = num_ver - 1; i >= 0; i--)
		if (negotiate_nvsp_ver(device, net_device, init_packet,
				       ver_list[i])  == 0) {
			net_device->nvsp_version = ver_list[i];
			break;
		}

	if (i < 0) {
337
		ret = -EPROTO;
338
		goto cleanup;
339
	}
340 341 342

	pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version);

343
	/* Send the ndis version */
344
	memset(init_packet, 0, sizeof(struct nvsp_message));
345

346
	if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_4)
347
		ndis_version = 0x00060001;
348 349
	else
		ndis_version = 0x0006001e;
350

351 352 353
	init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
	init_packet->msg.v1_msg.
		send_ndis_ver.ndis_major_ver =
354
				(ndis_version & 0xFFFF0000) >> 16;
355 356
	init_packet->msg.v1_msg.
		send_ndis_ver.ndis_minor_ver =
357
				ndis_version & 0xFFFF;
358

359
	/* Send the init request */
360
	ret = vmbus_sendpacket(device->channel, init_packet,
361 362 363
				sizeof(struct nvsp_message),
				(unsigned long)init_packet,
				VM_PKT_DATA_INBAND, 0);
364
	if (ret != 0)
365
		goto cleanup;
366 367

	/* Post the big receive buffer to NetVSP */
368 369 370 371 372
	if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
		net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
	else
		net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;

373
	ret = netvsc_init_recv_buf(device);
374

375
cleanup:
376 377 378
	return ret;
}

379
static void netvsc_disconnect_vsp(struct netvsc_device *net_device)
380
{
381
	netvsc_destroy_recv_buf(net_device);
382 383
}

384
/*
385
 * netvsc_device_remove - Callback when the root bus device is removed
386
 */
387
int netvsc_device_remove(struct hv_device *device)
388
{
389
	struct netvsc_device *net_device;
390
	unsigned long flags;
391

392
	net_device = hv_get_drvdata(device);
393

394
	netvsc_disconnect_vsp(net_device);
395

396
	/*
397 398 399 400 401
	 * Since we have already drained, we don't need to busy wait
	 * as was done in final_release_stor_device()
	 * Note that we cannot set the ext pointer to NULL until
	 * we have drained - to drain the outgoing packets, we need to
	 * allow incoming packets.
402
	 */
403 404

	spin_lock_irqsave(&device->channel->inbound_lock, flags);
405
	hv_set_drvdata(device, NULL);
406
	spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
407

408 409 410 411
	/*
	 * At this point, no one should be accessing net_device
	 * except in here
	 */
412
	dev_notice(&device->device, "net device safe to remove\n");
413

414
	/* Now, we can close the channel safely */
415
	vmbus_close(device->channel);
416

417
	/* Release all resources */
418 419 420
	if (net_device->sub_cb_buf)
		vfree(net_device->sub_cb_buf);

421
	kfree(net_device);
422
	return 0;
423 424
}

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442

#define RING_AVAIL_PERCENT_HIWATER 20
#define RING_AVAIL_PERCENT_LOWATER 10

/*
 * Get the percentage of available bytes to write in the ring.
 * The return value is in range from 0 to 100.
 */
static inline u32 hv_ringbuf_avail_percent(
		struct hv_ring_buffer_info *ring_info)
{
	u32 avail_read, avail_write;

	hv_get_ringbuffer_availbytes(ring_info, &avail_read, &avail_write);

	return avail_write * 100 / ring_info->ring_datasize;
}

443 444
static void netvsc_send_completion(struct netvsc_device *net_device,
				   struct hv_device *device,
445
				   struct vmpacket_descriptor *packet)
446
{
447 448
	struct nvsp_message *nvsp_packet;
	struct hv_netvsc_packet *nvsc_packet;
449
	struct net_device *ndev;
450

451
	ndev = net_device->ndev;
452

453
	nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
454
			(packet->offset8 << 3));
455

456 457 458 459
	if ((nvsp_packet->hdr.msg_type == NVSP_MSG_TYPE_INIT_COMPLETE) ||
	    (nvsp_packet->hdr.msg_type ==
	     NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE) ||
	    (nvsp_packet->hdr.msg_type ==
460 461 462
	     NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE) ||
	    (nvsp_packet->hdr.msg_type ==
	     NVSP_MSG5_TYPE_SUBCHANNEL)) {
463
		/* Copy the response back */
464
		memcpy(&net_device->channel_init_pkt, nvsp_packet,
465
		       sizeof(struct nvsp_message));
466
		complete(&net_device->channel_init_wait);
467 468
	} else if (nvsp_packet->hdr.msg_type ==
		   NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE) {
469
		int num_outstanding_sends;
470 471 472
		u16 q_idx = 0;
		struct vmbus_channel *channel = device->channel;
		int queue_sends;
473

474
		/* Get the send context */
475
		nvsc_packet = (struct hv_netvsc_packet *)(unsigned long)
476
			packet->trans_id;
477

478
		/* Notify the layer above us */
479 480 481
		if (nvsc_packet) {
			q_idx = nvsc_packet->q_idx;
			channel = nvsc_packet->channel;
482 483
			nvsc_packet->send_completion(nvsc_packet->
						     send_completion_ctx);
484
		}
485

486 487
		num_outstanding_sends =
			atomic_dec_return(&net_device->num_outstanding_sends);
488 489
		queue_sends = atomic_dec_return(&net_device->
						queue_sends[q_idx]);
490

491 492 493
		if (net_device->destroy && num_outstanding_sends == 0)
			wake_up(&net_device->wait_drain);

494 495 496 497 498 499
		if (netif_tx_queue_stopped(netdev_get_tx_queue(ndev, q_idx)) &&
		    !net_device->start_remove &&
		    (hv_ringbuf_avail_percent(&channel->outbound) >
		     RING_AVAIL_PERCENT_HIWATER || queue_sends < 1))
				netif_tx_wake_queue(netdev_get_tx_queue(
						    ndev, q_idx));
500
	} else {
501
		netdev_err(ndev, "Unknown send completion packet type- "
502
			   "%d received!!\n", nvsp_packet->hdr.msg_type);
503 504 505 506
	}

}

507
int netvsc_send(struct hv_device *device,
508
			struct hv_netvsc_packet *packet)
509
{
510
	struct netvsc_device *net_device;
511
	int ret = 0;
512
	struct nvsp_message sendMessage;
513
	struct net_device *ndev;
514
	struct vmbus_channel *out_channel = NULL;
515
	u64 req_id;
516

517
	net_device = get_outbound_net_device(device);
518
	if (!net_device)
519
		return -ENODEV;
520
	ndev = net_device->ndev;
521

522
	sendMessage.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
523
	if (packet->is_data_pkt) {
524
		/* 0 is RMC_DATA; */
525
		sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 0;
526 527
	} else {
		/* 1 is RMC_CONTROL; */
528
		sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 1;
529
	}
530

531
	/* Not using send buffer section */
532 533 534
	sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_index =
		0xFFFFFFFF;
	sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_size = 0;
535

536
	if (packet->send_completion)
537
		req_id = (ulong)packet;
538 539 540
	else
		req_id = 0;

541 542 543 544 545
	out_channel = net_device->chn_table[packet->q_idx];
	if (out_channel == NULL)
		out_channel = device->channel;
	packet->channel = out_channel;

546
	if (packet->page_buf_cnt) {
547
		ret = vmbus_sendpacket_pagebuffer(out_channel,
548 549
						  packet->page_buf,
						  packet->page_buf_cnt,
550 551
						  &sendMessage,
						  sizeof(struct nvsp_message),
552
						  req_id);
553
	} else {
554
		ret = vmbus_sendpacket(out_channel, &sendMessage,
555
				sizeof(struct nvsp_message),
556
				req_id,
557 558
				VM_PKT_DATA_INBAND,
				VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
559 560
	}

561 562
	if (ret == 0) {
		atomic_inc(&net_device->num_outstanding_sends);
563 564 565
		atomic_inc(&net_device->queue_sends[packet->q_idx]);

		if (hv_ringbuf_avail_percent(&out_channel->outbound) <
566
			RING_AVAIL_PERCENT_LOWATER) {
567 568 569
			netif_tx_stop_queue(netdev_get_tx_queue(
					    ndev, packet->q_idx));

570
			if (atomic_read(&net_device->
571 572 573
				queue_sends[packet->q_idx]) < 1)
				netif_tx_wake_queue(netdev_get_tx_queue(
						    ndev, packet->q_idx));
574
		}
575
	} else if (ret == -EAGAIN) {
576 577 578 579 580
		netif_tx_stop_queue(netdev_get_tx_queue(
				    ndev, packet->q_idx));
		if (atomic_read(&net_device->queue_sends[packet->q_idx]) < 1) {
			netif_tx_wake_queue(netdev_get_tx_queue(
					    ndev, packet->q_idx));
581 582
			ret = -ENOSPC;
		}
583
	} else {
584
		netdev_err(ndev, "Unable to send packet %p ret %d\n",
585
			   packet, ret);
586
	}
587 588 589 590

	return ret;
}

591
static void netvsc_send_recv_completion(struct hv_device *device,
592
					struct vmbus_channel *channel,
593
					struct netvsc_device *net_device,
594
					u64 transaction_id, u32 status)
595 596 597 598
{
	struct nvsp_message recvcompMessage;
	int retries = 0;
	int ret;
599 600 601
	struct net_device *ndev;

	ndev = net_device->ndev;
602 603 604 605

	recvcompMessage.hdr.msg_type =
				NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;

606
	recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status = status;
607 608 609

retry_send_cmplt:
	/* Send the completion */
610
	ret = vmbus_sendpacket(channel, &recvcompMessage,
611 612 613 614 615
			       sizeof(struct nvsp_message), transaction_id,
			       VM_PKT_COMP, 0);
	if (ret == 0) {
		/* success */
		/* no-op */
616
	} else if (ret == -EAGAIN) {
617 618
		/* no more room...wait a bit and attempt to retry 3 times */
		retries++;
619
		netdev_err(ndev, "unable to send receive completion pkt"
620
			" (tid %llx)...retrying %d\n", transaction_id, retries);
621 622 623 624 625

		if (retries < 4) {
			udelay(100);
			goto retry_send_cmplt;
		} else {
626
			netdev_err(ndev, "unable to send receive "
627
				"completion pkt (tid %llx)...give up retrying\n",
628 629 630
				transaction_id);
		}
	} else {
631
		netdev_err(ndev, "unable to send receive "
632
			"completion pkt - %llx\n", transaction_id);
633 634 635
	}
}

636
static void netvsc_receive(struct netvsc_device *net_device,
637
			struct vmbus_channel *channel,
638 639
			struct hv_device *device,
			struct vmpacket_descriptor *packet)
640
{
641 642
	struct vmtransfer_page_packet_header *vmxferpage_packet;
	struct nvsp_message *nvsp_packet;
643 644 645
	struct hv_netvsc_packet nv_pkt;
	struct hv_netvsc_packet *netvsc_packet = &nv_pkt;
	u32 status = NVSP_STAT_SUCCESS;
646 647
	int i;
	int count = 0;
648
	struct net_device *ndev;
649

650
	ndev = net_device->ndev;
651

652 653 654 655
	/*
	 * All inbound packets other than send completion should be xfer page
	 * packet
	 */
656
	if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
657
		netdev_err(ndev, "Unknown packet type received - %d\n",
658
			   packet->type);
659 660 661
		return;
	}

662
	nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
663
			(packet->offset8 << 3));
664

665
	/* Make sure this is a valid nvsp packet */
666 667
	if (nvsp_packet->hdr.msg_type !=
	    NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
668
		netdev_err(ndev, "Unknown nvsp packet type received-"
669
			" %d\n", nvsp_packet->hdr.msg_type);
670 671 672
		return;
	}

673
	vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
674

675
	if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) {
676
		netdev_err(ndev, "Invalid xfer page set id - "
677
			   "expecting %x got %x\n", NETVSC_RECEIVE_BUFFER_ID,
678
			   vmxferpage_packet->xfer_pageset_id);
679 680 681
		return;
	}

682 683 684
	count = vmxferpage_packet->range_cnt;
	netvsc_packet->device = device;
	netvsc_packet->channel = channel;
685

686
	/* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
687
	for (i = 0; i < count; i++) {
688
		/* Initialize the netvsc packet */
689
		netvsc_packet->status = NVSP_STAT_SUCCESS;
690 691
		netvsc_packet->data = (void *)((unsigned long)net_device->
			recv_buf + vmxferpage_packet->ranges[i].byte_offset);
692
		netvsc_packet->total_data_buflen =
693
					vmxferpage_packet->ranges[i].byte_count;
694

695
		/* Pass it to the upper layer */
696
		rndis_filter_receive(device, netvsc_packet);
697

698 699
		if (netvsc_packet->status != NVSP_STAT_SUCCESS)
			status = NVSP_STAT_FAIL;
700 701
	}

702 703
	netvsc_send_recv_completion(device, channel, net_device,
				    vmxferpage_packet->d.trans_id, status);
704 705
}

706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740

static void netvsc_send_table(struct hv_device *hdev,
			      struct vmpacket_descriptor *vmpkt)
{
	struct netvsc_device *nvscdev;
	struct net_device *ndev;
	struct nvsp_message *nvmsg;
	int i;
	u32 count, *tab;

	nvscdev = get_outbound_net_device(hdev);
	if (!nvscdev)
		return;
	ndev = nvscdev->ndev;

	nvmsg = (struct nvsp_message *)((unsigned long)vmpkt +
					(vmpkt->offset8 << 3));

	if (nvmsg->hdr.msg_type != NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE)
		return;

	count = nvmsg->msg.v5_msg.send_table.count;
	if (count != VRSS_SEND_TAB_SIZE) {
		netdev_err(ndev, "Received wrong send-table size:%u\n", count);
		return;
	}

	tab = (u32 *)((unsigned long)&nvmsg->msg.v5_msg.send_table +
		      nvmsg->msg.v5_msg.send_table.offset);

	for (i = 0; i < count; i++)
		nvscdev->send_table[i] = tab[i];
}

void netvsc_channel_cb(void *context)
741
{
742
	int ret;
743 744
	struct vmbus_channel *channel = (struct vmbus_channel *)context;
	struct hv_device *device;
745 746 747
	struct netvsc_device *net_device;
	u32 bytes_recvd;
	u64 request_id;
748
	struct vmpacket_descriptor *desc;
749 750
	unsigned char *buffer;
	int bufferlen = NETVSC_PACKET_SIZE;
751
	struct net_device *ndev;
752

753 754 755 756 757
	if (channel->primary_channel != NULL)
		device = channel->primary_channel->device_obj;
	else
		device = channel->device_obj;

758
	net_device = get_inbound_net_device(device);
759
	if (!net_device)
760
		return;
761
	ndev = net_device->ndev;
762
	buffer = get_per_channel_state(channel);
763

764
	do {
765
		ret = vmbus_recvpacket_raw(channel, buffer, bufferlen,
766
					   &bytes_recvd, &request_id);
767
		if (ret == 0) {
768
			if (bytes_recvd > 0) {
769
				desc = (struct vmpacket_descriptor *)buffer;
770 771
				switch (desc->type) {
				case VM_PKT_COMP:
772 773
					netvsc_send_completion(net_device,
								device, desc);
774 775
					break;

776
				case VM_PKT_DATA_USING_XFER_PAGES:
777 778 779 780 781 782
					netvsc_receive(net_device, channel,
						       device, desc);
					break;

				case VM_PKT_DATA_INBAND:
					netvsc_send_table(device, desc);
783 784 785
					break;

				default:
786
					netdev_err(ndev,
787 788
						   "unhandled packet type %d, "
						   "tid %llx len %d\n",
789
						   desc->type, request_id,
790
						   bytes_recvd);
791
					break;
792 793
				}

794
			} else {
795 796 797
				/*
				 * We are done for this pass.
				 */
798 799
				break;
			}
800

801
		} else if (ret == -ENOBUFS) {
802 803
			if (bufferlen > NETVSC_PACKET_SIZE)
				kfree(buffer);
804
			/* Handle large packet */
805
			buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
806
			if (buffer == NULL) {
807
				/* Try again next time around */
808
				netdev_err(ndev,
809
					   "unable to allocate buffer of size "
810
					   "(%d)!!\n", bytes_recvd);
811 812 813
				break;
			}

814
			bufferlen = bytes_recvd;
815 816 817
		}
	} while (1);

818 819
	if (bufferlen > NETVSC_PACKET_SIZE)
		kfree(buffer);
820 821
	return;
}
822

823 824 825 826
/*
 * netvsc_device_add - Callback when the device belonging to this
 * driver is added
 */
827
int netvsc_device_add(struct hv_device *device, void *additional_info)
828 829
{
	int ret = 0;
830 831
	int ring_size =
	((struct netvsc_device_info *)additional_info)->ring_size;
832
	struct netvsc_device *net_device;
833
	struct net_device *ndev;
834 835 836

	net_device = alloc_net_device(device);
	if (!net_device) {
837
		ret = -ENOMEM;
838 839 840
		goto cleanup;
	}

841 842
	net_device->ring_size = ring_size;

843 844 845 846 847 848 849 850 851
	/*
	 * Coming into this function, struct net_device * is
	 * registered as the driver private data.
	 * In alloc_net_device(), we register struct netvsc_device *
	 * as the driver private data and stash away struct net_device *
	 * in struct netvsc_device *.
	 */
	ndev = net_device->ndev;

852
	/* Initialize the NetVSC channel extension */
853
	init_completion(&net_device->channel_init_wait);
854

855 856
	set_per_channel_state(device->channel, net_device->cb_buffer);

857
	/* Open the channel */
858 859
	ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
			 ring_size * PAGE_SIZE, NULL, 0,
860
			 netvsc_channel_cb, device->channel);
861 862

	if (ret != 0) {
863
		netdev_err(ndev, "unable to open channel: %d\n", ret);
864 865 866 867
		goto cleanup;
	}

	/* Channel is opened */
868
	pr_info("hv_netvsc channel opened successfully\n");
869

870 871
	net_device->chn_table[0] = device->channel;

872 873 874
	/* Connect with the NetVsp */
	ret = netvsc_connect_vsp(device);
	if (ret != 0) {
875
		netdev_err(ndev,
876
			"unable to connect to NetVSP - %d\n", ret);
877 878 879 880 881 882 883 884 885 886 887
		goto close;
	}

	return ret;

close:
	/* Now, we can close the channel safely */
	vmbus_close(device->channel);

cleanup:

888
	if (net_device)
889
		kfree(net_device);
890 891 892

	return ret;
}