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