hv: delete vmbus_get_debug_info()
[cascardo/linux.git] / drivers / hv / vmbus_drv.c
1 /*
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>
20  *   K. Y. Srinivasan <kys@microsoft.com>
21  *
22  */
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/device.h>
28 #include <linux/irq.h>
29 #include <linux/interrupt.h>
30 #include <linux/sysctl.h>
31 #include <linux/slab.h>
32 #include <linux/acpi.h>
33 #include <acpi/acpi_bus.h>
34 #include <linux/completion.h>
35 #include <linux/hyperv.h>
36 #include <linux/kernel_stat.h>
37 #include <asm/hyperv.h>
38 #include <asm/hypervisor.h>
39 #include <asm/mshyperv.h>
40 #include "hyperv_vmbus.h"
41
42
43 static struct acpi_device  *hv_acpi_dev;
44
45 static struct tasklet_struct msg_dpc;
46 static struct completion probe_event;
47 static int irq;
48
49 struct hv_device_info {
50         struct hv_dev_port_info inbound;
51         struct hv_dev_port_info outbound;
52 };
53
54 static int vmbus_exists(void)
55 {
56         if (hv_acpi_dev == NULL)
57                 return -ENODEV;
58
59         return 0;
60 }
61
62
63 static void get_channel_info(struct hv_device *device,
64                              struct hv_device_info *info)
65 {
66         struct hv_ring_buffer_debug_info inbound;
67         struct hv_ring_buffer_debug_info outbound;
68
69         if (!device->channel)
70                 return;
71
72         hv_ringbuffer_get_debuginfo(&device->channel->inbound, &inbound);
73         hv_ringbuffer_get_debuginfo(&device->channel->outbound, &outbound);
74
75         info->inbound.int_mask = inbound.current_interrupt_mask;
76         info->inbound.read_idx = inbound.current_read_index;
77         info->inbound.write_idx = inbound.current_write_index;
78         info->inbound.bytes_avail_toread = inbound.bytes_avail_toread;
79         info->inbound.bytes_avail_towrite = inbound.bytes_avail_towrite;
80
81         info->outbound.int_mask = outbound.current_interrupt_mask;
82         info->outbound.read_idx = outbound.current_read_index;
83         info->outbound.write_idx = outbound.current_write_index;
84         info->outbound.bytes_avail_toread = outbound.bytes_avail_toread;
85         info->outbound.bytes_avail_towrite = outbound.bytes_avail_towrite;
86 }
87
88 #define VMBUS_ALIAS_LEN ((sizeof((struct hv_vmbus_device_id *)0)->guid) * 2)
89 static void print_alias_name(struct hv_device *hv_dev, char *alias_name)
90 {
91         int i;
92         for (i = 0; i < VMBUS_ALIAS_LEN; i += 2)
93                 sprintf(&alias_name[i], "%02x", hv_dev->dev_type.b[i/2]);
94 }
95
96 /*
97  * vmbus_show_device_attr - Show the device attribute in sysfs.
98  *
99  * This is invoked when user does a
100  * "cat /sys/bus/vmbus/devices/<busdevice>/<attr name>"
101  */
102 static ssize_t vmbus_show_device_attr(struct device *dev,
103                                       struct device_attribute *dev_attr,
104                                       char *buf)
105 {
106         struct hv_device *hv_dev = device_to_hv_device(dev);
107         struct hv_device_info *device_info;
108         int ret = 0;
109
110         device_info = kzalloc(sizeof(struct hv_device_info), GFP_KERNEL);
111         if (!device_info)
112                 return ret;
113
114         get_channel_info(hv_dev, device_info);
115
116         if (!strcmp(dev_attr->attr.name, "out_intr_mask")) {
117                 ret = sprintf(buf, "%d\n", device_info->outbound.int_mask);
118         } else if (!strcmp(dev_attr->attr.name, "out_read_index")) {
119                 ret = sprintf(buf, "%d\n", device_info->outbound.read_idx);
120         } else if (!strcmp(dev_attr->attr.name, "out_write_index")) {
121                 ret = sprintf(buf, "%d\n", device_info->outbound.write_idx);
122         } else if (!strcmp(dev_attr->attr.name, "out_read_bytes_avail")) {
123                 ret = sprintf(buf, "%d\n",
124                                device_info->outbound.bytes_avail_toread);
125         } else if (!strcmp(dev_attr->attr.name, "out_write_bytes_avail")) {
126                 ret = sprintf(buf, "%d\n",
127                                device_info->outbound.bytes_avail_towrite);
128         } else if (!strcmp(dev_attr->attr.name, "in_intr_mask")) {
129                 ret = sprintf(buf, "%d\n", device_info->inbound.int_mask);
130         } else if (!strcmp(dev_attr->attr.name, "in_read_index")) {
131                 ret = sprintf(buf, "%d\n", device_info->inbound.read_idx);
132         } else if (!strcmp(dev_attr->attr.name, "in_write_index")) {
133                 ret = sprintf(buf, "%d\n", device_info->inbound.write_idx);
134         } else if (!strcmp(dev_attr->attr.name, "in_read_bytes_avail")) {
135                 ret = sprintf(buf, "%d\n",
136                                device_info->inbound.bytes_avail_toread);
137         } else if (!strcmp(dev_attr->attr.name, "in_write_bytes_avail")) {
138                 ret = sprintf(buf, "%d\n",
139                                device_info->inbound.bytes_avail_towrite);
140         }
141
142         kfree(device_info);
143         return ret;
144 }
145
146 static u8 channel_monitor_group(struct vmbus_channel *channel)
147 {
148         return (u8)channel->offermsg.monitorid / 32;
149 }
150
151 static u8 channel_monitor_offset(struct vmbus_channel *channel)
152 {
153         return (u8)channel->offermsg.monitorid % 32;
154 }
155
156 static u32 channel_pending(struct vmbus_channel *channel,
157                            struct hv_monitor_page *monitor_page)
158 {
159         u8 monitor_group = channel_monitor_group(channel);
160         return monitor_page->trigger_group[monitor_group].pending;
161 }
162
163 static u32 channel_latency(struct vmbus_channel *channel,
164                            struct hv_monitor_page *monitor_page)
165 {
166         u8 monitor_group = channel_monitor_group(channel);
167         u8 monitor_offset = channel_monitor_offset(channel);
168         return monitor_page->latency[monitor_group][monitor_offset];
169 }
170
171 static u32 channel_conn_id(struct vmbus_channel *channel,
172                            struct hv_monitor_page *monitor_page)
173 {
174         u8 monitor_group = channel_monitor_group(channel);
175         u8 monitor_offset = channel_monitor_offset(channel);
176         return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
177 }
178
179 static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
180                        char *buf)
181 {
182         struct hv_device *hv_dev = device_to_hv_device(dev);
183
184         if (!hv_dev->channel)
185                 return -ENODEV;
186         return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
187 }
188 static DEVICE_ATTR_RO(id);
189
190 static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
191                           char *buf)
192 {
193         struct hv_device *hv_dev = device_to_hv_device(dev);
194
195         if (!hv_dev->channel)
196                 return -ENODEV;
197         return sprintf(buf, "%d\n", hv_dev->channel->state);
198 }
199 static DEVICE_ATTR_RO(state);
200
201 static ssize_t monitor_id_show(struct device *dev,
202                                struct device_attribute *dev_attr, char *buf)
203 {
204         struct hv_device *hv_dev = device_to_hv_device(dev);
205
206         if (!hv_dev->channel)
207                 return -ENODEV;
208         return sprintf(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
209 }
210 static DEVICE_ATTR_RO(monitor_id);
211
212 static ssize_t class_id_show(struct device *dev,
213                                struct device_attribute *dev_attr, char *buf)
214 {
215         struct hv_device *hv_dev = device_to_hv_device(dev);
216
217         if (!hv_dev->channel)
218                 return -ENODEV;
219         return sprintf(buf, "{%pUl}\n",
220                        hv_dev->channel->offermsg.offer.if_type.b);
221 }
222 static DEVICE_ATTR_RO(class_id);
223
224 static ssize_t device_id_show(struct device *dev,
225                               struct device_attribute *dev_attr, char *buf)
226 {
227         struct hv_device *hv_dev = device_to_hv_device(dev);
228
229         if (!hv_dev->channel)
230                 return -ENODEV;
231         return sprintf(buf, "{%pUl}\n",
232                        hv_dev->channel->offermsg.offer.if_instance.b);
233 }
234 static DEVICE_ATTR_RO(device_id);
235
236 static ssize_t modalias_show(struct device *dev,
237                              struct device_attribute *dev_attr, char *buf)
238 {
239         struct hv_device *hv_dev = device_to_hv_device(dev);
240         char alias_name[VMBUS_ALIAS_LEN + 1];
241
242         print_alias_name(hv_dev, alias_name);
243         return sprintf(buf, "vmbus:%s\n", alias_name);
244 }
245 static DEVICE_ATTR_RO(modalias);
246
247 static ssize_t server_monitor_pending_show(struct device *dev,
248                                            struct device_attribute *dev_attr,
249                                            char *buf)
250 {
251         struct hv_device *hv_dev = device_to_hv_device(dev);
252
253         if (!hv_dev->channel)
254                 return -ENODEV;
255         return sprintf(buf, "%d\n",
256                        channel_pending(hv_dev->channel,
257                                        vmbus_connection.monitor_pages[1]));
258 }
259 static DEVICE_ATTR_RO(server_monitor_pending);
260
261 static ssize_t client_monitor_pending_show(struct device *dev,
262                                            struct device_attribute *dev_attr,
263                                            char *buf)
264 {
265         struct hv_device *hv_dev = device_to_hv_device(dev);
266
267         if (!hv_dev->channel)
268                 return -ENODEV;
269         return sprintf(buf, "%d\n",
270                        channel_pending(hv_dev->channel,
271                                        vmbus_connection.monitor_pages[1]));
272 }
273 static DEVICE_ATTR_RO(client_monitor_pending);
274
275 static ssize_t server_monitor_latency_show(struct device *dev,
276                                            struct device_attribute *dev_attr,
277                                            char *buf)
278 {
279         struct hv_device *hv_dev = device_to_hv_device(dev);
280
281         if (!hv_dev->channel)
282                 return -ENODEV;
283         return sprintf(buf, "%d\n",
284                        channel_latency(hv_dev->channel,
285                                        vmbus_connection.monitor_pages[0]));
286 }
287 static DEVICE_ATTR_RO(server_monitor_latency);
288
289 static ssize_t client_monitor_latency_show(struct device *dev,
290                                            struct device_attribute *dev_attr,
291                                            char *buf)
292 {
293         struct hv_device *hv_dev = device_to_hv_device(dev);
294
295         if (!hv_dev->channel)
296                 return -ENODEV;
297         return sprintf(buf, "%d\n",
298                        channel_latency(hv_dev->channel,
299                                        vmbus_connection.monitor_pages[1]));
300 }
301 static DEVICE_ATTR_RO(client_monitor_latency);
302
303 static ssize_t server_monitor_conn_id_show(struct device *dev,
304                                            struct device_attribute *dev_attr,
305                                            char *buf)
306 {
307         struct hv_device *hv_dev = device_to_hv_device(dev);
308
309         if (!hv_dev->channel)
310                 return -ENODEV;
311         return sprintf(buf, "%d\n",
312                        channel_conn_id(hv_dev->channel,
313                                        vmbus_connection.monitor_pages[0]));
314 }
315 static DEVICE_ATTR_RO(server_monitor_conn_id);
316
317 static ssize_t client_monitor_conn_id_show(struct device *dev,
318                                            struct device_attribute *dev_attr,
319                                            char *buf)
320 {
321         struct hv_device *hv_dev = device_to_hv_device(dev);
322
323         if (!hv_dev->channel)
324                 return -ENODEV;
325         return sprintf(buf, "%d\n",
326                        channel_conn_id(hv_dev->channel,
327                                        vmbus_connection.monitor_pages[1]));
328 }
329 static DEVICE_ATTR_RO(client_monitor_conn_id);
330
331 static struct attribute *vmbus_attrs[] = {
332         &dev_attr_id.attr,
333         &dev_attr_state.attr,
334         &dev_attr_monitor_id.attr,
335         &dev_attr_class_id.attr,
336         &dev_attr_device_id.attr,
337         &dev_attr_modalias.attr,
338         &dev_attr_server_monitor_pending.attr,
339         &dev_attr_client_monitor_pending.attr,
340         &dev_attr_server_monitor_latency.attr,
341         &dev_attr_client_monitor_latency.attr,
342         &dev_attr_server_monitor_conn_id.attr,
343         &dev_attr_client_monitor_conn_id.attr,
344         NULL,
345 };
346 ATTRIBUTE_GROUPS(vmbus);
347
348 /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
349 static struct device_attribute vmbus_device_attrs[] = {
350         __ATTR(out_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL),
351         __ATTR(out_read_index, S_IRUGO, vmbus_show_device_attr, NULL),
352         __ATTR(out_write_index, S_IRUGO, vmbus_show_device_attr, NULL),
353         __ATTR(out_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
354         __ATTR(out_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
355
356         __ATTR(in_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL),
357         __ATTR(in_read_index, S_IRUGO, vmbus_show_device_attr, NULL),
358         __ATTR(in_write_index, S_IRUGO, vmbus_show_device_attr, NULL),
359         __ATTR(in_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
360         __ATTR(in_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
361         __ATTR_NULL
362 };
363
364
365 /*
366  * vmbus_uevent - add uevent for our device
367  *
368  * This routine is invoked when a device is added or removed on the vmbus to
369  * generate a uevent to udev in the userspace. The udev will then look at its
370  * rule and the uevent generated here to load the appropriate driver
371  *
372  * The alias string will be of the form vmbus:guid where guid is the string
373  * representation of the device guid (each byte of the guid will be
374  * represented with two hex characters.
375  */
376 static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
377 {
378         struct hv_device *dev = device_to_hv_device(device);
379         int ret;
380         char alias_name[VMBUS_ALIAS_LEN + 1];
381
382         print_alias_name(dev, alias_name);
383         ret = add_uevent_var(env, "MODALIAS=vmbus:%s", alias_name);
384         return ret;
385 }
386
387 static uuid_le null_guid;
388
389 static inline bool is_null_guid(const __u8 *guid)
390 {
391         if (memcmp(guid, &null_guid, sizeof(uuid_le)))
392                 return false;
393         return true;
394 }
395
396 /*
397  * Return a matching hv_vmbus_device_id pointer.
398  * If there is no match, return NULL.
399  */
400 static const struct hv_vmbus_device_id *hv_vmbus_get_id(
401                                         const struct hv_vmbus_device_id *id,
402                                         __u8 *guid)
403 {
404         for (; !is_null_guid(id->guid); id++)
405                 if (!memcmp(&id->guid, guid, sizeof(uuid_le)))
406                         return id;
407
408         return NULL;
409 }
410
411
412
413 /*
414  * vmbus_match - Attempt to match the specified device to the specified driver
415  */
416 static int vmbus_match(struct device *device, struct device_driver *driver)
417 {
418         struct hv_driver *drv = drv_to_hv_drv(driver);
419         struct hv_device *hv_dev = device_to_hv_device(device);
420
421         if (hv_vmbus_get_id(drv->id_table, hv_dev->dev_type.b))
422                 return 1;
423
424         return 0;
425 }
426
427 /*
428  * vmbus_probe - Add the new vmbus's child device
429  */
430 static int vmbus_probe(struct device *child_device)
431 {
432         int ret = 0;
433         struct hv_driver *drv =
434                         drv_to_hv_drv(child_device->driver);
435         struct hv_device *dev = device_to_hv_device(child_device);
436         const struct hv_vmbus_device_id *dev_id;
437
438         dev_id = hv_vmbus_get_id(drv->id_table, dev->dev_type.b);
439         if (drv->probe) {
440                 ret = drv->probe(dev, dev_id);
441                 if (ret != 0)
442                         pr_err("probe failed for device %s (%d)\n",
443                                dev_name(child_device), ret);
444
445         } else {
446                 pr_err("probe not set for driver %s\n",
447                        dev_name(child_device));
448                 ret = -ENODEV;
449         }
450         return ret;
451 }
452
453 /*
454  * vmbus_remove - Remove a vmbus device
455  */
456 static int vmbus_remove(struct device *child_device)
457 {
458         struct hv_driver *drv = drv_to_hv_drv(child_device->driver);
459         struct hv_device *dev = device_to_hv_device(child_device);
460
461         if (drv->remove)
462                 drv->remove(dev);
463         else
464                 pr_err("remove not set for driver %s\n",
465                         dev_name(child_device));
466
467         return 0;
468 }
469
470
471 /*
472  * vmbus_shutdown - Shutdown a vmbus device
473  */
474 static void vmbus_shutdown(struct device *child_device)
475 {
476         struct hv_driver *drv;
477         struct hv_device *dev = device_to_hv_device(child_device);
478
479
480         /* The device may not be attached yet */
481         if (!child_device->driver)
482                 return;
483
484         drv = drv_to_hv_drv(child_device->driver);
485
486         if (drv->shutdown)
487                 drv->shutdown(dev);
488
489         return;
490 }
491
492
493 /*
494  * vmbus_device_release - Final callback release of the vmbus child device
495  */
496 static void vmbus_device_release(struct device *device)
497 {
498         struct hv_device *hv_dev = device_to_hv_device(device);
499
500         kfree(hv_dev);
501
502 }
503
504 /* The one and only one */
505 static struct bus_type  hv_bus = {
506         .name =         "vmbus",
507         .match =                vmbus_match,
508         .shutdown =             vmbus_shutdown,
509         .remove =               vmbus_remove,
510         .probe =                vmbus_probe,
511         .uevent =               vmbus_uevent,
512         .dev_attrs =    vmbus_device_attrs,
513         .dev_groups =           vmbus_groups,
514 };
515
516 static const char *driver_name = "hyperv";
517
518
519 struct onmessage_work_context {
520         struct work_struct work;
521         struct hv_message msg;
522 };
523
524 static void vmbus_onmessage_work(struct work_struct *work)
525 {
526         struct onmessage_work_context *ctx;
527
528         ctx = container_of(work, struct onmessage_work_context,
529                            work);
530         vmbus_onmessage(&ctx->msg);
531         kfree(ctx);
532 }
533
534 static void vmbus_on_msg_dpc(unsigned long data)
535 {
536         int cpu = smp_processor_id();
537         void *page_addr = hv_context.synic_message_page[cpu];
538         struct hv_message *msg = (struct hv_message *)page_addr +
539                                   VMBUS_MESSAGE_SINT;
540         struct onmessage_work_context *ctx;
541
542         while (1) {
543                 if (msg->header.message_type == HVMSG_NONE) {
544                         /* no msg */
545                         break;
546                 } else {
547                         ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
548                         if (ctx == NULL)
549                                 continue;
550                         INIT_WORK(&ctx->work, vmbus_onmessage_work);
551                         memcpy(&ctx->msg, msg, sizeof(*msg));
552                         queue_work(vmbus_connection.work_queue, &ctx->work);
553                 }
554
555                 msg->header.message_type = HVMSG_NONE;
556
557                 /*
558                  * Make sure the write to MessageType (ie set to
559                  * HVMSG_NONE) happens before we read the
560                  * MessagePending and EOMing. Otherwise, the EOMing
561                  * will not deliver any more messages since there is
562                  * no empty slot
563                  */
564                 mb();
565
566                 if (msg->header.message_flags.msg_pending) {
567                         /*
568                          * This will cause message queue rescan to
569                          * possibly deliver another msg from the
570                          * hypervisor
571                          */
572                         wrmsrl(HV_X64_MSR_EOM, 0);
573                 }
574         }
575 }
576
577 static irqreturn_t vmbus_isr(int irq, void *dev_id)
578 {
579         int cpu = smp_processor_id();
580         void *page_addr;
581         struct hv_message *msg;
582         union hv_synic_event_flags *event;
583         bool handled = false;
584
585         page_addr = hv_context.synic_event_page[cpu];
586         if (page_addr == NULL)
587                 return IRQ_NONE;
588
589         event = (union hv_synic_event_flags *)page_addr +
590                                          VMBUS_MESSAGE_SINT;
591         /*
592          * Check for events before checking for messages. This is the order
593          * in which events and messages are checked in Windows guests on
594          * Hyper-V, and the Windows team suggested we do the same.
595          */
596
597         if ((vmbus_proto_version == VERSION_WS2008) ||
598                 (vmbus_proto_version == VERSION_WIN7)) {
599
600                 /* Since we are a child, we only need to check bit 0 */
601                 if (sync_test_and_clear_bit(0,
602                         (unsigned long *) &event->flags32[0])) {
603                         handled = true;
604                 }
605         } else {
606                 /*
607                  * Our host is win8 or above. The signaling mechanism
608                  * has changed and we can directly look at the event page.
609                  * If bit n is set then we have an interrup on the channel
610                  * whose id is n.
611                  */
612                 handled = true;
613         }
614
615         if (handled)
616                 tasklet_schedule(hv_context.event_dpc[cpu]);
617
618
619         page_addr = hv_context.synic_message_page[cpu];
620         msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
621
622         /* Check if there are actual msgs to be processed */
623         if (msg->header.message_type != HVMSG_NONE) {
624                 handled = true;
625                 tasklet_schedule(&msg_dpc);
626         }
627
628         if (handled)
629                 return IRQ_HANDLED;
630         else
631                 return IRQ_NONE;
632 }
633
634 /*
635  * vmbus interrupt flow handler:
636  * vmbus interrupts can concurrently occur on multiple CPUs and
637  * can be handled concurrently.
638  */
639
640 static void vmbus_flow_handler(unsigned int irq, struct irq_desc *desc)
641 {
642         kstat_incr_irqs_this_cpu(irq, desc);
643
644         desc->action->handler(irq, desc->action->dev_id);
645 }
646
647 /*
648  * vmbus_bus_init -Main vmbus driver initialization routine.
649  *
650  * Here, we
651  *      - initialize the vmbus driver context
652  *      - invoke the vmbus hv main init routine
653  *      - get the irq resource
654  *      - retrieve the channel offers
655  */
656 static int vmbus_bus_init(int irq)
657 {
658         int ret;
659
660         /* Hypervisor initialization...setup hypercall page..etc */
661         ret = hv_init();
662         if (ret != 0) {
663                 pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
664                 return ret;
665         }
666
667         tasklet_init(&msg_dpc, vmbus_on_msg_dpc, 0);
668
669         ret = bus_register(&hv_bus);
670         if (ret)
671                 goto err_cleanup;
672
673         ret = request_irq(irq, vmbus_isr, 0, driver_name, hv_acpi_dev);
674
675         if (ret != 0) {
676                 pr_err("Unable to request IRQ %d\n",
677                            irq);
678                 goto err_unregister;
679         }
680
681         /*
682          * Vmbus interrupts can be handled concurrently on
683          * different CPUs. Establish an appropriate interrupt flow
684          * handler that can support this model.
685          */
686         irq_set_handler(irq, vmbus_flow_handler);
687
688         /*
689          * Register our interrupt handler.
690          */
691         hv_register_vmbus_handler(irq, vmbus_isr);
692
693         ret = hv_synic_alloc();
694         if (ret)
695                 goto err_alloc;
696         /*
697          * Initialize the per-cpu interrupt state and
698          * connect to the host.
699          */
700         on_each_cpu(hv_synic_init, NULL, 1);
701         ret = vmbus_connect();
702         if (ret)
703                 goto err_alloc;
704
705         vmbus_request_offers();
706
707         return 0;
708
709 err_alloc:
710         hv_synic_free();
711         free_irq(irq, hv_acpi_dev);
712
713 err_unregister:
714         bus_unregister(&hv_bus);
715
716 err_cleanup:
717         hv_cleanup();
718
719         return ret;
720 }
721
722 /**
723  * __vmbus_child_driver_register - Register a vmbus's driver
724  * @drv: Pointer to driver structure you want to register
725  * @owner: owner module of the drv
726  * @mod_name: module name string
727  *
728  * Registers the given driver with Linux through the 'driver_register()' call
729  * and sets up the hyper-v vmbus handling for this driver.
730  * It will return the state of the 'driver_register()' call.
731  *
732  */
733 int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
734 {
735         int ret;
736
737         pr_info("registering driver %s\n", hv_driver->name);
738
739         ret = vmbus_exists();
740         if (ret < 0)
741                 return ret;
742
743         hv_driver->driver.name = hv_driver->name;
744         hv_driver->driver.owner = owner;
745         hv_driver->driver.mod_name = mod_name;
746         hv_driver->driver.bus = &hv_bus;
747
748         ret = driver_register(&hv_driver->driver);
749
750         return ret;
751 }
752 EXPORT_SYMBOL_GPL(__vmbus_driver_register);
753
754 /**
755  * vmbus_driver_unregister() - Unregister a vmbus's driver
756  * @drv: Pointer to driver structure you want to un-register
757  *
758  * Un-register the given driver that was previous registered with a call to
759  * vmbus_driver_register()
760  */
761 void vmbus_driver_unregister(struct hv_driver *hv_driver)
762 {
763         pr_info("unregistering driver %s\n", hv_driver->name);
764
765         if (!vmbus_exists())
766                 driver_unregister(&hv_driver->driver);
767 }
768 EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
769
770 /*
771  * vmbus_device_create - Creates and registers a new child device
772  * on the vmbus.
773  */
774 struct hv_device *vmbus_device_create(uuid_le *type,
775                                             uuid_le *instance,
776                                             struct vmbus_channel *channel)
777 {
778         struct hv_device *child_device_obj;
779
780         child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
781         if (!child_device_obj) {
782                 pr_err("Unable to allocate device object for child device\n");
783                 return NULL;
784         }
785
786         child_device_obj->channel = channel;
787         memcpy(&child_device_obj->dev_type, type, sizeof(uuid_le));
788         memcpy(&child_device_obj->dev_instance, instance,
789                sizeof(uuid_le));
790
791
792         return child_device_obj;
793 }
794
795 /*
796  * vmbus_device_register - Register the child device
797  */
798 int vmbus_device_register(struct hv_device *child_device_obj)
799 {
800         int ret = 0;
801
802         static atomic_t device_num = ATOMIC_INIT(0);
803
804         dev_set_name(&child_device_obj->device, "vmbus_0_%d",
805                      atomic_inc_return(&device_num));
806
807         child_device_obj->device.bus = &hv_bus;
808         child_device_obj->device.parent = &hv_acpi_dev->dev;
809         child_device_obj->device.release = vmbus_device_release;
810
811         /*
812          * Register with the LDM. This will kick off the driver/device
813          * binding...which will eventually call vmbus_match() and vmbus_probe()
814          */
815         ret = device_register(&child_device_obj->device);
816
817         if (ret)
818                 pr_err("Unable to register child device\n");
819         else
820                 pr_debug("child device %s registered\n",
821                         dev_name(&child_device_obj->device));
822
823         return ret;
824 }
825
826 /*
827  * vmbus_device_unregister - Remove the specified child device
828  * from the vmbus.
829  */
830 void vmbus_device_unregister(struct hv_device *device_obj)
831 {
832         pr_debug("child device %s unregistered\n",
833                 dev_name(&device_obj->device));
834
835         /*
836          * Kick off the process of unregistering the device.
837          * This will call vmbus_remove() and eventually vmbus_device_release()
838          */
839         device_unregister(&device_obj->device);
840 }
841
842
843 /*
844  * VMBUS is an acpi enumerated device. Get the the IRQ information
845  * from DSDT.
846  */
847
848 static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *irq)
849 {
850
851         if (res->type == ACPI_RESOURCE_TYPE_IRQ) {
852                 struct acpi_resource_irq *irqp;
853                 irqp = &res->data.irq;
854
855                 *((unsigned int *)irq) = irqp->interrupts[0];
856         }
857
858         return AE_OK;
859 }
860
861 static int vmbus_acpi_add(struct acpi_device *device)
862 {
863         acpi_status result;
864
865         hv_acpi_dev = device;
866
867         result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
868                                         vmbus_walk_resources, &irq);
869
870         if (ACPI_FAILURE(result)) {
871                 complete(&probe_event);
872                 return -ENODEV;
873         }
874         complete(&probe_event);
875         return 0;
876 }
877
878 static const struct acpi_device_id vmbus_acpi_device_ids[] = {
879         {"VMBUS", 0},
880         {"VMBus", 0},
881         {"", 0},
882 };
883 MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
884
885 static struct acpi_driver vmbus_acpi_driver = {
886         .name = "vmbus",
887         .ids = vmbus_acpi_device_ids,
888         .ops = {
889                 .add = vmbus_acpi_add,
890         },
891 };
892
893 static int __init hv_acpi_init(void)
894 {
895         int ret, t;
896
897         if (x86_hyper != &x86_hyper_ms_hyperv)
898                 return -ENODEV;
899
900         init_completion(&probe_event);
901
902         /*
903          * Get irq resources first.
904          */
905
906         ret = acpi_bus_register_driver(&vmbus_acpi_driver);
907
908         if (ret)
909                 return ret;
910
911         t = wait_for_completion_timeout(&probe_event, 5*HZ);
912         if (t == 0) {
913                 ret = -ETIMEDOUT;
914                 goto cleanup;
915         }
916
917         if (irq <= 0) {
918                 ret = -ENODEV;
919                 goto cleanup;
920         }
921
922         ret = vmbus_bus_init(irq);
923         if (ret)
924                 goto cleanup;
925
926         return 0;
927
928 cleanup:
929         acpi_bus_unregister_driver(&vmbus_acpi_driver);
930         hv_acpi_dev = NULL;
931         return ret;
932 }
933
934 static void __exit vmbus_exit(void)
935 {
936
937         free_irq(irq, hv_acpi_dev);
938         vmbus_free_channels();
939         bus_unregister(&hv_bus);
940         hv_cleanup();
941         acpi_bus_unregister_driver(&vmbus_acpi_driver);
942 }
943
944
945 MODULE_LICENSE("GPL");
946
947 subsys_initcall(hv_acpi_init);
948 module_exit(vmbus_exit);