aboutsummaryrefslogtreecommitdiff
path: root/drivers/staging/hv/Vmbus.c
blob: 54a120dc05568766dc16c8ea875509be114e2938 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
 *
 * Copyright (c) 2009, Microsoft Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307 USA.
 *
 * Authors:
 *   Haiyang Zhang <haiyangz@microsoft.com>
 *   Hank Janssen  <hjanssen@microsoft.com>
 *
 */


#include "logging.h"
#include "VersionInfo.h"
#include "VmbusPrivate.h"

//
// Globals
//
static const char* gDriverName="vmbus";

// Windows vmbus does not defined this. We defined this to be consistent with other devices
//{c5295816-f63a-4d5f-8d1a-4daf999ca185}
static const GUID gVmbusDeviceType={
	.Data = {0x16, 0x58, 0x29, 0xc5, 0x3a, 0xf6, 0x5f, 0x4d, 0x8d, 0x1a, 0x4d, 0xaf, 0x99, 0x9c, 0xa1, 0x85}
};

//{ac3760fc-9adf-40aa-9427-a70ed6de95c5}
static const GUID gVmbusDeviceId={
	.Data = {0xfc, 0x60, 0x37, 0xac, 0xdf, 0x9a, 0xaa, 0x40, 0x94, 0x27, 0xa7, 0x0e, 0xd6, 0xde, 0x95, 0xc5}
};

static DRIVER_OBJECT* gDriver; // vmbus driver object
static DEVICE_OBJECT* gDevice; // vmbus root device


//
// Internal routines
//

static void
VmbusGetChannelInterface(
	VMBUS_CHANNEL_INTERFACE *Interface
	);

static void
VmbusGetChannelInfo(
	DEVICE_OBJECT	*DeviceObject,
	DEVICE_INFO		*DeviceInfo
	);

static void
VmbusGetChannelOffers(
	void
	);

static int
VmbusOnDeviceAdd(
	DEVICE_OBJECT	*Device,
	void			*AdditionalInfo
	);

static int
VmbusOnDeviceRemove(
	DEVICE_OBJECT* dev
	);

static void
VmbusOnCleanup(
	DRIVER_OBJECT* drv
	);

static int
VmbusOnISR(
	DRIVER_OBJECT* drv
	);

static void
VmbusOnMsgDPC(
	DRIVER_OBJECT* drv
	);

static void
VmbusOnEventDPC(
	DRIVER_OBJECT* drv
	);

/*++;

Name:
	VmbusInitialize()

Description:
	Main entry point

--*/
int
VmbusInitialize(
	DRIVER_OBJECT* drv
	)
{
	VMBUS_DRIVER_OBJECT* driver = (VMBUS_DRIVER_OBJECT*)drv;
	int ret=0;

	DPRINT_ENTER(VMBUS);

	DPRINT_INFO(VMBUS, "+++++++ Build Date=%s %s +++++++", VersionDate, VersionTime);
	DPRINT_INFO(VMBUS, "+++++++ Build Description=%s +++++++", VersionDesc);

	DPRINT_INFO(VMBUS, "+++++++ Vmbus supported version = %d +++++++", VMBUS_REVISION_NUMBER);
	DPRINT_INFO(VMBUS, "+++++++ Vmbus using SINT %d +++++++", VMBUS_MESSAGE_SINT);

	DPRINT_DBG(VMBUS, "sizeof(VMBUS_CHANNEL_PACKET_PAGE_BUFFER)=%d, sizeof(VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER)=%d",
		sizeof(VMBUS_CHANNEL_PACKET_PAGE_BUFFER), sizeof(VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER));

	drv->name = gDriverName;
	memcpy(&drv->deviceType, &gVmbusDeviceType, sizeof(GUID));

	// Setup dispatch table
	driver->Base.OnDeviceAdd		= VmbusOnDeviceAdd;
	driver->Base.OnDeviceRemove		= VmbusOnDeviceRemove;
	driver->Base.OnCleanup			= VmbusOnCleanup;
	driver->OnIsr					= VmbusOnISR;
	driver->OnMsgDpc				= VmbusOnMsgDPC;
	driver->OnEventDpc				= VmbusOnEventDPC;
	driver->GetChannelOffers		= VmbusGetChannelOffers;
	driver->GetChannelInterface		= VmbusGetChannelInterface;
	driver->GetChannelInfo			= VmbusGetChannelInfo;

	// Hypervisor initialization...setup hypercall page..etc
	ret = HvInit();
	if (ret != 0)
	{
		DPRINT_ERR(VMBUS, "Unable to initialize the hypervisor - 0x%x", ret);
	}

	gDriver = drv;

	DPRINT_EXIT(VMBUS);

	return ret;
}


/*++;

Name:
	VmbusGetChannelOffers()

Description:
	Retrieve the channel offers from the parent partition

--*/

static void
VmbusGetChannelOffers(void)
{
	DPRINT_ENTER(VMBUS);
	VmbusChannelRequestOffers();
	DPRINT_EXIT(VMBUS);
}


/*++;

Name:
	VmbusGetChannelInterface()

Description:
	Get the channel interface

--*/
static void
VmbusGetChannelInterface(
	VMBUS_CHANNEL_INTERFACE *Interface
	)
{
	GetChannelInterface(Interface);
}


/*++;

Name:
	VmbusGetChannelInterface()

Description:
	Get the device info for the specified device object

--*/
static void
VmbusGetChannelInfo(
	DEVICE_OBJECT	*DeviceObject,
	DEVICE_INFO		*DeviceInfo
	)
{
	GetChannelInfo(DeviceObject, DeviceInfo);
}



/*++

Name:
	VmbusCreateChildDevice()

Description:
	Creates the child device on the bus that represents the channel offer

--*/

DEVICE_OBJECT*
VmbusChildDeviceCreate(
	GUID DeviceType,
	GUID DeviceInstance,
	void *Context)
{
	VMBUS_DRIVER_OBJECT* vmbusDriver = (VMBUS_DRIVER_OBJECT*)gDriver;

	return vmbusDriver->OnChildDeviceCreate(
		DeviceType,
		DeviceInstance,
		Context);
}


/*++

Name:
	VmbusChildDeviceAdd()

Description:
	Registers the child device with the vmbus

--*/
int
VmbusChildDeviceAdd(
   DEVICE_OBJECT* ChildDevice)
{
	VMBUS_DRIVER_OBJECT* vmbusDriver = (VMBUS_DRIVER_OBJECT*)gDriver;

	return vmbusDriver->OnChildDeviceAdd(gDevice, ChildDevice);
}


/*++

Name:
	VmbusChildDeviceRemove()

Description:
	Unregisters the child device from the vmbus

--*/
void
VmbusChildDeviceRemove(
   DEVICE_OBJECT* ChildDevice)
{
	VMBUS_DRIVER_OBJECT* vmbusDriver = (VMBUS_DRIVER_OBJECT*)gDriver;

	vmbusDriver->OnChildDeviceRemove(ChildDevice);
}

/*++

Name:
	VmbusChildDeviceDestroy()

Description:
	Release the child device from the vmbus

--*/
//void
//VmbusChildDeviceDestroy(
//	DEVICE_OBJECT* ChildDevice
//	)
//{
//	VMBUS_DRIVER_OBJECT* vmbusDriver = (VMBUS_DRIVER_OBJECT*)gDriver;
//
//	vmbusDriver->OnChildDeviceDestroy(ChildDevice);
//}

/*++

Name:
	VmbusOnDeviceAdd()

Description:
	Callback when the root bus device is added

--*/
static int
VmbusOnDeviceAdd(
	DEVICE_OBJECT	*dev,
	void			*AdditionalInfo
	)
{
	UINT32 *irqvector = (UINT32*) AdditionalInfo;
	int ret=0;

	DPRINT_ENTER(VMBUS);

	gDevice = dev;

	memcpy(&gDevice->deviceType, &gVmbusDeviceType, sizeof(GUID));
	memcpy(&gDevice->deviceInstance, &gVmbusDeviceId, sizeof(GUID));

	//strcpy(dev->name, "vmbus");
	// SynIC setup...
	ret = HvSynicInit(*irqvector);

	// Connect to VMBus in the root partition
	ret = VmbusConnect();

	//VmbusSendEvent(device->localPortId+1);
	DPRINT_EXIT(VMBUS);

	return ret;
}


/*++

Name:
	VmbusOnDeviceRemove()

Description:
	Callback when the root bus device is removed

--*/
int VmbusOnDeviceRemove(
	DEVICE_OBJECT* dev
	)
{
	int ret=0;

	DPRINT_ENTER(VMBUS);

	VmbusChannelReleaseUnattachedChannels();

	VmbusDisconnect();

	HvSynicCleanup();

	DPRINT_EXIT(VMBUS);

	return ret;
}


/*++

Name:
	VmbusOnCleanup()

Description:
	Perform any cleanup when the driver is removed

--*/
void
VmbusOnCleanup(
	DRIVER_OBJECT* drv
	)
{
	//VMBUS_DRIVER_OBJECT* driver = (VMBUS_DRIVER_OBJECT*)drv;

	DPRINT_ENTER(VMBUS);

	HvCleanup();

	DPRINT_EXIT(VMBUS);
}


/*++

Name:
	VmbusOnMsgDPC()

Description:
	DPC routine to handle messages from the hypervisior

--*/
void
VmbusOnMsgDPC(
	DRIVER_OBJECT* drv
	)
{
	void *page_addr = gHvContext.synICMessagePage[0];

	HV_MESSAGE* msg = (HV_MESSAGE*)page_addr + VMBUS_MESSAGE_SINT;
	HV_MESSAGE *copied;
	while (1)
	{
		if (msg->Header.MessageType == HvMessageTypeNone) // no msg
		{
			break;
		}
		else
		{
			copied = MemAllocAtomic(sizeof(HV_MESSAGE));
			if (copied == NULL)
			{
				continue;
			}

			memcpy(copied, msg, sizeof(HV_MESSAGE));
			WorkQueueQueueWorkItem(gVmbusConnection.WorkQueue, VmbusOnChannelMessage, (void*)copied);
		}

		msg->Header.MessageType = HvMessageTypeNone;

		// Make sure the write to MessageType (ie set to HvMessageTypeNone) happens
		// before we read the MessagePending and EOMing. Otherwise, the EOMing will not deliver
		// any more messages since there is no empty slot
		MemoryFence();

		if (msg->Header.MessageFlags.MessagePending)
		{
			// This will cause message queue rescan to possibly deliver another msg from the hypervisor
			WriteMsr(HV_X64_MSR_EOM, 0);
		}
	}
}

/*++

Name:
	VmbusOnEventDPC()

Description:
	DPC routine to handle events from the hypervisior

--*/
void
VmbusOnEventDPC(
	DRIVER_OBJECT* drv
	)
{
	// TODO: Process any events
	VmbusOnEvents();
}


/*++

Name:
	VmbusOnISR()

Description:
	ISR routine

--*/
int
VmbusOnISR(
	DRIVER_OBJECT* drv
	)
{
	//VMBUS_DRIVER_OBJECT* driver = (VMBUS_DRIVER_OBJECT*)drv;

	int ret=0;
	//struct page* page;
	void *page_addr;
	HV_MESSAGE* msg;
	HV_SYNIC_EVENT_FLAGS* event;

	//page = SynICMessagePage[0];
	//page_addr = page_address(page);
	page_addr = gHvContext.synICMessagePage[0];
	msg = (HV_MESSAGE*)page_addr + VMBUS_MESSAGE_SINT;

	DPRINT_ENTER(VMBUS);

	// Check if there are actual msgs to be process
	if (msg->Header.MessageType != HvMessageTypeNone)
    {
		DPRINT_DBG(VMBUS, "received msg type %d size %d", msg->Header.MessageType, msg->Header.PayloadSize);
		ret |= 0x1;
    }

	// TODO: Check if there are events to be process
	page_addr = gHvContext.synICEventPage[0];
	event = (HV_SYNIC_EVENT_FLAGS*)page_addr + VMBUS_MESSAGE_SINT;

	// Since we are a child, we only need to check bit 0
	if (BitTestAndClear(&event->Flags32[0], 0))
	{
		DPRINT_DBG(VMBUS, "received event %d", event->Flags32[0]);
		ret |= 0x2;
	}

	DPRINT_EXIT(VMBUS);
	return ret;
}

// eof