blob: 32ec951d334f61bfd193451eb56a35ab152e79ce [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>
Dexuan Cui63ecc6d2019-09-05 23:01:16 +000034#include <linux/syscore_ops.h>
Michael Kelleyfd1fea62019-07-01 04:25:56 +000035#include <clocksource/hyperv_timer.h>
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070036#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070037
Stephen Hemmingerfc769362016-12-03 12:34:39 -080038struct vmbus_dynid {
39 struct list_head node;
40 struct hv_vmbus_device_id id;
41};
42
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -070043static struct acpi_device *hv_acpi_dev;
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -070044
K. Y. Srinivasan71a66552011-04-29 13:45:04 -070045static struct completion probe_event;
Hank Janssen3e7ee492009-07-13 16:02:34 -070046
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -080047static int hyperv_cpuhp_online;
Nick Meier96c1d052015-02-28 11:39:01 -080048
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +000049static void *hv_panic_page;
50
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -070051static int hyperv_panic_event(struct notifier_block *nb, unsigned long val,
52 void *args)
53{
54 struct pt_regs *regs;
55
56 regs = current_pt_regs();
57
K. Y. Srinivasan7ed43252017-10-29 11:33:41 -070058 hyperv_report_panic(regs, val);
Nick Meier96c1d052015-02-28 11:39:01 -080059 return NOTIFY_DONE;
60}
61
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -070062static int hyperv_die_event(struct notifier_block *nb, unsigned long val,
63 void *args)
64{
65 struct die_args *die = (struct die_args *)args;
66 struct pt_regs *regs = die->regs;
67
K. Y. Srinivasan7ed43252017-10-29 11:33:41 -070068 hyperv_report_panic(regs, val);
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -070069 return NOTIFY_DONE;
70}
71
72static struct notifier_block hyperv_die_block = {
73 .notifier_call = hyperv_die_event,
74};
Nick Meier96c1d052015-02-28 11:39:01 -080075static struct notifier_block hyperv_panic_block = {
76 .notifier_call = hyperv_panic_event,
77};
78
Jake Oshins6d146ae2016-04-05 10:22:55 -070079static const char *fb_mmio_name = "fb_range";
80static struct resource *fb_mmio;
Stephen Hemmingere2e80842016-09-07 05:39:33 -070081static struct resource *hyperv_mmio;
82static DEFINE_SEMAPHORE(hyperv_mmio_lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -070083
K. Y. Srinivasancf6a2ea2011-12-01 09:59:34 -080084static int vmbus_exists(void)
85{
86 if (hv_acpi_dev == NULL)
87 return -ENODEV;
88
89 return 0;
90}
91
Olaf Heringfd776ba2011-09-02 18:25:56 +020092#define VMBUS_ALIAS_LEN ((sizeof((struct hv_vmbus_device_id *)0)->guid) * 2)
93static void print_alias_name(struct hv_device *hv_dev, char *alias_name)
94{
95 int i;
96 for (i = 0; i < VMBUS_ALIAS_LEN; i += 2)
97 sprintf(&alias_name[i], "%02x", hv_dev->dev_type.b[i/2]);
98}
99
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -0700100static u8 channel_monitor_group(const struct vmbus_channel *channel)
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700101{
102 return (u8)channel->offermsg.monitorid / 32;
103}
104
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -0700105static u8 channel_monitor_offset(const struct vmbus_channel *channel)
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700106{
107 return (u8)channel->offermsg.monitorid % 32;
108}
109
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -0700110static u32 channel_pending(const struct vmbus_channel *channel,
111 const struct hv_monitor_page *monitor_page)
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700112{
113 u8 monitor_group = channel_monitor_group(channel);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -0700114
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700115 return monitor_page->trigger_group[monitor_group].pending;
116}
117
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -0700118static u32 channel_latency(const struct vmbus_channel *channel,
119 const struct hv_monitor_page *monitor_page)
Greg Kroah-Hartman1cee2722013-09-13 11:32:57 -0700120{
121 u8 monitor_group = channel_monitor_group(channel);
122 u8 monitor_offset = channel_monitor_offset(channel);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -0700123
Greg Kroah-Hartman1cee2722013-09-13 11:32:57 -0700124 return monitor_page->latency[monitor_group][monitor_offset];
125}
126
Greg Kroah-Hartman4947c742013-09-13 11:32:58 -0700127static u32 channel_conn_id(struct vmbus_channel *channel,
128 struct hv_monitor_page *monitor_page)
129{
130 u8 monitor_group = channel_monitor_group(channel);
131 u8 monitor_offset = channel_monitor_offset(channel);
132 return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
133}
134
Greg Kroah-Hartman03f3a912013-09-13 11:32:49 -0700135static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
136 char *buf)
137{
138 struct hv_device *hv_dev = device_to_hv_device(dev);
139
140 if (!hv_dev->channel)
141 return -ENODEV;
142 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
143}
144static DEVICE_ATTR_RO(id);
145
Greg Kroah-Hartmana8fb5f32013-09-13 11:32:50 -0700146static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
147 char *buf)
148{
149 struct hv_device *hv_dev = device_to_hv_device(dev);
150
151 if (!hv_dev->channel)
152 return -ENODEV;
153 return sprintf(buf, "%d\n", hv_dev->channel->state);
154}
155static DEVICE_ATTR_RO(state);
156
Greg Kroah-Hartman5ffd00e2013-09-13 11:32:51 -0700157static ssize_t monitor_id_show(struct device *dev,
158 struct device_attribute *dev_attr, char *buf)
159{
160 struct hv_device *hv_dev = device_to_hv_device(dev);
161
162 if (!hv_dev->channel)
163 return -ENODEV;
164 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
165}
166static DEVICE_ATTR_RO(monitor_id);
167
Greg Kroah-Hartman68234c02013-09-13 11:32:53 -0700168static ssize_t class_id_show(struct device *dev,
169 struct device_attribute *dev_attr, char *buf)
170{
171 struct hv_device *hv_dev = device_to_hv_device(dev);
172
173 if (!hv_dev->channel)
174 return -ENODEV;
175 return sprintf(buf, "{%pUl}\n",
176 hv_dev->channel->offermsg.offer.if_type.b);
177}
178static DEVICE_ATTR_RO(class_id);
179
Greg Kroah-Hartman7c55e1d2013-09-13 11:32:54 -0700180static ssize_t device_id_show(struct device *dev,
181 struct device_attribute *dev_attr, char *buf)
182{
183 struct hv_device *hv_dev = device_to_hv_device(dev);
184
185 if (!hv_dev->channel)
186 return -ENODEV;
187 return sprintf(buf, "{%pUl}\n",
188 hv_dev->channel->offermsg.offer.if_instance.b);
189}
190static DEVICE_ATTR_RO(device_id);
191
Greg Kroah-Hartman647fa372013-09-13 11:32:52 -0700192static ssize_t modalias_show(struct device *dev,
193 struct device_attribute *dev_attr, char *buf)
194{
195 struct hv_device *hv_dev = device_to_hv_device(dev);
196 char alias_name[VMBUS_ALIAS_LEN + 1];
197
198 print_alias_name(hv_dev, alias_name);
199 return sprintf(buf, "vmbus:%s\n", alias_name);
200}
201static DEVICE_ATTR_RO(modalias);
202
Stephen Hemminger7ceb1c32018-07-28 21:58:48 +0000203#ifdef CONFIG_NUMA
204static ssize_t numa_node_show(struct device *dev,
205 struct device_attribute *attr, char *buf)
206{
207 struct hv_device *hv_dev = device_to_hv_device(dev);
208
209 if (!hv_dev->channel)
210 return -ENODEV;
211
212 return sprintf(buf, "%d\n", hv_dev->channel->numa_node);
213}
214static DEVICE_ATTR_RO(numa_node);
215#endif
216
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700217static ssize_t server_monitor_pending_show(struct device *dev,
218 struct device_attribute *dev_attr,
219 char *buf)
220{
221 struct hv_device *hv_dev = device_to_hv_device(dev);
222
223 if (!hv_dev->channel)
224 return -ENODEV;
225 return sprintf(buf, "%d\n",
226 channel_pending(hv_dev->channel,
Kimberly Brownfd8e3c32019-02-19 00:38:06 -0500227 vmbus_connection.monitor_pages[0]));
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700228}
229static DEVICE_ATTR_RO(server_monitor_pending);
230
231static ssize_t client_monitor_pending_show(struct device *dev,
232 struct device_attribute *dev_attr,
233 char *buf)
234{
235 struct hv_device *hv_dev = device_to_hv_device(dev);
236
237 if (!hv_dev->channel)
238 return -ENODEV;
239 return sprintf(buf, "%d\n",
240 channel_pending(hv_dev->channel,
241 vmbus_connection.monitor_pages[1]));
242}
243static DEVICE_ATTR_RO(client_monitor_pending);
Greg Kroah-Hartman68234c02013-09-13 11:32:53 -0700244
Greg Kroah-Hartman1cee2722013-09-13 11:32:57 -0700245static ssize_t server_monitor_latency_show(struct device *dev,
246 struct device_attribute *dev_attr,
247 char *buf)
248{
249 struct hv_device *hv_dev = device_to_hv_device(dev);
250
251 if (!hv_dev->channel)
252 return -ENODEV;
253 return sprintf(buf, "%d\n",
254 channel_latency(hv_dev->channel,
255 vmbus_connection.monitor_pages[0]));
256}
257static DEVICE_ATTR_RO(server_monitor_latency);
258
259static ssize_t client_monitor_latency_show(struct device *dev,
260 struct device_attribute *dev_attr,
261 char *buf)
262{
263 struct hv_device *hv_dev = device_to_hv_device(dev);
264
265 if (!hv_dev->channel)
266 return -ENODEV;
267 return sprintf(buf, "%d\n",
268 channel_latency(hv_dev->channel,
269 vmbus_connection.monitor_pages[1]));
270}
271static DEVICE_ATTR_RO(client_monitor_latency);
272
Greg Kroah-Hartman4947c742013-09-13 11:32:58 -0700273static ssize_t server_monitor_conn_id_show(struct device *dev,
274 struct device_attribute *dev_attr,
275 char *buf)
276{
277 struct hv_device *hv_dev = device_to_hv_device(dev);
278
279 if (!hv_dev->channel)
280 return -ENODEV;
281 return sprintf(buf, "%d\n",
282 channel_conn_id(hv_dev->channel,
283 vmbus_connection.monitor_pages[0]));
284}
285static DEVICE_ATTR_RO(server_monitor_conn_id);
286
287static ssize_t client_monitor_conn_id_show(struct device *dev,
288 struct device_attribute *dev_attr,
289 char *buf)
290{
291 struct hv_device *hv_dev = device_to_hv_device(dev);
292
293 if (!hv_dev->channel)
294 return -ENODEV;
295 return sprintf(buf, "%d\n",
296 channel_conn_id(hv_dev->channel,
297 vmbus_connection.monitor_pages[1]));
298}
299static DEVICE_ATTR_RO(client_monitor_conn_id);
300
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700301static ssize_t out_intr_mask_show(struct device *dev,
302 struct device_attribute *dev_attr, char *buf)
303{
304 struct hv_device *hv_dev = device_to_hv_device(dev);
305 struct hv_ring_buffer_debug_info outbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000306 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700307
308 if (!hv_dev->channel)
309 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000310
311 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
312 &outbound);
313 if (ret < 0)
314 return ret;
315
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700316 return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
317}
318static DEVICE_ATTR_RO(out_intr_mask);
319
320static ssize_t out_read_index_show(struct device *dev,
321 struct device_attribute *dev_attr, char *buf)
322{
323 struct hv_device *hv_dev = device_to_hv_device(dev);
324 struct hv_ring_buffer_debug_info outbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000325 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700326
327 if (!hv_dev->channel)
328 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000329
330 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
331 &outbound);
332 if (ret < 0)
333 return ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700334 return sprintf(buf, "%d\n", outbound.current_read_index);
335}
336static DEVICE_ATTR_RO(out_read_index);
337
338static ssize_t out_write_index_show(struct device *dev,
339 struct device_attribute *dev_attr,
340 char *buf)
341{
342 struct hv_device *hv_dev = device_to_hv_device(dev);
343 struct hv_ring_buffer_debug_info outbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000344 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700345
346 if (!hv_dev->channel)
347 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000348
349 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
350 &outbound);
351 if (ret < 0)
352 return ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700353 return sprintf(buf, "%d\n", outbound.current_write_index);
354}
355static DEVICE_ATTR_RO(out_write_index);
356
357static ssize_t out_read_bytes_avail_show(struct device *dev,
358 struct device_attribute *dev_attr,
359 char *buf)
360{
361 struct hv_device *hv_dev = device_to_hv_device(dev);
362 struct hv_ring_buffer_debug_info outbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000363 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700364
365 if (!hv_dev->channel)
366 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000367
368 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
369 &outbound);
370 if (ret < 0)
371 return ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700372 return sprintf(buf, "%d\n", outbound.bytes_avail_toread);
373}
374static DEVICE_ATTR_RO(out_read_bytes_avail);
375
376static ssize_t out_write_bytes_avail_show(struct device *dev,
377 struct device_attribute *dev_attr,
378 char *buf)
379{
380 struct hv_device *hv_dev = device_to_hv_device(dev);
381 struct hv_ring_buffer_debug_info outbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000382 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700383
384 if (!hv_dev->channel)
385 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000386
387 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
388 &outbound);
389 if (ret < 0)
390 return ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700391 return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
392}
393static DEVICE_ATTR_RO(out_write_bytes_avail);
394
395static ssize_t in_intr_mask_show(struct device *dev,
396 struct device_attribute *dev_attr, char *buf)
397{
398 struct hv_device *hv_dev = device_to_hv_device(dev);
399 struct hv_ring_buffer_debug_info inbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000400 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700401
402 if (!hv_dev->channel)
403 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000404
405 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
406 if (ret < 0)
407 return ret;
408
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700409 return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
410}
411static DEVICE_ATTR_RO(in_intr_mask);
412
413static ssize_t in_read_index_show(struct device *dev,
414 struct device_attribute *dev_attr, char *buf)
415{
416 struct hv_device *hv_dev = device_to_hv_device(dev);
417 struct hv_ring_buffer_debug_info inbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000418 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700419
420 if (!hv_dev->channel)
421 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000422
423 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
424 if (ret < 0)
425 return ret;
426
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700427 return sprintf(buf, "%d\n", inbound.current_read_index);
428}
429static DEVICE_ATTR_RO(in_read_index);
430
431static ssize_t in_write_index_show(struct device *dev,
432 struct device_attribute *dev_attr, char *buf)
433{
434 struct hv_device *hv_dev = device_to_hv_device(dev);
435 struct hv_ring_buffer_debug_info inbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000436 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700437
438 if (!hv_dev->channel)
439 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000440
441 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
442 if (ret < 0)
443 return ret;
444
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700445 return sprintf(buf, "%d\n", inbound.current_write_index);
446}
447static DEVICE_ATTR_RO(in_write_index);
448
449static ssize_t in_read_bytes_avail_show(struct device *dev,
450 struct device_attribute *dev_attr,
451 char *buf)
452{
453 struct hv_device *hv_dev = device_to_hv_device(dev);
454 struct hv_ring_buffer_debug_info inbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000455 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700456
457 if (!hv_dev->channel)
458 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000459
460 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
461 if (ret < 0)
462 return ret;
463
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700464 return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
465}
466static DEVICE_ATTR_RO(in_read_bytes_avail);
467
468static ssize_t in_write_bytes_avail_show(struct device *dev,
469 struct device_attribute *dev_attr,
470 char *buf)
471{
472 struct hv_device *hv_dev = device_to_hv_device(dev);
473 struct hv_ring_buffer_debug_info inbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000474 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700475
476 if (!hv_dev->channel)
477 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000478
479 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
480 if (ret < 0)
481 return ret;
482
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700483 return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
484}
485static DEVICE_ATTR_RO(in_write_bytes_avail);
486
Dexuan Cui042ab032015-08-05 00:52:43 -0700487static ssize_t channel_vp_mapping_show(struct device *dev,
488 struct device_attribute *dev_attr,
489 char *buf)
490{
491 struct hv_device *hv_dev = device_to_hv_device(dev);
492 struct vmbus_channel *channel = hv_dev->channel, *cur_sc;
493 unsigned long flags;
494 int buf_size = PAGE_SIZE, n_written, tot_written;
495 struct list_head *cur;
496
497 if (!channel)
498 return -ENODEV;
499
500 tot_written = snprintf(buf, buf_size, "%u:%u\n",
501 channel->offermsg.child_relid, channel->target_cpu);
502
503 spin_lock_irqsave(&channel->lock, flags);
504
505 list_for_each(cur, &channel->sc_list) {
506 if (tot_written >= buf_size - 1)
507 break;
508
509 cur_sc = list_entry(cur, struct vmbus_channel, sc_list);
510 n_written = scnprintf(buf + tot_written,
511 buf_size - tot_written,
512 "%u:%u\n",
513 cur_sc->offermsg.child_relid,
514 cur_sc->target_cpu);
515 tot_written += n_written;
516 }
517
518 spin_unlock_irqrestore(&channel->lock, flags);
519
520 return tot_written;
521}
522static DEVICE_ATTR_RO(channel_vp_mapping);
523
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800524static ssize_t vendor_show(struct device *dev,
525 struct device_attribute *dev_attr,
526 char *buf)
527{
528 struct hv_device *hv_dev = device_to_hv_device(dev);
529 return sprintf(buf, "0x%x\n", hv_dev->vendor_id);
530}
531static DEVICE_ATTR_RO(vendor);
532
533static ssize_t device_show(struct device *dev,
534 struct device_attribute *dev_attr,
535 char *buf)
536{
537 struct hv_device *hv_dev = device_to_hv_device(dev);
538 return sprintf(buf, "0x%x\n", hv_dev->device_id);
539}
540static DEVICE_ATTR_RO(device);
541
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000542static ssize_t driver_override_store(struct device *dev,
543 struct device_attribute *attr,
544 const char *buf, size_t count)
545{
546 struct hv_device *hv_dev = device_to_hv_device(dev);
547 char *driver_override, *old, *cp;
548
549 /* We need to keep extra room for a newline */
550 if (count >= (PAGE_SIZE - 1))
551 return -EINVAL;
552
553 driver_override = kstrndup(buf, count, GFP_KERNEL);
554 if (!driver_override)
555 return -ENOMEM;
556
557 cp = strchr(driver_override, '\n');
558 if (cp)
559 *cp = '\0';
560
561 device_lock(dev);
562 old = hv_dev->driver_override;
563 if (strlen(driver_override)) {
564 hv_dev->driver_override = driver_override;
565 } else {
566 kfree(driver_override);
567 hv_dev->driver_override = NULL;
568 }
569 device_unlock(dev);
570
571 kfree(old);
572
573 return count;
574}
575
576static ssize_t driver_override_show(struct device *dev,
577 struct device_attribute *attr, char *buf)
578{
579 struct hv_device *hv_dev = device_to_hv_device(dev);
580 ssize_t len;
581
582 device_lock(dev);
583 len = snprintf(buf, PAGE_SIZE, "%s\n", hv_dev->driver_override);
584 device_unlock(dev);
585
586 return len;
587}
588static DEVICE_ATTR_RW(driver_override);
589
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700590/* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800591static struct attribute *vmbus_dev_attrs[] = {
Greg Kroah-Hartman03f3a912013-09-13 11:32:49 -0700592 &dev_attr_id.attr,
Greg Kroah-Hartmana8fb5f32013-09-13 11:32:50 -0700593 &dev_attr_state.attr,
Greg Kroah-Hartman5ffd00e2013-09-13 11:32:51 -0700594 &dev_attr_monitor_id.attr,
Greg Kroah-Hartman68234c02013-09-13 11:32:53 -0700595 &dev_attr_class_id.attr,
Greg Kroah-Hartman7c55e1d2013-09-13 11:32:54 -0700596 &dev_attr_device_id.attr,
Greg Kroah-Hartman647fa372013-09-13 11:32:52 -0700597 &dev_attr_modalias.attr,
Stephen Hemminger7ceb1c32018-07-28 21:58:48 +0000598#ifdef CONFIG_NUMA
599 &dev_attr_numa_node.attr,
600#endif
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700601 &dev_attr_server_monitor_pending.attr,
602 &dev_attr_client_monitor_pending.attr,
Greg Kroah-Hartman1cee2722013-09-13 11:32:57 -0700603 &dev_attr_server_monitor_latency.attr,
604 &dev_attr_client_monitor_latency.attr,
Greg Kroah-Hartman4947c742013-09-13 11:32:58 -0700605 &dev_attr_server_monitor_conn_id.attr,
606 &dev_attr_client_monitor_conn_id.attr,
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700607 &dev_attr_out_intr_mask.attr,
608 &dev_attr_out_read_index.attr,
609 &dev_attr_out_write_index.attr,
610 &dev_attr_out_read_bytes_avail.attr,
611 &dev_attr_out_write_bytes_avail.attr,
612 &dev_attr_in_intr_mask.attr,
613 &dev_attr_in_read_index.attr,
614 &dev_attr_in_write_index.attr,
615 &dev_attr_in_read_bytes_avail.attr,
616 &dev_attr_in_write_bytes_avail.attr,
Dexuan Cui042ab032015-08-05 00:52:43 -0700617 &dev_attr_channel_vp_mapping.attr,
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800618 &dev_attr_vendor.attr,
619 &dev_attr_device.attr,
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000620 &dev_attr_driver_override.attr,
Greg Kroah-Hartman03f3a912013-09-13 11:32:49 -0700621 NULL,
622};
Kimberly Brown46fc1542019-03-19 00:04:01 -0400623
624/*
625 * Device-level attribute_group callback function. Returns the permission for
626 * each attribute, and returns 0 if an attribute is not visible.
627 */
628static umode_t vmbus_dev_attr_is_visible(struct kobject *kobj,
629 struct attribute *attr, int idx)
630{
631 struct device *dev = kobj_to_dev(kobj);
632 const struct hv_device *hv_dev = device_to_hv_device(dev);
633
634 /* Hide the monitor attributes if the monitor mechanism is not used. */
635 if (!hv_dev->channel->offermsg.monitor_allocated &&
636 (attr == &dev_attr_monitor_id.attr ||
637 attr == &dev_attr_server_monitor_pending.attr ||
638 attr == &dev_attr_client_monitor_pending.attr ||
639 attr == &dev_attr_server_monitor_latency.attr ||
640 attr == &dev_attr_client_monitor_latency.attr ||
641 attr == &dev_attr_server_monitor_conn_id.attr ||
642 attr == &dev_attr_client_monitor_conn_id.attr))
643 return 0;
644
645 return attr->mode;
646}
647
648static const struct attribute_group vmbus_dev_group = {
649 .attrs = vmbus_dev_attrs,
650 .is_visible = vmbus_dev_attr_is_visible
651};
652__ATTRIBUTE_GROUPS(vmbus_dev);
Greg Kroah-Hartman03f3a912013-09-13 11:32:49 -0700653
K. Y. Srinivasanadde2482011-03-15 15:03:37 -0700654/*
655 * vmbus_uevent - add uevent for our device
656 *
657 * This routine is invoked when a device is added or removed on the vmbus to
658 * generate a uevent to udev in the userspace. The udev will then look at its
659 * rule and the uevent generated here to load the appropriate driver
K. Y. Srinivasan0ddda662011-08-25 09:48:38 -0700660 *
661 * The alias string will be of the form vmbus:guid where guid is the string
662 * representation of the device guid (each byte of the guid will be
663 * represented with two hex characters.
K. Y. Srinivasanadde2482011-03-15 15:03:37 -0700664 */
665static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
666{
667 struct hv_device *dev = device_to_hv_device(device);
Olaf Heringfd776ba2011-09-02 18:25:56 +0200668 int ret;
669 char alias_name[VMBUS_ALIAS_LEN + 1];
K. Y. Srinivasanadde2482011-03-15 15:03:37 -0700670
Olaf Heringfd776ba2011-09-02 18:25:56 +0200671 print_alias_name(dev, alias_name);
K. Y. Srinivasan0ddda662011-08-25 09:48:38 -0700672 ret = add_uevent_var(env, "MODALIAS=vmbus:%s", alias_name);
673 return ret;
K. Y. Srinivasanadde2482011-03-15 15:03:37 -0700674}
675
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000676static const struct hv_vmbus_device_id *
Andy Shevchenko593db802019-01-10 16:25:32 +0200677hv_vmbus_dev_match(const struct hv_vmbus_device_id *id, const guid_t *guid)
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000678{
679 if (id == NULL)
680 return NULL; /* empty device table */
681
Andy Shevchenko593db802019-01-10 16:25:32 +0200682 for (; !guid_is_null(&id->guid); id++)
683 if (guid_equal(&id->guid, guid))
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000684 return id;
685
686 return NULL;
687}
688
689static const struct hv_vmbus_device_id *
Andy Shevchenko593db802019-01-10 16:25:32 +0200690hv_vmbus_dynid_match(struct hv_driver *drv, const guid_t *guid)
K. Y. Srinivasan3037a7b2011-09-13 10:59:37 -0700691{
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800692 const struct hv_vmbus_device_id *id = NULL;
693 struct vmbus_dynid *dynid;
694
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800695 spin_lock(&drv->dynids.lock);
696 list_for_each_entry(dynid, &drv->dynids.list, node) {
Andy Shevchenko593db802019-01-10 16:25:32 +0200697 if (guid_equal(&dynid->id.guid, guid)) {
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800698 id = &dynid->id;
699 break;
700 }
701 }
702 spin_unlock(&drv->dynids.lock);
703
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000704 return id;
705}
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800706
Andy Shevchenko593db802019-01-10 16:25:32 +0200707static const struct hv_vmbus_device_id vmbus_device_null;
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800708
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000709/*
710 * Return a matching hv_vmbus_device_id pointer.
711 * If there is no match, return NULL.
712 */
713static const struct hv_vmbus_device_id *hv_vmbus_get_id(struct hv_driver *drv,
714 struct hv_device *dev)
715{
Andy Shevchenko593db802019-01-10 16:25:32 +0200716 const guid_t *guid = &dev->dev_type;
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000717 const struct hv_vmbus_device_id *id;
K. Y. Srinivasan3037a7b2011-09-13 10:59:37 -0700718
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000719 /* When driver_override is set, only bind to the matching driver */
720 if (dev->driver_override && strcmp(dev->driver_override, drv->name))
721 return NULL;
722
723 /* Look at the dynamic ids first, before the static ones */
724 id = hv_vmbus_dynid_match(drv, guid);
725 if (!id)
726 id = hv_vmbus_dev_match(drv->id_table, guid);
727
728 /* driver_override will always match, send a dummy id */
729 if (!id && dev->driver_override)
730 id = &vmbus_device_null;
731
732 return id;
K. Y. Srinivasan3037a7b2011-09-13 10:59:37 -0700733}
734
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800735/* vmbus_add_dynid - add a new device ID to this driver and re-probe devices */
Andy Shevchenko593db802019-01-10 16:25:32 +0200736static int vmbus_add_dynid(struct hv_driver *drv, guid_t *guid)
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800737{
738 struct vmbus_dynid *dynid;
739
740 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
741 if (!dynid)
742 return -ENOMEM;
743
744 dynid->id.guid = *guid;
745
746 spin_lock(&drv->dynids.lock);
747 list_add_tail(&dynid->node, &drv->dynids.list);
748 spin_unlock(&drv->dynids.lock);
749
750 return driver_attach(&drv->driver);
751}
752
753static void vmbus_free_dynids(struct hv_driver *drv)
754{
755 struct vmbus_dynid *dynid, *n;
756
757 spin_lock(&drv->dynids.lock);
758 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
759 list_del(&dynid->node);
760 kfree(dynid);
761 }
762 spin_unlock(&drv->dynids.lock);
763}
764
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800765/*
766 * store_new_id - sysfs frontend to vmbus_add_dynid()
767 *
768 * Allow GUIDs to be added to an existing driver via sysfs.
769 */
770static ssize_t new_id_store(struct device_driver *driver, const char *buf,
771 size_t count)
772{
773 struct hv_driver *drv = drv_to_hv_drv(driver);
Andy Shevchenko593db802019-01-10 16:25:32 +0200774 guid_t guid;
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800775 ssize_t retval;
776
Andy Shevchenko593db802019-01-10 16:25:32 +0200777 retval = guid_parse(buf, &guid);
Andy Shevchenko31100102017-05-18 10:46:06 -0700778 if (retval)
779 return retval;
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800780
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000781 if (hv_vmbus_dynid_match(drv, &guid))
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800782 return -EEXIST;
783
784 retval = vmbus_add_dynid(drv, &guid);
785 if (retval)
786 return retval;
787 return count;
788}
789static DRIVER_ATTR_WO(new_id);
790
791/*
792 * store_remove_id - remove a PCI device ID from this driver
793 *
794 * Removes a dynamic pci device ID to this driver.
795 */
796static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
797 size_t count)
798{
799 struct hv_driver *drv = drv_to_hv_drv(driver);
800 struct vmbus_dynid *dynid, *n;
Andy Shevchenko593db802019-01-10 16:25:32 +0200801 guid_t guid;
Andy Shevchenko31100102017-05-18 10:46:06 -0700802 ssize_t retval;
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800803
Andy Shevchenko593db802019-01-10 16:25:32 +0200804 retval = guid_parse(buf, &guid);
Andy Shevchenko31100102017-05-18 10:46:06 -0700805 if (retval)
806 return retval;
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800807
Andy Shevchenko31100102017-05-18 10:46:06 -0700808 retval = -ENODEV;
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800809 spin_lock(&drv->dynids.lock);
810 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
811 struct hv_vmbus_device_id *id = &dynid->id;
812
Andy Shevchenko593db802019-01-10 16:25:32 +0200813 if (guid_equal(&id->guid, &guid)) {
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800814 list_del(&dynid->node);
815 kfree(dynid);
816 retval = count;
817 break;
818 }
819 }
820 spin_unlock(&drv->dynids.lock);
821
822 return retval;
823}
824static DRIVER_ATTR_WO(remove_id);
825
826static struct attribute *vmbus_drv_attrs[] = {
827 &driver_attr_new_id.attr,
828 &driver_attr_remove_id.attr,
829 NULL,
830};
831ATTRIBUTE_GROUPS(vmbus_drv);
K. Y. Srinivasan3037a7b2011-09-13 10:59:37 -0700832
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700833
834/*
835 * vmbus_match - Attempt to match the specified device to the specified driver
836 */
837static int vmbus_match(struct device *device, struct device_driver *driver)
838{
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700839 struct hv_driver *drv = drv_to_hv_drv(driver);
K. Y. Srinivasane8e27042011-06-06 15:50:04 -0700840 struct hv_device *hv_dev = device_to_hv_device(device);
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700841
Dexuan Cui8981da32016-01-27 22:29:41 -0800842 /* The hv_sock driver handles all hv_sock offers. */
843 if (is_hvsock_channel(hv_dev->channel))
844 return drv->hvsock;
845
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000846 if (hv_vmbus_get_id(drv, hv_dev))
K. Y. Srinivasan3037a7b2011-09-13 10:59:37 -0700847 return 1;
K. Y. Srinivasande632a2b2011-04-26 09:20:24 -0700848
K. Y. Srinivasan5841a822011-08-25 09:48:39 -0700849 return 0;
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700850}
851
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700852/*
853 * vmbus_probe - Add the new vmbus's child device
854 */
855static int vmbus_probe(struct device *child_device)
856{
857 int ret = 0;
858 struct hv_driver *drv =
859 drv_to_hv_drv(child_device->driver);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700860 struct hv_device *dev = device_to_hv_device(child_device);
K. Y. Srinivasan84946892011-09-13 10:59:38 -0700861 const struct hv_vmbus_device_id *dev_id;
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700862
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000863 dev_id = hv_vmbus_get_id(drv, dev);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700864 if (drv->probe) {
K. Y. Srinivasan84946892011-09-13 10:59:38 -0700865 ret = drv->probe(dev, dev_id);
K. Y. Srinivasanb14a7b32011-04-29 13:45:03 -0700866 if (ret != 0)
Hank Janssen0a466182011-03-29 13:58:47 -0700867 pr_err("probe failed for device %s (%d)\n",
868 dev_name(child_device), ret);
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700869
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700870 } else {
Hank Janssen0a466182011-03-29 13:58:47 -0700871 pr_err("probe not set for driver %s\n",
872 dev_name(child_device));
K. Y. Srinivasan6de925b2011-06-06 15:50:07 -0700873 ret = -ENODEV;
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700874 }
875 return ret;
876}
877
K. Y. Srinivasanc5dce3d2011-03-15 15:03:40 -0700878/*
879 * vmbus_remove - Remove a vmbus device
880 */
881static int vmbus_remove(struct device *child_device)
882{
K. Y. Srinivasand15a0302015-02-28 11:18:16 -0800883 struct hv_driver *drv;
K. Y. Srinivasan415b0232011-04-29 13:45:12 -0700884 struct hv_device *dev = device_to_hv_device(child_device);
K. Y. Srinivasanc5dce3d2011-03-15 15:03:40 -0700885
K. Y. Srinivasand15a0302015-02-28 11:18:16 -0800886 if (child_device->driver) {
887 drv = drv_to_hv_drv(child_device->driver);
888 if (drv->remove)
889 drv->remove(dev);
K. Y. Srinivasand15a0302015-02-28 11:18:16 -0800890 }
K. Y. Srinivasanc5dce3d2011-03-15 15:03:40 -0700891
892 return 0;
893}
894
K. Y. Srinivasaneb1bb252011-03-15 15:03:41 -0700895
896/*
897 * vmbus_shutdown - Shutdown a vmbus device
898 */
899static void vmbus_shutdown(struct device *child_device)
900{
901 struct hv_driver *drv;
K. Y. Srinivasanca6887f2011-04-29 13:45:14 -0700902 struct hv_device *dev = device_to_hv_device(child_device);
K. Y. Srinivasaneb1bb252011-03-15 15:03:41 -0700903
904
905 /* The device may not be attached yet */
906 if (!child_device->driver)
907 return;
908
909 drv = drv_to_hv_drv(child_device->driver);
910
K. Y. Srinivasanca6887f2011-04-29 13:45:14 -0700911 if (drv->shutdown)
912 drv->shutdown(dev);
K. Y. Srinivasaneb1bb252011-03-15 15:03:41 -0700913}
914
Dexuan Cui271b2222019-09-05 23:01:17 +0000915/*
916 * vmbus_suspend - Suspend a vmbus device
917 */
918static int vmbus_suspend(struct device *child_device)
919{
920 struct hv_driver *drv;
921 struct hv_device *dev = device_to_hv_device(child_device);
922
923 /* The device may not be attached yet */
924 if (!child_device->driver)
925 return 0;
926
927 drv = drv_to_hv_drv(child_device->driver);
928 if (!drv->suspend)
929 return -EOPNOTSUPP;
930
931 return drv->suspend(dev);
932}
933
934/*
935 * vmbus_resume - Resume a vmbus device
936 */
937static int vmbus_resume(struct device *child_device)
938{
939 struct hv_driver *drv;
940 struct hv_device *dev = device_to_hv_device(child_device);
941
942 /* The device may not be attached yet */
943 if (!child_device->driver)
944 return 0;
945
946 drv = drv_to_hv_drv(child_device->driver);
947 if (!drv->resume)
948 return -EOPNOTSUPP;
949
950 return drv->resume(dev);
951}
K. Y. Srinivasan086e7a52011-03-15 15:03:42 -0700952
953/*
954 * vmbus_device_release - Final callback release of the vmbus child device
955 */
956static void vmbus_device_release(struct device *device)
957{
K. Y. Srinivasane8e27042011-06-06 15:50:04 -0700958 struct hv_device *hv_dev = device_to_hv_device(device);
Dexuan Cui34c68012015-12-14 16:01:49 -0800959 struct vmbus_channel *channel = hv_dev->channel;
K. Y. Srinivasan086e7a52011-03-15 15:03:42 -0700960
K. Y. Srinivasan54a662652017-04-30 16:21:18 -0700961 mutex_lock(&vmbus_connection.channel_mutex);
Stephen Hemminger800b9322018-09-14 09:10:15 -0700962 hv_process_channel_removal(channel);
K. Y. Srinivasan54a662652017-04-30 16:21:18 -0700963 mutex_unlock(&vmbus_connection.channel_mutex);
K. Y. Srinivasane8e27042011-06-06 15:50:04 -0700964 kfree(hv_dev);
K. Y. Srinivasan086e7a52011-03-15 15:03:42 -0700965}
966
Dexuan Cui271b2222019-09-05 23:01:17 +0000967/*
968 * Note: we must use SET_NOIRQ_SYSTEM_SLEEP_PM_OPS rather than
969 * SET_SYSTEM_SLEEP_PM_OPS: see the comment before vmbus_bus_pm.
970 */
971static const struct dev_pm_ops vmbus_pm = {
972 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(vmbus_suspend, vmbus_resume)
973};
974
Bill Pemberton454f18a2009-07-27 16:47:24 -0400975/* The one and only one */
K. Y. Srinivasan9adcac52011-04-29 13:45:08 -0700976static struct bus_type hv_bus = {
977 .name = "vmbus",
978 .match = vmbus_match,
979 .shutdown = vmbus_shutdown,
980 .remove = vmbus_remove,
981 .probe = vmbus_probe,
982 .uevent = vmbus_uevent,
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800983 .dev_groups = vmbus_dev_groups,
984 .drv_groups = vmbus_drv_groups,
Dexuan Cui271b2222019-09-05 23:01:17 +0000985 .pm = &vmbus_pm,
Hank Janssen3e7ee492009-07-13 16:02:34 -0700986};
987
Timo Teräsbf6506f2010-12-15 20:48:08 +0200988struct onmessage_work_context {
989 struct work_struct work;
990 struct hv_message msg;
991};
992
993static void vmbus_onmessage_work(struct work_struct *work)
994{
995 struct onmessage_work_context *ctx;
996
Vitaly Kuznetsov09a19622015-02-27 11:25:54 -0800997 /* Do not process messages if we're in DISCONNECTED state */
998 if (vmbus_connection.conn_state == DISCONNECTED)
999 return;
1000
Timo Teräsbf6506f2010-12-15 20:48:08 +02001001 ctx = container_of(work, struct onmessage_work_context,
1002 work);
1003 vmbus_onmessage(&ctx->msg);
1004 kfree(ctx);
1005}
1006
K. Y. Srinivasand81274a2016-02-26 15:13:21 -08001007void vmbus_on_msg_dpc(unsigned long data)
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001008{
Stephen Hemminger37cdd992017-02-11 23:02:19 -07001009 struct hv_per_cpu_context *hv_cpu = (void *)data;
1010 void *page_addr = hv_cpu->synic_message_page;
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001011 struct hv_message *msg = (struct hv_message *)page_addr +
1012 VMBUS_MESSAGE_SINT;
Dexuan Cui652594c2015-03-27 09:10:08 -07001013 struct vmbus_channel_message_header *hdr;
Stephen Hemmingere6242fa2017-03-04 18:27:16 -07001014 const struct vmbus_channel_message_table_entry *entry;
Timo Teräsbf6506f2010-12-15 20:48:08 +02001015 struct onmessage_work_context *ctx;
Vitaly Kuznetsovcd95aad2016-04-30 19:21:34 -07001016 u32 message_type = msg->header.message_type;
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001017
Vitaly Kuznetsovcd95aad2016-04-30 19:21:34 -07001018 if (message_type == HVMSG_NONE)
Vitaly Kuznetsov7be3e162016-02-26 15:13:15 -08001019 /* no msg */
1020 return;
Dexuan Cui652594c2015-03-27 09:10:08 -07001021
Vitaly Kuznetsov7be3e162016-02-26 15:13:15 -08001022 hdr = (struct vmbus_channel_message_header *)msg->u.payload;
Dexuan Cui652594c2015-03-27 09:10:08 -07001023
Vitaly Kuznetsovc9fe0f82017-10-29 12:21:00 -07001024 trace_vmbus_on_msg_dpc(hdr);
1025
Vitaly Kuznetsov7be3e162016-02-26 15:13:15 -08001026 if (hdr->msgtype >= CHANNELMSG_COUNT) {
1027 WARN_ONCE(1, "unknown msgtype=%d\n", hdr->msgtype);
1028 goto msg_handled;
1029 }
Dexuan Cui652594c2015-03-27 09:10:08 -07001030
Vitaly Kuznetsov7be3e162016-02-26 15:13:15 -08001031 entry = &channel_message_table[hdr->msgtype];
1032 if (entry->handler_type == VMHT_BLOCKING) {
1033 ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
1034 if (ctx == NULL)
1035 return;
Dexuan Cui652594c2015-03-27 09:10:08 -07001036
Vitaly Kuznetsov7be3e162016-02-26 15:13:15 -08001037 INIT_WORK(&ctx->work, vmbus_onmessage_work);
1038 memcpy(&ctx->msg, msg, sizeof(*msg));
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001039
K. Y. Srinivasan54a662652017-04-30 16:21:18 -07001040 /*
1041 * The host can generate a rescind message while we
1042 * may still be handling the original offer. We deal with
1043 * this condition by ensuring the processing is done on the
1044 * same CPU.
1045 */
1046 switch (hdr->msgtype) {
1047 case CHANNELMSG_RESCIND_CHANNELOFFER:
1048 /*
1049 * If we are handling the rescind message;
1050 * schedule the work on the global work queue.
1051 */
1052 schedule_work_on(vmbus_connection.connect_cpu,
1053 &ctx->work);
1054 break;
1055
1056 case CHANNELMSG_OFFERCHANNEL:
1057 atomic_inc(&vmbus_connection.offer_in_progress);
1058 queue_work_on(vmbus_connection.connect_cpu,
1059 vmbus_connection.work_queue,
1060 &ctx->work);
1061 break;
1062
1063 default:
1064 queue_work(vmbus_connection.work_queue, &ctx->work);
1065 }
Vitaly Kuznetsov7be3e162016-02-26 15:13:15 -08001066 } else
1067 entry->message_handler(hdr);
Dexuan Cui652594c2015-03-27 09:10:08 -07001068
1069msg_handled:
Vitaly Kuznetsovcd95aad2016-04-30 19:21:34 -07001070 vmbus_signal_eom(msg, message_type);
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001071}
1072
Dexuan Cui1f48dcf2019-09-05 23:01:20 +00001073/*
1074 * Fake RESCIND_CHANNEL messages to clean up hv_sock channels by force for
1075 * hibernation, because hv_sock connections can not persist across hibernation.
1076 */
1077static void vmbus_force_channel_rescinded(struct vmbus_channel *channel)
1078{
1079 struct onmessage_work_context *ctx;
1080 struct vmbus_channel_rescind_offer *rescind;
1081
1082 WARN_ON(!is_hvsock_channel(channel));
1083
1084 /*
1085 * sizeof(*ctx) is small and the allocation should really not fail,
1086 * otherwise the state of the hv_sock connections ends up in limbo.
1087 */
1088 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL | __GFP_NOFAIL);
1089
1090 /*
1091 * So far, these are not really used by Linux. Just set them to the
1092 * reasonable values conforming to the definitions of the fields.
1093 */
1094 ctx->msg.header.message_type = 1;
1095 ctx->msg.header.payload_size = sizeof(*rescind);
1096
1097 /* These values are actually used by Linux. */
1098 rescind = (struct vmbus_channel_rescind_offer *)ctx->msg.u.payload;
1099 rescind->header.msgtype = CHANNELMSG_RESCIND_CHANNELOFFER;
1100 rescind->child_relid = channel->offermsg.child_relid;
1101
1102 INIT_WORK(&ctx->work, vmbus_onmessage_work);
1103
1104 queue_work_on(vmbus_connection.connect_cpu,
1105 vmbus_connection.work_queue,
1106 &ctx->work);
1107}
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001108
1109/*
Stephen Hemmingerb71e3282017-02-11 23:02:21 -07001110 * Direct callback for channels using other deferred processing
1111 */
1112static void vmbus_channel_isr(struct vmbus_channel *channel)
1113{
1114 void (*callback_fn)(void *);
1115
1116 callback_fn = READ_ONCE(channel->onchannel_callback);
1117 if (likely(callback_fn != NULL))
1118 (*callback_fn)(channel->channel_callback_context);
1119}
1120
1121/*
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001122 * Schedule all channels with events pending
1123 */
1124static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu)
1125{
1126 unsigned long *recv_int_page;
1127 u32 maxbits, relid;
1128
1129 if (vmbus_proto_version < VERSION_WIN8) {
1130 maxbits = MAX_NUM_CHANNELS_SUPPORTED;
1131 recv_int_page = vmbus_connection.recv_int_page;
1132 } else {
1133 /*
1134 * When the host is win8 and beyond, the event page
1135 * can be directly checked to get the id of the channel
1136 * that has the interrupt pending.
1137 */
1138 void *page_addr = hv_cpu->synic_event_page;
1139 union hv_synic_event_flags *event
1140 = (union hv_synic_event_flags *)page_addr +
1141 VMBUS_MESSAGE_SINT;
1142
1143 maxbits = HV_EVENT_FLAGS_COUNT;
1144 recv_int_page = event->flags;
1145 }
1146
1147 if (unlikely(!recv_int_page))
1148 return;
1149
1150 for_each_set_bit(relid, recv_int_page, maxbits) {
1151 struct vmbus_channel *channel;
1152
1153 if (!sync_test_and_clear_bit(relid, recv_int_page))
1154 continue;
1155
1156 /* Special case - vmbus channel protocol msg */
1157 if (relid == 0)
1158 continue;
1159
Stephen Hemminger8200f202017-03-04 18:13:57 -07001160 rcu_read_lock();
1161
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001162 /* Find channel based on relid */
Stephen Hemminger8200f202017-03-04 18:13:57 -07001163 list_for_each_entry_rcu(channel, &hv_cpu->chan_list, percpu_list) {
Stephen Hemmingerb71e3282017-02-11 23:02:21 -07001164 if (channel->offermsg.child_relid != relid)
1165 continue;
1166
K. Y. Srinivasan6f3d7912017-08-11 10:03:59 -07001167 if (channel->rescind)
1168 continue;
1169
Vitaly Kuznetsov991f8f12017-10-29 12:21:16 -07001170 trace_vmbus_chan_sched(channel);
1171
Stephen Hemminger6981fbf2017-10-29 11:33:40 -07001172 ++channel->interrupts;
1173
Stephen Hemmingerb71e3282017-02-11 23:02:21 -07001174 switch (channel->callback_mode) {
1175 case HV_CALL_ISR:
1176 vmbus_channel_isr(channel);
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001177 break;
Stephen Hemmingerb71e3282017-02-11 23:02:21 -07001178
1179 case HV_CALL_BATCHED:
1180 hv_begin_read(&channel->inbound);
1181 /* fallthrough */
1182 case HV_CALL_DIRECT:
1183 tasklet_schedule(&channel->callback_event);
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001184 }
1185 }
Stephen Hemminger8200f202017-03-04 18:13:57 -07001186
1187 rcu_read_unlock();
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001188 }
1189}
1190
Thomas Gleixner76d388c2014-03-05 13:42:14 +01001191static void vmbus_isr(void)
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001192{
Stephen Hemminger37cdd992017-02-11 23:02:19 -07001193 struct hv_per_cpu_context *hv_cpu
1194 = this_cpu_ptr(hv_context.cpu_context);
1195 void *page_addr = hv_cpu->synic_event_page;
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001196 struct hv_message *msg;
1197 union hv_synic_event_flags *event;
K. Y. Srinivasanae4636e2011-08-27 11:31:35 -07001198 bool handled = false;
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001199
Stephen Hemminger37cdd992017-02-11 23:02:19 -07001200 if (unlikely(page_addr == NULL))
Thomas Gleixner76d388c2014-03-05 13:42:14 +01001201 return;
K. Y. Srinivasan5ab05952012-12-01 06:46:55 -08001202
1203 event = (union hv_synic_event_flags *)page_addr +
1204 VMBUS_MESSAGE_SINT;
K. Y. Srinivasan7341d902011-08-31 14:35:56 -07001205 /*
1206 * Check for events before checking for messages. This is the order
1207 * in which events and messages are checked in Windows guests on
1208 * Hyper-V, and the Windows team suggested we do the same.
1209 */
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001210
K. Y. Srinivasan6552ecd72012-12-01 06:46:49 -08001211 if ((vmbus_proto_version == VERSION_WS2008) ||
1212 (vmbus_proto_version == VERSION_WIN7)) {
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001213
K. Y. Srinivasan6552ecd72012-12-01 06:46:49 -08001214 /* Since we are a child, we only need to check bit 0 */
Stephen Hemminger5c1bec62017-02-05 17:20:31 -07001215 if (sync_test_and_clear_bit(0, event->flags))
K. Y. Srinivasan6552ecd72012-12-01 06:46:49 -08001216 handled = true;
K. Y. Srinivasan6552ecd72012-12-01 06:46:49 -08001217 } else {
1218 /*
1219 * Our host is win8 or above. The signaling mechanism
1220 * has changed and we can directly look at the event page.
1221 * If bit n is set then we have an interrup on the channel
1222 * whose id is n.
1223 */
K. Y. Srinivasanae4636e2011-08-27 11:31:35 -07001224 handled = true;
K. Y. Srinivasan793be9c2011-03-15 15:03:43 -07001225 }
K. Y. Srinivasanae4636e2011-08-27 11:31:35 -07001226
K. Y. Srinivasan6552ecd72012-12-01 06:46:49 -08001227 if (handled)
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001228 vmbus_chan_sched(hv_cpu);
K. Y. Srinivasan6552ecd72012-12-01 06:46:49 -08001229
Stephen Hemminger37cdd992017-02-11 23:02:19 -07001230 page_addr = hv_cpu->synic_message_page;
K. Y. Srinivasan7341d902011-08-31 14:35:56 -07001231 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
1232
1233 /* Check if there are actual msgs to be processed */
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -08001234 if (msg->header.message_type != HVMSG_NONE) {
Michael Kelleyfd1fea62019-07-01 04:25:56 +00001235 if (msg->header.message_type == HVMSG_TIMER_EXPIRED) {
1236 hv_stimer0_isr();
1237 vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED);
1238 } else
Stephen Hemminger37cdd992017-02-11 23:02:19 -07001239 tasklet_schedule(&hv_cpu->msg_dpc);
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -08001240 }
Stephan Mueller4b44f2d2016-05-02 02:14:34 -04001241
1242 add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0);
K. Y. Srinivasan793be9c2011-03-15 15:03:43 -07001243}
1244
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001245/*
1246 * Boolean to control whether to report panic messages over Hyper-V.
1247 *
1248 * It can be set via /proc/sys/kernel/hyperv/record_panic_msg
1249 */
1250static int sysctl_record_panic_msg = 1;
1251
1252/*
1253 * Callback from kmsg_dump. Grab as much as possible from the end of the kmsg
1254 * buffer and call into Hyper-V to transfer the data.
1255 */
1256static void hv_kmsg_dump(struct kmsg_dumper *dumper,
1257 enum kmsg_dump_reason reason)
1258{
1259 size_t bytes_written;
1260 phys_addr_t panic_pa;
1261
1262 /* We are only interested in panics. */
1263 if ((reason != KMSG_DUMP_PANIC) || (!sysctl_record_panic_msg))
1264 return;
1265
1266 panic_pa = virt_to_phys(hv_panic_page);
1267
1268 /*
1269 * Write dump contents to the page. No need to synchronize; panic should
1270 * be single-threaded.
1271 */
Sunil Muthuswamyddcaf3c2018-07-28 21:58:45 +00001272 kmsg_dump_get_buffer(dumper, true, hv_panic_page, PAGE_SIZE,
1273 &bytes_written);
1274 if (bytes_written)
1275 hyperv_report_panic_msg(panic_pa, bytes_written);
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001276}
1277
1278static struct kmsg_dumper hv_kmsg_dumper = {
1279 .dump = hv_kmsg_dump,
1280};
1281
1282static struct ctl_table_header *hv_ctl_table_hdr;
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001283
1284/*
1285 * sysctl option to allow the user to control whether kmsg data should be
1286 * reported to Hyper-V on panic.
1287 */
1288static struct ctl_table hv_ctl_table[] = {
1289 {
1290 .procname = "hyperv_record_panic_msg",
1291 .data = &sysctl_record_panic_msg,
1292 .maxlen = sizeof(int),
1293 .mode = 0644,
1294 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -07001295 .extra1 = SYSCTL_ZERO,
1296 .extra2 = SYSCTL_ONE
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001297 },
1298 {}
1299};
1300
1301static struct ctl_table hv_root_table[] = {
1302 {
1303 .procname = "kernel",
1304 .mode = 0555,
1305 .child = hv_ctl_table
1306 },
1307 {}
1308};
Vitaly Kuznetsove5132292015-02-27 11:25:51 -08001309
Hank Janssen3e189512010-03-04 22:11:00 +00001310/*
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001311 * vmbus_bus_init -Main vmbus driver initialization routine.
1312 *
1313 * Here, we
Lars Lindley0686e4f2010-03-11 23:51:23 +01001314 * - initialize the vmbus driver context
Lars Lindley0686e4f2010-03-11 23:51:23 +01001315 * - invoke the vmbus hv main init routine
Lars Lindley0686e4f2010-03-11 23:51:23 +01001316 * - retrieve the channel offers
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001317 */
K. Y. Srinivasanefc26722015-12-14 16:01:46 -08001318static int vmbus_bus_init(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001319{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001320 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001321
Greg Kroah-Hartman6d26e38fa22010-12-02 12:08:08 -08001322 /* Hypervisor initialization...setup hypercall page..etc */
1323 ret = hv_init();
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001324 if (ret != 0) {
Hank Janssen0a466182011-03-29 13:58:47 -07001325 pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
K. Y. Srinivasand6c1c5d2011-06-06 15:50:08 -07001326 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001327 }
1328
K. Y. Srinivasan9adcac52011-04-29 13:45:08 -07001329 ret = bus_register(&hv_bus);
K. Y. Srinivasand6c1c5d2011-06-06 15:50:08 -07001330 if (ret)
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -07001331 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001332
Thomas Gleixner76d388c2014-03-05 13:42:14 +01001333 hv_setup_vmbus_irq(vmbus_isr);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001334
Jason Wang2608fb62013-06-19 11:28:10 +08001335 ret = hv_synic_alloc();
1336 if (ret)
1337 goto err_alloc;
Michael Kelleyfd1fea62019-07-01 04:25:56 +00001338
1339 ret = hv_stimer_alloc(VMBUS_MESSAGE_SINT);
1340 if (ret < 0)
1341 goto err_alloc;
1342
K. Y. Srinivasan800b6902011-03-15 15:03:33 -07001343 /*
Michael Kelleyfd1fea62019-07-01 04:25:56 +00001344 * Initialize the per-cpu interrupt state and stimer state.
1345 * Then connect to the host.
K. Y. Srinivasan800b6902011-03-15 15:03:33 -07001346 */
Michael Kelley4a5f3cd2017-12-22 11:19:02 -07001347 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hyperv/vmbus:online",
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -08001348 hv_synic_init, hv_synic_cleanup);
1349 if (ret < 0)
Michael Kelleyfd1fea62019-07-01 04:25:56 +00001350 goto err_cpuhp;
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -08001351 hyperv_cpuhp_online = ret;
1352
K. Y. Srinivasan800b6902011-03-15 15:03:33 -07001353 ret = vmbus_connect();
K. Y. Srinivasan8b9987e92011-08-31 14:35:55 -07001354 if (ret)
Andrey Smetanin17efbee2015-12-14 16:01:38 -08001355 goto err_connect;
K. Y. Srinivasan800b6902011-03-15 15:03:33 -07001356
Nick Meier96c1d052015-02-28 11:39:01 -08001357 /*
1358 * Only register if the crash MSRs are available
1359 */
Denis V. Lunevcc2dd402015-08-01 16:08:20 -07001360 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001361 u64 hyperv_crash_ctl;
1362 /*
1363 * Sysctl registration is not fatal, since by default
1364 * reporting is enabled.
1365 */
1366 hv_ctl_table_hdr = register_sysctl_table(hv_root_table);
1367 if (!hv_ctl_table_hdr)
1368 pr_err("Hyper-V: sysctl table register error");
1369
1370 /*
1371 * Register for panic kmsg callback only if the right
1372 * capability is supported by the hypervisor.
1373 */
Sunil Muthuswamy9d9c9652018-07-28 21:58:47 +00001374 hv_get_crash_ctl(hyperv_crash_ctl);
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001375 if (hyperv_crash_ctl & HV_CRASH_CTL_CRASH_NOTIFY_MSG) {
1376 hv_panic_page = (void *)get_zeroed_page(GFP_KERNEL);
1377 if (hv_panic_page) {
1378 ret = kmsg_dump_register(&hv_kmsg_dumper);
1379 if (ret)
1380 pr_err("Hyper-V: kmsg dump register "
1381 "error 0x%x\n", ret);
1382 } else
1383 pr_err("Hyper-V: panic message page memory "
1384 "allocation failed");
1385 }
1386
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -07001387 register_die_notifier(&hyperv_die_block);
Nick Meier96c1d052015-02-28 11:39:01 -08001388 atomic_notifier_chain_register(&panic_notifier_list,
1389 &hyperv_panic_block);
1390 }
1391
Greg Kroah-Hartman2d6e8822010-12-02 08:50:58 -08001392 vmbus_request_offers();
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +00001393
K. Y. Srinivasand6c1c5d2011-06-06 15:50:08 -07001394 return 0;
K. Y. Srinivasan8b9987e92011-08-31 14:35:55 -07001395
Andrey Smetanin17efbee2015-12-14 16:01:38 -08001396err_connect:
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -08001397 cpuhp_remove_state(hyperv_cpuhp_online);
Michael Kelleyfd1fea62019-07-01 04:25:56 +00001398err_cpuhp:
1399 hv_stimer_free();
Jason Wang2608fb62013-06-19 11:28:10 +08001400err_alloc:
1401 hv_synic_free();
Thomas Gleixner76d388c2014-03-05 13:42:14 +01001402 hv_remove_vmbus_irq();
K. Y. Srinivasan8b9987e92011-08-31 14:35:55 -07001403
K. Y. Srinivasan8b9987e92011-08-31 14:35:55 -07001404 bus_unregister(&hv_bus);
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001405 free_page((unsigned long)hv_panic_page);
Sunil Muthuswamy8afc06d2018-07-28 21:58:46 +00001406 unregister_sysctl_table(hv_ctl_table_hdr);
1407 hv_ctl_table_hdr = NULL;
K. Y. Srinivasan8b9987e92011-08-31 14:35:55 -07001408 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001409}
1410
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001411/**
Jake Oshins35464482015-08-05 00:52:37 -07001412 * __vmbus_child_driver_register() - Register a vmbus's driver
1413 * @hv_driver: Pointer to driver structure you want to register
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001414 * @owner: owner module of the drv
1415 * @mod_name: module name string
Hank Janssen3e189512010-03-04 22:11:00 +00001416 *
1417 * Registers the given driver with Linux through the 'driver_register()' call
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001418 * and sets up the hyper-v vmbus handling for this driver.
Hank Janssen3e189512010-03-04 22:11:00 +00001419 * It will return the state of the 'driver_register()' call.
1420 *
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001421 */
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001422int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001423{
Bill Pemberton5d48a1c2009-07-27 16:47:36 -04001424 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001425
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001426 pr_info("registering driver %s\n", hv_driver->name);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001427
K. Y. Srinivasancf6a2ea2011-12-01 09:59:34 -08001428 ret = vmbus_exists();
1429 if (ret < 0)
1430 return ret;
1431
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001432 hv_driver->driver.name = hv_driver->name;
1433 hv_driver->driver.owner = owner;
1434 hv_driver->driver.mod_name = mod_name;
1435 hv_driver->driver.bus = &hv_bus;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001436
Stephen Hemmingerfc769362016-12-03 12:34:39 -08001437 spin_lock_init(&hv_driver->dynids.lock);
1438 INIT_LIST_HEAD(&hv_driver->dynids.list);
1439
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001440 ret = driver_register(&hv_driver->driver);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001441
Bill Pemberton5d48a1c2009-07-27 16:47:36 -04001442 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001443}
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001444EXPORT_SYMBOL_GPL(__vmbus_driver_register);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001445
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001446/**
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001447 * vmbus_driver_unregister() - Unregister a vmbus's driver
Jake Oshins35464482015-08-05 00:52:37 -07001448 * @hv_driver: Pointer to driver structure you want to
1449 * un-register
Hank Janssen3e189512010-03-04 22:11:00 +00001450 *
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001451 * Un-register the given driver that was previous registered with a call to
1452 * vmbus_driver_register()
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001453 */
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001454void vmbus_driver_unregister(struct hv_driver *hv_driver)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001455{
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001456 pr_info("unregistering driver %s\n", hv_driver->name);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001457
Stephen Hemmingerfc769362016-12-03 12:34:39 -08001458 if (!vmbus_exists()) {
K. Y. Srinivasan8f257a12011-12-27 13:49:37 -08001459 driver_unregister(&hv_driver->driver);
Stephen Hemmingerfc769362016-12-03 12:34:39 -08001460 vmbus_free_dynids(hv_driver);
1461 }
Hank Janssen3e7ee492009-07-13 16:02:34 -07001462}
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001463EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001464
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001465
1466/*
1467 * Called when last reference to channel is gone.
1468 */
1469static void vmbus_chan_release(struct kobject *kobj)
1470{
1471 struct vmbus_channel *channel
1472 = container_of(kobj, struct vmbus_channel, kobj);
1473
1474 kfree_rcu(channel, rcu);
1475}
1476
1477struct vmbus_chan_attribute {
1478 struct attribute attr;
Kimberly Brown14948e32019-03-14 16:05:15 -04001479 ssize_t (*show)(struct vmbus_channel *chan, char *buf);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001480 ssize_t (*store)(struct vmbus_channel *chan,
1481 const char *buf, size_t count);
1482};
1483#define VMBUS_CHAN_ATTR(_name, _mode, _show, _store) \
1484 struct vmbus_chan_attribute chan_attr_##_name \
1485 = __ATTR(_name, _mode, _show, _store)
1486#define VMBUS_CHAN_ATTR_RW(_name) \
1487 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RW(_name)
1488#define VMBUS_CHAN_ATTR_RO(_name) \
1489 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RO(_name)
1490#define VMBUS_CHAN_ATTR_WO(_name) \
1491 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_WO(_name)
1492
1493static ssize_t vmbus_chan_attr_show(struct kobject *kobj,
1494 struct attribute *attr, char *buf)
1495{
1496 const struct vmbus_chan_attribute *attribute
1497 = container_of(attr, struct vmbus_chan_attribute, attr);
Kimberly Brown14948e32019-03-14 16:05:15 -04001498 struct vmbus_channel *chan
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001499 = container_of(kobj, struct vmbus_channel, kobj);
1500
1501 if (!attribute->show)
1502 return -EIO;
1503
1504 return attribute->show(chan, buf);
1505}
1506
1507static const struct sysfs_ops vmbus_chan_sysfs_ops = {
1508 .show = vmbus_chan_attr_show,
1509};
1510
Kimberly Brown14948e32019-03-14 16:05:15 -04001511static ssize_t out_mask_show(struct vmbus_channel *channel, char *buf)
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001512{
Kimberly Brown14948e32019-03-14 16:05:15 -04001513 struct hv_ring_buffer_info *rbi = &channel->outbound;
1514 ssize_t ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001515
Kimberly Brown14948e32019-03-14 16:05:15 -04001516 mutex_lock(&rbi->ring_buffer_mutex);
1517 if (!rbi->ring_buffer) {
1518 mutex_unlock(&rbi->ring_buffer_mutex);
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001519 return -EINVAL;
Kimberly Brown14948e32019-03-14 16:05:15 -04001520 }
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001521
Kimberly Brown14948e32019-03-14 16:05:15 -04001522 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1523 mutex_unlock(&rbi->ring_buffer_mutex);
1524 return ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001525}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001526static VMBUS_CHAN_ATTR_RO(out_mask);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001527
Kimberly Brown14948e32019-03-14 16:05:15 -04001528static ssize_t in_mask_show(struct vmbus_channel *channel, char *buf)
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001529{
Kimberly Brown14948e32019-03-14 16:05:15 -04001530 struct hv_ring_buffer_info *rbi = &channel->inbound;
1531 ssize_t ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001532
Kimberly Brown14948e32019-03-14 16:05:15 -04001533 mutex_lock(&rbi->ring_buffer_mutex);
1534 if (!rbi->ring_buffer) {
1535 mutex_unlock(&rbi->ring_buffer_mutex);
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001536 return -EINVAL;
Kimberly Brown14948e32019-03-14 16:05:15 -04001537 }
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001538
Kimberly Brown14948e32019-03-14 16:05:15 -04001539 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1540 mutex_unlock(&rbi->ring_buffer_mutex);
1541 return ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001542}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001543static VMBUS_CHAN_ATTR_RO(in_mask);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001544
Kimberly Brown14948e32019-03-14 16:05:15 -04001545static ssize_t read_avail_show(struct vmbus_channel *channel, char *buf)
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001546{
Kimberly Brown14948e32019-03-14 16:05:15 -04001547 struct hv_ring_buffer_info *rbi = &channel->inbound;
1548 ssize_t ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001549
Kimberly Brown14948e32019-03-14 16:05:15 -04001550 mutex_lock(&rbi->ring_buffer_mutex);
1551 if (!rbi->ring_buffer) {
1552 mutex_unlock(&rbi->ring_buffer_mutex);
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001553 return -EINVAL;
Kimberly Brown14948e32019-03-14 16:05:15 -04001554 }
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001555
Kimberly Brown14948e32019-03-14 16:05:15 -04001556 ret = sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi));
1557 mutex_unlock(&rbi->ring_buffer_mutex);
1558 return ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001559}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001560static VMBUS_CHAN_ATTR_RO(read_avail);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001561
Kimberly Brown14948e32019-03-14 16:05:15 -04001562static ssize_t write_avail_show(struct vmbus_channel *channel, char *buf)
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001563{
Kimberly Brown14948e32019-03-14 16:05:15 -04001564 struct hv_ring_buffer_info *rbi = &channel->outbound;
1565 ssize_t ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001566
Kimberly Brown14948e32019-03-14 16:05:15 -04001567 mutex_lock(&rbi->ring_buffer_mutex);
1568 if (!rbi->ring_buffer) {
1569 mutex_unlock(&rbi->ring_buffer_mutex);
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001570 return -EINVAL;
Kimberly Brown14948e32019-03-14 16:05:15 -04001571 }
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001572
Kimberly Brown14948e32019-03-14 16:05:15 -04001573 ret = sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi));
1574 mutex_unlock(&rbi->ring_buffer_mutex);
1575 return ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001576}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001577static VMBUS_CHAN_ATTR_RO(write_avail);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001578
Kimberly Brown14948e32019-03-14 16:05:15 -04001579static ssize_t show_target_cpu(struct vmbus_channel *channel, char *buf)
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001580{
1581 return sprintf(buf, "%u\n", channel->target_cpu);
1582}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001583static VMBUS_CHAN_ATTR(cpu, S_IRUGO, show_target_cpu, NULL);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001584
Kimberly Brown14948e32019-03-14 16:05:15 -04001585static ssize_t channel_pending_show(struct vmbus_channel *channel,
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001586 char *buf)
1587{
1588 return sprintf(buf, "%d\n",
1589 channel_pending(channel,
1590 vmbus_connection.monitor_pages[1]));
1591}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001592static VMBUS_CHAN_ATTR(pending, S_IRUGO, channel_pending_show, NULL);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001593
Kimberly Brown14948e32019-03-14 16:05:15 -04001594static ssize_t channel_latency_show(struct vmbus_channel *channel,
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001595 char *buf)
1596{
1597 return sprintf(buf, "%d\n",
1598 channel_latency(channel,
1599 vmbus_connection.monitor_pages[1]));
1600}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001601static VMBUS_CHAN_ATTR(latency, S_IRUGO, channel_latency_show, NULL);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001602
Kimberly Brown14948e32019-03-14 16:05:15 -04001603static ssize_t channel_interrupts_show(struct vmbus_channel *channel, char *buf)
Stephen Hemminger6981fbf2017-10-29 11:33:40 -07001604{
1605 return sprintf(buf, "%llu\n", channel->interrupts);
1606}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001607static VMBUS_CHAN_ATTR(interrupts, S_IRUGO, channel_interrupts_show, NULL);
Stephen Hemminger6981fbf2017-10-29 11:33:40 -07001608
Kimberly Brown14948e32019-03-14 16:05:15 -04001609static ssize_t channel_events_show(struct vmbus_channel *channel, char *buf)
Stephen Hemminger6981fbf2017-10-29 11:33:40 -07001610{
1611 return sprintf(buf, "%llu\n", channel->sig_events);
1612}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001613static VMBUS_CHAN_ATTR(events, S_IRUGO, channel_events_show, NULL);
Stephen Hemminger6981fbf2017-10-29 11:33:40 -07001614
Kimberly Brown14948e32019-03-14 16:05:15 -04001615static ssize_t channel_intr_in_full_show(struct vmbus_channel *channel,
Kimberly Brown396ae572019-02-04 02:13:09 -05001616 char *buf)
1617{
1618 return sprintf(buf, "%llu\n",
1619 (unsigned long long)channel->intr_in_full);
1620}
1621static VMBUS_CHAN_ATTR(intr_in_full, 0444, channel_intr_in_full_show, NULL);
1622
Kimberly Brown14948e32019-03-14 16:05:15 -04001623static ssize_t channel_intr_out_empty_show(struct vmbus_channel *channel,
Kimberly Brown396ae572019-02-04 02:13:09 -05001624 char *buf)
1625{
1626 return sprintf(buf, "%llu\n",
1627 (unsigned long long)channel->intr_out_empty);
1628}
1629static VMBUS_CHAN_ATTR(intr_out_empty, 0444, channel_intr_out_empty_show, NULL);
1630
Kimberly Brown14948e32019-03-14 16:05:15 -04001631static ssize_t channel_out_full_first_show(struct vmbus_channel *channel,
Kimberly Brown396ae572019-02-04 02:13:09 -05001632 char *buf)
1633{
1634 return sprintf(buf, "%llu\n",
1635 (unsigned long long)channel->out_full_first);
1636}
1637static VMBUS_CHAN_ATTR(out_full_first, 0444, channel_out_full_first_show, NULL);
1638
Kimberly Brown14948e32019-03-14 16:05:15 -04001639static ssize_t channel_out_full_total_show(struct vmbus_channel *channel,
Kimberly Brown396ae572019-02-04 02:13:09 -05001640 char *buf)
1641{
1642 return sprintf(buf, "%llu\n",
1643 (unsigned long long)channel->out_full_total);
1644}
1645static VMBUS_CHAN_ATTR(out_full_total, 0444, channel_out_full_total_show, NULL);
1646
Kimberly Brown14948e32019-03-14 16:05:15 -04001647static ssize_t subchannel_monitor_id_show(struct vmbus_channel *channel,
Stephen Hemmingerf0fa2972018-01-09 10:29:06 -08001648 char *buf)
1649{
1650 return sprintf(buf, "%u\n", channel->offermsg.monitorid);
1651}
1652static VMBUS_CHAN_ATTR(monitor_id, S_IRUGO, subchannel_monitor_id_show, NULL);
1653
Kimberly Brown14948e32019-03-14 16:05:15 -04001654static ssize_t subchannel_id_show(struct vmbus_channel *channel,
Stephen Hemmingerf0fa2972018-01-09 10:29:06 -08001655 char *buf)
1656{
1657 return sprintf(buf, "%u\n",
1658 channel->offermsg.offer.sub_channel_index);
1659}
1660static VMBUS_CHAN_ATTR_RO(subchannel_id);
1661
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001662static struct attribute *vmbus_chan_attrs[] = {
1663 &chan_attr_out_mask.attr,
1664 &chan_attr_in_mask.attr,
1665 &chan_attr_read_avail.attr,
1666 &chan_attr_write_avail.attr,
1667 &chan_attr_cpu.attr,
1668 &chan_attr_pending.attr,
1669 &chan_attr_latency.attr,
Stephen Hemminger6981fbf2017-10-29 11:33:40 -07001670 &chan_attr_interrupts.attr,
1671 &chan_attr_events.attr,
Kimberly Brown396ae572019-02-04 02:13:09 -05001672 &chan_attr_intr_in_full.attr,
1673 &chan_attr_intr_out_empty.attr,
1674 &chan_attr_out_full_first.attr,
1675 &chan_attr_out_full_total.attr,
Stephen Hemmingerf0fa2972018-01-09 10:29:06 -08001676 &chan_attr_monitor_id.attr,
1677 &chan_attr_subchannel_id.attr,
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001678 NULL
1679};
1680
Kimberly Brown46fc1542019-03-19 00:04:01 -04001681/*
1682 * Channel-level attribute_group callback function. Returns the permission for
1683 * each attribute, and returns 0 if an attribute is not visible.
1684 */
1685static umode_t vmbus_chan_attr_is_visible(struct kobject *kobj,
1686 struct attribute *attr, int idx)
1687{
1688 const struct vmbus_channel *channel =
1689 container_of(kobj, struct vmbus_channel, kobj);
1690
1691 /* Hide the monitor attributes if the monitor mechanism is not used. */
1692 if (!channel->offermsg.monitor_allocated &&
1693 (attr == &chan_attr_pending.attr ||
1694 attr == &chan_attr_latency.attr ||
1695 attr == &chan_attr_monitor_id.attr))
1696 return 0;
1697
1698 return attr->mode;
1699}
1700
1701static struct attribute_group vmbus_chan_group = {
1702 .attrs = vmbus_chan_attrs,
1703 .is_visible = vmbus_chan_attr_is_visible
1704};
1705
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001706static struct kobj_type vmbus_chan_ktype = {
1707 .sysfs_ops = &vmbus_chan_sysfs_ops,
1708 .release = vmbus_chan_release,
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001709};
1710
1711/*
1712 * vmbus_add_channel_kobj - setup a sub-directory under device/channels
1713 */
1714int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
1715{
Kimberly Brown46fc1542019-03-19 00:04:01 -04001716 const struct device *device = &dev->device;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001717 struct kobject *kobj = &channel->kobj;
1718 u32 relid = channel->offermsg.child_relid;
1719 int ret;
1720
1721 kobj->kset = dev->channels_kset;
1722 ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
1723 "%u", relid);
1724 if (ret)
1725 return ret;
1726
Kimberly Brown46fc1542019-03-19 00:04:01 -04001727 ret = sysfs_create_group(kobj, &vmbus_chan_group);
1728
1729 if (ret) {
1730 /*
1731 * The calling functions' error handling paths will cleanup the
1732 * empty channel directory.
1733 */
1734 dev_err(device, "Unable to set up channel sysfs files\n");
1735 return ret;
1736 }
1737
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001738 kobject_uevent(kobj, KOBJ_ADD);
1739
1740 return 0;
1741}
1742
Hank Janssen3e189512010-03-04 22:11:00 +00001743/*
Kimberly Brown46fc1542019-03-19 00:04:01 -04001744 * vmbus_remove_channel_attr_group - remove the channel's attribute group
1745 */
1746void vmbus_remove_channel_attr_group(struct vmbus_channel *channel)
1747{
1748 sysfs_remove_group(&channel->kobj, &vmbus_chan_group);
1749}
1750
1751/*
K. Y. Srinivasanf2c73012011-09-08 07:24:12 -07001752 * vmbus_device_create - Creates and registers a new child device
Hank Janssen3e189512010-03-04 22:11:00 +00001753 * on the vmbus.
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001754 */
Andy Shevchenko593db802019-01-10 16:25:32 +02001755struct hv_device *vmbus_device_create(const guid_t *type,
1756 const guid_t *instance,
stephen hemminger1b9d48f2014-06-03 08:38:15 -07001757 struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001758{
Nicolas Palix3d3b5512009-07-28 17:32:53 +02001759 struct hv_device *child_device_obj;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001760
K. Y. Srinivasan6bad88da2011-03-07 13:35:48 -08001761 child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
1762 if (!child_device_obj) {
Hank Janssen0a466182011-03-29 13:58:47 -07001763 pr_err("Unable to allocate device object for child device\n");
Hank Janssen3e7ee492009-07-13 16:02:34 -07001764 return NULL;
1765 }
1766
Greg Kroah-Hartmancae5b842010-10-21 09:05:27 -07001767 child_device_obj->channel = channel;
Andy Shevchenko593db802019-01-10 16:25:32 +02001768 guid_copy(&child_device_obj->dev_type, type);
1769 guid_copy(&child_device_obj->dev_instance, instance);
K. Y. Srinivasan7047f172015-12-25 20:00:30 -08001770 child_device_obj->vendor_id = 0x1414; /* MSFT vendor ID */
Hank Janssen3e7ee492009-07-13 16:02:34 -07001771
Hank Janssen3e7ee492009-07-13 16:02:34 -07001772 return child_device_obj;
1773}
1774
Hank Janssen3e189512010-03-04 22:11:00 +00001775/*
K. Y. Srinivasan227942812011-09-08 07:24:13 -07001776 * vmbus_device_register - Register the child device
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001777 */
K. Y. Srinivasan227942812011-09-08 07:24:13 -07001778int vmbus_device_register(struct hv_device *child_device_obj)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001779{
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001780 struct kobject *kobj = &child_device_obj->device.kobj;
1781 int ret;
K. Y. Srinivasan6bad88da2011-03-07 13:35:48 -08001782
Stephen Hemmingerf6b2db02016-11-01 00:01:59 -07001783 dev_set_name(&child_device_obj->device, "%pUl",
Vitaly Kuznetsovb2948092016-09-16 09:01:17 -07001784 child_device_obj->channel->offermsg.offer.if_instance.b);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001785
K. Y. Srinivasan0bce28b2011-08-27 11:31:39 -07001786 child_device_obj->device.bus = &hv_bus;
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -07001787 child_device_obj->device.parent = &hv_acpi_dev->dev;
K. Y. Srinivasan6bad88da2011-03-07 13:35:48 -08001788 child_device_obj->device.release = vmbus_device_release;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001789
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001790 /*
1791 * Register with the LDM. This will kick off the driver/device
1792 * binding...which will eventually call vmbus_match() and vmbus_probe()
1793 */
K. Y. Srinivasan6bad88da2011-03-07 13:35:48 -08001794 ret = device_register(&child_device_obj->device);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001795 if (ret) {
Hank Janssen0a466182011-03-29 13:58:47 -07001796 pr_err("Unable to register child device\n");
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001797 return ret;
1798 }
Hank Janssen3e7ee492009-07-13 16:02:34 -07001799
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001800 child_device_obj->channels_kset = kset_create_and_add("channels",
1801 NULL, kobj);
1802 if (!child_device_obj->channels_kset) {
1803 ret = -ENOMEM;
1804 goto err_dev_unregister;
1805 }
1806
1807 ret = vmbus_add_channel_kobj(child_device_obj,
1808 child_device_obj->channel);
1809 if (ret) {
1810 pr_err("Unable to register primary channeln");
1811 goto err_kset_unregister;
1812 }
1813
1814 return 0;
1815
1816err_kset_unregister:
1817 kset_unregister(child_device_obj->channels_kset);
1818
1819err_dev_unregister:
1820 device_unregister(&child_device_obj->device);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001821 return ret;
1822}
1823
Hank Janssen3e189512010-03-04 22:11:00 +00001824/*
K. Y. Srinivasan696453b2011-09-08 07:24:14 -07001825 * vmbus_device_unregister - Remove the specified child device
Hank Janssen3e189512010-03-04 22:11:00 +00001826 * from the vmbus.
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001827 */
K. Y. Srinivasan696453b2011-09-08 07:24:14 -07001828void vmbus_device_unregister(struct hv_device *device_obj)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001829{
Fernando Soto84672362013-06-14 23:13:35 +00001830 pr_debug("child device %s unregistered\n",
1831 dev_name(&device_obj->device));
1832
Dexuan Cui869b5562017-11-14 06:53:32 -07001833 kset_unregister(device_obj->channels_kset);
1834
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001835 /*
1836 * Kick off the process of unregistering the device.
1837 * This will call vmbus_remove() and eventually vmbus_device_release()
1838 */
K. Y. Srinivasan6bad88da2011-03-07 13:35:48 -08001839 device_unregister(&device_obj->device);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001840}
1841
Hank Janssen3e7ee492009-07-13 16:02:34 -07001842
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001843/*
Jake Oshins7f163a62015-08-05 00:52:36 -07001844 * VMBUS is an acpi enumerated device. Get the information we
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08001845 * need from DSDT.
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001846 */
Jake Oshins7f163a62015-08-05 00:52:36 -07001847#define VTPM_BASE_ADDRESS 0xfed40000
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08001848static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001849{
Jake Oshins7f163a62015-08-05 00:52:36 -07001850 resource_size_t start = 0;
1851 resource_size_t end = 0;
1852 struct resource *new_res;
1853 struct resource **old_res = &hyperv_mmio;
1854 struct resource **prev_res = NULL;
1855
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08001856 switch (res->type) {
Jake Oshins7f163a62015-08-05 00:52:36 -07001857
1858 /*
1859 * "Address" descriptors are for bus windows. Ignore
1860 * "memory" descriptors, which are for registers on
1861 * devices.
1862 */
1863 case ACPI_RESOURCE_TYPE_ADDRESS32:
1864 start = res->data.address32.address.minimum;
1865 end = res->data.address32.address.maximum;
Gerd Hoffmann4eb923f2014-02-24 14:17:08 +01001866 break;
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001867
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08001868 case ACPI_RESOURCE_TYPE_ADDRESS64:
Jake Oshins7f163a62015-08-05 00:52:36 -07001869 start = res->data.address64.address.minimum;
1870 end = res->data.address64.address.maximum;
Gerd Hoffmann4eb923f2014-02-24 14:17:08 +01001871 break;
Jake Oshins7f163a62015-08-05 00:52:36 -07001872
1873 default:
1874 /* Unused resource type */
1875 return AE_OK;
1876
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001877 }
Jake Oshins7f163a62015-08-05 00:52:36 -07001878 /*
1879 * Ignore ranges that are below 1MB, as they're not
1880 * necessary or useful here.
1881 */
1882 if (end < 0x100000)
1883 return AE_OK;
1884
1885 new_res = kzalloc(sizeof(*new_res), GFP_ATOMIC);
1886 if (!new_res)
1887 return AE_NO_MEMORY;
1888
1889 /* If this range overlaps the virtual TPM, truncate it. */
1890 if (end > VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS)
1891 end = VTPM_BASE_ADDRESS;
1892
1893 new_res->name = "hyperv mmio";
1894 new_res->flags = IORESOURCE_MEM;
1895 new_res->start = start;
1896 new_res->end = end;
1897
Jake Oshins40f26f32015-12-14 16:01:52 -08001898 /*
Jake Oshins40f26f32015-12-14 16:01:52 -08001899 * If two ranges are adjacent, merge them.
1900 */
Jake Oshins7f163a62015-08-05 00:52:36 -07001901 do {
1902 if (!*old_res) {
1903 *old_res = new_res;
1904 break;
1905 }
1906
Jake Oshins40f26f32015-12-14 16:01:52 -08001907 if (((*old_res)->end + 1) == new_res->start) {
1908 (*old_res)->end = new_res->end;
1909 kfree(new_res);
1910 break;
1911 }
1912
1913 if ((*old_res)->start == new_res->end + 1) {
1914 (*old_res)->start = new_res->start;
1915 kfree(new_res);
1916 break;
1917 }
1918
Jake Oshins23a06832016-04-05 10:22:53 -07001919 if ((*old_res)->start > new_res->end) {
Jake Oshins7f163a62015-08-05 00:52:36 -07001920 new_res->sibling = *old_res;
1921 if (prev_res)
1922 (*prev_res)->sibling = new_res;
1923 *old_res = new_res;
1924 break;
1925 }
1926
1927 prev_res = old_res;
1928 old_res = &(*old_res)->sibling;
1929
1930 } while (1);
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001931
1932 return AE_OK;
1933}
1934
Jake Oshins7f163a62015-08-05 00:52:36 -07001935static int vmbus_acpi_remove(struct acpi_device *device)
1936{
1937 struct resource *cur_res;
1938 struct resource *next_res;
1939
1940 if (hyperv_mmio) {
Jake Oshins6d146ae2016-04-05 10:22:55 -07001941 if (fb_mmio) {
1942 __release_region(hyperv_mmio, fb_mmio->start,
1943 resource_size(fb_mmio));
1944 fb_mmio = NULL;
1945 }
1946
Jake Oshins7f163a62015-08-05 00:52:36 -07001947 for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) {
1948 next_res = cur_res->sibling;
1949 kfree(cur_res);
1950 }
1951 }
1952
1953 return 0;
1954}
1955
Jake Oshins6d146ae2016-04-05 10:22:55 -07001956static void vmbus_reserve_fb(void)
1957{
1958 int size;
1959 /*
1960 * Make a claim for the frame buffer in the resource tree under the
1961 * first node, which will be the one below 4GB. The length seems to
1962 * be underreported, particularly in a Generation 1 VM. So start out
1963 * reserving a larger area and make it smaller until it succeeds.
1964 */
1965
1966 if (screen_info.lfb_base) {
1967 if (efi_enabled(EFI_BOOT))
1968 size = max_t(__u32, screen_info.lfb_size, 0x800000);
1969 else
1970 size = max_t(__u32, screen_info.lfb_size, 0x4000000);
1971
1972 for (; !fb_mmio && (size >= 0x100000); size >>= 1) {
1973 fb_mmio = __request_region(hyperv_mmio,
1974 screen_info.lfb_base, size,
1975 fb_mmio_name, 0);
1976 }
1977 }
1978}
1979
Jake Oshins35464482015-08-05 00:52:37 -07001980/**
1981 * vmbus_allocate_mmio() - Pick a memory-mapped I/O range.
1982 * @new: If successful, supplied a pointer to the
1983 * allocated MMIO space.
1984 * @device_obj: Identifies the caller
1985 * @min: Minimum guest physical address of the
1986 * allocation
1987 * @max: Maximum guest physical address
1988 * @size: Size of the range to be allocated
1989 * @align: Alignment of the range to be allocated
1990 * @fb_overlap_ok: Whether this allocation can be allowed
1991 * to overlap the video frame buffer.
1992 *
1993 * This function walks the resources granted to VMBus by the
1994 * _CRS object in the ACPI namespace underneath the parent
1995 * "bridge" whether that's a root PCI bus in the Generation 1
1996 * case or a Module Device in the Generation 2 case. It then
1997 * attempts to allocate from the global MMIO pool in a way that
1998 * matches the constraints supplied in these parameters and by
1999 * that _CRS.
2000 *
2001 * Return: 0 on success, -errno on failure
2002 */
2003int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
2004 resource_size_t min, resource_size_t max,
2005 resource_size_t size, resource_size_t align,
2006 bool fb_overlap_ok)
2007{
Jake Oshinsbe000f92016-04-05 10:22:54 -07002008 struct resource *iter, *shadow;
Jake Oshinsea37a6b2016-04-05 10:22:56 -07002009 resource_size_t range_min, range_max, start;
Jake Oshins35464482015-08-05 00:52:37 -07002010 const char *dev_n = dev_name(&device_obj->device);
Jake Oshinsea37a6b2016-04-05 10:22:56 -07002011 int retval;
Jake Oshinse16dad62016-04-05 10:22:50 -07002012
2013 retval = -ENXIO;
2014 down(&hyperv_mmio_lock);
Jake Oshins35464482015-08-05 00:52:37 -07002015
Jake Oshinsea37a6b2016-04-05 10:22:56 -07002016 /*
2017 * If overlaps with frame buffers are allowed, then first attempt to
2018 * make the allocation from within the reserved region. Because it
2019 * is already reserved, no shadow allocation is necessary.
2020 */
2021 if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) &&
2022 !(max < fb_mmio->start)) {
2023
2024 range_min = fb_mmio->start;
2025 range_max = fb_mmio->end;
2026 start = (range_min + align - 1) & ~(align - 1);
2027 for (; start + size - 1 <= range_max; start += align) {
2028 *new = request_mem_region_exclusive(start, size, dev_n);
2029 if (*new) {
2030 retval = 0;
2031 goto exit;
2032 }
2033 }
2034 }
2035
Jake Oshins35464482015-08-05 00:52:37 -07002036 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
2037 if ((iter->start >= max) || (iter->end <= min))
2038 continue;
2039
2040 range_min = iter->start;
2041 range_max = iter->end;
Jake Oshinsea37a6b2016-04-05 10:22:56 -07002042 start = (range_min + align - 1) & ~(align - 1);
2043 for (; start + size - 1 <= range_max; start += align) {
2044 shadow = __request_region(iter, start, size, NULL,
2045 IORESOURCE_BUSY);
2046 if (!shadow)
2047 continue;
Jake Oshins35464482015-08-05 00:52:37 -07002048
Jake Oshinsea37a6b2016-04-05 10:22:56 -07002049 *new = request_mem_region_exclusive(start, size, dev_n);
2050 if (*new) {
2051 shadow->name = (char *)*new;
2052 retval = 0;
2053 goto exit;
Jake Oshins35464482015-08-05 00:52:37 -07002054 }
2055
Jake Oshinsea37a6b2016-04-05 10:22:56 -07002056 __release_region(iter, start, size);
Jake Oshins35464482015-08-05 00:52:37 -07002057 }
2058 }
2059
Jake Oshinse16dad62016-04-05 10:22:50 -07002060exit:
2061 up(&hyperv_mmio_lock);
2062 return retval;
Jake Oshins35464482015-08-05 00:52:37 -07002063}
2064EXPORT_SYMBOL_GPL(vmbus_allocate_mmio);
2065
Jake Oshins619848b2015-12-14 16:01:39 -08002066/**
Jake Oshins97fb77dc2016-04-05 10:22:51 -07002067 * vmbus_free_mmio() - Free a memory-mapped I/O range.
2068 * @start: Base address of region to release.
2069 * @size: Size of the range to be allocated
2070 *
2071 * This function releases anything requested by
2072 * vmbus_mmio_allocate().
2073 */
2074void vmbus_free_mmio(resource_size_t start, resource_size_t size)
2075{
Jake Oshinsbe000f92016-04-05 10:22:54 -07002076 struct resource *iter;
2077
2078 down(&hyperv_mmio_lock);
2079 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
2080 if ((iter->start >= start + size) || (iter->end <= start))
2081 continue;
2082
2083 __release_region(iter, start, size);
2084 }
Jake Oshins97fb77dc2016-04-05 10:22:51 -07002085 release_mem_region(start, size);
Jake Oshinsbe000f92016-04-05 10:22:54 -07002086 up(&hyperv_mmio_lock);
Jake Oshins97fb77dc2016-04-05 10:22:51 -07002087
2088}
2089EXPORT_SYMBOL_GPL(vmbus_free_mmio);
2090
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002091static int vmbus_acpi_add(struct acpi_device *device)
2092{
2093 acpi_status result;
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002094 int ret_val = -ENODEV;
Jake Oshins7f163a62015-08-05 00:52:36 -07002095 struct acpi_device *ancestor;
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002096
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -07002097 hv_acpi_dev = device;
2098
K. Y. Srinivasan0a4425b2011-08-27 11:31:38 -07002099 result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002100 vmbus_walk_resources, NULL);
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002101
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002102 if (ACPI_FAILURE(result))
2103 goto acpi_walk_err;
2104 /*
Jake Oshins7f163a62015-08-05 00:52:36 -07002105 * Some ancestor of the vmbus acpi device (Gen1 or Gen2
2106 * firmware) is the VMOD that has the mmio ranges. Get that.
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002107 */
Jake Oshins7f163a62015-08-05 00:52:36 -07002108 for (ancestor = device->parent; ancestor; ancestor = ancestor->parent) {
2109 result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS,
2110 vmbus_walk_resources, NULL);
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002111
2112 if (ACPI_FAILURE(result))
Jake Oshins7f163a62015-08-05 00:52:36 -07002113 continue;
Jake Oshins6d146ae2016-04-05 10:22:55 -07002114 if (hyperv_mmio) {
2115 vmbus_reserve_fb();
Jake Oshins7f163a62015-08-05 00:52:36 -07002116 break;
Jake Oshins6d146ae2016-04-05 10:22:55 -07002117 }
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002118 }
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002119 ret_val = 0;
2120
2121acpi_walk_err:
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002122 complete(&probe_event);
Jake Oshins7f163a62015-08-05 00:52:36 -07002123 if (ret_val)
2124 vmbus_acpi_remove(device);
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002125 return ret_val;
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002126}
2127
Dexuan Cuif53335e2019-09-05 23:01:19 +00002128static int vmbus_bus_suspend(struct device *dev)
2129{
Dexuan Cuib307b382019-09-05 23:01:21 +00002130 struct vmbus_channel *channel, *sc;
2131 unsigned long flags;
Dexuan Cui1f48dcf2019-09-05 23:01:20 +00002132
2133 while (atomic_read(&vmbus_connection.offer_in_progress) != 0) {
2134 /*
2135 * We wait here until the completion of any channel
2136 * offers that are currently in progress.
2137 */
2138 msleep(1);
2139 }
2140
2141 mutex_lock(&vmbus_connection.channel_mutex);
2142 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
2143 if (!is_hvsock_channel(channel))
2144 continue;
2145
2146 vmbus_force_channel_rescinded(channel);
2147 }
2148 mutex_unlock(&vmbus_connection.channel_mutex);
2149
Dexuan Cuib307b382019-09-05 23:01:21 +00002150 /*
2151 * Wait until all the sub-channels and hv_sock channels have been
2152 * cleaned up. Sub-channels should be destroyed upon suspend, otherwise
2153 * they would conflict with the new sub-channels that will be created
2154 * in the resume path. hv_sock channels should also be destroyed, but
2155 * a hv_sock channel of an established hv_sock connection can not be
2156 * really destroyed since it may still be referenced by the userspace
2157 * application, so we just force the hv_sock channel to be rescinded
2158 * by vmbus_force_channel_rescinded(), and the userspace application
2159 * will thoroughly destroy the channel after hibernation.
2160 *
2161 * Note: the counter nr_chan_close_on_suspend may never go above 0 if
2162 * the VM has no sub-channel and hv_sock channel, e.g. a 1-vCPU VM.
2163 */
2164 if (atomic_read(&vmbus_connection.nr_chan_close_on_suspend) > 0)
2165 wait_for_completion(&vmbus_connection.ready_for_suspend_event);
2166
2167 mutex_lock(&vmbus_connection.channel_mutex);
2168
2169 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
2170 if (is_hvsock_channel(channel)) {
2171 if (!channel->rescind) {
2172 pr_err("hv_sock channel not rescinded!\n");
2173 WARN_ON_ONCE(1);
2174 }
2175 continue;
2176 }
2177
2178 spin_lock_irqsave(&channel->lock, flags);
2179 list_for_each_entry(sc, &channel->sc_list, sc_list) {
2180 pr_err("Sub-channel not deleted!\n");
2181 WARN_ON_ONCE(1);
2182 }
2183 spin_unlock_irqrestore(&channel->lock, flags);
2184 }
2185
2186 mutex_unlock(&vmbus_connection.channel_mutex);
2187
Dexuan Cuif53335e2019-09-05 23:01:19 +00002188 vmbus_initiate_unload(false);
2189
2190 vmbus_connection.conn_state = DISCONNECTED;
2191
2192 return 0;
2193}
2194
2195static int vmbus_bus_resume(struct device *dev)
2196{
2197 struct vmbus_channel_msginfo *msginfo;
2198 size_t msgsize;
2199 int ret;
2200
2201 /*
2202 * We only use the 'vmbus_proto_version', which was in use before
2203 * hibernation, to re-negotiate with the host.
2204 */
2205 if (vmbus_proto_version == VERSION_INVAL ||
2206 vmbus_proto_version == 0) {
2207 pr_err("Invalid proto version = 0x%x\n", vmbus_proto_version);
2208 return -EINVAL;
2209 }
2210
2211 msgsize = sizeof(*msginfo) +
2212 sizeof(struct vmbus_channel_initiate_contact);
2213
2214 msginfo = kzalloc(msgsize, GFP_KERNEL);
2215
2216 if (msginfo == NULL)
2217 return -ENOMEM;
2218
2219 ret = vmbus_negotiate_version(msginfo, vmbus_proto_version);
2220
2221 kfree(msginfo);
2222
2223 if (ret != 0)
2224 return ret;
2225
2226 vmbus_request_offers();
2227
Dexuan Cuib307b382019-09-05 23:01:21 +00002228 /* Reset the event for the next suspend. */
2229 reinit_completion(&vmbus_connection.ready_for_suspend_event);
2230
Dexuan Cuif53335e2019-09-05 23:01:19 +00002231 return 0;
2232}
2233
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002234static const struct acpi_device_id vmbus_acpi_device_ids[] = {
2235 {"VMBUS", 0},
K. Y. Srinivasan9d7b18d2011-06-06 15:49:42 -07002236 {"VMBus", 0},
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002237 {"", 0},
2238};
2239MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
2240
Dexuan Cuif53335e2019-09-05 23:01:19 +00002241/*
2242 * Note: we must use SET_NOIRQ_SYSTEM_SLEEP_PM_OPS rather than
2243 * SET_SYSTEM_SLEEP_PM_OPS, otherwise NIC SR-IOV can not work, because the
2244 * "pci_dev_pm_ops" uses the "noirq" callbacks: in the resume path, the
2245 * pci "noirq" restore callback runs before "non-noirq" callbacks (see
2246 * resume_target_kernel() -> dpm_resume_start(), and hibernation_restore() ->
2247 * dpm_resume_end()). This means vmbus_bus_resume() and the pci-hyperv's
2248 * resume callback must also run via the "noirq" callbacks.
2249 */
2250static const struct dev_pm_ops vmbus_bus_pm = {
2251 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(vmbus_bus_suspend, vmbus_bus_resume)
2252};
2253
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002254static struct acpi_driver vmbus_acpi_driver = {
2255 .name = "vmbus",
2256 .ids = vmbus_acpi_device_ids,
2257 .ops = {
2258 .add = vmbus_acpi_add,
Vitaly Kuznetsove4ecb412015-04-22 21:31:28 -07002259 .remove = vmbus_acpi_remove,
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002260 },
Dexuan Cuif53335e2019-09-05 23:01:19 +00002261 .drv.pm = &vmbus_bus_pm,
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002262};
2263
Vitaly Kuznetsov25172812015-08-01 16:08:07 -07002264static void hv_kexec_handler(void)
2265{
Michael Kelleyfd1fea62019-07-01 04:25:56 +00002266 hv_stimer_global_cleanup();
Vitaly Kuznetsov75ff3a82016-02-26 15:13:16 -08002267 vmbus_initiate_unload(false);
Vitaly Kuznetsov523b9402016-12-07 14:53:12 -08002268 vmbus_connection.conn_state = DISCONNECTED;
2269 /* Make sure conn_state is set as hv_synic_cleanup checks for it */
2270 mb();
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -08002271 cpuhp_remove_state(hyperv_cpuhp_online);
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -07002272 hyperv_cleanup();
Vitaly Kuznetsov25172812015-08-01 16:08:07 -07002273};
2274
Vitaly Kuznetsovb4370df2015-08-01 16:08:09 -07002275static void hv_crash_handler(struct pt_regs *regs)
2276{
Michael Kelleyfd1fea62019-07-01 04:25:56 +00002277 int cpu;
2278
Vitaly Kuznetsov75ff3a82016-02-26 15:13:16 -08002279 vmbus_initiate_unload(true);
Vitaly Kuznetsovb4370df2015-08-01 16:08:09 -07002280 /*
2281 * In crash handler we can't schedule synic cleanup for all CPUs,
2282 * doing the cleanup for current CPU only. This should be sufficient
2283 * for kdump.
2284 */
Vitaly Kuznetsov523b9402016-12-07 14:53:12 -08002285 vmbus_connection.conn_state = DISCONNECTED;
Michael Kelleyfd1fea62019-07-01 04:25:56 +00002286 cpu = smp_processor_id();
2287 hv_stimer_cleanup(cpu);
2288 hv_synic_cleanup(cpu);
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -07002289 hyperv_cleanup();
Vitaly Kuznetsovb4370df2015-08-01 16:08:09 -07002290};
2291
Dexuan Cui63ecc6d2019-09-05 23:01:16 +00002292static int hv_synic_suspend(void)
2293{
2294 /*
2295 * When we reach here, all the non-boot CPUs have been offlined, and
2296 * the stimers on them have been unbound in hv_synic_cleanup() ->
2297 * hv_stimer_cleanup() -> clockevents_unbind_device().
2298 *
2299 * hv_synic_suspend() only runs on CPU0 with interrupts disabled. Here
2300 * we do not unbind the stimer on CPU0 because: 1) it's unnecessary
2301 * because the interrupts remain disabled between syscore_suspend()
2302 * and syscore_resume(): see create_image() and resume_target_kernel();
2303 * 2) the stimer on CPU0 is automatically disabled later by
2304 * syscore_suspend() -> timekeeping_suspend() -> tick_suspend() -> ...
2305 * -> clockevents_shutdown() -> ... -> hv_ce_shutdown(); 3) a warning
2306 * would be triggered if we call clockevents_unbind_device(), which
2307 * may sleep, in an interrupts-disabled context. So, we intentionally
2308 * don't call hv_stimer_cleanup(0) here.
2309 */
2310
2311 hv_synic_disable_regs(0);
2312
2313 return 0;
2314}
2315
2316static void hv_synic_resume(void)
2317{
2318 hv_synic_enable_regs(0);
2319
2320 /*
2321 * Note: we don't need to call hv_stimer_init(0), because the timer
2322 * on CPU0 is not unbound in hv_synic_suspend(), and the timer is
2323 * automatically re-enabled in timekeeping_resume().
2324 */
2325}
2326
2327/* The callbacks run only on CPU0, with irqs_disabled. */
2328static struct syscore_ops hv_synic_syscore_ops = {
2329 .suspend = hv_synic_suspend,
2330 .resume = hv_synic_resume,
2331};
2332
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -07002333static int __init hv_acpi_init(void)
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -07002334{
K. Y. Srinivasan2dda95f2011-07-15 13:38:56 -07002335 int ret, t;
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002336
Michael Kelley4a5f3cd2017-12-22 11:19:02 -07002337 if (!hv_is_hyperv_initialized())
Jason Wang05929692012-08-17 18:52:43 +08002338 return -ENODEV;
2339
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002340 init_completion(&probe_event);
2341
2342 /*
K. Y. Srinivasanefc26722015-12-14 16:01:46 -08002343 * Get ACPI resources first.
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002344 */
K. Y. Srinivasan02466042011-06-06 15:49:40 -07002345 ret = acpi_bus_register_driver(&vmbus_acpi_driver);
2346
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002347 if (ret)
2348 return ret;
2349
K. Y. Srinivasan2dda95f2011-07-15 13:38:56 -07002350 t = wait_for_completion_timeout(&probe_event, 5*HZ);
2351 if (t == 0) {
2352 ret = -ETIMEDOUT;
2353 goto cleanup;
2354 }
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002355
K. Y. Srinivasanefc26722015-12-14 16:01:46 -08002356 ret = vmbus_bus_init();
K. Y. Srinivasan91fd7992011-06-16 13:16:38 -07002357 if (ret)
K. Y. Srinivasan2dda95f2011-07-15 13:38:56 -07002358 goto cleanup;
2359
Vitaly Kuznetsov25172812015-08-01 16:08:07 -07002360 hv_setup_kexec_handler(hv_kexec_handler);
Vitaly Kuznetsovb4370df2015-08-01 16:08:09 -07002361 hv_setup_crash_handler(hv_crash_handler);
Vitaly Kuznetsov25172812015-08-01 16:08:07 -07002362
Dexuan Cui63ecc6d2019-09-05 23:01:16 +00002363 register_syscore_ops(&hv_synic_syscore_ops);
2364
K. Y. Srinivasan2dda95f2011-07-15 13:38:56 -07002365 return 0;
2366
2367cleanup:
2368 acpi_bus_unregister_driver(&vmbus_acpi_driver);
K. Y. Srinivasancf6a2ea2011-12-01 09:59:34 -08002369 hv_acpi_dev = NULL;
K. Y. Srinivasan91fd7992011-06-16 13:16:38 -07002370 return ret;
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -07002371}
2372
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08002373static void __exit vmbus_exit(void)
2374{
Vitaly Kuznetsove72e7ac2015-02-27 11:25:55 -08002375 int cpu;
2376
Dexuan Cui63ecc6d2019-09-05 23:01:16 +00002377 unregister_syscore_ops(&hv_synic_syscore_ops);
2378
Vitaly Kuznetsov25172812015-08-01 16:08:07 -07002379 hv_remove_kexec_handler();
Vitaly Kuznetsovb4370df2015-08-01 16:08:09 -07002380 hv_remove_crash_handler();
Vitaly Kuznetsov09a19622015-02-27 11:25:54 -08002381 vmbus_connection.conn_state = DISCONNECTED;
Michael Kelleyfd1fea62019-07-01 04:25:56 +00002382 hv_stimer_global_cleanup();
K. Y. Srinivasan2db84ef2015-04-22 21:31:32 -07002383 vmbus_disconnect();
Thomas Gleixner76d388c2014-03-05 13:42:14 +01002384 hv_remove_vmbus_irq();
Stephen Hemminger37cdd992017-02-11 23:02:19 -07002385 for_each_online_cpu(cpu) {
2386 struct hv_per_cpu_context *hv_cpu
2387 = per_cpu_ptr(hv_context.cpu_context, cpu);
2388
2389 tasklet_kill(&hv_cpu->msg_dpc);
2390 }
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08002391 vmbus_free_channels();
Stephen Hemminger37cdd992017-02-11 23:02:19 -07002392
Denis V. Lunevcc2dd402015-08-01 16:08:20 -07002393 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00002394 kmsg_dump_unregister(&hv_kmsg_dumper);
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -07002395 unregister_die_notifier(&hyperv_die_block);
Vitaly Kuznetsov096c6052015-04-22 21:31:29 -07002396 atomic_notifier_chain_unregister(&panic_notifier_list,
2397 &hyperv_panic_block);
2398 }
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00002399
2400 free_page((unsigned long)hv_panic_page);
Sunil Muthuswamy8afc06d2018-07-28 21:58:46 +00002401 unregister_sysctl_table(hv_ctl_table_hdr);
2402 hv_ctl_table_hdr = NULL;
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08002403 bus_unregister(&hv_bus);
Stephen Hemminger37cdd992017-02-11 23:02:19 -07002404
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -08002405 cpuhp_remove_state(hyperv_cpuhp_online);
Vitaly Kuznetsov06210b42015-08-01 16:08:05 -07002406 hv_synic_free();
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08002407 acpi_bus_unregister_driver(&vmbus_acpi_driver);
2408}
2409
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -07002410
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07002411MODULE_LICENSE("GPL");
Joseph Salisbury674eecb2019-04-23 03:47:27 +00002412MODULE_DESCRIPTION("Microsoft Hyper-V VMBus Driver");
Hank Janssen3e7ee492009-07-13 16:02:34 -07002413
K. Y. Srinivasan43d4e112011-10-24 11:28:12 -07002414subsys_initcall(hv_acpi_init);
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08002415module_exit(vmbus_exit);