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