blob: 38bfff044dfb8adc633a1d80cb6f6c8deddde5b0 [file] [log] [blame]
Hank Janssen3e7ee492009-07-13 16:02:34 -07001/*
Hank Janssen3e7ee492009-07-13 16:02:34 -07002 * 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>
K. Y. Srinivasan52e5c1c2011-03-15 15:03:34 -070020 *
21 * 3/9/2011: K. Y. Srinivasan - Significant restructuring and cleanup
Hank Janssen3e7ee492009-07-13 16:02:34 -070022 */
Hank Janssen0a466182011-03-29 13:58:47 -070023#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
Hank Janssen3e7ee492009-07-13 16:02:34 -070025#include <linux/init.h>
26#include <linux/module.h>
27#include <linux/device.h>
28#include <linux/irq.h>
29#include <linux/interrupt.h>
30#include <linux/sysctl.h>
Greg Kroah-Hartman9a775db2010-02-25 16:42:10 -080031#include <linux/pci.h>
Greg Kroah-Hartmanc22090f2010-02-25 16:43:15 -080032#include <linux/dmi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +000034#include <linux/completion.h>
Greg Kroah-Hartman2d82f6c2010-05-05 22:52:28 -070035#include "version_info.h"
K. Y. Srinivasane3fe0bb2011-02-11 10:00:12 -080036#include "hv_api.h"
Greg Kroah-Hartman645954c2009-08-28 16:22:59 -070037#include "logging.h"
Greg Kroah-Hartman870cde82009-08-19 16:21:28 -070038#include "vmbus.h"
Greg Kroah-Hartman150b19d2010-10-20 16:07:11 -070039#include "channel.h"
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -080040#include "vmbus_private.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070041
Hank Janssen3e7ee492009-07-13 16:02:34 -070042
K. Y. Srinivasanef58f5d2011-04-29 13:45:05 -070043static struct pci_dev *hv_pci_dev;
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -070044
K. Y. Srinivasan59c0e4f2011-04-29 13:45:06 -070045static struct tasklet_struct msg_dpc;
46
Bill Pemberton454f18a2009-07-27 16:47:24 -040047/* Main vmbus driver data structure */
K. Y. Srinivasan04677c02011-03-15 15:03:35 -070048struct hv_bus {
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -070049 struct bus_type bus;
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -070050 struct tasklet_struct event_dpc;
Hank Janssen3e7ee492009-07-13 16:02:34 -070051};
52
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -070053unsigned int vmbus_loglevel = (ALL_MODULES << 16 | INFO_LVL);
Hank Janssen3e7ee492009-07-13 16:02:34 -070054EXPORT_SYMBOL(vmbus_loglevel);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -070055 /* (ALL_MODULES << 16 | DEBUG_LVL_ENTEREXIT); */
56 /* (((VMBUS | VMBUS_DRV)<<16) | DEBUG_LVL_ENTEREXIT); */
Hank Janssen3e7ee492009-07-13 16:02:34 -070057
K. Y. Srinivasan71a66552011-04-29 13:45:04 -070058static int pci_probe_error;
59static struct completion probe_event;
Hank Janssen3e7ee492009-07-13 16:02:34 -070060
K. Y. Srinivasan98db4332011-03-15 15:03:44 -070061static void get_channel_info(struct hv_device *device,
62 struct hv_device_info *info)
63{
64 struct vmbus_channel_debug_info debug_info;
65
66 if (!device->channel)
67 return;
68
69 vmbus_get_debug_info(device->channel, &debug_info);
70
71 info->chn_id = debug_info.relid;
72 info->chn_state = debug_info.state;
73 memcpy(&info->chn_type, &debug_info.interfacetype,
74 sizeof(struct hv_guid));
75 memcpy(&info->chn_instance, &debug_info.interface_instance,
76 sizeof(struct hv_guid));
77
78 info->monitor_id = debug_info.monitorid;
79
80 info->server_monitor_pending = debug_info.servermonitor_pending;
81 info->server_monitor_latency = debug_info.servermonitor_latency;
82 info->server_monitor_conn_id = debug_info.servermonitor_connectionid;
83
84 info->client_monitor_pending = debug_info.clientmonitor_pending;
85 info->client_monitor_latency = debug_info.clientmonitor_latency;
86 info->client_monitor_conn_id = debug_info.clientmonitor_connectionid;
87
88 info->inbound.int_mask = debug_info.inbound.current_interrupt_mask;
89 info->inbound.read_idx = debug_info.inbound.current_read_index;
90 info->inbound.write_idx = debug_info.inbound.current_write_index;
91 info->inbound.bytes_avail_toread =
92 debug_info.inbound.bytes_avail_toread;
93 info->inbound.bytes_avail_towrite =
94 debug_info.inbound.bytes_avail_towrite;
95
96 info->outbound.int_mask =
97 debug_info.outbound.current_interrupt_mask;
98 info->outbound.read_idx = debug_info.outbound.current_read_index;
99 info->outbound.write_idx = debug_info.outbound.current_write_index;
100 info->outbound.bytes_avail_toread =
101 debug_info.outbound.bytes_avail_toread;
102 info->outbound.bytes_avail_towrite =
103 debug_info.outbound.bytes_avail_towrite;
104}
105
106/*
107 * vmbus_show_device_attr - Show the device attribute in sysfs.
108 *
109 * This is invoked when user does a
110 * "cat /sys/bus/vmbus/devices/<busdevice>/<attr name>"
111 */
112static ssize_t vmbus_show_device_attr(struct device *dev,
113 struct device_attribute *dev_attr,
114 char *buf)
115{
116 struct hv_device *device_ctx = device_to_hv_device(dev);
117 struct hv_device_info device_info;
118
119 memset(&device_info, 0, sizeof(struct hv_device_info));
120
121 get_channel_info(device_ctx, &device_info);
122
123 if (!strcmp(dev_attr->attr.name, "class_id")) {
124 return sprintf(buf, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-"
125 "%02x%02x%02x%02x%02x%02x%02x%02x}\n",
126 device_info.chn_type.data[3],
127 device_info.chn_type.data[2],
128 device_info.chn_type.data[1],
129 device_info.chn_type.data[0],
130 device_info.chn_type.data[5],
131 device_info.chn_type.data[4],
132 device_info.chn_type.data[7],
133 device_info.chn_type.data[6],
134 device_info.chn_type.data[8],
135 device_info.chn_type.data[9],
136 device_info.chn_type.data[10],
137 device_info.chn_type.data[11],
138 device_info.chn_type.data[12],
139 device_info.chn_type.data[13],
140 device_info.chn_type.data[14],
141 device_info.chn_type.data[15]);
142 } else if (!strcmp(dev_attr->attr.name, "device_id")) {
143 return sprintf(buf, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-"
144 "%02x%02x%02x%02x%02x%02x%02x%02x}\n",
145 device_info.chn_instance.data[3],
146 device_info.chn_instance.data[2],
147 device_info.chn_instance.data[1],
148 device_info.chn_instance.data[0],
149 device_info.chn_instance.data[5],
150 device_info.chn_instance.data[4],
151 device_info.chn_instance.data[7],
152 device_info.chn_instance.data[6],
153 device_info.chn_instance.data[8],
154 device_info.chn_instance.data[9],
155 device_info.chn_instance.data[10],
156 device_info.chn_instance.data[11],
157 device_info.chn_instance.data[12],
158 device_info.chn_instance.data[13],
159 device_info.chn_instance.data[14],
160 device_info.chn_instance.data[15]);
161 } else if (!strcmp(dev_attr->attr.name, "state")) {
162 return sprintf(buf, "%d\n", device_info.chn_state);
163 } else if (!strcmp(dev_attr->attr.name, "id")) {
164 return sprintf(buf, "%d\n", device_info.chn_id);
165 } else if (!strcmp(dev_attr->attr.name, "out_intr_mask")) {
166 return sprintf(buf, "%d\n", device_info.outbound.int_mask);
167 } else if (!strcmp(dev_attr->attr.name, "out_read_index")) {
168 return sprintf(buf, "%d\n", device_info.outbound.read_idx);
169 } else if (!strcmp(dev_attr->attr.name, "out_write_index")) {
170 return sprintf(buf, "%d\n", device_info.outbound.write_idx);
171 } else if (!strcmp(dev_attr->attr.name, "out_read_bytes_avail")) {
172 return sprintf(buf, "%d\n",
173 device_info.outbound.bytes_avail_toread);
174 } else if (!strcmp(dev_attr->attr.name, "out_write_bytes_avail")) {
175 return sprintf(buf, "%d\n",
176 device_info.outbound.bytes_avail_towrite);
177 } else if (!strcmp(dev_attr->attr.name, "in_intr_mask")) {
178 return sprintf(buf, "%d\n", device_info.inbound.int_mask);
179 } else if (!strcmp(dev_attr->attr.name, "in_read_index")) {
180 return sprintf(buf, "%d\n", device_info.inbound.read_idx);
181 } else if (!strcmp(dev_attr->attr.name, "in_write_index")) {
182 return sprintf(buf, "%d\n", device_info.inbound.write_idx);
183 } else if (!strcmp(dev_attr->attr.name, "in_read_bytes_avail")) {
184 return sprintf(buf, "%d\n",
185 device_info.inbound.bytes_avail_toread);
186 } else if (!strcmp(dev_attr->attr.name, "in_write_bytes_avail")) {
187 return sprintf(buf, "%d\n",
188 device_info.inbound.bytes_avail_towrite);
189 } else if (!strcmp(dev_attr->attr.name, "monitor_id")) {
190 return sprintf(buf, "%d\n", device_info.monitor_id);
191 } else if (!strcmp(dev_attr->attr.name, "server_monitor_pending")) {
192 return sprintf(buf, "%d\n", device_info.server_monitor_pending);
193 } else if (!strcmp(dev_attr->attr.name, "server_monitor_latency")) {
194 return sprintf(buf, "%d\n", device_info.server_monitor_latency);
195 } else if (!strcmp(dev_attr->attr.name, "server_monitor_conn_id")) {
196 return sprintf(buf, "%d\n",
197 device_info.server_monitor_conn_id);
198 } else if (!strcmp(dev_attr->attr.name, "client_monitor_pending")) {
199 return sprintf(buf, "%d\n", device_info.client_monitor_pending);
200 } else if (!strcmp(dev_attr->attr.name, "client_monitor_latency")) {
201 return sprintf(buf, "%d\n", device_info.client_monitor_latency);
202 } else if (!strcmp(dev_attr->attr.name, "client_monitor_conn_id")) {
203 return sprintf(buf, "%d\n",
204 device_info.client_monitor_conn_id);
205 } else {
206 return 0;
207 }
208}
209
Bill Pemberton454f18a2009-07-27 16:47:24 -0400210/* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700211static struct device_attribute vmbus_device_attrs[] = {
212 __ATTR(id, S_IRUGO, vmbus_show_device_attr, NULL),
213 __ATTR(state, S_IRUGO, vmbus_show_device_attr, NULL),
214 __ATTR(class_id, S_IRUGO, vmbus_show_device_attr, NULL),
215 __ATTR(device_id, S_IRUGO, vmbus_show_device_attr, NULL),
216 __ATTR(monitor_id, S_IRUGO, vmbus_show_device_attr, NULL),
217
218 __ATTR(server_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL),
219 __ATTR(server_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL),
220 __ATTR(server_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL),
221
222 __ATTR(client_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL),
223 __ATTR(client_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL),
224 __ATTR(client_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL),
225
226 __ATTR(out_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL),
227 __ATTR(out_read_index, S_IRUGO, vmbus_show_device_attr, NULL),
228 __ATTR(out_write_index, S_IRUGO, vmbus_show_device_attr, NULL),
229 __ATTR(out_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
230 __ATTR(out_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
231
232 __ATTR(in_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL),
233 __ATTR(in_read_index, S_IRUGO, vmbus_show_device_attr, NULL),
234 __ATTR(in_write_index, S_IRUGO, vmbus_show_device_attr, NULL),
235 __ATTR(in_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
236 __ATTR(in_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
237 __ATTR_NULL
238};
Hank Janssen3e7ee492009-07-13 16:02:34 -0700239
K. Y. Srinivasan793be9c2011-03-15 15:03:43 -0700240
K. Y. Srinivasanadde2482011-03-15 15:03:37 -0700241/*
242 * vmbus_uevent - add uevent for our device
243 *
244 * This routine is invoked when a device is added or removed on the vmbus to
245 * generate a uevent to udev in the userspace. The udev will then look at its
246 * rule and the uevent generated here to load the appropriate driver
247 */
248static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
249{
250 struct hv_device *dev = device_to_hv_device(device);
251 int ret;
252
K. Y. Srinivasanadde2482011-03-15 15:03:37 -0700253 ret = add_uevent_var(env, "VMBUS_DEVICE_CLASS_GUID={"
254 "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
255 "%02x%02x%02x%02x%02x%02x%02x%02x}",
256 dev->dev_type.data[3],
257 dev->dev_type.data[2],
258 dev->dev_type.data[1],
259 dev->dev_type.data[0],
260 dev->dev_type.data[5],
261 dev->dev_type.data[4],
262 dev->dev_type.data[7],
263 dev->dev_type.data[6],
264 dev->dev_type.data[8],
265 dev->dev_type.data[9],
266 dev->dev_type.data[10],
267 dev->dev_type.data[11],
268 dev->dev_type.data[12],
269 dev->dev_type.data[13],
270 dev->dev_type.data[14],
271 dev->dev_type.data[15]);
272
273 if (ret)
274 return ret;
275
276 ret = add_uevent_var(env, "VMBUS_DEVICE_DEVICE_GUID={"
277 "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
278 "%02x%02x%02x%02x%02x%02x%02x%02x}",
279 dev->dev_instance.data[3],
280 dev->dev_instance.data[2],
281 dev->dev_instance.data[1],
282 dev->dev_instance.data[0],
283 dev->dev_instance.data[5],
284 dev->dev_instance.data[4],
285 dev->dev_instance.data[7],
286 dev->dev_instance.data[6],
287 dev->dev_instance.data[8],
288 dev->dev_instance.data[9],
289 dev->dev_instance.data[10],
290 dev->dev_instance.data[11],
291 dev->dev_instance.data[12],
292 dev->dev_instance.data[13],
293 dev->dev_instance.data[14],
294 dev->dev_instance.data[15]);
295 if (ret)
296 return ret;
297
298 return 0;
299}
300
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700301
302/*
303 * vmbus_match - Attempt to match the specified device to the specified driver
304 */
305static int vmbus_match(struct device *device, struct device_driver *driver)
306{
307 int match = 0;
308 struct hv_driver *drv = drv_to_hv_drv(driver);
309 struct hv_device *device_ctx = device_to_hv_device(device);
310
311 /* We found our driver ? */
312 if (memcmp(&device_ctx->dev_type, &drv->dev_type,
K. Y. Srinivasande632a22011-04-26 09:20:24 -0700313 sizeof(struct hv_guid)) == 0)
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700314 match = 1;
K. Y. Srinivasande632a22011-04-26 09:20:24 -0700315
K. Y. Srinivasanb7fc1472011-03-15 15:03:38 -0700316 return match;
317}
318
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700319/*
320 * vmbus_probe - Add the new vmbus's child device
321 */
322static int vmbus_probe(struct device *child_device)
323{
324 int ret = 0;
325 struct hv_driver *drv =
326 drv_to_hv_drv(child_device->driver);
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700327
328 /* Let the specific open-source driver handles the probe if it can */
329 if (drv->driver.probe) {
K. Y. Srinivasanb14a7b32011-04-29 13:45:03 -0700330 ret = drv->driver.probe(child_device);
331 if (ret != 0)
Hank Janssen0a466182011-03-29 13:58:47 -0700332 pr_err("probe failed for device %s (%d)\n",
333 dev_name(child_device), ret);
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700334
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700335 } else {
Hank Janssen0a466182011-03-29 13:58:47 -0700336 pr_err("probe not set for driver %s\n",
337 dev_name(child_device));
K. Y. Srinivasanf1f0d672011-03-15 15:03:39 -0700338 ret = -1;
339 }
340 return ret;
341}
342
K. Y. Srinivasanc5dce3d2011-03-15 15:03:40 -0700343/*
344 * vmbus_remove - Remove a vmbus device
345 */
346static int vmbus_remove(struct device *child_device)
347{
348 int ret;
349 struct hv_driver *drv;
350
351
352 if (child_device->driver) {
353 drv = drv_to_hv_drv(child_device->driver);
354
355 /*
356 * Let the specific open-source driver handles the removal if
357 * it can
358 */
359 if (drv->driver.remove) {
360 ret = drv->driver.remove(child_device);
361 } else {
Hank Janssen0a466182011-03-29 13:58:47 -0700362 pr_err("remove not set for driver %s\n",
363 dev_name(child_device));
K. Y. Srinivasanc5dce3d2011-03-15 15:03:40 -0700364 ret = -1;
365 }
366 }
367
368 return 0;
369}
370
K. Y. Srinivasaneb1bb252011-03-15 15:03:41 -0700371
372/*
373 * vmbus_shutdown - Shutdown a vmbus device
374 */
375static void vmbus_shutdown(struct device *child_device)
376{
377 struct hv_driver *drv;
378
379
380 /* The device may not be attached yet */
381 if (!child_device->driver)
382 return;
383
384 drv = drv_to_hv_drv(child_device->driver);
385
386 /* Let the specific open-source driver handles the removal if it can */
387 if (drv->driver.shutdown)
388 drv->driver.shutdown(child_device);
389
390 return;
391}
392
K. Y. Srinivasan086e7a52011-03-15 15:03:42 -0700393
394/*
395 * vmbus_device_release - Final callback release of the vmbus child device
396 */
397static void vmbus_device_release(struct device *device)
398{
399 struct hv_device *device_ctx = device_to_hv_device(device);
400
401 kfree(device_ctx);
402
403}
404
Bill Pemberton454f18a2009-07-27 16:47:24 -0400405/* The one and only one */
K. Y. Srinivasan04677c02011-03-15 15:03:35 -0700406static struct hv_bus hv_bus = {
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700407 .bus.name = "vmbus",
408 .bus.match = vmbus_match,
409 .bus.shutdown = vmbus_shutdown,
410 .bus.remove = vmbus_remove,
411 .bus.probe = vmbus_probe,
412 .bus.uevent = vmbus_uevent,
413 .bus.dev_attrs = vmbus_device_attrs,
Hank Janssen3e7ee492009-07-13 16:02:34 -0700414};
415
Haiyang Zhangadf874c2011-01-26 12:12:09 -0800416static const char *driver_name = "hyperv";
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800417
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800418
Timo Teräsbf6506f2010-12-15 20:48:08 +0200419struct onmessage_work_context {
420 struct work_struct work;
421 struct hv_message msg;
422};
423
424static void vmbus_onmessage_work(struct work_struct *work)
425{
426 struct onmessage_work_context *ctx;
427
428 ctx = container_of(work, struct onmessage_work_context,
429 work);
430 vmbus_onmessage(&ctx->msg);
431 kfree(ctx);
432}
433
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800434/*
435 * vmbus_on_msg_dpc - DPC routine to handle messages from the hypervisior
436 */
K. Y. Srinivasan62c10592011-03-10 14:04:53 -0800437static void vmbus_on_msg_dpc(unsigned long data)
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800438{
439 int cpu = smp_processor_id();
440 void *page_addr = hv_context.synic_message_page[cpu];
441 struct hv_message *msg = (struct hv_message *)page_addr +
442 VMBUS_MESSAGE_SINT;
Timo Teräsbf6506f2010-12-15 20:48:08 +0200443 struct onmessage_work_context *ctx;
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800444
445 while (1) {
446 if (msg->header.message_type == HVMSG_NONE) {
447 /* no msg */
448 break;
449 } else {
Timo Teräsbf6506f2010-12-15 20:48:08 +0200450 ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
451 if (ctx == NULL)
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800452 continue;
Timo Teräsbf6506f2010-12-15 20:48:08 +0200453 INIT_WORK(&ctx->work, vmbus_onmessage_work);
454 memcpy(&ctx->msg, msg, sizeof(*msg));
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800455 queue_work(vmbus_connection.work_queue, &ctx->work);
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800456 }
457
458 msg->header.message_type = HVMSG_NONE;
459
460 /*
461 * Make sure the write to MessageType (ie set to
462 * HVMSG_NONE) happens before we read the
463 * MessagePending and EOMing. Otherwise, the EOMing
464 * will not deliver any more messages since there is
465 * no empty slot
466 */
467 mb();
468
469 if (msg->header.message_flags.msg_pending) {
470 /*
471 * This will cause message queue rescan to
472 * possibly deliver another msg from the
473 * hypervisor
474 */
475 wrmsrl(HV_X64_MSR_EOM, 0);
476 }
477 }
478}
479
480/*
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800481 * vmbus_on_isr - ISR routine
482 */
K. Y. Srinivasan480ae582011-03-10 14:06:06 -0800483static int vmbus_on_isr(void)
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800484{
485 int ret = 0;
486 int cpu = smp_processor_id();
487 void *page_addr;
488 struct hv_message *msg;
489 union hv_synic_event_flags *event;
490
491 page_addr = hv_context.synic_message_page[cpu];
492 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
493
494 /* Check if there are actual msgs to be process */
Hank Janssen98e08702011-03-29 13:58:44 -0700495 if (msg->header.message_type != HVMSG_NONE)
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800496 ret |= 0x1;
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800497
498 /* TODO: Check if there are events to be process */
499 page_addr = hv_context.synic_event_page[cpu];
500 event = (union hv_synic_event_flags *)page_addr + VMBUS_MESSAGE_SINT;
501
502 /* Since we are a child, we only need to check bit 0 */
Greg Kroah-Hartman32235b02011-04-13 12:14:05 -0700503 if (sync_test_and_clear_bit(0, (unsigned long *) &event->flags32[0]))
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800504 ret |= 0x2;
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800505
506 return ret;
507}
508
K. Y. Srinivasan793be9c2011-03-15 15:03:43 -0700509
510static irqreturn_t vmbus_isr(int irq, void *dev_id)
511{
512 int ret;
513
514 ret = vmbus_on_isr();
515
516 /* Schedules a dpc if necessary */
517 if (ret > 0) {
518 if (test_bit(0, (unsigned long *)&ret))
K. Y. Srinivasan59c0e4f2011-04-29 13:45:06 -0700519 tasklet_schedule(&msg_dpc);
K. Y. Srinivasan793be9c2011-03-15 15:03:43 -0700520
521 if (test_bit(1, (unsigned long *)&ret))
522 tasklet_schedule(&hv_bus.event_dpc);
523
524 return IRQ_HANDLED;
525 } else {
526 return IRQ_NONE;
527 }
528}
529
Hank Janssen3e189512010-03-04 22:11:00 +0000530/*
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700531 * vmbus_bus_init -Main vmbus driver initialization routine.
532 *
533 * Here, we
Lars Lindley0686e4f2010-03-11 23:51:23 +0100534 * - initialize the vmbus driver context
Lars Lindley0686e4f2010-03-11 23:51:23 +0100535 * - invoke the vmbus hv main init routine
536 * - get the irq resource
Lars Lindley0686e4f2010-03-11 23:51:23 +0100537 * - retrieve the channel offers
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700538 */
K. Y. Srinivasan52e5c1c2011-03-15 15:03:34 -0700539static int vmbus_bus_init(struct pci_dev *pdev)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700540{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700541 int ret;
542 unsigned int vector;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700543
Greg Kroah-Hartman6d26e38fa22010-12-02 12:08:08 -0800544 /* Hypervisor initialization...setup hypercall page..etc */
545 ret = hv_init();
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700546 if (ret != 0) {
Hank Janssen0a466182011-03-29 13:58:47 -0700547 pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700548 goto cleanup;
549 }
550
K. Y. Srinivasan04677c02011-03-15 15:03:35 -0700551 hv_bus.bus.name = driver_name;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700552
Bill Pemberton454f18a2009-07-27 16:47:24 -0400553 /* Initialize the bus context */
K. Y. Srinivasan59c0e4f2011-04-29 13:45:06 -0700554 tasklet_init(&msg_dpc, vmbus_on_msg_dpc, 0);
Olaf Hering8fea1912011-04-16 18:50:41 +0200555 tasklet_init(&hv_bus.event_dpc, vmbus_on_event, 0);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700556
K. Y. Srinivasana6e4d8e2011-03-10 14:03:51 -0800557 /* Now, register the bus with LDM */
K. Y. Srinivasan04677c02011-03-15 15:03:35 -0700558 ret = bus_register(&hv_bus.bus);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700559 if (ret) {
Bill Pembertonc19fbca2009-07-27 16:47:35 -0400560 ret = -1;
561 goto cleanup;
562 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700563
Bill Pemberton454f18a2009-07-27 16:47:24 -0400564 /* Get the interrupt resource */
K. Y. Srinivasan52e5c1c2011-03-15 15:03:34 -0700565 ret = request_irq(pdev->irq, vmbus_isr,
566 IRQF_SHARED | IRQF_SAMPLE_RANDOM,
567 driver_name, pdev);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700568
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700569 if (ret != 0) {
Hank Janssen0a466182011-03-29 13:58:47 -0700570 pr_err("Unable to request IRQ %d\n",
K. Y. Srinivasan52e5c1c2011-03-15 15:03:34 -0700571 pdev->irq);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700572
K. Y. Srinivasan04677c02011-03-15 15:03:35 -0700573 bus_unregister(&hv_bus.bus);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700574
575 ret = -1;
576 goto cleanup;
577 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700578
K. Y. Srinivasan52e5c1c2011-03-15 15:03:34 -0700579 vector = IRQ0_VECTOR + pdev->irq;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700580
K. Y. Srinivasan800b6902011-03-15 15:03:33 -0700581 /*
582 * Notify the hypervisor of our irq and
583 * connect to the host.
584 */
585 on_each_cpu(hv_synic_init, (void *)&vector, 1);
586 ret = vmbus_connect();
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700587 if (ret) {
K. Y. Srinivasan52e5c1c2011-03-15 15:03:34 -0700588 free_irq(pdev->irq, pdev);
K. Y. Srinivasan04677c02011-03-15 15:03:35 -0700589 bus_unregister(&hv_bus.bus);
Bill Pemberton5d48a1c2009-07-27 16:47:36 -0400590 goto cleanup;
591 }
592
K. Y. Srinivasan800b6902011-03-15 15:03:33 -0700593
Greg Kroah-Hartman2d6e8822010-12-02 08:50:58 -0800594 vmbus_request_offers();
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +0000595 wait_for_completion(&hv_channel_ready);
596
Hank Janssen3e7ee492009-07-13 16:02:34 -0700597cleanup:
Hank Janssen3e7ee492009-07-13 16:02:34 -0700598 return ret;
599}
600
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700601/**
Hank Janssen3e189512010-03-04 22:11:00 +0000602 * vmbus_child_driver_register() - Register a vmbus's child driver
K. Y. Srinivasanc643269d2011-03-07 13:23:43 -0800603 * @drv: Pointer to driver structure you want to register
Hank Janssen3e189512010-03-04 22:11:00 +0000604 *
Hank Janssen3e189512010-03-04 22:11:00 +0000605 *
606 * Registers the given driver with Linux through the 'driver_register()' call
607 * And sets up the hyper-v vmbus handling for this driver.
608 * It will return the state of the 'driver_register()' call.
609 *
610 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700611 */
K. Y. Srinivasanc643269d2011-03-07 13:23:43 -0800612int vmbus_child_driver_register(struct device_driver *drv)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700613{
Bill Pemberton5d48a1c2009-07-27 16:47:36 -0400614 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700615
Hank Janssen0a466182011-03-29 13:58:47 -0700616 pr_info("child driver registering - name %s\n", drv->name);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700617
Bill Pemberton454f18a2009-07-27 16:47:24 -0400618 /* The child driver on this vmbus */
K. Y. Srinivasan04677c02011-03-15 15:03:35 -0700619 drv->bus = &hv_bus.bus;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700620
K. Y. Srinivasanc643269d2011-03-07 13:23:43 -0800621 ret = driver_register(drv);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700622
Greg Kroah-Hartman2d6e8822010-12-02 08:50:58 -0800623 vmbus_request_offers();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700624
Bill Pemberton5d48a1c2009-07-27 16:47:36 -0400625 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700626}
Hank Janssen3e7ee492009-07-13 16:02:34 -0700627EXPORT_SYMBOL(vmbus_child_driver_register);
628
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700629/**
Hank Janssen3e189512010-03-04 22:11:00 +0000630 * vmbus_child_driver_unregister() - Unregister a vmbus's child driver
K. Y. Srinivasan06de23f2011-03-07 13:24:23 -0800631 * @drv: Pointer to driver structure you want to un-register
Hank Janssen3e189512010-03-04 22:11:00 +0000632 *
Hank Janssen3e189512010-03-04 22:11:00 +0000633 *
634 * Un-register the given driver with Linux through the 'driver_unregister()'
635 * call. And ungegisters the driver from the Hyper-V vmbus handler.
636 *
637 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700638 */
K. Y. Srinivasan06de23f2011-03-07 13:24:23 -0800639void vmbus_child_driver_unregister(struct device_driver *drv)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700640{
Hank Janssen0a466182011-03-29 13:58:47 -0700641 pr_info("child driver unregistering - name %s\n", drv->name);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700642
K. Y. Srinivasan06de23f2011-03-07 13:24:23 -0800643 driver_unregister(drv);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700644
K. Y. Srinivasan06de23f2011-03-07 13:24:23 -0800645 drv->bus = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700646}
Hank Janssen3e7ee492009-07-13 16:02:34 -0700647EXPORT_SYMBOL(vmbus_child_driver_unregister);
648
Hank Janssen3e189512010-03-04 22:11:00 +0000649/*
Hank Janssen3e189512010-03-04 22:11:00 +0000650 * vmbus_child_device_create - Creates and registers a new child device
651 * on the vmbus.
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700652 */
Greg Kroah-Hartman89733aa2010-12-02 08:22:41 -0800653struct hv_device *vmbus_child_device_create(struct hv_guid *type,
654 struct hv_guid *instance,
655 struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700656{
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200657 struct hv_device *child_device_obj;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700658
Bill Pemberton454f18a2009-07-27 16:47:24 -0400659 /* Allocate the new child device */
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800660 child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
661 if (!child_device_obj) {
Hank Janssen0a466182011-03-29 13:58:47 -0700662 pr_err("Unable to allocate device object for child device\n");
Hank Janssen3e7ee492009-07-13 16:02:34 -0700663 return NULL;
664 }
665
Greg Kroah-Hartmancae5b842010-10-21 09:05:27 -0700666 child_device_obj->channel = channel;
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800667 memcpy(&child_device_obj->dev_type, type, sizeof(struct hv_guid));
668 memcpy(&child_device_obj->dev_instance, instance,
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700669 sizeof(struct hv_guid));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700670
Hank Janssen3e7ee492009-07-13 16:02:34 -0700671
Hank Janssen3e7ee492009-07-13 16:02:34 -0700672 return child_device_obj;
673}
674
Hank Janssen3e189512010-03-04 22:11:00 +0000675/*
K. Y. Srinivasane13a0b52011-03-10 14:07:44 -0800676 * vmbus_child_device_register - Register the child device
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700677 */
K. Y. Srinivasan3ca07cb2011-03-10 14:07:21 -0800678int vmbus_child_device_register(struct hv_device *child_device_obj)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700679{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700680 int ret = 0;
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800681
Bill Pembertonf4888412009-07-29 17:00:12 -0400682 static atomic_t device_num = ATOMIC_INIT(0);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700683
Haiyang Zhang1bb40a22009-10-23 18:14:24 +0000684 /* Set the device name. Otherwise, device_register() will fail. */
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800685 dev_set_name(&child_device_obj->device, "vmbus_0_%d",
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700686 atomic_inc_return(&device_num));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700687
Bill Pemberton454f18a2009-07-27 16:47:24 -0400688 /* The new device belongs to this bus */
K. Y. Srinivasan04677c02011-03-15 15:03:35 -0700689 child_device_obj->device.bus = &hv_bus.bus; /* device->dev.bus; */
K. Y. Srinivasan800b6902011-03-15 15:03:33 -0700690 child_device_obj->device.parent = &hv_pci_dev->dev;
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800691 child_device_obj->device.release = vmbus_device_release;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700692
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700693 /*
694 * Register with the LDM. This will kick off the driver/device
695 * binding...which will eventually call vmbus_match() and vmbus_probe()
696 */
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800697 ret = device_register(&child_device_obj->device);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700698
Hank Janssen3e7ee492009-07-13 16:02:34 -0700699 if (ret)
Hank Janssen0a466182011-03-29 13:58:47 -0700700 pr_err("Unable to register child device\n");
Hank Janssen3e7ee492009-07-13 16:02:34 -0700701 else
Hank Janssen0a466182011-03-29 13:58:47 -0700702 pr_info("child device %s registered\n",
703 dev_name(&child_device_obj->device));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700704
Hank Janssen3e7ee492009-07-13 16:02:34 -0700705 return ret;
706}
707
Hank Janssen3e189512010-03-04 22:11:00 +0000708/*
709 * vmbus_child_device_unregister - Remove the specified child device
710 * from the vmbus.
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700711 */
Greg Kroah-Hartman9d8bd712010-12-02 08:34:45 -0800712void vmbus_child_device_unregister(struct hv_device *device_obj)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700713{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700714 /*
715 * Kick off the process of unregistering the device.
716 * This will call vmbus_remove() and eventually vmbus_device_release()
717 */
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800718 device_unregister(&device_obj->device);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700719
Hank Janssen0a466182011-03-29 13:58:47 -0700720 pr_info("child device %s unregistered\n",
721 dev_name(&device_obj->device));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700722}
723
Hank Janssen3e7ee492009-07-13 16:02:34 -0700724
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -0700725static int __devinit hv_pci_probe(struct pci_dev *pdev,
726 const struct pci_device_id *ent)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700727{
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -0700728 hv_pci_dev = pdev;
Greg Kroah-Hartmanc22090f2010-02-25 16:43:15 -0800729
K. Y. Srinivasan71a66552011-04-29 13:45:04 -0700730 pci_probe_error = pci_enable_device(pdev);
731 if (pci_probe_error)
732 goto probe_cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700733
K. Y. Srinivasan71a66552011-04-29 13:45:04 -0700734 pci_probe_error = vmbus_bus_init(pdev);
735 if (pci_probe_error)
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -0700736 pci_disable_device(pdev);
737
K. Y. Srinivasan71a66552011-04-29 13:45:04 -0700738probe_cleanup:
739 complete(&probe_event);
740 return pci_probe_error;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700741}
742
Greg Kroah-Hartman9a775db2010-02-25 16:42:10 -0800743/*
744 * We use a PCI table to determine if we should autoload this driver This is
745 * needed by distro tools to determine if the hyperv drivers should be
746 * installed and/or configured. We don't do anything else with the table, but
747 * it needs to be present.
Greg Kroah-Hartman9a775db2010-02-25 16:42:10 -0800748 */
Tobias Klauser15f0beb2010-05-20 10:39:38 +0200749static const struct pci_device_id microsoft_hv_pci_table[] = {
Greg Kroah-Hartman9a775db2010-02-25 16:42:10 -0800750 { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
751 { 0 }
752};
753MODULE_DEVICE_TABLE(pci, microsoft_hv_pci_table);
754
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -0700755static struct pci_driver hv_bus_driver = {
756 .name = "hv_bus",
757 .probe = hv_pci_probe,
758 .id_table = microsoft_hv_pci_table,
759};
760
761static int __init hv_pci_init(void)
762{
K. Y. Srinivasan71a66552011-04-29 13:45:04 -0700763 int ret;
764 init_completion(&probe_event);
765 ret = pci_register_driver(&hv_bus_driver);
766 if (ret)
767 return ret;
768 /*
769 * All the vmbus initialization occurs within the
770 * hv_pci_probe() function. Wait for hv_pci_probe()
771 * to complete.
772 */
773 wait_for_completion(&probe_event);
774
775 if (pci_probe_error)
776 pci_unregister_driver(&hv_bus_driver);
777 return pci_probe_error;
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -0700778}
779
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -0700780
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700781MODULE_LICENSE("GPL");
Hank Janssen26c14cc2010-02-11 23:02:42 +0000782MODULE_VERSION(HV_DRV_VERSION);
Olaf Hering13399cb2011-04-16 18:50:42 +0200783module_param(vmbus_loglevel, int, S_IRUGO|S_IWUSR);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700784
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -0700785module_init(hv_pci_init);