blob: 894eecfc63ca5bd3e8e84fb2b5bf11dc4680df05 [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>
Hank Janssen3e7ee492009-07-13 16:02:34 -070020 */
Hank Janssen3e7ee492009-07-13 16:02:34 -070021#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/device.h>
24#include <linux/irq.h>
25#include <linux/interrupt.h>
26#include <linux/sysctl.h>
Greg Kroah-Hartman4983b392009-08-19 16:14:47 -070027#include "osd.h"
Greg Kroah-Hartman645954c2009-08-28 16:22:59 -070028#include "logging.h"
Greg Kroah-Hartman870cde82009-08-19 16:21:28 -070029#include "vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070030
Hank Janssen3e7ee492009-07-13 16:02:34 -070031
Bill Pemberton454f18a2009-07-27 16:47:24 -040032/* FIXME! We need to do this dynamically for PIC and APIC system */
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -070033#define VMBUS_IRQ 0x5
34#define VMBUS_IRQ_VECTOR IRQ5_VECTOR
Bill Pemberton454f18a2009-07-27 16:47:24 -040035
36/* Main vmbus driver data structure */
Hank Janssen3e7ee492009-07-13 16:02:34 -070037struct vmbus_driver_context {
Bill Pemberton454f18a2009-07-27 16:47:24 -040038 /* !! These must be the first 2 fields !! */
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -070039 /* FIXME, this is a bug */
Bill Pemberton454f18a2009-07-27 16:47:24 -040040 /* The driver field is not used in here. Instead, the bus field is */
41 /* used to represent the driver */
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -070042 struct driver_context drv_ctx;
Greg Kroah-Hartmanee3d7dd2009-08-20 12:17:36 -070043 struct vmbus_driver drv_obj;
Hank Janssen3e7ee492009-07-13 16:02:34 -070044
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
Bill Pemberton454f18a2009-07-27 16:47:24 -040049 /* The bus root device */
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -070050 struct device_context device_ctx;
Hank Janssen3e7ee492009-07-13 16:02:34 -070051};
52
Hank Janssen3e7ee492009-07-13 16:02:34 -070053static int vmbus_match(struct device *device, struct device_driver *driver);
54static int vmbus_probe(struct device *device);
55static int vmbus_remove(struct device *device);
56static void vmbus_shutdown(struct device *device);
Hank Janssen3e7ee492009-07-13 16:02:34 -070057static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env);
Hank Janssen3e7ee492009-07-13 16:02:34 -070058static void vmbus_msg_dpc(unsigned long data);
59static void vmbus_event_dpc(unsigned long data);
60
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -070061static irqreturn_t vmbus_isr(int irq, void *dev_id);
Hank Janssen3e7ee492009-07-13 16:02:34 -070062
63static void vmbus_device_release(struct device *device);
64static void vmbus_bus_release(struct device *device);
65
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -070066static struct hv_device *vmbus_child_device_create(struct hv_guid *type,
67 struct hv_guid *instance,
68 void *context);
Nicolas Palix3d3b5512009-07-28 17:32:53 +020069static void vmbus_child_device_destroy(struct hv_device *device_obj);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -070070static int vmbus_child_device_register(struct hv_device *root_device_obj,
71 struct hv_device *child_device_obj);
Nicolas Palix3d3b5512009-07-28 17:32:53 +020072static void vmbus_child_device_unregister(struct hv_device *child_device_obj);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -070073static void vmbus_child_device_get_info(struct hv_device *device_obj,
74 struct hv_device_info *device_info);
75static ssize_t vmbus_show_device_attr(struct device *dev,
76 struct device_attribute *dev_attr,
77 char *buf);
Hank Janssen3e7ee492009-07-13 16:02:34 -070078
Hank Janssen3e7ee492009-07-13 16:02:34 -070079
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -070080unsigned int vmbus_loglevel = (ALL_MODULES << 16 | INFO_LVL);
Hank Janssen3e7ee492009-07-13 16:02:34 -070081EXPORT_SYMBOL(vmbus_loglevel);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -070082 /* (ALL_MODULES << 16 | DEBUG_LVL_ENTEREXIT); */
83 /* (((VMBUS | VMBUS_DRV)<<16) | DEBUG_LVL_ENTEREXIT); */
Hank Janssen3e7ee492009-07-13 16:02:34 -070084
85static int vmbus_irq = VMBUS_IRQ;
86
Bill Pemberton454f18a2009-07-27 16:47:24 -040087/* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
Hank Janssen3e7ee492009-07-13 16:02:34 -070088static struct device_attribute vmbus_device_attrs[] = {
89 __ATTR(id, S_IRUGO, vmbus_show_device_attr, NULL),
90 __ATTR(state, S_IRUGO, vmbus_show_device_attr, NULL),
91 __ATTR(class_id, S_IRUGO, vmbus_show_device_attr, NULL),
92 __ATTR(device_id, S_IRUGO, vmbus_show_device_attr, NULL),
93 __ATTR(monitor_id, S_IRUGO, vmbus_show_device_attr, NULL),
94
95 __ATTR(server_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL),
96 __ATTR(server_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL),
97 __ATTR(server_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL),
98
99 __ATTR(client_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL),
100 __ATTR(client_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL),
101 __ATTR(client_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL),
102
103 __ATTR(out_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL),
104 __ATTR(out_read_index, S_IRUGO, vmbus_show_device_attr, NULL),
105 __ATTR(out_write_index, S_IRUGO, vmbus_show_device_attr, NULL),
106 __ATTR(out_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
107 __ATTR(out_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
108
109 __ATTR(in_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL),
110 __ATTR(in_read_index, S_IRUGO, vmbus_show_device_attr, NULL),
111 __ATTR(in_write_index, S_IRUGO, vmbus_show_device_attr, NULL),
112 __ATTR(in_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
113 __ATTR(in_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
114 __ATTR_NULL
115};
Hank Janssen3e7ee492009-07-13 16:02:34 -0700116
Bill Pemberton454f18a2009-07-27 16:47:24 -0400117/* The one and only one */
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700118static struct vmbus_driver_context g_vmbus_drv = {
119 .bus.name = "vmbus",
120 .bus.match = vmbus_match,
121 .bus.shutdown = vmbus_shutdown,
122 .bus.remove = vmbus_remove,
123 .bus.probe = vmbus_probe,
124 .bus.uevent = vmbus_uevent,
125 .bus.dev_attrs = vmbus_device_attrs,
Hank Janssen3e7ee492009-07-13 16:02:34 -0700126};
127
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700128/**
129 * vmbus_show_device_attr - Show the device attribute in sysfs.
130 *
131 * This is invoked when user does a
132 * "cat /sys/bus/vmbus/devices/<busdevice>/<attr name>"
133 */
134static ssize_t vmbus_show_device_attr(struct device *dev,
135 struct device_attribute *dev_attr,
136 char *buf)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700137{
138 struct device_context *device_ctx = device_to_device_context(dev);
Greg Kroah-Hartmanee3d7dd2009-08-20 12:17:36 -0700139 struct hv_device_info device_info;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700140
Greg Kroah-Hartmanee3d7dd2009-08-20 12:17:36 -0700141 memset(&device_info, 0, sizeof(struct hv_device_info));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700142
143 vmbus_child_device_get_info(&device_ctx->device_obj, &device_info);
144
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700145 if (!strcmp(dev_attr->attr.name, "class_id")) {
146 return sprintf(buf, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-"
147 "%02x%02x%02x%02x%02x%02x%02x%02x}\n",
148 device_info.ChannelType.data[3],
149 device_info.ChannelType.data[2],
150 device_info.ChannelType.data[1],
151 device_info.ChannelType.data[0],
152 device_info.ChannelType.data[5],
153 device_info.ChannelType.data[4],
154 device_info.ChannelType.data[7],
155 device_info.ChannelType.data[6],
156 device_info.ChannelType.data[8],
157 device_info.ChannelType.data[9],
158 device_info.ChannelType.data[10],
159 device_info.ChannelType.data[11],
160 device_info.ChannelType.data[12],
161 device_info.ChannelType.data[13],
162 device_info.ChannelType.data[14],
163 device_info.ChannelType.data[15]);
164 } else if (!strcmp(dev_attr->attr.name, "device_id")) {
165 return sprintf(buf, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-"
166 "%02x%02x%02x%02x%02x%02x%02x%02x}\n",
167 device_info.ChannelInstance.data[3],
168 device_info.ChannelInstance.data[2],
169 device_info.ChannelInstance.data[1],
170 device_info.ChannelInstance.data[0],
171 device_info.ChannelInstance.data[5],
172 device_info.ChannelInstance.data[4],
173 device_info.ChannelInstance.data[7],
174 device_info.ChannelInstance.data[6],
175 device_info.ChannelInstance.data[8],
176 device_info.ChannelInstance.data[9],
177 device_info.ChannelInstance.data[10],
178 device_info.ChannelInstance.data[11],
179 device_info.ChannelInstance.data[12],
180 device_info.ChannelInstance.data[13],
181 device_info.ChannelInstance.data[14],
182 device_info.ChannelInstance.data[15]);
183 } else if (!strcmp(dev_attr->attr.name, "state")) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700184 return sprintf(buf, "%d\n", device_info.ChannelState);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700185 } else if (!strcmp(dev_attr->attr.name, "id")) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700186 return sprintf(buf, "%d\n", device_info.ChannelId);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700187 } else if (!strcmp(dev_attr->attr.name, "out_intr_mask")) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700188 return sprintf(buf, "%d\n", device_info.Outbound.InterruptMask);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700189 } else if (!strcmp(dev_attr->attr.name, "out_read_index")) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700190 return sprintf(buf, "%d\n", device_info.Outbound.ReadIndex);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700191 } else if (!strcmp(dev_attr->attr.name, "out_write_index")) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700192 return sprintf(buf, "%d\n", device_info.Outbound.WriteIndex);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700193 } else if (!strcmp(dev_attr->attr.name, "out_read_bytes_avail")) {
194 return sprintf(buf, "%d\n",
195 device_info.Outbound.BytesAvailToRead);
196 } else if (!strcmp(dev_attr->attr.name, "out_write_bytes_avail")) {
197 return sprintf(buf, "%d\n",
198 device_info.Outbound.BytesAvailToWrite);
199 } else if (!strcmp(dev_attr->attr.name, "in_intr_mask")) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700200 return sprintf(buf, "%d\n", device_info.Inbound.InterruptMask);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700201 } else if (!strcmp(dev_attr->attr.name, "in_read_index")) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700202 return sprintf(buf, "%d\n", device_info.Inbound.ReadIndex);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700203 } else if (!strcmp(dev_attr->attr.name, "in_write_index")) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700204 return sprintf(buf, "%d\n", device_info.Inbound.WriteIndex);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700205 } else if (!strcmp(dev_attr->attr.name, "in_read_bytes_avail")) {
206 return sprintf(buf, "%d\n",
207 device_info.Inbound.BytesAvailToRead);
208 } else if (!strcmp(dev_attr->attr.name, "in_write_bytes_avail")) {
209 return sprintf(buf, "%d\n",
210 device_info.Inbound.BytesAvailToWrite);
211 } else if (!strcmp(dev_attr->attr.name, "monitor_id")) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700212 return sprintf(buf, "%d\n", device_info.MonitorId);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700213 } else if (!strcmp(dev_attr->attr.name, "server_monitor_pending")) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700214 return sprintf(buf, "%d\n", device_info.ServerMonitorPending);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700215 } else if (!strcmp(dev_attr->attr.name, "server_monitor_latency")) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700216 return sprintf(buf, "%d\n", device_info.ServerMonitorLatency);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700217 } else if (!strcmp(dev_attr->attr.name, "server_monitor_conn_id")) {
218 return sprintf(buf, "%d\n",
219 device_info.ServerMonitorConnectionId);
220 } else if (!strcmp(dev_attr->attr.name, "client_monitor_pending")) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700221 return sprintf(buf, "%d\n", device_info.ClientMonitorPending);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700222 } else if (!strcmp(dev_attr->attr.name, "client_monitor_latency")) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700223 return sprintf(buf, "%d\n", device_info.ClientMonitorLatency);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700224 } else if (!strcmp(dev_attr->attr.name, "client_monitor_conn_id")) {
225 return sprintf(buf, "%d\n",
226 device_info.ClientMonitorConnectionId);
227 } else {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700228 return 0;
229 }
230}
231
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700232/**
233 * vmbus_bus_init -Main vmbus driver initialization routine.
234 *
235 * Here, we
236 * - initialize the vmbus driver context
237 * - setup various driver entry points
238 * - invoke the vmbus hv main init routine
239 * - get the irq resource
240 * - invoke the vmbus to add the vmbus root device
241 * - setup the vmbus root device
242 * - retrieve the channel offers
243 */
Greg Kroah-Hartman21707be2009-09-02 11:53:59 -0700244static int vmbus_bus_init(int (*drv_init)(struct hv_driver *drv))
Hank Janssen3e7ee492009-07-13 16:02:34 -0700245{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700246 struct vmbus_driver_context *vmbus_drv_ctx = &g_vmbus_drv;
Greg Kroah-Hartmanee3d7dd2009-08-20 12:17:36 -0700247 struct vmbus_driver *vmbus_drv_obj = &g_vmbus_drv.drv_obj;
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700248 struct device_context *dev_ctx = &g_vmbus_drv.device_ctx;
249 int ret;
250 unsigned int vector;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700251
252 DPRINT_ENTER(VMBUS_DRV);
253
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700254 /*
255 * Set this up to allow lower layer to callback to add/remove child
256 * devices on the bus
257 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700258 vmbus_drv_obj->OnChildDeviceCreate = vmbus_child_device_create;
259 vmbus_drv_obj->OnChildDeviceDestroy = vmbus_child_device_destroy;
260 vmbus_drv_obj->OnChildDeviceAdd = vmbus_child_device_register;
261 vmbus_drv_obj->OnChildDeviceRemove = vmbus_child_device_unregister;
262
Bill Pemberton454f18a2009-07-27 16:47:24 -0400263 /* Call to bus driver to initialize */
Greg Kroah-Hartman21707be2009-09-02 11:53:59 -0700264 ret = drv_init(&vmbus_drv_obj->Base);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700265 if (ret != 0) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700266 DPRINT_ERR(VMBUS_DRV, "Unable to initialize vmbus (%d)", ret);
267 goto cleanup;
268 }
269
Bill Pemberton454f18a2009-07-27 16:47:24 -0400270 /* Sanity checks */
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700271 if (!vmbus_drv_obj->Base.OnDeviceAdd) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700272 DPRINT_ERR(VMBUS_DRV, "OnDeviceAdd() routine not set");
273 ret = -1;
274 goto cleanup;
275 }
276
277 vmbus_drv_ctx->bus.name = vmbus_drv_obj->Base.name;
278
Bill Pemberton454f18a2009-07-27 16:47:24 -0400279 /* Initialize the bus context */
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700280 tasklet_init(&vmbus_drv_ctx->msg_dpc, vmbus_msg_dpc,
281 (unsigned long)vmbus_drv_obj);
282 tasklet_init(&vmbus_drv_ctx->event_dpc, vmbus_event_dpc,
283 (unsigned long)vmbus_drv_obj);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700284
Bill Pemberton454f18a2009-07-27 16:47:24 -0400285 /* Now, register the bus driver with LDM */
Bill Pembertonc19fbca2009-07-27 16:47:35 -0400286 ret = bus_register(&vmbus_drv_ctx->bus);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700287 if (ret) {
Bill Pembertonc19fbca2009-07-27 16:47:35 -0400288 ret = -1;
289 goto cleanup;
290 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700291
Bill Pemberton454f18a2009-07-27 16:47:24 -0400292 /* Get the interrupt resource */
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700293 ret = request_irq(vmbus_irq, vmbus_isr, IRQF_SAMPLE_RANDOM,
294 vmbus_drv_obj->Base.name, NULL);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700295
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700296 if (ret != 0) {
297 DPRINT_ERR(VMBUS_DRV, "ERROR - Unable to request IRQ %d",
298 vmbus_irq);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700299
300 bus_unregister(&vmbus_drv_ctx->bus);
301
302 ret = -1;
303 goto cleanup;
304 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700305 vector = VMBUS_IRQ_VECTOR;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700306
307 DPRINT_INFO(VMBUS_DRV, "irq 0x%x vector 0x%x", vmbus_irq, vector);
308
Bill Pemberton454f18a2009-07-27 16:47:24 -0400309 /* Call to bus driver to add the root device */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700310 memset(dev_ctx, 0, sizeof(struct device_context));
311
312 ret = vmbus_drv_obj->Base.OnDeviceAdd(&dev_ctx->device_obj, &vector);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700313 if (ret != 0) {
314 DPRINT_ERR(VMBUS_DRV,
315 "ERROR - Unable to add vmbus root device");
Hank Janssen3e7ee492009-07-13 16:02:34 -0700316
317 free_irq(vmbus_irq, NULL);
318
319 bus_unregister(&vmbus_drv_ctx->bus);
320
321 ret = -1;
322 goto cleanup;
323 }
Bill Pemberton454f18a2009-07-27 16:47:24 -0400324 /* strcpy(dev_ctx->device.bus_id, dev_ctx->device_obj.name); */
Greg Kroah-Hartman09d50ff2009-07-13 17:09:34 -0700325 dev_set_name(&dev_ctx->device, "vmbus_0_0");
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700326 memcpy(&dev_ctx->class_id, &dev_ctx->device_obj.deviceType,
327 sizeof(struct hv_guid));
328 memcpy(&dev_ctx->device_id, &dev_ctx->device_obj.deviceInstance,
329 sizeof(struct hv_guid));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700330
Bill Pemberton454f18a2009-07-27 16:47:24 -0400331 /* No need to bind a driver to the root device. */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700332 dev_ctx->device.parent = NULL;
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700333 /* NULL; vmbus_remove() does not get invoked */
334 dev_ctx->device.bus = &vmbus_drv_ctx->bus;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700335
Bill Pemberton454f18a2009-07-27 16:47:24 -0400336 /* Setup the device dispatch table */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700337 dev_ctx->device.release = vmbus_bus_release;
338
Bill Pemberton454f18a2009-07-27 16:47:24 -0400339 /* Setup the bus as root device */
Bill Pemberton5d48a1c2009-07-27 16:47:36 -0400340 ret = device_register(&dev_ctx->device);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700341 if (ret) {
342 DPRINT_ERR(VMBUS_DRV,
343 "ERROR - Unable to register vmbus root device");
Bill Pemberton5d48a1c2009-07-27 16:47:36 -0400344
345 free_irq(vmbus_irq, NULL);
346 bus_unregister(&vmbus_drv_ctx->bus);
347
348 ret = -1;
349 goto cleanup;
350 }
351
Hank Janssen3e7ee492009-07-13 16:02:34 -0700352
353 vmbus_drv_obj->GetChannelOffers();
354
355cleanup:
356 DPRINT_EXIT(VMBUS_DRV);
357
358 return ret;
359}
360
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700361/**
362 * vmbus_bus_exit - Terminate the vmbus driver.
363 *
364 * This routine is opposite of vmbus_bus_init()
365 */
Greg Kroah-Hartmanbd1de702009-07-29 09:04:51 -0700366static void vmbus_bus_exit(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700367{
Greg Kroah-Hartmanee3d7dd2009-08-20 12:17:36 -0700368 struct vmbus_driver *vmbus_drv_obj = &g_vmbus_drv.drv_obj;
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700369 struct vmbus_driver_context *vmbus_drv_ctx = &g_vmbus_drv;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700370
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700371 struct device_context *dev_ctx = &g_vmbus_drv.device_ctx;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700372
373 DPRINT_ENTER(VMBUS_DRV);
374
Bill Pemberton454f18a2009-07-27 16:47:24 -0400375 /* Remove the root device */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700376 if (vmbus_drv_obj->Base.OnDeviceRemove)
377 vmbus_drv_obj->Base.OnDeviceRemove(&dev_ctx->device_obj);
378
379 if (vmbus_drv_obj->Base.OnCleanup)
380 vmbus_drv_obj->Base.OnCleanup(&vmbus_drv_obj->Base);
381
Bill Pemberton454f18a2009-07-27 16:47:24 -0400382 /* Unregister the root bus device */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700383 device_unregister(&dev_ctx->device);
384
385 bus_unregister(&vmbus_drv_ctx->bus);
386
387 free_irq(vmbus_irq, NULL);
388
389 tasklet_kill(&vmbus_drv_ctx->msg_dpc);
390 tasklet_kill(&vmbus_drv_ctx->event_dpc);
391
392 DPRINT_EXIT(VMBUS_DRV);
393
394 return;
395}
396
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700397/**
398 * vmbus_child_driver_register - Register a vmbus's child driver
399 */
400int vmbus_child_driver_register(struct driver_context *driver_ctx)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700401{
Greg Kroah-Hartmanee3d7dd2009-08-20 12:17:36 -0700402 struct vmbus_driver *vmbus_drv_obj = &g_vmbus_drv.drv_obj;
Bill Pemberton5d48a1c2009-07-27 16:47:36 -0400403 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700404
405 DPRINT_ENTER(VMBUS_DRV);
406
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700407 DPRINT_INFO(VMBUS_DRV, "child driver (%p) registering - name %s",
408 driver_ctx, driver_ctx->driver.name);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700409
Bill Pemberton454f18a2009-07-27 16:47:24 -0400410 /* The child driver on this vmbus */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700411 driver_ctx->driver.bus = &g_vmbus_drv.bus;
412
Bill Pemberton5d48a1c2009-07-27 16:47:36 -0400413 ret = driver_register(&driver_ctx->driver);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700414
415 vmbus_drv_obj->GetChannelOffers();
416
417 DPRINT_EXIT(VMBUS_DRV);
Bill Pemberton5d48a1c2009-07-27 16:47:36 -0400418
419 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700420}
Hank Janssen3e7ee492009-07-13 16:02:34 -0700421EXPORT_SYMBOL(vmbus_child_driver_register);
422
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700423/**
424 * vmbus_child_driver_unregister Unregister a vmbus's child driver
425 */
426void vmbus_child_driver_unregister(struct driver_context *driver_ctx)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700427{
428 DPRINT_ENTER(VMBUS_DRV);
429
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700430 DPRINT_INFO(VMBUS_DRV, "child driver (%p) unregistering - name %s",
431 driver_ctx, driver_ctx->driver.name);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700432
433 driver_unregister(&driver_ctx->driver);
434
435 driver_ctx->driver.bus = NULL;
436
437 DPRINT_EXIT(VMBUS_DRV);
438}
Hank Janssen3e7ee492009-07-13 16:02:34 -0700439EXPORT_SYMBOL(vmbus_child_driver_unregister);
440
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700441/**
442 * vmbus_get_interface - Get the vmbus channel interface.
443 *
444 * This is invoked by child/client driver that sits above vmbus
445 */
Greg Kroah-Hartmanee3d7dd2009-08-20 12:17:36 -0700446void vmbus_get_interface(struct vmbus_channel_interface *interface)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700447{
Greg Kroah-Hartmanee3d7dd2009-08-20 12:17:36 -0700448 struct vmbus_driver *vmbus_drv_obj = &g_vmbus_drv.drv_obj;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700449
450 vmbus_drv_obj->GetChannelInterface(interface);
451}
Hank Janssen3e7ee492009-07-13 16:02:34 -0700452EXPORT_SYMBOL(vmbus_get_interface);
453
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700454/**
455 * vmbus_child_device_get_info - Get the vmbus child device info.
456 *
457 * This is invoked to display various device attributes in sysfs.
458 */
459static void vmbus_child_device_get_info(struct hv_device *device_obj,
460 struct hv_device_info *device_info)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700461{
Greg Kroah-Hartmanee3d7dd2009-08-20 12:17:36 -0700462 struct vmbus_driver *vmbus_drv_obj = &g_vmbus_drv.drv_obj;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700463
464 vmbus_drv_obj->GetChannelInfo(device_obj, device_info);
465}
466
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700467/**
468 * vmbus_child_device_create - Creates and registers a new child device on the vmbus.
469 */
Greg Kroah-Hartmandaaa8cc2009-08-19 16:18:56 -0700470static struct hv_device *vmbus_child_device_create(struct hv_guid *type,
471 struct hv_guid *instance,
472 void *context)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700473{
474 struct device_context *child_device_ctx;
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200475 struct hv_device *child_device_obj;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700476
477 DPRINT_ENTER(VMBUS_DRV);
478
Bill Pemberton454f18a2009-07-27 16:47:24 -0400479 /* Allocate the new child device */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700480 child_device_ctx = kzalloc(sizeof(struct device_context), GFP_KERNEL);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700481 if (!child_device_ctx) {
482 DPRINT_ERR(VMBUS_DRV,
483 "unable to allocate device_context for child device");
Hank Janssen3e7ee492009-07-13 16:02:34 -0700484 DPRINT_EXIT(VMBUS_DRV);
485
486 return NULL;
487 }
488
489 DPRINT_DBG(VMBUS_DRV, "child device (%p) allocated - "
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700490 "type {%02x%02x%02x%02x-%02x%02x-%02x%02x-"
491 "%02x%02x%02x%02x%02x%02x%02x%02x},"
492 "id {%02x%02x%02x%02x-%02x%02x-%02x%02x-"
493 "%02x%02x%02x%02x%02x%02x%02x%02x}",
Hank Janssen3e7ee492009-07-13 16:02:34 -0700494 &child_device_ctx->device,
Greg Kroah-Hartmandaaa8cc2009-08-19 16:18:56 -0700495 type->data[3], type->data[2], type->data[1], type->data[0],
496 type->data[5], type->data[4], type->data[7], type->data[6],
497 type->data[8], type->data[9], type->data[10], type->data[11],
498 type->data[12], type->data[13], type->data[14], type->data[15],
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700499 instance->data[3], instance->data[2],
500 instance->data[1], instance->data[0],
501 instance->data[5], instance->data[4],
502 instance->data[7], instance->data[6],
503 instance->data[8], instance->data[9],
504 instance->data[10], instance->data[11],
505 instance->data[12], instance->data[13],
506 instance->data[14], instance->data[15]);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700507
508 child_device_obj = &child_device_ctx->device_obj;
509 child_device_obj->context = context;
Milan Dadok9fb5cce2009-10-28 23:23:27 +0100510 memcpy(&child_device_obj->deviceType, type, sizeof(struct hv_guid));
511 memcpy(&child_device_obj->deviceInstance, instance,
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700512 sizeof(struct hv_guid));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700513
Milan Dadok9fb5cce2009-10-28 23:23:27 +0100514 memcpy(&child_device_ctx->class_id, type, sizeof(struct hv_guid));
515 memcpy(&child_device_ctx->device_id, instance, sizeof(struct hv_guid));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700516
517 DPRINT_EXIT(VMBUS_DRV);
518
519 return child_device_obj;
520}
521
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700522/**
523 * vmbus_child_device_register - Register the child device on the specified bus
524 */
525static int vmbus_child_device_register(struct hv_device *root_device_obj,
526 struct hv_device *child_device_obj)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700527{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700528 int ret = 0;
529 struct device_context *root_device_ctx =
530 to_device_context(root_device_obj);
531 struct device_context *child_device_ctx =
532 to_device_context(child_device_obj);
Bill Pembertonf4888412009-07-29 17:00:12 -0400533 static atomic_t device_num = ATOMIC_INIT(0);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700534
535 DPRINT_ENTER(VMBUS_DRV);
536
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700537 DPRINT_DBG(VMBUS_DRV, "child device (%p) registering",
538 child_device_ctx);
Bill Pemberton454f18a2009-07-27 16:47:24 -0400539
Haiyang Zhang1bb40a22009-10-23 18:14:24 +0000540 /* Set the device name. Otherwise, device_register() will fail. */
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700541 dev_set_name(&child_device_ctx->device, "vmbus_0_%d",
542 atomic_inc_return(&device_num));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700543
Bill Pemberton454f18a2009-07-27 16:47:24 -0400544 /* The new device belongs to this bus */
545 child_device_ctx->device.bus = &g_vmbus_drv.bus; /* device->dev.bus; */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700546 child_device_ctx->device.parent = &root_device_ctx->device;
547 child_device_ctx->device.release = vmbus_device_release;
548
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700549 /*
550 * Register with the LDM. This will kick off the driver/device
551 * binding...which will eventually call vmbus_match() and vmbus_probe()
552 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700553 ret = device_register(&child_device_ctx->device);
554
Bill Pemberton454f18a2009-07-27 16:47:24 -0400555 /* vmbus_probe() error does not get propergate to device_register(). */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700556 ret = child_device_ctx->probe_error;
557
558 if (ret)
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700559 DPRINT_ERR(VMBUS_DRV, "unable to register child device (%p)",
560 &child_device_ctx->device);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700561 else
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700562 DPRINT_INFO(VMBUS_DRV, "child device (%p) registered",
563 &child_device_ctx->device);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700564
Hank Janssen3e7ee492009-07-13 16:02:34 -0700565 DPRINT_EXIT(VMBUS_DRV);
566
567 return ret;
568}
569
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700570/**
571 * vmbus_child_device_unregister - Remove the specified child device from the vmbus.
572 */
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200573static void vmbus_child_device_unregister(struct hv_device *device_obj)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700574{
575 struct device_context *device_ctx = to_device_context(device_obj);
576
577 DPRINT_ENTER(VMBUS_DRV);
578
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700579 DPRINT_INFO(VMBUS_DRV, "unregistering child device (%p)",
580 &device_ctx->device);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700581
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700582 /*
583 * Kick off the process of unregistering the device.
584 * This will call vmbus_remove() and eventually vmbus_device_release()
585 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700586 device_unregister(&device_ctx->device);
587
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700588 DPRINT_INFO(VMBUS_DRV, "child device (%p) unregistered",
589 &device_ctx->device);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700590
591 DPRINT_EXIT(VMBUS_DRV);
592}
593
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700594/**
595 * vmbus_child_device_destroy - Destroy the specified child device on the vmbus.
596 */
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200597static void vmbus_child_device_destroy(struct hv_device *device_obj)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700598{
599 DPRINT_ENTER(VMBUS_DRV);
600
601 DPRINT_EXIT(VMBUS_DRV);
602}
603
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700604/**
605 * vmbus_uevent - add uevent for our device
606 *
607 * This routine is invoked when a device is added or removed on the vmbus to
608 * generate a uevent to udev in the userspace. The udev will then look at its
609 * rule and the uevent generated here to load the appropriate driver
610 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700611static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
612{
613 struct device_context *device_ctx = device_to_device_context(device);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700614 int ret;
615
616 DPRINT_ENTER(VMBUS_DRV);
617
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700618 DPRINT_INFO(VMBUS_DRV, "generating uevent - VMBUS_DEVICE_CLASS_GUID={"
619 "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
620 "%02x%02x%02x%02x%02x%02x%02x%02x}",
621 device_ctx->class_id.data[3], device_ctx->class_id.data[2],
622 device_ctx->class_id.data[1], device_ctx->class_id.data[0],
623 device_ctx->class_id.data[5], device_ctx->class_id.data[4],
624 device_ctx->class_id.data[7], device_ctx->class_id.data[6],
625 device_ctx->class_id.data[8], device_ctx->class_id.data[9],
626 device_ctx->class_id.data[10],
627 device_ctx->class_id.data[11],
628 device_ctx->class_id.data[12],
629 device_ctx->class_id.data[13],
630 device_ctx->class_id.data[14],
631 device_ctx->class_id.data[15]);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700632
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700633 ret = add_uevent_var(env, "VMBUS_DEVICE_CLASS_GUID={"
634 "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
635 "%02x%02x%02x%02x%02x%02x%02x%02x}",
636 device_ctx->class_id.data[3],
637 device_ctx->class_id.data[2],
638 device_ctx->class_id.data[1],
639 device_ctx->class_id.data[0],
640 device_ctx->class_id.data[5],
641 device_ctx->class_id.data[4],
642 device_ctx->class_id.data[7],
643 device_ctx->class_id.data[6],
644 device_ctx->class_id.data[8],
645 device_ctx->class_id.data[9],
646 device_ctx->class_id.data[10],
647 device_ctx->class_id.data[11],
648 device_ctx->class_id.data[12],
649 device_ctx->class_id.data[13],
650 device_ctx->class_id.data[14],
651 device_ctx->class_id.data[15]);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700652
653 if (ret)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700654 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700655
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700656 ret = add_uevent_var(env, "VMBUS_DEVICE_DEVICE_GUID={"
657 "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
658 "%02x%02x%02x%02x%02x%02x%02x%02x}",
659 device_ctx->device_id.data[3],
660 device_ctx->device_id.data[2],
661 device_ctx->device_id.data[1],
662 device_ctx->device_id.data[0],
663 device_ctx->device_id.data[5],
664 device_ctx->device_id.data[4],
665 device_ctx->device_id.data[7],
666 device_ctx->device_id.data[6],
667 device_ctx->device_id.data[8],
668 device_ctx->device_id.data[9],
669 device_ctx->device_id.data[10],
670 device_ctx->device_id.data[11],
671 device_ctx->device_id.data[12],
672 device_ctx->device_id.data[13],
673 device_ctx->device_id.data[14],
674 device_ctx->device_id.data[15]);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700675 if (ret)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700676 return ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700677
Hank Janssen3e7ee492009-07-13 16:02:34 -0700678 DPRINT_EXIT(VMBUS_DRV);
679
680 return 0;
681}
682
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700683/**
684 * vmbus_match - Attempt to match the specified device to the specified driver
685 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700686static int vmbus_match(struct device *device, struct device_driver *driver)
687{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700688 int match = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700689 struct driver_context *driver_ctx = driver_to_driver_context(driver);
690 struct device_context *device_ctx = device_to_device_context(device);
691
692 DPRINT_ENTER(VMBUS_DRV);
693
Bill Pemberton454f18a2009-07-27 16:47:24 -0400694 /* We found our driver ? */
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700695 if (memcmp(&device_ctx->class_id, &driver_ctx->class_id,
696 sizeof(struct hv_guid)) == 0) {
697 /*
698 * !! NOTE: The driver_ctx is not a vmbus_drv_ctx. We typecast
699 * it here to access the struct hv_driver field
700 */
701 struct vmbus_driver_context *vmbus_drv_ctx =
702 (struct vmbus_driver_context *)driver_ctx;
703
Hank Janssen3e7ee492009-07-13 16:02:34 -0700704 device_ctx->device_obj.Driver = &vmbus_drv_ctx->drv_obj.Base;
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700705 DPRINT_INFO(VMBUS_DRV,
706 "device object (%p) set to driver object (%p)",
707 &device_ctx->device_obj,
708 device_ctx->device_obj.Driver);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700709
710 match = 1;
711 }
712
713 DPRINT_EXIT(VMBUS_DRV);
714
715 return match;
716}
717
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700718/**
719 * vmbus_probe_failed_cb - Callback when a driver probe failed in vmbus_probe()
720 *
721 * We need a callback because we cannot invoked device_unregister() inside
722 * vmbus_probe() since vmbus_probe() may be invoked inside device_register()
723 * i.e. we cannot call device_unregister() inside device_register()
724 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700725static void vmbus_probe_failed_cb(struct work_struct *context)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700726{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700727 struct device_context *device_ctx = (struct device_context *)context;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700728
729 DPRINT_ENTER(VMBUS_DRV);
730
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700731 /*
732 * Kick off the process of unregistering the device.
733 * This will call vmbus_remove() and eventually vmbus_device_release()
734 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700735 device_unregister(&device_ctx->device);
736
Bill Pemberton454f18a2009-07-27 16:47:24 -0400737 /* put_device(&device_ctx->device); */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700738 DPRINT_EXIT(VMBUS_DRV);
739}
740
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700741/**
742 * vmbus_probe - Add the new vmbus's child device
743 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700744static int vmbus_probe(struct device *child_device)
745{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700746 int ret = 0;
747 struct driver_context *driver_ctx =
748 driver_to_driver_context(child_device->driver);
749 struct device_context *device_ctx =
750 device_to_device_context(child_device);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700751
752 DPRINT_ENTER(VMBUS_DRV);
753
Bill Pemberton454f18a2009-07-27 16:47:24 -0400754 /* Let the specific open-source driver handles the probe if it can */
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700755 if (driver_ctx->probe) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700756 ret = device_ctx->probe_error = driver_ctx->probe(child_device);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700757 if (ret != 0) {
758 DPRINT_ERR(VMBUS_DRV, "probe() failed for device %s "
759 "(%p) on driver %s (%d)...",
760 dev_name(child_device), child_device,
761 child_device->driver->name, ret);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700762
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700763 INIT_WORK(&device_ctx->probe_failed_work_item,
764 vmbus_probe_failed_cb);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700765 schedule_work(&device_ctx->probe_failed_work_item);
766 }
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700767 } else {
768 DPRINT_ERR(VMBUS_DRV, "probe() method not set for driver - %s",
769 child_device->driver->name);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700770 ret = -1;
771 }
772
773 DPRINT_EXIT(VMBUS_DRV);
774 return ret;
775}
776
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700777/**
778 * vmbus_remove - Remove a vmbus device
779 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700780static int vmbus_remove(struct device *child_device)
781{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700782 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700783 struct driver_context *driver_ctx;
784
785 DPRINT_ENTER(VMBUS_DRV);
786
Bill Pemberton454f18a2009-07-27 16:47:24 -0400787 /* Special case root bus device */
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700788 if (child_device->parent == NULL) {
789 /*
790 * No-op since it is statically defined and handle in
791 * vmbus_bus_exit()
792 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700793 DPRINT_EXIT(VMBUS_DRV);
794 return 0;
795 }
796
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700797 if (child_device->driver) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700798 driver_ctx = driver_to_driver_context(child_device->driver);
799
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700800 /*
801 * Let the specific open-source driver handles the removal if
802 * it can
803 */
804 if (driver_ctx->remove) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700805 ret = driver_ctx->remove(child_device);
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700806 } else {
807 DPRINT_ERR(VMBUS_DRV,
808 "remove() method not set for driver - %s",
809 child_device->driver->name);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700810 ret = -1;
811 }
812 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700813
814 DPRINT_EXIT(VMBUS_DRV);
815
816 return 0;
817}
818
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700819/**
820 * vmbus_shutdown - Shutdown a vmbus device
821 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700822static void vmbus_shutdown(struct device *child_device)
823{
824 struct driver_context *driver_ctx;
825
826 DPRINT_ENTER(VMBUS_DRV);
827
Bill Pemberton454f18a2009-07-27 16:47:24 -0400828 /* Special case root bus device */
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700829 if (child_device->parent == NULL) {
830 /*
831 * No-op since it is statically defined and handle in
832 * vmbus_bus_exit()
833 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700834 DPRINT_EXIT(VMBUS_DRV);
835 return;
836 }
837
Bill Pemberton454f18a2009-07-27 16:47:24 -0400838 /* The device may not be attached yet */
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700839 if (!child_device->driver) {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700840 DPRINT_EXIT(VMBUS_DRV);
841 return;
842 }
843
844 driver_ctx = driver_to_driver_context(child_device->driver);
845
Bill Pemberton454f18a2009-07-27 16:47:24 -0400846 /* Let the specific open-source driver handles the removal if it can */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700847 if (driver_ctx->shutdown)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700848 driver_ctx->shutdown(child_device);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700849
850 DPRINT_EXIT(VMBUS_DRV);
851
852 return;
853}
854
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700855/**
856 * vmbus_bus_release - Final callback release of the vmbus root device
857 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700858static void vmbus_bus_release(struct device *device)
859{
860 DPRINT_ENTER(VMBUS_DRV);
Greg Kroah-Hartman689bf402009-09-01 20:12:58 -0700861 /* FIXME */
862 /* Empty release functions are a bug, or a major sign
863 * of a problem design, this MUST BE FIXED! */
864 dev_err(device, "%s needs to be fixed!\n", __func__);
865 WARN_ON(1);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700866 DPRINT_EXIT(VMBUS_DRV);
867}
868
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700869/**
870 * vmbus_device_release - Final callback release of the vmbus child device
871 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700872static void vmbus_device_release(struct device *device)
873{
874 struct device_context *device_ctx = device_to_device_context(device);
875
876 DPRINT_ENTER(VMBUS_DRV);
877
Bill Pemberton454f18a2009-07-27 16:47:24 -0400878 /* vmbus_child_device_destroy(&device_ctx->device_obj); */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700879 kfree(device_ctx);
880
Bill Pemberton454f18a2009-07-27 16:47:24 -0400881 /* !!DO NOT REFERENCE device_ctx anymore at this point!! */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700882 DPRINT_EXIT(VMBUS_DRV);
883
884 return;
885}
886
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700887/**
888 * vmbus_msg_dpc - Tasklet routine to handle hypervisor messages
889 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700890static void vmbus_msg_dpc(unsigned long data)
891{
Greg Kroah-Hartmanee3d7dd2009-08-20 12:17:36 -0700892 struct vmbus_driver *vmbus_drv_obj = (struct vmbus_driver *)data;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700893
894 DPRINT_ENTER(VMBUS_DRV);
895
896 ASSERT(vmbus_drv_obj->OnMsgDpc != NULL);
897
Bill Pemberton454f18a2009-07-27 16:47:24 -0400898 /* Call to bus driver to handle interrupt */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700899 vmbus_drv_obj->OnMsgDpc(&vmbus_drv_obj->Base);
900
901 DPRINT_EXIT(VMBUS_DRV);
902}
903
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700904/**
905 * vmbus_msg_dpc - Tasklet routine to handle hypervisor events
906 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700907static void vmbus_event_dpc(unsigned long data)
908{
Greg Kroah-Hartmanee3d7dd2009-08-20 12:17:36 -0700909 struct vmbus_driver *vmbus_drv_obj = (struct vmbus_driver *)data;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700910
911 DPRINT_ENTER(VMBUS_DRV);
912
913 ASSERT(vmbus_drv_obj->OnEventDpc != NULL);
914
Bill Pemberton454f18a2009-07-27 16:47:24 -0400915 /* Call to bus driver to handle interrupt */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700916 vmbus_drv_obj->OnEventDpc(&vmbus_drv_obj->Base);
917
918 DPRINT_EXIT(VMBUS_DRV);
919}
920
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700921static irqreturn_t vmbus_isr(int irq, void *dev_id)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700922{
Greg Kroah-Hartmanee3d7dd2009-08-20 12:17:36 -0700923 struct vmbus_driver *vmbus_driver_obj = &g_vmbus_drv.drv_obj;
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700924 int ret;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700925
926 DPRINT_ENTER(VMBUS_DRV);
927
928 ASSERT(vmbus_driver_obj->OnIsr != NULL);
929
Bill Pemberton454f18a2009-07-27 16:47:24 -0400930 /* Call to bus driver to handle interrupt */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700931 ret = vmbus_driver_obj->OnIsr(&vmbus_driver_obj->Base);
932
Bill Pemberton454f18a2009-07-27 16:47:24 -0400933 /* Schedules a dpc if necessary */
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700934 if (ret > 0) {
935 if (test_bit(0, (unsigned long *)&ret))
Hank Janssen3e7ee492009-07-13 16:02:34 -0700936 tasklet_schedule(&g_vmbus_drv.msg_dpc);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700937
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700938 if (test_bit(1, (unsigned long *)&ret))
Hank Janssen3e7ee492009-07-13 16:02:34 -0700939 tasklet_schedule(&g_vmbus_drv.event_dpc);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700940
941 DPRINT_EXIT(VMBUS_DRV);
942 return IRQ_HANDLED;
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700943 } else {
Hank Janssen3e7ee492009-07-13 16:02:34 -0700944 DPRINT_EXIT(VMBUS_DRV);
945 return IRQ_NONE;
946 }
947}
948
Hank Janssen3e7ee492009-07-13 16:02:34 -0700949static int __init vmbus_init(void)
950{
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700951 int ret = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700952
953 DPRINT_ENTER(VMBUS_DRV);
954
955 DPRINT_INFO(VMBUS_DRV,
956 "Vmbus initializing.... current log level 0x%x (%x,%x)",
957 vmbus_loglevel, HIWORD(vmbus_loglevel), LOWORD(vmbus_loglevel));
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700958 /* Todo: it is used for loglevel, to be ported to new kernel. */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700959
960 ret = vmbus_bus_init(VmbusInitialize);
961
962 DPRINT_EXIT(VMBUS_DRV);
963 return ret;
964}
965
Hank Janssen3e7ee492009-07-13 16:02:34 -0700966static void __exit vmbus_exit(void)
967{
968 DPRINT_ENTER(VMBUS_DRV);
969
970 vmbus_bus_exit();
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700971 /* Todo: it is used for loglevel, to be ported to new kernel. */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700972 DPRINT_EXIT(VMBUS_DRV);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700973 return;
974}
975
Greg Kroah-Hartman90c99602009-09-02 07:11:14 -0700976MODULE_LICENSE("GPL");
Hank Janssen3e7ee492009-07-13 16:02:34 -0700977module_param(vmbus_irq, int, S_IRUGO);
978module_param(vmbus_loglevel, int, S_IRUGO);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700979
980module_init(vmbus_init);
981module_exit(vmbus_exit);