forked from virtio-win/kvm-guest-drivers-windows
-
Notifications
You must be signed in to change notification settings - Fork 2
/
VirtIOPCICommon.c
executable file
·412 lines (354 loc) · 11.9 KB
/
VirtIOPCICommon.c
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
/*
* Virtio PCI driver - common functionality for all device versions
*
* Copyright IBM Corp. 2007
* Copyright Red Hat, Inc. 2014
*
* Authors:
* Anthony Liguori <[email protected]>
* Rusty Russell <[email protected]>
* Michael S. Tsirkin <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met :
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and / or other materials provided with the distribution.
* 3. Neither the names of the copyright holders nor the names of their contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "osdep.h"
#include "virtio_pci.h"
#include "virtio.h"
#include "kdebugprint.h"
#include <stddef.h>
#include "virtio_pci_common.h"
NTSTATUS virtio_device_initialize(VirtIODevice *vdev,
const VirtIOSystemOps *pSystemOps,
PVOID DeviceContext,
bool msix_used)
{
NTSTATUS status;
RtlZeroMemory(vdev, sizeof(VirtIODevice));
vdev->DeviceContext = DeviceContext;
vdev->system = pSystemOps;
vdev->msix_used = msix_used;
vdev->info = vdev->inline_info;
vdev->maxQueues = ARRAYSIZE(vdev->inline_info);
status = vio_modern_initialize(vdev);
if (status == STATUS_DEVICE_NOT_CONNECTED) {
/* fall back to legacy virtio device */
status = vio_legacy_initialize(vdev);
}
if (NT_SUCCESS(status)) {
/* Always start by resetting the device */
virtio_device_reset(vdev);
/* Acknowledge that we've seen the device. */
virtio_add_status(vdev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
/* If we are here, we must have found a driver for the device */
virtio_add_status(vdev, VIRTIO_CONFIG_S_DRIVER);
}
return status;
}
void virtio_device_shutdown(VirtIODevice *vdev)
{
if (vdev->info &&
vdev->info != vdev->inline_info) {
mem_free_nonpaged_block(vdev, vdev->info);
vdev->info = NULL;
}
}
u8 virtio_get_status(VirtIODevice *vdev)
{
return vdev->device->get_status(vdev);
}
void virtio_set_status(VirtIODevice *vdev, u8 status)
{
vdev->device->set_status(vdev, status);
}
void virtio_add_status(VirtIODevice *vdev, u8 status)
{
vdev->device->set_status(vdev, (u8)(vdev->device->get_status(vdev) | status));
}
void virtio_device_reset(VirtIODevice *vdev)
{
vdev->device->reset(vdev);
}
void virtio_device_ready(VirtIODevice *vdev)
{
unsigned status = vdev->device->get_status(vdev);
ASSERT(!(status & VIRTIO_CONFIG_S_DRIVER_OK));
vdev->device->set_status(vdev, (u8)(status | VIRTIO_CONFIG_S_DRIVER_OK));
}
u64 virtio_get_features(VirtIODevice *vdev)
{
return vdev->device->get_features(vdev);
}
NTSTATUS virtio_set_features(VirtIODevice *vdev, u64 features)
{
unsigned char dev_status;
NTSTATUS status;
vdev->event_suppression_enabled = virtio_is_feature_enabled(features, VIRTIO_RING_F_EVENT_IDX);
vdev->packed_ring = virtio_is_feature_enabled(features, VIRTIO_F_RING_PACKED);
status = vdev->device->set_features(vdev, features);
if (!NT_SUCCESS(status)) {
return status;
}
if (!virtio_is_feature_enabled(features, VIRTIO_F_VERSION_1)) {
return status;
}
virtio_add_status(vdev, VIRTIO_CONFIG_S_FEATURES_OK);
dev_status = vdev->device->get_status(vdev);
if (!(dev_status & VIRTIO_CONFIG_S_FEATURES_OK)) {
DPrintf(0, "virtio: device refuses features: %x\n", dev_status);
status = STATUS_INVALID_PARAMETER;
}
return status;
}
/* Read @count fields, @bytes each. */
static void virtio_cread_many(VirtIODevice *vdev,
unsigned int offset,
void *buf, size_t count, size_t bytes)
{
u32 old, gen = vdev->device->get_config_generation ?
vdev->device->get_config_generation(vdev) : 0;
size_t i;
do {
old = gen;
for (i = 0; i < count; i++) {
vdev->device->get_config(vdev, (unsigned)(offset + bytes * i),
(char *)buf + i * bytes, (unsigned)bytes);
}
gen = vdev->device->get_config_generation ?
vdev->device->get_config_generation(vdev) : 0;
} while (gen != old);
}
void virtio_get_config(VirtIODevice *vdev, unsigned offset,
void *buf, unsigned len)
{
switch (len) {
case 1:
case 2:
case 4:
vdev->device->get_config(vdev, offset, buf, len);
break;
case 8:
virtio_cread_many(vdev, offset, buf, 2, sizeof(u32));
break;
default:
virtio_cread_many(vdev, offset, buf, len, 1);
break;
}
}
/* Write @count fields, @bytes each. */
static void virtio_cwrite_many(VirtIODevice *vdev,
unsigned int offset,
void *buf, size_t count, size_t bytes)
{
size_t i;
for (i = 0; i < count; i++) {
vdev->device->set_config(vdev, (unsigned)(offset + bytes * i),
(char *)buf + i * bytes, (unsigned)bytes);
}
}
void virtio_set_config(VirtIODevice *vdev, unsigned offset,
void *buf, unsigned len)
{
switch (len) {
case 1:
case 2:
case 4:
vdev->device->set_config(vdev, offset, buf, len);
break;
case 8:
virtio_cwrite_many(vdev, offset, buf, 2, sizeof(u32));
break;
default:
virtio_cwrite_many(vdev, offset, buf, len, 1);
break;
}
}
NTSTATUS virtio_query_queue_allocation(VirtIODevice *vdev,
unsigned index,
unsigned short *pNumEntries,
unsigned long *pRingSize,
unsigned long *pHeapSize)
{
return vdev->device->query_queue_alloc(vdev, index, pNumEntries, pRingSize, pHeapSize);
}
NTSTATUS virtio_reserve_queue_memory(VirtIODevice *vdev, unsigned nvqs)
{
if (nvqs > vdev->maxQueues) {
/* allocate new space for queue infos */
void *new_info = mem_alloc_nonpaged_block(vdev, nvqs * virtio_get_queue_descriptor_size());
if (!new_info) {
return STATUS_INSUFFICIENT_RESOURCES;
}
if (vdev->info && vdev->info != vdev->inline_info) {
mem_free_nonpaged_block(vdev, vdev->info);
}
vdev->info = new_info;
vdev->maxQueues = nvqs;
}
return STATUS_SUCCESS;
}
static NTSTATUS vp_setup_vq(struct virtqueue **queue,
VirtIODevice *vdev, unsigned index,
u16 msix_vec)
{
VirtIOQueueInfo *info = &vdev->info[index];
NTSTATUS status = vdev->device->setup_queue(queue, vdev, info, index, msix_vec);
if (NT_SUCCESS(status)) {
info->vq = *queue;
}
return status;
}
NTSTATUS virtio_find_queue(VirtIODevice *vdev, unsigned index,
struct virtqueue **vq)
{
u16 msix_vec = vdev_get_msix_vector(vdev, index);
return vp_setup_vq(
vq,
vdev,
index,
msix_vec);
}
NTSTATUS virtio_find_queues(VirtIODevice *vdev,
unsigned nvqs,
struct virtqueue *vqs[])
{
unsigned i;
NTSTATUS status;
u16 msix_vec;
status = virtio_reserve_queue_memory(vdev, nvqs);
if (!NT_SUCCESS(status)) {
return status;
}
/* set up the device config interrupt */
msix_vec = vdev_get_msix_vector(vdev, -1);
if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
msix_vec = vdev->device->set_config_vector(vdev, msix_vec);
/* Verify we had enough resources to assign the vector */
if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
status = STATUS_DEVICE_BUSY;
goto error_find;
}
}
/* set up queue interrupts */
for (i = 0; i < nvqs; i++) {
msix_vec = vdev_get_msix_vector(vdev, i);
status = vp_setup_vq(
&vqs[i],
vdev,
i,
msix_vec);
if (!NT_SUCCESS(status)) {
goto error_find;
}
}
return STATUS_SUCCESS;
error_find:
virtio_delete_queues(vdev);
return status;
}
void virtio_delete_queue(struct virtqueue *vq)
{
VirtIODevice *vdev = vq->vdev;
unsigned i = vq->index;
vdev->device->delete_queue(&vdev->info[i]);
vdev->info[i].vq = NULL;
}
void virtio_delete_queues(VirtIODevice *vdev)
{
struct virtqueue *vq;
unsigned i;
if (vdev->info == NULL)
return;
for (i = 0; i < vdev->maxQueues; i++) {
vq = vdev->info[i].vq;
if (vq != NULL) {
vdev->device->delete_queue(&vdev->info[i]);
vdev->info[i].vq = NULL;
}
}
}
u32 virtio_get_queue_size(struct virtqueue *vq)
{
return vq->vdev->info[vq->index].num;
}
u16 virtio_set_config_vector(VirtIODevice *vdev, u16 vector)
{
return vdev->device->set_config_vector(vdev, vector);
}
u16 virtio_set_queue_vector(struct virtqueue *vq, u16 vector)
{
return vq->vdev->device->set_queue_vector(vq, vector);
}
u8 virtio_read_isr_status(VirtIODevice *vdev)
{
return ioread8(vdev, vdev->isr);
}
int virtio_get_bar_index(PPCI_COMMON_HEADER pPCIHeader, PHYSICAL_ADDRESS BasePA)
{
int iBar, i;
/* no point in supporting PCI and CardBus bridges */
ASSERT((pPCIHeader->HeaderType & ~PCI_MULTIFUNCTION) == PCI_DEVICE_TYPE);
for (i = 0; i < PCI_TYPE0_ADDRESSES; i++) {
PHYSICAL_ADDRESS BAR;
BAR.LowPart = pPCIHeader->u.type0.BaseAddresses[i];
iBar = i;
if (BAR.LowPart & PCI_ADDRESS_IO_SPACE) {
/* I/O space */
BAR.LowPart &= PCI_ADDRESS_IO_ADDRESS_MASK;
BAR.HighPart = 0;
} else if ((BAR.LowPart & PCI_ADDRESS_MEMORY_TYPE_MASK) == PCI_TYPE_64BIT) {
/* memory space 64-bit */
BAR.LowPart &= PCI_ADDRESS_MEMORY_ADDRESS_MASK;
BAR.HighPart = pPCIHeader->u.type0.BaseAddresses[++i];
} else {
/* memory space 32-bit */
BAR.LowPart &= PCI_ADDRESS_MEMORY_ADDRESS_MASK;
BAR.HighPart = 0;
}
if (BAR.QuadPart == BasePA.QuadPart) {
return iBar;
}
}
return -1;
}
/* The notify function used when creating a virt queue, common to both modern
* and legacy (the difference is in how vq->notification_addr is set up).
*/
bool vp_notify(struct virtqueue *vq)
{
/* we write the queue's selector into the notification register to
* signal the other end */
iowrite16(vq->vdev, (unsigned short)vq->index, vq->notification_addr);
DPrintf(6, "virtio: vp_notify vq->index = %x\n", vq->index);
return true;
}
void virtqueue_notify(struct virtqueue *vq)
{
vq->notification_cb(vq);
}
void virtqueue_kick(struct virtqueue *vq)
{
if (virtqueue_kick_prepare(vq)) {
virtqueue_notify(vq);
}
}