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