blob: e06c6b9555cfb6800bf67910730b79f5870359ed [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
K. Y. Srinivasan302a3c02013-02-17 11:30:44 -080026#include <asm/mshyperv.h>
Dexuan Cui1f48dcf2019-09-05 23:01:20 +000027#include <linux/delay.h>
Nick Meier96c1d052015-02-28 11:39:01 -080028#include <linux/notifier.h>
29#include <linux/ptrace.h>
Jake Oshins35464482015-08-05 00:52:37 -070030#include <linux/screen_info.h>
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -070031#include <linux/kdebug.h>
Jake Oshins6d146ae2016-04-05 10:22:55 -070032#include <linux/efi.h>
Stephan Mueller4b44f2d2016-05-02 02:14:34 -040033#include <linux/random.h>
Tianyu Lanf3a99e72020-04-06 08:53:31 -070034#include <linux/kernel.h>
Dexuan Cui63ecc6d2019-09-05 23:01:16 +000035#include <linux/syscore_ops.h>
Michael Kelleyfd1fea62019-07-01 04:25:56 +000036#include <clocksource/hyperv_timer.h>
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070037#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070038
Stephen Hemmingerfc769362016-12-03 12:34:39 -080039struct vmbus_dynid {
40 struct list_head node;
41 struct hv_vmbus_device_id id;
42};
43
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -070044static struct acpi_device *hv_acpi_dev;
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -070045
K. Y. Srinivasan71a66552011-04-29 13:45:04 -070046static struct completion probe_event;
Hank Janssen3e7ee492009-07-13 16:02:34 -070047
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -080048static int hyperv_cpuhp_online;
Nick Meier96c1d052015-02-28 11:39:01 -080049
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +000050static void *hv_panic_page;
51
Tianyu Lan040026d2020-04-06 08:53:30 -070052/*
53 * Boolean to control whether to report panic messages over Hyper-V.
54 *
55 * It can be set via /proc/sys/kernel/hyperv/record_panic_msg
56 */
57static int sysctl_record_panic_msg = 1;
58
59static int hyperv_report_reg(void)
60{
61 return !sysctl_record_panic_msg || !hv_panic_page;
62}
63
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -070064static int hyperv_panic_event(struct notifier_block *nb, unsigned long val,
65 void *args)
66{
67 struct pt_regs *regs;
68
Tianyu Lan74347a92020-04-06 08:53:26 -070069 vmbus_initiate_unload(true);
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -070070
Tianyu Lan73f26e52020-04-06 08:53:28 -070071 /*
72 * Hyper-V should be notified only once about a panic. If we will be
73 * doing hyperv_report_panic_msg() later with kmsg data, don't do
74 * the notification here.
75 */
76 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE
Tianyu Lan040026d2020-04-06 08:53:30 -070077 && hyperv_report_reg()) {
Tianyu Lan74347a92020-04-06 08:53:26 -070078 regs = current_pt_regs();
Tianyu Lanf3a99e72020-04-06 08:53:31 -070079 hyperv_report_panic(regs, val, false);
Tianyu Lan74347a92020-04-06 08:53:26 -070080 }
Nick Meier96c1d052015-02-28 11:39:01 -080081 return NOTIFY_DONE;
82}
83
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -070084static int hyperv_die_event(struct notifier_block *nb, unsigned long val,
85 void *args)
86{
87 struct die_args *die = (struct die_args *)args;
88 struct pt_regs *regs = die->regs;
89
Tianyu Lan73f26e52020-04-06 08:53:28 -070090 /*
91 * Hyper-V should be notified only once about a panic. If we will be
92 * doing hyperv_report_panic_msg() later with kmsg data, don't do
93 * the notification here.
94 */
Tianyu Lan040026d2020-04-06 08:53:30 -070095 if (hyperv_report_reg())
Tianyu Lanf3a99e72020-04-06 08:53:31 -070096 hyperv_report_panic(regs, val, true);
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -070097 return NOTIFY_DONE;
98}
99
100static struct notifier_block hyperv_die_block = {
101 .notifier_call = hyperv_die_event,
102};
Nick Meier96c1d052015-02-28 11:39:01 -0800103static struct notifier_block hyperv_panic_block = {
104 .notifier_call = hyperv_panic_event,
105};
106
Jake Oshins6d146ae2016-04-05 10:22:55 -0700107static const char *fb_mmio_name = "fb_range";
108static struct resource *fb_mmio;
Stephen Hemmingere2e80842016-09-07 05:39:33 -0700109static struct resource *hyperv_mmio;
Davidlohr Bueso8aea7f82019-11-01 13:00:04 -0700110static DEFINE_MUTEX(hyperv_mmio_lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700111
K. Y. Srinivasancf6a2ea2011-12-01 09:59:34 -0800112static int vmbus_exists(void)
113{
114 if (hv_acpi_dev == NULL)
115 return -ENODEV;
116
117 return 0;
118}
119
Olaf Heringfd776ba2011-09-02 18:25:56 +0200120#define VMBUS_ALIAS_LEN ((sizeof((struct hv_vmbus_device_id *)0)->guid) * 2)
121static void print_alias_name(struct hv_device *hv_dev, char *alias_name)
122{
123 int i;
124 for (i = 0; i < VMBUS_ALIAS_LEN; i += 2)
125 sprintf(&alias_name[i], "%02x", hv_dev->dev_type.b[i/2]);
126}
127
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -0700128static u8 channel_monitor_group(const struct vmbus_channel *channel)
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700129{
130 return (u8)channel->offermsg.monitorid / 32;
131}
132
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -0700133static u8 channel_monitor_offset(const struct vmbus_channel *channel)
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700134{
135 return (u8)channel->offermsg.monitorid % 32;
136}
137
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -0700138static u32 channel_pending(const struct vmbus_channel *channel,
139 const struct hv_monitor_page *monitor_page)
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700140{
141 u8 monitor_group = channel_monitor_group(channel);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -0700142
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700143 return monitor_page->trigger_group[monitor_group].pending;
144}
145
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -0700146static u32 channel_latency(const struct vmbus_channel *channel,
147 const struct hv_monitor_page *monitor_page)
Greg Kroah-Hartman1cee2722013-09-13 11:32:57 -0700148{
149 u8 monitor_group = channel_monitor_group(channel);
150 u8 monitor_offset = channel_monitor_offset(channel);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -0700151
Greg Kroah-Hartman1cee2722013-09-13 11:32:57 -0700152 return monitor_page->latency[monitor_group][monitor_offset];
153}
154
Greg Kroah-Hartman4947c742013-09-13 11:32:58 -0700155static u32 channel_conn_id(struct vmbus_channel *channel,
156 struct hv_monitor_page *monitor_page)
157{
158 u8 monitor_group = channel_monitor_group(channel);
159 u8 monitor_offset = channel_monitor_offset(channel);
160 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",
204 hv_dev->channel->offermsg.offer.if_type.b);
205}
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",
216 hv_dev->channel->offermsg.offer.if_instance.b);
217}
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);
224 char alias_name[VMBUS_ALIAS_LEN + 1];
225
226 print_alias_name(hv_dev, alias_name);
227 return sprintf(buf, "vmbus:%s\n", alias_name);
228}
229static DEVICE_ATTR_RO(modalias);
230
Stephen Hemminger7ceb1c32018-07-28 21:58:48 +0000231#ifdef CONFIG_NUMA
232static ssize_t numa_node_show(struct device *dev,
233 struct device_attribute *attr, char *buf)
234{
235 struct hv_device *hv_dev = device_to_hv_device(dev);
236
237 if (!hv_dev->channel)
238 return -ENODEV;
239
240 return sprintf(buf, "%d\n", hv_dev->channel->numa_node);
241}
242static DEVICE_ATTR_RO(numa_node);
243#endif
244
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700245static ssize_t server_monitor_pending_show(struct device *dev,
246 struct device_attribute *dev_attr,
247 char *buf)
248{
249 struct hv_device *hv_dev = device_to_hv_device(dev);
250
251 if (!hv_dev->channel)
252 return -ENODEV;
253 return sprintf(buf, "%d\n",
254 channel_pending(hv_dev->channel,
Kimberly Brownfd8e3c32019-02-19 00:38:06 -0500255 vmbus_connection.monitor_pages[0]));
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700256}
257static DEVICE_ATTR_RO(server_monitor_pending);
258
259static ssize_t client_monitor_pending_show(struct device *dev,
260 struct device_attribute *dev_attr,
261 char *buf)
262{
263 struct hv_device *hv_dev = device_to_hv_device(dev);
264
265 if (!hv_dev->channel)
266 return -ENODEV;
267 return sprintf(buf, "%d\n",
268 channel_pending(hv_dev->channel,
269 vmbus_connection.monitor_pages[1]));
270}
271static DEVICE_ATTR_RO(client_monitor_pending);
Greg Kroah-Hartman68234c02013-09-13 11:32:53 -0700272
Greg Kroah-Hartman1cee2722013-09-13 11:32:57 -0700273static ssize_t server_monitor_latency_show(struct device *dev,
274 struct device_attribute *dev_attr,
275 char *buf)
276{
277 struct hv_device *hv_dev = device_to_hv_device(dev);
278
279 if (!hv_dev->channel)
280 return -ENODEV;
281 return sprintf(buf, "%d\n",
282 channel_latency(hv_dev->channel,
283 vmbus_connection.monitor_pages[0]));
284}
285static DEVICE_ATTR_RO(server_monitor_latency);
286
287static ssize_t client_monitor_latency_show(struct device *dev,
288 struct device_attribute *dev_attr,
289 char *buf)
290{
291 struct hv_device *hv_dev = device_to_hv_device(dev);
292
293 if (!hv_dev->channel)
294 return -ENODEV;
295 return sprintf(buf, "%d\n",
296 channel_latency(hv_dev->channel,
297 vmbus_connection.monitor_pages[1]));
298}
299static DEVICE_ATTR_RO(client_monitor_latency);
300
Greg Kroah-Hartman4947c742013-09-13 11:32:58 -0700301static ssize_t server_monitor_conn_id_show(struct device *dev,
302 struct device_attribute *dev_attr,
303 char *buf)
304{
305 struct hv_device *hv_dev = device_to_hv_device(dev);
306
307 if (!hv_dev->channel)
308 return -ENODEV;
309 return sprintf(buf, "%d\n",
310 channel_conn_id(hv_dev->channel,
311 vmbus_connection.monitor_pages[0]));
312}
313static DEVICE_ATTR_RO(server_monitor_conn_id);
314
315static ssize_t client_monitor_conn_id_show(struct device *dev,
316 struct device_attribute *dev_attr,
317 char *buf)
318{
319 struct hv_device *hv_dev = device_to_hv_device(dev);
320
321 if (!hv_dev->channel)
322 return -ENODEV;
323 return sprintf(buf, "%d\n",
324 channel_conn_id(hv_dev->channel,
325 vmbus_connection.monitor_pages[1]));
326}
327static DEVICE_ATTR_RO(client_monitor_conn_id);
328
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700329static ssize_t out_intr_mask_show(struct device *dev,
330 struct device_attribute *dev_attr, char *buf)
331{
332 struct hv_device *hv_dev = device_to_hv_device(dev);
333 struct hv_ring_buffer_debug_info outbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000334 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700335
336 if (!hv_dev->channel)
337 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000338
339 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
340 &outbound);
341 if (ret < 0)
342 return ret;
343
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700344 return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
345}
346static DEVICE_ATTR_RO(out_intr_mask);
347
348static ssize_t out_read_index_show(struct device *dev,
349 struct device_attribute *dev_attr, char *buf)
350{
351 struct hv_device *hv_dev = device_to_hv_device(dev);
352 struct hv_ring_buffer_debug_info outbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000353 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700354
355 if (!hv_dev->channel)
356 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000357
358 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
359 &outbound);
360 if (ret < 0)
361 return ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700362 return sprintf(buf, "%d\n", outbound.current_read_index);
363}
364static DEVICE_ATTR_RO(out_read_index);
365
366static ssize_t out_write_index_show(struct device *dev,
367 struct device_attribute *dev_attr,
368 char *buf)
369{
370 struct hv_device *hv_dev = device_to_hv_device(dev);
371 struct hv_ring_buffer_debug_info outbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000372 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700373
374 if (!hv_dev->channel)
375 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000376
377 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
378 &outbound);
379 if (ret < 0)
380 return ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700381 return sprintf(buf, "%d\n", outbound.current_write_index);
382}
383static DEVICE_ATTR_RO(out_write_index);
384
385static ssize_t out_read_bytes_avail_show(struct device *dev,
386 struct device_attribute *dev_attr,
387 char *buf)
388{
389 struct hv_device *hv_dev = device_to_hv_device(dev);
390 struct hv_ring_buffer_debug_info outbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000391 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700392
393 if (!hv_dev->channel)
394 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000395
396 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
397 &outbound);
398 if (ret < 0)
399 return ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700400 return sprintf(buf, "%d\n", outbound.bytes_avail_toread);
401}
402static DEVICE_ATTR_RO(out_read_bytes_avail);
403
404static ssize_t out_write_bytes_avail_show(struct device *dev,
405 struct device_attribute *dev_attr,
406 char *buf)
407{
408 struct hv_device *hv_dev = device_to_hv_device(dev);
409 struct hv_ring_buffer_debug_info outbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000410 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700411
412 if (!hv_dev->channel)
413 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000414
415 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
416 &outbound);
417 if (ret < 0)
418 return ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700419 return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
420}
421static DEVICE_ATTR_RO(out_write_bytes_avail);
422
423static ssize_t in_intr_mask_show(struct device *dev,
424 struct device_attribute *dev_attr, char *buf)
425{
426 struct hv_device *hv_dev = device_to_hv_device(dev);
427 struct hv_ring_buffer_debug_info inbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000428 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700429
430 if (!hv_dev->channel)
431 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000432
433 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
434 if (ret < 0)
435 return ret;
436
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700437 return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
438}
439static DEVICE_ATTR_RO(in_intr_mask);
440
441static ssize_t in_read_index_show(struct device *dev,
442 struct device_attribute *dev_attr, char *buf)
443{
444 struct hv_device *hv_dev = device_to_hv_device(dev);
445 struct hv_ring_buffer_debug_info inbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000446 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700447
448 if (!hv_dev->channel)
449 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000450
451 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
452 if (ret < 0)
453 return ret;
454
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700455 return sprintf(buf, "%d\n", inbound.current_read_index);
456}
457static DEVICE_ATTR_RO(in_read_index);
458
459static ssize_t in_write_index_show(struct device *dev,
460 struct device_attribute *dev_attr, char *buf)
461{
462 struct hv_device *hv_dev = device_to_hv_device(dev);
463 struct hv_ring_buffer_debug_info inbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000464 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700465
466 if (!hv_dev->channel)
467 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000468
469 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
470 if (ret < 0)
471 return ret;
472
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700473 return sprintf(buf, "%d\n", inbound.current_write_index);
474}
475static DEVICE_ATTR_RO(in_write_index);
476
477static ssize_t in_read_bytes_avail_show(struct device *dev,
478 struct device_attribute *dev_attr,
479 char *buf)
480{
481 struct hv_device *hv_dev = device_to_hv_device(dev);
482 struct hv_ring_buffer_debug_info inbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000483 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700484
485 if (!hv_dev->channel)
486 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000487
488 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
489 if (ret < 0)
490 return ret;
491
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700492 return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
493}
494static DEVICE_ATTR_RO(in_read_bytes_avail);
495
496static ssize_t in_write_bytes_avail_show(struct device *dev,
497 struct device_attribute *dev_attr,
498 char *buf)
499{
500 struct hv_device *hv_dev = device_to_hv_device(dev);
501 struct hv_ring_buffer_debug_info inbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000502 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700503
504 if (!hv_dev->channel)
505 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000506
507 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
508 if (ret < 0)
509 return ret;
510
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700511 return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
512}
513static DEVICE_ATTR_RO(in_write_bytes_avail);
514
Dexuan Cui042ab032015-08-05 00:52:43 -0700515static ssize_t channel_vp_mapping_show(struct device *dev,
516 struct device_attribute *dev_attr,
517 char *buf)
518{
519 struct hv_device *hv_dev = device_to_hv_device(dev);
520 struct vmbus_channel *channel = hv_dev->channel, *cur_sc;
521 unsigned long flags;
522 int buf_size = PAGE_SIZE, n_written, tot_written;
523 struct list_head *cur;
524
525 if (!channel)
526 return -ENODEV;
527
528 tot_written = snprintf(buf, buf_size, "%u:%u\n",
529 channel->offermsg.child_relid, channel->target_cpu);
530
531 spin_lock_irqsave(&channel->lock, flags);
532
533 list_for_each(cur, &channel->sc_list) {
534 if (tot_written >= buf_size - 1)
535 break;
536
537 cur_sc = list_entry(cur, struct vmbus_channel, sc_list);
538 n_written = scnprintf(buf + tot_written,
539 buf_size - tot_written,
540 "%u:%u\n",
541 cur_sc->offermsg.child_relid,
542 cur_sc->target_cpu);
543 tot_written += n_written;
544 }
545
546 spin_unlock_irqrestore(&channel->lock, flags);
547
548 return tot_written;
549}
550static DEVICE_ATTR_RO(channel_vp_mapping);
551
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800552static ssize_t vendor_show(struct device *dev,
553 struct device_attribute *dev_attr,
554 char *buf)
555{
556 struct hv_device *hv_dev = device_to_hv_device(dev);
557 return sprintf(buf, "0x%x\n", hv_dev->vendor_id);
558}
559static DEVICE_ATTR_RO(vendor);
560
561static ssize_t device_show(struct device *dev,
562 struct device_attribute *dev_attr,
563 char *buf)
564{
565 struct hv_device *hv_dev = device_to_hv_device(dev);
566 return sprintf(buf, "0x%x\n", hv_dev->device_id);
567}
568static DEVICE_ATTR_RO(device);
569
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000570static ssize_t driver_override_store(struct device *dev,
571 struct device_attribute *attr,
572 const char *buf, size_t count)
573{
574 struct hv_device *hv_dev = device_to_hv_device(dev);
575 char *driver_override, *old, *cp;
576
577 /* We need to keep extra room for a newline */
578 if (count >= (PAGE_SIZE - 1))
579 return -EINVAL;
580
581 driver_override = kstrndup(buf, count, GFP_KERNEL);
582 if (!driver_override)
583 return -ENOMEM;
584
585 cp = strchr(driver_override, '\n');
586 if (cp)
587 *cp = '\0';
588
589 device_lock(dev);
590 old = hv_dev->driver_override;
591 if (strlen(driver_override)) {
592 hv_dev->driver_override = driver_override;
593 } else {
594 kfree(driver_override);
595 hv_dev->driver_override = NULL;
596 }
597 device_unlock(dev);
598
599 kfree(old);
600
601 return count;
602}
603
604static ssize_t driver_override_show(struct device *dev,
605 struct device_attribute *attr, char *buf)
606{
607 struct hv_device *hv_dev = device_to_hv_device(dev);
608 ssize_t len;
609
610 device_lock(dev);
611 len = snprintf(buf, PAGE_SIZE, "%s\n", hv_dev->driver_override);
612 device_unlock(dev);
613
614 return len;
615}
616static DEVICE_ATTR_RW(driver_override);
617
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700618/* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800619static struct attribute *vmbus_dev_attrs[] = {
Greg Kroah-Hartman03f3a912013-09-13 11:32:49 -0700620 &dev_attr_id.attr,
Greg Kroah-Hartmana8fb5f32013-09-13 11:32:50 -0700621 &dev_attr_state.attr,
Greg Kroah-Hartman5ffd00e2013-09-13 11:32:51 -0700622 &dev_attr_monitor_id.attr,
Greg Kroah-Hartman68234c02013-09-13 11:32:53 -0700623 &dev_attr_class_id.attr,
Greg Kroah-Hartman7c55e1d2013-09-13 11:32:54 -0700624 &dev_attr_device_id.attr,
Greg Kroah-Hartman647fa372013-09-13 11:32:52 -0700625 &dev_attr_modalias.attr,
Stephen Hemminger7ceb1c32018-07-28 21:58:48 +0000626#ifdef CONFIG_NUMA
627 &dev_attr_numa_node.attr,
628#endif
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700629 &dev_attr_server_monitor_pending.attr,
630 &dev_attr_client_monitor_pending.attr,
Greg Kroah-Hartman1cee2722013-09-13 11:32:57 -0700631 &dev_attr_server_monitor_latency.attr,
632 &dev_attr_client_monitor_latency.attr,
Greg Kroah-Hartman4947c742013-09-13 11:32:58 -0700633 &dev_attr_server_monitor_conn_id.attr,
634 &dev_attr_client_monitor_conn_id.attr,
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700635 &dev_attr_out_intr_mask.attr,
636 &dev_attr_out_read_index.attr,
637 &dev_attr_out_write_index.attr,
638 &dev_attr_out_read_bytes_avail.attr,
639 &dev_attr_out_write_bytes_avail.attr,
640 &dev_attr_in_intr_mask.attr,
641 &dev_attr_in_read_index.attr,
642 &dev_attr_in_write_index.attr,
643 &dev_attr_in_read_bytes_avail.attr,
644 &dev_attr_in_write_bytes_avail.attr,
Dexuan Cui042ab032015-08-05 00:52:43 -0700645 &dev_attr_channel_vp_mapping.attr,
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800646 &dev_attr_vendor.attr,
647 &dev_attr_device.attr,
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000648 &dev_attr_driver_override.attr,
Greg Kroah-Hartman03f3a912013-09-13 11:32:49 -0700649 NULL,
650};
Kimberly Brown46fc1542019-03-19 00:04:01 -0400651
652/*
653 * Device-level attribute_group callback function. Returns the permission for
654 * each attribute, and returns 0 if an attribute is not visible.
655 */
656static umode_t vmbus_dev_attr_is_visible(struct kobject *kobj,
657 struct attribute *attr, int idx)
658{
659 struct device *dev = kobj_to_dev(kobj);
660 const struct hv_device *hv_dev = device_to_hv_device(dev);
661
662 /* Hide the monitor attributes if the monitor mechanism is not used. */
663 if (!hv_dev->channel->offermsg.monitor_allocated &&
664 (attr == &dev_attr_monitor_id.attr ||
665 attr == &dev_attr_server_monitor_pending.attr ||
666 attr == &dev_attr_client_monitor_pending.attr ||
667 attr == &dev_attr_server_monitor_latency.attr ||
668 attr == &dev_attr_client_monitor_latency.attr ||
669 attr == &dev_attr_server_monitor_conn_id.attr ||
670 attr == &dev_attr_client_monitor_conn_id.attr))
671 return 0;
672
673 return attr->mode;
674}
675
676static const struct attribute_group vmbus_dev_group = {
677 .attrs = vmbus_dev_attrs,
678 .is_visible = vmbus_dev_attr_is_visible
679};
680__ATTRIBUTE_GROUPS(vmbus_dev);
Greg Kroah-Hartman03f3a912013-09-13 11:32:49 -0700681
K. Y. Srinivasanadde2482011-03-15 15:03:37 -0700682/*
683 * vmbus_uevent - add uevent for our device
684 *
685 * This routine is invoked when a device is added or removed on the vmbus to
686 * generate a uevent to udev in the userspace. The udev will then look at its
687 * rule and the uevent generated here to load the appropriate driver
K. Y. Srinivasan0ddda662011-08-25 09:48:38 -0700688 *
689 * The alias string will be of the form vmbus:guid where guid is the string
690 * representation of the device guid (each byte of the guid will be
691 * represented with two hex characters.
K. Y. Srinivasanadde2482011-03-15 15:03:37 -0700692 */
693static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
694{
695 struct hv_device *dev = device_to_hv_device(device);
Olaf Heringfd776ba2011-09-02 18:25:56 +0200696 int ret;
697 char alias_name[VMBUS_ALIAS_LEN + 1];
K. Y. Srinivasanadde2482011-03-15 15:03:37 -0700698
Olaf Heringfd776ba2011-09-02 18:25:56 +0200699 print_alias_name(dev, alias_name);
K. Y. Srinivasan0ddda662011-08-25 09:48:38 -0700700 ret = add_uevent_var(env, "MODALIAS=vmbus:%s", alias_name);
701 return ret;
K. Y. Srinivasanadde2482011-03-15 15:03:37 -0700702}
703
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000704static const struct hv_vmbus_device_id *
Andy Shevchenko593db802019-01-10 16:25:32 +0200705hv_vmbus_dev_match(const struct hv_vmbus_device_id *id, const guid_t *guid)
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000706{
707 if (id == NULL)
708 return NULL; /* empty device table */
709
Andy Shevchenko593db802019-01-10 16:25:32 +0200710 for (; !guid_is_null(&id->guid); id++)
711 if (guid_equal(&id->guid, guid))
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000712 return id;
713
714 return NULL;
715}
716
717static const struct hv_vmbus_device_id *
Andy Shevchenko593db802019-01-10 16:25:32 +0200718hv_vmbus_dynid_match(struct hv_driver *drv, const guid_t *guid)
K. Y. Srinivasan3037a7b2011-09-13 10:59:37 -0700719{
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800720 const struct hv_vmbus_device_id *id = NULL;
721 struct vmbus_dynid *dynid;
722
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800723 spin_lock(&drv->dynids.lock);
724 list_for_each_entry(dynid, &drv->dynids.list, node) {
Andy Shevchenko593db802019-01-10 16:25:32 +0200725 if (guid_equal(&dynid->id.guid, guid)) {
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800726 id = &dynid->id;
727 break;
728 }
729 }
730 spin_unlock(&drv->dynids.lock);
731
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000732 return id;
733}
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800734
Andy Shevchenko593db802019-01-10 16:25:32 +0200735static const struct hv_vmbus_device_id vmbus_device_null;
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800736
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000737/*
738 * Return a matching hv_vmbus_device_id pointer.
739 * If there is no match, return NULL.
740 */
741static const struct hv_vmbus_device_id *hv_vmbus_get_id(struct hv_driver *drv,
742 struct hv_device *dev)
743{
Andy Shevchenko593db802019-01-10 16:25:32 +0200744 const guid_t *guid = &dev->dev_type;
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000745 const struct hv_vmbus_device_id *id;
K. Y. Srinivasan3037a7b2011-09-13 10:59:37 -0700746
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000747 /* When driver_override is set, only bind to the matching driver */
748 if (dev->driver_override && strcmp(dev->driver_override, drv->name))
749 return NULL;
750
751 /* Look at the dynamic ids first, before the static ones */
752 id = hv_vmbus_dynid_match(drv, guid);
753 if (!id)
754 id = hv_vmbus_dev_match(drv->id_table, guid);
755
756 /* driver_override will always match, send a dummy id */
757 if (!id && dev->driver_override)
758 id = &vmbus_device_null;
759
760 return id;
K. Y. Srinivasan3037a7b2011-09-13 10:59:37 -0700761}
762
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800763/* vmbus_add_dynid - add a new device ID to this driver and re-probe devices */
Andy Shevchenko593db802019-01-10 16:25:32 +0200764static int vmbus_add_dynid(struct hv_driver *drv, guid_t *guid)
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800765{
766 struct vmbus_dynid *dynid;
767
768 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
769 if (!dynid)
770 return -ENOMEM;
771
772 dynid->id.guid = *guid;
773
774 spin_lock(&drv->dynids.lock);
775 list_add_tail(&dynid->node, &drv->dynids.list);
776 spin_unlock(&drv->dynids.lock);
777
778 return driver_attach(&drv->driver);
779}
780
781static void vmbus_free_dynids(struct hv_driver *drv)
782{
783 struct vmbus_dynid *dynid, *n;
784
785 spin_lock(&drv->dynids.lock);
786 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
787 list_del(&dynid->node);
788 kfree(dynid);
789 }
790 spin_unlock(&drv->dynids.lock);
791}
792
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800793/*
794 * store_new_id - sysfs frontend to vmbus_add_dynid()
795 *
796 * Allow GUIDs to be added to an existing driver via sysfs.
797 */
798static ssize_t new_id_store(struct device_driver *driver, const char *buf,
799 size_t count)
800{
801 struct hv_driver *drv = drv_to_hv_drv(driver);
Andy Shevchenko593db802019-01-10 16:25:32 +0200802 guid_t guid;
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800803 ssize_t retval;
804
Andy Shevchenko593db802019-01-10 16:25:32 +0200805 retval = guid_parse(buf, &guid);
Andy Shevchenko31100102017-05-18 10:46:06 -0700806 if (retval)
807 return retval;
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800808
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000809 if (hv_vmbus_dynid_match(drv, &guid))
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800810 return -EEXIST;
811
812 retval = vmbus_add_dynid(drv, &guid);
813 if (retval)
814 return retval;
815 return count;
816}
817static DRIVER_ATTR_WO(new_id);
818
819/*
820 * store_remove_id - remove a PCI device ID from this driver
821 *
822 * Removes a dynamic pci device ID to this driver.
823 */
824static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
825 size_t count)
826{
827 struct hv_driver *drv = drv_to_hv_drv(driver);
828 struct vmbus_dynid *dynid, *n;
Andy Shevchenko593db802019-01-10 16:25:32 +0200829 guid_t guid;
Andy Shevchenko31100102017-05-18 10:46:06 -0700830 ssize_t retval;
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800831
Andy Shevchenko593db802019-01-10 16:25:32 +0200832 retval = guid_parse(buf, &guid);
Andy Shevchenko31100102017-05-18 10:46:06 -0700833 if (retval)
834 return retval;
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800835
Andy Shevchenko31100102017-05-18 10:46:06 -0700836 retval = -ENODEV;
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800837 spin_lock(&drv->dynids.lock);
838 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
839 struct hv_vmbus_device_id *id = &dynid->id;
840
Andy Shevchenko593db802019-01-10 16:25:32 +0200841 if (guid_equal(&id->guid, &guid)) {
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800842 list_del(&dynid->node);
843 kfree(dynid);
844 retval = count;
845 break;
846 }
847 }
848 spin_unlock(&drv->dynids.lock);
849
850 return retval;
851}
852static DRIVER_ATTR_WO(remove_id);
853
854static struct attribute *vmbus_drv_attrs[] = {
855 &driver_attr_new_id.attr,
856 &driver_attr_remove_id.attr,
857 NULL,
858};
859ATTRIBUTE_GROUPS(vmbus_drv);
K. Y. Srinivasan3037a7b2011-09-13 10:59:37 -0700860
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700861
862/*
863 * vmbus_match - Attempt to match the specified device to the specified driver
864 */
865static int vmbus_match(struct device *device, struct device_driver *driver)
866{
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700867 struct hv_driver *drv = drv_to_hv_drv(driver);
K. Y. Srinivasane8e27042011-06-06 15:50:04 -0700868 struct hv_device *hv_dev = device_to_hv_device(device);
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700869
Dexuan Cui8981da32016-01-27 22:29:41 -0800870 /* The hv_sock driver handles all hv_sock offers. */
871 if (is_hvsock_channel(hv_dev->channel))
872 return drv->hvsock;
873
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000874 if (hv_vmbus_get_id(drv, hv_dev))
K. Y. Srinivasan3037a7b2011-09-13 10:59:37 -0700875 return 1;
K. Y. Srinivasande632a2b2011-04-26 09:20:24 -0700876
K. Y. Srinivasan5841a822011-08-25 09:48:39 -0700877 return 0;
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700878}
879
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700880/*
881 * vmbus_probe - Add the new vmbus's child device
882 */
883static int vmbus_probe(struct device *child_device)
884{
885 int ret = 0;
886 struct hv_driver *drv =
887 drv_to_hv_drv(child_device->driver);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700888 struct hv_device *dev = device_to_hv_device(child_device);
K. Y. Srinivasan84946892011-09-13 10:59:38 -0700889 const struct hv_vmbus_device_id *dev_id;
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700890
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000891 dev_id = hv_vmbus_get_id(drv, dev);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700892 if (drv->probe) {
K. Y. Srinivasan84946892011-09-13 10:59:38 -0700893 ret = drv->probe(dev, dev_id);
K. Y. Srinivasanb14a7b32011-04-29 13:45:03 -0700894 if (ret != 0)
Hank Janssen0a466182011-03-29 13:58:47 -0700895 pr_err("probe failed for device %s (%d)\n",
896 dev_name(child_device), ret);
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700897
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700898 } else {
Hank Janssen0a466182011-03-29 13:58:47 -0700899 pr_err("probe not set for driver %s\n",
900 dev_name(child_device));
K. Y. Srinivasan6de925b2011-06-06 15:50:07 -0700901 ret = -ENODEV;
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700902 }
903 return ret;
904}
905
K. Y. Srinivasanc5dce3d2011-03-15 15:03:40 -0700906/*
907 * vmbus_remove - Remove a vmbus device
908 */
909static int vmbus_remove(struct device *child_device)
910{
K. Y. Srinivasand15a0302015-02-28 11:18:16 -0800911 struct hv_driver *drv;
K. Y. Srinivasan415b0232011-04-29 13:45:12 -0700912 struct hv_device *dev = device_to_hv_device(child_device);
K. Y. Srinivasanc5dce3d2011-03-15 15:03:40 -0700913
K. Y. Srinivasand15a0302015-02-28 11:18:16 -0800914 if (child_device->driver) {
915 drv = drv_to_hv_drv(child_device->driver);
916 if (drv->remove)
917 drv->remove(dev);
K. Y. Srinivasand15a0302015-02-28 11:18:16 -0800918 }
K. Y. Srinivasanc5dce3d2011-03-15 15:03:40 -0700919
920 return 0;
921}
922
K. Y. Srinivasaneb1bb252011-03-15 15:03:41 -0700923
924/*
925 * vmbus_shutdown - Shutdown a vmbus device
926 */
927static void vmbus_shutdown(struct device *child_device)
928{
929 struct hv_driver *drv;
K. Y. Srinivasanca6887f2011-04-29 13:45:14 -0700930 struct hv_device *dev = device_to_hv_device(child_device);
K. Y. Srinivasaneb1bb252011-03-15 15:03:41 -0700931
932
933 /* The device may not be attached yet */
934 if (!child_device->driver)
935 return;
936
937 drv = drv_to_hv_drv(child_device->driver);
938
K. Y. Srinivasanca6887f2011-04-29 13:45:14 -0700939 if (drv->shutdown)
940 drv->shutdown(dev);
K. Y. Srinivasaneb1bb252011-03-15 15:03:41 -0700941}
942
Dexuan Cui83b50f82019-09-19 21:46:12 +0000943#ifdef CONFIG_PM_SLEEP
Dexuan Cui271b2222019-09-05 23:01:17 +0000944/*
945 * vmbus_suspend - Suspend a vmbus device
946 */
947static int vmbus_suspend(struct device *child_device)
948{
949 struct hv_driver *drv;
950 struct hv_device *dev = device_to_hv_device(child_device);
951
952 /* The device may not be attached yet */
953 if (!child_device->driver)
954 return 0;
955
956 drv = drv_to_hv_drv(child_device->driver);
957 if (!drv->suspend)
958 return -EOPNOTSUPP;
959
960 return drv->suspend(dev);
961}
962
963/*
964 * vmbus_resume - Resume a vmbus device
965 */
966static int vmbus_resume(struct device *child_device)
967{
968 struct hv_driver *drv;
969 struct hv_device *dev = device_to_hv_device(child_device);
970
971 /* The device may not be attached yet */
972 if (!child_device->driver)
973 return 0;
974
975 drv = drv_to_hv_drv(child_device->driver);
976 if (!drv->resume)
977 return -EOPNOTSUPP;
978
979 return drv->resume(dev);
980}
Dexuan Cui1a06d012020-04-11 20:50:35 -0700981#else
982#define vmbus_suspend NULL
983#define vmbus_resume NULL
Dexuan Cui83b50f82019-09-19 21:46:12 +0000984#endif /* CONFIG_PM_SLEEP */
K. Y. Srinivasan086e7a52011-03-15 15:03:42 -0700985
986/*
987 * vmbus_device_release - Final callback release of the vmbus child device
988 */
989static void vmbus_device_release(struct device *device)
990{
K. Y. Srinivasane8e27042011-06-06 15:50:04 -0700991 struct hv_device *hv_dev = device_to_hv_device(device);
Dexuan Cui34c68012015-12-14 16:01:49 -0800992 struct vmbus_channel *channel = hv_dev->channel;
K. Y. Srinivasan086e7a52011-03-15 15:03:42 -0700993
Branden Bonabyaf9ca6f2019-10-03 17:01:49 -0400994 hv_debug_rm_dev_dir(hv_dev);
995
K. Y. Srinivasan54a662652017-04-30 16:21:18 -0700996 mutex_lock(&vmbus_connection.channel_mutex);
Stephen Hemminger800b9322018-09-14 09:10:15 -0700997 hv_process_channel_removal(channel);
K. Y. Srinivasan54a662652017-04-30 16:21:18 -0700998 mutex_unlock(&vmbus_connection.channel_mutex);
K. Y. Srinivasane8e27042011-06-06 15:50:04 -0700999 kfree(hv_dev);
K. Y. Srinivasan086e7a52011-03-15 15:03:42 -07001000}
1001
Dexuan Cui271b2222019-09-05 23:01:17 +00001002/*
Dexuan Cui1a06d012020-04-11 20:50:35 -07001003 * Note: we must use the "noirq" ops: see the comment before vmbus_bus_pm.
1004 *
1005 * suspend_noirq/resume_noirq are set to NULL to support Suspend-to-Idle: we
1006 * shouldn't suspend the vmbus devices upon Suspend-to-Idle, otherwise there
1007 * is no way to wake up a Generation-2 VM.
1008 *
1009 * The other 4 ops are for hibernation.
Dexuan Cui271b2222019-09-05 23:01:17 +00001010 */
Dexuan Cui1a06d012020-04-11 20:50:35 -07001011
Dexuan Cui271b2222019-09-05 23:01:17 +00001012static const struct dev_pm_ops vmbus_pm = {
Dexuan Cui1a06d012020-04-11 20:50:35 -07001013 .suspend_noirq = NULL,
1014 .resume_noirq = NULL,
1015 .freeze_noirq = vmbus_suspend,
1016 .thaw_noirq = vmbus_resume,
1017 .poweroff_noirq = vmbus_suspend,
1018 .restore_noirq = vmbus_resume,
Dexuan Cui271b2222019-09-05 23:01:17 +00001019};
1020
Bill Pemberton454f18a2009-07-27 16:47:24 -04001021/* The one and only one */
K. Y. Srinivasan9adcac52011-04-29 13:45:08 -07001022static struct bus_type hv_bus = {
1023 .name = "vmbus",
1024 .match = vmbus_match,
1025 .shutdown = vmbus_shutdown,
1026 .remove = vmbus_remove,
1027 .probe = vmbus_probe,
1028 .uevent = vmbus_uevent,
Stephen Hemmingerfc769362016-12-03 12:34:39 -08001029 .dev_groups = vmbus_dev_groups,
1030 .drv_groups = vmbus_drv_groups,
Dexuan Cui271b2222019-09-05 23:01:17 +00001031 .pm = &vmbus_pm,
Hank Janssen3e7ee492009-07-13 16:02:34 -07001032};
1033
Timo Teräsbf6506f2010-12-15 20:48:08 +02001034struct onmessage_work_context {
1035 struct work_struct work;
1036 struct hv_message msg;
1037};
1038
1039static void vmbus_onmessage_work(struct work_struct *work)
1040{
1041 struct onmessage_work_context *ctx;
1042
Vitaly Kuznetsov09a19622015-02-27 11:25:54 -08001043 /* Do not process messages if we're in DISCONNECTED state */
1044 if (vmbus_connection.conn_state == DISCONNECTED)
1045 return;
1046
Timo Teräsbf6506f2010-12-15 20:48:08 +02001047 ctx = container_of(work, struct onmessage_work_context,
1048 work);
1049 vmbus_onmessage(&ctx->msg);
1050 kfree(ctx);
1051}
1052
K. Y. Srinivasand81274a2016-02-26 15:13:21 -08001053void vmbus_on_msg_dpc(unsigned long data)
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001054{
Stephen Hemminger37cdd992017-02-11 23:02:19 -07001055 struct hv_per_cpu_context *hv_cpu = (void *)data;
1056 void *page_addr = hv_cpu->synic_message_page;
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001057 struct hv_message *msg = (struct hv_message *)page_addr +
1058 VMBUS_MESSAGE_SINT;
Dexuan Cui652594c2015-03-27 09:10:08 -07001059 struct vmbus_channel_message_header *hdr;
Stephen Hemmingere6242fa2017-03-04 18:27:16 -07001060 const struct vmbus_channel_message_table_entry *entry;
Timo Teräsbf6506f2010-12-15 20:48:08 +02001061 struct onmessage_work_context *ctx;
Vitaly Kuznetsovcd95aad2016-04-30 19:21:34 -07001062 u32 message_type = msg->header.message_type;
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001063
Vitaly Kuznetsovcd95aad2016-04-30 19:21:34 -07001064 if (message_type == HVMSG_NONE)
Vitaly Kuznetsov7be3e162016-02-26 15:13:15 -08001065 /* no msg */
1066 return;
Dexuan Cui652594c2015-03-27 09:10:08 -07001067
Vitaly Kuznetsov7be3e162016-02-26 15:13:15 -08001068 hdr = (struct vmbus_channel_message_header *)msg->u.payload;
Dexuan Cui652594c2015-03-27 09:10:08 -07001069
Vitaly Kuznetsovc9fe0f82017-10-29 12:21:00 -07001070 trace_vmbus_on_msg_dpc(hdr);
1071
Vitaly Kuznetsov7be3e162016-02-26 15:13:15 -08001072 if (hdr->msgtype >= CHANNELMSG_COUNT) {
1073 WARN_ONCE(1, "unknown msgtype=%d\n", hdr->msgtype);
1074 goto msg_handled;
1075 }
Dexuan Cui652594c2015-03-27 09:10:08 -07001076
Vitaly Kuznetsov7be3e162016-02-26 15:13:15 -08001077 entry = &channel_message_table[hdr->msgtype];
Dexuan Cuiddc9d352020-01-19 15:29:22 -08001078
1079 if (!entry->message_handler)
1080 goto msg_handled;
1081
Vitaly Kuznetsov7be3e162016-02-26 15:13:15 -08001082 if (entry->handler_type == VMHT_BLOCKING) {
1083 ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
1084 if (ctx == NULL)
1085 return;
Dexuan Cui652594c2015-03-27 09:10:08 -07001086
Vitaly Kuznetsov7be3e162016-02-26 15:13:15 -08001087 INIT_WORK(&ctx->work, vmbus_onmessage_work);
1088 memcpy(&ctx->msg, msg, sizeof(*msg));
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001089
K. Y. Srinivasan54a662652017-04-30 16:21:18 -07001090 /*
1091 * The host can generate a rescind message while we
1092 * may still be handling the original offer. We deal with
1093 * this condition by ensuring the processing is done on the
1094 * same CPU.
1095 */
1096 switch (hdr->msgtype) {
1097 case CHANNELMSG_RESCIND_CHANNELOFFER:
1098 /*
1099 * If we are handling the rescind message;
1100 * schedule the work on the global work queue.
1101 */
1102 schedule_work_on(vmbus_connection.connect_cpu,
1103 &ctx->work);
1104 break;
1105
1106 case CHANNELMSG_OFFERCHANNEL:
1107 atomic_inc(&vmbus_connection.offer_in_progress);
1108 queue_work_on(vmbus_connection.connect_cpu,
1109 vmbus_connection.work_queue,
1110 &ctx->work);
1111 break;
1112
1113 default:
1114 queue_work(vmbus_connection.work_queue, &ctx->work);
1115 }
Vitaly Kuznetsov7be3e162016-02-26 15:13:15 -08001116 } else
1117 entry->message_handler(hdr);
Dexuan Cui652594c2015-03-27 09:10:08 -07001118
1119msg_handled:
Vitaly Kuznetsovcd95aad2016-04-30 19:21:34 -07001120 vmbus_signal_eom(msg, message_type);
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001121}
1122
Dexuan Cui83b50f82019-09-19 21:46:12 +00001123#ifdef CONFIG_PM_SLEEP
Dexuan Cui1f48dcf2019-09-05 23:01:20 +00001124/*
1125 * Fake RESCIND_CHANNEL messages to clean up hv_sock channels by force for
1126 * hibernation, because hv_sock connections can not persist across hibernation.
1127 */
1128static void vmbus_force_channel_rescinded(struct vmbus_channel *channel)
1129{
1130 struct onmessage_work_context *ctx;
1131 struct vmbus_channel_rescind_offer *rescind;
1132
1133 WARN_ON(!is_hvsock_channel(channel));
1134
1135 /*
1136 * sizeof(*ctx) is small and the allocation should really not fail,
1137 * otherwise the state of the hv_sock connections ends up in limbo.
1138 */
1139 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL | __GFP_NOFAIL);
1140
1141 /*
1142 * So far, these are not really used by Linux. Just set them to the
1143 * reasonable values conforming to the definitions of the fields.
1144 */
1145 ctx->msg.header.message_type = 1;
1146 ctx->msg.header.payload_size = sizeof(*rescind);
1147
1148 /* These values are actually used by Linux. */
1149 rescind = (struct vmbus_channel_rescind_offer *)ctx->msg.u.payload;
1150 rescind->header.msgtype = CHANNELMSG_RESCIND_CHANNELOFFER;
1151 rescind->child_relid = channel->offermsg.child_relid;
1152
1153 INIT_WORK(&ctx->work, vmbus_onmessage_work);
1154
1155 queue_work_on(vmbus_connection.connect_cpu,
1156 vmbus_connection.work_queue,
1157 &ctx->work);
1158}
Dexuan Cui83b50f82019-09-19 21:46:12 +00001159#endif /* CONFIG_PM_SLEEP */
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001160
1161/*
Stephen Hemmingerb71e3282017-02-11 23:02:21 -07001162 * Direct callback for channels using other deferred processing
1163 */
1164static void vmbus_channel_isr(struct vmbus_channel *channel)
1165{
1166 void (*callback_fn)(void *);
1167
1168 callback_fn = READ_ONCE(channel->onchannel_callback);
1169 if (likely(callback_fn != NULL))
1170 (*callback_fn)(channel->channel_callback_context);
1171}
1172
1173/*
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001174 * Schedule all channels with events pending
1175 */
1176static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu)
1177{
1178 unsigned long *recv_int_page;
1179 u32 maxbits, relid;
1180
1181 if (vmbus_proto_version < VERSION_WIN8) {
1182 maxbits = MAX_NUM_CHANNELS_SUPPORTED;
1183 recv_int_page = vmbus_connection.recv_int_page;
1184 } else {
1185 /*
1186 * When the host is win8 and beyond, the event page
1187 * can be directly checked to get the id of the channel
1188 * that has the interrupt pending.
1189 */
1190 void *page_addr = hv_cpu->synic_event_page;
1191 union hv_synic_event_flags *event
1192 = (union hv_synic_event_flags *)page_addr +
1193 VMBUS_MESSAGE_SINT;
1194
1195 maxbits = HV_EVENT_FLAGS_COUNT;
1196 recv_int_page = event->flags;
1197 }
1198
1199 if (unlikely(!recv_int_page))
1200 return;
1201
1202 for_each_set_bit(relid, recv_int_page, maxbits) {
1203 struct vmbus_channel *channel;
1204
1205 if (!sync_test_and_clear_bit(relid, recv_int_page))
1206 continue;
1207
1208 /* Special case - vmbus channel protocol msg */
1209 if (relid == 0)
1210 continue;
1211
Stephen Hemminger8200f202017-03-04 18:13:57 -07001212 rcu_read_lock();
1213
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001214 /* Find channel based on relid */
Stephen Hemminger8200f202017-03-04 18:13:57 -07001215 list_for_each_entry_rcu(channel, &hv_cpu->chan_list, percpu_list) {
Stephen Hemmingerb71e3282017-02-11 23:02:21 -07001216 if (channel->offermsg.child_relid != relid)
1217 continue;
1218
K. Y. Srinivasan6f3d7912017-08-11 10:03:59 -07001219 if (channel->rescind)
1220 continue;
1221
Vitaly Kuznetsov991f8f12017-10-29 12:21:16 -07001222 trace_vmbus_chan_sched(channel);
1223
Stephen Hemminger6981fbf2017-10-29 11:33:40 -07001224 ++channel->interrupts;
1225
Stephen Hemmingerb71e3282017-02-11 23:02:21 -07001226 switch (channel->callback_mode) {
1227 case HV_CALL_ISR:
1228 vmbus_channel_isr(channel);
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001229 break;
Stephen Hemmingerb71e3282017-02-11 23:02:21 -07001230
1231 case HV_CALL_BATCHED:
1232 hv_begin_read(&channel->inbound);
1233 /* fallthrough */
1234 case HV_CALL_DIRECT:
1235 tasklet_schedule(&channel->callback_event);
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001236 }
1237 }
Stephen Hemminger8200f202017-03-04 18:13:57 -07001238
1239 rcu_read_unlock();
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001240 }
1241}
1242
Thomas Gleixner76d388c2014-03-05 13:42:14 +01001243static void vmbus_isr(void)
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001244{
Stephen Hemminger37cdd992017-02-11 23:02:19 -07001245 struct hv_per_cpu_context *hv_cpu
1246 = this_cpu_ptr(hv_context.cpu_context);
1247 void *page_addr = hv_cpu->synic_event_page;
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001248 struct hv_message *msg;
1249 union hv_synic_event_flags *event;
K. Y. Srinivasanae4636e2011-08-27 11:31:35 -07001250 bool handled = false;
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001251
Stephen Hemminger37cdd992017-02-11 23:02:19 -07001252 if (unlikely(page_addr == NULL))
Thomas Gleixner76d388c2014-03-05 13:42:14 +01001253 return;
K. Y. Srinivasan5ab05952012-12-01 06:46:55 -08001254
1255 event = (union hv_synic_event_flags *)page_addr +
1256 VMBUS_MESSAGE_SINT;
K. Y. Srinivasan7341d902011-08-31 14:35:56 -07001257 /*
1258 * Check for events before checking for messages. This is the order
1259 * in which events and messages are checked in Windows guests on
1260 * Hyper-V, and the Windows team suggested we do the same.
1261 */
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001262
K. Y. Srinivasan6552ecd72012-12-01 06:46:49 -08001263 if ((vmbus_proto_version == VERSION_WS2008) ||
1264 (vmbus_proto_version == VERSION_WIN7)) {
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001265
K. Y. Srinivasan6552ecd72012-12-01 06:46:49 -08001266 /* Since we are a child, we only need to check bit 0 */
Stephen Hemminger5c1bec62017-02-05 17:20:31 -07001267 if (sync_test_and_clear_bit(0, event->flags))
K. Y. Srinivasan6552ecd72012-12-01 06:46:49 -08001268 handled = true;
K. Y. Srinivasan6552ecd72012-12-01 06:46:49 -08001269 } else {
1270 /*
1271 * Our host is win8 or above. The signaling mechanism
1272 * has changed and we can directly look at the event page.
1273 * If bit n is set then we have an interrup on the channel
1274 * whose id is n.
1275 */
K. Y. Srinivasanae4636e2011-08-27 11:31:35 -07001276 handled = true;
K. Y. Srinivasan793be9c2011-03-15 15:03:43 -07001277 }
K. Y. Srinivasanae4636e2011-08-27 11:31:35 -07001278
K. Y. Srinivasan6552ecd72012-12-01 06:46:49 -08001279 if (handled)
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001280 vmbus_chan_sched(hv_cpu);
K. Y. Srinivasan6552ecd72012-12-01 06:46:49 -08001281
Stephen Hemminger37cdd992017-02-11 23:02:19 -07001282 page_addr = hv_cpu->synic_message_page;
K. Y. Srinivasan7341d902011-08-31 14:35:56 -07001283 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
1284
1285 /* Check if there are actual msgs to be processed */
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -08001286 if (msg->header.message_type != HVMSG_NONE) {
Michael Kelleyfd1fea62019-07-01 04:25:56 +00001287 if (msg->header.message_type == HVMSG_TIMER_EXPIRED) {
1288 hv_stimer0_isr();
1289 vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED);
1290 } else
Stephen Hemminger37cdd992017-02-11 23:02:19 -07001291 tasklet_schedule(&hv_cpu->msg_dpc);
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -08001292 }
Stephan Mueller4b44f2d2016-05-02 02:14:34 -04001293
1294 add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0);
K. Y. Srinivasan793be9c2011-03-15 15:03:43 -07001295}
1296
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001297/*
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001298 * Callback from kmsg_dump. Grab as much as possible from the end of the kmsg
1299 * buffer and call into Hyper-V to transfer the data.
1300 */
1301static void hv_kmsg_dump(struct kmsg_dumper *dumper,
1302 enum kmsg_dump_reason reason)
1303{
1304 size_t bytes_written;
1305 phys_addr_t panic_pa;
1306
1307 /* We are only interested in panics. */
1308 if ((reason != KMSG_DUMP_PANIC) || (!sysctl_record_panic_msg))
1309 return;
1310
1311 panic_pa = virt_to_phys(hv_panic_page);
1312
1313 /*
1314 * Write dump contents to the page. No need to synchronize; panic should
1315 * be single-threaded.
1316 */
Himadri Pandya53edce02019-07-30 09:49:44 +00001317 kmsg_dump_get_buffer(dumper, true, hv_panic_page, HV_HYP_PAGE_SIZE,
Sunil Muthuswamyddcaf3c2018-07-28 21:58:45 +00001318 &bytes_written);
1319 if (bytes_written)
1320 hyperv_report_panic_msg(panic_pa, bytes_written);
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001321}
1322
1323static struct kmsg_dumper hv_kmsg_dumper = {
1324 .dump = hv_kmsg_dump,
1325};
1326
1327static struct ctl_table_header *hv_ctl_table_hdr;
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001328
1329/*
1330 * sysctl option to allow the user to control whether kmsg data should be
1331 * reported to Hyper-V on panic.
1332 */
1333static struct ctl_table hv_ctl_table[] = {
1334 {
1335 .procname = "hyperv_record_panic_msg",
1336 .data = &sysctl_record_panic_msg,
1337 .maxlen = sizeof(int),
1338 .mode = 0644,
1339 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -07001340 .extra1 = SYSCTL_ZERO,
1341 .extra2 = SYSCTL_ONE
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001342 },
1343 {}
1344};
1345
1346static struct ctl_table hv_root_table[] = {
1347 {
1348 .procname = "kernel",
1349 .mode = 0555,
1350 .child = hv_ctl_table
1351 },
1352 {}
1353};
Vitaly Kuznetsove5132292015-02-27 11:25:51 -08001354
Hank Janssen3e189512010-03-04 22:11:00 +00001355/*
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001356 * vmbus_bus_init -Main vmbus driver initialization routine.
1357 *
1358 * Here, we
Lars Lindley0686e4f2010-03-11 23:51:23 +01001359 * - initialize the vmbus driver context
Lars Lindley0686e4f2010-03-11 23:51:23 +01001360 * - invoke the vmbus hv main init routine
Lars Lindley0686e4f2010-03-11 23:51:23 +01001361 * - retrieve the channel offers
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001362 */
K. Y. Srinivasanefc26722015-12-14 16:01:46 -08001363static int vmbus_bus_init(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001364{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001365 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001366
Greg Kroah-Hartman6d26e38fa22010-12-02 12:08:08 -08001367 /* Hypervisor initialization...setup hypercall page..etc */
1368 ret = hv_init();
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001369 if (ret != 0) {
Hank Janssen0a466182011-03-29 13:58:47 -07001370 pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
K. Y. Srinivasand6c1c5d2011-06-06 15:50:08 -07001371 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001372 }
1373
K. Y. Srinivasan9adcac52011-04-29 13:45:08 -07001374 ret = bus_register(&hv_bus);
K. Y. Srinivasand6c1c5d2011-06-06 15:50:08 -07001375 if (ret)
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -07001376 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001377
Thomas Gleixner76d388c2014-03-05 13:42:14 +01001378 hv_setup_vmbus_irq(vmbus_isr);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001379
Jason Wang2608fb62013-06-19 11:28:10 +08001380 ret = hv_synic_alloc();
1381 if (ret)
1382 goto err_alloc;
Michael Kelleyfd1fea62019-07-01 04:25:56 +00001383
K. Y. Srinivasan800b6902011-03-15 15:03:33 -07001384 /*
Michael Kelleyfd1fea62019-07-01 04:25:56 +00001385 * Initialize the per-cpu interrupt state and stimer state.
1386 * Then connect to the host.
K. Y. Srinivasan800b6902011-03-15 15:03:33 -07001387 */
Michael Kelley4a5f3cd2017-12-22 11:19:02 -07001388 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hyperv/vmbus:online",
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -08001389 hv_synic_init, hv_synic_cleanup);
1390 if (ret < 0)
Michael Kelleyfd1fea62019-07-01 04:25:56 +00001391 goto err_cpuhp;
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -08001392 hyperv_cpuhp_online = ret;
1393
K. Y. Srinivasan800b6902011-03-15 15:03:33 -07001394 ret = vmbus_connect();
K. Y. Srinivasan8b9987e92011-08-31 14:35:55 -07001395 if (ret)
Andrey Smetanin17efbee2015-12-14 16:01:38 -08001396 goto err_connect;
K. Y. Srinivasan800b6902011-03-15 15:03:33 -07001397
Nick Meier96c1d052015-02-28 11:39:01 -08001398 /*
1399 * Only register if the crash MSRs are available
1400 */
Denis V. Lunevcc2dd402015-08-01 16:08:20 -07001401 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001402 u64 hyperv_crash_ctl;
1403 /*
1404 * Sysctl registration is not fatal, since by default
1405 * reporting is enabled.
1406 */
1407 hv_ctl_table_hdr = register_sysctl_table(hv_root_table);
1408 if (!hv_ctl_table_hdr)
1409 pr_err("Hyper-V: sysctl table register error");
1410
1411 /*
1412 * Register for panic kmsg callback only if the right
1413 * capability is supported by the hypervisor.
1414 */
Sunil Muthuswamy9d9c9652018-07-28 21:58:47 +00001415 hv_get_crash_ctl(hyperv_crash_ctl);
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001416 if (hyperv_crash_ctl & HV_CRASH_CTL_CRASH_NOTIFY_MSG) {
Himadri Pandya53edce02019-07-30 09:49:44 +00001417 hv_panic_page = (void *)hv_alloc_hyperv_zeroed_page();
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001418 if (hv_panic_page) {
1419 ret = kmsg_dump_register(&hv_kmsg_dumper);
Tianyu Lan7f11a2c2020-04-06 08:53:27 -07001420 if (ret) {
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001421 pr_err("Hyper-V: kmsg dump register "
1422 "error 0x%x\n", ret);
Tianyu Lan7f11a2c2020-04-06 08:53:27 -07001423 hv_free_hyperv_page(
1424 (unsigned long)hv_panic_page);
1425 hv_panic_page = NULL;
1426 }
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001427 } else
1428 pr_err("Hyper-V: panic message page memory "
1429 "allocation failed");
1430 }
1431
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -07001432 register_die_notifier(&hyperv_die_block);
Nick Meier96c1d052015-02-28 11:39:01 -08001433 }
1434
Tianyu Lan74347a92020-04-06 08:53:26 -07001435 /*
1436 * Always register the panic notifier because we need to unload
1437 * the VMbus channel connection to prevent any VMbus
1438 * activity after the VM panics.
1439 */
1440 atomic_notifier_chain_register(&panic_notifier_list,
1441 &hyperv_panic_block);
1442
Greg Kroah-Hartman2d6e8822010-12-02 08:50:58 -08001443 vmbus_request_offers();
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +00001444
K. Y. Srinivasand6c1c5d2011-06-06 15:50:08 -07001445 return 0;
K. Y. Srinivasan8b9987e92011-08-31 14:35:55 -07001446
Andrey Smetanin17efbee2015-12-14 16:01:38 -08001447err_connect:
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -08001448 cpuhp_remove_state(hyperv_cpuhp_online);
Michael Kelleyfd1fea62019-07-01 04:25:56 +00001449err_cpuhp:
Jason Wang2608fb62013-06-19 11:28:10 +08001450 hv_synic_free();
Michael Kelley4df4cb9e92019-11-13 01:11:49 +00001451err_alloc:
Thomas Gleixner76d388c2014-03-05 13:42:14 +01001452 hv_remove_vmbus_irq();
K. Y. Srinivasan8b9987e92011-08-31 14:35:55 -07001453
K. Y. Srinivasan8b9987e92011-08-31 14:35:55 -07001454 bus_unregister(&hv_bus);
Sunil Muthuswamy8afc06d2018-07-28 21:58:46 +00001455 unregister_sysctl_table(hv_ctl_table_hdr);
1456 hv_ctl_table_hdr = NULL;
K. Y. Srinivasan8b9987e92011-08-31 14:35:55 -07001457 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001458}
1459
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001460/**
Jake Oshins35464482015-08-05 00:52:37 -07001461 * __vmbus_child_driver_register() - Register a vmbus's driver
1462 * @hv_driver: Pointer to driver structure you want to register
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001463 * @owner: owner module of the drv
1464 * @mod_name: module name string
Hank Janssen3e189512010-03-04 22:11:00 +00001465 *
1466 * Registers the given driver with Linux through the 'driver_register()' call
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001467 * and sets up the hyper-v vmbus handling for this driver.
Hank Janssen3e189512010-03-04 22:11:00 +00001468 * It will return the state of the 'driver_register()' call.
1469 *
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001470 */
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001471int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001472{
Bill Pemberton5d48a1c2009-07-27 16:47:36 -04001473 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001474
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001475 pr_info("registering driver %s\n", hv_driver->name);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001476
K. Y. Srinivasancf6a2ea2011-12-01 09:59:34 -08001477 ret = vmbus_exists();
1478 if (ret < 0)
1479 return ret;
1480
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001481 hv_driver->driver.name = hv_driver->name;
1482 hv_driver->driver.owner = owner;
1483 hv_driver->driver.mod_name = mod_name;
1484 hv_driver->driver.bus = &hv_bus;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001485
Stephen Hemmingerfc769362016-12-03 12:34:39 -08001486 spin_lock_init(&hv_driver->dynids.lock);
1487 INIT_LIST_HEAD(&hv_driver->dynids.list);
1488
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001489 ret = driver_register(&hv_driver->driver);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001490
Bill Pemberton5d48a1c2009-07-27 16:47:36 -04001491 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001492}
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001493EXPORT_SYMBOL_GPL(__vmbus_driver_register);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001494
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001495/**
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001496 * vmbus_driver_unregister() - Unregister a vmbus's driver
Jake Oshins35464482015-08-05 00:52:37 -07001497 * @hv_driver: Pointer to driver structure you want to
1498 * un-register
Hank Janssen3e189512010-03-04 22:11:00 +00001499 *
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001500 * Un-register the given driver that was previous registered with a call to
1501 * vmbus_driver_register()
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001502 */
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001503void vmbus_driver_unregister(struct hv_driver *hv_driver)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001504{
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001505 pr_info("unregistering driver %s\n", hv_driver->name);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001506
Stephen Hemmingerfc769362016-12-03 12:34:39 -08001507 if (!vmbus_exists()) {
K. Y. Srinivasan8f257a12011-12-27 13:49:37 -08001508 driver_unregister(&hv_driver->driver);
Stephen Hemmingerfc769362016-12-03 12:34:39 -08001509 vmbus_free_dynids(hv_driver);
1510 }
Hank Janssen3e7ee492009-07-13 16:02:34 -07001511}
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001512EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001513
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001514
1515/*
1516 * Called when last reference to channel is gone.
1517 */
1518static void vmbus_chan_release(struct kobject *kobj)
1519{
1520 struct vmbus_channel *channel
1521 = container_of(kobj, struct vmbus_channel, kobj);
1522
1523 kfree_rcu(channel, rcu);
1524}
1525
1526struct vmbus_chan_attribute {
1527 struct attribute attr;
Kimberly Brown14948e32019-03-14 16:05:15 -04001528 ssize_t (*show)(struct vmbus_channel *chan, char *buf);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001529 ssize_t (*store)(struct vmbus_channel *chan,
1530 const char *buf, size_t count);
1531};
1532#define VMBUS_CHAN_ATTR(_name, _mode, _show, _store) \
1533 struct vmbus_chan_attribute chan_attr_##_name \
1534 = __ATTR(_name, _mode, _show, _store)
1535#define VMBUS_CHAN_ATTR_RW(_name) \
1536 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RW(_name)
1537#define VMBUS_CHAN_ATTR_RO(_name) \
1538 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RO(_name)
1539#define VMBUS_CHAN_ATTR_WO(_name) \
1540 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_WO(_name)
1541
1542static ssize_t vmbus_chan_attr_show(struct kobject *kobj,
1543 struct attribute *attr, char *buf)
1544{
1545 const struct vmbus_chan_attribute *attribute
1546 = container_of(attr, struct vmbus_chan_attribute, attr);
Kimberly Brown14948e32019-03-14 16:05:15 -04001547 struct vmbus_channel *chan
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001548 = container_of(kobj, struct vmbus_channel, kobj);
1549
1550 if (!attribute->show)
1551 return -EIO;
1552
1553 return attribute->show(chan, buf);
1554}
1555
1556static const struct sysfs_ops vmbus_chan_sysfs_ops = {
1557 .show = vmbus_chan_attr_show,
1558};
1559
Kimberly Brown14948e32019-03-14 16:05:15 -04001560static ssize_t out_mask_show(struct vmbus_channel *channel, char *buf)
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001561{
Kimberly Brown14948e32019-03-14 16:05:15 -04001562 struct hv_ring_buffer_info *rbi = &channel->outbound;
1563 ssize_t ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001564
Kimberly Brown14948e32019-03-14 16:05:15 -04001565 mutex_lock(&rbi->ring_buffer_mutex);
1566 if (!rbi->ring_buffer) {
1567 mutex_unlock(&rbi->ring_buffer_mutex);
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001568 return -EINVAL;
Kimberly Brown14948e32019-03-14 16:05:15 -04001569 }
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001570
Kimberly Brown14948e32019-03-14 16:05:15 -04001571 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1572 mutex_unlock(&rbi->ring_buffer_mutex);
1573 return ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001574}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001575static VMBUS_CHAN_ATTR_RO(out_mask);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001576
Kimberly Brown14948e32019-03-14 16:05:15 -04001577static ssize_t in_mask_show(struct vmbus_channel *channel, char *buf)
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001578{
Kimberly Brown14948e32019-03-14 16:05:15 -04001579 struct hv_ring_buffer_info *rbi = &channel->inbound;
1580 ssize_t ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001581
Kimberly Brown14948e32019-03-14 16:05:15 -04001582 mutex_lock(&rbi->ring_buffer_mutex);
1583 if (!rbi->ring_buffer) {
1584 mutex_unlock(&rbi->ring_buffer_mutex);
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001585 return -EINVAL;
Kimberly Brown14948e32019-03-14 16:05:15 -04001586 }
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001587
Kimberly Brown14948e32019-03-14 16:05:15 -04001588 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1589 mutex_unlock(&rbi->ring_buffer_mutex);
1590 return ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001591}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001592static VMBUS_CHAN_ATTR_RO(in_mask);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001593
Kimberly Brown14948e32019-03-14 16:05:15 -04001594static ssize_t read_avail_show(struct vmbus_channel *channel, char *buf)
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001595{
Kimberly Brown14948e32019-03-14 16:05:15 -04001596 struct hv_ring_buffer_info *rbi = &channel->inbound;
1597 ssize_t ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001598
Kimberly Brown14948e32019-03-14 16:05:15 -04001599 mutex_lock(&rbi->ring_buffer_mutex);
1600 if (!rbi->ring_buffer) {
1601 mutex_unlock(&rbi->ring_buffer_mutex);
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001602 return -EINVAL;
Kimberly Brown14948e32019-03-14 16:05:15 -04001603 }
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001604
Kimberly Brown14948e32019-03-14 16:05:15 -04001605 ret = sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi));
1606 mutex_unlock(&rbi->ring_buffer_mutex);
1607 return ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001608}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001609static VMBUS_CHAN_ATTR_RO(read_avail);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001610
Kimberly Brown14948e32019-03-14 16:05:15 -04001611static ssize_t write_avail_show(struct vmbus_channel *channel, char *buf)
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001612{
Kimberly Brown14948e32019-03-14 16:05:15 -04001613 struct hv_ring_buffer_info *rbi = &channel->outbound;
1614 ssize_t ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001615
Kimberly Brown14948e32019-03-14 16:05:15 -04001616 mutex_lock(&rbi->ring_buffer_mutex);
1617 if (!rbi->ring_buffer) {
1618 mutex_unlock(&rbi->ring_buffer_mutex);
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001619 return -EINVAL;
Kimberly Brown14948e32019-03-14 16:05:15 -04001620 }
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001621
Kimberly Brown14948e32019-03-14 16:05:15 -04001622 ret = sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi));
1623 mutex_unlock(&rbi->ring_buffer_mutex);
1624 return ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001625}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001626static VMBUS_CHAN_ATTR_RO(write_avail);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001627
Kimberly Brown14948e32019-03-14 16:05:15 -04001628static ssize_t show_target_cpu(struct vmbus_channel *channel, char *buf)
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001629{
1630 return sprintf(buf, "%u\n", channel->target_cpu);
1631}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001632static VMBUS_CHAN_ATTR(cpu, S_IRUGO, show_target_cpu, NULL);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001633
Kimberly Brown14948e32019-03-14 16:05:15 -04001634static ssize_t channel_pending_show(struct vmbus_channel *channel,
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001635 char *buf)
1636{
1637 return sprintf(buf, "%d\n",
1638 channel_pending(channel,
1639 vmbus_connection.monitor_pages[1]));
1640}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001641static VMBUS_CHAN_ATTR(pending, S_IRUGO, channel_pending_show, NULL);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001642
Kimberly Brown14948e32019-03-14 16:05:15 -04001643static ssize_t channel_latency_show(struct vmbus_channel *channel,
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001644 char *buf)
1645{
1646 return sprintf(buf, "%d\n",
1647 channel_latency(channel,
1648 vmbus_connection.monitor_pages[1]));
1649}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001650static VMBUS_CHAN_ATTR(latency, S_IRUGO, channel_latency_show, NULL);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001651
Kimberly Brown14948e32019-03-14 16:05:15 -04001652static ssize_t channel_interrupts_show(struct vmbus_channel *channel, char *buf)
Stephen Hemminger6981fbf2017-10-29 11:33:40 -07001653{
1654 return sprintf(buf, "%llu\n", channel->interrupts);
1655}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001656static VMBUS_CHAN_ATTR(interrupts, S_IRUGO, channel_interrupts_show, NULL);
Stephen Hemminger6981fbf2017-10-29 11:33:40 -07001657
Kimberly Brown14948e32019-03-14 16:05:15 -04001658static ssize_t channel_events_show(struct vmbus_channel *channel, char *buf)
Stephen Hemminger6981fbf2017-10-29 11:33:40 -07001659{
1660 return sprintf(buf, "%llu\n", channel->sig_events);
1661}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001662static VMBUS_CHAN_ATTR(events, S_IRUGO, channel_events_show, NULL);
Stephen Hemminger6981fbf2017-10-29 11:33:40 -07001663
Kimberly Brown14948e32019-03-14 16:05:15 -04001664static ssize_t channel_intr_in_full_show(struct vmbus_channel *channel,
Kimberly Brown396ae572019-02-04 02:13:09 -05001665 char *buf)
1666{
1667 return sprintf(buf, "%llu\n",
1668 (unsigned long long)channel->intr_in_full);
1669}
1670static VMBUS_CHAN_ATTR(intr_in_full, 0444, channel_intr_in_full_show, NULL);
1671
Kimberly Brown14948e32019-03-14 16:05:15 -04001672static ssize_t channel_intr_out_empty_show(struct vmbus_channel *channel,
Kimberly Brown396ae572019-02-04 02:13:09 -05001673 char *buf)
1674{
1675 return sprintf(buf, "%llu\n",
1676 (unsigned long long)channel->intr_out_empty);
1677}
1678static VMBUS_CHAN_ATTR(intr_out_empty, 0444, channel_intr_out_empty_show, NULL);
1679
Kimberly Brown14948e32019-03-14 16:05:15 -04001680static ssize_t channel_out_full_first_show(struct vmbus_channel *channel,
Kimberly Brown396ae572019-02-04 02:13:09 -05001681 char *buf)
1682{
1683 return sprintf(buf, "%llu\n",
1684 (unsigned long long)channel->out_full_first);
1685}
1686static VMBUS_CHAN_ATTR(out_full_first, 0444, channel_out_full_first_show, NULL);
1687
Kimberly Brown14948e32019-03-14 16:05:15 -04001688static ssize_t channel_out_full_total_show(struct vmbus_channel *channel,
Kimberly Brown396ae572019-02-04 02:13:09 -05001689 char *buf)
1690{
1691 return sprintf(buf, "%llu\n",
1692 (unsigned long long)channel->out_full_total);
1693}
1694static VMBUS_CHAN_ATTR(out_full_total, 0444, channel_out_full_total_show, NULL);
1695
Kimberly Brown14948e32019-03-14 16:05:15 -04001696static ssize_t subchannel_monitor_id_show(struct vmbus_channel *channel,
Stephen Hemmingerf0fa2972018-01-09 10:29:06 -08001697 char *buf)
1698{
1699 return sprintf(buf, "%u\n", channel->offermsg.monitorid);
1700}
1701static VMBUS_CHAN_ATTR(monitor_id, S_IRUGO, subchannel_monitor_id_show, NULL);
1702
Kimberly Brown14948e32019-03-14 16:05:15 -04001703static ssize_t subchannel_id_show(struct vmbus_channel *channel,
Stephen Hemmingerf0fa2972018-01-09 10:29:06 -08001704 char *buf)
1705{
1706 return sprintf(buf, "%u\n",
1707 channel->offermsg.offer.sub_channel_index);
1708}
1709static VMBUS_CHAN_ATTR_RO(subchannel_id);
1710
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001711static struct attribute *vmbus_chan_attrs[] = {
1712 &chan_attr_out_mask.attr,
1713 &chan_attr_in_mask.attr,
1714 &chan_attr_read_avail.attr,
1715 &chan_attr_write_avail.attr,
1716 &chan_attr_cpu.attr,
1717 &chan_attr_pending.attr,
1718 &chan_attr_latency.attr,
Stephen Hemminger6981fbf2017-10-29 11:33:40 -07001719 &chan_attr_interrupts.attr,
1720 &chan_attr_events.attr,
Kimberly Brown396ae572019-02-04 02:13:09 -05001721 &chan_attr_intr_in_full.attr,
1722 &chan_attr_intr_out_empty.attr,
1723 &chan_attr_out_full_first.attr,
1724 &chan_attr_out_full_total.attr,
Stephen Hemmingerf0fa2972018-01-09 10:29:06 -08001725 &chan_attr_monitor_id.attr,
1726 &chan_attr_subchannel_id.attr,
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001727 NULL
1728};
1729
Kimberly Brown46fc1542019-03-19 00:04:01 -04001730/*
1731 * Channel-level attribute_group callback function. Returns the permission for
1732 * each attribute, and returns 0 if an attribute is not visible.
1733 */
1734static umode_t vmbus_chan_attr_is_visible(struct kobject *kobj,
1735 struct attribute *attr, int idx)
1736{
1737 const struct vmbus_channel *channel =
1738 container_of(kobj, struct vmbus_channel, kobj);
1739
1740 /* Hide the monitor attributes if the monitor mechanism is not used. */
1741 if (!channel->offermsg.monitor_allocated &&
1742 (attr == &chan_attr_pending.attr ||
1743 attr == &chan_attr_latency.attr ||
1744 attr == &chan_attr_monitor_id.attr))
1745 return 0;
1746
1747 return attr->mode;
1748}
1749
1750static struct attribute_group vmbus_chan_group = {
1751 .attrs = vmbus_chan_attrs,
1752 .is_visible = vmbus_chan_attr_is_visible
1753};
1754
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001755static struct kobj_type vmbus_chan_ktype = {
1756 .sysfs_ops = &vmbus_chan_sysfs_ops,
1757 .release = vmbus_chan_release,
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001758};
1759
1760/*
1761 * vmbus_add_channel_kobj - setup a sub-directory under device/channels
1762 */
1763int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
1764{
Kimberly Brown46fc1542019-03-19 00:04:01 -04001765 const struct device *device = &dev->device;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001766 struct kobject *kobj = &channel->kobj;
1767 u32 relid = channel->offermsg.child_relid;
1768 int ret;
1769
1770 kobj->kset = dev->channels_kset;
1771 ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
1772 "%u", relid);
1773 if (ret)
1774 return ret;
1775
Kimberly Brown46fc1542019-03-19 00:04:01 -04001776 ret = sysfs_create_group(kobj, &vmbus_chan_group);
1777
1778 if (ret) {
1779 /*
1780 * The calling functions' error handling paths will cleanup the
1781 * empty channel directory.
1782 */
1783 dev_err(device, "Unable to set up channel sysfs files\n");
1784 return ret;
1785 }
1786
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001787 kobject_uevent(kobj, KOBJ_ADD);
1788
1789 return 0;
1790}
1791
Hank Janssen3e189512010-03-04 22:11:00 +00001792/*
Kimberly Brown46fc1542019-03-19 00:04:01 -04001793 * vmbus_remove_channel_attr_group - remove the channel's attribute group
1794 */
1795void vmbus_remove_channel_attr_group(struct vmbus_channel *channel)
1796{
1797 sysfs_remove_group(&channel->kobj, &vmbus_chan_group);
1798}
1799
1800/*
K. Y. Srinivasanf2c73012011-09-08 07:24:12 -07001801 * vmbus_device_create - Creates and registers a new child device
Hank Janssen3e189512010-03-04 22:11:00 +00001802 * on the vmbus.
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001803 */
Andy Shevchenko593db802019-01-10 16:25:32 +02001804struct hv_device *vmbus_device_create(const guid_t *type,
1805 const guid_t *instance,
stephen hemminger1b9d48f2014-06-03 08:38:15 -07001806 struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001807{
Nicolas Palix3d3b5512009-07-28 17:32:53 +02001808 struct hv_device *child_device_obj;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001809
K. Y. Srinivasan6bad88da2011-03-07 13:35:48 -08001810 child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
1811 if (!child_device_obj) {
Hank Janssen0a466182011-03-29 13:58:47 -07001812 pr_err("Unable to allocate device object for child device\n");
Hank Janssen3e7ee492009-07-13 16:02:34 -07001813 return NULL;
1814 }
1815
Greg Kroah-Hartmancae5b842010-10-21 09:05:27 -07001816 child_device_obj->channel = channel;
Andy Shevchenko593db802019-01-10 16:25:32 +02001817 guid_copy(&child_device_obj->dev_type, type);
1818 guid_copy(&child_device_obj->dev_instance, instance);
K. Y. Srinivasan7047f172015-12-25 20:00:30 -08001819 child_device_obj->vendor_id = 0x1414; /* MSFT vendor ID */
Hank Janssen3e7ee492009-07-13 16:02:34 -07001820
Hank Janssen3e7ee492009-07-13 16:02:34 -07001821 return child_device_obj;
1822}
1823
Hank Janssen3e189512010-03-04 22:11:00 +00001824/*
K. Y. Srinivasan227942812011-09-08 07:24:13 -07001825 * vmbus_device_register - Register the child device
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001826 */
K. Y. Srinivasan227942812011-09-08 07:24:13 -07001827int vmbus_device_register(struct hv_device *child_device_obj)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001828{
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001829 struct kobject *kobj = &child_device_obj->device.kobj;
1830 int ret;
K. Y. Srinivasan6bad88da2011-03-07 13:35:48 -08001831
Stephen Hemmingerf6b2db02016-11-01 00:01:59 -07001832 dev_set_name(&child_device_obj->device, "%pUl",
Vitaly Kuznetsovb2948092016-09-16 09:01:17 -07001833 child_device_obj->channel->offermsg.offer.if_instance.b);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001834
K. Y. Srinivasan0bce28b2011-08-27 11:31:39 -07001835 child_device_obj->device.bus = &hv_bus;
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -07001836 child_device_obj->device.parent = &hv_acpi_dev->dev;
K. Y. Srinivasan6bad88da2011-03-07 13:35:48 -08001837 child_device_obj->device.release = vmbus_device_release;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001838
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001839 /*
1840 * Register with the LDM. This will kick off the driver/device
1841 * binding...which will eventually call vmbus_match() and vmbus_probe()
1842 */
K. Y. Srinivasan6bad88da2011-03-07 13:35:48 -08001843 ret = device_register(&child_device_obj->device);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001844 if (ret) {
Hank Janssen0a466182011-03-29 13:58:47 -07001845 pr_err("Unable to register child device\n");
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001846 return ret;
1847 }
Hank Janssen3e7ee492009-07-13 16:02:34 -07001848
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001849 child_device_obj->channels_kset = kset_create_and_add("channels",
1850 NULL, kobj);
1851 if (!child_device_obj->channels_kset) {
1852 ret = -ENOMEM;
1853 goto err_dev_unregister;
1854 }
1855
1856 ret = vmbus_add_channel_kobj(child_device_obj,
1857 child_device_obj->channel);
1858 if (ret) {
1859 pr_err("Unable to register primary channeln");
1860 goto err_kset_unregister;
1861 }
Branden Bonabyaf9ca6f2019-10-03 17:01:49 -04001862 hv_debug_add_dev_dir(child_device_obj);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001863
1864 return 0;
1865
1866err_kset_unregister:
1867 kset_unregister(child_device_obj->channels_kset);
1868
1869err_dev_unregister:
1870 device_unregister(&child_device_obj->device);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001871 return ret;
1872}
1873
Hank Janssen3e189512010-03-04 22:11:00 +00001874/*
K. Y. Srinivasan696453b2011-09-08 07:24:14 -07001875 * vmbus_device_unregister - Remove the specified child device
Hank Janssen3e189512010-03-04 22:11:00 +00001876 * from the vmbus.
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001877 */
K. Y. Srinivasan696453b2011-09-08 07:24:14 -07001878void vmbus_device_unregister(struct hv_device *device_obj)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001879{
Fernando Soto84672362013-06-14 23:13:35 +00001880 pr_debug("child device %s unregistered\n",
1881 dev_name(&device_obj->device));
1882
Dexuan Cui869b5562017-11-14 06:53:32 -07001883 kset_unregister(device_obj->channels_kset);
1884
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001885 /*
1886 * Kick off the process of unregistering the device.
1887 * This will call vmbus_remove() and eventually vmbus_device_release()
1888 */
K. Y. Srinivasan6bad88da2011-03-07 13:35:48 -08001889 device_unregister(&device_obj->device);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001890}
1891
Hank Janssen3e7ee492009-07-13 16:02:34 -07001892
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001893/*
Jake Oshins7f163a62015-08-05 00:52:36 -07001894 * VMBUS is an acpi enumerated device. Get the information we
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08001895 * need from DSDT.
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001896 */
Jake Oshins7f163a62015-08-05 00:52:36 -07001897#define VTPM_BASE_ADDRESS 0xfed40000
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08001898static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001899{
Jake Oshins7f163a62015-08-05 00:52:36 -07001900 resource_size_t start = 0;
1901 resource_size_t end = 0;
1902 struct resource *new_res;
1903 struct resource **old_res = &hyperv_mmio;
1904 struct resource **prev_res = NULL;
1905
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08001906 switch (res->type) {
Jake Oshins7f163a62015-08-05 00:52:36 -07001907
1908 /*
1909 * "Address" descriptors are for bus windows. Ignore
1910 * "memory" descriptors, which are for registers on
1911 * devices.
1912 */
1913 case ACPI_RESOURCE_TYPE_ADDRESS32:
1914 start = res->data.address32.address.minimum;
1915 end = res->data.address32.address.maximum;
Gerd Hoffmann4eb923f2014-02-24 14:17:08 +01001916 break;
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001917
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08001918 case ACPI_RESOURCE_TYPE_ADDRESS64:
Jake Oshins7f163a62015-08-05 00:52:36 -07001919 start = res->data.address64.address.minimum;
1920 end = res->data.address64.address.maximum;
Gerd Hoffmann4eb923f2014-02-24 14:17:08 +01001921 break;
Jake Oshins7f163a62015-08-05 00:52:36 -07001922
1923 default:
1924 /* Unused resource type */
1925 return AE_OK;
1926
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001927 }
Jake Oshins7f163a62015-08-05 00:52:36 -07001928 /*
1929 * Ignore ranges that are below 1MB, as they're not
1930 * necessary or useful here.
1931 */
1932 if (end < 0x100000)
1933 return AE_OK;
1934
1935 new_res = kzalloc(sizeof(*new_res), GFP_ATOMIC);
1936 if (!new_res)
1937 return AE_NO_MEMORY;
1938
1939 /* If this range overlaps the virtual TPM, truncate it. */
1940 if (end > VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS)
1941 end = VTPM_BASE_ADDRESS;
1942
1943 new_res->name = "hyperv mmio";
1944 new_res->flags = IORESOURCE_MEM;
1945 new_res->start = start;
1946 new_res->end = end;
1947
Jake Oshins40f26f32015-12-14 16:01:52 -08001948 /*
Jake Oshins40f26f32015-12-14 16:01:52 -08001949 * If two ranges are adjacent, merge them.
1950 */
Jake Oshins7f163a62015-08-05 00:52:36 -07001951 do {
1952 if (!*old_res) {
1953 *old_res = new_res;
1954 break;
1955 }
1956
Jake Oshins40f26f32015-12-14 16:01:52 -08001957 if (((*old_res)->end + 1) == new_res->start) {
1958 (*old_res)->end = new_res->end;
1959 kfree(new_res);
1960 break;
1961 }
1962
1963 if ((*old_res)->start == new_res->end + 1) {
1964 (*old_res)->start = new_res->start;
1965 kfree(new_res);
1966 break;
1967 }
1968
Jake Oshins23a06832016-04-05 10:22:53 -07001969 if ((*old_res)->start > new_res->end) {
Jake Oshins7f163a62015-08-05 00:52:36 -07001970 new_res->sibling = *old_res;
1971 if (prev_res)
1972 (*prev_res)->sibling = new_res;
1973 *old_res = new_res;
1974 break;
1975 }
1976
1977 prev_res = old_res;
1978 old_res = &(*old_res)->sibling;
1979
1980 } while (1);
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001981
1982 return AE_OK;
1983}
1984
Jake Oshins7f163a62015-08-05 00:52:36 -07001985static int vmbus_acpi_remove(struct acpi_device *device)
1986{
1987 struct resource *cur_res;
1988 struct resource *next_res;
1989
1990 if (hyperv_mmio) {
Jake Oshins6d146ae2016-04-05 10:22:55 -07001991 if (fb_mmio) {
1992 __release_region(hyperv_mmio, fb_mmio->start,
1993 resource_size(fb_mmio));
1994 fb_mmio = NULL;
1995 }
1996
Jake Oshins7f163a62015-08-05 00:52:36 -07001997 for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) {
1998 next_res = cur_res->sibling;
1999 kfree(cur_res);
2000 }
2001 }
2002
2003 return 0;
2004}
2005
Jake Oshins6d146ae2016-04-05 10:22:55 -07002006static void vmbus_reserve_fb(void)
2007{
2008 int size;
2009 /*
2010 * Make a claim for the frame buffer in the resource tree under the
2011 * first node, which will be the one below 4GB. The length seems to
2012 * be underreported, particularly in a Generation 1 VM. So start out
2013 * reserving a larger area and make it smaller until it succeeds.
2014 */
2015
2016 if (screen_info.lfb_base) {
2017 if (efi_enabled(EFI_BOOT))
2018 size = max_t(__u32, screen_info.lfb_size, 0x800000);
2019 else
2020 size = max_t(__u32, screen_info.lfb_size, 0x4000000);
2021
2022 for (; !fb_mmio && (size >= 0x100000); size >>= 1) {
2023 fb_mmio = __request_region(hyperv_mmio,
2024 screen_info.lfb_base, size,
2025 fb_mmio_name, 0);
2026 }
2027 }
2028}
2029
Jake Oshins35464482015-08-05 00:52:37 -07002030/**
2031 * vmbus_allocate_mmio() - Pick a memory-mapped I/O range.
2032 * @new: If successful, supplied a pointer to the
2033 * allocated MMIO space.
2034 * @device_obj: Identifies the caller
2035 * @min: Minimum guest physical address of the
2036 * allocation
2037 * @max: Maximum guest physical address
2038 * @size: Size of the range to be allocated
2039 * @align: Alignment of the range to be allocated
2040 * @fb_overlap_ok: Whether this allocation can be allowed
2041 * to overlap the video frame buffer.
2042 *
2043 * This function walks the resources granted to VMBus by the
2044 * _CRS object in the ACPI namespace underneath the parent
2045 * "bridge" whether that's a root PCI bus in the Generation 1
2046 * case or a Module Device in the Generation 2 case. It then
2047 * attempts to allocate from the global MMIO pool in a way that
2048 * matches the constraints supplied in these parameters and by
2049 * that _CRS.
2050 *
2051 * Return: 0 on success, -errno on failure
2052 */
2053int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
2054 resource_size_t min, resource_size_t max,
2055 resource_size_t size, resource_size_t align,
2056 bool fb_overlap_ok)
2057{
Jake Oshinsbe000f92016-04-05 10:22:54 -07002058 struct resource *iter, *shadow;
Jake Oshinsea37a6b2016-04-05 10:22:56 -07002059 resource_size_t range_min, range_max, start;
Jake Oshins35464482015-08-05 00:52:37 -07002060 const char *dev_n = dev_name(&device_obj->device);
Jake Oshinsea37a6b2016-04-05 10:22:56 -07002061 int retval;
Jake Oshinse16dad62016-04-05 10:22:50 -07002062
2063 retval = -ENXIO;
Davidlohr Bueso8aea7f82019-11-01 13:00:04 -07002064 mutex_lock(&hyperv_mmio_lock);
Jake Oshins35464482015-08-05 00:52:37 -07002065
Jake Oshinsea37a6b2016-04-05 10:22:56 -07002066 /*
2067 * If overlaps with frame buffers are allowed, then first attempt to
2068 * make the allocation from within the reserved region. Because it
2069 * is already reserved, no shadow allocation is necessary.
2070 */
2071 if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) &&
2072 !(max < fb_mmio->start)) {
2073
2074 range_min = fb_mmio->start;
2075 range_max = fb_mmio->end;
2076 start = (range_min + align - 1) & ~(align - 1);
2077 for (; start + size - 1 <= range_max; start += align) {
2078 *new = request_mem_region_exclusive(start, size, dev_n);
2079 if (*new) {
2080 retval = 0;
2081 goto exit;
2082 }
2083 }
2084 }
2085
Jake Oshins35464482015-08-05 00:52:37 -07002086 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
2087 if ((iter->start >= max) || (iter->end <= min))
2088 continue;
2089
2090 range_min = iter->start;
2091 range_max = iter->end;
Jake Oshinsea37a6b2016-04-05 10:22:56 -07002092 start = (range_min + align - 1) & ~(align - 1);
2093 for (; start + size - 1 <= range_max; start += align) {
2094 shadow = __request_region(iter, start, size, NULL,
2095 IORESOURCE_BUSY);
2096 if (!shadow)
2097 continue;
Jake Oshins35464482015-08-05 00:52:37 -07002098
Jake Oshinsea37a6b2016-04-05 10:22:56 -07002099 *new = request_mem_region_exclusive(start, size, dev_n);
2100 if (*new) {
2101 shadow->name = (char *)*new;
2102 retval = 0;
2103 goto exit;
Jake Oshins35464482015-08-05 00:52:37 -07002104 }
2105
Jake Oshinsea37a6b2016-04-05 10:22:56 -07002106 __release_region(iter, start, size);
Jake Oshins35464482015-08-05 00:52:37 -07002107 }
2108 }
2109
Jake Oshinse16dad62016-04-05 10:22:50 -07002110exit:
Davidlohr Bueso8aea7f82019-11-01 13:00:04 -07002111 mutex_unlock(&hyperv_mmio_lock);
Jake Oshinse16dad62016-04-05 10:22:50 -07002112 return retval;
Jake Oshins35464482015-08-05 00:52:37 -07002113}
2114EXPORT_SYMBOL_GPL(vmbus_allocate_mmio);
2115
Jake Oshins619848b2015-12-14 16:01:39 -08002116/**
Jake Oshins97fb77dc2016-04-05 10:22:51 -07002117 * vmbus_free_mmio() - Free a memory-mapped I/O range.
2118 * @start: Base address of region to release.
2119 * @size: Size of the range to be allocated
2120 *
2121 * This function releases anything requested by
2122 * vmbus_mmio_allocate().
2123 */
2124void vmbus_free_mmio(resource_size_t start, resource_size_t size)
2125{
Jake Oshinsbe000f92016-04-05 10:22:54 -07002126 struct resource *iter;
2127
Davidlohr Bueso8aea7f82019-11-01 13:00:04 -07002128 mutex_lock(&hyperv_mmio_lock);
Jake Oshinsbe000f92016-04-05 10:22:54 -07002129 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
2130 if ((iter->start >= start + size) || (iter->end <= start))
2131 continue;
2132
2133 __release_region(iter, start, size);
2134 }
Jake Oshins97fb77dc2016-04-05 10:22:51 -07002135 release_mem_region(start, size);
Davidlohr Bueso8aea7f82019-11-01 13:00:04 -07002136 mutex_unlock(&hyperv_mmio_lock);
Jake Oshins97fb77dc2016-04-05 10:22:51 -07002137
2138}
2139EXPORT_SYMBOL_GPL(vmbus_free_mmio);
2140
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002141static int vmbus_acpi_add(struct acpi_device *device)
2142{
2143 acpi_status result;
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002144 int ret_val = -ENODEV;
Jake Oshins7f163a62015-08-05 00:52:36 -07002145 struct acpi_device *ancestor;
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002146
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -07002147 hv_acpi_dev = device;
2148
K. Y. Srinivasan0a4425b2011-08-27 11:31:38 -07002149 result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002150 vmbus_walk_resources, NULL);
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002151
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002152 if (ACPI_FAILURE(result))
2153 goto acpi_walk_err;
2154 /*
Jake Oshins7f163a62015-08-05 00:52:36 -07002155 * Some ancestor of the vmbus acpi device (Gen1 or Gen2
2156 * firmware) is the VMOD that has the mmio ranges. Get that.
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002157 */
Jake Oshins7f163a62015-08-05 00:52:36 -07002158 for (ancestor = device->parent; ancestor; ancestor = ancestor->parent) {
2159 result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS,
2160 vmbus_walk_resources, NULL);
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002161
2162 if (ACPI_FAILURE(result))
Jake Oshins7f163a62015-08-05 00:52:36 -07002163 continue;
Jake Oshins6d146ae2016-04-05 10:22:55 -07002164 if (hyperv_mmio) {
2165 vmbus_reserve_fb();
Jake Oshins7f163a62015-08-05 00:52:36 -07002166 break;
Jake Oshins6d146ae2016-04-05 10:22:55 -07002167 }
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002168 }
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002169 ret_val = 0;
2170
2171acpi_walk_err:
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002172 complete(&probe_event);
Jake Oshins7f163a62015-08-05 00:52:36 -07002173 if (ret_val)
2174 vmbus_acpi_remove(device);
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002175 return ret_val;
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002176}
2177
Dexuan Cui83b50f82019-09-19 21:46:12 +00002178#ifdef CONFIG_PM_SLEEP
Dexuan Cuif53335e2019-09-05 23:01:19 +00002179static int vmbus_bus_suspend(struct device *dev)
2180{
Dexuan Cuib307b382019-09-05 23:01:21 +00002181 struct vmbus_channel *channel, *sc;
2182 unsigned long flags;
Dexuan Cui1f48dcf2019-09-05 23:01:20 +00002183
2184 while (atomic_read(&vmbus_connection.offer_in_progress) != 0) {
2185 /*
2186 * We wait here until the completion of any channel
2187 * offers that are currently in progress.
2188 */
2189 msleep(1);
2190 }
2191
2192 mutex_lock(&vmbus_connection.channel_mutex);
2193 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
2194 if (!is_hvsock_channel(channel))
2195 continue;
2196
2197 vmbus_force_channel_rescinded(channel);
2198 }
2199 mutex_unlock(&vmbus_connection.channel_mutex);
2200
Dexuan Cuib307b382019-09-05 23:01:21 +00002201 /*
2202 * Wait until all the sub-channels and hv_sock channels have been
2203 * cleaned up. Sub-channels should be destroyed upon suspend, otherwise
2204 * they would conflict with the new sub-channels that will be created
2205 * in the resume path. hv_sock channels should also be destroyed, but
2206 * a hv_sock channel of an established hv_sock connection can not be
2207 * really destroyed since it may still be referenced by the userspace
2208 * application, so we just force the hv_sock channel to be rescinded
2209 * by vmbus_force_channel_rescinded(), and the userspace application
2210 * will thoroughly destroy the channel after hibernation.
2211 *
2212 * Note: the counter nr_chan_close_on_suspend may never go above 0 if
2213 * the VM has no sub-channel and hv_sock channel, e.g. a 1-vCPU VM.
2214 */
2215 if (atomic_read(&vmbus_connection.nr_chan_close_on_suspend) > 0)
2216 wait_for_completion(&vmbus_connection.ready_for_suspend_event);
2217
Dexuan Cuid8bd2d42019-09-05 23:01:22 +00002218 WARN_ON(atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) != 0);
2219
Dexuan Cuib307b382019-09-05 23:01:21 +00002220 mutex_lock(&vmbus_connection.channel_mutex);
2221
2222 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
Dexuan Cuid8bd2d42019-09-05 23:01:22 +00002223 /*
2224 * Invalidate the field. Upon resume, vmbus_onoffer() will fix
2225 * up the field, and the other fields (if necessary).
2226 */
2227 channel->offermsg.child_relid = INVALID_RELID;
2228
Dexuan Cuib307b382019-09-05 23:01:21 +00002229 if (is_hvsock_channel(channel)) {
2230 if (!channel->rescind) {
2231 pr_err("hv_sock channel not rescinded!\n");
2232 WARN_ON_ONCE(1);
2233 }
2234 continue;
2235 }
2236
2237 spin_lock_irqsave(&channel->lock, flags);
2238 list_for_each_entry(sc, &channel->sc_list, sc_list) {
2239 pr_err("Sub-channel not deleted!\n");
2240 WARN_ON_ONCE(1);
2241 }
2242 spin_unlock_irqrestore(&channel->lock, flags);
Dexuan Cuid8bd2d42019-09-05 23:01:22 +00002243
2244 atomic_inc(&vmbus_connection.nr_chan_fixup_on_resume);
Dexuan Cuib307b382019-09-05 23:01:21 +00002245 }
2246
2247 mutex_unlock(&vmbus_connection.channel_mutex);
2248
Dexuan Cuif53335e2019-09-05 23:01:19 +00002249 vmbus_initiate_unload(false);
2250
Dexuan Cuid8bd2d42019-09-05 23:01:22 +00002251 /* Reset the event for the next resume. */
2252 reinit_completion(&vmbus_connection.ready_for_resume_event);
2253
Dexuan Cuif53335e2019-09-05 23:01:19 +00002254 return 0;
2255}
2256
2257static int vmbus_bus_resume(struct device *dev)
2258{
2259 struct vmbus_channel_msginfo *msginfo;
2260 size_t msgsize;
2261 int ret;
2262
2263 /*
2264 * We only use the 'vmbus_proto_version', which was in use before
2265 * hibernation, to re-negotiate with the host.
2266 */
Andrea Parribedc61a2019-10-15 13:46:44 +02002267 if (!vmbus_proto_version) {
Dexuan Cuif53335e2019-09-05 23:01:19 +00002268 pr_err("Invalid proto version = 0x%x\n", vmbus_proto_version);
2269 return -EINVAL;
2270 }
2271
2272 msgsize = sizeof(*msginfo) +
2273 sizeof(struct vmbus_channel_initiate_contact);
2274
2275 msginfo = kzalloc(msgsize, GFP_KERNEL);
2276
2277 if (msginfo == NULL)
2278 return -ENOMEM;
2279
2280 ret = vmbus_negotiate_version(msginfo, vmbus_proto_version);
2281
2282 kfree(msginfo);
2283
2284 if (ret != 0)
2285 return ret;
2286
Dexuan Cuid8bd2d42019-09-05 23:01:22 +00002287 WARN_ON(atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) == 0);
2288
Dexuan Cuif53335e2019-09-05 23:01:19 +00002289 vmbus_request_offers();
2290
Dexuan Cuid8bd2d42019-09-05 23:01:22 +00002291 wait_for_completion(&vmbus_connection.ready_for_resume_event);
2292
Dexuan Cuib307b382019-09-05 23:01:21 +00002293 /* Reset the event for the next suspend. */
2294 reinit_completion(&vmbus_connection.ready_for_suspend_event);
2295
Dexuan Cuif53335e2019-09-05 23:01:19 +00002296 return 0;
2297}
Dexuan Cui1a06d012020-04-11 20:50:35 -07002298#else
2299#define vmbus_bus_suspend NULL
2300#define vmbus_bus_resume NULL
Dexuan Cui83b50f82019-09-19 21:46:12 +00002301#endif /* CONFIG_PM_SLEEP */
Dexuan Cuif53335e2019-09-05 23:01:19 +00002302
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002303static const struct acpi_device_id vmbus_acpi_device_ids[] = {
2304 {"VMBUS", 0},
K. Y. Srinivasan9d7b18d2011-06-06 15:49:42 -07002305 {"VMBus", 0},
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002306 {"", 0},
2307};
2308MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
2309
Dexuan Cuif53335e2019-09-05 23:01:19 +00002310/*
Dexuan Cui1a06d012020-04-11 20:50:35 -07002311 * Note: we must use the "no_irq" ops, otherwise hibernation can not work with
2312 * PCI device assignment, because "pci_dev_pm_ops" uses the "noirq" ops: in
2313 * the resume path, the pci "noirq" restore op runs before "non-noirq" op (see
Dexuan Cuif53335e2019-09-05 23:01:19 +00002314 * resume_target_kernel() -> dpm_resume_start(), and hibernation_restore() ->
2315 * dpm_resume_end()). This means vmbus_bus_resume() and the pci-hyperv's
Dexuan Cui1a06d012020-04-11 20:50:35 -07002316 * resume callback must also run via the "noirq" ops.
2317 *
2318 * Set suspend_noirq/resume_noirq to NULL for Suspend-to-Idle: see the comment
2319 * earlier in this file before vmbus_pm.
Dexuan Cuif53335e2019-09-05 23:01:19 +00002320 */
Dexuan Cui1a06d012020-04-11 20:50:35 -07002321
Dexuan Cuif53335e2019-09-05 23:01:19 +00002322static const struct dev_pm_ops vmbus_bus_pm = {
Dexuan Cui1a06d012020-04-11 20:50:35 -07002323 .suspend_noirq = NULL,
2324 .resume_noirq = NULL,
2325 .freeze_noirq = vmbus_bus_suspend,
2326 .thaw_noirq = vmbus_bus_resume,
2327 .poweroff_noirq = vmbus_bus_suspend,
2328 .restore_noirq = vmbus_bus_resume
Dexuan Cuif53335e2019-09-05 23:01:19 +00002329};
2330
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002331static struct acpi_driver vmbus_acpi_driver = {
2332 .name = "vmbus",
2333 .ids = vmbus_acpi_device_ids,
2334 .ops = {
2335 .add = vmbus_acpi_add,
Vitaly Kuznetsove4ecb412015-04-22 21:31:28 -07002336 .remove = vmbus_acpi_remove,
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002337 },
Dexuan Cuif53335e2019-09-05 23:01:19 +00002338 .drv.pm = &vmbus_bus_pm,
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002339};
2340
Vitaly Kuznetsov25172812015-08-01 16:08:07 -07002341static void hv_kexec_handler(void)
2342{
Michael Kelleyfd1fea62019-07-01 04:25:56 +00002343 hv_stimer_global_cleanup();
Vitaly Kuznetsov75ff3a82016-02-26 15:13:16 -08002344 vmbus_initiate_unload(false);
Vitaly Kuznetsov523b9402016-12-07 14:53:12 -08002345 /* Make sure conn_state is set as hv_synic_cleanup checks for it */
2346 mb();
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -08002347 cpuhp_remove_state(hyperv_cpuhp_online);
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -07002348 hyperv_cleanup();
Vitaly Kuznetsov25172812015-08-01 16:08:07 -07002349};
2350
Vitaly Kuznetsovb4370df2015-08-01 16:08:09 -07002351static void hv_crash_handler(struct pt_regs *regs)
2352{
Michael Kelleyfd1fea62019-07-01 04:25:56 +00002353 int cpu;
2354
Vitaly Kuznetsov75ff3a82016-02-26 15:13:16 -08002355 vmbus_initiate_unload(true);
Vitaly Kuznetsovb4370df2015-08-01 16:08:09 -07002356 /*
2357 * In crash handler we can't schedule synic cleanup for all CPUs,
2358 * doing the cleanup for current CPU only. This should be sufficient
2359 * for kdump.
2360 */
Michael Kelleyfd1fea62019-07-01 04:25:56 +00002361 cpu = smp_processor_id();
2362 hv_stimer_cleanup(cpu);
Michael Kelley7a1323b2019-11-14 06:32:01 +00002363 hv_synic_disable_regs(cpu);
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -07002364 hyperv_cleanup();
Vitaly Kuznetsovb4370df2015-08-01 16:08:09 -07002365};
2366
Dexuan Cui63ecc6d2019-09-05 23:01:16 +00002367static int hv_synic_suspend(void)
2368{
2369 /*
Michael Kelley4df4cb9e92019-11-13 01:11:49 +00002370 * When we reach here, all the non-boot CPUs have been offlined.
2371 * If we're in a legacy configuration where stimer Direct Mode is
2372 * not enabled, the stimers on the non-boot CPUs have been unbound
2373 * in hv_synic_cleanup() -> hv_stimer_legacy_cleanup() ->
Dexuan Cui63ecc6d2019-09-05 23:01:16 +00002374 * hv_stimer_cleanup() -> clockevents_unbind_device().
2375 *
Michael Kelley4df4cb9e92019-11-13 01:11:49 +00002376 * hv_synic_suspend() only runs on CPU0 with interrupts disabled.
2377 * Here we do not call hv_stimer_legacy_cleanup() on CPU0 because:
2378 * 1) it's unnecessary as interrupts remain disabled between
2379 * syscore_suspend() and syscore_resume(): see create_image() and
2380 * resume_target_kernel()
Dexuan Cui63ecc6d2019-09-05 23:01:16 +00002381 * 2) the stimer on CPU0 is automatically disabled later by
2382 * syscore_suspend() -> timekeeping_suspend() -> tick_suspend() -> ...
Michael Kelley4df4cb9e92019-11-13 01:11:49 +00002383 * -> clockevents_shutdown() -> ... -> hv_ce_shutdown()
2384 * 3) a warning would be triggered if we call
2385 * clockevents_unbind_device(), which may sleep, in an
2386 * interrupts-disabled context.
Dexuan Cui63ecc6d2019-09-05 23:01:16 +00002387 */
2388
2389 hv_synic_disable_regs(0);
2390
2391 return 0;
2392}
2393
2394static void hv_synic_resume(void)
2395{
2396 hv_synic_enable_regs(0);
2397
2398 /*
2399 * Note: we don't need to call hv_stimer_init(0), because the timer
2400 * on CPU0 is not unbound in hv_synic_suspend(), and the timer is
2401 * automatically re-enabled in timekeeping_resume().
2402 */
2403}
2404
2405/* The callbacks run only on CPU0, with irqs_disabled. */
2406static struct syscore_ops hv_synic_syscore_ops = {
2407 .suspend = hv_synic_suspend,
2408 .resume = hv_synic_resume,
2409};
2410
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -07002411static int __init hv_acpi_init(void)
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -07002412{
K. Y. Srinivasan2dda95f2011-07-15 13:38:56 -07002413 int ret, t;
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002414
Michael Kelley4a5f3cd2017-12-22 11:19:02 -07002415 if (!hv_is_hyperv_initialized())
Jason Wang05929692012-08-17 18:52:43 +08002416 return -ENODEV;
2417
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002418 init_completion(&probe_event);
2419
2420 /*
K. Y. Srinivasanefc26722015-12-14 16:01:46 -08002421 * Get ACPI resources first.
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002422 */
K. Y. Srinivasan02466042011-06-06 15:49:40 -07002423 ret = acpi_bus_register_driver(&vmbus_acpi_driver);
2424
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002425 if (ret)
2426 return ret;
2427
K. Y. Srinivasan2dda95f2011-07-15 13:38:56 -07002428 t = wait_for_completion_timeout(&probe_event, 5*HZ);
2429 if (t == 0) {
2430 ret = -ETIMEDOUT;
2431 goto cleanup;
2432 }
Branden Bonabyaf9ca6f2019-10-03 17:01:49 -04002433 hv_debug_init();
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002434
K. Y. Srinivasanefc26722015-12-14 16:01:46 -08002435 ret = vmbus_bus_init();
K. Y. Srinivasan91fd7992011-06-16 13:16:38 -07002436 if (ret)
K. Y. Srinivasan2dda95f2011-07-15 13:38:56 -07002437 goto cleanup;
2438
Vitaly Kuznetsov25172812015-08-01 16:08:07 -07002439 hv_setup_kexec_handler(hv_kexec_handler);
Vitaly Kuznetsovb4370df2015-08-01 16:08:09 -07002440 hv_setup_crash_handler(hv_crash_handler);
Vitaly Kuznetsov25172812015-08-01 16:08:07 -07002441
Dexuan Cui63ecc6d2019-09-05 23:01:16 +00002442 register_syscore_ops(&hv_synic_syscore_ops);
2443
K. Y. Srinivasan2dda95f2011-07-15 13:38:56 -07002444 return 0;
2445
2446cleanup:
2447 acpi_bus_unregister_driver(&vmbus_acpi_driver);
K. Y. Srinivasancf6a2ea2011-12-01 09:59:34 -08002448 hv_acpi_dev = NULL;
K. Y. Srinivasan91fd7992011-06-16 13:16:38 -07002449 return ret;
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -07002450}
2451
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08002452static void __exit vmbus_exit(void)
2453{
Vitaly Kuznetsove72e7ac2015-02-27 11:25:55 -08002454 int cpu;
2455
Dexuan Cui63ecc6d2019-09-05 23:01:16 +00002456 unregister_syscore_ops(&hv_synic_syscore_ops);
2457
Vitaly Kuznetsov25172812015-08-01 16:08:07 -07002458 hv_remove_kexec_handler();
Vitaly Kuznetsovb4370df2015-08-01 16:08:09 -07002459 hv_remove_crash_handler();
Vitaly Kuznetsov09a19622015-02-27 11:25:54 -08002460 vmbus_connection.conn_state = DISCONNECTED;
Michael Kelleyfd1fea62019-07-01 04:25:56 +00002461 hv_stimer_global_cleanup();
K. Y. Srinivasan2db84ef2015-04-22 21:31:32 -07002462 vmbus_disconnect();
Thomas Gleixner76d388c2014-03-05 13:42:14 +01002463 hv_remove_vmbus_irq();
Stephen Hemminger37cdd992017-02-11 23:02:19 -07002464 for_each_online_cpu(cpu) {
2465 struct hv_per_cpu_context *hv_cpu
2466 = per_cpu_ptr(hv_context.cpu_context, cpu);
2467
2468 tasklet_kill(&hv_cpu->msg_dpc);
2469 }
Branden Bonabyaf9ca6f2019-10-03 17:01:49 -04002470 hv_debug_rm_all_dir();
2471
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08002472 vmbus_free_channels();
Stephen Hemminger37cdd992017-02-11 23:02:19 -07002473
Denis V. Lunevcc2dd402015-08-01 16:08:20 -07002474 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00002475 kmsg_dump_unregister(&hv_kmsg_dumper);
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -07002476 unregister_die_notifier(&hyperv_die_block);
Vitaly Kuznetsov096c6052015-04-22 21:31:29 -07002477 atomic_notifier_chain_unregister(&panic_notifier_list,
2478 &hyperv_panic_block);
2479 }
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00002480
2481 free_page((unsigned long)hv_panic_page);
Sunil Muthuswamy8afc06d2018-07-28 21:58:46 +00002482 unregister_sysctl_table(hv_ctl_table_hdr);
2483 hv_ctl_table_hdr = NULL;
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08002484 bus_unregister(&hv_bus);
Stephen Hemminger37cdd992017-02-11 23:02:19 -07002485
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -08002486 cpuhp_remove_state(hyperv_cpuhp_online);
Vitaly Kuznetsov06210b42015-08-01 16:08:05 -07002487 hv_synic_free();
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08002488 acpi_bus_unregister_driver(&vmbus_acpi_driver);
2489}
2490
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -07002491
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07002492MODULE_LICENSE("GPL");
Joseph Salisbury674eecb2019-04-23 03:47:27 +00002493MODULE_DESCRIPTION("Microsoft Hyper-V VMBus Driver");
Hank Janssen3e7ee492009-07-13 16:02:34 -07002494
K. Y. Srinivasan43d4e112011-10-24 11:28:12 -07002495subsys_initcall(hv_acpi_init);
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08002496module_exit(vmbus_exit);