xref: /openbmc/linux/drivers/hv/channel.c (revision 31b90347)
1 /*
2  * Copyright (c) 2009, Microsoft Corporation.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15  * Place - Suite 330, Boston, MA 02111-1307 USA.
16  *
17  * Authors:
18  *   Haiyang Zhang <haiyangz@microsoft.com>
19  *   Hank Janssen  <hjanssen@microsoft.com>
20  */
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #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 
33 #define NUM_PAGES_SPANNED(addr, len) \
34 ((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
35 
36 /*
37  * vmbus_setevent- Trigger an event notification on the specified
38  * channel.
39  */
40 static void vmbus_setevent(struct vmbus_channel *channel)
41 {
42 	struct hv_monitor_page *monitorpage;
43 
44 	if (channel->offermsg.monitor_allocated) {
45 		/* Each u32 represents 32 channels */
46 		sync_set_bit(channel->offermsg.child_relid & 31,
47 			(unsigned long *) vmbus_connection.send_int_page +
48 			(channel->offermsg.child_relid >> 5));
49 
50 		/* Get the child to parent monitor page */
51 		monitorpage = vmbus_connection.monitor_pages[1];
52 
53 		sync_set_bit(channel->monitor_bit,
54 			(unsigned long *)&monitorpage->trigger_group
55 					[channel->monitor_grp].pending);
56 
57 	} else {
58 		vmbus_set_event(channel);
59 	}
60 }
61 
62 /*
63  * vmbus_open - Open the specified channel.
64  */
65 int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
66 		     u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
67 		     void (*onchannelcallback)(void *context), void *context)
68 {
69 	struct vmbus_channel_open_channel *open_msg;
70 	struct vmbus_channel_msginfo *open_info = NULL;
71 	void *in, *out;
72 	unsigned long flags;
73 	int ret, t, err = 0;
74 
75 	spin_lock_irqsave(&newchannel->sc_lock, flags);
76 	if (newchannel->state == CHANNEL_OPEN_STATE) {
77 		newchannel->state = CHANNEL_OPENING_STATE;
78 	} else {
79 		spin_unlock_irqrestore(&newchannel->sc_lock, flags);
80 		return -EINVAL;
81 	}
82 	spin_unlock_irqrestore(&newchannel->sc_lock, flags);
83 
84 	newchannel->onchannel_callback = onchannelcallback;
85 	newchannel->channel_callback_context = context;
86 
87 	/* Allocate the ring buffer */
88 	out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
89 		get_order(send_ringbuffer_size + recv_ringbuffer_size));
90 
91 	if (!out)
92 		return -ENOMEM;
93 
94 
95 	in = (void *)((unsigned long)out + send_ringbuffer_size);
96 
97 	newchannel->ringbuffer_pages = out;
98 	newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
99 					   recv_ringbuffer_size) >> PAGE_SHIFT;
100 
101 	ret = hv_ringbuffer_init(
102 		&newchannel->outbound, out, send_ringbuffer_size);
103 
104 	if (ret != 0) {
105 		err = ret;
106 		goto error0;
107 	}
108 
109 	ret = hv_ringbuffer_init(
110 		&newchannel->inbound, in, recv_ringbuffer_size);
111 	if (ret != 0) {
112 		err = ret;
113 		goto error0;
114 	}
115 
116 
117 	/* Establish the gpadl for the ring buffer */
118 	newchannel->ringbuffer_gpadlhandle = 0;
119 
120 	ret = vmbus_establish_gpadl(newchannel,
121 					 newchannel->outbound.ring_buffer,
122 					 send_ringbuffer_size +
123 					 recv_ringbuffer_size,
124 					 &newchannel->ringbuffer_gpadlhandle);
125 
126 	if (ret != 0) {
127 		err = ret;
128 		goto error0;
129 	}
130 
131 	/* Create and init the channel open message */
132 	open_info = kmalloc(sizeof(*open_info) +
133 			   sizeof(struct vmbus_channel_open_channel),
134 			   GFP_KERNEL);
135 	if (!open_info) {
136 		err = -ENOMEM;
137 		goto error0;
138 	}
139 
140 	init_completion(&open_info->waitevent);
141 
142 	open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
143 	open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
144 	open_msg->openid = newchannel->offermsg.child_relid;
145 	open_msg->child_relid = newchannel->offermsg.child_relid;
146 	open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
147 	open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
148 						  PAGE_SHIFT;
149 	open_msg->target_vp = newchannel->target_vp;
150 
151 	if (userdatalen > MAX_USER_DEFINED_BYTES) {
152 		err = -EINVAL;
153 		goto error0;
154 	}
155 
156 	if (userdatalen)
157 		memcpy(open_msg->userdata, userdata, userdatalen);
158 
159 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
160 	list_add_tail(&open_info->msglistentry,
161 		      &vmbus_connection.chn_msg_list);
162 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
163 
164 	ret = vmbus_post_msg(open_msg,
165 			       sizeof(struct vmbus_channel_open_channel));
166 
167 	if (ret != 0)
168 		goto error1;
169 
170 	t = wait_for_completion_timeout(&open_info->waitevent, 5*HZ);
171 	if (t == 0) {
172 		err = -ETIMEDOUT;
173 		goto error1;
174 	}
175 
176 
177 	if (open_info->response.open_result.status)
178 		err = open_info->response.open_result.status;
179 
180 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
181 	list_del(&open_info->msglistentry);
182 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
183 
184 	if (err == 0)
185 		newchannel->state = CHANNEL_OPENED_STATE;
186 
187 	kfree(open_info);
188 	return err;
189 
190 error1:
191 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
192 	list_del(&open_info->msglistentry);
193 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
194 
195 error0:
196 	free_pages((unsigned long)out,
197 		get_order(send_ringbuffer_size + recv_ringbuffer_size));
198 	kfree(open_info);
199 	return err;
200 }
201 EXPORT_SYMBOL_GPL(vmbus_open);
202 
203 /*
204  * create_gpadl_header - Creates a gpadl for the specified buffer
205  */
206 static int create_gpadl_header(void *kbuffer, u32 size,
207 					 struct vmbus_channel_msginfo **msginfo,
208 					 u32 *messagecount)
209 {
210 	int i;
211 	int pagecount;
212 	unsigned long long pfn;
213 	struct vmbus_channel_gpadl_header *gpadl_header;
214 	struct vmbus_channel_gpadl_body *gpadl_body;
215 	struct vmbus_channel_msginfo *msgheader;
216 	struct vmbus_channel_msginfo *msgbody = NULL;
217 	u32 msgsize;
218 
219 	int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
220 
221 	pagecount = size >> PAGE_SHIFT;
222 	pfn = virt_to_phys(kbuffer) >> PAGE_SHIFT;
223 
224 	/* do we need a gpadl body msg */
225 	pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
226 		  sizeof(struct vmbus_channel_gpadl_header) -
227 		  sizeof(struct gpa_range);
228 	pfncount = pfnsize / sizeof(u64);
229 
230 	if (pagecount > pfncount) {
231 		/* we need a gpadl body */
232 		/* fill in the header */
233 		msgsize = sizeof(struct vmbus_channel_msginfo) +
234 			  sizeof(struct vmbus_channel_gpadl_header) +
235 			  sizeof(struct gpa_range) + pfncount * sizeof(u64);
236 		msgheader =  kzalloc(msgsize, GFP_KERNEL);
237 		if (!msgheader)
238 			goto nomem;
239 
240 		INIT_LIST_HEAD(&msgheader->submsglist);
241 		msgheader->msgsize = msgsize;
242 
243 		gpadl_header = (struct vmbus_channel_gpadl_header *)
244 			msgheader->msg;
245 		gpadl_header->rangecount = 1;
246 		gpadl_header->range_buflen = sizeof(struct gpa_range) +
247 					 pagecount * sizeof(u64);
248 		gpadl_header->range[0].byte_offset = 0;
249 		gpadl_header->range[0].byte_count = size;
250 		for (i = 0; i < pfncount; i++)
251 			gpadl_header->range[0].pfn_array[i] = pfn+i;
252 		*msginfo = msgheader;
253 		*messagecount = 1;
254 
255 		pfnsum = pfncount;
256 		pfnleft = pagecount - pfncount;
257 
258 		/* how many pfns can we fit */
259 		pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
260 			  sizeof(struct vmbus_channel_gpadl_body);
261 		pfncount = pfnsize / sizeof(u64);
262 
263 		/* fill in the body */
264 		while (pfnleft) {
265 			if (pfnleft > pfncount)
266 				pfncurr = pfncount;
267 			else
268 				pfncurr = pfnleft;
269 
270 			msgsize = sizeof(struct vmbus_channel_msginfo) +
271 				  sizeof(struct vmbus_channel_gpadl_body) +
272 				  pfncurr * sizeof(u64);
273 			msgbody = kzalloc(msgsize, GFP_KERNEL);
274 
275 			if (!msgbody) {
276 				struct vmbus_channel_msginfo *pos = NULL;
277 				struct vmbus_channel_msginfo *tmp = NULL;
278 				/*
279 				 * Free up all the allocated messages.
280 				 */
281 				list_for_each_entry_safe(pos, tmp,
282 					&msgheader->submsglist,
283 					msglistentry) {
284 
285 					list_del(&pos->msglistentry);
286 					kfree(pos);
287 				}
288 
289 				goto nomem;
290 			}
291 
292 			msgbody->msgsize = msgsize;
293 			(*messagecount)++;
294 			gpadl_body =
295 				(struct vmbus_channel_gpadl_body *)msgbody->msg;
296 
297 			/*
298 			 * Gpadl is u32 and we are using a pointer which could
299 			 * be 64-bit
300 			 * This is governed by the guest/host protocol and
301 			 * so the hypervisor gurantees that this is ok.
302 			 */
303 			for (i = 0; i < pfncurr; i++)
304 				gpadl_body->pfn[i] = pfn + pfnsum + i;
305 
306 			/* add to msg header */
307 			list_add_tail(&msgbody->msglistentry,
308 				      &msgheader->submsglist);
309 			pfnsum += pfncurr;
310 			pfnleft -= pfncurr;
311 		}
312 	} else {
313 		/* everything fits in a header */
314 		msgsize = sizeof(struct vmbus_channel_msginfo) +
315 			  sizeof(struct vmbus_channel_gpadl_header) +
316 			  sizeof(struct gpa_range) + pagecount * sizeof(u64);
317 		msgheader = kzalloc(msgsize, GFP_KERNEL);
318 		if (msgheader == NULL)
319 			goto nomem;
320 		msgheader->msgsize = msgsize;
321 
322 		gpadl_header = (struct vmbus_channel_gpadl_header *)
323 			msgheader->msg;
324 		gpadl_header->rangecount = 1;
325 		gpadl_header->range_buflen = sizeof(struct gpa_range) +
326 					 pagecount * sizeof(u64);
327 		gpadl_header->range[0].byte_offset = 0;
328 		gpadl_header->range[0].byte_count = size;
329 		for (i = 0; i < pagecount; i++)
330 			gpadl_header->range[0].pfn_array[i] = pfn+i;
331 
332 		*msginfo = msgheader;
333 		*messagecount = 1;
334 	}
335 
336 	return 0;
337 nomem:
338 	kfree(msgheader);
339 	kfree(msgbody);
340 	return -ENOMEM;
341 }
342 
343 /*
344  * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
345  *
346  * @channel: a channel
347  * @kbuffer: from kmalloc
348  * @size: page-size multiple
349  * @gpadl_handle: some funky thing
350  */
351 int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
352 			       u32 size, u32 *gpadl_handle)
353 {
354 	struct vmbus_channel_gpadl_header *gpadlmsg;
355 	struct vmbus_channel_gpadl_body *gpadl_body;
356 	struct vmbus_channel_msginfo *msginfo = NULL;
357 	struct vmbus_channel_msginfo *submsginfo;
358 	u32 msgcount;
359 	struct list_head *curr;
360 	u32 next_gpadl_handle;
361 	unsigned long flags;
362 	int ret = 0;
363 	int t;
364 
365 	next_gpadl_handle = atomic_read(&vmbus_connection.next_gpadl_handle);
366 	atomic_inc(&vmbus_connection.next_gpadl_handle);
367 
368 	ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
369 	if (ret)
370 		return ret;
371 
372 	init_completion(&msginfo->waitevent);
373 
374 	gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
375 	gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
376 	gpadlmsg->child_relid = channel->offermsg.child_relid;
377 	gpadlmsg->gpadl = next_gpadl_handle;
378 
379 
380 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
381 	list_add_tail(&msginfo->msglistentry,
382 		      &vmbus_connection.chn_msg_list);
383 
384 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
385 
386 	ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
387 			       sizeof(*msginfo));
388 	if (ret != 0)
389 		goto cleanup;
390 
391 	if (msgcount > 1) {
392 		list_for_each(curr, &msginfo->submsglist) {
393 
394 			submsginfo = (struct vmbus_channel_msginfo *)curr;
395 			gpadl_body =
396 			     (struct vmbus_channel_gpadl_body *)submsginfo->msg;
397 
398 			gpadl_body->header.msgtype =
399 				CHANNELMSG_GPADL_BODY;
400 			gpadl_body->gpadl = next_gpadl_handle;
401 
402 			ret = vmbus_post_msg(gpadl_body,
403 					       submsginfo->msgsize -
404 					       sizeof(*submsginfo));
405 			if (ret != 0)
406 				goto cleanup;
407 
408 		}
409 	}
410 	t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
411 	BUG_ON(t == 0);
412 
413 
414 	/* At this point, we received the gpadl created msg */
415 	*gpadl_handle = gpadlmsg->gpadl;
416 
417 cleanup:
418 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
419 	list_del(&msginfo->msglistentry);
420 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
421 
422 	kfree(msginfo);
423 	return ret;
424 }
425 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
426 
427 /*
428  * vmbus_teardown_gpadl -Teardown the specified GPADL handle
429  */
430 int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
431 {
432 	struct vmbus_channel_gpadl_teardown *msg;
433 	struct vmbus_channel_msginfo *info;
434 	unsigned long flags;
435 	int ret, t;
436 
437 	info = kmalloc(sizeof(*info) +
438 		       sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
439 	if (!info)
440 		return -ENOMEM;
441 
442 	init_completion(&info->waitevent);
443 
444 	msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
445 
446 	msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
447 	msg->child_relid = channel->offermsg.child_relid;
448 	msg->gpadl = gpadl_handle;
449 
450 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
451 	list_add_tail(&info->msglistentry,
452 		      &vmbus_connection.chn_msg_list);
453 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
454 	ret = vmbus_post_msg(msg,
455 			       sizeof(struct vmbus_channel_gpadl_teardown));
456 
457 	BUG_ON(ret != 0);
458 	t = wait_for_completion_timeout(&info->waitevent, 5*HZ);
459 	BUG_ON(t == 0);
460 
461 	/* Received a torndown response */
462 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
463 	list_del(&info->msglistentry);
464 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
465 
466 	kfree(info);
467 	return ret;
468 }
469 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
470 
471 static void vmbus_close_internal(struct vmbus_channel *channel)
472 {
473 	struct vmbus_channel_close_channel *msg;
474 	int ret;
475 	unsigned long flags;
476 
477 	channel->state = CHANNEL_OPEN_STATE;
478 	channel->sc_creation_callback = NULL;
479 	/* Stop callback and cancel the timer asap */
480 	spin_lock_irqsave(&channel->inbound_lock, flags);
481 	channel->onchannel_callback = NULL;
482 	spin_unlock_irqrestore(&channel->inbound_lock, flags);
483 
484 	/* Send a closing message */
485 
486 	msg = &channel->close_msg.msg;
487 
488 	msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
489 	msg->child_relid = channel->offermsg.child_relid;
490 
491 	ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
492 
493 	BUG_ON(ret != 0);
494 	/* Tear down the gpadl for the channel's ring buffer */
495 	if (channel->ringbuffer_gpadlhandle)
496 		vmbus_teardown_gpadl(channel,
497 					  channel->ringbuffer_gpadlhandle);
498 
499 	/* Cleanup the ring buffers for this channel */
500 	hv_ringbuffer_cleanup(&channel->outbound);
501 	hv_ringbuffer_cleanup(&channel->inbound);
502 
503 	free_pages((unsigned long)channel->ringbuffer_pages,
504 		get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
505 
506 
507 }
508 
509 /*
510  * vmbus_close - Close the specified channel
511  */
512 void vmbus_close(struct vmbus_channel *channel)
513 {
514 	struct list_head *cur, *tmp;
515 	struct vmbus_channel *cur_channel;
516 
517 	if (channel->primary_channel != NULL) {
518 		/*
519 		 * We will only close sub-channels when
520 		 * the primary is closed.
521 		 */
522 		return;
523 	}
524 	/*
525 	 * Close all the sub-channels first and then close the
526 	 * primary channel.
527 	 */
528 	list_for_each_safe(cur, tmp, &channel->sc_list) {
529 		cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
530 		if (cur_channel->state != CHANNEL_OPENED_STATE)
531 			continue;
532 		vmbus_close_internal(cur_channel);
533 	}
534 	/*
535 	 * Now close the primary.
536 	 */
537 	vmbus_close_internal(channel);
538 }
539 EXPORT_SYMBOL_GPL(vmbus_close);
540 
541 /**
542  * vmbus_sendpacket() - Send the specified buffer on the given channel
543  * @channel: Pointer to vmbus_channel structure.
544  * @buffer: Pointer to the buffer you want to receive the data into.
545  * @bufferlen: Maximum size of what the the buffer will hold
546  * @requestid: Identifier of the request
547  * @type: Type of packet that is being send e.g. negotiate, time
548  * packet etc.
549  *
550  * Sends data in @buffer directly to hyper-v via the vmbus
551  * This will send the data unparsed to hyper-v.
552  *
553  * Mainly used by Hyper-V drivers.
554  */
555 int vmbus_sendpacket(struct vmbus_channel *channel, const void *buffer,
556 			   u32 bufferlen, u64 requestid,
557 			   enum vmbus_packet_type type, u32 flags)
558 {
559 	struct vmpacket_descriptor desc;
560 	u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
561 	u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
562 	struct scatterlist bufferlist[3];
563 	u64 aligned_data = 0;
564 	int ret;
565 	bool signal = false;
566 
567 
568 	/* Setup the descriptor */
569 	desc.type = type; /* VmbusPacketTypeDataInBand; */
570 	desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
571 	/* in 8-bytes granularity */
572 	desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
573 	desc.len8 = (u16)(packetlen_aligned >> 3);
574 	desc.trans_id = requestid;
575 
576 	sg_init_table(bufferlist, 3);
577 	sg_set_buf(&bufferlist[0], &desc, sizeof(struct vmpacket_descriptor));
578 	sg_set_buf(&bufferlist[1], buffer, bufferlen);
579 	sg_set_buf(&bufferlist[2], &aligned_data,
580 		   packetlen_aligned - packetlen);
581 
582 	ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
583 
584 	if (ret == 0 && signal)
585 		vmbus_setevent(channel);
586 
587 	return ret;
588 }
589 EXPORT_SYMBOL(vmbus_sendpacket);
590 
591 /*
592  * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
593  * packets using a GPADL Direct packet type.
594  */
595 int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
596 				     struct hv_page_buffer pagebuffers[],
597 				     u32 pagecount, void *buffer, u32 bufferlen,
598 				     u64 requestid)
599 {
600 	int ret;
601 	int i;
602 	struct vmbus_channel_packet_page_buffer desc;
603 	u32 descsize;
604 	u32 packetlen;
605 	u32 packetlen_aligned;
606 	struct scatterlist bufferlist[3];
607 	u64 aligned_data = 0;
608 	bool signal = false;
609 
610 	if (pagecount > MAX_PAGE_BUFFER_COUNT)
611 		return -EINVAL;
612 
613 
614 	/*
615 	 * Adjust the size down since vmbus_channel_packet_page_buffer is the
616 	 * largest size we support
617 	 */
618 	descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
619 			  ((MAX_PAGE_BUFFER_COUNT - pagecount) *
620 			  sizeof(struct hv_page_buffer));
621 	packetlen = descsize + bufferlen;
622 	packetlen_aligned = ALIGN(packetlen, sizeof(u64));
623 
624 	/* Setup the descriptor */
625 	desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
626 	desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
627 	desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
628 	desc.length8 = (u16)(packetlen_aligned >> 3);
629 	desc.transactionid = requestid;
630 	desc.rangecount = pagecount;
631 
632 	for (i = 0; i < pagecount; i++) {
633 		desc.range[i].len = pagebuffers[i].len;
634 		desc.range[i].offset = pagebuffers[i].offset;
635 		desc.range[i].pfn	 = pagebuffers[i].pfn;
636 	}
637 
638 	sg_init_table(bufferlist, 3);
639 	sg_set_buf(&bufferlist[0], &desc, descsize);
640 	sg_set_buf(&bufferlist[1], buffer, bufferlen);
641 	sg_set_buf(&bufferlist[2], &aligned_data,
642 		packetlen_aligned - packetlen);
643 
644 	ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
645 
646 	if (ret == 0 && signal)
647 		vmbus_setevent(channel);
648 
649 	return ret;
650 }
651 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
652 
653 /*
654  * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
655  * using a GPADL Direct packet type.
656  */
657 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
658 				struct hv_multipage_buffer *multi_pagebuffer,
659 				void *buffer, u32 bufferlen, u64 requestid)
660 {
661 	int ret;
662 	struct vmbus_channel_packet_multipage_buffer desc;
663 	u32 descsize;
664 	u32 packetlen;
665 	u32 packetlen_aligned;
666 	struct scatterlist bufferlist[3];
667 	u64 aligned_data = 0;
668 	bool signal = false;
669 	u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
670 					 multi_pagebuffer->len);
671 
672 
673 	if ((pfncount < 0) || (pfncount > MAX_MULTIPAGE_BUFFER_COUNT))
674 		return -EINVAL;
675 
676 	/*
677 	 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
678 	 * the largest size we support
679 	 */
680 	descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
681 			  ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
682 			  sizeof(u64));
683 	packetlen = descsize + bufferlen;
684 	packetlen_aligned = ALIGN(packetlen, sizeof(u64));
685 
686 
687 	/* Setup the descriptor */
688 	desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
689 	desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
690 	desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
691 	desc.length8 = (u16)(packetlen_aligned >> 3);
692 	desc.transactionid = requestid;
693 	desc.rangecount = 1;
694 
695 	desc.range.len = multi_pagebuffer->len;
696 	desc.range.offset = multi_pagebuffer->offset;
697 
698 	memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
699 	       pfncount * sizeof(u64));
700 
701 	sg_init_table(bufferlist, 3);
702 	sg_set_buf(&bufferlist[0], &desc, descsize);
703 	sg_set_buf(&bufferlist[1], buffer, bufferlen);
704 	sg_set_buf(&bufferlist[2], &aligned_data,
705 		packetlen_aligned - packetlen);
706 
707 	ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
708 
709 	if (ret == 0 && signal)
710 		vmbus_setevent(channel);
711 
712 	return ret;
713 }
714 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
715 
716 /**
717  * vmbus_recvpacket() - Retrieve the user packet on the specified channel
718  * @channel: Pointer to vmbus_channel structure.
719  * @buffer: Pointer to the buffer you want to receive the data into.
720  * @bufferlen: Maximum size of what the the buffer will hold
721  * @buffer_actual_len: The actual size of the data after it was received
722  * @requestid: Identifier of the request
723  *
724  * Receives directly from the hyper-v vmbus and puts the data it received
725  * into Buffer. This will receive the data unparsed from hyper-v.
726  *
727  * Mainly used by Hyper-V drivers.
728  */
729 int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
730 			u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
731 {
732 	struct vmpacket_descriptor desc;
733 	u32 packetlen;
734 	u32 userlen;
735 	int ret;
736 	bool signal = false;
737 
738 	*buffer_actual_len = 0;
739 	*requestid = 0;
740 
741 
742 	ret = hv_ringbuffer_peek(&channel->inbound, &desc,
743 			     sizeof(struct vmpacket_descriptor));
744 	if (ret != 0)
745 		return 0;
746 
747 	packetlen = desc.len8 << 3;
748 	userlen = packetlen - (desc.offset8 << 3);
749 
750 	*buffer_actual_len = userlen;
751 
752 	if (userlen > bufferlen) {
753 
754 		pr_err("Buffer too small - got %d needs %d\n",
755 			   bufferlen, userlen);
756 		return -ETOOSMALL;
757 	}
758 
759 	*requestid = desc.trans_id;
760 
761 	/* Copy over the packet to the user buffer */
762 	ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen,
763 			     (desc.offset8 << 3), &signal);
764 
765 	if (signal)
766 		vmbus_setevent(channel);
767 
768 	return 0;
769 }
770 EXPORT_SYMBOL(vmbus_recvpacket);
771 
772 /*
773  * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
774  */
775 int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
776 			      u32 bufferlen, u32 *buffer_actual_len,
777 			      u64 *requestid)
778 {
779 	struct vmpacket_descriptor desc;
780 	u32 packetlen;
781 	int ret;
782 	bool signal = false;
783 
784 	*buffer_actual_len = 0;
785 	*requestid = 0;
786 
787 
788 	ret = hv_ringbuffer_peek(&channel->inbound, &desc,
789 			     sizeof(struct vmpacket_descriptor));
790 	if (ret != 0)
791 		return 0;
792 
793 
794 	packetlen = desc.len8 << 3;
795 
796 	*buffer_actual_len = packetlen;
797 
798 	if (packetlen > bufferlen) {
799 		pr_err("Buffer too small - needed %d bytes but "
800 			"got space for only %d bytes\n",
801 			packetlen, bufferlen);
802 		return -ENOBUFS;
803 	}
804 
805 	*requestid = desc.trans_id;
806 
807 	/* Copy over the entire packet to the user buffer */
808 	ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0,
809 				 &signal);
810 
811 	if (signal)
812 		vmbus_setevent(channel);
813 
814 	return ret;
815 }
816 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);
817