Commit | Line | Data |
---|---|---|
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 | */ |
0a46618d HJ |
23 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
24 | ||
3e7ee490 HJ |
25 | #include <linux/init.h> |
26 | #include <linux/module.h> | |
27 | #include <linux/device.h> | |
28 | #include <linux/irq.h> | |
29 | #include <linux/interrupt.h> | |
30 | #include <linux/sysctl.h> | |
9a775dbd | 31 | #include <linux/pci.h> |
c22090fa | 32 | #include <linux/dmi.h> |
5a0e3ad6 | 33 | #include <linux/slab.h> |
8b5d6d3b | 34 | #include <linux/completion.h> |
2d82f6c7 | 35 | #include "version_info.h" |
e3fe0bb6 | 36 | #include "hv_api.h" |
645954c5 | 37 | #include "logging.h" |
870cde80 | 38 | #include "vmbus.h" |
150b19d4 | 39 | #include "channel.h" |
36199a99 | 40 | #include "vmbus_private.h" |
3e7ee490 | 41 | |
3e7ee490 | 42 | |
ef58f5d1 | 43 | static struct pci_dev *hv_pci_dev; |
1168ac22 | 44 | |
59c0e4f0 | 45 | static struct tasklet_struct msg_dpc; |
66d9229c | 46 | static struct tasklet_struct event_dpc; |
59c0e4f0 | 47 | |
454f18a9 | 48 | /* Main vmbus driver data structure */ |
04677c08 | 49 | struct hv_bus { |
90c9960e | 50 | struct bus_type bus; |
3e7ee490 HJ |
51 | }; |
52 | ||
98db4335 S |
53 | unsigned int vmbus_loglevel = (ALL_MODULES << 16 | INFO_LVL); |
54 | EXPORT_SYMBOL(vmbus_loglevel); | |
55 | /* (ALL_MODULES << 16 | DEBUG_LVL_ENTEREXIT); */ | |
56 | /* (((VMBUS | VMBUS_DRV)<<16) | DEBUG_LVL_ENTEREXIT); */ | |
57 | ||
71a6655d S |
58 | static int pci_probe_error; |
59 | static struct completion probe_event; | |
98db4335 S |
60 | |
61 | static 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; | |
3e7ee490 | 95 | |
98db4335 S |
96 | info->outbound.int_mask = |
97 | debug_info.outbound.current_interrupt_mask; | |
98 | info->outbound.read_idx = debug_info.outbound.current_read_index; | |
99 | info->outbound.write_idx = debug_info.outbound.current_write_index; | |
100 | info->outbound.bytes_avail_toread = | |
101 | debug_info.outbound.bytes_avail_toread; | |
102 | info->outbound.bytes_avail_towrite = | |
103 | debug_info.outbound.bytes_avail_towrite; | |
104 | } | |
3e7ee490 | 105 | |
98db4335 S |
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 | */ | |
90c9960e GKH |
112 | static ssize_t vmbus_show_device_attr(struct device *dev, |
113 | struct device_attribute *dev_attr, | |
98db4335 S |
114 | char *buf) |
115 | { | |
116 | struct hv_device *device_ctx = device_to_hv_device(dev); | |
117 | struct hv_device_info device_info; | |
3e7ee490 | 118 | |
98db4335 | 119 | memset(&device_info, 0, sizeof(struct hv_device_info)); |
3e7ee490 | 120 | |
98db4335 | 121 | get_channel_info(device_ctx, &device_info); |
3e7ee490 | 122 | |
98db4335 S |
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 | } | |
3e7ee490 | 209 | |
454f18a9 | 210 | /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */ |
3e7ee490 HJ |
211 | static 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 | }; | |
3e7ee490 | 239 | |
793be9c7 | 240 | |
adde2487 S |
241 | /* |
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 | */ | |
248 | static 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 | ||
adde2487 S |
253 | 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 | ||
b7fc147b S |
301 | |
302 | /* | |
303 | * vmbus_match - Attempt to match the specified device to the specified driver | |
304 | */ | |
305 | static 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, | |
de632a2b | 313 | sizeof(struct hv_guid)) == 0) |
b7fc147b | 314 | match = 1; |
de632a2b | 315 | |
b7fc147b S |
316 | return match; |
317 | } | |
318 | ||
f1f0d67b S |
319 | /* |
320 | * vmbus_probe - Add the new vmbus's child device | |
321 | */ | |
322 | static 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); | |
f1f0d67b S |
327 | |
328 | /* Let the specific open-source driver handles the probe if it can */ | |
329 | if (drv->driver.probe) { | |
b14a7b30 S |
330 | ret = drv->driver.probe(child_device); |
331 | if (ret != 0) | |
0a46618d HJ |
332 | pr_err("probe failed for device %s (%d)\n", |
333 | dev_name(child_device), ret); | |
f1f0d67b | 334 | |
f1f0d67b | 335 | } else { |
0a46618d HJ |
336 | pr_err("probe not set for driver %s\n", |
337 | dev_name(child_device)); | |
f1f0d67b S |
338 | ret = -1; |
339 | } | |
340 | return ret; | |
341 | } | |
342 | ||
c5dce3db S |
343 | /* |
344 | * vmbus_remove - Remove a vmbus device | |
345 | */ | |
346 | static 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 { | |
0a46618d HJ |
362 | pr_err("remove not set for driver %s\n", |
363 | dev_name(child_device)); | |
c5dce3db S |
364 | ret = -1; |
365 | } | |
366 | } | |
367 | ||
368 | return 0; | |
369 | } | |
370 | ||
eb1bb259 S |
371 | |
372 | /* | |
373 | * vmbus_shutdown - Shutdown a vmbus device | |
374 | */ | |
375 | static 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 | ||
086e7a56 S |
393 | |
394 | /* | |
395 | * vmbus_device_release - Final callback release of the vmbus child device | |
396 | */ | |
397 | static 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 | ||
454f18a9 | 405 | /* The one and only one */ |
04677c08 | 406 | static struct hv_bus hv_bus = { |
90c9960e GKH |
407 | .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, | |
3e7ee490 HJ |
414 | }; |
415 | ||
adf874cb | 416 | static const char *driver_name = "hyperv"; |
36199a99 | 417 | |
36199a99 | 418 | |
bf6506f6 TT |
419 | struct onmessage_work_context { |
420 | struct work_struct work; | |
421 | struct hv_message msg; | |
422 | }; | |
423 | ||
424 | static 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 | ||
36199a99 GKH |
434 | /* |
435 | * vmbus_on_msg_dpc - DPC routine to handle messages from the hypervisior | |
436 | */ | |
62c1059d | 437 | static void vmbus_on_msg_dpc(unsigned long data) |
36199a99 GKH |
438 | { |
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; | |
bf6506f6 | 443 | struct onmessage_work_context *ctx; |
36199a99 GKH |
444 | |
445 | while (1) { | |
446 | if (msg->header.message_type == HVMSG_NONE) { | |
447 | /* no msg */ | |
448 | break; | |
449 | } else { | |
bf6506f6 TT |
450 | ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC); |
451 | if (ctx == NULL) | |
36199a99 | 452 | continue; |
bf6506f6 TT |
453 | INIT_WORK(&ctx->work, vmbus_onmessage_work); |
454 | memcpy(&ctx->msg, msg, sizeof(*msg)); | |
da9fcb72 | 455 | queue_work(vmbus_connection.work_queue, &ctx->work); |
36199a99 GKH |
456 | } |
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 | ||
36199a99 GKH |
480 | /* |
481 | * vmbus_on_isr - ISR routine | |
482 | */ | |
480ae58d | 483 | static int vmbus_on_isr(void) |
36199a99 GKH |
484 | { |
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 */ | |
98e08702 | 495 | if (msg->header.message_type != HVMSG_NONE) |
36199a99 | 496 | ret |= 0x1; |
36199a99 GKH |
497 | |
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 */ | |
32235b07 | 503 | if (sync_test_and_clear_bit(0, (unsigned long *) &event->flags32[0])) |
36199a99 | 504 | ret |= 0x2; |
36199a99 GKH |
505 | |
506 | return ret; | |
507 | } | |
508 | ||
793be9c7 S |
509 | |
510 | static 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)) | |
59c0e4f0 | 519 | tasklet_schedule(&msg_dpc); |
793be9c7 S |
520 | |
521 | if (test_bit(1, (unsigned long *)&ret)) | |
66d9229c | 522 | tasklet_schedule(&event_dpc); |
793be9c7 S |
523 | |
524 | return IRQ_HANDLED; | |
525 | } else { | |
526 | return IRQ_NONE; | |
527 | } | |
528 | } | |
529 | ||
3e189519 | 530 | /* |
90c9960e GKH |
531 | * vmbus_bus_init -Main vmbus driver initialization routine. |
532 | * | |
533 | * Here, we | |
0686e4f4 | 534 | * - initialize the vmbus driver context |
0686e4f4 LL |
535 | * - invoke the vmbus hv main init routine |
536 | * - get the irq resource | |
0686e4f4 | 537 | * - retrieve the channel offers |
90c9960e | 538 | */ |
52e5c1ce | 539 | static int vmbus_bus_init(struct pci_dev *pdev) |
3e7ee490 | 540 | { |
90c9960e GKH |
541 | int ret; |
542 | unsigned int vector; | |
3e7ee490 | 543 | |
6d26e38f GKH |
544 | /* Hypervisor initialization...setup hypercall page..etc */ |
545 | ret = hv_init(); | |
90c9960e | 546 | if (ret != 0) { |
0a46618d | 547 | pr_err("Unable to initialize the hypervisor - 0x%x\n", ret); |
3e7ee490 HJ |
548 | goto cleanup; |
549 | } | |
550 | ||
04677c08 | 551 | hv_bus.bus.name = driver_name; |
3e7ee490 | 552 | |
454f18a9 | 553 | /* Initialize the bus context */ |
59c0e4f0 | 554 | tasklet_init(&msg_dpc, vmbus_on_msg_dpc, 0); |
66d9229c | 555 | tasklet_init(&event_dpc, vmbus_on_event, 0); |
3e7ee490 | 556 | |
a6e4d8e3 | 557 | /* Now, register the bus with LDM */ |
04677c08 | 558 | ret = bus_register(&hv_bus.bus); |
90c9960e | 559 | if (ret) { |
c19fbca3 BP |
560 | ret = -1; |
561 | goto cleanup; | |
562 | } | |
3e7ee490 | 563 | |
454f18a9 | 564 | /* Get the interrupt resource */ |
52e5c1ce S |
565 | ret = request_irq(pdev->irq, vmbus_isr, |
566 | IRQF_SHARED | IRQF_SAMPLE_RANDOM, | |
567 | driver_name, pdev); | |
3e7ee490 | 568 | |
90c9960e | 569 | if (ret != 0) { |
0a46618d | 570 | pr_err("Unable to request IRQ %d\n", |
52e5c1ce | 571 | pdev->irq); |
3e7ee490 | 572 | |
04677c08 | 573 | bus_unregister(&hv_bus.bus); |
3e7ee490 HJ |
574 | |
575 | ret = -1; | |
576 | goto cleanup; | |
577 | } | |
3e7ee490 | 578 | |
52e5c1ce | 579 | vector = IRQ0_VECTOR + pdev->irq; |
3e7ee490 | 580 | |
800b6902 S |
581 | /* |
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(); | |
90c9960e | 587 | if (ret) { |
52e5c1ce | 588 | free_irq(pdev->irq, pdev); |
04677c08 | 589 | bus_unregister(&hv_bus.bus); |
5d48a1c2 BP |
590 | goto cleanup; |
591 | } | |
592 | ||
800b6902 | 593 | |
2d6e882b | 594 | vmbus_request_offers(); |
8b5d6d3b HZ |
595 | wait_for_completion(&hv_channel_ready); |
596 | ||
3e7ee490 | 597 | cleanup: |
3e7ee490 HJ |
598 | return ret; |
599 | } | |
600 | ||
90c9960e | 601 | /** |
3e189519 | 602 | * vmbus_child_driver_register() - Register a vmbus's child driver |
c643269d | 603 | * @drv: Pointer to driver structure you want to register |
3e189519 | 604 | * |
3e189519 HJ |
605 | * |
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. | |
90c9960e | 611 | */ |
c643269d | 612 | int vmbus_child_driver_register(struct device_driver *drv) |
3e7ee490 | 613 | { |
5d48a1c2 | 614 | int ret; |
3e7ee490 | 615 | |
0a46618d | 616 | pr_info("child driver registering - name %s\n", drv->name); |
3e7ee490 | 617 | |
454f18a9 | 618 | /* The child driver on this vmbus */ |
04677c08 | 619 | drv->bus = &hv_bus.bus; |
3e7ee490 | 620 | |
c643269d | 621 | ret = driver_register(drv); |
3e7ee490 | 622 | |
2d6e882b | 623 | vmbus_request_offers(); |
3e7ee490 | 624 | |
5d48a1c2 | 625 | return ret; |
3e7ee490 | 626 | } |
3e7ee490 HJ |
627 | EXPORT_SYMBOL(vmbus_child_driver_register); |
628 | ||
90c9960e | 629 | /** |
3e189519 | 630 | * vmbus_child_driver_unregister() - Unregister a vmbus's child driver |
06de23f7 | 631 | * @drv: Pointer to driver structure you want to un-register |
3e189519 | 632 | * |
3e189519 HJ |
633 | * |
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. | |
90c9960e | 638 | */ |
06de23f7 | 639 | void vmbus_child_driver_unregister(struct device_driver *drv) |
3e7ee490 | 640 | { |
0a46618d | 641 | pr_info("child driver unregistering - name %s\n", drv->name); |
3e7ee490 | 642 | |
06de23f7 | 643 | driver_unregister(drv); |
3e7ee490 | 644 | |
06de23f7 | 645 | drv->bus = NULL; |
3e7ee490 | 646 | } |
3e7ee490 HJ |
647 | EXPORT_SYMBOL(vmbus_child_driver_unregister); |
648 | ||
3e189519 HJ |
649 | /* |
650 | * vmbus_child_device_create - Creates and registers a new child device | |
651 | * on the vmbus. | |
90c9960e | 652 | */ |
89733aa9 GKH |
653 | struct hv_device *vmbus_child_device_create(struct hv_guid *type, |
654 | struct hv_guid *instance, | |
655 | struct vmbus_channel *channel) | |
3e7ee490 | 656 | { |
3d3b5518 | 657 | struct hv_device *child_device_obj; |
3e7ee490 | 658 | |
454f18a9 | 659 | /* Allocate the new child device */ |
6bad88da S |
660 | child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL); |
661 | if (!child_device_obj) { | |
0a46618d | 662 | pr_err("Unable to allocate device object for child device\n"); |
3e7ee490 HJ |
663 | return NULL; |
664 | } | |
665 | ||
cae5b843 | 666 | child_device_obj->channel = channel; |
ca623ad3 HZ |
667 | memcpy(&child_device_obj->dev_type, type, sizeof(struct hv_guid)); |
668 | memcpy(&child_device_obj->dev_instance, instance, | |
90c9960e | 669 | sizeof(struct hv_guid)); |
3e7ee490 | 670 | |
3e7ee490 | 671 | |
3e7ee490 HJ |
672 | return child_device_obj; |
673 | } | |
674 | ||
3e189519 | 675 | /* |
e13a0b5a | 676 | * vmbus_child_device_register - Register the child device |
90c9960e | 677 | */ |
3ca07cb0 | 678 | int vmbus_child_device_register(struct hv_device *child_device_obj) |
3e7ee490 | 679 | { |
90c9960e | 680 | int ret = 0; |
6bad88da | 681 | |
f4888417 | 682 | static atomic_t device_num = ATOMIC_INIT(0); |
3e7ee490 | 683 | |
1bb40a25 | 684 | /* Set the device name. Otherwise, device_register() will fail. */ |
6bad88da | 685 | dev_set_name(&child_device_obj->device, "vmbus_0_%d", |
90c9960e | 686 | atomic_inc_return(&device_num)); |
3e7ee490 | 687 | |
454f18a9 | 688 | /* The new device belongs to this bus */ |
04677c08 | 689 | child_device_obj->device.bus = &hv_bus.bus; /* device->dev.bus; */ |
800b6902 | 690 | child_device_obj->device.parent = &hv_pci_dev->dev; |
6bad88da | 691 | child_device_obj->device.release = vmbus_device_release; |
3e7ee490 | 692 | |
90c9960e GKH |
693 | /* |
694 | * Register with the LDM. This will kick off the driver/device | |
695 | * binding...which will eventually call vmbus_match() and vmbus_probe() | |
696 | */ | |
6bad88da | 697 | ret = device_register(&child_device_obj->device); |
3e7ee490 | 698 | |
3e7ee490 | 699 | if (ret) |
0a46618d | 700 | pr_err("Unable to register child device\n"); |
3e7ee490 | 701 | else |
0a46618d HJ |
702 | pr_info("child device %s registered\n", |
703 | dev_name(&child_device_obj->device)); | |
3e7ee490 | 704 | |
3e7ee490 HJ |
705 | return ret; |
706 | } | |
707 | ||
3e189519 HJ |
708 | /* |
709 | * vmbus_child_device_unregister - Remove the specified child device | |
710 | * from the vmbus. | |
90c9960e | 711 | */ |
9d8bd71a | 712 | void vmbus_child_device_unregister(struct hv_device *device_obj) |
3e7ee490 | 713 | { |
90c9960e GKH |
714 | /* |
715 | * Kick off the process of unregistering the device. | |
716 | * This will call vmbus_remove() and eventually vmbus_device_release() | |
717 | */ | |
6bad88da | 718 | device_unregister(&device_obj->device); |
3e7ee490 | 719 | |
0a46618d HJ |
720 | pr_info("child device %s unregistered\n", |
721 | dev_name(&device_obj->device)); | |
3e7ee490 HJ |
722 | } |
723 | ||
3e7ee490 | 724 | |
1168ac22 S |
725 | static int __devinit hv_pci_probe(struct pci_dev *pdev, |
726 | const struct pci_device_id *ent) | |
3e7ee490 | 727 | { |
1168ac22 | 728 | hv_pci_dev = pdev; |
c22090fa | 729 | |
71a6655d S |
730 | pci_probe_error = pci_enable_device(pdev); |
731 | if (pci_probe_error) | |
732 | goto probe_cleanup; | |
3e7ee490 | 733 | |
71a6655d S |
734 | pci_probe_error = vmbus_bus_init(pdev); |
735 | if (pci_probe_error) | |
1168ac22 S |
736 | pci_disable_device(pdev); |
737 | ||
71a6655d S |
738 | probe_cleanup: |
739 | complete(&probe_event); | |
740 | return pci_probe_error; | |
3e7ee490 HJ |
741 | } |
742 | ||
9a775dbd GKH |
743 | /* |
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. | |
9a775dbd | 748 | */ |
15f0beb1 | 749 | static const struct pci_device_id microsoft_hv_pci_table[] = { |
9a775dbd GKH |
750 | { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */ |
751 | { 0 } | |
752 | }; | |
753 | MODULE_DEVICE_TABLE(pci, microsoft_hv_pci_table); | |
754 | ||
1168ac22 S |
755 | static struct pci_driver hv_bus_driver = { |
756 | .name = "hv_bus", | |
757 | .probe = hv_pci_probe, | |
758 | .id_table = microsoft_hv_pci_table, | |
759 | }; | |
760 | ||
761 | static int __init hv_pci_init(void) | |
762 | { | |
71a6655d S |
763 | 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; | |
1168ac22 S |
778 | } |
779 | ||
1168ac22 | 780 | |
90c9960e | 781 | MODULE_LICENSE("GPL"); |
26c14cc1 | 782 | MODULE_VERSION(HV_DRV_VERSION); |
13399cba | 783 | module_param(vmbus_loglevel, int, S_IRUGO|S_IWUSR); |
3e7ee490 | 784 | |
1168ac22 | 785 | module_init(hv_pci_init); |