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/task_stack.h>
26 #include <linux/delay.h>
27 #include <linux/notifier.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 <clocksource/hyperv_timer.h>
38 #include "hyperv_vmbus.h"
41 struct list_head node;
42 struct hv_vmbus_device_id id;
45 static struct acpi_device *hv_acpi_dev;
47 static struct completion probe_event;
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;
71 static int hyperv_panic_event(struct notifier_block *nb, unsigned long val,
76 vmbus_initiate_unload(true);
79 * Hyper-V should be notified only once about a panic. If we will be
80 * doing hv_kmsg_dump() with kmsg data later, don't do the notification
83 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE
84 && hyperv_report_reg()) {
85 regs = current_pt_regs();
86 hyperv_report_panic(regs, val, false);
91 static int hyperv_die_event(struct notifier_block *nb, unsigned long val,
94 struct die_args *die = args;
95 struct pt_regs *regs = die->regs;
97 /* Don't notify Hyper-V if the die event is other than oops */
102 * Hyper-V should be notified only once about a panic. If we will be
103 * doing hv_kmsg_dump() with kmsg data later, don't do the notification
106 if (hyperv_report_reg())
107 hyperv_report_panic(regs, val, true);
111 static struct notifier_block hyperv_die_block = {
112 .notifier_call = hyperv_die_event,
114 static struct notifier_block hyperv_panic_block = {
115 .notifier_call = hyperv_panic_event,
118 static const char *fb_mmio_name = "fb_range";
119 static struct resource *fb_mmio;
120 static struct resource *hyperv_mmio;
121 static DEFINE_MUTEX(hyperv_mmio_lock);
123 static int vmbus_exists(void)
125 if (hv_acpi_dev == NULL)
131 static u8 channel_monitor_group(const struct vmbus_channel *channel)
133 return (u8)channel->offermsg.monitorid / 32;
136 static u8 channel_monitor_offset(const struct vmbus_channel *channel)
138 return (u8)channel->offermsg.monitorid % 32;
141 static u32 channel_pending(const struct vmbus_channel *channel,
142 const struct hv_monitor_page *monitor_page)
144 u8 monitor_group = channel_monitor_group(channel);
146 return monitor_page->trigger_group[monitor_group].pending;
149 static u32 channel_latency(const struct vmbus_channel *channel,
150 const struct hv_monitor_page *monitor_page)
152 u8 monitor_group = channel_monitor_group(channel);
153 u8 monitor_offset = channel_monitor_offset(channel);
155 return monitor_page->latency[monitor_group][monitor_offset];
158 static u32 channel_conn_id(struct vmbus_channel *channel,
159 struct hv_monitor_page *monitor_page)
161 u8 monitor_group = channel_monitor_group(channel);
162 u8 monitor_offset = channel_monitor_offset(channel);
164 return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
167 static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
170 struct hv_device *hv_dev = device_to_hv_device(dev);
172 if (!hv_dev->channel)
174 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
176 static DEVICE_ATTR_RO(id);
178 static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
181 struct hv_device *hv_dev = device_to_hv_device(dev);
183 if (!hv_dev->channel)
185 return sprintf(buf, "%d\n", hv_dev->channel->state);
187 static DEVICE_ATTR_RO(state);
189 static ssize_t monitor_id_show(struct device *dev,
190 struct device_attribute *dev_attr, char *buf)
192 struct hv_device *hv_dev = device_to_hv_device(dev);
194 if (!hv_dev->channel)
196 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
198 static DEVICE_ATTR_RO(monitor_id);
200 static ssize_t class_id_show(struct device *dev,
201 struct device_attribute *dev_attr, char *buf)
203 struct hv_device *hv_dev = device_to_hv_device(dev);
205 if (!hv_dev->channel)
207 return sprintf(buf, "{%pUl}\n",
208 &hv_dev->channel->offermsg.offer.if_type);
210 static DEVICE_ATTR_RO(class_id);
212 static ssize_t device_id_show(struct device *dev,
213 struct device_attribute *dev_attr, char *buf)
215 struct hv_device *hv_dev = device_to_hv_device(dev);
217 if (!hv_dev->channel)
219 return sprintf(buf, "{%pUl}\n",
220 &hv_dev->channel->offermsg.offer.if_instance);
222 static DEVICE_ATTR_RO(device_id);
224 static ssize_t modalias_show(struct device *dev,
225 struct device_attribute *dev_attr, char *buf)
227 struct hv_device *hv_dev = device_to_hv_device(dev);
229 return sprintf(buf, "vmbus:%*phN\n", UUID_SIZE, &hv_dev->dev_type);
231 static DEVICE_ATTR_RO(modalias);
234 static ssize_t numa_node_show(struct device *dev,
235 struct device_attribute *attr, char *buf)
237 struct hv_device *hv_dev = device_to_hv_device(dev);
239 if (!hv_dev->channel)
242 return sprintf(buf, "%d\n", cpu_to_node(hv_dev->channel->target_cpu));
244 static DEVICE_ATTR_RO(numa_node);
247 static ssize_t server_monitor_pending_show(struct device *dev,
248 struct device_attribute *dev_attr,
251 struct hv_device *hv_dev = device_to_hv_device(dev);
253 if (!hv_dev->channel)
255 return sprintf(buf, "%d\n",
256 channel_pending(hv_dev->channel,
257 vmbus_connection.monitor_pages[0]));
259 static DEVICE_ATTR_RO(server_monitor_pending);
261 static ssize_t client_monitor_pending_show(struct device *dev,
262 struct device_attribute *dev_attr,
265 struct hv_device *hv_dev = device_to_hv_device(dev);
267 if (!hv_dev->channel)
269 return sprintf(buf, "%d\n",
270 channel_pending(hv_dev->channel,
271 vmbus_connection.monitor_pages[1]));
273 static DEVICE_ATTR_RO(client_monitor_pending);
275 static ssize_t server_monitor_latency_show(struct device *dev,
276 struct device_attribute *dev_attr,
279 struct hv_device *hv_dev = device_to_hv_device(dev);
281 if (!hv_dev->channel)
283 return sprintf(buf, "%d\n",
284 channel_latency(hv_dev->channel,
285 vmbus_connection.monitor_pages[0]));
287 static DEVICE_ATTR_RO(server_monitor_latency);
289 static ssize_t client_monitor_latency_show(struct device *dev,
290 struct device_attribute *dev_attr,
293 struct hv_device *hv_dev = device_to_hv_device(dev);
295 if (!hv_dev->channel)
297 return sprintf(buf, "%d\n",
298 channel_latency(hv_dev->channel,
299 vmbus_connection.monitor_pages[1]));
301 static DEVICE_ATTR_RO(client_monitor_latency);
303 static ssize_t server_monitor_conn_id_show(struct device *dev,
304 struct device_attribute *dev_attr,
307 struct hv_device *hv_dev = device_to_hv_device(dev);
309 if (!hv_dev->channel)
311 return sprintf(buf, "%d\n",
312 channel_conn_id(hv_dev->channel,
313 vmbus_connection.monitor_pages[0]));
315 static DEVICE_ATTR_RO(server_monitor_conn_id);
317 static ssize_t client_monitor_conn_id_show(struct device *dev,
318 struct device_attribute *dev_attr,
321 struct hv_device *hv_dev = device_to_hv_device(dev);
323 if (!hv_dev->channel)
325 return sprintf(buf, "%d\n",
326 channel_conn_id(hv_dev->channel,
327 vmbus_connection.monitor_pages[1]));
329 static DEVICE_ATTR_RO(client_monitor_conn_id);
331 static ssize_t out_intr_mask_show(struct device *dev,
332 struct device_attribute *dev_attr, char *buf)
334 struct hv_device *hv_dev = device_to_hv_device(dev);
335 struct hv_ring_buffer_debug_info outbound;
338 if (!hv_dev->channel)
341 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
346 return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
348 static DEVICE_ATTR_RO(out_intr_mask);
350 static ssize_t out_read_index_show(struct device *dev,
351 struct device_attribute *dev_attr, char *buf)
353 struct hv_device *hv_dev = device_to_hv_device(dev);
354 struct hv_ring_buffer_debug_info outbound;
357 if (!hv_dev->channel)
360 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
364 return sprintf(buf, "%d\n", outbound.current_read_index);
366 static DEVICE_ATTR_RO(out_read_index);
368 static ssize_t out_write_index_show(struct device *dev,
369 struct device_attribute *dev_attr,
372 struct hv_device *hv_dev = device_to_hv_device(dev);
373 struct hv_ring_buffer_debug_info outbound;
376 if (!hv_dev->channel)
379 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
383 return sprintf(buf, "%d\n", outbound.current_write_index);
385 static DEVICE_ATTR_RO(out_write_index);
387 static ssize_t out_read_bytes_avail_show(struct device *dev,
388 struct device_attribute *dev_attr,
391 struct hv_device *hv_dev = device_to_hv_device(dev);
392 struct hv_ring_buffer_debug_info outbound;
395 if (!hv_dev->channel)
398 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
402 return sprintf(buf, "%d\n", outbound.bytes_avail_toread);
404 static DEVICE_ATTR_RO(out_read_bytes_avail);
406 static ssize_t out_write_bytes_avail_show(struct device *dev,
407 struct device_attribute *dev_attr,
410 struct hv_device *hv_dev = device_to_hv_device(dev);
411 struct hv_ring_buffer_debug_info outbound;
414 if (!hv_dev->channel)
417 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
421 return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
423 static DEVICE_ATTR_RO(out_write_bytes_avail);
425 static ssize_t in_intr_mask_show(struct device *dev,
426 struct device_attribute *dev_attr, char *buf)
428 struct hv_device *hv_dev = device_to_hv_device(dev);
429 struct hv_ring_buffer_debug_info inbound;
432 if (!hv_dev->channel)
435 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
439 return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
441 static DEVICE_ATTR_RO(in_intr_mask);
443 static ssize_t in_read_index_show(struct device *dev,
444 struct device_attribute *dev_attr, char *buf)
446 struct hv_device *hv_dev = device_to_hv_device(dev);
447 struct hv_ring_buffer_debug_info inbound;
450 if (!hv_dev->channel)
453 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
457 return sprintf(buf, "%d\n", inbound.current_read_index);
459 static DEVICE_ATTR_RO(in_read_index);
461 static ssize_t in_write_index_show(struct device *dev,
462 struct device_attribute *dev_attr, char *buf)
464 struct hv_device *hv_dev = device_to_hv_device(dev);
465 struct hv_ring_buffer_debug_info inbound;
468 if (!hv_dev->channel)
471 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
475 return sprintf(buf, "%d\n", inbound.current_write_index);
477 static DEVICE_ATTR_RO(in_write_index);
479 static ssize_t in_read_bytes_avail_show(struct device *dev,
480 struct device_attribute *dev_attr,
483 struct hv_device *hv_dev = device_to_hv_device(dev);
484 struct hv_ring_buffer_debug_info inbound;
487 if (!hv_dev->channel)
490 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
494 return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
496 static DEVICE_ATTR_RO(in_read_bytes_avail);
498 static ssize_t in_write_bytes_avail_show(struct device *dev,
499 struct device_attribute *dev_attr,
502 struct hv_device *hv_dev = device_to_hv_device(dev);
503 struct hv_ring_buffer_debug_info inbound;
506 if (!hv_dev->channel)
509 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
513 return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
515 static DEVICE_ATTR_RO(in_write_bytes_avail);
517 static ssize_t channel_vp_mapping_show(struct device *dev,
518 struct device_attribute *dev_attr,
521 struct hv_device *hv_dev = device_to_hv_device(dev);
522 struct vmbus_channel *channel = hv_dev->channel, *cur_sc;
523 int buf_size = PAGE_SIZE, n_written, tot_written;
524 struct list_head *cur;
529 mutex_lock(&vmbus_connection.channel_mutex);
531 tot_written = snprintf(buf, buf_size, "%u:%u\n",
532 channel->offermsg.child_relid, channel->target_cpu);
534 list_for_each(cur, &channel->sc_list) {
535 if (tot_written >= buf_size - 1)
538 cur_sc = list_entry(cur, struct vmbus_channel, sc_list);
539 n_written = scnprintf(buf + tot_written,
540 buf_size - tot_written,
542 cur_sc->offermsg.child_relid,
544 tot_written += n_written;
547 mutex_unlock(&vmbus_connection.channel_mutex);
551 static DEVICE_ATTR_RO(channel_vp_mapping);
553 static ssize_t vendor_show(struct device *dev,
554 struct device_attribute *dev_attr,
557 struct hv_device *hv_dev = device_to_hv_device(dev);
559 return sprintf(buf, "0x%x\n", hv_dev->vendor_id);
561 static DEVICE_ATTR_RO(vendor);
563 static ssize_t device_show(struct device *dev,
564 struct device_attribute *dev_attr,
567 struct hv_device *hv_dev = device_to_hv_device(dev);
569 return sprintf(buf, "0x%x\n", hv_dev->device_id);
571 static DEVICE_ATTR_RO(device);
573 static ssize_t driver_override_store(struct device *dev,
574 struct device_attribute *attr,
575 const char *buf, size_t count)
577 struct hv_device *hv_dev = device_to_hv_device(dev);
578 char *driver_override, *old, *cp;
580 /* We need to keep extra room for a newline */
581 if (count >= (PAGE_SIZE - 1))
584 driver_override = kstrndup(buf, count, GFP_KERNEL);
585 if (!driver_override)
588 cp = strchr(driver_override, '\n');
593 old = hv_dev->driver_override;
594 if (strlen(driver_override)) {
595 hv_dev->driver_override = driver_override;
597 kfree(driver_override);
598 hv_dev->driver_override = NULL;
607 static ssize_t driver_override_show(struct device *dev,
608 struct device_attribute *attr, char *buf)
610 struct hv_device *hv_dev = device_to_hv_device(dev);
614 len = snprintf(buf, PAGE_SIZE, "%s\n", hv_dev->driver_override);
619 static DEVICE_ATTR_RW(driver_override);
621 /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
622 static struct attribute *vmbus_dev_attrs[] = {
624 &dev_attr_state.attr,
625 &dev_attr_monitor_id.attr,
626 &dev_attr_class_id.attr,
627 &dev_attr_device_id.attr,
628 &dev_attr_modalias.attr,
630 &dev_attr_numa_node.attr,
632 &dev_attr_server_monitor_pending.attr,
633 &dev_attr_client_monitor_pending.attr,
634 &dev_attr_server_monitor_latency.attr,
635 &dev_attr_client_monitor_latency.attr,
636 &dev_attr_server_monitor_conn_id.attr,
637 &dev_attr_client_monitor_conn_id.attr,
638 &dev_attr_out_intr_mask.attr,
639 &dev_attr_out_read_index.attr,
640 &dev_attr_out_write_index.attr,
641 &dev_attr_out_read_bytes_avail.attr,
642 &dev_attr_out_write_bytes_avail.attr,
643 &dev_attr_in_intr_mask.attr,
644 &dev_attr_in_read_index.attr,
645 &dev_attr_in_write_index.attr,
646 &dev_attr_in_read_bytes_avail.attr,
647 &dev_attr_in_write_bytes_avail.attr,
648 &dev_attr_channel_vp_mapping.attr,
649 &dev_attr_vendor.attr,
650 &dev_attr_device.attr,
651 &dev_attr_driver_override.attr,
656 * Device-level attribute_group callback function. Returns the permission for
657 * each attribute, and returns 0 if an attribute is not visible.
659 static umode_t vmbus_dev_attr_is_visible(struct kobject *kobj,
660 struct attribute *attr, int idx)
662 struct device *dev = kobj_to_dev(kobj);
663 const struct hv_device *hv_dev = device_to_hv_device(dev);
665 /* Hide the monitor attributes if the monitor mechanism is not used. */
666 if (!hv_dev->channel->offermsg.monitor_allocated &&
667 (attr == &dev_attr_monitor_id.attr ||
668 attr == &dev_attr_server_monitor_pending.attr ||
669 attr == &dev_attr_client_monitor_pending.attr ||
670 attr == &dev_attr_server_monitor_latency.attr ||
671 attr == &dev_attr_client_monitor_latency.attr ||
672 attr == &dev_attr_server_monitor_conn_id.attr ||
673 attr == &dev_attr_client_monitor_conn_id.attr))
679 static const struct attribute_group vmbus_dev_group = {
680 .attrs = vmbus_dev_attrs,
681 .is_visible = vmbus_dev_attr_is_visible
683 __ATTRIBUTE_GROUPS(vmbus_dev);
685 /* Set up the attribute for /sys/bus/vmbus/hibernation */
686 static ssize_t hibernation_show(struct bus_type *bus, char *buf)
688 return sprintf(buf, "%d\n", !!hv_is_hibernation_supported());
691 static BUS_ATTR_RO(hibernation);
693 static struct attribute *vmbus_bus_attrs[] = {
694 &bus_attr_hibernation.attr,
697 static const struct attribute_group vmbus_bus_group = {
698 .attrs = vmbus_bus_attrs,
700 __ATTRIBUTE_GROUPS(vmbus_bus);
703 * vmbus_uevent - add uevent for our device
705 * This routine is invoked when a device is added or removed on the vmbus to
706 * generate a uevent to udev in the userspace. The udev will then look at its
707 * rule and the uevent generated here to load the appropriate driver
709 * The alias string will be of the form vmbus:guid where guid is the string
710 * representation of the device guid (each byte of the guid will be
711 * represented with two hex characters.
713 static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
715 struct hv_device *dev = device_to_hv_device(device);
716 const char *format = "MODALIAS=vmbus:%*phN";
718 return add_uevent_var(env, format, UUID_SIZE, &dev->dev_type);
721 static const struct hv_vmbus_device_id *
722 hv_vmbus_dev_match(const struct hv_vmbus_device_id *id, const guid_t *guid)
725 return NULL; /* empty device table */
727 for (; !guid_is_null(&id->guid); id++)
728 if (guid_equal(&id->guid, guid))
734 static const struct hv_vmbus_device_id *
735 hv_vmbus_dynid_match(struct hv_driver *drv, const guid_t *guid)
737 const struct hv_vmbus_device_id *id = NULL;
738 struct vmbus_dynid *dynid;
740 spin_lock(&drv->dynids.lock);
741 list_for_each_entry(dynid, &drv->dynids.list, node) {
742 if (guid_equal(&dynid->id.guid, guid)) {
747 spin_unlock(&drv->dynids.lock);
752 static const struct hv_vmbus_device_id vmbus_device_null;
755 * Return a matching hv_vmbus_device_id pointer.
756 * If there is no match, return NULL.
758 static const struct hv_vmbus_device_id *hv_vmbus_get_id(struct hv_driver *drv,
759 struct hv_device *dev)
761 const guid_t *guid = &dev->dev_type;
762 const struct hv_vmbus_device_id *id;
764 /* When driver_override is set, only bind to the matching driver */
765 if (dev->driver_override && strcmp(dev->driver_override, drv->name))
768 /* Look at the dynamic ids first, before the static ones */
769 id = hv_vmbus_dynid_match(drv, guid);
771 id = hv_vmbus_dev_match(drv->id_table, guid);
773 /* driver_override will always match, send a dummy id */
774 if (!id && dev->driver_override)
775 id = &vmbus_device_null;
780 /* vmbus_add_dynid - add a new device ID to this driver and re-probe devices */
781 static int vmbus_add_dynid(struct hv_driver *drv, guid_t *guid)
783 struct vmbus_dynid *dynid;
785 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
789 dynid->id.guid = *guid;
791 spin_lock(&drv->dynids.lock);
792 list_add_tail(&dynid->node, &drv->dynids.list);
793 spin_unlock(&drv->dynids.lock);
795 return driver_attach(&drv->driver);
798 static void vmbus_free_dynids(struct hv_driver *drv)
800 struct vmbus_dynid *dynid, *n;
802 spin_lock(&drv->dynids.lock);
803 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
804 list_del(&dynid->node);
807 spin_unlock(&drv->dynids.lock);
811 * store_new_id - sysfs frontend to vmbus_add_dynid()
813 * Allow GUIDs to be added to an existing driver via sysfs.
815 static ssize_t new_id_store(struct device_driver *driver, const char *buf,
818 struct hv_driver *drv = drv_to_hv_drv(driver);
822 retval = guid_parse(buf, &guid);
826 if (hv_vmbus_dynid_match(drv, &guid))
829 retval = vmbus_add_dynid(drv, &guid);
834 static DRIVER_ATTR_WO(new_id);
837 * store_remove_id - remove a PCI device ID from this driver
839 * Removes a dynamic pci device ID to this driver.
841 static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
844 struct hv_driver *drv = drv_to_hv_drv(driver);
845 struct vmbus_dynid *dynid, *n;
849 retval = guid_parse(buf, &guid);
854 spin_lock(&drv->dynids.lock);
855 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
856 struct hv_vmbus_device_id *id = &dynid->id;
858 if (guid_equal(&id->guid, &guid)) {
859 list_del(&dynid->node);
865 spin_unlock(&drv->dynids.lock);
869 static DRIVER_ATTR_WO(remove_id);
871 static struct attribute *vmbus_drv_attrs[] = {
872 &driver_attr_new_id.attr,
873 &driver_attr_remove_id.attr,
876 ATTRIBUTE_GROUPS(vmbus_drv);
880 * vmbus_match - Attempt to match the specified device to the specified driver
882 static int vmbus_match(struct device *device, struct device_driver *driver)
884 struct hv_driver *drv = drv_to_hv_drv(driver);
885 struct hv_device *hv_dev = device_to_hv_device(device);
887 /* The hv_sock driver handles all hv_sock offers. */
888 if (is_hvsock_channel(hv_dev->channel))
891 if (hv_vmbus_get_id(drv, hv_dev))
898 * vmbus_probe - Add the new vmbus's child device
900 static int vmbus_probe(struct device *child_device)
903 struct hv_driver *drv =
904 drv_to_hv_drv(child_device->driver);
905 struct hv_device *dev = device_to_hv_device(child_device);
906 const struct hv_vmbus_device_id *dev_id;
908 dev_id = hv_vmbus_get_id(drv, dev);
910 ret = drv->probe(dev, dev_id);
912 pr_err("probe failed for device %s (%d)\n",
913 dev_name(child_device), ret);
916 pr_err("probe not set for driver %s\n",
917 dev_name(child_device));
924 * vmbus_dma_configure -- Configure DMA coherence for VMbus device
926 static int vmbus_dma_configure(struct device *child_device)
929 * On ARM64, propagate the DMA coherence setting from the top level
930 * VMbus ACPI device to the child VMbus device being added here.
931 * On x86/x64 coherence is assumed and these calls have no effect.
933 hv_setup_dma_ops(child_device,
934 device_get_dma_attr(&hv_acpi_dev->dev) == DEV_DMA_COHERENT);
939 * vmbus_remove - Remove a vmbus device
941 static void vmbus_remove(struct device *child_device)
943 struct hv_driver *drv;
944 struct hv_device *dev = device_to_hv_device(child_device);
946 if (child_device->driver) {
947 drv = drv_to_hv_drv(child_device->driver);
954 * vmbus_shutdown - Shutdown a vmbus device
956 static void vmbus_shutdown(struct device *child_device)
958 struct hv_driver *drv;
959 struct hv_device *dev = device_to_hv_device(child_device);
962 /* The device may not be attached yet */
963 if (!child_device->driver)
966 drv = drv_to_hv_drv(child_device->driver);
972 #ifdef CONFIG_PM_SLEEP
974 * vmbus_suspend - Suspend a vmbus device
976 static int vmbus_suspend(struct device *child_device)
978 struct hv_driver *drv;
979 struct hv_device *dev = device_to_hv_device(child_device);
981 /* The device may not be attached yet */
982 if (!child_device->driver)
985 drv = drv_to_hv_drv(child_device->driver);
989 return drv->suspend(dev);
993 * vmbus_resume - Resume a vmbus device
995 static int vmbus_resume(struct device *child_device)
997 struct hv_driver *drv;
998 struct hv_device *dev = device_to_hv_device(child_device);
1000 /* The device may not be attached yet */
1001 if (!child_device->driver)
1004 drv = drv_to_hv_drv(child_device->driver);
1008 return drv->resume(dev);
1011 #define vmbus_suspend NULL
1012 #define vmbus_resume NULL
1013 #endif /* CONFIG_PM_SLEEP */
1016 * vmbus_device_release - Final callback release of the vmbus child device
1018 static void vmbus_device_release(struct device *device)
1020 struct hv_device *hv_dev = device_to_hv_device(device);
1021 struct vmbus_channel *channel = hv_dev->channel;
1023 hv_debug_rm_dev_dir(hv_dev);
1025 mutex_lock(&vmbus_connection.channel_mutex);
1026 hv_process_channel_removal(channel);
1027 mutex_unlock(&vmbus_connection.channel_mutex);
1032 * Note: we must use the "noirq" ops: see the comment before vmbus_bus_pm.
1034 * suspend_noirq/resume_noirq are set to NULL to support Suspend-to-Idle: we
1035 * shouldn't suspend the vmbus devices upon Suspend-to-Idle, otherwise there
1036 * is no way to wake up a Generation-2 VM.
1038 * The other 4 ops are for hibernation.
1041 static const struct dev_pm_ops vmbus_pm = {
1042 .suspend_noirq = NULL,
1043 .resume_noirq = NULL,
1044 .freeze_noirq = vmbus_suspend,
1045 .thaw_noirq = vmbus_resume,
1046 .poweroff_noirq = vmbus_suspend,
1047 .restore_noirq = vmbus_resume,
1050 /* The one and only one */
1051 static struct bus_type hv_bus = {
1053 .match = vmbus_match,
1054 .shutdown = vmbus_shutdown,
1055 .remove = vmbus_remove,
1056 .probe = vmbus_probe,
1057 .uevent = vmbus_uevent,
1058 .dma_configure = vmbus_dma_configure,
1059 .dev_groups = vmbus_dev_groups,
1060 .drv_groups = vmbus_drv_groups,
1061 .bus_groups = vmbus_bus_groups,
1065 struct onmessage_work_context {
1066 struct work_struct work;
1068 struct hv_message_header header;
1073 static void vmbus_onmessage_work(struct work_struct *work)
1075 struct onmessage_work_context *ctx;
1077 /* Do not process messages if we're in DISCONNECTED state */
1078 if (vmbus_connection.conn_state == DISCONNECTED)
1081 ctx = container_of(work, struct onmessage_work_context,
1083 vmbus_onmessage((struct vmbus_channel_message_header *)
1088 void vmbus_on_msg_dpc(unsigned long data)
1090 struct hv_per_cpu_context *hv_cpu = (void *)data;
1091 void *page_addr = hv_cpu->synic_message_page;
1092 struct hv_message msg_copy, *msg = (struct hv_message *)page_addr +
1094 struct vmbus_channel_message_header *hdr;
1095 enum vmbus_channel_message_type msgtype;
1096 const struct vmbus_channel_message_table_entry *entry;
1097 struct onmessage_work_context *ctx;
1102 * 'enum vmbus_channel_message_type' is supposed to always be 'u32' as
1103 * it is being used in 'struct vmbus_channel_message_header' definition
1104 * which is supposed to match hypervisor ABI.
1106 BUILD_BUG_ON(sizeof(enum vmbus_channel_message_type) != sizeof(u32));
1109 * Since the message is in memory shared with the host, an erroneous or
1110 * malicious Hyper-V could modify the message while vmbus_on_msg_dpc()
1111 * or individual message handlers are executing; to prevent this, copy
1112 * the message into private memory.
1114 memcpy(&msg_copy, msg, sizeof(struct hv_message));
1116 message_type = msg_copy.header.message_type;
1117 if (message_type == HVMSG_NONE)
1121 hdr = (struct vmbus_channel_message_header *)msg_copy.u.payload;
1122 msgtype = hdr->msgtype;
1124 trace_vmbus_on_msg_dpc(hdr);
1126 if (msgtype >= CHANNELMSG_COUNT) {
1127 WARN_ONCE(1, "unknown msgtype=%d\n", msgtype);
1131 payload_size = msg_copy.header.payload_size;
1132 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT) {
1133 WARN_ONCE(1, "payload size is too large (%d)\n", payload_size);
1137 entry = &channel_message_table[msgtype];
1139 if (!entry->message_handler)
1142 if (payload_size < entry->min_payload_len) {
1143 WARN_ONCE(1, "message too short: msgtype=%d len=%d\n", msgtype, payload_size);
1147 if (entry->handler_type == VMHT_BLOCKING) {
1148 ctx = kmalloc(struct_size(ctx, msg.payload, payload_size), GFP_ATOMIC);
1152 INIT_WORK(&ctx->work, vmbus_onmessage_work);
1153 memcpy(&ctx->msg, &msg_copy, sizeof(msg->header) + payload_size);
1156 * The host can generate a rescind message while we
1157 * may still be handling the original offer. We deal with
1158 * this condition by relying on the synchronization provided
1159 * by offer_in_progress and by channel_mutex. See also the
1160 * inline comments in vmbus_onoffer_rescind().
1163 case CHANNELMSG_RESCIND_CHANNELOFFER:
1165 * If we are handling the rescind message;
1166 * schedule the work on the global work queue.
1168 * The OFFER message and the RESCIND message should
1169 * not be handled by the same serialized work queue,
1170 * because the OFFER handler may call vmbus_open(),
1171 * which tries to open the channel by sending an
1172 * OPEN_CHANNEL message to the host and waits for
1173 * the host's response; however, if the host has
1174 * rescinded the channel before it receives the
1175 * OPEN_CHANNEL message, the host just silently
1176 * ignores the OPEN_CHANNEL message; as a result,
1177 * the guest's OFFER handler hangs for ever, if we
1178 * handle the RESCIND message in the same serialized
1179 * work queue: the RESCIND handler can not start to
1180 * run before the OFFER handler finishes.
1182 schedule_work(&ctx->work);
1185 case CHANNELMSG_OFFERCHANNEL:
1187 * The host sends the offer message of a given channel
1188 * before sending the rescind message of the same
1189 * channel. These messages are sent to the guest's
1190 * connect CPU; the guest then starts processing them
1191 * in the tasklet handler on this CPU:
1195 * [vmbus_on_msg_dpc()]
1196 * atomic_inc() // CHANNELMSG_OFFERCHANNEL
1199 * [vmbus_on_msg_dpc()]
1200 * schedule_work() // CHANNELMSG_RESCIND_CHANNELOFFER
1202 * We rely on the memory-ordering properties of the
1203 * queue_work() and schedule_work() primitives, which
1204 * guarantee that the atomic increment will be visible
1205 * to the CPUs which will execute the offer & rescind
1206 * works by the time these works will start execution.
1208 atomic_inc(&vmbus_connection.offer_in_progress);
1212 queue_work(vmbus_connection.work_queue, &ctx->work);
1215 entry->message_handler(hdr);
1218 vmbus_signal_eom(msg, message_type);
1221 #ifdef CONFIG_PM_SLEEP
1223 * Fake RESCIND_CHANNEL messages to clean up hv_sock channels by force for
1224 * hibernation, because hv_sock connections can not persist across hibernation.
1226 static void vmbus_force_channel_rescinded(struct vmbus_channel *channel)
1228 struct onmessage_work_context *ctx;
1229 struct vmbus_channel_rescind_offer *rescind;
1231 WARN_ON(!is_hvsock_channel(channel));
1234 * Allocation size is small and the allocation should really not fail,
1235 * otherwise the state of the hv_sock connections ends up in limbo.
1237 ctx = kzalloc(sizeof(*ctx) + sizeof(*rescind),
1238 GFP_KERNEL | __GFP_NOFAIL);
1241 * So far, these are not really used by Linux. Just set them to the
1242 * reasonable values conforming to the definitions of the fields.
1244 ctx->msg.header.message_type = 1;
1245 ctx->msg.header.payload_size = sizeof(*rescind);
1247 /* These values are actually used by Linux. */
1248 rescind = (struct vmbus_channel_rescind_offer *)ctx->msg.payload;
1249 rescind->header.msgtype = CHANNELMSG_RESCIND_CHANNELOFFER;
1250 rescind->child_relid = channel->offermsg.child_relid;
1252 INIT_WORK(&ctx->work, vmbus_onmessage_work);
1254 queue_work(vmbus_connection.work_queue, &ctx->work);
1256 #endif /* CONFIG_PM_SLEEP */
1259 * Schedule all channels with events pending
1261 static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu)
1263 unsigned long *recv_int_page;
1267 * The event page can be directly checked to get the id of
1268 * the channel that has the interrupt pending.
1270 void *page_addr = hv_cpu->synic_event_page;
1271 union hv_synic_event_flags *event
1272 = (union hv_synic_event_flags *)page_addr +
1275 maxbits = HV_EVENT_FLAGS_COUNT;
1276 recv_int_page = event->flags;
1278 if (unlikely(!recv_int_page))
1281 for_each_set_bit(relid, recv_int_page, maxbits) {
1282 void (*callback_fn)(void *context);
1283 struct vmbus_channel *channel;
1285 if (!sync_test_and_clear_bit(relid, recv_int_page))
1288 /* Special case - vmbus channel protocol msg */
1293 * Pairs with the kfree_rcu() in vmbus_chan_release().
1294 * Guarantees that the channel data structure doesn't
1295 * get freed while the channel pointer below is being
1300 /* Find channel based on relid */
1301 channel = relid2channel(relid);
1302 if (channel == NULL)
1303 goto sched_unlock_rcu;
1305 if (channel->rescind)
1306 goto sched_unlock_rcu;
1309 * Make sure that the ring buffer data structure doesn't get
1310 * freed while we dereference the ring buffer pointer. Test
1311 * for the channel's onchannel_callback being NULL within a
1312 * sched_lock critical section. See also the inline comments
1313 * in vmbus_reset_channel_cb().
1315 spin_lock(&channel->sched_lock);
1317 callback_fn = channel->onchannel_callback;
1318 if (unlikely(callback_fn == NULL))
1321 trace_vmbus_chan_sched(channel);
1323 ++channel->interrupts;
1325 switch (channel->callback_mode) {
1327 (*callback_fn)(channel->channel_callback_context);
1330 case HV_CALL_BATCHED:
1331 hv_begin_read(&channel->inbound);
1333 case HV_CALL_DIRECT:
1334 tasklet_schedule(&channel->callback_event);
1338 spin_unlock(&channel->sched_lock);
1344 static void vmbus_isr(void)
1346 struct hv_per_cpu_context *hv_cpu
1347 = this_cpu_ptr(hv_context.cpu_context);
1349 struct hv_message *msg;
1351 vmbus_chan_sched(hv_cpu);
1353 page_addr = hv_cpu->synic_message_page;
1354 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
1356 /* Check if there are actual msgs to be processed */
1357 if (msg->header.message_type != HVMSG_NONE) {
1358 if (msg->header.message_type == HVMSG_TIMER_EXPIRED) {
1360 vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED);
1362 tasklet_schedule(&hv_cpu->msg_dpc);
1365 add_interrupt_randomness(vmbus_interrupt);
1368 static irqreturn_t vmbus_percpu_isr(int irq, void *dev_id)
1375 * Callback from kmsg_dump. Grab as much as possible from the end of the kmsg
1376 * buffer and call into Hyper-V to transfer the data.
1378 static void hv_kmsg_dump(struct kmsg_dumper *dumper,
1379 enum kmsg_dump_reason reason)
1381 struct kmsg_dump_iter iter;
1382 size_t bytes_written;
1384 /* We are only interested in panics. */
1385 if ((reason != KMSG_DUMP_PANIC) || (!sysctl_record_panic_msg))
1389 * Write dump contents to the page. No need to synchronize; panic should
1390 * be single-threaded.
1392 kmsg_dump_rewind(&iter);
1393 kmsg_dump_get_buffer(&iter, false, hv_panic_page, HV_HYP_PAGE_SIZE,
1398 * P3 to contain the physical address of the panic page & P4 to
1399 * contain the size of the panic data in that page. Rest of the
1400 * registers are no-op when the NOTIFY_MSG flag is set.
1402 hv_set_register(HV_REGISTER_CRASH_P0, 0);
1403 hv_set_register(HV_REGISTER_CRASH_P1, 0);
1404 hv_set_register(HV_REGISTER_CRASH_P2, 0);
1405 hv_set_register(HV_REGISTER_CRASH_P3, virt_to_phys(hv_panic_page));
1406 hv_set_register(HV_REGISTER_CRASH_P4, bytes_written);
1409 * Let Hyper-V know there is crash data available along with
1410 * the panic message.
1412 hv_set_register(HV_REGISTER_CRASH_CTL,
1413 (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG));
1416 static struct kmsg_dumper hv_kmsg_dumper = {
1417 .dump = hv_kmsg_dump,
1420 static void hv_kmsg_dump_register(void)
1424 hv_panic_page = hv_alloc_hyperv_zeroed_page();
1425 if (!hv_panic_page) {
1426 pr_err("Hyper-V: panic message page memory allocation failed\n");
1430 ret = kmsg_dump_register(&hv_kmsg_dumper);
1432 pr_err("Hyper-V: kmsg dump register error 0x%x\n", ret);
1433 hv_free_hyperv_page((unsigned long)hv_panic_page);
1434 hv_panic_page = NULL;
1438 static struct ctl_table_header *hv_ctl_table_hdr;
1441 * sysctl option to allow the user to control whether kmsg data should be
1442 * reported to Hyper-V on panic.
1444 static struct ctl_table hv_ctl_table[] = {
1446 .procname = "hyperv_record_panic_msg",
1447 .data = &sysctl_record_panic_msg,
1448 .maxlen = sizeof(int),
1450 .proc_handler = proc_dointvec_minmax,
1451 .extra1 = SYSCTL_ZERO,
1452 .extra2 = SYSCTL_ONE
1457 static struct ctl_table hv_root_table[] = {
1459 .procname = "kernel",
1461 .child = hv_ctl_table
1467 * vmbus_bus_init -Main vmbus driver initialization routine.
1470 * - initialize the vmbus driver context
1471 * - invoke the vmbus hv main init routine
1472 * - retrieve the channel offers
1474 static int vmbus_bus_init(void)
1480 pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
1484 ret = bus_register(&hv_bus);
1489 * VMbus interrupts are best modeled as per-cpu interrupts. If
1490 * on an architecture with support for per-cpu IRQs (e.g. ARM64),
1491 * allocate a per-cpu IRQ using standard Linux kernel functionality.
1492 * If not on such an architecture (e.g., x86/x64), then rely on
1493 * code in the arch-specific portion of the code tree to connect
1494 * the VMbus interrupt handler.
1497 if (vmbus_irq == -1) {
1498 hv_setup_vmbus_handler(vmbus_isr);
1500 vmbus_evt = alloc_percpu(long);
1501 ret = request_percpu_irq(vmbus_irq, vmbus_percpu_isr,
1502 "Hyper-V VMbus", vmbus_evt);
1504 pr_err("Can't request Hyper-V VMbus IRQ %d, Err %d",
1506 free_percpu(vmbus_evt);
1511 ret = hv_synic_alloc();
1516 * Initialize the per-cpu interrupt state and stimer state.
1517 * Then connect to the host.
1519 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hyperv/vmbus:online",
1520 hv_synic_init, hv_synic_cleanup);
1523 hyperv_cpuhp_online = ret;
1525 ret = vmbus_connect();
1529 if (hv_is_isolation_supported())
1530 sysctl_record_panic_msg = 0;
1533 * Only register if the crash MSRs are available
1535 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
1536 u64 hyperv_crash_ctl;
1538 * Panic message recording (sysctl_record_panic_msg)
1539 * is enabled by default in non-isolated guests and
1540 * disabled by default in isolated guests; the panic
1541 * message recording won't be available in isolated
1542 * guests should the following registration fail.
1544 hv_ctl_table_hdr = register_sysctl_table(hv_root_table);
1545 if (!hv_ctl_table_hdr)
1546 pr_err("Hyper-V: sysctl table register error");
1549 * Register for panic kmsg callback only if the right
1550 * capability is supported by the hypervisor.
1552 hyperv_crash_ctl = hv_get_register(HV_REGISTER_CRASH_CTL);
1553 if (hyperv_crash_ctl & HV_CRASH_CTL_CRASH_NOTIFY_MSG)
1554 hv_kmsg_dump_register();
1556 register_die_notifier(&hyperv_die_block);
1560 * Always register the panic notifier because we need to unload
1561 * the VMbus channel connection to prevent any VMbus
1562 * activity after the VM panics.
1564 atomic_notifier_chain_register(&panic_notifier_list,
1565 &hyperv_panic_block);
1567 vmbus_request_offers();
1572 cpuhp_remove_state(hyperv_cpuhp_online);
1576 if (vmbus_irq == -1) {
1577 hv_remove_vmbus_handler();
1579 free_percpu_irq(vmbus_irq, vmbus_evt);
1580 free_percpu(vmbus_evt);
1583 bus_unregister(&hv_bus);
1584 unregister_sysctl_table(hv_ctl_table_hdr);
1585 hv_ctl_table_hdr = NULL;
1590 * __vmbus_child_driver_register() - Register a vmbus's driver
1591 * @hv_driver: Pointer to driver structure you want to register
1592 * @owner: owner module of the drv
1593 * @mod_name: module name string
1595 * Registers the given driver with Linux through the 'driver_register()' call
1596 * and sets up the hyper-v vmbus handling for this driver.
1597 * It will return the state of the 'driver_register()' call.
1600 int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
1604 pr_info("registering driver %s\n", hv_driver->name);
1606 ret = vmbus_exists();
1610 hv_driver->driver.name = hv_driver->name;
1611 hv_driver->driver.owner = owner;
1612 hv_driver->driver.mod_name = mod_name;
1613 hv_driver->driver.bus = &hv_bus;
1615 spin_lock_init(&hv_driver->dynids.lock);
1616 INIT_LIST_HEAD(&hv_driver->dynids.list);
1618 ret = driver_register(&hv_driver->driver);
1622 EXPORT_SYMBOL_GPL(__vmbus_driver_register);
1625 * vmbus_driver_unregister() - Unregister a vmbus's driver
1626 * @hv_driver: Pointer to driver structure you want to
1629 * Un-register the given driver that was previous registered with a call to
1630 * vmbus_driver_register()
1632 void vmbus_driver_unregister(struct hv_driver *hv_driver)
1634 pr_info("unregistering driver %s\n", hv_driver->name);
1636 if (!vmbus_exists()) {
1637 driver_unregister(&hv_driver->driver);
1638 vmbus_free_dynids(hv_driver);
1641 EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
1645 * Called when last reference to channel is gone.
1647 static void vmbus_chan_release(struct kobject *kobj)
1649 struct vmbus_channel *channel
1650 = container_of(kobj, struct vmbus_channel, kobj);
1652 kfree_rcu(channel, rcu);
1655 struct vmbus_chan_attribute {
1656 struct attribute attr;
1657 ssize_t (*show)(struct vmbus_channel *chan, char *buf);
1658 ssize_t (*store)(struct vmbus_channel *chan,
1659 const char *buf, size_t count);
1661 #define VMBUS_CHAN_ATTR(_name, _mode, _show, _store) \
1662 struct vmbus_chan_attribute chan_attr_##_name \
1663 = __ATTR(_name, _mode, _show, _store)
1664 #define VMBUS_CHAN_ATTR_RW(_name) \
1665 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RW(_name)
1666 #define VMBUS_CHAN_ATTR_RO(_name) \
1667 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RO(_name)
1668 #define VMBUS_CHAN_ATTR_WO(_name) \
1669 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_WO(_name)
1671 static ssize_t vmbus_chan_attr_show(struct kobject *kobj,
1672 struct attribute *attr, char *buf)
1674 const struct vmbus_chan_attribute *attribute
1675 = container_of(attr, struct vmbus_chan_attribute, attr);
1676 struct vmbus_channel *chan
1677 = container_of(kobj, struct vmbus_channel, kobj);
1679 if (!attribute->show)
1682 return attribute->show(chan, buf);
1685 static ssize_t vmbus_chan_attr_store(struct kobject *kobj,
1686 struct attribute *attr, const char *buf,
1689 const struct vmbus_chan_attribute *attribute
1690 = container_of(attr, struct vmbus_chan_attribute, attr);
1691 struct vmbus_channel *chan
1692 = container_of(kobj, struct vmbus_channel, kobj);
1694 if (!attribute->store)
1697 return attribute->store(chan, buf, count);
1700 static const struct sysfs_ops vmbus_chan_sysfs_ops = {
1701 .show = vmbus_chan_attr_show,
1702 .store = vmbus_chan_attr_store,
1705 static ssize_t out_mask_show(struct vmbus_channel *channel, char *buf)
1707 struct hv_ring_buffer_info *rbi = &channel->outbound;
1710 mutex_lock(&rbi->ring_buffer_mutex);
1711 if (!rbi->ring_buffer) {
1712 mutex_unlock(&rbi->ring_buffer_mutex);
1716 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1717 mutex_unlock(&rbi->ring_buffer_mutex);
1720 static VMBUS_CHAN_ATTR_RO(out_mask);
1722 static ssize_t in_mask_show(struct vmbus_channel *channel, char *buf)
1724 struct hv_ring_buffer_info *rbi = &channel->inbound;
1727 mutex_lock(&rbi->ring_buffer_mutex);
1728 if (!rbi->ring_buffer) {
1729 mutex_unlock(&rbi->ring_buffer_mutex);
1733 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1734 mutex_unlock(&rbi->ring_buffer_mutex);
1737 static VMBUS_CHAN_ATTR_RO(in_mask);
1739 static ssize_t read_avail_show(struct vmbus_channel *channel, char *buf)
1741 struct hv_ring_buffer_info *rbi = &channel->inbound;
1744 mutex_lock(&rbi->ring_buffer_mutex);
1745 if (!rbi->ring_buffer) {
1746 mutex_unlock(&rbi->ring_buffer_mutex);
1750 ret = sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi));
1751 mutex_unlock(&rbi->ring_buffer_mutex);
1754 static VMBUS_CHAN_ATTR_RO(read_avail);
1756 static ssize_t write_avail_show(struct vmbus_channel *channel, char *buf)
1758 struct hv_ring_buffer_info *rbi = &channel->outbound;
1761 mutex_lock(&rbi->ring_buffer_mutex);
1762 if (!rbi->ring_buffer) {
1763 mutex_unlock(&rbi->ring_buffer_mutex);
1767 ret = sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi));
1768 mutex_unlock(&rbi->ring_buffer_mutex);
1771 static VMBUS_CHAN_ATTR_RO(write_avail);
1773 static ssize_t target_cpu_show(struct vmbus_channel *channel, char *buf)
1775 return sprintf(buf, "%u\n", channel->target_cpu);
1777 static ssize_t target_cpu_store(struct vmbus_channel *channel,
1778 const char *buf, size_t count)
1780 u32 target_cpu, origin_cpu;
1781 ssize_t ret = count;
1783 if (vmbus_proto_version < VERSION_WIN10_V4_1)
1786 if (sscanf(buf, "%uu", &target_cpu) != 1)
1789 /* Validate target_cpu for the cpumask_test_cpu() operation below. */
1790 if (target_cpu >= nr_cpumask_bits)
1793 /* No CPUs should come up or down during this. */
1796 if (!cpu_online(target_cpu)) {
1802 * Synchronizes target_cpu_store() and channel closure:
1804 * { Initially: state = CHANNEL_OPENED }
1808 * [target_cpu_store()] [vmbus_disconnect_ring()]
1810 * LOCK channel_mutex LOCK channel_mutex
1811 * LOAD r1 = state LOAD r2 = state
1812 * IF (r1 == CHANNEL_OPENED) IF (r2 == CHANNEL_OPENED)
1813 * SEND MODIFYCHANNEL STORE state = CHANNEL_OPEN
1814 * [...] SEND CLOSECHANNEL
1815 * UNLOCK channel_mutex UNLOCK channel_mutex
1817 * Forbids: r1 == r2 == CHANNEL_OPENED (i.e., CPU1's LOCK precedes
1818 * CPU2's LOCK) && CPU2's SEND precedes CPU1's SEND
1820 * Note. The host processes the channel messages "sequentially", in
1821 * the order in which they are received on a per-partition basis.
1823 mutex_lock(&vmbus_connection.channel_mutex);
1826 * Hyper-V will ignore MODIFYCHANNEL messages for "non-open" channels;
1827 * avoid sending the message and fail here for such channels.
1829 if (channel->state != CHANNEL_OPENED_STATE) {
1831 goto cpu_store_unlock;
1834 origin_cpu = channel->target_cpu;
1835 if (target_cpu == origin_cpu)
1836 goto cpu_store_unlock;
1838 if (vmbus_send_modifychannel(channel,
1839 hv_cpu_number_to_vp_number(target_cpu))) {
1841 goto cpu_store_unlock;
1845 * For version before VERSION_WIN10_V5_3, the following warning holds:
1847 * Warning. At this point, there is *no* guarantee that the host will
1848 * have successfully processed the vmbus_send_modifychannel() request.
1849 * See the header comment of vmbus_send_modifychannel() for more info.
1851 * Lags in the processing of the above vmbus_send_modifychannel() can
1852 * result in missed interrupts if the "old" target CPU is taken offline
1853 * before Hyper-V starts sending interrupts to the "new" target CPU.
1854 * But apart from this offlining scenario, the code tolerates such
1855 * lags. It will function correctly even if a channel interrupt comes
1856 * in on a CPU that is different from the channel target_cpu value.
1859 channel->target_cpu = target_cpu;
1861 /* See init_vp_index(). */
1862 if (hv_is_perf_channel(channel))
1863 hv_update_allocated_cpus(origin_cpu, target_cpu);
1865 /* Currently set only for storvsc channels. */
1866 if (channel->change_target_cpu_callback) {
1867 (*channel->change_target_cpu_callback)(channel,
1868 origin_cpu, target_cpu);
1872 mutex_unlock(&vmbus_connection.channel_mutex);
1876 static VMBUS_CHAN_ATTR(cpu, 0644, target_cpu_show, target_cpu_store);
1878 static ssize_t channel_pending_show(struct vmbus_channel *channel,
1881 return sprintf(buf, "%d\n",
1882 channel_pending(channel,
1883 vmbus_connection.monitor_pages[1]));
1885 static VMBUS_CHAN_ATTR(pending, 0444, channel_pending_show, NULL);
1887 static ssize_t channel_latency_show(struct vmbus_channel *channel,
1890 return sprintf(buf, "%d\n",
1891 channel_latency(channel,
1892 vmbus_connection.monitor_pages[1]));
1894 static VMBUS_CHAN_ATTR(latency, 0444, channel_latency_show, NULL);
1896 static ssize_t channel_interrupts_show(struct vmbus_channel *channel, char *buf)
1898 return sprintf(buf, "%llu\n", channel->interrupts);
1900 static VMBUS_CHAN_ATTR(interrupts, 0444, channel_interrupts_show, NULL);
1902 static ssize_t channel_events_show(struct vmbus_channel *channel, char *buf)
1904 return sprintf(buf, "%llu\n", channel->sig_events);
1906 static VMBUS_CHAN_ATTR(events, 0444, channel_events_show, NULL);
1908 static ssize_t channel_intr_in_full_show(struct vmbus_channel *channel,
1911 return sprintf(buf, "%llu\n",
1912 (unsigned long long)channel->intr_in_full);
1914 static VMBUS_CHAN_ATTR(intr_in_full, 0444, channel_intr_in_full_show, NULL);
1916 static ssize_t channel_intr_out_empty_show(struct vmbus_channel *channel,
1919 return sprintf(buf, "%llu\n",
1920 (unsigned long long)channel->intr_out_empty);
1922 static VMBUS_CHAN_ATTR(intr_out_empty, 0444, channel_intr_out_empty_show, NULL);
1924 static ssize_t channel_out_full_first_show(struct vmbus_channel *channel,
1927 return sprintf(buf, "%llu\n",
1928 (unsigned long long)channel->out_full_first);
1930 static VMBUS_CHAN_ATTR(out_full_first, 0444, channel_out_full_first_show, NULL);
1932 static ssize_t channel_out_full_total_show(struct vmbus_channel *channel,
1935 return sprintf(buf, "%llu\n",
1936 (unsigned long long)channel->out_full_total);
1938 static VMBUS_CHAN_ATTR(out_full_total, 0444, channel_out_full_total_show, NULL);
1940 static ssize_t subchannel_monitor_id_show(struct vmbus_channel *channel,
1943 return sprintf(buf, "%u\n", channel->offermsg.monitorid);
1945 static VMBUS_CHAN_ATTR(monitor_id, 0444, subchannel_monitor_id_show, NULL);
1947 static ssize_t subchannel_id_show(struct vmbus_channel *channel,
1950 return sprintf(buf, "%u\n",
1951 channel->offermsg.offer.sub_channel_index);
1953 static VMBUS_CHAN_ATTR_RO(subchannel_id);
1955 static struct attribute *vmbus_chan_attrs[] = {
1956 &chan_attr_out_mask.attr,
1957 &chan_attr_in_mask.attr,
1958 &chan_attr_read_avail.attr,
1959 &chan_attr_write_avail.attr,
1960 &chan_attr_cpu.attr,
1961 &chan_attr_pending.attr,
1962 &chan_attr_latency.attr,
1963 &chan_attr_interrupts.attr,
1964 &chan_attr_events.attr,
1965 &chan_attr_intr_in_full.attr,
1966 &chan_attr_intr_out_empty.attr,
1967 &chan_attr_out_full_first.attr,
1968 &chan_attr_out_full_total.attr,
1969 &chan_attr_monitor_id.attr,
1970 &chan_attr_subchannel_id.attr,
1975 * Channel-level attribute_group callback function. Returns the permission for
1976 * each attribute, and returns 0 if an attribute is not visible.
1978 static umode_t vmbus_chan_attr_is_visible(struct kobject *kobj,
1979 struct attribute *attr, int idx)
1981 const struct vmbus_channel *channel =
1982 container_of(kobj, struct vmbus_channel, kobj);
1984 /* Hide the monitor attributes if the monitor mechanism is not used. */
1985 if (!channel->offermsg.monitor_allocated &&
1986 (attr == &chan_attr_pending.attr ||
1987 attr == &chan_attr_latency.attr ||
1988 attr == &chan_attr_monitor_id.attr))
1994 static struct attribute_group vmbus_chan_group = {
1995 .attrs = vmbus_chan_attrs,
1996 .is_visible = vmbus_chan_attr_is_visible
1999 static struct kobj_type vmbus_chan_ktype = {
2000 .sysfs_ops = &vmbus_chan_sysfs_ops,
2001 .release = vmbus_chan_release,
2005 * vmbus_add_channel_kobj - setup a sub-directory under device/channels
2007 int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
2009 const struct device *device = &dev->device;
2010 struct kobject *kobj = &channel->kobj;
2011 u32 relid = channel->offermsg.child_relid;
2014 kobj->kset = dev->channels_kset;
2015 ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
2022 ret = sysfs_create_group(kobj, &vmbus_chan_group);
2026 * The calling functions' error handling paths will cleanup the
2027 * empty channel directory.
2030 dev_err(device, "Unable to set up channel sysfs files\n");
2034 kobject_uevent(kobj, KOBJ_ADD);
2040 * vmbus_remove_channel_attr_group - remove the channel's attribute group
2042 void vmbus_remove_channel_attr_group(struct vmbus_channel *channel)
2044 sysfs_remove_group(&channel->kobj, &vmbus_chan_group);
2048 * vmbus_device_create - Creates and registers a new child device
2051 struct hv_device *vmbus_device_create(const guid_t *type,
2052 const guid_t *instance,
2053 struct vmbus_channel *channel)
2055 struct hv_device *child_device_obj;
2057 child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
2058 if (!child_device_obj) {
2059 pr_err("Unable to allocate device object for child device\n");
2063 child_device_obj->channel = channel;
2064 guid_copy(&child_device_obj->dev_type, type);
2065 guid_copy(&child_device_obj->dev_instance, instance);
2066 child_device_obj->vendor_id = 0x1414; /* MSFT vendor ID */
2068 return child_device_obj;
2072 * vmbus_device_register - Register the child device
2074 int vmbus_device_register(struct hv_device *child_device_obj)
2076 struct kobject *kobj = &child_device_obj->device.kobj;
2079 dev_set_name(&child_device_obj->device, "%pUl",
2080 &child_device_obj->channel->offermsg.offer.if_instance);
2082 child_device_obj->device.bus = &hv_bus;
2083 child_device_obj->device.parent = &hv_acpi_dev->dev;
2084 child_device_obj->device.release = vmbus_device_release;
2086 child_device_obj->device.dma_parms = &child_device_obj->dma_parms;
2087 child_device_obj->device.dma_mask = &child_device_obj->dma_mask;
2088 dma_set_mask(&child_device_obj->device, DMA_BIT_MASK(64));
2091 * Register with the LDM. This will kick off the driver/device
2092 * binding...which will eventually call vmbus_match() and vmbus_probe()
2094 ret = device_register(&child_device_obj->device);
2096 pr_err("Unable to register child device\n");
2100 child_device_obj->channels_kset = kset_create_and_add("channels",
2102 if (!child_device_obj->channels_kset) {
2104 goto err_dev_unregister;
2107 ret = vmbus_add_channel_kobj(child_device_obj,
2108 child_device_obj->channel);
2110 pr_err("Unable to register primary channeln");
2111 goto err_kset_unregister;
2113 hv_debug_add_dev_dir(child_device_obj);
2117 err_kset_unregister:
2118 kset_unregister(child_device_obj->channels_kset);
2121 device_unregister(&child_device_obj->device);
2126 * vmbus_device_unregister - Remove the specified child device
2129 void vmbus_device_unregister(struct hv_device *device_obj)
2131 pr_debug("child device %s unregistered\n",
2132 dev_name(&device_obj->device));
2134 kset_unregister(device_obj->channels_kset);
2137 * Kick off the process of unregistering the device.
2138 * This will call vmbus_remove() and eventually vmbus_device_release()
2140 device_unregister(&device_obj->device);
2145 * VMBUS is an acpi enumerated device. Get the information we
2148 #define VTPM_BASE_ADDRESS 0xfed40000
2149 static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
2151 resource_size_t start = 0;
2152 resource_size_t end = 0;
2153 struct resource *new_res;
2154 struct resource **old_res = &hyperv_mmio;
2155 struct resource **prev_res = NULL;
2158 switch (res->type) {
2161 * "Address" descriptors are for bus windows. Ignore
2162 * "memory" descriptors, which are for registers on
2165 case ACPI_RESOURCE_TYPE_ADDRESS32:
2166 start = res->data.address32.address.minimum;
2167 end = res->data.address32.address.maximum;
2170 case ACPI_RESOURCE_TYPE_ADDRESS64:
2171 start = res->data.address64.address.minimum;
2172 end = res->data.address64.address.maximum;
2176 * The IRQ information is needed only on ARM64, which Hyper-V
2177 * sets up in the extended format. IRQ information is present
2178 * on x86/x64 in the non-extended format but it is not used by
2179 * Linux. So don't bother checking for the non-extended format.
2181 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
2182 if (!acpi_dev_resource_interrupt(res, 0, &r)) {
2183 pr_err("Unable to parse Hyper-V ACPI interrupt\n");
2186 /* ARM64 INTID for VMbus */
2187 vmbus_interrupt = res->data.extended_irq.interrupts[0];
2188 /* Linux IRQ number */
2189 vmbus_irq = r.start;
2193 /* Unused resource type */
2198 * Ignore ranges that are below 1MB, as they're not
2199 * necessary or useful here.
2204 new_res = kzalloc(sizeof(*new_res), GFP_ATOMIC);
2206 return AE_NO_MEMORY;
2208 /* If this range overlaps the virtual TPM, truncate it. */
2209 if (end > VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS)
2210 end = VTPM_BASE_ADDRESS;
2212 new_res->name = "hyperv mmio";
2213 new_res->flags = IORESOURCE_MEM;
2214 new_res->start = start;
2218 * If two ranges are adjacent, merge them.
2226 if (((*old_res)->end + 1) == new_res->start) {
2227 (*old_res)->end = new_res->end;
2232 if ((*old_res)->start == new_res->end + 1) {
2233 (*old_res)->start = new_res->start;
2238 if ((*old_res)->start > new_res->end) {
2239 new_res->sibling = *old_res;
2241 (*prev_res)->sibling = new_res;
2247 old_res = &(*old_res)->sibling;
2254 static int vmbus_acpi_remove(struct acpi_device *device)
2256 struct resource *cur_res;
2257 struct resource *next_res;
2261 __release_region(hyperv_mmio, fb_mmio->start,
2262 resource_size(fb_mmio));
2266 for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) {
2267 next_res = cur_res->sibling;
2275 static void vmbus_reserve_fb(void)
2279 * Make a claim for the frame buffer in the resource tree under the
2280 * first node, which will be the one below 4GB. The length seems to
2281 * be underreported, particularly in a Generation 1 VM. So start out
2282 * reserving a larger area and make it smaller until it succeeds.
2285 if (screen_info.lfb_base) {
2286 if (efi_enabled(EFI_BOOT))
2287 size = max_t(__u32, screen_info.lfb_size, 0x800000);
2289 size = max_t(__u32, screen_info.lfb_size, 0x4000000);
2291 for (; !fb_mmio && (size >= 0x100000); size >>= 1) {
2292 fb_mmio = __request_region(hyperv_mmio,
2293 screen_info.lfb_base, size,
2300 * vmbus_allocate_mmio() - Pick a memory-mapped I/O range.
2301 * @new: If successful, supplied a pointer to the
2302 * allocated MMIO space.
2303 * @device_obj: Identifies the caller
2304 * @min: Minimum guest physical address of the
2306 * @max: Maximum guest physical address
2307 * @size: Size of the range to be allocated
2308 * @align: Alignment of the range to be allocated
2309 * @fb_overlap_ok: Whether this allocation can be allowed
2310 * to overlap the video frame buffer.
2312 * This function walks the resources granted to VMBus by the
2313 * _CRS object in the ACPI namespace underneath the parent
2314 * "bridge" whether that's a root PCI bus in the Generation 1
2315 * case or a Module Device in the Generation 2 case. It then
2316 * attempts to allocate from the global MMIO pool in a way that
2317 * matches the constraints supplied in these parameters and by
2320 * Return: 0 on success, -errno on failure
2322 int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
2323 resource_size_t min, resource_size_t max,
2324 resource_size_t size, resource_size_t align,
2327 struct resource *iter, *shadow;
2328 resource_size_t range_min, range_max, start;
2329 const char *dev_n = dev_name(&device_obj->device);
2333 mutex_lock(&hyperv_mmio_lock);
2336 * If overlaps with frame buffers are allowed, then first attempt to
2337 * make the allocation from within the reserved region. Because it
2338 * is already reserved, no shadow allocation is necessary.
2340 if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) &&
2341 !(max < fb_mmio->start)) {
2343 range_min = fb_mmio->start;
2344 range_max = fb_mmio->end;
2345 start = (range_min + align - 1) & ~(align - 1);
2346 for (; start + size - 1 <= range_max; start += align) {
2347 *new = request_mem_region_exclusive(start, size, dev_n);
2355 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
2356 if ((iter->start >= max) || (iter->end <= min))
2359 range_min = iter->start;
2360 range_max = iter->end;
2361 start = (range_min + align - 1) & ~(align - 1);
2362 for (; start + size - 1 <= range_max; start += align) {
2363 shadow = __request_region(iter, start, size, NULL,
2368 *new = request_mem_region_exclusive(start, size, dev_n);
2370 shadow->name = (char *)*new;
2375 __release_region(iter, start, size);
2380 mutex_unlock(&hyperv_mmio_lock);
2383 EXPORT_SYMBOL_GPL(vmbus_allocate_mmio);
2386 * vmbus_free_mmio() - Free a memory-mapped I/O range.
2387 * @start: Base address of region to release.
2388 * @size: Size of the range to be allocated
2390 * This function releases anything requested by
2391 * vmbus_mmio_allocate().
2393 void vmbus_free_mmio(resource_size_t start, resource_size_t size)
2395 struct resource *iter;
2397 mutex_lock(&hyperv_mmio_lock);
2398 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
2399 if ((iter->start >= start + size) || (iter->end <= start))
2402 __release_region(iter, start, size);
2404 release_mem_region(start, size);
2405 mutex_unlock(&hyperv_mmio_lock);
2408 EXPORT_SYMBOL_GPL(vmbus_free_mmio);
2410 static int vmbus_acpi_add(struct acpi_device *device)
2413 int ret_val = -ENODEV;
2414 struct acpi_device *ancestor;
2416 hv_acpi_dev = device;
2419 * Older versions of Hyper-V for ARM64 fail to include the _CCA
2420 * method on the top level VMbus device in the DSDT. But devices
2421 * are hardware coherent in all current Hyper-V use cases, so fix
2422 * up the ACPI device to behave as if _CCA is present and indicates
2423 * hardware coherence.
2425 ACPI_COMPANION_SET(&device->dev, device);
2426 if (IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED) &&
2427 device_get_dma_attr(&device->dev) == DEV_DMA_NOT_SUPPORTED) {
2428 pr_info("No ACPI _CCA found; assuming coherent device I/O\n");
2429 device->flags.cca_seen = true;
2430 device->flags.coherent_dma = true;
2433 result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
2434 vmbus_walk_resources, NULL);
2436 if (ACPI_FAILURE(result))
2439 * Some ancestor of the vmbus acpi device (Gen1 or Gen2
2440 * firmware) is the VMOD that has the mmio ranges. Get that.
2442 for (ancestor = device->parent; ancestor; ancestor = ancestor->parent) {
2443 result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS,
2444 vmbus_walk_resources, NULL);
2446 if (ACPI_FAILURE(result))
2456 complete(&probe_event);
2458 vmbus_acpi_remove(device);
2462 #ifdef CONFIG_PM_SLEEP
2463 static int vmbus_bus_suspend(struct device *dev)
2465 struct vmbus_channel *channel, *sc;
2467 while (atomic_read(&vmbus_connection.offer_in_progress) != 0) {
2469 * We wait here until the completion of any channel
2470 * offers that are currently in progress.
2472 usleep_range(1000, 2000);
2475 mutex_lock(&vmbus_connection.channel_mutex);
2476 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
2477 if (!is_hvsock_channel(channel))
2480 vmbus_force_channel_rescinded(channel);
2482 mutex_unlock(&vmbus_connection.channel_mutex);
2485 * Wait until all the sub-channels and hv_sock channels have been
2486 * cleaned up. Sub-channels should be destroyed upon suspend, otherwise
2487 * they would conflict with the new sub-channels that will be created
2488 * in the resume path. hv_sock channels should also be destroyed, but
2489 * a hv_sock channel of an established hv_sock connection can not be
2490 * really destroyed since it may still be referenced by the userspace
2491 * application, so we just force the hv_sock channel to be rescinded
2492 * by vmbus_force_channel_rescinded(), and the userspace application
2493 * will thoroughly destroy the channel after hibernation.
2495 * Note: the counter nr_chan_close_on_suspend may never go above 0 if
2496 * the VM has no sub-channel and hv_sock channel, e.g. a 1-vCPU VM.
2498 if (atomic_read(&vmbus_connection.nr_chan_close_on_suspend) > 0)
2499 wait_for_completion(&vmbus_connection.ready_for_suspend_event);
2501 if (atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) != 0) {
2502 pr_err("Can not suspend due to a previous failed resuming\n");
2506 mutex_lock(&vmbus_connection.channel_mutex);
2508 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
2510 * Remove the channel from the array of channels and invalidate
2511 * the channel's relid. Upon resume, vmbus_onoffer() will fix
2512 * up the relid (and other fields, if necessary) and add the
2513 * channel back to the array.
2515 vmbus_channel_unmap_relid(channel);
2516 channel->offermsg.child_relid = INVALID_RELID;
2518 if (is_hvsock_channel(channel)) {
2519 if (!channel->rescind) {
2520 pr_err("hv_sock channel not rescinded!\n");
2526 list_for_each_entry(sc, &channel->sc_list, sc_list) {
2527 pr_err("Sub-channel not deleted!\n");
2531 atomic_inc(&vmbus_connection.nr_chan_fixup_on_resume);
2534 mutex_unlock(&vmbus_connection.channel_mutex);
2536 vmbus_initiate_unload(false);
2538 /* Reset the event for the next resume. */
2539 reinit_completion(&vmbus_connection.ready_for_resume_event);
2544 static int vmbus_bus_resume(struct device *dev)
2546 struct vmbus_channel_msginfo *msginfo;
2551 * We only use the 'vmbus_proto_version', which was in use before
2552 * hibernation, to re-negotiate with the host.
2554 if (!vmbus_proto_version) {
2555 pr_err("Invalid proto version = 0x%x\n", vmbus_proto_version);
2559 msgsize = sizeof(*msginfo) +
2560 sizeof(struct vmbus_channel_initiate_contact);
2562 msginfo = kzalloc(msgsize, GFP_KERNEL);
2564 if (msginfo == NULL)
2567 ret = vmbus_negotiate_version(msginfo, vmbus_proto_version);
2574 WARN_ON(atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) == 0);
2576 vmbus_request_offers();
2578 if (wait_for_completion_timeout(
2579 &vmbus_connection.ready_for_resume_event, 10 * HZ) == 0)
2580 pr_err("Some vmbus device is missing after suspending?\n");
2582 /* Reset the event for the next suspend. */
2583 reinit_completion(&vmbus_connection.ready_for_suspend_event);
2588 #define vmbus_bus_suspend NULL
2589 #define vmbus_bus_resume NULL
2590 #endif /* CONFIG_PM_SLEEP */
2592 static const struct acpi_device_id vmbus_acpi_device_ids[] = {
2597 MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
2600 * Note: we must use the "no_irq" ops, otherwise hibernation can not work with
2601 * PCI device assignment, because "pci_dev_pm_ops" uses the "noirq" ops: in
2602 * the resume path, the pci "noirq" restore op runs before "non-noirq" op (see
2603 * resume_target_kernel() -> dpm_resume_start(), and hibernation_restore() ->
2604 * dpm_resume_end()). This means vmbus_bus_resume() and the pci-hyperv's
2605 * resume callback must also run via the "noirq" ops.
2607 * Set suspend_noirq/resume_noirq to NULL for Suspend-to-Idle: see the comment
2608 * earlier in this file before vmbus_pm.
2611 static const struct dev_pm_ops vmbus_bus_pm = {
2612 .suspend_noirq = NULL,
2613 .resume_noirq = NULL,
2614 .freeze_noirq = vmbus_bus_suspend,
2615 .thaw_noirq = vmbus_bus_resume,
2616 .poweroff_noirq = vmbus_bus_suspend,
2617 .restore_noirq = vmbus_bus_resume
2620 static struct acpi_driver vmbus_acpi_driver = {
2622 .ids = vmbus_acpi_device_ids,
2624 .add = vmbus_acpi_add,
2625 .remove = vmbus_acpi_remove,
2627 .drv.pm = &vmbus_bus_pm,
2630 static void hv_kexec_handler(void)
2632 hv_stimer_global_cleanup();
2633 vmbus_initiate_unload(false);
2634 /* Make sure conn_state is set as hv_synic_cleanup checks for it */
2636 cpuhp_remove_state(hyperv_cpuhp_online);
2639 static void hv_crash_handler(struct pt_regs *regs)
2643 vmbus_initiate_unload(true);
2645 * In crash handler we can't schedule synic cleanup for all CPUs,
2646 * doing the cleanup for current CPU only. This should be sufficient
2649 cpu = smp_processor_id();
2650 hv_stimer_cleanup(cpu);
2651 hv_synic_disable_regs(cpu);
2654 static int hv_synic_suspend(void)
2657 * When we reach here, all the non-boot CPUs have been offlined.
2658 * If we're in a legacy configuration where stimer Direct Mode is
2659 * not enabled, the stimers on the non-boot CPUs have been unbound
2660 * in hv_synic_cleanup() -> hv_stimer_legacy_cleanup() ->
2661 * hv_stimer_cleanup() -> clockevents_unbind_device().
2663 * hv_synic_suspend() only runs on CPU0 with interrupts disabled.
2664 * Here we do not call hv_stimer_legacy_cleanup() on CPU0 because:
2665 * 1) it's unnecessary as interrupts remain disabled between
2666 * syscore_suspend() and syscore_resume(): see create_image() and
2667 * resume_target_kernel()
2668 * 2) the stimer on CPU0 is automatically disabled later by
2669 * syscore_suspend() -> timekeeping_suspend() -> tick_suspend() -> ...
2670 * -> clockevents_shutdown() -> ... -> hv_ce_shutdown()
2671 * 3) a warning would be triggered if we call
2672 * clockevents_unbind_device(), which may sleep, in an
2673 * interrupts-disabled context.
2676 hv_synic_disable_regs(0);
2681 static void hv_synic_resume(void)
2683 hv_synic_enable_regs(0);
2686 * Note: we don't need to call hv_stimer_init(0), because the timer
2687 * on CPU0 is not unbound in hv_synic_suspend(), and the timer is
2688 * automatically re-enabled in timekeeping_resume().
2692 /* The callbacks run only on CPU0, with irqs_disabled. */
2693 static struct syscore_ops hv_synic_syscore_ops = {
2694 .suspend = hv_synic_suspend,
2695 .resume = hv_synic_resume,
2698 static int __init hv_acpi_init(void)
2702 if (!hv_is_hyperv_initialized())
2705 if (hv_root_partition)
2708 init_completion(&probe_event);
2711 * Get ACPI resources first.
2713 ret = acpi_bus_register_driver(&vmbus_acpi_driver);
2718 t = wait_for_completion_timeout(&probe_event, 5*HZ);
2725 * If we're on an architecture with a hardcoded hypervisor
2726 * vector (i.e. x86/x64), override the VMbus interrupt found
2727 * in the ACPI tables. Ensure vmbus_irq is not set since the
2728 * normal Linux IRQ mechanism is not used in this case.
2730 #ifdef HYPERVISOR_CALLBACK_VECTOR
2731 vmbus_interrupt = HYPERVISOR_CALLBACK_VECTOR;
2737 ret = vmbus_bus_init();
2741 hv_setup_kexec_handler(hv_kexec_handler);
2742 hv_setup_crash_handler(hv_crash_handler);
2744 register_syscore_ops(&hv_synic_syscore_ops);
2749 acpi_bus_unregister_driver(&vmbus_acpi_driver);
2754 static void __exit vmbus_exit(void)
2758 unregister_syscore_ops(&hv_synic_syscore_ops);
2760 hv_remove_kexec_handler();
2761 hv_remove_crash_handler();
2762 vmbus_connection.conn_state = DISCONNECTED;
2763 hv_stimer_global_cleanup();
2765 if (vmbus_irq == -1) {
2766 hv_remove_vmbus_handler();
2768 free_percpu_irq(vmbus_irq, vmbus_evt);
2769 free_percpu(vmbus_evt);
2771 for_each_online_cpu(cpu) {
2772 struct hv_per_cpu_context *hv_cpu
2773 = per_cpu_ptr(hv_context.cpu_context, cpu);
2775 tasklet_kill(&hv_cpu->msg_dpc);
2777 hv_debug_rm_all_dir();
2779 vmbus_free_channels();
2780 kfree(vmbus_connection.channels);
2782 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
2783 kmsg_dump_unregister(&hv_kmsg_dumper);
2784 unregister_die_notifier(&hyperv_die_block);
2788 * The panic notifier is always registered, hence we should
2789 * also unconditionally unregister it here as well.
2791 atomic_notifier_chain_unregister(&panic_notifier_list,
2792 &hyperv_panic_block);
2794 free_page((unsigned long)hv_panic_page);
2795 unregister_sysctl_table(hv_ctl_table_hdr);
2796 hv_ctl_table_hdr = NULL;
2797 bus_unregister(&hv_bus);
2799 cpuhp_remove_state(hyperv_cpuhp_online);
2801 acpi_bus_unregister_driver(&vmbus_acpi_driver);
2805 MODULE_LICENSE("GPL");
2806 MODULE_DESCRIPTION("Microsoft Hyper-V VMBus Driver");
2808 subsys_initcall(hv_acpi_init);
2809 module_exit(vmbus_exit);