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