blob: e7b0bcd453e7a46e2067656cdd15187b91a09b6a [file] [log] [blame]
Hank Janssen3e7ee492009-07-13 16:02:34 -07001/*
Hank Janssen3e7ee492009-07-13 16:02:34 -07002 * Copyright (c) 2009, Microsoft Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
16 *
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -070020 * K. Y. Srinivasan <kys@microsoft.com>
K. Y. Srinivasan52e5c1c2011-03-15 15:03:34 -070021 *
Hank Janssen3e7ee492009-07-13 16:02:34 -070022 */
Hank Janssen0a466182011-03-29 13:58:47 -070023#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
Hank Janssen3e7ee492009-07-13 16:02:34 -070025#include <linux/init.h>
26#include <linux/module.h>
27#include <linux/device.h>
Hank Janssen3e7ee492009-07-13 16:02:34 -070028#include <linux/interrupt.h>
29#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -070031#include <linux/acpi.h>
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +000032#include <linux/completion.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070033#include <linux/hyperv.h>
K. Y. Srinivasanb0209502012-12-01 06:46:54 -080034#include <linux/kernel_stat.h>
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -080035#include <linux/clockchips.h>
Vitaly Kuznetsove5132292015-02-27 11:25:51 -080036#include <linux/cpu.h>
Greg Kroah-Hartman407dd162011-10-11 08:36:44 -060037#include <asm/hyperv.h>
Jason Wang1f94ea82012-08-31 13:32:44 +080038#include <asm/hypervisor.h>
K. Y. Srinivasan302a3c02013-02-17 11:30:44 -080039#include <asm/mshyperv.h>
Nick Meier96c1d052015-02-28 11:39:01 -080040#include <linux/notifier.h>
41#include <linux/ptrace.h>
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -070042#include <linux/kdebug.h>
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070043#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070044
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -070045static struct acpi_device *hv_acpi_dev;
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -070046
K. Y. Srinivasan59c0e4f2011-04-29 13:45:06 -070047static struct tasklet_struct msg_dpc;
K. Y. Srinivasan71a66552011-04-29 13:45:04 -070048static struct completion probe_event;
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -070049static int irq;
Hank Janssen3e7ee492009-07-13 16:02:34 -070050
Nick Meier96c1d052015-02-28 11:39:01 -080051
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -070052static void hyperv_report_panic(struct pt_regs *regs)
Nick Meier96c1d052015-02-28 11:39:01 -080053{
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -070054 static bool panic_reported;
Nick Meier96c1d052015-02-28 11:39:01 -080055
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -070056 /*
57 * We prefer to report panic on 'die' chain as we have proper
58 * registers to report, but if we miss it (e.g. on BUG()) we need
59 * to report it on 'panic'.
60 */
61 if (panic_reported)
62 return;
63 panic_reported = true;
Nick Meier96c1d052015-02-28 11:39:01 -080064
65 wrmsrl(HV_X64_MSR_CRASH_P0, regs->ip);
66 wrmsrl(HV_X64_MSR_CRASH_P1, regs->ax);
67 wrmsrl(HV_X64_MSR_CRASH_P2, regs->bx);
68 wrmsrl(HV_X64_MSR_CRASH_P3, regs->cx);
69 wrmsrl(HV_X64_MSR_CRASH_P4, regs->dx);
70
71 /*
72 * Let Hyper-V know there is crash data available
73 */
74 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -070075}
76
77static int hyperv_panic_event(struct notifier_block *nb, unsigned long val,
78 void *args)
79{
80 struct pt_regs *regs;
81
82 regs = current_pt_regs();
83
84 hyperv_report_panic(regs);
Nick Meier96c1d052015-02-28 11:39:01 -080085 return NOTIFY_DONE;
86}
87
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -070088static int hyperv_die_event(struct notifier_block *nb, unsigned long val,
89 void *args)
90{
91 struct die_args *die = (struct die_args *)args;
92 struct pt_regs *regs = die->regs;
93
94 hyperv_report_panic(regs);
95 return NOTIFY_DONE;
96}
97
98static struct notifier_block hyperv_die_block = {
99 .notifier_call = hyperv_die_event,
100};
Nick Meier96c1d052015-02-28 11:39:01 -0800101static struct notifier_block hyperv_panic_block = {
102 .notifier_call = hyperv_panic_event,
103};
104
Gerd Hoffmann90eedf02014-02-24 14:17:09 +0100105struct resource hyperv_mmio = {
106 .name = "hyperv mmio",
107 .flags = IORESOURCE_MEM,
108};
109EXPORT_SYMBOL_GPL(hyperv_mmio);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700110
K. Y. Srinivasancf6a2ea2011-12-01 09:59:34 -0800111static int vmbus_exists(void)
112{
113 if (hv_acpi_dev == NULL)
114 return -ENODEV;
115
116 return 0;
117}
118
Olaf Heringfd776ba2011-09-02 18:25:56 +0200119#define VMBUS_ALIAS_LEN ((sizeof((struct hv_vmbus_device_id *)0)->guid) * 2)
120static void print_alias_name(struct hv_device *hv_dev, char *alias_name)
121{
122 int i;
123 for (i = 0; i < VMBUS_ALIAS_LEN; i += 2)
124 sprintf(&alias_name[i], "%02x", hv_dev->dev_type.b[i/2]);
125}
126
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700127static u8 channel_monitor_group(struct vmbus_channel *channel)
128{
129 return (u8)channel->offermsg.monitorid / 32;
130}
131
132static u8 channel_monitor_offset(struct vmbus_channel *channel)
133{
134 return (u8)channel->offermsg.monitorid % 32;
135}
136
137static u32 channel_pending(struct vmbus_channel *channel,
138 struct hv_monitor_page *monitor_page)
139{
140 u8 monitor_group = channel_monitor_group(channel);
141 return monitor_page->trigger_group[monitor_group].pending;
142}
143
Greg Kroah-Hartman1cee2722013-09-13 11:32:57 -0700144static u32 channel_latency(struct vmbus_channel *channel,
145 struct hv_monitor_page *monitor_page)
146{
147 u8 monitor_group = channel_monitor_group(channel);
148 u8 monitor_offset = channel_monitor_offset(channel);
149 return monitor_page->latency[monitor_group][monitor_offset];
150}
151
Greg Kroah-Hartman4947c742013-09-13 11:32:58 -0700152static u32 channel_conn_id(struct vmbus_channel *channel,
153 struct hv_monitor_page *monitor_page)
154{
155 u8 monitor_group = channel_monitor_group(channel);
156 u8 monitor_offset = channel_monitor_offset(channel);
157 return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
158}
159
Greg Kroah-Hartman03f3a912013-09-13 11:32:49 -0700160static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
161 char *buf)
162{
163 struct hv_device *hv_dev = device_to_hv_device(dev);
164
165 if (!hv_dev->channel)
166 return -ENODEV;
167 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
168}
169static DEVICE_ATTR_RO(id);
170
Greg Kroah-Hartmana8fb5f32013-09-13 11:32:50 -0700171static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
172 char *buf)
173{
174 struct hv_device *hv_dev = device_to_hv_device(dev);
175
176 if (!hv_dev->channel)
177 return -ENODEV;
178 return sprintf(buf, "%d\n", hv_dev->channel->state);
179}
180static DEVICE_ATTR_RO(state);
181
Greg Kroah-Hartman5ffd00e2013-09-13 11:32:51 -0700182static ssize_t monitor_id_show(struct device *dev,
183 struct device_attribute *dev_attr, char *buf)
184{
185 struct hv_device *hv_dev = device_to_hv_device(dev);
186
187 if (!hv_dev->channel)
188 return -ENODEV;
189 return sprintf(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
190}
191static DEVICE_ATTR_RO(monitor_id);
192
Greg Kroah-Hartman68234c02013-09-13 11:32:53 -0700193static ssize_t class_id_show(struct device *dev,
194 struct device_attribute *dev_attr, char *buf)
195{
196 struct hv_device *hv_dev = device_to_hv_device(dev);
197
198 if (!hv_dev->channel)
199 return -ENODEV;
200 return sprintf(buf, "{%pUl}\n",
201 hv_dev->channel->offermsg.offer.if_type.b);
202}
203static DEVICE_ATTR_RO(class_id);
204
Greg Kroah-Hartman7c55e1d2013-09-13 11:32:54 -0700205static ssize_t device_id_show(struct device *dev,
206 struct device_attribute *dev_attr, char *buf)
207{
208 struct hv_device *hv_dev = device_to_hv_device(dev);
209
210 if (!hv_dev->channel)
211 return -ENODEV;
212 return sprintf(buf, "{%pUl}\n",
213 hv_dev->channel->offermsg.offer.if_instance.b);
214}
215static DEVICE_ATTR_RO(device_id);
216
Greg Kroah-Hartman647fa372013-09-13 11:32:52 -0700217static ssize_t modalias_show(struct device *dev,
218 struct device_attribute *dev_attr, char *buf)
219{
220 struct hv_device *hv_dev = device_to_hv_device(dev);
221 char alias_name[VMBUS_ALIAS_LEN + 1];
222
223 print_alias_name(hv_dev, alias_name);
224 return sprintf(buf, "vmbus:%s\n", alias_name);
225}
226static DEVICE_ATTR_RO(modalias);
227
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700228static ssize_t server_monitor_pending_show(struct device *dev,
229 struct device_attribute *dev_attr,
230 char *buf)
231{
232 struct hv_device *hv_dev = device_to_hv_device(dev);
233
234 if (!hv_dev->channel)
235 return -ENODEV;
236 return sprintf(buf, "%d\n",
237 channel_pending(hv_dev->channel,
238 vmbus_connection.monitor_pages[1]));
239}
240static DEVICE_ATTR_RO(server_monitor_pending);
241
242static ssize_t client_monitor_pending_show(struct device *dev,
243 struct device_attribute *dev_attr,
244 char *buf)
245{
246 struct hv_device *hv_dev = device_to_hv_device(dev);
247
248 if (!hv_dev->channel)
249 return -ENODEV;
250 return sprintf(buf, "%d\n",
251 channel_pending(hv_dev->channel,
252 vmbus_connection.monitor_pages[1]));
253}
254static DEVICE_ATTR_RO(client_monitor_pending);
Greg Kroah-Hartman68234c02013-09-13 11:32:53 -0700255
Greg Kroah-Hartman1cee2722013-09-13 11:32:57 -0700256static ssize_t server_monitor_latency_show(struct device *dev,
257 struct device_attribute *dev_attr,
258 char *buf)
259{
260 struct hv_device *hv_dev = device_to_hv_device(dev);
261
262 if (!hv_dev->channel)
263 return -ENODEV;
264 return sprintf(buf, "%d\n",
265 channel_latency(hv_dev->channel,
266 vmbus_connection.monitor_pages[0]));
267}
268static DEVICE_ATTR_RO(server_monitor_latency);
269
270static ssize_t client_monitor_latency_show(struct device *dev,
271 struct device_attribute *dev_attr,
272 char *buf)
273{
274 struct hv_device *hv_dev = device_to_hv_device(dev);
275
276 if (!hv_dev->channel)
277 return -ENODEV;
278 return sprintf(buf, "%d\n",
279 channel_latency(hv_dev->channel,
280 vmbus_connection.monitor_pages[1]));
281}
282static DEVICE_ATTR_RO(client_monitor_latency);
283
Greg Kroah-Hartman4947c742013-09-13 11:32:58 -0700284static ssize_t server_monitor_conn_id_show(struct device *dev,
285 struct device_attribute *dev_attr,
286 char *buf)
287{
288 struct hv_device *hv_dev = device_to_hv_device(dev);
289
290 if (!hv_dev->channel)
291 return -ENODEV;
292 return sprintf(buf, "%d\n",
293 channel_conn_id(hv_dev->channel,
294 vmbus_connection.monitor_pages[0]));
295}
296static DEVICE_ATTR_RO(server_monitor_conn_id);
297
298static ssize_t client_monitor_conn_id_show(struct device *dev,
299 struct device_attribute *dev_attr,
300 char *buf)
301{
302 struct hv_device *hv_dev = device_to_hv_device(dev);
303
304 if (!hv_dev->channel)
305 return -ENODEV;
306 return sprintf(buf, "%d\n",
307 channel_conn_id(hv_dev->channel,
308 vmbus_connection.monitor_pages[1]));
309}
310static DEVICE_ATTR_RO(client_monitor_conn_id);
311
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700312static ssize_t out_intr_mask_show(struct device *dev,
313 struct device_attribute *dev_attr, char *buf)
314{
315 struct hv_device *hv_dev = device_to_hv_device(dev);
316 struct hv_ring_buffer_debug_info outbound;
317
318 if (!hv_dev->channel)
319 return -ENODEV;
320 hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
321 return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
322}
323static DEVICE_ATTR_RO(out_intr_mask);
324
325static ssize_t out_read_index_show(struct device *dev,
326 struct device_attribute *dev_attr, char *buf)
327{
328 struct hv_device *hv_dev = device_to_hv_device(dev);
329 struct hv_ring_buffer_debug_info outbound;
330
331 if (!hv_dev->channel)
332 return -ENODEV;
333 hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
334 return sprintf(buf, "%d\n", outbound.current_read_index);
335}
336static DEVICE_ATTR_RO(out_read_index);
337
338static ssize_t out_write_index_show(struct device *dev,
339 struct device_attribute *dev_attr,
340 char *buf)
341{
342 struct hv_device *hv_dev = device_to_hv_device(dev);
343 struct hv_ring_buffer_debug_info outbound;
344
345 if (!hv_dev->channel)
346 return -ENODEV;
347 hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
348 return sprintf(buf, "%d\n", outbound.current_write_index);
349}
350static DEVICE_ATTR_RO(out_write_index);
351
352static ssize_t out_read_bytes_avail_show(struct device *dev,
353 struct device_attribute *dev_attr,
354 char *buf)
355{
356 struct hv_device *hv_dev = device_to_hv_device(dev);
357 struct hv_ring_buffer_debug_info outbound;
358
359 if (!hv_dev->channel)
360 return -ENODEV;
361 hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
362 return sprintf(buf, "%d\n", outbound.bytes_avail_toread);
363}
364static DEVICE_ATTR_RO(out_read_bytes_avail);
365
366static ssize_t out_write_bytes_avail_show(struct device *dev,
367 struct device_attribute *dev_attr,
368 char *buf)
369{
370 struct hv_device *hv_dev = device_to_hv_device(dev);
371 struct hv_ring_buffer_debug_info outbound;
372
373 if (!hv_dev->channel)
374 return -ENODEV;
375 hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
376 return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
377}
378static DEVICE_ATTR_RO(out_write_bytes_avail);
379
380static ssize_t in_intr_mask_show(struct device *dev,
381 struct device_attribute *dev_attr, char *buf)
382{
383 struct hv_device *hv_dev = device_to_hv_device(dev);
384 struct hv_ring_buffer_debug_info inbound;
385
386 if (!hv_dev->channel)
387 return -ENODEV;
388 hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
389 return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
390}
391static DEVICE_ATTR_RO(in_intr_mask);
392
393static ssize_t in_read_index_show(struct device *dev,
394 struct device_attribute *dev_attr, char *buf)
395{
396 struct hv_device *hv_dev = device_to_hv_device(dev);
397 struct hv_ring_buffer_debug_info inbound;
398
399 if (!hv_dev->channel)
400 return -ENODEV;
401 hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
402 return sprintf(buf, "%d\n", inbound.current_read_index);
403}
404static DEVICE_ATTR_RO(in_read_index);
405
406static ssize_t in_write_index_show(struct device *dev,
407 struct device_attribute *dev_attr, char *buf)
408{
409 struct hv_device *hv_dev = device_to_hv_device(dev);
410 struct hv_ring_buffer_debug_info inbound;
411
412 if (!hv_dev->channel)
413 return -ENODEV;
414 hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
415 return sprintf(buf, "%d\n", inbound.current_write_index);
416}
417static DEVICE_ATTR_RO(in_write_index);
418
419static ssize_t in_read_bytes_avail_show(struct device *dev,
420 struct device_attribute *dev_attr,
421 char *buf)
422{
423 struct hv_device *hv_dev = device_to_hv_device(dev);
424 struct hv_ring_buffer_debug_info inbound;
425
426 if (!hv_dev->channel)
427 return -ENODEV;
428 hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
429 return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
430}
431static DEVICE_ATTR_RO(in_read_bytes_avail);
432
433static ssize_t in_write_bytes_avail_show(struct device *dev,
434 struct device_attribute *dev_attr,
435 char *buf)
436{
437 struct hv_device *hv_dev = device_to_hv_device(dev);
438 struct hv_ring_buffer_debug_info inbound;
439
440 if (!hv_dev->channel)
441 return -ENODEV;
442 hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
443 return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
444}
445static DEVICE_ATTR_RO(in_write_bytes_avail);
446
447/* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
Greg Kroah-Hartman03f3a912013-09-13 11:32:49 -0700448static struct attribute *vmbus_attrs[] = {
449 &dev_attr_id.attr,
Greg Kroah-Hartmana8fb5f32013-09-13 11:32:50 -0700450 &dev_attr_state.attr,
Greg Kroah-Hartman5ffd00e2013-09-13 11:32:51 -0700451 &dev_attr_monitor_id.attr,
Greg Kroah-Hartman68234c02013-09-13 11:32:53 -0700452 &dev_attr_class_id.attr,
Greg Kroah-Hartman7c55e1d2013-09-13 11:32:54 -0700453 &dev_attr_device_id.attr,
Greg Kroah-Hartman647fa372013-09-13 11:32:52 -0700454 &dev_attr_modalias.attr,
Greg Kroah-Hartman76c52bb2013-09-13 11:32:56 -0700455 &dev_attr_server_monitor_pending.attr,
456 &dev_attr_client_monitor_pending.attr,
Greg Kroah-Hartman1cee2722013-09-13 11:32:57 -0700457 &dev_attr_server_monitor_latency.attr,
458 &dev_attr_client_monitor_latency.attr,
Greg Kroah-Hartman4947c742013-09-13 11:32:58 -0700459 &dev_attr_server_monitor_conn_id.attr,
460 &dev_attr_client_monitor_conn_id.attr,
Greg Kroah-Hartman98f4c652013-09-13 11:33:01 -0700461 &dev_attr_out_intr_mask.attr,
462 &dev_attr_out_read_index.attr,
463 &dev_attr_out_write_index.attr,
464 &dev_attr_out_read_bytes_avail.attr,
465 &dev_attr_out_write_bytes_avail.attr,
466 &dev_attr_in_intr_mask.attr,
467 &dev_attr_in_read_index.attr,
468 &dev_attr_in_write_index.attr,
469 &dev_attr_in_read_bytes_avail.attr,
470 &dev_attr_in_write_bytes_avail.attr,
Greg Kroah-Hartman03f3a912013-09-13 11:32:49 -0700471 NULL,
472};
473ATTRIBUTE_GROUPS(vmbus);
474
K. Y. Srinivasanadde2482011-03-15 15:03:37 -0700475/*
476 * vmbus_uevent - add uevent for our device
477 *
478 * This routine is invoked when a device is added or removed on the vmbus to
479 * generate a uevent to udev in the userspace. The udev will then look at its
480 * rule and the uevent generated here to load the appropriate driver
K. Y. Srinivasan0ddda662011-08-25 09:48:38 -0700481 *
482 * The alias string will be of the form vmbus:guid where guid is the string
483 * representation of the device guid (each byte of the guid will be
484 * represented with two hex characters.
K. Y. Srinivasanadde2482011-03-15 15:03:37 -0700485 */
486static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
487{
488 struct hv_device *dev = device_to_hv_device(device);
Olaf Heringfd776ba2011-09-02 18:25:56 +0200489 int ret;
490 char alias_name[VMBUS_ALIAS_LEN + 1];
K. Y. Srinivasanadde2482011-03-15 15:03:37 -0700491
Olaf Heringfd776ba2011-09-02 18:25:56 +0200492 print_alias_name(dev, alias_name);
K. Y. Srinivasan0ddda662011-08-25 09:48:38 -0700493 ret = add_uevent_var(env, "MODALIAS=vmbus:%s", alias_name);
494 return ret;
K. Y. Srinivasanadde2482011-03-15 15:03:37 -0700495}
496
stephen hemminger1b9d48f2014-06-03 08:38:15 -0700497static const uuid_le null_guid;
K. Y. Srinivasan5841a822011-08-25 09:48:39 -0700498
499static inline bool is_null_guid(const __u8 *guid)
500{
501 if (memcmp(guid, &null_guid, sizeof(uuid_le)))
502 return false;
503 return true;
504}
505
K. Y. Srinivasan3037a7b2011-09-13 10:59:37 -0700506/*
507 * Return a matching hv_vmbus_device_id pointer.
508 * If there is no match, return NULL.
509 */
510static const struct hv_vmbus_device_id *hv_vmbus_get_id(
511 const struct hv_vmbus_device_id *id,
stephen hemminger1b9d48f2014-06-03 08:38:15 -0700512 const __u8 *guid)
K. Y. Srinivasan3037a7b2011-09-13 10:59:37 -0700513{
514 for (; !is_null_guid(id->guid); id++)
515 if (!memcmp(&id->guid, guid, sizeof(uuid_le)))
516 return id;
517
518 return NULL;
519}
520
521
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700522
523/*
524 * vmbus_match - Attempt to match the specified device to the specified driver
525 */
526static int vmbus_match(struct device *device, struct device_driver *driver)
527{
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700528 struct hv_driver *drv = drv_to_hv_drv(driver);
K. Y. Srinivasane8e27042011-06-06 15:50:04 -0700529 struct hv_device *hv_dev = device_to_hv_device(device);
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700530
K. Y. Srinivasan3037a7b2011-09-13 10:59:37 -0700531 if (hv_vmbus_get_id(drv->id_table, hv_dev->dev_type.b))
532 return 1;
K. Y. Srinivasande632a22011-04-26 09:20:24 -0700533
K. Y. Srinivasan5841a822011-08-25 09:48:39 -0700534 return 0;
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700535}
536
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700537/*
538 * vmbus_probe - Add the new vmbus's child device
539 */
540static int vmbus_probe(struct device *child_device)
541{
542 int ret = 0;
543 struct hv_driver *drv =
544 drv_to_hv_drv(child_device->driver);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700545 struct hv_device *dev = device_to_hv_device(child_device);
K. Y. Srinivasan84946892011-09-13 10:59:38 -0700546 const struct hv_vmbus_device_id *dev_id;
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700547
K. Y. Srinivasan84946892011-09-13 10:59:38 -0700548 dev_id = hv_vmbus_get_id(drv->id_table, dev->dev_type.b);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700549 if (drv->probe) {
K. Y. Srinivasan84946892011-09-13 10:59:38 -0700550 ret = drv->probe(dev, dev_id);
K. Y. Srinivasanb14a7b32011-04-29 13:45:03 -0700551 if (ret != 0)
Hank Janssen0a466182011-03-29 13:58:47 -0700552 pr_err("probe failed for device %s (%d)\n",
553 dev_name(child_device), ret);
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700554
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700555 } else {
Hank Janssen0a466182011-03-29 13:58:47 -0700556 pr_err("probe not set for driver %s\n",
557 dev_name(child_device));
K. Y. Srinivasan6de925b2011-06-06 15:50:07 -0700558 ret = -ENODEV;
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700559 }
560 return ret;
561}
562
K. Y. Srinivasanc5dce3d2011-03-15 15:03:40 -0700563/*
564 * vmbus_remove - Remove a vmbus device
565 */
566static int vmbus_remove(struct device *child_device)
567{
K. Y. Srinivasand15a0302015-02-28 11:18:16 -0800568 struct hv_driver *drv;
K. Y. Srinivasan415b0232011-04-29 13:45:12 -0700569 struct hv_device *dev = device_to_hv_device(child_device);
K. Y. Srinivasaned6cfcc2015-02-28 11:18:17 -0800570 u32 relid = dev->channel->offermsg.child_relid;
K. Y. Srinivasanc5dce3d2011-03-15 15:03:40 -0700571
K. Y. Srinivasand15a0302015-02-28 11:18:16 -0800572 if (child_device->driver) {
573 drv = drv_to_hv_drv(child_device->driver);
574 if (drv->remove)
575 drv->remove(dev);
K. Y. Srinivasaned6cfcc2015-02-28 11:18:17 -0800576 else {
577 hv_process_channel_removal(dev->channel, relid);
K. Y. Srinivasand15a0302015-02-28 11:18:16 -0800578 pr_err("remove not set for driver %s\n",
579 dev_name(child_device));
K. Y. Srinivasaned6cfcc2015-02-28 11:18:17 -0800580 }
581 } else {
582 /*
583 * We don't have a driver for this device; deal with the
584 * rescind message by removing the channel.
585 */
586 hv_process_channel_removal(dev->channel, relid);
K. Y. Srinivasand15a0302015-02-28 11:18:16 -0800587 }
K. Y. Srinivasanc5dce3d2011-03-15 15:03:40 -0700588
589 return 0;
590}
591
K. Y. Srinivasaneb1bb252011-03-15 15:03:41 -0700592
593/*
594 * vmbus_shutdown - Shutdown a vmbus device
595 */
596static void vmbus_shutdown(struct device *child_device)
597{
598 struct hv_driver *drv;
K. Y. Srinivasanca6887f2011-04-29 13:45:14 -0700599 struct hv_device *dev = device_to_hv_device(child_device);
K. Y. Srinivasaneb1bb252011-03-15 15:03:41 -0700600
601
602 /* The device may not be attached yet */
603 if (!child_device->driver)
604 return;
605
606 drv = drv_to_hv_drv(child_device->driver);
607
K. Y. Srinivasanca6887f2011-04-29 13:45:14 -0700608 if (drv->shutdown)
609 drv->shutdown(dev);
K. Y. Srinivasaneb1bb252011-03-15 15:03:41 -0700610
611 return;
612}
613
K. Y. Srinivasan086e7a52011-03-15 15:03:42 -0700614
615/*
616 * vmbus_device_release - Final callback release of the vmbus child device
617 */
618static void vmbus_device_release(struct device *device)
619{
K. Y. Srinivasane8e27042011-06-06 15:50:04 -0700620 struct hv_device *hv_dev = device_to_hv_device(device);
K. Y. Srinivasan086e7a52011-03-15 15:03:42 -0700621
K. Y. Srinivasane8e27042011-06-06 15:50:04 -0700622 kfree(hv_dev);
K. Y. Srinivasan086e7a52011-03-15 15:03:42 -0700623
624}
625
Bill Pemberton454f18a2009-07-27 16:47:24 -0400626/* The one and only one */
K. Y. Srinivasan9adcac52011-04-29 13:45:08 -0700627static struct bus_type hv_bus = {
628 .name = "vmbus",
629 .match = vmbus_match,
630 .shutdown = vmbus_shutdown,
631 .remove = vmbus_remove,
632 .probe = vmbus_probe,
633 .uevent = vmbus_uevent,
Greg Kroah-Hartman03f3a912013-09-13 11:32:49 -0700634 .dev_groups = vmbus_groups,
Hank Janssen3e7ee492009-07-13 16:02:34 -0700635};
636
Timo Teräsbf6506f2010-12-15 20:48:08 +0200637struct onmessage_work_context {
638 struct work_struct work;
639 struct hv_message msg;
640};
641
642static void vmbus_onmessage_work(struct work_struct *work)
643{
644 struct onmessage_work_context *ctx;
645
Vitaly Kuznetsov09a19622015-02-27 11:25:54 -0800646 /* Do not process messages if we're in DISCONNECTED state */
647 if (vmbus_connection.conn_state == DISCONNECTED)
648 return;
649
Timo Teräsbf6506f2010-12-15 20:48:08 +0200650 ctx = container_of(work, struct onmessage_work_context,
651 work);
652 vmbus_onmessage(&ctx->msg);
653 kfree(ctx);
654}
655
kbuild test robotd8a60e02015-01-26 01:17:54 +0800656static void hv_process_timer_expiration(struct hv_message *msg, int cpu)
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800657{
658 struct clock_event_device *dev = hv_context.clk_evt[cpu];
659
660 if (dev->event_handler)
661 dev->event_handler(dev);
662
663 msg->header.message_type = HVMSG_NONE;
664
665 /*
666 * Make sure the write to MessageType (ie set to
667 * HVMSG_NONE) happens before we read the
668 * MessagePending and EOMing. Otherwise, the EOMing
669 * will not deliver any more messages since there is
670 * no empty slot
671 */
672 mb();
673
674 if (msg->header.message_flags.msg_pending) {
675 /*
676 * This will cause message queue rescan to
677 * possibly deliver another msg from the
678 * hypervisor
679 */
680 wrmsrl(HV_X64_MSR_EOM, 0);
681 }
682}
683
K. Y. Srinivasan62c10592011-03-10 14:04:53 -0800684static void vmbus_on_msg_dpc(unsigned long data)
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800685{
686 int cpu = smp_processor_id();
687 void *page_addr = hv_context.synic_message_page[cpu];
688 struct hv_message *msg = (struct hv_message *)page_addr +
689 VMBUS_MESSAGE_SINT;
Dexuan Cui652594c2015-03-27 09:10:08 -0700690 struct vmbus_channel_message_header *hdr;
691 struct vmbus_channel_message_table_entry *entry;
Timo Teräsbf6506f2010-12-15 20:48:08 +0200692 struct onmessage_work_context *ctx;
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800693
694 while (1) {
Dexuan Cui652594c2015-03-27 09:10:08 -0700695 if (msg->header.message_type == HVMSG_NONE)
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800696 /* no msg */
697 break;
Dexuan Cui652594c2015-03-27 09:10:08 -0700698
699 hdr = (struct vmbus_channel_message_header *)msg->u.payload;
700
701 if (hdr->msgtype >= CHANNELMSG_COUNT) {
702 WARN_ONCE(1, "unknown msgtype=%d\n", hdr->msgtype);
703 goto msg_handled;
704 }
705
706 entry = &channel_message_table[hdr->msgtype];
707 if (entry->handler_type == VMHT_BLOCKING) {
Timo Teräsbf6506f2010-12-15 20:48:08 +0200708 ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
709 if (ctx == NULL)
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800710 continue;
Dexuan Cui652594c2015-03-27 09:10:08 -0700711
Timo Teräsbf6506f2010-12-15 20:48:08 +0200712 INIT_WORK(&ctx->work, vmbus_onmessage_work);
713 memcpy(&ctx->msg, msg, sizeof(*msg));
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800714
Dexuan Cui652594c2015-03-27 09:10:08 -0700715 queue_work(vmbus_connection.work_queue, &ctx->work);
716 } else
717 entry->message_handler(hdr);
718
719msg_handled:
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800720 msg->header.message_type = HVMSG_NONE;
721
722 /*
723 * Make sure the write to MessageType (ie set to
724 * HVMSG_NONE) happens before we read the
725 * MessagePending and EOMing. Otherwise, the EOMing
726 * will not deliver any more messages since there is
727 * no empty slot
728 */
Jason Wang35848f62013-06-18 13:04:23 +0800729 mb();
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800730
731 if (msg->header.message_flags.msg_pending) {
732 /*
733 * This will cause message queue rescan to
734 * possibly deliver another msg from the
735 * hypervisor
736 */
737 wrmsrl(HV_X64_MSR_EOM, 0);
738 }
739 }
740}
741
Thomas Gleixner76d388c2014-03-05 13:42:14 +0100742static void vmbus_isr(void)
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800743{
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800744 int cpu = smp_processor_id();
745 void *page_addr;
746 struct hv_message *msg;
747 union hv_synic_event_flags *event;
K. Y. Srinivasanae4636e2011-08-27 11:31:35 -0700748 bool handled = false;
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800749
K. Y. Srinivasan5ab05952012-12-01 06:46:55 -0800750 page_addr = hv_context.synic_event_page[cpu];
751 if (page_addr == NULL)
Thomas Gleixner76d388c2014-03-05 13:42:14 +0100752 return;
K. Y. Srinivasan5ab05952012-12-01 06:46:55 -0800753
754 event = (union hv_synic_event_flags *)page_addr +
755 VMBUS_MESSAGE_SINT;
K. Y. Srinivasan7341d902011-08-31 14:35:56 -0700756 /*
757 * Check for events before checking for messages. This is the order
758 * in which events and messages are checked in Windows guests on
759 * Hyper-V, and the Windows team suggested we do the same.
760 */
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800761
K. Y. Srinivasan6552ecd2012-12-01 06:46:49 -0800762 if ((vmbus_proto_version == VERSION_WS2008) ||
763 (vmbus_proto_version == VERSION_WIN7)) {
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800764
K. Y. Srinivasan6552ecd2012-12-01 06:46:49 -0800765 /* Since we are a child, we only need to check bit 0 */
766 if (sync_test_and_clear_bit(0,
767 (unsigned long *) &event->flags32[0])) {
768 handled = true;
769 }
770 } else {
771 /*
772 * Our host is win8 or above. The signaling mechanism
773 * has changed and we can directly look at the event page.
774 * If bit n is set then we have an interrup on the channel
775 * whose id is n.
776 */
K. Y. Srinivasanae4636e2011-08-27 11:31:35 -0700777 handled = true;
K. Y. Srinivasan793be9c2011-03-15 15:03:43 -0700778 }
K. Y. Srinivasanae4636e2011-08-27 11:31:35 -0700779
K. Y. Srinivasan6552ecd2012-12-01 06:46:49 -0800780 if (handled)
K. Y. Srinivasandb11f122012-12-01 06:46:53 -0800781 tasklet_schedule(hv_context.event_dpc[cpu]);
K. Y. Srinivasan6552ecd2012-12-01 06:46:49 -0800782
783
K. Y. Srinivasan7341d902011-08-31 14:35:56 -0700784 page_addr = hv_context.synic_message_page[cpu];
785 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
786
787 /* Check if there are actual msgs to be processed */
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800788 if (msg->header.message_type != HVMSG_NONE) {
789 if (msg->header.message_type == HVMSG_TIMER_EXPIRED)
790 hv_process_timer_expiration(msg, cpu);
791 else
792 tasklet_schedule(&msg_dpc);
793 }
K. Y. Srinivasan793be9c2011-03-15 15:03:43 -0700794}
795
Vitaly Kuznetsove5132292015-02-27 11:25:51 -0800796#ifdef CONFIG_HOTPLUG_CPU
797static int hyperv_cpu_disable(void)
798{
799 return -ENOSYS;
800}
801
802static void hv_cpu_hotplug_quirk(bool vmbus_loaded)
803{
804 static void *previous_cpu_disable;
805
806 /*
807 * Offlining a CPU when running on newer hypervisors (WS2012R2, Win8,
808 * ...) is not supported at this moment as channel interrupts are
809 * distributed across all of them.
810 */
811
812 if ((vmbus_proto_version == VERSION_WS2008) ||
813 (vmbus_proto_version == VERSION_WIN7))
814 return;
815
816 if (vmbus_loaded) {
817 previous_cpu_disable = smp_ops.cpu_disable;
818 smp_ops.cpu_disable = hyperv_cpu_disable;
819 pr_notice("CPU offlining is not supported by hypervisor\n");
820 } else if (previous_cpu_disable)
821 smp_ops.cpu_disable = previous_cpu_disable;
822}
823#else
824static void hv_cpu_hotplug_quirk(bool vmbus_loaded)
825{
826}
827#endif
828
Hank Janssen3e189512010-03-04 22:11:00 +0000829/*
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700830 * vmbus_bus_init -Main vmbus driver initialization routine.
831 *
832 * Here, we
Lars Lindley0686e4f2010-03-11 23:51:23 +0100833 * - initialize the vmbus driver context
Lars Lindley0686e4f2010-03-11 23:51:23 +0100834 * - invoke the vmbus hv main init routine
835 * - get the irq resource
Lars Lindley0686e4f2010-03-11 23:51:23 +0100836 * - retrieve the channel offers
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700837 */
K. Y. Srinivasan9aaa9952011-06-06 15:49:37 -0700838static int vmbus_bus_init(int irq)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700839{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700840 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700841
Greg Kroah-Hartman6d26e38fa22010-12-02 12:08:08 -0800842 /* Hypervisor initialization...setup hypercall page..etc */
843 ret = hv_init();
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700844 if (ret != 0) {
Hank Janssen0a466182011-03-29 13:58:47 -0700845 pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
K. Y. Srinivasand6c1c5d2011-06-06 15:50:08 -0700846 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700847 }
848
K. Y. Srinivasan59c0e4f2011-04-29 13:45:06 -0700849 tasklet_init(&msg_dpc, vmbus_on_msg_dpc, 0);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700850
K. Y. Srinivasan9adcac52011-04-29 13:45:08 -0700851 ret = bus_register(&hv_bus);
K. Y. Srinivasand6c1c5d2011-06-06 15:50:08 -0700852 if (ret)
K. Y. Srinivasan8b9987e2011-08-31 14:35:55 -0700853 goto err_cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700854
Thomas Gleixner76d388c2014-03-05 13:42:14 +0100855 hv_setup_vmbus_irq(vmbus_isr);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700856
Jason Wang2608fb62013-06-19 11:28:10 +0800857 ret = hv_synic_alloc();
858 if (ret)
859 goto err_alloc;
K. Y. Srinivasan800b6902011-03-15 15:03:33 -0700860 /*
K. Y. Srinivasan302a3c02013-02-17 11:30:44 -0800861 * Initialize the per-cpu interrupt state and
K. Y. Srinivasan800b6902011-03-15 15:03:33 -0700862 * connect to the host.
863 */
K. Y. Srinivasan302a3c02013-02-17 11:30:44 -0800864 on_each_cpu(hv_synic_init, NULL, 1);
K. Y. Srinivasan800b6902011-03-15 15:03:33 -0700865 ret = vmbus_connect();
K. Y. Srinivasan8b9987e2011-08-31 14:35:55 -0700866 if (ret)
Jason Wang2608fb62013-06-19 11:28:10 +0800867 goto err_alloc;
K. Y. Srinivasan800b6902011-03-15 15:03:33 -0700868
Vitaly Kuznetsove5132292015-02-27 11:25:51 -0800869 hv_cpu_hotplug_quirk(true);
Nick Meier96c1d052015-02-28 11:39:01 -0800870
871 /*
872 * Only register if the crash MSRs are available
873 */
Denis V. Lunevcc2dd402015-08-01 16:08:20 -0700874 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -0700875 register_die_notifier(&hyperv_die_block);
Nick Meier96c1d052015-02-28 11:39:01 -0800876 atomic_notifier_chain_register(&panic_notifier_list,
877 &hyperv_panic_block);
878 }
879
Greg Kroah-Hartman2d6e8822010-12-02 08:50:58 -0800880 vmbus_request_offers();
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +0000881
K. Y. Srinivasand6c1c5d2011-06-06 15:50:08 -0700882 return 0;
K. Y. Srinivasan8b9987e2011-08-31 14:35:55 -0700883
Jason Wang2608fb62013-06-19 11:28:10 +0800884err_alloc:
885 hv_synic_free();
Thomas Gleixner76d388c2014-03-05 13:42:14 +0100886 hv_remove_vmbus_irq();
K. Y. Srinivasan8b9987e2011-08-31 14:35:55 -0700887
K. Y. Srinivasan8b9987e2011-08-31 14:35:55 -0700888 bus_unregister(&hv_bus);
889
890err_cleanup:
891 hv_cleanup();
892
893 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700894}
895
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700896/**
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -0700897 * __vmbus_child_driver_register - Register a vmbus's driver
898 * @drv: Pointer to driver structure you want to register
899 * @owner: owner module of the drv
900 * @mod_name: module name string
Hank Janssen3e189512010-03-04 22:11:00 +0000901 *
902 * Registers the given driver with Linux through the 'driver_register()' call
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -0700903 * and sets up the hyper-v vmbus handling for this driver.
Hank Janssen3e189512010-03-04 22:11:00 +0000904 * It will return the state of the 'driver_register()' call.
905 *
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700906 */
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -0700907int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700908{
Bill Pemberton5d48a1c2009-07-27 16:47:36 -0400909 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700910
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -0700911 pr_info("registering driver %s\n", hv_driver->name);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700912
K. Y. Srinivasancf6a2ea2011-12-01 09:59:34 -0800913 ret = vmbus_exists();
914 if (ret < 0)
915 return ret;
916
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -0700917 hv_driver->driver.name = hv_driver->name;
918 hv_driver->driver.owner = owner;
919 hv_driver->driver.mod_name = mod_name;
920 hv_driver->driver.bus = &hv_bus;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700921
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -0700922 ret = driver_register(&hv_driver->driver);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700923
Bill Pemberton5d48a1c2009-07-27 16:47:36 -0400924 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700925}
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -0700926EXPORT_SYMBOL_GPL(__vmbus_driver_register);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700927
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700928/**
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -0700929 * vmbus_driver_unregister() - Unregister a vmbus's driver
930 * @drv: Pointer to driver structure you want to un-register
Hank Janssen3e189512010-03-04 22:11:00 +0000931 *
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -0700932 * Un-register the given driver that was previous registered with a call to
933 * vmbus_driver_register()
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700934 */
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -0700935void vmbus_driver_unregister(struct hv_driver *hv_driver)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700936{
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -0700937 pr_info("unregistering driver %s\n", hv_driver->name);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700938
K. Y. Srinivasancf6a2ea2011-12-01 09:59:34 -0800939 if (!vmbus_exists())
K. Y. Srinivasan8f257a12011-12-27 13:49:37 -0800940 driver_unregister(&hv_driver->driver);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700941}
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -0700942EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700943
Hank Janssen3e189512010-03-04 22:11:00 +0000944/*
K. Y. Srinivasanf2c73012011-09-08 07:24:12 -0700945 * vmbus_device_create - Creates and registers a new child device
Hank Janssen3e189512010-03-04 22:11:00 +0000946 * on the vmbus.
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700947 */
stephen hemminger1b9d48f2014-06-03 08:38:15 -0700948struct hv_device *vmbus_device_create(const uuid_le *type,
949 const uuid_le *instance,
950 struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700951{
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200952 struct hv_device *child_device_obj;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700953
K. Y. Srinivasan6bad88da2011-03-07 13:35:48 -0800954 child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
955 if (!child_device_obj) {
Hank Janssen0a466182011-03-29 13:58:47 -0700956 pr_err("Unable to allocate device object for child device\n");
Hank Janssen3e7ee492009-07-13 16:02:34 -0700957 return NULL;
958 }
959
Greg Kroah-Hartmancae5b842010-10-21 09:05:27 -0700960 child_device_obj->channel = channel;
K. Y. Srinivasan358d2ee2011-08-25 09:48:28 -0700961 memcpy(&child_device_obj->dev_type, type, sizeof(uuid_le));
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800962 memcpy(&child_device_obj->dev_instance, instance,
K. Y. Srinivasan358d2ee2011-08-25 09:48:28 -0700963 sizeof(uuid_le));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700964
Hank Janssen3e7ee492009-07-13 16:02:34 -0700965
Hank Janssen3e7ee492009-07-13 16:02:34 -0700966 return child_device_obj;
967}
968
Hank Janssen3e189512010-03-04 22:11:00 +0000969/*
K. Y. Srinivasan227942812011-09-08 07:24:13 -0700970 * vmbus_device_register - Register the child device
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700971 */
K. Y. Srinivasan227942812011-09-08 07:24:13 -0700972int vmbus_device_register(struct hv_device *child_device_obj)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700973{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700974 int ret = 0;
K. Y. Srinivasan6bad88da2011-03-07 13:35:48 -0800975
Vitaly Kuznetsovbc63b6f2015-02-27 11:25:52 -0800976 dev_set_name(&child_device_obj->device, "vmbus_%d",
977 child_device_obj->channel->id);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700978
K. Y. Srinivasan0bce28b2011-08-27 11:31:39 -0700979 child_device_obj->device.bus = &hv_bus;
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -0700980 child_device_obj->device.parent = &hv_acpi_dev->dev;
K. Y. Srinivasan6bad88da2011-03-07 13:35:48 -0800981 child_device_obj->device.release = vmbus_device_release;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700982
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700983 /*
984 * Register with the LDM. This will kick off the driver/device
985 * binding...which will eventually call vmbus_match() and vmbus_probe()
986 */
K. Y. Srinivasan6bad88da2011-03-07 13:35:48 -0800987 ret = device_register(&child_device_obj->device);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700988
Hank Janssen3e7ee492009-07-13 16:02:34 -0700989 if (ret)
Hank Janssen0a466182011-03-29 13:58:47 -0700990 pr_err("Unable to register child device\n");
Hank Janssen3e7ee492009-07-13 16:02:34 -0700991 else
Fernando Soto84672362013-06-14 23:13:35 +0000992 pr_debug("child device %s registered\n",
Hank Janssen0a466182011-03-29 13:58:47 -0700993 dev_name(&child_device_obj->device));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700994
Hank Janssen3e7ee492009-07-13 16:02:34 -0700995 return ret;
996}
997
Hank Janssen3e189512010-03-04 22:11:00 +0000998/*
K. Y. Srinivasan696453b2011-09-08 07:24:14 -0700999 * vmbus_device_unregister - Remove the specified child device
Hank Janssen3e189512010-03-04 22:11:00 +00001000 * from the vmbus.
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001001 */
K. Y. Srinivasan696453b2011-09-08 07:24:14 -07001002void vmbus_device_unregister(struct hv_device *device_obj)
Hank Janssen3e7ee492009-07-13 16:02:34 -07001003{
Fernando Soto84672362013-06-14 23:13:35 +00001004 pr_debug("child device %s unregistered\n",
1005 dev_name(&device_obj->device));
1006
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001007 /*
1008 * Kick off the process of unregistering the device.
1009 * This will call vmbus_remove() and eventually vmbus_device_release()
1010 */
K. Y. Srinivasan6bad88da2011-03-07 13:35:48 -08001011 device_unregister(&device_obj->device);
Hank Janssen3e7ee492009-07-13 16:02:34 -07001012}
1013
Hank Janssen3e7ee492009-07-13 16:02:34 -07001014
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001015/*
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08001016 * VMBUS is an acpi enumerated device. Get the the information we
1017 * need from DSDT.
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001018 */
1019
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08001020static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001021{
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08001022 switch (res->type) {
1023 case ACPI_RESOURCE_TYPE_IRQ:
1024 irq = res->data.irq.interrupts[0];
Gerd Hoffmann4eb923f2014-02-24 14:17:08 +01001025 break;
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001026
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08001027 case ACPI_RESOURCE_TYPE_ADDRESS64:
Lv Zhenga45de932015-01-26 16:58:56 +08001028 hyperv_mmio.start = res->data.address64.address.minimum;
1029 hyperv_mmio.end = res->data.address64.address.maximum;
Gerd Hoffmann4eb923f2014-02-24 14:17:08 +01001030 break;
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001031 }
1032
1033 return AE_OK;
1034}
1035
1036static int vmbus_acpi_add(struct acpi_device *device)
1037{
1038 acpi_status result;
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08001039 int ret_val = -ENODEV;
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001040
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -07001041 hv_acpi_dev = device;
1042
K. Y. Srinivasan0a4425b2011-08-27 11:31:38 -07001043 result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08001044 vmbus_walk_resources, NULL);
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001045
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08001046 if (ACPI_FAILURE(result))
1047 goto acpi_walk_err;
1048 /*
1049 * The parent of the vmbus acpi device (Gen2 firmware) is the VMOD that
1050 * has the mmio ranges. Get that.
1051 */
1052 if (device->parent) {
1053 result = acpi_walk_resources(device->parent->handle,
1054 METHOD_NAME__CRS,
1055 vmbus_walk_resources, NULL);
1056
1057 if (ACPI_FAILURE(result))
1058 goto acpi_walk_err;
Gerd Hoffmann90eedf02014-02-24 14:17:09 +01001059 if (hyperv_mmio.start && hyperv_mmio.end)
1060 request_resource(&iomem_resource, &hyperv_mmio);
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001061 }
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08001062 ret_val = 0;
1063
1064acpi_walk_err:
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001065 complete(&probe_event);
K. Y. Srinivasan90f34532014-01-29 18:14:39 -08001066 return ret_val;
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001067}
1068
Vitaly Kuznetsove4ecb412015-04-22 21:31:28 -07001069static int vmbus_acpi_remove(struct acpi_device *device)
1070{
1071 int ret = 0;
1072
1073 if (hyperv_mmio.start && hyperv_mmio.end)
1074 ret = release_resource(&hyperv_mmio);
1075 return ret;
1076}
1077
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001078static const struct acpi_device_id vmbus_acpi_device_ids[] = {
1079 {"VMBUS", 0},
K. Y. Srinivasan9d7b18d2011-06-06 15:49:42 -07001080 {"VMBus", 0},
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001081 {"", 0},
1082};
1083MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
1084
1085static struct acpi_driver vmbus_acpi_driver = {
1086 .name = "vmbus",
1087 .ids = vmbus_acpi_device_ids,
1088 .ops = {
1089 .add = vmbus_acpi_add,
Vitaly Kuznetsove4ecb412015-04-22 21:31:28 -07001090 .remove = vmbus_acpi_remove,
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001091 },
1092};
1093
Vitaly Kuznetsov25172812015-08-01 16:08:07 -07001094static void hv_kexec_handler(void)
1095{
1096 int cpu;
1097
1098 hv_synic_clockevents_cleanup();
1099 vmbus_initiate_unload();
1100 for_each_online_cpu(cpu)
1101 smp_call_function_single(cpu, hv_synic_cleanup, NULL, 1);
1102 hv_cleanup();
1103};
1104
Vitaly Kuznetsovb4370df2015-08-01 16:08:09 -07001105static void hv_crash_handler(struct pt_regs *regs)
1106{
1107 vmbus_initiate_unload();
1108 /*
1109 * In crash handler we can't schedule synic cleanup for all CPUs,
1110 * doing the cleanup for current CPU only. This should be sufficient
1111 * for kdump.
1112 */
1113 hv_synic_cleanup(NULL);
1114 hv_cleanup();
1115};
1116
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -07001117static int __init hv_acpi_init(void)
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -07001118{
K. Y. Srinivasan2dda95f2011-07-15 13:38:56 -07001119 int ret, t;
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001120
Jason Wang1f94ea82012-08-31 13:32:44 +08001121 if (x86_hyper != &x86_hyper_ms_hyperv)
Jason Wang05929692012-08-17 18:52:43 +08001122 return -ENODEV;
1123
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001124 init_completion(&probe_event);
1125
1126 /*
1127 * Get irq resources first.
1128 */
K. Y. Srinivasan02466042011-06-06 15:49:40 -07001129 ret = acpi_bus_register_driver(&vmbus_acpi_driver);
1130
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001131 if (ret)
1132 return ret;
1133
K. Y. Srinivasan2dda95f2011-07-15 13:38:56 -07001134 t = wait_for_completion_timeout(&probe_event, 5*HZ);
1135 if (t == 0) {
1136 ret = -ETIMEDOUT;
1137 goto cleanup;
1138 }
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001139
1140 if (irq <= 0) {
K. Y. Srinivasan2dda95f2011-07-15 13:38:56 -07001141 ret = -ENODEV;
1142 goto cleanup;
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -07001143 }
1144
K. Y. Srinivasan91fd7992011-06-16 13:16:38 -07001145 ret = vmbus_bus_init(irq);
1146 if (ret)
K. Y. Srinivasan2dda95f2011-07-15 13:38:56 -07001147 goto cleanup;
1148
Vitaly Kuznetsov25172812015-08-01 16:08:07 -07001149 hv_setup_kexec_handler(hv_kexec_handler);
Vitaly Kuznetsovb4370df2015-08-01 16:08:09 -07001150 hv_setup_crash_handler(hv_crash_handler);
Vitaly Kuznetsov25172812015-08-01 16:08:07 -07001151
K. Y. Srinivasan2dda95f2011-07-15 13:38:56 -07001152 return 0;
1153
1154cleanup:
1155 acpi_bus_unregister_driver(&vmbus_acpi_driver);
K. Y. Srinivasancf6a2ea2011-12-01 09:59:34 -08001156 hv_acpi_dev = NULL;
K. Y. Srinivasan91fd7992011-06-16 13:16:38 -07001157 return ret;
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -07001158}
1159
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08001160static void __exit vmbus_exit(void)
1161{
Vitaly Kuznetsove72e7ac2015-02-27 11:25:55 -08001162 int cpu;
1163
Vitaly Kuznetsov25172812015-08-01 16:08:07 -07001164 hv_remove_kexec_handler();
Vitaly Kuznetsovb4370df2015-08-01 16:08:09 -07001165 hv_remove_crash_handler();
Vitaly Kuznetsov09a19622015-02-27 11:25:54 -08001166 vmbus_connection.conn_state = DISCONNECTED;
Vitaly Kuznetsove0867482015-02-27 11:25:57 -08001167 hv_synic_clockevents_cleanup();
K. Y. Srinivasan2db84ef2015-04-22 21:31:32 -07001168 vmbus_disconnect();
Thomas Gleixner76d388c2014-03-05 13:42:14 +01001169 hv_remove_vmbus_irq();
Vitaly Kuznetsov1959a282015-05-06 17:47:41 -07001170 tasklet_kill(&msg_dpc);
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08001171 vmbus_free_channels();
Denis V. Lunevcc2dd402015-08-01 16:08:20 -07001172 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
Vitaly Kuznetsov510f7ae2015-08-01 16:08:10 -07001173 unregister_die_notifier(&hyperv_die_block);
Vitaly Kuznetsov096c6052015-04-22 21:31:29 -07001174 atomic_notifier_chain_unregister(&panic_notifier_list,
1175 &hyperv_panic_block);
1176 }
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08001177 bus_unregister(&hv_bus);
1178 hv_cleanup();
Vitaly Kuznetsov1959a282015-05-06 17:47:41 -07001179 for_each_online_cpu(cpu) {
1180 tasklet_kill(hv_context.event_dpc[cpu]);
Vitaly Kuznetsove72e7ac2015-02-27 11:25:55 -08001181 smp_call_function_single(cpu, hv_synic_cleanup, NULL, 1);
Vitaly Kuznetsov1959a282015-05-06 17:47:41 -07001182 }
Vitaly Kuznetsov06210b42015-08-01 16:08:05 -07001183 hv_synic_free();
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08001184 acpi_bus_unregister_driver(&vmbus_acpi_driver);
Vitaly Kuznetsove5132292015-02-27 11:25:51 -08001185 hv_cpu_hotplug_quirk(false);
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08001186}
1187
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -07001188
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -07001189MODULE_LICENSE("GPL");
Hank Janssen3e7ee492009-07-13 16:02:34 -07001190
K. Y. Srinivasan43d4e112011-10-24 11:28:12 -07001191subsys_initcall(hv_acpi_init);
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -08001192module_exit(vmbus_exit);