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