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