Thomas Gleixner | 3b20eb2 | 2019-05-29 16:57:35 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 2 | /* |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 3 | * Copyright (c) 2009, Microsoft Corporation. |
| 4 | * |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 5 | * Authors: |
| 6 | * Haiyang Zhang <haiyangz@microsoft.com> |
| 7 | * Hank Janssen <hjanssen@microsoft.com> |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 8 | */ |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 10 | |
Greg Kroah-Hartman | 5654e93 | 2009-07-14 15:08:20 -0700 | [diff] [blame] | 11 | #include <linux/kernel.h> |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 12 | #include <linux/sched.h> |
| 13 | #include <linux/wait.h> |
Greg Kroah-Hartman | a0086dc | 2009-08-17 17:22:08 -0700 | [diff] [blame] | 14 | #include <linux/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 16 | #include <linux/module.h> |
Greg Kroah-Hartman | 46a9719 | 2011-10-04 12:29:52 -0700 | [diff] [blame] | 17 | #include <linux/hyperv.h> |
K. Y. Srinivasan | 011a7c3 | 2014-02-01 19:02:20 -0800 | [diff] [blame] | 18 | #include <linux/uio.h> |
Dexuan Cui | 63d55b2 | 2015-12-14 16:01:47 -0800 | [diff] [blame] | 19 | #include <linux/interrupt.h> |
Michael Kelley | 6ba3417 | 2018-08-02 03:08:24 +0000 | [diff] [blame] | 20 | #include <asm/page.h> |
Andrea Parri (Microsoft) | 5bf7468 | 2020-06-17 18:46:35 +0200 | [diff] [blame] | 21 | #include <asm/mshyperv.h> |
K. Y. Srinivasan | 3f335ea | 2011-05-12 19:34:15 -0700 | [diff] [blame] | 22 | |
K. Y. Srinivasan | 0f2a661 | 2011-05-12 19:34:28 -0700 | [diff] [blame] | 23 | #include "hyperv_vmbus.h" |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 24 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 25 | /* |
Boqun Feng | c1135c7 | 2020-09-16 11:48:09 +0800 | [diff] [blame] | 26 | * hv_gpadl_size - Return the real size of a gpadl, the size that Hyper-V uses |
| 27 | * |
| 28 | * For BUFFER gpadl, Hyper-V uses the exact same size as the guest does. |
| 29 | * |
| 30 | * For RING gpadl, in each ring, the guest uses one PAGE_SIZE as the header |
| 31 | * (because of the alignment requirement), however, the hypervisor only |
| 32 | * uses the first HV_HYP_PAGE_SIZE as the header, therefore leaving a |
| 33 | * (PAGE_SIZE - HV_HYP_PAGE_SIZE) gap. And since there are two rings in a |
| 34 | * ringbuffer, the total size for a RING gpadl that Hyper-V uses is the |
| 35 | * total size that the guest uses minus twice of the gap size. |
| 36 | */ |
| 37 | static inline u32 hv_gpadl_size(enum hv_gpadl_type type, u32 size) |
| 38 | { |
| 39 | switch (type) { |
| 40 | case HV_GPADL_BUFFER: |
| 41 | return size; |
| 42 | case HV_GPADL_RING: |
| 43 | /* The size of a ringbuffer must be page-aligned */ |
| 44 | BUG_ON(size % PAGE_SIZE); |
| 45 | /* |
| 46 | * Two things to notice here: |
| 47 | * 1) We're processing two ring buffers as a unit |
| 48 | * 2) We're skipping any space larger than HV_HYP_PAGE_SIZE in |
| 49 | * the first guest-size page of each of the two ring buffers. |
| 50 | * So we effectively subtract out two guest-size pages, and add |
| 51 | * back two Hyper-V size pages. |
| 52 | */ |
| 53 | return size - 2 * (PAGE_SIZE - HV_HYP_PAGE_SIZE); |
| 54 | } |
| 55 | BUG(); |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * hv_ring_gpadl_send_hvpgoffset - Calculate the send offset (in unit of |
| 61 | * HV_HYP_PAGE) in a ring gpadl based on the |
| 62 | * offset in the guest |
| 63 | * |
| 64 | * @offset: the offset (in bytes) where the send ringbuffer starts in the |
| 65 | * virtual address space of the guest |
| 66 | */ |
| 67 | static inline u32 hv_ring_gpadl_send_hvpgoffset(u32 offset) |
| 68 | { |
| 69 | |
| 70 | /* |
| 71 | * For RING gpadl, in each ring, the guest uses one PAGE_SIZE as the |
| 72 | * header (because of the alignment requirement), however, the |
| 73 | * hypervisor only uses the first HV_HYP_PAGE_SIZE as the header, |
| 74 | * therefore leaving a (PAGE_SIZE - HV_HYP_PAGE_SIZE) gap. |
| 75 | * |
| 76 | * And to calculate the effective send offset in gpadl, we need to |
| 77 | * substract this gap. |
| 78 | */ |
| 79 | return (offset - (PAGE_SIZE - HV_HYP_PAGE_SIZE)) >> HV_HYP_PAGE_SHIFT; |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * hv_gpadl_hvpfn - Return the Hyper-V page PFN of the @i th Hyper-V page in |
| 84 | * the gpadl |
| 85 | * |
| 86 | * @type: the type of the gpadl |
| 87 | * @kbuffer: the pointer to the gpadl in the guest |
| 88 | * @size: the total size (in bytes) of the gpadl |
| 89 | * @send_offset: the offset (in bytes) where the send ringbuffer starts in the |
| 90 | * virtual address space of the guest |
| 91 | * @i: the index |
| 92 | */ |
| 93 | static inline u64 hv_gpadl_hvpfn(enum hv_gpadl_type type, void *kbuffer, |
| 94 | u32 size, u32 send_offset, int i) |
| 95 | { |
| 96 | int send_idx = hv_ring_gpadl_send_hvpgoffset(send_offset); |
| 97 | unsigned long delta = 0UL; |
| 98 | |
| 99 | switch (type) { |
| 100 | case HV_GPADL_BUFFER: |
| 101 | break; |
| 102 | case HV_GPADL_RING: |
| 103 | if (i == 0) |
| 104 | delta = 0; |
| 105 | else if (i <= send_idx) |
| 106 | delta = PAGE_SIZE - HV_HYP_PAGE_SIZE; |
| 107 | else |
| 108 | delta = 2 * (PAGE_SIZE - HV_HYP_PAGE_SIZE); |
| 109 | break; |
| 110 | default: |
| 111 | BUG(); |
| 112 | break; |
| 113 | } |
| 114 | |
| 115 | return virt_to_hvpfn(kbuffer + delta + (HV_HYP_PAGE_SIZE * i)); |
| 116 | } |
| 117 | |
| 118 | /* |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 119 | * vmbus_setevent- Trigger an event notification on the specified |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 120 | * channel. |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 121 | */ |
K. Y. Srinivasan | 1f6ee4e | 2016-11-06 13:14:17 -0800 | [diff] [blame] | 122 | void vmbus_setevent(struct vmbus_channel *channel) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 123 | { |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 124 | struct hv_monitor_page *monitorpage; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 125 | |
Vitaly Kuznetsov | 991f8f1 | 2017-10-29 12:21:16 -0700 | [diff] [blame] | 126 | trace_vmbus_setevent(channel); |
| 127 | |
K. Y. Srinivasan | 3724287 | 2016-07-01 16:26:37 -0700 | [diff] [blame] | 128 | /* |
| 129 | * For channels marked as in "low latency" mode |
| 130 | * bypass the monitor page mechanism. |
| 131 | */ |
Stephen Hemminger | 5c1bec6 | 2017-02-05 17:20:31 -0700 | [diff] [blame] | 132 | if (channel->offermsg.monitor_allocated && !channel->low_latency) { |
| 133 | vmbus_send_interrupt(channel->offermsg.child_relid); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 134 | |
Greg Kroah-Hartman | 8681db4 | 2013-09-13 11:32:55 -0700 | [diff] [blame] | 135 | /* Get the child to parent monitor page */ |
| 136 | monitorpage = vmbus_connection.monitor_pages[1]; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 137 | |
Olaf Hering | 2235658 | 2011-03-21 14:41:37 +0100 | [diff] [blame] | 138 | sync_set_bit(channel->monitor_bit, |
Haiyang Zhang | f6feebe | 2010-11-08 14:04:39 -0800 | [diff] [blame] | 139 | (unsigned long *)&monitorpage->trigger_group |
| 140 | [channel->monitor_grp].pending); |
Bill Pemberton | 7c369f4 | 2009-07-29 17:00:11 -0400 | [diff] [blame] | 141 | |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 142 | } else { |
K. Y. Srinivasan | 21c3bef | 2012-12-01 06:46:43 -0800 | [diff] [blame] | 143 | vmbus_set_event(channel); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 144 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 145 | } |
K. Y. Srinivasan | 1f6ee4e | 2016-11-06 13:14:17 -0800 | [diff] [blame] | 146 | EXPORT_SYMBOL_GPL(vmbus_setevent); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 147 | |
Stephen Hemminger | ae6935e | 2018-09-14 09:10:17 -0700 | [diff] [blame] | 148 | /* vmbus_free_ring - drop mapping of ring buffer */ |
| 149 | void vmbus_free_ring(struct vmbus_channel *channel) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 150 | { |
Stephen Hemminger | ae6935e | 2018-09-14 09:10:17 -0700 | [diff] [blame] | 151 | hv_ringbuffer_cleanup(&channel->outbound); |
| 152 | hv_ringbuffer_cleanup(&channel->inbound); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 153 | |
Stephen Hemminger | ae6935e | 2018-09-14 09:10:17 -0700 | [diff] [blame] | 154 | if (channel->ringbuffer_page) { |
| 155 | __free_pages(channel->ringbuffer_page, |
| 156 | get_order(channel->ringbuffer_pagecount |
| 157 | << PAGE_SHIFT)); |
| 158 | channel->ringbuffer_page = NULL; |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 159 | } |
Stephen Hemminger | ae6935e | 2018-09-14 09:10:17 -0700 | [diff] [blame] | 160 | } |
| 161 | EXPORT_SYMBOL_GPL(vmbus_free_ring); |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 162 | |
Stephen Hemminger | ae6935e | 2018-09-14 09:10:17 -0700 | [diff] [blame] | 163 | /* vmbus_alloc_ring - allocate and map pages for ring buffer */ |
| 164 | int vmbus_alloc_ring(struct vmbus_channel *newchannel, |
| 165 | u32 send_size, u32 recv_size) |
| 166 | { |
| 167 | struct page *page; |
| 168 | int order; |
| 169 | |
| 170 | if (send_size % PAGE_SIZE || recv_size % PAGE_SIZE) |
| 171 | return -EINVAL; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 172 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 173 | /* Allocate the ring buffer */ |
Stephen Hemminger | ae6935e | 2018-09-14 09:10:17 -0700 | [diff] [blame] | 174 | order = get_order(send_size + recv_size); |
K. Y. Srinivasan | 294409d | 2015-05-31 21:27:03 -0700 | [diff] [blame] | 175 | page = alloc_pages_node(cpu_to_node(newchannel->target_cpu), |
Stephen Hemminger | 52a42c2 | 2018-09-14 09:10:16 -0700 | [diff] [blame] | 176 | GFP_KERNEL|__GFP_ZERO, order); |
K. Y. Srinivasan | 294409d | 2015-05-31 21:27:03 -0700 | [diff] [blame] | 177 | |
| 178 | if (!page) |
Stephen Hemminger | 52a42c2 | 2018-09-14 09:10:16 -0700 | [diff] [blame] | 179 | page = alloc_pages(GFP_KERNEL|__GFP_ZERO, order); |
K. Y. Srinivasan | df3493e | 2011-02-11 09:59:00 -0800 | [diff] [blame] | 180 | |
Stephen Hemminger | ae6935e | 2018-09-14 09:10:17 -0700 | [diff] [blame] | 181 | if (!page) |
| 182 | return -ENOMEM; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 183 | |
Stephen Hemminger | 52a42c2 | 2018-09-14 09:10:16 -0700 | [diff] [blame] | 184 | newchannel->ringbuffer_page = page; |
Stephen Hemminger | ae6935e | 2018-09-14 09:10:17 -0700 | [diff] [blame] | 185 | newchannel->ringbuffer_pagecount = (send_size + recv_size) >> PAGE_SHIFT; |
| 186 | newchannel->ringbuffer_send_offset = send_size >> PAGE_SHIFT; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 187 | |
Stephen Hemminger | ae6935e | 2018-09-14 09:10:17 -0700 | [diff] [blame] | 188 | return 0; |
| 189 | } |
| 190 | EXPORT_SYMBOL_GPL(vmbus_alloc_ring); |
K. Y. Srinivasan | 72a95cb | 2011-05-10 07:55:21 -0700 | [diff] [blame] | 191 | |
Dexuan Cui | 5c23a1a | 2016-01-27 22:29:40 -0800 | [diff] [blame] | 192 | /* Used for Hyper-V Socket: a guest client's connect() to the host */ |
Andy Shevchenko | 593db80 | 2019-01-10 16:25:32 +0200 | [diff] [blame] | 193 | int vmbus_send_tl_connect_request(const guid_t *shv_guest_servie_id, |
| 194 | const guid_t *shv_host_servie_id) |
Dexuan Cui | 5c23a1a | 2016-01-27 22:29:40 -0800 | [diff] [blame] | 195 | { |
| 196 | struct vmbus_channel_tl_connect_request conn_msg; |
Vitaly Kuznetsov | 98f31a0 | 2017-10-29 12:21:15 -0700 | [diff] [blame] | 197 | int ret; |
Dexuan Cui | 5c23a1a | 2016-01-27 22:29:40 -0800 | [diff] [blame] | 198 | |
| 199 | memset(&conn_msg, 0, sizeof(conn_msg)); |
| 200 | conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST; |
| 201 | conn_msg.guest_endpoint_id = *shv_guest_servie_id; |
| 202 | conn_msg.host_service_id = *shv_host_servie_id; |
| 203 | |
Vitaly Kuznetsov | 98f31a0 | 2017-10-29 12:21:15 -0700 | [diff] [blame] | 204 | ret = vmbus_post_msg(&conn_msg, sizeof(conn_msg), true); |
| 205 | |
| 206 | trace_vmbus_send_tl_connect_request(&conn_msg, ret); |
| 207 | |
| 208 | return ret; |
Dexuan Cui | 5c23a1a | 2016-01-27 22:29:40 -0800 | [diff] [blame] | 209 | } |
| 210 | EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request); |
| 211 | |
Andrea Parri (Microsoft) | 870ced0 | 2021-04-16 16:34:48 +0200 | [diff] [blame] | 212 | static int send_modifychannel_without_ack(struct vmbus_channel *channel, u32 target_vp) |
| 213 | { |
| 214 | struct vmbus_channel_modifychannel msg; |
| 215 | int ret; |
| 216 | |
| 217 | memset(&msg, 0, sizeof(msg)); |
| 218 | msg.header.msgtype = CHANNELMSG_MODIFYCHANNEL; |
| 219 | msg.child_relid = channel->offermsg.child_relid; |
| 220 | msg.target_vp = target_vp; |
| 221 | |
| 222 | ret = vmbus_post_msg(&msg, sizeof(msg), true); |
| 223 | trace_vmbus_send_modifychannel(&msg, ret); |
| 224 | |
| 225 | return ret; |
| 226 | } |
| 227 | |
| 228 | static int send_modifychannel_with_ack(struct vmbus_channel *channel, u32 target_vp) |
| 229 | { |
| 230 | struct vmbus_channel_modifychannel *msg; |
| 231 | struct vmbus_channel_msginfo *info; |
| 232 | unsigned long flags; |
| 233 | int ret; |
| 234 | |
| 235 | info = kzalloc(sizeof(struct vmbus_channel_msginfo) + |
| 236 | sizeof(struct vmbus_channel_modifychannel), |
| 237 | GFP_KERNEL); |
| 238 | if (!info) |
| 239 | return -ENOMEM; |
| 240 | |
| 241 | init_completion(&info->waitevent); |
| 242 | info->waiting_channel = channel; |
| 243 | |
| 244 | msg = (struct vmbus_channel_modifychannel *)info->msg; |
| 245 | msg->header.msgtype = CHANNELMSG_MODIFYCHANNEL; |
| 246 | msg->child_relid = channel->offermsg.child_relid; |
| 247 | msg->target_vp = target_vp; |
| 248 | |
| 249 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
| 250 | list_add_tail(&info->msglistentry, &vmbus_connection.chn_msg_list); |
| 251 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
| 252 | |
| 253 | ret = vmbus_post_msg(msg, sizeof(*msg), true); |
| 254 | trace_vmbus_send_modifychannel(msg, ret); |
| 255 | if (ret != 0) { |
| 256 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
| 257 | list_del(&info->msglistentry); |
| 258 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
| 259 | goto free_info; |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Release channel_mutex; otherwise, vmbus_onoffer_rescind() could block on |
| 264 | * the mutex and be unable to signal the completion. |
| 265 | * |
| 266 | * See the caller target_cpu_store() for information about the usage of the |
| 267 | * mutex. |
| 268 | */ |
| 269 | mutex_unlock(&vmbus_connection.channel_mutex); |
| 270 | wait_for_completion(&info->waitevent); |
| 271 | mutex_lock(&vmbus_connection.channel_mutex); |
| 272 | |
| 273 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
| 274 | list_del(&info->msglistentry); |
| 275 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
| 276 | |
| 277 | if (info->response.modify_response.status) |
| 278 | ret = -EAGAIN; |
| 279 | |
| 280 | free_info: |
| 281 | kfree(info); |
| 282 | return ret; |
| 283 | } |
| 284 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 285 | /* |
Andrea Parri (Microsoft) | 7527810 | 2020-04-06 02:15:13 +0200 | [diff] [blame] | 286 | * Set/change the vCPU (@target_vp) the channel (@child_relid) will interrupt. |
| 287 | * |
Andrea Parri (Microsoft) | 870ced0 | 2021-04-16 16:34:48 +0200 | [diff] [blame] | 288 | * CHANNELMSG_MODIFYCHANNEL messages are aynchronous. When VMbus version 5.3 |
| 289 | * or later is negotiated, Hyper-V always sends an ACK in response to such a |
| 290 | * message. For VMbus version 5.2 and earlier, it never sends an ACK. With- |
| 291 | * out an ACK, we can not know when the host will stop interrupting the "old" |
| 292 | * vCPU and start interrupting the "new" vCPU for the given channel. |
Andrea Parri (Microsoft) | 7527810 | 2020-04-06 02:15:13 +0200 | [diff] [blame] | 293 | * |
| 294 | * The CHANNELMSG_MODIFYCHANNEL message type is supported since VMBus version |
| 295 | * VERSION_WIN10_V4_1. |
| 296 | */ |
Andrea Parri (Microsoft) | 870ced0 | 2021-04-16 16:34:48 +0200 | [diff] [blame] | 297 | int vmbus_send_modifychannel(struct vmbus_channel *channel, u32 target_vp) |
Andrea Parri (Microsoft) | 7527810 | 2020-04-06 02:15:13 +0200 | [diff] [blame] | 298 | { |
Andrea Parri (Microsoft) | 870ced0 | 2021-04-16 16:34:48 +0200 | [diff] [blame] | 299 | if (vmbus_proto_version >= VERSION_WIN10_V5_3) |
| 300 | return send_modifychannel_with_ack(channel, target_vp); |
| 301 | return send_modifychannel_without_ack(channel, target_vp); |
Andrea Parri (Microsoft) | 7527810 | 2020-04-06 02:15:13 +0200 | [diff] [blame] | 302 | } |
| 303 | EXPORT_SYMBOL_GPL(vmbus_send_modifychannel); |
| 304 | |
| 305 | /* |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 306 | * create_gpadl_header - Creates a gpadl for the specified buffer |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 307 | */ |
Boqun Feng | c1135c7 | 2020-09-16 11:48:09 +0800 | [diff] [blame] | 308 | static int create_gpadl_header(enum hv_gpadl_type type, void *kbuffer, |
| 309 | u32 size, u32 send_offset, |
Vitaly Kuznetsov | 4d63763 | 2016-06-03 17:09:23 -0700 | [diff] [blame] | 310 | struct vmbus_channel_msginfo **msginfo) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 311 | { |
| 312 | int i; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 313 | int pagecount; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 314 | struct vmbus_channel_gpadl_header *gpadl_header; |
| 315 | struct vmbus_channel_gpadl_body *gpadl_body; |
| 316 | struct vmbus_channel_msginfo *msgheader; |
| 317 | struct vmbus_channel_msginfo *msgbody = NULL; |
| 318 | u32 msgsize; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 319 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 320 | int pfnsum, pfncount, pfnleft, pfncurr, pfnsize; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 321 | |
Boqun Feng | c1135c7 | 2020-09-16 11:48:09 +0800 | [diff] [blame] | 322 | pagecount = hv_gpadl_size(type, size) >> HV_HYP_PAGE_SHIFT; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 323 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 324 | /* do we need a gpadl body msg */ |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 325 | pfnsize = MAX_SIZE_CHANNEL_MESSAGE - |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 326 | sizeof(struct vmbus_channel_gpadl_header) - |
| 327 | sizeof(struct gpa_range); |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 328 | pfncount = pfnsize / sizeof(u64); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 329 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 330 | if (pagecount > pfncount) { |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 331 | /* we need a gpadl body */ |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 332 | /* fill in the 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) + pfncount * sizeof(u64); |
| 336 | msgheader = kzalloc(msgsize, GFP_KERNEL); |
| 337 | if (!msgheader) |
Bill Pemberton | d1c250b | 2010-05-05 15:27:35 -0400 | [diff] [blame] | 338 | goto nomem; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 339 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 340 | INIT_LIST_HEAD(&msgheader->submsglist); |
| 341 | msgheader->msgsize = msgsize; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 342 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 343 | gpadl_header = (struct vmbus_channel_gpadl_header *) |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 344 | msgheader->msg; |
| 345 | gpadl_header->rangecount = 1; |
| 346 | gpadl_header->range_buflen = sizeof(struct gpa_range) + |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 347 | pagecount * sizeof(u64); |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 348 | gpadl_header->range[0].byte_offset = 0; |
Boqun Feng | c1135c7 | 2020-09-16 11:48:09 +0800 | [diff] [blame] | 349 | gpadl_header->range[0].byte_count = hv_gpadl_size(type, size); |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 350 | for (i = 0; i < pfncount; i++) |
Boqun Feng | c1135c7 | 2020-09-16 11:48:09 +0800 | [diff] [blame] | 351 | gpadl_header->range[0].pfn_array[i] = hv_gpadl_hvpfn( |
| 352 | type, kbuffer, size, send_offset, i); |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 353 | *msginfo = msgheader; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 354 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 355 | pfnsum = pfncount; |
| 356 | pfnleft = pagecount - pfncount; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 357 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 358 | /* how many pfns can we fit */ |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 359 | pfnsize = MAX_SIZE_CHANNEL_MESSAGE - |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 360 | sizeof(struct vmbus_channel_gpadl_body); |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 361 | pfncount = pfnsize / sizeof(u64); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 362 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 363 | /* fill in the body */ |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 364 | while (pfnleft) { |
| 365 | if (pfnleft > pfncount) |
| 366 | pfncurr = pfncount; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 367 | else |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 368 | pfncurr = pfnleft; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 369 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 370 | msgsize = sizeof(struct vmbus_channel_msginfo) + |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 371 | sizeof(struct vmbus_channel_gpadl_body) + |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 372 | pfncurr * sizeof(u64); |
| 373 | msgbody = kzalloc(msgsize, GFP_KERNEL); |
K. Y. Srinivasan | f38cf9c | 2011-06-06 15:50:06 -0700 | [diff] [blame] | 374 | |
| 375 | if (!msgbody) { |
| 376 | struct vmbus_channel_msginfo *pos = NULL; |
| 377 | struct vmbus_channel_msginfo *tmp = NULL; |
| 378 | /* |
| 379 | * Free up all the allocated messages. |
| 380 | */ |
| 381 | list_for_each_entry_safe(pos, tmp, |
| 382 | &msgheader->submsglist, |
| 383 | msglistentry) { |
| 384 | |
| 385 | list_del(&pos->msglistentry); |
| 386 | kfree(pos); |
| 387 | } |
| 388 | |
Bill Pemberton | d1c250b | 2010-05-05 15:27:35 -0400 | [diff] [blame] | 389 | goto nomem; |
K. Y. Srinivasan | f38cf9c | 2011-06-06 15:50:06 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 392 | msgbody->msgsize = msgsize; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 393 | gpadl_body = |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 394 | (struct vmbus_channel_gpadl_body *)msgbody->msg; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 395 | |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 396 | /* |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 397 | * Gpadl is u32 and we are using a pointer which could |
| 398 | * be 64-bit |
K. Y. Srinivasan | f27df64 | 2011-06-06 15:49:56 -0700 | [diff] [blame] | 399 | * This is governed by the guest/host protocol and |
Stephen Hemminger | bdc1dd4 | 2017-03-04 18:27:14 -0700 | [diff] [blame] | 400 | * so the hypervisor guarantees that this is ok. |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 401 | */ |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 402 | for (i = 0; i < pfncurr; i++) |
Boqun Feng | c1135c7 | 2020-09-16 11:48:09 +0800 | [diff] [blame] | 403 | gpadl_body->pfn[i] = hv_gpadl_hvpfn(type, |
| 404 | kbuffer, size, send_offset, pfnsum + i); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 405 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 406 | /* add to msg header */ |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 407 | list_add_tail(&msgbody->msglistentry, |
| 408 | &msgheader->submsglist); |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 409 | pfnsum += pfncurr; |
| 410 | pfnleft -= pfncurr; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 411 | } |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 412 | } else { |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 413 | /* everything fits in a header */ |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 414 | msgsize = sizeof(struct vmbus_channel_msginfo) + |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 415 | sizeof(struct vmbus_channel_gpadl_header) + |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 416 | sizeof(struct gpa_range) + pagecount * sizeof(u64); |
| 417 | msgheader = kzalloc(msgsize, GFP_KERNEL); |
| 418 | if (msgheader == NULL) |
Kulikov Vasiliy | e3eb7cd | 2010-07-16 20:13:51 +0400 | [diff] [blame] | 419 | goto nomem; |
Vitaly Kuznetsov | 4d63763 | 2016-06-03 17:09:23 -0700 | [diff] [blame] | 420 | |
| 421 | INIT_LIST_HEAD(&msgheader->submsglist); |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 422 | msgheader->msgsize = msgsize; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 423 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 424 | gpadl_header = (struct vmbus_channel_gpadl_header *) |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 425 | msgheader->msg; |
| 426 | gpadl_header->rangecount = 1; |
| 427 | gpadl_header->range_buflen = sizeof(struct gpa_range) + |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 428 | pagecount * sizeof(u64); |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 429 | gpadl_header->range[0].byte_offset = 0; |
Boqun Feng | c1135c7 | 2020-09-16 11:48:09 +0800 | [diff] [blame] | 430 | gpadl_header->range[0].byte_count = hv_gpadl_size(type, size); |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 431 | for (i = 0; i < pagecount; i++) |
Boqun Feng | c1135c7 | 2020-09-16 11:48:09 +0800 | [diff] [blame] | 432 | gpadl_header->range[0].pfn_array[i] = hv_gpadl_hvpfn( |
| 433 | type, kbuffer, size, send_offset, i); |
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 | *msginfo = msgheader; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | return 0; |
Bill Pemberton | d1c250b | 2010-05-05 15:27:35 -0400 | [diff] [blame] | 439 | nomem: |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 440 | kfree(msgheader); |
| 441 | kfree(msgbody); |
Bill Pemberton | d1c250b | 2010-05-05 15:27:35 -0400 | [diff] [blame] | 442 | return -ENOMEM; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 445 | /* |
Boqun Feng | c1135c7 | 2020-09-16 11:48:09 +0800 | [diff] [blame] | 446 | * __vmbus_establish_gpadl - Establish a GPADL for a buffer or ringbuffer |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 447 | * |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 448 | * @channel: a channel |
Boqun Feng | c1135c7 | 2020-09-16 11:48:09 +0800 | [diff] [blame] | 449 | * @type: the type of the corresponding GPADL, only meaningful for the guest. |
Haiyang Zhang | b679ef7 | 2014-01-27 15:03:42 -0800 | [diff] [blame] | 450 | * @kbuffer: from kmalloc or vmalloc |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 451 | * @size: page-size multiple |
Boqun Feng | c1135c7 | 2020-09-16 11:48:09 +0800 | [diff] [blame] | 452 | * @send_offset: the offset (in bytes) where the send ring buffer starts, |
Vasanth | f850a4c | 2021-02-19 22:43:11 +0530 | [diff] [blame] | 453 | * should be 0 for BUFFER type gpadl |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 454 | * @gpadl_handle: some funky thing |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 455 | */ |
Boqun Feng | c1135c7 | 2020-09-16 11:48:09 +0800 | [diff] [blame] | 456 | static int __vmbus_establish_gpadl(struct vmbus_channel *channel, |
| 457 | enum hv_gpadl_type type, void *kbuffer, |
| 458 | u32 size, u32 send_offset, |
| 459 | u32 *gpadl_handle) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 460 | { |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 461 | struct vmbus_channel_gpadl_header *gpadlmsg; |
| 462 | struct vmbus_channel_gpadl_body *gpadl_body; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 463 | struct vmbus_channel_msginfo *msginfo = NULL; |
Vitaly Kuznetsov | 7cc80c9 | 2016-06-03 17:09:24 -0700 | [diff] [blame] | 464 | struct vmbus_channel_msginfo *submsginfo, *tmp; |
Bill Pemberton | 53af545 | 2009-09-11 21:46:44 -0400 | [diff] [blame] | 465 | struct list_head *curr; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 466 | u32 next_gpadl_handle; |
Greg Kroah-Hartman | dd0813b | 2009-07-15 14:56:45 -0700 | [diff] [blame] | 467 | unsigned long flags; |
Bill Pemberton | c3bf2e2 | 2010-05-05 15:27:34 -0400 | [diff] [blame] | 468 | int ret = 0; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 469 | |
K. Y. Srinivasan | 9f52a16 | 2015-01-09 23:54:33 -0800 | [diff] [blame] | 470 | next_gpadl_handle = |
| 471 | (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 472 | |
Boqun Feng | c1135c7 | 2020-09-16 11:48:09 +0800 | [diff] [blame] | 473 | ret = create_gpadl_header(type, kbuffer, size, send_offset, &msginfo); |
Bill Pemberton | c3bf2e2 | 2010-05-05 15:27:34 -0400 | [diff] [blame] | 474 | if (ret) |
| 475 | return ret; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 476 | |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 477 | init_completion(&msginfo->waitevent); |
K. Y. Srinivasan | ccb61f8 | 2016-12-22 16:54:00 -0800 | [diff] [blame] | 478 | msginfo->waiting_channel = channel; |
Bill Pemberton | c3bf2e2 | 2010-05-05 15:27:34 -0400 | [diff] [blame] | 479 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 480 | gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg; |
| 481 | gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER; |
| 482 | gpadlmsg->child_relid = channel->offermsg.child_relid; |
| 483 | gpadlmsg->gpadl = next_gpadl_handle; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 484 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 485 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 486 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 487 | list_add_tail(&msginfo->msglistentry, |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 488 | &vmbus_connection.chn_msg_list); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 489 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 490 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 491 | |
K. Y. Srinivasan | 6f3d791 | 2017-08-11 10:03:59 -0700 | [diff] [blame] | 492 | if (channel->rescind) { |
| 493 | ret = -ENODEV; |
| 494 | goto cleanup; |
| 495 | } |
| 496 | |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 497 | ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize - |
Vitaly Kuznetsov | c0bb039 | 2016-12-07 01:16:24 -0800 | [diff] [blame] | 498 | sizeof(*msginfo), true); |
Vitaly Kuznetsov | 69edbd5 | 2017-10-29 12:21:11 -0700 | [diff] [blame] | 499 | |
| 500 | trace_vmbus_establish_gpadl_header(gpadlmsg, ret); |
| 501 | |
Hank Janssen | 98e0870 | 2011-03-29 13:58:44 -0700 | [diff] [blame] | 502 | if (ret != 0) |
K. Y. Srinivasan | 00d760b | 2011-06-06 15:50:12 -0700 | [diff] [blame] | 503 | goto cleanup; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 504 | |
Vitaly Kuznetsov | 4d63763 | 2016-06-03 17:09:23 -0700 | [diff] [blame] | 505 | list_for_each(curr, &msginfo->submsglist) { |
| 506 | submsginfo = (struct vmbus_channel_msginfo *)curr; |
| 507 | gpadl_body = |
| 508 | (struct vmbus_channel_gpadl_body *)submsginfo->msg; |
Bill Pemberton | 53af545 | 2009-09-11 21:46:44 -0400 | [diff] [blame] | 509 | |
Vitaly Kuznetsov | 4d63763 | 2016-06-03 17:09:23 -0700 | [diff] [blame] | 510 | gpadl_body->header.msgtype = |
| 511 | CHANNELMSG_GPADL_BODY; |
| 512 | gpadl_body->gpadl = next_gpadl_handle; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 513 | |
Vitaly Kuznetsov | 4d63763 | 2016-06-03 17:09:23 -0700 | [diff] [blame] | 514 | ret = vmbus_post_msg(gpadl_body, |
Vitaly Kuznetsov | c0bb039 | 2016-12-07 01:16:24 -0800 | [diff] [blame] | 515 | submsginfo->msgsize - sizeof(*submsginfo), |
| 516 | true); |
Vitaly Kuznetsov | 69edbd5 | 2017-10-29 12:21:11 -0700 | [diff] [blame] | 517 | |
| 518 | trace_vmbus_establish_gpadl_body(gpadl_body, ret); |
| 519 | |
Vitaly Kuznetsov | 4d63763 | 2016-06-03 17:09:23 -0700 | [diff] [blame] | 520 | if (ret != 0) |
| 521 | goto cleanup; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 522 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 523 | } |
K. Y. Srinivasan | 72c6b71 | 2014-08-27 16:25:34 -0700 | [diff] [blame] | 524 | wait_for_completion(&msginfo->waitevent); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 525 | |
Dexuan Cui | eceb059 | 2018-11-26 02:29:56 +0000 | [diff] [blame] | 526 | if (msginfo->response.gpadl_created.creation_status != 0) { |
| 527 | pr_err("Failed to establish GPADL: err = 0x%x\n", |
| 528 | msginfo->response.gpadl_created.creation_status); |
| 529 | |
| 530 | ret = -EDQUOT; |
| 531 | goto cleanup; |
| 532 | } |
| 533 | |
K. Y. Srinivasan | ccb61f8 | 2016-12-22 16:54:00 -0800 | [diff] [blame] | 534 | if (channel->rescind) { |
| 535 | ret = -ENODEV; |
| 536 | goto cleanup; |
| 537 | } |
| 538 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 539 | /* At this point, we received the gpadl created msg */ |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 540 | *gpadl_handle = gpadlmsg->gpadl; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 541 | |
K. Y. Srinivasan | 00d760b | 2011-06-06 15:50:12 -0700 | [diff] [blame] | 542 | cleanup: |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 543 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 544 | list_del(&msginfo->msglistentry); |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 545 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Vitaly Kuznetsov | 7cc80c9 | 2016-06-03 17:09:24 -0700 | [diff] [blame] | 546 | list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist, |
| 547 | msglistentry) { |
| 548 | kfree(submsginfo); |
| 549 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 550 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 551 | kfree(msginfo); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 552 | return ret; |
| 553 | } |
Boqun Feng | c1135c7 | 2020-09-16 11:48:09 +0800 | [diff] [blame] | 554 | |
| 555 | /* |
| 556 | * vmbus_establish_gpadl - Establish a GPADL for the specified buffer |
| 557 | * |
| 558 | * @channel: a channel |
| 559 | * @kbuffer: from kmalloc or vmalloc |
| 560 | * @size: page-size multiple |
| 561 | * @gpadl_handle: some funky thing |
| 562 | */ |
| 563 | int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer, |
| 564 | u32 size, u32 *gpadl_handle) |
| 565 | { |
| 566 | return __vmbus_establish_gpadl(channel, HV_GPADL_BUFFER, kbuffer, size, |
| 567 | 0U, gpadl_handle); |
| 568 | } |
Greg Kroah-Hartman | 9887372 | 2010-10-21 08:47:43 -0700 | [diff] [blame] | 569 | EXPORT_SYMBOL_GPL(vmbus_establish_gpadl); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 570 | |
Andres Beltran | e8b7db3 | 2020-11-09 11:04:00 +0100 | [diff] [blame] | 571 | /** |
| 572 | * request_arr_init - Allocates memory for the requestor array. Each slot |
| 573 | * keeps track of the next available slot in the array. Initially, each |
| 574 | * slot points to the next one (as in a Linked List). The last slot |
| 575 | * does not point to anything, so its value is U64_MAX by default. |
| 576 | * @size The size of the array |
| 577 | */ |
| 578 | static u64 *request_arr_init(u32 size) |
| 579 | { |
| 580 | int i; |
| 581 | u64 *req_arr; |
| 582 | |
| 583 | req_arr = kcalloc(size, sizeof(u64), GFP_KERNEL); |
| 584 | if (!req_arr) |
| 585 | return NULL; |
| 586 | |
| 587 | for (i = 0; i < size - 1; i++) |
| 588 | req_arr[i] = i + 1; |
| 589 | |
| 590 | /* Last slot (no more available slots) */ |
| 591 | req_arr[i] = U64_MAX; |
| 592 | |
| 593 | return req_arr; |
| 594 | } |
| 595 | |
| 596 | /* |
| 597 | * vmbus_alloc_requestor - Initializes @rqstor's fields. |
| 598 | * Index 0 is the first free slot |
| 599 | * @size: Size of the requestor array |
| 600 | */ |
| 601 | static int vmbus_alloc_requestor(struct vmbus_requestor *rqstor, u32 size) |
| 602 | { |
| 603 | u64 *rqst_arr; |
| 604 | unsigned long *bitmap; |
| 605 | |
| 606 | rqst_arr = request_arr_init(size); |
| 607 | if (!rqst_arr) |
| 608 | return -ENOMEM; |
| 609 | |
| 610 | bitmap = bitmap_zalloc(size, GFP_KERNEL); |
| 611 | if (!bitmap) { |
| 612 | kfree(rqst_arr); |
| 613 | return -ENOMEM; |
| 614 | } |
| 615 | |
| 616 | rqstor->req_arr = rqst_arr; |
| 617 | rqstor->req_bitmap = bitmap; |
| 618 | rqstor->size = size; |
| 619 | rqstor->next_request_id = 0; |
| 620 | spin_lock_init(&rqstor->req_lock); |
| 621 | |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | /* |
| 626 | * vmbus_free_requestor - Frees memory allocated for @rqstor |
| 627 | * @rqstor: Pointer to the requestor struct |
| 628 | */ |
| 629 | static void vmbus_free_requestor(struct vmbus_requestor *rqstor) |
| 630 | { |
| 631 | kfree(rqstor->req_arr); |
| 632 | bitmap_free(rqstor->req_bitmap); |
| 633 | } |
| 634 | |
Boqun Feng | edd9bbc | 2020-09-16 11:48:08 +0800 | [diff] [blame] | 635 | static int __vmbus_open(struct vmbus_channel *newchannel, |
| 636 | void *userdata, u32 userdatalen, |
| 637 | void (*onchannelcallback)(void *context), void *context) |
| 638 | { |
| 639 | struct vmbus_channel_open_channel *open_msg; |
| 640 | struct vmbus_channel_msginfo *open_info = NULL; |
| 641 | struct page *page = newchannel->ringbuffer_page; |
| 642 | u32 send_pages, recv_pages; |
| 643 | unsigned long flags; |
| 644 | int err; |
| 645 | |
| 646 | if (userdatalen > MAX_USER_DEFINED_BYTES) |
| 647 | return -EINVAL; |
| 648 | |
| 649 | send_pages = newchannel->ringbuffer_send_offset; |
| 650 | recv_pages = newchannel->ringbuffer_pagecount - send_pages; |
| 651 | |
| 652 | if (newchannel->state != CHANNEL_OPEN_STATE) |
| 653 | return -EINVAL; |
| 654 | |
Andres Beltran | e8b7db3 | 2020-11-09 11:04:00 +0100 | [diff] [blame] | 655 | /* Create and init requestor */ |
| 656 | if (newchannel->rqstor_size) { |
| 657 | if (vmbus_alloc_requestor(&newchannel->requestor, newchannel->rqstor_size)) |
| 658 | return -ENOMEM; |
| 659 | } |
| 660 | |
Boqun Feng | edd9bbc | 2020-09-16 11:48:08 +0800 | [diff] [blame] | 661 | newchannel->state = CHANNEL_OPENING_STATE; |
| 662 | newchannel->onchannel_callback = onchannelcallback; |
| 663 | newchannel->channel_callback_context = context; |
| 664 | |
Andres Beltran | adae1e9 | 2021-04-08 18:14:39 +0200 | [diff] [blame^] | 665 | if (!newchannel->max_pkt_size) |
| 666 | newchannel->max_pkt_size = VMBUS_DEFAULT_MAX_PKT_SIZE; |
| 667 | |
| 668 | err = hv_ringbuffer_init(&newchannel->outbound, page, send_pages, 0); |
Boqun Feng | edd9bbc | 2020-09-16 11:48:08 +0800 | [diff] [blame] | 669 | if (err) |
| 670 | goto error_clean_ring; |
| 671 | |
Andres Beltran | adae1e9 | 2021-04-08 18:14:39 +0200 | [diff] [blame^] | 672 | err = hv_ringbuffer_init(&newchannel->inbound, &page[send_pages], |
| 673 | recv_pages, newchannel->max_pkt_size); |
Boqun Feng | edd9bbc | 2020-09-16 11:48:08 +0800 | [diff] [blame] | 674 | if (err) |
| 675 | goto error_clean_ring; |
| 676 | |
| 677 | /* Establish the gpadl for the ring buffer */ |
| 678 | newchannel->ringbuffer_gpadlhandle = 0; |
| 679 | |
Boqun Feng | c1135c7 | 2020-09-16 11:48:09 +0800 | [diff] [blame] | 680 | err = __vmbus_establish_gpadl(newchannel, HV_GPADL_RING, |
| 681 | page_address(newchannel->ringbuffer_page), |
| 682 | (send_pages + recv_pages) << PAGE_SHIFT, |
| 683 | newchannel->ringbuffer_send_offset << PAGE_SHIFT, |
| 684 | &newchannel->ringbuffer_gpadlhandle); |
Boqun Feng | edd9bbc | 2020-09-16 11:48:08 +0800 | [diff] [blame] | 685 | if (err) |
| 686 | goto error_clean_ring; |
| 687 | |
| 688 | /* Create and init the channel open message */ |
Andrea Parri (Microsoft) | e99c4af | 2020-12-09 08:08:22 +0100 | [diff] [blame] | 689 | open_info = kzalloc(sizeof(*open_info) + |
Boqun Feng | edd9bbc | 2020-09-16 11:48:08 +0800 | [diff] [blame] | 690 | sizeof(struct vmbus_channel_open_channel), |
| 691 | GFP_KERNEL); |
| 692 | if (!open_info) { |
| 693 | err = -ENOMEM; |
| 694 | goto error_free_gpadl; |
| 695 | } |
| 696 | |
| 697 | init_completion(&open_info->waitevent); |
| 698 | open_info->waiting_channel = newchannel; |
| 699 | |
| 700 | open_msg = (struct vmbus_channel_open_channel *)open_info->msg; |
| 701 | open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL; |
| 702 | open_msg->openid = newchannel->offermsg.child_relid; |
| 703 | open_msg->child_relid = newchannel->offermsg.child_relid; |
| 704 | open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle; |
Boqun Feng | c1135c7 | 2020-09-16 11:48:09 +0800 | [diff] [blame] | 705 | /* |
| 706 | * The unit of ->downstream_ringbuffer_pageoffset is HV_HYP_PAGE and |
| 707 | * the unit of ->ringbuffer_send_offset (i.e. send_pages) is PAGE, so |
| 708 | * here we calculate it into HV_HYP_PAGE. |
| 709 | */ |
| 710 | open_msg->downstream_ringbuffer_pageoffset = |
| 711 | hv_ring_gpadl_send_hvpgoffset(send_pages << PAGE_SHIFT); |
Boqun Feng | edd9bbc | 2020-09-16 11:48:08 +0800 | [diff] [blame] | 712 | open_msg->target_vp = hv_cpu_number_to_vp_number(newchannel->target_cpu); |
| 713 | |
| 714 | if (userdatalen) |
| 715 | memcpy(open_msg->userdata, userdata, userdatalen); |
| 716 | |
| 717 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
| 718 | list_add_tail(&open_info->msglistentry, |
| 719 | &vmbus_connection.chn_msg_list); |
| 720 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
| 721 | |
| 722 | if (newchannel->rescind) { |
| 723 | err = -ENODEV; |
Dan Carpenter | 3e9bf43 | 2021-04-13 13:50:04 +0300 | [diff] [blame] | 724 | goto error_clean_msglist; |
Boqun Feng | edd9bbc | 2020-09-16 11:48:08 +0800 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | err = vmbus_post_msg(open_msg, |
| 728 | sizeof(struct vmbus_channel_open_channel), true); |
| 729 | |
| 730 | trace_vmbus_open(open_msg, err); |
| 731 | |
| 732 | if (err != 0) |
| 733 | goto error_clean_msglist; |
| 734 | |
| 735 | wait_for_completion(&open_info->waitevent); |
| 736 | |
| 737 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
| 738 | list_del(&open_info->msglistentry); |
| 739 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
| 740 | |
| 741 | if (newchannel->rescind) { |
| 742 | err = -ENODEV; |
| 743 | goto error_free_info; |
| 744 | } |
| 745 | |
| 746 | if (open_info->response.open_result.status) { |
| 747 | err = -EAGAIN; |
| 748 | goto error_free_info; |
| 749 | } |
| 750 | |
| 751 | newchannel->state = CHANNEL_OPENED_STATE; |
| 752 | kfree(open_info); |
| 753 | return 0; |
| 754 | |
| 755 | error_clean_msglist: |
| 756 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
| 757 | list_del(&open_info->msglistentry); |
| 758 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
| 759 | error_free_info: |
| 760 | kfree(open_info); |
| 761 | error_free_gpadl: |
| 762 | vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle); |
| 763 | newchannel->ringbuffer_gpadlhandle = 0; |
| 764 | error_clean_ring: |
| 765 | hv_ringbuffer_cleanup(&newchannel->outbound); |
| 766 | hv_ringbuffer_cleanup(&newchannel->inbound); |
Andres Beltran | e8b7db3 | 2020-11-09 11:04:00 +0100 | [diff] [blame] | 767 | vmbus_free_requestor(&newchannel->requestor); |
Boqun Feng | edd9bbc | 2020-09-16 11:48:08 +0800 | [diff] [blame] | 768 | newchannel->state = CHANNEL_OPEN_STATE; |
| 769 | return err; |
| 770 | } |
| 771 | |
| 772 | /* |
| 773 | * vmbus_connect_ring - Open the channel but reuse ring buffer |
| 774 | */ |
| 775 | int vmbus_connect_ring(struct vmbus_channel *newchannel, |
| 776 | void (*onchannelcallback)(void *context), void *context) |
| 777 | { |
| 778 | return __vmbus_open(newchannel, NULL, 0, onchannelcallback, context); |
| 779 | } |
| 780 | EXPORT_SYMBOL_GPL(vmbus_connect_ring); |
| 781 | |
| 782 | /* |
| 783 | * vmbus_open - Open the specified channel. |
| 784 | */ |
| 785 | int vmbus_open(struct vmbus_channel *newchannel, |
| 786 | u32 send_ringbuffer_size, u32 recv_ringbuffer_size, |
| 787 | void *userdata, u32 userdatalen, |
| 788 | void (*onchannelcallback)(void *context), void *context) |
| 789 | { |
| 790 | int err; |
| 791 | |
| 792 | err = vmbus_alloc_ring(newchannel, send_ringbuffer_size, |
| 793 | recv_ringbuffer_size); |
| 794 | if (err) |
| 795 | return err; |
| 796 | |
| 797 | err = __vmbus_open(newchannel, userdata, userdatalen, |
| 798 | onchannelcallback, context); |
| 799 | if (err) |
| 800 | vmbus_free_ring(newchannel); |
| 801 | |
| 802 | return err; |
| 803 | } |
| 804 | EXPORT_SYMBOL_GPL(vmbus_open); |
| 805 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 806 | /* |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 807 | * vmbus_teardown_gpadl -Teardown the specified GPADL handle |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 808 | */ |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 809 | int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 810 | { |
Greg Kroah-Hartman | 8225021 | 2009-08-26 15:16:04 -0700 | [diff] [blame] | 811 | struct vmbus_channel_gpadl_teardown *msg; |
Greg Kroah-Hartman | aded716 | 2009-08-18 15:21:19 -0700 | [diff] [blame] | 812 | struct vmbus_channel_msginfo *info; |
Greg Kroah-Hartman | dd0813b | 2009-07-15 14:56:45 -0700 | [diff] [blame] | 813 | unsigned long flags; |
K. Y. Srinivasan | 66be653 | 2014-08-27 16:25:32 -0700 | [diff] [blame] | 814 | int ret; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 815 | |
Andrea Parri (Microsoft) | e99c4af | 2020-12-09 08:08:22 +0100 | [diff] [blame] | 816 | info = kzalloc(sizeof(*info) + |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 817 | sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL); |
Bill Pemberton | c3bf2e2 | 2010-05-05 15:27:34 -0400 | [diff] [blame] | 818 | if (!info) |
| 819 | return -ENOMEM; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 820 | |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 821 | init_completion(&info->waitevent); |
K. Y. Srinivasan | ccb61f8 | 2016-12-22 16:54:00 -0800 | [diff] [blame] | 822 | info->waiting_channel = channel; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 823 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 824 | msg = (struct vmbus_channel_gpadl_teardown *)info->msg; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 825 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 826 | msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN; |
| 827 | msg->child_relid = channel->offermsg.child_relid; |
| 828 | msg->gpadl = gpadl_handle; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 829 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 830 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 831 | list_add_tail(&info->msglistentry, |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 832 | &vmbus_connection.chn_msg_list); |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 833 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
K. Y. Srinivasan | 6f3d791 | 2017-08-11 10:03:59 -0700 | [diff] [blame] | 834 | |
| 835 | if (channel->rescind) |
| 836 | goto post_msg_err; |
| 837 | |
Vitaly Kuznetsov | c0bb039 | 2016-12-07 01:16:24 -0800 | [diff] [blame] | 838 | ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_gpadl_teardown), |
| 839 | true); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 840 | |
Vitaly Kuznetsov | 09cdf8f | 2017-10-29 12:21:12 -0700 | [diff] [blame] | 841 | trace_vmbus_teardown_gpadl(msg, ret); |
| 842 | |
K. Y. Srinivasan | 66be653 | 2014-08-27 16:25:32 -0700 | [diff] [blame] | 843 | if (ret) |
| 844 | goto post_msg_err; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 845 | |
K. Y. Srinivasan | 66be653 | 2014-08-27 16:25:32 -0700 | [diff] [blame] | 846 | wait_for_completion(&info->waitevent); |
| 847 | |
| 848 | post_msg_err: |
K. Y. Srinivasan | 5e030d5 | 2017-03-12 20:00:30 -0700 | [diff] [blame] | 849 | /* |
| 850 | * If the channel has been rescinded; |
| 851 | * we will be awakened by the rescind |
| 852 | * handler; set the error code to zero so we don't leak memory. |
| 853 | */ |
| 854 | if (channel->rescind) |
| 855 | ret = 0; |
| 856 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 857 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 858 | list_del(&info->msglistentry); |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 859 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 860 | |
Greg Kroah-Hartman | 8c69f52 | 2009-07-15 12:48:29 -0700 | [diff] [blame] | 861 | kfree(info); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 862 | return ret; |
| 863 | } |
Greg Kroah-Hartman | 18726d7 | 2010-10-21 08:39:59 -0700 | [diff] [blame] | 864 | EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 865 | |
Dexuan Cui | d3b26dd | 2018-08-02 03:08:23 +0000 | [diff] [blame] | 866 | void vmbus_reset_channel_cb(struct vmbus_channel *channel) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 867 | { |
Andrea Parri (Microsoft) | 9403b66 | 2020-04-06 02:15:09 +0200 | [diff] [blame] | 868 | unsigned long flags; |
| 869 | |
Dexuan Cui | 63d55b2 | 2015-12-14 16:01:47 -0800 | [diff] [blame] | 870 | /* |
Dexuan Cui | dad72a1 | 2017-03-04 18:13:58 -0700 | [diff] [blame] | 871 | * vmbus_on_event(), running in the per-channel tasklet, can race |
Dexuan Cui | 63d55b2 | 2015-12-14 16:01:47 -0800 | [diff] [blame] | 872 | * with vmbus_close_internal() in the case of SMP guest, e.g., when |
| 873 | * the former is accessing channel->inbound.ring_buffer, the latter |
Dexuan Cui | dad72a1 | 2017-03-04 18:13:58 -0700 | [diff] [blame] | 874 | * could be freeing the ring_buffer pages, so here we must stop it |
| 875 | * first. |
Andrea Parri (Microsoft) | ac50476 | 2020-04-06 02:15:07 +0200 | [diff] [blame] | 876 | * |
| 877 | * vmbus_chan_sched() might call the netvsc driver callback function |
| 878 | * that ends up scheduling NAPI work that accesses the ring buffer. |
| 879 | * At this point, we have to ensure that any such work is completed |
| 880 | * and that the channel ring buffer is no longer being accessed, cf. |
| 881 | * the calls to napi_disable() in netvsc_device_remove(). |
Dexuan Cui | 63d55b2 | 2015-12-14 16:01:47 -0800 | [diff] [blame] | 882 | */ |
Dexuan Cui | dad72a1 | 2017-03-04 18:13:58 -0700 | [diff] [blame] | 883 | tasklet_disable(&channel->callback_event); |
Dexuan Cui | 63d55b2 | 2015-12-14 16:01:47 -0800 | [diff] [blame] | 884 | |
Andrea Parri (Microsoft) | 9403b66 | 2020-04-06 02:15:09 +0200 | [diff] [blame] | 885 | /* See the inline comments in vmbus_chan_sched(). */ |
| 886 | spin_lock_irqsave(&channel->sched_lock, flags); |
| 887 | channel->onchannel_callback = NULL; |
| 888 | spin_unlock_irqrestore(&channel->sched_lock, flags); |
Dexuan Cui | d3b26dd | 2018-08-02 03:08:23 +0000 | [diff] [blame] | 889 | |
Andrea Parri (Microsoft) | 9403b66 | 2020-04-06 02:15:09 +0200 | [diff] [blame] | 890 | channel->sc_creation_callback = NULL; |
Dexuan Cui | d3b26dd | 2018-08-02 03:08:23 +0000 | [diff] [blame] | 891 | |
| 892 | /* Re-enable tasklet for use on re-open */ |
| 893 | tasklet_enable(&channel->callback_event); |
| 894 | } |
| 895 | |
| 896 | static int vmbus_close_internal(struct vmbus_channel *channel) |
| 897 | { |
| 898 | struct vmbus_channel_close_channel *msg; |
| 899 | int ret; |
| 900 | |
| 901 | vmbus_reset_channel_cb(channel); |
| 902 | |
Dexuan Cui | 64b7faf | 2015-12-14 16:01:48 -0800 | [diff] [blame] | 903 | /* |
| 904 | * In case a device driver's probe() fails (e.g., |
| 905 | * util_probe() -> vmbus_open() returns -ENOMEM) and the device is |
Masahiro Yamada | 8a1115f | 2017-03-09 16:16:31 -0800 | [diff] [blame] | 906 | * rescinded later (e.g., we dynamically disable an Integrated Service |
Dexuan Cui | 64b7faf | 2015-12-14 16:01:48 -0800 | [diff] [blame] | 907 | * in Hyper-V Manager), the driver's remove() invokes vmbus_close(): |
| 908 | * here we should skip most of the below cleanup work. |
| 909 | */ |
Stephen Hemminger | ae6935e | 2018-09-14 09:10:17 -0700 | [diff] [blame] | 910 | if (channel->state != CHANNEL_OPENED_STATE) |
| 911 | return -EINVAL; |
Dexuan Cui | 64b7faf | 2015-12-14 16:01:48 -0800 | [diff] [blame] | 912 | |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 913 | channel->state = CHANNEL_OPEN_STATE; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 914 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 915 | /* Send a closing message */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 916 | |
K. Y. Srinivasan | e9a27a9 | 2011-06-06 15:49:59 -0700 | [diff] [blame] | 917 | msg = &channel->close_msg.msg; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 918 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 919 | msg->header.msgtype = CHANNELMSG_CLOSECHANNEL; |
| 920 | msg->child_relid = channel->offermsg.child_relid; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 921 | |
Vitaly Kuznetsov | c0bb039 | 2016-12-07 01:16:24 -0800 | [diff] [blame] | 922 | ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel), |
| 923 | true); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 924 | |
Vitaly Kuznetsov | 633b005 | 2017-10-29 12:21:10 -0700 | [diff] [blame] | 925 | trace_vmbus_close_internal(msg, ret); |
| 926 | |
K. Y. Srinivasan | 98d731b | 2014-08-27 16:25:33 -0700 | [diff] [blame] | 927 | if (ret) { |
| 928 | pr_err("Close failed: close post msg return is %d\n", ret); |
| 929 | /* |
| 930 | * If we failed to post the close msg, |
| 931 | * it is perhaps better to leak memory. |
| 932 | */ |
K. Y. Srinivasan | 98d731b | 2014-08-27 16:25:33 -0700 | [diff] [blame] | 933 | } |
| 934 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 935 | /* Tear down the gpadl for the channel's ring buffer */ |
Stephen Hemminger | ae6935e | 2018-09-14 09:10:17 -0700 | [diff] [blame] | 936 | else if (channel->ringbuffer_gpadlhandle) { |
K. Y. Srinivasan | 98d731b | 2014-08-27 16:25:33 -0700 | [diff] [blame] | 937 | ret = vmbus_teardown_gpadl(channel, |
| 938 | channel->ringbuffer_gpadlhandle); |
| 939 | if (ret) { |
| 940 | pr_err("Close failed: teardown gpadl return %d\n", ret); |
| 941 | /* |
| 942 | * If we failed to teardown gpadl, |
| 943 | * it is perhaps better to leak memory. |
| 944 | */ |
K. Y. Srinivasan | 98d731b | 2014-08-27 16:25:33 -0700 | [diff] [blame] | 945 | } |
Stephen Hemminger | ae6935e | 2018-09-14 09:10:17 -0700 | [diff] [blame] | 946 | |
| 947 | channel->ringbuffer_gpadlhandle = 0; |
K. Y. Srinivasan | 98d731b | 2014-08-27 16:25:33 -0700 | [diff] [blame] | 948 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 949 | |
Andres Beltran | e8b7db3 | 2020-11-09 11:04:00 +0100 | [diff] [blame] | 950 | if (!ret) |
| 951 | vmbus_free_requestor(&channel->requestor); |
| 952 | |
K. Y. Srinivasan | 98d731b | 2014-08-27 16:25:33 -0700 | [diff] [blame] | 953 | return ret; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 954 | } |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 955 | |
Stephen Hemminger | ae6935e | 2018-09-14 09:10:17 -0700 | [diff] [blame] | 956 | /* disconnect ring - close all channels */ |
| 957 | int vmbus_disconnect_ring(struct vmbus_channel *channel) |
| 958 | { |
| 959 | struct vmbus_channel *cur_channel, *tmp; |
Stephen Hemminger | ae6935e | 2018-09-14 09:10:17 -0700 | [diff] [blame] | 960 | int ret; |
| 961 | |
| 962 | if (channel->primary_channel != NULL) |
| 963 | return -EINVAL; |
| 964 | |
Dexuan Cui | b5679ce | 2019-01-09 20:56:06 +0000 | [diff] [blame] | 965 | list_for_each_entry_safe(cur_channel, tmp, &channel->sc_list, sc_list) { |
Stephen Hemminger | ae6935e | 2018-09-14 09:10:17 -0700 | [diff] [blame] | 966 | if (cur_channel->rescind) |
| 967 | wait_for_completion(&cur_channel->rescind_event); |
| 968 | |
| 969 | mutex_lock(&vmbus_connection.channel_mutex); |
| 970 | if (vmbus_close_internal(cur_channel) == 0) { |
| 971 | vmbus_free_ring(cur_channel); |
| 972 | |
| 973 | if (cur_channel->rescind) |
| 974 | hv_process_channel_removal(cur_channel); |
| 975 | } |
| 976 | mutex_unlock(&vmbus_connection.channel_mutex); |
| 977 | } |
| 978 | |
| 979 | /* |
| 980 | * Now close the primary. |
| 981 | */ |
| 982 | mutex_lock(&vmbus_connection.channel_mutex); |
| 983 | ret = vmbus_close_internal(channel); |
| 984 | mutex_unlock(&vmbus_connection.channel_mutex); |
| 985 | |
| 986 | return ret; |
| 987 | } |
| 988 | EXPORT_SYMBOL_GPL(vmbus_disconnect_ring); |
| 989 | |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 990 | /* |
| 991 | * vmbus_close - Close the specified channel |
| 992 | */ |
| 993 | void vmbus_close(struct vmbus_channel *channel) |
| 994 | { |
Stephen Hemminger | ae6935e | 2018-09-14 09:10:17 -0700 | [diff] [blame] | 995 | if (vmbus_disconnect_ring(channel) == 0) |
| 996 | vmbus_free_ring(channel); |
K. Y. Srinivasan | e68d297 | 2013-05-23 12:02:32 -0700 | [diff] [blame] | 997 | } |
Greg Kroah-Hartman | 70bfa30 | 2010-10-21 09:52:22 -0700 | [diff] [blame] | 998 | EXPORT_SYMBOL_GPL(vmbus_close); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 999 | |
stephen hemminger | 5dd0fb9 | 2017-08-16 08:56:26 -0700 | [diff] [blame] | 1000 | /** |
| 1001 | * vmbus_sendpacket() - Send the specified buffer on the given channel |
Dexuan Cui | fe857bb | 2018-09-23 21:10:41 +0000 | [diff] [blame] | 1002 | * @channel: Pointer to vmbus_channel structure |
| 1003 | * @buffer: Pointer to the buffer you want to send the data from. |
| 1004 | * @bufferlen: Maximum size of what the buffer holds. |
stephen hemminger | 5dd0fb9 | 2017-08-16 08:56:26 -0700 | [diff] [blame] | 1005 | * @requestid: Identifier of the request |
Dexuan Cui | fe857bb | 2018-09-23 21:10:41 +0000 | [diff] [blame] | 1006 | * @type: Type of packet that is being sent e.g. negotiate, time |
| 1007 | * packet etc. |
| 1008 | * @flags: 0 or VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED |
stephen hemminger | 5dd0fb9 | 2017-08-16 08:56:26 -0700 | [diff] [blame] | 1009 | * |
Dexuan Cui | fe857bb | 2018-09-23 21:10:41 +0000 | [diff] [blame] | 1010 | * Sends data in @buffer directly to Hyper-V via the vmbus. |
| 1011 | * This will send the data unparsed to Hyper-V. |
stephen hemminger | 5dd0fb9 | 2017-08-16 08:56:26 -0700 | [diff] [blame] | 1012 | * |
| 1013 | * Mainly used by Hyper-V drivers. |
| 1014 | */ |
| 1015 | int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer, |
| 1016 | u32 bufferlen, u64 requestid, |
| 1017 | enum vmbus_packet_type type, u32 flags) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1018 | { |
Greg Kroah-Hartman | 8dc0a06 | 2009-08-27 16:02:36 -0700 | [diff] [blame] | 1019 | struct vmpacket_descriptor desc; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 1020 | u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen; |
Uwe Kleine-König | 7350968 | 2011-01-20 09:32:01 +0100 | [diff] [blame] | 1021 | u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64)); |
K. Y. Srinivasan | 011a7c3 | 2014-02-01 19:02:20 -0800 | [diff] [blame] | 1022 | struct kvec bufferlist[3]; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 1023 | u64 aligned_data = 0; |
K. Y. Srinivasan | b81658c | 2015-08-01 16:08:14 -0700 | [diff] [blame] | 1024 | int num_vecs = ((bufferlen != 0) ? 3 : 1); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1025 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1026 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 1027 | /* Setup the descriptor */ |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 1028 | desc.type = type; /* VmbusPacketTypeDataInBand; */ |
| 1029 | desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */ |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 1030 | /* in 8-bytes granularity */ |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 1031 | desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3; |
| 1032 | desc.len8 = (u16)(packetlen_aligned >> 3); |
Andres Beltran | e8b7db3 | 2020-11-09 11:04:00 +0100 | [diff] [blame] | 1033 | desc.trans_id = VMBUS_RQST_ERROR; /* will be updated in hv_ringbuffer_write() */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1034 | |
K. Y. Srinivasan | 011a7c3 | 2014-02-01 19:02:20 -0800 | [diff] [blame] | 1035 | bufferlist[0].iov_base = &desc; |
| 1036 | bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor); |
| 1037 | bufferlist[1].iov_base = buffer; |
| 1038 | bufferlist[1].iov_len = bufferlen; |
| 1039 | bufferlist[2].iov_base = &aligned_data; |
| 1040 | bufferlist[2].iov_len = (packetlen_aligned - packetlen); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1041 | |
Andres Beltran | e8b7db3 | 2020-11-09 11:04:00 +0100 | [diff] [blame] | 1042 | return hv_ringbuffer_write(channel, bufferlist, num_vecs, requestid); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1043 | } |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 1044 | EXPORT_SYMBOL(vmbus_sendpacket); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1045 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 1046 | /* |
stephen hemminger | 5a668d8 | 2017-08-16 08:56:25 -0700 | [diff] [blame] | 1047 | * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer |
K. Y. Srinivasan | 87e93d6 | 2015-02-28 11:39:03 -0800 | [diff] [blame] | 1048 | * packets using a GPADL Direct packet type. This interface allows you |
| 1049 | * to control notifying the host. This will be useful for sending |
| 1050 | * batched data. Also the sender can control the send flags |
| 1051 | * explicitly. |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 1052 | */ |
stephen hemminger | 5a668d8 | 2017-08-16 08:56:25 -0700 | [diff] [blame] | 1053 | int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel, |
| 1054 | struct hv_page_buffer pagebuffers[], |
| 1055 | u32 pagecount, void *buffer, u32 bufferlen, |
| 1056 | u64 requestid) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1057 | { |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 1058 | int i; |
Haiyang Zhang | 430a8e9 | 2010-09-20 21:07:51 +0000 | [diff] [blame] | 1059 | struct vmbus_channel_packet_page_buffer desc; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 1060 | u32 descsize; |
| 1061 | u32 packetlen; |
| 1062 | u32 packetlen_aligned; |
K. Y. Srinivasan | 011a7c3 | 2014-02-01 19:02:20 -0800 | [diff] [blame] | 1063 | struct kvec bufferlist[3]; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 1064 | u64 aligned_data = 0; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1065 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 1066 | if (pagecount > MAX_PAGE_BUFFER_COUNT) |
Bill Pemberton | 002b53e | 2010-05-05 15:27:39 -0400 | [diff] [blame] | 1067 | return -EINVAL; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1068 | |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 1069 | /* |
Haiyang Zhang | 430a8e9 | 2010-09-20 21:07:51 +0000 | [diff] [blame] | 1070 | * 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] | 1071 | * largest size we support |
| 1072 | */ |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 1073 | descsize = sizeof(struct vmbus_channel_packet_page_buffer) - |
| 1074 | ((MAX_PAGE_BUFFER_COUNT - pagecount) * |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 1075 | sizeof(struct hv_page_buffer)); |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 1076 | packetlen = descsize + bufferlen; |
Uwe Kleine-König | 7350968 | 2011-01-20 09:32:01 +0100 | [diff] [blame] | 1077 | packetlen_aligned = ALIGN(packetlen, sizeof(u64)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1078 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 1079 | /* Setup the descriptor */ |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 1080 | desc.type = VM_PKT_DATA_USING_GPA_DIRECT; |
stephen hemminger | 5a668d8 | 2017-08-16 08:56:25 -0700 | [diff] [blame] | 1081 | desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; |
Stephen Hemminger | bdc1dd4 | 2017-03-04 18:27:14 -0700 | [diff] [blame] | 1082 | desc.dataoffset8 = descsize >> 3; /* in 8-bytes granularity */ |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 1083 | desc.length8 = (u16)(packetlen_aligned >> 3); |
Andres Beltran | e8b7db3 | 2020-11-09 11:04:00 +0100 | [diff] [blame] | 1084 | desc.transactionid = VMBUS_RQST_ERROR; /* will be updated in hv_ringbuffer_write() */ |
Stephen Hemminger | 33d426a | 2017-10-05 17:35:05 -0700 | [diff] [blame] | 1085 | desc.reserved = 0; |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 1086 | desc.rangecount = pagecount; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1087 | |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 1088 | for (i = 0; i < pagecount; i++) { |
Haiyang Zhang | ca623ad | 2011-01-26 12:12:11 -0800 | [diff] [blame] | 1089 | desc.range[i].len = pagebuffers[i].len; |
| 1090 | desc.range[i].offset = pagebuffers[i].offset; |
| 1091 | desc.range[i].pfn = pagebuffers[i].pfn; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
K. Y. Srinivasan | 011a7c3 | 2014-02-01 19:02:20 -0800 | [diff] [blame] | 1094 | bufferlist[0].iov_base = &desc; |
| 1095 | bufferlist[0].iov_len = descsize; |
| 1096 | bufferlist[1].iov_base = buffer; |
| 1097 | bufferlist[1].iov_len = bufferlen; |
| 1098 | bufferlist[2].iov_base = &aligned_data; |
| 1099 | bufferlist[2].iov_len = (packetlen_aligned - packetlen); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1100 | |
Andres Beltran | e8b7db3 | 2020-11-09 11:04:00 +0100 | [diff] [blame] | 1101 | return hv_ringbuffer_write(channel, bufferlist, 3, requestid); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1102 | } |
Greg Kroah-Hartman | 713efeb | 2010-10-21 09:29:54 -0700 | [diff] [blame] | 1103 | EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1104 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 1105 | /* |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 1106 | * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 1107 | * using a GPADL Direct packet type. |
K. Y. Srinivasan | d61031e | 2015-01-09 23:54:34 -0800 | [diff] [blame] | 1108 | * The buffer includes the vmbus descriptor. |
| 1109 | */ |
| 1110 | int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel, |
| 1111 | struct vmbus_packet_mpb_array *desc, |
| 1112 | u32 desc_size, |
| 1113 | void *buffer, u32 bufferlen, u64 requestid) |
| 1114 | { |
K. Y. Srinivasan | d61031e | 2015-01-09 23:54:34 -0800 | [diff] [blame] | 1115 | u32 packetlen; |
| 1116 | u32 packetlen_aligned; |
| 1117 | struct kvec bufferlist[3]; |
| 1118 | u64 aligned_data = 0; |
K. Y. Srinivasan | d61031e | 2015-01-09 23:54:34 -0800 | [diff] [blame] | 1119 | |
| 1120 | packetlen = desc_size + bufferlen; |
| 1121 | packetlen_aligned = ALIGN(packetlen, sizeof(u64)); |
| 1122 | |
| 1123 | /* Setup the descriptor */ |
| 1124 | desc->type = VM_PKT_DATA_USING_GPA_DIRECT; |
| 1125 | desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; |
Stephen Hemminger | bdc1dd4 | 2017-03-04 18:27:14 -0700 | [diff] [blame] | 1126 | desc->dataoffset8 = desc_size >> 3; /* in 8-bytes granularity */ |
K. Y. Srinivasan | d61031e | 2015-01-09 23:54:34 -0800 | [diff] [blame] | 1127 | desc->length8 = (u16)(packetlen_aligned >> 3); |
Andres Beltran | e8b7db3 | 2020-11-09 11:04:00 +0100 | [diff] [blame] | 1128 | desc->transactionid = VMBUS_RQST_ERROR; /* will be updated in hv_ringbuffer_write() */ |
Stephen Hemminger | 33d426a | 2017-10-05 17:35:05 -0700 | [diff] [blame] | 1129 | desc->reserved = 0; |
K. Y. Srinivasan | d61031e | 2015-01-09 23:54:34 -0800 | [diff] [blame] | 1130 | desc->rangecount = 1; |
| 1131 | |
| 1132 | bufferlist[0].iov_base = desc; |
| 1133 | bufferlist[0].iov_len = desc_size; |
| 1134 | bufferlist[1].iov_base = buffer; |
| 1135 | bufferlist[1].iov_len = bufferlen; |
| 1136 | bufferlist[2].iov_base = &aligned_data; |
| 1137 | bufferlist[2].iov_len = (packetlen_aligned - packetlen); |
| 1138 | |
Andres Beltran | e8b7db3 | 2020-11-09 11:04:00 +0100 | [diff] [blame] | 1139 | return hv_ringbuffer_write(channel, bufferlist, 3, requestid); |
K. Y. Srinivasan | d61031e | 2015-01-09 23:54:34 -0800 | [diff] [blame] | 1140 | } |
| 1141 | EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc); |
| 1142 | |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 1143 | /** |
Dexuan Cui | fe857bb | 2018-09-23 21:10:41 +0000 | [diff] [blame] | 1144 | * __vmbus_recvpacket() - Retrieve the user packet on the specified channel |
| 1145 | * @channel: Pointer to vmbus_channel structure |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 1146 | * @buffer: Pointer to the buffer you want to receive the data into. |
Dexuan Cui | fe857bb | 2018-09-23 21:10:41 +0000 | [diff] [blame] | 1147 | * @bufferlen: Maximum size of what the buffer can hold. |
| 1148 | * @buffer_actual_len: The actual size of the data after it was received. |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 1149 | * @requestid: Identifier of the request |
Dexuan Cui | fe857bb | 2018-09-23 21:10:41 +0000 | [diff] [blame] | 1150 | * @raw: true means keep the vmpacket_descriptor header in the received data. |
Hank Janssen | c88c4e4 | 2010-05-04 15:55:05 -0700 | [diff] [blame] | 1151 | * |
| 1152 | * Receives directly from the hyper-v vmbus and puts the data it received |
| 1153 | * into Buffer. This will receive the data unparsed from hyper-v. |
| 1154 | * |
| 1155 | * Mainly used by Hyper-V drivers. |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 1156 | */ |
Vitaly Kuznetsov | 667d374 | 2015-12-14 19:02:00 -0800 | [diff] [blame] | 1157 | static inline int |
| 1158 | __vmbus_recvpacket(struct vmbus_channel *channel, void *buffer, |
| 1159 | u32 bufferlen, u32 *buffer_actual_len, u64 *requestid, |
| 1160 | bool raw) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1161 | { |
K. Y. Srinivasan | 3372592 | 2016-11-06 13:14:18 -0800 | [diff] [blame] | 1162 | return hv_ringbuffer_read(channel, buffer, bufferlen, |
| 1163 | buffer_actual_len, requestid, raw); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1164 | |
Vitaly Kuznetsov | 667d374 | 2015-12-14 19:02:00 -0800 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer, |
| 1168 | u32 bufferlen, u32 *buffer_actual_len, |
| 1169 | u64 *requestid) |
| 1170 | { |
| 1171 | return __vmbus_recvpacket(channel, buffer, bufferlen, |
| 1172 | buffer_actual_len, requestid, false); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1173 | } |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 1174 | EXPORT_SYMBOL(vmbus_recvpacket); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1175 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 1176 | /* |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 1177 | * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel |
Greg Kroah-Hartman | f4266e3 | 2009-09-01 17:24:57 -0700 | [diff] [blame] | 1178 | */ |
Haiyang Zhang | fff41b2 | 2010-10-07 11:40:08 -0700 | [diff] [blame] | 1179 | int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer, |
Haiyang Zhang | 39d70a4 | 2010-09-30 10:52:13 -0700 | [diff] [blame] | 1180 | u32 bufferlen, u32 *buffer_actual_len, |
| 1181 | u64 *requestid) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1182 | { |
Vitaly Kuznetsov | 667d374 | 2015-12-14 19:02:00 -0800 | [diff] [blame] | 1183 | return __vmbus_recvpacket(channel, buffer, bufferlen, |
| 1184 | buffer_actual_len, requestid, true); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1185 | } |
Greg Kroah-Hartman | adaee6b | 2010-10-21 09:09:23 -0700 | [diff] [blame] | 1186 | EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw); |
Andres Beltran | e8b7db3 | 2020-11-09 11:04:00 +0100 | [diff] [blame] | 1187 | |
| 1188 | /* |
| 1189 | * vmbus_next_request_id - Returns a new request id. It is also |
| 1190 | * the index at which the guest memory address is stored. |
| 1191 | * Uses a spin lock to avoid race conditions. |
| 1192 | * @rqstor: Pointer to the requestor struct |
| 1193 | * @rqst_add: Guest memory address to be stored in the array |
| 1194 | */ |
| 1195 | u64 vmbus_next_request_id(struct vmbus_requestor *rqstor, u64 rqst_addr) |
| 1196 | { |
| 1197 | unsigned long flags; |
| 1198 | u64 current_id; |
| 1199 | const struct vmbus_channel *channel = |
| 1200 | container_of(rqstor, const struct vmbus_channel, requestor); |
| 1201 | |
| 1202 | /* Check rqstor has been initialized */ |
| 1203 | if (!channel->rqstor_size) |
| 1204 | return VMBUS_NO_RQSTOR; |
| 1205 | |
| 1206 | spin_lock_irqsave(&rqstor->req_lock, flags); |
| 1207 | current_id = rqstor->next_request_id; |
| 1208 | |
| 1209 | /* Requestor array is full */ |
| 1210 | if (current_id >= rqstor->size) { |
| 1211 | spin_unlock_irqrestore(&rqstor->req_lock, flags); |
| 1212 | return VMBUS_RQST_ERROR; |
| 1213 | } |
| 1214 | |
| 1215 | rqstor->next_request_id = rqstor->req_arr[current_id]; |
| 1216 | rqstor->req_arr[current_id] = rqst_addr; |
| 1217 | |
| 1218 | /* The already held spin lock provides atomicity */ |
| 1219 | bitmap_set(rqstor->req_bitmap, current_id, 1); |
| 1220 | |
| 1221 | spin_unlock_irqrestore(&rqstor->req_lock, flags); |
| 1222 | |
| 1223 | /* |
| 1224 | * Cannot return an ID of 0, which is reserved for an unsolicited |
| 1225 | * message from Hyper-V. |
| 1226 | */ |
| 1227 | return current_id + 1; |
| 1228 | } |
| 1229 | EXPORT_SYMBOL_GPL(vmbus_next_request_id); |
| 1230 | |
| 1231 | /* |
| 1232 | * vmbus_request_addr - Returns the memory address stored at @trans_id |
| 1233 | * in @rqstor. Uses a spin lock to avoid race conditions. |
| 1234 | * @rqstor: Pointer to the requestor struct |
| 1235 | * @trans_id: Request id sent back from Hyper-V. Becomes the requestor's |
| 1236 | * next request id. |
| 1237 | */ |
| 1238 | u64 vmbus_request_addr(struct vmbus_requestor *rqstor, u64 trans_id) |
| 1239 | { |
| 1240 | unsigned long flags; |
| 1241 | u64 req_addr; |
| 1242 | const struct vmbus_channel *channel = |
| 1243 | container_of(rqstor, const struct vmbus_channel, requestor); |
| 1244 | |
| 1245 | /* Check rqstor has been initialized */ |
| 1246 | if (!channel->rqstor_size) |
| 1247 | return VMBUS_NO_RQSTOR; |
| 1248 | |
| 1249 | /* Hyper-V can send an unsolicited message with ID of 0 */ |
| 1250 | if (!trans_id) |
| 1251 | return trans_id; |
| 1252 | |
| 1253 | spin_lock_irqsave(&rqstor->req_lock, flags); |
| 1254 | |
| 1255 | /* Data corresponding to trans_id is stored at trans_id - 1 */ |
| 1256 | trans_id--; |
| 1257 | |
| 1258 | /* Invalid trans_id */ |
| 1259 | if (trans_id >= rqstor->size || !test_bit(trans_id, rqstor->req_bitmap)) { |
| 1260 | spin_unlock_irqrestore(&rqstor->req_lock, flags); |
| 1261 | return VMBUS_RQST_ERROR; |
| 1262 | } |
| 1263 | |
| 1264 | req_addr = rqstor->req_arr[trans_id]; |
| 1265 | rqstor->req_arr[trans_id] = rqstor->next_request_id; |
| 1266 | rqstor->next_request_id = trans_id; |
| 1267 | |
| 1268 | /* The already held spin lock provides atomicity */ |
| 1269 | bitmap_clear(rqstor->req_bitmap, trans_id, 1); |
| 1270 | |
| 1271 | spin_unlock_irqrestore(&rqstor->req_lock, flags); |
| 1272 | return req_addr; |
| 1273 | } |
| 1274 | EXPORT_SYMBOL_GPL(vmbus_request_addr); |