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