1 /* $NetBSD: ld_virtio.c,v 1.4 2011/12/03 10:53:09 hannken 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.4 2011/12/03 10:53:09 hannken Exp $"); 30 31 #include "rnd.h" 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/kernel.h> 36 #include <sys/buf.h> 37 #include <sys/bus.h> 38 #include <sys/device.h> 39 #include <sys/disk.h> 40 #include <sys/mutex.h> 41 #if NRND > 0 42 #include <sys/rnd.h> 43 #endif 44 45 #include <dev/pci/pcidevs.h> 46 #include <dev/pci/pcireg.h> 47 #include <dev/pci/pcivar.h> 48 49 #include <dev/ldvar.h> 50 #include <dev/pci/virtioreg.h> 51 #include <dev/pci/virtiovar.h> 52 53 /* 54 * ld_virtioreg: 55 */ 56 /* Configuration registers */ 57 #define VIRTIO_BLK_CONFIG_CAPACITY 0 /* 64bit */ 58 #define VIRTIO_BLK_CONFIG_SIZE_MAX 8 /* 32bit */ 59 #define VIRTIO_BLK_CONFIG_SEG_MAX 12 /* 32bit */ 60 #define VIRTIO_BLK_CONFIG_GEOMETRY_C 16 /* 16bit */ 61 #define VIRTIO_BLK_CONFIG_GEOMETRY_H 18 /* 8bit */ 62 #define VIRTIO_BLK_CONFIG_GEOMETRY_S 19 /* 8bit */ 63 #define VIRTIO_BLK_CONFIG_BLK_SIZE 20 /* 32bit */ 64 65 /* Feature bits */ 66 #define VIRTIO_BLK_F_BARRIER (1<<0) 67 #define VIRTIO_BLK_F_SIZE_MAX (1<<1) 68 #define VIRTIO_BLK_F_SEG_MAX (1<<2) 69 #define VIRTIO_BLK_F_GEOMETRY (1<<4) 70 #define VIRTIO_BLK_F_RO (1<<5) 71 #define VIRTIO_BLK_F_BLK_SIZE (1<<6) 72 #define VIRTIO_BLK_F_SCSI (1<<7) 73 #define VIRTIO_BLK_F_FLUSH (1<<9) 74 75 /* Command */ 76 #define VIRTIO_BLK_T_IN 0 77 #define VIRTIO_BLK_T_OUT 1 78 #define VIRTIO_BLK_T_BARRIER 0x80000000 79 80 /* Status */ 81 #define VIRTIO_BLK_S_OK 0 82 #define VIRTIO_BLK_S_IOERR 1 83 84 /* Request header structure */ 85 struct virtio_blk_req_hdr { 86 uint32_t type; /* VIRTIO_BLK_T_* */ 87 uint32_t ioprio; 88 uint64_t sector; 89 } __packed; 90 /* 512*virtio_blk_req_hdr.sector byte payload and 1 byte status follows */ 91 92 93 /* 94 * ld_virtiovar: 95 */ 96 struct virtio_blk_req { 97 struct virtio_blk_req_hdr vr_hdr; 98 uint8_t vr_status; 99 struct buf *vr_bp; 100 bus_dmamap_t vr_cmdsts; 101 bus_dmamap_t vr_payload; 102 }; 103 104 struct ld_virtio_softc { 105 struct ld_softc sc_ld; 106 device_t sc_dev; 107 108 struct virtio_softc *sc_virtio; 109 struct virtqueue sc_vq[1]; 110 111 struct virtio_blk_req *sc_reqs; 112 bus_dma_segment_t sc_reqs_segs[1]; 113 114 kmutex_t sc_lock; 115 116 int sc_readonly; 117 }; 118 119 static int ld_virtio_match(device_t, cfdata_t, void *); 120 static void ld_virtio_attach(device_t, device_t, void *); 121 static int ld_virtio_detach(device_t, int); 122 123 CFATTACH_DECL_NEW(ld_virtio, sizeof(struct ld_virtio_softc), 124 ld_virtio_match, ld_virtio_attach, ld_virtio_detach, NULL); 125 126 static int 127 ld_virtio_match(device_t parent, cfdata_t match, void *aux) 128 { 129 struct virtio_softc *va = aux; 130 131 if (va->sc_childdevid == PCI_PRODUCT_VIRTIO_BLOCK) 132 return 1; 133 134 return 0; 135 } 136 137 static int ld_virtio_vq_done(struct virtqueue *); 138 static int ld_virtio_dump(struct ld_softc *, void *, int, int); 139 static int ld_virtio_start(struct ld_softc *, struct buf *); 140 141 static int 142 ld_virtio_alloc_reqs(struct ld_virtio_softc *sc, int qsize) 143 { 144 int allocsize, r, rsegs, i; 145 struct ld_softc *ld = &sc->sc_ld; 146 void *vaddr; 147 148 allocsize = sizeof(struct virtio_blk_req) * qsize; 149 r = bus_dmamem_alloc(sc->sc_virtio->sc_dmat, allocsize, 0, 0, 150 &sc->sc_reqs_segs[0], 1, &rsegs, BUS_DMA_NOWAIT); 151 if (r != 0) { 152 aprint_error_dev(sc->sc_dev, 153 "DMA memory allocation failed, size %d, " 154 "error code %d\n", allocsize, r); 155 goto err_none; 156 } 157 r = bus_dmamem_map(sc->sc_virtio->sc_dmat, 158 &sc->sc_reqs_segs[0], 1, allocsize, 159 &vaddr, BUS_DMA_NOWAIT); 160 if (r != 0) { 161 aprint_error_dev(sc->sc_dev, 162 "DMA memory map failed, " 163 "error code %d\n", r); 164 goto err_dmamem_alloc; 165 } 166 sc->sc_reqs = vaddr; 167 memset(vaddr, 0, allocsize); 168 for (i = 0; i < qsize; i++) { 169 struct virtio_blk_req *vr = &sc->sc_reqs[i]; 170 r = bus_dmamap_create(sc->sc_virtio->sc_dmat, 171 offsetof(struct virtio_blk_req, vr_bp), 172 1, 173 offsetof(struct virtio_blk_req, vr_bp), 174 0, 175 BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, 176 &vr->vr_cmdsts); 177 if (r != 0) { 178 aprint_error_dev(sc->sc_dev, 179 "command dmamap creation failed, " 180 "error code %d\n", r); 181 goto err_reqs; 182 } 183 r = bus_dmamap_load(sc->sc_virtio->sc_dmat, vr->vr_cmdsts, 184 &vr->vr_hdr, 185 offsetof(struct virtio_blk_req, vr_bp), 186 NULL, BUS_DMA_NOWAIT); 187 if (r != 0) { 188 aprint_error_dev(sc->sc_dev, 189 "command dmamap load failed, " 190 "error code %d\n", r); 191 goto err_reqs; 192 } 193 r = bus_dmamap_create(sc->sc_virtio->sc_dmat, 194 ld->sc_maxxfer, 195 (ld->sc_maxxfer / NBPG) + 2, 196 ld->sc_maxxfer, 197 0, 198 BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, 199 &vr->vr_payload); 200 if (r != 0) { 201 aprint_error_dev(sc->sc_dev, 202 "payload dmamap creation failed, " 203 "error code %d\n", r); 204 goto err_reqs; 205 } 206 } 207 return 0; 208 209 err_reqs: 210 for (i = 0; i < qsize; i++) { 211 struct virtio_blk_req *vr = &sc->sc_reqs[i]; 212 if (vr->vr_cmdsts) { 213 bus_dmamap_destroy(sc->sc_virtio->sc_dmat, 214 vr->vr_cmdsts); 215 vr->vr_cmdsts = 0; 216 } 217 if (vr->vr_payload) { 218 bus_dmamap_destroy(sc->sc_virtio->sc_dmat, 219 vr->vr_payload); 220 vr->vr_payload = 0; 221 } 222 } 223 bus_dmamem_unmap(sc->sc_virtio->sc_dmat, sc->sc_reqs, allocsize); 224 err_dmamem_alloc: 225 bus_dmamem_free(sc->sc_virtio->sc_dmat, &sc->sc_reqs_segs[0], 1); 226 err_none: 227 return -1; 228 } 229 230 static void 231 ld_virtio_attach(device_t parent, device_t self, void *aux) 232 { 233 struct ld_virtio_softc *sc = device_private(self); 234 struct ld_softc *ld = &sc->sc_ld; 235 struct virtio_softc *vsc = device_private(parent); 236 uint32_t features; 237 int qsize, maxxfersize; 238 239 if (vsc->sc_child != NULL) { 240 aprint_normal(": child already attached for %s; " 241 "something wrong...\n", 242 device_xname(parent)); 243 return; 244 } 245 aprint_normal("\n"); 246 aprint_naive("\n"); 247 248 sc->sc_dev = self; 249 sc->sc_virtio = vsc; 250 251 vsc->sc_child = self; 252 vsc->sc_ipl = IPL_BIO; 253 vsc->sc_vqs = &sc->sc_vq[0]; 254 vsc->sc_nvqs = 1; 255 vsc->sc_config_change = 0; 256 vsc->sc_intrhand = virtio_vq_intr; 257 258 features = virtio_negotiate_features(vsc, 259 (VIRTIO_BLK_F_SIZE_MAX | 260 VIRTIO_BLK_F_SEG_MAX | 261 VIRTIO_BLK_F_GEOMETRY | 262 VIRTIO_BLK_F_RO | 263 VIRTIO_BLK_F_BLK_SIZE)); 264 if (features & VIRTIO_BLK_F_RO) 265 sc->sc_readonly = 1; 266 else 267 sc->sc_readonly = 0; 268 269 ld->sc_secsize = 512; 270 if (features & VIRTIO_BLK_F_BLK_SIZE) { 271 ld->sc_secsize = virtio_read_device_config_4(vsc, 272 VIRTIO_BLK_CONFIG_BLK_SIZE); 273 } 274 maxxfersize = MAXPHYS; 275 #if 0 /* At least genfs_io assumes maxxfer == MAXPHYS. */ 276 if (features & VIRTIO_BLK_F_SEG_MAX) { 277 maxxfersize = virtio_read_device_config_4(vsc, 278 VIRTIO_BLK_CONFIG_SEG_MAX) 279 * ld->sc_secsize; 280 if (maxxfersize > MAXPHYS) 281 maxxfersize = MAXPHYS; 282 } 283 #endif 284 285 if (virtio_alloc_vq(vsc, &sc->sc_vq[0], 0, 286 maxxfersize, maxxfersize / NBPG + 2, 287 "I/O request") != 0) { 288 goto err; 289 } 290 qsize = sc->sc_vq[0].vq_num; 291 sc->sc_vq[0].vq_done = ld_virtio_vq_done; 292 293 ld->sc_dv = self; 294 ld->sc_secperunit = virtio_read_device_config_8(vsc, 295 VIRTIO_BLK_CONFIG_CAPACITY); 296 ld->sc_maxxfer = maxxfersize; 297 if (features & VIRTIO_BLK_F_GEOMETRY) { 298 ld->sc_ncylinders = virtio_read_device_config_2(vsc, 299 VIRTIO_BLK_CONFIG_GEOMETRY_C); 300 ld->sc_nheads = virtio_read_device_config_1(vsc, 301 VIRTIO_BLK_CONFIG_GEOMETRY_H); 302 ld->sc_nsectors = virtio_read_device_config_1(vsc, 303 VIRTIO_BLK_CONFIG_GEOMETRY_S); 304 } 305 ld->sc_maxqueuecnt = qsize; 306 307 if (ld_virtio_alloc_reqs(sc, qsize) < 0) 308 goto err; 309 310 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_BIO); 311 312 ld->sc_dump = ld_virtio_dump; 313 ld->sc_flush = NULL; 314 ld->sc_start = ld_virtio_start; 315 316 ld->sc_flags = LDF_ENABLED; 317 ldattach(ld); 318 319 return; 320 321 err: 322 vsc->sc_child = (void*)1; 323 return; 324 } 325 326 static int 327 ld_virtio_start(struct ld_softc *ld, struct buf *bp) 328 { 329 /* splbio */ 330 struct ld_virtio_softc *sc = device_private(ld->sc_dv); 331 struct virtio_softc *vsc = sc->sc_virtio; 332 struct virtqueue *vq = &sc->sc_vq[0]; 333 struct virtio_blk_req *vr; 334 int r; 335 int isread = (bp->b_flags & B_READ); 336 int slot; 337 338 if (sc->sc_readonly && !isread) 339 return EIO; 340 341 r = virtio_enqueue_prep(vsc, vq, &slot); 342 if (r != 0) 343 return r; 344 vr = &sc->sc_reqs[slot]; 345 r = bus_dmamap_load(vsc->sc_dmat, vr->vr_payload, 346 bp->b_data, bp->b_bcount, NULL, 347 ((isread?BUS_DMA_READ:BUS_DMA_WRITE) 348 |BUS_DMA_NOWAIT)); 349 if (r != 0) 350 return r; 351 352 r = virtio_enqueue_reserve(vsc, vq, slot, vr->vr_payload->dm_nsegs + 2); 353 if (r != 0) { 354 bus_dmamap_unload(vsc->sc_dmat, vr->vr_payload); 355 return r; 356 } 357 358 vr->vr_bp = bp; 359 vr->vr_hdr.type = isread?VIRTIO_BLK_T_IN:VIRTIO_BLK_T_OUT; 360 vr->vr_hdr.ioprio = 0; 361 vr->vr_hdr.sector = bp->b_rawblkno * sc->sc_ld.sc_secsize / 512; 362 363 bus_dmamap_sync(vsc->sc_dmat, vr->vr_cmdsts, 364 0, sizeof(struct virtio_blk_req_hdr), 365 BUS_DMASYNC_PREWRITE); 366 bus_dmamap_sync(vsc->sc_dmat, vr->vr_payload, 367 0, bp->b_bcount, 368 isread?BUS_DMASYNC_PREREAD:BUS_DMASYNC_PREWRITE); 369 bus_dmamap_sync(vsc->sc_dmat, vr->vr_cmdsts, 370 offsetof(struct virtio_blk_req, vr_status), 371 sizeof(uint8_t), 372 BUS_DMASYNC_PREREAD); 373 374 virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts, 375 0, sizeof(struct virtio_blk_req_hdr), 376 true); 377 virtio_enqueue(vsc, vq, slot, vr->vr_payload, !isread); 378 virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts, 379 offsetof(struct virtio_blk_req, vr_status), 380 sizeof(uint8_t), 381 false); 382 virtio_enqueue_commit(vsc, vq, slot, true); 383 384 return 0; 385 } 386 387 static void 388 ld_virtio_vq_done1(struct ld_virtio_softc *sc, struct virtio_softc *vsc, 389 struct virtqueue *vq, int slot) 390 { 391 struct virtio_blk_req *vr = &sc->sc_reqs[slot]; 392 struct buf *bp = vr->vr_bp; 393 394 bus_dmamap_sync(vsc->sc_dmat, vr->vr_cmdsts, 395 0, sizeof(struct virtio_blk_req_hdr), 396 BUS_DMASYNC_POSTWRITE); 397 bus_dmamap_sync(vsc->sc_dmat, vr->vr_payload, 398 0, bp->b_bcount, 399 (bp->b_flags & B_READ)?BUS_DMASYNC_POSTREAD 400 :BUS_DMASYNC_POSTWRITE); 401 bus_dmamap_sync(vsc->sc_dmat, vr->vr_cmdsts, 402 sizeof(struct virtio_blk_req_hdr), sizeof(uint8_t), 403 BUS_DMASYNC_POSTREAD); 404 405 if (vr->vr_status != VIRTIO_BLK_S_OK) { 406 bp->b_error = EIO; 407 bp->b_resid = bp->b_bcount; 408 } else { 409 bp->b_error = 0; 410 bp->b_resid = 0; 411 } 412 413 virtio_dequeue_commit(vsc, vq, slot); 414 415 lddone(&sc->sc_ld, bp); 416 } 417 418 static int 419 ld_virtio_vq_done(struct virtqueue *vq) 420 { 421 struct virtio_softc *vsc = vq->vq_owner; 422 struct ld_virtio_softc *sc = device_private(vsc->sc_child); 423 int r = 0; 424 int slot; 425 426 again: 427 if (virtio_dequeue(vsc, vq, &slot, NULL)) 428 return r; 429 r = 1; 430 431 ld_virtio_vq_done1(sc, vsc, vq, slot); 432 goto again; 433 } 434 435 static int 436 ld_virtio_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt) 437 { 438 struct ld_virtio_softc *sc = device_private(ld->sc_dv); 439 struct virtio_softc *vsc = sc->sc_virtio; 440 struct virtqueue *vq = &sc->sc_vq[0]; 441 struct virtio_blk_req *vr; 442 int slot, r; 443 444 if (sc->sc_readonly) 445 return EIO; 446 447 r = virtio_enqueue_prep(vsc, vq, &slot); 448 if (r != 0) { 449 if (r == EAGAIN) { /* no free slot; dequeue first */ 450 delay(100); 451 ld_virtio_vq_done(vq); 452 r = virtio_enqueue_prep(vsc, vq, &slot); 453 if (r != 0) 454 return r; 455 } 456 return r; 457 } 458 vr = &sc->sc_reqs[slot]; 459 r = bus_dmamap_load(vsc->sc_dmat, vr->vr_payload, 460 data, blkcnt*ld->sc_secsize, NULL, 461 BUS_DMA_WRITE|BUS_DMA_NOWAIT); 462 if (r != 0) 463 return r; 464 465 r = virtio_enqueue_reserve(vsc, vq, slot, vr->vr_payload->dm_nsegs + 2); 466 if (r != 0) { 467 bus_dmamap_unload(vsc->sc_dmat, vr->vr_payload); 468 return r; 469 } 470 471 vr->vr_bp = (void*)0xdeadbeef; 472 vr->vr_hdr.type = VIRTIO_BLK_T_OUT; 473 vr->vr_hdr.ioprio = 0; 474 vr->vr_hdr.sector = (daddr_t) blkno * ld->sc_secsize / 512; 475 476 bus_dmamap_sync(vsc->sc_dmat, vr->vr_cmdsts, 477 0, sizeof(struct virtio_blk_req_hdr), 478 BUS_DMASYNC_PREWRITE); 479 bus_dmamap_sync(vsc->sc_dmat, vr->vr_payload, 480 0, blkcnt*ld->sc_secsize, 481 BUS_DMASYNC_PREWRITE); 482 bus_dmamap_sync(vsc->sc_dmat, vr->vr_cmdsts, 483 offsetof(struct virtio_blk_req, vr_status), 484 sizeof(uint8_t), 485 BUS_DMASYNC_PREREAD); 486 487 virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts, 488 0, sizeof(struct virtio_blk_req_hdr), 489 true); 490 virtio_enqueue(vsc, vq, slot, vr->vr_payload, true); 491 virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts, 492 offsetof(struct virtio_blk_req, vr_status), 493 sizeof(uint8_t), 494 false); 495 virtio_enqueue_commit(vsc, vq, slot, true); 496 497 for ( ; ; ) { 498 int dslot; 499 500 r = virtio_dequeue(vsc, vq, &dslot, NULL); 501 if (r != 0) 502 continue; 503 if (dslot != slot) { 504 ld_virtio_vq_done1(sc, vsc, vq, dslot); 505 continue; 506 } else 507 break; 508 } 509 510 bus_dmamap_sync(vsc->sc_dmat, vr->vr_cmdsts, 511 0, sizeof(struct virtio_blk_req_hdr), 512 BUS_DMASYNC_POSTWRITE); 513 bus_dmamap_sync(vsc->sc_dmat, vr->vr_payload, 514 0, blkcnt*ld->sc_secsize, 515 BUS_DMASYNC_POSTWRITE); 516 bus_dmamap_sync(vsc->sc_dmat, vr->vr_cmdsts, 517 offsetof(struct virtio_blk_req, vr_status), 518 sizeof(uint8_t), 519 BUS_DMASYNC_POSTREAD); 520 if (vr->vr_status == VIRTIO_BLK_S_OK) 521 r = 0; 522 else 523 r = EIO; 524 virtio_dequeue_commit(vsc, vq, slot); 525 526 return r; 527 } 528 529 static int 530 ld_virtio_detach(device_t self, int flags) 531 { 532 struct ld_virtio_softc *sc = device_private(self); 533 struct ld_softc *ld = &sc->sc_ld; 534 bus_dma_tag_t dmat = sc->sc_virtio->sc_dmat; 535 int r, i, qsize; 536 537 qsize = sc->sc_vq[0].vq_num; 538 r = ldbegindetach(ld, flags); 539 if (r != 0) 540 return r; 541 virtio_reset(sc->sc_virtio); 542 virtio_free_vq(sc->sc_virtio, &sc->sc_vq[0]); 543 544 for (i = 0; i < qsize; i++) { 545 bus_dmamap_destroy(dmat, 546 sc->sc_reqs[i].vr_cmdsts); 547 bus_dmamap_destroy(dmat, 548 sc->sc_reqs[i].vr_payload); 549 } 550 bus_dmamem_unmap(dmat, sc->sc_reqs, 551 sizeof(struct virtio_blk_req) * qsize); 552 bus_dmamem_free(dmat, &sc->sc_reqs_segs[0], 1); 553 554 ldenddetach(ld); 555 556 return 0; 557 } 558