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