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