]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/hv/vmbus_drv.c
Staging: hv: remove OnEventDpc vmbus_driver callback
[mv-sheeva.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  */
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/device.h>
24 #include <linux/irq.h>
25 #include <linux/interrupt.h>
26 #include <linux/sysctl.h>
27 #include <linux/pci.h>
28 #include <linux/dmi.h>
29 #include <linux/slab.h>
30 #include <linux/completion.h>
31 #include "version_info.h"
32 #include "osd.h"
33 #include "logging.h"
34 #include "vmbus.h"
35 #include "channel.h"
36
37
38 /* FIXME! We need to do this dynamically for PIC and APIC system */
39 #define VMBUS_IRQ               0x5
40 #define VMBUS_IRQ_VECTOR        IRQ5_VECTOR
41
42 /* Main vmbus driver data structure */
43 struct vmbus_driver_context {
44         /* !! These must be the first 2 fields !! */
45         /* FIXME, this is a bug */
46         /* The driver field is not used in here. Instead, the bus field is */
47         /* used to represent the driver */
48         struct driver_context drv_ctx;
49         struct vmbus_driver drv_obj;
50
51         struct bus_type bus;
52         struct tasklet_struct msg_dpc;
53         struct tasklet_struct event_dpc;
54
55         /* The bus root device */
56         struct vm_device device_ctx;
57 };
58
59 static int vmbus_match(struct device *device, struct device_driver *driver);
60 static int vmbus_probe(struct device *device);
61 static int vmbus_remove(struct device *device);
62 static void vmbus_shutdown(struct device *device);
63 static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env);
64 static void vmbus_msg_dpc(unsigned long data);
65 static void vmbus_event_dpc(unsigned long data);
66
67 static irqreturn_t vmbus_isr(int irq, void *dev_id);
68
69 static void vmbus_device_release(struct device *device);
70 static void vmbus_bus_release(struct device *device);
71
72 static int vmbus_child_device_register(struct hv_device *root_device_obj,
73                                        struct hv_device *child_device_obj);
74 static ssize_t vmbus_show_device_attr(struct device *dev,
75                                       struct device_attribute *dev_attr,
76                                       char *buf);
77
78
79 unsigned int vmbus_loglevel = (ALL_MODULES << 16 | INFO_LVL);
80 EXPORT_SYMBOL(vmbus_loglevel);
81         /* (ALL_MODULES << 16 | DEBUG_LVL_ENTEREXIT); */
82         /* (((VMBUS | VMBUS_DRV)<<16) | DEBUG_LVL_ENTEREXIT); */
83
84 static int vmbus_irq = VMBUS_IRQ;
85
86 /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
87 static struct device_attribute vmbus_device_attrs[] = {
88         __ATTR(id, S_IRUGO, vmbus_show_device_attr, NULL),
89         __ATTR(state, S_IRUGO, vmbus_show_device_attr, NULL),
90         __ATTR(class_id, S_IRUGO, vmbus_show_device_attr, NULL),
91         __ATTR(device_id, S_IRUGO, vmbus_show_device_attr, NULL),
92         __ATTR(monitor_id, S_IRUGO, vmbus_show_device_attr, NULL),
93
94         __ATTR(server_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL),
95         __ATTR(server_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL),
96         __ATTR(server_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL),
97
98         __ATTR(client_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL),
99         __ATTR(client_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL),
100         __ATTR(client_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL),
101
102         __ATTR(out_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL),
103         __ATTR(out_read_index, S_IRUGO, vmbus_show_device_attr, NULL),
104         __ATTR(out_write_index, S_IRUGO, vmbus_show_device_attr, NULL),
105         __ATTR(out_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
106         __ATTR(out_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
107
108         __ATTR(in_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL),
109         __ATTR(in_read_index, S_IRUGO, vmbus_show_device_attr, NULL),
110         __ATTR(in_write_index, S_IRUGO, vmbus_show_device_attr, NULL),
111         __ATTR(in_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
112         __ATTR(in_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
113         __ATTR_NULL
114 };
115
116 /* The one and only one */
117 static struct vmbus_driver_context g_vmbus_drv = {
118         .bus.name =             "vmbus",
119         .bus.match =            vmbus_match,
120         .bus.shutdown =         vmbus_shutdown,
121         .bus.remove =           vmbus_remove,
122         .bus.probe =            vmbus_probe,
123         .bus.uevent =           vmbus_uevent,
124         .bus.dev_attrs =        vmbus_device_attrs,
125 };
126
127 static void get_channel_info(struct hv_device *device,
128                              struct hv_device_info *info)
129 {
130         struct vmbus_channel_debug_info debug_info;
131
132         if (!device->channel)
133                 return;
134
135         vmbus_get_debug_info(device->channel, &debug_info);
136
137         info->ChannelId = debug_info.relid;
138         info->ChannelState = debug_info.state;
139         memcpy(&info->ChannelType, &debug_info.interfacetype,
140                sizeof(struct hv_guid));
141         memcpy(&info->ChannelInstance, &debug_info.interface_instance,
142                sizeof(struct hv_guid));
143
144         info->MonitorId = debug_info.monitorid;
145
146         info->ServerMonitorPending = debug_info.servermonitor_pending;
147         info->ServerMonitorLatency = debug_info.servermonitor_latency;
148         info->ServerMonitorConnectionId = debug_info.servermonitor_connectionid;
149
150         info->ClientMonitorPending = debug_info.clientmonitor_pending;
151         info->ClientMonitorLatency = debug_info.clientmonitor_latency;
152         info->ClientMonitorConnectionId = debug_info.clientmonitor_connectionid;
153
154         info->Inbound.InterruptMask = debug_info.inbound.current_interrupt_mask;
155         info->Inbound.ReadIndex = debug_info.inbound.current_read_index;
156         info->Inbound.WriteIndex = debug_info.inbound.current_write_index;
157         info->Inbound.BytesAvailToRead = debug_info.inbound.bytes_avail_toread;
158         info->Inbound.BytesAvailToWrite =
159                 debug_info.inbound.bytes_avail_towrite;
160
161         info->Outbound.InterruptMask =
162                 debug_info.outbound.current_interrupt_mask;
163         info->Outbound.ReadIndex = debug_info.outbound.current_read_index;
164         info->Outbound.WriteIndex = debug_info.outbound.current_write_index;
165         info->Outbound.BytesAvailToRead =
166                 debug_info.outbound.bytes_avail_toread;
167         info->Outbound.BytesAvailToWrite =
168                 debug_info.outbound.bytes_avail_towrite;
169 }
170
171 /*
172  * vmbus_show_device_attr - Show the device attribute in sysfs.
173  *
174  * This is invoked when user does a
175  * "cat /sys/bus/vmbus/devices/<busdevice>/<attr name>"
176  */
177 static ssize_t vmbus_show_device_attr(struct device *dev,
178                                       struct device_attribute *dev_attr,
179                                       char *buf)
180 {
181         struct vm_device *device_ctx = device_to_vm_device(dev);
182         struct hv_device_info device_info;
183
184         memset(&device_info, 0, sizeof(struct hv_device_info));
185
186         get_channel_info(&device_ctx->device_obj, &device_info);
187
188         if (!strcmp(dev_attr->attr.name, "class_id")) {
189                 return sprintf(buf, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-"
190                                "%02x%02x%02x%02x%02x%02x%02x%02x}\n",
191                                device_info.ChannelType.data[3],
192                                device_info.ChannelType.data[2],
193                                device_info.ChannelType.data[1],
194                                device_info.ChannelType.data[0],
195                                device_info.ChannelType.data[5],
196                                device_info.ChannelType.data[4],
197                                device_info.ChannelType.data[7],
198                                device_info.ChannelType.data[6],
199                                device_info.ChannelType.data[8],
200                                device_info.ChannelType.data[9],
201                                device_info.ChannelType.data[10],
202                                device_info.ChannelType.data[11],
203                                device_info.ChannelType.data[12],
204                                device_info.ChannelType.data[13],
205                                device_info.ChannelType.data[14],
206                                device_info.ChannelType.data[15]);
207         } else if (!strcmp(dev_attr->attr.name, "device_id")) {
208                 return sprintf(buf, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-"
209                                "%02x%02x%02x%02x%02x%02x%02x%02x}\n",
210                                device_info.ChannelInstance.data[3],
211                                device_info.ChannelInstance.data[2],
212                                device_info.ChannelInstance.data[1],
213                                device_info.ChannelInstance.data[0],
214                                device_info.ChannelInstance.data[5],
215                                device_info.ChannelInstance.data[4],
216                                device_info.ChannelInstance.data[7],
217                                device_info.ChannelInstance.data[6],
218                                device_info.ChannelInstance.data[8],
219                                device_info.ChannelInstance.data[9],
220                                device_info.ChannelInstance.data[10],
221                                device_info.ChannelInstance.data[11],
222                                device_info.ChannelInstance.data[12],
223                                device_info.ChannelInstance.data[13],
224                                device_info.ChannelInstance.data[14],
225                                device_info.ChannelInstance.data[15]);
226         } else if (!strcmp(dev_attr->attr.name, "state")) {
227                 return sprintf(buf, "%d\n", device_info.ChannelState);
228         } else if (!strcmp(dev_attr->attr.name, "id")) {
229                 return sprintf(buf, "%d\n", device_info.ChannelId);
230         } else if (!strcmp(dev_attr->attr.name, "out_intr_mask")) {
231                 return sprintf(buf, "%d\n", device_info.Outbound.InterruptMask);
232         } else if (!strcmp(dev_attr->attr.name, "out_read_index")) {
233                 return sprintf(buf, "%d\n", device_info.Outbound.ReadIndex);
234         } else if (!strcmp(dev_attr->attr.name, "out_write_index")) {
235                 return sprintf(buf, "%d\n", device_info.Outbound.WriteIndex);
236         } else if (!strcmp(dev_attr->attr.name, "out_read_bytes_avail")) {
237                 return sprintf(buf, "%d\n",
238                                device_info.Outbound.BytesAvailToRead);
239         } else if (!strcmp(dev_attr->attr.name, "out_write_bytes_avail")) {
240                 return sprintf(buf, "%d\n",
241                                device_info.Outbound.BytesAvailToWrite);
242         } else if (!strcmp(dev_attr->attr.name, "in_intr_mask")) {
243                 return sprintf(buf, "%d\n", device_info.Inbound.InterruptMask);
244         } else if (!strcmp(dev_attr->attr.name, "in_read_index")) {
245                 return sprintf(buf, "%d\n", device_info.Inbound.ReadIndex);
246         } else if (!strcmp(dev_attr->attr.name, "in_write_index")) {
247                 return sprintf(buf, "%d\n", device_info.Inbound.WriteIndex);
248         } else if (!strcmp(dev_attr->attr.name, "in_read_bytes_avail")) {
249                 return sprintf(buf, "%d\n",
250                                device_info.Inbound.BytesAvailToRead);
251         } else if (!strcmp(dev_attr->attr.name, "in_write_bytes_avail")) {
252                 return sprintf(buf, "%d\n",
253                                device_info.Inbound.BytesAvailToWrite);
254         } else if (!strcmp(dev_attr->attr.name, "monitor_id")) {
255                 return sprintf(buf, "%d\n", device_info.MonitorId);
256         } else if (!strcmp(dev_attr->attr.name, "server_monitor_pending")) {
257                 return sprintf(buf, "%d\n", device_info.ServerMonitorPending);
258         } else if (!strcmp(dev_attr->attr.name, "server_monitor_latency")) {
259                 return sprintf(buf, "%d\n", device_info.ServerMonitorLatency);
260         } else if (!strcmp(dev_attr->attr.name, "server_monitor_conn_id")) {
261                 return sprintf(buf, "%d\n",
262                                device_info.ServerMonitorConnectionId);
263         } else if (!strcmp(dev_attr->attr.name, "client_monitor_pending")) {
264                 return sprintf(buf, "%d\n", device_info.ClientMonitorPending);
265         } else if (!strcmp(dev_attr->attr.name, "client_monitor_latency")) {
266                 return sprintf(buf, "%d\n", device_info.ClientMonitorLatency);
267         } else if (!strcmp(dev_attr->attr.name, "client_monitor_conn_id")) {
268                 return sprintf(buf, "%d\n",
269                                device_info.ClientMonitorConnectionId);
270         } else {
271                 return 0;
272         }
273 }
274
275 /*
276  * vmbus_bus_init -Main vmbus driver initialization routine.
277  *
278  * Here, we
279  *      - initialize the vmbus driver context
280  *      - setup various driver entry points
281  *      - invoke the vmbus hv main init routine
282  *      - get the irq resource
283  *      - invoke the vmbus to add the vmbus root device
284  *      - setup the vmbus root device
285  *      - retrieve the channel offers
286  */
287 static int vmbus_bus_init(int (*drv_init)(struct hv_driver *drv))
288 {
289         struct vmbus_driver_context *vmbus_drv_ctx = &g_vmbus_drv;
290         struct vmbus_driver *vmbus_drv_obj = &g_vmbus_drv.drv_obj;
291         struct vm_device *dev_ctx = &g_vmbus_drv.device_ctx;
292         int ret;
293         unsigned int vector;
294
295         /*
296          * Set this up to allow lower layer to callback to add/remove child
297          * devices on the bus
298          */
299         vmbus_drv_obj->OnChildDeviceAdd = vmbus_child_device_register;
300
301         /* Call to bus driver to initialize */
302         ret = drv_init(&vmbus_drv_obj->Base);
303         if (ret != 0) {
304                 DPRINT_ERR(VMBUS_DRV, "Unable to initialize vmbus (%d)", ret);
305                 goto cleanup;
306         }
307
308         /* Sanity checks */
309         if (!vmbus_drv_obj->Base.OnDeviceAdd) {
310                 DPRINT_ERR(VMBUS_DRV, "OnDeviceAdd() routine not set");
311                 ret = -1;
312                 goto cleanup;
313         }
314
315         vmbus_drv_ctx->bus.name = vmbus_drv_obj->Base.name;
316
317         /* Initialize the bus context */
318         tasklet_init(&vmbus_drv_ctx->msg_dpc, vmbus_msg_dpc,
319                      (unsigned long)vmbus_drv_obj);
320         tasklet_init(&vmbus_drv_ctx->event_dpc, vmbus_event_dpc,
321                      (unsigned long)vmbus_drv_obj);
322
323         /* Now, register the bus driver with LDM */
324         ret = bus_register(&vmbus_drv_ctx->bus);
325         if (ret) {
326                 ret = -1;
327                 goto cleanup;
328         }
329
330         /* Get the interrupt resource */
331         ret = request_irq(vmbus_irq, vmbus_isr, IRQF_SAMPLE_RANDOM,
332                           vmbus_drv_obj->Base.name, NULL);
333
334         if (ret != 0) {
335                 DPRINT_ERR(VMBUS_DRV, "ERROR - Unable to request IRQ %d",
336                            vmbus_irq);
337
338                 bus_unregister(&vmbus_drv_ctx->bus);
339
340                 ret = -1;
341                 goto cleanup;
342         }
343         vector = VMBUS_IRQ_VECTOR;
344
345         DPRINT_INFO(VMBUS_DRV, "irq 0x%x vector 0x%x", vmbus_irq, vector);
346
347         /* Call to bus driver to add the root device */
348         memset(dev_ctx, 0, sizeof(struct vm_device));
349
350         ret = vmbus_drv_obj->Base.OnDeviceAdd(&dev_ctx->device_obj, &vector);
351         if (ret != 0) {
352                 DPRINT_ERR(VMBUS_DRV,
353                            "ERROR - Unable to add vmbus root device");
354
355                 free_irq(vmbus_irq, NULL);
356
357                 bus_unregister(&vmbus_drv_ctx->bus);
358
359                 ret = -1;
360                 goto cleanup;
361         }
362         /* strcpy(dev_ctx->device.bus_id, dev_ctx->device_obj.name); */
363         dev_set_name(&dev_ctx->device, "vmbus_0_0");
364         memcpy(&dev_ctx->class_id, &dev_ctx->device_obj.deviceType,
365                 sizeof(struct hv_guid));
366         memcpy(&dev_ctx->device_id, &dev_ctx->device_obj.deviceInstance,
367                 sizeof(struct hv_guid));
368
369         /* No need to bind a driver to the root device. */
370         dev_ctx->device.parent = NULL;
371         /* NULL; vmbus_remove() does not get invoked */
372         dev_ctx->device.bus = &vmbus_drv_ctx->bus;
373
374         /* Setup the device dispatch table */
375         dev_ctx->device.release = vmbus_bus_release;
376
377         /* Setup the bus as root device */
378         ret = device_register(&dev_ctx->device);
379         if (ret) {
380                 DPRINT_ERR(VMBUS_DRV,
381                            "ERROR - Unable to register vmbus root device");
382
383                 free_irq(vmbus_irq, NULL);
384                 bus_unregister(&vmbus_drv_ctx->bus);
385
386                 ret = -1;
387                 goto cleanup;
388         }
389
390
391         vmbus_drv_obj->GetChannelOffers();
392
393         wait_for_completion(&hv_channel_ready);
394
395 cleanup:
396         return ret;
397 }
398
399 /*
400  * vmbus_bus_exit - Terminate the vmbus driver.
401  *
402  * This routine is opposite of vmbus_bus_init()
403  */
404 static void vmbus_bus_exit(void)
405 {
406         struct vmbus_driver *vmbus_drv_obj = &g_vmbus_drv.drv_obj;
407         struct vmbus_driver_context *vmbus_drv_ctx = &g_vmbus_drv;
408
409         struct vm_device *dev_ctx = &g_vmbus_drv.device_ctx;
410
411         /* Remove the root device */
412         if (vmbus_drv_obj->Base.OnDeviceRemove)
413                 vmbus_drv_obj->Base.OnDeviceRemove(&dev_ctx->device_obj);
414
415         if (vmbus_drv_obj->Base.OnCleanup)
416                 vmbus_drv_obj->Base.OnCleanup(&vmbus_drv_obj->Base);
417
418         /* Unregister the root bus device */
419         device_unregister(&dev_ctx->device);
420
421         bus_unregister(&vmbus_drv_ctx->bus);
422
423         free_irq(vmbus_irq, NULL);
424
425         tasklet_kill(&vmbus_drv_ctx->msg_dpc);
426         tasklet_kill(&vmbus_drv_ctx->event_dpc);
427 }
428
429
430 /**
431  * vmbus_child_driver_register() - Register a vmbus's child driver
432  * @driver_ctx:        Pointer to driver structure you want to register
433  *
434  * @driver_ctx is of type &struct driver_context
435  *
436  * Registers the given driver with Linux through the 'driver_register()' call
437  * And sets up the hyper-v vmbus handling for this driver.
438  * It will return the state of the 'driver_register()' call.
439  *
440  * Mainly used by Hyper-V drivers.
441  */
442 int vmbus_child_driver_register(struct driver_context *driver_ctx)
443 {
444         struct vmbus_driver *vmbus_drv_obj = &g_vmbus_drv.drv_obj;
445         int ret;
446
447         DPRINT_INFO(VMBUS_DRV, "child driver (%p) registering - name %s",
448                     driver_ctx, driver_ctx->driver.name);
449
450         /* The child driver on this vmbus */
451         driver_ctx->driver.bus = &g_vmbus_drv.bus;
452
453         ret = driver_register(&driver_ctx->driver);
454
455         vmbus_drv_obj->GetChannelOffers();
456
457         return ret;
458 }
459 EXPORT_SYMBOL(vmbus_child_driver_register);
460
461 /**
462  * vmbus_child_driver_unregister() - Unregister a vmbus's child driver
463  * @driver_ctx:        Pointer to driver structure you want to un-register
464  *
465  * @driver_ctx is of type &struct driver_context
466  *
467  * Un-register the given driver with Linux through the 'driver_unregister()'
468  * call. And ungegisters the driver from the Hyper-V vmbus handler.
469  *
470  * Mainly used by Hyper-V drivers.
471  */
472 void vmbus_child_driver_unregister(struct driver_context *driver_ctx)
473 {
474         DPRINT_INFO(VMBUS_DRV, "child driver (%p) unregistering - name %s",
475                     driver_ctx, driver_ctx->driver.name);
476
477         driver_unregister(&driver_ctx->driver);
478
479         driver_ctx->driver.bus = NULL;
480 }
481 EXPORT_SYMBOL(vmbus_child_driver_unregister);
482
483 /*
484  * vmbus_child_device_create - Creates and registers a new child device
485  * on the vmbus.
486  */
487 struct hv_device *vmbus_child_device_create(struct hv_guid *type,
488                                             struct hv_guid *instance,
489                                             struct vmbus_channel *channel)
490 {
491         struct vm_device *child_device_ctx;
492         struct hv_device *child_device_obj;
493
494         /* Allocate the new child device */
495         child_device_ctx = kzalloc(sizeof(struct vm_device), GFP_KERNEL);
496         if (!child_device_ctx) {
497                 DPRINT_ERR(VMBUS_DRV,
498                         "unable to allocate device_context for child device");
499                 return NULL;
500         }
501
502         DPRINT_DBG(VMBUS_DRV, "child device (%p) allocated - "
503                 "type {%02x%02x%02x%02x-%02x%02x-%02x%02x-"
504                 "%02x%02x%02x%02x%02x%02x%02x%02x},"
505                 "id {%02x%02x%02x%02x-%02x%02x-%02x%02x-"
506                 "%02x%02x%02x%02x%02x%02x%02x%02x}",
507                 &child_device_ctx->device,
508                 type->data[3], type->data[2], type->data[1], type->data[0],
509                 type->data[5], type->data[4], type->data[7], type->data[6],
510                 type->data[8], type->data[9], type->data[10], type->data[11],
511                 type->data[12], type->data[13], type->data[14], type->data[15],
512                 instance->data[3], instance->data[2],
513                 instance->data[1], instance->data[0],
514                 instance->data[5], instance->data[4],
515                 instance->data[7], instance->data[6],
516                 instance->data[8], instance->data[9],
517                 instance->data[10], instance->data[11],
518                 instance->data[12], instance->data[13],
519                 instance->data[14], instance->data[15]);
520
521         child_device_obj = &child_device_ctx->device_obj;
522         child_device_obj->channel = channel;
523         memcpy(&child_device_obj->deviceType, type, sizeof(struct hv_guid));
524         memcpy(&child_device_obj->deviceInstance, instance,
525                sizeof(struct hv_guid));
526
527         memcpy(&child_device_ctx->class_id, type, sizeof(struct hv_guid));
528         memcpy(&child_device_ctx->device_id, instance, sizeof(struct hv_guid));
529
530         return child_device_obj;
531 }
532
533 /*
534  * vmbus_child_device_register - Register the child device on the specified bus
535  */
536 static int vmbus_child_device_register(struct hv_device *root_device_obj,
537                                        struct hv_device *child_device_obj)
538 {
539         int ret = 0;
540         struct vm_device *root_device_ctx =
541                                 to_vm_device(root_device_obj);
542         struct vm_device *child_device_ctx =
543                                 to_vm_device(child_device_obj);
544         static atomic_t device_num = ATOMIC_INIT(0);
545
546         DPRINT_DBG(VMBUS_DRV, "child device (%p) registering",
547                    child_device_ctx);
548
549         /* Set the device name. Otherwise, device_register() will fail. */
550         dev_set_name(&child_device_ctx->device, "vmbus_0_%d",
551                      atomic_inc_return(&device_num));
552
553         /* The new device belongs to this bus */
554         child_device_ctx->device.bus = &g_vmbus_drv.bus; /* device->dev.bus; */
555         child_device_ctx->device.parent = &root_device_ctx->device;
556         child_device_ctx->device.release = vmbus_device_release;
557
558         /*
559          * Register with the LDM. This will kick off the driver/device
560          * binding...which will eventually call vmbus_match() and vmbus_probe()
561          */
562         ret = device_register(&child_device_ctx->device);
563
564         /* vmbus_probe() error does not get propergate to device_register(). */
565         ret = child_device_ctx->probe_error;
566
567         if (ret)
568                 DPRINT_ERR(VMBUS_DRV, "unable to register child device (%p)",
569                            &child_device_ctx->device);
570         else
571                 DPRINT_INFO(VMBUS_DRV, "child device (%p) registered",
572                             &child_device_ctx->device);
573
574         return ret;
575 }
576
577 /*
578  * vmbus_child_device_unregister - Remove the specified child device
579  * from the vmbus.
580  */
581 void vmbus_child_device_unregister(struct hv_device *device_obj)
582 {
583         struct vm_device *device_ctx = to_vm_device(device_obj);
584
585         DPRINT_INFO(VMBUS_DRV, "unregistering child device (%p)",
586                     &device_ctx->device);
587
588         /*
589          * Kick off the process of unregistering the device.
590          * This will call vmbus_remove() and eventually vmbus_device_release()
591          */
592         device_unregister(&device_ctx->device);
593
594         DPRINT_INFO(VMBUS_DRV, "child device (%p) unregistered",
595                     &device_ctx->device);
596 }
597
598 /*
599  * vmbus_uevent - add uevent for our device
600  *
601  * This routine is invoked when a device is added or removed on the vmbus to
602  * generate a uevent to udev in the userspace. The udev will then look at its
603  * rule and the uevent generated here to load the appropriate driver
604  */
605 static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
606 {
607         struct vm_device *device_ctx = device_to_vm_device(device);
608         int ret;
609
610         DPRINT_INFO(VMBUS_DRV, "generating uevent - VMBUS_DEVICE_CLASS_GUID={"
611                     "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
612                     "%02x%02x%02x%02x%02x%02x%02x%02x}",
613                     device_ctx->class_id.data[3], device_ctx->class_id.data[2],
614                     device_ctx->class_id.data[1], device_ctx->class_id.data[0],
615                     device_ctx->class_id.data[5], device_ctx->class_id.data[4],
616                     device_ctx->class_id.data[7], device_ctx->class_id.data[6],
617                     device_ctx->class_id.data[8], device_ctx->class_id.data[9],
618                     device_ctx->class_id.data[10],
619                     device_ctx->class_id.data[11],
620                     device_ctx->class_id.data[12],
621                     device_ctx->class_id.data[13],
622                     device_ctx->class_id.data[14],
623                     device_ctx->class_id.data[15]);
624
625         ret = add_uevent_var(env, "VMBUS_DEVICE_CLASS_GUID={"
626                              "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
627                              "%02x%02x%02x%02x%02x%02x%02x%02x}",
628                              device_ctx->class_id.data[3],
629                              device_ctx->class_id.data[2],
630                              device_ctx->class_id.data[1],
631                              device_ctx->class_id.data[0],
632                              device_ctx->class_id.data[5],
633                              device_ctx->class_id.data[4],
634                              device_ctx->class_id.data[7],
635                              device_ctx->class_id.data[6],
636                              device_ctx->class_id.data[8],
637                              device_ctx->class_id.data[9],
638                              device_ctx->class_id.data[10],
639                              device_ctx->class_id.data[11],
640                              device_ctx->class_id.data[12],
641                              device_ctx->class_id.data[13],
642                              device_ctx->class_id.data[14],
643                              device_ctx->class_id.data[15]);
644
645         if (ret)
646                 return ret;
647
648         ret = add_uevent_var(env, "VMBUS_DEVICE_DEVICE_GUID={"
649                              "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
650                              "%02x%02x%02x%02x%02x%02x%02x%02x}",
651                              device_ctx->device_id.data[3],
652                              device_ctx->device_id.data[2],
653                              device_ctx->device_id.data[1],
654                              device_ctx->device_id.data[0],
655                              device_ctx->device_id.data[5],
656                              device_ctx->device_id.data[4],
657                              device_ctx->device_id.data[7],
658                              device_ctx->device_id.data[6],
659                              device_ctx->device_id.data[8],
660                              device_ctx->device_id.data[9],
661                              device_ctx->device_id.data[10],
662                              device_ctx->device_id.data[11],
663                              device_ctx->device_id.data[12],
664                              device_ctx->device_id.data[13],
665                              device_ctx->device_id.data[14],
666                              device_ctx->device_id.data[15]);
667         if (ret)
668                 return ret;
669
670         return 0;
671 }
672
673 /*
674  * vmbus_match - Attempt to match the specified device to the specified driver
675  */
676 static int vmbus_match(struct device *device, struct device_driver *driver)
677 {
678         int match = 0;
679         struct driver_context *driver_ctx = driver_to_driver_context(driver);
680         struct vm_device *device_ctx = device_to_vm_device(device);
681
682         /* We found our driver ? */
683         if (memcmp(&device_ctx->class_id, &driver_ctx->class_id,
684                    sizeof(struct hv_guid)) == 0) {
685                 /*
686                  * !! NOTE: The driver_ctx is not a vmbus_drv_ctx. We typecast
687                  * it here to access the struct hv_driver field
688                  */
689                 struct vmbus_driver_context *vmbus_drv_ctx =
690                         (struct vmbus_driver_context *)driver_ctx;
691
692                 device_ctx->device_obj.Driver = &vmbus_drv_ctx->drv_obj.Base;
693                 DPRINT_INFO(VMBUS_DRV,
694                             "device object (%p) set to driver object (%p)",
695                             &device_ctx->device_obj,
696                             device_ctx->device_obj.Driver);
697
698                 match = 1;
699         }
700         return match;
701 }
702
703 /*
704  * vmbus_probe_failed_cb - Callback when a driver probe failed in vmbus_probe()
705  *
706  * We need a callback because we cannot invoked device_unregister() inside
707  * vmbus_probe() since vmbus_probe() may be invoked inside device_register()
708  * i.e. we cannot call device_unregister() inside device_register()
709  */
710 static void vmbus_probe_failed_cb(struct work_struct *context)
711 {
712         struct vm_device *device_ctx = (struct vm_device *)context;
713
714         /*
715          * Kick off the process of unregistering the device.
716          * This will call vmbus_remove() and eventually vmbus_device_release()
717          */
718         device_unregister(&device_ctx->device);
719
720         /* put_device(&device_ctx->device); */
721 }
722
723 /*
724  * vmbus_probe - Add the new vmbus's child device
725  */
726 static int vmbus_probe(struct device *child_device)
727 {
728         int ret = 0;
729         struct driver_context *driver_ctx =
730                         driver_to_driver_context(child_device->driver);
731         struct vm_device *device_ctx =
732                         device_to_vm_device(child_device);
733
734         /* Let the specific open-source driver handles the probe if it can */
735         if (driver_ctx->probe) {
736                 ret = device_ctx->probe_error = driver_ctx->probe(child_device);
737                 if (ret != 0) {
738                         DPRINT_ERR(VMBUS_DRV, "probe() failed for device %s "
739                                    "(%p) on driver %s (%d)...",
740                                    dev_name(child_device), child_device,
741                                    child_device->driver->name, ret);
742
743                         INIT_WORK(&device_ctx->probe_failed_work_item,
744                                   vmbus_probe_failed_cb);
745                         schedule_work(&device_ctx->probe_failed_work_item);
746                 }
747         } else {
748                 DPRINT_ERR(VMBUS_DRV, "probe() method not set for driver - %s",
749                            child_device->driver->name);
750                 ret = -1;
751         }
752         return ret;
753 }
754
755 /*
756  * vmbus_remove - Remove a vmbus device
757  */
758 static int vmbus_remove(struct device *child_device)
759 {
760         int ret;
761         struct driver_context *driver_ctx;
762
763         /* Special case root bus device */
764         if (child_device->parent == NULL) {
765                 /*
766                  * No-op since it is statically defined and handle in
767                  * vmbus_bus_exit()
768                  */
769                 return 0;
770         }
771
772         if (child_device->driver) {
773                 driver_ctx = driver_to_driver_context(child_device->driver);
774
775                 /*
776                  * Let the specific open-source driver handles the removal if
777                  * it can
778                  */
779                 if (driver_ctx->remove) {
780                         ret = driver_ctx->remove(child_device);
781                 } else {
782                         DPRINT_ERR(VMBUS_DRV,
783                                    "remove() method not set for driver - %s",
784                                    child_device->driver->name);
785                         ret = -1;
786                 }
787         }
788
789         return 0;
790 }
791
792 /*
793  * vmbus_shutdown - Shutdown a vmbus device
794  */
795 static void vmbus_shutdown(struct device *child_device)
796 {
797         struct driver_context *driver_ctx;
798
799         /* Special case root bus device */
800         if (child_device->parent == NULL) {
801                 /*
802                  * No-op since it is statically defined and handle in
803                  * vmbus_bus_exit()
804                  */
805                 return;
806         }
807
808         /* The device may not be attached yet */
809         if (!child_device->driver)
810                 return;
811
812         driver_ctx = driver_to_driver_context(child_device->driver);
813
814         /* Let the specific open-source driver handles the removal if it can */
815         if (driver_ctx->shutdown)
816                 driver_ctx->shutdown(child_device);
817
818         return;
819 }
820
821 /*
822  * vmbus_bus_release - Final callback release of the vmbus root device
823  */
824 static void vmbus_bus_release(struct device *device)
825 {
826         /* FIXME */
827         /* Empty release functions are a bug, or a major sign
828          * of a problem design, this MUST BE FIXED! */
829         dev_err(device, "%s needs to be fixed!\n", __func__);
830         WARN_ON(1);
831 }
832
833 /*
834  * vmbus_device_release - Final callback release of the vmbus child device
835  */
836 static void vmbus_device_release(struct device *device)
837 {
838         struct vm_device *device_ctx = device_to_vm_device(device);
839
840         kfree(device_ctx);
841
842         /* !!DO NOT REFERENCE device_ctx anymore at this point!! */
843 }
844
845 /*
846  * vmbus_msg_dpc - Tasklet routine to handle hypervisor messages
847  */
848 static void vmbus_msg_dpc(unsigned long data)
849 {
850         struct vmbus_driver *vmbus_drv_obj = (struct vmbus_driver *)data;
851
852         /* Call to bus driver to handle interrupt */
853         vmbus_on_msg_dpc(&vmbus_drv_obj->Base);
854 }
855
856 /*
857  * vmbus_event_dpc - Tasklet routine to handle hypervisor events
858  */
859 static void vmbus_event_dpc(unsigned long data)
860 {
861         struct vmbus_driver *vmbus_drv_obj = (struct vmbus_driver *)data;
862
863         /* Call to bus driver to handle interrupt */
864         vmbus_on_event_dpc(&vmbus_drv_obj->Base);
865 }
866
867 static irqreturn_t vmbus_isr(int irq, void *dev_id)
868 {
869         struct vmbus_driver *vmbus_driver_obj = &g_vmbus_drv.drv_obj;
870         int ret;
871
872         /* Call to bus driver to handle interrupt */
873         ret = vmbus_on_isr(&vmbus_driver_obj->Base);
874
875         /* Schedules a dpc if necessary */
876         if (ret > 0) {
877                 if (test_bit(0, (unsigned long *)&ret))
878                         tasklet_schedule(&g_vmbus_drv.msg_dpc);
879
880                 if (test_bit(1, (unsigned long *)&ret))
881                         tasklet_schedule(&g_vmbus_drv.event_dpc);
882
883                 return IRQ_HANDLED;
884         } else {
885                 return IRQ_NONE;
886         }
887 }
888
889 static struct dmi_system_id __initdata microsoft_hv_dmi_table[] = {
890         {
891                 .ident = "Hyper-V",
892                 .matches = {
893                         DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
894                         DMI_MATCH(DMI_PRODUCT_NAME, "Virtual Machine"),
895                         DMI_MATCH(DMI_BOARD_NAME, "Virtual Machine"),
896                 },
897         },
898         { },
899 };
900 MODULE_DEVICE_TABLE(dmi, microsoft_hv_dmi_table);
901
902 static int __init vmbus_init(void)
903 {
904         DPRINT_INFO(VMBUS_DRV,
905                 "Vmbus initializing.... current log level 0x%x (%x,%x)",
906                 vmbus_loglevel, HIWORD(vmbus_loglevel), LOWORD(vmbus_loglevel));
907         /* Todo: it is used for loglevel, to be ported to new kernel. */
908
909         if (!dmi_check_system(microsoft_hv_dmi_table))
910                 return -ENODEV;
911
912         return vmbus_bus_init(VmbusInitialize);
913 }
914
915 static void __exit vmbus_exit(void)
916 {
917         vmbus_bus_exit();
918         /* Todo: it is used for loglevel, to be ported to new kernel. */
919 }
920
921 /*
922  * We use a PCI table to determine if we should autoload this driver  This is
923  * needed by distro tools to determine if the hyperv drivers should be
924  * installed and/or configured.  We don't do anything else with the table, but
925  * it needs to be present.
926  */
927 static const struct pci_device_id microsoft_hv_pci_table[] = {
928         { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
929         { 0 }
930 };
931 MODULE_DEVICE_TABLE(pci, microsoft_hv_pci_table);
932
933 MODULE_LICENSE("GPL");
934 MODULE_VERSION(HV_DRV_VERSION);
935 module_param(vmbus_irq, int, S_IRUGO);
936 module_param(vmbus_loglevel, int, S_IRUGO);
937
938 module_init(vmbus_init);
939 module_exit(vmbus_exit);