at commit c03c1a14
| 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/platform_device.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/sysctl.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/acpi.h> |
| 20 | #include <linux/completion.h> |
| 21 | #include <linux/hyperv.h> |
| 22 | #include <linux/kernel_stat.h> |
| 23 | #include <linux/of_address.h> |
| 24 | #include <linux/clockchips.h> |
| 25 | #include <linux/cpu.h> |
| 26 | #include <linux/sched/isolation.h> |
| 27 | #include <linux/sched/task_stack.h> |
| 28 | #include <linux/smpboot.h> |
| 29 | |
| 30 | #include <linux/delay.h> |
| 31 | #include <linux/panic_notifier.h> |
| 32 | #include <linux/ptrace.h> |
| 33 | #include <linux/sysfb.h> |
| 34 | #include <linux/efi.h> |
| 35 | #include <linux/kernel.h> |
| 36 | #include <linux/syscore_ops.h> |
| 37 | #include <linux/dma-map-ops.h> |
| 38 | #include <linux/pci.h> |
| 39 | #include <linux/export.h> |
| 40 | #include <clocksource/hyperv_timer.h> |
| 41 | #include <asm/mshyperv.h> |
| 42 | #include "hyperv_vmbus.h" |
| 43 | |
| 44 | struct vmbus_dynid { |
| 45 | struct list_head node; |
| 46 | struct hv_vmbus_device_id id; |
| 47 | }; |
| 48 | |
| 49 | /* VMBus Root Device */ |
| 50 | static struct device *vmbus_root_device; |
| 51 | |
| 52 | static int hyperv_cpuhp_online; |
| 53 | |
| 54 | static DEFINE_PER_CPU(long, vmbus_evt); |
| 55 | |
| 56 | /* Values parsed from ACPI DSDT */ |
| 57 | int vmbus_irq; |
| 58 | int vmbus_interrupt; |
| 59 | |
| 60 | /* |
| 61 | * If the Confidential VMBus is used, the data on the "wire" is not |
| 62 | * visible to either the host or the hypervisor. |
| 63 | */ |
| 64 | static bool is_confidential; |
| 65 | |
| 66 | bool vmbus_is_confidential(void) |
| 67 | { |
| 68 | return is_confidential; |
| 69 | } |
| 70 | EXPORT_SYMBOL_GPL(vmbus_is_confidential); |
| 71 | |
| 72 | static bool skip_vmbus_unload; |
| 73 | |
| 74 | /* |
| 75 | * Allow a VMBus framebuffer driver to specify that in the case of a panic, |
| 76 | * it will do the VMbus unload operation once it has flushed any dirty |
| 77 | * portions of the framebuffer to the Hyper-V host. |
| 78 | */ |
| 79 | void vmbus_set_skip_unload(bool skip) |
| 80 | { |
| 81 | skip_vmbus_unload = skip; |
| 82 | } |
| 83 | EXPORT_SYMBOL_GPL(vmbus_set_skip_unload); |
| 84 | |
| 85 | /* |
| 86 | * The panic notifier below is responsible solely for unloading the |
| 87 | * vmbus connection, which is necessary in a panic event. |
| 88 | */ |
| 89 | static int hv_panic_vmbus_unload(struct notifier_block *nb, unsigned long val, |
| 90 | void *args) |
| 91 | { |
| 92 | if (!skip_vmbus_unload) |
| 93 | vmbus_initiate_unload(true); |
| 94 | |
| 95 | return NOTIFY_DONE; |
| 96 | } |
| 97 | static struct notifier_block hyperv_panic_vmbus_unload_block = { |
| 98 | .notifier_call = hv_panic_vmbus_unload, |
| 99 | .priority = INT_MIN + 1, /* almost the latest one to execute */ |
| 100 | }; |
| 101 | |
| 102 | static const char *fb_mmio_name = "fb_range"; |
| 103 | static struct resource *fb_mmio; |
| 104 | static struct resource *hyperv_mmio; |
| 105 | static DEFINE_MUTEX(hyperv_mmio_lock); |
| 106 | |
| 107 | struct device *hv_get_vmbus_root_device(void) |
| 108 | { |
| 109 | return vmbus_root_device; |
| 110 | } |
| 111 | EXPORT_SYMBOL_GPL(hv_get_vmbus_root_device); |
| 112 | |
| 113 | bool hv_vmbus_exists(void) |
| 114 | { |
| 115 | return vmbus_root_device != NULL; |
| 116 | } |
| 117 | EXPORT_SYMBOL_GPL(hv_vmbus_exists); |
| 118 | |
| 119 | static u8 channel_monitor_group(const struct vmbus_channel *channel) |
| 120 | { |
| 121 | return (u8)channel->offermsg.monitorid / 32; |
| 122 | } |
| 123 | |
| 124 | static u8 channel_monitor_offset(const struct vmbus_channel *channel) |
| 125 | { |
| 126 | return (u8)channel->offermsg.monitorid % 32; |
| 127 | } |
| 128 | |
| 129 | static u32 channel_pending(const struct vmbus_channel *channel, |
| 130 | const struct hv_monitor_page *monitor_page) |
| 131 | { |
| 132 | u8 monitor_group = channel_monitor_group(channel); |
| 133 | |
| 134 | return monitor_page->trigger_group[monitor_group].pending; |
| 135 | } |
| 136 | |
| 137 | static u32 channel_latency(const struct vmbus_channel *channel, |
| 138 | const struct hv_monitor_page *monitor_page) |
| 139 | { |
| 140 | u8 monitor_group = channel_monitor_group(channel); |
| 141 | u8 monitor_offset = channel_monitor_offset(channel); |
| 142 | |
| 143 | return monitor_page->latency[monitor_group][monitor_offset]; |
| 144 | } |
| 145 | |
| 146 | static u32 channel_conn_id(struct vmbus_channel *channel, |
| 147 | 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->parameter[monitor_group][monitor_offset].connectionid.u.id; |
| 153 | } |
| 154 | |
| 155 | static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr, |
| 156 | char *buf) |
| 157 | { |
| 158 | struct hv_device *hv_dev = device_to_hv_device(dev); |
| 159 | |
| 160 | if (!hv_dev->channel) |
| 161 | return -ENODEV; |
| 162 | return sysfs_emit(buf, "%d\n", hv_dev->channel->offermsg.child_relid); |
| 163 | } |
| 164 | static DEVICE_ATTR_RO(id); |
| 165 | |
| 166 | static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr, |
| 167 | char *buf) |
| 168 | { |
| 169 | struct hv_device *hv_dev = device_to_hv_device(dev); |
| 170 | |
| 171 | if (!hv_dev->channel) |
| 172 | return -ENODEV; |
| 173 | return sysfs_emit(buf, "%d\n", hv_dev->channel->state); |
| 174 | } |
| 175 | static DEVICE_ATTR_RO(state); |
| 176 | |
| 177 | static ssize_t monitor_id_show(struct device *dev, |
| 178 | struct device_attribute *dev_attr, char *buf) |
| 179 | { |
| 180 | struct hv_device *hv_dev = device_to_hv_device(dev); |
| 181 | |
| 182 | if (!hv_dev->channel) |
| 183 | return -ENODEV; |
| 184 | return sysfs_emit(buf, "%d\n", hv_dev->channel->offermsg.monitorid); |
| 185 | } |
| 186 | static DEVICE_ATTR_RO(monitor_id); |
| 187 | |
| 188 | static ssize_t class_id_show(struct device *dev, |
| 189 | struct device_attribute *dev_attr, char *buf) |
| 190 | { |
| 191 | struct hv_device *hv_dev = device_to_hv_device(dev); |
| 192 | |
| 193 | if (!hv_dev->channel) |
| 194 | return -ENODEV; |
| 195 | return sysfs_emit(buf, "{%pUl}\n", |
| 196 | &hv_dev->channel->offermsg.offer.if_type); |
| 197 | } |
| 198 | static DEVICE_ATTR_RO(class_id); |
| 199 | |
| 200 | static ssize_t device_id_show(struct device *dev, |
| 201 | struct device_attribute *dev_attr, char *buf) |
| 202 | { |
| 203 | struct hv_device *hv_dev = device_to_hv_device(dev); |
| 204 | |
| 205 | if (!hv_dev->channel) |
| 206 | return -ENODEV; |
| 207 | return sysfs_emit(buf, "{%pUl}\n", |
| 208 | &hv_dev->channel->offermsg.offer.if_instance); |
| 209 | } |
| 210 | static DEVICE_ATTR_RO(device_id); |
| 211 | |
| 212 | static ssize_t modalias_show(struct device *dev, |
| 213 | struct device_attribute *dev_attr, char *buf) |
| 214 | { |
| 215 | struct hv_device *hv_dev = device_to_hv_device(dev); |
| 216 | |
| 217 | return sysfs_emit(buf, "vmbus:%*phN\n", UUID_SIZE, &hv_dev->dev_type); |
| 218 | } |
| 219 | static DEVICE_ATTR_RO(modalias); |
| 220 | |
| 221 | #ifdef CONFIG_NUMA |
| 222 | static ssize_t numa_node_show(struct device *dev, |
| 223 | struct device_attribute *attr, char *buf) |
| 224 | { |
| 225 | struct hv_device *hv_dev = device_to_hv_device(dev); |
| 226 | |
| 227 | if (!hv_dev->channel) |
| 228 | return -ENODEV; |
| 229 | |
| 230 | return sysfs_emit(buf, "%d\n", cpu_to_node(hv_dev->channel->target_cpu)); |
| 231 | } |
| 232 | static DEVICE_ATTR_RO(numa_node); |
| 233 | #endif |
| 234 | |
| 235 | static ssize_t server_monitor_pending_show(struct device *dev, |
| 236 | struct device_attribute *dev_attr, |
| 237 | char *buf) |
| 238 | { |
| 239 | struct hv_device *hv_dev = device_to_hv_device(dev); |
| 240 | |
| 241 | if (!hv_dev->channel) |
| 242 | return -ENODEV; |
| 243 | return sysfs_emit(buf, "%d\n", channel_pending(hv_dev->channel, |
| 244 | vmbus_connection.monitor_pages[0])); |
| 245 | } |
| 246 | static DEVICE_ATTR_RO(server_monitor_pending); |
| 247 | |
| 248 | static ssize_t client_monitor_pending_show(struct device *dev, |
| 249 | struct device_attribute *dev_attr, |
| 250 | char *buf) |
| 251 | { |
| 252 | struct hv_device *hv_dev = device_to_hv_device(dev); |
| 253 | |
| 254 | if (!hv_dev->channel) |
| 255 | return -ENODEV; |
| 256 | return sysfs_emit(buf, "%d\n", channel_pending(hv_dev->channel, |
| 257 | vmbus_connection.monitor_pages[1])); |
| 258 | } |
| 259 | static DEVICE_ATTR_RO(client_monitor_pending); |
| 260 | |
| 261 | static ssize_t server_monitor_latency_show(struct device *dev, |
| 262 | struct device_attribute *dev_attr, |
| 263 | char *buf) |
| 264 | { |
| 265 | struct hv_device *hv_dev = device_to_hv_device(dev); |
| 266 | |
| 267 | if (!hv_dev->channel) |
| 268 | return -ENODEV; |
| 269 | return sysfs_emit(buf, "%d\n", channel_latency(hv_dev->channel, |
| 270 | vmbus_connection.monitor_pages[0])); |
| 271 | } |
| 272 | static DEVICE_ATTR_RO(server_monitor_latency); |
| 273 | |
| 274 | static ssize_t client_monitor_latency_show(struct device *dev, |
| 275 | struct device_attribute *dev_attr, |
| 276 | char *buf) |
| 277 | { |
| 278 | struct hv_device *hv_dev = device_to_hv_device(dev); |
| 279 | |
| 280 | if (!hv_dev->channel) |
| 281 | return -ENODEV; |
| 282 | return sysfs_emit(buf, "%d\n", channel_latency(hv_dev->channel, |
| 283 | vmbus_connection.monitor_pages[1])); |
| 284 | } |
| 285 | static DEVICE_ATTR_RO(client_monitor_latency); |
| 286 | |
| 287 | static ssize_t server_monitor_conn_id_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 sysfs_emit(buf, "%d\n", channel_conn_id(hv_dev->channel, |
| 296 | vmbus_connection.monitor_pages[0])); |
| 297 | } |
| 298 | static DEVICE_ATTR_RO(server_monitor_conn_id); |
| 299 | |
| 300 | static ssize_t client_monitor_conn_id_show(struct device *dev, |
| 301 | struct device_attribute *dev_attr, |
| 302 | char *buf) |
| 303 | { |
| 304 | struct hv_device *hv_dev = device_to_hv_device(dev); |
| 305 | |
| 306 | if (!hv_dev->channel) |
| 307 | return -ENODEV; |
| 308 | return sysfs_emit(buf, "%d\n", channel_conn_id(hv_dev->channel, |
| 309 | vmbus_connection.monitor_pages[1])); |
| 310 | } |
| 311 | static DEVICE_ATTR_RO(client_monitor_conn_id); |
| 312 | |
| 313 | static ssize_t out_intr_mask_show(struct device *dev, |
| 314 | struct device_attribute *dev_attr, char *buf) |
| 315 | { |
| 316 | struct hv_device *hv_dev = device_to_hv_device(dev); |
| 317 | struct hv_ring_buffer_debug_info outbound; |
| 318 | int ret; |
| 319 | |
| 320 | if (!hv_dev->channel) |
| 321 | return -ENODEV; |
| 322 | |
| 323 | ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, |
| 324 | &outbound); |
| 325 | if (ret < 0) |
| 326 | return ret; |
| 327 | |
| 328 | return sysfs_emit(buf, "%d\n", outbound.current_interrupt_mask); |
| 329 | } |
| 330 | static DEVICE_ATTR_RO(out_intr_mask); |
| 331 | |
| 332 | static ssize_t out_read_index_show(struct device *dev, |
| 333 | struct device_attribute *dev_attr, char *buf) |
| 334 | { |
| 335 | struct hv_device *hv_dev = device_to_hv_device(dev); |
| 336 | struct hv_ring_buffer_debug_info outbound; |
| 337 | int ret; |
| 338 | |
| 339 | if (!hv_dev->channel) |
| 340 | return -ENODEV; |
| 341 | |
| 342 | ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, |
| 343 | &outbound); |
| 344 | if (ret < 0) |
| 345 | return ret; |
| 346 | return sysfs_emit(buf, "%u\n", outbound.current_read_index); |
| 347 | } |
| 348 | static DEVICE_ATTR_RO(out_read_index); |
| 349 | |
| 350 | static ssize_t out_write_index_show(struct device *dev, |
| 351 | struct device_attribute *dev_attr, |
| 352 | char *buf) |
| 353 | { |
| 354 | struct hv_device *hv_dev = device_to_hv_device(dev); |
| 355 | struct hv_ring_buffer_debug_info outbound; |
| 356 | int ret; |
| 357 | |
| 358 | if (!hv_dev->channel) |
| 359 | return -ENODEV; |
| 360 | |
| 361 | ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, |
| 362 | &outbound); |
| 363 | if (ret < 0) |
| 364 | return ret; |
| 365 | return sysfs_emit(buf, "%u\n", outbound.current_write_index); |
| 366 | } |
| 367 | static DEVICE_ATTR_RO(out_write_index); |
| 368 | |
| 369 | static ssize_t out_read_bytes_avail_show(struct device *dev, |
| 370 | struct device_attribute *dev_attr, |
| 371 | char *buf) |
| 372 | { |
| 373 | struct hv_device *hv_dev = device_to_hv_device(dev); |
| 374 | struct hv_ring_buffer_debug_info outbound; |
| 375 | int ret; |
| 376 | |
| 377 | if (!hv_dev->channel) |
| 378 | return -ENODEV; |
| 379 | |
| 380 | ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, |
| 381 | &outbound); |
| 382 | if (ret < 0) |
| 383 | return ret; |
| 384 | return sysfs_emit(buf, "%d\n", outbound.bytes_avail_toread); |
| 385 | } |
| 386 | static DEVICE_ATTR_RO(out_read_bytes_avail); |
| 387 | |
| 388 | static ssize_t out_write_bytes_avail_show(struct device *dev, |
| 389 | struct device_attribute *dev_attr, |
| 390 | char *buf) |
| 391 | { |
| 392 | struct hv_device *hv_dev = device_to_hv_device(dev); |
| 393 | struct hv_ring_buffer_debug_info outbound; |
| 394 | int ret; |
| 395 | |
| 396 | if (!hv_dev->channel) |
| 397 | return -ENODEV; |
| 398 | |
| 399 | ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, |
| 400 | &outbound); |
| 401 | if (ret < 0) |
| 402 | return ret; |
| 403 | return sysfs_emit(buf, "%d\n", outbound.bytes_avail_towrite); |
| 404 | } |
| 405 | static DEVICE_ATTR_RO(out_write_bytes_avail); |
| 406 | |
| 407 | static ssize_t in_intr_mask_show(struct device *dev, |
| 408 | struct device_attribute *dev_attr, char *buf) |
| 409 | { |
| 410 | struct hv_device *hv_dev = device_to_hv_device(dev); |
| 411 | struct hv_ring_buffer_debug_info inbound; |
| 412 | int ret; |
| 413 | |
| 414 | if (!hv_dev->channel) |
| 415 | return -ENODEV; |
| 416 | |
| 417 | ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound); |
| 418 | if (ret < 0) |
| 419 | return ret; |
| 420 | |
| 421 | return sysfs_emit(buf, "%d\n", inbound.current_interrupt_mask); |
| 422 | } |
| 423 | static DEVICE_ATTR_RO(in_intr_mask); |
| 424 | |
| 425 | static ssize_t in_read_index_show(struct device *dev, |
| 426 | struct device_attribute *dev_attr, char *buf) |
| 427 | { |
| 428 | struct hv_device *hv_dev = device_to_hv_device(dev); |
| 429 | struct hv_ring_buffer_debug_info inbound; |
| 430 | int ret; |
| 431 | |
| 432 | if (!hv_dev->channel) |
| 433 | return -ENODEV; |
| 434 | |
| 435 | ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound); |
| 436 | if (ret < 0) |
| 437 | return ret; |
| 438 | |
| 439 | return sysfs_emit(buf, "%d\n", inbound.current_read_index); |
| 440 | } |
| 441 | static DEVICE_ATTR_RO(in_read_index); |
| 442 | |
| 443 | static ssize_t in_write_index_show(struct device *dev, |
| 444 | struct device_attribute *dev_attr, char *buf) |
| 445 | { |
| 446 | struct hv_device *hv_dev = device_to_hv_device(dev); |
| 447 | struct hv_ring_buffer_debug_info inbound; |
| 448 | int ret; |
| 449 | |
| 450 | if (!hv_dev->channel) |
| 451 | return -ENODEV; |
| 452 | |
| 453 | ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound); |
| 454 | if (ret < 0) |
| 455 | return ret; |
| 456 | |
| 457 | return sysfs_emit(buf, "%d\n", inbound.current_write_index); |
| 458 | } |
| 459 | static DEVICE_ATTR_RO(in_write_index); |
| 460 | |
| 461 | static ssize_t in_read_bytes_avail_show(struct device *dev, |
| 462 | struct device_attribute *dev_attr, |
| 463 | char *buf) |
| 464 | { |
| 465 | struct hv_device *hv_dev = device_to_hv_device(dev); |
| 466 | struct hv_ring_buffer_debug_info inbound; |
| 467 | int ret; |
| 468 | |
| 469 | if (!hv_dev->channel) |
| 470 | return -ENODEV; |
| 471 | |
| 472 | ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound); |
| 473 | if (ret < 0) |
| 474 | return ret; |
| 475 | |
| 476 | return sysfs_emit(buf, "%d\n", inbound.bytes_avail_toread); |
| 477 | } |
| 478 | static DEVICE_ATTR_RO(in_read_bytes_avail); |
| 479 | |
| 480 | static ssize_t in_write_bytes_avail_show(struct device *dev, |
| 481 | struct device_attribute *dev_attr, |
| 482 | char *buf) |
| 483 | { |
| 484 | struct hv_device *hv_dev = device_to_hv_device(dev); |
| 485 | struct hv_ring_buffer_debug_info inbound; |
| 486 | int ret; |
| 487 | |
| 488 | if (!hv_dev->channel) |
| 489 | return -ENODEV; |
| 490 | |
| 491 | ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound); |
| 492 | if (ret < 0) |
| 493 | return ret; |
| 494 | |
| 495 | return sysfs_emit(buf, "%d\n", inbound.bytes_avail_towrite); |
| 496 | } |
| 497 | static DEVICE_ATTR_RO(in_write_bytes_avail); |
| 498 | |
| 499 | static ssize_t channel_vp_mapping_show(struct device *dev, |
| 500 | struct device_attribute *dev_attr, |
| 501 | char *buf) |
| 502 | { |
| 503 | struct hv_device *hv_dev = device_to_hv_device(dev); |
| 504 | struct vmbus_channel *channel = hv_dev->channel, *cur_sc; |
| 505 | int n_written; |
| 506 | struct list_head *cur; |
| 507 | |
| 508 | if (!channel) |
| 509 | return -ENODEV; |
| 510 | |
| 511 | mutex_lock(&vmbus_connection.channel_mutex); |
| 512 | |
| 513 | n_written = sysfs_emit(buf, "%u:%u\n", |
| 514 | channel->offermsg.child_relid, |
| 515 | channel->target_cpu); |
| 516 | |
| 517 | list_for_each(cur, &channel->sc_list) { |
| 518 | |
| 519 | cur_sc = list_entry(cur, struct vmbus_channel, sc_list); |
| 520 | n_written += sysfs_emit_at(buf, n_written, "%u:%u\n", |
| 521 | cur_sc->offermsg.child_relid, |
| 522 | cur_sc->target_cpu); |
| 523 | } |
| 524 | |
| 525 | mutex_unlock(&vmbus_connection.channel_mutex); |
| 526 | |
| 527 | return n_written; |
| 528 | } |
| 529 | static DEVICE_ATTR_RO(channel_vp_mapping); |
| 530 | |
| 531 | static ssize_t vendor_show(struct device *dev, |
| 532 | struct device_attribute *dev_attr, |
| 533 | char *buf) |
| 534 | { |
| 535 | struct hv_device *hv_dev = device_to_hv_device(dev); |
| 536 | |
| 537 | return sysfs_emit(buf, "0x%x\n", hv_dev->vendor_id); |
| 538 | } |
| 539 | static DEVICE_ATTR_RO(vendor); |
| 540 | |
| 541 | static ssize_t device_show(struct device *dev, |
| 542 | struct device_attribute *dev_attr, |
| 543 | char *buf) |
| 544 | { |
| 545 | struct hv_device *hv_dev = device_to_hv_device(dev); |
| 546 | |
| 547 | return sysfs_emit(buf, "0x%x\n", hv_dev->device_id); |
| 548 | } |
| 549 | static DEVICE_ATTR_RO(device); |
| 550 | |
| 551 | /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */ |
| 552 | static struct attribute *vmbus_dev_attrs[] = { |
| 553 | &dev_attr_id.attr, |
| 554 | &dev_attr_state.attr, |
| 555 | &dev_attr_monitor_id.attr, |
| 556 | &dev_attr_class_id.attr, |
| 557 | &dev_attr_device_id.attr, |
| 558 | &dev_attr_modalias.attr, |
| 559 | #ifdef CONFIG_NUMA |
| 560 | &dev_attr_numa_node.attr, |
| 561 | #endif |
| 562 | &dev_attr_server_monitor_pending.attr, |
| 563 | &dev_attr_client_monitor_pending.attr, |
| 564 | &dev_attr_server_monitor_latency.attr, |
| 565 | &dev_attr_client_monitor_latency.attr, |
| 566 | &dev_attr_server_monitor_conn_id.attr, |
| 567 | &dev_attr_client_monitor_conn_id.attr, |
| 568 | &dev_attr_out_intr_mask.attr, |
| 569 | &dev_attr_out_read_index.attr, |
| 570 | &dev_attr_out_write_index.attr, |
| 571 | &dev_attr_out_read_bytes_avail.attr, |
| 572 | &dev_attr_out_write_bytes_avail.attr, |
| 573 | &dev_attr_in_intr_mask.attr, |
| 574 | &dev_attr_in_read_index.attr, |
| 575 | &dev_attr_in_write_index.attr, |
| 576 | &dev_attr_in_read_bytes_avail.attr, |
| 577 | &dev_attr_in_write_bytes_avail.attr, |
| 578 | &dev_attr_channel_vp_mapping.attr, |
| 579 | &dev_attr_vendor.attr, |
| 580 | &dev_attr_device.attr, |
| 581 | NULL, |
| 582 | }; |
| 583 | |
| 584 | /* |
| 585 | * Device-level attribute_group callback function. Returns the permission for |
| 586 | * each attribute, and returns 0 if an attribute is not visible. |
| 587 | */ |
| 588 | static umode_t vmbus_dev_attr_is_visible(struct kobject *kobj, |
| 589 | struct attribute *attr, int idx) |
| 590 | { |
| 591 | struct device *dev = kobj_to_dev(kobj); |
| 592 | const struct hv_device *hv_dev = device_to_hv_device(dev); |
| 593 | |
| 594 | /* Hide the monitor attributes if the monitor mechanism is not used. */ |
| 595 | if (!hv_dev->channel->offermsg.monitor_allocated && |
| 596 | (attr == &dev_attr_monitor_id.attr || |
| 597 | attr == &dev_attr_server_monitor_pending.attr || |
| 598 | attr == &dev_attr_client_monitor_pending.attr || |
| 599 | attr == &dev_attr_server_monitor_latency.attr || |
| 600 | attr == &dev_attr_client_monitor_latency.attr || |
| 601 | attr == &dev_attr_server_monitor_conn_id.attr || |
| 602 | attr == &dev_attr_client_monitor_conn_id.attr)) |
| 603 | return 0; |
| 604 | |
| 605 | return attr->mode; |
| 606 | } |
| 607 | |
| 608 | static const struct attribute_group vmbus_dev_group = { |
| 609 | .attrs = vmbus_dev_attrs, |
| 610 | .is_visible = vmbus_dev_attr_is_visible |
| 611 | }; |
| 612 | __ATTRIBUTE_GROUPS(vmbus_dev); |
| 613 | |
| 614 | /* Set up the attribute for /sys/bus/vmbus/hibernation */ |
| 615 | static ssize_t hibernation_show(const struct bus_type *bus, char *buf) |
| 616 | { |
| 617 | return sprintf(buf, "%d\n", !!hv_is_hibernation_supported()); |
| 618 | } |
| 619 | |
| 620 | static BUS_ATTR_RO(hibernation); |
| 621 | |
| 622 | static struct attribute *vmbus_bus_attrs[] = { |
| 623 | &bus_attr_hibernation.attr, |
| 624 | NULL, |
| 625 | }; |
| 626 | static const struct attribute_group vmbus_bus_group = { |
| 627 | .attrs = vmbus_bus_attrs, |
| 628 | }; |
| 629 | __ATTRIBUTE_GROUPS(vmbus_bus); |
| 630 | |
| 631 | /* |
| 632 | * vmbus_uevent - add uevent for our device |
| 633 | * |
| 634 | * This routine is invoked when a device is added or removed on the vmbus to |
| 635 | * generate a uevent to udev in the userspace. The udev will then look at its |
| 636 | * rule and the uevent generated here to load the appropriate driver |
| 637 | * |
| 638 | * The alias string will be of the form vmbus:guid where guid is the string |
| 639 | * representation of the device guid (each byte of the guid will be |
| 640 | * represented with two hex characters. |
| 641 | */ |
| 642 | static int vmbus_uevent(const struct device *device, struct kobj_uevent_env *env) |
| 643 | { |
| 644 | const struct hv_device *dev = device_to_hv_device(device); |
| 645 | const char *format = "MODALIAS=vmbus:%*phN"; |
| 646 | |
| 647 | return add_uevent_var(env, format, UUID_SIZE, &dev->dev_type); |
| 648 | } |
| 649 | |
| 650 | static const struct hv_vmbus_device_id * |
| 651 | hv_vmbus_dev_match(const struct hv_vmbus_device_id *id, const guid_t *guid) |
| 652 | { |
| 653 | if (id == NULL) |
| 654 | return NULL; /* empty device table */ |
| 655 | |
| 656 | for (; !guid_is_null(&id->guid); id++) |
| 657 | if (guid_equal(&id->guid, guid)) |
| 658 | return id; |
| 659 | |
| 660 | return NULL; |
| 661 | } |
| 662 | |
| 663 | static const struct hv_vmbus_device_id * |
| 664 | hv_vmbus_dynid_match(struct hv_driver *drv, const guid_t *guid) |
| 665 | { |
| 666 | const struct hv_vmbus_device_id *id = NULL; |
| 667 | struct vmbus_dynid *dynid; |
| 668 | |
| 669 | spin_lock(&drv->dynids.lock); |
| 670 | list_for_each_entry(dynid, &drv->dynids.list, node) { |
| 671 | if (guid_equal(&dynid->id.guid, guid)) { |
| 672 | id = &dynid->id; |
| 673 | break; |
| 674 | } |
| 675 | } |
| 676 | spin_unlock(&drv->dynids.lock); |
| 677 | |
| 678 | return id; |
| 679 | } |
| 680 | |
| 681 | static const struct hv_vmbus_device_id vmbus_device_null; |
| 682 | |
| 683 | /* |
| 684 | * Return a matching hv_vmbus_device_id pointer. |
| 685 | * If there is no match, return NULL. |
| 686 | */ |
| 687 | static const struct hv_vmbus_device_id *hv_vmbus_get_id(const struct hv_driver *drv, |
| 688 | struct hv_device *dev) |
| 689 | { |
| 690 | const guid_t *guid = &dev->dev_type; |
| 691 | const struct hv_vmbus_device_id *id; |
| 692 | int ret; |
| 693 | |
| 694 | /* If a driver override is set, only bind to the matching driver */ |
| 695 | ret = device_match_driver_override(&dev->device, &drv->driver); |
| 696 | if (ret == 0) |
| 697 | return NULL; |
| 698 | |
| 699 | /* Look at the dynamic ids first, before the static ones */ |
| 700 | id = hv_vmbus_dynid_match((struct hv_driver *)drv, guid); |
| 701 | if (!id) |
| 702 | id = hv_vmbus_dev_match(drv->id_table, guid); |
| 703 | |
| 704 | /* |
| 705 | * If there's a matching driver override, this function should succeed, |
| 706 | * thus return a dummy device ID if no matching ID is found. |
| 707 | */ |
| 708 | if (!id && ret > 0) |
| 709 | id = &vmbus_device_null; |
| 710 | |
| 711 | return id; |
| 712 | } |
| 713 | |
| 714 | /* vmbus_add_dynid - add a new device ID to this driver and re-probe devices |
| 715 | * |
| 716 | * This function can race with vmbus_device_register(). This function is |
| 717 | * typically running on a user thread in response to writing to the "new_id" |
| 718 | * sysfs entry for a driver. vmbus_device_register() is running on a |
| 719 | * workqueue thread in response to the Hyper-V host offering a device to the |
| 720 | * guest. This function calls driver_attach(), which looks for an existing |
| 721 | * device matching the new id, and attaches the driver to which the new id |
| 722 | * has been assigned. vmbus_device_register() calls device_register(), which |
| 723 | * looks for a driver that matches the device being registered. If both |
| 724 | * operations are running simultaneously, the device driver probe function runs |
| 725 | * on whichever thread establishes the linkage between the driver and device. |
| 726 | * |
| 727 | * In most cases, it doesn't matter which thread runs the driver probe |
| 728 | * function. But if vmbus_device_register() does not find a matching driver, |
| 729 | * it proceeds to create the "channels" subdirectory and numbered per-channel |
| 730 | * subdirectory in sysfs. While that multi-step creation is in progress, this |
| 731 | * function could run the driver probe function. If the probe function checks |
| 732 | * for, or operates on, entries in the "channels" subdirectory, including by |
| 733 | * calling hv_create_ring_sysfs(), the operation may or may not succeed |
| 734 | * depending on the race. The race can't create a kernel failure in VMBus |
| 735 | * or device subsystem code, but probe functions in VMBus drivers doing such |
| 736 | * operations must be prepared for the failure case. |
| 737 | */ |
| 738 | static int vmbus_add_dynid(struct hv_driver *drv, guid_t *guid) |
| 739 | { |
| 740 | struct vmbus_dynid *dynid; |
| 741 | |
| 742 | dynid = kzalloc_obj(*dynid); |
| 743 | if (!dynid) |
| 744 | return -ENOMEM; |
| 745 | |
| 746 | dynid->id.guid = *guid; |
| 747 | |
| 748 | spin_lock(&drv->dynids.lock); |
| 749 | list_add_tail(&dynid->node, &drv->dynids.list); |
| 750 | spin_unlock(&drv->dynids.lock); |
| 751 | |
| 752 | return driver_attach(&drv->driver); |
| 753 | } |
| 754 | |
| 755 | static void vmbus_free_dynids(struct hv_driver *drv) |
| 756 | { |
| 757 | struct vmbus_dynid *dynid, *n; |
| 758 | |
| 759 | spin_lock(&drv->dynids.lock); |
| 760 | list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) { |
| 761 | list_del(&dynid->node); |
| 762 | kfree(dynid); |
| 763 | } |
| 764 | spin_unlock(&drv->dynids.lock); |
| 765 | } |
| 766 | |
| 767 | /* |
| 768 | * store_new_id - sysfs frontend to vmbus_add_dynid() |
| 769 | * |
| 770 | * Allow GUIDs to be added to an existing driver via sysfs. |
| 771 | */ |
| 772 | static ssize_t new_id_store(struct device_driver *driver, const char *buf, |
| 773 | size_t count) |
| 774 | { |
| 775 | struct hv_driver *drv = drv_to_hv_drv(driver); |
| 776 | guid_t guid; |
| 777 | ssize_t retval; |
| 778 | |
| 779 | retval = guid_parse(buf, &guid); |
| 780 | if (retval) |
| 781 | return retval; |
| 782 | |
| 783 | if (hv_vmbus_dynid_match(drv, &guid)) |
| 784 | return -EEXIST; |
| 785 | |
| 786 | retval = vmbus_add_dynid(drv, &guid); |
| 787 | if (retval) |
| 788 | return retval; |
| 789 | return count; |
| 790 | } |
| 791 | static DRIVER_ATTR_WO(new_id); |
| 792 | |
| 793 | /* |
| 794 | * store_remove_id - remove a PCI device ID from this driver |
| 795 | * |
| 796 | * Removes a dynamic pci device ID to this driver. |
| 797 | */ |
| 798 | static ssize_t remove_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 | struct vmbus_dynid *dynid, *n; |
| 803 | guid_t guid; |
| 804 | ssize_t retval; |
| 805 | |
| 806 | retval = guid_parse(buf, &guid); |
| 807 | if (retval) |
| 808 | return retval; |
| 809 | |
| 810 | retval = -ENODEV; |
| 811 | spin_lock(&drv->dynids.lock); |
| 812 | list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) { |
| 813 | struct hv_vmbus_device_id *id = &dynid->id; |
| 814 | |
| 815 | if (guid_equal(&id->guid, &guid)) { |
| 816 | list_del(&dynid->node); |
| 817 | kfree(dynid); |
| 818 | retval = count; |
| 819 | break; |
| 820 | } |
| 821 | } |
| 822 | spin_unlock(&drv->dynids.lock); |
| 823 | |
| 824 | return retval; |
| 825 | } |
| 826 | static DRIVER_ATTR_WO(remove_id); |
| 827 | |
| 828 | static struct attribute *vmbus_drv_attrs[] = { |
| 829 | &driver_attr_new_id.attr, |
| 830 | &driver_attr_remove_id.attr, |
| 831 | NULL, |
| 832 | }; |
| 833 | ATTRIBUTE_GROUPS(vmbus_drv); |
| 834 | |
| 835 | |
| 836 | /* |
| 837 | * vmbus_match - Attempt to match the specified device to the specified driver |
| 838 | */ |
| 839 | static int vmbus_match(struct device *device, const struct device_driver *driver) |
| 840 | { |
| 841 | const struct hv_driver *drv = drv_to_hv_drv(driver); |
| 842 | struct hv_device *hv_dev = device_to_hv_device(device); |
| 843 | |
| 844 | /* The hv_sock driver handles all hv_sock offers. */ |
| 845 | if (is_hvsock_channel(hv_dev->channel)) |
| 846 | return drv->hvsock; |
| 847 | |
| 848 | if (hv_vmbus_get_id(drv, hv_dev)) |
| 849 | return 1; |
| 850 | |
| 851 | return 0; |
| 852 | } |
| 853 | |
| 854 | /* |
| 855 | * vmbus_probe - Add the new vmbus's child device |
| 856 | */ |
| 857 | static int vmbus_probe(struct device *child_device) |
| 858 | { |
| 859 | int ret = 0; |
| 860 | struct hv_driver *drv = |
| 861 | drv_to_hv_drv(child_device->driver); |
| 862 | struct hv_device *dev = device_to_hv_device(child_device); |
| 863 | const struct hv_vmbus_device_id *dev_id; |
| 864 | |
| 865 | dev_id = hv_vmbus_get_id(drv, dev); |
| 866 | if (drv->probe) { |
| 867 | ret = drv->probe(dev, dev_id); |
| 868 | if (ret != 0) |
| 869 | pr_err("probe failed for device %s (%d)\n", |
| 870 | dev_name(child_device), ret); |
| 871 | |
| 872 | } else { |
| 873 | pr_err("probe not set for driver %s\n", |
| 874 | dev_name(child_device)); |
| 875 | ret = -ENODEV; |
| 876 | } |
| 877 | return ret; |
| 878 | } |
| 879 | |
| 880 | /* |
| 881 | * vmbus_dma_configure -- Configure DMA coherence for VMbus device |
| 882 | */ |
| 883 | static int vmbus_dma_configure(struct device *child_device) |
| 884 | { |
| 885 | /* |
| 886 | * On ARM64, propagate the DMA coherence setting from the top level |
| 887 | * VMbus ACPI device to the child VMbus device being added here. |
| 888 | * On x86/x64 coherence is assumed and these calls have no effect. |
| 889 | */ |
| 890 | hv_setup_dma_ops(child_device, |
| 891 | device_get_dma_attr(vmbus_root_device) == DEV_DMA_COHERENT); |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | /* |
| 896 | * vmbus_remove - Remove a vmbus device |
| 897 | */ |
| 898 | static void vmbus_remove(struct device *child_device) |
| 899 | { |
| 900 | struct hv_driver *drv; |
| 901 | struct hv_device *dev = device_to_hv_device(child_device); |
| 902 | |
| 903 | if (child_device->driver) { |
| 904 | drv = drv_to_hv_drv(child_device->driver); |
| 905 | if (drv->remove) |
| 906 | drv->remove(dev); |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | /* |
| 911 | * vmbus_shutdown - Shutdown a vmbus device |
| 912 | */ |
| 913 | static void vmbus_shutdown(struct device *child_device) |
| 914 | { |
| 915 | struct hv_driver *drv; |
| 916 | struct hv_device *dev = device_to_hv_device(child_device); |
| 917 | |
| 918 | |
| 919 | /* The device may not be attached yet */ |
| 920 | if (!child_device->driver) |
| 921 | return; |
| 922 | |
| 923 | drv = drv_to_hv_drv(child_device->driver); |
| 924 | |
| 925 | if (drv->shutdown) |
| 926 | drv->shutdown(dev); |
| 927 | } |
| 928 | |
| 929 | #ifdef CONFIG_PM_SLEEP |
| 930 | /* |
| 931 | * vmbus_suspend - Suspend a vmbus device |
| 932 | */ |
| 933 | static int vmbus_suspend(struct device *child_device) |
| 934 | { |
| 935 | struct hv_driver *drv; |
| 936 | struct hv_device *dev = device_to_hv_device(child_device); |
| 937 | |
| 938 | /* The device may not be attached yet */ |
| 939 | if (!child_device->driver) |
| 940 | return 0; |
| 941 | |
| 942 | drv = drv_to_hv_drv(child_device->driver); |
| 943 | if (!drv->suspend) |
| 944 | return -EOPNOTSUPP; |
| 945 | |
| 946 | return drv->suspend(dev); |
| 947 | } |
| 948 | |
| 949 | /* |
| 950 | * vmbus_resume - Resume a vmbus device |
| 951 | */ |
| 952 | static int vmbus_resume(struct device *child_device) |
| 953 | { |
| 954 | struct hv_driver *drv; |
| 955 | struct hv_device *dev = device_to_hv_device(child_device); |
| 956 | |
| 957 | /* The device may not be attached yet */ |
| 958 | if (!child_device->driver) |
| 959 | return 0; |
| 960 | |
| 961 | drv = drv_to_hv_drv(child_device->driver); |
| 962 | if (!drv->resume) |
| 963 | return -EOPNOTSUPP; |
| 964 | |
| 965 | return drv->resume(dev); |
| 966 | } |
| 967 | #else |
| 968 | #define vmbus_suspend NULL |
| 969 | #define vmbus_resume NULL |
| 970 | #endif /* CONFIG_PM_SLEEP */ |
| 971 | |
| 972 | /* |
| 973 | * vmbus_device_release - Final callback release of the vmbus child device |
| 974 | */ |
| 975 | static void vmbus_device_release(struct device *device) |
| 976 | { |
| 977 | struct hv_device *hv_dev = device_to_hv_device(device); |
| 978 | struct vmbus_channel *channel = hv_dev->channel; |
| 979 | |
| 980 | hv_debug_rm_dev_dir(hv_dev); |
| 981 | |
| 982 | mutex_lock(&vmbus_connection.channel_mutex); |
| 983 | hv_process_channel_removal(channel); |
| 984 | mutex_unlock(&vmbus_connection.channel_mutex); |
| 985 | kfree(hv_dev); |
| 986 | } |
| 987 | |
| 988 | /* |
| 989 | * Note: we must use the "noirq" ops: see the comment before vmbus_bus_pm. |
| 990 | * |
| 991 | * suspend_noirq/resume_noirq are set to NULL to support Suspend-to-Idle: we |
| 992 | * shouldn't suspend the vmbus devices upon Suspend-to-Idle, otherwise there |
| 993 | * is no way to wake up a Generation-2 VM. |
| 994 | * |
| 995 | * The other 4 ops are for hibernation. |
| 996 | */ |
| 997 | |
| 998 | static const struct dev_pm_ops vmbus_pm = { |
| 999 | .suspend_noirq = NULL, |
| 1000 | .resume_noirq = NULL, |
| 1001 | .freeze_noirq = vmbus_suspend, |
| 1002 | .thaw_noirq = vmbus_resume, |
| 1003 | .poweroff_noirq = vmbus_suspend, |
| 1004 | .restore_noirq = vmbus_resume, |
| 1005 | }; |
| 1006 | |
| 1007 | /* The one and only one */ |
| 1008 | static const struct bus_type hv_bus = { |
| 1009 | .name = "vmbus", |
| 1010 | .driver_override = true, |
| 1011 | .match = vmbus_match, |
| 1012 | .shutdown = vmbus_shutdown, |
| 1013 | .remove = vmbus_remove, |
| 1014 | .probe = vmbus_probe, |
| 1015 | .uevent = vmbus_uevent, |
| 1016 | .dma_configure = vmbus_dma_configure, |
| 1017 | .dev_groups = vmbus_dev_groups, |
| 1018 | .drv_groups = vmbus_drv_groups, |
| 1019 | .bus_groups = vmbus_bus_groups, |
| 1020 | .pm = &vmbus_pm, |
| 1021 | }; |
| 1022 | |
| 1023 | struct onmessage_work_context { |
| 1024 | struct work_struct work; |
| 1025 | struct { |
| 1026 | struct hv_message_header header; |
| 1027 | u8 payload[]; |
| 1028 | } msg; |
| 1029 | }; |
| 1030 | |
| 1031 | static void vmbus_onmessage_work(struct work_struct *work) |
| 1032 | { |
| 1033 | struct onmessage_work_context *ctx; |
| 1034 | |
| 1035 | /* Do not process messages if we're in DISCONNECTED state */ |
| 1036 | if (vmbus_connection.conn_state == DISCONNECTED) |
| 1037 | return; |
| 1038 | |
| 1039 | ctx = container_of(work, struct onmessage_work_context, |
| 1040 | work); |
| 1041 | vmbus_onmessage((struct vmbus_channel_message_header *) |
| 1042 | &ctx->msg.payload); |
| 1043 | kfree(ctx); |
| 1044 | } |
| 1045 | |
| 1046 | static void __vmbus_on_msg_dpc(void *message_page_addr) |
| 1047 | { |
| 1048 | struct hv_message msg_copy, *msg; |
| 1049 | struct vmbus_channel_message_header *hdr; |
| 1050 | enum vmbus_channel_message_type msgtype; |
| 1051 | const struct vmbus_channel_message_table_entry *entry; |
| 1052 | struct onmessage_work_context *ctx; |
| 1053 | __u8 payload_size; |
| 1054 | u32 message_type; |
| 1055 | |
| 1056 | if (!message_page_addr) |
| 1057 | return; |
| 1058 | msg = (struct hv_message *)message_page_addr + VMBUS_MESSAGE_SINT; |
| 1059 | |
| 1060 | /* |
| 1061 | * 'enum vmbus_channel_message_type' is supposed to always be 'u32' as |
| 1062 | * it is being used in 'struct vmbus_channel_message_header' definition |
| 1063 | * which is supposed to match hypervisor ABI. |
| 1064 | */ |
| 1065 | BUILD_BUG_ON(sizeof(enum vmbus_channel_message_type) != sizeof(u32)); |
| 1066 | |
| 1067 | /* |
| 1068 | * Since the message is in memory shared with the host, an erroneous or |
| 1069 | * malicious Hyper-V could modify the message while vmbus_on_msg_dpc() |
| 1070 | * or individual message handlers are executing; to prevent this, copy |
| 1071 | * the message into private memory. |
| 1072 | */ |
| 1073 | memcpy(&msg_copy, msg, sizeof(struct hv_message)); |
| 1074 | |
| 1075 | message_type = msg_copy.header.message_type; |
| 1076 | if (message_type == HVMSG_NONE) |
| 1077 | /* no msg */ |
| 1078 | return; |
| 1079 | |
| 1080 | hdr = (struct vmbus_channel_message_header *)msg_copy.u.payload; |
| 1081 | msgtype = hdr->msgtype; |
| 1082 | |
| 1083 | trace_vmbus_on_msg_dpc(hdr); |
| 1084 | |
| 1085 | if (msgtype >= CHANNELMSG_COUNT) { |
| 1086 | WARN_ONCE(1, "unknown msgtype=%d\n", msgtype); |
| 1087 | goto msg_handled; |
| 1088 | } |
| 1089 | |
| 1090 | payload_size = msg_copy.header.payload_size; |
| 1091 | if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT) { |
| 1092 | WARN_ONCE(1, "payload size is too large (%d)\n", payload_size); |
| 1093 | goto msg_handled; |
| 1094 | } |
| 1095 | |
| 1096 | entry = &channel_message_table[msgtype]; |
| 1097 | |
| 1098 | if (!entry->message_handler) |
| 1099 | goto msg_handled; |
| 1100 | |
| 1101 | if (payload_size < entry->min_payload_len) { |
| 1102 | WARN_ONCE(1, "message too short: msgtype=%d len=%d\n", msgtype, payload_size); |
| 1103 | goto msg_handled; |
| 1104 | } |
| 1105 | |
| 1106 | if (entry->handler_type == VMHT_BLOCKING) { |
| 1107 | ctx = kmalloc_flex(*ctx, msg.payload, payload_size, GFP_ATOMIC); |
| 1108 | if (ctx == NULL) |
| 1109 | return; |
| 1110 | |
| 1111 | INIT_WORK(&ctx->work, vmbus_onmessage_work); |
| 1112 | ctx->msg.header = msg_copy.header; |
| 1113 | memcpy(&ctx->msg.payload, msg_copy.u.payload, payload_size); |
| 1114 | |
| 1115 | /* |
| 1116 | * The host can generate a rescind message while we |
| 1117 | * may still be handling the original offer. We deal with |
| 1118 | * this condition by relying on the synchronization provided |
| 1119 | * by offer_in_progress and by channel_mutex. See also the |
| 1120 | * inline comments in vmbus_onoffer_rescind(). |
| 1121 | */ |
| 1122 | switch (msgtype) { |
| 1123 | case CHANNELMSG_RESCIND_CHANNELOFFER: |
| 1124 | /* |
| 1125 | * If we are handling the rescind message; |
| 1126 | * schedule the work on the global work queue. |
| 1127 | * |
| 1128 | * The OFFER message and the RESCIND message should |
| 1129 | * not be handled by the same serialized work queue, |
| 1130 | * because the OFFER handler may call vmbus_open(), |
| 1131 | * which tries to open the channel by sending an |
| 1132 | * OPEN_CHANNEL message to the host and waits for |
| 1133 | * the host's response; however, if the host has |
| 1134 | * rescinded the channel before it receives the |
| 1135 | * OPEN_CHANNEL message, the host just silently |
| 1136 | * ignores the OPEN_CHANNEL message; as a result, |
| 1137 | * the guest's OFFER handler hangs for ever, if we |
| 1138 | * handle the RESCIND message in the same serialized |
| 1139 | * work queue: the RESCIND handler can not start to |
| 1140 | * run before the OFFER handler finishes. |
| 1141 | */ |
| 1142 | if (vmbus_connection.ignore_any_offer_msg) |
| 1143 | break; |
| 1144 | queue_work(vmbus_connection.rescind_work_queue, &ctx->work); |
| 1145 | break; |
| 1146 | |
| 1147 | case CHANNELMSG_OFFERCHANNEL: |
| 1148 | /* |
| 1149 | * The host sends the offer message of a given channel |
| 1150 | * before sending the rescind message of the same |
| 1151 | * channel. These messages are sent to the guest's |
| 1152 | * connect CPU; the guest then starts processing them |
| 1153 | * in the tasklet handler on this CPU: |
| 1154 | * |
| 1155 | * VMBUS_CONNECT_CPU |
| 1156 | * |
| 1157 | * [vmbus_on_msg_dpc()] |
| 1158 | * atomic_inc() // CHANNELMSG_OFFERCHANNEL |
| 1159 | * queue_work() |
| 1160 | * ... |
| 1161 | * [vmbus_on_msg_dpc()] |
| 1162 | * schedule_work() // CHANNELMSG_RESCIND_CHANNELOFFER |
| 1163 | * |
| 1164 | * We rely on the memory-ordering properties of the |
| 1165 | * queue_work() and schedule_work() primitives, which |
| 1166 | * guarantee that the atomic increment will be visible |
| 1167 | * to the CPUs which will execute the offer & rescind |
| 1168 | * works by the time these works will start execution. |
| 1169 | */ |
| 1170 | if (vmbus_connection.ignore_any_offer_msg) |
| 1171 | break; |
| 1172 | atomic_inc(&vmbus_connection.offer_in_progress); |
| 1173 | fallthrough; |
| 1174 | |
| 1175 | default: |
| 1176 | queue_work(vmbus_connection.work_queue, &ctx->work); |
| 1177 | } |
| 1178 | } else |
| 1179 | entry->message_handler(hdr); |
| 1180 | |
| 1181 | msg_handled: |
| 1182 | vmbus_signal_eom(msg, message_type); |
| 1183 | } |
| 1184 | |
| 1185 | void vmbus_on_msg_dpc(unsigned long data) |
| 1186 | { |
| 1187 | struct hv_per_cpu_context *hv_cpu = (void *)data; |
| 1188 | |
| 1189 | __vmbus_on_msg_dpc(hv_cpu->hyp_synic_message_page); |
| 1190 | __vmbus_on_msg_dpc(hv_cpu->para_synic_message_page); |
| 1191 | } |
| 1192 | |
| 1193 | #ifdef CONFIG_PM_SLEEP |
| 1194 | /* |
| 1195 | * Fake RESCIND_CHANNEL messages to clean up hv_sock channels by force for |
| 1196 | * hibernation, because hv_sock connections can not persist across hibernation. |
| 1197 | */ |
| 1198 | static void vmbus_force_channel_rescinded(struct vmbus_channel *channel) |
| 1199 | { |
| 1200 | struct onmessage_work_context *ctx; |
| 1201 | struct vmbus_channel_rescind_offer *rescind; |
| 1202 | |
| 1203 | WARN_ON(!is_hvsock_channel(channel)); |
| 1204 | |
| 1205 | /* |
| 1206 | * Allocation size is small and the allocation should really not fail, |
| 1207 | * otherwise the state of the hv_sock connections ends up in limbo. |
| 1208 | */ |
| 1209 | ctx = kzalloc(sizeof(*ctx) + sizeof(*rescind), |
| 1210 | GFP_KERNEL | __GFP_NOFAIL); |
| 1211 | |
| 1212 | /* |
| 1213 | * So far, these are not really used by Linux. Just set them to the |
| 1214 | * reasonable values conforming to the definitions of the fields. |
| 1215 | */ |
| 1216 | ctx->msg.header.message_type = 1; |
| 1217 | ctx->msg.header.payload_size = sizeof(*rescind); |
| 1218 | |
| 1219 | /* These values are actually used by Linux. */ |
| 1220 | rescind = (struct vmbus_channel_rescind_offer *)ctx->msg.payload; |
| 1221 | rescind->header.msgtype = CHANNELMSG_RESCIND_CHANNELOFFER; |
| 1222 | rescind->child_relid = channel->offermsg.child_relid; |
| 1223 | |
| 1224 | INIT_WORK(&ctx->work, vmbus_onmessage_work); |
| 1225 | |
| 1226 | queue_work(vmbus_connection.work_queue, &ctx->work); |
| 1227 | } |
| 1228 | #endif /* CONFIG_PM_SLEEP */ |
| 1229 | |
| 1230 | /* |
| 1231 | * Schedule all channels with events pending. |
| 1232 | * The event page can be directly checked to get the id of |
| 1233 | * the channel that has the interrupt pending. |
| 1234 | */ |
| 1235 | static void vmbus_chan_sched(void *event_page_addr) |
| 1236 | { |
| 1237 | unsigned long *recv_int_page; |
| 1238 | u32 maxbits, relid; |
| 1239 | union hv_synic_event_flags *event; |
| 1240 | |
| 1241 | if (!event_page_addr) |
| 1242 | return; |
| 1243 | event = (union hv_synic_event_flags *)event_page_addr + VMBUS_MESSAGE_SINT; |
| 1244 | |
| 1245 | maxbits = READ_ONCE(vmbus_connection.relid_hiwater) + 1; |
| 1246 | recv_int_page = event->flags; |
| 1247 | |
| 1248 | if (unlikely(!recv_int_page)) |
| 1249 | return; |
| 1250 | |
| 1251 | for_each_set_bit(relid, recv_int_page, maxbits) { |
| 1252 | void (*callback_fn)(void *context); |
| 1253 | struct vmbus_channel *channel; |
| 1254 | |
| 1255 | if (!sync_test_and_clear_bit(relid, recv_int_page)) |
| 1256 | continue; |
| 1257 | |
| 1258 | /* Special case - vmbus channel protocol msg */ |
| 1259 | if (relid == 0) |
| 1260 | continue; |
| 1261 | |
| 1262 | /* |
| 1263 | * Pairs with the kfree_rcu() in vmbus_chan_release(). |
| 1264 | * Guarantees that the channel data structure doesn't |
| 1265 | * get freed while the channel pointer below is being |
| 1266 | * dereferenced. |
| 1267 | */ |
| 1268 | rcu_read_lock(); |
| 1269 | |
| 1270 | /* Find channel based on relid */ |
| 1271 | channel = relid2channel(relid); |
| 1272 | if (channel == NULL) |
| 1273 | goto sched_unlock_rcu; |
| 1274 | |
| 1275 | if (channel->rescind) |
| 1276 | goto sched_unlock_rcu; |
| 1277 | |
| 1278 | /* |
| 1279 | * Make sure that the ring buffer data structure doesn't get |
| 1280 | * freed while we dereference the ring buffer pointer. Test |
| 1281 | * for the channel's onchannel_callback being NULL within a |
| 1282 | * sched_lock critical section. See also the inline comments |
| 1283 | * in vmbus_reset_channel_cb(). |
| 1284 | */ |
| 1285 | spin_lock(&channel->sched_lock); |
| 1286 | |
| 1287 | callback_fn = channel->onchannel_callback; |
| 1288 | if (unlikely(callback_fn == NULL)) |
| 1289 | goto sched_unlock; |
| 1290 | |
| 1291 | trace_vmbus_chan_sched(channel); |
| 1292 | |
| 1293 | ++channel->interrupts; |
| 1294 | |
| 1295 | switch (channel->callback_mode) { |
| 1296 | case HV_CALL_ISR: |
| 1297 | (*callback_fn)(channel->channel_callback_context); |
| 1298 | break; |
| 1299 | |
| 1300 | case HV_CALL_BATCHED: |
| 1301 | hv_begin_read(&channel->inbound); |
| 1302 | fallthrough; |
| 1303 | case HV_CALL_DIRECT: |
| 1304 | tasklet_schedule(&channel->callback_event); |
| 1305 | } |
| 1306 | |
| 1307 | sched_unlock: |
| 1308 | spin_unlock(&channel->sched_lock); |
| 1309 | sched_unlock_rcu: |
| 1310 | rcu_read_unlock(); |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | static void vmbus_message_sched(struct hv_per_cpu_context *hv_cpu, void *message_page_addr) |
| 1315 | { |
| 1316 | struct hv_message *msg; |
| 1317 | |
| 1318 | if (!message_page_addr) |
| 1319 | return; |
| 1320 | msg = (struct hv_message *)message_page_addr + VMBUS_MESSAGE_SINT; |
| 1321 | |
| 1322 | /* Check if there are actual msgs to be processed */ |
| 1323 | if (msg->header.message_type != HVMSG_NONE) |
| 1324 | tasklet_schedule(&hv_cpu->msg_dpc); |
| 1325 | } |
| 1326 | |
| 1327 | static void __vmbus_isr(void) |
| 1328 | { |
| 1329 | struct hv_per_cpu_context *hv_cpu |
| 1330 | = this_cpu_ptr(hv_context.cpu_context); |
| 1331 | |
| 1332 | vmbus_chan_sched(hv_cpu->hyp_synic_event_page); |
| 1333 | vmbus_chan_sched(hv_cpu->para_synic_event_page); |
| 1334 | |
| 1335 | vmbus_message_sched(hv_cpu, hv_cpu->hyp_synic_message_page); |
| 1336 | vmbus_message_sched(hv_cpu, hv_cpu->para_synic_message_page); |
| 1337 | } |
| 1338 | |
| 1339 | static DEFINE_PER_CPU(bool, vmbus_irq_pending); |
| 1340 | static DEFINE_PER_CPU(struct task_struct *, vmbus_irqd); |
| 1341 | |
| 1342 | static void vmbus_irqd_wake(void) |
| 1343 | { |
| 1344 | struct task_struct *tsk = __this_cpu_read(vmbus_irqd); |
| 1345 | |
| 1346 | __this_cpu_write(vmbus_irq_pending, true); |
| 1347 | wake_up_process(tsk); |
| 1348 | } |
| 1349 | |
| 1350 | static void vmbus_irqd_setup(unsigned int cpu) |
| 1351 | { |
| 1352 | sched_set_fifo(current); |
| 1353 | } |
| 1354 | |
| 1355 | static int vmbus_irqd_should_run(unsigned int cpu) |
| 1356 | { |
| 1357 | return __this_cpu_read(vmbus_irq_pending); |
| 1358 | } |
| 1359 | |
| 1360 | static void run_vmbus_irqd(unsigned int cpu) |
| 1361 | { |
| 1362 | __this_cpu_write(vmbus_irq_pending, false); |
| 1363 | __vmbus_isr(); |
| 1364 | } |
| 1365 | |
| 1366 | static struct smp_hotplug_thread vmbus_irq_threads = { |
| 1367 | .store = &vmbus_irqd, |
| 1368 | .setup = vmbus_irqd_setup, |
| 1369 | .thread_should_run = vmbus_irqd_should_run, |
| 1370 | .thread_fn = run_vmbus_irqd, |
| 1371 | .thread_comm = "vmbus_irq/%u", |
| 1372 | }; |
| 1373 | |
| 1374 | void vmbus_isr(void) |
| 1375 | { |
| 1376 | if (IS_ENABLED(CONFIG_PREEMPT_RT)) { |
| 1377 | vmbus_irqd_wake(); |
| 1378 | } else { |
| 1379 | static DEFINE_WAIT_OVERRIDE_MAP(vmbus_map, LD_WAIT_CONFIG); |
| 1380 | |
| 1381 | /* |
| 1382 | * vmbus_isr is never force-threaded and always invoked at hard |
| 1383 | * IRQ level. __vmbus_isr() below can acquire a spinlock_t |
| 1384 | * which becomes a sleeping lock and must not be acquired in |
| 1385 | * this context. Therefore on PREEMPT_RT this will be threaded |
| 1386 | * via vmbus_irqd_wake(). On non-PREEMPT the annotation lets |
| 1387 | * lockdep know that acquiring a spinlock_t is not an issue. |
| 1388 | */ |
| 1389 | lock_map_acquire_try(&vmbus_map); |
| 1390 | __vmbus_isr(); |
| 1391 | lock_map_release(&vmbus_map); |
| 1392 | } |
| 1393 | } |
| 1394 | EXPORT_SYMBOL_FOR_MODULES(vmbus_isr, "mshv_vtl"); |
| 1395 | |
| 1396 | static irqreturn_t vmbus_percpu_isr(int irq, void *dev_id) |
| 1397 | { |
| 1398 | vmbus_isr(); |
| 1399 | return IRQ_HANDLED; |
| 1400 | } |
| 1401 | |
| 1402 | static void vmbus_percpu_work(struct work_struct *work) |
| 1403 | { |
| 1404 | unsigned int cpu = smp_processor_id(); |
| 1405 | |
| 1406 | hv_synic_init(cpu); |
| 1407 | } |
| 1408 | |
| 1409 | static int vmbus_alloc_synic_and_connect(void) |
| 1410 | { |
| 1411 | int ret, cpu; |
| 1412 | struct work_struct __percpu *works; |
| 1413 | |
| 1414 | ret = hv_synic_alloc(); |
| 1415 | if (ret < 0) |
| 1416 | goto err_alloc; |
| 1417 | |
| 1418 | works = alloc_percpu(struct work_struct); |
| 1419 | if (!works) { |
| 1420 | ret = -ENOMEM; |
| 1421 | goto err_alloc; |
| 1422 | } |
| 1423 | |
| 1424 | /* |
| 1425 | * Initialize the per-cpu interrupt state and stimer state. |
| 1426 | * Then connect to the host. |
| 1427 | */ |
| 1428 | cpus_read_lock(); |
| 1429 | for_each_online_cpu(cpu) { |
| 1430 | struct work_struct *work = per_cpu_ptr(works, cpu); |
| 1431 | |
| 1432 | INIT_WORK(work, vmbus_percpu_work); |
| 1433 | schedule_work_on(cpu, work); |
| 1434 | } |
| 1435 | |
| 1436 | for_each_online_cpu(cpu) |
| 1437 | flush_work(per_cpu_ptr(works, cpu)); |
| 1438 | |
| 1439 | /* Register the callbacks for possible CPU online/offline'ing */ |
| 1440 | ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN, "hyperv/vmbus:online", |
| 1441 | hv_synic_init, hv_synic_cleanup); |
| 1442 | cpus_read_unlock(); |
| 1443 | free_percpu(works); |
| 1444 | if (ret < 0) |
| 1445 | goto err_alloc; |
| 1446 | hyperv_cpuhp_online = ret; |
| 1447 | |
| 1448 | ret = vmbus_connect(); |
| 1449 | if (ret) |
| 1450 | goto err_connect; |
| 1451 | return 0; |
| 1452 | |
| 1453 | err_connect: |
| 1454 | cpuhp_remove_state(hyperv_cpuhp_online); |
| 1455 | return -ENODEV; |
| 1456 | err_alloc: |
| 1457 | hv_synic_free(); |
| 1458 | return -ENOMEM; |
| 1459 | } |
| 1460 | |
| 1461 | /* |
| 1462 | * vmbus_bus_init -Main vmbus driver initialization routine. |
| 1463 | * |
| 1464 | * Here, we |
| 1465 | * - initialize the vmbus driver context |
| 1466 | * - invoke the vmbus hv main init routine |
| 1467 | * - retrieve the channel offers |
| 1468 | */ |
| 1469 | static int vmbus_bus_init(void) |
| 1470 | { |
| 1471 | int ret; |
| 1472 | |
| 1473 | ret = hv_init(); |
| 1474 | if (ret != 0) { |
| 1475 | pr_err("Unable to initialize the hypervisor - 0x%x\n", ret); |
| 1476 | return ret; |
| 1477 | } |
| 1478 | |
| 1479 | ret = bus_register(&hv_bus); |
| 1480 | if (ret) |
| 1481 | return ret; |
| 1482 | |
| 1483 | /* |
| 1484 | * VMbus interrupts are best modeled as per-cpu interrupts. If |
| 1485 | * on an architecture with support for per-cpu IRQs (e.g. ARM64), |
| 1486 | * allocate a per-cpu IRQ using standard Linux kernel functionality. |
| 1487 | * If not on such an architecture (e.g., x86/x64), then rely on |
| 1488 | * code in the arch-specific portion of the code tree to connect |
| 1489 | * the VMbus interrupt handler. |
| 1490 | */ |
| 1491 | |
| 1492 | if (IS_ENABLED(CONFIG_PREEMPT_RT)) { |
| 1493 | ret = smpboot_register_percpu_thread(&vmbus_irq_threads); |
| 1494 | if (ret) |
| 1495 | goto err_kthread; |
| 1496 | } |
| 1497 | |
| 1498 | if (vmbus_irq == -1) { |
| 1499 | hv_setup_vmbus_handler(vmbus_isr); |
| 1500 | } else { |
| 1501 | ret = request_percpu_irq(vmbus_irq, vmbus_percpu_isr, |
| 1502 | "Hyper-V VMbus", &vmbus_evt); |
| 1503 | if (ret) { |
| 1504 | pr_err("Can't request Hyper-V VMbus IRQ %d, Err %d", |
| 1505 | vmbus_irq, ret); |
| 1506 | goto err_setup; |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | /* |
| 1511 | * Cache the value as getting it involves a VM exit on x86(_64), and |
| 1512 | * doing that on each VP while initializing SynIC's wastes time. |
| 1513 | */ |
| 1514 | is_confidential = ms_hyperv.confidential_vmbus_available; |
| 1515 | if (is_confidential) |
| 1516 | pr_info("Establishing connection to the confidential VMBus\n"); |
| 1517 | hv_para_set_sint_proxy(!is_confidential); |
| 1518 | ret = vmbus_alloc_synic_and_connect(); |
| 1519 | if (ret) |
| 1520 | goto err_connect; |
| 1521 | |
| 1522 | /* |
| 1523 | * Always register the vmbus unload panic notifier because we |
| 1524 | * need to shut the VMbus channel connection on panic. |
| 1525 | */ |
| 1526 | atomic_notifier_chain_register(&panic_notifier_list, |
| 1527 | &hyperv_panic_vmbus_unload_block); |
| 1528 | |
| 1529 | vmbus_request_offers(); |
| 1530 | |
| 1531 | return 0; |
| 1532 | |
| 1533 | err_connect: |
| 1534 | if (vmbus_irq == -1) |
| 1535 | hv_remove_vmbus_handler(); |
| 1536 | else |
| 1537 | free_percpu_irq(vmbus_irq, &vmbus_evt); |
| 1538 | err_setup: |
| 1539 | if (IS_ENABLED(CONFIG_PREEMPT_RT)) |
| 1540 | smpboot_unregister_percpu_thread(&vmbus_irq_threads); |
| 1541 | err_kthread: |
| 1542 | bus_unregister(&hv_bus); |
| 1543 | return ret; |
| 1544 | } |
| 1545 | |
| 1546 | /** |
| 1547 | * __vmbus_driver_register() - Register a vmbus's driver |
| 1548 | * @hv_driver: Pointer to driver structure you want to register |
| 1549 | * @owner: owner module of the drv |
| 1550 | * @mod_name: module name string |
| 1551 | * |
| 1552 | * Registers the given driver with Linux through the 'driver_register()' call |
| 1553 | * and sets up the hyper-v vmbus handling for this driver. |
| 1554 | * It will return the state of the 'driver_register()' call. |
| 1555 | * |
| 1556 | */ |
| 1557 | int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name) |
| 1558 | { |
| 1559 | int ret; |
| 1560 | |
| 1561 | if (!hv_vmbus_exists()) |
| 1562 | return -ENODEV; |
| 1563 | |
| 1564 | pr_info("registering driver %s\n", hv_driver->name); |
| 1565 | |
| 1566 | hv_driver->driver.name = hv_driver->name; |
| 1567 | hv_driver->driver.owner = owner; |
| 1568 | hv_driver->driver.mod_name = mod_name; |
| 1569 | hv_driver->driver.bus = &hv_bus; |
| 1570 | |
| 1571 | spin_lock_init(&hv_driver->dynids.lock); |
| 1572 | INIT_LIST_HEAD(&hv_driver->dynids.list); |
| 1573 | |
| 1574 | ret = driver_register(&hv_driver->driver); |
| 1575 | |
| 1576 | return ret; |
| 1577 | } |
| 1578 | EXPORT_SYMBOL_GPL(__vmbus_driver_register); |
| 1579 | |
| 1580 | /** |
| 1581 | * vmbus_driver_unregister() - Unregister a vmbus's driver |
| 1582 | * @hv_driver: Pointer to driver structure you want to |
| 1583 | * un-register |
| 1584 | * |
| 1585 | * Un-register the given driver that was previous registered with a call to |
| 1586 | * vmbus_driver_register() |
| 1587 | */ |
| 1588 | void vmbus_driver_unregister(struct hv_driver *hv_driver) |
| 1589 | { |
| 1590 | if (hv_vmbus_exists()) { |
| 1591 | pr_info("unregistering driver %s\n", hv_driver->name); |
| 1592 | driver_unregister(&hv_driver->driver); |
| 1593 | vmbus_free_dynids(hv_driver); |
| 1594 | } |
| 1595 | } |
| 1596 | EXPORT_SYMBOL_GPL(vmbus_driver_unregister); |
| 1597 | |
| 1598 | |
| 1599 | /* |
| 1600 | * Called when last reference to channel is gone. |
| 1601 | */ |
| 1602 | static void vmbus_chan_release(struct kobject *kobj) |
| 1603 | { |
| 1604 | struct vmbus_channel *channel |
| 1605 | = container_of(kobj, struct vmbus_channel, kobj); |
| 1606 | |
| 1607 | kfree_rcu(channel, rcu); |
| 1608 | } |
| 1609 | |
| 1610 | struct vmbus_chan_attribute { |
| 1611 | struct attribute attr; |
| 1612 | ssize_t (*show)(struct vmbus_channel *chan, char *buf); |
| 1613 | ssize_t (*store)(struct vmbus_channel *chan, |
| 1614 | const char *buf, size_t count); |
| 1615 | }; |
| 1616 | #define VMBUS_CHAN_ATTR(_name, _mode, _show, _store) \ |
| 1617 | struct vmbus_chan_attribute chan_attr_##_name \ |
| 1618 | = __ATTR(_name, _mode, _show, _store) |
| 1619 | #define VMBUS_CHAN_ATTR_RW(_name) \ |
| 1620 | struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RW(_name) |
| 1621 | #define VMBUS_CHAN_ATTR_RO(_name) \ |
| 1622 | struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RO(_name) |
| 1623 | #define VMBUS_CHAN_ATTR_WO(_name) \ |
| 1624 | struct vmbus_chan_attribute chan_attr_##_name = __ATTR_WO(_name) |
| 1625 | |
| 1626 | static ssize_t vmbus_chan_attr_show(struct kobject *kobj, |
| 1627 | struct attribute *attr, char *buf) |
| 1628 | { |
| 1629 | const struct vmbus_chan_attribute *attribute |
| 1630 | = container_of(attr, struct vmbus_chan_attribute, attr); |
| 1631 | struct vmbus_channel *chan |
| 1632 | = container_of(kobj, struct vmbus_channel, kobj); |
| 1633 | |
| 1634 | if (!attribute->show) |
| 1635 | return -EIO; |
| 1636 | |
| 1637 | return attribute->show(chan, buf); |
| 1638 | } |
| 1639 | |
| 1640 | static ssize_t vmbus_chan_attr_store(struct kobject *kobj, |
| 1641 | struct attribute *attr, const char *buf, |
| 1642 | size_t count) |
| 1643 | { |
| 1644 | const struct vmbus_chan_attribute *attribute |
| 1645 | = container_of(attr, struct vmbus_chan_attribute, attr); |
| 1646 | struct vmbus_channel *chan |
| 1647 | = container_of(kobj, struct vmbus_channel, kobj); |
| 1648 | |
| 1649 | if (!attribute->store) |
| 1650 | return -EIO; |
| 1651 | |
| 1652 | return attribute->store(chan, buf, count); |
| 1653 | } |
| 1654 | |
| 1655 | static const struct sysfs_ops vmbus_chan_sysfs_ops = { |
| 1656 | .show = vmbus_chan_attr_show, |
| 1657 | .store = vmbus_chan_attr_store, |
| 1658 | }; |
| 1659 | |
| 1660 | static ssize_t out_mask_show(struct vmbus_channel *channel, char *buf) |
| 1661 | { |
| 1662 | struct hv_ring_buffer_info *rbi = &channel->outbound; |
| 1663 | ssize_t ret; |
| 1664 | |
| 1665 | mutex_lock(&rbi->ring_buffer_mutex); |
| 1666 | if (!rbi->ring_buffer) { |
| 1667 | mutex_unlock(&rbi->ring_buffer_mutex); |
| 1668 | return -EINVAL; |
| 1669 | } |
| 1670 | |
| 1671 | ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask); |
| 1672 | mutex_unlock(&rbi->ring_buffer_mutex); |
| 1673 | return ret; |
| 1674 | } |
| 1675 | static VMBUS_CHAN_ATTR_RO(out_mask); |
| 1676 | |
| 1677 | static ssize_t in_mask_show(struct vmbus_channel *channel, char *buf) |
| 1678 | { |
| 1679 | struct hv_ring_buffer_info *rbi = &channel->inbound; |
| 1680 | ssize_t ret; |
| 1681 | |
| 1682 | mutex_lock(&rbi->ring_buffer_mutex); |
| 1683 | if (!rbi->ring_buffer) { |
| 1684 | mutex_unlock(&rbi->ring_buffer_mutex); |
| 1685 | return -EINVAL; |
| 1686 | } |
| 1687 | |
| 1688 | ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask); |
| 1689 | mutex_unlock(&rbi->ring_buffer_mutex); |
| 1690 | return ret; |
| 1691 | } |
| 1692 | static VMBUS_CHAN_ATTR_RO(in_mask); |
| 1693 | |
| 1694 | static ssize_t read_avail_show(struct vmbus_channel *channel, char *buf) |
| 1695 | { |
| 1696 | struct hv_ring_buffer_info *rbi = &channel->inbound; |
| 1697 | ssize_t ret; |
| 1698 | |
| 1699 | mutex_lock(&rbi->ring_buffer_mutex); |
| 1700 | if (!rbi->ring_buffer) { |
| 1701 | mutex_unlock(&rbi->ring_buffer_mutex); |
| 1702 | return -EINVAL; |
| 1703 | } |
| 1704 | |
| 1705 | ret = sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi)); |
| 1706 | mutex_unlock(&rbi->ring_buffer_mutex); |
| 1707 | return ret; |
| 1708 | } |
| 1709 | static VMBUS_CHAN_ATTR_RO(read_avail); |
| 1710 | |
| 1711 | static ssize_t write_avail_show(struct vmbus_channel *channel, char *buf) |
| 1712 | { |
| 1713 | struct hv_ring_buffer_info *rbi = &channel->outbound; |
| 1714 | ssize_t ret; |
| 1715 | |
| 1716 | mutex_lock(&rbi->ring_buffer_mutex); |
| 1717 | if (!rbi->ring_buffer) { |
| 1718 | mutex_unlock(&rbi->ring_buffer_mutex); |
| 1719 | return -EINVAL; |
| 1720 | } |
| 1721 | |
| 1722 | ret = sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi)); |
| 1723 | mutex_unlock(&rbi->ring_buffer_mutex); |
| 1724 | return ret; |
| 1725 | } |
| 1726 | static VMBUS_CHAN_ATTR_RO(write_avail); |
| 1727 | |
| 1728 | static ssize_t target_cpu_show(struct vmbus_channel *channel, char *buf) |
| 1729 | { |
| 1730 | return sprintf(buf, "%u\n", channel->target_cpu); |
| 1731 | } |
| 1732 | |
| 1733 | int vmbus_channel_set_cpu(struct vmbus_channel *channel, u32 target_cpu) |
| 1734 | { |
| 1735 | u32 origin_cpu; |
| 1736 | int ret = 0; |
| 1737 | |
| 1738 | lockdep_assert_cpus_held(); |
| 1739 | lockdep_assert_held(&vmbus_connection.channel_mutex); |
| 1740 | |
| 1741 | if (vmbus_proto_version < VERSION_WIN10_V4_1) |
| 1742 | return -EIO; |
| 1743 | |
| 1744 | /* Validate target_cpu for the cpumask_test_cpu() operation below. */ |
| 1745 | if (target_cpu >= nr_cpumask_bits) |
| 1746 | return -EINVAL; |
| 1747 | |
| 1748 | if (!cpumask_test_cpu(target_cpu, housekeeping_cpumask(HK_TYPE_MANAGED_IRQ))) |
| 1749 | return -EINVAL; |
| 1750 | |
| 1751 | if (!cpu_online(target_cpu)) |
| 1752 | return -EINVAL; |
| 1753 | |
| 1754 | /* |
| 1755 | * Synchronizes vmbus_channel_set_cpu() and channel closure: |
| 1756 | * |
| 1757 | * { Initially: state = CHANNEL_OPENED } |
| 1758 | * |
| 1759 | * CPU1 CPU2 |
| 1760 | * |
| 1761 | * [vmbus_channel_set_cpu()] [vmbus_disconnect_ring()] |
| 1762 | * |
| 1763 | * LOCK channel_mutex LOCK channel_mutex |
| 1764 | * LOAD r1 = state LOAD r2 = state |
| 1765 | * IF (r1 == CHANNEL_OPENED) IF (r2 == CHANNEL_OPENED) |
| 1766 | * SEND MODIFYCHANNEL STORE state = CHANNEL_OPEN |
| 1767 | * [...] SEND CLOSECHANNEL |
| 1768 | * UNLOCK channel_mutex UNLOCK channel_mutex |
| 1769 | * |
| 1770 | * Forbids: r1 == r2 == CHANNEL_OPENED (i.e., CPU1's LOCK precedes |
| 1771 | * CPU2's LOCK) && CPU2's SEND precedes CPU1's SEND |
| 1772 | * |
| 1773 | * Note. The host processes the channel messages "sequentially", in |
| 1774 | * the order in which they are received on a per-partition basis. |
| 1775 | */ |
| 1776 | |
| 1777 | /* |
| 1778 | * Hyper-V will ignore MODIFYCHANNEL messages for "non-open" channels; |
| 1779 | * avoid sending the message and fail here for such channels. |
| 1780 | */ |
| 1781 | if (channel->state != CHANNEL_OPENED_STATE) { |
| 1782 | ret = -EIO; |
| 1783 | goto end; |
| 1784 | } |
| 1785 | |
| 1786 | origin_cpu = channel->target_cpu; |
| 1787 | if (target_cpu == origin_cpu) |
| 1788 | goto end; |
| 1789 | |
| 1790 | if (vmbus_send_modifychannel(channel, |
| 1791 | hv_cpu_number_to_vp_number(target_cpu))) { |
| 1792 | ret = -EIO; |
| 1793 | goto end; |
| 1794 | } |
| 1795 | |
| 1796 | /* |
| 1797 | * For version before VERSION_WIN10_V5_3, the following warning holds: |
| 1798 | * |
| 1799 | * Warning. At this point, there is *no* guarantee that the host will |
| 1800 | * have successfully processed the vmbus_send_modifychannel() request. |
| 1801 | * See the header comment of vmbus_send_modifychannel() for more info. |
| 1802 | * |
| 1803 | * Lags in the processing of the above vmbus_send_modifychannel() can |
| 1804 | * result in missed interrupts if the "old" target CPU is taken offline |
| 1805 | * before Hyper-V starts sending interrupts to the "new" target CPU. |
| 1806 | * But apart from this offlining scenario, the code tolerates such |
| 1807 | * lags. It will function correctly even if a channel interrupt comes |
| 1808 | * in on a CPU that is different from the channel target_cpu value. |
| 1809 | */ |
| 1810 | |
| 1811 | channel->target_cpu = target_cpu; |
| 1812 | |
| 1813 | /* See init_vp_index(). */ |
| 1814 | if (hv_is_perf_channel(channel)) |
| 1815 | hv_update_allocated_cpus(origin_cpu, target_cpu); |
| 1816 | |
| 1817 | /* Currently set only for storvsc channels. */ |
| 1818 | if (channel->change_target_cpu_callback) { |
| 1819 | (*channel->change_target_cpu_callback)(channel, |
| 1820 | origin_cpu, target_cpu); |
| 1821 | } |
| 1822 | |
| 1823 | end: |
| 1824 | return ret; |
| 1825 | } |
| 1826 | |
| 1827 | static ssize_t target_cpu_store(struct vmbus_channel *channel, |
| 1828 | const char *buf, size_t count) |
| 1829 | { |
| 1830 | u32 target_cpu; |
| 1831 | ssize_t ret; |
| 1832 | |
| 1833 | if (sscanf(buf, "%u", &target_cpu) != 1) |
| 1834 | return -EIO; |
| 1835 | |
| 1836 | cpus_read_lock(); |
| 1837 | mutex_lock(&vmbus_connection.channel_mutex); |
| 1838 | ret = vmbus_channel_set_cpu(channel, target_cpu); |
| 1839 | mutex_unlock(&vmbus_connection.channel_mutex); |
| 1840 | cpus_read_unlock(); |
| 1841 | |
| 1842 | return ret ?: count; |
| 1843 | } |
| 1844 | static VMBUS_CHAN_ATTR(cpu, 0644, target_cpu_show, target_cpu_store); |
| 1845 | |
| 1846 | static ssize_t channel_pending_show(struct vmbus_channel *channel, |
| 1847 | char *buf) |
| 1848 | { |
| 1849 | return sprintf(buf, "%d\n", |
| 1850 | channel_pending(channel, |
| 1851 | vmbus_connection.monitor_pages[1])); |
| 1852 | } |
| 1853 | static VMBUS_CHAN_ATTR(pending, 0444, channel_pending_show, NULL); |
| 1854 | |
| 1855 | static ssize_t channel_latency_show(struct vmbus_channel *channel, |
| 1856 | char *buf) |
| 1857 | { |
| 1858 | return sprintf(buf, "%d\n", |
| 1859 | channel_latency(channel, |
| 1860 | vmbus_connection.monitor_pages[1])); |
| 1861 | } |
| 1862 | static VMBUS_CHAN_ATTR(latency, 0444, channel_latency_show, NULL); |
| 1863 | |
| 1864 | static ssize_t channel_interrupts_show(struct vmbus_channel *channel, char *buf) |
| 1865 | { |
| 1866 | return sprintf(buf, "%llu\n", channel->interrupts); |
| 1867 | } |
| 1868 | static VMBUS_CHAN_ATTR(interrupts, 0444, channel_interrupts_show, NULL); |
| 1869 | |
| 1870 | static ssize_t channel_events_show(struct vmbus_channel *channel, char *buf) |
| 1871 | { |
| 1872 | return sprintf(buf, "%llu\n", channel->sig_events); |
| 1873 | } |
| 1874 | static VMBUS_CHAN_ATTR(events, 0444, channel_events_show, NULL); |
| 1875 | |
| 1876 | static ssize_t channel_intr_in_full_show(struct vmbus_channel *channel, |
| 1877 | char *buf) |
| 1878 | { |
| 1879 | return sprintf(buf, "%llu\n", |
| 1880 | (unsigned long long)channel->intr_in_full); |
| 1881 | } |
| 1882 | static VMBUS_CHAN_ATTR(intr_in_full, 0444, channel_intr_in_full_show, NULL); |
| 1883 | |
| 1884 | static ssize_t channel_intr_out_empty_show(struct vmbus_channel *channel, |
| 1885 | char *buf) |
| 1886 | { |
| 1887 | return sprintf(buf, "%llu\n", |
| 1888 | (unsigned long long)channel->intr_out_empty); |
| 1889 | } |
| 1890 | static VMBUS_CHAN_ATTR(intr_out_empty, 0444, channel_intr_out_empty_show, NULL); |
| 1891 | |
| 1892 | static ssize_t channel_out_full_first_show(struct vmbus_channel *channel, |
| 1893 | char *buf) |
| 1894 | { |
| 1895 | return sprintf(buf, "%llu\n", |
| 1896 | (unsigned long long)channel->out_full_first); |
| 1897 | } |
| 1898 | static VMBUS_CHAN_ATTR(out_full_first, 0444, channel_out_full_first_show, NULL); |
| 1899 | |
| 1900 | static ssize_t channel_out_full_total_show(struct vmbus_channel *channel, |
| 1901 | char *buf) |
| 1902 | { |
| 1903 | return sprintf(buf, "%llu\n", |
| 1904 | (unsigned long long)channel->out_full_total); |
| 1905 | } |
| 1906 | static VMBUS_CHAN_ATTR(out_full_total, 0444, channel_out_full_total_show, NULL); |
| 1907 | |
| 1908 | static ssize_t subchannel_monitor_id_show(struct vmbus_channel *channel, |
| 1909 | char *buf) |
| 1910 | { |
| 1911 | return sprintf(buf, "%u\n", channel->offermsg.monitorid); |
| 1912 | } |
| 1913 | static VMBUS_CHAN_ATTR(monitor_id, 0444, subchannel_monitor_id_show, NULL); |
| 1914 | |
| 1915 | static ssize_t subchannel_id_show(struct vmbus_channel *channel, |
| 1916 | char *buf) |
| 1917 | { |
| 1918 | return sprintf(buf, "%u\n", |
| 1919 | channel->offermsg.offer.sub_channel_index); |
| 1920 | } |
| 1921 | static VMBUS_CHAN_ATTR_RO(subchannel_id); |
| 1922 | |
| 1923 | static int hv_mmap_ring_buffer_wrapper(struct file *filp, struct kobject *kobj, |
| 1924 | const struct bin_attribute *attr, |
| 1925 | struct vm_area_struct *vma) |
| 1926 | { |
| 1927 | struct vmbus_channel *channel = container_of(kobj, struct vmbus_channel, kobj); |
| 1928 | struct vm_area_desc desc; |
| 1929 | int err; |
| 1930 | |
| 1931 | /* |
| 1932 | * hv_(create|remove)_ring_sysfs implementation ensures that |
| 1933 | * mmap_prepare_ring_buffer is not NULL. |
| 1934 | */ |
| 1935 | compat_set_desc_from_vma(&desc, filp, vma); |
| 1936 | err = channel->mmap_prepare_ring_buffer(channel, &desc); |
| 1937 | if (err) |
| 1938 | return err; |
| 1939 | |
| 1940 | return __compat_vma_mmap(&desc, vma); |
| 1941 | } |
| 1942 | |
| 1943 | static struct bin_attribute chan_attr_ring_buffer = { |
| 1944 | .attr = { |
| 1945 | .name = "ring", |
| 1946 | .mode = 0600, |
| 1947 | }, |
| 1948 | .mmap = hv_mmap_ring_buffer_wrapper, |
| 1949 | }; |
| 1950 | static struct attribute *vmbus_chan_attrs[] = { |
| 1951 | &chan_attr_out_mask.attr, |
| 1952 | &chan_attr_in_mask.attr, |
| 1953 | &chan_attr_read_avail.attr, |
| 1954 | &chan_attr_write_avail.attr, |
| 1955 | &chan_attr_cpu.attr, |
| 1956 | &chan_attr_pending.attr, |
| 1957 | &chan_attr_latency.attr, |
| 1958 | &chan_attr_interrupts.attr, |
| 1959 | &chan_attr_events.attr, |
| 1960 | &chan_attr_intr_in_full.attr, |
| 1961 | &chan_attr_intr_out_empty.attr, |
| 1962 | &chan_attr_out_full_first.attr, |
| 1963 | &chan_attr_out_full_total.attr, |
| 1964 | &chan_attr_monitor_id.attr, |
| 1965 | &chan_attr_subchannel_id.attr, |
| 1966 | NULL |
| 1967 | }; |
| 1968 | |
| 1969 | static const struct bin_attribute *vmbus_chan_bin_attrs[] = { |
| 1970 | &chan_attr_ring_buffer, |
| 1971 | NULL |
| 1972 | }; |
| 1973 | |
| 1974 | /* |
| 1975 | * Channel-level attribute_group callback function. Returns the permission for |
| 1976 | * each attribute, and returns 0 if an attribute is not visible. |
| 1977 | */ |
| 1978 | static umode_t vmbus_chan_attr_is_visible(struct kobject *kobj, |
| 1979 | struct attribute *attr, int idx) |
| 1980 | { |
| 1981 | const struct vmbus_channel *channel = |
| 1982 | container_of(kobj, struct vmbus_channel, kobj); |
| 1983 | |
| 1984 | /* Hide the monitor attributes if the monitor mechanism is not used. */ |
| 1985 | if (!channel->offermsg.monitor_allocated && |
| 1986 | (attr == &chan_attr_pending.attr || |
| 1987 | attr == &chan_attr_latency.attr || |
| 1988 | attr == &chan_attr_monitor_id.attr)) |
| 1989 | return 0; |
| 1990 | |
| 1991 | return attr->mode; |
| 1992 | } |
| 1993 | |
| 1994 | static umode_t vmbus_chan_bin_attr_is_visible(struct kobject *kobj, |
| 1995 | const struct bin_attribute *attr, int idx) |
| 1996 | { |
| 1997 | const struct vmbus_channel *channel = |
| 1998 | container_of(kobj, struct vmbus_channel, kobj); |
| 1999 | |
| 2000 | /* Hide ring attribute if channel's ring_sysfs_visible is set to false */ |
| 2001 | if (attr == &chan_attr_ring_buffer && !channel->ring_sysfs_visible) |
| 2002 | return 0; |
| 2003 | |
| 2004 | return attr->attr.mode; |
| 2005 | } |
| 2006 | |
| 2007 | static size_t vmbus_chan_bin_size(struct kobject *kobj, |
| 2008 | const struct bin_attribute *bin_attr, int a) |
| 2009 | { |
| 2010 | const struct vmbus_channel *channel = |
| 2011 | container_of(kobj, struct vmbus_channel, kobj); |
| 2012 | |
| 2013 | return channel->ringbuffer_pagecount << PAGE_SHIFT; |
| 2014 | } |
| 2015 | |
| 2016 | static const struct attribute_group vmbus_chan_group = { |
| 2017 | .attrs = vmbus_chan_attrs, |
| 2018 | .bin_attrs = vmbus_chan_bin_attrs, |
| 2019 | .is_visible = vmbus_chan_attr_is_visible, |
| 2020 | .is_bin_visible = vmbus_chan_bin_attr_is_visible, |
| 2021 | .bin_size = vmbus_chan_bin_size, |
| 2022 | }; |
| 2023 | |
| 2024 | static const struct kobj_type vmbus_chan_ktype = { |
| 2025 | .sysfs_ops = &vmbus_chan_sysfs_ops, |
| 2026 | .release = vmbus_chan_release, |
| 2027 | }; |
| 2028 | |
| 2029 | /** |
| 2030 | * hv_create_ring_sysfs() - create "ring" sysfs entry corresponding to ring buffers for a channel. |
| 2031 | * @channel: Pointer to vmbus_channel structure |
| 2032 | * @hv_mmap_prepare_ring_buffer: function pointer for initializing the function to be called on mmap |
| 2033 | * channel's "ring" sysfs node, which is for the ring buffer of that channel. |
| 2034 | * Function pointer is of below type: |
| 2035 | * int (*hv_mmap_prepare_ring_buffer)(struct vmbus_channel *channel, |
| 2036 | * struct vm_area_desc *desc)) |
| 2037 | * This has a pointer to the channel and a pointer to vm_area_desc, |
| 2038 | * used for mmap_prepare, as arguments. |
| 2039 | * |
| 2040 | * Sysfs node for ring buffer of a channel is created along with other fields, however its |
| 2041 | * visibility is disabled by default. Sysfs creation needs to be controlled when the use-case |
| 2042 | * is running. |
| 2043 | * For example, HV_NIC device is used either by uio_hv_generic or hv_netvsc at any given point of |
| 2044 | * time, and "ring" sysfs is needed only when uio_hv_generic is bound to that device. To avoid |
| 2045 | * exposing the ring buffer by default, this function is responsible to enable visibility of |
| 2046 | * ring for userspace to use. |
| 2047 | * Note: Race conditions can happen with userspace and it is not encouraged to create new |
| 2048 | * use-cases for this. This was added to maintain backward compatibility, while solving |
| 2049 | * one of the race conditions in uio_hv_generic while creating sysfs. See comments with |
| 2050 | * vmbus_add_dynid() and vmbus_device_register(). |
| 2051 | * |
| 2052 | * Returns 0 on success or error code on failure. |
| 2053 | */ |
| 2054 | int hv_create_ring_sysfs(struct vmbus_channel *channel, |
| 2055 | int (*hv_mmap_prepare_ring_buffer)(struct vmbus_channel *channel, |
| 2056 | struct vm_area_desc *desc)) |
| 2057 | { |
| 2058 | struct kobject *kobj = &channel->kobj; |
| 2059 | |
| 2060 | channel->mmap_prepare_ring_buffer = hv_mmap_prepare_ring_buffer; |
| 2061 | channel->ring_sysfs_visible = true; |
| 2062 | |
| 2063 | return sysfs_update_group(kobj, &vmbus_chan_group); |
| 2064 | } |
| 2065 | EXPORT_SYMBOL_GPL(hv_create_ring_sysfs); |
| 2066 | |
| 2067 | /** |
| 2068 | * hv_remove_ring_sysfs() - remove ring sysfs entry corresponding to ring buffers for a channel. |
| 2069 | * @channel: Pointer to vmbus_channel structure |
| 2070 | * |
| 2071 | * Hide "ring" sysfs for a channel by changing its is_visible attribute and updating sysfs group. |
| 2072 | * |
| 2073 | * Returns 0 on success or error code on failure. |
| 2074 | */ |
| 2075 | int hv_remove_ring_sysfs(struct vmbus_channel *channel) |
| 2076 | { |
| 2077 | struct kobject *kobj = &channel->kobj; |
| 2078 | int ret; |
| 2079 | |
| 2080 | channel->ring_sysfs_visible = false; |
| 2081 | ret = sysfs_update_group(kobj, &vmbus_chan_group); |
| 2082 | channel->mmap_prepare_ring_buffer = NULL; |
| 2083 | return ret; |
| 2084 | } |
| 2085 | EXPORT_SYMBOL_GPL(hv_remove_ring_sysfs); |
| 2086 | |
| 2087 | /* |
| 2088 | * vmbus_add_channel_kobj - setup a sub-directory under device/channels |
| 2089 | */ |
| 2090 | int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel) |
| 2091 | { |
| 2092 | const struct device *device = &dev->device; |
| 2093 | struct kobject *kobj = &channel->kobj; |
| 2094 | u32 relid = channel->offermsg.child_relid; |
| 2095 | int ret; |
| 2096 | |
| 2097 | kobj->kset = dev->channels_kset; |
| 2098 | ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL, |
| 2099 | "%u", relid); |
| 2100 | if (ret) { |
| 2101 | kobject_put(kobj); |
| 2102 | return ret; |
| 2103 | } |
| 2104 | |
| 2105 | ret = sysfs_create_group(kobj, &vmbus_chan_group); |
| 2106 | |
| 2107 | if (ret) { |
| 2108 | /* |
| 2109 | * The calling functions' error handling paths will cleanup the |
| 2110 | * empty channel directory. |
| 2111 | */ |
| 2112 | kobject_put(kobj); |
| 2113 | dev_err(device, "Unable to set up channel sysfs files\n"); |
| 2114 | return ret; |
| 2115 | } |
| 2116 | |
| 2117 | kobject_uevent(kobj, KOBJ_ADD); |
| 2118 | |
| 2119 | return 0; |
| 2120 | } |
| 2121 | |
| 2122 | /* |
| 2123 | * vmbus_remove_channel_attr_group - remove the channel's attribute group |
| 2124 | */ |
| 2125 | void vmbus_remove_channel_attr_group(struct vmbus_channel *channel) |
| 2126 | { |
| 2127 | sysfs_remove_group(&channel->kobj, &vmbus_chan_group); |
| 2128 | } |
| 2129 | |
| 2130 | /* |
| 2131 | * vmbus_device_create - Creates and registers a new child device |
| 2132 | * on the vmbus. |
| 2133 | */ |
| 2134 | struct hv_device *vmbus_device_create(const guid_t *type, |
| 2135 | const guid_t *instance, |
| 2136 | struct vmbus_channel *channel) |
| 2137 | { |
| 2138 | struct hv_device *child_device_obj; |
| 2139 | |
| 2140 | child_device_obj = kzalloc_obj(struct hv_device); |
| 2141 | if (!child_device_obj) { |
| 2142 | pr_err("Unable to allocate device object for child device\n"); |
| 2143 | return NULL; |
| 2144 | } |
| 2145 | |
| 2146 | child_device_obj->channel = channel; |
| 2147 | guid_copy(&child_device_obj->dev_type, type); |
| 2148 | guid_copy(&child_device_obj->dev_instance, instance); |
| 2149 | child_device_obj->vendor_id = PCI_VENDOR_ID_MICROSOFT; |
| 2150 | |
| 2151 | return child_device_obj; |
| 2152 | } |
| 2153 | |
| 2154 | /* |
| 2155 | * vmbus_device_register - Register the child device |
| 2156 | */ |
| 2157 | int vmbus_device_register(struct hv_device *child_device_obj) |
| 2158 | { |
| 2159 | struct kobject *kobj = &child_device_obj->device.kobj; |
| 2160 | int ret; |
| 2161 | |
| 2162 | dev_set_name(&child_device_obj->device, "%pUl", |
| 2163 | &child_device_obj->channel->offermsg.offer.if_instance); |
| 2164 | |
| 2165 | child_device_obj->device.bus = &hv_bus; |
| 2166 | child_device_obj->device.parent = vmbus_root_device; |
| 2167 | child_device_obj->device.release = vmbus_device_release; |
| 2168 | |
| 2169 | child_device_obj->device.dma_parms = &child_device_obj->dma_parms; |
| 2170 | child_device_obj->device.dma_mask = &child_device_obj->dma_mask; |
| 2171 | dma_set_mask(&child_device_obj->device, DMA_BIT_MASK(64)); |
| 2172 | dma_set_coherent_mask(&child_device_obj->device, DMA_BIT_MASK(64)); |
| 2173 | |
| 2174 | /* |
| 2175 | * Register with the LDM. This will kick off the driver/device |
| 2176 | * binding...which will eventually call vmbus_match() and vmbus_probe() |
| 2177 | */ |
| 2178 | ret = device_register(&child_device_obj->device); |
| 2179 | if (ret) { |
| 2180 | pr_err("Unable to register child device\n"); |
| 2181 | put_device(&child_device_obj->device); |
| 2182 | return ret; |
| 2183 | } |
| 2184 | |
| 2185 | /* |
| 2186 | * If device_register() found a driver to assign to the device, the |
| 2187 | * driver's probe function has already run at this point. If that |
| 2188 | * probe function accesses or operates on the "channels" subdirectory |
| 2189 | * in sysfs, those operations will have failed because the "channels" |
| 2190 | * subdirectory doesn't exist until the code below runs. Or if the |
| 2191 | * probe function creates a /dev entry, a user space program could |
| 2192 | * find and open the /dev entry, and then create a race by accessing |
| 2193 | * the "channels" subdirectory while the creation steps are in progress |
| 2194 | * here. The race can't result in a kernel failure, but the user space |
| 2195 | * program may get an error in accessing "channels" or its |
| 2196 | * subdirectories. See also comments with vmbus_add_dynid() about a |
| 2197 | * related race condition. |
| 2198 | */ |
| 2199 | child_device_obj->channels_kset = kset_create_and_add("channels", |
| 2200 | NULL, kobj); |
| 2201 | if (!child_device_obj->channels_kset) { |
| 2202 | ret = -ENOMEM; |
| 2203 | goto err_dev_unregister; |
| 2204 | } |
| 2205 | |
| 2206 | ret = vmbus_add_channel_kobj(child_device_obj, |
| 2207 | child_device_obj->channel); |
| 2208 | if (ret) { |
| 2209 | pr_err("Unable to register primary channel\n"); |
| 2210 | goto err_kset_unregister; |
| 2211 | } |
| 2212 | hv_debug_add_dev_dir(child_device_obj); |
| 2213 | |
| 2214 | return 0; |
| 2215 | |
| 2216 | err_kset_unregister: |
| 2217 | kset_unregister(child_device_obj->channels_kset); |
| 2218 | |
| 2219 | err_dev_unregister: |
| 2220 | device_unregister(&child_device_obj->device); |
| 2221 | return ret; |
| 2222 | } |
| 2223 | |
| 2224 | /* |
| 2225 | * vmbus_device_unregister - Remove the specified child device |
| 2226 | * from the vmbus. |
| 2227 | */ |
| 2228 | void vmbus_device_unregister(struct hv_device *device_obj) |
| 2229 | { |
| 2230 | pr_debug("child device %s unregistered\n", |
| 2231 | dev_name(&device_obj->device)); |
| 2232 | |
| 2233 | kset_unregister(device_obj->channels_kset); |
| 2234 | |
| 2235 | /* |
| 2236 | * Kick off the process of unregistering the device. |
| 2237 | * This will call vmbus_remove() and eventually vmbus_device_release() |
| 2238 | */ |
| 2239 | device_unregister(&device_obj->device); |
| 2240 | } |
| 2241 | EXPORT_SYMBOL_GPL(vmbus_device_unregister); |
| 2242 | |
| 2243 | #ifdef CONFIG_ACPI |
| 2244 | /* |
| 2245 | * VMBUS is an acpi enumerated device. Get the information we |
| 2246 | * need from DSDT. |
| 2247 | */ |
| 2248 | static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx) |
| 2249 | { |
| 2250 | resource_size_t start = 0; |
| 2251 | resource_size_t end = 0; |
| 2252 | struct resource *new_res; |
| 2253 | struct resource **old_res = &hyperv_mmio; |
| 2254 | struct resource **prev_res = NULL; |
| 2255 | struct resource r; |
| 2256 | |
| 2257 | switch (res->type) { |
| 2258 | |
| 2259 | /* |
| 2260 | * "Address" descriptors are for bus windows. Ignore |
| 2261 | * "memory" descriptors, which are for registers on |
| 2262 | * devices. |
| 2263 | */ |
| 2264 | case ACPI_RESOURCE_TYPE_ADDRESS32: |
| 2265 | start = res->data.address32.address.minimum; |
| 2266 | end = res->data.address32.address.maximum; |
| 2267 | break; |
| 2268 | |
| 2269 | case ACPI_RESOURCE_TYPE_ADDRESS64: |
| 2270 | start = res->data.address64.address.minimum; |
| 2271 | end = res->data.address64.address.maximum; |
| 2272 | break; |
| 2273 | |
| 2274 | /* |
| 2275 | * The IRQ information is needed only on ARM64, which Hyper-V |
| 2276 | * sets up in the extended format. IRQ information is present |
| 2277 | * on x86/x64 in the non-extended format but it is not used by |
| 2278 | * Linux. So don't bother checking for the non-extended format. |
| 2279 | */ |
| 2280 | case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: |
| 2281 | if (!acpi_dev_resource_interrupt(res, 0, &r)) { |
| 2282 | pr_err("Unable to parse Hyper-V ACPI interrupt\n"); |
| 2283 | return AE_ERROR; |
| 2284 | } |
| 2285 | /* ARM64 INTID for VMbus */ |
| 2286 | vmbus_interrupt = res->data.extended_irq.interrupts[0]; |
| 2287 | /* Linux IRQ number */ |
| 2288 | vmbus_irq = r.start; |
| 2289 | return AE_OK; |
| 2290 | |
| 2291 | default: |
| 2292 | /* Unused resource type */ |
| 2293 | return AE_OK; |
| 2294 | |
| 2295 | } |
| 2296 | /* |
| 2297 | * Ignore ranges that are below 1MB, as they're not |
| 2298 | * necessary or useful here. |
| 2299 | */ |
| 2300 | if (end < 0x100000) |
| 2301 | return AE_OK; |
| 2302 | |
| 2303 | new_res = kzalloc_obj(*new_res, GFP_ATOMIC); |
| 2304 | if (!new_res) |
| 2305 | return AE_NO_MEMORY; |
| 2306 | |
| 2307 | /* If this range overlaps the virtual TPM, truncate it. */ |
| 2308 | if (end >= VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS) |
| 2309 | end = VTPM_BASE_ADDRESS - 1; |
| 2310 | |
| 2311 | new_res->name = "hyperv mmio"; |
| 2312 | new_res->flags = IORESOURCE_MEM; |
| 2313 | new_res->start = start; |
| 2314 | new_res->end = end; |
| 2315 | |
| 2316 | /* |
| 2317 | * If two ranges are adjacent, merge them. |
| 2318 | */ |
| 2319 | do { |
| 2320 | if (!*old_res) { |
| 2321 | *old_res = new_res; |
| 2322 | break; |
| 2323 | } |
| 2324 | |
| 2325 | if (((*old_res)->end + 1) == new_res->start) { |
| 2326 | (*old_res)->end = new_res->end; |
| 2327 | kfree(new_res); |
| 2328 | break; |
| 2329 | } |
| 2330 | |
| 2331 | if ((*old_res)->start == new_res->end + 1) { |
| 2332 | (*old_res)->start = new_res->start; |
| 2333 | kfree(new_res); |
| 2334 | break; |
| 2335 | } |
| 2336 | |
| 2337 | if ((*old_res)->start > new_res->end) { |
| 2338 | new_res->sibling = *old_res; |
| 2339 | if (prev_res) |
| 2340 | (*prev_res)->sibling = new_res; |
| 2341 | *old_res = new_res; |
| 2342 | break; |
| 2343 | } |
| 2344 | |
| 2345 | prev_res = old_res; |
| 2346 | old_res = &(*old_res)->sibling; |
| 2347 | |
| 2348 | } while (1); |
| 2349 | |
| 2350 | return AE_OK; |
| 2351 | } |
| 2352 | #endif |
| 2353 | |
| 2354 | static void vmbus_mmio_remove(void) |
| 2355 | { |
| 2356 | struct resource *cur_res; |
| 2357 | struct resource *next_res; |
| 2358 | |
| 2359 | if (hyperv_mmio) { |
| 2360 | if (fb_mmio) { |
| 2361 | __release_region(hyperv_mmio, fb_mmio->start, |
| 2362 | resource_size(fb_mmio)); |
| 2363 | fb_mmio = NULL; |
| 2364 | } |
| 2365 | |
| 2366 | for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) { |
| 2367 | next_res = cur_res->sibling; |
| 2368 | kfree(cur_res); |
| 2369 | } |
| 2370 | } |
| 2371 | } |
| 2372 | |
| 2373 | static void __maybe_unused vmbus_reserve_fb(void) |
| 2374 | { |
| 2375 | resource_size_t start = 0, size; |
| 2376 | resource_size_t low_mmio_base; |
| 2377 | struct pci_dev *pdev; |
| 2378 | |
| 2379 | if (efi_enabled(EFI_BOOT)) { |
| 2380 | /* Gen2 VM: get FB base from EFI framebuffer */ |
| 2381 | if (IS_ENABLED(CONFIG_SYSFB)) { |
| 2382 | start = sysfb_primary_display.screen.lfb_base; |
| 2383 | size = max_t(__u32, sysfb_primary_display.screen.lfb_size, 0x800000); |
| 2384 | |
| 2385 | low_mmio_base = hyperv_mmio->start; |
| 2386 | if (!low_mmio_base || upper_32_bits(low_mmio_base) || |
| 2387 | (start && start < low_mmio_base)) { |
| 2388 | pr_warn("Unexpected low mmio base %pa\n", &low_mmio_base); |
| 2389 | } else { |
| 2390 | /* |
| 2391 | * If the kdump/kexec or CVM kernel's lfb_base |
| 2392 | * is 0, fall back to the low mmio base. |
| 2393 | */ |
| 2394 | if (!start) |
| 2395 | start = low_mmio_base; |
| 2396 | /* |
| 2397 | * Reserve half of the space below 4GB for high |
| 2398 | * resolutions, but cap the reservation to 128MB. |
| 2399 | */ |
| 2400 | size = min((SZ_4G - start) / 2, SZ_128M); |
| 2401 | } |
| 2402 | } |
| 2403 | } else { |
| 2404 | /* Gen1 VM: get FB base from PCI */ |
| 2405 | pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT, |
| 2406 | PCI_DEVICE_ID_HYPERV_VIDEO, NULL); |
| 2407 | if (!pdev) |
| 2408 | return; |
| 2409 | |
| 2410 | if (pdev->resource[0].flags & IORESOURCE_MEM) { |
| 2411 | start = pci_resource_start(pdev, 0); |
| 2412 | size = pci_resource_len(pdev, 0); |
| 2413 | } |
| 2414 | |
| 2415 | /* |
| 2416 | * Release the PCI device so hyperv_drm driver can grab it |
| 2417 | * later. |
| 2418 | */ |
| 2419 | pci_dev_put(pdev); |
| 2420 | } |
| 2421 | |
| 2422 | if (!start) { |
| 2423 | pr_warn("Unexpected framebuffer mmio base of zero\n"); |
| 2424 | return; |
| 2425 | } |
| 2426 | |
| 2427 | /* |
| 2428 | * Make a claim for the frame buffer in the resource tree under the |
| 2429 | * first node, which will be the one below 4GB. The length seems to |
| 2430 | * be underreported, particularly in a Generation 1 VM. So start out |
| 2431 | * reserving a larger area and make it smaller until it succeeds. |
| 2432 | */ |
| 2433 | for (; !fb_mmio && (size >= 0x100000); size >>= 1) |
| 2434 | fb_mmio = __request_region(hyperv_mmio, start, size, fb_mmio_name, 0); |
| 2435 | |
| 2436 | pr_info("hv_mmio=%pR,%pR fb=%pR\n", hyperv_mmio, hyperv_mmio->sibling, fb_mmio); |
| 2437 | } |
| 2438 | |
| 2439 | /** |
| 2440 | * vmbus_allocate_mmio() - Pick a memory-mapped I/O range. |
| 2441 | * @new: If successful, supplied a pointer to the |
| 2442 | * allocated MMIO space. |
| 2443 | * @device_obj: Identifies the caller |
| 2444 | * @min: Minimum guest physical address of the |
| 2445 | * allocation |
| 2446 | * @max: Maximum guest physical address |
| 2447 | * @size: Size of the range to be allocated |
| 2448 | * @align: Alignment of the range to be allocated |
| 2449 | * @fb_overlap_ok: Whether this allocation can be allowed |
| 2450 | * to overlap the video frame buffer. |
| 2451 | * |
| 2452 | * This function walks the resources granted to VMBus by the |
| 2453 | * _CRS object in the ACPI namespace underneath the parent |
| 2454 | * "bridge" whether that's a root PCI bus in the Generation 1 |
| 2455 | * case or a Module Device in the Generation 2 case. It then |
| 2456 | * attempts to allocate from the global MMIO pool in a way that |
| 2457 | * matches the constraints supplied in these parameters and by |
| 2458 | * that _CRS. |
| 2459 | * |
| 2460 | * Return: 0 on success, -errno on failure |
| 2461 | */ |
| 2462 | int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj, |
| 2463 | resource_size_t min, resource_size_t max, |
| 2464 | resource_size_t size, resource_size_t align, |
| 2465 | bool fb_overlap_ok) |
| 2466 | { |
| 2467 | struct resource *iter, *shadow; |
| 2468 | resource_size_t range_min, range_max, start, end; |
| 2469 | const char *dev_n = dev_name(&device_obj->device); |
| 2470 | int retval; |
| 2471 | |
| 2472 | retval = -ENXIO; |
| 2473 | mutex_lock(&hyperv_mmio_lock); |
| 2474 | |
| 2475 | /* |
| 2476 | * If overlaps with frame buffers are allowed, then first attempt to |
| 2477 | * make the allocation from within the reserved region. Because it |
| 2478 | * is already reserved, no shadow allocation is necessary. |
| 2479 | */ |
| 2480 | if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) && |
| 2481 | !(max < fb_mmio->start)) { |
| 2482 | |
| 2483 | range_min = fb_mmio->start; |
| 2484 | range_max = fb_mmio->end; |
| 2485 | start = (range_min + align - 1) & ~(align - 1); |
| 2486 | for (; start + size - 1 <= range_max; start += align) { |
| 2487 | *new = request_mem_region_exclusive(start, size, dev_n); |
| 2488 | if (*new) { |
| 2489 | retval = 0; |
| 2490 | goto exit; |
| 2491 | } |
| 2492 | } |
| 2493 | } |
| 2494 | |
| 2495 | for (iter = hyperv_mmio; iter; iter = iter->sibling) { |
| 2496 | if ((iter->start >= max) || (iter->end <= min)) |
| 2497 | continue; |
| 2498 | |
| 2499 | range_min = iter->start; |
| 2500 | range_max = iter->end; |
| 2501 | start = (range_min + align - 1) & ~(align - 1); |
| 2502 | for (; start + size - 1 <= range_max; start += align) { |
| 2503 | end = start + size - 1; |
| 2504 | |
| 2505 | /* Skip the whole fb_mmio region if not fb_overlap_ok */ |
| 2506 | if (!fb_overlap_ok && fb_mmio && |
| 2507 | (((start >= fb_mmio->start) && (start <= fb_mmio->end)) || |
| 2508 | ((end >= fb_mmio->start) && (end <= fb_mmio->end)))) |
| 2509 | continue; |
| 2510 | |
| 2511 | shadow = __request_region(iter, start, size, NULL, |
| 2512 | IORESOURCE_BUSY); |
| 2513 | if (!shadow) |
| 2514 | continue; |
| 2515 | |
| 2516 | *new = request_mem_region_exclusive(start, size, dev_n); |
| 2517 | if (*new) { |
| 2518 | shadow->name = (char *)*new; |
| 2519 | retval = 0; |
| 2520 | goto exit; |
| 2521 | } |
| 2522 | |
| 2523 | __release_region(iter, start, size); |
| 2524 | } |
| 2525 | } |
| 2526 | |
| 2527 | exit: |
| 2528 | mutex_unlock(&hyperv_mmio_lock); |
| 2529 | return retval; |
| 2530 | } |
| 2531 | EXPORT_SYMBOL_GPL(vmbus_allocate_mmio); |
| 2532 | |
| 2533 | /** |
| 2534 | * vmbus_free_mmio() - Free a memory-mapped I/O range. |
| 2535 | * @start: Base address of region to release. |
| 2536 | * @size: Size of the range to be allocated |
| 2537 | * |
| 2538 | * This function releases anything requested by |
| 2539 | * vmbus_mmio_allocate(). |
| 2540 | */ |
| 2541 | void vmbus_free_mmio(resource_size_t start, resource_size_t size) |
| 2542 | { |
| 2543 | struct resource *iter; |
| 2544 | |
| 2545 | mutex_lock(&hyperv_mmio_lock); |
| 2546 | |
| 2547 | /* |
| 2548 | * If all bytes of the MMIO range to be released are within the |
| 2549 | * special case fb_mmio shadow region, skip releasing the shadow |
| 2550 | * region since no corresponding __request_region() was done |
| 2551 | * in vmbus_allocate_mmio(). |
| 2552 | */ |
| 2553 | if (fb_mmio && start >= fb_mmio->start && |
| 2554 | (start + size - 1 <= fb_mmio->end)) |
| 2555 | goto skip_shadow_release; |
| 2556 | |
| 2557 | for (iter = hyperv_mmio; iter; iter = iter->sibling) { |
| 2558 | if ((iter->start >= start + size) || (iter->end <= start)) |
| 2559 | continue; |
| 2560 | |
| 2561 | __release_region(iter, start, size); |
| 2562 | } |
| 2563 | |
| 2564 | skip_shadow_release: |
| 2565 | release_mem_region(start, size); |
| 2566 | mutex_unlock(&hyperv_mmio_lock); |
| 2567 | |
| 2568 | } |
| 2569 | EXPORT_SYMBOL_GPL(vmbus_free_mmio); |
| 2570 | |
| 2571 | #ifdef CONFIG_ACPI |
| 2572 | static int vmbus_acpi_add(struct platform_device *pdev) |
| 2573 | { |
| 2574 | acpi_status result; |
| 2575 | int ret_val = -ENODEV; |
| 2576 | struct acpi_device *ancestor; |
| 2577 | struct acpi_device *device = ACPI_COMPANION(&pdev->dev); |
| 2578 | |
| 2579 | vmbus_root_device = &device->dev; |
| 2580 | |
| 2581 | /* |
| 2582 | * Older versions of Hyper-V for ARM64 fail to include the _CCA |
| 2583 | * method on the top level VMbus device in the DSDT. But devices |
| 2584 | * are hardware coherent in all current Hyper-V use cases, so fix |
| 2585 | * up the ACPI device to behave as if _CCA is present and indicates |
| 2586 | * hardware coherence. |
| 2587 | */ |
| 2588 | ACPI_COMPANION_SET(&device->dev, device); |
| 2589 | if (IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED) && |
| 2590 | device_get_dma_attr(&device->dev) == DEV_DMA_NOT_SUPPORTED) { |
| 2591 | pr_info("No ACPI _CCA found; assuming coherent device I/O\n"); |
| 2592 | device->flags.cca_seen = true; |
| 2593 | device->flags.coherent_dma = true; |
| 2594 | } |
| 2595 | |
| 2596 | result = acpi_walk_resources(device->handle, METHOD_NAME__CRS, |
| 2597 | vmbus_walk_resources, NULL); |
| 2598 | |
| 2599 | if (ACPI_FAILURE(result)) |
| 2600 | goto acpi_walk_err; |
| 2601 | /* |
| 2602 | * Some ancestor of the vmbus acpi device (Gen1 or Gen2 |
| 2603 | * firmware) is the VMOD that has the mmio ranges. Get that. |
| 2604 | */ |
| 2605 | for (ancestor = acpi_dev_parent(device); |
| 2606 | ancestor && ancestor->handle != ACPI_ROOT_OBJECT; |
| 2607 | ancestor = acpi_dev_parent(ancestor)) { |
| 2608 | result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS, |
| 2609 | vmbus_walk_resources, NULL); |
| 2610 | |
| 2611 | if (ACPI_FAILURE(result)) |
| 2612 | continue; |
| 2613 | if (hyperv_mmio) { |
| 2614 | vmbus_reserve_fb(); |
| 2615 | break; |
| 2616 | } |
| 2617 | } |
| 2618 | ret_val = 0; |
| 2619 | |
| 2620 | acpi_walk_err: |
| 2621 | if (ret_val) |
| 2622 | vmbus_mmio_remove(); |
| 2623 | return ret_val; |
| 2624 | } |
| 2625 | #else |
| 2626 | static int vmbus_acpi_add(struct platform_device *pdev) |
| 2627 | { |
| 2628 | return 0; |
| 2629 | } |
| 2630 | #endif |
| 2631 | #ifndef HYPERVISOR_CALLBACK_VECTOR |
| 2632 | static int vmbus_set_irq(struct platform_device *pdev) |
| 2633 | { |
| 2634 | struct irq_data *data; |
| 2635 | int irq; |
| 2636 | irq_hw_number_t hwirq; |
| 2637 | |
| 2638 | irq = platform_get_irq(pdev, 0); |
| 2639 | /* platform_get_irq() may not return 0. */ |
| 2640 | if (irq < 0) |
| 2641 | return irq; |
| 2642 | |
| 2643 | data = irq_get_irq_data(irq); |
| 2644 | if (!data) { |
| 2645 | pr_err("No interrupt data for VMBus virq %d\n", irq); |
| 2646 | return -ENODEV; |
| 2647 | } |
| 2648 | hwirq = irqd_to_hwirq(data); |
| 2649 | |
| 2650 | vmbus_irq = irq; |
| 2651 | vmbus_interrupt = hwirq; |
| 2652 | pr_debug("VMBus virq %d, hwirq %d\n", vmbus_irq, vmbus_interrupt); |
| 2653 | |
| 2654 | return 0; |
| 2655 | } |
| 2656 | #endif |
| 2657 | |
| 2658 | static int vmbus_device_add(struct platform_device *pdev) |
| 2659 | { |
| 2660 | struct resource **cur_res = &hyperv_mmio; |
| 2661 | struct of_range range; |
| 2662 | struct of_range_parser parser; |
| 2663 | struct device_node *np = pdev->dev.of_node; |
| 2664 | int ret; |
| 2665 | |
| 2666 | vmbus_root_device = &pdev->dev; |
| 2667 | |
| 2668 | ret = of_range_parser_init(&parser, np); |
| 2669 | if (ret) |
| 2670 | return ret; |
| 2671 | |
| 2672 | #ifndef HYPERVISOR_CALLBACK_VECTOR |
| 2673 | ret = vmbus_set_irq(pdev); |
| 2674 | if (ret) |
| 2675 | return ret; |
| 2676 | #endif |
| 2677 | for_each_of_range(&parser, &range) { |
| 2678 | struct resource *res; |
| 2679 | |
| 2680 | res = kzalloc_obj(*res); |
| 2681 | if (!res) { |
| 2682 | vmbus_mmio_remove(); |
| 2683 | return -ENOMEM; |
| 2684 | } |
| 2685 | |
| 2686 | res->name = "hyperv mmio"; |
| 2687 | res->flags = range.flags; |
| 2688 | res->start = range.cpu_addr; |
| 2689 | res->end = range.cpu_addr + range.size; |
| 2690 | |
| 2691 | *cur_res = res; |
| 2692 | cur_res = &res->sibling; |
| 2693 | } |
| 2694 | |
| 2695 | return ret; |
| 2696 | } |
| 2697 | |
| 2698 | static int vmbus_platform_driver_probe(struct platform_device *pdev) |
| 2699 | { |
| 2700 | if (acpi_disabled) |
| 2701 | return vmbus_device_add(pdev); |
| 2702 | else |
| 2703 | return vmbus_acpi_add(pdev); |
| 2704 | } |
| 2705 | |
| 2706 | static void vmbus_platform_driver_remove(struct platform_device *pdev) |
| 2707 | { |
| 2708 | vmbus_mmio_remove(); |
| 2709 | } |
| 2710 | |
| 2711 | #ifdef CONFIG_PM_SLEEP |
| 2712 | static int vmbus_bus_suspend(struct device *dev) |
| 2713 | { |
| 2714 | struct hv_per_cpu_context *hv_cpu = per_cpu_ptr( |
| 2715 | hv_context.cpu_context, VMBUS_CONNECT_CPU); |
| 2716 | struct vmbus_channel *channel, *sc; |
| 2717 | |
| 2718 | tasklet_disable(&hv_cpu->msg_dpc); |
| 2719 | vmbus_connection.ignore_any_offer_msg = true; |
| 2720 | /* The tasklet_enable() takes care of providing a memory barrier */ |
| 2721 | tasklet_enable(&hv_cpu->msg_dpc); |
| 2722 | |
| 2723 | /* Drain all the workqueues as we are in suspend */ |
| 2724 | drain_workqueue(vmbus_connection.rescind_work_queue); |
| 2725 | drain_workqueue(vmbus_connection.work_queue); |
| 2726 | drain_workqueue(vmbus_connection.handle_primary_chan_wq); |
| 2727 | drain_workqueue(vmbus_connection.handle_sub_chan_wq); |
| 2728 | |
| 2729 | mutex_lock(&vmbus_connection.channel_mutex); |
| 2730 | list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) { |
| 2731 | if (!is_hvsock_channel(channel)) |
| 2732 | continue; |
| 2733 | |
| 2734 | vmbus_force_channel_rescinded(channel); |
| 2735 | } |
| 2736 | mutex_unlock(&vmbus_connection.channel_mutex); |
| 2737 | |
| 2738 | /* |
| 2739 | * Wait until all the sub-channels and hv_sock channels have been |
| 2740 | * cleaned up. Sub-channels should be destroyed upon suspend, otherwise |
| 2741 | * they would conflict with the new sub-channels that will be created |
| 2742 | * in the resume path. hv_sock channels should also be destroyed, but |
| 2743 | * a hv_sock channel of an established hv_sock connection can not be |
| 2744 | * really destroyed since it may still be referenced by the userspace |
| 2745 | * application, so we just force the hv_sock channel to be rescinded |
| 2746 | * by vmbus_force_channel_rescinded(), and the userspace application |
| 2747 | * will thoroughly destroy the channel after hibernation. |
| 2748 | * |
| 2749 | * Note: the counter nr_chan_close_on_suspend may never go above 0 if |
| 2750 | * the VM has no sub-channel and hv_sock channel, e.g. a 1-vCPU VM. |
| 2751 | */ |
| 2752 | if (atomic_read(&vmbus_connection.nr_chan_close_on_suspend) > 0) |
| 2753 | wait_for_completion(&vmbus_connection.ready_for_suspend_event); |
| 2754 | |
| 2755 | mutex_lock(&vmbus_connection.channel_mutex); |
| 2756 | |
| 2757 | list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) { |
| 2758 | /* |
| 2759 | * Remove the channel from the array of channels and invalidate |
| 2760 | * the channel's relid. Upon resume, vmbus_onoffer() will fix |
| 2761 | * up the relid (and other fields, if necessary) and add the |
| 2762 | * channel back to the array. |
| 2763 | */ |
| 2764 | vmbus_channel_unmap_relid(channel); |
| 2765 | channel->offermsg.child_relid = INVALID_RELID; |
| 2766 | |
| 2767 | if (is_hvsock_channel(channel)) { |
| 2768 | if (!channel->rescind) { |
| 2769 | pr_err("hv_sock channel not rescinded!\n"); |
| 2770 | WARN_ON_ONCE(1); |
| 2771 | } |
| 2772 | continue; |
| 2773 | } |
| 2774 | |
| 2775 | list_for_each_entry(sc, &channel->sc_list, sc_list) { |
| 2776 | pr_err("Sub-channel not deleted!\n"); |
| 2777 | WARN_ON_ONCE(1); |
| 2778 | } |
| 2779 | } |
| 2780 | |
| 2781 | mutex_unlock(&vmbus_connection.channel_mutex); |
| 2782 | |
| 2783 | vmbus_initiate_unload(false); |
| 2784 | |
| 2785 | return 0; |
| 2786 | } |
| 2787 | |
| 2788 | static int vmbus_bus_resume(struct device *dev) |
| 2789 | { |
| 2790 | struct vmbus_channel *channel; |
| 2791 | struct vmbus_channel_msginfo *msginfo; |
| 2792 | size_t msgsize; |
| 2793 | int ret; |
| 2794 | |
| 2795 | vmbus_connection.ignore_any_offer_msg = false; |
| 2796 | |
| 2797 | /* |
| 2798 | * We only use the 'vmbus_proto_version', which was in use before |
| 2799 | * hibernation, to re-negotiate with the host. |
| 2800 | */ |
| 2801 | if (!vmbus_proto_version) { |
| 2802 | pr_err("Invalid proto version = 0x%x\n", vmbus_proto_version); |
| 2803 | return -EINVAL; |
| 2804 | } |
| 2805 | |
| 2806 | msgsize = sizeof(*msginfo) + |
| 2807 | sizeof(struct vmbus_channel_initiate_contact); |
| 2808 | |
| 2809 | msginfo = kzalloc(msgsize, GFP_KERNEL); |
| 2810 | |
| 2811 | if (msginfo == NULL) |
| 2812 | return -ENOMEM; |
| 2813 | |
| 2814 | ret = vmbus_negotiate_version(msginfo, vmbus_proto_version); |
| 2815 | |
| 2816 | kfree(msginfo); |
| 2817 | |
| 2818 | if (ret != 0) |
| 2819 | return ret; |
| 2820 | |
| 2821 | vmbus_request_offers(); |
| 2822 | |
| 2823 | mutex_lock(&vmbus_connection.channel_mutex); |
| 2824 | list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) { |
| 2825 | if (channel->offermsg.child_relid != INVALID_RELID) |
| 2826 | continue; |
| 2827 | |
| 2828 | /* hvsock channels are not expected to be present. */ |
| 2829 | if (is_hvsock_channel(channel)) |
| 2830 | continue; |
| 2831 | |
| 2832 | pr_err("channel %pUl/%pUl not present after resume.\n", |
| 2833 | &channel->offermsg.offer.if_type, |
| 2834 | &channel->offermsg.offer.if_instance); |
| 2835 | /* ToDo: Cleanup these channels here */ |
| 2836 | } |
| 2837 | mutex_unlock(&vmbus_connection.channel_mutex); |
| 2838 | |
| 2839 | /* Reset the event for the next suspend. */ |
| 2840 | reinit_completion(&vmbus_connection.ready_for_suspend_event); |
| 2841 | |
| 2842 | return 0; |
| 2843 | } |
| 2844 | #else |
| 2845 | #define vmbus_bus_suspend NULL |
| 2846 | #define vmbus_bus_resume NULL |
| 2847 | #endif /* CONFIG_PM_SLEEP */ |
| 2848 | |
| 2849 | static const __maybe_unused struct of_device_id vmbus_of_match[] = { |
| 2850 | { |
| 2851 | .compatible = "microsoft,vmbus", |
| 2852 | }, |
| 2853 | { |
| 2854 | /* sentinel */ |
| 2855 | }, |
| 2856 | }; |
| 2857 | MODULE_DEVICE_TABLE(of, vmbus_of_match); |
| 2858 | |
| 2859 | static const __maybe_unused struct acpi_device_id vmbus_acpi_device_ids[] = { |
| 2860 | {"VMBUS", 0}, |
| 2861 | {"VMBus", 0}, |
| 2862 | {"", 0}, |
| 2863 | }; |
| 2864 | MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids); |
| 2865 | |
| 2866 | /* |
| 2867 | * Note: we must use the "no_irq" ops, otherwise hibernation can not work with |
| 2868 | * PCI device assignment, because "pci_dev_pm_ops" uses the "noirq" ops: in |
| 2869 | * the resume path, the pci "noirq" restore op runs before "non-noirq" op (see |
| 2870 | * resume_target_kernel() -> dpm_resume_start(), and hibernation_restore() -> |
| 2871 | * dpm_resume_end()). This means vmbus_bus_resume() and the pci-hyperv's |
| 2872 | * resume callback must also run via the "noirq" ops. |
| 2873 | * |
| 2874 | * Set suspend_noirq/resume_noirq to NULL for Suspend-to-Idle: see the comment |
| 2875 | * earlier in this file before vmbus_pm. |
| 2876 | */ |
| 2877 | |
| 2878 | static const struct dev_pm_ops vmbus_bus_pm = { |
| 2879 | .suspend_noirq = NULL, |
| 2880 | .resume_noirq = NULL, |
| 2881 | .freeze_noirq = vmbus_bus_suspend, |
| 2882 | .thaw_noirq = vmbus_bus_resume, |
| 2883 | .poweroff_noirq = vmbus_bus_suspend, |
| 2884 | .restore_noirq = vmbus_bus_resume |
| 2885 | }; |
| 2886 | |
| 2887 | static struct platform_driver vmbus_platform_driver = { |
| 2888 | .probe = vmbus_platform_driver_probe, |
| 2889 | .remove = vmbus_platform_driver_remove, |
| 2890 | .driver = { |
| 2891 | .name = "vmbus", |
| 2892 | .acpi_match_table = ACPI_PTR(vmbus_acpi_device_ids), |
| 2893 | .of_match_table = of_match_ptr(vmbus_of_match), |
| 2894 | .pm = &vmbus_bus_pm, |
| 2895 | .probe_type = PROBE_FORCE_SYNCHRONOUS, |
| 2896 | } |
| 2897 | }; |
| 2898 | |
| 2899 | static void hv_kexec_handler(void) |
| 2900 | { |
| 2901 | vmbus_initiate_unload(false); |
| 2902 | /* Make sure conn_state is set as hv_synic_cleanup checks for it */ |
| 2903 | mb(); |
| 2904 | cpuhp_remove_state(hyperv_cpuhp_online); |
| 2905 | }; |
| 2906 | |
| 2907 | static void hv_crash_handler(struct pt_regs *regs) |
| 2908 | { |
| 2909 | int cpu; |
| 2910 | |
| 2911 | if (!skip_vmbus_unload) |
| 2912 | vmbus_initiate_unload(true); |
| 2913 | /* |
| 2914 | * In crash handler we can't schedule synic cleanup for all CPUs, |
| 2915 | * doing the cleanup for current CPU only. This should be sufficient |
| 2916 | * for kdump. |
| 2917 | */ |
| 2918 | cpu = smp_processor_id(); |
| 2919 | hv_stimer_cleanup(cpu); |
| 2920 | hv_hyp_synic_disable_regs(cpu); |
| 2921 | }; |
| 2922 | |
| 2923 | static int hv_synic_suspend(void *data) |
| 2924 | { |
| 2925 | /* |
| 2926 | * When we reach here, all the non-boot CPUs have been offlined, |
| 2927 | * and their stimers have been cleaned up by the cpuhp teardown |
| 2928 | * callback hv_stimer_cleanup(). |
| 2929 | * |
| 2930 | * hv_synic_suspend() only runs on CPU0 with interrupts disabled. |
| 2931 | * The stimer on CPU0 is not explicitly cleaned up here because: |
| 2932 | * 1) it's harmless as interrupts remain disabled between |
| 2933 | * syscore_suspend() and syscore_resume(), so the stimer cannot |
| 2934 | * fire during this window: see create_image() and |
| 2935 | * resume_target_kernel() |
| 2936 | * 2) the stimer on CPU0 is automatically disabled later by |
| 2937 | * syscore_suspend() -> timekeeping_suspend() -> tick_suspend() -> ... |
| 2938 | * -> clockevents_shutdown() -> ... -> hv_ce_shutdown() |
| 2939 | */ |
| 2940 | |
| 2941 | hv_hyp_synic_disable_regs(0); |
| 2942 | |
| 2943 | return 0; |
| 2944 | } |
| 2945 | |
| 2946 | static void hv_synic_resume(void *data) |
| 2947 | { |
| 2948 | hv_hyp_synic_enable_regs(0); |
| 2949 | |
| 2950 | /* |
| 2951 | * Note: we don't need to call hv_stimer_init(0), because the timer |
| 2952 | * on CPU0 is not unbound in hv_synic_suspend(), and the timer is |
| 2953 | * automatically re-enabled in timekeeping_resume(). |
| 2954 | */ |
| 2955 | } |
| 2956 | |
| 2957 | /* The callbacks run only on CPU0, with irqs_disabled. */ |
| 2958 | static const struct syscore_ops hv_synic_syscore_ops = { |
| 2959 | .suspend = hv_synic_suspend, |
| 2960 | .resume = hv_synic_resume, |
| 2961 | }; |
| 2962 | |
| 2963 | static struct syscore hv_synic_syscore = { |
| 2964 | .ops = &hv_synic_syscore_ops, |
| 2965 | }; |
| 2966 | |
| 2967 | static int __init hv_acpi_init(void) |
| 2968 | { |
| 2969 | int ret; |
| 2970 | |
| 2971 | if (!hv_is_hyperv_initialized()) |
| 2972 | return -ENODEV; |
| 2973 | |
| 2974 | if (hv_root_partition() && !hv_nested) |
| 2975 | /* |
| 2976 | * A non-nested root partition does not need VMBus client |
| 2977 | * functionality. However, the mshv_root module may have |
| 2978 | * a dependency on the VMBus module as described in |
| 2979 | * commit 840b740a35bf. Return success so the module |
| 2980 | * loads even though no VMBus initialization is done. |
| 2981 | */ |
| 2982 | return 0; |
| 2983 | |
| 2984 | /* |
| 2985 | * Get ACPI resources first. |
| 2986 | */ |
| 2987 | ret = platform_driver_register(&vmbus_platform_driver); |
| 2988 | if (ret) |
| 2989 | return ret; |
| 2990 | |
| 2991 | if (!vmbus_root_device) { |
| 2992 | ret = -ENODEV; |
| 2993 | goto cleanup; |
| 2994 | } |
| 2995 | |
| 2996 | /* |
| 2997 | * If we're on an architecture with a hardcoded hypervisor |
| 2998 | * vector (i.e. x86/x64), override the VMbus interrupt found |
| 2999 | * in the ACPI tables. Ensure vmbus_irq is not set since the |
| 3000 | * normal Linux IRQ mechanism is not used in this case. |
| 3001 | */ |
| 3002 | #ifdef HYPERVISOR_CALLBACK_VECTOR |
| 3003 | vmbus_interrupt = HYPERVISOR_CALLBACK_VECTOR; |
| 3004 | vmbus_irq = -1; |
| 3005 | #endif |
| 3006 | |
| 3007 | hv_debug_init(); |
| 3008 | |
| 3009 | ret = vmbus_bus_init(); |
| 3010 | if (ret) |
| 3011 | goto cleanup; |
| 3012 | |
| 3013 | hv_setup_kexec_handler(hv_kexec_handler); |
| 3014 | hv_setup_crash_handler(hv_crash_handler); |
| 3015 | |
| 3016 | register_syscore(&hv_synic_syscore); |
| 3017 | |
| 3018 | return 0; |
| 3019 | |
| 3020 | cleanup: |
| 3021 | platform_driver_unregister(&vmbus_platform_driver); |
| 3022 | vmbus_root_device = NULL; |
| 3023 | return ret; |
| 3024 | } |
| 3025 | |
| 3026 | static void __exit vmbus_exit(void) |
| 3027 | { |
| 3028 | int cpu; |
| 3029 | |
| 3030 | if (hv_root_partition() && !hv_nested) |
| 3031 | /* |
| 3032 | * If a non-nested root partition loaded the VMBus module, |
| 3033 | * hv_acpi_init() did not do any VMBus initialization. |
| 3034 | * There's nothing to clean up, so just return. |
| 3035 | */ |
| 3036 | return; |
| 3037 | |
| 3038 | unregister_syscore(&hv_synic_syscore); |
| 3039 | |
| 3040 | hv_remove_kexec_handler(); |
| 3041 | hv_remove_crash_handler(); |
| 3042 | hv_stimer_global_cleanup(); |
| 3043 | vmbus_disconnect(); |
| 3044 | if (vmbus_irq == -1) |
| 3045 | hv_remove_vmbus_handler(); |
| 3046 | else |
| 3047 | free_percpu_irq(vmbus_irq, &vmbus_evt); |
| 3048 | if (IS_ENABLED(CONFIG_PREEMPT_RT)) |
| 3049 | smpboot_unregister_percpu_thread(&vmbus_irq_threads); |
| 3050 | |
| 3051 | for_each_online_cpu(cpu) { |
| 3052 | struct hv_per_cpu_context *hv_cpu |
| 3053 | = per_cpu_ptr(hv_context.cpu_context, cpu); |
| 3054 | |
| 3055 | tasklet_kill(&hv_cpu->msg_dpc); |
| 3056 | } |
| 3057 | hv_debug_rm_all_dir(); |
| 3058 | |
| 3059 | vmbus_free_channels(); |
| 3060 | kfree(vmbus_connection.channels); |
| 3061 | |
| 3062 | /* |
| 3063 | * The vmbus panic notifier is always registered, hence we should |
| 3064 | * also unconditionally unregister it here as well. |
| 3065 | */ |
| 3066 | atomic_notifier_chain_unregister(&panic_notifier_list, |
| 3067 | &hyperv_panic_vmbus_unload_block); |
| 3068 | |
| 3069 | bus_unregister(&hv_bus); |
| 3070 | |
| 3071 | cpuhp_remove_state(hyperv_cpuhp_online); |
| 3072 | hv_synic_free(); |
| 3073 | platform_driver_unregister(&vmbus_platform_driver); |
| 3074 | } |
| 3075 | |
| 3076 | |
| 3077 | MODULE_LICENSE("GPL"); |
| 3078 | MODULE_DESCRIPTION("Microsoft Hyper-V VMBus Driver"); |
| 3079 | |
| 3080 | subsys_initcall(hv_acpi_init); |
| 3081 | module_exit(vmbus_exit); |