2 * XenBSD block device driver
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
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:
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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>
44 #include <sys/module.h>
45 #include <sys/sysctl.h>
47 #include <machine/bus.h>
49 #include <machine/resource.h>
50 #include <machine/intr_machdep.h>
51 #include <machine/vmparam.h>
52 #include <sys/bus_dma.h>
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>
62 #include <machine/_inttypes.h>
63 #include <machine/xen/xenvar.h>
65 #include <geom/geom_disk.h>
67 #include <dev/xen/blkfront/block.h>
69 #include "xenbus_if.h"
71 /*--------------------------- Forward Declarations ---------------------------*/
72 static void xbd_closing(device_t);
73 static void xbd_startio(struct xbd_softc *sc);
75 /*---------------------------------- Macros ----------------------------------*/
77 #define DPRINTK(fmt, args...) printf("[XEN] %s:%d: " fmt ".\n", __func__, __LINE__, ##args)
79 #define DPRINTK(fmt, args...)
82 #define XBD_SECTOR_SHFT 9
84 /*---------------------------- Global Static Data ----------------------------*/
85 static MALLOC_DEFINE(M_XENBLOCKFRONT, "xbd", "Xen Block Front driver data");
87 /*---------------------------- Command Processing ----------------------------*/
89 xbd_freeze(struct xbd_softc *sc, xbd_flag_t xbd_flag)
91 if (xbd_flag != XBDF_NONE && (sc->xbd_flags & xbd_flag) != 0)
94 sc->xbd_flags |= xbd_flag;
95 sc->xbd_qfrozen_cnt++;
99 xbd_thaw(struct xbd_softc *sc, xbd_flag_t xbd_flag)
101 if (xbd_flag != XBDF_NONE && (sc->xbd_flags & xbd_flag) == 0)
104 if (sc->xbd_qfrozen_cnt == 0)
105 panic("%s: Thaw with flag 0x%x while not frozen.",
108 sc->xbd_flags &= ~xbd_flag;
109 sc->xbd_qfrozen_cnt--;
113 xbd_cm_freeze(struct xbd_softc *sc, struct xbd_command *cm, xbdc_flag_t cm_flag)
115 if ((cm->cm_flags & XBDCF_FROZEN) != 0)
118 cm->cm_flags |= XBDCF_FROZEN|cm_flag;
119 xbd_freeze(sc, XBDF_NONE);
123 xbd_cm_thaw(struct xbd_softc *sc, struct xbd_command *cm)
125 if ((cm->cm_flags & XBDCF_FROZEN) == 0)
128 cm->cm_flags &= ~XBDCF_FROZEN;
129 xbd_thaw(sc, XBDF_NONE);
133 xbd_flush_requests(struct xbd_softc *sc)
137 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&sc->xbd_ring, notify);
140 xen_intr_signal(sc->xen_intr_handle);
144 xbd_free_command(struct xbd_command *cm)
147 KASSERT((cm->cm_flags & XBDCF_Q_MASK) == XBD_Q_NONE,
148 ("Freeing command that is still on queue %d.",
149 cm->cm_flags & XBDCF_Q_MASK));
151 cm->cm_flags = XBDCF_INITIALIZER;
153 cm->cm_complete = NULL;
154 xbd_enqueue_cm(cm, XBD_Q_FREE);
155 xbd_thaw(cm->cm_sc, XBDF_CM_SHORTAGE);
159 xbd_mksegarray(bus_dma_segment_t *segs, int nsegs,
160 grant_ref_t * gref_head, int otherend_id, int readonly,
161 grant_ref_t * sg_ref, blkif_request_segment_t * sg)
163 struct blkif_request_segment *last_block_sg = sg + nsegs;
164 vm_paddr_t buffer_ma;
165 uint64_t fsect, lsect;
168 while (sg < last_block_sg) {
169 buffer_ma = segs->ds_addr;
170 fsect = (buffer_ma & PAGE_MASK) >> XBD_SECTOR_SHFT;
171 lsect = fsect + (segs->ds_len >> XBD_SECTOR_SHFT) - 1;
173 KASSERT(lsect <= 7, ("XEN disk driver data cannot "
174 "cross a page boundary"));
176 /* install a grant reference. */
177 ref = gnttab_claim_grant_reference(gref_head);
180 * GNTTAB_LIST_END == 0xffffffff, but it is private
183 KASSERT(ref != ~0, ("grant_reference failed"));
185 gnttab_grant_foreign_access_ref(
188 buffer_ma >> PAGE_SHIFT,
192 *sg = (struct blkif_request_segment) {
204 xbd_queue_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
206 struct xbd_softc *sc;
207 struct xbd_command *cm;
208 blkif_request_t *ring_req;
215 cm->cm_bp->bio_error = EIO;
217 xbd_free_command(cm);
221 KASSERT(nsegs <= BLKIF_MAX_SEGMENTS_PER_REQUEST,
222 ("Too many segments in a blkfront I/O"));
224 /* Fill out a communications ring structure. */
225 ring_req = RING_GET_REQUEST(&sc->xbd_ring, sc->xbd_ring.req_prod_pvt);
226 sc->xbd_ring.req_prod_pvt++;
227 ring_req->id = cm->cm_id;
228 ring_req->operation = cm->cm_operation;
229 ring_req->sector_number = cm->cm_sector_number;
230 ring_req->handle = (blkif_vdev_t)(uintptr_t)sc->xbd_disk;
231 ring_req->nr_segments = nsegs;
233 xbd_mksegarray(segs, nsegs, &cm->cm_gref_head,
234 xenbus_get_otherend_id(sc->xbd_dev),
235 cm->cm_operation == BLKIF_OP_WRITE,
236 cm->cm_sg_refs, ring_req->seg);
238 if (cm->cm_operation == BLKIF_OP_READ)
239 op = BUS_DMASYNC_PREREAD;
240 else if (cm->cm_operation == BLKIF_OP_WRITE)
241 op = BUS_DMASYNC_PREWRITE;
244 bus_dmamap_sync(sc->xbd_io_dmat, cm->cm_map, op);
246 gnttab_free_grant_references(cm->cm_gref_head);
248 xbd_enqueue_cm(cm, XBD_Q_BUSY);
251 * If bus dma had to asynchronously call us back to dispatch
252 * this command, we are no longer executing in the context of
253 * xbd_startio(). Thus we cannot rely on xbd_startio()'s call to
254 * xbd_flush_requests() to publish this command to the backend
255 * along with any other commands that it could batch.
257 if ((cm->cm_flags & XBDCF_ASYNC_MAPPING) != 0)
258 xbd_flush_requests(sc);
264 xbd_queue_request(struct xbd_softc *sc, struct xbd_command *cm)
268 error = bus_dmamap_load(sc->xbd_io_dmat, cm->cm_map, cm->cm_data,
269 cm->cm_datalen, xbd_queue_cb, cm, 0);
270 if (error == EINPROGRESS) {
272 * Maintain queuing order by freezing the queue. The next
273 * command may not require as many resources as the command
274 * we just attempted to map, so we can't rely on bus dma
275 * blocking for it too.
277 xbd_cm_freeze(sc, cm, XBDCF_ASYNC_MAPPING);
285 xbd_restart_queue_callback(void *arg)
287 struct xbd_softc *sc = arg;
289 mtx_lock(&sc->xbd_io_lock);
291 xbd_thaw(sc, XBDF_GNT_SHORTAGE);
295 mtx_unlock(&sc->xbd_io_lock);
298 static struct xbd_command *
299 xbd_bio_command(struct xbd_softc *sc)
301 struct xbd_command *cm;
304 if (__predict_false(sc->xbd_state != XBD_STATE_CONNECTED))
307 bp = xbd_dequeue_bio(sc);
311 if ((cm = xbd_dequeue_cm(sc, XBD_Q_FREE)) == NULL) {
312 xbd_freeze(sc, XBDF_CM_SHORTAGE);
313 xbd_requeue_bio(sc, bp);
317 if (gnttab_alloc_grant_references(sc->xbd_max_request_segments,
318 &cm->cm_gref_head) != 0) {
319 gnttab_request_free_callback(&sc->xbd_callback,
320 xbd_restart_queue_callback, sc,
321 sc->xbd_max_request_segments);
322 xbd_freeze(sc, XBDF_GNT_SHORTAGE);
323 xbd_requeue_bio(sc, bp);
324 xbd_enqueue_cm(cm, XBD_Q_FREE);
329 cm->cm_data = bp->bio_data;
330 cm->cm_datalen = bp->bio_bcount;
331 cm->cm_sector_number = (blkif_sector_t)bp->bio_pblkno;
333 switch (bp->bio_cmd) {
335 cm->cm_operation = BLKIF_OP_READ;
338 cm->cm_operation = BLKIF_OP_WRITE;
339 if ((bp->bio_flags & BIO_ORDERED) != 0) {
340 if ((sc->xbd_flags & XBDF_BARRIER) != 0) {
341 cm->cm_operation = BLKIF_OP_WRITE_BARRIER;
344 * Single step this command.
346 cm->cm_flags |= XBDCF_Q_FREEZE;
347 if (xbd_queue_length(sc, XBD_Q_BUSY) != 0) {
349 * Wait for in-flight requests to
352 xbd_freeze(sc, XBDF_WAIT_IDLE);
353 xbd_requeue_cm(cm, XBD_Q_READY);
360 if ((sc->xbd_flags & XBDF_FLUSH) != 0)
361 cm->cm_operation = BLKIF_OP_FLUSH_DISKCACHE;
362 else if ((sc->xbd_flags & XBDF_BARRIER) != 0)
363 cm->cm_operation = BLKIF_OP_WRITE_BARRIER;
365 panic("flush request, but no flush support available");
368 panic("unknown bio command %d", bp->bio_cmd);
375 * Dequeue buffers and place them in the shared communication ring.
376 * Return when no more requests can be accepted or all buffers have
379 * Signal XEN once the ring has been filled out.
382 xbd_startio(struct xbd_softc *sc)
384 struct xbd_command *cm;
385 int error, queued = 0;
387 mtx_assert(&sc->xbd_io_lock, MA_OWNED);
389 if (sc->xbd_state != XBD_STATE_CONNECTED)
392 while (!RING_FULL(&sc->xbd_ring)) {
394 if (sc->xbd_qfrozen_cnt != 0)
397 cm = xbd_dequeue_cm(sc, XBD_Q_READY);
400 cm = xbd_bio_command(sc);
405 if ((cm->cm_flags & XBDCF_Q_FREEZE) != 0) {
407 * Single step command. Future work is
408 * held off until this command completes.
410 xbd_cm_freeze(sc, cm, XBDCF_Q_FREEZE);
413 if ((error = xbd_queue_request(sc, cm)) != 0) {
414 printf("xbd_queue_request returned %d\n", error);
421 xbd_flush_requests(sc);
425 xbd_bio_complete(struct xbd_softc *sc, struct xbd_command *cm)
431 if (__predict_false(cm->cm_status != BLKIF_RSP_OKAY)) {
432 disk_err(bp, "disk error" , -1, 0);
433 printf(" status: %x\n", cm->cm_status);
434 bp->bio_flags |= BIO_ERROR;
437 if (bp->bio_flags & BIO_ERROR)
442 xbd_free_command(cm);
449 struct xbd_softc *sc = xsc;
450 struct xbd_command *cm;
451 blkif_response_t *bret;
455 mtx_lock(&sc->xbd_io_lock);
457 if (__predict_false(sc->xbd_state == XBD_STATE_DISCONNECTED)) {
458 mtx_unlock(&sc->xbd_io_lock);
463 rp = sc->xbd_ring.sring->rsp_prod;
464 rmb(); /* Ensure we see queued responses up to 'rp'. */
466 for (i = sc->xbd_ring.rsp_cons; i != rp;) {
467 bret = RING_GET_RESPONSE(&sc->xbd_ring, i);
468 cm = &sc->xbd_shadow[bret->id];
470 xbd_remove_cm(cm, XBD_Q_BUSY);
471 gnttab_end_foreign_access_references(cm->cm_nseg,
475 if (cm->cm_operation == BLKIF_OP_READ)
476 op = BUS_DMASYNC_POSTREAD;
477 else if (cm->cm_operation == BLKIF_OP_WRITE ||
478 cm->cm_operation == BLKIF_OP_WRITE_BARRIER)
479 op = BUS_DMASYNC_POSTWRITE;
482 bus_dmamap_sync(sc->xbd_io_dmat, cm->cm_map, op);
483 bus_dmamap_unload(sc->xbd_io_dmat, cm->cm_map);
486 * Release any hold this command has on future command
492 * Directly call the i/o complete routine to save an
493 * an indirection in the common case.
495 cm->cm_status = bret->status;
497 xbd_bio_complete(sc, cm);
498 else if (cm->cm_complete != NULL)
501 xbd_free_command(cm);
504 sc->xbd_ring.rsp_cons = i;
506 if (i != sc->xbd_ring.req_prod_pvt) {
508 RING_FINAL_CHECK_FOR_RESPONSES(&sc->xbd_ring, more_to_do);
512 sc->xbd_ring.sring->rsp_event = i + 1;
515 if (xbd_queue_length(sc, XBD_Q_BUSY) == 0)
516 xbd_thaw(sc, XBDF_WAIT_IDLE);
520 if (__predict_false(sc->xbd_state == XBD_STATE_SUSPENDED))
521 wakeup(&sc->xbd_cm_q[XBD_Q_BUSY]);
523 mtx_unlock(&sc->xbd_io_lock);
526 /*------------------------------- Dump Support -------------------------------*/
528 * Quiesce the disk writes for a dump file before allowing the next buffer.
531 xbd_quiesce(struct xbd_softc *sc)
535 // While there are outstanding requests
536 while (xbd_queue_length(sc, XBD_Q_BUSY) != 0) {
537 RING_FINAL_CHECK_FOR_RESPONSES(&sc->xbd_ring, mtd);
539 /* Recieved request completions, update queue. */
542 if (xbd_queue_length(sc, XBD_Q_BUSY) != 0) {
544 * Still pending requests, wait for the disk i/o
552 /* Kernel dump function for a paravirtualized disk device */
554 xbd_dump_complete(struct xbd_command *cm)
557 xbd_enqueue_cm(cm, XBD_Q_COMPLETE);
561 xbd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset,
564 struct disk *dp = arg;
565 struct xbd_softc *sc = dp->d_drv1;
566 struct xbd_command *cm;
574 xbd_quiesce(sc); /* All quiet on the western front. */
577 * If this lock is held, then this module is failing, and a
578 * successful kernel dump is highly unlikely anyway.
580 mtx_lock(&sc->xbd_io_lock);
582 /* Split the 64KB block as needed */
583 for (sbp=0; length > 0; sbp++) {
584 cm = xbd_dequeue_cm(sc, XBD_Q_FREE);
586 mtx_unlock(&sc->xbd_io_lock);
587 device_printf(sc->xbd_dev, "dump: no more commands?\n");
591 if (gnttab_alloc_grant_references(sc->xbd_max_request_segments,
592 &cm->cm_gref_head) != 0) {
593 xbd_free_command(cm);
594 mtx_unlock(&sc->xbd_io_lock);
595 device_printf(sc->xbd_dev, "no more grant allocs?\n");
599 chunk = length > sc->xbd_max_request_size ?
600 sc->xbd_max_request_size : length;
601 cm->cm_data = virtual;
602 cm->cm_datalen = chunk;
603 cm->cm_operation = BLKIF_OP_WRITE;
604 cm->cm_sector_number = offset / dp->d_sectorsize;
605 cm->cm_complete = xbd_dump_complete;
607 xbd_enqueue_cm(cm, XBD_Q_READY);
611 virtual = (char *) virtual + chunk;
614 /* Tell DOM0 to do the I/O */
616 mtx_unlock(&sc->xbd_io_lock);
618 /* Poll for the completion. */
619 xbd_quiesce(sc); /* All quite on the eastern front */
621 /* If there were any errors, bail out... */
622 while ((cm = xbd_dequeue_cm(sc, XBD_Q_COMPLETE)) != NULL) {
623 if (cm->cm_status != BLKIF_RSP_OKAY) {
624 device_printf(sc->xbd_dev,
625 "Dump I/O failed at sector %jd\n",
626 cm->cm_sector_number);
629 xbd_free_command(cm);
635 /*----------------------------- Disk Entrypoints -----------------------------*/
637 xbd_open(struct disk *dp)
639 struct xbd_softc *sc = dp->d_drv1;
642 printf("xb%d: not found", sc->xbd_unit);
646 sc->xbd_flags |= XBDF_OPEN;
652 xbd_close(struct disk *dp)
654 struct xbd_softc *sc = dp->d_drv1;
658 sc->xbd_flags &= ~XBDF_OPEN;
659 if (--(sc->xbd_users) == 0) {
661 * Check whether we have been instructed to close. We will
662 * have ignored this request initially, as the device was
665 if (xenbus_get_otherend_state(sc->xbd_dev) ==
667 xbd_closing(sc->xbd_dev);
673 xbd_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
675 struct xbd_softc *sc = dp->d_drv1;
684 * Read/write routine for a buffer. Finds the proper unit, place it on
685 * the sortq and kick the controller.
688 xbd_strategy(struct bio *bp)
690 struct xbd_softc *sc = bp->bio_disk->d_drv1;
694 bp->bio_error = EINVAL;
695 bp->bio_flags |= BIO_ERROR;
696 bp->bio_resid = bp->bio_bcount;
702 * Place it in the queue of disk activities for this disk
704 mtx_lock(&sc->xbd_io_lock);
706 xbd_enqueue_bio(sc, bp);
709 mtx_unlock(&sc->xbd_io_lock);
713 /*------------------------------ Ring Management -----------------------------*/
715 xbd_alloc_ring(struct xbd_softc *sc)
717 blkif_sring_t *sring;
718 uintptr_t sring_page_addr;
722 sring = malloc(sc->xbd_ring_pages * PAGE_SIZE, M_XENBLOCKFRONT,
725 xenbus_dev_fatal(sc->xbd_dev, ENOMEM, "allocating shared ring");
728 SHARED_RING_INIT(sring);
729 FRONT_RING_INIT(&sc->xbd_ring, sring, sc->xbd_ring_pages * PAGE_SIZE);
731 for (i = 0, sring_page_addr = (uintptr_t)sring;
732 i < sc->xbd_ring_pages;
733 i++, sring_page_addr += PAGE_SIZE) {
735 error = xenbus_grant_ring(sc->xbd_dev,
736 (vtomach(sring_page_addr) >> PAGE_SHIFT),
737 &sc->xbd_ring_ref[i]);
739 xenbus_dev_fatal(sc->xbd_dev, error,
740 "granting ring_ref(%d)", i);
744 if (sc->xbd_ring_pages == 1) {
745 error = xs_printf(XST_NIL, xenbus_get_node(sc->xbd_dev),
746 "ring-ref", "%u", sc->xbd_ring_ref[0]);
748 xenbus_dev_fatal(sc->xbd_dev, error,
749 "writing %s/ring-ref",
750 xenbus_get_node(sc->xbd_dev));
754 for (i = 0; i < sc->xbd_ring_pages; i++) {
755 char ring_ref_name[]= "ring_refXX";
757 snprintf(ring_ref_name, sizeof(ring_ref_name),
759 error = xs_printf(XST_NIL, xenbus_get_node(sc->xbd_dev),
760 ring_ref_name, "%u", sc->xbd_ring_ref[i]);
762 xenbus_dev_fatal(sc->xbd_dev, error,
764 xenbus_get_node(sc->xbd_dev),
771 error = xen_intr_alloc_and_bind_local_port(sc->xbd_dev,
772 xenbus_get_otherend_id(sc->xbd_dev), NULL, xbd_int, sc,
773 INTR_TYPE_BIO | INTR_MPSAFE, &sc->xen_intr_handle);
775 xenbus_dev_fatal(sc->xbd_dev, error,
776 "xen_intr_alloc_and_bind_local_port failed");
784 xbd_free_ring(struct xbd_softc *sc)
788 if (sc->xbd_ring.sring == NULL)
791 for (i = 0; i < sc->xbd_ring_pages; i++) {
792 if (sc->xbd_ring_ref[i] != GRANT_REF_INVALID) {
793 gnttab_end_foreign_access_ref(sc->xbd_ring_ref[i]);
794 sc->xbd_ring_ref[i] = GRANT_REF_INVALID;
797 free(sc->xbd_ring.sring, M_XENBLOCKFRONT);
798 sc->xbd_ring.sring = NULL;
801 /*-------------------------- Initialization/Teardown -------------------------*/
803 xbd_feature_string(struct xbd_softc *sc, char *features, size_t len)
808 sbuf_new(&sb, features, len, SBUF_FIXEDLEN);
811 if ((sc->xbd_flags & XBDF_FLUSH) != 0) {
812 sbuf_printf(&sb, "flush");
816 if ((sc->xbd_flags & XBDF_BARRIER) != 0) {
817 if (feature_cnt != 0)
818 sbuf_printf(&sb, ", ");
819 sbuf_printf(&sb, "write_barrier");
823 (void) sbuf_finish(&sb);
824 return (sbuf_len(&sb));
828 xbd_sysctl_features(SYSCTL_HANDLER_ARGS)
831 struct xbd_softc *sc = arg1;
835 error = sysctl_wire_old_buffer(req, 0);
839 len = xbd_feature_string(sc, features, sizeof(features));
841 /* len is -1 on error, which will make the SYSCTL_OUT a no-op. */
842 return (SYSCTL_OUT(req, features, len + 1/*NUL*/));
846 xbd_setup_sysctl(struct xbd_softc *xbd)
848 struct sysctl_ctx_list *sysctl_ctx = NULL;
849 struct sysctl_oid *sysctl_tree = NULL;
850 struct sysctl_oid_list *children;
852 sysctl_ctx = device_get_sysctl_ctx(xbd->xbd_dev);
853 if (sysctl_ctx == NULL)
856 sysctl_tree = device_get_sysctl_tree(xbd->xbd_dev);
857 if (sysctl_tree == NULL)
860 children = SYSCTL_CHILDREN(sysctl_tree);
861 SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO,
862 "max_requests", CTLFLAG_RD, &xbd->xbd_max_requests, -1,
863 "maximum outstanding requests (negotiated)");
865 SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO,
866 "max_request_segments", CTLFLAG_RD,
867 &xbd->xbd_max_request_segments, 0,
868 "maximum number of pages per requests (negotiated)");
870 SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO,
871 "max_request_size", CTLFLAG_RD, &xbd->xbd_max_request_size, 0,
872 "maximum size in bytes of a request (negotiated)");
874 SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO,
875 "ring_pages", CTLFLAG_RD, &xbd->xbd_ring_pages, 0,
876 "communication channel pages (negotiated)");
878 SYSCTL_ADD_PROC(sysctl_ctx, children, OID_AUTO,
879 "features", CTLTYPE_STRING|CTLFLAG_RD, xbd, 0,
880 xbd_sysctl_features, "A", "protocol features (negotiated)");
884 * Translate Linux major/minor to an appropriate name and unit
885 * number. For HVM guests, this allows us to use the same drive names
886 * with blkfront as the emulated drives, easing transition slightly.
889 xbd_vdevice_to_unit(uint32_t vdevice, int *unit, const char **name)
891 static struct vdev_info {
897 {3, 6, 0, "ada"}, /* ide0 */
898 {22, 6, 2, "ada"}, /* ide1 */
899 {33, 6, 4, "ada"}, /* ide2 */
900 {34, 6, 6, "ada"}, /* ide3 */
901 {56, 6, 8, "ada"}, /* ide4 */
902 {57, 6, 10, "ada"}, /* ide5 */
903 {88, 6, 12, "ada"}, /* ide6 */
904 {89, 6, 14, "ada"}, /* ide7 */
905 {90, 6, 16, "ada"}, /* ide8 */
906 {91, 6, 18, "ada"}, /* ide9 */
908 {8, 4, 0, "da"}, /* scsi disk0 */
909 {65, 4, 16, "da"}, /* scsi disk1 */
910 {66, 4, 32, "da"}, /* scsi disk2 */
911 {67, 4, 48, "da"}, /* scsi disk3 */
912 {68, 4, 64, "da"}, /* scsi disk4 */
913 {69, 4, 80, "da"}, /* scsi disk5 */
914 {70, 4, 96, "da"}, /* scsi disk6 */
915 {71, 4, 112, "da"}, /* scsi disk7 */
916 {128, 4, 128, "da"}, /* scsi disk8 */
917 {129, 4, 144, "da"}, /* scsi disk9 */
918 {130, 4, 160, "da"}, /* scsi disk10 */
919 {131, 4, 176, "da"}, /* scsi disk11 */
920 {132, 4, 192, "da"}, /* scsi disk12 */
921 {133, 4, 208, "da"}, /* scsi disk13 */
922 {134, 4, 224, "da"}, /* scsi disk14 */
923 {135, 4, 240, "da"}, /* scsi disk15 */
925 {202, 4, 0, "xbd"}, /* xbd */
929 int major = vdevice >> 8;
930 int minor = vdevice & 0xff;
933 if (vdevice & (1 << 28)) {
934 *unit = (vdevice & ((1 << 28) - 1)) >> 8;
939 for (i = 0; info[i].major; i++) {
940 if (info[i].major == major) {
941 *unit = info[i].base + (minor >> info[i].shift);
942 *name = info[i].name;
952 xbd_instance_create(struct xbd_softc *sc, blkif_sector_t sectors,
953 int vdevice, uint16_t vdisk_info, unsigned long sector_size)
959 xbd_vdevice_to_unit(vdevice, &unit, &name);
963 if (strcmp(name, "xbd") != 0)
964 device_printf(sc->xbd_dev, "attaching as %s%d\n", name, unit);
966 if (xbd_feature_string(sc, features, sizeof(features)) > 0) {
967 device_printf(sc->xbd_dev, "features: %s\n",
971 sc->xbd_disk = disk_alloc();
972 sc->xbd_disk->d_unit = sc->xbd_unit;
973 sc->xbd_disk->d_open = xbd_open;
974 sc->xbd_disk->d_close = xbd_close;
975 sc->xbd_disk->d_ioctl = xbd_ioctl;
976 sc->xbd_disk->d_strategy = xbd_strategy;
977 sc->xbd_disk->d_dump = xbd_dump;
978 sc->xbd_disk->d_name = name;
979 sc->xbd_disk->d_drv1 = sc;
980 sc->xbd_disk->d_sectorsize = sector_size;
982 sc->xbd_disk->d_mediasize = sectors * sector_size;
983 sc->xbd_disk->d_maxsize = sc->xbd_max_request_size;
984 sc->xbd_disk->d_flags = 0;
985 if ((sc->xbd_flags & (XBDF_FLUSH|XBDF_BARRIER)) != 0) {
986 sc->xbd_disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
987 device_printf(sc->xbd_dev,
988 "synchronize cache commands enabled.\n");
990 disk_create(sc->xbd_disk, DISK_VERSION);
996 xbd_free(struct xbd_softc *sc)
1000 /* Prevent new requests being issued until we fix things up. */
1001 mtx_lock(&sc->xbd_io_lock);
1002 sc->xbd_state = XBD_STATE_DISCONNECTED;
1003 mtx_unlock(&sc->xbd_io_lock);
1005 /* Free resources associated with old device channel. */
1007 if (sc->xbd_shadow) {
1009 for (i = 0; i < sc->xbd_max_requests; i++) {
1010 struct xbd_command *cm;
1012 cm = &sc->xbd_shadow[i];
1013 if (cm->cm_sg_refs != NULL) {
1014 free(cm->cm_sg_refs, M_XENBLOCKFRONT);
1015 cm->cm_sg_refs = NULL;
1018 bus_dmamap_destroy(sc->xbd_io_dmat, cm->cm_map);
1020 free(sc->xbd_shadow, M_XENBLOCKFRONT);
1021 sc->xbd_shadow = NULL;
1023 bus_dma_tag_destroy(sc->xbd_io_dmat);
1025 xbd_initq_cm(sc, XBD_Q_FREE);
1026 xbd_initq_cm(sc, XBD_Q_READY);
1027 xbd_initq_cm(sc, XBD_Q_COMPLETE);
1030 xen_intr_unbind(&sc->xen_intr_handle);
1034 /*--------------------------- State Change Handlers --------------------------*/
1036 xbd_initialize(struct xbd_softc *sc)
1038 const char *otherend_path;
1039 const char *node_path;
1040 uint32_t max_ring_page_order;
1043 if (xenbus_get_state(sc->xbd_dev) != XenbusStateInitialising) {
1044 /* Initialization has already been performed. */
1049 * Protocol defaults valid even if negotiation for a
1052 max_ring_page_order = 0;
1053 sc->xbd_ring_pages = 1;
1054 sc->xbd_max_request_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST;
1055 sc->xbd_max_request_size =
1056 XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments);
1059 * Protocol negotiation.
1061 * \note xs_gather() returns on the first encountered error, so
1062 * we must use independant calls in order to guarantee
1063 * we don't miss information in a sparsly populated back-end
1066 * \note xs_scanf() does not update variables for unmatched
1069 otherend_path = xenbus_get_otherend_path(sc->xbd_dev);
1070 node_path = xenbus_get_node(sc->xbd_dev);
1072 /* Support both backend schemes for relaying ring page limits. */
1073 (void)xs_scanf(XST_NIL, otherend_path,
1074 "max-ring-page-order", NULL, "%" PRIu32,
1075 &max_ring_page_order);
1076 sc->xbd_ring_pages = 1 << max_ring_page_order;
1077 (void)xs_scanf(XST_NIL, otherend_path,
1078 "max-ring-pages", NULL, "%" PRIu32,
1079 &sc->xbd_ring_pages);
1080 if (sc->xbd_ring_pages < 1)
1081 sc->xbd_ring_pages = 1;
1083 if (sc->xbd_ring_pages > XBD_MAX_RING_PAGES) {
1084 device_printf(sc->xbd_dev,
1085 "Back-end specified ring-pages of %u "
1086 "limited to front-end limit of %u.\n",
1087 sc->xbd_ring_pages, XBD_MAX_RING_PAGES);
1088 sc->xbd_ring_pages = XBD_MAX_RING_PAGES;
1091 if (powerof2(sc->xbd_ring_pages) == 0) {
1092 uint32_t new_page_limit;
1094 new_page_limit = 0x01 << (fls(sc->xbd_ring_pages) - 1);
1095 device_printf(sc->xbd_dev,
1096 "Back-end specified ring-pages of %u "
1097 "is not a power of 2. Limited to %u.\n",
1098 sc->xbd_ring_pages, new_page_limit);
1099 sc->xbd_ring_pages = new_page_limit;
1102 sc->xbd_max_requests =
1103 BLKIF_MAX_RING_REQUESTS(sc->xbd_ring_pages * PAGE_SIZE);
1104 if (sc->xbd_max_requests > XBD_MAX_REQUESTS) {
1105 device_printf(sc->xbd_dev,
1106 "Back-end specified max_requests of %u "
1107 "limited to front-end limit of %zu.\n",
1108 sc->xbd_max_requests, XBD_MAX_REQUESTS);
1109 sc->xbd_max_requests = XBD_MAX_REQUESTS;
1112 if (xbd_alloc_ring(sc) != 0)
1115 /* Support both backend schemes for relaying ring page limits. */
1116 if (sc->xbd_ring_pages > 1) {
1117 error = xs_printf(XST_NIL, node_path,
1118 "num-ring-pages","%u",
1119 sc->xbd_ring_pages);
1121 xenbus_dev_fatal(sc->xbd_dev, error,
1122 "writing %s/num-ring-pages",
1127 error = xs_printf(XST_NIL, node_path,
1128 "ring-page-order", "%u",
1129 fls(sc->xbd_ring_pages) - 1);
1131 xenbus_dev_fatal(sc->xbd_dev, error,
1132 "writing %s/ring-page-order",
1138 error = xs_printf(XST_NIL, node_path, "event-channel",
1139 "%u", xen_intr_port(sc->xen_intr_handle));
1141 xenbus_dev_fatal(sc->xbd_dev, error,
1142 "writing %s/event-channel",
1147 error = xs_printf(XST_NIL, node_path, "protocol",
1148 "%s", XEN_IO_PROTO_ABI_NATIVE);
1150 xenbus_dev_fatal(sc->xbd_dev, error,
1151 "writing %s/protocol",
1156 xenbus_set_state(sc->xbd_dev, XenbusStateInitialised);
1160 * Invoked when the backend is finally 'ready' (and has published
1161 * the details about the physical device - #sectors, size, etc).
1164 xbd_connect(struct xbd_softc *sc)
1166 device_t dev = sc->xbd_dev;
1167 unsigned long sectors, sector_size;
1169 int err, feature_barrier, feature_flush;
1172 if (sc->xbd_state == XBD_STATE_CONNECTED ||
1173 sc->xbd_state == XBD_STATE_SUSPENDED)
1176 DPRINTK("blkfront.c:connect:%s.\n", xenbus_get_otherend_path(dev));
1178 err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
1179 "sectors", "%lu", §ors,
1180 "info", "%u", &binfo,
1181 "sector-size", "%lu", §or_size,
1184 xenbus_dev_fatal(dev, err,
1185 "reading backend fields at %s",
1186 xenbus_get_otherend_path(dev));
1189 err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
1190 "feature-barrier", "%lu", &feature_barrier,
1192 if (err == 0 && feature_barrier != 0)
1193 sc->xbd_flags |= XBDF_BARRIER;
1195 err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
1196 "feature-flush-cache", "%lu", &feature_flush,
1198 if (err == 0 && feature_flush != 0)
1199 sc->xbd_flags |= XBDF_FLUSH;
1201 /* Allocate datastructures based on negotiated values. */
1202 err = bus_dma_tag_create(
1203 bus_get_dma_tag(sc->xbd_dev), /* parent */
1204 512, PAGE_SIZE, /* algnmnt, boundary */
1205 BUS_SPACE_MAXADDR, /* lowaddr */
1206 BUS_SPACE_MAXADDR, /* highaddr */
1207 NULL, NULL, /* filter, filterarg */
1208 sc->xbd_max_request_size,
1209 sc->xbd_max_request_segments,
1210 PAGE_SIZE, /* maxsegsize */
1211 BUS_DMA_ALLOCNOW, /* flags */
1212 busdma_lock_mutex, /* lockfunc */
1213 &sc->xbd_io_lock, /* lockarg */
1216 xenbus_dev_fatal(sc->xbd_dev, err,
1217 "Cannot allocate parent DMA tag\n");
1221 /* Per-transaction data allocation. */
1222 sc->xbd_shadow = malloc(sizeof(*sc->xbd_shadow) * sc->xbd_max_requests,
1223 M_XENBLOCKFRONT, M_NOWAIT|M_ZERO);
1224 if (sc->xbd_shadow == NULL) {
1225 bus_dma_tag_destroy(sc->xbd_io_dmat);
1226 xenbus_dev_fatal(sc->xbd_dev, ENOMEM,
1227 "Cannot allocate request structures\n");
1231 for (i = 0; i < sc->xbd_max_requests; i++) {
1232 struct xbd_command *cm;
1234 cm = &sc->xbd_shadow[i];
1235 cm->cm_sg_refs = malloc(
1236 sizeof(grant_ref_t) * sc->xbd_max_request_segments,
1237 M_XENBLOCKFRONT, M_NOWAIT);
1238 if (cm->cm_sg_refs == NULL)
1241 cm->cm_flags = XBDCF_INITIALIZER;
1243 if (bus_dmamap_create(sc->xbd_io_dmat, 0, &cm->cm_map) != 0)
1245 xbd_free_command(cm);
1248 if (sc->xbd_disk == NULL) {
1249 device_printf(dev, "%juMB <%s> at %s",
1250 (uintmax_t) sectors / (1048576 / sector_size),
1251 device_get_desc(dev),
1252 xenbus_get_node(dev));
1253 bus_print_child_footer(device_get_parent(dev), dev);
1255 xbd_instance_create(sc, sectors, sc->xbd_vdevice, binfo,
1259 (void)xenbus_set_state(dev, XenbusStateConnected);
1261 /* Kick pending requests. */
1262 mtx_lock(&sc->xbd_io_lock);
1263 sc->xbd_state = XBD_STATE_CONNECTED;
1265 sc->xbd_flags |= XBDF_READY;
1266 mtx_unlock(&sc->xbd_io_lock);
1270 * Handle the change of state of the backend to Closing. We must delete our
1271 * device-layer structures now, to ensure that writes are flushed through to
1272 * the backend. Once this is done, we can switch to Closed in
1276 xbd_closing(device_t dev)
1278 struct xbd_softc *sc = device_get_softc(dev);
1280 xenbus_set_state(dev, XenbusStateClosing);
1282 DPRINTK("xbd_closing: %s removed\n", xenbus_get_node(dev));
1284 if (sc->xbd_disk != NULL) {
1285 disk_destroy(sc->xbd_disk);
1286 sc->xbd_disk = NULL;
1289 xenbus_set_state(dev, XenbusStateClosed);
1292 /*---------------------------- NewBus Entrypoints ----------------------------*/
1294 xbd_probe(device_t dev)
1296 if (strcmp(xenbus_get_type(dev), "vbd") != 0)
1299 if (xen_hvm_domain()) {
1304 * When running in an HVM domain, IDE disk emulation is
1305 * disabled early in boot so that native drivers will
1306 * not see emulated hardware. However, CDROM device
1307 * emulation cannot be disabled.
1309 * Through use of FreeBSD's vm_guest and xen_hvm_domain()
1310 * APIs, we could modify the native CDROM driver to fail its
1311 * probe when running under Xen. Unfortunatlely, the PV
1312 * CDROM support in XenServer (up through at least version
1313 * 6.2) isn't functional, so we instead rely on the emulated
1314 * CDROM instance, and fail to attach the PV one here in
1315 * the blkfront driver.
1317 error = xs_read(XST_NIL, xenbus_get_node(dev),
1318 "device-type", NULL, (void **) &type);
1322 if (strncmp(type, "cdrom", 5) == 0) {
1323 free(type, M_XENSTORE);
1326 free(type, M_XENSTORE);
1329 device_set_desc(dev, "Virtual Block Device");
1335 * Setup supplies the backend dir, virtual device. We place an event
1336 * channel and shared frame entries. We watch backend to wait if it's
1340 xbd_attach(device_t dev)
1342 struct xbd_softc *sc;
1349 /* FIXME: Use dynamic device id if this is not set. */
1350 error = xs_scanf(XST_NIL, xenbus_get_node(dev),
1351 "virtual-device", NULL, "%" PRIu32, &vdevice);
1353 error = xs_scanf(XST_NIL, xenbus_get_node(dev),
1354 "virtual-device-ext", NULL, "%" PRIu32, &vdevice);
1356 xenbus_dev_fatal(dev, error, "reading virtual-device");
1357 device_printf(dev, "Couldn't determine virtual device.\n");
1361 xbd_vdevice_to_unit(vdevice, &unit, &name);
1362 if (!strcmp(name, "xbd"))
1363 device_set_unit(dev, unit);
1365 sc = device_get_softc(dev);
1366 mtx_init(&sc->xbd_io_lock, "blkfront i/o lock", NULL, MTX_DEF);
1368 for (i = 0; i < XBD_MAX_RING_PAGES; i++)
1369 sc->xbd_ring_ref[i] = GRANT_REF_INVALID;
1372 sc->xbd_vdevice = vdevice;
1373 sc->xbd_state = XBD_STATE_DISCONNECTED;
1375 xbd_setup_sysctl(sc);
1377 /* Wait for backend device to publish its protocol capabilities. */
1378 xenbus_set_state(dev, XenbusStateInitialising);
1384 xbd_detach(device_t dev)
1386 struct xbd_softc *sc = device_get_softc(dev);
1388 DPRINTK("%s: %s removed\n", __func__, xenbus_get_node(dev));
1391 mtx_destroy(&sc->xbd_io_lock);
1397 xbd_suspend(device_t dev)
1399 struct xbd_softc *sc = device_get_softc(dev);
1403 /* Prevent new requests being issued until we fix things up. */
1404 mtx_lock(&sc->xbd_io_lock);
1405 saved_state = sc->xbd_state;
1406 sc->xbd_state = XBD_STATE_SUSPENDED;
1408 /* Wait for outstanding I/O to drain. */
1410 while (xbd_queue_length(sc, XBD_Q_BUSY) != 0) {
1411 if (msleep(&sc->xbd_cm_q[XBD_Q_BUSY], &sc->xbd_io_lock,
1412 PRIBIO, "blkf_susp", 30 * hz) == EWOULDBLOCK) {
1417 mtx_unlock(&sc->xbd_io_lock);
1420 sc->xbd_state = saved_state;
1426 xbd_resume(device_t dev)
1428 struct xbd_softc *sc = device_get_softc(dev);
1430 DPRINTK("xbd_resume: %s\n", xenbus_get_node(dev));
1438 * Callback received when the backend's state changes.
1441 xbd_backend_changed(device_t dev, XenbusState backend_state)
1443 struct xbd_softc *sc = device_get_softc(dev);
1445 DPRINTK("backend_state=%d\n", backend_state);
1447 switch (backend_state) {
1448 case XenbusStateUnknown:
1449 case XenbusStateInitialising:
1450 case XenbusStateReconfigured:
1451 case XenbusStateReconfiguring:
1452 case XenbusStateClosed:
1455 case XenbusStateInitWait:
1456 case XenbusStateInitialised:
1460 case XenbusStateConnected:
1465 case XenbusStateClosing:
1466 if (sc->xbd_users > 0)
1467 xenbus_dev_error(dev, -EBUSY,
1468 "Device in use; refusing to close");
1475 /*---------------------------- NewBus Registration ---------------------------*/
1476 static device_method_t xbd_methods[] = {
1477 /* Device interface */
1478 DEVMETHOD(device_probe, xbd_probe),
1479 DEVMETHOD(device_attach, xbd_attach),
1480 DEVMETHOD(device_detach, xbd_detach),
1481 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1482 DEVMETHOD(device_suspend, xbd_suspend),
1483 DEVMETHOD(device_resume, xbd_resume),
1485 /* Xenbus interface */
1486 DEVMETHOD(xenbus_otherend_changed, xbd_backend_changed),
1491 static driver_t xbd_driver = {
1494 sizeof(struct xbd_softc),
1496 devclass_t xbd_devclass;
1498 DRIVER_MODULE(xbd, xenbusb_front, xbd_driver, xbd_devclass, 0, 0);