2 * Copyright (c) 2009-2011 Spectra Logic Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * substantially similar to the "NO WARRANTY" disclaimer below
13 * ("Disclaimer") and any redistribution must be conditioned upon
14 * including a substantially similar Disclaimer requirement for further
15 * binary redistribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGES.
30 * Authors: Justin T. Gibbs (Spectra Logic Corporation)
31 * Ken Merry (Spectra Logic Corporation)
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
39 * \brief Device driver supporting the vending of block storage from
40 * a FreeBSD domain to other domains.
43 #include "opt_kdtrace.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
53 #include <sys/devicestat.h>
55 #include <sys/fcntl.h>
56 #include <sys/filedesc.h>
58 #include <sys/module.h>
59 #include <sys/namei.h>
62 #include <sys/taskqueue.h>
63 #include <sys/types.h>
64 #include <sys/vnode.h>
65 #include <sys/mount.h>
66 #include <sys/sysctl.h>
67 #include <sys/bitstring.h>
70 #include <geom/geom.h>
72 #include <machine/_inttypes.h>
73 #include <machine/xen/xen-os.h>
76 #include <vm/vm_extern.h>
77 #include <vm/vm_kern.h>
79 #include <xen/blkif.h>
80 #include <xen/evtchn.h>
81 #include <xen/gnttab.h>
82 #include <xen/xen_intr.h>
84 #include <xen/interface/event_channel.h>
85 #include <xen/interface/grant_table.h>
87 #include <xen/xenbus/xenbusvar.h>
89 /*--------------------------- Compile-time Tunables --------------------------*/
91 * The maximum number of outstanding request blocks (request headers plus
92 * additional segment blocks) we will allow in a negotiated block-front/back
93 * communication channel.
95 #define XBB_MAX_REQUESTS 256
98 * \brief Define to force all I/O to be performed on memory owned by the
99 * backend device, with a copy-in/out to the remote domain's memory.
101 * \note This option is currently required when this driver's domain is
102 * operating in HVM mode on a system using an IOMMU.
104 * This driver uses Xen's grant table API to gain access to the memory of
105 * the remote domains it serves. When our domain is operating in PV mode,
106 * the grant table mechanism directly updates our domain's page table entries
107 * to point to the physical pages of the remote domain. This scheme guarantees
108 * that blkback and the backing devices it uses can safely perform DMA
109 * operations to satisfy requests. In HVM mode, Xen may use a HW IOMMU to
110 * insure that our domain cannot DMA to pages owned by another domain. As
111 * of Xen 4.0, IOMMU mappings for HVM guests are not updated via the grant
112 * table API. For this reason, in HVM mode, we must bounce all requests into
113 * memory that is mapped into our domain at domain startup and thus has
114 * valid IOMMU mappings.
116 #define XBB_USE_BOUNCE_BUFFERS
119 * \brief Define to enable rudimentary request logging to the console.
123 /*---------------------------------- Macros ----------------------------------*/
125 * Custom malloc type for all driver allocations.
127 static MALLOC_DEFINE(M_XENBLOCKBACK, "xbbd", "Xen Block Back Driver Data");
130 #define DPRINTF(fmt, args...) \
131 printf("xbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
133 #define DPRINTF(fmt, args...) do {} while(0)
137 * The maximum mapped region size per request we will allow in a negotiated
138 * block-front/back communication channel.
140 #define XBB_MAX_REQUEST_SIZE \
141 MIN(MAXPHYS, BLKIF_MAX_SEGMENTS_PER_REQUEST * PAGE_SIZE)
144 * The maximum number of segments (within a request header and accompanying
145 * segment blocks) per request we will allow in a negotiated block-front/back
146 * communication channel.
148 #define XBB_MAX_SEGMENTS_PER_REQUEST \
150 MIN(BLKIF_MAX_SEGMENTS_PER_REQUEST, \
151 (XBB_MAX_REQUEST_SIZE / PAGE_SIZE) + 1)))
154 * The maximum number of shared memory ring pages we will allow in a
155 * negotiated block-front/back communication channel. Allow enough
156 * ring space for all requests to be XBB_MAX_REQUEST_SIZE'd.
158 #define XBB_MAX_RING_PAGES \
159 BLKIF_RING_PAGES(BLKIF_SEGS_TO_BLOCKS(XBB_MAX_SEGMENTS_PER_REQUEST) \
162 * The maximum number of ring pages that we can allow per request list.
163 * We limit this to the maximum number of segments per request, because
164 * that is already a reasonable number of segments to aggregate. This
165 * number should never be smaller than XBB_MAX_SEGMENTS_PER_REQUEST,
166 * because that would leave situations where we can't dispatch even one
169 #define XBB_MAX_SEGMENTS_PER_REQLIST XBB_MAX_SEGMENTS_PER_REQUEST
171 /*--------------------------- Forward Declarations ---------------------------*/
175 static void xbb_attach_failed(struct xbb_softc *xbb, int err, const char *fmt,
176 ...) __attribute__((format(printf, 3, 4)));
177 static int xbb_shutdown(struct xbb_softc *xbb);
178 static int xbb_detach(device_t dev);
180 /*------------------------------ Data Structures -----------------------------*/
182 STAILQ_HEAD(xbb_xen_req_list, xbb_xen_req);
185 XBB_REQLIST_NONE = 0x00,
186 XBB_REQLIST_MAPPED = 0x01
189 struct xbb_xen_reqlist {
191 * Back reference to the parent block back instance for this
192 * request. Used during bio_done handling.
194 struct xbb_softc *xbb;
197 * BLKIF_OP code for this request.
202 * Set to BLKIF_RSP_* to indicate request status.
204 * This field allows an error status to be recorded even if the
205 * delivery of this status must be deferred. Deferred reporting
206 * is necessary, for example, when an error is detected during
207 * completion processing of one bio when other bios for this
208 * request are still outstanding.
213 * Number of 512 byte sectors not transferred.
215 int residual_512b_sectors;
218 * Starting sector number of the first request in the list.
220 off_t starting_sector_number;
223 * If we're going to coalesce, the next contiguous sector would be
226 off_t next_contig_sector;
229 * Number of child requests in the list.
234 * Number of I/O requests dispatched to the backend.
239 * Total number of segments for requests in the list.
244 * Flags for this particular request list.
246 xbb_reqlist_flags flags;
249 * Kernel virtual address space reserved for this request
250 * list structure and used to map the remote domain's pages for
251 * this I/O, into our domain's address space.
256 * Base, psuedo-physical address, corresponding to the start
257 * of this request's kva region.
262 #ifdef XBB_USE_BOUNCE_BUFFERS
264 * Pre-allocated domain local memory used to proxy remote
265 * domain memory during I/O operations.
271 * Array of grant handles (one per page) used to map this request.
273 grant_handle_t *gnt_handles;
276 * Device statistics request ordering type (ordered or simple).
278 devstat_tag_type ds_tag_type;
281 * Device statistics request type (read, write, no_data).
283 devstat_trans_flags ds_trans_type;
286 * The start time for this request.
288 struct bintime ds_t0;
291 * Linked list of contiguous requests with the same operation type.
293 struct xbb_xen_req_list contig_req_list;
296 * Linked list links used to aggregate idle requests in the
297 * request list free pool (xbb->reqlist_free_stailq) and pending
298 * requests waiting for execution (xbb->reqlist_pending_stailq).
300 STAILQ_ENTRY(xbb_xen_reqlist) links;
303 STAILQ_HEAD(xbb_xen_reqlist_list, xbb_xen_reqlist);
306 * \brief Object tracking an in-flight I/O from a Xen VBD consumer.
310 * Linked list links used to aggregate requests into a reqlist
311 * and to store them in the request free pool.
313 STAILQ_ENTRY(xbb_xen_req) links;
316 * The remote domain's identifier for this I/O request.
321 * The number of pages currently mapped for this request.
326 * The number of 512 byte sectors comprising this requests.
331 * The number of struct bio requests still outstanding for this
332 * request on the backend device. This field is only used for
333 * device (rather than file) backed I/O.
338 * BLKIF_OP code for this request.
343 * Storage used for non-native ring requests.
345 blkif_request_t ring_req_storage;
348 * Pointer to the Xen request in the ring.
350 blkif_request_t *ring_req;
353 * Consumer index for this request.
355 RING_IDX req_ring_idx;
358 * The start time for this request.
360 struct bintime ds_t0;
363 * Pointer back to our parent request list.
365 struct xbb_xen_reqlist *reqlist;
367 SLIST_HEAD(xbb_xen_req_slist, xbb_xen_req);
370 * \brief Configuration data for the shared memory request ring
371 * used to communicate with the front-end client of this
374 struct xbb_ring_config {
375 /** KVA address where ring memory is mapped. */
378 /** The pseudo-physical address where ring memory is mapped.*/
382 * Grant table handles, one per-ring page, returned by the
383 * hyperpervisor upon mapping of the ring and required to
384 * unmap it when a connection is torn down.
386 grant_handle_t handle[XBB_MAX_RING_PAGES];
389 * The device bus address returned by the hypervisor when
390 * mapping the ring and required to unmap it when a connection
393 uint64_t bus_addr[XBB_MAX_RING_PAGES];
395 /** The number of ring pages mapped for the current connection. */
399 * The grant references, one per-ring page, supplied by the
400 * front-end, allowing us to reference the ring pages in the
401 * front-end's domain and to map these pages into our own domain.
403 grant_ref_t ring_ref[XBB_MAX_RING_PAGES];
405 /** The interrupt driven even channel used to signal ring events. */
406 evtchn_port_t evtchn;
410 * Per-instance connection state flags.
415 * The front-end requested a read-only mount of the
416 * back-end device/file.
418 XBBF_READ_ONLY = 0x01,
420 /** Communication with the front-end has been established. */
421 XBBF_RING_CONNECTED = 0x02,
424 * Front-end requests exist in the ring and are waiting for
425 * xbb_xen_req objects to free up.
427 XBBF_RESOURCE_SHORTAGE = 0x04,
429 /** Connection teardown in progress. */
430 XBBF_SHUTDOWN = 0x08,
432 /** A thread is already performing shutdown processing. */
433 XBBF_IN_SHUTDOWN = 0x10
436 /** Backend device type. */
438 /** Backend type unknown. */
439 XBB_TYPE_NONE = 0x00,
442 * Backend type disk (access via cdev switch
445 XBB_TYPE_DISK = 0x01,
447 /** Backend type file (access vnode operations.). */
452 * \brief Structure used to memoize information about a per-request
453 * scatter-gather list.
455 * The chief benefit of using this data structure is it avoids having
456 * to reparse the possibly discontiguous S/G list in the original
457 * request. Due to the way that the mapping of the memory backing an
458 * I/O transaction is handled by Xen, a second pass is unavoidable.
459 * At least this way the second walk is a simple array traversal.
461 * \note A single Scatter/Gather element in the block interface covers
462 * at most 1 machine page. In this context a sector (blkif
463 * nomenclature, not what I'd choose) is a 512b aligned unit
464 * of mapping within the machine page referenced by an S/G
468 /** The number of 512b data chunks mapped in this S/G element. */
472 * The index (0 based) of the first 512b data chunk mapped
473 * in this S/G element.
478 * The index (0 based) of the last 512b data chunk mapped
479 * in this S/G element.
485 * Character device backend specific configuration data.
487 struct xbb_dev_data {
488 /** Cdev used for device backend access. */
491 /** Cdev switch used for device backend access. */
494 /** Used to hold a reference on opened cdev backend devices. */
499 * File backend specific configuration data.
501 struct xbb_file_data {
502 /** Credentials to use for vnode backed (file based) I/O. */
506 * \brief Array of io vectors used to process file based I/O.
508 * Only a single file based request is outstanding per-xbb instance,
509 * so we only need one of these.
511 struct iovec xiovecs[XBB_MAX_SEGMENTS_PER_REQLIST];
512 #ifdef XBB_USE_BOUNCE_BUFFERS
515 * \brief Array of io vectors used to handle bouncing of file reads.
517 * Vnode operations are free to modify uio data during their
518 * exectuion. In the case of a read with bounce buffering active,
519 * we need some of the data from the original uio in order to
520 * bounce-out the read data. This array serves as the temporary
521 * storage for this saved data.
523 struct iovec saved_xiovecs[XBB_MAX_SEGMENTS_PER_REQLIST];
526 * \brief Array of memoized bounce buffer kva offsets used
527 * in the file based backend.
529 * Due to the way that the mapping of the memory backing an
530 * I/O transaction is handled by Xen, a second pass through
531 * the request sg elements is unavoidable. We memoize the computed
532 * bounce address here to reduce the cost of the second walk.
534 void *xiovecs_vaddr[XBB_MAX_SEGMENTS_PER_REQLIST];
535 #endif /* XBB_USE_BOUNCE_BUFFERS */
539 * Collection of backend type specific data.
541 union xbb_backend_data {
542 struct xbb_dev_data dev;
543 struct xbb_file_data file;
547 * Function signature of backend specific I/O handlers.
549 typedef int (*xbb_dispatch_t)(struct xbb_softc *xbb,
550 struct xbb_xen_reqlist *reqlist, int operation,
554 * Per-instance configuration data.
559 * Task-queue used to process I/O requests.
561 struct taskqueue *io_taskqueue;
564 * Single "run the request queue" task enqueued
569 /** Device type for this instance. */
570 xbb_type device_type;
572 /** NewBus device corresponding to this instance. */
575 /** Backend specific dispatch routine for this instance. */
576 xbb_dispatch_t dispatch_io;
578 /** The number of requests outstanding on the backend device/file. */
579 int active_request_count;
581 /** Free pool of request tracking structures. */
582 struct xbb_xen_req_list request_free_stailq;
584 /** Array, sized at connection time, of request tracking structures. */
585 struct xbb_xen_req *requests;
587 /** Free pool of request list structures. */
588 struct xbb_xen_reqlist_list reqlist_free_stailq;
590 /** List of pending request lists awaiting execution. */
591 struct xbb_xen_reqlist_list reqlist_pending_stailq;
593 /** Array, sized at connection time, of request list structures. */
594 struct xbb_xen_reqlist *request_lists;
597 * Global pool of kva used for mapping remote domain ring
598 * and I/O transaction data.
602 /** Psuedo-physical address corresponding to kva. */
603 uint64_t gnt_base_addr;
605 /** The size of the global kva pool. */
608 /** The size of the KVA area used for request lists. */
609 int reqlist_kva_size;
611 /** The number of pages of KVA used for request lists */
612 int reqlist_kva_pages;
614 /** Bitmap of free KVA pages */
618 * \brief Cached value of the front-end's domain id.
620 * This value is used at once for each mapped page in
621 * a transaction. We cache it to avoid incuring the
622 * cost of an ivar access every time this is needed.
627 * \brief The blkif protocol abi in effect.
629 * There are situations where the back and front ends can
630 * have a different, native abi (e.g. intel x86_64 and
631 * 32bit x86 domains on the same machine). The back-end
632 * always accomodates the front-end's native abi. That
633 * value is pulled from the XenStore and recorded here.
638 * \brief The maximum number of requests and request lists allowed
639 * to be in flight at a time.
641 * This value is negotiated via the XenStore.
646 * \brief The maximum number of segments (1 page per segment)
647 * that can be mapped by a request.
649 * This value is negotiated via the XenStore.
651 u_int max_request_segments;
654 * \brief Maximum number of segments per request list.
656 * This value is derived from and will generally be larger than
657 * max_request_segments.
659 u_int max_reqlist_segments;
662 * The maximum size of any request to this back-end
665 * This value is negotiated via the XenStore.
667 u_int max_request_size;
670 * The maximum size of any request list. This is derived directly
671 * from max_reqlist_segments.
673 u_int max_reqlist_size;
675 /** Various configuration and state bit flags. */
678 /** Ring mapping and interrupt configuration data. */
679 struct xbb_ring_config ring_config;
681 /** Runtime, cross-abi safe, structures for ring access. */
682 blkif_back_rings_t rings;
684 /** IRQ mapping for the communication ring event channel. */
688 * \brief Backend access mode flags (e.g. write, or read-only).
690 * This value is passed to us by the front-end via the XenStore.
695 * \brief Backend device type (e.g. "disk", "cdrom", "floppy").
697 * This value is passed to us by the front-end via the XenStore.
703 * \brief Backend device/file identifier.
705 * This value is passed to us by the front-end via the XenStore.
706 * We expect this to be a POSIX path indicating the file or
712 * Vnode corresponding to the backend device node or file
717 union xbb_backend_data backend;
719 /** The native sector size of the backend. */
722 /** log2 of sector_size. */
723 u_int sector_size_shift;
725 /** Size in bytes of the backend device or file. */
729 * \brief media_size expressed in terms of the backend native
732 * (e.g. xbb->media_size >> xbb->sector_size_shift).
734 uint64_t media_num_sectors;
737 * \brief Array of memoized scatter gather data computed during the
738 * conversion of blkif ring requests to internal xbb_xen_req
741 * Ring processing is serialized so we only need one of these.
743 struct xbb_sg xbb_sgs[XBB_MAX_SEGMENTS_PER_REQLIST];
746 * Temporary grant table map used in xbb_dispatch_io(). When
747 * XBB_MAX_SEGMENTS_PER_REQLIST gets large, keeping this on the
748 * stack could cause a stack overflow.
750 struct gnttab_map_grant_ref maps[XBB_MAX_SEGMENTS_PER_REQLIST];
752 /** Mutex protecting per-instance data. */
757 * Resource representing allocated physical address space
758 * associated with our per-instance kva region.
760 struct resource *pseudo_phys_res;
762 /** Resource id for allocated physical address space. */
763 int pseudo_phys_res_id;
767 * I/O statistics from BlockBack dispatch down. These are
768 * coalesced requests, and we start them right before execution.
770 struct devstat *xbb_stats;
773 * I/O statistics coming into BlockBack. These are the requests as
774 * we get them from BlockFront. They are started as soon as we
775 * receive a request, and completed when the I/O is complete.
777 struct devstat *xbb_stats_in;
779 /** Disable sending flush to the backend */
782 /** Send a real flush for every N flush requests */
785 /** Count of flush requests in the interval */
788 /** Don't coalesce requests if this is set */
789 int no_coalesce_reqs;
791 /** Number of requests we have received */
792 uint64_t reqs_received;
794 /** Number of requests we have completed*/
795 uint64_t reqs_completed;
797 /** How many forced dispatches (i.e. without coalescing) have happend */
798 uint64_t forced_dispatch;
800 /** How many normal dispatches have happend */
801 uint64_t normal_dispatch;
803 /** How many total dispatches have happend */
804 uint64_t total_dispatch;
806 /** How many times we have run out of KVA */
807 uint64_t kva_shortages;
809 /** How many times we have run out of request structures */
810 uint64_t request_shortages;
813 /*---------------------------- Request Processing ----------------------------*/
815 * Allocate an internal transaction tracking structure from the free pool.
817 * \param xbb Per-instance xbb configuration structure.
819 * \return On success, a pointer to the allocated xbb_xen_req structure.
822 static inline struct xbb_xen_req *
823 xbb_get_req(struct xbb_softc *xbb)
825 struct xbb_xen_req *req;
829 mtx_assert(&xbb->lock, MA_OWNED);
831 if ((req = STAILQ_FIRST(&xbb->request_free_stailq)) != NULL) {
832 STAILQ_REMOVE_HEAD(&xbb->request_free_stailq, links);
833 xbb->active_request_count++;
840 * Return an allocated transaction tracking structure to the free pool.
842 * \param xbb Per-instance xbb configuration structure.
843 * \param req The request structure to free.
846 xbb_release_req(struct xbb_softc *xbb, struct xbb_xen_req *req)
848 mtx_assert(&xbb->lock, MA_OWNED);
850 STAILQ_INSERT_HEAD(&xbb->request_free_stailq, req, links);
851 xbb->active_request_count--;
853 KASSERT(xbb->active_request_count >= 0,
854 ("xbb_release_req: negative active count"));
858 * Return an xbb_xen_req_list of allocated xbb_xen_reqs to the free pool.
860 * \param xbb Per-instance xbb configuration structure.
861 * \param req_list The list of requests to free.
862 * \param nreqs The number of items in the list.
865 xbb_release_reqs(struct xbb_softc *xbb, struct xbb_xen_req_list *req_list,
868 mtx_assert(&xbb->lock, MA_OWNED);
870 STAILQ_CONCAT(&xbb->request_free_stailq, req_list);
871 xbb->active_request_count -= nreqs;
873 KASSERT(xbb->active_request_count >= 0,
874 ("xbb_release_reqs: negative active count"));
878 * Given a page index and 512b sector offset within that page,
879 * calculate an offset into a request's kva region.
881 * \param reqlist The request structure whose kva region will be accessed.
882 * \param pagenr The page index used to compute the kva offset.
883 * \param sector The 512b sector index used to compute the page relative
886 * \return The computed global KVA offset.
888 static inline uint8_t *
889 xbb_reqlist_vaddr(struct xbb_xen_reqlist *reqlist, int pagenr, int sector)
891 return (reqlist->kva + (PAGE_SIZE * pagenr) + (sector << 9));
894 #ifdef XBB_USE_BOUNCE_BUFFERS
896 * Given a page index and 512b sector offset within that page,
897 * calculate an offset into a request's local bounce memory region.
899 * \param reqlist The request structure whose bounce region will be accessed.
900 * \param pagenr The page index used to compute the bounce offset.
901 * \param sector The 512b sector index used to compute the page relative
904 * \return The computed global bounce buffer address.
906 static inline uint8_t *
907 xbb_reqlist_bounce_addr(struct xbb_xen_reqlist *reqlist, int pagenr, int sector)
909 return (reqlist->bounce + (PAGE_SIZE * pagenr) + (sector << 9));
914 * Given a page number and 512b sector offset within that page,
915 * calculate an offset into the request's memory region that the
916 * underlying backend device/file should use for I/O.
918 * \param reqlist The request structure whose I/O region will be accessed.
919 * \param pagenr The page index used to compute the I/O offset.
920 * \param sector The 512b sector index used to compute the page relative
923 * \return The computed global I/O address.
925 * Depending on configuration, this will either be a local bounce buffer
926 * or a pointer to the memory mapped in from the front-end domain for
929 static inline uint8_t *
930 xbb_reqlist_ioaddr(struct xbb_xen_reqlist *reqlist, int pagenr, int sector)
932 #ifdef XBB_USE_BOUNCE_BUFFERS
933 return (xbb_reqlist_bounce_addr(reqlist, pagenr, sector));
935 return (xbb_reqlist_vaddr(reqlist, pagenr, sector));
940 * Given a page index and 512b sector offset within that page, calculate
941 * an offset into the local psuedo-physical address space used to map a
942 * front-end's request data into a request.
944 * \param reqlist The request list structure whose pseudo-physical region
946 * \param pagenr The page index used to compute the pseudo-physical offset.
947 * \param sector The 512b sector index used to compute the page relative
948 * pseudo-physical offset.
950 * \return The computed global pseudo-phsyical address.
952 * Depending on configuration, this will either be a local bounce buffer
953 * or a pointer to the memory mapped in from the front-end domain for
956 static inline uintptr_t
957 xbb_get_gntaddr(struct xbb_xen_reqlist *reqlist, int pagenr, int sector)
959 struct xbb_softc *xbb;
963 return ((uintptr_t)(xbb->gnt_base_addr +
964 (uintptr_t)(reqlist->kva - xbb->kva) +
965 (PAGE_SIZE * pagenr) + (sector << 9)));
969 * Get Kernel Virtual Address space for mapping requests.
971 * \param xbb Per-instance xbb configuration structure.
972 * \param nr_pages Number of pages needed.
973 * \param check_only If set, check for free KVA but don't allocate it.
974 * \param have_lock If set, xbb lock is already held.
976 * \return On success, a pointer to the allocated KVA region. Otherwise NULL.
978 * Note: This should be unnecessary once we have either chaining or
979 * scatter/gather support for struct bio. At that point we'll be able to
980 * put multiple addresses and lengths in one bio/bio chain and won't need
981 * to map everything into one virtual segment.
984 xbb_get_kva(struct xbb_softc *xbb, int nr_pages)
986 intptr_t first_clear;
991 KASSERT(nr_pages != 0, ("xbb_get_kva of zero length"));
996 mtx_lock(&xbb->lock);
999 * Look for the first available page. If there are none, we're done.
1001 bit_ffc(xbb->kva_free, xbb->reqlist_kva_pages, &first_clear);
1003 if (first_clear == -1)
1007 * Starting at the first available page, look for consecutive free
1008 * pages that will satisfy the user's request.
1010 for (i = first_clear, num_clear = 0; i < xbb->reqlist_kva_pages; i++) {
1012 * If this is true, the page is used, so we have to reset
1013 * the number of clear pages and the first clear page
1014 * (since it pointed to a region with an insufficient number
1017 if (bit_test(xbb->kva_free, i)) {
1023 if (first_clear == -1)
1027 * If this is true, we've found a large enough free region
1028 * to satisfy the request.
1030 if (++num_clear == nr_pages) {
1032 bit_nset(xbb->kva_free, first_clear,
1033 first_clear + nr_pages - 1);
1035 free_kva = xbb->kva +
1036 (uint8_t *)(first_clear * PAGE_SIZE);
1038 KASSERT(free_kva >= (uint8_t *)xbb->kva &&
1039 free_kva + (nr_pages * PAGE_SIZE) <=
1040 (uint8_t *)xbb->ring_config.va,
1041 ("Free KVA %p len %d out of range, "
1042 "kva = %#jx, ring VA = %#jx\n", free_kva,
1043 nr_pages * PAGE_SIZE, (uintmax_t)xbb->kva,
1044 (uintmax_t)xbb->ring_config.va));
1051 if (free_kva == NULL) {
1052 xbb->flags |= XBBF_RESOURCE_SHORTAGE;
1053 xbb->kva_shortages++;
1056 mtx_unlock(&xbb->lock);
1062 * Free allocated KVA.
1064 * \param xbb Per-instance xbb configuration structure.
1065 * \param kva_ptr Pointer to allocated KVA region.
1066 * \param nr_pages Number of pages in the KVA region.
1069 xbb_free_kva(struct xbb_softc *xbb, uint8_t *kva_ptr, int nr_pages)
1071 intptr_t start_page;
1073 mtx_assert(&xbb->lock, MA_OWNED);
1075 start_page = (intptr_t)(kva_ptr - xbb->kva) >> PAGE_SHIFT;
1076 bit_nclear(xbb->kva_free, start_page, start_page + nr_pages - 1);
1081 * Unmap the front-end pages associated with this I/O request.
1083 * \param req The request structure to unmap.
1086 xbb_unmap_reqlist(struct xbb_xen_reqlist *reqlist)
1088 struct gnttab_unmap_grant_ref unmap[XBB_MAX_SEGMENTS_PER_REQLIST];
1094 for (i = 0; i < reqlist->nr_segments; i++) {
1096 if (reqlist->gnt_handles[i] == GRANT_REF_INVALID)
1099 unmap[invcount].host_addr = xbb_get_gntaddr(reqlist, i, 0);
1100 unmap[invcount].dev_bus_addr = 0;
1101 unmap[invcount].handle = reqlist->gnt_handles[i];
1102 reqlist->gnt_handles[i] = GRANT_REF_INVALID;
1106 error = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,
1108 KASSERT(error == 0, ("Grant table operation failed"));
1112 * Allocate an internal transaction tracking structure from the free pool.
1114 * \param xbb Per-instance xbb configuration structure.
1116 * \return On success, a pointer to the allocated xbb_xen_reqlist structure.
1119 static inline struct xbb_xen_reqlist *
1120 xbb_get_reqlist(struct xbb_softc *xbb)
1122 struct xbb_xen_reqlist *reqlist;
1126 mtx_assert(&xbb->lock, MA_OWNED);
1128 if ((reqlist = STAILQ_FIRST(&xbb->reqlist_free_stailq)) != NULL) {
1130 STAILQ_REMOVE_HEAD(&xbb->reqlist_free_stailq, links);
1131 reqlist->flags = XBB_REQLIST_NONE;
1132 reqlist->kva = NULL;
1133 reqlist->status = BLKIF_RSP_OKAY;
1134 reqlist->residual_512b_sectors = 0;
1135 reqlist->num_children = 0;
1136 reqlist->nr_segments = 0;
1137 STAILQ_INIT(&reqlist->contig_req_list);
1144 * Return an allocated transaction tracking structure to the free pool.
1146 * \param xbb Per-instance xbb configuration structure.
1147 * \param req The request list structure to free.
1148 * \param wakeup If set, wakeup the work thread if freeing this reqlist
1149 * during a resource shortage condition.
1152 xbb_release_reqlist(struct xbb_softc *xbb, struct xbb_xen_reqlist *reqlist,
1156 mtx_lock(&xbb->lock);
1159 wakeup = xbb->flags & XBBF_RESOURCE_SHORTAGE;
1160 xbb->flags &= ~XBBF_RESOURCE_SHORTAGE;
1163 if (reqlist->kva != NULL)
1164 xbb_free_kva(xbb, reqlist->kva, reqlist->nr_segments);
1166 xbb_release_reqs(xbb, &reqlist->contig_req_list, reqlist->num_children);
1168 STAILQ_INSERT_TAIL(&xbb->reqlist_free_stailq, reqlist, links);
1170 if ((xbb->flags & XBBF_SHUTDOWN) != 0) {
1172 * Shutdown is in progress. See if we can
1173 * progress further now that one more request
1174 * has completed and been returned to the
1180 mtx_unlock(&xbb->lock);
1183 taskqueue_enqueue(xbb->io_taskqueue, &xbb->io_task);
1187 * Request resources and do basic request setup.
1189 * \param xbb Per-instance xbb configuration structure.
1190 * \param reqlist Pointer to reqlist pointer.
1191 * \param ring_req Pointer to a block ring request.
1192 * \param ring_index The ring index of this request.
1194 * \return 0 for success, non-zero for failure.
1197 xbb_get_resources(struct xbb_softc *xbb, struct xbb_xen_reqlist **reqlist,
1198 blkif_request_t *ring_req, RING_IDX ring_idx)
1200 struct xbb_xen_reqlist *nreqlist;
1201 struct xbb_xen_req *nreq;
1206 mtx_lock(&xbb->lock);
1209 * We don't allow new resources to be allocated if we're in the
1210 * process of shutting down.
1212 if ((xbb->flags & XBBF_SHUTDOWN) != 0) {
1213 mtx_unlock(&xbb->lock);
1218 * Allocate a reqlist if the caller doesn't have one already.
1220 if (*reqlist == NULL) {
1221 nreqlist = xbb_get_reqlist(xbb);
1222 if (nreqlist == NULL)
1226 /* We always allocate a request. */
1227 nreq = xbb_get_req(xbb);
1231 mtx_unlock(&xbb->lock);
1233 if (*reqlist == NULL) {
1234 *reqlist = nreqlist;
1235 nreqlist->operation = ring_req->operation;
1236 nreqlist->starting_sector_number = ring_req->sector_number;
1237 STAILQ_INSERT_TAIL(&xbb->reqlist_pending_stailq, nreqlist,
1241 nreq->reqlist = *reqlist;
1242 nreq->req_ring_idx = ring_idx;
1244 if (xbb->abi != BLKIF_PROTOCOL_NATIVE) {
1245 bcopy(ring_req, &nreq->ring_req_storage, sizeof(*ring_req));
1246 nreq->ring_req = &nreq->ring_req_storage;
1248 nreq->ring_req = ring_req;
1251 binuptime(&nreq->ds_t0);
1252 devstat_start_transaction(xbb->xbb_stats_in, &nreq->ds_t0);
1253 STAILQ_INSERT_TAIL(&(*reqlist)->contig_req_list, nreq, links);
1254 (*reqlist)->num_children++;
1255 (*reqlist)->nr_segments += ring_req->nr_segments;
1262 * We're out of resources, so set the shortage flag. The next time
1263 * a request is released, we'll try waking up the work thread to
1264 * see if we can allocate more resources.
1266 xbb->flags |= XBBF_RESOURCE_SHORTAGE;
1267 xbb->request_shortages++;
1270 xbb_release_req(xbb, nreq);
1272 mtx_unlock(&xbb->lock);
1274 if (nreqlist != NULL)
1275 xbb_release_reqlist(xbb, nreqlist, /*wakeup*/ 0);
1281 * Create and transmit a response to a blkif request.
1283 * \param xbb Per-instance xbb configuration structure.
1284 * \param req The request structure to which to respond.
1285 * \param status The status code to report. See BLKIF_RSP_*
1286 * in sys/xen/interface/io/blkif.h.
1289 xbb_send_response(struct xbb_softc *xbb, struct xbb_xen_req *req, int status)
1291 blkif_response_t *resp;
1298 * Place on the response ring for the relevant domain.
1299 * For now, only the spacing between entries is different
1300 * in the different ABIs, not the response entry layout.
1302 mtx_lock(&xbb->lock);
1304 case BLKIF_PROTOCOL_NATIVE:
1305 resp = RING_GET_RESPONSE(&xbb->rings.native,
1306 xbb->rings.native.rsp_prod_pvt);
1308 case BLKIF_PROTOCOL_X86_32:
1309 resp = (blkif_response_t *)
1310 RING_GET_RESPONSE(&xbb->rings.x86_32,
1311 xbb->rings.x86_32.rsp_prod_pvt);
1313 case BLKIF_PROTOCOL_X86_64:
1314 resp = (blkif_response_t *)
1315 RING_GET_RESPONSE(&xbb->rings.x86_64,
1316 xbb->rings.x86_64.rsp_prod_pvt);
1319 panic("Unexpected blkif protocol ABI.");
1323 resp->operation = req->operation;
1324 resp->status = status;
1326 xbb->rings.common.rsp_prod_pvt += BLKIF_SEGS_TO_BLOCKS(req->nr_pages);
1327 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&xbb->rings.common, notify);
1329 if (xbb->rings.common.rsp_prod_pvt == xbb->rings.common.req_cons) {
1332 * Tail check for pending requests. Allows frontend to avoid
1333 * notifications if requests are already in flight (lower
1334 * overheads and promotes batching).
1336 RING_FINAL_CHECK_FOR_REQUESTS(&xbb->rings.common, more_to_do);
1337 } else if (RING_HAS_UNCONSUMED_REQUESTS(&xbb->rings.common)) {
1342 xbb->reqs_completed++;
1344 mtx_unlock(&xbb->lock);
1347 taskqueue_enqueue(xbb->io_taskqueue, &xbb->io_task);
1350 notify_remote_via_irq(xbb->irq);
1354 * Complete a request list.
1356 * \param xbb Per-instance xbb configuration structure.
1357 * \param reqlist Allocated internal request list structure.
1360 xbb_complete_reqlist(struct xbb_softc *xbb, struct xbb_xen_reqlist *reqlist)
1362 struct xbb_xen_req *nreq;
1367 if (reqlist->flags & XBB_REQLIST_MAPPED)
1368 xbb_unmap_reqlist(reqlist);
1371 * All I/O is done, send the response. A lock should not be
1372 * necessary here because the request list is complete, and
1373 * therefore this is the only context accessing this request
1374 * right now. The functions we call do their own locking if
1377 STAILQ_FOREACH(nreq, &reqlist->contig_req_list, links) {
1378 off_t cur_sectors_sent;
1380 xbb_send_response(xbb, nreq, reqlist->status);
1382 /* We don't report bytes sent if there is an error. */
1383 if (reqlist->status == BLKIF_RSP_OKAY)
1384 cur_sectors_sent = nreq->nr_512b_sectors;
1386 cur_sectors_sent = 0;
1388 sectors_sent += cur_sectors_sent;
1390 devstat_end_transaction(xbb->xbb_stats_in,
1391 /*bytes*/cur_sectors_sent << 9,
1392 reqlist->ds_tag_type,
1393 reqlist->ds_trans_type,
1395 /*then*/&nreq->ds_t0);
1399 * Take out any sectors not sent. If we wind up negative (which
1400 * might happen if an error is reported as well as a residual), just
1401 * report 0 sectors sent.
1403 sectors_sent -= reqlist->residual_512b_sectors;
1404 if (sectors_sent < 0)
1407 devstat_end_transaction(xbb->xbb_stats,
1408 /*bytes*/ sectors_sent << 9,
1409 reqlist->ds_tag_type,
1410 reqlist->ds_trans_type,
1412 /*then*/&reqlist->ds_t0);
1414 xbb_release_reqlist(xbb, reqlist, /*wakeup*/ 1);
1418 * Completion handler for buffer I/O requests issued by the device
1421 * \param bio The buffer I/O request on which to perform completion
1425 xbb_bio_done(struct bio *bio)
1427 struct xbb_softc *xbb;
1428 struct xbb_xen_reqlist *reqlist;
1430 reqlist = bio->bio_caller1;
1433 reqlist->residual_512b_sectors += bio->bio_resid >> 9;
1436 * This is a bit imprecise. With aggregated I/O a single
1437 * request list can contain multiple front-end requests and
1438 * a multiple bios may point to a single request. By carefully
1439 * walking the request list, we could map residuals and errors
1440 * back to the original front-end request, but the interface
1441 * isn't sufficiently rich for us to properly report the error.
1442 * So, we just treat the entire request list as having failed if an
1443 * error occurs on any part. And, if an error occurs, we treat
1444 * the amount of data transferred as 0.
1446 * For residuals, we report it on the overall aggregated device,
1447 * but not on the individual requests, since we don't currently
1448 * do the work to determine which front-end request to which the
1451 if (bio->bio_error) {
1452 DPRINTF("BIO returned error %d for operation on device %s\n",
1453 bio->bio_error, xbb->dev_name);
1454 reqlist->status = BLKIF_RSP_ERROR;
1456 if (bio->bio_error == ENXIO
1457 && xenbus_get_state(xbb->dev) == XenbusStateConnected) {
1460 * Backend device has disappeared. Signal the
1461 * front-end that we (the device proxy) want to
1464 xenbus_set_state(xbb->dev, XenbusStateClosing);
1468 #ifdef XBB_USE_BOUNCE_BUFFERS
1469 if (bio->bio_cmd == BIO_READ) {
1470 vm_offset_t kva_offset;
1472 kva_offset = (vm_offset_t)bio->bio_data
1473 - (vm_offset_t)reqlist->bounce;
1474 memcpy((uint8_t *)reqlist->kva + kva_offset,
1475 bio->bio_data, bio->bio_bcount);
1477 #endif /* XBB_USE_BOUNCE_BUFFERS */
1480 * Decrement the pending count for the request list. When we're
1481 * done with the requests, send status back for all of them.
1483 if (atomic_fetchadd_int(&reqlist->pendcnt, -1) == 1)
1484 xbb_complete_reqlist(xbb, reqlist);
1490 * Parse a blkif request into an internal request structure and send
1491 * it to the backend for processing.
1493 * \param xbb Per-instance xbb configuration structure.
1494 * \param reqlist Allocated internal request list structure.
1496 * \return On success, 0. For resource shortages, non-zero.
1498 * This routine performs the backend common aspects of request parsing
1499 * including compiling an internal request structure, parsing the S/G
1500 * list and any secondary ring requests in which they may reside, and
1501 * the mapping of front-end I/O pages into our domain.
1504 xbb_dispatch_io(struct xbb_softc *xbb, struct xbb_xen_reqlist *reqlist)
1506 struct xbb_sg *xbb_sg;
1507 struct gnttab_map_grant_ref *map;
1508 struct blkif_request_segment *sg;
1509 struct blkif_request_segment *last_block_sg;
1510 struct xbb_xen_req *nreq;
1520 reqlist->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1526 * First determine whether we have enough free KVA to satisfy this
1527 * request list. If not, tell xbb_run_queue() so it can go to
1528 * sleep until we have more KVA.
1530 reqlist->kva = NULL;
1531 if (reqlist->nr_segments != 0) {
1532 reqlist->kva = xbb_get_kva(xbb, reqlist->nr_segments);
1533 if (reqlist->kva == NULL) {
1535 * If we're out of KVA, return ENOMEM.
1541 binuptime(&reqlist->ds_t0);
1542 devstat_start_transaction(xbb->xbb_stats, &reqlist->ds_t0);
1544 switch (reqlist->operation) {
1545 case BLKIF_OP_WRITE_BARRIER:
1546 bio_flags |= BIO_ORDERED;
1547 reqlist->ds_tag_type = DEVSTAT_TAG_ORDERED;
1549 case BLKIF_OP_WRITE:
1550 operation = BIO_WRITE;
1551 reqlist->ds_trans_type = DEVSTAT_WRITE;
1552 if ((xbb->flags & XBBF_READ_ONLY) != 0) {
1553 DPRINTF("Attempt to write to read only device %s\n",
1555 reqlist->status = BLKIF_RSP_ERROR;
1560 operation = BIO_READ;
1561 reqlist->ds_trans_type = DEVSTAT_READ;
1563 case BLKIF_OP_FLUSH_DISKCACHE:
1565 * If this is true, the user has requested that we disable
1566 * flush support. So we just complete the requests
1569 if (xbb->disable_flush != 0) {
1574 * The user has requested that we only send a real flush
1575 * for every N flush requests. So keep count, and either
1576 * complete the request immediately or queue it for the
1579 if (xbb->flush_interval != 0) {
1580 if (++(xbb->flush_count) < xbb->flush_interval) {
1583 xbb->flush_count = 0;
1586 operation = BIO_FLUSH;
1587 reqlist->ds_tag_type = DEVSTAT_TAG_ORDERED;
1588 reqlist->ds_trans_type = DEVSTAT_NO_DATA;
1592 DPRINTF("error: unknown block io operation [%d]\n",
1593 reqlist->operation);
1594 reqlist->status = BLKIF_RSP_ERROR;
1599 xbb_sg = xbb->xbb_sgs;
1603 STAILQ_FOREACH(nreq, &reqlist->contig_req_list, links) {
1604 blkif_request_t *ring_req;
1605 RING_IDX req_ring_idx;
1608 ring_req = nreq->ring_req;
1609 req_ring_idx = nreq->req_ring_idx;
1611 nseg = ring_req->nr_segments;
1612 nreq->id = ring_req->id;
1613 nreq->nr_pages = nseg;
1614 nreq->nr_512b_sectors = 0;
1618 /* Check that number of segments is sane. */
1619 if (unlikely(nseg == 0)
1620 || unlikely(nseg > xbb->max_request_segments)) {
1621 DPRINTF("Bad number of segments in request (%d)\n",
1623 reqlist->status = BLKIF_RSP_ERROR;
1627 block_segs = MIN(nreq->nr_pages,
1628 BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK);
1630 last_block_sg = sg + block_segs;
1633 while (sg < last_block_sg) {
1635 XBB_MAX_SEGMENTS_PER_REQLIST,
1636 ("seg_idx %d is too large, max "
1637 "segs %d\n", seg_idx,
1638 XBB_MAX_SEGMENTS_PER_REQLIST));
1640 xbb_sg->first_sect = sg->first_sect;
1641 xbb_sg->last_sect = sg->last_sect;
1643 (int8_t)(sg->last_sect -
1644 sg->first_sect + 1);
1646 if ((sg->last_sect >= (PAGE_SIZE >> 9))
1647 || (xbb_sg->nsect <= 0)) {
1648 reqlist->status = BLKIF_RSP_ERROR;
1652 nr_sects += xbb_sg->nsect;
1653 map->host_addr = xbb_get_gntaddr(reqlist,
1654 seg_idx, /*sector*/0);
1655 KASSERT(map->host_addr + PAGE_SIZE <=
1656 xbb->ring_config.gnt_addr,
1657 ("Host address %#jx len %d overlaps "
1658 "ring address %#jx\n",
1659 (uintmax_t)map->host_addr, PAGE_SIZE,
1660 (uintmax_t)xbb->ring_config.gnt_addr));
1662 map->flags = GNTMAP_host_map;
1663 map->ref = sg->gref;
1664 map->dom = xbb->otherend_id;
1665 if (operation == BIO_WRITE)
1666 map->flags |= GNTMAP_readonly;
1674 block_segs = MIN(nseg - req_seg_idx,
1675 BLKIF_MAX_SEGMENTS_PER_SEGMENT_BLOCK);
1676 if (block_segs == 0)
1680 * Fetch the next request block full of SG elements.
1681 * For now, only the spacing between entries is
1682 * different in the different ABIs, not the sg entry
1687 case BLKIF_PROTOCOL_NATIVE:
1688 sg = BLKRING_GET_SEG_BLOCK(&xbb->rings.native,
1691 case BLKIF_PROTOCOL_X86_32:
1693 sg = BLKRING_GET_SEG_BLOCK(&xbb->rings.x86_32,
1697 case BLKIF_PROTOCOL_X86_64:
1699 sg = BLKRING_GET_SEG_BLOCK(&xbb->rings.x86_64,
1704 panic("Unexpected blkif protocol ABI.");
1707 last_block_sg = sg + block_segs;
1710 /* Convert to the disk's sector size */
1711 nreq->nr_512b_sectors = nr_sects;
1712 nr_sects = (nr_sects << 9) >> xbb->sector_size_shift;
1713 total_sects += nr_sects;
1715 if ((nreq->nr_512b_sectors &
1716 ((xbb->sector_size >> 9) - 1)) != 0) {
1717 device_printf(xbb->dev, "%s: I/O size (%d) is not "
1718 "a multiple of the backing store sector "
1719 "size (%d)\n", __func__,
1720 nreq->nr_512b_sectors << 9,
1722 reqlist->status = BLKIF_RSP_ERROR;
1727 error = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref,
1728 xbb->maps, reqlist->nr_segments);
1730 panic("Grant table operation failed (%d)", error);
1732 reqlist->flags |= XBB_REQLIST_MAPPED;
1734 for (seg_idx = 0, map = xbb->maps; seg_idx < reqlist->nr_segments;
1737 if (unlikely(map->status != 0)) {
1738 DPRINTF("invalid buffer -- could not remap "
1739 "it (%d)\n", map->status);
1740 DPRINTF("Mapping(%d): Host Addr 0x%lx, flags "
1741 "0x%x ref 0x%x, dom %d\n", seg_idx,
1742 map->host_addr, map->flags, map->ref,
1744 reqlist->status = BLKIF_RSP_ERROR;
1748 reqlist->gnt_handles[seg_idx] = map->handle;
1750 if (reqlist->starting_sector_number + total_sects >
1751 xbb->media_num_sectors) {
1753 DPRINTF("%s of [%" PRIu64 ",%" PRIu64 "] "
1754 "extends past end of device %s\n",
1755 operation == BIO_READ ? "read" : "write",
1756 reqlist->starting_sector_number,
1757 reqlist->starting_sector_number + total_sects,
1759 reqlist->status = BLKIF_RSP_ERROR;
1765 error = xbb->dispatch_io(xbb,
1771 reqlist->status = BLKIF_RSP_ERROR;
1779 xbb_complete_reqlist(xbb, reqlist);
1785 xbb_count_sects(blkif_request_t *ring_req)
1790 for (i = 0; i < ring_req->nr_segments; i++) {
1793 nsect = (int8_t)(ring_req->seg[i].last_sect -
1794 ring_req->seg[i].first_sect + 1);
1805 * Process incoming requests from the shared communication ring in response
1806 * to a signal on the ring's event channel.
1808 * \param context Callback argument registerd during task initialization -
1809 * the xbb_softc for this instance.
1810 * \param pending The number of taskqueue_enqueue events that have
1811 * occurred since this handler was last run.
1814 xbb_run_queue(void *context, int pending)
1816 struct xbb_softc *xbb;
1817 blkif_back_rings_t *rings;
1819 uint64_t cur_sector;
1821 struct xbb_xen_reqlist *reqlist;
1824 xbb = (struct xbb_softc *)context;
1825 rings = &xbb->rings;
1828 * Work gather and dispatch loop. Note that we have a bias here
1829 * towards gathering I/O sent by blockfront. We first gather up
1830 * everything in the ring, as long as we have resources. Then we
1831 * dispatch one request, and then attempt to gather up any
1832 * additional requests that have come in while we were dispatching
1835 * This allows us to get a clearer picture (via devstat) of how
1836 * many requests blockfront is queueing to us at any given time.
1842 * Initialize reqlist to the last element in the pending
1843 * queue, if there is one. This allows us to add more
1844 * requests to that request list, if we have room.
1846 reqlist = STAILQ_LAST(&xbb->reqlist_pending_stailq,
1847 xbb_xen_reqlist, links);
1848 if (reqlist != NULL) {
1849 cur_sector = reqlist->next_contig_sector;
1850 cur_operation = reqlist->operation;
1857 * Cache req_prod to avoid accessing a cache line shared
1858 * with the frontend.
1860 rp = rings->common.sring->req_prod;
1862 /* Ensure we see queued requests up to 'rp'. */
1866 * Run so long as there is work to consume and the generation
1867 * of a response will not overflow the ring.
1869 * @note There's a 1 to 1 relationship between requests and
1870 * responses, so an overflow should never occur. This
1871 * test is to protect our domain from digesting bogus
1872 * data. Shouldn't we log this?
1874 while (rings->common.req_cons != rp
1875 && RING_REQUEST_CONS_OVERFLOW(&rings->common,
1876 rings->common.req_cons) == 0){
1877 blkif_request_t ring_req_storage;
1878 blkif_request_t *ring_req;
1882 case BLKIF_PROTOCOL_NATIVE:
1883 ring_req = RING_GET_REQUEST(&xbb->rings.native,
1884 rings->common.req_cons);
1886 case BLKIF_PROTOCOL_X86_32:
1888 struct blkif_x86_32_request *ring_req32;
1890 ring_req32 = RING_GET_REQUEST(
1891 &xbb->rings.x86_32, rings->common.req_cons);
1892 blkif_get_x86_32_req(&ring_req_storage,
1894 ring_req = &ring_req_storage;
1897 case BLKIF_PROTOCOL_X86_64:
1899 struct blkif_x86_64_request *ring_req64;
1901 ring_req64 =RING_GET_REQUEST(&xbb->rings.x86_64,
1902 rings->common.req_cons);
1903 blkif_get_x86_64_req(&ring_req_storage,
1905 ring_req = &ring_req_storage;
1909 panic("Unexpected blkif protocol ABI.");
1914 * Check for situations that would require closing
1915 * off this I/O for further coalescing:
1916 * - Coalescing is turned off.
1917 * - Current I/O is out of sequence with the previous
1919 * - Coalesced I/O would be too large.
1921 if ((reqlist != NULL)
1922 && ((xbb->no_coalesce_reqs != 0)
1923 || ((xbb->no_coalesce_reqs == 0)
1924 && ((ring_req->sector_number != cur_sector)
1925 || (ring_req->operation != cur_operation)
1926 || ((ring_req->nr_segments + reqlist->nr_segments) >
1927 xbb->max_reqlist_segments))))) {
1932 * Grab and check for all resources in one shot.
1933 * If we can't get all of the resources we need,
1934 * the shortage is noted and the thread will get
1935 * woken up when more resources are available.
1937 retval = xbb_get_resources(xbb, &reqlist, ring_req,
1938 xbb->rings.common.req_cons);
1942 * Resource shortage has been recorded.
1943 * We'll be scheduled to run once a request
1944 * object frees up due to a completion.
1950 * Signify that we can overwrite this request with
1951 * a response by incrementing our consumer index.
1952 * The response won't be generated until after
1953 * we've already consumed all necessary data out
1954 * of the version of the request in the ring buffer
1955 * (for native mode). We must update the consumer
1956 * index before issueing back-end I/O so there is
1957 * no possibility that it will complete and a
1958 * response be generated before we make room in
1959 * the queue for that response.
1961 xbb->rings.common.req_cons +=
1962 BLKIF_SEGS_TO_BLOCKS(ring_req->nr_segments);
1963 xbb->reqs_received++;
1965 cur_size = xbb_count_sects(ring_req);
1966 cur_sector = ring_req->sector_number + cur_size;
1967 reqlist->next_contig_sector = cur_sector;
1968 cur_operation = ring_req->operation;
1971 /* Check for I/O to dispatch */
1972 reqlist = STAILQ_FIRST(&xbb->reqlist_pending_stailq);
1973 if (reqlist == NULL) {
1975 * We're out of work to do, put the task queue to
1982 * Grab the first request off the queue and attempt
1985 STAILQ_REMOVE_HEAD(&xbb->reqlist_pending_stailq, links);
1987 retval = xbb_dispatch_io(xbb, reqlist);
1990 * xbb_dispatch_io() returns non-zero only when
1991 * there is a resource shortage. If that's the
1992 * case, re-queue this request on the head of the
1993 * queue, and go to sleep until we have more
1996 STAILQ_INSERT_HEAD(&xbb->reqlist_pending_stailq,
2001 * If we still have anything on the queue after
2002 * removing the head entry, that is because we
2003 * met one of the criteria to create a new
2004 * request list (outlined above), and we'll call
2005 * that a forced dispatch for statistical purposes.
2007 * Otherwise, if there is only one element on the
2008 * queue, we coalesced everything available on
2009 * the ring and we'll call that a normal dispatch.
2011 reqlist = STAILQ_FIRST(&xbb->reqlist_pending_stailq);
2013 if (reqlist != NULL)
2014 xbb->forced_dispatch++;
2016 xbb->normal_dispatch++;
2018 xbb->total_dispatch++;
2024 * Interrupt handler bound to the shared ring's event channel.
2026 * \param arg Callback argument registerd during event channel
2027 * binding - the xbb_softc for this instance.
2032 struct xbb_softc *xbb;
2034 /* Defer to kernel thread. */
2035 xbb = (struct xbb_softc *)arg;
2036 taskqueue_enqueue(xbb->io_taskqueue, &xbb->io_task);
2039 SDT_PROVIDER_DEFINE(xbb);
2040 SDT_PROBE_DEFINE1(xbb, kernel, xbb_dispatch_dev, flush, flush, "int");
2041 SDT_PROBE_DEFINE3(xbb, kernel, xbb_dispatch_dev, read, read, "int", "uint64_t",
2043 SDT_PROBE_DEFINE3(xbb, kernel, xbb_dispatch_dev, write, write, "int",
2044 "uint64_t", "uint64_t");
2046 /*----------------------------- Backend Handlers -----------------------------*/
2048 * Backend handler for character device access.
2050 * \param xbb Per-instance xbb configuration structure.
2051 * \param reqlist Allocated internal request list structure.
2052 * \param operation BIO_* I/O operation code.
2053 * \param bio_flags Additional bio_flag data to pass to any generated
2054 * bios (e.g. BIO_ORDERED)..
2056 * \return 0 for success, errno codes for failure.
2059 xbb_dispatch_dev(struct xbb_softc *xbb, struct xbb_xen_reqlist *reqlist,
2060 int operation, int bio_flags)
2062 struct xbb_dev_data *dev_data;
2063 struct bio *bios[XBB_MAX_SEGMENTS_PER_REQLIST];
2064 struct xbb_xen_req *nreq;
2067 struct xbb_sg *xbb_sg;
2074 dev_data = &xbb->backend.dev;
2075 bio_offset = (off_t)reqlist->starting_sector_number
2076 << xbb->sector_size_shift;
2081 if (operation == BIO_FLUSH) {
2082 nreq = STAILQ_FIRST(&reqlist->contig_req_list);
2084 if (unlikely(bio == NULL)) {
2085 DPRINTF("Unable to allocate bio for BIO_FLUSH\n");
2090 bio->bio_cmd = BIO_FLUSH;
2091 bio->bio_flags |= BIO_ORDERED;
2092 bio->bio_dev = dev_data->cdev;
2093 bio->bio_offset = 0;
2095 bio->bio_done = xbb_bio_done;
2096 bio->bio_caller1 = nreq;
2097 bio->bio_pblkno = 0;
2101 SDT_PROBE1(xbb, kernel, xbb_dispatch_dev, flush,
2102 device_get_unit(xbb->dev));
2104 (*dev_data->csw->d_strategy)(bio);
2109 xbb_sg = xbb->xbb_sgs;
2111 nseg = reqlist->nr_segments;
2113 for (seg_idx = 0; seg_idx < nseg; seg_idx++, xbb_sg++) {
2116 * KVA will not be contiguous, so any additional
2117 * I/O will need to be represented in a new bio.
2120 && (xbb_sg->first_sect != 0)) {
2121 if ((bio->bio_length & (xbb->sector_size - 1)) != 0) {
2122 printf("%s: Discontiguous I/O request "
2123 "from domain %d ends on "
2124 "non-sector boundary\n",
2125 __func__, xbb->otherend_id);
2127 goto fail_free_bios;
2134 * Make sure that the start of this bio is
2135 * aligned to a device sector.
2137 if ((bio_offset & (xbb->sector_size - 1)) != 0){
2138 printf("%s: Misaligned I/O request "
2139 "from domain %d\n", __func__,
2142 goto fail_free_bios;
2145 bio = bios[nbio++] = g_new_bio();
2146 if (unlikely(bio == NULL)) {
2148 goto fail_free_bios;
2150 bio->bio_cmd = operation;
2151 bio->bio_flags |= bio_flags;
2152 bio->bio_dev = dev_data->cdev;
2153 bio->bio_offset = bio_offset;
2154 bio->bio_data = xbb_reqlist_ioaddr(reqlist, seg_idx,
2155 xbb_sg->first_sect);
2156 bio->bio_done = xbb_bio_done;
2157 bio->bio_caller1 = reqlist;
2158 bio->bio_pblkno = bio_offset >> xbb->sector_size_shift;
2161 bio->bio_length += xbb_sg->nsect << 9;
2162 bio->bio_bcount = bio->bio_length;
2163 bio_offset += xbb_sg->nsect << 9;
2165 if (xbb_sg->last_sect != (PAGE_SIZE - 512) >> 9) {
2167 if ((bio->bio_length & (xbb->sector_size - 1)) != 0) {
2168 printf("%s: Discontiguous I/O request "
2169 "from domain %d ends on "
2170 "non-sector boundary\n",
2171 __func__, xbb->otherend_id);
2173 goto fail_free_bios;
2176 * KVA will not be contiguous, so any additional
2177 * I/O will need to be represented in a new bio.
2183 reqlist->pendcnt = nbio;
2185 for (bio_idx = 0; bio_idx < nbio; bio_idx++)
2187 #ifdef XBB_USE_BOUNCE_BUFFERS
2188 vm_offset_t kva_offset;
2190 kva_offset = (vm_offset_t)bios[bio_idx]->bio_data
2191 - (vm_offset_t)reqlist->bounce;
2192 if (operation == BIO_WRITE) {
2193 memcpy(bios[bio_idx]->bio_data,
2194 (uint8_t *)reqlist->kva + kva_offset,
2195 bios[bio_idx]->bio_bcount);
2198 if (operation == BIO_READ) {
2199 SDT_PROBE3(xbb, kernel, xbb_dispatch_dev, read,
2200 device_get_unit(xbb->dev),
2201 bios[bio_idx]->bio_offset,
2202 bios[bio_idx]->bio_length);
2203 } else if (operation == BIO_WRITE) {
2204 SDT_PROBE3(xbb, kernel, xbb_dispatch_dev, write,
2205 device_get_unit(xbb->dev),
2206 bios[bio_idx]->bio_offset,
2207 bios[bio_idx]->bio_length);
2209 (*dev_data->csw->d_strategy)(bios[bio_idx]);
2215 for (bio_idx = 0; bio_idx < (nbio-1); bio_idx++)
2216 g_destroy_bio(bios[bio_idx]);
2221 SDT_PROBE_DEFINE1(xbb, kernel, xbb_dispatch_file, flush, flush, "int");
2222 SDT_PROBE_DEFINE3(xbb, kernel, xbb_dispatch_file, read, read, "int", "uint64_t",
2224 SDT_PROBE_DEFINE3(xbb, kernel, xbb_dispatch_file, write, write, "int",
2225 "uint64_t", "uint64_t");
2228 * Backend handler for file access.
2230 * \param xbb Per-instance xbb configuration structure.
2231 * \param reqlist Allocated internal request list.
2232 * \param operation BIO_* I/O operation code.
2233 * \param flags Additional bio_flag data to pass to any generated bios
2234 * (e.g. BIO_ORDERED)..
2236 * \return 0 for success, errno codes for failure.
2239 xbb_dispatch_file(struct xbb_softc *xbb, struct xbb_xen_reqlist *reqlist,
2240 int operation, int flags)
2242 struct xbb_file_data *file_data;
2247 struct xbb_sg *xbb_sg;
2248 struct iovec *xiovec;
2249 #ifdef XBB_USE_BOUNCE_BUFFERS
2251 int saved_uio_iovcnt;
2252 #endif /* XBB_USE_BOUNCE_BUFFERS */
2256 file_data = &xbb->backend.file;
2259 bzero(&xuio, sizeof(xuio));
2261 switch (operation) {
2263 xuio.uio_rw = UIO_READ;
2266 xuio.uio_rw = UIO_WRITE;
2269 struct mount *mountpoint;
2271 SDT_PROBE1(xbb, kernel, xbb_dispatch_file, flush,
2272 device_get_unit(xbb->dev));
2274 vfs_is_locked = VFS_LOCK_GIANT(xbb->vn->v_mount);
2276 (void) vn_start_write(xbb->vn, &mountpoint, V_WAIT);
2278 vn_lock(xbb->vn, LK_EXCLUSIVE | LK_RETRY);
2279 error = VOP_FSYNC(xbb->vn, MNT_WAIT, curthread);
2280 VOP_UNLOCK(xbb->vn, 0);
2282 vn_finished_write(mountpoint);
2284 VFS_UNLOCK_GIANT(vfs_is_locked);
2286 goto bailout_send_response;
2290 panic("invalid operation %d", operation);
2293 xuio.uio_offset = (vm_offset_t)reqlist->starting_sector_number
2294 << xbb->sector_size_shift;
2295 xuio.uio_segflg = UIO_SYSSPACE;
2296 xuio.uio_iov = file_data->xiovecs;
2297 xuio.uio_iovcnt = 0;
2298 xbb_sg = xbb->xbb_sgs;
2299 nseg = reqlist->nr_segments;
2301 for (xiovec = NULL, seg_idx = 0; seg_idx < nseg; seg_idx++, xbb_sg++) {
2304 * If the first sector is not 0, the KVA will
2305 * not be contiguous and we'll need to go on
2306 * to another segment.
2308 if (xbb_sg->first_sect != 0)
2311 if (xiovec == NULL) {
2312 xiovec = &file_data->xiovecs[xuio.uio_iovcnt];
2313 xiovec->iov_base = xbb_reqlist_ioaddr(reqlist,
2314 seg_idx, xbb_sg->first_sect);
2315 #ifdef XBB_USE_BOUNCE_BUFFERS
2317 * Store the address of the incoming
2318 * buffer at this particular offset
2319 * as well, so we can do the copy
2320 * later without having to do more
2321 * work to recalculate this address.
2323 p_vaddr = &file_data->xiovecs_vaddr[xuio.uio_iovcnt];
2324 *p_vaddr = xbb_reqlist_vaddr(reqlist, seg_idx,
2325 xbb_sg->first_sect);
2326 #endif /* XBB_USE_BOUNCE_BUFFERS */
2327 xiovec->iov_len = 0;
2331 xiovec->iov_len += xbb_sg->nsect << 9;
2333 xuio.uio_resid += xbb_sg->nsect << 9;
2336 * If the last sector is not the full page
2337 * size count, the next segment will not be
2338 * contiguous in KVA and we need a new iovec.
2340 if (xbb_sg->last_sect != (PAGE_SIZE - 512) >> 9)
2344 xuio.uio_td = curthread;
2346 #ifdef XBB_USE_BOUNCE_BUFFERS
2347 saved_uio_iovcnt = xuio.uio_iovcnt;
2349 if (operation == BIO_WRITE) {
2350 /* Copy the write data to the local buffer. */
2351 for (seg_idx = 0, p_vaddr = file_data->xiovecs_vaddr,
2352 xiovec = xuio.uio_iov; seg_idx < xuio.uio_iovcnt;
2353 seg_idx++, xiovec++, p_vaddr++) {
2355 memcpy(xiovec->iov_base, *p_vaddr, xiovec->iov_len);
2359 * We only need to save off the iovecs in the case of a
2360 * read, because the copy for the read happens after the
2361 * VOP_READ(). (The uio will get modified in that call
2364 memcpy(file_data->saved_xiovecs, xuio.uio_iov,
2365 xuio.uio_iovcnt * sizeof(xuio.uio_iov[0]));
2367 #endif /* XBB_USE_BOUNCE_BUFFERS */
2369 vfs_is_locked = VFS_LOCK_GIANT(xbb->vn->v_mount);
2370 switch (operation) {
2373 SDT_PROBE3(xbb, kernel, xbb_dispatch_file, read,
2374 device_get_unit(xbb->dev), xuio.uio_offset,
2377 vn_lock(xbb->vn, LK_EXCLUSIVE | LK_RETRY);
2380 * UFS pays attention to IO_DIRECT for reads. If the
2381 * DIRECTIO option is configured into the kernel, it calls
2382 * ffs_rawread(). But that only works for single-segment
2383 * uios with user space addresses. In our case, with a
2384 * kernel uio, it still reads into the buffer cache, but it
2385 * will just try to release the buffer from the cache later
2388 * ZFS does not pay attention to IO_DIRECT for reads.
2390 * UFS does not pay attention to IO_SYNC for reads.
2392 * ZFS pays attention to IO_SYNC (which translates into the
2393 * Solaris define FRSYNC for zfs_read()) for reads. It
2394 * attempts to sync the file before reading.
2396 * So, to attempt to provide some barrier semantics in the
2397 * BIO_ORDERED case, set both IO_DIRECT and IO_SYNC.
2399 error = VOP_READ(xbb->vn, &xuio, (flags & BIO_ORDERED) ?
2400 (IO_DIRECT|IO_SYNC) : 0, file_data->cred);
2402 VOP_UNLOCK(xbb->vn, 0);
2405 struct mount *mountpoint;
2407 SDT_PROBE3(xbb, kernel, xbb_dispatch_file, write,
2408 device_get_unit(xbb->dev), xuio.uio_offset,
2411 (void)vn_start_write(xbb->vn, &mountpoint, V_WAIT);
2413 vn_lock(xbb->vn, LK_EXCLUSIVE | LK_RETRY);
2416 * UFS pays attention to IO_DIRECT for writes. The write
2417 * is done asynchronously. (Normally the write would just
2418 * get put into cache.
2420 * UFS pays attention to IO_SYNC for writes. It will
2421 * attempt to write the buffer out synchronously if that
2424 * ZFS does not pay attention to IO_DIRECT for writes.
2426 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
2427 * for writes. It will flush the transaction from the
2428 * cache before returning.
2430 * So if we've got the BIO_ORDERED flag set, we want
2431 * IO_SYNC in either the UFS or ZFS case.
2433 error = VOP_WRITE(xbb->vn, &xuio, (flags & BIO_ORDERED) ?
2434 IO_SYNC : 0, file_data->cred);
2435 VOP_UNLOCK(xbb->vn, 0);
2437 vn_finished_write(mountpoint);
2442 panic("invalid operation %d", operation);
2445 VFS_UNLOCK_GIANT(vfs_is_locked);
2447 #ifdef XBB_USE_BOUNCE_BUFFERS
2448 /* We only need to copy here for read operations */
2449 if (operation == BIO_READ) {
2451 for (seg_idx = 0, p_vaddr = file_data->xiovecs_vaddr,
2452 xiovec = file_data->saved_xiovecs;
2453 seg_idx < saved_uio_iovcnt; seg_idx++,
2454 xiovec++, p_vaddr++) {
2457 * Note that we have to use the copy of the
2458 * io vector we made above. uiomove() modifies
2459 * the uio and its referenced vector as uiomove
2460 * performs the copy, so we can't rely on any
2461 * state from the original uio.
2463 memcpy(*p_vaddr, xiovec->iov_base, xiovec->iov_len);
2466 #endif /* XBB_USE_BOUNCE_BUFFERS */
2468 bailout_send_response:
2471 reqlist->status = BLKIF_RSP_ERROR;
2473 xbb_complete_reqlist(xbb, reqlist);
2478 /*--------------------------- Backend Configuration --------------------------*/
2480 * Close and cleanup any backend device/file specific state for this
2481 * block back instance.
2483 * \param xbb Per-instance xbb configuration structure.
2486 xbb_close_backend(struct xbb_softc *xbb)
2489 DPRINTF("closing dev=%s\n", xbb->dev_name);
2492 int vfs_is_locked = 0;
2494 if ((xbb->flags & XBBF_READ_ONLY) == 0)
2497 switch (xbb->device_type) {
2499 if (xbb->backend.dev.csw) {
2500 dev_relthread(xbb->backend.dev.cdev,
2501 xbb->backend.dev.dev_ref);
2502 xbb->backend.dev.csw = NULL;
2503 xbb->backend.dev.cdev = NULL;
2507 vfs_is_locked = VFS_LOCK_GIANT(xbb->vn->v_mount);
2511 panic("Unexpected backend type.");
2515 (void)vn_close(xbb->vn, flags, NOCRED, curthread);
2518 switch (xbb->device_type) {
2522 VFS_UNLOCK_GIANT(vfs_is_locked);
2523 if (xbb->backend.file.cred != NULL) {
2524 crfree(xbb->backend.file.cred);
2525 xbb->backend.file.cred = NULL;
2530 panic("Unexpected backend type.");
2538 * Open a character device to be used for backend I/O.
2540 * \param xbb Per-instance xbb configuration structure.
2542 * \return 0 for success, errno codes for failure.
2545 xbb_open_dev(struct xbb_softc *xbb)
2549 struct cdevsw *devsw;
2552 xbb->device_type = XBB_TYPE_DISK;
2553 xbb->dispatch_io = xbb_dispatch_dev;
2554 xbb->backend.dev.cdev = xbb->vn->v_rdev;
2555 xbb->backend.dev.csw = dev_refthread(xbb->backend.dev.cdev,
2556 &xbb->backend.dev.dev_ref);
2557 if (xbb->backend.dev.csw == NULL)
2558 panic("Unable to retrieve device switch");
2560 error = VOP_GETATTR(xbb->vn, &vattr, NOCRED);
2562 xenbus_dev_fatal(xbb->dev, error, "error getting "
2563 "vnode attributes for device %s",
2569 dev = xbb->vn->v_rdev;
2570 devsw = dev->si_devsw;
2571 if (!devsw->d_ioctl) {
2572 xenbus_dev_fatal(xbb->dev, ENODEV, "no d_ioctl for "
2573 "device %s!", xbb->dev_name);
2577 error = devsw->d_ioctl(dev, DIOCGSECTORSIZE,
2578 (caddr_t)&xbb->sector_size, FREAD,
2581 xenbus_dev_fatal(xbb->dev, error,
2582 "error calling ioctl DIOCGSECTORSIZE "
2583 "for device %s", xbb->dev_name);
2587 error = devsw->d_ioctl(dev, DIOCGMEDIASIZE,
2588 (caddr_t)&xbb->media_size, FREAD,
2591 xenbus_dev_fatal(xbb->dev, error,
2592 "error calling ioctl DIOCGMEDIASIZE "
2593 "for device %s", xbb->dev_name);
2601 * Open a file to be used for backend I/O.
2603 * \param xbb Per-instance xbb configuration structure.
2605 * \return 0 for success, errno codes for failure.
2608 xbb_open_file(struct xbb_softc *xbb)
2610 struct xbb_file_data *file_data;
2614 file_data = &xbb->backend.file;
2615 xbb->device_type = XBB_TYPE_FILE;
2616 xbb->dispatch_io = xbb_dispatch_file;
2617 error = VOP_GETATTR(xbb->vn, &vattr, curthread->td_ucred);
2619 xenbus_dev_fatal(xbb->dev, error,
2620 "error calling VOP_GETATTR()"
2621 "for file %s", xbb->dev_name);
2626 * Verify that we have the ability to upgrade to exclusive
2627 * access on this file so we can trap errors at open instead
2628 * of reporting them during first access.
2630 if (VOP_ISLOCKED(xbb->vn) != LK_EXCLUSIVE) {
2631 vn_lock(xbb->vn, LK_UPGRADE | LK_RETRY);
2632 if (xbb->vn->v_iflag & VI_DOOMED) {
2634 xenbus_dev_fatal(xbb->dev, error,
2635 "error locking file %s",
2642 file_data->cred = crhold(curthread->td_ucred);
2643 xbb->media_size = vattr.va_size;
2646 * XXX KDM vattr.va_blocksize may be larger than 512 bytes here.
2647 * With ZFS, it is 131072 bytes. Block sizes that large don't work
2648 * with disklabel and UFS on FreeBSD at least. Large block sizes
2649 * may not work with other OSes as well. So just export a sector
2650 * size of 512 bytes, which should work with any OS or
2651 * application. Since our backing is a file, any block size will
2652 * work fine for the backing store.
2655 xbb->sector_size = vattr.va_blocksize;
2657 xbb->sector_size = 512;
2660 * Sanity check. The media size has to be at least one
2663 if (xbb->media_size < xbb->sector_size) {
2665 xenbus_dev_fatal(xbb->dev, error,
2666 "file %s size %ju < block size %u",
2668 (uintmax_t)xbb->media_size,
2675 * Open the backend provider for this connection.
2677 * \param xbb Per-instance xbb configuration structure.
2679 * \return 0 for success, errno codes for failure.
2682 xbb_open_backend(struct xbb_softc *xbb)
2684 struct nameidata nd;
2692 DPRINTF("opening dev=%s\n", xbb->dev_name);
2694 if (rootvnode == NULL) {
2695 xenbus_dev_fatal(xbb->dev, ENOENT,
2696 "Root file system not mounted");
2700 if ((xbb->flags & XBBF_READ_ONLY) == 0)
2703 if (!curthread->td_proc->p_fd->fd_cdir) {
2704 curthread->td_proc->p_fd->fd_cdir = rootvnode;
2707 if (!curthread->td_proc->p_fd->fd_rdir) {
2708 curthread->td_proc->p_fd->fd_rdir = rootvnode;
2711 if (!curthread->td_proc->p_fd->fd_jdir) {
2712 curthread->td_proc->p_fd->fd_jdir = rootvnode;
2717 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, xbb->dev_name, curthread);
2718 error = vn_open(&nd, &flags, 0, NULL);
2721 * This is the only reasonable guess we can make as far as
2722 * path if the user doesn't give us a fully qualified path.
2723 * If they want to specify a file, they need to specify the
2726 if (xbb->dev_name[0] != '/') {
2727 char *dev_path = "/dev/";
2730 /* Try adding device path at beginning of name */
2731 dev_name = malloc(strlen(xbb->dev_name)
2732 + strlen(dev_path) + 1,
2733 M_XENBLOCKBACK, M_NOWAIT);
2735 sprintf(dev_name, "%s%s", dev_path,
2737 free(xbb->dev_name, M_XENBLOCKBACK);
2738 xbb->dev_name = dev_name;
2742 xenbus_dev_fatal(xbb->dev, error, "error opening device %s",
2747 vfs_is_locked = NDHASGIANT(&nd);
2749 NDFREE(&nd, NDF_ONLY_PNBUF);
2753 /* We only support disks and files. */
2754 if (vn_isdisk(xbb->vn, &error)) {
2755 error = xbb_open_dev(xbb);
2756 } else if (xbb->vn->v_type == VREG) {
2757 error = xbb_open_file(xbb);
2760 xenbus_dev_fatal(xbb->dev, error, "%s is not a disk "
2761 "or file", xbb->dev_name);
2763 VOP_UNLOCK(xbb->vn, 0);
2764 VFS_UNLOCK_GIANT(vfs_is_locked);
2767 xbb_close_backend(xbb);
2771 xbb->sector_size_shift = fls(xbb->sector_size) - 1;
2772 xbb->media_num_sectors = xbb->media_size >> xbb->sector_size_shift;
2774 DPRINTF("opened %s=%s sector_size=%u media_size=%" PRId64 "\n",
2775 (xbb->device_type == XBB_TYPE_DISK) ? "dev" : "file",
2776 xbb->dev_name, xbb->sector_size, xbb->media_size);
2781 /*------------------------ Inter-Domain Communication ------------------------*/
2783 * Free dynamically allocated KVA or pseudo-physical address allocations.
2785 * \param xbb Per-instance xbb configuration structure.
2788 xbb_free_communication_mem(struct xbb_softc *xbb)
2790 if (xbb->kva != 0) {
2792 kmem_free(kernel_map, xbb->kva, xbb->kva_size);
2794 if (xbb->pseudo_phys_res != NULL) {
2795 bus_release_resource(xbb->dev, SYS_RES_MEMORY,
2796 xbb->pseudo_phys_res_id,
2797 xbb->pseudo_phys_res);
2798 xbb->pseudo_phys_res = NULL;
2803 xbb->gnt_base_addr = 0;
2804 if (xbb->kva_free != NULL) {
2805 free(xbb->kva_free, M_XENBLOCKBACK);
2806 xbb->kva_free = NULL;
2811 * Cleanup all inter-domain communication mechanisms.
2813 * \param xbb Per-instance xbb configuration structure.
2816 xbb_disconnect(struct xbb_softc *xbb)
2818 struct gnttab_unmap_grant_ref ops[XBB_MAX_RING_PAGES];
2819 struct gnttab_unmap_grant_ref *op;
2825 if ((xbb->flags & XBBF_RING_CONNECTED) == 0)
2828 if (xbb->irq != 0) {
2829 unbind_from_irqhandler(xbb->irq);
2833 mtx_unlock(&xbb->lock);
2834 taskqueue_drain(xbb->io_taskqueue, &xbb->io_task);
2835 mtx_lock(&xbb->lock);
2838 * No new interrupts can generate work, but we must wait
2839 * for all currently active requests to drain.
2841 if (xbb->active_request_count != 0)
2844 for (ring_idx = 0, op = ops;
2845 ring_idx < xbb->ring_config.ring_pages;
2848 op->host_addr = xbb->ring_config.gnt_addr
2849 + (ring_idx * PAGE_SIZE);
2850 op->dev_bus_addr = xbb->ring_config.bus_addr[ring_idx];
2851 op->handle = xbb->ring_config.handle[ring_idx];
2854 error = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, ops,
2855 xbb->ring_config.ring_pages);
2857 panic("Grant table op failed (%d)", error);
2859 xbb_free_communication_mem(xbb);
2861 if (xbb->requests != NULL) {
2862 free(xbb->requests, M_XENBLOCKBACK);
2863 xbb->requests = NULL;
2866 if (xbb->request_lists != NULL) {
2867 struct xbb_xen_reqlist *reqlist;
2870 /* There is one request list for ever allocated request. */
2871 for (i = 0, reqlist = xbb->request_lists;
2872 i < xbb->max_requests; i++, reqlist++){
2873 #ifdef XBB_USE_BOUNCE_BUFFERS
2874 if (reqlist->bounce != NULL) {
2875 free(reqlist->bounce, M_XENBLOCKBACK);
2876 reqlist->bounce = NULL;
2879 if (reqlist->gnt_handles != NULL) {
2880 free(reqlist->gnt_handles, M_XENBLOCKBACK);
2881 reqlist->gnt_handles = NULL;
2884 free(xbb->request_lists, M_XENBLOCKBACK);
2885 xbb->request_lists = NULL;
2888 xbb->flags &= ~XBBF_RING_CONNECTED;
2893 * Map shared memory ring into domain local address space, initialize
2894 * ring control structures, and bind an interrupt to the event channel
2895 * used to notify us of ring changes.
2897 * \param xbb Per-instance xbb configuration structure.
2900 xbb_connect_ring(struct xbb_softc *xbb)
2902 struct gnttab_map_grant_ref gnts[XBB_MAX_RING_PAGES];
2903 struct gnttab_map_grant_ref *gnt;
2907 if ((xbb->flags & XBBF_RING_CONNECTED) != 0)
2911 * Kva for our ring is at the tail of the region of kva allocated
2912 * by xbb_alloc_communication_mem().
2914 xbb->ring_config.va = xbb->kva
2916 - (xbb->ring_config.ring_pages * PAGE_SIZE));
2917 xbb->ring_config.gnt_addr = xbb->gnt_base_addr
2919 - (xbb->ring_config.ring_pages * PAGE_SIZE));
2921 for (ring_idx = 0, gnt = gnts;
2922 ring_idx < xbb->ring_config.ring_pages;
2923 ring_idx++, gnt++) {
2925 gnt->host_addr = xbb->ring_config.gnt_addr
2926 + (ring_idx * PAGE_SIZE);
2927 gnt->flags = GNTMAP_host_map;
2928 gnt->ref = xbb->ring_config.ring_ref[ring_idx];
2929 gnt->dom = xbb->otherend_id;
2932 error = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, gnts,
2933 xbb->ring_config.ring_pages);
2935 panic("blkback: Ring page grant table op failed (%d)", error);
2937 for (ring_idx = 0, gnt = gnts;
2938 ring_idx < xbb->ring_config.ring_pages;
2939 ring_idx++, gnt++) {
2940 if (gnt->status != 0) {
2941 xbb->ring_config.va = 0;
2942 xenbus_dev_fatal(xbb->dev, EACCES,
2943 "Ring shared page mapping failed. "
2944 "Status %d.", gnt->status);
2947 xbb->ring_config.handle[ring_idx] = gnt->handle;
2948 xbb->ring_config.bus_addr[ring_idx] = gnt->dev_bus_addr;
2951 /* Initialize the ring based on ABI. */
2953 case BLKIF_PROTOCOL_NATIVE:
2955 blkif_sring_t *sring;
2956 sring = (blkif_sring_t *)xbb->ring_config.va;
2957 BACK_RING_INIT(&xbb->rings.native, sring,
2958 xbb->ring_config.ring_pages * PAGE_SIZE);
2961 case BLKIF_PROTOCOL_X86_32:
2963 blkif_x86_32_sring_t *sring_x86_32;
2964 sring_x86_32 = (blkif_x86_32_sring_t *)xbb->ring_config.va;
2965 BACK_RING_INIT(&xbb->rings.x86_32, sring_x86_32,
2966 xbb->ring_config.ring_pages * PAGE_SIZE);
2969 case BLKIF_PROTOCOL_X86_64:
2971 blkif_x86_64_sring_t *sring_x86_64;
2972 sring_x86_64 = (blkif_x86_64_sring_t *)xbb->ring_config.va;
2973 BACK_RING_INIT(&xbb->rings.x86_64, sring_x86_64,
2974 xbb->ring_config.ring_pages * PAGE_SIZE);
2978 panic("Unexpected blkif protocol ABI.");
2981 xbb->flags |= XBBF_RING_CONNECTED;
2984 bind_interdomain_evtchn_to_irqhandler(xbb->otherend_id,
2985 xbb->ring_config.evtchn,
2986 device_get_nameunit(xbb->dev),
2987 xbb_intr, /*arg*/xbb,
2988 INTR_TYPE_BIO | INTR_MPSAFE,
2991 (void)xbb_disconnect(xbb);
2992 xenbus_dev_fatal(xbb->dev, error, "binding event channel");
2996 DPRINTF("rings connected!\n");
3001 /* Needed to make bit_alloc() macro work */
3002 #define calloc(count, size) malloc((count)*(size), M_XENBLOCKBACK, \
3006 * Size KVA and pseudo-physical address allocations based on negotiated
3007 * values for the size and number of I/O requests, and the size of our
3008 * communication ring.
3010 * \param xbb Per-instance xbb configuration structure.
3012 * These address spaces are used to dynamically map pages in the
3013 * front-end's domain into our own.
3016 xbb_alloc_communication_mem(struct xbb_softc *xbb)
3018 xbb->reqlist_kva_pages = xbb->max_requests * xbb->max_request_segments;
3019 xbb->reqlist_kva_size = xbb->reqlist_kva_pages * PAGE_SIZE;
3020 xbb->kva_size = xbb->reqlist_kva_size +
3021 (xbb->ring_config.ring_pages * PAGE_SIZE);
3023 xbb->kva_free = bit_alloc(xbb->reqlist_kva_pages);
3024 if (xbb->kva_free == NULL)
3027 DPRINTF("%s: kva_size = %d, reqlist_kva_size = %d\n",
3028 device_get_nameunit(xbb->dev), xbb->kva_size,
3029 xbb->reqlist_kva_size);
3031 xbb->kva = kmem_alloc_nofault(kernel_map, xbb->kva_size);
3034 xbb->gnt_base_addr = xbb->kva;
3037 * Reserve a range of pseudo physical memory that we can map
3038 * into kva. These pages will only be backed by machine
3039 * pages ("real memory") during the lifetime of front-end requests
3040 * via grant table operations.
3042 xbb->pseudo_phys_res_id = 0;
3043 xbb->pseudo_phys_res = bus_alloc_resource(xbb->dev, SYS_RES_MEMORY,
3044 &xbb->pseudo_phys_res_id,
3045 0, ~0, xbb->kva_size,
3047 if (xbb->pseudo_phys_res == NULL) {
3051 xbb->kva = (vm_offset_t)rman_get_virtual(xbb->pseudo_phys_res);
3052 xbb->gnt_base_addr = rman_get_start(xbb->pseudo_phys_res);
3055 DPRINTF("%s: kva: %#jx, gnt_base_addr: %#jx\n",
3056 device_get_nameunit(xbb->dev), (uintmax_t)xbb->kva,
3057 (uintmax_t)xbb->gnt_base_addr);
3062 * Collect front-end information from the XenStore.
3064 * \param xbb Per-instance xbb configuration structure.
3067 xbb_collect_frontend_info(struct xbb_softc *xbb)
3069 char protocol_abi[64];
3070 const char *otherend_path;
3073 u_int ring_page_order;
3076 otherend_path = xenbus_get_otherend_path(xbb->dev);
3079 * Protocol defaults valid even if all negotiation fails.
3081 xbb->ring_config.ring_pages = 1;
3082 xbb->max_request_segments = BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK;
3083 xbb->max_request_size = xbb->max_request_segments * PAGE_SIZE;
3086 * Mandatory data (used in all versions of the protocol) first.
3088 error = xs_scanf(XST_NIL, otherend_path,
3089 "event-channel", NULL, "%" PRIu32,
3090 &xbb->ring_config.evtchn);
3092 xenbus_dev_fatal(xbb->dev, error,
3093 "Unable to retrieve event-channel information "
3094 "from frontend %s. Unable to connect.",
3095 xenbus_get_otherend_path(xbb->dev));
3100 * These fields are initialized to legacy protocol defaults
3101 * so we only need to fail if reading the updated value succeeds
3102 * and the new value is outside of its allowed range.
3104 * \note xs_gather() returns on the first encountered error, so
3105 * we must use independant calls in order to guarantee
3106 * we don't miss information in a sparsly populated front-end
3109 * \note xs_scanf() does not update variables for unmatched
3112 ring_page_order = 0;
3113 (void)xs_scanf(XST_NIL, otherend_path,
3114 "ring-page-order", NULL, "%u",
3116 xbb->ring_config.ring_pages = 1 << ring_page_order;
3117 (void)xs_scanf(XST_NIL, otherend_path,
3118 "num-ring-pages", NULL, "%u",
3119 &xbb->ring_config.ring_pages);
3120 ring_size = PAGE_SIZE * xbb->ring_config.ring_pages;
3121 xbb->max_requests = BLKIF_MAX_RING_REQUESTS(ring_size);
3123 (void)xs_scanf(XST_NIL, otherend_path,
3124 "max-requests", NULL, "%u",
3125 &xbb->max_requests);
3127 (void)xs_scanf(XST_NIL, otherend_path,
3128 "max-request-segments", NULL, "%u",
3129 &xbb->max_request_segments);
3131 (void)xs_scanf(XST_NIL, otherend_path,
3132 "max-request-size", NULL, "%u",
3133 &xbb->max_request_size);
3135 if (xbb->ring_config.ring_pages > XBB_MAX_RING_PAGES) {
3136 xenbus_dev_fatal(xbb->dev, EINVAL,
3137 "Front-end specified ring-pages of %u "
3138 "exceeds backend limit of %zu. "
3139 "Unable to connect.",
3140 xbb->ring_config.ring_pages,
3141 XBB_MAX_RING_PAGES);
3143 } else if (xbb->max_requests > XBB_MAX_REQUESTS) {
3144 xenbus_dev_fatal(xbb->dev, EINVAL,
3145 "Front-end specified max_requests of %u "
3146 "exceeds backend limit of %u. "
3147 "Unable to connect.",
3151 } else if (xbb->max_request_segments > XBB_MAX_SEGMENTS_PER_REQUEST) {
3152 xenbus_dev_fatal(xbb->dev, EINVAL,
3153 "Front-end specified max_requests_segments "
3154 "of %u exceeds backend limit of %u. "
3155 "Unable to connect.",
3156 xbb->max_request_segments,
3157 XBB_MAX_SEGMENTS_PER_REQUEST);
3159 } else if (xbb->max_request_size > XBB_MAX_REQUEST_SIZE) {
3160 xenbus_dev_fatal(xbb->dev, EINVAL,
3161 "Front-end specified max_request_size "
3162 "of %u exceeds backend limit of %u. "
3163 "Unable to connect.",
3164 xbb->max_request_size,
3165 XBB_MAX_REQUEST_SIZE);
3169 if (xbb->ring_config.ring_pages == 1) {
3170 error = xs_gather(XST_NIL, otherend_path,
3171 "ring-ref", "%" PRIu32,
3172 &xbb->ring_config.ring_ref[0],
3175 xenbus_dev_fatal(xbb->dev, error,
3176 "Unable to retrieve ring information "
3177 "from frontend %s. Unable to "
3179 xenbus_get_otherend_path(xbb->dev));
3183 /* Multi-page ring format. */
3184 for (ring_idx = 0; ring_idx < xbb->ring_config.ring_pages;
3186 char ring_ref_name[]= "ring_refXX";
3188 snprintf(ring_ref_name, sizeof(ring_ref_name),
3189 "ring-ref%u", ring_idx);
3190 error = xs_scanf(XST_NIL, otherend_path,
3191 ring_ref_name, NULL, "%" PRIu32,
3192 &xbb->ring_config.ring_ref[ring_idx]);
3194 xenbus_dev_fatal(xbb->dev, error,
3195 "Failed to retriev grant "
3196 "reference for page %u of "
3197 "shared ring. Unable "
3198 "to connect.", ring_idx);
3204 error = xs_gather(XST_NIL, otherend_path,
3205 "protocol", "%63s", protocol_abi,
3208 || !strcmp(protocol_abi, XEN_IO_PROTO_ABI_NATIVE)) {
3210 * Assume native if the frontend has not
3211 * published ABI data or it has published and
3212 * matches our own ABI.
3214 xbb->abi = BLKIF_PROTOCOL_NATIVE;
3215 } else if (!strcmp(protocol_abi, XEN_IO_PROTO_ABI_X86_32)) {
3217 xbb->abi = BLKIF_PROTOCOL_X86_32;
3218 } else if (!strcmp(protocol_abi, XEN_IO_PROTO_ABI_X86_64)) {
3220 xbb->abi = BLKIF_PROTOCOL_X86_64;
3223 xenbus_dev_fatal(xbb->dev, EINVAL,
3224 "Unknown protocol ABI (%s) published by "
3225 "frontend. Unable to connect.", protocol_abi);
3232 * Allocate per-request data structures given request size and number
3233 * information negotiated with the front-end.
3235 * \param xbb Per-instance xbb configuration structure.
3238 xbb_alloc_requests(struct xbb_softc *xbb)
3240 struct xbb_xen_req *req;
3241 struct xbb_xen_req *last_req;
3244 * Allocate request book keeping datastructures.
3246 xbb->requests = malloc(xbb->max_requests * sizeof(*xbb->requests),
3247 M_XENBLOCKBACK, M_NOWAIT|M_ZERO);
3248 if (xbb->requests == NULL) {
3249 xenbus_dev_fatal(xbb->dev, ENOMEM,
3250 "Unable to allocate request structures");
3254 req = xbb->requests;
3255 last_req = &xbb->requests[xbb->max_requests - 1];
3256 STAILQ_INIT(&xbb->request_free_stailq);
3257 while (req <= last_req) {
3258 STAILQ_INSERT_TAIL(&xbb->request_free_stailq, req, links);
3265 xbb_alloc_request_lists(struct xbb_softc *xbb)
3267 struct xbb_xen_reqlist *reqlist;
3271 * If no requests can be merged, we need 1 request list per
3272 * in flight request.
3274 xbb->request_lists = malloc(xbb->max_requests *
3275 sizeof(*xbb->request_lists), M_XENBLOCKBACK, M_NOWAIT|M_ZERO);
3276 if (xbb->request_lists == NULL) {
3277 xenbus_dev_fatal(xbb->dev, ENOMEM,
3278 "Unable to allocate request list structures");
3282 STAILQ_INIT(&xbb->reqlist_free_stailq);
3283 STAILQ_INIT(&xbb->reqlist_pending_stailq);
3284 for (i = 0; i < xbb->max_requests; i++) {
3287 reqlist = &xbb->request_lists[i];
3291 #ifdef XBB_USE_BOUNCE_BUFFERS
3292 reqlist->bounce = malloc(xbb->max_reqlist_size,
3293 M_XENBLOCKBACK, M_NOWAIT);
3294 if (reqlist->bounce == NULL) {
3295 xenbus_dev_fatal(xbb->dev, ENOMEM,
3296 "Unable to allocate request "
3300 #endif /* XBB_USE_BOUNCE_BUFFERS */
3302 reqlist->gnt_handles = malloc(xbb->max_reqlist_segments *
3303 sizeof(*reqlist->gnt_handles),
3304 M_XENBLOCKBACK, M_NOWAIT|M_ZERO);
3305 if (reqlist->gnt_handles == NULL) {
3306 xenbus_dev_fatal(xbb->dev, ENOMEM,
3307 "Unable to allocate request "
3308 "grant references");
3312 for (seg = 0; seg < xbb->max_reqlist_segments; seg++)
3313 reqlist->gnt_handles[seg] = GRANT_REF_INVALID;
3315 STAILQ_INSERT_TAIL(&xbb->reqlist_free_stailq, reqlist, links);
3321 * Supply information about the physical device to the frontend
3324 * \param xbb Per-instance xbb configuration structure.
3327 xbb_publish_backend_info(struct xbb_softc *xbb)
3329 struct xs_transaction xst;
3330 const char *our_path;
3334 our_path = xenbus_get_node(xbb->dev);
3336 error = xs_transaction_start(&xst);
3338 xenbus_dev_fatal(xbb->dev, error,
3339 "Error publishing backend info "
3340 "(start transaction)");
3345 error = xs_printf(xst, our_path, leaf,
3346 "%"PRIu64, xbb->media_num_sectors);
3350 /* XXX Support all VBD attributes here. */
3352 error = xs_printf(xst, our_path, leaf, "%u",
3353 xbb->flags & XBBF_READ_ONLY
3354 ? VDISK_READONLY : 0);
3358 leaf = "sector-size";
3359 error = xs_printf(xst, our_path, leaf, "%u",
3364 error = xs_transaction_end(xst, 0);
3367 } else if (error != EAGAIN) {
3368 xenbus_dev_fatal(xbb->dev, error, "ending transaction");
3373 xenbus_dev_fatal(xbb->dev, error, "writing %s/%s",
3375 xs_transaction_end(xst, 1);
3380 * Connect to our blkfront peer now that it has completed publishing
3381 * its configuration into the XenStore.
3383 * \param xbb Per-instance xbb configuration structure.
3386 xbb_connect(struct xbb_softc *xbb)
3390 if (xenbus_get_state(xbb->dev) == XenbusStateConnected)
3393 if (xbb_collect_frontend_info(xbb) != 0)
3396 xbb->flags &= ~XBBF_SHUTDOWN;
3399 * We limit the maximum number of reqlist segments to the maximum
3400 * number of segments in the ring, or our absolute maximum,
3401 * whichever is smaller.
3403 xbb->max_reqlist_segments = MIN(xbb->max_request_segments *
3404 xbb->max_requests, XBB_MAX_SEGMENTS_PER_REQLIST);
3407 * The maximum size is simply a function of the number of segments
3410 xbb->max_reqlist_size = xbb->max_reqlist_segments * PAGE_SIZE;
3412 /* Allocate resources whose size depends on front-end configuration. */
3413 error = xbb_alloc_communication_mem(xbb);
3415 xenbus_dev_fatal(xbb->dev, error,
3416 "Unable to allocate communication memory");
3420 error = xbb_alloc_requests(xbb);
3422 /* Specific errors are reported by xbb_alloc_requests(). */
3426 error = xbb_alloc_request_lists(xbb);
3428 /* Specific errors are reported by xbb_alloc_request_lists(). */
3433 * Connect communication channel.
3435 error = xbb_connect_ring(xbb);
3437 /* Specific errors are reported by xbb_connect_ring(). */
3441 if (xbb_publish_backend_info(xbb) != 0) {
3443 * If we can't publish our data, we cannot participate
3444 * in this connection, and waiting for a front-end state
3445 * change will not help the situation.
3447 (void)xbb_disconnect(xbb);
3451 /* Ready for I/O. */
3452 xenbus_set_state(xbb->dev, XenbusStateConnected);
3455 /*-------------------------- Device Teardown Support -------------------------*/
3457 * Perform device shutdown functions.
3459 * \param xbb Per-instance xbb configuration structure.
3461 * Mark this instance as shutting down, wait for any active I/O on the
3462 * backend device/file to drain, disconnect from the front-end, and notify
3463 * any waiters (e.g. a thread invoking our detach method) that detach can
3467 xbb_shutdown(struct xbb_softc *xbb)
3469 XenbusState frontState;
3475 * Due to the need to drop our mutex during some
3476 * xenbus operations, it is possible for two threads
3477 * to attempt to close out shutdown processing at
3478 * the same time. Tell the caller that hits this
3479 * race to try back later.
3481 if ((xbb->flags & XBBF_IN_SHUTDOWN) != 0)
3484 xbb->flags |= XBBF_IN_SHUTDOWN;
3485 mtx_unlock(&xbb->lock);
3487 if (xenbus_get_state(xbb->dev) < XenbusStateClosing)
3488 xenbus_set_state(xbb->dev, XenbusStateClosing);
3490 frontState = xenbus_get_otherend_state(xbb->dev);
3491 mtx_lock(&xbb->lock);
3492 xbb->flags &= ~XBBF_IN_SHUTDOWN;
3494 /* The front can submit I/O until entering the closed state. */
3495 if (frontState < XenbusStateClosed)
3500 /* Indicate shutdown is in progress. */
3501 xbb->flags |= XBBF_SHUTDOWN;
3503 /* Disconnect from the front-end. */
3504 error = xbb_disconnect(xbb);
3507 * Requests still outstanding. We'll be called again
3508 * once they complete.
3510 KASSERT(error == EAGAIN,
3511 ("%s: Unexpected xbb_disconnect() failure %d",
3519 /* Indicate to xbb_detach() that is it safe to proceed. */
3526 * Report an attach time error to the console and Xen, and cleanup
3527 * this instance by forcing immediate detach processing.
3529 * \param xbb Per-instance xbb configuration structure.
3530 * \param err Errno describing the error.
3531 * \param fmt Printf style format and arguments
3534 xbb_attach_failed(struct xbb_softc *xbb, int err, const char *fmt, ...)
3540 va_copy(ap_hotplug, ap);
3541 xs_vprintf(XST_NIL, xenbus_get_node(xbb->dev),
3542 "hotplug-error", fmt, ap_hotplug);
3544 xs_printf(XST_NIL, xenbus_get_node(xbb->dev),
3545 "hotplug-status", "error");
3547 xenbus_dev_vfatal(xbb->dev, err, fmt, ap);
3550 xs_printf(XST_NIL, xenbus_get_node(xbb->dev),
3552 xbb_detach(xbb->dev);
3555 /*---------------------------- NewBus Entrypoints ----------------------------*/
3557 * Inspect a XenBus device and claim it if is of the appropriate type.
3559 * \param dev NewBus device object representing a candidate XenBus device.
3561 * \return 0 for success, errno codes for failure.
3564 xbb_probe(device_t dev)
3567 if (!strcmp(xenbus_get_type(dev), "vbd")) {
3568 device_set_desc(dev, "Backend Virtual Block Device");
3577 * Setup sysctl variables to control various Block Back parameters.
3579 * \param xbb Xen Block Back softc.
3583 xbb_setup_sysctl(struct xbb_softc *xbb)
3585 struct sysctl_ctx_list *sysctl_ctx = NULL;
3586 struct sysctl_oid *sysctl_tree = NULL;
3588 sysctl_ctx = device_get_sysctl_ctx(xbb->dev);
3589 if (sysctl_ctx == NULL)
3592 sysctl_tree = device_get_sysctl_tree(xbb->dev);
3593 if (sysctl_tree == NULL)
3596 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
3597 "disable_flush", CTLFLAG_RW, &xbb->disable_flush, 0,
3598 "fake the flush command");
3600 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
3601 "flush_interval", CTLFLAG_RW, &xbb->flush_interval, 0,
3602 "send a real flush for N flush requests");
3604 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
3605 "no_coalesce_reqs", CTLFLAG_RW, &xbb->no_coalesce_reqs,0,
3606 "Don't coalesce contiguous requests");
3608 SYSCTL_ADD_UQUAD(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
3609 "reqs_received", CTLFLAG_RW, &xbb->reqs_received,
3610 "how many I/O requests we have received");
3612 SYSCTL_ADD_UQUAD(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
3613 "reqs_completed", CTLFLAG_RW, &xbb->reqs_completed,
3614 "how many I/O requests have been completed");
3616 SYSCTL_ADD_UQUAD(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
3617 "forced_dispatch", CTLFLAG_RW, &xbb->forced_dispatch,
3618 "how many I/O dispatches were forced");
3620 SYSCTL_ADD_UQUAD(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
3621 "normal_dispatch", CTLFLAG_RW, &xbb->normal_dispatch,
3622 "how many I/O dispatches were normal");
3624 SYSCTL_ADD_UQUAD(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
3625 "total_dispatch", CTLFLAG_RW, &xbb->total_dispatch,
3626 "total number of I/O dispatches");
3628 SYSCTL_ADD_UQUAD(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
3629 "kva_shortages", CTLFLAG_RW, &xbb->kva_shortages,
3630 "how many times we have run out of KVA");
3632 SYSCTL_ADD_UQUAD(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
3633 "request_shortages", CTLFLAG_RW,
3634 &xbb->request_shortages,
3635 "how many times we have run out of requests");
3637 SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
3638 "max_requests", CTLFLAG_RD, &xbb->max_requests, 0,
3639 "maximum outstanding requests (negotiated)");
3641 SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
3642 "max_request_segments", CTLFLAG_RD,
3643 &xbb->max_request_segments, 0,
3644 "maximum number of pages per requests (negotiated)");
3646 SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
3647 "max_request_size", CTLFLAG_RD,
3648 &xbb->max_request_size, 0,
3649 "maximum size in bytes of a request (negotiated)");
3651 SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
3652 "ring_pages", CTLFLAG_RD,
3653 &xbb->ring_config.ring_pages, 0,
3654 "communication channel pages (negotiated)");
3658 * Attach to a XenBus device that has been claimed by our probe routine.
3660 * \param dev NewBus device object representing this Xen Block Back instance.
3662 * \return 0 for success, errno codes for failure.
3665 xbb_attach(device_t dev)
3667 struct xbb_softc *xbb;
3669 u_int max_ring_page_order;
3671 DPRINTF("Attaching to %s\n", xenbus_get_node(dev));
3674 * Basic initialization.
3675 * After this block it is safe to call xbb_detach()
3676 * to clean up any allocated data for this instance.
3678 xbb = device_get_softc(dev);
3680 xbb->otherend_id = xenbus_get_otherend_id(dev);
3681 TASK_INIT(&xbb->io_task, /*priority*/0, xbb_run_queue, xbb);
3682 mtx_init(&xbb->lock, device_get_nameunit(dev), NULL, MTX_DEF);
3685 * Publish protocol capabilities for consumption by the
3688 error = xs_printf(XST_NIL, xenbus_get_node(xbb->dev),
3689 "feature-barrier", "1");
3691 xbb_attach_failed(xbb, error, "writing %s/feature-barrier",
3692 xenbus_get_node(xbb->dev));
3696 error = xs_printf(XST_NIL, xenbus_get_node(xbb->dev),
3697 "feature-flush-cache", "1");
3699 xbb_attach_failed(xbb, error, "writing %s/feature-flush-cache",
3700 xenbus_get_node(xbb->dev));
3705 * Amazon EC2 client compatility. They refer to max-ring-pages
3706 * instead of to max-ring-page-order.
3708 error = xs_printf(XST_NIL, xenbus_get_node(xbb->dev),
3709 "max-ring-pages", "%zu", XBB_MAX_RING_PAGES);
3711 xbb_attach_failed(xbb, error, "writing %s/max-ring-pages",
3712 xenbus_get_node(xbb->dev));
3716 max_ring_page_order = flsl(XBB_MAX_RING_PAGES) - 1;
3717 error = xs_printf(XST_NIL, xenbus_get_node(xbb->dev),
3718 "max-ring-page-order", "%u", max_ring_page_order);
3720 xbb_attach_failed(xbb, error, "writing %s/max-ring-page-order",
3721 xenbus_get_node(xbb->dev));
3725 error = xs_printf(XST_NIL, xenbus_get_node(xbb->dev),
3726 "max-requests", "%u", XBB_MAX_REQUESTS);
3728 xbb_attach_failed(xbb, error, "writing %s/max-requests",
3729 xenbus_get_node(xbb->dev));
3733 error = xs_printf(XST_NIL, xenbus_get_node(xbb->dev),
3734 "max-request-segments", "%u",
3735 XBB_MAX_SEGMENTS_PER_REQUEST);
3737 xbb_attach_failed(xbb, error, "writing %s/max-request-segments",
3738 xenbus_get_node(xbb->dev));
3742 error = xs_printf(XST_NIL, xenbus_get_node(xbb->dev),
3743 "max-request-size", "%u",
3744 XBB_MAX_REQUEST_SIZE);
3746 xbb_attach_failed(xbb, error, "writing %s/max-request-size",
3747 xenbus_get_node(xbb->dev));
3751 /* Collect physical device information. */
3752 error = xs_gather(XST_NIL, xenbus_get_otherend_path(xbb->dev),
3753 "device-type", NULL, &xbb->dev_type,
3756 xbb->dev_type = NULL;
3758 error = xs_gather(XST_NIL, xenbus_get_node(dev),
3759 "mode", NULL, &xbb->dev_mode,
3760 "params", NULL, &xbb->dev_name,
3763 xbb_attach_failed(xbb, error, "reading backend fields at %s",
3764 xenbus_get_node(dev));
3768 /* Parse fopen style mode flags. */
3769 if (strchr(xbb->dev_mode, 'w') == NULL)
3770 xbb->flags |= XBBF_READ_ONLY;
3773 * Verify the physical device is present and can support
3774 * the desired I/O mode.
3777 error = xbb_open_backend(xbb);
3780 xbb_attach_failed(xbb, error, "Unable to open %s",
3785 /* Use devstat(9) for recording statistics. */
3786 xbb->xbb_stats = devstat_new_entry("xbb", device_get_unit(xbb->dev),
3788 DEVSTAT_ALL_SUPPORTED,
3790 | DEVSTAT_TYPE_IF_OTHER,
3791 DEVSTAT_PRIORITY_OTHER);
3793 xbb->xbb_stats_in = devstat_new_entry("xbbi", device_get_unit(xbb->dev),
3795 DEVSTAT_ALL_SUPPORTED,
3797 | DEVSTAT_TYPE_IF_OTHER,
3798 DEVSTAT_PRIORITY_OTHER);
3800 * Setup sysctl variables.
3802 xbb_setup_sysctl(xbb);
3805 * Create a taskqueue for doing work that must occur from a
3808 xbb->io_taskqueue = taskqueue_create(device_get_nameunit(dev), M_NOWAIT,
3809 taskqueue_thread_enqueue,
3810 /*context*/&xbb->io_taskqueue);
3811 if (xbb->io_taskqueue == NULL) {
3812 xbb_attach_failed(xbb, error, "Unable to create taskqueue");
3816 taskqueue_start_threads(&xbb->io_taskqueue,
3820 "%s taskq", device_get_nameunit(dev));
3822 /* Update hot-plug status to satisfy xend. */
3823 error = xs_printf(XST_NIL, xenbus_get_node(xbb->dev),
3824 "hotplug-status", "connected");
3826 xbb_attach_failed(xbb, error, "writing %s/hotplug-status",
3827 xenbus_get_node(xbb->dev));
3831 /* Tell the front end that we are ready to connect. */
3832 xenbus_set_state(dev, XenbusStateInitWait);
3838 * Detach from a block back device instance.
3840 * \param dev NewBus device object representing this Xen Block Back instance.
3842 * \return 0 for success, errno codes for failure.
3844 * \note A block back device may be detached at any time in its life-cycle,
3845 * including part way through the attach process. For this reason,
3846 * initialization order and the intialization state checks in this
3847 * routine must be carefully coupled so that attach time failures
3848 * are gracefully handled.
3851 xbb_detach(device_t dev)
3853 struct xbb_softc *xbb;
3857 xbb = device_get_softc(dev);
3858 mtx_lock(&xbb->lock);
3859 while (xbb_shutdown(xbb) == EAGAIN) {
3860 msleep(xbb, &xbb->lock, /*wakeup prio unchanged*/0,
3863 mtx_unlock(&xbb->lock);
3867 if (xbb->io_taskqueue != NULL)
3868 taskqueue_free(xbb->io_taskqueue);
3870 if (xbb->xbb_stats != NULL)
3871 devstat_remove_entry(xbb->xbb_stats);
3873 if (xbb->xbb_stats_in != NULL)
3874 devstat_remove_entry(xbb->xbb_stats_in);
3876 xbb_close_backend(xbb);
3878 if (xbb->dev_mode != NULL) {
3879 free(xbb->dev_mode, M_XENBUS);
3880 xbb->dev_mode = NULL;
3883 if (xbb->dev_type != NULL) {
3884 free(xbb->dev_type, M_XENBUS);
3885 xbb->dev_type = NULL;
3888 if (xbb->dev_name != NULL) {
3889 free(xbb->dev_name, M_XENBUS);
3890 xbb->dev_name = NULL;
3893 mtx_destroy(&xbb->lock);
3898 * Prepare this block back device for suspension of this VM.
3900 * \param dev NewBus device object representing this Xen Block Back instance.
3902 * \return 0 for success, errno codes for failure.
3905 xbb_suspend(device_t dev)
3908 struct xbb_softc *sc = device_get_softc(dev);
3910 /* Prevent new requests being issued until we fix things up. */
3911 mtx_lock(&sc->xb_io_lock);
3912 sc->connected = BLKIF_STATE_SUSPENDED;
3913 mtx_unlock(&sc->xb_io_lock);
3920 * Perform any processing required to recover from a suspended state.
3922 * \param dev NewBus device object representing this Xen Block Back instance.
3924 * \return 0 for success, errno codes for failure.
3927 xbb_resume(device_t dev)
3933 * Handle state changes expressed via the XenStore by our front-end peer.
3935 * \param dev NewBus device object representing this Xen
3936 * Block Back instance.
3937 * \param frontend_state The new state of the front-end.
3939 * \return 0 for success, errno codes for failure.
3942 xbb_frontend_changed(device_t dev, XenbusState frontend_state)
3944 struct xbb_softc *xbb = device_get_softc(dev);
3946 DPRINTF("frontend_state=%s, xbb_state=%s\n",
3947 xenbus_strstate(frontend_state),
3948 xenbus_strstate(xenbus_get_state(xbb->dev)));
3950 switch (frontend_state) {
3951 case XenbusStateInitialising:
3953 case XenbusStateInitialised:
3954 case XenbusStateConnected:
3957 case XenbusStateClosing:
3958 case XenbusStateClosed:
3959 mtx_lock(&xbb->lock);
3961 mtx_unlock(&xbb->lock);
3962 if (frontend_state == XenbusStateClosed)
3963 xenbus_set_state(xbb->dev, XenbusStateClosed);
3966 xenbus_dev_fatal(xbb->dev, EINVAL, "saw state %d at frontend",
3972 /*---------------------------- NewBus Registration ---------------------------*/
3973 static device_method_t xbb_methods[] = {
3974 /* Device interface */
3975 DEVMETHOD(device_probe, xbb_probe),
3976 DEVMETHOD(device_attach, xbb_attach),
3977 DEVMETHOD(device_detach, xbb_detach),
3978 DEVMETHOD(device_shutdown, bus_generic_shutdown),
3979 DEVMETHOD(device_suspend, xbb_suspend),
3980 DEVMETHOD(device_resume, xbb_resume),
3982 /* Xenbus interface */
3983 DEVMETHOD(xenbus_otherend_changed, xbb_frontend_changed),
3988 static driver_t xbb_driver = {
3991 sizeof(struct xbb_softc),
3993 devclass_t xbb_devclass;
3995 DRIVER_MODULE(xbbd, xenbusb_back, xbb_driver, xbb_devclass, 0, 0);