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