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 | 3f335ea | 2011-05-12 19:34:15 -0700 | [diff] [blame] | 30 | |
K. Y. Srinivasan | 0f2a661 | 2011-05-12 19:34:28 -0700 | [diff] [blame] | 31 | #include "hyperv_vmbus.h" |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 32 | |
K. Y. Srinivasan | e3fe0bb | 2011-02-11 10:00:12 -0800 | [diff] [blame] | 33 | #define NUM_PAGES_SPANNED(addr, len) \ |
| 34 | ((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT)) |
| 35 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 36 | /* |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 37 | * vmbus_setevent- Trigger an event notification on the specified |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 38 | * channel. |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 39 | */ |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 40 | static void vmbus_setevent(struct vmbus_channel *channel) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 41 | { |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 42 | struct hv_monitor_page *monitorpage; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 43 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 44 | if (channel->offermsg.monitor_allocated) { |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 45 | /* Each u32 represents 32 channels */ |
Olaf Hering | 2235658 | 2011-03-21 14:41:37 +0100 | [diff] [blame] | 46 | sync_set_bit(channel->offermsg.child_relid & 31, |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 47 | (unsigned long *) vmbus_connection.send_int_page + |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 48 | (channel->offermsg.child_relid >> 5)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 49 | |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 50 | monitorpage = vmbus_connection.monitor_pages; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 51 | monitorpage++; /* Get the child to parent monitor page */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 52 | |
Olaf Hering | 2235658 | 2011-03-21 14:41:37 +0100 | [diff] [blame] | 53 | sync_set_bit(channel->monitor_bit, |
Haiyang Zhang | f6feebe | 2010-11-08 14:04:39 -0800 | [diff] [blame] | 54 | (unsigned long *)&monitorpage->trigger_group |
| 55 | [channel->monitor_grp].pending); |
Bill Pemberton | 7c369f4 | 2009-07-29 17:00:11 -0400 | [diff] [blame] | 56 | |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 57 | } else { |
K. Y. Srinivasan | 21c3bef | 2012-12-01 06:46:43 -0800 | [diff] [blame] | 58 | vmbus_set_event(channel); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 59 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 62 | /* |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 63 | * vmbus_get_debug_info -Retrieve various channel debug info |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 64 | */ |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 65 | void vmbus_get_debug_info(struct vmbus_channel *channel, |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 66 | struct vmbus_channel_debug_info *debuginfo) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 67 | { |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 68 | struct hv_monitor_page *monitorpage; |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 69 | u8 monitor_group = (u8)channel->offermsg.monitorid / 32; |
| 70 | u8 monitor_offset = (u8)channel->offermsg.monitorid % 32; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 71 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 72 | debuginfo->relid = channel->offermsg.child_relid; |
| 73 | debuginfo->state = channel->state; |
| 74 | memcpy(&debuginfo->interfacetype, |
K. Y. Srinivasan | 358d2ee | 2011-08-25 09:48:28 -0700 | [diff] [blame] | 75 | &channel->offermsg.offer.if_type, sizeof(uuid_le)); |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 76 | memcpy(&debuginfo->interface_instance, |
Haiyang Zhang | 767dff6 | 2011-01-26 12:12:12 -0800 | [diff] [blame] | 77 | &channel->offermsg.offer.if_instance, |
K. Y. Srinivasan | 358d2ee | 2011-08-25 09:48:28 -0700 | [diff] [blame] | 78 | sizeof(uuid_le)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 79 | |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 80 | monitorpage = (struct hv_monitor_page *)vmbus_connection.monitor_pages; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 81 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 82 | debuginfo->monitorid = channel->offermsg.monitorid; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 83 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 84 | debuginfo->servermonitor_pending = |
Haiyang Zhang | f6feebe | 2010-11-08 14:04:39 -0800 | [diff] [blame] | 85 | monitorpage->trigger_group[monitor_group].pending; |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 86 | debuginfo->servermonitor_latency = |
Haiyang Zhang | f6feebe | 2010-11-08 14:04:39 -0800 | [diff] [blame] | 87 | monitorpage->latency[monitor_group][monitor_offset]; |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 88 | debuginfo->servermonitor_connectionid = |
Haiyang Zhang | f6feebe | 2010-11-08 14:04:39 -0800 | [diff] [blame] | 89 | monitorpage->parameter[monitor_group] |
| 90 | [monitor_offset].connectionid.u.id; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 91 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 92 | monitorpage++; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 93 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 94 | debuginfo->clientmonitor_pending = |
Haiyang Zhang | f6feebe | 2010-11-08 14:04:39 -0800 | [diff] [blame] | 95 | monitorpage->trigger_group[monitor_group].pending; |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 96 | debuginfo->clientmonitor_latency = |
Haiyang Zhang | f6feebe | 2010-11-08 14:04:39 -0800 | [diff] [blame] | 97 | monitorpage->latency[monitor_group][monitor_offset]; |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 98 | debuginfo->clientmonitor_connectionid = |
Haiyang Zhang | f6feebe | 2010-11-08 14:04:39 -0800 | [diff] [blame] | 99 | monitorpage->parameter[monitor_group] |
| 100 | [monitor_offset].connectionid.u.id; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 101 | |
K. Y. Srinivasan | a75b61d | 2011-05-10 07:55:28 -0700 | [diff] [blame] | 102 | hv_ringbuffer_get_debuginfo(&channel->inbound, &debuginfo->inbound); |
| 103 | hv_ringbuffer_get_debuginfo(&channel->outbound, &debuginfo->outbound); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 106 | /* |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 107 | * vmbus_open - Open the specified channel. |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 108 | */ |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 109 | int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size, |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 110 | u32 recv_ringbuffer_size, void *userdata, u32 userdatalen, |
| 111 | void (*onchannelcallback)(void *context), void *context) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 112 | { |
K. Y. Srinivasan | 0987ff6 | 2011-08-25 09:48:55 -0700 | [diff] [blame] | 113 | struct vmbus_channel_open_channel *open_msg; |
K. Y. Srinivasan | 176fb9e | 2011-08-25 09:48:57 -0700 | [diff] [blame] | 114 | struct vmbus_channel_msginfo *open_info = NULL; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 115 | void *in, *out; |
Greg Kroah-Hartman | dd0813b | 2009-07-15 14:56:45 -0700 | [diff] [blame] | 116 | unsigned long flags; |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 117 | int ret, t, err = 0; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 118 | |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 119 | spin_lock_irqsave(&newchannel->sc_lock, flags); |
| 120 | if (newchannel->state == CHANNEL_OPEN_STATE) { |
| 121 | newchannel->state = CHANNEL_OPENING_STATE; |
| 122 | } else { |
| 123 | spin_unlock_irqrestore(&newchannel->sc_lock, flags); |
| 124 | return -EINVAL; |
| 125 | } |
| 126 | spin_unlock_irqrestore(&newchannel->sc_lock, flags); |
| 127 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 128 | newchannel->onchannel_callback = onchannelcallback; |
| 129 | newchannel->channel_callback_context = context; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 130 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 131 | /* Allocate the ring buffer */ |
K. Y. Srinivasan | df3493e | 2011-02-11 09:59:00 -0800 | [diff] [blame] | 132 | out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, |
| 133 | get_order(send_ringbuffer_size + recv_ringbuffer_size)); |
| 134 | |
Bill Pemberton | 7e052d9 | 2010-05-05 15:27:30 -0400 | [diff] [blame] | 135 | if (!out) |
| 136 | return -ENOMEM; |
| 137 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 138 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 139 | in = (void *)((unsigned long)out + send_ringbuffer_size); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 140 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 141 | newchannel->ringbuffer_pages = out; |
| 142 | newchannel->ringbuffer_pagecount = (send_ringbuffer_size + |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 143 | recv_ringbuffer_size) >> PAGE_SHIFT; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 144 | |
K. Y. Srinivasan | 72a95cb | 2011-05-10 07:55:21 -0700 | [diff] [blame] | 145 | ret = hv_ringbuffer_init( |
| 146 | &newchannel->outbound, out, send_ringbuffer_size); |
| 147 | |
Haiyang Zhang | fd4dc88 | 2010-05-13 15:56:30 +0000 | [diff] [blame] | 148 | if (ret != 0) { |
Bill Pemberton | 3324fb4 | 2010-05-05 15:27:49 -0400 | [diff] [blame] | 149 | err = ret; |
K. Y. Srinivasan | 1392550 | 2012-10-12 13:22:42 -0700 | [diff] [blame] | 150 | goto error0; |
Bill Pemberton | 3324fb4 | 2010-05-05 15:27:49 -0400 | [diff] [blame] | 151 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 152 | |
K. Y. Srinivasan | 72a95cb | 2011-05-10 07:55:21 -0700 | [diff] [blame] | 153 | ret = hv_ringbuffer_init( |
| 154 | &newchannel->inbound, in, recv_ringbuffer_size); |
Haiyang Zhang | fd4dc88 | 2010-05-13 15:56:30 +0000 | [diff] [blame] | 155 | if (ret != 0) { |
Bill Pemberton | 3324fb4 | 2010-05-05 15:27:49 -0400 | [diff] [blame] | 156 | err = ret; |
K. Y. Srinivasan | 1392550 | 2012-10-12 13:22:42 -0700 | [diff] [blame] | 157 | goto error0; |
Bill Pemberton | 3324fb4 | 2010-05-05 15:27:49 -0400 | [diff] [blame] | 158 | } |
| 159 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 160 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 161 | /* Establish the gpadl for the ring buffer */ |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 162 | newchannel->ringbuffer_gpadlhandle = 0; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 163 | |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 164 | ret = vmbus_establish_gpadl(newchannel, |
Haiyang Zhang | 82f8bd4 | 2010-11-08 14:04:45 -0800 | [diff] [blame] | 165 | newchannel->outbound.ring_buffer, |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 166 | send_ringbuffer_size + |
| 167 | recv_ringbuffer_size, |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 168 | &newchannel->ringbuffer_gpadlhandle); |
Bill Pemberton | b94ef34 | 2010-05-05 15:27:37 -0400 | [diff] [blame] | 169 | |
Haiyang Zhang | fd4dc88 | 2010-05-13 15:56:30 +0000 | [diff] [blame] | 170 | if (ret != 0) { |
Bill Pemberton | b94ef34 | 2010-05-05 15:27:37 -0400 | [diff] [blame] | 171 | err = ret; |
K. Y. Srinivasan | 1392550 | 2012-10-12 13:22:42 -0700 | [diff] [blame] | 172 | goto error0; |
Bill Pemberton | b94ef34 | 2010-05-05 15:27:37 -0400 | [diff] [blame] | 173 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 174 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 175 | /* Create and init the channel open message */ |
K. Y. Srinivasan | 176fb9e | 2011-08-25 09:48:57 -0700 | [diff] [blame] | 176 | open_info = kmalloc(sizeof(*open_info) + |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 177 | sizeof(struct vmbus_channel_open_channel), |
| 178 | GFP_KERNEL); |
K. Y. Srinivasan | 176fb9e | 2011-08-25 09:48:57 -0700 | [diff] [blame] | 179 | if (!open_info) { |
Bill Pemberton | c3bf2e2 | 2010-05-05 15:27:34 -0400 | [diff] [blame] | 180 | err = -ENOMEM; |
K. Y. Srinivasan | 1392550 | 2012-10-12 13:22:42 -0700 | [diff] [blame] | 181 | goto error0; |
Bill Pemberton | c3bf2e2 | 2010-05-05 15:27:34 -0400 | [diff] [blame] | 182 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 183 | |
K. Y. Srinivasan | 176fb9e | 2011-08-25 09:48:57 -0700 | [diff] [blame] | 184 | init_completion(&open_info->waitevent); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 185 | |
K. Y. Srinivasan | 176fb9e | 2011-08-25 09:48:57 -0700 | [diff] [blame] | 186 | open_msg = (struct vmbus_channel_open_channel *)open_info->msg; |
K. Y. Srinivasan | 0987ff6 | 2011-08-25 09:48:55 -0700 | [diff] [blame] | 187 | open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL; |
| 188 | open_msg->openid = newchannel->offermsg.child_relid; |
| 189 | open_msg->child_relid = newchannel->offermsg.child_relid; |
| 190 | open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle; |
| 191 | open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >> |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 192 | PAGE_SHIFT; |
K. Y. Srinivasan | abbf3b2 | 2012-12-01 06:46:48 -0800 | [diff] [blame] | 193 | open_msg->target_vp = newchannel->target_vp; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 194 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 195 | if (userdatalen > MAX_USER_DEFINED_BYTES) { |
Bill Pemberton | c827f94 | 2010-05-05 15:27:38 -0400 | [diff] [blame] | 196 | err = -EINVAL; |
K. Y. Srinivasan | 1392550 | 2012-10-12 13:22:42 -0700 | [diff] [blame] | 197 | goto error0; |
Bill Pemberton | c827f94 | 2010-05-05 15:27:38 -0400 | [diff] [blame] | 198 | } |
| 199 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 200 | if (userdatalen) |
K. Y. Srinivasan | 0987ff6 | 2011-08-25 09:48:55 -0700 | [diff] [blame] | 201 | memcpy(open_msg->userdata, userdata, userdatalen); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 202 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 203 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
K. Y. Srinivasan | 176fb9e | 2011-08-25 09:48:57 -0700 | [diff] [blame] | 204 | list_add_tail(&open_info->msglistentry, |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 205 | &vmbus_connection.chn_msg_list); |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 206 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 207 | |
K. Y. Srinivasan | 0987ff6 | 2011-08-25 09:48:55 -0700 | [diff] [blame] | 208 | ret = vmbus_post_msg(open_msg, |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 209 | sizeof(struct vmbus_channel_open_channel)); |
Hank Janssen | 98e0870 | 2011-03-29 13:58:44 -0700 | [diff] [blame] | 210 | |
| 211 | if (ret != 0) |
K. Y. Srinivasan | 1392550 | 2012-10-12 13:22:42 -0700 | [diff] [blame] | 212 | goto error1; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 213 | |
K. Y. Srinivasan | 176fb9e | 2011-08-25 09:48:57 -0700 | [diff] [blame] | 214 | t = wait_for_completion_timeout(&open_info->waitevent, 5*HZ); |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 215 | if (t == 0) { |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 216 | err = -ETIMEDOUT; |
K. Y. Srinivasan | 1392550 | 2012-10-12 13:22:42 -0700 | [diff] [blame] | 217 | goto error1; |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 220 | |
K. Y. Srinivasan | 176fb9e | 2011-08-25 09:48:57 -0700 | [diff] [blame] | 221 | if (open_info->response.open_result.status) |
| 222 | err = open_info->response.open_result.status; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 223 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 224 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
K. Y. Srinivasan | 176fb9e | 2011-08-25 09:48:57 -0700 | [diff] [blame] | 225 | list_del(&open_info->msglistentry); |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 226 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 227 | |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 228 | if (err == 0) |
| 229 | newchannel->state = CHANNEL_OPENED_STATE; |
| 230 | |
K. Y. Srinivasan | 176fb9e | 2011-08-25 09:48:57 -0700 | [diff] [blame] | 231 | kfree(open_info); |
Hank Janssen | 98e0870 | 2011-03-29 13:58:44 -0700 | [diff] [blame] | 232 | return err; |
Bill Pemberton | c3bf2e2 | 2010-05-05 15:27:34 -0400 | [diff] [blame] | 233 | |
K. Y. Srinivasan | 1392550 | 2012-10-12 13:22:42 -0700 | [diff] [blame] | 234 | error1: |
| 235 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
| 236 | list_del(&open_info->msglistentry); |
| 237 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
| 238 | |
| 239 | error0: |
K. Y. Srinivasan | df3493e | 2011-02-11 09:59:00 -0800 | [diff] [blame] | 240 | free_pages((unsigned long)out, |
| 241 | get_order(send_ringbuffer_size + recv_ringbuffer_size)); |
K. Y. Srinivasan | 176fb9e | 2011-08-25 09:48:57 -0700 | [diff] [blame] | 242 | kfree(open_info); |
Bill Pemberton | c3bf2e2 | 2010-05-05 15:27:34 -0400 | [diff] [blame] | 243 | return err; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 244 | } |
Greg Kroah-Hartman | 36ceadf | 2010-10-21 09:58:21 -0700 | [diff] [blame] | 245 | EXPORT_SYMBOL_GPL(vmbus_open); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 246 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 247 | /* |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 248 | * create_gpadl_header - Creates a gpadl for the specified buffer |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 249 | */ |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 250 | static int create_gpadl_header(void *kbuffer, u32 size, |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 251 | struct vmbus_channel_msginfo **msginfo, |
| 252 | u32 *messagecount) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 253 | { |
| 254 | int i; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 255 | int pagecount; |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 256 | unsigned long long pfn; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 257 | struct vmbus_channel_gpadl_header *gpadl_header; |
| 258 | struct vmbus_channel_gpadl_body *gpadl_body; |
| 259 | struct vmbus_channel_msginfo *msgheader; |
| 260 | struct vmbus_channel_msginfo *msgbody = NULL; |
| 261 | u32 msgsize; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 262 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 263 | int pfnsum, pfncount, pfnleft, pfncurr, pfnsize; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 264 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 265 | pagecount = size >> PAGE_SHIFT; |
| 266 | pfn = virt_to_phys(kbuffer) >> PAGE_SHIFT; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 267 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 268 | /* do we need a gpadl body msg */ |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 269 | pfnsize = MAX_SIZE_CHANNEL_MESSAGE - |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 270 | sizeof(struct vmbus_channel_gpadl_header) - |
| 271 | sizeof(struct gpa_range); |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 272 | pfncount = pfnsize / sizeof(u64); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 273 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 274 | if (pagecount > pfncount) { |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 275 | /* we need a gpadl body */ |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 276 | /* fill in the header */ |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 277 | msgsize = sizeof(struct vmbus_channel_msginfo) + |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 278 | sizeof(struct vmbus_channel_gpadl_header) + |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 279 | sizeof(struct gpa_range) + pfncount * sizeof(u64); |
| 280 | msgheader = kzalloc(msgsize, GFP_KERNEL); |
| 281 | if (!msgheader) |
Bill Pemberton | d1c250b | 2010-05-05 15:27:35 -0400 | [diff] [blame] | 282 | goto nomem; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 283 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 284 | INIT_LIST_HEAD(&msgheader->submsglist); |
| 285 | msgheader->msgsize = msgsize; |
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 | gpadl_header = (struct vmbus_channel_gpadl_header *) |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 288 | msgheader->msg; |
| 289 | gpadl_header->rangecount = 1; |
| 290 | gpadl_header->range_buflen = sizeof(struct gpa_range) + |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 291 | pagecount * sizeof(u64); |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 292 | gpadl_header->range[0].byte_offset = 0; |
| 293 | gpadl_header->range[0].byte_count = size; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 294 | for (i = 0; i < pfncount; i++) |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 295 | gpadl_header->range[0].pfn_array[i] = pfn+i; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 296 | *msginfo = msgheader; |
| 297 | *messagecount = 1; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 298 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 299 | pfnsum = pfncount; |
| 300 | pfnleft = pagecount - pfncount; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 301 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 302 | /* how many pfns can we fit */ |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 303 | pfnsize = MAX_SIZE_CHANNEL_MESSAGE - |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 304 | sizeof(struct vmbus_channel_gpadl_body); |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 305 | pfncount = pfnsize / sizeof(u64); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 306 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 307 | /* fill in the body */ |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 308 | while (pfnleft) { |
| 309 | if (pfnleft > pfncount) |
| 310 | pfncurr = pfncount; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 311 | else |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 312 | pfncurr = pfnleft; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 313 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 314 | msgsize = sizeof(struct vmbus_channel_msginfo) + |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 315 | sizeof(struct vmbus_channel_gpadl_body) + |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 316 | pfncurr * sizeof(u64); |
| 317 | msgbody = kzalloc(msgsize, GFP_KERNEL); |
K. Y. Srinivasan | f38cf9c | 2011-06-06 15:50:06 -0700 | [diff] [blame] | 318 | |
| 319 | if (!msgbody) { |
| 320 | struct vmbus_channel_msginfo *pos = NULL; |
| 321 | struct vmbus_channel_msginfo *tmp = NULL; |
| 322 | /* |
| 323 | * Free up all the allocated messages. |
| 324 | */ |
| 325 | list_for_each_entry_safe(pos, tmp, |
| 326 | &msgheader->submsglist, |
| 327 | msglistentry) { |
| 328 | |
| 329 | list_del(&pos->msglistentry); |
| 330 | kfree(pos); |
| 331 | } |
| 332 | |
Bill Pemberton | d1c250b | 2010-05-05 15:27:35 -0400 | [diff] [blame] | 333 | goto nomem; |
K. Y. Srinivasan | f38cf9c | 2011-06-06 15:50:06 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 336 | msgbody->msgsize = msgsize; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 337 | (*messagecount)++; |
| 338 | gpadl_body = |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 339 | (struct vmbus_channel_gpadl_body *)msgbody->msg; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 340 | |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 341 | /* |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 342 | * Gpadl is u32 and we are using a pointer which could |
| 343 | * be 64-bit |
K. Y. Srinivasan | f27df64 | 2011-06-06 15:49:56 -0700 | [diff] [blame] | 344 | * This is governed by the guest/host protocol and |
| 345 | * so the hypervisor gurantees that this is ok. |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 346 | */ |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 347 | for (i = 0; i < pfncurr; i++) |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 348 | gpadl_body->pfn[i] = pfn + pfnsum + i; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 349 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 350 | /* add to msg header */ |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 351 | list_add_tail(&msgbody->msglistentry, |
| 352 | &msgheader->submsglist); |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 353 | pfnsum += pfncurr; |
| 354 | pfnleft -= pfncurr; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 355 | } |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 356 | } else { |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 357 | /* everything fits in a header */ |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 358 | msgsize = sizeof(struct vmbus_channel_msginfo) + |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 359 | sizeof(struct vmbus_channel_gpadl_header) + |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 360 | sizeof(struct gpa_range) + pagecount * sizeof(u64); |
| 361 | msgheader = kzalloc(msgsize, GFP_KERNEL); |
| 362 | if (msgheader == NULL) |
Kulikov Vasiliy | e3eb7cd | 2010-07-16 20:13:51 +0400 | [diff] [blame] | 363 | goto nomem; |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 364 | msgheader->msgsize = msgsize; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 365 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 366 | gpadl_header = (struct vmbus_channel_gpadl_header *) |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 367 | msgheader->msg; |
| 368 | gpadl_header->rangecount = 1; |
| 369 | gpadl_header->range_buflen = sizeof(struct gpa_range) + |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 370 | pagecount * sizeof(u64); |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 371 | gpadl_header->range[0].byte_offset = 0; |
| 372 | gpadl_header->range[0].byte_count = size; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 373 | for (i = 0; i < pagecount; i++) |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 374 | gpadl_header->range[0].pfn_array[i] = pfn+i; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 375 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 376 | *msginfo = msgheader; |
| 377 | *messagecount = 1; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | return 0; |
Bill Pemberton | d1c250b | 2010-05-05 15:27:35 -0400 | [diff] [blame] | 381 | nomem: |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 382 | kfree(msgheader); |
| 383 | kfree(msgbody); |
Bill Pemberton | d1c250b | 2010-05-05 15:27:35 -0400 | [diff] [blame] | 384 | return -ENOMEM; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 387 | /* |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 388 | * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 389 | * |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 390 | * @channel: a channel |
| 391 | * @kbuffer: from kmalloc |
| 392 | * @size: page-size multiple |
| 393 | * @gpadl_handle: some funky thing |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 394 | */ |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 395 | int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer, |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 396 | u32 size, u32 *gpadl_handle) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 397 | { |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 398 | struct vmbus_channel_gpadl_header *gpadlmsg; |
| 399 | struct vmbus_channel_gpadl_body *gpadl_body; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 400 | struct vmbus_channel_msginfo *msginfo = NULL; |
| 401 | struct vmbus_channel_msginfo *submsginfo; |
| 402 | u32 msgcount; |
Bill Pemberton | 53af545 | 2009-09-11 21:46:44 -0400 | [diff] [blame] | 403 | struct list_head *curr; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 404 | u32 next_gpadl_handle; |
Greg Kroah-Hartman | dd0813b | 2009-07-15 14:56:45 -0700 | [diff] [blame] | 405 | unsigned long flags; |
Bill Pemberton | c3bf2e2 | 2010-05-05 15:27:34 -0400 | [diff] [blame] | 406 | int ret = 0; |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 407 | int t; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 408 | |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 409 | next_gpadl_handle = atomic_read(&vmbus_connection.next_gpadl_handle); |
| 410 | atomic_inc(&vmbus_connection.next_gpadl_handle); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 411 | |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 412 | ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount); |
Bill Pemberton | c3bf2e2 | 2010-05-05 15:27:34 -0400 | [diff] [blame] | 413 | if (ret) |
| 414 | return ret; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 415 | |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 416 | init_completion(&msginfo->waitevent); |
Bill Pemberton | c3bf2e2 | 2010-05-05 15:27:34 -0400 | [diff] [blame] | 417 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 418 | gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg; |
| 419 | gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER; |
| 420 | gpadlmsg->child_relid = channel->offermsg.child_relid; |
| 421 | gpadlmsg->gpadl = next_gpadl_handle; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 422 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 423 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 424 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 425 | list_add_tail(&msginfo->msglistentry, |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 426 | &vmbus_connection.chn_msg_list); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 427 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 428 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 429 | |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 430 | ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize - |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 431 | sizeof(*msginfo)); |
Hank Janssen | 98e0870 | 2011-03-29 13:58:44 -0700 | [diff] [blame] | 432 | if (ret != 0) |
K. Y. Srinivasan | 00d760b | 2011-06-06 15:50:12 -0700 | [diff] [blame] | 433 | goto cleanup; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 434 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 435 | if (msgcount > 1) { |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 436 | list_for_each(curr, &msginfo->submsglist) { |
Bill Pemberton | 53af545 | 2009-09-11 21:46:44 -0400 | [diff] [blame] | 437 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 438 | submsginfo = (struct vmbus_channel_msginfo *)curr; |
| 439 | gpadl_body = |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 440 | (struct vmbus_channel_gpadl_body *)submsginfo->msg; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 441 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 442 | gpadl_body->header.msgtype = |
| 443 | CHANNELMSG_GPADL_BODY; |
| 444 | gpadl_body->gpadl = next_gpadl_handle; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 445 | |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 446 | ret = vmbus_post_msg(gpadl_body, |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 447 | submsginfo->msgsize - |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 448 | sizeof(*submsginfo)); |
Haiyang Zhang | fd4dc88 | 2010-05-13 15:56:30 +0000 | [diff] [blame] | 449 | if (ret != 0) |
K. Y. Srinivasan | 00d760b | 2011-06-06 15:50:12 -0700 | [diff] [blame] | 450 | goto cleanup; |
Bill Pemberton | 9925915 | 2010-05-05 15:27:36 -0400 | [diff] [blame] | 451 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 452 | } |
| 453 | } |
K. Y. Srinivasan | 40961de | 2011-06-06 15:50:05 -0700 | [diff] [blame] | 454 | t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ); |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 455 | BUG_ON(t == 0); |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 456 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 457 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 458 | /* At this point, we received the gpadl created msg */ |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 459 | *gpadl_handle = gpadlmsg->gpadl; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 460 | |
K. Y. Srinivasan | 00d760b | 2011-06-06 15:50:12 -0700 | [diff] [blame] | 461 | cleanup: |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 462 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 463 | list_del(&msginfo->msglistentry); |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 464 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 465 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 466 | kfree(msginfo); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 467 | return ret; |
| 468 | } |
Greg Kroah-Hartman | 9887372 | 2010-10-21 08:47:43 -0700 | [diff] [blame] | 469 | EXPORT_SYMBOL_GPL(vmbus_establish_gpadl); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 470 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 471 | /* |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 472 | * vmbus_teardown_gpadl -Teardown the specified GPADL handle |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 473 | */ |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 474 | int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 475 | { |
Greg Kroah-Hartman | 8225021 | 2009-08-26 15:16:04 -0700 | [diff] [blame] | 476 | struct vmbus_channel_gpadl_teardown *msg; |
Greg Kroah-Hartman | aded7165 | 2009-08-18 15:21:19 -0700 | [diff] [blame] | 477 | struct vmbus_channel_msginfo *info; |
Greg Kroah-Hartman | dd0813b | 2009-07-15 14:56:45 -0700 | [diff] [blame] | 478 | unsigned long flags; |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 479 | int ret, t; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 480 | |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 481 | info = kmalloc(sizeof(*info) + |
| 482 | sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL); |
Bill Pemberton | c3bf2e2 | 2010-05-05 15:27:34 -0400 | [diff] [blame] | 483 | if (!info) |
| 484 | return -ENOMEM; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 485 | |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 486 | init_completion(&info->waitevent); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 487 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 488 | msg = (struct vmbus_channel_gpadl_teardown *)info->msg; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 489 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 490 | msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN; |
| 491 | msg->child_relid = channel->offermsg.child_relid; |
| 492 | msg->gpadl = gpadl_handle; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 493 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 494 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 495 | list_add_tail(&info->msglistentry, |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 496 | &vmbus_connection.chn_msg_list); |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 497 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 498 | ret = vmbus_post_msg(msg, |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 499 | sizeof(struct vmbus_channel_gpadl_teardown)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 500 | |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 501 | BUG_ON(ret != 0); |
K. Y. Srinivasan | 40961de | 2011-06-06 15:50:05 -0700 | [diff] [blame] | 502 | t = wait_for_completion_timeout(&info->waitevent, 5*HZ); |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 503 | BUG_ON(t == 0); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 504 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 505 | /* Received a torndown response */ |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 506 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 507 | list_del(&info->msglistentry); |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 508 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 509 | |
Greg Kroah-Hartman | 8c69f52 | 2009-07-15 12:48:29 -0700 | [diff] [blame] | 510 | kfree(info); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 511 | return ret; |
| 512 | } |
Greg Kroah-Hartman | 18726d7 | 2010-10-21 08:39:59 -0700 | [diff] [blame] | 513 | EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 514 | |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 515 | static void vmbus_close_internal(struct vmbus_channel *channel) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 516 | { |
Greg Kroah-Hartman | 8225021 | 2009-08-26 15:16:04 -0700 | [diff] [blame] | 517 | struct vmbus_channel_close_channel *msg; |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 518 | int ret; |
K. Y. Srinivasan | dad76bf | 2011-08-27 11:31:33 -0700 | [diff] [blame] | 519 | unsigned long flags; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 520 | |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 521 | channel->state = CHANNEL_OPEN_STATE; |
| 522 | channel->sc_creation_callback = NULL; |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 523 | /* Stop callback and cancel the timer asap */ |
K. Y. Srinivasan | dad76bf | 2011-08-27 11:31:33 -0700 | [diff] [blame] | 524 | spin_lock_irqsave(&channel->inbound_lock, flags); |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 525 | channel->onchannel_callback = NULL; |
K. Y. Srinivasan | dad76bf | 2011-08-27 11:31:33 -0700 | [diff] [blame] | 526 | spin_unlock_irqrestore(&channel->inbound_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 527 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 528 | /* Send a closing message */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 529 | |
K. Y. Srinivasan | e9a27a9 | 2011-06-06 15:49:59 -0700 | [diff] [blame] | 530 | msg = &channel->close_msg.msg; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 531 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 532 | msg->header.msgtype = CHANNELMSG_CLOSECHANNEL; |
| 533 | msg->child_relid = channel->offermsg.child_relid; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 534 | |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 535 | ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 536 | |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 537 | BUG_ON(ret != 0); |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 538 | /* Tear down the gpadl for the channel's ring buffer */ |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 539 | if (channel->ringbuffer_gpadlhandle) |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 540 | vmbus_teardown_gpadl(channel, |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 541 | channel->ringbuffer_gpadlhandle); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 542 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 543 | /* Cleanup the ring buffers for this channel */ |
K. Y. Srinivasan | 2dba688 | 2011-05-10 07:55:22 -0700 | [diff] [blame] | 544 | hv_ringbuffer_cleanup(&channel->outbound); |
| 545 | hv_ringbuffer_cleanup(&channel->inbound); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 546 | |
K. Y. Srinivasan | df3493e | 2011-02-11 09:59:00 -0800 | [diff] [blame] | 547 | free_pages((unsigned long)channel->ringbuffer_pages, |
| 548 | get_order(channel->ringbuffer_pagecount * PAGE_SIZE)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 549 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 550 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 551 | } |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 552 | |
| 553 | /* |
| 554 | * vmbus_close - Close the specified channel |
| 555 | */ |
| 556 | void vmbus_close(struct vmbus_channel *channel) |
| 557 | { |
| 558 | struct list_head *cur, *tmp; |
| 559 | struct vmbus_channel *cur_channel; |
| 560 | |
| 561 | if (channel->primary_channel != NULL) { |
| 562 | /* |
| 563 | * We will only close sub-channels when |
| 564 | * the primary is closed. |
| 565 | */ |
| 566 | return; |
| 567 | } |
| 568 | /* |
| 569 | * Close all the sub-channels first and then close the |
| 570 | * primary channel. |
| 571 | */ |
| 572 | list_for_each_safe(cur, tmp, &channel->sc_list) { |
| 573 | cur_channel = list_entry(cur, struct vmbus_channel, sc_list); |
| 574 | if (cur_channel->state != CHANNEL_OPENED_STATE) |
| 575 | continue; |
| 576 | vmbus_close_internal(cur_channel); |
| 577 | } |
| 578 | /* |
| 579 | * Now close the primary. |
| 580 | */ |
| 581 | vmbus_close_internal(channel); |
| 582 | } |
Greg Kroah-Hartman | 70bfa30 | 2010-10-21 09:52:22 -0700 | [diff] [blame] | 583 | EXPORT_SYMBOL_GPL(vmbus_close); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 584 | |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 585 | /** |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 586 | * vmbus_sendpacket() - Send the specified buffer on the given channel |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 587 | * @channel: Pointer to vmbus_channel structure. |
| 588 | * @buffer: Pointer to the buffer you want to receive the data into. |
| 589 | * @bufferlen: Maximum size of what the the buffer will hold |
| 590 | * @requestid: Identifier of the request |
| 591 | * @type: Type of packet that is being send e.g. negotiate, time |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 592 | * packet etc. |
| 593 | * |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 594 | * Sends data in @buffer directly to hyper-v via the vmbus |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 595 | * This will send the data unparsed to hyper-v. |
| 596 | * |
| 597 | * Mainly used by Hyper-V drivers. |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 598 | */ |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 599 | int vmbus_sendpacket(struct vmbus_channel *channel, const void *buffer, |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 600 | u32 bufferlen, u64 requestid, |
| 601 | enum vmbus_packet_type type, u32 flags) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 602 | { |
Greg Kroah-Hartman | 8dc0a06 | 2009-08-27 16:02:36 -0700 | [diff] [blame] | 603 | struct vmpacket_descriptor desc; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 604 | u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen; |
Uwe Kleine-König | 7350968 | 2011-01-20 09:32:01 +0100 | [diff] [blame] | 605 | u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64)); |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 606 | struct scatterlist bufferlist[3]; |
| 607 | u64 aligned_data = 0; |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 608 | int ret; |
K. Y. Srinivasan | 98fa8cf | 2012-12-01 06:46:36 -0800 | [diff] [blame] | 609 | bool signal = false; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 610 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 611 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 612 | /* Setup the descriptor */ |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 613 | desc.type = type; /* VmbusPacketTypeDataInBand; */ |
| 614 | desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */ |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 615 | /* in 8-bytes granularity */ |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 616 | desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3; |
| 617 | desc.len8 = (u16)(packetlen_aligned >> 3); |
| 618 | desc.trans_id = requestid; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 619 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 620 | sg_init_table(bufferlist, 3); |
| 621 | sg_set_buf(&bufferlist[0], &desc, sizeof(struct vmpacket_descriptor)); |
| 622 | sg_set_buf(&bufferlist[1], buffer, bufferlen); |
| 623 | sg_set_buf(&bufferlist[2], &aligned_data, |
| 624 | packetlen_aligned - packetlen); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 625 | |
K. Y. Srinivasan | 98fa8cf | 2012-12-01 06:46:36 -0800 | [diff] [blame] | 626 | ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 627 | |
K. Y. Srinivasan | 98fa8cf | 2012-12-01 06:46:36 -0800 | [diff] [blame] | 628 | if (ret == 0 && signal) |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 629 | vmbus_setevent(channel); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 630 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 631 | return ret; |
| 632 | } |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 633 | EXPORT_SYMBOL(vmbus_sendpacket); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 634 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 635 | /* |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 636 | * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 637 | * packets using a GPADL Direct packet type. |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 638 | */ |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 639 | int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel, |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 640 | struct hv_page_buffer pagebuffers[], |
| 641 | u32 pagecount, void *buffer, u32 bufferlen, |
| 642 | u64 requestid) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 643 | { |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 644 | int ret; |
| 645 | int i; |
Haiyang Zhang | 430a8e9 | 2010-09-20 21:07:51 +0000 | [diff] [blame] | 646 | struct vmbus_channel_packet_page_buffer desc; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 647 | u32 descsize; |
| 648 | u32 packetlen; |
| 649 | u32 packetlen_aligned; |
| 650 | struct scatterlist bufferlist[3]; |
| 651 | u64 aligned_data = 0; |
K. Y. Srinivasan | 98fa8cf | 2012-12-01 06:46:36 -0800 | [diff] [blame] | 652 | bool signal = false; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 653 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 654 | if (pagecount > MAX_PAGE_BUFFER_COUNT) |
Bill Pemberton | 002b53e | 2010-05-05 15:27:39 -0400 | [diff] [blame] | 655 | return -EINVAL; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 656 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 657 | |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 658 | /* |
Haiyang Zhang | 430a8e9 | 2010-09-20 21:07:51 +0000 | [diff] [blame] | 659 | * 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] | 660 | * largest size we support |
| 661 | */ |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 662 | descsize = sizeof(struct vmbus_channel_packet_page_buffer) - |
| 663 | ((MAX_PAGE_BUFFER_COUNT - pagecount) * |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 664 | sizeof(struct hv_page_buffer)); |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 665 | packetlen = descsize + bufferlen; |
Uwe Kleine-König | 7350968 | 2011-01-20 09:32:01 +0100 | [diff] [blame] | 666 | packetlen_aligned = ALIGN(packetlen, sizeof(u64)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 667 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 668 | /* Setup the descriptor */ |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 669 | desc.type = VM_PKT_DATA_USING_GPA_DIRECT; |
Haiyang Zhang | 430a8e9 | 2010-09-20 21:07:51 +0000 | [diff] [blame] | 670 | desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 671 | desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */ |
| 672 | desc.length8 = (u16)(packetlen_aligned >> 3); |
| 673 | desc.transactionid = requestid; |
| 674 | desc.rangecount = pagecount; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 675 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 676 | for (i = 0; i < pagecount; i++) { |
Haiyang Zhang | ca623ad | 2011-01-26 12:12:11 -0800 | [diff] [blame] | 677 | desc.range[i].len = pagebuffers[i].len; |
| 678 | desc.range[i].offset = pagebuffers[i].offset; |
| 679 | desc.range[i].pfn = pagebuffers[i].pfn; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 680 | } |
| 681 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 682 | sg_init_table(bufferlist, 3); |
| 683 | sg_set_buf(&bufferlist[0], &desc, descsize); |
| 684 | sg_set_buf(&bufferlist[1], buffer, bufferlen); |
| 685 | sg_set_buf(&bufferlist[2], &aligned_data, |
| 686 | packetlen_aligned - packetlen); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 687 | |
K. Y. Srinivasan | 98fa8cf | 2012-12-01 06:46:36 -0800 | [diff] [blame] | 688 | ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 689 | |
K. Y. Srinivasan | 98fa8cf | 2012-12-01 06:46:36 -0800 | [diff] [blame] | 690 | if (ret == 0 && signal) |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 691 | vmbus_setevent(channel); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 692 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 693 | return ret; |
| 694 | } |
Greg Kroah-Hartman | 713efeb | 2010-10-21 09:29:54 -0700 | [diff] [blame] | 695 | EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 696 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 697 | /* |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 698 | * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 699 | * using a GPADL Direct packet type. |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 700 | */ |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 701 | int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel, |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 702 | struct hv_multipage_buffer *multi_pagebuffer, |
| 703 | void *buffer, u32 bufferlen, u64 requestid) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 704 | { |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 705 | int ret; |
Haiyang Zhang | 430a8e9 | 2010-09-20 21:07:51 +0000 | [diff] [blame] | 706 | struct vmbus_channel_packet_multipage_buffer desc; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 707 | u32 descsize; |
| 708 | u32 packetlen; |
| 709 | u32 packetlen_aligned; |
| 710 | struct scatterlist bufferlist[3]; |
| 711 | u64 aligned_data = 0; |
K. Y. Srinivasan | 98fa8cf | 2012-12-01 06:46:36 -0800 | [diff] [blame] | 712 | bool signal = false; |
Haiyang Zhang | ca623ad | 2011-01-26 12:12:11 -0800 | [diff] [blame] | 713 | u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset, |
| 714 | multi_pagebuffer->len); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 715 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 716 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 717 | if ((pfncount < 0) || (pfncount > MAX_MULTIPAGE_BUFFER_COUNT)) |
Bill Pemberton | 002b53e | 2010-05-05 15:27:39 -0400 | [diff] [blame] | 718 | return -EINVAL; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 719 | |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 720 | /* |
Haiyang Zhang | 430a8e9 | 2010-09-20 21:07:51 +0000 | [diff] [blame] | 721 | * Adjust the size down since vmbus_channel_packet_multipage_buffer is |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 722 | * the largest size we support |
| 723 | */ |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 724 | descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) - |
| 725 | ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) * |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 726 | sizeof(u64)); |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 727 | packetlen = descsize + bufferlen; |
Uwe Kleine-König | 7350968 | 2011-01-20 09:32:01 +0100 | [diff] [blame] | 728 | packetlen_aligned = ALIGN(packetlen, sizeof(u64)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 729 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 730 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 731 | /* Setup the descriptor */ |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 732 | desc.type = VM_PKT_DATA_USING_GPA_DIRECT; |
Haiyang Zhang | 430a8e9 | 2010-09-20 21:07:51 +0000 | [diff] [blame] | 733 | desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 734 | desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */ |
| 735 | desc.length8 = (u16)(packetlen_aligned >> 3); |
| 736 | desc.transactionid = requestid; |
Haiyang Zhang | 430a8e9 | 2010-09-20 21:07:51 +0000 | [diff] [blame] | 737 | desc.rangecount = 1; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 738 | |
Haiyang Zhang | ca623ad | 2011-01-26 12:12:11 -0800 | [diff] [blame] | 739 | desc.range.len = multi_pagebuffer->len; |
| 740 | desc.range.offset = multi_pagebuffer->offset; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 741 | |
Haiyang Zhang | ca623ad | 2011-01-26 12:12:11 -0800 | [diff] [blame] | 742 | memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array, |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 743 | pfncount * sizeof(u64)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 744 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 745 | sg_init_table(bufferlist, 3); |
| 746 | sg_set_buf(&bufferlist[0], &desc, descsize); |
| 747 | sg_set_buf(&bufferlist[1], buffer, bufferlen); |
| 748 | sg_set_buf(&bufferlist[2], &aligned_data, |
| 749 | packetlen_aligned - packetlen); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 750 | |
K. Y. Srinivasan | 98fa8cf | 2012-12-01 06:46:36 -0800 | [diff] [blame] | 751 | ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, &signal); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 752 | |
K. Y. Srinivasan | 98fa8cf | 2012-12-01 06:46:36 -0800 | [diff] [blame] | 753 | if (ret == 0 && signal) |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 754 | vmbus_setevent(channel); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 755 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 756 | return ret; |
| 757 | } |
Greg Kroah-Hartman | 4cb106f | 2010-10-21 09:23:59 -0700 | [diff] [blame] | 758 | EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer); |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 759 | |
| 760 | /** |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 761 | * vmbus_recvpacket() - Retrieve the user packet on the specified channel |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 762 | * @channel: Pointer to vmbus_channel structure. |
| 763 | * @buffer: Pointer to the buffer you want to receive the data into. |
| 764 | * @bufferlen: Maximum size of what the the buffer will hold |
| 765 | * @buffer_actual_len: The actual size of the data after it was received |
| 766 | * @requestid: Identifier of the request |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 767 | * |
| 768 | * Receives directly from the hyper-v vmbus and puts the data it received |
| 769 | * into Buffer. This will receive the data unparsed from hyper-v. |
| 770 | * |
| 771 | * Mainly used by Hyper-V drivers. |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 772 | */ |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 773 | int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer, |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 774 | u32 bufferlen, u32 *buffer_actual_len, u64 *requestid) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 775 | { |
Greg Kroah-Hartman | 8dc0a06 | 2009-08-27 16:02:36 -0700 | [diff] [blame] | 776 | struct vmpacket_descriptor desc; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 777 | u32 packetlen; |
| 778 | u32 userlen; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 779 | int ret; |
K. Y. Srinivasan | c2b8e52 | 2012-12-01 06:46:57 -0800 | [diff] [blame] | 780 | bool signal = false; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 781 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 782 | *buffer_actual_len = 0; |
| 783 | *requestid = 0; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 784 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 785 | |
K. Y. Srinivasan | a89186c | 2011-05-10 07:55:24 -0700 | [diff] [blame] | 786 | ret = hv_ringbuffer_peek(&channel->inbound, &desc, |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 787 | sizeof(struct vmpacket_descriptor)); |
K. Y. Srinivasan | dad76bf | 2011-08-27 11:31:33 -0700 | [diff] [blame] | 788 | if (ret != 0) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 789 | return 0; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 790 | |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 791 | packetlen = desc.len8 << 3; |
| 792 | userlen = packetlen - (desc.offset8 << 3); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 793 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 794 | *buffer_actual_len = userlen; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 795 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 796 | if (userlen > bufferlen) { |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 797 | |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 798 | pr_err("Buffer too small - got %d needs %d\n", |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 799 | bufferlen, userlen); |
K. Y. Srinivasan | 926ae526 | 2011-06-06 15:50:11 -0700 | [diff] [blame] | 800 | return -ETOOSMALL; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 803 | *requestid = desc.trans_id; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 804 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 805 | /* Copy over the packet to the user buffer */ |
K. Y. Srinivasan | 38397c8 | 2011-05-10 07:55:25 -0700 | [diff] [blame] | 806 | ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen, |
K. Y. Srinivasan | c2b8e52 | 2012-12-01 06:46:57 -0800 | [diff] [blame] | 807 | (desc.offset8 << 3), &signal); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 808 | |
K. Y. Srinivasan | c2b8e52 | 2012-12-01 06:46:57 -0800 | [diff] [blame] | 809 | if (signal) |
| 810 | vmbus_setevent(channel); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 811 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 812 | return 0; |
| 813 | } |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 814 | EXPORT_SYMBOL(vmbus_recvpacket); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 815 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 816 | /* |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 817 | * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 818 | */ |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 819 | int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer, |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 820 | u32 bufferlen, u32 *buffer_actual_len, |
| 821 | u64 *requestid) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 822 | { |
Greg Kroah-Hartman | 8dc0a06 | 2009-08-27 16:02:36 -0700 | [diff] [blame] | 823 | struct vmpacket_descriptor desc; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 824 | u32 packetlen; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 825 | int ret; |
K. Y. Srinivasan | c2b8e52 | 2012-12-01 06:46:57 -0800 | [diff] [blame] | 826 | bool signal = false; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 827 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 828 | *buffer_actual_len = 0; |
| 829 | *requestid = 0; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 830 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 831 | |
K. Y. Srinivasan | a89186c | 2011-05-10 07:55:24 -0700 | [diff] [blame] | 832 | ret = hv_ringbuffer_peek(&channel->inbound, &desc, |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 833 | sizeof(struct vmpacket_descriptor)); |
K. Y. Srinivasan | dad76bf | 2011-08-27 11:31:33 -0700 | [diff] [blame] | 834 | if (ret != 0) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 835 | return 0; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 836 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 837 | |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 838 | packetlen = desc.len8 << 3; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 839 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 840 | *buffer_actual_len = packetlen; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 841 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 842 | if (packetlen > bufferlen) { |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 843 | pr_err("Buffer too small - needed %d bytes but " |
| 844 | "got space for only %d bytes\n", |
| 845 | packetlen, bufferlen); |
K. Y. Srinivasan | 3d5cad9 | 2011-08-25 09:48:59 -0700 | [diff] [blame] | 846 | return -ENOBUFS; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 849 | *requestid = desc.trans_id; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 850 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 851 | /* Copy over the entire packet to the user buffer */ |
K. Y. Srinivasan | c2b8e52 | 2012-12-01 06:46:57 -0800 | [diff] [blame] | 852 | ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0, |
| 853 | &signal); |
| 854 | |
| 855 | if (signal) |
| 856 | vmbus_setevent(channel); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 857 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 858 | return 0; |
| 859 | } |
Greg Kroah-Hartman | adaee6b | 2010-10-21 09:09:23 -0700 | [diff] [blame] | 860 | EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw); |