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