treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / hv / vmbus_drv.c
blob029378c27421d1b30a168dbe25c1ba13c4c8de57
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/syscore_ops.h>
35 #include <clocksource/hyperv_timer.h>
36 #include "hyperv_vmbus.h"
38 struct vmbus_dynid {
39 struct list_head node;
40 struct hv_vmbus_device_id id;
43 static struct acpi_device *hv_acpi_dev;
45 static struct completion probe_event;
47 static int hyperv_cpuhp_online;
49 static void *hv_panic_page;
51 static int hyperv_panic_event(struct notifier_block *nb, unsigned long val,
52 void *args)
54 struct pt_regs *regs;
56 regs = current_pt_regs();
58 hyperv_report_panic(regs, val);
59 return NOTIFY_DONE;
62 static int hyperv_die_event(struct notifier_block *nb, unsigned long val,
63 void *args)
65 struct die_args *die = (struct die_args *)args;
66 struct pt_regs *regs = die->regs;
68 hyperv_report_panic(regs, val);
69 return NOTIFY_DONE;
72 static struct notifier_block hyperv_die_block = {
73 .notifier_call = hyperv_die_event,
75 static struct notifier_block hyperv_panic_block = {
76 .notifier_call = hyperv_panic_event,
79 static const char *fb_mmio_name = "fb_range";
80 static struct resource *fb_mmio;
81 static struct resource *hyperv_mmio;
82 static DEFINE_MUTEX(hyperv_mmio_lock);
84 static int vmbus_exists(void)
86 if (hv_acpi_dev == NULL)
87 return -ENODEV;
89 return 0;
92 #define VMBUS_ALIAS_LEN ((sizeof((struct hv_vmbus_device_id *)0)->guid) * 2)
93 static void print_alias_name(struct hv_device *hv_dev, char *alias_name)
95 int i;
96 for (i = 0; i < VMBUS_ALIAS_LEN; i += 2)
97 sprintf(&alias_name[i], "%02x", hv_dev->dev_type.b[i/2]);
100 static u8 channel_monitor_group(const struct vmbus_channel *channel)
102 return (u8)channel->offermsg.monitorid / 32;
105 static u8 channel_monitor_offset(const struct vmbus_channel *channel)
107 return (u8)channel->offermsg.monitorid % 32;
110 static u32 channel_pending(const struct vmbus_channel *channel,
111 const struct hv_monitor_page *monitor_page)
113 u8 monitor_group = channel_monitor_group(channel);
115 return monitor_page->trigger_group[monitor_group].pending;
118 static u32 channel_latency(const struct vmbus_channel *channel,
119 const struct hv_monitor_page *monitor_page)
121 u8 monitor_group = channel_monitor_group(channel);
122 u8 monitor_offset = channel_monitor_offset(channel);
124 return monitor_page->latency[monitor_group][monitor_offset];
127 static u32 channel_conn_id(struct vmbus_channel *channel,
128 struct hv_monitor_page *monitor_page)
130 u8 monitor_group = channel_monitor_group(channel);
131 u8 monitor_offset = channel_monitor_offset(channel);
132 return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
135 static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
136 char *buf)
138 struct hv_device *hv_dev = device_to_hv_device(dev);
140 if (!hv_dev->channel)
141 return -ENODEV;
142 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
144 static DEVICE_ATTR_RO(id);
146 static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
147 char *buf)
149 struct hv_device *hv_dev = device_to_hv_device(dev);
151 if (!hv_dev->channel)
152 return -ENODEV;
153 return sprintf(buf, "%d\n", hv_dev->channel->state);
155 static DEVICE_ATTR_RO(state);
157 static ssize_t monitor_id_show(struct device *dev,
158 struct device_attribute *dev_attr, char *buf)
160 struct hv_device *hv_dev = device_to_hv_device(dev);
162 if (!hv_dev->channel)
163 return -ENODEV;
164 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
166 static DEVICE_ATTR_RO(monitor_id);
168 static ssize_t class_id_show(struct device *dev,
169 struct device_attribute *dev_attr, char *buf)
171 struct hv_device *hv_dev = device_to_hv_device(dev);
173 if (!hv_dev->channel)
174 return -ENODEV;
175 return sprintf(buf, "{%pUl}\n",
176 hv_dev->channel->offermsg.offer.if_type.b);
178 static DEVICE_ATTR_RO(class_id);
180 static ssize_t device_id_show(struct device *dev,
181 struct device_attribute *dev_attr, char *buf)
183 struct hv_device *hv_dev = device_to_hv_device(dev);
185 if (!hv_dev->channel)
186 return -ENODEV;
187 return sprintf(buf, "{%pUl}\n",
188 hv_dev->channel->offermsg.offer.if_instance.b);
190 static DEVICE_ATTR_RO(device_id);
192 static ssize_t modalias_show(struct device *dev,
193 struct device_attribute *dev_attr, char *buf)
195 struct hv_device *hv_dev = device_to_hv_device(dev);
196 char alias_name[VMBUS_ALIAS_LEN + 1];
198 print_alias_name(hv_dev, alias_name);
199 return sprintf(buf, "vmbus:%s\n", alias_name);
201 static DEVICE_ATTR_RO(modalias);
203 #ifdef CONFIG_NUMA
204 static ssize_t numa_node_show(struct device *dev,
205 struct device_attribute *attr, char *buf)
207 struct hv_device *hv_dev = device_to_hv_device(dev);
209 if (!hv_dev->channel)
210 return -ENODEV;
212 return sprintf(buf, "%d\n", hv_dev->channel->numa_node);
214 static DEVICE_ATTR_RO(numa_node);
215 #endif
217 static ssize_t server_monitor_pending_show(struct device *dev,
218 struct device_attribute *dev_attr,
219 char *buf)
221 struct hv_device *hv_dev = device_to_hv_device(dev);
223 if (!hv_dev->channel)
224 return -ENODEV;
225 return sprintf(buf, "%d\n",
226 channel_pending(hv_dev->channel,
227 vmbus_connection.monitor_pages[0]));
229 static DEVICE_ATTR_RO(server_monitor_pending);
231 static ssize_t client_monitor_pending_show(struct device *dev,
232 struct device_attribute *dev_attr,
233 char *buf)
235 struct hv_device *hv_dev = device_to_hv_device(dev);
237 if (!hv_dev->channel)
238 return -ENODEV;
239 return sprintf(buf, "%d\n",
240 channel_pending(hv_dev->channel,
241 vmbus_connection.monitor_pages[1]));
243 static DEVICE_ATTR_RO(client_monitor_pending);
245 static ssize_t server_monitor_latency_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_latency(hv_dev->channel,
255 vmbus_connection.monitor_pages[0]));
257 static DEVICE_ATTR_RO(server_monitor_latency);
259 static ssize_t client_monitor_latency_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_latency(hv_dev->channel,
269 vmbus_connection.monitor_pages[1]));
271 static DEVICE_ATTR_RO(client_monitor_latency);
273 static ssize_t server_monitor_conn_id_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_conn_id(hv_dev->channel,
283 vmbus_connection.monitor_pages[0]));
285 static DEVICE_ATTR_RO(server_monitor_conn_id);
287 static ssize_t client_monitor_conn_id_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_conn_id(hv_dev->channel,
297 vmbus_connection.monitor_pages[1]));
299 static DEVICE_ATTR_RO(client_monitor_conn_id);
301 static ssize_t out_intr_mask_show(struct device *dev,
302 struct device_attribute *dev_attr, char *buf)
304 struct hv_device *hv_dev = device_to_hv_device(dev);
305 struct hv_ring_buffer_debug_info outbound;
306 int ret;
308 if (!hv_dev->channel)
309 return -ENODEV;
311 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
312 &outbound);
313 if (ret < 0)
314 return ret;
316 return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
318 static DEVICE_ATTR_RO(out_intr_mask);
320 static ssize_t out_read_index_show(struct device *dev,
321 struct device_attribute *dev_attr, char *buf)
323 struct hv_device *hv_dev = device_to_hv_device(dev);
324 struct hv_ring_buffer_debug_info outbound;
325 int ret;
327 if (!hv_dev->channel)
328 return -ENODEV;
330 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
331 &outbound);
332 if (ret < 0)
333 return ret;
334 return sprintf(buf, "%d\n", outbound.current_read_index);
336 static DEVICE_ATTR_RO(out_read_index);
338 static ssize_t out_write_index_show(struct device *dev,
339 struct device_attribute *dev_attr,
340 char *buf)
342 struct hv_device *hv_dev = device_to_hv_device(dev);
343 struct hv_ring_buffer_debug_info outbound;
344 int ret;
346 if (!hv_dev->channel)
347 return -ENODEV;
349 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
350 &outbound);
351 if (ret < 0)
352 return ret;
353 return sprintf(buf, "%d\n", outbound.current_write_index);
355 static DEVICE_ATTR_RO(out_write_index);
357 static ssize_t out_read_bytes_avail_show(struct device *dev,
358 struct device_attribute *dev_attr,
359 char *buf)
361 struct hv_device *hv_dev = device_to_hv_device(dev);
362 struct hv_ring_buffer_debug_info outbound;
363 int ret;
365 if (!hv_dev->channel)
366 return -ENODEV;
368 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
369 &outbound);
370 if (ret < 0)
371 return ret;
372 return sprintf(buf, "%d\n", outbound.bytes_avail_toread);
374 static DEVICE_ATTR_RO(out_read_bytes_avail);
376 static ssize_t out_write_bytes_avail_show(struct device *dev,
377 struct device_attribute *dev_attr,
378 char *buf)
380 struct hv_device *hv_dev = device_to_hv_device(dev);
381 struct hv_ring_buffer_debug_info outbound;
382 int ret;
384 if (!hv_dev->channel)
385 return -ENODEV;
387 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
388 &outbound);
389 if (ret < 0)
390 return ret;
391 return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
393 static DEVICE_ATTR_RO(out_write_bytes_avail);
395 static ssize_t in_intr_mask_show(struct device *dev,
396 struct device_attribute *dev_attr, char *buf)
398 struct hv_device *hv_dev = device_to_hv_device(dev);
399 struct hv_ring_buffer_debug_info inbound;
400 int ret;
402 if (!hv_dev->channel)
403 return -ENODEV;
405 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
406 if (ret < 0)
407 return ret;
409 return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
411 static DEVICE_ATTR_RO(in_intr_mask);
413 static ssize_t in_read_index_show(struct device *dev,
414 struct device_attribute *dev_attr, char *buf)
416 struct hv_device *hv_dev = device_to_hv_device(dev);
417 struct hv_ring_buffer_debug_info inbound;
418 int ret;
420 if (!hv_dev->channel)
421 return -ENODEV;
423 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
424 if (ret < 0)
425 return ret;
427 return sprintf(buf, "%d\n", inbound.current_read_index);
429 static DEVICE_ATTR_RO(in_read_index);
431 static ssize_t in_write_index_show(struct device *dev,
432 struct device_attribute *dev_attr, char *buf)
434 struct hv_device *hv_dev = device_to_hv_device(dev);
435 struct hv_ring_buffer_debug_info inbound;
436 int ret;
438 if (!hv_dev->channel)
439 return -ENODEV;
441 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
442 if (ret < 0)
443 return ret;
445 return sprintf(buf, "%d\n", inbound.current_write_index);
447 static DEVICE_ATTR_RO(in_write_index);
449 static ssize_t in_read_bytes_avail_show(struct device *dev,
450 struct device_attribute *dev_attr,
451 char *buf)
453 struct hv_device *hv_dev = device_to_hv_device(dev);
454 struct hv_ring_buffer_debug_info inbound;
455 int ret;
457 if (!hv_dev->channel)
458 return -ENODEV;
460 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
461 if (ret < 0)
462 return ret;
464 return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
466 static DEVICE_ATTR_RO(in_read_bytes_avail);
468 static ssize_t in_write_bytes_avail_show(struct device *dev,
469 struct device_attribute *dev_attr,
470 char *buf)
472 struct hv_device *hv_dev = device_to_hv_device(dev);
473 struct hv_ring_buffer_debug_info inbound;
474 int ret;
476 if (!hv_dev->channel)
477 return -ENODEV;
479 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
480 if (ret < 0)
481 return ret;
483 return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
485 static DEVICE_ATTR_RO(in_write_bytes_avail);
487 static ssize_t channel_vp_mapping_show(struct device *dev,
488 struct device_attribute *dev_attr,
489 char *buf)
491 struct hv_device *hv_dev = device_to_hv_device(dev);
492 struct vmbus_channel *channel = hv_dev->channel, *cur_sc;
493 unsigned long flags;
494 int buf_size = PAGE_SIZE, n_written, tot_written;
495 struct list_head *cur;
497 if (!channel)
498 return -ENODEV;
500 tot_written = snprintf(buf, buf_size, "%u:%u\n",
501 channel->offermsg.child_relid, channel->target_cpu);
503 spin_lock_irqsave(&channel->lock, flags);
505 list_for_each(cur, &channel->sc_list) {
506 if (tot_written >= buf_size - 1)
507 break;
509 cur_sc = list_entry(cur, struct vmbus_channel, sc_list);
510 n_written = scnprintf(buf + tot_written,
511 buf_size - tot_written,
512 "%u:%u\n",
513 cur_sc->offermsg.child_relid,
514 cur_sc->target_cpu);
515 tot_written += n_written;
518 spin_unlock_irqrestore(&channel->lock, flags);
520 return tot_written;
522 static DEVICE_ATTR_RO(channel_vp_mapping);
524 static ssize_t vendor_show(struct device *dev,
525 struct device_attribute *dev_attr,
526 char *buf)
528 struct hv_device *hv_dev = device_to_hv_device(dev);
529 return sprintf(buf, "0x%x\n", hv_dev->vendor_id);
531 static DEVICE_ATTR_RO(vendor);
533 static ssize_t device_show(struct device *dev,
534 struct device_attribute *dev_attr,
535 char *buf)
537 struct hv_device *hv_dev = device_to_hv_device(dev);
538 return sprintf(buf, "0x%x\n", hv_dev->device_id);
540 static DEVICE_ATTR_RO(device);
542 static ssize_t driver_override_store(struct device *dev,
543 struct device_attribute *attr,
544 const char *buf, size_t count)
546 struct hv_device *hv_dev = device_to_hv_device(dev);
547 char *driver_override, *old, *cp;
549 /* We need to keep extra room for a newline */
550 if (count >= (PAGE_SIZE - 1))
551 return -EINVAL;
553 driver_override = kstrndup(buf, count, GFP_KERNEL);
554 if (!driver_override)
555 return -ENOMEM;
557 cp = strchr(driver_override, '\n');
558 if (cp)
559 *cp = '\0';
561 device_lock(dev);
562 old = hv_dev->driver_override;
563 if (strlen(driver_override)) {
564 hv_dev->driver_override = driver_override;
565 } else {
566 kfree(driver_override);
567 hv_dev->driver_override = NULL;
569 device_unlock(dev);
571 kfree(old);
573 return count;
576 static ssize_t driver_override_show(struct device *dev,
577 struct device_attribute *attr, char *buf)
579 struct hv_device *hv_dev = device_to_hv_device(dev);
580 ssize_t len;
582 device_lock(dev);
583 len = snprintf(buf, PAGE_SIZE, "%s\n", hv_dev->driver_override);
584 device_unlock(dev);
586 return len;
588 static DEVICE_ATTR_RW(driver_override);
590 /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
591 static struct attribute *vmbus_dev_attrs[] = {
592 &dev_attr_id.attr,
593 &dev_attr_state.attr,
594 &dev_attr_monitor_id.attr,
595 &dev_attr_class_id.attr,
596 &dev_attr_device_id.attr,
597 &dev_attr_modalias.attr,
598 #ifdef CONFIG_NUMA
599 &dev_attr_numa_node.attr,
600 #endif
601 &dev_attr_server_monitor_pending.attr,
602 &dev_attr_client_monitor_pending.attr,
603 &dev_attr_server_monitor_latency.attr,
604 &dev_attr_client_monitor_latency.attr,
605 &dev_attr_server_monitor_conn_id.attr,
606 &dev_attr_client_monitor_conn_id.attr,
607 &dev_attr_out_intr_mask.attr,
608 &dev_attr_out_read_index.attr,
609 &dev_attr_out_write_index.attr,
610 &dev_attr_out_read_bytes_avail.attr,
611 &dev_attr_out_write_bytes_avail.attr,
612 &dev_attr_in_intr_mask.attr,
613 &dev_attr_in_read_index.attr,
614 &dev_attr_in_write_index.attr,
615 &dev_attr_in_read_bytes_avail.attr,
616 &dev_attr_in_write_bytes_avail.attr,
617 &dev_attr_channel_vp_mapping.attr,
618 &dev_attr_vendor.attr,
619 &dev_attr_device.attr,
620 &dev_attr_driver_override.attr,
621 NULL,
625 * Device-level attribute_group callback function. Returns the permission for
626 * each attribute, and returns 0 if an attribute is not visible.
628 static umode_t vmbus_dev_attr_is_visible(struct kobject *kobj,
629 struct attribute *attr, int idx)
631 struct device *dev = kobj_to_dev(kobj);
632 const struct hv_device *hv_dev = device_to_hv_device(dev);
634 /* Hide the monitor attributes if the monitor mechanism is not used. */
635 if (!hv_dev->channel->offermsg.monitor_allocated &&
636 (attr == &dev_attr_monitor_id.attr ||
637 attr == &dev_attr_server_monitor_pending.attr ||
638 attr == &dev_attr_client_monitor_pending.attr ||
639 attr == &dev_attr_server_monitor_latency.attr ||
640 attr == &dev_attr_client_monitor_latency.attr ||
641 attr == &dev_attr_server_monitor_conn_id.attr ||
642 attr == &dev_attr_client_monitor_conn_id.attr))
643 return 0;
645 return attr->mode;
648 static const struct attribute_group vmbus_dev_group = {
649 .attrs = vmbus_dev_attrs,
650 .is_visible = vmbus_dev_attr_is_visible
652 __ATTRIBUTE_GROUPS(vmbus_dev);
655 * vmbus_uevent - add uevent for our device
657 * This routine is invoked when a device is added or removed on the vmbus to
658 * generate a uevent to udev in the userspace. The udev will then look at its
659 * rule and the uevent generated here to load the appropriate driver
661 * The alias string will be of the form vmbus:guid where guid is the string
662 * representation of the device guid (each byte of the guid will be
663 * represented with two hex characters.
665 static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
667 struct hv_device *dev = device_to_hv_device(device);
668 int ret;
669 char alias_name[VMBUS_ALIAS_LEN + 1];
671 print_alias_name(dev, alias_name);
672 ret = add_uevent_var(env, "MODALIAS=vmbus:%s", alias_name);
673 return ret;
676 static const struct hv_vmbus_device_id *
677 hv_vmbus_dev_match(const struct hv_vmbus_device_id *id, const guid_t *guid)
679 if (id == NULL)
680 return NULL; /* empty device table */
682 for (; !guid_is_null(&id->guid); id++)
683 if (guid_equal(&id->guid, guid))
684 return id;
686 return NULL;
689 static const struct hv_vmbus_device_id *
690 hv_vmbus_dynid_match(struct hv_driver *drv, const guid_t *guid)
692 const struct hv_vmbus_device_id *id = NULL;
693 struct vmbus_dynid *dynid;
695 spin_lock(&drv->dynids.lock);
696 list_for_each_entry(dynid, &drv->dynids.list, node) {
697 if (guid_equal(&dynid->id.guid, guid)) {
698 id = &dynid->id;
699 break;
702 spin_unlock(&drv->dynids.lock);
704 return id;
707 static const struct hv_vmbus_device_id vmbus_device_null;
710 * Return a matching hv_vmbus_device_id pointer.
711 * If there is no match, return NULL.
713 static const struct hv_vmbus_device_id *hv_vmbus_get_id(struct hv_driver *drv,
714 struct hv_device *dev)
716 const guid_t *guid = &dev->dev_type;
717 const struct hv_vmbus_device_id *id;
719 /* When driver_override is set, only bind to the matching driver */
720 if (dev->driver_override && strcmp(dev->driver_override, drv->name))
721 return NULL;
723 /* Look at the dynamic ids first, before the static ones */
724 id = hv_vmbus_dynid_match(drv, guid);
725 if (!id)
726 id = hv_vmbus_dev_match(drv->id_table, guid);
728 /* driver_override will always match, send a dummy id */
729 if (!id && dev->driver_override)
730 id = &vmbus_device_null;
732 return id;
735 /* vmbus_add_dynid - add a new device ID to this driver and re-probe devices */
736 static int vmbus_add_dynid(struct hv_driver *drv, guid_t *guid)
738 struct vmbus_dynid *dynid;
740 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
741 if (!dynid)
742 return -ENOMEM;
744 dynid->id.guid = *guid;
746 spin_lock(&drv->dynids.lock);
747 list_add_tail(&dynid->node, &drv->dynids.list);
748 spin_unlock(&drv->dynids.lock);
750 return driver_attach(&drv->driver);
753 static void vmbus_free_dynids(struct hv_driver *drv)
755 struct vmbus_dynid *dynid, *n;
757 spin_lock(&drv->dynids.lock);
758 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
759 list_del(&dynid->node);
760 kfree(dynid);
762 spin_unlock(&drv->dynids.lock);
766 * store_new_id - sysfs frontend to vmbus_add_dynid()
768 * Allow GUIDs to be added to an existing driver via sysfs.
770 static ssize_t new_id_store(struct device_driver *driver, const char *buf,
771 size_t count)
773 struct hv_driver *drv = drv_to_hv_drv(driver);
774 guid_t guid;
775 ssize_t retval;
777 retval = guid_parse(buf, &guid);
778 if (retval)
779 return retval;
781 if (hv_vmbus_dynid_match(drv, &guid))
782 return -EEXIST;
784 retval = vmbus_add_dynid(drv, &guid);
785 if (retval)
786 return retval;
787 return count;
789 static DRIVER_ATTR_WO(new_id);
792 * store_remove_id - remove a PCI device ID from this driver
794 * Removes a dynamic pci device ID to this driver.
796 static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
797 size_t count)
799 struct hv_driver *drv = drv_to_hv_drv(driver);
800 struct vmbus_dynid *dynid, *n;
801 guid_t guid;
802 ssize_t retval;
804 retval = guid_parse(buf, &guid);
805 if (retval)
806 return retval;
808 retval = -ENODEV;
809 spin_lock(&drv->dynids.lock);
810 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
811 struct hv_vmbus_device_id *id = &dynid->id;
813 if (guid_equal(&id->guid, &guid)) {
814 list_del(&dynid->node);
815 kfree(dynid);
816 retval = count;
817 break;
820 spin_unlock(&drv->dynids.lock);
822 return retval;
824 static DRIVER_ATTR_WO(remove_id);
826 static struct attribute *vmbus_drv_attrs[] = {
827 &driver_attr_new_id.attr,
828 &driver_attr_remove_id.attr,
829 NULL,
831 ATTRIBUTE_GROUPS(vmbus_drv);
835 * vmbus_match - Attempt to match the specified device to the specified driver
837 static int vmbus_match(struct device *device, struct device_driver *driver)
839 struct hv_driver *drv = drv_to_hv_drv(driver);
840 struct hv_device *hv_dev = device_to_hv_device(device);
842 /* The hv_sock driver handles all hv_sock offers. */
843 if (is_hvsock_channel(hv_dev->channel))
844 return drv->hvsock;
846 if (hv_vmbus_get_id(drv, hv_dev))
847 return 1;
849 return 0;
853 * vmbus_probe - Add the new vmbus's child device
855 static int vmbus_probe(struct device *child_device)
857 int ret = 0;
858 struct hv_driver *drv =
859 drv_to_hv_drv(child_device->driver);
860 struct hv_device *dev = device_to_hv_device(child_device);
861 const struct hv_vmbus_device_id *dev_id;
863 dev_id = hv_vmbus_get_id(drv, dev);
864 if (drv->probe) {
865 ret = drv->probe(dev, dev_id);
866 if (ret != 0)
867 pr_err("probe failed for device %s (%d)\n",
868 dev_name(child_device), ret);
870 } else {
871 pr_err("probe not set for driver %s\n",
872 dev_name(child_device));
873 ret = -ENODEV;
875 return ret;
879 * vmbus_remove - Remove a vmbus device
881 static int vmbus_remove(struct device *child_device)
883 struct hv_driver *drv;
884 struct hv_device *dev = device_to_hv_device(child_device);
886 if (child_device->driver) {
887 drv = drv_to_hv_drv(child_device->driver);
888 if (drv->remove)
889 drv->remove(dev);
892 return 0;
897 * vmbus_shutdown - Shutdown a vmbus device
899 static void vmbus_shutdown(struct device *child_device)
901 struct hv_driver *drv;
902 struct hv_device *dev = device_to_hv_device(child_device);
905 /* The device may not be attached yet */
906 if (!child_device->driver)
907 return;
909 drv = drv_to_hv_drv(child_device->driver);
911 if (drv->shutdown)
912 drv->shutdown(dev);
915 #ifdef CONFIG_PM_SLEEP
917 * vmbus_suspend - Suspend a vmbus device
919 static int vmbus_suspend(struct device *child_device)
921 struct hv_driver *drv;
922 struct hv_device *dev = device_to_hv_device(child_device);
924 /* The device may not be attached yet */
925 if (!child_device->driver)
926 return 0;
928 drv = drv_to_hv_drv(child_device->driver);
929 if (!drv->suspend)
930 return -EOPNOTSUPP;
932 return drv->suspend(dev);
936 * vmbus_resume - Resume a vmbus device
938 static int vmbus_resume(struct device *child_device)
940 struct hv_driver *drv;
941 struct hv_device *dev = device_to_hv_device(child_device);
943 /* The device may not be attached yet */
944 if (!child_device->driver)
945 return 0;
947 drv = drv_to_hv_drv(child_device->driver);
948 if (!drv->resume)
949 return -EOPNOTSUPP;
951 return drv->resume(dev);
953 #endif /* CONFIG_PM_SLEEP */
956 * vmbus_device_release - Final callback release of the vmbus child device
958 static void vmbus_device_release(struct device *device)
960 struct hv_device *hv_dev = device_to_hv_device(device);
961 struct vmbus_channel *channel = hv_dev->channel;
963 hv_debug_rm_dev_dir(hv_dev);
965 mutex_lock(&vmbus_connection.channel_mutex);
966 hv_process_channel_removal(channel);
967 mutex_unlock(&vmbus_connection.channel_mutex);
968 kfree(hv_dev);
972 * Note: we must use SET_NOIRQ_SYSTEM_SLEEP_PM_OPS rather than
973 * SET_SYSTEM_SLEEP_PM_OPS: see the comment before vmbus_bus_pm.
975 static const struct dev_pm_ops vmbus_pm = {
976 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(vmbus_suspend, vmbus_resume)
979 /* The one and only one */
980 static struct bus_type hv_bus = {
981 .name = "vmbus",
982 .match = vmbus_match,
983 .shutdown = vmbus_shutdown,
984 .remove = vmbus_remove,
985 .probe = vmbus_probe,
986 .uevent = vmbus_uevent,
987 .dev_groups = vmbus_dev_groups,
988 .drv_groups = vmbus_drv_groups,
989 .pm = &vmbus_pm,
992 struct onmessage_work_context {
993 struct work_struct work;
994 struct hv_message msg;
997 static void vmbus_onmessage_work(struct work_struct *work)
999 struct onmessage_work_context *ctx;
1001 /* Do not process messages if we're in DISCONNECTED state */
1002 if (vmbus_connection.conn_state == DISCONNECTED)
1003 return;
1005 ctx = container_of(work, struct onmessage_work_context,
1006 work);
1007 vmbus_onmessage(&ctx->msg);
1008 kfree(ctx);
1011 void vmbus_on_msg_dpc(unsigned long data)
1013 struct hv_per_cpu_context *hv_cpu = (void *)data;
1014 void *page_addr = hv_cpu->synic_message_page;
1015 struct hv_message *msg = (struct hv_message *)page_addr +
1016 VMBUS_MESSAGE_SINT;
1017 struct vmbus_channel_message_header *hdr;
1018 const struct vmbus_channel_message_table_entry *entry;
1019 struct onmessage_work_context *ctx;
1020 u32 message_type = msg->header.message_type;
1022 if (message_type == HVMSG_NONE)
1023 /* no msg */
1024 return;
1026 hdr = (struct vmbus_channel_message_header *)msg->u.payload;
1028 trace_vmbus_on_msg_dpc(hdr);
1030 if (hdr->msgtype >= CHANNELMSG_COUNT) {
1031 WARN_ONCE(1, "unknown msgtype=%d\n", hdr->msgtype);
1032 goto msg_handled;
1035 entry = &channel_message_table[hdr->msgtype];
1037 if (!entry->message_handler)
1038 goto msg_handled;
1040 if (entry->handler_type == VMHT_BLOCKING) {
1041 ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
1042 if (ctx == NULL)
1043 return;
1045 INIT_WORK(&ctx->work, vmbus_onmessage_work);
1046 memcpy(&ctx->msg, msg, sizeof(*msg));
1049 * The host can generate a rescind message while we
1050 * may still be handling the original offer. We deal with
1051 * this condition by ensuring the processing is done on the
1052 * same CPU.
1054 switch (hdr->msgtype) {
1055 case CHANNELMSG_RESCIND_CHANNELOFFER:
1057 * If we are handling the rescind message;
1058 * schedule the work on the global work queue.
1060 schedule_work_on(vmbus_connection.connect_cpu,
1061 &ctx->work);
1062 break;
1064 case CHANNELMSG_OFFERCHANNEL:
1065 atomic_inc(&vmbus_connection.offer_in_progress);
1066 queue_work_on(vmbus_connection.connect_cpu,
1067 vmbus_connection.work_queue,
1068 &ctx->work);
1069 break;
1071 default:
1072 queue_work(vmbus_connection.work_queue, &ctx->work);
1074 } else
1075 entry->message_handler(hdr);
1077 msg_handled:
1078 vmbus_signal_eom(msg, message_type);
1081 #ifdef CONFIG_PM_SLEEP
1083 * Fake RESCIND_CHANNEL messages to clean up hv_sock channels by force for
1084 * hibernation, because hv_sock connections can not persist across hibernation.
1086 static void vmbus_force_channel_rescinded(struct vmbus_channel *channel)
1088 struct onmessage_work_context *ctx;
1089 struct vmbus_channel_rescind_offer *rescind;
1091 WARN_ON(!is_hvsock_channel(channel));
1094 * sizeof(*ctx) is small and the allocation should really not fail,
1095 * otherwise the state of the hv_sock connections ends up in limbo.
1097 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL | __GFP_NOFAIL);
1100 * So far, these are not really used by Linux. Just set them to the
1101 * reasonable values conforming to the definitions of the fields.
1103 ctx->msg.header.message_type = 1;
1104 ctx->msg.header.payload_size = sizeof(*rescind);
1106 /* These values are actually used by Linux. */
1107 rescind = (struct vmbus_channel_rescind_offer *)ctx->msg.u.payload;
1108 rescind->header.msgtype = CHANNELMSG_RESCIND_CHANNELOFFER;
1109 rescind->child_relid = channel->offermsg.child_relid;
1111 INIT_WORK(&ctx->work, vmbus_onmessage_work);
1113 queue_work_on(vmbus_connection.connect_cpu,
1114 vmbus_connection.work_queue,
1115 &ctx->work);
1117 #endif /* CONFIG_PM_SLEEP */
1120 * Direct callback for channels using other deferred processing
1122 static void vmbus_channel_isr(struct vmbus_channel *channel)
1124 void (*callback_fn)(void *);
1126 callback_fn = READ_ONCE(channel->onchannel_callback);
1127 if (likely(callback_fn != NULL))
1128 (*callback_fn)(channel->channel_callback_context);
1132 * Schedule all channels with events pending
1134 static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu)
1136 unsigned long *recv_int_page;
1137 u32 maxbits, relid;
1139 if (vmbus_proto_version < VERSION_WIN8) {
1140 maxbits = MAX_NUM_CHANNELS_SUPPORTED;
1141 recv_int_page = vmbus_connection.recv_int_page;
1142 } else {
1144 * When the host is win8 and beyond, the event page
1145 * can be directly checked to get the id of the channel
1146 * that has the interrupt pending.
1148 void *page_addr = hv_cpu->synic_event_page;
1149 union hv_synic_event_flags *event
1150 = (union hv_synic_event_flags *)page_addr +
1151 VMBUS_MESSAGE_SINT;
1153 maxbits = HV_EVENT_FLAGS_COUNT;
1154 recv_int_page = event->flags;
1157 if (unlikely(!recv_int_page))
1158 return;
1160 for_each_set_bit(relid, recv_int_page, maxbits) {
1161 struct vmbus_channel *channel;
1163 if (!sync_test_and_clear_bit(relid, recv_int_page))
1164 continue;
1166 /* Special case - vmbus channel protocol msg */
1167 if (relid == 0)
1168 continue;
1170 rcu_read_lock();
1172 /* Find channel based on relid */
1173 list_for_each_entry_rcu(channel, &hv_cpu->chan_list, percpu_list) {
1174 if (channel->offermsg.child_relid != relid)
1175 continue;
1177 if (channel->rescind)
1178 continue;
1180 trace_vmbus_chan_sched(channel);
1182 ++channel->interrupts;
1184 switch (channel->callback_mode) {
1185 case HV_CALL_ISR:
1186 vmbus_channel_isr(channel);
1187 break;
1189 case HV_CALL_BATCHED:
1190 hv_begin_read(&channel->inbound);
1191 /* fallthrough */
1192 case HV_CALL_DIRECT:
1193 tasklet_schedule(&channel->callback_event);
1197 rcu_read_unlock();
1201 static void vmbus_isr(void)
1203 struct hv_per_cpu_context *hv_cpu
1204 = this_cpu_ptr(hv_context.cpu_context);
1205 void *page_addr = hv_cpu->synic_event_page;
1206 struct hv_message *msg;
1207 union hv_synic_event_flags *event;
1208 bool handled = false;
1210 if (unlikely(page_addr == NULL))
1211 return;
1213 event = (union hv_synic_event_flags *)page_addr +
1214 VMBUS_MESSAGE_SINT;
1216 * Check for events before checking for messages. This is the order
1217 * in which events and messages are checked in Windows guests on
1218 * Hyper-V, and the Windows team suggested we do the same.
1221 if ((vmbus_proto_version == VERSION_WS2008) ||
1222 (vmbus_proto_version == VERSION_WIN7)) {
1224 /* Since we are a child, we only need to check bit 0 */
1225 if (sync_test_and_clear_bit(0, event->flags))
1226 handled = true;
1227 } else {
1229 * Our host is win8 or above. The signaling mechanism
1230 * has changed and we can directly look at the event page.
1231 * If bit n is set then we have an interrup on the channel
1232 * whose id is n.
1234 handled = true;
1237 if (handled)
1238 vmbus_chan_sched(hv_cpu);
1240 page_addr = hv_cpu->synic_message_page;
1241 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
1243 /* Check if there are actual msgs to be processed */
1244 if (msg->header.message_type != HVMSG_NONE) {
1245 if (msg->header.message_type == HVMSG_TIMER_EXPIRED) {
1246 hv_stimer0_isr();
1247 vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED);
1248 } else
1249 tasklet_schedule(&hv_cpu->msg_dpc);
1252 add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0);
1256 * Boolean to control whether to report panic messages over Hyper-V.
1258 * It can be set via /proc/sys/kernel/hyperv/record_panic_msg
1260 static int sysctl_record_panic_msg = 1;
1263 * Callback from kmsg_dump. Grab as much as possible from the end of the kmsg
1264 * buffer and call into Hyper-V to transfer the data.
1266 static void hv_kmsg_dump(struct kmsg_dumper *dumper,
1267 enum kmsg_dump_reason reason)
1269 size_t bytes_written;
1270 phys_addr_t panic_pa;
1272 /* We are only interested in panics. */
1273 if ((reason != KMSG_DUMP_PANIC) || (!sysctl_record_panic_msg))
1274 return;
1276 panic_pa = virt_to_phys(hv_panic_page);
1279 * Write dump contents to the page. No need to synchronize; panic should
1280 * be single-threaded.
1282 kmsg_dump_get_buffer(dumper, true, hv_panic_page, HV_HYP_PAGE_SIZE,
1283 &bytes_written);
1284 if (bytes_written)
1285 hyperv_report_panic_msg(panic_pa, bytes_written);
1288 static struct kmsg_dumper hv_kmsg_dumper = {
1289 .dump = hv_kmsg_dump,
1292 static struct ctl_table_header *hv_ctl_table_hdr;
1295 * sysctl option to allow the user to control whether kmsg data should be
1296 * reported to Hyper-V on panic.
1298 static struct ctl_table hv_ctl_table[] = {
1300 .procname = "hyperv_record_panic_msg",
1301 .data = &sysctl_record_panic_msg,
1302 .maxlen = sizeof(int),
1303 .mode = 0644,
1304 .proc_handler = proc_dointvec_minmax,
1305 .extra1 = SYSCTL_ZERO,
1306 .extra2 = SYSCTL_ONE
1311 static struct ctl_table hv_root_table[] = {
1313 .procname = "kernel",
1314 .mode = 0555,
1315 .child = hv_ctl_table
1321 * vmbus_bus_init -Main vmbus driver initialization routine.
1323 * Here, we
1324 * - initialize the vmbus driver context
1325 * - invoke the vmbus hv main init routine
1326 * - retrieve the channel offers
1328 static int vmbus_bus_init(void)
1330 int ret;
1332 /* Hypervisor initialization...setup hypercall page..etc */
1333 ret = hv_init();
1334 if (ret != 0) {
1335 pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
1336 return ret;
1339 ret = bus_register(&hv_bus);
1340 if (ret)
1341 return ret;
1343 hv_setup_vmbus_irq(vmbus_isr);
1345 ret = hv_synic_alloc();
1346 if (ret)
1347 goto err_alloc;
1350 * Initialize the per-cpu interrupt state and stimer state.
1351 * Then connect to the host.
1353 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hyperv/vmbus:online",
1354 hv_synic_init, hv_synic_cleanup);
1355 if (ret < 0)
1356 goto err_cpuhp;
1357 hyperv_cpuhp_online = ret;
1359 ret = vmbus_connect();
1360 if (ret)
1361 goto err_connect;
1364 * Only register if the crash MSRs are available
1366 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
1367 u64 hyperv_crash_ctl;
1369 * Sysctl registration is not fatal, since by default
1370 * reporting is enabled.
1372 hv_ctl_table_hdr = register_sysctl_table(hv_root_table);
1373 if (!hv_ctl_table_hdr)
1374 pr_err("Hyper-V: sysctl table register error");
1377 * Register for panic kmsg callback only if the right
1378 * capability is supported by the hypervisor.
1380 hv_get_crash_ctl(hyperv_crash_ctl);
1381 if (hyperv_crash_ctl & HV_CRASH_CTL_CRASH_NOTIFY_MSG) {
1382 hv_panic_page = (void *)hv_alloc_hyperv_zeroed_page();
1383 if (hv_panic_page) {
1384 ret = kmsg_dump_register(&hv_kmsg_dumper);
1385 if (ret)
1386 pr_err("Hyper-V: kmsg dump register "
1387 "error 0x%x\n", ret);
1388 } else
1389 pr_err("Hyper-V: panic message page memory "
1390 "allocation failed");
1393 register_die_notifier(&hyperv_die_block);
1394 atomic_notifier_chain_register(&panic_notifier_list,
1395 &hyperv_panic_block);
1398 vmbus_request_offers();
1400 return 0;
1402 err_connect:
1403 cpuhp_remove_state(hyperv_cpuhp_online);
1404 err_cpuhp:
1405 hv_synic_free();
1406 err_alloc:
1407 hv_remove_vmbus_irq();
1409 bus_unregister(&hv_bus);
1410 hv_free_hyperv_page((unsigned long)hv_panic_page);
1411 unregister_sysctl_table(hv_ctl_table_hdr);
1412 hv_ctl_table_hdr = NULL;
1413 return ret;
1417 * __vmbus_child_driver_register() - Register a vmbus's driver
1418 * @hv_driver: Pointer to driver structure you want to register
1419 * @owner: owner module of the drv
1420 * @mod_name: module name string
1422 * Registers the given driver with Linux through the 'driver_register()' call
1423 * and sets up the hyper-v vmbus handling for this driver.
1424 * It will return the state of the 'driver_register()' call.
1427 int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
1429 int ret;
1431 pr_info("registering driver %s\n", hv_driver->name);
1433 ret = vmbus_exists();
1434 if (ret < 0)
1435 return ret;
1437 hv_driver->driver.name = hv_driver->name;
1438 hv_driver->driver.owner = owner;
1439 hv_driver->driver.mod_name = mod_name;
1440 hv_driver->driver.bus = &hv_bus;
1442 spin_lock_init(&hv_driver->dynids.lock);
1443 INIT_LIST_HEAD(&hv_driver->dynids.list);
1445 ret = driver_register(&hv_driver->driver);
1447 return ret;
1449 EXPORT_SYMBOL_GPL(__vmbus_driver_register);
1452 * vmbus_driver_unregister() - Unregister a vmbus's driver
1453 * @hv_driver: Pointer to driver structure you want to
1454 * un-register
1456 * Un-register the given driver that was previous registered with a call to
1457 * vmbus_driver_register()
1459 void vmbus_driver_unregister(struct hv_driver *hv_driver)
1461 pr_info("unregistering driver %s\n", hv_driver->name);
1463 if (!vmbus_exists()) {
1464 driver_unregister(&hv_driver->driver);
1465 vmbus_free_dynids(hv_driver);
1468 EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
1472 * Called when last reference to channel is gone.
1474 static void vmbus_chan_release(struct kobject *kobj)
1476 struct vmbus_channel *channel
1477 = container_of(kobj, struct vmbus_channel, kobj);
1479 kfree_rcu(channel, rcu);
1482 struct vmbus_chan_attribute {
1483 struct attribute attr;
1484 ssize_t (*show)(struct vmbus_channel *chan, char *buf);
1485 ssize_t (*store)(struct vmbus_channel *chan,
1486 const char *buf, size_t count);
1488 #define VMBUS_CHAN_ATTR(_name, _mode, _show, _store) \
1489 struct vmbus_chan_attribute chan_attr_##_name \
1490 = __ATTR(_name, _mode, _show, _store)
1491 #define VMBUS_CHAN_ATTR_RW(_name) \
1492 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RW(_name)
1493 #define VMBUS_CHAN_ATTR_RO(_name) \
1494 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RO(_name)
1495 #define VMBUS_CHAN_ATTR_WO(_name) \
1496 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_WO(_name)
1498 static ssize_t vmbus_chan_attr_show(struct kobject *kobj,
1499 struct attribute *attr, char *buf)
1501 const struct vmbus_chan_attribute *attribute
1502 = container_of(attr, struct vmbus_chan_attribute, attr);
1503 struct vmbus_channel *chan
1504 = container_of(kobj, struct vmbus_channel, kobj);
1506 if (!attribute->show)
1507 return -EIO;
1509 return attribute->show(chan, buf);
1512 static const struct sysfs_ops vmbus_chan_sysfs_ops = {
1513 .show = vmbus_chan_attr_show,
1516 static ssize_t out_mask_show(struct vmbus_channel *channel, char *buf)
1518 struct hv_ring_buffer_info *rbi = &channel->outbound;
1519 ssize_t ret;
1521 mutex_lock(&rbi->ring_buffer_mutex);
1522 if (!rbi->ring_buffer) {
1523 mutex_unlock(&rbi->ring_buffer_mutex);
1524 return -EINVAL;
1527 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1528 mutex_unlock(&rbi->ring_buffer_mutex);
1529 return ret;
1531 static VMBUS_CHAN_ATTR_RO(out_mask);
1533 static ssize_t in_mask_show(struct vmbus_channel *channel, char *buf)
1535 struct hv_ring_buffer_info *rbi = &channel->inbound;
1536 ssize_t ret;
1538 mutex_lock(&rbi->ring_buffer_mutex);
1539 if (!rbi->ring_buffer) {
1540 mutex_unlock(&rbi->ring_buffer_mutex);
1541 return -EINVAL;
1544 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1545 mutex_unlock(&rbi->ring_buffer_mutex);
1546 return ret;
1548 static VMBUS_CHAN_ATTR_RO(in_mask);
1550 static ssize_t read_avail_show(struct vmbus_channel *channel, char *buf)
1552 struct hv_ring_buffer_info *rbi = &channel->inbound;
1553 ssize_t ret;
1555 mutex_lock(&rbi->ring_buffer_mutex);
1556 if (!rbi->ring_buffer) {
1557 mutex_unlock(&rbi->ring_buffer_mutex);
1558 return -EINVAL;
1561 ret = sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi));
1562 mutex_unlock(&rbi->ring_buffer_mutex);
1563 return ret;
1565 static VMBUS_CHAN_ATTR_RO(read_avail);
1567 static ssize_t write_avail_show(struct vmbus_channel *channel, char *buf)
1569 struct hv_ring_buffer_info *rbi = &channel->outbound;
1570 ssize_t ret;
1572 mutex_lock(&rbi->ring_buffer_mutex);
1573 if (!rbi->ring_buffer) {
1574 mutex_unlock(&rbi->ring_buffer_mutex);
1575 return -EINVAL;
1578 ret = sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi));
1579 mutex_unlock(&rbi->ring_buffer_mutex);
1580 return ret;
1582 static VMBUS_CHAN_ATTR_RO(write_avail);
1584 static ssize_t show_target_cpu(struct vmbus_channel *channel, char *buf)
1586 return sprintf(buf, "%u\n", channel->target_cpu);
1588 static VMBUS_CHAN_ATTR(cpu, S_IRUGO, show_target_cpu, NULL);
1590 static ssize_t channel_pending_show(struct vmbus_channel *channel,
1591 char *buf)
1593 return sprintf(buf, "%d\n",
1594 channel_pending(channel,
1595 vmbus_connection.monitor_pages[1]));
1597 static VMBUS_CHAN_ATTR(pending, S_IRUGO, channel_pending_show, NULL);
1599 static ssize_t channel_latency_show(struct vmbus_channel *channel,
1600 char *buf)
1602 return sprintf(buf, "%d\n",
1603 channel_latency(channel,
1604 vmbus_connection.monitor_pages[1]));
1606 static VMBUS_CHAN_ATTR(latency, S_IRUGO, channel_latency_show, NULL);
1608 static ssize_t channel_interrupts_show(struct vmbus_channel *channel, char *buf)
1610 return sprintf(buf, "%llu\n", channel->interrupts);
1612 static VMBUS_CHAN_ATTR(interrupts, S_IRUGO, channel_interrupts_show, NULL);
1614 static ssize_t channel_events_show(struct vmbus_channel *channel, char *buf)
1616 return sprintf(buf, "%llu\n", channel->sig_events);
1618 static VMBUS_CHAN_ATTR(events, S_IRUGO, channel_events_show, NULL);
1620 static ssize_t channel_intr_in_full_show(struct vmbus_channel *channel,
1621 char *buf)
1623 return sprintf(buf, "%llu\n",
1624 (unsigned long long)channel->intr_in_full);
1626 static VMBUS_CHAN_ATTR(intr_in_full, 0444, channel_intr_in_full_show, NULL);
1628 static ssize_t channel_intr_out_empty_show(struct vmbus_channel *channel,
1629 char *buf)
1631 return sprintf(buf, "%llu\n",
1632 (unsigned long long)channel->intr_out_empty);
1634 static VMBUS_CHAN_ATTR(intr_out_empty, 0444, channel_intr_out_empty_show, NULL);
1636 static ssize_t channel_out_full_first_show(struct vmbus_channel *channel,
1637 char *buf)
1639 return sprintf(buf, "%llu\n",
1640 (unsigned long long)channel->out_full_first);
1642 static VMBUS_CHAN_ATTR(out_full_first, 0444, channel_out_full_first_show, NULL);
1644 static ssize_t channel_out_full_total_show(struct vmbus_channel *channel,
1645 char *buf)
1647 return sprintf(buf, "%llu\n",
1648 (unsigned long long)channel->out_full_total);
1650 static VMBUS_CHAN_ATTR(out_full_total, 0444, channel_out_full_total_show, NULL);
1652 static ssize_t subchannel_monitor_id_show(struct vmbus_channel *channel,
1653 char *buf)
1655 return sprintf(buf, "%u\n", channel->offermsg.monitorid);
1657 static VMBUS_CHAN_ATTR(monitor_id, S_IRUGO, subchannel_monitor_id_show, NULL);
1659 static ssize_t subchannel_id_show(struct vmbus_channel *channel,
1660 char *buf)
1662 return sprintf(buf, "%u\n",
1663 channel->offermsg.offer.sub_channel_index);
1665 static VMBUS_CHAN_ATTR_RO(subchannel_id);
1667 static struct attribute *vmbus_chan_attrs[] = {
1668 &chan_attr_out_mask.attr,
1669 &chan_attr_in_mask.attr,
1670 &chan_attr_read_avail.attr,
1671 &chan_attr_write_avail.attr,
1672 &chan_attr_cpu.attr,
1673 &chan_attr_pending.attr,
1674 &chan_attr_latency.attr,
1675 &chan_attr_interrupts.attr,
1676 &chan_attr_events.attr,
1677 &chan_attr_intr_in_full.attr,
1678 &chan_attr_intr_out_empty.attr,
1679 &chan_attr_out_full_first.attr,
1680 &chan_attr_out_full_total.attr,
1681 &chan_attr_monitor_id.attr,
1682 &chan_attr_subchannel_id.attr,
1683 NULL
1687 * Channel-level attribute_group callback function. Returns the permission for
1688 * each attribute, and returns 0 if an attribute is not visible.
1690 static umode_t vmbus_chan_attr_is_visible(struct kobject *kobj,
1691 struct attribute *attr, int idx)
1693 const struct vmbus_channel *channel =
1694 container_of(kobj, struct vmbus_channel, kobj);
1696 /* Hide the monitor attributes if the monitor mechanism is not used. */
1697 if (!channel->offermsg.monitor_allocated &&
1698 (attr == &chan_attr_pending.attr ||
1699 attr == &chan_attr_latency.attr ||
1700 attr == &chan_attr_monitor_id.attr))
1701 return 0;
1703 return attr->mode;
1706 static struct attribute_group vmbus_chan_group = {
1707 .attrs = vmbus_chan_attrs,
1708 .is_visible = vmbus_chan_attr_is_visible
1711 static struct kobj_type vmbus_chan_ktype = {
1712 .sysfs_ops = &vmbus_chan_sysfs_ops,
1713 .release = vmbus_chan_release,
1717 * vmbus_add_channel_kobj - setup a sub-directory under device/channels
1719 int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
1721 const struct device *device = &dev->device;
1722 struct kobject *kobj = &channel->kobj;
1723 u32 relid = channel->offermsg.child_relid;
1724 int ret;
1726 kobj->kset = dev->channels_kset;
1727 ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
1728 "%u", relid);
1729 if (ret)
1730 return ret;
1732 ret = sysfs_create_group(kobj, &vmbus_chan_group);
1734 if (ret) {
1736 * The calling functions' error handling paths will cleanup the
1737 * empty channel directory.
1739 dev_err(device, "Unable to set up channel sysfs files\n");
1740 return ret;
1743 kobject_uevent(kobj, KOBJ_ADD);
1745 return 0;
1749 * vmbus_remove_channel_attr_group - remove the channel's attribute group
1751 void vmbus_remove_channel_attr_group(struct vmbus_channel *channel)
1753 sysfs_remove_group(&channel->kobj, &vmbus_chan_group);
1757 * vmbus_device_create - Creates and registers a new child device
1758 * on the vmbus.
1760 struct hv_device *vmbus_device_create(const guid_t *type,
1761 const guid_t *instance,
1762 struct vmbus_channel *channel)
1764 struct hv_device *child_device_obj;
1766 child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
1767 if (!child_device_obj) {
1768 pr_err("Unable to allocate device object for child device\n");
1769 return NULL;
1772 child_device_obj->channel = channel;
1773 guid_copy(&child_device_obj->dev_type, type);
1774 guid_copy(&child_device_obj->dev_instance, instance);
1775 child_device_obj->vendor_id = 0x1414; /* MSFT vendor ID */
1777 return child_device_obj;
1781 * vmbus_device_register - Register the child device
1783 int vmbus_device_register(struct hv_device *child_device_obj)
1785 struct kobject *kobj = &child_device_obj->device.kobj;
1786 int ret;
1788 dev_set_name(&child_device_obj->device, "%pUl",
1789 child_device_obj->channel->offermsg.offer.if_instance.b);
1791 child_device_obj->device.bus = &hv_bus;
1792 child_device_obj->device.parent = &hv_acpi_dev->dev;
1793 child_device_obj->device.release = vmbus_device_release;
1796 * Register with the LDM. This will kick off the driver/device
1797 * binding...which will eventually call vmbus_match() and vmbus_probe()
1799 ret = device_register(&child_device_obj->device);
1800 if (ret) {
1801 pr_err("Unable to register child device\n");
1802 return ret;
1805 child_device_obj->channels_kset = kset_create_and_add("channels",
1806 NULL, kobj);
1807 if (!child_device_obj->channels_kset) {
1808 ret = -ENOMEM;
1809 goto err_dev_unregister;
1812 ret = vmbus_add_channel_kobj(child_device_obj,
1813 child_device_obj->channel);
1814 if (ret) {
1815 pr_err("Unable to register primary channeln");
1816 goto err_kset_unregister;
1818 hv_debug_add_dev_dir(child_device_obj);
1820 return 0;
1822 err_kset_unregister:
1823 kset_unregister(child_device_obj->channels_kset);
1825 err_dev_unregister:
1826 device_unregister(&child_device_obj->device);
1827 return ret;
1831 * vmbus_device_unregister - Remove the specified child device
1832 * from the vmbus.
1834 void vmbus_device_unregister(struct hv_device *device_obj)
1836 pr_debug("child device %s unregistered\n",
1837 dev_name(&device_obj->device));
1839 kset_unregister(device_obj->channels_kset);
1842 * Kick off the process of unregistering the device.
1843 * This will call vmbus_remove() and eventually vmbus_device_release()
1845 device_unregister(&device_obj->device);
1850 * VMBUS is an acpi enumerated device. Get the information we
1851 * need from DSDT.
1853 #define VTPM_BASE_ADDRESS 0xfed40000
1854 static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
1856 resource_size_t start = 0;
1857 resource_size_t end = 0;
1858 struct resource *new_res;
1859 struct resource **old_res = &hyperv_mmio;
1860 struct resource **prev_res = NULL;
1862 switch (res->type) {
1865 * "Address" descriptors are for bus windows. Ignore
1866 * "memory" descriptors, which are for registers on
1867 * devices.
1869 case ACPI_RESOURCE_TYPE_ADDRESS32:
1870 start = res->data.address32.address.minimum;
1871 end = res->data.address32.address.maximum;
1872 break;
1874 case ACPI_RESOURCE_TYPE_ADDRESS64:
1875 start = res->data.address64.address.minimum;
1876 end = res->data.address64.address.maximum;
1877 break;
1879 default:
1880 /* Unused resource type */
1881 return AE_OK;
1885 * Ignore ranges that are below 1MB, as they're not
1886 * necessary or useful here.
1888 if (end < 0x100000)
1889 return AE_OK;
1891 new_res = kzalloc(sizeof(*new_res), GFP_ATOMIC);
1892 if (!new_res)
1893 return AE_NO_MEMORY;
1895 /* If this range overlaps the virtual TPM, truncate it. */
1896 if (end > VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS)
1897 end = VTPM_BASE_ADDRESS;
1899 new_res->name = "hyperv mmio";
1900 new_res->flags = IORESOURCE_MEM;
1901 new_res->start = start;
1902 new_res->end = end;
1905 * If two ranges are adjacent, merge them.
1907 do {
1908 if (!*old_res) {
1909 *old_res = new_res;
1910 break;
1913 if (((*old_res)->end + 1) == new_res->start) {
1914 (*old_res)->end = new_res->end;
1915 kfree(new_res);
1916 break;
1919 if ((*old_res)->start == new_res->end + 1) {
1920 (*old_res)->start = new_res->start;
1921 kfree(new_res);
1922 break;
1925 if ((*old_res)->start > new_res->end) {
1926 new_res->sibling = *old_res;
1927 if (prev_res)
1928 (*prev_res)->sibling = new_res;
1929 *old_res = new_res;
1930 break;
1933 prev_res = old_res;
1934 old_res = &(*old_res)->sibling;
1936 } while (1);
1938 return AE_OK;
1941 static int vmbus_acpi_remove(struct acpi_device *device)
1943 struct resource *cur_res;
1944 struct resource *next_res;
1946 if (hyperv_mmio) {
1947 if (fb_mmio) {
1948 __release_region(hyperv_mmio, fb_mmio->start,
1949 resource_size(fb_mmio));
1950 fb_mmio = NULL;
1953 for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) {
1954 next_res = cur_res->sibling;
1955 kfree(cur_res);
1959 return 0;
1962 static void vmbus_reserve_fb(void)
1964 int size;
1966 * Make a claim for the frame buffer in the resource tree under the
1967 * first node, which will be the one below 4GB. The length seems to
1968 * be underreported, particularly in a Generation 1 VM. So start out
1969 * reserving a larger area and make it smaller until it succeeds.
1972 if (screen_info.lfb_base) {
1973 if (efi_enabled(EFI_BOOT))
1974 size = max_t(__u32, screen_info.lfb_size, 0x800000);
1975 else
1976 size = max_t(__u32, screen_info.lfb_size, 0x4000000);
1978 for (; !fb_mmio && (size >= 0x100000); size >>= 1) {
1979 fb_mmio = __request_region(hyperv_mmio,
1980 screen_info.lfb_base, size,
1981 fb_mmio_name, 0);
1987 * vmbus_allocate_mmio() - Pick a memory-mapped I/O range.
1988 * @new: If successful, supplied a pointer to the
1989 * allocated MMIO space.
1990 * @device_obj: Identifies the caller
1991 * @min: Minimum guest physical address of the
1992 * allocation
1993 * @max: Maximum guest physical address
1994 * @size: Size of the range to be allocated
1995 * @align: Alignment of the range to be allocated
1996 * @fb_overlap_ok: Whether this allocation can be allowed
1997 * to overlap the video frame buffer.
1999 * This function walks the resources granted to VMBus by the
2000 * _CRS object in the ACPI namespace underneath the parent
2001 * "bridge" whether that's a root PCI bus in the Generation 1
2002 * case or a Module Device in the Generation 2 case. It then
2003 * attempts to allocate from the global MMIO pool in a way that
2004 * matches the constraints supplied in these parameters and by
2005 * that _CRS.
2007 * Return: 0 on success, -errno on failure
2009 int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
2010 resource_size_t min, resource_size_t max,
2011 resource_size_t size, resource_size_t align,
2012 bool fb_overlap_ok)
2014 struct resource *iter, *shadow;
2015 resource_size_t range_min, range_max, start;
2016 const char *dev_n = dev_name(&device_obj->device);
2017 int retval;
2019 retval = -ENXIO;
2020 mutex_lock(&hyperv_mmio_lock);
2023 * If overlaps with frame buffers are allowed, then first attempt to
2024 * make the allocation from within the reserved region. Because it
2025 * is already reserved, no shadow allocation is necessary.
2027 if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) &&
2028 !(max < fb_mmio->start)) {
2030 range_min = fb_mmio->start;
2031 range_max = fb_mmio->end;
2032 start = (range_min + align - 1) & ~(align - 1);
2033 for (; start + size - 1 <= range_max; start += align) {
2034 *new = request_mem_region_exclusive(start, size, dev_n);
2035 if (*new) {
2036 retval = 0;
2037 goto exit;
2042 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
2043 if ((iter->start >= max) || (iter->end <= min))
2044 continue;
2046 range_min = iter->start;
2047 range_max = iter->end;
2048 start = (range_min + align - 1) & ~(align - 1);
2049 for (; start + size - 1 <= range_max; start += align) {
2050 shadow = __request_region(iter, start, size, NULL,
2051 IORESOURCE_BUSY);
2052 if (!shadow)
2053 continue;
2055 *new = request_mem_region_exclusive(start, size, dev_n);
2056 if (*new) {
2057 shadow->name = (char *)*new;
2058 retval = 0;
2059 goto exit;
2062 __release_region(iter, start, size);
2066 exit:
2067 mutex_unlock(&hyperv_mmio_lock);
2068 return retval;
2070 EXPORT_SYMBOL_GPL(vmbus_allocate_mmio);
2073 * vmbus_free_mmio() - Free a memory-mapped I/O range.
2074 * @start: Base address of region to release.
2075 * @size: Size of the range to be allocated
2077 * This function releases anything requested by
2078 * vmbus_mmio_allocate().
2080 void vmbus_free_mmio(resource_size_t start, resource_size_t size)
2082 struct resource *iter;
2084 mutex_lock(&hyperv_mmio_lock);
2085 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
2086 if ((iter->start >= start + size) || (iter->end <= start))
2087 continue;
2089 __release_region(iter, start, size);
2091 release_mem_region(start, size);
2092 mutex_unlock(&hyperv_mmio_lock);
2095 EXPORT_SYMBOL_GPL(vmbus_free_mmio);
2097 static int vmbus_acpi_add(struct acpi_device *device)
2099 acpi_status result;
2100 int ret_val = -ENODEV;
2101 struct acpi_device *ancestor;
2103 hv_acpi_dev = device;
2105 result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
2106 vmbus_walk_resources, NULL);
2108 if (ACPI_FAILURE(result))
2109 goto acpi_walk_err;
2111 * Some ancestor of the vmbus acpi device (Gen1 or Gen2
2112 * firmware) is the VMOD that has the mmio ranges. Get that.
2114 for (ancestor = device->parent; ancestor; ancestor = ancestor->parent) {
2115 result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS,
2116 vmbus_walk_resources, NULL);
2118 if (ACPI_FAILURE(result))
2119 continue;
2120 if (hyperv_mmio) {
2121 vmbus_reserve_fb();
2122 break;
2125 ret_val = 0;
2127 acpi_walk_err:
2128 complete(&probe_event);
2129 if (ret_val)
2130 vmbus_acpi_remove(device);
2131 return ret_val;
2134 #ifdef CONFIG_PM_SLEEP
2135 static int vmbus_bus_suspend(struct device *dev)
2137 struct vmbus_channel *channel, *sc;
2138 unsigned long flags;
2140 while (atomic_read(&vmbus_connection.offer_in_progress) != 0) {
2142 * We wait here until the completion of any channel
2143 * offers that are currently in progress.
2145 msleep(1);
2148 mutex_lock(&vmbus_connection.channel_mutex);
2149 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
2150 if (!is_hvsock_channel(channel))
2151 continue;
2153 vmbus_force_channel_rescinded(channel);
2155 mutex_unlock(&vmbus_connection.channel_mutex);
2158 * Wait until all the sub-channels and hv_sock channels have been
2159 * cleaned up. Sub-channels should be destroyed upon suspend, otherwise
2160 * they would conflict with the new sub-channels that will be created
2161 * in the resume path. hv_sock channels should also be destroyed, but
2162 * a hv_sock channel of an established hv_sock connection can not be
2163 * really destroyed since it may still be referenced by the userspace
2164 * application, so we just force the hv_sock channel to be rescinded
2165 * by vmbus_force_channel_rescinded(), and the userspace application
2166 * will thoroughly destroy the channel after hibernation.
2168 * Note: the counter nr_chan_close_on_suspend may never go above 0 if
2169 * the VM has no sub-channel and hv_sock channel, e.g. a 1-vCPU VM.
2171 if (atomic_read(&vmbus_connection.nr_chan_close_on_suspend) > 0)
2172 wait_for_completion(&vmbus_connection.ready_for_suspend_event);
2174 WARN_ON(atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) != 0);
2176 mutex_lock(&vmbus_connection.channel_mutex);
2178 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
2180 * Invalidate the field. Upon resume, vmbus_onoffer() will fix
2181 * up the field, and the other fields (if necessary).
2183 channel->offermsg.child_relid = INVALID_RELID;
2185 if (is_hvsock_channel(channel)) {
2186 if (!channel->rescind) {
2187 pr_err("hv_sock channel not rescinded!\n");
2188 WARN_ON_ONCE(1);
2190 continue;
2193 spin_lock_irqsave(&channel->lock, flags);
2194 list_for_each_entry(sc, &channel->sc_list, sc_list) {
2195 pr_err("Sub-channel not deleted!\n");
2196 WARN_ON_ONCE(1);
2198 spin_unlock_irqrestore(&channel->lock, flags);
2200 atomic_inc(&vmbus_connection.nr_chan_fixup_on_resume);
2203 mutex_unlock(&vmbus_connection.channel_mutex);
2205 vmbus_initiate_unload(false);
2207 vmbus_connection.conn_state = DISCONNECTED;
2209 /* Reset the event for the next resume. */
2210 reinit_completion(&vmbus_connection.ready_for_resume_event);
2212 return 0;
2215 static int vmbus_bus_resume(struct device *dev)
2217 struct vmbus_channel_msginfo *msginfo;
2218 size_t msgsize;
2219 int ret;
2222 * We only use the 'vmbus_proto_version', which was in use before
2223 * hibernation, to re-negotiate with the host.
2225 if (!vmbus_proto_version) {
2226 pr_err("Invalid proto version = 0x%x\n", vmbus_proto_version);
2227 return -EINVAL;
2230 msgsize = sizeof(*msginfo) +
2231 sizeof(struct vmbus_channel_initiate_contact);
2233 msginfo = kzalloc(msgsize, GFP_KERNEL);
2235 if (msginfo == NULL)
2236 return -ENOMEM;
2238 ret = vmbus_negotiate_version(msginfo, vmbus_proto_version);
2240 kfree(msginfo);
2242 if (ret != 0)
2243 return ret;
2245 WARN_ON(atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) == 0);
2247 vmbus_request_offers();
2249 wait_for_completion(&vmbus_connection.ready_for_resume_event);
2251 /* Reset the event for the next suspend. */
2252 reinit_completion(&vmbus_connection.ready_for_suspend_event);
2254 return 0;
2256 #endif /* CONFIG_PM_SLEEP */
2258 static const struct acpi_device_id vmbus_acpi_device_ids[] = {
2259 {"VMBUS", 0},
2260 {"VMBus", 0},
2261 {"", 0},
2263 MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
2266 * Note: we must use SET_NOIRQ_SYSTEM_SLEEP_PM_OPS rather than
2267 * SET_SYSTEM_SLEEP_PM_OPS, otherwise NIC SR-IOV can not work, because the
2268 * "pci_dev_pm_ops" uses the "noirq" callbacks: in the resume path, the
2269 * pci "noirq" restore callback runs before "non-noirq" callbacks (see
2270 * resume_target_kernel() -> dpm_resume_start(), and hibernation_restore() ->
2271 * dpm_resume_end()). This means vmbus_bus_resume() and the pci-hyperv's
2272 * resume callback must also run via the "noirq" callbacks.
2274 static const struct dev_pm_ops vmbus_bus_pm = {
2275 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(vmbus_bus_suspend, vmbus_bus_resume)
2278 static struct acpi_driver vmbus_acpi_driver = {
2279 .name = "vmbus",
2280 .ids = vmbus_acpi_device_ids,
2281 .ops = {
2282 .add = vmbus_acpi_add,
2283 .remove = vmbus_acpi_remove,
2285 .drv.pm = &vmbus_bus_pm,
2288 static void hv_kexec_handler(void)
2290 hv_stimer_global_cleanup();
2291 vmbus_initiate_unload(false);
2292 vmbus_connection.conn_state = DISCONNECTED;
2293 /* Make sure conn_state is set as hv_synic_cleanup checks for it */
2294 mb();
2295 cpuhp_remove_state(hyperv_cpuhp_online);
2296 hyperv_cleanup();
2299 static void hv_crash_handler(struct pt_regs *regs)
2301 int cpu;
2303 vmbus_initiate_unload(true);
2305 * In crash handler we can't schedule synic cleanup for all CPUs,
2306 * doing the cleanup for current CPU only. This should be sufficient
2307 * for kdump.
2309 vmbus_connection.conn_state = DISCONNECTED;
2310 cpu = smp_processor_id();
2311 hv_stimer_cleanup(cpu);
2312 hv_synic_disable_regs(cpu);
2313 hyperv_cleanup();
2316 static int hv_synic_suspend(void)
2319 * When we reach here, all the non-boot CPUs have been offlined.
2320 * If we're in a legacy configuration where stimer Direct Mode is
2321 * not enabled, the stimers on the non-boot CPUs have been unbound
2322 * in hv_synic_cleanup() -> hv_stimer_legacy_cleanup() ->
2323 * hv_stimer_cleanup() -> clockevents_unbind_device().
2325 * hv_synic_suspend() only runs on CPU0 with interrupts disabled.
2326 * Here we do not call hv_stimer_legacy_cleanup() on CPU0 because:
2327 * 1) it's unnecessary as interrupts remain disabled between
2328 * syscore_suspend() and syscore_resume(): see create_image() and
2329 * resume_target_kernel()
2330 * 2) the stimer on CPU0 is automatically disabled later by
2331 * syscore_suspend() -> timekeeping_suspend() -> tick_suspend() -> ...
2332 * -> clockevents_shutdown() -> ... -> hv_ce_shutdown()
2333 * 3) a warning would be triggered if we call
2334 * clockevents_unbind_device(), which may sleep, in an
2335 * interrupts-disabled context.
2338 hv_synic_disable_regs(0);
2340 return 0;
2343 static void hv_synic_resume(void)
2345 hv_synic_enable_regs(0);
2348 * Note: we don't need to call hv_stimer_init(0), because the timer
2349 * on CPU0 is not unbound in hv_synic_suspend(), and the timer is
2350 * automatically re-enabled in timekeeping_resume().
2354 /* The callbacks run only on CPU0, with irqs_disabled. */
2355 static struct syscore_ops hv_synic_syscore_ops = {
2356 .suspend = hv_synic_suspend,
2357 .resume = hv_synic_resume,
2360 static int __init hv_acpi_init(void)
2362 int ret, t;
2364 if (!hv_is_hyperv_initialized())
2365 return -ENODEV;
2367 init_completion(&probe_event);
2370 * Get ACPI resources first.
2372 ret = acpi_bus_register_driver(&vmbus_acpi_driver);
2374 if (ret)
2375 return ret;
2377 t = wait_for_completion_timeout(&probe_event, 5*HZ);
2378 if (t == 0) {
2379 ret = -ETIMEDOUT;
2380 goto cleanup;
2382 hv_debug_init();
2384 ret = vmbus_bus_init();
2385 if (ret)
2386 goto cleanup;
2388 hv_setup_kexec_handler(hv_kexec_handler);
2389 hv_setup_crash_handler(hv_crash_handler);
2391 register_syscore_ops(&hv_synic_syscore_ops);
2393 return 0;
2395 cleanup:
2396 acpi_bus_unregister_driver(&vmbus_acpi_driver);
2397 hv_acpi_dev = NULL;
2398 return ret;
2401 static void __exit vmbus_exit(void)
2403 int cpu;
2405 unregister_syscore_ops(&hv_synic_syscore_ops);
2407 hv_remove_kexec_handler();
2408 hv_remove_crash_handler();
2409 vmbus_connection.conn_state = DISCONNECTED;
2410 hv_stimer_global_cleanup();
2411 vmbus_disconnect();
2412 hv_remove_vmbus_irq();
2413 for_each_online_cpu(cpu) {
2414 struct hv_per_cpu_context *hv_cpu
2415 = per_cpu_ptr(hv_context.cpu_context, cpu);
2417 tasklet_kill(&hv_cpu->msg_dpc);
2419 hv_debug_rm_all_dir();
2421 vmbus_free_channels();
2423 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
2424 kmsg_dump_unregister(&hv_kmsg_dumper);
2425 unregister_die_notifier(&hyperv_die_block);
2426 atomic_notifier_chain_unregister(&panic_notifier_list,
2427 &hyperv_panic_block);
2430 free_page((unsigned long)hv_panic_page);
2431 unregister_sysctl_table(hv_ctl_table_hdr);
2432 hv_ctl_table_hdr = NULL;
2433 bus_unregister(&hv_bus);
2435 cpuhp_remove_state(hyperv_cpuhp_online);
2436 hv_synic_free();
2437 acpi_bus_unregister_driver(&vmbus_acpi_driver);
2441 MODULE_LICENSE("GPL");
2442 MODULE_DESCRIPTION("Microsoft Hyper-V VMBus Driver");
2444 subsys_initcall(hv_acpi_init);
2445 module_exit(vmbus_exit);