blob: 2ef375ce58acd118eafa7fae9014ed00c2be1f7b [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>
Nick Meier96c1d052015-02-28 11:39:01 -080027#include <linux/notifier.h>
28#include <linux/ptrace.h>
Jake Oshins35464482015-08-05 00:52:37 -070029#include <linux/screen_info.h>
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -070030#include <linux/kdebug.h>
Jake Oshins6d146ae2016-04-05 10:22:55 -070031#include <linux/efi.h>
Stephan Mueller4b44f2d2016-05-02 02:14:34 -040032#include <linux/random.h>
Dexuan Cui63ecc6d2019-09-05 23:01:16 +000033#include <linux/syscore_ops.h>
Michael Kelleyfd1fea62019-07-01 04:25:56 +000034#include <clocksource/hyperv_timer.h>
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070035#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070036
Stephen Hemmingerfc769362016-12-03 12:34:39 -080037struct vmbus_dynid {
38 struct list_head node;
39 struct hv_vmbus_device_id id;
40};
41
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -070042static struct acpi_device *hv_acpi_dev;
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -070043
K. Y. Srinivasan71a66552011-04-29 13:45:04 -070044static struct completion probe_event;
Hank Janssen3e7ee492009-07-13 16:02:34 -070045
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -080046static int hyperv_cpuhp_online;
Nick Meier96c1d052015-02-28 11:39:01 -080047
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +000048static void *hv_panic_page;
49
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -070050static int hyperv_panic_event(struct notifier_block *nb, unsigned long val,
51 void *args)
52{
53 struct pt_regs *regs;
54
55 regs = current_pt_regs();
56
K. Y. Srinivasan7ed43252017-10-29 11:33:41 -070057 hyperv_report_panic(regs, val);
Nick Meier96c1d052015-02-28 11:39:01 -080058 return NOTIFY_DONE;
59}
60
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -070061static int hyperv_die_event(struct notifier_block *nb, unsigned long val,
62 void *args)
63{
64 struct die_args *die = (struct die_args *)args;
65 struct pt_regs *regs = die->regs;
66
K. Y. Srinivasan7ed43252017-10-29 11:33:41 -070067 hyperv_report_panic(regs, val);
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -070068 return NOTIFY_DONE;
69}
70
71static struct notifier_block hyperv_die_block = {
72 .notifier_call = hyperv_die_event,
73};
Nick Meier96c1d052015-02-28 11:39:01 -080074static struct notifier_block hyperv_panic_block = {
75 .notifier_call = hyperv_panic_event,
76};
77
Jake Oshins6d146ae2016-04-05 10:22:55 -070078static const char *fb_mmio_name = "fb_range";
79static struct resource *fb_mmio;
Stephen Hemmingere2e80842016-09-07 05:39:33 -070080static struct resource *hyperv_mmio;
81static DEFINE_SEMAPHORE(hyperv_mmio_lock);
Hank Janssen3e7ee492009-07-13 16:02:34 -070082
K. Y. Srinivasancf6a2ea2011-12-01 09:59:34 -080083static int vmbus_exists(void)
84{
85 if (hv_acpi_dev == NULL)
86 return -ENODEV;
87
88 return 0;
89}
90
Olaf Heringfd776ba2011-09-02 18:25:56 +020091#define VMBUS_ALIAS_LEN ((sizeof((struct hv_vmbus_device_id *)0)->guid) * 2)
92static void print_alias_name(struct hv_device *hv_dev, char *alias_name)
93{
94 int i;
95 for (i = 0; i < VMBUS_ALIAS_LEN; i += 2)
96 sprintf(&alias_name[i], "%02x", hv_dev->dev_type.b[i/2]);
97}
98
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -070099static u8 channel_monitor_group(const struct vmbus_channel *channel)
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700100{
101 return (u8)channel->offermsg.monitorid / 32;
102}
103
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -0700104static u8 channel_monitor_offset(const struct vmbus_channel *channel)
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700105{
106 return (u8)channel->offermsg.monitorid % 32;
107}
108
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -0700109static u32 channel_pending(const struct vmbus_channel *channel,
110 const struct hv_monitor_page *monitor_page)
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700111{
112 u8 monitor_group = channel_monitor_group(channel);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -0700113
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700114 return monitor_page->trigger_group[monitor_group].pending;
115}
116
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -0700117static u32 channel_latency(const struct vmbus_channel *channel,
118 const struct hv_monitor_page *monitor_page)
Greg Kroah-Hartman1cee2722013-09-13 11:32:57 -0700119{
120 u8 monitor_group = channel_monitor_group(channel);
121 u8 monitor_offset = channel_monitor_offset(channel);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -0700122
Greg Kroah-Hartman1cee2722013-09-13 11:32:57 -0700123 return monitor_page->latency[monitor_group][monitor_offset];
124}
125
Greg Kroah-Hartman4947c742013-09-13 11:32:58 -0700126static u32 channel_conn_id(struct vmbus_channel *channel,
127 struct hv_monitor_page *monitor_page)
128{
129 u8 monitor_group = channel_monitor_group(channel);
130 u8 monitor_offset = channel_monitor_offset(channel);
131 return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
132}
133
Greg Kroah-Hartman03f3a912013-09-13 11:32:49 -0700134static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
135 char *buf)
136{
137 struct hv_device *hv_dev = device_to_hv_device(dev);
138
139 if (!hv_dev->channel)
140 return -ENODEV;
141 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
142}
143static DEVICE_ATTR_RO(id);
144
Greg Kroah-Hartmana8fb5f32013-09-13 11:32:50 -0700145static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
146 char *buf)
147{
148 struct hv_device *hv_dev = device_to_hv_device(dev);
149
150 if (!hv_dev->channel)
151 return -ENODEV;
152 return sprintf(buf, "%d\n", hv_dev->channel->state);
153}
154static DEVICE_ATTR_RO(state);
155
Greg Kroah-Hartman5ffd00e2013-09-13 11:32:51 -0700156static ssize_t monitor_id_show(struct device *dev,
157 struct device_attribute *dev_attr, char *buf)
158{
159 struct hv_device *hv_dev = device_to_hv_device(dev);
160
161 if (!hv_dev->channel)
162 return -ENODEV;
163 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
164}
165static DEVICE_ATTR_RO(monitor_id);
166
Greg Kroah-Hartman68234c02013-09-13 11:32:53 -0700167static ssize_t class_id_show(struct device *dev,
168 struct device_attribute *dev_attr, char *buf)
169{
170 struct hv_device *hv_dev = device_to_hv_device(dev);
171
172 if (!hv_dev->channel)
173 return -ENODEV;
174 return sprintf(buf, "{%pUl}\n",
175 hv_dev->channel->offermsg.offer.if_type.b);
176}
177static DEVICE_ATTR_RO(class_id);
178
Greg Kroah-Hartman7c55e1d2013-09-13 11:32:54 -0700179static ssize_t device_id_show(struct device *dev,
180 struct device_attribute *dev_attr, char *buf)
181{
182 struct hv_device *hv_dev = device_to_hv_device(dev);
183
184 if (!hv_dev->channel)
185 return -ENODEV;
186 return sprintf(buf, "{%pUl}\n",
187 hv_dev->channel->offermsg.offer.if_instance.b);
188}
189static DEVICE_ATTR_RO(device_id);
190
Greg Kroah-Hartman647fa372013-09-13 11:32:52 -0700191static ssize_t modalias_show(struct device *dev,
192 struct device_attribute *dev_attr, char *buf)
193{
194 struct hv_device *hv_dev = device_to_hv_device(dev);
195 char alias_name[VMBUS_ALIAS_LEN + 1];
196
197 print_alias_name(hv_dev, alias_name);
198 return sprintf(buf, "vmbus:%s\n", alias_name);
199}
200static DEVICE_ATTR_RO(modalias);
201
Stephen Hemminger7ceb1c32018-07-28 21:58:48 +0000202#ifdef CONFIG_NUMA
203static ssize_t numa_node_show(struct device *dev,
204 struct device_attribute *attr, char *buf)
205{
206 struct hv_device *hv_dev = device_to_hv_device(dev);
207
208 if (!hv_dev->channel)
209 return -ENODEV;
210
211 return sprintf(buf, "%d\n", hv_dev->channel->numa_node);
212}
213static DEVICE_ATTR_RO(numa_node);
214#endif
215
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700216static ssize_t server_monitor_pending_show(struct device *dev,
217 struct device_attribute *dev_attr,
218 char *buf)
219{
220 struct hv_device *hv_dev = device_to_hv_device(dev);
221
222 if (!hv_dev->channel)
223 return -ENODEV;
224 return sprintf(buf, "%d\n",
225 channel_pending(hv_dev->channel,
Kimberly Brownfd8e3c32019-02-19 00:38:06 -0500226 vmbus_connection.monitor_pages[0]));
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700227}
228static DEVICE_ATTR_RO(server_monitor_pending);
229
230static ssize_t client_monitor_pending_show(struct device *dev,
231 struct device_attribute *dev_attr,
232 char *buf)
233{
234 struct hv_device *hv_dev = device_to_hv_device(dev);
235
236 if (!hv_dev->channel)
237 return -ENODEV;
238 return sprintf(buf, "%d\n",
239 channel_pending(hv_dev->channel,
240 vmbus_connection.monitor_pages[1]));
241}
242static DEVICE_ATTR_RO(client_monitor_pending);
Greg Kroah-Hartman68234c02013-09-13 11:32:53 -0700243
Greg Kroah-Hartman1cee2722013-09-13 11:32:57 -0700244static ssize_t server_monitor_latency_show(struct device *dev,
245 struct device_attribute *dev_attr,
246 char *buf)
247{
248 struct hv_device *hv_dev = device_to_hv_device(dev);
249
250 if (!hv_dev->channel)
251 return -ENODEV;
252 return sprintf(buf, "%d\n",
253 channel_latency(hv_dev->channel,
254 vmbus_connection.monitor_pages[0]));
255}
256static DEVICE_ATTR_RO(server_monitor_latency);
257
258static ssize_t client_monitor_latency_show(struct device *dev,
259 struct device_attribute *dev_attr,
260 char *buf)
261{
262 struct hv_device *hv_dev = device_to_hv_device(dev);
263
264 if (!hv_dev->channel)
265 return -ENODEV;
266 return sprintf(buf, "%d\n",
267 channel_latency(hv_dev->channel,
268 vmbus_connection.monitor_pages[1]));
269}
270static DEVICE_ATTR_RO(client_monitor_latency);
271
Greg Kroah-Hartman4947c742013-09-13 11:32:58 -0700272static ssize_t server_monitor_conn_id_show(struct device *dev,
273 struct device_attribute *dev_attr,
274 char *buf)
275{
276 struct hv_device *hv_dev = device_to_hv_device(dev);
277
278 if (!hv_dev->channel)
279 return -ENODEV;
280 return sprintf(buf, "%d\n",
281 channel_conn_id(hv_dev->channel,
282 vmbus_connection.monitor_pages[0]));
283}
284static DEVICE_ATTR_RO(server_monitor_conn_id);
285
286static ssize_t client_monitor_conn_id_show(struct device *dev,
287 struct device_attribute *dev_attr,
288 char *buf)
289{
290 struct hv_device *hv_dev = device_to_hv_device(dev);
291
292 if (!hv_dev->channel)
293 return -ENODEV;
294 return sprintf(buf, "%d\n",
295 channel_conn_id(hv_dev->channel,
296 vmbus_connection.monitor_pages[1]));
297}
298static DEVICE_ATTR_RO(client_monitor_conn_id);
299
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700300static ssize_t out_intr_mask_show(struct device *dev,
301 struct device_attribute *dev_attr, char *buf)
302{
303 struct hv_device *hv_dev = device_to_hv_device(dev);
304 struct hv_ring_buffer_debug_info outbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000305 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700306
307 if (!hv_dev->channel)
308 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000309
310 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
311 &outbound);
312 if (ret < 0)
313 return ret;
314
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700315 return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
316}
317static DEVICE_ATTR_RO(out_intr_mask);
318
319static ssize_t out_read_index_show(struct device *dev,
320 struct device_attribute *dev_attr, char *buf)
321{
322 struct hv_device *hv_dev = device_to_hv_device(dev);
323 struct hv_ring_buffer_debug_info outbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000324 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700325
326 if (!hv_dev->channel)
327 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000328
329 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
330 &outbound);
331 if (ret < 0)
332 return ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700333 return sprintf(buf, "%d\n", outbound.current_read_index);
334}
335static DEVICE_ATTR_RO(out_read_index);
336
337static ssize_t out_write_index_show(struct device *dev,
338 struct device_attribute *dev_attr,
339 char *buf)
340{
341 struct hv_device *hv_dev = device_to_hv_device(dev);
342 struct hv_ring_buffer_debug_info outbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000343 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700344
345 if (!hv_dev->channel)
346 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000347
348 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
349 &outbound);
350 if (ret < 0)
351 return ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700352 return sprintf(buf, "%d\n", outbound.current_write_index);
353}
354static DEVICE_ATTR_RO(out_write_index);
355
356static ssize_t out_read_bytes_avail_show(struct device *dev,
357 struct device_attribute *dev_attr,
358 char *buf)
359{
360 struct hv_device *hv_dev = device_to_hv_device(dev);
361 struct hv_ring_buffer_debug_info outbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000362 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700363
364 if (!hv_dev->channel)
365 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000366
367 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
368 &outbound);
369 if (ret < 0)
370 return ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700371 return sprintf(buf, "%d\n", outbound.bytes_avail_toread);
372}
373static DEVICE_ATTR_RO(out_read_bytes_avail);
374
375static ssize_t out_write_bytes_avail_show(struct device *dev,
376 struct device_attribute *dev_attr,
377 char *buf)
378{
379 struct hv_device *hv_dev = device_to_hv_device(dev);
380 struct hv_ring_buffer_debug_info outbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000381 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700382
383 if (!hv_dev->channel)
384 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000385
386 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
387 &outbound);
388 if (ret < 0)
389 return ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700390 return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
391}
392static DEVICE_ATTR_RO(out_write_bytes_avail);
393
394static ssize_t in_intr_mask_show(struct device *dev,
395 struct device_attribute *dev_attr, char *buf)
396{
397 struct hv_device *hv_dev = device_to_hv_device(dev);
398 struct hv_ring_buffer_debug_info inbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000399 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700400
401 if (!hv_dev->channel)
402 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000403
404 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
405 if (ret < 0)
406 return ret;
407
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700408 return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
409}
410static DEVICE_ATTR_RO(in_intr_mask);
411
412static ssize_t in_read_index_show(struct device *dev,
413 struct device_attribute *dev_attr, char *buf)
414{
415 struct hv_device *hv_dev = device_to_hv_device(dev);
416 struct hv_ring_buffer_debug_info inbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000417 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700418
419 if (!hv_dev->channel)
420 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000421
422 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
423 if (ret < 0)
424 return ret;
425
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700426 return sprintf(buf, "%d\n", inbound.current_read_index);
427}
428static DEVICE_ATTR_RO(in_read_index);
429
430static ssize_t in_write_index_show(struct device *dev,
431 struct device_attribute *dev_attr, char *buf)
432{
433 struct hv_device *hv_dev = device_to_hv_device(dev);
434 struct hv_ring_buffer_debug_info inbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000435 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700436
437 if (!hv_dev->channel)
438 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000439
440 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
441 if (ret < 0)
442 return ret;
443
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700444 return sprintf(buf, "%d\n", inbound.current_write_index);
445}
446static DEVICE_ATTR_RO(in_write_index);
447
448static ssize_t in_read_bytes_avail_show(struct device *dev,
449 struct device_attribute *dev_attr,
450 char *buf)
451{
452 struct hv_device *hv_dev = device_to_hv_device(dev);
453 struct hv_ring_buffer_debug_info inbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000454 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700455
456 if (!hv_dev->channel)
457 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000458
459 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
460 if (ret < 0)
461 return ret;
462
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700463 return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
464}
465static DEVICE_ATTR_RO(in_read_bytes_avail);
466
467static ssize_t in_write_bytes_avail_show(struct device *dev,
468 struct device_attribute *dev_attr,
469 char *buf)
470{
471 struct hv_device *hv_dev = device_to_hv_device(dev);
472 struct hv_ring_buffer_debug_info inbound;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000473 int ret;
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700474
475 if (!hv_dev->channel)
476 return -ENODEV;
Dexuan Cuiba50bf12018-12-17 20:16:09 +0000477
478 ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
479 if (ret < 0)
480 return ret;
481
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700482 return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
483}
484static DEVICE_ATTR_RO(in_write_bytes_avail);
485
Dexuan Cui042ab032015-08-05 00:52:43 -0700486static ssize_t channel_vp_mapping_show(struct device *dev,
487 struct device_attribute *dev_attr,
488 char *buf)
489{
490 struct hv_device *hv_dev = device_to_hv_device(dev);
491 struct vmbus_channel *channel = hv_dev->channel, *cur_sc;
492 unsigned long flags;
493 int buf_size = PAGE_SIZE, n_written, tot_written;
494 struct list_head *cur;
495
496 if (!channel)
497 return -ENODEV;
498
499 tot_written = snprintf(buf, buf_size, "%u:%u\n",
500 channel->offermsg.child_relid, channel->target_cpu);
501
502 spin_lock_irqsave(&channel->lock, flags);
503
504 list_for_each(cur, &channel->sc_list) {
505 if (tot_written >= buf_size - 1)
506 break;
507
508 cur_sc = list_entry(cur, struct vmbus_channel, sc_list);
509 n_written = scnprintf(buf + tot_written,
510 buf_size - tot_written,
511 "%u:%u\n",
512 cur_sc->offermsg.child_relid,
513 cur_sc->target_cpu);
514 tot_written += n_written;
515 }
516
517 spin_unlock_irqrestore(&channel->lock, flags);
518
519 return tot_written;
520}
521static DEVICE_ATTR_RO(channel_vp_mapping);
522
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800523static ssize_t vendor_show(struct device *dev,
524 struct device_attribute *dev_attr,
525 char *buf)
526{
527 struct hv_device *hv_dev = device_to_hv_device(dev);
528 return sprintf(buf, "0x%x\n", hv_dev->vendor_id);
529}
530static DEVICE_ATTR_RO(vendor);
531
532static ssize_t device_show(struct device *dev,
533 struct device_attribute *dev_attr,
534 char *buf)
535{
536 struct hv_device *hv_dev = device_to_hv_device(dev);
537 return sprintf(buf, "0x%x\n", hv_dev->device_id);
538}
539static DEVICE_ATTR_RO(device);
540
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000541static ssize_t driver_override_store(struct device *dev,
542 struct device_attribute *attr,
543 const char *buf, size_t count)
544{
545 struct hv_device *hv_dev = device_to_hv_device(dev);
546 char *driver_override, *old, *cp;
547
548 /* We need to keep extra room for a newline */
549 if (count >= (PAGE_SIZE - 1))
550 return -EINVAL;
551
552 driver_override = kstrndup(buf, count, GFP_KERNEL);
553 if (!driver_override)
554 return -ENOMEM;
555
556 cp = strchr(driver_override, '\n');
557 if (cp)
558 *cp = '\0';
559
560 device_lock(dev);
561 old = hv_dev->driver_override;
562 if (strlen(driver_override)) {
563 hv_dev->driver_override = driver_override;
564 } else {
565 kfree(driver_override);
566 hv_dev->driver_override = NULL;
567 }
568 device_unlock(dev);
569
570 kfree(old);
571
572 return count;
573}
574
575static ssize_t driver_override_show(struct device *dev,
576 struct device_attribute *attr, char *buf)
577{
578 struct hv_device *hv_dev = device_to_hv_device(dev);
579 ssize_t len;
580
581 device_lock(dev);
582 len = snprintf(buf, PAGE_SIZE, "%s\n", hv_dev->driver_override);
583 device_unlock(dev);
584
585 return len;
586}
587static DEVICE_ATTR_RW(driver_override);
588
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700589/* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800590static struct attribute *vmbus_dev_attrs[] = {
Greg Kroah-Hartman03f3a912013-09-13 11:32:49 -0700591 &dev_attr_id.attr,
Greg Kroah-Hartmana8fb5f32013-09-13 11:32:50 -0700592 &dev_attr_state.attr,
Greg Kroah-Hartman5ffd00e2013-09-13 11:32:51 -0700593 &dev_attr_monitor_id.attr,
Greg Kroah-Hartman68234c02013-09-13 11:32:53 -0700594 &dev_attr_class_id.attr,
Greg Kroah-Hartman7c55e1d2013-09-13 11:32:54 -0700595 &dev_attr_device_id.attr,
Greg Kroah-Hartman647fa372013-09-13 11:32:52 -0700596 &dev_attr_modalias.attr,
Stephen Hemminger7ceb1c32018-07-28 21:58:48 +0000597#ifdef CONFIG_NUMA
598 &dev_attr_numa_node.attr,
599#endif
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700600 &dev_attr_server_monitor_pending.attr,
601 &dev_attr_client_monitor_pending.attr,
Greg Kroah-Hartman1cee2722013-09-13 11:32:57 -0700602 &dev_attr_server_monitor_latency.attr,
603 &dev_attr_client_monitor_latency.attr,
Greg Kroah-Hartman4947c742013-09-13 11:32:58 -0700604 &dev_attr_server_monitor_conn_id.attr,
605 &dev_attr_client_monitor_conn_id.attr,
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700606 &dev_attr_out_intr_mask.attr,
607 &dev_attr_out_read_index.attr,
608 &dev_attr_out_write_index.attr,
609 &dev_attr_out_read_bytes_avail.attr,
610 &dev_attr_out_write_bytes_avail.attr,
611 &dev_attr_in_intr_mask.attr,
612 &dev_attr_in_read_index.attr,
613 &dev_attr_in_write_index.attr,
614 &dev_attr_in_read_bytes_avail.attr,
615 &dev_attr_in_write_bytes_avail.attr,
Dexuan Cui042ab032015-08-05 00:52:43 -0700616 &dev_attr_channel_vp_mapping.attr,
K. Y. Srinivasan7047f172015-12-25 20:00:30 -0800617 &dev_attr_vendor.attr,
618 &dev_attr_device.attr,
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000619 &dev_attr_driver_override.attr,
Greg Kroah-Hartman03f3a912013-09-13 11:32:49 -0700620 NULL,
621};
Kimberly Brown46fc1542019-03-19 00:04:01 -0400622
623/*
624 * Device-level attribute_group callback function. Returns the permission for
625 * each attribute, and returns 0 if an attribute is not visible.
626 */
627static umode_t vmbus_dev_attr_is_visible(struct kobject *kobj,
628 struct attribute *attr, int idx)
629{
630 struct device *dev = kobj_to_dev(kobj);
631 const struct hv_device *hv_dev = device_to_hv_device(dev);
632
633 /* Hide the monitor attributes if the monitor mechanism is not used. */
634 if (!hv_dev->channel->offermsg.monitor_allocated &&
635 (attr == &dev_attr_monitor_id.attr ||
636 attr == &dev_attr_server_monitor_pending.attr ||
637 attr == &dev_attr_client_monitor_pending.attr ||
638 attr == &dev_attr_server_monitor_latency.attr ||
639 attr == &dev_attr_client_monitor_latency.attr ||
640 attr == &dev_attr_server_monitor_conn_id.attr ||
641 attr == &dev_attr_client_monitor_conn_id.attr))
642 return 0;
643
644 return attr->mode;
645}
646
647static const struct attribute_group vmbus_dev_group = {
648 .attrs = vmbus_dev_attrs,
649 .is_visible = vmbus_dev_attr_is_visible
650};
651__ATTRIBUTE_GROUPS(vmbus_dev);
Greg Kroah-Hartman03f3a912013-09-13 11:32:49 -0700652
K. Y. Srinivasanadde2482011-03-15 15:03:37 -0700653/*
654 * vmbus_uevent - add uevent for our device
655 *
656 * This routine is invoked when a device is added or removed on the vmbus to
657 * generate a uevent to udev in the userspace. The udev will then look at its
658 * rule and the uevent generated here to load the appropriate driver
K. Y. Srinivasan0ddda662011-08-25 09:48:38 -0700659 *
660 * The alias string will be of the form vmbus:guid where guid is the string
661 * representation of the device guid (each byte of the guid will be
662 * represented with two hex characters.
K. Y. Srinivasanadde2482011-03-15 15:03:37 -0700663 */
664static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
665{
666 struct hv_device *dev = device_to_hv_device(device);
Olaf Heringfd776ba2011-09-02 18:25:56 +0200667 int ret;
668 char alias_name[VMBUS_ALIAS_LEN + 1];
K. Y. Srinivasanadde2482011-03-15 15:03:37 -0700669
Olaf Heringfd776ba2011-09-02 18:25:56 +0200670 print_alias_name(dev, alias_name);
K. Y. Srinivasan0ddda662011-08-25 09:48:38 -0700671 ret = add_uevent_var(env, "MODALIAS=vmbus:%s", alias_name);
672 return ret;
K. Y. Srinivasanadde2482011-03-15 15:03:37 -0700673}
674
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000675static const struct hv_vmbus_device_id *
Andy Shevchenko593db802019-01-10 16:25:32 +0200676hv_vmbus_dev_match(const struct hv_vmbus_device_id *id, const guid_t *guid)
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000677{
678 if (id == NULL)
679 return NULL; /* empty device table */
680
Andy Shevchenko593db802019-01-10 16:25:32 +0200681 for (; !guid_is_null(&id->guid); id++)
682 if (guid_equal(&id->guid, guid))
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000683 return id;
684
685 return NULL;
686}
687
688static const struct hv_vmbus_device_id *
Andy Shevchenko593db802019-01-10 16:25:32 +0200689hv_vmbus_dynid_match(struct hv_driver *drv, const guid_t *guid)
K. Y. Srinivasan3037a7b2011-09-13 10:59:37 -0700690{
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800691 const struct hv_vmbus_device_id *id = NULL;
692 struct vmbus_dynid *dynid;
693
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800694 spin_lock(&drv->dynids.lock);
695 list_for_each_entry(dynid, &drv->dynids.list, node) {
Andy Shevchenko593db802019-01-10 16:25:32 +0200696 if (guid_equal(&dynid->id.guid, guid)) {
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800697 id = &dynid->id;
698 break;
699 }
700 }
701 spin_unlock(&drv->dynids.lock);
702
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000703 return id;
704}
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800705
Andy Shevchenko593db802019-01-10 16:25:32 +0200706static const struct hv_vmbus_device_id vmbus_device_null;
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800707
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000708/*
709 * Return a matching hv_vmbus_device_id pointer.
710 * If there is no match, return NULL.
711 */
712static const struct hv_vmbus_device_id *hv_vmbus_get_id(struct hv_driver *drv,
713 struct hv_device *dev)
714{
Andy Shevchenko593db802019-01-10 16:25:32 +0200715 const guid_t *guid = &dev->dev_type;
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000716 const struct hv_vmbus_device_id *id;
K. Y. Srinivasan3037a7b2011-09-13 10:59:37 -0700717
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000718 /* When driver_override is set, only bind to the matching driver */
719 if (dev->driver_override && strcmp(dev->driver_override, drv->name))
720 return NULL;
721
722 /* Look at the dynamic ids first, before the static ones */
723 id = hv_vmbus_dynid_match(drv, guid);
724 if (!id)
725 id = hv_vmbus_dev_match(drv->id_table, guid);
726
727 /* driver_override will always match, send a dummy id */
728 if (!id && dev->driver_override)
729 id = &vmbus_device_null;
730
731 return id;
K. Y. Srinivasan3037a7b2011-09-13 10:59:37 -0700732}
733
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800734/* vmbus_add_dynid - add a new device ID to this driver and re-probe devices */
Andy Shevchenko593db802019-01-10 16:25:32 +0200735static int vmbus_add_dynid(struct hv_driver *drv, guid_t *guid)
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800736{
737 struct vmbus_dynid *dynid;
738
739 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
740 if (!dynid)
741 return -ENOMEM;
742
743 dynid->id.guid = *guid;
744
745 spin_lock(&drv->dynids.lock);
746 list_add_tail(&dynid->node, &drv->dynids.list);
747 spin_unlock(&drv->dynids.lock);
748
749 return driver_attach(&drv->driver);
750}
751
752static void vmbus_free_dynids(struct hv_driver *drv)
753{
754 struct vmbus_dynid *dynid, *n;
755
756 spin_lock(&drv->dynids.lock);
757 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
758 list_del(&dynid->node);
759 kfree(dynid);
760 }
761 spin_unlock(&drv->dynids.lock);
762}
763
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800764/*
765 * store_new_id - sysfs frontend to vmbus_add_dynid()
766 *
767 * Allow GUIDs to be added to an existing driver via sysfs.
768 */
769static ssize_t new_id_store(struct device_driver *driver, const char *buf,
770 size_t count)
771{
772 struct hv_driver *drv = drv_to_hv_drv(driver);
Andy Shevchenko593db802019-01-10 16:25:32 +0200773 guid_t guid;
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800774 ssize_t retval;
775
Andy Shevchenko593db802019-01-10 16:25:32 +0200776 retval = guid_parse(buf, &guid);
Andy Shevchenko31100102017-05-18 10:46:06 -0700777 if (retval)
778 return retval;
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800779
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000780 if (hv_vmbus_dynid_match(drv, &guid))
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800781 return -EEXIST;
782
783 retval = vmbus_add_dynid(drv, &guid);
784 if (retval)
785 return retval;
786 return count;
787}
788static DRIVER_ATTR_WO(new_id);
789
790/*
791 * store_remove_id - remove a PCI device ID from this driver
792 *
793 * Removes a dynamic pci device ID to this driver.
794 */
795static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
796 size_t count)
797{
798 struct hv_driver *drv = drv_to_hv_drv(driver);
799 struct vmbus_dynid *dynid, *n;
Andy Shevchenko593db802019-01-10 16:25:32 +0200800 guid_t guid;
Andy Shevchenko31100102017-05-18 10:46:06 -0700801 ssize_t retval;
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800802
Andy Shevchenko593db802019-01-10 16:25:32 +0200803 retval = guid_parse(buf, &guid);
Andy Shevchenko31100102017-05-18 10:46:06 -0700804 if (retval)
805 return retval;
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800806
Andy Shevchenko31100102017-05-18 10:46:06 -0700807 retval = -ENODEV;
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800808 spin_lock(&drv->dynids.lock);
809 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
810 struct hv_vmbus_device_id *id = &dynid->id;
811
Andy Shevchenko593db802019-01-10 16:25:32 +0200812 if (guid_equal(&id->guid, &guid)) {
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800813 list_del(&dynid->node);
814 kfree(dynid);
815 retval = count;
816 break;
817 }
818 }
819 spin_unlock(&drv->dynids.lock);
820
821 return retval;
822}
823static DRIVER_ATTR_WO(remove_id);
824
825static struct attribute *vmbus_drv_attrs[] = {
826 &driver_attr_new_id.attr,
827 &driver_attr_remove_id.attr,
828 NULL,
829};
830ATTRIBUTE_GROUPS(vmbus_drv);
K. Y. Srinivasan3037a7b2011-09-13 10:59:37 -0700831
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700832
833/*
834 * vmbus_match - Attempt to match the specified device to the specified driver
835 */
836static int vmbus_match(struct device *device, struct device_driver *driver)
837{
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700838 struct hv_driver *drv = drv_to_hv_drv(driver);
K. Y. Srinivasane8e27042011-06-06 15:50:04 -0700839 struct hv_device *hv_dev = device_to_hv_device(device);
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700840
Dexuan Cui8981da32016-01-27 22:29:41 -0800841 /* The hv_sock driver handles all hv_sock offers. */
842 if (is_hvsock_channel(hv_dev->channel))
843 return drv->hvsock;
844
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000845 if (hv_vmbus_get_id(drv, hv_dev))
K. Y. Srinivasan3037a7b2011-09-13 10:59:37 -0700846 return 1;
K. Y. Srinivasande632a2b2011-04-26 09:20:24 -0700847
K. Y. Srinivasan5841a822011-08-25 09:48:39 -0700848 return 0;
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700849}
850
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700851/*
852 * vmbus_probe - Add the new vmbus's child device
853 */
854static int vmbus_probe(struct device *child_device)
855{
856 int ret = 0;
857 struct hv_driver *drv =
858 drv_to_hv_drv(child_device->driver);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700859 struct hv_device *dev = device_to_hv_device(child_device);
K. Y. Srinivasan84946892011-09-13 10:59:38 -0700860 const struct hv_vmbus_device_id *dev_id;
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700861
Stephen Hemmingerd765edb2018-08-10 23:06:08 +0000862 dev_id = hv_vmbus_get_id(drv, dev);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700863 if (drv->probe) {
K. Y. Srinivasan84946892011-09-13 10:59:38 -0700864 ret = drv->probe(dev, dev_id);
K. Y. Srinivasanb14a7b32011-04-29 13:45:03 -0700865 if (ret != 0)
Hank Janssen0a466182011-03-29 13:58:47 -0700866 pr_err("probe failed for device %s (%d)\n",
867 dev_name(child_device), ret);
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700868
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700869 } else {
Hank Janssen0a466182011-03-29 13:58:47 -0700870 pr_err("probe not set for driver %s\n",
871 dev_name(child_device));
K. Y. Srinivasan6de925b2011-06-06 15:50:07 -0700872 ret = -ENODEV;
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700873 }
874 return ret;
875}
876
K. Y. Srinivasanc5dce3d2011-03-15 15:03:40 -0700877/*
878 * vmbus_remove - Remove a vmbus device
879 */
880static int vmbus_remove(struct device *child_device)
881{
K. Y. Srinivasand15a0302015-02-28 11:18:16 -0800882 struct hv_driver *drv;
K. Y. Srinivasan415b0232011-04-29 13:45:12 -0700883 struct hv_device *dev = device_to_hv_device(child_device);
K. Y. Srinivasanc5dce3d2011-03-15 15:03:40 -0700884
K. Y. Srinivasand15a0302015-02-28 11:18:16 -0800885 if (child_device->driver) {
886 drv = drv_to_hv_drv(child_device->driver);
887 if (drv->remove)
888 drv->remove(dev);
K. Y. Srinivasand15a0302015-02-28 11:18:16 -0800889 }
K. Y. Srinivasanc5dce3d2011-03-15 15:03:40 -0700890
891 return 0;
892}
893
K. Y. Srinivasaneb1bb252011-03-15 15:03:41 -0700894
895/*
896 * vmbus_shutdown - Shutdown a vmbus device
897 */
898static void vmbus_shutdown(struct device *child_device)
899{
900 struct hv_driver *drv;
K. Y. Srinivasanca6887f2011-04-29 13:45:14 -0700901 struct hv_device *dev = device_to_hv_device(child_device);
K. Y. Srinivasaneb1bb252011-03-15 15:03:41 -0700902
903
904 /* The device may not be attached yet */
905 if (!child_device->driver)
906 return;
907
908 drv = drv_to_hv_drv(child_device->driver);
909
K. Y. Srinivasanca6887f2011-04-29 13:45:14 -0700910 if (drv->shutdown)
911 drv->shutdown(dev);
K. Y. Srinivasaneb1bb252011-03-15 15:03:41 -0700912}
913
K. Y. Srinivasan086e7a52011-03-15 15:03:42 -0700914
915/*
916 * vmbus_device_release - Final callback release of the vmbus child device
917 */
918static void vmbus_device_release(struct device *device)
919{
K. Y. Srinivasane8e27042011-06-06 15:50:04 -0700920 struct hv_device *hv_dev = device_to_hv_device(device);
Dexuan Cui34c68012015-12-14 16:01:49 -0800921 struct vmbus_channel *channel = hv_dev->channel;
K. Y. Srinivasan086e7a52011-03-15 15:03:42 -0700922
K. Y. Srinivasan54a662652017-04-30 16:21:18 -0700923 mutex_lock(&vmbus_connection.channel_mutex);
Stephen Hemminger800b9322018-09-14 09:10:15 -0700924 hv_process_channel_removal(channel);
K. Y. Srinivasan54a662652017-04-30 16:21:18 -0700925 mutex_unlock(&vmbus_connection.channel_mutex);
K. Y. Srinivasane8e27042011-06-06 15:50:04 -0700926 kfree(hv_dev);
K. Y. Srinivasan086e7a52011-03-15 15:03:42 -0700927}
928
Bill Pemberton454f18a2009-07-27 16:47:24 -0400929/* The one and only one */
K. Y. Srinivasan9adcac52011-04-29 13:45:08 -0700930static struct bus_type hv_bus = {
931 .name = "vmbus",
932 .match = vmbus_match,
933 .shutdown = vmbus_shutdown,
934 .remove = vmbus_remove,
935 .probe = vmbus_probe,
936 .uevent = vmbus_uevent,
Stephen Hemmingerfc769362016-12-03 12:34:39 -0800937 .dev_groups = vmbus_dev_groups,
938 .drv_groups = vmbus_drv_groups,
Hank Janssen3e7ee492009-07-13 16:02:34 -0700939};
940
Timo Teräsbf6506f2010-12-15 20:48:08 +0200941struct onmessage_work_context {
942 struct work_struct work;
943 struct hv_message msg;
944};
945
946static void vmbus_onmessage_work(struct work_struct *work)
947{
948 struct onmessage_work_context *ctx;
949
Vitaly Kuznetsov09a19622015-02-27 11:25:54 -0800950 /* Do not process messages if we're in DISCONNECTED state */
951 if (vmbus_connection.conn_state == DISCONNECTED)
952 return;
953
Timo Teräsbf6506f2010-12-15 20:48:08 +0200954 ctx = container_of(work, struct onmessage_work_context,
955 work);
956 vmbus_onmessage(&ctx->msg);
957 kfree(ctx);
958}
959
K. Y. Srinivasand81274a2016-02-26 15:13:21 -0800960void vmbus_on_msg_dpc(unsigned long data)
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800961{
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700962 struct hv_per_cpu_context *hv_cpu = (void *)data;
963 void *page_addr = hv_cpu->synic_message_page;
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800964 struct hv_message *msg = (struct hv_message *)page_addr +
965 VMBUS_MESSAGE_SINT;
Dexuan Cui652594c2015-03-27 09:10:08 -0700966 struct vmbus_channel_message_header *hdr;
Stephen Hemmingere6242fa2017-03-04 18:27:16 -0700967 const struct vmbus_channel_message_table_entry *entry;
Timo Teräsbf6506f2010-12-15 20:48:08 +0200968 struct onmessage_work_context *ctx;
Vitaly Kuznetsovcd95aad2016-04-30 19:21:34 -0700969 u32 message_type = msg->header.message_type;
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800970
Vitaly Kuznetsovcd95aad2016-04-30 19:21:34 -0700971 if (message_type == HVMSG_NONE)
Vitaly Kuznetsov7be3e162016-02-26 15:13:15 -0800972 /* no msg */
973 return;
Dexuan Cui652594c2015-03-27 09:10:08 -0700974
Vitaly Kuznetsov7be3e162016-02-26 15:13:15 -0800975 hdr = (struct vmbus_channel_message_header *)msg->u.payload;
Dexuan Cui652594c2015-03-27 09:10:08 -0700976
Vitaly Kuznetsovc9fe0f82017-10-29 12:21:00 -0700977 trace_vmbus_on_msg_dpc(hdr);
978
Vitaly Kuznetsov7be3e162016-02-26 15:13:15 -0800979 if (hdr->msgtype >= CHANNELMSG_COUNT) {
980 WARN_ONCE(1, "unknown msgtype=%d\n", hdr->msgtype);
981 goto msg_handled;
982 }
Dexuan Cui652594c2015-03-27 09:10:08 -0700983
Vitaly Kuznetsov7be3e162016-02-26 15:13:15 -0800984 entry = &channel_message_table[hdr->msgtype];
985 if (entry->handler_type == VMHT_BLOCKING) {
986 ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
987 if (ctx == NULL)
988 return;
Dexuan Cui652594c2015-03-27 09:10:08 -0700989
Vitaly Kuznetsov7be3e162016-02-26 15:13:15 -0800990 INIT_WORK(&ctx->work, vmbus_onmessage_work);
991 memcpy(&ctx->msg, msg, sizeof(*msg));
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800992
K. Y. Srinivasan54a662652017-04-30 16:21:18 -0700993 /*
994 * The host can generate a rescind message while we
995 * may still be handling the original offer. We deal with
996 * this condition by ensuring the processing is done on the
997 * same CPU.
998 */
999 switch (hdr->msgtype) {
1000 case CHANNELMSG_RESCIND_CHANNELOFFER:
1001 /*
1002 * If we are handling the rescind message;
1003 * schedule the work on the global work queue.
1004 */
1005 schedule_work_on(vmbus_connection.connect_cpu,
1006 &ctx->work);
1007 break;
1008
1009 case CHANNELMSG_OFFERCHANNEL:
1010 atomic_inc(&vmbus_connection.offer_in_progress);
1011 queue_work_on(vmbus_connection.connect_cpu,
1012 vmbus_connection.work_queue,
1013 &ctx->work);
1014 break;
1015
1016 default:
1017 queue_work(vmbus_connection.work_queue, &ctx->work);
1018 }
Vitaly Kuznetsov7be3e162016-02-26 15:13:15 -08001019 } else
1020 entry->message_handler(hdr);
Dexuan Cui652594c2015-03-27 09:10:08 -07001021
1022msg_handled:
Vitaly Kuznetsovcd95aad2016-04-30 19:21:34 -07001023 vmbus_signal_eom(msg, message_type);
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001024}
1025
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001026
1027/*
Stephen Hemmingerb71e3282017-02-11 23:02:21 -07001028 * Direct callback for channels using other deferred processing
1029 */
1030static void vmbus_channel_isr(struct vmbus_channel *channel)
1031{
1032 void (*callback_fn)(void *);
1033
1034 callback_fn = READ_ONCE(channel->onchannel_callback);
1035 if (likely(callback_fn != NULL))
1036 (*callback_fn)(channel->channel_callback_context);
1037}
1038
1039/*
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001040 * Schedule all channels with events pending
1041 */
1042static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu)
1043{
1044 unsigned long *recv_int_page;
1045 u32 maxbits, relid;
1046
1047 if (vmbus_proto_version < VERSION_WIN8) {
1048 maxbits = MAX_NUM_CHANNELS_SUPPORTED;
1049 recv_int_page = vmbus_connection.recv_int_page;
1050 } else {
1051 /*
1052 * When the host is win8 and beyond, the event page
1053 * can be directly checked to get the id of the channel
1054 * that has the interrupt pending.
1055 */
1056 void *page_addr = hv_cpu->synic_event_page;
1057 union hv_synic_event_flags *event
1058 = (union hv_synic_event_flags *)page_addr +
1059 VMBUS_MESSAGE_SINT;
1060
1061 maxbits = HV_EVENT_FLAGS_COUNT;
1062 recv_int_page = event->flags;
1063 }
1064
1065 if (unlikely(!recv_int_page))
1066 return;
1067
1068 for_each_set_bit(relid, recv_int_page, maxbits) {
1069 struct vmbus_channel *channel;
1070
1071 if (!sync_test_and_clear_bit(relid, recv_int_page))
1072 continue;
1073
1074 /* Special case - vmbus channel protocol msg */
1075 if (relid == 0)
1076 continue;
1077
Stephen Hemminger8200f202017-03-04 18:13:57 -07001078 rcu_read_lock();
1079
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001080 /* Find channel based on relid */
Stephen Hemminger8200f202017-03-04 18:13:57 -07001081 list_for_each_entry_rcu(channel, &hv_cpu->chan_list, percpu_list) {
Stephen Hemmingerb71e3282017-02-11 23:02:21 -07001082 if (channel->offermsg.child_relid != relid)
1083 continue;
1084
K. Y. Srinivasan6f3d7912017-08-11 10:03:59 -07001085 if (channel->rescind)
1086 continue;
1087
Vitaly Kuznetsov991f8f12017-10-29 12:21:16 -07001088 trace_vmbus_chan_sched(channel);
1089
Stephen Hemminger6981fbf2017-10-29 11:33:40 -07001090 ++channel->interrupts;
1091
Stephen Hemmingerb71e3282017-02-11 23:02:21 -07001092 switch (channel->callback_mode) {
1093 case HV_CALL_ISR:
1094 vmbus_channel_isr(channel);
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001095 break;
Stephen Hemmingerb71e3282017-02-11 23:02:21 -07001096
1097 case HV_CALL_BATCHED:
1098 hv_begin_read(&channel->inbound);
1099 /* fallthrough */
1100 case HV_CALL_DIRECT:
1101 tasklet_schedule(&channel->callback_event);
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001102 }
1103 }
Stephen Hemminger8200f202017-03-04 18:13:57 -07001104
1105 rcu_read_unlock();
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001106 }
1107}
1108
Thomas Gleixner76d388c2014-03-05 13:42:14 +01001109static void vmbus_isr(void)
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001110{
Stephen Hemminger37cdd992017-02-11 23:02:19 -07001111 struct hv_per_cpu_context *hv_cpu
1112 = this_cpu_ptr(hv_context.cpu_context);
1113 void *page_addr = hv_cpu->synic_event_page;
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001114 struct hv_message *msg;
1115 union hv_synic_event_flags *event;
K. Y. Srinivasanae4636e2011-08-27 11:31:35 -07001116 bool handled = false;
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001117
Stephen Hemminger37cdd992017-02-11 23:02:19 -07001118 if (unlikely(page_addr == NULL))
Thomas Gleixner76d388c2014-03-05 13:42:14 +01001119 return;
K. Y. Srinivasan5ab05952012-12-01 06:46:55 -08001120
1121 event = (union hv_synic_event_flags *)page_addr +
1122 VMBUS_MESSAGE_SINT;
K. Y. Srinivasan7341d902011-08-31 14:35:56 -07001123 /*
1124 * Check for events before checking for messages. This is the order
1125 * in which events and messages are checked in Windows guests on
1126 * Hyper-V, and the Windows team suggested we do the same.
1127 */
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001128
K. Y. Srinivasan6552ecd72012-12-01 06:46:49 -08001129 if ((vmbus_proto_version == VERSION_WS2008) ||
1130 (vmbus_proto_version == VERSION_WIN7)) {
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -08001131
K. Y. Srinivasan6552ecd72012-12-01 06:46:49 -08001132 /* Since we are a child, we only need to check bit 0 */
Stephen Hemminger5c1bec62017-02-05 17:20:31 -07001133 if (sync_test_and_clear_bit(0, event->flags))
K. Y. Srinivasan6552ecd72012-12-01 06:46:49 -08001134 handled = true;
K. Y. Srinivasan6552ecd72012-12-01 06:46:49 -08001135 } else {
1136 /*
1137 * Our host is win8 or above. The signaling mechanism
1138 * has changed and we can directly look at the event page.
1139 * If bit n is set then we have an interrup on the channel
1140 * whose id is n.
1141 */
K. Y. Srinivasanae4636e2011-08-27 11:31:35 -07001142 handled = true;
K. Y. Srinivasan793be9c2011-03-15 15:03:43 -07001143 }
K. Y. Srinivasanae4636e2011-08-27 11:31:35 -07001144
K. Y. Srinivasan6552ecd72012-12-01 06:46:49 -08001145 if (handled)
Stephen Hemminger631e63a2017-02-11 23:02:20 -07001146 vmbus_chan_sched(hv_cpu);
K. Y. Srinivasan6552ecd72012-12-01 06:46:49 -08001147
Stephen Hemminger37cdd992017-02-11 23:02:19 -07001148 page_addr = hv_cpu->synic_message_page;
K. Y. Srinivasan7341d902011-08-31 14:35:56 -07001149 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
1150
1151 /* Check if there are actual msgs to be processed */
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -08001152 if (msg->header.message_type != HVMSG_NONE) {
Michael Kelleyfd1fea62019-07-01 04:25:56 +00001153 if (msg->header.message_type == HVMSG_TIMER_EXPIRED) {
1154 hv_stimer0_isr();
1155 vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED);
1156 } else
Stephen Hemminger37cdd992017-02-11 23:02:19 -07001157 tasklet_schedule(&hv_cpu->msg_dpc);
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -08001158 }
Stephan Mueller4b44f2d2016-05-02 02:14:34 -04001159
1160 add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0);
K. Y. Srinivasan793be9c2011-03-15 15:03:43 -07001161}
1162
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001163/*
1164 * Boolean to control whether to report panic messages over Hyper-V.
1165 *
1166 * It can be set via /proc/sys/kernel/hyperv/record_panic_msg
1167 */
1168static int sysctl_record_panic_msg = 1;
1169
1170/*
1171 * Callback from kmsg_dump. Grab as much as possible from the end of the kmsg
1172 * buffer and call into Hyper-V to transfer the data.
1173 */
1174static void hv_kmsg_dump(struct kmsg_dumper *dumper,
1175 enum kmsg_dump_reason reason)
1176{
1177 size_t bytes_written;
1178 phys_addr_t panic_pa;
1179
1180 /* We are only interested in panics. */
1181 if ((reason != KMSG_DUMP_PANIC) || (!sysctl_record_panic_msg))
1182 return;
1183
1184 panic_pa = virt_to_phys(hv_panic_page);
1185
1186 /*
1187 * Write dump contents to the page. No need to synchronize; panic should
1188 * be single-threaded.
1189 */
Sunil Muthuswamyddcaf3c2018-07-28 21:58:45 +00001190 kmsg_dump_get_buffer(dumper, true, hv_panic_page, PAGE_SIZE,
1191 &bytes_written);
1192 if (bytes_written)
1193 hyperv_report_panic_msg(panic_pa, bytes_written);
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001194}
1195
1196static struct kmsg_dumper hv_kmsg_dumper = {
1197 .dump = hv_kmsg_dump,
1198};
1199
1200static struct ctl_table_header *hv_ctl_table_hdr;
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001201
1202/*
1203 * sysctl option to allow the user to control whether kmsg data should be
1204 * reported to Hyper-V on panic.
1205 */
1206static struct ctl_table hv_ctl_table[] = {
1207 {
1208 .procname = "hyperv_record_panic_msg",
1209 .data = &sysctl_record_panic_msg,
1210 .maxlen = sizeof(int),
1211 .mode = 0644,
1212 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -07001213 .extra1 = SYSCTL_ZERO,
1214 .extra2 = SYSCTL_ONE
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001215 },
1216 {}
1217};
1218
1219static struct ctl_table hv_root_table[] = {
1220 {
1221 .procname = "kernel",
1222 .mode = 0555,
1223 .child = hv_ctl_table
1224 },
1225 {}
1226};
Vitaly Kuznetsove5132292015-02-27 11:25:51 -08001227
Hank Janssen3e189512010-03-04 22:11:00 +00001228/*
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001229 * vmbus_bus_init -Main vmbus driver initialization routine.
1230 *
1231 * Here, we
Lars Lindley0686e4f2010-03-11 23:51:23 +01001232 * - initialize the vmbus driver context
Lars Lindley0686e4f2010-03-11 23:51:23 +01001233 * - invoke the vmbus hv main init routine
Lars Lindley0686e4f2010-03-11 23:51:23 +01001234 * - retrieve the channel offers
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001235 */
K. Y. Srinivasanefc26722015-12-14 16:01:46 -08001236static int vmbus_bus_init(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001237{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001238 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001239
Greg Kroah-Hartman6d26e38fa22010-12-02 12:08:08 -08001240 /* Hypervisor initialization...setup hypercall page..etc */
1241 ret = hv_init();
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001242 if (ret != 0) {
Hank Janssen0a466182011-03-29 13:58:47 -07001243 pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
K. Y. Srinivasand6c1c5d2011-06-06 15:50:08 -07001244 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001245 }
1246
K. Y. Srinivasan9adcac52011-04-29 13:45:08 -07001247 ret = bus_register(&hv_bus);
K. Y. Srinivasand6c1c5d2011-06-06 15:50:08 -07001248 if (ret)
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -07001249 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001250
Thomas Gleixner76d388c2014-03-05 13:42:14 +01001251 hv_setup_vmbus_irq(vmbus_isr);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001252
Jason Wang2608fb62013-06-19 11:28:10 +08001253 ret = hv_synic_alloc();
1254 if (ret)
1255 goto err_alloc;
Michael Kelleyfd1fea62019-07-01 04:25:56 +00001256
1257 ret = hv_stimer_alloc(VMBUS_MESSAGE_SINT);
1258 if (ret < 0)
1259 goto err_alloc;
1260
K. Y. Srinivasan800b6902011-03-15 15:03:33 -07001261 /*
Michael Kelleyfd1fea62019-07-01 04:25:56 +00001262 * Initialize the per-cpu interrupt state and stimer state.
1263 * Then connect to the host.
K. Y. Srinivasan800b6902011-03-15 15:03:33 -07001264 */
Michael Kelley4a5f3cd2017-12-22 11:19:02 -07001265 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hyperv/vmbus:online",
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -08001266 hv_synic_init, hv_synic_cleanup);
1267 if (ret < 0)
Michael Kelleyfd1fea62019-07-01 04:25:56 +00001268 goto err_cpuhp;
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -08001269 hyperv_cpuhp_online = ret;
1270
K. Y. Srinivasan800b6902011-03-15 15:03:33 -07001271 ret = vmbus_connect();
K. Y. Srinivasan8b9987e92011-08-31 14:35:55 -07001272 if (ret)
Andrey Smetanin17efbee2015-12-14 16:01:38 -08001273 goto err_connect;
K. Y. Srinivasan800b6902011-03-15 15:03:33 -07001274
Nick Meier96c1d052015-02-28 11:39:01 -08001275 /*
1276 * Only register if the crash MSRs are available
1277 */
Denis V. Lunevcc2dd402015-08-01 16:08:20 -07001278 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001279 u64 hyperv_crash_ctl;
1280 /*
1281 * Sysctl registration is not fatal, since by default
1282 * reporting is enabled.
1283 */
1284 hv_ctl_table_hdr = register_sysctl_table(hv_root_table);
1285 if (!hv_ctl_table_hdr)
1286 pr_err("Hyper-V: sysctl table register error");
1287
1288 /*
1289 * Register for panic kmsg callback only if the right
1290 * capability is supported by the hypervisor.
1291 */
Sunil Muthuswamy9d9c9652018-07-28 21:58:47 +00001292 hv_get_crash_ctl(hyperv_crash_ctl);
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001293 if (hyperv_crash_ctl & HV_CRASH_CTL_CRASH_NOTIFY_MSG) {
1294 hv_panic_page = (void *)get_zeroed_page(GFP_KERNEL);
1295 if (hv_panic_page) {
1296 ret = kmsg_dump_register(&hv_kmsg_dumper);
1297 if (ret)
1298 pr_err("Hyper-V: kmsg dump register "
1299 "error 0x%x\n", ret);
1300 } else
1301 pr_err("Hyper-V: panic message page memory "
1302 "allocation failed");
1303 }
1304
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -07001305 register_die_notifier(&hyperv_die_block);
Nick Meier96c1d052015-02-28 11:39:01 -08001306 atomic_notifier_chain_register(&panic_notifier_list,
1307 &hyperv_panic_block);
1308 }
1309
Greg Kroah-Hartman2d6e8822010-12-02 08:50:58 -08001310 vmbus_request_offers();
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +00001311
K. Y. Srinivasand6c1c5d2011-06-06 15:50:08 -07001312 return 0;
K. Y. Srinivasan8b9987e92011-08-31 14:35:55 -07001313
Andrey Smetanin17efbee2015-12-14 16:01:38 -08001314err_connect:
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -08001315 cpuhp_remove_state(hyperv_cpuhp_online);
Michael Kelleyfd1fea62019-07-01 04:25:56 +00001316err_cpuhp:
1317 hv_stimer_free();
Jason Wang2608fb62013-06-19 11:28:10 +08001318err_alloc:
1319 hv_synic_free();
Thomas Gleixner76d388c2014-03-05 13:42:14 +01001320 hv_remove_vmbus_irq();
K. Y. Srinivasan8b9987e92011-08-31 14:35:55 -07001321
K. Y. Srinivasan8b9987e92011-08-31 14:35:55 -07001322 bus_unregister(&hv_bus);
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00001323 free_page((unsigned long)hv_panic_page);
Sunil Muthuswamy8afc06d2018-07-28 21:58:46 +00001324 unregister_sysctl_table(hv_ctl_table_hdr);
1325 hv_ctl_table_hdr = NULL;
K. Y. Srinivasan8b9987e92011-08-31 14:35:55 -07001326 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001327}
1328
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001329/**
Jake Oshins35464482015-08-05 00:52:37 -07001330 * __vmbus_child_driver_register() - Register a vmbus's driver
1331 * @hv_driver: Pointer to driver structure you want to register
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001332 * @owner: owner module of the drv
1333 * @mod_name: module name string
Hank Janssen3e189512010-03-04 22:11:00 +00001334 *
1335 * Registers the given driver with Linux through the 'driver_register()' call
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001336 * and sets up the hyper-v vmbus handling for this driver.
Hank Janssen3e189512010-03-04 22:11:00 +00001337 * It will return the state of the 'driver_register()' call.
1338 *
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001339 */
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001340int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001341{
Bill Pemberton5d48a1c2009-07-27 16:47:36 -04001342 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001343
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001344 pr_info("registering driver %s\n", hv_driver->name);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001345
K. Y. Srinivasancf6a2ea2011-12-01 09:59:34 -08001346 ret = vmbus_exists();
1347 if (ret < 0)
1348 return ret;
1349
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001350 hv_driver->driver.name = hv_driver->name;
1351 hv_driver->driver.owner = owner;
1352 hv_driver->driver.mod_name = mod_name;
1353 hv_driver->driver.bus = &hv_bus;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001354
Stephen Hemmingerfc769362016-12-03 12:34:39 -08001355 spin_lock_init(&hv_driver->dynids.lock);
1356 INIT_LIST_HEAD(&hv_driver->dynids.list);
1357
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001358 ret = driver_register(&hv_driver->driver);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001359
Bill Pemberton5d48a1c2009-07-27 16:47:36 -04001360 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001361}
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001362EXPORT_SYMBOL_GPL(__vmbus_driver_register);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001363
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001364/**
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001365 * vmbus_driver_unregister() - Unregister a vmbus's driver
Jake Oshins35464482015-08-05 00:52:37 -07001366 * @hv_driver: Pointer to driver structure you want to
1367 * un-register
Hank Janssen3e189512010-03-04 22:11:00 +00001368 *
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001369 * Un-register the given driver that was previous registered with a call to
1370 * vmbus_driver_register()
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001371 */
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001372void vmbus_driver_unregister(struct hv_driver *hv_driver)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001373{
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001374 pr_info("unregistering driver %s\n", hv_driver->name);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001375
Stephen Hemmingerfc769362016-12-03 12:34:39 -08001376 if (!vmbus_exists()) {
K. Y. Srinivasan8f257a12011-12-27 13:49:37 -08001377 driver_unregister(&hv_driver->driver);
Stephen Hemmingerfc769362016-12-03 12:34:39 -08001378 vmbus_free_dynids(hv_driver);
1379 }
Hank Janssen3e7ee492009-07-13 16:02:34 -07001380}
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001381EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001382
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001383
1384/*
1385 * Called when last reference to channel is gone.
1386 */
1387static void vmbus_chan_release(struct kobject *kobj)
1388{
1389 struct vmbus_channel *channel
1390 = container_of(kobj, struct vmbus_channel, kobj);
1391
1392 kfree_rcu(channel, rcu);
1393}
1394
1395struct vmbus_chan_attribute {
1396 struct attribute attr;
Kimberly Brown14948e32019-03-14 16:05:15 -04001397 ssize_t (*show)(struct vmbus_channel *chan, char *buf);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001398 ssize_t (*store)(struct vmbus_channel *chan,
1399 const char *buf, size_t count);
1400};
1401#define VMBUS_CHAN_ATTR(_name, _mode, _show, _store) \
1402 struct vmbus_chan_attribute chan_attr_##_name \
1403 = __ATTR(_name, _mode, _show, _store)
1404#define VMBUS_CHAN_ATTR_RW(_name) \
1405 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RW(_name)
1406#define VMBUS_CHAN_ATTR_RO(_name) \
1407 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RO(_name)
1408#define VMBUS_CHAN_ATTR_WO(_name) \
1409 struct vmbus_chan_attribute chan_attr_##_name = __ATTR_WO(_name)
1410
1411static ssize_t vmbus_chan_attr_show(struct kobject *kobj,
1412 struct attribute *attr, char *buf)
1413{
1414 const struct vmbus_chan_attribute *attribute
1415 = container_of(attr, struct vmbus_chan_attribute, attr);
Kimberly Brown14948e32019-03-14 16:05:15 -04001416 struct vmbus_channel *chan
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001417 = container_of(kobj, struct vmbus_channel, kobj);
1418
1419 if (!attribute->show)
1420 return -EIO;
1421
1422 return attribute->show(chan, buf);
1423}
1424
1425static const struct sysfs_ops vmbus_chan_sysfs_ops = {
1426 .show = vmbus_chan_attr_show,
1427};
1428
Kimberly Brown14948e32019-03-14 16:05:15 -04001429static ssize_t out_mask_show(struct vmbus_channel *channel, char *buf)
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001430{
Kimberly Brown14948e32019-03-14 16:05:15 -04001431 struct hv_ring_buffer_info *rbi = &channel->outbound;
1432 ssize_t ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001433
Kimberly Brown14948e32019-03-14 16:05:15 -04001434 mutex_lock(&rbi->ring_buffer_mutex);
1435 if (!rbi->ring_buffer) {
1436 mutex_unlock(&rbi->ring_buffer_mutex);
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001437 return -EINVAL;
Kimberly Brown14948e32019-03-14 16:05:15 -04001438 }
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001439
Kimberly Brown14948e32019-03-14 16:05:15 -04001440 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1441 mutex_unlock(&rbi->ring_buffer_mutex);
1442 return ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001443}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001444static VMBUS_CHAN_ATTR_RO(out_mask);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001445
Kimberly Brown14948e32019-03-14 16:05:15 -04001446static ssize_t in_mask_show(struct vmbus_channel *channel, char *buf)
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001447{
Kimberly Brown14948e32019-03-14 16:05:15 -04001448 struct hv_ring_buffer_info *rbi = &channel->inbound;
1449 ssize_t ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001450
Kimberly Brown14948e32019-03-14 16:05:15 -04001451 mutex_lock(&rbi->ring_buffer_mutex);
1452 if (!rbi->ring_buffer) {
1453 mutex_unlock(&rbi->ring_buffer_mutex);
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001454 return -EINVAL;
Kimberly Brown14948e32019-03-14 16:05:15 -04001455 }
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001456
Kimberly Brown14948e32019-03-14 16:05:15 -04001457 ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1458 mutex_unlock(&rbi->ring_buffer_mutex);
1459 return ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001460}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001461static VMBUS_CHAN_ATTR_RO(in_mask);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001462
Kimberly Brown14948e32019-03-14 16:05:15 -04001463static ssize_t read_avail_show(struct vmbus_channel *channel, char *buf)
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001464{
Kimberly Brown14948e32019-03-14 16:05:15 -04001465 struct hv_ring_buffer_info *rbi = &channel->inbound;
1466 ssize_t ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001467
Kimberly Brown14948e32019-03-14 16:05:15 -04001468 mutex_lock(&rbi->ring_buffer_mutex);
1469 if (!rbi->ring_buffer) {
1470 mutex_unlock(&rbi->ring_buffer_mutex);
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001471 return -EINVAL;
Kimberly Brown14948e32019-03-14 16:05:15 -04001472 }
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001473
Kimberly Brown14948e32019-03-14 16:05:15 -04001474 ret = sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi));
1475 mutex_unlock(&rbi->ring_buffer_mutex);
1476 return ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001477}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001478static VMBUS_CHAN_ATTR_RO(read_avail);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001479
Kimberly Brown14948e32019-03-14 16:05:15 -04001480static ssize_t write_avail_show(struct vmbus_channel *channel, char *buf)
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001481{
Kimberly Brown14948e32019-03-14 16:05:15 -04001482 struct hv_ring_buffer_info *rbi = &channel->outbound;
1483 ssize_t ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001484
Kimberly Brown14948e32019-03-14 16:05:15 -04001485 mutex_lock(&rbi->ring_buffer_mutex);
1486 if (!rbi->ring_buffer) {
1487 mutex_unlock(&rbi->ring_buffer_mutex);
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001488 return -EINVAL;
Kimberly Brown14948e32019-03-14 16:05:15 -04001489 }
Kimberly Brownfcedbb22019-03-14 16:05:00 -04001490
Kimberly Brown14948e32019-03-14 16:05:15 -04001491 ret = sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi));
1492 mutex_unlock(&rbi->ring_buffer_mutex);
1493 return ret;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001494}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001495static VMBUS_CHAN_ATTR_RO(write_avail);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001496
Kimberly Brown14948e32019-03-14 16:05:15 -04001497static ssize_t show_target_cpu(struct vmbus_channel *channel, char *buf)
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001498{
1499 return sprintf(buf, "%u\n", channel->target_cpu);
1500}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001501static VMBUS_CHAN_ATTR(cpu, S_IRUGO, show_target_cpu, NULL);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001502
Kimberly Brown14948e32019-03-14 16:05:15 -04001503static ssize_t channel_pending_show(struct vmbus_channel *channel,
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001504 char *buf)
1505{
1506 return sprintf(buf, "%d\n",
1507 channel_pending(channel,
1508 vmbus_connection.monitor_pages[1]));
1509}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001510static VMBUS_CHAN_ATTR(pending, S_IRUGO, channel_pending_show, NULL);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001511
Kimberly Brown14948e32019-03-14 16:05:15 -04001512static ssize_t channel_latency_show(struct vmbus_channel *channel,
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001513 char *buf)
1514{
1515 return sprintf(buf, "%d\n",
1516 channel_latency(channel,
1517 vmbus_connection.monitor_pages[1]));
1518}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001519static VMBUS_CHAN_ATTR(latency, S_IRUGO, channel_latency_show, NULL);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001520
Kimberly Brown14948e32019-03-14 16:05:15 -04001521static ssize_t channel_interrupts_show(struct vmbus_channel *channel, char *buf)
Stephen Hemminger6981fbf2017-10-29 11:33:40 -07001522{
1523 return sprintf(buf, "%llu\n", channel->interrupts);
1524}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001525static VMBUS_CHAN_ATTR(interrupts, S_IRUGO, channel_interrupts_show, NULL);
Stephen Hemminger6981fbf2017-10-29 11:33:40 -07001526
Kimberly Brown14948e32019-03-14 16:05:15 -04001527static ssize_t channel_events_show(struct vmbus_channel *channel, char *buf)
Stephen Hemminger6981fbf2017-10-29 11:33:40 -07001528{
1529 return sprintf(buf, "%llu\n", channel->sig_events);
1530}
Stephen Hemminger875c3622018-01-04 14:13:25 -08001531static VMBUS_CHAN_ATTR(events, S_IRUGO, channel_events_show, NULL);
Stephen Hemminger6981fbf2017-10-29 11:33:40 -07001532
Kimberly Brown14948e32019-03-14 16:05:15 -04001533static ssize_t channel_intr_in_full_show(struct vmbus_channel *channel,
Kimberly Brown396ae572019-02-04 02:13:09 -05001534 char *buf)
1535{
1536 return sprintf(buf, "%llu\n",
1537 (unsigned long long)channel->intr_in_full);
1538}
1539static VMBUS_CHAN_ATTR(intr_in_full, 0444, channel_intr_in_full_show, NULL);
1540
Kimberly Brown14948e32019-03-14 16:05:15 -04001541static ssize_t channel_intr_out_empty_show(struct vmbus_channel *channel,
Kimberly Brown396ae572019-02-04 02:13:09 -05001542 char *buf)
1543{
1544 return sprintf(buf, "%llu\n",
1545 (unsigned long long)channel->intr_out_empty);
1546}
1547static VMBUS_CHAN_ATTR(intr_out_empty, 0444, channel_intr_out_empty_show, NULL);
1548
Kimberly Brown14948e32019-03-14 16:05:15 -04001549static ssize_t channel_out_full_first_show(struct vmbus_channel *channel,
Kimberly Brown396ae572019-02-04 02:13:09 -05001550 char *buf)
1551{
1552 return sprintf(buf, "%llu\n",
1553 (unsigned long long)channel->out_full_first);
1554}
1555static VMBUS_CHAN_ATTR(out_full_first, 0444, channel_out_full_first_show, NULL);
1556
Kimberly Brown14948e32019-03-14 16:05:15 -04001557static ssize_t channel_out_full_total_show(struct vmbus_channel *channel,
Kimberly Brown396ae572019-02-04 02:13:09 -05001558 char *buf)
1559{
1560 return sprintf(buf, "%llu\n",
1561 (unsigned long long)channel->out_full_total);
1562}
1563static VMBUS_CHAN_ATTR(out_full_total, 0444, channel_out_full_total_show, NULL);
1564
Kimberly Brown14948e32019-03-14 16:05:15 -04001565static ssize_t subchannel_monitor_id_show(struct vmbus_channel *channel,
Stephen Hemmingerf0fa2972018-01-09 10:29:06 -08001566 char *buf)
1567{
1568 return sprintf(buf, "%u\n", channel->offermsg.monitorid);
1569}
1570static VMBUS_CHAN_ATTR(monitor_id, S_IRUGO, subchannel_monitor_id_show, NULL);
1571
Kimberly Brown14948e32019-03-14 16:05:15 -04001572static ssize_t subchannel_id_show(struct vmbus_channel *channel,
Stephen Hemmingerf0fa2972018-01-09 10:29:06 -08001573 char *buf)
1574{
1575 return sprintf(buf, "%u\n",
1576 channel->offermsg.offer.sub_channel_index);
1577}
1578static VMBUS_CHAN_ATTR_RO(subchannel_id);
1579
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001580static struct attribute *vmbus_chan_attrs[] = {
1581 &chan_attr_out_mask.attr,
1582 &chan_attr_in_mask.attr,
1583 &chan_attr_read_avail.attr,
1584 &chan_attr_write_avail.attr,
1585 &chan_attr_cpu.attr,
1586 &chan_attr_pending.attr,
1587 &chan_attr_latency.attr,
Stephen Hemminger6981fbf2017-10-29 11:33:40 -07001588 &chan_attr_interrupts.attr,
1589 &chan_attr_events.attr,
Kimberly Brown396ae572019-02-04 02:13:09 -05001590 &chan_attr_intr_in_full.attr,
1591 &chan_attr_intr_out_empty.attr,
1592 &chan_attr_out_full_first.attr,
1593 &chan_attr_out_full_total.attr,
Stephen Hemmingerf0fa2972018-01-09 10:29:06 -08001594 &chan_attr_monitor_id.attr,
1595 &chan_attr_subchannel_id.attr,
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001596 NULL
1597};
1598
Kimberly Brown46fc1542019-03-19 00:04:01 -04001599/*
1600 * Channel-level attribute_group callback function. Returns the permission for
1601 * each attribute, and returns 0 if an attribute is not visible.
1602 */
1603static umode_t vmbus_chan_attr_is_visible(struct kobject *kobj,
1604 struct attribute *attr, int idx)
1605{
1606 const struct vmbus_channel *channel =
1607 container_of(kobj, struct vmbus_channel, kobj);
1608
1609 /* Hide the monitor attributes if the monitor mechanism is not used. */
1610 if (!channel->offermsg.monitor_allocated &&
1611 (attr == &chan_attr_pending.attr ||
1612 attr == &chan_attr_latency.attr ||
1613 attr == &chan_attr_monitor_id.attr))
1614 return 0;
1615
1616 return attr->mode;
1617}
1618
1619static struct attribute_group vmbus_chan_group = {
1620 .attrs = vmbus_chan_attrs,
1621 .is_visible = vmbus_chan_attr_is_visible
1622};
1623
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001624static struct kobj_type vmbus_chan_ktype = {
1625 .sysfs_ops = &vmbus_chan_sysfs_ops,
1626 .release = vmbus_chan_release,
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001627};
1628
1629/*
1630 * vmbus_add_channel_kobj - setup a sub-directory under device/channels
1631 */
1632int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
1633{
Kimberly Brown46fc1542019-03-19 00:04:01 -04001634 const struct device *device = &dev->device;
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001635 struct kobject *kobj = &channel->kobj;
1636 u32 relid = channel->offermsg.child_relid;
1637 int ret;
1638
1639 kobj->kset = dev->channels_kset;
1640 ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
1641 "%u", relid);
1642 if (ret)
1643 return ret;
1644
Kimberly Brown46fc1542019-03-19 00:04:01 -04001645 ret = sysfs_create_group(kobj, &vmbus_chan_group);
1646
1647 if (ret) {
1648 /*
1649 * The calling functions' error handling paths will cleanup the
1650 * empty channel directory.
1651 */
1652 dev_err(device, "Unable to set up channel sysfs files\n");
1653 return ret;
1654 }
1655
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001656 kobject_uevent(kobj, KOBJ_ADD);
1657
1658 return 0;
1659}
1660
Hank Janssen3e189512010-03-04 22:11:00 +00001661/*
Kimberly Brown46fc1542019-03-19 00:04:01 -04001662 * vmbus_remove_channel_attr_group - remove the channel's attribute group
1663 */
1664void vmbus_remove_channel_attr_group(struct vmbus_channel *channel)
1665{
1666 sysfs_remove_group(&channel->kobj, &vmbus_chan_group);
1667}
1668
1669/*
K. Y. Srinivasanf2c73012011-09-08 07:24:12 -07001670 * vmbus_device_create - Creates and registers a new child device
Hank Janssen3e189512010-03-04 22:11:00 +00001671 * on the vmbus.
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001672 */
Andy Shevchenko593db802019-01-10 16:25:32 +02001673struct hv_device *vmbus_device_create(const guid_t *type,
1674 const guid_t *instance,
stephen hemminger1b9d48f2014-06-03 08:38:15 -07001675 struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001676{
Nicolas Palix3d3b5512009-07-28 17:32:53 +02001677 struct hv_device *child_device_obj;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001678
K. Y. Srinivasan6bad88da2011-03-07 13:35:48 -08001679 child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
1680 if (!child_device_obj) {
Hank Janssen0a466182011-03-29 13:58:47 -07001681 pr_err("Unable to allocate device object for child device\n");
Hank Janssen3e7ee492009-07-13 16:02:34 -07001682 return NULL;
1683 }
1684
Greg Kroah-Hartmancae5b842010-10-21 09:05:27 -07001685 child_device_obj->channel = channel;
Andy Shevchenko593db802019-01-10 16:25:32 +02001686 guid_copy(&child_device_obj->dev_type, type);
1687 guid_copy(&child_device_obj->dev_instance, instance);
K. Y. Srinivasan7047f172015-12-25 20:00:30 -08001688 child_device_obj->vendor_id = 0x1414; /* MSFT vendor ID */
Hank Janssen3e7ee492009-07-13 16:02:34 -07001689
Hank Janssen3e7ee492009-07-13 16:02:34 -07001690 return child_device_obj;
1691}
1692
Hank Janssen3e189512010-03-04 22:11:00 +00001693/*
K. Y. Srinivasan227942812011-09-08 07:24:13 -07001694 * vmbus_device_register - Register the child device
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001695 */
K. Y. Srinivasan227942812011-09-08 07:24:13 -07001696int vmbus_device_register(struct hv_device *child_device_obj)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001697{
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001698 struct kobject *kobj = &child_device_obj->device.kobj;
1699 int ret;
K. Y. Srinivasan6bad88da2011-03-07 13:35:48 -08001700
Stephen Hemmingerf6b2db02016-11-01 00:01:59 -07001701 dev_set_name(&child_device_obj->device, "%pUl",
Vitaly Kuznetsovb2948092016-09-16 09:01:17 -07001702 child_device_obj->channel->offermsg.offer.if_instance.b);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001703
K. Y. Srinivasan0bce28b2011-08-27 11:31:39 -07001704 child_device_obj->device.bus = &hv_bus;
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -07001705 child_device_obj->device.parent = &hv_acpi_dev->dev;
K. Y. Srinivasan6bad88da2011-03-07 13:35:48 -08001706 child_device_obj->device.release = vmbus_device_release;
Hank Janssen3e7ee492009-07-13 16:02:34 -07001707
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001708 /*
1709 * Register with the LDM. This will kick off the driver/device
1710 * binding...which will eventually call vmbus_match() and vmbus_probe()
1711 */
K. Y. Srinivasan6bad88da2011-03-07 13:35:48 -08001712 ret = device_register(&child_device_obj->device);
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001713 if (ret) {
Hank Janssen0a466182011-03-29 13:58:47 -07001714 pr_err("Unable to register child device\n");
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001715 return ret;
1716 }
Hank Janssen3e7ee492009-07-13 16:02:34 -07001717
Stephen Hemmingerc2e5df62017-09-21 20:58:49 -07001718 child_device_obj->channels_kset = kset_create_and_add("channels",
1719 NULL, kobj);
1720 if (!child_device_obj->channels_kset) {
1721 ret = -ENOMEM;
1722 goto err_dev_unregister;
1723 }
1724
1725 ret = vmbus_add_channel_kobj(child_device_obj,
1726 child_device_obj->channel);
1727 if (ret) {
1728 pr_err("Unable to register primary channeln");
1729 goto err_kset_unregister;
1730 }
1731
1732 return 0;
1733
1734err_kset_unregister:
1735 kset_unregister(child_device_obj->channels_kset);
1736
1737err_dev_unregister:
1738 device_unregister(&child_device_obj->device);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001739 return ret;
1740}
1741
Hank Janssen3e189512010-03-04 22:11:00 +00001742/*
K. Y. Srinivasan696453b2011-09-08 07:24:14 -07001743 * vmbus_device_unregister - Remove the specified child device
Hank Janssen3e189512010-03-04 22:11:00 +00001744 * from the vmbus.
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001745 */
K. Y. Srinivasan696453b2011-09-08 07:24:14 -07001746void vmbus_device_unregister(struct hv_device *device_obj)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001747{
Fernando Soto84672362013-06-14 23:13:35 +00001748 pr_debug("child device %s unregistered\n",
1749 dev_name(&device_obj->device));
1750
Dexuan Cui869b5562017-11-14 06:53:32 -07001751 kset_unregister(device_obj->channels_kset);
1752
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001753 /*
1754 * Kick off the process of unregistering the device.
1755 * This will call vmbus_remove() and eventually vmbus_device_release()
1756 */
K. Y. Srinivasan6bad88da2011-03-07 13:35:48 -08001757 device_unregister(&device_obj->device);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001758}
1759
Hank Janssen3e7ee492009-07-13 16:02:34 -07001760
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001761/*
Jake Oshins7f163a62015-08-05 00:52:36 -07001762 * VMBUS is an acpi enumerated device. Get the information we
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08001763 * need from DSDT.
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001764 */
Jake Oshins7f163a62015-08-05 00:52:36 -07001765#define VTPM_BASE_ADDRESS 0xfed40000
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08001766static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001767{
Jake Oshins7f163a62015-08-05 00:52:36 -07001768 resource_size_t start = 0;
1769 resource_size_t end = 0;
1770 struct resource *new_res;
1771 struct resource **old_res = &hyperv_mmio;
1772 struct resource **prev_res = NULL;
1773
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08001774 switch (res->type) {
Jake Oshins7f163a62015-08-05 00:52:36 -07001775
1776 /*
1777 * "Address" descriptors are for bus windows. Ignore
1778 * "memory" descriptors, which are for registers on
1779 * devices.
1780 */
1781 case ACPI_RESOURCE_TYPE_ADDRESS32:
1782 start = res->data.address32.address.minimum;
1783 end = res->data.address32.address.maximum;
Gerd Hoffmann4eb923f2014-02-24 14:17:08 +01001784 break;
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001785
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08001786 case ACPI_RESOURCE_TYPE_ADDRESS64:
Jake Oshins7f163a62015-08-05 00:52:36 -07001787 start = res->data.address64.address.minimum;
1788 end = res->data.address64.address.maximum;
Gerd Hoffmann4eb923f2014-02-24 14:17:08 +01001789 break;
Jake Oshins7f163a62015-08-05 00:52:36 -07001790
1791 default:
1792 /* Unused resource type */
1793 return AE_OK;
1794
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001795 }
Jake Oshins7f163a62015-08-05 00:52:36 -07001796 /*
1797 * Ignore ranges that are below 1MB, as they're not
1798 * necessary or useful here.
1799 */
1800 if (end < 0x100000)
1801 return AE_OK;
1802
1803 new_res = kzalloc(sizeof(*new_res), GFP_ATOMIC);
1804 if (!new_res)
1805 return AE_NO_MEMORY;
1806
1807 /* If this range overlaps the virtual TPM, truncate it. */
1808 if (end > VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS)
1809 end = VTPM_BASE_ADDRESS;
1810
1811 new_res->name = "hyperv mmio";
1812 new_res->flags = IORESOURCE_MEM;
1813 new_res->start = start;
1814 new_res->end = end;
1815
Jake Oshins40f26f32015-12-14 16:01:52 -08001816 /*
Jake Oshins40f26f32015-12-14 16:01:52 -08001817 * If two ranges are adjacent, merge them.
1818 */
Jake Oshins7f163a62015-08-05 00:52:36 -07001819 do {
1820 if (!*old_res) {
1821 *old_res = new_res;
1822 break;
1823 }
1824
Jake Oshins40f26f32015-12-14 16:01:52 -08001825 if (((*old_res)->end + 1) == new_res->start) {
1826 (*old_res)->end = new_res->end;
1827 kfree(new_res);
1828 break;
1829 }
1830
1831 if ((*old_res)->start == new_res->end + 1) {
1832 (*old_res)->start = new_res->start;
1833 kfree(new_res);
1834 break;
1835 }
1836
Jake Oshins23a06832016-04-05 10:22:53 -07001837 if ((*old_res)->start > new_res->end) {
Jake Oshins7f163a62015-08-05 00:52:36 -07001838 new_res->sibling = *old_res;
1839 if (prev_res)
1840 (*prev_res)->sibling = new_res;
1841 *old_res = new_res;
1842 break;
1843 }
1844
1845 prev_res = old_res;
1846 old_res = &(*old_res)->sibling;
1847
1848 } while (1);
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001849
1850 return AE_OK;
1851}
1852
Jake Oshins7f163a62015-08-05 00:52:36 -07001853static int vmbus_acpi_remove(struct acpi_device *device)
1854{
1855 struct resource *cur_res;
1856 struct resource *next_res;
1857
1858 if (hyperv_mmio) {
Jake Oshins6d146ae2016-04-05 10:22:55 -07001859 if (fb_mmio) {
1860 __release_region(hyperv_mmio, fb_mmio->start,
1861 resource_size(fb_mmio));
1862 fb_mmio = NULL;
1863 }
1864
Jake Oshins7f163a62015-08-05 00:52:36 -07001865 for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) {
1866 next_res = cur_res->sibling;
1867 kfree(cur_res);
1868 }
1869 }
1870
1871 return 0;
1872}
1873
Jake Oshins6d146ae2016-04-05 10:22:55 -07001874static void vmbus_reserve_fb(void)
1875{
1876 int size;
1877 /*
1878 * Make a claim for the frame buffer in the resource tree under the
1879 * first node, which will be the one below 4GB. The length seems to
1880 * be underreported, particularly in a Generation 1 VM. So start out
1881 * reserving a larger area and make it smaller until it succeeds.
1882 */
1883
1884 if (screen_info.lfb_base) {
1885 if (efi_enabled(EFI_BOOT))
1886 size = max_t(__u32, screen_info.lfb_size, 0x800000);
1887 else
1888 size = max_t(__u32, screen_info.lfb_size, 0x4000000);
1889
1890 for (; !fb_mmio && (size >= 0x100000); size >>= 1) {
1891 fb_mmio = __request_region(hyperv_mmio,
1892 screen_info.lfb_base, size,
1893 fb_mmio_name, 0);
1894 }
1895 }
1896}
1897
Jake Oshins35464482015-08-05 00:52:37 -07001898/**
1899 * vmbus_allocate_mmio() - Pick a memory-mapped I/O range.
1900 * @new: If successful, supplied a pointer to the
1901 * allocated MMIO space.
1902 * @device_obj: Identifies the caller
1903 * @min: Minimum guest physical address of the
1904 * allocation
1905 * @max: Maximum guest physical address
1906 * @size: Size of the range to be allocated
1907 * @align: Alignment of the range to be allocated
1908 * @fb_overlap_ok: Whether this allocation can be allowed
1909 * to overlap the video frame buffer.
1910 *
1911 * This function walks the resources granted to VMBus by the
1912 * _CRS object in the ACPI namespace underneath the parent
1913 * "bridge" whether that's a root PCI bus in the Generation 1
1914 * case or a Module Device in the Generation 2 case. It then
1915 * attempts to allocate from the global MMIO pool in a way that
1916 * matches the constraints supplied in these parameters and by
1917 * that _CRS.
1918 *
1919 * Return: 0 on success, -errno on failure
1920 */
1921int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
1922 resource_size_t min, resource_size_t max,
1923 resource_size_t size, resource_size_t align,
1924 bool fb_overlap_ok)
1925{
Jake Oshinsbe000f92016-04-05 10:22:54 -07001926 struct resource *iter, *shadow;
Jake Oshinsea37a6b2016-04-05 10:22:56 -07001927 resource_size_t range_min, range_max, start;
Jake Oshins35464482015-08-05 00:52:37 -07001928 const char *dev_n = dev_name(&device_obj->device);
Jake Oshinsea37a6b2016-04-05 10:22:56 -07001929 int retval;
Jake Oshinse16dad62016-04-05 10:22:50 -07001930
1931 retval = -ENXIO;
1932 down(&hyperv_mmio_lock);
Jake Oshins35464482015-08-05 00:52:37 -07001933
Jake Oshinsea37a6b2016-04-05 10:22:56 -07001934 /*
1935 * If overlaps with frame buffers are allowed, then first attempt to
1936 * make the allocation from within the reserved region. Because it
1937 * is already reserved, no shadow allocation is necessary.
1938 */
1939 if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) &&
1940 !(max < fb_mmio->start)) {
1941
1942 range_min = fb_mmio->start;
1943 range_max = fb_mmio->end;
1944 start = (range_min + align - 1) & ~(align - 1);
1945 for (; start + size - 1 <= range_max; start += align) {
1946 *new = request_mem_region_exclusive(start, size, dev_n);
1947 if (*new) {
1948 retval = 0;
1949 goto exit;
1950 }
1951 }
1952 }
1953
Jake Oshins35464482015-08-05 00:52:37 -07001954 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
1955 if ((iter->start >= max) || (iter->end <= min))
1956 continue;
1957
1958 range_min = iter->start;
1959 range_max = iter->end;
Jake Oshinsea37a6b2016-04-05 10:22:56 -07001960 start = (range_min + align - 1) & ~(align - 1);
1961 for (; start + size - 1 <= range_max; start += align) {
1962 shadow = __request_region(iter, start, size, NULL,
1963 IORESOURCE_BUSY);
1964 if (!shadow)
1965 continue;
Jake Oshins35464482015-08-05 00:52:37 -07001966
Jake Oshinsea37a6b2016-04-05 10:22:56 -07001967 *new = request_mem_region_exclusive(start, size, dev_n);
1968 if (*new) {
1969 shadow->name = (char *)*new;
1970 retval = 0;
1971 goto exit;
Jake Oshins35464482015-08-05 00:52:37 -07001972 }
1973
Jake Oshinsea37a6b2016-04-05 10:22:56 -07001974 __release_region(iter, start, size);
Jake Oshins35464482015-08-05 00:52:37 -07001975 }
1976 }
1977
Jake Oshinse16dad62016-04-05 10:22:50 -07001978exit:
1979 up(&hyperv_mmio_lock);
1980 return retval;
Jake Oshins35464482015-08-05 00:52:37 -07001981}
1982EXPORT_SYMBOL_GPL(vmbus_allocate_mmio);
1983
Jake Oshins619848b2015-12-14 16:01:39 -08001984/**
Jake Oshins97fb77dc2016-04-05 10:22:51 -07001985 * vmbus_free_mmio() - Free a memory-mapped I/O range.
1986 * @start: Base address of region to release.
1987 * @size: Size of the range to be allocated
1988 *
1989 * This function releases anything requested by
1990 * vmbus_mmio_allocate().
1991 */
1992void vmbus_free_mmio(resource_size_t start, resource_size_t size)
1993{
Jake Oshinsbe000f92016-04-05 10:22:54 -07001994 struct resource *iter;
1995
1996 down(&hyperv_mmio_lock);
1997 for (iter = hyperv_mmio; iter; iter = iter->sibling) {
1998 if ((iter->start >= start + size) || (iter->end <= start))
1999 continue;
2000
2001 __release_region(iter, start, size);
2002 }
Jake Oshins97fb77dc2016-04-05 10:22:51 -07002003 release_mem_region(start, size);
Jake Oshinsbe000f92016-04-05 10:22:54 -07002004 up(&hyperv_mmio_lock);
Jake Oshins97fb77dc2016-04-05 10:22:51 -07002005
2006}
2007EXPORT_SYMBOL_GPL(vmbus_free_mmio);
2008
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002009static int vmbus_acpi_add(struct acpi_device *device)
2010{
2011 acpi_status result;
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002012 int ret_val = -ENODEV;
Jake Oshins7f163a62015-08-05 00:52:36 -07002013 struct acpi_device *ancestor;
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002014
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -07002015 hv_acpi_dev = device;
2016
K. Y. Srinivasan0a4425b2011-08-27 11:31:38 -07002017 result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002018 vmbus_walk_resources, NULL);
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002019
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002020 if (ACPI_FAILURE(result))
2021 goto acpi_walk_err;
2022 /*
Jake Oshins7f163a62015-08-05 00:52:36 -07002023 * Some ancestor of the vmbus acpi device (Gen1 or Gen2
2024 * firmware) is the VMOD that has the mmio ranges. Get that.
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002025 */
Jake Oshins7f163a62015-08-05 00:52:36 -07002026 for (ancestor = device->parent; ancestor; ancestor = ancestor->parent) {
2027 result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS,
2028 vmbus_walk_resources, NULL);
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002029
2030 if (ACPI_FAILURE(result))
Jake Oshins7f163a62015-08-05 00:52:36 -07002031 continue;
Jake Oshins6d146ae2016-04-05 10:22:55 -07002032 if (hyperv_mmio) {
2033 vmbus_reserve_fb();
Jake Oshins7f163a62015-08-05 00:52:36 -07002034 break;
Jake Oshins6d146ae2016-04-05 10:22:55 -07002035 }
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002036 }
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002037 ret_val = 0;
2038
2039acpi_walk_err:
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002040 complete(&probe_event);
Jake Oshins7f163a62015-08-05 00:52:36 -07002041 if (ret_val)
2042 vmbus_acpi_remove(device);
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08002043 return ret_val;
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002044}
2045
2046static const struct acpi_device_id vmbus_acpi_device_ids[] = {
2047 {"VMBUS", 0},
K. Y. Srinivasan9d7b18d2011-06-06 15:49:42 -07002048 {"VMBus", 0},
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002049 {"", 0},
2050};
2051MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
2052
2053static struct acpi_driver vmbus_acpi_driver = {
2054 .name = "vmbus",
2055 .ids = vmbus_acpi_device_ids,
2056 .ops = {
2057 .add = vmbus_acpi_add,
Vitaly Kuznetsove4ecb412015-04-22 21:31:28 -07002058 .remove = vmbus_acpi_remove,
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002059 },
2060};
2061
Vitaly Kuznetsov25172812015-08-01 16:08:07 -07002062static void hv_kexec_handler(void)
2063{
Michael Kelleyfd1fea62019-07-01 04:25:56 +00002064 hv_stimer_global_cleanup();
Vitaly Kuznetsov75ff3a82016-02-26 15:13:16 -08002065 vmbus_initiate_unload(false);
Vitaly Kuznetsov523b9402016-12-07 14:53:12 -08002066 vmbus_connection.conn_state = DISCONNECTED;
2067 /* Make sure conn_state is set as hv_synic_cleanup checks for it */
2068 mb();
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -08002069 cpuhp_remove_state(hyperv_cpuhp_online);
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -07002070 hyperv_cleanup();
Vitaly Kuznetsov25172812015-08-01 16:08:07 -07002071};
2072
Vitaly Kuznetsovb4370df2015-08-01 16:08:09 -07002073static void hv_crash_handler(struct pt_regs *regs)
2074{
Michael Kelleyfd1fea62019-07-01 04:25:56 +00002075 int cpu;
2076
Vitaly Kuznetsov75ff3a82016-02-26 15:13:16 -08002077 vmbus_initiate_unload(true);
Vitaly Kuznetsovb4370df2015-08-01 16:08:09 -07002078 /*
2079 * In crash handler we can't schedule synic cleanup for all CPUs,
2080 * doing the cleanup for current CPU only. This should be sufficient
2081 * for kdump.
2082 */
Vitaly Kuznetsov523b9402016-12-07 14:53:12 -08002083 vmbus_connection.conn_state = DISCONNECTED;
Michael Kelleyfd1fea62019-07-01 04:25:56 +00002084 cpu = smp_processor_id();
2085 hv_stimer_cleanup(cpu);
2086 hv_synic_cleanup(cpu);
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -07002087 hyperv_cleanup();
Vitaly Kuznetsovb4370df2015-08-01 16:08:09 -07002088};
2089
Dexuan Cui63ecc6d2019-09-05 23:01:16 +00002090static int hv_synic_suspend(void)
2091{
2092 /*
2093 * When we reach here, all the non-boot CPUs have been offlined, and
2094 * the stimers on them have been unbound in hv_synic_cleanup() ->
2095 * hv_stimer_cleanup() -> clockevents_unbind_device().
2096 *
2097 * hv_synic_suspend() only runs on CPU0 with interrupts disabled. Here
2098 * we do not unbind the stimer on CPU0 because: 1) it's unnecessary
2099 * because the interrupts remain disabled between syscore_suspend()
2100 * and syscore_resume(): see create_image() and resume_target_kernel();
2101 * 2) the stimer on CPU0 is automatically disabled later by
2102 * syscore_suspend() -> timekeeping_suspend() -> tick_suspend() -> ...
2103 * -> clockevents_shutdown() -> ... -> hv_ce_shutdown(); 3) a warning
2104 * would be triggered if we call clockevents_unbind_device(), which
2105 * may sleep, in an interrupts-disabled context. So, we intentionally
2106 * don't call hv_stimer_cleanup(0) here.
2107 */
2108
2109 hv_synic_disable_regs(0);
2110
2111 return 0;
2112}
2113
2114static void hv_synic_resume(void)
2115{
2116 hv_synic_enable_regs(0);
2117
2118 /*
2119 * Note: we don't need to call hv_stimer_init(0), because the timer
2120 * on CPU0 is not unbound in hv_synic_suspend(), and the timer is
2121 * automatically re-enabled in timekeeping_resume().
2122 */
2123}
2124
2125/* The callbacks run only on CPU0, with irqs_disabled. */
2126static struct syscore_ops hv_synic_syscore_ops = {
2127 .suspend = hv_synic_suspend,
2128 .resume = hv_synic_resume,
2129};
2130
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -07002131static int __init hv_acpi_init(void)
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -07002132{
K. Y. Srinivasan2dda95f2011-07-15 13:38:56 -07002133 int ret, t;
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002134
Michael Kelley4a5f3cd2017-12-22 11:19:02 -07002135 if (!hv_is_hyperv_initialized())
Jason Wang05929692012-08-17 18:52:43 +08002136 return -ENODEV;
2137
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002138 init_completion(&probe_event);
2139
2140 /*
K. Y. Srinivasanefc26722015-12-14 16:01:46 -08002141 * Get ACPI resources first.
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002142 */
K. Y. Srinivasan02466042011-06-06 15:49:40 -07002143 ret = acpi_bus_register_driver(&vmbus_acpi_driver);
2144
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002145 if (ret)
2146 return ret;
2147
K. Y. Srinivasan2dda95f2011-07-15 13:38:56 -07002148 t = wait_for_completion_timeout(&probe_event, 5*HZ);
2149 if (t == 0) {
2150 ret = -ETIMEDOUT;
2151 goto cleanup;
2152 }
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07002153
K. Y. Srinivasanefc26722015-12-14 16:01:46 -08002154 ret = vmbus_bus_init();
K. Y. Srinivasan91fd7992011-06-16 13:16:38 -07002155 if (ret)
K. Y. Srinivasan2dda95f2011-07-15 13:38:56 -07002156 goto cleanup;
2157
Vitaly Kuznetsov25172812015-08-01 16:08:07 -07002158 hv_setup_kexec_handler(hv_kexec_handler);
Vitaly Kuznetsovb4370df2015-08-01 16:08:09 -07002159 hv_setup_crash_handler(hv_crash_handler);
Vitaly Kuznetsov25172812015-08-01 16:08:07 -07002160
Dexuan Cui63ecc6d2019-09-05 23:01:16 +00002161 register_syscore_ops(&hv_synic_syscore_ops);
2162
K. Y. Srinivasan2dda95f2011-07-15 13:38:56 -07002163 return 0;
2164
2165cleanup:
2166 acpi_bus_unregister_driver(&vmbus_acpi_driver);
K. Y. Srinivasancf6a2ea2011-12-01 09:59:34 -08002167 hv_acpi_dev = NULL;
K. Y. Srinivasan91fd7992011-06-16 13:16:38 -07002168 return ret;
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -07002169}
2170
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08002171static void __exit vmbus_exit(void)
2172{
Vitaly Kuznetsove72e7ac2015-02-27 11:25:55 -08002173 int cpu;
2174
Dexuan Cui63ecc6d2019-09-05 23:01:16 +00002175 unregister_syscore_ops(&hv_synic_syscore_ops);
2176
Vitaly Kuznetsov25172812015-08-01 16:08:07 -07002177 hv_remove_kexec_handler();
Vitaly Kuznetsovb4370df2015-08-01 16:08:09 -07002178 hv_remove_crash_handler();
Vitaly Kuznetsov09a19622015-02-27 11:25:54 -08002179 vmbus_connection.conn_state = DISCONNECTED;
Michael Kelleyfd1fea62019-07-01 04:25:56 +00002180 hv_stimer_global_cleanup();
K. Y. Srinivasan2db84ef2015-04-22 21:31:32 -07002181 vmbus_disconnect();
Thomas Gleixner76d388c2014-03-05 13:42:14 +01002182 hv_remove_vmbus_irq();
Stephen Hemminger37cdd992017-02-11 23:02:19 -07002183 for_each_online_cpu(cpu) {
2184 struct hv_per_cpu_context *hv_cpu
2185 = per_cpu_ptr(hv_context.cpu_context, cpu);
2186
2187 tasklet_kill(&hv_cpu->msg_dpc);
2188 }
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08002189 vmbus_free_channels();
Stephen Hemminger37cdd992017-02-11 23:02:19 -07002190
Denis V. Lunevcc2dd402015-08-01 16:08:20 -07002191 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00002192 kmsg_dump_unregister(&hv_kmsg_dumper);
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -07002193 unregister_die_notifier(&hyperv_die_block);
Vitaly Kuznetsov096c6052015-04-22 21:31:29 -07002194 atomic_notifier_chain_unregister(&panic_notifier_list,
2195 &hyperv_panic_block);
2196 }
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +00002197
2198 free_page((unsigned long)hv_panic_page);
Sunil Muthuswamy8afc06d2018-07-28 21:58:46 +00002199 unregister_sysctl_table(hv_ctl_table_hdr);
2200 hv_ctl_table_hdr = NULL;
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08002201 bus_unregister(&hv_bus);
Stephen Hemminger37cdd992017-02-11 23:02:19 -07002202
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -08002203 cpuhp_remove_state(hyperv_cpuhp_online);
Vitaly Kuznetsov06210b42015-08-01 16:08:05 -07002204 hv_synic_free();
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08002205 acpi_bus_unregister_driver(&vmbus_acpi_driver);
2206}
2207
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -07002208
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07002209MODULE_LICENSE("GPL");
Joseph Salisbury674eecb2019-04-23 03:47:27 +00002210MODULE_DESCRIPTION("Microsoft Hyper-V VMBus Driver");
Hank Janssen3e7ee492009-07-13 16:02:34 -07002211
K. Y. Srinivasan43d4e112011-10-24 11:28:12 -07002212subsys_initcall(hv_acpi_init);
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08002213module_exit(vmbus_exit);