blob: 5243d9802f7239999985e70a6cd8264ee66ab8ac [file] [log] [blame]
Hank Janssen3e7ee492009-07-13 16:02:34 -07001/*
Hank Janssen3e7ee492009-07-13 16:02:34 -07002 * Copyright (c) 2009, Microsoft Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
16 *
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
K. Y. Srinivasan52e5c1c2011-03-15 15:03:34 -070020 *
21 * 3/9/2011: K. Y. Srinivasan - Significant restructuring and cleanup
Hank Janssen3e7ee492009-07-13 16:02:34 -070022 */
Hank Janssen3e7ee492009-07-13 16:02:34 -070023#include <linux/init.h>
24#include <linux/module.h>
25#include <linux/device.h>
26#include <linux/irq.h>
27#include <linux/interrupt.h>
28#include <linux/sysctl.h>
Greg Kroah-Hartman9a775db2010-02-25 16:42:10 -080029#include <linux/pci.h>
Greg Kroah-Hartmanc22090f2010-02-25 16:43:15 -080030#include <linux/dmi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +000032#include <linux/completion.h>
Greg Kroah-Hartman2d82f6c2010-05-05 22:52:28 -070033#include "version_info.h"
K. Y. Srinivasane3fe0bb2011-02-11 10:00:12 -080034#include "hv_api.h"
Greg Kroah-Hartman645954c2009-08-28 16:22:59 -070035#include "logging.h"
Greg Kroah-Hartman870cde82009-08-19 16:21:28 -070036#include "vmbus.h"
Greg Kroah-Hartman150b19d2010-10-20 16:07:11 -070037#include "channel.h"
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -080038#include "vmbus_private.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070039
Hank Janssen3e7ee492009-07-13 16:02:34 -070040
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -070041struct pci_dev *hv_pci_dev;
42
Bill Pemberton454f18a2009-07-27 16:47:24 -040043/* Main vmbus driver data structure */
K. Y. Srinivasan04677c02011-03-15 15:03:35 -070044struct hv_bus {
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -070045 struct bus_type bus;
46 struct tasklet_struct msg_dpc;
47 struct tasklet_struct event_dpc;
Hank Janssen3e7ee492009-07-13 16:02:34 -070048};
49
Hank Janssen3e7ee492009-07-13 16:02:34 -070050static int vmbus_match(struct device *device, struct device_driver *driver);
51static int vmbus_probe(struct device *device);
52static int vmbus_remove(struct device *device);
53static void vmbus_shutdown(struct device *device);
Hank Janssen3e7ee492009-07-13 16:02:34 -070054
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -070055static irqreturn_t vmbus_isr(int irq, void *dev_id);
Hank Janssen3e7ee492009-07-13 16:02:34 -070056
57static void vmbus_device_release(struct device *device);
Hank Janssen3e7ee492009-07-13 16:02:34 -070058
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -070059static ssize_t vmbus_show_device_attr(struct device *dev,
60 struct device_attribute *dev_attr,
61 char *buf);
Hank Janssen3e7ee492009-07-13 16:02:34 -070062
Hank Janssen3e7ee492009-07-13 16:02:34 -070063
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -070064unsigned int vmbus_loglevel = (ALL_MODULES << 16 | INFO_LVL);
Hank Janssen3e7ee492009-07-13 16:02:34 -070065EXPORT_SYMBOL(vmbus_loglevel);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -070066 /* (ALL_MODULES << 16 | DEBUG_LVL_ENTEREXIT); */
67 /* (((VMBUS | VMBUS_DRV)<<16) | DEBUG_LVL_ENTEREXIT); */
Hank Janssen3e7ee492009-07-13 16:02:34 -070068
Hank Janssen3e7ee492009-07-13 16:02:34 -070069
Bill Pemberton454f18a2009-07-27 16:47:24 -040070/* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
Hank Janssen3e7ee492009-07-13 16:02:34 -070071static struct device_attribute vmbus_device_attrs[] = {
72 __ATTR(id, S_IRUGO, vmbus_show_device_attr, NULL),
73 __ATTR(state, S_IRUGO, vmbus_show_device_attr, NULL),
74 __ATTR(class_id, S_IRUGO, vmbus_show_device_attr, NULL),
75 __ATTR(device_id, S_IRUGO, vmbus_show_device_attr, NULL),
76 __ATTR(monitor_id, S_IRUGO, vmbus_show_device_attr, NULL),
77
78 __ATTR(server_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL),
79 __ATTR(server_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL),
80 __ATTR(server_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL),
81
82 __ATTR(client_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL),
83 __ATTR(client_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL),
84 __ATTR(client_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL),
85
86 __ATTR(out_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL),
87 __ATTR(out_read_index, S_IRUGO, vmbus_show_device_attr, NULL),
88 __ATTR(out_write_index, S_IRUGO, vmbus_show_device_attr, NULL),
89 __ATTR(out_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
90 __ATTR(out_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
91
92 __ATTR(in_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL),
93 __ATTR(in_read_index, S_IRUGO, vmbus_show_device_attr, NULL),
94 __ATTR(in_write_index, S_IRUGO, vmbus_show_device_attr, NULL),
95 __ATTR(in_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
96 __ATTR(in_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
97 __ATTR_NULL
98};
Hank Janssen3e7ee492009-07-13 16:02:34 -070099
K. Y. Srinivasanadde2482011-03-15 15:03:37 -0700100/*
101 * vmbus_uevent - add uevent for our device
102 *
103 * This routine is invoked when a device is added or removed on the vmbus to
104 * generate a uevent to udev in the userspace. The udev will then look at its
105 * rule and the uevent generated here to load the appropriate driver
106 */
107static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
108{
109 struct hv_device *dev = device_to_hv_device(device);
110 int ret;
111
112 DPRINT_INFO(VMBUS_DRV, "generating uevent - VMBUS_DEVICE_CLASS_GUID={"
113 "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
114 "%02x%02x%02x%02x%02x%02x%02x%02x}",
115 dev->dev_type.data[3], dev->dev_type.data[2],
116 dev->dev_type.data[1], dev->dev_type.data[0],
117 dev->dev_type.data[5], dev->dev_type.data[4],
118 dev->dev_type.data[7], dev->dev_type.data[6],
119 dev->dev_type.data[8], dev->dev_type.data[9],
120 dev->dev_type.data[10],
121 dev->dev_type.data[11],
122 dev->dev_type.data[12],
123 dev->dev_type.data[13],
124 dev->dev_type.data[14],
125 dev->dev_type.data[15]);
126
127 ret = add_uevent_var(env, "VMBUS_DEVICE_CLASS_GUID={"
128 "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
129 "%02x%02x%02x%02x%02x%02x%02x%02x}",
130 dev->dev_type.data[3],
131 dev->dev_type.data[2],
132 dev->dev_type.data[1],
133 dev->dev_type.data[0],
134 dev->dev_type.data[5],
135 dev->dev_type.data[4],
136 dev->dev_type.data[7],
137 dev->dev_type.data[6],
138 dev->dev_type.data[8],
139 dev->dev_type.data[9],
140 dev->dev_type.data[10],
141 dev->dev_type.data[11],
142 dev->dev_type.data[12],
143 dev->dev_type.data[13],
144 dev->dev_type.data[14],
145 dev->dev_type.data[15]);
146
147 if (ret)
148 return ret;
149
150 ret = add_uevent_var(env, "VMBUS_DEVICE_DEVICE_GUID={"
151 "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
152 "%02x%02x%02x%02x%02x%02x%02x%02x}",
153 dev->dev_instance.data[3],
154 dev->dev_instance.data[2],
155 dev->dev_instance.data[1],
156 dev->dev_instance.data[0],
157 dev->dev_instance.data[5],
158 dev->dev_instance.data[4],
159 dev->dev_instance.data[7],
160 dev->dev_instance.data[6],
161 dev->dev_instance.data[8],
162 dev->dev_instance.data[9],
163 dev->dev_instance.data[10],
164 dev->dev_instance.data[11],
165 dev->dev_instance.data[12],
166 dev->dev_instance.data[13],
167 dev->dev_instance.data[14],
168 dev->dev_instance.data[15]);
169 if (ret)
170 return ret;
171
172 return 0;
173}
174
Bill Pemberton454f18a2009-07-27 16:47:24 -0400175/* The one and only one */
K. Y. Srinivasan04677c02011-03-15 15:03:35 -0700176static struct hv_bus hv_bus = {
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700177 .bus.name = "vmbus",
178 .bus.match = vmbus_match,
179 .bus.shutdown = vmbus_shutdown,
180 .bus.remove = vmbus_remove,
181 .bus.probe = vmbus_probe,
182 .bus.uevent = vmbus_uevent,
183 .bus.dev_attrs = vmbus_device_attrs,
Hank Janssen3e7ee492009-07-13 16:02:34 -0700184};
185
Haiyang Zhangadf874c2011-01-26 12:12:09 -0800186static const char *driver_name = "hyperv";
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800187
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800188
Timo Teräsbf6506f2010-12-15 20:48:08 +0200189struct onmessage_work_context {
190 struct work_struct work;
191 struct hv_message msg;
192};
193
194static void vmbus_onmessage_work(struct work_struct *work)
195{
196 struct onmessage_work_context *ctx;
197
198 ctx = container_of(work, struct onmessage_work_context,
199 work);
200 vmbus_onmessage(&ctx->msg);
201 kfree(ctx);
202}
203
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800204/*
205 * vmbus_on_msg_dpc - DPC routine to handle messages from the hypervisior
206 */
K. Y. Srinivasan62c10592011-03-10 14:04:53 -0800207static void vmbus_on_msg_dpc(unsigned long data)
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800208{
209 int cpu = smp_processor_id();
210 void *page_addr = hv_context.synic_message_page[cpu];
211 struct hv_message *msg = (struct hv_message *)page_addr +
212 VMBUS_MESSAGE_SINT;
Timo Teräsbf6506f2010-12-15 20:48:08 +0200213 struct onmessage_work_context *ctx;
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800214
215 while (1) {
216 if (msg->header.message_type == HVMSG_NONE) {
217 /* no msg */
218 break;
219 } else {
Timo Teräsbf6506f2010-12-15 20:48:08 +0200220 ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
221 if (ctx == NULL)
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800222 continue;
Timo Teräsbf6506f2010-12-15 20:48:08 +0200223 INIT_WORK(&ctx->work, vmbus_onmessage_work);
224 memcpy(&ctx->msg, msg, sizeof(*msg));
Haiyang Zhangda9fcb72011-01-26 12:12:14 -0800225 queue_work(vmbus_connection.work_queue, &ctx->work);
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800226 }
227
228 msg->header.message_type = HVMSG_NONE;
229
230 /*
231 * Make sure the write to MessageType (ie set to
232 * HVMSG_NONE) happens before we read the
233 * MessagePending and EOMing. Otherwise, the EOMing
234 * will not deliver any more messages since there is
235 * no empty slot
236 */
237 mb();
238
239 if (msg->header.message_flags.msg_pending) {
240 /*
241 * This will cause message queue rescan to
242 * possibly deliver another msg from the
243 * hypervisor
244 */
245 wrmsrl(HV_X64_MSR_EOM, 0);
246 }
247 }
248}
249
250/*
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800251 * vmbus_on_isr - ISR routine
252 */
K. Y. Srinivasan480ae582011-03-10 14:06:06 -0800253static int vmbus_on_isr(void)
Greg Kroah-Hartman36199a92010-12-02 11:59:22 -0800254{
255 int ret = 0;
256 int cpu = smp_processor_id();
257 void *page_addr;
258 struct hv_message *msg;
259 union hv_synic_event_flags *event;
260
261 page_addr = hv_context.synic_message_page[cpu];
262 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
263
264 /* Check if there are actual msgs to be process */
265 if (msg->header.message_type != HVMSG_NONE) {
266 DPRINT_DBG(VMBUS, "received msg type %d size %d",
267 msg->header.message_type,
268 msg->header.payload_size);
269 ret |= 0x1;
270 }
271
272 /* TODO: Check if there are events to be process */
273 page_addr = hv_context.synic_event_page[cpu];
274 event = (union hv_synic_event_flags *)page_addr + VMBUS_MESSAGE_SINT;
275
276 /* Since we are a child, we only need to check bit 0 */
277 if (test_and_clear_bit(0, (unsigned long *) &event->flags32[0])) {
278 DPRINT_DBG(VMBUS, "received event %d", event->flags32[0]);
279 ret |= 0x2;
280 }
281
282 return ret;
283}
284
Greg Kroah-Hartman150b19d2010-10-20 16:07:11 -0700285static void get_channel_info(struct hv_device *device,
286 struct hv_device_info *info)
287{
288 struct vmbus_channel_debug_info debug_info;
289
Greg Kroah-Hartmancae5b842010-10-21 09:05:27 -0700290 if (!device->channel)
Greg Kroah-Hartman150b19d2010-10-20 16:07:11 -0700291 return;
292
Greg Kroah-Hartmancae5b842010-10-21 09:05:27 -0700293 vmbus_get_debug_info(device->channel, &debug_info);
Greg Kroah-Hartman150b19d2010-10-20 16:07:11 -0700294
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800295 info->chn_id = debug_info.relid;
296 info->chn_state = debug_info.state;
297 memcpy(&info->chn_type, &debug_info.interfacetype,
Greg Kroah-Hartman150b19d2010-10-20 16:07:11 -0700298 sizeof(struct hv_guid));
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800299 memcpy(&info->chn_instance, &debug_info.interface_instance,
Greg Kroah-Hartman150b19d2010-10-20 16:07:11 -0700300 sizeof(struct hv_guid));
301
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800302 info->monitor_id = debug_info.monitorid;
Greg Kroah-Hartman150b19d2010-10-20 16:07:11 -0700303
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800304 info->server_monitor_pending = debug_info.servermonitor_pending;
305 info->server_monitor_latency = debug_info.servermonitor_latency;
306 info->server_monitor_conn_id = debug_info.servermonitor_connectionid;
Greg Kroah-Hartman150b19d2010-10-20 16:07:11 -0700307
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800308 info->client_monitor_pending = debug_info.clientmonitor_pending;
309 info->client_monitor_latency = debug_info.clientmonitor_latency;
310 info->client_monitor_conn_id = debug_info.clientmonitor_connectionid;
Greg Kroah-Hartman150b19d2010-10-20 16:07:11 -0700311
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800312 info->inbound.int_mask = debug_info.inbound.current_interrupt_mask;
313 info->inbound.read_idx = debug_info.inbound.current_read_index;
314 info->inbound.write_idx = debug_info.inbound.current_write_index;
315 info->inbound.bytes_avail_toread =
316 debug_info.inbound.bytes_avail_toread;
317 info->inbound.bytes_avail_towrite =
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800318 debug_info.inbound.bytes_avail_towrite;
Greg Kroah-Hartman150b19d2010-10-20 16:07:11 -0700319
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800320 info->outbound.int_mask =
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800321 debug_info.outbound.current_interrupt_mask;
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800322 info->outbound.read_idx = debug_info.outbound.current_read_index;
323 info->outbound.write_idx = debug_info.outbound.current_write_index;
324 info->outbound.bytes_avail_toread =
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800325 debug_info.outbound.bytes_avail_toread;
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800326 info->outbound.bytes_avail_towrite =
Haiyang Zhang82f8bd42010-11-08 14:04:45 -0800327 debug_info.outbound.bytes_avail_towrite;
Greg Kroah-Hartman150b19d2010-10-20 16:07:11 -0700328}
329
Hank Janssen3e189512010-03-04 22:11:00 +0000330/*
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700331 * vmbus_show_device_attr - Show the device attribute in sysfs.
332 *
333 * This is invoked when user does a
334 * "cat /sys/bus/vmbus/devices/<busdevice>/<attr name>"
335 */
336static ssize_t vmbus_show_device_attr(struct device *dev,
337 struct device_attribute *dev_attr,
338 char *buf)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700339{
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800340 struct hv_device *device_ctx = device_to_hv_device(dev);
Greg Kroah-Hartmanee3d7dd2009-08-20 12:17:36 -0700341 struct hv_device_info device_info;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700342
Greg Kroah-Hartmanee3d7dd2009-08-20 12:17:36 -0700343 memset(&device_info, 0, sizeof(struct hv_device_info));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700344
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800345 get_channel_info(device_ctx, &device_info);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700346
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700347 if (!strcmp(dev_attr->attr.name, "class_id")) {
348 return sprintf(buf, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-"
349 "%02x%02x%02x%02x%02x%02x%02x%02x}\n",
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800350 device_info.chn_type.data[3],
351 device_info.chn_type.data[2],
352 device_info.chn_type.data[1],
353 device_info.chn_type.data[0],
354 device_info.chn_type.data[5],
355 device_info.chn_type.data[4],
356 device_info.chn_type.data[7],
357 device_info.chn_type.data[6],
358 device_info.chn_type.data[8],
359 device_info.chn_type.data[9],
360 device_info.chn_type.data[10],
361 device_info.chn_type.data[11],
362 device_info.chn_type.data[12],
363 device_info.chn_type.data[13],
364 device_info.chn_type.data[14],
365 device_info.chn_type.data[15]);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700366 } else if (!strcmp(dev_attr->attr.name, "device_id")) {
367 return sprintf(buf, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-"
368 "%02x%02x%02x%02x%02x%02x%02x%02x}\n",
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800369 device_info.chn_instance.data[3],
370 device_info.chn_instance.data[2],
371 device_info.chn_instance.data[1],
372 device_info.chn_instance.data[0],
373 device_info.chn_instance.data[5],
374 device_info.chn_instance.data[4],
375 device_info.chn_instance.data[7],
376 device_info.chn_instance.data[6],
377 device_info.chn_instance.data[8],
378 device_info.chn_instance.data[9],
379 device_info.chn_instance.data[10],
380 device_info.chn_instance.data[11],
381 device_info.chn_instance.data[12],
382 device_info.chn_instance.data[13],
383 device_info.chn_instance.data[14],
384 device_info.chn_instance.data[15]);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700385 } else if (!strcmp(dev_attr->attr.name, "state")) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800386 return sprintf(buf, "%d\n", device_info.chn_state);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700387 } else if (!strcmp(dev_attr->attr.name, "id")) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800388 return sprintf(buf, "%d\n", device_info.chn_id);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700389 } else if (!strcmp(dev_attr->attr.name, "out_intr_mask")) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800390 return sprintf(buf, "%d\n", device_info.outbound.int_mask);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700391 } else if (!strcmp(dev_attr->attr.name, "out_read_index")) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800392 return sprintf(buf, "%d\n", device_info.outbound.read_idx);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700393 } else if (!strcmp(dev_attr->attr.name, "out_write_index")) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800394 return sprintf(buf, "%d\n", device_info.outbound.write_idx);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700395 } else if (!strcmp(dev_attr->attr.name, "out_read_bytes_avail")) {
396 return sprintf(buf, "%d\n",
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800397 device_info.outbound.bytes_avail_toread);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700398 } else if (!strcmp(dev_attr->attr.name, "out_write_bytes_avail")) {
399 return sprintf(buf, "%d\n",
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800400 device_info.outbound.bytes_avail_towrite);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700401 } else if (!strcmp(dev_attr->attr.name, "in_intr_mask")) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800402 return sprintf(buf, "%d\n", device_info.inbound.int_mask);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700403 } else if (!strcmp(dev_attr->attr.name, "in_read_index")) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800404 return sprintf(buf, "%d\n", device_info.inbound.read_idx);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700405 } else if (!strcmp(dev_attr->attr.name, "in_write_index")) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800406 return sprintf(buf, "%d\n", device_info.inbound.write_idx);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700407 } else if (!strcmp(dev_attr->attr.name, "in_read_bytes_avail")) {
408 return sprintf(buf, "%d\n",
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800409 device_info.inbound.bytes_avail_toread);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700410 } else if (!strcmp(dev_attr->attr.name, "in_write_bytes_avail")) {
411 return sprintf(buf, "%d\n",
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800412 device_info.inbound.bytes_avail_towrite);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700413 } else if (!strcmp(dev_attr->attr.name, "monitor_id")) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800414 return sprintf(buf, "%d\n", device_info.monitor_id);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700415 } else if (!strcmp(dev_attr->attr.name, "server_monitor_pending")) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800416 return sprintf(buf, "%d\n", device_info.server_monitor_pending);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700417 } else if (!strcmp(dev_attr->attr.name, "server_monitor_latency")) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800418 return sprintf(buf, "%d\n", device_info.server_monitor_latency);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700419 } else if (!strcmp(dev_attr->attr.name, "server_monitor_conn_id")) {
420 return sprintf(buf, "%d\n",
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800421 device_info.server_monitor_conn_id);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700422 } else if (!strcmp(dev_attr->attr.name, "client_monitor_pending")) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800423 return sprintf(buf, "%d\n", device_info.client_monitor_pending);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700424 } else if (!strcmp(dev_attr->attr.name, "client_monitor_latency")) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800425 return sprintf(buf, "%d\n", device_info.client_monitor_latency);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700426 } else if (!strcmp(dev_attr->attr.name, "client_monitor_conn_id")) {
427 return sprintf(buf, "%d\n",
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800428 device_info.client_monitor_conn_id);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700429 } else {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700430 return 0;
431 }
432}
433
Hank Janssen3e189512010-03-04 22:11:00 +0000434/*
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700435 * vmbus_bus_init -Main vmbus driver initialization routine.
436 *
437 * Here, we
Lars Lindley0686e4f2010-03-11 23:51:23 +0100438 * - initialize the vmbus driver context
Lars Lindley0686e4f2010-03-11 23:51:23 +0100439 * - invoke the vmbus hv main init routine
440 * - get the irq resource
Lars Lindley0686e4f2010-03-11 23:51:23 +0100441 * - retrieve the channel offers
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700442 */
K. Y. Srinivasan52e5c1c2011-03-15 15:03:34 -0700443static int vmbus_bus_init(struct pci_dev *pdev)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700444{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700445 int ret;
446 unsigned int vector;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700447
Greg Kroah-Hartman6d26e38fa22010-12-02 12:08:08 -0800448 DPRINT_INFO(VMBUS, "+++++++ HV Driver version = %s +++++++",
449 HV_DRV_VERSION);
450 DPRINT_INFO(VMBUS, "+++++++ Vmbus supported version = %d +++++++",
451 VMBUS_REVISION_NUMBER);
452 DPRINT_INFO(VMBUS, "+++++++ Vmbus using SINT %d +++++++",
453 VMBUS_MESSAGE_SINT);
454 DPRINT_DBG(VMBUS, "sizeof(vmbus_channel_packet_page_buffer)=%zd, "
455 "sizeof(VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER)=%zd",
456 sizeof(struct vmbus_channel_packet_page_buffer),
457 sizeof(struct vmbus_channel_packet_multipage_buffer));
458
Greg Kroah-Hartman6d26e38fa22010-12-02 12:08:08 -0800459
460 /* Hypervisor initialization...setup hypercall page..etc */
461 ret = hv_init();
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700462 if (ret != 0) {
Greg Kroah-Hartman6d26e38fa22010-12-02 12:08:08 -0800463 DPRINT_ERR(VMBUS, "Unable to initialize the hypervisor - 0x%x",
464 ret);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700465 goto cleanup;
466 }
467
Hank Janssen3e7ee492009-07-13 16:02:34 -0700468
K. Y. Srinivasan04677c02011-03-15 15:03:35 -0700469 hv_bus.bus.name = driver_name;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700470
Bill Pemberton454f18a2009-07-27 16:47:24 -0400471 /* Initialize the bus context */
K. Y. Srinivasan04677c02011-03-15 15:03:35 -0700472 tasklet_init(&hv_bus.msg_dpc, vmbus_on_msg_dpc,
K. Y. Srinivasana6e4d8e2011-03-10 14:03:51 -0800473 (unsigned long)NULL);
K. Y. Srinivasan04677c02011-03-15 15:03:35 -0700474 tasklet_init(&hv_bus.event_dpc, vmbus_on_event,
K. Y. Srinivasana6e4d8e2011-03-10 14:03:51 -0800475 (unsigned long)NULL);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700476
K. Y. Srinivasana6e4d8e2011-03-10 14:03:51 -0800477 /* Now, register the bus with LDM */
K. Y. Srinivasan04677c02011-03-15 15:03:35 -0700478 ret = bus_register(&hv_bus.bus);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700479 if (ret) {
Bill Pembertonc19fbca2009-07-27 16:47:35 -0400480 ret = -1;
481 goto cleanup;
482 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700483
Bill Pemberton454f18a2009-07-27 16:47:24 -0400484 /* Get the interrupt resource */
K. Y. Srinivasan52e5c1c2011-03-15 15:03:34 -0700485 ret = request_irq(pdev->irq, vmbus_isr,
486 IRQF_SHARED | IRQF_SAMPLE_RANDOM,
487 driver_name, pdev);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700488
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700489 if (ret != 0) {
490 DPRINT_ERR(VMBUS_DRV, "ERROR - Unable to request IRQ %d",
K. Y. Srinivasan52e5c1c2011-03-15 15:03:34 -0700491 pdev->irq);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700492
K. Y. Srinivasan04677c02011-03-15 15:03:35 -0700493 bus_unregister(&hv_bus.bus);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700494
495 ret = -1;
496 goto cleanup;
497 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700498
K. Y. Srinivasan52e5c1c2011-03-15 15:03:34 -0700499 vector = IRQ0_VECTOR + pdev->irq;
500 DPRINT_INFO(VMBUS_DRV, "irq 0x%x vector 0x%x", pdev->irq,
501 vector);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700502
K. Y. Srinivasan800b6902011-03-15 15:03:33 -0700503 /*
504 * Notify the hypervisor of our irq and
505 * connect to the host.
506 */
507 on_each_cpu(hv_synic_init, (void *)&vector, 1);
508 ret = vmbus_connect();
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700509 if (ret) {
K. Y. Srinivasan52e5c1c2011-03-15 15:03:34 -0700510 free_irq(pdev->irq, pdev);
K. Y. Srinivasan04677c02011-03-15 15:03:35 -0700511 bus_unregister(&hv_bus.bus);
Bill Pemberton5d48a1c2009-07-27 16:47:36 -0400512 goto cleanup;
513 }
514
K. Y. Srinivasan800b6902011-03-15 15:03:33 -0700515
Greg Kroah-Hartman2d6e8822010-12-02 08:50:58 -0800516 vmbus_request_offers();
Haiyang Zhang8b5d6d32010-05-28 23:22:44 +0000517 wait_for_completion(&hv_channel_ready);
518
Hank Janssen3e7ee492009-07-13 16:02:34 -0700519cleanup:
Hank Janssen3e7ee492009-07-13 16:02:34 -0700520 return ret;
521}
522
Hank Janssen3e189512010-03-04 22:11:00 +0000523/*
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700524 * vmbus_bus_exit - Terminate the vmbus driver.
525 *
526 * This routine is opposite of vmbus_bus_init()
527 */
Greg Kroah-Hartmanbd1de702009-07-29 09:04:51 -0700528static void vmbus_bus_exit(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700529{
Hank Janssen3e7ee492009-07-13 16:02:34 -0700530
Hank Janssen3e7ee492009-07-13 16:02:34 -0700531
K. Y. Srinivasanf51b3592011-03-10 14:06:31 -0800532 vmbus_release_unattached_channels();
533 vmbus_disconnect();
534 on_each_cpu(hv_synic_cleanup, NULL, 1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700535
K. Y. Srinivasanece0e322011-03-10 14:06:55 -0800536 hv_cleanup();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700537
K. Y. Srinivasan04677c02011-03-15 15:03:35 -0700538 bus_unregister(&hv_bus.bus);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700539
K. Y. Srinivasan52e5c1c2011-03-15 15:03:34 -0700540 free_irq(hv_pci_dev->irq, hv_pci_dev);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700541
K. Y. Srinivasan04677c02011-03-15 15:03:35 -0700542 tasklet_kill(&hv_bus.msg_dpc);
543 tasklet_kill(&hv_bus.event_dpc);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700544}
545
Hank Janssen3e189512010-03-04 22:11:00 +0000546
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700547/**
Hank Janssen3e189512010-03-04 22:11:00 +0000548 * vmbus_child_driver_register() - Register a vmbus's child driver
K. Y. Srinivasanc643269d2011-03-07 13:23:43 -0800549 * @drv: Pointer to driver structure you want to register
Hank Janssen3e189512010-03-04 22:11:00 +0000550 *
Hank Janssen3e189512010-03-04 22:11:00 +0000551 *
552 * Registers the given driver with Linux through the 'driver_register()' call
553 * And sets up the hyper-v vmbus handling for this driver.
554 * It will return the state of the 'driver_register()' call.
555 *
556 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700557 */
K. Y. Srinivasanc643269d2011-03-07 13:23:43 -0800558int vmbus_child_driver_register(struct device_driver *drv)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700559{
Bill Pemberton5d48a1c2009-07-27 16:47:36 -0400560 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700561
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700562 DPRINT_INFO(VMBUS_DRV, "child driver (%p) registering - name %s",
K. Y. Srinivasanc643269d2011-03-07 13:23:43 -0800563 drv, drv->name);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700564
Bill Pemberton454f18a2009-07-27 16:47:24 -0400565 /* The child driver on this vmbus */
K. Y. Srinivasan04677c02011-03-15 15:03:35 -0700566 drv->bus = &hv_bus.bus;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700567
K. Y. Srinivasanc643269d2011-03-07 13:23:43 -0800568 ret = driver_register(drv);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700569
Greg Kroah-Hartman2d6e8822010-12-02 08:50:58 -0800570 vmbus_request_offers();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700571
Bill Pemberton5d48a1c2009-07-27 16:47:36 -0400572 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700573}
Hank Janssen3e7ee492009-07-13 16:02:34 -0700574EXPORT_SYMBOL(vmbus_child_driver_register);
575
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700576/**
Hank Janssen3e189512010-03-04 22:11:00 +0000577 * vmbus_child_driver_unregister() - Unregister a vmbus's child driver
K. Y. Srinivasan06de23f2011-03-07 13:24:23 -0800578 * @drv: Pointer to driver structure you want to un-register
Hank Janssen3e189512010-03-04 22:11:00 +0000579 *
Hank Janssen3e189512010-03-04 22:11:00 +0000580 *
581 * Un-register the given driver with Linux through the 'driver_unregister()'
582 * call. And ungegisters the driver from the Hyper-V vmbus handler.
583 *
584 * Mainly used by Hyper-V drivers.
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700585 */
K. Y. Srinivasan06de23f2011-03-07 13:24:23 -0800586void vmbus_child_driver_unregister(struct device_driver *drv)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700587{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700588 DPRINT_INFO(VMBUS_DRV, "child driver (%p) unregistering - name %s",
K. Y. Srinivasan06de23f2011-03-07 13:24:23 -0800589 drv, drv->name);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700590
K. Y. Srinivasan06de23f2011-03-07 13:24:23 -0800591 driver_unregister(drv);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700592
K. Y. Srinivasan06de23f2011-03-07 13:24:23 -0800593 drv->bus = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700594}
Hank Janssen3e7ee492009-07-13 16:02:34 -0700595EXPORT_SYMBOL(vmbus_child_driver_unregister);
596
Hank Janssen3e189512010-03-04 22:11:00 +0000597/*
Hank Janssen3e189512010-03-04 22:11:00 +0000598 * vmbus_child_device_create - Creates and registers a new child device
599 * on the vmbus.
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700600 */
Greg Kroah-Hartman89733aa2010-12-02 08:22:41 -0800601struct hv_device *vmbus_child_device_create(struct hv_guid *type,
602 struct hv_guid *instance,
603 struct vmbus_channel *channel)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700604{
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200605 struct hv_device *child_device_obj;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700606
Bill Pemberton454f18a2009-07-27 16:47:24 -0400607 /* Allocate the new child device */
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800608 child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
609 if (!child_device_obj) {
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700610 DPRINT_ERR(VMBUS_DRV,
611 "unable to allocate device_context for child device");
Hank Janssen3e7ee492009-07-13 16:02:34 -0700612 return NULL;
613 }
614
615 DPRINT_DBG(VMBUS_DRV, "child device (%p) allocated - "
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700616 "type {%02x%02x%02x%02x-%02x%02x-%02x%02x-"
617 "%02x%02x%02x%02x%02x%02x%02x%02x},"
618 "id {%02x%02x%02x%02x-%02x%02x-%02x%02x-"
619 "%02x%02x%02x%02x%02x%02x%02x%02x}",
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800620 &child_device_obj->device,
Greg Kroah-Hartmandaaa8cc2009-08-19 16:18:56 -0700621 type->data[3], type->data[2], type->data[1], type->data[0],
622 type->data[5], type->data[4], type->data[7], type->data[6],
623 type->data[8], type->data[9], type->data[10], type->data[11],
624 type->data[12], type->data[13], type->data[14], type->data[15],
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700625 instance->data[3], instance->data[2],
626 instance->data[1], instance->data[0],
627 instance->data[5], instance->data[4],
628 instance->data[7], instance->data[6],
629 instance->data[8], instance->data[9],
630 instance->data[10], instance->data[11],
631 instance->data[12], instance->data[13],
632 instance->data[14], instance->data[15]);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700633
Greg Kroah-Hartmancae5b842010-10-21 09:05:27 -0700634 child_device_obj->channel = channel;
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800635 memcpy(&child_device_obj->dev_type, type, sizeof(struct hv_guid));
636 memcpy(&child_device_obj->dev_instance, instance,
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700637 sizeof(struct hv_guid));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700638
Hank Janssen3e7ee492009-07-13 16:02:34 -0700639
Hank Janssen3e7ee492009-07-13 16:02:34 -0700640 return child_device_obj;
641}
642
Hank Janssen3e189512010-03-04 22:11:00 +0000643/*
K. Y. Srinivasane13a0b52011-03-10 14:07:44 -0800644 * vmbus_child_device_register - Register the child device
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700645 */
K. Y. Srinivasan3ca07cb2011-03-10 14:07:21 -0800646int vmbus_child_device_register(struct hv_device *child_device_obj)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700647{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700648 int ret = 0;
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800649
Bill Pembertonf4888412009-07-29 17:00:12 -0400650 static atomic_t device_num = ATOMIC_INIT(0);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700651
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700652 DPRINT_DBG(VMBUS_DRV, "child device (%p) registering",
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800653 child_device_obj);
Bill Pemberton454f18a2009-07-27 16:47:24 -0400654
Haiyang Zhang1bb40a22009-10-23 18:14:24 +0000655 /* Set the device name. Otherwise, device_register() will fail. */
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800656 dev_set_name(&child_device_obj->device, "vmbus_0_%d",
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700657 atomic_inc_return(&device_num));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700658
Bill Pemberton454f18a2009-07-27 16:47:24 -0400659 /* The new device belongs to this bus */
K. Y. Srinivasan04677c02011-03-15 15:03:35 -0700660 child_device_obj->device.bus = &hv_bus.bus; /* device->dev.bus; */
K. Y. Srinivasan800b6902011-03-15 15:03:33 -0700661 child_device_obj->device.parent = &hv_pci_dev->dev;
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800662 child_device_obj->device.release = vmbus_device_release;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700663
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700664 /*
665 * Register with the LDM. This will kick off the driver/device
666 * binding...which will eventually call vmbus_match() and vmbus_probe()
667 */
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800668 ret = device_register(&child_device_obj->device);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700669
Bill Pemberton454f18a2009-07-27 16:47:24 -0400670 /* vmbus_probe() error does not get propergate to device_register(). */
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800671 ret = child_device_obj->probe_error;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700672
673 if (ret)
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700674 DPRINT_ERR(VMBUS_DRV, "unable to register child device (%p)",
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800675 &child_device_obj->device);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700676 else
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700677 DPRINT_INFO(VMBUS_DRV, "child device (%p) registered",
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800678 &child_device_obj->device);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700679
Hank Janssen3e7ee492009-07-13 16:02:34 -0700680 return ret;
681}
682
Hank Janssen3e189512010-03-04 22:11:00 +0000683/*
684 * vmbus_child_device_unregister - Remove the specified child device
685 * from the vmbus.
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700686 */
Greg Kroah-Hartman9d8bd712010-12-02 08:34:45 -0800687void vmbus_child_device_unregister(struct hv_device *device_obj)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700688{
Hank Janssen3e7ee492009-07-13 16:02:34 -0700689
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700690 DPRINT_INFO(VMBUS_DRV, "unregistering child device (%p)",
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800691 &device_obj->device);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700692
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700693 /*
694 * Kick off the process of unregistering the device.
695 * This will call vmbus_remove() and eventually vmbus_device_release()
696 */
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800697 device_unregister(&device_obj->device);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700698
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700699 DPRINT_INFO(VMBUS_DRV, "child device (%p) unregistered",
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800700 &device_obj->device);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700701}
702
Hank Janssen3e189512010-03-04 22:11:00 +0000703/*
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700704 * vmbus_match - Attempt to match the specified device to the specified driver
705 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700706static int vmbus_match(struct device *device, struct device_driver *driver)
707{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700708 int match = 0;
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800709 struct hv_driver *drv = drv_to_hv_drv(driver);
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800710 struct hv_device *device_ctx = device_to_hv_device(device);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700711
Bill Pemberton454f18a2009-07-27 16:47:24 -0400712 /* We found our driver ? */
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800713 if (memcmp(&device_ctx->dev_type, &drv->dev_type,
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700714 sizeof(struct hv_guid)) == 0) {
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700715
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800716 device_ctx->drv = drv->priv;
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700717 DPRINT_INFO(VMBUS_DRV,
718 "device object (%p) set to driver object (%p)",
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800719 &device_ctx,
720 device_ctx->drv);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700721
722 match = 1;
723 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700724 return match;
725}
726
Hank Janssen3e189512010-03-04 22:11:00 +0000727/*
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700728 * vmbus_probe_failed_cb - Callback when a driver probe failed in vmbus_probe()
729 *
730 * We need a callback because we cannot invoked device_unregister() inside
731 * vmbus_probe() since vmbus_probe() may be invoked inside device_register()
732 * i.e. we cannot call device_unregister() inside device_register()
733 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700734static void vmbus_probe_failed_cb(struct work_struct *context)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700735{
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800736 struct hv_device *device_ctx = (struct hv_device *)context;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700737
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700738 /*
739 * Kick off the process of unregistering the device.
740 * This will call vmbus_remove() and eventually vmbus_device_release()
741 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700742 device_unregister(&device_ctx->device);
743
Bill Pemberton454f18a2009-07-27 16:47:24 -0400744 /* put_device(&device_ctx->device); */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700745}
746
Hank Janssen3e189512010-03-04 22:11:00 +0000747/*
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700748 * vmbus_probe - Add the new vmbus's child device
749 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700750static int vmbus_probe(struct device *child_device)
751{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700752 int ret = 0;
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800753 struct hv_driver *drv =
754 drv_to_hv_drv(child_device->driver);
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800755 struct hv_device *dev = device_to_hv_device(child_device);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700756
Bill Pemberton454f18a2009-07-27 16:47:24 -0400757 /* Let the specific open-source driver handles the probe if it can */
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800758 if (drv->driver.probe) {
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800759 ret = dev->probe_error =
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800760 drv->driver.probe(child_device);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700761 if (ret != 0) {
762 DPRINT_ERR(VMBUS_DRV, "probe() failed for device %s "
763 "(%p) on driver %s (%d)...",
764 dev_name(child_device), child_device,
765 child_device->driver->name, ret);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700766
K. Y. Srinivasana3c7fe92011-03-07 13:34:27 -0800767 INIT_WORK(&dev->probe_failed_work_item,
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700768 vmbus_probe_failed_cb);
K. Y. Srinivasana3c7fe92011-03-07 13:34:27 -0800769 schedule_work(&dev->probe_failed_work_item);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700770 }
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700771 } else {
772 DPRINT_ERR(VMBUS_DRV, "probe() method not set for driver - %s",
773 child_device->driver->name);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700774 ret = -1;
775 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700776 return ret;
777}
778
Hank Janssen3e189512010-03-04 22:11:00 +0000779/*
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700780 * vmbus_remove - Remove a vmbus device
781 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700782static int vmbus_remove(struct device *child_device)
783{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700784 int ret;
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800785 struct hv_driver *drv;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700786
Hank Janssen3e7ee492009-07-13 16:02:34 -0700787
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700788 if (child_device->driver) {
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800789 drv = drv_to_hv_drv(child_device->driver);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700790
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700791 /*
792 * Let the specific open-source driver handles the removal if
793 * it can
794 */
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800795 if (drv->driver.remove) {
796 ret = drv->driver.remove(child_device);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700797 } else {
798 DPRINT_ERR(VMBUS_DRV,
799 "remove() method not set for driver - %s",
800 child_device->driver->name);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700801 ret = -1;
802 }
803 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700804
Hank Janssen3e7ee492009-07-13 16:02:34 -0700805 return 0;
806}
807
Hank Janssen3e189512010-03-04 22:11:00 +0000808/*
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700809 * vmbus_shutdown - Shutdown a vmbus device
810 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700811static void vmbus_shutdown(struct device *child_device)
812{
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800813 struct hv_driver *drv;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700814
Hank Janssen3e7ee492009-07-13 16:02:34 -0700815
Bill Pemberton454f18a2009-07-27 16:47:24 -0400816 /* The device may not be attached yet */
Greg Kroah-Hartman83c720e2010-07-22 15:14:04 -0700817 if (!child_device->driver)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700818 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700819
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800820 drv = drv_to_hv_drv(child_device->driver);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700821
Bill Pemberton454f18a2009-07-27 16:47:24 -0400822 /* Let the specific open-source driver handles the removal if it can */
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800823 if (drv->driver.shutdown)
824 drv->driver.shutdown(child_device);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700825
Hank Janssen3e7ee492009-07-13 16:02:34 -0700826 return;
827}
828
Hank Janssen3e7ee492009-07-13 16:02:34 -0700829
Hank Janssen3e189512010-03-04 22:11:00 +0000830/*
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700831 * vmbus_device_release - Final callback release of the vmbus child device
832 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700833static void vmbus_device_release(struct device *device)
834{
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800835 struct hv_device *device_ctx = device_to_hv_device(device);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700836
Hank Janssen3e7ee492009-07-13 16:02:34 -0700837 kfree(device_ctx);
838
Bill Pemberton454f18a2009-07-27 16:47:24 -0400839 /* !!DO NOT REFERENCE device_ctx anymore at this point!! */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700840}
841
Hank Janssen3e7ee492009-07-13 16:02:34 -0700842
Hank Janssen3e7ee492009-07-13 16:02:34 -0700843
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700844static irqreturn_t vmbus_isr(int irq, void *dev_id)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700845{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700846 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700847
K. Y. Srinivasan480ae582011-03-10 14:06:06 -0800848 ret = vmbus_on_isr();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700849
Bill Pemberton454f18a2009-07-27 16:47:24 -0400850 /* Schedules a dpc if necessary */
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700851 if (ret > 0) {
852 if (test_bit(0, (unsigned long *)&ret))
K. Y. Srinivasan04677c02011-03-15 15:03:35 -0700853 tasklet_schedule(&hv_bus.msg_dpc);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700854
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700855 if (test_bit(1, (unsigned long *)&ret))
K. Y. Srinivasan04677c02011-03-15 15:03:35 -0700856 tasklet_schedule(&hv_bus.event_dpc);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700857
Hank Janssen3e7ee492009-07-13 16:02:34 -0700858 return IRQ_HANDLED;
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700859 } else {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700860 return IRQ_NONE;
861 }
862}
863
Greg Kroah-Hartmanc22090f2010-02-25 16:43:15 -0800864
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -0700865
866static int __devinit hv_pci_probe(struct pci_dev *pdev,
867 const struct pci_device_id *ent)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700868{
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -0700869 int err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700870
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -0700871 hv_pci_dev = pdev;
Greg Kroah-Hartmanc22090f2010-02-25 16:43:15 -0800872
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -0700873 err = pci_enable_device(pdev);
874 if (err)
875 return err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700876
K. Y. Srinivasan52e5c1c2011-03-15 15:03:34 -0700877 err = vmbus_bus_init(pdev);
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -0700878 if (err)
879 pci_disable_device(pdev);
880
881 return err;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700882}
883
Greg Kroah-Hartman9a775db2010-02-25 16:42:10 -0800884/*
885 * We use a PCI table to determine if we should autoload this driver This is
886 * needed by distro tools to determine if the hyperv drivers should be
887 * installed and/or configured. We don't do anything else with the table, but
888 * it needs to be present.
Greg Kroah-Hartman9a775db2010-02-25 16:42:10 -0800889 */
Tobias Klauser15f0beb2010-05-20 10:39:38 +0200890static const struct pci_device_id microsoft_hv_pci_table[] = {
Greg Kroah-Hartman9a775db2010-02-25 16:42:10 -0800891 { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
892 { 0 }
893};
894MODULE_DEVICE_TABLE(pci, microsoft_hv_pci_table);
895
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -0700896static struct pci_driver hv_bus_driver = {
897 .name = "hv_bus",
898 .probe = hv_pci_probe,
899 .id_table = microsoft_hv_pci_table,
900};
901
902static int __init hv_pci_init(void)
903{
904 return pci_register_driver(&hv_bus_driver);
905}
906
907static void __exit hv_pci_exit(void)
908{
909 vmbus_bus_exit();
910 pci_unregister_driver(&hv_bus_driver);
911}
912
913
914
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700915MODULE_LICENSE("GPL");
Hank Janssen26c14cc2010-02-11 23:02:42 +0000916MODULE_VERSION(HV_DRV_VERSION);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700917module_param(vmbus_loglevel, int, S_IRUGO);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700918
K. Y. Srinivasan1168ac22011-03-15 15:03:32 -0700919module_init(hv_pci_init);
920module_exit(hv_pci_exit);