1 /* 2 * XenBSD block device driver 3 * 4 * Copyright (c) 2010-2013 Spectra Logic Corporation 5 * Copyright (c) 2009 Scott Long, Yahoo! 6 * Copyright (c) 2009 Frank Suchomel, Citrix 7 * Copyright (c) 2009 Doug F. Rabson, Citrix 8 * Copyright (c) 2005 Kip Macy 9 * Copyright (c) 2003-2004, Keir Fraser & Steve Hand 10 * Modifications by Mark A. Williamson are (c) Intel Research Cambridge 11 * 12 * 13 * Permission is hereby granted, free of charge, to any person obtaining a copy 14 * of this software and associated documentation files (the "Software"), to 15 * deal in the Software without restriction, including without limitation the 16 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 17 * sell copies of the Software, and to permit persons to whom the Software is 18 * furnished to do so, subject to the following conditions: 19 * 20 * The above copyright notice and this permission notice shall be included in 21 * all copies or substantial portions of the Software. 22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 28 * DEALINGS IN THE SOFTWARE. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/malloc.h> 37 #include <sys/kernel.h> 38 #include <vm/vm.h> 39 #include <vm/pmap.h> 40 41 #include <sys/bio.h> 42 #include <sys/bus.h> 43 #include <sys/conf.h> 44 #include <sys/module.h> 45 #include <sys/sysctl.h> 46 47 #include <machine/bus.h> 48 #include <sys/rman.h> 49 #include <machine/resource.h> 50 #include <machine/intr_machdep.h> 51 #include <machine/vmparam.h> 52 #include <sys/bus_dma.h> 53 54 #include <xen/xen-os.h> 55 #include <xen/hypervisor.h> 56 #include <xen/xen_intr.h> 57 #include <xen/gnttab.h> 58 #include <xen/interface/grant_table.h> 59 #include <xen/interface/io/protocols.h> 60 #include <xen/xenbus/xenbusvar.h> 61 62 #include <machine/_inttypes.h> 63 64 #include <geom/geom_disk.h> 65 66 #include <dev/xen/blkfront/block.h> 67 68 #include "xenbus_if.h" 69 70 /*--------------------------- Forward Declarations ---------------------------*/ 71 static void xbd_closing(device_t); 72 static void xbd_startio(struct xbd_softc *sc); 73 74 /*---------------------------------- Macros ----------------------------------*/ 75 #if 0 76 #define DPRINTK(fmt, args...) printf("[XEN] %s:%d: " fmt ".\n", __func__, __LINE__, ##args) 77 #else 78 #define DPRINTK(fmt, args...) 79 #endif 80 81 #define XBD_SECTOR_SHFT 9 82 83 /*---------------------------- Global Static Data ----------------------------*/ 84 static MALLOC_DEFINE(M_XENBLOCKFRONT, "xbd", "Xen Block Front driver data"); 85 86 static int xbd_enable_indirect = 1; 87 SYSCTL_NODE(_hw, OID_AUTO, xbd, CTLFLAG_RD, 0, "xbd driver parameters"); 88 SYSCTL_INT(_hw_xbd, OID_AUTO, xbd_enable_indirect, CTLFLAG_RDTUN, 89 &xbd_enable_indirect, 0, "Enable xbd indirect segments"); 90 91 /*---------------------------- Command Processing ----------------------------*/ 92 static void 93 xbd_freeze(struct xbd_softc *sc, xbd_flag_t xbd_flag) 94 { 95 if (xbd_flag != XBDF_NONE && (sc->xbd_flags & xbd_flag) != 0) 96 return; 97 98 sc->xbd_flags |= xbd_flag; 99 sc->xbd_qfrozen_cnt++; 100 } 101 102 static void 103 xbd_thaw(struct xbd_softc *sc, xbd_flag_t xbd_flag) 104 { 105 if (xbd_flag != XBDF_NONE && (sc->xbd_flags & xbd_flag) == 0) 106 return; 107 108 if (sc->xbd_qfrozen_cnt == 0) 109 panic("%s: Thaw with flag 0x%x while not frozen.", 110 __func__, xbd_flag); 111 112 sc->xbd_flags &= ~xbd_flag; 113 sc->xbd_qfrozen_cnt--; 114 } 115 116 static void 117 xbd_cm_freeze(struct xbd_softc *sc, struct xbd_command *cm, xbdc_flag_t cm_flag) 118 { 119 if ((cm->cm_flags & XBDCF_FROZEN) != 0) 120 return; 121 122 cm->cm_flags |= XBDCF_FROZEN|cm_flag; 123 xbd_freeze(sc, XBDF_NONE); 124 } 125 126 static void 127 xbd_cm_thaw(struct xbd_softc *sc, struct xbd_command *cm) 128 { 129 if ((cm->cm_flags & XBDCF_FROZEN) == 0) 130 return; 131 132 cm->cm_flags &= ~XBDCF_FROZEN; 133 xbd_thaw(sc, XBDF_NONE); 134 } 135 136 static inline void 137 xbd_flush_requests(struct xbd_softc *sc) 138 { 139 int notify; 140 141 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&sc->xbd_ring, notify); 142 143 if (notify) 144 xen_intr_signal(sc->xen_intr_handle); 145 } 146 147 static void 148 xbd_free_command(struct xbd_command *cm) 149 { 150 151 KASSERT((cm->cm_flags & XBDCF_Q_MASK) == XBD_Q_NONE, 152 ("Freeing command that is still on queue %d.", 153 cm->cm_flags & XBDCF_Q_MASK)); 154 155 cm->cm_flags = XBDCF_INITIALIZER; 156 cm->cm_bp = NULL; 157 cm->cm_complete = NULL; 158 xbd_enqueue_cm(cm, XBD_Q_FREE); 159 xbd_thaw(cm->cm_sc, XBDF_CM_SHORTAGE); 160 } 161 162 static void 163 xbd_mksegarray(bus_dma_segment_t *segs, int nsegs, 164 grant_ref_t * gref_head, int otherend_id, int readonly, 165 grant_ref_t * sg_ref, struct blkif_request_segment *sg) 166 { 167 struct blkif_request_segment *last_block_sg = sg + nsegs; 168 vm_paddr_t buffer_ma; 169 uint64_t fsect, lsect; 170 int ref; 171 172 while (sg < last_block_sg) { 173 KASSERT(segs->ds_addr % (1 << XBD_SECTOR_SHFT) == 0, 174 ("XEN disk driver I/O must be sector aligned")); 175 KASSERT(segs->ds_len % (1 << XBD_SECTOR_SHFT) == 0, 176 ("XEN disk driver I/Os must be a multiple of " 177 "the sector length")); 178 buffer_ma = segs->ds_addr; 179 fsect = (buffer_ma & PAGE_MASK) >> XBD_SECTOR_SHFT; 180 lsect = fsect + (segs->ds_len >> XBD_SECTOR_SHFT) - 1; 181 182 KASSERT(lsect <= 7, ("XEN disk driver data cannot " 183 "cross a page boundary")); 184 185 /* install a grant reference. */ 186 ref = gnttab_claim_grant_reference(gref_head); 187 188 /* 189 * GNTTAB_LIST_END == 0xffffffff, but it is private 190 * to gnttab.c. 191 */ 192 KASSERT(ref != ~0, ("grant_reference failed")); 193 194 gnttab_grant_foreign_access_ref( 195 ref, 196 otherend_id, 197 buffer_ma >> PAGE_SHIFT, 198 readonly); 199 200 *sg_ref = ref; 201 *sg = (struct blkif_request_segment) { 202 .gref = ref, 203 .first_sect = fsect, 204 .last_sect = lsect 205 }; 206 sg++; 207 sg_ref++; 208 segs++; 209 } 210 } 211 212 static void 213 xbd_queue_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 214 { 215 struct xbd_softc *sc; 216 struct xbd_command *cm; 217 int op; 218 219 cm = arg; 220 sc = cm->cm_sc; 221 222 if (error) { 223 cm->cm_bp->bio_error = EIO; 224 biodone(cm->cm_bp); 225 xbd_free_command(cm); 226 return; 227 } 228 229 KASSERT(nsegs <= sc->xbd_max_request_segments, 230 ("Too many segments in a blkfront I/O")); 231 232 if (nsegs <= BLKIF_MAX_SEGMENTS_PER_REQUEST) { 233 blkif_request_t *ring_req; 234 235 /* Fill out a blkif_request_t structure. */ 236 ring_req = (blkif_request_t *) 237 RING_GET_REQUEST(&sc->xbd_ring, sc->xbd_ring.req_prod_pvt); 238 sc->xbd_ring.req_prod_pvt++; 239 ring_req->id = cm->cm_id; 240 ring_req->operation = cm->cm_operation; 241 ring_req->sector_number = cm->cm_sector_number; 242 ring_req->handle = (blkif_vdev_t)(uintptr_t)sc->xbd_disk; 243 ring_req->nr_segments = nsegs; 244 cm->cm_nseg = nsegs; 245 xbd_mksegarray(segs, nsegs, &cm->cm_gref_head, 246 xenbus_get_otherend_id(sc->xbd_dev), 247 cm->cm_operation == BLKIF_OP_WRITE, 248 cm->cm_sg_refs, ring_req->seg); 249 } else { 250 blkif_request_indirect_t *ring_req; 251 252 /* Fill out a blkif_request_indirect_t structure. */ 253 ring_req = (blkif_request_indirect_t *) 254 RING_GET_REQUEST(&sc->xbd_ring, sc->xbd_ring.req_prod_pvt); 255 sc->xbd_ring.req_prod_pvt++; 256 ring_req->id = cm->cm_id; 257 ring_req->operation = BLKIF_OP_INDIRECT; 258 ring_req->indirect_op = cm->cm_operation; 259 ring_req->sector_number = cm->cm_sector_number; 260 ring_req->handle = (blkif_vdev_t)(uintptr_t)sc->xbd_disk; 261 ring_req->nr_segments = nsegs; 262 cm->cm_nseg = nsegs; 263 xbd_mksegarray(segs, nsegs, &cm->cm_gref_head, 264 xenbus_get_otherend_id(sc->xbd_dev), 265 cm->cm_operation == BLKIF_OP_WRITE, 266 cm->cm_sg_refs, cm->cm_indirectionpages); 267 memcpy(ring_req->indirect_grefs, &cm->cm_indirectionrefs, 268 sizeof(grant_ref_t) * sc->xbd_max_request_indirectpages); 269 } 270 271 if (cm->cm_operation == BLKIF_OP_READ) 272 op = BUS_DMASYNC_PREREAD; 273 else if (cm->cm_operation == BLKIF_OP_WRITE) 274 op = BUS_DMASYNC_PREWRITE; 275 else 276 op = 0; 277 bus_dmamap_sync(sc->xbd_io_dmat, cm->cm_map, op); 278 279 gnttab_free_grant_references(cm->cm_gref_head); 280 281 xbd_enqueue_cm(cm, XBD_Q_BUSY); 282 283 /* 284 * If bus dma had to asynchronously call us back to dispatch 285 * this command, we are no longer executing in the context of 286 * xbd_startio(). Thus we cannot rely on xbd_startio()'s call to 287 * xbd_flush_requests() to publish this command to the backend 288 * along with any other commands that it could batch. 289 */ 290 if ((cm->cm_flags & XBDCF_ASYNC_MAPPING) != 0) 291 xbd_flush_requests(sc); 292 293 return; 294 } 295 296 static int 297 xbd_queue_request(struct xbd_softc *sc, struct xbd_command *cm) 298 { 299 int error; 300 301 if (cm->cm_bp != NULL) 302 error = bus_dmamap_load_bio(sc->xbd_io_dmat, cm->cm_map, 303 cm->cm_bp, xbd_queue_cb, cm, 0); 304 else 305 error = bus_dmamap_load(sc->xbd_io_dmat, cm->cm_map, 306 cm->cm_data, cm->cm_datalen, xbd_queue_cb, cm, 0); 307 if (error == EINPROGRESS) { 308 /* 309 * Maintain queuing order by freezing the queue. The next 310 * command may not require as many resources as the command 311 * we just attempted to map, so we can't rely on bus dma 312 * blocking for it too. 313 */ 314 xbd_cm_freeze(sc, cm, XBDCF_ASYNC_MAPPING); 315 return (0); 316 } 317 318 return (error); 319 } 320 321 static void 322 xbd_restart_queue_callback(void *arg) 323 { 324 struct xbd_softc *sc = arg; 325 326 mtx_lock(&sc->xbd_io_lock); 327 328 xbd_thaw(sc, XBDF_GNT_SHORTAGE); 329 330 xbd_startio(sc); 331 332 mtx_unlock(&sc->xbd_io_lock); 333 } 334 335 static struct xbd_command * 336 xbd_bio_command(struct xbd_softc *sc) 337 { 338 struct xbd_command *cm; 339 struct bio *bp; 340 341 if (__predict_false(sc->xbd_state != XBD_STATE_CONNECTED)) 342 return (NULL); 343 344 bp = xbd_dequeue_bio(sc); 345 if (bp == NULL) 346 return (NULL); 347 348 if ((cm = xbd_dequeue_cm(sc, XBD_Q_FREE)) == NULL) { 349 xbd_freeze(sc, XBDF_CM_SHORTAGE); 350 xbd_requeue_bio(sc, bp); 351 return (NULL); 352 } 353 354 if (gnttab_alloc_grant_references(sc->xbd_max_request_segments, 355 &cm->cm_gref_head) != 0) { 356 gnttab_request_free_callback(&sc->xbd_callback, 357 xbd_restart_queue_callback, sc, 358 sc->xbd_max_request_segments); 359 xbd_freeze(sc, XBDF_GNT_SHORTAGE); 360 xbd_requeue_bio(sc, bp); 361 xbd_enqueue_cm(cm, XBD_Q_FREE); 362 return (NULL); 363 } 364 365 cm->cm_bp = bp; 366 cm->cm_sector_number = (blkif_sector_t)bp->bio_pblkno; 367 368 switch (bp->bio_cmd) { 369 case BIO_READ: 370 cm->cm_operation = BLKIF_OP_READ; 371 break; 372 case BIO_WRITE: 373 cm->cm_operation = BLKIF_OP_WRITE; 374 if ((bp->bio_flags & BIO_ORDERED) != 0) { 375 if ((sc->xbd_flags & XBDF_BARRIER) != 0) { 376 cm->cm_operation = BLKIF_OP_WRITE_BARRIER; 377 } else { 378 /* 379 * Single step this command. 380 */ 381 cm->cm_flags |= XBDCF_Q_FREEZE; 382 if (xbd_queue_length(sc, XBD_Q_BUSY) != 0) { 383 /* 384 * Wait for in-flight requests to 385 * finish. 386 */ 387 xbd_freeze(sc, XBDF_WAIT_IDLE); 388 xbd_requeue_cm(cm, XBD_Q_READY); 389 return (NULL); 390 } 391 } 392 } 393 break; 394 case BIO_FLUSH: 395 if ((sc->xbd_flags & XBDF_FLUSH) != 0) 396 cm->cm_operation = BLKIF_OP_FLUSH_DISKCACHE; 397 else if ((sc->xbd_flags & XBDF_BARRIER) != 0) 398 cm->cm_operation = BLKIF_OP_WRITE_BARRIER; 399 else 400 panic("flush request, but no flush support available"); 401 break; 402 default: 403 panic("unknown bio command %d", bp->bio_cmd); 404 } 405 406 return (cm); 407 } 408 409 /* 410 * Dequeue buffers and place them in the shared communication ring. 411 * Return when no more requests can be accepted or all buffers have 412 * been queued. 413 * 414 * Signal XEN once the ring has been filled out. 415 */ 416 static void 417 xbd_startio(struct xbd_softc *sc) 418 { 419 struct xbd_command *cm; 420 int error, queued = 0; 421 422 mtx_assert(&sc->xbd_io_lock, MA_OWNED); 423 424 if (sc->xbd_state != XBD_STATE_CONNECTED) 425 return; 426 427 while (!RING_FULL(&sc->xbd_ring)) { 428 429 if (sc->xbd_qfrozen_cnt != 0) 430 break; 431 432 cm = xbd_dequeue_cm(sc, XBD_Q_READY); 433 434 if (cm == NULL) 435 cm = xbd_bio_command(sc); 436 437 if (cm == NULL) 438 break; 439 440 if ((cm->cm_flags & XBDCF_Q_FREEZE) != 0) { 441 /* 442 * Single step command. Future work is 443 * held off until this command completes. 444 */ 445 xbd_cm_freeze(sc, cm, XBDCF_Q_FREEZE); 446 } 447 448 if ((error = xbd_queue_request(sc, cm)) != 0) { 449 printf("xbd_queue_request returned %d\n", error); 450 break; 451 } 452 queued++; 453 } 454 455 if (queued != 0) 456 xbd_flush_requests(sc); 457 } 458 459 static void 460 xbd_bio_complete(struct xbd_softc *sc, struct xbd_command *cm) 461 { 462 struct bio *bp; 463 464 bp = cm->cm_bp; 465 466 if (__predict_false(cm->cm_status != BLKIF_RSP_OKAY)) { 467 disk_err(bp, "disk error" , -1, 0); 468 printf(" status: %x\n", cm->cm_status); 469 bp->bio_flags |= BIO_ERROR; 470 } 471 472 if (bp->bio_flags & BIO_ERROR) 473 bp->bio_error = EIO; 474 else 475 bp->bio_resid = 0; 476 477 xbd_free_command(cm); 478 biodone(bp); 479 } 480 481 static void 482 xbd_int(void *xsc) 483 { 484 struct xbd_softc *sc = xsc; 485 struct xbd_command *cm; 486 blkif_response_t *bret; 487 RING_IDX i, rp; 488 int op; 489 490 mtx_lock(&sc->xbd_io_lock); 491 492 if (__predict_false(sc->xbd_state == XBD_STATE_DISCONNECTED)) { 493 mtx_unlock(&sc->xbd_io_lock); 494 return; 495 } 496 497 again: 498 rp = sc->xbd_ring.sring->rsp_prod; 499 rmb(); /* Ensure we see queued responses up to 'rp'. */ 500 501 for (i = sc->xbd_ring.rsp_cons; i != rp;) { 502 bret = RING_GET_RESPONSE(&sc->xbd_ring, i); 503 cm = &sc->xbd_shadow[bret->id]; 504 505 xbd_remove_cm(cm, XBD_Q_BUSY); 506 gnttab_end_foreign_access_references(cm->cm_nseg, 507 cm->cm_sg_refs); 508 i++; 509 510 if (cm->cm_operation == BLKIF_OP_READ) 511 op = BUS_DMASYNC_POSTREAD; 512 else if (cm->cm_operation == BLKIF_OP_WRITE || 513 cm->cm_operation == BLKIF_OP_WRITE_BARRIER) 514 op = BUS_DMASYNC_POSTWRITE; 515 else 516 op = 0; 517 bus_dmamap_sync(sc->xbd_io_dmat, cm->cm_map, op); 518 bus_dmamap_unload(sc->xbd_io_dmat, cm->cm_map); 519 520 /* 521 * Release any hold this command has on future command 522 * dispatch. 523 */ 524 xbd_cm_thaw(sc, cm); 525 526 /* 527 * Directly call the i/o complete routine to save an 528 * an indirection in the common case. 529 */ 530 cm->cm_status = bret->status; 531 if (cm->cm_bp) 532 xbd_bio_complete(sc, cm); 533 else if (cm->cm_complete != NULL) 534 cm->cm_complete(cm); 535 else 536 xbd_free_command(cm); 537 } 538 539 sc->xbd_ring.rsp_cons = i; 540 541 if (i != sc->xbd_ring.req_prod_pvt) { 542 int more_to_do; 543 RING_FINAL_CHECK_FOR_RESPONSES(&sc->xbd_ring, more_to_do); 544 if (more_to_do) 545 goto again; 546 } else { 547 sc->xbd_ring.sring->rsp_event = i + 1; 548 } 549 550 if (xbd_queue_length(sc, XBD_Q_BUSY) == 0) 551 xbd_thaw(sc, XBDF_WAIT_IDLE); 552 553 xbd_startio(sc); 554 555 if (__predict_false(sc->xbd_state == XBD_STATE_SUSPENDED)) 556 wakeup(&sc->xbd_cm_q[XBD_Q_BUSY]); 557 558 mtx_unlock(&sc->xbd_io_lock); 559 } 560 561 /*------------------------------- Dump Support -------------------------------*/ 562 /** 563 * Quiesce the disk writes for a dump file before allowing the next buffer. 564 */ 565 static void 566 xbd_quiesce(struct xbd_softc *sc) 567 { 568 int mtd; 569 570 // While there are outstanding requests 571 while (xbd_queue_length(sc, XBD_Q_BUSY) != 0) { 572 RING_FINAL_CHECK_FOR_RESPONSES(&sc->xbd_ring, mtd); 573 if (mtd) { 574 /* Recieved request completions, update queue. */ 575 xbd_int(sc); 576 } 577 if (xbd_queue_length(sc, XBD_Q_BUSY) != 0) { 578 /* 579 * Still pending requests, wait for the disk i/o 580 * to complete. 581 */ 582 HYPERVISOR_yield(); 583 } 584 } 585 } 586 587 /* Kernel dump function for a paravirtualized disk device */ 588 static void 589 xbd_dump_complete(struct xbd_command *cm) 590 { 591 592 xbd_enqueue_cm(cm, XBD_Q_COMPLETE); 593 } 594 595 static int 596 xbd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, 597 size_t length) 598 { 599 struct disk *dp = arg; 600 struct xbd_softc *sc = dp->d_drv1; 601 struct xbd_command *cm; 602 size_t chunk; 603 int sbp; 604 int rc = 0; 605 606 if (length <= 0) 607 return (rc); 608 609 xbd_quiesce(sc); /* All quiet on the western front. */ 610 611 /* 612 * If this lock is held, then this module is failing, and a 613 * successful kernel dump is highly unlikely anyway. 614 */ 615 mtx_lock(&sc->xbd_io_lock); 616 617 /* Split the 64KB block as needed */ 618 for (sbp=0; length > 0; sbp++) { 619 cm = xbd_dequeue_cm(sc, XBD_Q_FREE); 620 if (cm == NULL) { 621 mtx_unlock(&sc->xbd_io_lock); 622 device_printf(sc->xbd_dev, "dump: no more commands?\n"); 623 return (EBUSY); 624 } 625 626 if (gnttab_alloc_grant_references(sc->xbd_max_request_segments, 627 &cm->cm_gref_head) != 0) { 628 xbd_free_command(cm); 629 mtx_unlock(&sc->xbd_io_lock); 630 device_printf(sc->xbd_dev, "no more grant allocs?\n"); 631 return (EBUSY); 632 } 633 634 chunk = length > sc->xbd_max_request_size ? 635 sc->xbd_max_request_size : length; 636 cm->cm_data = virtual; 637 cm->cm_datalen = chunk; 638 cm->cm_operation = BLKIF_OP_WRITE; 639 cm->cm_sector_number = offset / dp->d_sectorsize; 640 cm->cm_complete = xbd_dump_complete; 641 642 xbd_enqueue_cm(cm, XBD_Q_READY); 643 644 length -= chunk; 645 offset += chunk; 646 virtual = (char *) virtual + chunk; 647 } 648 649 /* Tell DOM0 to do the I/O */ 650 xbd_startio(sc); 651 mtx_unlock(&sc->xbd_io_lock); 652 653 /* Poll for the completion. */ 654 xbd_quiesce(sc); /* All quite on the eastern front */ 655 656 /* If there were any errors, bail out... */ 657 while ((cm = xbd_dequeue_cm(sc, XBD_Q_COMPLETE)) != NULL) { 658 if (cm->cm_status != BLKIF_RSP_OKAY) { 659 device_printf(sc->xbd_dev, 660 "Dump I/O failed at sector %jd\n", 661 cm->cm_sector_number); 662 rc = EIO; 663 } 664 xbd_free_command(cm); 665 } 666 667 return (rc); 668 } 669 670 /*----------------------------- Disk Entrypoints -----------------------------*/ 671 static int 672 xbd_open(struct disk *dp) 673 { 674 struct xbd_softc *sc = dp->d_drv1; 675 676 if (sc == NULL) { 677 printf("xbd%d: not found", dp->d_unit); 678 return (ENXIO); 679 } 680 681 sc->xbd_flags |= XBDF_OPEN; 682 sc->xbd_users++; 683 return (0); 684 } 685 686 static int 687 xbd_close(struct disk *dp) 688 { 689 struct xbd_softc *sc = dp->d_drv1; 690 691 if (sc == NULL) 692 return (ENXIO); 693 sc->xbd_flags &= ~XBDF_OPEN; 694 if (--(sc->xbd_users) == 0) { 695 /* 696 * Check whether we have been instructed to close. We will 697 * have ignored this request initially, as the device was 698 * still mounted. 699 */ 700 if (xenbus_get_otherend_state(sc->xbd_dev) == 701 XenbusStateClosing) 702 xbd_closing(sc->xbd_dev); 703 } 704 return (0); 705 } 706 707 static int 708 xbd_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td) 709 { 710 struct xbd_softc *sc = dp->d_drv1; 711 712 if (sc == NULL) 713 return (ENXIO); 714 715 return (ENOTTY); 716 } 717 718 /* 719 * Read/write routine for a buffer. Finds the proper unit, place it on 720 * the sortq and kick the controller. 721 */ 722 static void 723 xbd_strategy(struct bio *bp) 724 { 725 struct xbd_softc *sc = bp->bio_disk->d_drv1; 726 727 /* bogus disk? */ 728 if (sc == NULL) { 729 bp->bio_error = EINVAL; 730 bp->bio_flags |= BIO_ERROR; 731 bp->bio_resid = bp->bio_bcount; 732 biodone(bp); 733 return; 734 } 735 736 /* 737 * Place it in the queue of disk activities for this disk 738 */ 739 mtx_lock(&sc->xbd_io_lock); 740 741 xbd_enqueue_bio(sc, bp); 742 xbd_startio(sc); 743 744 mtx_unlock(&sc->xbd_io_lock); 745 return; 746 } 747 748 /*------------------------------ Ring Management -----------------------------*/ 749 static int 750 xbd_alloc_ring(struct xbd_softc *sc) 751 { 752 blkif_sring_t *sring; 753 uintptr_t sring_page_addr; 754 int error; 755 int i; 756 757 sring = malloc(sc->xbd_ring_pages * PAGE_SIZE, M_XENBLOCKFRONT, 758 M_NOWAIT|M_ZERO); 759 if (sring == NULL) { 760 xenbus_dev_fatal(sc->xbd_dev, ENOMEM, "allocating shared ring"); 761 return (ENOMEM); 762 } 763 SHARED_RING_INIT(sring); 764 FRONT_RING_INIT(&sc->xbd_ring, sring, sc->xbd_ring_pages * PAGE_SIZE); 765 766 for (i = 0, sring_page_addr = (uintptr_t)sring; 767 i < sc->xbd_ring_pages; 768 i++, sring_page_addr += PAGE_SIZE) { 769 770 error = xenbus_grant_ring(sc->xbd_dev, 771 (vtophys(sring_page_addr) >> PAGE_SHIFT), 772 &sc->xbd_ring_ref[i]); 773 if (error) { 774 xenbus_dev_fatal(sc->xbd_dev, error, 775 "granting ring_ref(%d)", i); 776 return (error); 777 } 778 } 779 if (sc->xbd_ring_pages == 1) { 780 error = xs_printf(XST_NIL, xenbus_get_node(sc->xbd_dev), 781 "ring-ref", "%u", sc->xbd_ring_ref[0]); 782 if (error) { 783 xenbus_dev_fatal(sc->xbd_dev, error, 784 "writing %s/ring-ref", 785 xenbus_get_node(sc->xbd_dev)); 786 return (error); 787 } 788 } else { 789 for (i = 0; i < sc->xbd_ring_pages; i++) { 790 char ring_ref_name[]= "ring_refXX"; 791 792 snprintf(ring_ref_name, sizeof(ring_ref_name), 793 "ring-ref%u", i); 794 error = xs_printf(XST_NIL, xenbus_get_node(sc->xbd_dev), 795 ring_ref_name, "%u", sc->xbd_ring_ref[i]); 796 if (error) { 797 xenbus_dev_fatal(sc->xbd_dev, error, 798 "writing %s/%s", 799 xenbus_get_node(sc->xbd_dev), 800 ring_ref_name); 801 return (error); 802 } 803 } 804 } 805 806 error = xen_intr_alloc_and_bind_local_port(sc->xbd_dev, 807 xenbus_get_otherend_id(sc->xbd_dev), NULL, xbd_int, sc, 808 INTR_TYPE_BIO | INTR_MPSAFE, &sc->xen_intr_handle); 809 if (error) { 810 xenbus_dev_fatal(sc->xbd_dev, error, 811 "xen_intr_alloc_and_bind_local_port failed"); 812 return (error); 813 } 814 815 return (0); 816 } 817 818 static void 819 xbd_free_ring(struct xbd_softc *sc) 820 { 821 int i; 822 823 if (sc->xbd_ring.sring == NULL) 824 return; 825 826 for (i = 0; i < sc->xbd_ring_pages; i++) { 827 if (sc->xbd_ring_ref[i] != GRANT_REF_INVALID) { 828 gnttab_end_foreign_access_ref(sc->xbd_ring_ref[i]); 829 sc->xbd_ring_ref[i] = GRANT_REF_INVALID; 830 } 831 } 832 free(sc->xbd_ring.sring, M_XENBLOCKFRONT); 833 sc->xbd_ring.sring = NULL; 834 } 835 836 /*-------------------------- Initialization/Teardown -------------------------*/ 837 static int 838 xbd_feature_string(struct xbd_softc *sc, char *features, size_t len) 839 { 840 struct sbuf sb; 841 int feature_cnt; 842 843 sbuf_new(&sb, features, len, SBUF_FIXEDLEN); 844 845 feature_cnt = 0; 846 if ((sc->xbd_flags & XBDF_FLUSH) != 0) { 847 sbuf_printf(&sb, "flush"); 848 feature_cnt++; 849 } 850 851 if ((sc->xbd_flags & XBDF_BARRIER) != 0) { 852 if (feature_cnt != 0) 853 sbuf_printf(&sb, ", "); 854 sbuf_printf(&sb, "write_barrier"); 855 feature_cnt++; 856 } 857 858 (void) sbuf_finish(&sb); 859 return (sbuf_len(&sb)); 860 } 861 862 static int 863 xbd_sysctl_features(SYSCTL_HANDLER_ARGS) 864 { 865 char features[80]; 866 struct xbd_softc *sc = arg1; 867 int error; 868 int len; 869 870 error = sysctl_wire_old_buffer(req, 0); 871 if (error != 0) 872 return (error); 873 874 len = xbd_feature_string(sc, features, sizeof(features)); 875 876 /* len is -1 on error, which will make the SYSCTL_OUT a no-op. */ 877 return (SYSCTL_OUT(req, features, len + 1/*NUL*/)); 878 } 879 880 static void 881 xbd_setup_sysctl(struct xbd_softc *xbd) 882 { 883 struct sysctl_ctx_list *sysctl_ctx = NULL; 884 struct sysctl_oid *sysctl_tree = NULL; 885 struct sysctl_oid_list *children; 886 887 sysctl_ctx = device_get_sysctl_ctx(xbd->xbd_dev); 888 if (sysctl_ctx == NULL) 889 return; 890 891 sysctl_tree = device_get_sysctl_tree(xbd->xbd_dev); 892 if (sysctl_tree == NULL) 893 return; 894 895 children = SYSCTL_CHILDREN(sysctl_tree); 896 SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO, 897 "max_requests", CTLFLAG_RD, &xbd->xbd_max_requests, -1, 898 "maximum outstanding requests (negotiated)"); 899 900 SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO, 901 "max_request_segments", CTLFLAG_RD, 902 &xbd->xbd_max_request_segments, 0, 903 "maximum number of pages per requests (negotiated)"); 904 905 SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO, 906 "max_request_size", CTLFLAG_RD, &xbd->xbd_max_request_size, 0, 907 "maximum size in bytes of a request (negotiated)"); 908 909 SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO, 910 "ring_pages", CTLFLAG_RD, &xbd->xbd_ring_pages, 0, 911 "communication channel pages (negotiated)"); 912 913 SYSCTL_ADD_PROC(sysctl_ctx, children, OID_AUTO, 914 "features", CTLTYPE_STRING|CTLFLAG_RD, xbd, 0, 915 xbd_sysctl_features, "A", "protocol features (negotiated)"); 916 } 917 918 /* 919 * Translate Linux major/minor to an appropriate name and unit 920 * number. For HVM guests, this allows us to use the same drive names 921 * with blkfront as the emulated drives, easing transition slightly. 922 */ 923 static void 924 xbd_vdevice_to_unit(uint32_t vdevice, int *unit, const char **name) 925 { 926 static struct vdev_info { 927 int major; 928 int shift; 929 int base; 930 const char *name; 931 } info[] = { 932 {3, 6, 0, "ada"}, /* ide0 */ 933 {22, 6, 2, "ada"}, /* ide1 */ 934 {33, 6, 4, "ada"}, /* ide2 */ 935 {34, 6, 6, "ada"}, /* ide3 */ 936 {56, 6, 8, "ada"}, /* ide4 */ 937 {57, 6, 10, "ada"}, /* ide5 */ 938 {88, 6, 12, "ada"}, /* ide6 */ 939 {89, 6, 14, "ada"}, /* ide7 */ 940 {90, 6, 16, "ada"}, /* ide8 */ 941 {91, 6, 18, "ada"}, /* ide9 */ 942 943 {8, 4, 0, "da"}, /* scsi disk0 */ 944 {65, 4, 16, "da"}, /* scsi disk1 */ 945 {66, 4, 32, "da"}, /* scsi disk2 */ 946 {67, 4, 48, "da"}, /* scsi disk3 */ 947 {68, 4, 64, "da"}, /* scsi disk4 */ 948 {69, 4, 80, "da"}, /* scsi disk5 */ 949 {70, 4, 96, "da"}, /* scsi disk6 */ 950 {71, 4, 112, "da"}, /* scsi disk7 */ 951 {128, 4, 128, "da"}, /* scsi disk8 */ 952 {129, 4, 144, "da"}, /* scsi disk9 */ 953 {130, 4, 160, "da"}, /* scsi disk10 */ 954 {131, 4, 176, "da"}, /* scsi disk11 */ 955 {132, 4, 192, "da"}, /* scsi disk12 */ 956 {133, 4, 208, "da"}, /* scsi disk13 */ 957 {134, 4, 224, "da"}, /* scsi disk14 */ 958 {135, 4, 240, "da"}, /* scsi disk15 */ 959 960 {202, 4, 0, "xbd"}, /* xbd */ 961 962 {0, 0, 0, NULL}, 963 }; 964 int major = vdevice >> 8; 965 int minor = vdevice & 0xff; 966 int i; 967 968 if (vdevice & (1 << 28)) { 969 *unit = (vdevice & ((1 << 28) - 1)) >> 8; 970 *name = "xbd"; 971 return; 972 } 973 974 for (i = 0; info[i].major; i++) { 975 if (info[i].major == major) { 976 *unit = info[i].base + (minor >> info[i].shift); 977 *name = info[i].name; 978 return; 979 } 980 } 981 982 *unit = minor >> 4; 983 *name = "xbd"; 984 } 985 986 int 987 xbd_instance_create(struct xbd_softc *sc, blkif_sector_t sectors, 988 int vdevice, uint16_t vdisk_info, unsigned long sector_size) 989 { 990 char features[80]; 991 int unit, error = 0; 992 const char *name; 993 994 xbd_vdevice_to_unit(vdevice, &unit, &name); 995 996 sc->xbd_unit = unit; 997 998 if (strcmp(name, "xbd") != 0) 999 device_printf(sc->xbd_dev, "attaching as %s%d\n", name, unit); 1000 1001 if (xbd_feature_string(sc, features, sizeof(features)) > 0) { 1002 device_printf(sc->xbd_dev, "features: %s\n", 1003 features); 1004 } 1005 1006 sc->xbd_disk = disk_alloc(); 1007 sc->xbd_disk->d_unit = sc->xbd_unit; 1008 sc->xbd_disk->d_open = xbd_open; 1009 sc->xbd_disk->d_close = xbd_close; 1010 sc->xbd_disk->d_ioctl = xbd_ioctl; 1011 sc->xbd_disk->d_strategy = xbd_strategy; 1012 sc->xbd_disk->d_dump = xbd_dump; 1013 sc->xbd_disk->d_name = name; 1014 sc->xbd_disk->d_drv1 = sc; 1015 sc->xbd_disk->d_sectorsize = sector_size; 1016 1017 sc->xbd_disk->d_mediasize = sectors * sector_size; 1018 sc->xbd_disk->d_maxsize = sc->xbd_max_request_size; 1019 sc->xbd_disk->d_flags = DISKFLAG_UNMAPPED_BIO; 1020 if ((sc->xbd_flags & (XBDF_FLUSH|XBDF_BARRIER)) != 0) { 1021 sc->xbd_disk->d_flags |= DISKFLAG_CANFLUSHCACHE; 1022 device_printf(sc->xbd_dev, 1023 "synchronize cache commands enabled.\n"); 1024 } 1025 disk_create(sc->xbd_disk, DISK_VERSION); 1026 1027 return error; 1028 } 1029 1030 static void 1031 xbd_free(struct xbd_softc *sc) 1032 { 1033 int i; 1034 1035 /* Prevent new requests being issued until we fix things up. */ 1036 mtx_lock(&sc->xbd_io_lock); 1037 sc->xbd_state = XBD_STATE_DISCONNECTED; 1038 mtx_unlock(&sc->xbd_io_lock); 1039 1040 /* Free resources associated with old device channel. */ 1041 xbd_free_ring(sc); 1042 if (sc->xbd_shadow) { 1043 1044 for (i = 0; i < sc->xbd_max_requests; i++) { 1045 struct xbd_command *cm; 1046 1047 cm = &sc->xbd_shadow[i]; 1048 if (cm->cm_sg_refs != NULL) { 1049 free(cm->cm_sg_refs, M_XENBLOCKFRONT); 1050 cm->cm_sg_refs = NULL; 1051 } 1052 1053 if (cm->cm_indirectionpages != NULL) { 1054 gnttab_end_foreign_access_references( 1055 sc->xbd_max_request_indirectpages, 1056 &cm->cm_indirectionrefs[0]); 1057 contigfree(cm->cm_indirectionpages, PAGE_SIZE * 1058 sc->xbd_max_request_indirectpages, 1059 M_XENBLOCKFRONT); 1060 cm->cm_indirectionpages = NULL; 1061 } 1062 1063 bus_dmamap_destroy(sc->xbd_io_dmat, cm->cm_map); 1064 } 1065 free(sc->xbd_shadow, M_XENBLOCKFRONT); 1066 sc->xbd_shadow = NULL; 1067 1068 bus_dma_tag_destroy(sc->xbd_io_dmat); 1069 1070 xbd_initq_cm(sc, XBD_Q_FREE); 1071 xbd_initq_cm(sc, XBD_Q_READY); 1072 xbd_initq_cm(sc, XBD_Q_COMPLETE); 1073 } 1074 1075 xen_intr_unbind(&sc->xen_intr_handle); 1076 1077 } 1078 1079 /*--------------------------- State Change Handlers --------------------------*/ 1080 static void 1081 xbd_initialize(struct xbd_softc *sc) 1082 { 1083 const char *otherend_path; 1084 const char *node_path; 1085 uint32_t max_ring_page_order; 1086 int error; 1087 1088 if (xenbus_get_state(sc->xbd_dev) != XenbusStateInitialising) { 1089 /* Initialization has already been performed. */ 1090 return; 1091 } 1092 1093 /* 1094 * Protocol defaults valid even if negotiation for a 1095 * setting fails. 1096 */ 1097 max_ring_page_order = 0; 1098 sc->xbd_ring_pages = 1; 1099 1100 /* 1101 * Protocol negotiation. 1102 * 1103 * \note xs_gather() returns on the first encountered error, so 1104 * we must use independant calls in order to guarantee 1105 * we don't miss information in a sparsly populated back-end 1106 * tree. 1107 * 1108 * \note xs_scanf() does not update variables for unmatched 1109 * fields. 1110 */ 1111 otherend_path = xenbus_get_otherend_path(sc->xbd_dev); 1112 node_path = xenbus_get_node(sc->xbd_dev); 1113 1114 /* Support both backend schemes for relaying ring page limits. */ 1115 (void)xs_scanf(XST_NIL, otherend_path, 1116 "max-ring-page-order", NULL, "%" PRIu32, 1117 &max_ring_page_order); 1118 sc->xbd_ring_pages = 1 << max_ring_page_order; 1119 (void)xs_scanf(XST_NIL, otherend_path, 1120 "max-ring-pages", NULL, "%" PRIu32, 1121 &sc->xbd_ring_pages); 1122 if (sc->xbd_ring_pages < 1) 1123 sc->xbd_ring_pages = 1; 1124 1125 if (sc->xbd_ring_pages > XBD_MAX_RING_PAGES) { 1126 device_printf(sc->xbd_dev, 1127 "Back-end specified ring-pages of %u " 1128 "limited to front-end limit of %u.\n", 1129 sc->xbd_ring_pages, XBD_MAX_RING_PAGES); 1130 sc->xbd_ring_pages = XBD_MAX_RING_PAGES; 1131 } 1132 1133 if (powerof2(sc->xbd_ring_pages) == 0) { 1134 uint32_t new_page_limit; 1135 1136 new_page_limit = 0x01 << (fls(sc->xbd_ring_pages) - 1); 1137 device_printf(sc->xbd_dev, 1138 "Back-end specified ring-pages of %u " 1139 "is not a power of 2. Limited to %u.\n", 1140 sc->xbd_ring_pages, new_page_limit); 1141 sc->xbd_ring_pages = new_page_limit; 1142 } 1143 1144 sc->xbd_max_requests = 1145 BLKIF_MAX_RING_REQUESTS(sc->xbd_ring_pages * PAGE_SIZE); 1146 if (sc->xbd_max_requests > XBD_MAX_REQUESTS) { 1147 device_printf(sc->xbd_dev, 1148 "Back-end specified max_requests of %u " 1149 "limited to front-end limit of %zu.\n", 1150 sc->xbd_max_requests, XBD_MAX_REQUESTS); 1151 sc->xbd_max_requests = XBD_MAX_REQUESTS; 1152 } 1153 1154 if (xbd_alloc_ring(sc) != 0) 1155 return; 1156 1157 /* Support both backend schemes for relaying ring page limits. */ 1158 if (sc->xbd_ring_pages > 1) { 1159 error = xs_printf(XST_NIL, node_path, 1160 "num-ring-pages","%u", 1161 sc->xbd_ring_pages); 1162 if (error) { 1163 xenbus_dev_fatal(sc->xbd_dev, error, 1164 "writing %s/num-ring-pages", 1165 node_path); 1166 return; 1167 } 1168 1169 error = xs_printf(XST_NIL, node_path, 1170 "ring-page-order", "%u", 1171 fls(sc->xbd_ring_pages) - 1); 1172 if (error) { 1173 xenbus_dev_fatal(sc->xbd_dev, error, 1174 "writing %s/ring-page-order", 1175 node_path); 1176 return; 1177 } 1178 } 1179 1180 error = xs_printf(XST_NIL, node_path, "event-channel", 1181 "%u", xen_intr_port(sc->xen_intr_handle)); 1182 if (error) { 1183 xenbus_dev_fatal(sc->xbd_dev, error, 1184 "writing %s/event-channel", 1185 node_path); 1186 return; 1187 } 1188 1189 error = xs_printf(XST_NIL, node_path, "protocol", 1190 "%s", XEN_IO_PROTO_ABI_NATIVE); 1191 if (error) { 1192 xenbus_dev_fatal(sc->xbd_dev, error, 1193 "writing %s/protocol", 1194 node_path); 1195 return; 1196 } 1197 1198 xenbus_set_state(sc->xbd_dev, XenbusStateInitialised); 1199 } 1200 1201 /* 1202 * Invoked when the backend is finally 'ready' (and has published 1203 * the details about the physical device - #sectors, size, etc). 1204 */ 1205 static void 1206 xbd_connect(struct xbd_softc *sc) 1207 { 1208 device_t dev = sc->xbd_dev; 1209 unsigned long sectors, sector_size; 1210 unsigned int binfo; 1211 int err, feature_barrier, feature_flush; 1212 int i, j; 1213 1214 if (sc->xbd_state == XBD_STATE_CONNECTED || 1215 sc->xbd_state == XBD_STATE_SUSPENDED) 1216 return; 1217 1218 DPRINTK("blkfront.c:connect:%s.\n", xenbus_get_otherend_path(dev)); 1219 1220 err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev), 1221 "sectors", "%lu", §ors, 1222 "info", "%u", &binfo, 1223 "sector-size", "%lu", §or_size, 1224 NULL); 1225 if (err) { 1226 xenbus_dev_fatal(dev, err, 1227 "reading backend fields at %s", 1228 xenbus_get_otherend_path(dev)); 1229 return; 1230 } 1231 err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev), 1232 "feature-barrier", "%lu", &feature_barrier, 1233 NULL); 1234 if (err == 0 && feature_barrier != 0) 1235 sc->xbd_flags |= XBDF_BARRIER; 1236 1237 err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev), 1238 "feature-flush-cache", "%lu", &feature_flush, 1239 NULL); 1240 if (err == 0 && feature_flush != 0) 1241 sc->xbd_flags |= XBDF_FLUSH; 1242 1243 err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev), 1244 "feature-max-indirect-segments", "%" PRIu32, 1245 &sc->xbd_max_request_segments, NULL); 1246 if ((err != 0) || (xbd_enable_indirect == 0)) 1247 sc->xbd_max_request_segments = 0; 1248 if (sc->xbd_max_request_segments > XBD_MAX_INDIRECT_SEGMENTS) 1249 sc->xbd_max_request_segments = XBD_MAX_INDIRECT_SEGMENTS; 1250 if (sc->xbd_max_request_segments > XBD_SIZE_TO_SEGS(MAXPHYS)) 1251 sc->xbd_max_request_segments = XBD_SIZE_TO_SEGS(MAXPHYS); 1252 sc->xbd_max_request_indirectpages = 1253 XBD_INDIRECT_SEGS_TO_PAGES(sc->xbd_max_request_segments); 1254 if (sc->xbd_max_request_segments < BLKIF_MAX_SEGMENTS_PER_REQUEST) 1255 sc->xbd_max_request_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST; 1256 sc->xbd_max_request_size = 1257 XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments); 1258 1259 /* Allocate datastructures based on negotiated values. */ 1260 err = bus_dma_tag_create( 1261 bus_get_dma_tag(sc->xbd_dev), /* parent */ 1262 512, PAGE_SIZE, /* algnmnt, boundary */ 1263 BUS_SPACE_MAXADDR, /* lowaddr */ 1264 BUS_SPACE_MAXADDR, /* highaddr */ 1265 NULL, NULL, /* filter, filterarg */ 1266 sc->xbd_max_request_size, 1267 sc->xbd_max_request_segments, 1268 PAGE_SIZE, /* maxsegsize */ 1269 BUS_DMA_ALLOCNOW, /* flags */ 1270 busdma_lock_mutex, /* lockfunc */ 1271 &sc->xbd_io_lock, /* lockarg */ 1272 &sc->xbd_io_dmat); 1273 if (err != 0) { 1274 xenbus_dev_fatal(sc->xbd_dev, err, 1275 "Cannot allocate parent DMA tag\n"); 1276 return; 1277 } 1278 1279 /* Per-transaction data allocation. */ 1280 sc->xbd_shadow = malloc(sizeof(*sc->xbd_shadow) * sc->xbd_max_requests, 1281 M_XENBLOCKFRONT, M_NOWAIT|M_ZERO); 1282 if (sc->xbd_shadow == NULL) { 1283 bus_dma_tag_destroy(sc->xbd_io_dmat); 1284 xenbus_dev_fatal(sc->xbd_dev, ENOMEM, 1285 "Cannot allocate request structures\n"); 1286 return; 1287 } 1288 1289 for (i = 0; i < sc->xbd_max_requests; i++) { 1290 struct xbd_command *cm; 1291 void * indirectpages; 1292 1293 cm = &sc->xbd_shadow[i]; 1294 cm->cm_sg_refs = malloc( 1295 sizeof(grant_ref_t) * sc->xbd_max_request_segments, 1296 M_XENBLOCKFRONT, M_NOWAIT); 1297 if (cm->cm_sg_refs == NULL) 1298 break; 1299 cm->cm_id = i; 1300 cm->cm_flags = XBDCF_INITIALIZER; 1301 cm->cm_sc = sc; 1302 if (bus_dmamap_create(sc->xbd_io_dmat, 0, &cm->cm_map) != 0) 1303 break; 1304 if (sc->xbd_max_request_indirectpages > 0) { 1305 indirectpages = contigmalloc( 1306 PAGE_SIZE * sc->xbd_max_request_indirectpages, 1307 M_XENBLOCKFRONT, M_ZERO, 0, ~0, PAGE_SIZE, 0); 1308 } else { 1309 indirectpages = NULL; 1310 } 1311 for (j = 0; j < sc->xbd_max_request_indirectpages; j++) { 1312 if (gnttab_grant_foreign_access( 1313 xenbus_get_otherend_id(sc->xbd_dev), 1314 (vtophys(indirectpages) >> PAGE_SHIFT) + j, 1315 1 /* grant read-only access */, 1316 &cm->cm_indirectionrefs[j])) 1317 break; 1318 } 1319 if (j < sc->xbd_max_request_indirectpages) 1320 break; 1321 cm->cm_indirectionpages = indirectpages; 1322 xbd_free_command(cm); 1323 } 1324 1325 if (sc->xbd_disk == NULL) { 1326 device_printf(dev, "%juMB <%s> at %s", 1327 (uintmax_t) sectors / (1048576 / sector_size), 1328 device_get_desc(dev), 1329 xenbus_get_node(dev)); 1330 bus_print_child_footer(device_get_parent(dev), dev); 1331 1332 xbd_instance_create(sc, sectors, sc->xbd_vdevice, binfo, 1333 sector_size); 1334 } 1335 1336 (void)xenbus_set_state(dev, XenbusStateConnected); 1337 1338 /* Kick pending requests. */ 1339 mtx_lock(&sc->xbd_io_lock); 1340 sc->xbd_state = XBD_STATE_CONNECTED; 1341 xbd_startio(sc); 1342 sc->xbd_flags |= XBDF_READY; 1343 mtx_unlock(&sc->xbd_io_lock); 1344 } 1345 1346 /** 1347 * Handle the change of state of the backend to Closing. We must delete our 1348 * device-layer structures now, to ensure that writes are flushed through to 1349 * the backend. Once this is done, we can switch to Closed in 1350 * acknowledgement. 1351 */ 1352 static void 1353 xbd_closing(device_t dev) 1354 { 1355 struct xbd_softc *sc = device_get_softc(dev); 1356 1357 xenbus_set_state(dev, XenbusStateClosing); 1358 1359 DPRINTK("xbd_closing: %s removed\n", xenbus_get_node(dev)); 1360 1361 if (sc->xbd_disk != NULL) { 1362 disk_destroy(sc->xbd_disk); 1363 sc->xbd_disk = NULL; 1364 } 1365 1366 xenbus_set_state(dev, XenbusStateClosed); 1367 } 1368 1369 /*---------------------------- NewBus Entrypoints ----------------------------*/ 1370 static int 1371 xbd_probe(device_t dev) 1372 { 1373 if (strcmp(xenbus_get_type(dev), "vbd") != 0) 1374 return (ENXIO); 1375 1376 if (xen_hvm_domain() && xen_disable_pv_disks != 0) 1377 return (ENXIO); 1378 1379 if (xen_hvm_domain()) { 1380 int error; 1381 char *type; 1382 1383 /* 1384 * When running in an HVM domain, IDE disk emulation is 1385 * disabled early in boot so that native drivers will 1386 * not see emulated hardware. However, CDROM device 1387 * emulation cannot be disabled. 1388 * 1389 * Through use of FreeBSD's vm_guest and xen_hvm_domain() 1390 * APIs, we could modify the native CDROM driver to fail its 1391 * probe when running under Xen. Unfortunatlely, the PV 1392 * CDROM support in XenServer (up through at least version 1393 * 6.2) isn't functional, so we instead rely on the emulated 1394 * CDROM instance, and fail to attach the PV one here in 1395 * the blkfront driver. 1396 */ 1397 error = xs_read(XST_NIL, xenbus_get_node(dev), 1398 "device-type", NULL, (void **) &type); 1399 if (error) 1400 return (ENXIO); 1401 1402 if (strncmp(type, "cdrom", 5) == 0) { 1403 free(type, M_XENSTORE); 1404 return (ENXIO); 1405 } 1406 free(type, M_XENSTORE); 1407 } 1408 1409 device_set_desc(dev, "Virtual Block Device"); 1410 device_quiet(dev); 1411 return (0); 1412 } 1413 1414 /* 1415 * Setup supplies the backend dir, virtual device. We place an event 1416 * channel and shared frame entries. We watch backend to wait if it's 1417 * ok. 1418 */ 1419 static int 1420 xbd_attach(device_t dev) 1421 { 1422 struct xbd_softc *sc; 1423 const char *name; 1424 uint32_t vdevice; 1425 int error; 1426 int i; 1427 int unit; 1428 1429 /* FIXME: Use dynamic device id if this is not set. */ 1430 error = xs_scanf(XST_NIL, xenbus_get_node(dev), 1431 "virtual-device", NULL, "%" PRIu32, &vdevice); 1432 if (error) 1433 error = xs_scanf(XST_NIL, xenbus_get_node(dev), 1434 "virtual-device-ext", NULL, "%" PRIu32, &vdevice); 1435 if (error) { 1436 xenbus_dev_fatal(dev, error, "reading virtual-device"); 1437 device_printf(dev, "Couldn't determine virtual device.\n"); 1438 return (error); 1439 } 1440 1441 xbd_vdevice_to_unit(vdevice, &unit, &name); 1442 if (!strcmp(name, "xbd")) 1443 device_set_unit(dev, unit); 1444 1445 sc = device_get_softc(dev); 1446 mtx_init(&sc->xbd_io_lock, "blkfront i/o lock", NULL, MTX_DEF); 1447 xbd_initqs(sc); 1448 for (i = 0; i < XBD_MAX_RING_PAGES; i++) 1449 sc->xbd_ring_ref[i] = GRANT_REF_INVALID; 1450 1451 sc->xbd_dev = dev; 1452 sc->xbd_vdevice = vdevice; 1453 sc->xbd_state = XBD_STATE_DISCONNECTED; 1454 1455 xbd_setup_sysctl(sc); 1456 1457 /* Wait for backend device to publish its protocol capabilities. */ 1458 xenbus_set_state(dev, XenbusStateInitialising); 1459 1460 return (0); 1461 } 1462 1463 static int 1464 xbd_detach(device_t dev) 1465 { 1466 struct xbd_softc *sc = device_get_softc(dev); 1467 1468 DPRINTK("%s: %s removed\n", __func__, xenbus_get_node(dev)); 1469 1470 xbd_free(sc); 1471 mtx_destroy(&sc->xbd_io_lock); 1472 1473 return 0; 1474 } 1475 1476 static int 1477 xbd_suspend(device_t dev) 1478 { 1479 struct xbd_softc *sc = device_get_softc(dev); 1480 int retval; 1481 int saved_state; 1482 1483 /* Prevent new requests being issued until we fix things up. */ 1484 mtx_lock(&sc->xbd_io_lock); 1485 saved_state = sc->xbd_state; 1486 sc->xbd_state = XBD_STATE_SUSPENDED; 1487 1488 /* Wait for outstanding I/O to drain. */ 1489 retval = 0; 1490 while (xbd_queue_length(sc, XBD_Q_BUSY) != 0) { 1491 if (msleep(&sc->xbd_cm_q[XBD_Q_BUSY], &sc->xbd_io_lock, 1492 PRIBIO, "blkf_susp", 30 * hz) == EWOULDBLOCK) { 1493 retval = EBUSY; 1494 break; 1495 } 1496 } 1497 mtx_unlock(&sc->xbd_io_lock); 1498 1499 if (retval != 0) 1500 sc->xbd_state = saved_state; 1501 1502 return (retval); 1503 } 1504 1505 static int 1506 xbd_resume(device_t dev) 1507 { 1508 struct xbd_softc *sc = device_get_softc(dev); 1509 1510 DPRINTK("xbd_resume: %s\n", xenbus_get_node(dev)); 1511 1512 xbd_free(sc); 1513 xbd_initialize(sc); 1514 return (0); 1515 } 1516 1517 /** 1518 * Callback received when the backend's state changes. 1519 */ 1520 static void 1521 xbd_backend_changed(device_t dev, XenbusState backend_state) 1522 { 1523 struct xbd_softc *sc = device_get_softc(dev); 1524 1525 DPRINTK("backend_state=%d\n", backend_state); 1526 1527 switch (backend_state) { 1528 case XenbusStateUnknown: 1529 case XenbusStateInitialising: 1530 case XenbusStateReconfigured: 1531 case XenbusStateReconfiguring: 1532 case XenbusStateClosed: 1533 break; 1534 1535 case XenbusStateInitWait: 1536 case XenbusStateInitialised: 1537 xbd_initialize(sc); 1538 break; 1539 1540 case XenbusStateConnected: 1541 xbd_initialize(sc); 1542 xbd_connect(sc); 1543 break; 1544 1545 case XenbusStateClosing: 1546 if (sc->xbd_users > 0) 1547 xenbus_dev_error(dev, -EBUSY, 1548 "Device in use; refusing to close"); 1549 else 1550 xbd_closing(dev); 1551 break; 1552 } 1553 } 1554 1555 /*---------------------------- NewBus Registration ---------------------------*/ 1556 static device_method_t xbd_methods[] = { 1557 /* Device interface */ 1558 DEVMETHOD(device_probe, xbd_probe), 1559 DEVMETHOD(device_attach, xbd_attach), 1560 DEVMETHOD(device_detach, xbd_detach), 1561 DEVMETHOD(device_shutdown, bus_generic_shutdown), 1562 DEVMETHOD(device_suspend, xbd_suspend), 1563 DEVMETHOD(device_resume, xbd_resume), 1564 1565 /* Xenbus interface */ 1566 DEVMETHOD(xenbus_otherend_changed, xbd_backend_changed), 1567 1568 { 0, 0 } 1569 }; 1570 1571 static driver_t xbd_driver = { 1572 "xbd", 1573 xbd_methods, 1574 sizeof(struct xbd_softc), 1575 }; 1576 devclass_t xbd_devclass; 1577 1578 DRIVER_MODULE(xbd, xenbusb_front, xbd_driver, xbd_devclass, 0, 0); 1579