rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2009, Microsoft Corporation. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms and conditions of the GNU General Public License, |
| 6 | * version 2, as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
| 15 | * Place - Suite 330, Boston, MA 02111-1307 USA. |
| 16 | * |
| 17 | * Authors: |
| 18 | * Haiyang Zhang <haiyangz@microsoft.com> |
| 19 | * Hank Janssen <hjanssen@microsoft.com> |
| 20 | */ |
| 21 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/sched.h> |
| 25 | #include <linux/wait.h> |
| 26 | #include <linux/mm.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/hyperv.h> |
| 30 | #include <linux/uio.h> |
| 31 | #include <linux/interrupt.h> |
| 32 | |
| 33 | #include "hyperv_vmbus.h" |
| 34 | |
| 35 | #define NUM_PAGES_SPANNED(addr, len) \ |
| 36 | ((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT)) |
| 37 | |
| 38 | /* |
| 39 | * vmbus_setevent- Trigger an event notification on the specified |
| 40 | * channel. |
| 41 | */ |
| 42 | void vmbus_setevent(struct vmbus_channel *channel) |
| 43 | { |
| 44 | struct hv_monitor_page *monitorpage; |
| 45 | |
| 46 | /* |
| 47 | * For channels marked as in "low latency" mode |
| 48 | * bypass the monitor page mechanism. |
| 49 | */ |
| 50 | if (channel->offermsg.monitor_allocated && !channel->low_latency) { |
| 51 | vmbus_send_interrupt(channel->offermsg.child_relid); |
| 52 | |
| 53 | /* Get the child to parent monitor page */ |
| 54 | monitorpage = vmbus_connection.monitor_pages[1]; |
| 55 | |
| 56 | sync_set_bit(channel->monitor_bit, |
| 57 | (unsigned long *)&monitorpage->trigger_group |
| 58 | [channel->monitor_grp].pending); |
| 59 | |
| 60 | } else { |
| 61 | vmbus_set_event(channel); |
| 62 | } |
| 63 | } |
| 64 | EXPORT_SYMBOL_GPL(vmbus_setevent); |
| 65 | |
| 66 | /* |
| 67 | * vmbus_open - Open the specified channel. |
| 68 | */ |
| 69 | int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size, |
| 70 | u32 recv_ringbuffer_size, void *userdata, u32 userdatalen, |
| 71 | void (*onchannelcallback)(void *context), void *context) |
| 72 | { |
| 73 | struct vmbus_channel_open_channel *open_msg; |
| 74 | struct vmbus_channel_msginfo *open_info = NULL; |
| 75 | unsigned long flags; |
| 76 | int ret, err = 0; |
| 77 | struct page *page; |
| 78 | |
| 79 | if (send_ringbuffer_size % PAGE_SIZE || |
| 80 | recv_ringbuffer_size % PAGE_SIZE) |
| 81 | return -EINVAL; |
| 82 | |
| 83 | spin_lock_irqsave(&newchannel->lock, flags); |
| 84 | if (newchannel->state == CHANNEL_OPEN_STATE) { |
| 85 | newchannel->state = CHANNEL_OPENING_STATE; |
| 86 | } else { |
| 87 | spin_unlock_irqrestore(&newchannel->lock, flags); |
| 88 | return -EINVAL; |
| 89 | } |
| 90 | spin_unlock_irqrestore(&newchannel->lock, flags); |
| 91 | |
| 92 | newchannel->onchannel_callback = onchannelcallback; |
| 93 | newchannel->channel_callback_context = context; |
| 94 | |
| 95 | /* Allocate the ring buffer */ |
| 96 | page = alloc_pages_node(cpu_to_node(newchannel->target_cpu), |
| 97 | GFP_KERNEL|__GFP_ZERO, |
| 98 | get_order(send_ringbuffer_size + |
| 99 | recv_ringbuffer_size)); |
| 100 | |
| 101 | if (!page) |
| 102 | page = alloc_pages(GFP_KERNEL|__GFP_ZERO, |
| 103 | get_order(send_ringbuffer_size + |
| 104 | recv_ringbuffer_size)); |
| 105 | |
| 106 | if (!page) { |
| 107 | err = -ENOMEM; |
| 108 | goto error_set_chnstate; |
| 109 | } |
| 110 | |
| 111 | newchannel->ringbuffer_pages = page_address(page); |
| 112 | newchannel->ringbuffer_pagecount = (send_ringbuffer_size + |
| 113 | recv_ringbuffer_size) >> PAGE_SHIFT; |
| 114 | |
| 115 | ret = hv_ringbuffer_init(&newchannel->outbound, page, |
| 116 | send_ringbuffer_size >> PAGE_SHIFT); |
| 117 | |
| 118 | if (ret != 0) { |
| 119 | err = ret; |
| 120 | goto error_free_pages; |
| 121 | } |
| 122 | |
| 123 | ret = hv_ringbuffer_init(&newchannel->inbound, |
| 124 | &page[send_ringbuffer_size >> PAGE_SHIFT], |
| 125 | recv_ringbuffer_size >> PAGE_SHIFT); |
| 126 | if (ret != 0) { |
| 127 | err = ret; |
| 128 | goto error_free_pages; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | /* Establish the gpadl for the ring buffer */ |
| 133 | newchannel->ringbuffer_gpadlhandle = 0; |
| 134 | |
| 135 | ret = vmbus_establish_gpadl(newchannel, |
| 136 | page_address(page), |
| 137 | send_ringbuffer_size + |
| 138 | recv_ringbuffer_size, |
| 139 | &newchannel->ringbuffer_gpadlhandle); |
| 140 | |
| 141 | if (ret != 0) { |
| 142 | err = ret; |
| 143 | goto error_free_pages; |
| 144 | } |
| 145 | |
| 146 | /* Create and init the channel open message */ |
| 147 | open_info = kmalloc(sizeof(*open_info) + |
| 148 | sizeof(struct vmbus_channel_open_channel), |
| 149 | GFP_KERNEL); |
| 150 | if (!open_info) { |
| 151 | err = -ENOMEM; |
| 152 | goto error_free_gpadl; |
| 153 | } |
| 154 | |
| 155 | init_completion(&open_info->waitevent); |
| 156 | open_info->waiting_channel = newchannel; |
| 157 | |
| 158 | open_msg = (struct vmbus_channel_open_channel *)open_info->msg; |
| 159 | open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL; |
| 160 | open_msg->openid = newchannel->offermsg.child_relid; |
| 161 | open_msg->child_relid = newchannel->offermsg.child_relid; |
| 162 | open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle; |
| 163 | open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >> |
| 164 | PAGE_SHIFT; |
| 165 | open_msg->target_vp = newchannel->target_vp; |
| 166 | |
| 167 | if (userdatalen > MAX_USER_DEFINED_BYTES) { |
| 168 | err = -EINVAL; |
| 169 | goto error_free_gpadl; |
| 170 | } |
| 171 | |
| 172 | if (userdatalen) |
| 173 | memcpy(open_msg->userdata, userdata, userdatalen); |
| 174 | |
| 175 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
| 176 | list_add_tail(&open_info->msglistentry, |
| 177 | &vmbus_connection.chn_msg_list); |
| 178 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
| 179 | |
| 180 | if (newchannel->rescind) { |
| 181 | err = -ENODEV; |
| 182 | goto error_free_gpadl; |
| 183 | } |
| 184 | |
| 185 | ret = vmbus_post_msg(open_msg, |
| 186 | sizeof(struct vmbus_channel_open_channel), true); |
| 187 | |
| 188 | if (ret != 0) { |
| 189 | err = ret; |
| 190 | goto error_clean_msglist; |
| 191 | } |
| 192 | |
| 193 | wait_for_completion(&open_info->waitevent); |
| 194 | |
| 195 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
| 196 | list_del(&open_info->msglistentry); |
| 197 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
| 198 | |
| 199 | if (newchannel->rescind) { |
| 200 | err = -ENODEV; |
| 201 | goto error_free_gpadl; |
| 202 | } |
| 203 | |
| 204 | if (open_info->response.open_result.status) { |
| 205 | err = -EAGAIN; |
| 206 | goto error_free_gpadl; |
| 207 | } |
| 208 | |
| 209 | newchannel->state = CHANNEL_OPENED_STATE; |
| 210 | kfree(open_info); |
| 211 | return 0; |
| 212 | |
| 213 | error_clean_msglist: |
| 214 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
| 215 | list_del(&open_info->msglistentry); |
| 216 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
| 217 | |
| 218 | error_free_gpadl: |
| 219 | vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle); |
| 220 | kfree(open_info); |
| 221 | error_free_pages: |
| 222 | hv_ringbuffer_cleanup(&newchannel->outbound); |
| 223 | hv_ringbuffer_cleanup(&newchannel->inbound); |
| 224 | __free_pages(page, |
| 225 | get_order(send_ringbuffer_size + recv_ringbuffer_size)); |
| 226 | error_set_chnstate: |
| 227 | newchannel->state = CHANNEL_OPEN_STATE; |
| 228 | return err; |
| 229 | } |
| 230 | EXPORT_SYMBOL_GPL(vmbus_open); |
| 231 | |
| 232 | /* Used for Hyper-V Socket: a guest client's connect() to the host */ |
| 233 | int vmbus_send_tl_connect_request(const uuid_le *shv_guest_servie_id, |
| 234 | const uuid_le *shv_host_servie_id) |
| 235 | { |
| 236 | struct vmbus_channel_tl_connect_request conn_msg; |
| 237 | |
| 238 | memset(&conn_msg, 0, sizeof(conn_msg)); |
| 239 | conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST; |
| 240 | conn_msg.guest_endpoint_id = *shv_guest_servie_id; |
| 241 | conn_msg.host_service_id = *shv_host_servie_id; |
| 242 | |
| 243 | return vmbus_post_msg(&conn_msg, sizeof(conn_msg), true); |
| 244 | } |
| 245 | EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request); |
| 246 | |
| 247 | /* |
| 248 | * create_gpadl_header - Creates a gpadl for the specified buffer |
| 249 | */ |
| 250 | static int create_gpadl_header(void *kbuffer, u32 size, |
| 251 | struct vmbus_channel_msginfo **msginfo) |
| 252 | { |
| 253 | int i; |
| 254 | int pagecount; |
| 255 | struct vmbus_channel_gpadl_header *gpadl_header; |
| 256 | struct vmbus_channel_gpadl_body *gpadl_body; |
| 257 | struct vmbus_channel_msginfo *msgheader; |
| 258 | struct vmbus_channel_msginfo *msgbody = NULL; |
| 259 | u32 msgsize; |
| 260 | |
| 261 | int pfnsum, pfncount, pfnleft, pfncurr, pfnsize; |
| 262 | |
| 263 | pagecount = size >> PAGE_SHIFT; |
| 264 | |
| 265 | /* do we need a gpadl body msg */ |
| 266 | pfnsize = MAX_SIZE_CHANNEL_MESSAGE - |
| 267 | sizeof(struct vmbus_channel_gpadl_header) - |
| 268 | sizeof(struct gpa_range); |
| 269 | pfncount = pfnsize / sizeof(u64); |
| 270 | |
| 271 | if (pagecount > pfncount) { |
| 272 | /* we need a gpadl body */ |
| 273 | /* fill in the header */ |
| 274 | msgsize = sizeof(struct vmbus_channel_msginfo) + |
| 275 | sizeof(struct vmbus_channel_gpadl_header) + |
| 276 | sizeof(struct gpa_range) + pfncount * sizeof(u64); |
| 277 | msgheader = kzalloc(msgsize, GFP_KERNEL); |
| 278 | if (!msgheader) |
| 279 | goto nomem; |
| 280 | |
| 281 | INIT_LIST_HEAD(&msgheader->submsglist); |
| 282 | msgheader->msgsize = msgsize; |
| 283 | |
| 284 | gpadl_header = (struct vmbus_channel_gpadl_header *) |
| 285 | msgheader->msg; |
| 286 | gpadl_header->rangecount = 1; |
| 287 | gpadl_header->range_buflen = sizeof(struct gpa_range) + |
| 288 | pagecount * sizeof(u64); |
| 289 | gpadl_header->range[0].byte_offset = 0; |
| 290 | gpadl_header->range[0].byte_count = size; |
| 291 | for (i = 0; i < pfncount; i++) |
| 292 | gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys( |
| 293 | kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT; |
| 294 | *msginfo = msgheader; |
| 295 | |
| 296 | pfnsum = pfncount; |
| 297 | pfnleft = pagecount - pfncount; |
| 298 | |
| 299 | /* how many pfns can we fit */ |
| 300 | pfnsize = MAX_SIZE_CHANNEL_MESSAGE - |
| 301 | sizeof(struct vmbus_channel_gpadl_body); |
| 302 | pfncount = pfnsize / sizeof(u64); |
| 303 | |
| 304 | /* fill in the body */ |
| 305 | while (pfnleft) { |
| 306 | if (pfnleft > pfncount) |
| 307 | pfncurr = pfncount; |
| 308 | else |
| 309 | pfncurr = pfnleft; |
| 310 | |
| 311 | msgsize = sizeof(struct vmbus_channel_msginfo) + |
| 312 | sizeof(struct vmbus_channel_gpadl_body) + |
| 313 | pfncurr * sizeof(u64); |
| 314 | msgbody = kzalloc(msgsize, GFP_KERNEL); |
| 315 | |
| 316 | if (!msgbody) { |
| 317 | struct vmbus_channel_msginfo *pos = NULL; |
| 318 | struct vmbus_channel_msginfo *tmp = NULL; |
| 319 | /* |
| 320 | * Free up all the allocated messages. |
| 321 | */ |
| 322 | list_for_each_entry_safe(pos, tmp, |
| 323 | &msgheader->submsglist, |
| 324 | msglistentry) { |
| 325 | |
| 326 | list_del(&pos->msglistentry); |
| 327 | kfree(pos); |
| 328 | } |
| 329 | |
| 330 | goto nomem; |
| 331 | } |
| 332 | |
| 333 | msgbody->msgsize = msgsize; |
| 334 | gpadl_body = |
| 335 | (struct vmbus_channel_gpadl_body *)msgbody->msg; |
| 336 | |
| 337 | /* |
| 338 | * Gpadl is u32 and we are using a pointer which could |
| 339 | * be 64-bit |
| 340 | * This is governed by the guest/host protocol and |
| 341 | * so the hypervisor guarantees that this is ok. |
| 342 | */ |
| 343 | for (i = 0; i < pfncurr; i++) |
| 344 | gpadl_body->pfn[i] = slow_virt_to_phys( |
| 345 | kbuffer + PAGE_SIZE * (pfnsum + i)) >> |
| 346 | PAGE_SHIFT; |
| 347 | |
| 348 | /* add to msg header */ |
| 349 | list_add_tail(&msgbody->msglistentry, |
| 350 | &msgheader->submsglist); |
| 351 | pfnsum += pfncurr; |
| 352 | pfnleft -= pfncurr; |
| 353 | } |
| 354 | } else { |
| 355 | /* everything fits in a header */ |
| 356 | msgsize = sizeof(struct vmbus_channel_msginfo) + |
| 357 | sizeof(struct vmbus_channel_gpadl_header) + |
| 358 | sizeof(struct gpa_range) + pagecount * sizeof(u64); |
| 359 | msgheader = kzalloc(msgsize, GFP_KERNEL); |
| 360 | if (msgheader == NULL) |
| 361 | goto nomem; |
| 362 | |
| 363 | INIT_LIST_HEAD(&msgheader->submsglist); |
| 364 | msgheader->msgsize = msgsize; |
| 365 | |
| 366 | gpadl_header = (struct vmbus_channel_gpadl_header *) |
| 367 | msgheader->msg; |
| 368 | gpadl_header->rangecount = 1; |
| 369 | gpadl_header->range_buflen = sizeof(struct gpa_range) + |
| 370 | pagecount * sizeof(u64); |
| 371 | gpadl_header->range[0].byte_offset = 0; |
| 372 | gpadl_header->range[0].byte_count = size; |
| 373 | for (i = 0; i < pagecount; i++) |
| 374 | gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys( |
| 375 | kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT; |
| 376 | |
| 377 | *msginfo = msgheader; |
| 378 | } |
| 379 | |
| 380 | return 0; |
| 381 | nomem: |
| 382 | kfree(msgheader); |
| 383 | kfree(msgbody); |
| 384 | return -ENOMEM; |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * vmbus_establish_gpadl - Establish a GPADL for the specified buffer |
| 389 | * |
| 390 | * @channel: a channel |
| 391 | * @kbuffer: from kmalloc or vmalloc |
| 392 | * @size: page-size multiple |
| 393 | * @gpadl_handle: some funky thing |
| 394 | */ |
| 395 | int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer, |
| 396 | u32 size, u32 *gpadl_handle) |
| 397 | { |
| 398 | struct vmbus_channel_gpadl_header *gpadlmsg; |
| 399 | struct vmbus_channel_gpadl_body *gpadl_body; |
| 400 | struct vmbus_channel_msginfo *msginfo = NULL; |
| 401 | struct vmbus_channel_msginfo *submsginfo, *tmp; |
| 402 | struct list_head *curr; |
| 403 | u32 next_gpadl_handle; |
| 404 | unsigned long flags; |
| 405 | int ret = 0; |
| 406 | |
| 407 | next_gpadl_handle = |
| 408 | (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1); |
| 409 | |
| 410 | ret = create_gpadl_header(kbuffer, size, &msginfo); |
| 411 | if (ret) |
| 412 | return ret; |
| 413 | |
| 414 | init_completion(&msginfo->waitevent); |
| 415 | msginfo->waiting_channel = channel; |
| 416 | |
| 417 | gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg; |
| 418 | gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER; |
| 419 | gpadlmsg->child_relid = channel->offermsg.child_relid; |
| 420 | gpadlmsg->gpadl = next_gpadl_handle; |
| 421 | |
| 422 | |
| 423 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
| 424 | list_add_tail(&msginfo->msglistentry, |
| 425 | &vmbus_connection.chn_msg_list); |
| 426 | |
| 427 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
| 428 | |
| 429 | if (channel->rescind) { |
| 430 | ret = -ENODEV; |
| 431 | goto cleanup; |
| 432 | } |
| 433 | |
| 434 | ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize - |
| 435 | sizeof(*msginfo), true); |
| 436 | if (ret != 0) |
| 437 | goto cleanup; |
| 438 | |
| 439 | list_for_each(curr, &msginfo->submsglist) { |
| 440 | submsginfo = (struct vmbus_channel_msginfo *)curr; |
| 441 | gpadl_body = |
| 442 | (struct vmbus_channel_gpadl_body *)submsginfo->msg; |
| 443 | |
| 444 | gpadl_body->header.msgtype = |
| 445 | CHANNELMSG_GPADL_BODY; |
| 446 | gpadl_body->gpadl = next_gpadl_handle; |
| 447 | |
| 448 | ret = vmbus_post_msg(gpadl_body, |
| 449 | submsginfo->msgsize - sizeof(*submsginfo), |
| 450 | true); |
| 451 | if (ret != 0) |
| 452 | goto cleanup; |
| 453 | |
| 454 | } |
| 455 | wait_for_completion(&msginfo->waitevent); |
| 456 | |
| 457 | if (msginfo->response.gpadl_created.creation_status != 0) { |
| 458 | pr_err("Failed to establish GPADL: err = 0x%x\n", |
| 459 | msginfo->response.gpadl_created.creation_status); |
| 460 | |
| 461 | ret = -EDQUOT; |
| 462 | goto cleanup; |
| 463 | } |
| 464 | |
| 465 | if (channel->rescind) { |
| 466 | ret = -ENODEV; |
| 467 | goto cleanup; |
| 468 | } |
| 469 | |
| 470 | /* At this point, we received the gpadl created msg */ |
| 471 | *gpadl_handle = gpadlmsg->gpadl; |
| 472 | |
| 473 | cleanup: |
| 474 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
| 475 | list_del(&msginfo->msglistentry); |
| 476 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
| 477 | list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist, |
| 478 | msglistentry) { |
| 479 | kfree(submsginfo); |
| 480 | } |
| 481 | |
| 482 | kfree(msginfo); |
| 483 | return ret; |
| 484 | } |
| 485 | EXPORT_SYMBOL_GPL(vmbus_establish_gpadl); |
| 486 | |
| 487 | /* |
| 488 | * vmbus_teardown_gpadl -Teardown the specified GPADL handle |
| 489 | */ |
| 490 | int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle) |
| 491 | { |
| 492 | struct vmbus_channel_gpadl_teardown *msg; |
| 493 | struct vmbus_channel_msginfo *info; |
| 494 | unsigned long flags; |
| 495 | int ret; |
| 496 | |
| 497 | info = kmalloc(sizeof(*info) + |
| 498 | sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL); |
| 499 | if (!info) |
| 500 | return -ENOMEM; |
| 501 | |
| 502 | init_completion(&info->waitevent); |
| 503 | info->waiting_channel = channel; |
| 504 | |
| 505 | msg = (struct vmbus_channel_gpadl_teardown *)info->msg; |
| 506 | |
| 507 | msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN; |
| 508 | msg->child_relid = channel->offermsg.child_relid; |
| 509 | msg->gpadl = gpadl_handle; |
| 510 | |
| 511 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
| 512 | list_add_tail(&info->msglistentry, |
| 513 | &vmbus_connection.chn_msg_list); |
| 514 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
| 515 | |
| 516 | if (channel->rescind) |
| 517 | goto post_msg_err; |
| 518 | |
| 519 | ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_gpadl_teardown), |
| 520 | true); |
| 521 | |
| 522 | if (ret) |
| 523 | goto post_msg_err; |
| 524 | |
| 525 | wait_for_completion(&info->waitevent); |
| 526 | |
| 527 | post_msg_err: |
| 528 | /* |
| 529 | * If the channel has been rescinded; |
| 530 | * we will be awakened by the rescind |
| 531 | * handler; set the error code to zero so we don't leak memory. |
| 532 | */ |
| 533 | if (channel->rescind) |
| 534 | ret = 0; |
| 535 | |
| 536 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
| 537 | list_del(&info->msglistentry); |
| 538 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
| 539 | |
| 540 | kfree(info); |
| 541 | return ret; |
| 542 | } |
| 543 | EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl); |
| 544 | |
| 545 | static void reset_channel_cb(void *arg) |
| 546 | { |
| 547 | struct vmbus_channel *channel = arg; |
| 548 | |
| 549 | channel->onchannel_callback = NULL; |
| 550 | } |
| 551 | |
| 552 | void vmbus_reset_channel_cb(struct vmbus_channel *channel) |
| 553 | { |
| 554 | /* |
| 555 | * vmbus_on_event(), running in the per-channel tasklet, can race |
| 556 | * with vmbus_close_internal() in the case of SMP guest, e.g., when |
| 557 | * the former is accessing channel->inbound.ring_buffer, the latter |
| 558 | * could be freeing the ring_buffer pages, so here we must stop it |
| 559 | * first. |
| 560 | */ |
| 561 | tasklet_disable(&channel->callback_event); |
| 562 | |
| 563 | channel->sc_creation_callback = NULL; |
| 564 | |
| 565 | /* Stop the callback asap */ |
| 566 | if (channel->target_cpu != get_cpu()) { |
| 567 | put_cpu(); |
| 568 | smp_call_function_single(channel->target_cpu, reset_channel_cb, |
| 569 | channel, true); |
| 570 | } else { |
| 571 | reset_channel_cb(channel); |
| 572 | put_cpu(); |
| 573 | } |
| 574 | |
| 575 | /* Re-enable tasklet for use on re-open */ |
| 576 | tasklet_enable(&channel->callback_event); |
| 577 | } |
| 578 | |
| 579 | static int vmbus_close_internal(struct vmbus_channel *channel) |
| 580 | { |
| 581 | struct vmbus_channel_close_channel *msg; |
| 582 | int ret; |
| 583 | |
| 584 | vmbus_reset_channel_cb(channel); |
| 585 | |
| 586 | /* |
| 587 | * In case a device driver's probe() fails (e.g., |
| 588 | * util_probe() -> vmbus_open() returns -ENOMEM) and the device is |
| 589 | * rescinded later (e.g., we dynamically disable an Integrated Service |
| 590 | * in Hyper-V Manager), the driver's remove() invokes vmbus_close(): |
| 591 | * here we should skip most of the below cleanup work. |
| 592 | */ |
| 593 | if (channel->state != CHANNEL_OPENED_STATE) { |
| 594 | ret = -EINVAL; |
| 595 | goto out; |
| 596 | } |
| 597 | |
| 598 | channel->state = CHANNEL_OPEN_STATE; |
| 599 | |
| 600 | /* Send a closing message */ |
| 601 | |
| 602 | msg = &channel->close_msg.msg; |
| 603 | |
| 604 | msg->header.msgtype = CHANNELMSG_CLOSECHANNEL; |
| 605 | msg->child_relid = channel->offermsg.child_relid; |
| 606 | |
| 607 | ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel), |
| 608 | true); |
| 609 | |
| 610 | if (ret) { |
| 611 | pr_err("Close failed: close post msg return is %d\n", ret); |
| 612 | /* |
| 613 | * If we failed to post the close msg, |
| 614 | * it is perhaps better to leak memory. |
| 615 | */ |
| 616 | goto out; |
| 617 | } |
| 618 | |
| 619 | /* Tear down the gpadl for the channel's ring buffer */ |
| 620 | if (channel->ringbuffer_gpadlhandle) { |
| 621 | ret = vmbus_teardown_gpadl(channel, |
| 622 | channel->ringbuffer_gpadlhandle); |
| 623 | if (ret) { |
| 624 | pr_err("Close failed: teardown gpadl return %d\n", ret); |
| 625 | /* |
| 626 | * If we failed to teardown gpadl, |
| 627 | * it is perhaps better to leak memory. |
| 628 | */ |
| 629 | goto out; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | /* Cleanup the ring buffers for this channel */ |
| 634 | hv_ringbuffer_cleanup(&channel->outbound); |
| 635 | hv_ringbuffer_cleanup(&channel->inbound); |
| 636 | |
| 637 | free_pages((unsigned long)channel->ringbuffer_pages, |
| 638 | get_order(channel->ringbuffer_pagecount * PAGE_SIZE)); |
| 639 | |
| 640 | out: |
| 641 | return ret; |
| 642 | } |
| 643 | |
| 644 | /* |
| 645 | * vmbus_close - Close the specified channel |
| 646 | */ |
| 647 | void vmbus_close(struct vmbus_channel *channel) |
| 648 | { |
| 649 | struct list_head *cur, *tmp; |
| 650 | struct vmbus_channel *cur_channel; |
| 651 | |
| 652 | if (channel->primary_channel != NULL) { |
| 653 | /* |
| 654 | * We will only close sub-channels when |
| 655 | * the primary is closed. |
| 656 | */ |
| 657 | return; |
| 658 | } |
| 659 | /* |
| 660 | * Close all the sub-channels first and then close the |
| 661 | * primary channel. |
| 662 | */ |
| 663 | list_for_each_safe(cur, tmp, &channel->sc_list) { |
| 664 | cur_channel = list_entry(cur, struct vmbus_channel, sc_list); |
| 665 | if (cur_channel->rescind) { |
| 666 | wait_for_completion(&cur_channel->rescind_event); |
| 667 | mutex_lock(&vmbus_connection.channel_mutex); |
| 668 | vmbus_close_internal(cur_channel); |
| 669 | hv_process_channel_removal( |
| 670 | cur_channel->offermsg.child_relid); |
| 671 | } else { |
| 672 | mutex_lock(&vmbus_connection.channel_mutex); |
| 673 | vmbus_close_internal(cur_channel); |
| 674 | } |
| 675 | mutex_unlock(&vmbus_connection.channel_mutex); |
| 676 | } |
| 677 | /* |
| 678 | * Now close the primary. |
| 679 | */ |
| 680 | mutex_lock(&vmbus_connection.channel_mutex); |
| 681 | vmbus_close_internal(channel); |
| 682 | mutex_unlock(&vmbus_connection.channel_mutex); |
| 683 | } |
| 684 | EXPORT_SYMBOL_GPL(vmbus_close); |
| 685 | |
| 686 | /** |
| 687 | * vmbus_sendpacket() - Send the specified buffer on the given channel |
| 688 | * @channel: Pointer to vmbus_channel structure. |
| 689 | * @buffer: Pointer to the buffer you want to receive the data into. |
| 690 | * @bufferlen: Maximum size of what the the buffer will hold |
| 691 | * @requestid: Identifier of the request |
| 692 | * @type: Type of packet that is being send e.g. negotiate, time |
| 693 | * packet etc. |
| 694 | * |
| 695 | * Sends data in @buffer directly to hyper-v via the vmbus |
| 696 | * This will send the data unparsed to hyper-v. |
| 697 | * |
| 698 | * Mainly used by Hyper-V drivers. |
| 699 | */ |
| 700 | int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer, |
| 701 | u32 bufferlen, u64 requestid, |
| 702 | enum vmbus_packet_type type, u32 flags) |
| 703 | { |
| 704 | struct vmpacket_descriptor desc; |
| 705 | u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen; |
| 706 | u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64)); |
| 707 | struct kvec bufferlist[3]; |
| 708 | u64 aligned_data = 0; |
| 709 | int num_vecs = ((bufferlen != 0) ? 3 : 1); |
| 710 | |
| 711 | |
| 712 | /* Setup the descriptor */ |
| 713 | desc.type = type; /* VmbusPacketTypeDataInBand; */ |
| 714 | desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */ |
| 715 | /* in 8-bytes granularity */ |
| 716 | desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3; |
| 717 | desc.len8 = (u16)(packetlen_aligned >> 3); |
| 718 | desc.trans_id = requestid; |
| 719 | |
| 720 | bufferlist[0].iov_base = &desc; |
| 721 | bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor); |
| 722 | bufferlist[1].iov_base = buffer; |
| 723 | bufferlist[1].iov_len = bufferlen; |
| 724 | bufferlist[2].iov_base = &aligned_data; |
| 725 | bufferlist[2].iov_len = (packetlen_aligned - packetlen); |
| 726 | |
| 727 | return hv_ringbuffer_write(channel, bufferlist, num_vecs); |
| 728 | } |
| 729 | EXPORT_SYMBOL(vmbus_sendpacket); |
| 730 | |
| 731 | /* |
| 732 | * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer |
| 733 | * packets using a GPADL Direct packet type. This interface allows you |
| 734 | * to control notifying the host. This will be useful for sending |
| 735 | * batched data. Also the sender can control the send flags |
| 736 | * explicitly. |
| 737 | */ |
| 738 | int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel, |
| 739 | struct hv_page_buffer pagebuffers[], |
| 740 | u32 pagecount, void *buffer, u32 bufferlen, |
| 741 | u64 requestid) |
| 742 | { |
| 743 | int i; |
| 744 | struct vmbus_channel_packet_page_buffer desc; |
| 745 | u32 descsize; |
| 746 | u32 packetlen; |
| 747 | u32 packetlen_aligned; |
| 748 | struct kvec bufferlist[3]; |
| 749 | u64 aligned_data = 0; |
| 750 | |
| 751 | if (pagecount > MAX_PAGE_BUFFER_COUNT) |
| 752 | return -EINVAL; |
| 753 | |
| 754 | /* |
| 755 | * Adjust the size down since vmbus_channel_packet_page_buffer is the |
| 756 | * largest size we support |
| 757 | */ |
| 758 | descsize = sizeof(struct vmbus_channel_packet_page_buffer) - |
| 759 | ((MAX_PAGE_BUFFER_COUNT - pagecount) * |
| 760 | sizeof(struct hv_page_buffer)); |
| 761 | packetlen = descsize + bufferlen; |
| 762 | packetlen_aligned = ALIGN(packetlen, sizeof(u64)); |
| 763 | |
| 764 | /* Setup the descriptor */ |
| 765 | desc.type = VM_PKT_DATA_USING_GPA_DIRECT; |
| 766 | desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; |
| 767 | desc.dataoffset8 = descsize >> 3; /* in 8-bytes granularity */ |
| 768 | desc.length8 = (u16)(packetlen_aligned >> 3); |
| 769 | desc.transactionid = requestid; |
| 770 | desc.rangecount = pagecount; |
| 771 | |
| 772 | for (i = 0; i < pagecount; i++) { |
| 773 | desc.range[i].len = pagebuffers[i].len; |
| 774 | desc.range[i].offset = pagebuffers[i].offset; |
| 775 | desc.range[i].pfn = pagebuffers[i].pfn; |
| 776 | } |
| 777 | |
| 778 | bufferlist[0].iov_base = &desc; |
| 779 | bufferlist[0].iov_len = descsize; |
| 780 | bufferlist[1].iov_base = buffer; |
| 781 | bufferlist[1].iov_len = bufferlen; |
| 782 | bufferlist[2].iov_base = &aligned_data; |
| 783 | bufferlist[2].iov_len = (packetlen_aligned - packetlen); |
| 784 | |
| 785 | return hv_ringbuffer_write(channel, bufferlist, 3); |
| 786 | } |
| 787 | EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer); |
| 788 | |
| 789 | /* |
| 790 | * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet |
| 791 | * using a GPADL Direct packet type. |
| 792 | * The buffer includes the vmbus descriptor. |
| 793 | */ |
| 794 | int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel, |
| 795 | struct vmbus_packet_mpb_array *desc, |
| 796 | u32 desc_size, |
| 797 | void *buffer, u32 bufferlen, u64 requestid) |
| 798 | { |
| 799 | u32 packetlen; |
| 800 | u32 packetlen_aligned; |
| 801 | struct kvec bufferlist[3]; |
| 802 | u64 aligned_data = 0; |
| 803 | |
| 804 | packetlen = desc_size + bufferlen; |
| 805 | packetlen_aligned = ALIGN(packetlen, sizeof(u64)); |
| 806 | |
| 807 | /* Setup the descriptor */ |
| 808 | desc->type = VM_PKT_DATA_USING_GPA_DIRECT; |
| 809 | desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; |
| 810 | desc->dataoffset8 = desc_size >> 3; /* in 8-bytes granularity */ |
| 811 | desc->length8 = (u16)(packetlen_aligned >> 3); |
| 812 | desc->transactionid = requestid; |
| 813 | desc->rangecount = 1; |
| 814 | |
| 815 | bufferlist[0].iov_base = desc; |
| 816 | bufferlist[0].iov_len = desc_size; |
| 817 | bufferlist[1].iov_base = buffer; |
| 818 | bufferlist[1].iov_len = bufferlen; |
| 819 | bufferlist[2].iov_base = &aligned_data; |
| 820 | bufferlist[2].iov_len = (packetlen_aligned - packetlen); |
| 821 | |
| 822 | return hv_ringbuffer_write(channel, bufferlist, 3); |
| 823 | } |
| 824 | EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc); |
| 825 | |
| 826 | /** |
| 827 | * vmbus_recvpacket() - Retrieve the user packet on the specified channel |
| 828 | * @channel: Pointer to vmbus_channel structure. |
| 829 | * @buffer: Pointer to the buffer you want to receive the data into. |
| 830 | * @bufferlen: Maximum size of what the the buffer will hold |
| 831 | * @buffer_actual_len: The actual size of the data after it was received |
| 832 | * @requestid: Identifier of the request |
| 833 | * |
| 834 | * Receives directly from the hyper-v vmbus and puts the data it received |
| 835 | * into Buffer. This will receive the data unparsed from hyper-v. |
| 836 | * |
| 837 | * Mainly used by Hyper-V drivers. |
| 838 | */ |
| 839 | static inline int |
| 840 | __vmbus_recvpacket(struct vmbus_channel *channel, void *buffer, |
| 841 | u32 bufferlen, u32 *buffer_actual_len, u64 *requestid, |
| 842 | bool raw) |
| 843 | { |
| 844 | return hv_ringbuffer_read(channel, buffer, bufferlen, |
| 845 | buffer_actual_len, requestid, raw); |
| 846 | |
| 847 | } |
| 848 | |
| 849 | int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer, |
| 850 | u32 bufferlen, u32 *buffer_actual_len, |
| 851 | u64 *requestid) |
| 852 | { |
| 853 | return __vmbus_recvpacket(channel, buffer, bufferlen, |
| 854 | buffer_actual_len, requestid, false); |
| 855 | } |
| 856 | EXPORT_SYMBOL(vmbus_recvpacket); |
| 857 | |
| 858 | /* |
| 859 | * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel |
| 860 | */ |
| 861 | int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer, |
| 862 | u32 bufferlen, u32 *buffer_actual_len, |
| 863 | u64 *requestid) |
| 864 | { |
| 865 | return __vmbus_recvpacket(channel, buffer, bufferlen, |
| 866 | buffer_actual_len, requestid, true); |
| 867 | } |
| 868 | EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw); |