1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2009, Microsoft Corporation.
6 * Haiyang Zhang <haiyangz@microsoft.com>
7 * Hank Janssen <hjanssen@microsoft.com>
8 * K. Y. Srinivasan <kys@microsoft.com>
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/interrupt.h>
16 #include <linux/sysctl.h>
17 #include <linux/slab.h>
18 #include <linux/acpi.h>
19 #include <linux/completion.h>
20 #include <linux/hyperv.h>
21 #include <linux/kernel_stat.h>
22 #include <linux/clockchips.h>
23 #include <linux/cpu.h>
24 #include <linux/sched/isolation.h>
25 #include <linux/sched/task_stack.h>
27 #include <linux/delay.h>
28 #include <linux/panic_notifier.h>
29 #include <linux/ptrace.h>
30 #include <linux/screen_info.h>
31 #include <linux/kdebug.h>
32 #include <linux/efi.h>
33 #include <linux/random.h>
34 #include <linux/kernel.h>
35 #include <linux/syscore_ops.h>
36 #include <linux/dma-map-ops.h>
37 #include <linux/pci.h>
38 #include <clocksource/hyperv_timer.h>
39 #include <asm/mshyperv.h>
40 #include "hyperv_vmbus.h"
43 struct list_head node;
44 struct hv_vmbus_device_id id;
47 static struct acpi_device *hv_acpi_dev;
49 static int hyperv_cpuhp_online;
51 static void *hv_panic_page;
53 static long __percpu *vmbus_evt;
55 /* Values parsed from ACPI DSDT */
60 * Boolean to control whether to report panic messages over Hyper-V.
62 * It can be set via /proc/sys/kernel/hyperv_record_panic_msg
64 static int sysctl_record_panic_msg = 1;
66 static int hyperv_report_reg(void)
68 return !sysctl_record_panic_msg || !hv_panic_page;
72 * The panic notifier below is responsible solely for unloading the
73 * vmbus connection, which is necessary in a panic event.
75 * Notice an intrincate relation of this notifier with Hyper-V
76 * framebuffer panic notifier exists - we need vmbus connection alive
77 * there in order to succeed, so we need to order both with each other
78 * [see hvfb_on_panic()] - this is done using notifiers' priorities.
80 static int hv_panic_vmbus_unload(struct notifier_block *nb, unsigned long val,
83 vmbus_initiate_unload(true);
86 static struct notifier_block hyperv_panic_vmbus_unload_block = {
87 .notifier_call = hv_panic_vmbus_unload,
88 .priority = INT_MIN + 1, /* almost the latest one to execute */
91 static int hv_die_panic_notify_crash(struct notifier_block *self,
92 unsigned long val, void *args);
94 static struct notifier_block hyperv_die_report_block = {
95 .notifier_call = hv_die_panic_notify_crash,
97 static struct notifier_block hyperv_panic_report_block = {
98 .notifier_call = hv_die_panic_notify_crash,
102 * The following callback works both as die and panic notifier; its
103 * goal is to provide panic information to the hypervisor unless the
104 * kmsg dumper is used [see hv_kmsg_dump()], which provides more
105 * information but isn't always available.
107 * Notice that both the panic/die report notifiers are registered only
108 * if we have the capability HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE set.
110 static int hv_die_panic_notify_crash(struct notifier_block *self,
111 unsigned long val, void *args)
113 struct pt_regs *regs;
116 /* Don't notify Hyper-V unless we have a die oops event or panic. */
117 if (self == &hyperv_panic_report_block) {
119 regs = current_pt_regs();
120 } else { /* die event */
125 regs = ((struct die_args *)args)->regs;
129 * Hyper-V should be notified only once about a panic/die. If we will
130 * be calling hv_kmsg_dump() later with kmsg data, don't do the
133 if (hyperv_report_reg())
134 hyperv_report_panic(regs, val, is_die);
139 static const char *fb_mmio_name = "fb_range";
140 static struct resource *fb_mmio;
141 static struct resource *hyperv_mmio;
142 static DEFINE_MUTEX(hyperv_mmio_lock);
144 static int vmbus_exists(void)
146 if (hv_acpi_dev == NULL)
152 static u8 channel_monitor_group(const struct vmbus_channel *channel)
154 return (u8)channel->offermsg.monitorid / 32;
157 static u8 channel_monitor_offset(const struct vmbus_channel *channel)
159 return (u8)channel->offermsg.monitorid % 32;
162 static u32 channel_pending(const struct vmbus_channel *channel,
163 const struct hv_monitor_page *monitor_page)
165 u8 monitor_group = channel_monitor_group(channel);
167 return monitor_page->trigger_group[monitor_group].pending;
170 static u32 channel_latency(const struct vmbus_channel *channel,
171 const struct hv_monitor_page *monitor_page)
173 u8 monitor_group = channel_monitor_group(channel);
174 u8 monitor_offset = channel_monitor_offset(channel);
176 return monitor_page->latency[monitor_group][monitor_offset];
179 static u32 channel_conn_id(struct vmbus_channel *channel,
180 struct hv_monitor_page *monitor_page)
182 u8 monitor_group = channel_monitor_group(channel);
183 u8 monitor_offset = channel_monitor_offset(channel);
185 return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
188 static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
191 struct hv_device *hv_dev = device_to_hv_device(dev);
193 if (!hv_dev->channel)
195 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
197 static DEVICE_ATTR_RO(id);
199 static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
202 struct hv_device *hv_dev = device_to_hv_device(dev);
204 if (!hv_dev->channel)
206 return sprintf(buf, "%d\n", hv_dev->channel->state);
208 static DEVICE_ATTR_RO(state);
210 static ssize_t monitor_id_show(struct device *dev,
211 struct device_attribute *dev_attr, char *buf)
213 struct hv_device *hv_dev = device_to_hv_device(dev);
215 if (!hv_dev->channel)
217 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
219 static DEVICE_ATTR_RO(monitor_id);
221 static ssize_t class_id_show(struct device *dev,
222 struct device_attribute *dev_attr, char *buf)
224 struct hv_device *hv_dev = device_to_hv_device(dev);
226 if (!hv_dev->channel)
228 return sprintf(buf, "{%pUl}\n",
229 &hv_dev->channel->offermsg.offer.if_type);
231 static DEVICE_ATTR_RO(class_id);
233 static ssize_t device_id_show(struct device *dev,
234 struct device_attribute *dev_attr, char *buf)
236 struct hv_device *hv_dev = device_to_hv_device(dev);
238 if (!hv_dev->channel)
240 return sprintf(buf, "{%pUl}\n",
241 &hv_dev->channel->offermsg.offer.if_instance);
243 static DEVICE_ATTR_RO(device_id);
245 static ssize_t modalias_show(struct device *dev,
246 struct device_attribute *dev_attr, char *buf)
248 struct hv_device *hv_dev = device_to_hv_device(dev);
250 return sprintf(buf, "vmbus:%*phN\n", UUID_SIZE, &hv_dev->dev_type);
252 static DEVICE_ATTR_RO(modalias);
255 static ssize_t numa_node_show(struct device *dev,
256 struct device_attribute *attr, char *buf)
258 struct hv_device *hv_dev = device_to_hv_device(dev);
260 if (!hv_dev->channel)
263 return sprintf(buf, "%d\n", cpu_to_node(hv_dev->channel->target_cpu));
265 static DEVICE_ATTR_RO(numa_node);
268 static ssize_t server_monitor_pending_show(struct device *dev,
269 struct device_attribute *dev_attr,
272 struct hv_device *hv_dev = device_to_hv_device(dev);
274 if (!hv_dev->channel)
276 return sprintf(buf, "%d\n",
277 channel_pending(hv_dev->channel,
278 vmbus_connection.monitor_pages[0]));
280 static DEVICE_ATTR_RO(server_monitor_pending);
282 static ssize_t client_monitor_pending_show(struct device *dev,
283 struct device_attribute *dev_attr,
286 struct hv_device *hv_dev = device_to_hv_device(dev);
288 if (!hv_dev->channel)
290 return sprintf(buf, "%d\n",
291 channel_pending(hv_dev->channel,
292 vmbus_connection.monitor_pages[1]));
294 static DEVICE_ATTR_RO(client_monitor_pending);
296 static ssize_t server_monitor_latency_show(struct device *dev,
297 struct device_attribute *dev_attr,
300 struct hv_device *hv_dev = device_to_hv_device(dev);
302 if (!hv_dev->channel)
304 return sprintf(buf, "%d\n",
305 channel_latency(hv_dev->channel,
306 vmbus_connection.monitor_pages[0]));
308 static DEVICE_ATTR_RO(server_monitor_latency);
310 static ssize_t client_monitor_latency_show(struct device *dev,
311 struct device_attribute *dev_attr,
314 struct hv_device *hv_dev = device_to_hv_device(dev);
316 if (!hv_dev->channel)
318 return sprintf(buf, "%d\n",
319 channel_latency(hv_dev->channel,
320 vmbus_connection.monitor_pages[1]));
322 static DEVICE_ATTR_RO(client_monitor_latency);
324 static ssize_t server_monitor_conn_id_show(struct device *dev,
325 struct device_attribute *dev_attr,
328 struct hv_device *hv_dev = device_to_hv_device(dev);
330 if (!hv_dev->channel)
332 return sprintf(buf, "%d\n",
333 channel_conn_id(hv_dev->channel,
334 vmbus_connection.monitor_pages[0]));
336 static DEVICE_ATTR_RO(server_monitor_conn_id);
338 static ssize_t client_monitor_conn_id_show(struct device *dev,
339 struct device_attribute *dev_attr,
342 struct hv_device *hv_dev = device_to_hv_device(dev);
344 if (!hv_dev->channel)
346 return sprintf(buf, "%d\n",
347 channel_conn_id(hv_dev->channel,
348 vmbus_connection.monitor_pages[1]));
350 static DEVICE_ATTR_RO(client_monitor_conn_id);
352 static ssize_t out_intr_mask_show(struct device *dev,
353 struct device_attribute *dev_attr, char *buf)
355 struct hv_device *hv_dev = device_to_hv_device(dev);
356 struct hv_ring_buffer_debug_info outbound;
359 if (!hv_dev->channel)
362 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
367 return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
369 static DEVICE_ATTR_RO(out_intr_mask);
371 static ssize_t out_read_index_show(struct device *dev,
372 struct device_attribute *dev_attr, char *buf)
374 struct hv_device *hv_dev = device_to_hv_device(dev);
375 struct hv_ring_buffer_debug_info outbound;
378 if (!hv_dev->channel)
381 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
385 return sprintf(buf, "%d\n", outbound.current_read_index);
387 static DEVICE_ATTR_RO(out_read_index);
389 static ssize_t out_write_index_show(struct device *dev,
390 struct device_attribute *dev_attr,
393 struct hv_device *hv_dev = device_to_hv_device(dev);
394 struct hv_ring_buffer_debug_info outbound;
397 if (!hv_dev->channel)
400 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
404 return sprintf(buf, "%d\n", outbound.current_write_index);
406 static DEVICE_ATTR_RO(out_write_index);
408 static ssize_t out_read_bytes_avail_show(struct device *dev,
409 struct device_attribute *dev_attr,
412 struct hv_device *hv_dev = device_to_hv_device(dev);
413 struct hv_ring_buffer_debug_info outbound;
416 if (!hv_dev->channel)
419 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
423 return sprintf(buf, "%d\n", outbound.bytes_avail_toread);
425 static DEVICE_ATTR_RO(out_read_bytes_avail);
427 static ssize_t out_write_bytes_avail_show(struct device *dev,
428 struct device_attribute *dev_attr,
431 struct hv_device *hv_dev = device_to_hv_device(dev);
432 struct hv_ring_buffer_debug_info outbound;
435 if (!hv_dev->channel)
438 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
442 return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
444 static DEVICE_ATTR_RO(out_write_bytes_avail);
446 static ssize_t in_intr_mask_show(struct device *dev,
447 struct device_attribute *dev_attr, char *buf)
449 struct hv_device *hv_dev = device_to_hv_device(dev);
450 struct hv_ring_buffer_debug_info inbound;
453 if (!hv_dev->channel)
456 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
460 return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
462 static DEVICE_ATTR_RO(in_intr_mask);
464 static ssize_t in_read_index_show(struct device *dev,
465 struct device_attribute *dev_attr, char *buf)
467 struct hv_device *hv_dev = device_to_hv_device(dev);
468 struct hv_ring_buffer_debug_info inbound;
471 if (!hv_dev->channel)
474 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
478 return sprintf(buf, "%d\n", inbound.current_read_index);
480 static DEVICE_ATTR_RO(in_read_index);
482 static ssize_t in_write_index_show(struct device *dev,
483 struct device_attribute *dev_attr, char *buf)
485 struct hv_device *hv_dev = device_to_hv_device(dev);
486 struct hv_ring_buffer_debug_info inbound;
489 if (!hv_dev->channel)
492 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
496 return sprintf(buf, "%d\n", inbound.current_write_index);
498 static DEVICE_ATTR_RO(in_write_index);
500 static ssize_t in_read_bytes_avail_show(struct device *dev,
501 struct device_attribute *dev_attr,
504 struct hv_device *hv_dev = device_to_hv_device(dev);
505 struct hv_ring_buffer_debug_info inbound;
508 if (!hv_dev->channel)
511 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
515 return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
517 static DEVICE_ATTR_RO(in_read_bytes_avail);
519 static ssize_t in_write_bytes_avail_show(struct device *dev,
520 struct device_attribute *dev_attr,
523 struct hv_device *hv_dev = device_to_hv_device(dev);
524 struct hv_ring_buffer_debug_info inbound;
527 if (!hv_dev->channel)
530 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
534 return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
536 static DEVICE_ATTR_RO(in_write_bytes_avail);
538 static ssize_t channel_vp_mapping_show(struct device *dev,
539 struct device_attribute *dev_attr,
542 struct hv_device *hv_dev = device_to_hv_device(dev);
543 struct vmbus_channel *channel = hv_dev->channel, *cur_sc;
544 int buf_size = PAGE_SIZE, n_written, tot_written;
545 struct list_head *cur;
550 mutex_lock(&vmbus_connection.channel_mutex);
552 tot_written = snprintf(buf, buf_size, "%u:%u\n",
553 channel->offermsg.child_relid, channel->target_cpu);
555 list_for_each(cur, &channel->sc_list) {
556 if (tot_written >= buf_size - 1)
559 cur_sc = list_entry(cur, struct vmbus_channel, sc_list);
560 n_written = scnprintf(buf + tot_written,
561 buf_size - tot_written,
563 cur_sc->offermsg.child_relid,
565 tot_written += n_written;
568 mutex_unlock(&vmbus_connection.channel_mutex);
572 static DEVICE_ATTR_RO(channel_vp_mapping);
574 static ssize_t vendor_show(struct device *dev,
575 struct device_attribute *dev_attr,
578 struct hv_device *hv_dev = device_to_hv_device(dev);
580 return sprintf(buf, "0x%x\n", hv_dev->vendor_id);
582 static DEVICE_ATTR_RO(vendor);
584 static ssize_t device_show(struct device *dev,
585 struct device_attribute *dev_attr,
588 struct hv_device *hv_dev = device_to_hv_device(dev);
590 return sprintf(buf, "0x%x\n", hv_dev->device_id);
592 static DEVICE_ATTR_RO(device);
594 static ssize_t driver_override_store(struct device *dev,
595 struct device_attribute *attr,
596 const char *buf, size_t count)
598 struct hv_device *hv_dev = device_to_hv_device(dev);
601 ret = driver_set_override(dev, &hv_dev->driver_override, buf, count);
608 static ssize_t driver_override_show(struct device *dev,
609 struct device_attribute *attr, char *buf)
611 struct hv_device *hv_dev = device_to_hv_device(dev);
615 len = snprintf(buf, PAGE_SIZE, "%s\n", hv_dev->driver_override);
620 static DEVICE_ATTR_RW(driver_override);
622 /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
623 static struct attribute *vmbus_dev_attrs[] = {
625 &dev_attr_state.attr,
626 &dev_attr_monitor_id.attr,
627 &dev_attr_class_id.attr,
628 &dev_attr_device_id.attr,
629 &dev_attr_modalias.attr,
631 &dev_attr_numa_node.attr,
633 &dev_attr_server_monitor_pending.attr,
634 &dev_attr_client_monitor_pending.attr,
635 &dev_attr_server_monitor_latency.attr,
636 &dev_attr_client_monitor_latency.attr,
637 &dev_attr_server_monitor_conn_id.attr,
638 &dev_attr_client_monitor_conn_id.attr,
639 &dev_attr_out_intr_mask.attr,
640 &dev_attr_out_read_index.attr,
641 &dev_attr_out_write_index.attr,
642 &dev_attr_out_read_bytes_avail.attr,
643 &dev_attr_out_write_bytes_avail.attr,
644 &dev_attr_in_intr_mask.attr,
645 &dev_attr_in_read_index.attr,
646 &dev_attr_in_write_index.attr,
647 &dev_attr_in_read_bytes_avail.attr,
648 &dev_attr_in_write_bytes_avail.attr,
649 &dev_attr_channel_vp_mapping.attr,
650 &dev_attr_vendor.attr,
651 &dev_attr_device.attr,
652 &dev_attr_driver_override.attr,
657 * Device-level attribute_group callback function. Returns the permission for
658 * each attribute, and returns 0 if an attribute is not visible.
660 static umode_t vmbus_dev_attr_is_visible(struct kobject *kobj,
661 struct attribute *attr, int idx)
663 struct device *dev = kobj_to_dev(kobj);
664 const struct hv_device *hv_dev = device_to_hv_device(dev);
666 /* Hide the monitor attributes if the monitor mechanism is not used. */
667 if (!hv_dev->channel->offermsg.monitor_allocated &&
668 (attr == &dev_attr_monitor_id.attr ||
669 attr == &dev_attr_server_monitor_pending.attr ||
670 attr == &dev_attr_client_monitor_pending.attr ||
671 attr == &dev_attr_server_monitor_latency.attr ||
672 attr == &dev_attr_client_monitor_latency.attr ||
673 attr == &dev_attr_server_monitor_conn_id.attr ||
674 attr == &dev_attr_client_monitor_conn_id.attr))
680 static const struct attribute_group vmbus_dev_group = {
681 .attrs = vmbus_dev_attrs,
682 .is_visible = vmbus_dev_attr_is_visible
684 __ATTRIBUTE_GROUPS(vmbus_dev);
686 /* Set up the attribute for /sys/bus/vmbus/hibernation */
687 static ssize_t hibernation_show(struct bus_type *bus, char *buf)
689 return sprintf(buf, "%d\n", !!hv_is_hibernation_supported());
692 static BUS_ATTR_RO(hibernation);
694 static struct attribute *vmbus_bus_attrs[] = {
695 &bus_attr_hibernation.attr,
698 static const struct attribute_group vmbus_bus_group = {
699 .attrs = vmbus_bus_attrs,
701 __ATTRIBUTE_GROUPS(vmbus_bus);
704 * vmbus_uevent - add uevent for our device
706 * This routine is invoked when a device is added or removed on the vmbus to
707 * generate a uevent to udev in the userspace. The udev will then look at its
708 * rule and the uevent generated here to load the appropriate driver
710 * The alias string will be of the form vmbus:guid where guid is the string
711 * representation of the device guid (each byte of the guid will be
712 * represented with two hex characters.
714 static int vmbus_uevent(const struct device *device, struct kobj_uevent_env *env)
716 const struct hv_device *dev = device_to_hv_device(device);
717 const char *format = "MODALIAS=vmbus:%*phN";
719 return add_uevent_var(env, format, UUID_SIZE, &dev->dev_type);
722 static const struct hv_vmbus_device_id *
723 hv_vmbus_dev_match(const struct hv_vmbus_device_id *id, const guid_t *guid)
726 return NULL; /* empty device table */
728 for (; !guid_is_null(&id->guid); id++)
729 if (guid_equal(&id->guid, guid))
735 static const struct hv_vmbus_device_id *
736 hv_vmbus_dynid_match(struct hv_driver *drv, const guid_t *guid)
738 const struct hv_vmbus_device_id *id = NULL;
739 struct vmbus_dynid *dynid;
741 spin_lock(&drv->dynids.lock);
742 list_for_each_entry(dynid, &drv->dynids.list, node) {
743 if (guid_equal(&dynid->id.guid, guid)) {
748 spin_unlock(&drv->dynids.lock);
753 static const struct hv_vmbus_device_id vmbus_device_null;
756 * Return a matching hv_vmbus_device_id pointer.
757 * If there is no match, return NULL.
759 static const struct hv_vmbus_device_id *hv_vmbus_get_id(struct hv_driver *drv,
760 struct hv_device *dev)
762 const guid_t *guid = &dev->dev_type;
763 const struct hv_vmbus_device_id *id;
765 /* When driver_override is set, only bind to the matching driver */
766 if (dev->driver_override && strcmp(dev->driver_override, drv->name))
769 /* Look at the dynamic ids first, before the static ones */
770 id = hv_vmbus_dynid_match(drv, guid);
772 id = hv_vmbus_dev_match(drv->id_table, guid);
774 /* driver_override will always match, send a dummy id */
775 if (!id && dev->driver_override)
776 id = &vmbus_device_null;
781 /* vmbus_add_dynid - add a new device ID to this driver and re-probe devices */
782 static int vmbus_add_dynid(struct hv_driver *drv, guid_t *guid)
784 struct vmbus_dynid *dynid;
786 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
790 dynid->id.guid = *guid;
792 spin_lock(&drv->dynids.lock);
793 list_add_tail(&dynid->node, &drv->dynids.list);
794 spin_unlock(&drv->dynids.lock);
796 return driver_attach(&drv->driver);
799 static void vmbus_free_dynids(struct hv_driver *drv)
801 struct vmbus_dynid *dynid, *n;
803 spin_lock(&drv->dynids.lock);
804 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
805 list_del(&dynid->node);
808 spin_unlock(&drv->dynids.lock);
812 * store_new_id - sysfs frontend to vmbus_add_dynid()
814 * Allow GUIDs to be added to an existing driver via sysfs.
816 static ssize_t new_id_store(struct device_driver *driver, const char *buf,
819 struct hv_driver *drv = drv_to_hv_drv(driver);
823 retval = guid_parse(buf, &guid);
827 if (hv_vmbus_dynid_match(drv, &guid))
830 retval = vmbus_add_dynid(drv, &guid);
835 static DRIVER_ATTR_WO(new_id);
838 * store_remove_id - remove a PCI device ID from this driver
840 * Removes a dynamic pci device ID to this driver.
842 static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
845 struct hv_driver *drv = drv_to_hv_drv(driver);
846 struct vmbus_dynid *dynid, *n;
850 retval = guid_parse(buf, &guid);
855 spin_lock(&drv->dynids.lock);
856 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
857 struct hv_vmbus_device_id *id = &dynid->id;
859 if (guid_equal(&id->guid, &guid)) {
860 list_del(&dynid->node);
866 spin_unlock(&drv->dynids.lock);
870 static DRIVER_ATTR_WO(remove_id);
872 static struct attribute *vmbus_drv_attrs[] = {
873 &driver_attr_new_id.attr,
874 &driver_attr_remove_id.attr,
877 ATTRIBUTE_GROUPS(vmbus_drv);
881 * vmbus_match - Attempt to match the specified device to the specified driver
883 static int vmbus_match(struct device *device, struct device_driver *driver)
885 struct hv_driver *drv = drv_to_hv_drv(driver);
886 struct hv_device *hv_dev = device_to_hv_device(device);
888 /* The hv_sock driver handles all hv_sock offers. */
889 if (is_hvsock_channel(hv_dev->channel))
892 if (hv_vmbus_get_id(drv, hv_dev))
899 * vmbus_probe - Add the new vmbus's child device
901 static int vmbus_probe(struct device *child_device)
904 struct hv_driver *drv =
905 drv_to_hv_drv(child_device->driver);
906 struct hv_device *dev = device_to_hv_device(child_device);
907 const struct hv_vmbus_device_id *dev_id;
909 dev_id = hv_vmbus_get_id(drv, dev);
911 ret = drv->probe(dev, dev_id);
913 pr_err("probe failed for device %s (%d)\n",
914 dev_name(child_device), ret);
917 pr_err("probe not set for driver %s\n",
918 dev_name(child_device));
925 * vmbus_dma_configure -- Configure DMA coherence for VMbus device
927 static int vmbus_dma_configure(struct device *child_device)
930 * On ARM64, propagate the DMA coherence setting from the top level
931 * VMbus ACPI device to the child VMbus device being added here.
932 * On x86/x64 coherence is assumed and these calls have no effect.
934 hv_setup_dma_ops(child_device,
935 device_get_dma_attr(&hv_acpi_dev->dev) == DEV_DMA_COHERENT);
940 * vmbus_remove - Remove a vmbus device
942 static void vmbus_remove(struct device *child_device)
944 struct hv_driver *drv;
945 struct hv_device *dev = device_to_hv_device(child_device);
947 if (child_device->driver) {
948 drv = drv_to_hv_drv(child_device->driver);
955 * vmbus_shutdown - Shutdown a vmbus device
957 static void vmbus_shutdown(struct device *child_device)
959 struct hv_driver *drv;
960 struct hv_device *dev = device_to_hv_device(child_device);
963 /* The device may not be attached yet */
964 if (!child_device->driver)
967 drv = drv_to_hv_drv(child_device->driver);
973 #ifdef CONFIG_PM_SLEEP
975 * vmbus_suspend - Suspend a vmbus device
977 static int vmbus_suspend(struct device *child_device)
979 struct hv_driver *drv;
980 struct hv_device *dev = device_to_hv_device(child_device);
982 /* The device may not be attached yet */
983 if (!child_device->driver)
986 drv = drv_to_hv_drv(child_device->driver);
990 return drv->suspend(dev);
994 * vmbus_resume - Resume a vmbus device
996 static int vmbus_resume(struct device *child_device)
998 struct hv_driver *drv;
999 struct hv_device *dev = device_to_hv_device(child_device);
1001 /* The device may not be attached yet */
1002 if (!child_device->driver)
1005 drv = drv_to_hv_drv(child_device->driver);
1009 return drv->resume(dev);
1012 #define vmbus_suspend NULL
1013 #define vmbus_resume NULL
1014 #endif /* CONFIG_PM_SLEEP */
1017 * vmbus_device_release - Final callback release of the vmbus child device
1019 static void vmbus_device_release(struct device *device)
1021 struct hv_device *hv_dev = device_to_hv_device(device);
1022 struct vmbus_channel *channel = hv_dev->channel;
1024 hv_debug_rm_dev_dir(hv_dev);
1026 mutex_lock(&vmbus_connection.channel_mutex);
1027 hv_process_channel_removal(channel);
1028 mutex_unlock(&vmbus_connection.channel_mutex);
1033 * Note: we must use the "noirq" ops: see the comment before vmbus_bus_pm.
1035 * suspend_noirq/resume_noirq are set to NULL to support Suspend-to-Idle: we
1036 * shouldn't suspend the vmbus devices upon Suspend-to-Idle, otherwise there
1037 * is no way to wake up a Generation-2 VM.
1039 * The other 4 ops are for hibernation.
1042 static const struct dev_pm_ops vmbus_pm = {
1043 .suspend_noirq = NULL,
1044 .resume_noirq = NULL,
1045 .freeze_noirq = vmbus_suspend,
1046 .thaw_noirq = vmbus_resume,
1047 .poweroff_noirq = vmbus_suspend,
1048 .restore_noirq = vmbus_resume,
1051 /* The one and only one */
1052 static struct bus_type hv_bus = {
1054 .match = vmbus_match,
1055 .shutdown = vmbus_shutdown,
1056 .remove = vmbus_remove,
1057 .probe = vmbus_probe,
1058 .uevent = vmbus_uevent,
1059 .dma_configure = vmbus_dma_configure,
1060 .dev_groups = vmbus_dev_groups,
1061 .drv_groups = vmbus_drv_groups,
1062 .bus_groups = vmbus_bus_groups,
1066 struct onmessage_work_context {
1067 struct work_struct work;
1069 struct hv_message_header header;
1074 static void vmbus_onmessage_work(struct work_struct *work)
1076 struct onmessage_work_context *ctx;
1078 /* Do not process messages if we're in DISCONNECTED state */
1079 if (vmbus_connection.conn_state == DISCONNECTED)
1082 ctx = container_of(work, struct onmessage_work_context,
1084 vmbus_onmessage((struct vmbus_channel_message_header *)
1089 void vmbus_on_msg_dpc(unsigned long data)
1091 struct hv_per_cpu_context *hv_cpu = (void *)data;
1092 void *page_addr = hv_cpu->synic_message_page;
1093 struct hv_message msg_copy, *msg = (struct hv_message *)page_addr +
1095 struct vmbus_channel_message_header *hdr;
1096 enum vmbus_channel_message_type msgtype;
1097 const struct vmbus_channel_message_table_entry *entry;
1098 struct onmessage_work_context *ctx;
1103 * 'enum vmbus_channel_message_type' is supposed to always be 'u32' as
1104 * it is being used in 'struct vmbus_channel_message_header' definition
1105 * which is supposed to match hypervisor ABI.
1107 BUILD_BUG_ON(sizeof(enum vmbus_channel_message_type) != sizeof(u32));
1110 * Since the message is in memory shared with the host, an erroneous or
1111 * malicious Hyper-V could modify the message while vmbus_on_msg_dpc()
1112 * or individual message handlers are executing; to prevent this, copy
1113 * the message into private memory.
1115 memcpy(&msg_copy, msg, sizeof(struct hv_message));
1117 message_type = msg_copy.header.message_type;
1118 if (message_type == HVMSG_NONE)
1122 hdr = (struct vmbus_channel_message_header *)msg_copy.u.payload;
1123 msgtype = hdr->msgtype;
1125 trace_vmbus_on_msg_dpc(hdr);
1127 if (msgtype >= CHANNELMSG_COUNT) {
1128 WARN_ONCE(1, "unknown msgtype=%d\n", msgtype);
1132 payload_size = msg_copy.header.payload_size;
1133 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT) {
1134 WARN_ONCE(1, "payload size is too large (%d)\n", payload_size);
1138 entry = &channel_message_table[msgtype];
1140 if (!entry->message_handler)
1143 if (payload_size < entry->min_payload_len) {
1144 WARN_ONCE(1, "message too short: msgtype=%d len=%d\n", msgtype, payload_size);
1148 if (entry->handler_type == VMHT_BLOCKING) {
1149 ctx = kmalloc(struct_size(ctx, msg.payload, payload_size), GFP_ATOMIC);
1153 INIT_WORK(&ctx->work, vmbus_onmessage_work);
1154 ctx->msg.header = msg_copy.header;
1155 memcpy(&ctx->msg.payload, msg_copy.u.payload, payload_size);
1158 * The host can generate a rescind message while we
1159 * may still be handling the original offer. We deal with
1160 * this condition by relying on the synchronization provided
1161 * by offer_in_progress and by channel_mutex. See also the
1162 * inline comments in vmbus_onoffer_rescind().
1165 case CHANNELMSG_RESCIND_CHANNELOFFER:
1167 * If we are handling the rescind message;
1168 * schedule the work on the global work queue.
1170 * The OFFER message and the RESCIND message should
1171 * not be handled by the same serialized work queue,
1172 * because the OFFER handler may call vmbus_open(),
1173 * which tries to open the channel by sending an
1174 * OPEN_CHANNEL message to the host and waits for
1175 * the host's response; however, if the host has
1176 * rescinded the channel before it receives the
1177 * OPEN_CHANNEL message, the host just silently
1178 * ignores the OPEN_CHANNEL message; as a result,
1179 * the guest's OFFER handler hangs for ever, if we
1180 * handle the RESCIND message in the same serialized
1181 * work queue: the RESCIND handler can not start to
1182 * run before the OFFER handler finishes.
1184 if (vmbus_connection.ignore_any_offer_msg)
1186 queue_work(vmbus_connection.rescind_work_queue, &ctx->work);
1189 case CHANNELMSG_OFFERCHANNEL:
1191 * The host sends the offer message of a given channel
1192 * before sending the rescind message of the same
1193 * channel. These messages are sent to the guest's
1194 * connect CPU; the guest then starts processing them
1195 * in the tasklet handler on this CPU:
1199 * [vmbus_on_msg_dpc()]
1200 * atomic_inc() // CHANNELMSG_OFFERCHANNEL
1203 * [vmbus_on_msg_dpc()]
1204 * schedule_work() // CHANNELMSG_RESCIND_CHANNELOFFER
1206 * We rely on the memory-ordering properties of the
1207 * queue_work() and schedule_work() primitives, which
1208 * guarantee that the atomic increment will be visible
1209 * to the CPUs which will execute the offer & rescind
1210 * works by the time these works will start execution.
1212 if (vmbus_connection.ignore_any_offer_msg)
1214 atomic_inc(&vmbus_connection.offer_in_progress);
1218 queue_work(vmbus_connection.work_queue, &ctx->work);
1221 entry->message_handler(hdr);
1224 vmbus_signal_eom(msg, message_type);
1227 #ifdef CONFIG_PM_SLEEP
1229 * Fake RESCIND_CHANNEL messages to clean up hv_sock channels by force for
1230 * hibernation, because hv_sock connections can not persist across hibernation.
1232 static void vmbus_force_channel_rescinded(struct vmbus_channel *channel)
1234 struct onmessage_work_context *ctx;
1235 struct vmbus_channel_rescind_offer *rescind;
1237 WARN_ON(!is_hvsock_channel(channel));
1240 * Allocation size is small and the allocation should really not fail,
1241 * otherwise the state of the hv_sock connections ends up in limbo.
1243 ctx = kzalloc(sizeof(*ctx) + sizeof(*rescind),
1244 GFP_KERNEL | __GFP_NOFAIL);
1247 * So far, these are not really used by Linux. Just set them to the
1248 * reasonable values conforming to the definitions of the fields.
1250 ctx->msg.header.message_type = 1;
1251 ctx->msg.header.payload_size = sizeof(*rescind);
1253 /* These values are actually used by Linux. */
1254 rescind = (struct vmbus_channel_rescind_offer *)ctx->msg.payload;
1255 rescind->header.msgtype = CHANNELMSG_RESCIND_CHANNELOFFER;
1256 rescind->child_relid = channel->offermsg.child_relid;
1258 INIT_WORK(&ctx->work, vmbus_onmessage_work);
1260 queue_work(vmbus_connection.work_queue, &ctx->work);
1262 #endif /* CONFIG_PM_SLEEP */
1265 * Schedule all channels with events pending
1267 static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu)
1269 unsigned long *recv_int_page;
1273 * The event page can be directly checked to get the id of
1274 * the channel that has the interrupt pending.
1276 void *page_addr = hv_cpu->synic_event_page;
1277 union hv_synic_event_flags *event
1278 = (union hv_synic_event_flags *)page_addr +
1281 maxbits = HV_EVENT_FLAGS_COUNT;
1282 recv_int_page = event->flags;
1284 if (unlikely(!recv_int_page))
1287 for_each_set_bit(relid, recv_int_page, maxbits) {
1288 void (*callback_fn)(void *context);
1289 struct vmbus_channel *channel;
1291 if (!sync_test_and_clear_bit(relid, recv_int_page))
1294 /* Special case - vmbus channel protocol msg */
1299 * Pairs with the kfree_rcu() in vmbus_chan_release().
1300 * Guarantees that the channel data structure doesn't
1301 * get freed while the channel pointer below is being
1306 /* Find channel based on relid */
1307 channel = relid2channel(relid);
1308 if (channel == NULL)
1309 goto sched_unlock_rcu;
1311 if (channel->rescind)
1312 goto sched_unlock_rcu;
1315 * Make sure that the ring buffer data structure doesn't get
1316 * freed while we dereference the ring buffer pointer. Test
1317 * for the channel's onchannel_callback being NULL within a
1318 * sched_lock critical section. See also the inline comments
1319 * in vmbus_reset_channel_cb().
1321 spin_lock(&channel->sched_lock);
1323 callback_fn = channel->onchannel_callback;
1324 if (unlikely(callback_fn == NULL))
1327 trace_vmbus_chan_sched(channel);
1329 ++channel->interrupts;
1331 switch (channel->callback_mode) {
1333 (*callback_fn)(channel->channel_callback_context);
1336 case HV_CALL_BATCHED:
1337 hv_begin_read(&channel->inbound);
1339 case HV_CALL_DIRECT:
1340 tasklet_schedule(&channel->callback_event);
1344 spin_unlock(&channel->sched_lock);
1350 static void vmbus_isr(void)
1352 struct hv_per_cpu_context *hv_cpu
1353 = this_cpu_ptr(hv_context.cpu_context);
1355 struct hv_message *msg;
1357 vmbus_chan_sched(hv_cpu);
1359 page_addr = hv_cpu->synic_message_page;
1360 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
1362 /* Check if there are actual msgs to be processed */
1363 if (msg->header.message_type != HVMSG_NONE) {
1364 if (msg->header.message_type == HVMSG_TIMER_EXPIRED) {
1366 vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED);
1368 tasklet_schedule(&hv_cpu->msg_dpc);
1371 add_interrupt_randomness(vmbus_interrupt);
1374 static irqreturn_t vmbus_percpu_isr(int irq, void *dev_id)
1381 * Callback from kmsg_dump. Grab as much as possible from the end of the kmsg
1382 * buffer and call into Hyper-V to transfer the data.
1384 static void hv_kmsg_dump(struct kmsg_dumper *dumper,
1385 enum kmsg_dump_reason reason)
1387 struct kmsg_dump_iter iter;
1388 size_t bytes_written;
1390 /* We are only interested in panics. */
1391 if ((reason != KMSG_DUMP_PANIC) || (!sysctl_record_panic_msg))
1395 * Write dump contents to the page. No need to synchronize; panic should
1396 * be single-threaded.
1398 kmsg_dump_rewind(&iter);
1399 kmsg_dump_get_buffer(&iter, false, hv_panic_page, HV_HYP_PAGE_SIZE,
1404 * P3 to contain the physical address of the panic page & P4 to
1405 * contain the size of the panic data in that page. Rest of the
1406 * registers are no-op when the NOTIFY_MSG flag is set.
1408 hv_set_register(HV_REGISTER_CRASH_P0, 0);
1409 hv_set_register(HV_REGISTER_CRASH_P1, 0);
1410 hv_set_register(HV_REGISTER_CRASH_P2, 0);
1411 hv_set_register(HV_REGISTER_CRASH_P3, virt_to_phys(hv_panic_page));
1412 hv_set_register(HV_REGISTER_CRASH_P4, bytes_written);
1415 * Let Hyper-V know there is crash data available along with
1416 * the panic message.
1418 hv_set_register(HV_REGISTER_CRASH_CTL,
1419 (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG));
1422 static struct kmsg_dumper hv_kmsg_dumper = {
1423 .dump = hv_kmsg_dump,
1426 static void hv_kmsg_dump_register(void)
1430 hv_panic_page = hv_alloc_hyperv_zeroed_page();
1431 if (!hv_panic_page) {
1432 pr_err("Hyper-V: panic message page memory allocation failed\n");
1436 ret = kmsg_dump_register(&hv_kmsg_dumper);
1438 pr_err("Hyper-V: kmsg dump register error 0x%x\n", ret);
1439 hv_free_hyperv_page((unsigned long)hv_panic_page);
1440 hv_panic_page = NULL;
1444 static struct ctl_table_header *hv_ctl_table_hdr;
1447 * sysctl option to allow the user to control whether kmsg data should be
1448 * reported to Hyper-V on panic.
1450 static struct ctl_table hv_ctl_table[] = {
1452 .procname = "hyperv_record_panic_msg",
1453 .data = &sysctl_record_panic_msg,
1454 .maxlen = sizeof(int),
1456 .proc_handler = proc_dointvec_minmax,
1457 .extra1 = SYSCTL_ZERO,
1458 .extra2 = SYSCTL_ONE
1463 static struct ctl_table hv_root_table[] = {
1465 .procname = "kernel",
1467 .child = hv_ctl_table
1473 * vmbus_bus_init -Main vmbus driver initialization routine.
1476 * - initialize the vmbus driver context
1477 * - invoke the vmbus hv main init routine
1478 * - retrieve the channel offers
1480 static int vmbus_bus_init(void)
1486 pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
1490 ret = bus_register(&hv_bus);
1495 * VMbus interrupts are best modeled as per-cpu interrupts. If
1496 * on an architecture with support for per-cpu IRQs (e.g. ARM64),
1497 * allocate a per-cpu IRQ using standard Linux kernel functionality.
1498 * If not on such an architecture (e.g., x86/x64), then rely on
1499 * code in the arch-specific portion of the code tree to connect
1500 * the VMbus interrupt handler.
1503 if (vmbus_irq == -1) {
1504 hv_setup_vmbus_handler(vmbus_isr);
1506 vmbus_evt = alloc_percpu(long);
1507 ret = request_percpu_irq(vmbus_irq, vmbus_percpu_isr,
1508 "Hyper-V VMbus", vmbus_evt);
1510 pr_err("Can't request Hyper-V VMbus IRQ %d, Err %d",
1512 free_percpu(vmbus_evt);
1517 ret = hv_synic_alloc();
1522 * Initialize the per-cpu interrupt state and stimer state.
1523 * Then connect to the host.
1525 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hyperv/vmbus:online",
1526 hv_synic_init, hv_synic_cleanup);
1529 hyperv_cpuhp_online = ret;
1531 ret = vmbus_connect();
1535 if (hv_is_isolation_supported())
1536 sysctl_record_panic_msg = 0;
1539 * Only register if the crash MSRs are available
1541 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
1542 u64 hyperv_crash_ctl;
1544 * Panic message recording (sysctl_record_panic_msg)
1545 * is enabled by default in non-isolated guests and
1546 * disabled by default in isolated guests; the panic
1547 * message recording won't be available in isolated
1548 * guests should the following registration fail.
1550 hv_ctl_table_hdr = register_sysctl_table(hv_root_table);
1551 if (!hv_ctl_table_hdr)
1552 pr_err("Hyper-V: sysctl table register error");
1555 * Register for panic kmsg callback only if the right
1556 * capability is supported by the hypervisor.
1558 hyperv_crash_ctl = hv_get_register(HV_REGISTER_CRASH_CTL);
1559 if (hyperv_crash_ctl & HV_CRASH_CTL_CRASH_NOTIFY_MSG)
1560 hv_kmsg_dump_register();
1562 register_die_notifier(&hyperv_die_report_block);
1563 atomic_notifier_chain_register(&panic_notifier_list,
1564 &hyperv_panic_report_block);
1568 * Always register the vmbus unload panic notifier because we
1569 * need to shut the VMbus channel connection on panic.
1571 atomic_notifier_chain_register(&panic_notifier_list,
1572 &hyperv_panic_vmbus_unload_block);
1574 vmbus_request_offers();
1579 cpuhp_remove_state(hyperv_cpuhp_online);
1583 if (vmbus_irq == -1) {
1584 hv_remove_vmbus_handler();
1586 free_percpu_irq(vmbus_irq, vmbus_evt);
1587 free_percpu(vmbus_evt);
1590 bus_unregister(&hv_bus);
1591 unregister_sysctl_table(hv_ctl_table_hdr);
1592 hv_ctl_table_hdr = NULL;
1597 * __vmbus_driver_register() - Register a vmbus's driver
1598 * @hv_driver: Pointer to driver structure you want to register
1599 * @owner: owner module of the drv
1600 * @mod_name: module name string
1602 * Registers the given driver with Linux through the 'driver_register()' call
1603 * and sets up the hyper-v vmbus handling for this driver.
1604 * It will return the state of the 'driver_register()' call.
1607 int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
1611 pr_info("registering driver %s\n", hv_driver->name);
1613 ret = vmbus_exists();
1617 hv_driver->driver.name = hv_driver->name;
1618 hv_driver->driver.owner = owner;
1619 hv_driver->driver.mod_name = mod_name;
1620 hv_driver->driver.bus = &hv_bus;
1622 spin_lock_init(&hv_driver->dynids.lock);
1623 INIT_LIST_HEAD(&hv_driver->dynids.list);
1625 ret = driver_register(&hv_driver->driver);
1629 EXPORT_SYMBOL_GPL(__vmbus_driver_register);
1632 * vmbus_driver_unregister() - Unregister a vmbus's driver
1633 * @hv_driver: Pointer to driver structure you want to
1636 * Un-register the given driver that was previous registered with a call to
1637 * vmbus_driver_register()
1639 void vmbus_driver_unregister(struct hv_driver *hv_driver)
1641 pr_info("unregistering driver %s\n", hv_driver->name);
1643 if (!vmbus_exists()) {
1644 driver_unregister(&hv_driver->driver);
1645 vmbus_free_dynids(hv_driver);
1648 EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
1652 * Called when last reference to channel is gone.
1654 static void vmbus_chan_release(struct kobject *kobj)
1656 struct vmbus_channel *channel
1657 = container_of(kobj, struct vmbus_channel, kobj);
1659 kfree_rcu(channel, rcu);
1662 struct vmbus_chan_attribute {
1663 struct attribute attr;
1664 ssize_t (*show)(struct vmbus_channel *chan, char *buf);
1665 ssize_t (*store)(struct vmbus_channel *chan,
1666 const char *buf, size_t count);
1668 #define VMBUS_CHAN_ATTR(_name, _mode, _show, _store) \
1669 struct vmbus_chan_attribute chan_attr_##_name \
1670 = __ATTR(_name, _mode, _show, _store)
1671 #define VMBUS_CHAN_ATTR_RW(_name) \
1672 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RW(_name)
1673 #define VMBUS_CHAN_ATTR_RO(_name) \
1674 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RO(_name)
1675 #define VMBUS_CHAN_ATTR_WO(_name) \
1676 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_WO(_name)
1678 static ssize_t vmbus_chan_attr_show(struct kobject *kobj,
1679 struct attribute *attr, char *buf)
1681 const struct vmbus_chan_attribute *attribute
1682 = container_of(attr, struct vmbus_chan_attribute, attr);
1683 struct vmbus_channel *chan
1684 = container_of(kobj, struct vmbus_channel, kobj);
1686 if (!attribute->show)
1689 return attribute->show(chan, buf);
1692 static ssize_t vmbus_chan_attr_store(struct kobject *kobj,
1693 struct attribute *attr, const char *buf,
1696 const struct vmbus_chan_attribute *attribute
1697 = container_of(attr, struct vmbus_chan_attribute, attr);
1698 struct vmbus_channel *chan
1699 = container_of(kobj, struct vmbus_channel, kobj);
1701 if (!attribute->store)
1704 return attribute->store(chan, buf, count);
1707 static const struct sysfs_ops vmbus_chan_sysfs_ops = {
1708 .show = vmbus_chan_attr_show,
1709 .store = vmbus_chan_attr_store,
1712 static ssize_t out_mask_show(struct vmbus_channel *channel, char *buf)
1714 struct hv_ring_buffer_info *rbi = &channel->outbound;
1717 mutex_lock(&rbi->ring_buffer_mutex);
1718 if (!rbi->ring_buffer) {
1719 mutex_unlock(&rbi->ring_buffer_mutex);
1723 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1724 mutex_unlock(&rbi->ring_buffer_mutex);
1727 static VMBUS_CHAN_ATTR_RO(out_mask);
1729 static ssize_t in_mask_show(struct vmbus_channel *channel, char *buf)
1731 struct hv_ring_buffer_info *rbi = &channel->inbound;
1734 mutex_lock(&rbi->ring_buffer_mutex);
1735 if (!rbi->ring_buffer) {
1736 mutex_unlock(&rbi->ring_buffer_mutex);
1740 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1741 mutex_unlock(&rbi->ring_buffer_mutex);
1744 static VMBUS_CHAN_ATTR_RO(in_mask);
1746 static ssize_t read_avail_show(struct vmbus_channel *channel, char *buf)
1748 struct hv_ring_buffer_info *rbi = &channel->inbound;
1751 mutex_lock(&rbi->ring_buffer_mutex);
1752 if (!rbi->ring_buffer) {
1753 mutex_unlock(&rbi->ring_buffer_mutex);
1757 ret = sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi));
1758 mutex_unlock(&rbi->ring_buffer_mutex);
1761 static VMBUS_CHAN_ATTR_RO(read_avail);
1763 static ssize_t write_avail_show(struct vmbus_channel *channel, char *buf)
1765 struct hv_ring_buffer_info *rbi = &channel->outbound;
1768 mutex_lock(&rbi->ring_buffer_mutex);
1769 if (!rbi->ring_buffer) {
1770 mutex_unlock(&rbi->ring_buffer_mutex);
1774 ret = sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi));
1775 mutex_unlock(&rbi->ring_buffer_mutex);
1778 static VMBUS_CHAN_ATTR_RO(write_avail);
1780 static ssize_t target_cpu_show(struct vmbus_channel *channel, char *buf)
1782 return sprintf(buf, "%u\n", channel->target_cpu);
1784 static ssize_t target_cpu_store(struct vmbus_channel *channel,
1785 const char *buf, size_t count)
1787 u32 target_cpu, origin_cpu;
1788 ssize_t ret = count;
1790 if (vmbus_proto_version < VERSION_WIN10_V4_1)
1793 if (sscanf(buf, "%uu", &target_cpu) != 1)
1796 /* Validate target_cpu for the cpumask_test_cpu() operation below. */
1797 if (target_cpu >= nr_cpumask_bits)
1800 if (!cpumask_test_cpu(target_cpu, housekeeping_cpumask(HK_TYPE_MANAGED_IRQ)))
1803 /* No CPUs should come up or down during this. */
1806 if (!cpu_online(target_cpu)) {
1812 * Synchronizes target_cpu_store() and channel closure:
1814 * { Initially: state = CHANNEL_OPENED }
1818 * [target_cpu_store()] [vmbus_disconnect_ring()]
1820 * LOCK channel_mutex LOCK channel_mutex
1821 * LOAD r1 = state LOAD r2 = state
1822 * IF (r1 == CHANNEL_OPENED) IF (r2 == CHANNEL_OPENED)
1823 * SEND MODIFYCHANNEL STORE state = CHANNEL_OPEN
1824 * [...] SEND CLOSECHANNEL
1825 * UNLOCK channel_mutex UNLOCK channel_mutex
1827 * Forbids: r1 == r2 == CHANNEL_OPENED (i.e., CPU1's LOCK precedes
1828 * CPU2's LOCK) && CPU2's SEND precedes CPU1's SEND
1830 * Note. The host processes the channel messages "sequentially", in
1831 * the order in which they are received on a per-partition basis.
1833 mutex_lock(&vmbus_connection.channel_mutex);
1836 * Hyper-V will ignore MODIFYCHANNEL messages for "non-open" channels;
1837 * avoid sending the message and fail here for such channels.
1839 if (channel->state != CHANNEL_OPENED_STATE) {
1841 goto cpu_store_unlock;
1844 origin_cpu = channel->target_cpu;
1845 if (target_cpu == origin_cpu)
1846 goto cpu_store_unlock;
1848 if (vmbus_send_modifychannel(channel,
1849 hv_cpu_number_to_vp_number(target_cpu))) {
1851 goto cpu_store_unlock;
1855 * For version before VERSION_WIN10_V5_3, the following warning holds:
1857 * Warning. At this point, there is *no* guarantee that the host will
1858 * have successfully processed the vmbus_send_modifychannel() request.
1859 * See the header comment of vmbus_send_modifychannel() for more info.
1861 * Lags in the processing of the above vmbus_send_modifychannel() can
1862 * result in missed interrupts if the "old" target CPU is taken offline
1863 * before Hyper-V starts sending interrupts to the "new" target CPU.
1864 * But apart from this offlining scenario, the code tolerates such
1865 * lags. It will function correctly even if a channel interrupt comes
1866 * in on a CPU that is different from the channel target_cpu value.
1869 channel->target_cpu = target_cpu;
1871 /* See init_vp_index(). */
1872 if (hv_is_perf_channel(channel))
1873 hv_update_allocated_cpus(origin_cpu, target_cpu);
1875 /* Currently set only for storvsc channels. */
1876 if (channel->change_target_cpu_callback) {
1877 (*channel->change_target_cpu_callback)(channel,
1878 origin_cpu, target_cpu);
1882 mutex_unlock(&vmbus_connection.channel_mutex);
1886 static VMBUS_CHAN_ATTR(cpu, 0644, target_cpu_show, target_cpu_store);
1888 static ssize_t channel_pending_show(struct vmbus_channel *channel,
1891 return sprintf(buf, "%d\n",
1892 channel_pending(channel,
1893 vmbus_connection.monitor_pages[1]));
1895 static VMBUS_CHAN_ATTR(pending, 0444, channel_pending_show, NULL);
1897 static ssize_t channel_latency_show(struct vmbus_channel *channel,
1900 return sprintf(buf, "%d\n",
1901 channel_latency(channel,
1902 vmbus_connection.monitor_pages[1]));
1904 static VMBUS_CHAN_ATTR(latency, 0444, channel_latency_show, NULL);
1906 static ssize_t channel_interrupts_show(struct vmbus_channel *channel, char *buf)
1908 return sprintf(buf, "%llu\n", channel->interrupts);
1910 static VMBUS_CHAN_ATTR(interrupts, 0444, channel_interrupts_show, NULL);
1912 static ssize_t channel_events_show(struct vmbus_channel *channel, char *buf)
1914 return sprintf(buf, "%llu\n", channel->sig_events);
1916 static VMBUS_CHAN_ATTR(events, 0444, channel_events_show, NULL);
1918 static ssize_t channel_intr_in_full_show(struct vmbus_channel *channel,
1921 return sprintf(buf, "%llu\n",
1922 (unsigned long long)channel->intr_in_full);
1924 static VMBUS_CHAN_ATTR(intr_in_full, 0444, channel_intr_in_full_show, NULL);
1926 static ssize_t channel_intr_out_empty_show(struct vmbus_channel *channel,
1929 return sprintf(buf, "%llu\n",
1930 (unsigned long long)channel->intr_out_empty);
1932 static VMBUS_CHAN_ATTR(intr_out_empty, 0444, channel_intr_out_empty_show, NULL);
1934 static ssize_t channel_out_full_first_show(struct vmbus_channel *channel,
1937 return sprintf(buf, "%llu\n",
1938 (unsigned long long)channel->out_full_first);
1940 static VMBUS_CHAN_ATTR(out_full_first, 0444, channel_out_full_first_show, NULL);
1942 static ssize_t channel_out_full_total_show(struct vmbus_channel *channel,
1945 return sprintf(buf, "%llu\n",
1946 (unsigned long long)channel->out_full_total);
1948 static VMBUS_CHAN_ATTR(out_full_total, 0444, channel_out_full_total_show, NULL);
1950 static ssize_t subchannel_monitor_id_show(struct vmbus_channel *channel,
1953 return sprintf(buf, "%u\n", channel->offermsg.monitorid);
1955 static VMBUS_CHAN_ATTR(monitor_id, 0444, subchannel_monitor_id_show, NULL);
1957 static ssize_t subchannel_id_show(struct vmbus_channel *channel,
1960 return sprintf(buf, "%u\n",
1961 channel->offermsg.offer.sub_channel_index);
1963 static VMBUS_CHAN_ATTR_RO(subchannel_id);
1965 static struct attribute *vmbus_chan_attrs[] = {
1966 &chan_attr_out_mask.attr,
1967 &chan_attr_in_mask.attr,
1968 &chan_attr_read_avail.attr,
1969 &chan_attr_write_avail.attr,
1970 &chan_attr_cpu.attr,
1971 &chan_attr_pending.attr,
1972 &chan_attr_latency.attr,
1973 &chan_attr_interrupts.attr,
1974 &chan_attr_events.attr,
1975 &chan_attr_intr_in_full.attr,
1976 &chan_attr_intr_out_empty.attr,
1977 &chan_attr_out_full_first.attr,
1978 &chan_attr_out_full_total.attr,
1979 &chan_attr_monitor_id.attr,
1980 &chan_attr_subchannel_id.attr,
1985 * Channel-level attribute_group callback function. Returns the permission for
1986 * each attribute, and returns 0 if an attribute is not visible.
1988 static umode_t vmbus_chan_attr_is_visible(struct kobject *kobj,
1989 struct attribute *attr, int idx)
1991 const struct vmbus_channel *channel =
1992 container_of(kobj, struct vmbus_channel, kobj);
1994 /* Hide the monitor attributes if the monitor mechanism is not used. */
1995 if (!channel->offermsg.monitor_allocated &&
1996 (attr == &chan_attr_pending.attr ||
1997 attr == &chan_attr_latency.attr ||
1998 attr == &chan_attr_monitor_id.attr))
2004 static struct attribute_group vmbus_chan_group = {
2005 .attrs = vmbus_chan_attrs,
2006 .is_visible = vmbus_chan_attr_is_visible
2009 static struct kobj_type vmbus_chan_ktype = {
2010 .sysfs_ops = &vmbus_chan_sysfs_ops,
2011 .release = vmbus_chan_release,
2015 * vmbus_add_channel_kobj - setup a sub-directory under device/channels
2017 int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
2019 const struct device *device = &dev->device;
2020 struct kobject *kobj = &channel->kobj;
2021 u32 relid = channel->offermsg.child_relid;
2024 kobj->kset = dev->channels_kset;
2025 ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
2032 ret = sysfs_create_group(kobj, &vmbus_chan_group);
2036 * The calling functions' error handling paths will cleanup the
2037 * empty channel directory.
2040 dev_err(device, "Unable to set up channel sysfs files\n");
2044 kobject_uevent(kobj, KOBJ_ADD);
2050 * vmbus_remove_channel_attr_group - remove the channel's attribute group
2052 void vmbus_remove_channel_attr_group(struct vmbus_channel *channel)
2054 sysfs_remove_group(&channel->kobj, &vmbus_chan_group);
2058 * vmbus_device_create - Creates and registers a new child device
2061 struct hv_device *vmbus_device_create(const guid_t *type,
2062 const guid_t *instance,
2063 struct vmbus_channel *channel)
2065 struct hv_device *child_device_obj;
2067 child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
2068 if (!child_device_obj) {
2069 pr_err("Unable to allocate device object for child device\n");
2073 child_device_obj->channel = channel;
2074 guid_copy(&child_device_obj->dev_type, type);
2075 guid_copy(&child_device_obj->dev_instance, instance);
2076 child_device_obj->vendor_id = PCI_VENDOR_ID_MICROSOFT;
2078 return child_device_obj;
2082 * vmbus_device_register - Register the child device
2084 int vmbus_device_register(struct hv_device *child_device_obj)
2086 struct kobject *kobj = &child_device_obj->device.kobj;
2089 dev_set_name(&child_device_obj->device, "%pUl",
2090 &child_device_obj->channel->offermsg.offer.if_instance);
2092 child_device_obj->device.bus = &hv_bus;
2093 child_device_obj->device.parent = &hv_acpi_dev->dev;
2094 child_device_obj->device.release = vmbus_device_release;
2096 child_device_obj->device.dma_parms = &child_device_obj->dma_parms;
2097 child_device_obj->device.dma_mask = &child_device_obj->dma_mask;
2098 dma_set_mask(&child_device_obj->device, DMA_BIT_MASK(64));
2101 * Register with the LDM. This will kick off the driver/device
2102 * binding...which will eventually call vmbus_match() and vmbus_probe()
2104 ret = device_register(&child_device_obj->device);
2106 pr_err("Unable to register child device\n");
2107 put_device(&child_device_obj->device);
2111 child_device_obj->channels_kset = kset_create_and_add("channels",
2113 if (!child_device_obj->channels_kset) {
2115 goto err_dev_unregister;
2118 ret = vmbus_add_channel_kobj(child_device_obj,
2119 child_device_obj->channel);
2121 pr_err("Unable to register primary channeln");
2122 goto err_kset_unregister;
2124 hv_debug_add_dev_dir(child_device_obj);
2128 err_kset_unregister:
2129 kset_unregister(child_device_obj->channels_kset);
2132 device_unregister(&child_device_obj->device);
2137 * vmbus_device_unregister - Remove the specified child device
2140 void vmbus_device_unregister(struct hv_device *device_obj)
2142 pr_debug("child device %s unregistered\n",
2143 dev_name(&device_obj->device));
2145 kset_unregister(device_obj->channels_kset);
2148 * Kick off the process of unregistering the device.
2149 * This will call vmbus_remove() and eventually vmbus_device_release()
2151 device_unregister(&device_obj->device);
2156 * VMBUS is an acpi enumerated device. Get the information we
2159 #define VTPM_BASE_ADDRESS 0xfed40000
2160 static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
2162 resource_size_t start = 0;
2163 resource_size_t end = 0;
2164 struct resource *new_res;
2165 struct resource **old_res = &hyperv_mmio;
2166 struct resource **prev_res = NULL;
2169 switch (res->type) {
2172 * "Address" descriptors are for bus windows. Ignore
2173 * "memory" descriptors, which are for registers on
2176 case ACPI_RESOURCE_TYPE_ADDRESS32:
2177 start = res->data.address32.address.minimum;
2178 end = res->data.address32.address.maximum;
2181 case ACPI_RESOURCE_TYPE_ADDRESS64:
2182 start = res->data.address64.address.minimum;
2183 end = res->data.address64.address.maximum;
2187 * The IRQ information is needed only on ARM64, which Hyper-V
2188 * sets up in the extended format. IRQ information is present
2189 * on x86/x64 in the non-extended format but it is not used by
2190 * Linux. So don't bother checking for the non-extended format.
2192 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
2193 if (!acpi_dev_resource_interrupt(res, 0, &r)) {
2194 pr_err("Unable to parse Hyper-V ACPI interrupt\n");
2197 /* ARM64 INTID for VMbus */
2198 vmbus_interrupt = res->data.extended_irq.interrupts[0];
2199 /* Linux IRQ number */
2200 vmbus_irq = r.start;
2204 /* Unused resource type */
2209 * Ignore ranges that are below 1MB, as they're not
2210 * necessary or useful here.
2215 new_res = kzalloc(sizeof(*new_res), GFP_ATOMIC);
2217 return AE_NO_MEMORY;
2219 /* If this range overlaps the virtual TPM, truncate it. */
2220 if (end > VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS)
2221 end = VTPM_BASE_ADDRESS;
2223 new_res->name = "hyperv mmio";
2224 new_res->flags = IORESOURCE_MEM;
2225 new_res->start = start;
2229 * If two ranges are adjacent, merge them.
2237 if (((*old_res)->end + 1) == new_res->start) {
2238 (*old_res)->end = new_res->end;
2243 if ((*old_res)->start == new_res->end + 1) {
2244 (*old_res)->start = new_res->start;
2249 if ((*old_res)->start > new_res->end) {
2250 new_res->sibling = *old_res;
2252 (*prev_res)->sibling = new_res;
2258 old_res = &(*old_res)->sibling;
2265 static void vmbus_acpi_remove(struct acpi_device *device)
2267 struct resource *cur_res;
2268 struct resource *next_res;
2272 __release_region(hyperv_mmio, fb_mmio->start,
2273 resource_size(fb_mmio));
2277 for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) {
2278 next_res = cur_res->sibling;
2284 static void vmbus_reserve_fb(void)
2286 resource_size_t start = 0, size;
2287 struct pci_dev *pdev;
2289 if (efi_enabled(EFI_BOOT)) {
2290 /* Gen2 VM: get FB base from EFI framebuffer */
2291 start = screen_info.lfb_base;
2292 size = max_t(__u32, screen_info.lfb_size, 0x800000);
2294 /* Gen1 VM: get FB base from PCI */
2295 pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
2296 PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
2300 if (pdev->resource[0].flags & IORESOURCE_MEM) {
2301 start = pci_resource_start(pdev, 0);
2302 size = pci_resource_len(pdev, 0);
2306 * Release the PCI device so hyperv_drm or hyperv_fb driver can
2316 * Make a claim for the frame buffer in the resource tree under the
2317 * first node, which will be the one below 4GB. The length seems to
2318 * be underreported, particularly in a Generation 1 VM. So start out
2319 * reserving a larger area and make it smaller until it succeeds.
2321 for (; !fb_mmio && (size >= 0x100000); size >>= 1)
2322 fb_mmio = __request_region(hyperv_mmio, start, size, fb_mmio_name, 0);
2326 * vmbus_allocate_mmio() - Pick a memory-mapped I/O range.
2327 * @new: If successful, supplied a pointer to the
2328 * allocated MMIO space.
2329 * @device_obj: Identifies the caller
2330 * @min: Minimum guest physical address of the
2332 * @max: Maximum guest physical address
2333 * @size: Size of the range to be allocated
2334 * @align: Alignment of the range to be allocated
2335 * @fb_overlap_ok: Whether this allocation can be allowed
2336 * to overlap the video frame buffer.
2338 * This function walks the resources granted to VMBus by the
2339 * _CRS object in the ACPI namespace underneath the parent
2340 * "bridge" whether that's a root PCI bus in the Generation 1
2341 * case or a Module Device in the Generation 2 case. It then
2342 * attempts to allocate from the global MMIO pool in a way that
2343 * matches the constraints supplied in these parameters and by
2346 * Return: 0 on success, -errno on failure
2348 int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
2349 resource_size_t min, resource_size_t max,
2350 resource_size_t size, resource_size_t align,
2353 struct resource *iter, *shadow;
2354 resource_size_t range_min, range_max, start, end;
2355 const char *dev_n = dev_name(&device_obj->device);
2359 mutex_lock(&hyperv_mmio_lock);
2362 * If overlaps with frame buffers are allowed, then first attempt to
2363 * make the allocation from within the reserved region. Because it
2364 * is already reserved, no shadow allocation is necessary.
2366 if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) &&
2367 !(max < fb_mmio->start)) {
2369 range_min = fb_mmio->start;
2370 range_max = fb_mmio->end;
2371 start = (range_min + align - 1) & ~(align - 1);
2372 for (; start + size - 1 <= range_max; start += align) {
2373 *new = request_mem_region_exclusive(start, size, dev_n);
2381 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
2382 if ((iter->start >= max) || (iter->end <= min))
2385 range_min = iter->start;
2386 range_max = iter->end;
2387 start = (range_min + align - 1) & ~(align - 1);
2388 for (; start + size - 1 <= range_max; start += align) {
2389 end = start + size - 1;
2391 /* Skip the whole fb_mmio region if not fb_overlap_ok */
2392 if (!fb_overlap_ok && fb_mmio &&
2393 (((start >= fb_mmio->start) && (start <= fb_mmio->end)) ||
2394 ((end >= fb_mmio->start) && (end <= fb_mmio->end))))
2397 shadow = __request_region(iter, start, size, NULL,
2402 *new = request_mem_region_exclusive(start, size, dev_n);
2404 shadow->name = (char *)*new;
2409 __release_region(iter, start, size);
2414 mutex_unlock(&hyperv_mmio_lock);
2417 EXPORT_SYMBOL_GPL(vmbus_allocate_mmio);
2420 * vmbus_free_mmio() - Free a memory-mapped I/O range.
2421 * @start: Base address of region to release.
2422 * @size: Size of the range to be allocated
2424 * This function releases anything requested by
2425 * vmbus_mmio_allocate().
2427 void vmbus_free_mmio(resource_size_t start, resource_size_t size)
2429 struct resource *iter;
2431 mutex_lock(&hyperv_mmio_lock);
2432 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
2433 if ((iter->start >= start + size) || (iter->end <= start))
2436 __release_region(iter, start, size);
2438 release_mem_region(start, size);
2439 mutex_unlock(&hyperv_mmio_lock);
2442 EXPORT_SYMBOL_GPL(vmbus_free_mmio);
2444 static int vmbus_acpi_add(struct acpi_device *device)
2447 int ret_val = -ENODEV;
2448 struct acpi_device *ancestor;
2450 hv_acpi_dev = device;
2453 * Older versions of Hyper-V for ARM64 fail to include the _CCA
2454 * method on the top level VMbus device in the DSDT. But devices
2455 * are hardware coherent in all current Hyper-V use cases, so fix
2456 * up the ACPI device to behave as if _CCA is present and indicates
2457 * hardware coherence.
2459 ACPI_COMPANION_SET(&device->dev, device);
2460 if (IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED) &&
2461 device_get_dma_attr(&device->dev) == DEV_DMA_NOT_SUPPORTED) {
2462 pr_info("No ACPI _CCA found; assuming coherent device I/O\n");
2463 device->flags.cca_seen = true;
2464 device->flags.coherent_dma = true;
2467 result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
2468 vmbus_walk_resources, NULL);
2470 if (ACPI_FAILURE(result))
2473 * Some ancestor of the vmbus acpi device (Gen1 or Gen2
2474 * firmware) is the VMOD that has the mmio ranges. Get that.
2476 for (ancestor = acpi_dev_parent(device); ancestor;
2477 ancestor = acpi_dev_parent(ancestor)) {
2478 result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS,
2479 vmbus_walk_resources, NULL);
2481 if (ACPI_FAILURE(result))
2492 vmbus_acpi_remove(device);
2496 #ifdef CONFIG_PM_SLEEP
2497 static int vmbus_bus_suspend(struct device *dev)
2499 struct hv_per_cpu_context *hv_cpu = per_cpu_ptr(
2500 hv_context.cpu_context, VMBUS_CONNECT_CPU);
2501 struct vmbus_channel *channel, *sc;
2503 tasklet_disable(&hv_cpu->msg_dpc);
2504 vmbus_connection.ignore_any_offer_msg = true;
2505 /* The tasklet_enable() takes care of providing a memory barrier */
2506 tasklet_enable(&hv_cpu->msg_dpc);
2508 /* Drain all the workqueues as we are in suspend */
2509 drain_workqueue(vmbus_connection.rescind_work_queue);
2510 drain_workqueue(vmbus_connection.work_queue);
2511 drain_workqueue(vmbus_connection.handle_primary_chan_wq);
2512 drain_workqueue(vmbus_connection.handle_sub_chan_wq);
2514 mutex_lock(&vmbus_connection.channel_mutex);
2515 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
2516 if (!is_hvsock_channel(channel))
2519 vmbus_force_channel_rescinded(channel);
2521 mutex_unlock(&vmbus_connection.channel_mutex);
2524 * Wait until all the sub-channels and hv_sock channels have been
2525 * cleaned up. Sub-channels should be destroyed upon suspend, otherwise
2526 * they would conflict with the new sub-channels that will be created
2527 * in the resume path. hv_sock channels should also be destroyed, but
2528 * a hv_sock channel of an established hv_sock connection can not be
2529 * really destroyed since it may still be referenced by the userspace
2530 * application, so we just force the hv_sock channel to be rescinded
2531 * by vmbus_force_channel_rescinded(), and the userspace application
2532 * will thoroughly destroy the channel after hibernation.
2534 * Note: the counter nr_chan_close_on_suspend may never go above 0 if
2535 * the VM has no sub-channel and hv_sock channel, e.g. a 1-vCPU VM.
2537 if (atomic_read(&vmbus_connection.nr_chan_close_on_suspend) > 0)
2538 wait_for_completion(&vmbus_connection.ready_for_suspend_event);
2540 if (atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) != 0) {
2541 pr_err("Can not suspend due to a previous failed resuming\n");
2545 mutex_lock(&vmbus_connection.channel_mutex);
2547 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
2549 * Remove the channel from the array of channels and invalidate
2550 * the channel's relid. Upon resume, vmbus_onoffer() will fix
2551 * up the relid (and other fields, if necessary) and add the
2552 * channel back to the array.
2554 vmbus_channel_unmap_relid(channel);
2555 channel->offermsg.child_relid = INVALID_RELID;
2557 if (is_hvsock_channel(channel)) {
2558 if (!channel->rescind) {
2559 pr_err("hv_sock channel not rescinded!\n");
2565 list_for_each_entry(sc, &channel->sc_list, sc_list) {
2566 pr_err("Sub-channel not deleted!\n");
2570 atomic_inc(&vmbus_connection.nr_chan_fixup_on_resume);
2573 mutex_unlock(&vmbus_connection.channel_mutex);
2575 vmbus_initiate_unload(false);
2577 /* Reset the event for the next resume. */
2578 reinit_completion(&vmbus_connection.ready_for_resume_event);
2583 static int vmbus_bus_resume(struct device *dev)
2585 struct vmbus_channel_msginfo *msginfo;
2589 vmbus_connection.ignore_any_offer_msg = false;
2592 * We only use the 'vmbus_proto_version', which was in use before
2593 * hibernation, to re-negotiate with the host.
2595 if (!vmbus_proto_version) {
2596 pr_err("Invalid proto version = 0x%x\n", vmbus_proto_version);
2600 msgsize = sizeof(*msginfo) +
2601 sizeof(struct vmbus_channel_initiate_contact);
2603 msginfo = kzalloc(msgsize, GFP_KERNEL);
2605 if (msginfo == NULL)
2608 ret = vmbus_negotiate_version(msginfo, vmbus_proto_version);
2615 WARN_ON(atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) == 0);
2617 vmbus_request_offers();
2619 if (wait_for_completion_timeout(
2620 &vmbus_connection.ready_for_resume_event, 10 * HZ) == 0)
2621 pr_err("Some vmbus device is missing after suspending?\n");
2623 /* Reset the event for the next suspend. */
2624 reinit_completion(&vmbus_connection.ready_for_suspend_event);
2629 #define vmbus_bus_suspend NULL
2630 #define vmbus_bus_resume NULL
2631 #endif /* CONFIG_PM_SLEEP */
2633 static const struct acpi_device_id vmbus_acpi_device_ids[] = {
2638 MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
2641 * Note: we must use the "no_irq" ops, otherwise hibernation can not work with
2642 * PCI device assignment, because "pci_dev_pm_ops" uses the "noirq" ops: in
2643 * the resume path, the pci "noirq" restore op runs before "non-noirq" op (see
2644 * resume_target_kernel() -> dpm_resume_start(), and hibernation_restore() ->
2645 * dpm_resume_end()). This means vmbus_bus_resume() and the pci-hyperv's
2646 * resume callback must also run via the "noirq" ops.
2648 * Set suspend_noirq/resume_noirq to NULL for Suspend-to-Idle: see the comment
2649 * earlier in this file before vmbus_pm.
2652 static const struct dev_pm_ops vmbus_bus_pm = {
2653 .suspend_noirq = NULL,
2654 .resume_noirq = NULL,
2655 .freeze_noirq = vmbus_bus_suspend,
2656 .thaw_noirq = vmbus_bus_resume,
2657 .poweroff_noirq = vmbus_bus_suspend,
2658 .restore_noirq = vmbus_bus_resume
2661 static struct acpi_driver vmbus_acpi_driver = {
2663 .ids = vmbus_acpi_device_ids,
2665 .add = vmbus_acpi_add,
2666 .remove = vmbus_acpi_remove,
2668 .drv.pm = &vmbus_bus_pm,
2669 .drv.probe_type = PROBE_FORCE_SYNCHRONOUS,
2672 static void hv_kexec_handler(void)
2674 hv_stimer_global_cleanup();
2675 vmbus_initiate_unload(false);
2676 /* Make sure conn_state is set as hv_synic_cleanup checks for it */
2678 cpuhp_remove_state(hyperv_cpuhp_online);
2681 static void hv_crash_handler(struct pt_regs *regs)
2685 vmbus_initiate_unload(true);
2687 * In crash handler we can't schedule synic cleanup for all CPUs,
2688 * doing the cleanup for current CPU only. This should be sufficient
2691 cpu = smp_processor_id();
2692 hv_stimer_cleanup(cpu);
2693 hv_synic_disable_regs(cpu);
2696 static int hv_synic_suspend(void)
2699 * When we reach here, all the non-boot CPUs have been offlined.
2700 * If we're in a legacy configuration where stimer Direct Mode is
2701 * not enabled, the stimers on the non-boot CPUs have been unbound
2702 * in hv_synic_cleanup() -> hv_stimer_legacy_cleanup() ->
2703 * hv_stimer_cleanup() -> clockevents_unbind_device().
2705 * hv_synic_suspend() only runs on CPU0 with interrupts disabled.
2706 * Here we do not call hv_stimer_legacy_cleanup() on CPU0 because:
2707 * 1) it's unnecessary as interrupts remain disabled between
2708 * syscore_suspend() and syscore_resume(): see create_image() and
2709 * resume_target_kernel()
2710 * 2) the stimer on CPU0 is automatically disabled later by
2711 * syscore_suspend() -> timekeeping_suspend() -> tick_suspend() -> ...
2712 * -> clockevents_shutdown() -> ... -> hv_ce_shutdown()
2713 * 3) a warning would be triggered if we call
2714 * clockevents_unbind_device(), which may sleep, in an
2715 * interrupts-disabled context.
2718 hv_synic_disable_regs(0);
2723 static void hv_synic_resume(void)
2725 hv_synic_enable_regs(0);
2728 * Note: we don't need to call hv_stimer_init(0), because the timer
2729 * on CPU0 is not unbound in hv_synic_suspend(), and the timer is
2730 * automatically re-enabled in timekeeping_resume().
2734 /* The callbacks run only on CPU0, with irqs_disabled. */
2735 static struct syscore_ops hv_synic_syscore_ops = {
2736 .suspend = hv_synic_suspend,
2737 .resume = hv_synic_resume,
2740 static int __init hv_acpi_init(void)
2744 if (!hv_is_hyperv_initialized())
2747 if (hv_root_partition && !hv_nested)
2751 * Get ACPI resources first.
2753 ret = acpi_bus_register_driver(&vmbus_acpi_driver);
2764 * If we're on an architecture with a hardcoded hypervisor
2765 * vector (i.e. x86/x64), override the VMbus interrupt found
2766 * in the ACPI tables. Ensure vmbus_irq is not set since the
2767 * normal Linux IRQ mechanism is not used in this case.
2769 #ifdef HYPERVISOR_CALLBACK_VECTOR
2770 vmbus_interrupt = HYPERVISOR_CALLBACK_VECTOR;
2776 ret = vmbus_bus_init();
2780 hv_setup_kexec_handler(hv_kexec_handler);
2781 hv_setup_crash_handler(hv_crash_handler);
2783 register_syscore_ops(&hv_synic_syscore_ops);
2788 acpi_bus_unregister_driver(&vmbus_acpi_driver);
2793 static void __exit vmbus_exit(void)
2797 unregister_syscore_ops(&hv_synic_syscore_ops);
2799 hv_remove_kexec_handler();
2800 hv_remove_crash_handler();
2801 vmbus_connection.conn_state = DISCONNECTED;
2802 hv_stimer_global_cleanup();
2804 if (vmbus_irq == -1) {
2805 hv_remove_vmbus_handler();
2807 free_percpu_irq(vmbus_irq, vmbus_evt);
2808 free_percpu(vmbus_evt);
2810 for_each_online_cpu(cpu) {
2811 struct hv_per_cpu_context *hv_cpu
2812 = per_cpu_ptr(hv_context.cpu_context, cpu);
2814 tasklet_kill(&hv_cpu->msg_dpc);
2816 hv_debug_rm_all_dir();
2818 vmbus_free_channels();
2819 kfree(vmbus_connection.channels);
2821 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
2822 kmsg_dump_unregister(&hv_kmsg_dumper);
2823 unregister_die_notifier(&hyperv_die_report_block);
2824 atomic_notifier_chain_unregister(&panic_notifier_list,
2825 &hyperv_panic_report_block);
2829 * The vmbus panic notifier is always registered, hence we should
2830 * also unconditionally unregister it here as well.
2832 atomic_notifier_chain_unregister(&panic_notifier_list,
2833 &hyperv_panic_vmbus_unload_block);
2835 free_page((unsigned long)hv_panic_page);
2836 unregister_sysctl_table(hv_ctl_table_hdr);
2837 hv_ctl_table_hdr = NULL;
2838 bus_unregister(&hv_bus);
2840 cpuhp_remove_state(hyperv_cpuhp_online);
2842 acpi_bus_unregister_driver(&vmbus_acpi_driver);
2846 MODULE_LICENSE("GPL");
2847 MODULE_DESCRIPTION("Microsoft Hyper-V VMBus Driver");
2849 subsys_initcall(hv_acpi_init);
2850 module_exit(vmbus_exit);