sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / hv / channel.c
blob5fb4c6d9209b753a12dcddb4c441ff00c3e8746a
1 /*
2 * Copyright (c) 2009, Microsoft Corporation.
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.
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.
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.
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
33 #include "hyperv_vmbus.h"
35 #define NUM_PAGES_SPANNED(addr, len) \
36 ((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
39 * vmbus_setevent- Trigger an event notification on the specified
40 * channel.
42 void vmbus_setevent(struct vmbus_channel *channel)
44 struct hv_monitor_page *monitorpage;
47 * For channels marked as in "low latency" mode
48 * bypass the monitor page mechanism.
50 if ((channel->offermsg.monitor_allocated) &&
51 (!channel->low_latency)) {
52 /* Each u32 represents 32 channels */
53 sync_set_bit(channel->offermsg.child_relid & 31,
54 (unsigned long *) vmbus_connection.send_int_page +
55 (channel->offermsg.child_relid >> 5));
57 /* Get the child to parent monitor page */
58 monitorpage = vmbus_connection.monitor_pages[1];
60 sync_set_bit(channel->monitor_bit,
61 (unsigned long *)&monitorpage->trigger_group
62 [channel->monitor_grp].pending);
64 } else {
65 vmbus_set_event(channel);
68 EXPORT_SYMBOL_GPL(vmbus_setevent);
71 * vmbus_open - Open the specified channel.
73 int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
74 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
75 void (*onchannelcallback)(void *context), void *context)
77 struct vmbus_channel_open_channel *open_msg;
78 struct vmbus_channel_msginfo *open_info = NULL;
79 unsigned long flags;
80 int ret, err = 0;
81 struct page *page;
83 if (send_ringbuffer_size % PAGE_SIZE ||
84 recv_ringbuffer_size % PAGE_SIZE)
85 return -EINVAL;
87 spin_lock_irqsave(&newchannel->lock, flags);
88 if (newchannel->state == CHANNEL_OPEN_STATE) {
89 newchannel->state = CHANNEL_OPENING_STATE;
90 } else {
91 spin_unlock_irqrestore(&newchannel->lock, flags);
92 return -EINVAL;
94 spin_unlock_irqrestore(&newchannel->lock, flags);
96 newchannel->onchannel_callback = onchannelcallback;
97 newchannel->channel_callback_context = context;
99 /* Allocate the ring buffer */
100 page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
101 GFP_KERNEL|__GFP_ZERO,
102 get_order(send_ringbuffer_size +
103 recv_ringbuffer_size));
105 if (!page)
106 page = alloc_pages(GFP_KERNEL|__GFP_ZERO,
107 get_order(send_ringbuffer_size +
108 recv_ringbuffer_size));
110 if (!page) {
111 err = -ENOMEM;
112 goto error_set_chnstate;
115 newchannel->ringbuffer_pages = page_address(page);
116 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
117 recv_ringbuffer_size) >> PAGE_SHIFT;
119 ret = hv_ringbuffer_init(&newchannel->outbound, page,
120 send_ringbuffer_size >> PAGE_SHIFT);
122 if (ret != 0) {
123 err = ret;
124 goto error_free_pages;
127 ret = hv_ringbuffer_init(&newchannel->inbound,
128 &page[send_ringbuffer_size >> PAGE_SHIFT],
129 recv_ringbuffer_size >> PAGE_SHIFT);
130 if (ret != 0) {
131 err = ret;
132 goto error_free_pages;
136 /* Establish the gpadl for the ring buffer */
137 newchannel->ringbuffer_gpadlhandle = 0;
139 ret = vmbus_establish_gpadl(newchannel,
140 page_address(page),
141 send_ringbuffer_size +
142 recv_ringbuffer_size,
143 &newchannel->ringbuffer_gpadlhandle);
145 if (ret != 0) {
146 err = ret;
147 goto error_free_pages;
150 /* Create and init the channel open message */
151 open_info = kmalloc(sizeof(*open_info) +
152 sizeof(struct vmbus_channel_open_channel),
153 GFP_KERNEL);
154 if (!open_info) {
155 err = -ENOMEM;
156 goto error_free_gpadl;
159 init_completion(&open_info->waitevent);
161 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
162 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
163 open_msg->openid = newchannel->offermsg.child_relid;
164 open_msg->child_relid = newchannel->offermsg.child_relid;
165 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
166 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
167 PAGE_SHIFT;
168 open_msg->target_vp = newchannel->target_vp;
170 if (userdatalen > MAX_USER_DEFINED_BYTES) {
171 err = -EINVAL;
172 goto error_free_gpadl;
175 if (userdatalen)
176 memcpy(open_msg->userdata, userdata, userdatalen);
178 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
179 list_add_tail(&open_info->msglistentry,
180 &vmbus_connection.chn_msg_list);
181 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
183 ret = vmbus_post_msg(open_msg,
184 sizeof(struct vmbus_channel_open_channel));
186 if (ret != 0) {
187 err = ret;
188 goto error_clean_msglist;
191 wait_for_completion(&open_info->waitevent);
193 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
194 list_del(&open_info->msglistentry);
195 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
197 if (open_info->response.open_result.status) {
198 err = -EAGAIN;
199 goto error_free_gpadl;
202 newchannel->state = CHANNEL_OPENED_STATE;
203 kfree(open_info);
204 return 0;
206 error_clean_msglist:
207 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
208 list_del(&open_info->msglistentry);
209 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
211 error_free_gpadl:
212 vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
213 kfree(open_info);
214 error_free_pages:
215 hv_ringbuffer_cleanup(&newchannel->outbound);
216 hv_ringbuffer_cleanup(&newchannel->inbound);
217 __free_pages(page,
218 get_order(send_ringbuffer_size + recv_ringbuffer_size));
219 error_set_chnstate:
220 newchannel->state = CHANNEL_OPEN_STATE;
221 return err;
223 EXPORT_SYMBOL_GPL(vmbus_open);
225 /* Used for Hyper-V Socket: a guest client's connect() to the host */
226 int vmbus_send_tl_connect_request(const uuid_le *shv_guest_servie_id,
227 const uuid_le *shv_host_servie_id)
229 struct vmbus_channel_tl_connect_request conn_msg;
231 memset(&conn_msg, 0, sizeof(conn_msg));
232 conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST;
233 conn_msg.guest_endpoint_id = *shv_guest_servie_id;
234 conn_msg.host_service_id = *shv_host_servie_id;
236 return vmbus_post_msg(&conn_msg, sizeof(conn_msg));
238 EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request);
241 * create_gpadl_header - Creates a gpadl for the specified buffer
243 static int create_gpadl_header(void *kbuffer, u32 size,
244 struct vmbus_channel_msginfo **msginfo)
246 int i;
247 int pagecount;
248 struct vmbus_channel_gpadl_header *gpadl_header;
249 struct vmbus_channel_gpadl_body *gpadl_body;
250 struct vmbus_channel_msginfo *msgheader;
251 struct vmbus_channel_msginfo *msgbody = NULL;
252 u32 msgsize;
254 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
256 pagecount = size >> PAGE_SHIFT;
258 /* do we need a gpadl body msg */
259 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
260 sizeof(struct vmbus_channel_gpadl_header) -
261 sizeof(struct gpa_range);
262 pfncount = pfnsize / sizeof(u64);
264 if (pagecount > pfncount) {
265 /* we need a gpadl body */
266 /* fill in the header */
267 msgsize = sizeof(struct vmbus_channel_msginfo) +
268 sizeof(struct vmbus_channel_gpadl_header) +
269 sizeof(struct gpa_range) + pfncount * sizeof(u64);
270 msgheader = kzalloc(msgsize, GFP_KERNEL);
271 if (!msgheader)
272 goto nomem;
274 INIT_LIST_HEAD(&msgheader->submsglist);
275 msgheader->msgsize = msgsize;
277 gpadl_header = (struct vmbus_channel_gpadl_header *)
278 msgheader->msg;
279 gpadl_header->rangecount = 1;
280 gpadl_header->range_buflen = sizeof(struct gpa_range) +
281 pagecount * sizeof(u64);
282 gpadl_header->range[0].byte_offset = 0;
283 gpadl_header->range[0].byte_count = size;
284 for (i = 0; i < pfncount; i++)
285 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
286 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
287 *msginfo = msgheader;
289 pfnsum = pfncount;
290 pfnleft = pagecount - pfncount;
292 /* how many pfns can we fit */
293 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
294 sizeof(struct vmbus_channel_gpadl_body);
295 pfncount = pfnsize / sizeof(u64);
297 /* fill in the body */
298 while (pfnleft) {
299 if (pfnleft > pfncount)
300 pfncurr = pfncount;
301 else
302 pfncurr = pfnleft;
304 msgsize = sizeof(struct vmbus_channel_msginfo) +
305 sizeof(struct vmbus_channel_gpadl_body) +
306 pfncurr * sizeof(u64);
307 msgbody = kzalloc(msgsize, GFP_KERNEL);
309 if (!msgbody) {
310 struct vmbus_channel_msginfo *pos = NULL;
311 struct vmbus_channel_msginfo *tmp = NULL;
313 * Free up all the allocated messages.
315 list_for_each_entry_safe(pos, tmp,
316 &msgheader->submsglist,
317 msglistentry) {
319 list_del(&pos->msglistentry);
320 kfree(pos);
323 goto nomem;
326 msgbody->msgsize = msgsize;
327 gpadl_body =
328 (struct vmbus_channel_gpadl_body *)msgbody->msg;
331 * Gpadl is u32 and we are using a pointer which could
332 * be 64-bit
333 * This is governed by the guest/host protocol and
334 * so the hypervisor gurantees that this is ok.
336 for (i = 0; i < pfncurr; i++)
337 gpadl_body->pfn[i] = slow_virt_to_phys(
338 kbuffer + PAGE_SIZE * (pfnsum + i)) >>
339 PAGE_SHIFT;
341 /* add to msg header */
342 list_add_tail(&msgbody->msglistentry,
343 &msgheader->submsglist);
344 pfnsum += pfncurr;
345 pfnleft -= pfncurr;
347 } else {
348 /* everything fits in a header */
349 msgsize = sizeof(struct vmbus_channel_msginfo) +
350 sizeof(struct vmbus_channel_gpadl_header) +
351 sizeof(struct gpa_range) + pagecount * sizeof(u64);
352 msgheader = kzalloc(msgsize, GFP_KERNEL);
353 if (msgheader == NULL)
354 goto nomem;
356 INIT_LIST_HEAD(&msgheader->submsglist);
357 msgheader->msgsize = msgsize;
359 gpadl_header = (struct vmbus_channel_gpadl_header *)
360 msgheader->msg;
361 gpadl_header->rangecount = 1;
362 gpadl_header->range_buflen = sizeof(struct gpa_range) +
363 pagecount * sizeof(u64);
364 gpadl_header->range[0].byte_offset = 0;
365 gpadl_header->range[0].byte_count = size;
366 for (i = 0; i < pagecount; i++)
367 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
368 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
370 *msginfo = msgheader;
373 return 0;
374 nomem:
375 kfree(msgheader);
376 kfree(msgbody);
377 return -ENOMEM;
381 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
383 * @channel: a channel
384 * @kbuffer: from kmalloc or vmalloc
385 * @size: page-size multiple
386 * @gpadl_handle: some funky thing
388 int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
389 u32 size, u32 *gpadl_handle)
391 struct vmbus_channel_gpadl_header *gpadlmsg;
392 struct vmbus_channel_gpadl_body *gpadl_body;
393 struct vmbus_channel_msginfo *msginfo = NULL;
394 struct vmbus_channel_msginfo *submsginfo, *tmp;
395 struct list_head *curr;
396 u32 next_gpadl_handle;
397 unsigned long flags;
398 int ret = 0;
400 next_gpadl_handle =
401 (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
403 ret = create_gpadl_header(kbuffer, size, &msginfo);
404 if (ret)
405 return ret;
407 init_completion(&msginfo->waitevent);
409 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
410 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
411 gpadlmsg->child_relid = channel->offermsg.child_relid;
412 gpadlmsg->gpadl = next_gpadl_handle;
415 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
416 list_add_tail(&msginfo->msglistentry,
417 &vmbus_connection.chn_msg_list);
419 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
421 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
422 sizeof(*msginfo));
423 if (ret != 0)
424 goto cleanup;
426 list_for_each(curr, &msginfo->submsglist) {
427 submsginfo = (struct vmbus_channel_msginfo *)curr;
428 gpadl_body =
429 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
431 gpadl_body->header.msgtype =
432 CHANNELMSG_GPADL_BODY;
433 gpadl_body->gpadl = next_gpadl_handle;
435 ret = vmbus_post_msg(gpadl_body,
436 submsginfo->msgsize -
437 sizeof(*submsginfo));
438 if (ret != 0)
439 goto cleanup;
442 wait_for_completion(&msginfo->waitevent);
444 /* At this point, we received the gpadl created msg */
445 *gpadl_handle = gpadlmsg->gpadl;
447 cleanup:
448 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
449 list_del(&msginfo->msglistentry);
450 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
451 list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
452 msglistentry) {
453 kfree(submsginfo);
456 kfree(msginfo);
457 return ret;
459 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
462 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
464 int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
466 struct vmbus_channel_gpadl_teardown *msg;
467 struct vmbus_channel_msginfo *info;
468 unsigned long flags;
469 int ret;
471 info = kmalloc(sizeof(*info) +
472 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
473 if (!info)
474 return -ENOMEM;
476 init_completion(&info->waitevent);
478 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
480 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
481 msg->child_relid = channel->offermsg.child_relid;
482 msg->gpadl = gpadl_handle;
484 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
485 list_add_tail(&info->msglistentry,
486 &vmbus_connection.chn_msg_list);
487 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
488 ret = vmbus_post_msg(msg,
489 sizeof(struct vmbus_channel_gpadl_teardown));
491 if (ret)
492 goto post_msg_err;
494 wait_for_completion(&info->waitevent);
496 post_msg_err:
497 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
498 list_del(&info->msglistentry);
499 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
501 kfree(info);
502 return ret;
504 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
506 static void reset_channel_cb(void *arg)
508 struct vmbus_channel *channel = arg;
510 channel->onchannel_callback = NULL;
513 static int vmbus_close_internal(struct vmbus_channel *channel)
515 struct vmbus_channel_close_channel *msg;
516 int ret;
519 * process_chn_event(), running in the tasklet, can race
520 * with vmbus_close_internal() in the case of SMP guest, e.g., when
521 * the former is accessing channel->inbound.ring_buffer, the latter
522 * could be freeing the ring_buffer pages.
524 * To resolve the race, we can serialize them by disabling the
525 * tasklet when the latter is running here.
527 hv_event_tasklet_disable(channel);
530 * In case a device driver's probe() fails (e.g.,
531 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
532 * rescinded later (e.g., we dynamically disble an Integrated Service
533 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
534 * here we should skip most of the below cleanup work.
536 if (channel->state != CHANNEL_OPENED_STATE) {
537 ret = -EINVAL;
538 goto out;
541 channel->state = CHANNEL_OPEN_STATE;
542 channel->sc_creation_callback = NULL;
543 /* Stop callback and cancel the timer asap */
544 if (channel->target_cpu != get_cpu()) {
545 put_cpu();
546 smp_call_function_single(channel->target_cpu, reset_channel_cb,
547 channel, true);
548 } else {
549 reset_channel_cb(channel);
550 put_cpu();
553 /* Send a closing message */
555 msg = &channel->close_msg.msg;
557 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
558 msg->child_relid = channel->offermsg.child_relid;
560 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
562 if (ret) {
563 pr_err("Close failed: close post msg return is %d\n", ret);
565 * If we failed to post the close msg,
566 * it is perhaps better to leak memory.
568 goto out;
571 /* Tear down the gpadl for the channel's ring buffer */
572 if (channel->ringbuffer_gpadlhandle) {
573 ret = vmbus_teardown_gpadl(channel,
574 channel->ringbuffer_gpadlhandle);
575 if (ret) {
576 pr_err("Close failed: teardown gpadl return %d\n", ret);
578 * If we failed to teardown gpadl,
579 * it is perhaps better to leak memory.
581 goto out;
585 /* Cleanup the ring buffers for this channel */
586 hv_ringbuffer_cleanup(&channel->outbound);
587 hv_ringbuffer_cleanup(&channel->inbound);
589 free_pages((unsigned long)channel->ringbuffer_pages,
590 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
592 out:
593 hv_event_tasklet_enable(channel);
595 return ret;
599 * vmbus_close - Close the specified channel
601 void vmbus_close(struct vmbus_channel *channel)
603 struct list_head *cur, *tmp;
604 struct vmbus_channel *cur_channel;
606 if (channel->primary_channel != NULL) {
608 * We will only close sub-channels when
609 * the primary is closed.
611 return;
614 * Close all the sub-channels first and then close the
615 * primary channel.
617 list_for_each_safe(cur, tmp, &channel->sc_list) {
618 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
619 if (cur_channel->state != CHANNEL_OPENED_STATE)
620 continue;
621 vmbus_close_internal(cur_channel);
624 * Now close the primary.
626 vmbus_close_internal(channel);
628 EXPORT_SYMBOL_GPL(vmbus_close);
630 int vmbus_sendpacket_ctl(struct vmbus_channel *channel, void *buffer,
631 u32 bufferlen, u64 requestid,
632 enum vmbus_packet_type type, u32 flags, bool kick_q)
634 struct vmpacket_descriptor desc;
635 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
636 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
637 struct kvec bufferlist[3];
638 u64 aligned_data = 0;
639 bool lock = channel->acquire_ring_lock;
640 int num_vecs = ((bufferlen != 0) ? 3 : 1);
643 /* Setup the descriptor */
644 desc.type = type; /* VmbusPacketTypeDataInBand; */
645 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
646 /* in 8-bytes granularity */
647 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
648 desc.len8 = (u16)(packetlen_aligned >> 3);
649 desc.trans_id = requestid;
651 bufferlist[0].iov_base = &desc;
652 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
653 bufferlist[1].iov_base = buffer;
654 bufferlist[1].iov_len = bufferlen;
655 bufferlist[2].iov_base = &aligned_data;
656 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
658 return hv_ringbuffer_write(channel, bufferlist, num_vecs,
659 lock, kick_q);
662 EXPORT_SYMBOL(vmbus_sendpacket_ctl);
665 * vmbus_sendpacket() - Send the specified buffer on the given channel
666 * @channel: Pointer to vmbus_channel structure.
667 * @buffer: Pointer to the buffer you want to receive the data into.
668 * @bufferlen: Maximum size of what the the buffer will hold
669 * @requestid: Identifier of the request
670 * @type: Type of packet that is being send e.g. negotiate, time
671 * packet etc.
673 * Sends data in @buffer directly to hyper-v via the vmbus
674 * This will send the data unparsed to hyper-v.
676 * Mainly used by Hyper-V drivers.
678 int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
679 u32 bufferlen, u64 requestid,
680 enum vmbus_packet_type type, u32 flags)
682 return vmbus_sendpacket_ctl(channel, buffer, bufferlen, requestid,
683 type, flags, true);
685 EXPORT_SYMBOL(vmbus_sendpacket);
688 * vmbus_sendpacket_pagebuffer_ctl - Send a range of single-page buffer
689 * packets using a GPADL Direct packet type. This interface allows you
690 * to control notifying the host. This will be useful for sending
691 * batched data. Also the sender can control the send flags
692 * explicitly.
694 int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel *channel,
695 struct hv_page_buffer pagebuffers[],
696 u32 pagecount, void *buffer, u32 bufferlen,
697 u64 requestid,
698 u32 flags,
699 bool kick_q)
701 int i;
702 struct vmbus_channel_packet_page_buffer desc;
703 u32 descsize;
704 u32 packetlen;
705 u32 packetlen_aligned;
706 struct kvec bufferlist[3];
707 u64 aligned_data = 0;
708 bool lock = channel->acquire_ring_lock;
710 if (pagecount > MAX_PAGE_BUFFER_COUNT)
711 return -EINVAL;
715 * Adjust the size down since vmbus_channel_packet_page_buffer is the
716 * largest size we support
718 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
719 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
720 sizeof(struct hv_page_buffer));
721 packetlen = descsize + bufferlen;
722 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
724 /* Setup the descriptor */
725 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
726 desc.flags = flags;
727 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
728 desc.length8 = (u16)(packetlen_aligned >> 3);
729 desc.transactionid = requestid;
730 desc.rangecount = pagecount;
732 for (i = 0; i < pagecount; i++) {
733 desc.range[i].len = pagebuffers[i].len;
734 desc.range[i].offset = pagebuffers[i].offset;
735 desc.range[i].pfn = pagebuffers[i].pfn;
738 bufferlist[0].iov_base = &desc;
739 bufferlist[0].iov_len = descsize;
740 bufferlist[1].iov_base = buffer;
741 bufferlist[1].iov_len = bufferlen;
742 bufferlist[2].iov_base = &aligned_data;
743 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
745 return hv_ringbuffer_write(channel, bufferlist, 3,
746 lock, kick_q);
748 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer_ctl);
751 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
752 * packets using a GPADL Direct packet type.
754 int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
755 struct hv_page_buffer pagebuffers[],
756 u32 pagecount, void *buffer, u32 bufferlen,
757 u64 requestid)
759 u32 flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
760 return vmbus_sendpacket_pagebuffer_ctl(channel, pagebuffers, pagecount,
761 buffer, bufferlen, requestid,
762 flags, true);
765 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
768 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
769 * using a GPADL Direct packet type.
770 * The buffer includes the vmbus descriptor.
772 int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
773 struct vmbus_packet_mpb_array *desc,
774 u32 desc_size,
775 void *buffer, u32 bufferlen, u64 requestid)
777 u32 packetlen;
778 u32 packetlen_aligned;
779 struct kvec bufferlist[3];
780 u64 aligned_data = 0;
781 bool lock = channel->acquire_ring_lock;
783 packetlen = desc_size + bufferlen;
784 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
786 /* Setup the descriptor */
787 desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
788 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
789 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes grandularity */
790 desc->length8 = (u16)(packetlen_aligned >> 3);
791 desc->transactionid = requestid;
792 desc->rangecount = 1;
794 bufferlist[0].iov_base = desc;
795 bufferlist[0].iov_len = desc_size;
796 bufferlist[1].iov_base = buffer;
797 bufferlist[1].iov_len = bufferlen;
798 bufferlist[2].iov_base = &aligned_data;
799 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
801 return hv_ringbuffer_write(channel, bufferlist, 3,
802 lock, true);
804 EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
807 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
808 * using a GPADL Direct packet type.
810 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
811 struct hv_multipage_buffer *multi_pagebuffer,
812 void *buffer, u32 bufferlen, u64 requestid)
814 struct vmbus_channel_packet_multipage_buffer desc;
815 u32 descsize;
816 u32 packetlen;
817 u32 packetlen_aligned;
818 struct kvec bufferlist[3];
819 u64 aligned_data = 0;
820 bool lock = channel->acquire_ring_lock;
821 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
822 multi_pagebuffer->len);
824 if (pfncount > MAX_MULTIPAGE_BUFFER_COUNT)
825 return -EINVAL;
828 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
829 * the largest size we support
831 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
832 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
833 sizeof(u64));
834 packetlen = descsize + bufferlen;
835 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
838 /* Setup the descriptor */
839 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
840 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
841 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
842 desc.length8 = (u16)(packetlen_aligned >> 3);
843 desc.transactionid = requestid;
844 desc.rangecount = 1;
846 desc.range.len = multi_pagebuffer->len;
847 desc.range.offset = multi_pagebuffer->offset;
849 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
850 pfncount * sizeof(u64));
852 bufferlist[0].iov_base = &desc;
853 bufferlist[0].iov_len = descsize;
854 bufferlist[1].iov_base = buffer;
855 bufferlist[1].iov_len = bufferlen;
856 bufferlist[2].iov_base = &aligned_data;
857 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
859 return hv_ringbuffer_write(channel, bufferlist, 3,
860 lock, true);
862 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
865 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
866 * @channel: Pointer to vmbus_channel structure.
867 * @buffer: Pointer to the buffer you want to receive the data into.
868 * @bufferlen: Maximum size of what the the buffer will hold
869 * @buffer_actual_len: The actual size of the data after it was received
870 * @requestid: Identifier of the request
872 * Receives directly from the hyper-v vmbus and puts the data it received
873 * into Buffer. This will receive the data unparsed from hyper-v.
875 * Mainly used by Hyper-V drivers.
877 static inline int
878 __vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
879 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
880 bool raw)
882 return hv_ringbuffer_read(channel, buffer, bufferlen,
883 buffer_actual_len, requestid, raw);
887 int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
888 u32 bufferlen, u32 *buffer_actual_len,
889 u64 *requestid)
891 return __vmbus_recvpacket(channel, buffer, bufferlen,
892 buffer_actual_len, requestid, false);
894 EXPORT_SYMBOL(vmbus_recvpacket);
897 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
899 int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
900 u32 bufferlen, u32 *buffer_actual_len,
901 u64 *requestid)
903 return __vmbus_recvpacket(channel, buffer, bufferlen,
904 buffer_actual_len, requestid, true);
906 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);