-
Notifications
You must be signed in to change notification settings - Fork 9
/
virtio_pci.c
executable file
·251 lines (225 loc) · 8.27 KB
/
virtio_pci.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
/*
* Virtio PCI driver
*
* Copyright IBM Corp. 2007
*
* Authors:
* Anthony Liguori <[email protected]>
* Windows porting - Yan Vugenfirer <[email protected]>
* StorPort/ScsiPort code adjustment Vadim Rozenfeld <[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_stor_utils.h"
#include "virtio_stor_hw_helper.h"
#include "virtio_stor.h"
#if defined(EVENT_TRACING)
#include "virtio_pci.tmh"
#endif
/* The lower 64k of memory is never mapped so we can use the same routines
* for both port I/O and memory access and use the address alone to decide
* which space to use.
*/
#define PORT_MASK 0xFFFF
static u32 ReadVirtIODeviceRegister(ULONG_PTR ulRegister)
{
if (ulRegister & ~PORT_MASK) {
return StorPortReadRegisterUlong(NULL, (PULONG)(ulRegister));
} else {
return StorPortReadPortUlong(NULL, (PULONG)(ulRegister));
}
}
static void WriteVirtIODeviceRegister(ULONG_PTR ulRegister, u32 ulValue)
{
if (ulRegister & ~PORT_MASK) {
StorPortWriteRegisterUlong(NULL, (PULONG)(ulRegister), (ULONG)(ulValue));
} else {
StorPortWritePortUlong(NULL, (PULONG)(ulRegister), (ULONG)(ulValue));
}
}
static u8 ReadVirtIODeviceByte(ULONG_PTR ulRegister)
{
if (ulRegister & ~PORT_MASK) {
return StorPortReadRegisterUchar(NULL, (PUCHAR)(ulRegister));
} else {
return StorPortReadPortUchar(NULL, (PUCHAR)(ulRegister));
}
}
static void WriteVirtIODeviceByte(ULONG_PTR ulRegister, u8 bValue)
{
if (ulRegister & ~PORT_MASK) {
StorPortWriteRegisterUchar(NULL, (PUCHAR)(ulRegister), (UCHAR)(bValue));
} else {
StorPortWritePortUchar(NULL, (PUCHAR)(ulRegister), (UCHAR)(bValue));
}
}
static u16 ReadVirtIODeviceWord(ULONG_PTR ulRegister)
{
if (ulRegister & ~PORT_MASK) {
return StorPortReadRegisterUshort(NULL, (PUSHORT)(ulRegister));
} else {
return StorPortReadPortUshort(NULL, (PUSHORT)(ulRegister));
}
}
static void WriteVirtIODeviceWord(ULONG_PTR ulRegister, u16 wValue)
{
if (ulRegister & ~PORT_MASK) {
StorPortWriteRegisterUshort(NULL, (PUSHORT)(ulRegister), (USHORT)(wValue));
} else {
StorPortWritePortUshort(NULL, (PUSHORT)(ulRegister), (USHORT)(wValue));
}
}
static void *mem_alloc_contiguous_pages(void *context, size_t size)
{
PADAPTER_EXTENSION adaptExt = (PADAPTER_EXTENSION)context;
PVOID ptr = (PVOID)((ULONG_PTR)adaptExt->pageAllocationVa + adaptExt->pageOffset);
if ((adaptExt->pageOffset + size) <= adaptExt->pageAllocationSize) {
size = ROUND_TO_PAGES(size);
adaptExt->pageOffset += size;
RtlZeroMemory(ptr, size);
return ptr;
} else {
RhelDbgPrint(TRACE_LEVEL_FATAL, " Ran out of memory in (%Id)\n", size);
return NULL;
}
}
static void mem_free_contiguous_pages(void *context, void *virt)
{
UNREFERENCED_PARAMETER(context);
UNREFERENCED_PARAMETER(virt);
/* We allocate pages from a single uncached extension by simply moving the
* adaptExt->allocationOffset pointer forward. Nothing to do here.
*/
}
static ULONGLONG mem_get_physical_address(void *context, void *virt)
{
ULONG uLength;
STOR_PHYSICAL_ADDRESS pa = StorPortGetPhysicalAddress(context, NULL, virt, &uLength);
return pa.QuadPart;
}
static void *mem_alloc_nonpaged_block(void *context, size_t size)
{
return VioStorPoolAlloc(context, size);
}
static void mem_free_nonpaged_block(void *context, void *addr)
{
/* We allocate memory from a single non-paged pool allocation by simply moving
* the adaptExt->poolOffset pointer forward. Nothing to do here.
*/
}
static int pci_read_config_byte(void *context, int where, u8 *bVal)
{
PADAPTER_EXTENSION adaptExt = (PADAPTER_EXTENSION)context;
*bVal = adaptExt->pci_config_buf[where];
return 0;
}
static int pci_read_config_word(void *context, int where, u16 *wVal)
{
PADAPTER_EXTENSION adaptExt = (PADAPTER_EXTENSION)context;
*wVal = *(u16 *)&adaptExt->pci_config_buf[where];
return 0;
}
static int pci_read_config_dword(void *context, int where, u32 *dwVal)
{
PADAPTER_EXTENSION adaptExt = (PADAPTER_EXTENSION)context;
*dwVal = *(u32 *)&adaptExt->pci_config_buf[where];
return 0;
}
static size_t pci_get_resource_len(void *context, int bar)
{
PADAPTER_EXTENSION adaptExt = (PADAPTER_EXTENSION)context;
if (bar < PCI_TYPE0_ADDRESSES) {
return adaptExt->pci_bars[bar].uLength;
}
return 0;
}
static void *pci_map_address_range(void *context, int bar, size_t offset, size_t maxlen)
{
PADAPTER_EXTENSION adaptExt = (PADAPTER_EXTENSION)context;
if (bar < PCI_TYPE0_ADDRESSES) {
PVIRTIO_BAR pBar = &adaptExt->pci_bars[bar];
if (pBar->pBase == NULL) {
pBar->pBase = StorPortGetDeviceBase(
adaptExt,
PCIBus,
adaptExt->system_io_bus_number,
pBar->BasePA,
pBar->uLength,
!!pBar->bPortSpace);
}
if (pBar->pBase != NULL && offset < pBar->uLength) {
return (PUCHAR)pBar->pBase + offset;
}
}
return NULL;
}
static u16 vdev_get_msix_vector(void *context, int queue)
{
PADAPTER_EXTENSION adaptExt = (PADAPTER_EXTENSION)context;
u16 vector = VIRTIO_MSI_NO_VECTOR;
if (queue >= 0) {
/* queue interrupt */
if (adaptExt->msix_enabled) {
if (adaptExt->msix_one_vector) {
vector = 0;
} else {
vector = queue + 1;
}
}
} else {
/* on-device-config-change interrupt */
if (!adaptExt->msix_one_vector) {
vector = VIRTIO_BLK_MSIX_CONFIG_VECTOR;
}
}
return vector;
}
static void vdev_sleep(void *context, unsigned int msecs)
{
UNREFERENCED_PARAMETER(context);
/* We can't really sleep in a storage miniport so we just busy wait. */
StorPortStallExecution(1000 * msecs);
}
VirtIOSystemOps VioStorSystemOps = {
.vdev_read_byte = ReadVirtIODeviceByte,
.vdev_read_word = ReadVirtIODeviceWord,
.vdev_read_dword = ReadVirtIODeviceRegister,
.vdev_write_byte = WriteVirtIODeviceByte,
.vdev_write_word = WriteVirtIODeviceWord,
.vdev_write_dword = WriteVirtIODeviceRegister,
.mem_alloc_contiguous_pages = mem_alloc_contiguous_pages,
.mem_free_contiguous_pages = mem_free_contiguous_pages,
.mem_get_physical_address = mem_get_physical_address,
.mem_alloc_nonpaged_block = mem_alloc_nonpaged_block,
.mem_free_nonpaged_block = mem_free_nonpaged_block,
.pci_read_config_byte = pci_read_config_byte,
.pci_read_config_word = pci_read_config_word,
.pci_read_config_dword = pci_read_config_dword,
.pci_get_resource_len = pci_get_resource_len,
.pci_map_address_range = pci_map_address_range,
.vdev_get_msix_vector = vdev_get_msix_vector,
.vdev_sleep = vdev_sleep,
};