xref: /openbmc/linux/drivers/hv/vmbus_drv.c (revision 113094f7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2009, Microsoft Corporation.
4  *
5  * Authors:
6  *   Haiyang Zhang <haiyangz@microsoft.com>
7  *   Hank Janssen  <hjanssen@microsoft.com>
8  *   K. Y. Srinivasan <kys@microsoft.com>
9  */
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 
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>
25 
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"
35 
36 struct vmbus_dynid {
37 	struct list_head node;
38 	struct hv_vmbus_device_id id;
39 };
40 
41 static struct acpi_device  *hv_acpi_dev;
42 
43 static struct completion probe_event;
44 
45 static int hyperv_cpuhp_online;
46 
47 static void *hv_panic_page;
48 
49 static int hyperv_panic_event(struct notifier_block *nb, unsigned long val,
50 			      void *args)
51 {
52 	struct pt_regs *regs;
53 
54 	regs = current_pt_regs();
55 
56 	hyperv_report_panic(regs, val);
57 	return NOTIFY_DONE;
58 }
59 
60 static int hyperv_die_event(struct notifier_block *nb, unsigned long val,
61 			    void *args)
62 {
63 	struct die_args *die = (struct die_args *)args;
64 	struct pt_regs *regs = die->regs;
65 
66 	hyperv_report_panic(regs, val);
67 	return NOTIFY_DONE;
68 }
69 
70 static struct notifier_block hyperv_die_block = {
71 	.notifier_call = hyperv_die_event,
72 };
73 static struct notifier_block hyperv_panic_block = {
74 	.notifier_call = hyperv_panic_event,
75 };
76 
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);
81 
82 static int vmbus_exists(void)
83 {
84 	if (hv_acpi_dev == NULL)
85 		return -ENODEV;
86 
87 	return 0;
88 }
89 
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)
92 {
93 	int i;
94 	for (i = 0; i < VMBUS_ALIAS_LEN; i += 2)
95 		sprintf(&alias_name[i], "%02x", hv_dev->dev_type.b[i/2]);
96 }
97 
98 static u8 channel_monitor_group(const struct vmbus_channel *channel)
99 {
100 	return (u8)channel->offermsg.monitorid / 32;
101 }
102 
103 static u8 channel_monitor_offset(const struct vmbus_channel *channel)
104 {
105 	return (u8)channel->offermsg.monitorid % 32;
106 }
107 
108 static u32 channel_pending(const struct vmbus_channel *channel,
109 			   const struct hv_monitor_page *monitor_page)
110 {
111 	u8 monitor_group = channel_monitor_group(channel);
112 
113 	return monitor_page->trigger_group[monitor_group].pending;
114 }
115 
116 static u32 channel_latency(const struct vmbus_channel *channel,
117 			   const struct hv_monitor_page *monitor_page)
118 {
119 	u8 monitor_group = channel_monitor_group(channel);
120 	u8 monitor_offset = channel_monitor_offset(channel);
121 
122 	return monitor_page->latency[monitor_group][monitor_offset];
123 }
124 
125 static u32 channel_conn_id(struct vmbus_channel *channel,
126 			   struct hv_monitor_page *monitor_page)
127 {
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;
131 }
132 
133 static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
134 		       char *buf)
135 {
136 	struct hv_device *hv_dev = device_to_hv_device(dev);
137 
138 	if (!hv_dev->channel)
139 		return -ENODEV;
140 	return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
141 }
142 static DEVICE_ATTR_RO(id);
143 
144 static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
145 			  char *buf)
146 {
147 	struct hv_device *hv_dev = device_to_hv_device(dev);
148 
149 	if (!hv_dev->channel)
150 		return -ENODEV;
151 	return sprintf(buf, "%d\n", hv_dev->channel->state);
152 }
153 static DEVICE_ATTR_RO(state);
154 
155 static ssize_t monitor_id_show(struct device *dev,
156 			       struct device_attribute *dev_attr, char *buf)
157 {
158 	struct hv_device *hv_dev = device_to_hv_device(dev);
159 
160 	if (!hv_dev->channel)
161 		return -ENODEV;
162 	return sprintf(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
163 }
164 static DEVICE_ATTR_RO(monitor_id);
165 
166 static ssize_t class_id_show(struct device *dev,
167 			       struct device_attribute *dev_attr, char *buf)
168 {
169 	struct hv_device *hv_dev = device_to_hv_device(dev);
170 
171 	if (!hv_dev->channel)
172 		return -ENODEV;
173 	return sprintf(buf, "{%pUl}\n",
174 		       hv_dev->channel->offermsg.offer.if_type.b);
175 }
176 static DEVICE_ATTR_RO(class_id);
177 
178 static ssize_t device_id_show(struct device *dev,
179 			      struct device_attribute *dev_attr, char *buf)
180 {
181 	struct hv_device *hv_dev = device_to_hv_device(dev);
182 
183 	if (!hv_dev->channel)
184 		return -ENODEV;
185 	return sprintf(buf, "{%pUl}\n",
186 		       hv_dev->channel->offermsg.offer.if_instance.b);
187 }
188 static DEVICE_ATTR_RO(device_id);
189 
190 static ssize_t modalias_show(struct device *dev,
191 			     struct device_attribute *dev_attr, char *buf)
192 {
193 	struct hv_device *hv_dev = device_to_hv_device(dev);
194 	char alias_name[VMBUS_ALIAS_LEN + 1];
195 
196 	print_alias_name(hv_dev, alias_name);
197 	return sprintf(buf, "vmbus:%s\n", alias_name);
198 }
199 static DEVICE_ATTR_RO(modalias);
200 
201 #ifdef CONFIG_NUMA
202 static ssize_t numa_node_show(struct device *dev,
203 			      struct device_attribute *attr, char *buf)
204 {
205 	struct hv_device *hv_dev = device_to_hv_device(dev);
206 
207 	if (!hv_dev->channel)
208 		return -ENODEV;
209 
210 	return sprintf(buf, "%d\n", hv_dev->channel->numa_node);
211 }
212 static DEVICE_ATTR_RO(numa_node);
213 #endif
214 
215 static ssize_t server_monitor_pending_show(struct device *dev,
216 					   struct device_attribute *dev_attr,
217 					   char *buf)
218 {
219 	struct hv_device *hv_dev = device_to_hv_device(dev);
220 
221 	if (!hv_dev->channel)
222 		return -ENODEV;
223 	return sprintf(buf, "%d\n",
224 		       channel_pending(hv_dev->channel,
225 				       vmbus_connection.monitor_pages[0]));
226 }
227 static DEVICE_ATTR_RO(server_monitor_pending);
228 
229 static ssize_t client_monitor_pending_show(struct device *dev,
230 					   struct device_attribute *dev_attr,
231 					   char *buf)
232 {
233 	struct hv_device *hv_dev = device_to_hv_device(dev);
234 
235 	if (!hv_dev->channel)
236 		return -ENODEV;
237 	return sprintf(buf, "%d\n",
238 		       channel_pending(hv_dev->channel,
239 				       vmbus_connection.monitor_pages[1]));
240 }
241 static DEVICE_ATTR_RO(client_monitor_pending);
242 
243 static ssize_t server_monitor_latency_show(struct device *dev,
244 					   struct device_attribute *dev_attr,
245 					   char *buf)
246 {
247 	struct hv_device *hv_dev = device_to_hv_device(dev);
248 
249 	if (!hv_dev->channel)
250 		return -ENODEV;
251 	return sprintf(buf, "%d\n",
252 		       channel_latency(hv_dev->channel,
253 				       vmbus_connection.monitor_pages[0]));
254 }
255 static DEVICE_ATTR_RO(server_monitor_latency);
256 
257 static ssize_t client_monitor_latency_show(struct device *dev,
258 					   struct device_attribute *dev_attr,
259 					   char *buf)
260 {
261 	struct hv_device *hv_dev = device_to_hv_device(dev);
262 
263 	if (!hv_dev->channel)
264 		return -ENODEV;
265 	return sprintf(buf, "%d\n",
266 		       channel_latency(hv_dev->channel,
267 				       vmbus_connection.monitor_pages[1]));
268 }
269 static DEVICE_ATTR_RO(client_monitor_latency);
270 
271 static ssize_t server_monitor_conn_id_show(struct device *dev,
272 					   struct device_attribute *dev_attr,
273 					   char *buf)
274 {
275 	struct hv_device *hv_dev = device_to_hv_device(dev);
276 
277 	if (!hv_dev->channel)
278 		return -ENODEV;
279 	return sprintf(buf, "%d\n",
280 		       channel_conn_id(hv_dev->channel,
281 				       vmbus_connection.monitor_pages[0]));
282 }
283 static DEVICE_ATTR_RO(server_monitor_conn_id);
284 
285 static ssize_t client_monitor_conn_id_show(struct device *dev,
286 					   struct device_attribute *dev_attr,
287 					   char *buf)
288 {
289 	struct hv_device *hv_dev = device_to_hv_device(dev);
290 
291 	if (!hv_dev->channel)
292 		return -ENODEV;
293 	return sprintf(buf, "%d\n",
294 		       channel_conn_id(hv_dev->channel,
295 				       vmbus_connection.monitor_pages[1]));
296 }
297 static DEVICE_ATTR_RO(client_monitor_conn_id);
298 
299 static ssize_t out_intr_mask_show(struct device *dev,
300 				  struct device_attribute *dev_attr, char *buf)
301 {
302 	struct hv_device *hv_dev = device_to_hv_device(dev);
303 	struct hv_ring_buffer_debug_info outbound;
304 	int ret;
305 
306 	if (!hv_dev->channel)
307 		return -ENODEV;
308 
309 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
310 					  &outbound);
311 	if (ret < 0)
312 		return ret;
313 
314 	return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
315 }
316 static DEVICE_ATTR_RO(out_intr_mask);
317 
318 static ssize_t out_read_index_show(struct device *dev,
319 				   struct device_attribute *dev_attr, char *buf)
320 {
321 	struct hv_device *hv_dev = device_to_hv_device(dev);
322 	struct hv_ring_buffer_debug_info outbound;
323 	int ret;
324 
325 	if (!hv_dev->channel)
326 		return -ENODEV;
327 
328 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
329 					  &outbound);
330 	if (ret < 0)
331 		return ret;
332 	return sprintf(buf, "%d\n", outbound.current_read_index);
333 }
334 static DEVICE_ATTR_RO(out_read_index);
335 
336 static ssize_t out_write_index_show(struct device *dev,
337 				    struct device_attribute *dev_attr,
338 				    char *buf)
339 {
340 	struct hv_device *hv_dev = device_to_hv_device(dev);
341 	struct hv_ring_buffer_debug_info outbound;
342 	int ret;
343 
344 	if (!hv_dev->channel)
345 		return -ENODEV;
346 
347 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
348 					  &outbound);
349 	if (ret < 0)
350 		return ret;
351 	return sprintf(buf, "%d\n", outbound.current_write_index);
352 }
353 static DEVICE_ATTR_RO(out_write_index);
354 
355 static ssize_t out_read_bytes_avail_show(struct device *dev,
356 					 struct device_attribute *dev_attr,
357 					 char *buf)
358 {
359 	struct hv_device *hv_dev = device_to_hv_device(dev);
360 	struct hv_ring_buffer_debug_info outbound;
361 	int ret;
362 
363 	if (!hv_dev->channel)
364 		return -ENODEV;
365 
366 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
367 					  &outbound);
368 	if (ret < 0)
369 		return ret;
370 	return sprintf(buf, "%d\n", outbound.bytes_avail_toread);
371 }
372 static DEVICE_ATTR_RO(out_read_bytes_avail);
373 
374 static ssize_t out_write_bytes_avail_show(struct device *dev,
375 					  struct device_attribute *dev_attr,
376 					  char *buf)
377 {
378 	struct hv_device *hv_dev = device_to_hv_device(dev);
379 	struct hv_ring_buffer_debug_info outbound;
380 	int ret;
381 
382 	if (!hv_dev->channel)
383 		return -ENODEV;
384 
385 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
386 					  &outbound);
387 	if (ret < 0)
388 		return ret;
389 	return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
390 }
391 static DEVICE_ATTR_RO(out_write_bytes_avail);
392 
393 static ssize_t in_intr_mask_show(struct device *dev,
394 				 struct device_attribute *dev_attr, char *buf)
395 {
396 	struct hv_device *hv_dev = device_to_hv_device(dev);
397 	struct hv_ring_buffer_debug_info inbound;
398 	int ret;
399 
400 	if (!hv_dev->channel)
401 		return -ENODEV;
402 
403 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
404 	if (ret < 0)
405 		return ret;
406 
407 	return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
408 }
409 static DEVICE_ATTR_RO(in_intr_mask);
410 
411 static ssize_t in_read_index_show(struct device *dev,
412 				  struct device_attribute *dev_attr, char *buf)
413 {
414 	struct hv_device *hv_dev = device_to_hv_device(dev);
415 	struct hv_ring_buffer_debug_info inbound;
416 	int ret;
417 
418 	if (!hv_dev->channel)
419 		return -ENODEV;
420 
421 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
422 	if (ret < 0)
423 		return ret;
424 
425 	return sprintf(buf, "%d\n", inbound.current_read_index);
426 }
427 static DEVICE_ATTR_RO(in_read_index);
428 
429 static ssize_t in_write_index_show(struct device *dev,
430 				   struct device_attribute *dev_attr, char *buf)
431 {
432 	struct hv_device *hv_dev = device_to_hv_device(dev);
433 	struct hv_ring_buffer_debug_info inbound;
434 	int ret;
435 
436 	if (!hv_dev->channel)
437 		return -ENODEV;
438 
439 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
440 	if (ret < 0)
441 		return ret;
442 
443 	return sprintf(buf, "%d\n", inbound.current_write_index);
444 }
445 static DEVICE_ATTR_RO(in_write_index);
446 
447 static ssize_t in_read_bytes_avail_show(struct device *dev,
448 					struct device_attribute *dev_attr,
449 					char *buf)
450 {
451 	struct hv_device *hv_dev = device_to_hv_device(dev);
452 	struct hv_ring_buffer_debug_info inbound;
453 	int ret;
454 
455 	if (!hv_dev->channel)
456 		return -ENODEV;
457 
458 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
459 	if (ret < 0)
460 		return ret;
461 
462 	return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
463 }
464 static DEVICE_ATTR_RO(in_read_bytes_avail);
465 
466 static ssize_t in_write_bytes_avail_show(struct device *dev,
467 					 struct device_attribute *dev_attr,
468 					 char *buf)
469 {
470 	struct hv_device *hv_dev = device_to_hv_device(dev);
471 	struct hv_ring_buffer_debug_info inbound;
472 	int ret;
473 
474 	if (!hv_dev->channel)
475 		return -ENODEV;
476 
477 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
478 	if (ret < 0)
479 		return ret;
480 
481 	return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
482 }
483 static DEVICE_ATTR_RO(in_write_bytes_avail);
484 
485 static ssize_t channel_vp_mapping_show(struct device *dev,
486 				       struct device_attribute *dev_attr,
487 				       char *buf)
488 {
489 	struct hv_device *hv_dev = device_to_hv_device(dev);
490 	struct vmbus_channel *channel = hv_dev->channel, *cur_sc;
491 	unsigned long flags;
492 	int buf_size = PAGE_SIZE, n_written, tot_written;
493 	struct list_head *cur;
494 
495 	if (!channel)
496 		return -ENODEV;
497 
498 	tot_written = snprintf(buf, buf_size, "%u:%u\n",
499 		channel->offermsg.child_relid, channel->target_cpu);
500 
501 	spin_lock_irqsave(&channel->lock, flags);
502 
503 	list_for_each(cur, &channel->sc_list) {
504 		if (tot_written >= buf_size - 1)
505 			break;
506 
507 		cur_sc = list_entry(cur, struct vmbus_channel, sc_list);
508 		n_written = scnprintf(buf + tot_written,
509 				     buf_size - tot_written,
510 				     "%u:%u\n",
511 				     cur_sc->offermsg.child_relid,
512 				     cur_sc->target_cpu);
513 		tot_written += n_written;
514 	}
515 
516 	spin_unlock_irqrestore(&channel->lock, flags);
517 
518 	return tot_written;
519 }
520 static DEVICE_ATTR_RO(channel_vp_mapping);
521 
522 static ssize_t vendor_show(struct device *dev,
523 			   struct device_attribute *dev_attr,
524 			   char *buf)
525 {
526 	struct hv_device *hv_dev = device_to_hv_device(dev);
527 	return sprintf(buf, "0x%x\n", hv_dev->vendor_id);
528 }
529 static DEVICE_ATTR_RO(vendor);
530 
531 static ssize_t device_show(struct device *dev,
532 			   struct device_attribute *dev_attr,
533 			   char *buf)
534 {
535 	struct hv_device *hv_dev = device_to_hv_device(dev);
536 	return sprintf(buf, "0x%x\n", hv_dev->device_id);
537 }
538 static DEVICE_ATTR_RO(device);
539 
540 static ssize_t driver_override_store(struct device *dev,
541 				     struct device_attribute *attr,
542 				     const char *buf, size_t count)
543 {
544 	struct hv_device *hv_dev = device_to_hv_device(dev);
545 	char *driver_override, *old, *cp;
546 
547 	/* We need to keep extra room for a newline */
548 	if (count >= (PAGE_SIZE - 1))
549 		return -EINVAL;
550 
551 	driver_override = kstrndup(buf, count, GFP_KERNEL);
552 	if (!driver_override)
553 		return -ENOMEM;
554 
555 	cp = strchr(driver_override, '\n');
556 	if (cp)
557 		*cp = '\0';
558 
559 	device_lock(dev);
560 	old = hv_dev->driver_override;
561 	if (strlen(driver_override)) {
562 		hv_dev->driver_override = driver_override;
563 	} else {
564 		kfree(driver_override);
565 		hv_dev->driver_override = NULL;
566 	}
567 	device_unlock(dev);
568 
569 	kfree(old);
570 
571 	return count;
572 }
573 
574 static ssize_t driver_override_show(struct device *dev,
575 				    struct device_attribute *attr, char *buf)
576 {
577 	struct hv_device *hv_dev = device_to_hv_device(dev);
578 	ssize_t len;
579 
580 	device_lock(dev);
581 	len = snprintf(buf, PAGE_SIZE, "%s\n", hv_dev->driver_override);
582 	device_unlock(dev);
583 
584 	return len;
585 }
586 static DEVICE_ATTR_RW(driver_override);
587 
588 /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
589 static struct attribute *vmbus_dev_attrs[] = {
590 	&dev_attr_id.attr,
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,
596 #ifdef CONFIG_NUMA
597 	&dev_attr_numa_node.attr,
598 #endif
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,
619 	NULL,
620 };
621 
622 /*
623  * Device-level attribute_group callback function. Returns the permission for
624  * each attribute, and returns 0 if an attribute is not visible.
625  */
626 static umode_t vmbus_dev_attr_is_visible(struct kobject *kobj,
627 					 struct attribute *attr, int idx)
628 {
629 	struct device *dev = kobj_to_dev(kobj);
630 	const struct hv_device *hv_dev = device_to_hv_device(dev);
631 
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))
641 		return 0;
642 
643 	return attr->mode;
644 }
645 
646 static const struct attribute_group vmbus_dev_group = {
647 	.attrs = vmbus_dev_attrs,
648 	.is_visible = vmbus_dev_attr_is_visible
649 };
650 __ATTRIBUTE_GROUPS(vmbus_dev);
651 
652 /*
653  * vmbus_uevent - add uevent for our device
654  *
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
658  *
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.
662  */
663 static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
664 {
665 	struct hv_device *dev = device_to_hv_device(device);
666 	int ret;
667 	char alias_name[VMBUS_ALIAS_LEN + 1];
668 
669 	print_alias_name(dev, alias_name);
670 	ret = add_uevent_var(env, "MODALIAS=vmbus:%s", alias_name);
671 	return ret;
672 }
673 
674 static const struct hv_vmbus_device_id *
675 hv_vmbus_dev_match(const struct hv_vmbus_device_id *id, const guid_t *guid)
676 {
677 	if (id == NULL)
678 		return NULL; /* empty device table */
679 
680 	for (; !guid_is_null(&id->guid); id++)
681 		if (guid_equal(&id->guid, guid))
682 			return id;
683 
684 	return NULL;
685 }
686 
687 static const struct hv_vmbus_device_id *
688 hv_vmbus_dynid_match(struct hv_driver *drv, const guid_t *guid)
689 {
690 	const struct hv_vmbus_device_id *id = NULL;
691 	struct vmbus_dynid *dynid;
692 
693 	spin_lock(&drv->dynids.lock);
694 	list_for_each_entry(dynid, &drv->dynids.list, node) {
695 		if (guid_equal(&dynid->id.guid, guid)) {
696 			id = &dynid->id;
697 			break;
698 		}
699 	}
700 	spin_unlock(&drv->dynids.lock);
701 
702 	return id;
703 }
704 
705 static const struct hv_vmbus_device_id vmbus_device_null;
706 
707 /*
708  * Return a matching hv_vmbus_device_id pointer.
709  * If there is no match, return NULL.
710  */
711 static const struct hv_vmbus_device_id *hv_vmbus_get_id(struct hv_driver *drv,
712 							struct hv_device *dev)
713 {
714 	const guid_t *guid = &dev->dev_type;
715 	const struct hv_vmbus_device_id *id;
716 
717 	/* When driver_override is set, only bind to the matching driver */
718 	if (dev->driver_override && strcmp(dev->driver_override, drv->name))
719 		return NULL;
720 
721 	/* Look at the dynamic ids first, before the static ones */
722 	id = hv_vmbus_dynid_match(drv, guid);
723 	if (!id)
724 		id = hv_vmbus_dev_match(drv->id_table, guid);
725 
726 	/* driver_override will always match, send a dummy id */
727 	if (!id && dev->driver_override)
728 		id = &vmbus_device_null;
729 
730 	return id;
731 }
732 
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)
735 {
736 	struct vmbus_dynid *dynid;
737 
738 	dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
739 	if (!dynid)
740 		return -ENOMEM;
741 
742 	dynid->id.guid = *guid;
743 
744 	spin_lock(&drv->dynids.lock);
745 	list_add_tail(&dynid->node, &drv->dynids.list);
746 	spin_unlock(&drv->dynids.lock);
747 
748 	return driver_attach(&drv->driver);
749 }
750 
751 static void vmbus_free_dynids(struct hv_driver *drv)
752 {
753 	struct vmbus_dynid *dynid, *n;
754 
755 	spin_lock(&drv->dynids.lock);
756 	list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
757 		list_del(&dynid->node);
758 		kfree(dynid);
759 	}
760 	spin_unlock(&drv->dynids.lock);
761 }
762 
763 /*
764  * store_new_id - sysfs frontend to vmbus_add_dynid()
765  *
766  * Allow GUIDs to be added to an existing driver via sysfs.
767  */
768 static ssize_t new_id_store(struct device_driver *driver, const char *buf,
769 			    size_t count)
770 {
771 	struct hv_driver *drv = drv_to_hv_drv(driver);
772 	guid_t guid;
773 	ssize_t retval;
774 
775 	retval = guid_parse(buf, &guid);
776 	if (retval)
777 		return retval;
778 
779 	if (hv_vmbus_dynid_match(drv, &guid))
780 		return -EEXIST;
781 
782 	retval = vmbus_add_dynid(drv, &guid);
783 	if (retval)
784 		return retval;
785 	return count;
786 }
787 static DRIVER_ATTR_WO(new_id);
788 
789 /*
790  * store_remove_id - remove a PCI device ID from this driver
791  *
792  * Removes a dynamic pci device ID to this driver.
793  */
794 static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
795 			       size_t count)
796 {
797 	struct hv_driver *drv = drv_to_hv_drv(driver);
798 	struct vmbus_dynid *dynid, *n;
799 	guid_t guid;
800 	ssize_t retval;
801 
802 	retval = guid_parse(buf, &guid);
803 	if (retval)
804 		return retval;
805 
806 	retval = -ENODEV;
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;
810 
811 		if (guid_equal(&id->guid, &guid)) {
812 			list_del(&dynid->node);
813 			kfree(dynid);
814 			retval = count;
815 			break;
816 		}
817 	}
818 	spin_unlock(&drv->dynids.lock);
819 
820 	return retval;
821 }
822 static DRIVER_ATTR_WO(remove_id);
823 
824 static struct attribute *vmbus_drv_attrs[] = {
825 	&driver_attr_new_id.attr,
826 	&driver_attr_remove_id.attr,
827 	NULL,
828 };
829 ATTRIBUTE_GROUPS(vmbus_drv);
830 
831 
832 /*
833  * vmbus_match - Attempt to match the specified device to the specified driver
834  */
835 static int vmbus_match(struct device *device, struct device_driver *driver)
836 {
837 	struct hv_driver *drv = drv_to_hv_drv(driver);
838 	struct hv_device *hv_dev = device_to_hv_device(device);
839 
840 	/* The hv_sock driver handles all hv_sock offers. */
841 	if (is_hvsock_channel(hv_dev->channel))
842 		return drv->hvsock;
843 
844 	if (hv_vmbus_get_id(drv, hv_dev))
845 		return 1;
846 
847 	return 0;
848 }
849 
850 /*
851  * vmbus_probe - Add the new vmbus's child device
852  */
853 static int vmbus_probe(struct device *child_device)
854 {
855 	int ret = 0;
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;
860 
861 	dev_id = hv_vmbus_get_id(drv, dev);
862 	if (drv->probe) {
863 		ret = drv->probe(dev, dev_id);
864 		if (ret != 0)
865 			pr_err("probe failed for device %s (%d)\n",
866 			       dev_name(child_device), ret);
867 
868 	} else {
869 		pr_err("probe not set for driver %s\n",
870 		       dev_name(child_device));
871 		ret = -ENODEV;
872 	}
873 	return ret;
874 }
875 
876 /*
877  * vmbus_remove - Remove a vmbus device
878  */
879 static int vmbus_remove(struct device *child_device)
880 {
881 	struct hv_driver *drv;
882 	struct hv_device *dev = device_to_hv_device(child_device);
883 
884 	if (child_device->driver) {
885 		drv = drv_to_hv_drv(child_device->driver);
886 		if (drv->remove)
887 			drv->remove(dev);
888 	}
889 
890 	return 0;
891 }
892 
893 
894 /*
895  * vmbus_shutdown - Shutdown a vmbus device
896  */
897 static void vmbus_shutdown(struct device *child_device)
898 {
899 	struct hv_driver *drv;
900 	struct hv_device *dev = device_to_hv_device(child_device);
901 
902 
903 	/* The device may not be attached yet */
904 	if (!child_device->driver)
905 		return;
906 
907 	drv = drv_to_hv_drv(child_device->driver);
908 
909 	if (drv->shutdown)
910 		drv->shutdown(dev);
911 }
912 
913 
914 /*
915  * vmbus_device_release - Final callback release of the vmbus child device
916  */
917 static void vmbus_device_release(struct device *device)
918 {
919 	struct hv_device *hv_dev = device_to_hv_device(device);
920 	struct vmbus_channel *channel = hv_dev->channel;
921 
922 	mutex_lock(&vmbus_connection.channel_mutex);
923 	hv_process_channel_removal(channel);
924 	mutex_unlock(&vmbus_connection.channel_mutex);
925 	kfree(hv_dev);
926 }
927 
928 /* The one and only one */
929 static struct bus_type  hv_bus = {
930 	.name =		"vmbus",
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,
938 };
939 
940 struct onmessage_work_context {
941 	struct work_struct work;
942 	struct hv_message msg;
943 };
944 
945 static void vmbus_onmessage_work(struct work_struct *work)
946 {
947 	struct onmessage_work_context *ctx;
948 
949 	/* Do not process messages if we're in DISCONNECTED state */
950 	if (vmbus_connection.conn_state == DISCONNECTED)
951 		return;
952 
953 	ctx = container_of(work, struct onmessage_work_context,
954 			   work);
955 	vmbus_onmessage(&ctx->msg);
956 	kfree(ctx);
957 }
958 
959 void vmbus_on_msg_dpc(unsigned long data)
960 {
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 +
964 				  VMBUS_MESSAGE_SINT;
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;
969 
970 	if (message_type == HVMSG_NONE)
971 		/* no msg */
972 		return;
973 
974 	hdr = (struct vmbus_channel_message_header *)msg->u.payload;
975 
976 	trace_vmbus_on_msg_dpc(hdr);
977 
978 	if (hdr->msgtype >= CHANNELMSG_COUNT) {
979 		WARN_ONCE(1, "unknown msgtype=%d\n", hdr->msgtype);
980 		goto msg_handled;
981 	}
982 
983 	entry = &channel_message_table[hdr->msgtype];
984 	if (entry->handler_type	== VMHT_BLOCKING) {
985 		ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
986 		if (ctx == NULL)
987 			return;
988 
989 		INIT_WORK(&ctx->work, vmbus_onmessage_work);
990 		memcpy(&ctx->msg, msg, sizeof(*msg));
991 
992 		/*
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
996 		 * same CPU.
997 		 */
998 		switch (hdr->msgtype) {
999 		case CHANNELMSG_RESCIND_CHANNELOFFER:
1000 			/*
1001 			 * If we are handling the rescind message;
1002 			 * schedule the work on the global work queue.
1003 			 */
1004 			schedule_work_on(vmbus_connection.connect_cpu,
1005 					 &ctx->work);
1006 			break;
1007 
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,
1012 				      &ctx->work);
1013 			break;
1014 
1015 		default:
1016 			queue_work(vmbus_connection.work_queue, &ctx->work);
1017 		}
1018 	} else
1019 		entry->message_handler(hdr);
1020 
1021 msg_handled:
1022 	vmbus_signal_eom(msg, message_type);
1023 }
1024 
1025 
1026 /*
1027  * Direct callback for channels using other deferred processing
1028  */
1029 static void vmbus_channel_isr(struct vmbus_channel *channel)
1030 {
1031 	void (*callback_fn)(void *);
1032 
1033 	callback_fn = READ_ONCE(channel->onchannel_callback);
1034 	if (likely(callback_fn != NULL))
1035 		(*callback_fn)(channel->channel_callback_context);
1036 }
1037 
1038 /*
1039  * Schedule all channels with events pending
1040  */
1041 static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu)
1042 {
1043 	unsigned long *recv_int_page;
1044 	u32 maxbits, relid;
1045 
1046 	if (vmbus_proto_version < VERSION_WIN8) {
1047 		maxbits = MAX_NUM_CHANNELS_SUPPORTED;
1048 		recv_int_page = vmbus_connection.recv_int_page;
1049 	} else {
1050 		/*
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.
1054 		 */
1055 		void *page_addr = hv_cpu->synic_event_page;
1056 		union hv_synic_event_flags *event
1057 			= (union hv_synic_event_flags *)page_addr +
1058 						 VMBUS_MESSAGE_SINT;
1059 
1060 		maxbits = HV_EVENT_FLAGS_COUNT;
1061 		recv_int_page = event->flags;
1062 	}
1063 
1064 	if (unlikely(!recv_int_page))
1065 		return;
1066 
1067 	for_each_set_bit(relid, recv_int_page, maxbits) {
1068 		struct vmbus_channel *channel;
1069 
1070 		if (!sync_test_and_clear_bit(relid, recv_int_page))
1071 			continue;
1072 
1073 		/* Special case - vmbus channel protocol msg */
1074 		if (relid == 0)
1075 			continue;
1076 
1077 		rcu_read_lock();
1078 
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)
1082 				continue;
1083 
1084 			if (channel->rescind)
1085 				continue;
1086 
1087 			trace_vmbus_chan_sched(channel);
1088 
1089 			++channel->interrupts;
1090 
1091 			switch (channel->callback_mode) {
1092 			case HV_CALL_ISR:
1093 				vmbus_channel_isr(channel);
1094 				break;
1095 
1096 			case HV_CALL_BATCHED:
1097 				hv_begin_read(&channel->inbound);
1098 				/* fallthrough */
1099 			case HV_CALL_DIRECT:
1100 				tasklet_schedule(&channel->callback_event);
1101 			}
1102 		}
1103 
1104 		rcu_read_unlock();
1105 	}
1106 }
1107 
1108 static void vmbus_isr(void)
1109 {
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;
1116 
1117 	if (unlikely(page_addr == NULL))
1118 		return;
1119 
1120 	event = (union hv_synic_event_flags *)page_addr +
1121 					 VMBUS_MESSAGE_SINT;
1122 	/*
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.
1126 	 */
1127 
1128 	if ((vmbus_proto_version == VERSION_WS2008) ||
1129 		(vmbus_proto_version == VERSION_WIN7)) {
1130 
1131 		/* Since we are a child, we only need to check bit 0 */
1132 		if (sync_test_and_clear_bit(0, event->flags))
1133 			handled = true;
1134 	} else {
1135 		/*
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
1139 		 * whose id is n.
1140 		 */
1141 		handled = true;
1142 	}
1143 
1144 	if (handled)
1145 		vmbus_chan_sched(hv_cpu);
1146 
1147 	page_addr = hv_cpu->synic_message_page;
1148 	msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
1149 
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) {
1153 			hv_stimer0_isr();
1154 			vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED);
1155 		} else
1156 			tasklet_schedule(&hv_cpu->msg_dpc);
1157 	}
1158 
1159 	add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0);
1160 }
1161 
1162 /*
1163  * Boolean to control whether to report panic messages over Hyper-V.
1164  *
1165  * It can be set via /proc/sys/kernel/hyperv/record_panic_msg
1166  */
1167 static int sysctl_record_panic_msg = 1;
1168 
1169 /*
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.
1172  */
1173 static void hv_kmsg_dump(struct kmsg_dumper *dumper,
1174 			 enum kmsg_dump_reason reason)
1175 {
1176 	size_t bytes_written;
1177 	phys_addr_t panic_pa;
1178 
1179 	/* We are only interested in panics. */
1180 	if ((reason != KMSG_DUMP_PANIC) || (!sysctl_record_panic_msg))
1181 		return;
1182 
1183 	panic_pa = virt_to_phys(hv_panic_page);
1184 
1185 	/*
1186 	 * Write dump contents to the page. No need to synchronize; panic should
1187 	 * be single-threaded.
1188 	 */
1189 	kmsg_dump_get_buffer(dumper, true, hv_panic_page, PAGE_SIZE,
1190 			     &bytes_written);
1191 	if (bytes_written)
1192 		hyperv_report_panic_msg(panic_pa, bytes_written);
1193 }
1194 
1195 static struct kmsg_dumper hv_kmsg_dumper = {
1196 	.dump = hv_kmsg_dump,
1197 };
1198 
1199 static struct ctl_table_header *hv_ctl_table_hdr;
1200 static int zero;
1201 static int one = 1;
1202 
1203 /*
1204  * sysctl option to allow the user to control whether kmsg data should be
1205  * reported to Hyper-V on panic.
1206  */
1207 static struct ctl_table hv_ctl_table[] = {
1208 	{
1209 		.procname       = "hyperv_record_panic_msg",
1210 		.data           = &sysctl_record_panic_msg,
1211 		.maxlen         = sizeof(int),
1212 		.mode           = 0644,
1213 		.proc_handler   = proc_dointvec_minmax,
1214 		.extra1		= &zero,
1215 		.extra2		= &one
1216 	},
1217 	{}
1218 };
1219 
1220 static struct ctl_table hv_root_table[] = {
1221 	{
1222 		.procname	= "kernel",
1223 		.mode		= 0555,
1224 		.child		= hv_ctl_table
1225 	},
1226 	{}
1227 };
1228 
1229 /*
1230  * vmbus_bus_init -Main vmbus driver initialization routine.
1231  *
1232  * Here, we
1233  *	- initialize the vmbus driver context
1234  *	- invoke the vmbus hv main init routine
1235  *	- retrieve the channel offers
1236  */
1237 static int vmbus_bus_init(void)
1238 {
1239 	int ret;
1240 
1241 	/* Hypervisor initialization...setup hypercall page..etc */
1242 	ret = hv_init();
1243 	if (ret != 0) {
1244 		pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
1245 		return ret;
1246 	}
1247 
1248 	ret = bus_register(&hv_bus);
1249 	if (ret)
1250 		return ret;
1251 
1252 	hv_setup_vmbus_irq(vmbus_isr);
1253 
1254 	ret = hv_synic_alloc();
1255 	if (ret)
1256 		goto err_alloc;
1257 
1258 	ret = hv_stimer_alloc(VMBUS_MESSAGE_SINT);
1259 	if (ret < 0)
1260 		goto err_alloc;
1261 
1262 	/*
1263 	 * Initialize the per-cpu interrupt state and stimer state.
1264 	 * Then connect to the host.
1265 	 */
1266 	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hyperv/vmbus:online",
1267 				hv_synic_init, hv_synic_cleanup);
1268 	if (ret < 0)
1269 		goto err_cpuhp;
1270 	hyperv_cpuhp_online = ret;
1271 
1272 	ret = vmbus_connect();
1273 	if (ret)
1274 		goto err_connect;
1275 
1276 	/*
1277 	 * Only register if the crash MSRs are available
1278 	 */
1279 	if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
1280 		u64 hyperv_crash_ctl;
1281 		/*
1282 		 * Sysctl registration is not fatal, since by default
1283 		 * reporting is enabled.
1284 		 */
1285 		hv_ctl_table_hdr = register_sysctl_table(hv_root_table);
1286 		if (!hv_ctl_table_hdr)
1287 			pr_err("Hyper-V: sysctl table register error");
1288 
1289 		/*
1290 		 * Register for panic kmsg callback only if the right
1291 		 * capability is supported by the hypervisor.
1292 		 */
1293 		hv_get_crash_ctl(hyperv_crash_ctl);
1294 		if (hyperv_crash_ctl & HV_CRASH_CTL_CRASH_NOTIFY_MSG) {
1295 			hv_panic_page = (void *)get_zeroed_page(GFP_KERNEL);
1296 			if (hv_panic_page) {
1297 				ret = kmsg_dump_register(&hv_kmsg_dumper);
1298 				if (ret)
1299 					pr_err("Hyper-V: kmsg dump register "
1300 						"error 0x%x\n", ret);
1301 			} else
1302 				pr_err("Hyper-V: panic message page memory "
1303 					"allocation failed");
1304 		}
1305 
1306 		register_die_notifier(&hyperv_die_block);
1307 		atomic_notifier_chain_register(&panic_notifier_list,
1308 					       &hyperv_panic_block);
1309 	}
1310 
1311 	vmbus_request_offers();
1312 
1313 	return 0;
1314 
1315 err_connect:
1316 	cpuhp_remove_state(hyperv_cpuhp_online);
1317 err_cpuhp:
1318 	hv_stimer_free();
1319 err_alloc:
1320 	hv_synic_free();
1321 	hv_remove_vmbus_irq();
1322 
1323 	bus_unregister(&hv_bus);
1324 	free_page((unsigned long)hv_panic_page);
1325 	unregister_sysctl_table(hv_ctl_table_hdr);
1326 	hv_ctl_table_hdr = NULL;
1327 	return ret;
1328 }
1329 
1330 /**
1331  * __vmbus_child_driver_register() - Register a vmbus's driver
1332  * @hv_driver: Pointer to driver structure you want to register
1333  * @owner: owner module of the drv
1334  * @mod_name: module name string
1335  *
1336  * Registers the given driver with Linux through the 'driver_register()' call
1337  * and sets up the hyper-v vmbus handling for this driver.
1338  * It will return the state of the 'driver_register()' call.
1339  *
1340  */
1341 int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
1342 {
1343 	int ret;
1344 
1345 	pr_info("registering driver %s\n", hv_driver->name);
1346 
1347 	ret = vmbus_exists();
1348 	if (ret < 0)
1349 		return ret;
1350 
1351 	hv_driver->driver.name = hv_driver->name;
1352 	hv_driver->driver.owner = owner;
1353 	hv_driver->driver.mod_name = mod_name;
1354 	hv_driver->driver.bus = &hv_bus;
1355 
1356 	spin_lock_init(&hv_driver->dynids.lock);
1357 	INIT_LIST_HEAD(&hv_driver->dynids.list);
1358 
1359 	ret = driver_register(&hv_driver->driver);
1360 
1361 	return ret;
1362 }
1363 EXPORT_SYMBOL_GPL(__vmbus_driver_register);
1364 
1365 /**
1366  * vmbus_driver_unregister() - Unregister a vmbus's driver
1367  * @hv_driver: Pointer to driver structure you want to
1368  *             un-register
1369  *
1370  * Un-register the given driver that was previous registered with a call to
1371  * vmbus_driver_register()
1372  */
1373 void vmbus_driver_unregister(struct hv_driver *hv_driver)
1374 {
1375 	pr_info("unregistering driver %s\n", hv_driver->name);
1376 
1377 	if (!vmbus_exists()) {
1378 		driver_unregister(&hv_driver->driver);
1379 		vmbus_free_dynids(hv_driver);
1380 	}
1381 }
1382 EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
1383 
1384 
1385 /*
1386  * Called when last reference to channel is gone.
1387  */
1388 static void vmbus_chan_release(struct kobject *kobj)
1389 {
1390 	struct vmbus_channel *channel
1391 		= container_of(kobj, struct vmbus_channel, kobj);
1392 
1393 	kfree_rcu(channel, rcu);
1394 }
1395 
1396 struct vmbus_chan_attribute {
1397 	struct attribute attr;
1398 	ssize_t (*show)(struct vmbus_channel *chan, char *buf);
1399 	ssize_t (*store)(struct vmbus_channel *chan,
1400 			 const char *buf, size_t count);
1401 };
1402 #define VMBUS_CHAN_ATTR(_name, _mode, _show, _store) \
1403 	struct vmbus_chan_attribute chan_attr_##_name \
1404 		= __ATTR(_name, _mode, _show, _store)
1405 #define VMBUS_CHAN_ATTR_RW(_name) \
1406 	struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RW(_name)
1407 #define VMBUS_CHAN_ATTR_RO(_name) \
1408 	struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RO(_name)
1409 #define VMBUS_CHAN_ATTR_WO(_name) \
1410 	struct vmbus_chan_attribute chan_attr_##_name = __ATTR_WO(_name)
1411 
1412 static ssize_t vmbus_chan_attr_show(struct kobject *kobj,
1413 				    struct attribute *attr, char *buf)
1414 {
1415 	const struct vmbus_chan_attribute *attribute
1416 		= container_of(attr, struct vmbus_chan_attribute, attr);
1417 	struct vmbus_channel *chan
1418 		= container_of(kobj, struct vmbus_channel, kobj);
1419 
1420 	if (!attribute->show)
1421 		return -EIO;
1422 
1423 	return attribute->show(chan, buf);
1424 }
1425 
1426 static const struct sysfs_ops vmbus_chan_sysfs_ops = {
1427 	.show = vmbus_chan_attr_show,
1428 };
1429 
1430 static ssize_t out_mask_show(struct vmbus_channel *channel, char *buf)
1431 {
1432 	struct hv_ring_buffer_info *rbi = &channel->outbound;
1433 	ssize_t ret;
1434 
1435 	mutex_lock(&rbi->ring_buffer_mutex);
1436 	if (!rbi->ring_buffer) {
1437 		mutex_unlock(&rbi->ring_buffer_mutex);
1438 		return -EINVAL;
1439 	}
1440 
1441 	ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1442 	mutex_unlock(&rbi->ring_buffer_mutex);
1443 	return ret;
1444 }
1445 static VMBUS_CHAN_ATTR_RO(out_mask);
1446 
1447 static ssize_t in_mask_show(struct vmbus_channel *channel, char *buf)
1448 {
1449 	struct hv_ring_buffer_info *rbi = &channel->inbound;
1450 	ssize_t ret;
1451 
1452 	mutex_lock(&rbi->ring_buffer_mutex);
1453 	if (!rbi->ring_buffer) {
1454 		mutex_unlock(&rbi->ring_buffer_mutex);
1455 		return -EINVAL;
1456 	}
1457 
1458 	ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1459 	mutex_unlock(&rbi->ring_buffer_mutex);
1460 	return ret;
1461 }
1462 static VMBUS_CHAN_ATTR_RO(in_mask);
1463 
1464 static ssize_t read_avail_show(struct vmbus_channel *channel, char *buf)
1465 {
1466 	struct hv_ring_buffer_info *rbi = &channel->inbound;
1467 	ssize_t ret;
1468 
1469 	mutex_lock(&rbi->ring_buffer_mutex);
1470 	if (!rbi->ring_buffer) {
1471 		mutex_unlock(&rbi->ring_buffer_mutex);
1472 		return -EINVAL;
1473 	}
1474 
1475 	ret = sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi));
1476 	mutex_unlock(&rbi->ring_buffer_mutex);
1477 	return ret;
1478 }
1479 static VMBUS_CHAN_ATTR_RO(read_avail);
1480 
1481 static ssize_t write_avail_show(struct vmbus_channel *channel, char *buf)
1482 {
1483 	struct hv_ring_buffer_info *rbi = &channel->outbound;
1484 	ssize_t ret;
1485 
1486 	mutex_lock(&rbi->ring_buffer_mutex);
1487 	if (!rbi->ring_buffer) {
1488 		mutex_unlock(&rbi->ring_buffer_mutex);
1489 		return -EINVAL;
1490 	}
1491 
1492 	ret = sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi));
1493 	mutex_unlock(&rbi->ring_buffer_mutex);
1494 	return ret;
1495 }
1496 static VMBUS_CHAN_ATTR_RO(write_avail);
1497 
1498 static ssize_t show_target_cpu(struct vmbus_channel *channel, char *buf)
1499 {
1500 	return sprintf(buf, "%u\n", channel->target_cpu);
1501 }
1502 static VMBUS_CHAN_ATTR(cpu, S_IRUGO, show_target_cpu, NULL);
1503 
1504 static ssize_t channel_pending_show(struct vmbus_channel *channel,
1505 				    char *buf)
1506 {
1507 	return sprintf(buf, "%d\n",
1508 		       channel_pending(channel,
1509 				       vmbus_connection.monitor_pages[1]));
1510 }
1511 static VMBUS_CHAN_ATTR(pending, S_IRUGO, channel_pending_show, NULL);
1512 
1513 static ssize_t channel_latency_show(struct vmbus_channel *channel,
1514 				    char *buf)
1515 {
1516 	return sprintf(buf, "%d\n",
1517 		       channel_latency(channel,
1518 				       vmbus_connection.monitor_pages[1]));
1519 }
1520 static VMBUS_CHAN_ATTR(latency, S_IRUGO, channel_latency_show, NULL);
1521 
1522 static ssize_t channel_interrupts_show(struct vmbus_channel *channel, char *buf)
1523 {
1524 	return sprintf(buf, "%llu\n", channel->interrupts);
1525 }
1526 static VMBUS_CHAN_ATTR(interrupts, S_IRUGO, channel_interrupts_show, NULL);
1527 
1528 static ssize_t channel_events_show(struct vmbus_channel *channel, char *buf)
1529 {
1530 	return sprintf(buf, "%llu\n", channel->sig_events);
1531 }
1532 static VMBUS_CHAN_ATTR(events, S_IRUGO, channel_events_show, NULL);
1533 
1534 static ssize_t channel_intr_in_full_show(struct vmbus_channel *channel,
1535 					 char *buf)
1536 {
1537 	return sprintf(buf, "%llu\n",
1538 		       (unsigned long long)channel->intr_in_full);
1539 }
1540 static VMBUS_CHAN_ATTR(intr_in_full, 0444, channel_intr_in_full_show, NULL);
1541 
1542 static ssize_t channel_intr_out_empty_show(struct vmbus_channel *channel,
1543 					   char *buf)
1544 {
1545 	return sprintf(buf, "%llu\n",
1546 		       (unsigned long long)channel->intr_out_empty);
1547 }
1548 static VMBUS_CHAN_ATTR(intr_out_empty, 0444, channel_intr_out_empty_show, NULL);
1549 
1550 static ssize_t channel_out_full_first_show(struct vmbus_channel *channel,
1551 					   char *buf)
1552 {
1553 	return sprintf(buf, "%llu\n",
1554 		       (unsigned long long)channel->out_full_first);
1555 }
1556 static VMBUS_CHAN_ATTR(out_full_first, 0444, channel_out_full_first_show, NULL);
1557 
1558 static ssize_t channel_out_full_total_show(struct vmbus_channel *channel,
1559 					   char *buf)
1560 {
1561 	return sprintf(buf, "%llu\n",
1562 		       (unsigned long long)channel->out_full_total);
1563 }
1564 static VMBUS_CHAN_ATTR(out_full_total, 0444, channel_out_full_total_show, NULL);
1565 
1566 static ssize_t subchannel_monitor_id_show(struct vmbus_channel *channel,
1567 					  char *buf)
1568 {
1569 	return sprintf(buf, "%u\n", channel->offermsg.monitorid);
1570 }
1571 static VMBUS_CHAN_ATTR(monitor_id, S_IRUGO, subchannel_monitor_id_show, NULL);
1572 
1573 static ssize_t subchannel_id_show(struct vmbus_channel *channel,
1574 				  char *buf)
1575 {
1576 	return sprintf(buf, "%u\n",
1577 		       channel->offermsg.offer.sub_channel_index);
1578 }
1579 static VMBUS_CHAN_ATTR_RO(subchannel_id);
1580 
1581 static struct attribute *vmbus_chan_attrs[] = {
1582 	&chan_attr_out_mask.attr,
1583 	&chan_attr_in_mask.attr,
1584 	&chan_attr_read_avail.attr,
1585 	&chan_attr_write_avail.attr,
1586 	&chan_attr_cpu.attr,
1587 	&chan_attr_pending.attr,
1588 	&chan_attr_latency.attr,
1589 	&chan_attr_interrupts.attr,
1590 	&chan_attr_events.attr,
1591 	&chan_attr_intr_in_full.attr,
1592 	&chan_attr_intr_out_empty.attr,
1593 	&chan_attr_out_full_first.attr,
1594 	&chan_attr_out_full_total.attr,
1595 	&chan_attr_monitor_id.attr,
1596 	&chan_attr_subchannel_id.attr,
1597 	NULL
1598 };
1599 
1600 /*
1601  * Channel-level attribute_group callback function. Returns the permission for
1602  * each attribute, and returns 0 if an attribute is not visible.
1603  */
1604 static umode_t vmbus_chan_attr_is_visible(struct kobject *kobj,
1605 					  struct attribute *attr, int idx)
1606 {
1607 	const struct vmbus_channel *channel =
1608 		container_of(kobj, struct vmbus_channel, kobj);
1609 
1610 	/* Hide the monitor attributes if the monitor mechanism is not used. */
1611 	if (!channel->offermsg.monitor_allocated &&
1612 	    (attr == &chan_attr_pending.attr ||
1613 	     attr == &chan_attr_latency.attr ||
1614 	     attr == &chan_attr_monitor_id.attr))
1615 		return 0;
1616 
1617 	return attr->mode;
1618 }
1619 
1620 static struct attribute_group vmbus_chan_group = {
1621 	.attrs = vmbus_chan_attrs,
1622 	.is_visible = vmbus_chan_attr_is_visible
1623 };
1624 
1625 static struct kobj_type vmbus_chan_ktype = {
1626 	.sysfs_ops = &vmbus_chan_sysfs_ops,
1627 	.release = vmbus_chan_release,
1628 };
1629 
1630 /*
1631  * vmbus_add_channel_kobj - setup a sub-directory under device/channels
1632  */
1633 int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
1634 {
1635 	const struct device *device = &dev->device;
1636 	struct kobject *kobj = &channel->kobj;
1637 	u32 relid = channel->offermsg.child_relid;
1638 	int ret;
1639 
1640 	kobj->kset = dev->channels_kset;
1641 	ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
1642 				   "%u", relid);
1643 	if (ret)
1644 		return ret;
1645 
1646 	ret = sysfs_create_group(kobj, &vmbus_chan_group);
1647 
1648 	if (ret) {
1649 		/*
1650 		 * The calling functions' error handling paths will cleanup the
1651 		 * empty channel directory.
1652 		 */
1653 		dev_err(device, "Unable to set up channel sysfs files\n");
1654 		return ret;
1655 	}
1656 
1657 	kobject_uevent(kobj, KOBJ_ADD);
1658 
1659 	return 0;
1660 }
1661 
1662 /*
1663  * vmbus_remove_channel_attr_group - remove the channel's attribute group
1664  */
1665 void vmbus_remove_channel_attr_group(struct vmbus_channel *channel)
1666 {
1667 	sysfs_remove_group(&channel->kobj, &vmbus_chan_group);
1668 }
1669 
1670 /*
1671  * vmbus_device_create - Creates and registers a new child device
1672  * on the vmbus.
1673  */
1674 struct hv_device *vmbus_device_create(const guid_t *type,
1675 				      const guid_t *instance,
1676 				      struct vmbus_channel *channel)
1677 {
1678 	struct hv_device *child_device_obj;
1679 
1680 	child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
1681 	if (!child_device_obj) {
1682 		pr_err("Unable to allocate device object for child device\n");
1683 		return NULL;
1684 	}
1685 
1686 	child_device_obj->channel = channel;
1687 	guid_copy(&child_device_obj->dev_type, type);
1688 	guid_copy(&child_device_obj->dev_instance, instance);
1689 	child_device_obj->vendor_id = 0x1414; /* MSFT vendor ID */
1690 
1691 	return child_device_obj;
1692 }
1693 
1694 /*
1695  * vmbus_device_register - Register the child device
1696  */
1697 int vmbus_device_register(struct hv_device *child_device_obj)
1698 {
1699 	struct kobject *kobj = &child_device_obj->device.kobj;
1700 	int ret;
1701 
1702 	dev_set_name(&child_device_obj->device, "%pUl",
1703 		     child_device_obj->channel->offermsg.offer.if_instance.b);
1704 
1705 	child_device_obj->device.bus = &hv_bus;
1706 	child_device_obj->device.parent = &hv_acpi_dev->dev;
1707 	child_device_obj->device.release = vmbus_device_release;
1708 
1709 	/*
1710 	 * Register with the LDM. This will kick off the driver/device
1711 	 * binding...which will eventually call vmbus_match() and vmbus_probe()
1712 	 */
1713 	ret = device_register(&child_device_obj->device);
1714 	if (ret) {
1715 		pr_err("Unable to register child device\n");
1716 		return ret;
1717 	}
1718 
1719 	child_device_obj->channels_kset = kset_create_and_add("channels",
1720 							      NULL, kobj);
1721 	if (!child_device_obj->channels_kset) {
1722 		ret = -ENOMEM;
1723 		goto err_dev_unregister;
1724 	}
1725 
1726 	ret = vmbus_add_channel_kobj(child_device_obj,
1727 				     child_device_obj->channel);
1728 	if (ret) {
1729 		pr_err("Unable to register primary channeln");
1730 		goto err_kset_unregister;
1731 	}
1732 
1733 	return 0;
1734 
1735 err_kset_unregister:
1736 	kset_unregister(child_device_obj->channels_kset);
1737 
1738 err_dev_unregister:
1739 	device_unregister(&child_device_obj->device);
1740 	return ret;
1741 }
1742 
1743 /*
1744  * vmbus_device_unregister - Remove the specified child device
1745  * from the vmbus.
1746  */
1747 void vmbus_device_unregister(struct hv_device *device_obj)
1748 {
1749 	pr_debug("child device %s unregistered\n",
1750 		dev_name(&device_obj->device));
1751 
1752 	kset_unregister(device_obj->channels_kset);
1753 
1754 	/*
1755 	 * Kick off the process of unregistering the device.
1756 	 * This will call vmbus_remove() and eventually vmbus_device_release()
1757 	 */
1758 	device_unregister(&device_obj->device);
1759 }
1760 
1761 
1762 /*
1763  * VMBUS is an acpi enumerated device. Get the information we
1764  * need from DSDT.
1765  */
1766 #define VTPM_BASE_ADDRESS 0xfed40000
1767 static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
1768 {
1769 	resource_size_t start = 0;
1770 	resource_size_t end = 0;
1771 	struct resource *new_res;
1772 	struct resource **old_res = &hyperv_mmio;
1773 	struct resource **prev_res = NULL;
1774 
1775 	switch (res->type) {
1776 
1777 	/*
1778 	 * "Address" descriptors are for bus windows. Ignore
1779 	 * "memory" descriptors, which are for registers on
1780 	 * devices.
1781 	 */
1782 	case ACPI_RESOURCE_TYPE_ADDRESS32:
1783 		start = res->data.address32.address.minimum;
1784 		end = res->data.address32.address.maximum;
1785 		break;
1786 
1787 	case ACPI_RESOURCE_TYPE_ADDRESS64:
1788 		start = res->data.address64.address.minimum;
1789 		end = res->data.address64.address.maximum;
1790 		break;
1791 
1792 	default:
1793 		/* Unused resource type */
1794 		return AE_OK;
1795 
1796 	}
1797 	/*
1798 	 * Ignore ranges that are below 1MB, as they're not
1799 	 * necessary or useful here.
1800 	 */
1801 	if (end < 0x100000)
1802 		return AE_OK;
1803 
1804 	new_res = kzalloc(sizeof(*new_res), GFP_ATOMIC);
1805 	if (!new_res)
1806 		return AE_NO_MEMORY;
1807 
1808 	/* If this range overlaps the virtual TPM, truncate it. */
1809 	if (end > VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS)
1810 		end = VTPM_BASE_ADDRESS;
1811 
1812 	new_res->name = "hyperv mmio";
1813 	new_res->flags = IORESOURCE_MEM;
1814 	new_res->start = start;
1815 	new_res->end = end;
1816 
1817 	/*
1818 	 * If two ranges are adjacent, merge them.
1819 	 */
1820 	do {
1821 		if (!*old_res) {
1822 			*old_res = new_res;
1823 			break;
1824 		}
1825 
1826 		if (((*old_res)->end + 1) == new_res->start) {
1827 			(*old_res)->end = new_res->end;
1828 			kfree(new_res);
1829 			break;
1830 		}
1831 
1832 		if ((*old_res)->start == new_res->end + 1) {
1833 			(*old_res)->start = new_res->start;
1834 			kfree(new_res);
1835 			break;
1836 		}
1837 
1838 		if ((*old_res)->start > new_res->end) {
1839 			new_res->sibling = *old_res;
1840 			if (prev_res)
1841 				(*prev_res)->sibling = new_res;
1842 			*old_res = new_res;
1843 			break;
1844 		}
1845 
1846 		prev_res = old_res;
1847 		old_res = &(*old_res)->sibling;
1848 
1849 	} while (1);
1850 
1851 	return AE_OK;
1852 }
1853 
1854 static int vmbus_acpi_remove(struct acpi_device *device)
1855 {
1856 	struct resource *cur_res;
1857 	struct resource *next_res;
1858 
1859 	if (hyperv_mmio) {
1860 		if (fb_mmio) {
1861 			__release_region(hyperv_mmio, fb_mmio->start,
1862 					 resource_size(fb_mmio));
1863 			fb_mmio = NULL;
1864 		}
1865 
1866 		for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) {
1867 			next_res = cur_res->sibling;
1868 			kfree(cur_res);
1869 		}
1870 	}
1871 
1872 	return 0;
1873 }
1874 
1875 static void vmbus_reserve_fb(void)
1876 {
1877 	int size;
1878 	/*
1879 	 * Make a claim for the frame buffer in the resource tree under the
1880 	 * first node, which will be the one below 4GB.  The length seems to
1881 	 * be underreported, particularly in a Generation 1 VM.  So start out
1882 	 * reserving a larger area and make it smaller until it succeeds.
1883 	 */
1884 
1885 	if (screen_info.lfb_base) {
1886 		if (efi_enabled(EFI_BOOT))
1887 			size = max_t(__u32, screen_info.lfb_size, 0x800000);
1888 		else
1889 			size = max_t(__u32, screen_info.lfb_size, 0x4000000);
1890 
1891 		for (; !fb_mmio && (size >= 0x100000); size >>= 1) {
1892 			fb_mmio = __request_region(hyperv_mmio,
1893 						   screen_info.lfb_base, size,
1894 						   fb_mmio_name, 0);
1895 		}
1896 	}
1897 }
1898 
1899 /**
1900  * vmbus_allocate_mmio() - Pick a memory-mapped I/O range.
1901  * @new:		If successful, supplied a pointer to the
1902  *			allocated MMIO space.
1903  * @device_obj:		Identifies the caller
1904  * @min:		Minimum guest physical address of the
1905  *			allocation
1906  * @max:		Maximum guest physical address
1907  * @size:		Size of the range to be allocated
1908  * @align:		Alignment of the range to be allocated
1909  * @fb_overlap_ok:	Whether this allocation can be allowed
1910  *			to overlap the video frame buffer.
1911  *
1912  * This function walks the resources granted to VMBus by the
1913  * _CRS object in the ACPI namespace underneath the parent
1914  * "bridge" whether that's a root PCI bus in the Generation 1
1915  * case or a Module Device in the Generation 2 case.  It then
1916  * attempts to allocate from the global MMIO pool in a way that
1917  * matches the constraints supplied in these parameters and by
1918  * that _CRS.
1919  *
1920  * Return: 0 on success, -errno on failure
1921  */
1922 int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
1923 			resource_size_t min, resource_size_t max,
1924 			resource_size_t size, resource_size_t align,
1925 			bool fb_overlap_ok)
1926 {
1927 	struct resource *iter, *shadow;
1928 	resource_size_t range_min, range_max, start;
1929 	const char *dev_n = dev_name(&device_obj->device);
1930 	int retval;
1931 
1932 	retval = -ENXIO;
1933 	down(&hyperv_mmio_lock);
1934 
1935 	/*
1936 	 * If overlaps with frame buffers are allowed, then first attempt to
1937 	 * make the allocation from within the reserved region.  Because it
1938 	 * is already reserved, no shadow allocation is necessary.
1939 	 */
1940 	if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) &&
1941 	    !(max < fb_mmio->start)) {
1942 
1943 		range_min = fb_mmio->start;
1944 		range_max = fb_mmio->end;
1945 		start = (range_min + align - 1) & ~(align - 1);
1946 		for (; start + size - 1 <= range_max; start += align) {
1947 			*new = request_mem_region_exclusive(start, size, dev_n);
1948 			if (*new) {
1949 				retval = 0;
1950 				goto exit;
1951 			}
1952 		}
1953 	}
1954 
1955 	for (iter = hyperv_mmio; iter; iter = iter->sibling) {
1956 		if ((iter->start >= max) || (iter->end <= min))
1957 			continue;
1958 
1959 		range_min = iter->start;
1960 		range_max = iter->end;
1961 		start = (range_min + align - 1) & ~(align - 1);
1962 		for (; start + size - 1 <= range_max; start += align) {
1963 			shadow = __request_region(iter, start, size, NULL,
1964 						  IORESOURCE_BUSY);
1965 			if (!shadow)
1966 				continue;
1967 
1968 			*new = request_mem_region_exclusive(start, size, dev_n);
1969 			if (*new) {
1970 				shadow->name = (char *)*new;
1971 				retval = 0;
1972 				goto exit;
1973 			}
1974 
1975 			__release_region(iter, start, size);
1976 		}
1977 	}
1978 
1979 exit:
1980 	up(&hyperv_mmio_lock);
1981 	return retval;
1982 }
1983 EXPORT_SYMBOL_GPL(vmbus_allocate_mmio);
1984 
1985 /**
1986  * vmbus_free_mmio() - Free a memory-mapped I/O range.
1987  * @start:		Base address of region to release.
1988  * @size:		Size of the range to be allocated
1989  *
1990  * This function releases anything requested by
1991  * vmbus_mmio_allocate().
1992  */
1993 void vmbus_free_mmio(resource_size_t start, resource_size_t size)
1994 {
1995 	struct resource *iter;
1996 
1997 	down(&hyperv_mmio_lock);
1998 	for (iter = hyperv_mmio; iter; iter = iter->sibling) {
1999 		if ((iter->start >= start + size) || (iter->end <= start))
2000 			continue;
2001 
2002 		__release_region(iter, start, size);
2003 	}
2004 	release_mem_region(start, size);
2005 	up(&hyperv_mmio_lock);
2006 
2007 }
2008 EXPORT_SYMBOL_GPL(vmbus_free_mmio);
2009 
2010 static int vmbus_acpi_add(struct acpi_device *device)
2011 {
2012 	acpi_status result;
2013 	int ret_val = -ENODEV;
2014 	struct acpi_device *ancestor;
2015 
2016 	hv_acpi_dev = device;
2017 
2018 	result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
2019 					vmbus_walk_resources, NULL);
2020 
2021 	if (ACPI_FAILURE(result))
2022 		goto acpi_walk_err;
2023 	/*
2024 	 * Some ancestor of the vmbus acpi device (Gen1 or Gen2
2025 	 * firmware) is the VMOD that has the mmio ranges. Get that.
2026 	 */
2027 	for (ancestor = device->parent; ancestor; ancestor = ancestor->parent) {
2028 		result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS,
2029 					     vmbus_walk_resources, NULL);
2030 
2031 		if (ACPI_FAILURE(result))
2032 			continue;
2033 		if (hyperv_mmio) {
2034 			vmbus_reserve_fb();
2035 			break;
2036 		}
2037 	}
2038 	ret_val = 0;
2039 
2040 acpi_walk_err:
2041 	complete(&probe_event);
2042 	if (ret_val)
2043 		vmbus_acpi_remove(device);
2044 	return ret_val;
2045 }
2046 
2047 static const struct acpi_device_id vmbus_acpi_device_ids[] = {
2048 	{"VMBUS", 0},
2049 	{"VMBus", 0},
2050 	{"", 0},
2051 };
2052 MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
2053 
2054 static struct acpi_driver vmbus_acpi_driver = {
2055 	.name = "vmbus",
2056 	.ids = vmbus_acpi_device_ids,
2057 	.ops = {
2058 		.add = vmbus_acpi_add,
2059 		.remove = vmbus_acpi_remove,
2060 	},
2061 };
2062 
2063 static void hv_kexec_handler(void)
2064 {
2065 	hv_stimer_global_cleanup();
2066 	vmbus_initiate_unload(false);
2067 	vmbus_connection.conn_state = DISCONNECTED;
2068 	/* Make sure conn_state is set as hv_synic_cleanup checks for it */
2069 	mb();
2070 	cpuhp_remove_state(hyperv_cpuhp_online);
2071 	hyperv_cleanup();
2072 };
2073 
2074 static void hv_crash_handler(struct pt_regs *regs)
2075 {
2076 	int cpu;
2077 
2078 	vmbus_initiate_unload(true);
2079 	/*
2080 	 * In crash handler we can't schedule synic cleanup for all CPUs,
2081 	 * doing the cleanup for current CPU only. This should be sufficient
2082 	 * for kdump.
2083 	 */
2084 	vmbus_connection.conn_state = DISCONNECTED;
2085 	cpu = smp_processor_id();
2086 	hv_stimer_cleanup(cpu);
2087 	hv_synic_cleanup(cpu);
2088 	hyperv_cleanup();
2089 };
2090 
2091 static int __init hv_acpi_init(void)
2092 {
2093 	int ret, t;
2094 
2095 	if (!hv_is_hyperv_initialized())
2096 		return -ENODEV;
2097 
2098 	init_completion(&probe_event);
2099 
2100 	/*
2101 	 * Get ACPI resources first.
2102 	 */
2103 	ret = acpi_bus_register_driver(&vmbus_acpi_driver);
2104 
2105 	if (ret)
2106 		return ret;
2107 
2108 	t = wait_for_completion_timeout(&probe_event, 5*HZ);
2109 	if (t == 0) {
2110 		ret = -ETIMEDOUT;
2111 		goto cleanup;
2112 	}
2113 
2114 	ret = vmbus_bus_init();
2115 	if (ret)
2116 		goto cleanup;
2117 
2118 	hv_setup_kexec_handler(hv_kexec_handler);
2119 	hv_setup_crash_handler(hv_crash_handler);
2120 
2121 	return 0;
2122 
2123 cleanup:
2124 	acpi_bus_unregister_driver(&vmbus_acpi_driver);
2125 	hv_acpi_dev = NULL;
2126 	return ret;
2127 }
2128 
2129 static void __exit vmbus_exit(void)
2130 {
2131 	int cpu;
2132 
2133 	hv_remove_kexec_handler();
2134 	hv_remove_crash_handler();
2135 	vmbus_connection.conn_state = DISCONNECTED;
2136 	hv_stimer_global_cleanup();
2137 	vmbus_disconnect();
2138 	hv_remove_vmbus_irq();
2139 	for_each_online_cpu(cpu) {
2140 		struct hv_per_cpu_context *hv_cpu
2141 			= per_cpu_ptr(hv_context.cpu_context, cpu);
2142 
2143 		tasklet_kill(&hv_cpu->msg_dpc);
2144 	}
2145 	vmbus_free_channels();
2146 
2147 	if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
2148 		kmsg_dump_unregister(&hv_kmsg_dumper);
2149 		unregister_die_notifier(&hyperv_die_block);
2150 		atomic_notifier_chain_unregister(&panic_notifier_list,
2151 						 &hyperv_panic_block);
2152 	}
2153 
2154 	free_page((unsigned long)hv_panic_page);
2155 	unregister_sysctl_table(hv_ctl_table_hdr);
2156 	hv_ctl_table_hdr = NULL;
2157 	bus_unregister(&hv_bus);
2158 
2159 	cpuhp_remove_state(hyperv_cpuhp_online);
2160 	hv_synic_free();
2161 	acpi_bus_unregister_driver(&vmbus_acpi_driver);
2162 }
2163 
2164 
2165 MODULE_LICENSE("GPL");
2166 MODULE_DESCRIPTION("Microsoft Hyper-V VMBus Driver");
2167 
2168 subsys_initcall(hv_acpi_init);
2169 module_exit(vmbus_exit);
2170