Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1 | /* |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 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> |
K. Y. Srinivasan | b0069f4 | 2011-04-29 13:45:15 -0700 | [diff] [blame] | 20 | * K. Y. Srinivasan <kys@microsoft.com> |
K. Y. Srinivasan | 52e5c1c | 2011-03-15 15:03:34 -0700 | [diff] [blame] | 21 | * |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 22 | */ |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 23 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 24 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 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> |
Greg Kroah-Hartman | 9a775db | 2010-02-25 16:42:10 -0800 | [diff] [blame] | 31 | #include <linux/pci.h> |
Greg Kroah-Hartman | c22090f | 2010-02-25 16:43:15 -0800 | [diff] [blame] | 32 | #include <linux/dmi.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/slab.h> |
K. Y. Srinivasan | b0069f4 | 2011-04-29 13:45:15 -0700 | [diff] [blame] | 34 | #include <linux/acpi.h> |
| 35 | #include <acpi/acpi_bus.h> |
Haiyang Zhang | 8b5d6d3 | 2010-05-28 23:22:44 +0000 | [diff] [blame] | 36 | #include <linux/completion.h> |
K. Y. Srinivasan | 3f335ea | 2011-05-12 19:34:15 -0700 | [diff] [blame] | 37 | |
| 38 | #include "hyperv.h" |
K. Y. Srinivasan | e3fe0bb | 2011-02-11 10:00:12 -0800 | [diff] [blame] | 39 | #include "hv_api.h" |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 40 | #include "vmbus_private.h" |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 41 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 42 | |
K. Y. Srinivasan | ef58f5d | 2011-04-29 13:45:05 -0700 | [diff] [blame] | 43 | static struct pci_dev *hv_pci_dev; |
K. Y. Srinivasan | 1168ac2 | 2011-03-15 15:03:32 -0700 | [diff] [blame] | 44 | |
K. Y. Srinivasan | 59c0e4f | 2011-04-29 13:45:06 -0700 | [diff] [blame] | 45 | static struct tasklet_struct msg_dpc; |
K. Y. Srinivasan | 66d9229 | 2011-04-29 13:45:07 -0700 | [diff] [blame] | 46 | static struct tasklet_struct event_dpc; |
K. Y. Srinivasan | 59c0e4f | 2011-04-29 13:45:06 -0700 | [diff] [blame] | 47 | |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 48 | unsigned int vmbus_loglevel = (ALL_MODULES << 16 | INFO_LVL); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 49 | EXPORT_SYMBOL(vmbus_loglevel); |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 50 | /* (ALL_MODULES << 16 | DEBUG_LVL_ENTEREXIT); */ |
| 51 | /* (((VMBUS | VMBUS_DRV)<<16) | DEBUG_LVL_ENTEREXIT); */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 52 | |
K. Y. Srinivasan | 71a6655 | 2011-04-29 13:45:04 -0700 | [diff] [blame] | 53 | static int pci_probe_error; |
| 54 | static struct completion probe_event; |
K. Y. Srinivasan | b0069f4 | 2011-04-29 13:45:15 -0700 | [diff] [blame] | 55 | static int irq; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 56 | |
K. Y. Srinivasan | 98db433 | 2011-03-15 15:03:44 -0700 | [diff] [blame] | 57 | static void get_channel_info(struct hv_device *device, |
| 58 | struct hv_device_info *info) |
| 59 | { |
| 60 | struct vmbus_channel_debug_info debug_info; |
| 61 | |
| 62 | if (!device->channel) |
| 63 | return; |
| 64 | |
| 65 | vmbus_get_debug_info(device->channel, &debug_info); |
| 66 | |
| 67 | info->chn_id = debug_info.relid; |
| 68 | info->chn_state = debug_info.state; |
| 69 | memcpy(&info->chn_type, &debug_info.interfacetype, |
| 70 | sizeof(struct hv_guid)); |
| 71 | memcpy(&info->chn_instance, &debug_info.interface_instance, |
| 72 | sizeof(struct hv_guid)); |
| 73 | |
| 74 | info->monitor_id = debug_info.monitorid; |
| 75 | |
| 76 | info->server_monitor_pending = debug_info.servermonitor_pending; |
| 77 | info->server_monitor_latency = debug_info.servermonitor_latency; |
| 78 | info->server_monitor_conn_id = debug_info.servermonitor_connectionid; |
| 79 | |
| 80 | info->client_monitor_pending = debug_info.clientmonitor_pending; |
| 81 | info->client_monitor_latency = debug_info.clientmonitor_latency; |
| 82 | info->client_monitor_conn_id = debug_info.clientmonitor_connectionid; |
| 83 | |
| 84 | info->inbound.int_mask = debug_info.inbound.current_interrupt_mask; |
| 85 | info->inbound.read_idx = debug_info.inbound.current_read_index; |
| 86 | info->inbound.write_idx = debug_info.inbound.current_write_index; |
| 87 | info->inbound.bytes_avail_toread = |
| 88 | debug_info.inbound.bytes_avail_toread; |
| 89 | info->inbound.bytes_avail_towrite = |
| 90 | debug_info.inbound.bytes_avail_towrite; |
| 91 | |
| 92 | info->outbound.int_mask = |
| 93 | debug_info.outbound.current_interrupt_mask; |
| 94 | info->outbound.read_idx = debug_info.outbound.current_read_index; |
| 95 | info->outbound.write_idx = debug_info.outbound.current_write_index; |
| 96 | info->outbound.bytes_avail_toread = |
| 97 | debug_info.outbound.bytes_avail_toread; |
| 98 | info->outbound.bytes_avail_towrite = |
| 99 | debug_info.outbound.bytes_avail_towrite; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * vmbus_show_device_attr - Show the device attribute in sysfs. |
| 104 | * |
| 105 | * This is invoked when user does a |
| 106 | * "cat /sys/bus/vmbus/devices/<busdevice>/<attr name>" |
| 107 | */ |
| 108 | static ssize_t vmbus_show_device_attr(struct device *dev, |
| 109 | struct device_attribute *dev_attr, |
| 110 | char *buf) |
| 111 | { |
| 112 | struct hv_device *device_ctx = device_to_hv_device(dev); |
| 113 | struct hv_device_info device_info; |
| 114 | |
| 115 | memset(&device_info, 0, sizeof(struct hv_device_info)); |
| 116 | |
| 117 | get_channel_info(device_ctx, &device_info); |
| 118 | |
| 119 | if (!strcmp(dev_attr->attr.name, "class_id")) { |
| 120 | return sprintf(buf, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-" |
| 121 | "%02x%02x%02x%02x%02x%02x%02x%02x}\n", |
| 122 | device_info.chn_type.data[3], |
| 123 | device_info.chn_type.data[2], |
| 124 | device_info.chn_type.data[1], |
| 125 | device_info.chn_type.data[0], |
| 126 | device_info.chn_type.data[5], |
| 127 | device_info.chn_type.data[4], |
| 128 | device_info.chn_type.data[7], |
| 129 | device_info.chn_type.data[6], |
| 130 | device_info.chn_type.data[8], |
| 131 | device_info.chn_type.data[9], |
| 132 | device_info.chn_type.data[10], |
| 133 | device_info.chn_type.data[11], |
| 134 | device_info.chn_type.data[12], |
| 135 | device_info.chn_type.data[13], |
| 136 | device_info.chn_type.data[14], |
| 137 | device_info.chn_type.data[15]); |
| 138 | } else if (!strcmp(dev_attr->attr.name, "device_id")) { |
| 139 | return sprintf(buf, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-" |
| 140 | "%02x%02x%02x%02x%02x%02x%02x%02x}\n", |
| 141 | device_info.chn_instance.data[3], |
| 142 | device_info.chn_instance.data[2], |
| 143 | device_info.chn_instance.data[1], |
| 144 | device_info.chn_instance.data[0], |
| 145 | device_info.chn_instance.data[5], |
| 146 | device_info.chn_instance.data[4], |
| 147 | device_info.chn_instance.data[7], |
| 148 | device_info.chn_instance.data[6], |
| 149 | device_info.chn_instance.data[8], |
| 150 | device_info.chn_instance.data[9], |
| 151 | device_info.chn_instance.data[10], |
| 152 | device_info.chn_instance.data[11], |
| 153 | device_info.chn_instance.data[12], |
| 154 | device_info.chn_instance.data[13], |
| 155 | device_info.chn_instance.data[14], |
| 156 | device_info.chn_instance.data[15]); |
| 157 | } else if (!strcmp(dev_attr->attr.name, "state")) { |
| 158 | return sprintf(buf, "%d\n", device_info.chn_state); |
| 159 | } else if (!strcmp(dev_attr->attr.name, "id")) { |
| 160 | return sprintf(buf, "%d\n", device_info.chn_id); |
| 161 | } else if (!strcmp(dev_attr->attr.name, "out_intr_mask")) { |
| 162 | return sprintf(buf, "%d\n", device_info.outbound.int_mask); |
| 163 | } else if (!strcmp(dev_attr->attr.name, "out_read_index")) { |
| 164 | return sprintf(buf, "%d\n", device_info.outbound.read_idx); |
| 165 | } else if (!strcmp(dev_attr->attr.name, "out_write_index")) { |
| 166 | return sprintf(buf, "%d\n", device_info.outbound.write_idx); |
| 167 | } else if (!strcmp(dev_attr->attr.name, "out_read_bytes_avail")) { |
| 168 | return sprintf(buf, "%d\n", |
| 169 | device_info.outbound.bytes_avail_toread); |
| 170 | } else if (!strcmp(dev_attr->attr.name, "out_write_bytes_avail")) { |
| 171 | return sprintf(buf, "%d\n", |
| 172 | device_info.outbound.bytes_avail_towrite); |
| 173 | } else if (!strcmp(dev_attr->attr.name, "in_intr_mask")) { |
| 174 | return sprintf(buf, "%d\n", device_info.inbound.int_mask); |
| 175 | } else if (!strcmp(dev_attr->attr.name, "in_read_index")) { |
| 176 | return sprintf(buf, "%d\n", device_info.inbound.read_idx); |
| 177 | } else if (!strcmp(dev_attr->attr.name, "in_write_index")) { |
| 178 | return sprintf(buf, "%d\n", device_info.inbound.write_idx); |
| 179 | } else if (!strcmp(dev_attr->attr.name, "in_read_bytes_avail")) { |
| 180 | return sprintf(buf, "%d\n", |
| 181 | device_info.inbound.bytes_avail_toread); |
| 182 | } else if (!strcmp(dev_attr->attr.name, "in_write_bytes_avail")) { |
| 183 | return sprintf(buf, "%d\n", |
| 184 | device_info.inbound.bytes_avail_towrite); |
| 185 | } else if (!strcmp(dev_attr->attr.name, "monitor_id")) { |
| 186 | return sprintf(buf, "%d\n", device_info.monitor_id); |
| 187 | } else if (!strcmp(dev_attr->attr.name, "server_monitor_pending")) { |
| 188 | return sprintf(buf, "%d\n", device_info.server_monitor_pending); |
| 189 | } else if (!strcmp(dev_attr->attr.name, "server_monitor_latency")) { |
| 190 | return sprintf(buf, "%d\n", device_info.server_monitor_latency); |
| 191 | } else if (!strcmp(dev_attr->attr.name, "server_monitor_conn_id")) { |
| 192 | return sprintf(buf, "%d\n", |
| 193 | device_info.server_monitor_conn_id); |
| 194 | } else if (!strcmp(dev_attr->attr.name, "client_monitor_pending")) { |
| 195 | return sprintf(buf, "%d\n", device_info.client_monitor_pending); |
| 196 | } else if (!strcmp(dev_attr->attr.name, "client_monitor_latency")) { |
| 197 | return sprintf(buf, "%d\n", device_info.client_monitor_latency); |
| 198 | } else if (!strcmp(dev_attr->attr.name, "client_monitor_conn_id")) { |
| 199 | return sprintf(buf, "%d\n", |
| 200 | device_info.client_monitor_conn_id); |
| 201 | } else { |
| 202 | return 0; |
| 203 | } |
| 204 | } |
| 205 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 206 | /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 207 | static struct device_attribute vmbus_device_attrs[] = { |
| 208 | __ATTR(id, S_IRUGO, vmbus_show_device_attr, NULL), |
| 209 | __ATTR(state, S_IRUGO, vmbus_show_device_attr, NULL), |
| 210 | __ATTR(class_id, S_IRUGO, vmbus_show_device_attr, NULL), |
| 211 | __ATTR(device_id, S_IRUGO, vmbus_show_device_attr, NULL), |
| 212 | __ATTR(monitor_id, S_IRUGO, vmbus_show_device_attr, NULL), |
| 213 | |
| 214 | __ATTR(server_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL), |
| 215 | __ATTR(server_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL), |
| 216 | __ATTR(server_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL), |
| 217 | |
| 218 | __ATTR(client_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL), |
| 219 | __ATTR(client_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL), |
| 220 | __ATTR(client_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL), |
| 221 | |
| 222 | __ATTR(out_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL), |
| 223 | __ATTR(out_read_index, S_IRUGO, vmbus_show_device_attr, NULL), |
| 224 | __ATTR(out_write_index, S_IRUGO, vmbus_show_device_attr, NULL), |
| 225 | __ATTR(out_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL), |
| 226 | __ATTR(out_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL), |
| 227 | |
| 228 | __ATTR(in_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL), |
| 229 | __ATTR(in_read_index, S_IRUGO, vmbus_show_device_attr, NULL), |
| 230 | __ATTR(in_write_index, S_IRUGO, vmbus_show_device_attr, NULL), |
| 231 | __ATTR(in_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL), |
| 232 | __ATTR(in_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL), |
| 233 | __ATTR_NULL |
| 234 | }; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 235 | |
K. Y. Srinivasan | 793be9c | 2011-03-15 15:03:43 -0700 | [diff] [blame] | 236 | |
K. Y. Srinivasan | adde248 | 2011-03-15 15:03:37 -0700 | [diff] [blame] | 237 | /* |
| 238 | * vmbus_uevent - add uevent for our device |
| 239 | * |
| 240 | * This routine is invoked when a device is added or removed on the vmbus to |
| 241 | * generate a uevent to udev in the userspace. The udev will then look at its |
| 242 | * rule and the uevent generated here to load the appropriate driver |
| 243 | */ |
| 244 | static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env) |
| 245 | { |
| 246 | struct hv_device *dev = device_to_hv_device(device); |
| 247 | int ret; |
| 248 | |
K. Y. Srinivasan | adde248 | 2011-03-15 15:03:37 -0700 | [diff] [blame] | 249 | ret = add_uevent_var(env, "VMBUS_DEVICE_CLASS_GUID={" |
| 250 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-" |
| 251 | "%02x%02x%02x%02x%02x%02x%02x%02x}", |
| 252 | dev->dev_type.data[3], |
| 253 | dev->dev_type.data[2], |
| 254 | dev->dev_type.data[1], |
| 255 | dev->dev_type.data[0], |
| 256 | dev->dev_type.data[5], |
| 257 | dev->dev_type.data[4], |
| 258 | dev->dev_type.data[7], |
| 259 | dev->dev_type.data[6], |
| 260 | dev->dev_type.data[8], |
| 261 | dev->dev_type.data[9], |
| 262 | dev->dev_type.data[10], |
| 263 | dev->dev_type.data[11], |
| 264 | dev->dev_type.data[12], |
| 265 | dev->dev_type.data[13], |
| 266 | dev->dev_type.data[14], |
| 267 | dev->dev_type.data[15]); |
| 268 | |
| 269 | if (ret) |
| 270 | return ret; |
| 271 | |
| 272 | ret = add_uevent_var(env, "VMBUS_DEVICE_DEVICE_GUID={" |
| 273 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-" |
| 274 | "%02x%02x%02x%02x%02x%02x%02x%02x}", |
| 275 | dev->dev_instance.data[3], |
| 276 | dev->dev_instance.data[2], |
| 277 | dev->dev_instance.data[1], |
| 278 | dev->dev_instance.data[0], |
| 279 | dev->dev_instance.data[5], |
| 280 | dev->dev_instance.data[4], |
| 281 | dev->dev_instance.data[7], |
| 282 | dev->dev_instance.data[6], |
| 283 | dev->dev_instance.data[8], |
| 284 | dev->dev_instance.data[9], |
| 285 | dev->dev_instance.data[10], |
| 286 | dev->dev_instance.data[11], |
| 287 | dev->dev_instance.data[12], |
| 288 | dev->dev_instance.data[13], |
| 289 | dev->dev_instance.data[14], |
| 290 | dev->dev_instance.data[15]); |
| 291 | if (ret) |
| 292 | return ret; |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
K. Y. Srinivasan | b7fc147 | 2011-03-15 15:03:38 -0700 | [diff] [blame] | 297 | |
| 298 | /* |
| 299 | * vmbus_match - Attempt to match the specified device to the specified driver |
| 300 | */ |
| 301 | static int vmbus_match(struct device *device, struct device_driver *driver) |
| 302 | { |
| 303 | int match = 0; |
| 304 | struct hv_driver *drv = drv_to_hv_drv(driver); |
| 305 | struct hv_device *device_ctx = device_to_hv_device(device); |
| 306 | |
| 307 | /* We found our driver ? */ |
| 308 | if (memcmp(&device_ctx->dev_type, &drv->dev_type, |
K. Y. Srinivasan | de632a2 | 2011-04-26 09:20:24 -0700 | [diff] [blame] | 309 | sizeof(struct hv_guid)) == 0) |
K. Y. Srinivasan | b7fc147 | 2011-03-15 15:03:38 -0700 | [diff] [blame] | 310 | match = 1; |
K. Y. Srinivasan | de632a2 | 2011-04-26 09:20:24 -0700 | [diff] [blame] | 311 | |
K. Y. Srinivasan | b7fc147 | 2011-03-15 15:03:38 -0700 | [diff] [blame] | 312 | return match; |
| 313 | } |
| 314 | |
K. Y. Srinivasan | f1f0d67 | 2011-03-15 15:03:39 -0700 | [diff] [blame] | 315 | /* |
| 316 | * vmbus_probe - Add the new vmbus's child device |
| 317 | */ |
| 318 | static int vmbus_probe(struct device *child_device) |
| 319 | { |
| 320 | int ret = 0; |
| 321 | struct hv_driver *drv = |
| 322 | drv_to_hv_drv(child_device->driver); |
K. Y. Srinivasan | 9efd21e | 2011-04-29 13:45:10 -0700 | [diff] [blame] | 323 | struct hv_device *dev = device_to_hv_device(child_device); |
K. Y. Srinivasan | f1f0d67 | 2011-03-15 15:03:39 -0700 | [diff] [blame] | 324 | |
K. Y. Srinivasan | 9efd21e | 2011-04-29 13:45:10 -0700 | [diff] [blame] | 325 | if (drv->probe) { |
| 326 | ret = drv->probe(dev); |
K. Y. Srinivasan | b14a7b3 | 2011-04-29 13:45:03 -0700 | [diff] [blame] | 327 | if (ret != 0) |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 328 | pr_err("probe failed for device %s (%d)\n", |
| 329 | dev_name(child_device), ret); |
K. Y. Srinivasan | f1f0d67 | 2011-03-15 15:03:39 -0700 | [diff] [blame] | 330 | |
K. Y. Srinivasan | f1f0d67 | 2011-03-15 15:03:39 -0700 | [diff] [blame] | 331 | } else { |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 332 | pr_err("probe not set for driver %s\n", |
| 333 | dev_name(child_device)); |
K. Y. Srinivasan | f1f0d67 | 2011-03-15 15:03:39 -0700 | [diff] [blame] | 334 | ret = -1; |
| 335 | } |
| 336 | return ret; |
| 337 | } |
| 338 | |
K. Y. Srinivasan | c5dce3d | 2011-03-15 15:03:40 -0700 | [diff] [blame] | 339 | /* |
| 340 | * vmbus_remove - Remove a vmbus device |
| 341 | */ |
| 342 | static int vmbus_remove(struct device *child_device) |
| 343 | { |
| 344 | int ret; |
| 345 | struct hv_driver *drv; |
| 346 | |
K. Y. Srinivasan | 415b023 | 2011-04-29 13:45:12 -0700 | [diff] [blame] | 347 | struct hv_device *dev = device_to_hv_device(child_device); |
K. Y. Srinivasan | c5dce3d | 2011-03-15 15:03:40 -0700 | [diff] [blame] | 348 | |
| 349 | if (child_device->driver) { |
| 350 | drv = drv_to_hv_drv(child_device->driver); |
| 351 | |
K. Y. Srinivasan | 415b023 | 2011-04-29 13:45:12 -0700 | [diff] [blame] | 352 | if (drv->remove) { |
| 353 | ret = drv->remove(dev); |
K. Y. Srinivasan | c5dce3d | 2011-03-15 15:03:40 -0700 | [diff] [blame] | 354 | } else { |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 355 | pr_err("remove not set for driver %s\n", |
| 356 | dev_name(child_device)); |
K. Y. Srinivasan | c5dce3d | 2011-03-15 15:03:40 -0700 | [diff] [blame] | 357 | ret = -1; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
K. Y. Srinivasan | eb1bb25 | 2011-03-15 15:03:41 -0700 | [diff] [blame] | 364 | |
| 365 | /* |
| 366 | * vmbus_shutdown - Shutdown a vmbus device |
| 367 | */ |
| 368 | static void vmbus_shutdown(struct device *child_device) |
| 369 | { |
| 370 | struct hv_driver *drv; |
K. Y. Srinivasan | ca6887f | 2011-04-29 13:45:14 -0700 | [diff] [blame] | 371 | struct hv_device *dev = device_to_hv_device(child_device); |
K. Y. Srinivasan | eb1bb25 | 2011-03-15 15:03:41 -0700 | [diff] [blame] | 372 | |
| 373 | |
| 374 | /* The device may not be attached yet */ |
| 375 | if (!child_device->driver) |
| 376 | return; |
| 377 | |
| 378 | drv = drv_to_hv_drv(child_device->driver); |
| 379 | |
K. Y. Srinivasan | ca6887f | 2011-04-29 13:45:14 -0700 | [diff] [blame] | 380 | if (drv->shutdown) |
| 381 | drv->shutdown(dev); |
K. Y. Srinivasan | eb1bb25 | 2011-03-15 15:03:41 -0700 | [diff] [blame] | 382 | |
| 383 | return; |
| 384 | } |
| 385 | |
K. Y. Srinivasan | 086e7a5 | 2011-03-15 15:03:42 -0700 | [diff] [blame] | 386 | |
| 387 | /* |
| 388 | * vmbus_device_release - Final callback release of the vmbus child device |
| 389 | */ |
| 390 | static void vmbus_device_release(struct device *device) |
| 391 | { |
| 392 | struct hv_device *device_ctx = device_to_hv_device(device); |
| 393 | |
| 394 | kfree(device_ctx); |
| 395 | |
| 396 | } |
| 397 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 398 | /* The one and only one */ |
K. Y. Srinivasan | 9adcac5 | 2011-04-29 13:45:08 -0700 | [diff] [blame] | 399 | static struct bus_type hv_bus = { |
| 400 | .name = "vmbus", |
| 401 | .match = vmbus_match, |
| 402 | .shutdown = vmbus_shutdown, |
| 403 | .remove = vmbus_remove, |
| 404 | .probe = vmbus_probe, |
| 405 | .uevent = vmbus_uevent, |
| 406 | .dev_attrs = vmbus_device_attrs, |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 407 | }; |
| 408 | |
Haiyang Zhang | adf874c | 2011-01-26 12:12:09 -0800 | [diff] [blame] | 409 | static const char *driver_name = "hyperv"; |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 410 | |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 411 | |
Timo Teräs | bf6506f | 2010-12-15 20:48:08 +0200 | [diff] [blame] | 412 | struct onmessage_work_context { |
| 413 | struct work_struct work; |
| 414 | struct hv_message msg; |
| 415 | }; |
| 416 | |
| 417 | static void vmbus_onmessage_work(struct work_struct *work) |
| 418 | { |
| 419 | struct onmessage_work_context *ctx; |
| 420 | |
| 421 | ctx = container_of(work, struct onmessage_work_context, |
| 422 | work); |
| 423 | vmbus_onmessage(&ctx->msg); |
| 424 | kfree(ctx); |
| 425 | } |
| 426 | |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 427 | /* |
| 428 | * vmbus_on_msg_dpc - DPC routine to handle messages from the hypervisior |
| 429 | */ |
K. Y. Srinivasan | 62c1059 | 2011-03-10 14:04:53 -0800 | [diff] [blame] | 430 | static void vmbus_on_msg_dpc(unsigned long data) |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 431 | { |
| 432 | int cpu = smp_processor_id(); |
| 433 | void *page_addr = hv_context.synic_message_page[cpu]; |
| 434 | struct hv_message *msg = (struct hv_message *)page_addr + |
| 435 | VMBUS_MESSAGE_SINT; |
Timo Teräs | bf6506f | 2010-12-15 20:48:08 +0200 | [diff] [blame] | 436 | struct onmessage_work_context *ctx; |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 437 | |
| 438 | while (1) { |
| 439 | if (msg->header.message_type == HVMSG_NONE) { |
| 440 | /* no msg */ |
| 441 | break; |
| 442 | } else { |
Timo Teräs | bf6506f | 2010-12-15 20:48:08 +0200 | [diff] [blame] | 443 | ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC); |
| 444 | if (ctx == NULL) |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 445 | continue; |
Timo Teräs | bf6506f | 2010-12-15 20:48:08 +0200 | [diff] [blame] | 446 | INIT_WORK(&ctx->work, vmbus_onmessage_work); |
| 447 | memcpy(&ctx->msg, msg, sizeof(*msg)); |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 448 | queue_work(vmbus_connection.work_queue, &ctx->work); |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | msg->header.message_type = HVMSG_NONE; |
| 452 | |
| 453 | /* |
| 454 | * Make sure the write to MessageType (ie set to |
| 455 | * HVMSG_NONE) happens before we read the |
| 456 | * MessagePending and EOMing. Otherwise, the EOMing |
| 457 | * will not deliver any more messages since there is |
| 458 | * no empty slot |
| 459 | */ |
| 460 | mb(); |
| 461 | |
| 462 | if (msg->header.message_flags.msg_pending) { |
| 463 | /* |
| 464 | * This will cause message queue rescan to |
| 465 | * possibly deliver another msg from the |
| 466 | * hypervisor |
| 467 | */ |
| 468 | wrmsrl(HV_X64_MSR_EOM, 0); |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | /* |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 474 | * vmbus_on_isr - ISR routine |
| 475 | */ |
K. Y. Srinivasan | 480ae58 | 2011-03-10 14:06:06 -0800 | [diff] [blame] | 476 | static int vmbus_on_isr(void) |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 477 | { |
| 478 | int ret = 0; |
| 479 | int cpu = smp_processor_id(); |
| 480 | void *page_addr; |
| 481 | struct hv_message *msg; |
| 482 | union hv_synic_event_flags *event; |
| 483 | |
| 484 | page_addr = hv_context.synic_message_page[cpu]; |
| 485 | msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT; |
| 486 | |
| 487 | /* Check if there are actual msgs to be process */ |
Hank Janssen | 98e0870 | 2011-03-29 13:58:44 -0700 | [diff] [blame] | 488 | if (msg->header.message_type != HVMSG_NONE) |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 489 | ret |= 0x1; |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 490 | |
| 491 | /* TODO: Check if there are events to be process */ |
| 492 | page_addr = hv_context.synic_event_page[cpu]; |
| 493 | event = (union hv_synic_event_flags *)page_addr + VMBUS_MESSAGE_SINT; |
| 494 | |
| 495 | /* Since we are a child, we only need to check bit 0 */ |
Greg Kroah-Hartman | 32235b0 | 2011-04-13 12:14:05 -0700 | [diff] [blame] | 496 | if (sync_test_and_clear_bit(0, (unsigned long *) &event->flags32[0])) |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 497 | ret |= 0x2; |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 498 | |
| 499 | return ret; |
| 500 | } |
| 501 | |
K. Y. Srinivasan | 793be9c | 2011-03-15 15:03:43 -0700 | [diff] [blame] | 502 | |
| 503 | static irqreturn_t vmbus_isr(int irq, void *dev_id) |
| 504 | { |
| 505 | int ret; |
| 506 | |
| 507 | ret = vmbus_on_isr(); |
| 508 | |
| 509 | /* Schedules a dpc if necessary */ |
| 510 | if (ret > 0) { |
| 511 | if (test_bit(0, (unsigned long *)&ret)) |
K. Y. Srinivasan | 59c0e4f | 2011-04-29 13:45:06 -0700 | [diff] [blame] | 512 | tasklet_schedule(&msg_dpc); |
K. Y. Srinivasan | 793be9c | 2011-03-15 15:03:43 -0700 | [diff] [blame] | 513 | |
| 514 | if (test_bit(1, (unsigned long *)&ret)) |
K. Y. Srinivasan | 66d9229 | 2011-04-29 13:45:07 -0700 | [diff] [blame] | 515 | tasklet_schedule(&event_dpc); |
K. Y. Srinivasan | 793be9c | 2011-03-15 15:03:43 -0700 | [diff] [blame] | 516 | |
| 517 | return IRQ_HANDLED; |
| 518 | } else { |
| 519 | return IRQ_NONE; |
| 520 | } |
| 521 | } |
| 522 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 523 | /* |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 524 | * vmbus_bus_init -Main vmbus driver initialization routine. |
| 525 | * |
| 526 | * Here, we |
Lars Lindley | 0686e4f | 2010-03-11 23:51:23 +0100 | [diff] [blame] | 527 | * - initialize the vmbus driver context |
Lars Lindley | 0686e4f | 2010-03-11 23:51:23 +0100 | [diff] [blame] | 528 | * - invoke the vmbus hv main init routine |
| 529 | * - get the irq resource |
Lars Lindley | 0686e4f | 2010-03-11 23:51:23 +0100 | [diff] [blame] | 530 | * - retrieve the channel offers |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 531 | */ |
K. Y. Srinivasan | 52e5c1c | 2011-03-15 15:03:34 -0700 | [diff] [blame] | 532 | static int vmbus_bus_init(struct pci_dev *pdev) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 533 | { |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 534 | int ret; |
| 535 | unsigned int vector; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 536 | |
Greg Kroah-Hartman | 6d26e38fa2 | 2010-12-02 12:08:08 -0800 | [diff] [blame] | 537 | /* Hypervisor initialization...setup hypercall page..etc */ |
| 538 | ret = hv_init(); |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 539 | if (ret != 0) { |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 540 | pr_err("Unable to initialize the hypervisor - 0x%x\n", ret); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 541 | goto cleanup; |
| 542 | } |
| 543 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 544 | /* Initialize the bus context */ |
K. Y. Srinivasan | 59c0e4f | 2011-04-29 13:45:06 -0700 | [diff] [blame] | 545 | tasklet_init(&msg_dpc, vmbus_on_msg_dpc, 0); |
K. Y. Srinivasan | 66d9229 | 2011-04-29 13:45:07 -0700 | [diff] [blame] | 546 | tasklet_init(&event_dpc, vmbus_on_event, 0); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 547 | |
K. Y. Srinivasan | a6e4d8e | 2011-03-10 14:03:51 -0800 | [diff] [blame] | 548 | /* Now, register the bus with LDM */ |
K. Y. Srinivasan | 9adcac5 | 2011-04-29 13:45:08 -0700 | [diff] [blame] | 549 | ret = bus_register(&hv_bus); |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 550 | if (ret) { |
Bill Pemberton | c19fbca | 2009-07-27 16:47:35 -0400 | [diff] [blame] | 551 | ret = -1; |
| 552 | goto cleanup; |
| 553 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 554 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 555 | /* Get the interrupt resource */ |
K. Y. Srinivasan | 52e5c1c | 2011-03-15 15:03:34 -0700 | [diff] [blame] | 556 | ret = request_irq(pdev->irq, vmbus_isr, |
| 557 | IRQF_SHARED | IRQF_SAMPLE_RANDOM, |
| 558 | driver_name, pdev); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 559 | |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 560 | if (ret != 0) { |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 561 | pr_err("Unable to request IRQ %d\n", |
K. Y. Srinivasan | 52e5c1c | 2011-03-15 15:03:34 -0700 | [diff] [blame] | 562 | pdev->irq); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 563 | |
K. Y. Srinivasan | 9adcac5 | 2011-04-29 13:45:08 -0700 | [diff] [blame] | 564 | bus_unregister(&hv_bus); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 565 | |
| 566 | ret = -1; |
| 567 | goto cleanup; |
| 568 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 569 | |
K. Y. Srinivasan | 52e5c1c | 2011-03-15 15:03:34 -0700 | [diff] [blame] | 570 | vector = IRQ0_VECTOR + pdev->irq; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 571 | |
K. Y. Srinivasan | 800b690 | 2011-03-15 15:03:33 -0700 | [diff] [blame] | 572 | /* |
| 573 | * Notify the hypervisor of our irq and |
| 574 | * connect to the host. |
| 575 | */ |
| 576 | on_each_cpu(hv_synic_init, (void *)&vector, 1); |
| 577 | ret = vmbus_connect(); |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 578 | if (ret) { |
K. Y. Srinivasan | 52e5c1c | 2011-03-15 15:03:34 -0700 | [diff] [blame] | 579 | free_irq(pdev->irq, pdev); |
K. Y. Srinivasan | 9adcac5 | 2011-04-29 13:45:08 -0700 | [diff] [blame] | 580 | bus_unregister(&hv_bus); |
Bill Pemberton | 5d48a1c | 2009-07-27 16:47:36 -0400 | [diff] [blame] | 581 | goto cleanup; |
| 582 | } |
| 583 | |
K. Y. Srinivasan | 800b690 | 2011-03-15 15:03:33 -0700 | [diff] [blame] | 584 | |
Greg Kroah-Hartman | 2d6e882 | 2010-12-02 08:50:58 -0800 | [diff] [blame] | 585 | vmbus_request_offers(); |
Haiyang Zhang | 8b5d6d3 | 2010-05-28 23:22:44 +0000 | [diff] [blame] | 586 | wait_for_completion(&hv_channel_ready); |
| 587 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 588 | cleanup: |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 589 | return ret; |
| 590 | } |
| 591 | |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 592 | /** |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 593 | * vmbus_child_driver_register() - Register a vmbus's child driver |
K. Y. Srinivasan | c643269d | 2011-03-07 13:23:43 -0800 | [diff] [blame] | 594 | * @drv: Pointer to driver structure you want to register |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 595 | * |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 596 | * |
| 597 | * Registers the given driver with Linux through the 'driver_register()' call |
| 598 | * And sets up the hyper-v vmbus handling for this driver. |
| 599 | * It will return the state of the 'driver_register()' call. |
| 600 | * |
| 601 | * Mainly used by Hyper-V drivers. |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 602 | */ |
K. Y. Srinivasan | c643269d | 2011-03-07 13:23:43 -0800 | [diff] [blame] | 603 | int vmbus_child_driver_register(struct device_driver *drv) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 604 | { |
Bill Pemberton | 5d48a1c | 2009-07-27 16:47:36 -0400 | [diff] [blame] | 605 | int ret; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 606 | |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 607 | pr_info("child driver registering - name %s\n", drv->name); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 608 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 609 | /* The child driver on this vmbus */ |
K. Y. Srinivasan | 9adcac5 | 2011-04-29 13:45:08 -0700 | [diff] [blame] | 610 | drv->bus = &hv_bus; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 611 | |
K. Y. Srinivasan | c643269d | 2011-03-07 13:23:43 -0800 | [diff] [blame] | 612 | ret = driver_register(drv); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 613 | |
Greg Kroah-Hartman | 2d6e882 | 2010-12-02 08:50:58 -0800 | [diff] [blame] | 614 | vmbus_request_offers(); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 615 | |
Bill Pemberton | 5d48a1c | 2009-07-27 16:47:36 -0400 | [diff] [blame] | 616 | return ret; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 617 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 618 | EXPORT_SYMBOL(vmbus_child_driver_register); |
| 619 | |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 620 | /** |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 621 | * vmbus_child_driver_unregister() - Unregister a vmbus's child driver |
K. Y. Srinivasan | 06de23f | 2011-03-07 13:24:23 -0800 | [diff] [blame] | 622 | * @drv: Pointer to driver structure you want to un-register |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 623 | * |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 624 | * |
| 625 | * Un-register the given driver with Linux through the 'driver_unregister()' |
| 626 | * call. And ungegisters the driver from the Hyper-V vmbus handler. |
| 627 | * |
| 628 | * Mainly used by Hyper-V drivers. |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 629 | */ |
K. Y. Srinivasan | 06de23f | 2011-03-07 13:24:23 -0800 | [diff] [blame] | 630 | void vmbus_child_driver_unregister(struct device_driver *drv) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 631 | { |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 632 | pr_info("child driver unregistering - name %s\n", drv->name); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 633 | |
K. Y. Srinivasan | 06de23f | 2011-03-07 13:24:23 -0800 | [diff] [blame] | 634 | driver_unregister(drv); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 635 | |
K. Y. Srinivasan | 06de23f | 2011-03-07 13:24:23 -0800 | [diff] [blame] | 636 | drv->bus = NULL; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 637 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 638 | EXPORT_SYMBOL(vmbus_child_driver_unregister); |
| 639 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 640 | /* |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 641 | * vmbus_child_device_create - Creates and registers a new child device |
| 642 | * on the vmbus. |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 643 | */ |
Greg Kroah-Hartman | 89733aa | 2010-12-02 08:22:41 -0800 | [diff] [blame] | 644 | struct hv_device *vmbus_child_device_create(struct hv_guid *type, |
| 645 | struct hv_guid *instance, |
| 646 | struct vmbus_channel *channel) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 647 | { |
Nicolas Palix | 3d3b551 | 2009-07-28 17:32:53 +0200 | [diff] [blame] | 648 | struct hv_device *child_device_obj; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 649 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 650 | /* Allocate the new child device */ |
K. Y. Srinivasan | 6bad88d | 2011-03-07 13:35:48 -0800 | [diff] [blame] | 651 | child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL); |
| 652 | if (!child_device_obj) { |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 653 | pr_err("Unable to allocate device object for child device\n"); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 654 | return NULL; |
| 655 | } |
| 656 | |
Greg Kroah-Hartman | cae5b84 | 2010-10-21 09:05:27 -0700 | [diff] [blame] | 657 | child_device_obj->channel = channel; |
Haiyang Zhang | ca623ad | 2011-01-26 12:12:11 -0800 | [diff] [blame] | 658 | memcpy(&child_device_obj->dev_type, type, sizeof(struct hv_guid)); |
| 659 | memcpy(&child_device_obj->dev_instance, instance, |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 660 | sizeof(struct hv_guid)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 661 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 662 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 663 | return child_device_obj; |
| 664 | } |
| 665 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 666 | /* |
K. Y. Srinivasan | e13a0b5 | 2011-03-10 14:07:44 -0800 | [diff] [blame] | 667 | * vmbus_child_device_register - Register the child device |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 668 | */ |
K. Y. Srinivasan | 3ca07cb | 2011-03-10 14:07:21 -0800 | [diff] [blame] | 669 | int vmbus_child_device_register(struct hv_device *child_device_obj) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 670 | { |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 671 | int ret = 0; |
K. Y. Srinivasan | 6bad88d | 2011-03-07 13:35:48 -0800 | [diff] [blame] | 672 | |
Bill Pemberton | f488841 | 2009-07-29 17:00:12 -0400 | [diff] [blame] | 673 | static atomic_t device_num = ATOMIC_INIT(0); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 674 | |
Haiyang Zhang | 1bb40a2 | 2009-10-23 18:14:24 +0000 | [diff] [blame] | 675 | /* Set the device name. Otherwise, device_register() will fail. */ |
K. Y. Srinivasan | 6bad88d | 2011-03-07 13:35:48 -0800 | [diff] [blame] | 676 | dev_set_name(&child_device_obj->device, "vmbus_0_%d", |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 677 | atomic_inc_return(&device_num)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 678 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 679 | /* The new device belongs to this bus */ |
K. Y. Srinivasan | 9adcac5 | 2011-04-29 13:45:08 -0700 | [diff] [blame] | 680 | child_device_obj->device.bus = &hv_bus; /* device->dev.bus; */ |
K. Y. Srinivasan | 800b690 | 2011-03-15 15:03:33 -0700 | [diff] [blame] | 681 | child_device_obj->device.parent = &hv_pci_dev->dev; |
K. Y. Srinivasan | 6bad88d | 2011-03-07 13:35:48 -0800 | [diff] [blame] | 682 | child_device_obj->device.release = vmbus_device_release; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 683 | |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 684 | /* |
| 685 | * Register with the LDM. This will kick off the driver/device |
| 686 | * binding...which will eventually call vmbus_match() and vmbus_probe() |
| 687 | */ |
K. Y. Srinivasan | 6bad88d | 2011-03-07 13:35:48 -0800 | [diff] [blame] | 688 | ret = device_register(&child_device_obj->device); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 689 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 690 | if (ret) |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 691 | pr_err("Unable to register child device\n"); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 692 | else |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 693 | pr_info("child device %s registered\n", |
| 694 | dev_name(&child_device_obj->device)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 695 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 696 | return ret; |
| 697 | } |
| 698 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 699 | /* |
| 700 | * vmbus_child_device_unregister - Remove the specified child device |
| 701 | * from the vmbus. |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 702 | */ |
Greg Kroah-Hartman | 9d8bd71 | 2010-12-02 08:34:45 -0800 | [diff] [blame] | 703 | void vmbus_child_device_unregister(struct hv_device *device_obj) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 704 | { |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 705 | /* |
| 706 | * Kick off the process of unregistering the device. |
| 707 | * This will call vmbus_remove() and eventually vmbus_device_release() |
| 708 | */ |
K. Y. Srinivasan | 6bad88d | 2011-03-07 13:35:48 -0800 | [diff] [blame] | 709 | device_unregister(&device_obj->device); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 710 | |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 711 | pr_info("child device %s unregistered\n", |
| 712 | dev_name(&device_obj->device)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 715 | |
K. Y. Srinivasan | b0069f4 | 2011-04-29 13:45:15 -0700 | [diff] [blame] | 716 | /* |
| 717 | * VMBUS is an acpi enumerated device. Get the the IRQ information |
| 718 | * from DSDT. |
| 719 | */ |
| 720 | |
| 721 | static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *irq) |
| 722 | { |
| 723 | |
| 724 | if (res->type == ACPI_RESOURCE_TYPE_IRQ) { |
| 725 | struct acpi_resource_irq *irqp; |
| 726 | irqp = &res->data.irq; |
| 727 | |
| 728 | *((unsigned int *)irq) = irqp->interrupts[0]; |
| 729 | } |
| 730 | |
| 731 | return AE_OK; |
| 732 | } |
| 733 | |
| 734 | static int vmbus_acpi_add(struct acpi_device *device) |
| 735 | { |
| 736 | acpi_status result; |
| 737 | |
| 738 | result = |
| 739 | acpi_walk_resources(device->handle, METHOD_NAME__CRS, |
| 740 | vmbus_walk_resources, &irq); |
| 741 | |
| 742 | if (ACPI_FAILURE(result)) { |
| 743 | complete(&probe_event); |
| 744 | return -ENODEV; |
| 745 | } |
| 746 | complete(&probe_event); |
| 747 | return 0; |
| 748 | } |
| 749 | |
| 750 | static const struct acpi_device_id vmbus_acpi_device_ids[] = { |
| 751 | {"VMBUS", 0}, |
| 752 | {"", 0}, |
| 753 | }; |
| 754 | MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids); |
| 755 | |
| 756 | static struct acpi_driver vmbus_acpi_driver = { |
| 757 | .name = "vmbus", |
| 758 | .ids = vmbus_acpi_device_ids, |
| 759 | .ops = { |
| 760 | .add = vmbus_acpi_add, |
| 761 | }, |
| 762 | }; |
| 763 | |
| 764 | static int vmbus_acpi_init(void) |
| 765 | { |
| 766 | int result; |
| 767 | |
| 768 | |
| 769 | result = acpi_bus_register_driver(&vmbus_acpi_driver); |
| 770 | if (result < 0) |
| 771 | return result; |
| 772 | |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | static void vmbus_acpi_exit(void) |
| 777 | { |
| 778 | acpi_bus_unregister_driver(&vmbus_acpi_driver); |
| 779 | |
| 780 | return; |
| 781 | } |
| 782 | |
| 783 | |
K. Y. Srinivasan | 1168ac2 | 2011-03-15 15:03:32 -0700 | [diff] [blame] | 784 | static int __devinit hv_pci_probe(struct pci_dev *pdev, |
| 785 | const struct pci_device_id *ent) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 786 | { |
K. Y. Srinivasan | 1168ac2 | 2011-03-15 15:03:32 -0700 | [diff] [blame] | 787 | hv_pci_dev = pdev; |
Greg Kroah-Hartman | c22090f | 2010-02-25 16:43:15 -0800 | [diff] [blame] | 788 | |
K. Y. Srinivasan | 71a6655 | 2011-04-29 13:45:04 -0700 | [diff] [blame] | 789 | pci_probe_error = pci_enable_device(pdev); |
| 790 | if (pci_probe_error) |
| 791 | goto probe_cleanup; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 792 | |
K. Y. Srinivasan | b0069f4 | 2011-04-29 13:45:15 -0700 | [diff] [blame] | 793 | /* |
| 794 | * If the PCI sub-sytem did not assign us an |
| 795 | * irq, use the bios provided one. |
| 796 | */ |
| 797 | |
| 798 | if (pdev->irq == 0) |
| 799 | pdev->irq = irq; |
| 800 | |
K. Y. Srinivasan | 71a6655 | 2011-04-29 13:45:04 -0700 | [diff] [blame] | 801 | pci_probe_error = vmbus_bus_init(pdev); |
K. Y. Srinivasan | b0069f4 | 2011-04-29 13:45:15 -0700 | [diff] [blame] | 802 | |
K. Y. Srinivasan | 71a6655 | 2011-04-29 13:45:04 -0700 | [diff] [blame] | 803 | if (pci_probe_error) |
K. Y. Srinivasan | 1168ac2 | 2011-03-15 15:03:32 -0700 | [diff] [blame] | 804 | pci_disable_device(pdev); |
| 805 | |
K. Y. Srinivasan | 71a6655 | 2011-04-29 13:45:04 -0700 | [diff] [blame] | 806 | probe_cleanup: |
| 807 | complete(&probe_event); |
| 808 | return pci_probe_error; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 809 | } |
| 810 | |
Greg Kroah-Hartman | 9a775db | 2010-02-25 16:42:10 -0800 | [diff] [blame] | 811 | /* |
| 812 | * We use a PCI table to determine if we should autoload this driver This is |
| 813 | * needed by distro tools to determine if the hyperv drivers should be |
| 814 | * installed and/or configured. We don't do anything else with the table, but |
| 815 | * it needs to be present. |
Greg Kroah-Hartman | 9a775db | 2010-02-25 16:42:10 -0800 | [diff] [blame] | 816 | */ |
Tobias Klauser | 15f0beb | 2010-05-20 10:39:38 +0200 | [diff] [blame] | 817 | static const struct pci_device_id microsoft_hv_pci_table[] = { |
Greg Kroah-Hartman | 9a775db | 2010-02-25 16:42:10 -0800 | [diff] [blame] | 818 | { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */ |
| 819 | { 0 } |
| 820 | }; |
| 821 | MODULE_DEVICE_TABLE(pci, microsoft_hv_pci_table); |
| 822 | |
K. Y. Srinivasan | 1168ac2 | 2011-03-15 15:03:32 -0700 | [diff] [blame] | 823 | static struct pci_driver hv_bus_driver = { |
| 824 | .name = "hv_bus", |
| 825 | .probe = hv_pci_probe, |
| 826 | .id_table = microsoft_hv_pci_table, |
| 827 | }; |
| 828 | |
| 829 | static int __init hv_pci_init(void) |
| 830 | { |
K. Y. Srinivasan | 71a6655 | 2011-04-29 13:45:04 -0700 | [diff] [blame] | 831 | int ret; |
K. Y. Srinivasan | b0069f4 | 2011-04-29 13:45:15 -0700 | [diff] [blame] | 832 | |
| 833 | init_completion(&probe_event); |
| 834 | |
| 835 | /* |
| 836 | * Get irq resources first. |
| 837 | */ |
| 838 | |
| 839 | ret = vmbus_acpi_init(); |
| 840 | if (ret) |
| 841 | return ret; |
| 842 | |
| 843 | wait_for_completion(&probe_event); |
| 844 | |
| 845 | if (irq <= 0) { |
| 846 | vmbus_acpi_exit(); |
| 847 | return -ENODEV; |
| 848 | } |
| 849 | |
| 850 | vmbus_acpi_exit(); |
K. Y. Srinivasan | 71a6655 | 2011-04-29 13:45:04 -0700 | [diff] [blame] | 851 | init_completion(&probe_event); |
| 852 | ret = pci_register_driver(&hv_bus_driver); |
| 853 | if (ret) |
| 854 | return ret; |
| 855 | /* |
| 856 | * All the vmbus initialization occurs within the |
| 857 | * hv_pci_probe() function. Wait for hv_pci_probe() |
| 858 | * to complete. |
| 859 | */ |
| 860 | wait_for_completion(&probe_event); |
| 861 | |
| 862 | if (pci_probe_error) |
| 863 | pci_unregister_driver(&hv_bus_driver); |
| 864 | return pci_probe_error; |
K. Y. Srinivasan | 1168ac2 | 2011-03-15 15:03:32 -0700 | [diff] [blame] | 865 | } |
| 866 | |
K. Y. Srinivasan | 1168ac2 | 2011-03-15 15:03:32 -0700 | [diff] [blame] | 867 | |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 868 | MODULE_LICENSE("GPL"); |
Hank Janssen | 26c14cc | 2010-02-11 23:02:42 +0000 | [diff] [blame] | 869 | MODULE_VERSION(HV_DRV_VERSION); |
Olaf Hering | 13399cb | 2011-04-16 18:50:42 +0200 | [diff] [blame] | 870 | module_param(vmbus_loglevel, int, S_IRUGO|S_IWUSR); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 871 | |
K. Y. Srinivasan | 1168ac2 | 2011-03-15 15:03:32 -0700 | [diff] [blame] | 872 | module_init(hv_pci_init); |