]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/hv/connection.c
Drivers: hv: vmbus: Suspend/resume the vmbus itself for hibernation
[linux.git] / drivers / hv / connection.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
4  * Copyright (c) 2009, Microsoft Corporation.
5  *
6  * Authors:
7  *   Haiyang Zhang <haiyangz@microsoft.com>
8  *   Hank Janssen  <hjanssen@microsoft.com>
9  */
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/wait.h>
15 #include <linux/delay.h>
16 #include <linux/mm.h>
17 #include <linux/slab.h>
18 #include <linux/vmalloc.h>
19 #include <linux/hyperv.h>
20 #include <linux/export.h>
21 #include <asm/mshyperv.h>
22
23 #include "hyperv_vmbus.h"
24
25
26 struct vmbus_connection vmbus_connection = {
27         .conn_state             = DISCONNECTED,
28         .next_gpadl_handle      = ATOMIC_INIT(0xE1E10),
29 };
30 EXPORT_SYMBOL_GPL(vmbus_connection);
31
32 /*
33  * Negotiated protocol version with the host.
34  */
35 __u32 vmbus_proto_version;
36 EXPORT_SYMBOL_GPL(vmbus_proto_version);
37
38 static __u32 vmbus_get_next_version(__u32 current_version)
39 {
40         switch (current_version) {
41         case (VERSION_WIN7):
42                 return VERSION_WS2008;
43
44         case (VERSION_WIN8):
45                 return VERSION_WIN7;
46
47         case (VERSION_WIN8_1):
48                 return VERSION_WIN8;
49
50         case (VERSION_WIN10):
51                 return VERSION_WIN8_1;
52
53         case (VERSION_WIN10_V5):
54                 return VERSION_WIN10;
55
56         case (VERSION_WS2008):
57         default:
58                 return VERSION_INVAL;
59         }
60 }
61
62 int vmbus_negotiate_version(struct vmbus_channel_msginfo *msginfo, u32 version)
63 {
64         int ret = 0;
65         unsigned int cur_cpu;
66         struct vmbus_channel_initiate_contact *msg;
67         unsigned long flags;
68
69         init_completion(&msginfo->waitevent);
70
71         msg = (struct vmbus_channel_initiate_contact *)msginfo->msg;
72
73         memset(msg, 0, sizeof(*msg));
74         msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT;
75         msg->vmbus_version_requested = version;
76
77         /*
78          * VMBus protocol 5.0 (VERSION_WIN10_V5) requires that we must use
79          * VMBUS_MESSAGE_CONNECTION_ID_4 for the Initiate Contact Message,
80          * and for subsequent messages, we must use the Message Connection ID
81          * field in the host-returned Version Response Message. And, with
82          * VERSION_WIN10_V5, we don't use msg->interrupt_page, but we tell
83          * the host explicitly that we still use VMBUS_MESSAGE_SINT(2) for
84          * compatibility.
85          *
86          * On old hosts, we should always use VMBUS_MESSAGE_CONNECTION_ID (1).
87          */
88         if (version >= VERSION_WIN10_V5) {
89                 msg->msg_sint = VMBUS_MESSAGE_SINT;
90                 vmbus_connection.msg_conn_id = VMBUS_MESSAGE_CONNECTION_ID_4;
91         } else {
92                 msg->interrupt_page = virt_to_phys(vmbus_connection.int_page);
93                 vmbus_connection.msg_conn_id = VMBUS_MESSAGE_CONNECTION_ID;
94         }
95
96         msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages[0]);
97         msg->monitor_page2 = virt_to_phys(vmbus_connection.monitor_pages[1]);
98         /*
99          * We want all channel messages to be delivered on CPU 0.
100          * This has been the behavior pre-win8. This is not
101          * perf issue and having all channel messages delivered on CPU 0
102          * would be ok.
103          * For post win8 hosts, we support receiving channel messagges on
104          * all the CPUs. This is needed for kexec to work correctly where
105          * the CPU attempting to connect may not be CPU 0.
106          */
107         if (version >= VERSION_WIN8_1) {
108                 cur_cpu = get_cpu();
109                 msg->target_vcpu = hv_cpu_number_to_vp_number(cur_cpu);
110                 vmbus_connection.connect_cpu = cur_cpu;
111                 put_cpu();
112         } else {
113                 msg->target_vcpu = 0;
114                 vmbus_connection.connect_cpu = 0;
115         }
116
117         /*
118          * Add to list before we send the request since we may
119          * receive the response before returning from this routine
120          */
121         spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
122         list_add_tail(&msginfo->msglistentry,
123                       &vmbus_connection.chn_msg_list);
124
125         spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
126
127         ret = vmbus_post_msg(msg,
128                              sizeof(struct vmbus_channel_initiate_contact),
129                              true);
130
131         trace_vmbus_negotiate_version(msg, ret);
132
133         if (ret != 0) {
134                 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
135                 list_del(&msginfo->msglistentry);
136                 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock,
137                                         flags);
138                 return ret;
139         }
140
141         /* Wait for the connection response */
142         wait_for_completion(&msginfo->waitevent);
143
144         spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
145         list_del(&msginfo->msglistentry);
146         spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
147
148         /* Check if successful */
149         if (msginfo->response.version_response.version_supported) {
150                 vmbus_connection.conn_state = CONNECTED;
151
152                 if (version >= VERSION_WIN10_V5)
153                         vmbus_connection.msg_conn_id =
154                                 msginfo->response.version_response.msg_conn_id;
155         } else {
156                 return -ECONNREFUSED;
157         }
158
159         return ret;
160 }
161
162 /*
163  * vmbus_connect - Sends a connect request on the partition service connection
164  */
165 int vmbus_connect(void)
166 {
167         int ret = 0;
168         struct vmbus_channel_msginfo *msginfo = NULL;
169         __u32 version;
170
171         /* Initialize the vmbus connection */
172         vmbus_connection.conn_state = CONNECTING;
173         vmbus_connection.work_queue = create_workqueue("hv_vmbus_con");
174         if (!vmbus_connection.work_queue) {
175                 ret = -ENOMEM;
176                 goto cleanup;
177         }
178
179         vmbus_connection.handle_primary_chan_wq =
180                 create_workqueue("hv_pri_chan");
181         if (!vmbus_connection.handle_primary_chan_wq) {
182                 ret = -ENOMEM;
183                 goto cleanup;
184         }
185
186         vmbus_connection.handle_sub_chan_wq =
187                 create_workqueue("hv_sub_chan");
188         if (!vmbus_connection.handle_sub_chan_wq) {
189                 ret = -ENOMEM;
190                 goto cleanup;
191         }
192
193         INIT_LIST_HEAD(&vmbus_connection.chn_msg_list);
194         spin_lock_init(&vmbus_connection.channelmsg_lock);
195
196         INIT_LIST_HEAD(&vmbus_connection.chn_list);
197         mutex_init(&vmbus_connection.channel_mutex);
198
199         /*
200          * Setup the vmbus event connection for channel interrupt
201          * abstraction stuff
202          */
203         vmbus_connection.int_page =
204         (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, 0);
205         if (vmbus_connection.int_page == NULL) {
206                 ret = -ENOMEM;
207                 goto cleanup;
208         }
209
210         vmbus_connection.recv_int_page = vmbus_connection.int_page;
211         vmbus_connection.send_int_page =
212                 (void *)((unsigned long)vmbus_connection.int_page +
213                         (PAGE_SIZE >> 1));
214
215         /*
216          * Setup the monitor notification facility. The 1st page for
217          * parent->child and the 2nd page for child->parent
218          */
219         vmbus_connection.monitor_pages[0] = (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 0);
220         vmbus_connection.monitor_pages[1] = (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 0);
221         if ((vmbus_connection.monitor_pages[0] == NULL) ||
222             (vmbus_connection.monitor_pages[1] == NULL)) {
223                 ret = -ENOMEM;
224                 goto cleanup;
225         }
226
227         msginfo = kzalloc(sizeof(*msginfo) +
228                           sizeof(struct vmbus_channel_initiate_contact),
229                           GFP_KERNEL);
230         if (msginfo == NULL) {
231                 ret = -ENOMEM;
232                 goto cleanup;
233         }
234
235         /*
236          * Negotiate a compatible VMBUS version number with the
237          * host. We start with the highest number we can support
238          * and work our way down until we negotiate a compatible
239          * version.
240          */
241
242         version = VERSION_CURRENT;
243
244         do {
245                 ret = vmbus_negotiate_version(msginfo, version);
246                 if (ret == -ETIMEDOUT)
247                         goto cleanup;
248
249                 if (vmbus_connection.conn_state == CONNECTED)
250                         break;
251
252                 version = vmbus_get_next_version(version);
253         } while (version != VERSION_INVAL);
254
255         if (version == VERSION_INVAL)
256                 goto cleanup;
257
258         vmbus_proto_version = version;
259         pr_info("Vmbus version:%d.%d\n",
260                 version >> 16, version & 0xFFFF);
261
262         kfree(msginfo);
263         return 0;
264
265 cleanup:
266         pr_err("Unable to connect to host\n");
267
268         vmbus_connection.conn_state = DISCONNECTED;
269         vmbus_disconnect();
270
271         kfree(msginfo);
272
273         return ret;
274 }
275
276 void vmbus_disconnect(void)
277 {
278         /*
279          * First send the unload request to the host.
280          */
281         vmbus_initiate_unload(false);
282
283         if (vmbus_connection.handle_sub_chan_wq)
284                 destroy_workqueue(vmbus_connection.handle_sub_chan_wq);
285
286         if (vmbus_connection.handle_primary_chan_wq)
287                 destroy_workqueue(vmbus_connection.handle_primary_chan_wq);
288
289         if (vmbus_connection.work_queue)
290                 destroy_workqueue(vmbus_connection.work_queue);
291
292         if (vmbus_connection.int_page) {
293                 free_pages((unsigned long)vmbus_connection.int_page, 0);
294                 vmbus_connection.int_page = NULL;
295         }
296
297         free_pages((unsigned long)vmbus_connection.monitor_pages[0], 0);
298         free_pages((unsigned long)vmbus_connection.monitor_pages[1], 0);
299         vmbus_connection.monitor_pages[0] = NULL;
300         vmbus_connection.monitor_pages[1] = NULL;
301 }
302
303 /*
304  * relid2channel - Get the channel object given its
305  * child relative id (ie channel id)
306  */
307 struct vmbus_channel *relid2channel(u32 relid)
308 {
309         struct vmbus_channel *channel;
310         struct vmbus_channel *found_channel  = NULL;
311         struct list_head *cur, *tmp;
312         struct vmbus_channel *cur_sc;
313
314         BUG_ON(!mutex_is_locked(&vmbus_connection.channel_mutex));
315
316         list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
317                 if (channel->offermsg.child_relid == relid) {
318                         found_channel = channel;
319                         break;
320                 } else if (!list_empty(&channel->sc_list)) {
321                         /*
322                          * Deal with sub-channels.
323                          */
324                         list_for_each_safe(cur, tmp, &channel->sc_list) {
325                                 cur_sc = list_entry(cur, struct vmbus_channel,
326                                                         sc_list);
327                                 if (cur_sc->offermsg.child_relid == relid) {
328                                         found_channel = cur_sc;
329                                         break;
330                                 }
331                         }
332                 }
333         }
334
335         return found_channel;
336 }
337
338 /*
339  * vmbus_on_event - Process a channel event notification
340  *
341  * For batched channels (default) optimize host to guest signaling
342  * by ensuring:
343  * 1. While reading the channel, we disable interrupts from host.
344  * 2. Ensure that we process all posted messages from the host
345  *    before returning from this callback.
346  * 3. Once we return, enable signaling from the host. Once this
347  *    state is set we check to see if additional packets are
348  *    available to read. In this case we repeat the process.
349  *    If this tasklet has been running for a long time
350  *    then reschedule ourselves.
351  */
352 void vmbus_on_event(unsigned long data)
353 {
354         struct vmbus_channel *channel = (void *) data;
355         unsigned long time_limit = jiffies + 2;
356
357         trace_vmbus_on_event(channel);
358
359         do {
360                 void (*callback_fn)(void *);
361
362                 /* A channel once created is persistent even when
363                  * there is no driver handling the device. An
364                  * unloading driver sets the onchannel_callback to NULL.
365                  */
366                 callback_fn = READ_ONCE(channel->onchannel_callback);
367                 if (unlikely(callback_fn == NULL))
368                         return;
369
370                 (*callback_fn)(channel->channel_callback_context);
371
372                 if (channel->callback_mode != HV_CALL_BATCHED)
373                         return;
374
375                 if (likely(hv_end_read(&channel->inbound) == 0))
376                         return;
377
378                 hv_begin_read(&channel->inbound);
379         } while (likely(time_before(jiffies, time_limit)));
380
381         /* The time limit (2 jiffies) has been reached */
382         tasklet_schedule(&channel->callback_event);
383 }
384
385 /*
386  * vmbus_post_msg - Send a msg on the vmbus's message connection
387  */
388 int vmbus_post_msg(void *buffer, size_t buflen, bool can_sleep)
389 {
390         struct vmbus_channel_message_header *hdr;
391         union hv_connection_id conn_id;
392         int ret = 0;
393         int retries = 0;
394         u32 usec = 1;
395
396         conn_id.asu32 = 0;
397         conn_id.u.id = vmbus_connection.msg_conn_id;
398
399         /*
400          * hv_post_message() can have transient failures because of
401          * insufficient resources. Retry the operation a couple of
402          * times before giving up.
403          */
404         while (retries < 100) {
405                 ret = hv_post_message(conn_id, 1, buffer, buflen);
406
407                 switch (ret) {
408                 case HV_STATUS_INVALID_CONNECTION_ID:
409                         /*
410                          * See vmbus_negotiate_version(): VMBus protocol 5.0
411                          * requires that we must use
412                          * VMBUS_MESSAGE_CONNECTION_ID_4 for the Initiate
413                          * Contact message, but on old hosts that only
414                          * support VMBus protocol 4.0 or lower, here we get
415                          * HV_STATUS_INVALID_CONNECTION_ID and we should
416                          * return an error immediately without retrying.
417                          */
418                         hdr = buffer;
419                         if (hdr->msgtype == CHANNELMSG_INITIATE_CONTACT)
420                                 return -EINVAL;
421                         /*
422                          * We could get this if we send messages too
423                          * frequently.
424                          */
425                         ret = -EAGAIN;
426                         break;
427                 case HV_STATUS_INSUFFICIENT_MEMORY:
428                 case HV_STATUS_INSUFFICIENT_BUFFERS:
429                         ret = -ENOBUFS;
430                         break;
431                 case HV_STATUS_SUCCESS:
432                         return ret;
433                 default:
434                         pr_err("hv_post_msg() failed; error code:%d\n", ret);
435                         return -EINVAL;
436                 }
437
438                 retries++;
439                 if (can_sleep && usec > 1000)
440                         msleep(usec / 1000);
441                 else if (usec < MAX_UDELAY_MS * 1000)
442                         udelay(usec);
443                 else
444                         mdelay(usec / 1000);
445
446                 if (retries < 22)
447                         usec *= 2;
448         }
449         return ret;
450 }
451
452 /*
453  * vmbus_set_event - Send an event notification to the parent
454  */
455 void vmbus_set_event(struct vmbus_channel *channel)
456 {
457         u32 child_relid = channel->offermsg.child_relid;
458
459         if (!channel->is_dedicated_interrupt)
460                 vmbus_send_interrupt(child_relid);
461
462         ++channel->sig_events;
463
464         hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event);
465 }
466 EXPORT_SYMBOL_GPL(vmbus_set_event);