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 static 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) && 51 (!channel->low_latency)) { 52 /* Each u32 represents 32 channels */ 53 sync_set_bit(channel->offermsg.child_relid & 31, 54 (unsigned long *) vmbus_connection.send_int_page + 55 (channel->offermsg.child_relid >> 5)); 56 57 /* Get the child to parent monitor page */ 58 monitorpage = vmbus_connection.monitor_pages[1]; 59 60 sync_set_bit(channel->monitor_bit, 61 (unsigned long *)&monitorpage->trigger_group 62 [channel->monitor_grp].pending); 63 64 } else { 65 vmbus_set_event(channel); 66 } 67 } 68 69 /* 70 * vmbus_open - Open the specified channel. 71 */ 72 int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size, 73 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen, 74 void (*onchannelcallback)(void *context), void *context) 75 { 76 struct vmbus_channel_open_channel *open_msg; 77 struct vmbus_channel_msginfo *open_info = NULL; 78 unsigned long flags; 79 int ret, err = 0; 80 struct page *page; 81 82 if (send_ringbuffer_size % PAGE_SIZE || 83 recv_ringbuffer_size % PAGE_SIZE) 84 return -EINVAL; 85 86 spin_lock_irqsave(&newchannel->lock, flags); 87 if (newchannel->state == CHANNEL_OPEN_STATE) { 88 newchannel->state = CHANNEL_OPENING_STATE; 89 } else { 90 spin_unlock_irqrestore(&newchannel->lock, flags); 91 return -EINVAL; 92 } 93 spin_unlock_irqrestore(&newchannel->lock, flags); 94 95 newchannel->onchannel_callback = onchannelcallback; 96 newchannel->channel_callback_context = context; 97 98 /* Allocate the ring buffer */ 99 page = alloc_pages_node(cpu_to_node(newchannel->target_cpu), 100 GFP_KERNEL|__GFP_ZERO, 101 get_order(send_ringbuffer_size + 102 recv_ringbuffer_size)); 103 104 if (!page) 105 page = alloc_pages(GFP_KERNEL|__GFP_ZERO, 106 get_order(send_ringbuffer_size + 107 recv_ringbuffer_size)); 108 109 if (!page) { 110 err = -ENOMEM; 111 goto error_set_chnstate; 112 } 113 114 newchannel->ringbuffer_pages = page_address(page); 115 newchannel->ringbuffer_pagecount = (send_ringbuffer_size + 116 recv_ringbuffer_size) >> PAGE_SHIFT; 117 118 ret = hv_ringbuffer_init(&newchannel->outbound, page, 119 send_ringbuffer_size >> PAGE_SHIFT); 120 121 if (ret != 0) { 122 err = ret; 123 goto error_free_pages; 124 } 125 126 ret = hv_ringbuffer_init(&newchannel->inbound, 127 &page[send_ringbuffer_size >> PAGE_SHIFT], 128 recv_ringbuffer_size >> PAGE_SHIFT); 129 if (ret != 0) { 130 err = ret; 131 goto error_free_pages; 132 } 133 134 135 /* Establish the gpadl for the ring buffer */ 136 newchannel->ringbuffer_gpadlhandle = 0; 137 138 ret = vmbus_establish_gpadl(newchannel, 139 page_address(page), 140 send_ringbuffer_size + 141 recv_ringbuffer_size, 142 &newchannel->ringbuffer_gpadlhandle); 143 144 if (ret != 0) { 145 err = ret; 146 goto error_free_pages; 147 } 148 149 /* Create and init the channel open message */ 150 open_info = kmalloc(sizeof(*open_info) + 151 sizeof(struct vmbus_channel_open_channel), 152 GFP_KERNEL); 153 if (!open_info) { 154 err = -ENOMEM; 155 goto error_free_gpadl; 156 } 157 158 init_completion(&open_info->waitevent); 159 160 open_msg = (struct vmbus_channel_open_channel *)open_info->msg; 161 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL; 162 open_msg->openid = newchannel->offermsg.child_relid; 163 open_msg->child_relid = newchannel->offermsg.child_relid; 164 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle; 165 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >> 166 PAGE_SHIFT; 167 open_msg->target_vp = newchannel->target_vp; 168 169 if (userdatalen > MAX_USER_DEFINED_BYTES) { 170 err = -EINVAL; 171 goto error_free_gpadl; 172 } 173 174 if (userdatalen) 175 memcpy(open_msg->userdata, userdata, userdatalen); 176 177 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); 178 list_add_tail(&open_info->msglistentry, 179 &vmbus_connection.chn_msg_list); 180 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); 181 182 ret = vmbus_post_msg(open_msg, 183 sizeof(struct vmbus_channel_open_channel)); 184 185 if (ret != 0) { 186 err = ret; 187 goto error_clean_msglist; 188 } 189 190 wait_for_completion(&open_info->waitevent); 191 192 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); 193 list_del(&open_info->msglistentry); 194 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); 195 196 if (open_info->response.open_result.status) { 197 err = -EAGAIN; 198 goto error_free_gpadl; 199 } 200 201 newchannel->state = CHANNEL_OPENED_STATE; 202 kfree(open_info); 203 return 0; 204 205 error_clean_msglist: 206 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); 207 list_del(&open_info->msglistentry); 208 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); 209 210 error_free_gpadl: 211 vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle); 212 kfree(open_info); 213 error_free_pages: 214 hv_ringbuffer_cleanup(&newchannel->outbound); 215 hv_ringbuffer_cleanup(&newchannel->inbound); 216 __free_pages(page, 217 get_order(send_ringbuffer_size + recv_ringbuffer_size)); 218 error_set_chnstate: 219 newchannel->state = CHANNEL_OPEN_STATE; 220 return err; 221 } 222 EXPORT_SYMBOL_GPL(vmbus_open); 223 224 /* Used for Hyper-V Socket: a guest client's connect() to the host */ 225 int vmbus_send_tl_connect_request(const uuid_le *shv_guest_servie_id, 226 const uuid_le *shv_host_servie_id) 227 { 228 struct vmbus_channel_tl_connect_request conn_msg; 229 230 memset(&conn_msg, 0, sizeof(conn_msg)); 231 conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST; 232 conn_msg.guest_endpoint_id = *shv_guest_servie_id; 233 conn_msg.host_service_id = *shv_host_servie_id; 234 235 return vmbus_post_msg(&conn_msg, sizeof(conn_msg)); 236 } 237 EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request); 238 239 /* 240 * create_gpadl_header - Creates a gpadl for the specified buffer 241 */ 242 static int create_gpadl_header(void *kbuffer, u32 size, 243 struct vmbus_channel_msginfo **msginfo) 244 { 245 int i; 246 int pagecount; 247 struct vmbus_channel_gpadl_header *gpadl_header; 248 struct vmbus_channel_gpadl_body *gpadl_body; 249 struct vmbus_channel_msginfo *msgheader; 250 struct vmbus_channel_msginfo *msgbody = NULL; 251 u32 msgsize; 252 253 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize; 254 255 pagecount = size >> PAGE_SHIFT; 256 257 /* do we need a gpadl body msg */ 258 pfnsize = MAX_SIZE_CHANNEL_MESSAGE - 259 sizeof(struct vmbus_channel_gpadl_header) - 260 sizeof(struct gpa_range); 261 pfncount = pfnsize / sizeof(u64); 262 263 if (pagecount > pfncount) { 264 /* we need a gpadl body */ 265 /* fill in the header */ 266 msgsize = sizeof(struct vmbus_channel_msginfo) + 267 sizeof(struct vmbus_channel_gpadl_header) + 268 sizeof(struct gpa_range) + pfncount * sizeof(u64); 269 msgheader = kzalloc(msgsize, GFP_KERNEL); 270 if (!msgheader) 271 goto nomem; 272 273 INIT_LIST_HEAD(&msgheader->submsglist); 274 msgheader->msgsize = msgsize; 275 276 gpadl_header = (struct vmbus_channel_gpadl_header *) 277 msgheader->msg; 278 gpadl_header->rangecount = 1; 279 gpadl_header->range_buflen = sizeof(struct gpa_range) + 280 pagecount * sizeof(u64); 281 gpadl_header->range[0].byte_offset = 0; 282 gpadl_header->range[0].byte_count = size; 283 for (i = 0; i < pfncount; i++) 284 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys( 285 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT; 286 *msginfo = msgheader; 287 288 pfnsum = pfncount; 289 pfnleft = pagecount - pfncount; 290 291 /* how many pfns can we fit */ 292 pfnsize = MAX_SIZE_CHANNEL_MESSAGE - 293 sizeof(struct vmbus_channel_gpadl_body); 294 pfncount = pfnsize / sizeof(u64); 295 296 /* fill in the body */ 297 while (pfnleft) { 298 if (pfnleft > pfncount) 299 pfncurr = pfncount; 300 else 301 pfncurr = pfnleft; 302 303 msgsize = sizeof(struct vmbus_channel_msginfo) + 304 sizeof(struct vmbus_channel_gpadl_body) + 305 pfncurr * sizeof(u64); 306 msgbody = kzalloc(msgsize, GFP_KERNEL); 307 308 if (!msgbody) { 309 struct vmbus_channel_msginfo *pos = NULL; 310 struct vmbus_channel_msginfo *tmp = NULL; 311 /* 312 * Free up all the allocated messages. 313 */ 314 list_for_each_entry_safe(pos, tmp, 315 &msgheader->submsglist, 316 msglistentry) { 317 318 list_del(&pos->msglistentry); 319 kfree(pos); 320 } 321 322 goto nomem; 323 } 324 325 msgbody->msgsize = msgsize; 326 gpadl_body = 327 (struct vmbus_channel_gpadl_body *)msgbody->msg; 328 329 /* 330 * Gpadl is u32 and we are using a pointer which could 331 * be 64-bit 332 * This is governed by the guest/host protocol and 333 * so the hypervisor gurantees that this is ok. 334 */ 335 for (i = 0; i < pfncurr; i++) 336 gpadl_body->pfn[i] = slow_virt_to_phys( 337 kbuffer + PAGE_SIZE * (pfnsum + i)) >> 338 PAGE_SHIFT; 339 340 /* add to msg header */ 341 list_add_tail(&msgbody->msglistentry, 342 &msgheader->submsglist); 343 pfnsum += pfncurr; 344 pfnleft -= pfncurr; 345 } 346 } else { 347 /* everything fits in a header */ 348 msgsize = sizeof(struct vmbus_channel_msginfo) + 349 sizeof(struct vmbus_channel_gpadl_header) + 350 sizeof(struct gpa_range) + pagecount * sizeof(u64); 351 msgheader = kzalloc(msgsize, GFP_KERNEL); 352 if (msgheader == NULL) 353 goto nomem; 354 355 INIT_LIST_HEAD(&msgheader->submsglist); 356 msgheader->msgsize = msgsize; 357 358 gpadl_header = (struct vmbus_channel_gpadl_header *) 359 msgheader->msg; 360 gpadl_header->rangecount = 1; 361 gpadl_header->range_buflen = sizeof(struct gpa_range) + 362 pagecount * sizeof(u64); 363 gpadl_header->range[0].byte_offset = 0; 364 gpadl_header->range[0].byte_count = size; 365 for (i = 0; i < pagecount; i++) 366 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys( 367 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT; 368 369 *msginfo = msgheader; 370 } 371 372 return 0; 373 nomem: 374 kfree(msgheader); 375 kfree(msgbody); 376 return -ENOMEM; 377 } 378 379 /* 380 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer 381 * 382 * @channel: a channel 383 * @kbuffer: from kmalloc or vmalloc 384 * @size: page-size multiple 385 * @gpadl_handle: some funky thing 386 */ 387 int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer, 388 u32 size, u32 *gpadl_handle) 389 { 390 struct vmbus_channel_gpadl_header *gpadlmsg; 391 struct vmbus_channel_gpadl_body *gpadl_body; 392 struct vmbus_channel_msginfo *msginfo = NULL; 393 struct vmbus_channel_msginfo *submsginfo, *tmp; 394 struct list_head *curr; 395 u32 next_gpadl_handle; 396 unsigned long flags; 397 int ret = 0; 398 399 next_gpadl_handle = 400 (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1); 401 402 ret = create_gpadl_header(kbuffer, size, &msginfo); 403 if (ret) 404 return ret; 405 406 init_completion(&msginfo->waitevent); 407 408 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg; 409 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER; 410 gpadlmsg->child_relid = channel->offermsg.child_relid; 411 gpadlmsg->gpadl = next_gpadl_handle; 412 413 414 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); 415 list_add_tail(&msginfo->msglistentry, 416 &vmbus_connection.chn_msg_list); 417 418 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); 419 420 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize - 421 sizeof(*msginfo)); 422 if (ret != 0) 423 goto cleanup; 424 425 list_for_each(curr, &msginfo->submsglist) { 426 submsginfo = (struct vmbus_channel_msginfo *)curr; 427 gpadl_body = 428 (struct vmbus_channel_gpadl_body *)submsginfo->msg; 429 430 gpadl_body->header.msgtype = 431 CHANNELMSG_GPADL_BODY; 432 gpadl_body->gpadl = next_gpadl_handle; 433 434 ret = vmbus_post_msg(gpadl_body, 435 submsginfo->msgsize - 436 sizeof(*submsginfo)); 437 if (ret != 0) 438 goto cleanup; 439 440 } 441 wait_for_completion(&msginfo->waitevent); 442 443 /* At this point, we received the gpadl created msg */ 444 *gpadl_handle = gpadlmsg->gpadl; 445 446 cleanup: 447 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); 448 list_del(&msginfo->msglistentry); 449 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); 450 list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist, 451 msglistentry) { 452 kfree(submsginfo); 453 } 454 455 kfree(msginfo); 456 return ret; 457 } 458 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl); 459 460 /* 461 * vmbus_teardown_gpadl -Teardown the specified GPADL handle 462 */ 463 int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle) 464 { 465 struct vmbus_channel_gpadl_teardown *msg; 466 struct vmbus_channel_msginfo *info; 467 unsigned long flags; 468 int ret; 469 470 info = kmalloc(sizeof(*info) + 471 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL); 472 if (!info) 473 return -ENOMEM; 474 475 init_completion(&info->waitevent); 476 477 msg = (struct vmbus_channel_gpadl_teardown *)info->msg; 478 479 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN; 480 msg->child_relid = channel->offermsg.child_relid; 481 msg->gpadl = gpadl_handle; 482 483 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); 484 list_add_tail(&info->msglistentry, 485 &vmbus_connection.chn_msg_list); 486 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); 487 ret = vmbus_post_msg(msg, 488 sizeof(struct vmbus_channel_gpadl_teardown)); 489 490 if (ret) 491 goto post_msg_err; 492 493 wait_for_completion(&info->waitevent); 494 495 post_msg_err: 496 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); 497 list_del(&info->msglistentry); 498 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); 499 500 kfree(info); 501 return ret; 502 } 503 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl); 504 505 static void reset_channel_cb(void *arg) 506 { 507 struct vmbus_channel *channel = arg; 508 509 channel->onchannel_callback = NULL; 510 } 511 512 static int vmbus_close_internal(struct vmbus_channel *channel) 513 { 514 struct vmbus_channel_close_channel *msg; 515 int ret; 516 517 /* 518 * process_chn_event(), running in the tasklet, can race 519 * with vmbus_close_internal() in the case of SMP guest, e.g., when 520 * the former is accessing channel->inbound.ring_buffer, the latter 521 * could be freeing the ring_buffer pages. 522 * 523 * To resolve the race, we can serialize them by disabling the 524 * tasklet when the latter is running here. 525 */ 526 hv_event_tasklet_disable(channel); 527 528 /* 529 * In case a device driver's probe() fails (e.g., 530 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is 531 * rescinded later (e.g., we dynamically disble an Integrated Service 532 * in Hyper-V Manager), the driver's remove() invokes vmbus_close(): 533 * here we should skip most of the below cleanup work. 534 */ 535 if (channel->state != CHANNEL_OPENED_STATE) { 536 ret = -EINVAL; 537 goto out; 538 } 539 540 channel->state = CHANNEL_OPEN_STATE; 541 channel->sc_creation_callback = NULL; 542 /* Stop callback and cancel the timer asap */ 543 if (channel->target_cpu != get_cpu()) { 544 put_cpu(); 545 smp_call_function_single(channel->target_cpu, reset_channel_cb, 546 channel, true); 547 } else { 548 reset_channel_cb(channel); 549 put_cpu(); 550 } 551 552 /* Send a closing message */ 553 554 msg = &channel->close_msg.msg; 555 556 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL; 557 msg->child_relid = channel->offermsg.child_relid; 558 559 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel)); 560 561 if (ret) { 562 pr_err("Close failed: close post msg return is %d\n", ret); 563 /* 564 * If we failed to post the close msg, 565 * it is perhaps better to leak memory. 566 */ 567 goto out; 568 } 569 570 /* Tear down the gpadl for the channel's ring buffer */ 571 if (channel->ringbuffer_gpadlhandle) { 572 ret = vmbus_teardown_gpadl(channel, 573 channel->ringbuffer_gpadlhandle); 574 if (ret) { 575 pr_err("Close failed: teardown gpadl return %d\n", ret); 576 /* 577 * If we failed to teardown gpadl, 578 * it is perhaps better to leak memory. 579 */ 580 goto out; 581 } 582 } 583 584 /* Cleanup the ring buffers for this channel */ 585 hv_ringbuffer_cleanup(&channel->outbound); 586 hv_ringbuffer_cleanup(&channel->inbound); 587 588 free_pages((unsigned long)channel->ringbuffer_pages, 589 get_order(channel->ringbuffer_pagecount * PAGE_SIZE)); 590 591 out: 592 hv_event_tasklet_enable(channel); 593 594 return ret; 595 } 596 597 /* 598 * vmbus_close - Close the specified channel 599 */ 600 void vmbus_close(struct vmbus_channel *channel) 601 { 602 struct list_head *cur, *tmp; 603 struct vmbus_channel *cur_channel; 604 605 if (channel->primary_channel != NULL) { 606 /* 607 * We will only close sub-channels when 608 * the primary is closed. 609 */ 610 return; 611 } 612 /* 613 * Close all the sub-channels first and then close the 614 * primary channel. 615 */ 616 list_for_each_safe(cur, tmp, &channel->sc_list) { 617 cur_channel = list_entry(cur, struct vmbus_channel, sc_list); 618 if (cur_channel->state != CHANNEL_OPENED_STATE) 619 continue; 620 vmbus_close_internal(cur_channel); 621 } 622 /* 623 * Now close the primary. 624 */ 625 vmbus_close_internal(channel); 626 } 627 EXPORT_SYMBOL_GPL(vmbus_close); 628 629 int vmbus_sendpacket_ctl(struct vmbus_channel *channel, void *buffer, 630 u32 bufferlen, u64 requestid, 631 enum vmbus_packet_type type, u32 flags, bool kick_q) 632 { 633 struct vmpacket_descriptor desc; 634 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen; 635 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64)); 636 struct kvec bufferlist[3]; 637 u64 aligned_data = 0; 638 int ret; 639 bool signal = false; 640 bool lock = channel->acquire_ring_lock; 641 int num_vecs = ((bufferlen != 0) ? 3 : 1); 642 643 644 /* Setup the descriptor */ 645 desc.type = type; /* VmbusPacketTypeDataInBand; */ 646 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */ 647 /* in 8-bytes granularity */ 648 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3; 649 desc.len8 = (u16)(packetlen_aligned >> 3); 650 desc.trans_id = requestid; 651 652 bufferlist[0].iov_base = &desc; 653 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor); 654 bufferlist[1].iov_base = buffer; 655 bufferlist[1].iov_len = bufferlen; 656 bufferlist[2].iov_base = &aligned_data; 657 bufferlist[2].iov_len = (packetlen_aligned - packetlen); 658 659 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, num_vecs, 660 &signal, lock, channel->signal_policy); 661 662 /* 663 * Signalling the host is conditional on many factors: 664 * 1. The ring state changed from being empty to non-empty. 665 * This is tracked by the variable "signal". 666 * 2. The variable kick_q tracks if more data will be placed 667 * on the ring. We will not signal if more data is 668 * to be placed. 669 * 670 * Based on the channel signal state, we will decide 671 * which signaling policy will be applied. 672 * 673 * If we cannot write to the ring-buffer; signal the host 674 * even if we may not have written anything. This is a rare 675 * enough condition that it should not matter. 676 * NOTE: in this case, the hvsock channel is an exception, because 677 * it looks the host side's hvsock implementation has a throttling 678 * mechanism which can hurt the performance otherwise. 679 */ 680 681 if (((ret == 0) && kick_q && signal) || 682 (ret && !is_hvsock_channel(channel))) 683 vmbus_setevent(channel); 684 685 return ret; 686 } 687 EXPORT_SYMBOL(vmbus_sendpacket_ctl); 688 689 /** 690 * vmbus_sendpacket() - Send the specified buffer on the given channel 691 * @channel: Pointer to vmbus_channel structure. 692 * @buffer: Pointer to the buffer you want to receive the data into. 693 * @bufferlen: Maximum size of what the the buffer will hold 694 * @requestid: Identifier of the request 695 * @type: Type of packet that is being send e.g. negotiate, time 696 * packet etc. 697 * 698 * Sends data in @buffer directly to hyper-v via the vmbus 699 * This will send the data unparsed to hyper-v. 700 * 701 * Mainly used by Hyper-V drivers. 702 */ 703 int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer, 704 u32 bufferlen, u64 requestid, 705 enum vmbus_packet_type type, u32 flags) 706 { 707 return vmbus_sendpacket_ctl(channel, buffer, bufferlen, requestid, 708 type, flags, true); 709 } 710 EXPORT_SYMBOL(vmbus_sendpacket); 711 712 /* 713 * vmbus_sendpacket_pagebuffer_ctl - Send a range of single-page buffer 714 * packets using a GPADL Direct packet type. This interface allows you 715 * to control notifying the host. This will be useful for sending 716 * batched data. Also the sender can control the send flags 717 * explicitly. 718 */ 719 int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel *channel, 720 struct hv_page_buffer pagebuffers[], 721 u32 pagecount, void *buffer, u32 bufferlen, 722 u64 requestid, 723 u32 flags, 724 bool kick_q) 725 { 726 int ret; 727 int i; 728 struct vmbus_channel_packet_page_buffer desc; 729 u32 descsize; 730 u32 packetlen; 731 u32 packetlen_aligned; 732 struct kvec bufferlist[3]; 733 u64 aligned_data = 0; 734 bool signal = false; 735 bool lock = channel->acquire_ring_lock; 736 737 if (pagecount > MAX_PAGE_BUFFER_COUNT) 738 return -EINVAL; 739 740 741 /* 742 * Adjust the size down since vmbus_channel_packet_page_buffer is the 743 * largest size we support 744 */ 745 descsize = sizeof(struct vmbus_channel_packet_page_buffer) - 746 ((MAX_PAGE_BUFFER_COUNT - pagecount) * 747 sizeof(struct hv_page_buffer)); 748 packetlen = descsize + bufferlen; 749 packetlen_aligned = ALIGN(packetlen, sizeof(u64)); 750 751 /* Setup the descriptor */ 752 desc.type = VM_PKT_DATA_USING_GPA_DIRECT; 753 desc.flags = flags; 754 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */ 755 desc.length8 = (u16)(packetlen_aligned >> 3); 756 desc.transactionid = requestid; 757 desc.rangecount = pagecount; 758 759 for (i = 0; i < pagecount; i++) { 760 desc.range[i].len = pagebuffers[i].len; 761 desc.range[i].offset = pagebuffers[i].offset; 762 desc.range[i].pfn = pagebuffers[i].pfn; 763 } 764 765 bufferlist[0].iov_base = &desc; 766 bufferlist[0].iov_len = descsize; 767 bufferlist[1].iov_base = buffer; 768 bufferlist[1].iov_len = bufferlen; 769 bufferlist[2].iov_base = &aligned_data; 770 bufferlist[2].iov_len = (packetlen_aligned - packetlen); 771 772 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, 773 &signal, lock, channel->signal_policy); 774 775 /* 776 * Signalling the host is conditional on many factors: 777 * 1. The ring state changed from being empty to non-empty. 778 * This is tracked by the variable "signal". 779 * 2. The variable kick_q tracks if more data will be placed 780 * on the ring. We will not signal if more data is 781 * to be placed. 782 * 783 * Based on the channel signal state, we will decide 784 * which signaling policy will be applied. 785 * 786 * If we cannot write to the ring-buffer; signal the host 787 * even if we may not have written anything. This is a rare 788 * enough condition that it should not matter. 789 */ 790 791 if (((ret == 0) && kick_q && signal) || (ret)) 792 vmbus_setevent(channel); 793 794 return ret; 795 } 796 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer_ctl); 797 798 /* 799 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer 800 * packets using a GPADL Direct packet type. 801 */ 802 int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel, 803 struct hv_page_buffer pagebuffers[], 804 u32 pagecount, void *buffer, u32 bufferlen, 805 u64 requestid) 806 { 807 u32 flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; 808 return vmbus_sendpacket_pagebuffer_ctl(channel, pagebuffers, pagecount, 809 buffer, bufferlen, requestid, 810 flags, true); 811 812 } 813 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer); 814 815 /* 816 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet 817 * using a GPADL Direct packet type. 818 * The buffer includes the vmbus descriptor. 819 */ 820 int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel, 821 struct vmbus_packet_mpb_array *desc, 822 u32 desc_size, 823 void *buffer, u32 bufferlen, u64 requestid) 824 { 825 int ret; 826 u32 packetlen; 827 u32 packetlen_aligned; 828 struct kvec bufferlist[3]; 829 u64 aligned_data = 0; 830 bool signal = false; 831 bool lock = channel->acquire_ring_lock; 832 833 packetlen = desc_size + bufferlen; 834 packetlen_aligned = ALIGN(packetlen, sizeof(u64)); 835 836 /* Setup the descriptor */ 837 desc->type = VM_PKT_DATA_USING_GPA_DIRECT; 838 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; 839 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes grandularity */ 840 desc->length8 = (u16)(packetlen_aligned >> 3); 841 desc->transactionid = requestid; 842 desc->rangecount = 1; 843 844 bufferlist[0].iov_base = desc; 845 bufferlist[0].iov_len = desc_size; 846 bufferlist[1].iov_base = buffer; 847 bufferlist[1].iov_len = bufferlen; 848 bufferlist[2].iov_base = &aligned_data; 849 bufferlist[2].iov_len = (packetlen_aligned - packetlen); 850 851 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, 852 &signal, lock, channel->signal_policy); 853 854 if (ret == 0 && signal) 855 vmbus_setevent(channel); 856 857 return ret; 858 } 859 EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc); 860 861 /* 862 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet 863 * using a GPADL Direct packet type. 864 */ 865 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel, 866 struct hv_multipage_buffer *multi_pagebuffer, 867 void *buffer, u32 bufferlen, u64 requestid) 868 { 869 int ret; 870 struct vmbus_channel_packet_multipage_buffer desc; 871 u32 descsize; 872 u32 packetlen; 873 u32 packetlen_aligned; 874 struct kvec bufferlist[3]; 875 u64 aligned_data = 0; 876 bool signal = false; 877 bool lock = channel->acquire_ring_lock; 878 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset, 879 multi_pagebuffer->len); 880 881 if (pfncount > MAX_MULTIPAGE_BUFFER_COUNT) 882 return -EINVAL; 883 884 /* 885 * Adjust the size down since vmbus_channel_packet_multipage_buffer is 886 * the largest size we support 887 */ 888 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) - 889 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) * 890 sizeof(u64)); 891 packetlen = descsize + bufferlen; 892 packetlen_aligned = ALIGN(packetlen, sizeof(u64)); 893 894 895 /* Setup the descriptor */ 896 desc.type = VM_PKT_DATA_USING_GPA_DIRECT; 897 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; 898 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */ 899 desc.length8 = (u16)(packetlen_aligned >> 3); 900 desc.transactionid = requestid; 901 desc.rangecount = 1; 902 903 desc.range.len = multi_pagebuffer->len; 904 desc.range.offset = multi_pagebuffer->offset; 905 906 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array, 907 pfncount * sizeof(u64)); 908 909 bufferlist[0].iov_base = &desc; 910 bufferlist[0].iov_len = descsize; 911 bufferlist[1].iov_base = buffer; 912 bufferlist[1].iov_len = bufferlen; 913 bufferlist[2].iov_base = &aligned_data; 914 bufferlist[2].iov_len = (packetlen_aligned - packetlen); 915 916 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3, 917 &signal, lock, channel->signal_policy); 918 919 if (ret == 0 && signal) 920 vmbus_setevent(channel); 921 922 return ret; 923 } 924 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer); 925 926 /** 927 * vmbus_recvpacket() - Retrieve the user packet on the specified channel 928 * @channel: Pointer to vmbus_channel structure. 929 * @buffer: Pointer to the buffer you want to receive the data into. 930 * @bufferlen: Maximum size of what the the buffer will hold 931 * @buffer_actual_len: The actual size of the data after it was received 932 * @requestid: Identifier of the request 933 * 934 * Receives directly from the hyper-v vmbus and puts the data it received 935 * into Buffer. This will receive the data unparsed from hyper-v. 936 * 937 * Mainly used by Hyper-V drivers. 938 */ 939 static inline int 940 __vmbus_recvpacket(struct vmbus_channel *channel, void *buffer, 941 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid, 942 bool raw) 943 { 944 int ret; 945 bool signal = false; 946 947 ret = hv_ringbuffer_read(&channel->inbound, buffer, bufferlen, 948 buffer_actual_len, requestid, &signal, raw); 949 950 if (signal) 951 vmbus_setevent(channel); 952 953 return ret; 954 } 955 956 int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer, 957 u32 bufferlen, u32 *buffer_actual_len, 958 u64 *requestid) 959 { 960 return __vmbus_recvpacket(channel, buffer, bufferlen, 961 buffer_actual_len, requestid, false); 962 } 963 EXPORT_SYMBOL(vmbus_recvpacket); 964 965 /* 966 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel 967 */ 968 int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer, 969 u32 bufferlen, u32 *buffer_actual_len, 970 u64 *requestid) 971 { 972 return __vmbus_recvpacket(channel, buffer, bufferlen, 973 buffer_actual_len, requestid, true); 974 } 975 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw); 976