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