blob: 1c949f5fb716417c04dccda368bda7050f8b6401 [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>
28#include <linux/irq.h>
29#include <linux/interrupt.h>
30#include <linux/sysctl.h>
Greg Kroah-Hartman9a775db2010-02-25 16:42:10 -080031#include <linux/pci.h>
Greg Kroah-Hartmanc22090f2010-02-25 16:43:15 -080032#include <linux/dmi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -070034#include <linux/acpi.h>
35#include <acpi/acpi_bus.h>
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +000036#include <linux/completion.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070037
38#include "hyperv.h"
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070039#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070040
Hank Janssen3e7ee492009-07-13 16:02:34 -070041
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. Srinivasan59c0e4f2011-04-29 13:45:06 -070044static struct tasklet_struct msg_dpc;
K. Y. Srinivasan66d92292011-04-29 13:45:07 -070045static struct tasklet_struct event_dpc;
K. Y. Srinivasan59c0e4f2011-04-29 13:45:06 -070046
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -070047unsigned int vmbus_loglevel = (ALL_MODULES << 16 | INFO_LVL);
Hank Janssen3e7ee492009-07-13 16:02:34 -070048EXPORT_SYMBOL(vmbus_loglevel);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -070049 /* (ALL_MODULES << 16 | DEBUG_LVL_ENTEREXIT); */
50 /* (((VMBUS | VMBUS_DRV)<<16) | DEBUG_LVL_ENTEREXIT); */
Hank Janssen3e7ee492009-07-13 16:02:34 -070051
K. Y. Srinivasan71a66552011-04-29 13:45:04 -070052static struct completion probe_event;
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -070053static int irq;
Hank Janssen3e7ee492009-07-13 16:02:34 -070054
K. Y. Srinivasan98db4332011-03-15 15:03:44 -070055static void get_channel_info(struct hv_device *device,
56 struct hv_device_info *info)
57{
58 struct vmbus_channel_debug_info debug_info;
59
60 if (!device->channel)
61 return;
62
63 vmbus_get_debug_info(device->channel, &debug_info);
64
65 info->chn_id = debug_info.relid;
66 info->chn_state = debug_info.state;
67 memcpy(&info->chn_type, &debug_info.interfacetype,
68 sizeof(struct hv_guid));
69 memcpy(&info->chn_instance, &debug_info.interface_instance,
70 sizeof(struct hv_guid));
71
72 info->monitor_id = debug_info.monitorid;
73
74 info->server_monitor_pending = debug_info.servermonitor_pending;
75 info->server_monitor_latency = debug_info.servermonitor_latency;
76 info->server_monitor_conn_id = debug_info.servermonitor_connectionid;
77
78 info->client_monitor_pending = debug_info.clientmonitor_pending;
79 info->client_monitor_latency = debug_info.clientmonitor_latency;
80 info->client_monitor_conn_id = debug_info.clientmonitor_connectionid;
81
82 info->inbound.int_mask = debug_info.inbound.current_interrupt_mask;
83 info->inbound.read_idx = debug_info.inbound.current_read_index;
84 info->inbound.write_idx = debug_info.inbound.current_write_index;
85 info->inbound.bytes_avail_toread =
86 debug_info.inbound.bytes_avail_toread;
87 info->inbound.bytes_avail_towrite =
88 debug_info.inbound.bytes_avail_towrite;
89
90 info->outbound.int_mask =
91 debug_info.outbound.current_interrupt_mask;
92 info->outbound.read_idx = debug_info.outbound.current_read_index;
93 info->outbound.write_idx = debug_info.outbound.current_write_index;
94 info->outbound.bytes_avail_toread =
95 debug_info.outbound.bytes_avail_toread;
96 info->outbound.bytes_avail_towrite =
97 debug_info.outbound.bytes_avail_towrite;
98}
99
100/*
101 * vmbus_show_device_attr - Show the device attribute in sysfs.
102 *
103 * This is invoked when user does a
104 * "cat /sys/bus/vmbus/devices/<busdevice>/<attr name>"
105 */
106static ssize_t vmbus_show_device_attr(struct device *dev,
107 struct device_attribute *dev_attr,
108 char *buf)
109{
K. Y. Srinivasane8e27042011-06-06 15:50:04 -0700110 struct hv_device *hv_dev = device_to_hv_device(dev);
K. Y. Srinivasan98db4332011-03-15 15:03:44 -0700111 struct hv_device_info device_info;
112
113 memset(&device_info, 0, sizeof(struct hv_device_info));
114
K. Y. Srinivasane8e27042011-06-06 15:50:04 -0700115 get_channel_info(hv_dev, &device_info);
K. Y. Srinivasan98db4332011-03-15 15:03:44 -0700116
117 if (!strcmp(dev_attr->attr.name, "class_id")) {
118 return sprintf(buf, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-"
119 "%02x%02x%02x%02x%02x%02x%02x%02x}\n",
120 device_info.chn_type.data[3],
121 device_info.chn_type.data[2],
122 device_info.chn_type.data[1],
123 device_info.chn_type.data[0],
124 device_info.chn_type.data[5],
125 device_info.chn_type.data[4],
126 device_info.chn_type.data[7],
127 device_info.chn_type.data[6],
128 device_info.chn_type.data[8],
129 device_info.chn_type.data[9],
130 device_info.chn_type.data[10],
131 device_info.chn_type.data[11],
132 device_info.chn_type.data[12],
133 device_info.chn_type.data[13],
134 device_info.chn_type.data[14],
135 device_info.chn_type.data[15]);
136 } else if (!strcmp(dev_attr->attr.name, "device_id")) {
137 return sprintf(buf, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-"
138 "%02x%02x%02x%02x%02x%02x%02x%02x}\n",
139 device_info.chn_instance.data[3],
140 device_info.chn_instance.data[2],
141 device_info.chn_instance.data[1],
142 device_info.chn_instance.data[0],
143 device_info.chn_instance.data[5],
144 device_info.chn_instance.data[4],
145 device_info.chn_instance.data[7],
146 device_info.chn_instance.data[6],
147 device_info.chn_instance.data[8],
148 device_info.chn_instance.data[9],
149 device_info.chn_instance.data[10],
150 device_info.chn_instance.data[11],
151 device_info.chn_instance.data[12],
152 device_info.chn_instance.data[13],
153 device_info.chn_instance.data[14],
154 device_info.chn_instance.data[15]);
155 } else if (!strcmp(dev_attr->attr.name, "state")) {
156 return sprintf(buf, "%d\n", device_info.chn_state);
157 } else if (!strcmp(dev_attr->attr.name, "id")) {
158 return sprintf(buf, "%d\n", device_info.chn_id);
159 } else if (!strcmp(dev_attr->attr.name, "out_intr_mask")) {
160 return sprintf(buf, "%d\n", device_info.outbound.int_mask);
161 } else if (!strcmp(dev_attr->attr.name, "out_read_index")) {
162 return sprintf(buf, "%d\n", device_info.outbound.read_idx);
163 } else if (!strcmp(dev_attr->attr.name, "out_write_index")) {
164 return sprintf(buf, "%d\n", device_info.outbound.write_idx);
165 } else if (!strcmp(dev_attr->attr.name, "out_read_bytes_avail")) {
166 return sprintf(buf, "%d\n",
167 device_info.outbound.bytes_avail_toread);
168 } else if (!strcmp(dev_attr->attr.name, "out_write_bytes_avail")) {
169 return sprintf(buf, "%d\n",
170 device_info.outbound.bytes_avail_towrite);
171 } else if (!strcmp(dev_attr->attr.name, "in_intr_mask")) {
172 return sprintf(buf, "%d\n", device_info.inbound.int_mask);
173 } else if (!strcmp(dev_attr->attr.name, "in_read_index")) {
174 return sprintf(buf, "%d\n", device_info.inbound.read_idx);
175 } else if (!strcmp(dev_attr->attr.name, "in_write_index")) {
176 return sprintf(buf, "%d\n", device_info.inbound.write_idx);
177 } else if (!strcmp(dev_attr->attr.name, "in_read_bytes_avail")) {
178 return sprintf(buf, "%d\n",
179 device_info.inbound.bytes_avail_toread);
180 } else if (!strcmp(dev_attr->attr.name, "in_write_bytes_avail")) {
181 return sprintf(buf, "%d\n",
182 device_info.inbound.bytes_avail_towrite);
183 } else if (!strcmp(dev_attr->attr.name, "monitor_id")) {
184 return sprintf(buf, "%d\n", device_info.monitor_id);
185 } else if (!strcmp(dev_attr->attr.name, "server_monitor_pending")) {
186 return sprintf(buf, "%d\n", device_info.server_monitor_pending);
187 } else if (!strcmp(dev_attr->attr.name, "server_monitor_latency")) {
188 return sprintf(buf, "%d\n", device_info.server_monitor_latency);
189 } else if (!strcmp(dev_attr->attr.name, "server_monitor_conn_id")) {
190 return sprintf(buf, "%d\n",
191 device_info.server_monitor_conn_id);
192 } else if (!strcmp(dev_attr->attr.name, "client_monitor_pending")) {
193 return sprintf(buf, "%d\n", device_info.client_monitor_pending);
194 } else if (!strcmp(dev_attr->attr.name, "client_monitor_latency")) {
195 return sprintf(buf, "%d\n", device_info.client_monitor_latency);
196 } else if (!strcmp(dev_attr->attr.name, "client_monitor_conn_id")) {
197 return sprintf(buf, "%d\n",
198 device_info.client_monitor_conn_id);
199 } else {
200 return 0;
201 }
202}
203
Bill Pemberton454f18a2009-07-27 16:47:24 -0400204/* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700205static struct device_attribute vmbus_device_attrs[] = {
206 __ATTR(id, S_IRUGO, vmbus_show_device_attr, NULL),
207 __ATTR(state, S_IRUGO, vmbus_show_device_attr, NULL),
208 __ATTR(class_id, S_IRUGO, vmbus_show_device_attr, NULL),
209 __ATTR(device_id, S_IRUGO, vmbus_show_device_attr, NULL),
210 __ATTR(monitor_id, S_IRUGO, vmbus_show_device_attr, NULL),
211
212 __ATTR(server_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL),
213 __ATTR(server_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL),
214 __ATTR(server_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL),
215
216 __ATTR(client_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL),
217 __ATTR(client_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL),
218 __ATTR(client_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL),
219
220 __ATTR(out_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL),
221 __ATTR(out_read_index, S_IRUGO, vmbus_show_device_attr, NULL),
222 __ATTR(out_write_index, S_IRUGO, vmbus_show_device_attr, NULL),
223 __ATTR(out_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
224 __ATTR(out_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
225
226 __ATTR(in_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL),
227 __ATTR(in_read_index, S_IRUGO, vmbus_show_device_attr, NULL),
228 __ATTR(in_write_index, S_IRUGO, vmbus_show_device_attr, NULL),
229 __ATTR(in_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
230 __ATTR(in_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
231 __ATTR_NULL
232};
Hank Janssen3e7ee492009-07-13 16:02:34 -0700233
K. Y. Srinivasan793be9c2011-03-15 15:03:43 -0700234
K. Y. Srinivasanadde2482011-03-15 15:03:37 -0700235/*
236 * vmbus_uevent - add uevent for our device
237 *
238 * This routine is invoked when a device is added or removed on the vmbus to
239 * generate a uevent to udev in the userspace. The udev will then look at its
240 * rule and the uevent generated here to load the appropriate driver
241 */
242static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
243{
244 struct hv_device *dev = device_to_hv_device(device);
245 int ret;
246
K. Y. Srinivasanadde2482011-03-15 15:03:37 -0700247 ret = add_uevent_var(env, "VMBUS_DEVICE_CLASS_GUID={"
248 "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
249 "%02x%02x%02x%02x%02x%02x%02x%02x}",
250 dev->dev_type.data[3],
251 dev->dev_type.data[2],
252 dev->dev_type.data[1],
253 dev->dev_type.data[0],
254 dev->dev_type.data[5],
255 dev->dev_type.data[4],
256 dev->dev_type.data[7],
257 dev->dev_type.data[6],
258 dev->dev_type.data[8],
259 dev->dev_type.data[9],
260 dev->dev_type.data[10],
261 dev->dev_type.data[11],
262 dev->dev_type.data[12],
263 dev->dev_type.data[13],
264 dev->dev_type.data[14],
265 dev->dev_type.data[15]);
266
267 if (ret)
268 return ret;
269
270 ret = add_uevent_var(env, "VMBUS_DEVICE_DEVICE_GUID={"
271 "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
272 "%02x%02x%02x%02x%02x%02x%02x%02x}",
273 dev->dev_instance.data[3],
274 dev->dev_instance.data[2],
275 dev->dev_instance.data[1],
276 dev->dev_instance.data[0],
277 dev->dev_instance.data[5],
278 dev->dev_instance.data[4],
279 dev->dev_instance.data[7],
280 dev->dev_instance.data[6],
281 dev->dev_instance.data[8],
282 dev->dev_instance.data[9],
283 dev->dev_instance.data[10],
284 dev->dev_instance.data[11],
285 dev->dev_instance.data[12],
286 dev->dev_instance.data[13],
287 dev->dev_instance.data[14],
288 dev->dev_instance.data[15]);
289 if (ret)
290 return ret;
291
292 return 0;
293}
294
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700295
296/*
297 * vmbus_match - Attempt to match the specified device to the specified driver
298 */
299static int vmbus_match(struct device *device, struct device_driver *driver)
300{
301 int match = 0;
302 struct hv_driver *drv = drv_to_hv_drv(driver);
K. Y. Srinivasane8e27042011-06-06 15:50:04 -0700303 struct hv_device *hv_dev = device_to_hv_device(device);
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700304
305 /* We found our driver ? */
K. Y. Srinivasane8e27042011-06-06 15:50:04 -0700306 if (memcmp(&hv_dev->dev_type, &drv->dev_type,
K. Y. Srinivasande632a22011-04-26 09:20:24 -0700307 sizeof(struct hv_guid)) == 0)
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700308 match = 1;
K. Y. Srinivasande632a22011-04-26 09:20:24 -0700309
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700310 return match;
311}
312
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700313/*
314 * vmbus_probe - Add the new vmbus's child device
315 */
316static int vmbus_probe(struct device *child_device)
317{
318 int ret = 0;
319 struct hv_driver *drv =
320 drv_to_hv_drv(child_device->driver);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700321 struct hv_device *dev = device_to_hv_device(child_device);
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700322
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700323 if (drv->probe) {
324 ret = drv->probe(dev);
K. Y. Srinivasanb14a7b32011-04-29 13:45:03 -0700325 if (ret != 0)
Hank Janssen0a466182011-03-29 13:58:47 -0700326 pr_err("probe failed for device %s (%d)\n",
327 dev_name(child_device), ret);
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700328
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700329 } else {
Hank Janssen0a466182011-03-29 13:58:47 -0700330 pr_err("probe not set for driver %s\n",
331 dev_name(child_device));
K. Y. Srinivasan6de925b2011-06-06 15:50:07 -0700332 ret = -ENODEV;
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700333 }
334 return ret;
335}
336
K. Y. Srinivasanc5dce3d2011-03-15 15:03:40 -0700337/*
338 * vmbus_remove - Remove a vmbus device
339 */
340static int vmbus_remove(struct device *child_device)
341{
342 int ret;
343 struct hv_driver *drv;
344
K. Y. Srinivasan415b0232011-04-29 13:45:12 -0700345 struct hv_device *dev = device_to_hv_device(child_device);
K. Y. Srinivasanc5dce3d2011-03-15 15:03:40 -0700346
347 if (child_device->driver) {
348 drv = drv_to_hv_drv(child_device->driver);
349
K. Y. Srinivasan415b0232011-04-29 13:45:12 -0700350 if (drv->remove) {
351 ret = drv->remove(dev);
K. Y. Srinivasanc5dce3d2011-03-15 15:03:40 -0700352 } else {
Hank Janssen0a466182011-03-29 13:58:47 -0700353 pr_err("remove not set for driver %s\n",
354 dev_name(child_device));
K. Y. Srinivasan6de925b2011-06-06 15:50:07 -0700355 ret = -ENODEV;
K. Y. Srinivasanc5dce3d2011-03-15 15:03:40 -0700356 }
357 }
358
359 return 0;
360}
361
K. Y. Srinivasaneb1bb252011-03-15 15:03:41 -0700362
363/*
364 * vmbus_shutdown - Shutdown a vmbus device
365 */
366static void vmbus_shutdown(struct device *child_device)
367{
368 struct hv_driver *drv;
K. Y. Srinivasanca6887f2011-04-29 13:45:14 -0700369 struct hv_device *dev = device_to_hv_device(child_device);
K. Y. Srinivasaneb1bb252011-03-15 15:03:41 -0700370
371
372 /* The device may not be attached yet */
373 if (!child_device->driver)
374 return;
375
376 drv = drv_to_hv_drv(child_device->driver);
377
K. Y. Srinivasanca6887f2011-04-29 13:45:14 -0700378 if (drv->shutdown)
379 drv->shutdown(dev);
K. Y. Srinivasaneb1bb252011-03-15 15:03:41 -0700380
381 return;
382}
383
K. Y. Srinivasan086e7a52011-03-15 15:03:42 -0700384
385/*
386 * vmbus_device_release - Final callback release of the vmbus child device
387 */
388static void vmbus_device_release(struct device *device)
389{
K. Y. Srinivasane8e27042011-06-06 15:50:04 -0700390 struct hv_device *hv_dev = device_to_hv_device(device);
K. Y. Srinivasan086e7a52011-03-15 15:03:42 -0700391
K. Y. Srinivasane8e27042011-06-06 15:50:04 -0700392 kfree(hv_dev);
K. Y. Srinivasan086e7a52011-03-15 15:03:42 -0700393
394}
395
Bill Pemberton454f18a2009-07-27 16:47:24 -0400396/* The one and only one */
K. Y. Srinivasan9adcac52011-04-29 13:45:08 -0700397static struct bus_type hv_bus = {
398 .name = "vmbus",
399 .match = vmbus_match,
400 .shutdown = vmbus_shutdown,
401 .remove = vmbus_remove,
402 .probe = vmbus_probe,
403 .uevent = vmbus_uevent,
404 .dev_attrs = vmbus_device_attrs,
Hank Janssen3e7ee492009-07-13 16:02:34 -0700405};
406
Haiyang Zhangadf874c2011-01-26 12:12:09 -0800407static const char *driver_name = "hyperv";
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800408
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800409
Timo Teräsbf6506f2010-12-15 20:48:08 +0200410struct onmessage_work_context {
411 struct work_struct work;
412 struct hv_message msg;
413};
414
415static void vmbus_onmessage_work(struct work_struct *work)
416{
417 struct onmessage_work_context *ctx;
418
419 ctx = container_of(work, struct onmessage_work_context,
420 work);
421 vmbus_onmessage(&ctx->msg);
422 kfree(ctx);
423}
424
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800425/*
426 * vmbus_on_msg_dpc - DPC routine to handle messages from the hypervisior
427 */
K. Y. Srinivasan62c10592011-03-10 14:04:53 -0800428static void vmbus_on_msg_dpc(unsigned long data)
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800429{
430 int cpu = smp_processor_id();
431 void *page_addr = hv_context.synic_message_page[cpu];
432 struct hv_message *msg = (struct hv_message *)page_addr +
433 VMBUS_MESSAGE_SINT;
Timo Teräsbf6506f2010-12-15 20:48:08 +0200434 struct onmessage_work_context *ctx;
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800435
436 while (1) {
437 if (msg->header.message_type == HVMSG_NONE) {
438 /* no msg */
439 break;
440 } else {
Timo Teräsbf6506f2010-12-15 20:48:08 +0200441 ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
442 if (ctx == NULL)
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800443 continue;
Timo Teräsbf6506f2010-12-15 20:48:08 +0200444 INIT_WORK(&ctx->work, vmbus_onmessage_work);
445 memcpy(&ctx->msg, msg, sizeof(*msg));
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800446 queue_work(vmbus_connection.work_queue, &ctx->work);
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800447 }
448
449 msg->header.message_type = HVMSG_NONE;
450
451 /*
452 * Make sure the write to MessageType (ie set to
453 * HVMSG_NONE) happens before we read the
454 * MessagePending and EOMing. Otherwise, the EOMing
455 * will not deliver any more messages since there is
456 * no empty slot
457 */
K. Y. Srinivasane826f1d2011-06-06 15:50:03 -0700458 smp_mb();
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800459
460 if (msg->header.message_flags.msg_pending) {
461 /*
462 * This will cause message queue rescan to
463 * possibly deliver another msg from the
464 * hypervisor
465 */
466 wrmsrl(HV_X64_MSR_EOM, 0);
467 }
468 }
469}
470
471/*
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800472 * vmbus_on_isr - ISR routine
473 */
K. Y. Srinivasan480ae582011-03-10 14:06:06 -0800474static int vmbus_on_isr(void)
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800475{
476 int ret = 0;
477 int cpu = smp_processor_id();
478 void *page_addr;
479 struct hv_message *msg;
480 union hv_synic_event_flags *event;
481
482 page_addr = hv_context.synic_message_page[cpu];
483 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
484
485 /* Check if there are actual msgs to be process */
Hank Janssen98e08702011-03-29 13:58:44 -0700486 if (msg->header.message_type != HVMSG_NONE)
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800487 ret |= 0x1;
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800488
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800489 page_addr = hv_context.synic_event_page[cpu];
490 event = (union hv_synic_event_flags *)page_addr + VMBUS_MESSAGE_SINT;
491
492 /* Since we are a child, we only need to check bit 0 */
Greg Kroah-Hartman32235b02011-04-13 12:14:05 -0700493 if (sync_test_and_clear_bit(0, (unsigned long *) &event->flags32[0]))
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800494 ret |= 0x2;
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800495
496 return ret;
497}
498
K. Y. Srinivasan793be9c2011-03-15 15:03:43 -0700499
500static irqreturn_t vmbus_isr(int irq, void *dev_id)
501{
502 int ret;
503
504 ret = vmbus_on_isr();
505
506 /* Schedules a dpc if necessary */
507 if (ret > 0) {
508 if (test_bit(0, (unsigned long *)&ret))
K. Y. Srinivasan59c0e4f2011-04-29 13:45:06 -0700509 tasklet_schedule(&msg_dpc);
K. Y. Srinivasan793be9c2011-03-15 15:03:43 -0700510
511 if (test_bit(1, (unsigned long *)&ret))
K. Y. Srinivasan66d92292011-04-29 13:45:07 -0700512 tasklet_schedule(&event_dpc);
K. Y. Srinivasan793be9c2011-03-15 15:03:43 -0700513
514 return IRQ_HANDLED;
515 } else {
516 return IRQ_NONE;
517 }
518}
519
Hank Janssen3e189512010-03-04 22:11:00 +0000520/*
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700521 * vmbus_bus_init -Main vmbus driver initialization routine.
522 *
523 * Here, we
Lars Lindley0686e4f2010-03-11 23:51:23 +0100524 * - initialize the vmbus driver context
Lars Lindley0686e4f2010-03-11 23:51:23 +0100525 * - invoke the vmbus hv main init routine
526 * - get the irq resource
Lars Lindley0686e4f2010-03-11 23:51:23 +0100527 * - retrieve the channel offers
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700528 */
K. Y. Srinivasan9aaa9952011-06-06 15:49:37 -0700529static int vmbus_bus_init(int irq)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700530{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700531 int ret;
532 unsigned int vector;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700533
Greg Kroah-Hartman6d26e38fa22010-12-02 12:08:08 -0800534 /* Hypervisor initialization...setup hypercall page..etc */
535 ret = hv_init();
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700536 if (ret != 0) {
Hank Janssen0a466182011-03-29 13:58:47 -0700537 pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
K. Y. Srinivasand6c1c5d2011-06-06 15:50:08 -0700538 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700539 }
540
Bill Pemberton454f18a2009-07-27 16:47:24 -0400541 /* Initialize the bus context */
K. Y. Srinivasan59c0e4f2011-04-29 13:45:06 -0700542 tasklet_init(&msg_dpc, vmbus_on_msg_dpc, 0);
K. Y. Srinivasan66d92292011-04-29 13:45:07 -0700543 tasklet_init(&event_dpc, vmbus_on_event, 0);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700544
K. Y. Srinivasana6e4d8e2011-03-10 14:03:51 -0800545 /* Now, register the bus with LDM */
K. Y. Srinivasan9adcac52011-04-29 13:45:08 -0700546 ret = bus_register(&hv_bus);
K. Y. Srinivasand6c1c5d2011-06-06 15:50:08 -0700547 if (ret)
548 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700549
Bill Pemberton454f18a2009-07-27 16:47:24 -0400550 /* Get the interrupt resource */
K. Y. Srinivasan3d2de262011-06-06 15:49:38 -0700551 ret = request_irq(irq, vmbus_isr, IRQF_SAMPLE_RANDOM,
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -0700552 driver_name, hv_acpi_dev);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700553
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700554 if (ret != 0) {
Hank Janssen0a466182011-03-29 13:58:47 -0700555 pr_err("Unable to request IRQ %d\n",
K. Y. Srinivasan9aaa9952011-06-06 15:49:37 -0700556 irq);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700557
K. Y. Srinivasan9adcac52011-04-29 13:45:08 -0700558 bus_unregister(&hv_bus);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700559
K. Y. Srinivasand6c1c5d2011-06-06 15:50:08 -0700560 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700561 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700562
K. Y. Srinivasan9aaa9952011-06-06 15:49:37 -0700563 vector = IRQ0_VECTOR + irq;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700564
K. Y. Srinivasan800b6902011-03-15 15:03:33 -0700565 /*
566 * Notify the hypervisor of our irq and
567 * connect to the host.
568 */
569 on_each_cpu(hv_synic_init, (void *)&vector, 1);
570 ret = vmbus_connect();
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700571 if (ret) {
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -0700572 free_irq(irq, hv_acpi_dev);
K. Y. Srinivasan9adcac52011-04-29 13:45:08 -0700573 bus_unregister(&hv_bus);
K. Y. Srinivasand6c1c5d2011-06-06 15:50:08 -0700574 return ret;
Bill Pemberton5d48a1c2009-07-27 16:47:36 -0400575 }
576
K. Y. Srinivasan800b6902011-03-15 15:03:33 -0700577
Greg Kroah-Hartman2d6e8822010-12-02 08:50:58 -0800578 vmbus_request_offers();
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +0000579
K. Y. Srinivasand6c1c5d2011-06-06 15:50:08 -0700580 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700581}
582
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700583/**
Hank Janssen3e189512010-03-04 22:11:00 +0000584 * vmbus_child_driver_register() - Register a vmbus's child driver
K. Y. Srinivasanc643269d2011-03-07 13:23:43 -0800585 * @drv: Pointer to driver structure you want to register
Hank Janssen3e189512010-03-04 22:11:00 +0000586 *
Hank Janssen3e189512010-03-04 22:11:00 +0000587 *
588 * Registers the given driver with Linux through the 'driver_register()' call
589 * And sets up the hyper-v vmbus handling for this driver.
590 * It will return the state of the 'driver_register()' call.
591 *
592 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700593 */
K. Y. Srinivasanc643269d2011-03-07 13:23:43 -0800594int vmbus_child_driver_register(struct device_driver *drv)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700595{
Bill Pemberton5d48a1c2009-07-27 16:47:36 -0400596 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700597
Hank Janssen0a466182011-03-29 13:58:47 -0700598 pr_info("child driver registering - name %s\n", drv->name);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700599
Bill Pemberton454f18a2009-07-27 16:47:24 -0400600 /* The child driver on this vmbus */
K. Y. Srinivasan9adcac52011-04-29 13:45:08 -0700601 drv->bus = &hv_bus;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700602
K. Y. Srinivasanc643269d2011-03-07 13:23:43 -0800603 ret = driver_register(drv);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700604
Greg Kroah-Hartman2d6e8822010-12-02 08:50:58 -0800605 vmbus_request_offers();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700606
Bill Pemberton5d48a1c2009-07-27 16:47:36 -0400607 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700608}
Hank Janssen3e7ee492009-07-13 16:02:34 -0700609EXPORT_SYMBOL(vmbus_child_driver_register);
610
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700611/**
Hank Janssen3e189512010-03-04 22:11:00 +0000612 * vmbus_child_driver_unregister() - Unregister a vmbus's child driver
K. Y. Srinivasan06de23f2011-03-07 13:24:23 -0800613 * @drv: Pointer to driver structure you want to un-register
Hank Janssen3e189512010-03-04 22:11:00 +0000614 *
Hank Janssen3e189512010-03-04 22:11:00 +0000615 *
616 * Un-register the given driver with Linux through the 'driver_unregister()'
617 * call. And ungegisters the driver from the Hyper-V vmbus handler.
618 *
619 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700620 */
K. Y. Srinivasan06de23f2011-03-07 13:24:23 -0800621void vmbus_child_driver_unregister(struct device_driver *drv)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700622{
Hank Janssen0a466182011-03-29 13:58:47 -0700623 pr_info("child driver unregistering - name %s\n", drv->name);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700624
K. Y. Srinivasan06de23f2011-03-07 13:24:23 -0800625 driver_unregister(drv);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700626
Hank Janssen3e7ee492009-07-13 16:02:34 -0700627}
Hank Janssen3e7ee492009-07-13 16:02:34 -0700628EXPORT_SYMBOL(vmbus_child_driver_unregister);
629
Hank Janssen3e189512010-03-04 22:11:00 +0000630/*
Hank Janssen3e189512010-03-04 22:11:00 +0000631 * vmbus_child_device_create - Creates and registers a new child device
632 * on the vmbus.
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700633 */
Greg Kroah-Hartman89733aa2010-12-02 08:22:41 -0800634struct hv_device *vmbus_child_device_create(struct hv_guid *type,
635 struct hv_guid *instance,
636 struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700637{
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200638 struct hv_device *child_device_obj;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700639
Bill Pemberton454f18a2009-07-27 16:47:24 -0400640 /* Allocate the new child device */
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800641 child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
642 if (!child_device_obj) {
Hank Janssen0a466182011-03-29 13:58:47 -0700643 pr_err("Unable to allocate device object for child device\n");
Hank Janssen3e7ee492009-07-13 16:02:34 -0700644 return NULL;
645 }
646
Greg Kroah-Hartmancae5b842010-10-21 09:05:27 -0700647 child_device_obj->channel = channel;
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800648 memcpy(&child_device_obj->dev_type, type, sizeof(struct hv_guid));
649 memcpy(&child_device_obj->dev_instance, instance,
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700650 sizeof(struct hv_guid));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700651
Hank Janssen3e7ee492009-07-13 16:02:34 -0700652
Hank Janssen3e7ee492009-07-13 16:02:34 -0700653 return child_device_obj;
654}
655
Hank Janssen3e189512010-03-04 22:11:00 +0000656/*
K. Y. Srinivasane13a0b52011-03-10 14:07:44 -0800657 * vmbus_child_device_register - Register the child device
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700658 */
K. Y. Srinivasan3ca07cb2011-03-10 14:07:21 -0800659int vmbus_child_device_register(struct hv_device *child_device_obj)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700660{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700661 int ret = 0;
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800662
Bill Pembertonf4888412009-07-29 17:00:12 -0400663 static atomic_t device_num = ATOMIC_INIT(0);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700664
Haiyang Zhang1bb40a22009-10-23 18:14:24 +0000665 /* Set the device name. Otherwise, device_register() will fail. */
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800666 dev_set_name(&child_device_obj->device, "vmbus_0_%d",
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700667 atomic_inc_return(&device_num));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700668
Bill Pemberton454f18a2009-07-27 16:47:24 -0400669 /* The new device belongs to this bus */
K. Y. Srinivasan9adcac52011-04-29 13:45:08 -0700670 child_device_obj->device.bus = &hv_bus; /* device->dev.bus; */
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -0700671 child_device_obj->device.parent = &hv_acpi_dev->dev;
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800672 child_device_obj->device.release = vmbus_device_release;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700673
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700674 /*
675 * Register with the LDM. This will kick off the driver/device
676 * binding...which will eventually call vmbus_match() and vmbus_probe()
677 */
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800678 ret = device_register(&child_device_obj->device);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700679
Hank Janssen3e7ee492009-07-13 16:02:34 -0700680 if (ret)
Hank Janssen0a466182011-03-29 13:58:47 -0700681 pr_err("Unable to register child device\n");
Hank Janssen3e7ee492009-07-13 16:02:34 -0700682 else
Hank Janssen0a466182011-03-29 13:58:47 -0700683 pr_info("child device %s registered\n",
684 dev_name(&child_device_obj->device));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700685
Hank Janssen3e7ee492009-07-13 16:02:34 -0700686 return ret;
687}
688
Hank Janssen3e189512010-03-04 22:11:00 +0000689/*
690 * vmbus_child_device_unregister - Remove the specified child device
691 * from the vmbus.
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700692 */
Greg Kroah-Hartman9d8bd712010-12-02 08:34:45 -0800693void vmbus_child_device_unregister(struct hv_device *device_obj)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700694{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700695 /*
696 * Kick off the process of unregistering the device.
697 * This will call vmbus_remove() and eventually vmbus_device_release()
698 */
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800699 device_unregister(&device_obj->device);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700700
Hank Janssen0a466182011-03-29 13:58:47 -0700701 pr_info("child device %s unregistered\n",
702 dev_name(&device_obj->device));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700703}
704
Hank Janssen3e7ee492009-07-13 16:02:34 -0700705
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -0700706/*
707 * VMBUS is an acpi enumerated device. Get the the IRQ information
708 * from DSDT.
709 */
710
711static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *irq)
712{
713
714 if (res->type == ACPI_RESOURCE_TYPE_IRQ) {
715 struct acpi_resource_irq *irqp;
716 irqp = &res->data.irq;
717
718 *((unsigned int *)irq) = irqp->interrupts[0];
719 }
720
721 return AE_OK;
722}
723
724static int vmbus_acpi_add(struct acpi_device *device)
725{
726 acpi_status result;
727
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -0700728 hv_acpi_dev = device;
729
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -0700730 result =
731 acpi_walk_resources(device->handle, METHOD_NAME__CRS,
732 vmbus_walk_resources, &irq);
733
734 if (ACPI_FAILURE(result)) {
735 complete(&probe_event);
736 return -ENODEV;
737 }
738 complete(&probe_event);
739 return 0;
740}
741
742static const struct acpi_device_id vmbus_acpi_device_ids[] = {
743 {"VMBUS", 0},
K. Y. Srinivasan9d7b18d2011-06-06 15:49:42 -0700744 {"VMBus", 0},
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -0700745 {"", 0},
746};
747MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
748
749static struct acpi_driver vmbus_acpi_driver = {
750 .name = "vmbus",
751 .ids = vmbus_acpi_device_ids,
752 .ops = {
753 .add = vmbus_acpi_add,
754 },
755};
756
Greg Kroah-Hartman9a775db2010-02-25 16:42:10 -0800757/*
758 * We use a PCI table to determine if we should autoload this driver This is
759 * needed by distro tools to determine if the hyperv drivers should be
760 * installed and/or configured. We don't do anything else with the table, but
761 * it needs to be present.
Greg Kroah-Hartman9a775db2010-02-25 16:42:10 -0800762 */
Tobias Klauser15f0beb2010-05-20 10:39:38 +0200763static const struct pci_device_id microsoft_hv_pci_table[] = {
Greg Kroah-Hartman9a775db2010-02-25 16:42:10 -0800764 { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
765 { 0 }
766};
767MODULE_DEVICE_TABLE(pci, microsoft_hv_pci_table);
768
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -0700769static int __init hv_acpi_init(void)
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -0700770{
K. Y. Srinivasan71a66552011-04-29 13:45:04 -0700771 int ret;
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -0700772
773 init_completion(&probe_event);
774
775 /*
776 * Get irq resources first.
777 */
778
K. Y. Srinivasan02466042011-06-06 15:49:40 -0700779 ret = acpi_bus_register_driver(&vmbus_acpi_driver);
780
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -0700781 if (ret)
782 return ret;
783
784 wait_for_completion(&probe_event);
785
786 if (irq <= 0) {
K. Y. Srinivasan2da9e1d2011-06-06 15:49:41 -0700787 acpi_bus_unregister_driver(&vmbus_acpi_driver);
K. Y. Srinivasanb0069f42011-04-29 13:45:15 -0700788 return -ENODEV;
789 }
790
K. Y. Srinivasan91fd7992011-06-16 13:16:38 -0700791 ret = vmbus_bus_init(irq);
792 if (ret)
793 acpi_bus_unregister_driver(&vmbus_acpi_driver);
794 return ret;
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -0700795}
796
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -0700797
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700798MODULE_LICENSE("GPL");
Hank Janssen26c14cc2010-02-11 23:02:42 +0000799MODULE_VERSION(HV_DRV_VERSION);
Olaf Hering13399cb2011-04-16 18:50:42 +0200800module_param(vmbus_loglevel, int, S_IRUGO|S_IWUSR);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700801
K. Y. Srinivasan607c1a12011-06-06 15:49:39 -0700802module_init(hv_acpi_init);