Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / hv / vmbus.c
CommitLineData
3e7ee490 1/*
3e7ee490
HJ
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 */
5654e932 22#include <linux/kernel.h>
0ffa63b0 23#include <linux/mm.h>
5a0e3ad6 24#include <linux/slab.h>
4983b39a 25#include "osd.h"
645954c5 26#include "logging.h"
2d82f6c7 27#include "version_info.h"
72daf320 28#include "vmbus_private.h"
3e7ee490 29
772ec4e2 30static const char *gDriverName = "vmbus";
454f18a9 31
772ec4e2
GKH
32/*
33 * Windows vmbus does not defined this.
454f18a9
BP
34 * We defined this to be consistent with other devices
35 */
36/* {c5295816-f63a-4d5f-8d1a-4daf999ca185} */
caf26a31
GKH
37static 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 }
3e7ee490
HJ
42};
43
454f18a9 44/* {ac3760fc-9adf-40aa-9427-a70ed6de95c5} */
caf26a31
GKH
45static 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 }
3e7ee490
HJ
50};
51
775ef25e 52static struct hv_driver *gDriver; /* vmbus driver object */
772ec4e2 53static struct hv_device *gDevice; /* vmbus root device */
3e7ee490 54
3e189519 55/*
772ec4e2
GKH
56 * VmbusGetChannelOffers - Retrieve the channel offers from the parent partition
57 */
58static void VmbusGetChannelOffers(void)
3e7ee490 59{
3e7ee490 60 VmbusChannelRequestOffers();
3e7ee490
HJ
61}
62
3e189519 63/*
772ec4e2
GKH
64 * VmbusGetChannelInterface - Get the channel interface
65 */
ee3d7ddf 66static void VmbusGetChannelInterface(struct vmbus_channel_interface *Interface)
3e7ee490
HJ
67{
68 GetChannelInterface(Interface);
69}
70
3e189519 71/*
772ec4e2
GKH
72 * VmbusGetChannelInfo - Get the device info for the specified device object
73 */
74static void VmbusGetChannelInfo(struct hv_device *DeviceObject,
75 struct hv_device_info *DeviceInfo)
3e7ee490
HJ
76{
77 GetChannelInfo(DeviceObject, DeviceInfo);
78}
79
3e189519 80/*
772ec4e2
GKH
81 * VmbusCreateChildDevice - Creates the child device on the bus that represents the channel offer
82 */
daaa8cc3
GKH
83struct hv_device *VmbusChildDeviceCreate(struct hv_guid *DeviceType,
84 struct hv_guid *DeviceInstance,
f346fdc2 85 void *Context)
3e7ee490 86{
ee3d7ddf 87 struct vmbus_driver *vmbusDriver = (struct vmbus_driver *)gDriver;
3e7ee490 88
772ec4e2
GKH
89 return vmbusDriver->OnChildDeviceCreate(DeviceType, DeviceInstance,
90 Context);
3e7ee490
HJ
91}
92
3e189519 93/*
772ec4e2
GKH
94 * VmbusChildDeviceAdd - Registers the child device with the vmbus
95 */
f346fdc2 96int VmbusChildDeviceAdd(struct hv_device *ChildDevice)
3e7ee490 97{
ee3d7ddf 98 struct vmbus_driver *vmbusDriver = (struct vmbus_driver *)gDriver;
3e7ee490
HJ
99
100 return vmbusDriver->OnChildDeviceAdd(gDevice, ChildDevice);
101}
102
3e189519 103/*
772ec4e2
GKH
104 * VmbusChildDeviceRemove Unregisters the child device from the vmbus
105 */
f346fdc2 106void VmbusChildDeviceRemove(struct hv_device *ChildDevice)
3e7ee490 107{
ee3d7ddf 108 struct vmbus_driver *vmbusDriver = (struct vmbus_driver *)gDriver;
3e7ee490
HJ
109
110 vmbusDriver->OnChildDeviceRemove(ChildDevice);
111}
112
3e189519 113/*
772ec4e2
GKH
114 * VmbusOnDeviceAdd - Callback when the root bus device is added
115 */
116static int VmbusOnDeviceAdd(struct hv_device *dev, void *AdditionalInfo)
3e7ee490 117{
772ec4e2
GKH
118 u32 *irqvector = AdditionalInfo;
119 int ret;
3e7ee490 120
3e7ee490
HJ
121 gDevice = dev;
122
caf26a31 123 memcpy(&gDevice->deviceType, &gVmbusDeviceType, sizeof(struct hv_guid));
772ec4e2
GKH
124 memcpy(&gDevice->deviceInstance, &gVmbusDeviceId,
125 sizeof(struct hv_guid));
3e7ee490 126
454f18a9
BP
127 /* strcpy(dev->name, "vmbus"); */
128 /* SynIC setup... */
7692fd4d 129 on_each_cpu(HvSynicInit, (void *)irqvector, 1);
3e7ee490 130
454f18a9 131 /* Connect to VMBus in the root partition */
3e7ee490
HJ
132 ret = VmbusConnect();
133
454f18a9 134 /* VmbusSendEvent(device->localPortId+1); */
3e7ee490
HJ
135 return ret;
136}
137
3e189519 138/*
772ec4e2
GKH
139 * VmbusOnDeviceRemove - Callback when the root bus device is removed
140 */
141static int VmbusOnDeviceRemove(struct hv_device *dev)
3e7ee490 142{
772ec4e2 143 int ret = 0;
3e7ee490 144
3e7ee490 145 VmbusChannelReleaseUnattachedChannels();
3e7ee490 146 VmbusDisconnect();
7692fd4d 147 on_each_cpu(HvSynicCleanup, NULL, 1);
3e7ee490
HJ
148 return ret;
149}
150
3e189519 151/*
772ec4e2
GKH
152 * VmbusOnCleanup - Perform any cleanup when the driver is removed
153 */
154static void VmbusOnCleanup(struct hv_driver *drv)
3e7ee490 155{
ee3d7ddf 156 /* struct vmbus_driver *driver = (struct vmbus_driver *)drv; */
3e7ee490 157
3e7ee490 158 HvCleanup();
3e7ee490
HJ
159}
160
3e189519 161/*
772ec4e2
GKH
162 * VmbusOnMsgDPC - DPC routine to handle messages from the hypervisior
163 */
164static void VmbusOnMsgDPC(struct hv_driver *drv)
3e7ee490 165{
7692fd4d
GKH
166 int cpu = smp_processor_id();
167 void *page_addr = gHvContext.synICMessagePage[cpu];
772ec4e2
GKH
168 struct hv_message *msg = (struct hv_message *)page_addr +
169 VMBUS_MESSAGE_SINT;
eacb1b4d 170 struct hv_message *copied;
772ec4e2
GKH
171
172 while (1) {
173 if (msg->Header.MessageType == HvMessageTypeNone) {
174 /* no msg */
3e7ee490 175 break;
772ec4e2 176 } else {
94002c07 177 copied = kmemdup(msg, sizeof(*copied), GFP_ATOMIC);
3e7ee490 178 if (copied == NULL)
3e7ee490 179 continue;
3e7ee490 180
de65a384
BP
181 osd_schedule_callback(gVmbusConnection.WorkQueue,
182 VmbusOnChannelMessage,
183 (void *)copied);
3e7ee490
HJ
184 }
185
186 msg->Header.MessageType = HvMessageTypeNone;
187
454f18a9
BP
188 /*
189 * Make sure the write to MessageType (ie set to
190 * HvMessageTypeNone) happens before we read the
191 * MessagePending and EOMing. Otherwise, the EOMing
192 * will not deliver any more messages since there is
193 * no empty slot
194 */
28b6ca9c 195 mb();
3e7ee490 196
772ec4e2 197 if (msg->Header.MessageFlags.MessagePending) {
454f18a9
BP
198 /*
199 * This will cause message queue rescan to
200 * possibly deliver another msg from the
201 * hypervisor
202 */
a51ed7d6 203 wrmsrl(HV_X64_MSR_EOM, 0);
3e7ee490
HJ
204 }
205 }
206}
207
3e189519 208/*
772ec4e2
GKH
209 * VmbusOnEventDPC - DPC routine to handle events from the hypervisior
210 */
211static void VmbusOnEventDPC(struct hv_driver *drv)
3e7ee490 212{
454f18a9 213 /* TODO: Process any events */
3e7ee490
HJ
214 VmbusOnEvents();
215}
216
3e189519 217/*
772ec4e2
GKH
218 * VmbusOnISR - ISR routine
219 */
220static int VmbusOnISR(struct hv_driver *drv)
3e7ee490 221{
772ec4e2 222 int ret = 0;
7692fd4d 223 int cpu = smp_processor_id();
3e7ee490 224 void *page_addr;
eacb1b4d
GKH
225 struct hv_message *msg;
226 union hv_synic_event_flags *event;
3e7ee490 227
7692fd4d 228 page_addr = gHvContext.synICMessagePage[cpu];
eacb1b4d 229 msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
3e7ee490 230
454f18a9 231 /* Check if there are actual msgs to be process */
772ec4e2
GKH
232 if (msg->Header.MessageType != HvMessageTypeNone) {
233 DPRINT_DBG(VMBUS, "received msg type %d size %d",
234 msg->Header.MessageType,
235 msg->Header.PayloadSize);
3e7ee490 236 ret |= 0x1;
772ec4e2 237 }
3e7ee490 238
454f18a9 239 /* TODO: Check if there are events to be process */
7692fd4d 240 page_addr = gHvContext.synICEventPage[cpu];
eacb1b4d 241 event = (union hv_synic_event_flags *)page_addr + VMBUS_MESSAGE_SINT;
3e7ee490 242
454f18a9 243 /* Since we are a child, we only need to check bit 0 */
772ec4e2 244 if (test_and_clear_bit(0, (unsigned long *) &event->Flags32[0])) {
3e7ee490
HJ
245 DPRINT_DBG(VMBUS, "received event %d", event->Flags32[0]);
246 ret |= 0x2;
247 }
248
3e7ee490
HJ
249 return ret;
250}
b3bfb3ce 251
3e189519 252/*
b3bfb3ce
GKH
253 * VmbusInitialize - Main entry point
254 */
255int VmbusInitialize(struct hv_driver *drv)
256{
257 struct vmbus_driver *driver = (struct vmbus_driver *)drv;
258 int ret;
259
26c14cc1
HJ
260 DPRINT_INFO(VMBUS, "+++++++ HV Driver version = %s +++++++",
261 HV_DRV_VERSION);
b3bfb3ce
GKH
262 DPRINT_INFO(VMBUS, "+++++++ Vmbus supported version = %d +++++++",
263 VMBUS_REVISION_NUMBER);
264 DPRINT_INFO(VMBUS, "+++++++ Vmbus using SINT %d +++++++",
265 VMBUS_MESSAGE_SINT);
266 DPRINT_DBG(VMBUS, "sizeof(VMBUS_CHANNEL_PACKET_PAGE_BUFFER)=%zd, "
267 "sizeof(VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER)=%zd",
268 sizeof(struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER),
269 sizeof(struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER));
270
271 drv->name = gDriverName;
272 memcpy(&drv->deviceType, &gVmbusDeviceType, sizeof(struct hv_guid));
273
274 /* Setup dispatch table */
275 driver->Base.OnDeviceAdd = VmbusOnDeviceAdd;
276 driver->Base.OnDeviceRemove = VmbusOnDeviceRemove;
277 driver->Base.OnCleanup = VmbusOnCleanup;
278 driver->OnIsr = VmbusOnISR;
279 driver->OnMsgDpc = VmbusOnMsgDPC;
280 driver->OnEventDpc = VmbusOnEventDPC;
281 driver->GetChannelOffers = VmbusGetChannelOffers;
282 driver->GetChannelInterface = VmbusGetChannelInterface;
283 driver->GetChannelInfo = VmbusGetChannelInfo;
284
285 /* Hypervisor initialization...setup hypercall page..etc */
286 ret = HvInit();
287 if (ret != 0)
288 DPRINT_ERR(VMBUS, "Unable to initialize the hypervisor - 0x%x",
289 ret);
290 gDriver = drv;
291
b3bfb3ce
GKH
292 return ret;
293}