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