2 * Copyright (c) 2009, Microsoft Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
20 * K. Y. Srinivasan <kys@microsoft.com>
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/device.h>
28 #include <linux/interrupt.h>
29 #include <linux/sysctl.h>
30 #include <linux/slab.h>
31 #include <linux/acpi.h>
32 #include <linux/completion.h>
33 #include <linux/hyperv.h>
34 #include <linux/kernel_stat.h>
35 #include <linux/clockchips.h>
36 #include <linux/cpu.h>
37 #include <asm/hyperv.h>
38 #include <asm/hypervisor.h>
39 #include <asm/mshyperv.h>
40 #include <linux/notifier.h>
41 #include <linux/ptrace.h>
42 #include <linux/kdebug.h>
43 #include "hyperv_vmbus.h"
45 static struct acpi_device *hv_acpi_dev;
47 static struct tasklet_struct msg_dpc;
48 static struct completion probe_event;
52 static void hyperv_report_panic(struct pt_regs *regs)
54 static bool panic_reported;
57 * We prefer to report panic on 'die' chain as we have proper
58 * registers to report, but if we miss it (e.g. on BUG()) we need
59 * to report it on 'panic'.
63 panic_reported = true;
65 wrmsrl(HV_X64_MSR_CRASH_P0, regs->ip);
66 wrmsrl(HV_X64_MSR_CRASH_P1, regs->ax);
67 wrmsrl(HV_X64_MSR_CRASH_P2, regs->bx);
68 wrmsrl(HV_X64_MSR_CRASH_P3, regs->cx);
69 wrmsrl(HV_X64_MSR_CRASH_P4, regs->dx);
72 * Let Hyper-V know there is crash data available
74 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
77 static int hyperv_panic_event(struct notifier_block *nb, unsigned long val,
82 regs = current_pt_regs();
84 hyperv_report_panic(regs);
88 static int hyperv_die_event(struct notifier_block *nb, unsigned long val,
91 struct die_args *die = (struct die_args *)args;
92 struct pt_regs *regs = die->regs;
94 hyperv_report_panic(regs);
98 static struct notifier_block hyperv_die_block = {
99 .notifier_call = hyperv_die_event,
101 static struct notifier_block hyperv_panic_block = {
102 .notifier_call = hyperv_panic_event,
105 struct resource hyperv_mmio = {
106 .name = "hyperv mmio",
107 .flags = IORESOURCE_MEM,
109 EXPORT_SYMBOL_GPL(hyperv_mmio);
111 static int vmbus_exists(void)
113 if (hv_acpi_dev == NULL)
119 #define VMBUS_ALIAS_LEN ((sizeof((struct hv_vmbus_device_id *)0)->guid) * 2)
120 static void print_alias_name(struct hv_device *hv_dev, char *alias_name)
123 for (i = 0; i < VMBUS_ALIAS_LEN; i += 2)
124 sprintf(&alias_name[i], "%02x", hv_dev->dev_type.b[i/2]);
127 static u8 channel_monitor_group(struct vmbus_channel *channel)
129 return (u8)channel->offermsg.monitorid / 32;
132 static u8 channel_monitor_offset(struct vmbus_channel *channel)
134 return (u8)channel->offermsg.monitorid % 32;
137 static u32 channel_pending(struct vmbus_channel *channel,
138 struct hv_monitor_page *monitor_page)
140 u8 monitor_group = channel_monitor_group(channel);
141 return monitor_page->trigger_group[monitor_group].pending;
144 static u32 channel_latency(struct vmbus_channel *channel,
145 struct hv_monitor_page *monitor_page)
147 u8 monitor_group = channel_monitor_group(channel);
148 u8 monitor_offset = channel_monitor_offset(channel);
149 return monitor_page->latency[monitor_group][monitor_offset];
152 static u32 channel_conn_id(struct vmbus_channel *channel,
153 struct hv_monitor_page *monitor_page)
155 u8 monitor_group = channel_monitor_group(channel);
156 u8 monitor_offset = channel_monitor_offset(channel);
157 return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
160 static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
163 struct hv_device *hv_dev = device_to_hv_device(dev);
165 if (!hv_dev->channel)
167 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
169 static DEVICE_ATTR_RO(id);
171 static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
174 struct hv_device *hv_dev = device_to_hv_device(dev);
176 if (!hv_dev->channel)
178 return sprintf(buf, "%d\n", hv_dev->channel->state);
180 static DEVICE_ATTR_RO(state);
182 static ssize_t monitor_id_show(struct device *dev,
183 struct device_attribute *dev_attr, char *buf)
185 struct hv_device *hv_dev = device_to_hv_device(dev);
187 if (!hv_dev->channel)
189 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
191 static DEVICE_ATTR_RO(monitor_id);
193 static ssize_t class_id_show(struct device *dev,
194 struct device_attribute *dev_attr, char *buf)
196 struct hv_device *hv_dev = device_to_hv_device(dev);
198 if (!hv_dev->channel)
200 return sprintf(buf, "{%pUl}\n",
201 hv_dev->channel->offermsg.offer.if_type.b);
203 static DEVICE_ATTR_RO(class_id);
205 static ssize_t device_id_show(struct device *dev,
206 struct device_attribute *dev_attr, char *buf)
208 struct hv_device *hv_dev = device_to_hv_device(dev);
210 if (!hv_dev->channel)
212 return sprintf(buf, "{%pUl}\n",
213 hv_dev->channel->offermsg.offer.if_instance.b);
215 static DEVICE_ATTR_RO(device_id);
217 static ssize_t modalias_show(struct device *dev,
218 struct device_attribute *dev_attr, char *buf)
220 struct hv_device *hv_dev = device_to_hv_device(dev);
221 char alias_name[VMBUS_ALIAS_LEN + 1];
223 print_alias_name(hv_dev, alias_name);
224 return sprintf(buf, "vmbus:%s\n", alias_name);
226 static DEVICE_ATTR_RO(modalias);
228 static ssize_t server_monitor_pending_show(struct device *dev,
229 struct device_attribute *dev_attr,
232 struct hv_device *hv_dev = device_to_hv_device(dev);
234 if (!hv_dev->channel)
236 return sprintf(buf, "%d\n",
237 channel_pending(hv_dev->channel,
238 vmbus_connection.monitor_pages[1]));
240 static DEVICE_ATTR_RO(server_monitor_pending);
242 static ssize_t client_monitor_pending_show(struct device *dev,
243 struct device_attribute *dev_attr,
246 struct hv_device *hv_dev = device_to_hv_device(dev);
248 if (!hv_dev->channel)
250 return sprintf(buf, "%d\n",
251 channel_pending(hv_dev->channel,
252 vmbus_connection.monitor_pages[1]));
254 static DEVICE_ATTR_RO(client_monitor_pending);
256 static ssize_t server_monitor_latency_show(struct device *dev,
257 struct device_attribute *dev_attr,
260 struct hv_device *hv_dev = device_to_hv_device(dev);
262 if (!hv_dev->channel)
264 return sprintf(buf, "%d\n",
265 channel_latency(hv_dev->channel,
266 vmbus_connection.monitor_pages[0]));
268 static DEVICE_ATTR_RO(server_monitor_latency);
270 static ssize_t client_monitor_latency_show(struct device *dev,
271 struct device_attribute *dev_attr,
274 struct hv_device *hv_dev = device_to_hv_device(dev);
276 if (!hv_dev->channel)
278 return sprintf(buf, "%d\n",
279 channel_latency(hv_dev->channel,
280 vmbus_connection.monitor_pages[1]));
282 static DEVICE_ATTR_RO(client_monitor_latency);
284 static ssize_t server_monitor_conn_id_show(struct device *dev,
285 struct device_attribute *dev_attr,
288 struct hv_device *hv_dev = device_to_hv_device(dev);
290 if (!hv_dev->channel)
292 return sprintf(buf, "%d\n",
293 channel_conn_id(hv_dev->channel,
294 vmbus_connection.monitor_pages[0]));
296 static DEVICE_ATTR_RO(server_monitor_conn_id);
298 static ssize_t client_monitor_conn_id_show(struct device *dev,
299 struct device_attribute *dev_attr,
302 struct hv_device *hv_dev = device_to_hv_device(dev);
304 if (!hv_dev->channel)
306 return sprintf(buf, "%d\n",
307 channel_conn_id(hv_dev->channel,
308 vmbus_connection.monitor_pages[1]));
310 static DEVICE_ATTR_RO(client_monitor_conn_id);
312 static ssize_t out_intr_mask_show(struct device *dev,
313 struct device_attribute *dev_attr, char *buf)
315 struct hv_device *hv_dev = device_to_hv_device(dev);
316 struct hv_ring_buffer_debug_info outbound;
318 if (!hv_dev->channel)
320 hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
321 return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
323 static DEVICE_ATTR_RO(out_intr_mask);
325 static ssize_t out_read_index_show(struct device *dev,
326 struct device_attribute *dev_attr, char *buf)
328 struct hv_device *hv_dev = device_to_hv_device(dev);
329 struct hv_ring_buffer_debug_info outbound;
331 if (!hv_dev->channel)
333 hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
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,
342 struct hv_device *hv_dev = device_to_hv_device(dev);
343 struct hv_ring_buffer_debug_info outbound;
345 if (!hv_dev->channel)
347 hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
348 return sprintf(buf, "%d\n", outbound.current_write_index);
350 static DEVICE_ATTR_RO(out_write_index);
352 static ssize_t out_read_bytes_avail_show(struct device *dev,
353 struct device_attribute *dev_attr,
356 struct hv_device *hv_dev = device_to_hv_device(dev);
357 struct hv_ring_buffer_debug_info outbound;
359 if (!hv_dev->channel)
361 hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
362 return sprintf(buf, "%d\n", outbound.bytes_avail_toread);
364 static DEVICE_ATTR_RO(out_read_bytes_avail);
366 static ssize_t out_write_bytes_avail_show(struct device *dev,
367 struct device_attribute *dev_attr,
370 struct hv_device *hv_dev = device_to_hv_device(dev);
371 struct hv_ring_buffer_debug_info outbound;
373 if (!hv_dev->channel)
375 hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
376 return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
378 static DEVICE_ATTR_RO(out_write_bytes_avail);
380 static ssize_t in_intr_mask_show(struct device *dev,
381 struct device_attribute *dev_attr, char *buf)
383 struct hv_device *hv_dev = device_to_hv_device(dev);
384 struct hv_ring_buffer_debug_info inbound;
386 if (!hv_dev->channel)
388 hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
389 return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
391 static DEVICE_ATTR_RO(in_intr_mask);
393 static ssize_t in_read_index_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;
399 if (!hv_dev->channel)
401 hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
402 return sprintf(buf, "%d\n", inbound.current_read_index);
404 static DEVICE_ATTR_RO(in_read_index);
406 static ssize_t in_write_index_show(struct device *dev,
407 struct device_attribute *dev_attr, char *buf)
409 struct hv_device *hv_dev = device_to_hv_device(dev);
410 struct hv_ring_buffer_debug_info inbound;
412 if (!hv_dev->channel)
414 hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
415 return sprintf(buf, "%d\n", inbound.current_write_index);
417 static DEVICE_ATTR_RO(in_write_index);
419 static ssize_t in_read_bytes_avail_show(struct device *dev,
420 struct device_attribute *dev_attr,
423 struct hv_device *hv_dev = device_to_hv_device(dev);
424 struct hv_ring_buffer_debug_info inbound;
426 if (!hv_dev->channel)
428 hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
429 return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
431 static DEVICE_ATTR_RO(in_read_bytes_avail);
433 static ssize_t in_write_bytes_avail_show(struct device *dev,
434 struct device_attribute *dev_attr,
437 struct hv_device *hv_dev = device_to_hv_device(dev);
438 struct hv_ring_buffer_debug_info inbound;
440 if (!hv_dev->channel)
442 hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
443 return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
445 static DEVICE_ATTR_RO(in_write_bytes_avail);
447 /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
448 static struct attribute *vmbus_attrs[] = {
450 &dev_attr_state.attr,
451 &dev_attr_monitor_id.attr,
452 &dev_attr_class_id.attr,
453 &dev_attr_device_id.attr,
454 &dev_attr_modalias.attr,
455 &dev_attr_server_monitor_pending.attr,
456 &dev_attr_client_monitor_pending.attr,
457 &dev_attr_server_monitor_latency.attr,
458 &dev_attr_client_monitor_latency.attr,
459 &dev_attr_server_monitor_conn_id.attr,
460 &dev_attr_client_monitor_conn_id.attr,
461 &dev_attr_out_intr_mask.attr,
462 &dev_attr_out_read_index.attr,
463 &dev_attr_out_write_index.attr,
464 &dev_attr_out_read_bytes_avail.attr,
465 &dev_attr_out_write_bytes_avail.attr,
466 &dev_attr_in_intr_mask.attr,
467 &dev_attr_in_read_index.attr,
468 &dev_attr_in_write_index.attr,
469 &dev_attr_in_read_bytes_avail.attr,
470 &dev_attr_in_write_bytes_avail.attr,
473 ATTRIBUTE_GROUPS(vmbus);
476 * vmbus_uevent - add uevent for our device
478 * This routine is invoked when a device is added or removed on the vmbus to
479 * generate a uevent to udev in the userspace. The udev will then look at its
480 * rule and the uevent generated here to load the appropriate driver
482 * The alias string will be of the form vmbus:guid where guid is the string
483 * representation of the device guid (each byte of the guid will be
484 * represented with two hex characters.
486 static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
488 struct hv_device *dev = device_to_hv_device(device);
490 char alias_name[VMBUS_ALIAS_LEN + 1];
492 print_alias_name(dev, alias_name);
493 ret = add_uevent_var(env, "MODALIAS=vmbus:%s", alias_name);
497 static const uuid_le null_guid;
499 static inline bool is_null_guid(const __u8 *guid)
501 if (memcmp(guid, &null_guid, sizeof(uuid_le)))
507 * Return a matching hv_vmbus_device_id pointer.
508 * If there is no match, return NULL.
510 static const struct hv_vmbus_device_id *hv_vmbus_get_id(
511 const struct hv_vmbus_device_id *id,
514 for (; !is_null_guid(id->guid); id++)
515 if (!memcmp(&id->guid, guid, sizeof(uuid_le)))
524 * vmbus_match - Attempt to match the specified device to the specified driver
526 static int vmbus_match(struct device *device, struct device_driver *driver)
528 struct hv_driver *drv = drv_to_hv_drv(driver);
529 struct hv_device *hv_dev = device_to_hv_device(device);
531 if (hv_vmbus_get_id(drv->id_table, hv_dev->dev_type.b))
538 * vmbus_probe - Add the new vmbus's child device
540 static int vmbus_probe(struct device *child_device)
543 struct hv_driver *drv =
544 drv_to_hv_drv(child_device->driver);
545 struct hv_device *dev = device_to_hv_device(child_device);
546 const struct hv_vmbus_device_id *dev_id;
548 dev_id = hv_vmbus_get_id(drv->id_table, dev->dev_type.b);
550 ret = drv->probe(dev, dev_id);
552 pr_err("probe failed for device %s (%d)\n",
553 dev_name(child_device), ret);
556 pr_err("probe not set for driver %s\n",
557 dev_name(child_device));
564 * vmbus_remove - Remove a vmbus device
566 static int vmbus_remove(struct device *child_device)
568 struct hv_driver *drv;
569 struct hv_device *dev = device_to_hv_device(child_device);
570 u32 relid = dev->channel->offermsg.child_relid;
572 if (child_device->driver) {
573 drv = drv_to_hv_drv(child_device->driver);
577 hv_process_channel_removal(dev->channel, relid);
578 pr_err("remove not set for driver %s\n",
579 dev_name(child_device));
583 * We don't have a driver for this device; deal with the
584 * rescind message by removing the channel.
586 hv_process_channel_removal(dev->channel, relid);
594 * vmbus_shutdown - Shutdown a vmbus device
596 static void vmbus_shutdown(struct device *child_device)
598 struct hv_driver *drv;
599 struct hv_device *dev = device_to_hv_device(child_device);
602 /* The device may not be attached yet */
603 if (!child_device->driver)
606 drv = drv_to_hv_drv(child_device->driver);
616 * vmbus_device_release - Final callback release of the vmbus child device
618 static void vmbus_device_release(struct device *device)
620 struct hv_device *hv_dev = device_to_hv_device(device);
626 /* The one and only one */
627 static struct bus_type hv_bus = {
629 .match = vmbus_match,
630 .shutdown = vmbus_shutdown,
631 .remove = vmbus_remove,
632 .probe = vmbus_probe,
633 .uevent = vmbus_uevent,
634 .dev_groups = vmbus_groups,
637 struct onmessage_work_context {
638 struct work_struct work;
639 struct hv_message msg;
642 static void vmbus_onmessage_work(struct work_struct *work)
644 struct onmessage_work_context *ctx;
646 /* Do not process messages if we're in DISCONNECTED state */
647 if (vmbus_connection.conn_state == DISCONNECTED)
650 ctx = container_of(work, struct onmessage_work_context,
652 vmbus_onmessage(&ctx->msg);
656 static void hv_process_timer_expiration(struct hv_message *msg, int cpu)
658 struct clock_event_device *dev = hv_context.clk_evt[cpu];
660 if (dev->event_handler)
661 dev->event_handler(dev);
663 msg->header.message_type = HVMSG_NONE;
666 * Make sure the write to MessageType (ie set to
667 * HVMSG_NONE) happens before we read the
668 * MessagePending and EOMing. Otherwise, the EOMing
669 * will not deliver any more messages since there is
674 if (msg->header.message_flags.msg_pending) {
676 * This will cause message queue rescan to
677 * possibly deliver another msg from the
680 wrmsrl(HV_X64_MSR_EOM, 0);
684 static void vmbus_on_msg_dpc(unsigned long data)
686 int cpu = smp_processor_id();
687 void *page_addr = hv_context.synic_message_page[cpu];
688 struct hv_message *msg = (struct hv_message *)page_addr +
690 struct vmbus_channel_message_header *hdr;
691 struct vmbus_channel_message_table_entry *entry;
692 struct onmessage_work_context *ctx;
695 if (msg->header.message_type == HVMSG_NONE)
699 hdr = (struct vmbus_channel_message_header *)msg->u.payload;
701 if (hdr->msgtype >= CHANNELMSG_COUNT) {
702 WARN_ONCE(1, "unknown msgtype=%d\n", hdr->msgtype);
706 entry = &channel_message_table[hdr->msgtype];
707 if (entry->handler_type == VMHT_BLOCKING) {
708 ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
712 INIT_WORK(&ctx->work, vmbus_onmessage_work);
713 memcpy(&ctx->msg, msg, sizeof(*msg));
715 queue_work(vmbus_connection.work_queue, &ctx->work);
717 entry->message_handler(hdr);
720 msg->header.message_type = HVMSG_NONE;
723 * Make sure the write to MessageType (ie set to
724 * HVMSG_NONE) happens before we read the
725 * MessagePending and EOMing. Otherwise, the EOMing
726 * will not deliver any more messages since there is
731 if (msg->header.message_flags.msg_pending) {
733 * This will cause message queue rescan to
734 * possibly deliver another msg from the
737 wrmsrl(HV_X64_MSR_EOM, 0);
742 static void vmbus_isr(void)
744 int cpu = smp_processor_id();
746 struct hv_message *msg;
747 union hv_synic_event_flags *event;
748 bool handled = false;
750 page_addr = hv_context.synic_event_page[cpu];
751 if (page_addr == NULL)
754 event = (union hv_synic_event_flags *)page_addr +
757 * Check for events before checking for messages. This is the order
758 * in which events and messages are checked in Windows guests on
759 * Hyper-V, and the Windows team suggested we do the same.
762 if ((vmbus_proto_version == VERSION_WS2008) ||
763 (vmbus_proto_version == VERSION_WIN7)) {
765 /* Since we are a child, we only need to check bit 0 */
766 if (sync_test_and_clear_bit(0,
767 (unsigned long *) &event->flags32[0])) {
772 * Our host is win8 or above. The signaling mechanism
773 * has changed and we can directly look at the event page.
774 * If bit n is set then we have an interrup on the channel
781 tasklet_schedule(hv_context.event_dpc[cpu]);
784 page_addr = hv_context.synic_message_page[cpu];
785 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
787 /* Check if there are actual msgs to be processed */
788 if (msg->header.message_type != HVMSG_NONE) {
789 if (msg->header.message_type == HVMSG_TIMER_EXPIRED)
790 hv_process_timer_expiration(msg, cpu);
792 tasklet_schedule(&msg_dpc);
796 #ifdef CONFIG_HOTPLUG_CPU
797 static int hyperv_cpu_disable(void)
802 static void hv_cpu_hotplug_quirk(bool vmbus_loaded)
804 static void *previous_cpu_disable;
807 * Offlining a CPU when running on newer hypervisors (WS2012R2, Win8,
808 * ...) is not supported at this moment as channel interrupts are
809 * distributed across all of them.
812 if ((vmbus_proto_version == VERSION_WS2008) ||
813 (vmbus_proto_version == VERSION_WIN7))
817 previous_cpu_disable = smp_ops.cpu_disable;
818 smp_ops.cpu_disable = hyperv_cpu_disable;
819 pr_notice("CPU offlining is not supported by hypervisor\n");
820 } else if (previous_cpu_disable)
821 smp_ops.cpu_disable = previous_cpu_disable;
824 static void hv_cpu_hotplug_quirk(bool vmbus_loaded)
830 * vmbus_bus_init -Main vmbus driver initialization routine.
833 * - initialize the vmbus driver context
834 * - invoke the vmbus hv main init routine
835 * - get the irq resource
836 * - retrieve the channel offers
838 static int vmbus_bus_init(int irq)
842 /* Hypervisor initialization...setup hypercall page..etc */
845 pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
849 tasklet_init(&msg_dpc, vmbus_on_msg_dpc, 0);
851 ret = bus_register(&hv_bus);
855 hv_setup_vmbus_irq(vmbus_isr);
857 ret = hv_synic_alloc();
861 * Initialize the per-cpu interrupt state and
862 * connect to the host.
864 on_each_cpu(hv_synic_init, NULL, 1);
865 ret = vmbus_connect();
869 hv_cpu_hotplug_quirk(true);
872 * Only register if the crash MSRs are available
874 if (ms_hyperv.features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
875 register_die_notifier(&hyperv_die_block);
876 atomic_notifier_chain_register(&panic_notifier_list,
877 &hyperv_panic_block);
880 vmbus_request_offers();
886 hv_remove_vmbus_irq();
888 bus_unregister(&hv_bus);
897 * __vmbus_child_driver_register - Register a vmbus's driver
898 * @drv: Pointer to driver structure you want to register
899 * @owner: owner module of the drv
900 * @mod_name: module name string
902 * Registers the given driver with Linux through the 'driver_register()' call
903 * and sets up the hyper-v vmbus handling for this driver.
904 * It will return the state of the 'driver_register()' call.
907 int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
911 pr_info("registering driver %s\n", hv_driver->name);
913 ret = vmbus_exists();
917 hv_driver->driver.name = hv_driver->name;
918 hv_driver->driver.owner = owner;
919 hv_driver->driver.mod_name = mod_name;
920 hv_driver->driver.bus = &hv_bus;
922 ret = driver_register(&hv_driver->driver);
926 EXPORT_SYMBOL_GPL(__vmbus_driver_register);
929 * vmbus_driver_unregister() - Unregister a vmbus's driver
930 * @drv: Pointer to driver structure you want to un-register
932 * Un-register the given driver that was previous registered with a call to
933 * vmbus_driver_register()
935 void vmbus_driver_unregister(struct hv_driver *hv_driver)
937 pr_info("unregistering driver %s\n", hv_driver->name);
940 driver_unregister(&hv_driver->driver);
942 EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
945 * vmbus_device_create - Creates and registers a new child device
948 struct hv_device *vmbus_device_create(const uuid_le *type,
949 const uuid_le *instance,
950 struct vmbus_channel *channel)
952 struct hv_device *child_device_obj;
954 child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
955 if (!child_device_obj) {
956 pr_err("Unable to allocate device object for child device\n");
960 child_device_obj->channel = channel;
961 memcpy(&child_device_obj->dev_type, type, sizeof(uuid_le));
962 memcpy(&child_device_obj->dev_instance, instance,
966 return child_device_obj;
970 * vmbus_device_register - Register the child device
972 int vmbus_device_register(struct hv_device *child_device_obj)
976 dev_set_name(&child_device_obj->device, "vmbus_%d",
977 child_device_obj->channel->id);
979 child_device_obj->device.bus = &hv_bus;
980 child_device_obj->device.parent = &hv_acpi_dev->dev;
981 child_device_obj->device.release = vmbus_device_release;
984 * Register with the LDM. This will kick off the driver/device
985 * binding...which will eventually call vmbus_match() and vmbus_probe()
987 ret = device_register(&child_device_obj->device);
990 pr_err("Unable to register child device\n");
992 pr_debug("child device %s registered\n",
993 dev_name(&child_device_obj->device));
999 * vmbus_device_unregister - Remove the specified child device
1002 void vmbus_device_unregister(struct hv_device *device_obj)
1004 pr_debug("child device %s unregistered\n",
1005 dev_name(&device_obj->device));
1008 * Kick off the process of unregistering the device.
1009 * This will call vmbus_remove() and eventually vmbus_device_release()
1011 device_unregister(&device_obj->device);
1016 * VMBUS is an acpi enumerated device. Get the the information we
1020 static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
1022 switch (res->type) {
1023 case ACPI_RESOURCE_TYPE_IRQ:
1024 irq = res->data.irq.interrupts[0];
1027 case ACPI_RESOURCE_TYPE_ADDRESS64:
1028 hyperv_mmio.start = res->data.address64.address.minimum;
1029 hyperv_mmio.end = res->data.address64.address.maximum;
1036 static int vmbus_acpi_add(struct acpi_device *device)
1039 int ret_val = -ENODEV;
1041 hv_acpi_dev = device;
1043 result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
1044 vmbus_walk_resources, NULL);
1046 if (ACPI_FAILURE(result))
1049 * The parent of the vmbus acpi device (Gen2 firmware) is the VMOD that
1050 * has the mmio ranges. Get that.
1052 if (device->parent) {
1053 result = acpi_walk_resources(device->parent->handle,
1055 vmbus_walk_resources, NULL);
1057 if (ACPI_FAILURE(result))
1059 if (hyperv_mmio.start && hyperv_mmio.end)
1060 request_resource(&iomem_resource, &hyperv_mmio);
1065 complete(&probe_event);
1069 static int vmbus_acpi_remove(struct acpi_device *device)
1073 if (hyperv_mmio.start && hyperv_mmio.end)
1074 ret = release_resource(&hyperv_mmio);
1078 static const struct acpi_device_id vmbus_acpi_device_ids[] = {
1083 MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
1085 static struct acpi_driver vmbus_acpi_driver = {
1087 .ids = vmbus_acpi_device_ids,
1089 .add = vmbus_acpi_add,
1090 .remove = vmbus_acpi_remove,
1094 static void hv_kexec_handler(void)
1098 hv_synic_clockevents_cleanup();
1099 vmbus_initiate_unload();
1100 for_each_online_cpu(cpu)
1101 smp_call_function_single(cpu, hv_synic_cleanup, NULL, 1);
1105 static void hv_crash_handler(struct pt_regs *regs)
1107 vmbus_initiate_unload();
1109 * In crash handler we can't schedule synic cleanup for all CPUs,
1110 * doing the cleanup for current CPU only. This should be sufficient
1113 hv_synic_cleanup(NULL);
1117 static int __init hv_acpi_init(void)
1121 if (x86_hyper != &x86_hyper_ms_hyperv)
1124 init_completion(&probe_event);
1127 * Get irq resources first.
1129 ret = acpi_bus_register_driver(&vmbus_acpi_driver);
1134 t = wait_for_completion_timeout(&probe_event, 5*HZ);
1145 ret = vmbus_bus_init(irq);
1149 hv_setup_kexec_handler(hv_kexec_handler);
1150 hv_setup_crash_handler(hv_crash_handler);
1155 acpi_bus_unregister_driver(&vmbus_acpi_driver);
1160 static void __exit vmbus_exit(void)
1164 hv_remove_kexec_handler();
1165 hv_remove_crash_handler();
1166 vmbus_connection.conn_state = DISCONNECTED;
1167 hv_synic_clockevents_cleanup();
1169 hv_remove_vmbus_irq();
1170 tasklet_kill(&msg_dpc);
1171 vmbus_free_channels();
1172 if (ms_hyperv.features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
1173 unregister_die_notifier(&hyperv_die_block);
1174 atomic_notifier_chain_unregister(&panic_notifier_list,
1175 &hyperv_panic_block);
1177 bus_unregister(&hv_bus);
1179 for_each_online_cpu(cpu) {
1180 tasklet_kill(hv_context.event_dpc[cpu]);
1181 smp_call_function_single(cpu, hv_synic_cleanup, NULL, 1);
1184 acpi_bus_unregister_driver(&vmbus_acpi_driver);
1185 hv_cpu_hotplug_quirk(false);
1189 MODULE_LICENSE("GPL");
1191 subsys_initcall(hv_acpi_init);
1192 module_exit(vmbus_exit);