dm writecache: add cond_resched to loop in persistent_memory_claim()
[linux/fpc-iii.git] / drivers / hv / vmbus_drv.c
blobec173da45b42209a6975ca49f448289318c19953
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2009, Microsoft Corporation.
5 * Authors:
6 * Haiyang Zhang <haiyangz@microsoft.com>
7 * Hank Janssen <hjanssen@microsoft.com>
8 * K. Y. Srinivasan <kys@microsoft.com>
9 */
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 <asm/mshyperv.h>
27 #include <linux/delay.h>
28 #include <linux/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"
39 struct vmbus_dynid {
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;
53 * Boolean to control whether to report panic messages over Hyper-V.
55 * It can be set via /proc/sys/kernel/hyperv/record_panic_msg
57 static int sysctl_record_panic_msg = 1;
59 static int hyperv_report_reg(void)
61 return !sysctl_record_panic_msg || !hv_panic_page;
64 static int hyperv_panic_event(struct notifier_block *nb, unsigned long val,
65 void *args)
67 struct pt_regs *regs;
69 vmbus_initiate_unload(true);
72 * Hyper-V should be notified only once about a panic. If we will be
73 * doing hyperv_report_panic_msg() later with kmsg data, don't do
74 * the notification here.
76 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE
77 && hyperv_report_reg()) {
78 regs = current_pt_regs();
79 hyperv_report_panic(regs, val, false);
81 return NOTIFY_DONE;
84 static int hyperv_die_event(struct notifier_block *nb, unsigned long val,
85 void *args)
87 struct die_args *die = (struct die_args *)args;
88 struct pt_regs *regs = die->regs;
91 * Hyper-V should be notified only once about a panic. If we will be
92 * doing hyperv_report_panic_msg() later with kmsg data, don't do
93 * the notification here.
95 if (hyperv_report_reg())
96 hyperv_report_panic(regs, val, true);
97 return NOTIFY_DONE;
100 static struct notifier_block hyperv_die_block = {
101 .notifier_call = hyperv_die_event,
103 static struct notifier_block hyperv_panic_block = {
104 .notifier_call = hyperv_panic_event,
107 static const char *fb_mmio_name = "fb_range";
108 static struct resource *fb_mmio;
109 static struct resource *hyperv_mmio;
110 static DEFINE_MUTEX(hyperv_mmio_lock);
112 static int vmbus_exists(void)
114 if (hv_acpi_dev == NULL)
115 return -ENODEV;
117 return 0;
120 #define VMBUS_ALIAS_LEN ((sizeof((struct hv_vmbus_device_id *)0)->guid) * 2)
121 static void print_alias_name(struct hv_device *hv_dev, char *alias_name)
123 int i;
124 for (i = 0; i < VMBUS_ALIAS_LEN; i += 2)
125 sprintf(&alias_name[i], "%02x", hv_dev->dev_type.b[i/2]);
128 static u8 channel_monitor_group(const struct vmbus_channel *channel)
130 return (u8)channel->offermsg.monitorid / 32;
133 static u8 channel_monitor_offset(const struct vmbus_channel *channel)
135 return (u8)channel->offermsg.monitorid % 32;
138 static u32 channel_pending(const struct vmbus_channel *channel,
139 const struct hv_monitor_page *monitor_page)
141 u8 monitor_group = channel_monitor_group(channel);
143 return monitor_page->trigger_group[monitor_group].pending;
146 static u32 channel_latency(const struct vmbus_channel *channel,
147 const struct hv_monitor_page *monitor_page)
149 u8 monitor_group = channel_monitor_group(channel);
150 u8 monitor_offset = channel_monitor_offset(channel);
152 return monitor_page->latency[monitor_group][monitor_offset];
155 static u32 channel_conn_id(struct vmbus_channel *channel,
156 struct hv_monitor_page *monitor_page)
158 u8 monitor_group = channel_monitor_group(channel);
159 u8 monitor_offset = channel_monitor_offset(channel);
160 return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
163 static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
164 char *buf)
166 struct hv_device *hv_dev = device_to_hv_device(dev);
168 if (!hv_dev->channel)
169 return -ENODEV;
170 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
172 static DEVICE_ATTR_RO(id);
174 static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
175 char *buf)
177 struct hv_device *hv_dev = device_to_hv_device(dev);
179 if (!hv_dev->channel)
180 return -ENODEV;
181 return sprintf(buf, "%d\n", hv_dev->channel->state);
183 static DEVICE_ATTR_RO(state);
185 static ssize_t monitor_id_show(struct device *dev,
186 struct device_attribute *dev_attr, char *buf)
188 struct hv_device *hv_dev = device_to_hv_device(dev);
190 if (!hv_dev->channel)
191 return -ENODEV;
192 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
194 static DEVICE_ATTR_RO(monitor_id);
196 static ssize_t class_id_show(struct device *dev,
197 struct device_attribute *dev_attr, char *buf)
199 struct hv_device *hv_dev = device_to_hv_device(dev);
201 if (!hv_dev->channel)
202 return -ENODEV;
203 return sprintf(buf, "{%pUl}\n",
204 hv_dev->channel->offermsg.offer.if_type.b);
206 static DEVICE_ATTR_RO(class_id);
208 static ssize_t device_id_show(struct device *dev,
209 struct device_attribute *dev_attr, char *buf)
211 struct hv_device *hv_dev = device_to_hv_device(dev);
213 if (!hv_dev->channel)
214 return -ENODEV;
215 return sprintf(buf, "{%pUl}\n",
216 hv_dev->channel->offermsg.offer.if_instance.b);
218 static DEVICE_ATTR_RO(device_id);
220 static ssize_t modalias_show(struct device *dev,
221 struct device_attribute *dev_attr, char *buf)
223 struct hv_device *hv_dev = device_to_hv_device(dev);
224 char alias_name[VMBUS_ALIAS_LEN + 1];
226 print_alias_name(hv_dev, alias_name);
227 return sprintf(buf, "vmbus:%s\n", alias_name);
229 static DEVICE_ATTR_RO(modalias);
231 #ifdef CONFIG_NUMA
232 static ssize_t numa_node_show(struct device *dev,
233 struct device_attribute *attr, char *buf)
235 struct hv_device *hv_dev = device_to_hv_device(dev);
237 if (!hv_dev->channel)
238 return -ENODEV;
240 return sprintf(buf, "%d\n", hv_dev->channel->numa_node);
242 static DEVICE_ATTR_RO(numa_node);
243 #endif
245 static ssize_t server_monitor_pending_show(struct device *dev,
246 struct device_attribute *dev_attr,
247 char *buf)
249 struct hv_device *hv_dev = device_to_hv_device(dev);
251 if (!hv_dev->channel)
252 return -ENODEV;
253 return sprintf(buf, "%d\n",
254 channel_pending(hv_dev->channel,
255 vmbus_connection.monitor_pages[0]));
257 static DEVICE_ATTR_RO(server_monitor_pending);
259 static ssize_t client_monitor_pending_show(struct device *dev,
260 struct device_attribute *dev_attr,
261 char *buf)
263 struct hv_device *hv_dev = device_to_hv_device(dev);
265 if (!hv_dev->channel)
266 return -ENODEV;
267 return sprintf(buf, "%d\n",
268 channel_pending(hv_dev->channel,
269 vmbus_connection.monitor_pages[1]));
271 static DEVICE_ATTR_RO(client_monitor_pending);
273 static ssize_t server_monitor_latency_show(struct device *dev,
274 struct device_attribute *dev_attr,
275 char *buf)
277 struct hv_device *hv_dev = device_to_hv_device(dev);
279 if (!hv_dev->channel)
280 return -ENODEV;
281 return sprintf(buf, "%d\n",
282 channel_latency(hv_dev->channel,
283 vmbus_connection.monitor_pages[0]));
285 static DEVICE_ATTR_RO(server_monitor_latency);
287 static ssize_t client_monitor_latency_show(struct device *dev,
288 struct device_attribute *dev_attr,
289 char *buf)
291 struct hv_device *hv_dev = device_to_hv_device(dev);
293 if (!hv_dev->channel)
294 return -ENODEV;
295 return sprintf(buf, "%d\n",
296 channel_latency(hv_dev->channel,
297 vmbus_connection.monitor_pages[1]));
299 static DEVICE_ATTR_RO(client_monitor_latency);
301 static ssize_t server_monitor_conn_id_show(struct device *dev,
302 struct device_attribute *dev_attr,
303 char *buf)
305 struct hv_device *hv_dev = device_to_hv_device(dev);
307 if (!hv_dev->channel)
308 return -ENODEV;
309 return sprintf(buf, "%d\n",
310 channel_conn_id(hv_dev->channel,
311 vmbus_connection.monitor_pages[0]));
313 static DEVICE_ATTR_RO(server_monitor_conn_id);
315 static ssize_t client_monitor_conn_id_show(struct device *dev,
316 struct device_attribute *dev_attr,
317 char *buf)
319 struct hv_device *hv_dev = device_to_hv_device(dev);
321 if (!hv_dev->channel)
322 return -ENODEV;
323 return sprintf(buf, "%d\n",
324 channel_conn_id(hv_dev->channel,
325 vmbus_connection.monitor_pages[1]));
327 static DEVICE_ATTR_RO(client_monitor_conn_id);
329 static ssize_t out_intr_mask_show(struct device *dev,
330 struct device_attribute *dev_attr, char *buf)
332 struct hv_device *hv_dev = device_to_hv_device(dev);
333 struct hv_ring_buffer_debug_info outbound;
334 int ret;
336 if (!hv_dev->channel)
337 return -ENODEV;
339 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
340 &outbound);
341 if (ret < 0)
342 return ret;
344 return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
346 static DEVICE_ATTR_RO(out_intr_mask);
348 static ssize_t out_read_index_show(struct device *dev,
349 struct device_attribute *dev_attr, char *buf)
351 struct hv_device *hv_dev = device_to_hv_device(dev);
352 struct hv_ring_buffer_debug_info outbound;
353 int ret;
355 if (!hv_dev->channel)
356 return -ENODEV;
358 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
359 &outbound);
360 if (ret < 0)
361 return ret;
362 return sprintf(buf, "%d\n", outbound.current_read_index);
364 static DEVICE_ATTR_RO(out_read_index);
366 static ssize_t out_write_index_show(struct device *dev,
367 struct device_attribute *dev_attr,
368 char *buf)
370 struct hv_device *hv_dev = device_to_hv_device(dev);
371 struct hv_ring_buffer_debug_info outbound;
372 int ret;
374 if (!hv_dev->channel)
375 return -ENODEV;
377 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
378 &outbound);
379 if (ret < 0)
380 return ret;
381 return sprintf(buf, "%d\n", outbound.current_write_index);
383 static DEVICE_ATTR_RO(out_write_index);
385 static ssize_t out_read_bytes_avail_show(struct device *dev,
386 struct device_attribute *dev_attr,
387 char *buf)
389 struct hv_device *hv_dev = device_to_hv_device(dev);
390 struct hv_ring_buffer_debug_info outbound;
391 int ret;
393 if (!hv_dev->channel)
394 return -ENODEV;
396 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
397 &outbound);
398 if (ret < 0)
399 return ret;
400 return sprintf(buf, "%d\n", outbound.bytes_avail_toread);
402 static DEVICE_ATTR_RO(out_read_bytes_avail);
404 static ssize_t out_write_bytes_avail_show(struct device *dev,
405 struct device_attribute *dev_attr,
406 char *buf)
408 struct hv_device *hv_dev = device_to_hv_device(dev);
409 struct hv_ring_buffer_debug_info outbound;
410 int ret;
412 if (!hv_dev->channel)
413 return -ENODEV;
415 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
416 &outbound);
417 if (ret < 0)
418 return ret;
419 return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
421 static DEVICE_ATTR_RO(out_write_bytes_avail);
423 static ssize_t in_intr_mask_show(struct device *dev,
424 struct device_attribute *dev_attr, char *buf)
426 struct hv_device *hv_dev = device_to_hv_device(dev);
427 struct hv_ring_buffer_debug_info inbound;
428 int ret;
430 if (!hv_dev->channel)
431 return -ENODEV;
433 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
434 if (ret < 0)
435 return ret;
437 return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
439 static DEVICE_ATTR_RO(in_intr_mask);
441 static ssize_t in_read_index_show(struct device *dev,
442 struct device_attribute *dev_attr, char *buf)
444 struct hv_device *hv_dev = device_to_hv_device(dev);
445 struct hv_ring_buffer_debug_info inbound;
446 int ret;
448 if (!hv_dev->channel)
449 return -ENODEV;
451 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
452 if (ret < 0)
453 return ret;
455 return sprintf(buf, "%d\n", inbound.current_read_index);
457 static DEVICE_ATTR_RO(in_read_index);
459 static ssize_t in_write_index_show(struct device *dev,
460 struct device_attribute *dev_attr, char *buf)
462 struct hv_device *hv_dev = device_to_hv_device(dev);
463 struct hv_ring_buffer_debug_info inbound;
464 int ret;
466 if (!hv_dev->channel)
467 return -ENODEV;
469 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
470 if (ret < 0)
471 return ret;
473 return sprintf(buf, "%d\n", inbound.current_write_index);
475 static DEVICE_ATTR_RO(in_write_index);
477 static ssize_t in_read_bytes_avail_show(struct device *dev,
478 struct device_attribute *dev_attr,
479 char *buf)
481 struct hv_device *hv_dev = device_to_hv_device(dev);
482 struct hv_ring_buffer_debug_info inbound;
483 int ret;
485 if (!hv_dev->channel)
486 return -ENODEV;
488 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
489 if (ret < 0)
490 return ret;
492 return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
494 static DEVICE_ATTR_RO(in_read_bytes_avail);
496 static ssize_t in_write_bytes_avail_show(struct device *dev,
497 struct device_attribute *dev_attr,
498 char *buf)
500 struct hv_device *hv_dev = device_to_hv_device(dev);
501 struct hv_ring_buffer_debug_info inbound;
502 int ret;
504 if (!hv_dev->channel)
505 return -ENODEV;
507 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
508 if (ret < 0)
509 return ret;
511 return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
513 static DEVICE_ATTR_RO(in_write_bytes_avail);
515 static ssize_t channel_vp_mapping_show(struct device *dev,
516 struct device_attribute *dev_attr,
517 char *buf)
519 struct hv_device *hv_dev = device_to_hv_device(dev);
520 struct vmbus_channel *channel = hv_dev->channel, *cur_sc;
521 unsigned long flags;
522 int buf_size = PAGE_SIZE, n_written, tot_written;
523 struct list_head *cur;
525 if (!channel)
526 return -ENODEV;
528 tot_written = snprintf(buf, buf_size, "%u:%u\n",
529 channel->offermsg.child_relid, channel->target_cpu);
531 spin_lock_irqsave(&channel->lock, flags);
533 list_for_each(cur, &channel->sc_list) {
534 if (tot_written >= buf_size - 1)
535 break;
537 cur_sc = list_entry(cur, struct vmbus_channel, sc_list);
538 n_written = scnprintf(buf + tot_written,
539 buf_size - tot_written,
540 "%u:%u\n",
541 cur_sc->offermsg.child_relid,
542 cur_sc->target_cpu);
543 tot_written += n_written;
546 spin_unlock_irqrestore(&channel->lock, flags);
548 return tot_written;
550 static DEVICE_ATTR_RO(channel_vp_mapping);
552 static ssize_t vendor_show(struct device *dev,
553 struct device_attribute *dev_attr,
554 char *buf)
556 struct hv_device *hv_dev = device_to_hv_device(dev);
557 return sprintf(buf, "0x%x\n", hv_dev->vendor_id);
559 static DEVICE_ATTR_RO(vendor);
561 static ssize_t device_show(struct device *dev,
562 struct device_attribute *dev_attr,
563 char *buf)
565 struct hv_device *hv_dev = device_to_hv_device(dev);
566 return sprintf(buf, "0x%x\n", hv_dev->device_id);
568 static DEVICE_ATTR_RO(device);
570 static ssize_t driver_override_store(struct device *dev,
571 struct device_attribute *attr,
572 const char *buf, size_t count)
574 struct hv_device *hv_dev = device_to_hv_device(dev);
575 char *driver_override, *old, *cp;
577 /* We need to keep extra room for a newline */
578 if (count >= (PAGE_SIZE - 1))
579 return -EINVAL;
581 driver_override = kstrndup(buf, count, GFP_KERNEL);
582 if (!driver_override)
583 return -ENOMEM;
585 cp = strchr(driver_override, '\n');
586 if (cp)
587 *cp = '\0';
589 device_lock(dev);
590 old = hv_dev->driver_override;
591 if (strlen(driver_override)) {
592 hv_dev->driver_override = driver_override;
593 } else {
594 kfree(driver_override);
595 hv_dev->driver_override = NULL;
597 device_unlock(dev);
599 kfree(old);
601 return count;
604 static ssize_t driver_override_show(struct device *dev,
605 struct device_attribute *attr, char *buf)
607 struct hv_device *hv_dev = device_to_hv_device(dev);
608 ssize_t len;
610 device_lock(dev);
611 len = snprintf(buf, PAGE_SIZE, "%s\n", hv_dev->driver_override);
612 device_unlock(dev);
614 return len;
616 static DEVICE_ATTR_RW(driver_override);
618 /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
619 static struct attribute *vmbus_dev_attrs[] = {
620 &dev_attr_id.attr,
621 &dev_attr_state.attr,
622 &dev_attr_monitor_id.attr,
623 &dev_attr_class_id.attr,
624 &dev_attr_device_id.attr,
625 &dev_attr_modalias.attr,
626 #ifdef CONFIG_NUMA
627 &dev_attr_numa_node.attr,
628 #endif
629 &dev_attr_server_monitor_pending.attr,
630 &dev_attr_client_monitor_pending.attr,
631 &dev_attr_server_monitor_latency.attr,
632 &dev_attr_client_monitor_latency.attr,
633 &dev_attr_server_monitor_conn_id.attr,
634 &dev_attr_client_monitor_conn_id.attr,
635 &dev_attr_out_intr_mask.attr,
636 &dev_attr_out_read_index.attr,
637 &dev_attr_out_write_index.attr,
638 &dev_attr_out_read_bytes_avail.attr,
639 &dev_attr_out_write_bytes_avail.attr,
640 &dev_attr_in_intr_mask.attr,
641 &dev_attr_in_read_index.attr,
642 &dev_attr_in_write_index.attr,
643 &dev_attr_in_read_bytes_avail.attr,
644 &dev_attr_in_write_bytes_avail.attr,
645 &dev_attr_channel_vp_mapping.attr,
646 &dev_attr_vendor.attr,
647 &dev_attr_device.attr,
648 &dev_attr_driver_override.attr,
649 NULL,
653 * Device-level attribute_group callback function. Returns the permission for
654 * each attribute, and returns 0 if an attribute is not visible.
656 static umode_t vmbus_dev_attr_is_visible(struct kobject *kobj,
657 struct attribute *attr, int idx)
659 struct device *dev = kobj_to_dev(kobj);
660 const struct hv_device *hv_dev = device_to_hv_device(dev);
662 /* Hide the monitor attributes if the monitor mechanism is not used. */
663 if (!hv_dev->channel->offermsg.monitor_allocated &&
664 (attr == &dev_attr_monitor_id.attr ||
665 attr == &dev_attr_server_monitor_pending.attr ||
666 attr == &dev_attr_client_monitor_pending.attr ||
667 attr == &dev_attr_server_monitor_latency.attr ||
668 attr == &dev_attr_client_monitor_latency.attr ||
669 attr == &dev_attr_server_monitor_conn_id.attr ||
670 attr == &dev_attr_client_monitor_conn_id.attr))
671 return 0;
673 return attr->mode;
676 static const struct attribute_group vmbus_dev_group = {
677 .attrs = vmbus_dev_attrs,
678 .is_visible = vmbus_dev_attr_is_visible
680 __ATTRIBUTE_GROUPS(vmbus_dev);
683 * vmbus_uevent - add uevent for our device
685 * This routine is invoked when a device is added or removed on the vmbus to
686 * generate a uevent to udev in the userspace. The udev will then look at its
687 * rule and the uevent generated here to load the appropriate driver
689 * The alias string will be of the form vmbus:guid where guid is the string
690 * representation of the device guid (each byte of the guid will be
691 * represented with two hex characters.
693 static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
695 struct hv_device *dev = device_to_hv_device(device);
696 int ret;
697 char alias_name[VMBUS_ALIAS_LEN + 1];
699 print_alias_name(dev, alias_name);
700 ret = add_uevent_var(env, "MODALIAS=vmbus:%s", alias_name);
701 return ret;
704 static const struct hv_vmbus_device_id *
705 hv_vmbus_dev_match(const struct hv_vmbus_device_id *id, const guid_t *guid)
707 if (id == NULL)
708 return NULL; /* empty device table */
710 for (; !guid_is_null(&id->guid); id++)
711 if (guid_equal(&id->guid, guid))
712 return id;
714 return NULL;
717 static const struct hv_vmbus_device_id *
718 hv_vmbus_dynid_match(struct hv_driver *drv, const guid_t *guid)
720 const struct hv_vmbus_device_id *id = NULL;
721 struct vmbus_dynid *dynid;
723 spin_lock(&drv->dynids.lock);
724 list_for_each_entry(dynid, &drv->dynids.list, node) {
725 if (guid_equal(&dynid->id.guid, guid)) {
726 id = &dynid->id;
727 break;
730 spin_unlock(&drv->dynids.lock);
732 return id;
735 static const struct hv_vmbus_device_id vmbus_device_null;
738 * Return a matching hv_vmbus_device_id pointer.
739 * If there is no match, return NULL.
741 static const struct hv_vmbus_device_id *hv_vmbus_get_id(struct hv_driver *drv,
742 struct hv_device *dev)
744 const guid_t *guid = &dev->dev_type;
745 const struct hv_vmbus_device_id *id;
747 /* When driver_override is set, only bind to the matching driver */
748 if (dev->driver_override && strcmp(dev->driver_override, drv->name))
749 return NULL;
751 /* Look at the dynamic ids first, before the static ones */
752 id = hv_vmbus_dynid_match(drv, guid);
753 if (!id)
754 id = hv_vmbus_dev_match(drv->id_table, guid);
756 /* driver_override will always match, send a dummy id */
757 if (!id && dev->driver_override)
758 id = &vmbus_device_null;
760 return id;
763 /* vmbus_add_dynid - add a new device ID to this driver and re-probe devices */
764 static int vmbus_add_dynid(struct hv_driver *drv, guid_t *guid)
766 struct vmbus_dynid *dynid;
768 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
769 if (!dynid)
770 return -ENOMEM;
772 dynid->id.guid = *guid;
774 spin_lock(&drv->dynids.lock);
775 list_add_tail(&dynid->node, &drv->dynids.list);
776 spin_unlock(&drv->dynids.lock);
778 return driver_attach(&drv->driver);
781 static void vmbus_free_dynids(struct hv_driver *drv)
783 struct vmbus_dynid *dynid, *n;
785 spin_lock(&drv->dynids.lock);
786 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
787 list_del(&dynid->node);
788 kfree(dynid);
790 spin_unlock(&drv->dynids.lock);
794 * store_new_id - sysfs frontend to vmbus_add_dynid()
796 * Allow GUIDs to be added to an existing driver via sysfs.
798 static ssize_t new_id_store(struct device_driver *driver, const char *buf,
799 size_t count)
801 struct hv_driver *drv = drv_to_hv_drv(driver);
802 guid_t guid;
803 ssize_t retval;
805 retval = guid_parse(buf, &guid);
806 if (retval)
807 return retval;
809 if (hv_vmbus_dynid_match(drv, &guid))
810 return -EEXIST;
812 retval = vmbus_add_dynid(drv, &guid);
813 if (retval)
814 return retval;
815 return count;
817 static DRIVER_ATTR_WO(new_id);
820 * store_remove_id - remove a PCI device ID from this driver
822 * Removes a dynamic pci device ID to this driver.
824 static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
825 size_t count)
827 struct hv_driver *drv = drv_to_hv_drv(driver);
828 struct vmbus_dynid *dynid, *n;
829 guid_t guid;
830 ssize_t retval;
832 retval = guid_parse(buf, &guid);
833 if (retval)
834 return retval;
836 retval = -ENODEV;
837 spin_lock(&drv->dynids.lock);
838 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
839 struct hv_vmbus_device_id *id = &dynid->id;
841 if (guid_equal(&id->guid, &guid)) {
842 list_del(&dynid->node);
843 kfree(dynid);
844 retval = count;
845 break;
848 spin_unlock(&drv->dynids.lock);
850 return retval;
852 static DRIVER_ATTR_WO(remove_id);
854 static struct attribute *vmbus_drv_attrs[] = {
855 &driver_attr_new_id.attr,
856 &driver_attr_remove_id.attr,
857 NULL,
859 ATTRIBUTE_GROUPS(vmbus_drv);
863 * vmbus_match - Attempt to match the specified device to the specified driver
865 static int vmbus_match(struct device *device, struct device_driver *driver)
867 struct hv_driver *drv = drv_to_hv_drv(driver);
868 struct hv_device *hv_dev = device_to_hv_device(device);
870 /* The hv_sock driver handles all hv_sock offers. */
871 if (is_hvsock_channel(hv_dev->channel))
872 return drv->hvsock;
874 if (hv_vmbus_get_id(drv, hv_dev))
875 return 1;
877 return 0;
881 * vmbus_probe - Add the new vmbus's child device
883 static int vmbus_probe(struct device *child_device)
885 int ret = 0;
886 struct hv_driver *drv =
887 drv_to_hv_drv(child_device->driver);
888 struct hv_device *dev = device_to_hv_device(child_device);
889 const struct hv_vmbus_device_id *dev_id;
891 dev_id = hv_vmbus_get_id(drv, dev);
892 if (drv->probe) {
893 ret = drv->probe(dev, dev_id);
894 if (ret != 0)
895 pr_err("probe failed for device %s (%d)\n",
896 dev_name(child_device), ret);
898 } else {
899 pr_err("probe not set for driver %s\n",
900 dev_name(child_device));
901 ret = -ENODEV;
903 return ret;
907 * vmbus_remove - Remove a vmbus device
909 static int vmbus_remove(struct device *child_device)
911 struct hv_driver *drv;
912 struct hv_device *dev = device_to_hv_device(child_device);
914 if (child_device->driver) {
915 drv = drv_to_hv_drv(child_device->driver);
916 if (drv->remove)
917 drv->remove(dev);
920 return 0;
925 * vmbus_shutdown - Shutdown a vmbus device
927 static void vmbus_shutdown(struct device *child_device)
929 struct hv_driver *drv;
930 struct hv_device *dev = device_to_hv_device(child_device);
933 /* The device may not be attached yet */
934 if (!child_device->driver)
935 return;
937 drv = drv_to_hv_drv(child_device->driver);
939 if (drv->shutdown)
940 drv->shutdown(dev);
943 #ifdef CONFIG_PM_SLEEP
945 * vmbus_suspend - Suspend a vmbus device
947 static int vmbus_suspend(struct device *child_device)
949 struct hv_driver *drv;
950 struct hv_device *dev = device_to_hv_device(child_device);
952 /* The device may not be attached yet */
953 if (!child_device->driver)
954 return 0;
956 drv = drv_to_hv_drv(child_device->driver);
957 if (!drv->suspend)
958 return -EOPNOTSUPP;
960 return drv->suspend(dev);
964 * vmbus_resume - Resume a vmbus device
966 static int vmbus_resume(struct device *child_device)
968 struct hv_driver *drv;
969 struct hv_device *dev = device_to_hv_device(child_device);
971 /* The device may not be attached yet */
972 if (!child_device->driver)
973 return 0;
975 drv = drv_to_hv_drv(child_device->driver);
976 if (!drv->resume)
977 return -EOPNOTSUPP;
979 return drv->resume(dev);
981 #else
982 #define vmbus_suspend NULL
983 #define vmbus_resume NULL
984 #endif /* CONFIG_PM_SLEEP */
987 * vmbus_device_release - Final callback release of the vmbus child device
989 static void vmbus_device_release(struct device *device)
991 struct hv_device *hv_dev = device_to_hv_device(device);
992 struct vmbus_channel *channel = hv_dev->channel;
994 hv_debug_rm_dev_dir(hv_dev);
996 mutex_lock(&vmbus_connection.channel_mutex);
997 hv_process_channel_removal(channel);
998 mutex_unlock(&vmbus_connection.channel_mutex);
999 kfree(hv_dev);
1003 * Note: we must use the "noirq" ops: see the comment before vmbus_bus_pm.
1005 * suspend_noirq/resume_noirq are set to NULL to support Suspend-to-Idle: we
1006 * shouldn't suspend the vmbus devices upon Suspend-to-Idle, otherwise there
1007 * is no way to wake up a Generation-2 VM.
1009 * The other 4 ops are for hibernation.
1012 static const struct dev_pm_ops vmbus_pm = {
1013 .suspend_noirq = NULL,
1014 .resume_noirq = NULL,
1015 .freeze_noirq = vmbus_suspend,
1016 .thaw_noirq = vmbus_resume,
1017 .poweroff_noirq = vmbus_suspend,
1018 .restore_noirq = vmbus_resume,
1021 /* The one and only one */
1022 static struct bus_type hv_bus = {
1023 .name = "vmbus",
1024 .match = vmbus_match,
1025 .shutdown = vmbus_shutdown,
1026 .remove = vmbus_remove,
1027 .probe = vmbus_probe,
1028 .uevent = vmbus_uevent,
1029 .dev_groups = vmbus_dev_groups,
1030 .drv_groups = vmbus_drv_groups,
1031 .pm = &vmbus_pm,
1034 struct onmessage_work_context {
1035 struct work_struct work;
1036 struct hv_message msg;
1039 static void vmbus_onmessage_work(struct work_struct *work)
1041 struct onmessage_work_context *ctx;
1043 /* Do not process messages if we're in DISCONNECTED state */
1044 if (vmbus_connection.conn_state == DISCONNECTED)
1045 return;
1047 ctx = container_of(work, struct onmessage_work_context,
1048 work);
1049 vmbus_onmessage(&ctx->msg);
1050 kfree(ctx);
1053 void vmbus_on_msg_dpc(unsigned long data)
1055 struct hv_per_cpu_context *hv_cpu = (void *)data;
1056 void *page_addr = hv_cpu->synic_message_page;
1057 struct hv_message *msg = (struct hv_message *)page_addr +
1058 VMBUS_MESSAGE_SINT;
1059 struct vmbus_channel_message_header *hdr;
1060 const struct vmbus_channel_message_table_entry *entry;
1061 struct onmessage_work_context *ctx;
1062 u32 message_type = msg->header.message_type;
1064 if (message_type == HVMSG_NONE)
1065 /* no msg */
1066 return;
1068 hdr = (struct vmbus_channel_message_header *)msg->u.payload;
1070 trace_vmbus_on_msg_dpc(hdr);
1072 if (hdr->msgtype >= CHANNELMSG_COUNT) {
1073 WARN_ONCE(1, "unknown msgtype=%d\n", hdr->msgtype);
1074 goto msg_handled;
1077 entry = &channel_message_table[hdr->msgtype];
1079 if (!entry->message_handler)
1080 goto msg_handled;
1082 if (entry->handler_type == VMHT_BLOCKING) {
1083 ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
1084 if (ctx == NULL)
1085 return;
1087 INIT_WORK(&ctx->work, vmbus_onmessage_work);
1088 memcpy(&ctx->msg, msg, sizeof(*msg));
1091 * The host can generate a rescind message while we
1092 * may still be handling the original offer. We deal with
1093 * this condition by ensuring the processing is done on the
1094 * same CPU.
1096 switch (hdr->msgtype) {
1097 case CHANNELMSG_RESCIND_CHANNELOFFER:
1099 * If we are handling the rescind message;
1100 * schedule the work on the global work queue.
1102 * The OFFER message and the RESCIND message should
1103 * not be handled by the same serialized work queue,
1104 * because the OFFER handler may call vmbus_open(),
1105 * which tries to open the channel by sending an
1106 * OPEN_CHANNEL message to the host and waits for
1107 * the host's response; however, if the host has
1108 * rescinded the channel before it receives the
1109 * OPEN_CHANNEL message, the host just silently
1110 * ignores the OPEN_CHANNEL message; as a result,
1111 * the guest's OFFER handler hangs for ever, if we
1112 * handle the RESCIND message in the same serialized
1113 * work queue: the RESCIND handler can not start to
1114 * run before the OFFER handler finishes.
1116 schedule_work_on(VMBUS_CONNECT_CPU,
1117 &ctx->work);
1118 break;
1120 case CHANNELMSG_OFFERCHANNEL:
1121 atomic_inc(&vmbus_connection.offer_in_progress);
1122 queue_work_on(VMBUS_CONNECT_CPU,
1123 vmbus_connection.work_queue,
1124 &ctx->work);
1125 break;
1127 default:
1128 queue_work(vmbus_connection.work_queue, &ctx->work);
1130 } else
1131 entry->message_handler(hdr);
1133 msg_handled:
1134 vmbus_signal_eom(msg, message_type);
1137 #ifdef CONFIG_PM_SLEEP
1139 * Fake RESCIND_CHANNEL messages to clean up hv_sock channels by force for
1140 * hibernation, because hv_sock connections can not persist across hibernation.
1142 static void vmbus_force_channel_rescinded(struct vmbus_channel *channel)
1144 struct onmessage_work_context *ctx;
1145 struct vmbus_channel_rescind_offer *rescind;
1147 WARN_ON(!is_hvsock_channel(channel));
1150 * sizeof(*ctx) is small and the allocation should really not fail,
1151 * otherwise the state of the hv_sock connections ends up in limbo.
1153 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL | __GFP_NOFAIL);
1156 * So far, these are not really used by Linux. Just set them to the
1157 * reasonable values conforming to the definitions of the fields.
1159 ctx->msg.header.message_type = 1;
1160 ctx->msg.header.payload_size = sizeof(*rescind);
1162 /* These values are actually used by Linux. */
1163 rescind = (struct vmbus_channel_rescind_offer *)ctx->msg.u.payload;
1164 rescind->header.msgtype = CHANNELMSG_RESCIND_CHANNELOFFER;
1165 rescind->child_relid = channel->offermsg.child_relid;
1167 INIT_WORK(&ctx->work, vmbus_onmessage_work);
1169 queue_work_on(VMBUS_CONNECT_CPU,
1170 vmbus_connection.work_queue,
1171 &ctx->work);
1173 #endif /* CONFIG_PM_SLEEP */
1176 * Direct callback for channels using other deferred processing
1178 static void vmbus_channel_isr(struct vmbus_channel *channel)
1180 void (*callback_fn)(void *);
1182 callback_fn = READ_ONCE(channel->onchannel_callback);
1183 if (likely(callback_fn != NULL))
1184 (*callback_fn)(channel->channel_callback_context);
1188 * Schedule all channels with events pending
1190 static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu)
1192 unsigned long *recv_int_page;
1193 u32 maxbits, relid;
1195 if (vmbus_proto_version < VERSION_WIN8) {
1196 maxbits = MAX_NUM_CHANNELS_SUPPORTED;
1197 recv_int_page = vmbus_connection.recv_int_page;
1198 } else {
1200 * When the host is win8 and beyond, the event page
1201 * can be directly checked to get the id of the channel
1202 * that has the interrupt pending.
1204 void *page_addr = hv_cpu->synic_event_page;
1205 union hv_synic_event_flags *event
1206 = (union hv_synic_event_flags *)page_addr +
1207 VMBUS_MESSAGE_SINT;
1209 maxbits = HV_EVENT_FLAGS_COUNT;
1210 recv_int_page = event->flags;
1213 if (unlikely(!recv_int_page))
1214 return;
1216 for_each_set_bit(relid, recv_int_page, maxbits) {
1217 struct vmbus_channel *channel;
1219 if (!sync_test_and_clear_bit(relid, recv_int_page))
1220 continue;
1222 /* Special case - vmbus channel protocol msg */
1223 if (relid == 0)
1224 continue;
1226 rcu_read_lock();
1228 /* Find channel based on relid */
1229 list_for_each_entry_rcu(channel, &hv_cpu->chan_list, percpu_list) {
1230 if (channel->offermsg.child_relid != relid)
1231 continue;
1233 if (channel->rescind)
1234 continue;
1236 trace_vmbus_chan_sched(channel);
1238 ++channel->interrupts;
1240 switch (channel->callback_mode) {
1241 case HV_CALL_ISR:
1242 vmbus_channel_isr(channel);
1243 break;
1245 case HV_CALL_BATCHED:
1246 hv_begin_read(&channel->inbound);
1247 /* fallthrough */
1248 case HV_CALL_DIRECT:
1249 tasklet_schedule(&channel->callback_event);
1253 rcu_read_unlock();
1257 static void vmbus_isr(void)
1259 struct hv_per_cpu_context *hv_cpu
1260 = this_cpu_ptr(hv_context.cpu_context);
1261 void *page_addr = hv_cpu->synic_event_page;
1262 struct hv_message *msg;
1263 union hv_synic_event_flags *event;
1264 bool handled = false;
1266 if (unlikely(page_addr == NULL))
1267 return;
1269 event = (union hv_synic_event_flags *)page_addr +
1270 VMBUS_MESSAGE_SINT;
1272 * Check for events before checking for messages. This is the order
1273 * in which events and messages are checked in Windows guests on
1274 * Hyper-V, and the Windows team suggested we do the same.
1277 if ((vmbus_proto_version == VERSION_WS2008) ||
1278 (vmbus_proto_version == VERSION_WIN7)) {
1280 /* Since we are a child, we only need to check bit 0 */
1281 if (sync_test_and_clear_bit(0, event->flags))
1282 handled = true;
1283 } else {
1285 * Our host is win8 or above. The signaling mechanism
1286 * has changed and we can directly look at the event page.
1287 * If bit n is set then we have an interrup on the channel
1288 * whose id is n.
1290 handled = true;
1293 if (handled)
1294 vmbus_chan_sched(hv_cpu);
1296 page_addr = hv_cpu->synic_message_page;
1297 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
1299 /* Check if there are actual msgs to be processed */
1300 if (msg->header.message_type != HVMSG_NONE) {
1301 if (msg->header.message_type == HVMSG_TIMER_EXPIRED) {
1302 hv_stimer0_isr();
1303 vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED);
1304 } else
1305 tasklet_schedule(&hv_cpu->msg_dpc);
1308 add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0);
1312 * Callback from kmsg_dump. Grab as much as possible from the end of the kmsg
1313 * buffer and call into Hyper-V to transfer the data.
1315 static void hv_kmsg_dump(struct kmsg_dumper *dumper,
1316 enum kmsg_dump_reason reason)
1318 size_t bytes_written;
1319 phys_addr_t panic_pa;
1321 /* We are only interested in panics. */
1322 if ((reason != KMSG_DUMP_PANIC) || (!sysctl_record_panic_msg))
1323 return;
1325 panic_pa = virt_to_phys(hv_panic_page);
1328 * Write dump contents to the page. No need to synchronize; panic should
1329 * be single-threaded.
1331 kmsg_dump_get_buffer(dumper, true, hv_panic_page, HV_HYP_PAGE_SIZE,
1332 &bytes_written);
1333 if (bytes_written)
1334 hyperv_report_panic_msg(panic_pa, bytes_written);
1337 static struct kmsg_dumper hv_kmsg_dumper = {
1338 .dump = hv_kmsg_dump,
1341 static struct ctl_table_header *hv_ctl_table_hdr;
1344 * sysctl option to allow the user to control whether kmsg data should be
1345 * reported to Hyper-V on panic.
1347 static struct ctl_table hv_ctl_table[] = {
1349 .procname = "hyperv_record_panic_msg",
1350 .data = &sysctl_record_panic_msg,
1351 .maxlen = sizeof(int),
1352 .mode = 0644,
1353 .proc_handler = proc_dointvec_minmax,
1354 .extra1 = SYSCTL_ZERO,
1355 .extra2 = SYSCTL_ONE
1360 static struct ctl_table hv_root_table[] = {
1362 .procname = "kernel",
1363 .mode = 0555,
1364 .child = hv_ctl_table
1370 * vmbus_bus_init -Main vmbus driver initialization routine.
1372 * Here, we
1373 * - initialize the vmbus driver context
1374 * - invoke the vmbus hv main init routine
1375 * - retrieve the channel offers
1377 static int vmbus_bus_init(void)
1379 int ret;
1381 /* Hypervisor initialization...setup hypercall page..etc */
1382 ret = hv_init();
1383 if (ret != 0) {
1384 pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
1385 return ret;
1388 ret = bus_register(&hv_bus);
1389 if (ret)
1390 return ret;
1392 hv_setup_vmbus_irq(vmbus_isr);
1394 ret = hv_synic_alloc();
1395 if (ret)
1396 goto err_alloc;
1399 * Initialize the per-cpu interrupt state and stimer state.
1400 * Then connect to the host.
1402 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hyperv/vmbus:online",
1403 hv_synic_init, hv_synic_cleanup);
1404 if (ret < 0)
1405 goto err_cpuhp;
1406 hyperv_cpuhp_online = ret;
1408 ret = vmbus_connect();
1409 if (ret)
1410 goto err_connect;
1413 * Only register if the crash MSRs are available
1415 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
1416 u64 hyperv_crash_ctl;
1418 * Sysctl registration is not fatal, since by default
1419 * reporting is enabled.
1421 hv_ctl_table_hdr = register_sysctl_table(hv_root_table);
1422 if (!hv_ctl_table_hdr)
1423 pr_err("Hyper-V: sysctl table register error");
1426 * Register for panic kmsg callback only if the right
1427 * capability is supported by the hypervisor.
1429 hv_get_crash_ctl(hyperv_crash_ctl);
1430 if (hyperv_crash_ctl & HV_CRASH_CTL_CRASH_NOTIFY_MSG) {
1431 hv_panic_page = (void *)hv_alloc_hyperv_zeroed_page();
1432 if (hv_panic_page) {
1433 ret = kmsg_dump_register(&hv_kmsg_dumper);
1434 if (ret) {
1435 pr_err("Hyper-V: kmsg dump register "
1436 "error 0x%x\n", ret);
1437 hv_free_hyperv_page(
1438 (unsigned long)hv_panic_page);
1439 hv_panic_page = NULL;
1441 } else
1442 pr_err("Hyper-V: panic message page memory "
1443 "allocation failed");
1446 register_die_notifier(&hyperv_die_block);
1450 * Always register the panic notifier because we need to unload
1451 * the VMbus channel connection to prevent any VMbus
1452 * activity after the VM panics.
1454 atomic_notifier_chain_register(&panic_notifier_list,
1455 &hyperv_panic_block);
1457 vmbus_request_offers();
1459 return 0;
1461 err_connect:
1462 cpuhp_remove_state(hyperv_cpuhp_online);
1463 err_cpuhp:
1464 hv_synic_free();
1465 err_alloc:
1466 hv_remove_vmbus_irq();
1468 bus_unregister(&hv_bus);
1469 unregister_sysctl_table(hv_ctl_table_hdr);
1470 hv_ctl_table_hdr = NULL;
1471 return ret;
1475 * __vmbus_child_driver_register() - Register a vmbus's driver
1476 * @hv_driver: Pointer to driver structure you want to register
1477 * @owner: owner module of the drv
1478 * @mod_name: module name string
1480 * Registers the given driver with Linux through the 'driver_register()' call
1481 * and sets up the hyper-v vmbus handling for this driver.
1482 * It will return the state of the 'driver_register()' call.
1485 int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
1487 int ret;
1489 pr_info("registering driver %s\n", hv_driver->name);
1491 ret = vmbus_exists();
1492 if (ret < 0)
1493 return ret;
1495 hv_driver->driver.name = hv_driver->name;
1496 hv_driver->driver.owner = owner;
1497 hv_driver->driver.mod_name = mod_name;
1498 hv_driver->driver.bus = &hv_bus;
1500 spin_lock_init(&hv_driver->dynids.lock);
1501 INIT_LIST_HEAD(&hv_driver->dynids.list);
1503 ret = driver_register(&hv_driver->driver);
1505 return ret;
1507 EXPORT_SYMBOL_GPL(__vmbus_driver_register);
1510 * vmbus_driver_unregister() - Unregister a vmbus's driver
1511 * @hv_driver: Pointer to driver structure you want to
1512 * un-register
1514 * Un-register the given driver that was previous registered with a call to
1515 * vmbus_driver_register()
1517 void vmbus_driver_unregister(struct hv_driver *hv_driver)
1519 pr_info("unregistering driver %s\n", hv_driver->name);
1521 if (!vmbus_exists()) {
1522 driver_unregister(&hv_driver->driver);
1523 vmbus_free_dynids(hv_driver);
1526 EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
1530 * Called when last reference to channel is gone.
1532 static void vmbus_chan_release(struct kobject *kobj)
1534 struct vmbus_channel *channel
1535 = container_of(kobj, struct vmbus_channel, kobj);
1537 kfree_rcu(channel, rcu);
1540 struct vmbus_chan_attribute {
1541 struct attribute attr;
1542 ssize_t (*show)(struct vmbus_channel *chan, char *buf);
1543 ssize_t (*store)(struct vmbus_channel *chan,
1544 const char *buf, size_t count);
1546 #define VMBUS_CHAN_ATTR(_name, _mode, _show, _store) \
1547 struct vmbus_chan_attribute chan_attr_##_name \
1548 = __ATTR(_name, _mode, _show, _store)
1549 #define VMBUS_CHAN_ATTR_RW(_name) \
1550 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RW(_name)
1551 #define VMBUS_CHAN_ATTR_RO(_name) \
1552 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RO(_name)
1553 #define VMBUS_CHAN_ATTR_WO(_name) \
1554 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_WO(_name)
1556 static ssize_t vmbus_chan_attr_show(struct kobject *kobj,
1557 struct attribute *attr, char *buf)
1559 const struct vmbus_chan_attribute *attribute
1560 = container_of(attr, struct vmbus_chan_attribute, attr);
1561 struct vmbus_channel *chan
1562 = container_of(kobj, struct vmbus_channel, kobj);
1564 if (!attribute->show)
1565 return -EIO;
1567 return attribute->show(chan, buf);
1570 static const struct sysfs_ops vmbus_chan_sysfs_ops = {
1571 .show = vmbus_chan_attr_show,
1574 static ssize_t out_mask_show(struct vmbus_channel *channel, char *buf)
1576 struct hv_ring_buffer_info *rbi = &channel->outbound;
1577 ssize_t ret;
1579 mutex_lock(&rbi->ring_buffer_mutex);
1580 if (!rbi->ring_buffer) {
1581 mutex_unlock(&rbi->ring_buffer_mutex);
1582 return -EINVAL;
1585 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1586 mutex_unlock(&rbi->ring_buffer_mutex);
1587 return ret;
1589 static VMBUS_CHAN_ATTR_RO(out_mask);
1591 static ssize_t in_mask_show(struct vmbus_channel *channel, char *buf)
1593 struct hv_ring_buffer_info *rbi = &channel->inbound;
1594 ssize_t ret;
1596 mutex_lock(&rbi->ring_buffer_mutex);
1597 if (!rbi->ring_buffer) {
1598 mutex_unlock(&rbi->ring_buffer_mutex);
1599 return -EINVAL;
1602 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1603 mutex_unlock(&rbi->ring_buffer_mutex);
1604 return ret;
1606 static VMBUS_CHAN_ATTR_RO(in_mask);
1608 static ssize_t read_avail_show(struct vmbus_channel *channel, char *buf)
1610 struct hv_ring_buffer_info *rbi = &channel->inbound;
1611 ssize_t ret;
1613 mutex_lock(&rbi->ring_buffer_mutex);
1614 if (!rbi->ring_buffer) {
1615 mutex_unlock(&rbi->ring_buffer_mutex);
1616 return -EINVAL;
1619 ret = sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi));
1620 mutex_unlock(&rbi->ring_buffer_mutex);
1621 return ret;
1623 static VMBUS_CHAN_ATTR_RO(read_avail);
1625 static ssize_t write_avail_show(struct vmbus_channel *channel, char *buf)
1627 struct hv_ring_buffer_info *rbi = &channel->outbound;
1628 ssize_t ret;
1630 mutex_lock(&rbi->ring_buffer_mutex);
1631 if (!rbi->ring_buffer) {
1632 mutex_unlock(&rbi->ring_buffer_mutex);
1633 return -EINVAL;
1636 ret = sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi));
1637 mutex_unlock(&rbi->ring_buffer_mutex);
1638 return ret;
1640 static VMBUS_CHAN_ATTR_RO(write_avail);
1642 static ssize_t show_target_cpu(struct vmbus_channel *channel, char *buf)
1644 return sprintf(buf, "%u\n", channel->target_cpu);
1646 static VMBUS_CHAN_ATTR(cpu, S_IRUGO, show_target_cpu, NULL);
1648 static ssize_t channel_pending_show(struct vmbus_channel *channel,
1649 char *buf)
1651 return sprintf(buf, "%d\n",
1652 channel_pending(channel,
1653 vmbus_connection.monitor_pages[1]));
1655 static VMBUS_CHAN_ATTR(pending, S_IRUGO, channel_pending_show, NULL);
1657 static ssize_t channel_latency_show(struct vmbus_channel *channel,
1658 char *buf)
1660 return sprintf(buf, "%d\n",
1661 channel_latency(channel,
1662 vmbus_connection.monitor_pages[1]));
1664 static VMBUS_CHAN_ATTR(latency, S_IRUGO, channel_latency_show, NULL);
1666 static ssize_t channel_interrupts_show(struct vmbus_channel *channel, char *buf)
1668 return sprintf(buf, "%llu\n", channel->interrupts);
1670 static VMBUS_CHAN_ATTR(interrupts, S_IRUGO, channel_interrupts_show, NULL);
1672 static ssize_t channel_events_show(struct vmbus_channel *channel, char *buf)
1674 return sprintf(buf, "%llu\n", channel->sig_events);
1676 static VMBUS_CHAN_ATTR(events, S_IRUGO, channel_events_show, NULL);
1678 static ssize_t channel_intr_in_full_show(struct vmbus_channel *channel,
1679 char *buf)
1681 return sprintf(buf, "%llu\n",
1682 (unsigned long long)channel->intr_in_full);
1684 static VMBUS_CHAN_ATTR(intr_in_full, 0444, channel_intr_in_full_show, NULL);
1686 static ssize_t channel_intr_out_empty_show(struct vmbus_channel *channel,
1687 char *buf)
1689 return sprintf(buf, "%llu\n",
1690 (unsigned long long)channel->intr_out_empty);
1692 static VMBUS_CHAN_ATTR(intr_out_empty, 0444, channel_intr_out_empty_show, NULL);
1694 static ssize_t channel_out_full_first_show(struct vmbus_channel *channel,
1695 char *buf)
1697 return sprintf(buf, "%llu\n",
1698 (unsigned long long)channel->out_full_first);
1700 static VMBUS_CHAN_ATTR(out_full_first, 0444, channel_out_full_first_show, NULL);
1702 static ssize_t channel_out_full_total_show(struct vmbus_channel *channel,
1703 char *buf)
1705 return sprintf(buf, "%llu\n",
1706 (unsigned long long)channel->out_full_total);
1708 static VMBUS_CHAN_ATTR(out_full_total, 0444, channel_out_full_total_show, NULL);
1710 static ssize_t subchannel_monitor_id_show(struct vmbus_channel *channel,
1711 char *buf)
1713 return sprintf(buf, "%u\n", channel->offermsg.monitorid);
1715 static VMBUS_CHAN_ATTR(monitor_id, S_IRUGO, subchannel_monitor_id_show, NULL);
1717 static ssize_t subchannel_id_show(struct vmbus_channel *channel,
1718 char *buf)
1720 return sprintf(buf, "%u\n",
1721 channel->offermsg.offer.sub_channel_index);
1723 static VMBUS_CHAN_ATTR_RO(subchannel_id);
1725 static struct attribute *vmbus_chan_attrs[] = {
1726 &chan_attr_out_mask.attr,
1727 &chan_attr_in_mask.attr,
1728 &chan_attr_read_avail.attr,
1729 &chan_attr_write_avail.attr,
1730 &chan_attr_cpu.attr,
1731 &chan_attr_pending.attr,
1732 &chan_attr_latency.attr,
1733 &chan_attr_interrupts.attr,
1734 &chan_attr_events.attr,
1735 &chan_attr_intr_in_full.attr,
1736 &chan_attr_intr_out_empty.attr,
1737 &chan_attr_out_full_first.attr,
1738 &chan_attr_out_full_total.attr,
1739 &chan_attr_monitor_id.attr,
1740 &chan_attr_subchannel_id.attr,
1741 NULL
1745 * Channel-level attribute_group callback function. Returns the permission for
1746 * each attribute, and returns 0 if an attribute is not visible.
1748 static umode_t vmbus_chan_attr_is_visible(struct kobject *kobj,
1749 struct attribute *attr, int idx)
1751 const struct vmbus_channel *channel =
1752 container_of(kobj, struct vmbus_channel, kobj);
1754 /* Hide the monitor attributes if the monitor mechanism is not used. */
1755 if (!channel->offermsg.monitor_allocated &&
1756 (attr == &chan_attr_pending.attr ||
1757 attr == &chan_attr_latency.attr ||
1758 attr == &chan_attr_monitor_id.attr))
1759 return 0;
1761 return attr->mode;
1764 static struct attribute_group vmbus_chan_group = {
1765 .attrs = vmbus_chan_attrs,
1766 .is_visible = vmbus_chan_attr_is_visible
1769 static struct kobj_type vmbus_chan_ktype = {
1770 .sysfs_ops = &vmbus_chan_sysfs_ops,
1771 .release = vmbus_chan_release,
1775 * vmbus_add_channel_kobj - setup a sub-directory under device/channels
1777 int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
1779 const struct device *device = &dev->device;
1780 struct kobject *kobj = &channel->kobj;
1781 u32 relid = channel->offermsg.child_relid;
1782 int ret;
1784 kobj->kset = dev->channels_kset;
1785 ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
1786 "%u", relid);
1787 if (ret)
1788 return ret;
1790 ret = sysfs_create_group(kobj, &vmbus_chan_group);
1792 if (ret) {
1794 * The calling functions' error handling paths will cleanup the
1795 * empty channel directory.
1797 dev_err(device, "Unable to set up channel sysfs files\n");
1798 return ret;
1801 kobject_uevent(kobj, KOBJ_ADD);
1803 return 0;
1807 * vmbus_remove_channel_attr_group - remove the channel's attribute group
1809 void vmbus_remove_channel_attr_group(struct vmbus_channel *channel)
1811 sysfs_remove_group(&channel->kobj, &vmbus_chan_group);
1815 * vmbus_device_create - Creates and registers a new child device
1816 * on the vmbus.
1818 struct hv_device *vmbus_device_create(const guid_t *type,
1819 const guid_t *instance,
1820 struct vmbus_channel *channel)
1822 struct hv_device *child_device_obj;
1824 child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
1825 if (!child_device_obj) {
1826 pr_err("Unable to allocate device object for child device\n");
1827 return NULL;
1830 child_device_obj->channel = channel;
1831 guid_copy(&child_device_obj->dev_type, type);
1832 guid_copy(&child_device_obj->dev_instance, instance);
1833 child_device_obj->vendor_id = 0x1414; /* MSFT vendor ID */
1835 return child_device_obj;
1839 * vmbus_device_register - Register the child device
1841 int vmbus_device_register(struct hv_device *child_device_obj)
1843 struct kobject *kobj = &child_device_obj->device.kobj;
1844 int ret;
1846 dev_set_name(&child_device_obj->device, "%pUl",
1847 child_device_obj->channel->offermsg.offer.if_instance.b);
1849 child_device_obj->device.bus = &hv_bus;
1850 child_device_obj->device.parent = &hv_acpi_dev->dev;
1851 child_device_obj->device.release = vmbus_device_release;
1854 * Register with the LDM. This will kick off the driver/device
1855 * binding...which will eventually call vmbus_match() and vmbus_probe()
1857 ret = device_register(&child_device_obj->device);
1858 if (ret) {
1859 pr_err("Unable to register child device\n");
1860 return ret;
1863 child_device_obj->channels_kset = kset_create_and_add("channels",
1864 NULL, kobj);
1865 if (!child_device_obj->channels_kset) {
1866 ret = -ENOMEM;
1867 goto err_dev_unregister;
1870 ret = vmbus_add_channel_kobj(child_device_obj,
1871 child_device_obj->channel);
1872 if (ret) {
1873 pr_err("Unable to register primary channeln");
1874 goto err_kset_unregister;
1876 hv_debug_add_dev_dir(child_device_obj);
1878 return 0;
1880 err_kset_unregister:
1881 kset_unregister(child_device_obj->channels_kset);
1883 err_dev_unregister:
1884 device_unregister(&child_device_obj->device);
1885 return ret;
1889 * vmbus_device_unregister - Remove the specified child device
1890 * from the vmbus.
1892 void vmbus_device_unregister(struct hv_device *device_obj)
1894 pr_debug("child device %s unregistered\n",
1895 dev_name(&device_obj->device));
1897 kset_unregister(device_obj->channels_kset);
1900 * Kick off the process of unregistering the device.
1901 * This will call vmbus_remove() and eventually vmbus_device_release()
1903 device_unregister(&device_obj->device);
1908 * VMBUS is an acpi enumerated device. Get the information we
1909 * need from DSDT.
1911 #define VTPM_BASE_ADDRESS 0xfed40000
1912 static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
1914 resource_size_t start = 0;
1915 resource_size_t end = 0;
1916 struct resource *new_res;
1917 struct resource **old_res = &hyperv_mmio;
1918 struct resource **prev_res = NULL;
1920 switch (res->type) {
1923 * "Address" descriptors are for bus windows. Ignore
1924 * "memory" descriptors, which are for registers on
1925 * devices.
1927 case ACPI_RESOURCE_TYPE_ADDRESS32:
1928 start = res->data.address32.address.minimum;
1929 end = res->data.address32.address.maximum;
1930 break;
1932 case ACPI_RESOURCE_TYPE_ADDRESS64:
1933 start = res->data.address64.address.minimum;
1934 end = res->data.address64.address.maximum;
1935 break;
1937 default:
1938 /* Unused resource type */
1939 return AE_OK;
1943 * Ignore ranges that are below 1MB, as they're not
1944 * necessary or useful here.
1946 if (end < 0x100000)
1947 return AE_OK;
1949 new_res = kzalloc(sizeof(*new_res), GFP_ATOMIC);
1950 if (!new_res)
1951 return AE_NO_MEMORY;
1953 /* If this range overlaps the virtual TPM, truncate it. */
1954 if (end > VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS)
1955 end = VTPM_BASE_ADDRESS;
1957 new_res->name = "hyperv mmio";
1958 new_res->flags = IORESOURCE_MEM;
1959 new_res->start = start;
1960 new_res->end = end;
1963 * If two ranges are adjacent, merge them.
1965 do {
1966 if (!*old_res) {
1967 *old_res = new_res;
1968 break;
1971 if (((*old_res)->end + 1) == new_res->start) {
1972 (*old_res)->end = new_res->end;
1973 kfree(new_res);
1974 break;
1977 if ((*old_res)->start == new_res->end + 1) {
1978 (*old_res)->start = new_res->start;
1979 kfree(new_res);
1980 break;
1983 if ((*old_res)->start > new_res->end) {
1984 new_res->sibling = *old_res;
1985 if (prev_res)
1986 (*prev_res)->sibling = new_res;
1987 *old_res = new_res;
1988 break;
1991 prev_res = old_res;
1992 old_res = &(*old_res)->sibling;
1994 } while (1);
1996 return AE_OK;
1999 static int vmbus_acpi_remove(struct acpi_device *device)
2001 struct resource *cur_res;
2002 struct resource *next_res;
2004 if (hyperv_mmio) {
2005 if (fb_mmio) {
2006 __release_region(hyperv_mmio, fb_mmio->start,
2007 resource_size(fb_mmio));
2008 fb_mmio = NULL;
2011 for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) {
2012 next_res = cur_res->sibling;
2013 kfree(cur_res);
2017 return 0;
2020 static void vmbus_reserve_fb(void)
2022 int size;
2024 * Make a claim for the frame buffer in the resource tree under the
2025 * first node, which will be the one below 4GB. The length seems to
2026 * be underreported, particularly in a Generation 1 VM. So start out
2027 * reserving a larger area and make it smaller until it succeeds.
2030 if (screen_info.lfb_base) {
2031 if (efi_enabled(EFI_BOOT))
2032 size = max_t(__u32, screen_info.lfb_size, 0x800000);
2033 else
2034 size = max_t(__u32, screen_info.lfb_size, 0x4000000);
2036 for (; !fb_mmio && (size >= 0x100000); size >>= 1) {
2037 fb_mmio = __request_region(hyperv_mmio,
2038 screen_info.lfb_base, size,
2039 fb_mmio_name, 0);
2045 * vmbus_allocate_mmio() - Pick a memory-mapped I/O range.
2046 * @new: If successful, supplied a pointer to the
2047 * allocated MMIO space.
2048 * @device_obj: Identifies the caller
2049 * @min: Minimum guest physical address of the
2050 * allocation
2051 * @max: Maximum guest physical address
2052 * @size: Size of the range to be allocated
2053 * @align: Alignment of the range to be allocated
2054 * @fb_overlap_ok: Whether this allocation can be allowed
2055 * to overlap the video frame buffer.
2057 * This function walks the resources granted to VMBus by the
2058 * _CRS object in the ACPI namespace underneath the parent
2059 * "bridge" whether that's a root PCI bus in the Generation 1
2060 * case or a Module Device in the Generation 2 case. It then
2061 * attempts to allocate from the global MMIO pool in a way that
2062 * matches the constraints supplied in these parameters and by
2063 * that _CRS.
2065 * Return: 0 on success, -errno on failure
2067 int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
2068 resource_size_t min, resource_size_t max,
2069 resource_size_t size, resource_size_t align,
2070 bool fb_overlap_ok)
2072 struct resource *iter, *shadow;
2073 resource_size_t range_min, range_max, start;
2074 const char *dev_n = dev_name(&device_obj->device);
2075 int retval;
2077 retval = -ENXIO;
2078 mutex_lock(&hyperv_mmio_lock);
2081 * If overlaps with frame buffers are allowed, then first attempt to
2082 * make the allocation from within the reserved region. Because it
2083 * is already reserved, no shadow allocation is necessary.
2085 if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) &&
2086 !(max < fb_mmio->start)) {
2088 range_min = fb_mmio->start;
2089 range_max = fb_mmio->end;
2090 start = (range_min + align - 1) & ~(align - 1);
2091 for (; start + size - 1 <= range_max; start += align) {
2092 *new = request_mem_region_exclusive(start, size, dev_n);
2093 if (*new) {
2094 retval = 0;
2095 goto exit;
2100 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
2101 if ((iter->start >= max) || (iter->end <= min))
2102 continue;
2104 range_min = iter->start;
2105 range_max = iter->end;
2106 start = (range_min + align - 1) & ~(align - 1);
2107 for (; start + size - 1 <= range_max; start += align) {
2108 shadow = __request_region(iter, start, size, NULL,
2109 IORESOURCE_BUSY);
2110 if (!shadow)
2111 continue;
2113 *new = request_mem_region_exclusive(start, size, dev_n);
2114 if (*new) {
2115 shadow->name = (char *)*new;
2116 retval = 0;
2117 goto exit;
2120 __release_region(iter, start, size);
2124 exit:
2125 mutex_unlock(&hyperv_mmio_lock);
2126 return retval;
2128 EXPORT_SYMBOL_GPL(vmbus_allocate_mmio);
2131 * vmbus_free_mmio() - Free a memory-mapped I/O range.
2132 * @start: Base address of region to release.
2133 * @size: Size of the range to be allocated
2135 * This function releases anything requested by
2136 * vmbus_mmio_allocate().
2138 void vmbus_free_mmio(resource_size_t start, resource_size_t size)
2140 struct resource *iter;
2142 mutex_lock(&hyperv_mmio_lock);
2143 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
2144 if ((iter->start >= start + size) || (iter->end <= start))
2145 continue;
2147 __release_region(iter, start, size);
2149 release_mem_region(start, size);
2150 mutex_unlock(&hyperv_mmio_lock);
2153 EXPORT_SYMBOL_GPL(vmbus_free_mmio);
2155 static int vmbus_acpi_add(struct acpi_device *device)
2157 acpi_status result;
2158 int ret_val = -ENODEV;
2159 struct acpi_device *ancestor;
2161 hv_acpi_dev = device;
2163 result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
2164 vmbus_walk_resources, NULL);
2166 if (ACPI_FAILURE(result))
2167 goto acpi_walk_err;
2169 * Some ancestor of the vmbus acpi device (Gen1 or Gen2
2170 * firmware) is the VMOD that has the mmio ranges. Get that.
2172 for (ancestor = device->parent; ancestor; ancestor = ancestor->parent) {
2173 result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS,
2174 vmbus_walk_resources, NULL);
2176 if (ACPI_FAILURE(result))
2177 continue;
2178 if (hyperv_mmio) {
2179 vmbus_reserve_fb();
2180 break;
2183 ret_val = 0;
2185 acpi_walk_err:
2186 complete(&probe_event);
2187 if (ret_val)
2188 vmbus_acpi_remove(device);
2189 return ret_val;
2192 #ifdef CONFIG_PM_SLEEP
2193 static int vmbus_bus_suspend(struct device *dev)
2195 struct vmbus_channel *channel, *sc;
2196 unsigned long flags;
2198 while (atomic_read(&vmbus_connection.offer_in_progress) != 0) {
2200 * We wait here until the completion of any channel
2201 * offers that are currently in progress.
2203 msleep(1);
2206 mutex_lock(&vmbus_connection.channel_mutex);
2207 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
2208 if (!is_hvsock_channel(channel))
2209 continue;
2211 vmbus_force_channel_rescinded(channel);
2213 mutex_unlock(&vmbus_connection.channel_mutex);
2216 * Wait until all the sub-channels and hv_sock channels have been
2217 * cleaned up. Sub-channels should be destroyed upon suspend, otherwise
2218 * they would conflict with the new sub-channels that will be created
2219 * in the resume path. hv_sock channels should also be destroyed, but
2220 * a hv_sock channel of an established hv_sock connection can not be
2221 * really destroyed since it may still be referenced by the userspace
2222 * application, so we just force the hv_sock channel to be rescinded
2223 * by vmbus_force_channel_rescinded(), and the userspace application
2224 * will thoroughly destroy the channel after hibernation.
2226 * Note: the counter nr_chan_close_on_suspend may never go above 0 if
2227 * the VM has no sub-channel and hv_sock channel, e.g. a 1-vCPU VM.
2229 if (atomic_read(&vmbus_connection.nr_chan_close_on_suspend) > 0)
2230 wait_for_completion(&vmbus_connection.ready_for_suspend_event);
2232 WARN_ON(atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) != 0);
2234 mutex_lock(&vmbus_connection.channel_mutex);
2236 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
2238 * Invalidate the field. Upon resume, vmbus_onoffer() will fix
2239 * up the field, and the other fields (if necessary).
2241 channel->offermsg.child_relid = INVALID_RELID;
2243 if (is_hvsock_channel(channel)) {
2244 if (!channel->rescind) {
2245 pr_err("hv_sock channel not rescinded!\n");
2246 WARN_ON_ONCE(1);
2248 continue;
2251 spin_lock_irqsave(&channel->lock, flags);
2252 list_for_each_entry(sc, &channel->sc_list, sc_list) {
2253 pr_err("Sub-channel not deleted!\n");
2254 WARN_ON_ONCE(1);
2256 spin_unlock_irqrestore(&channel->lock, flags);
2258 atomic_inc(&vmbus_connection.nr_chan_fixup_on_resume);
2261 mutex_unlock(&vmbus_connection.channel_mutex);
2263 vmbus_initiate_unload(false);
2265 /* Reset the event for the next resume. */
2266 reinit_completion(&vmbus_connection.ready_for_resume_event);
2268 return 0;
2271 static int vmbus_bus_resume(struct device *dev)
2273 struct vmbus_channel_msginfo *msginfo;
2274 size_t msgsize;
2275 int ret;
2278 * We only use the 'vmbus_proto_version', which was in use before
2279 * hibernation, to re-negotiate with the host.
2281 if (!vmbus_proto_version) {
2282 pr_err("Invalid proto version = 0x%x\n", vmbus_proto_version);
2283 return -EINVAL;
2286 msgsize = sizeof(*msginfo) +
2287 sizeof(struct vmbus_channel_initiate_contact);
2289 msginfo = kzalloc(msgsize, GFP_KERNEL);
2291 if (msginfo == NULL)
2292 return -ENOMEM;
2294 ret = vmbus_negotiate_version(msginfo, vmbus_proto_version);
2296 kfree(msginfo);
2298 if (ret != 0)
2299 return ret;
2301 WARN_ON(atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) == 0);
2303 vmbus_request_offers();
2305 wait_for_completion(&vmbus_connection.ready_for_resume_event);
2307 /* Reset the event for the next suspend. */
2308 reinit_completion(&vmbus_connection.ready_for_suspend_event);
2310 return 0;
2312 #else
2313 #define vmbus_bus_suspend NULL
2314 #define vmbus_bus_resume NULL
2315 #endif /* CONFIG_PM_SLEEP */
2317 static const struct acpi_device_id vmbus_acpi_device_ids[] = {
2318 {"VMBUS", 0},
2319 {"VMBus", 0},
2320 {"", 0},
2322 MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
2325 * Note: we must use the "no_irq" ops, otherwise hibernation can not work with
2326 * PCI device assignment, because "pci_dev_pm_ops" uses the "noirq" ops: in
2327 * the resume path, the pci "noirq" restore op runs before "non-noirq" op (see
2328 * resume_target_kernel() -> dpm_resume_start(), and hibernation_restore() ->
2329 * dpm_resume_end()). This means vmbus_bus_resume() and the pci-hyperv's
2330 * resume callback must also run via the "noirq" ops.
2332 * Set suspend_noirq/resume_noirq to NULL for Suspend-to-Idle: see the comment
2333 * earlier in this file before vmbus_pm.
2336 static const struct dev_pm_ops vmbus_bus_pm = {
2337 .suspend_noirq = NULL,
2338 .resume_noirq = NULL,
2339 .freeze_noirq = vmbus_bus_suspend,
2340 .thaw_noirq = vmbus_bus_resume,
2341 .poweroff_noirq = vmbus_bus_suspend,
2342 .restore_noirq = vmbus_bus_resume
2345 static struct acpi_driver vmbus_acpi_driver = {
2346 .name = "vmbus",
2347 .ids = vmbus_acpi_device_ids,
2348 .ops = {
2349 .add = vmbus_acpi_add,
2350 .remove = vmbus_acpi_remove,
2352 .drv.pm = &vmbus_bus_pm,
2355 static void hv_kexec_handler(void)
2357 hv_stimer_global_cleanup();
2358 vmbus_initiate_unload(false);
2359 /* Make sure conn_state is set as hv_synic_cleanup checks for it */
2360 mb();
2361 cpuhp_remove_state(hyperv_cpuhp_online);
2362 hyperv_cleanup();
2365 static void hv_crash_handler(struct pt_regs *regs)
2367 int cpu;
2369 vmbus_initiate_unload(true);
2371 * In crash handler we can't schedule synic cleanup for all CPUs,
2372 * doing the cleanup for current CPU only. This should be sufficient
2373 * for kdump.
2375 cpu = smp_processor_id();
2376 hv_stimer_cleanup(cpu);
2377 hv_synic_disable_regs(cpu);
2378 hyperv_cleanup();
2381 static int hv_synic_suspend(void)
2384 * When we reach here, all the non-boot CPUs have been offlined.
2385 * If we're in a legacy configuration where stimer Direct Mode is
2386 * not enabled, the stimers on the non-boot CPUs have been unbound
2387 * in hv_synic_cleanup() -> hv_stimer_legacy_cleanup() ->
2388 * hv_stimer_cleanup() -> clockevents_unbind_device().
2390 * hv_synic_suspend() only runs on CPU0 with interrupts disabled.
2391 * Here we do not call hv_stimer_legacy_cleanup() on CPU0 because:
2392 * 1) it's unnecessary as interrupts remain disabled between
2393 * syscore_suspend() and syscore_resume(): see create_image() and
2394 * resume_target_kernel()
2395 * 2) the stimer on CPU0 is automatically disabled later by
2396 * syscore_suspend() -> timekeeping_suspend() -> tick_suspend() -> ...
2397 * -> clockevents_shutdown() -> ... -> hv_ce_shutdown()
2398 * 3) a warning would be triggered if we call
2399 * clockevents_unbind_device(), which may sleep, in an
2400 * interrupts-disabled context.
2403 hv_synic_disable_regs(0);
2405 return 0;
2408 static void hv_synic_resume(void)
2410 hv_synic_enable_regs(0);
2413 * Note: we don't need to call hv_stimer_init(0), because the timer
2414 * on CPU0 is not unbound in hv_synic_suspend(), and the timer is
2415 * automatically re-enabled in timekeeping_resume().
2419 /* The callbacks run only on CPU0, with irqs_disabled. */
2420 static struct syscore_ops hv_synic_syscore_ops = {
2421 .suspend = hv_synic_suspend,
2422 .resume = hv_synic_resume,
2425 static int __init hv_acpi_init(void)
2427 int ret, t;
2429 if (!hv_is_hyperv_initialized())
2430 return -ENODEV;
2432 init_completion(&probe_event);
2435 * Get ACPI resources first.
2437 ret = acpi_bus_register_driver(&vmbus_acpi_driver);
2439 if (ret)
2440 return ret;
2442 t = wait_for_completion_timeout(&probe_event, 5*HZ);
2443 if (t == 0) {
2444 ret = -ETIMEDOUT;
2445 goto cleanup;
2447 hv_debug_init();
2449 ret = vmbus_bus_init();
2450 if (ret)
2451 goto cleanup;
2453 hv_setup_kexec_handler(hv_kexec_handler);
2454 hv_setup_crash_handler(hv_crash_handler);
2456 register_syscore_ops(&hv_synic_syscore_ops);
2458 return 0;
2460 cleanup:
2461 acpi_bus_unregister_driver(&vmbus_acpi_driver);
2462 hv_acpi_dev = NULL;
2463 return ret;
2466 static void __exit vmbus_exit(void)
2468 int cpu;
2470 unregister_syscore_ops(&hv_synic_syscore_ops);
2472 hv_remove_kexec_handler();
2473 hv_remove_crash_handler();
2474 vmbus_connection.conn_state = DISCONNECTED;
2475 hv_stimer_global_cleanup();
2476 vmbus_disconnect();
2477 hv_remove_vmbus_irq();
2478 for_each_online_cpu(cpu) {
2479 struct hv_per_cpu_context *hv_cpu
2480 = per_cpu_ptr(hv_context.cpu_context, cpu);
2482 tasklet_kill(&hv_cpu->msg_dpc);
2484 hv_debug_rm_all_dir();
2486 vmbus_free_channels();
2488 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
2489 kmsg_dump_unregister(&hv_kmsg_dumper);
2490 unregister_die_notifier(&hyperv_die_block);
2491 atomic_notifier_chain_unregister(&panic_notifier_list,
2492 &hyperv_panic_block);
2495 free_page((unsigned long)hv_panic_page);
2496 unregister_sysctl_table(hv_ctl_table_hdr);
2497 hv_ctl_table_hdr = NULL;
2498 bus_unregister(&hv_bus);
2500 cpuhp_remove_state(hyperv_cpuhp_online);
2501 hv_synic_free();
2502 acpi_bus_unregister_driver(&vmbus_acpi_driver);
2506 MODULE_LICENSE("GPL");
2507 MODULE_DESCRIPTION("Microsoft Hyper-V VMBus Driver");
2509 subsys_initcall(hv_acpi_init);
2510 module_exit(vmbus_exit);