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 | 52e5c1c | 2011-03-15 15:03:34 -0700 | [diff] [blame] | 20 | * |
| 21 | * 3/9/2011: K. Y. Srinivasan - Significant restructuring and cleanup |
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> |
Haiyang Zhang | 8b5d6d3 | 2010-05-28 23:22:44 +0000 | [diff] [blame] | 34 | #include <linux/completion.h> |
Greg Kroah-Hartman | 2d82f6c | 2010-05-05 22:52:28 -0700 | [diff] [blame] | 35 | #include "version_info.h" |
K. Y. Srinivasan | e3fe0bb | 2011-02-11 10:00:12 -0800 | [diff] [blame] | 36 | #include "hv_api.h" |
Greg Kroah-Hartman | 645954c | 2009-08-28 16:22:59 -0700 | [diff] [blame] | 37 | #include "logging.h" |
Greg Kroah-Hartman | 870cde8 | 2009-08-19 16:21:28 -0700 | [diff] [blame] | 38 | #include "vmbus.h" |
Greg Kroah-Hartman | 150b19d | 2010-10-20 16:07:11 -0700 | [diff] [blame] | 39 | #include "channel.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; |
| 46 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 47 | /* Main vmbus driver data structure */ |
K. Y. Srinivasan | 04677c0 | 2011-03-15 15:03:35 -0700 | [diff] [blame] | 48 | struct hv_bus { |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 49 | struct bus_type bus; |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 50 | struct tasklet_struct event_dpc; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 53 | unsigned int vmbus_loglevel = (ALL_MODULES << 16 | INFO_LVL); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 54 | EXPORT_SYMBOL(vmbus_loglevel); |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 55 | /* (ALL_MODULES << 16 | DEBUG_LVL_ENTEREXIT); */ |
| 56 | /* (((VMBUS | VMBUS_DRV)<<16) | DEBUG_LVL_ENTEREXIT); */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 57 | |
K. Y. Srinivasan | 71a6655 | 2011-04-29 13:45:04 -0700 | [diff] [blame] | 58 | static int pci_probe_error; |
| 59 | static struct completion probe_event; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 60 | |
K. Y. Srinivasan | 98db433 | 2011-03-15 15:03:44 -0700 | [diff] [blame] | 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; |
| 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 | */ |
| 112 | static 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 Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 210 | /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 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 | }; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 239 | |
K. Y. Srinivasan | 793be9c | 2011-03-15 15:03:43 -0700 | [diff] [blame] | 240 | |
K. Y. Srinivasan | adde248 | 2011-03-15 15:03:37 -0700 | [diff] [blame] | 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 | |
K. Y. Srinivasan | adde248 | 2011-03-15 15:03:37 -0700 | [diff] [blame] | 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 | |
K. Y. Srinivasan | b7fc147 | 2011-03-15 15:03:38 -0700 | [diff] [blame] | 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, |
K. Y. Srinivasan | de632a2 | 2011-04-26 09:20:24 -0700 | [diff] [blame] | 313 | sizeof(struct hv_guid)) == 0) |
K. Y. Srinivasan | b7fc147 | 2011-03-15 15:03:38 -0700 | [diff] [blame] | 314 | match = 1; |
K. Y. Srinivasan | de632a2 | 2011-04-26 09:20:24 -0700 | [diff] [blame] | 315 | |
K. Y. Srinivasan | b7fc147 | 2011-03-15 15:03:38 -0700 | [diff] [blame] | 316 | return match; |
| 317 | } |
| 318 | |
K. Y. Srinivasan | f1f0d67 | 2011-03-15 15:03:39 -0700 | [diff] [blame] | 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); |
K. Y. Srinivasan | f1f0d67 | 2011-03-15 15:03:39 -0700 | [diff] [blame] | 327 | |
| 328 | /* Let the specific open-source driver handles the probe if it can */ |
| 329 | if (drv->driver.probe) { |
K. Y. Srinivasan | b14a7b3 | 2011-04-29 13:45:03 -0700 | [diff] [blame] | 330 | ret = drv->driver.probe(child_device); |
| 331 | if (ret != 0) |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 332 | pr_err("probe failed for device %s (%d)\n", |
| 333 | dev_name(child_device), ret); |
K. Y. Srinivasan | f1f0d67 | 2011-03-15 15:03:39 -0700 | [diff] [blame] | 334 | |
K. Y. Srinivasan | f1f0d67 | 2011-03-15 15:03:39 -0700 | [diff] [blame] | 335 | } else { |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 336 | pr_err("probe not set for driver %s\n", |
| 337 | dev_name(child_device)); |
K. Y. Srinivasan | f1f0d67 | 2011-03-15 15:03:39 -0700 | [diff] [blame] | 338 | ret = -1; |
| 339 | } |
| 340 | return ret; |
| 341 | } |
| 342 | |
K. Y. Srinivasan | c5dce3d | 2011-03-15 15:03:40 -0700 | [diff] [blame] | 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 { |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 362 | pr_err("remove not set for driver %s\n", |
| 363 | dev_name(child_device)); |
K. Y. Srinivasan | c5dce3d | 2011-03-15 15:03:40 -0700 | [diff] [blame] | 364 | ret = -1; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | return 0; |
| 369 | } |
| 370 | |
K. Y. Srinivasan | eb1bb25 | 2011-03-15 15:03:41 -0700 | [diff] [blame] | 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 | |
K. Y. Srinivasan | 086e7a5 | 2011-03-15 15:03:42 -0700 | [diff] [blame] | 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 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 405 | /* The one and only one */ |
K. Y. Srinivasan | 04677c0 | 2011-03-15 15:03:35 -0700 | [diff] [blame] | 406 | static struct hv_bus hv_bus = { |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 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, |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 414 | }; |
| 415 | |
Haiyang Zhang | adf874c | 2011-01-26 12:12:09 -0800 | [diff] [blame] | 416 | static const char *driver_name = "hyperv"; |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 417 | |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 418 | |
Timo Teräs | bf6506f | 2010-12-15 20:48:08 +0200 | [diff] [blame] | 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 | |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 434 | /* |
| 435 | * vmbus_on_msg_dpc - DPC routine to handle messages from the hypervisior |
| 436 | */ |
K. Y. Srinivasan | 62c1059 | 2011-03-10 14:04:53 -0800 | [diff] [blame] | 437 | static void vmbus_on_msg_dpc(unsigned long data) |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 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; |
Timo Teräs | bf6506f | 2010-12-15 20:48:08 +0200 | [diff] [blame] | 443 | struct onmessage_work_context *ctx; |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 444 | |
| 445 | while (1) { |
| 446 | if (msg->header.message_type == HVMSG_NONE) { |
| 447 | /* no msg */ |
| 448 | break; |
| 449 | } else { |
Timo Teräs | bf6506f | 2010-12-15 20:48:08 +0200 | [diff] [blame] | 450 | ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC); |
| 451 | if (ctx == NULL) |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 452 | continue; |
Timo Teräs | bf6506f | 2010-12-15 20:48:08 +0200 | [diff] [blame] | 453 | INIT_WORK(&ctx->work, vmbus_onmessage_work); |
| 454 | memcpy(&ctx->msg, msg, sizeof(*msg)); |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 455 | queue_work(vmbus_connection.work_queue, &ctx->work); |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 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 | |
| 480 | /* |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 481 | * vmbus_on_isr - ISR routine |
| 482 | */ |
K. Y. Srinivasan | 480ae58 | 2011-03-10 14:06:06 -0800 | [diff] [blame] | 483 | static int vmbus_on_isr(void) |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 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 */ |
Hank Janssen | 98e0870 | 2011-03-29 13:58:44 -0700 | [diff] [blame] | 495 | if (msg->header.message_type != HVMSG_NONE) |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 496 | ret |= 0x1; |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 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 */ |
Greg Kroah-Hartman | 32235b0 | 2011-04-13 12:14:05 -0700 | [diff] [blame] | 503 | 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] | 504 | ret |= 0x2; |
Greg Kroah-Hartman | 36199a9 | 2010-12-02 11:59:22 -0800 | [diff] [blame] | 505 | |
| 506 | return ret; |
| 507 | } |
| 508 | |
K. Y. Srinivasan | 793be9c | 2011-03-15 15:03:43 -0700 | [diff] [blame] | 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)) |
K. Y. Srinivasan | 59c0e4f | 2011-04-29 13:45:06 -0700 | [diff] [blame^] | 519 | tasklet_schedule(&msg_dpc); |
K. Y. Srinivasan | 793be9c | 2011-03-15 15:03:43 -0700 | [diff] [blame] | 520 | |
| 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 Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 530 | /* |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 531 | * vmbus_bus_init -Main vmbus driver initialization routine. |
| 532 | * |
| 533 | * Here, we |
Lars Lindley | 0686e4f | 2010-03-11 23:51:23 +0100 | [diff] [blame] | 534 | * - initialize the vmbus driver context |
Lars Lindley | 0686e4f | 2010-03-11 23:51:23 +0100 | [diff] [blame] | 535 | * - invoke the vmbus hv main init routine |
| 536 | * - get the irq resource |
Lars Lindley | 0686e4f | 2010-03-11 23:51:23 +0100 | [diff] [blame] | 537 | * - retrieve the channel offers |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 538 | */ |
K. Y. Srinivasan | 52e5c1c | 2011-03-15 15:03:34 -0700 | [diff] [blame] | 539 | static int vmbus_bus_init(struct pci_dev *pdev) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 540 | { |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 541 | int ret; |
| 542 | unsigned int vector; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 543 | |
Greg Kroah-Hartman | 6d26e38fa2 | 2010-12-02 12:08:08 -0800 | [diff] [blame] | 544 | /* Hypervisor initialization...setup hypercall page..etc */ |
| 545 | ret = hv_init(); |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 546 | if (ret != 0) { |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 547 | pr_err("Unable to initialize the hypervisor - 0x%x\n", ret); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 548 | goto cleanup; |
| 549 | } |
| 550 | |
K. Y. Srinivasan | 04677c0 | 2011-03-15 15:03:35 -0700 | [diff] [blame] | 551 | hv_bus.bus.name = driver_name; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 552 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 553 | /* Initialize the bus context */ |
K. Y. Srinivasan | 59c0e4f | 2011-04-29 13:45:06 -0700 | [diff] [blame^] | 554 | tasklet_init(&msg_dpc, vmbus_on_msg_dpc, 0); |
Olaf Hering | 8fea191 | 2011-04-16 18:50:41 +0200 | [diff] [blame] | 555 | tasklet_init(&hv_bus.event_dpc, vmbus_on_event, 0); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 556 | |
K. Y. Srinivasan | a6e4d8e | 2011-03-10 14:03:51 -0800 | [diff] [blame] | 557 | /* Now, register the bus with LDM */ |
K. Y. Srinivasan | 04677c0 | 2011-03-15 15:03:35 -0700 | [diff] [blame] | 558 | ret = bus_register(&hv_bus.bus); |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 559 | if (ret) { |
Bill Pemberton | c19fbca | 2009-07-27 16:47:35 -0400 | [diff] [blame] | 560 | ret = -1; |
| 561 | goto cleanup; |
| 562 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 563 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 564 | /* Get the interrupt resource */ |
K. Y. Srinivasan | 52e5c1c | 2011-03-15 15:03:34 -0700 | [diff] [blame] | 565 | ret = request_irq(pdev->irq, vmbus_isr, |
| 566 | IRQF_SHARED | IRQF_SAMPLE_RANDOM, |
| 567 | driver_name, pdev); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 568 | |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 569 | if (ret != 0) { |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 570 | pr_err("Unable to request IRQ %d\n", |
K. Y. Srinivasan | 52e5c1c | 2011-03-15 15:03:34 -0700 | [diff] [blame] | 571 | pdev->irq); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 572 | |
K. Y. Srinivasan | 04677c0 | 2011-03-15 15:03:35 -0700 | [diff] [blame] | 573 | bus_unregister(&hv_bus.bus); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 574 | |
| 575 | ret = -1; |
| 576 | goto cleanup; |
| 577 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 578 | |
K. Y. Srinivasan | 52e5c1c | 2011-03-15 15:03:34 -0700 | [diff] [blame] | 579 | vector = IRQ0_VECTOR + pdev->irq; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 580 | |
K. Y. Srinivasan | 800b690 | 2011-03-15 15:03:33 -0700 | [diff] [blame] | 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(); |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 587 | if (ret) { |
K. Y. Srinivasan | 52e5c1c | 2011-03-15 15:03:34 -0700 | [diff] [blame] | 588 | free_irq(pdev->irq, pdev); |
K. Y. Srinivasan | 04677c0 | 2011-03-15 15:03:35 -0700 | [diff] [blame] | 589 | bus_unregister(&hv_bus.bus); |
Bill Pemberton | 5d48a1c | 2009-07-27 16:47:36 -0400 | [diff] [blame] | 590 | goto cleanup; |
| 591 | } |
| 592 | |
K. Y. Srinivasan | 800b690 | 2011-03-15 15:03:33 -0700 | [diff] [blame] | 593 | |
Greg Kroah-Hartman | 2d6e882 | 2010-12-02 08:50:58 -0800 | [diff] [blame] | 594 | vmbus_request_offers(); |
Haiyang Zhang | 8b5d6d3 | 2010-05-28 23:22:44 +0000 | [diff] [blame] | 595 | wait_for_completion(&hv_channel_ready); |
| 596 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 597 | cleanup: |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 598 | return ret; |
| 599 | } |
| 600 | |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 601 | /** |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 602 | * vmbus_child_driver_register() - Register a vmbus's child driver |
K. Y. Srinivasan | c643269d | 2011-03-07 13:23:43 -0800 | [diff] [blame] | 603 | * @drv: Pointer to driver structure you want to register |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 604 | * |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 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. |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 611 | */ |
K. Y. Srinivasan | c643269d | 2011-03-07 13:23:43 -0800 | [diff] [blame] | 612 | int vmbus_child_driver_register(struct device_driver *drv) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 613 | { |
Bill Pemberton | 5d48a1c | 2009-07-27 16:47:36 -0400 | [diff] [blame] | 614 | int ret; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 615 | |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 616 | pr_info("child driver registering - name %s\n", drv->name); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 617 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 618 | /* The child driver on this vmbus */ |
K. Y. Srinivasan | 04677c0 | 2011-03-15 15:03:35 -0700 | [diff] [blame] | 619 | drv->bus = &hv_bus.bus; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 620 | |
K. Y. Srinivasan | c643269d | 2011-03-07 13:23:43 -0800 | [diff] [blame] | 621 | ret = driver_register(drv); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 622 | |
Greg Kroah-Hartman | 2d6e882 | 2010-12-02 08:50:58 -0800 | [diff] [blame] | 623 | vmbus_request_offers(); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 624 | |
Bill Pemberton | 5d48a1c | 2009-07-27 16:47:36 -0400 | [diff] [blame] | 625 | return ret; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 626 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 627 | EXPORT_SYMBOL(vmbus_child_driver_register); |
| 628 | |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 629 | /** |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 630 | * vmbus_child_driver_unregister() - Unregister a vmbus's child driver |
K. Y. Srinivasan | 06de23f | 2011-03-07 13:24:23 -0800 | [diff] [blame] | 631 | * @drv: Pointer to driver structure you want to un-register |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 632 | * |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 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. |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 638 | */ |
K. Y. Srinivasan | 06de23f | 2011-03-07 13:24:23 -0800 | [diff] [blame] | 639 | void vmbus_child_driver_unregister(struct device_driver *drv) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 640 | { |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 641 | pr_info("child driver unregistering - name %s\n", drv->name); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 642 | |
K. Y. Srinivasan | 06de23f | 2011-03-07 13:24:23 -0800 | [diff] [blame] | 643 | driver_unregister(drv); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 644 | |
K. Y. Srinivasan | 06de23f | 2011-03-07 13:24:23 -0800 | [diff] [blame] | 645 | drv->bus = NULL; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 646 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 647 | EXPORT_SYMBOL(vmbus_child_driver_unregister); |
| 648 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 649 | /* |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 650 | * vmbus_child_device_create - Creates and registers a new child device |
| 651 | * on the vmbus. |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 652 | */ |
Greg Kroah-Hartman | 89733aa | 2010-12-02 08:22:41 -0800 | [diff] [blame] | 653 | struct hv_device *vmbus_child_device_create(struct hv_guid *type, |
| 654 | struct hv_guid *instance, |
| 655 | struct vmbus_channel *channel) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 656 | { |
Nicolas Palix | 3d3b551 | 2009-07-28 17:32:53 +0200 | [diff] [blame] | 657 | struct hv_device *child_device_obj; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 658 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 659 | /* Allocate the new child device */ |
K. Y. Srinivasan | 6bad88d | 2011-03-07 13:35:48 -0800 | [diff] [blame] | 660 | child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL); |
| 661 | if (!child_device_obj) { |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 662 | pr_err("Unable to allocate device object for child device\n"); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 663 | return NULL; |
| 664 | } |
| 665 | |
Greg Kroah-Hartman | cae5b84 | 2010-10-21 09:05:27 -0700 | [diff] [blame] | 666 | child_device_obj->channel = channel; |
Haiyang Zhang | ca623ad | 2011-01-26 12:12:11 -0800 | [diff] [blame] | 667 | memcpy(&child_device_obj->dev_type, type, sizeof(struct hv_guid)); |
| 668 | memcpy(&child_device_obj->dev_instance, instance, |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 669 | sizeof(struct hv_guid)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 670 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 671 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 672 | return child_device_obj; |
| 673 | } |
| 674 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 675 | /* |
K. Y. Srinivasan | e13a0b5 | 2011-03-10 14:07:44 -0800 | [diff] [blame] | 676 | * vmbus_child_device_register - Register the child device |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 677 | */ |
K. Y. Srinivasan | 3ca07cb | 2011-03-10 14:07:21 -0800 | [diff] [blame] | 678 | int vmbus_child_device_register(struct hv_device *child_device_obj) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 679 | { |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 680 | int ret = 0; |
K. Y. Srinivasan | 6bad88d | 2011-03-07 13:35:48 -0800 | [diff] [blame] | 681 | |
Bill Pemberton | f488841 | 2009-07-29 17:00:12 -0400 | [diff] [blame] | 682 | static atomic_t device_num = ATOMIC_INIT(0); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 683 | |
Haiyang Zhang | 1bb40a2 | 2009-10-23 18:14:24 +0000 | [diff] [blame] | 684 | /* Set the device name. Otherwise, device_register() will fail. */ |
K. Y. Srinivasan | 6bad88d | 2011-03-07 13:35:48 -0800 | [diff] [blame] | 685 | dev_set_name(&child_device_obj->device, "vmbus_0_%d", |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 686 | atomic_inc_return(&device_num)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 687 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 688 | /* The new device belongs to this bus */ |
K. Y. Srinivasan | 04677c0 | 2011-03-15 15:03:35 -0700 | [diff] [blame] | 689 | child_device_obj->device.bus = &hv_bus.bus; /* device->dev.bus; */ |
K. Y. Srinivasan | 800b690 | 2011-03-15 15:03:33 -0700 | [diff] [blame] | 690 | child_device_obj->device.parent = &hv_pci_dev->dev; |
K. Y. Srinivasan | 6bad88d | 2011-03-07 13:35:48 -0800 | [diff] [blame] | 691 | child_device_obj->device.release = vmbus_device_release; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 692 | |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 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 | */ |
K. Y. Srinivasan | 6bad88d | 2011-03-07 13:35:48 -0800 | [diff] [blame] | 697 | ret = device_register(&child_device_obj->device); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 698 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 699 | if (ret) |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 700 | pr_err("Unable to register child device\n"); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 701 | else |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 702 | pr_info("child device %s registered\n", |
| 703 | dev_name(&child_device_obj->device)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 704 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 705 | return ret; |
| 706 | } |
| 707 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 708 | /* |
| 709 | * vmbus_child_device_unregister - Remove the specified child device |
| 710 | * from the vmbus. |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 711 | */ |
Greg Kroah-Hartman | 9d8bd71 | 2010-12-02 08:34:45 -0800 | [diff] [blame] | 712 | void vmbus_child_device_unregister(struct hv_device *device_obj) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 713 | { |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 714 | /* |
| 715 | * Kick off the process of unregistering the device. |
| 716 | * This will call vmbus_remove() and eventually vmbus_device_release() |
| 717 | */ |
K. Y. Srinivasan | 6bad88d | 2011-03-07 13:35:48 -0800 | [diff] [blame] | 718 | device_unregister(&device_obj->device); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 719 | |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 720 | pr_info("child device %s unregistered\n", |
| 721 | dev_name(&device_obj->device)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 722 | } |
| 723 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 724 | |
K. Y. Srinivasan | 1168ac2 | 2011-03-15 15:03:32 -0700 | [diff] [blame] | 725 | static int __devinit hv_pci_probe(struct pci_dev *pdev, |
| 726 | const struct pci_device_id *ent) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 727 | { |
K. Y. Srinivasan | 1168ac2 | 2011-03-15 15:03:32 -0700 | [diff] [blame] | 728 | hv_pci_dev = pdev; |
Greg Kroah-Hartman | c22090f | 2010-02-25 16:43:15 -0800 | [diff] [blame] | 729 | |
K. Y. Srinivasan | 71a6655 | 2011-04-29 13:45:04 -0700 | [diff] [blame] | 730 | pci_probe_error = pci_enable_device(pdev); |
| 731 | if (pci_probe_error) |
| 732 | goto probe_cleanup; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 733 | |
K. Y. Srinivasan | 71a6655 | 2011-04-29 13:45:04 -0700 | [diff] [blame] | 734 | pci_probe_error = vmbus_bus_init(pdev); |
| 735 | if (pci_probe_error) |
K. Y. Srinivasan | 1168ac2 | 2011-03-15 15:03:32 -0700 | [diff] [blame] | 736 | pci_disable_device(pdev); |
| 737 | |
K. Y. Srinivasan | 71a6655 | 2011-04-29 13:45:04 -0700 | [diff] [blame] | 738 | probe_cleanup: |
| 739 | complete(&probe_event); |
| 740 | return pci_probe_error; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 741 | } |
| 742 | |
Greg Kroah-Hartman | 9a775db | 2010-02-25 16:42:10 -0800 | [diff] [blame] | 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. |
Greg Kroah-Hartman | 9a775db | 2010-02-25 16:42:10 -0800 | [diff] [blame] | 748 | */ |
Tobias Klauser | 15f0beb | 2010-05-20 10:39:38 +0200 | [diff] [blame] | 749 | static const struct pci_device_id microsoft_hv_pci_table[] = { |
Greg Kroah-Hartman | 9a775db | 2010-02-25 16:42:10 -0800 | [diff] [blame] | 750 | { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */ |
| 751 | { 0 } |
| 752 | }; |
| 753 | MODULE_DEVICE_TABLE(pci, microsoft_hv_pci_table); |
| 754 | |
K. Y. Srinivasan | 1168ac2 | 2011-03-15 15:03:32 -0700 | [diff] [blame] | 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 | { |
K. Y. Srinivasan | 71a6655 | 2011-04-29 13:45:04 -0700 | [diff] [blame] | 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; |
K. Y. Srinivasan | 1168ac2 | 2011-03-15 15:03:32 -0700 | [diff] [blame] | 778 | } |
| 779 | |
K. Y. Srinivasan | 1168ac2 | 2011-03-15 15:03:32 -0700 | [diff] [blame] | 780 | |
Greg Kroah-Hartman | 90c9960 | 2009-09-02 07:11:14 -0700 | [diff] [blame] | 781 | MODULE_LICENSE("GPL"); |
Hank Janssen | 26c14cc | 2010-02-11 23:02:42 +0000 | [diff] [blame] | 782 | MODULE_VERSION(HV_DRV_VERSION); |
Olaf Hering | 13399cb | 2011-04-16 18:50:42 +0200 | [diff] [blame] | 783 | module_param(vmbus_loglevel, int, S_IRUGO|S_IWUSR); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 784 | |
K. Y. Srinivasan | 1168ac2 | 2011-03-15 15:03:32 -0700 | [diff] [blame] | 785 | module_init(hv_pci_init); |