-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathvirtio_storage.c
More file actions
347 lines (318 loc) · 13.4 KB
/
virtio_storage.c
File metadata and controls
347 lines (318 loc) · 13.4 KB
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
#include <kernel.h>
#include <storage.h>
#include "virtio_internal.h"
#include "virtio_mmio.h"
#include "virtio_pci.h"
//#define VIRTIO_BLK_DEBUG
#ifdef VIRTIO_BLK_DEBUG
#define virtio_blk_debug(x, ...) do {tprintf(sym(vtblk), 0, ss(x), ##__VA_ARGS__);} while(0)
#else
#define virtio_blk_debug(x, ...)
#endif
// this is not really a struct...fix the general encoding problem
typedef struct virtio_blk_req {
u32 type;
u32 reserved;
u64 sector;
u8 status;
} __attribute__((packed)) *virtio_blk_req;
// device configuration offsets
struct virtio_blk_config {
u64 capacity;
u32 size_max;
u32 seg_max;
struct virtio_blk_geometry {
u16 cylinders;
u8 heads;
u8 sectors;
} geometry;
u32 blk_size;
struct virtio_blk_topology {
// # of logical blocks per physical block (log2)
u8 physical_block_exp;
// offset of first aligned logical block
u8 alignment_offset;
// suggested minimum I/O size in blocks
u16 min_io_size;
// optimal (suggested maximum) I/O size in blocks
u32 opt_io_size;
} topology;
u8 writeback;
u8 unused0[3];
u32 max_discard_sectors;
u32 max_discard_seg;
u32 discard_sector_alignment;
u32 max_write_zeroes_sectors;
u32 max_write_zeroes_seg;
u8 write_zeroes_may_unmap;
u8 unused1[3];
} __attribute__((packed));
#define VIRTIO_BLK_F_SIZE_MAX U64_FROM_BIT(1)
#define VIRTIO_BLK_F_SEG_MAX U64_FROM_BIT(2)
#define VIRTIO_BLK_F_GEOMETRY U64_FROM_BIT(4)
#define VIRTIO_BLK_F_RO U64_FROM_BIT(5)
#define VIRTIO_BLK_F_BLK_SIZE U64_FROM_BIT(6)
#define VIRTIO_BLK_F_FLUSH U64_FROM_BIT(9)
#define VIRTIO_BLK_F_TOPOLOGY U64_FROM_BIT(10)
#define VIRTIO_BLK_F_CONFIG_WCE U64_FROM_BIT(11)
#define VIRTIO_BLK_R_CAPACITY_LOW (offsetof(struct virtio_blk_config *, capacity))
#define VIRTIO_BLK_R_CAPACITY_HIGH (offsetof(struct virtio_blk_config *, capacity) + 4)
#define VIRTIO_BLK_R_SIZE_MAX (offsetof(struct virtio_blk_config *, size_max))
#define VIRTIO_BLK_R_SEG_MAX (offsetof(struct virtio_blk_config *, seg_max))
#define VIRTIO_BLK_R_GEOM_CYLINDERS (offsetof(struct virtio_blk_config *, geometry) + offsetof(struct virtio_blk_geometry *, cylinders))
#define VIRTIO_BLK_R_GEOM_HEADS (offsetof(struct virtio_blk_config *, geometry) + offsetof(struct virtio_blk_geometry *, heads))
#define VIRTIO_BLK_R_GEOM_SECTORS (offsetof(struct virtio_blk_config *, geometry) + offsetof(struct virtio_blk_geometry *, sectors))
#define VIRTIO_BLK_R_BLOCK_SIZE (offsetof(struct virtio_blk_config *, blk_size))
#define VIRTIO_BLK_R_TOPOLOGY_PHYSICAL_BLOCK_EXP (offsetof(struct virtio_blk_config *, topology) + offsetof(struct virtio_blk_topology *, physical_block_exp))
#define VIRTIO_BLK_R_TOPOLOGY_ALIGNMENT_OFFSET (offsetof(struct virtio_blk_config *, topology) + offsetof(struct virtio_blk_topology *, alignment_offset))
#define VIRTIO_BLK_R_TOPOLOGY_MIN_IO_SIZE (offsetof(struct virtio_blk_config *, topology) + offsetof(struct virtio_blk_topology *, min_io_size))
#define VIRTIO_BLK_R_TOPOLOGY_OPT_IO_SIZE (offsetof(struct virtio_blk_config *, topology) + offsetof(struct virtio_blk_topology *, opt_io_size))
#define VIRTIO_BLK_R_WRITEBACK (offsetof(struct virtio_blk_config *, writeback))
#define VIRTIO_BLK_R_MAX_DISCARD_SECTORS (offsetof(struct virtio_blk_config *, max_discard_sectors))
#define VIRTIO_BLK_R_MAX_DISCARD_SEG (offsetof(struct virtio_blk_config *, max_discard_seg))
#define VIRTIO_BLK_R_DISCARD_SECTOR_ALIGNMENT (offsetof(struct virtio_blk_config *, discard_sector_alignment))
#define VIRTIO_BLK_R_MAX_WRITE_ZEROS_SECTORS (offsetof(struct virtio_blk_config *, max_write_zeroes_sectors))
#define VIRTIO_BLK_R_MAX_WRITE_ZEROS_SEG (offsetof(struct virtio_blk_config *, max_write_zeroes_seg))
#define VIRTIO_BLK_R_WRITE_ZEROS_MAY_UNMAP (offsetof(struct virtio_blk_config *, write_zeroes_may_unmap))
#define VIRTIO_BLK_REQ_HEADER_SIZE 16
#define VIRTIO_BLK_REQ_STATUS_SIZE 1
#define VIRTIO_BLK_T_IN 0
#define VIRTIO_BLK_T_OUT 1
#define VIRTIO_BLK_T_FLUSH 4
#define VIRTIO_BLK_S_OK 0
#define VIRTIO_BLK_S_IOERR 1
#define VIRTIO_BLK_S_UNSUPP 2
#define VIRTIO_BLK_DRIVER_FEATURES \
(VIRTIO_BLK_F_SEG_MAX | VIRTIO_BLK_F_BLK_SIZE | VIRTIO_BLK_F_CONFIG_WCE | VIRTIO_BLK_F_FLUSH)
typedef struct storage {
vtdev v;
closure_struct(storage_req_handler, req_handler);
struct virtqueue *command;
u64 capacity;
u64 block_size;
u32 seg_max;
} *storage;
static virtio_blk_req allocate_virtio_blk_req(storage st, u32 type, u64 sector, u64 *phys)
{
virtio_blk_req req = alloc_map(st->v->contiguous, sizeof(struct virtio_blk_req), phys);
if (req != INVALID_ADDRESS) {
req->type = type;
req->reserved = 0;
req->sector = sector;
req->status = 0;
}
return req;
}
static void deallocate_virtio_blk_req(storage st, virtio_blk_req req, u64 phys)
{
dealloc_unmap(st->v->contiguous, req, phys,
pad(sizeof(struct virtio_blk_req), st->v->contiguous->h.pagesize));
}
closure_function(4, 1, void, complete,
storage, s, status_handler, f, virtio_blk_req, req, u64, phys,
u64 len)
{
status st = 0;
// 1 is io error, 2 is unsupported operation
if (bound(req)->status) st = timm("result", "%d", bound(req)->status);
async_apply_status_handler(bound(f), st);
deallocate_virtio_blk_req(bound(s), bound(req), bound(phys));
closure_finish();
}
static inline void storage_rw_internal(storage st, boolean write, void * buf,
range sectors, status_handler sh)
{
sstring err;
virtio_blk_debug("%s: block range %R cap %ld\n", write ? ss("write") : ss("read"), sectors,
st->capacity);
u64 start_sector = sectors.start;
u64 nsectors = range_span(sectors);
if (nsectors == 0) {
err = ss("length must be > 0");
goto out_inval;
}
u64 req_phys;
virtio_blk_req req = allocate_virtio_blk_req(st, write ? VIRTIO_BLK_T_OUT : VIRTIO_BLK_T_IN,
start_sector, &req_phys);
if (req == INVALID_ADDRESS) {
apply(sh, timm_oom);
return;
}
virtqueue vq = st->command;
vqmsg m = allocate_vqmsg(vq, 3);
assert(m != INVALID_ADDRESS);
vqmsg_push(vq, m, req_phys, VIRTIO_BLK_REQ_HEADER_SIZE, false);
vqmsg_push(vq, m, physical_from_virtual(buf), nsectors * st->block_size, !write);
u64 statusp = req_phys + VIRTIO_BLK_REQ_HEADER_SIZE;
vqmsg_push(vq, m, statusp, VIRTIO_BLK_REQ_STATUS_SIZE, true);
vqfinish c = closure(st->v->general, complete, st, sh, req, req_phys);
vqmsg_commit(vq, m, c);
return;
out_inval:
msg_err("vtblk R/W error: %s", err);
apply(sh, timm("result", "%s", err));
}
static void virtio_storage_io_commit(storage st, virtqueue vq, vqmsg msg, virtio_blk_req req,
u64 req_phys, status_handler completion)
{
if (!vqmsg_push(vq, msg, req_phys + VIRTIO_BLK_REQ_HEADER_SIZE, VIRTIO_BLK_REQ_STATUS_SIZE,
true)) {
deallocate_vqmsg(vq, msg);
apply(completion, timm_oom);
return;
}
vqfinish c = closure(st->v->general, complete, st, completion, req, req_phys);
assert(c != INVALID_ADDRESS);
vqmsg_commit(vq, msg, c);
}
static void virtio_storage_io_sg(storage st, boolean write, sg_list sg, range blocks,
status_handler sh)
{
virtio_blk_debug("SG %c, blocks %R, sh %F\n", write ? 'w' : 'r', blocks, sh);
virtio_blk_req req = 0;
u64 req_phys;
heap h = st->v->general;
virtqueue vq = st->command;
vqmsg msg;
u32 desc_count;
merge m = 0;
while (range_span(blocks)) {
if (!req) {
req = allocate_virtio_blk_req(st, write ? VIRTIO_BLK_T_OUT : VIRTIO_BLK_T_IN,
blocks.start, &req_phys);
if (req == INVALID_ADDRESS) {
apply(sh, timm_oom);
return;
}
msg = allocate_vqmsg(vq, 3);
assert(msg != INVALID_ADDRESS);
vqmsg_push(vq, msg, req_phys, VIRTIO_BLK_REQ_HEADER_SIZE, false);
desc_count = 0;
}
sg_buf sgb = sg_list_head_peek(sg);
u64 length = sg_buf_len(sgb);
assert((length & (st->block_size - 1)) == 0);
length = MIN(range_span(blocks) * st->block_size, length);
if (!vqmsg_push(vq, msg, physical_from_virtual(sgb->buf + sgb->offset), length, !write)) {
deallocate_vqmsg(vq, msg);
if (m)
sh = apply_merge(m);
apply(sh, timm_oom);
return;
}
sg_consume(sg, length);
blocks.start += length / st->block_size;
if (++desc_count == st->seg_max) {
if (!m && range_span(blocks)) {
m = allocate_merge(h, sh);
sh = apply_merge(m);
}
virtio_storage_io_commit(st, vq, msg, req, req_phys, m ? apply_merge(m) : sh);
req = 0;
}
}
if (req) {
virtio_storage_io_commit(st, vq, msg, req, req_phys, m ? apply_merge(m) : sh);
}
if (m)
apply(sh, STATUS_OK);
}
static void storage_flush(storage st, status_handler s)
{
virtio_blk_debug("%s: handler %p (%F)\n", func_ss, s, s);
u64 req_phys;
virtio_blk_req req = allocate_virtio_blk_req(st, VIRTIO_BLK_T_FLUSH, 0, &req_phys);
if (req == INVALID_ADDRESS) {
apply(s, timm_oom);
return;
}
virtqueue vq = st->command;
vqmsg m = allocate_vqmsg(vq, 2);
assert(m != INVALID_ADDRESS);
vqmsg_push(vq, m, req_phys, VIRTIO_BLK_REQ_HEADER_SIZE, false);
vqmsg_push(vq, m, req_phys + VIRTIO_BLK_REQ_HEADER_SIZE, VIRTIO_BLK_REQ_STATUS_SIZE, true);
vqfinish c = closure(st->v->general, complete, st, s, req, req_phys);
assert(c != INVALID_ADDRESS);
vqmsg_commit(vq, m, c);
}
closure_func_basic(storage_req_handler, void, virtio_storage_req_handler,
storage_req req)
{
storage st = struct_from_field(closure_self(), storage, req_handler);
switch (req->op) {
case STORAGE_OP_READSG:
virtio_storage_io_sg(st, false, req->data, req->blocks, req->completion);
break;
case STORAGE_OP_WRITESG:
virtio_storage_io_sg(st, true, req->data, req->blocks, req->completion);
break;
case STORAGE_OP_FLUSH:
if (st->v->features & VIRTIO_BLK_F_FLUSH)
storage_flush(st, req->completion);
else
async_apply_status_handler(req->completion, STATUS_OK);
break;
case STORAGE_OP_READ:
storage_rw_internal(st, false, req->data, req->blocks, req->completion);
break;
case STORAGE_OP_WRITE:
storage_rw_internal(st, true, req->data, req->blocks, req->completion);
break;
}
}
static void virtio_blk_attach(heap general, storage_attach a, vtdev v)
{
storage s = allocate(general, sizeof(struct storage));
assert(s != INVALID_ADDRESS);
s->v = v;
s->block_size = (v->features & VIRTIO_BLK_F_BLK_SIZE) ?
vtdev_cfg_read_4(v, VIRTIO_BLK_R_BLOCK_SIZE) : SECTOR_SIZE;
s->capacity = (vtdev_cfg_read_4(v, VIRTIO_BLK_R_CAPACITY_LOW) |
((u64) vtdev_cfg_read_4(v, VIRTIO_BLK_R_CAPACITY_HIGH) << 32)) * s->block_size;
virtio_blk_debug("%s: capacity 0x%lx, block size 0x%x\n", func_ss, s->capacity, s->block_size);
virtio_alloc_virtqueue(v, ss("virtio blk"), 0, &s->command);
s->seg_max = (v->features & VIRTIO_BLK_F_SEG_MAX) ?
vtdev_cfg_read_4(v, VIRTIO_BLK_R_SEG_MAX) : 1;
if (v->features & VIRTIO_BLK_F_FLUSH) {
if (v->features & VIRTIO_BLK_F_CONFIG_WCE)
vtdev_cfg_write_1(v, VIRTIO_BLK_R_WRITEBACK, 1 /* writeback */);
}
vtdev_set_status(v, VIRTIO_CONFIG_STATUS_DRIVER_OK);
apply(a, init_closure_func(&s->req_handler, storage_req_handler, virtio_storage_req_handler),
s->capacity, -1);
}
closure_function(3, 1, boolean, vtpci_blk_probe,
heap, general, storage_attach, a, backed_heap, page_allocator,
pci_dev d)
{
virtio_blk_debug("%s\n", func_ss);
if (!vtpci_probe(d, VIRTIO_ID_BLOCK))
return false;
virtio_blk_debug(" attaching\n");
heap general = bound(general);
vtdev v = (vtdev)attach_vtpci(general, bound(page_allocator), d, VIRTIO_BLK_DRIVER_FEATURES);
virtio_blk_attach(general, bound(a), v);
return true;
}
closure_function(3, 1, void, vtmmio_blk_probe,
heap, general, storage_attach, a, backed_heap, page_allocator,
vtmmio d)
{
virtio_blk_debug("%s\n", __func__);
if ((vtmmio_get_u32(d, VTMMIO_OFFSET_DEVID) != VIRTIO_ID_BLOCK) ||
(d->memsize < VTMMIO_OFFSET_CONFIG +
sizeof(struct virtio_blk_config)))
return;
virtio_blk_debug(" attaching\n");
heap general = bound(general);
if (attach_vtmmio(general, bound(page_allocator), d, VIRTIO_BLK_DRIVER_FEATURES))
virtio_blk_attach(general, bound(a), (vtdev)d);
}
void init_virtio_blk(kernel_heaps kh, storage_attach a)
{
virtio_blk_debug("%s\n", func_ss);
heap h = heap_locked(kh);
backed_heap page_allocator = heap_linear_backed(kh);
register_pci_driver(closure(h, vtpci_blk_probe, h, a, page_allocator), 0);
vtmmio_probe_devs(stack_closure(vtmmio_blk_probe, h, a, page_allocator));
}