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