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