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