xref: /openbmc/linux/drivers/hv/vmbus_drv.c (revision 9726bfcd)
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 
1201 /*
1202  * sysctl option to allow the user to control whether kmsg data should be
1203  * reported to Hyper-V on panic.
1204  */
1205 static struct ctl_table hv_ctl_table[] = {
1206 	{
1207 		.procname       = "hyperv_record_panic_msg",
1208 		.data           = &sysctl_record_panic_msg,
1209 		.maxlen         = sizeof(int),
1210 		.mode           = 0644,
1211 		.proc_handler   = proc_dointvec_minmax,
1212 		.extra1		= SYSCTL_ZERO,
1213 		.extra2		= SYSCTL_ONE
1214 	},
1215 	{}
1216 };
1217 
1218 static struct ctl_table hv_root_table[] = {
1219 	{
1220 		.procname	= "kernel",
1221 		.mode		= 0555,
1222 		.child		= hv_ctl_table
1223 	},
1224 	{}
1225 };
1226 
1227 /*
1228  * vmbus_bus_init -Main vmbus driver initialization routine.
1229  *
1230  * Here, we
1231  *	- initialize the vmbus driver context
1232  *	- invoke the vmbus hv main init routine
1233  *	- retrieve the channel offers
1234  */
1235 static int vmbus_bus_init(void)
1236 {
1237 	int ret;
1238 
1239 	/* Hypervisor initialization...setup hypercall page..etc */
1240 	ret = hv_init();
1241 	if (ret != 0) {
1242 		pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
1243 		return ret;
1244 	}
1245 
1246 	ret = bus_register(&hv_bus);
1247 	if (ret)
1248 		return ret;
1249 
1250 	hv_setup_vmbus_irq(vmbus_isr);
1251 
1252 	ret = hv_synic_alloc();
1253 	if (ret)
1254 		goto err_alloc;
1255 
1256 	ret = hv_stimer_alloc(VMBUS_MESSAGE_SINT);
1257 	if (ret < 0)
1258 		goto err_alloc;
1259 
1260 	/*
1261 	 * Initialize the per-cpu interrupt state and stimer state.
1262 	 * Then connect to the host.
1263 	 */
1264 	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hyperv/vmbus:online",
1265 				hv_synic_init, hv_synic_cleanup);
1266 	if (ret < 0)
1267 		goto err_cpuhp;
1268 	hyperv_cpuhp_online = ret;
1269 
1270 	ret = vmbus_connect();
1271 	if (ret)
1272 		goto err_connect;
1273 
1274 	/*
1275 	 * Only register if the crash MSRs are available
1276 	 */
1277 	if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
1278 		u64 hyperv_crash_ctl;
1279 		/*
1280 		 * Sysctl registration is not fatal, since by default
1281 		 * reporting is enabled.
1282 		 */
1283 		hv_ctl_table_hdr = register_sysctl_table(hv_root_table);
1284 		if (!hv_ctl_table_hdr)
1285 			pr_err("Hyper-V: sysctl table register error");
1286 
1287 		/*
1288 		 * Register for panic kmsg callback only if the right
1289 		 * capability is supported by the hypervisor.
1290 		 */
1291 		hv_get_crash_ctl(hyperv_crash_ctl);
1292 		if (hyperv_crash_ctl & HV_CRASH_CTL_CRASH_NOTIFY_MSG) {
1293 			hv_panic_page = (void *)get_zeroed_page(GFP_KERNEL);
1294 			if (hv_panic_page) {
1295 				ret = kmsg_dump_register(&hv_kmsg_dumper);
1296 				if (ret)
1297 					pr_err("Hyper-V: kmsg dump register "
1298 						"error 0x%x\n", ret);
1299 			} else
1300 				pr_err("Hyper-V: panic message page memory "
1301 					"allocation failed");
1302 		}
1303 
1304 		register_die_notifier(&hyperv_die_block);
1305 		atomic_notifier_chain_register(&panic_notifier_list,
1306 					       &hyperv_panic_block);
1307 	}
1308 
1309 	vmbus_request_offers();
1310 
1311 	return 0;
1312 
1313 err_connect:
1314 	cpuhp_remove_state(hyperv_cpuhp_online);
1315 err_cpuhp:
1316 	hv_stimer_free();
1317 err_alloc:
1318 	hv_synic_free();
1319 	hv_remove_vmbus_irq();
1320 
1321 	bus_unregister(&hv_bus);
1322 	free_page((unsigned long)hv_panic_page);
1323 	unregister_sysctl_table(hv_ctl_table_hdr);
1324 	hv_ctl_table_hdr = NULL;
1325 	return ret;
1326 }
1327 
1328 /**
1329  * __vmbus_child_driver_register() - Register a vmbus's driver
1330  * @hv_driver: Pointer to driver structure you want to register
1331  * @owner: owner module of the drv
1332  * @mod_name: module name string
1333  *
1334  * Registers the given driver with Linux through the 'driver_register()' call
1335  * and sets up the hyper-v vmbus handling for this driver.
1336  * It will return the state of the 'driver_register()' call.
1337  *
1338  */
1339 int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
1340 {
1341 	int ret;
1342 
1343 	pr_info("registering driver %s\n", hv_driver->name);
1344 
1345 	ret = vmbus_exists();
1346 	if (ret < 0)
1347 		return ret;
1348 
1349 	hv_driver->driver.name = hv_driver->name;
1350 	hv_driver->driver.owner = owner;
1351 	hv_driver->driver.mod_name = mod_name;
1352 	hv_driver->driver.bus = &hv_bus;
1353 
1354 	spin_lock_init(&hv_driver->dynids.lock);
1355 	INIT_LIST_HEAD(&hv_driver->dynids.list);
1356 
1357 	ret = driver_register(&hv_driver->driver);
1358 
1359 	return ret;
1360 }
1361 EXPORT_SYMBOL_GPL(__vmbus_driver_register);
1362 
1363 /**
1364  * vmbus_driver_unregister() - Unregister a vmbus's driver
1365  * @hv_driver: Pointer to driver structure you want to
1366  *             un-register
1367  *
1368  * Un-register the given driver that was previous registered with a call to
1369  * vmbus_driver_register()
1370  */
1371 void vmbus_driver_unregister(struct hv_driver *hv_driver)
1372 {
1373 	pr_info("unregistering driver %s\n", hv_driver->name);
1374 
1375 	if (!vmbus_exists()) {
1376 		driver_unregister(&hv_driver->driver);
1377 		vmbus_free_dynids(hv_driver);
1378 	}
1379 }
1380 EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
1381 
1382 
1383 /*
1384  * Called when last reference to channel is gone.
1385  */
1386 static void vmbus_chan_release(struct kobject *kobj)
1387 {
1388 	struct vmbus_channel *channel
1389 		= container_of(kobj, struct vmbus_channel, kobj);
1390 
1391 	kfree_rcu(channel, rcu);
1392 }
1393 
1394 struct vmbus_chan_attribute {
1395 	struct attribute attr;
1396 	ssize_t (*show)(struct vmbus_channel *chan, char *buf);
1397 	ssize_t (*store)(struct vmbus_channel *chan,
1398 			 const char *buf, size_t count);
1399 };
1400 #define VMBUS_CHAN_ATTR(_name, _mode, _show, _store) \
1401 	struct vmbus_chan_attribute chan_attr_##_name \
1402 		= __ATTR(_name, _mode, _show, _store)
1403 #define VMBUS_CHAN_ATTR_RW(_name) \
1404 	struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RW(_name)
1405 #define VMBUS_CHAN_ATTR_RO(_name) \
1406 	struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RO(_name)
1407 #define VMBUS_CHAN_ATTR_WO(_name) \
1408 	struct vmbus_chan_attribute chan_attr_##_name = __ATTR_WO(_name)
1409 
1410 static ssize_t vmbus_chan_attr_show(struct kobject *kobj,
1411 				    struct attribute *attr, char *buf)
1412 {
1413 	const struct vmbus_chan_attribute *attribute
1414 		= container_of(attr, struct vmbus_chan_attribute, attr);
1415 	struct vmbus_channel *chan
1416 		= container_of(kobj, struct vmbus_channel, kobj);
1417 
1418 	if (!attribute->show)
1419 		return -EIO;
1420 
1421 	return attribute->show(chan, buf);
1422 }
1423 
1424 static const struct sysfs_ops vmbus_chan_sysfs_ops = {
1425 	.show = vmbus_chan_attr_show,
1426 };
1427 
1428 static ssize_t out_mask_show(struct vmbus_channel *channel, char *buf)
1429 {
1430 	struct hv_ring_buffer_info *rbi = &channel->outbound;
1431 	ssize_t ret;
1432 
1433 	mutex_lock(&rbi->ring_buffer_mutex);
1434 	if (!rbi->ring_buffer) {
1435 		mutex_unlock(&rbi->ring_buffer_mutex);
1436 		return -EINVAL;
1437 	}
1438 
1439 	ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1440 	mutex_unlock(&rbi->ring_buffer_mutex);
1441 	return ret;
1442 }
1443 static VMBUS_CHAN_ATTR_RO(out_mask);
1444 
1445 static ssize_t in_mask_show(struct vmbus_channel *channel, char *buf)
1446 {
1447 	struct hv_ring_buffer_info *rbi = &channel->inbound;
1448 	ssize_t ret;
1449 
1450 	mutex_lock(&rbi->ring_buffer_mutex);
1451 	if (!rbi->ring_buffer) {
1452 		mutex_unlock(&rbi->ring_buffer_mutex);
1453 		return -EINVAL;
1454 	}
1455 
1456 	ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1457 	mutex_unlock(&rbi->ring_buffer_mutex);
1458 	return ret;
1459 }
1460 static VMBUS_CHAN_ATTR_RO(in_mask);
1461 
1462 static ssize_t read_avail_show(struct vmbus_channel *channel, char *buf)
1463 {
1464 	struct hv_ring_buffer_info *rbi = &channel->inbound;
1465 	ssize_t ret;
1466 
1467 	mutex_lock(&rbi->ring_buffer_mutex);
1468 	if (!rbi->ring_buffer) {
1469 		mutex_unlock(&rbi->ring_buffer_mutex);
1470 		return -EINVAL;
1471 	}
1472 
1473 	ret = sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi));
1474 	mutex_unlock(&rbi->ring_buffer_mutex);
1475 	return ret;
1476 }
1477 static VMBUS_CHAN_ATTR_RO(read_avail);
1478 
1479 static ssize_t write_avail_show(struct vmbus_channel *channel, char *buf)
1480 {
1481 	struct hv_ring_buffer_info *rbi = &channel->outbound;
1482 	ssize_t ret;
1483 
1484 	mutex_lock(&rbi->ring_buffer_mutex);
1485 	if (!rbi->ring_buffer) {
1486 		mutex_unlock(&rbi->ring_buffer_mutex);
1487 		return -EINVAL;
1488 	}
1489 
1490 	ret = sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi));
1491 	mutex_unlock(&rbi->ring_buffer_mutex);
1492 	return ret;
1493 }
1494 static VMBUS_CHAN_ATTR_RO(write_avail);
1495 
1496 static ssize_t show_target_cpu(struct vmbus_channel *channel, char *buf)
1497 {
1498 	return sprintf(buf, "%u\n", channel->target_cpu);
1499 }
1500 static VMBUS_CHAN_ATTR(cpu, S_IRUGO, show_target_cpu, NULL);
1501 
1502 static ssize_t channel_pending_show(struct vmbus_channel *channel,
1503 				    char *buf)
1504 {
1505 	return sprintf(buf, "%d\n",
1506 		       channel_pending(channel,
1507 				       vmbus_connection.monitor_pages[1]));
1508 }
1509 static VMBUS_CHAN_ATTR(pending, S_IRUGO, channel_pending_show, NULL);
1510 
1511 static ssize_t channel_latency_show(struct vmbus_channel *channel,
1512 				    char *buf)
1513 {
1514 	return sprintf(buf, "%d\n",
1515 		       channel_latency(channel,
1516 				       vmbus_connection.monitor_pages[1]));
1517 }
1518 static VMBUS_CHAN_ATTR(latency, S_IRUGO, channel_latency_show, NULL);
1519 
1520 static ssize_t channel_interrupts_show(struct vmbus_channel *channel, char *buf)
1521 {
1522 	return sprintf(buf, "%llu\n", channel->interrupts);
1523 }
1524 static VMBUS_CHAN_ATTR(interrupts, S_IRUGO, channel_interrupts_show, NULL);
1525 
1526 static ssize_t channel_events_show(struct vmbus_channel *channel, char *buf)
1527 {
1528 	return sprintf(buf, "%llu\n", channel->sig_events);
1529 }
1530 static VMBUS_CHAN_ATTR(events, S_IRUGO, channel_events_show, NULL);
1531 
1532 static ssize_t channel_intr_in_full_show(struct vmbus_channel *channel,
1533 					 char *buf)
1534 {
1535 	return sprintf(buf, "%llu\n",
1536 		       (unsigned long long)channel->intr_in_full);
1537 }
1538 static VMBUS_CHAN_ATTR(intr_in_full, 0444, channel_intr_in_full_show, NULL);
1539 
1540 static ssize_t channel_intr_out_empty_show(struct vmbus_channel *channel,
1541 					   char *buf)
1542 {
1543 	return sprintf(buf, "%llu\n",
1544 		       (unsigned long long)channel->intr_out_empty);
1545 }
1546 static VMBUS_CHAN_ATTR(intr_out_empty, 0444, channel_intr_out_empty_show, NULL);
1547 
1548 static ssize_t channel_out_full_first_show(struct vmbus_channel *channel,
1549 					   char *buf)
1550 {
1551 	return sprintf(buf, "%llu\n",
1552 		       (unsigned long long)channel->out_full_first);
1553 }
1554 static VMBUS_CHAN_ATTR(out_full_first, 0444, channel_out_full_first_show, NULL);
1555 
1556 static ssize_t channel_out_full_total_show(struct vmbus_channel *channel,
1557 					   char *buf)
1558 {
1559 	return sprintf(buf, "%llu\n",
1560 		       (unsigned long long)channel->out_full_total);
1561 }
1562 static VMBUS_CHAN_ATTR(out_full_total, 0444, channel_out_full_total_show, NULL);
1563 
1564 static ssize_t subchannel_monitor_id_show(struct vmbus_channel *channel,
1565 					  char *buf)
1566 {
1567 	return sprintf(buf, "%u\n", channel->offermsg.monitorid);
1568 }
1569 static VMBUS_CHAN_ATTR(monitor_id, S_IRUGO, subchannel_monitor_id_show, NULL);
1570 
1571 static ssize_t subchannel_id_show(struct vmbus_channel *channel,
1572 				  char *buf)
1573 {
1574 	return sprintf(buf, "%u\n",
1575 		       channel->offermsg.offer.sub_channel_index);
1576 }
1577 static VMBUS_CHAN_ATTR_RO(subchannel_id);
1578 
1579 static struct attribute *vmbus_chan_attrs[] = {
1580 	&chan_attr_out_mask.attr,
1581 	&chan_attr_in_mask.attr,
1582 	&chan_attr_read_avail.attr,
1583 	&chan_attr_write_avail.attr,
1584 	&chan_attr_cpu.attr,
1585 	&chan_attr_pending.attr,
1586 	&chan_attr_latency.attr,
1587 	&chan_attr_interrupts.attr,
1588 	&chan_attr_events.attr,
1589 	&chan_attr_intr_in_full.attr,
1590 	&chan_attr_intr_out_empty.attr,
1591 	&chan_attr_out_full_first.attr,
1592 	&chan_attr_out_full_total.attr,
1593 	&chan_attr_monitor_id.attr,
1594 	&chan_attr_subchannel_id.attr,
1595 	NULL
1596 };
1597 
1598 /*
1599  * Channel-level attribute_group callback function. Returns the permission for
1600  * each attribute, and returns 0 if an attribute is not visible.
1601  */
1602 static umode_t vmbus_chan_attr_is_visible(struct kobject *kobj,
1603 					  struct attribute *attr, int idx)
1604 {
1605 	const struct vmbus_channel *channel =
1606 		container_of(kobj, struct vmbus_channel, kobj);
1607 
1608 	/* Hide the monitor attributes if the monitor mechanism is not used. */
1609 	if (!channel->offermsg.monitor_allocated &&
1610 	    (attr == &chan_attr_pending.attr ||
1611 	     attr == &chan_attr_latency.attr ||
1612 	     attr == &chan_attr_monitor_id.attr))
1613 		return 0;
1614 
1615 	return attr->mode;
1616 }
1617 
1618 static struct attribute_group vmbus_chan_group = {
1619 	.attrs = vmbus_chan_attrs,
1620 	.is_visible = vmbus_chan_attr_is_visible
1621 };
1622 
1623 static struct kobj_type vmbus_chan_ktype = {
1624 	.sysfs_ops = &vmbus_chan_sysfs_ops,
1625 	.release = vmbus_chan_release,
1626 };
1627 
1628 /*
1629  * vmbus_add_channel_kobj - setup a sub-directory under device/channels
1630  */
1631 int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
1632 {
1633 	const struct device *device = &dev->device;
1634 	struct kobject *kobj = &channel->kobj;
1635 	u32 relid = channel->offermsg.child_relid;
1636 	int ret;
1637 
1638 	kobj->kset = dev->channels_kset;
1639 	ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
1640 				   "%u", relid);
1641 	if (ret)
1642 		return ret;
1643 
1644 	ret = sysfs_create_group(kobj, &vmbus_chan_group);
1645 
1646 	if (ret) {
1647 		/*
1648 		 * The calling functions' error handling paths will cleanup the
1649 		 * empty channel directory.
1650 		 */
1651 		dev_err(device, "Unable to set up channel sysfs files\n");
1652 		return ret;
1653 	}
1654 
1655 	kobject_uevent(kobj, KOBJ_ADD);
1656 
1657 	return 0;
1658 }
1659 
1660 /*
1661  * vmbus_remove_channel_attr_group - remove the channel's attribute group
1662  */
1663 void vmbus_remove_channel_attr_group(struct vmbus_channel *channel)
1664 {
1665 	sysfs_remove_group(&channel->kobj, &vmbus_chan_group);
1666 }
1667 
1668 /*
1669  * vmbus_device_create - Creates and registers a new child device
1670  * on the vmbus.
1671  */
1672 struct hv_device *vmbus_device_create(const guid_t *type,
1673 				      const guid_t *instance,
1674 				      struct vmbus_channel *channel)
1675 {
1676 	struct hv_device *child_device_obj;
1677 
1678 	child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
1679 	if (!child_device_obj) {
1680 		pr_err("Unable to allocate device object for child device\n");
1681 		return NULL;
1682 	}
1683 
1684 	child_device_obj->channel = channel;
1685 	guid_copy(&child_device_obj->dev_type, type);
1686 	guid_copy(&child_device_obj->dev_instance, instance);
1687 	child_device_obj->vendor_id = 0x1414; /* MSFT vendor ID */
1688 
1689 	return child_device_obj;
1690 }
1691 
1692 /*
1693  * vmbus_device_register - Register the child device
1694  */
1695 int vmbus_device_register(struct hv_device *child_device_obj)
1696 {
1697 	struct kobject *kobj = &child_device_obj->device.kobj;
1698 	int ret;
1699 
1700 	dev_set_name(&child_device_obj->device, "%pUl",
1701 		     child_device_obj->channel->offermsg.offer.if_instance.b);
1702 
1703 	child_device_obj->device.bus = &hv_bus;
1704 	child_device_obj->device.parent = &hv_acpi_dev->dev;
1705 	child_device_obj->device.release = vmbus_device_release;
1706 
1707 	/*
1708 	 * Register with the LDM. This will kick off the driver/device
1709 	 * binding...which will eventually call vmbus_match() and vmbus_probe()
1710 	 */
1711 	ret = device_register(&child_device_obj->device);
1712 	if (ret) {
1713 		pr_err("Unable to register child device\n");
1714 		return ret;
1715 	}
1716 
1717 	child_device_obj->channels_kset = kset_create_and_add("channels",
1718 							      NULL, kobj);
1719 	if (!child_device_obj->channels_kset) {
1720 		ret = -ENOMEM;
1721 		goto err_dev_unregister;
1722 	}
1723 
1724 	ret = vmbus_add_channel_kobj(child_device_obj,
1725 				     child_device_obj->channel);
1726 	if (ret) {
1727 		pr_err("Unable to register primary channeln");
1728 		goto err_kset_unregister;
1729 	}
1730 
1731 	return 0;
1732 
1733 err_kset_unregister:
1734 	kset_unregister(child_device_obj->channels_kset);
1735 
1736 err_dev_unregister:
1737 	device_unregister(&child_device_obj->device);
1738 	return ret;
1739 }
1740 
1741 /*
1742  * vmbus_device_unregister - Remove the specified child device
1743  * from the vmbus.
1744  */
1745 void vmbus_device_unregister(struct hv_device *device_obj)
1746 {
1747 	pr_debug("child device %s unregistered\n",
1748 		dev_name(&device_obj->device));
1749 
1750 	kset_unregister(device_obj->channels_kset);
1751 
1752 	/*
1753 	 * Kick off the process of unregistering the device.
1754 	 * This will call vmbus_remove() and eventually vmbus_device_release()
1755 	 */
1756 	device_unregister(&device_obj->device);
1757 }
1758 
1759 
1760 /*
1761  * VMBUS is an acpi enumerated device. Get the information we
1762  * need from DSDT.
1763  */
1764 #define VTPM_BASE_ADDRESS 0xfed40000
1765 static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
1766 {
1767 	resource_size_t start = 0;
1768 	resource_size_t end = 0;
1769 	struct resource *new_res;
1770 	struct resource **old_res = &hyperv_mmio;
1771 	struct resource **prev_res = NULL;
1772 
1773 	switch (res->type) {
1774 
1775 	/*
1776 	 * "Address" descriptors are for bus windows. Ignore
1777 	 * "memory" descriptors, which are for registers on
1778 	 * devices.
1779 	 */
1780 	case ACPI_RESOURCE_TYPE_ADDRESS32:
1781 		start = res->data.address32.address.minimum;
1782 		end = res->data.address32.address.maximum;
1783 		break;
1784 
1785 	case ACPI_RESOURCE_TYPE_ADDRESS64:
1786 		start = res->data.address64.address.minimum;
1787 		end = res->data.address64.address.maximum;
1788 		break;
1789 
1790 	default:
1791 		/* Unused resource type */
1792 		return AE_OK;
1793 
1794 	}
1795 	/*
1796 	 * Ignore ranges that are below 1MB, as they're not
1797 	 * necessary or useful here.
1798 	 */
1799 	if (end < 0x100000)
1800 		return AE_OK;
1801 
1802 	new_res = kzalloc(sizeof(*new_res), GFP_ATOMIC);
1803 	if (!new_res)
1804 		return AE_NO_MEMORY;
1805 
1806 	/* If this range overlaps the virtual TPM, truncate it. */
1807 	if (end > VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS)
1808 		end = VTPM_BASE_ADDRESS;
1809 
1810 	new_res->name = "hyperv mmio";
1811 	new_res->flags = IORESOURCE_MEM;
1812 	new_res->start = start;
1813 	new_res->end = end;
1814 
1815 	/*
1816 	 * If two ranges are adjacent, merge them.
1817 	 */
1818 	do {
1819 		if (!*old_res) {
1820 			*old_res = new_res;
1821 			break;
1822 		}
1823 
1824 		if (((*old_res)->end + 1) == new_res->start) {
1825 			(*old_res)->end = new_res->end;
1826 			kfree(new_res);
1827 			break;
1828 		}
1829 
1830 		if ((*old_res)->start == new_res->end + 1) {
1831 			(*old_res)->start = new_res->start;
1832 			kfree(new_res);
1833 			break;
1834 		}
1835 
1836 		if ((*old_res)->start > new_res->end) {
1837 			new_res->sibling = *old_res;
1838 			if (prev_res)
1839 				(*prev_res)->sibling = new_res;
1840 			*old_res = new_res;
1841 			break;
1842 		}
1843 
1844 		prev_res = old_res;
1845 		old_res = &(*old_res)->sibling;
1846 
1847 	} while (1);
1848 
1849 	return AE_OK;
1850 }
1851 
1852 static int vmbus_acpi_remove(struct acpi_device *device)
1853 {
1854 	struct resource *cur_res;
1855 	struct resource *next_res;
1856 
1857 	if (hyperv_mmio) {
1858 		if (fb_mmio) {
1859 			__release_region(hyperv_mmio, fb_mmio->start,
1860 					 resource_size(fb_mmio));
1861 			fb_mmio = NULL;
1862 		}
1863 
1864 		for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) {
1865 			next_res = cur_res->sibling;
1866 			kfree(cur_res);
1867 		}
1868 	}
1869 
1870 	return 0;
1871 }
1872 
1873 static void vmbus_reserve_fb(void)
1874 {
1875 	int size;
1876 	/*
1877 	 * Make a claim for the frame buffer in the resource tree under the
1878 	 * first node, which will be the one below 4GB.  The length seems to
1879 	 * be underreported, particularly in a Generation 1 VM.  So start out
1880 	 * reserving a larger area and make it smaller until it succeeds.
1881 	 */
1882 
1883 	if (screen_info.lfb_base) {
1884 		if (efi_enabled(EFI_BOOT))
1885 			size = max_t(__u32, screen_info.lfb_size, 0x800000);
1886 		else
1887 			size = max_t(__u32, screen_info.lfb_size, 0x4000000);
1888 
1889 		for (; !fb_mmio && (size >= 0x100000); size >>= 1) {
1890 			fb_mmio = __request_region(hyperv_mmio,
1891 						   screen_info.lfb_base, size,
1892 						   fb_mmio_name, 0);
1893 		}
1894 	}
1895 }
1896 
1897 /**
1898  * vmbus_allocate_mmio() - Pick a memory-mapped I/O range.
1899  * @new:		If successful, supplied a pointer to the
1900  *			allocated MMIO space.
1901  * @device_obj:		Identifies the caller
1902  * @min:		Minimum guest physical address of the
1903  *			allocation
1904  * @max:		Maximum guest physical address
1905  * @size:		Size of the range to be allocated
1906  * @align:		Alignment of the range to be allocated
1907  * @fb_overlap_ok:	Whether this allocation can be allowed
1908  *			to overlap the video frame buffer.
1909  *
1910  * This function walks the resources granted to VMBus by the
1911  * _CRS object in the ACPI namespace underneath the parent
1912  * "bridge" whether that's a root PCI bus in the Generation 1
1913  * case or a Module Device in the Generation 2 case.  It then
1914  * attempts to allocate from the global MMIO pool in a way that
1915  * matches the constraints supplied in these parameters and by
1916  * that _CRS.
1917  *
1918  * Return: 0 on success, -errno on failure
1919  */
1920 int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
1921 			resource_size_t min, resource_size_t max,
1922 			resource_size_t size, resource_size_t align,
1923 			bool fb_overlap_ok)
1924 {
1925 	struct resource *iter, *shadow;
1926 	resource_size_t range_min, range_max, start;
1927 	const char *dev_n = dev_name(&device_obj->device);
1928 	int retval;
1929 
1930 	retval = -ENXIO;
1931 	down(&hyperv_mmio_lock);
1932 
1933 	/*
1934 	 * If overlaps with frame buffers are allowed, then first attempt to
1935 	 * make the allocation from within the reserved region.  Because it
1936 	 * is already reserved, no shadow allocation is necessary.
1937 	 */
1938 	if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) &&
1939 	    !(max < fb_mmio->start)) {
1940 
1941 		range_min = fb_mmio->start;
1942 		range_max = fb_mmio->end;
1943 		start = (range_min + align - 1) & ~(align - 1);
1944 		for (; start + size - 1 <= range_max; start += align) {
1945 			*new = request_mem_region_exclusive(start, size, dev_n);
1946 			if (*new) {
1947 				retval = 0;
1948 				goto exit;
1949 			}
1950 		}
1951 	}
1952 
1953 	for (iter = hyperv_mmio; iter; iter = iter->sibling) {
1954 		if ((iter->start >= max) || (iter->end <= min))
1955 			continue;
1956 
1957 		range_min = iter->start;
1958 		range_max = iter->end;
1959 		start = (range_min + align - 1) & ~(align - 1);
1960 		for (; start + size - 1 <= range_max; start += align) {
1961 			shadow = __request_region(iter, start, size, NULL,
1962 						  IORESOURCE_BUSY);
1963 			if (!shadow)
1964 				continue;
1965 
1966 			*new = request_mem_region_exclusive(start, size, dev_n);
1967 			if (*new) {
1968 				shadow->name = (char *)*new;
1969 				retval = 0;
1970 				goto exit;
1971 			}
1972 
1973 			__release_region(iter, start, size);
1974 		}
1975 	}
1976 
1977 exit:
1978 	up(&hyperv_mmio_lock);
1979 	return retval;
1980 }
1981 EXPORT_SYMBOL_GPL(vmbus_allocate_mmio);
1982 
1983 /**
1984  * vmbus_free_mmio() - Free a memory-mapped I/O range.
1985  * @start:		Base address of region to release.
1986  * @size:		Size of the range to be allocated
1987  *
1988  * This function releases anything requested by
1989  * vmbus_mmio_allocate().
1990  */
1991 void vmbus_free_mmio(resource_size_t start, resource_size_t size)
1992 {
1993 	struct resource *iter;
1994 
1995 	down(&hyperv_mmio_lock);
1996 	for (iter = hyperv_mmio; iter; iter = iter->sibling) {
1997 		if ((iter->start >= start + size) || (iter->end <= start))
1998 			continue;
1999 
2000 		__release_region(iter, start, size);
2001 	}
2002 	release_mem_region(start, size);
2003 	up(&hyperv_mmio_lock);
2004 
2005 }
2006 EXPORT_SYMBOL_GPL(vmbus_free_mmio);
2007 
2008 static int vmbus_acpi_add(struct acpi_device *device)
2009 {
2010 	acpi_status result;
2011 	int ret_val = -ENODEV;
2012 	struct acpi_device *ancestor;
2013 
2014 	hv_acpi_dev = device;
2015 
2016 	result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
2017 					vmbus_walk_resources, NULL);
2018 
2019 	if (ACPI_FAILURE(result))
2020 		goto acpi_walk_err;
2021 	/*
2022 	 * Some ancestor of the vmbus acpi device (Gen1 or Gen2
2023 	 * firmware) is the VMOD that has the mmio ranges. Get that.
2024 	 */
2025 	for (ancestor = device->parent; ancestor; ancestor = ancestor->parent) {
2026 		result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS,
2027 					     vmbus_walk_resources, NULL);
2028 
2029 		if (ACPI_FAILURE(result))
2030 			continue;
2031 		if (hyperv_mmio) {
2032 			vmbus_reserve_fb();
2033 			break;
2034 		}
2035 	}
2036 	ret_val = 0;
2037 
2038 acpi_walk_err:
2039 	complete(&probe_event);
2040 	if (ret_val)
2041 		vmbus_acpi_remove(device);
2042 	return ret_val;
2043 }
2044 
2045 static const struct acpi_device_id vmbus_acpi_device_ids[] = {
2046 	{"VMBUS", 0},
2047 	{"VMBus", 0},
2048 	{"", 0},
2049 };
2050 MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
2051 
2052 static struct acpi_driver vmbus_acpi_driver = {
2053 	.name = "vmbus",
2054 	.ids = vmbus_acpi_device_ids,
2055 	.ops = {
2056 		.add = vmbus_acpi_add,
2057 		.remove = vmbus_acpi_remove,
2058 	},
2059 };
2060 
2061 static void hv_kexec_handler(void)
2062 {
2063 	hv_stimer_global_cleanup();
2064 	vmbus_initiate_unload(false);
2065 	vmbus_connection.conn_state = DISCONNECTED;
2066 	/* Make sure conn_state is set as hv_synic_cleanup checks for it */
2067 	mb();
2068 	cpuhp_remove_state(hyperv_cpuhp_online);
2069 	hyperv_cleanup();
2070 };
2071 
2072 static void hv_crash_handler(struct pt_regs *regs)
2073 {
2074 	int cpu;
2075 
2076 	vmbus_initiate_unload(true);
2077 	/*
2078 	 * In crash handler we can't schedule synic cleanup for all CPUs,
2079 	 * doing the cleanup for current CPU only. This should be sufficient
2080 	 * for kdump.
2081 	 */
2082 	vmbus_connection.conn_state = DISCONNECTED;
2083 	cpu = smp_processor_id();
2084 	hv_stimer_cleanup(cpu);
2085 	hv_synic_cleanup(cpu);
2086 	hyperv_cleanup();
2087 };
2088 
2089 static int __init hv_acpi_init(void)
2090 {
2091 	int ret, t;
2092 
2093 	if (!hv_is_hyperv_initialized())
2094 		return -ENODEV;
2095 
2096 	init_completion(&probe_event);
2097 
2098 	/*
2099 	 * Get ACPI resources first.
2100 	 */
2101 	ret = acpi_bus_register_driver(&vmbus_acpi_driver);
2102 
2103 	if (ret)
2104 		return ret;
2105 
2106 	t = wait_for_completion_timeout(&probe_event, 5*HZ);
2107 	if (t == 0) {
2108 		ret = -ETIMEDOUT;
2109 		goto cleanup;
2110 	}
2111 
2112 	ret = vmbus_bus_init();
2113 	if (ret)
2114 		goto cleanup;
2115 
2116 	hv_setup_kexec_handler(hv_kexec_handler);
2117 	hv_setup_crash_handler(hv_crash_handler);
2118 
2119 	return 0;
2120 
2121 cleanup:
2122 	acpi_bus_unregister_driver(&vmbus_acpi_driver);
2123 	hv_acpi_dev = NULL;
2124 	return ret;
2125 }
2126 
2127 static void __exit vmbus_exit(void)
2128 {
2129 	int cpu;
2130 
2131 	hv_remove_kexec_handler();
2132 	hv_remove_crash_handler();
2133 	vmbus_connection.conn_state = DISCONNECTED;
2134 	hv_stimer_global_cleanup();
2135 	vmbus_disconnect();
2136 	hv_remove_vmbus_irq();
2137 	for_each_online_cpu(cpu) {
2138 		struct hv_per_cpu_context *hv_cpu
2139 			= per_cpu_ptr(hv_context.cpu_context, cpu);
2140 
2141 		tasklet_kill(&hv_cpu->msg_dpc);
2142 	}
2143 	vmbus_free_channels();
2144 
2145 	if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
2146 		kmsg_dump_unregister(&hv_kmsg_dumper);
2147 		unregister_die_notifier(&hyperv_die_block);
2148 		atomic_notifier_chain_unregister(&panic_notifier_list,
2149 						 &hyperv_panic_block);
2150 	}
2151 
2152 	free_page((unsigned long)hv_panic_page);
2153 	unregister_sysctl_table(hv_ctl_table_hdr);
2154 	hv_ctl_table_hdr = NULL;
2155 	bus_unregister(&hv_bus);
2156 
2157 	cpuhp_remove_state(hyperv_cpuhp_online);
2158 	hv_synic_free();
2159 	acpi_bus_unregister_driver(&vmbus_acpi_driver);
2160 }
2161 
2162 
2163 MODULE_LICENSE("GPL");
2164 MODULE_DESCRIPTION("Microsoft Hyper-V VMBus Driver");
2165 
2166 subsys_initcall(hv_acpi_init);
2167 module_exit(vmbus_exit);
2168