blob: 8dc48f53c1ac4f06060a0f6cd8d074bb3ca76270 [file] [log] [blame]
Thomas Gleixner3b20eb22019-05-29 16:57:35 -07001// SPDX-License-Identifier: GPL-2.0-only
Hank Janssen3e7ee492009-07-13 16:02:34 -07002/*
3 *
4 * Copyright (c) 2009, Microsoft Corporation.
5 *
Hank Janssen3e7ee492009-07-13 16:02:34 -07006 * Authors:
7 * Haiyang Zhang <haiyangz@microsoft.com>
8 * Hank Janssen <hjanssen@microsoft.com>
Hank Janssen3e7ee492009-07-13 16:02:34 -07009 */
Hank Janssen0a466182011-03-29 13:58:47 -070010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Greg Kroah-Hartmana0086dc2009-08-17 17:22:08 -070012#include <linux/kernel.h>
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -080013#include <linux/sched.h>
14#include <linux/wait.h>
K. Y. Srinivasan5289d3d2011-08-25 09:49:01 -070015#include <linux/delay.h>
Greg Kroah-Hartmana0086dc2009-08-17 17:22:08 -070016#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Greg Kroah-Hartmana0086dc2009-08-17 17:22:08 -070018#include <linux/vmalloc.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070019#include <linux/hyperv.h>
K. Y. Srinivasan37f72782012-12-01 06:46:41 -080020#include <linux/export.h>
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +020021#include <asm/mshyperv.h>
22
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070023#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070024
Hank Janssen3e7ee492009-07-13 16:02:34 -070025
Haiyang Zhangda9fcb72011-01-26 12:12:14 -080026struct vmbus_connection vmbus_connection = {
27 .conn_state = DISCONNECTED,
28 .next_gpadl_handle = ATOMIC_INIT(0xE1E10),
Dexuan Cuib307b382019-09-05 23:01:21 +000029
30 .ready_for_suspend_event= COMPLETION_INITIALIZER(
31 vmbus_connection.ready_for_suspend_event),
Dexuan Cuid8bd2d42019-09-05 23:01:22 +000032 .ready_for_resume_event = COMPLETION_INITIALIZER(
33 vmbus_connection.ready_for_resume_event),
Hank Janssen3e7ee492009-07-13 16:02:34 -070034};
Stephen Hemminger95096f22016-12-03 12:34:40 -080035EXPORT_SYMBOL_GPL(vmbus_connection);
Hank Janssen3e7ee492009-07-13 16:02:34 -070036
Hank Janssen3e189512010-03-04 22:11:00 +000037/*
K. Y. Srinivasan37f72782012-12-01 06:46:41 -080038 * Negotiated protocol version with the host.
39 */
40__u32 vmbus_proto_version;
41EXPORT_SYMBOL_GPL(vmbus_proto_version);
42
Andrea Parribedc61a2019-10-15 13:46:44 +020043/*
44 * Table of VMBus versions listed from newest to oldest.
45 */
46static __u32 vmbus_versions[] = {
47 VERSION_WIN10_V5,
48 VERSION_WIN10,
49 VERSION_WIN8_1,
50 VERSION_WIN8,
51 VERSION_WIN7,
52 VERSION_WS2008
53};
K. Y. Srinivasan610071c2012-12-01 06:46:38 -080054
Dexuan Cuif53335e2019-09-05 23:01:19 +000055int vmbus_negotiate_version(struct vmbus_channel_msginfo *msginfo, u32 version)
K. Y. Srinivasan610071c2012-12-01 06:46:38 -080056{
57 int ret = 0;
Dexuan Cui41e270f2018-09-17 04:14:54 +000058 unsigned int cur_cpu;
K. Y. Srinivasan610071c2012-12-01 06:46:38 -080059 struct vmbus_channel_initiate_contact *msg;
60 unsigned long flags;
K. Y. Srinivasan610071c2012-12-01 06:46:38 -080061
62 init_completion(&msginfo->waitevent);
63
64 msg = (struct vmbus_channel_initiate_contact *)msginfo->msg;
65
Dexuan Cuiae20b252018-05-12 02:30:33 -070066 memset(msg, 0, sizeof(*msg));
K. Y. Srinivasan610071c2012-12-01 06:46:38 -080067 msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
68 msg->vmbus_version_requested = version;
Dexuan Cuiae20b252018-05-12 02:30:33 -070069
70 /*
71 * VMBus protocol 5.0 (VERSION_WIN10_V5) requires that we must use
72 * VMBUS_MESSAGE_CONNECTION_ID_4 for the Initiate Contact Message,
73 * and for subsequent messages, we must use the Message Connection ID
74 * field in the host-returned Version Response Message. And, with
75 * VERSION_WIN10_V5, we don't use msg->interrupt_page, but we tell
76 * the host explicitly that we still use VMBUS_MESSAGE_SINT(2) for
77 * compatibility.
78 *
79 * On old hosts, we should always use VMBUS_MESSAGE_CONNECTION_ID (1).
80 */
81 if (version >= VERSION_WIN10_V5) {
82 msg->msg_sint = VMBUS_MESSAGE_SINT;
83 vmbus_connection.msg_conn_id = VMBUS_MESSAGE_CONNECTION_ID_4;
84 } else {
85 msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
86 vmbus_connection.msg_conn_id = VMBUS_MESSAGE_CONNECTION_ID;
87 }
88
Greg Kroah-Hartman8681db42013-09-13 11:32:55 -070089 msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages[0]);
90 msg->monitor_page2 = virt_to_phys(vmbus_connection.monitor_pages[1]);
K. Y. Srinivasanb282e4c2015-12-14 16:01:56 -080091 /*
92 * We want all channel messages to be delivered on CPU 0.
93 * This has been the behavior pre-win8. This is not
94 * perf issue and having all channel messages delivered on CPU 0
95 * would be ok.
Alex Ng72686442016-02-26 15:13:22 -080096 * For post win8 hosts, we support receiving channel messagges on
97 * all the CPUs. This is needed for kexec to work correctly where
98 * the CPU attempting to connect may not be CPU 0.
K. Y. Srinivasanb282e4c2015-12-14 16:01:56 -080099 */
K. Y. Srinivasan54a662652017-04-30 16:21:18 -0700100 if (version >= VERSION_WIN8_1) {
Dexuan Cui41e270f2018-09-17 04:14:54 +0000101 cur_cpu = get_cpu();
102 msg->target_vcpu = hv_cpu_number_to_vp_number(cur_cpu);
103 vmbus_connection.connect_cpu = cur_cpu;
104 put_cpu();
K. Y. Srinivasan54a662652017-04-30 16:21:18 -0700105 } else {
Alex Ng72686442016-02-26 15:13:22 -0800106 msg->target_vcpu = 0;
K. Y. Srinivasan54a662652017-04-30 16:21:18 -0700107 vmbus_connection.connect_cpu = 0;
108 }
K. Y. Srinivasan610071c2012-12-01 06:46:38 -0800109
110 /*
111 * Add to list before we send the request since we may
112 * receive the response before returning from this routine
113 */
114 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
115 list_add_tail(&msginfo->msglistentry,
116 &vmbus_connection.chn_msg_list);
117
118 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
119
120 ret = vmbus_post_msg(msg,
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800121 sizeof(struct vmbus_channel_initiate_contact),
122 true);
Vitaly Kuznetsov034ebf52017-10-29 12:21:13 -0700123
124 trace_vmbus_negotiate_version(msg, ret);
125
K. Y. Srinivasan610071c2012-12-01 06:46:38 -0800126 if (ret != 0) {
127 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
128 list_del(&msginfo->msglistentry);
129 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
130 flags);
131 return ret;
132 }
133
134 /* Wait for the connection response */
K. Y. Srinivasan269f9792014-01-16 11:59:58 -0800135 wait_for_completion(&msginfo->waitevent);
K. Y. Srinivasan610071c2012-12-01 06:46:38 -0800136
137 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
138 list_del(&msginfo->msglistentry);
139 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
140
141 /* Check if successful */
142 if (msginfo->response.version_response.version_supported) {
143 vmbus_connection.conn_state = CONNECTED;
Dexuan Cuiae20b252018-05-12 02:30:33 -0700144
145 if (version >= VERSION_WIN10_V5)
146 vmbus_connection.msg_conn_id =
147 msginfo->response.version_response.msg_conn_id;
K. Y. Srinivasan610071c2012-12-01 06:46:38 -0800148 } else {
K. Y. Srinivasan610071c2012-12-01 06:46:38 -0800149 return -ECONNREFUSED;
150 }
151
152 return ret;
153}
154
155/*
Haiyang Zhangc6977672011-01-26 12:12:08 -0800156 * vmbus_connect - Sends a connect request on the partition service connection
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700157 */
Haiyang Zhangc6977672011-01-26 12:12:08 -0800158int vmbus_connect(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700159{
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800160 struct vmbus_channel_msginfo *msginfo = NULL;
Andrea Parribedc61a2019-10-15 13:46:44 +0200161 int i, ret = 0;
K. Y. Srinivasan610071c2012-12-01 06:46:38 -0800162 __u32 version;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700163
Bill Pemberton454f18a2009-07-27 16:47:24 -0400164 /* Initialize the vmbus connection */
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800165 vmbus_connection.conn_state = CONNECTING;
166 vmbus_connection.work_queue = create_workqueue("hv_vmbus_con");
167 if (!vmbus_connection.work_queue) {
K. Y. Srinivasan3a7546d2011-06-06 15:50:10 -0700168 ret = -ENOMEM;
K. Y. Srinivasanb0043862011-05-10 07:55:44 -0700169 goto cleanup;
Bill Pembertonde65a382009-07-29 17:00:09 -0400170 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700171
Dexuan Cui37c25782018-12-03 00:54:35 +0000172 vmbus_connection.handle_primary_chan_wq =
173 create_workqueue("hv_pri_chan");
174 if (!vmbus_connection.handle_primary_chan_wq) {
175 ret = -ENOMEM;
176 goto cleanup;
177 }
178
179 vmbus_connection.handle_sub_chan_wq =
180 create_workqueue("hv_sub_chan");
181 if (!vmbus_connection.handle_sub_chan_wq) {
182 ret = -ENOMEM;
183 goto cleanup;
184 }
185
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800186 INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800187 spin_lock_init(&vmbus_connection.channelmsg_lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700188
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800189 INIT_LIST_HEAD(&vmbus_connection.chn_list);
Dexuan Cuid6f591e2015-12-14 16:01:51 -0800190 mutex_init(&vmbus_connection.channel_mutex);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700191
Bill Pemberton454f18a2009-07-27 16:47:24 -0400192 /*
193 * Setup the vmbus event connection for channel interrupt
194 * abstraction stuff
195 */
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800196 vmbus_connection.int_page =
197 (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, 0);
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800198 if (vmbus_connection.int_page == NULL) {
K. Y. Srinivasan3a7546d2011-06-06 15:50:10 -0700199 ret = -ENOMEM;
K. Y. Srinivasanb0043862011-05-10 07:55:44 -0700200 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700201 }
202
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800203 vmbus_connection.recv_int_page = vmbus_connection.int_page;
204 vmbus_connection.send_int_page =
205 (void *)((unsigned long)vmbus_connection.int_page +
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700206 (PAGE_SIZE >> 1));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700207
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700208 /*
209 * Setup the monitor notification facility. The 1st page for
210 * parent->child and the 2nd page for child->parent
Bill Pemberton454f18a2009-07-27 16:47:24 -0400211 */
Greg Kroah-Hartman8681db42013-09-13 11:32:55 -0700212 vmbus_connection.monitor_pages[0] = (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 0);
213 vmbus_connection.monitor_pages[1] = (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 0);
214 if ((vmbus_connection.monitor_pages[0] == NULL) ||
215 (vmbus_connection.monitor_pages[1] == NULL)) {
K. Y. Srinivasan3a7546d2011-06-06 15:50:10 -0700216 ret = -ENOMEM;
K. Y. Srinivasanb0043862011-05-10 07:55:44 -0700217 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700218 }
219
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800220 msginfo = kzalloc(sizeof(*msginfo) +
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700221 sizeof(struct vmbus_channel_initiate_contact),
222 GFP_KERNEL);
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800223 if (msginfo == NULL) {
Bill Pemberton8cad0af2010-05-05 15:27:32 -0400224 ret = -ENOMEM;
K. Y. Srinivasanb0043862011-05-10 07:55:44 -0700225 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700226 }
227
Bill Pemberton454f18a2009-07-27 16:47:24 -0400228 /*
K. Y. Srinivasan610071c2012-12-01 06:46:38 -0800229 * Negotiate a compatible VMBUS version number with the
230 * host. We start with the highest number we can support
231 * and work our way down until we negotiate a compatible
232 * version.
Bill Pemberton454f18a2009-07-27 16:47:24 -0400233 */
Bill Pemberton53af5452009-09-11 21:46:44 -0400234
Andrea Parribedc61a2019-10-15 13:46:44 +0200235 for (i = 0; ; i++) {
236 if (i == ARRAY_SIZE(vmbus_versions))
237 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700238
Andrea Parribedc61a2019-10-15 13:46:44 +0200239 version = vmbus_versions[i];
240
K. Y. Srinivasan610071c2012-12-01 06:46:38 -0800241 ret = vmbus_negotiate_version(msginfo, version);
K. Y. Srinivasan8bbf9f42013-09-04 15:41:58 -0700242 if (ret == -ETIMEDOUT)
K. Y. Srinivasan666b9ad2013-08-28 14:56:54 -0700243 goto cleanup;
244
245 if (vmbus_connection.conn_state == CONNECTED)
K. Y. Srinivasan610071c2012-12-01 06:46:38 -0800246 break;
Andrea Parribedc61a2019-10-15 13:46:44 +0200247 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700248
K. Y. Srinivasan37f72782012-12-01 06:46:41 -0800249 vmbus_proto_version = version;
K. Y. Srinivasan8de8af72017-01-19 11:51:47 -0700250 pr_info("Vmbus version:%d.%d\n",
251 version >> 16, version & 0xFFFF);
K. Y. Srinivasan3bacaf0c2012-12-01 06:46:59 -0800252
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800253 kfree(msginfo);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700254 return 0;
255
K. Y. Srinivasanb0043862011-05-10 07:55:44 -0700256cleanup:
K. Y. Srinivasan3bacaf0c2012-12-01 06:46:59 -0800257 pr_err("Unable to connect to host\n");
Hank Janssen3e7ee492009-07-13 16:02:34 -0700258
Vitaly Kuznetsov09a19622015-02-27 11:25:54 -0800259 vmbus_connection.conn_state = DISCONNECTED;
260 vmbus_disconnect();
261
262 kfree(msginfo);
263
264 return ret;
265}
266
267void vmbus_disconnect(void)
268{
K. Y. Srinivasan2db84ef2015-04-22 21:31:32 -0700269 /*
270 * First send the unload request to the host.
271 */
Vitaly Kuznetsov75ff3a82016-02-26 15:13:16 -0800272 vmbus_initiate_unload(false);
K. Y. Srinivasan2db84ef2015-04-22 21:31:32 -0700273
Dexuan Cui37c25782018-12-03 00:54:35 +0000274 if (vmbus_connection.handle_sub_chan_wq)
275 destroy_workqueue(vmbus_connection.handle_sub_chan_wq);
276
277 if (vmbus_connection.handle_primary_chan_wq)
278 destroy_workqueue(vmbus_connection.handle_primary_chan_wq);
279
280 if (vmbus_connection.work_queue)
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800281 destroy_workqueue(vmbus_connection.work_queue);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700282
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800283 if (vmbus_connection.int_page) {
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800284 free_pages((unsigned long)vmbus_connection.int_page, 0);
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800285 vmbus_connection.int_page = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700286 }
287
Radim Krčmářa100d88d2014-05-27 19:16:20 +0200288 free_pages((unsigned long)vmbus_connection.monitor_pages[0], 0);
289 free_pages((unsigned long)vmbus_connection.monitor_pages[1], 0);
Greg Kroah-Hartman8681db42013-09-13 11:32:55 -0700290 vmbus_connection.monitor_pages[0] = NULL;
291 vmbus_connection.monitor_pages[1] = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700292}
293
K. Y. Srinivasan3a28fa32014-04-08 18:45:54 -0700294/*
Haiyang Zhangc6977672011-01-26 12:12:08 -0800295 * relid2channel - Get the channel object given its
296 * child relative id (ie channel id)
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700297 */
Dexuan Cuid43e2fe2015-03-27 09:10:09 -0700298struct vmbus_channel *relid2channel(u32 relid)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700299{
Greg Kroah-Hartmanaded7162009-08-18 15:21:19 -0700300 struct vmbus_channel *channel;
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800301 struct vmbus_channel *found_channel = NULL;
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700302 struct list_head *cur, *tmp;
303 struct vmbus_channel *cur_sc;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700304
Dexuan Cui85d9aa72016-01-27 22:29:43 -0800305 BUG_ON(!mutex_is_locked(&vmbus_connection.channel_mutex));
306
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800307 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800308 if (channel->offermsg.child_relid == relid) {
309 found_channel = channel;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700310 break;
K. Y. Srinivasane68d2972013-05-23 12:02:32 -0700311 } else if (!list_empty(&channel->sc_list)) {
312 /*
313 * Deal with sub-channels.
314 */
315 list_for_each_safe(cur, tmp, &channel->sc_list) {
316 cur_sc = list_entry(cur, struct vmbus_channel,
317 sc_list);
318 if (cur_sc->offermsg.child_relid == relid) {
319 found_channel = cur_sc;
320 break;
321 }
322 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700323 }
324 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700325
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800326 return found_channel;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700327}
328
Hank Janssen3e189512010-03-04 22:11:00 +0000329/*
Stephen Hemminger631e63a2017-02-11 23:02:20 -0700330 * vmbus_on_event - Process a channel event notification
Stephen Hemmingerada6eb12017-03-04 18:27:10 -0700331 *
332 * For batched channels (default) optimize host to guest signaling
333 * by ensuring:
334 * 1. While reading the channel, we disable interrupts from host.
335 * 2. Ensure that we process all posted messages from the host
336 * before returning from this callback.
337 * 3. Once we return, enable signaling from the host. Once this
338 * state is set we check to see if additional packets are
339 * available to read. In this case we repeat the process.
340 * If this tasklet has been running for a long time
341 * then reschedule ourselves.
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700342 */
Stephen Hemminger631e63a2017-02-11 23:02:20 -0700343void vmbus_on_event(unsigned long data)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700344{
Stephen Hemminger631e63a2017-02-11 23:02:20 -0700345 struct vmbus_channel *channel = (void *) data;
Stephen Hemmingerada6eb12017-03-04 18:27:10 -0700346 unsigned long time_limit = jiffies + 2;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700347
Vitaly Kuznetsov991f8f12017-10-29 12:21:16 -0700348 trace_vmbus_on_event(channel);
349
Stephen Hemmingerada6eb12017-03-04 18:27:10 -0700350 do {
351 void (*callback_fn)(void *);
Stephen Hemmingerb71e3282017-02-11 23:02:21 -0700352
Stephen Hemmingerada6eb12017-03-04 18:27:10 -0700353 /* A channel once created is persistent even when
354 * there is no driver handling the device. An
355 * unloading driver sets the onchannel_callback to NULL.
K. Y. Srinivasanf878f3d2012-12-01 06:46:35 -0800356 */
Stephen Hemmingerada6eb12017-03-04 18:27:10 -0700357 callback_fn = READ_ONCE(channel->onchannel_callback);
358 if (unlikely(callback_fn == NULL))
359 return;
K. Y. Srinivasanf878f3d2012-12-01 06:46:35 -0800360
Stephen Hemmingerada6eb12017-03-04 18:27:10 -0700361 (*callback_fn)(channel->channel_callback_context);
362
363 if (channel->callback_mode != HV_CALL_BATCHED)
364 return;
365
366 if (likely(hv_end_read(&channel->inbound) == 0))
367 return;
368
369 hv_begin_read(&channel->inbound);
370 } while (likely(time_before(jiffies, time_limit)));
371
372 /* The time limit (2 jiffies) has been reached */
373 tasklet_schedule(&channel->callback_event);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700374}
375
Hank Janssen3e189512010-03-04 22:11:00 +0000376/*
Haiyang Zhangc6977672011-01-26 12:12:08 -0800377 * vmbus_post_msg - Send a msg on the vmbus's message connection
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700378 */
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800379int vmbus_post_msg(void *buffer, size_t buflen, bool can_sleep)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700380{
Dexuan Cuiae20b252018-05-12 02:30:33 -0700381 struct vmbus_channel_message_header *hdr;
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800382 union hv_connection_id conn_id;
K. Y. Srinivasan5289d3d2011-08-25 09:49:01 -0700383 int ret = 0;
384 int retries = 0;
K. Y. Srinivasan8de0d7e2016-07-01 16:26:36 -0700385 u32 usec = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700386
Haiyang Zhang15b2f642011-01-26 12:12:07 -0800387 conn_id.asu32 = 0;
Dexuan Cuiae20b252018-05-12 02:30:33 -0700388 conn_id.u.id = vmbus_connection.msg_conn_id;
K. Y. Srinivasan5289d3d2011-08-25 09:49:01 -0700389
390 /*
391 * hv_post_message() can have transient failures because of
392 * insufficient resources. Retry the operation a couple of
393 * times before giving up.
394 */
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800395 while (retries < 100) {
K. Y. Srinivasanfdeebcc2014-08-27 16:25:31 -0700396 ret = hv_post_message(conn_id, 1, buffer, buflen);
397
398 switch (ret) {
Dexuan Cui89f9f672015-02-27 11:25:59 -0800399 case HV_STATUS_INVALID_CONNECTION_ID:
400 /*
Dexuan Cuiae20b252018-05-12 02:30:33 -0700401 * See vmbus_negotiate_version(): VMBus protocol 5.0
402 * requires that we must use
403 * VMBUS_MESSAGE_CONNECTION_ID_4 for the Initiate
404 * Contact message, but on old hosts that only
405 * support VMBus protocol 4.0 or lower, here we get
406 * HV_STATUS_INVALID_CONNECTION_ID and we should
407 * return an error immediately without retrying.
408 */
Dexuan Cui89760932018-05-15 00:25:01 +0000409 hdr = buffer;
Dexuan Cuiae20b252018-05-12 02:30:33 -0700410 if (hdr->msgtype == CHANNELMSG_INITIATE_CONTACT)
411 return -EINVAL;
412 /*
Dexuan Cui89f9f672015-02-27 11:25:59 -0800413 * We could get this if we send messages too
414 * frequently.
415 */
416 ret = -EAGAIN;
417 break;
418 case HV_STATUS_INSUFFICIENT_MEMORY:
K. Y. Srinivasanfdeebcc2014-08-27 16:25:31 -0700419 case HV_STATUS_INSUFFICIENT_BUFFERS:
K. Y. Srinivasan48f4ccd2017-04-30 16:21:16 -0700420 ret = -ENOBUFS;
K. Y. Srinivasanfdeebcc2014-08-27 16:25:31 -0700421 break;
422 case HV_STATUS_SUCCESS:
K. Y. Srinivasan5289d3d2011-08-25 09:49:01 -0700423 return ret;
K. Y. Srinivasanfdeebcc2014-08-27 16:25:31 -0700424 default:
425 pr_err("hv_post_msg() failed; error code:%d\n", ret);
426 return -EINVAL;
427 }
428
K. Y. Srinivasan5289d3d2011-08-25 09:49:01 -0700429 retries++;
Vitaly Kuznetsovc0bb0392016-12-07 01:16:24 -0800430 if (can_sleep && usec > 1000)
431 msleep(usec / 1000);
432 else if (usec < MAX_UDELAY_MS * 1000)
433 udelay(usec);
434 else
435 mdelay(usec / 1000);
436
K. Y. Srinivasane917a5e2017-05-18 10:46:05 -0700437 if (retries < 22)
K. Y. Srinivasan8de0d7e2016-07-01 16:26:36 -0700438 usec *= 2;
K. Y. Srinivasan5289d3d2011-08-25 09:49:01 -0700439 }
440 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700441}
442
Hank Janssen3e189512010-03-04 22:11:00 +0000443/*
Haiyang Zhangc6977672011-01-26 12:12:08 -0800444 * vmbus_set_event - Send an event notification to the parent
Greg Kroah-Hartmanfd8b85e2009-08-31 11:40:14 -0700445 */
K. Y. Srinivasan1b807e12015-12-21 15:12:20 -0800446void vmbus_set_event(struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700447{
K. Y. Srinivasan21c3bef2012-12-01 06:46:43 -0800448 u32 child_relid = channel->offermsg.child_relid;
Bill Pemberton7c369f42009-07-29 17:00:11 -0400449
Stephen Hemminger5c1bec62017-02-05 17:20:31 -0700450 if (!channel->is_dedicated_interrupt)
451 vmbus_send_interrupt(child_relid);
K. Y. Srinivasan3be77772012-12-01 06:46:46 -0800452
Stephen Hemminger6981fbf2017-10-29 11:33:40 -0700453 ++channel->sig_events;
454
Vitaly Kuznetsov05784172017-08-02 18:09:16 +0200455 hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700456}
K. Y. Srinivasan5cc47242016-04-02 17:59:49 -0700457EXPORT_SYMBOL_GPL(vmbus_set_event);