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