ALSA: usb-audio: Check mixer unit descriptors more strictly
[linux/fpc-iii.git] / drivers / hv / vmbus_drv.c
blob2c6d5c7a444599326ea0b4252e8661169f12c1ca
1 /*
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
11 * more details.
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.
17 * Authors:
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 <linux/sched/task_stack.h>
39 #include <asm/mshyperv.h>
40 #include <linux/notifier.h>
41 #include <linux/ptrace.h>
42 #include <linux/screen_info.h>
43 #include <linux/kdebug.h>
44 #include <linux/efi.h>
45 #include <linux/random.h>
46 #include "hyperv_vmbus.h"
48 struct vmbus_dynid {
49 struct list_head node;
50 struct hv_vmbus_device_id id;
53 static struct acpi_device *hv_acpi_dev;
55 static struct completion probe_event;
57 static int hyperv_cpuhp_online;
59 static void *hv_panic_page;
61 static int hyperv_panic_event(struct notifier_block *nb, unsigned long val,
62 void *args)
64 struct pt_regs *regs;
66 regs = current_pt_regs();
68 hyperv_report_panic(regs, val);
69 return NOTIFY_DONE;
72 static int hyperv_die_event(struct notifier_block *nb, unsigned long val,
73 void *args)
75 struct die_args *die = (struct die_args *)args;
76 struct pt_regs *regs = die->regs;
78 hyperv_report_panic(regs, val);
79 return NOTIFY_DONE;
82 static struct notifier_block hyperv_die_block = {
83 .notifier_call = hyperv_die_event,
85 static struct notifier_block hyperv_panic_block = {
86 .notifier_call = hyperv_panic_event,
89 static const char *fb_mmio_name = "fb_range";
90 static struct resource *fb_mmio;
91 static struct resource *hyperv_mmio;
92 static DEFINE_SEMAPHORE(hyperv_mmio_lock);
94 static int vmbus_exists(void)
96 if (hv_acpi_dev == NULL)
97 return -ENODEV;
99 return 0;
102 #define VMBUS_ALIAS_LEN ((sizeof((struct hv_vmbus_device_id *)0)->guid) * 2)
103 static void print_alias_name(struct hv_device *hv_dev, char *alias_name)
105 int i;
106 for (i = 0; i < VMBUS_ALIAS_LEN; i += 2)
107 sprintf(&alias_name[i], "%02x", hv_dev->dev_type.b[i/2]);
110 static u8 channel_monitor_group(const struct vmbus_channel *channel)
112 return (u8)channel->offermsg.monitorid / 32;
115 static u8 channel_monitor_offset(const struct vmbus_channel *channel)
117 return (u8)channel->offermsg.monitorid % 32;
120 static u32 channel_pending(const struct vmbus_channel *channel,
121 const struct hv_monitor_page *monitor_page)
123 u8 monitor_group = channel_monitor_group(channel);
125 return monitor_page->trigger_group[monitor_group].pending;
128 static u32 channel_latency(const struct vmbus_channel *channel,
129 const struct hv_monitor_page *monitor_page)
131 u8 monitor_group = channel_monitor_group(channel);
132 u8 monitor_offset = channel_monitor_offset(channel);
134 return monitor_page->latency[monitor_group][monitor_offset];
137 static u32 channel_conn_id(struct vmbus_channel *channel,
138 struct hv_monitor_page *monitor_page)
140 u8 monitor_group = channel_monitor_group(channel);
141 u8 monitor_offset = channel_monitor_offset(channel);
142 return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
145 static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
146 char *buf)
148 struct hv_device *hv_dev = device_to_hv_device(dev);
150 if (!hv_dev->channel)
151 return -ENODEV;
152 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
154 static DEVICE_ATTR_RO(id);
156 static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
157 char *buf)
159 struct hv_device *hv_dev = device_to_hv_device(dev);
161 if (!hv_dev->channel)
162 return -ENODEV;
163 return sprintf(buf, "%d\n", hv_dev->channel->state);
165 static DEVICE_ATTR_RO(state);
167 static ssize_t monitor_id_show(struct device *dev,
168 struct device_attribute *dev_attr, char *buf)
170 struct hv_device *hv_dev = device_to_hv_device(dev);
172 if (!hv_dev->channel)
173 return -ENODEV;
174 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
176 static DEVICE_ATTR_RO(monitor_id);
178 static ssize_t class_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)
184 return -ENODEV;
185 return sprintf(buf, "{%pUl}\n",
186 hv_dev->channel->offermsg.offer.if_type.b);
188 static DEVICE_ATTR_RO(class_id);
190 static ssize_t device_id_show(struct device *dev,
191 struct device_attribute *dev_attr, char *buf)
193 struct hv_device *hv_dev = device_to_hv_device(dev);
195 if (!hv_dev->channel)
196 return -ENODEV;
197 return sprintf(buf, "{%pUl}\n",
198 hv_dev->channel->offermsg.offer.if_instance.b);
200 static DEVICE_ATTR_RO(device_id);
202 static ssize_t modalias_show(struct device *dev,
203 struct device_attribute *dev_attr, char *buf)
205 struct hv_device *hv_dev = device_to_hv_device(dev);
206 char alias_name[VMBUS_ALIAS_LEN + 1];
208 print_alias_name(hv_dev, alias_name);
209 return sprintf(buf, "vmbus:%s\n", alias_name);
211 static DEVICE_ATTR_RO(modalias);
213 #ifdef CONFIG_NUMA
214 static ssize_t numa_node_show(struct device *dev,
215 struct device_attribute *attr, char *buf)
217 struct hv_device *hv_dev = device_to_hv_device(dev);
219 if (!hv_dev->channel)
220 return -ENODEV;
222 return sprintf(buf, "%d\n", hv_dev->channel->numa_node);
224 static DEVICE_ATTR_RO(numa_node);
225 #endif
227 static ssize_t server_monitor_pending_show(struct device *dev,
228 struct device_attribute *dev_attr,
229 char *buf)
231 struct hv_device *hv_dev = device_to_hv_device(dev);
233 if (!hv_dev->channel)
234 return -ENODEV;
235 return sprintf(buf, "%d\n",
236 channel_pending(hv_dev->channel,
237 vmbus_connection.monitor_pages[1]));
239 static DEVICE_ATTR_RO(server_monitor_pending);
241 static ssize_t client_monitor_pending_show(struct device *dev,
242 struct device_attribute *dev_attr,
243 char *buf)
245 struct hv_device *hv_dev = device_to_hv_device(dev);
247 if (!hv_dev->channel)
248 return -ENODEV;
249 return sprintf(buf, "%d\n",
250 channel_pending(hv_dev->channel,
251 vmbus_connection.monitor_pages[1]));
253 static DEVICE_ATTR_RO(client_monitor_pending);
255 static ssize_t server_monitor_latency_show(struct device *dev,
256 struct device_attribute *dev_attr,
257 char *buf)
259 struct hv_device *hv_dev = device_to_hv_device(dev);
261 if (!hv_dev->channel)
262 return -ENODEV;
263 return sprintf(buf, "%d\n",
264 channel_latency(hv_dev->channel,
265 vmbus_connection.monitor_pages[0]));
267 static DEVICE_ATTR_RO(server_monitor_latency);
269 static ssize_t client_monitor_latency_show(struct device *dev,
270 struct device_attribute *dev_attr,
271 char *buf)
273 struct hv_device *hv_dev = device_to_hv_device(dev);
275 if (!hv_dev->channel)
276 return -ENODEV;
277 return sprintf(buf, "%d\n",
278 channel_latency(hv_dev->channel,
279 vmbus_connection.monitor_pages[1]));
281 static DEVICE_ATTR_RO(client_monitor_latency);
283 static ssize_t server_monitor_conn_id_show(struct device *dev,
284 struct device_attribute *dev_attr,
285 char *buf)
287 struct hv_device *hv_dev = device_to_hv_device(dev);
289 if (!hv_dev->channel)
290 return -ENODEV;
291 return sprintf(buf, "%d\n",
292 channel_conn_id(hv_dev->channel,
293 vmbus_connection.monitor_pages[0]));
295 static DEVICE_ATTR_RO(server_monitor_conn_id);
297 static ssize_t client_monitor_conn_id_show(struct device *dev,
298 struct device_attribute *dev_attr,
299 char *buf)
301 struct hv_device *hv_dev = device_to_hv_device(dev);
303 if (!hv_dev->channel)
304 return -ENODEV;
305 return sprintf(buf, "%d\n",
306 channel_conn_id(hv_dev->channel,
307 vmbus_connection.monitor_pages[1]));
309 static DEVICE_ATTR_RO(client_monitor_conn_id);
311 static ssize_t out_intr_mask_show(struct device *dev,
312 struct device_attribute *dev_attr, char *buf)
314 struct hv_device *hv_dev = device_to_hv_device(dev);
315 struct hv_ring_buffer_debug_info outbound;
317 if (!hv_dev->channel)
318 return -ENODEV;
319 if (hv_dev->channel->state != CHANNEL_OPENED_STATE)
320 return -EINVAL;
321 hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
322 return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
324 static DEVICE_ATTR_RO(out_intr_mask);
326 static ssize_t out_read_index_show(struct device *dev,
327 struct device_attribute *dev_attr, char *buf)
329 struct hv_device *hv_dev = device_to_hv_device(dev);
330 struct hv_ring_buffer_debug_info outbound;
332 if (!hv_dev->channel)
333 return -ENODEV;
334 if (hv_dev->channel->state != CHANNEL_OPENED_STATE)
335 return -EINVAL;
336 hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
337 return sprintf(buf, "%d\n", outbound.current_read_index);
339 static DEVICE_ATTR_RO(out_read_index);
341 static ssize_t out_write_index_show(struct device *dev,
342 struct device_attribute *dev_attr,
343 char *buf)
345 struct hv_device *hv_dev = device_to_hv_device(dev);
346 struct hv_ring_buffer_debug_info outbound;
348 if (!hv_dev->channel)
349 return -ENODEV;
350 if (hv_dev->channel->state != CHANNEL_OPENED_STATE)
351 return -EINVAL;
352 hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
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;
364 if (!hv_dev->channel)
365 return -ENODEV;
366 if (hv_dev->channel->state != CHANNEL_OPENED_STATE)
367 return -EINVAL;
368 hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
369 return sprintf(buf, "%d\n", outbound.bytes_avail_toread);
371 static DEVICE_ATTR_RO(out_read_bytes_avail);
373 static ssize_t out_write_bytes_avail_show(struct device *dev,
374 struct device_attribute *dev_attr,
375 char *buf)
377 struct hv_device *hv_dev = device_to_hv_device(dev);
378 struct hv_ring_buffer_debug_info outbound;
380 if (!hv_dev->channel)
381 return -ENODEV;
382 if (hv_dev->channel->state != CHANNEL_OPENED_STATE)
383 return -EINVAL;
384 hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
385 return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
387 static DEVICE_ATTR_RO(out_write_bytes_avail);
389 static ssize_t in_intr_mask_show(struct device *dev,
390 struct device_attribute *dev_attr, char *buf)
392 struct hv_device *hv_dev = device_to_hv_device(dev);
393 struct hv_ring_buffer_debug_info inbound;
395 if (!hv_dev->channel)
396 return -ENODEV;
397 if (hv_dev->channel->state != CHANNEL_OPENED_STATE)
398 return -EINVAL;
399 hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
400 return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
402 static DEVICE_ATTR_RO(in_intr_mask);
404 static ssize_t in_read_index_show(struct device *dev,
405 struct device_attribute *dev_attr, char *buf)
407 struct hv_device *hv_dev = device_to_hv_device(dev);
408 struct hv_ring_buffer_debug_info inbound;
410 if (!hv_dev->channel)
411 return -ENODEV;
412 if (hv_dev->channel->state != CHANNEL_OPENED_STATE)
413 return -EINVAL;
414 hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
415 return sprintf(buf, "%d\n", inbound.current_read_index);
417 static DEVICE_ATTR_RO(in_read_index);
419 static ssize_t in_write_index_show(struct device *dev,
420 struct device_attribute *dev_attr, char *buf)
422 struct hv_device *hv_dev = device_to_hv_device(dev);
423 struct hv_ring_buffer_debug_info inbound;
425 if (!hv_dev->channel)
426 return -ENODEV;
427 if (hv_dev->channel->state != CHANNEL_OPENED_STATE)
428 return -EINVAL;
429 hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
430 return sprintf(buf, "%d\n", inbound.current_write_index);
432 static DEVICE_ATTR_RO(in_write_index);
434 static ssize_t in_read_bytes_avail_show(struct device *dev,
435 struct device_attribute *dev_attr,
436 char *buf)
438 struct hv_device *hv_dev = device_to_hv_device(dev);
439 struct hv_ring_buffer_debug_info inbound;
441 if (!hv_dev->channel)
442 return -ENODEV;
443 if (hv_dev->channel->state != CHANNEL_OPENED_STATE)
444 return -EINVAL;
445 hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
446 return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
448 static DEVICE_ATTR_RO(in_read_bytes_avail);
450 static ssize_t in_write_bytes_avail_show(struct device *dev,
451 struct device_attribute *dev_attr,
452 char *buf)
454 struct hv_device *hv_dev = device_to_hv_device(dev);
455 struct hv_ring_buffer_debug_info inbound;
457 if (!hv_dev->channel)
458 return -ENODEV;
459 if (hv_dev->channel->state != CHANNEL_OPENED_STATE)
460 return -EINVAL;
461 hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
462 return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
464 static DEVICE_ATTR_RO(in_write_bytes_avail);
466 static ssize_t channel_vp_mapping_show(struct device *dev,
467 struct device_attribute *dev_attr,
468 char *buf)
470 struct hv_device *hv_dev = device_to_hv_device(dev);
471 struct vmbus_channel *channel = hv_dev->channel, *cur_sc;
472 unsigned long flags;
473 int buf_size = PAGE_SIZE, n_written, tot_written;
474 struct list_head *cur;
476 if (!channel)
477 return -ENODEV;
479 tot_written = snprintf(buf, buf_size, "%u:%u\n",
480 channel->offermsg.child_relid, channel->target_cpu);
482 spin_lock_irqsave(&channel->lock, flags);
484 list_for_each(cur, &channel->sc_list) {
485 if (tot_written >= buf_size - 1)
486 break;
488 cur_sc = list_entry(cur, struct vmbus_channel, sc_list);
489 n_written = scnprintf(buf + tot_written,
490 buf_size - tot_written,
491 "%u:%u\n",
492 cur_sc->offermsg.child_relid,
493 cur_sc->target_cpu);
494 tot_written += n_written;
497 spin_unlock_irqrestore(&channel->lock, flags);
499 return tot_written;
501 static DEVICE_ATTR_RO(channel_vp_mapping);
503 static ssize_t vendor_show(struct device *dev,
504 struct device_attribute *dev_attr,
505 char *buf)
507 struct hv_device *hv_dev = device_to_hv_device(dev);
508 return sprintf(buf, "0x%x\n", hv_dev->vendor_id);
510 static DEVICE_ATTR_RO(vendor);
512 static ssize_t device_show(struct device *dev,
513 struct device_attribute *dev_attr,
514 char *buf)
516 struct hv_device *hv_dev = device_to_hv_device(dev);
517 return sprintf(buf, "0x%x\n", hv_dev->device_id);
519 static DEVICE_ATTR_RO(device);
521 /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
522 static struct attribute *vmbus_dev_attrs[] = {
523 &dev_attr_id.attr,
524 &dev_attr_state.attr,
525 &dev_attr_monitor_id.attr,
526 &dev_attr_class_id.attr,
527 &dev_attr_device_id.attr,
528 &dev_attr_modalias.attr,
529 #ifdef CONFIG_NUMA
530 &dev_attr_numa_node.attr,
531 #endif
532 &dev_attr_server_monitor_pending.attr,
533 &dev_attr_client_monitor_pending.attr,
534 &dev_attr_server_monitor_latency.attr,
535 &dev_attr_client_monitor_latency.attr,
536 &dev_attr_server_monitor_conn_id.attr,
537 &dev_attr_client_monitor_conn_id.attr,
538 &dev_attr_out_intr_mask.attr,
539 &dev_attr_out_read_index.attr,
540 &dev_attr_out_write_index.attr,
541 &dev_attr_out_read_bytes_avail.attr,
542 &dev_attr_out_write_bytes_avail.attr,
543 &dev_attr_in_intr_mask.attr,
544 &dev_attr_in_read_index.attr,
545 &dev_attr_in_write_index.attr,
546 &dev_attr_in_read_bytes_avail.attr,
547 &dev_attr_in_write_bytes_avail.attr,
548 &dev_attr_channel_vp_mapping.attr,
549 &dev_attr_vendor.attr,
550 &dev_attr_device.attr,
551 NULL,
553 ATTRIBUTE_GROUPS(vmbus_dev);
556 * vmbus_uevent - add uevent for our device
558 * This routine is invoked when a device is added or removed on the vmbus to
559 * generate a uevent to udev in the userspace. The udev will then look at its
560 * rule and the uevent generated here to load the appropriate driver
562 * The alias string will be of the form vmbus:guid where guid is the string
563 * representation of the device guid (each byte of the guid will be
564 * represented with two hex characters.
566 static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
568 struct hv_device *dev = device_to_hv_device(device);
569 int ret;
570 char alias_name[VMBUS_ALIAS_LEN + 1];
572 print_alias_name(dev, alias_name);
573 ret = add_uevent_var(env, "MODALIAS=vmbus:%s", alias_name);
574 return ret;
577 static const uuid_le null_guid;
579 static inline bool is_null_guid(const uuid_le *guid)
581 if (uuid_le_cmp(*guid, null_guid))
582 return false;
583 return true;
587 * Return a matching hv_vmbus_device_id pointer.
588 * If there is no match, return NULL.
590 static const struct hv_vmbus_device_id *hv_vmbus_get_id(struct hv_driver *drv,
591 const uuid_le *guid)
593 const struct hv_vmbus_device_id *id = NULL;
594 struct vmbus_dynid *dynid;
596 /* Look at the dynamic ids first, before the static ones */
597 spin_lock(&drv->dynids.lock);
598 list_for_each_entry(dynid, &drv->dynids.list, node) {
599 if (!uuid_le_cmp(dynid->id.guid, *guid)) {
600 id = &dynid->id;
601 break;
604 spin_unlock(&drv->dynids.lock);
606 if (id)
607 return id;
609 id = drv->id_table;
610 if (id == NULL)
611 return NULL; /* empty device table */
613 for (; !is_null_guid(&id->guid); id++)
614 if (!uuid_le_cmp(id->guid, *guid))
615 return id;
617 return NULL;
620 /* vmbus_add_dynid - add a new device ID to this driver and re-probe devices */
621 static int vmbus_add_dynid(struct hv_driver *drv, uuid_le *guid)
623 struct vmbus_dynid *dynid;
625 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
626 if (!dynid)
627 return -ENOMEM;
629 dynid->id.guid = *guid;
631 spin_lock(&drv->dynids.lock);
632 list_add_tail(&dynid->node, &drv->dynids.list);
633 spin_unlock(&drv->dynids.lock);
635 return driver_attach(&drv->driver);
638 static void vmbus_free_dynids(struct hv_driver *drv)
640 struct vmbus_dynid *dynid, *n;
642 spin_lock(&drv->dynids.lock);
643 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
644 list_del(&dynid->node);
645 kfree(dynid);
647 spin_unlock(&drv->dynids.lock);
651 * store_new_id - sysfs frontend to vmbus_add_dynid()
653 * Allow GUIDs to be added to an existing driver via sysfs.
655 static ssize_t new_id_store(struct device_driver *driver, const char *buf,
656 size_t count)
658 struct hv_driver *drv = drv_to_hv_drv(driver);
659 uuid_le guid;
660 ssize_t retval;
662 retval = uuid_le_to_bin(buf, &guid);
663 if (retval)
664 return retval;
666 if (hv_vmbus_get_id(drv, &guid))
667 return -EEXIST;
669 retval = vmbus_add_dynid(drv, &guid);
670 if (retval)
671 return retval;
672 return count;
674 static DRIVER_ATTR_WO(new_id);
677 * store_remove_id - remove a PCI device ID from this driver
679 * Removes a dynamic pci device ID to this driver.
681 static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
682 size_t count)
684 struct hv_driver *drv = drv_to_hv_drv(driver);
685 struct vmbus_dynid *dynid, *n;
686 uuid_le guid;
687 ssize_t retval;
689 retval = uuid_le_to_bin(buf, &guid);
690 if (retval)
691 return retval;
693 retval = -ENODEV;
694 spin_lock(&drv->dynids.lock);
695 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
696 struct hv_vmbus_device_id *id = &dynid->id;
698 if (!uuid_le_cmp(id->guid, guid)) {
699 list_del(&dynid->node);
700 kfree(dynid);
701 retval = count;
702 break;
705 spin_unlock(&drv->dynids.lock);
707 return retval;
709 static DRIVER_ATTR_WO(remove_id);
711 static struct attribute *vmbus_drv_attrs[] = {
712 &driver_attr_new_id.attr,
713 &driver_attr_remove_id.attr,
714 NULL,
716 ATTRIBUTE_GROUPS(vmbus_drv);
720 * vmbus_match - Attempt to match the specified device to the specified driver
722 static int vmbus_match(struct device *device, struct device_driver *driver)
724 struct hv_driver *drv = drv_to_hv_drv(driver);
725 struct hv_device *hv_dev = device_to_hv_device(device);
727 /* The hv_sock driver handles all hv_sock offers. */
728 if (is_hvsock_channel(hv_dev->channel))
729 return drv->hvsock;
731 if (hv_vmbus_get_id(drv, &hv_dev->dev_type))
732 return 1;
734 return 0;
738 * vmbus_probe - Add the new vmbus's child device
740 static int vmbus_probe(struct device *child_device)
742 int ret = 0;
743 struct hv_driver *drv =
744 drv_to_hv_drv(child_device->driver);
745 struct hv_device *dev = device_to_hv_device(child_device);
746 const struct hv_vmbus_device_id *dev_id;
748 dev_id = hv_vmbus_get_id(drv, &dev->dev_type);
749 if (drv->probe) {
750 ret = drv->probe(dev, dev_id);
751 if (ret != 0)
752 pr_err("probe failed for device %s (%d)\n",
753 dev_name(child_device), ret);
755 } else {
756 pr_err("probe not set for driver %s\n",
757 dev_name(child_device));
758 ret = -ENODEV;
760 return ret;
764 * vmbus_remove - Remove a vmbus device
766 static int vmbus_remove(struct device *child_device)
768 struct hv_driver *drv;
769 struct hv_device *dev = device_to_hv_device(child_device);
771 if (child_device->driver) {
772 drv = drv_to_hv_drv(child_device->driver);
773 if (drv->remove)
774 drv->remove(dev);
777 return 0;
782 * vmbus_shutdown - Shutdown a vmbus device
784 static void vmbus_shutdown(struct device *child_device)
786 struct hv_driver *drv;
787 struct hv_device *dev = device_to_hv_device(child_device);
790 /* The device may not be attached yet */
791 if (!child_device->driver)
792 return;
794 drv = drv_to_hv_drv(child_device->driver);
796 if (drv->shutdown)
797 drv->shutdown(dev);
802 * vmbus_device_release - Final callback release of the vmbus child device
804 static void vmbus_device_release(struct device *device)
806 struct hv_device *hv_dev = device_to_hv_device(device);
807 struct vmbus_channel *channel = hv_dev->channel;
809 mutex_lock(&vmbus_connection.channel_mutex);
810 hv_process_channel_removal(channel->offermsg.child_relid);
811 mutex_unlock(&vmbus_connection.channel_mutex);
812 kfree(hv_dev);
816 /* The one and only one */
817 static struct bus_type hv_bus = {
818 .name = "vmbus",
819 .match = vmbus_match,
820 .shutdown = vmbus_shutdown,
821 .remove = vmbus_remove,
822 .probe = vmbus_probe,
823 .uevent = vmbus_uevent,
824 .dev_groups = vmbus_dev_groups,
825 .drv_groups = vmbus_drv_groups,
828 struct onmessage_work_context {
829 struct work_struct work;
830 struct hv_message msg;
833 static void vmbus_onmessage_work(struct work_struct *work)
835 struct onmessage_work_context *ctx;
837 /* Do not process messages if we're in DISCONNECTED state */
838 if (vmbus_connection.conn_state == DISCONNECTED)
839 return;
841 ctx = container_of(work, struct onmessage_work_context,
842 work);
843 vmbus_onmessage(&ctx->msg);
844 kfree(ctx);
847 static void hv_process_timer_expiration(struct hv_message *msg,
848 struct hv_per_cpu_context *hv_cpu)
850 struct clock_event_device *dev = hv_cpu->clk_evt;
852 if (dev->event_handler)
853 dev->event_handler(dev);
855 vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED);
858 void vmbus_on_msg_dpc(unsigned long data)
860 struct hv_per_cpu_context *hv_cpu = (void *)data;
861 void *page_addr = hv_cpu->synic_message_page;
862 struct hv_message *msg = (struct hv_message *)page_addr +
863 VMBUS_MESSAGE_SINT;
864 struct vmbus_channel_message_header *hdr;
865 const struct vmbus_channel_message_table_entry *entry;
866 struct onmessage_work_context *ctx;
867 u32 message_type = msg->header.message_type;
869 if (message_type == HVMSG_NONE)
870 /* no msg */
871 return;
873 hdr = (struct vmbus_channel_message_header *)msg->u.payload;
875 trace_vmbus_on_msg_dpc(hdr);
877 if (hdr->msgtype >= CHANNELMSG_COUNT) {
878 WARN_ONCE(1, "unknown msgtype=%d\n", hdr->msgtype);
879 goto msg_handled;
882 entry = &channel_message_table[hdr->msgtype];
883 if (entry->handler_type == VMHT_BLOCKING) {
884 ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
885 if (ctx == NULL)
886 return;
888 INIT_WORK(&ctx->work, vmbus_onmessage_work);
889 memcpy(&ctx->msg, msg, sizeof(*msg));
892 * The host can generate a rescind message while we
893 * may still be handling the original offer. We deal with
894 * this condition by ensuring the processing is done on the
895 * same CPU.
897 switch (hdr->msgtype) {
898 case CHANNELMSG_RESCIND_CHANNELOFFER:
900 * If we are handling the rescind message;
901 * schedule the work on the global work queue.
903 schedule_work_on(vmbus_connection.connect_cpu,
904 &ctx->work);
905 break;
907 case CHANNELMSG_OFFERCHANNEL:
908 atomic_inc(&vmbus_connection.offer_in_progress);
909 queue_work_on(vmbus_connection.connect_cpu,
910 vmbus_connection.work_queue,
911 &ctx->work);
912 break;
914 default:
915 queue_work(vmbus_connection.work_queue, &ctx->work);
917 } else
918 entry->message_handler(hdr);
920 msg_handled:
921 vmbus_signal_eom(msg, message_type);
926 * Direct callback for channels using other deferred processing
928 static void vmbus_channel_isr(struct vmbus_channel *channel)
930 void (*callback_fn)(void *);
932 callback_fn = READ_ONCE(channel->onchannel_callback);
933 if (likely(callback_fn != NULL))
934 (*callback_fn)(channel->channel_callback_context);
938 * Schedule all channels with events pending
940 static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu)
942 unsigned long *recv_int_page;
943 u32 maxbits, relid;
945 if (vmbus_proto_version < VERSION_WIN8) {
946 maxbits = MAX_NUM_CHANNELS_SUPPORTED;
947 recv_int_page = vmbus_connection.recv_int_page;
948 } else {
950 * When the host is win8 and beyond, the event page
951 * can be directly checked to get the id of the channel
952 * that has the interrupt pending.
954 void *page_addr = hv_cpu->synic_event_page;
955 union hv_synic_event_flags *event
956 = (union hv_synic_event_flags *)page_addr +
957 VMBUS_MESSAGE_SINT;
959 maxbits = HV_EVENT_FLAGS_COUNT;
960 recv_int_page = event->flags;
963 if (unlikely(!recv_int_page))
964 return;
966 for_each_set_bit(relid, recv_int_page, maxbits) {
967 struct vmbus_channel *channel;
969 if (!sync_test_and_clear_bit(relid, recv_int_page))
970 continue;
972 /* Special case - vmbus channel protocol msg */
973 if (relid == 0)
974 continue;
976 rcu_read_lock();
978 /* Find channel based on relid */
979 list_for_each_entry_rcu(channel, &hv_cpu->chan_list, percpu_list) {
980 if (channel->offermsg.child_relid != relid)
981 continue;
983 if (channel->rescind)
984 continue;
986 trace_vmbus_chan_sched(channel);
988 ++channel->interrupts;
990 switch (channel->callback_mode) {
991 case HV_CALL_ISR:
992 vmbus_channel_isr(channel);
993 break;
995 case HV_CALL_BATCHED:
996 hv_begin_read(&channel->inbound);
997 /* fallthrough */
998 case HV_CALL_DIRECT:
999 tasklet_schedule(&channel->callback_event);
1003 rcu_read_unlock();
1007 static void vmbus_isr(void)
1009 struct hv_per_cpu_context *hv_cpu
1010 = this_cpu_ptr(hv_context.cpu_context);
1011 void *page_addr = hv_cpu->synic_event_page;
1012 struct hv_message *msg;
1013 union hv_synic_event_flags *event;
1014 bool handled = false;
1016 if (unlikely(page_addr == NULL))
1017 return;
1019 event = (union hv_synic_event_flags *)page_addr +
1020 VMBUS_MESSAGE_SINT;
1022 * Check for events before checking for messages. This is the order
1023 * in which events and messages are checked in Windows guests on
1024 * Hyper-V, and the Windows team suggested we do the same.
1027 if ((vmbus_proto_version == VERSION_WS2008) ||
1028 (vmbus_proto_version == VERSION_WIN7)) {
1030 /* Since we are a child, we only need to check bit 0 */
1031 if (sync_test_and_clear_bit(0, event->flags))
1032 handled = true;
1033 } else {
1035 * Our host is win8 or above. The signaling mechanism
1036 * has changed and we can directly look at the event page.
1037 * If bit n is set then we have an interrup on the channel
1038 * whose id is n.
1040 handled = true;
1043 if (handled)
1044 vmbus_chan_sched(hv_cpu);
1046 page_addr = hv_cpu->synic_message_page;
1047 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
1049 /* Check if there are actual msgs to be processed */
1050 if (msg->header.message_type != HVMSG_NONE) {
1051 if (msg->header.message_type == HVMSG_TIMER_EXPIRED)
1052 hv_process_timer_expiration(msg, hv_cpu);
1053 else
1054 tasklet_schedule(&hv_cpu->msg_dpc);
1057 add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0);
1061 * Boolean to control whether to report panic messages over Hyper-V.
1063 * It can be set via /proc/sys/kernel/hyperv/record_panic_msg
1065 static int sysctl_record_panic_msg = 1;
1068 * Callback from kmsg_dump. Grab as much as possible from the end of the kmsg
1069 * buffer and call into Hyper-V to transfer the data.
1071 static void hv_kmsg_dump(struct kmsg_dumper *dumper,
1072 enum kmsg_dump_reason reason)
1074 size_t bytes_written;
1075 phys_addr_t panic_pa;
1077 /* We are only interested in panics. */
1078 if ((reason != KMSG_DUMP_PANIC) || (!sysctl_record_panic_msg))
1079 return;
1081 panic_pa = virt_to_phys(hv_panic_page);
1084 * Write dump contents to the page. No need to synchronize; panic should
1085 * be single-threaded.
1087 kmsg_dump_get_buffer(dumper, true, hv_panic_page, PAGE_SIZE,
1088 &bytes_written);
1089 if (bytes_written)
1090 hyperv_report_panic_msg(panic_pa, bytes_written);
1093 static struct kmsg_dumper hv_kmsg_dumper = {
1094 .dump = hv_kmsg_dump,
1097 static struct ctl_table_header *hv_ctl_table_hdr;
1098 static int zero;
1099 static int one = 1;
1102 * sysctl option to allow the user to control whether kmsg data should be
1103 * reported to Hyper-V on panic.
1105 static struct ctl_table hv_ctl_table[] = {
1107 .procname = "hyperv_record_panic_msg",
1108 .data = &sysctl_record_panic_msg,
1109 .maxlen = sizeof(int),
1110 .mode = 0644,
1111 .proc_handler = proc_dointvec_minmax,
1112 .extra1 = &zero,
1113 .extra2 = &one
1118 static struct ctl_table hv_root_table[] = {
1120 .procname = "kernel",
1121 .mode = 0555,
1122 .child = hv_ctl_table
1128 * vmbus_bus_init -Main vmbus driver initialization routine.
1130 * Here, we
1131 * - initialize the vmbus driver context
1132 * - invoke the vmbus hv main init routine
1133 * - retrieve the channel offers
1135 static int vmbus_bus_init(void)
1137 int ret;
1139 /* Hypervisor initialization...setup hypercall page..etc */
1140 ret = hv_init();
1141 if (ret != 0) {
1142 pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
1143 return ret;
1146 ret = bus_register(&hv_bus);
1147 if (ret)
1148 return ret;
1150 hv_setup_vmbus_irq(vmbus_isr);
1152 ret = hv_synic_alloc();
1153 if (ret)
1154 goto err_alloc;
1156 * Initialize the per-cpu interrupt state and
1157 * connect to the host.
1159 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hyperv/vmbus:online",
1160 hv_synic_init, hv_synic_cleanup);
1161 if (ret < 0)
1162 goto err_alloc;
1163 hyperv_cpuhp_online = ret;
1165 ret = vmbus_connect();
1166 if (ret)
1167 goto err_connect;
1170 * Only register if the crash MSRs are available
1172 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
1173 u64 hyperv_crash_ctl;
1175 * Sysctl registration is not fatal, since by default
1176 * reporting is enabled.
1178 hv_ctl_table_hdr = register_sysctl_table(hv_root_table);
1179 if (!hv_ctl_table_hdr)
1180 pr_err("Hyper-V: sysctl table register error");
1183 * Register for panic kmsg callback only if the right
1184 * capability is supported by the hypervisor.
1186 hv_get_crash_ctl(hyperv_crash_ctl);
1187 if (hyperv_crash_ctl & HV_CRASH_CTL_CRASH_NOTIFY_MSG) {
1188 hv_panic_page = (void *)get_zeroed_page(GFP_KERNEL);
1189 if (hv_panic_page) {
1190 ret = kmsg_dump_register(&hv_kmsg_dumper);
1191 if (ret)
1192 pr_err("Hyper-V: kmsg dump register "
1193 "error 0x%x\n", ret);
1194 } else
1195 pr_err("Hyper-V: panic message page memory "
1196 "allocation failed");
1199 register_die_notifier(&hyperv_die_block);
1200 atomic_notifier_chain_register(&panic_notifier_list,
1201 &hyperv_panic_block);
1204 vmbus_request_offers();
1206 return 0;
1208 err_connect:
1209 cpuhp_remove_state(hyperv_cpuhp_online);
1210 err_alloc:
1211 hv_synic_free();
1212 hv_remove_vmbus_irq();
1214 bus_unregister(&hv_bus);
1215 free_page((unsigned long)hv_panic_page);
1216 unregister_sysctl_table(hv_ctl_table_hdr);
1217 hv_ctl_table_hdr = NULL;
1218 return ret;
1222 * __vmbus_child_driver_register() - Register a vmbus's driver
1223 * @hv_driver: Pointer to driver structure you want to register
1224 * @owner: owner module of the drv
1225 * @mod_name: module name string
1227 * Registers the given driver with Linux through the 'driver_register()' call
1228 * and sets up the hyper-v vmbus handling for this driver.
1229 * It will return the state of the 'driver_register()' call.
1232 int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
1234 int ret;
1236 pr_info("registering driver %s\n", hv_driver->name);
1238 ret = vmbus_exists();
1239 if (ret < 0)
1240 return ret;
1242 hv_driver->driver.name = hv_driver->name;
1243 hv_driver->driver.owner = owner;
1244 hv_driver->driver.mod_name = mod_name;
1245 hv_driver->driver.bus = &hv_bus;
1247 spin_lock_init(&hv_driver->dynids.lock);
1248 INIT_LIST_HEAD(&hv_driver->dynids.list);
1250 ret = driver_register(&hv_driver->driver);
1252 return ret;
1254 EXPORT_SYMBOL_GPL(__vmbus_driver_register);
1257 * vmbus_driver_unregister() - Unregister a vmbus's driver
1258 * @hv_driver: Pointer to driver structure you want to
1259 * un-register
1261 * Un-register the given driver that was previous registered with a call to
1262 * vmbus_driver_register()
1264 void vmbus_driver_unregister(struct hv_driver *hv_driver)
1266 pr_info("unregistering driver %s\n", hv_driver->name);
1268 if (!vmbus_exists()) {
1269 driver_unregister(&hv_driver->driver);
1270 vmbus_free_dynids(hv_driver);
1273 EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
1277 * Called when last reference to channel is gone.
1279 static void vmbus_chan_release(struct kobject *kobj)
1281 struct vmbus_channel *channel
1282 = container_of(kobj, struct vmbus_channel, kobj);
1284 kfree_rcu(channel, rcu);
1287 struct vmbus_chan_attribute {
1288 struct attribute attr;
1289 ssize_t (*show)(const struct vmbus_channel *chan, char *buf);
1290 ssize_t (*store)(struct vmbus_channel *chan,
1291 const char *buf, size_t count);
1293 #define VMBUS_CHAN_ATTR(_name, _mode, _show, _store) \
1294 struct vmbus_chan_attribute chan_attr_##_name \
1295 = __ATTR(_name, _mode, _show, _store)
1296 #define VMBUS_CHAN_ATTR_RW(_name) \
1297 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RW(_name)
1298 #define VMBUS_CHAN_ATTR_RO(_name) \
1299 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RO(_name)
1300 #define VMBUS_CHAN_ATTR_WO(_name) \
1301 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_WO(_name)
1303 static ssize_t vmbus_chan_attr_show(struct kobject *kobj,
1304 struct attribute *attr, char *buf)
1306 const struct vmbus_chan_attribute *attribute
1307 = container_of(attr, struct vmbus_chan_attribute, attr);
1308 const struct vmbus_channel *chan
1309 = container_of(kobj, struct vmbus_channel, kobj);
1311 if (!attribute->show)
1312 return -EIO;
1314 if (chan->state != CHANNEL_OPENED_STATE)
1315 return -EINVAL;
1317 return attribute->show(chan, buf);
1320 static const struct sysfs_ops vmbus_chan_sysfs_ops = {
1321 .show = vmbus_chan_attr_show,
1324 static ssize_t out_mask_show(const struct vmbus_channel *channel, char *buf)
1326 const struct hv_ring_buffer_info *rbi = &channel->outbound;
1328 return sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1330 static VMBUS_CHAN_ATTR_RO(out_mask);
1332 static ssize_t in_mask_show(const struct vmbus_channel *channel, char *buf)
1334 const struct hv_ring_buffer_info *rbi = &channel->inbound;
1336 return sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1338 static VMBUS_CHAN_ATTR_RO(in_mask);
1340 static ssize_t read_avail_show(const struct vmbus_channel *channel, char *buf)
1342 const struct hv_ring_buffer_info *rbi = &channel->inbound;
1344 return sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi));
1346 static VMBUS_CHAN_ATTR_RO(read_avail);
1348 static ssize_t write_avail_show(const struct vmbus_channel *channel, char *buf)
1350 const struct hv_ring_buffer_info *rbi = &channel->outbound;
1352 return sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi));
1354 static VMBUS_CHAN_ATTR_RO(write_avail);
1356 static ssize_t show_target_cpu(const struct vmbus_channel *channel, char *buf)
1358 return sprintf(buf, "%u\n", channel->target_cpu);
1360 static VMBUS_CHAN_ATTR(cpu, S_IRUGO, show_target_cpu, NULL);
1362 static ssize_t channel_pending_show(const struct vmbus_channel *channel,
1363 char *buf)
1365 return sprintf(buf, "%d\n",
1366 channel_pending(channel,
1367 vmbus_connection.monitor_pages[1]));
1369 static VMBUS_CHAN_ATTR(pending, S_IRUGO, channel_pending_show, NULL);
1371 static ssize_t channel_latency_show(const struct vmbus_channel *channel,
1372 char *buf)
1374 return sprintf(buf, "%d\n",
1375 channel_latency(channel,
1376 vmbus_connection.monitor_pages[1]));
1378 static VMBUS_CHAN_ATTR(latency, S_IRUGO, channel_latency_show, NULL);
1380 static ssize_t channel_interrupts_show(const struct vmbus_channel *channel, char *buf)
1382 return sprintf(buf, "%llu\n", channel->interrupts);
1384 static VMBUS_CHAN_ATTR(interrupts, S_IRUGO, channel_interrupts_show, NULL);
1386 static ssize_t channel_events_show(const struct vmbus_channel *channel, char *buf)
1388 return sprintf(buf, "%llu\n", channel->sig_events);
1390 static VMBUS_CHAN_ATTR(events, S_IRUGO, channel_events_show, NULL);
1392 static ssize_t subchannel_monitor_id_show(const struct vmbus_channel *channel,
1393 char *buf)
1395 return sprintf(buf, "%u\n", channel->offermsg.monitorid);
1397 static VMBUS_CHAN_ATTR(monitor_id, S_IRUGO, subchannel_monitor_id_show, NULL);
1399 static ssize_t subchannel_id_show(const struct vmbus_channel *channel,
1400 char *buf)
1402 return sprintf(buf, "%u\n",
1403 channel->offermsg.offer.sub_channel_index);
1405 static VMBUS_CHAN_ATTR_RO(subchannel_id);
1407 static struct attribute *vmbus_chan_attrs[] = {
1408 &chan_attr_out_mask.attr,
1409 &chan_attr_in_mask.attr,
1410 &chan_attr_read_avail.attr,
1411 &chan_attr_write_avail.attr,
1412 &chan_attr_cpu.attr,
1413 &chan_attr_pending.attr,
1414 &chan_attr_latency.attr,
1415 &chan_attr_interrupts.attr,
1416 &chan_attr_events.attr,
1417 &chan_attr_monitor_id.attr,
1418 &chan_attr_subchannel_id.attr,
1419 NULL
1422 static struct kobj_type vmbus_chan_ktype = {
1423 .sysfs_ops = &vmbus_chan_sysfs_ops,
1424 .release = vmbus_chan_release,
1425 .default_attrs = vmbus_chan_attrs,
1429 * vmbus_add_channel_kobj - setup a sub-directory under device/channels
1431 int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
1433 struct kobject *kobj = &channel->kobj;
1434 u32 relid = channel->offermsg.child_relid;
1435 int ret;
1437 kobj->kset = dev->channels_kset;
1438 ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
1439 "%u", relid);
1440 if (ret)
1441 return ret;
1443 kobject_uevent(kobj, KOBJ_ADD);
1445 return 0;
1449 * vmbus_device_create - Creates and registers a new child device
1450 * on the vmbus.
1452 struct hv_device *vmbus_device_create(const uuid_le *type,
1453 const uuid_le *instance,
1454 struct vmbus_channel *channel)
1456 struct hv_device *child_device_obj;
1458 child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
1459 if (!child_device_obj) {
1460 pr_err("Unable to allocate device object for child device\n");
1461 return NULL;
1464 child_device_obj->channel = channel;
1465 memcpy(&child_device_obj->dev_type, type, sizeof(uuid_le));
1466 memcpy(&child_device_obj->dev_instance, instance,
1467 sizeof(uuid_le));
1468 child_device_obj->vendor_id = 0x1414; /* MSFT vendor ID */
1471 return child_device_obj;
1475 * vmbus_device_register - Register the child device
1477 int vmbus_device_register(struct hv_device *child_device_obj)
1479 struct kobject *kobj = &child_device_obj->device.kobj;
1480 int ret;
1482 dev_set_name(&child_device_obj->device, "%pUl",
1483 child_device_obj->channel->offermsg.offer.if_instance.b);
1485 child_device_obj->device.bus = &hv_bus;
1486 child_device_obj->device.parent = &hv_acpi_dev->dev;
1487 child_device_obj->device.release = vmbus_device_release;
1490 * Register with the LDM. This will kick off the driver/device
1491 * binding...which will eventually call vmbus_match() and vmbus_probe()
1493 ret = device_register(&child_device_obj->device);
1494 if (ret) {
1495 pr_err("Unable to register child device\n");
1496 return ret;
1499 child_device_obj->channels_kset = kset_create_and_add("channels",
1500 NULL, kobj);
1501 if (!child_device_obj->channels_kset) {
1502 ret = -ENOMEM;
1503 goto err_dev_unregister;
1506 ret = vmbus_add_channel_kobj(child_device_obj,
1507 child_device_obj->channel);
1508 if (ret) {
1509 pr_err("Unable to register primary channeln");
1510 goto err_kset_unregister;
1513 return 0;
1515 err_kset_unregister:
1516 kset_unregister(child_device_obj->channels_kset);
1518 err_dev_unregister:
1519 device_unregister(&child_device_obj->device);
1520 return ret;
1524 * vmbus_device_unregister - Remove the specified child device
1525 * from the vmbus.
1527 void vmbus_device_unregister(struct hv_device *device_obj)
1529 pr_debug("child device %s unregistered\n",
1530 dev_name(&device_obj->device));
1532 kset_unregister(device_obj->channels_kset);
1535 * Kick off the process of unregistering the device.
1536 * This will call vmbus_remove() and eventually vmbus_device_release()
1538 device_unregister(&device_obj->device);
1543 * VMBUS is an acpi enumerated device. Get the information we
1544 * need from DSDT.
1546 #define VTPM_BASE_ADDRESS 0xfed40000
1547 static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
1549 resource_size_t start = 0;
1550 resource_size_t end = 0;
1551 struct resource *new_res;
1552 struct resource **old_res = &hyperv_mmio;
1553 struct resource **prev_res = NULL;
1555 switch (res->type) {
1558 * "Address" descriptors are for bus windows. Ignore
1559 * "memory" descriptors, which are for registers on
1560 * devices.
1562 case ACPI_RESOURCE_TYPE_ADDRESS32:
1563 start = res->data.address32.address.minimum;
1564 end = res->data.address32.address.maximum;
1565 break;
1567 case ACPI_RESOURCE_TYPE_ADDRESS64:
1568 start = res->data.address64.address.minimum;
1569 end = res->data.address64.address.maximum;
1570 break;
1572 default:
1573 /* Unused resource type */
1574 return AE_OK;
1578 * Ignore ranges that are below 1MB, as they're not
1579 * necessary or useful here.
1581 if (end < 0x100000)
1582 return AE_OK;
1584 new_res = kzalloc(sizeof(*new_res), GFP_ATOMIC);
1585 if (!new_res)
1586 return AE_NO_MEMORY;
1588 /* If this range overlaps the virtual TPM, truncate it. */
1589 if (end > VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS)
1590 end = VTPM_BASE_ADDRESS;
1592 new_res->name = "hyperv mmio";
1593 new_res->flags = IORESOURCE_MEM;
1594 new_res->start = start;
1595 new_res->end = end;
1598 * If two ranges are adjacent, merge them.
1600 do {
1601 if (!*old_res) {
1602 *old_res = new_res;
1603 break;
1606 if (((*old_res)->end + 1) == new_res->start) {
1607 (*old_res)->end = new_res->end;
1608 kfree(new_res);
1609 break;
1612 if ((*old_res)->start == new_res->end + 1) {
1613 (*old_res)->start = new_res->start;
1614 kfree(new_res);
1615 break;
1618 if ((*old_res)->start > new_res->end) {
1619 new_res->sibling = *old_res;
1620 if (prev_res)
1621 (*prev_res)->sibling = new_res;
1622 *old_res = new_res;
1623 break;
1626 prev_res = old_res;
1627 old_res = &(*old_res)->sibling;
1629 } while (1);
1631 return AE_OK;
1634 static int vmbus_acpi_remove(struct acpi_device *device)
1636 struct resource *cur_res;
1637 struct resource *next_res;
1639 if (hyperv_mmio) {
1640 if (fb_mmio) {
1641 __release_region(hyperv_mmio, fb_mmio->start,
1642 resource_size(fb_mmio));
1643 fb_mmio = NULL;
1646 for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) {
1647 next_res = cur_res->sibling;
1648 kfree(cur_res);
1652 return 0;
1655 static void vmbus_reserve_fb(void)
1657 int size;
1659 * Make a claim for the frame buffer in the resource tree under the
1660 * first node, which will be the one below 4GB. The length seems to
1661 * be underreported, particularly in a Generation 1 VM. So start out
1662 * reserving a larger area and make it smaller until it succeeds.
1665 if (screen_info.lfb_base) {
1666 if (efi_enabled(EFI_BOOT))
1667 size = max_t(__u32, screen_info.lfb_size, 0x800000);
1668 else
1669 size = max_t(__u32, screen_info.lfb_size, 0x4000000);
1671 for (; !fb_mmio && (size >= 0x100000); size >>= 1) {
1672 fb_mmio = __request_region(hyperv_mmio,
1673 screen_info.lfb_base, size,
1674 fb_mmio_name, 0);
1680 * vmbus_allocate_mmio() - Pick a memory-mapped I/O range.
1681 * @new: If successful, supplied a pointer to the
1682 * allocated MMIO space.
1683 * @device_obj: Identifies the caller
1684 * @min: Minimum guest physical address of the
1685 * allocation
1686 * @max: Maximum guest physical address
1687 * @size: Size of the range to be allocated
1688 * @align: Alignment of the range to be allocated
1689 * @fb_overlap_ok: Whether this allocation can be allowed
1690 * to overlap the video frame buffer.
1692 * This function walks the resources granted to VMBus by the
1693 * _CRS object in the ACPI namespace underneath the parent
1694 * "bridge" whether that's a root PCI bus in the Generation 1
1695 * case or a Module Device in the Generation 2 case. It then
1696 * attempts to allocate from the global MMIO pool in a way that
1697 * matches the constraints supplied in these parameters and by
1698 * that _CRS.
1700 * Return: 0 on success, -errno on failure
1702 int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
1703 resource_size_t min, resource_size_t max,
1704 resource_size_t size, resource_size_t align,
1705 bool fb_overlap_ok)
1707 struct resource *iter, *shadow;
1708 resource_size_t range_min, range_max, start;
1709 const char *dev_n = dev_name(&device_obj->device);
1710 int retval;
1712 retval = -ENXIO;
1713 down(&hyperv_mmio_lock);
1716 * If overlaps with frame buffers are allowed, then first attempt to
1717 * make the allocation from within the reserved region. Because it
1718 * is already reserved, no shadow allocation is necessary.
1720 if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) &&
1721 !(max < fb_mmio->start)) {
1723 range_min = fb_mmio->start;
1724 range_max = fb_mmio->end;
1725 start = (range_min + align - 1) & ~(align - 1);
1726 for (; start + size - 1 <= range_max; start += align) {
1727 *new = request_mem_region_exclusive(start, size, dev_n);
1728 if (*new) {
1729 retval = 0;
1730 goto exit;
1735 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
1736 if ((iter->start >= max) || (iter->end <= min))
1737 continue;
1739 range_min = iter->start;
1740 range_max = iter->end;
1741 start = (range_min + align - 1) & ~(align - 1);
1742 for (; start + size - 1 <= range_max; start += align) {
1743 shadow = __request_region(iter, start, size, NULL,
1744 IORESOURCE_BUSY);
1745 if (!shadow)
1746 continue;
1748 *new = request_mem_region_exclusive(start, size, dev_n);
1749 if (*new) {
1750 shadow->name = (char *)*new;
1751 retval = 0;
1752 goto exit;
1755 __release_region(iter, start, size);
1759 exit:
1760 up(&hyperv_mmio_lock);
1761 return retval;
1763 EXPORT_SYMBOL_GPL(vmbus_allocate_mmio);
1766 * vmbus_free_mmio() - Free a memory-mapped I/O range.
1767 * @start: Base address of region to release.
1768 * @size: Size of the range to be allocated
1770 * This function releases anything requested by
1771 * vmbus_mmio_allocate().
1773 void vmbus_free_mmio(resource_size_t start, resource_size_t size)
1775 struct resource *iter;
1777 down(&hyperv_mmio_lock);
1778 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
1779 if ((iter->start >= start + size) || (iter->end <= start))
1780 continue;
1782 __release_region(iter, start, size);
1784 release_mem_region(start, size);
1785 up(&hyperv_mmio_lock);
1788 EXPORT_SYMBOL_GPL(vmbus_free_mmio);
1790 static int vmbus_acpi_add(struct acpi_device *device)
1792 acpi_status result;
1793 int ret_val = -ENODEV;
1794 struct acpi_device *ancestor;
1796 hv_acpi_dev = device;
1798 result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
1799 vmbus_walk_resources, NULL);
1801 if (ACPI_FAILURE(result))
1802 goto acpi_walk_err;
1804 * Some ancestor of the vmbus acpi device (Gen1 or Gen2
1805 * firmware) is the VMOD that has the mmio ranges. Get that.
1807 for (ancestor = device->parent; ancestor; ancestor = ancestor->parent) {
1808 result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS,
1809 vmbus_walk_resources, NULL);
1811 if (ACPI_FAILURE(result))
1812 continue;
1813 if (hyperv_mmio) {
1814 vmbus_reserve_fb();
1815 break;
1818 ret_val = 0;
1820 acpi_walk_err:
1821 complete(&probe_event);
1822 if (ret_val)
1823 vmbus_acpi_remove(device);
1824 return ret_val;
1827 static const struct acpi_device_id vmbus_acpi_device_ids[] = {
1828 {"VMBUS", 0},
1829 {"VMBus", 0},
1830 {"", 0},
1832 MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
1834 static struct acpi_driver vmbus_acpi_driver = {
1835 .name = "vmbus",
1836 .ids = vmbus_acpi_device_ids,
1837 .ops = {
1838 .add = vmbus_acpi_add,
1839 .remove = vmbus_acpi_remove,
1843 static void hv_kexec_handler(void)
1845 hv_synic_clockevents_cleanup();
1846 vmbus_initiate_unload(false);
1847 vmbus_connection.conn_state = DISCONNECTED;
1848 /* Make sure conn_state is set as hv_synic_cleanup checks for it */
1849 mb();
1850 cpuhp_remove_state(hyperv_cpuhp_online);
1851 hyperv_cleanup();
1854 static void hv_crash_handler(struct pt_regs *regs)
1856 vmbus_initiate_unload(true);
1858 * In crash handler we can't schedule synic cleanup for all CPUs,
1859 * doing the cleanup for current CPU only. This should be sufficient
1860 * for kdump.
1862 vmbus_connection.conn_state = DISCONNECTED;
1863 hv_synic_cleanup(smp_processor_id());
1864 hyperv_cleanup();
1867 static int __init hv_acpi_init(void)
1869 int ret, t;
1871 if (!hv_is_hyperv_initialized())
1872 return -ENODEV;
1874 init_completion(&probe_event);
1877 * Get ACPI resources first.
1879 ret = acpi_bus_register_driver(&vmbus_acpi_driver);
1881 if (ret)
1882 return ret;
1884 t = wait_for_completion_timeout(&probe_event, 5*HZ);
1885 if (t == 0) {
1886 ret = -ETIMEDOUT;
1887 goto cleanup;
1890 ret = vmbus_bus_init();
1891 if (ret)
1892 goto cleanup;
1894 hv_setup_kexec_handler(hv_kexec_handler);
1895 hv_setup_crash_handler(hv_crash_handler);
1897 return 0;
1899 cleanup:
1900 acpi_bus_unregister_driver(&vmbus_acpi_driver);
1901 hv_acpi_dev = NULL;
1902 return ret;
1905 static void __exit vmbus_exit(void)
1907 int cpu;
1909 hv_remove_kexec_handler();
1910 hv_remove_crash_handler();
1911 vmbus_connection.conn_state = DISCONNECTED;
1912 hv_synic_clockevents_cleanup();
1913 vmbus_disconnect();
1914 hv_remove_vmbus_irq();
1915 for_each_online_cpu(cpu) {
1916 struct hv_per_cpu_context *hv_cpu
1917 = per_cpu_ptr(hv_context.cpu_context, cpu);
1919 tasklet_kill(&hv_cpu->msg_dpc);
1921 vmbus_free_channels();
1923 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
1924 kmsg_dump_unregister(&hv_kmsg_dumper);
1925 unregister_die_notifier(&hyperv_die_block);
1926 atomic_notifier_chain_unregister(&panic_notifier_list,
1927 &hyperv_panic_block);
1930 free_page((unsigned long)hv_panic_page);
1931 unregister_sysctl_table(hv_ctl_table_hdr);
1932 hv_ctl_table_hdr = NULL;
1933 bus_unregister(&hv_bus);
1935 cpuhp_remove_state(hyperv_cpuhp_online);
1936 hv_synic_free();
1937 acpi_bus_unregister_driver(&vmbus_acpi_driver);
1941 MODULE_LICENSE("GPL");
1943 subsys_initcall(hv_acpi_init);
1944 module_exit(vmbus_exit);