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