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