Staging: hv: remove wrapper functions for bit operations
[deliverable/linux.git] / drivers / staging / hv / Vmbus.c
1 /*
2 *
3 * Copyright (c) 2009, Microsoft Corporation.
4 *
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.
8 *
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.
13 *
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.
17 *
18 * Authors:
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
21 *
22 */
23
24 #include <linux/kernel.h>
25 #include <linux/mm.h>
26 #include "include/logging.h"
27 #include "VersionInfo.h"
28 #include "VmbusPrivate.h"
29
30
31 /* Globals */
32
33 static const char* gDriverName="vmbus";
34
35 /* Windows vmbus does not defined this.
36 * We defined this to be consistent with other devices
37 */
38 /* {c5295816-f63a-4d5f-8d1a-4daf999ca185} */
39 static const GUID gVmbusDeviceType={
40 .Data = {0x16, 0x58, 0x29, 0xc5, 0x3a, 0xf6, 0x5f, 0x4d, 0x8d, 0x1a, 0x4d, 0xaf, 0x99, 0x9c, 0xa1, 0x85}
41 };
42
43 /* {ac3760fc-9adf-40aa-9427-a70ed6de95c5} */
44 static const GUID gVmbusDeviceId={
45 .Data = {0xfc, 0x60, 0x37, 0xac, 0xdf, 0x9a, 0xaa, 0x40, 0x94, 0x27, 0xa7, 0x0e, 0xd6, 0xde, 0x95, 0xc5}
46 };
47
48 static struct hv_driver *gDriver; /* vmbus driver object */
49 static struct hv_device* gDevice; /* vmbus root device */
50
51
52
53 /* Internal routines */
54
55
56 static void
57 VmbusGetChannelInterface(
58 VMBUS_CHANNEL_INTERFACE *Interface
59 );
60
61 static void
62 VmbusGetChannelInfo(
63 struct hv_device *DeviceObject,
64 DEVICE_INFO *DeviceInfo
65 );
66
67 static void
68 VmbusGetChannelOffers(
69 void
70 );
71
72 static int
73 VmbusOnDeviceAdd(
74 struct hv_device *Device,
75 void *AdditionalInfo
76 );
77
78 static int
79 VmbusOnDeviceRemove(
80 struct hv_device *dev
81 );
82
83 static void
84 VmbusOnCleanup(
85 struct hv_driver *drv
86 );
87
88 static int
89 VmbusOnISR(
90 struct hv_driver *drv
91 );
92
93 static void
94 VmbusOnMsgDPC(
95 struct hv_driver *drv
96 );
97
98 static void
99 VmbusOnEventDPC(
100 struct hv_driver *drv
101 );
102
103 /*++;
104
105 Name:
106 VmbusInitialize()
107
108 Description:
109 Main entry point
110
111 --*/
112 int
113 VmbusInitialize(
114 struct hv_driver *drv
115 )
116 {
117 VMBUS_DRIVER_OBJECT* driver = (VMBUS_DRIVER_OBJECT*)drv;
118 int ret=0;
119
120 DPRINT_ENTER(VMBUS);
121
122 DPRINT_INFO(VMBUS, "+++++++ Build Date=%s %s +++++++", VersionDate, VersionTime);
123 DPRINT_INFO(VMBUS, "+++++++ Build Description=%s +++++++", VersionDesc);
124
125 DPRINT_INFO(VMBUS, "+++++++ Vmbus supported version = %d +++++++", VMBUS_REVISION_NUMBER);
126 DPRINT_INFO(VMBUS, "+++++++ Vmbus using SINT %d +++++++", VMBUS_MESSAGE_SINT);
127
128 DPRINT_DBG(VMBUS, "sizeof(VMBUS_CHANNEL_PACKET_PAGE_BUFFER)=%zd, sizeof(VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER)=%zd",
129 sizeof(struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER), sizeof(struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER));
130
131 drv->name = gDriverName;
132 memcpy(&drv->deviceType, &gVmbusDeviceType, sizeof(GUID));
133
134 /* Setup dispatch table */
135 driver->Base.OnDeviceAdd = VmbusOnDeviceAdd;
136 driver->Base.OnDeviceRemove = VmbusOnDeviceRemove;
137 driver->Base.OnCleanup = VmbusOnCleanup;
138 driver->OnIsr = VmbusOnISR;
139 driver->OnMsgDpc = VmbusOnMsgDPC;
140 driver->OnEventDpc = VmbusOnEventDPC;
141 driver->GetChannelOffers = VmbusGetChannelOffers;
142 driver->GetChannelInterface = VmbusGetChannelInterface;
143 driver->GetChannelInfo = VmbusGetChannelInfo;
144
145 /* Hypervisor initialization...setup hypercall page..etc */
146 ret = HvInit();
147 if (ret != 0)
148 {
149 DPRINT_ERR(VMBUS, "Unable to initialize the hypervisor - 0x%x", ret);
150 }
151
152 gDriver = drv;
153
154 DPRINT_EXIT(VMBUS);
155
156 return ret;
157 }
158
159
160 /*++;
161
162 Name:
163 VmbusGetChannelOffers()
164
165 Description:
166 Retrieve the channel offers from the parent partition
167
168 --*/
169
170 static void
171 VmbusGetChannelOffers(void)
172 {
173 DPRINT_ENTER(VMBUS);
174 VmbusChannelRequestOffers();
175 DPRINT_EXIT(VMBUS);
176 }
177
178
179 /*++;
180
181 Name:
182 VmbusGetChannelInterface()
183
184 Description:
185 Get the channel interface
186
187 --*/
188 static void
189 VmbusGetChannelInterface(
190 VMBUS_CHANNEL_INTERFACE *Interface
191 )
192 {
193 GetChannelInterface(Interface);
194 }
195
196
197 /*++;
198
199 Name:
200 VmbusGetChannelInterface()
201
202 Description:
203 Get the device info for the specified device object
204
205 --*/
206 static void
207 VmbusGetChannelInfo(
208 struct hv_device *DeviceObject,
209 DEVICE_INFO *DeviceInfo
210 )
211 {
212 GetChannelInfo(DeviceObject, DeviceInfo);
213 }
214
215
216
217 /*++
218
219 Name:
220 VmbusCreateChildDevice()
221
222 Description:
223 Creates the child device on the bus that represents the channel offer
224
225 --*/
226
227 static struct hv_device*
228 VmbusChildDeviceCreate(
229 GUID DeviceType,
230 GUID DeviceInstance,
231 void *Context)
232 {
233 VMBUS_DRIVER_OBJECT* vmbusDriver = (VMBUS_DRIVER_OBJECT*)gDriver;
234
235 return vmbusDriver->OnChildDeviceCreate(
236 DeviceType,
237 DeviceInstance,
238 Context);
239 }
240
241
242 /*++
243
244 Name:
245 VmbusChildDeviceAdd()
246
247 Description:
248 Registers the child device with the vmbus
249
250 --*/
251 static int
252 VmbusChildDeviceAdd(
253 struct hv_device *ChildDevice)
254 {
255 VMBUS_DRIVER_OBJECT* vmbusDriver = (VMBUS_DRIVER_OBJECT*)gDriver;
256
257 return vmbusDriver->OnChildDeviceAdd(gDevice, ChildDevice);
258 }
259
260
261 /*++
262
263 Name:
264 VmbusChildDeviceRemove()
265
266 Description:
267 Unregisters the child device from the vmbus
268
269 --*/
270 static void
271 VmbusChildDeviceRemove(
272 struct hv_device *ChildDevice)
273 {
274 VMBUS_DRIVER_OBJECT* vmbusDriver = (VMBUS_DRIVER_OBJECT*)gDriver;
275
276 vmbusDriver->OnChildDeviceRemove(ChildDevice);
277 }
278
279 /*++
280
281 Name:
282 VmbusChildDeviceDestroy()
283
284 Description:
285 Release the child device from the vmbus
286
287 --*/
288
289 /* **************
290 void
291 VmbusChildDeviceDestroy(
292 struct hv_device *ChildDevice
293 )
294 {
295 VMBUS_DRIVER_OBJECT* vmbusDriver = (VMBUS_DRIVER_OBJECT*)gDriver;
296
297 vmbusDriver->OnChildDeviceDestroy(ChildDevice);
298 }
299 ************* */
300
301 /*++
302
303 Name:
304 VmbusOnDeviceAdd()
305
306 Description:
307 Callback when the root bus device is added
308
309 --*/
310 static int
311 VmbusOnDeviceAdd(
312 struct hv_device *dev,
313 void *AdditionalInfo
314 )
315 {
316 u32 *irqvector = (u32*) AdditionalInfo;
317 int ret=0;
318
319 DPRINT_ENTER(VMBUS);
320
321 gDevice = dev;
322
323 memcpy(&gDevice->deviceType, &gVmbusDeviceType, sizeof(GUID));
324 memcpy(&gDevice->deviceInstance, &gVmbusDeviceId, sizeof(GUID));
325
326 /* strcpy(dev->name, "vmbus"); */
327 /* SynIC setup... */
328 ret = HvSynicInit(*irqvector);
329
330 /* Connect to VMBus in the root partition */
331 ret = VmbusConnect();
332
333 /* VmbusSendEvent(device->localPortId+1); */
334 DPRINT_EXIT(VMBUS);
335
336 return ret;
337 }
338
339
340 /*++
341
342 Name:
343 VmbusOnDeviceRemove()
344
345 Description:
346 Callback when the root bus device is removed
347
348 --*/
349 static int VmbusOnDeviceRemove(
350 struct hv_device *dev
351 )
352 {
353 int ret=0;
354
355 DPRINT_ENTER(VMBUS);
356
357 VmbusChannelReleaseUnattachedChannels();
358
359 VmbusDisconnect();
360
361 HvSynicCleanup();
362
363 DPRINT_EXIT(VMBUS);
364
365 return ret;
366 }
367
368
369 /*++
370
371 Name:
372 VmbusOnCleanup()
373
374 Description:
375 Perform any cleanup when the driver is removed
376
377 --*/
378 static void
379 VmbusOnCleanup(
380 struct hv_driver *drv
381 )
382 {
383 /* VMBUS_DRIVER_OBJECT* driver = (VMBUS_DRIVER_OBJECT*)drv; */
384
385 DPRINT_ENTER(VMBUS);
386
387 HvCleanup();
388
389 DPRINT_EXIT(VMBUS);
390 }
391
392
393 /*++
394
395 Name:
396 VmbusOnMsgDPC()
397
398 Description:
399 DPC routine to handle messages from the hypervisior
400
401 --*/
402 static void
403 VmbusOnMsgDPC(
404 struct hv_driver *drv
405 )
406 {
407 void *page_addr = gHvContext.synICMessagePage[0];
408
409 HV_MESSAGE* msg = (HV_MESSAGE*)page_addr + VMBUS_MESSAGE_SINT;
410 HV_MESSAGE *copied;
411 while (1)
412 {
413 if (msg->Header.MessageType == HvMessageTypeNone) /* no msg */
414 {
415 break;
416 }
417 else
418 {
419 copied = kmalloc(sizeof(HV_MESSAGE), GFP_ATOMIC);
420 if (copied == NULL)
421 {
422 continue;
423 }
424
425 memcpy(copied, msg, sizeof(HV_MESSAGE));
426 osd_schedule_callback(gVmbusConnection.WorkQueue,
427 VmbusOnChannelMessage,
428 (void *)copied);
429 }
430
431 msg->Header.MessageType = HvMessageTypeNone;
432
433 /*
434 * Make sure the write to MessageType (ie set to
435 * HvMessageTypeNone) happens before we read the
436 * MessagePending and EOMing. Otherwise, the EOMing
437 * will not deliver any more messages since there is
438 * no empty slot
439 */
440 mb();
441
442 if (msg->Header.MessageFlags.MessagePending)
443 {
444 /*
445 * This will cause message queue rescan to
446 * possibly deliver another msg from the
447 * hypervisor
448 */
449 WriteMsr(HV_X64_MSR_EOM, 0);
450 }
451 }
452 }
453
454 /*++
455
456 Name:
457 VmbusOnEventDPC()
458
459 Description:
460 DPC routine to handle events from the hypervisior
461
462 --*/
463 static void
464 VmbusOnEventDPC(
465 struct hv_driver* drv
466 )
467 {
468 /* TODO: Process any events */
469 VmbusOnEvents();
470 }
471
472
473 /*++
474
475 Name:
476 VmbusOnISR()
477
478 Description:
479 ISR routine
480
481 --*/
482 static int
483 VmbusOnISR(
484 struct hv_driver *drv
485 )
486 {
487 /* VMBUS_DRIVER_OBJECT* driver = (VMBUS_DRIVER_OBJECT*)drv; */
488
489 int ret=0;
490 /* struct page* page; */
491 void *page_addr;
492 HV_MESSAGE* msg;
493 HV_SYNIC_EVENT_FLAGS* event;
494
495 /* page = SynICMessagePage[0]; */
496 /* page_addr = page_address(page); */
497 page_addr = gHvContext.synICMessagePage[0];
498 msg = (HV_MESSAGE*)page_addr + VMBUS_MESSAGE_SINT;
499
500 DPRINT_ENTER(VMBUS);
501
502 /* Check if there are actual msgs to be process */
503 if (msg->Header.MessageType != HvMessageTypeNone)
504 {
505 DPRINT_DBG(VMBUS, "received msg type %d size %d", msg->Header.MessageType, msg->Header.PayloadSize);
506 ret |= 0x1;
507 }
508
509 /* TODO: Check if there are events to be process */
510 page_addr = gHvContext.synICEventPage[0];
511 event = (HV_SYNIC_EVENT_FLAGS*)page_addr + VMBUS_MESSAGE_SINT;
512
513 /* Since we are a child, we only need to check bit 0 */
514 if (test_and_clear_bit(0, (unsigned long *) &event->Flags32[0]))
515 {
516 DPRINT_DBG(VMBUS, "received event %d", event->Flags32[0]);
517 ret |= 0x2;
518 }
519
520 DPRINT_EXIT(VMBUS);
521 return ret;
522 }
523
524 /* eof */
This page took 0.065106 seconds and 5 git commands to generate.