]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/staging/hv/vmbus_drv.c
Staging: hv: Create a common header for all hyperv drivers to include
[mirror_ubuntu-zesty-kernel.git] / drivers / staging / hv / vmbus_drv.c
CommitLineData
3e7ee490 1/*
3e7ee490
HJ
2 * 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>
b0069f43 20 * K. Y. Srinivasan <kys@microsoft.com>
52e5c1ce 21 *
3e7ee490 22 */
0a46618d
HJ
23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
3e7ee490
HJ
25#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>
9a775dbd 31#include <linux/pci.h>
c22090fa 32#include <linux/dmi.h>
5a0e3ad6 33#include <linux/slab.h>
b0069f43
S
34#include <linux/acpi.h>
35#include <acpi/acpi_bus.h>
8b5d6d3b 36#include <linux/completion.h>
2d82f6c7 37#include "version_info.h"
e3fe0bb6 38#include "hv_api.h"
645954c5 39#include "logging.h"
870cde80 40#include "vmbus.h"
150b19d4 41#include "channel.h"
36199a99 42#include "vmbus_private.h"
3e7ee490 43
3e7ee490 44
ef58f5d1 45static struct pci_dev *hv_pci_dev;
1168ac22 46
59c0e4f0 47static struct tasklet_struct msg_dpc;
66d9229c 48static struct tasklet_struct event_dpc;
59c0e4f0 49
98db4335
S
50unsigned int vmbus_loglevel = (ALL_MODULES << 16 | INFO_LVL);
51EXPORT_SYMBOL(vmbus_loglevel);
52 /* (ALL_MODULES << 16 | DEBUG_LVL_ENTEREXIT); */
53 /* (((VMBUS | VMBUS_DRV)<<16) | DEBUG_LVL_ENTEREXIT); */
54
71a6655d
S
55static int pci_probe_error;
56static struct completion probe_event;
b0069f43 57static int irq;
98db4335
S
58
59static void get_channel_info(struct hv_device *device,
60 struct hv_device_info *info)
61{
62 struct vmbus_channel_debug_info debug_info;
63
64 if (!device->channel)
65 return;
66
67 vmbus_get_debug_info(device->channel, &debug_info);
68
69 info->chn_id = debug_info.relid;
70 info->chn_state = debug_info.state;
71 memcpy(&info->chn_type, &debug_info.interfacetype,
72 sizeof(struct hv_guid));
73 memcpy(&info->chn_instance, &debug_info.interface_instance,
74 sizeof(struct hv_guid));
75
76 info->monitor_id = debug_info.monitorid;
77
78 info->server_monitor_pending = debug_info.servermonitor_pending;
79 info->server_monitor_latency = debug_info.servermonitor_latency;
80 info->server_monitor_conn_id = debug_info.servermonitor_connectionid;
81
82 info->client_monitor_pending = debug_info.clientmonitor_pending;
83 info->client_monitor_latency = debug_info.clientmonitor_latency;
84 info->client_monitor_conn_id = debug_info.clientmonitor_connectionid;
85
86 info->inbound.int_mask = debug_info.inbound.current_interrupt_mask;
87 info->inbound.read_idx = debug_info.inbound.current_read_index;
88 info->inbound.write_idx = debug_info.inbound.current_write_index;
89 info->inbound.bytes_avail_toread =
90 debug_info.inbound.bytes_avail_toread;
91 info->inbound.bytes_avail_towrite =
92 debug_info.inbound.bytes_avail_towrite;
3e7ee490 93
98db4335
S
94 info->outbound.int_mask =
95 debug_info.outbound.current_interrupt_mask;
96 info->outbound.read_idx = debug_info.outbound.current_read_index;
97 info->outbound.write_idx = debug_info.outbound.current_write_index;
98 info->outbound.bytes_avail_toread =
99 debug_info.outbound.bytes_avail_toread;
100 info->outbound.bytes_avail_towrite =
101 debug_info.outbound.bytes_avail_towrite;
102}
3e7ee490 103
98db4335
S
104/*
105 * vmbus_show_device_attr - Show the device attribute in sysfs.
106 *
107 * This is invoked when user does a
108 * "cat /sys/bus/vmbus/devices/<busdevice>/<attr name>"
109 */
90c9960e
GKH
110static ssize_t vmbus_show_device_attr(struct device *dev,
111 struct device_attribute *dev_attr,
98db4335
S
112 char *buf)
113{
114 struct hv_device *device_ctx = device_to_hv_device(dev);
115 struct hv_device_info device_info;
3e7ee490 116
98db4335 117 memset(&device_info, 0, sizeof(struct hv_device_info));
3e7ee490 118
98db4335 119 get_channel_info(device_ctx, &device_info);
3e7ee490 120
98db4335
S
121 if (!strcmp(dev_attr->attr.name, "class_id")) {
122 return sprintf(buf, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-"
123 "%02x%02x%02x%02x%02x%02x%02x%02x}\n",
124 device_info.chn_type.data[3],
125 device_info.chn_type.data[2],
126 device_info.chn_type.data[1],
127 device_info.chn_type.data[0],
128 device_info.chn_type.data[5],
129 device_info.chn_type.data[4],
130 device_info.chn_type.data[7],
131 device_info.chn_type.data[6],
132 device_info.chn_type.data[8],
133 device_info.chn_type.data[9],
134 device_info.chn_type.data[10],
135 device_info.chn_type.data[11],
136 device_info.chn_type.data[12],
137 device_info.chn_type.data[13],
138 device_info.chn_type.data[14],
139 device_info.chn_type.data[15]);
140 } else if (!strcmp(dev_attr->attr.name, "device_id")) {
141 return sprintf(buf, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-"
142 "%02x%02x%02x%02x%02x%02x%02x%02x}\n",
143 device_info.chn_instance.data[3],
144 device_info.chn_instance.data[2],
145 device_info.chn_instance.data[1],
146 device_info.chn_instance.data[0],
147 device_info.chn_instance.data[5],
148 device_info.chn_instance.data[4],
149 device_info.chn_instance.data[7],
150 device_info.chn_instance.data[6],
151 device_info.chn_instance.data[8],
152 device_info.chn_instance.data[9],
153 device_info.chn_instance.data[10],
154 device_info.chn_instance.data[11],
155 device_info.chn_instance.data[12],
156 device_info.chn_instance.data[13],
157 device_info.chn_instance.data[14],
158 device_info.chn_instance.data[15]);
159 } else if (!strcmp(dev_attr->attr.name, "state")) {
160 return sprintf(buf, "%d\n", device_info.chn_state);
161 } else if (!strcmp(dev_attr->attr.name, "id")) {
162 return sprintf(buf, "%d\n", device_info.chn_id);
163 } else if (!strcmp(dev_attr->attr.name, "out_intr_mask")) {
164 return sprintf(buf, "%d\n", device_info.outbound.int_mask);
165 } else if (!strcmp(dev_attr->attr.name, "out_read_index")) {
166 return sprintf(buf, "%d\n", device_info.outbound.read_idx);
167 } else if (!strcmp(dev_attr->attr.name, "out_write_index")) {
168 return sprintf(buf, "%d\n", device_info.outbound.write_idx);
169 } else if (!strcmp(dev_attr->attr.name, "out_read_bytes_avail")) {
170 return sprintf(buf, "%d\n",
171 device_info.outbound.bytes_avail_toread);
172 } else if (!strcmp(dev_attr->attr.name, "out_write_bytes_avail")) {
173 return sprintf(buf, "%d\n",
174 device_info.outbound.bytes_avail_towrite);
175 } else if (!strcmp(dev_attr->attr.name, "in_intr_mask")) {
176 return sprintf(buf, "%d\n", device_info.inbound.int_mask);
177 } else if (!strcmp(dev_attr->attr.name, "in_read_index")) {
178 return sprintf(buf, "%d\n", device_info.inbound.read_idx);
179 } else if (!strcmp(dev_attr->attr.name, "in_write_index")) {
180 return sprintf(buf, "%d\n", device_info.inbound.write_idx);
181 } else if (!strcmp(dev_attr->attr.name, "in_read_bytes_avail")) {
182 return sprintf(buf, "%d\n",
183 device_info.inbound.bytes_avail_toread);
184 } else if (!strcmp(dev_attr->attr.name, "in_write_bytes_avail")) {
185 return sprintf(buf, "%d\n",
186 device_info.inbound.bytes_avail_towrite);
187 } else if (!strcmp(dev_attr->attr.name, "monitor_id")) {
188 return sprintf(buf, "%d\n", device_info.monitor_id);
189 } else if (!strcmp(dev_attr->attr.name, "server_monitor_pending")) {
190 return sprintf(buf, "%d\n", device_info.server_monitor_pending);
191 } else if (!strcmp(dev_attr->attr.name, "server_monitor_latency")) {
192 return sprintf(buf, "%d\n", device_info.server_monitor_latency);
193 } else if (!strcmp(dev_attr->attr.name, "server_monitor_conn_id")) {
194 return sprintf(buf, "%d\n",
195 device_info.server_monitor_conn_id);
196 } else if (!strcmp(dev_attr->attr.name, "client_monitor_pending")) {
197 return sprintf(buf, "%d\n", device_info.client_monitor_pending);
198 } else if (!strcmp(dev_attr->attr.name, "client_monitor_latency")) {
199 return sprintf(buf, "%d\n", device_info.client_monitor_latency);
200 } else if (!strcmp(dev_attr->attr.name, "client_monitor_conn_id")) {
201 return sprintf(buf, "%d\n",
202 device_info.client_monitor_conn_id);
203 } else {
204 return 0;
205 }
206}
3e7ee490 207
454f18a9 208/* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
3e7ee490
HJ
209static struct device_attribute vmbus_device_attrs[] = {
210 __ATTR(id, S_IRUGO, vmbus_show_device_attr, NULL),
211 __ATTR(state, S_IRUGO, vmbus_show_device_attr, NULL),
212 __ATTR(class_id, S_IRUGO, vmbus_show_device_attr, NULL),
213 __ATTR(device_id, S_IRUGO, vmbus_show_device_attr, NULL),
214 __ATTR(monitor_id, S_IRUGO, vmbus_show_device_attr, NULL),
215
216 __ATTR(server_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL),
217 __ATTR(server_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL),
218 __ATTR(server_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL),
219
220 __ATTR(client_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL),
221 __ATTR(client_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL),
222 __ATTR(client_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL),
223
224 __ATTR(out_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL),
225 __ATTR(out_read_index, S_IRUGO, vmbus_show_device_attr, NULL),
226 __ATTR(out_write_index, S_IRUGO, vmbus_show_device_attr, NULL),
227 __ATTR(out_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
228 __ATTR(out_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
229
230 __ATTR(in_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL),
231 __ATTR(in_read_index, S_IRUGO, vmbus_show_device_attr, NULL),
232 __ATTR(in_write_index, S_IRUGO, vmbus_show_device_attr, NULL),
233 __ATTR(in_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
234 __ATTR(in_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
235 __ATTR_NULL
236};
3e7ee490 237
793be9c7 238
adde2487
S
239/*
240 * vmbus_uevent - add uevent for our device
241 *
242 * This routine is invoked when a device is added or removed on the vmbus to
243 * generate a uevent to udev in the userspace. The udev will then look at its
244 * rule and the uevent generated here to load the appropriate driver
245 */
246static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
247{
248 struct hv_device *dev = device_to_hv_device(device);
249 int ret;
250
adde2487
S
251 ret = add_uevent_var(env, "VMBUS_DEVICE_CLASS_GUID={"
252 "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
253 "%02x%02x%02x%02x%02x%02x%02x%02x}",
254 dev->dev_type.data[3],
255 dev->dev_type.data[2],
256 dev->dev_type.data[1],
257 dev->dev_type.data[0],
258 dev->dev_type.data[5],
259 dev->dev_type.data[4],
260 dev->dev_type.data[7],
261 dev->dev_type.data[6],
262 dev->dev_type.data[8],
263 dev->dev_type.data[9],
264 dev->dev_type.data[10],
265 dev->dev_type.data[11],
266 dev->dev_type.data[12],
267 dev->dev_type.data[13],
268 dev->dev_type.data[14],
269 dev->dev_type.data[15]);
270
271 if (ret)
272 return ret;
273
274 ret = add_uevent_var(env, "VMBUS_DEVICE_DEVICE_GUID={"
275 "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
276 "%02x%02x%02x%02x%02x%02x%02x%02x}",
277 dev->dev_instance.data[3],
278 dev->dev_instance.data[2],
279 dev->dev_instance.data[1],
280 dev->dev_instance.data[0],
281 dev->dev_instance.data[5],
282 dev->dev_instance.data[4],
283 dev->dev_instance.data[7],
284 dev->dev_instance.data[6],
285 dev->dev_instance.data[8],
286 dev->dev_instance.data[9],
287 dev->dev_instance.data[10],
288 dev->dev_instance.data[11],
289 dev->dev_instance.data[12],
290 dev->dev_instance.data[13],
291 dev->dev_instance.data[14],
292 dev->dev_instance.data[15]);
293 if (ret)
294 return ret;
295
296 return 0;
297}
298
b7fc147b
S
299
300/*
301 * vmbus_match - Attempt to match the specified device to the specified driver
302 */
303static int vmbus_match(struct device *device, struct device_driver *driver)
304{
305 int match = 0;
306 struct hv_driver *drv = drv_to_hv_drv(driver);
307 struct hv_device *device_ctx = device_to_hv_device(device);
308
309 /* We found our driver ? */
310 if (memcmp(&device_ctx->dev_type, &drv->dev_type,
de632a2b 311 sizeof(struct hv_guid)) == 0)
b7fc147b 312 match = 1;
de632a2b 313
b7fc147b
S
314 return match;
315}
316
f1f0d67b
S
317/*
318 * vmbus_probe - Add the new vmbus's child device
319 */
320static int vmbus_probe(struct device *child_device)
321{
322 int ret = 0;
323 struct hv_driver *drv =
324 drv_to_hv_drv(child_device->driver);
9efd21e1 325 struct hv_device *dev = device_to_hv_device(child_device);
f1f0d67b 326
9efd21e1
S
327 if (drv->probe) {
328 ret = drv->probe(dev);
b14a7b30 329 if (ret != 0)
0a46618d
HJ
330 pr_err("probe failed for device %s (%d)\n",
331 dev_name(child_device), ret);
f1f0d67b 332
f1f0d67b 333 } else {
0a46618d
HJ
334 pr_err("probe not set for driver %s\n",
335 dev_name(child_device));
f1f0d67b
S
336 ret = -1;
337 }
338 return ret;
339}
340
c5dce3db
S
341/*
342 * vmbus_remove - Remove a vmbus device
343 */
344static int vmbus_remove(struct device *child_device)
345{
346 int ret;
347 struct hv_driver *drv;
348
415b023a 349 struct hv_device *dev = device_to_hv_device(child_device);
c5dce3db
S
350
351 if (child_device->driver) {
352 drv = drv_to_hv_drv(child_device->driver);
353
415b023a
S
354 if (drv->remove) {
355 ret = drv->remove(dev);
c5dce3db 356 } else {
0a46618d
HJ
357 pr_err("remove not set for driver %s\n",
358 dev_name(child_device));
c5dce3db
S
359 ret = -1;
360 }
361 }
362
363 return 0;
364}
365
eb1bb259
S
366
367/*
368 * vmbus_shutdown - Shutdown a vmbus device
369 */
370static void vmbus_shutdown(struct device *child_device)
371{
372 struct hv_driver *drv;
ca6887fb 373 struct hv_device *dev = device_to_hv_device(child_device);
eb1bb259
S
374
375
376 /* The device may not be attached yet */
377 if (!child_device->driver)
378 return;
379
380 drv = drv_to_hv_drv(child_device->driver);
381
ca6887fb
S
382 if (drv->shutdown)
383 drv->shutdown(dev);
eb1bb259
S
384
385 return;
386}
387
086e7a56
S
388
389/*
390 * vmbus_device_release - Final callback release of the vmbus child device
391 */
392static void vmbus_device_release(struct device *device)
393{
394 struct hv_device *device_ctx = device_to_hv_device(device);
395
396 kfree(device_ctx);
397
398}
399
454f18a9 400/* The one and only one */
9adcac5c
S
401static struct bus_type hv_bus = {
402 .name = "vmbus",
403 .match = vmbus_match,
404 .shutdown = vmbus_shutdown,
405 .remove = vmbus_remove,
406 .probe = vmbus_probe,
407 .uevent = vmbus_uevent,
408 .dev_attrs = vmbus_device_attrs,
3e7ee490
HJ
409};
410
adf874cb 411static const char *driver_name = "hyperv";
36199a99 412
36199a99 413
bf6506f6
TT
414struct onmessage_work_context {
415 struct work_struct work;
416 struct hv_message msg;
417};
418
419static void vmbus_onmessage_work(struct work_struct *work)
420{
421 struct onmessage_work_context *ctx;
422
423 ctx = container_of(work, struct onmessage_work_context,
424 work);
425 vmbus_onmessage(&ctx->msg);
426 kfree(ctx);
427}
428
36199a99
GKH
429/*
430 * vmbus_on_msg_dpc - DPC routine to handle messages from the hypervisior
431 */
62c1059d 432static void vmbus_on_msg_dpc(unsigned long data)
36199a99
GKH
433{
434 int cpu = smp_processor_id();
435 void *page_addr = hv_context.synic_message_page[cpu];
436 struct hv_message *msg = (struct hv_message *)page_addr +
437 VMBUS_MESSAGE_SINT;
bf6506f6 438 struct onmessage_work_context *ctx;
36199a99
GKH
439
440 while (1) {
441 if (msg->header.message_type == HVMSG_NONE) {
442 /* no msg */
443 break;
444 } else {
bf6506f6
TT
445 ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
446 if (ctx == NULL)
36199a99 447 continue;
bf6506f6
TT
448 INIT_WORK(&ctx->work, vmbus_onmessage_work);
449 memcpy(&ctx->msg, msg, sizeof(*msg));
da9fcb72 450 queue_work(vmbus_connection.work_queue, &ctx->work);
36199a99
GKH
451 }
452
453 msg->header.message_type = HVMSG_NONE;
454
455 /*
456 * Make sure the write to MessageType (ie set to
457 * HVMSG_NONE) happens before we read the
458 * MessagePending and EOMing. Otherwise, the EOMing
459 * will not deliver any more messages since there is
460 * no empty slot
461 */
462 mb();
463
464 if (msg->header.message_flags.msg_pending) {
465 /*
466 * This will cause message queue rescan to
467 * possibly deliver another msg from the
468 * hypervisor
469 */
470 wrmsrl(HV_X64_MSR_EOM, 0);
471 }
472 }
473}
474
36199a99
GKH
475/*
476 * vmbus_on_isr - ISR routine
477 */
480ae58d 478static int vmbus_on_isr(void)
36199a99
GKH
479{
480 int ret = 0;
481 int cpu = smp_processor_id();
482 void *page_addr;
483 struct hv_message *msg;
484 union hv_synic_event_flags *event;
485
486 page_addr = hv_context.synic_message_page[cpu];
487 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
488
489 /* Check if there are actual msgs to be process */
98e08702 490 if (msg->header.message_type != HVMSG_NONE)
36199a99 491 ret |= 0x1;
36199a99
GKH
492
493 /* TODO: Check if there are events to be process */
494 page_addr = hv_context.synic_event_page[cpu];
495 event = (union hv_synic_event_flags *)page_addr + VMBUS_MESSAGE_SINT;
496
497 /* Since we are a child, we only need to check bit 0 */
32235b07 498 if (sync_test_and_clear_bit(0, (unsigned long *) &event->flags32[0]))
36199a99 499 ret |= 0x2;
36199a99
GKH
500
501 return ret;
502}
503
793be9c7
S
504
505static irqreturn_t vmbus_isr(int irq, void *dev_id)
506{
507 int ret;
508
509 ret = vmbus_on_isr();
510
511 /* Schedules a dpc if necessary */
512 if (ret > 0) {
513 if (test_bit(0, (unsigned long *)&ret))
59c0e4f0 514 tasklet_schedule(&msg_dpc);
793be9c7
S
515
516 if (test_bit(1, (unsigned long *)&ret))
66d9229c 517 tasklet_schedule(&event_dpc);
793be9c7
S
518
519 return IRQ_HANDLED;
520 } else {
521 return IRQ_NONE;
522 }
523}
524
3e189519 525/*
90c9960e
GKH
526 * vmbus_bus_init -Main vmbus driver initialization routine.
527 *
528 * Here, we
0686e4f4 529 * - initialize the vmbus driver context
0686e4f4
LL
530 * - invoke the vmbus hv main init routine
531 * - get the irq resource
0686e4f4 532 * - retrieve the channel offers
90c9960e 533 */
52e5c1ce 534static int vmbus_bus_init(struct pci_dev *pdev)
3e7ee490 535{
90c9960e
GKH
536 int ret;
537 unsigned int vector;
3e7ee490 538
6d26e38f
GKH
539 /* Hypervisor initialization...setup hypercall page..etc */
540 ret = hv_init();
90c9960e 541 if (ret != 0) {
0a46618d 542 pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
3e7ee490
HJ
543 goto cleanup;
544 }
545
454f18a9 546 /* Initialize the bus context */
59c0e4f0 547 tasklet_init(&msg_dpc, vmbus_on_msg_dpc, 0);
66d9229c 548 tasklet_init(&event_dpc, vmbus_on_event, 0);
3e7ee490 549
a6e4d8e3 550 /* Now, register the bus with LDM */
9adcac5c 551 ret = bus_register(&hv_bus);
90c9960e 552 if (ret) {
c19fbca3
BP
553 ret = -1;
554 goto cleanup;
555 }
3e7ee490 556
454f18a9 557 /* Get the interrupt resource */
52e5c1ce
S
558 ret = request_irq(pdev->irq, vmbus_isr,
559 IRQF_SHARED | IRQF_SAMPLE_RANDOM,
560 driver_name, pdev);
3e7ee490 561
90c9960e 562 if (ret != 0) {
0a46618d 563 pr_err("Unable to request IRQ %d\n",
52e5c1ce 564 pdev->irq);
3e7ee490 565
9adcac5c 566 bus_unregister(&hv_bus);
3e7ee490
HJ
567
568 ret = -1;
569 goto cleanup;
570 }
3e7ee490 571
52e5c1ce 572 vector = IRQ0_VECTOR + pdev->irq;
3e7ee490 573
800b6902
S
574 /*
575 * Notify the hypervisor of our irq and
576 * connect to the host.
577 */
578 on_each_cpu(hv_synic_init, (void *)&vector, 1);
579 ret = vmbus_connect();
90c9960e 580 if (ret) {
52e5c1ce 581 free_irq(pdev->irq, pdev);
9adcac5c 582 bus_unregister(&hv_bus);
5d48a1c2
BP
583 goto cleanup;
584 }
585
800b6902 586
2d6e882b 587 vmbus_request_offers();
8b5d6d3b
HZ
588 wait_for_completion(&hv_channel_ready);
589
3e7ee490 590cleanup:
3e7ee490
HJ
591 return ret;
592}
593
90c9960e 594/**
3e189519 595 * vmbus_child_driver_register() - Register a vmbus's child driver
c643269d 596 * @drv: Pointer to driver structure you want to register
3e189519 597 *
3e189519
HJ
598 *
599 * Registers the given driver with Linux through the 'driver_register()' call
600 * And sets up the hyper-v vmbus handling for this driver.
601 * It will return the state of the 'driver_register()' call.
602 *
603 * Mainly used by Hyper-V drivers.
90c9960e 604 */
c643269d 605int vmbus_child_driver_register(struct device_driver *drv)
3e7ee490 606{
5d48a1c2 607 int ret;
3e7ee490 608
0a46618d 609 pr_info("child driver registering - name %s\n", drv->name);
3e7ee490 610
454f18a9 611 /* The child driver on this vmbus */
9adcac5c 612 drv->bus = &hv_bus;
3e7ee490 613
c643269d 614 ret = driver_register(drv);
3e7ee490 615
2d6e882b 616 vmbus_request_offers();
3e7ee490 617
5d48a1c2 618 return ret;
3e7ee490 619}
3e7ee490
HJ
620EXPORT_SYMBOL(vmbus_child_driver_register);
621
90c9960e 622/**
3e189519 623 * vmbus_child_driver_unregister() - Unregister a vmbus's child driver
06de23f7 624 * @drv: Pointer to driver structure you want to un-register
3e189519 625 *
3e189519
HJ
626 *
627 * Un-register the given driver with Linux through the 'driver_unregister()'
628 * call. And ungegisters the driver from the Hyper-V vmbus handler.
629 *
630 * Mainly used by Hyper-V drivers.
90c9960e 631 */
06de23f7 632void vmbus_child_driver_unregister(struct device_driver *drv)
3e7ee490 633{
0a46618d 634 pr_info("child driver unregistering - name %s\n", drv->name);
3e7ee490 635
06de23f7 636 driver_unregister(drv);
3e7ee490 637
06de23f7 638 drv->bus = NULL;
3e7ee490 639}
3e7ee490
HJ
640EXPORT_SYMBOL(vmbus_child_driver_unregister);
641
3e189519
HJ
642/*
643 * vmbus_child_device_create - Creates and registers a new child device
644 * on the vmbus.
90c9960e 645 */
89733aa9
GKH
646struct hv_device *vmbus_child_device_create(struct hv_guid *type,
647 struct hv_guid *instance,
648 struct vmbus_channel *channel)
3e7ee490 649{
3d3b5518 650 struct hv_device *child_device_obj;
3e7ee490 651
454f18a9 652 /* Allocate the new child device */
6bad88da
S
653 child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
654 if (!child_device_obj) {
0a46618d 655 pr_err("Unable to allocate device object for child device\n");
3e7ee490
HJ
656 return NULL;
657 }
658
cae5b843 659 child_device_obj->channel = channel;
ca623ad3
HZ
660 memcpy(&child_device_obj->dev_type, type, sizeof(struct hv_guid));
661 memcpy(&child_device_obj->dev_instance, instance,
90c9960e 662 sizeof(struct hv_guid));
3e7ee490 663
3e7ee490 664
3e7ee490
HJ
665 return child_device_obj;
666}
667
3e189519 668/*
e13a0b5a 669 * vmbus_child_device_register - Register the child device
90c9960e 670 */
3ca07cb0 671int vmbus_child_device_register(struct hv_device *child_device_obj)
3e7ee490 672{
90c9960e 673 int ret = 0;
6bad88da 674
f4888417 675 static atomic_t device_num = ATOMIC_INIT(0);
3e7ee490 676
1bb40a25 677 /* Set the device name. Otherwise, device_register() will fail. */
6bad88da 678 dev_set_name(&child_device_obj->device, "vmbus_0_%d",
90c9960e 679 atomic_inc_return(&device_num));
3e7ee490 680
454f18a9 681 /* The new device belongs to this bus */
9adcac5c 682 child_device_obj->device.bus = &hv_bus; /* device->dev.bus; */
800b6902 683 child_device_obj->device.parent = &hv_pci_dev->dev;
6bad88da 684 child_device_obj->device.release = vmbus_device_release;
3e7ee490 685
90c9960e
GKH
686 /*
687 * Register with the LDM. This will kick off the driver/device
688 * binding...which will eventually call vmbus_match() and vmbus_probe()
689 */
6bad88da 690 ret = device_register(&child_device_obj->device);
3e7ee490 691
3e7ee490 692 if (ret)
0a46618d 693 pr_err("Unable to register child device\n");
3e7ee490 694 else
0a46618d
HJ
695 pr_info("child device %s registered\n",
696 dev_name(&child_device_obj->device));
3e7ee490 697
3e7ee490
HJ
698 return ret;
699}
700
3e189519
HJ
701/*
702 * vmbus_child_device_unregister - Remove the specified child device
703 * from the vmbus.
90c9960e 704 */
9d8bd71a 705void vmbus_child_device_unregister(struct hv_device *device_obj)
3e7ee490 706{
90c9960e
GKH
707 /*
708 * Kick off the process of unregistering the device.
709 * This will call vmbus_remove() and eventually vmbus_device_release()
710 */
6bad88da 711 device_unregister(&device_obj->device);
3e7ee490 712
0a46618d
HJ
713 pr_info("child device %s unregistered\n",
714 dev_name(&device_obj->device));
3e7ee490
HJ
715}
716
3e7ee490 717
b0069f43
S
718/*
719 * VMBUS is an acpi enumerated device. Get the the IRQ information
720 * from DSDT.
721 */
722
723static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *irq)
724{
725
726 if (res->type == ACPI_RESOURCE_TYPE_IRQ) {
727 struct acpi_resource_irq *irqp;
728 irqp = &res->data.irq;
729
730 *((unsigned int *)irq) = irqp->interrupts[0];
731 }
732
733 return AE_OK;
734}
735
736static int vmbus_acpi_add(struct acpi_device *device)
737{
738 acpi_status result;
739
740 result =
741 acpi_walk_resources(device->handle, METHOD_NAME__CRS,
742 vmbus_walk_resources, &irq);
743
744 if (ACPI_FAILURE(result)) {
745 complete(&probe_event);
746 return -ENODEV;
747 }
748 complete(&probe_event);
749 return 0;
750}
751
752static const struct acpi_device_id vmbus_acpi_device_ids[] = {
753 {"VMBUS", 0},
754 {"", 0},
755};
756MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
757
758static struct acpi_driver vmbus_acpi_driver = {
759 .name = "vmbus",
760 .ids = vmbus_acpi_device_ids,
761 .ops = {
762 .add = vmbus_acpi_add,
763 },
764};
765
766static int vmbus_acpi_init(void)
767{
768 int result;
769
770
771 result = acpi_bus_register_driver(&vmbus_acpi_driver);
772 if (result < 0)
773 return result;
774
775 return 0;
776}
777
778static void vmbus_acpi_exit(void)
779{
780 acpi_bus_unregister_driver(&vmbus_acpi_driver);
781
782 return;
783}
784
785
1168ac22
S
786static int __devinit hv_pci_probe(struct pci_dev *pdev,
787 const struct pci_device_id *ent)
3e7ee490 788{
1168ac22 789 hv_pci_dev = pdev;
c22090fa 790
71a6655d
S
791 pci_probe_error = pci_enable_device(pdev);
792 if (pci_probe_error)
793 goto probe_cleanup;
3e7ee490 794
b0069f43
S
795 /*
796 * If the PCI sub-sytem did not assign us an
797 * irq, use the bios provided one.
798 */
799
800 if (pdev->irq == 0)
801 pdev->irq = irq;
802
71a6655d 803 pci_probe_error = vmbus_bus_init(pdev);
b0069f43 804
71a6655d 805 if (pci_probe_error)
1168ac22
S
806 pci_disable_device(pdev);
807
71a6655d
S
808probe_cleanup:
809 complete(&probe_event);
810 return pci_probe_error;
3e7ee490
HJ
811}
812
9a775dbd
GKH
813/*
814 * We use a PCI table to determine if we should autoload this driver This is
815 * needed by distro tools to determine if the hyperv drivers should be
816 * installed and/or configured. We don't do anything else with the table, but
817 * it needs to be present.
9a775dbd 818 */
15f0beb1 819static const struct pci_device_id microsoft_hv_pci_table[] = {
9a775dbd
GKH
820 { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
821 { 0 }
822};
823MODULE_DEVICE_TABLE(pci, microsoft_hv_pci_table);
824
1168ac22
S
825static struct pci_driver hv_bus_driver = {
826 .name = "hv_bus",
827 .probe = hv_pci_probe,
828 .id_table = microsoft_hv_pci_table,
829};
830
831static int __init hv_pci_init(void)
832{
71a6655d 833 int ret;
b0069f43
S
834
835 init_completion(&probe_event);
836
837 /*
838 * Get irq resources first.
839 */
840
841 ret = vmbus_acpi_init();
842 if (ret)
843 return ret;
844
845 wait_for_completion(&probe_event);
846
847 if (irq <= 0) {
848 vmbus_acpi_exit();
849 return -ENODEV;
850 }
851
852 vmbus_acpi_exit();
71a6655d
S
853 init_completion(&probe_event);
854 ret = pci_register_driver(&hv_bus_driver);
855 if (ret)
856 return ret;
857 /*
858 * All the vmbus initialization occurs within the
859 * hv_pci_probe() function. Wait for hv_pci_probe()
860 * to complete.
861 */
862 wait_for_completion(&probe_event);
863
864 if (pci_probe_error)
865 pci_unregister_driver(&hv_bus_driver);
866 return pci_probe_error;
1168ac22
S
867}
868
1168ac22 869
90c9960e 870MODULE_LICENSE("GPL");
26c14cc1 871MODULE_VERSION(HV_DRV_VERSION);
13399cba 872module_param(vmbus_loglevel, int, S_IRUGO|S_IWUSR);
3e7ee490 873
1168ac22 874module_init(hv_pci_init);