ld_virtio.c revision 1.41 1 /* $NetBSD: ld_virtio.c,v 1.41 2025/02/23 22:04:06 mlelstv Exp $ */
2
3 /*
4 * Copyright (c) 2010 Minoura Makoto.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: ld_virtio.c,v 1.41 2025/02/23 22:04:06 mlelstv Exp $");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/buf.h>
35 #include <sys/bufq.h>
36 #include <sys/bus.h>
37 #include <sys/device.h>
38 #include <sys/disk.h>
39 #include <sys/mutex.h>
40 #include <sys/module.h>
41 #include <sys/kmem.h>
42
43 #include <dev/ldvar.h>
44 #include <dev/pci/virtioreg.h>
45 #include <dev/pci/virtiovar.h>
46
47 #include "ioconf.h"
48
49 /*
50 * ld_virtioreg:
51 */
52 /* Configuration registers */
53 #define VIRTIO_BLK_CONFIG_CAPACITY 0 /* 64bit */
54 #define VIRTIO_BLK_CONFIG_SIZE_MAX 8 /* 32bit */
55 #define VIRTIO_BLK_CONFIG_SEG_MAX 12 /* 32bit */
56 #define VIRTIO_BLK_CONFIG_GEOMETRY_C 16 /* 16bit */
57 #define VIRTIO_BLK_CONFIG_GEOMETRY_H 18 /* 8bit */
58 #define VIRTIO_BLK_CONFIG_GEOMETRY_S 19 /* 8bit */
59 #define VIRTIO_BLK_CONFIG_BLK_SIZE 20 /* 32bit */
60 #define VIRTIO_BLK_CONFIG_WRITEBACK 32 /* 8bit */
61 #define VIRTIO_BLK_CONFIG_NUM_QUEUES 34 /* 16bit */
62 #define VIRTIO_BLK_CONFIG_MAX_DISCARD_SECTORS 36 /* 32bit */
63 #define VIRTIO_BLK_CONFIG_MAX_DISCARD_SEG 40 /* 32bit */
64 #define VIRTIO_BLK_CONFIG_DISCARD_SECTOR_ALIGNMENT 44 /* 32bit */
65
66 /* Feature bits */
67 #define VIRTIO_BLK_F_BARRIER (1<<0)
68 #define VIRTIO_BLK_F_SIZE_MAX (1<<1)
69 #define VIRTIO_BLK_F_SEG_MAX (1<<2)
70 #define VIRTIO_BLK_F_GEOMETRY (1<<4)
71 #define VIRTIO_BLK_F_RO (1<<5)
72 #define VIRTIO_BLK_F_BLK_SIZE (1<<6)
73 #define VIRTIO_BLK_F_SCSI (1<<7)
74 #define VIRTIO_BLK_F_FLUSH (1<<9)
75 #define VIRTIO_BLK_F_TOPOLOGY (1<<10)
76 #define VIRTIO_BLK_F_CONFIG_WCE (1<<11)
77 #define VIRTIO_BLK_F_MQ (1<<12)
78 #define VIRTIO_BLK_F_DISCARD (1<<13)
79 #define VIRTIO_BLK_F_WRITE_ZEROES (1<<14)
80 #define VIRTIO_BLK_F_LIFETIME (1<<15)
81 #define VIRTIO_BLK_F_SECURE_ERASE (1<<16)
82
83 /*
84 * Each block request uses at least two segments - one for the header
85 * and one for the status.
86 */
87 #define VIRTIO_BLK_CTRL_SEGMENTS 2
88
89 #define VIRTIO_BLK_FLAG_BITS \
90 VIRTIO_COMMON_FLAG_BITS \
91 "b\x10" "SECURE_ERASE\0" \
92 "b\x0f" "LIFETIME\0" \
93 "b\x0e" "WRITE_ZEROES\0" \
94 "b\x0d" "DISCARD\0" \
95 "b\x0c" "MQ\0" \
96 "b\x0b" "CONFIG_WCE\0" \
97 "b\x0a" "TOPOLOGY\0" \
98 "b\x09" "FLUSH\0" \
99 "b\x07" "SCSI\0" \
100 "b\x06" "BLK_SIZE\0" \
101 "b\x05" "RO\0" \
102 "b\x04" "GEOMETRY\0" \
103 "b\x02" "SEG_MAX\0" \
104 "b\x01" "SIZE_MAX\0" \
105 "b\x00" "BARRIER\0"
106
107 /* Command */
108 #define VIRTIO_BLK_T_IN 0
109 #define VIRTIO_BLK_T_OUT 1
110 #define VIRTIO_BLK_T_FLUSH 4
111 #define VIRTIO_BLK_T_GET_ID 8
112 #define VIRTIO_BLK_T_GET_LIFETIME 10
113 #define VIRTIO_BLK_T_DISCARD 11
114 #define VIRTIO_BLK_T_WRITE_ZEROES 13
115 #define VIRTIO_BLK_T_SECURE_ERASE 14
116 #define VIRTIO_BLK_T_BARRIER 0x80000000
117
118 /* Sector */
119 #define VIRTIO_BLK_BSIZE 512
120
121 /* Status */
122 #define VIRTIO_BLK_S_OK 0
123 #define VIRTIO_BLK_S_IOERR 1
124 #define VIRTIO_BLK_S_UNSUPP 2
125
126 /* Request header structure */
127 struct virtio_blk_req_hdr {
128 uint32_t type; /* VIRTIO_BLK_T_* */
129 uint32_t ioprio;
130 uint64_t sector;
131 } __packed;
132 /* payload and 1 byte status follows */
133
134 struct virtio_blk_discard_write_zeroes {
135 uint64_t sector;
136 uint32_t num_sectors;
137 union {
138 uint32_t flags;
139 struct {
140 uint32_t unmap:1;
141 uint32_t reserved:31;
142 };
143 };
144 } __packed;
145
146 /*
147 * ld_virtiovar:
148 */
149 struct virtio_blk_req {
150 struct virtio_blk_req_hdr vr_hdr;
151 uint8_t vr_status;
152 struct buf *vr_bp;
153 #define DUMMY_VR_BP ((void *)1)
154 bus_dmamap_t vr_cmdsts;
155 bus_dmamap_t vr_payload;
156 void * vr_datap;
157 size_t vr_datas;
158 };
159
160 struct ld_virtio_softc {
161 struct ld_softc sc_ld;
162 device_t sc_dev;
163
164 uint32_t sc_seg_max; /* max number of segs in xfer */
165 uint32_t sc_size_max; /* max size of single seg */
166
167 struct virtio_softc *sc_virtio;
168 struct virtqueue sc_vq;
169
170 struct virtio_blk_req *sc_reqs;
171 bus_dma_segment_t sc_reqs_seg;
172
173 int sc_readonly;
174
175 enum {
176 SYNC_FREE, SYNC_BUSY, SYNC_DONE
177 } sc_sync_use;
178 kcondvar_t sc_sync_wait;
179 kmutex_t sc_sync_wait_lock;
180 uint8_t sc_sync_status;
181 uint8_t *sc_typename;
182
183 uint32_t sc_max_discard_sectors;
184 uint32_t sc_max_discard_seg;
185 #if 0
186 uint32_t sc_discard_sector_alignment;
187 #endif
188 };
189
190 static int ld_virtio_match(device_t, cfdata_t, void *);
191 static void ld_virtio_attach(device_t, device_t, void *);
192 static int ld_virtio_detach(device_t, int);
193
194 CFATTACH_DECL_NEW(ld_virtio, sizeof(struct ld_virtio_softc),
195 ld_virtio_match, ld_virtio_attach, ld_virtio_detach, NULL);
196
197 static int
198 ld_virtio_match(device_t parent, cfdata_t match, void *aux)
199 {
200 struct virtio_attach_args *va = aux;
201
202 if (va->sc_childdevid == VIRTIO_DEVICE_ID_BLOCK)
203 return 1;
204
205 return 0;
206 }
207
208 static int ld_virtio_vq_done(struct virtqueue *);
209 static int ld_virtio_dump(struct ld_softc *, void *, int, int);
210 static int ld_virtio_start(struct ld_softc *, struct buf *);
211 static int ld_virtio_ioctl(struct ld_softc *, u_long, void *, int32_t, bool);
212 static int ld_virtio_info(struct ld_softc *, bool);
213 static int ld_virtio_discard(struct ld_softc *, struct buf *);
214
215 static int
216 ld_virtio_alloc_reqs(struct ld_virtio_softc *sc, int qsize)
217 {
218 int allocsize, r, rsegs, i;
219 struct ld_softc *ld = &sc->sc_ld;
220 void *vaddr;
221
222 allocsize = sizeof(struct virtio_blk_req) * qsize;
223 r = bus_dmamem_alloc(virtio_dmat(sc->sc_virtio), allocsize, 0, 0,
224 &sc->sc_reqs_seg, 1, &rsegs, BUS_DMA_WAITOK);
225 if (r != 0) {
226 aprint_error_dev(sc->sc_dev,
227 "DMA memory allocation failed, size %d, "
228 "error code %d\n", allocsize, r);
229 goto err_none;
230 }
231 r = bus_dmamem_map(virtio_dmat(sc->sc_virtio),
232 &sc->sc_reqs_seg, 1, allocsize,
233 &vaddr, BUS_DMA_WAITOK);
234 if (r != 0) {
235 aprint_error_dev(sc->sc_dev,
236 "DMA memory map failed, "
237 "error code %d\n", r);
238 goto err_dmamem_alloc;
239 }
240 sc->sc_reqs = vaddr;
241 memset(vaddr, 0, allocsize);
242 for (i = 0; i < qsize; i++) {
243 struct virtio_blk_req *vr = &sc->sc_reqs[i];
244 r = bus_dmamap_create(virtio_dmat(sc->sc_virtio),
245 offsetof(struct virtio_blk_req, vr_bp),
246 1,
247 offsetof(struct virtio_blk_req, vr_bp),
248 0,
249 BUS_DMA_WAITOK|BUS_DMA_ALLOCNOW,
250 &vr->vr_cmdsts);
251 if (r != 0) {
252 aprint_error_dev(sc->sc_dev,
253 "command dmamap creation failed, "
254 "error code %d\n", r);
255 goto err_reqs;
256 }
257 r = bus_dmamap_load(virtio_dmat(sc->sc_virtio), vr->vr_cmdsts,
258 &vr->vr_hdr,
259 offsetof(struct virtio_blk_req, vr_bp),
260 NULL, BUS_DMA_WAITOK);
261 if (r != 0) {
262 aprint_error_dev(sc->sc_dev,
263 "command dmamap load failed, "
264 "error code %d\n", r);
265 goto err_reqs;
266 }
267 r = bus_dmamap_create(virtio_dmat(sc->sc_virtio),
268 /*size*/ld->sc_maxxfer,
269 /*nseg*/sc->sc_seg_max,
270 /*maxsegsz*/sc->sc_size_max,
271 /*boundary*/0,
272 BUS_DMA_WAITOK|BUS_DMA_ALLOCNOW,
273 &vr->vr_payload);
274 if (r != 0) {
275 aprint_error_dev(sc->sc_dev,
276 "payload dmamap creation failed, "
277 "error code %d\n", r);
278 goto err_reqs;
279 }
280 vr->vr_datap = NULL;
281 vr->vr_datas = 0;
282 }
283 return 0;
284
285 err_reqs:
286 for (i = 0; i < qsize; i++) {
287 struct virtio_blk_req *vr = &sc->sc_reqs[i];
288 if (vr->vr_cmdsts) {
289 bus_dmamap_destroy(virtio_dmat(sc->sc_virtio),
290 vr->vr_cmdsts);
291 vr->vr_cmdsts = 0;
292 }
293 if (vr->vr_payload) {
294 bus_dmamap_destroy(virtio_dmat(sc->sc_virtio),
295 vr->vr_payload);
296 vr->vr_payload = 0;
297 }
298 }
299 bus_dmamem_unmap(virtio_dmat(sc->sc_virtio), sc->sc_reqs, allocsize);
300 err_dmamem_alloc:
301 bus_dmamem_free(virtio_dmat(sc->sc_virtio), &sc->sc_reqs_seg, 1);
302 err_none:
303 return -1;
304 }
305
306 static void
307 ld_virtio_attach(device_t parent, device_t self, void *aux)
308 {
309 struct ld_virtio_softc *sc = device_private(self);
310 struct ld_softc *ld = &sc->sc_ld;
311 struct virtio_softc *vsc = device_private(parent);
312 uint64_t features;
313 int qsize;
314
315 if (virtio_child(vsc) != NULL) {
316 aprint_normal(": child already attached for %s; "
317 "something wrong...\n", device_xname(parent));
318 return;
319 }
320
321 sc->sc_dev = self;
322 sc->sc_virtio = vsc;
323
324 virtio_child_attach_start(vsc, self, IPL_BIO,
325 (VIRTIO_BLK_F_SIZE_MAX | VIRTIO_BLK_F_SEG_MAX |
326 VIRTIO_BLK_F_GEOMETRY | VIRTIO_BLK_F_RO | VIRTIO_BLK_F_BLK_SIZE |
327 VIRTIO_BLK_F_FLUSH | VIRTIO_BLK_F_CONFIG_WCE |
328 VIRTIO_BLK_F_DISCARD),
329 VIRTIO_BLK_FLAG_BITS);
330
331 features = virtio_features(vsc);
332 if (features == 0)
333 goto err;
334
335 if (features & VIRTIO_BLK_F_RO)
336 sc->sc_readonly = 1;
337 else
338 sc->sc_readonly = 0;
339
340 if (features & VIRTIO_BLK_F_BLK_SIZE) {
341 ld->sc_secsize = virtio_read_device_config_4(vsc,
342 VIRTIO_BLK_CONFIG_BLK_SIZE);
343 } else
344 ld->sc_secsize = VIRTIO_BLK_BSIZE;
345
346 if (features & VIRTIO_BLK_F_SEG_MAX) {
347 sc->sc_seg_max = virtio_read_device_config_4(vsc,
348 VIRTIO_BLK_CONFIG_SEG_MAX);
349 if (sc->sc_seg_max == 0) {
350 aprint_error_dev(sc->sc_dev,
351 "Invalid SEG_MAX %d\n", sc->sc_seg_max);
352 goto err;
353 }
354 } else {
355 sc->sc_seg_max = 1;
356 aprint_verbose_dev(sc->sc_dev,
357 "Unknown SEG_MAX, assuming %"PRIu32"\n", sc->sc_seg_max);
358 }
359
360 /* At least genfs_io assumes size_max*seg_max >= MAXPHYS. */
361 if (features & VIRTIO_BLK_F_SIZE_MAX) {
362 sc->sc_size_max = virtio_read_device_config_4(vsc,
363 VIRTIO_BLK_CONFIG_SIZE_MAX);
364 if (sc->sc_size_max < MAXPHYS/sc->sc_seg_max) {
365 aprint_error_dev(sc->sc_dev,
366 "Too small SIZE_MAX %d minimum is %d\n",
367 sc->sc_size_max, MAXPHYS/sc->sc_seg_max);
368 // goto err;
369 sc->sc_size_max = MAXPHYS/sc->sc_seg_max;
370 } else if (sc->sc_size_max > MAXPHYS) {
371 aprint_verbose_dev(sc->sc_dev,
372 "Clip SIZE_MAX from %d to %d\n",
373 sc->sc_size_max, MAXPHYS);
374 sc->sc_size_max = MAXPHYS;
375 }
376 } else {
377 sc->sc_size_max = MAXPHYS;
378 aprint_verbose_dev(sc->sc_dev,
379 "Unknown SIZE_MAX, assuming %"PRIu32"\n",
380 sc->sc_size_max);
381 }
382
383 aprint_normal_dev(sc->sc_dev, "max %"PRIu32" segs"
384 " of max %"PRIu32" bytes\n",
385 sc->sc_seg_max, sc->sc_size_max);
386
387 virtio_init_vq_vqdone(vsc, &sc->sc_vq, 0,
388 ld_virtio_vq_done);
389
390 if (virtio_alloc_vq(vsc, &sc->sc_vq, sc->sc_size_max,
391 sc->sc_seg_max + VIRTIO_BLK_CTRL_SEGMENTS, "I/O request") != 0)
392 goto err;
393 qsize = sc->sc_vq.vq_num;
394
395 if (virtio_child_attach_finish(vsc, &sc->sc_vq, 1,
396 NULL, VIRTIO_F_INTR_MSIX) != 0)
397 goto err;
398
399 ld->sc_dv = self;
400 ld->sc_secperunit = virtio_read_device_config_8(vsc,
401 VIRTIO_BLK_CONFIG_CAPACITY) / (ld->sc_secsize / VIRTIO_BLK_BSIZE);
402
403 /*
404 * Clamp ld->sc_maxxfer to MAXPHYS before ld_virtio_alloc_reqs
405 * allocates DMA maps of at most ld->sc_maxxfer bytes.
406 * ldattach will also clamp to MAXPHYS, but not until after
407 * ld_virtio_alloc_reqs is done, so that doesn't help.
408 */
409 ld->sc_maxxfer = MIN(MAXPHYS, sc->sc_size_max * sc->sc_seg_max);
410
411 if (features & VIRTIO_BLK_F_GEOMETRY) {
412 ld->sc_ncylinders = virtio_read_device_config_2(vsc,
413 VIRTIO_BLK_CONFIG_GEOMETRY_C);
414 ld->sc_nheads = virtio_read_device_config_1(vsc,
415 VIRTIO_BLK_CONFIG_GEOMETRY_H);
416 ld->sc_nsectors = virtio_read_device_config_1(vsc,
417 VIRTIO_BLK_CONFIG_GEOMETRY_S);
418 }
419 ld->sc_maxqueuecnt = qsize - 1; /* reserve slot for dumps, flushes */
420
421 if (ld_virtio_alloc_reqs(sc, qsize) < 0)
422 goto err;
423
424 cv_init(&sc->sc_sync_wait, "vblksync");
425 mutex_init(&sc->sc_sync_wait_lock, MUTEX_DEFAULT, IPL_BIO);
426 sc->sc_sync_use = SYNC_FREE;
427
428 ld->sc_dump = ld_virtio_dump;
429 ld->sc_start = ld_virtio_start;
430 ld->sc_ioctl = ld_virtio_ioctl;
431
432 if (ld_virtio_info(ld, true) == 0)
433 ld->sc_typename = sc->sc_typename;
434 else
435 ld->sc_typename = __UNCONST("Virtio Block Device");
436
437 if (features & VIRTIO_BLK_F_DISCARD) {
438 ld->sc_discard = ld_virtio_discard;
439 sc->sc_max_discard_sectors = virtio_read_device_config_4(vsc,
440 VIRTIO_BLK_CONFIG_MAX_DISCARD_SECTORS);
441 sc->sc_max_discard_seg = virtio_read_device_config_4(vsc,
442 VIRTIO_BLK_CONFIG_MAX_DISCARD_SEG);
443 #if 0
444 sc->sc_discard_sector_alignment =
445 virtio_read_device_config_4(vsc,
446 VIRTIO_BLK_CONFIG_DISCARD_SECTOR_ALIGNMENT);
447 #endif
448 }
449
450 ld->sc_flags = LDF_ENABLED | LDF_MPSAFE;
451 ldattach(ld, BUFQ_DISK_DEFAULT_STRAT);
452
453 return;
454
455 err:
456 virtio_child_attach_failed(vsc);
457 return;
458 }
459
460 static int __used
461 ld_virtio_info(struct ld_softc *ld, bool poll)
462 {
463 struct ld_virtio_softc *sc = device_private(ld->sc_dv);
464 struct virtio_softc *vsc = sc->sc_virtio;
465 struct virtqueue *vq = &sc->sc_vq;
466 struct virtio_blk_req *vr;
467 int r;
468 int slot;
469 uint8_t *id_data; /* virtio v1.2 5.2.6 */
470 size_t id_len = 20;
471 bool unload = false;
472
473 if (sc->sc_typename != NULL) {
474 kmem_strfree(sc->sc_typename);
475 sc->sc_typename = NULL;
476 }
477
478 id_data = kmem_alloc(id_len, KM_SLEEP);
479
480 mutex_enter(&sc->sc_sync_wait_lock);
481 while (sc->sc_sync_use != SYNC_FREE) {
482 if (poll) {
483 mutex_exit(&sc->sc_sync_wait_lock);
484 ld_virtio_vq_done(vq);
485 mutex_enter(&sc->sc_sync_wait_lock);
486 continue;
487 }
488 cv_wait(&sc->sc_sync_wait, &sc->sc_sync_wait_lock);
489 }
490 sc->sc_sync_use = SYNC_BUSY;
491 mutex_exit(&sc->sc_sync_wait_lock);
492
493 r = virtio_enqueue_prep(vsc, vq, &slot);
494 if (r != 0)
495 goto done;
496
497 vr = &sc->sc_reqs[slot];
498 KASSERT(vr->vr_bp == NULL);
499
500 r = bus_dmamap_load(virtio_dmat(vsc), vr->vr_payload,
501 id_data, id_len, NULL,
502 BUS_DMA_READ|BUS_DMA_NOWAIT);
503 if (r != 0) {
504 aprint_error_dev(sc->sc_dev,
505 "payload dmamap failed, error code %d\n", r);
506 virtio_enqueue_abort(vsc, vq, slot);
507 goto done;
508 }
509 unload = true;
510
511 KASSERT(vr->vr_payload->dm_nsegs <= sc->sc_seg_max);
512 r = virtio_enqueue_reserve(vsc, vq, slot, vr->vr_payload->dm_nsegs +
513 VIRTIO_BLK_CTRL_SEGMENTS);
514 if (r != 0) {
515 bus_dmamap_unload(virtio_dmat(vsc), vr->vr_payload);
516 goto done;
517 }
518
519 vr->vr_bp = DUMMY_VR_BP;
520 vr->vr_hdr.type = virtio_rw32(vsc, VIRTIO_BLK_T_GET_ID);
521 vr->vr_hdr.ioprio = virtio_rw32(vsc, 0);
522 vr->vr_hdr.sector = virtio_rw64(vsc, 0);
523
524 bus_dmamap_sync(virtio_dmat(vsc), vr->vr_cmdsts,
525 0, sizeof(struct virtio_blk_req_hdr),
526 BUS_DMASYNC_PREWRITE);
527 bus_dmamap_sync(virtio_dmat(vsc), vr->vr_payload,
528 0, id_len,
529 BUS_DMASYNC_PREREAD);
530 bus_dmamap_sync(virtio_dmat(vsc), vr->vr_cmdsts,
531 offsetof(struct virtio_blk_req, vr_status),
532 sizeof(uint8_t),
533 BUS_DMASYNC_PREREAD);
534
535 virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts,
536 0, sizeof(struct virtio_blk_req_hdr),
537 true);
538 virtio_enqueue(vsc, vq, slot, vr->vr_payload, false);
539 virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts,
540 offsetof(struct virtio_blk_req, vr_status),
541 sizeof(uint8_t),
542 false);
543 virtio_enqueue_commit(vsc, vq, slot, true);
544
545 done:
546 mutex_enter(&sc->sc_sync_wait_lock);
547 while (sc->sc_sync_use != SYNC_DONE) {
548 if (poll) {
549 mutex_exit(&sc->sc_sync_wait_lock);
550 ld_virtio_vq_done(vq);
551 mutex_enter(&sc->sc_sync_wait_lock);
552 continue;
553 }
554 cv_wait(&sc->sc_sync_wait, &sc->sc_sync_wait_lock);
555 }
556
557 if (sc->sc_sync_status == VIRTIO_BLK_S_OK)
558 r = 0;
559 else
560 r = EIO;
561
562 sc->sc_sync_use = SYNC_FREE;
563 cv_broadcast(&sc->sc_sync_wait);
564 mutex_exit(&sc->sc_sync_wait_lock);
565
566 if (unload) {
567 bus_dmamap_sync(virtio_dmat(vsc), vr->vr_payload,
568 0, id_len, BUS_DMASYNC_POSTREAD);
569 bus_dmamap_unload(virtio_dmat(vsc), vr->vr_payload);
570 }
571
572 if (r == 0)
573 sc->sc_typename = kmem_strndup(id_data, sizeof(id_data), KM_NOSLEEP);
574
575 kmem_free(id_data, id_len);
576
577 return r;
578 }
579
580 static int
581 ld_virtio_start(struct ld_softc *ld, struct buf *bp)
582 {
583 /* splbio */
584 struct ld_virtio_softc *sc = device_private(ld->sc_dv);
585 struct virtio_softc *vsc = sc->sc_virtio;
586 struct virtqueue *vq = &sc->sc_vq;
587 struct virtio_blk_req *vr;
588 int r;
589 int isread = (bp->b_flags & B_READ);
590 int slot;
591
592 if (sc->sc_readonly && !isread)
593 return EIO;
594
595 r = virtio_enqueue_prep(vsc, vq, &slot);
596 if (r != 0)
597 return r;
598
599 vr = &sc->sc_reqs[slot];
600 KASSERT(vr->vr_bp == NULL);
601
602 r = bus_dmamap_load(virtio_dmat(vsc), vr->vr_payload,
603 bp->b_data, bp->b_bcount, NULL,
604 ((isread?BUS_DMA_READ:BUS_DMA_WRITE)
605 |BUS_DMA_NOWAIT));
606 if (r != 0) {
607 aprint_error_dev(sc->sc_dev,
608 "payload dmamap failed, error code %d\n", r);
609 virtio_enqueue_abort(vsc, vq, slot);
610 return r;
611 }
612
613 KASSERT(vr->vr_payload->dm_nsegs <= sc->sc_seg_max);
614 r = virtio_enqueue_reserve(vsc, vq, slot, vr->vr_payload->dm_nsegs +
615 VIRTIO_BLK_CTRL_SEGMENTS);
616 if (r != 0) {
617 bus_dmamap_unload(virtio_dmat(vsc), vr->vr_payload);
618 return r;
619 }
620
621 vr->vr_bp = bp;
622 vr->vr_hdr.type = virtio_rw32(vsc,
623 isread ? VIRTIO_BLK_T_IN : VIRTIO_BLK_T_OUT);
624 vr->vr_hdr.ioprio = virtio_rw32(vsc, 0);
625 vr->vr_hdr.sector = virtio_rw64(vsc,
626 bp->b_rawblkno * sc->sc_ld.sc_secsize /
627 VIRTIO_BLK_BSIZE);
628
629 bus_dmamap_sync(virtio_dmat(vsc), vr->vr_cmdsts,
630 0, sizeof(struct virtio_blk_req_hdr),
631 BUS_DMASYNC_PREWRITE);
632 bus_dmamap_sync(virtio_dmat(vsc), vr->vr_payload,
633 0, bp->b_bcount,
634 isread?BUS_DMASYNC_PREREAD:BUS_DMASYNC_PREWRITE);
635 bus_dmamap_sync(virtio_dmat(vsc), vr->vr_cmdsts,
636 offsetof(struct virtio_blk_req, vr_status),
637 sizeof(uint8_t),
638 BUS_DMASYNC_PREREAD);
639
640 virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts,
641 0, sizeof(struct virtio_blk_req_hdr),
642 true);
643 virtio_enqueue(vsc, vq, slot, vr->vr_payload, !isread);
644 virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts,
645 offsetof(struct virtio_blk_req, vr_status),
646 sizeof(uint8_t),
647 false);
648 virtio_enqueue_commit(vsc, vq, slot, true);
649
650 return 0;
651 }
652
653 static void
654 ld_virtio_vq_done1(struct ld_virtio_softc *sc, struct virtio_softc *vsc,
655 struct virtqueue *vq, int slot)
656 {
657 struct virtio_blk_req *vr = &sc->sc_reqs[slot];
658 struct buf *bp = vr->vr_bp;
659 const uint32_t rt = virtio_rw32(vsc, vr->vr_hdr.type);
660
661 vr->vr_bp = NULL;
662
663 bus_dmamap_sync(virtio_dmat(vsc), vr->vr_cmdsts,
664 0, sizeof(struct virtio_blk_req_hdr),
665 BUS_DMASYNC_POSTWRITE);
666 bus_dmamap_sync(virtio_dmat(vsc), vr->vr_cmdsts,
667 sizeof(struct virtio_blk_req_hdr), sizeof(uint8_t),
668 BUS_DMASYNC_POSTREAD);
669 if (bp == DUMMY_VR_BP) {
670 mutex_enter(&sc->sc_sync_wait_lock);
671 sc->sc_sync_status = vr->vr_status;
672 sc->sc_sync_use = SYNC_DONE;
673 cv_broadcast(&sc->sc_sync_wait);
674 mutex_exit(&sc->sc_sync_wait_lock);
675 virtio_dequeue_commit(vsc, vq, slot);
676 return;
677 }
678 switch (rt) {
679 case VIRTIO_BLK_T_OUT:
680 case VIRTIO_BLK_T_IN:
681 bus_dmamap_sync(virtio_dmat(vsc), vr->vr_payload,
682 0, bp->b_bcount,
683 (bp->b_flags & B_READ)?BUS_DMASYNC_POSTREAD
684 :BUS_DMASYNC_POSTWRITE);
685 break;
686 default:
687 if (vr->vr_datap == NULL)
688 break;
689 bus_dmamap_sync(virtio_dmat(vsc), vr->vr_payload,
690 0, vr->vr_datas, BUS_DMASYNC_POSTREAD |
691 BUS_DMASYNC_POSTWRITE);
692 break;
693 }
694 bus_dmamap_unload(virtio_dmat(vsc), vr->vr_payload);
695
696 if (vr->vr_status != VIRTIO_BLK_S_OK) {
697 bp->b_error = EIO;
698 bp->b_resid = bp->b_bcount;
699 } else {
700 bp->b_error = 0;
701 bp->b_resid = 0;
702 }
703
704 if (vr->vr_datap != NULL) {
705 kmem_free(vr->vr_datap, vr->vr_datas);
706 vr->vr_datap = NULL;
707 vr->vr_datas = 0;
708 }
709
710 virtio_dequeue_commit(vsc, vq, slot);
711
712 switch (rt) {
713 case VIRTIO_BLK_T_OUT:
714 case VIRTIO_BLK_T_IN:
715 lddone(&sc->sc_ld, bp);
716 break;
717 case VIRTIO_BLK_T_DISCARD:
718 lddiscardend(&sc->sc_ld, bp);
719 break;
720 }
721 }
722
723 static int
724 ld_virtio_vq_done(struct virtqueue *vq)
725 {
726 struct virtio_softc *vsc = vq->vq_owner;
727 struct ld_virtio_softc *sc = device_private(virtio_child(vsc));
728 int r = 0;
729 int slot;
730
731 again:
732 if (virtio_dequeue(vsc, vq, &slot, NULL))
733 return r;
734 r = 1;
735
736 ld_virtio_vq_done1(sc, vsc, vq, slot);
737 goto again;
738 }
739
740 static int
741 ld_virtio_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
742 {
743 struct ld_virtio_softc *sc = device_private(ld->sc_dv);
744 struct virtio_softc *vsc = sc->sc_virtio;
745 struct virtqueue *vq = &sc->sc_vq;
746 struct virtio_blk_req *vr;
747 int slot, r;
748
749 if (sc->sc_readonly)
750 return EIO;
751
752 r = virtio_enqueue_prep(vsc, vq, &slot);
753 if (r != 0) {
754 if (r == EAGAIN) { /* no free slot; dequeue first */
755 delay(100);
756 ld_virtio_vq_done(vq);
757 r = virtio_enqueue_prep(vsc, vq, &slot);
758 if (r != 0)
759 return r;
760 }
761 return r;
762 }
763 vr = &sc->sc_reqs[slot];
764 r = bus_dmamap_load(virtio_dmat(vsc), vr->vr_payload,
765 data, blkcnt*ld->sc_secsize, NULL,
766 BUS_DMA_WRITE|BUS_DMA_NOWAIT);
767 if (r != 0)
768 return r;
769
770 r = virtio_enqueue_reserve(vsc, vq, slot, vr->vr_payload->dm_nsegs +
771 VIRTIO_BLK_CTRL_SEGMENTS);
772 if (r != 0) {
773 bus_dmamap_unload(virtio_dmat(vsc), vr->vr_payload);
774 return r;
775 }
776
777 vr->vr_bp = (void*)0xdeadbeef;
778 vr->vr_hdr.type = virtio_rw32(vsc, VIRTIO_BLK_T_OUT);
779 vr->vr_hdr.ioprio = virtio_rw32(vsc, 0);
780 vr->vr_hdr.sector = virtio_rw64(vsc,
781 (daddr_t) blkno * ld->sc_secsize /
782 VIRTIO_BLK_BSIZE);
783
784 bus_dmamap_sync(virtio_dmat(vsc), vr->vr_cmdsts,
785 0, sizeof(struct virtio_blk_req_hdr),
786 BUS_DMASYNC_PREWRITE);
787 bus_dmamap_sync(virtio_dmat(vsc), vr->vr_payload,
788 0, blkcnt*ld->sc_secsize,
789 BUS_DMASYNC_PREWRITE);
790 bus_dmamap_sync(virtio_dmat(vsc), vr->vr_cmdsts,
791 offsetof(struct virtio_blk_req, vr_status),
792 sizeof(uint8_t),
793 BUS_DMASYNC_PREREAD);
794
795 virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts,
796 0, sizeof(struct virtio_blk_req_hdr),
797 true);
798 virtio_enqueue(vsc, vq, slot, vr->vr_payload, true);
799 virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts,
800 offsetof(struct virtio_blk_req, vr_status),
801 sizeof(uint8_t),
802 false);
803 virtio_enqueue_commit(vsc, vq, slot, true);
804
805 for ( ; ; ) {
806 int dslot;
807
808 r = virtio_dequeue(vsc, vq, &dslot, NULL);
809 if (r != 0)
810 continue;
811 if (dslot != slot) {
812 ld_virtio_vq_done1(sc, vsc, vq, dslot);
813 continue;
814 } else
815 break;
816 }
817
818 bus_dmamap_sync(virtio_dmat(vsc), vr->vr_cmdsts,
819 0, sizeof(struct virtio_blk_req_hdr),
820 BUS_DMASYNC_POSTWRITE);
821 bus_dmamap_sync(virtio_dmat(vsc), vr->vr_payload,
822 0, blkcnt*ld->sc_secsize,
823 BUS_DMASYNC_POSTWRITE);
824 bus_dmamap_sync(virtio_dmat(vsc), vr->vr_cmdsts,
825 offsetof(struct virtio_blk_req, vr_status),
826 sizeof(uint8_t),
827 BUS_DMASYNC_POSTREAD);
828 if (vr->vr_status == VIRTIO_BLK_S_OK)
829 r = 0;
830 else
831 r = EIO;
832 virtio_dequeue_commit(vsc, vq, slot);
833
834 return r;
835 }
836
837 static int
838 ld_virtio_detach(device_t self, int flags)
839 {
840 struct ld_virtio_softc *sc = device_private(self);
841 struct ld_softc *ld = &sc->sc_ld;
842 bus_dma_tag_t dmat = virtio_dmat(sc->sc_virtio);
843 int r, i, qsize;
844
845 qsize = sc->sc_vq.vq_num;
846 r = ldbegindetach(ld, flags);
847 if (r != 0)
848 return r;
849 virtio_reset(sc->sc_virtio);
850 virtio_free_vq(sc->sc_virtio, &sc->sc_vq);
851
852 for (i = 0; i < qsize; i++) {
853 bus_dmamap_destroy(dmat,
854 sc->sc_reqs[i].vr_cmdsts);
855 bus_dmamap_destroy(dmat,
856 sc->sc_reqs[i].vr_payload);
857 }
858 bus_dmamem_unmap(dmat, sc->sc_reqs,
859 sizeof(struct virtio_blk_req) * qsize);
860 bus_dmamem_free(dmat, &sc->sc_reqs_seg, 1);
861
862 ldenddetach(ld);
863
864 if (sc->sc_typename != NULL)
865 kmem_strfree(sc->sc_typename);
866
867 cv_destroy(&sc->sc_sync_wait);
868 mutex_destroy(&sc->sc_sync_wait_lock);
869
870 virtio_child_detach(sc->sc_virtio);
871
872 return 0;
873 }
874
875 static int
876 ld_virtio_flush(struct ld_softc *ld, bool poll)
877 {
878 struct ld_virtio_softc * const sc = device_private(ld->sc_dv);
879 struct virtio_softc * const vsc = sc->sc_virtio;
880 const uint64_t features = virtio_features(vsc);
881 struct virtqueue *vq = &sc->sc_vq;
882 struct virtio_blk_req *vr;
883 int slot;
884 int r;
885
886 if ((features & VIRTIO_BLK_F_FLUSH) == 0)
887 return 0;
888
889 mutex_enter(&sc->sc_sync_wait_lock);
890 while (sc->sc_sync_use != SYNC_FREE) {
891 if (poll) {
892 mutex_exit(&sc->sc_sync_wait_lock);
893 ld_virtio_vq_done(vq);
894 mutex_enter(&sc->sc_sync_wait_lock);
895 continue;
896 }
897 cv_wait(&sc->sc_sync_wait, &sc->sc_sync_wait_lock);
898 }
899 sc->sc_sync_use = SYNC_BUSY;
900 mutex_exit(&sc->sc_sync_wait_lock);
901
902 r = virtio_enqueue_prep(vsc, vq, &slot);
903 if (r != 0) {
904 return r;
905 }
906
907 vr = &sc->sc_reqs[slot];
908 KASSERT(vr->vr_bp == NULL);
909
910 r = virtio_enqueue_reserve(vsc, vq, slot, VIRTIO_BLK_CTRL_SEGMENTS);
911 if (r != 0) {
912 return r;
913 }
914
915 vr->vr_bp = DUMMY_VR_BP;
916 vr->vr_hdr.type = virtio_rw32(vsc, VIRTIO_BLK_T_FLUSH);
917 vr->vr_hdr.ioprio = virtio_rw32(vsc, 0);
918 vr->vr_hdr.sector = virtio_rw64(vsc, 0);
919
920 bus_dmamap_sync(virtio_dmat(vsc), vr->vr_cmdsts,
921 0, sizeof(struct virtio_blk_req_hdr),
922 BUS_DMASYNC_PREWRITE);
923 bus_dmamap_sync(virtio_dmat(vsc), vr->vr_cmdsts,
924 offsetof(struct virtio_blk_req, vr_status),
925 sizeof(uint8_t),
926 BUS_DMASYNC_PREREAD);
927
928 virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts,
929 0, sizeof(struct virtio_blk_req_hdr),
930 true);
931 virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts,
932 offsetof(struct virtio_blk_req, vr_status),
933 sizeof(uint8_t),
934 false);
935 virtio_enqueue_commit(vsc, vq, slot, true);
936
937 mutex_enter(&sc->sc_sync_wait_lock);
938 while (sc->sc_sync_use != SYNC_DONE) {
939 if (poll) {
940 mutex_exit(&sc->sc_sync_wait_lock);
941 ld_virtio_vq_done(vq);
942 mutex_enter(&sc->sc_sync_wait_lock);
943 continue;
944 }
945 cv_wait(&sc->sc_sync_wait, &sc->sc_sync_wait_lock);
946 }
947
948 if (sc->sc_sync_status == VIRTIO_BLK_S_OK)
949 r = 0;
950 else
951 r = EIO;
952
953 sc->sc_sync_use = SYNC_FREE;
954 cv_broadcast(&sc->sc_sync_wait);
955 mutex_exit(&sc->sc_sync_wait_lock);
956
957 return r;
958 }
959
960 static int
961 ld_virtio_getcache(struct ld_softc *ld, int *bitsp)
962 {
963 struct ld_virtio_softc * const sc = device_private(ld->sc_dv);
964 struct virtio_softc * const vsc = sc->sc_virtio;
965 const uint64_t features = virtio_features(vsc);
966
967 *bitsp = DKCACHE_READ;
968 if ((features & VIRTIO_BLK_F_CONFIG_WCE) != 0)
969 *bitsp |= DKCACHE_WCHANGE;
970 if (virtio_read_device_config_1(vsc,
971 VIRTIO_BLK_CONFIG_WRITEBACK) != 0x00)
972 *bitsp |= DKCACHE_WRITE;
973
974 return 0;
975 }
976
977 static int
978 ld_virtio_setcache(struct ld_softc *ld, int bits)
979 {
980 struct ld_virtio_softc * const sc = device_private(ld->sc_dv);
981 struct virtio_softc * const vsc = sc->sc_virtio;
982 const uint8_t wce = (bits & DKCACHE_WRITE) ? 0x01 : 0x00;
983
984 virtio_write_device_config_1(vsc,
985 VIRTIO_BLK_CONFIG_WRITEBACK, wce);
986 if (virtio_read_device_config_1(vsc,
987 VIRTIO_BLK_CONFIG_WRITEBACK) != wce)
988 return EIO;
989
990 return 0;
991 }
992
993 static int
994 ld_virtio_ioctl(struct ld_softc *ld, u_long cmd, void *addr, int32_t flag, bool poll)
995 {
996 int error;
997
998 switch (cmd) {
999 case DIOCCACHESYNC:
1000 error = ld_virtio_flush(ld, poll);
1001 break;
1002
1003 case DIOCGCACHE:
1004 error = ld_virtio_getcache(ld, (int *)addr);
1005 break;
1006
1007 case DIOCSCACHE:
1008 error = ld_virtio_setcache(ld, *(int *)addr);
1009 break;
1010
1011 default:
1012 error = EPASSTHROUGH;
1013 break;
1014 }
1015
1016 return error;
1017 }
1018
1019 static int
1020 ld_virtio_discard(struct ld_softc *ld, struct buf *bp)
1021 {
1022 struct ld_virtio_softc * const sc = device_private(ld->sc_dv);
1023 struct virtio_softc * const vsc = sc->sc_virtio;
1024 struct virtqueue * const vq = &sc->sc_vq;
1025 struct virtio_blk_req *vr;
1026 const uint64_t features = virtio_features(vsc);
1027 int r;
1028 int slot;
1029 uint64_t blkno;
1030 uint32_t nblks;
1031 struct virtio_blk_discard_write_zeroes * dwz;
1032
1033 if ((features & VIRTIO_BLK_F_DISCARD) == 0 ||
1034 sc->sc_max_discard_seg < 1)
1035 return EINVAL;
1036
1037 if (sc->sc_readonly)
1038 return EIO;
1039
1040 blkno = bp->b_rawblkno * sc->sc_ld.sc_secsize / VIRTIO_BLK_BSIZE;
1041 nblks = bp->b_bcount / VIRTIO_BLK_BSIZE;
1042
1043 if (nblks > sc->sc_max_discard_sectors)
1044 return ERANGE;
1045
1046 r = virtio_enqueue_prep(vsc, vq, &slot);
1047 if (r != 0) {
1048 return r;
1049 }
1050
1051 vr = &sc->sc_reqs[slot];
1052 KASSERT(vr->vr_bp == NULL);
1053
1054 dwz = kmem_alloc(sizeof(*dwz), KM_SLEEP);
1055
1056 r = bus_dmamap_load(virtio_dmat(vsc), vr->vr_payload,
1057 dwz, sizeof(*dwz), NULL, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1058 if (r != 0) {
1059 device_printf(sc->sc_dev,
1060 "discard payload dmamap failed, error code %d\n", r);
1061 virtio_enqueue_abort(vsc, vq, slot);
1062 kmem_free(dwz, sizeof(*dwz));
1063 return r;
1064 }
1065
1066 KASSERT(vr->vr_payload->dm_nsegs <= sc->sc_seg_max);
1067 r = virtio_enqueue_reserve(vsc, vq, slot, vr->vr_payload->dm_nsegs +
1068 VIRTIO_BLK_CTRL_SEGMENTS);
1069 if (r != 0) {
1070 bus_dmamap_unload(virtio_dmat(vsc), vr->vr_payload);
1071 kmem_free(dwz, sizeof(*dwz));
1072 return r;
1073 }
1074
1075 vr->vr_hdr.type = virtio_rw32(vsc, VIRTIO_BLK_T_DISCARD);
1076 vr->vr_hdr.ioprio = virtio_rw32(vsc, 0);
1077 vr->vr_hdr.sector = virtio_rw64(vsc, 0);
1078 vr->vr_bp = bp;
1079
1080 KASSERT(vr->vr_datap == NULL);
1081 vr->vr_datap = dwz;
1082 vr->vr_datas = sizeof(*dwz);
1083
1084 dwz->sector = virtio_rw64(vsc, blkno);
1085 dwz->num_sectors = virtio_rw32(vsc, nblks);
1086 dwz->flags = virtio_rw32(vsc, 0);
1087
1088 bus_dmamap_sync(virtio_dmat(vsc), vr->vr_cmdsts,
1089 0, sizeof(struct virtio_blk_req_hdr),
1090 BUS_DMASYNC_PREWRITE);
1091 bus_dmamap_sync(virtio_dmat(vsc), vr->vr_payload,
1092 0, vr->vr_datas, BUS_DMASYNC_PREWRITE);
1093 bus_dmamap_sync(virtio_dmat(vsc), vr->vr_cmdsts,
1094 offsetof(struct virtio_blk_req, vr_status),
1095 sizeof(uint8_t),
1096 BUS_DMASYNC_PREREAD);
1097
1098 virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts,
1099 0, sizeof(struct virtio_blk_req_hdr),
1100 true);
1101 virtio_enqueue(vsc, vq, slot, vr->vr_payload, true);
1102 virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts,
1103 offsetof(struct virtio_blk_req, vr_status),
1104 sizeof(uint8_t),
1105 false);
1106 virtio_enqueue_commit(vsc, vq, slot, true);
1107
1108 return 0;
1109 }
1110
1111 MODULE(MODULE_CLASS_DRIVER, ld_virtio, "ld,virtio");
1112
1113 static int
1114 ld_virtio_modcmd(modcmd_t cmd, void *opaque)
1115 {
1116 int error = 0;
1117
1118 switch (cmd) {
1119 case MODULE_CMD_INIT:
1120 #ifdef _MODULE
1121 error = config_init_component(cfdriver_ioconf_ld_virtio,
1122 cfattach_ioconf_ld_virtio, cfdata_ioconf_ld_virtio);
1123 #endif
1124 break;
1125 case MODULE_CMD_FINI:
1126 #ifdef _MODULE
1127 error = config_fini_component(cfdriver_ioconf_ld_virtio,
1128 cfattach_ioconf_ld_virtio, cfdata_ioconf_ld_virtio);
1129 #endif
1130 break;
1131 default:
1132 error = ENOTTY;
1133 break;
1134 }
1135
1136 return error;
1137 }
1138