channel.c 22.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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:
 *   Haiyang Zhang <haiyangz@microsoft.com>
 *   Hank Janssen  <hjanssen@microsoft.com>
 */
21 22
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

23
#include <linux/kernel.h>
24 25
#include <linux/sched.h>
#include <linux/wait.h>
26
#include <linux/mm.h>
27
#include <linux/slab.h>
28
#include <linux/module.h>
29
#include <linux/hyperv.h>
30

31
#include "hyperv_vmbus.h"
32

K
K. Y. Srinivasan 已提交
33 34 35
#define NUM_PAGES_SPANNED(addr, len) \
((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))

36
/* Internal routines */
37
static int create_gpadl_header(
38 39 40 41
	void *kbuffer,	/* must be phys and virt contiguous */
	u32 size,	/* page-size multiple */
	struct vmbus_channel_msginfo **msginfo,
	u32 *messagecount);
42
static void vmbus_setevent(struct vmbus_channel *channel);
43

44
/*
45
 * vmbus_setevent- Trigger an event notification on the specified
46
 * channel.
47
 */
48
static void vmbus_setevent(struct vmbus_channel *channel)
49
{
50
	struct hv_monitor_page *monitorpage;
51

52
	if (channel->offermsg.monitor_allocated) {
53
		/* Each u32 represents 32 channels */
54
		sync_set_bit(channel->offermsg.child_relid & 31,
55
			(unsigned long *) vmbus_connection.send_int_page +
56
			(channel->offermsg.child_relid >> 5));
57

58
		monitorpage = vmbus_connection.monitor_pages;
59
		monitorpage++; /* Get the child to parent monitor page */
60

61
		sync_set_bit(channel->monitor_bit,
62 63
			(unsigned long *)&monitorpage->trigger_group
					[channel->monitor_grp].pending);
64

65
	} else {
66
		vmbus_set_event(channel->offermsg.child_relid);
67 68 69
	}
}

70
/*
71
 * vmbus_get_debug_info -Retrieve various channel debug info
72
 */
73
void vmbus_get_debug_info(struct vmbus_channel *channel,
74
			      struct vmbus_channel_debug_info *debuginfo)
75
{
76
	struct hv_monitor_page *monitorpage;
77 78
	u8 monitor_group = (u8)channel->offermsg.monitorid / 32;
	u8 monitor_offset = (u8)channel->offermsg.monitorid % 32;
79

80 81 82
	debuginfo->relid = channel->offermsg.child_relid;
	debuginfo->state = channel->state;
	memcpy(&debuginfo->interfacetype,
83
	       &channel->offermsg.offer.if_type, sizeof(uuid_le));
84
	memcpy(&debuginfo->interface_instance,
85
	       &channel->offermsg.offer.if_instance,
86
	       sizeof(uuid_le));
87

88
	monitorpage = (struct hv_monitor_page *)vmbus_connection.monitor_pages;
89

90
	debuginfo->monitorid = channel->offermsg.monitorid;
91

92
	debuginfo->servermonitor_pending =
93
			monitorpage->trigger_group[monitor_group].pending;
94
	debuginfo->servermonitor_latency =
95
			monitorpage->latency[monitor_group][monitor_offset];
96
	debuginfo->servermonitor_connectionid =
97 98
			monitorpage->parameter[monitor_group]
					[monitor_offset].connectionid.u.id;
99

100
	monitorpage++;
101

102
	debuginfo->clientmonitor_pending =
103
			monitorpage->trigger_group[monitor_group].pending;
104
	debuginfo->clientmonitor_latency =
105
			monitorpage->latency[monitor_group][monitor_offset];
106
	debuginfo->clientmonitor_connectionid =
107 108
			monitorpage->parameter[monitor_group]
					[monitor_offset].connectionid.u.id;
109

110 111
	hv_ringbuffer_get_debuginfo(&channel->inbound, &debuginfo->inbound);
	hv_ringbuffer_get_debuginfo(&channel->outbound, &debuginfo->outbound);
112 113
}

114
/*
115
 * vmbus_open - Open the specified channel.
116
 */
117
int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
118 119
		     u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
		     void (*onchannelcallback)(void *context), void *context)
120
{
121
	struct vmbus_channel_open_channel *open_msg;
122
	struct vmbus_channel_msginfo *open_info = NULL;
123
	void *in, *out;
124
	unsigned long flags;
125
	int ret, t, err = 0;
126

127 128
	newchannel->onchannel_callback = onchannelcallback;
	newchannel->channel_callback_context = context;
129

130
	/* Allocate the ring buffer */
131 132 133
	out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
		get_order(send_ringbuffer_size + recv_ringbuffer_size));

134 135 136
	if (!out)
		return -ENOMEM;

137

138
	in = (void *)((unsigned long)out + send_ringbuffer_size);
139

140 141
	newchannel->ringbuffer_pages = out;
	newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
142
					   recv_ringbuffer_size) >> PAGE_SHIFT;
143

144 145 146
	ret = hv_ringbuffer_init(
		&newchannel->outbound, out, send_ringbuffer_size);

147
	if (ret != 0) {
148 149 150 151
		err = ret;
		goto errorout;
	}

152 153
	ret = hv_ringbuffer_init(
		&newchannel->inbound, in, recv_ringbuffer_size);
154
	if (ret != 0) {
155 156 157
		err = ret;
		goto errorout;
	}
158 159


160
	/* Establish the gpadl for the ring buffer */
161
	newchannel->ringbuffer_gpadlhandle = 0;
162

163
	ret = vmbus_establish_gpadl(newchannel,
164
					 newchannel->outbound.ring_buffer,
165 166
					 send_ringbuffer_size +
					 recv_ringbuffer_size,
167
					 &newchannel->ringbuffer_gpadlhandle);
168

169
	if (ret != 0) {
170 171 172
		err = ret;
		goto errorout;
	}
173

174
	/* Create and init the channel open message */
175
	open_info = kmalloc(sizeof(*open_info) +
176 177
			   sizeof(struct vmbus_channel_open_channel),
			   GFP_KERNEL);
178
	if (!open_info) {
179 180 181
		err = -ENOMEM;
		goto errorout;
	}
182

183
	init_completion(&open_info->waitevent);
184

185
	open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
186 187 188 189 190
	open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
	open_msg->openid = newchannel->offermsg.child_relid;
	open_msg->child_relid = newchannel->offermsg.child_relid;
	open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
	open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
191
						  PAGE_SHIFT;
192
	open_msg->server_contextarea_gpadlhandle = 0;
193

194
	if (userdatalen > MAX_USER_DEFINED_BYTES) {
195 196 197 198
		err = -EINVAL;
		goto errorout;
	}

199
	if (userdatalen)
200
		memcpy(open_msg->userdata, userdata, userdatalen);
201

202
	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
203
	list_add_tail(&open_info->msglistentry,
204
		      &vmbus_connection.chn_msg_list);
205
	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
206

207
	ret = vmbus_post_msg(open_msg,
208
			       sizeof(struct vmbus_channel_open_channel));
209 210

	if (ret != 0)
211
		goto cleanup;
212

213
	t = wait_for_completion_timeout(&open_info->waitevent, 5*HZ);
214
	if (t == 0) {
215 216 217 218
		err = -ETIMEDOUT;
		goto errorout;
	}

219

220 221
	if (open_info->response.open_result.status)
		err = open_info->response.open_result.status;
222

223
cleanup:
224
	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
225
	list_del(&open_info->msglistentry);
226
	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
227

228
	kfree(open_info);
229
	return err;
230 231

errorout:
232 233
	hv_ringbuffer_cleanup(&newchannel->outbound);
	hv_ringbuffer_cleanup(&newchannel->inbound);
234 235
	free_pages((unsigned long)out,
		get_order(send_ringbuffer_size + recv_ringbuffer_size));
236
	kfree(open_info);
237
	return err;
238
}
239
EXPORT_SYMBOL_GPL(vmbus_open);
240

241
/*
242
 * create_gpadl_header - Creates a gpadl for the specified buffer
243
 */
244
static int create_gpadl_header(void *kbuffer, u32 size,
245 246
					 struct vmbus_channel_msginfo **msginfo,
					 u32 *messagecount)
247 248
{
	int i;
249
	int pagecount;
250
	unsigned long long pfn;
251 252 253 254 255
	struct vmbus_channel_gpadl_header *gpadl_header;
	struct vmbus_channel_gpadl_body *gpadl_body;
	struct vmbus_channel_msginfo *msgheader;
	struct vmbus_channel_msginfo *msgbody = NULL;
	u32 msgsize;
256

257
	int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
258

259 260
	pagecount = size >> PAGE_SHIFT;
	pfn = virt_to_phys(kbuffer) >> PAGE_SHIFT;
261

262
	/* do we need a gpadl body msg */
263
	pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
264 265
		  sizeof(struct vmbus_channel_gpadl_header) -
		  sizeof(struct gpa_range);
266
	pfncount = pfnsize / sizeof(u64);
267

268
	if (pagecount > pfncount) {
269
		/* we need a gpadl body */
270
		/* fill in the header */
271
		msgsize = sizeof(struct vmbus_channel_msginfo) +
272
			  sizeof(struct vmbus_channel_gpadl_header) +
273 274 275
			  sizeof(struct gpa_range) + pfncount * sizeof(u64);
		msgheader =  kzalloc(msgsize, GFP_KERNEL);
		if (!msgheader)
276
			goto nomem;
277

278 279
		INIT_LIST_HEAD(&msgheader->submsglist);
		msgheader->msgsize = msgsize;
280

281
		gpadl_header = (struct vmbus_channel_gpadl_header *)
282 283 284
			msgheader->msg;
		gpadl_header->rangecount = 1;
		gpadl_header->range_buflen = sizeof(struct gpa_range) +
285
					 pagecount * sizeof(u64);
286 287
		gpadl_header->range[0].byte_offset = 0;
		gpadl_header->range[0].byte_count = size;
288
		for (i = 0; i < pfncount; i++)
289
			gpadl_header->range[0].pfn_array[i] = pfn+i;
290 291
		*msginfo = msgheader;
		*messagecount = 1;
292

293 294
		pfnsum = pfncount;
		pfnleft = pagecount - pfncount;
295

296
		/* how many pfns can we fit */
297
		pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
298
			  sizeof(struct vmbus_channel_gpadl_body);
299
		pfncount = pfnsize / sizeof(u64);
300

301
		/* fill in the body */
302 303 304
		while (pfnleft) {
			if (pfnleft > pfncount)
				pfncurr = pfncount;
305
			else
306
				pfncurr = pfnleft;
307

308
			msgsize = sizeof(struct vmbus_channel_msginfo) +
309
				  sizeof(struct vmbus_channel_gpadl_body) +
310 311
				  pfncurr * sizeof(u64);
			msgbody = kzalloc(msgsize, GFP_KERNEL);
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326

			if (!msgbody) {
				struct vmbus_channel_msginfo *pos = NULL;
				struct vmbus_channel_msginfo *tmp = NULL;
				/*
				 * Free up all the allocated messages.
				 */
				list_for_each_entry_safe(pos, tmp,
					&msgheader->submsglist,
					msglistentry) {

					list_del(&pos->msglistentry);
					kfree(pos);
				}

327
				goto nomem;
328 329
			}

330
			msgbody->msgsize = msgsize;
331 332
			(*messagecount)++;
			gpadl_body =
333
				(struct vmbus_channel_gpadl_body *)msgbody->msg;
334 335 336 337

			/*
			 * Gpadl is u32 and we are using a pointer which could
			 * be 64-bit
338 339
			 * This is governed by the guest/host protocol and
			 * so the hypervisor gurantees that this is ok.
340
			 */
341
			for (i = 0; i < pfncurr; i++)
342
				gpadl_body->pfn[i] = pfn + pfnsum + i;
343

344
			/* add to msg header */
345 346
			list_add_tail(&msgbody->msglistentry,
				      &msgheader->submsglist);
347 348
			pfnsum += pfncurr;
			pfnleft -= pfncurr;
349
		}
350
	} else {
351
		/* everything fits in a header */
352
		msgsize = sizeof(struct vmbus_channel_msginfo) +
353
			  sizeof(struct vmbus_channel_gpadl_header) +
354 355 356
			  sizeof(struct gpa_range) + pagecount * sizeof(u64);
		msgheader = kzalloc(msgsize, GFP_KERNEL);
		if (msgheader == NULL)
357
			goto nomem;
358
		msgheader->msgsize = msgsize;
359 360

		gpadl_header = (struct vmbus_channel_gpadl_header *)
361 362 363
			msgheader->msg;
		gpadl_header->rangecount = 1;
		gpadl_header->range_buflen = sizeof(struct gpa_range) +
364
					 pagecount * sizeof(u64);
365 366
		gpadl_header->range[0].byte_offset = 0;
		gpadl_header->range[0].byte_count = size;
367
		for (i = 0; i < pagecount; i++)
368
			gpadl_header->range[0].pfn_array[i] = pfn+i;
369 370 371

		*msginfo = msgheader;
		*messagecount = 1;
372 373 374
	}

	return 0;
375
nomem:
376 377
	kfree(msgheader);
	kfree(msgbody);
378
	return -ENOMEM;
379 380
}

381
/*
382
 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
383
 *
384 385 386 387
 * @channel: a channel
 * @kbuffer: from kmalloc
 * @size: page-size multiple
 * @gpadl_handle: some funky thing
388
 */
389
int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
390
			       u32 size, u32 *gpadl_handle)
391
{
392 393 394 395 396
	struct vmbus_channel_gpadl_header *gpadlmsg;
	struct vmbus_channel_gpadl_body *gpadl_body;
	struct vmbus_channel_msginfo *msginfo = NULL;
	struct vmbus_channel_msginfo *submsginfo;
	u32 msgcount;
397
	struct list_head *curr;
398
	u32 next_gpadl_handle;
399
	unsigned long flags;
400
	int ret = 0;
401
	int t;
402

403 404
	next_gpadl_handle = atomic_read(&vmbus_connection.next_gpadl_handle);
	atomic_inc(&vmbus_connection.next_gpadl_handle);
405

406
	ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
407 408
	if (ret)
		return ret;
409

410
	init_completion(&msginfo->waitevent);
411

412 413 414 415
	gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
	gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
	gpadlmsg->child_relid = channel->offermsg.child_relid;
	gpadlmsg->gpadl = next_gpadl_handle;
416 417


418
	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
419
	list_add_tail(&msginfo->msglistentry,
420
		      &vmbus_connection.chn_msg_list);
421

422
	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
423

424
	ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
425
			       sizeof(*msginfo));
426
	if (ret != 0)
427
		goto cleanup;
428

429
	if (msgcount > 1) {
430
		list_for_each(curr, &msginfo->submsglist) {
431

432 433
			submsginfo = (struct vmbus_channel_msginfo *)curr;
			gpadl_body =
434
			     (struct vmbus_channel_gpadl_body *)submsginfo->msg;
435

436 437 438
			gpadl_body->header.msgtype =
				CHANNELMSG_GPADL_BODY;
			gpadl_body->gpadl = next_gpadl_handle;
439

440
			ret = vmbus_post_msg(gpadl_body,
441
					       submsginfo->msgsize -
442
					       sizeof(*submsginfo));
443
			if (ret != 0)
444
				goto cleanup;
445

446 447
		}
	}
448
	t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
449
	BUG_ON(t == 0);
450

451

452
	/* At this point, we received the gpadl created msg */
453
	*gpadl_handle = gpadlmsg->gpadl;
454

455
cleanup:
456
	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
457
	list_del(&msginfo->msglistentry);
458
	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
459

460
	kfree(msginfo);
461 462
	return ret;
}
463
EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
464

465
/*
466
 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
467
 */
468
int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
469
{
470
	struct vmbus_channel_gpadl_teardown *msg;
471
	struct vmbus_channel_msginfo *info;
472
	unsigned long flags;
473
	int ret, t;
474

475 476
	info = kmalloc(sizeof(*info) +
		       sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
477 478
	if (!info)
		return -ENOMEM;
479

480
	init_completion(&info->waitevent);
481

482
	msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
483

484 485 486
	msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
	msg->child_relid = channel->offermsg.child_relid;
	msg->gpadl = gpadl_handle;
487

488
	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
489
	list_add_tail(&info->msglistentry,
490
		      &vmbus_connection.chn_msg_list);
491
	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
492
	ret = vmbus_post_msg(msg,
493
			       sizeof(struct vmbus_channel_gpadl_teardown));
494

495
	BUG_ON(ret != 0);
496
	t = wait_for_completion_timeout(&info->waitevent, 5*HZ);
497
	BUG_ON(t == 0);
498

499
	/* Received a torndown response */
500
	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
501
	list_del(&info->msglistentry);
502
	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
503

504
	kfree(info);
505 506
	return ret;
}
507
EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
508

509
/*
510
 * vmbus_close - Close the specified channel
511
 */
512
void vmbus_close(struct vmbus_channel *channel)
513
{
514
	struct vmbus_channel_close_channel *msg;
515
	int ret;
516
	unsigned long flags;
517

518
	/* Stop callback and cancel the timer asap */
519
	spin_lock_irqsave(&channel->inbound_lock, flags);
520
	channel->onchannel_callback = NULL;
521
	spin_unlock_irqrestore(&channel->inbound_lock, flags);
522

523
	/* Send a closing message */
524

525
	msg = &channel->close_msg.msg;
526

527 528
	msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
	msg->child_relid = channel->offermsg.child_relid;
529

530
	ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
531

532
	BUG_ON(ret != 0);
533
	/* Tear down the gpadl for the channel's ring buffer */
534
	if (channel->ringbuffer_gpadlhandle)
535
		vmbus_teardown_gpadl(channel,
536
					  channel->ringbuffer_gpadlhandle);
537

538
	/* Cleanup the ring buffers for this channel */
539 540
	hv_ringbuffer_cleanup(&channel->outbound);
	hv_ringbuffer_cleanup(&channel->inbound);
541

542 543
	free_pages((unsigned long)channel->ringbuffer_pages,
		get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
544 545 546


}
547
EXPORT_SYMBOL_GPL(vmbus_close);
548

549
/**
550
 * vmbus_sendpacket() - Send the specified buffer on the given channel
551 552 553 554 555
 * @channel: Pointer to vmbus_channel structure.
 * @buffer: Pointer to the buffer you want to receive the data into.
 * @bufferlen: Maximum size of what the the buffer will hold
 * @requestid: Identifier of the request
 * @type: Type of packet that is being send e.g. negotiate, time
556 557
 * packet etc.
 *
558
 * Sends data in @buffer directly to hyper-v via the vmbus
559 560 561
 * This will send the data unparsed to hyper-v.
 *
 * Mainly used by Hyper-V drivers.
562
 */
563
int vmbus_sendpacket(struct vmbus_channel *channel, const void *buffer,
564 565
			   u32 bufferlen, u64 requestid,
			   enum vmbus_packet_type type, u32 flags)
566
{
567
	struct vmpacket_descriptor desc;
568
	u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
569
	u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
570 571
	struct scatterlist bufferlist[3];
	u64 aligned_data = 0;
572
	int ret;
573 574


575
	/* Setup the descriptor */
576 577
	desc.type = type; /* VmbusPacketTypeDataInBand; */
	desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
578
	/* in 8-bytes granularity */
579 580 581
	desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
	desc.len8 = (u16)(packetlen_aligned >> 3);
	desc.trans_id = requestid;
582

583 584 585 586 587
	sg_init_table(bufferlist, 3);
	sg_set_buf(&bufferlist[0], &desc, sizeof(struct vmpacket_descriptor));
	sg_set_buf(&bufferlist[1], buffer, bufferlen);
	sg_set_buf(&bufferlist[2], &aligned_data,
		   packetlen_aligned - packetlen);
588

589
	ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
590

591
	if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
592
		vmbus_setevent(channel);
593 594 595

	return ret;
}
596
EXPORT_SYMBOL(vmbus_sendpacket);
597

598
/*
599
 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
600
 * packets using a GPADL Direct packet type.
601
 */
602
int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
603 604 605
				     struct hv_page_buffer pagebuffers[],
				     u32 pagecount, void *buffer, u32 bufferlen,
				     u64 requestid)
606
{
607 608
	int ret;
	int i;
609
	struct vmbus_channel_packet_page_buffer desc;
610 611 612 613 614
	u32 descsize;
	u32 packetlen;
	u32 packetlen_aligned;
	struct scatterlist bufferlist[3];
	u64 aligned_data = 0;
615

616
	if (pagecount > MAX_PAGE_BUFFER_COUNT)
617
		return -EINVAL;
618 619


620
	/*
621
	 * Adjust the size down since vmbus_channel_packet_page_buffer is the
622 623
	 * largest size we support
	 */
624 625
	descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
			  ((MAX_PAGE_BUFFER_COUNT - pagecount) *
626
			  sizeof(struct hv_page_buffer));
627
	packetlen = descsize + bufferlen;
628
	packetlen_aligned = ALIGN(packetlen, sizeof(u64));
629

630
	/* Setup the descriptor */
631
	desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
632
	desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
633 634 635 636 637 638
	desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
	desc.length8 = (u16)(packetlen_aligned >> 3);
	desc.transactionid = requestid;
	desc.rangecount = pagecount;

	for (i = 0; i < pagecount; i++) {
639 640 641
		desc.range[i].len = pagebuffers[i].len;
		desc.range[i].offset = pagebuffers[i].offset;
		desc.range[i].pfn	 = pagebuffers[i].pfn;
642 643
	}

644 645 646 647 648
	sg_init_table(bufferlist, 3);
	sg_set_buf(&bufferlist[0], &desc, descsize);
	sg_set_buf(&bufferlist[1], buffer, bufferlen);
	sg_set_buf(&bufferlist[2], &aligned_data,
		packetlen_aligned - packetlen);
649

650
	ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
651

652
	if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
653
		vmbus_setevent(channel);
654 655 656

	return ret;
}
657
EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
658

659
/*
660
 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
661
 * using a GPADL Direct packet type.
662
 */
663
int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
664 665
				struct hv_multipage_buffer *multi_pagebuffer,
				void *buffer, u32 bufferlen, u64 requestid)
666
{
667
	int ret;
668
	struct vmbus_channel_packet_multipage_buffer desc;
669 670 671 672 673
	u32 descsize;
	u32 packetlen;
	u32 packetlen_aligned;
	struct scatterlist bufferlist[3];
	u64 aligned_data = 0;
674 675
	u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
					 multi_pagebuffer->len);
676 677


678
	if ((pfncount < 0) || (pfncount > MAX_MULTIPAGE_BUFFER_COUNT))
679
		return -EINVAL;
680

681
	/*
682
	 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
683 684
	 * the largest size we support
	 */
685 686
	descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
			  ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
687
			  sizeof(u64));
688
	packetlen = descsize + bufferlen;
689
	packetlen_aligned = ALIGN(packetlen, sizeof(u64));
690 691


692
	/* Setup the descriptor */
693
	desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
694
	desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
695 696 697
	desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
	desc.length8 = (u16)(packetlen_aligned >> 3);
	desc.transactionid = requestid;
698
	desc.rangecount = 1;
699

700 701
	desc.range.len = multi_pagebuffer->len;
	desc.range.offset = multi_pagebuffer->offset;
702

703
	memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
704
	       pfncount * sizeof(u64));
705

706 707 708 709 710
	sg_init_table(bufferlist, 3);
	sg_set_buf(&bufferlist[0], &desc, descsize);
	sg_set_buf(&bufferlist[1], buffer, bufferlen);
	sg_set_buf(&bufferlist[2], &aligned_data,
		packetlen_aligned - packetlen);
711

712
	ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
713

714
	if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
715
		vmbus_setevent(channel);
716 717 718

	return ret;
}
719
EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
720 721

/**
722
 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
723 724 725 726 727
 * @channel: Pointer to vmbus_channel structure.
 * @buffer: Pointer to the buffer you want to receive the data into.
 * @bufferlen: Maximum size of what the the buffer will hold
 * @buffer_actual_len: The actual size of the data after it was received
 * @requestid: Identifier of the request
728 729 730 731 732
 *
 * Receives directly from the hyper-v vmbus and puts the data it received
 * into Buffer. This will receive the data unparsed from hyper-v.
 *
 * Mainly used by Hyper-V drivers.
733
 */
734
int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
735
			u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
736
{
737
	struct vmpacket_descriptor desc;
738 739
	u32 packetlen;
	u32 userlen;
740 741
	int ret;

742 743
	*buffer_actual_len = 0;
	*requestid = 0;
744 745


746
	ret = hv_ringbuffer_peek(&channel->inbound, &desc,
747
			     sizeof(struct vmpacket_descriptor));
748
	if (ret != 0)
749 750
		return 0;

751 752
	packetlen = desc.len8 << 3;
	userlen = packetlen - (desc.offset8 << 3);
753

754
	*buffer_actual_len = userlen;
755

756
	if (userlen > bufferlen) {
757

758
		pr_err("Buffer too small - got %d needs %d\n",
759
			   bufferlen, userlen);
760
		return -ETOOSMALL;
761 762
	}

763
	*requestid = desc.trans_id;
764

765
	/* Copy over the packet to the user buffer */
766
	ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen,
767
			     (desc.offset8 << 3));
768 769 770 771


	return 0;
}
772
EXPORT_SYMBOL(vmbus_recvpacket);
773

774
/*
775
 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
776
 */
777
int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
778 779
			      u32 bufferlen, u32 *buffer_actual_len,
			      u64 *requestid)
780
{
781
	struct vmpacket_descriptor desc;
782 783
	u32 packetlen;
	u32 userlen;
784 785
	int ret;

786 787
	*buffer_actual_len = 0;
	*requestid = 0;
788 789


790
	ret = hv_ringbuffer_peek(&channel->inbound, &desc,
791
			     sizeof(struct vmpacket_descriptor));
792
	if (ret != 0)
793 794 795
		return 0;


796 797
	packetlen = desc.len8 << 3;
	userlen = packetlen - (desc.offset8 << 3);
798

799
	*buffer_actual_len = packetlen;
800

801
	if (packetlen > bufferlen) {
802 803 804
		pr_err("Buffer too small - needed %d bytes but "
			"got space for only %d bytes\n",
			packetlen, bufferlen);
805
		return -ENOBUFS;
806 807
	}

808
	*requestid = desc.trans_id;
809

810
	/* Copy over the entire packet to the user buffer */
811
	ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0);
812 813 814

	return 0;
}
815
EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);