1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2011 NetApp, Inc. 5 * All rights reserved. 6 * Copyright 2020 Joyent, Inc. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 /* 32 * This file and its contents are supplied under the terms of the 33 * Common Development and Distribution License ("CDDL"), version 1.0. 34 * You may only use this file in accordance with the terms of version 35 * 1.0 of the CDDL. 36 * 37 * A full copy of the text of the CDDL should have accompanied this 38 * source. A copy of the CDDL is also available via the Internet at 39 * http://www.illumos.org/license/CDDL. 40 * 41 * Copyright 2014 Pluribus Networks Inc. 42 */ 43 44 #include <sys/cdefs.h> 45 __FBSDID("$FreeBSD$"); 46 47 #include <sys/param.h> 48 #include <sys/linker_set.h> 49 #include <sys/stat.h> 50 #include <sys/uio.h> 51 #include <sys/ioctl.h> 52 #include <sys/disk.h> 53 54 #include <errno.h> 55 #include <fcntl.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <stdint.h> 59 #include <string.h> 60 #include <strings.h> 61 #include <unistd.h> 62 #include <assert.h> 63 #include <pthread.h> 64 #include <md5.h> 65 66 #include "bhyverun.h" 67 #include "pci_emul.h" 68 #include "virtio.h" 69 #include "block_if.h" 70 71 #define VTBLK_BSIZE 512 72 #define VTBLK_RINGSZ 128 73 74 _Static_assert(VTBLK_RINGSZ <= BLOCKIF_RING_MAX, "Each ring entry must be able to queue a request"); 75 76 #define VTBLK_S_OK 0 77 #define VTBLK_S_IOERR 1 78 #define VTBLK_S_UNSUPP 2 79 80 #define VTBLK_BLK_ID_BYTES 20 + 1 81 82 /* Capability bits */ 83 #define VTBLK_F_BARRIER (1 << 0) /* Does host support barriers? */ 84 #define VTBLK_F_SIZE_MAX (1 << 1) /* Indicates maximum segment size */ 85 #define VTBLK_F_SEG_MAX (1 << 2) /* Indicates maximum # of segments */ 86 #define VTBLK_F_GEOMETRY (1 << 4) /* Legacy geometry available */ 87 #define VTBLK_F_RO (1 << 5) /* Disk is read-only */ 88 #define VTBLK_F_BLK_SIZE (1 << 6) /* Block size of disk is available*/ 89 #define VTBLK_F_SCSI (1 << 7) /* Supports scsi command passthru */ 90 #define VTBLK_F_FLUSH (1 << 9) /* Writeback mode enabled after reset */ 91 #define VTBLK_F_WCE (1 << 9) /* Legacy alias for FLUSH */ 92 #define VTBLK_F_TOPOLOGY (1 << 10) /* Topology information is available */ 93 #define VTBLK_F_CONFIG_WCE (1 << 11) /* Writeback mode available in config */ 94 #define VTBLK_F_DISCARD (1 << 13) /* Trim blocks */ 95 #define VTBLK_F_WRITE_ZEROES (1 << 14) /* Write zeros */ 96 97 /* 98 * Host capabilities 99 */ 100 #define VTBLK_S_HOSTCAPS \ 101 ( VTBLK_F_SEG_MAX | \ 102 VTBLK_F_BLK_SIZE | \ 103 VTBLK_F_FLUSH | \ 104 VTBLK_F_TOPOLOGY | \ 105 VIRTIO_RING_F_INDIRECT_DESC ) /* indirect descriptors */ 106 107 /* 108 * The current blockif_delete() interface only allows a single delete 109 * request at a time. 110 */ 111 #define VTBLK_MAX_DISCARD_SEG 1 112 113 /* 114 * An arbitrary limit to prevent excessive latency due to large 115 * delete requests. 116 */ 117 #define VTBLK_MAX_DISCARD_SECT ((16 << 20) / VTBLK_BSIZE) /* 16 MiB */ 118 119 /* 120 * Config space "registers" 121 */ 122 struct vtblk_config { 123 uint64_t vbc_capacity; 124 uint32_t vbc_size_max; 125 uint32_t vbc_seg_max; 126 struct { 127 uint16_t cylinders; 128 uint8_t heads; 129 uint8_t sectors; 130 } vbc_geometry; 131 uint32_t vbc_blk_size; 132 struct { 133 uint8_t physical_block_exp; 134 uint8_t alignment_offset; 135 uint16_t min_io_size; 136 uint32_t opt_io_size; 137 } vbc_topology; 138 uint8_t vbc_writeback; 139 uint8_t unused0[3]; 140 uint32_t max_discard_sectors; 141 uint32_t max_discard_seg; 142 uint32_t discard_sector_alignment; 143 uint32_t max_write_zeroes_sectors; 144 uint32_t max_write_zeroes_seg; 145 uint8_t write_zeroes_may_unmap; 146 uint8_t unused1[3]; 147 } __packed; 148 149 /* 150 * Fixed-size block header 151 */ 152 struct virtio_blk_hdr { 153 #define VBH_OP_READ 0 154 #define VBH_OP_WRITE 1 155 #define VBH_OP_SCSI_CMD 2 156 #define VBH_OP_SCSI_CMD_OUT 3 157 #define VBH_OP_FLUSH 4 158 #define VBH_OP_FLUSH_OUT 5 159 #define VBH_OP_IDENT 8 160 #define VBH_OP_DISCARD 11 161 #define VBH_OP_WRITE_ZEROES 13 162 163 #define VBH_FLAG_BARRIER 0x80000000 /* OR'ed into vbh_type */ 164 uint32_t vbh_type; 165 uint32_t vbh_ioprio; 166 uint64_t vbh_sector; 167 } __packed; 168 169 /* 170 * Debug printf 171 */ 172 static int pci_vtblk_debug; 173 #define DPRINTF(params) if (pci_vtblk_debug) printf params 174 #define WPRINTF(params) printf params 175 176 struct pci_vtblk_ioreq { 177 struct blockif_req io_req; 178 struct pci_vtblk_softc *io_sc; 179 uint8_t *io_status; 180 uint16_t io_idx; 181 }; 182 183 struct virtio_blk_discard_write_zeroes { 184 uint64_t sector; 185 uint32_t num_sectors; 186 struct { 187 uint32_t unmap:1; 188 uint32_t reserved:31; 189 } flags; 190 }; 191 192 /* 193 * Per-device softc 194 */ 195 struct pci_vtblk_softc { 196 struct virtio_softc vbsc_vs; 197 pthread_mutex_t vsc_mtx; 198 struct vqueue_info vbsc_vq; 199 struct vtblk_config vbsc_cfg; 200 struct virtio_consts vbsc_consts; 201 struct blockif_ctxt *bc; 202 #ifndef __FreeBSD__ 203 int vbsc_wce; 204 #endif 205 char vbsc_ident[VTBLK_BLK_ID_BYTES]; 206 struct pci_vtblk_ioreq vbsc_ios[VTBLK_RINGSZ]; 207 }; 208 209 static void pci_vtblk_reset(void *); 210 static void pci_vtblk_notify(void *, struct vqueue_info *); 211 static int pci_vtblk_cfgread(void *, int, int, uint32_t *); 212 static int pci_vtblk_cfgwrite(void *, int, int, uint32_t); 213 #ifndef __FreeBSD__ 214 static void pci_vtblk_apply_feats(void *, uint64_t); 215 #endif 216 217 static struct virtio_consts vtblk_vi_consts = { 218 "vtblk", /* our name */ 219 1, /* we support 1 virtqueue */ 220 sizeof(struct vtblk_config), /* config reg size */ 221 pci_vtblk_reset, /* reset */ 222 pci_vtblk_notify, /* device-wide qnotify */ 223 pci_vtblk_cfgread, /* read PCI config */ 224 pci_vtblk_cfgwrite, /* write PCI config */ 225 #ifndef __FreeBSD__ 226 pci_vtblk_apply_feats, /* apply negotiated features */ 227 #else 228 NULL, /* apply negotiated features */ 229 #endif 230 VTBLK_S_HOSTCAPS, /* our capabilities */ 231 }; 232 233 static void 234 pci_vtblk_reset(void *vsc) 235 { 236 struct pci_vtblk_softc *sc = vsc; 237 238 DPRINTF(("vtblk: device reset requested !\n")); 239 vi_reset_dev(&sc->vbsc_vs); 240 #ifndef __FreeBSD__ 241 /* Disable write cache until FLUSH feature is negotiated */ 242 (void) blockif_set_wce(sc->bc, 0); 243 sc->vbsc_wce = 0; 244 #endif 245 } 246 247 static void 248 pci_vtblk_done_locked(struct pci_vtblk_ioreq *io, int err) 249 { 250 struct pci_vtblk_softc *sc = io->io_sc; 251 252 /* convert errno into a virtio block error return */ 253 if (err == EOPNOTSUPP || err == ENOSYS) 254 *io->io_status = VTBLK_S_UNSUPP; 255 else if (err != 0) 256 *io->io_status = VTBLK_S_IOERR; 257 else 258 *io->io_status = VTBLK_S_OK; 259 260 /* 261 * Return the descriptor back to the host. 262 * We wrote 1 byte (our status) to host. 263 */ 264 vq_relchain(&sc->vbsc_vq, io->io_idx, 1); 265 vq_endchains(&sc->vbsc_vq, 0); 266 } 267 268 static void 269 pci_vtblk_done(struct blockif_req *br, int err) 270 { 271 struct pci_vtblk_ioreq *io = br->br_param; 272 struct pci_vtblk_softc *sc = io->io_sc; 273 274 pthread_mutex_lock(&sc->vsc_mtx); 275 pci_vtblk_done_locked(io, err); 276 pthread_mutex_unlock(&sc->vsc_mtx); 277 } 278 279 static void 280 pci_vtblk_proc(struct pci_vtblk_softc *sc, struct vqueue_info *vq) 281 { 282 struct virtio_blk_hdr *vbh; 283 struct pci_vtblk_ioreq *io; 284 int i, n; 285 int err; 286 ssize_t iolen; 287 int writeop, type; 288 struct iovec iov[BLOCKIF_IOV_MAX + 2]; 289 uint16_t idx, flags[BLOCKIF_IOV_MAX + 2]; 290 struct virtio_blk_discard_write_zeroes *discard; 291 292 n = vq_getchain(vq, &idx, iov, BLOCKIF_IOV_MAX + 2, flags); 293 294 /* 295 * The first descriptor will be the read-only fixed header, 296 * and the last is for status (hence +2 above and below). 297 * The remaining iov's are the actual data I/O vectors. 298 * 299 * XXX - note - this fails on crash dump, which does a 300 * VIRTIO_BLK_T_FLUSH with a zero transfer length 301 */ 302 assert(n >= 2 && n <= BLOCKIF_IOV_MAX + 2); 303 304 io = &sc->vbsc_ios[idx]; 305 assert((flags[0] & VRING_DESC_F_WRITE) == 0); 306 assert(iov[0].iov_len == sizeof(struct virtio_blk_hdr)); 307 vbh = (struct virtio_blk_hdr *)iov[0].iov_base; 308 memcpy(&io->io_req.br_iov, &iov[1], sizeof(struct iovec) * (n - 2)); 309 io->io_req.br_iovcnt = n - 2; 310 io->io_req.br_offset = vbh->vbh_sector * VTBLK_BSIZE; 311 io->io_status = (uint8_t *)iov[--n].iov_base; 312 assert(iov[n].iov_len == 1); 313 assert(flags[n] & VRING_DESC_F_WRITE); 314 315 /* 316 * XXX 317 * The guest should not be setting the BARRIER flag because 318 * we don't advertise the capability. 319 */ 320 type = vbh->vbh_type & ~VBH_FLAG_BARRIER; 321 writeop = (type == VBH_OP_WRITE || type == VBH_OP_DISCARD); 322 323 iolen = 0; 324 for (i = 1; i < n; i++) { 325 /* 326 * - write op implies read-only descriptor, 327 * - read/ident op implies write-only descriptor, 328 * therefore test the inverse of the descriptor bit 329 * to the op. 330 */ 331 assert(((flags[i] & VRING_DESC_F_WRITE) == 0) == writeop); 332 iolen += iov[i].iov_len; 333 } 334 io->io_req.br_resid = iolen; 335 336 DPRINTF(("virtio-block: %s op, %zd bytes, %d segs, offset %ld\n\r", 337 writeop ? "write/discard" : "read/ident", iolen, i - 1, 338 io->io_req.br_offset)); 339 340 switch (type) { 341 case VBH_OP_READ: 342 err = blockif_read(sc->bc, &io->io_req); 343 break; 344 case VBH_OP_WRITE: 345 err = blockif_write(sc->bc, &io->io_req); 346 break; 347 case VBH_OP_DISCARD: 348 /* 349 * We currently only support a single request, if the guest 350 * has submitted a request that doesn't conform to the 351 * requirements, we return a error. 352 */ 353 if (iov[1].iov_len != sizeof (*discard)) { 354 pci_vtblk_done_locked(io, EINVAL); 355 return; 356 } 357 358 /* The segments to discard are provided rather than data */ 359 discard = (struct virtio_blk_discard_write_zeroes *) 360 iov[1].iov_base; 361 362 /* 363 * virtio v1.1 5.2.6.2: 364 * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP 365 * for discard and write zeroes commands if any unknown flag is 366 * set. Furthermore, the device MUST set the status byte to 367 * VIRTIO_BLK_S_UNSUPP for discard commands if the unmap flag 368 * is set. 369 * 370 * Currently there are no known flags for a DISCARD request. 371 */ 372 if (discard->flags.unmap != 0 || discard->flags.reserved != 0) { 373 pci_vtblk_done_locked(io, ENOTSUP); 374 return; 375 } 376 377 /* Make sure the request doesn't exceed our size limit */ 378 if (discard->num_sectors > VTBLK_MAX_DISCARD_SECT) { 379 pci_vtblk_done_locked(io, EINVAL); 380 return; 381 } 382 383 io->io_req.br_offset = discard->sector * VTBLK_BSIZE; 384 io->io_req.br_resid = discard->num_sectors * VTBLK_BSIZE; 385 err = blockif_delete(sc->bc, &io->io_req); 386 break; 387 case VBH_OP_FLUSH: 388 case VBH_OP_FLUSH_OUT: 389 err = blockif_flush(sc->bc, &io->io_req); 390 break; 391 case VBH_OP_IDENT: 392 /* Assume a single buffer */ 393 /* S/n equal to buffer is not zero-terminated. */ 394 memset(iov[1].iov_base, 0, iov[1].iov_len); 395 strncpy(iov[1].iov_base, sc->vbsc_ident, 396 MIN(iov[1].iov_len, sizeof(sc->vbsc_ident))); 397 pci_vtblk_done_locked(io, 0); 398 return; 399 default: 400 pci_vtblk_done_locked(io, EOPNOTSUPP); 401 return; 402 } 403 assert(err == 0); 404 } 405 406 static void 407 pci_vtblk_notify(void *vsc, struct vqueue_info *vq) 408 { 409 struct pci_vtblk_softc *sc = vsc; 410 411 while (vq_has_descs(vq)) 412 pci_vtblk_proc(sc, vq); 413 } 414 415 static int 416 pci_vtblk_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) 417 { 418 char bident[sizeof("XX:X:X")]; 419 struct blockif_ctxt *bctxt; 420 MD5_CTX mdctx; 421 u_char digest[16]; 422 struct pci_vtblk_softc *sc; 423 off_t size; 424 int i, sectsz, sts, sto; 425 426 if (opts == NULL) { 427 printf("virtio-block: backing device required\n"); 428 return (1); 429 } 430 431 /* 432 * The supplied backing file has to exist 433 */ 434 snprintf(bident, sizeof(bident), "%d:%d", pi->pi_slot, pi->pi_func); 435 bctxt = blockif_open(opts, bident); 436 if (bctxt == NULL) { 437 perror("Could not open backing file"); 438 return (1); 439 } 440 441 size = blockif_size(bctxt); 442 sectsz = blockif_sectsz(bctxt); 443 blockif_psectsz(bctxt, &sts, &sto); 444 445 sc = calloc(1, sizeof(struct pci_vtblk_softc)); 446 sc->bc = bctxt; 447 for (i = 0; i < VTBLK_RINGSZ; i++) { 448 struct pci_vtblk_ioreq *io = &sc->vbsc_ios[i]; 449 io->io_req.br_callback = pci_vtblk_done; 450 io->io_req.br_param = io; 451 io->io_sc = sc; 452 io->io_idx = i; 453 } 454 455 bcopy(&vtblk_vi_consts, &sc->vbsc_consts, sizeof (vtblk_vi_consts)); 456 if (blockif_candelete(sc->bc)) 457 sc->vbsc_consts.vc_hv_caps |= VTBLK_F_DISCARD; 458 459 #ifndef __FreeBSD__ 460 /* Disable write cache until FLUSH feature is negotiated */ 461 (void) blockif_set_wce(sc->bc, 0); 462 sc->vbsc_wce = 0; 463 #endif 464 465 pthread_mutex_init(&sc->vsc_mtx, NULL); 466 467 /* init virtio softc and virtqueues */ 468 vi_softc_linkup(&sc->vbsc_vs, &sc->vbsc_consts, sc, pi, &sc->vbsc_vq); 469 sc->vbsc_vs.vs_mtx = &sc->vsc_mtx; 470 471 sc->vbsc_vq.vq_qsize = VTBLK_RINGSZ; 472 /* sc->vbsc_vq.vq_notify = we have no per-queue notify */ 473 474 /* 475 * Create an identifier for the backing file. Use parts of the 476 * md5 sum of the filename 477 */ 478 MD5Init(&mdctx); 479 MD5Update(&mdctx, opts, strlen(opts)); 480 MD5Final(digest, &mdctx); 481 snprintf(sc->vbsc_ident, VTBLK_BLK_ID_BYTES, 482 "BHYVE-%02X%02X-%02X%02X-%02X%02X", 483 digest[0], digest[1], digest[2], digest[3], digest[4], digest[5]); 484 485 /* setup virtio block config space */ 486 sc->vbsc_cfg.vbc_capacity = size / VTBLK_BSIZE; /* 512-byte units */ 487 sc->vbsc_cfg.vbc_size_max = 0; /* not negotiated */ 488 489 /* 490 * If Linux is presented with a seg_max greater than the virtio queue 491 * size, it can stumble into situations where it violates its own 492 * invariants and panics. For safety, we keep seg_max clamped, paying 493 * heed to the two extra descriptors needed for the header and status 494 * of a request. 495 */ 496 sc->vbsc_cfg.vbc_seg_max = MIN(VTBLK_RINGSZ - 2, BLOCKIF_IOV_MAX); 497 sc->vbsc_cfg.vbc_geometry.cylinders = 0; /* no geometry */ 498 sc->vbsc_cfg.vbc_geometry.heads = 0; 499 sc->vbsc_cfg.vbc_geometry.sectors = 0; 500 sc->vbsc_cfg.vbc_blk_size = sectsz; 501 sc->vbsc_cfg.vbc_topology.physical_block_exp = 502 (sts > sectsz) ? (ffsll(sts / sectsz) - 1) : 0; 503 sc->vbsc_cfg.vbc_topology.alignment_offset = 504 (sto != 0) ? ((sts - sto) / sectsz) : 0; 505 sc->vbsc_cfg.vbc_topology.min_io_size = 0; 506 sc->vbsc_cfg.vbc_topology.opt_io_size = 0; 507 sc->vbsc_cfg.vbc_writeback = 0; 508 sc->vbsc_cfg.max_discard_sectors = VTBLK_MAX_DISCARD_SECT; 509 sc->vbsc_cfg.max_discard_seg = VTBLK_MAX_DISCARD_SEG; 510 sc->vbsc_cfg.discard_sector_alignment = sectsz / VTBLK_BSIZE; 511 512 /* 513 * Should we move some of this into virtio.c? Could 514 * have the device, class, and subdev_0 as fields in 515 * the virtio constants structure. 516 */ 517 pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_BLOCK); 518 pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR); 519 pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_STORAGE); 520 pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_BLOCK); 521 pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR); 522 523 if (vi_intr_init(&sc->vbsc_vs, 1, fbsdrun_virtio_msix())) { 524 blockif_close(sc->bc); 525 free(sc); 526 return (1); 527 } 528 vi_set_io_bar(&sc->vbsc_vs, 0); 529 return (0); 530 } 531 532 static int 533 pci_vtblk_cfgwrite(void *vsc, int offset, int size, uint32_t value) 534 { 535 536 DPRINTF(("vtblk: write to readonly reg %d\n\r", offset)); 537 return (1); 538 } 539 540 static int 541 pci_vtblk_cfgread(void *vsc, int offset, int size, uint32_t *retval) 542 { 543 struct pci_vtblk_softc *sc = vsc; 544 void *ptr; 545 546 /* our caller has already verified offset and size */ 547 ptr = (uint8_t *)&sc->vbsc_cfg + offset; 548 memcpy(retval, ptr, size); 549 return (0); 550 } 551 552 #ifndef __FreeBSD__ 553 void 554 pci_vtblk_apply_feats(void *vsc, uint64_t caps) 555 { 556 struct pci_vtblk_softc *sc = vsc; 557 const int wce_next = ((caps & VTBLK_F_FLUSH) != 0) ? 1 : 0; 558 559 if (sc->vbsc_wce != wce_next) { 560 (void) blockif_set_wce(sc->bc, wce_next); 561 sc->vbsc_wce = wce_next; 562 } 563 } 564 #endif /* __FreeBSD__ */ 565 566 struct pci_devemu pci_de_vblk = { 567 .pe_emu = "virtio-blk", 568 .pe_init = pci_vtblk_init, 569 .pe_barwrite = vi_pci_write, 570 .pe_barread = vi_pci_read 571 }; 572 PCI_EMUL_SET(pci_de_vblk); 573