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