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