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