xref: /linux/drivers/hv/channel.c (revision 5bdd5fbb35ab0fe21bf2263106f51c5bee466a07)
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 #include <linux/uio.h>
31 #include <linux/interrupt.h>
32 
33 #include "hyperv_vmbus.h"
34 
35 #define NUM_PAGES_SPANNED(addr, len) \
36 ((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
37 
38 /*
39  * vmbus_setevent- Trigger an event notification on the specified
40  * channel.
41  */
42 void vmbus_setevent(struct vmbus_channel *channel)
43 {
44 	struct hv_monitor_page *monitorpage;
45 
46 	trace_vmbus_setevent(channel);
47 
48 	/*
49 	 * For channels marked as in "low latency" mode
50 	 * bypass the monitor page mechanism.
51 	 */
52 	if (channel->offermsg.monitor_allocated && !channel->low_latency) {
53 		vmbus_send_interrupt(channel->offermsg.child_relid);
54 
55 		/* Get the child to parent monitor page */
56 		monitorpage = vmbus_connection.monitor_pages[1];
57 
58 		sync_set_bit(channel->monitor_bit,
59 			(unsigned long *)&monitorpage->trigger_group
60 					[channel->monitor_grp].pending);
61 
62 	} else {
63 		vmbus_set_event(channel);
64 	}
65 }
66 EXPORT_SYMBOL_GPL(vmbus_setevent);
67 
68 /*
69  * vmbus_open - Open the specified channel.
70  */
71 int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
72 		     u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
73 		     void (*onchannelcallback)(void *context), void *context)
74 {
75 	struct vmbus_channel_open_channel *open_msg;
76 	struct vmbus_channel_msginfo *open_info = NULL;
77 	unsigned long flags;
78 	int ret, err = 0;
79 	struct page *page;
80 
81 	if (send_ringbuffer_size % PAGE_SIZE ||
82 	    recv_ringbuffer_size % PAGE_SIZE)
83 		return -EINVAL;
84 
85 	spin_lock_irqsave(&newchannel->lock, flags);
86 	if (newchannel->state == CHANNEL_OPEN_STATE) {
87 		newchannel->state = CHANNEL_OPENING_STATE;
88 	} else {
89 		spin_unlock_irqrestore(&newchannel->lock, flags);
90 		return -EINVAL;
91 	}
92 	spin_unlock_irqrestore(&newchannel->lock, flags);
93 
94 	newchannel->onchannel_callback = onchannelcallback;
95 	newchannel->channel_callback_context = context;
96 
97 	/* Allocate the ring buffer */
98 	page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
99 				GFP_KERNEL|__GFP_ZERO,
100 				get_order(send_ringbuffer_size +
101 				recv_ringbuffer_size));
102 
103 	if (!page)
104 		page = alloc_pages(GFP_KERNEL|__GFP_ZERO,
105 				   get_order(send_ringbuffer_size +
106 					     recv_ringbuffer_size));
107 
108 	if (!page) {
109 		err = -ENOMEM;
110 		goto error_set_chnstate;
111 	}
112 
113 	newchannel->ringbuffer_pages = page_address(page);
114 	newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
115 					   recv_ringbuffer_size) >> PAGE_SHIFT;
116 
117 	ret = hv_ringbuffer_init(&newchannel->outbound, page,
118 				 send_ringbuffer_size >> PAGE_SHIFT);
119 
120 	if (ret != 0) {
121 		err = ret;
122 		goto error_free_pages;
123 	}
124 
125 	ret = hv_ringbuffer_init(&newchannel->inbound,
126 				 &page[send_ringbuffer_size >> PAGE_SHIFT],
127 				 recv_ringbuffer_size >> PAGE_SHIFT);
128 	if (ret != 0) {
129 		err = ret;
130 		goto error_free_pages;
131 	}
132 
133 
134 	/* Establish the gpadl for the ring buffer */
135 	newchannel->ringbuffer_gpadlhandle = 0;
136 
137 	ret = vmbus_establish_gpadl(newchannel,
138 				    page_address(page),
139 				    send_ringbuffer_size +
140 				    recv_ringbuffer_size,
141 				    &newchannel->ringbuffer_gpadlhandle);
142 
143 	if (ret != 0) {
144 		err = ret;
145 		goto error_free_pages;
146 	}
147 
148 	/* Create and init the channel open message */
149 	open_info = kmalloc(sizeof(*open_info) +
150 			   sizeof(struct vmbus_channel_open_channel),
151 			   GFP_KERNEL);
152 	if (!open_info) {
153 		err = -ENOMEM;
154 		goto error_free_gpadl;
155 	}
156 
157 	init_completion(&open_info->waitevent);
158 	open_info->waiting_channel = newchannel;
159 
160 	open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
161 	open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
162 	open_msg->openid = newchannel->offermsg.child_relid;
163 	open_msg->child_relid = newchannel->offermsg.child_relid;
164 	open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
165 	open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
166 						  PAGE_SHIFT;
167 	open_msg->target_vp = newchannel->target_vp;
168 
169 	if (userdatalen > MAX_USER_DEFINED_BYTES) {
170 		err = -EINVAL;
171 		goto error_free_gpadl;
172 	}
173 
174 	if (userdatalen)
175 		memcpy(open_msg->userdata, userdata, userdatalen);
176 
177 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
178 	list_add_tail(&open_info->msglistentry,
179 		      &vmbus_connection.chn_msg_list);
180 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
181 
182 	if (newchannel->rescind) {
183 		err = -ENODEV;
184 		goto error_free_gpadl;
185 	}
186 
187 	ret = vmbus_post_msg(open_msg,
188 			     sizeof(struct vmbus_channel_open_channel), true);
189 
190 	trace_vmbus_open(open_msg, ret);
191 
192 	if (ret != 0) {
193 		err = ret;
194 		goto error_clean_msglist;
195 	}
196 
197 	wait_for_completion(&open_info->waitevent);
198 
199 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
200 	list_del(&open_info->msglistentry);
201 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
202 
203 	if (newchannel->rescind) {
204 		err = -ENODEV;
205 		goto error_free_gpadl;
206 	}
207 
208 	if (open_info->response.open_result.status) {
209 		err = -EAGAIN;
210 		goto error_free_gpadl;
211 	}
212 
213 	newchannel->state = CHANNEL_OPENED_STATE;
214 	kfree(open_info);
215 	return 0;
216 
217 error_clean_msglist:
218 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
219 	list_del(&open_info->msglistentry);
220 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
221 
222 error_free_gpadl:
223 	vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
224 	kfree(open_info);
225 error_free_pages:
226 	hv_ringbuffer_cleanup(&newchannel->outbound);
227 	hv_ringbuffer_cleanup(&newchannel->inbound);
228 	__free_pages(page,
229 		     get_order(send_ringbuffer_size + recv_ringbuffer_size));
230 error_set_chnstate:
231 	newchannel->state = CHANNEL_OPEN_STATE;
232 	return err;
233 }
234 EXPORT_SYMBOL_GPL(vmbus_open);
235 
236 /* Used for Hyper-V Socket: a guest client's connect() to the host */
237 int vmbus_send_tl_connect_request(const uuid_le *shv_guest_servie_id,
238 				  const uuid_le *shv_host_servie_id)
239 {
240 	struct vmbus_channel_tl_connect_request conn_msg;
241 	int ret;
242 
243 	memset(&conn_msg, 0, sizeof(conn_msg));
244 	conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST;
245 	conn_msg.guest_endpoint_id = *shv_guest_servie_id;
246 	conn_msg.host_service_id = *shv_host_servie_id;
247 
248 	ret = vmbus_post_msg(&conn_msg, sizeof(conn_msg), true);
249 
250 	trace_vmbus_send_tl_connect_request(&conn_msg, ret);
251 
252 	return ret;
253 }
254 EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request);
255 
256 /*
257  * create_gpadl_header - Creates a gpadl for the specified buffer
258  */
259 static int create_gpadl_header(void *kbuffer, u32 size,
260 			       struct vmbus_channel_msginfo **msginfo)
261 {
262 	int i;
263 	int pagecount;
264 	struct vmbus_channel_gpadl_header *gpadl_header;
265 	struct vmbus_channel_gpadl_body *gpadl_body;
266 	struct vmbus_channel_msginfo *msgheader;
267 	struct vmbus_channel_msginfo *msgbody = NULL;
268 	u32 msgsize;
269 
270 	int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
271 
272 	pagecount = size >> PAGE_SHIFT;
273 
274 	/* do we need a gpadl body msg */
275 	pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
276 		  sizeof(struct vmbus_channel_gpadl_header) -
277 		  sizeof(struct gpa_range);
278 	pfncount = pfnsize / sizeof(u64);
279 
280 	if (pagecount > pfncount) {
281 		/* we need a gpadl body */
282 		/* fill in the header */
283 		msgsize = sizeof(struct vmbus_channel_msginfo) +
284 			  sizeof(struct vmbus_channel_gpadl_header) +
285 			  sizeof(struct gpa_range) + pfncount * sizeof(u64);
286 		msgheader =  kzalloc(msgsize, GFP_KERNEL);
287 		if (!msgheader)
288 			goto nomem;
289 
290 		INIT_LIST_HEAD(&msgheader->submsglist);
291 		msgheader->msgsize = msgsize;
292 
293 		gpadl_header = (struct vmbus_channel_gpadl_header *)
294 			msgheader->msg;
295 		gpadl_header->rangecount = 1;
296 		gpadl_header->range_buflen = sizeof(struct gpa_range) +
297 					 pagecount * sizeof(u64);
298 		gpadl_header->range[0].byte_offset = 0;
299 		gpadl_header->range[0].byte_count = size;
300 		for (i = 0; i < pfncount; i++)
301 			gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
302 				kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
303 		*msginfo = msgheader;
304 
305 		pfnsum = pfncount;
306 		pfnleft = pagecount - pfncount;
307 
308 		/* how many pfns can we fit */
309 		pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
310 			  sizeof(struct vmbus_channel_gpadl_body);
311 		pfncount = pfnsize / sizeof(u64);
312 
313 		/* fill in the body */
314 		while (pfnleft) {
315 			if (pfnleft > pfncount)
316 				pfncurr = pfncount;
317 			else
318 				pfncurr = pfnleft;
319 
320 			msgsize = sizeof(struct vmbus_channel_msginfo) +
321 				  sizeof(struct vmbus_channel_gpadl_body) +
322 				  pfncurr * sizeof(u64);
323 			msgbody = kzalloc(msgsize, GFP_KERNEL);
324 
325 			if (!msgbody) {
326 				struct vmbus_channel_msginfo *pos = NULL;
327 				struct vmbus_channel_msginfo *tmp = NULL;
328 				/*
329 				 * Free up all the allocated messages.
330 				 */
331 				list_for_each_entry_safe(pos, tmp,
332 					&msgheader->submsglist,
333 					msglistentry) {
334 
335 					list_del(&pos->msglistentry);
336 					kfree(pos);
337 				}
338 
339 				goto nomem;
340 			}
341 
342 			msgbody->msgsize = msgsize;
343 			gpadl_body =
344 				(struct vmbus_channel_gpadl_body *)msgbody->msg;
345 
346 			/*
347 			 * Gpadl is u32 and we are using a pointer which could
348 			 * be 64-bit
349 			 * This is governed by the guest/host protocol and
350 			 * so the hypervisor guarantees that this is ok.
351 			 */
352 			for (i = 0; i < pfncurr; i++)
353 				gpadl_body->pfn[i] = slow_virt_to_phys(
354 					kbuffer + PAGE_SIZE * (pfnsum + i)) >>
355 					PAGE_SHIFT;
356 
357 			/* add to msg header */
358 			list_add_tail(&msgbody->msglistentry,
359 				      &msgheader->submsglist);
360 			pfnsum += pfncurr;
361 			pfnleft -= pfncurr;
362 		}
363 	} else {
364 		/* everything fits in a header */
365 		msgsize = sizeof(struct vmbus_channel_msginfo) +
366 			  sizeof(struct vmbus_channel_gpadl_header) +
367 			  sizeof(struct gpa_range) + pagecount * sizeof(u64);
368 		msgheader = kzalloc(msgsize, GFP_KERNEL);
369 		if (msgheader == NULL)
370 			goto nomem;
371 
372 		INIT_LIST_HEAD(&msgheader->submsglist);
373 		msgheader->msgsize = msgsize;
374 
375 		gpadl_header = (struct vmbus_channel_gpadl_header *)
376 			msgheader->msg;
377 		gpadl_header->rangecount = 1;
378 		gpadl_header->range_buflen = sizeof(struct gpa_range) +
379 					 pagecount * sizeof(u64);
380 		gpadl_header->range[0].byte_offset = 0;
381 		gpadl_header->range[0].byte_count = size;
382 		for (i = 0; i < pagecount; i++)
383 			gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
384 				kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
385 
386 		*msginfo = msgheader;
387 	}
388 
389 	return 0;
390 nomem:
391 	kfree(msgheader);
392 	kfree(msgbody);
393 	return -ENOMEM;
394 }
395 
396 /*
397  * vmbus_establish_gpadl - Establish a GPADL for the specified buffer
398  *
399  * @channel: a channel
400  * @kbuffer: from kmalloc or vmalloc
401  * @size: page-size multiple
402  * @gpadl_handle: some funky thing
403  */
404 int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
405 			       u32 size, u32 *gpadl_handle)
406 {
407 	struct vmbus_channel_gpadl_header *gpadlmsg;
408 	struct vmbus_channel_gpadl_body *gpadl_body;
409 	struct vmbus_channel_msginfo *msginfo = NULL;
410 	struct vmbus_channel_msginfo *submsginfo, *tmp;
411 	struct list_head *curr;
412 	u32 next_gpadl_handle;
413 	unsigned long flags;
414 	int ret = 0;
415 
416 	next_gpadl_handle =
417 		(atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
418 
419 	ret = create_gpadl_header(kbuffer, size, &msginfo);
420 	if (ret)
421 		return ret;
422 
423 	init_completion(&msginfo->waitevent);
424 	msginfo->waiting_channel = channel;
425 
426 	gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
427 	gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
428 	gpadlmsg->child_relid = channel->offermsg.child_relid;
429 	gpadlmsg->gpadl = next_gpadl_handle;
430 
431 
432 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
433 	list_add_tail(&msginfo->msglistentry,
434 		      &vmbus_connection.chn_msg_list);
435 
436 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
437 
438 	if (channel->rescind) {
439 		ret = -ENODEV;
440 		goto cleanup;
441 	}
442 
443 	ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
444 			     sizeof(*msginfo), true);
445 
446 	trace_vmbus_establish_gpadl_header(gpadlmsg, ret);
447 
448 	if (ret != 0)
449 		goto cleanup;
450 
451 	list_for_each(curr, &msginfo->submsglist) {
452 		submsginfo = (struct vmbus_channel_msginfo *)curr;
453 		gpadl_body =
454 			(struct vmbus_channel_gpadl_body *)submsginfo->msg;
455 
456 		gpadl_body->header.msgtype =
457 			CHANNELMSG_GPADL_BODY;
458 		gpadl_body->gpadl = next_gpadl_handle;
459 
460 		ret = vmbus_post_msg(gpadl_body,
461 				     submsginfo->msgsize - sizeof(*submsginfo),
462 				     true);
463 
464 		trace_vmbus_establish_gpadl_body(gpadl_body, ret);
465 
466 		if (ret != 0)
467 			goto cleanup;
468 
469 	}
470 	wait_for_completion(&msginfo->waitevent);
471 
472 	if (channel->rescind) {
473 		ret = -ENODEV;
474 		goto cleanup;
475 	}
476 
477 	/* At this point, we received the gpadl created msg */
478 	*gpadl_handle = gpadlmsg->gpadl;
479 
480 cleanup:
481 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
482 	list_del(&msginfo->msglistentry);
483 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
484 	list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
485 				 msglistentry) {
486 		kfree(submsginfo);
487 	}
488 
489 	kfree(msginfo);
490 	return ret;
491 }
492 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
493 
494 /*
495  * vmbus_teardown_gpadl -Teardown the specified GPADL handle
496  */
497 int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
498 {
499 	struct vmbus_channel_gpadl_teardown *msg;
500 	struct vmbus_channel_msginfo *info;
501 	unsigned long flags;
502 	int ret;
503 
504 	info = kmalloc(sizeof(*info) +
505 		       sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
506 	if (!info)
507 		return -ENOMEM;
508 
509 	init_completion(&info->waitevent);
510 	info->waiting_channel = channel;
511 
512 	msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
513 
514 	msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
515 	msg->child_relid = channel->offermsg.child_relid;
516 	msg->gpadl = gpadl_handle;
517 
518 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
519 	list_add_tail(&info->msglistentry,
520 		      &vmbus_connection.chn_msg_list);
521 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
522 
523 	if (channel->rescind)
524 		goto post_msg_err;
525 
526 	ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_gpadl_teardown),
527 			     true);
528 
529 	trace_vmbus_teardown_gpadl(msg, ret);
530 
531 	if (ret)
532 		goto post_msg_err;
533 
534 	wait_for_completion(&info->waitevent);
535 
536 post_msg_err:
537 	/*
538 	 * If the channel has been rescinded;
539 	 * we will be awakened by the rescind
540 	 * handler; set the error code to zero so we don't leak memory.
541 	 */
542 	if (channel->rescind)
543 		ret = 0;
544 
545 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
546 	list_del(&info->msglistentry);
547 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
548 
549 	kfree(info);
550 	return ret;
551 }
552 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
553 
554 static void reset_channel_cb(void *arg)
555 {
556 	struct vmbus_channel *channel = arg;
557 
558 	channel->onchannel_callback = NULL;
559 }
560 
561 static int vmbus_close_internal(struct vmbus_channel *channel)
562 {
563 	struct vmbus_channel_close_channel *msg;
564 	int ret;
565 
566 	/*
567 	 * vmbus_on_event(), running in the per-channel tasklet, can race
568 	 * with vmbus_close_internal() in the case of SMP guest, e.g., when
569 	 * the former is accessing channel->inbound.ring_buffer, the latter
570 	 * could be freeing the ring_buffer pages, so here we must stop it
571 	 * first.
572 	 */
573 	tasklet_disable(&channel->callback_event);
574 
575 	/*
576 	 * In case a device driver's probe() fails (e.g.,
577 	 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
578 	 * rescinded later (e.g., we dynamically disable an Integrated Service
579 	 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
580 	 * here we should skip most of the below cleanup work.
581 	 */
582 	if (channel->state != CHANNEL_OPENED_STATE) {
583 		ret = -EINVAL;
584 		goto out;
585 	}
586 
587 	channel->state = CHANNEL_OPEN_STATE;
588 	channel->sc_creation_callback = NULL;
589 	/* Stop callback and cancel the timer asap */
590 	if (channel->target_cpu != get_cpu()) {
591 		put_cpu();
592 		smp_call_function_single(channel->target_cpu, reset_channel_cb,
593 					 channel, true);
594 	} else {
595 		reset_channel_cb(channel);
596 		put_cpu();
597 	}
598 
599 	/* Send a closing message */
600 
601 	msg = &channel->close_msg.msg;
602 
603 	msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
604 	msg->child_relid = channel->offermsg.child_relid;
605 
606 	ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel),
607 			     true);
608 
609 	trace_vmbus_close_internal(msg, ret);
610 
611 	if (ret) {
612 		pr_err("Close failed: close post msg return is %d\n", ret);
613 		/*
614 		 * If we failed to post the close msg,
615 		 * it is perhaps better to leak memory.
616 		 */
617 		goto out;
618 	}
619 
620 	/* Tear down the gpadl for the channel's ring buffer */
621 	if (channel->ringbuffer_gpadlhandle) {
622 		ret = vmbus_teardown_gpadl(channel,
623 					   channel->ringbuffer_gpadlhandle);
624 		if (ret) {
625 			pr_err("Close failed: teardown gpadl return %d\n", ret);
626 			/*
627 			 * If we failed to teardown gpadl,
628 			 * it is perhaps better to leak memory.
629 			 */
630 			goto out;
631 		}
632 	}
633 
634 	/* Cleanup the ring buffers for this channel */
635 	hv_ringbuffer_cleanup(&channel->outbound);
636 	hv_ringbuffer_cleanup(&channel->inbound);
637 
638 	free_pages((unsigned long)channel->ringbuffer_pages,
639 		get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
640 
641 out:
642 	/* re-enable tasklet for use on re-open */
643 	tasklet_enable(&channel->callback_event);
644 	return ret;
645 }
646 
647 /*
648  * vmbus_close - Close the specified channel
649  */
650 void vmbus_close(struct vmbus_channel *channel)
651 {
652 	struct list_head *cur, *tmp;
653 	struct vmbus_channel *cur_channel;
654 
655 	if (channel->primary_channel != NULL) {
656 		/*
657 		 * We will only close sub-channels when
658 		 * the primary is closed.
659 		 */
660 		return;
661 	}
662 	/*
663 	 * Close all the sub-channels first and then close the
664 	 * primary channel.
665 	 */
666 	list_for_each_safe(cur, tmp, &channel->sc_list) {
667 		cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
668 		if (cur_channel->rescind) {
669 			wait_for_completion(&cur_channel->rescind_event);
670 			mutex_lock(&vmbus_connection.channel_mutex);
671 			vmbus_close_internal(cur_channel);
672 			hv_process_channel_removal(
673 					   cur_channel->offermsg.child_relid);
674 		} else {
675 			mutex_lock(&vmbus_connection.channel_mutex);
676 			vmbus_close_internal(cur_channel);
677 		}
678 		mutex_unlock(&vmbus_connection.channel_mutex);
679 	}
680 	/*
681 	 * Now close the primary.
682 	 */
683 	mutex_lock(&vmbus_connection.channel_mutex);
684 	vmbus_close_internal(channel);
685 	mutex_unlock(&vmbus_connection.channel_mutex);
686 }
687 EXPORT_SYMBOL_GPL(vmbus_close);
688 
689 /**
690  * vmbus_sendpacket() - Send the specified buffer on the given channel
691  * @channel: Pointer to vmbus_channel structure.
692  * @buffer: Pointer to the buffer you want to receive the data into.
693  * @bufferlen: Maximum size of what the the buffer will hold
694  * @requestid: Identifier of the request
695  * @type: Type of packet that is being send e.g. negotiate, time
696  * packet etc.
697  *
698  * Sends data in @buffer directly to hyper-v via the vmbus
699  * This will send the data unparsed to hyper-v.
700  *
701  * Mainly used by Hyper-V drivers.
702  */
703 int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
704 			   u32 bufferlen, u64 requestid,
705 			   enum vmbus_packet_type type, u32 flags)
706 {
707 	struct vmpacket_descriptor desc;
708 	u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
709 	u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
710 	struct kvec bufferlist[3];
711 	u64 aligned_data = 0;
712 	int num_vecs = ((bufferlen != 0) ? 3 : 1);
713 
714 
715 	/* Setup the descriptor */
716 	desc.type = type; /* VmbusPacketTypeDataInBand; */
717 	desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
718 	/* in 8-bytes granularity */
719 	desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
720 	desc.len8 = (u16)(packetlen_aligned >> 3);
721 	desc.trans_id = requestid;
722 
723 	bufferlist[0].iov_base = &desc;
724 	bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
725 	bufferlist[1].iov_base = buffer;
726 	bufferlist[1].iov_len = bufferlen;
727 	bufferlist[2].iov_base = &aligned_data;
728 	bufferlist[2].iov_len = (packetlen_aligned - packetlen);
729 
730 	return hv_ringbuffer_write(channel, bufferlist, num_vecs);
731 }
732 EXPORT_SYMBOL(vmbus_sendpacket);
733 
734 /*
735  * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
736  * packets using a GPADL Direct packet type. This interface allows you
737  * to control notifying the host. This will be useful for sending
738  * batched data. Also the sender can control the send flags
739  * explicitly.
740  */
741 int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
742 				struct hv_page_buffer pagebuffers[],
743 				u32 pagecount, void *buffer, u32 bufferlen,
744 				u64 requestid)
745 {
746 	int i;
747 	struct vmbus_channel_packet_page_buffer desc;
748 	u32 descsize;
749 	u32 packetlen;
750 	u32 packetlen_aligned;
751 	struct kvec bufferlist[3];
752 	u64 aligned_data = 0;
753 
754 	if (pagecount > MAX_PAGE_BUFFER_COUNT)
755 		return -EINVAL;
756 
757 	/*
758 	 * Adjust the size down since vmbus_channel_packet_page_buffer is the
759 	 * largest size we support
760 	 */
761 	descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
762 			  ((MAX_PAGE_BUFFER_COUNT - pagecount) *
763 			  sizeof(struct hv_page_buffer));
764 	packetlen = descsize + bufferlen;
765 	packetlen_aligned = ALIGN(packetlen, sizeof(u64));
766 
767 	/* Setup the descriptor */
768 	desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
769 	desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
770 	desc.dataoffset8 = descsize >> 3; /* in 8-bytes granularity */
771 	desc.length8 = (u16)(packetlen_aligned >> 3);
772 	desc.transactionid = requestid;
773 	desc.reserved = 0;
774 	desc.rangecount = pagecount;
775 
776 	for (i = 0; i < pagecount; i++) {
777 		desc.range[i].len = pagebuffers[i].len;
778 		desc.range[i].offset = pagebuffers[i].offset;
779 		desc.range[i].pfn	 = pagebuffers[i].pfn;
780 	}
781 
782 	bufferlist[0].iov_base = &desc;
783 	bufferlist[0].iov_len = descsize;
784 	bufferlist[1].iov_base = buffer;
785 	bufferlist[1].iov_len = bufferlen;
786 	bufferlist[2].iov_base = &aligned_data;
787 	bufferlist[2].iov_len = (packetlen_aligned - packetlen);
788 
789 	return hv_ringbuffer_write(channel, bufferlist, 3);
790 }
791 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
792 
793 /*
794  * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
795  * using a GPADL Direct packet type.
796  * The buffer includes the vmbus descriptor.
797  */
798 int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
799 			      struct vmbus_packet_mpb_array *desc,
800 			      u32 desc_size,
801 			      void *buffer, u32 bufferlen, u64 requestid)
802 {
803 	u32 packetlen;
804 	u32 packetlen_aligned;
805 	struct kvec bufferlist[3];
806 	u64 aligned_data = 0;
807 
808 	packetlen = desc_size + bufferlen;
809 	packetlen_aligned = ALIGN(packetlen, sizeof(u64));
810 
811 	/* Setup the descriptor */
812 	desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
813 	desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
814 	desc->dataoffset8 = desc_size >> 3; /* in 8-bytes granularity */
815 	desc->length8 = (u16)(packetlen_aligned >> 3);
816 	desc->transactionid = requestid;
817 	desc->reserved = 0;
818 	desc->rangecount = 1;
819 
820 	bufferlist[0].iov_base = desc;
821 	bufferlist[0].iov_len = desc_size;
822 	bufferlist[1].iov_base = buffer;
823 	bufferlist[1].iov_len = bufferlen;
824 	bufferlist[2].iov_base = &aligned_data;
825 	bufferlist[2].iov_len = (packetlen_aligned - packetlen);
826 
827 	return hv_ringbuffer_write(channel, bufferlist, 3);
828 }
829 EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
830 
831 /**
832  * vmbus_recvpacket() - Retrieve the user packet on the specified channel
833  * @channel: Pointer to vmbus_channel structure.
834  * @buffer: Pointer to the buffer you want to receive the data into.
835  * @bufferlen: Maximum size of what the the buffer will hold
836  * @buffer_actual_len: The actual size of the data after it was received
837  * @requestid: Identifier of the request
838  *
839  * Receives directly from the hyper-v vmbus and puts the data it received
840  * into Buffer. This will receive the data unparsed from hyper-v.
841  *
842  * Mainly used by Hyper-V drivers.
843  */
844 static inline int
845 __vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
846 		   u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
847 		   bool raw)
848 {
849 	return hv_ringbuffer_read(channel, buffer, bufferlen,
850 				  buffer_actual_len, requestid, raw);
851 
852 }
853 
854 int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
855 		     u32 bufferlen, u32 *buffer_actual_len,
856 		     u64 *requestid)
857 {
858 	return __vmbus_recvpacket(channel, buffer, bufferlen,
859 				  buffer_actual_len, requestid, false);
860 }
861 EXPORT_SYMBOL(vmbus_recvpacket);
862 
863 /*
864  * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
865  */
866 int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
867 			      u32 bufferlen, u32 *buffer_actual_len,
868 			      u64 *requestid)
869 {
870 	return __vmbus_recvpacket(channel, buffer, bufferlen,
871 				  buffer_actual_len, requestid, true);
872 }
873 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);
874