Merge branch 'nfs-for-3.1' of git://git.linux-nfs.org/projects/trondmy/linux-nfs
[cascardo/linux.git] / drivers / staging / hv / vmbus_drv.c
1 /*
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>
20  *   K. Y. Srinivasan <kys@microsoft.com>
21  *
22  */
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
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>
31 #include <linux/pci.h>
32 #include <linux/dmi.h>
33 #include <linux/slab.h>
34 #include <linux/acpi.h>
35 #include <acpi/acpi_bus.h>
36 #include <linux/completion.h>
37
38 #include "hyperv.h"
39 #include "hyperv_vmbus.h"
40
41
42 static struct acpi_device  *hv_acpi_dev;
43
44 static struct tasklet_struct msg_dpc;
45 static struct tasklet_struct event_dpc;
46
47 unsigned int vmbus_loglevel = (ALL_MODULES << 16 | INFO_LVL);
48 EXPORT_SYMBOL(vmbus_loglevel);
49         /* (ALL_MODULES << 16 | DEBUG_LVL_ENTEREXIT); */
50         /* (((VMBUS | VMBUS_DRV)<<16) | DEBUG_LVL_ENTEREXIT); */
51
52 static struct completion probe_event;
53 static int irq;
54
55 static 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  */
106 static ssize_t vmbus_show_device_attr(struct device *dev,
107                                       struct device_attribute *dev_attr,
108                                       char *buf)
109 {
110         struct hv_device *hv_dev = device_to_hv_device(dev);
111         struct hv_device_info device_info;
112
113         memset(&device_info, 0, sizeof(struct hv_device_info));
114
115         get_channel_info(hv_dev, &device_info);
116
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
204 /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
205 static 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 };
233
234
235 /*
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  */
242 static 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
247         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
295
296 /*
297  * vmbus_match - Attempt to match the specified device to the specified driver
298  */
299 static 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);
303         struct hv_device *hv_dev = device_to_hv_device(device);
304
305         /* We found our driver ? */
306         if (memcmp(&hv_dev->dev_type, &drv->dev_type,
307                    sizeof(struct hv_guid)) == 0)
308                 match = 1;
309
310         return match;
311 }
312
313 /*
314  * vmbus_probe - Add the new vmbus's child device
315  */
316 static 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);
321         struct hv_device *dev = device_to_hv_device(child_device);
322
323         if (drv->probe) {
324                 ret = drv->probe(dev);
325                 if (ret != 0)
326                         pr_err("probe failed for device %s (%d)\n",
327                                dev_name(child_device), ret);
328
329         } else {
330                 pr_err("probe not set for driver %s\n",
331                        dev_name(child_device));
332                 ret = -ENODEV;
333         }
334         return ret;
335 }
336
337 /*
338  * vmbus_remove - Remove a vmbus device
339  */
340 static int vmbus_remove(struct device *child_device)
341 {
342         int ret;
343         struct hv_driver *drv;
344
345         struct hv_device *dev = device_to_hv_device(child_device);
346
347         if (child_device->driver) {
348                 drv = drv_to_hv_drv(child_device->driver);
349
350                 if (drv->remove) {
351                         ret = drv->remove(dev);
352                 } else {
353                         pr_err("remove not set for driver %s\n",
354                                 dev_name(child_device));
355                         ret = -ENODEV;
356                 }
357         }
358
359         return 0;
360 }
361
362
363 /*
364  * vmbus_shutdown - Shutdown a vmbus device
365  */
366 static void vmbus_shutdown(struct device *child_device)
367 {
368         struct hv_driver *drv;
369         struct hv_device *dev = device_to_hv_device(child_device);
370
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
378         if (drv->shutdown)
379                 drv->shutdown(dev);
380
381         return;
382 }
383
384
385 /*
386  * vmbus_device_release - Final callback release of the vmbus child device
387  */
388 static void vmbus_device_release(struct device *device)
389 {
390         struct hv_device *hv_dev = device_to_hv_device(device);
391
392         kfree(hv_dev);
393
394 }
395
396 /* The one and only one */
397 static 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,
405 };
406
407 static const char *driver_name = "hyperv";
408
409
410 struct onmessage_work_context {
411         struct work_struct work;
412         struct hv_message msg;
413 };
414
415 static 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
425 /*
426  * vmbus_on_msg_dpc - DPC routine to handle messages from the hypervisior
427  */
428 static void vmbus_on_msg_dpc(unsigned long data)
429 {
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;
434         struct onmessage_work_context *ctx;
435
436         while (1) {
437                 if (msg->header.message_type == HVMSG_NONE) {
438                         /* no msg */
439                         break;
440                 } else {
441                         ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
442                         if (ctx == NULL)
443                                 continue;
444                         INIT_WORK(&ctx->work, vmbus_onmessage_work);
445                         memcpy(&ctx->msg, msg, sizeof(*msg));
446                         queue_work(vmbus_connection.work_queue, &ctx->work);
447                 }
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                  */
458                 smp_mb();
459
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 /*
472  * vmbus_on_isr - ISR routine
473  */
474 static int vmbus_on_isr(void)
475 {
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 */
486         if (msg->header.message_type != HVMSG_NONE)
487                 ret |= 0x1;
488
489         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 */
493         if (sync_test_and_clear_bit(0, (unsigned long *) &event->flags32[0]))
494                 ret |= 0x2;
495
496         return ret;
497 }
498
499
500 static 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))
509                         tasklet_schedule(&msg_dpc);
510
511                 if (test_bit(1, (unsigned long *)&ret))
512                         tasklet_schedule(&event_dpc);
513
514                 return IRQ_HANDLED;
515         } else {
516                 return IRQ_NONE;
517         }
518 }
519
520 /*
521  * vmbus_bus_init -Main vmbus driver initialization routine.
522  *
523  * Here, we
524  *      - initialize the vmbus driver context
525  *      - invoke the vmbus hv main init routine
526  *      - get the irq resource
527  *      - retrieve the channel offers
528  */
529 static int vmbus_bus_init(int irq)
530 {
531         int ret;
532         unsigned int vector;
533
534         /* Hypervisor initialization...setup hypercall page..etc */
535         ret = hv_init();
536         if (ret != 0) {
537                 pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
538                 return ret;
539         }
540
541         /* Initialize the bus context */
542         tasklet_init(&msg_dpc, vmbus_on_msg_dpc, 0);
543         tasklet_init(&event_dpc, vmbus_on_event, 0);
544
545         /* Now, register the bus  with LDM */
546         ret = bus_register(&hv_bus);
547         if (ret)
548                 return ret;
549
550         /* Get the interrupt resource */
551         ret = request_irq(irq, vmbus_isr, IRQF_SAMPLE_RANDOM,
552                         driver_name, hv_acpi_dev);
553
554         if (ret != 0) {
555                 pr_err("Unable to request IRQ %d\n",
556                            irq);
557
558                 bus_unregister(&hv_bus);
559
560                 return ret;
561         }
562
563         vector = IRQ0_VECTOR + irq;
564
565         /*
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();
571         if (ret) {
572                 free_irq(irq, hv_acpi_dev);
573                 bus_unregister(&hv_bus);
574                 return ret;
575         }
576
577
578         vmbus_request_offers();
579
580         return 0;
581 }
582
583 /**
584  * vmbus_child_driver_register() - Register a vmbus's child driver
585  * @drv:        Pointer to driver structure you want to register
586  *
587  *
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.
593  */
594 int vmbus_child_driver_register(struct device_driver *drv)
595 {
596         int ret;
597
598         pr_info("child driver registering - name %s\n", drv->name);
599
600         /* The child driver on this vmbus */
601         drv->bus = &hv_bus;
602
603         ret = driver_register(drv);
604
605         vmbus_request_offers();
606
607         return ret;
608 }
609 EXPORT_SYMBOL(vmbus_child_driver_register);
610
611 /**
612  * vmbus_child_driver_unregister() - Unregister a vmbus's child driver
613  * @drv:        Pointer to driver structure you want to un-register
614  *
615  *
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.
620  */
621 void vmbus_child_driver_unregister(struct device_driver *drv)
622 {
623         pr_info("child driver unregistering - name %s\n", drv->name);
624
625         driver_unregister(drv);
626
627 }
628 EXPORT_SYMBOL(vmbus_child_driver_unregister);
629
630 /*
631  * vmbus_child_device_create - Creates and registers a new child device
632  * on the vmbus.
633  */
634 struct hv_device *vmbus_child_device_create(struct hv_guid *type,
635                                             struct hv_guid *instance,
636                                             struct vmbus_channel *channel)
637 {
638         struct hv_device *child_device_obj;
639
640         /* Allocate the new child device */
641         child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
642         if (!child_device_obj) {
643                 pr_err("Unable to allocate device object for child device\n");
644                 return NULL;
645         }
646
647         child_device_obj->channel = channel;
648         memcpy(&child_device_obj->dev_type, type, sizeof(struct hv_guid));
649         memcpy(&child_device_obj->dev_instance, instance,
650                sizeof(struct hv_guid));
651
652
653         return child_device_obj;
654 }
655
656 /*
657  * vmbus_child_device_register - Register the child device
658  */
659 int vmbus_child_device_register(struct hv_device *child_device_obj)
660 {
661         int ret = 0;
662
663         static atomic_t device_num = ATOMIC_INIT(0);
664
665         /* Set the device name. Otherwise, device_register() will fail. */
666         dev_set_name(&child_device_obj->device, "vmbus_0_%d",
667                      atomic_inc_return(&device_num));
668
669         /* The new device belongs to this bus */
670         child_device_obj->device.bus = &hv_bus; /* device->dev.bus; */
671         child_device_obj->device.parent = &hv_acpi_dev->dev;
672         child_device_obj->device.release = vmbus_device_release;
673
674         /*
675          * Register with the LDM. This will kick off the driver/device
676          * binding...which will eventually call vmbus_match() and vmbus_probe()
677          */
678         ret = device_register(&child_device_obj->device);
679
680         if (ret)
681                 pr_err("Unable to register child device\n");
682         else
683                 pr_info("child device %s registered\n",
684                         dev_name(&child_device_obj->device));
685
686         return ret;
687 }
688
689 /*
690  * vmbus_child_device_unregister - Remove the specified child device
691  * from the vmbus.
692  */
693 void vmbus_child_device_unregister(struct hv_device *device_obj)
694 {
695         /*
696          * Kick off the process of unregistering the device.
697          * This will call vmbus_remove() and eventually vmbus_device_release()
698          */
699         device_unregister(&device_obj->device);
700
701         pr_info("child device %s unregistered\n",
702                 dev_name(&device_obj->device));
703 }
704
705
706 /*
707  * VMBUS is an acpi enumerated device. Get the the IRQ information
708  * from DSDT.
709  */
710
711 static 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
724 static int vmbus_acpi_add(struct acpi_device *device)
725 {
726         acpi_status result;
727
728         hv_acpi_dev = device;
729
730         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
742 static const struct acpi_device_id vmbus_acpi_device_ids[] = {
743         {"VMBUS", 0},
744         {"VMBus", 0},
745         {"", 0},
746 };
747 MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
748
749 static 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
757 /*
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.
762  */
763 static const struct pci_device_id microsoft_hv_pci_table[] = {
764         { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
765         { 0 }
766 };
767 MODULE_DEVICE_TABLE(pci, microsoft_hv_pci_table);
768
769 static int __init hv_acpi_init(void)
770 {
771         int ret;
772
773         init_completion(&probe_event);
774
775         /*
776          * Get irq resources first.
777          */
778
779         ret = acpi_bus_register_driver(&vmbus_acpi_driver);
780
781         if (ret)
782                 return ret;
783
784         wait_for_completion(&probe_event);
785
786         if (irq <= 0) {
787                 acpi_bus_unregister_driver(&vmbus_acpi_driver);
788                 return -ENODEV;
789         }
790
791         ret = vmbus_bus_init(irq);
792         if (ret)
793                 acpi_bus_unregister_driver(&vmbus_acpi_driver);
794         return ret;
795 }
796
797
798 MODULE_LICENSE("GPL");
799 MODULE_VERSION(HV_DRV_VERSION);
800 module_param(vmbus_loglevel, int, S_IRUGO|S_IWUSR);
801
802 module_init(hv_acpi_init);