linux/drivers/hv/vmbus_drv.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright (c) 2009, Microsoft Corporation.
   4 *
   5 * Authors:
   6 *   Haiyang Zhang <haiyangz@microsoft.com>
   7 *   Hank Janssen  <hjanssen@microsoft.com>
   8 *   K. Y. Srinivasan <kys@microsoft.com>
   9 */
  10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11
  12#include <linux/init.h>
  13#include <linux/module.h>
  14#include <linux/device.h>
  15#include <linux/interrupt.h>
  16#include <linux/sysctl.h>
  17#include <linux/slab.h>
  18#include <linux/acpi.h>
  19#include <linux/completion.h>
  20#include <linux/hyperv.h>
  21#include <linux/kernel_stat.h>
  22#include <linux/clockchips.h>
  23#include <linux/cpu.h>
  24#include <linux/sched/task_stack.h>
  25
  26#include <asm/mshyperv.h>
  27#include <linux/notifier.h>
  28#include <linux/ptrace.h>
  29#include <linux/screen_info.h>
  30#include <linux/kdebug.h>
  31#include <linux/efi.h>
  32#include <linux/random.h>
  33#include "hyperv_vmbus.h"
  34
  35struct vmbus_dynid {
  36        struct list_head node;
  37        struct hv_vmbus_device_id id;
  38};
  39
  40static struct acpi_device  *hv_acpi_dev;
  41
  42static struct completion probe_event;
  43
  44static int hyperv_cpuhp_online;
  45
  46static void *hv_panic_page;
  47
  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
  55        hyperv_report_panic(regs, val);
  56        return NOTIFY_DONE;
  57}
  58
  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
  65        hyperv_report_panic(regs, val);
  66        return NOTIFY_DONE;
  67}
  68
  69static struct notifier_block hyperv_die_block = {
  70        .notifier_call = hyperv_die_event,
  71};
  72static struct notifier_block hyperv_panic_block = {
  73        .notifier_call = hyperv_panic_event,
  74};
  75
  76static const char *fb_mmio_name = "fb_range";
  77static struct resource *fb_mmio;
  78static struct resource *hyperv_mmio;
  79static DEFINE_SEMAPHORE(hyperv_mmio_lock);
  80
  81static int vmbus_exists(void)
  82{
  83        if (hv_acpi_dev == NULL)
  84                return -ENODEV;
  85
  86        return 0;
  87}
  88
  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
  97static u8 channel_monitor_group(const struct vmbus_channel *channel)
  98{
  99        return (u8)channel->offermsg.monitorid / 32;
 100}
 101
 102static u8 channel_monitor_offset(const struct vmbus_channel *channel)
 103{
 104        return (u8)channel->offermsg.monitorid % 32;
 105}
 106
 107static u32 channel_pending(const struct vmbus_channel *channel,
 108                           const struct hv_monitor_page *monitor_page)
 109{
 110        u8 monitor_group = channel_monitor_group(channel);
 111
 112        return monitor_page->trigger_group[monitor_group].pending;
 113}
 114
 115static u32 channel_latency(const struct vmbus_channel *channel,
 116                           const struct hv_monitor_page *monitor_page)
 117{
 118        u8 monitor_group = channel_monitor_group(channel);
 119        u8 monitor_offset = channel_monitor_offset(channel);
 120
 121        return monitor_page->latency[monitor_group][monitor_offset];
 122}
 123
 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
 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
 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
 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
 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
 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
 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
 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
 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,
 224                                       vmbus_connection.monitor_pages[0]));
 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);
 241
 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
 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
 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;
 303        int ret;
 304
 305        if (!hv_dev->channel)
 306                return -ENODEV;
 307
 308        ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
 309                                          &outbound);
 310        if (ret < 0)
 311                return ret;
 312
 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;
 322        int ret;
 323
 324        if (!hv_dev->channel)
 325                return -ENODEV;
 326
 327        ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
 328                                          &outbound);
 329        if (ret < 0)
 330                return ret;
 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;
 341        int ret;
 342
 343        if (!hv_dev->channel)
 344                return -ENODEV;
 345
 346        ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
 347                                          &outbound);
 348        if (ret < 0)
 349                return ret;
 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;
 360        int ret;
 361
 362        if (!hv_dev->channel)
 363                return -ENODEV;
 364
 365        ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
 366                                          &outbound);
 367        if (ret < 0)
 368                return ret;
 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        int ret;
 380
 381        if (!hv_dev->channel)
 382                return -ENODEV;
 383
 384        ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
 385                                          &outbound);
 386        if (ret < 0)
 387                return ret;
 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;
 397        int ret;
 398
 399        if (!hv_dev->channel)
 400                return -ENODEV;
 401
 402        ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
 403        if (ret < 0)
 404                return ret;
 405
 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;
 415        int ret;
 416
 417        if (!hv_dev->channel)
 418                return -ENODEV;
 419
 420        ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
 421        if (ret < 0)
 422                return ret;
 423
 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;
 433        int ret;
 434
 435        if (!hv_dev->channel)
 436                return -ENODEV;
 437
 438        ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
 439        if (ret < 0)
 440                return ret;
 441
 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;
 452        int ret;
 453
 454        if (!hv_dev->channel)
 455                return -ENODEV;
 456
 457        ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
 458        if (ret < 0)
 459                return ret;
 460
 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;
 471        int ret;
 472
 473        if (!hv_dev->channel)
 474                return -ENODEV;
 475
 476        ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
 477        if (ret < 0)
 478                return ret;
 479
 480        return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
 481}
 482static DEVICE_ATTR_RO(in_write_bytes_avail);
 483
 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
 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
 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
 587/* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
 588static struct attribute *vmbus_dev_attrs[] = {
 589        &dev_attr_id.attr,
 590        &dev_attr_state.attr,
 591        &dev_attr_monitor_id.attr,
 592        &dev_attr_class_id.attr,
 593        &dev_attr_device_id.attr,
 594        &dev_attr_modalias.attr,
 595#ifdef CONFIG_NUMA
 596        &dev_attr_numa_node.attr,
 597#endif
 598        &dev_attr_server_monitor_pending.attr,
 599        &dev_attr_client_monitor_pending.attr,
 600        &dev_attr_server_monitor_latency.attr,
 601        &dev_attr_client_monitor_latency.attr,
 602        &dev_attr_server_monitor_conn_id.attr,
 603        &dev_attr_client_monitor_conn_id.attr,
 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,
 614        &dev_attr_channel_vp_mapping.attr,
 615        &dev_attr_vendor.attr,
 616        &dev_attr_device.attr,
 617        &dev_attr_driver_override.attr,
 618        NULL,
 619};
 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);
 650
 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
 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.
 661 */
 662static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
 663{
 664        struct hv_device *dev = device_to_hv_device(device);
 665        int ret;
 666        char alias_name[VMBUS_ALIAS_LEN + 1];
 667
 668        print_alias_name(dev, alias_name);
 669        ret = add_uevent_var(env, "MODALIAS=vmbus:%s", alias_name);
 670        return ret;
 671}
 672
 673static const struct hv_vmbus_device_id *
 674hv_vmbus_dev_match(const struct hv_vmbus_device_id *id, const guid_t *guid)
 675{
 676        if (id == NULL)
 677                return NULL; /* empty device table */
 678
 679        for (; !guid_is_null(&id->guid); id++)
 680                if (guid_equal(&id->guid, guid))
 681                        return id;
 682
 683        return NULL;
 684}
 685
 686static const struct hv_vmbus_device_id *
 687hv_vmbus_dynid_match(struct hv_driver *drv, const guid_t *guid)
 688{
 689        const struct hv_vmbus_device_id *id = NULL;
 690        struct vmbus_dynid *dynid;
 691
 692        spin_lock(&drv->dynids.lock);
 693        list_for_each_entry(dynid, &drv->dynids.list, node) {
 694                if (guid_equal(&dynid->id.guid, guid)) {
 695                        id = &dynid->id;
 696                        break;
 697                }
 698        }
 699        spin_unlock(&drv->dynids.lock);
 700
 701        return id;
 702}
 703
 704static const struct hv_vmbus_device_id vmbus_device_null;
 705
 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{
 713        const guid_t *guid = &dev->dev_type;
 714        const struct hv_vmbus_device_id *id;
 715
 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;
 730}
 731
 732/* vmbus_add_dynid - add a new device ID to this driver and re-probe devices */
 733static int vmbus_add_dynid(struct hv_driver *drv, guid_t *guid)
 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
 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);
 771        guid_t guid;
 772        ssize_t retval;
 773
 774        retval = guid_parse(buf, &guid);
 775        if (retval)
 776                return retval;
 777
 778        if (hv_vmbus_dynid_match(drv, &guid))
 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;
 798        guid_t guid;
 799        ssize_t retval;
 800
 801        retval = guid_parse(buf, &guid);
 802        if (retval)
 803                return retval;
 804
 805        retval = -ENODEV;
 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
 810                if (guid_equal(&id->guid, &guid)) {
 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);
 829
 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{
 836        struct hv_driver *drv = drv_to_hv_drv(driver);
 837        struct hv_device *hv_dev = device_to_hv_device(device);
 838
 839        /* The hv_sock driver handles all hv_sock offers. */
 840        if (is_hvsock_channel(hv_dev->channel))
 841                return drv->hvsock;
 842
 843        if (hv_vmbus_get_id(drv, hv_dev))
 844                return 1;
 845
 846        return 0;
 847}
 848
 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);
 857        struct hv_device *dev = device_to_hv_device(child_device);
 858        const struct hv_vmbus_device_id *dev_id;
 859
 860        dev_id = hv_vmbus_get_id(drv, dev);
 861        if (drv->probe) {
 862                ret = drv->probe(dev, dev_id);
 863                if (ret != 0)
 864                        pr_err("probe failed for device %s (%d)\n",
 865                               dev_name(child_device), ret);
 866
 867        } else {
 868                pr_err("probe not set for driver %s\n",
 869                       dev_name(child_device));
 870                ret = -ENODEV;
 871        }
 872        return ret;
 873}
 874
 875/*
 876 * vmbus_remove - Remove a vmbus device
 877 */
 878static int vmbus_remove(struct device *child_device)
 879{
 880        struct hv_driver *drv;
 881        struct hv_device *dev = device_to_hv_device(child_device);
 882
 883        if (child_device->driver) {
 884                drv = drv_to_hv_drv(child_device->driver);
 885                if (drv->remove)
 886                        drv->remove(dev);
 887        }
 888
 889        return 0;
 890}
 891
 892
 893/*
 894 * vmbus_shutdown - Shutdown a vmbus device
 895 */
 896static void vmbus_shutdown(struct device *child_device)
 897{
 898        struct hv_driver *drv;
 899        struct hv_device *dev = device_to_hv_device(child_device);
 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
 908        if (drv->shutdown)
 909                drv->shutdown(dev);
 910}
 911
 912
 913/*
 914 * vmbus_device_release - Final callback release of the vmbus child device
 915 */
 916static void vmbus_device_release(struct device *device)
 917{
 918        struct hv_device *hv_dev = device_to_hv_device(device);
 919        struct vmbus_channel *channel = hv_dev->channel;
 920
 921        mutex_lock(&vmbus_connection.channel_mutex);
 922        hv_process_channel_removal(channel);
 923        mutex_unlock(&vmbus_connection.channel_mutex);
 924        kfree(hv_dev);
 925}
 926
 927/* The one and only one */
 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,
 935        .dev_groups =           vmbus_dev_groups,
 936        .drv_groups =           vmbus_drv_groups,
 937};
 938
 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
 948        /* Do not process messages if we're in DISCONNECTED state */
 949        if (vmbus_connection.conn_state == DISCONNECTED)
 950                return;
 951
 952        ctx = container_of(work, struct onmessage_work_context,
 953                           work);
 954        vmbus_onmessage(&ctx->msg);
 955        kfree(ctx);
 956}
 957
 958static void hv_process_timer_expiration(struct hv_message *msg,
 959                                        struct hv_per_cpu_context *hv_cpu)
 960{
 961        struct clock_event_device *dev = hv_cpu->clk_evt;
 962
 963        if (dev->event_handler)
 964                dev->event_handler(dev);
 965
 966        vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED);
 967}
 968
 969void vmbus_on_msg_dpc(unsigned long data)
 970{
 971        struct hv_per_cpu_context *hv_cpu = (void *)data;
 972        void *page_addr = hv_cpu->synic_message_page;
 973        struct hv_message *msg = (struct hv_message *)page_addr +
 974                                  VMBUS_MESSAGE_SINT;
 975        struct vmbus_channel_message_header *hdr;
 976        const struct vmbus_channel_message_table_entry *entry;
 977        struct onmessage_work_context *ctx;
 978        u32 message_type = msg->header.message_type;
 979
 980        if (message_type == HVMSG_NONE)
 981                /* no msg */
 982                return;
 983
 984        hdr = (struct vmbus_channel_message_header *)msg->u.payload;
 985
 986        trace_vmbus_on_msg_dpc(hdr);
 987
 988        if (hdr->msgtype >= CHANNELMSG_COUNT) {
 989                WARN_ONCE(1, "unknown msgtype=%d\n", hdr->msgtype);
 990                goto msg_handled;
 991        }
 992
 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;
 998
 999                INIT_WORK(&ctx->work, vmbus_onmessage_work);
1000                memcpy(&ctx->msg, msg, sizeof(*msg));
1001
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                }
1028        } else
1029                entry->message_handler(hdr);
1030
1031msg_handled:
1032        vmbus_signal_eom(msg, message_type);
1033}
1034
1035
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
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
1087                rcu_read_lock();
1088
1089                /* Find channel based on relid */
1090                list_for_each_entry_rcu(channel, &hv_cpu->chan_list, percpu_list) {
1091                        if (channel->offermsg.child_relid != relid)
1092                                continue;
1093
1094                        if (channel->rescind)
1095                                continue;
1096
1097                        trace_vmbus_chan_sched(channel);
1098
1099                        ++channel->interrupts;
1100
1101                        switch (channel->callback_mode) {
1102                        case HV_CALL_ISR:
1103                                vmbus_channel_isr(channel);
1104                                break;
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);
1111                        }
1112                }
1113
1114                rcu_read_unlock();
1115        }
1116}
1117
1118static void vmbus_isr(void)
1119{
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;
1123        struct hv_message *msg;
1124        union hv_synic_event_flags *event;
1125        bool handled = false;
1126
1127        if (unlikely(page_addr == NULL))
1128                return;
1129
1130        event = (union hv_synic_event_flags *)page_addr +
1131                                         VMBUS_MESSAGE_SINT;
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         */
1137
1138        if ((vmbus_proto_version == VERSION_WS2008) ||
1139                (vmbus_proto_version == VERSION_WIN7)) {
1140
1141                /* Since we are a child, we only need to check bit 0 */
1142                if (sync_test_and_clear_bit(0, event->flags))
1143                        handled = true;
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                 */
1151                handled = true;
1152        }
1153
1154        if (handled)
1155                vmbus_chan_sched(hv_cpu);
1156
1157        page_addr = hv_cpu->synic_message_page;
1158        msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
1159
1160        /* Check if there are actual msgs to be processed */
1161        if (msg->header.message_type != HVMSG_NONE) {
1162                if (msg->header.message_type == HVMSG_TIMER_EXPIRED)
1163                        hv_process_timer_expiration(msg, hv_cpu);
1164                else
1165                        tasklet_schedule(&hv_cpu->msg_dpc);
1166        }
1167
1168        add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0);
1169}
1170
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         */
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);
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};
1237
1238/*
1239 * vmbus_bus_init -Main vmbus driver initialization routine.
1240 *
1241 * Here, we
1242 *      - initialize the vmbus driver context
1243 *      - invoke the vmbus hv main init routine
1244 *      - retrieve the channel offers
1245 */
1246static int vmbus_bus_init(void)
1247{
1248        int ret;
1249
1250        /* Hypervisor initialization...setup hypercall page..etc */
1251        ret = hv_init();
1252        if (ret != 0) {
1253                pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
1254                return ret;
1255        }
1256
1257        ret = bus_register(&hv_bus);
1258        if (ret)
1259                return ret;
1260
1261        hv_setup_vmbus_irq(vmbus_isr);
1262
1263        ret = hv_synic_alloc();
1264        if (ret)
1265                goto err_alloc;
1266        /*
1267         * Initialize the per-cpu interrupt state and
1268         * connect to the host.
1269         */
1270        ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hyperv/vmbus:online",
1271                                hv_synic_init, hv_synic_cleanup);
1272        if (ret < 0)
1273                goto err_alloc;
1274        hyperv_cpuhp_online = ret;
1275
1276        ret = vmbus_connect();
1277        if (ret)
1278                goto err_connect;
1279
1280        /*
1281         * Only register if the crash MSRs are available
1282         */
1283        if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
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                 */
1297                hv_get_crash_ctl(hyperv_crash_ctl);
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
1310                register_die_notifier(&hyperv_die_block);
1311                atomic_notifier_chain_register(&panic_notifier_list,
1312                                               &hyperv_panic_block);
1313        }
1314
1315        vmbus_request_offers();
1316
1317        return 0;
1318
1319err_connect:
1320        cpuhp_remove_state(hyperv_cpuhp_online);
1321err_alloc:
1322        hv_synic_free();
1323        hv_remove_vmbus_irq();
1324
1325        bus_unregister(&hv_bus);
1326        free_page((unsigned long)hv_panic_page);
1327        unregister_sysctl_table(hv_ctl_table_hdr);
1328        hv_ctl_table_hdr = NULL;
1329        return ret;
1330}
1331
1332/**
1333 * __vmbus_child_driver_register() - Register a vmbus's driver
1334 * @hv_driver: Pointer to driver structure you want to register
1335 * @owner: owner module of the drv
1336 * @mod_name: module name string
1337 *
1338 * Registers the given driver with Linux through the 'driver_register()' call
1339 * and sets up the hyper-v vmbus handling for this driver.
1340 * It will return the state of the 'driver_register()' call.
1341 *
1342 */
1343int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
1344{
1345        int ret;
1346
1347        pr_info("registering driver %s\n", hv_driver->name);
1348
1349        ret = vmbus_exists();
1350        if (ret < 0)
1351                return ret;
1352
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;
1357
1358        spin_lock_init(&hv_driver->dynids.lock);
1359        INIT_LIST_HEAD(&hv_driver->dynids.list);
1360
1361        ret = driver_register(&hv_driver->driver);
1362
1363        return ret;
1364}
1365EXPORT_SYMBOL_GPL(__vmbus_driver_register);
1366
1367/**
1368 * vmbus_driver_unregister() - Unregister a vmbus's driver
1369 * @hv_driver: Pointer to driver structure you want to
1370 *             un-register
1371 *
1372 * Un-register the given driver that was previous registered with a call to
1373 * vmbus_driver_register()
1374 */
1375void vmbus_driver_unregister(struct hv_driver *hv_driver)
1376{
1377        pr_info("unregistering driver %s\n", hv_driver->name);
1378
1379        if (!vmbus_exists()) {
1380                driver_unregister(&hv_driver->driver);
1381                vmbus_free_dynids(hv_driver);
1382        }
1383}
1384EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
1385
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;
1400        ssize_t (*show)(struct vmbus_channel *chan, char *buf);
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);
1419        struct vmbus_channel *chan
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
1432static ssize_t out_mask_show(struct vmbus_channel *channel, char *buf)
1433{
1434        struct hv_ring_buffer_info *rbi = &channel->outbound;
1435        ssize_t ret;
1436
1437        mutex_lock(&rbi->ring_buffer_mutex);
1438        if (!rbi->ring_buffer) {
1439                mutex_unlock(&rbi->ring_buffer_mutex);
1440                return -EINVAL;
1441        }
1442
1443        ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1444        mutex_unlock(&rbi->ring_buffer_mutex);
1445        return ret;
1446}
1447static VMBUS_CHAN_ATTR_RO(out_mask);
1448
1449static ssize_t in_mask_show(struct vmbus_channel *channel, char *buf)
1450{
1451        struct hv_ring_buffer_info *rbi = &channel->inbound;
1452        ssize_t ret;
1453
1454        mutex_lock(&rbi->ring_buffer_mutex);
1455        if (!rbi->ring_buffer) {
1456                mutex_unlock(&rbi->ring_buffer_mutex);
1457                return -EINVAL;
1458        }
1459
1460        ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1461        mutex_unlock(&rbi->ring_buffer_mutex);
1462        return ret;
1463}
1464static VMBUS_CHAN_ATTR_RO(in_mask);
1465
1466static ssize_t read_avail_show(struct vmbus_channel *channel, char *buf)
1467{
1468        struct hv_ring_buffer_info *rbi = &channel->inbound;
1469        ssize_t ret;
1470
1471        mutex_lock(&rbi->ring_buffer_mutex);
1472        if (!rbi->ring_buffer) {
1473                mutex_unlock(&rbi->ring_buffer_mutex);
1474                return -EINVAL;
1475        }
1476
1477        ret = sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi));
1478        mutex_unlock(&rbi->ring_buffer_mutex);
1479        return ret;
1480}
1481static VMBUS_CHAN_ATTR_RO(read_avail);
1482
1483static ssize_t write_avail_show(struct vmbus_channel *channel, char *buf)
1484{
1485        struct hv_ring_buffer_info *rbi = &channel->outbound;
1486        ssize_t ret;
1487
1488        mutex_lock(&rbi->ring_buffer_mutex);
1489        if (!rbi->ring_buffer) {
1490                mutex_unlock(&rbi->ring_buffer_mutex);
1491                return -EINVAL;
1492        }
1493
1494        ret = sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi));
1495        mutex_unlock(&rbi->ring_buffer_mutex);
1496        return ret;
1497}
1498static VMBUS_CHAN_ATTR_RO(write_avail);
1499
1500static ssize_t show_target_cpu(struct vmbus_channel *channel, char *buf)
1501{
1502        return sprintf(buf, "%u\n", channel->target_cpu);
1503}
1504static VMBUS_CHAN_ATTR(cpu, S_IRUGO, show_target_cpu, NULL);
1505
1506static ssize_t channel_pending_show(struct vmbus_channel *channel,
1507                                    char *buf)
1508{
1509        return sprintf(buf, "%d\n",
1510                       channel_pending(channel,
1511                                       vmbus_connection.monitor_pages[1]));
1512}
1513static VMBUS_CHAN_ATTR(pending, S_IRUGO, channel_pending_show, NULL);
1514
1515static ssize_t channel_latency_show(struct vmbus_channel *channel,
1516                                    char *buf)
1517{
1518        return sprintf(buf, "%d\n",
1519                       channel_latency(channel,
1520                                       vmbus_connection.monitor_pages[1]));
1521}
1522static VMBUS_CHAN_ATTR(latency, S_IRUGO, channel_latency_show, NULL);
1523
1524static ssize_t channel_interrupts_show(struct vmbus_channel *channel, char *buf)
1525{
1526        return sprintf(buf, "%llu\n", channel->interrupts);
1527}
1528static VMBUS_CHAN_ATTR(interrupts, S_IRUGO, channel_interrupts_show, NULL);
1529
1530static ssize_t channel_events_show(struct vmbus_channel *channel, char *buf)
1531{
1532        return sprintf(buf, "%llu\n", channel->sig_events);
1533}
1534static VMBUS_CHAN_ATTR(events, S_IRUGO, channel_events_show, NULL);
1535
1536static ssize_t channel_intr_in_full_show(struct vmbus_channel *channel,
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
1544static ssize_t channel_intr_out_empty_show(struct vmbus_channel *channel,
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
1552static ssize_t channel_out_full_first_show(struct vmbus_channel *channel,
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
1560static ssize_t channel_out_full_total_show(struct vmbus_channel *channel,
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
1568static ssize_t subchannel_monitor_id_show(struct vmbus_channel *channel,
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
1575static ssize_t subchannel_id_show(struct vmbus_channel *channel,
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
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,
1591        &chan_attr_interrupts.attr,
1592        &chan_attr_events.attr,
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,
1597        &chan_attr_monitor_id.attr,
1598        &chan_attr_subchannel_id.attr,
1599        NULL
1600};
1601
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
1627static struct kobj_type vmbus_chan_ktype = {
1628        .sysfs_ops = &vmbus_chan_sysfs_ops,
1629        .release = vmbus_chan_release,
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{
1637        const struct device *device = &dev->device;
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
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
1659        kobject_uevent(kobj, KOBJ_ADD);
1660
1661        return 0;
1662}
1663
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
1672/*
1673 * vmbus_device_create - Creates and registers a new child device
1674 * on the vmbus.
1675 */
1676struct hv_device *vmbus_device_create(const guid_t *type,
1677                                      const guid_t *instance,
1678                                      struct vmbus_channel *channel)
1679{
1680        struct hv_device *child_device_obj;
1681
1682        child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
1683        if (!child_device_obj) {
1684                pr_err("Unable to allocate device object for child device\n");
1685                return NULL;
1686        }
1687
1688        child_device_obj->channel = channel;
1689        guid_copy(&child_device_obj->dev_type, type);
1690        guid_copy(&child_device_obj->dev_instance, instance);
1691        child_device_obj->vendor_id = 0x1414; /* MSFT vendor ID */
1692
1693        return child_device_obj;
1694}
1695
1696/*
1697 * vmbus_device_register - Register the child device
1698 */
1699int vmbus_device_register(struct hv_device *child_device_obj)
1700{
1701        struct kobject *kobj = &child_device_obj->device.kobj;
1702        int ret;
1703
1704        dev_set_name(&child_device_obj->device, "%pUl",
1705                     child_device_obj->channel->offermsg.offer.if_instance.b);
1706
1707        child_device_obj->device.bus = &hv_bus;
1708        child_device_obj->device.parent = &hv_acpi_dev->dev;
1709        child_device_obj->device.release = vmbus_device_release;
1710
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         */
1715        ret = device_register(&child_device_obj->device);
1716        if (ret) {
1717                pr_err("Unable to register child device\n");
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);
1739
1740err_dev_unregister:
1741        device_unregister(&child_device_obj->device);
1742        return ret;
1743}
1744
1745/*
1746 * vmbus_device_unregister - Remove the specified child device
1747 * from the vmbus.
1748 */
1749void vmbus_device_unregister(struct hv_device *device_obj)
1750{
1751        pr_debug("child device %s unregistered\n",
1752                dev_name(&device_obj->device));
1753
1754        kset_unregister(device_obj->channels_kset);
1755
1756        /*
1757         * Kick off the process of unregistering the device.
1758         * This will call vmbus_remove() and eventually vmbus_device_release()
1759         */
1760        device_unregister(&device_obj->device);
1761}
1762
1763
1764/*
1765 * VMBUS is an acpi enumerated device. Get the information we
1766 * need from DSDT.
1767 */
1768#define VTPM_BASE_ADDRESS 0xfed40000
1769static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
1770{
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
1777        switch (res->type) {
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;
1787                break;
1788
1789        case ACPI_RESOURCE_TYPE_ADDRESS64:
1790                start = res->data.address64.address.minimum;
1791                end = res->data.address64.address.maximum;
1792                break;
1793
1794        default:
1795                /* Unused resource type */
1796                return AE_OK;
1797
1798        }
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
1819        /*
1820         * If two ranges are adjacent, merge them.
1821         */
1822        do {
1823                if (!*old_res) {
1824                        *old_res = new_res;
1825                        break;
1826                }
1827
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
1840                if ((*old_res)->start > new_res->end) {
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);
1852
1853        return AE_OK;
1854}
1855
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) {
1862                if (fb_mmio) {
1863                        __release_region(hyperv_mmio, fb_mmio->start,
1864                                         resource_size(fb_mmio));
1865                        fb_mmio = NULL;
1866                }
1867
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
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
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{
1929        struct resource *iter, *shadow;
1930        resource_size_t range_min, range_max, start;
1931        const char *dev_n = dev_name(&device_obj->device);
1932        int retval;
1933
1934        retval = -ENXIO;
1935        down(&hyperv_mmio_lock);
1936
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
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;
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;
1975                        }
1976
1977                        __release_region(iter, start, size);
1978                }
1979        }
1980
1981exit:
1982        up(&hyperv_mmio_lock);
1983        return retval;
1984}
1985EXPORT_SYMBOL_GPL(vmbus_allocate_mmio);
1986
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{
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        }
2006        release_mem_region(start, size);
2007        up(&hyperv_mmio_lock);
2008
2009}
2010EXPORT_SYMBOL_GPL(vmbus_free_mmio);
2011
2012static int vmbus_acpi_add(struct acpi_device *device)
2013{
2014        acpi_status result;
2015        int ret_val = -ENODEV;
2016        struct acpi_device *ancestor;
2017
2018        hv_acpi_dev = device;
2019
2020        result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
2021                                        vmbus_walk_resources, NULL);
2022
2023        if (ACPI_FAILURE(result))
2024                goto acpi_walk_err;
2025        /*
2026         * Some ancestor of the vmbus acpi device (Gen1 or Gen2
2027         * firmware) is the VMOD that has the mmio ranges. Get that.
2028         */
2029        for (ancestor = device->parent; ancestor; ancestor = ancestor->parent) {
2030                result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS,
2031                                             vmbus_walk_resources, NULL);
2032
2033                if (ACPI_FAILURE(result))
2034                        continue;
2035                if (hyperv_mmio) {
2036                        vmbus_reserve_fb();
2037                        break;
2038                }
2039        }
2040        ret_val = 0;
2041
2042acpi_walk_err:
2043        complete(&probe_event);
2044        if (ret_val)
2045                vmbus_acpi_remove(device);
2046        return ret_val;
2047}
2048
2049static const struct acpi_device_id vmbus_acpi_device_ids[] = {
2050        {"VMBUS", 0},
2051        {"VMBus", 0},
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,
2061                .remove = vmbus_acpi_remove,
2062        },
2063};
2064
2065static void hv_kexec_handler(void)
2066{
2067        hv_synic_clockevents_cleanup();
2068        vmbus_initiate_unload(false);
2069        vmbus_connection.conn_state = DISCONNECTED;
2070        /* Make sure conn_state is set as hv_synic_cleanup checks for it */
2071        mb();
2072        cpuhp_remove_state(hyperv_cpuhp_online);
2073        hyperv_cleanup();
2074};
2075
2076static void hv_crash_handler(struct pt_regs *regs)
2077{
2078        vmbus_initiate_unload(true);
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         */
2084        vmbus_connection.conn_state = DISCONNECTED;
2085        hv_synic_cleanup(smp_processor_id());
2086        hyperv_cleanup();
2087};
2088
2089static int __init hv_acpi_init(void)
2090{
2091        int ret, t;
2092
2093        if (!hv_is_hyperv_initialized())
2094                return -ENODEV;
2095
2096        init_completion(&probe_event);
2097
2098        /*
2099         * Get ACPI resources first.
2100         */
2101        ret = acpi_bus_register_driver(&vmbus_acpi_driver);
2102
2103        if (ret)
2104                return ret;
2105
2106        t = wait_for_completion_timeout(&probe_event, 5*HZ);
2107        if (t == 0) {
2108                ret = -ETIMEDOUT;
2109                goto cleanup;
2110        }
2111
2112        ret = vmbus_bus_init();
2113        if (ret)
2114                goto cleanup;
2115
2116        hv_setup_kexec_handler(hv_kexec_handler);
2117        hv_setup_crash_handler(hv_crash_handler);
2118
2119        return 0;
2120
2121cleanup:
2122        acpi_bus_unregister_driver(&vmbus_acpi_driver);
2123        hv_acpi_dev = NULL;
2124        return ret;
2125}
2126
2127static void __exit vmbus_exit(void)
2128{
2129        int cpu;
2130
2131        hv_remove_kexec_handler();
2132        hv_remove_crash_handler();
2133        vmbus_connection.conn_state = DISCONNECTED;
2134        hv_synic_clockevents_cleanup();
2135        vmbus_disconnect();
2136        hv_remove_vmbus_irq();
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        }
2143        vmbus_free_channels();
2144
2145        if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
2146                kmsg_dump_unregister(&hv_kmsg_dumper);
2147                unregister_die_notifier(&hyperv_die_block);
2148                atomic_notifier_chain_unregister(&panic_notifier_list,
2149                                                 &hyperv_panic_block);
2150        }
2151
2152        free_page((unsigned long)hv_panic_page);
2153        unregister_sysctl_table(hv_ctl_table_hdr);
2154        hv_ctl_table_hdr = NULL;
2155        bus_unregister(&hv_bus);
2156
2157        cpuhp_remove_state(hyperv_cpuhp_online);
2158        hv_synic_free();
2159        acpi_bus_unregister_driver(&vmbus_acpi_driver);
2160}
2161
2162
2163MODULE_LICENSE("GPL");
2164
2165subsys_initcall(hv_acpi_init);
2166module_exit(vmbus_exit);
2167