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