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