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 ret = vmbus_post_msg(open_msg, 181 sizeof(struct vmbus_channel_open_channel), true); 182 183 if (ret != 0) { 184 err = ret; 185 goto error_clean_msglist; 186 } 187 188 wait_for_completion(&open_info->waitevent); 189 190 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); 191 list_del(&open_info->msglistentry); 192 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); 193 194 if (newchannel->rescind) { 195 err = -ENODEV; 196 goto error_free_gpadl; 197 } 198 199 if (open_info->response.open_result.status) { 200 err = -EAGAIN; 201 goto error_free_gpadl; 202 } 203 204 newchannel->state = CHANNEL_OPENED_STATE; 205 kfree(open_info); 206 return 0; 207 208 error_clean_msglist: 209 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); 210 list_del(&open_info->msglistentry); 211 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); 212 213 error_free_gpadl: 214 vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle); 215 kfree(open_info); 216 error_free_pages: 217 hv_ringbuffer_cleanup(&newchannel->outbound); 218 hv_ringbuffer_cleanup(&newchannel->inbound); 219 __free_pages(page, 220 get_order(send_ringbuffer_size + recv_ringbuffer_size)); 221 error_set_chnstate: 222 newchannel->state = CHANNEL_OPEN_STATE; 223 return err; 224 } 225 EXPORT_SYMBOL_GPL(vmbus_open); 226 227 /* Used for Hyper-V Socket: a guest client's connect() to the host */ 228 int vmbus_send_tl_connect_request(const uuid_le *shv_guest_servie_id, 229 const uuid_le *shv_host_servie_id) 230 { 231 struct vmbus_channel_tl_connect_request conn_msg; 232 233 memset(&conn_msg, 0, sizeof(conn_msg)); 234 conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST; 235 conn_msg.guest_endpoint_id = *shv_guest_servie_id; 236 conn_msg.host_service_id = *shv_host_servie_id; 237 238 return vmbus_post_msg(&conn_msg, sizeof(conn_msg), true); 239 } 240 EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request); 241 242 /* 243 * create_gpadl_header - Creates a gpadl for the specified buffer 244 */ 245 static int create_gpadl_header(void *kbuffer, u32 size, 246 struct vmbus_channel_msginfo **msginfo) 247 { 248 int i; 249 int pagecount; 250 struct vmbus_channel_gpadl_header *gpadl_header; 251 struct vmbus_channel_gpadl_body *gpadl_body; 252 struct vmbus_channel_msginfo *msgheader; 253 struct vmbus_channel_msginfo *msgbody = NULL; 254 u32 msgsize; 255 256 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize; 257 258 pagecount = size >> PAGE_SHIFT; 259 260 /* do we need a gpadl body msg */ 261 pfnsize = MAX_SIZE_CHANNEL_MESSAGE - 262 sizeof(struct vmbus_channel_gpadl_header) - 263 sizeof(struct gpa_range); 264 pfncount = pfnsize / sizeof(u64); 265 266 if (pagecount > pfncount) { 267 /* we need a gpadl body */ 268 /* fill in the header */ 269 msgsize = sizeof(struct vmbus_channel_msginfo) + 270 sizeof(struct vmbus_channel_gpadl_header) + 271 sizeof(struct gpa_range) + pfncount * sizeof(u64); 272 msgheader = kzalloc(msgsize, GFP_KERNEL); 273 if (!msgheader) 274 goto nomem; 275 276 INIT_LIST_HEAD(&msgheader->submsglist); 277 msgheader->msgsize = msgsize; 278 279 gpadl_header = (struct vmbus_channel_gpadl_header *) 280 msgheader->msg; 281 gpadl_header->rangecount = 1; 282 gpadl_header->range_buflen = sizeof(struct gpa_range) + 283 pagecount * sizeof(u64); 284 gpadl_header->range[0].byte_offset = 0; 285 gpadl_header->range[0].byte_count = size; 286 for (i = 0; i < pfncount; i++) 287 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys( 288 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT; 289 *msginfo = msgheader; 290 291 pfnsum = pfncount; 292 pfnleft = pagecount - pfncount; 293 294 /* how many pfns can we fit */ 295 pfnsize = MAX_SIZE_CHANNEL_MESSAGE - 296 sizeof(struct vmbus_channel_gpadl_body); 297 pfncount = pfnsize / sizeof(u64); 298 299 /* fill in the body */ 300 while (pfnleft) { 301 if (pfnleft > pfncount) 302 pfncurr = pfncount; 303 else 304 pfncurr = pfnleft; 305 306 msgsize = sizeof(struct vmbus_channel_msginfo) + 307 sizeof(struct vmbus_channel_gpadl_body) + 308 pfncurr * sizeof(u64); 309 msgbody = kzalloc(msgsize, GFP_KERNEL); 310 311 if (!msgbody) { 312 struct vmbus_channel_msginfo *pos = NULL; 313 struct vmbus_channel_msginfo *tmp = NULL; 314 /* 315 * Free up all the allocated messages. 316 */ 317 list_for_each_entry_safe(pos, tmp, 318 &msgheader->submsglist, 319 msglistentry) { 320 321 list_del(&pos->msglistentry); 322 kfree(pos); 323 } 324 325 goto nomem; 326 } 327 328 msgbody->msgsize = msgsize; 329 gpadl_body = 330 (struct vmbus_channel_gpadl_body *)msgbody->msg; 331 332 /* 333 * Gpadl is u32 and we are using a pointer which could 334 * be 64-bit 335 * This is governed by the guest/host protocol and 336 * so the hypervisor gurantees that this is ok. 337 */ 338 for (i = 0; i < pfncurr; i++) 339 gpadl_body->pfn[i] = slow_virt_to_phys( 340 kbuffer + PAGE_SIZE * (pfnsum + i)) >> 341 PAGE_SHIFT; 342 343 /* add to msg header */ 344 list_add_tail(&msgbody->msglistentry, 345 &msgheader->submsglist); 346 pfnsum += pfncurr; 347 pfnleft -= pfncurr; 348 } 349 } else { 350 /* everything fits in a header */ 351 msgsize = sizeof(struct vmbus_channel_msginfo) + 352 sizeof(struct vmbus_channel_gpadl_header) + 353 sizeof(struct gpa_range) + pagecount * sizeof(u64); 354 msgheader = kzalloc(msgsize, GFP_KERNEL); 355 if (msgheader == NULL) 356 goto nomem; 357 358 INIT_LIST_HEAD(&msgheader->submsglist); 359 msgheader->msgsize = msgsize; 360 361 gpadl_header = (struct vmbus_channel_gpadl_header *) 362 msgheader->msg; 363 gpadl_header->rangecount = 1; 364 gpadl_header->range_buflen = sizeof(struct gpa_range) + 365 pagecount * sizeof(u64); 366 gpadl_header->range[0].byte_offset = 0; 367 gpadl_header->range[0].byte_count = size; 368 for (i = 0; i < pagecount; i++) 369 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys( 370 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT; 371 372 *msginfo = msgheader; 373 } 374 375 return 0; 376 nomem: 377 kfree(msgheader); 378 kfree(msgbody); 379 return -ENOMEM; 380 } 381 382 /* 383 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer 384 * 385 * @channel: a channel 386 * @kbuffer: from kmalloc or vmalloc 387 * @size: page-size multiple 388 * @gpadl_handle: some funky thing 389 */ 390 int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer, 391 u32 size, u32 *gpadl_handle) 392 { 393 struct vmbus_channel_gpadl_header *gpadlmsg; 394 struct vmbus_channel_gpadl_body *gpadl_body; 395 struct vmbus_channel_msginfo *msginfo = NULL; 396 struct vmbus_channel_msginfo *submsginfo, *tmp; 397 struct list_head *curr; 398 u32 next_gpadl_handle; 399 unsigned long flags; 400 int ret = 0; 401 402 next_gpadl_handle = 403 (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1); 404 405 ret = create_gpadl_header(kbuffer, size, &msginfo); 406 if (ret) 407 return ret; 408 409 init_completion(&msginfo->waitevent); 410 msginfo->waiting_channel = channel; 411 412 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg; 413 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER; 414 gpadlmsg->child_relid = channel->offermsg.child_relid; 415 gpadlmsg->gpadl = next_gpadl_handle; 416 417 418 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); 419 list_add_tail(&msginfo->msglistentry, 420 &vmbus_connection.chn_msg_list); 421 422 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); 423 424 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize - 425 sizeof(*msginfo), true); 426 if (ret != 0) 427 goto cleanup; 428 429 list_for_each(curr, &msginfo->submsglist) { 430 submsginfo = (struct vmbus_channel_msginfo *)curr; 431 gpadl_body = 432 (struct vmbus_channel_gpadl_body *)submsginfo->msg; 433 434 gpadl_body->header.msgtype = 435 CHANNELMSG_GPADL_BODY; 436 gpadl_body->gpadl = next_gpadl_handle; 437 438 ret = vmbus_post_msg(gpadl_body, 439 submsginfo->msgsize - sizeof(*submsginfo), 440 true); 441 if (ret != 0) 442 goto cleanup; 443 444 } 445 wait_for_completion(&msginfo->waitevent); 446 447 if (channel->rescind) { 448 ret = -ENODEV; 449 goto cleanup; 450 } 451 452 /* At this point, we received the gpadl created msg */ 453 *gpadl_handle = gpadlmsg->gpadl; 454 455 cleanup: 456 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); 457 list_del(&msginfo->msglistentry); 458 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); 459 list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist, 460 msglistentry) { 461 kfree(submsginfo); 462 } 463 464 kfree(msginfo); 465 return ret; 466 } 467 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl); 468 469 /* 470 * vmbus_teardown_gpadl -Teardown the specified GPADL handle 471 */ 472 int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle) 473 { 474 struct vmbus_channel_gpadl_teardown *msg; 475 struct vmbus_channel_msginfo *info; 476 unsigned long flags; 477 int ret; 478 479 info = kmalloc(sizeof(*info) + 480 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL); 481 if (!info) 482 return -ENOMEM; 483 484 init_completion(&info->waitevent); 485 info->waiting_channel = channel; 486 487 msg = (struct vmbus_channel_gpadl_teardown *)info->msg; 488 489 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN; 490 msg->child_relid = channel->offermsg.child_relid; 491 msg->gpadl = gpadl_handle; 492 493 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); 494 list_add_tail(&info->msglistentry, 495 &vmbus_connection.chn_msg_list); 496 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); 497 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_gpadl_teardown), 498 true); 499 500 if (ret) 501 goto post_msg_err; 502 503 wait_for_completion(&info->waitevent); 504 505 post_msg_err: 506 /* 507 * If the channel has been rescinded; 508 * we will be awakened by the rescind 509 * handler; set the error code to zero so we don't leak memory. 510 */ 511 if (channel->rescind) 512 ret = 0; 513 514 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); 515 list_del(&info->msglistentry); 516 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); 517 518 kfree(info); 519 return ret; 520 } 521 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl); 522 523 static void reset_channel_cb(void *arg) 524 { 525 struct vmbus_channel *channel = arg; 526 527 channel->onchannel_callback = NULL; 528 } 529 530 static int vmbus_close_internal(struct vmbus_channel *channel) 531 { 532 struct vmbus_channel_close_channel *msg; 533 int ret; 534 535 /* 536 * vmbus_on_event(), running in the per-channel tasklet, can race 537 * with vmbus_close_internal() in the case of SMP guest, e.g., when 538 * the former is accessing channel->inbound.ring_buffer, the latter 539 * could be freeing the ring_buffer pages, so here we must stop it 540 * first. 541 */ 542 tasklet_disable(&channel->callback_event); 543 544 /* 545 * In case a device driver's probe() fails (e.g., 546 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is 547 * rescinded later (e.g., we dynamically disable an Integrated Service 548 * in Hyper-V Manager), the driver's remove() invokes vmbus_close(): 549 * here we should skip most of the below cleanup work. 550 */ 551 if (channel->state != CHANNEL_OPENED_STATE) { 552 ret = -EINVAL; 553 goto out; 554 } 555 556 channel->state = CHANNEL_OPEN_STATE; 557 channel->sc_creation_callback = NULL; 558 /* Stop callback and cancel the timer asap */ 559 if (channel->target_cpu != get_cpu()) { 560 put_cpu(); 561 smp_call_function_single(channel->target_cpu, reset_channel_cb, 562 channel, true); 563 } else { 564 reset_channel_cb(channel); 565 put_cpu(); 566 } 567 568 /* Send a closing message */ 569 570 msg = &channel->close_msg.msg; 571 572 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL; 573 msg->child_relid = channel->offermsg.child_relid; 574 575 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel), 576 true); 577 578 if (ret) { 579 pr_err("Close failed: close post msg return is %d\n", ret); 580 /* 581 * If we failed to post the close msg, 582 * it is perhaps better to leak memory. 583 */ 584 goto out; 585 } 586 587 /* Tear down the gpadl for the channel's ring buffer */ 588 if (channel->ringbuffer_gpadlhandle) { 589 ret = vmbus_teardown_gpadl(channel, 590 channel->ringbuffer_gpadlhandle); 591 if (ret) { 592 pr_err("Close failed: teardown gpadl return %d\n", ret); 593 /* 594 * If we failed to teardown gpadl, 595 * it is perhaps better to leak memory. 596 */ 597 goto out; 598 } 599 } 600 601 /* Cleanup the ring buffers for this channel */ 602 hv_ringbuffer_cleanup(&channel->outbound); 603 hv_ringbuffer_cleanup(&channel->inbound); 604 605 free_pages((unsigned long)channel->ringbuffer_pages, 606 get_order(channel->ringbuffer_pagecount * PAGE_SIZE)); 607 608 out: 609 return ret; 610 } 611 612 /* 613 * vmbus_close - Close the specified channel 614 */ 615 void vmbus_close(struct vmbus_channel *channel) 616 { 617 struct list_head *cur, *tmp; 618 struct vmbus_channel *cur_channel; 619 620 if (channel->primary_channel != NULL) { 621 /* 622 * We will only close sub-channels when 623 * the primary is closed. 624 */ 625 return; 626 } 627 /* 628 * Close all the sub-channels first and then close the 629 * primary channel. 630 */ 631 list_for_each_safe(cur, tmp, &channel->sc_list) { 632 cur_channel = list_entry(cur, struct vmbus_channel, sc_list); 633 if (cur_channel->state != CHANNEL_OPENED_STATE) 634 continue; 635 vmbus_close_internal(cur_channel); 636 } 637 /* 638 * Now close the primary. 639 */ 640 vmbus_close_internal(channel); 641 } 642 EXPORT_SYMBOL_GPL(vmbus_close); 643 644 int vmbus_sendpacket_ctl(struct vmbus_channel *channel, void *buffer, 645 u32 bufferlen, u64 requestid, 646 enum vmbus_packet_type type, u32 flags) 647 { 648 struct vmpacket_descriptor desc; 649 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen; 650 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64)); 651 struct kvec bufferlist[3]; 652 u64 aligned_data = 0; 653 int num_vecs = ((bufferlen != 0) ? 3 : 1); 654 655 656 /* Setup the descriptor */ 657 desc.type = type; /* VmbusPacketTypeDataInBand; */ 658 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */ 659 /* in 8-bytes granularity */ 660 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3; 661 desc.len8 = (u16)(packetlen_aligned >> 3); 662 desc.trans_id = requestid; 663 664 bufferlist[0].iov_base = &desc; 665 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor); 666 bufferlist[1].iov_base = buffer; 667 bufferlist[1].iov_len = bufferlen; 668 bufferlist[2].iov_base = &aligned_data; 669 bufferlist[2].iov_len = (packetlen_aligned - packetlen); 670 671 return hv_ringbuffer_write(channel, bufferlist, num_vecs); 672 } 673 EXPORT_SYMBOL(vmbus_sendpacket_ctl); 674 675 /** 676 * vmbus_sendpacket() - Send the specified buffer on the given channel 677 * @channel: Pointer to vmbus_channel structure. 678 * @buffer: Pointer to the buffer you want to receive the data into. 679 * @bufferlen: Maximum size of what the the buffer will hold 680 * @requestid: Identifier of the request 681 * @type: Type of packet that is being send e.g. negotiate, time 682 * packet etc. 683 * 684 * Sends data in @buffer directly to hyper-v via the vmbus 685 * This will send the data unparsed to hyper-v. 686 * 687 * Mainly used by Hyper-V drivers. 688 */ 689 int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer, 690 u32 bufferlen, u64 requestid, 691 enum vmbus_packet_type type, u32 flags) 692 { 693 return vmbus_sendpacket_ctl(channel, buffer, bufferlen, requestid, 694 type, flags); 695 } 696 EXPORT_SYMBOL(vmbus_sendpacket); 697 698 /* 699 * vmbus_sendpacket_pagebuffer_ctl - Send a range of single-page buffer 700 * packets using a GPADL Direct packet type. This interface allows you 701 * to control notifying the host. This will be useful for sending 702 * batched data. Also the sender can control the send flags 703 * explicitly. 704 */ 705 int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel *channel, 706 struct hv_page_buffer pagebuffers[], 707 u32 pagecount, void *buffer, u32 bufferlen, 708 u64 requestid, u32 flags) 709 { 710 int i; 711 struct vmbus_channel_packet_page_buffer desc; 712 u32 descsize; 713 u32 packetlen; 714 u32 packetlen_aligned; 715 struct kvec bufferlist[3]; 716 u64 aligned_data = 0; 717 718 if (pagecount > MAX_PAGE_BUFFER_COUNT) 719 return -EINVAL; 720 721 /* 722 * Adjust the size down since vmbus_channel_packet_page_buffer is the 723 * largest size we support 724 */ 725 descsize = sizeof(struct vmbus_channel_packet_page_buffer) - 726 ((MAX_PAGE_BUFFER_COUNT - pagecount) * 727 sizeof(struct hv_page_buffer)); 728 packetlen = descsize + bufferlen; 729 packetlen_aligned = ALIGN(packetlen, sizeof(u64)); 730 731 /* Setup the descriptor */ 732 desc.type = VM_PKT_DATA_USING_GPA_DIRECT; 733 desc.flags = flags; 734 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */ 735 desc.length8 = (u16)(packetlen_aligned >> 3); 736 desc.transactionid = requestid; 737 desc.rangecount = pagecount; 738 739 for (i = 0; i < pagecount; i++) { 740 desc.range[i].len = pagebuffers[i].len; 741 desc.range[i].offset = pagebuffers[i].offset; 742 desc.range[i].pfn = pagebuffers[i].pfn; 743 } 744 745 bufferlist[0].iov_base = &desc; 746 bufferlist[0].iov_len = descsize; 747 bufferlist[1].iov_base = buffer; 748 bufferlist[1].iov_len = bufferlen; 749 bufferlist[2].iov_base = &aligned_data; 750 bufferlist[2].iov_len = (packetlen_aligned - packetlen); 751 752 return hv_ringbuffer_write(channel, bufferlist, 3); 753 } 754 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer_ctl); 755 756 /* 757 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer 758 * packets using a GPADL Direct packet type. 759 */ 760 int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel, 761 struct hv_page_buffer pagebuffers[], 762 u32 pagecount, void *buffer, u32 bufferlen, 763 u64 requestid) 764 { 765 u32 flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; 766 767 return vmbus_sendpacket_pagebuffer_ctl(channel, pagebuffers, pagecount, 768 buffer, bufferlen, 769 requestid, flags); 770 771 } 772 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer); 773 774 /* 775 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet 776 * using a GPADL Direct packet type. 777 * The buffer includes the vmbus descriptor. 778 */ 779 int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel, 780 struct vmbus_packet_mpb_array *desc, 781 u32 desc_size, 782 void *buffer, u32 bufferlen, u64 requestid) 783 { 784 u32 packetlen; 785 u32 packetlen_aligned; 786 struct kvec bufferlist[3]; 787 u64 aligned_data = 0; 788 789 packetlen = desc_size + bufferlen; 790 packetlen_aligned = ALIGN(packetlen, sizeof(u64)); 791 792 /* Setup the descriptor */ 793 desc->type = VM_PKT_DATA_USING_GPA_DIRECT; 794 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; 795 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes grandularity */ 796 desc->length8 = (u16)(packetlen_aligned >> 3); 797 desc->transactionid = requestid; 798 desc->rangecount = 1; 799 800 bufferlist[0].iov_base = desc; 801 bufferlist[0].iov_len = desc_size; 802 bufferlist[1].iov_base = buffer; 803 bufferlist[1].iov_len = bufferlen; 804 bufferlist[2].iov_base = &aligned_data; 805 bufferlist[2].iov_len = (packetlen_aligned - packetlen); 806 807 return hv_ringbuffer_write(channel, bufferlist, 3); 808 } 809 EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc); 810 811 /* 812 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet 813 * using a GPADL Direct packet type. 814 */ 815 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel, 816 struct hv_multipage_buffer *multi_pagebuffer, 817 void *buffer, u32 bufferlen, u64 requestid) 818 { 819 struct vmbus_channel_packet_multipage_buffer desc; 820 u32 descsize; 821 u32 packetlen; 822 u32 packetlen_aligned; 823 struct kvec bufferlist[3]; 824 u64 aligned_data = 0; 825 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset, 826 multi_pagebuffer->len); 827 828 if (pfncount > MAX_MULTIPAGE_BUFFER_COUNT) 829 return -EINVAL; 830 831 /* 832 * Adjust the size down since vmbus_channel_packet_multipage_buffer is 833 * the largest size we support 834 */ 835 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) - 836 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) * 837 sizeof(u64)); 838 packetlen = descsize + bufferlen; 839 packetlen_aligned = ALIGN(packetlen, sizeof(u64)); 840 841 842 /* Setup the descriptor */ 843 desc.type = VM_PKT_DATA_USING_GPA_DIRECT; 844 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; 845 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */ 846 desc.length8 = (u16)(packetlen_aligned >> 3); 847 desc.transactionid = requestid; 848 desc.rangecount = 1; 849 850 desc.range.len = multi_pagebuffer->len; 851 desc.range.offset = multi_pagebuffer->offset; 852 853 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array, 854 pfncount * sizeof(u64)); 855 856 bufferlist[0].iov_base = &desc; 857 bufferlist[0].iov_len = descsize; 858 bufferlist[1].iov_base = buffer; 859 bufferlist[1].iov_len = bufferlen; 860 bufferlist[2].iov_base = &aligned_data; 861 bufferlist[2].iov_len = (packetlen_aligned - packetlen); 862 863 return hv_ringbuffer_write(channel, bufferlist, 3); 864 } 865 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer); 866 867 /** 868 * vmbus_recvpacket() - Retrieve the user packet on the specified channel 869 * @channel: Pointer to vmbus_channel structure. 870 * @buffer: Pointer to the buffer you want to receive the data into. 871 * @bufferlen: Maximum size of what the the buffer will hold 872 * @buffer_actual_len: The actual size of the data after it was received 873 * @requestid: Identifier of the request 874 * 875 * Receives directly from the hyper-v vmbus and puts the data it received 876 * into Buffer. This will receive the data unparsed from hyper-v. 877 * 878 * Mainly used by Hyper-V drivers. 879 */ 880 static inline int 881 __vmbus_recvpacket(struct vmbus_channel *channel, void *buffer, 882 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid, 883 bool raw) 884 { 885 return hv_ringbuffer_read(channel, buffer, bufferlen, 886 buffer_actual_len, requestid, raw); 887 888 } 889 890 int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer, 891 u32 bufferlen, u32 *buffer_actual_len, 892 u64 *requestid) 893 { 894 return __vmbus_recvpacket(channel, buffer, bufferlen, 895 buffer_actual_len, requestid, false); 896 } 897 EXPORT_SYMBOL(vmbus_recvpacket); 898 899 /* 900 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel 901 */ 902 int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer, 903 u32 bufferlen, u32 *buffer_actual_len, 904 u64 *requestid) 905 { 906 return __vmbus_recvpacket(channel, buffer, bufferlen, 907 buffer_actual_len, requestid, true); 908 } 909 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw); 910