Staging: hv: move logging.h
[linux-2.6/mini2440.git] / drivers / staging / hv / Vmbus.c
blob5c4c96f649a88dea07ee8e4b7ec9b43bc3a70e6a
1 /*
3 * Copyright (c) 2009, Microsoft Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307 USA.
18 * Authors:
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
24 #include <linux/kernel.h>
25 #include <linux/mm.h>
26 #include "osd.h"
27 #include "logging.h"
28 #include "VersionInfo.h"
29 #include "VmbusPrivate.h"
32 /* Globals */
34 static const char* gDriverName="vmbus";
36 /* Windows vmbus does not defined this.
37 * We defined this to be consistent with other devices
39 /* {c5295816-f63a-4d5f-8d1a-4daf999ca185} */
40 static const struct hv_guid gVmbusDeviceType = {
41 .data = {
42 0x16, 0x58, 0x29, 0xc5, 0x3a, 0xf6, 0x5f, 0x4d,
43 0x8d, 0x1a, 0x4d, 0xaf, 0x99, 0x9c, 0xa1, 0x85
47 /* {ac3760fc-9adf-40aa-9427-a70ed6de95c5} */
48 static const struct hv_guid gVmbusDeviceId = {
49 .data = {
50 0xfc, 0x60, 0x37, 0xac, 0xdf, 0x9a, 0xaa, 0x40,
51 0x94, 0x27, 0xa7, 0x0e, 0xd6, 0xde, 0x95, 0xc5
55 static struct hv_driver *gDriver; /* vmbus driver object */
56 static struct hv_device* gDevice; /* vmbus root device */
60 /* Internal routines */
63 static void VmbusGetChannelInterface(struct vmbus_channel_interface *Interface);
65 static void
66 VmbusGetChannelInfo(
67 struct hv_device *DeviceObject,
68 struct hv_device_info *DeviceInfo
71 static void
72 VmbusGetChannelOffers(
73 void
76 static int
77 VmbusOnDeviceAdd(
78 struct hv_device *Device,
79 void *AdditionalInfo
82 static int
83 VmbusOnDeviceRemove(
84 struct hv_device *dev
87 static void
88 VmbusOnCleanup(
89 struct hv_driver *drv
92 static int
93 VmbusOnISR(
94 struct hv_driver *drv
97 static void
98 VmbusOnMsgDPC(
99 struct hv_driver *drv
102 static void
103 VmbusOnEventDPC(
104 struct hv_driver *drv
107 /*++;
109 Name:
110 VmbusInitialize()
112 Description:
113 Main entry point
115 --*/
117 VmbusInitialize(
118 struct hv_driver *drv
121 struct vmbus_driver *driver = (struct vmbus_driver *)drv;
122 int ret=0;
124 DPRINT_ENTER(VMBUS);
126 DPRINT_INFO(VMBUS, "+++++++ Build Date=%s %s +++++++", VersionDate, VersionTime);
127 DPRINT_INFO(VMBUS, "+++++++ Build Description=%s +++++++", VersionDesc);
129 DPRINT_INFO(VMBUS, "+++++++ Vmbus supported version = %d +++++++", VMBUS_REVISION_NUMBER);
130 DPRINT_INFO(VMBUS, "+++++++ Vmbus using SINT %d +++++++", VMBUS_MESSAGE_SINT);
132 DPRINT_DBG(VMBUS, "sizeof(VMBUS_CHANNEL_PACKET_PAGE_BUFFER)=%zd, sizeof(VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER)=%zd",
133 sizeof(struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER), sizeof(struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER));
135 drv->name = gDriverName;
136 memcpy(&drv->deviceType, &gVmbusDeviceType, sizeof(struct hv_guid));
138 /* Setup dispatch table */
139 driver->Base.OnDeviceAdd = VmbusOnDeviceAdd;
140 driver->Base.OnDeviceRemove = VmbusOnDeviceRemove;
141 driver->Base.OnCleanup = VmbusOnCleanup;
142 driver->OnIsr = VmbusOnISR;
143 driver->OnMsgDpc = VmbusOnMsgDPC;
144 driver->OnEventDpc = VmbusOnEventDPC;
145 driver->GetChannelOffers = VmbusGetChannelOffers;
146 driver->GetChannelInterface = VmbusGetChannelInterface;
147 driver->GetChannelInfo = VmbusGetChannelInfo;
149 /* Hypervisor initialization...setup hypercall page..etc */
150 ret = HvInit();
151 if (ret != 0)
153 DPRINT_ERR(VMBUS, "Unable to initialize the hypervisor - 0x%x", ret);
156 gDriver = drv;
158 DPRINT_EXIT(VMBUS);
160 return ret;
164 /*++;
166 Name:
167 VmbusGetChannelOffers()
169 Description:
170 Retrieve the channel offers from the parent partition
172 --*/
174 static void
175 VmbusGetChannelOffers(void)
177 DPRINT_ENTER(VMBUS);
178 VmbusChannelRequestOffers();
179 DPRINT_EXIT(VMBUS);
183 /*++;
185 Name:
186 VmbusGetChannelInterface()
188 Description:
189 Get the channel interface
191 --*/
192 static void VmbusGetChannelInterface(struct vmbus_channel_interface *Interface)
194 GetChannelInterface(Interface);
198 /*++;
200 Name:
201 VmbusGetChannelInfo()
203 Description:
204 Get the device info for the specified device object
206 --*/
207 static void
208 VmbusGetChannelInfo(
209 struct hv_device *DeviceObject,
210 struct hv_device_info *DeviceInfo
213 GetChannelInfo(DeviceObject, DeviceInfo);
218 /*++
220 Name:
221 VmbusCreateChildDevice()
223 Description:
224 Creates the child device on the bus that represents the channel offer
226 --*/
228 struct hv_device *VmbusChildDeviceCreate(struct hv_guid *DeviceType,
229 struct hv_guid *DeviceInstance,
230 void *Context)
232 struct vmbus_driver *vmbusDriver = (struct vmbus_driver *)gDriver;
234 return vmbusDriver->OnChildDeviceCreate(
235 DeviceType,
236 DeviceInstance,
237 Context);
241 /*++
243 Name:
244 VmbusChildDeviceAdd()
246 Description:
247 Registers the child device with the vmbus
249 --*/
250 int VmbusChildDeviceAdd(struct hv_device *ChildDevice)
252 struct vmbus_driver *vmbusDriver = (struct vmbus_driver *)gDriver;
254 return vmbusDriver->OnChildDeviceAdd(gDevice, ChildDevice);
258 /*++
260 Name:
261 VmbusChildDeviceRemove()
263 Description:
264 Unregisters the child device from the vmbus
266 --*/
267 void VmbusChildDeviceRemove(struct hv_device *ChildDevice)
269 struct vmbus_driver *vmbusDriver = (struct vmbus_driver *)gDriver;
271 vmbusDriver->OnChildDeviceRemove(ChildDevice);
274 /*++
276 Name:
277 VmbusChildDeviceDestroy()
279 Description:
280 Release the child device from the vmbus
282 --*/
284 /* **************
285 void
286 VmbusChildDeviceDestroy(
287 struct hv_device *ChildDevice
290 struct vmbus_driver *vmbusDriver = (struct vmbus_driver *)gDriver;
292 vmbusDriver->OnChildDeviceDestroy(ChildDevice);
294 ************* */
296 /*++
298 Name:
299 VmbusOnDeviceAdd()
301 Description:
302 Callback when the root bus device is added
304 --*/
305 static int
306 VmbusOnDeviceAdd(
307 struct hv_device *dev,
308 void *AdditionalInfo
311 u32 *irqvector = (u32*) AdditionalInfo;
312 int ret=0;
314 DPRINT_ENTER(VMBUS);
316 gDevice = dev;
318 memcpy(&gDevice->deviceType, &gVmbusDeviceType, sizeof(struct hv_guid));
319 memcpy(&gDevice->deviceInstance, &gVmbusDeviceId, sizeof(struct hv_guid));
321 /* strcpy(dev->name, "vmbus"); */
322 /* SynIC setup... */
323 ret = HvSynicInit(*irqvector);
325 /* Connect to VMBus in the root partition */
326 ret = VmbusConnect();
328 /* VmbusSendEvent(device->localPortId+1); */
329 DPRINT_EXIT(VMBUS);
331 return ret;
335 /*++
337 Name:
338 VmbusOnDeviceRemove()
340 Description:
341 Callback when the root bus device is removed
343 --*/
344 static int VmbusOnDeviceRemove(
345 struct hv_device *dev
348 int ret=0;
350 DPRINT_ENTER(VMBUS);
352 VmbusChannelReleaseUnattachedChannels();
354 VmbusDisconnect();
356 HvSynicCleanup();
358 DPRINT_EXIT(VMBUS);
360 return ret;
364 /*++
366 Name:
367 VmbusOnCleanup()
369 Description:
370 Perform any cleanup when the driver is removed
372 --*/
373 static void
374 VmbusOnCleanup(
375 struct hv_driver *drv
378 /* struct vmbus_driver *driver = (struct vmbus_driver *)drv; */
380 DPRINT_ENTER(VMBUS);
382 HvCleanup();
384 DPRINT_EXIT(VMBUS);
388 /*++
390 Name:
391 VmbusOnMsgDPC()
393 Description:
394 DPC routine to handle messages from the hypervisior
396 --*/
397 static void
398 VmbusOnMsgDPC(
399 struct hv_driver *drv
402 void *page_addr = gHvContext.synICMessagePage[0];
404 struct hv_message *msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
405 struct hv_message *copied;
406 while (1)
408 if (msg->Header.MessageType == HvMessageTypeNone) /* no msg */
410 break;
412 else
414 copied = kmalloc(sizeof(*copied), GFP_ATOMIC);
415 if (copied == NULL)
417 continue;
420 memcpy(copied, msg, sizeof(*copied));
421 osd_schedule_callback(gVmbusConnection.WorkQueue,
422 VmbusOnChannelMessage,
423 (void *)copied);
426 msg->Header.MessageType = HvMessageTypeNone;
429 * Make sure the write to MessageType (ie set to
430 * HvMessageTypeNone) happens before we read the
431 * MessagePending and EOMing. Otherwise, the EOMing
432 * will not deliver any more messages since there is
433 * no empty slot
435 mb();
437 if (msg->Header.MessageFlags.MessagePending)
440 * This will cause message queue rescan to
441 * possibly deliver another msg from the
442 * hypervisor
444 wrmsrl(HV_X64_MSR_EOM, 0);
449 /*++
451 Name:
452 VmbusOnEventDPC()
454 Description:
455 DPC routine to handle events from the hypervisior
457 --*/
458 static void
459 VmbusOnEventDPC(
460 struct hv_driver* drv
463 /* TODO: Process any events */
464 VmbusOnEvents();
468 /*++
470 Name:
471 VmbusOnISR()
473 Description:
474 ISR routine
476 --*/
477 static int
478 VmbusOnISR(
479 struct hv_driver *drv
482 /* struct vmbus_driver *driver = (struct vmbus_driver *)drv; */
484 int ret=0;
485 /* struct page* page; */
486 void *page_addr;
487 struct hv_message *msg;
488 union hv_synic_event_flags *event;
490 /* page = SynICMessagePage[0]; */
491 /* page_addr = page_address(page); */
492 page_addr = gHvContext.synICMessagePage[0];
493 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
495 DPRINT_ENTER(VMBUS);
497 /* Check if there are actual msgs to be process */
498 if (msg->Header.MessageType != HvMessageTypeNone)
500 DPRINT_DBG(VMBUS, "received msg type %d size %d", msg->Header.MessageType, msg->Header.PayloadSize);
501 ret |= 0x1;
504 /* TODO: Check if there are events to be process */
505 page_addr = gHvContext.synICEventPage[0];
506 event = (union hv_synic_event_flags *)page_addr + VMBUS_MESSAGE_SINT;
508 /* Since we are a child, we only need to check bit 0 */
509 if (test_and_clear_bit(0, (unsigned long *) &event->Flags32[0]))
511 DPRINT_DBG(VMBUS, "received event %d", event->Flags32[0]);
512 ret |= 0x2;
515 DPRINT_EXIT(VMBUS);
516 return ret;
519 /* eof */