xref: /openbmc/linux/drivers/hv/channel.c (revision 8e9356c6)
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 	struct vmbus_channel_gpadl_header *gpadl_header;
213 	struct vmbus_channel_gpadl_body *gpadl_body;
214 	struct vmbus_channel_msginfo *msgheader;
215 	struct vmbus_channel_msginfo *msgbody = NULL;
216 	u32 msgsize;
217 
218 	int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
219 
220 	pagecount = size >> PAGE_SHIFT;
221 
222 	/* do we need a gpadl body msg */
223 	pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
224 		  sizeof(struct vmbus_channel_gpadl_header) -
225 		  sizeof(struct gpa_range);
226 	pfncount = pfnsize / sizeof(u64);
227 
228 	if (pagecount > pfncount) {
229 		/* we need a gpadl body */
230 		/* fill in the header */
231 		msgsize = sizeof(struct vmbus_channel_msginfo) +
232 			  sizeof(struct vmbus_channel_gpadl_header) +
233 			  sizeof(struct gpa_range) + pfncount * sizeof(u64);
234 		msgheader =  kzalloc(msgsize, GFP_KERNEL);
235 		if (!msgheader)
236 			goto nomem;
237 
238 		INIT_LIST_HEAD(&msgheader->submsglist);
239 		msgheader->msgsize = msgsize;
240 
241 		gpadl_header = (struct vmbus_channel_gpadl_header *)
242 			msgheader->msg;
243 		gpadl_header->rangecount = 1;
244 		gpadl_header->range_buflen = sizeof(struct gpa_range) +
245 					 pagecount * sizeof(u64);
246 		gpadl_header->range[0].byte_offset = 0;
247 		gpadl_header->range[0].byte_count = size;
248 		for (i = 0; i < pfncount; i++)
249 			gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
250 				kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
251 		*msginfo = msgheader;
252 		*messagecount = 1;
253 
254 		pfnsum = pfncount;
255 		pfnleft = pagecount - pfncount;
256 
257 		/* how many pfns can we fit */
258 		pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
259 			  sizeof(struct vmbus_channel_gpadl_body);
260 		pfncount = pfnsize / sizeof(u64);
261 
262 		/* fill in the body */
263 		while (pfnleft) {
264 			if (pfnleft > pfncount)
265 				pfncurr = pfncount;
266 			else
267 				pfncurr = pfnleft;
268 
269 			msgsize = sizeof(struct vmbus_channel_msginfo) +
270 				  sizeof(struct vmbus_channel_gpadl_body) +
271 				  pfncurr * sizeof(u64);
272 			msgbody = kzalloc(msgsize, GFP_KERNEL);
273 
274 			if (!msgbody) {
275 				struct vmbus_channel_msginfo *pos = NULL;
276 				struct vmbus_channel_msginfo *tmp = NULL;
277 				/*
278 				 * Free up all the allocated messages.
279 				 */
280 				list_for_each_entry_safe(pos, tmp,
281 					&msgheader->submsglist,
282 					msglistentry) {
283 
284 					list_del(&pos->msglistentry);
285 					kfree(pos);
286 				}
287 
288 				goto nomem;
289 			}
290 
291 			msgbody->msgsize = msgsize;
292 			(*messagecount)++;
293 			gpadl_body =
294 				(struct vmbus_channel_gpadl_body *)msgbody->msg;
295 
296 			/*
297 			 * Gpadl is u32 and we are using a pointer which could
298 			 * be 64-bit
299 			 * This is governed by the guest/host protocol and
300 			 * so the hypervisor gurantees that this is ok.
301 			 */
302 			for (i = 0; i < pfncurr; i++)
303 				gpadl_body->pfn[i] = slow_virt_to_phys(
304 					kbuffer + PAGE_SIZE * (pfnsum + i)) >>
305 					PAGE_SHIFT;
306 
307 			/* add to msg header */
308 			list_add_tail(&msgbody->msglistentry,
309 				      &msgheader->submsglist);
310 			pfnsum += pfncurr;
311 			pfnleft -= pfncurr;
312 		}
313 	} else {
314 		/* everything fits in a header */
315 		msgsize = sizeof(struct vmbus_channel_msginfo) +
316 			  sizeof(struct vmbus_channel_gpadl_header) +
317 			  sizeof(struct gpa_range) + pagecount * sizeof(u64);
318 		msgheader = kzalloc(msgsize, GFP_KERNEL);
319 		if (msgheader == NULL)
320 			goto nomem;
321 		msgheader->msgsize = msgsize;
322 
323 		gpadl_header = (struct vmbus_channel_gpadl_header *)
324 			msgheader->msg;
325 		gpadl_header->rangecount = 1;
326 		gpadl_header->range_buflen = sizeof(struct gpa_range) +
327 					 pagecount * sizeof(u64);
328 		gpadl_header->range[0].byte_offset = 0;
329 		gpadl_header->range[0].byte_count = size;
330 		for (i = 0; i < pagecount; i++)
331 			gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
332 				kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
333 
334 		*msginfo = msgheader;
335 		*messagecount = 1;
336 	}
337 
338 	return 0;
339 nomem:
340 	kfree(msgheader);
341 	kfree(msgbody);
342 	return -ENOMEM;
343 }
344 
345 /*
346  * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
347  *
348  * @channel: a channel
349  * @kbuffer: from kmalloc or vmalloc
350  * @size: page-size multiple
351  * @gpadl_handle: some funky thing
352  */
353 int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
354 			       u32 size, u32 *gpadl_handle)
355 {
356 	struct vmbus_channel_gpadl_header *gpadlmsg;
357 	struct vmbus_channel_gpadl_body *gpadl_body;
358 	struct vmbus_channel_msginfo *msginfo = NULL;
359 	struct vmbus_channel_msginfo *submsginfo;
360 	u32 msgcount;
361 	struct list_head *curr;
362 	u32 next_gpadl_handle;
363 	unsigned long flags;
364 	int ret = 0;
365 	int t;
366 
367 	next_gpadl_handle = atomic_read(&vmbus_connection.next_gpadl_handle);
368 	atomic_inc(&vmbus_connection.next_gpadl_handle);
369 
370 	ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
371 	if (ret)
372 		return ret;
373 
374 	init_completion(&msginfo->waitevent);
375 
376 	gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
377 	gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
378 	gpadlmsg->child_relid = channel->offermsg.child_relid;
379 	gpadlmsg->gpadl = next_gpadl_handle;
380 
381 
382 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
383 	list_add_tail(&msginfo->msglistentry,
384 		      &vmbus_connection.chn_msg_list);
385 
386 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
387 
388 	ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
389 			       sizeof(*msginfo));
390 	if (ret != 0)
391 		goto cleanup;
392 
393 	if (msgcount > 1) {
394 		list_for_each(curr, &msginfo->submsglist) {
395 
396 			submsginfo = (struct vmbus_channel_msginfo *)curr;
397 			gpadl_body =
398 			     (struct vmbus_channel_gpadl_body *)submsginfo->msg;
399 
400 			gpadl_body->header.msgtype =
401 				CHANNELMSG_GPADL_BODY;
402 			gpadl_body->gpadl = next_gpadl_handle;
403 
404 			ret = vmbus_post_msg(gpadl_body,
405 					       submsginfo->msgsize -
406 					       sizeof(*submsginfo));
407 			if (ret != 0)
408 				goto cleanup;
409 
410 		}
411 	}
412 	t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
413 	BUG_ON(t == 0);
414 
415 
416 	/* At this point, we received the gpadl created msg */
417 	*gpadl_handle = gpadlmsg->gpadl;
418 
419 cleanup:
420 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
421 	list_del(&msginfo->msglistentry);
422 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
423 
424 	kfree(msginfo);
425 	return ret;
426 }
427 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
428 
429 /*
430  * vmbus_teardown_gpadl -Teardown the specified GPADL handle
431  */
432 int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
433 {
434 	struct vmbus_channel_gpadl_teardown *msg;
435 	struct vmbus_channel_msginfo *info;
436 	unsigned long flags;
437 	int ret, t;
438 
439 	info = kmalloc(sizeof(*info) +
440 		       sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
441 	if (!info)
442 		return -ENOMEM;
443 
444 	init_completion(&info->waitevent);
445 
446 	msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
447 
448 	msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
449 	msg->child_relid = channel->offermsg.child_relid;
450 	msg->gpadl = gpadl_handle;
451 
452 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
453 	list_add_tail(&info->msglistentry,
454 		      &vmbus_connection.chn_msg_list);
455 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
456 	ret = vmbus_post_msg(msg,
457 			       sizeof(struct vmbus_channel_gpadl_teardown));
458 
459 	BUG_ON(ret != 0);
460 	t = wait_for_completion_timeout(&info->waitevent, 5*HZ);
461 	BUG_ON(t == 0);
462 
463 	/* Received a torndown response */
464 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
465 	list_del(&info->msglistentry);
466 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
467 
468 	kfree(info);
469 	return ret;
470 }
471 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
472 
473 static void vmbus_close_internal(struct vmbus_channel *channel)
474 {
475 	struct vmbus_channel_close_channel *msg;
476 	int ret;
477 	unsigned long flags;
478 
479 	channel->state = CHANNEL_OPEN_STATE;
480 	channel->sc_creation_callback = NULL;
481 	/* Stop callback and cancel the timer asap */
482 	spin_lock_irqsave(&channel->inbound_lock, flags);
483 	channel->onchannel_callback = NULL;
484 	spin_unlock_irqrestore(&channel->inbound_lock, flags);
485 
486 	/* Send a closing message */
487 
488 	msg = &channel->close_msg.msg;
489 
490 	msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
491 	msg->child_relid = channel->offermsg.child_relid;
492 
493 	ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
494 
495 	BUG_ON(ret != 0);
496 	/* Tear down the gpadl for the channel's ring buffer */
497 	if (channel->ringbuffer_gpadlhandle)
498 		vmbus_teardown_gpadl(channel,
499 					  channel->ringbuffer_gpadlhandle);
500 
501 	/* Cleanup the ring buffers for this channel */
502 	hv_ringbuffer_cleanup(&channel->outbound);
503 	hv_ringbuffer_cleanup(&channel->inbound);
504 
505 	free_pages((unsigned long)channel->ringbuffer_pages,
506 		get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
507 
508 
509 }
510 
511 /*
512  * vmbus_close - Close the specified channel
513  */
514 void vmbus_close(struct vmbus_channel *channel)
515 {
516 	struct list_head *cur, *tmp;
517 	struct vmbus_channel *cur_channel;
518 
519 	if (channel->primary_channel != NULL) {
520 		/*
521 		 * We will only close sub-channels when
522 		 * the primary is closed.
523 		 */
524 		return;
525 	}
526 	/*
527 	 * Close all the sub-channels first and then close the
528 	 * primary channel.
529 	 */
530 	list_for_each_safe(cur, tmp, &channel->sc_list) {
531 		cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
532 		if (cur_channel->state != CHANNEL_OPENED_STATE)
533 			continue;
534 		vmbus_close_internal(cur_channel);
535 	}
536 	/*
537 	 * Now close the primary.
538 	 */
539 	vmbus_close_internal(channel);
540 }
541 EXPORT_SYMBOL_GPL(vmbus_close);
542 
543 /**
544  * vmbus_sendpacket() - Send the specified buffer on the given channel
545  * @channel: Pointer to vmbus_channel structure.
546  * @buffer: Pointer to the buffer you want to receive the data into.
547  * @bufferlen: Maximum size of what the the buffer will hold
548  * @requestid: Identifier of the request
549  * @type: Type of packet that is being send e.g. negotiate, time
550  * packet etc.
551  *
552  * Sends data in @buffer directly to hyper-v via the vmbus
553  * This will send the data unparsed to hyper-v.
554  *
555  * Mainly used by Hyper-V drivers.
556  */
557 int vmbus_sendpacket(struct vmbus_channel *channel, const void *buffer,
558 			   u32 bufferlen, u64 requestid,
559 			   enum vmbus_packet_type type, u32 flags)
560 {
561 	struct vmpacket_descriptor desc;
562 	u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
563 	u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
564 	struct scatterlist bufferlist[3];
565 	u64 aligned_data = 0;
566 	int ret;
567 	bool signal = false;
568 
569 
570 	/* Setup the descriptor */
571 	desc.type = type; /* VmbusPacketTypeDataInBand; */
572 	desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
573 	/* in 8-bytes granularity */
574 	desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
575 	desc.len8 = (u16)(packetlen_aligned >> 3);
576 	desc.trans_id = requestid;
577 
578 	sg_init_table(bufferlist, 3);
579 	sg_set_buf(&bufferlist[0], &desc, sizeof(struct vmpacket_descriptor));
580 	sg_set_buf(&bufferlist[1], buffer, bufferlen);
581 	sg_set_buf(&bufferlist[2], &aligned_data,
582 		   packetlen_aligned - packetlen);
583 
584 	ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
585 
586 	if (ret == 0 && signal)
587 		vmbus_setevent(channel);
588 
589 	return ret;
590 }
591 EXPORT_SYMBOL(vmbus_sendpacket);
592 
593 /*
594  * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
595  * packets using a GPADL Direct packet type.
596  */
597 int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
598 				     struct hv_page_buffer pagebuffers[],
599 				     u32 pagecount, void *buffer, u32 bufferlen,
600 				     u64 requestid)
601 {
602 	int ret;
603 	int i;
604 	struct vmbus_channel_packet_page_buffer desc;
605 	u32 descsize;
606 	u32 packetlen;
607 	u32 packetlen_aligned;
608 	struct scatterlist bufferlist[3];
609 	u64 aligned_data = 0;
610 	bool signal = false;
611 
612 	if (pagecount > MAX_PAGE_BUFFER_COUNT)
613 		return -EINVAL;
614 
615 
616 	/*
617 	 * Adjust the size down since vmbus_channel_packet_page_buffer is the
618 	 * largest size we support
619 	 */
620 	descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
621 			  ((MAX_PAGE_BUFFER_COUNT - pagecount) *
622 			  sizeof(struct hv_page_buffer));
623 	packetlen = descsize + bufferlen;
624 	packetlen_aligned = ALIGN(packetlen, sizeof(u64));
625 
626 	/* Setup the descriptor */
627 	desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
628 	desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
629 	desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
630 	desc.length8 = (u16)(packetlen_aligned >> 3);
631 	desc.transactionid = requestid;
632 	desc.rangecount = pagecount;
633 
634 	for (i = 0; i < pagecount; i++) {
635 		desc.range[i].len = pagebuffers[i].len;
636 		desc.range[i].offset = pagebuffers[i].offset;
637 		desc.range[i].pfn	 = pagebuffers[i].pfn;
638 	}
639 
640 	sg_init_table(bufferlist, 3);
641 	sg_set_buf(&bufferlist[0], &desc, descsize);
642 	sg_set_buf(&bufferlist[1], buffer, bufferlen);
643 	sg_set_buf(&bufferlist[2], &aligned_data,
644 		packetlen_aligned - packetlen);
645 
646 	ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
647 
648 	if (ret == 0 && signal)
649 		vmbus_setevent(channel);
650 
651 	return ret;
652 }
653 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
654 
655 /*
656  * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
657  * using a GPADL Direct packet type.
658  */
659 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
660 				struct hv_multipage_buffer *multi_pagebuffer,
661 				void *buffer, u32 bufferlen, u64 requestid)
662 {
663 	int ret;
664 	struct vmbus_channel_packet_multipage_buffer desc;
665 	u32 descsize;
666 	u32 packetlen;
667 	u32 packetlen_aligned;
668 	struct scatterlist bufferlist[3];
669 	u64 aligned_data = 0;
670 	bool signal = false;
671 	u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
672 					 multi_pagebuffer->len);
673 
674 
675 	if ((pfncount < 0) || (pfncount > MAX_MULTIPAGE_BUFFER_COUNT))
676 		return -EINVAL;
677 
678 	/*
679 	 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
680 	 * the largest size we support
681 	 */
682 	descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
683 			  ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
684 			  sizeof(u64));
685 	packetlen = descsize + bufferlen;
686 	packetlen_aligned = ALIGN(packetlen, sizeof(u64));
687 
688 
689 	/* Setup the descriptor */
690 	desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
691 	desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
692 	desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
693 	desc.length8 = (u16)(packetlen_aligned >> 3);
694 	desc.transactionid = requestid;
695 	desc.rangecount = 1;
696 
697 	desc.range.len = multi_pagebuffer->len;
698 	desc.range.offset = multi_pagebuffer->offset;
699 
700 	memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
701 	       pfncount * sizeof(u64));
702 
703 	sg_init_table(bufferlist, 3);
704 	sg_set_buf(&bufferlist[0], &desc, descsize);
705 	sg_set_buf(&bufferlist[1], buffer, bufferlen);
706 	sg_set_buf(&bufferlist[2], &aligned_data,
707 		packetlen_aligned - packetlen);
708 
709 	ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal);
710 
711 	if (ret == 0 && signal)
712 		vmbus_setevent(channel);
713 
714 	return ret;
715 }
716 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
717 
718 /**
719  * vmbus_recvpacket() - Retrieve the user packet on the specified channel
720  * @channel: Pointer to vmbus_channel structure.
721  * @buffer: Pointer to the buffer you want to receive the data into.
722  * @bufferlen: Maximum size of what the the buffer will hold
723  * @buffer_actual_len: The actual size of the data after it was received
724  * @requestid: Identifier of the request
725  *
726  * Receives directly from the hyper-v vmbus and puts the data it received
727  * into Buffer. This will receive the data unparsed from hyper-v.
728  *
729  * Mainly used by Hyper-V drivers.
730  */
731 int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
732 			u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
733 {
734 	struct vmpacket_descriptor desc;
735 	u32 packetlen;
736 	u32 userlen;
737 	int ret;
738 	bool signal = false;
739 
740 	*buffer_actual_len = 0;
741 	*requestid = 0;
742 
743 
744 	ret = hv_ringbuffer_peek(&channel->inbound, &desc,
745 			     sizeof(struct vmpacket_descriptor));
746 	if (ret != 0)
747 		return 0;
748 
749 	packetlen = desc.len8 << 3;
750 	userlen = packetlen - (desc.offset8 << 3);
751 
752 	*buffer_actual_len = userlen;
753 
754 	if (userlen > bufferlen) {
755 
756 		pr_err("Buffer too small - got %d needs %d\n",
757 			   bufferlen, userlen);
758 		return -ETOOSMALL;
759 	}
760 
761 	*requestid = desc.trans_id;
762 
763 	/* Copy over the packet to the user buffer */
764 	ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen,
765 			     (desc.offset8 << 3), &signal);
766 
767 	if (signal)
768 		vmbus_setevent(channel);
769 
770 	return 0;
771 }
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 			      u32 bufferlen, u32 *buffer_actual_len,
779 			      u64 *requestid)
780 {
781 	struct vmpacket_descriptor desc;
782 	u32 packetlen;
783 	int ret;
784 	bool signal = false;
785 
786 	*buffer_actual_len = 0;
787 	*requestid = 0;
788 
789 
790 	ret = hv_ringbuffer_peek(&channel->inbound, &desc,
791 			     sizeof(struct vmpacket_descriptor));
792 	if (ret != 0)
793 		return 0;
794 
795 
796 	packetlen = desc.len8 << 3;
797 
798 	*buffer_actual_len = packetlen;
799 
800 	if (packetlen > bufferlen) {
801 		pr_err("Buffer too small - needed %d bytes but "
802 			"got space for only %d bytes\n",
803 			packetlen, bufferlen);
804 		return -ENOBUFS;
805 	}
806 
807 	*requestid = desc.trans_id;
808 
809 	/* Copy over the entire packet to the user buffer */
810 	ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0,
811 				 &signal);
812 
813 	if (signal)
814 		vmbus_setevent(channel);
815 
816 	return ret;
817 }
818 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);
819