Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1 | /* |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 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> |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 20 | */ |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 21 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 22 | |
Greg Kroah-Hartman | 5654e93 | 2009-07-14 15:08:20 -0700 | [diff] [blame] | 23 | #include <linux/kernel.h> |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 24 | #include <linux/sched.h> |
| 25 | #include <linux/wait.h> |
Greg Kroah-Hartman | a0086dc | 2009-08-17 17:22:08 -0700 | [diff] [blame] | 26 | #include <linux/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 28 | #include <linux/module.h> |
Greg Kroah-Hartman | 46a9719 | 2011-10-04 12:29:52 -0700 | [diff] [blame] | 29 | #include <linux/hyperv.h> |
K. Y. Srinivasan | 011a7c3 | 2014-02-01 19:02:20 -0800 | [diff] [blame] | 30 | #include <linux/uio.h> |
K. Y. Srinivasan | 3f335ea | 2011-05-12 19:34:15 -0700 | [diff] [blame] | 31 | |
K. Y. Srinivasan | 0f2a661 | 2011-05-12 19:34:28 -0700 | [diff] [blame] | 32 | #include "hyperv_vmbus.h" |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 33 | |
K. Y. Srinivasan | e3fe0bb | 2011-02-11 10:00:12 -0800 | [diff] [blame] | 34 | #define NUM_PAGES_SPANNED(addr, len) \ |
| 35 | ((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT)) |
| 36 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 37 | /* |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 38 | * vmbus_setevent- Trigger an event notification on the specified |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 39 | * channel. |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 40 | */ |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 41 | static void vmbus_setevent(struct vmbus_channel *channel) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 42 | { |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 43 | struct hv_monitor_page *monitorpage; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 44 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 45 | if (channel->offermsg.monitor_allocated) { |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 46 | /* Each u32 represents 32 channels */ |
Olaf Hering | 2235658 | 2011-03-21 14:41:37 +0100 | [diff] [blame] | 47 | sync_set_bit(channel->offermsg.child_relid & 31, |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 48 | (unsigned long *) vmbus_connection.send_int_page + |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 49 | (channel->offermsg.child_relid >> 5)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 50 | |
Greg Kroah-Hartman | 8681db4 | 2013-09-13 11:32:55 -0700 | [diff] [blame] | 51 | /* Get the child to parent monitor page */ |
| 52 | monitorpage = vmbus_connection.monitor_pages[1]; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 53 | |
Olaf Hering | 2235658 | 2011-03-21 14:41:37 +0100 | [diff] [blame] | 54 | sync_set_bit(channel->monitor_bit, |
Haiyang Zhang | f6feebe | 2010-11-08 14:04:39 -0800 | [diff] [blame] | 55 | (unsigned long *)&monitorpage->trigger_group |
| 56 | [channel->monitor_grp].pending); |
Bill Pemberton | 7c369f4 | 2009-07-29 17:00:11 -0400 | [diff] [blame] | 57 | |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 58 | } else { |
K. Y. Srinivasan | 21c3bef | 2012-12-01 06:46:43 -0800 | [diff] [blame] | 59 | vmbus_set_event(channel); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 60 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 63 | /* |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 64 | * vmbus_open - Open the specified channel. |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 65 | */ |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 66 | int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size, |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 67 | u32 recv_ringbuffer_size, void *userdata, u32 userdatalen, |
| 68 | void (*onchannelcallback)(void *context), void *context) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 69 | { |
K. Y. Srinivasan | 0987ff6 | 2011-08-25 09:48:55 -0700 | [diff] [blame] | 70 | struct vmbus_channel_open_channel *open_msg; |
K. Y. Srinivasan | 176fb9e | 2011-08-25 09:48:57 -0700 | [diff] [blame] | 71 | struct vmbus_channel_msginfo *open_info = NULL; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 72 | void *in, *out; |
Greg Kroah-Hartman | dd0813b | 2009-07-15 14:56:45 -0700 | [diff] [blame] | 73 | unsigned long flags; |
Nicholas Mc Guire | 08a9513 | 2015-02-27 11:26:01 -0800 | [diff] [blame] | 74 | int ret, err = 0; |
| 75 | unsigned long t; |
K. Y. Srinivasan | 294409d | 2015-05-31 21:27:03 -0700 | [diff] [blame] | 76 | struct page *page; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 77 | |
Vitaly Kuznetsov | 67fae05 | 2015-01-20 16:45:05 +0100 | [diff] [blame] | 78 | spin_lock_irqsave(&newchannel->lock, flags); |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 79 | if (newchannel->state == CHANNEL_OPEN_STATE) { |
| 80 | newchannel->state = CHANNEL_OPENING_STATE; |
| 81 | } else { |
Vitaly Kuznetsov | 67fae05 | 2015-01-20 16:45:05 +0100 | [diff] [blame] | 82 | spin_unlock_irqrestore(&newchannel->lock, flags); |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 83 | return -EINVAL; |
| 84 | } |
Vitaly Kuznetsov | 67fae05 | 2015-01-20 16:45:05 +0100 | [diff] [blame] | 85 | spin_unlock_irqrestore(&newchannel->lock, flags); |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 86 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 87 | newchannel->onchannel_callback = onchannelcallback; |
| 88 | newchannel->channel_callback_context = context; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 89 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 90 | /* Allocate the ring buffer */ |
K. Y. Srinivasan | 294409d | 2015-05-31 21:27:03 -0700 | [diff] [blame] | 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); |
K. Y. Srinivasan | df3493e | 2011-02-11 09:59:00 -0800 | [diff] [blame] | 102 | |
Dexuan Cui | ac0d12b | 2015-02-27 11:26:00 -0800 | [diff] [blame] | 103 | if (!out) { |
| 104 | err = -ENOMEM; |
| 105 | goto error0; |
| 106 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 107 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 108 | in = (void *)((unsigned long)out + send_ringbuffer_size); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 109 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 110 | newchannel->ringbuffer_pages = out; |
| 111 | newchannel->ringbuffer_pagecount = (send_ringbuffer_size + |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 112 | recv_ringbuffer_size) >> PAGE_SHIFT; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 113 | |
K. Y. Srinivasan | 72a95cb | 2011-05-10 07:55:21 -0700 | [diff] [blame] | 114 | ret = hv_ringbuffer_init( |
| 115 | &newchannel->outbound, out, send_ringbuffer_size); |
| 116 | |
Haiyang Zhang | fd4dc88 | 2010-05-13 15:56:30 +0000 | [diff] [blame] | 117 | if (ret != 0) { |
Bill Pemberton | 3324fb4 | 2010-05-05 15:27:49 -0400 | [diff] [blame] | 118 | err = ret; |
K. Y. Srinivasan | 1392550 | 2012-10-12 13:22:42 -0700 | [diff] [blame] | 119 | goto error0; |
Bill Pemberton | 3324fb4 | 2010-05-05 15:27:49 -0400 | [diff] [blame] | 120 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 121 | |
K. Y. Srinivasan | 72a95cb | 2011-05-10 07:55:21 -0700 | [diff] [blame] | 122 | ret = hv_ringbuffer_init( |
| 123 | &newchannel->inbound, in, recv_ringbuffer_size); |
Haiyang Zhang | fd4dc88 | 2010-05-13 15:56:30 +0000 | [diff] [blame] | 124 | if (ret != 0) { |
Bill Pemberton | 3324fb4 | 2010-05-05 15:27:49 -0400 | [diff] [blame] | 125 | err = ret; |
K. Y. Srinivasan | 1392550 | 2012-10-12 13:22:42 -0700 | [diff] [blame] | 126 | goto error0; |
Bill Pemberton | 3324fb4 | 2010-05-05 15:27:49 -0400 | [diff] [blame] | 127 | } |
| 128 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 129 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 130 | /* Establish the gpadl for the ring buffer */ |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 131 | newchannel->ringbuffer_gpadlhandle = 0; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 132 | |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 133 | ret = vmbus_establish_gpadl(newchannel, |
Haiyang Zhang | 82f8bd4 | 2010-11-08 14:04:45 -0800 | [diff] [blame] | 134 | newchannel->outbound.ring_buffer, |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 135 | send_ringbuffer_size + |
| 136 | recv_ringbuffer_size, |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 137 | &newchannel->ringbuffer_gpadlhandle); |
Bill Pemberton | b94ef34 | 2010-05-05 15:27:37 -0400 | [diff] [blame] | 138 | |
Haiyang Zhang | fd4dc88 | 2010-05-13 15:56:30 +0000 | [diff] [blame] | 139 | if (ret != 0) { |
Bill Pemberton | b94ef34 | 2010-05-05 15:27:37 -0400 | [diff] [blame] | 140 | err = ret; |
K. Y. Srinivasan | 1392550 | 2012-10-12 13:22:42 -0700 | [diff] [blame] | 141 | goto error0; |
Bill Pemberton | b94ef34 | 2010-05-05 15:27:37 -0400 | [diff] [blame] | 142 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 143 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 144 | /* Create and init the channel open message */ |
K. Y. Srinivasan | 176fb9e | 2011-08-25 09:48:57 -0700 | [diff] [blame] | 145 | open_info = kmalloc(sizeof(*open_info) + |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 146 | sizeof(struct vmbus_channel_open_channel), |
| 147 | GFP_KERNEL); |
K. Y. Srinivasan | 176fb9e | 2011-08-25 09:48:57 -0700 | [diff] [blame] | 148 | if (!open_info) { |
Bill Pemberton | c3bf2e2 | 2010-05-05 15:27:34 -0400 | [diff] [blame] | 149 | err = -ENOMEM; |
K. Y. Srinivasan | 40384e4 | 2015-02-27 11:26:04 -0800 | [diff] [blame] | 150 | goto error_gpadl; |
Bill Pemberton | c3bf2e2 | 2010-05-05 15:27:34 -0400 | [diff] [blame] | 151 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 152 | |
K. Y. Srinivasan | 176fb9e | 2011-08-25 09:48:57 -0700 | [diff] [blame] | 153 | init_completion(&open_info->waitevent); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 154 | |
K. Y. Srinivasan | 176fb9e | 2011-08-25 09:48:57 -0700 | [diff] [blame] | 155 | open_msg = (struct vmbus_channel_open_channel *)open_info->msg; |
K. Y. Srinivasan | 0987ff6 | 2011-08-25 09:48:55 -0700 | [diff] [blame] | 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 >> |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 161 | PAGE_SHIFT; |
K. Y. Srinivasan | abbf3b2 | 2012-12-01 06:46:48 -0800 | [diff] [blame] | 162 | open_msg->target_vp = newchannel->target_vp; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 163 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 164 | if (userdatalen > MAX_USER_DEFINED_BYTES) { |
Bill Pemberton | c827f94 | 2010-05-05 15:27:38 -0400 | [diff] [blame] | 165 | err = -EINVAL; |
K. Y. Srinivasan | 40384e4 | 2015-02-27 11:26:04 -0800 | [diff] [blame] | 166 | goto error_gpadl; |
Bill Pemberton | c827f94 | 2010-05-05 15:27:38 -0400 | [diff] [blame] | 167 | } |
| 168 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 169 | if (userdatalen) |
K. Y. Srinivasan | 0987ff6 | 2011-08-25 09:48:55 -0700 | [diff] [blame] | 170 | memcpy(open_msg->userdata, userdata, userdatalen); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 171 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 172 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
K. Y. Srinivasan | 176fb9e | 2011-08-25 09:48:57 -0700 | [diff] [blame] | 173 | list_add_tail(&open_info->msglistentry, |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 174 | &vmbus_connection.chn_msg_list); |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 175 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 176 | |
K. Y. Srinivasan | 0987ff6 | 2011-08-25 09:48:55 -0700 | [diff] [blame] | 177 | ret = vmbus_post_msg(open_msg, |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 178 | sizeof(struct vmbus_channel_open_channel)); |
Hank Janssen | 98e0870 | 2011-03-29 13:58:44 -0700 | [diff] [blame] | 179 | |
K. Y. Srinivasan | 45d727c | 2014-08-27 16:25:35 -0700 | [diff] [blame] | 180 | if (ret != 0) { |
| 181 | err = ret; |
K. Y. Srinivasan | 1392550 | 2012-10-12 13:22:42 -0700 | [diff] [blame] | 182 | goto error1; |
K. Y. Srinivasan | 45d727c | 2014-08-27 16:25:35 -0700 | [diff] [blame] | 183 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 184 | |
K. Y. Srinivasan | 176fb9e | 2011-08-25 09:48:57 -0700 | [diff] [blame] | 185 | t = wait_for_completion_timeout(&open_info->waitevent, 5*HZ); |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 186 | if (t == 0) { |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 187 | err = -ETIMEDOUT; |
K. Y. Srinivasan | 1392550 | 2012-10-12 13:22:42 -0700 | [diff] [blame] | 188 | goto error1; |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 191 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
K. Y. Srinivasan | 176fb9e | 2011-08-25 09:48:57 -0700 | [diff] [blame] | 192 | list_del(&open_info->msglistentry); |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 193 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 194 | |
Vitaly Kuznetsov | ffc151f | 2015-05-06 17:47:40 -0700 | [diff] [blame] | 195 | if (open_info->response.open_result.status) { |
| 196 | err = -EAGAIN; |
| 197 | goto error_gpadl; |
| 198 | } |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 199 | |
Vitaly Kuznetsov | ffc151f | 2015-05-06 17:47:40 -0700 | [diff] [blame] | 200 | newchannel->state = CHANNEL_OPENED_STATE; |
K. Y. Srinivasan | 176fb9e | 2011-08-25 09:48:57 -0700 | [diff] [blame] | 201 | kfree(open_info); |
Vitaly Kuznetsov | ffc151f | 2015-05-06 17:47:40 -0700 | [diff] [blame] | 202 | return 0; |
Bill Pemberton | c3bf2e2 | 2010-05-05 15:27:34 -0400 | [diff] [blame] | 203 | |
K. Y. Srinivasan | 1392550 | 2012-10-12 13:22:42 -0700 | [diff] [blame] | 204 | error1: |
| 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 | |
K. Y. Srinivasan | 40384e4 | 2015-02-27 11:26:04 -0800 | [diff] [blame] | 209 | error_gpadl: |
| 210 | vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle); |
| 211 | |
K. Y. Srinivasan | 1392550 | 2012-10-12 13:22:42 -0700 | [diff] [blame] | 212 | error0: |
K. Y. Srinivasan | df3493e | 2011-02-11 09:59:00 -0800 | [diff] [blame] | 213 | free_pages((unsigned long)out, |
| 214 | get_order(send_ringbuffer_size + recv_ringbuffer_size)); |
K. Y. Srinivasan | 176fb9e | 2011-08-25 09:48:57 -0700 | [diff] [blame] | 215 | kfree(open_info); |
Dexuan Cui | ac0d12b | 2015-02-27 11:26:00 -0800 | [diff] [blame] | 216 | newchannel->state = CHANNEL_OPEN_STATE; |
Bill Pemberton | c3bf2e2 | 2010-05-05 15:27:34 -0400 | [diff] [blame] | 217 | return err; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 218 | } |
Greg Kroah-Hartman | 36ceadf | 2010-10-21 09:58:21 -0700 | [diff] [blame] | 219 | EXPORT_SYMBOL_GPL(vmbus_open); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 220 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 221 | /* |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 222 | * create_gpadl_header - Creates a gpadl for the specified buffer |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 223 | */ |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 224 | static int create_gpadl_header(void *kbuffer, u32 size, |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 225 | struct vmbus_channel_msginfo **msginfo, |
| 226 | u32 *messagecount) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 227 | { |
| 228 | int i; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 229 | int pagecount; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 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; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 235 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 236 | int pfnsum, pfncount, pfnleft, pfncurr, pfnsize; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 237 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 238 | pagecount = size >> PAGE_SHIFT; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 239 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 240 | /* do we need a gpadl body msg */ |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 241 | pfnsize = MAX_SIZE_CHANNEL_MESSAGE - |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 242 | sizeof(struct vmbus_channel_gpadl_header) - |
| 243 | sizeof(struct gpa_range); |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 244 | pfncount = pfnsize / sizeof(u64); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 245 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 246 | if (pagecount > pfncount) { |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 247 | /* we need a gpadl body */ |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 248 | /* fill in the header */ |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 249 | msgsize = sizeof(struct vmbus_channel_msginfo) + |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 250 | sizeof(struct vmbus_channel_gpadl_header) + |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 251 | sizeof(struct gpa_range) + pfncount * sizeof(u64); |
| 252 | msgheader = kzalloc(msgsize, GFP_KERNEL); |
| 253 | if (!msgheader) |
Bill Pemberton | d1c250b | 2010-05-05 15:27:35 -0400 | [diff] [blame] | 254 | goto nomem; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 255 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 256 | INIT_LIST_HEAD(&msgheader->submsglist); |
| 257 | msgheader->msgsize = msgsize; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 258 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 259 | gpadl_header = (struct vmbus_channel_gpadl_header *) |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 260 | msgheader->msg; |
| 261 | gpadl_header->rangecount = 1; |
| 262 | gpadl_header->range_buflen = sizeof(struct gpa_range) + |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 263 | pagecount * sizeof(u64); |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 264 | gpadl_header->range[0].byte_offset = 0; |
| 265 | gpadl_header->range[0].byte_count = size; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 266 | for (i = 0; i < pfncount; i++) |
Haiyang Zhang | b679ef7 | 2014-01-27 15:03:42 -0800 | [diff] [blame] | 267 | gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys( |
| 268 | kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 269 | *msginfo = msgheader; |
| 270 | *messagecount = 1; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 271 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 272 | pfnsum = pfncount; |
| 273 | pfnleft = pagecount - pfncount; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 274 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 275 | /* how many pfns can we fit */ |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 276 | pfnsize = MAX_SIZE_CHANNEL_MESSAGE - |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 277 | sizeof(struct vmbus_channel_gpadl_body); |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 278 | pfncount = pfnsize / sizeof(u64); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 279 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 280 | /* fill in the body */ |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 281 | while (pfnleft) { |
| 282 | if (pfnleft > pfncount) |
| 283 | pfncurr = pfncount; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 284 | else |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 285 | pfncurr = pfnleft; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 286 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 287 | msgsize = sizeof(struct vmbus_channel_msginfo) + |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 288 | sizeof(struct vmbus_channel_gpadl_body) + |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 289 | pfncurr * sizeof(u64); |
| 290 | msgbody = kzalloc(msgsize, GFP_KERNEL); |
K. Y. Srinivasan | f38cf9c | 2011-06-06 15:50:06 -0700 | [diff] [blame] | 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 | |
Bill Pemberton | d1c250b | 2010-05-05 15:27:35 -0400 | [diff] [blame] | 306 | goto nomem; |
K. Y. Srinivasan | f38cf9c | 2011-06-06 15:50:06 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 309 | msgbody->msgsize = msgsize; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 310 | (*messagecount)++; |
| 311 | gpadl_body = |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 312 | (struct vmbus_channel_gpadl_body *)msgbody->msg; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 313 | |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 314 | /* |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 315 | * Gpadl is u32 and we are using a pointer which could |
| 316 | * be 64-bit |
K. Y. Srinivasan | f27df64 | 2011-06-06 15:49:56 -0700 | [diff] [blame] | 317 | * This is governed by the guest/host protocol and |
| 318 | * so the hypervisor gurantees that this is ok. |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 319 | */ |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 320 | for (i = 0; i < pfncurr; i++) |
Haiyang Zhang | b679ef7 | 2014-01-27 15:03:42 -0800 | [diff] [blame] | 321 | gpadl_body->pfn[i] = slow_virt_to_phys( |
| 322 | kbuffer + PAGE_SIZE * (pfnsum + i)) >> |
| 323 | PAGE_SHIFT; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 324 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 325 | /* add to msg header */ |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 326 | list_add_tail(&msgbody->msglistentry, |
| 327 | &msgheader->submsglist); |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 328 | pfnsum += pfncurr; |
| 329 | pfnleft -= pfncurr; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 330 | } |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 331 | } else { |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 332 | /* everything fits in a header */ |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 333 | msgsize = sizeof(struct vmbus_channel_msginfo) + |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 334 | sizeof(struct vmbus_channel_gpadl_header) + |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 335 | sizeof(struct gpa_range) + pagecount * sizeof(u64); |
| 336 | msgheader = kzalloc(msgsize, GFP_KERNEL); |
| 337 | if (msgheader == NULL) |
Kulikov Vasiliy | e3eb7cd | 2010-07-16 20:13:51 +0400 | [diff] [blame] | 338 | goto nomem; |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 339 | msgheader->msgsize = msgsize; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 340 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 341 | gpadl_header = (struct vmbus_channel_gpadl_header *) |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 342 | msgheader->msg; |
| 343 | gpadl_header->rangecount = 1; |
| 344 | gpadl_header->range_buflen = sizeof(struct gpa_range) + |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 345 | pagecount * sizeof(u64); |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 346 | gpadl_header->range[0].byte_offset = 0; |
| 347 | gpadl_header->range[0].byte_count = size; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 348 | for (i = 0; i < pagecount; i++) |
Haiyang Zhang | b679ef7 | 2014-01-27 15:03:42 -0800 | [diff] [blame] | 349 | gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys( |
| 350 | kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 351 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 352 | *msginfo = msgheader; |
| 353 | *messagecount = 1; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | return 0; |
Bill Pemberton | d1c250b | 2010-05-05 15:27:35 -0400 | [diff] [blame] | 357 | nomem: |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 358 | kfree(msgheader); |
| 359 | kfree(msgbody); |
Bill Pemberton | d1c250b | 2010-05-05 15:27:35 -0400 | [diff] [blame] | 360 | return -ENOMEM; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 363 | /* |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 364 | * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 365 | * |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 366 | * @channel: a channel |
Haiyang Zhang | b679ef7 | 2014-01-27 15:03:42 -0800 | [diff] [blame] | 367 | * @kbuffer: from kmalloc or vmalloc |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 368 | * @size: page-size multiple |
| 369 | * @gpadl_handle: some funky thing |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 370 | */ |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 371 | int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer, |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 372 | u32 size, u32 *gpadl_handle) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 373 | { |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 374 | struct vmbus_channel_gpadl_header *gpadlmsg; |
| 375 | struct vmbus_channel_gpadl_body *gpadl_body; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 376 | struct vmbus_channel_msginfo *msginfo = NULL; |
| 377 | struct vmbus_channel_msginfo *submsginfo; |
| 378 | u32 msgcount; |
Bill Pemberton | 53af545 | 2009-09-11 21:46:44 -0400 | [diff] [blame] | 379 | struct list_head *curr; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 380 | u32 next_gpadl_handle; |
Greg Kroah-Hartman | dd0813b | 2009-07-15 14:56:45 -0700 | [diff] [blame] | 381 | unsigned long flags; |
Bill Pemberton | c3bf2e2 | 2010-05-05 15:27:34 -0400 | [diff] [blame] | 382 | int ret = 0; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 383 | |
K. Y. Srinivasan | 9f52a16 | 2015-01-09 23:54:33 -0800 | [diff] [blame] | 384 | next_gpadl_handle = |
| 385 | (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 386 | |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 387 | ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount); |
Bill Pemberton | c3bf2e2 | 2010-05-05 15:27:34 -0400 | [diff] [blame] | 388 | if (ret) |
| 389 | return ret; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 390 | |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 391 | init_completion(&msginfo->waitevent); |
Bill Pemberton | c3bf2e2 | 2010-05-05 15:27:34 -0400 | [diff] [blame] | 392 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 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; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 397 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 398 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 399 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 400 | list_add_tail(&msginfo->msglistentry, |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 401 | &vmbus_connection.chn_msg_list); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 402 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 403 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 404 | |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 405 | ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize - |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 406 | sizeof(*msginfo)); |
Hank Janssen | 98e0870 | 2011-03-29 13:58:44 -0700 | [diff] [blame] | 407 | if (ret != 0) |
K. Y. Srinivasan | 00d760b | 2011-06-06 15:50:12 -0700 | [diff] [blame] | 408 | goto cleanup; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 409 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 410 | if (msgcount > 1) { |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 411 | list_for_each(curr, &msginfo->submsglist) { |
Bill Pemberton | 53af545 | 2009-09-11 21:46:44 -0400 | [diff] [blame] | 412 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 413 | submsginfo = (struct vmbus_channel_msginfo *)curr; |
| 414 | gpadl_body = |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 415 | (struct vmbus_channel_gpadl_body *)submsginfo->msg; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 416 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 417 | gpadl_body->header.msgtype = |
| 418 | CHANNELMSG_GPADL_BODY; |
| 419 | gpadl_body->gpadl = next_gpadl_handle; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 420 | |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 421 | ret = vmbus_post_msg(gpadl_body, |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 422 | submsginfo->msgsize - |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 423 | sizeof(*submsginfo)); |
Haiyang Zhang | fd4dc88 | 2010-05-13 15:56:30 +0000 | [diff] [blame] | 424 | if (ret != 0) |
K. Y. Srinivasan | 00d760b | 2011-06-06 15:50:12 -0700 | [diff] [blame] | 425 | goto cleanup; |
Bill Pemberton | 9925915 | 2010-05-05 15:27:36 -0400 | [diff] [blame] | 426 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 427 | } |
| 428 | } |
K. Y. Srinivasan | 72c6b71 | 2014-08-27 16:25:34 -0700 | [diff] [blame] | 429 | wait_for_completion(&msginfo->waitevent); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 430 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 431 | /* At this point, we received the gpadl created msg */ |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 432 | *gpadl_handle = gpadlmsg->gpadl; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 433 | |
K. Y. Srinivasan | 00d760b | 2011-06-06 15:50:12 -0700 | [diff] [blame] | 434 | cleanup: |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 435 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 436 | list_del(&msginfo->msglistentry); |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 437 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 438 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 439 | kfree(msginfo); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 440 | return ret; |
| 441 | } |
Greg Kroah-Hartman | 9887372 | 2010-10-21 08:47:43 -0700 | [diff] [blame] | 442 | EXPORT_SYMBOL_GPL(vmbus_establish_gpadl); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 443 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 444 | /* |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 445 | * vmbus_teardown_gpadl -Teardown the specified GPADL handle |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 446 | */ |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 447 | int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 448 | { |
Greg Kroah-Hartman | 8225021 | 2009-08-26 15:16:04 -0700 | [diff] [blame] | 449 | struct vmbus_channel_gpadl_teardown *msg; |
Greg Kroah-Hartman | aded7165 | 2009-08-18 15:21:19 -0700 | [diff] [blame] | 450 | struct vmbus_channel_msginfo *info; |
Greg Kroah-Hartman | dd0813b | 2009-07-15 14:56:45 -0700 | [diff] [blame] | 451 | unsigned long flags; |
K. Y. Srinivasan | 66be653 | 2014-08-27 16:25:32 -0700 | [diff] [blame] | 452 | int ret; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 453 | |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 454 | info = kmalloc(sizeof(*info) + |
| 455 | sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL); |
Bill Pemberton | c3bf2e2 | 2010-05-05 15:27:34 -0400 | [diff] [blame] | 456 | if (!info) |
| 457 | return -ENOMEM; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 458 | |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 459 | init_completion(&info->waitevent); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 460 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 461 | msg = (struct vmbus_channel_gpadl_teardown *)info->msg; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 462 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 463 | msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN; |
| 464 | msg->child_relid = channel->offermsg.child_relid; |
| 465 | msg->gpadl = gpadl_handle; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 466 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 467 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 468 | list_add_tail(&info->msglistentry, |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 469 | &vmbus_connection.chn_msg_list); |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 470 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 471 | ret = vmbus_post_msg(msg, |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 472 | sizeof(struct vmbus_channel_gpadl_teardown)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 473 | |
K. Y. Srinivasan | 66be653 | 2014-08-27 16:25:32 -0700 | [diff] [blame] | 474 | if (ret) |
| 475 | goto post_msg_err; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 476 | |
K. Y. Srinivasan | 66be653 | 2014-08-27 16:25:32 -0700 | [diff] [blame] | 477 | wait_for_completion(&info->waitevent); |
| 478 | |
| 479 | post_msg_err: |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 480 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 481 | list_del(&info->msglistentry); |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 482 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 483 | |
Greg Kroah-Hartman | 8c69f52 | 2009-07-15 12:48:29 -0700 | [diff] [blame] | 484 | kfree(info); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 485 | return ret; |
| 486 | } |
Greg Kroah-Hartman | 18726d7 | 2010-10-21 08:39:59 -0700 | [diff] [blame] | 487 | EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 488 | |
K. Y. Srinivasan | d3ba720 | 2014-04-08 18:45:53 -0700 | [diff] [blame] | 489 | static void reset_channel_cb(void *arg) |
| 490 | { |
| 491 | struct vmbus_channel *channel = arg; |
| 492 | |
| 493 | channel->onchannel_callback = NULL; |
| 494 | } |
| 495 | |
K. Y. Srinivasan | 98d731b | 2014-08-27 16:25:33 -0700 | [diff] [blame] | 496 | static int vmbus_close_internal(struct vmbus_channel *channel) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 497 | { |
Greg Kroah-Hartman | 8225021 | 2009-08-26 15:16:04 -0700 | [diff] [blame] | 498 | struct vmbus_channel_close_channel *msg; |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 499 | int ret; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 500 | |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 501 | channel->state = CHANNEL_OPEN_STATE; |
| 502 | channel->sc_creation_callback = NULL; |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 503 | /* Stop callback and cancel the timer asap */ |
K. Y. Srinivasan | 2115b56 | 2014-08-28 18:29:53 -0700 | [diff] [blame] | 504 | if (channel->target_cpu != get_cpu()) { |
| 505 | put_cpu(); |
K. Y. Srinivasan | d3ba720 | 2014-04-08 18:45:53 -0700 | [diff] [blame] | 506 | smp_call_function_single(channel->target_cpu, reset_channel_cb, |
| 507 | channel, true); |
K. Y. Srinivasan | 2115b56 | 2014-08-28 18:29:53 -0700 | [diff] [blame] | 508 | } else { |
K. Y. Srinivasan | d3ba720 | 2014-04-08 18:45:53 -0700 | [diff] [blame] | 509 | reset_channel_cb(channel); |
K. Y. Srinivasan | 2115b56 | 2014-08-28 18:29:53 -0700 | [diff] [blame] | 510 | put_cpu(); |
| 511 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 512 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 513 | /* Send a closing message */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 514 | |
K. Y. Srinivasan | e9a27a9 | 2011-06-06 15:49:59 -0700 | [diff] [blame] | 515 | msg = &channel->close_msg.msg; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 516 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 517 | msg->header.msgtype = CHANNELMSG_CLOSECHANNEL; |
| 518 | msg->child_relid = channel->offermsg.child_relid; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 519 | |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 520 | ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 521 | |
K. Y. Srinivasan | 98d731b | 2014-08-27 16:25:33 -0700 | [diff] [blame] | 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 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 531 | /* Tear down the gpadl for the channel's ring buffer */ |
K. Y. Srinivasan | 98d731b | 2014-08-27 16:25:33 -0700 | [diff] [blame] | 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 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 544 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 545 | /* Cleanup the ring buffers for this channel */ |
K. Y. Srinivasan | 2dba688 | 2011-05-10 07:55:22 -0700 | [diff] [blame] | 546 | hv_ringbuffer_cleanup(&channel->outbound); |
| 547 | hv_ringbuffer_cleanup(&channel->inbound); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 548 | |
K. Y. Srinivasan | df3493e | 2011-02-11 09:59:00 -0800 | [diff] [blame] | 549 | free_pages((unsigned long)channel->ringbuffer_pages, |
| 550 | get_order(channel->ringbuffer_pagecount * PAGE_SIZE)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 551 | |
K. Y. Srinivasan | f2eddbc | 2015-03-18 12:29:27 -0700 | [diff] [blame] | 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); |
K. Y. Srinivasan | 98d731b | 2014-08-27 16:25:33 -0700 | [diff] [blame] | 558 | return ret; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 559 | } |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 560 | |
| 561 | /* |
| 562 | * vmbus_close - Close the specified channel |
| 563 | */ |
| 564 | void 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 | } |
Greg Kroah-Hartman | 70bfa30 | 2010-10-21 09:52:22 -0700 | [diff] [blame] | 591 | EXPORT_SYMBOL_GPL(vmbus_close); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 592 | |
K. Y. Srinivasan | e9395e3 | 2015-02-28 11:39:04 -0800 | [diff] [blame] | 593 | int vmbus_sendpacket_ctl(struct vmbus_channel *channel, void *buffer, |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 594 | u32 bufferlen, u64 requestid, |
K. Y. Srinivasan | e9395e3 | 2015-02-28 11:39:04 -0800 | [diff] [blame] | 595 | enum vmbus_packet_type type, u32 flags, bool kick_q) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 596 | { |
Greg Kroah-Hartman | 8dc0a06 | 2009-08-27 16:02:36 -0700 | [diff] [blame] | 597 | struct vmpacket_descriptor desc; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 598 | u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen; |
Uwe Kleine-König | 7350968 | 2011-01-20 09:32:01 +0100 | [diff] [blame] | 599 | u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64)); |
K. Y. Srinivasan | 011a7c3 | 2014-02-01 19:02:20 -0800 | [diff] [blame] | 600 | struct kvec bufferlist[3]; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 601 | u64 aligned_data = 0; |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 602 | int ret; |
K. Y. Srinivasan | 98fa8cf | 2012-12-01 06:46:36 -0800 | [diff] [blame] | 603 | bool signal = false; |
K. Y. Srinivasan | b81658c | 2015-08-01 16:08:14 -0700 | [diff] [blame] | 604 | int num_vecs = ((bufferlen != 0) ? 3 : 1); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 605 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 606 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 607 | /* Setup the descriptor */ |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 608 | desc.type = type; /* VmbusPacketTypeDataInBand; */ |
| 609 | desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */ |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 610 | /* in 8-bytes granularity */ |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 611 | desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3; |
| 612 | desc.len8 = (u16)(packetlen_aligned >> 3); |
| 613 | desc.trans_id = requestid; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 614 | |
K. Y. Srinivasan | 011a7c3 | 2014-02-01 19:02:20 -0800 | [diff] [blame] | 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); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 621 | |
K. Y. Srinivasan | b81658c | 2015-08-01 16:08:14 -0700 | [diff] [blame] | 622 | ret = hv_ringbuffer_write(&channel->outbound, bufferlist, num_vecs, |
| 623 | &signal); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 624 | |
K. Y. Srinivasan | 5f5cc81 | 2015-03-18 12:29:30 -0700 | [diff] [blame] | 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 | * If we cannot write to the ring-buffer; signal the host |
| 634 | * even if we may not have written anything. This is a rare |
| 635 | * enough condition that it should not matter. |
| 636 | */ |
| 637 | if (((ret == 0) && kick_q && signal) || (ret)) |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 638 | vmbus_setevent(channel); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 639 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 640 | return ret; |
| 641 | } |
K. Y. Srinivasan | e9395e3 | 2015-02-28 11:39:04 -0800 | [diff] [blame] | 642 | EXPORT_SYMBOL(vmbus_sendpacket_ctl); |
| 643 | |
| 644 | /** |
| 645 | * vmbus_sendpacket() - Send the specified buffer on the given channel |
| 646 | * @channel: Pointer to vmbus_channel structure. |
| 647 | * @buffer: Pointer to the buffer you want to receive the data into. |
| 648 | * @bufferlen: Maximum size of what the the buffer will hold |
| 649 | * @requestid: Identifier of the request |
| 650 | * @type: Type of packet that is being send e.g. negotiate, time |
| 651 | * packet etc. |
| 652 | * |
| 653 | * Sends data in @buffer directly to hyper-v via the vmbus |
| 654 | * This will send the data unparsed to hyper-v. |
| 655 | * |
| 656 | * Mainly used by Hyper-V drivers. |
| 657 | */ |
| 658 | int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer, |
| 659 | u32 bufferlen, u64 requestid, |
| 660 | enum vmbus_packet_type type, u32 flags) |
| 661 | { |
| 662 | return vmbus_sendpacket_ctl(channel, buffer, bufferlen, requestid, |
| 663 | type, flags, true); |
| 664 | } |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 665 | EXPORT_SYMBOL(vmbus_sendpacket); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 666 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 667 | /* |
K. Y. Srinivasan | 87e93d6 | 2015-02-28 11:39:03 -0800 | [diff] [blame] | 668 | * vmbus_sendpacket_pagebuffer_ctl - Send a range of single-page buffer |
| 669 | * packets using a GPADL Direct packet type. This interface allows you |
| 670 | * to control notifying the host. This will be useful for sending |
| 671 | * batched data. Also the sender can control the send flags |
| 672 | * explicitly. |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 673 | */ |
K. Y. Srinivasan | 87e93d6 | 2015-02-28 11:39:03 -0800 | [diff] [blame] | 674 | int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel *channel, |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 675 | struct hv_page_buffer pagebuffers[], |
| 676 | u32 pagecount, void *buffer, u32 bufferlen, |
K. Y. Srinivasan | 87e93d6 | 2015-02-28 11:39:03 -0800 | [diff] [blame] | 677 | u64 requestid, |
| 678 | u32 flags, |
| 679 | bool kick_q) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 680 | { |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 681 | int ret; |
| 682 | int i; |
Haiyang Zhang | 430a8e9 | 2010-09-20 21:07:51 +0000 | [diff] [blame] | 683 | struct vmbus_channel_packet_page_buffer desc; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 684 | u32 descsize; |
| 685 | u32 packetlen; |
| 686 | u32 packetlen_aligned; |
K. Y. Srinivasan | 011a7c3 | 2014-02-01 19:02:20 -0800 | [diff] [blame] | 687 | struct kvec bufferlist[3]; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 688 | u64 aligned_data = 0; |
K. Y. Srinivasan | 98fa8cf | 2012-12-01 06:46:36 -0800 | [diff] [blame] | 689 | bool signal = false; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 690 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 691 | if (pagecount > MAX_PAGE_BUFFER_COUNT) |
Bill Pemberton | 002b53e | 2010-05-05 15:27:39 -0400 | [diff] [blame] | 692 | return -EINVAL; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 693 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 694 | |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 695 | /* |
Haiyang Zhang | 430a8e9 | 2010-09-20 21:07:51 +0000 | [diff] [blame] | 696 | * Adjust the size down since vmbus_channel_packet_page_buffer is the |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 697 | * largest size we support |
| 698 | */ |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 699 | descsize = sizeof(struct vmbus_channel_packet_page_buffer) - |
| 700 | ((MAX_PAGE_BUFFER_COUNT - pagecount) * |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 701 | sizeof(struct hv_page_buffer)); |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 702 | packetlen = descsize + bufferlen; |
Uwe Kleine-König | 7350968 | 2011-01-20 09:32:01 +0100 | [diff] [blame] | 703 | packetlen_aligned = ALIGN(packetlen, sizeof(u64)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 704 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 705 | /* Setup the descriptor */ |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 706 | desc.type = VM_PKT_DATA_USING_GPA_DIRECT; |
K. Y. Srinivasan | 87e93d6 | 2015-02-28 11:39:03 -0800 | [diff] [blame] | 707 | desc.flags = flags; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 708 | desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */ |
| 709 | desc.length8 = (u16)(packetlen_aligned >> 3); |
| 710 | desc.transactionid = requestid; |
| 711 | desc.rangecount = pagecount; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 712 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 713 | for (i = 0; i < pagecount; i++) { |
Haiyang Zhang | ca623ad | 2011-01-26 12:12:11 -0800 | [diff] [blame] | 714 | desc.range[i].len = pagebuffers[i].len; |
| 715 | desc.range[i].offset = pagebuffers[i].offset; |
| 716 | desc.range[i].pfn = pagebuffers[i].pfn; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 717 | } |
| 718 | |
K. Y. Srinivasan | 011a7c3 | 2014-02-01 19:02:20 -0800 | [diff] [blame] | 719 | bufferlist[0].iov_base = &desc; |
| 720 | bufferlist[0].iov_len = descsize; |
| 721 | bufferlist[1].iov_base = buffer; |
| 722 | bufferlist[1].iov_len = bufferlen; |
| 723 | bufferlist[2].iov_base = &aligned_data; |
| 724 | bufferlist[2].iov_len = (packetlen_aligned - packetlen); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 725 | |
K. Y. Srinivasan | 98fa8cf | 2012-12-01 06:46:36 -0800 | [diff] [blame] | 726 | ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 727 | |
K. Y. Srinivasan | 5f5cc81 | 2015-03-18 12:29:30 -0700 | [diff] [blame] | 728 | /* |
| 729 | * Signalling the host is conditional on many factors: |
| 730 | * 1. The ring state changed from being empty to non-empty. |
| 731 | * This is tracked by the variable "signal". |
| 732 | * 2. The variable kick_q tracks if more data will be placed |
| 733 | * on the ring. We will not signal if more data is |
| 734 | * to be placed. |
| 735 | * |
| 736 | * If we cannot write to the ring-buffer; signal the host |
| 737 | * even if we may not have written anything. This is a rare |
| 738 | * enough condition that it should not matter. |
| 739 | */ |
| 740 | if (((ret == 0) && kick_q && signal) || (ret)) |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 741 | vmbus_setevent(channel); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 742 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 743 | return ret; |
| 744 | } |
K. Y. Srinivasan | b3a19b3 | 2015-03-18 12:29:29 -0700 | [diff] [blame] | 745 | EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer_ctl); |
K. Y. Srinivasan | 87e93d6 | 2015-02-28 11:39:03 -0800 | [diff] [blame] | 746 | |
| 747 | /* |
| 748 | * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer |
| 749 | * packets using a GPADL Direct packet type. |
| 750 | */ |
| 751 | int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel, |
| 752 | struct hv_page_buffer pagebuffers[], |
| 753 | u32 pagecount, void *buffer, u32 bufferlen, |
| 754 | u64 requestid) |
| 755 | { |
| 756 | u32 flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; |
| 757 | return vmbus_sendpacket_pagebuffer_ctl(channel, pagebuffers, pagecount, |
| 758 | buffer, bufferlen, requestid, |
| 759 | flags, true); |
| 760 | |
| 761 | } |
Greg Kroah-Hartman | 713efeb | 2010-10-21 09:29:54 -0700 | [diff] [blame] | 762 | EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 763 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 764 | /* |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 765 | * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 766 | * using a GPADL Direct packet type. |
K. Y. Srinivasan | d61031e | 2015-01-09 23:54:34 -0800 | [diff] [blame] | 767 | * The buffer includes the vmbus descriptor. |
| 768 | */ |
| 769 | int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel, |
| 770 | struct vmbus_packet_mpb_array *desc, |
| 771 | u32 desc_size, |
| 772 | void *buffer, u32 bufferlen, u64 requestid) |
| 773 | { |
| 774 | int ret; |
| 775 | u32 packetlen; |
| 776 | u32 packetlen_aligned; |
| 777 | struct kvec bufferlist[3]; |
| 778 | u64 aligned_data = 0; |
| 779 | bool signal = false; |
| 780 | |
| 781 | packetlen = desc_size + bufferlen; |
| 782 | packetlen_aligned = ALIGN(packetlen, sizeof(u64)); |
| 783 | |
| 784 | /* Setup the descriptor */ |
| 785 | desc->type = VM_PKT_DATA_USING_GPA_DIRECT; |
| 786 | desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; |
| 787 | desc->dataoffset8 = desc_size >> 3; /* in 8-bytes grandularity */ |
| 788 | desc->length8 = (u16)(packetlen_aligned >> 3); |
| 789 | desc->transactionid = requestid; |
| 790 | desc->rangecount = 1; |
| 791 | |
| 792 | bufferlist[0].iov_base = desc; |
| 793 | bufferlist[0].iov_len = desc_size; |
| 794 | bufferlist[1].iov_base = buffer; |
| 795 | bufferlist[1].iov_len = bufferlen; |
| 796 | bufferlist[2].iov_base = &aligned_data; |
| 797 | bufferlist[2].iov_len = (packetlen_aligned - packetlen); |
| 798 | |
| 799 | ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal); |
| 800 | |
| 801 | if (ret == 0 && signal) |
| 802 | vmbus_setevent(channel); |
| 803 | |
| 804 | return ret; |
| 805 | } |
| 806 | EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc); |
| 807 | |
| 808 | /* |
| 809 | * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet |
| 810 | * using a GPADL Direct packet type. |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 811 | */ |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 812 | int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel, |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 813 | struct hv_multipage_buffer *multi_pagebuffer, |
| 814 | void *buffer, u32 bufferlen, u64 requestid) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 815 | { |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 816 | int ret; |
Haiyang Zhang | 430a8e9 | 2010-09-20 21:07:51 +0000 | [diff] [blame] | 817 | struct vmbus_channel_packet_multipage_buffer desc; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 818 | u32 descsize; |
| 819 | u32 packetlen; |
| 820 | u32 packetlen_aligned; |
K. Y. Srinivasan | 011a7c3 | 2014-02-01 19:02:20 -0800 | [diff] [blame] | 821 | struct kvec bufferlist[3]; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 822 | u64 aligned_data = 0; |
K. Y. Srinivasan | 98fa8cf | 2012-12-01 06:46:36 -0800 | [diff] [blame] | 823 | bool signal = false; |
Haiyang Zhang | ca623ad | 2011-01-26 12:12:11 -0800 | [diff] [blame] | 824 | u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset, |
| 825 | multi_pagebuffer->len); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 826 | |
Tobias Klauser | 24b8a40 | 2014-04-25 17:03:50 +0200 | [diff] [blame] | 827 | if (pfncount > MAX_MULTIPAGE_BUFFER_COUNT) |
Bill Pemberton | 002b53e | 2010-05-05 15:27:39 -0400 | [diff] [blame] | 828 | return -EINVAL; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 829 | |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 830 | /* |
Haiyang Zhang | 430a8e9 | 2010-09-20 21:07:51 +0000 | [diff] [blame] | 831 | * Adjust the size down since vmbus_channel_packet_multipage_buffer is |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 832 | * the largest size we support |
| 833 | */ |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 834 | descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) - |
| 835 | ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) * |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 836 | sizeof(u64)); |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 837 | packetlen = descsize + bufferlen; |
Uwe Kleine-König | 7350968 | 2011-01-20 09:32:01 +0100 | [diff] [blame] | 838 | packetlen_aligned = ALIGN(packetlen, sizeof(u64)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 839 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 840 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 841 | /* Setup the descriptor */ |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 842 | desc.type = VM_PKT_DATA_USING_GPA_DIRECT; |
Haiyang Zhang | 430a8e9 | 2010-09-20 21:07:51 +0000 | [diff] [blame] | 843 | desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 844 | desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */ |
| 845 | desc.length8 = (u16)(packetlen_aligned >> 3); |
| 846 | desc.transactionid = requestid; |
Haiyang Zhang | 430a8e9 | 2010-09-20 21:07:51 +0000 | [diff] [blame] | 847 | desc.rangecount = 1; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 848 | |
Haiyang Zhang | ca623ad | 2011-01-26 12:12:11 -0800 | [diff] [blame] | 849 | desc.range.len = multi_pagebuffer->len; |
| 850 | desc.range.offset = multi_pagebuffer->offset; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 851 | |
Haiyang Zhang | ca623ad | 2011-01-26 12:12:11 -0800 | [diff] [blame] | 852 | memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array, |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 853 | pfncount * sizeof(u64)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 854 | |
K. Y. Srinivasan | 011a7c3 | 2014-02-01 19:02:20 -0800 | [diff] [blame] | 855 | bufferlist[0].iov_base = &desc; |
| 856 | bufferlist[0].iov_len = descsize; |
| 857 | bufferlist[1].iov_base = buffer; |
| 858 | bufferlist[1].iov_len = bufferlen; |
| 859 | bufferlist[2].iov_base = &aligned_data; |
| 860 | bufferlist[2].iov_len = (packetlen_aligned - packetlen); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 861 | |
K. Y. Srinivasan | 98fa8cf | 2012-12-01 06:46:36 -0800 | [diff] [blame] | 862 | ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 863 | |
K. Y. Srinivasan | 98fa8cf | 2012-12-01 06:46:36 -0800 | [diff] [blame] | 864 | if (ret == 0 && signal) |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 865 | vmbus_setevent(channel); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 866 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 867 | return ret; |
| 868 | } |
Greg Kroah-Hartman | 4cb106f | 2010-10-21 09:23:59 -0700 | [diff] [blame] | 869 | EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer); |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 870 | |
| 871 | /** |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 872 | * vmbus_recvpacket() - Retrieve the user packet on the specified channel |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 873 | * @channel: Pointer to vmbus_channel structure. |
| 874 | * @buffer: Pointer to the buffer you want to receive the data into. |
| 875 | * @bufferlen: Maximum size of what the the buffer will hold |
| 876 | * @buffer_actual_len: The actual size of the data after it was received |
| 877 | * @requestid: Identifier of the request |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 878 | * |
| 879 | * Receives directly from the hyper-v vmbus and puts the data it received |
| 880 | * into Buffer. This will receive the data unparsed from hyper-v. |
| 881 | * |
| 882 | * Mainly used by Hyper-V drivers. |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 883 | */ |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 884 | int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer, |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 885 | u32 bufferlen, u32 *buffer_actual_len, u64 *requestid) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 886 | { |
Greg Kroah-Hartman | 8dc0a06 | 2009-08-27 16:02:36 -0700 | [diff] [blame] | 887 | struct vmpacket_descriptor desc; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 888 | u32 packetlen; |
| 889 | u32 userlen; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 890 | int ret; |
K. Y. Srinivasan | c2b8e52 | 2012-12-01 06:46:57 -0800 | [diff] [blame] | 891 | bool signal = false; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 892 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 893 | *buffer_actual_len = 0; |
| 894 | *requestid = 0; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 895 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 896 | |
K. Y. Srinivasan | a89186c | 2011-05-10 07:55:24 -0700 | [diff] [blame] | 897 | ret = hv_ringbuffer_peek(&channel->inbound, &desc, |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 898 | sizeof(struct vmpacket_descriptor)); |
K. Y. Srinivasan | dad76bf | 2011-08-27 11:31:33 -0700 | [diff] [blame] | 899 | if (ret != 0) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 900 | return 0; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 901 | |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 902 | packetlen = desc.len8 << 3; |
| 903 | userlen = packetlen - (desc.offset8 << 3); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 904 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 905 | *buffer_actual_len = userlen; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 906 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 907 | if (userlen > bufferlen) { |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 908 | |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 909 | pr_err("Buffer too small - got %d needs %d\n", |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 910 | bufferlen, userlen); |
K. Y. Srinivasan | 926ae526 | 2011-06-06 15:50:11 -0700 | [diff] [blame] | 911 | return -ETOOSMALL; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 912 | } |
| 913 | |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 914 | *requestid = desc.trans_id; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 915 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 916 | /* Copy over the packet to the user buffer */ |
K. Y. Srinivasan | 38397c8 | 2011-05-10 07:55:25 -0700 | [diff] [blame] | 917 | ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen, |
K. Y. Srinivasan | c2b8e52 | 2012-12-01 06:46:57 -0800 | [diff] [blame] | 918 | (desc.offset8 << 3), &signal); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 919 | |
K. Y. Srinivasan | c2b8e52 | 2012-12-01 06:46:57 -0800 | [diff] [blame] | 920 | if (signal) |
| 921 | vmbus_setevent(channel); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 922 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 923 | return 0; |
| 924 | } |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 925 | EXPORT_SYMBOL(vmbus_recvpacket); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 926 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 927 | /* |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 928 | * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 929 | */ |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 930 | int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer, |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 931 | u32 bufferlen, u32 *buffer_actual_len, |
| 932 | u64 *requestid) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 933 | { |
Greg Kroah-Hartman | 8dc0a06 | 2009-08-27 16:02:36 -0700 | [diff] [blame] | 934 | struct vmpacket_descriptor desc; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 935 | u32 packetlen; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 936 | int ret; |
K. Y. Srinivasan | c2b8e52 | 2012-12-01 06:46:57 -0800 | [diff] [blame] | 937 | bool signal = false; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 938 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 939 | *buffer_actual_len = 0; |
| 940 | *requestid = 0; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 941 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 942 | |
K. Y. Srinivasan | a89186c | 2011-05-10 07:55:24 -0700 | [diff] [blame] | 943 | ret = hv_ringbuffer_peek(&channel->inbound, &desc, |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 944 | sizeof(struct vmpacket_descriptor)); |
K. Y. Srinivasan | dad76bf | 2011-08-27 11:31:33 -0700 | [diff] [blame] | 945 | if (ret != 0) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 946 | return 0; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 947 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 948 | |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 949 | packetlen = desc.len8 << 3; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 950 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 951 | *buffer_actual_len = packetlen; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 952 | |
Jason Wang | 7a446d6 | 2014-06-30 14:14:48 +0800 | [diff] [blame] | 953 | if (packetlen > bufferlen) |
K. Y. Srinivasan | 3d5cad9 | 2011-08-25 09:48:59 -0700 | [diff] [blame] | 954 | return -ENOBUFS; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 955 | |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 956 | *requestid = desc.trans_id; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 957 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 958 | /* Copy over the entire packet to the user buffer */ |
K. Y. Srinivasan | c2b8e52 | 2012-12-01 06:46:57 -0800 | [diff] [blame] | 959 | ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0, |
| 960 | &signal); |
| 961 | |
| 962 | if (signal) |
| 963 | vmbus_setevent(channel); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 964 | |
Dan Carpenter | 33b0693 | 2013-09-17 00:58:36 +0300 | [diff] [blame] | 965 | return ret; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 966 | } |
Greg Kroah-Hartman | adaee6b | 2010-10-21 09:09:23 -0700 | [diff] [blame] | 967 | EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw); |