f389c3c290fb76c2d7056fe09e96d3703e526c97
[deliverable/linux.git] / drivers / staging / hv / vmbus.c
1 /*
2 * Copyright (c) 2009, Microsoft Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
16 *
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
20 *
21 */
22 #include <linux/kernel.h>
23 #include <linux/mm.h>
24 #include <linux/slab.h>
25 #include "osd.h"
26 #include "logging.h"
27 #include "version_info.h"
28 #include "vmbus_private.h"
29
30 static const char *gDriverName = "vmbus";
31
32 /*
33 * Windows vmbus does not defined this.
34 * We defined this to be consistent with other devices
35 */
36 /* {c5295816-f63a-4d5f-8d1a-4daf999ca185} */
37 static const struct hv_guid gVmbusDeviceType = {
38 .data = {
39 0x16, 0x58, 0x29, 0xc5, 0x3a, 0xf6, 0x5f, 0x4d,
40 0x8d, 0x1a, 0x4d, 0xaf, 0x99, 0x9c, 0xa1, 0x85
41 }
42 };
43
44 /* {ac3760fc-9adf-40aa-9427-a70ed6de95c5} */
45 static const struct hv_guid gVmbusDeviceId = {
46 .data = {
47 0xfc, 0x60, 0x37, 0xac, 0xdf, 0x9a, 0xaa, 0x40,
48 0x94, 0x27, 0xa7, 0x0e, 0xd6, 0xde, 0x95, 0xc5
49 }
50 };
51
52 static struct hv_driver *gDriver; /* vmbus driver object */
53 static struct hv_device *gDevice; /* vmbus root device */
54
55 /*
56 * VmbusGetChannelOffers - Retrieve the channel offers from the parent partition
57 */
58 static void VmbusGetChannelOffers(void)
59 {
60 VmbusChannelRequestOffers();
61 DPRINT_EXIT(VMBUS);
62 }
63
64 /*
65 * VmbusGetChannelInterface - Get the channel interface
66 */
67 static void VmbusGetChannelInterface(struct vmbus_channel_interface *Interface)
68 {
69 GetChannelInterface(Interface);
70 }
71
72 /*
73 * VmbusGetChannelInfo - Get the device info for the specified device object
74 */
75 static void VmbusGetChannelInfo(struct hv_device *DeviceObject,
76 struct hv_device_info *DeviceInfo)
77 {
78 GetChannelInfo(DeviceObject, DeviceInfo);
79 }
80
81 /*
82 * VmbusCreateChildDevice - Creates the child device on the bus that represents the channel offer
83 */
84 struct hv_device *VmbusChildDeviceCreate(struct hv_guid *DeviceType,
85 struct hv_guid *DeviceInstance,
86 void *Context)
87 {
88 struct vmbus_driver *vmbusDriver = (struct vmbus_driver *)gDriver;
89
90 return vmbusDriver->OnChildDeviceCreate(DeviceType, DeviceInstance,
91 Context);
92 }
93
94 /*
95 * VmbusChildDeviceAdd - Registers the child device with the vmbus
96 */
97 int VmbusChildDeviceAdd(struct hv_device *ChildDevice)
98 {
99 struct vmbus_driver *vmbusDriver = (struct vmbus_driver *)gDriver;
100
101 return vmbusDriver->OnChildDeviceAdd(gDevice, ChildDevice);
102 }
103
104 /*
105 * VmbusChildDeviceRemove Unregisters the child device from the vmbus
106 */
107 void VmbusChildDeviceRemove(struct hv_device *ChildDevice)
108 {
109 struct vmbus_driver *vmbusDriver = (struct vmbus_driver *)gDriver;
110
111 vmbusDriver->OnChildDeviceRemove(ChildDevice);
112 }
113
114 /*
115 * VmbusOnDeviceAdd - Callback when the root bus device is added
116 */
117 static int VmbusOnDeviceAdd(struct hv_device *dev, void *AdditionalInfo)
118 {
119 u32 *irqvector = AdditionalInfo;
120 int ret;
121
122 gDevice = dev;
123
124 memcpy(&gDevice->deviceType, &gVmbusDeviceType, sizeof(struct hv_guid));
125 memcpy(&gDevice->deviceInstance, &gVmbusDeviceId,
126 sizeof(struct hv_guid));
127
128 /* strcpy(dev->name, "vmbus"); */
129 /* SynIC setup... */
130 on_each_cpu(HvSynicInit, (void *)irqvector, 1);
131
132 /* Connect to VMBus in the root partition */
133 ret = VmbusConnect();
134
135 /* VmbusSendEvent(device->localPortId+1); */
136 DPRINT_EXIT(VMBUS);
137
138 return ret;
139 }
140
141 /*
142 * VmbusOnDeviceRemove - Callback when the root bus device is removed
143 */
144 static int VmbusOnDeviceRemove(struct hv_device *dev)
145 {
146 int ret = 0;
147
148 VmbusChannelReleaseUnattachedChannels();
149 VmbusDisconnect();
150 on_each_cpu(HvSynicCleanup, NULL, 1);
151 DPRINT_EXIT(VMBUS);
152
153 return ret;
154 }
155
156 /*
157 * VmbusOnCleanup - Perform any cleanup when the driver is removed
158 */
159 static void VmbusOnCleanup(struct hv_driver *drv)
160 {
161 /* struct vmbus_driver *driver = (struct vmbus_driver *)drv; */
162
163 HvCleanup();
164 DPRINT_EXIT(VMBUS);
165 }
166
167 /*
168 * VmbusOnMsgDPC - DPC routine to handle messages from the hypervisior
169 */
170 static void VmbusOnMsgDPC(struct hv_driver *drv)
171 {
172 int cpu = smp_processor_id();
173 void *page_addr = gHvContext.synICMessagePage[cpu];
174 struct hv_message *msg = (struct hv_message *)page_addr +
175 VMBUS_MESSAGE_SINT;
176 struct hv_message *copied;
177
178 while (1) {
179 if (msg->Header.MessageType == HvMessageTypeNone) {
180 /* no msg */
181 break;
182 } else {
183 copied = kmemdup(msg, sizeof(*copied), GFP_ATOMIC);
184 if (copied == NULL)
185 continue;
186
187 osd_schedule_callback(gVmbusConnection.WorkQueue,
188 VmbusOnChannelMessage,
189 (void *)copied);
190 }
191
192 msg->Header.MessageType = HvMessageTypeNone;
193
194 /*
195 * Make sure the write to MessageType (ie set to
196 * HvMessageTypeNone) happens before we read the
197 * MessagePending and EOMing. Otherwise, the EOMing
198 * will not deliver any more messages since there is
199 * no empty slot
200 */
201 mb();
202
203 if (msg->Header.MessageFlags.MessagePending) {
204 /*
205 * This will cause message queue rescan to
206 * possibly deliver another msg from the
207 * hypervisor
208 */
209 wrmsrl(HV_X64_MSR_EOM, 0);
210 }
211 }
212 }
213
214 /*
215 * VmbusOnEventDPC - DPC routine to handle events from the hypervisior
216 */
217 static void VmbusOnEventDPC(struct hv_driver *drv)
218 {
219 /* TODO: Process any events */
220 VmbusOnEvents();
221 }
222
223 /*
224 * VmbusOnISR - ISR routine
225 */
226 static int VmbusOnISR(struct hv_driver *drv)
227 {
228 int ret = 0;
229 int cpu = smp_processor_id();
230 void *page_addr;
231 struct hv_message *msg;
232 union hv_synic_event_flags *event;
233
234 page_addr = gHvContext.synICMessagePage[cpu];
235 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
236
237 /* Check if there are actual msgs to be process */
238 if (msg->Header.MessageType != HvMessageTypeNone) {
239 DPRINT_DBG(VMBUS, "received msg type %d size %d",
240 msg->Header.MessageType,
241 msg->Header.PayloadSize);
242 ret |= 0x1;
243 }
244
245 /* TODO: Check if there are events to be process */
246 page_addr = gHvContext.synICEventPage[cpu];
247 event = (union hv_synic_event_flags *)page_addr + VMBUS_MESSAGE_SINT;
248
249 /* Since we are a child, we only need to check bit 0 */
250 if (test_and_clear_bit(0, (unsigned long *) &event->Flags32[0])) {
251 DPRINT_DBG(VMBUS, "received event %d", event->Flags32[0]);
252 ret |= 0x2;
253 }
254
255 DPRINT_EXIT(VMBUS);
256 return ret;
257 }
258
259 /*
260 * VmbusInitialize - Main entry point
261 */
262 int VmbusInitialize(struct hv_driver *drv)
263 {
264 struct vmbus_driver *driver = (struct vmbus_driver *)drv;
265 int ret;
266
267 DPRINT_INFO(VMBUS, "+++++++ HV Driver version = %s +++++++",
268 HV_DRV_VERSION);
269 DPRINT_INFO(VMBUS, "+++++++ Vmbus supported version = %d +++++++",
270 VMBUS_REVISION_NUMBER);
271 DPRINT_INFO(VMBUS, "+++++++ Vmbus using SINT %d +++++++",
272 VMBUS_MESSAGE_SINT);
273 DPRINT_DBG(VMBUS, "sizeof(VMBUS_CHANNEL_PACKET_PAGE_BUFFER)=%zd, "
274 "sizeof(VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER)=%zd",
275 sizeof(struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER),
276 sizeof(struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER));
277
278 drv->name = gDriverName;
279 memcpy(&drv->deviceType, &gVmbusDeviceType, sizeof(struct hv_guid));
280
281 /* Setup dispatch table */
282 driver->Base.OnDeviceAdd = VmbusOnDeviceAdd;
283 driver->Base.OnDeviceRemove = VmbusOnDeviceRemove;
284 driver->Base.OnCleanup = VmbusOnCleanup;
285 driver->OnIsr = VmbusOnISR;
286 driver->OnMsgDpc = VmbusOnMsgDPC;
287 driver->OnEventDpc = VmbusOnEventDPC;
288 driver->GetChannelOffers = VmbusGetChannelOffers;
289 driver->GetChannelInterface = VmbusGetChannelInterface;
290 driver->GetChannelInfo = VmbusGetChannelInfo;
291
292 /* Hypervisor initialization...setup hypercall page..etc */
293 ret = HvInit();
294 if (ret != 0)
295 DPRINT_ERR(VMBUS, "Unable to initialize the hypervisor - 0x%x",
296 ret);
297 gDriver = drv;
298
299 DPRINT_EXIT(VMBUS);
300
301 return ret;
302 }
This page took 0.051067 seconds and 4 git commands to generate.