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