]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/staging/hv/vmbus_drv.c
Staging: hv: Get rid of the forward declaration for vmbus_isr
[mirror_ubuntu-artful-kernel.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>
52e5c1ce
S
20 *
21 * 3/9/2011: K. Y. Srinivasan - Significant restructuring and cleanup
3e7ee490 22 */
3e7ee490
HJ
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>
9a775dbd 29#include <linux/pci.h>
c22090fa 30#include <linux/dmi.h>
5a0e3ad6 31#include <linux/slab.h>
8b5d6d3b 32#include <linux/completion.h>
2d82f6c7 33#include "version_info.h"
e3fe0bb6 34#include "hv_api.h"
645954c5 35#include "logging.h"
870cde80 36#include "vmbus.h"
150b19d4 37#include "channel.h"
36199a99 38#include "vmbus_private.h"
3e7ee490 39
3e7ee490 40
1168ac22
S
41struct pci_dev *hv_pci_dev;
42
454f18a9 43/* Main vmbus driver data structure */
04677c08 44struct hv_bus {
90c9960e
GKH
45 struct bus_type bus;
46 struct tasklet_struct msg_dpc;
47 struct tasklet_struct event_dpc;
3e7ee490
HJ
48};
49
3e7ee490 50
3e7ee490 51
90c9960e
GKH
52static ssize_t vmbus_show_device_attr(struct device *dev,
53 struct device_attribute *dev_attr,
54 char *buf);
3e7ee490 55
3e7ee490 56
90c9960e 57unsigned int vmbus_loglevel = (ALL_MODULES << 16 | INFO_LVL);
3e7ee490 58EXPORT_SYMBOL(vmbus_loglevel);
90c9960e
GKH
59 /* (ALL_MODULES << 16 | DEBUG_LVL_ENTEREXIT); */
60 /* (((VMBUS | VMBUS_DRV)<<16) | DEBUG_LVL_ENTEREXIT); */
3e7ee490 61
3e7ee490 62
454f18a9 63/* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
3e7ee490
HJ
64static struct device_attribute vmbus_device_attrs[] = {
65 __ATTR(id, S_IRUGO, vmbus_show_device_attr, NULL),
66 __ATTR(state, S_IRUGO, vmbus_show_device_attr, NULL),
67 __ATTR(class_id, S_IRUGO, vmbus_show_device_attr, NULL),
68 __ATTR(device_id, S_IRUGO, vmbus_show_device_attr, NULL),
69 __ATTR(monitor_id, S_IRUGO, vmbus_show_device_attr, NULL),
70
71 __ATTR(server_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL),
72 __ATTR(server_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL),
73 __ATTR(server_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL),
74
75 __ATTR(client_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL),
76 __ATTR(client_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL),
77 __ATTR(client_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL),
78
79 __ATTR(out_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL),
80 __ATTR(out_read_index, S_IRUGO, vmbus_show_device_attr, NULL),
81 __ATTR(out_write_index, S_IRUGO, vmbus_show_device_attr, NULL),
82 __ATTR(out_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
83 __ATTR(out_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
84
85 __ATTR(in_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL),
86 __ATTR(in_read_index, S_IRUGO, vmbus_show_device_attr, NULL),
87 __ATTR(in_write_index, S_IRUGO, vmbus_show_device_attr, NULL),
88 __ATTR(in_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
89 __ATTR(in_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
90 __ATTR_NULL
91};
3e7ee490 92
793be9c7 93
adde2487
S
94/*
95 * vmbus_uevent - add uevent for our device
96 *
97 * This routine is invoked when a device is added or removed on the vmbus to
98 * generate a uevent to udev in the userspace. The udev will then look at its
99 * rule and the uevent generated here to load the appropriate driver
100 */
101static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
102{
103 struct hv_device *dev = device_to_hv_device(device);
104 int ret;
105
106 DPRINT_INFO(VMBUS_DRV, "generating uevent - VMBUS_DEVICE_CLASS_GUID={"
107 "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
108 "%02x%02x%02x%02x%02x%02x%02x%02x}",
109 dev->dev_type.data[3], dev->dev_type.data[2],
110 dev->dev_type.data[1], dev->dev_type.data[0],
111 dev->dev_type.data[5], dev->dev_type.data[4],
112 dev->dev_type.data[7], dev->dev_type.data[6],
113 dev->dev_type.data[8], dev->dev_type.data[9],
114 dev->dev_type.data[10],
115 dev->dev_type.data[11],
116 dev->dev_type.data[12],
117 dev->dev_type.data[13],
118 dev->dev_type.data[14],
119 dev->dev_type.data[15]);
120
121 ret = add_uevent_var(env, "VMBUS_DEVICE_CLASS_GUID={"
122 "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
123 "%02x%02x%02x%02x%02x%02x%02x%02x}",
124 dev->dev_type.data[3],
125 dev->dev_type.data[2],
126 dev->dev_type.data[1],
127 dev->dev_type.data[0],
128 dev->dev_type.data[5],
129 dev->dev_type.data[4],
130 dev->dev_type.data[7],
131 dev->dev_type.data[6],
132 dev->dev_type.data[8],
133 dev->dev_type.data[9],
134 dev->dev_type.data[10],
135 dev->dev_type.data[11],
136 dev->dev_type.data[12],
137 dev->dev_type.data[13],
138 dev->dev_type.data[14],
139 dev->dev_type.data[15]);
140
141 if (ret)
142 return ret;
143
144 ret = add_uevent_var(env, "VMBUS_DEVICE_DEVICE_GUID={"
145 "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
146 "%02x%02x%02x%02x%02x%02x%02x%02x}",
147 dev->dev_instance.data[3],
148 dev->dev_instance.data[2],
149 dev->dev_instance.data[1],
150 dev->dev_instance.data[0],
151 dev->dev_instance.data[5],
152 dev->dev_instance.data[4],
153 dev->dev_instance.data[7],
154 dev->dev_instance.data[6],
155 dev->dev_instance.data[8],
156 dev->dev_instance.data[9],
157 dev->dev_instance.data[10],
158 dev->dev_instance.data[11],
159 dev->dev_instance.data[12],
160 dev->dev_instance.data[13],
161 dev->dev_instance.data[14],
162 dev->dev_instance.data[15]);
163 if (ret)
164 return ret;
165
166 return 0;
167}
168
b7fc147b
S
169
170/*
171 * vmbus_match - Attempt to match the specified device to the specified driver
172 */
173static int vmbus_match(struct device *device, struct device_driver *driver)
174{
175 int match = 0;
176 struct hv_driver *drv = drv_to_hv_drv(driver);
177 struct hv_device *device_ctx = device_to_hv_device(device);
178
179 /* We found our driver ? */
180 if (memcmp(&device_ctx->dev_type, &drv->dev_type,
181 sizeof(struct hv_guid)) == 0) {
182
183 device_ctx->drv = drv->priv;
184 DPRINT_INFO(VMBUS_DRV,
185 "device object (%p) set to driver object (%p)",
186 &device_ctx,
187 device_ctx->drv);
188
189 match = 1;
190 }
191 return match;
192}
193
f1f0d67b
S
194
195/*
196 * vmbus_probe_failed_cb - Callback when a driver probe failed in vmbus_probe()
197 *
198 * We need a callback because we cannot invoked device_unregister() inside
199 * vmbus_probe() since vmbus_probe() may be invoked inside device_register()
200 * i.e. we cannot call device_unregister() inside device_register()
201 */
202static void vmbus_probe_failed_cb(struct work_struct *context)
203{
204 struct hv_device *device_ctx = (struct hv_device *)context;
205
206 /*
207 * Kick off the process of unregistering the device.
208 * This will call vmbus_remove() and eventually vmbus_device_release()
209 */
210 device_unregister(&device_ctx->device);
211
212 /* put_device(&device_ctx->device); */
213}
214
215/*
216 * vmbus_probe - Add the new vmbus's child device
217 */
218static int vmbus_probe(struct device *child_device)
219{
220 int ret = 0;
221 struct hv_driver *drv =
222 drv_to_hv_drv(child_device->driver);
223 struct hv_device *dev = device_to_hv_device(child_device);
224
225 /* Let the specific open-source driver handles the probe if it can */
226 if (drv->driver.probe) {
227 ret = dev->probe_error =
228 drv->driver.probe(child_device);
229 if (ret != 0) {
230 DPRINT_ERR(VMBUS_DRV, "probe() failed for device %s "
231 "(%p) on driver %s (%d)...",
232 dev_name(child_device), child_device,
233 child_device->driver->name, ret);
234
235 INIT_WORK(&dev->probe_failed_work_item,
236 vmbus_probe_failed_cb);
237 schedule_work(&dev->probe_failed_work_item);
238 }
239 } else {
240 DPRINT_ERR(VMBUS_DRV, "probe() method not set for driver - %s",
241 child_device->driver->name);
242 ret = -1;
243 }
244 return ret;
245}
246
c5dce3db
S
247/*
248 * vmbus_remove - Remove a vmbus device
249 */
250static int vmbus_remove(struct device *child_device)
251{
252 int ret;
253 struct hv_driver *drv;
254
255
256 if (child_device->driver) {
257 drv = drv_to_hv_drv(child_device->driver);
258
259 /*
260 * Let the specific open-source driver handles the removal if
261 * it can
262 */
263 if (drv->driver.remove) {
264 ret = drv->driver.remove(child_device);
265 } else {
266 DPRINT_ERR(VMBUS_DRV,
267 "remove() method not set for driver - %s",
268 child_device->driver->name);
269 ret = -1;
270 }
271 }
272
273 return 0;
274}
275
eb1bb259
S
276
277/*
278 * vmbus_shutdown - Shutdown a vmbus device
279 */
280static void vmbus_shutdown(struct device *child_device)
281{
282 struct hv_driver *drv;
283
284
285 /* The device may not be attached yet */
286 if (!child_device->driver)
287 return;
288
289 drv = drv_to_hv_drv(child_device->driver);
290
291 /* Let the specific open-source driver handles the removal if it can */
292 if (drv->driver.shutdown)
293 drv->driver.shutdown(child_device);
294
295 return;
296}
297
086e7a56
S
298
299/*
300 * vmbus_device_release - Final callback release of the vmbus child device
301 */
302static void vmbus_device_release(struct device *device)
303{
304 struct hv_device *device_ctx = device_to_hv_device(device);
305
306 kfree(device_ctx);
307
308}
309
454f18a9 310/* The one and only one */
04677c08 311static struct hv_bus hv_bus = {
90c9960e
GKH
312 .bus.name = "vmbus",
313 .bus.match = vmbus_match,
314 .bus.shutdown = vmbus_shutdown,
315 .bus.remove = vmbus_remove,
316 .bus.probe = vmbus_probe,
317 .bus.uevent = vmbus_uevent,
318 .bus.dev_attrs = vmbus_device_attrs,
3e7ee490
HJ
319};
320
adf874cb 321static const char *driver_name = "hyperv";
36199a99 322
36199a99 323
bf6506f6
TT
324struct onmessage_work_context {
325 struct work_struct work;
326 struct hv_message msg;
327};
328
329static void vmbus_onmessage_work(struct work_struct *work)
330{
331 struct onmessage_work_context *ctx;
332
333 ctx = container_of(work, struct onmessage_work_context,
334 work);
335 vmbus_onmessage(&ctx->msg);
336 kfree(ctx);
337}
338
36199a99
GKH
339/*
340 * vmbus_on_msg_dpc - DPC routine to handle messages from the hypervisior
341 */
62c1059d 342static void vmbus_on_msg_dpc(unsigned long data)
36199a99
GKH
343{
344 int cpu = smp_processor_id();
345 void *page_addr = hv_context.synic_message_page[cpu];
346 struct hv_message *msg = (struct hv_message *)page_addr +
347 VMBUS_MESSAGE_SINT;
bf6506f6 348 struct onmessage_work_context *ctx;
36199a99
GKH
349
350 while (1) {
351 if (msg->header.message_type == HVMSG_NONE) {
352 /* no msg */
353 break;
354 } else {
bf6506f6
TT
355 ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
356 if (ctx == NULL)
36199a99 357 continue;
bf6506f6
TT
358 INIT_WORK(&ctx->work, vmbus_onmessage_work);
359 memcpy(&ctx->msg, msg, sizeof(*msg));
da9fcb72 360 queue_work(vmbus_connection.work_queue, &ctx->work);
36199a99
GKH
361 }
362
363 msg->header.message_type = HVMSG_NONE;
364
365 /*
366 * Make sure the write to MessageType (ie set to
367 * HVMSG_NONE) happens before we read the
368 * MessagePending and EOMing. Otherwise, the EOMing
369 * will not deliver any more messages since there is
370 * no empty slot
371 */
372 mb();
373
374 if (msg->header.message_flags.msg_pending) {
375 /*
376 * This will cause message queue rescan to
377 * possibly deliver another msg from the
378 * hypervisor
379 */
380 wrmsrl(HV_X64_MSR_EOM, 0);
381 }
382 }
383}
384
36199a99
GKH
385/*
386 * vmbus_on_isr - ISR routine
387 */
480ae58d 388static int vmbus_on_isr(void)
36199a99
GKH
389{
390 int ret = 0;
391 int cpu = smp_processor_id();
392 void *page_addr;
393 struct hv_message *msg;
394 union hv_synic_event_flags *event;
395
396 page_addr = hv_context.synic_message_page[cpu];
397 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
398
399 /* Check if there are actual msgs to be process */
400 if (msg->header.message_type != HVMSG_NONE) {
401 DPRINT_DBG(VMBUS, "received msg type %d size %d",
402 msg->header.message_type,
403 msg->header.payload_size);
404 ret |= 0x1;
405 }
406
407 /* TODO: Check if there are events to be process */
408 page_addr = hv_context.synic_event_page[cpu];
409 event = (union hv_synic_event_flags *)page_addr + VMBUS_MESSAGE_SINT;
410
411 /* Since we are a child, we only need to check bit 0 */
412 if (test_and_clear_bit(0, (unsigned long *) &event->flags32[0])) {
413 DPRINT_DBG(VMBUS, "received event %d", event->flags32[0]);
414 ret |= 0x2;
415 }
416
417 return ret;
418}
419
793be9c7
S
420
421static irqreturn_t vmbus_isr(int irq, void *dev_id)
422{
423 int ret;
424
425 ret = vmbus_on_isr();
426
427 /* Schedules a dpc if necessary */
428 if (ret > 0) {
429 if (test_bit(0, (unsigned long *)&ret))
430 tasklet_schedule(&hv_bus.msg_dpc);
431
432 if (test_bit(1, (unsigned long *)&ret))
433 tasklet_schedule(&hv_bus.event_dpc);
434
435 return IRQ_HANDLED;
436 } else {
437 return IRQ_NONE;
438 }
439}
440
441
150b19d4
GKH
442static void get_channel_info(struct hv_device *device,
443 struct hv_device_info *info)
444{
445 struct vmbus_channel_debug_info debug_info;
446
cae5b843 447 if (!device->channel)
150b19d4
GKH
448 return;
449
cae5b843 450 vmbus_get_debug_info(device->channel, &debug_info);
150b19d4 451
ca623ad3
HZ
452 info->chn_id = debug_info.relid;
453 info->chn_state = debug_info.state;
454 memcpy(&info->chn_type, &debug_info.interfacetype,
150b19d4 455 sizeof(struct hv_guid));
ca623ad3 456 memcpy(&info->chn_instance, &debug_info.interface_instance,
150b19d4
GKH
457 sizeof(struct hv_guid));
458
ca623ad3 459 info->monitor_id = debug_info.monitorid;
150b19d4 460
ca623ad3
HZ
461 info->server_monitor_pending = debug_info.servermonitor_pending;
462 info->server_monitor_latency = debug_info.servermonitor_latency;
463 info->server_monitor_conn_id = debug_info.servermonitor_connectionid;
150b19d4 464
ca623ad3
HZ
465 info->client_monitor_pending = debug_info.clientmonitor_pending;
466 info->client_monitor_latency = debug_info.clientmonitor_latency;
467 info->client_monitor_conn_id = debug_info.clientmonitor_connectionid;
150b19d4 468
ca623ad3
HZ
469 info->inbound.int_mask = debug_info.inbound.current_interrupt_mask;
470 info->inbound.read_idx = debug_info.inbound.current_read_index;
471 info->inbound.write_idx = debug_info.inbound.current_write_index;
472 info->inbound.bytes_avail_toread =
473 debug_info.inbound.bytes_avail_toread;
474 info->inbound.bytes_avail_towrite =
82f8bd40
HZ
475 debug_info.inbound.bytes_avail_towrite;
476
ca623ad3 477 info->outbound.int_mask =
82f8bd40 478 debug_info.outbound.current_interrupt_mask;
ca623ad3
HZ
479 info->outbound.read_idx = debug_info.outbound.current_read_index;
480 info->outbound.write_idx = debug_info.outbound.current_write_index;
481 info->outbound.bytes_avail_toread =
82f8bd40 482 debug_info.outbound.bytes_avail_toread;
ca623ad3 483 info->outbound.bytes_avail_towrite =
82f8bd40 484 debug_info.outbound.bytes_avail_towrite;
150b19d4
GKH
485}
486
3e189519 487/*
90c9960e
GKH
488 * vmbus_show_device_attr - Show the device attribute in sysfs.
489 *
490 * This is invoked when user does a
491 * "cat /sys/bus/vmbus/devices/<busdevice>/<attr name>"
492 */
493static ssize_t vmbus_show_device_attr(struct device *dev,
494 struct device_attribute *dev_attr,
495 char *buf)
3e7ee490 496{
6bad88da 497 struct hv_device *device_ctx = device_to_hv_device(dev);
ee3d7ddf 498 struct hv_device_info device_info;
3e7ee490 499
ee3d7ddf 500 memset(&device_info, 0, sizeof(struct hv_device_info));
3e7ee490 501
6bad88da 502 get_channel_info(device_ctx, &device_info);
3e7ee490 503
90c9960e
GKH
504 if (!strcmp(dev_attr->attr.name, "class_id")) {
505 return sprintf(buf, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-"
506 "%02x%02x%02x%02x%02x%02x%02x%02x}\n",
ca623ad3
HZ
507 device_info.chn_type.data[3],
508 device_info.chn_type.data[2],
509 device_info.chn_type.data[1],
510 device_info.chn_type.data[0],
511 device_info.chn_type.data[5],
512 device_info.chn_type.data[4],
513 device_info.chn_type.data[7],
514 device_info.chn_type.data[6],
515 device_info.chn_type.data[8],
516 device_info.chn_type.data[9],
517 device_info.chn_type.data[10],
518 device_info.chn_type.data[11],
519 device_info.chn_type.data[12],
520 device_info.chn_type.data[13],
521 device_info.chn_type.data[14],
522 device_info.chn_type.data[15]);
90c9960e
GKH
523 } else if (!strcmp(dev_attr->attr.name, "device_id")) {
524 return sprintf(buf, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-"
525 "%02x%02x%02x%02x%02x%02x%02x%02x}\n",
ca623ad3
HZ
526 device_info.chn_instance.data[3],
527 device_info.chn_instance.data[2],
528 device_info.chn_instance.data[1],
529 device_info.chn_instance.data[0],
530 device_info.chn_instance.data[5],
531 device_info.chn_instance.data[4],
532 device_info.chn_instance.data[7],
533 device_info.chn_instance.data[6],
534 device_info.chn_instance.data[8],
535 device_info.chn_instance.data[9],
536 device_info.chn_instance.data[10],
537 device_info.chn_instance.data[11],
538 device_info.chn_instance.data[12],
539 device_info.chn_instance.data[13],
540 device_info.chn_instance.data[14],
541 device_info.chn_instance.data[15]);
90c9960e 542 } else if (!strcmp(dev_attr->attr.name, "state")) {
ca623ad3 543 return sprintf(buf, "%d\n", device_info.chn_state);
90c9960e 544 } else if (!strcmp(dev_attr->attr.name, "id")) {
ca623ad3 545 return sprintf(buf, "%d\n", device_info.chn_id);
90c9960e 546 } else if (!strcmp(dev_attr->attr.name, "out_intr_mask")) {
ca623ad3 547 return sprintf(buf, "%d\n", device_info.outbound.int_mask);
90c9960e 548 } else if (!strcmp(dev_attr->attr.name, "out_read_index")) {
ca623ad3 549 return sprintf(buf, "%d\n", device_info.outbound.read_idx);
90c9960e 550 } else if (!strcmp(dev_attr->attr.name, "out_write_index")) {
ca623ad3 551 return sprintf(buf, "%d\n", device_info.outbound.write_idx);
90c9960e
GKH
552 } else if (!strcmp(dev_attr->attr.name, "out_read_bytes_avail")) {
553 return sprintf(buf, "%d\n",
ca623ad3 554 device_info.outbound.bytes_avail_toread);
90c9960e
GKH
555 } else if (!strcmp(dev_attr->attr.name, "out_write_bytes_avail")) {
556 return sprintf(buf, "%d\n",
ca623ad3 557 device_info.outbound.bytes_avail_towrite);
90c9960e 558 } else if (!strcmp(dev_attr->attr.name, "in_intr_mask")) {
ca623ad3 559 return sprintf(buf, "%d\n", device_info.inbound.int_mask);
90c9960e 560 } else if (!strcmp(dev_attr->attr.name, "in_read_index")) {
ca623ad3 561 return sprintf(buf, "%d\n", device_info.inbound.read_idx);
90c9960e 562 } else if (!strcmp(dev_attr->attr.name, "in_write_index")) {
ca623ad3 563 return sprintf(buf, "%d\n", device_info.inbound.write_idx);
90c9960e
GKH
564 } else if (!strcmp(dev_attr->attr.name, "in_read_bytes_avail")) {
565 return sprintf(buf, "%d\n",
ca623ad3 566 device_info.inbound.bytes_avail_toread);
90c9960e
GKH
567 } else if (!strcmp(dev_attr->attr.name, "in_write_bytes_avail")) {
568 return sprintf(buf, "%d\n",
ca623ad3 569 device_info.inbound.bytes_avail_towrite);
90c9960e 570 } else if (!strcmp(dev_attr->attr.name, "monitor_id")) {
ca623ad3 571 return sprintf(buf, "%d\n", device_info.monitor_id);
90c9960e 572 } else if (!strcmp(dev_attr->attr.name, "server_monitor_pending")) {
ca623ad3 573 return sprintf(buf, "%d\n", device_info.server_monitor_pending);
90c9960e 574 } else if (!strcmp(dev_attr->attr.name, "server_monitor_latency")) {
ca623ad3 575 return sprintf(buf, "%d\n", device_info.server_monitor_latency);
90c9960e
GKH
576 } else if (!strcmp(dev_attr->attr.name, "server_monitor_conn_id")) {
577 return sprintf(buf, "%d\n",
ca623ad3 578 device_info.server_monitor_conn_id);
90c9960e 579 } else if (!strcmp(dev_attr->attr.name, "client_monitor_pending")) {
ca623ad3 580 return sprintf(buf, "%d\n", device_info.client_monitor_pending);
90c9960e 581 } else if (!strcmp(dev_attr->attr.name, "client_monitor_latency")) {
ca623ad3 582 return sprintf(buf, "%d\n", device_info.client_monitor_latency);
90c9960e
GKH
583 } else if (!strcmp(dev_attr->attr.name, "client_monitor_conn_id")) {
584 return sprintf(buf, "%d\n",
ca623ad3 585 device_info.client_monitor_conn_id);
90c9960e 586 } else {
3e7ee490
HJ
587 return 0;
588 }
589}
590
3e189519 591/*
90c9960e
GKH
592 * vmbus_bus_init -Main vmbus driver initialization routine.
593 *
594 * Here, we
0686e4f4 595 * - initialize the vmbus driver context
0686e4f4
LL
596 * - invoke the vmbus hv main init routine
597 * - get the irq resource
0686e4f4 598 * - retrieve the channel offers
90c9960e 599 */
52e5c1ce 600static int vmbus_bus_init(struct pci_dev *pdev)
3e7ee490 601{
90c9960e
GKH
602 int ret;
603 unsigned int vector;
3e7ee490 604
6d26e38f
GKH
605 DPRINT_INFO(VMBUS, "+++++++ HV Driver version = %s +++++++",
606 HV_DRV_VERSION);
607 DPRINT_INFO(VMBUS, "+++++++ Vmbus supported version = %d +++++++",
608 VMBUS_REVISION_NUMBER);
609 DPRINT_INFO(VMBUS, "+++++++ Vmbus using SINT %d +++++++",
610 VMBUS_MESSAGE_SINT);
611 DPRINT_DBG(VMBUS, "sizeof(vmbus_channel_packet_page_buffer)=%zd, "
612 "sizeof(VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER)=%zd",
613 sizeof(struct vmbus_channel_packet_page_buffer),
614 sizeof(struct vmbus_channel_packet_multipage_buffer));
615
6d26e38f
GKH
616
617 /* Hypervisor initialization...setup hypercall page..etc */
618 ret = hv_init();
90c9960e 619 if (ret != 0) {
6d26e38f
GKH
620 DPRINT_ERR(VMBUS, "Unable to initialize the hypervisor - 0x%x",
621 ret);
3e7ee490
HJ
622 goto cleanup;
623 }
624
3e7ee490 625
04677c08 626 hv_bus.bus.name = driver_name;
3e7ee490 627
454f18a9 628 /* Initialize the bus context */
04677c08 629 tasklet_init(&hv_bus.msg_dpc, vmbus_on_msg_dpc,
a6e4d8e3 630 (unsigned long)NULL);
04677c08 631 tasklet_init(&hv_bus.event_dpc, vmbus_on_event,
a6e4d8e3 632 (unsigned long)NULL);
3e7ee490 633
a6e4d8e3 634 /* Now, register the bus with LDM */
04677c08 635 ret = bus_register(&hv_bus.bus);
90c9960e 636 if (ret) {
c19fbca3
BP
637 ret = -1;
638 goto cleanup;
639 }
3e7ee490 640
454f18a9 641 /* Get the interrupt resource */
52e5c1ce
S
642 ret = request_irq(pdev->irq, vmbus_isr,
643 IRQF_SHARED | IRQF_SAMPLE_RANDOM,
644 driver_name, pdev);
3e7ee490 645
90c9960e
GKH
646 if (ret != 0) {
647 DPRINT_ERR(VMBUS_DRV, "ERROR - Unable to request IRQ %d",
52e5c1ce 648 pdev->irq);
3e7ee490 649
04677c08 650 bus_unregister(&hv_bus.bus);
3e7ee490
HJ
651
652 ret = -1;
653 goto cleanup;
654 }
3e7ee490 655
52e5c1ce
S
656 vector = IRQ0_VECTOR + pdev->irq;
657 DPRINT_INFO(VMBUS_DRV, "irq 0x%x vector 0x%x", pdev->irq,
658 vector);
3e7ee490 659
800b6902
S
660 /*
661 * Notify the hypervisor of our irq and
662 * connect to the host.
663 */
664 on_each_cpu(hv_synic_init, (void *)&vector, 1);
665 ret = vmbus_connect();
90c9960e 666 if (ret) {
52e5c1ce 667 free_irq(pdev->irq, pdev);
04677c08 668 bus_unregister(&hv_bus.bus);
5d48a1c2
BP
669 goto cleanup;
670 }
671
800b6902 672
2d6e882b 673 vmbus_request_offers();
8b5d6d3b
HZ
674 wait_for_completion(&hv_channel_ready);
675
3e7ee490 676cleanup:
3e7ee490
HJ
677 return ret;
678}
679
3e189519 680/*
90c9960e
GKH
681 * vmbus_bus_exit - Terminate the vmbus driver.
682 *
683 * This routine is opposite of vmbus_bus_init()
684 */
bd1de709 685static void vmbus_bus_exit(void)
3e7ee490 686{
3e7ee490 687
3e7ee490 688
f51b3593
S
689 vmbus_release_unattached_channels();
690 vmbus_disconnect();
691 on_each_cpu(hv_synic_cleanup, NULL, 1);
3e7ee490 692
ece0e32f 693 hv_cleanup();
3e7ee490 694
04677c08 695 bus_unregister(&hv_bus.bus);
3e7ee490 696
52e5c1ce 697 free_irq(hv_pci_dev->irq, hv_pci_dev);
3e7ee490 698
04677c08
S
699 tasklet_kill(&hv_bus.msg_dpc);
700 tasklet_kill(&hv_bus.event_dpc);
3e7ee490
HJ
701}
702
3e189519 703
90c9960e 704/**
3e189519 705 * vmbus_child_driver_register() - Register a vmbus's child driver
c643269d 706 * @drv: Pointer to driver structure you want to register
3e189519 707 *
3e189519
HJ
708 *
709 * Registers the given driver with Linux through the 'driver_register()' call
710 * And sets up the hyper-v vmbus handling for this driver.
711 * It will return the state of the 'driver_register()' call.
712 *
713 * Mainly used by Hyper-V drivers.
90c9960e 714 */
c643269d 715int vmbus_child_driver_register(struct device_driver *drv)
3e7ee490 716{
5d48a1c2 717 int ret;
3e7ee490 718
90c9960e 719 DPRINT_INFO(VMBUS_DRV, "child driver (%p) registering - name %s",
c643269d 720 drv, drv->name);
3e7ee490 721
454f18a9 722 /* The child driver on this vmbus */
04677c08 723 drv->bus = &hv_bus.bus;
3e7ee490 724
c643269d 725 ret = driver_register(drv);
3e7ee490 726
2d6e882b 727 vmbus_request_offers();
3e7ee490 728
5d48a1c2 729 return ret;
3e7ee490 730}
3e7ee490
HJ
731EXPORT_SYMBOL(vmbus_child_driver_register);
732
90c9960e 733/**
3e189519 734 * vmbus_child_driver_unregister() - Unregister a vmbus's child driver
06de23f7 735 * @drv: Pointer to driver structure you want to un-register
3e189519 736 *
3e189519
HJ
737 *
738 * Un-register the given driver with Linux through the 'driver_unregister()'
739 * call. And ungegisters the driver from the Hyper-V vmbus handler.
740 *
741 * Mainly used by Hyper-V drivers.
90c9960e 742 */
06de23f7 743void vmbus_child_driver_unregister(struct device_driver *drv)
3e7ee490 744{
90c9960e 745 DPRINT_INFO(VMBUS_DRV, "child driver (%p) unregistering - name %s",
06de23f7 746 drv, drv->name);
3e7ee490 747
06de23f7 748 driver_unregister(drv);
3e7ee490 749
06de23f7 750 drv->bus = NULL;
3e7ee490 751}
3e7ee490
HJ
752EXPORT_SYMBOL(vmbus_child_driver_unregister);
753
3e189519
HJ
754/*
755 * vmbus_child_device_create - Creates and registers a new child device
756 * on the vmbus.
90c9960e 757 */
89733aa9
GKH
758struct hv_device *vmbus_child_device_create(struct hv_guid *type,
759 struct hv_guid *instance,
760 struct vmbus_channel *channel)
3e7ee490 761{
3d3b5518 762 struct hv_device *child_device_obj;
3e7ee490 763
454f18a9 764 /* Allocate the new child device */
6bad88da
S
765 child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
766 if (!child_device_obj) {
90c9960e
GKH
767 DPRINT_ERR(VMBUS_DRV,
768 "unable to allocate device_context for child device");
3e7ee490
HJ
769 return NULL;
770 }
771
772 DPRINT_DBG(VMBUS_DRV, "child device (%p) allocated - "
90c9960e
GKH
773 "type {%02x%02x%02x%02x-%02x%02x-%02x%02x-"
774 "%02x%02x%02x%02x%02x%02x%02x%02x},"
775 "id {%02x%02x%02x%02x-%02x%02x-%02x%02x-"
776 "%02x%02x%02x%02x%02x%02x%02x%02x}",
6bad88da 777 &child_device_obj->device,
daaa8cc3
GKH
778 type->data[3], type->data[2], type->data[1], type->data[0],
779 type->data[5], type->data[4], type->data[7], type->data[6],
780 type->data[8], type->data[9], type->data[10], type->data[11],
781 type->data[12], type->data[13], type->data[14], type->data[15],
90c9960e
GKH
782 instance->data[3], instance->data[2],
783 instance->data[1], instance->data[0],
784 instance->data[5], instance->data[4],
785 instance->data[7], instance->data[6],
786 instance->data[8], instance->data[9],
787 instance->data[10], instance->data[11],
788 instance->data[12], instance->data[13],
789 instance->data[14], instance->data[15]);
3e7ee490 790
cae5b843 791 child_device_obj->channel = channel;
ca623ad3
HZ
792 memcpy(&child_device_obj->dev_type, type, sizeof(struct hv_guid));
793 memcpy(&child_device_obj->dev_instance, instance,
90c9960e 794 sizeof(struct hv_guid));
3e7ee490 795
3e7ee490 796
3e7ee490
HJ
797 return child_device_obj;
798}
799
3e189519 800/*
e13a0b5a 801 * vmbus_child_device_register - Register the child device
90c9960e 802 */
3ca07cb0 803int vmbus_child_device_register(struct hv_device *child_device_obj)
3e7ee490 804{
90c9960e 805 int ret = 0;
6bad88da 806
f4888417 807 static atomic_t device_num = ATOMIC_INIT(0);
3e7ee490 808
90c9960e 809 DPRINT_DBG(VMBUS_DRV, "child device (%p) registering",
6bad88da 810 child_device_obj);
454f18a9 811
1bb40a25 812 /* Set the device name. Otherwise, device_register() will fail. */
6bad88da 813 dev_set_name(&child_device_obj->device, "vmbus_0_%d",
90c9960e 814 atomic_inc_return(&device_num));
3e7ee490 815
454f18a9 816 /* The new device belongs to this bus */
04677c08 817 child_device_obj->device.bus = &hv_bus.bus; /* device->dev.bus; */
800b6902 818 child_device_obj->device.parent = &hv_pci_dev->dev;
6bad88da 819 child_device_obj->device.release = vmbus_device_release;
3e7ee490 820
90c9960e
GKH
821 /*
822 * Register with the LDM. This will kick off the driver/device
823 * binding...which will eventually call vmbus_match() and vmbus_probe()
824 */
6bad88da 825 ret = device_register(&child_device_obj->device);
3e7ee490 826
454f18a9 827 /* vmbus_probe() error does not get propergate to device_register(). */
6bad88da 828 ret = child_device_obj->probe_error;
3e7ee490
HJ
829
830 if (ret)
90c9960e 831 DPRINT_ERR(VMBUS_DRV, "unable to register child device (%p)",
6bad88da 832 &child_device_obj->device);
3e7ee490 833 else
90c9960e 834 DPRINT_INFO(VMBUS_DRV, "child device (%p) registered",
6bad88da 835 &child_device_obj->device);
3e7ee490 836
3e7ee490
HJ
837 return ret;
838}
839
3e189519
HJ
840/*
841 * vmbus_child_device_unregister - Remove the specified child device
842 * from the vmbus.
90c9960e 843 */
9d8bd71a 844void vmbus_child_device_unregister(struct hv_device *device_obj)
3e7ee490 845{
3e7ee490 846
90c9960e 847 DPRINT_INFO(VMBUS_DRV, "unregistering child device (%p)",
6bad88da 848 &device_obj->device);
3e7ee490 849
90c9960e
GKH
850 /*
851 * Kick off the process of unregistering the device.
852 * This will call vmbus_remove() and eventually vmbus_device_release()
853 */
6bad88da 854 device_unregister(&device_obj->device);
3e7ee490 855
90c9960e 856 DPRINT_INFO(VMBUS_DRV, "child device (%p) unregistered",
6bad88da 857 &device_obj->device);
3e7ee490
HJ
858}
859
3e7ee490 860
1168ac22
S
861static int __devinit hv_pci_probe(struct pci_dev *pdev,
862 const struct pci_device_id *ent)
3e7ee490 863{
1168ac22 864 int err;
3e7ee490 865
1168ac22 866 hv_pci_dev = pdev;
c22090fa 867
1168ac22
S
868 err = pci_enable_device(pdev);
869 if (err)
870 return err;
3e7ee490 871
52e5c1ce 872 err = vmbus_bus_init(pdev);
1168ac22
S
873 if (err)
874 pci_disable_device(pdev);
875
876 return err;
3e7ee490
HJ
877}
878
9a775dbd
GKH
879/*
880 * We use a PCI table to determine if we should autoload this driver This is
881 * needed by distro tools to determine if the hyperv drivers should be
882 * installed and/or configured. We don't do anything else with the table, but
883 * it needs to be present.
9a775dbd 884 */
15f0beb1 885static const struct pci_device_id microsoft_hv_pci_table[] = {
9a775dbd
GKH
886 { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
887 { 0 }
888};
889MODULE_DEVICE_TABLE(pci, microsoft_hv_pci_table);
890
1168ac22
S
891static struct pci_driver hv_bus_driver = {
892 .name = "hv_bus",
893 .probe = hv_pci_probe,
894 .id_table = microsoft_hv_pci_table,
895};
896
897static int __init hv_pci_init(void)
898{
899 return pci_register_driver(&hv_bus_driver);
900}
901
902static void __exit hv_pci_exit(void)
903{
904 vmbus_bus_exit();
905 pci_unregister_driver(&hv_bus_driver);
906}
907
908
909
90c9960e 910MODULE_LICENSE("GPL");
26c14cc1 911MODULE_VERSION(HV_DRV_VERSION);
3e7ee490 912module_param(vmbus_loglevel, int, S_IRUGO);
3e7ee490 913
1168ac22
S
914module_init(hv_pci_init);
915module_exit(hv_pci_exit);