blob: 10dce9f9121689eb32e4c941c6ee8d88927bc5fc [file] [log] [blame]
Thomas Gleixner3b20eb22019-05-29 16:57:35 -07001// SPDX-License-Identifier: GPL-2.0-only
Hank Janssen3e7ee492009-07-13 16:02:34 -07002/*
Hank Janssen3e7ee492009-07-13 16:02:34 -07003 * Copyright (c) 2009, Microsoft Corporation.
4 *
Hank Janssen3e7ee492009-07-13 16:02:34 -07005 * Authors:
6 * Haiyang Zhang <haiyangz@microsoft.com>
7 * Hank Janssen <hjanssen@microsoft.com>
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07008 * K. Y. Srinivasan <kys@microsoft.com>
Hank Janssen3e7ee492009-07-13 16:02:34 -07009 */
Hank Janssen0a466182011-03-29 13:58:47 -070010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Hank Janssen3e7ee492009-07-13 16:02:34 -070012#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/device.h>
Hank Janssen3e7ee492009-07-13 16:02:34 -070015#include <linux/interrupt.h>
16#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -070018#include <linux/acpi.h>
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +000019#include <linux/completion.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070020#include <linux/hyperv.h>
K. Y. Srinivasanb0209502012-12-01 06:46:54 -080021#include <linux/kernel_stat.h>
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -080022#include <linux/clockchips.h>
Vitaly Kuznetsove5132292015-02-27 11:25:51 -080023#include <linux/cpu.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010024#include <linux/sched/task_stack.h>
25
Dexuan Cui1f48dcf2019-09-05 23:01:20 +000026#include <linux/delay.h>
Nick Meier96c1d052015-02-28 11:39:01 -080027#include <linux/notifier.h>
28#include <linux/ptrace.h>
Jake Oshins35464482015-08-05 00:52:37 -070029#include <linux/screen_info.h>
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -070030#include <linux/kdebug.h>
Jake Oshins6d146ae2016-04-05 10:22:55 -070031#include <linux/efi.h>
Stephan Mueller4b44f2d2016-05-02 02:14:34 -040032#include <linux/random.h>
Tianyu Lanf3a99e72020-04-06 08:53:31 -070033#include <linux/kernel.h>
Dexuan Cui63ecc6d2019-09-05 23:01:16 +000034#include <linux/syscore_ops.h>
Michael Kelleyfd1fea62019-07-01 04:25:56 +000035#include <clocksource/hyperv_timer.h>
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070036#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070037
Stephen Hemmingerfc769362016-12-03 12:34:39 -080038struct vmbus_dynid {
39 struct list_head node;
40 struct hv_vmbus_device_id id;
41};
42
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -070043static struct acpi_device *hv_acpi_dev;
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -070044
K. Y. Srinivasan71a66552011-04-29 13:45:04 -070045static struct completion probe_event;
Hank Janssen3e7ee492009-07-13 16:02:34 -070046
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -080047static int hyperv_cpuhp_online;
Nick Meier96c1d052015-02-28 11:39:01 -080048
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +000049static void *hv_panic_page;
50
Michael Kelley626b9012020-08-14 12:45:04 -070051/* Values parsed from ACPI DSDT */
52static int vmbus_irq;
53int vmbus_interrupt;
54
Tianyu Lan040026d2020-04-06 08:53:30 -070055/*
56 * Boolean to control whether to report panic messages over Hyper-V.
57 *
Matheus Castellob18e3582020-11-15 16:57:29 -030058 * It can be set via /proc/sys/kernel/hyperv_record_panic_msg
Tianyu Lan040026d2020-04-06 08:53:30 -070059 */
60static int sysctl_record_panic_msg = 1;
61
62static int hyperv_report_reg(void)
63{
64 return !sysctl_record_panic_msg || !hv_panic_page;
65}
66
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -070067static int hyperv_panic_event(struct notifier_block *nb, unsigned long val,
68 void *args)
69{
70 struct pt_regs *regs;
71
Tianyu Lan74347a92020-04-06 08:53:26 -070072 vmbus_initiate_unload(true);
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -070073
Tianyu Lan73f26e52020-04-06 08:53:28 -070074 /*
75 * Hyper-V should be notified only once about a panic. If we will be
76 * doing hyperv_report_panic_msg() later with kmsg data, don't do
77 * the notification here.
78 */
79 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE
Tianyu Lan040026d2020-04-06 08:53:30 -070080 && hyperv_report_reg()) {
Tianyu Lan74347a92020-04-06 08:53:26 -070081 regs = current_pt_regs();
Tianyu Lanf3a99e72020-04-06 08:53:31 -070082 hyperv_report_panic(regs, val, false);
Tianyu Lan74347a92020-04-06 08:53:26 -070083 }
Nick Meier96c1d052015-02-28 11:39:01 -080084 return NOTIFY_DONE;
85}
86
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -070087static int hyperv_die_event(struct notifier_block *nb, unsigned long val,
88 void *args)
89{
Olaf Hering49971e62020-08-19 11:05:09 +020090 struct die_args *die = args;
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -070091 struct pt_regs *regs = die->regs;
92
Michael Kelley608a9732020-08-06 09:22:15 -070093 /* Don't notify Hyper-V if the die event is other than oops */
94 if (val != DIE_OOPS)
95 return NOTIFY_DONE;
96
Tianyu Lan73f26e52020-04-06 08:53:28 -070097 /*
98 * Hyper-V should be notified only once about a panic. If we will be
99 * doing hyperv_report_panic_msg() later with kmsg data, don't do
100 * the notification here.
101 */
Tianyu Lan040026d2020-04-06 08:53:30 -0700102 if (hyperv_report_reg())
Tianyu Lanf3a99e72020-04-06 08:53:31 -0700103 hyperv_report_panic(regs, val, true);
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -0700104 return NOTIFY_DONE;
105}
106
107static struct notifier_block hyperv_die_block = {
108 .notifier_call = hyperv_die_event,
109};
Nick Meier96c1d052015-02-28 11:39:01 -0800110static struct notifier_block hyperv_panic_block = {
111 .notifier_call = hyperv_panic_event,
112};
113
Jake Oshins6d146ae2016-04-05 10:22:55 -0700114static const char *fb_mmio_name = "fb_range";
115static struct resource *fb_mmio;
Stephen Hemmingere2e80842016-09-07 05:39:33 -0700116static struct resource *hyperv_mmio;
Davidlohr Bueso8aea7f82019-11-01 13:00:04 -0700117static DEFINE_MUTEX(hyperv_mmio_lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700118
K. Y. Srinivasancf6a2ea2011-12-01 09:59:34 -0800119static int vmbus_exists(void)
120{
121 if (hv_acpi_dev == NULL)
122 return -ENODEV;
123
124 return 0;
125}
126
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -0700127static u8 channel_monitor_group(const struct vmbus_channel *channel)
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700128{
129 return (u8)channel->offermsg.monitorid / 32;
130}
131
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -0700132static u8 channel_monitor_offset(const struct vmbus_channel *channel)
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700133{
134 return (u8)channel->offermsg.monitorid % 32;
135}
136
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -0700137static u32 channel_pending(const struct vmbus_channel *channel,
138 const struct hv_monitor_page *monitor_page)
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700139{
140 u8 monitor_group = channel_monitor_group(channel);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -0700141
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700142 return monitor_page->trigger_group[monitor_group].pending;
143}
144
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -0700145static u32 channel_latency(const struct vmbus_channel *channel,
146 const struct hv_monitor_page *monitor_page)
Greg Kroah-Hartman1cee2722013-09-13 11:32:57 -0700147{
148 u8 monitor_group = channel_monitor_group(channel);
149 u8 monitor_offset = channel_monitor_offset(channel);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -0700150
Greg Kroah-Hartman1cee2722013-09-13 11:32:57 -0700151 return monitor_page->latency[monitor_group][monitor_offset];
152}
153
Greg Kroah-Hartman4947c742013-09-13 11:32:58 -0700154static u32 channel_conn_id(struct vmbus_channel *channel,
155 struct hv_monitor_page *monitor_page)
156{
157 u8 monitor_group = channel_monitor_group(channel);
158 u8 monitor_offset = channel_monitor_offset(channel);
Matheus Castelloe4f22122020-11-15 16:57:31 -0300159
Greg Kroah-Hartman4947c742013-09-13 11:32:58 -0700160 return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
161}
162
Greg Kroah-Hartman03f3a912013-09-13 11:32:49 -0700163static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
164 char *buf)
165{
166 struct hv_device *hv_dev = device_to_hv_device(dev);
167
168 if (!hv_dev->channel)
169 return -ENODEV;
170 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
171}
172static DEVICE_ATTR_RO(id);
173
Greg Kroah-Hartmana8fb5f32013-09-13 11:32:50 -0700174static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
175 char *buf)
176{
177 struct hv_device *hv_dev = device_to_hv_device(dev);
178
179 if (!hv_dev->channel)
180 return -ENODEV;
181 return sprintf(buf, "%d\n", hv_dev->channel->state);
182}
183static DEVICE_ATTR_RO(state);
184
Greg Kroah-Hartman5ffd00e2013-09-13 11:32:51 -0700185static ssize_t monitor_id_show(struct device *dev,
186 struct device_attribute *dev_attr, char *buf)
187{
188 struct hv_device *hv_dev = device_to_hv_device(dev);
189
190 if (!hv_dev->channel)
191 return -ENODEV;
192 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
193}
194static DEVICE_ATTR_RO(monitor_id);
195
Greg Kroah-Hartman68234c02013-09-13 11:32:53 -0700196static ssize_t class_id_show(struct device *dev,
197 struct device_attribute *dev_attr, char *buf)
198{
199 struct hv_device *hv_dev = device_to_hv_device(dev);
200
201 if (!hv_dev->channel)
202 return -ENODEV;
203 return sprintf(buf, "{%pUl}\n",
Andy Shevchenko458c4472020-04-23 16:45:03 +0300204 &hv_dev->channel->offermsg.offer.if_type);
Greg Kroah-Hartman68234c02013-09-13 11:32:53 -0700205}
206static DEVICE_ATTR_RO(class_id);
207
Greg Kroah-Hartman7c55e1d2013-09-13 11:32:54 -0700208static ssize_t device_id_show(struct device *dev,
209 struct device_attribute *dev_attr, char *buf)
210{
211 struct hv_device *hv_dev = device_to_hv_device(dev);
212
213 if (!hv_dev->channel)
214 return -ENODEV;
215 return sprintf(buf, "{%pUl}\n",
Andy Shevchenko458c4472020-04-23 16:45:03 +0300216 &hv_dev->channel->offermsg.offer.if_instance);
Greg Kroah-Hartman7c55e1d2013-09-13 11:32:54 -0700217}
218static DEVICE_ATTR_RO(device_id);
219
Greg Kroah-Hartman647fa372013-09-13 11:32:52 -0700220static ssize_t modalias_show(struct device *dev,
221 struct device_attribute *dev_attr, char *buf)
222{
223 struct hv_device *hv_dev = device_to_hv_device(dev);
Greg Kroah-Hartman647fa372013-09-13 11:32:52 -0700224
Andy Shevchenko0027e3f2020-04-23 16:45:04 +0300225 return sprintf(buf, "vmbus:%*phN\n", UUID_SIZE, &hv_dev->dev_type);
Greg Kroah-Hartman647fa372013-09-13 11:32:52 -0700226}
227static DEVICE_ATTR_RO(modalias);
228
Stephen Hemminger7ceb1c32018-07-28 21:58:48 +0000229#ifdef CONFIG_NUMA
230static ssize_t numa_node_show(struct device *dev,
231 struct device_attribute *attr, char *buf)
232{
233 struct hv_device *hv_dev = device_to_hv_device(dev);
234
235 if (!hv_dev->channel)
236 return -ENODEV;
237
Andrea Parri (Microsoft)458d0902020-06-17 18:46:36 +0200238 return sprintf(buf, "%d\n", cpu_to_node(hv_dev->channel->target_cpu));
Stephen Hemminger7ceb1c32018-07-28 21:58:48 +0000239}
240static DEVICE_ATTR_RO(numa_node);
241#endif
242
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700243static ssize_t server_monitor_pending_show(struct device *dev,
244 struct device_attribute *dev_attr,
245 char *buf)
246{
247 struct hv_device *hv_dev = device_to_hv_device(dev);
248
249 if (!hv_dev->channel)
250 return -ENODEV;
251 return sprintf(buf, "%d\n",
252 channel_pending(hv_dev->channel,
Kimberly Brownfd8e3c32019-02-19 00:38:06 -0500253 vmbus_connection.monitor_pages[0]));
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700254}
255static DEVICE_ATTR_RO(server_monitor_pending);
256
257static ssize_t client_monitor_pending_show(struct device *dev,
258 struct device_attribute *dev_attr,
259 char *buf)
260{
261 struct hv_device *hv_dev = device_to_hv_device(dev);
262
263 if (!hv_dev->channel)
264 return -ENODEV;
265 return sprintf(buf, "%d\n",
266 channel_pending(hv_dev->channel,
267 vmbus_connection.monitor_pages[1]));
268}
269static DEVICE_ATTR_RO(client_monitor_pending);
Greg Kroah-Hartman68234c02013-09-13 11:32:53 -0700270
Greg Kroah-Hartman1cee2722013-09-13 11:32:57 -0700271static ssize_t server_monitor_latency_show(struct device *dev,
272 struct device_attribute *dev_attr,
273 char *buf)
274{
275 struct hv_device *hv_dev = device_to_hv_device(dev);
276
277 if (!hv_dev->channel)
278 return -ENODEV;
279 return sprintf(buf, "%d\n",
280 channel_latency(hv_dev->channel,
281 vmbus_connection.monitor_pages[0]));
282}
283static DEVICE_ATTR_RO(server_monitor_latency);
284
285static ssize_t client_monitor_latency_show(struct device *dev,
286 struct device_attribute *dev_attr,
287 char *buf)
288{
289 struct hv_device *hv_dev = device_to_hv_device(dev);
290
291 if (!hv_dev->channel)
292 return -ENODEV;
293 return sprintf(buf, "%d\n",
294 channel_latency(hv_dev->channel,
295 vmbus_connection.monitor_pages[1]));
296}
297static DEVICE_ATTR_RO(client_monitor_latency);
298
Greg Kroah-Hartman4947c742013-09-13 11:32:58 -0700299static ssize_t server_monitor_conn_id_show(struct device *dev,
300 struct device_attribute *dev_attr,
301 char *buf)
302{
303 struct hv_device *hv_dev = device_to_hv_device(dev);
304
305 if (!hv_dev->channel)
306 return -ENODEV;
307 return sprintf(buf, "%d\n",
308 channel_conn_id(hv_dev->channel,
309 vmbus_connection.monitor_pages[0]));
310}
311static DEVICE_ATTR_RO(server_monitor_conn_id);
312
313static ssize_t client_monitor_conn_id_show(struct device *dev,
314 struct device_attribute *dev_attr,
315 char *buf)
316{
317 struct hv_device *hv_dev = device_to_hv_device(dev);
318
319 if (!hv_dev->channel)
320 return -ENODEV;
321 return sprintf(buf, "%d\n",
322 channel_conn_id(hv_dev->channel,
323 vmbus_connection.monitor_pages[1]));
324}
325static DEVICE_ATTR_RO(client_monitor_conn_id);
326
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700327static ssize_t out_intr_mask_show(struct device *dev,
328 struct device_attribute *dev_attr, char *buf)
329{
330 struct hv_device *hv_dev = device_to_hv_device(dev);
331 struct hv_ring_buffer_debug_info outbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000332 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700333
334 if (!hv_dev->channel)
335 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000336
337 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
338 &outbound);
339 if (ret < 0)
340 return ret;
341
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700342 return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
343}
344static DEVICE_ATTR_RO(out_intr_mask);
345
346static ssize_t out_read_index_show(struct device *dev,
347 struct device_attribute *dev_attr, char *buf)
348{
349 struct hv_device *hv_dev = device_to_hv_device(dev);
350 struct hv_ring_buffer_debug_info outbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000351 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700352
353 if (!hv_dev->channel)
354 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000355
356 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
357 &outbound);
358 if (ret < 0)
359 return ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700360 return sprintf(buf, "%d\n", outbound.current_read_index);
361}
362static DEVICE_ATTR_RO(out_read_index);
363
364static ssize_t out_write_index_show(struct device *dev,
365 struct device_attribute *dev_attr,
366 char *buf)
367{
368 struct hv_device *hv_dev = device_to_hv_device(dev);
369 struct hv_ring_buffer_debug_info outbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000370 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700371
372 if (!hv_dev->channel)
373 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000374
375 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
376 &outbound);
377 if (ret < 0)
378 return ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700379 return sprintf(buf, "%d\n", outbound.current_write_index);
380}
381static DEVICE_ATTR_RO(out_write_index);
382
383static ssize_t out_read_bytes_avail_show(struct device *dev,
384 struct device_attribute *dev_attr,
385 char *buf)
386{
387 struct hv_device *hv_dev = device_to_hv_device(dev);
388 struct hv_ring_buffer_debug_info outbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000389 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700390
391 if (!hv_dev->channel)
392 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000393
394 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
395 &outbound);
396 if (ret < 0)
397 return ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700398 return sprintf(buf, "%d\n", outbound.bytes_avail_toread);
399}
400static DEVICE_ATTR_RO(out_read_bytes_avail);
401
402static ssize_t out_write_bytes_avail_show(struct device *dev,
403 struct device_attribute *dev_attr,
404 char *buf)
405{
406 struct hv_device *hv_dev = device_to_hv_device(dev);
407 struct hv_ring_buffer_debug_info outbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000408 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700409
410 if (!hv_dev->channel)
411 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000412
413 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
414 &outbound);
415 if (ret < 0)
416 return ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700417 return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
418}
419static DEVICE_ATTR_RO(out_write_bytes_avail);
420
421static ssize_t in_intr_mask_show(struct device *dev,
422 struct device_attribute *dev_attr, char *buf)
423{
424 struct hv_device *hv_dev = device_to_hv_device(dev);
425 struct hv_ring_buffer_debug_info inbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000426 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700427
428 if (!hv_dev->channel)
429 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000430
431 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
432 if (ret < 0)
433 return ret;
434
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700435 return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
436}
437static DEVICE_ATTR_RO(in_intr_mask);
438
439static ssize_t in_read_index_show(struct device *dev,
440 struct device_attribute *dev_attr, char *buf)
441{
442 struct hv_device *hv_dev = device_to_hv_device(dev);
443 struct hv_ring_buffer_debug_info inbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000444 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700445
446 if (!hv_dev->channel)
447 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000448
449 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
450 if (ret < 0)
451 return ret;
452
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700453 return sprintf(buf, "%d\n", inbound.current_read_index);
454}
455static DEVICE_ATTR_RO(in_read_index);
456
457static ssize_t in_write_index_show(struct device *dev,
458 struct device_attribute *dev_attr, char *buf)
459{
460 struct hv_device *hv_dev = device_to_hv_device(dev);
461 struct hv_ring_buffer_debug_info inbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000462 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700463
464 if (!hv_dev->channel)
465 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000466
467 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
468 if (ret < 0)
469 return ret;
470
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700471 return sprintf(buf, "%d\n", inbound.current_write_index);
472}
473static DEVICE_ATTR_RO(in_write_index);
474
475static ssize_t in_read_bytes_avail_show(struct device *dev,
476 struct device_attribute *dev_attr,
477 char *buf)
478{
479 struct hv_device *hv_dev = device_to_hv_device(dev);
480 struct hv_ring_buffer_debug_info inbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000481 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700482
483 if (!hv_dev->channel)
484 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000485
486 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
487 if (ret < 0)
488 return ret;
489
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700490 return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
491}
492static DEVICE_ATTR_RO(in_read_bytes_avail);
493
494static ssize_t in_write_bytes_avail_show(struct device *dev,
495 struct device_attribute *dev_attr,
496 char *buf)
497{
498 struct hv_device *hv_dev = device_to_hv_device(dev);
499 struct hv_ring_buffer_debug_info inbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000500 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700501
502 if (!hv_dev->channel)
503 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000504
505 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
506 if (ret < 0)
507 return ret;
508
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700509 return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
510}
511static DEVICE_ATTR_RO(in_write_bytes_avail);
512
Dexuan Cui042ab032015-08-05 00:52:43 -0700513static ssize_t channel_vp_mapping_show(struct device *dev,
514 struct device_attribute *dev_attr,
515 char *buf)
516{
517 struct hv_device *hv_dev = device_to_hv_device(dev);
518 struct vmbus_channel *channel = hv_dev->channel, *cur_sc;
Dexuan Cui042ab032015-08-05 00:52:43 -0700519 int buf_size = PAGE_SIZE, n_written, tot_written;
520 struct list_head *cur;
521
522 if (!channel)
523 return -ENODEV;
524
Andrea Parri (Microsoft)3eb0ac82020-06-17 18:46:39 +0200525 mutex_lock(&vmbus_connection.channel_mutex);
526
Dexuan Cui042ab032015-08-05 00:52:43 -0700527 tot_written = snprintf(buf, buf_size, "%u:%u\n",
528 channel->offermsg.child_relid, channel->target_cpu);
529
Dexuan Cui042ab032015-08-05 00:52:43 -0700530 list_for_each(cur, &channel->sc_list) {
531 if (tot_written >= buf_size - 1)
532 break;
533
534 cur_sc = list_entry(cur, struct vmbus_channel, sc_list);
535 n_written = scnprintf(buf + tot_written,
536 buf_size - tot_written,
537 "%u:%u\n",
538 cur_sc->offermsg.child_relid,
539 cur_sc->target_cpu);
540 tot_written += n_written;
541 }
542
Andrea Parri (Microsoft)3eb0ac82020-06-17 18:46:39 +0200543 mutex_unlock(&vmbus_connection.channel_mutex);
Dexuan Cui042ab032015-08-05 00:52:43 -0700544
545 return tot_written;
546}
547static DEVICE_ATTR_RO(channel_vp_mapping);
548
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800549static ssize_t vendor_show(struct device *dev,
550 struct device_attribute *dev_attr,
551 char *buf)
552{
553 struct hv_device *hv_dev = device_to_hv_device(dev);
Matheus Castelloe4f22122020-11-15 16:57:31 -0300554
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800555 return sprintf(buf, "0x%x\n", hv_dev->vendor_id);
556}
557static DEVICE_ATTR_RO(vendor);
558
559static ssize_t device_show(struct device *dev,
560 struct device_attribute *dev_attr,
561 char *buf)
562{
563 struct hv_device *hv_dev = device_to_hv_device(dev);
Matheus Castelloe4f22122020-11-15 16:57:31 -0300564
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800565 return sprintf(buf, "0x%x\n", hv_dev->device_id);
566}
567static DEVICE_ATTR_RO(device);
568
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000569static ssize_t driver_override_store(struct device *dev,
570 struct device_attribute *attr,
571 const char *buf, size_t count)
572{
573 struct hv_device *hv_dev = device_to_hv_device(dev);
574 char *driver_override, *old, *cp;
575
576 /* We need to keep extra room for a newline */
577 if (count >= (PAGE_SIZE - 1))
578 return -EINVAL;
579
580 driver_override = kstrndup(buf, count, GFP_KERNEL);
581 if (!driver_override)
582 return -ENOMEM;
583
584 cp = strchr(driver_override, '\n');
585 if (cp)
586 *cp = '\0';
587
588 device_lock(dev);
589 old = hv_dev->driver_override;
590 if (strlen(driver_override)) {
591 hv_dev->driver_override = driver_override;
592 } else {
593 kfree(driver_override);
594 hv_dev->driver_override = NULL;
595 }
596 device_unlock(dev);
597
598 kfree(old);
599
600 return count;
601}
602
603static ssize_t driver_override_show(struct device *dev,
604 struct device_attribute *attr, char *buf)
605{
606 struct hv_device *hv_dev = device_to_hv_device(dev);
607 ssize_t len;
608
609 device_lock(dev);
610 len = snprintf(buf, PAGE_SIZE, "%s\n", hv_dev->driver_override);
611 device_unlock(dev);
612
613 return len;
614}
615static DEVICE_ATTR_RW(driver_override);
616
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700617/* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800618static struct attribute *vmbus_dev_attrs[] = {
Greg Kroah-Hartman03f3a912013-09-13 11:32:49 -0700619 &dev_attr_id.attr,
Greg Kroah-Hartmana8fb5f32013-09-13 11:32:50 -0700620 &dev_attr_state.attr,
Greg Kroah-Hartman5ffd00e2013-09-13 11:32:51 -0700621 &dev_attr_monitor_id.attr,
Greg Kroah-Hartman68234c02013-09-13 11:32:53 -0700622 &dev_attr_class_id.attr,
Greg Kroah-Hartman7c55e1d2013-09-13 11:32:54 -0700623 &dev_attr_device_id.attr,
Greg Kroah-Hartman647fa372013-09-13 11:32:52 -0700624 &dev_attr_modalias.attr,
Stephen Hemminger7ceb1c32018-07-28 21:58:48 +0000625#ifdef CONFIG_NUMA
626 &dev_attr_numa_node.attr,
627#endif
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700628 &dev_attr_server_monitor_pending.attr,
629 &dev_attr_client_monitor_pending.attr,
Greg Kroah-Hartman1cee2722013-09-13 11:32:57 -0700630 &dev_attr_server_monitor_latency.attr,
631 &dev_attr_client_monitor_latency.attr,
Greg Kroah-Hartman4947c742013-09-13 11:32:58 -0700632 &dev_attr_server_monitor_conn_id.attr,
633 &dev_attr_client_monitor_conn_id.attr,
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700634 &dev_attr_out_intr_mask.attr,
635 &dev_attr_out_read_index.attr,
636 &dev_attr_out_write_index.attr,
637 &dev_attr_out_read_bytes_avail.attr,
638 &dev_attr_out_write_bytes_avail.attr,
639 &dev_attr_in_intr_mask.attr,
640 &dev_attr_in_read_index.attr,
641 &dev_attr_in_write_index.attr,
642 &dev_attr_in_read_bytes_avail.attr,
643 &dev_attr_in_write_bytes_avail.attr,
Dexuan Cui042ab032015-08-05 00:52:43 -0700644 &dev_attr_channel_vp_mapping.attr,
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800645 &dev_attr_vendor.attr,
646 &dev_attr_device.attr,
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000647 &dev_attr_driver_override.attr,
Greg Kroah-Hartman03f3a912013-09-13 11:32:49 -0700648 NULL,
649};
Kimberly Brown46fc1542019-03-19 00:04:01 -0400650
651/*
652 * Device-level attribute_group callback function. Returns the permission for
653 * each attribute, and returns 0 if an attribute is not visible.
654 */
655static umode_t vmbus_dev_attr_is_visible(struct kobject *kobj,
656 struct attribute *attr, int idx)
657{
658 struct device *dev = kobj_to_dev(kobj);
659 const struct hv_device *hv_dev = device_to_hv_device(dev);
660
661 /* Hide the monitor attributes if the monitor mechanism is not used. */
662 if (!hv_dev->channel->offermsg.monitor_allocated &&
663 (attr == &dev_attr_monitor_id.attr ||
664 attr == &dev_attr_server_monitor_pending.attr ||
665 attr == &dev_attr_client_monitor_pending.attr ||
666 attr == &dev_attr_server_monitor_latency.attr ||
667 attr == &dev_attr_client_monitor_latency.attr ||
668 attr == &dev_attr_server_monitor_conn_id.attr ||
669 attr == &dev_attr_client_monitor_conn_id.attr))
670 return 0;
671
672 return attr->mode;
673}
674
675static const struct attribute_group vmbus_dev_group = {
676 .attrs = vmbus_dev_attrs,
677 .is_visible = vmbus_dev_attr_is_visible
678};
679__ATTRIBUTE_GROUPS(vmbus_dev);
Greg Kroah-Hartman03f3a912013-09-13 11:32:49 -0700680
Dexuan Cuic068e3f2021-01-06 17:45:52 -0800681/* Set up the attribute for /sys/bus/vmbus/hibernation */
682static ssize_t hibernation_show(struct bus_type *bus, char *buf)
683{
684 return sprintf(buf, "%d\n", !!hv_is_hibernation_supported());
685}
686
687static BUS_ATTR_RO(hibernation);
688
689static struct attribute *vmbus_bus_attrs[] = {
690 &bus_attr_hibernation.attr,
691 NULL,
692};
693static const struct attribute_group vmbus_bus_group = {
694 .attrs = vmbus_bus_attrs,
695};
696__ATTRIBUTE_GROUPS(vmbus_bus);
697
K. Y. Srinivasanadde2482011-03-15 15:03:37 -0700698/*
699 * vmbus_uevent - add uevent for our device
700 *
701 * This routine is invoked when a device is added or removed on the vmbus to
702 * generate a uevent to udev in the userspace. The udev will then look at its
703 * rule and the uevent generated here to load the appropriate driver
K. Y. Srinivasan0ddda662011-08-25 09:48:38 -0700704 *
705 * The alias string will be of the form vmbus:guid where guid is the string
706 * representation of the device guid (each byte of the guid will be
707 * represented with two hex characters.
K. Y. Srinivasanadde2482011-03-15 15:03:37 -0700708 */
709static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
710{
711 struct hv_device *dev = device_to_hv_device(device);
Andy Shevchenko0027e3f2020-04-23 16:45:04 +0300712 const char *format = "MODALIAS=vmbus:%*phN";
K. Y. Srinivasanadde2482011-03-15 15:03:37 -0700713
Andy Shevchenko0027e3f2020-04-23 16:45:04 +0300714 return add_uevent_var(env, format, UUID_SIZE, &dev->dev_type);
K. Y. Srinivasanadde2482011-03-15 15:03:37 -0700715}
716
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000717static const struct hv_vmbus_device_id *
Andy Shevchenko593db802019-01-10 16:25:32 +0200718hv_vmbus_dev_match(const struct hv_vmbus_device_id *id, const guid_t *guid)
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000719{
720 if (id == NULL)
721 return NULL; /* empty device table */
722
Andy Shevchenko593db802019-01-10 16:25:32 +0200723 for (; !guid_is_null(&id->guid); id++)
724 if (guid_equal(&id->guid, guid))
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000725 return id;
726
727 return NULL;
728}
729
730static const struct hv_vmbus_device_id *
Andy Shevchenko593db802019-01-10 16:25:32 +0200731hv_vmbus_dynid_match(struct hv_driver *drv, const guid_t *guid)
K. Y. Srinivasan3037a7b2011-09-13 10:59:37 -0700732{
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800733 const struct hv_vmbus_device_id *id = NULL;
734 struct vmbus_dynid *dynid;
735
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800736 spin_lock(&drv->dynids.lock);
737 list_for_each_entry(dynid, &drv->dynids.list, node) {
Andy Shevchenko593db802019-01-10 16:25:32 +0200738 if (guid_equal(&dynid->id.guid, guid)) {
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800739 id = &dynid->id;
740 break;
741 }
742 }
743 spin_unlock(&drv->dynids.lock);
744
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000745 return id;
746}
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800747
Andy Shevchenko593db802019-01-10 16:25:32 +0200748static const struct hv_vmbus_device_id vmbus_device_null;
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800749
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000750/*
751 * Return a matching hv_vmbus_device_id pointer.
752 * If there is no match, return NULL.
753 */
754static const struct hv_vmbus_device_id *hv_vmbus_get_id(struct hv_driver *drv,
755 struct hv_device *dev)
756{
Andy Shevchenko593db802019-01-10 16:25:32 +0200757 const guid_t *guid = &dev->dev_type;
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000758 const struct hv_vmbus_device_id *id;
K. Y. Srinivasan3037a7b2011-09-13 10:59:37 -0700759
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000760 /* When driver_override is set, only bind to the matching driver */
761 if (dev->driver_override && strcmp(dev->driver_override, drv->name))
762 return NULL;
763
764 /* Look at the dynamic ids first, before the static ones */
765 id = hv_vmbus_dynid_match(drv, guid);
766 if (!id)
767 id = hv_vmbus_dev_match(drv->id_table, guid);
768
769 /* driver_override will always match, send a dummy id */
770 if (!id && dev->driver_override)
771 id = &vmbus_device_null;
772
773 return id;
K. Y. Srinivasan3037a7b2011-09-13 10:59:37 -0700774}
775
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800776/* vmbus_add_dynid - add a new device ID to this driver and re-probe devices */
Andy Shevchenko593db802019-01-10 16:25:32 +0200777static int vmbus_add_dynid(struct hv_driver *drv, guid_t *guid)
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800778{
779 struct vmbus_dynid *dynid;
780
781 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
782 if (!dynid)
783 return -ENOMEM;
784
785 dynid->id.guid = *guid;
786
787 spin_lock(&drv->dynids.lock);
788 list_add_tail(&dynid->node, &drv->dynids.list);
789 spin_unlock(&drv->dynids.lock);
790
791 return driver_attach(&drv->driver);
792}
793
794static void vmbus_free_dynids(struct hv_driver *drv)
795{
796 struct vmbus_dynid *dynid, *n;
797
798 spin_lock(&drv->dynids.lock);
799 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
800 list_del(&dynid->node);
801 kfree(dynid);
802 }
803 spin_unlock(&drv->dynids.lock);
804}
805
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800806/*
807 * store_new_id - sysfs frontend to vmbus_add_dynid()
808 *
809 * Allow GUIDs to be added to an existing driver via sysfs.
810 */
811static ssize_t new_id_store(struct device_driver *driver, const char *buf,
812 size_t count)
813{
814 struct hv_driver *drv = drv_to_hv_drv(driver);
Andy Shevchenko593db802019-01-10 16:25:32 +0200815 guid_t guid;
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800816 ssize_t retval;
817
Andy Shevchenko593db802019-01-10 16:25:32 +0200818 retval = guid_parse(buf, &guid);
Andy Shevchenko31100102017-05-18 10:46:06 -0700819 if (retval)
820 return retval;
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800821
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000822 if (hv_vmbus_dynid_match(drv, &guid))
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800823 return -EEXIST;
824
825 retval = vmbus_add_dynid(drv, &guid);
826 if (retval)
827 return retval;
828 return count;
829}
830static DRIVER_ATTR_WO(new_id);
831
832/*
833 * store_remove_id - remove a PCI device ID from this driver
834 *
835 * Removes a dynamic pci device ID to this driver.
836 */
837static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
838 size_t count)
839{
840 struct hv_driver *drv = drv_to_hv_drv(driver);
841 struct vmbus_dynid *dynid, *n;
Andy Shevchenko593db802019-01-10 16:25:32 +0200842 guid_t guid;
Andy Shevchenko31100102017-05-18 10:46:06 -0700843 ssize_t retval;
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800844
Andy Shevchenko593db802019-01-10 16:25:32 +0200845 retval = guid_parse(buf, &guid);
Andy Shevchenko31100102017-05-18 10:46:06 -0700846 if (retval)
847 return retval;
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800848
Andy Shevchenko31100102017-05-18 10:46:06 -0700849 retval = -ENODEV;
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800850 spin_lock(&drv->dynids.lock);
851 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
852 struct hv_vmbus_device_id *id = &dynid->id;
853
Andy Shevchenko593db802019-01-10 16:25:32 +0200854 if (guid_equal(&id->guid, &guid)) {
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800855 list_del(&dynid->node);
856 kfree(dynid);
857 retval = count;
858 break;
859 }
860 }
861 spin_unlock(&drv->dynids.lock);
862
863 return retval;
864}
865static DRIVER_ATTR_WO(remove_id);
866
867static struct attribute *vmbus_drv_attrs[] = {
868 &driver_attr_new_id.attr,
869 &driver_attr_remove_id.attr,
870 NULL,
871};
872ATTRIBUTE_GROUPS(vmbus_drv);
K. Y. Srinivasan3037a7b2011-09-13 10:59:37 -0700873
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700874
875/*
876 * vmbus_match - Attempt to match the specified device to the specified driver
877 */
878static int vmbus_match(struct device *device, struct device_driver *driver)
879{
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700880 struct hv_driver *drv = drv_to_hv_drv(driver);
K. Y. Srinivasane8e27042011-06-06 15:50:04 -0700881 struct hv_device *hv_dev = device_to_hv_device(device);
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700882
Dexuan Cui8981da32016-01-27 22:29:41 -0800883 /* The hv_sock driver handles all hv_sock offers. */
884 if (is_hvsock_channel(hv_dev->channel))
885 return drv->hvsock;
886
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000887 if (hv_vmbus_get_id(drv, hv_dev))
K. Y. Srinivasan3037a7b2011-09-13 10:59:37 -0700888 return 1;
K. Y. Srinivasande632a2b2011-04-26 09:20:24 -0700889
K. Y. Srinivasan5841a822011-08-25 09:48:39 -0700890 return 0;
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700891}
892
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700893/*
894 * vmbus_probe - Add the new vmbus's child device
895 */
896static int vmbus_probe(struct device *child_device)
897{
898 int ret = 0;
899 struct hv_driver *drv =
900 drv_to_hv_drv(child_device->driver);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700901 struct hv_device *dev = device_to_hv_device(child_device);
K. Y. Srinivasan84946892011-09-13 10:59:38 -0700902 const struct hv_vmbus_device_id *dev_id;
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700903
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000904 dev_id = hv_vmbus_get_id(drv, dev);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700905 if (drv->probe) {
K. Y. Srinivasan84946892011-09-13 10:59:38 -0700906 ret = drv->probe(dev, dev_id);
K. Y. Srinivasanb14a7b32011-04-29 13:45:03 -0700907 if (ret != 0)
Hank Janssen0a466182011-03-29 13:58:47 -0700908 pr_err("probe failed for device %s (%d)\n",
909 dev_name(child_device), ret);
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700910
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700911 } else {
Hank Janssen0a466182011-03-29 13:58:47 -0700912 pr_err("probe not set for driver %s\n",
913 dev_name(child_device));
K. Y. Srinivasan6de925b2011-06-06 15:50:07 -0700914 ret = -ENODEV;
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700915 }
916 return ret;
917}
918
K. Y. Srinivasanc5dce3d2011-03-15 15:03:40 -0700919/*
920 * vmbus_remove - Remove a vmbus device
921 */
922static int vmbus_remove(struct device *child_device)
923{
K. Y. Srinivasand15a0302015-02-28 11:18:16 -0800924 struct hv_driver *drv;
K. Y. Srinivasan415b0232011-04-29 13:45:12 -0700925 struct hv_device *dev = device_to_hv_device(child_device);
K. Y. Srinivasanc5dce3d2011-03-15 15:03:40 -0700926
K. Y. Srinivasand15a0302015-02-28 11:18:16 -0800927 if (child_device->driver) {
928 drv = drv_to_hv_drv(child_device->driver);
929 if (drv->remove)
930 drv->remove(dev);
K. Y. Srinivasand15a0302015-02-28 11:18:16 -0800931 }
K. Y. Srinivasanc5dce3d2011-03-15 15:03:40 -0700932
933 return 0;
934}
935
K. Y. Srinivasaneb1bb252011-03-15 15:03:41 -0700936
937/*
938 * vmbus_shutdown - Shutdown a vmbus device
939 */
940static void vmbus_shutdown(struct device *child_device)
941{
942 struct hv_driver *drv;
K. Y. Srinivasanca6887f2011-04-29 13:45:14 -0700943 struct hv_device *dev = device_to_hv_device(child_device);
K. Y. Srinivasaneb1bb252011-03-15 15:03:41 -0700944
945
946 /* The device may not be attached yet */
947 if (!child_device->driver)
948 return;
949
950 drv = drv_to_hv_drv(child_device->driver);
951
K. Y. Srinivasanca6887f2011-04-29 13:45:14 -0700952 if (drv->shutdown)
953 drv->shutdown(dev);
K. Y. Srinivasaneb1bb252011-03-15 15:03:41 -0700954}
955
Dexuan Cui83b50f82019-09-19 21:46:12 +0000956#ifdef CONFIG_PM_SLEEP
Dexuan Cui271b2222019-09-05 23:01:17 +0000957/*
958 * vmbus_suspend - Suspend a vmbus device
959 */
960static int vmbus_suspend(struct device *child_device)
961{
962 struct hv_driver *drv;
963 struct hv_device *dev = device_to_hv_device(child_device);
964
965 /* The device may not be attached yet */
966 if (!child_device->driver)
967 return 0;
968
969 drv = drv_to_hv_drv(child_device->driver);
970 if (!drv->suspend)
971 return -EOPNOTSUPP;
972
973 return drv->suspend(dev);
974}
975
976/*
977 * vmbus_resume - Resume a vmbus device
978 */
979static int vmbus_resume(struct device *child_device)
980{
981 struct hv_driver *drv;
982 struct hv_device *dev = device_to_hv_device(child_device);
983
984 /* The device may not be attached yet */
985 if (!child_device->driver)
986 return 0;
987
988 drv = drv_to_hv_drv(child_device->driver);
989 if (!drv->resume)
990 return -EOPNOTSUPP;
991
992 return drv->resume(dev);
993}
Dexuan Cui1a06d012020-04-11 20:50:35 -0700994#else
995#define vmbus_suspend NULL
996#define vmbus_resume NULL
Dexuan Cui83b50f82019-09-19 21:46:12 +0000997#endif /* CONFIG_PM_SLEEP */
K. Y. Srinivasan086e7a52011-03-15 15:03:42 -0700998
999/*
1000 * vmbus_device_release - Final callback release of the vmbus child device
1001 */
1002static void vmbus_device_release(struct device *device)
1003{
K. Y. Srinivasane8e27042011-06-06 15:50:04 -07001004 struct hv_device *hv_dev = device_to_hv_device(device);
Dexuan Cui34c68012015-12-14 16:01:49 -08001005 struct vmbus_channel *channel = hv_dev->channel;
K. Y. Srinivasan086e7a52011-03-15 15:03:42 -07001006
Branden Bonabyaf9ca6f2019-10-03 17:01:49 -04001007 hv_debug_rm_dev_dir(hv_dev);
1008
K. Y. Srinivasan54a662652017-04-30 16:21:18 -07001009 mutex_lock(&vmbus_connection.channel_mutex);
Stephen Hemminger800b9322018-09-14 09:10:15 -07001010 hv_process_channel_removal(channel);
K. Y. Srinivasan54a662652017-04-30 16:21:18 -07001011 mutex_unlock(&vmbus_connection.channel_mutex);
K. Y. Srinivasane8e27042011-06-06 15:50:04 -07001012 kfree(hv_dev);
K. Y. Srinivasan086e7a52011-03-15 15:03:42 -07001013}
1014
Dexuan Cui271b2222019-09-05 23:01:17 +00001015/*
Dexuan Cui1a06d012020-04-11 20:50:35 -07001016 * Note: we must use the "noirq" ops: see the comment before vmbus_bus_pm.
1017 *
1018 * suspend_noirq/resume_noirq are set to NULL to support Suspend-to-Idle: we
1019 * shouldn't suspend the vmbus devices upon Suspend-to-Idle, otherwise there
1020 * is no way to wake up a Generation-2 VM.
1021 *
1022 * The other 4 ops are for hibernation.
Dexuan Cui271b2222019-09-05 23:01:17 +00001023 */
Dexuan Cui1a06d012020-04-11 20:50:35 -07001024
Dexuan Cui271b2222019-09-05 23:01:17 +00001025static const struct dev_pm_ops vmbus_pm = {
Dexuan Cui1a06d012020-04-11 20:50:35 -07001026 .suspend_noirq = NULL,
1027 .resume_noirq = NULL,
1028 .freeze_noirq = vmbus_suspend,
1029 .thaw_noirq = vmbus_resume,
1030 .poweroff_noirq = vmbus_suspend,
1031 .restore_noirq = vmbus_resume,
Dexuan Cui271b2222019-09-05 23:01:17 +00001032};
1033
Bill Pemberton454f18a2009-07-27 16:47:24 -04001034/* The one and only one */
K. Y. Srinivasan9adcac52011-04-29 13:45:08 -07001035static struct bus_type hv_bus = {
1036 .name = "vmbus",
1037 .match = vmbus_match,
1038 .shutdown = vmbus_shutdown,
1039 .remove = vmbus_remove,
1040 .probe = vmbus_probe,
1041 .uevent = vmbus_uevent,
Stephen Hemmingerfc769362016-12-03 12:34:39 -08001042 .dev_groups = vmbus_dev_groups,
1043 .drv_groups = vmbus_drv_groups,
Dexuan Cuic068e3f2021-01-06 17:45:52 -08001044 .bus_groups = vmbus_bus_groups,
Dexuan Cui271b2222019-09-05 23:01:17 +00001045 .pm = &vmbus_pm,
Hank Janssen3e7ee492009-07-13 16:02:34 -07001046};
1047
Timo Teräsbf6506f2010-12-15 20:48:08 +02001048struct onmessage_work_context {
1049 struct work_struct work;
Vitaly Kuznetsova2764632020-04-06 12:41:51 +02001050 struct {
1051 struct hv_message_header header;
1052 u8 payload[];
1053 } msg;
Timo Teräsbf6506f2010-12-15 20:48:08 +02001054};
1055
1056static void vmbus_onmessage_work(struct work_struct *work)
1057{
1058 struct onmessage_work_context *ctx;
1059
Vitaly Kuznetsov09a19622015-02-27 11:25:54 -08001060 /* Do not process messages if we're in DISCONNECTED state */
1061 if (vmbus_connection.conn_state == DISCONNECTED)
1062 return;
1063
Timo Teräsbf6506f2010-12-15 20:48:08 +02001064 ctx = container_of(work, struct onmessage_work_context,
1065 work);
Vitaly Kuznetsov5cc41502020-04-06 12:41:52 +02001066 vmbus_onmessage((struct vmbus_channel_message_header *)
1067 &ctx->msg.payload);
Timo Teräsbf6506f2010-12-15 20:48:08 +02001068 kfree(ctx);
1069}
1070
K. Y. Srinivasand81274a2016-02-26 15:13:21 -08001071void vmbus_on_msg_dpc(unsigned long data)
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001072{
Stephen Hemminger37cdd992017-02-11 23:02:19 -07001073 struct hv_per_cpu_context *hv_cpu = (void *)data;
1074 void *page_addr = hv_cpu->synic_message_page;
Andrea Parri (Microsoft)fe8c1b12020-12-09 08:08:24 +01001075 struct hv_message msg_copy, *msg = (struct hv_message *)page_addr +
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001076 VMBUS_MESSAGE_SINT;
Dexuan Cui652594c2015-03-27 09:10:08 -07001077 struct vmbus_channel_message_header *hdr;
Andrea Parri (Microsoft)9c400d32020-12-09 08:08:23 +01001078 enum vmbus_channel_message_type msgtype;
Stephen Hemmingere6242fa2017-03-04 18:27:16 -07001079 const struct vmbus_channel_message_table_entry *entry;
Timo Teräsbf6506f2010-12-15 20:48:08 +02001080 struct onmessage_work_context *ctx;
Andrea Parri (Microsoft)9c400d32020-12-09 08:08:23 +01001081 __u8 payload_size;
Andrea Parri (Microsoft)fe8c1b12020-12-09 08:08:24 +01001082 u32 message_type;
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001083
Vitaly Kuznetsovb0a284d2020-04-06 12:43:15 +02001084 /*
1085 * 'enum vmbus_channel_message_type' is supposed to always be 'u32' as
1086 * it is being used in 'struct vmbus_channel_message_header' definition
1087 * which is supposed to match hypervisor ABI.
1088 */
1089 BUILD_BUG_ON(sizeof(enum vmbus_channel_message_type) != sizeof(u32));
1090
Andrea Parri (Microsoft)fe8c1b12020-12-09 08:08:24 +01001091 /*
1092 * Since the message is in memory shared with the host, an erroneous or
1093 * malicious Hyper-V could modify the message while vmbus_on_msg_dpc()
1094 * or individual message handlers are executing; to prevent this, copy
1095 * the message into private memory.
1096 */
1097 memcpy(&msg_copy, msg, sizeof(struct hv_message));
1098
1099 message_type = msg_copy.header.message_type;
Vitaly Kuznetsovcd95aad2016-04-30 19:21:34 -07001100 if (message_type == HVMSG_NONE)
Vitaly Kuznetsov7be3e162016-02-26 15:13:15 -08001101 /* no msg */
1102 return;
Dexuan Cui652594c2015-03-27 09:10:08 -07001103
Andrea Parri (Microsoft)fe8c1b12020-12-09 08:08:24 +01001104 hdr = (struct vmbus_channel_message_header *)msg_copy.u.payload;
Andrea Parri (Microsoft)9c400d32020-12-09 08:08:23 +01001105 msgtype = hdr->msgtype;
Dexuan Cui652594c2015-03-27 09:10:08 -07001106
Vitaly Kuznetsovc9fe0f82017-10-29 12:21:00 -07001107 trace_vmbus_on_msg_dpc(hdr);
1108
Andrea Parri (Microsoft)9c400d32020-12-09 08:08:23 +01001109 if (msgtype >= CHANNELMSG_COUNT) {
1110 WARN_ONCE(1, "unknown msgtype=%d\n", msgtype);
Vitaly Kuznetsov7be3e162016-02-26 15:13:15 -08001111 goto msg_handled;
1112 }
Dexuan Cui652594c2015-03-27 09:10:08 -07001113
Andrea Parri (Microsoft)fe8c1b12020-12-09 08:08:24 +01001114 payload_size = msg_copy.header.payload_size;
Andrea Parri (Microsoft)9c400d32020-12-09 08:08:23 +01001115 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT) {
1116 WARN_ONCE(1, "payload size is too large (%d)\n", payload_size);
Vitaly Kuznetsovac0f7d422020-04-06 12:41:50 +02001117 goto msg_handled;
1118 }
1119
Andrea Parri (Microsoft)9c400d32020-12-09 08:08:23 +01001120 entry = &channel_message_table[msgtype];
Dexuan Cuiddc9d352020-01-19 15:29:22 -08001121
1122 if (!entry->message_handler)
1123 goto msg_handled;
1124
Andrea Parri (Microsoft)9c400d32020-12-09 08:08:23 +01001125 if (payload_size < entry->min_payload_len) {
1126 WARN_ONCE(1, "message too short: msgtype=%d len=%d\n", msgtype, payload_size);
Vitaly Kuznetsov52c78032020-04-06 12:43:26 +02001127 goto msg_handled;
1128 }
1129
Vitaly Kuznetsov7be3e162016-02-26 15:13:15 -08001130 if (entry->handler_type == VMHT_BLOCKING) {
Andrea Parri (Microsoft)9c400d32020-12-09 08:08:23 +01001131 ctx = kmalloc(sizeof(*ctx) + payload_size, GFP_ATOMIC);
Vitaly Kuznetsov7be3e162016-02-26 15:13:15 -08001132 if (ctx == NULL)
1133 return;
Dexuan Cui652594c2015-03-27 09:10:08 -07001134
Vitaly Kuznetsov7be3e162016-02-26 15:13:15 -08001135 INIT_WORK(&ctx->work, vmbus_onmessage_work);
Andrea Parri (Microsoft)fe8c1b12020-12-09 08:08:24 +01001136 memcpy(&ctx->msg, &msg_copy, sizeof(msg->header) + payload_size);
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001137
K. Y. Srinivasan54a662652017-04-30 16:21:18 -07001138 /*
1139 * The host can generate a rescind message while we
1140 * may still be handling the original offer. We deal with
Andrea Parri (Microsoft)b9fa1b82020-04-06 02:15:05 +02001141 * this condition by relying on the synchronization provided
1142 * by offer_in_progress and by channel_mutex. See also the
1143 * inline comments in vmbus_onoffer_rescind().
K. Y. Srinivasan54a662652017-04-30 16:21:18 -07001144 */
Andrea Parri (Microsoft)9c400d32020-12-09 08:08:23 +01001145 switch (msgtype) {
K. Y. Srinivasan54a662652017-04-30 16:21:18 -07001146 case CHANNELMSG_RESCIND_CHANNELOFFER:
1147 /*
1148 * If we are handling the rescind message;
1149 * schedule the work on the global work queue.
Andrea Parri (Microsoft)8a857c52020-04-06 02:15:04 +02001150 *
1151 * The OFFER message and the RESCIND message should
1152 * not be handled by the same serialized work queue,
1153 * because the OFFER handler may call vmbus_open(),
1154 * which tries to open the channel by sending an
1155 * OPEN_CHANNEL message to the host and waits for
1156 * the host's response; however, if the host has
1157 * rescinded the channel before it receives the
1158 * OPEN_CHANNEL message, the host just silently
1159 * ignores the OPEN_CHANNEL message; as a result,
1160 * the guest's OFFER handler hangs for ever, if we
1161 * handle the RESCIND message in the same serialized
1162 * work queue: the RESCIND handler can not start to
1163 * run before the OFFER handler finishes.
K. Y. Srinivasan54a662652017-04-30 16:21:18 -07001164 */
Andrea Parri (Microsoft)b9fa1b82020-04-06 02:15:05 +02001165 schedule_work(&ctx->work);
K. Y. Srinivasan54a662652017-04-30 16:21:18 -07001166 break;
1167
1168 case CHANNELMSG_OFFERCHANNEL:
Andrea Parri (Microsoft)b9fa1b82020-04-06 02:15:05 +02001169 /*
1170 * The host sends the offer message of a given channel
1171 * before sending the rescind message of the same
1172 * channel. These messages are sent to the guest's
1173 * connect CPU; the guest then starts processing them
1174 * in the tasklet handler on this CPU:
1175 *
1176 * VMBUS_CONNECT_CPU
1177 *
1178 * [vmbus_on_msg_dpc()]
1179 * atomic_inc() // CHANNELMSG_OFFERCHANNEL
1180 * queue_work()
1181 * ...
1182 * [vmbus_on_msg_dpc()]
1183 * schedule_work() // CHANNELMSG_RESCIND_CHANNELOFFER
1184 *
1185 * We rely on the memory-ordering properties of the
1186 * queue_work() and schedule_work() primitives, which
1187 * guarantee that the atomic increment will be visible
1188 * to the CPUs which will execute the offer & rescind
1189 * works by the time these works will start execution.
1190 */
K. Y. Srinivasan54a662652017-04-30 16:21:18 -07001191 atomic_inc(&vmbus_connection.offer_in_progress);
Andrea Parri (Microsoft)b9fa1b82020-04-06 02:15:05 +02001192 fallthrough;
K. Y. Srinivasan54a662652017-04-30 16:21:18 -07001193
1194 default:
1195 queue_work(vmbus_connection.work_queue, &ctx->work);
1196 }
Vitaly Kuznetsov7be3e162016-02-26 15:13:15 -08001197 } else
1198 entry->message_handler(hdr);
Dexuan Cui652594c2015-03-27 09:10:08 -07001199
1200msg_handled:
Vitaly Kuznetsovcd95aad2016-04-30 19:21:34 -07001201 vmbus_signal_eom(msg, message_type);
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001202}
1203
Dexuan Cui83b50f82019-09-19 21:46:12 +00001204#ifdef CONFIG_PM_SLEEP
Dexuan Cui1f48dcf2019-09-05 23:01:20 +00001205/*
1206 * Fake RESCIND_CHANNEL messages to clean up hv_sock channels by force for
1207 * hibernation, because hv_sock connections can not persist across hibernation.
1208 */
1209static void vmbus_force_channel_rescinded(struct vmbus_channel *channel)
1210{
1211 struct onmessage_work_context *ctx;
1212 struct vmbus_channel_rescind_offer *rescind;
1213
1214 WARN_ON(!is_hvsock_channel(channel));
1215
1216 /*
Vitaly Kuznetsova2764632020-04-06 12:41:51 +02001217 * Allocation size is small and the allocation should really not fail,
Dexuan Cui1f48dcf2019-09-05 23:01:20 +00001218 * otherwise the state of the hv_sock connections ends up in limbo.
1219 */
Vitaly Kuznetsova2764632020-04-06 12:41:51 +02001220 ctx = kzalloc(sizeof(*ctx) + sizeof(*rescind),
1221 GFP_KERNEL | __GFP_NOFAIL);
Dexuan Cui1f48dcf2019-09-05 23:01:20 +00001222
1223 /*
1224 * So far, these are not really used by Linux. Just set them to the
1225 * reasonable values conforming to the definitions of the fields.
1226 */
1227 ctx->msg.header.message_type = 1;
1228 ctx->msg.header.payload_size = sizeof(*rescind);
1229
1230 /* These values are actually used by Linux. */
Vitaly Kuznetsova2764632020-04-06 12:41:51 +02001231 rescind = (struct vmbus_channel_rescind_offer *)ctx->msg.payload;
Dexuan Cui1f48dcf2019-09-05 23:01:20 +00001232 rescind->header.msgtype = CHANNELMSG_RESCIND_CHANNELOFFER;
1233 rescind->child_relid = channel->offermsg.child_relid;
1234
1235 INIT_WORK(&ctx->work, vmbus_onmessage_work);
1236
Andrea Parri (Microsoft)b9fa1b82020-04-06 02:15:05 +02001237 queue_work(vmbus_connection.work_queue, &ctx->work);
Dexuan Cui1f48dcf2019-09-05 23:01:20 +00001238}
Dexuan Cui83b50f82019-09-19 21:46:12 +00001239#endif /* CONFIG_PM_SLEEP */
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001240
1241/*
1242 * Schedule all channels with events pending
1243 */
1244static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu)
1245{
1246 unsigned long *recv_int_page;
1247 u32 maxbits, relid;
1248
1249 if (vmbus_proto_version < VERSION_WIN8) {
1250 maxbits = MAX_NUM_CHANNELS_SUPPORTED;
1251 recv_int_page = vmbus_connection.recv_int_page;
1252 } else {
1253 /*
1254 * When the host is win8 and beyond, the event page
1255 * can be directly checked to get the id of the channel
1256 * that has the interrupt pending.
1257 */
1258 void *page_addr = hv_cpu->synic_event_page;
1259 union hv_synic_event_flags *event
1260 = (union hv_synic_event_flags *)page_addr +
1261 VMBUS_MESSAGE_SINT;
1262
1263 maxbits = HV_EVENT_FLAGS_COUNT;
1264 recv_int_page = event->flags;
1265 }
1266
1267 if (unlikely(!recv_int_page))
1268 return;
1269
1270 for_each_set_bit(relid, recv_int_page, maxbits) {
Andrea Parri (Microsoft)9403b662020-04-06 02:15:09 +02001271 void (*callback_fn)(void *context);
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001272 struct vmbus_channel *channel;
1273
1274 if (!sync_test_and_clear_bit(relid, recv_int_page))
1275 continue;
1276
1277 /* Special case - vmbus channel protocol msg */
1278 if (relid == 0)
1279 continue;
1280
Andrea Parri (Microsoft)8b6a8772020-04-06 02:15:06 +02001281 /*
1282 * Pairs with the kfree_rcu() in vmbus_chan_release().
1283 * Guarantees that the channel data structure doesn't
1284 * get freed while the channel pointer below is being
1285 * dereferenced.
1286 */
Stephen Hemminger8200f202017-03-04 18:13:57 -07001287 rcu_read_lock();
1288
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001289 /* Find channel based on relid */
Andrea Parri (Microsoft)8b6a8772020-04-06 02:15:06 +02001290 channel = relid2channel(relid);
1291 if (channel == NULL)
1292 goto sched_unlock_rcu;
Stephen Hemmingerb71e3282017-02-11 23:02:21 -07001293
Andrea Parri (Microsoft)8b6a8772020-04-06 02:15:06 +02001294 if (channel->rescind)
1295 goto sched_unlock_rcu;
K. Y. Srinivasan6f3d7912017-08-11 10:03:59 -07001296
Andrea Parri (Microsoft)9403b662020-04-06 02:15:09 +02001297 /*
1298 * Make sure that the ring buffer data structure doesn't get
1299 * freed while we dereference the ring buffer pointer. Test
1300 * for the channel's onchannel_callback being NULL within a
1301 * sched_lock critical section. See also the inline comments
1302 * in vmbus_reset_channel_cb().
1303 */
1304 spin_lock(&channel->sched_lock);
Vitaly Kuznetsov991f8f12017-10-29 12:21:16 -07001305
Andrea Parri (Microsoft)9403b662020-04-06 02:15:09 +02001306 callback_fn = channel->onchannel_callback;
1307 if (unlikely(callback_fn == NULL))
1308 goto sched_unlock;
Stephen Hemminger6981fbf2017-10-29 11:33:40 -07001309
Andrea Parri (Microsoft)8b6a8772020-04-06 02:15:06 +02001310 trace_vmbus_chan_sched(channel);
Stephen Hemmingerb71e3282017-02-11 23:02:21 -07001311
Andrea Parri (Microsoft)8b6a8772020-04-06 02:15:06 +02001312 ++channel->interrupts;
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001313
Andrea Parri (Microsoft)8b6a8772020-04-06 02:15:06 +02001314 switch (channel->callback_mode) {
1315 case HV_CALL_ISR:
Andrea Parri (Microsoft)9403b662020-04-06 02:15:09 +02001316 (*callback_fn)(channel->channel_callback_context);
Andrea Parri (Microsoft)8b6a8772020-04-06 02:15:06 +02001317 break;
Stephen Hemmingerb71e3282017-02-11 23:02:21 -07001318
Andrea Parri (Microsoft)8b6a8772020-04-06 02:15:06 +02001319 case HV_CALL_BATCHED:
1320 hv_begin_read(&channel->inbound);
1321 fallthrough;
1322 case HV_CALL_DIRECT:
1323 tasklet_schedule(&channel->callback_event);
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001324 }
Stephen Hemminger8200f202017-03-04 18:13:57 -07001325
Andrea Parri (Microsoft)9403b662020-04-06 02:15:09 +02001326sched_unlock:
1327 spin_unlock(&channel->sched_lock);
Andrea Parri (Microsoft)8b6a8772020-04-06 02:15:06 +02001328sched_unlock_rcu:
Stephen Hemminger8200f202017-03-04 18:13:57 -07001329 rcu_read_unlock();
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001330 }
1331}
1332
Thomas Gleixner76d388c2014-03-05 13:42:14 +01001333static void vmbus_isr(void)
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001334{
Stephen Hemminger37cdd992017-02-11 23:02:19 -07001335 struct hv_per_cpu_context *hv_cpu
1336 = this_cpu_ptr(hv_context.cpu_context);
1337 void *page_addr = hv_cpu->synic_event_page;
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001338 struct hv_message *msg;
1339 union hv_synic_event_flags *event;
K. Y. Srinivasanae4636e2011-08-27 11:31:35 -07001340 bool handled = false;
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001341
Stephen Hemminger37cdd992017-02-11 23:02:19 -07001342 if (unlikely(page_addr == NULL))
Thomas Gleixner76d388c2014-03-05 13:42:14 +01001343 return;
K. Y. Srinivasan5ab05952012-12-01 06:46:55 -08001344
1345 event = (union hv_synic_event_flags *)page_addr +
1346 VMBUS_MESSAGE_SINT;
K. Y. Srinivasan7341d902011-08-31 14:35:56 -07001347 /*
1348 * Check for events before checking for messages. This is the order
1349 * in which events and messages are checked in Windows guests on
1350 * Hyper-V, and the Windows team suggested we do the same.
1351 */
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001352
K. Y. Srinivasan6552ecd72012-12-01 06:46:49 -08001353 if ((vmbus_proto_version == VERSION_WS2008) ||
1354 (vmbus_proto_version == VERSION_WIN7)) {
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001355
K. Y. Srinivasan6552ecd72012-12-01 06:46:49 -08001356 /* Since we are a child, we only need to check bit 0 */
Stephen Hemminger5c1bec62017-02-05 17:20:31 -07001357 if (sync_test_and_clear_bit(0, event->flags))
K. Y. Srinivasan6552ecd72012-12-01 06:46:49 -08001358 handled = true;
K. Y. Srinivasan6552ecd72012-12-01 06:46:49 -08001359 } else {
1360 /*
1361 * Our host is win8 or above. The signaling mechanism
1362 * has changed and we can directly look at the event page.
1363 * If bit n is set then we have an interrup on the channel
1364 * whose id is n.
1365 */
K. Y. Srinivasanae4636e2011-08-27 11:31:35 -07001366 handled = true;
K. Y. Srinivasan793be9c2011-03-15 15:03:43 -07001367 }
K. Y. Srinivasanae4636e2011-08-27 11:31:35 -07001368
K. Y. Srinivasan6552ecd72012-12-01 06:46:49 -08001369 if (handled)
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001370 vmbus_chan_sched(hv_cpu);
K. Y. Srinivasan6552ecd72012-12-01 06:46:49 -08001371
Stephen Hemminger37cdd992017-02-11 23:02:19 -07001372 page_addr = hv_cpu->synic_message_page;
K. Y. Srinivasan7341d902011-08-31 14:35:56 -07001373 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
1374
1375 /* Check if there are actual msgs to be processed */
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -08001376 if (msg->header.message_type != HVMSG_NONE) {
Michael Kelleyfd1fea62019-07-01 04:25:56 +00001377 if (msg->header.message_type == HVMSG_TIMER_EXPIRED) {
1378 hv_stimer0_isr();
1379 vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED);
1380 } else
Stephen Hemminger37cdd992017-02-11 23:02:19 -07001381 tasklet_schedule(&hv_cpu->msg_dpc);
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -08001382 }
Stephan Mueller4b44f2d2016-05-02 02:14:34 -04001383
Michael Kelley626b9012020-08-14 12:45:04 -07001384 add_interrupt_randomness(hv_get_vector(), 0);
K. Y. Srinivasan793be9c2011-03-15 15:03:43 -07001385}
1386
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001387/*
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001388 * Callback from kmsg_dump. Grab as much as possible from the end of the kmsg
1389 * buffer and call into Hyper-V to transfer the data.
1390 */
1391static void hv_kmsg_dump(struct kmsg_dumper *dumper,
1392 enum kmsg_dump_reason reason)
1393{
1394 size_t bytes_written;
1395 phys_addr_t panic_pa;
1396
1397 /* We are only interested in panics. */
1398 if ((reason != KMSG_DUMP_PANIC) || (!sysctl_record_panic_msg))
1399 return;
1400
1401 panic_pa = virt_to_phys(hv_panic_page);
1402
1403 /*
1404 * Write dump contents to the page. No need to synchronize; panic should
1405 * be single-threaded.
1406 */
Joseph Salisbury77b48be2020-06-26 15:28:17 -07001407 kmsg_dump_get_buffer(dumper, false, hv_panic_page, HV_HYP_PAGE_SIZE,
Sunil Muthuswamyddcaf3c2018-07-28 21:58:45 +00001408 &bytes_written);
1409 if (bytes_written)
1410 hyperv_report_panic_msg(panic_pa, bytes_written);
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001411}
1412
1413static struct kmsg_dumper hv_kmsg_dumper = {
1414 .dump = hv_kmsg_dump,
1415};
1416
Matheus Castellob0c03ef2020-11-25 00:29:26 -03001417static void hv_kmsg_dump_register(void)
1418{
1419 int ret;
1420
1421 hv_panic_page = hv_alloc_hyperv_zeroed_page();
1422 if (!hv_panic_page) {
1423 pr_err("Hyper-V: panic message page memory allocation failed\n");
1424 return;
1425 }
1426
1427 ret = kmsg_dump_register(&hv_kmsg_dumper);
1428 if (ret) {
1429 pr_err("Hyper-V: kmsg dump register error 0x%x\n", ret);
1430 hv_free_hyperv_page((unsigned long)hv_panic_page);
1431 hv_panic_page = NULL;
1432 }
1433}
1434
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001435static struct ctl_table_header *hv_ctl_table_hdr;
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001436
1437/*
1438 * sysctl option to allow the user to control whether kmsg data should be
1439 * reported to Hyper-V on panic.
1440 */
1441static struct ctl_table hv_ctl_table[] = {
1442 {
1443 .procname = "hyperv_record_panic_msg",
1444 .data = &sysctl_record_panic_msg,
1445 .maxlen = sizeof(int),
1446 .mode = 0644,
1447 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -07001448 .extra1 = SYSCTL_ZERO,
1449 .extra2 = SYSCTL_ONE
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001450 },
1451 {}
1452};
1453
1454static struct ctl_table hv_root_table[] = {
1455 {
1456 .procname = "kernel",
1457 .mode = 0555,
1458 .child = hv_ctl_table
1459 },
1460 {}
1461};
Vitaly Kuznetsove5132292015-02-27 11:25:51 -08001462
Hank Janssen3e189512010-03-04 22:11:00 +00001463/*
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001464 * vmbus_bus_init -Main vmbus driver initialization routine.
1465 *
1466 * Here, we
Lars Lindley0686e4f2010-03-11 23:51:23 +01001467 * - initialize the vmbus driver context
Lars Lindley0686e4f2010-03-11 23:51:23 +01001468 * - invoke the vmbus hv main init routine
Lars Lindley0686e4f2010-03-11 23:51:23 +01001469 * - retrieve the channel offers
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001470 */
K. Y. Srinivasanefc26722015-12-14 16:01:46 -08001471static int vmbus_bus_init(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001472{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001473 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001474
Greg Kroah-Hartman6d26e38fa22010-12-02 12:08:08 -08001475 ret = hv_init();
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001476 if (ret != 0) {
Hank Janssen0a466182011-03-29 13:58:47 -07001477 pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
K. Y. Srinivasand6c1c5d2011-06-06 15:50:08 -07001478 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001479 }
1480
K. Y. Srinivasan9adcac52011-04-29 13:45:08 -07001481 ret = bus_register(&hv_bus);
K. Y. Srinivasand6c1c5d2011-06-06 15:50:08 -07001482 if (ret)
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -07001483 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001484
Michael Kelley626b9012020-08-14 12:45:04 -07001485 ret = hv_setup_vmbus_irq(vmbus_irq, vmbus_isr);
1486 if (ret)
1487 goto err_setup;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001488
Jason Wang2608fb62013-06-19 11:28:10 +08001489 ret = hv_synic_alloc();
1490 if (ret)
1491 goto err_alloc;
Michael Kelleyfd1fea62019-07-01 04:25:56 +00001492
K. Y. Srinivasan800b6902011-03-15 15:03:33 -07001493 /*
Michael Kelleyfd1fea62019-07-01 04:25:56 +00001494 * Initialize the per-cpu interrupt state and stimer state.
1495 * Then connect to the host.
K. Y. Srinivasan800b6902011-03-15 15:03:33 -07001496 */
Michael Kelley4a5f3cd2017-12-22 11:19:02 -07001497 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hyperv/vmbus:online",
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -08001498 hv_synic_init, hv_synic_cleanup);
1499 if (ret < 0)
Michael Kelleyfd1fea62019-07-01 04:25:56 +00001500 goto err_cpuhp;
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -08001501 hyperv_cpuhp_online = ret;
1502
K. Y. Srinivasan800b6902011-03-15 15:03:33 -07001503 ret = vmbus_connect();
K. Y. Srinivasan8b9987e92011-08-31 14:35:55 -07001504 if (ret)
Andrey Smetanin17efbee2015-12-14 16:01:38 -08001505 goto err_connect;
K. Y. Srinivasan800b6902011-03-15 15:03:33 -07001506
Nick Meier96c1d052015-02-28 11:39:01 -08001507 /*
1508 * Only register if the crash MSRs are available
1509 */
Denis V. Lunevcc2dd402015-08-01 16:08:20 -07001510 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001511 u64 hyperv_crash_ctl;
1512 /*
1513 * Sysctl registration is not fatal, since by default
1514 * reporting is enabled.
1515 */
1516 hv_ctl_table_hdr = register_sysctl_table(hv_root_table);
1517 if (!hv_ctl_table_hdr)
1518 pr_err("Hyper-V: sysctl table register error");
1519
1520 /*
1521 * Register for panic kmsg callback only if the right
1522 * capability is supported by the hypervisor.
1523 */
Sunil Muthuswamy9d9c9652018-07-28 21:58:47 +00001524 hv_get_crash_ctl(hyperv_crash_ctl);
Matheus Castellob0c03ef2020-11-25 00:29:26 -03001525 if (hyperv_crash_ctl & HV_CRASH_CTL_CRASH_NOTIFY_MSG)
1526 hv_kmsg_dump_register();
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001527
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -07001528 register_die_notifier(&hyperv_die_block);
Nick Meier96c1d052015-02-28 11:39:01 -08001529 }
1530
Tianyu Lan74347a92020-04-06 08:53:26 -07001531 /*
1532 * Always register the panic notifier because we need to unload
1533 * the VMbus channel connection to prevent any VMbus
1534 * activity after the VM panics.
1535 */
1536 atomic_notifier_chain_register(&panic_notifier_list,
1537 &hyperv_panic_block);
1538
Greg Kroah-Hartman2d6e8822010-12-02 08:50:58 -08001539 vmbus_request_offers();
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +00001540
K. Y. Srinivasand6c1c5d2011-06-06 15:50:08 -07001541 return 0;
K. Y. Srinivasan8b9987e92011-08-31 14:35:55 -07001542
Andrey Smetanin17efbee2015-12-14 16:01:38 -08001543err_connect:
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -08001544 cpuhp_remove_state(hyperv_cpuhp_online);
Michael Kelleyfd1fea62019-07-01 04:25:56 +00001545err_cpuhp:
Jason Wang2608fb62013-06-19 11:28:10 +08001546 hv_synic_free();
Michael Kelley4df4cb9e92019-11-13 01:11:49 +00001547err_alloc:
Thomas Gleixner76d388c2014-03-05 13:42:14 +01001548 hv_remove_vmbus_irq();
Michael Kelley626b9012020-08-14 12:45:04 -07001549err_setup:
K. Y. Srinivasan8b9987e92011-08-31 14:35:55 -07001550 bus_unregister(&hv_bus);
Sunil Muthuswamy8afc06d2018-07-28 21:58:46 +00001551 unregister_sysctl_table(hv_ctl_table_hdr);
1552 hv_ctl_table_hdr = NULL;
K. Y. Srinivasan8b9987e92011-08-31 14:35:55 -07001553 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001554}
1555
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001556/**
Jake Oshins35464482015-08-05 00:52:37 -07001557 * __vmbus_child_driver_register() - Register a vmbus's driver
1558 * @hv_driver: Pointer to driver structure you want to register
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001559 * @owner: owner module of the drv
1560 * @mod_name: module name string
Hank Janssen3e189512010-03-04 22:11:00 +00001561 *
1562 * Registers the given driver with Linux through the 'driver_register()' call
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001563 * and sets up the hyper-v vmbus handling for this driver.
Hank Janssen3e189512010-03-04 22:11:00 +00001564 * It will return the state of the 'driver_register()' call.
1565 *
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001566 */
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001567int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001568{
Bill Pemberton5d48a1c2009-07-27 16:47:36 -04001569 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001570
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001571 pr_info("registering driver %s\n", hv_driver->name);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001572
K. Y. Srinivasancf6a2ea2011-12-01 09:59:34 -08001573 ret = vmbus_exists();
1574 if (ret < 0)
1575 return ret;
1576
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001577 hv_driver->driver.name = hv_driver->name;
1578 hv_driver->driver.owner = owner;
1579 hv_driver->driver.mod_name = mod_name;
1580 hv_driver->driver.bus = &hv_bus;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001581
Stephen Hemmingerfc769362016-12-03 12:34:39 -08001582 spin_lock_init(&hv_driver->dynids.lock);
1583 INIT_LIST_HEAD(&hv_driver->dynids.list);
1584
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001585 ret = driver_register(&hv_driver->driver);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001586
Bill Pemberton5d48a1c2009-07-27 16:47:36 -04001587 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001588}
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001589EXPORT_SYMBOL_GPL(__vmbus_driver_register);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001590
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001591/**
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001592 * vmbus_driver_unregister() - Unregister a vmbus's driver
Jake Oshins35464482015-08-05 00:52:37 -07001593 * @hv_driver: Pointer to driver structure you want to
1594 * un-register
Hank Janssen3e189512010-03-04 22:11:00 +00001595 *
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001596 * Un-register the given driver that was previous registered with a call to
1597 * vmbus_driver_register()
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001598 */
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001599void vmbus_driver_unregister(struct hv_driver *hv_driver)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001600{
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001601 pr_info("unregistering driver %s\n", hv_driver->name);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001602
Stephen Hemmingerfc769362016-12-03 12:34:39 -08001603 if (!vmbus_exists()) {
K. Y. Srinivasan8f257a12011-12-27 13:49:37 -08001604 driver_unregister(&hv_driver->driver);
Stephen Hemmingerfc769362016-12-03 12:34:39 -08001605 vmbus_free_dynids(hv_driver);
1606 }
Hank Janssen3e7ee492009-07-13 16:02:34 -07001607}
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001608EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001609
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001610
1611/*
1612 * Called when last reference to channel is gone.
1613 */
1614static void vmbus_chan_release(struct kobject *kobj)
1615{
1616 struct vmbus_channel *channel
1617 = container_of(kobj, struct vmbus_channel, kobj);
1618
1619 kfree_rcu(channel, rcu);
1620}
1621
1622struct vmbus_chan_attribute {
1623 struct attribute attr;
Kimberly Brown14948e32019-03-14 16:05:15 -04001624 ssize_t (*show)(struct vmbus_channel *chan, char *buf);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001625 ssize_t (*store)(struct vmbus_channel *chan,
1626 const char *buf, size_t count);
1627};
1628#define VMBUS_CHAN_ATTR(_name, _mode, _show, _store) \
1629 struct vmbus_chan_attribute chan_attr_##_name \
1630 = __ATTR(_name, _mode, _show, _store)
1631#define VMBUS_CHAN_ATTR_RW(_name) \
1632 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RW(_name)
1633#define VMBUS_CHAN_ATTR_RO(_name) \
1634 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RO(_name)
1635#define VMBUS_CHAN_ATTR_WO(_name) \
1636 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_WO(_name)
1637
1638static ssize_t vmbus_chan_attr_show(struct kobject *kobj,
1639 struct attribute *attr, char *buf)
1640{
1641 const struct vmbus_chan_attribute *attribute
1642 = container_of(attr, struct vmbus_chan_attribute, attr);
Kimberly Brown14948e32019-03-14 16:05:15 -04001643 struct vmbus_channel *chan
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001644 = container_of(kobj, struct vmbus_channel, kobj);
1645
1646 if (!attribute->show)
1647 return -EIO;
1648
1649 return attribute->show(chan, buf);
1650}
1651
Andrea Parri (Microsoft)75278102020-04-06 02:15:13 +02001652static ssize_t vmbus_chan_attr_store(struct kobject *kobj,
1653 struct attribute *attr, const char *buf,
1654 size_t count)
1655{
1656 const struct vmbus_chan_attribute *attribute
1657 = container_of(attr, struct vmbus_chan_attribute, attr);
1658 struct vmbus_channel *chan
1659 = container_of(kobj, struct vmbus_channel, kobj);
1660
1661 if (!attribute->store)
1662 return -EIO;
1663
1664 return attribute->store(chan, buf, count);
1665}
1666
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001667static const struct sysfs_ops vmbus_chan_sysfs_ops = {
1668 .show = vmbus_chan_attr_show,
Andrea Parri (Microsoft)75278102020-04-06 02:15:13 +02001669 .store = vmbus_chan_attr_store,
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001670};
1671
Kimberly Brown14948e32019-03-14 16:05:15 -04001672static ssize_t out_mask_show(struct vmbus_channel *channel, char *buf)
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001673{
Kimberly Brown14948e32019-03-14 16:05:15 -04001674 struct hv_ring_buffer_info *rbi = &channel->outbound;
1675 ssize_t ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001676
Kimberly Brown14948e32019-03-14 16:05:15 -04001677 mutex_lock(&rbi->ring_buffer_mutex);
1678 if (!rbi->ring_buffer) {
1679 mutex_unlock(&rbi->ring_buffer_mutex);
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001680 return -EINVAL;
Kimberly Brown14948e32019-03-14 16:05:15 -04001681 }
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001682
Kimberly Brown14948e32019-03-14 16:05:15 -04001683 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1684 mutex_unlock(&rbi->ring_buffer_mutex);
1685 return ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001686}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001687static VMBUS_CHAN_ATTR_RO(out_mask);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001688
Kimberly Brown14948e32019-03-14 16:05:15 -04001689static ssize_t in_mask_show(struct vmbus_channel *channel, char *buf)
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001690{
Kimberly Brown14948e32019-03-14 16:05:15 -04001691 struct hv_ring_buffer_info *rbi = &channel->inbound;
1692 ssize_t ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001693
Kimberly Brown14948e32019-03-14 16:05:15 -04001694 mutex_lock(&rbi->ring_buffer_mutex);
1695 if (!rbi->ring_buffer) {
1696 mutex_unlock(&rbi->ring_buffer_mutex);
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001697 return -EINVAL;
Kimberly Brown14948e32019-03-14 16:05:15 -04001698 }
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001699
Kimberly Brown14948e32019-03-14 16:05:15 -04001700 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1701 mutex_unlock(&rbi->ring_buffer_mutex);
1702 return ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001703}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001704static VMBUS_CHAN_ATTR_RO(in_mask);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001705
Kimberly Brown14948e32019-03-14 16:05:15 -04001706static ssize_t read_avail_show(struct vmbus_channel *channel, char *buf)
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001707{
Kimberly Brown14948e32019-03-14 16:05:15 -04001708 struct hv_ring_buffer_info *rbi = &channel->inbound;
1709 ssize_t ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001710
Kimberly Brown14948e32019-03-14 16:05:15 -04001711 mutex_lock(&rbi->ring_buffer_mutex);
1712 if (!rbi->ring_buffer) {
1713 mutex_unlock(&rbi->ring_buffer_mutex);
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001714 return -EINVAL;
Kimberly Brown14948e32019-03-14 16:05:15 -04001715 }
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001716
Kimberly Brown14948e32019-03-14 16:05:15 -04001717 ret = sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi));
1718 mutex_unlock(&rbi->ring_buffer_mutex);
1719 return ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001720}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001721static VMBUS_CHAN_ATTR_RO(read_avail);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001722
Kimberly Brown14948e32019-03-14 16:05:15 -04001723static ssize_t write_avail_show(struct vmbus_channel *channel, char *buf)
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001724{
Kimberly Brown14948e32019-03-14 16:05:15 -04001725 struct hv_ring_buffer_info *rbi = &channel->outbound;
1726 ssize_t ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001727
Kimberly Brown14948e32019-03-14 16:05:15 -04001728 mutex_lock(&rbi->ring_buffer_mutex);
1729 if (!rbi->ring_buffer) {
1730 mutex_unlock(&rbi->ring_buffer_mutex);
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001731 return -EINVAL;
Kimberly Brown14948e32019-03-14 16:05:15 -04001732 }
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001733
Kimberly Brown14948e32019-03-14 16:05:15 -04001734 ret = sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi));
1735 mutex_unlock(&rbi->ring_buffer_mutex);
1736 return ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001737}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001738static VMBUS_CHAN_ATTR_RO(write_avail);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001739
Andrea Parri (Microsoft)75278102020-04-06 02:15:13 +02001740static ssize_t target_cpu_show(struct vmbus_channel *channel, char *buf)
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001741{
1742 return sprintf(buf, "%u\n", channel->target_cpu);
1743}
Andrea Parri (Microsoft)75278102020-04-06 02:15:13 +02001744static ssize_t target_cpu_store(struct vmbus_channel *channel,
1745 const char *buf, size_t count)
1746{
Andrea Parri (Microsoft)afaa33d2020-05-22 19:19:01 +02001747 u32 target_cpu, origin_cpu;
Andrea Parri (Microsoft)75278102020-04-06 02:15:13 +02001748 ssize_t ret = count;
Andrea Parri (Microsoft)75278102020-04-06 02:15:13 +02001749
1750 if (vmbus_proto_version < VERSION_WIN10_V4_1)
1751 return -EIO;
1752
1753 if (sscanf(buf, "%uu", &target_cpu) != 1)
1754 return -EIO;
1755
1756 /* Validate target_cpu for the cpumask_test_cpu() operation below. */
1757 if (target_cpu >= nr_cpumask_bits)
1758 return -EINVAL;
1759
1760 /* No CPUs should come up or down during this. */
1761 cpus_read_lock();
1762
Andrea Parri (Microsoft)0a968202020-06-17 18:46:37 +02001763 if (!cpu_online(target_cpu)) {
Andrea Parri (Microsoft)75278102020-04-06 02:15:13 +02001764 cpus_read_unlock();
1765 return -EINVAL;
1766 }
1767
1768 /*
1769 * Synchronizes target_cpu_store() and channel closure:
1770 *
1771 * { Initially: state = CHANNEL_OPENED }
1772 *
1773 * CPU1 CPU2
1774 *
1775 * [target_cpu_store()] [vmbus_disconnect_ring()]
1776 *
1777 * LOCK channel_mutex LOCK channel_mutex
1778 * LOAD r1 = state LOAD r2 = state
1779 * IF (r1 == CHANNEL_OPENED) IF (r2 == CHANNEL_OPENED)
1780 * SEND MODIFYCHANNEL STORE state = CHANNEL_OPEN
1781 * [...] SEND CLOSECHANNEL
1782 * UNLOCK channel_mutex UNLOCK channel_mutex
1783 *
1784 * Forbids: r1 == r2 == CHANNEL_OPENED (i.e., CPU1's LOCK precedes
1785 * CPU2's LOCK) && CPU2's SEND precedes CPU1's SEND
1786 *
1787 * Note. The host processes the channel messages "sequentially", in
1788 * the order in which they are received on a per-partition basis.
1789 */
1790 mutex_lock(&vmbus_connection.channel_mutex);
1791
1792 /*
1793 * Hyper-V will ignore MODIFYCHANNEL messages for "non-open" channels;
1794 * avoid sending the message and fail here for such channels.
1795 */
1796 if (channel->state != CHANNEL_OPENED_STATE) {
1797 ret = -EIO;
1798 goto cpu_store_unlock;
1799 }
1800
Andrea Parri (Microsoft)afaa33d2020-05-22 19:19:01 +02001801 origin_cpu = channel->target_cpu;
1802 if (target_cpu == origin_cpu)
Andrea Parri (Microsoft)75278102020-04-06 02:15:13 +02001803 goto cpu_store_unlock;
1804
1805 if (vmbus_send_modifychannel(channel->offermsg.child_relid,
1806 hv_cpu_number_to_vp_number(target_cpu))) {
1807 ret = -EIO;
1808 goto cpu_store_unlock;
1809 }
1810
1811 /*
1812 * Warning. At this point, there is *no* guarantee that the host will
1813 * have successfully processed the vmbus_send_modifychannel() request.
1814 * See the header comment of vmbus_send_modifychannel() for more info.
1815 *
1816 * Lags in the processing of the above vmbus_send_modifychannel() can
1817 * result in missed interrupts if the "old" target CPU is taken offline
1818 * before Hyper-V starts sending interrupts to the "new" target CPU.
1819 * But apart from this offlining scenario, the code tolerates such
1820 * lags. It will function correctly even if a channel interrupt comes
1821 * in on a CPU that is different from the channel target_cpu value.
1822 */
1823
1824 channel->target_cpu = target_cpu;
Andrea Parri (Microsoft)75278102020-04-06 02:15:13 +02001825
Andrea Parri (Microsoft)afaa33d2020-05-22 19:19:01 +02001826 /* See init_vp_index(). */
1827 if (hv_is_perf_channel(channel))
1828 hv_update_alloced_cpus(origin_cpu, target_cpu);
1829
1830 /* Currently set only for storvsc channels. */
1831 if (channel->change_target_cpu_callback) {
1832 (*channel->change_target_cpu_callback)(channel,
1833 origin_cpu, target_cpu);
1834 }
1835
Andrea Parri (Microsoft)75278102020-04-06 02:15:13 +02001836cpu_store_unlock:
1837 mutex_unlock(&vmbus_connection.channel_mutex);
1838 cpus_read_unlock();
1839 return ret;
1840}
1841static VMBUS_CHAN_ATTR(cpu, 0644, target_cpu_show, target_cpu_store);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001842
Kimberly Brown14948e32019-03-14 16:05:15 -04001843static ssize_t channel_pending_show(struct vmbus_channel *channel,
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001844 char *buf)
1845{
1846 return sprintf(buf, "%d\n",
1847 channel_pending(channel,
1848 vmbus_connection.monitor_pages[1]));
1849}
Matheus Castellof0434de42020-11-15 16:57:30 -03001850static VMBUS_CHAN_ATTR(pending, 0444, channel_pending_show, NULL);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001851
Kimberly Brown14948e32019-03-14 16:05:15 -04001852static ssize_t channel_latency_show(struct vmbus_channel *channel,
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001853 char *buf)
1854{
1855 return sprintf(buf, "%d\n",
1856 channel_latency(channel,
1857 vmbus_connection.monitor_pages[1]));
1858}
Matheus Castellof0434de42020-11-15 16:57:30 -03001859static VMBUS_CHAN_ATTR(latency, 0444, channel_latency_show, NULL);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001860
Kimberly Brown14948e32019-03-14 16:05:15 -04001861static ssize_t channel_interrupts_show(struct vmbus_channel *channel, char *buf)
Stephen Hemminger6981fbf2017-10-29 11:33:40 -07001862{
1863 return sprintf(buf, "%llu\n", channel->interrupts);
1864}
Matheus Castellof0434de42020-11-15 16:57:30 -03001865static VMBUS_CHAN_ATTR(interrupts, 0444, channel_interrupts_show, NULL);
Stephen Hemminger6981fbf2017-10-29 11:33:40 -07001866
Kimberly Brown14948e32019-03-14 16:05:15 -04001867static ssize_t channel_events_show(struct vmbus_channel *channel, char *buf)
Stephen Hemminger6981fbf2017-10-29 11:33:40 -07001868{
1869 return sprintf(buf, "%llu\n", channel->sig_events);
1870}
Matheus Castellof0434de42020-11-15 16:57:30 -03001871static VMBUS_CHAN_ATTR(events, 0444, channel_events_show, NULL);
Stephen Hemminger6981fbf2017-10-29 11:33:40 -07001872
Kimberly Brown14948e32019-03-14 16:05:15 -04001873static ssize_t channel_intr_in_full_show(struct vmbus_channel *channel,
Kimberly Brown396ae572019-02-04 02:13:09 -05001874 char *buf)
1875{
1876 return sprintf(buf, "%llu\n",
1877 (unsigned long long)channel->intr_in_full);
1878}
1879static VMBUS_CHAN_ATTR(intr_in_full, 0444, channel_intr_in_full_show, NULL);
1880
Kimberly Brown14948e32019-03-14 16:05:15 -04001881static ssize_t channel_intr_out_empty_show(struct vmbus_channel *channel,
Kimberly Brown396ae572019-02-04 02:13:09 -05001882 char *buf)
1883{
1884 return sprintf(buf, "%llu\n",
1885 (unsigned long long)channel->intr_out_empty);
1886}
1887static VMBUS_CHAN_ATTR(intr_out_empty, 0444, channel_intr_out_empty_show, NULL);
1888
Kimberly Brown14948e32019-03-14 16:05:15 -04001889static ssize_t channel_out_full_first_show(struct vmbus_channel *channel,
Kimberly Brown396ae572019-02-04 02:13:09 -05001890 char *buf)
1891{
1892 return sprintf(buf, "%llu\n",
1893 (unsigned long long)channel->out_full_first);
1894}
1895static VMBUS_CHAN_ATTR(out_full_first, 0444, channel_out_full_first_show, NULL);
1896
Kimberly Brown14948e32019-03-14 16:05:15 -04001897static ssize_t channel_out_full_total_show(struct vmbus_channel *channel,
Kimberly Brown396ae572019-02-04 02:13:09 -05001898 char *buf)
1899{
1900 return sprintf(buf, "%llu\n",
1901 (unsigned long long)channel->out_full_total);
1902}
1903static VMBUS_CHAN_ATTR(out_full_total, 0444, channel_out_full_total_show, NULL);
1904
Kimberly Brown14948e32019-03-14 16:05:15 -04001905static ssize_t subchannel_monitor_id_show(struct vmbus_channel *channel,
Stephen Hemmingerf0fa2972018-01-09 10:29:06 -08001906 char *buf)
1907{
1908 return sprintf(buf, "%u\n", channel->offermsg.monitorid);
1909}
Matheus Castellof0434de42020-11-15 16:57:30 -03001910static VMBUS_CHAN_ATTR(monitor_id, 0444, subchannel_monitor_id_show, NULL);
Stephen Hemmingerf0fa2972018-01-09 10:29:06 -08001911
Kimberly Brown14948e32019-03-14 16:05:15 -04001912static ssize_t subchannel_id_show(struct vmbus_channel *channel,
Stephen Hemmingerf0fa2972018-01-09 10:29:06 -08001913 char *buf)
1914{
1915 return sprintf(buf, "%u\n",
1916 channel->offermsg.offer.sub_channel_index);
1917}
1918static VMBUS_CHAN_ATTR_RO(subchannel_id);
1919
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001920static struct attribute *vmbus_chan_attrs[] = {
1921 &chan_attr_out_mask.attr,
1922 &chan_attr_in_mask.attr,
1923 &chan_attr_read_avail.attr,
1924 &chan_attr_write_avail.attr,
1925 &chan_attr_cpu.attr,
1926 &chan_attr_pending.attr,
1927 &chan_attr_latency.attr,
Stephen Hemminger6981fbf2017-10-29 11:33:40 -07001928 &chan_attr_interrupts.attr,
1929 &chan_attr_events.attr,
Kimberly Brown396ae572019-02-04 02:13:09 -05001930 &chan_attr_intr_in_full.attr,
1931 &chan_attr_intr_out_empty.attr,
1932 &chan_attr_out_full_first.attr,
1933 &chan_attr_out_full_total.attr,
Stephen Hemmingerf0fa2972018-01-09 10:29:06 -08001934 &chan_attr_monitor_id.attr,
1935 &chan_attr_subchannel_id.attr,
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001936 NULL
1937};
1938
Kimberly Brown46fc1542019-03-19 00:04:01 -04001939/*
1940 * Channel-level attribute_group callback function. Returns the permission for
1941 * each attribute, and returns 0 if an attribute is not visible.
1942 */
1943static umode_t vmbus_chan_attr_is_visible(struct kobject *kobj,
1944 struct attribute *attr, int idx)
1945{
1946 const struct vmbus_channel *channel =
1947 container_of(kobj, struct vmbus_channel, kobj);
1948
1949 /* Hide the monitor attributes if the monitor mechanism is not used. */
1950 if (!channel->offermsg.monitor_allocated &&
1951 (attr == &chan_attr_pending.attr ||
1952 attr == &chan_attr_latency.attr ||
1953 attr == &chan_attr_monitor_id.attr))
1954 return 0;
1955
1956 return attr->mode;
1957}
1958
1959static struct attribute_group vmbus_chan_group = {
1960 .attrs = vmbus_chan_attrs,
1961 .is_visible = vmbus_chan_attr_is_visible
1962};
1963
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001964static struct kobj_type vmbus_chan_ktype = {
1965 .sysfs_ops = &vmbus_chan_sysfs_ops,
1966 .release = vmbus_chan_release,
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001967};
1968
1969/*
1970 * vmbus_add_channel_kobj - setup a sub-directory under device/channels
1971 */
1972int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
1973{
Kimberly Brown46fc1542019-03-19 00:04:01 -04001974 const struct device *device = &dev->device;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001975 struct kobject *kobj = &channel->kobj;
1976 u32 relid = channel->offermsg.child_relid;
1977 int ret;
1978
1979 kobj->kset = dev->channels_kset;
1980 ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
1981 "%u", relid);
1982 if (ret)
1983 return ret;
1984
Kimberly Brown46fc1542019-03-19 00:04:01 -04001985 ret = sysfs_create_group(kobj, &vmbus_chan_group);
1986
1987 if (ret) {
1988 /*
1989 * The calling functions' error handling paths will cleanup the
1990 * empty channel directory.
1991 */
1992 dev_err(device, "Unable to set up channel sysfs files\n");
1993 return ret;
1994 }
1995
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001996 kobject_uevent(kobj, KOBJ_ADD);
1997
1998 return 0;
1999}
2000
Hank Janssen3e189512010-03-04 22:11:00 +00002001/*
Kimberly Brown46fc1542019-03-19 00:04:01 -04002002 * vmbus_remove_channel_attr_group - remove the channel's attribute group
2003 */
2004void vmbus_remove_channel_attr_group(struct vmbus_channel *channel)
2005{
2006 sysfs_remove_group(&channel->kobj, &vmbus_chan_group);
2007}
2008
2009/*
K. Y. Srinivasanf2c73012011-09-08 07:24:12 -07002010 * vmbus_device_create - Creates and registers a new child device
Hank Janssen3e189512010-03-04 22:11:00 +00002011 * on the vmbus.
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07002012 */
Andy Shevchenko593db802019-01-10 16:25:32 +02002013struct hv_device *vmbus_device_create(const guid_t *type,
2014 const guid_t *instance,
stephen hemminger1b9d48f2014-06-03 08:38:15 -07002015 struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -07002016{
Nicolas Palix3d3b5512009-07-28 17:32:53 +02002017 struct hv_device *child_device_obj;
Hank Janssen3e7ee492009-07-13 16:02:34 -07002018
K. Y. Srinivasan6bad88da2011-03-07 13:35:48 -08002019 child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
2020 if (!child_device_obj) {
Hank Janssen0a466182011-03-29 13:58:47 -07002021 pr_err("Unable to allocate device object for child device\n");
Hank Janssen3e7ee492009-07-13 16:02:34 -07002022 return NULL;
2023 }
2024
Greg Kroah-Hartmancae5b842010-10-21 09:05:27 -07002025 child_device_obj->channel = channel;
Andy Shevchenko593db802019-01-10 16:25:32 +02002026 guid_copy(&child_device_obj->dev_type, type);
2027 guid_copy(&child_device_obj->dev_instance, instance);
K. Y. Srinivasan7047f172015-12-25 20:00:30 -08002028 child_device_obj->vendor_id = 0x1414; /* MSFT vendor ID */
Hank Janssen3e7ee492009-07-13 16:02:34 -07002029
Hank Janssen3e7ee492009-07-13 16:02:34 -07002030 return child_device_obj;
2031}
2032
Hank Janssen3e189512010-03-04 22:11:00 +00002033/*
K. Y. Srinivasan227942812011-09-08 07:24:13 -07002034 * vmbus_device_register - Register the child device
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07002035 */
K. Y. Srinivasan227942812011-09-08 07:24:13 -07002036int vmbus_device_register(struct hv_device *child_device_obj)
Hank Janssen3e7ee492009-07-13 16:02:34 -07002037{
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07002038 struct kobject *kobj = &child_device_obj->device.kobj;
2039 int ret;
K. Y. Srinivasan6bad88da2011-03-07 13:35:48 -08002040
Stephen Hemmingerf6b2db02016-11-01 00:01:59 -07002041 dev_set_name(&child_device_obj->device, "%pUl",
Andy Shevchenko458c4472020-04-23 16:45:03 +03002042 &child_device_obj->channel->offermsg.offer.if_instance);
Hank Janssen3e7ee492009-07-13 16:02:34 -07002043
K. Y. Srinivasan0bce28b2011-08-27 11:31:39 -07002044 child_device_obj->device.bus = &hv_bus;
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -07002045 child_device_obj->device.parent = &hv_acpi_dev->dev;
K. Y. Srinivasan6bad88da2011-03-07 13:35:48 -08002046 child_device_obj->device.release = vmbus_device_release;
Hank Janssen3e7ee492009-07-13 16:02:34 -07002047
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07002048 /*
2049 * Register with the LDM. This will kick off the driver/device
2050 * binding...which will eventually call vmbus_match() and vmbus_probe()
2051 */
K. Y. Srinivasan6bad88da2011-03-07 13:35:48 -08002052 ret = device_register(&child_device_obj->device);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07002053 if (ret) {
Hank Janssen0a466182011-03-29 13:58:47 -07002054 pr_err("Unable to register child device\n");
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07002055 return ret;
2056 }
Hank Janssen3e7ee492009-07-13 16:02:34 -07002057
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07002058 child_device_obj->channels_kset = kset_create_and_add("channels",
2059 NULL, kobj);
2060 if (!child_device_obj->channels_kset) {
2061 ret = -ENOMEM;
2062 goto err_dev_unregister;
2063 }
2064
2065 ret = vmbus_add_channel_kobj(child_device_obj,
2066 child_device_obj->channel);
2067 if (ret) {
2068 pr_err("Unable to register primary channeln");
2069 goto err_kset_unregister;
2070 }
Branden Bonabyaf9ca6f2019-10-03 17:01:49 -04002071 hv_debug_add_dev_dir(child_device_obj);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07002072
2073 return 0;
2074
2075err_kset_unregister:
2076 kset_unregister(child_device_obj->channels_kset);
2077
2078err_dev_unregister:
2079 device_unregister(&child_device_obj->device);
Hank Janssen3e7ee492009-07-13 16:02:34 -07002080 return ret;
2081}
2082
Hank Janssen3e189512010-03-04 22:11:00 +00002083/*
K. Y. Srinivasan696453b2011-09-08 07:24:14 -07002084 * vmbus_device_unregister - Remove the specified child device
Hank Janssen3e189512010-03-04 22:11:00 +00002085 * from the vmbus.
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07002086 */
K. Y. Srinivasan696453b2011-09-08 07:24:14 -07002087void vmbus_device_unregister(struct hv_device *device_obj)
Hank Janssen3e7ee492009-07-13 16:02:34 -07002088{
Fernando Soto84672362013-06-14 23:13:35 +00002089 pr_debug("child device %s unregistered\n",
2090 dev_name(&device_obj->device));
2091
Dexuan Cui869b5562017-11-14 06:53:32 -07002092 kset_unregister(device_obj->channels_kset);
2093
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07002094 /*
2095 * Kick off the process of unregistering the device.
2096 * This will call vmbus_remove() and eventually vmbus_device_release()
2097 */
K. Y. Srinivasan6bad88da2011-03-07 13:35:48 -08002098 device_unregister(&device_obj->device);
Hank Janssen3e7ee492009-07-13 16:02:34 -07002099}
2100
Hank Janssen3e7ee492009-07-13 16:02:34 -07002101
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002102/*
Jake Oshins7f163a62015-08-05 00:52:36 -07002103 * VMBUS is an acpi enumerated device. Get the information we
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002104 * need from DSDT.
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002105 */
Jake Oshins7f163a62015-08-05 00:52:36 -07002106#define VTPM_BASE_ADDRESS 0xfed40000
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002107static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002108{
Jake Oshins7f163a62015-08-05 00:52:36 -07002109 resource_size_t start = 0;
2110 resource_size_t end = 0;
2111 struct resource *new_res;
2112 struct resource **old_res = &hyperv_mmio;
2113 struct resource **prev_res = NULL;
Michael Kelley626b9012020-08-14 12:45:04 -07002114 struct resource r;
Jake Oshins7f163a62015-08-05 00:52:36 -07002115
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002116 switch (res->type) {
Jake Oshins7f163a62015-08-05 00:52:36 -07002117
2118 /*
2119 * "Address" descriptors are for bus windows. Ignore
2120 * "memory" descriptors, which are for registers on
2121 * devices.
2122 */
2123 case ACPI_RESOURCE_TYPE_ADDRESS32:
2124 start = res->data.address32.address.minimum;
2125 end = res->data.address32.address.maximum;
Gerd Hoffmann4eb923f2014-02-24 14:17:08 +01002126 break;
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002127
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002128 case ACPI_RESOURCE_TYPE_ADDRESS64:
Jake Oshins7f163a62015-08-05 00:52:36 -07002129 start = res->data.address64.address.minimum;
2130 end = res->data.address64.address.maximum;
Gerd Hoffmann4eb923f2014-02-24 14:17:08 +01002131 break;
Jake Oshins7f163a62015-08-05 00:52:36 -07002132
Michael Kelley626b9012020-08-14 12:45:04 -07002133 /*
2134 * The IRQ information is needed only on ARM64, which Hyper-V
2135 * sets up in the extended format. IRQ information is present
2136 * on x86/x64 in the non-extended format but it is not used by
2137 * Linux. So don't bother checking for the non-extended format.
2138 */
2139 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
2140 if (!acpi_dev_resource_interrupt(res, 0, &r)) {
2141 pr_err("Unable to parse Hyper-V ACPI interrupt\n");
2142 return AE_ERROR;
2143 }
2144 /* ARM64 INTID for VMbus */
2145 vmbus_interrupt = res->data.extended_irq.interrupts[0];
2146 /* Linux IRQ number */
2147 vmbus_irq = r.start;
2148 return AE_OK;
2149
Jake Oshins7f163a62015-08-05 00:52:36 -07002150 default:
2151 /* Unused resource type */
2152 return AE_OK;
2153
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002154 }
Jake Oshins7f163a62015-08-05 00:52:36 -07002155 /*
2156 * Ignore ranges that are below 1MB, as they're not
2157 * necessary or useful here.
2158 */
2159 if (end < 0x100000)
2160 return AE_OK;
2161
2162 new_res = kzalloc(sizeof(*new_res), GFP_ATOMIC);
2163 if (!new_res)
2164 return AE_NO_MEMORY;
2165
2166 /* If this range overlaps the virtual TPM, truncate it. */
2167 if (end > VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS)
2168 end = VTPM_BASE_ADDRESS;
2169
2170 new_res->name = "hyperv mmio";
2171 new_res->flags = IORESOURCE_MEM;
2172 new_res->start = start;
2173 new_res->end = end;
2174
Jake Oshins40f26f32015-12-14 16:01:52 -08002175 /*
Jake Oshins40f26f32015-12-14 16:01:52 -08002176 * If two ranges are adjacent, merge them.
2177 */
Jake Oshins7f163a62015-08-05 00:52:36 -07002178 do {
2179 if (!*old_res) {
2180 *old_res = new_res;
2181 break;
2182 }
2183
Jake Oshins40f26f32015-12-14 16:01:52 -08002184 if (((*old_res)->end + 1) == new_res->start) {
2185 (*old_res)->end = new_res->end;
2186 kfree(new_res);
2187 break;
2188 }
2189
2190 if ((*old_res)->start == new_res->end + 1) {
2191 (*old_res)->start = new_res->start;
2192 kfree(new_res);
2193 break;
2194 }
2195
Jake Oshins23a06832016-04-05 10:22:53 -07002196 if ((*old_res)->start > new_res->end) {
Jake Oshins7f163a62015-08-05 00:52:36 -07002197 new_res->sibling = *old_res;
2198 if (prev_res)
2199 (*prev_res)->sibling = new_res;
2200 *old_res = new_res;
2201 break;
2202 }
2203
2204 prev_res = old_res;
2205 old_res = &(*old_res)->sibling;
2206
2207 } while (1);
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002208
2209 return AE_OK;
2210}
2211
Jake Oshins7f163a62015-08-05 00:52:36 -07002212static int vmbus_acpi_remove(struct acpi_device *device)
2213{
2214 struct resource *cur_res;
2215 struct resource *next_res;
2216
2217 if (hyperv_mmio) {
Jake Oshins6d146ae2016-04-05 10:22:55 -07002218 if (fb_mmio) {
2219 __release_region(hyperv_mmio, fb_mmio->start,
2220 resource_size(fb_mmio));
2221 fb_mmio = NULL;
2222 }
2223
Jake Oshins7f163a62015-08-05 00:52:36 -07002224 for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) {
2225 next_res = cur_res->sibling;
2226 kfree(cur_res);
2227 }
2228 }
2229
2230 return 0;
2231}
2232
Jake Oshins6d146ae2016-04-05 10:22:55 -07002233static void vmbus_reserve_fb(void)
2234{
2235 int size;
2236 /*
2237 * Make a claim for the frame buffer in the resource tree under the
2238 * first node, which will be the one below 4GB. The length seems to
2239 * be underreported, particularly in a Generation 1 VM. So start out
2240 * reserving a larger area and make it smaller until it succeeds.
2241 */
2242
2243 if (screen_info.lfb_base) {
2244 if (efi_enabled(EFI_BOOT))
2245 size = max_t(__u32, screen_info.lfb_size, 0x800000);
2246 else
2247 size = max_t(__u32, screen_info.lfb_size, 0x4000000);
2248
2249 for (; !fb_mmio && (size >= 0x100000); size >>= 1) {
2250 fb_mmio = __request_region(hyperv_mmio,
2251 screen_info.lfb_base, size,
2252 fb_mmio_name, 0);
2253 }
2254 }
2255}
2256
Jake Oshins35464482015-08-05 00:52:37 -07002257/**
2258 * vmbus_allocate_mmio() - Pick a memory-mapped I/O range.
2259 * @new: If successful, supplied a pointer to the
2260 * allocated MMIO space.
2261 * @device_obj: Identifies the caller
2262 * @min: Minimum guest physical address of the
2263 * allocation
2264 * @max: Maximum guest physical address
2265 * @size: Size of the range to be allocated
2266 * @align: Alignment of the range to be allocated
2267 * @fb_overlap_ok: Whether this allocation can be allowed
2268 * to overlap the video frame buffer.
2269 *
2270 * This function walks the resources granted to VMBus by the
2271 * _CRS object in the ACPI namespace underneath the parent
2272 * "bridge" whether that's a root PCI bus in the Generation 1
2273 * case or a Module Device in the Generation 2 case. It then
2274 * attempts to allocate from the global MMIO pool in a way that
2275 * matches the constraints supplied in these parameters and by
2276 * that _CRS.
2277 *
2278 * Return: 0 on success, -errno on failure
2279 */
2280int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
2281 resource_size_t min, resource_size_t max,
2282 resource_size_t size, resource_size_t align,
2283 bool fb_overlap_ok)
2284{
Jake Oshinsbe000f92016-04-05 10:22:54 -07002285 struct resource *iter, *shadow;
Jake Oshinsea37a6b2016-04-05 10:22:56 -07002286 resource_size_t range_min, range_max, start;
Jake Oshins35464482015-08-05 00:52:37 -07002287 const char *dev_n = dev_name(&device_obj->device);
Jake Oshinsea37a6b2016-04-05 10:22:56 -07002288 int retval;
Jake Oshinse16dad62016-04-05 10:22:50 -07002289
2290 retval = -ENXIO;
Davidlohr Bueso8aea7f82019-11-01 13:00:04 -07002291 mutex_lock(&hyperv_mmio_lock);
Jake Oshins35464482015-08-05 00:52:37 -07002292
Jake Oshinsea37a6b2016-04-05 10:22:56 -07002293 /*
2294 * If overlaps with frame buffers are allowed, then first attempt to
2295 * make the allocation from within the reserved region. Because it
2296 * is already reserved, no shadow allocation is necessary.
2297 */
2298 if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) &&
2299 !(max < fb_mmio->start)) {
2300
2301 range_min = fb_mmio->start;
2302 range_max = fb_mmio->end;
2303 start = (range_min + align - 1) & ~(align - 1);
2304 for (; start + size - 1 <= range_max; start += align) {
2305 *new = request_mem_region_exclusive(start, size, dev_n);
2306 if (*new) {
2307 retval = 0;
2308 goto exit;
2309 }
2310 }
2311 }
2312
Jake Oshins35464482015-08-05 00:52:37 -07002313 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
2314 if ((iter->start >= max) || (iter->end <= min))
2315 continue;
2316
2317 range_min = iter->start;
2318 range_max = iter->end;
Jake Oshinsea37a6b2016-04-05 10:22:56 -07002319 start = (range_min + align - 1) & ~(align - 1);
2320 for (; start + size - 1 <= range_max; start += align) {
2321 shadow = __request_region(iter, start, size, NULL,
2322 IORESOURCE_BUSY);
2323 if (!shadow)
2324 continue;
Jake Oshins35464482015-08-05 00:52:37 -07002325
Jake Oshinsea37a6b2016-04-05 10:22:56 -07002326 *new = request_mem_region_exclusive(start, size, dev_n);
2327 if (*new) {
2328 shadow->name = (char *)*new;
2329 retval = 0;
2330 goto exit;
Jake Oshins35464482015-08-05 00:52:37 -07002331 }
2332
Jake Oshinsea37a6b2016-04-05 10:22:56 -07002333 __release_region(iter, start, size);
Jake Oshins35464482015-08-05 00:52:37 -07002334 }
2335 }
2336
Jake Oshinse16dad62016-04-05 10:22:50 -07002337exit:
Davidlohr Bueso8aea7f82019-11-01 13:00:04 -07002338 mutex_unlock(&hyperv_mmio_lock);
Jake Oshinse16dad62016-04-05 10:22:50 -07002339 return retval;
Jake Oshins35464482015-08-05 00:52:37 -07002340}
2341EXPORT_SYMBOL_GPL(vmbus_allocate_mmio);
2342
Jake Oshins619848b2015-12-14 16:01:39 -08002343/**
Jake Oshins97fb77dc2016-04-05 10:22:51 -07002344 * vmbus_free_mmio() - Free a memory-mapped I/O range.
2345 * @start: Base address of region to release.
2346 * @size: Size of the range to be allocated
2347 *
2348 * This function releases anything requested by
2349 * vmbus_mmio_allocate().
2350 */
2351void vmbus_free_mmio(resource_size_t start, resource_size_t size)
2352{
Jake Oshinsbe000f92016-04-05 10:22:54 -07002353 struct resource *iter;
2354
Davidlohr Bueso8aea7f82019-11-01 13:00:04 -07002355 mutex_lock(&hyperv_mmio_lock);
Jake Oshinsbe000f92016-04-05 10:22:54 -07002356 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
2357 if ((iter->start >= start + size) || (iter->end <= start))
2358 continue;
2359
2360 __release_region(iter, start, size);
2361 }
Jake Oshins97fb77dc2016-04-05 10:22:51 -07002362 release_mem_region(start, size);
Davidlohr Bueso8aea7f82019-11-01 13:00:04 -07002363 mutex_unlock(&hyperv_mmio_lock);
Jake Oshins97fb77dc2016-04-05 10:22:51 -07002364
2365}
2366EXPORT_SYMBOL_GPL(vmbus_free_mmio);
2367
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002368static int vmbus_acpi_add(struct acpi_device *device)
2369{
2370 acpi_status result;
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002371 int ret_val = -ENODEV;
Jake Oshins7f163a62015-08-05 00:52:36 -07002372 struct acpi_device *ancestor;
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002373
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -07002374 hv_acpi_dev = device;
2375
K. Y. Srinivasan0a4425b2011-08-27 11:31:38 -07002376 result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002377 vmbus_walk_resources, NULL);
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002378
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002379 if (ACPI_FAILURE(result))
2380 goto acpi_walk_err;
2381 /*
Jake Oshins7f163a62015-08-05 00:52:36 -07002382 * Some ancestor of the vmbus acpi device (Gen1 or Gen2
2383 * firmware) is the VMOD that has the mmio ranges. Get that.
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002384 */
Jake Oshins7f163a62015-08-05 00:52:36 -07002385 for (ancestor = device->parent; ancestor; ancestor = ancestor->parent) {
2386 result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS,
2387 vmbus_walk_resources, NULL);
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002388
2389 if (ACPI_FAILURE(result))
Jake Oshins7f163a62015-08-05 00:52:36 -07002390 continue;
Jake Oshins6d146ae2016-04-05 10:22:55 -07002391 if (hyperv_mmio) {
2392 vmbus_reserve_fb();
Jake Oshins7f163a62015-08-05 00:52:36 -07002393 break;
Jake Oshins6d146ae2016-04-05 10:22:55 -07002394 }
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002395 }
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002396 ret_val = 0;
2397
2398acpi_walk_err:
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002399 complete(&probe_event);
Jake Oshins7f163a62015-08-05 00:52:36 -07002400 if (ret_val)
2401 vmbus_acpi_remove(device);
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002402 return ret_val;
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002403}
2404
Dexuan Cui83b50f82019-09-19 21:46:12 +00002405#ifdef CONFIG_PM_SLEEP
Dexuan Cuif53335e2019-09-05 23:01:19 +00002406static int vmbus_bus_suspend(struct device *dev)
2407{
Dexuan Cuib307b382019-09-05 23:01:21 +00002408 struct vmbus_channel *channel, *sc;
Dexuan Cui1f48dcf2019-09-05 23:01:20 +00002409
2410 while (atomic_read(&vmbus_connection.offer_in_progress) != 0) {
2411 /*
2412 * We wait here until the completion of any channel
2413 * offers that are currently in progress.
2414 */
Matheus Castello14c685d2020-11-15 16:57:34 -03002415 usleep_range(1000, 2000);
Dexuan Cui1f48dcf2019-09-05 23:01:20 +00002416 }
2417
2418 mutex_lock(&vmbus_connection.channel_mutex);
2419 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
2420 if (!is_hvsock_channel(channel))
2421 continue;
2422
2423 vmbus_force_channel_rescinded(channel);
2424 }
2425 mutex_unlock(&vmbus_connection.channel_mutex);
2426
Dexuan Cuib307b382019-09-05 23:01:21 +00002427 /*
2428 * Wait until all the sub-channels and hv_sock channels have been
2429 * cleaned up. Sub-channels should be destroyed upon suspend, otherwise
2430 * they would conflict with the new sub-channels that will be created
2431 * in the resume path. hv_sock channels should also be destroyed, but
2432 * a hv_sock channel of an established hv_sock connection can not be
2433 * really destroyed since it may still be referenced by the userspace
2434 * application, so we just force the hv_sock channel to be rescinded
2435 * by vmbus_force_channel_rescinded(), and the userspace application
2436 * will thoroughly destroy the channel after hibernation.
2437 *
2438 * Note: the counter nr_chan_close_on_suspend may never go above 0 if
2439 * the VM has no sub-channel and hv_sock channel, e.g. a 1-vCPU VM.
2440 */
2441 if (atomic_read(&vmbus_connection.nr_chan_close_on_suspend) > 0)
2442 wait_for_completion(&vmbus_connection.ready_for_suspend_event);
2443
Dexuan Cui19873eec2020-09-04 19:55:55 -07002444 if (atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) != 0) {
2445 pr_err("Can not suspend due to a previous failed resuming\n");
2446 return -EBUSY;
2447 }
Dexuan Cuid8bd2d42019-09-05 23:01:22 +00002448
Dexuan Cuib307b382019-09-05 23:01:21 +00002449 mutex_lock(&vmbus_connection.channel_mutex);
2450
2451 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
Dexuan Cuid8bd2d42019-09-05 23:01:22 +00002452 /*
Andrea Parri (Microsoft)8b6a8772020-04-06 02:15:06 +02002453 * Remove the channel from the array of channels and invalidate
2454 * the channel's relid. Upon resume, vmbus_onoffer() will fix
2455 * up the relid (and other fields, if necessary) and add the
2456 * channel back to the array.
Dexuan Cuid8bd2d42019-09-05 23:01:22 +00002457 */
Andrea Parri (Microsoft)8b6a8772020-04-06 02:15:06 +02002458 vmbus_channel_unmap_relid(channel);
Dexuan Cuid8bd2d42019-09-05 23:01:22 +00002459 channel->offermsg.child_relid = INVALID_RELID;
2460
Dexuan Cuib307b382019-09-05 23:01:21 +00002461 if (is_hvsock_channel(channel)) {
2462 if (!channel->rescind) {
2463 pr_err("hv_sock channel not rescinded!\n");
2464 WARN_ON_ONCE(1);
2465 }
2466 continue;
2467 }
2468
Dexuan Cuib307b382019-09-05 23:01:21 +00002469 list_for_each_entry(sc, &channel->sc_list, sc_list) {
2470 pr_err("Sub-channel not deleted!\n");
2471 WARN_ON_ONCE(1);
2472 }
Dexuan Cuid8bd2d42019-09-05 23:01:22 +00002473
2474 atomic_inc(&vmbus_connection.nr_chan_fixup_on_resume);
Dexuan Cuib307b382019-09-05 23:01:21 +00002475 }
2476
2477 mutex_unlock(&vmbus_connection.channel_mutex);
2478
Dexuan Cuif53335e2019-09-05 23:01:19 +00002479 vmbus_initiate_unload(false);
2480
Dexuan Cuid8bd2d42019-09-05 23:01:22 +00002481 /* Reset the event for the next resume. */
2482 reinit_completion(&vmbus_connection.ready_for_resume_event);
2483
Dexuan Cuif53335e2019-09-05 23:01:19 +00002484 return 0;
2485}
2486
2487static int vmbus_bus_resume(struct device *dev)
2488{
2489 struct vmbus_channel_msginfo *msginfo;
2490 size_t msgsize;
2491 int ret;
2492
2493 /*
2494 * We only use the 'vmbus_proto_version', which was in use before
2495 * hibernation, to re-negotiate with the host.
2496 */
Andrea Parribedc61a2019-10-15 13:46:44 +02002497 if (!vmbus_proto_version) {
Dexuan Cuif53335e2019-09-05 23:01:19 +00002498 pr_err("Invalid proto version = 0x%x\n", vmbus_proto_version);
2499 return -EINVAL;
2500 }
2501
2502 msgsize = sizeof(*msginfo) +
2503 sizeof(struct vmbus_channel_initiate_contact);
2504
2505 msginfo = kzalloc(msgsize, GFP_KERNEL);
2506
2507 if (msginfo == NULL)
2508 return -ENOMEM;
2509
2510 ret = vmbus_negotiate_version(msginfo, vmbus_proto_version);
2511
2512 kfree(msginfo);
2513
2514 if (ret != 0)
2515 return ret;
2516
Dexuan Cuid8bd2d42019-09-05 23:01:22 +00002517 WARN_ON(atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) == 0);
2518
Dexuan Cuif53335e2019-09-05 23:01:19 +00002519 vmbus_request_offers();
2520
Dexuan Cui19873eec2020-09-04 19:55:55 -07002521 if (wait_for_completion_timeout(
2522 &vmbus_connection.ready_for_resume_event, 10 * HZ) == 0)
2523 pr_err("Some vmbus device is missing after suspending?\n");
Dexuan Cuid8bd2d42019-09-05 23:01:22 +00002524
Dexuan Cuib307b382019-09-05 23:01:21 +00002525 /* Reset the event for the next suspend. */
2526 reinit_completion(&vmbus_connection.ready_for_suspend_event);
2527
Dexuan Cuif53335e2019-09-05 23:01:19 +00002528 return 0;
2529}
Dexuan Cui1a06d012020-04-11 20:50:35 -07002530#else
2531#define vmbus_bus_suspend NULL
2532#define vmbus_bus_resume NULL
Dexuan Cui83b50f82019-09-19 21:46:12 +00002533#endif /* CONFIG_PM_SLEEP */
Dexuan Cuif53335e2019-09-05 23:01:19 +00002534
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002535static const struct acpi_device_id vmbus_acpi_device_ids[] = {
2536 {"VMBUS", 0},
K. Y. Srinivasan9d7b18d2011-06-06 15:49:42 -07002537 {"VMBus", 0},
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002538 {"", 0},
2539};
2540MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
2541
Dexuan Cuif53335e2019-09-05 23:01:19 +00002542/*
Dexuan Cui1a06d012020-04-11 20:50:35 -07002543 * Note: we must use the "no_irq" ops, otherwise hibernation can not work with
2544 * PCI device assignment, because "pci_dev_pm_ops" uses the "noirq" ops: in
2545 * the resume path, the pci "noirq" restore op runs before "non-noirq" op (see
Dexuan Cuif53335e2019-09-05 23:01:19 +00002546 * resume_target_kernel() -> dpm_resume_start(), and hibernation_restore() ->
2547 * dpm_resume_end()). This means vmbus_bus_resume() and the pci-hyperv's
Dexuan Cui1a06d012020-04-11 20:50:35 -07002548 * resume callback must also run via the "noirq" ops.
2549 *
2550 * Set suspend_noirq/resume_noirq to NULL for Suspend-to-Idle: see the comment
2551 * earlier in this file before vmbus_pm.
Dexuan Cuif53335e2019-09-05 23:01:19 +00002552 */
Dexuan Cui1a06d012020-04-11 20:50:35 -07002553
Dexuan Cuif53335e2019-09-05 23:01:19 +00002554static const struct dev_pm_ops vmbus_bus_pm = {
Dexuan Cui1a06d012020-04-11 20:50:35 -07002555 .suspend_noirq = NULL,
2556 .resume_noirq = NULL,
2557 .freeze_noirq = vmbus_bus_suspend,
2558 .thaw_noirq = vmbus_bus_resume,
2559 .poweroff_noirq = vmbus_bus_suspend,
2560 .restore_noirq = vmbus_bus_resume
Dexuan Cuif53335e2019-09-05 23:01:19 +00002561};
2562
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002563static struct acpi_driver vmbus_acpi_driver = {
2564 .name = "vmbus",
2565 .ids = vmbus_acpi_device_ids,
2566 .ops = {
2567 .add = vmbus_acpi_add,
Vitaly Kuznetsove4ecb412015-04-22 21:31:28 -07002568 .remove = vmbus_acpi_remove,
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002569 },
Dexuan Cuif53335e2019-09-05 23:01:19 +00002570 .drv.pm = &vmbus_bus_pm,
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002571};
2572
Vitaly Kuznetsov25172812015-08-01 16:08:07 -07002573static void hv_kexec_handler(void)
2574{
Michael Kelleyfd1fea62019-07-01 04:25:56 +00002575 hv_stimer_global_cleanup();
Vitaly Kuznetsov75ff3a82016-02-26 15:13:16 -08002576 vmbus_initiate_unload(false);
Vitaly Kuznetsov523b9402016-12-07 14:53:12 -08002577 /* Make sure conn_state is set as hv_synic_cleanup checks for it */
2578 mb();
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -08002579 cpuhp_remove_state(hyperv_cpuhp_online);
Vitaly Kuznetsov25172812015-08-01 16:08:07 -07002580};
2581
Vitaly Kuznetsovb4370df2015-08-01 16:08:09 -07002582static void hv_crash_handler(struct pt_regs *regs)
2583{
Michael Kelleyfd1fea62019-07-01 04:25:56 +00002584 int cpu;
2585
Vitaly Kuznetsov75ff3a82016-02-26 15:13:16 -08002586 vmbus_initiate_unload(true);
Vitaly Kuznetsovb4370df2015-08-01 16:08:09 -07002587 /*
2588 * In crash handler we can't schedule synic cleanup for all CPUs,
2589 * doing the cleanup for current CPU only. This should be sufficient
2590 * for kdump.
2591 */
Michael Kelleyfd1fea62019-07-01 04:25:56 +00002592 cpu = smp_processor_id();
2593 hv_stimer_cleanup(cpu);
Michael Kelley7a1323b2019-11-14 06:32:01 +00002594 hv_synic_disable_regs(cpu);
Vitaly Kuznetsovb4370df2015-08-01 16:08:09 -07002595};
2596
Dexuan Cui63ecc6d2019-09-05 23:01:16 +00002597static int hv_synic_suspend(void)
2598{
2599 /*
Michael Kelley4df4cb9e92019-11-13 01:11:49 +00002600 * When we reach here, all the non-boot CPUs have been offlined.
2601 * If we're in a legacy configuration where stimer Direct Mode is
2602 * not enabled, the stimers on the non-boot CPUs have been unbound
2603 * in hv_synic_cleanup() -> hv_stimer_legacy_cleanup() ->
Dexuan Cui63ecc6d2019-09-05 23:01:16 +00002604 * hv_stimer_cleanup() -> clockevents_unbind_device().
2605 *
Michael Kelley4df4cb9e92019-11-13 01:11:49 +00002606 * hv_synic_suspend() only runs on CPU0 with interrupts disabled.
2607 * Here we do not call hv_stimer_legacy_cleanup() on CPU0 because:
2608 * 1) it's unnecessary as interrupts remain disabled between
2609 * syscore_suspend() and syscore_resume(): see create_image() and
2610 * resume_target_kernel()
Dexuan Cui63ecc6d2019-09-05 23:01:16 +00002611 * 2) the stimer on CPU0 is automatically disabled later by
2612 * syscore_suspend() -> timekeeping_suspend() -> tick_suspend() -> ...
Michael Kelley4df4cb9e92019-11-13 01:11:49 +00002613 * -> clockevents_shutdown() -> ... -> hv_ce_shutdown()
2614 * 3) a warning would be triggered if we call
2615 * clockevents_unbind_device(), which may sleep, in an
2616 * interrupts-disabled context.
Dexuan Cui63ecc6d2019-09-05 23:01:16 +00002617 */
2618
2619 hv_synic_disable_regs(0);
2620
2621 return 0;
2622}
2623
2624static void hv_synic_resume(void)
2625{
2626 hv_synic_enable_regs(0);
2627
2628 /*
2629 * Note: we don't need to call hv_stimer_init(0), because the timer
2630 * on CPU0 is not unbound in hv_synic_suspend(), and the timer is
2631 * automatically re-enabled in timekeeping_resume().
2632 */
2633}
2634
2635/* The callbacks run only on CPU0, with irqs_disabled. */
2636static struct syscore_ops hv_synic_syscore_ops = {
2637 .suspend = hv_synic_suspend,
2638 .resume = hv_synic_resume,
2639};
2640
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -07002641static int __init hv_acpi_init(void)
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -07002642{
K. Y. Srinivasan2dda95f2011-07-15 13:38:56 -07002643 int ret, t;
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002644
Michael Kelley4a5f3cd2017-12-22 11:19:02 -07002645 if (!hv_is_hyperv_initialized())
Jason Wang05929692012-08-17 18:52:43 +08002646 return -ENODEV;
2647
Wei Liu7e279d72021-02-03 15:04:22 +00002648 if (hv_root_partition)
2649 return 0;
2650
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002651 init_completion(&probe_event);
2652
2653 /*
K. Y. Srinivasanefc26722015-12-14 16:01:46 -08002654 * Get ACPI resources first.
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002655 */
K. Y. Srinivasan02466042011-06-06 15:49:40 -07002656 ret = acpi_bus_register_driver(&vmbus_acpi_driver);
2657
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002658 if (ret)
2659 return ret;
2660
K. Y. Srinivasan2dda95f2011-07-15 13:38:56 -07002661 t = wait_for_completion_timeout(&probe_event, 5*HZ);
2662 if (t == 0) {
2663 ret = -ETIMEDOUT;
2664 goto cleanup;
2665 }
Branden Bonabyaf9ca6f2019-10-03 17:01:49 -04002666 hv_debug_init();
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002667
K. Y. Srinivasanefc26722015-12-14 16:01:46 -08002668 ret = vmbus_bus_init();
K. Y. Srinivasan91fd7992011-06-16 13:16:38 -07002669 if (ret)
K. Y. Srinivasan2dda95f2011-07-15 13:38:56 -07002670 goto cleanup;
2671
Vitaly Kuznetsov25172812015-08-01 16:08:07 -07002672 hv_setup_kexec_handler(hv_kexec_handler);
Vitaly Kuznetsovb4370df2015-08-01 16:08:09 -07002673 hv_setup_crash_handler(hv_crash_handler);
Vitaly Kuznetsov25172812015-08-01 16:08:07 -07002674
Dexuan Cui63ecc6d2019-09-05 23:01:16 +00002675 register_syscore_ops(&hv_synic_syscore_ops);
2676
K. Y. Srinivasan2dda95f2011-07-15 13:38:56 -07002677 return 0;
2678
2679cleanup:
2680 acpi_bus_unregister_driver(&vmbus_acpi_driver);
K. Y. Srinivasancf6a2ea2011-12-01 09:59:34 -08002681 hv_acpi_dev = NULL;
K. Y. Srinivasan91fd7992011-06-16 13:16:38 -07002682 return ret;
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -07002683}
2684
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08002685static void __exit vmbus_exit(void)
2686{
Vitaly Kuznetsove72e7ac2015-02-27 11:25:55 -08002687 int cpu;
2688
Dexuan Cui63ecc6d2019-09-05 23:01:16 +00002689 unregister_syscore_ops(&hv_synic_syscore_ops);
2690
Vitaly Kuznetsov25172812015-08-01 16:08:07 -07002691 hv_remove_kexec_handler();
Vitaly Kuznetsovb4370df2015-08-01 16:08:09 -07002692 hv_remove_crash_handler();
Vitaly Kuznetsov09a19622015-02-27 11:25:54 -08002693 vmbus_connection.conn_state = DISCONNECTED;
Michael Kelleyfd1fea62019-07-01 04:25:56 +00002694 hv_stimer_global_cleanup();
K. Y. Srinivasan2db84ef2015-04-22 21:31:32 -07002695 vmbus_disconnect();
Thomas Gleixner76d388c2014-03-05 13:42:14 +01002696 hv_remove_vmbus_irq();
Stephen Hemminger37cdd992017-02-11 23:02:19 -07002697 for_each_online_cpu(cpu) {
2698 struct hv_per_cpu_context *hv_cpu
2699 = per_cpu_ptr(hv_context.cpu_context, cpu);
2700
2701 tasklet_kill(&hv_cpu->msg_dpc);
2702 }
Branden Bonabyaf9ca6f2019-10-03 17:01:49 -04002703 hv_debug_rm_all_dir();
2704
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08002705 vmbus_free_channels();
Andrea Parri (Microsoft)8b6a8772020-04-06 02:15:06 +02002706 kfree(vmbus_connection.channels);
Stephen Hemminger37cdd992017-02-11 23:02:19 -07002707
Denis V. Lunevcc2dd402015-08-01 16:08:20 -07002708 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00002709 kmsg_dump_unregister(&hv_kmsg_dumper);
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -07002710 unregister_die_notifier(&hyperv_die_block);
Vitaly Kuznetsov096c6052015-04-22 21:31:29 -07002711 atomic_notifier_chain_unregister(&panic_notifier_list,
2712 &hyperv_panic_block);
2713 }
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00002714
2715 free_page((unsigned long)hv_panic_page);
Sunil Muthuswamy8afc06d2018-07-28 21:58:46 +00002716 unregister_sysctl_table(hv_ctl_table_hdr);
2717 hv_ctl_table_hdr = NULL;
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08002718 bus_unregister(&hv_bus);
Stephen Hemminger37cdd992017-02-11 23:02:19 -07002719
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -08002720 cpuhp_remove_state(hyperv_cpuhp_online);
Vitaly Kuznetsov06210b42015-08-01 16:08:05 -07002721 hv_synic_free();
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08002722 acpi_bus_unregister_driver(&vmbus_acpi_driver);
2723}
2724
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -07002725
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07002726MODULE_LICENSE("GPL");
Joseph Salisbury674eecb2019-04-23 03:47:27 +00002727MODULE_DESCRIPTION("Microsoft Hyper-V VMBus Driver");
Hank Janssen3e7ee492009-07-13 16:02:34 -07002728
K. Y. Srinivasan43d4e112011-10-24 11:28:12 -07002729subsys_initcall(hv_acpi_init);
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08002730module_exit(vmbus_exit);