1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2009, Microsoft Corporation.
6 * Haiyang Zhang <haiyangz@microsoft.com>
7 * Hank Janssen <hjanssen@microsoft.com>
8 * K. Y. Srinivasan <kys@microsoft.com>
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/interrupt.h>
16 #include <linux/sysctl.h>
17 #include <linux/slab.h>
18 #include <linux/acpi.h>
19 #include <linux/completion.h>
20 #include <linux/hyperv.h>
21 #include <linux/kernel_stat.h>
22 #include <linux/clockchips.h>
23 #include <linux/cpu.h>
24 #include <linux/sched/task_stack.h>
26 #include <asm/mshyperv.h>
27 #include <linux/notifier.h>
28 #include <linux/ptrace.h>
29 #include <linux/screen_info.h>
30 #include <linux/kdebug.h>
31 #include <linux/efi.h>
32 #include <linux/random.h>
33 #include <clocksource/hyperv_timer.h>
34 #include "hyperv_vmbus.h"
37 struct list_head node
;
38 struct hv_vmbus_device_id id
;
41 static struct acpi_device
*hv_acpi_dev
;
43 static struct completion probe_event
;
45 static int hyperv_cpuhp_online
;
47 static void *hv_panic_page
;
49 static int hyperv_panic_event(struct notifier_block
*nb
, unsigned long val
,
54 regs
= current_pt_regs();
56 hyperv_report_panic(regs
, val
);
60 static int hyperv_die_event(struct notifier_block
*nb
, unsigned long val
,
63 struct die_args
*die
= (struct die_args
*)args
;
64 struct pt_regs
*regs
= die
->regs
;
66 hyperv_report_panic(regs
, val
);
70 static struct notifier_block hyperv_die_block
= {
71 .notifier_call
= hyperv_die_event
,
73 static struct notifier_block hyperv_panic_block
= {
74 .notifier_call
= hyperv_panic_event
,
77 static const char *fb_mmio_name
= "fb_range";
78 static struct resource
*fb_mmio
;
79 static struct resource
*hyperv_mmio
;
80 static DEFINE_SEMAPHORE(hyperv_mmio_lock
);
82 static int vmbus_exists(void)
84 if (hv_acpi_dev
== NULL
)
90 #define VMBUS_ALIAS_LEN ((sizeof((struct hv_vmbus_device_id *)0)->guid) * 2)
91 static void print_alias_name(struct hv_device
*hv_dev
, char *alias_name
)
94 for (i
= 0; i
< VMBUS_ALIAS_LEN
; i
+= 2)
95 sprintf(&alias_name
[i
], "%02x", hv_dev
->dev_type
.b
[i
/2]);
98 static u8
channel_monitor_group(const struct vmbus_channel
*channel
)
100 return (u8
)channel
->offermsg
.monitorid
/ 32;
103 static u8
channel_monitor_offset(const struct vmbus_channel
*channel
)
105 return (u8
)channel
->offermsg
.monitorid
% 32;
108 static u32
channel_pending(const struct vmbus_channel
*channel
,
109 const struct hv_monitor_page
*monitor_page
)
111 u8 monitor_group
= channel_monitor_group(channel
);
113 return monitor_page
->trigger_group
[monitor_group
].pending
;
116 static u32
channel_latency(const struct vmbus_channel
*channel
,
117 const struct hv_monitor_page
*monitor_page
)
119 u8 monitor_group
= channel_monitor_group(channel
);
120 u8 monitor_offset
= channel_monitor_offset(channel
);
122 return monitor_page
->latency
[monitor_group
][monitor_offset
];
125 static u32
channel_conn_id(struct vmbus_channel
*channel
,
126 struct hv_monitor_page
*monitor_page
)
128 u8 monitor_group
= channel_monitor_group(channel
);
129 u8 monitor_offset
= channel_monitor_offset(channel
);
130 return monitor_page
->parameter
[monitor_group
][monitor_offset
].connectionid
.u
.id
;
133 static ssize_t
id_show(struct device
*dev
, struct device_attribute
*dev_attr
,
136 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
138 if (!hv_dev
->channel
)
140 return sprintf(buf
, "%d\n", hv_dev
->channel
->offermsg
.child_relid
);
142 static DEVICE_ATTR_RO(id
);
144 static ssize_t
state_show(struct device
*dev
, struct device_attribute
*dev_attr
,
147 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
149 if (!hv_dev
->channel
)
151 return sprintf(buf
, "%d\n", hv_dev
->channel
->state
);
153 static DEVICE_ATTR_RO(state
);
155 static ssize_t
monitor_id_show(struct device
*dev
,
156 struct device_attribute
*dev_attr
, char *buf
)
158 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
160 if (!hv_dev
->channel
)
162 return sprintf(buf
, "%d\n", hv_dev
->channel
->offermsg
.monitorid
);
164 static DEVICE_ATTR_RO(monitor_id
);
166 static ssize_t
class_id_show(struct device
*dev
,
167 struct device_attribute
*dev_attr
, char *buf
)
169 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
171 if (!hv_dev
->channel
)
173 return sprintf(buf
, "{%pUl}\n",
174 hv_dev
->channel
->offermsg
.offer
.if_type
.b
);
176 static DEVICE_ATTR_RO(class_id
);
178 static ssize_t
device_id_show(struct device
*dev
,
179 struct device_attribute
*dev_attr
, char *buf
)
181 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
183 if (!hv_dev
->channel
)
185 return sprintf(buf
, "{%pUl}\n",
186 hv_dev
->channel
->offermsg
.offer
.if_instance
.b
);
188 static DEVICE_ATTR_RO(device_id
);
190 static ssize_t
modalias_show(struct device
*dev
,
191 struct device_attribute
*dev_attr
, char *buf
)
193 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
194 char alias_name
[VMBUS_ALIAS_LEN
+ 1];
196 print_alias_name(hv_dev
, alias_name
);
197 return sprintf(buf
, "vmbus:%s\n", alias_name
);
199 static DEVICE_ATTR_RO(modalias
);
202 static ssize_t
numa_node_show(struct device
*dev
,
203 struct device_attribute
*attr
, char *buf
)
205 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
207 if (!hv_dev
->channel
)
210 return sprintf(buf
, "%d\n", hv_dev
->channel
->numa_node
);
212 static DEVICE_ATTR_RO(numa_node
);
215 static ssize_t
server_monitor_pending_show(struct device
*dev
,
216 struct device_attribute
*dev_attr
,
219 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
221 if (!hv_dev
->channel
)
223 return sprintf(buf
, "%d\n",
224 channel_pending(hv_dev
->channel
,
225 vmbus_connection
.monitor_pages
[0]));
227 static DEVICE_ATTR_RO(server_monitor_pending
);
229 static ssize_t
client_monitor_pending_show(struct device
*dev
,
230 struct device_attribute
*dev_attr
,
233 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
235 if (!hv_dev
->channel
)
237 return sprintf(buf
, "%d\n",
238 channel_pending(hv_dev
->channel
,
239 vmbus_connection
.monitor_pages
[1]));
241 static DEVICE_ATTR_RO(client_monitor_pending
);
243 static ssize_t
server_monitor_latency_show(struct device
*dev
,
244 struct device_attribute
*dev_attr
,
247 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
249 if (!hv_dev
->channel
)
251 return sprintf(buf
, "%d\n",
252 channel_latency(hv_dev
->channel
,
253 vmbus_connection
.monitor_pages
[0]));
255 static DEVICE_ATTR_RO(server_monitor_latency
);
257 static ssize_t
client_monitor_latency_show(struct device
*dev
,
258 struct device_attribute
*dev_attr
,
261 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
263 if (!hv_dev
->channel
)
265 return sprintf(buf
, "%d\n",
266 channel_latency(hv_dev
->channel
,
267 vmbus_connection
.monitor_pages
[1]));
269 static DEVICE_ATTR_RO(client_monitor_latency
);
271 static ssize_t
server_monitor_conn_id_show(struct device
*dev
,
272 struct device_attribute
*dev_attr
,
275 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
277 if (!hv_dev
->channel
)
279 return sprintf(buf
, "%d\n",
280 channel_conn_id(hv_dev
->channel
,
281 vmbus_connection
.monitor_pages
[0]));
283 static DEVICE_ATTR_RO(server_monitor_conn_id
);
285 static ssize_t
client_monitor_conn_id_show(struct device
*dev
,
286 struct device_attribute
*dev_attr
,
289 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
291 if (!hv_dev
->channel
)
293 return sprintf(buf
, "%d\n",
294 channel_conn_id(hv_dev
->channel
,
295 vmbus_connection
.monitor_pages
[1]));
297 static DEVICE_ATTR_RO(client_monitor_conn_id
);
299 static ssize_t
out_intr_mask_show(struct device
*dev
,
300 struct device_attribute
*dev_attr
, char *buf
)
302 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
303 struct hv_ring_buffer_debug_info outbound
;
306 if (!hv_dev
->channel
)
309 ret
= hv_ringbuffer_get_debuginfo(&hv_dev
->channel
->outbound
,
314 return sprintf(buf
, "%d\n", outbound
.current_interrupt_mask
);
316 static DEVICE_ATTR_RO(out_intr_mask
);
318 static ssize_t
out_read_index_show(struct device
*dev
,
319 struct device_attribute
*dev_attr
, char *buf
)
321 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
322 struct hv_ring_buffer_debug_info outbound
;
325 if (!hv_dev
->channel
)
328 ret
= hv_ringbuffer_get_debuginfo(&hv_dev
->channel
->outbound
,
332 return sprintf(buf
, "%d\n", outbound
.current_read_index
);
334 static DEVICE_ATTR_RO(out_read_index
);
336 static ssize_t
out_write_index_show(struct device
*dev
,
337 struct device_attribute
*dev_attr
,
340 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
341 struct hv_ring_buffer_debug_info outbound
;
344 if (!hv_dev
->channel
)
347 ret
= hv_ringbuffer_get_debuginfo(&hv_dev
->channel
->outbound
,
351 return sprintf(buf
, "%d\n", outbound
.current_write_index
);
353 static DEVICE_ATTR_RO(out_write_index
);
355 static ssize_t
out_read_bytes_avail_show(struct device
*dev
,
356 struct device_attribute
*dev_attr
,
359 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
360 struct hv_ring_buffer_debug_info outbound
;
363 if (!hv_dev
->channel
)
366 ret
= hv_ringbuffer_get_debuginfo(&hv_dev
->channel
->outbound
,
370 return sprintf(buf
, "%d\n", outbound
.bytes_avail_toread
);
372 static DEVICE_ATTR_RO(out_read_bytes_avail
);
374 static ssize_t
out_write_bytes_avail_show(struct device
*dev
,
375 struct device_attribute
*dev_attr
,
378 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
379 struct hv_ring_buffer_debug_info outbound
;
382 if (!hv_dev
->channel
)
385 ret
= hv_ringbuffer_get_debuginfo(&hv_dev
->channel
->outbound
,
389 return sprintf(buf
, "%d\n", outbound
.bytes_avail_towrite
);
391 static DEVICE_ATTR_RO(out_write_bytes_avail
);
393 static ssize_t
in_intr_mask_show(struct device
*dev
,
394 struct device_attribute
*dev_attr
, char *buf
)
396 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
397 struct hv_ring_buffer_debug_info inbound
;
400 if (!hv_dev
->channel
)
403 ret
= hv_ringbuffer_get_debuginfo(&hv_dev
->channel
->inbound
, &inbound
);
407 return sprintf(buf
, "%d\n", inbound
.current_interrupt_mask
);
409 static DEVICE_ATTR_RO(in_intr_mask
);
411 static ssize_t
in_read_index_show(struct device
*dev
,
412 struct device_attribute
*dev_attr
, char *buf
)
414 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
415 struct hv_ring_buffer_debug_info inbound
;
418 if (!hv_dev
->channel
)
421 ret
= hv_ringbuffer_get_debuginfo(&hv_dev
->channel
->inbound
, &inbound
);
425 return sprintf(buf
, "%d\n", inbound
.current_read_index
);
427 static DEVICE_ATTR_RO(in_read_index
);
429 static ssize_t
in_write_index_show(struct device
*dev
,
430 struct device_attribute
*dev_attr
, char *buf
)
432 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
433 struct hv_ring_buffer_debug_info inbound
;
436 if (!hv_dev
->channel
)
439 ret
= hv_ringbuffer_get_debuginfo(&hv_dev
->channel
->inbound
, &inbound
);
443 return sprintf(buf
, "%d\n", inbound
.current_write_index
);
445 static DEVICE_ATTR_RO(in_write_index
);
447 static ssize_t
in_read_bytes_avail_show(struct device
*dev
,
448 struct device_attribute
*dev_attr
,
451 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
452 struct hv_ring_buffer_debug_info inbound
;
455 if (!hv_dev
->channel
)
458 ret
= hv_ringbuffer_get_debuginfo(&hv_dev
->channel
->inbound
, &inbound
);
462 return sprintf(buf
, "%d\n", inbound
.bytes_avail_toread
);
464 static DEVICE_ATTR_RO(in_read_bytes_avail
);
466 static ssize_t
in_write_bytes_avail_show(struct device
*dev
,
467 struct device_attribute
*dev_attr
,
470 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
471 struct hv_ring_buffer_debug_info inbound
;
474 if (!hv_dev
->channel
)
477 ret
= hv_ringbuffer_get_debuginfo(&hv_dev
->channel
->inbound
, &inbound
);
481 return sprintf(buf
, "%d\n", inbound
.bytes_avail_towrite
);
483 static DEVICE_ATTR_RO(in_write_bytes_avail
);
485 static ssize_t
channel_vp_mapping_show(struct device
*dev
,
486 struct device_attribute
*dev_attr
,
489 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
490 struct vmbus_channel
*channel
= hv_dev
->channel
, *cur_sc
;
492 int buf_size
= PAGE_SIZE
, n_written
, tot_written
;
493 struct list_head
*cur
;
498 tot_written
= snprintf(buf
, buf_size
, "%u:%u\n",
499 channel
->offermsg
.child_relid
, channel
->target_cpu
);
501 spin_lock_irqsave(&channel
->lock
, flags
);
503 list_for_each(cur
, &channel
->sc_list
) {
504 if (tot_written
>= buf_size
- 1)
507 cur_sc
= list_entry(cur
, struct vmbus_channel
, sc_list
);
508 n_written
= scnprintf(buf
+ tot_written
,
509 buf_size
- tot_written
,
511 cur_sc
->offermsg
.child_relid
,
513 tot_written
+= n_written
;
516 spin_unlock_irqrestore(&channel
->lock
, flags
);
520 static DEVICE_ATTR_RO(channel_vp_mapping
);
522 static ssize_t
vendor_show(struct device
*dev
,
523 struct device_attribute
*dev_attr
,
526 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
527 return sprintf(buf
, "0x%x\n", hv_dev
->vendor_id
);
529 static DEVICE_ATTR_RO(vendor
);
531 static ssize_t
device_show(struct device
*dev
,
532 struct device_attribute
*dev_attr
,
535 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
536 return sprintf(buf
, "0x%x\n", hv_dev
->device_id
);
538 static DEVICE_ATTR_RO(device
);
540 static ssize_t
driver_override_store(struct device
*dev
,
541 struct device_attribute
*attr
,
542 const char *buf
, size_t count
)
544 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
545 char *driver_override
, *old
, *cp
;
547 /* We need to keep extra room for a newline */
548 if (count
>= (PAGE_SIZE
- 1))
551 driver_override
= kstrndup(buf
, count
, GFP_KERNEL
);
552 if (!driver_override
)
555 cp
= strchr(driver_override
, '\n');
560 old
= hv_dev
->driver_override
;
561 if (strlen(driver_override
)) {
562 hv_dev
->driver_override
= driver_override
;
564 kfree(driver_override
);
565 hv_dev
->driver_override
= NULL
;
574 static ssize_t
driver_override_show(struct device
*dev
,
575 struct device_attribute
*attr
, char *buf
)
577 struct hv_device
*hv_dev
= device_to_hv_device(dev
);
581 len
= snprintf(buf
, PAGE_SIZE
, "%s\n", hv_dev
->driver_override
);
586 static DEVICE_ATTR_RW(driver_override
);
588 /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
589 static struct attribute
*vmbus_dev_attrs
[] = {
591 &dev_attr_state
.attr
,
592 &dev_attr_monitor_id
.attr
,
593 &dev_attr_class_id
.attr
,
594 &dev_attr_device_id
.attr
,
595 &dev_attr_modalias
.attr
,
597 &dev_attr_numa_node
.attr
,
599 &dev_attr_server_monitor_pending
.attr
,
600 &dev_attr_client_monitor_pending
.attr
,
601 &dev_attr_server_monitor_latency
.attr
,
602 &dev_attr_client_monitor_latency
.attr
,
603 &dev_attr_server_monitor_conn_id
.attr
,
604 &dev_attr_client_monitor_conn_id
.attr
,
605 &dev_attr_out_intr_mask
.attr
,
606 &dev_attr_out_read_index
.attr
,
607 &dev_attr_out_write_index
.attr
,
608 &dev_attr_out_read_bytes_avail
.attr
,
609 &dev_attr_out_write_bytes_avail
.attr
,
610 &dev_attr_in_intr_mask
.attr
,
611 &dev_attr_in_read_index
.attr
,
612 &dev_attr_in_write_index
.attr
,
613 &dev_attr_in_read_bytes_avail
.attr
,
614 &dev_attr_in_write_bytes_avail
.attr
,
615 &dev_attr_channel_vp_mapping
.attr
,
616 &dev_attr_vendor
.attr
,
617 &dev_attr_device
.attr
,
618 &dev_attr_driver_override
.attr
,
623 * Device-level attribute_group callback function. Returns the permission for
624 * each attribute, and returns 0 if an attribute is not visible.
626 static umode_t
vmbus_dev_attr_is_visible(struct kobject
*kobj
,
627 struct attribute
*attr
, int idx
)
629 struct device
*dev
= kobj_to_dev(kobj
);
630 const struct hv_device
*hv_dev
= device_to_hv_device(dev
);
632 /* Hide the monitor attributes if the monitor mechanism is not used. */
633 if (!hv_dev
->channel
->offermsg
.monitor_allocated
&&
634 (attr
== &dev_attr_monitor_id
.attr
||
635 attr
== &dev_attr_server_monitor_pending
.attr
||
636 attr
== &dev_attr_client_monitor_pending
.attr
||
637 attr
== &dev_attr_server_monitor_latency
.attr
||
638 attr
== &dev_attr_client_monitor_latency
.attr
||
639 attr
== &dev_attr_server_monitor_conn_id
.attr
||
640 attr
== &dev_attr_client_monitor_conn_id
.attr
))
646 static const struct attribute_group vmbus_dev_group
= {
647 .attrs
= vmbus_dev_attrs
,
648 .is_visible
= vmbus_dev_attr_is_visible
650 __ATTRIBUTE_GROUPS(vmbus_dev
);
653 * vmbus_uevent - add uevent for our device
655 * This routine is invoked when a device is added or removed on the vmbus to
656 * generate a uevent to udev in the userspace. The udev will then look at its
657 * rule and the uevent generated here to load the appropriate driver
659 * The alias string will be of the form vmbus:guid where guid is the string
660 * representation of the device guid (each byte of the guid will be
661 * represented with two hex characters.
663 static int vmbus_uevent(struct device
*device
, struct kobj_uevent_env
*env
)
665 struct hv_device
*dev
= device_to_hv_device(device
);
667 char alias_name
[VMBUS_ALIAS_LEN
+ 1];
669 print_alias_name(dev
, alias_name
);
670 ret
= add_uevent_var(env
, "MODALIAS=vmbus:%s", alias_name
);
674 static const struct hv_vmbus_device_id
*
675 hv_vmbus_dev_match(const struct hv_vmbus_device_id
*id
, const guid_t
*guid
)
678 return NULL
; /* empty device table */
680 for (; !guid_is_null(&id
->guid
); id
++)
681 if (guid_equal(&id
->guid
, guid
))
687 static const struct hv_vmbus_device_id
*
688 hv_vmbus_dynid_match(struct hv_driver
*drv
, const guid_t
*guid
)
690 const struct hv_vmbus_device_id
*id
= NULL
;
691 struct vmbus_dynid
*dynid
;
693 spin_lock(&drv
->dynids
.lock
);
694 list_for_each_entry(dynid
, &drv
->dynids
.list
, node
) {
695 if (guid_equal(&dynid
->id
.guid
, guid
)) {
700 spin_unlock(&drv
->dynids
.lock
);
705 static const struct hv_vmbus_device_id vmbus_device_null
;
708 * Return a matching hv_vmbus_device_id pointer.
709 * If there is no match, return NULL.
711 static const struct hv_vmbus_device_id
*hv_vmbus_get_id(struct hv_driver
*drv
,
712 struct hv_device
*dev
)
714 const guid_t
*guid
= &dev
->dev_type
;
715 const struct hv_vmbus_device_id
*id
;
717 /* When driver_override is set, only bind to the matching driver */
718 if (dev
->driver_override
&& strcmp(dev
->driver_override
, drv
->name
))
721 /* Look at the dynamic ids first, before the static ones */
722 id
= hv_vmbus_dynid_match(drv
, guid
);
724 id
= hv_vmbus_dev_match(drv
->id_table
, guid
);
726 /* driver_override will always match, send a dummy id */
727 if (!id
&& dev
->driver_override
)
728 id
= &vmbus_device_null
;
733 /* vmbus_add_dynid - add a new device ID to this driver and re-probe devices */
734 static int vmbus_add_dynid(struct hv_driver
*drv
, guid_t
*guid
)
736 struct vmbus_dynid
*dynid
;
738 dynid
= kzalloc(sizeof(*dynid
), GFP_KERNEL
);
742 dynid
->id
.guid
= *guid
;
744 spin_lock(&drv
->dynids
.lock
);
745 list_add_tail(&dynid
->node
, &drv
->dynids
.list
);
746 spin_unlock(&drv
->dynids
.lock
);
748 return driver_attach(&drv
->driver
);
751 static void vmbus_free_dynids(struct hv_driver
*drv
)
753 struct vmbus_dynid
*dynid
, *n
;
755 spin_lock(&drv
->dynids
.lock
);
756 list_for_each_entry_safe(dynid
, n
, &drv
->dynids
.list
, node
) {
757 list_del(&dynid
->node
);
760 spin_unlock(&drv
->dynids
.lock
);
764 * store_new_id - sysfs frontend to vmbus_add_dynid()
766 * Allow GUIDs to be added to an existing driver via sysfs.
768 static ssize_t
new_id_store(struct device_driver
*driver
, const char *buf
,
771 struct hv_driver
*drv
= drv_to_hv_drv(driver
);
775 retval
= guid_parse(buf
, &guid
);
779 if (hv_vmbus_dynid_match(drv
, &guid
))
782 retval
= vmbus_add_dynid(drv
, &guid
);
787 static DRIVER_ATTR_WO(new_id
);
790 * store_remove_id - remove a PCI device ID from this driver
792 * Removes a dynamic pci device ID to this driver.
794 static ssize_t
remove_id_store(struct device_driver
*driver
, const char *buf
,
797 struct hv_driver
*drv
= drv_to_hv_drv(driver
);
798 struct vmbus_dynid
*dynid
, *n
;
802 retval
= guid_parse(buf
, &guid
);
807 spin_lock(&drv
->dynids
.lock
);
808 list_for_each_entry_safe(dynid
, n
, &drv
->dynids
.list
, node
) {
809 struct hv_vmbus_device_id
*id
= &dynid
->id
;
811 if (guid_equal(&id
->guid
, &guid
)) {
812 list_del(&dynid
->node
);
818 spin_unlock(&drv
->dynids
.lock
);
822 static DRIVER_ATTR_WO(remove_id
);
824 static struct attribute
*vmbus_drv_attrs
[] = {
825 &driver_attr_new_id
.attr
,
826 &driver_attr_remove_id
.attr
,
829 ATTRIBUTE_GROUPS(vmbus_drv
);
833 * vmbus_match - Attempt to match the specified device to the specified driver
835 static int vmbus_match(struct device
*device
, struct device_driver
*driver
)
837 struct hv_driver
*drv
= drv_to_hv_drv(driver
);
838 struct hv_device
*hv_dev
= device_to_hv_device(device
);
840 /* The hv_sock driver handles all hv_sock offers. */
841 if (is_hvsock_channel(hv_dev
->channel
))
844 if (hv_vmbus_get_id(drv
, hv_dev
))
851 * vmbus_probe - Add the new vmbus's child device
853 static int vmbus_probe(struct device
*child_device
)
856 struct hv_driver
*drv
=
857 drv_to_hv_drv(child_device
->driver
);
858 struct hv_device
*dev
= device_to_hv_device(child_device
);
859 const struct hv_vmbus_device_id
*dev_id
;
861 dev_id
= hv_vmbus_get_id(drv
, dev
);
863 ret
= drv
->probe(dev
, dev_id
);
865 pr_err("probe failed for device %s (%d)\n",
866 dev_name(child_device
), ret
);
869 pr_err("probe not set for driver %s\n",
870 dev_name(child_device
));
877 * vmbus_remove - Remove a vmbus device
879 static int vmbus_remove(struct device
*child_device
)
881 struct hv_driver
*drv
;
882 struct hv_device
*dev
= device_to_hv_device(child_device
);
884 if (child_device
->driver
) {
885 drv
= drv_to_hv_drv(child_device
->driver
);
895 * vmbus_shutdown - Shutdown a vmbus device
897 static void vmbus_shutdown(struct device
*child_device
)
899 struct hv_driver
*drv
;
900 struct hv_device
*dev
= device_to_hv_device(child_device
);
903 /* The device may not be attached yet */
904 if (!child_device
->driver
)
907 drv
= drv_to_hv_drv(child_device
->driver
);
915 * vmbus_device_release - Final callback release of the vmbus child device
917 static void vmbus_device_release(struct device
*device
)
919 struct hv_device
*hv_dev
= device_to_hv_device(device
);
920 struct vmbus_channel
*channel
= hv_dev
->channel
;
922 mutex_lock(&vmbus_connection
.channel_mutex
);
923 hv_process_channel_removal(channel
);
924 mutex_unlock(&vmbus_connection
.channel_mutex
);
928 /* The one and only one */
929 static struct bus_type hv_bus
= {
931 .match
= vmbus_match
,
932 .shutdown
= vmbus_shutdown
,
933 .remove
= vmbus_remove
,
934 .probe
= vmbus_probe
,
935 .uevent
= vmbus_uevent
,
936 .dev_groups
= vmbus_dev_groups
,
937 .drv_groups
= vmbus_drv_groups
,
940 struct onmessage_work_context
{
941 struct work_struct work
;
942 struct hv_message msg
;
945 static void vmbus_onmessage_work(struct work_struct
*work
)
947 struct onmessage_work_context
*ctx
;
949 /* Do not process messages if we're in DISCONNECTED state */
950 if (vmbus_connection
.conn_state
== DISCONNECTED
)
953 ctx
= container_of(work
, struct onmessage_work_context
,
955 vmbus_onmessage(&ctx
->msg
);
959 void vmbus_on_msg_dpc(unsigned long data
)
961 struct hv_per_cpu_context
*hv_cpu
= (void *)data
;
962 void *page_addr
= hv_cpu
->synic_message_page
;
963 struct hv_message
*msg
= (struct hv_message
*)page_addr
+
965 struct vmbus_channel_message_header
*hdr
;
966 const struct vmbus_channel_message_table_entry
*entry
;
967 struct onmessage_work_context
*ctx
;
968 u32 message_type
= msg
->header
.message_type
;
970 if (message_type
== HVMSG_NONE
)
974 hdr
= (struct vmbus_channel_message_header
*)msg
->u
.payload
;
976 trace_vmbus_on_msg_dpc(hdr
);
978 if (hdr
->msgtype
>= CHANNELMSG_COUNT
) {
979 WARN_ONCE(1, "unknown msgtype=%d\n", hdr
->msgtype
);
983 entry
= &channel_message_table
[hdr
->msgtype
];
984 if (entry
->handler_type
== VMHT_BLOCKING
) {
985 ctx
= kmalloc(sizeof(*ctx
), GFP_ATOMIC
);
989 INIT_WORK(&ctx
->work
, vmbus_onmessage_work
);
990 memcpy(&ctx
->msg
, msg
, sizeof(*msg
));
993 * The host can generate a rescind message while we
994 * may still be handling the original offer. We deal with
995 * this condition by ensuring the processing is done on the
998 switch (hdr
->msgtype
) {
999 case CHANNELMSG_RESCIND_CHANNELOFFER
:
1001 * If we are handling the rescind message;
1002 * schedule the work on the global work queue.
1004 schedule_work_on(vmbus_connection
.connect_cpu
,
1008 case CHANNELMSG_OFFERCHANNEL
:
1009 atomic_inc(&vmbus_connection
.offer_in_progress
);
1010 queue_work_on(vmbus_connection
.connect_cpu
,
1011 vmbus_connection
.work_queue
,
1016 queue_work(vmbus_connection
.work_queue
, &ctx
->work
);
1019 entry
->message_handler(hdr
);
1022 vmbus_signal_eom(msg
, message_type
);
1027 * Direct callback for channels using other deferred processing
1029 static void vmbus_channel_isr(struct vmbus_channel
*channel
)
1031 void (*callback_fn
)(void *);
1033 callback_fn
= READ_ONCE(channel
->onchannel_callback
);
1034 if (likely(callback_fn
!= NULL
))
1035 (*callback_fn
)(channel
->channel_callback_context
);
1039 * Schedule all channels with events pending
1041 static void vmbus_chan_sched(struct hv_per_cpu_context
*hv_cpu
)
1043 unsigned long *recv_int_page
;
1046 if (vmbus_proto_version
< VERSION_WIN8
) {
1047 maxbits
= MAX_NUM_CHANNELS_SUPPORTED
;
1048 recv_int_page
= vmbus_connection
.recv_int_page
;
1051 * When the host is win8 and beyond, the event page
1052 * can be directly checked to get the id of the channel
1053 * that has the interrupt pending.
1055 void *page_addr
= hv_cpu
->synic_event_page
;
1056 union hv_synic_event_flags
*event
1057 = (union hv_synic_event_flags
*)page_addr
+
1060 maxbits
= HV_EVENT_FLAGS_COUNT
;
1061 recv_int_page
= event
->flags
;
1064 if (unlikely(!recv_int_page
))
1067 for_each_set_bit(relid
, recv_int_page
, maxbits
) {
1068 struct vmbus_channel
*channel
;
1070 if (!sync_test_and_clear_bit(relid
, recv_int_page
))
1073 /* Special case - vmbus channel protocol msg */
1079 /* Find channel based on relid */
1080 list_for_each_entry_rcu(channel
, &hv_cpu
->chan_list
, percpu_list
) {
1081 if (channel
->offermsg
.child_relid
!= relid
)
1084 if (channel
->rescind
)
1087 trace_vmbus_chan_sched(channel
);
1089 ++channel
->interrupts
;
1091 switch (channel
->callback_mode
) {
1093 vmbus_channel_isr(channel
);
1096 case HV_CALL_BATCHED
:
1097 hv_begin_read(&channel
->inbound
);
1099 case HV_CALL_DIRECT
:
1100 tasklet_schedule(&channel
->callback_event
);
1108 static void vmbus_isr(void)
1110 struct hv_per_cpu_context
*hv_cpu
1111 = this_cpu_ptr(hv_context
.cpu_context
);
1112 void *page_addr
= hv_cpu
->synic_event_page
;
1113 struct hv_message
*msg
;
1114 union hv_synic_event_flags
*event
;
1115 bool handled
= false;
1117 if (unlikely(page_addr
== NULL
))
1120 event
= (union hv_synic_event_flags
*)page_addr
+
1123 * Check for events before checking for messages. This is the order
1124 * in which events and messages are checked in Windows guests on
1125 * Hyper-V, and the Windows team suggested we do the same.
1128 if ((vmbus_proto_version
== VERSION_WS2008
) ||
1129 (vmbus_proto_version
== VERSION_WIN7
)) {
1131 /* Since we are a child, we only need to check bit 0 */
1132 if (sync_test_and_clear_bit(0, event
->flags
))
1136 * Our host is win8 or above. The signaling mechanism
1137 * has changed and we can directly look at the event page.
1138 * If bit n is set then we have an interrup on the channel
1145 vmbus_chan_sched(hv_cpu
);
1147 page_addr
= hv_cpu
->synic_message_page
;
1148 msg
= (struct hv_message
*)page_addr
+ VMBUS_MESSAGE_SINT
;
1150 /* Check if there are actual msgs to be processed */
1151 if (msg
->header
.message_type
!= HVMSG_NONE
) {
1152 if (msg
->header
.message_type
== HVMSG_TIMER_EXPIRED
) {
1154 vmbus_signal_eom(msg
, HVMSG_TIMER_EXPIRED
);
1156 tasklet_schedule(&hv_cpu
->msg_dpc
);
1159 add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR
, 0);
1163 * Boolean to control whether to report panic messages over Hyper-V.
1165 * It can be set via /proc/sys/kernel/hyperv/record_panic_msg
1167 static int sysctl_record_panic_msg
= 1;
1170 * Callback from kmsg_dump. Grab as much as possible from the end of the kmsg
1171 * buffer and call into Hyper-V to transfer the data.
1173 static void hv_kmsg_dump(struct kmsg_dumper
*dumper
,
1174 enum kmsg_dump_reason reason
)
1176 size_t bytes_written
;
1177 phys_addr_t panic_pa
;
1179 /* We are only interested in panics. */
1180 if ((reason
!= KMSG_DUMP_PANIC
) || (!sysctl_record_panic_msg
))
1183 panic_pa
= virt_to_phys(hv_panic_page
);
1186 * Write dump contents to the page. No need to synchronize; panic should
1187 * be single-threaded.
1189 kmsg_dump_get_buffer(dumper
, true, hv_panic_page
, PAGE_SIZE
,
1192 hyperv_report_panic_msg(panic_pa
, bytes_written
);
1195 static struct kmsg_dumper hv_kmsg_dumper
= {
1196 .dump
= hv_kmsg_dump
,
1199 static struct ctl_table_header
*hv_ctl_table_hdr
;
1202 * sysctl option to allow the user to control whether kmsg data should be
1203 * reported to Hyper-V on panic.
1205 static struct ctl_table hv_ctl_table
[] = {
1207 .procname
= "hyperv_record_panic_msg",
1208 .data
= &sysctl_record_panic_msg
,
1209 .maxlen
= sizeof(int),
1211 .proc_handler
= proc_dointvec_minmax
,
1212 .extra1
= SYSCTL_ZERO
,
1213 .extra2
= SYSCTL_ONE
1218 static struct ctl_table hv_root_table
[] = {
1220 .procname
= "kernel",
1222 .child
= hv_ctl_table
1228 * vmbus_bus_init -Main vmbus driver initialization routine.
1231 * - initialize the vmbus driver context
1232 * - invoke the vmbus hv main init routine
1233 * - retrieve the channel offers
1235 static int vmbus_bus_init(void)
1239 /* Hypervisor initialization...setup hypercall page..etc */
1242 pr_err("Unable to initialize the hypervisor - 0x%x\n", ret
);
1246 ret
= bus_register(&hv_bus
);
1250 hv_setup_vmbus_irq(vmbus_isr
);
1252 ret
= hv_synic_alloc();
1256 ret
= hv_stimer_alloc(VMBUS_MESSAGE_SINT
);
1261 * Initialize the per-cpu interrupt state and stimer state.
1262 * Then connect to the host.
1264 ret
= cpuhp_setup_state(CPUHP_AP_ONLINE_DYN
, "hyperv/vmbus:online",
1265 hv_synic_init
, hv_synic_cleanup
);
1268 hyperv_cpuhp_online
= ret
;
1270 ret
= vmbus_connect();
1275 * Only register if the crash MSRs are available
1277 if (ms_hyperv
.misc_features
& HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE
) {
1278 u64 hyperv_crash_ctl
;
1280 * Sysctl registration is not fatal, since by default
1281 * reporting is enabled.
1283 hv_ctl_table_hdr
= register_sysctl_table(hv_root_table
);
1284 if (!hv_ctl_table_hdr
)
1285 pr_err("Hyper-V: sysctl table register error");
1288 * Register for panic kmsg callback only if the right
1289 * capability is supported by the hypervisor.
1291 hv_get_crash_ctl(hyperv_crash_ctl
);
1292 if (hyperv_crash_ctl
& HV_CRASH_CTL_CRASH_NOTIFY_MSG
) {
1293 hv_panic_page
= (void *)get_zeroed_page(GFP_KERNEL
);
1294 if (hv_panic_page
) {
1295 ret
= kmsg_dump_register(&hv_kmsg_dumper
);
1297 pr_err("Hyper-V: kmsg dump register "
1298 "error 0x%x\n", ret
);
1300 pr_err("Hyper-V: panic message page memory "
1301 "allocation failed");
1304 register_die_notifier(&hyperv_die_block
);
1305 atomic_notifier_chain_register(&panic_notifier_list
,
1306 &hyperv_panic_block
);
1309 vmbus_request_offers();
1314 cpuhp_remove_state(hyperv_cpuhp_online
);
1319 hv_remove_vmbus_irq();
1321 bus_unregister(&hv_bus
);
1322 free_page((unsigned long)hv_panic_page
);
1323 unregister_sysctl_table(hv_ctl_table_hdr
);
1324 hv_ctl_table_hdr
= NULL
;
1329 * __vmbus_child_driver_register() - Register a vmbus's driver
1330 * @hv_driver: Pointer to driver structure you want to register
1331 * @owner: owner module of the drv
1332 * @mod_name: module name string
1334 * Registers the given driver with Linux through the 'driver_register()' call
1335 * and sets up the hyper-v vmbus handling for this driver.
1336 * It will return the state of the 'driver_register()' call.
1339 int __vmbus_driver_register(struct hv_driver
*hv_driver
, struct module
*owner
, const char *mod_name
)
1343 pr_info("registering driver %s\n", hv_driver
->name
);
1345 ret
= vmbus_exists();
1349 hv_driver
->driver
.name
= hv_driver
->name
;
1350 hv_driver
->driver
.owner
= owner
;
1351 hv_driver
->driver
.mod_name
= mod_name
;
1352 hv_driver
->driver
.bus
= &hv_bus
;
1354 spin_lock_init(&hv_driver
->dynids
.lock
);
1355 INIT_LIST_HEAD(&hv_driver
->dynids
.list
);
1357 ret
= driver_register(&hv_driver
->driver
);
1361 EXPORT_SYMBOL_GPL(__vmbus_driver_register
);
1364 * vmbus_driver_unregister() - Unregister a vmbus's driver
1365 * @hv_driver: Pointer to driver structure you want to
1368 * Un-register the given driver that was previous registered with a call to
1369 * vmbus_driver_register()
1371 void vmbus_driver_unregister(struct hv_driver
*hv_driver
)
1373 pr_info("unregistering driver %s\n", hv_driver
->name
);
1375 if (!vmbus_exists()) {
1376 driver_unregister(&hv_driver
->driver
);
1377 vmbus_free_dynids(hv_driver
);
1380 EXPORT_SYMBOL_GPL(vmbus_driver_unregister
);
1384 * Called when last reference to channel is gone.
1386 static void vmbus_chan_release(struct kobject
*kobj
)
1388 struct vmbus_channel
*channel
1389 = container_of(kobj
, struct vmbus_channel
, kobj
);
1391 kfree_rcu(channel
, rcu
);
1394 struct vmbus_chan_attribute
{
1395 struct attribute attr
;
1396 ssize_t (*show
)(struct vmbus_channel
*chan
, char *buf
);
1397 ssize_t (*store
)(struct vmbus_channel
*chan
,
1398 const char *buf
, size_t count
);
1400 #define VMBUS_CHAN_ATTR(_name, _mode, _show, _store) \
1401 struct vmbus_chan_attribute chan_attr_##_name \
1402 = __ATTR(_name, _mode, _show, _store)
1403 #define VMBUS_CHAN_ATTR_RW(_name) \
1404 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RW(_name)
1405 #define VMBUS_CHAN_ATTR_RO(_name) \
1406 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RO(_name)
1407 #define VMBUS_CHAN_ATTR_WO(_name) \
1408 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_WO(_name)
1410 static ssize_t
vmbus_chan_attr_show(struct kobject
*kobj
,
1411 struct attribute
*attr
, char *buf
)
1413 const struct vmbus_chan_attribute
*attribute
1414 = container_of(attr
, struct vmbus_chan_attribute
, attr
);
1415 struct vmbus_channel
*chan
1416 = container_of(kobj
, struct vmbus_channel
, kobj
);
1418 if (!attribute
->show
)
1421 return attribute
->show(chan
, buf
);
1424 static const struct sysfs_ops vmbus_chan_sysfs_ops
= {
1425 .show
= vmbus_chan_attr_show
,
1428 static ssize_t
out_mask_show(struct vmbus_channel
*channel
, char *buf
)
1430 struct hv_ring_buffer_info
*rbi
= &channel
->outbound
;
1433 mutex_lock(&rbi
->ring_buffer_mutex
);
1434 if (!rbi
->ring_buffer
) {
1435 mutex_unlock(&rbi
->ring_buffer_mutex
);
1439 ret
= sprintf(buf
, "%u\n", rbi
->ring_buffer
->interrupt_mask
);
1440 mutex_unlock(&rbi
->ring_buffer_mutex
);
1443 static VMBUS_CHAN_ATTR_RO(out_mask
);
1445 static ssize_t
in_mask_show(struct vmbus_channel
*channel
, char *buf
)
1447 struct hv_ring_buffer_info
*rbi
= &channel
->inbound
;
1450 mutex_lock(&rbi
->ring_buffer_mutex
);
1451 if (!rbi
->ring_buffer
) {
1452 mutex_unlock(&rbi
->ring_buffer_mutex
);
1456 ret
= sprintf(buf
, "%u\n", rbi
->ring_buffer
->interrupt_mask
);
1457 mutex_unlock(&rbi
->ring_buffer_mutex
);
1460 static VMBUS_CHAN_ATTR_RO(in_mask
);
1462 static ssize_t
read_avail_show(struct vmbus_channel
*channel
, char *buf
)
1464 struct hv_ring_buffer_info
*rbi
= &channel
->inbound
;
1467 mutex_lock(&rbi
->ring_buffer_mutex
);
1468 if (!rbi
->ring_buffer
) {
1469 mutex_unlock(&rbi
->ring_buffer_mutex
);
1473 ret
= sprintf(buf
, "%u\n", hv_get_bytes_to_read(rbi
));
1474 mutex_unlock(&rbi
->ring_buffer_mutex
);
1477 static VMBUS_CHAN_ATTR_RO(read_avail
);
1479 static ssize_t
write_avail_show(struct vmbus_channel
*channel
, char *buf
)
1481 struct hv_ring_buffer_info
*rbi
= &channel
->outbound
;
1484 mutex_lock(&rbi
->ring_buffer_mutex
);
1485 if (!rbi
->ring_buffer
) {
1486 mutex_unlock(&rbi
->ring_buffer_mutex
);
1490 ret
= sprintf(buf
, "%u\n", hv_get_bytes_to_write(rbi
));
1491 mutex_unlock(&rbi
->ring_buffer_mutex
);
1494 static VMBUS_CHAN_ATTR_RO(write_avail
);
1496 static ssize_t
show_target_cpu(struct vmbus_channel
*channel
, char *buf
)
1498 return sprintf(buf
, "%u\n", channel
->target_cpu
);
1500 static VMBUS_CHAN_ATTR(cpu
, S_IRUGO
, show_target_cpu
, NULL
);
1502 static ssize_t
channel_pending_show(struct vmbus_channel
*channel
,
1505 return sprintf(buf
, "%d\n",
1506 channel_pending(channel
,
1507 vmbus_connection
.monitor_pages
[1]));
1509 static VMBUS_CHAN_ATTR(pending
, S_IRUGO
, channel_pending_show
, NULL
);
1511 static ssize_t
channel_latency_show(struct vmbus_channel
*channel
,
1514 return sprintf(buf
, "%d\n",
1515 channel_latency(channel
,
1516 vmbus_connection
.monitor_pages
[1]));
1518 static VMBUS_CHAN_ATTR(latency
, S_IRUGO
, channel_latency_show
, NULL
);
1520 static ssize_t
channel_interrupts_show(struct vmbus_channel
*channel
, char *buf
)
1522 return sprintf(buf
, "%llu\n", channel
->interrupts
);
1524 static VMBUS_CHAN_ATTR(interrupts
, S_IRUGO
, channel_interrupts_show
, NULL
);
1526 static ssize_t
channel_events_show(struct vmbus_channel
*channel
, char *buf
)
1528 return sprintf(buf
, "%llu\n", channel
->sig_events
);
1530 static VMBUS_CHAN_ATTR(events
, S_IRUGO
, channel_events_show
, NULL
);
1532 static ssize_t
channel_intr_in_full_show(struct vmbus_channel
*channel
,
1535 return sprintf(buf
, "%llu\n",
1536 (unsigned long long)channel
->intr_in_full
);
1538 static VMBUS_CHAN_ATTR(intr_in_full
, 0444, channel_intr_in_full_show
, NULL
);
1540 static ssize_t
channel_intr_out_empty_show(struct vmbus_channel
*channel
,
1543 return sprintf(buf
, "%llu\n",
1544 (unsigned long long)channel
->intr_out_empty
);
1546 static VMBUS_CHAN_ATTR(intr_out_empty
, 0444, channel_intr_out_empty_show
, NULL
);
1548 static ssize_t
channel_out_full_first_show(struct vmbus_channel
*channel
,
1551 return sprintf(buf
, "%llu\n",
1552 (unsigned long long)channel
->out_full_first
);
1554 static VMBUS_CHAN_ATTR(out_full_first
, 0444, channel_out_full_first_show
, NULL
);
1556 static ssize_t
channel_out_full_total_show(struct vmbus_channel
*channel
,
1559 return sprintf(buf
, "%llu\n",
1560 (unsigned long long)channel
->out_full_total
);
1562 static VMBUS_CHAN_ATTR(out_full_total
, 0444, channel_out_full_total_show
, NULL
);
1564 static ssize_t
subchannel_monitor_id_show(struct vmbus_channel
*channel
,
1567 return sprintf(buf
, "%u\n", channel
->offermsg
.monitorid
);
1569 static VMBUS_CHAN_ATTR(monitor_id
, S_IRUGO
, subchannel_monitor_id_show
, NULL
);
1571 static ssize_t
subchannel_id_show(struct vmbus_channel
*channel
,
1574 return sprintf(buf
, "%u\n",
1575 channel
->offermsg
.offer
.sub_channel_index
);
1577 static VMBUS_CHAN_ATTR_RO(subchannel_id
);
1579 static struct attribute
*vmbus_chan_attrs
[] = {
1580 &chan_attr_out_mask
.attr
,
1581 &chan_attr_in_mask
.attr
,
1582 &chan_attr_read_avail
.attr
,
1583 &chan_attr_write_avail
.attr
,
1584 &chan_attr_cpu
.attr
,
1585 &chan_attr_pending
.attr
,
1586 &chan_attr_latency
.attr
,
1587 &chan_attr_interrupts
.attr
,
1588 &chan_attr_events
.attr
,
1589 &chan_attr_intr_in_full
.attr
,
1590 &chan_attr_intr_out_empty
.attr
,
1591 &chan_attr_out_full_first
.attr
,
1592 &chan_attr_out_full_total
.attr
,
1593 &chan_attr_monitor_id
.attr
,
1594 &chan_attr_subchannel_id
.attr
,
1599 * Channel-level attribute_group callback function. Returns the permission for
1600 * each attribute, and returns 0 if an attribute is not visible.
1602 static umode_t
vmbus_chan_attr_is_visible(struct kobject
*kobj
,
1603 struct attribute
*attr
, int idx
)
1605 const struct vmbus_channel
*channel
=
1606 container_of(kobj
, struct vmbus_channel
, kobj
);
1608 /* Hide the monitor attributes if the monitor mechanism is not used. */
1609 if (!channel
->offermsg
.monitor_allocated
&&
1610 (attr
== &chan_attr_pending
.attr
||
1611 attr
== &chan_attr_latency
.attr
||
1612 attr
== &chan_attr_monitor_id
.attr
))
1618 static struct attribute_group vmbus_chan_group
= {
1619 .attrs
= vmbus_chan_attrs
,
1620 .is_visible
= vmbus_chan_attr_is_visible
1623 static struct kobj_type vmbus_chan_ktype
= {
1624 .sysfs_ops
= &vmbus_chan_sysfs_ops
,
1625 .release
= vmbus_chan_release
,
1629 * vmbus_add_channel_kobj - setup a sub-directory under device/channels
1631 int vmbus_add_channel_kobj(struct hv_device
*dev
, struct vmbus_channel
*channel
)
1633 const struct device
*device
= &dev
->device
;
1634 struct kobject
*kobj
= &channel
->kobj
;
1635 u32 relid
= channel
->offermsg
.child_relid
;
1638 kobj
->kset
= dev
->channels_kset
;
1639 ret
= kobject_init_and_add(kobj
, &vmbus_chan_ktype
, NULL
,
1644 ret
= sysfs_create_group(kobj
, &vmbus_chan_group
);
1648 * The calling functions' error handling paths will cleanup the
1649 * empty channel directory.
1651 dev_err(device
, "Unable to set up channel sysfs files\n");
1655 kobject_uevent(kobj
, KOBJ_ADD
);
1661 * vmbus_remove_channel_attr_group - remove the channel's attribute group
1663 void vmbus_remove_channel_attr_group(struct vmbus_channel
*channel
)
1665 sysfs_remove_group(&channel
->kobj
, &vmbus_chan_group
);
1669 * vmbus_device_create - Creates and registers a new child device
1672 struct hv_device
*vmbus_device_create(const guid_t
*type
,
1673 const guid_t
*instance
,
1674 struct vmbus_channel
*channel
)
1676 struct hv_device
*child_device_obj
;
1678 child_device_obj
= kzalloc(sizeof(struct hv_device
), GFP_KERNEL
);
1679 if (!child_device_obj
) {
1680 pr_err("Unable to allocate device object for child device\n");
1684 child_device_obj
->channel
= channel
;
1685 guid_copy(&child_device_obj
->dev_type
, type
);
1686 guid_copy(&child_device_obj
->dev_instance
, instance
);
1687 child_device_obj
->vendor_id
= 0x1414; /* MSFT vendor ID */
1689 return child_device_obj
;
1693 * vmbus_device_register - Register the child device
1695 int vmbus_device_register(struct hv_device
*child_device_obj
)
1697 struct kobject
*kobj
= &child_device_obj
->device
.kobj
;
1700 dev_set_name(&child_device_obj
->device
, "%pUl",
1701 child_device_obj
->channel
->offermsg
.offer
.if_instance
.b
);
1703 child_device_obj
->device
.bus
= &hv_bus
;
1704 child_device_obj
->device
.parent
= &hv_acpi_dev
->dev
;
1705 child_device_obj
->device
.release
= vmbus_device_release
;
1708 * Register with the LDM. This will kick off the driver/device
1709 * binding...which will eventually call vmbus_match() and vmbus_probe()
1711 ret
= device_register(&child_device_obj
->device
);
1713 pr_err("Unable to register child device\n");
1717 child_device_obj
->channels_kset
= kset_create_and_add("channels",
1719 if (!child_device_obj
->channels_kset
) {
1721 goto err_dev_unregister
;
1724 ret
= vmbus_add_channel_kobj(child_device_obj
,
1725 child_device_obj
->channel
);
1727 pr_err("Unable to register primary channeln");
1728 goto err_kset_unregister
;
1733 err_kset_unregister
:
1734 kset_unregister(child_device_obj
->channels_kset
);
1737 device_unregister(&child_device_obj
->device
);
1742 * vmbus_device_unregister - Remove the specified child device
1745 void vmbus_device_unregister(struct hv_device
*device_obj
)
1747 pr_debug("child device %s unregistered\n",
1748 dev_name(&device_obj
->device
));
1750 kset_unregister(device_obj
->channels_kset
);
1753 * Kick off the process of unregistering the device.
1754 * This will call vmbus_remove() and eventually vmbus_device_release()
1756 device_unregister(&device_obj
->device
);
1761 * VMBUS is an acpi enumerated device. Get the information we
1764 #define VTPM_BASE_ADDRESS 0xfed40000
1765 static acpi_status
vmbus_walk_resources(struct acpi_resource
*res
, void *ctx
)
1767 resource_size_t start
= 0;
1768 resource_size_t end
= 0;
1769 struct resource
*new_res
;
1770 struct resource
**old_res
= &hyperv_mmio
;
1771 struct resource
**prev_res
= NULL
;
1773 switch (res
->type
) {
1776 * "Address" descriptors are for bus windows. Ignore
1777 * "memory" descriptors, which are for registers on
1780 case ACPI_RESOURCE_TYPE_ADDRESS32
:
1781 start
= res
->data
.address32
.address
.minimum
;
1782 end
= res
->data
.address32
.address
.maximum
;
1785 case ACPI_RESOURCE_TYPE_ADDRESS64
:
1786 start
= res
->data
.address64
.address
.minimum
;
1787 end
= res
->data
.address64
.address
.maximum
;
1791 /* Unused resource type */
1796 * Ignore ranges that are below 1MB, as they're not
1797 * necessary or useful here.
1802 new_res
= kzalloc(sizeof(*new_res
), GFP_ATOMIC
);
1804 return AE_NO_MEMORY
;
1806 /* If this range overlaps the virtual TPM, truncate it. */
1807 if (end
> VTPM_BASE_ADDRESS
&& start
< VTPM_BASE_ADDRESS
)
1808 end
= VTPM_BASE_ADDRESS
;
1810 new_res
->name
= "hyperv mmio";
1811 new_res
->flags
= IORESOURCE_MEM
;
1812 new_res
->start
= start
;
1816 * If two ranges are adjacent, merge them.
1824 if (((*old_res
)->end
+ 1) == new_res
->start
) {
1825 (*old_res
)->end
= new_res
->end
;
1830 if ((*old_res
)->start
== new_res
->end
+ 1) {
1831 (*old_res
)->start
= new_res
->start
;
1836 if ((*old_res
)->start
> new_res
->end
) {
1837 new_res
->sibling
= *old_res
;
1839 (*prev_res
)->sibling
= new_res
;
1845 old_res
= &(*old_res
)->sibling
;
1852 static int vmbus_acpi_remove(struct acpi_device
*device
)
1854 struct resource
*cur_res
;
1855 struct resource
*next_res
;
1859 __release_region(hyperv_mmio
, fb_mmio
->start
,
1860 resource_size(fb_mmio
));
1864 for (cur_res
= hyperv_mmio
; cur_res
; cur_res
= next_res
) {
1865 next_res
= cur_res
->sibling
;
1873 static void vmbus_reserve_fb(void)
1877 * Make a claim for the frame buffer in the resource tree under the
1878 * first node, which will be the one below 4GB. The length seems to
1879 * be underreported, particularly in a Generation 1 VM. So start out
1880 * reserving a larger area and make it smaller until it succeeds.
1883 if (screen_info
.lfb_base
) {
1884 if (efi_enabled(EFI_BOOT
))
1885 size
= max_t(__u32
, screen_info
.lfb_size
, 0x800000);
1887 size
= max_t(__u32
, screen_info
.lfb_size
, 0x4000000);
1889 for (; !fb_mmio
&& (size
>= 0x100000); size
>>= 1) {
1890 fb_mmio
= __request_region(hyperv_mmio
,
1891 screen_info
.lfb_base
, size
,
1898 * vmbus_allocate_mmio() - Pick a memory-mapped I/O range.
1899 * @new: If successful, supplied a pointer to the
1900 * allocated MMIO space.
1901 * @device_obj: Identifies the caller
1902 * @min: Minimum guest physical address of the
1904 * @max: Maximum guest physical address
1905 * @size: Size of the range to be allocated
1906 * @align: Alignment of the range to be allocated
1907 * @fb_overlap_ok: Whether this allocation can be allowed
1908 * to overlap the video frame buffer.
1910 * This function walks the resources granted to VMBus by the
1911 * _CRS object in the ACPI namespace underneath the parent
1912 * "bridge" whether that's a root PCI bus in the Generation 1
1913 * case or a Module Device in the Generation 2 case. It then
1914 * attempts to allocate from the global MMIO pool in a way that
1915 * matches the constraints supplied in these parameters and by
1918 * Return: 0 on success, -errno on failure
1920 int vmbus_allocate_mmio(struct resource
**new, struct hv_device
*device_obj
,
1921 resource_size_t min
, resource_size_t max
,
1922 resource_size_t size
, resource_size_t align
,
1925 struct resource
*iter
, *shadow
;
1926 resource_size_t range_min
, range_max
, start
;
1927 const char *dev_n
= dev_name(&device_obj
->device
);
1931 down(&hyperv_mmio_lock
);
1934 * If overlaps with frame buffers are allowed, then first attempt to
1935 * make the allocation from within the reserved region. Because it
1936 * is already reserved, no shadow allocation is necessary.
1938 if (fb_overlap_ok
&& fb_mmio
&& !(min
> fb_mmio
->end
) &&
1939 !(max
< fb_mmio
->start
)) {
1941 range_min
= fb_mmio
->start
;
1942 range_max
= fb_mmio
->end
;
1943 start
= (range_min
+ align
- 1) & ~(align
- 1);
1944 for (; start
+ size
- 1 <= range_max
; start
+= align
) {
1945 *new = request_mem_region_exclusive(start
, size
, dev_n
);
1953 for (iter
= hyperv_mmio
; iter
; iter
= iter
->sibling
) {
1954 if ((iter
->start
>= max
) || (iter
->end
<= min
))
1957 range_min
= iter
->start
;
1958 range_max
= iter
->end
;
1959 start
= (range_min
+ align
- 1) & ~(align
- 1);
1960 for (; start
+ size
- 1 <= range_max
; start
+= align
) {
1961 shadow
= __request_region(iter
, start
, size
, NULL
,
1966 *new = request_mem_region_exclusive(start
, size
, dev_n
);
1968 shadow
->name
= (char *)*new;
1973 __release_region(iter
, start
, size
);
1978 up(&hyperv_mmio_lock
);
1981 EXPORT_SYMBOL_GPL(vmbus_allocate_mmio
);
1984 * vmbus_free_mmio() - Free a memory-mapped I/O range.
1985 * @start: Base address of region to release.
1986 * @size: Size of the range to be allocated
1988 * This function releases anything requested by
1989 * vmbus_mmio_allocate().
1991 void vmbus_free_mmio(resource_size_t start
, resource_size_t size
)
1993 struct resource
*iter
;
1995 down(&hyperv_mmio_lock
);
1996 for (iter
= hyperv_mmio
; iter
; iter
= iter
->sibling
) {
1997 if ((iter
->start
>= start
+ size
) || (iter
->end
<= start
))
2000 __release_region(iter
, start
, size
);
2002 release_mem_region(start
, size
);
2003 up(&hyperv_mmio_lock
);
2006 EXPORT_SYMBOL_GPL(vmbus_free_mmio
);
2008 static int vmbus_acpi_add(struct acpi_device
*device
)
2011 int ret_val
= -ENODEV
;
2012 struct acpi_device
*ancestor
;
2014 hv_acpi_dev
= device
;
2016 result
= acpi_walk_resources(device
->handle
, METHOD_NAME__CRS
,
2017 vmbus_walk_resources
, NULL
);
2019 if (ACPI_FAILURE(result
))
2022 * Some ancestor of the vmbus acpi device (Gen1 or Gen2
2023 * firmware) is the VMOD that has the mmio ranges. Get that.
2025 for (ancestor
= device
->parent
; ancestor
; ancestor
= ancestor
->parent
) {
2026 result
= acpi_walk_resources(ancestor
->handle
, METHOD_NAME__CRS
,
2027 vmbus_walk_resources
, NULL
);
2029 if (ACPI_FAILURE(result
))
2039 complete(&probe_event
);
2041 vmbus_acpi_remove(device
);
2045 static const struct acpi_device_id vmbus_acpi_device_ids
[] = {
2050 MODULE_DEVICE_TABLE(acpi
, vmbus_acpi_device_ids
);
2052 static struct acpi_driver vmbus_acpi_driver
= {
2054 .ids
= vmbus_acpi_device_ids
,
2056 .add
= vmbus_acpi_add
,
2057 .remove
= vmbus_acpi_remove
,
2061 static void hv_kexec_handler(void)
2063 hv_stimer_global_cleanup();
2064 vmbus_initiate_unload(false);
2065 vmbus_connection
.conn_state
= DISCONNECTED
;
2066 /* Make sure conn_state is set as hv_synic_cleanup checks for it */
2068 cpuhp_remove_state(hyperv_cpuhp_online
);
2072 static void hv_crash_handler(struct pt_regs
*regs
)
2076 vmbus_initiate_unload(true);
2078 * In crash handler we can't schedule synic cleanup for all CPUs,
2079 * doing the cleanup for current CPU only. This should be sufficient
2082 vmbus_connection
.conn_state
= DISCONNECTED
;
2083 cpu
= smp_processor_id();
2084 hv_stimer_cleanup(cpu
);
2085 hv_synic_cleanup(cpu
);
2089 static int __init
hv_acpi_init(void)
2093 if (!hv_is_hyperv_initialized())
2096 init_completion(&probe_event
);
2099 * Get ACPI resources first.
2101 ret
= acpi_bus_register_driver(&vmbus_acpi_driver
);
2106 t
= wait_for_completion_timeout(&probe_event
, 5*HZ
);
2112 ret
= vmbus_bus_init();
2116 hv_setup_kexec_handler(hv_kexec_handler
);
2117 hv_setup_crash_handler(hv_crash_handler
);
2122 acpi_bus_unregister_driver(&vmbus_acpi_driver
);
2127 static void __exit
vmbus_exit(void)
2131 hv_remove_kexec_handler();
2132 hv_remove_crash_handler();
2133 vmbus_connection
.conn_state
= DISCONNECTED
;
2134 hv_stimer_global_cleanup();
2136 hv_remove_vmbus_irq();
2137 for_each_online_cpu(cpu
) {
2138 struct hv_per_cpu_context
*hv_cpu
2139 = per_cpu_ptr(hv_context
.cpu_context
, cpu
);
2141 tasklet_kill(&hv_cpu
->msg_dpc
);
2143 vmbus_free_channels();
2145 if (ms_hyperv
.misc_features
& HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE
) {
2146 kmsg_dump_unregister(&hv_kmsg_dumper
);
2147 unregister_die_notifier(&hyperv_die_block
);
2148 atomic_notifier_chain_unregister(&panic_notifier_list
,
2149 &hyperv_panic_block
);
2152 free_page((unsigned long)hv_panic_page
);
2153 unregister_sysctl_table(hv_ctl_table_hdr
);
2154 hv_ctl_table_hdr
= NULL
;
2155 bus_unregister(&hv_bus
);
2157 cpuhp_remove_state(hyperv_cpuhp_online
);
2159 acpi_bus_unregister_driver(&vmbus_acpi_driver
);
2163 MODULE_LICENSE("GPL");
2164 MODULE_DESCRIPTION("Microsoft Hyper-V VMBus Driver");
2166 subsys_initcall(hv_acpi_init
);
2167 module_exit(vmbus_exit
);