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