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