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 <clocksource/hyperv_timer.h>
37 #include "hyperv_vmbus.h"
40 struct list_head node;
41 struct hv_vmbus_device_id id;
44 static struct acpi_device *hv_acpi_dev;
46 static struct completion probe_event;
48 static int hyperv_cpuhp_online;
50 static void *hv_panic_page;
52 static long __percpu *vmbus_evt;
54 /* Values parsed from ACPI DSDT */
59 * Boolean to control whether to report panic messages over Hyper-V.
61 * It can be set via /proc/sys/kernel/hyperv_record_panic_msg
63 static int sysctl_record_panic_msg = 1;
65 static int hyperv_report_reg(void)
67 return !sysctl_record_panic_msg || !hv_panic_page;
70 static int hyperv_panic_event(struct notifier_block *nb, unsigned long val,
75 vmbus_initiate_unload(true);
78 * Hyper-V should be notified only once about a panic. If we will be
79 * doing hv_kmsg_dump() with kmsg data later, don't do the notification
82 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE
83 && hyperv_report_reg()) {
84 regs = current_pt_regs();
85 hyperv_report_panic(regs, val, false);
90 static int hyperv_die_event(struct notifier_block *nb, unsigned long val,
93 struct die_args *die = args;
94 struct pt_regs *regs = die->regs;
96 /* Don't notify Hyper-V if the die event is other than oops */
101 * Hyper-V should be notified only once about a panic. If we will be
102 * doing hv_kmsg_dump() with kmsg data later, don't do the notification
105 if (hyperv_report_reg())
106 hyperv_report_panic(regs, val, true);
110 static struct notifier_block hyperv_die_block = {
111 .notifier_call = hyperv_die_event,
113 static struct notifier_block hyperv_panic_block = {
114 .notifier_call = hyperv_panic_event,
117 static const char *fb_mmio_name = "fb_range";
118 static struct resource *fb_mmio;
119 static struct resource *hyperv_mmio;
120 static DEFINE_MUTEX(hyperv_mmio_lock);
122 static int vmbus_exists(void)
124 if (hv_acpi_dev == NULL)
130 static u8 channel_monitor_group(const struct vmbus_channel *channel)
132 return (u8)channel->offermsg.monitorid / 32;
135 static u8 channel_monitor_offset(const struct vmbus_channel *channel)
137 return (u8)channel->offermsg.monitorid % 32;
140 static u32 channel_pending(const struct vmbus_channel *channel,
141 const struct hv_monitor_page *monitor_page)
143 u8 monitor_group = channel_monitor_group(channel);
145 return monitor_page->trigger_group[monitor_group].pending;
148 static u32 channel_latency(const struct vmbus_channel *channel,
149 const struct hv_monitor_page *monitor_page)
151 u8 monitor_group = channel_monitor_group(channel);
152 u8 monitor_offset = channel_monitor_offset(channel);
154 return monitor_page->latency[monitor_group][monitor_offset];
157 static u32 channel_conn_id(struct vmbus_channel *channel,
158 struct hv_monitor_page *monitor_page)
160 u8 monitor_group = channel_monitor_group(channel);
161 u8 monitor_offset = channel_monitor_offset(channel);
163 return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
166 static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
169 struct hv_device *hv_dev = device_to_hv_device(dev);
171 if (!hv_dev->channel)
173 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
175 static DEVICE_ATTR_RO(id);
177 static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
180 struct hv_device *hv_dev = device_to_hv_device(dev);
182 if (!hv_dev->channel)
184 return sprintf(buf, "%d\n", hv_dev->channel->state);
186 static DEVICE_ATTR_RO(state);
188 static ssize_t monitor_id_show(struct device *dev,
189 struct device_attribute *dev_attr, char *buf)
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.monitorid);
197 static DEVICE_ATTR_RO(monitor_id);
199 static ssize_t class_id_show(struct device *dev,
200 struct device_attribute *dev_attr, char *buf)
202 struct hv_device *hv_dev = device_to_hv_device(dev);
204 if (!hv_dev->channel)
206 return sprintf(buf, "{%pUl}\n",
207 &hv_dev->channel->offermsg.offer.if_type);
209 static DEVICE_ATTR_RO(class_id);
211 static ssize_t device_id_show(struct device *dev,
212 struct device_attribute *dev_attr, char *buf)
214 struct hv_device *hv_dev = device_to_hv_device(dev);
216 if (!hv_dev->channel)
218 return sprintf(buf, "{%pUl}\n",
219 &hv_dev->channel->offermsg.offer.if_instance);
221 static DEVICE_ATTR_RO(device_id);
223 static ssize_t modalias_show(struct device *dev,
224 struct device_attribute *dev_attr, char *buf)
226 struct hv_device *hv_dev = device_to_hv_device(dev);
228 return sprintf(buf, "vmbus:%*phN\n", UUID_SIZE, &hv_dev->dev_type);
230 static DEVICE_ATTR_RO(modalias);
233 static ssize_t numa_node_show(struct device *dev,
234 struct device_attribute *attr, char *buf)
236 struct hv_device *hv_dev = device_to_hv_device(dev);
238 if (!hv_dev->channel)
241 return sprintf(buf, "%d\n", cpu_to_node(hv_dev->channel->target_cpu));
243 static DEVICE_ATTR_RO(numa_node);
246 static ssize_t server_monitor_pending_show(struct device *dev,
247 struct device_attribute *dev_attr,
250 struct hv_device *hv_dev = device_to_hv_device(dev);
252 if (!hv_dev->channel)
254 return sprintf(buf, "%d\n",
255 channel_pending(hv_dev->channel,
256 vmbus_connection.monitor_pages[0]));
258 static DEVICE_ATTR_RO(server_monitor_pending);
260 static ssize_t client_monitor_pending_show(struct device *dev,
261 struct device_attribute *dev_attr,
264 struct hv_device *hv_dev = device_to_hv_device(dev);
266 if (!hv_dev->channel)
268 return sprintf(buf, "%d\n",
269 channel_pending(hv_dev->channel,
270 vmbus_connection.monitor_pages[1]));
272 static DEVICE_ATTR_RO(client_monitor_pending);
274 static ssize_t server_monitor_latency_show(struct device *dev,
275 struct device_attribute *dev_attr,
278 struct hv_device *hv_dev = device_to_hv_device(dev);
280 if (!hv_dev->channel)
282 return sprintf(buf, "%d\n",
283 channel_latency(hv_dev->channel,
284 vmbus_connection.monitor_pages[0]));
286 static DEVICE_ATTR_RO(server_monitor_latency);
288 static ssize_t client_monitor_latency_show(struct device *dev,
289 struct device_attribute *dev_attr,
292 struct hv_device *hv_dev = device_to_hv_device(dev);
294 if (!hv_dev->channel)
296 return sprintf(buf, "%d\n",
297 channel_latency(hv_dev->channel,
298 vmbus_connection.monitor_pages[1]));
300 static DEVICE_ATTR_RO(client_monitor_latency);
302 static ssize_t server_monitor_conn_id_show(struct device *dev,
303 struct device_attribute *dev_attr,
306 struct hv_device *hv_dev = device_to_hv_device(dev);
308 if (!hv_dev->channel)
310 return sprintf(buf, "%d\n",
311 channel_conn_id(hv_dev->channel,
312 vmbus_connection.monitor_pages[0]));
314 static DEVICE_ATTR_RO(server_monitor_conn_id);
316 static ssize_t client_monitor_conn_id_show(struct device *dev,
317 struct device_attribute *dev_attr,
320 struct hv_device *hv_dev = device_to_hv_device(dev);
322 if (!hv_dev->channel)
324 return sprintf(buf, "%d\n",
325 channel_conn_id(hv_dev->channel,
326 vmbus_connection.monitor_pages[1]));
328 static DEVICE_ATTR_RO(client_monitor_conn_id);
330 static ssize_t out_intr_mask_show(struct device *dev,
331 struct device_attribute *dev_attr, char *buf)
333 struct hv_device *hv_dev = device_to_hv_device(dev);
334 struct hv_ring_buffer_debug_info outbound;
337 if (!hv_dev->channel)
340 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
345 return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
347 static DEVICE_ATTR_RO(out_intr_mask);
349 static ssize_t out_read_index_show(struct device *dev,
350 struct device_attribute *dev_attr, char *buf)
352 struct hv_device *hv_dev = device_to_hv_device(dev);
353 struct hv_ring_buffer_debug_info outbound;
356 if (!hv_dev->channel)
359 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
363 return sprintf(buf, "%d\n", outbound.current_read_index);
365 static DEVICE_ATTR_RO(out_read_index);
367 static ssize_t out_write_index_show(struct device *dev,
368 struct device_attribute *dev_attr,
371 struct hv_device *hv_dev = device_to_hv_device(dev);
372 struct hv_ring_buffer_debug_info outbound;
375 if (!hv_dev->channel)
378 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
382 return sprintf(buf, "%d\n", outbound.current_write_index);
384 static DEVICE_ATTR_RO(out_write_index);
386 static ssize_t out_read_bytes_avail_show(struct device *dev,
387 struct device_attribute *dev_attr,
390 struct hv_device *hv_dev = device_to_hv_device(dev);
391 struct hv_ring_buffer_debug_info outbound;
394 if (!hv_dev->channel)
397 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
401 return sprintf(buf, "%d\n", outbound.bytes_avail_toread);
403 static DEVICE_ATTR_RO(out_read_bytes_avail);
405 static ssize_t out_write_bytes_avail_show(struct device *dev,
406 struct device_attribute *dev_attr,
409 struct hv_device *hv_dev = device_to_hv_device(dev);
410 struct hv_ring_buffer_debug_info outbound;
413 if (!hv_dev->channel)
416 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
420 return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
422 static DEVICE_ATTR_RO(out_write_bytes_avail);
424 static ssize_t in_intr_mask_show(struct device *dev,
425 struct device_attribute *dev_attr, char *buf)
427 struct hv_device *hv_dev = device_to_hv_device(dev);
428 struct hv_ring_buffer_debug_info inbound;
431 if (!hv_dev->channel)
434 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
438 return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
440 static DEVICE_ATTR_RO(in_intr_mask);
442 static ssize_t in_read_index_show(struct device *dev,
443 struct device_attribute *dev_attr, char *buf)
445 struct hv_device *hv_dev = device_to_hv_device(dev);
446 struct hv_ring_buffer_debug_info inbound;
449 if (!hv_dev->channel)
452 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
456 return sprintf(buf, "%d\n", inbound.current_read_index);
458 static DEVICE_ATTR_RO(in_read_index);
460 static ssize_t in_write_index_show(struct device *dev,
461 struct device_attribute *dev_attr, char *buf)
463 struct hv_device *hv_dev = device_to_hv_device(dev);
464 struct hv_ring_buffer_debug_info inbound;
467 if (!hv_dev->channel)
470 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
474 return sprintf(buf, "%d\n", inbound.current_write_index);
476 static DEVICE_ATTR_RO(in_write_index);
478 static ssize_t in_read_bytes_avail_show(struct device *dev,
479 struct device_attribute *dev_attr,
482 struct hv_device *hv_dev = device_to_hv_device(dev);
483 struct hv_ring_buffer_debug_info inbound;
486 if (!hv_dev->channel)
489 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
493 return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
495 static DEVICE_ATTR_RO(in_read_bytes_avail);
497 static ssize_t in_write_bytes_avail_show(struct device *dev,
498 struct device_attribute *dev_attr,
501 struct hv_device *hv_dev = device_to_hv_device(dev);
502 struct hv_ring_buffer_debug_info inbound;
505 if (!hv_dev->channel)
508 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
512 return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
514 static DEVICE_ATTR_RO(in_write_bytes_avail);
516 static ssize_t channel_vp_mapping_show(struct device *dev,
517 struct device_attribute *dev_attr,
520 struct hv_device *hv_dev = device_to_hv_device(dev);
521 struct vmbus_channel *channel = hv_dev->channel, *cur_sc;
522 int buf_size = PAGE_SIZE, n_written, tot_written;
523 struct list_head *cur;
528 mutex_lock(&vmbus_connection.channel_mutex);
530 tot_written = snprintf(buf, buf_size, "%u:%u\n",
531 channel->offermsg.child_relid, channel->target_cpu);
533 list_for_each(cur, &channel->sc_list) {
534 if (tot_written >= buf_size - 1)
537 cur_sc = list_entry(cur, struct vmbus_channel, sc_list);
538 n_written = scnprintf(buf + tot_written,
539 buf_size - tot_written,
541 cur_sc->offermsg.child_relid,
543 tot_written += n_written;
546 mutex_unlock(&vmbus_connection.channel_mutex);
550 static DEVICE_ATTR_RO(channel_vp_mapping);
552 static ssize_t vendor_show(struct device *dev,
553 struct device_attribute *dev_attr,
556 struct hv_device *hv_dev = device_to_hv_device(dev);
558 return sprintf(buf, "0x%x\n", hv_dev->vendor_id);
560 static DEVICE_ATTR_RO(vendor);
562 static ssize_t device_show(struct device *dev,
563 struct device_attribute *dev_attr,
566 struct hv_device *hv_dev = device_to_hv_device(dev);
568 return sprintf(buf, "0x%x\n", hv_dev->device_id);
570 static DEVICE_ATTR_RO(device);
572 static ssize_t driver_override_store(struct device *dev,
573 struct device_attribute *attr,
574 const char *buf, size_t count)
576 struct hv_device *hv_dev = device_to_hv_device(dev);
577 char *driver_override, *old, *cp;
579 /* We need to keep extra room for a newline */
580 if (count >= (PAGE_SIZE - 1))
583 driver_override = kstrndup(buf, count, GFP_KERNEL);
584 if (!driver_override)
587 cp = strchr(driver_override, '\n');
592 old = hv_dev->driver_override;
593 if (strlen(driver_override)) {
594 hv_dev->driver_override = driver_override;
596 kfree(driver_override);
597 hv_dev->driver_override = NULL;
606 static ssize_t driver_override_show(struct device *dev,
607 struct device_attribute *attr, char *buf)
609 struct hv_device *hv_dev = device_to_hv_device(dev);
613 len = snprintf(buf, PAGE_SIZE, "%s\n", hv_dev->driver_override);
618 static DEVICE_ATTR_RW(driver_override);
620 /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
621 static struct attribute *vmbus_dev_attrs[] = {
623 &dev_attr_state.attr,
624 &dev_attr_monitor_id.attr,
625 &dev_attr_class_id.attr,
626 &dev_attr_device_id.attr,
627 &dev_attr_modalias.attr,
629 &dev_attr_numa_node.attr,
631 &dev_attr_server_monitor_pending.attr,
632 &dev_attr_client_monitor_pending.attr,
633 &dev_attr_server_monitor_latency.attr,
634 &dev_attr_client_monitor_latency.attr,
635 &dev_attr_server_monitor_conn_id.attr,
636 &dev_attr_client_monitor_conn_id.attr,
637 &dev_attr_out_intr_mask.attr,
638 &dev_attr_out_read_index.attr,
639 &dev_attr_out_write_index.attr,
640 &dev_attr_out_read_bytes_avail.attr,
641 &dev_attr_out_write_bytes_avail.attr,
642 &dev_attr_in_intr_mask.attr,
643 &dev_attr_in_read_index.attr,
644 &dev_attr_in_write_index.attr,
645 &dev_attr_in_read_bytes_avail.attr,
646 &dev_attr_in_write_bytes_avail.attr,
647 &dev_attr_channel_vp_mapping.attr,
648 &dev_attr_vendor.attr,
649 &dev_attr_device.attr,
650 &dev_attr_driver_override.attr,
655 * Device-level attribute_group callback function. Returns the permission for
656 * each attribute, and returns 0 if an attribute is not visible.
658 static umode_t vmbus_dev_attr_is_visible(struct kobject *kobj,
659 struct attribute *attr, int idx)
661 struct device *dev = kobj_to_dev(kobj);
662 const struct hv_device *hv_dev = device_to_hv_device(dev);
664 /* Hide the monitor attributes if the monitor mechanism is not used. */
665 if (!hv_dev->channel->offermsg.monitor_allocated &&
666 (attr == &dev_attr_monitor_id.attr ||
667 attr == &dev_attr_server_monitor_pending.attr ||
668 attr == &dev_attr_client_monitor_pending.attr ||
669 attr == &dev_attr_server_monitor_latency.attr ||
670 attr == &dev_attr_client_monitor_latency.attr ||
671 attr == &dev_attr_server_monitor_conn_id.attr ||
672 attr == &dev_attr_client_monitor_conn_id.attr))
678 static const struct attribute_group vmbus_dev_group = {
679 .attrs = vmbus_dev_attrs,
680 .is_visible = vmbus_dev_attr_is_visible
682 __ATTRIBUTE_GROUPS(vmbus_dev);
684 /* Set up the attribute for /sys/bus/vmbus/hibernation */
685 static ssize_t hibernation_show(struct bus_type *bus, char *buf)
687 return sprintf(buf, "%d\n", !!hv_is_hibernation_supported());
690 static BUS_ATTR_RO(hibernation);
692 static struct attribute *vmbus_bus_attrs[] = {
693 &bus_attr_hibernation.attr,
696 static const struct attribute_group vmbus_bus_group = {
697 .attrs = vmbus_bus_attrs,
699 __ATTRIBUTE_GROUPS(vmbus_bus);
702 * vmbus_uevent - add uevent for our device
704 * This routine is invoked when a device is added or removed on the vmbus to
705 * generate a uevent to udev in the userspace. The udev will then look at its
706 * rule and the uevent generated here to load the appropriate driver
708 * The alias string will be of the form vmbus:guid where guid is the string
709 * representation of the device guid (each byte of the guid will be
710 * represented with two hex characters.
712 static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
714 struct hv_device *dev = device_to_hv_device(device);
715 const char *format = "MODALIAS=vmbus:%*phN";
717 return add_uevent_var(env, format, UUID_SIZE, &dev->dev_type);
720 static const struct hv_vmbus_device_id *
721 hv_vmbus_dev_match(const struct hv_vmbus_device_id *id, const guid_t *guid)
724 return NULL; /* empty device table */
726 for (; !guid_is_null(&id->guid); id++)
727 if (guid_equal(&id->guid, guid))
733 static const struct hv_vmbus_device_id *
734 hv_vmbus_dynid_match(struct hv_driver *drv, const guid_t *guid)
736 const struct hv_vmbus_device_id *id = NULL;
737 struct vmbus_dynid *dynid;
739 spin_lock(&drv->dynids.lock);
740 list_for_each_entry(dynid, &drv->dynids.list, node) {
741 if (guid_equal(&dynid->id.guid, guid)) {
746 spin_unlock(&drv->dynids.lock);
751 static const struct hv_vmbus_device_id vmbus_device_null;
754 * Return a matching hv_vmbus_device_id pointer.
755 * If there is no match, return NULL.
757 static const struct hv_vmbus_device_id *hv_vmbus_get_id(struct hv_driver *drv,
758 struct hv_device *dev)
760 const guid_t *guid = &dev->dev_type;
761 const struct hv_vmbus_device_id *id;
763 /* When driver_override is set, only bind to the matching driver */
764 if (dev->driver_override && strcmp(dev->driver_override, drv->name))
767 /* Look at the dynamic ids first, before the static ones */
768 id = hv_vmbus_dynid_match(drv, guid);
770 id = hv_vmbus_dev_match(drv->id_table, guid);
772 /* driver_override will always match, send a dummy id */
773 if (!id && dev->driver_override)
774 id = &vmbus_device_null;
779 /* vmbus_add_dynid - add a new device ID to this driver and re-probe devices */
780 static int vmbus_add_dynid(struct hv_driver *drv, guid_t *guid)
782 struct vmbus_dynid *dynid;
784 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
788 dynid->id.guid = *guid;
790 spin_lock(&drv->dynids.lock);
791 list_add_tail(&dynid->node, &drv->dynids.list);
792 spin_unlock(&drv->dynids.lock);
794 return driver_attach(&drv->driver);
797 static void vmbus_free_dynids(struct hv_driver *drv)
799 struct vmbus_dynid *dynid, *n;
801 spin_lock(&drv->dynids.lock);
802 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
803 list_del(&dynid->node);
806 spin_unlock(&drv->dynids.lock);
810 * store_new_id - sysfs frontend to vmbus_add_dynid()
812 * Allow GUIDs to be added to an existing driver via sysfs.
814 static ssize_t new_id_store(struct device_driver *driver, const char *buf,
817 struct hv_driver *drv = drv_to_hv_drv(driver);
821 retval = guid_parse(buf, &guid);
825 if (hv_vmbus_dynid_match(drv, &guid))
828 retval = vmbus_add_dynid(drv, &guid);
833 static DRIVER_ATTR_WO(new_id);
836 * store_remove_id - remove a PCI device ID from this driver
838 * Removes a dynamic pci device ID to this driver.
840 static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
843 struct hv_driver *drv = drv_to_hv_drv(driver);
844 struct vmbus_dynid *dynid, *n;
848 retval = guid_parse(buf, &guid);
853 spin_lock(&drv->dynids.lock);
854 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
855 struct hv_vmbus_device_id *id = &dynid->id;
857 if (guid_equal(&id->guid, &guid)) {
858 list_del(&dynid->node);
864 spin_unlock(&drv->dynids.lock);
868 static DRIVER_ATTR_WO(remove_id);
870 static struct attribute *vmbus_drv_attrs[] = {
871 &driver_attr_new_id.attr,
872 &driver_attr_remove_id.attr,
875 ATTRIBUTE_GROUPS(vmbus_drv);
879 * vmbus_match - Attempt to match the specified device to the specified driver
881 static int vmbus_match(struct device *device, struct device_driver *driver)
883 struct hv_driver *drv = drv_to_hv_drv(driver);
884 struct hv_device *hv_dev = device_to_hv_device(device);
886 /* The hv_sock driver handles all hv_sock offers. */
887 if (is_hvsock_channel(hv_dev->channel))
890 if (hv_vmbus_get_id(drv, hv_dev))
897 * vmbus_probe - Add the new vmbus's child device
899 static int vmbus_probe(struct device *child_device)
902 struct hv_driver *drv =
903 drv_to_hv_drv(child_device->driver);
904 struct hv_device *dev = device_to_hv_device(child_device);
905 const struct hv_vmbus_device_id *dev_id;
907 dev_id = hv_vmbus_get_id(drv, dev);
909 ret = drv->probe(dev, dev_id);
911 pr_err("probe failed for device %s (%d)\n",
912 dev_name(child_device), ret);
915 pr_err("probe not set for driver %s\n",
916 dev_name(child_device));
923 * vmbus_remove - Remove a vmbus device
925 static void vmbus_remove(struct device *child_device)
927 struct hv_driver *drv;
928 struct hv_device *dev = device_to_hv_device(child_device);
930 if (child_device->driver) {
931 drv = drv_to_hv_drv(child_device->driver);
938 * vmbus_shutdown - Shutdown a vmbus device
940 static void vmbus_shutdown(struct device *child_device)
942 struct hv_driver *drv;
943 struct hv_device *dev = device_to_hv_device(child_device);
946 /* The device may not be attached yet */
947 if (!child_device->driver)
950 drv = drv_to_hv_drv(child_device->driver);
956 #ifdef CONFIG_PM_SLEEP
958 * vmbus_suspend - Suspend a vmbus device
960 static int vmbus_suspend(struct device *child_device)
962 struct hv_driver *drv;
963 struct hv_device *dev = device_to_hv_device(child_device);
965 /* The device may not be attached yet */
966 if (!child_device->driver)
969 drv = drv_to_hv_drv(child_device->driver);
973 return drv->suspend(dev);
977 * vmbus_resume - Resume a vmbus device
979 static int vmbus_resume(struct device *child_device)
981 struct hv_driver *drv;
982 struct hv_device *dev = device_to_hv_device(child_device);
984 /* The device may not be attached yet */
985 if (!child_device->driver)
988 drv = drv_to_hv_drv(child_device->driver);
992 return drv->resume(dev);
995 #define vmbus_suspend NULL
996 #define vmbus_resume NULL
997 #endif /* CONFIG_PM_SLEEP */
1000 * vmbus_device_release - Final callback release of the vmbus child device
1002 static void vmbus_device_release(struct device *device)
1004 struct hv_device *hv_dev = device_to_hv_device(device);
1005 struct vmbus_channel *channel = hv_dev->channel;
1007 hv_debug_rm_dev_dir(hv_dev);
1009 mutex_lock(&vmbus_connection.channel_mutex);
1010 hv_process_channel_removal(channel);
1011 mutex_unlock(&vmbus_connection.channel_mutex);
1016 * Note: we must use the "noirq" ops: see the comment before vmbus_bus_pm.
1018 * suspend_noirq/resume_noirq are set to NULL to support Suspend-to-Idle: we
1019 * shouldn't suspend the vmbus devices upon Suspend-to-Idle, otherwise there
1020 * is no way to wake up a Generation-2 VM.
1022 * The other 4 ops are for hibernation.
1025 static const struct dev_pm_ops vmbus_pm = {
1026 .suspend_noirq = NULL,
1027 .resume_noirq = NULL,
1028 .freeze_noirq = vmbus_suspend,
1029 .thaw_noirq = vmbus_resume,
1030 .poweroff_noirq = vmbus_suspend,
1031 .restore_noirq = vmbus_resume,
1034 /* The one and only one */
1035 static struct bus_type hv_bus = {
1037 .match = vmbus_match,
1038 .shutdown = vmbus_shutdown,
1039 .remove = vmbus_remove,
1040 .probe = vmbus_probe,
1041 .uevent = vmbus_uevent,
1042 .dev_groups = vmbus_dev_groups,
1043 .drv_groups = vmbus_drv_groups,
1044 .bus_groups = vmbus_bus_groups,
1048 struct onmessage_work_context {
1049 struct work_struct work;
1051 struct hv_message_header header;
1056 static void vmbus_onmessage_work(struct work_struct *work)
1058 struct onmessage_work_context *ctx;
1060 /* Do not process messages if we're in DISCONNECTED state */
1061 if (vmbus_connection.conn_state == DISCONNECTED)
1064 ctx = container_of(work, struct onmessage_work_context,
1066 vmbus_onmessage((struct vmbus_channel_message_header *)
1071 void vmbus_on_msg_dpc(unsigned long data)
1073 struct hv_per_cpu_context *hv_cpu = (void *)data;
1074 void *page_addr = hv_cpu->synic_message_page;
1075 struct hv_message msg_copy, *msg = (struct hv_message *)page_addr +
1077 struct vmbus_channel_message_header *hdr;
1078 enum vmbus_channel_message_type msgtype;
1079 const struct vmbus_channel_message_table_entry *entry;
1080 struct onmessage_work_context *ctx;
1085 * 'enum vmbus_channel_message_type' is supposed to always be 'u32' as
1086 * it is being used in 'struct vmbus_channel_message_header' definition
1087 * which is supposed to match hypervisor ABI.
1089 BUILD_BUG_ON(sizeof(enum vmbus_channel_message_type) != sizeof(u32));
1092 * Since the message is in memory shared with the host, an erroneous or
1093 * malicious Hyper-V could modify the message while vmbus_on_msg_dpc()
1094 * or individual message handlers are executing; to prevent this, copy
1095 * the message into private memory.
1097 memcpy(&msg_copy, msg, sizeof(struct hv_message));
1099 message_type = msg_copy.header.message_type;
1100 if (message_type == HVMSG_NONE)
1104 hdr = (struct vmbus_channel_message_header *)msg_copy.u.payload;
1105 msgtype = hdr->msgtype;
1107 trace_vmbus_on_msg_dpc(hdr);
1109 if (msgtype >= CHANNELMSG_COUNT) {
1110 WARN_ONCE(1, "unknown msgtype=%d\n", msgtype);
1114 payload_size = msg_copy.header.payload_size;
1115 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT) {
1116 WARN_ONCE(1, "payload size is too large (%d)\n", payload_size);
1120 entry = &channel_message_table[msgtype];
1122 if (!entry->message_handler)
1125 if (payload_size < entry->min_payload_len) {
1126 WARN_ONCE(1, "message too short: msgtype=%d len=%d\n", msgtype, payload_size);
1130 if (entry->handler_type == VMHT_BLOCKING) {
1131 ctx = kmalloc(sizeof(*ctx) + payload_size, GFP_ATOMIC);
1135 INIT_WORK(&ctx->work, vmbus_onmessage_work);
1136 memcpy(&ctx->msg, &msg_copy, sizeof(msg->header) + payload_size);
1139 * The host can generate a rescind message while we
1140 * may still be handling the original offer. We deal with
1141 * this condition by relying on the synchronization provided
1142 * by offer_in_progress and by channel_mutex. See also the
1143 * inline comments in vmbus_onoffer_rescind().
1146 case CHANNELMSG_RESCIND_CHANNELOFFER:
1148 * If we are handling the rescind message;
1149 * schedule the work on the global work queue.
1151 * The OFFER message and the RESCIND message should
1152 * not be handled by the same serialized work queue,
1153 * because the OFFER handler may call vmbus_open(),
1154 * which tries to open the channel by sending an
1155 * OPEN_CHANNEL message to the host and waits for
1156 * the host's response; however, if the host has
1157 * rescinded the channel before it receives the
1158 * OPEN_CHANNEL message, the host just silently
1159 * ignores the OPEN_CHANNEL message; as a result,
1160 * the guest's OFFER handler hangs for ever, if we
1161 * handle the RESCIND message in the same serialized
1162 * work queue: the RESCIND handler can not start to
1163 * run before the OFFER handler finishes.
1165 schedule_work(&ctx->work);
1168 case CHANNELMSG_OFFERCHANNEL:
1170 * The host sends the offer message of a given channel
1171 * before sending the rescind message of the same
1172 * channel. These messages are sent to the guest's
1173 * connect CPU; the guest then starts processing them
1174 * in the tasklet handler on this CPU:
1178 * [vmbus_on_msg_dpc()]
1179 * atomic_inc() // CHANNELMSG_OFFERCHANNEL
1182 * [vmbus_on_msg_dpc()]
1183 * schedule_work() // CHANNELMSG_RESCIND_CHANNELOFFER
1185 * We rely on the memory-ordering properties of the
1186 * queue_work() and schedule_work() primitives, which
1187 * guarantee that the atomic increment will be visible
1188 * to the CPUs which will execute the offer & rescind
1189 * works by the time these works will start execution.
1191 atomic_inc(&vmbus_connection.offer_in_progress);
1195 queue_work(vmbus_connection.work_queue, &ctx->work);
1198 entry->message_handler(hdr);
1201 vmbus_signal_eom(msg, message_type);
1204 #ifdef CONFIG_PM_SLEEP
1206 * Fake RESCIND_CHANNEL messages to clean up hv_sock channels by force for
1207 * hibernation, because hv_sock connections can not persist across hibernation.
1209 static void vmbus_force_channel_rescinded(struct vmbus_channel *channel)
1211 struct onmessage_work_context *ctx;
1212 struct vmbus_channel_rescind_offer *rescind;
1214 WARN_ON(!is_hvsock_channel(channel));
1217 * Allocation size is small and the allocation should really not fail,
1218 * otherwise the state of the hv_sock connections ends up in limbo.
1220 ctx = kzalloc(sizeof(*ctx) + sizeof(*rescind),
1221 GFP_KERNEL | __GFP_NOFAIL);
1224 * So far, these are not really used by Linux. Just set them to the
1225 * reasonable values conforming to the definitions of the fields.
1227 ctx->msg.header.message_type = 1;
1228 ctx->msg.header.payload_size = sizeof(*rescind);
1230 /* These values are actually used by Linux. */
1231 rescind = (struct vmbus_channel_rescind_offer *)ctx->msg.payload;
1232 rescind->header.msgtype = CHANNELMSG_RESCIND_CHANNELOFFER;
1233 rescind->child_relid = channel->offermsg.child_relid;
1235 INIT_WORK(&ctx->work, vmbus_onmessage_work);
1237 queue_work(vmbus_connection.work_queue, &ctx->work);
1239 #endif /* CONFIG_PM_SLEEP */
1242 * Schedule all channels with events pending
1244 static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu)
1246 unsigned long *recv_int_page;
1249 if (vmbus_proto_version < VERSION_WIN8) {
1250 maxbits = MAX_NUM_CHANNELS_SUPPORTED;
1251 recv_int_page = vmbus_connection.recv_int_page;
1254 * When the host is win8 and beyond, the event page
1255 * can be directly checked to get the id of the channel
1256 * that has the interrupt pending.
1258 void *page_addr = hv_cpu->synic_event_page;
1259 union hv_synic_event_flags *event
1260 = (union hv_synic_event_flags *)page_addr +
1263 maxbits = HV_EVENT_FLAGS_COUNT;
1264 recv_int_page = event->flags;
1267 if (unlikely(!recv_int_page))
1270 for_each_set_bit(relid, recv_int_page, maxbits) {
1271 void (*callback_fn)(void *context);
1272 struct vmbus_channel *channel;
1274 if (!sync_test_and_clear_bit(relid, recv_int_page))
1277 /* Special case - vmbus channel protocol msg */
1282 * Pairs with the kfree_rcu() in vmbus_chan_release().
1283 * Guarantees that the channel data structure doesn't
1284 * get freed while the channel pointer below is being
1289 /* Find channel based on relid */
1290 channel = relid2channel(relid);
1291 if (channel == NULL)
1292 goto sched_unlock_rcu;
1294 if (channel->rescind)
1295 goto sched_unlock_rcu;
1298 * Make sure that the ring buffer data structure doesn't get
1299 * freed while we dereference the ring buffer pointer. Test
1300 * for the channel's onchannel_callback being NULL within a
1301 * sched_lock critical section. See also the inline comments
1302 * in vmbus_reset_channel_cb().
1304 spin_lock(&channel->sched_lock);
1306 callback_fn = channel->onchannel_callback;
1307 if (unlikely(callback_fn == NULL))
1310 trace_vmbus_chan_sched(channel);
1312 ++channel->interrupts;
1314 switch (channel->callback_mode) {
1316 (*callback_fn)(channel->channel_callback_context);
1319 case HV_CALL_BATCHED:
1320 hv_begin_read(&channel->inbound);
1322 case HV_CALL_DIRECT:
1323 tasklet_schedule(&channel->callback_event);
1327 spin_unlock(&channel->sched_lock);
1333 static void vmbus_isr(void)
1335 struct hv_per_cpu_context *hv_cpu
1336 = this_cpu_ptr(hv_context.cpu_context);
1337 void *page_addr = hv_cpu->synic_event_page;
1338 struct hv_message *msg;
1339 union hv_synic_event_flags *event;
1340 bool handled = false;
1342 if (unlikely(page_addr == NULL))
1345 event = (union hv_synic_event_flags *)page_addr +
1348 * Check for events before checking for messages. This is the order
1349 * in which events and messages are checked in Windows guests on
1350 * Hyper-V, and the Windows team suggested we do the same.
1353 if ((vmbus_proto_version == VERSION_WS2008) ||
1354 (vmbus_proto_version == VERSION_WIN7)) {
1356 /* Since we are a child, we only need to check bit 0 */
1357 if (sync_test_and_clear_bit(0, event->flags))
1361 * Our host is win8 or above. The signaling mechanism
1362 * has changed and we can directly look at the event page.
1363 * If bit n is set then we have an interrup on the channel
1370 vmbus_chan_sched(hv_cpu);
1372 page_addr = hv_cpu->synic_message_page;
1373 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
1375 /* Check if there are actual msgs to be processed */
1376 if (msg->header.message_type != HVMSG_NONE) {
1377 if (msg->header.message_type == HVMSG_TIMER_EXPIRED) {
1379 vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED);
1381 tasklet_schedule(&hv_cpu->msg_dpc);
1384 add_interrupt_randomness(vmbus_interrupt);
1387 static irqreturn_t vmbus_percpu_isr(int irq, void *dev_id)
1394 * Callback from kmsg_dump. Grab as much as possible from the end of the kmsg
1395 * buffer and call into Hyper-V to transfer the data.
1397 static void hv_kmsg_dump(struct kmsg_dumper *dumper,
1398 enum kmsg_dump_reason reason)
1400 struct kmsg_dump_iter iter;
1401 size_t bytes_written;
1403 /* We are only interested in panics. */
1404 if ((reason != KMSG_DUMP_PANIC) || (!sysctl_record_panic_msg))
1408 * Write dump contents to the page. No need to synchronize; panic should
1409 * be single-threaded.
1411 kmsg_dump_rewind(&iter);
1412 kmsg_dump_get_buffer(&iter, false, hv_panic_page, HV_HYP_PAGE_SIZE,
1417 * P3 to contain the physical address of the panic page & P4 to
1418 * contain the size of the panic data in that page. Rest of the
1419 * registers are no-op when the NOTIFY_MSG flag is set.
1421 hv_set_register(HV_REGISTER_CRASH_P0, 0);
1422 hv_set_register(HV_REGISTER_CRASH_P1, 0);
1423 hv_set_register(HV_REGISTER_CRASH_P2, 0);
1424 hv_set_register(HV_REGISTER_CRASH_P3, virt_to_phys(hv_panic_page));
1425 hv_set_register(HV_REGISTER_CRASH_P4, bytes_written);
1428 * Let Hyper-V know there is crash data available along with
1429 * the panic message.
1431 hv_set_register(HV_REGISTER_CRASH_CTL,
1432 (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG));
1435 static struct kmsg_dumper hv_kmsg_dumper = {
1436 .dump = hv_kmsg_dump,
1439 static void hv_kmsg_dump_register(void)
1443 hv_panic_page = hv_alloc_hyperv_zeroed_page();
1444 if (!hv_panic_page) {
1445 pr_err("Hyper-V: panic message page memory allocation failed\n");
1449 ret = kmsg_dump_register(&hv_kmsg_dumper);
1451 pr_err("Hyper-V: kmsg dump register error 0x%x\n", ret);
1452 hv_free_hyperv_page((unsigned long)hv_panic_page);
1453 hv_panic_page = NULL;
1457 static struct ctl_table_header *hv_ctl_table_hdr;
1460 * sysctl option to allow the user to control whether kmsg data should be
1461 * reported to Hyper-V on panic.
1463 static struct ctl_table hv_ctl_table[] = {
1465 .procname = "hyperv_record_panic_msg",
1466 .data = &sysctl_record_panic_msg,
1467 .maxlen = sizeof(int),
1469 .proc_handler = proc_dointvec_minmax,
1470 .extra1 = SYSCTL_ZERO,
1471 .extra2 = SYSCTL_ONE
1476 static struct ctl_table hv_root_table[] = {
1478 .procname = "kernel",
1480 .child = hv_ctl_table
1486 * vmbus_bus_init -Main vmbus driver initialization routine.
1489 * - initialize the vmbus driver context
1490 * - invoke the vmbus hv main init routine
1491 * - retrieve the channel offers
1493 static int vmbus_bus_init(void)
1499 pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
1503 ret = bus_register(&hv_bus);
1508 * VMbus interrupts are best modeled as per-cpu interrupts. If
1509 * on an architecture with support for per-cpu IRQs (e.g. ARM64),
1510 * allocate a per-cpu IRQ using standard Linux kernel functionality.
1511 * If not on such an architecture (e.g., x86/x64), then rely on
1512 * code in the arch-specific portion of the code tree to connect
1513 * the VMbus interrupt handler.
1516 if (vmbus_irq == -1) {
1517 hv_setup_vmbus_handler(vmbus_isr);
1519 vmbus_evt = alloc_percpu(long);
1520 ret = request_percpu_irq(vmbus_irq, vmbus_percpu_isr,
1521 "Hyper-V VMbus", vmbus_evt);
1523 pr_err("Can't request Hyper-V VMbus IRQ %d, Err %d",
1525 free_percpu(vmbus_evt);
1530 ret = hv_synic_alloc();
1535 * Initialize the per-cpu interrupt state and stimer state.
1536 * Then connect to the host.
1538 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hyperv/vmbus:online",
1539 hv_synic_init, hv_synic_cleanup);
1542 hyperv_cpuhp_online = ret;
1544 ret = vmbus_connect();
1548 if (hv_is_isolation_supported())
1549 sysctl_record_panic_msg = 0;
1552 * Only register if the crash MSRs are available
1554 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
1555 u64 hyperv_crash_ctl;
1557 * Panic message recording (sysctl_record_panic_msg)
1558 * is enabled by default in non-isolated guests and
1559 * disabled by default in isolated guests; the panic
1560 * message recording won't be available in isolated
1561 * guests should the following registration fail.
1563 hv_ctl_table_hdr = register_sysctl_table(hv_root_table);
1564 if (!hv_ctl_table_hdr)
1565 pr_err("Hyper-V: sysctl table register error");
1568 * Register for panic kmsg callback only if the right
1569 * capability is supported by the hypervisor.
1571 hyperv_crash_ctl = hv_get_register(HV_REGISTER_CRASH_CTL);
1572 if (hyperv_crash_ctl & HV_CRASH_CTL_CRASH_NOTIFY_MSG)
1573 hv_kmsg_dump_register();
1575 register_die_notifier(&hyperv_die_block);
1579 * Always register the panic notifier because we need to unload
1580 * the VMbus channel connection to prevent any VMbus
1581 * activity after the VM panics.
1583 atomic_notifier_chain_register(&panic_notifier_list,
1584 &hyperv_panic_block);
1586 vmbus_request_offers();
1591 cpuhp_remove_state(hyperv_cpuhp_online);
1595 if (vmbus_irq == -1) {
1596 hv_remove_vmbus_handler();
1598 free_percpu_irq(vmbus_irq, vmbus_evt);
1599 free_percpu(vmbus_evt);
1602 bus_unregister(&hv_bus);
1603 unregister_sysctl_table(hv_ctl_table_hdr);
1604 hv_ctl_table_hdr = NULL;
1609 * __vmbus_child_driver_register() - Register a vmbus's driver
1610 * @hv_driver: Pointer to driver structure you want to register
1611 * @owner: owner module of the drv
1612 * @mod_name: module name string
1614 * Registers the given driver with Linux through the 'driver_register()' call
1615 * and sets up the hyper-v vmbus handling for this driver.
1616 * It will return the state of the 'driver_register()' call.
1619 int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
1623 pr_info("registering driver %s\n", hv_driver->name);
1625 ret = vmbus_exists();
1629 hv_driver->driver.name = hv_driver->name;
1630 hv_driver->driver.owner = owner;
1631 hv_driver->driver.mod_name = mod_name;
1632 hv_driver->driver.bus = &hv_bus;
1634 spin_lock_init(&hv_driver->dynids.lock);
1635 INIT_LIST_HEAD(&hv_driver->dynids.list);
1637 ret = driver_register(&hv_driver->driver);
1641 EXPORT_SYMBOL_GPL(__vmbus_driver_register);
1644 * vmbus_driver_unregister() - Unregister a vmbus's driver
1645 * @hv_driver: Pointer to driver structure you want to
1648 * Un-register the given driver that was previous registered with a call to
1649 * vmbus_driver_register()
1651 void vmbus_driver_unregister(struct hv_driver *hv_driver)
1653 pr_info("unregistering driver %s\n", hv_driver->name);
1655 if (!vmbus_exists()) {
1656 driver_unregister(&hv_driver->driver);
1657 vmbus_free_dynids(hv_driver);
1660 EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
1664 * Called when last reference to channel is gone.
1666 static void vmbus_chan_release(struct kobject *kobj)
1668 struct vmbus_channel *channel
1669 = container_of(kobj, struct vmbus_channel, kobj);
1671 kfree_rcu(channel, rcu);
1674 struct vmbus_chan_attribute {
1675 struct attribute attr;
1676 ssize_t (*show)(struct vmbus_channel *chan, char *buf);
1677 ssize_t (*store)(struct vmbus_channel *chan,
1678 const char *buf, size_t count);
1680 #define VMBUS_CHAN_ATTR(_name, _mode, _show, _store) \
1681 struct vmbus_chan_attribute chan_attr_##_name \
1682 = __ATTR(_name, _mode, _show, _store)
1683 #define VMBUS_CHAN_ATTR_RW(_name) \
1684 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RW(_name)
1685 #define VMBUS_CHAN_ATTR_RO(_name) \
1686 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RO(_name)
1687 #define VMBUS_CHAN_ATTR_WO(_name) \
1688 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_WO(_name)
1690 static ssize_t vmbus_chan_attr_show(struct kobject *kobj,
1691 struct attribute *attr, char *buf)
1693 const struct vmbus_chan_attribute *attribute
1694 = container_of(attr, struct vmbus_chan_attribute, attr);
1695 struct vmbus_channel *chan
1696 = container_of(kobj, struct vmbus_channel, kobj);
1698 if (!attribute->show)
1701 return attribute->show(chan, buf);
1704 static ssize_t vmbus_chan_attr_store(struct kobject *kobj,
1705 struct attribute *attr, const char *buf,
1708 const struct vmbus_chan_attribute *attribute
1709 = container_of(attr, struct vmbus_chan_attribute, attr);
1710 struct vmbus_channel *chan
1711 = container_of(kobj, struct vmbus_channel, kobj);
1713 if (!attribute->store)
1716 return attribute->store(chan, buf, count);
1719 static const struct sysfs_ops vmbus_chan_sysfs_ops = {
1720 .show = vmbus_chan_attr_show,
1721 .store = vmbus_chan_attr_store,
1724 static ssize_t out_mask_show(struct vmbus_channel *channel, char *buf)
1726 struct hv_ring_buffer_info *rbi = &channel->outbound;
1729 mutex_lock(&rbi->ring_buffer_mutex);
1730 if (!rbi->ring_buffer) {
1731 mutex_unlock(&rbi->ring_buffer_mutex);
1735 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1736 mutex_unlock(&rbi->ring_buffer_mutex);
1739 static VMBUS_CHAN_ATTR_RO(out_mask);
1741 static ssize_t in_mask_show(struct vmbus_channel *channel, char *buf)
1743 struct hv_ring_buffer_info *rbi = &channel->inbound;
1746 mutex_lock(&rbi->ring_buffer_mutex);
1747 if (!rbi->ring_buffer) {
1748 mutex_unlock(&rbi->ring_buffer_mutex);
1752 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1753 mutex_unlock(&rbi->ring_buffer_mutex);
1756 static VMBUS_CHAN_ATTR_RO(in_mask);
1758 static ssize_t read_avail_show(struct vmbus_channel *channel, char *buf)
1760 struct hv_ring_buffer_info *rbi = &channel->inbound;
1763 mutex_lock(&rbi->ring_buffer_mutex);
1764 if (!rbi->ring_buffer) {
1765 mutex_unlock(&rbi->ring_buffer_mutex);
1769 ret = sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi));
1770 mutex_unlock(&rbi->ring_buffer_mutex);
1773 static VMBUS_CHAN_ATTR_RO(read_avail);
1775 static ssize_t write_avail_show(struct vmbus_channel *channel, char *buf)
1777 struct hv_ring_buffer_info *rbi = &channel->outbound;
1780 mutex_lock(&rbi->ring_buffer_mutex);
1781 if (!rbi->ring_buffer) {
1782 mutex_unlock(&rbi->ring_buffer_mutex);
1786 ret = sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi));
1787 mutex_unlock(&rbi->ring_buffer_mutex);
1790 static VMBUS_CHAN_ATTR_RO(write_avail);
1792 static ssize_t target_cpu_show(struct vmbus_channel *channel, char *buf)
1794 return sprintf(buf, "%u\n", channel->target_cpu);
1796 static ssize_t target_cpu_store(struct vmbus_channel *channel,
1797 const char *buf, size_t count)
1799 u32 target_cpu, origin_cpu;
1800 ssize_t ret = count;
1802 if (vmbus_proto_version < VERSION_WIN10_V4_1)
1805 if (sscanf(buf, "%uu", &target_cpu) != 1)
1808 /* Validate target_cpu for the cpumask_test_cpu() operation below. */
1809 if (target_cpu >= nr_cpumask_bits)
1812 /* No CPUs should come up or down during this. */
1815 if (!cpu_online(target_cpu)) {
1821 * Synchronizes target_cpu_store() and channel closure:
1823 * { Initially: state = CHANNEL_OPENED }
1827 * [target_cpu_store()] [vmbus_disconnect_ring()]
1829 * LOCK channel_mutex LOCK channel_mutex
1830 * LOAD r1 = state LOAD r2 = state
1831 * IF (r1 == CHANNEL_OPENED) IF (r2 == CHANNEL_OPENED)
1832 * SEND MODIFYCHANNEL STORE state = CHANNEL_OPEN
1833 * [...] SEND CLOSECHANNEL
1834 * UNLOCK channel_mutex UNLOCK channel_mutex
1836 * Forbids: r1 == r2 == CHANNEL_OPENED (i.e., CPU1's LOCK precedes
1837 * CPU2's LOCK) && CPU2's SEND precedes CPU1's SEND
1839 * Note. The host processes the channel messages "sequentially", in
1840 * the order in which they are received on a per-partition basis.
1842 mutex_lock(&vmbus_connection.channel_mutex);
1845 * Hyper-V will ignore MODIFYCHANNEL messages for "non-open" channels;
1846 * avoid sending the message and fail here for such channels.
1848 if (channel->state != CHANNEL_OPENED_STATE) {
1850 goto cpu_store_unlock;
1853 origin_cpu = channel->target_cpu;
1854 if (target_cpu == origin_cpu)
1855 goto cpu_store_unlock;
1857 if (vmbus_send_modifychannel(channel,
1858 hv_cpu_number_to_vp_number(target_cpu))) {
1860 goto cpu_store_unlock;
1864 * For version before VERSION_WIN10_V5_3, the following warning holds:
1866 * Warning. At this point, there is *no* guarantee that the host will
1867 * have successfully processed the vmbus_send_modifychannel() request.
1868 * See the header comment of vmbus_send_modifychannel() for more info.
1870 * Lags in the processing of the above vmbus_send_modifychannel() can
1871 * result in missed interrupts if the "old" target CPU is taken offline
1872 * before Hyper-V starts sending interrupts to the "new" target CPU.
1873 * But apart from this offlining scenario, the code tolerates such
1874 * lags. It will function correctly even if a channel interrupt comes
1875 * in on a CPU that is different from the channel target_cpu value.
1878 channel->target_cpu = target_cpu;
1880 /* See init_vp_index(). */
1881 if (hv_is_perf_channel(channel))
1882 hv_update_alloced_cpus(origin_cpu, target_cpu);
1884 /* Currently set only for storvsc channels. */
1885 if (channel->change_target_cpu_callback) {
1886 (*channel->change_target_cpu_callback)(channel,
1887 origin_cpu, target_cpu);
1891 mutex_unlock(&vmbus_connection.channel_mutex);
1895 static VMBUS_CHAN_ATTR(cpu, 0644, target_cpu_show, target_cpu_store);
1897 static ssize_t channel_pending_show(struct vmbus_channel *channel,
1900 return sprintf(buf, "%d\n",
1901 channel_pending(channel,
1902 vmbus_connection.monitor_pages[1]));
1904 static VMBUS_CHAN_ATTR(pending, 0444, channel_pending_show, NULL);
1906 static ssize_t channel_latency_show(struct vmbus_channel *channel,
1909 return sprintf(buf, "%d\n",
1910 channel_latency(channel,
1911 vmbus_connection.monitor_pages[1]));
1913 static VMBUS_CHAN_ATTR(latency, 0444, channel_latency_show, NULL);
1915 static ssize_t channel_interrupts_show(struct vmbus_channel *channel, char *buf)
1917 return sprintf(buf, "%llu\n", channel->interrupts);
1919 static VMBUS_CHAN_ATTR(interrupts, 0444, channel_interrupts_show, NULL);
1921 static ssize_t channel_events_show(struct vmbus_channel *channel, char *buf)
1923 return sprintf(buf, "%llu\n", channel->sig_events);
1925 static VMBUS_CHAN_ATTR(events, 0444, channel_events_show, NULL);
1927 static ssize_t channel_intr_in_full_show(struct vmbus_channel *channel,
1930 return sprintf(buf, "%llu\n",
1931 (unsigned long long)channel->intr_in_full);
1933 static VMBUS_CHAN_ATTR(intr_in_full, 0444, channel_intr_in_full_show, NULL);
1935 static ssize_t channel_intr_out_empty_show(struct vmbus_channel *channel,
1938 return sprintf(buf, "%llu\n",
1939 (unsigned long long)channel->intr_out_empty);
1941 static VMBUS_CHAN_ATTR(intr_out_empty, 0444, channel_intr_out_empty_show, NULL);
1943 static ssize_t channel_out_full_first_show(struct vmbus_channel *channel,
1946 return sprintf(buf, "%llu\n",
1947 (unsigned long long)channel->out_full_first);
1949 static VMBUS_CHAN_ATTR(out_full_first, 0444, channel_out_full_first_show, NULL);
1951 static ssize_t channel_out_full_total_show(struct vmbus_channel *channel,
1954 return sprintf(buf, "%llu\n",
1955 (unsigned long long)channel->out_full_total);
1957 static VMBUS_CHAN_ATTR(out_full_total, 0444, channel_out_full_total_show, NULL);
1959 static ssize_t subchannel_monitor_id_show(struct vmbus_channel *channel,
1962 return sprintf(buf, "%u\n", channel->offermsg.monitorid);
1964 static VMBUS_CHAN_ATTR(monitor_id, 0444, subchannel_monitor_id_show, NULL);
1966 static ssize_t subchannel_id_show(struct vmbus_channel *channel,
1969 return sprintf(buf, "%u\n",
1970 channel->offermsg.offer.sub_channel_index);
1972 static VMBUS_CHAN_ATTR_RO(subchannel_id);
1974 static struct attribute *vmbus_chan_attrs[] = {
1975 &chan_attr_out_mask.attr,
1976 &chan_attr_in_mask.attr,
1977 &chan_attr_read_avail.attr,
1978 &chan_attr_write_avail.attr,
1979 &chan_attr_cpu.attr,
1980 &chan_attr_pending.attr,
1981 &chan_attr_latency.attr,
1982 &chan_attr_interrupts.attr,
1983 &chan_attr_events.attr,
1984 &chan_attr_intr_in_full.attr,
1985 &chan_attr_intr_out_empty.attr,
1986 &chan_attr_out_full_first.attr,
1987 &chan_attr_out_full_total.attr,
1988 &chan_attr_monitor_id.attr,
1989 &chan_attr_subchannel_id.attr,
1994 * Channel-level attribute_group callback function. Returns the permission for
1995 * each attribute, and returns 0 if an attribute is not visible.
1997 static umode_t vmbus_chan_attr_is_visible(struct kobject *kobj,
1998 struct attribute *attr, int idx)
2000 const struct vmbus_channel *channel =
2001 container_of(kobj, struct vmbus_channel, kobj);
2003 /* Hide the monitor attributes if the monitor mechanism is not used. */
2004 if (!channel->offermsg.monitor_allocated &&
2005 (attr == &chan_attr_pending.attr ||
2006 attr == &chan_attr_latency.attr ||
2007 attr == &chan_attr_monitor_id.attr))
2013 static struct attribute_group vmbus_chan_group = {
2014 .attrs = vmbus_chan_attrs,
2015 .is_visible = vmbus_chan_attr_is_visible
2018 static struct kobj_type vmbus_chan_ktype = {
2019 .sysfs_ops = &vmbus_chan_sysfs_ops,
2020 .release = vmbus_chan_release,
2024 * vmbus_add_channel_kobj - setup a sub-directory under device/channels
2026 int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
2028 const struct device *device = &dev->device;
2029 struct kobject *kobj = &channel->kobj;
2030 u32 relid = channel->offermsg.child_relid;
2033 kobj->kset = dev->channels_kset;
2034 ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
2041 ret = sysfs_create_group(kobj, &vmbus_chan_group);
2045 * The calling functions' error handling paths will cleanup the
2046 * empty channel directory.
2049 dev_err(device, "Unable to set up channel sysfs files\n");
2053 kobject_uevent(kobj, KOBJ_ADD);
2059 * vmbus_remove_channel_attr_group - remove the channel's attribute group
2061 void vmbus_remove_channel_attr_group(struct vmbus_channel *channel)
2063 sysfs_remove_group(&channel->kobj, &vmbus_chan_group);
2067 * vmbus_device_create - Creates and registers a new child device
2070 struct hv_device *vmbus_device_create(const guid_t *type,
2071 const guid_t *instance,
2072 struct vmbus_channel *channel)
2074 struct hv_device *child_device_obj;
2076 child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
2077 if (!child_device_obj) {
2078 pr_err("Unable to allocate device object for child device\n");
2082 child_device_obj->channel = channel;
2083 guid_copy(&child_device_obj->dev_type, type);
2084 guid_copy(&child_device_obj->dev_instance, instance);
2085 child_device_obj->vendor_id = 0x1414; /* MSFT vendor ID */
2087 return child_device_obj;
2091 * vmbus_device_register - Register the child device
2093 int vmbus_device_register(struct hv_device *child_device_obj)
2095 struct kobject *kobj = &child_device_obj->device.kobj;
2098 dev_set_name(&child_device_obj->device, "%pUl",
2099 &child_device_obj->channel->offermsg.offer.if_instance);
2101 child_device_obj->device.bus = &hv_bus;
2102 child_device_obj->device.parent = &hv_acpi_dev->dev;
2103 child_device_obj->device.release = vmbus_device_release;
2106 * Register with the LDM. This will kick off the driver/device
2107 * binding...which will eventually call vmbus_match() and vmbus_probe()
2109 ret = device_register(&child_device_obj->device);
2111 pr_err("Unable to register child device\n");
2115 child_device_obj->channels_kset = kset_create_and_add("channels",
2117 if (!child_device_obj->channels_kset) {
2119 goto err_dev_unregister;
2122 ret = vmbus_add_channel_kobj(child_device_obj,
2123 child_device_obj->channel);
2125 pr_err("Unable to register primary channeln");
2126 goto err_kset_unregister;
2128 hv_debug_add_dev_dir(child_device_obj);
2132 err_kset_unregister:
2133 kset_unregister(child_device_obj->channels_kset);
2136 device_unregister(&child_device_obj->device);
2141 * vmbus_device_unregister - Remove the specified child device
2144 void vmbus_device_unregister(struct hv_device *device_obj)
2146 pr_debug("child device %s unregistered\n",
2147 dev_name(&device_obj->device));
2149 kset_unregister(device_obj->channels_kset);
2152 * Kick off the process of unregistering the device.
2153 * This will call vmbus_remove() and eventually vmbus_device_release()
2155 device_unregister(&device_obj->device);
2160 * VMBUS is an acpi enumerated device. Get the information we
2163 #define VTPM_BASE_ADDRESS 0xfed40000
2164 static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
2166 resource_size_t start = 0;
2167 resource_size_t end = 0;
2168 struct resource *new_res;
2169 struct resource **old_res = &hyperv_mmio;
2170 struct resource **prev_res = NULL;
2173 switch (res->type) {
2176 * "Address" descriptors are for bus windows. Ignore
2177 * "memory" descriptors, which are for registers on
2180 case ACPI_RESOURCE_TYPE_ADDRESS32:
2181 start = res->data.address32.address.minimum;
2182 end = res->data.address32.address.maximum;
2185 case ACPI_RESOURCE_TYPE_ADDRESS64:
2186 start = res->data.address64.address.minimum;
2187 end = res->data.address64.address.maximum;
2191 * The IRQ information is needed only on ARM64, which Hyper-V
2192 * sets up in the extended format. IRQ information is present
2193 * on x86/x64 in the non-extended format but it is not used by
2194 * Linux. So don't bother checking for the non-extended format.
2196 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
2197 if (!acpi_dev_resource_interrupt(res, 0, &r)) {
2198 pr_err("Unable to parse Hyper-V ACPI interrupt\n");
2201 /* ARM64 INTID for VMbus */
2202 vmbus_interrupt = res->data.extended_irq.interrupts[0];
2203 /* Linux IRQ number */
2204 vmbus_irq = r.start;
2208 /* Unused resource type */
2213 * Ignore ranges that are below 1MB, as they're not
2214 * necessary or useful here.
2219 new_res = kzalloc(sizeof(*new_res), GFP_ATOMIC);
2221 return AE_NO_MEMORY;
2223 /* If this range overlaps the virtual TPM, truncate it. */
2224 if (end > VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS)
2225 end = VTPM_BASE_ADDRESS;
2227 new_res->name = "hyperv mmio";
2228 new_res->flags = IORESOURCE_MEM;
2229 new_res->start = start;
2233 * If two ranges are adjacent, merge them.
2241 if (((*old_res)->end + 1) == new_res->start) {
2242 (*old_res)->end = new_res->end;
2247 if ((*old_res)->start == new_res->end + 1) {
2248 (*old_res)->start = new_res->start;
2253 if ((*old_res)->start > new_res->end) {
2254 new_res->sibling = *old_res;
2256 (*prev_res)->sibling = new_res;
2262 old_res = &(*old_res)->sibling;
2269 static int vmbus_acpi_remove(struct acpi_device *device)
2271 struct resource *cur_res;
2272 struct resource *next_res;
2276 __release_region(hyperv_mmio, fb_mmio->start,
2277 resource_size(fb_mmio));
2281 for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) {
2282 next_res = cur_res->sibling;
2290 static void vmbus_reserve_fb(void)
2294 * Make a claim for the frame buffer in the resource tree under the
2295 * first node, which will be the one below 4GB. The length seems to
2296 * be underreported, particularly in a Generation 1 VM. So start out
2297 * reserving a larger area and make it smaller until it succeeds.
2300 if (screen_info.lfb_base) {
2301 if (efi_enabled(EFI_BOOT))
2302 size = max_t(__u32, screen_info.lfb_size, 0x800000);
2304 size = max_t(__u32, screen_info.lfb_size, 0x4000000);
2306 for (; !fb_mmio && (size >= 0x100000); size >>= 1) {
2307 fb_mmio = __request_region(hyperv_mmio,
2308 screen_info.lfb_base, size,
2315 * vmbus_allocate_mmio() - Pick a memory-mapped I/O range.
2316 * @new: If successful, supplied a pointer to the
2317 * allocated MMIO space.
2318 * @device_obj: Identifies the caller
2319 * @min: Minimum guest physical address of the
2321 * @max: Maximum guest physical address
2322 * @size: Size of the range to be allocated
2323 * @align: Alignment of the range to be allocated
2324 * @fb_overlap_ok: Whether this allocation can be allowed
2325 * to overlap the video frame buffer.
2327 * This function walks the resources granted to VMBus by the
2328 * _CRS object in the ACPI namespace underneath the parent
2329 * "bridge" whether that's a root PCI bus in the Generation 1
2330 * case or a Module Device in the Generation 2 case. It then
2331 * attempts to allocate from the global MMIO pool in a way that
2332 * matches the constraints supplied in these parameters and by
2335 * Return: 0 on success, -errno on failure
2337 int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
2338 resource_size_t min, resource_size_t max,
2339 resource_size_t size, resource_size_t align,
2342 struct resource *iter, *shadow;
2343 resource_size_t range_min, range_max, start, end;
2344 const char *dev_n = dev_name(&device_obj->device);
2348 mutex_lock(&hyperv_mmio_lock);
2351 * If overlaps with frame buffers are allowed, then first attempt to
2352 * make the allocation from within the reserved region. Because it
2353 * is already reserved, no shadow allocation is necessary.
2355 if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) &&
2356 !(max < fb_mmio->start)) {
2358 range_min = fb_mmio->start;
2359 range_max = fb_mmio->end;
2360 start = (range_min + align - 1) & ~(align - 1);
2361 for (; start + size - 1 <= range_max; start += align) {
2362 *new = request_mem_region_exclusive(start, size, dev_n);
2370 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
2371 if ((iter->start >= max) || (iter->end <= min))
2374 range_min = iter->start;
2375 range_max = iter->end;
2376 start = (range_min + align - 1) & ~(align - 1);
2377 for (; start + size - 1 <= range_max; start += align) {
2378 end = start + size - 1;
2380 /* Skip the whole fb_mmio region if not fb_overlap_ok */
2381 if (!fb_overlap_ok && fb_mmio &&
2382 (((start >= fb_mmio->start) && (start <= fb_mmio->end)) ||
2383 ((end >= fb_mmio->start) && (end <= fb_mmio->end))))
2386 shadow = __request_region(iter, start, size, NULL,
2391 *new = request_mem_region_exclusive(start, size, dev_n);
2393 shadow->name = (char *)*new;
2398 __release_region(iter, start, size);
2403 mutex_unlock(&hyperv_mmio_lock);
2406 EXPORT_SYMBOL_GPL(vmbus_allocate_mmio);
2409 * vmbus_free_mmio() - Free a memory-mapped I/O range.
2410 * @start: Base address of region to release.
2411 * @size: Size of the range to be allocated
2413 * This function releases anything requested by
2414 * vmbus_mmio_allocate().
2416 void vmbus_free_mmio(resource_size_t start, resource_size_t size)
2418 struct resource *iter;
2420 mutex_lock(&hyperv_mmio_lock);
2421 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
2422 if ((iter->start >= start + size) || (iter->end <= start))
2425 __release_region(iter, start, size);
2427 release_mem_region(start, size);
2428 mutex_unlock(&hyperv_mmio_lock);
2431 EXPORT_SYMBOL_GPL(vmbus_free_mmio);
2433 static int vmbus_acpi_add(struct acpi_device *device)
2436 int ret_val = -ENODEV;
2437 struct acpi_device *ancestor;
2439 hv_acpi_dev = device;
2441 result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
2442 vmbus_walk_resources, NULL);
2444 if (ACPI_FAILURE(result))
2447 * Some ancestor of the vmbus acpi device (Gen1 or Gen2
2448 * firmware) is the VMOD that has the mmio ranges. Get that.
2450 for (ancestor = device->parent; ancestor; ancestor = ancestor->parent) {
2451 result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS,
2452 vmbus_walk_resources, NULL);
2454 if (ACPI_FAILURE(result))
2464 complete(&probe_event);
2466 vmbus_acpi_remove(device);
2470 #ifdef CONFIG_PM_SLEEP
2471 static int vmbus_bus_suspend(struct device *dev)
2473 struct vmbus_channel *channel, *sc;
2475 while (atomic_read(&vmbus_connection.offer_in_progress) != 0) {
2477 * We wait here until the completion of any channel
2478 * offers that are currently in progress.
2480 usleep_range(1000, 2000);
2483 mutex_lock(&vmbus_connection.channel_mutex);
2484 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
2485 if (!is_hvsock_channel(channel))
2488 vmbus_force_channel_rescinded(channel);
2490 mutex_unlock(&vmbus_connection.channel_mutex);
2493 * Wait until all the sub-channels and hv_sock channels have been
2494 * cleaned up. Sub-channels should be destroyed upon suspend, otherwise
2495 * they would conflict with the new sub-channels that will be created
2496 * in the resume path. hv_sock channels should also be destroyed, but
2497 * a hv_sock channel of an established hv_sock connection can not be
2498 * really destroyed since it may still be referenced by the userspace
2499 * application, so we just force the hv_sock channel to be rescinded
2500 * by vmbus_force_channel_rescinded(), and the userspace application
2501 * will thoroughly destroy the channel after hibernation.
2503 * Note: the counter nr_chan_close_on_suspend may never go above 0 if
2504 * the VM has no sub-channel and hv_sock channel, e.g. a 1-vCPU VM.
2506 if (atomic_read(&vmbus_connection.nr_chan_close_on_suspend) > 0)
2507 wait_for_completion(&vmbus_connection.ready_for_suspend_event);
2509 if (atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) != 0) {
2510 pr_err("Can not suspend due to a previous failed resuming\n");
2514 mutex_lock(&vmbus_connection.channel_mutex);
2516 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
2518 * Remove the channel from the array of channels and invalidate
2519 * the channel's relid. Upon resume, vmbus_onoffer() will fix
2520 * up the relid (and other fields, if necessary) and add the
2521 * channel back to the array.
2523 vmbus_channel_unmap_relid(channel);
2524 channel->offermsg.child_relid = INVALID_RELID;
2526 if (is_hvsock_channel(channel)) {
2527 if (!channel->rescind) {
2528 pr_err("hv_sock channel not rescinded!\n");
2534 list_for_each_entry(sc, &channel->sc_list, sc_list) {
2535 pr_err("Sub-channel not deleted!\n");
2539 atomic_inc(&vmbus_connection.nr_chan_fixup_on_resume);
2542 mutex_unlock(&vmbus_connection.channel_mutex);
2544 vmbus_initiate_unload(false);
2546 /* Reset the event for the next resume. */
2547 reinit_completion(&vmbus_connection.ready_for_resume_event);
2552 static int vmbus_bus_resume(struct device *dev)
2554 struct vmbus_channel_msginfo *msginfo;
2559 * We only use the 'vmbus_proto_version', which was in use before
2560 * hibernation, to re-negotiate with the host.
2562 if (!vmbus_proto_version) {
2563 pr_err("Invalid proto version = 0x%x\n", vmbus_proto_version);
2567 msgsize = sizeof(*msginfo) +
2568 sizeof(struct vmbus_channel_initiate_contact);
2570 msginfo = kzalloc(msgsize, GFP_KERNEL);
2572 if (msginfo == NULL)
2575 ret = vmbus_negotiate_version(msginfo, vmbus_proto_version);
2582 WARN_ON(atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) == 0);
2584 vmbus_request_offers();
2586 if (wait_for_completion_timeout(
2587 &vmbus_connection.ready_for_resume_event, 10 * HZ) == 0)
2588 pr_err("Some vmbus device is missing after suspending?\n");
2590 /* Reset the event for the next suspend. */
2591 reinit_completion(&vmbus_connection.ready_for_suspend_event);
2596 #define vmbus_bus_suspend NULL
2597 #define vmbus_bus_resume NULL
2598 #endif /* CONFIG_PM_SLEEP */
2600 static const struct acpi_device_id vmbus_acpi_device_ids[] = {
2605 MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
2608 * Note: we must use the "no_irq" ops, otherwise hibernation can not work with
2609 * PCI device assignment, because "pci_dev_pm_ops" uses the "noirq" ops: in
2610 * the resume path, the pci "noirq" restore op runs before "non-noirq" op (see
2611 * resume_target_kernel() -> dpm_resume_start(), and hibernation_restore() ->
2612 * dpm_resume_end()). This means vmbus_bus_resume() and the pci-hyperv's
2613 * resume callback must also run via the "noirq" ops.
2615 * Set suspend_noirq/resume_noirq to NULL for Suspend-to-Idle: see the comment
2616 * earlier in this file before vmbus_pm.
2619 static const struct dev_pm_ops vmbus_bus_pm = {
2620 .suspend_noirq = NULL,
2621 .resume_noirq = NULL,
2622 .freeze_noirq = vmbus_bus_suspend,
2623 .thaw_noirq = vmbus_bus_resume,
2624 .poweroff_noirq = vmbus_bus_suspend,
2625 .restore_noirq = vmbus_bus_resume
2628 static struct acpi_driver vmbus_acpi_driver = {
2630 .ids = vmbus_acpi_device_ids,
2632 .add = vmbus_acpi_add,
2633 .remove = vmbus_acpi_remove,
2635 .drv.pm = &vmbus_bus_pm,
2638 static void hv_kexec_handler(void)
2640 hv_stimer_global_cleanup();
2641 vmbus_initiate_unload(false);
2642 /* Make sure conn_state is set as hv_synic_cleanup checks for it */
2644 cpuhp_remove_state(hyperv_cpuhp_online);
2647 static void hv_crash_handler(struct pt_regs *regs)
2651 vmbus_initiate_unload(true);
2653 * In crash handler we can't schedule synic cleanup for all CPUs,
2654 * doing the cleanup for current CPU only. This should be sufficient
2657 cpu = smp_processor_id();
2658 hv_stimer_cleanup(cpu);
2659 hv_synic_disable_regs(cpu);
2662 static int hv_synic_suspend(void)
2665 * When we reach here, all the non-boot CPUs have been offlined.
2666 * If we're in a legacy configuration where stimer Direct Mode is
2667 * not enabled, the stimers on the non-boot CPUs have been unbound
2668 * in hv_synic_cleanup() -> hv_stimer_legacy_cleanup() ->
2669 * hv_stimer_cleanup() -> clockevents_unbind_device().
2671 * hv_synic_suspend() only runs on CPU0 with interrupts disabled.
2672 * Here we do not call hv_stimer_legacy_cleanup() on CPU0 because:
2673 * 1) it's unnecessary as interrupts remain disabled between
2674 * syscore_suspend() and syscore_resume(): see create_image() and
2675 * resume_target_kernel()
2676 * 2) the stimer on CPU0 is automatically disabled later by
2677 * syscore_suspend() -> timekeeping_suspend() -> tick_suspend() -> ...
2678 * -> clockevents_shutdown() -> ... -> hv_ce_shutdown()
2679 * 3) a warning would be triggered if we call
2680 * clockevents_unbind_device(), which may sleep, in an
2681 * interrupts-disabled context.
2684 hv_synic_disable_regs(0);
2689 static void hv_synic_resume(void)
2691 hv_synic_enable_regs(0);
2694 * Note: we don't need to call hv_stimer_init(0), because the timer
2695 * on CPU0 is not unbound in hv_synic_suspend(), and the timer is
2696 * automatically re-enabled in timekeeping_resume().
2700 /* The callbacks run only on CPU0, with irqs_disabled. */
2701 static struct syscore_ops hv_synic_syscore_ops = {
2702 .suspend = hv_synic_suspend,
2703 .resume = hv_synic_resume,
2706 static int __init hv_acpi_init(void)
2710 if (!hv_is_hyperv_initialized())
2713 if (hv_root_partition)
2716 init_completion(&probe_event);
2719 * Get ACPI resources first.
2721 ret = acpi_bus_register_driver(&vmbus_acpi_driver);
2726 t = wait_for_completion_timeout(&probe_event, 5*HZ);
2733 * If we're on an architecture with a hardcoded hypervisor
2734 * vector (i.e. x86/x64), override the VMbus interrupt found
2735 * in the ACPI tables. Ensure vmbus_irq is not set since the
2736 * normal Linux IRQ mechanism is not used in this case.
2738 #ifdef HYPERVISOR_CALLBACK_VECTOR
2739 vmbus_interrupt = HYPERVISOR_CALLBACK_VECTOR;
2745 ret = vmbus_bus_init();
2749 hv_setup_kexec_handler(hv_kexec_handler);
2750 hv_setup_crash_handler(hv_crash_handler);
2752 register_syscore_ops(&hv_synic_syscore_ops);
2757 acpi_bus_unregister_driver(&vmbus_acpi_driver);
2762 static void __exit vmbus_exit(void)
2766 unregister_syscore_ops(&hv_synic_syscore_ops);
2768 hv_remove_kexec_handler();
2769 hv_remove_crash_handler();
2770 vmbus_connection.conn_state = DISCONNECTED;
2771 hv_stimer_global_cleanup();
2773 if (vmbus_irq == -1) {
2774 hv_remove_vmbus_handler();
2776 free_percpu_irq(vmbus_irq, vmbus_evt);
2777 free_percpu(vmbus_evt);
2779 for_each_online_cpu(cpu) {
2780 struct hv_per_cpu_context *hv_cpu
2781 = per_cpu_ptr(hv_context.cpu_context, cpu);
2783 tasklet_kill(&hv_cpu->msg_dpc);
2785 hv_debug_rm_all_dir();
2787 vmbus_free_channels();
2788 kfree(vmbus_connection.channels);
2790 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
2791 kmsg_dump_unregister(&hv_kmsg_dumper);
2792 unregister_die_notifier(&hyperv_die_block);
2796 * The panic notifier is always registered, hence we should
2797 * also unconditionally unregister it here as well.
2799 atomic_notifier_chain_unregister(&panic_notifier_list,
2800 &hyperv_panic_block);
2802 free_page((unsigned long)hv_panic_page);
2803 unregister_sysctl_table(hv_ctl_table_hdr);
2804 hv_ctl_table_hdr = NULL;
2805 bus_unregister(&hv_bus);
2807 cpuhp_remove_state(hyperv_cpuhp_online);
2809 acpi_bus_unregister_driver(&vmbus_acpi_driver);
2813 MODULE_LICENSE("GPL");
2814 MODULE_DESCRIPTION("Microsoft Hyper-V VMBus Driver");
2816 subsys_initcall(hv_acpi_init);
2817 module_exit(vmbus_exit);