Staging: hv: Get rid of the forward declaration for vmbus_probe
[linux-2.6.git] / drivers / staging / hv / vmbus_drv.c
blob8153a4d493d5e40044fd2a624d8d6fe1f1d66216
1 /*
2 * Copyright (c) 2009, Microsoft Corporation.
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.
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.
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.
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
21 * 3/9/2011: K. Y. Srinivasan - Significant restructuring and cleanup
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/device.h>
26 #include <linux/irq.h>
27 #include <linux/interrupt.h>
28 #include <linux/sysctl.h>
29 #include <linux/pci.h>
30 #include <linux/dmi.h>
31 #include <linux/slab.h>
32 #include <linux/completion.h>
33 #include "version_info.h"
34 #include "hv_api.h"
35 #include "logging.h"
36 #include "vmbus.h"
37 #include "channel.h"
38 #include "vmbus_private.h"
41 struct pci_dev *hv_pci_dev;
43 /* Main vmbus driver data structure */
44 struct hv_bus {
45 struct bus_type bus;
46 struct tasklet_struct msg_dpc;
47 struct tasklet_struct event_dpc;
50 static int vmbus_remove(struct device *device);
51 static void vmbus_shutdown(struct device *device);
53 static irqreturn_t vmbus_isr(int irq, void *dev_id);
55 static void vmbus_device_release(struct device *device);
57 static ssize_t vmbus_show_device_attr(struct device *dev,
58 struct device_attribute *dev_attr,
59 char *buf);
62 unsigned int vmbus_loglevel = (ALL_MODULES << 16 | INFO_LVL);
63 EXPORT_SYMBOL(vmbus_loglevel);
64 /* (ALL_MODULES << 16 | DEBUG_LVL_ENTEREXIT); */
65 /* (((VMBUS | VMBUS_DRV)<<16) | DEBUG_LVL_ENTEREXIT); */
68 /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
69 static struct device_attribute vmbus_device_attrs[] = {
70 __ATTR(id, S_IRUGO, vmbus_show_device_attr, NULL),
71 __ATTR(state, S_IRUGO, vmbus_show_device_attr, NULL),
72 __ATTR(class_id, S_IRUGO, vmbus_show_device_attr, NULL),
73 __ATTR(device_id, S_IRUGO, vmbus_show_device_attr, NULL),
74 __ATTR(monitor_id, S_IRUGO, vmbus_show_device_attr, NULL),
76 __ATTR(server_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL),
77 __ATTR(server_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL),
78 __ATTR(server_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL),
80 __ATTR(client_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL),
81 __ATTR(client_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL),
82 __ATTR(client_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL),
84 __ATTR(out_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL),
85 __ATTR(out_read_index, S_IRUGO, vmbus_show_device_attr, NULL),
86 __ATTR(out_write_index, S_IRUGO, vmbus_show_device_attr, NULL),
87 __ATTR(out_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
88 __ATTR(out_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
90 __ATTR(in_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL),
91 __ATTR(in_read_index, S_IRUGO, vmbus_show_device_attr, NULL),
92 __ATTR(in_write_index, S_IRUGO, vmbus_show_device_attr, NULL),
93 __ATTR(in_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
94 __ATTR(in_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
95 __ATTR_NULL
99 * vmbus_uevent - add uevent for our device
101 * This routine is invoked when a device is added or removed on the vmbus to
102 * generate a uevent to udev in the userspace. The udev will then look at its
103 * rule and the uevent generated here to load the appropriate driver
105 static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
107 struct hv_device *dev = device_to_hv_device(device);
108 int ret;
110 DPRINT_INFO(VMBUS_DRV, "generating uevent - VMBUS_DEVICE_CLASS_GUID={"
111 "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
112 "%02x%02x%02x%02x%02x%02x%02x%02x}",
113 dev->dev_type.data[3], dev->dev_type.data[2],
114 dev->dev_type.data[1], dev->dev_type.data[0],
115 dev->dev_type.data[5], dev->dev_type.data[4],
116 dev->dev_type.data[7], dev->dev_type.data[6],
117 dev->dev_type.data[8], dev->dev_type.data[9],
118 dev->dev_type.data[10],
119 dev->dev_type.data[11],
120 dev->dev_type.data[12],
121 dev->dev_type.data[13],
122 dev->dev_type.data[14],
123 dev->dev_type.data[15]);
125 ret = add_uevent_var(env, "VMBUS_DEVICE_CLASS_GUID={"
126 "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
127 "%02x%02x%02x%02x%02x%02x%02x%02x}",
128 dev->dev_type.data[3],
129 dev->dev_type.data[2],
130 dev->dev_type.data[1],
131 dev->dev_type.data[0],
132 dev->dev_type.data[5],
133 dev->dev_type.data[4],
134 dev->dev_type.data[7],
135 dev->dev_type.data[6],
136 dev->dev_type.data[8],
137 dev->dev_type.data[9],
138 dev->dev_type.data[10],
139 dev->dev_type.data[11],
140 dev->dev_type.data[12],
141 dev->dev_type.data[13],
142 dev->dev_type.data[14],
143 dev->dev_type.data[15]);
145 if (ret)
146 return ret;
148 ret = add_uevent_var(env, "VMBUS_DEVICE_DEVICE_GUID={"
149 "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
150 "%02x%02x%02x%02x%02x%02x%02x%02x}",
151 dev->dev_instance.data[3],
152 dev->dev_instance.data[2],
153 dev->dev_instance.data[1],
154 dev->dev_instance.data[0],
155 dev->dev_instance.data[5],
156 dev->dev_instance.data[4],
157 dev->dev_instance.data[7],
158 dev->dev_instance.data[6],
159 dev->dev_instance.data[8],
160 dev->dev_instance.data[9],
161 dev->dev_instance.data[10],
162 dev->dev_instance.data[11],
163 dev->dev_instance.data[12],
164 dev->dev_instance.data[13],
165 dev->dev_instance.data[14],
166 dev->dev_instance.data[15]);
167 if (ret)
168 return ret;
170 return 0;
175 * vmbus_match - Attempt to match the specified device to the specified driver
177 static int vmbus_match(struct device *device, struct device_driver *driver)
179 int match = 0;
180 struct hv_driver *drv = drv_to_hv_drv(driver);
181 struct hv_device *device_ctx = device_to_hv_device(device);
183 /* We found our driver ? */
184 if (memcmp(&device_ctx->dev_type, &drv->dev_type,
185 sizeof(struct hv_guid)) == 0) {
187 device_ctx->drv = drv->priv;
188 DPRINT_INFO(VMBUS_DRV,
189 "device object (%p) set to driver object (%p)",
190 &device_ctx,
191 device_ctx->drv);
193 match = 1;
195 return match;
200 * vmbus_probe_failed_cb - Callback when a driver probe failed in vmbus_probe()
202 * We need a callback because we cannot invoked device_unregister() inside
203 * vmbus_probe() since vmbus_probe() may be invoked inside device_register()
204 * i.e. we cannot call device_unregister() inside device_register()
206 static void vmbus_probe_failed_cb(struct work_struct *context)
208 struct hv_device *device_ctx = (struct hv_device *)context;
211 * Kick off the process of unregistering the device.
212 * This will call vmbus_remove() and eventually vmbus_device_release()
214 device_unregister(&device_ctx->device);
216 /* put_device(&device_ctx->device); */
220 * vmbus_probe - Add the new vmbus's child device
222 static int vmbus_probe(struct device *child_device)
224 int ret = 0;
225 struct hv_driver *drv =
226 drv_to_hv_drv(child_device->driver);
227 struct hv_device *dev = device_to_hv_device(child_device);
229 /* Let the specific open-source driver handles the probe if it can */
230 if (drv->driver.probe) {
231 ret = dev->probe_error =
232 drv->driver.probe(child_device);
233 if (ret != 0) {
234 DPRINT_ERR(VMBUS_DRV, "probe() failed for device %s "
235 "(%p) on driver %s (%d)...",
236 dev_name(child_device), child_device,
237 child_device->driver->name, ret);
239 INIT_WORK(&dev->probe_failed_work_item,
240 vmbus_probe_failed_cb);
241 schedule_work(&dev->probe_failed_work_item);
243 } else {
244 DPRINT_ERR(VMBUS_DRV, "probe() method not set for driver - %s",
245 child_device->driver->name);
246 ret = -1;
248 return ret;
251 /* The one and only one */
252 static struct hv_bus hv_bus = {
253 .bus.name = "vmbus",
254 .bus.match = vmbus_match,
255 .bus.shutdown = vmbus_shutdown,
256 .bus.remove = vmbus_remove,
257 .bus.probe = vmbus_probe,
258 .bus.uevent = vmbus_uevent,
259 .bus.dev_attrs = vmbus_device_attrs,
262 static const char *driver_name = "hyperv";
265 struct onmessage_work_context {
266 struct work_struct work;
267 struct hv_message msg;
270 static void vmbus_onmessage_work(struct work_struct *work)
272 struct onmessage_work_context *ctx;
274 ctx = container_of(work, struct onmessage_work_context,
275 work);
276 vmbus_onmessage(&ctx->msg);
277 kfree(ctx);
281 * vmbus_on_msg_dpc - DPC routine to handle messages from the hypervisior
283 static void vmbus_on_msg_dpc(unsigned long data)
285 int cpu = smp_processor_id();
286 void *page_addr = hv_context.synic_message_page[cpu];
287 struct hv_message *msg = (struct hv_message *)page_addr +
288 VMBUS_MESSAGE_SINT;
289 struct onmessage_work_context *ctx;
291 while (1) {
292 if (msg->header.message_type == HVMSG_NONE) {
293 /* no msg */
294 break;
295 } else {
296 ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
297 if (ctx == NULL)
298 continue;
299 INIT_WORK(&ctx->work, vmbus_onmessage_work);
300 memcpy(&ctx->msg, msg, sizeof(*msg));
301 queue_work(vmbus_connection.work_queue, &ctx->work);
304 msg->header.message_type = HVMSG_NONE;
307 * Make sure the write to MessageType (ie set to
308 * HVMSG_NONE) happens before we read the
309 * MessagePending and EOMing. Otherwise, the EOMing
310 * will not deliver any more messages since there is
311 * no empty slot
313 mb();
315 if (msg->header.message_flags.msg_pending) {
317 * This will cause message queue rescan to
318 * possibly deliver another msg from the
319 * hypervisor
321 wrmsrl(HV_X64_MSR_EOM, 0);
327 * vmbus_on_isr - ISR routine
329 static int vmbus_on_isr(void)
331 int ret = 0;
332 int cpu = smp_processor_id();
333 void *page_addr;
334 struct hv_message *msg;
335 union hv_synic_event_flags *event;
337 page_addr = hv_context.synic_message_page[cpu];
338 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
340 /* Check if there are actual msgs to be process */
341 if (msg->header.message_type != HVMSG_NONE) {
342 DPRINT_DBG(VMBUS, "received msg type %d size %d",
343 msg->header.message_type,
344 msg->header.payload_size);
345 ret |= 0x1;
348 /* TODO: Check if there are events to be process */
349 page_addr = hv_context.synic_event_page[cpu];
350 event = (union hv_synic_event_flags *)page_addr + VMBUS_MESSAGE_SINT;
352 /* Since we are a child, we only need to check bit 0 */
353 if (test_and_clear_bit(0, (unsigned long *) &event->flags32[0])) {
354 DPRINT_DBG(VMBUS, "received event %d", event->flags32[0]);
355 ret |= 0x2;
358 return ret;
361 static void get_channel_info(struct hv_device *device,
362 struct hv_device_info *info)
364 struct vmbus_channel_debug_info debug_info;
366 if (!device->channel)
367 return;
369 vmbus_get_debug_info(device->channel, &debug_info);
371 info->chn_id = debug_info.relid;
372 info->chn_state = debug_info.state;
373 memcpy(&info->chn_type, &debug_info.interfacetype,
374 sizeof(struct hv_guid));
375 memcpy(&info->chn_instance, &debug_info.interface_instance,
376 sizeof(struct hv_guid));
378 info->monitor_id = debug_info.monitorid;
380 info->server_monitor_pending = debug_info.servermonitor_pending;
381 info->server_monitor_latency = debug_info.servermonitor_latency;
382 info->server_monitor_conn_id = debug_info.servermonitor_connectionid;
384 info->client_monitor_pending = debug_info.clientmonitor_pending;
385 info->client_monitor_latency = debug_info.clientmonitor_latency;
386 info->client_monitor_conn_id = debug_info.clientmonitor_connectionid;
388 info->inbound.int_mask = debug_info.inbound.current_interrupt_mask;
389 info->inbound.read_idx = debug_info.inbound.current_read_index;
390 info->inbound.write_idx = debug_info.inbound.current_write_index;
391 info->inbound.bytes_avail_toread =
392 debug_info.inbound.bytes_avail_toread;
393 info->inbound.bytes_avail_towrite =
394 debug_info.inbound.bytes_avail_towrite;
396 info->outbound.int_mask =
397 debug_info.outbound.current_interrupt_mask;
398 info->outbound.read_idx = debug_info.outbound.current_read_index;
399 info->outbound.write_idx = debug_info.outbound.current_write_index;
400 info->outbound.bytes_avail_toread =
401 debug_info.outbound.bytes_avail_toread;
402 info->outbound.bytes_avail_towrite =
403 debug_info.outbound.bytes_avail_towrite;
407 * vmbus_show_device_attr - Show the device attribute in sysfs.
409 * This is invoked when user does a
410 * "cat /sys/bus/vmbus/devices/<busdevice>/<attr name>"
412 static ssize_t vmbus_show_device_attr(struct device *dev,
413 struct device_attribute *dev_attr,
414 char *buf)
416 struct hv_device *device_ctx = device_to_hv_device(dev);
417 struct hv_device_info device_info;
419 memset(&device_info, 0, sizeof(struct hv_device_info));
421 get_channel_info(device_ctx, &device_info);
423 if (!strcmp(dev_attr->attr.name, "class_id")) {
424 return sprintf(buf, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-"
425 "%02x%02x%02x%02x%02x%02x%02x%02x}\n",
426 device_info.chn_type.data[3],
427 device_info.chn_type.data[2],
428 device_info.chn_type.data[1],
429 device_info.chn_type.data[0],
430 device_info.chn_type.data[5],
431 device_info.chn_type.data[4],
432 device_info.chn_type.data[7],
433 device_info.chn_type.data[6],
434 device_info.chn_type.data[8],
435 device_info.chn_type.data[9],
436 device_info.chn_type.data[10],
437 device_info.chn_type.data[11],
438 device_info.chn_type.data[12],
439 device_info.chn_type.data[13],
440 device_info.chn_type.data[14],
441 device_info.chn_type.data[15]);
442 } else if (!strcmp(dev_attr->attr.name, "device_id")) {
443 return sprintf(buf, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-"
444 "%02x%02x%02x%02x%02x%02x%02x%02x}\n",
445 device_info.chn_instance.data[3],
446 device_info.chn_instance.data[2],
447 device_info.chn_instance.data[1],
448 device_info.chn_instance.data[0],
449 device_info.chn_instance.data[5],
450 device_info.chn_instance.data[4],
451 device_info.chn_instance.data[7],
452 device_info.chn_instance.data[6],
453 device_info.chn_instance.data[8],
454 device_info.chn_instance.data[9],
455 device_info.chn_instance.data[10],
456 device_info.chn_instance.data[11],
457 device_info.chn_instance.data[12],
458 device_info.chn_instance.data[13],
459 device_info.chn_instance.data[14],
460 device_info.chn_instance.data[15]);
461 } else if (!strcmp(dev_attr->attr.name, "state")) {
462 return sprintf(buf, "%d\n", device_info.chn_state);
463 } else if (!strcmp(dev_attr->attr.name, "id")) {
464 return sprintf(buf, "%d\n", device_info.chn_id);
465 } else if (!strcmp(dev_attr->attr.name, "out_intr_mask")) {
466 return sprintf(buf, "%d\n", device_info.outbound.int_mask);
467 } else if (!strcmp(dev_attr->attr.name, "out_read_index")) {
468 return sprintf(buf, "%d\n", device_info.outbound.read_idx);
469 } else if (!strcmp(dev_attr->attr.name, "out_write_index")) {
470 return sprintf(buf, "%d\n", device_info.outbound.write_idx);
471 } else if (!strcmp(dev_attr->attr.name, "out_read_bytes_avail")) {
472 return sprintf(buf, "%d\n",
473 device_info.outbound.bytes_avail_toread);
474 } else if (!strcmp(dev_attr->attr.name, "out_write_bytes_avail")) {
475 return sprintf(buf, "%d\n",
476 device_info.outbound.bytes_avail_towrite);
477 } else if (!strcmp(dev_attr->attr.name, "in_intr_mask")) {
478 return sprintf(buf, "%d\n", device_info.inbound.int_mask);
479 } else if (!strcmp(dev_attr->attr.name, "in_read_index")) {
480 return sprintf(buf, "%d\n", device_info.inbound.read_idx);
481 } else if (!strcmp(dev_attr->attr.name, "in_write_index")) {
482 return sprintf(buf, "%d\n", device_info.inbound.write_idx);
483 } else if (!strcmp(dev_attr->attr.name, "in_read_bytes_avail")) {
484 return sprintf(buf, "%d\n",
485 device_info.inbound.bytes_avail_toread);
486 } else if (!strcmp(dev_attr->attr.name, "in_write_bytes_avail")) {
487 return sprintf(buf, "%d\n",
488 device_info.inbound.bytes_avail_towrite);
489 } else if (!strcmp(dev_attr->attr.name, "monitor_id")) {
490 return sprintf(buf, "%d\n", device_info.monitor_id);
491 } else if (!strcmp(dev_attr->attr.name, "server_monitor_pending")) {
492 return sprintf(buf, "%d\n", device_info.server_monitor_pending);
493 } else if (!strcmp(dev_attr->attr.name, "server_monitor_latency")) {
494 return sprintf(buf, "%d\n", device_info.server_monitor_latency);
495 } else if (!strcmp(dev_attr->attr.name, "server_monitor_conn_id")) {
496 return sprintf(buf, "%d\n",
497 device_info.server_monitor_conn_id);
498 } else if (!strcmp(dev_attr->attr.name, "client_monitor_pending")) {
499 return sprintf(buf, "%d\n", device_info.client_monitor_pending);
500 } else if (!strcmp(dev_attr->attr.name, "client_monitor_latency")) {
501 return sprintf(buf, "%d\n", device_info.client_monitor_latency);
502 } else if (!strcmp(dev_attr->attr.name, "client_monitor_conn_id")) {
503 return sprintf(buf, "%d\n",
504 device_info.client_monitor_conn_id);
505 } else {
506 return 0;
511 * vmbus_bus_init -Main vmbus driver initialization routine.
513 * Here, we
514 * - initialize the vmbus driver context
515 * - invoke the vmbus hv main init routine
516 * - get the irq resource
517 * - retrieve the channel offers
519 static int vmbus_bus_init(struct pci_dev *pdev)
521 int ret;
522 unsigned int vector;
524 DPRINT_INFO(VMBUS, "+++++++ HV Driver version = %s +++++++",
525 HV_DRV_VERSION);
526 DPRINT_INFO(VMBUS, "+++++++ Vmbus supported version = %d +++++++",
527 VMBUS_REVISION_NUMBER);
528 DPRINT_INFO(VMBUS, "+++++++ Vmbus using SINT %d +++++++",
529 VMBUS_MESSAGE_SINT);
530 DPRINT_DBG(VMBUS, "sizeof(vmbus_channel_packet_page_buffer)=%zd, "
531 "sizeof(VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER)=%zd",
532 sizeof(struct vmbus_channel_packet_page_buffer),
533 sizeof(struct vmbus_channel_packet_multipage_buffer));
536 /* Hypervisor initialization...setup hypercall page..etc */
537 ret = hv_init();
538 if (ret != 0) {
539 DPRINT_ERR(VMBUS, "Unable to initialize the hypervisor - 0x%x",
540 ret);
541 goto cleanup;
545 hv_bus.bus.name = driver_name;
547 /* Initialize the bus context */
548 tasklet_init(&hv_bus.msg_dpc, vmbus_on_msg_dpc,
549 (unsigned long)NULL);
550 tasklet_init(&hv_bus.event_dpc, vmbus_on_event,
551 (unsigned long)NULL);
553 /* Now, register the bus with LDM */
554 ret = bus_register(&hv_bus.bus);
555 if (ret) {
556 ret = -1;
557 goto cleanup;
560 /* Get the interrupt resource */
561 ret = request_irq(pdev->irq, vmbus_isr,
562 IRQF_SHARED | IRQF_SAMPLE_RANDOM,
563 driver_name, pdev);
565 if (ret != 0) {
566 DPRINT_ERR(VMBUS_DRV, "ERROR - Unable to request IRQ %d",
567 pdev->irq);
569 bus_unregister(&hv_bus.bus);
571 ret = -1;
572 goto cleanup;
575 vector = IRQ0_VECTOR + pdev->irq;
576 DPRINT_INFO(VMBUS_DRV, "irq 0x%x vector 0x%x", pdev->irq,
577 vector);
580 * Notify the hypervisor of our irq and
581 * connect to the host.
583 on_each_cpu(hv_synic_init, (void *)&vector, 1);
584 ret = vmbus_connect();
585 if (ret) {
586 free_irq(pdev->irq, pdev);
587 bus_unregister(&hv_bus.bus);
588 goto cleanup;
592 vmbus_request_offers();
593 wait_for_completion(&hv_channel_ready);
595 cleanup:
596 return ret;
600 * vmbus_bus_exit - Terminate the vmbus driver.
602 * This routine is opposite of vmbus_bus_init()
604 static void vmbus_bus_exit(void)
608 vmbus_release_unattached_channels();
609 vmbus_disconnect();
610 on_each_cpu(hv_synic_cleanup, NULL, 1);
612 hv_cleanup();
614 bus_unregister(&hv_bus.bus);
616 free_irq(hv_pci_dev->irq, hv_pci_dev);
618 tasklet_kill(&hv_bus.msg_dpc);
619 tasklet_kill(&hv_bus.event_dpc);
624 * vmbus_child_driver_register() - Register a vmbus's child driver
625 * @drv: Pointer to driver structure you want to register
628 * Registers the given driver with Linux through the 'driver_register()' call
629 * And sets up the hyper-v vmbus handling for this driver.
630 * It will return the state of the 'driver_register()' call.
632 * Mainly used by Hyper-V drivers.
634 int vmbus_child_driver_register(struct device_driver *drv)
636 int ret;
638 DPRINT_INFO(VMBUS_DRV, "child driver (%p) registering - name %s",
639 drv, drv->name);
641 /* The child driver on this vmbus */
642 drv->bus = &hv_bus.bus;
644 ret = driver_register(drv);
646 vmbus_request_offers();
648 return ret;
650 EXPORT_SYMBOL(vmbus_child_driver_register);
653 * vmbus_child_driver_unregister() - Unregister a vmbus's child driver
654 * @drv: Pointer to driver structure you want to un-register
657 * Un-register the given driver with Linux through the 'driver_unregister()'
658 * call. And ungegisters the driver from the Hyper-V vmbus handler.
660 * Mainly used by Hyper-V drivers.
662 void vmbus_child_driver_unregister(struct device_driver *drv)
664 DPRINT_INFO(VMBUS_DRV, "child driver (%p) unregistering - name %s",
665 drv, drv->name);
667 driver_unregister(drv);
669 drv->bus = NULL;
671 EXPORT_SYMBOL(vmbus_child_driver_unregister);
674 * vmbus_child_device_create - Creates and registers a new child device
675 * on the vmbus.
677 struct hv_device *vmbus_child_device_create(struct hv_guid *type,
678 struct hv_guid *instance,
679 struct vmbus_channel *channel)
681 struct hv_device *child_device_obj;
683 /* Allocate the new child device */
684 child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
685 if (!child_device_obj) {
686 DPRINT_ERR(VMBUS_DRV,
687 "unable to allocate device_context for child device");
688 return NULL;
691 DPRINT_DBG(VMBUS_DRV, "child device (%p) allocated - "
692 "type {%02x%02x%02x%02x-%02x%02x-%02x%02x-"
693 "%02x%02x%02x%02x%02x%02x%02x%02x},"
694 "id {%02x%02x%02x%02x-%02x%02x-%02x%02x-"
695 "%02x%02x%02x%02x%02x%02x%02x%02x}",
696 &child_device_obj->device,
697 type->data[3], type->data[2], type->data[1], type->data[0],
698 type->data[5], type->data[4], type->data[7], type->data[6],
699 type->data[8], type->data[9], type->data[10], type->data[11],
700 type->data[12], type->data[13], type->data[14], type->data[15],
701 instance->data[3], instance->data[2],
702 instance->data[1], instance->data[0],
703 instance->data[5], instance->data[4],
704 instance->data[7], instance->data[6],
705 instance->data[8], instance->data[9],
706 instance->data[10], instance->data[11],
707 instance->data[12], instance->data[13],
708 instance->data[14], instance->data[15]);
710 child_device_obj->channel = channel;
711 memcpy(&child_device_obj->dev_type, type, sizeof(struct hv_guid));
712 memcpy(&child_device_obj->dev_instance, instance,
713 sizeof(struct hv_guid));
716 return child_device_obj;
720 * vmbus_child_device_register - Register the child device
722 int vmbus_child_device_register(struct hv_device *child_device_obj)
724 int ret = 0;
726 static atomic_t device_num = ATOMIC_INIT(0);
728 DPRINT_DBG(VMBUS_DRV, "child device (%p) registering",
729 child_device_obj);
731 /* Set the device name. Otherwise, device_register() will fail. */
732 dev_set_name(&child_device_obj->device, "vmbus_0_%d",
733 atomic_inc_return(&device_num));
735 /* The new device belongs to this bus */
736 child_device_obj->device.bus = &hv_bus.bus; /* device->dev.bus; */
737 child_device_obj->device.parent = &hv_pci_dev->dev;
738 child_device_obj->device.release = vmbus_device_release;
741 * Register with the LDM. This will kick off the driver/device
742 * binding...which will eventually call vmbus_match() and vmbus_probe()
744 ret = device_register(&child_device_obj->device);
746 /* vmbus_probe() error does not get propergate to device_register(). */
747 ret = child_device_obj->probe_error;
749 if (ret)
750 DPRINT_ERR(VMBUS_DRV, "unable to register child device (%p)",
751 &child_device_obj->device);
752 else
753 DPRINT_INFO(VMBUS_DRV, "child device (%p) registered",
754 &child_device_obj->device);
756 return ret;
760 * vmbus_child_device_unregister - Remove the specified child device
761 * from the vmbus.
763 void vmbus_child_device_unregister(struct hv_device *device_obj)
766 DPRINT_INFO(VMBUS_DRV, "unregistering child device (%p)",
767 &device_obj->device);
770 * Kick off the process of unregistering the device.
771 * This will call vmbus_remove() and eventually vmbus_device_release()
773 device_unregister(&device_obj->device);
775 DPRINT_INFO(VMBUS_DRV, "child device (%p) unregistered",
776 &device_obj->device);
780 * vmbus_remove - Remove a vmbus device
782 static int vmbus_remove(struct device *child_device)
784 int ret;
785 struct hv_driver *drv;
788 if (child_device->driver) {
789 drv = drv_to_hv_drv(child_device->driver);
792 * Let the specific open-source driver handles the removal if
793 * it can
795 if (drv->driver.remove) {
796 ret = drv->driver.remove(child_device);
797 } else {
798 DPRINT_ERR(VMBUS_DRV,
799 "remove() method not set for driver - %s",
800 child_device->driver->name);
801 ret = -1;
805 return 0;
809 * vmbus_shutdown - Shutdown a vmbus device
811 static void vmbus_shutdown(struct device *child_device)
813 struct hv_driver *drv;
816 /* The device may not be attached yet */
817 if (!child_device->driver)
818 return;
820 drv = drv_to_hv_drv(child_device->driver);
822 /* Let the specific open-source driver handles the removal if it can */
823 if (drv->driver.shutdown)
824 drv->driver.shutdown(child_device);
826 return;
831 * vmbus_device_release - Final callback release of the vmbus child device
833 static void vmbus_device_release(struct device *device)
835 struct hv_device *device_ctx = device_to_hv_device(device);
837 kfree(device_ctx);
839 /* !!DO NOT REFERENCE device_ctx anymore at this point!! */
844 static irqreturn_t vmbus_isr(int irq, void *dev_id)
846 int ret;
848 ret = vmbus_on_isr();
850 /* Schedules a dpc if necessary */
851 if (ret > 0) {
852 if (test_bit(0, (unsigned long *)&ret))
853 tasklet_schedule(&hv_bus.msg_dpc);
855 if (test_bit(1, (unsigned long *)&ret))
856 tasklet_schedule(&hv_bus.event_dpc);
858 return IRQ_HANDLED;
859 } else {
860 return IRQ_NONE;
866 static int __devinit hv_pci_probe(struct pci_dev *pdev,
867 const struct pci_device_id *ent)
869 int err;
871 hv_pci_dev = pdev;
873 err = pci_enable_device(pdev);
874 if (err)
875 return err;
877 err = vmbus_bus_init(pdev);
878 if (err)
879 pci_disable_device(pdev);
881 return err;
885 * We use a PCI table to determine if we should autoload this driver This is
886 * needed by distro tools to determine if the hyperv drivers should be
887 * installed and/or configured. We don't do anything else with the table, but
888 * it needs to be present.
890 static const struct pci_device_id microsoft_hv_pci_table[] = {
891 { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
892 { 0 }
894 MODULE_DEVICE_TABLE(pci, microsoft_hv_pci_table);
896 static struct pci_driver hv_bus_driver = {
897 .name = "hv_bus",
898 .probe = hv_pci_probe,
899 .id_table = microsoft_hv_pci_table,
902 static int __init hv_pci_init(void)
904 return pci_register_driver(&hv_bus_driver);
907 static void __exit hv_pci_exit(void)
909 vmbus_bus_exit();
910 pci_unregister_driver(&hv_bus_driver);
915 MODULE_LICENSE("GPL");
916 MODULE_VERSION(HV_DRV_VERSION);
917 module_param(vmbus_loglevel, int, S_IRUGO);
919 module_init(hv_pci_init);
920 module_exit(hv_pci_exit);