1 /*- 2 * All rights reserved. 3 * 4 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 5 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 6 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 7 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 8 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 9 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 10 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 11 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 12 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 13 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 14 * SUCH DAMAGE. 15 * 16 */ 17 18 /* 19 * XenoBSD block device driver 20 */ 21 22 #include <sys/cdefs.h> 23 __FBSDID("$FreeBSD$"); 24 25 #include <sys/param.h> 26 #include <sys/systm.h> 27 #include <sys/malloc.h> 28 #include <sys/kernel.h> 29 #include <vm/vm.h> 30 #include <vm/pmap.h> 31 32 #include <sys/bio.h> 33 #include <sys/bus.h> 34 #include <sys/conf.h> 35 #include <sys/module.h> 36 37 #include <machine/bus.h> 38 #include <sys/rman.h> 39 #include <machine/resource.h> 40 #include <machine/intr_machdep.h> 41 #include <machine/vmparam.h> 42 43 #include <machine/xen/hypervisor.h> 44 #include <machine/xen/xen-os.h> 45 #include <machine/xen/xen_intr.h> 46 #include <machine/xen/xenbus.h> 47 #include <machine/xen/evtchn.h> 48 #include <xen/interface/grant_table.h> 49 50 #include <geom/geom_disk.h> 51 #include <machine/xen/xenfunc.h> 52 #include <xen/gnttab.h> 53 54 #include <dev/xen/blkfront/block.h> 55 56 #define ASSERT(S) KASSERT(S, (#S)) 57 /* prototypes */ 58 struct xb_softc; 59 static void xb_startio(struct xb_softc *sc); 60 static void connect(struct blkfront_info *); 61 static void blkfront_closing(struct xenbus_device *); 62 static int blkfront_remove(struct xenbus_device *); 63 static int talk_to_backend(struct xenbus_device *, struct blkfront_info *); 64 static int setup_blkring(struct xenbus_device *, struct blkfront_info *); 65 static void blkif_int(void *); 66 #if 0 67 static void blkif_restart_queue(void *arg); 68 #endif 69 static void blkif_recover(struct blkfront_info *); 70 static void blkif_completion(struct blk_shadow *); 71 static void blkif_free(struct blkfront_info *, int); 72 73 #define GRANT_INVALID_REF 0 74 #define BLK_RING_SIZE __RING_SIZE((blkif_sring_t *)0, PAGE_SIZE) 75 76 LIST_HEAD(xb_softc_list_head, xb_softc) xbsl_head; 77 78 /* Control whether runtime update of vbds is enabled. */ 79 #define ENABLE_VBD_UPDATE 0 80 81 #if ENABLE_VBD_UPDATE 82 static void vbd_update(void); 83 #endif 84 85 86 #define BLKIF_STATE_DISCONNECTED 0 87 #define BLKIF_STATE_CONNECTED 1 88 #define BLKIF_STATE_SUSPENDED 2 89 90 #ifdef notyet 91 static char *blkif_state_name[] = { 92 [BLKIF_STATE_DISCONNECTED] = "disconnected", 93 [BLKIF_STATE_CONNECTED] = "connected", 94 [BLKIF_STATE_SUSPENDED] = "closed", 95 }; 96 97 static char * blkif_status_name[] = { 98 [BLKIF_INTERFACE_STATUS_CLOSED] = "closed", 99 [BLKIF_INTERFACE_STATUS_DISCONNECTED] = "disconnected", 100 [BLKIF_INTERFACE_STATUS_CONNECTED] = "connected", 101 [BLKIF_INTERFACE_STATUS_CHANGED] = "changed", 102 }; 103 #endif 104 #define WPRINTK(fmt, args...) printf("[XEN] " fmt, ##args) 105 #if 0 106 #define DPRINTK(fmt, args...) printf("[XEN] %s:%d" fmt ".\n", __FUNCTION__, __LINE__,##args) 107 #else 108 #define DPRINTK(fmt, args...) 109 #endif 110 111 static grant_ref_t gref_head; 112 #define MAXIMUM_OUTSTANDING_BLOCK_REQS \ 113 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE) 114 115 static void kick_pending_request_queues(struct blkfront_info *); 116 static int blkif_open(struct disk *dp); 117 static int blkif_close(struct disk *dp); 118 static int blkif_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td); 119 static int blkif_queue_request(struct bio *bp); 120 static void xb_strategy(struct bio *bp); 121 122 123 124 /* XXX move to xb_vbd.c when VBD update support is added */ 125 #define MAX_VBDS 64 126 127 #define XBD_SECTOR_SIZE 512 /* XXX: assume for now */ 128 #define XBD_SECTOR_SHFT 9 129 130 static struct mtx blkif_io_lock; 131 132 static vm_paddr_t 133 pfn_to_mfn(vm_paddr_t pfn) 134 { 135 return (phystomach(pfn << PAGE_SHIFT) >> PAGE_SHIFT); 136 } 137 138 139 int 140 xlvbd_add(blkif_sector_t capacity, int unit, uint16_t vdisk_info, uint16_t sector_size, 141 struct blkfront_info *info) 142 { 143 struct xb_softc *sc; 144 int error = 0; 145 int unitno = unit - 767; 146 147 sc = (struct xb_softc *)malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO); 148 sc->xb_unit = unitno; 149 sc->xb_info = info; 150 info->sc = sc; 151 152 memset(&sc->xb_disk, 0, sizeof(sc->xb_disk)); 153 sc->xb_disk = disk_alloc(); 154 sc->xb_disk->d_unit = unitno; 155 sc->xb_disk->d_open = blkif_open; 156 sc->xb_disk->d_close = blkif_close; 157 sc->xb_disk->d_ioctl = blkif_ioctl; 158 sc->xb_disk->d_strategy = xb_strategy; 159 sc->xb_disk->d_name = "xbd"; 160 sc->xb_disk->d_drv1 = sc; 161 sc->xb_disk->d_sectorsize = sector_size; 162 163 /* XXX */ 164 sc->xb_disk->d_mediasize = capacity << XBD_SECTOR_SHFT; 165 #if 0 166 sc->xb_disk->d_maxsize = DFLTPHYS; 167 #else /* XXX: xen can't handle large single i/o requests */ 168 sc->xb_disk->d_maxsize = 4096; 169 #endif 170 #ifdef notyet 171 XENPRINTF("attaching device 0x%x unit %d capacity %llu\n", 172 xb_diskinfo[sc->xb_unit].device, sc->xb_unit, 173 sc->xb_disk->d_mediasize); 174 #endif 175 sc->xb_disk->d_flags = 0; 176 disk_create(sc->xb_disk, DISK_VERSION_00); 177 bioq_init(&sc->xb_bioq); 178 179 return error; 180 } 181 182 void 183 xlvbd_del(struct blkfront_info *info) 184 { 185 struct xb_softc *sc; 186 187 sc = info->sc; 188 disk_destroy(sc->xb_disk); 189 } 190 /************************ end VBD support *****************/ 191 192 /* 193 * Read/write routine for a buffer. Finds the proper unit, place it on 194 * the sortq and kick the controller. 195 */ 196 static void 197 xb_strategy(struct bio *bp) 198 { 199 struct xb_softc *sc = (struct xb_softc *)bp->bio_disk->d_drv1; 200 201 /* bogus disk? */ 202 if (sc == NULL) { 203 bp->bio_error = EINVAL; 204 bp->bio_flags |= BIO_ERROR; 205 goto bad; 206 } 207 208 DPRINTK(""); 209 210 /* 211 * Place it in the queue of disk activities for this disk 212 */ 213 mtx_lock(&blkif_io_lock); 214 bioq_disksort(&sc->xb_bioq, bp); 215 216 xb_startio(sc); 217 mtx_unlock(&blkif_io_lock); 218 return; 219 220 bad: 221 /* 222 * Correctly set the bio to indicate a failed tranfer. 223 */ 224 bp->bio_resid = bp->bio_bcount; 225 biodone(bp); 226 return; 227 } 228 229 230 /* Setup supplies the backend dir, virtual device. 231 232 We place an event channel and shared frame entries. 233 We watch backend to wait if it's ok. */ 234 static int blkfront_probe(struct xenbus_device *dev, 235 const struct xenbus_device_id *id) 236 { 237 int err, vdevice, i; 238 struct blkfront_info *info; 239 240 /* FIXME: Use dynamic device id if this is not set. */ 241 err = xenbus_scanf(XBT_NIL, dev->nodename, 242 "virtual-device", "%i", &vdevice); 243 if (err != 1) { 244 xenbus_dev_fatal(dev, err, "reading virtual-device"); 245 printf("couldn't find virtual device"); 246 return (err); 247 } 248 249 info = malloc(sizeof(*info), M_DEVBUF, M_NOWAIT|M_ZERO); 250 if (info == NULL) { 251 xenbus_dev_fatal(dev, ENOMEM, "allocating info structure"); 252 return ENOMEM; 253 } 254 255 /* 256 * XXX debug only 257 */ 258 for (i = 0; i < sizeof(*info); i++) 259 if (((uint8_t *)info)[i] != 0) 260 panic("non-null memory"); 261 262 info->shadow_free = 0; 263 info->xbdev = dev; 264 info->vdevice = vdevice; 265 info->connected = BLKIF_STATE_DISCONNECTED; 266 267 /* work queue needed ? */ 268 for (i = 0; i < BLK_RING_SIZE; i++) 269 info->shadow[i].req.id = i+1; 270 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff; 271 272 /* Front end dir is a number, which is used as the id. */ 273 info->handle = strtoul(strrchr(dev->nodename,'/')+1, NULL, 0); 274 dev->dev_driver_data = info; 275 276 err = talk_to_backend(dev, info); 277 if (err) { 278 free(info, M_DEVBUF); 279 dev->dev_driver_data = NULL; 280 return err; 281 } 282 283 return 0; 284 } 285 286 287 static int blkfront_resume(struct xenbus_device *dev) 288 { 289 struct blkfront_info *info = dev->dev_driver_data; 290 int err; 291 292 DPRINTK("blkfront_resume: %s\n", dev->nodename); 293 294 blkif_free(info, 1); 295 296 err = talk_to_backend(dev, info); 297 if (!err) 298 blkif_recover(info); 299 300 return err; 301 } 302 303 /* Common code used when first setting up, and when resuming. */ 304 static int talk_to_backend(struct xenbus_device *dev, 305 struct blkfront_info *info) 306 { 307 const char *message = NULL; 308 struct xenbus_transaction xbt; 309 int err; 310 311 /* Create shared ring, alloc event channel. */ 312 err = setup_blkring(dev, info); 313 if (err) 314 goto out; 315 316 again: 317 err = xenbus_transaction_start(&xbt); 318 if (err) { 319 xenbus_dev_fatal(dev, err, "starting transaction"); 320 goto destroy_blkring; 321 } 322 323 err = xenbus_printf(xbt, dev->nodename, 324 "ring-ref","%u", info->ring_ref); 325 if (err) { 326 message = "writing ring-ref"; 327 goto abort_transaction; 328 } 329 err = xenbus_printf(xbt, dev->nodename, 330 "event-channel", "%u", irq_to_evtchn_port(info->irq)); 331 if (err) { 332 message = "writing event-channel"; 333 goto abort_transaction; 334 } 335 336 err = xenbus_transaction_end(xbt, 0); 337 if (err) { 338 if (err == -EAGAIN) 339 goto again; 340 xenbus_dev_fatal(dev, err, "completing transaction"); 341 goto destroy_blkring; 342 } 343 xenbus_switch_state(dev, XenbusStateInitialised); 344 345 return 0; 346 347 abort_transaction: 348 xenbus_transaction_end(xbt, 1); 349 if (message) 350 xenbus_dev_fatal(dev, err, "%s", message); 351 destroy_blkring: 352 blkif_free(info, 0); 353 out: 354 return err; 355 } 356 357 static int 358 setup_blkring(struct xenbus_device *dev, struct blkfront_info *info) 359 { 360 blkif_sring_t *sring; 361 int err; 362 363 info->ring_ref = GRANT_INVALID_REF; 364 365 sring = (blkif_sring_t *)malloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT|M_ZERO); 366 if (sring == NULL) { 367 xenbus_dev_fatal(dev, ENOMEM, "allocating shared ring"); 368 return ENOMEM; 369 } 370 SHARED_RING_INIT(sring); 371 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE); 372 373 err = xenbus_grant_ring(dev, (vtomach(info->ring.sring) >> PAGE_SHIFT)); 374 if (err < 0) { 375 free(sring, M_DEVBUF); 376 info->ring.sring = NULL; 377 goto fail; 378 } 379 info->ring_ref = err; 380 381 err = bind_listening_port_to_irqhandler(dev->otherend_id, 382 "xbd", (driver_intr_t *)blkif_int, info, 383 INTR_TYPE_BIO | INTR_MPSAFE, NULL); 384 if (err <= 0) { 385 xenbus_dev_fatal(dev, err, 386 "bind_evtchn_to_irqhandler failed"); 387 goto fail; 388 } 389 info->irq = err; 390 391 return 0; 392 fail: 393 blkif_free(info, 0); 394 return err; 395 } 396 397 398 /** 399 * Callback received when the backend's state changes. 400 */ 401 static void backend_changed(struct xenbus_device *dev, 402 XenbusState backend_state) 403 { 404 struct blkfront_info *info = dev->dev_driver_data; 405 406 DPRINTK("blkfront:backend_changed.\n"); 407 408 switch (backend_state) { 409 case XenbusStateUnknown: 410 case XenbusStateInitialising: 411 case XenbusStateInitWait: 412 case XenbusStateInitialised: 413 case XenbusStateClosed: 414 break; 415 416 case XenbusStateConnected: 417 connect(info); 418 break; 419 420 case XenbusStateClosing: 421 if (info->users > 0) 422 xenbus_dev_error(dev, -EBUSY, 423 "Device in use; refusing to close"); 424 else 425 blkfront_closing(dev); 426 #ifdef notyet 427 bd = bdget(info->dev); 428 if (bd == NULL) 429 xenbus_dev_fatal(dev, -ENODEV, "bdget failed"); 430 431 down(&bd->bd_sem); 432 if (info->users > 0) 433 xenbus_dev_error(dev, -EBUSY, 434 "Device in use; refusing to close"); 435 else 436 blkfront_closing(dev); 437 up(&bd->bd_sem); 438 bdput(bd); 439 #endif 440 } 441 } 442 443 /* 444 ** Invoked when the backend is finally 'ready' (and has told produced 445 ** the details about the physical device - #sectors, size, etc). 446 */ 447 static void 448 connect(struct blkfront_info *info) 449 { 450 unsigned long sectors, sector_size; 451 unsigned int binfo; 452 int err; 453 454 if( (info->connected == BLKIF_STATE_CONNECTED) || 455 (info->connected == BLKIF_STATE_SUSPENDED) ) 456 return; 457 458 DPRINTK("blkfront.c:connect:%s.\n", info->xbdev->otherend); 459 460 err = xenbus_gather(XBT_NIL, info->xbdev->otherend, 461 "sectors", "%lu", §ors, 462 "info", "%u", &binfo, 463 "sector-size", "%lu", §or_size, 464 NULL); 465 if (err) { 466 xenbus_dev_fatal(info->xbdev, err, 467 "reading backend fields at %s", 468 info->xbdev->otherend); 469 return; 470 } 471 err = xenbus_gather(XBT_NIL, info->xbdev->otherend, 472 "feature-barrier", "%lu", &info->feature_barrier, 473 NULL); 474 if (err) 475 info->feature_barrier = 0; 476 477 xlvbd_add(sectors, info->vdevice, binfo, sector_size, info); 478 479 (void)xenbus_switch_state(info->xbdev, XenbusStateConnected); 480 481 /* Kick pending requests. */ 482 mtx_lock(&blkif_io_lock); 483 info->connected = BLKIF_STATE_CONNECTED; 484 kick_pending_request_queues(info); 485 mtx_unlock(&blkif_io_lock); 486 info->is_ready = 1; 487 488 #if 0 489 add_disk(info->gd); 490 #endif 491 } 492 493 /** 494 * Handle the change of state of the backend to Closing. We must delete our 495 * device-layer structures now, to ensure that writes are flushed through to 496 * the backend. Once is this done, we can switch to Closed in 497 * acknowledgement. 498 */ 499 static void blkfront_closing(struct xenbus_device *dev) 500 { 501 struct blkfront_info *info = dev->dev_driver_data; 502 503 DPRINTK("blkfront_closing: %s removed\n", dev->nodename); 504 505 if (info->mi) { 506 DPRINTK("Calling xlvbd_del\n"); 507 xlvbd_del(info); 508 info->mi = NULL; 509 } 510 511 xenbus_switch_state(dev, XenbusStateClosed); 512 } 513 514 515 static int blkfront_remove(struct xenbus_device *dev) 516 { 517 struct blkfront_info *info = dev->dev_driver_data; 518 519 DPRINTK("blkfront_remove: %s removed\n", dev->nodename); 520 521 blkif_free(info, 0); 522 523 free(info, M_DEVBUF); 524 525 return 0; 526 } 527 528 529 static inline int 530 GET_ID_FROM_FREELIST(struct blkfront_info *info) 531 { 532 unsigned long nfree = info->shadow_free; 533 534 KASSERT(nfree <= BLK_RING_SIZE, ("free %lu > RING_SIZE", nfree)); 535 info->shadow_free = info->shadow[nfree].req.id; 536 info->shadow[nfree].req.id = 0x0fffffee; /* debug */ 537 return nfree; 538 } 539 540 static inline void 541 ADD_ID_TO_FREELIST(struct blkfront_info *info, unsigned long id) 542 { 543 info->shadow[id].req.id = info->shadow_free; 544 info->shadow[id].request = 0; 545 info->shadow_free = id; 546 } 547 548 static inline void 549 flush_requests(struct blkfront_info *info) 550 { 551 int notify; 552 553 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify); 554 555 if (notify) 556 notify_remote_via_irq(info->irq); 557 } 558 559 static void 560 kick_pending_request_queues(struct blkfront_info *info) 561 { 562 /* XXX check if we can't simplify */ 563 #if 0 564 if (!RING_FULL(&info->ring)) { 565 /* Re-enable calldowns. */ 566 blk_start_queue(info->rq); 567 /* Kick things off immediately. */ 568 do_blkif_request(info->rq); 569 } 570 #endif 571 if (!RING_FULL(&info->ring)) { 572 #if 0 573 sc = LIST_FIRST(&xbsl_head); 574 LIST_REMOVE(sc, entry); 575 /* Re-enable calldowns. */ 576 blk_start_queue(di->rq); 577 #endif 578 /* Kick things off immediately. */ 579 xb_startio(info->sc); 580 } 581 } 582 583 #if 0 584 /* XXX */ 585 static void blkif_restart_queue(void *arg) 586 { 587 struct blkfront_info *info = (struct blkfront_info *)arg; 588 589 mtx_lock(&blkif_io_lock); 590 kick_pending_request_queues(info); 591 mtx_unlock(&blkif_io_lock); 592 } 593 #endif 594 595 static void blkif_restart_queue_callback(void *arg) 596 { 597 #if 0 598 struct blkfront_info *info = (struct blkfront_info *)arg; 599 /* XXX BSD equiv ? */ 600 601 schedule_work(&info->work); 602 #endif 603 } 604 605 static int 606 blkif_open(struct disk *dp) 607 { 608 struct xb_softc *sc = (struct xb_softc *)dp->d_drv1; 609 610 if (sc == NULL) { 611 printk("xb%d: not found", sc->xb_unit); 612 return (ENXIO); 613 } 614 615 sc->xb_flags |= XB_OPEN; 616 sc->xb_info->users++; 617 return (0); 618 } 619 620 static int 621 blkif_close(struct disk *dp) 622 { 623 struct xb_softc *sc = (struct xb_softc *)dp->d_drv1; 624 625 if (sc == NULL) 626 return (ENXIO); 627 sc->xb_flags &= ~XB_OPEN; 628 if (--(sc->xb_info->users) == 0) { 629 /* Check whether we have been instructed to close. We will 630 have ignored this request initially, as the device was 631 still mounted. */ 632 struct xenbus_device * dev = sc->xb_info->xbdev; 633 XenbusState state = xenbus_read_driver_state(dev->otherend); 634 635 if (state == XenbusStateClosing) 636 blkfront_closing(dev); 637 } 638 return (0); 639 } 640 641 static int 642 blkif_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td) 643 { 644 struct xb_softc *sc = (struct xb_softc *)dp->d_drv1; 645 646 if (sc == NULL) 647 return (ENXIO); 648 649 return (ENOTTY); 650 } 651 652 653 /* 654 * blkif_queue_request 655 * 656 * request block io 657 * 658 * id: for guest use only. 659 * operation: BLKIF_OP_{READ,WRITE,PROBE} 660 * buffer: buffer to read/write into. this should be a 661 * virtual address in the guest os. 662 */ 663 static int blkif_queue_request(struct bio *bp) 664 { 665 caddr_t alignbuf; 666 vm_paddr_t buffer_ma; 667 blkif_request_t *ring_req; 668 unsigned long id; 669 uint64_t fsect, lsect; 670 struct xb_softc *sc = (struct xb_softc *)bp->bio_disk->d_drv1; 671 struct blkfront_info *info = sc->xb_info; 672 int ref; 673 674 if (unlikely(sc->xb_info->connected != BLKIF_STATE_CONNECTED)) 675 return 1; 676 677 if (gnttab_alloc_grant_references( 678 BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) { 679 gnttab_request_free_callback( 680 &info->callback, 681 blkif_restart_queue_callback, 682 info, 683 BLKIF_MAX_SEGMENTS_PER_REQUEST); 684 return 1; 685 } 686 687 /* Check if the buffer is properly aligned */ 688 if ((vm_offset_t)bp->bio_data & PAGE_MASK) { 689 int align = (bp->bio_bcount < PAGE_SIZE/2) ? XBD_SECTOR_SIZE : 690 PAGE_SIZE; 691 caddr_t newbuf = malloc(bp->bio_bcount + align, M_DEVBUF, 692 M_NOWAIT); 693 694 alignbuf = (char *)roundup2((u_long)newbuf, align); 695 696 /* save a copy of the current buffer */ 697 bp->bio_driver1 = newbuf; 698 bp->bio_driver2 = alignbuf; 699 700 /* Copy the data for a write */ 701 if (bp->bio_cmd == BIO_WRITE) 702 bcopy(bp->bio_data, alignbuf, bp->bio_bcount); 703 } else 704 alignbuf = bp->bio_data; 705 706 /* Fill out a communications ring structure. */ 707 ring_req = RING_GET_REQUEST(&info->ring, 708 info->ring.req_prod_pvt); 709 id = GET_ID_FROM_FREELIST(info); 710 info->shadow[id].request = (unsigned long)bp; 711 712 ring_req->id = id; 713 ring_req->operation = (bp->bio_cmd == BIO_READ) ? BLKIF_OP_READ : 714 BLKIF_OP_WRITE; 715 716 ring_req->sector_number= (blkif_sector_t)bp->bio_pblkno; 717 ring_req->handle = (blkif_vdev_t)(uintptr_t)sc->xb_disk; 718 719 ring_req->nr_segments = 0; /* XXX not doing scatter/gather since buffer 720 * chaining is not supported. 721 */ 722 723 buffer_ma = vtomach(alignbuf); 724 fsect = (buffer_ma & PAGE_MASK) >> XBD_SECTOR_SHFT; 725 lsect = fsect + (bp->bio_bcount >> XBD_SECTOR_SHFT) - 1; 726 /* install a grant reference. */ 727 ref = gnttab_claim_grant_reference(&gref_head); 728 KASSERT( ref != -ENOSPC, ("grant_reference failed") ); 729 730 gnttab_grant_foreign_access_ref( 731 ref, 732 info->xbdev->otherend_id, 733 buffer_ma >> PAGE_SHIFT, 734 ring_req->operation & 1 ); /* ??? */ 735 info->shadow[id].frame[ring_req->nr_segments] = 736 buffer_ma >> PAGE_SHIFT; 737 738 ring_req->seg[ring_req->nr_segments] = 739 (struct blkif_request_segment) { 740 .gref = ref, 741 .first_sect = fsect, 742 .last_sect = lsect }; 743 744 ring_req->nr_segments++; 745 KASSERT((buffer_ma & (XBD_SECTOR_SIZE-1)) == 0, 746 ("XEN buffer must be sector aligned")); 747 KASSERT(lsect <= 7, 748 ("XEN disk driver data cannot cross a page boundary")); 749 750 buffer_ma &= ~PAGE_MASK; 751 752 info->ring.req_prod_pvt++; 753 754 /* Keep a private copy so we can reissue requests when recovering. */ 755 info->shadow[id].req = *ring_req; 756 757 gnttab_free_grant_references(gref_head); 758 759 return 0; 760 } 761 762 763 764 /* 765 * Dequeue buffers and place them in the shared communication ring. 766 * Return when no more requests can be accepted or all buffers have 767 * been queued. 768 * 769 * Signal XEN once the ring has been filled out. 770 */ 771 static void 772 xb_startio(struct xb_softc *sc) 773 { 774 struct bio *bp; 775 int queued = 0; 776 struct blkfront_info *info = sc->xb_info; 777 DPRINTK(""); 778 779 mtx_assert(&blkif_io_lock, MA_OWNED); 780 781 while ((bp = bioq_takefirst(&sc->xb_bioq)) != NULL) { 782 783 if (RING_FULL(&info->ring)) 784 goto wait; 785 786 if (blkif_queue_request(bp)) { 787 wait: 788 bioq_insert_head(&sc->xb_bioq, bp); 789 break; 790 } 791 queued++; 792 } 793 794 if (queued != 0) 795 flush_requests(sc->xb_info); 796 } 797 798 static void 799 blkif_int(void *xsc) 800 { 801 struct xb_softc *sc = NULL; 802 struct bio *bp; 803 blkif_response_t *bret; 804 RING_IDX i, rp; 805 struct blkfront_info *info = xsc; 806 DPRINTK(""); 807 808 TRACE_ENTER; 809 810 mtx_lock(&blkif_io_lock); 811 812 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) { 813 mtx_unlock(&blkif_io_lock); 814 return; 815 } 816 817 again: 818 rp = info->ring.sring->rsp_prod; 819 rmb(); /* Ensure we see queued responses up to 'rp'. */ 820 821 for (i = info->ring.rsp_cons; i != rp; i++) { 822 unsigned long id; 823 824 bret = RING_GET_RESPONSE(&info->ring, i); 825 id = bret->id; 826 bp = (struct bio *)info->shadow[id].request; 827 828 blkif_completion(&info->shadow[id]); 829 830 ADD_ID_TO_FREELIST(info, id); 831 832 switch (bret->operation) { 833 case BLKIF_OP_READ: 834 /* had an unaligned buffer that needs to be copied */ 835 if (bp->bio_driver1) 836 bcopy(bp->bio_driver2, bp->bio_data, bp->bio_bcount); 837 /* FALLTHROUGH */ 838 case BLKIF_OP_WRITE: 839 840 /* free the copy buffer */ 841 if (bp->bio_driver1) { 842 free(bp->bio_driver1, M_DEVBUF); 843 bp->bio_driver1 = NULL; 844 } 845 846 if ( unlikely(bret->status != BLKIF_RSP_OKAY) ) { 847 printf("Bad return from blkdev data request: %x\n", 848 bret->status); 849 bp->bio_flags |= BIO_ERROR; 850 } 851 852 sc = (struct xb_softc *)bp->bio_disk->d_drv1; 853 854 if (bp->bio_flags & BIO_ERROR) 855 bp->bio_error = EIO; 856 else 857 bp->bio_resid = 0; 858 859 biodone(bp); 860 break; 861 default: 862 panic("received invalid operation"); 863 break; 864 } 865 } 866 867 info->ring.rsp_cons = i; 868 869 if (i != info->ring.req_prod_pvt) { 870 int more_to_do; 871 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do); 872 if (more_to_do) 873 goto again; 874 } else { 875 info->ring.sring->rsp_event = i + 1; 876 } 877 878 kick_pending_request_queues(info); 879 880 mtx_unlock(&blkif_io_lock); 881 } 882 883 static void 884 blkif_free(struct blkfront_info *info, int suspend) 885 { 886 887 /* Prevent new requests being issued until we fix things up. */ 888 mtx_lock(&blkif_io_lock); 889 info->connected = suspend ? 890 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED; 891 mtx_unlock(&blkif_io_lock); 892 893 /* Free resources associated with old device channel. */ 894 if (info->ring_ref != GRANT_INVALID_REF) { 895 gnttab_end_foreign_access(info->ring_ref, 0, 896 info->ring.sring); 897 info->ring_ref = GRANT_INVALID_REF; 898 info->ring.sring = NULL; 899 } 900 if (info->irq) 901 unbind_from_irqhandler(info->irq, info); 902 info->irq = 0; 903 904 } 905 906 static void 907 blkif_completion(struct blk_shadow *s) 908 { 909 int i; 910 911 for (i = 0; i < s->req.nr_segments; i++) 912 gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL); 913 } 914 915 static void 916 blkif_recover(struct blkfront_info *info) 917 { 918 int i, j; 919 blkif_request_t *req; 920 struct blk_shadow *copy; 921 922 /* Stage 1: Make a safe copy of the shadow state. */ 923 copy = (struct blk_shadow *)malloc(sizeof(info->shadow), M_DEVBUF, M_NOWAIT|M_ZERO); 924 PANIC_IF(copy == NULL); 925 memcpy(copy, info->shadow, sizeof(info->shadow)); 926 927 /* Stage 2: Set up free list. */ 928 memset(&info->shadow, 0, sizeof(info->shadow)); 929 for (i = 0; i < BLK_RING_SIZE; i++) 930 info->shadow[i].req.id = i+1; 931 info->shadow_free = info->ring.req_prod_pvt; 932 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff; 933 934 /* Stage 3: Find pending requests and requeue them. */ 935 for (i = 0; i < BLK_RING_SIZE; i++) { 936 /* Not in use? */ 937 if (copy[i].request == 0) 938 continue; 939 940 /* Grab a request slot and copy shadow state into it. */ 941 req = RING_GET_REQUEST( 942 &info->ring, info->ring.req_prod_pvt); 943 *req = copy[i].req; 944 945 /* We get a new request id, and must reset the shadow state. */ 946 req->id = GET_ID_FROM_FREELIST(info); 947 memcpy(&info->shadow[req->id], ©[i], sizeof(copy[i])); 948 949 /* Rewrite any grant references invalidated by suspend/resume. */ 950 for (j = 0; j < req->nr_segments; j++) 951 gnttab_grant_foreign_access_ref( 952 req->seg[j].gref, 953 info->xbdev->otherend_id, 954 pfn_to_mfn(info->shadow[req->id].frame[j]), 955 0 /* assume not readonly */); 956 957 info->shadow[req->id].req = *req; 958 959 info->ring.req_prod_pvt++; 960 } 961 962 free(copy, M_DEVBUF); 963 964 xenbus_switch_state(info->xbdev, XenbusStateConnected); 965 966 /* Now safe for us to use the shared ring */ 967 mtx_lock(&blkif_io_lock); 968 info->connected = BLKIF_STATE_CONNECTED; 969 mtx_unlock(&blkif_io_lock); 970 971 /* Send off requeued requests */ 972 mtx_lock(&blkif_io_lock); 973 flush_requests(info); 974 975 /* Kick any other new requests queued since we resumed */ 976 kick_pending_request_queues(info); 977 mtx_unlock(&blkif_io_lock); 978 } 979 980 static int 981 blkfront_is_ready(struct xenbus_device *dev) 982 { 983 struct blkfront_info *info = dev->dev_driver_data; 984 985 return info->is_ready; 986 } 987 988 static struct xenbus_device_id blkfront_ids[] = { 989 { "vbd" }, 990 { "" } 991 }; 992 993 994 static struct xenbus_driver blkfront = { 995 .name = "vbd", 996 .ids = blkfront_ids, 997 .probe = blkfront_probe, 998 .remove = blkfront_remove, 999 .resume = blkfront_resume, 1000 .otherend_changed = backend_changed, 1001 .is_ready = blkfront_is_ready, 1002 }; 1003 1004 1005 1006 static void 1007 xenbus_init(void) 1008 { 1009 xenbus_register_frontend(&blkfront); 1010 } 1011 1012 MTX_SYSINIT(ioreq, &blkif_io_lock, "BIO LOCK", MTX_NOWITNESS); /* XXX how does one enroll a lock? */ 1013 SYSINIT(xbdev, SI_SUB_PSEUDO, SI_ORDER_SECOND, xenbus_init, NULL); 1014 1015 1016 /* 1017 * Local variables: 1018 * mode: C 1019 * c-set-style: "BSD" 1020 * c-basic-offset: 8 1021 * tab-width: 4 1022 * indent-tabs-mode: t 1023 * End: 1024 */ 1025