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