root/drivers/hv/vmbus_drv.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. hyperv_report_reg
  2. hyperv_panic_event
  3. hyperv_die_event
  4. vmbus_exists
  5. print_alias_name
  6. channel_monitor_group
  7. channel_monitor_offset
  8. channel_pending
  9. channel_latency
  10. channel_conn_id
  11. id_show
  12. state_show
  13. monitor_id_show
  14. class_id_show
  15. device_id_show
  16. modalias_show
  17. numa_node_show
  18. server_monitor_pending_show
  19. client_monitor_pending_show
  20. server_monitor_latency_show
  21. client_monitor_latency_show
  22. server_monitor_conn_id_show
  23. client_monitor_conn_id_show
  24. out_intr_mask_show
  25. out_read_index_show
  26. out_write_index_show
  27. out_read_bytes_avail_show
  28. out_write_bytes_avail_show
  29. in_intr_mask_show
  30. in_read_index_show
  31. in_write_index_show
  32. in_read_bytes_avail_show
  33. in_write_bytes_avail_show
  34. channel_vp_mapping_show
  35. vendor_show
  36. device_show
  37. driver_override_store
  38. driver_override_show
  39. vmbus_dev_attr_is_visible
  40. vmbus_uevent
  41. hv_vmbus_dev_match
  42. hv_vmbus_dynid_match
  43. hv_vmbus_get_id
  44. vmbus_add_dynid
  45. vmbus_free_dynids
  46. new_id_store
  47. remove_id_store
  48. vmbus_match
  49. vmbus_probe
  50. vmbus_remove
  51. vmbus_shutdown
  52. vmbus_suspend
  53. vmbus_resume
  54. vmbus_device_release
  55. vmbus_onmessage_work
  56. vmbus_on_msg_dpc
  57. vmbus_force_channel_rescinded
  58. vmbus_channel_isr
  59. vmbus_chan_sched
  60. vmbus_isr
  61. hv_kmsg_dump
  62. vmbus_bus_init
  63. __vmbus_driver_register
  64. vmbus_driver_unregister
  65. vmbus_chan_release
  66. vmbus_chan_attr_show
  67. out_mask_show
  68. in_mask_show
  69. read_avail_show
  70. write_avail_show
  71. show_target_cpu
  72. channel_pending_show
  73. channel_latency_show
  74. channel_interrupts_show
  75. channel_events_show
  76. channel_intr_in_full_show
  77. channel_intr_out_empty_show
  78. channel_out_full_first_show
  79. channel_out_full_total_show
  80. subchannel_monitor_id_show
  81. subchannel_id_show
  82. vmbus_chan_attr_is_visible
  83. vmbus_add_channel_kobj
  84. vmbus_remove_channel_attr_group
  85. vmbus_device_create
  86. vmbus_device_register
  87. vmbus_device_unregister
  88. vmbus_walk_resources
  89. vmbus_acpi_remove
  90. vmbus_reserve_fb
  91. vmbus_allocate_mmio
  92. vmbus_free_mmio
  93. vmbus_acpi_add
  94. vmbus_bus_suspend
  95. vmbus_bus_resume
  96. hv_kexec_handler
  97. hv_crash_handler
  98. hv_synic_suspend
  99. hv_synic_resume
  100. hv_acpi_init
  101. vmbus_exit

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

/* [<][>][^][v][top][bottom][index][help] */