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