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