xref: /openbmc/linux/drivers/hv/vmbus_drv.c (revision b548a774)
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 <linux/delay.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 <linux/kernel.h>
34 #include <linux/syscore_ops.h>
35 #include <clocksource/hyperv_timer.h>
36 #include "hyperv_vmbus.h"
37 
38 struct vmbus_dynid {
39 	struct list_head node;
40 	struct hv_vmbus_device_id id;
41 };
42 
43 static struct acpi_device  *hv_acpi_dev;
44 
45 static struct completion probe_event;
46 
47 static int hyperv_cpuhp_online;
48 
49 static void *hv_panic_page;
50 
51 /* Values parsed from ACPI DSDT */
52 static int vmbus_irq;
53 int vmbus_interrupt;
54 
55 /*
56  * Boolean to control whether to report panic messages over Hyper-V.
57  *
58  * It can be set via /proc/sys/kernel/hyperv_record_panic_msg
59  */
60 static int sysctl_record_panic_msg = 1;
61 
62 static int hyperv_report_reg(void)
63 {
64 	return !sysctl_record_panic_msg || !hv_panic_page;
65 }
66 
67 static int hyperv_panic_event(struct notifier_block *nb, unsigned long val,
68 			      void *args)
69 {
70 	struct pt_regs *regs;
71 
72 	vmbus_initiate_unload(true);
73 
74 	/*
75 	 * Hyper-V should be notified only once about a panic.  If we will be
76 	 * doing hyperv_report_panic_msg() later with kmsg data, don't do
77 	 * the notification here.
78 	 */
79 	if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE
80 	    && hyperv_report_reg()) {
81 		regs = current_pt_regs();
82 		hyperv_report_panic(regs, val, false);
83 	}
84 	return NOTIFY_DONE;
85 }
86 
87 static int hyperv_die_event(struct notifier_block *nb, unsigned long val,
88 			    void *args)
89 {
90 	struct die_args *die = args;
91 	struct pt_regs *regs = die->regs;
92 
93 	/* Don't notify Hyper-V if the die event is other than oops */
94 	if (val != DIE_OOPS)
95 		return NOTIFY_DONE;
96 
97 	/*
98 	 * Hyper-V should be notified only once about a panic.  If we will be
99 	 * doing hyperv_report_panic_msg() later with kmsg data, don't do
100 	 * the notification here.
101 	 */
102 	if (hyperv_report_reg())
103 		hyperv_report_panic(regs, val, true);
104 	return NOTIFY_DONE;
105 }
106 
107 static struct notifier_block hyperv_die_block = {
108 	.notifier_call = hyperv_die_event,
109 };
110 static struct notifier_block hyperv_panic_block = {
111 	.notifier_call = hyperv_panic_event,
112 };
113 
114 static const char *fb_mmio_name = "fb_range";
115 static struct resource *fb_mmio;
116 static struct resource *hyperv_mmio;
117 static DEFINE_MUTEX(hyperv_mmio_lock);
118 
119 static int vmbus_exists(void)
120 {
121 	if (hv_acpi_dev == NULL)
122 		return -ENODEV;
123 
124 	return 0;
125 }
126 
127 static u8 channel_monitor_group(const struct vmbus_channel *channel)
128 {
129 	return (u8)channel->offermsg.monitorid / 32;
130 }
131 
132 static u8 channel_monitor_offset(const struct vmbus_channel *channel)
133 {
134 	return (u8)channel->offermsg.monitorid % 32;
135 }
136 
137 static u32 channel_pending(const struct vmbus_channel *channel,
138 			   const struct hv_monitor_page *monitor_page)
139 {
140 	u8 monitor_group = channel_monitor_group(channel);
141 
142 	return monitor_page->trigger_group[monitor_group].pending;
143 }
144 
145 static u32 channel_latency(const struct vmbus_channel *channel,
146 			   const struct hv_monitor_page *monitor_page)
147 {
148 	u8 monitor_group = channel_monitor_group(channel);
149 	u8 monitor_offset = channel_monitor_offset(channel);
150 
151 	return monitor_page->latency[monitor_group][monitor_offset];
152 }
153 
154 static u32 channel_conn_id(struct vmbus_channel *channel,
155 			   struct hv_monitor_page *monitor_page)
156 {
157 	u8 monitor_group = channel_monitor_group(channel);
158 	u8 monitor_offset = channel_monitor_offset(channel);
159 
160 	return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
161 }
162 
163 static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
164 		       char *buf)
165 {
166 	struct hv_device *hv_dev = device_to_hv_device(dev);
167 
168 	if (!hv_dev->channel)
169 		return -ENODEV;
170 	return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
171 }
172 static DEVICE_ATTR_RO(id);
173 
174 static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
175 			  char *buf)
176 {
177 	struct hv_device *hv_dev = device_to_hv_device(dev);
178 
179 	if (!hv_dev->channel)
180 		return -ENODEV;
181 	return sprintf(buf, "%d\n", hv_dev->channel->state);
182 }
183 static DEVICE_ATTR_RO(state);
184 
185 static ssize_t monitor_id_show(struct device *dev,
186 			       struct device_attribute *dev_attr, char *buf)
187 {
188 	struct hv_device *hv_dev = device_to_hv_device(dev);
189 
190 	if (!hv_dev->channel)
191 		return -ENODEV;
192 	return sprintf(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
193 }
194 static DEVICE_ATTR_RO(monitor_id);
195 
196 static ssize_t class_id_show(struct device *dev,
197 			       struct device_attribute *dev_attr, char *buf)
198 {
199 	struct hv_device *hv_dev = device_to_hv_device(dev);
200 
201 	if (!hv_dev->channel)
202 		return -ENODEV;
203 	return sprintf(buf, "{%pUl}\n",
204 		       &hv_dev->channel->offermsg.offer.if_type);
205 }
206 static DEVICE_ATTR_RO(class_id);
207 
208 static ssize_t device_id_show(struct device *dev,
209 			      struct device_attribute *dev_attr, char *buf)
210 {
211 	struct hv_device *hv_dev = device_to_hv_device(dev);
212 
213 	if (!hv_dev->channel)
214 		return -ENODEV;
215 	return sprintf(buf, "{%pUl}\n",
216 		       &hv_dev->channel->offermsg.offer.if_instance);
217 }
218 static DEVICE_ATTR_RO(device_id);
219 
220 static ssize_t modalias_show(struct device *dev,
221 			     struct device_attribute *dev_attr, char *buf)
222 {
223 	struct hv_device *hv_dev = device_to_hv_device(dev);
224 
225 	return sprintf(buf, "vmbus:%*phN\n", UUID_SIZE, &hv_dev->dev_type);
226 }
227 static DEVICE_ATTR_RO(modalias);
228 
229 #ifdef CONFIG_NUMA
230 static ssize_t numa_node_show(struct device *dev,
231 			      struct device_attribute *attr, char *buf)
232 {
233 	struct hv_device *hv_dev = device_to_hv_device(dev);
234 
235 	if (!hv_dev->channel)
236 		return -ENODEV;
237 
238 	return sprintf(buf, "%d\n", cpu_to_node(hv_dev->channel->target_cpu));
239 }
240 static DEVICE_ATTR_RO(numa_node);
241 #endif
242 
243 static ssize_t server_monitor_pending_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_pending(hv_dev->channel,
253 				       vmbus_connection.monitor_pages[0]));
254 }
255 static DEVICE_ATTR_RO(server_monitor_pending);
256 
257 static ssize_t client_monitor_pending_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_pending(hv_dev->channel,
267 				       vmbus_connection.monitor_pages[1]));
268 }
269 static DEVICE_ATTR_RO(client_monitor_pending);
270 
271 static ssize_t server_monitor_latency_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_latency(hv_dev->channel,
281 				       vmbus_connection.monitor_pages[0]));
282 }
283 static DEVICE_ATTR_RO(server_monitor_latency);
284 
285 static ssize_t client_monitor_latency_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_latency(hv_dev->channel,
295 				       vmbus_connection.monitor_pages[1]));
296 }
297 static DEVICE_ATTR_RO(client_monitor_latency);
298 
299 static ssize_t server_monitor_conn_id_show(struct device *dev,
300 					   struct device_attribute *dev_attr,
301 					   char *buf)
302 {
303 	struct hv_device *hv_dev = device_to_hv_device(dev);
304 
305 	if (!hv_dev->channel)
306 		return -ENODEV;
307 	return sprintf(buf, "%d\n",
308 		       channel_conn_id(hv_dev->channel,
309 				       vmbus_connection.monitor_pages[0]));
310 }
311 static DEVICE_ATTR_RO(server_monitor_conn_id);
312 
313 static ssize_t client_monitor_conn_id_show(struct device *dev,
314 					   struct device_attribute *dev_attr,
315 					   char *buf)
316 {
317 	struct hv_device *hv_dev = device_to_hv_device(dev);
318 
319 	if (!hv_dev->channel)
320 		return -ENODEV;
321 	return sprintf(buf, "%d\n",
322 		       channel_conn_id(hv_dev->channel,
323 				       vmbus_connection.monitor_pages[1]));
324 }
325 static DEVICE_ATTR_RO(client_monitor_conn_id);
326 
327 static ssize_t out_intr_mask_show(struct device *dev,
328 				  struct device_attribute *dev_attr, char *buf)
329 {
330 	struct hv_device *hv_dev = device_to_hv_device(dev);
331 	struct hv_ring_buffer_debug_info outbound;
332 	int ret;
333 
334 	if (!hv_dev->channel)
335 		return -ENODEV;
336 
337 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
338 					  &outbound);
339 	if (ret < 0)
340 		return ret;
341 
342 	return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
343 }
344 static DEVICE_ATTR_RO(out_intr_mask);
345 
346 static ssize_t out_read_index_show(struct device *dev,
347 				   struct device_attribute *dev_attr, char *buf)
348 {
349 	struct hv_device *hv_dev = device_to_hv_device(dev);
350 	struct hv_ring_buffer_debug_info outbound;
351 	int ret;
352 
353 	if (!hv_dev->channel)
354 		return -ENODEV;
355 
356 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
357 					  &outbound);
358 	if (ret < 0)
359 		return ret;
360 	return sprintf(buf, "%d\n", outbound.current_read_index);
361 }
362 static DEVICE_ATTR_RO(out_read_index);
363 
364 static ssize_t out_write_index_show(struct device *dev,
365 				    struct device_attribute *dev_attr,
366 				    char *buf)
367 {
368 	struct hv_device *hv_dev = device_to_hv_device(dev);
369 	struct hv_ring_buffer_debug_info outbound;
370 	int ret;
371 
372 	if (!hv_dev->channel)
373 		return -ENODEV;
374 
375 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
376 					  &outbound);
377 	if (ret < 0)
378 		return ret;
379 	return sprintf(buf, "%d\n", outbound.current_write_index);
380 }
381 static DEVICE_ATTR_RO(out_write_index);
382 
383 static ssize_t out_read_bytes_avail_show(struct device *dev,
384 					 struct device_attribute *dev_attr,
385 					 char *buf)
386 {
387 	struct hv_device *hv_dev = device_to_hv_device(dev);
388 	struct hv_ring_buffer_debug_info outbound;
389 	int ret;
390 
391 	if (!hv_dev->channel)
392 		return -ENODEV;
393 
394 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
395 					  &outbound);
396 	if (ret < 0)
397 		return ret;
398 	return sprintf(buf, "%d\n", outbound.bytes_avail_toread);
399 }
400 static DEVICE_ATTR_RO(out_read_bytes_avail);
401 
402 static ssize_t out_write_bytes_avail_show(struct device *dev,
403 					  struct device_attribute *dev_attr,
404 					  char *buf)
405 {
406 	struct hv_device *hv_dev = device_to_hv_device(dev);
407 	struct hv_ring_buffer_debug_info outbound;
408 	int ret;
409 
410 	if (!hv_dev->channel)
411 		return -ENODEV;
412 
413 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
414 					  &outbound);
415 	if (ret < 0)
416 		return ret;
417 	return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
418 }
419 static DEVICE_ATTR_RO(out_write_bytes_avail);
420 
421 static ssize_t in_intr_mask_show(struct device *dev,
422 				 struct device_attribute *dev_attr, char *buf)
423 {
424 	struct hv_device *hv_dev = device_to_hv_device(dev);
425 	struct hv_ring_buffer_debug_info inbound;
426 	int ret;
427 
428 	if (!hv_dev->channel)
429 		return -ENODEV;
430 
431 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
432 	if (ret < 0)
433 		return ret;
434 
435 	return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
436 }
437 static DEVICE_ATTR_RO(in_intr_mask);
438 
439 static ssize_t in_read_index_show(struct device *dev,
440 				  struct device_attribute *dev_attr, char *buf)
441 {
442 	struct hv_device *hv_dev = device_to_hv_device(dev);
443 	struct hv_ring_buffer_debug_info inbound;
444 	int ret;
445 
446 	if (!hv_dev->channel)
447 		return -ENODEV;
448 
449 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
450 	if (ret < 0)
451 		return ret;
452 
453 	return sprintf(buf, "%d\n", inbound.current_read_index);
454 }
455 static DEVICE_ATTR_RO(in_read_index);
456 
457 static ssize_t in_write_index_show(struct device *dev,
458 				   struct device_attribute *dev_attr, char *buf)
459 {
460 	struct hv_device *hv_dev = device_to_hv_device(dev);
461 	struct hv_ring_buffer_debug_info inbound;
462 	int ret;
463 
464 	if (!hv_dev->channel)
465 		return -ENODEV;
466 
467 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
468 	if (ret < 0)
469 		return ret;
470 
471 	return sprintf(buf, "%d\n", inbound.current_write_index);
472 }
473 static DEVICE_ATTR_RO(in_write_index);
474 
475 static ssize_t in_read_bytes_avail_show(struct device *dev,
476 					struct device_attribute *dev_attr,
477 					char *buf)
478 {
479 	struct hv_device *hv_dev = device_to_hv_device(dev);
480 	struct hv_ring_buffer_debug_info inbound;
481 	int ret;
482 
483 	if (!hv_dev->channel)
484 		return -ENODEV;
485 
486 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
487 	if (ret < 0)
488 		return ret;
489 
490 	return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
491 }
492 static DEVICE_ATTR_RO(in_read_bytes_avail);
493 
494 static ssize_t in_write_bytes_avail_show(struct device *dev,
495 					 struct device_attribute *dev_attr,
496 					 char *buf)
497 {
498 	struct hv_device *hv_dev = device_to_hv_device(dev);
499 	struct hv_ring_buffer_debug_info inbound;
500 	int ret;
501 
502 	if (!hv_dev->channel)
503 		return -ENODEV;
504 
505 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
506 	if (ret < 0)
507 		return ret;
508 
509 	return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
510 }
511 static DEVICE_ATTR_RO(in_write_bytes_avail);
512 
513 static ssize_t channel_vp_mapping_show(struct device *dev,
514 				       struct device_attribute *dev_attr,
515 				       char *buf)
516 {
517 	struct hv_device *hv_dev = device_to_hv_device(dev);
518 	struct vmbus_channel *channel = hv_dev->channel, *cur_sc;
519 	int buf_size = PAGE_SIZE, n_written, tot_written;
520 	struct list_head *cur;
521 
522 	if (!channel)
523 		return -ENODEV;
524 
525 	mutex_lock(&vmbus_connection.channel_mutex);
526 
527 	tot_written = snprintf(buf, buf_size, "%u:%u\n",
528 		channel->offermsg.child_relid, channel->target_cpu);
529 
530 	list_for_each(cur, &channel->sc_list) {
531 		if (tot_written >= buf_size - 1)
532 			break;
533 
534 		cur_sc = list_entry(cur, struct vmbus_channel, sc_list);
535 		n_written = scnprintf(buf + tot_written,
536 				     buf_size - tot_written,
537 				     "%u:%u\n",
538 				     cur_sc->offermsg.child_relid,
539 				     cur_sc->target_cpu);
540 		tot_written += n_written;
541 	}
542 
543 	mutex_unlock(&vmbus_connection.channel_mutex);
544 
545 	return tot_written;
546 }
547 static DEVICE_ATTR_RO(channel_vp_mapping);
548 
549 static ssize_t vendor_show(struct device *dev,
550 			   struct device_attribute *dev_attr,
551 			   char *buf)
552 {
553 	struct hv_device *hv_dev = device_to_hv_device(dev);
554 
555 	return sprintf(buf, "0x%x\n", hv_dev->vendor_id);
556 }
557 static DEVICE_ATTR_RO(vendor);
558 
559 static ssize_t device_show(struct device *dev,
560 			   struct device_attribute *dev_attr,
561 			   char *buf)
562 {
563 	struct hv_device *hv_dev = device_to_hv_device(dev);
564 
565 	return sprintf(buf, "0x%x\n", hv_dev->device_id);
566 }
567 static DEVICE_ATTR_RO(device);
568 
569 static ssize_t driver_override_store(struct device *dev,
570 				     struct device_attribute *attr,
571 				     const char *buf, size_t count)
572 {
573 	struct hv_device *hv_dev = device_to_hv_device(dev);
574 	char *driver_override, *old, *cp;
575 
576 	/* We need to keep extra room for a newline */
577 	if (count >= (PAGE_SIZE - 1))
578 		return -EINVAL;
579 
580 	driver_override = kstrndup(buf, count, GFP_KERNEL);
581 	if (!driver_override)
582 		return -ENOMEM;
583 
584 	cp = strchr(driver_override, '\n');
585 	if (cp)
586 		*cp = '\0';
587 
588 	device_lock(dev);
589 	old = hv_dev->driver_override;
590 	if (strlen(driver_override)) {
591 		hv_dev->driver_override = driver_override;
592 	} else {
593 		kfree(driver_override);
594 		hv_dev->driver_override = NULL;
595 	}
596 	device_unlock(dev);
597 
598 	kfree(old);
599 
600 	return count;
601 }
602 
603 static ssize_t driver_override_show(struct device *dev,
604 				    struct device_attribute *attr, char *buf)
605 {
606 	struct hv_device *hv_dev = device_to_hv_device(dev);
607 	ssize_t len;
608 
609 	device_lock(dev);
610 	len = snprintf(buf, PAGE_SIZE, "%s\n", hv_dev->driver_override);
611 	device_unlock(dev);
612 
613 	return len;
614 }
615 static DEVICE_ATTR_RW(driver_override);
616 
617 /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
618 static struct attribute *vmbus_dev_attrs[] = {
619 	&dev_attr_id.attr,
620 	&dev_attr_state.attr,
621 	&dev_attr_monitor_id.attr,
622 	&dev_attr_class_id.attr,
623 	&dev_attr_device_id.attr,
624 	&dev_attr_modalias.attr,
625 #ifdef CONFIG_NUMA
626 	&dev_attr_numa_node.attr,
627 #endif
628 	&dev_attr_server_monitor_pending.attr,
629 	&dev_attr_client_monitor_pending.attr,
630 	&dev_attr_server_monitor_latency.attr,
631 	&dev_attr_client_monitor_latency.attr,
632 	&dev_attr_server_monitor_conn_id.attr,
633 	&dev_attr_client_monitor_conn_id.attr,
634 	&dev_attr_out_intr_mask.attr,
635 	&dev_attr_out_read_index.attr,
636 	&dev_attr_out_write_index.attr,
637 	&dev_attr_out_read_bytes_avail.attr,
638 	&dev_attr_out_write_bytes_avail.attr,
639 	&dev_attr_in_intr_mask.attr,
640 	&dev_attr_in_read_index.attr,
641 	&dev_attr_in_write_index.attr,
642 	&dev_attr_in_read_bytes_avail.attr,
643 	&dev_attr_in_write_bytes_avail.attr,
644 	&dev_attr_channel_vp_mapping.attr,
645 	&dev_attr_vendor.attr,
646 	&dev_attr_device.attr,
647 	&dev_attr_driver_override.attr,
648 	NULL,
649 };
650 
651 /*
652  * Device-level attribute_group callback function. Returns the permission for
653  * each attribute, and returns 0 if an attribute is not visible.
654  */
655 static umode_t vmbus_dev_attr_is_visible(struct kobject *kobj,
656 					 struct attribute *attr, int idx)
657 {
658 	struct device *dev = kobj_to_dev(kobj);
659 	const struct hv_device *hv_dev = device_to_hv_device(dev);
660 
661 	/* Hide the monitor attributes if the monitor mechanism is not used. */
662 	if (!hv_dev->channel->offermsg.monitor_allocated &&
663 	    (attr == &dev_attr_monitor_id.attr ||
664 	     attr == &dev_attr_server_monitor_pending.attr ||
665 	     attr == &dev_attr_client_monitor_pending.attr ||
666 	     attr == &dev_attr_server_monitor_latency.attr ||
667 	     attr == &dev_attr_client_monitor_latency.attr ||
668 	     attr == &dev_attr_server_monitor_conn_id.attr ||
669 	     attr == &dev_attr_client_monitor_conn_id.attr))
670 		return 0;
671 
672 	return attr->mode;
673 }
674 
675 static const struct attribute_group vmbus_dev_group = {
676 	.attrs = vmbus_dev_attrs,
677 	.is_visible = vmbus_dev_attr_is_visible
678 };
679 __ATTRIBUTE_GROUPS(vmbus_dev);
680 
681 /* Set up the attribute for /sys/bus/vmbus/hibernation */
682 static ssize_t hibernation_show(struct bus_type *bus, char *buf)
683 {
684 	return sprintf(buf, "%d\n", !!hv_is_hibernation_supported());
685 }
686 
687 static BUS_ATTR_RO(hibernation);
688 
689 static struct attribute *vmbus_bus_attrs[] = {
690 	&bus_attr_hibernation.attr,
691 	NULL,
692 };
693 static const struct attribute_group vmbus_bus_group = {
694 	.attrs = vmbus_bus_attrs,
695 };
696 __ATTRIBUTE_GROUPS(vmbus_bus);
697 
698 /*
699  * vmbus_uevent - add uevent for our device
700  *
701  * This routine is invoked when a device is added or removed on the vmbus to
702  * generate a uevent to udev in the userspace. The udev will then look at its
703  * rule and the uevent generated here to load the appropriate driver
704  *
705  * The alias string will be of the form vmbus:guid where guid is the string
706  * representation of the device guid (each byte of the guid will be
707  * represented with two hex characters.
708  */
709 static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
710 {
711 	struct hv_device *dev = device_to_hv_device(device);
712 	const char *format = "MODALIAS=vmbus:%*phN";
713 
714 	return add_uevent_var(env, format, UUID_SIZE, &dev->dev_type);
715 }
716 
717 static const struct hv_vmbus_device_id *
718 hv_vmbus_dev_match(const struct hv_vmbus_device_id *id, const guid_t *guid)
719 {
720 	if (id == NULL)
721 		return NULL; /* empty device table */
722 
723 	for (; !guid_is_null(&id->guid); id++)
724 		if (guid_equal(&id->guid, guid))
725 			return id;
726 
727 	return NULL;
728 }
729 
730 static const struct hv_vmbus_device_id *
731 hv_vmbus_dynid_match(struct hv_driver *drv, const guid_t *guid)
732 {
733 	const struct hv_vmbus_device_id *id = NULL;
734 	struct vmbus_dynid *dynid;
735 
736 	spin_lock(&drv->dynids.lock);
737 	list_for_each_entry(dynid, &drv->dynids.list, node) {
738 		if (guid_equal(&dynid->id.guid, guid)) {
739 			id = &dynid->id;
740 			break;
741 		}
742 	}
743 	spin_unlock(&drv->dynids.lock);
744 
745 	return id;
746 }
747 
748 static const struct hv_vmbus_device_id vmbus_device_null;
749 
750 /*
751  * Return a matching hv_vmbus_device_id pointer.
752  * If there is no match, return NULL.
753  */
754 static const struct hv_vmbus_device_id *hv_vmbus_get_id(struct hv_driver *drv,
755 							struct hv_device *dev)
756 {
757 	const guid_t *guid = &dev->dev_type;
758 	const struct hv_vmbus_device_id *id;
759 
760 	/* When driver_override is set, only bind to the matching driver */
761 	if (dev->driver_override && strcmp(dev->driver_override, drv->name))
762 		return NULL;
763 
764 	/* Look at the dynamic ids first, before the static ones */
765 	id = hv_vmbus_dynid_match(drv, guid);
766 	if (!id)
767 		id = hv_vmbus_dev_match(drv->id_table, guid);
768 
769 	/* driver_override will always match, send a dummy id */
770 	if (!id && dev->driver_override)
771 		id = &vmbus_device_null;
772 
773 	return id;
774 }
775 
776 /* vmbus_add_dynid - add a new device ID to this driver and re-probe devices */
777 static int vmbus_add_dynid(struct hv_driver *drv, guid_t *guid)
778 {
779 	struct vmbus_dynid *dynid;
780 
781 	dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
782 	if (!dynid)
783 		return -ENOMEM;
784 
785 	dynid->id.guid = *guid;
786 
787 	spin_lock(&drv->dynids.lock);
788 	list_add_tail(&dynid->node, &drv->dynids.list);
789 	spin_unlock(&drv->dynids.lock);
790 
791 	return driver_attach(&drv->driver);
792 }
793 
794 static void vmbus_free_dynids(struct hv_driver *drv)
795 {
796 	struct vmbus_dynid *dynid, *n;
797 
798 	spin_lock(&drv->dynids.lock);
799 	list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
800 		list_del(&dynid->node);
801 		kfree(dynid);
802 	}
803 	spin_unlock(&drv->dynids.lock);
804 }
805 
806 /*
807  * store_new_id - sysfs frontend to vmbus_add_dynid()
808  *
809  * Allow GUIDs to be added to an existing driver via sysfs.
810  */
811 static ssize_t new_id_store(struct device_driver *driver, const char *buf,
812 			    size_t count)
813 {
814 	struct hv_driver *drv = drv_to_hv_drv(driver);
815 	guid_t guid;
816 	ssize_t retval;
817 
818 	retval = guid_parse(buf, &guid);
819 	if (retval)
820 		return retval;
821 
822 	if (hv_vmbus_dynid_match(drv, &guid))
823 		return -EEXIST;
824 
825 	retval = vmbus_add_dynid(drv, &guid);
826 	if (retval)
827 		return retval;
828 	return count;
829 }
830 static DRIVER_ATTR_WO(new_id);
831 
832 /*
833  * store_remove_id - remove a PCI device ID from this driver
834  *
835  * Removes a dynamic pci device ID to this driver.
836  */
837 static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
838 			       size_t count)
839 {
840 	struct hv_driver *drv = drv_to_hv_drv(driver);
841 	struct vmbus_dynid *dynid, *n;
842 	guid_t guid;
843 	ssize_t retval;
844 
845 	retval = guid_parse(buf, &guid);
846 	if (retval)
847 		return retval;
848 
849 	retval = -ENODEV;
850 	spin_lock(&drv->dynids.lock);
851 	list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
852 		struct hv_vmbus_device_id *id = &dynid->id;
853 
854 		if (guid_equal(&id->guid, &guid)) {
855 			list_del(&dynid->node);
856 			kfree(dynid);
857 			retval = count;
858 			break;
859 		}
860 	}
861 	spin_unlock(&drv->dynids.lock);
862 
863 	return retval;
864 }
865 static DRIVER_ATTR_WO(remove_id);
866 
867 static struct attribute *vmbus_drv_attrs[] = {
868 	&driver_attr_new_id.attr,
869 	&driver_attr_remove_id.attr,
870 	NULL,
871 };
872 ATTRIBUTE_GROUPS(vmbus_drv);
873 
874 
875 /*
876  * vmbus_match - Attempt to match the specified device to the specified driver
877  */
878 static int vmbus_match(struct device *device, struct device_driver *driver)
879 {
880 	struct hv_driver *drv = drv_to_hv_drv(driver);
881 	struct hv_device *hv_dev = device_to_hv_device(device);
882 
883 	/* The hv_sock driver handles all hv_sock offers. */
884 	if (is_hvsock_channel(hv_dev->channel))
885 		return drv->hvsock;
886 
887 	if (hv_vmbus_get_id(drv, hv_dev))
888 		return 1;
889 
890 	return 0;
891 }
892 
893 /*
894  * vmbus_probe - Add the new vmbus's child device
895  */
896 static int vmbus_probe(struct device *child_device)
897 {
898 	int ret = 0;
899 	struct hv_driver *drv =
900 			drv_to_hv_drv(child_device->driver);
901 	struct hv_device *dev = device_to_hv_device(child_device);
902 	const struct hv_vmbus_device_id *dev_id;
903 
904 	dev_id = hv_vmbus_get_id(drv, dev);
905 	if (drv->probe) {
906 		ret = drv->probe(dev, dev_id);
907 		if (ret != 0)
908 			pr_err("probe failed for device %s (%d)\n",
909 			       dev_name(child_device), ret);
910 
911 	} else {
912 		pr_err("probe not set for driver %s\n",
913 		       dev_name(child_device));
914 		ret = -ENODEV;
915 	}
916 	return ret;
917 }
918 
919 /*
920  * vmbus_remove - Remove a vmbus device
921  */
922 static int vmbus_remove(struct device *child_device)
923 {
924 	struct hv_driver *drv;
925 	struct hv_device *dev = device_to_hv_device(child_device);
926 
927 	if (child_device->driver) {
928 		drv = drv_to_hv_drv(child_device->driver);
929 		if (drv->remove)
930 			drv->remove(dev);
931 	}
932 
933 	return 0;
934 }
935 
936 
937 /*
938  * vmbus_shutdown - Shutdown a vmbus device
939  */
940 static void vmbus_shutdown(struct device *child_device)
941 {
942 	struct hv_driver *drv;
943 	struct hv_device *dev = device_to_hv_device(child_device);
944 
945 
946 	/* The device may not be attached yet */
947 	if (!child_device->driver)
948 		return;
949 
950 	drv = drv_to_hv_drv(child_device->driver);
951 
952 	if (drv->shutdown)
953 		drv->shutdown(dev);
954 }
955 
956 #ifdef CONFIG_PM_SLEEP
957 /*
958  * vmbus_suspend - Suspend a vmbus device
959  */
960 static int vmbus_suspend(struct device *child_device)
961 {
962 	struct hv_driver *drv;
963 	struct hv_device *dev = device_to_hv_device(child_device);
964 
965 	/* The device may not be attached yet */
966 	if (!child_device->driver)
967 		return 0;
968 
969 	drv = drv_to_hv_drv(child_device->driver);
970 	if (!drv->suspend)
971 		return -EOPNOTSUPP;
972 
973 	return drv->suspend(dev);
974 }
975 
976 /*
977  * vmbus_resume - Resume a vmbus device
978  */
979 static int vmbus_resume(struct device *child_device)
980 {
981 	struct hv_driver *drv;
982 	struct hv_device *dev = device_to_hv_device(child_device);
983 
984 	/* The device may not be attached yet */
985 	if (!child_device->driver)
986 		return 0;
987 
988 	drv = drv_to_hv_drv(child_device->driver);
989 	if (!drv->resume)
990 		return -EOPNOTSUPP;
991 
992 	return drv->resume(dev);
993 }
994 #else
995 #define vmbus_suspend NULL
996 #define vmbus_resume NULL
997 #endif /* CONFIG_PM_SLEEP */
998 
999 /*
1000  * vmbus_device_release - Final callback release of the vmbus child device
1001  */
1002 static void vmbus_device_release(struct device *device)
1003 {
1004 	struct hv_device *hv_dev = device_to_hv_device(device);
1005 	struct vmbus_channel *channel = hv_dev->channel;
1006 
1007 	hv_debug_rm_dev_dir(hv_dev);
1008 
1009 	mutex_lock(&vmbus_connection.channel_mutex);
1010 	hv_process_channel_removal(channel);
1011 	mutex_unlock(&vmbus_connection.channel_mutex);
1012 	kfree(hv_dev);
1013 }
1014 
1015 /*
1016  * Note: we must use the "noirq" ops: see the comment before vmbus_bus_pm.
1017  *
1018  * suspend_noirq/resume_noirq are set to NULL to support Suspend-to-Idle: we
1019  * shouldn't suspend the vmbus devices upon Suspend-to-Idle, otherwise there
1020  * is no way to wake up a Generation-2 VM.
1021  *
1022  * The other 4 ops are for hibernation.
1023  */
1024 
1025 static const struct dev_pm_ops vmbus_pm = {
1026 	.suspend_noirq	= NULL,
1027 	.resume_noirq	= NULL,
1028 	.freeze_noirq	= vmbus_suspend,
1029 	.thaw_noirq	= vmbus_resume,
1030 	.poweroff_noirq	= vmbus_suspend,
1031 	.restore_noirq	= vmbus_resume,
1032 };
1033 
1034 /* The one and only one */
1035 static struct bus_type  hv_bus = {
1036 	.name =		"vmbus",
1037 	.match =		vmbus_match,
1038 	.shutdown =		vmbus_shutdown,
1039 	.remove =		vmbus_remove,
1040 	.probe =		vmbus_probe,
1041 	.uevent =		vmbus_uevent,
1042 	.dev_groups =		vmbus_dev_groups,
1043 	.drv_groups =		vmbus_drv_groups,
1044 	.bus_groups =		vmbus_bus_groups,
1045 	.pm =			&vmbus_pm,
1046 };
1047 
1048 struct onmessage_work_context {
1049 	struct work_struct work;
1050 	struct {
1051 		struct hv_message_header header;
1052 		u8 payload[];
1053 	} msg;
1054 };
1055 
1056 static void vmbus_onmessage_work(struct work_struct *work)
1057 {
1058 	struct onmessage_work_context *ctx;
1059 
1060 	/* Do not process messages if we're in DISCONNECTED state */
1061 	if (vmbus_connection.conn_state == DISCONNECTED)
1062 		return;
1063 
1064 	ctx = container_of(work, struct onmessage_work_context,
1065 			   work);
1066 	vmbus_onmessage((struct vmbus_channel_message_header *)
1067 			&ctx->msg.payload);
1068 	kfree(ctx);
1069 }
1070 
1071 void vmbus_on_msg_dpc(unsigned long data)
1072 {
1073 	struct hv_per_cpu_context *hv_cpu = (void *)data;
1074 	void *page_addr = hv_cpu->synic_message_page;
1075 	struct hv_message msg_copy, *msg = (struct hv_message *)page_addr +
1076 				  VMBUS_MESSAGE_SINT;
1077 	struct vmbus_channel_message_header *hdr;
1078 	enum vmbus_channel_message_type msgtype;
1079 	const struct vmbus_channel_message_table_entry *entry;
1080 	struct onmessage_work_context *ctx;
1081 	__u8 payload_size;
1082 	u32 message_type;
1083 
1084 	/*
1085 	 * 'enum vmbus_channel_message_type' is supposed to always be 'u32' as
1086 	 * it is being used in 'struct vmbus_channel_message_header' definition
1087 	 * which is supposed to match hypervisor ABI.
1088 	 */
1089 	BUILD_BUG_ON(sizeof(enum vmbus_channel_message_type) != sizeof(u32));
1090 
1091 	/*
1092 	 * Since the message is in memory shared with the host, an erroneous or
1093 	 * malicious Hyper-V could modify the message while vmbus_on_msg_dpc()
1094 	 * or individual message handlers are executing; to prevent this, copy
1095 	 * the message into private memory.
1096 	 */
1097 	memcpy(&msg_copy, msg, sizeof(struct hv_message));
1098 
1099 	message_type = msg_copy.header.message_type;
1100 	if (message_type == HVMSG_NONE)
1101 		/* no msg */
1102 		return;
1103 
1104 	hdr = (struct vmbus_channel_message_header *)msg_copy.u.payload;
1105 	msgtype = hdr->msgtype;
1106 
1107 	trace_vmbus_on_msg_dpc(hdr);
1108 
1109 	if (msgtype >= CHANNELMSG_COUNT) {
1110 		WARN_ONCE(1, "unknown msgtype=%d\n", msgtype);
1111 		goto msg_handled;
1112 	}
1113 
1114 	payload_size = msg_copy.header.payload_size;
1115 	if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT) {
1116 		WARN_ONCE(1, "payload size is too large (%d)\n", payload_size);
1117 		goto msg_handled;
1118 	}
1119 
1120 	entry = &channel_message_table[msgtype];
1121 
1122 	if (!entry->message_handler)
1123 		goto msg_handled;
1124 
1125 	if (payload_size < entry->min_payload_len) {
1126 		WARN_ONCE(1, "message too short: msgtype=%d len=%d\n", msgtype, payload_size);
1127 		goto msg_handled;
1128 	}
1129 
1130 	if (entry->handler_type	== VMHT_BLOCKING) {
1131 		ctx = kmalloc(sizeof(*ctx) + payload_size, GFP_ATOMIC);
1132 		if (ctx == NULL)
1133 			return;
1134 
1135 		INIT_WORK(&ctx->work, vmbus_onmessage_work);
1136 		memcpy(&ctx->msg, &msg_copy, sizeof(msg->header) + payload_size);
1137 
1138 		/*
1139 		 * The host can generate a rescind message while we
1140 		 * may still be handling the original offer. We deal with
1141 		 * this condition by relying on the synchronization provided
1142 		 * by offer_in_progress and by channel_mutex.  See also the
1143 		 * inline comments in vmbus_onoffer_rescind().
1144 		 */
1145 		switch (msgtype) {
1146 		case CHANNELMSG_RESCIND_CHANNELOFFER:
1147 			/*
1148 			 * If we are handling the rescind message;
1149 			 * schedule the work on the global work queue.
1150 			 *
1151 			 * The OFFER message and the RESCIND message should
1152 			 * not be handled by the same serialized work queue,
1153 			 * because the OFFER handler may call vmbus_open(),
1154 			 * which tries to open the channel by sending an
1155 			 * OPEN_CHANNEL message to the host and waits for
1156 			 * the host's response; however, if the host has
1157 			 * rescinded the channel before it receives the
1158 			 * OPEN_CHANNEL message, the host just silently
1159 			 * ignores the OPEN_CHANNEL message; as a result,
1160 			 * the guest's OFFER handler hangs for ever, if we
1161 			 * handle the RESCIND message in the same serialized
1162 			 * work queue: the RESCIND handler can not start to
1163 			 * run before the OFFER handler finishes.
1164 			 */
1165 			schedule_work(&ctx->work);
1166 			break;
1167 
1168 		case CHANNELMSG_OFFERCHANNEL:
1169 			/*
1170 			 * The host sends the offer message of a given channel
1171 			 * before sending the rescind message of the same
1172 			 * channel.  These messages are sent to the guest's
1173 			 * connect CPU; the guest then starts processing them
1174 			 * in the tasklet handler on this CPU:
1175 			 *
1176 			 * VMBUS_CONNECT_CPU
1177 			 *
1178 			 * [vmbus_on_msg_dpc()]
1179 			 * atomic_inc()  // CHANNELMSG_OFFERCHANNEL
1180 			 * queue_work()
1181 			 * ...
1182 			 * [vmbus_on_msg_dpc()]
1183 			 * schedule_work()  // CHANNELMSG_RESCIND_CHANNELOFFER
1184 			 *
1185 			 * We rely on the memory-ordering properties of the
1186 			 * queue_work() and schedule_work() primitives, which
1187 			 * guarantee that the atomic increment will be visible
1188 			 * to the CPUs which will execute the offer & rescind
1189 			 * works by the time these works will start execution.
1190 			 */
1191 			atomic_inc(&vmbus_connection.offer_in_progress);
1192 			fallthrough;
1193 
1194 		default:
1195 			queue_work(vmbus_connection.work_queue, &ctx->work);
1196 		}
1197 	} else
1198 		entry->message_handler(hdr);
1199 
1200 msg_handled:
1201 	vmbus_signal_eom(msg, message_type);
1202 }
1203 
1204 #ifdef CONFIG_PM_SLEEP
1205 /*
1206  * Fake RESCIND_CHANNEL messages to clean up hv_sock channels by force for
1207  * hibernation, because hv_sock connections can not persist across hibernation.
1208  */
1209 static void vmbus_force_channel_rescinded(struct vmbus_channel *channel)
1210 {
1211 	struct onmessage_work_context *ctx;
1212 	struct vmbus_channel_rescind_offer *rescind;
1213 
1214 	WARN_ON(!is_hvsock_channel(channel));
1215 
1216 	/*
1217 	 * Allocation size is small and the allocation should really not fail,
1218 	 * otherwise the state of the hv_sock connections ends up in limbo.
1219 	 */
1220 	ctx = kzalloc(sizeof(*ctx) + sizeof(*rescind),
1221 		      GFP_KERNEL | __GFP_NOFAIL);
1222 
1223 	/*
1224 	 * So far, these are not really used by Linux. Just set them to the
1225 	 * reasonable values conforming to the definitions of the fields.
1226 	 */
1227 	ctx->msg.header.message_type = 1;
1228 	ctx->msg.header.payload_size = sizeof(*rescind);
1229 
1230 	/* These values are actually used by Linux. */
1231 	rescind = (struct vmbus_channel_rescind_offer *)ctx->msg.payload;
1232 	rescind->header.msgtype = CHANNELMSG_RESCIND_CHANNELOFFER;
1233 	rescind->child_relid = channel->offermsg.child_relid;
1234 
1235 	INIT_WORK(&ctx->work, vmbus_onmessage_work);
1236 
1237 	queue_work(vmbus_connection.work_queue, &ctx->work);
1238 }
1239 #endif /* CONFIG_PM_SLEEP */
1240 
1241 /*
1242  * Schedule all channels with events pending
1243  */
1244 static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu)
1245 {
1246 	unsigned long *recv_int_page;
1247 	u32 maxbits, relid;
1248 
1249 	if (vmbus_proto_version < VERSION_WIN8) {
1250 		maxbits = MAX_NUM_CHANNELS_SUPPORTED;
1251 		recv_int_page = vmbus_connection.recv_int_page;
1252 	} else {
1253 		/*
1254 		 * When the host is win8 and beyond, the event page
1255 		 * can be directly checked to get the id of the channel
1256 		 * that has the interrupt pending.
1257 		 */
1258 		void *page_addr = hv_cpu->synic_event_page;
1259 		union hv_synic_event_flags *event
1260 			= (union hv_synic_event_flags *)page_addr +
1261 						 VMBUS_MESSAGE_SINT;
1262 
1263 		maxbits = HV_EVENT_FLAGS_COUNT;
1264 		recv_int_page = event->flags;
1265 	}
1266 
1267 	if (unlikely(!recv_int_page))
1268 		return;
1269 
1270 	for_each_set_bit(relid, recv_int_page, maxbits) {
1271 		void (*callback_fn)(void *context);
1272 		struct vmbus_channel *channel;
1273 
1274 		if (!sync_test_and_clear_bit(relid, recv_int_page))
1275 			continue;
1276 
1277 		/* Special case - vmbus channel protocol msg */
1278 		if (relid == 0)
1279 			continue;
1280 
1281 		/*
1282 		 * Pairs with the kfree_rcu() in vmbus_chan_release().
1283 		 * Guarantees that the channel data structure doesn't
1284 		 * get freed while the channel pointer below is being
1285 		 * dereferenced.
1286 		 */
1287 		rcu_read_lock();
1288 
1289 		/* Find channel based on relid */
1290 		channel = relid2channel(relid);
1291 		if (channel == NULL)
1292 			goto sched_unlock_rcu;
1293 
1294 		if (channel->rescind)
1295 			goto sched_unlock_rcu;
1296 
1297 		/*
1298 		 * Make sure that the ring buffer data structure doesn't get
1299 		 * freed while we dereference the ring buffer pointer.  Test
1300 		 * for the channel's onchannel_callback being NULL within a
1301 		 * sched_lock critical section.  See also the inline comments
1302 		 * in vmbus_reset_channel_cb().
1303 		 */
1304 		spin_lock(&channel->sched_lock);
1305 
1306 		callback_fn = channel->onchannel_callback;
1307 		if (unlikely(callback_fn == NULL))
1308 			goto sched_unlock;
1309 
1310 		trace_vmbus_chan_sched(channel);
1311 
1312 		++channel->interrupts;
1313 
1314 		switch (channel->callback_mode) {
1315 		case HV_CALL_ISR:
1316 			(*callback_fn)(channel->channel_callback_context);
1317 			break;
1318 
1319 		case HV_CALL_BATCHED:
1320 			hv_begin_read(&channel->inbound);
1321 			fallthrough;
1322 		case HV_CALL_DIRECT:
1323 			tasklet_schedule(&channel->callback_event);
1324 		}
1325 
1326 sched_unlock:
1327 		spin_unlock(&channel->sched_lock);
1328 sched_unlock_rcu:
1329 		rcu_read_unlock();
1330 	}
1331 }
1332 
1333 static void vmbus_isr(void)
1334 {
1335 	struct hv_per_cpu_context *hv_cpu
1336 		= this_cpu_ptr(hv_context.cpu_context);
1337 	void *page_addr = hv_cpu->synic_event_page;
1338 	struct hv_message *msg;
1339 	union hv_synic_event_flags *event;
1340 	bool handled = false;
1341 
1342 	if (unlikely(page_addr == NULL))
1343 		return;
1344 
1345 	event = (union hv_synic_event_flags *)page_addr +
1346 					 VMBUS_MESSAGE_SINT;
1347 	/*
1348 	 * Check for events before checking for messages. This is the order
1349 	 * in which events and messages are checked in Windows guests on
1350 	 * Hyper-V, and the Windows team suggested we do the same.
1351 	 */
1352 
1353 	if ((vmbus_proto_version == VERSION_WS2008) ||
1354 		(vmbus_proto_version == VERSION_WIN7)) {
1355 
1356 		/* Since we are a child, we only need to check bit 0 */
1357 		if (sync_test_and_clear_bit(0, event->flags))
1358 			handled = true;
1359 	} else {
1360 		/*
1361 		 * Our host is win8 or above. The signaling mechanism
1362 		 * has changed and we can directly look at the event page.
1363 		 * If bit n is set then we have an interrup on the channel
1364 		 * whose id is n.
1365 		 */
1366 		handled = true;
1367 	}
1368 
1369 	if (handled)
1370 		vmbus_chan_sched(hv_cpu);
1371 
1372 	page_addr = hv_cpu->synic_message_page;
1373 	msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
1374 
1375 	/* Check if there are actual msgs to be processed */
1376 	if (msg->header.message_type != HVMSG_NONE) {
1377 		if (msg->header.message_type == HVMSG_TIMER_EXPIRED) {
1378 			hv_stimer0_isr();
1379 			vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED);
1380 		} else
1381 			tasklet_schedule(&hv_cpu->msg_dpc);
1382 	}
1383 
1384 	add_interrupt_randomness(hv_get_vector(), 0);
1385 }
1386 
1387 /*
1388  * Callback from kmsg_dump. Grab as much as possible from the end of the kmsg
1389  * buffer and call into Hyper-V to transfer the data.
1390  */
1391 static void hv_kmsg_dump(struct kmsg_dumper *dumper,
1392 			 enum kmsg_dump_reason reason)
1393 {
1394 	size_t bytes_written;
1395 
1396 	/* We are only interested in panics. */
1397 	if ((reason != KMSG_DUMP_PANIC) || (!sysctl_record_panic_msg))
1398 		return;
1399 
1400 	/*
1401 	 * Write dump contents to the page. No need to synchronize; panic should
1402 	 * be single-threaded.
1403 	 */
1404 	kmsg_dump_get_buffer(dumper, false, hv_panic_page, HV_HYP_PAGE_SIZE,
1405 			     &bytes_written);
1406 	if (!bytes_written)
1407 		return;
1408 	/*
1409 	 * P3 to contain the physical address of the panic page & P4 to
1410 	 * contain the size of the panic data in that page. Rest of the
1411 	 * registers are no-op when the NOTIFY_MSG flag is set.
1412 	 */
1413 	hv_set_register(HV_REGISTER_CRASH_P0, 0);
1414 	hv_set_register(HV_REGISTER_CRASH_P1, 0);
1415 	hv_set_register(HV_REGISTER_CRASH_P2, 0);
1416 	hv_set_register(HV_REGISTER_CRASH_P3, virt_to_phys(hv_panic_page));
1417 	hv_set_register(HV_REGISTER_CRASH_P4, bytes_written);
1418 
1419 	/*
1420 	 * Let Hyper-V know there is crash data available along with
1421 	 * the panic message.
1422 	 */
1423 	hv_set_register(HV_REGISTER_CRASH_CTL,
1424 	       (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG));
1425 }
1426 
1427 static struct kmsg_dumper hv_kmsg_dumper = {
1428 	.dump = hv_kmsg_dump,
1429 };
1430 
1431 static void hv_kmsg_dump_register(void)
1432 {
1433 	int ret;
1434 
1435 	hv_panic_page = hv_alloc_hyperv_zeroed_page();
1436 	if (!hv_panic_page) {
1437 		pr_err("Hyper-V: panic message page memory allocation failed\n");
1438 		return;
1439 	}
1440 
1441 	ret = kmsg_dump_register(&hv_kmsg_dumper);
1442 	if (ret) {
1443 		pr_err("Hyper-V: kmsg dump register error 0x%x\n", ret);
1444 		hv_free_hyperv_page((unsigned long)hv_panic_page);
1445 		hv_panic_page = NULL;
1446 	}
1447 }
1448 
1449 static struct ctl_table_header *hv_ctl_table_hdr;
1450 
1451 /*
1452  * sysctl option to allow the user to control whether kmsg data should be
1453  * reported to Hyper-V on panic.
1454  */
1455 static struct ctl_table hv_ctl_table[] = {
1456 	{
1457 		.procname       = "hyperv_record_panic_msg",
1458 		.data           = &sysctl_record_panic_msg,
1459 		.maxlen         = sizeof(int),
1460 		.mode           = 0644,
1461 		.proc_handler   = proc_dointvec_minmax,
1462 		.extra1		= SYSCTL_ZERO,
1463 		.extra2		= SYSCTL_ONE
1464 	},
1465 	{}
1466 };
1467 
1468 static struct ctl_table hv_root_table[] = {
1469 	{
1470 		.procname	= "kernel",
1471 		.mode		= 0555,
1472 		.child		= hv_ctl_table
1473 	},
1474 	{}
1475 };
1476 
1477 /*
1478  * vmbus_bus_init -Main vmbus driver initialization routine.
1479  *
1480  * Here, we
1481  *	- initialize the vmbus driver context
1482  *	- invoke the vmbus hv main init routine
1483  *	- retrieve the channel offers
1484  */
1485 static int vmbus_bus_init(void)
1486 {
1487 	int ret;
1488 
1489 	ret = hv_init();
1490 	if (ret != 0) {
1491 		pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
1492 		return ret;
1493 	}
1494 
1495 	ret = bus_register(&hv_bus);
1496 	if (ret)
1497 		return ret;
1498 
1499 	ret = hv_setup_vmbus_irq(vmbus_irq, vmbus_isr);
1500 	if (ret)
1501 		goto err_setup;
1502 
1503 	ret = hv_synic_alloc();
1504 	if (ret)
1505 		goto err_alloc;
1506 
1507 	/*
1508 	 * Initialize the per-cpu interrupt state and stimer state.
1509 	 * Then connect to the host.
1510 	 */
1511 	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hyperv/vmbus:online",
1512 				hv_synic_init, hv_synic_cleanup);
1513 	if (ret < 0)
1514 		goto err_cpuhp;
1515 	hyperv_cpuhp_online = ret;
1516 
1517 	ret = vmbus_connect();
1518 	if (ret)
1519 		goto err_connect;
1520 
1521 	/*
1522 	 * Only register if the crash MSRs are available
1523 	 */
1524 	if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
1525 		u64 hyperv_crash_ctl;
1526 		/*
1527 		 * Sysctl registration is not fatal, since by default
1528 		 * reporting is enabled.
1529 		 */
1530 		hv_ctl_table_hdr = register_sysctl_table(hv_root_table);
1531 		if (!hv_ctl_table_hdr)
1532 			pr_err("Hyper-V: sysctl table register error");
1533 
1534 		/*
1535 		 * Register for panic kmsg callback only if the right
1536 		 * capability is supported by the hypervisor.
1537 		 */
1538 		hyperv_crash_ctl = hv_get_register(HV_REGISTER_CRASH_CTL);
1539 		if (hyperv_crash_ctl & HV_CRASH_CTL_CRASH_NOTIFY_MSG)
1540 			hv_kmsg_dump_register();
1541 
1542 		register_die_notifier(&hyperv_die_block);
1543 	}
1544 
1545 	/*
1546 	 * Always register the panic notifier because we need to unload
1547 	 * the VMbus channel connection to prevent any VMbus
1548 	 * activity after the VM panics.
1549 	 */
1550 	atomic_notifier_chain_register(&panic_notifier_list,
1551 			       &hyperv_panic_block);
1552 
1553 	vmbus_request_offers();
1554 
1555 	return 0;
1556 
1557 err_connect:
1558 	cpuhp_remove_state(hyperv_cpuhp_online);
1559 err_cpuhp:
1560 	hv_synic_free();
1561 err_alloc:
1562 	hv_remove_vmbus_irq();
1563 err_setup:
1564 	bus_unregister(&hv_bus);
1565 	unregister_sysctl_table(hv_ctl_table_hdr);
1566 	hv_ctl_table_hdr = NULL;
1567 	return ret;
1568 }
1569 
1570 /**
1571  * __vmbus_child_driver_register() - Register a vmbus's driver
1572  * @hv_driver: Pointer to driver structure you want to register
1573  * @owner: owner module of the drv
1574  * @mod_name: module name string
1575  *
1576  * Registers the given driver with Linux through the 'driver_register()' call
1577  * and sets up the hyper-v vmbus handling for this driver.
1578  * It will return the state of the 'driver_register()' call.
1579  *
1580  */
1581 int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
1582 {
1583 	int ret;
1584 
1585 	pr_info("registering driver %s\n", hv_driver->name);
1586 
1587 	ret = vmbus_exists();
1588 	if (ret < 0)
1589 		return ret;
1590 
1591 	hv_driver->driver.name = hv_driver->name;
1592 	hv_driver->driver.owner = owner;
1593 	hv_driver->driver.mod_name = mod_name;
1594 	hv_driver->driver.bus = &hv_bus;
1595 
1596 	spin_lock_init(&hv_driver->dynids.lock);
1597 	INIT_LIST_HEAD(&hv_driver->dynids.list);
1598 
1599 	ret = driver_register(&hv_driver->driver);
1600 
1601 	return ret;
1602 }
1603 EXPORT_SYMBOL_GPL(__vmbus_driver_register);
1604 
1605 /**
1606  * vmbus_driver_unregister() - Unregister a vmbus's driver
1607  * @hv_driver: Pointer to driver structure you want to
1608  *             un-register
1609  *
1610  * Un-register the given driver that was previous registered with a call to
1611  * vmbus_driver_register()
1612  */
1613 void vmbus_driver_unregister(struct hv_driver *hv_driver)
1614 {
1615 	pr_info("unregistering driver %s\n", hv_driver->name);
1616 
1617 	if (!vmbus_exists()) {
1618 		driver_unregister(&hv_driver->driver);
1619 		vmbus_free_dynids(hv_driver);
1620 	}
1621 }
1622 EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
1623 
1624 
1625 /*
1626  * Called when last reference to channel is gone.
1627  */
1628 static void vmbus_chan_release(struct kobject *kobj)
1629 {
1630 	struct vmbus_channel *channel
1631 		= container_of(kobj, struct vmbus_channel, kobj);
1632 
1633 	kfree_rcu(channel, rcu);
1634 }
1635 
1636 struct vmbus_chan_attribute {
1637 	struct attribute attr;
1638 	ssize_t (*show)(struct vmbus_channel *chan, char *buf);
1639 	ssize_t (*store)(struct vmbus_channel *chan,
1640 			 const char *buf, size_t count);
1641 };
1642 #define VMBUS_CHAN_ATTR(_name, _mode, _show, _store) \
1643 	struct vmbus_chan_attribute chan_attr_##_name \
1644 		= __ATTR(_name, _mode, _show, _store)
1645 #define VMBUS_CHAN_ATTR_RW(_name) \
1646 	struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RW(_name)
1647 #define VMBUS_CHAN_ATTR_RO(_name) \
1648 	struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RO(_name)
1649 #define VMBUS_CHAN_ATTR_WO(_name) \
1650 	struct vmbus_chan_attribute chan_attr_##_name = __ATTR_WO(_name)
1651 
1652 static ssize_t vmbus_chan_attr_show(struct kobject *kobj,
1653 				    struct attribute *attr, char *buf)
1654 {
1655 	const struct vmbus_chan_attribute *attribute
1656 		= container_of(attr, struct vmbus_chan_attribute, attr);
1657 	struct vmbus_channel *chan
1658 		= container_of(kobj, struct vmbus_channel, kobj);
1659 
1660 	if (!attribute->show)
1661 		return -EIO;
1662 
1663 	return attribute->show(chan, buf);
1664 }
1665 
1666 static ssize_t vmbus_chan_attr_store(struct kobject *kobj,
1667 				     struct attribute *attr, const char *buf,
1668 				     size_t count)
1669 {
1670 	const struct vmbus_chan_attribute *attribute
1671 		= container_of(attr, struct vmbus_chan_attribute, attr);
1672 	struct vmbus_channel *chan
1673 		= container_of(kobj, struct vmbus_channel, kobj);
1674 
1675 	if (!attribute->store)
1676 		return -EIO;
1677 
1678 	return attribute->store(chan, buf, count);
1679 }
1680 
1681 static const struct sysfs_ops vmbus_chan_sysfs_ops = {
1682 	.show = vmbus_chan_attr_show,
1683 	.store = vmbus_chan_attr_store,
1684 };
1685 
1686 static ssize_t out_mask_show(struct vmbus_channel *channel, char *buf)
1687 {
1688 	struct hv_ring_buffer_info *rbi = &channel->outbound;
1689 	ssize_t ret;
1690 
1691 	mutex_lock(&rbi->ring_buffer_mutex);
1692 	if (!rbi->ring_buffer) {
1693 		mutex_unlock(&rbi->ring_buffer_mutex);
1694 		return -EINVAL;
1695 	}
1696 
1697 	ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1698 	mutex_unlock(&rbi->ring_buffer_mutex);
1699 	return ret;
1700 }
1701 static VMBUS_CHAN_ATTR_RO(out_mask);
1702 
1703 static ssize_t in_mask_show(struct vmbus_channel *channel, char *buf)
1704 {
1705 	struct hv_ring_buffer_info *rbi = &channel->inbound;
1706 	ssize_t ret;
1707 
1708 	mutex_lock(&rbi->ring_buffer_mutex);
1709 	if (!rbi->ring_buffer) {
1710 		mutex_unlock(&rbi->ring_buffer_mutex);
1711 		return -EINVAL;
1712 	}
1713 
1714 	ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1715 	mutex_unlock(&rbi->ring_buffer_mutex);
1716 	return ret;
1717 }
1718 static VMBUS_CHAN_ATTR_RO(in_mask);
1719 
1720 static ssize_t read_avail_show(struct vmbus_channel *channel, char *buf)
1721 {
1722 	struct hv_ring_buffer_info *rbi = &channel->inbound;
1723 	ssize_t ret;
1724 
1725 	mutex_lock(&rbi->ring_buffer_mutex);
1726 	if (!rbi->ring_buffer) {
1727 		mutex_unlock(&rbi->ring_buffer_mutex);
1728 		return -EINVAL;
1729 	}
1730 
1731 	ret = sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi));
1732 	mutex_unlock(&rbi->ring_buffer_mutex);
1733 	return ret;
1734 }
1735 static VMBUS_CHAN_ATTR_RO(read_avail);
1736 
1737 static ssize_t write_avail_show(struct vmbus_channel *channel, char *buf)
1738 {
1739 	struct hv_ring_buffer_info *rbi = &channel->outbound;
1740 	ssize_t ret;
1741 
1742 	mutex_lock(&rbi->ring_buffer_mutex);
1743 	if (!rbi->ring_buffer) {
1744 		mutex_unlock(&rbi->ring_buffer_mutex);
1745 		return -EINVAL;
1746 	}
1747 
1748 	ret = sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi));
1749 	mutex_unlock(&rbi->ring_buffer_mutex);
1750 	return ret;
1751 }
1752 static VMBUS_CHAN_ATTR_RO(write_avail);
1753 
1754 static ssize_t target_cpu_show(struct vmbus_channel *channel, char *buf)
1755 {
1756 	return sprintf(buf, "%u\n", channel->target_cpu);
1757 }
1758 static ssize_t target_cpu_store(struct vmbus_channel *channel,
1759 				const char *buf, size_t count)
1760 {
1761 	u32 target_cpu, origin_cpu;
1762 	ssize_t ret = count;
1763 
1764 	if (vmbus_proto_version < VERSION_WIN10_V4_1)
1765 		return -EIO;
1766 
1767 	if (sscanf(buf, "%uu", &target_cpu) != 1)
1768 		return -EIO;
1769 
1770 	/* Validate target_cpu for the cpumask_test_cpu() operation below. */
1771 	if (target_cpu >= nr_cpumask_bits)
1772 		return -EINVAL;
1773 
1774 	/* No CPUs should come up or down during this. */
1775 	cpus_read_lock();
1776 
1777 	if (!cpu_online(target_cpu)) {
1778 		cpus_read_unlock();
1779 		return -EINVAL;
1780 	}
1781 
1782 	/*
1783 	 * Synchronizes target_cpu_store() and channel closure:
1784 	 *
1785 	 * { Initially: state = CHANNEL_OPENED }
1786 	 *
1787 	 * CPU1				CPU2
1788 	 *
1789 	 * [target_cpu_store()]		[vmbus_disconnect_ring()]
1790 	 *
1791 	 * LOCK channel_mutex		LOCK channel_mutex
1792 	 * LOAD r1 = state		LOAD r2 = state
1793 	 * IF (r1 == CHANNEL_OPENED)	IF (r2 == CHANNEL_OPENED)
1794 	 *   SEND MODIFYCHANNEL		  STORE state = CHANNEL_OPEN
1795 	 *   [...]			  SEND CLOSECHANNEL
1796 	 * UNLOCK channel_mutex		UNLOCK channel_mutex
1797 	 *
1798 	 * Forbids: r1 == r2 == CHANNEL_OPENED (i.e., CPU1's LOCK precedes
1799 	 * 		CPU2's LOCK) && CPU2's SEND precedes CPU1's SEND
1800 	 *
1801 	 * Note.  The host processes the channel messages "sequentially", in
1802 	 * the order in which they are received on a per-partition basis.
1803 	 */
1804 	mutex_lock(&vmbus_connection.channel_mutex);
1805 
1806 	/*
1807 	 * Hyper-V will ignore MODIFYCHANNEL messages for "non-open" channels;
1808 	 * avoid sending the message and fail here for such channels.
1809 	 */
1810 	if (channel->state != CHANNEL_OPENED_STATE) {
1811 		ret = -EIO;
1812 		goto cpu_store_unlock;
1813 	}
1814 
1815 	origin_cpu = channel->target_cpu;
1816 	if (target_cpu == origin_cpu)
1817 		goto cpu_store_unlock;
1818 
1819 	if (vmbus_send_modifychannel(channel->offermsg.child_relid,
1820 				     hv_cpu_number_to_vp_number(target_cpu))) {
1821 		ret = -EIO;
1822 		goto cpu_store_unlock;
1823 	}
1824 
1825 	/*
1826 	 * Warning.  At this point, there is *no* guarantee that the host will
1827 	 * have successfully processed the vmbus_send_modifychannel() request.
1828 	 * See the header comment of vmbus_send_modifychannel() for more info.
1829 	 *
1830 	 * Lags in the processing of the above vmbus_send_modifychannel() can
1831 	 * result in missed interrupts if the "old" target CPU is taken offline
1832 	 * before Hyper-V starts sending interrupts to the "new" target CPU.
1833 	 * But apart from this offlining scenario, the code tolerates such
1834 	 * lags.  It will function correctly even if a channel interrupt comes
1835 	 * in on a CPU that is different from the channel target_cpu value.
1836 	 */
1837 
1838 	channel->target_cpu = target_cpu;
1839 
1840 	/* See init_vp_index(). */
1841 	if (hv_is_perf_channel(channel))
1842 		hv_update_alloced_cpus(origin_cpu, target_cpu);
1843 
1844 	/* Currently set only for storvsc channels. */
1845 	if (channel->change_target_cpu_callback) {
1846 		(*channel->change_target_cpu_callback)(channel,
1847 				origin_cpu, target_cpu);
1848 	}
1849 
1850 cpu_store_unlock:
1851 	mutex_unlock(&vmbus_connection.channel_mutex);
1852 	cpus_read_unlock();
1853 	return ret;
1854 }
1855 static VMBUS_CHAN_ATTR(cpu, 0644, target_cpu_show, target_cpu_store);
1856 
1857 static ssize_t channel_pending_show(struct vmbus_channel *channel,
1858 				    char *buf)
1859 {
1860 	return sprintf(buf, "%d\n",
1861 		       channel_pending(channel,
1862 				       vmbus_connection.monitor_pages[1]));
1863 }
1864 static VMBUS_CHAN_ATTR(pending, 0444, channel_pending_show, NULL);
1865 
1866 static ssize_t channel_latency_show(struct vmbus_channel *channel,
1867 				    char *buf)
1868 {
1869 	return sprintf(buf, "%d\n",
1870 		       channel_latency(channel,
1871 				       vmbus_connection.monitor_pages[1]));
1872 }
1873 static VMBUS_CHAN_ATTR(latency, 0444, channel_latency_show, NULL);
1874 
1875 static ssize_t channel_interrupts_show(struct vmbus_channel *channel, char *buf)
1876 {
1877 	return sprintf(buf, "%llu\n", channel->interrupts);
1878 }
1879 static VMBUS_CHAN_ATTR(interrupts, 0444, channel_interrupts_show, NULL);
1880 
1881 static ssize_t channel_events_show(struct vmbus_channel *channel, char *buf)
1882 {
1883 	return sprintf(buf, "%llu\n", channel->sig_events);
1884 }
1885 static VMBUS_CHAN_ATTR(events, 0444, channel_events_show, NULL);
1886 
1887 static ssize_t channel_intr_in_full_show(struct vmbus_channel *channel,
1888 					 char *buf)
1889 {
1890 	return sprintf(buf, "%llu\n",
1891 		       (unsigned long long)channel->intr_in_full);
1892 }
1893 static VMBUS_CHAN_ATTR(intr_in_full, 0444, channel_intr_in_full_show, NULL);
1894 
1895 static ssize_t channel_intr_out_empty_show(struct vmbus_channel *channel,
1896 					   char *buf)
1897 {
1898 	return sprintf(buf, "%llu\n",
1899 		       (unsigned long long)channel->intr_out_empty);
1900 }
1901 static VMBUS_CHAN_ATTR(intr_out_empty, 0444, channel_intr_out_empty_show, NULL);
1902 
1903 static ssize_t channel_out_full_first_show(struct vmbus_channel *channel,
1904 					   char *buf)
1905 {
1906 	return sprintf(buf, "%llu\n",
1907 		       (unsigned long long)channel->out_full_first);
1908 }
1909 static VMBUS_CHAN_ATTR(out_full_first, 0444, channel_out_full_first_show, NULL);
1910 
1911 static ssize_t channel_out_full_total_show(struct vmbus_channel *channel,
1912 					   char *buf)
1913 {
1914 	return sprintf(buf, "%llu\n",
1915 		       (unsigned long long)channel->out_full_total);
1916 }
1917 static VMBUS_CHAN_ATTR(out_full_total, 0444, channel_out_full_total_show, NULL);
1918 
1919 static ssize_t subchannel_monitor_id_show(struct vmbus_channel *channel,
1920 					  char *buf)
1921 {
1922 	return sprintf(buf, "%u\n", channel->offermsg.monitorid);
1923 }
1924 static VMBUS_CHAN_ATTR(monitor_id, 0444, subchannel_monitor_id_show, NULL);
1925 
1926 static ssize_t subchannel_id_show(struct vmbus_channel *channel,
1927 				  char *buf)
1928 {
1929 	return sprintf(buf, "%u\n",
1930 		       channel->offermsg.offer.sub_channel_index);
1931 }
1932 static VMBUS_CHAN_ATTR_RO(subchannel_id);
1933 
1934 static struct attribute *vmbus_chan_attrs[] = {
1935 	&chan_attr_out_mask.attr,
1936 	&chan_attr_in_mask.attr,
1937 	&chan_attr_read_avail.attr,
1938 	&chan_attr_write_avail.attr,
1939 	&chan_attr_cpu.attr,
1940 	&chan_attr_pending.attr,
1941 	&chan_attr_latency.attr,
1942 	&chan_attr_interrupts.attr,
1943 	&chan_attr_events.attr,
1944 	&chan_attr_intr_in_full.attr,
1945 	&chan_attr_intr_out_empty.attr,
1946 	&chan_attr_out_full_first.attr,
1947 	&chan_attr_out_full_total.attr,
1948 	&chan_attr_monitor_id.attr,
1949 	&chan_attr_subchannel_id.attr,
1950 	NULL
1951 };
1952 
1953 /*
1954  * Channel-level attribute_group callback function. Returns the permission for
1955  * each attribute, and returns 0 if an attribute is not visible.
1956  */
1957 static umode_t vmbus_chan_attr_is_visible(struct kobject *kobj,
1958 					  struct attribute *attr, int idx)
1959 {
1960 	const struct vmbus_channel *channel =
1961 		container_of(kobj, struct vmbus_channel, kobj);
1962 
1963 	/* Hide the monitor attributes if the monitor mechanism is not used. */
1964 	if (!channel->offermsg.monitor_allocated &&
1965 	    (attr == &chan_attr_pending.attr ||
1966 	     attr == &chan_attr_latency.attr ||
1967 	     attr == &chan_attr_monitor_id.attr))
1968 		return 0;
1969 
1970 	return attr->mode;
1971 }
1972 
1973 static struct attribute_group vmbus_chan_group = {
1974 	.attrs = vmbus_chan_attrs,
1975 	.is_visible = vmbus_chan_attr_is_visible
1976 };
1977 
1978 static struct kobj_type vmbus_chan_ktype = {
1979 	.sysfs_ops = &vmbus_chan_sysfs_ops,
1980 	.release = vmbus_chan_release,
1981 };
1982 
1983 /*
1984  * vmbus_add_channel_kobj - setup a sub-directory under device/channels
1985  */
1986 int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
1987 {
1988 	const struct device *device = &dev->device;
1989 	struct kobject *kobj = &channel->kobj;
1990 	u32 relid = channel->offermsg.child_relid;
1991 	int ret;
1992 
1993 	kobj->kset = dev->channels_kset;
1994 	ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
1995 				   "%u", relid);
1996 	if (ret)
1997 		return ret;
1998 
1999 	ret = sysfs_create_group(kobj, &vmbus_chan_group);
2000 
2001 	if (ret) {
2002 		/*
2003 		 * The calling functions' error handling paths will cleanup the
2004 		 * empty channel directory.
2005 		 */
2006 		dev_err(device, "Unable to set up channel sysfs files\n");
2007 		return ret;
2008 	}
2009 
2010 	kobject_uevent(kobj, KOBJ_ADD);
2011 
2012 	return 0;
2013 }
2014 
2015 /*
2016  * vmbus_remove_channel_attr_group - remove the channel's attribute group
2017  */
2018 void vmbus_remove_channel_attr_group(struct vmbus_channel *channel)
2019 {
2020 	sysfs_remove_group(&channel->kobj, &vmbus_chan_group);
2021 }
2022 
2023 /*
2024  * vmbus_device_create - Creates and registers a new child device
2025  * on the vmbus.
2026  */
2027 struct hv_device *vmbus_device_create(const guid_t *type,
2028 				      const guid_t *instance,
2029 				      struct vmbus_channel *channel)
2030 {
2031 	struct hv_device *child_device_obj;
2032 
2033 	child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
2034 	if (!child_device_obj) {
2035 		pr_err("Unable to allocate device object for child device\n");
2036 		return NULL;
2037 	}
2038 
2039 	child_device_obj->channel = channel;
2040 	guid_copy(&child_device_obj->dev_type, type);
2041 	guid_copy(&child_device_obj->dev_instance, instance);
2042 	child_device_obj->vendor_id = 0x1414; /* MSFT vendor ID */
2043 
2044 	return child_device_obj;
2045 }
2046 
2047 /*
2048  * vmbus_device_register - Register the child device
2049  */
2050 int vmbus_device_register(struct hv_device *child_device_obj)
2051 {
2052 	struct kobject *kobj = &child_device_obj->device.kobj;
2053 	int ret;
2054 
2055 	dev_set_name(&child_device_obj->device, "%pUl",
2056 		     &child_device_obj->channel->offermsg.offer.if_instance);
2057 
2058 	child_device_obj->device.bus = &hv_bus;
2059 	child_device_obj->device.parent = &hv_acpi_dev->dev;
2060 	child_device_obj->device.release = vmbus_device_release;
2061 
2062 	/*
2063 	 * Register with the LDM. This will kick off the driver/device
2064 	 * binding...which will eventually call vmbus_match() and vmbus_probe()
2065 	 */
2066 	ret = device_register(&child_device_obj->device);
2067 	if (ret) {
2068 		pr_err("Unable to register child device\n");
2069 		return ret;
2070 	}
2071 
2072 	child_device_obj->channels_kset = kset_create_and_add("channels",
2073 							      NULL, kobj);
2074 	if (!child_device_obj->channels_kset) {
2075 		ret = -ENOMEM;
2076 		goto err_dev_unregister;
2077 	}
2078 
2079 	ret = vmbus_add_channel_kobj(child_device_obj,
2080 				     child_device_obj->channel);
2081 	if (ret) {
2082 		pr_err("Unable to register primary channeln");
2083 		goto err_kset_unregister;
2084 	}
2085 	hv_debug_add_dev_dir(child_device_obj);
2086 
2087 	return 0;
2088 
2089 err_kset_unregister:
2090 	kset_unregister(child_device_obj->channels_kset);
2091 
2092 err_dev_unregister:
2093 	device_unregister(&child_device_obj->device);
2094 	return ret;
2095 }
2096 
2097 /*
2098  * vmbus_device_unregister - Remove the specified child device
2099  * from the vmbus.
2100  */
2101 void vmbus_device_unregister(struct hv_device *device_obj)
2102 {
2103 	pr_debug("child device %s unregistered\n",
2104 		dev_name(&device_obj->device));
2105 
2106 	kset_unregister(device_obj->channels_kset);
2107 
2108 	/*
2109 	 * Kick off the process of unregistering the device.
2110 	 * This will call vmbus_remove() and eventually vmbus_device_release()
2111 	 */
2112 	device_unregister(&device_obj->device);
2113 }
2114 
2115 
2116 /*
2117  * VMBUS is an acpi enumerated device. Get the information we
2118  * need from DSDT.
2119  */
2120 #define VTPM_BASE_ADDRESS 0xfed40000
2121 static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
2122 {
2123 	resource_size_t start = 0;
2124 	resource_size_t end = 0;
2125 	struct resource *new_res;
2126 	struct resource **old_res = &hyperv_mmio;
2127 	struct resource **prev_res = NULL;
2128 	struct resource r;
2129 
2130 	switch (res->type) {
2131 
2132 	/*
2133 	 * "Address" descriptors are for bus windows. Ignore
2134 	 * "memory" descriptors, which are for registers on
2135 	 * devices.
2136 	 */
2137 	case ACPI_RESOURCE_TYPE_ADDRESS32:
2138 		start = res->data.address32.address.minimum;
2139 		end = res->data.address32.address.maximum;
2140 		break;
2141 
2142 	case ACPI_RESOURCE_TYPE_ADDRESS64:
2143 		start = res->data.address64.address.minimum;
2144 		end = res->data.address64.address.maximum;
2145 		break;
2146 
2147 	/*
2148 	 * The IRQ information is needed only on ARM64, which Hyper-V
2149 	 * sets up in the extended format. IRQ information is present
2150 	 * on x86/x64 in the non-extended format but it is not used by
2151 	 * Linux. So don't bother checking for the non-extended format.
2152 	 */
2153 	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
2154 		if (!acpi_dev_resource_interrupt(res, 0, &r)) {
2155 			pr_err("Unable to parse Hyper-V ACPI interrupt\n");
2156 			return AE_ERROR;
2157 		}
2158 		/* ARM64 INTID for VMbus */
2159 		vmbus_interrupt = res->data.extended_irq.interrupts[0];
2160 		/* Linux IRQ number */
2161 		vmbus_irq = r.start;
2162 		return AE_OK;
2163 
2164 	default:
2165 		/* Unused resource type */
2166 		return AE_OK;
2167 
2168 	}
2169 	/*
2170 	 * Ignore ranges that are below 1MB, as they're not
2171 	 * necessary or useful here.
2172 	 */
2173 	if (end < 0x100000)
2174 		return AE_OK;
2175 
2176 	new_res = kzalloc(sizeof(*new_res), GFP_ATOMIC);
2177 	if (!new_res)
2178 		return AE_NO_MEMORY;
2179 
2180 	/* If this range overlaps the virtual TPM, truncate it. */
2181 	if (end > VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS)
2182 		end = VTPM_BASE_ADDRESS;
2183 
2184 	new_res->name = "hyperv mmio";
2185 	new_res->flags = IORESOURCE_MEM;
2186 	new_res->start = start;
2187 	new_res->end = end;
2188 
2189 	/*
2190 	 * If two ranges are adjacent, merge them.
2191 	 */
2192 	do {
2193 		if (!*old_res) {
2194 			*old_res = new_res;
2195 			break;
2196 		}
2197 
2198 		if (((*old_res)->end + 1) == new_res->start) {
2199 			(*old_res)->end = new_res->end;
2200 			kfree(new_res);
2201 			break;
2202 		}
2203 
2204 		if ((*old_res)->start == new_res->end + 1) {
2205 			(*old_res)->start = new_res->start;
2206 			kfree(new_res);
2207 			break;
2208 		}
2209 
2210 		if ((*old_res)->start > new_res->end) {
2211 			new_res->sibling = *old_res;
2212 			if (prev_res)
2213 				(*prev_res)->sibling = new_res;
2214 			*old_res = new_res;
2215 			break;
2216 		}
2217 
2218 		prev_res = old_res;
2219 		old_res = &(*old_res)->sibling;
2220 
2221 	} while (1);
2222 
2223 	return AE_OK;
2224 }
2225 
2226 static int vmbus_acpi_remove(struct acpi_device *device)
2227 {
2228 	struct resource *cur_res;
2229 	struct resource *next_res;
2230 
2231 	if (hyperv_mmio) {
2232 		if (fb_mmio) {
2233 			__release_region(hyperv_mmio, fb_mmio->start,
2234 					 resource_size(fb_mmio));
2235 			fb_mmio = NULL;
2236 		}
2237 
2238 		for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) {
2239 			next_res = cur_res->sibling;
2240 			kfree(cur_res);
2241 		}
2242 	}
2243 
2244 	return 0;
2245 }
2246 
2247 static void vmbus_reserve_fb(void)
2248 {
2249 	int size;
2250 	/*
2251 	 * Make a claim for the frame buffer in the resource tree under the
2252 	 * first node, which will be the one below 4GB.  The length seems to
2253 	 * be underreported, particularly in a Generation 1 VM.  So start out
2254 	 * reserving a larger area and make it smaller until it succeeds.
2255 	 */
2256 
2257 	if (screen_info.lfb_base) {
2258 		if (efi_enabled(EFI_BOOT))
2259 			size = max_t(__u32, screen_info.lfb_size, 0x800000);
2260 		else
2261 			size = max_t(__u32, screen_info.lfb_size, 0x4000000);
2262 
2263 		for (; !fb_mmio && (size >= 0x100000); size >>= 1) {
2264 			fb_mmio = __request_region(hyperv_mmio,
2265 						   screen_info.lfb_base, size,
2266 						   fb_mmio_name, 0);
2267 		}
2268 	}
2269 }
2270 
2271 /**
2272  * vmbus_allocate_mmio() - Pick a memory-mapped I/O range.
2273  * @new:		If successful, supplied a pointer to the
2274  *			allocated MMIO space.
2275  * @device_obj:		Identifies the caller
2276  * @min:		Minimum guest physical address of the
2277  *			allocation
2278  * @max:		Maximum guest physical address
2279  * @size:		Size of the range to be allocated
2280  * @align:		Alignment of the range to be allocated
2281  * @fb_overlap_ok:	Whether this allocation can be allowed
2282  *			to overlap the video frame buffer.
2283  *
2284  * This function walks the resources granted to VMBus by the
2285  * _CRS object in the ACPI namespace underneath the parent
2286  * "bridge" whether that's a root PCI bus in the Generation 1
2287  * case or a Module Device in the Generation 2 case.  It then
2288  * attempts to allocate from the global MMIO pool in a way that
2289  * matches the constraints supplied in these parameters and by
2290  * that _CRS.
2291  *
2292  * Return: 0 on success, -errno on failure
2293  */
2294 int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
2295 			resource_size_t min, resource_size_t max,
2296 			resource_size_t size, resource_size_t align,
2297 			bool fb_overlap_ok)
2298 {
2299 	struct resource *iter, *shadow;
2300 	resource_size_t range_min, range_max, start;
2301 	const char *dev_n = dev_name(&device_obj->device);
2302 	int retval;
2303 
2304 	retval = -ENXIO;
2305 	mutex_lock(&hyperv_mmio_lock);
2306 
2307 	/*
2308 	 * If overlaps with frame buffers are allowed, then first attempt to
2309 	 * make the allocation from within the reserved region.  Because it
2310 	 * is already reserved, no shadow allocation is necessary.
2311 	 */
2312 	if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) &&
2313 	    !(max < fb_mmio->start)) {
2314 
2315 		range_min = fb_mmio->start;
2316 		range_max = fb_mmio->end;
2317 		start = (range_min + align - 1) & ~(align - 1);
2318 		for (; start + size - 1 <= range_max; start += align) {
2319 			*new = request_mem_region_exclusive(start, size, dev_n);
2320 			if (*new) {
2321 				retval = 0;
2322 				goto exit;
2323 			}
2324 		}
2325 	}
2326 
2327 	for (iter = hyperv_mmio; iter; iter = iter->sibling) {
2328 		if ((iter->start >= max) || (iter->end <= min))
2329 			continue;
2330 
2331 		range_min = iter->start;
2332 		range_max = iter->end;
2333 		start = (range_min + align - 1) & ~(align - 1);
2334 		for (; start + size - 1 <= range_max; start += align) {
2335 			shadow = __request_region(iter, start, size, NULL,
2336 						  IORESOURCE_BUSY);
2337 			if (!shadow)
2338 				continue;
2339 
2340 			*new = request_mem_region_exclusive(start, size, dev_n);
2341 			if (*new) {
2342 				shadow->name = (char *)*new;
2343 				retval = 0;
2344 				goto exit;
2345 			}
2346 
2347 			__release_region(iter, start, size);
2348 		}
2349 	}
2350 
2351 exit:
2352 	mutex_unlock(&hyperv_mmio_lock);
2353 	return retval;
2354 }
2355 EXPORT_SYMBOL_GPL(vmbus_allocate_mmio);
2356 
2357 /**
2358  * vmbus_free_mmio() - Free a memory-mapped I/O range.
2359  * @start:		Base address of region to release.
2360  * @size:		Size of the range to be allocated
2361  *
2362  * This function releases anything requested by
2363  * vmbus_mmio_allocate().
2364  */
2365 void vmbus_free_mmio(resource_size_t start, resource_size_t size)
2366 {
2367 	struct resource *iter;
2368 
2369 	mutex_lock(&hyperv_mmio_lock);
2370 	for (iter = hyperv_mmio; iter; iter = iter->sibling) {
2371 		if ((iter->start >= start + size) || (iter->end <= start))
2372 			continue;
2373 
2374 		__release_region(iter, start, size);
2375 	}
2376 	release_mem_region(start, size);
2377 	mutex_unlock(&hyperv_mmio_lock);
2378 
2379 }
2380 EXPORT_SYMBOL_GPL(vmbus_free_mmio);
2381 
2382 static int vmbus_acpi_add(struct acpi_device *device)
2383 {
2384 	acpi_status result;
2385 	int ret_val = -ENODEV;
2386 	struct acpi_device *ancestor;
2387 
2388 	hv_acpi_dev = device;
2389 
2390 	result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
2391 					vmbus_walk_resources, NULL);
2392 
2393 	if (ACPI_FAILURE(result))
2394 		goto acpi_walk_err;
2395 	/*
2396 	 * Some ancestor of the vmbus acpi device (Gen1 or Gen2
2397 	 * firmware) is the VMOD that has the mmio ranges. Get that.
2398 	 */
2399 	for (ancestor = device->parent; ancestor; ancestor = ancestor->parent) {
2400 		result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS,
2401 					     vmbus_walk_resources, NULL);
2402 
2403 		if (ACPI_FAILURE(result))
2404 			continue;
2405 		if (hyperv_mmio) {
2406 			vmbus_reserve_fb();
2407 			break;
2408 		}
2409 	}
2410 	ret_val = 0;
2411 
2412 acpi_walk_err:
2413 	complete(&probe_event);
2414 	if (ret_val)
2415 		vmbus_acpi_remove(device);
2416 	return ret_val;
2417 }
2418 
2419 #ifdef CONFIG_PM_SLEEP
2420 static int vmbus_bus_suspend(struct device *dev)
2421 {
2422 	struct vmbus_channel *channel, *sc;
2423 
2424 	while (atomic_read(&vmbus_connection.offer_in_progress) != 0) {
2425 		/*
2426 		 * We wait here until the completion of any channel
2427 		 * offers that are currently in progress.
2428 		 */
2429 		usleep_range(1000, 2000);
2430 	}
2431 
2432 	mutex_lock(&vmbus_connection.channel_mutex);
2433 	list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
2434 		if (!is_hvsock_channel(channel))
2435 			continue;
2436 
2437 		vmbus_force_channel_rescinded(channel);
2438 	}
2439 	mutex_unlock(&vmbus_connection.channel_mutex);
2440 
2441 	/*
2442 	 * Wait until all the sub-channels and hv_sock channels have been
2443 	 * cleaned up. Sub-channels should be destroyed upon suspend, otherwise
2444 	 * they would conflict with the new sub-channels that will be created
2445 	 * in the resume path. hv_sock channels should also be destroyed, but
2446 	 * a hv_sock channel of an established hv_sock connection can not be
2447 	 * really destroyed since it may still be referenced by the userspace
2448 	 * application, so we just force the hv_sock channel to be rescinded
2449 	 * by vmbus_force_channel_rescinded(), and the userspace application
2450 	 * will thoroughly destroy the channel after hibernation.
2451 	 *
2452 	 * Note: the counter nr_chan_close_on_suspend may never go above 0 if
2453 	 * the VM has no sub-channel and hv_sock channel, e.g. a 1-vCPU VM.
2454 	 */
2455 	if (atomic_read(&vmbus_connection.nr_chan_close_on_suspend) > 0)
2456 		wait_for_completion(&vmbus_connection.ready_for_suspend_event);
2457 
2458 	if (atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) != 0) {
2459 		pr_err("Can not suspend due to a previous failed resuming\n");
2460 		return -EBUSY;
2461 	}
2462 
2463 	mutex_lock(&vmbus_connection.channel_mutex);
2464 
2465 	list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
2466 		/*
2467 		 * Remove the channel from the array of channels and invalidate
2468 		 * the channel's relid.  Upon resume, vmbus_onoffer() will fix
2469 		 * up the relid (and other fields, if necessary) and add the
2470 		 * channel back to the array.
2471 		 */
2472 		vmbus_channel_unmap_relid(channel);
2473 		channel->offermsg.child_relid = INVALID_RELID;
2474 
2475 		if (is_hvsock_channel(channel)) {
2476 			if (!channel->rescind) {
2477 				pr_err("hv_sock channel not rescinded!\n");
2478 				WARN_ON_ONCE(1);
2479 			}
2480 			continue;
2481 		}
2482 
2483 		list_for_each_entry(sc, &channel->sc_list, sc_list) {
2484 			pr_err("Sub-channel not deleted!\n");
2485 			WARN_ON_ONCE(1);
2486 		}
2487 
2488 		atomic_inc(&vmbus_connection.nr_chan_fixup_on_resume);
2489 	}
2490 
2491 	mutex_unlock(&vmbus_connection.channel_mutex);
2492 
2493 	vmbus_initiate_unload(false);
2494 
2495 	/* Reset the event for the next resume. */
2496 	reinit_completion(&vmbus_connection.ready_for_resume_event);
2497 
2498 	return 0;
2499 }
2500 
2501 static int vmbus_bus_resume(struct device *dev)
2502 {
2503 	struct vmbus_channel_msginfo *msginfo;
2504 	size_t msgsize;
2505 	int ret;
2506 
2507 	/*
2508 	 * We only use the 'vmbus_proto_version', which was in use before
2509 	 * hibernation, to re-negotiate with the host.
2510 	 */
2511 	if (!vmbus_proto_version) {
2512 		pr_err("Invalid proto version = 0x%x\n", vmbus_proto_version);
2513 		return -EINVAL;
2514 	}
2515 
2516 	msgsize = sizeof(*msginfo) +
2517 		  sizeof(struct vmbus_channel_initiate_contact);
2518 
2519 	msginfo = kzalloc(msgsize, GFP_KERNEL);
2520 
2521 	if (msginfo == NULL)
2522 		return -ENOMEM;
2523 
2524 	ret = vmbus_negotiate_version(msginfo, vmbus_proto_version);
2525 
2526 	kfree(msginfo);
2527 
2528 	if (ret != 0)
2529 		return ret;
2530 
2531 	WARN_ON(atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) == 0);
2532 
2533 	vmbus_request_offers();
2534 
2535 	if (wait_for_completion_timeout(
2536 		&vmbus_connection.ready_for_resume_event, 10 * HZ) == 0)
2537 		pr_err("Some vmbus device is missing after suspending?\n");
2538 
2539 	/* Reset the event for the next suspend. */
2540 	reinit_completion(&vmbus_connection.ready_for_suspend_event);
2541 
2542 	return 0;
2543 }
2544 #else
2545 #define vmbus_bus_suspend NULL
2546 #define vmbus_bus_resume NULL
2547 #endif /* CONFIG_PM_SLEEP */
2548 
2549 static const struct acpi_device_id vmbus_acpi_device_ids[] = {
2550 	{"VMBUS", 0},
2551 	{"VMBus", 0},
2552 	{"", 0},
2553 };
2554 MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
2555 
2556 /*
2557  * Note: we must use the "no_irq" ops, otherwise hibernation can not work with
2558  * PCI device assignment, because "pci_dev_pm_ops" uses the "noirq" ops: in
2559  * the resume path, the pci "noirq" restore op runs before "non-noirq" op (see
2560  * resume_target_kernel() -> dpm_resume_start(), and hibernation_restore() ->
2561  * dpm_resume_end()). This means vmbus_bus_resume() and the pci-hyperv's
2562  * resume callback must also run via the "noirq" ops.
2563  *
2564  * Set suspend_noirq/resume_noirq to NULL for Suspend-to-Idle: see the comment
2565  * earlier in this file before vmbus_pm.
2566  */
2567 
2568 static const struct dev_pm_ops vmbus_bus_pm = {
2569 	.suspend_noirq	= NULL,
2570 	.resume_noirq	= NULL,
2571 	.freeze_noirq	= vmbus_bus_suspend,
2572 	.thaw_noirq	= vmbus_bus_resume,
2573 	.poweroff_noirq	= vmbus_bus_suspend,
2574 	.restore_noirq	= vmbus_bus_resume
2575 };
2576 
2577 static struct acpi_driver vmbus_acpi_driver = {
2578 	.name = "vmbus",
2579 	.ids = vmbus_acpi_device_ids,
2580 	.ops = {
2581 		.add = vmbus_acpi_add,
2582 		.remove = vmbus_acpi_remove,
2583 	},
2584 	.drv.pm = &vmbus_bus_pm,
2585 };
2586 
2587 static void hv_kexec_handler(void)
2588 {
2589 	hv_stimer_global_cleanup();
2590 	vmbus_initiate_unload(false);
2591 	/* Make sure conn_state is set as hv_synic_cleanup checks for it */
2592 	mb();
2593 	cpuhp_remove_state(hyperv_cpuhp_online);
2594 };
2595 
2596 static void hv_crash_handler(struct pt_regs *regs)
2597 {
2598 	int cpu;
2599 
2600 	vmbus_initiate_unload(true);
2601 	/*
2602 	 * In crash handler we can't schedule synic cleanup for all CPUs,
2603 	 * doing the cleanup for current CPU only. This should be sufficient
2604 	 * for kdump.
2605 	 */
2606 	cpu = smp_processor_id();
2607 	hv_stimer_cleanup(cpu);
2608 	hv_synic_disable_regs(cpu);
2609 };
2610 
2611 static int hv_synic_suspend(void)
2612 {
2613 	/*
2614 	 * When we reach here, all the non-boot CPUs have been offlined.
2615 	 * If we're in a legacy configuration where stimer Direct Mode is
2616 	 * not enabled, the stimers on the non-boot CPUs have been unbound
2617 	 * in hv_synic_cleanup() -> hv_stimer_legacy_cleanup() ->
2618 	 * hv_stimer_cleanup() -> clockevents_unbind_device().
2619 	 *
2620 	 * hv_synic_suspend() only runs on CPU0 with interrupts disabled.
2621 	 * Here we do not call hv_stimer_legacy_cleanup() on CPU0 because:
2622 	 * 1) it's unnecessary as interrupts remain disabled between
2623 	 * syscore_suspend() and syscore_resume(): see create_image() and
2624 	 * resume_target_kernel()
2625 	 * 2) the stimer on CPU0 is automatically disabled later by
2626 	 * syscore_suspend() -> timekeeping_suspend() -> tick_suspend() -> ...
2627 	 * -> clockevents_shutdown() -> ... -> hv_ce_shutdown()
2628 	 * 3) a warning would be triggered if we call
2629 	 * clockevents_unbind_device(), which may sleep, in an
2630 	 * interrupts-disabled context.
2631 	 */
2632 
2633 	hv_synic_disable_regs(0);
2634 
2635 	return 0;
2636 }
2637 
2638 static void hv_synic_resume(void)
2639 {
2640 	hv_synic_enable_regs(0);
2641 
2642 	/*
2643 	 * Note: we don't need to call hv_stimer_init(0), because the timer
2644 	 * on CPU0 is not unbound in hv_synic_suspend(), and the timer is
2645 	 * automatically re-enabled in timekeeping_resume().
2646 	 */
2647 }
2648 
2649 /* The callbacks run only on CPU0, with irqs_disabled. */
2650 static struct syscore_ops hv_synic_syscore_ops = {
2651 	.suspend = hv_synic_suspend,
2652 	.resume = hv_synic_resume,
2653 };
2654 
2655 static int __init hv_acpi_init(void)
2656 {
2657 	int ret, t;
2658 
2659 	if (!hv_is_hyperv_initialized())
2660 		return -ENODEV;
2661 
2662 	if (hv_root_partition)
2663 		return 0;
2664 
2665 	init_completion(&probe_event);
2666 
2667 	/*
2668 	 * Get ACPI resources first.
2669 	 */
2670 	ret = acpi_bus_register_driver(&vmbus_acpi_driver);
2671 
2672 	if (ret)
2673 		return ret;
2674 
2675 	t = wait_for_completion_timeout(&probe_event, 5*HZ);
2676 	if (t == 0) {
2677 		ret = -ETIMEDOUT;
2678 		goto cleanup;
2679 	}
2680 	hv_debug_init();
2681 
2682 	ret = vmbus_bus_init();
2683 	if (ret)
2684 		goto cleanup;
2685 
2686 	hv_setup_kexec_handler(hv_kexec_handler);
2687 	hv_setup_crash_handler(hv_crash_handler);
2688 
2689 	register_syscore_ops(&hv_synic_syscore_ops);
2690 
2691 	return 0;
2692 
2693 cleanup:
2694 	acpi_bus_unregister_driver(&vmbus_acpi_driver);
2695 	hv_acpi_dev = NULL;
2696 	return ret;
2697 }
2698 
2699 static void __exit vmbus_exit(void)
2700 {
2701 	int cpu;
2702 
2703 	unregister_syscore_ops(&hv_synic_syscore_ops);
2704 
2705 	hv_remove_kexec_handler();
2706 	hv_remove_crash_handler();
2707 	vmbus_connection.conn_state = DISCONNECTED;
2708 	hv_stimer_global_cleanup();
2709 	vmbus_disconnect();
2710 	hv_remove_vmbus_irq();
2711 	for_each_online_cpu(cpu) {
2712 		struct hv_per_cpu_context *hv_cpu
2713 			= per_cpu_ptr(hv_context.cpu_context, cpu);
2714 
2715 		tasklet_kill(&hv_cpu->msg_dpc);
2716 	}
2717 	hv_debug_rm_all_dir();
2718 
2719 	vmbus_free_channels();
2720 	kfree(vmbus_connection.channels);
2721 
2722 	if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
2723 		kmsg_dump_unregister(&hv_kmsg_dumper);
2724 		unregister_die_notifier(&hyperv_die_block);
2725 		atomic_notifier_chain_unregister(&panic_notifier_list,
2726 						 &hyperv_panic_block);
2727 	}
2728 
2729 	free_page((unsigned long)hv_panic_page);
2730 	unregister_sysctl_table(hv_ctl_table_hdr);
2731 	hv_ctl_table_hdr = NULL;
2732 	bus_unregister(&hv_bus);
2733 
2734 	cpuhp_remove_state(hyperv_cpuhp_online);
2735 	hv_synic_free();
2736 	acpi_bus_unregister_driver(&vmbus_acpi_driver);
2737 }
2738 
2739 
2740 MODULE_LICENSE("GPL");
2741 MODULE_DESCRIPTION("Microsoft Hyper-V VMBus Driver");
2742 
2743 subsys_initcall(hv_acpi_init);
2744 module_exit(vmbus_exit);
2745