xref: /freebsd/sys/dev/xen/blkfront/blkfront.c (revision 83c2fa73e682235d96a356991ed9028d782c5796)
189e0f4d2SKip Macy /*
28698b76cSKip Macy  * XenBSD block device driver
38698b76cSKip Macy  *
433eebb6aSJustin T. Gibbs  * Copyright (c) 2010-2013 Spectra Logic Corporation
5e4808c4bSKip Macy  * Copyright (c) 2009 Scott Long, Yahoo!
68698b76cSKip Macy  * Copyright (c) 2009 Frank Suchomel, Citrix
79999d2cbSKip Macy  * Copyright (c) 2009 Doug F. Rabson, Citrix
89999d2cbSKip Macy  * Copyright (c) 2005 Kip Macy
99999d2cbSKip Macy  * Copyright (c) 2003-2004, Keir Fraser & Steve Hand
109999d2cbSKip Macy  * Modifications by Mark A. Williamson are (c) Intel Research Cambridge
119999d2cbSKip Macy  *
129999d2cbSKip Macy  *
139999d2cbSKip Macy  * Permission is hereby granted, free of charge, to any person obtaining a copy
149999d2cbSKip Macy  * of this software and associated documentation files (the "Software"), to
159999d2cbSKip Macy  * deal in the Software without restriction, including without limitation the
169999d2cbSKip Macy  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
179999d2cbSKip Macy  * sell copies of the Software, and to permit persons to whom the Software is
189999d2cbSKip Macy  * furnished to do so, subject to the following conditions:
199999d2cbSKip Macy  *
209999d2cbSKip Macy  * The above copyright notice and this permission notice shall be included in
219999d2cbSKip Macy  * all copies or substantial portions of the Software.
229999d2cbSKip Macy  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
239999d2cbSKip Macy  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
249999d2cbSKip Macy  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
259999d2cbSKip Macy  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
269999d2cbSKip Macy  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
279999d2cbSKip Macy  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
289999d2cbSKip Macy  * DEALINGS IN THE SOFTWARE.
2989e0f4d2SKip Macy  */
3089e0f4d2SKip Macy 
3189e0f4d2SKip Macy #include <sys/cdefs.h>
3289e0f4d2SKip Macy __FBSDID("$FreeBSD$");
3389e0f4d2SKip Macy 
3489e0f4d2SKip Macy #include <sys/param.h>
3589e0f4d2SKip Macy #include <sys/systm.h>
3689e0f4d2SKip Macy #include <sys/malloc.h>
3789e0f4d2SKip Macy #include <sys/kernel.h>
3889e0f4d2SKip Macy #include <vm/vm.h>
3989e0f4d2SKip Macy #include <vm/pmap.h>
4089e0f4d2SKip Macy 
4189e0f4d2SKip Macy #include <sys/bio.h>
4289e0f4d2SKip Macy #include <sys/bus.h>
4389e0f4d2SKip Macy #include <sys/conf.h>
4489e0f4d2SKip Macy #include <sys/module.h>
458b8bfa35SJustin T. Gibbs #include <sys/sysctl.h>
4689e0f4d2SKip Macy 
4789e0f4d2SKip Macy #include <machine/bus.h>
4889e0f4d2SKip Macy #include <sys/rman.h>
4989e0f4d2SKip Macy #include <machine/resource.h>
5089e0f4d2SKip Macy #include <machine/intr_machdep.h>
5189e0f4d2SKip Macy #include <machine/vmparam.h>
5289e0f4d2SKip Macy 
5376acc41fSJustin T. Gibbs #include <xen/xen-os.h>
5412678024SDoug Rabson #include <xen/hypervisor.h>
553a6d1fcfSKip Macy #include <xen/xen_intr.h>
5612678024SDoug Rabson #include <xen/gnttab.h>
5789e0f4d2SKip Macy #include <xen/interface/grant_table.h>
5823dc5621SKip Macy #include <xen/interface/io/protocols.h>
5923dc5621SKip Macy #include <xen/xenbus/xenbusvar.h>
6089e0f4d2SKip Macy 
6176acc41fSJustin T. Gibbs #include <machine/_inttypes.h>
6276acc41fSJustin T. Gibbs 
6389e0f4d2SKip Macy #include <geom/geom_disk.h>
6489e0f4d2SKip Macy 
6589e0f4d2SKip Macy #include <dev/xen/blkfront/block.h>
6689e0f4d2SKip Macy 
6723dc5621SKip Macy #include "xenbus_if.h"
6823dc5621SKip Macy 
69fac3fd80SJustin T. Gibbs /*--------------------------- Forward Declarations ---------------------------*/
7033eebb6aSJustin T. Gibbs static void xbd_closing(device_t);
71fac3fd80SJustin T. Gibbs static void xbd_startio(struct xbd_softc *sc);
7289e0f4d2SKip Macy 
73fac3fd80SJustin T. Gibbs /*---------------------------------- Macros ----------------------------------*/
74fac3fd80SJustin T. Gibbs #if 0
75fac3fd80SJustin T. Gibbs #define DPRINTK(fmt, args...) printf("[XEN] %s:%d: " fmt ".\n", __func__, __LINE__, ##args)
76fac3fd80SJustin T. Gibbs #else
77fac3fd80SJustin T. Gibbs #define DPRINTK(fmt, args...)
78fac3fd80SJustin T. Gibbs #endif
79fac3fd80SJustin T. Gibbs 
80fac3fd80SJustin T. Gibbs #define XBD_SECTOR_SHFT		9
81ff662b5cSJustin T. Gibbs 
82fac3fd80SJustin T. Gibbs /*---------------------------- Global Static Data ----------------------------*/
83fac3fd80SJustin T. Gibbs static MALLOC_DEFINE(M_XENBLOCKFRONT, "xbd", "Xen Block Front driver data");
8489e0f4d2SKip Macy 
85aaebf690SColin Percival static int xbd_enable_indirect = 1;
86aaebf690SColin Percival SYSCTL_NODE(_hw, OID_AUTO, xbd, CTLFLAG_RD, 0, "xbd driver parameters");
87aaebf690SColin Percival SYSCTL_INT(_hw_xbd, OID_AUTO, xbd_enable_indirect, CTLFLAG_RDTUN,
88aaebf690SColin Percival     &xbd_enable_indirect, 0, "Enable xbd indirect segments");
89aaebf690SColin Percival 
90fac3fd80SJustin T. Gibbs /*---------------------------- Command Processing ----------------------------*/
91127a9483SJustin T. Gibbs static void
92127a9483SJustin T. Gibbs xbd_freeze(struct xbd_softc *sc, xbd_flag_t xbd_flag)
93127a9483SJustin T. Gibbs {
94127a9483SJustin T. Gibbs 	if (xbd_flag != XBDF_NONE && (sc->xbd_flags & xbd_flag) != 0)
95127a9483SJustin T. Gibbs 		return;
96127a9483SJustin T. Gibbs 
97127a9483SJustin T. Gibbs 	sc->xbd_flags |= xbd_flag;
98127a9483SJustin T. Gibbs 	sc->xbd_qfrozen_cnt++;
99127a9483SJustin T. Gibbs }
100127a9483SJustin T. Gibbs 
101127a9483SJustin T. Gibbs static void
102127a9483SJustin T. Gibbs xbd_thaw(struct xbd_softc *sc, xbd_flag_t xbd_flag)
103127a9483SJustin T. Gibbs {
104127a9483SJustin T. Gibbs 	if (xbd_flag != XBDF_NONE && (sc->xbd_flags & xbd_flag) == 0)
105127a9483SJustin T. Gibbs 		return;
106127a9483SJustin T. Gibbs 
107b834eea6SJustin T. Gibbs 	if (sc->xbd_qfrozen_cnt == 0)
108127a9483SJustin T. Gibbs 		panic("%s: Thaw with flag 0x%x while not frozen.",
109127a9483SJustin T. Gibbs 		    __func__, xbd_flag);
110127a9483SJustin T. Gibbs 
111127a9483SJustin T. Gibbs 	sc->xbd_flags &= ~xbd_flag;
112127a9483SJustin T. Gibbs 	sc->xbd_qfrozen_cnt--;
113127a9483SJustin T. Gibbs }
114127a9483SJustin T. Gibbs 
1159985113bSJustin T. Gibbs static void
1169985113bSJustin T. Gibbs xbd_cm_freeze(struct xbd_softc *sc, struct xbd_command *cm, xbdc_flag_t cm_flag)
1179985113bSJustin T. Gibbs {
1189985113bSJustin T. Gibbs 	if ((cm->cm_flags & XBDCF_FROZEN) != 0)
1199985113bSJustin T. Gibbs 		return;
1209985113bSJustin T. Gibbs 
1219985113bSJustin T. Gibbs 	cm->cm_flags |= XBDCF_FROZEN|cm_flag;
1229985113bSJustin T. Gibbs 	xbd_freeze(sc, XBDF_NONE);
1239985113bSJustin T. Gibbs }
1249985113bSJustin T. Gibbs 
1259985113bSJustin T. Gibbs static void
1269985113bSJustin T. Gibbs xbd_cm_thaw(struct xbd_softc *sc, struct xbd_command *cm)
1279985113bSJustin T. Gibbs {
1289985113bSJustin T. Gibbs 	if ((cm->cm_flags & XBDCF_FROZEN) == 0)
1299985113bSJustin T. Gibbs 		return;
1309985113bSJustin T. Gibbs 
1319985113bSJustin T. Gibbs 	cm->cm_flags &= ~XBDCF_FROZEN;
1329985113bSJustin T. Gibbs 	xbd_thaw(sc, XBDF_NONE);
1339985113bSJustin T. Gibbs }
1349985113bSJustin T. Gibbs 
135fac3fd80SJustin T. Gibbs static inline void
136cdf5d66fSJustin T. Gibbs xbd_flush_requests(struct xbd_softc *sc)
137fac3fd80SJustin T. Gibbs {
138fac3fd80SJustin T. Gibbs 	int notify;
139e4808c4bSKip Macy 
140fac3fd80SJustin T. Gibbs 	RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&sc->xbd_ring, notify);
14189e0f4d2SKip Macy 
142fac3fd80SJustin T. Gibbs 	if (notify)
14376acc41fSJustin T. Gibbs 		xen_intr_signal(sc->xen_intr_handle);
144fac3fd80SJustin T. Gibbs }
14589e0f4d2SKip Macy 
14623dc5621SKip Macy static void
147fac3fd80SJustin T. Gibbs xbd_free_command(struct xbd_command *cm)
14823dc5621SKip Macy {
14923dc5621SKip Macy 
150e2c1fe90SJustin T. Gibbs 	KASSERT((cm->cm_flags & XBDCF_Q_MASK) == XBD_Q_NONE,
151e2c1fe90SJustin T. Gibbs 	    ("Freeing command that is still on queue %d.",
152e2c1fe90SJustin T. Gibbs 	    cm->cm_flags & XBDCF_Q_MASK));
15323dc5621SKip Macy 
154e2c1fe90SJustin T. Gibbs 	cm->cm_flags = XBDCF_INITIALIZER;
155fac3fd80SJustin T. Gibbs 	cm->cm_bp = NULL;
156fac3fd80SJustin T. Gibbs 	cm->cm_complete = NULL;
157e2c1fe90SJustin T. Gibbs 	xbd_enqueue_cm(cm, XBD_Q_FREE);
158127a9483SJustin T. Gibbs 	xbd_thaw(cm->cm_sc, XBDF_CM_SHORTAGE);
15923dc5621SKip Macy }
16023dc5621SKip Macy 
16189e0f4d2SKip Macy static void
1627a70e152SColin Percival xbd_mksegarray(bus_dma_segment_t *segs, int nsegs,
163d0ecc14dSColin Percival     grant_ref_t * gref_head, int otherend_id, int readonly,
1641a52c105SRoger Pau Monné     grant_ref_t * sg_ref, struct blkif_request_segment *sg)
165d0ecc14dSColin Percival {
166d0ecc14dSColin Percival 	struct blkif_request_segment *last_block_sg = sg + nsegs;
167d0ecc14dSColin Percival 	vm_paddr_t buffer_ma;
168d0ecc14dSColin Percival 	uint64_t fsect, lsect;
169d0ecc14dSColin Percival 	int ref;
170d0ecc14dSColin Percival 
171d0ecc14dSColin Percival 	while (sg < last_block_sg) {
172cbb261aeSColin Percival 		KASSERT(segs->ds_addr % (1 << XBD_SECTOR_SHFT) == 0,
173cbb261aeSColin Percival 		    ("XEN disk driver I/O must be sector aligned"));
174cbb261aeSColin Percival 		KASSERT(segs->ds_len % (1 << XBD_SECTOR_SHFT) == 0,
175cbb261aeSColin Percival 		    ("XEN disk driver I/Os must be a multiple of "
176cbb261aeSColin Percival 		    "the sector length"));
177d0ecc14dSColin Percival 		buffer_ma = segs->ds_addr;
178d0ecc14dSColin Percival 		fsect = (buffer_ma & PAGE_MASK) >> XBD_SECTOR_SHFT;
179d0ecc14dSColin Percival 		lsect = fsect + (segs->ds_len  >> XBD_SECTOR_SHFT) - 1;
180d0ecc14dSColin Percival 
181d0ecc14dSColin Percival 		KASSERT(lsect <= 7, ("XEN disk driver data cannot "
182d0ecc14dSColin Percival 		    "cross a page boundary"));
183d0ecc14dSColin Percival 
184d0ecc14dSColin Percival 		/* install a grant reference. */
185d0ecc14dSColin Percival 		ref = gnttab_claim_grant_reference(gref_head);
186d0ecc14dSColin Percival 
187d0ecc14dSColin Percival 		/*
188d0ecc14dSColin Percival 		 * GNTTAB_LIST_END == 0xffffffff, but it is private
189d0ecc14dSColin Percival 		 * to gnttab.c.
190d0ecc14dSColin Percival 		 */
191d0ecc14dSColin Percival 		KASSERT(ref != ~0, ("grant_reference failed"));
192d0ecc14dSColin Percival 
193d0ecc14dSColin Percival 		gnttab_grant_foreign_access_ref(
194d0ecc14dSColin Percival 		    ref,
195d0ecc14dSColin Percival 		    otherend_id,
196d0ecc14dSColin Percival 		    buffer_ma >> PAGE_SHIFT,
197d0ecc14dSColin Percival 		    readonly);
198d0ecc14dSColin Percival 
199d0ecc14dSColin Percival 		*sg_ref = ref;
200d0ecc14dSColin Percival 		*sg = (struct blkif_request_segment) {
201d0ecc14dSColin Percival 			.gref       = ref,
202d0ecc14dSColin Percival 			.first_sect = fsect,
203d0ecc14dSColin Percival 			.last_sect  = lsect
204d0ecc14dSColin Percival 		};
205d0ecc14dSColin Percival 		sg++;
206d0ecc14dSColin Percival 		sg_ref++;
207d0ecc14dSColin Percival 		segs++;
208d0ecc14dSColin Percival 	}
209d0ecc14dSColin Percival }
210d0ecc14dSColin Percival 
211d0ecc14dSColin Percival static void
212fac3fd80SJustin T. Gibbs xbd_queue_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
21389e0f4d2SKip Macy {
214fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc;
215fac3fd80SJustin T. Gibbs 	struct xbd_command *cm;
216fac3fd80SJustin T. Gibbs 	int op;
21789e0f4d2SKip Macy 
218fac3fd80SJustin T. Gibbs 	cm = arg;
219fac3fd80SJustin T. Gibbs 	sc = cm->cm_sc;
220fac3fd80SJustin T. Gibbs 
221fac3fd80SJustin T. Gibbs 	if (error) {
222fac3fd80SJustin T. Gibbs 		cm->cm_bp->bio_error = EIO;
223fac3fd80SJustin T. Gibbs 		biodone(cm->cm_bp);
224fac3fd80SJustin T. Gibbs 		xbd_free_command(cm);
22589e0f4d2SKip Macy 		return;
22689e0f4d2SKip Macy 	}
22789e0f4d2SKip Macy 
228aaebf690SColin Percival 	KASSERT(nsegs <= sc->xbd_max_request_segments,
229d33a1217SColin Percival 	    ("Too many segments in a blkfront I/O"));
230d33a1217SColin Percival 
231aaebf690SColin Percival 	if (nsegs <= BLKIF_MAX_SEGMENTS_PER_REQUEST) {
232aaebf690SColin Percival 		blkif_request_t	*ring_req;
233aaebf690SColin Percival 
234aaebf690SColin Percival 		/* Fill out a blkif_request_t structure. */
235aaebf690SColin Percival 		ring_req = (blkif_request_t *)
236aaebf690SColin Percival 		    RING_GET_REQUEST(&sc->xbd_ring, sc->xbd_ring.req_prod_pvt);
237fac3fd80SJustin T. Gibbs 		sc->xbd_ring.req_prod_pvt++;
238fac3fd80SJustin T. Gibbs 		ring_req->id = cm->cm_id;
239fac3fd80SJustin T. Gibbs 		ring_req->operation = cm->cm_operation;
240fac3fd80SJustin T. Gibbs 		ring_req->sector_number = cm->cm_sector_number;
241fac3fd80SJustin T. Gibbs 		ring_req->handle = (blkif_vdev_t)(uintptr_t)sc->xbd_disk;
242fac3fd80SJustin T. Gibbs 		ring_req->nr_segments = nsegs;
243fac3fd80SJustin T. Gibbs 		cm->cm_nseg = nsegs;
2447a70e152SColin Percival 		xbd_mksegarray(segs, nsegs, &cm->cm_gref_head,
245fac3fd80SJustin T. Gibbs 		    xenbus_get_otherend_id(sc->xbd_dev),
246d0ecc14dSColin Percival 		    cm->cm_operation == BLKIF_OP_WRITE,
247d0ecc14dSColin Percival 		    cm->cm_sg_refs, ring_req->seg);
248aaebf690SColin Percival 	} else {
249aaebf690SColin Percival 		blkif_request_indirect_t *ring_req;
250aaebf690SColin Percival 
251aaebf690SColin Percival 		/* Fill out a blkif_request_indirect_t structure. */
252aaebf690SColin Percival 		ring_req = (blkif_request_indirect_t *)
253aaebf690SColin Percival 		    RING_GET_REQUEST(&sc->xbd_ring, sc->xbd_ring.req_prod_pvt);
254aaebf690SColin Percival 		sc->xbd_ring.req_prod_pvt++;
255aaebf690SColin Percival 		ring_req->id = cm->cm_id;
256aaebf690SColin Percival 		ring_req->operation = BLKIF_OP_INDIRECT;
257aaebf690SColin Percival 		ring_req->indirect_op = cm->cm_operation;
258aaebf690SColin Percival 		ring_req->sector_number = cm->cm_sector_number;
259aaebf690SColin Percival 		ring_req->handle = (blkif_vdev_t)(uintptr_t)sc->xbd_disk;
260aaebf690SColin Percival 		ring_req->nr_segments = nsegs;
261aaebf690SColin Percival 		cm->cm_nseg = nsegs;
262aaebf690SColin Percival 		xbd_mksegarray(segs, nsegs, &cm->cm_gref_head,
263aaebf690SColin Percival 		    xenbus_get_otherend_id(sc->xbd_dev),
264aaebf690SColin Percival 		    cm->cm_operation == BLKIF_OP_WRITE,
265aaebf690SColin Percival 		    cm->cm_sg_refs, cm->cm_indirectionpages);
266aaebf690SColin Percival 		memcpy(ring_req->indirect_grefs, &cm->cm_indirectionrefs,
267aaebf690SColin Percival 		    sizeof(grant_ref_t) * sc->xbd_max_request_indirectpages);
268aaebf690SColin Percival 	}
269fac3fd80SJustin T. Gibbs 
270fac3fd80SJustin T. Gibbs 	if (cm->cm_operation == BLKIF_OP_READ)
271fac3fd80SJustin T. Gibbs 		op = BUS_DMASYNC_PREREAD;
272fac3fd80SJustin T. Gibbs 	else if (cm->cm_operation == BLKIF_OP_WRITE)
273fac3fd80SJustin T. Gibbs 		op = BUS_DMASYNC_PREWRITE;
274fac3fd80SJustin T. Gibbs 	else
275fac3fd80SJustin T. Gibbs 		op = 0;
276fac3fd80SJustin T. Gibbs 	bus_dmamap_sync(sc->xbd_io_dmat, cm->cm_map, op);
277fac3fd80SJustin T. Gibbs 
278fac3fd80SJustin T. Gibbs 	gnttab_free_grant_references(cm->cm_gref_head);
279fac3fd80SJustin T. Gibbs 
280e2c1fe90SJustin T. Gibbs 	xbd_enqueue_cm(cm, XBD_Q_BUSY);
281fac3fd80SJustin T. Gibbs 
282fac3fd80SJustin T. Gibbs 	/*
283127a9483SJustin T. Gibbs 	 * If bus dma had to asynchronously call us back to dispatch
284127a9483SJustin T. Gibbs 	 * this command, we are no longer executing in the context of
285127a9483SJustin T. Gibbs 	 * xbd_startio().  Thus we cannot rely on xbd_startio()'s call to
286127a9483SJustin T. Gibbs 	 * xbd_flush_requests() to publish this command to the backend
287127a9483SJustin T. Gibbs 	 * along with any other commands that it could batch.
288fac3fd80SJustin T. Gibbs 	 */
289127a9483SJustin T. Gibbs 	if ((cm->cm_flags & XBDCF_ASYNC_MAPPING) != 0)
290cdf5d66fSJustin T. Gibbs 		xbd_flush_requests(sc);
291fac3fd80SJustin T. Gibbs 
292fac3fd80SJustin T. Gibbs 	return;
293fac3fd80SJustin T. Gibbs }
294fac3fd80SJustin T. Gibbs 
295fac3fd80SJustin T. Gibbs static int
296fac3fd80SJustin T. Gibbs xbd_queue_request(struct xbd_softc *sc, struct xbd_command *cm)
297fac3fd80SJustin T. Gibbs {
298fac3fd80SJustin T. Gibbs 	int error;
299fac3fd80SJustin T. Gibbs 
300a55a04a8SRoger Pau Monné 	if (cm->cm_bp != NULL)
301a55a04a8SRoger Pau Monné 		error = bus_dmamap_load_bio(sc->xbd_io_dmat, cm->cm_map,
302a55a04a8SRoger Pau Monné 		    cm->cm_bp, xbd_queue_cb, cm, 0);
303a55a04a8SRoger Pau Monné 	else
304a55a04a8SRoger Pau Monné 		error = bus_dmamap_load(sc->xbd_io_dmat, cm->cm_map,
305a55a04a8SRoger Pau Monné 		    cm->cm_data, cm->cm_datalen, xbd_queue_cb, cm, 0);
306fac3fd80SJustin T. Gibbs 	if (error == EINPROGRESS) {
307127a9483SJustin T. Gibbs 		/*
308127a9483SJustin T. Gibbs 		 * Maintain queuing order by freezing the queue.  The next
309127a9483SJustin T. Gibbs 		 * command may not require as many resources as the command
310127a9483SJustin T. Gibbs 		 * we just attempted to map, so we can't rely on bus dma
311127a9483SJustin T. Gibbs 		 * blocking for it too.
312127a9483SJustin T. Gibbs 		 */
3139985113bSJustin T. Gibbs 		xbd_cm_freeze(sc, cm, XBDCF_ASYNC_MAPPING);
314fac3fd80SJustin T. Gibbs 		return (0);
315fac3fd80SJustin T. Gibbs 	}
316fac3fd80SJustin T. Gibbs 
317fac3fd80SJustin T. Gibbs 	return (error);
318fac3fd80SJustin T. Gibbs }
319fac3fd80SJustin T. Gibbs 
320fac3fd80SJustin T. Gibbs static void
321fac3fd80SJustin T. Gibbs xbd_restart_queue_callback(void *arg)
322fac3fd80SJustin T. Gibbs {
323fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = arg;
324fac3fd80SJustin T. Gibbs 
32533eebb6aSJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
326e4808c4bSKip Macy 
327127a9483SJustin T. Gibbs 	xbd_thaw(sc, XBDF_GNT_SHORTAGE);
328127a9483SJustin T. Gibbs 
32933eebb6aSJustin T. Gibbs 	xbd_startio(sc);
330e4808c4bSKip Macy 
33133eebb6aSJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
332fac3fd80SJustin T. Gibbs }
333fac3fd80SJustin T. Gibbs 
334fac3fd80SJustin T. Gibbs static struct xbd_command *
335fac3fd80SJustin T. Gibbs xbd_bio_command(struct xbd_softc *sc)
336fac3fd80SJustin T. Gibbs {
337fac3fd80SJustin T. Gibbs 	struct xbd_command *cm;
338fac3fd80SJustin T. Gibbs 	struct bio *bp;
339fac3fd80SJustin T. Gibbs 
34076acc41fSJustin T. Gibbs 	if (__predict_false(sc->xbd_state != XBD_STATE_CONNECTED))
341fac3fd80SJustin T. Gibbs 		return (NULL);
342fac3fd80SJustin T. Gibbs 
343fac3fd80SJustin T. Gibbs 	bp = xbd_dequeue_bio(sc);
344fac3fd80SJustin T. Gibbs 	if (bp == NULL)
345fac3fd80SJustin T. Gibbs 		return (NULL);
346fac3fd80SJustin T. Gibbs 
347e2c1fe90SJustin T. Gibbs 	if ((cm = xbd_dequeue_cm(sc, XBD_Q_FREE)) == NULL) {
348127a9483SJustin T. Gibbs 		xbd_freeze(sc, XBDF_CM_SHORTAGE);
349fac3fd80SJustin T. Gibbs 		xbd_requeue_bio(sc, bp);
350fac3fd80SJustin T. Gibbs 		return (NULL);
351fac3fd80SJustin T. Gibbs 	}
352fac3fd80SJustin T. Gibbs 
353fac3fd80SJustin T. Gibbs 	if (gnttab_alloc_grant_references(sc->xbd_max_request_segments,
354fac3fd80SJustin T. Gibbs 	    &cm->cm_gref_head) != 0) {
355fac3fd80SJustin T. Gibbs 		gnttab_request_free_callback(&sc->xbd_callback,
356fac3fd80SJustin T. Gibbs 		    xbd_restart_queue_callback, sc,
357fac3fd80SJustin T. Gibbs 		    sc->xbd_max_request_segments);
358127a9483SJustin T. Gibbs 		xbd_freeze(sc, XBDF_GNT_SHORTAGE);
359fac3fd80SJustin T. Gibbs 		xbd_requeue_bio(sc, bp);
360e2c1fe90SJustin T. Gibbs 		xbd_enqueue_cm(cm, XBD_Q_FREE);
361fac3fd80SJustin T. Gibbs 		return (NULL);
362fac3fd80SJustin T. Gibbs 	}
363fac3fd80SJustin T. Gibbs 
364fac3fd80SJustin T. Gibbs 	cm->cm_bp = bp;
365fac3fd80SJustin T. Gibbs 	cm->cm_sector_number = (blkif_sector_t)bp->bio_pblkno;
366fac3fd80SJustin T. Gibbs 
3679985113bSJustin T. Gibbs 	switch (bp->bio_cmd) {
3689985113bSJustin T. Gibbs 	case BIO_READ:
3699985113bSJustin T. Gibbs 		cm->cm_operation = BLKIF_OP_READ;
3709985113bSJustin T. Gibbs 		break;
3719985113bSJustin T. Gibbs 	case BIO_WRITE:
3729985113bSJustin T. Gibbs 		cm->cm_operation = BLKIF_OP_WRITE;
3739985113bSJustin T. Gibbs 		if ((bp->bio_flags & BIO_ORDERED) != 0) {
3749985113bSJustin T. Gibbs 			if ((sc->xbd_flags & XBDF_BARRIER) != 0) {
3759985113bSJustin T. Gibbs 				cm->cm_operation = BLKIF_OP_WRITE_BARRIER;
3769985113bSJustin T. Gibbs 			} else {
3779985113bSJustin T. Gibbs 				/*
3789985113bSJustin T. Gibbs 				 * Single step this command.
3799985113bSJustin T. Gibbs 				 */
3809985113bSJustin T. Gibbs 				cm->cm_flags |= XBDCF_Q_FREEZE;
3819985113bSJustin T. Gibbs 				if (xbd_queue_length(sc, XBD_Q_BUSY) != 0) {
3829985113bSJustin T. Gibbs 					/*
3839985113bSJustin T. Gibbs 					 * Wait for in-flight requests to
3849985113bSJustin T. Gibbs 					 * finish.
3859985113bSJustin T. Gibbs 					 */
3869985113bSJustin T. Gibbs 					xbd_freeze(sc, XBDF_WAIT_IDLE);
3879985113bSJustin T. Gibbs 					xbd_requeue_cm(cm, XBD_Q_READY);
3889985113bSJustin T. Gibbs 					return (NULL);
3899985113bSJustin T. Gibbs 				}
3909985113bSJustin T. Gibbs 			}
3919985113bSJustin T. Gibbs 		}
3929985113bSJustin T. Gibbs 		break;
3939985113bSJustin T. Gibbs 	case BIO_FLUSH:
3949985113bSJustin T. Gibbs 		if ((sc->xbd_flags & XBDF_FLUSH) != 0)
3959985113bSJustin T. Gibbs 			cm->cm_operation = BLKIF_OP_FLUSH_DISKCACHE;
3969985113bSJustin T. Gibbs 		else if ((sc->xbd_flags & XBDF_BARRIER) != 0)
3979985113bSJustin T. Gibbs 			cm->cm_operation = BLKIF_OP_WRITE_BARRIER;
3989985113bSJustin T. Gibbs 		else
3999985113bSJustin T. Gibbs 			panic("flush request, but no flush support available");
4009985113bSJustin T. Gibbs 		break;
4019985113bSJustin T. Gibbs 	default:
4029985113bSJustin T. Gibbs 		panic("unknown bio command %d", bp->bio_cmd);
4039985113bSJustin T. Gibbs 	}
4049985113bSJustin T. Gibbs 
405fac3fd80SJustin T. Gibbs 	return (cm);
406fac3fd80SJustin T. Gibbs }
407fac3fd80SJustin T. Gibbs 
408fac3fd80SJustin T. Gibbs /*
409fac3fd80SJustin T. Gibbs  * Dequeue buffers and place them in the shared communication ring.
410fac3fd80SJustin T. Gibbs  * Return when no more requests can be accepted or all buffers have
411fac3fd80SJustin T. Gibbs  * been queued.
412fac3fd80SJustin T. Gibbs  *
413fac3fd80SJustin T. Gibbs  * Signal XEN once the ring has been filled out.
414fac3fd80SJustin T. Gibbs  */
415fac3fd80SJustin T. Gibbs static void
416fac3fd80SJustin T. Gibbs xbd_startio(struct xbd_softc *sc)
417fac3fd80SJustin T. Gibbs {
418fac3fd80SJustin T. Gibbs 	struct xbd_command *cm;
419fac3fd80SJustin T. Gibbs 	int error, queued = 0;
420fac3fd80SJustin T. Gibbs 
421fac3fd80SJustin T. Gibbs 	mtx_assert(&sc->xbd_io_lock, MA_OWNED);
422fac3fd80SJustin T. Gibbs 
423e2c1fe90SJustin T. Gibbs 	if (sc->xbd_state != XBD_STATE_CONNECTED)
424e4808c4bSKip Macy 		return;
425fac3fd80SJustin T. Gibbs 
426112cacaeSRoger Pau Monné 	while (!RING_FULL(&sc->xbd_ring)) {
427112cacaeSRoger Pau Monné 
428127a9483SJustin T. Gibbs 		if (sc->xbd_qfrozen_cnt != 0)
429fac3fd80SJustin T. Gibbs 			break;
430fac3fd80SJustin T. Gibbs 
431e2c1fe90SJustin T. Gibbs 		cm = xbd_dequeue_cm(sc, XBD_Q_READY);
432fac3fd80SJustin T. Gibbs 
433fac3fd80SJustin T. Gibbs 		if (cm == NULL)
434fac3fd80SJustin T. Gibbs 		    cm = xbd_bio_command(sc);
435fac3fd80SJustin T. Gibbs 
436fac3fd80SJustin T. Gibbs 		if (cm == NULL)
437fac3fd80SJustin T. Gibbs 			break;
438fac3fd80SJustin T. Gibbs 
4399985113bSJustin T. Gibbs 		if ((cm->cm_flags & XBDCF_Q_FREEZE) != 0) {
4409985113bSJustin T. Gibbs 			/*
4419985113bSJustin T. Gibbs 			 * Single step command.  Future work is
4429985113bSJustin T. Gibbs 			 * held off until this command completes.
4439985113bSJustin T. Gibbs 			 */
4449985113bSJustin T. Gibbs 			xbd_cm_freeze(sc, cm, XBDCF_Q_FREEZE);
4459985113bSJustin T. Gibbs 		}
4469985113bSJustin T. Gibbs 
447fac3fd80SJustin T. Gibbs 		if ((error = xbd_queue_request(sc, cm)) != 0) {
448fac3fd80SJustin T. Gibbs 			printf("xbd_queue_request returned %d\n", error);
449fac3fd80SJustin T. Gibbs 			break;
450fac3fd80SJustin T. Gibbs 		}
451fac3fd80SJustin T. Gibbs 		queued++;
452fac3fd80SJustin T. Gibbs 	}
453fac3fd80SJustin T. Gibbs 
454fac3fd80SJustin T. Gibbs 	if (queued != 0)
455cdf5d66fSJustin T. Gibbs 		xbd_flush_requests(sc);
456e4808c4bSKip Macy }
457e4808c4bSKip Macy 
458e4808c4bSKip Macy static void
45933eebb6aSJustin T. Gibbs xbd_bio_complete(struct xbd_softc *sc, struct xbd_command *cm)
460e4808c4bSKip Macy {
461e4808c4bSKip Macy 	struct bio *bp;
462e4808c4bSKip Macy 
46333eebb6aSJustin T. Gibbs 	bp = cm->cm_bp;
464e4808c4bSKip Macy 
46576acc41fSJustin T. Gibbs 	if (__predict_false(cm->cm_status != BLKIF_RSP_OKAY)) {
466e4808c4bSKip Macy 		disk_err(bp, "disk error" , -1, 0);
46733eebb6aSJustin T. Gibbs 		printf(" status: %x\n", cm->cm_status);
468e4808c4bSKip Macy 		bp->bio_flags |= BIO_ERROR;
469e4808c4bSKip Macy 	}
470e4808c4bSKip Macy 
471e4808c4bSKip Macy 	if (bp->bio_flags & BIO_ERROR)
472e4808c4bSKip Macy 		bp->bio_error = EIO;
473e4808c4bSKip Macy 	else
474e4808c4bSKip Macy 		bp->bio_resid = 0;
475e4808c4bSKip Macy 
47633eebb6aSJustin T. Gibbs 	xbd_free_command(cm);
477e4808c4bSKip Macy 	biodone(bp);
478e4808c4bSKip Macy }
479e4808c4bSKip Macy 
480fac3fd80SJustin T. Gibbs static void
481fac3fd80SJustin T. Gibbs xbd_int(void *xsc)
482fac3fd80SJustin T. Gibbs {
483fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = xsc;
484fac3fd80SJustin T. Gibbs 	struct xbd_command *cm;
485fac3fd80SJustin T. Gibbs 	blkif_response_t *bret;
486fac3fd80SJustin T. Gibbs 	RING_IDX i, rp;
487fac3fd80SJustin T. Gibbs 	int op;
488fac3fd80SJustin T. Gibbs 
489fac3fd80SJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
490fac3fd80SJustin T. Gibbs 
49176acc41fSJustin T. Gibbs 	if (__predict_false(sc->xbd_state == XBD_STATE_DISCONNECTED)) {
492fac3fd80SJustin T. Gibbs 		mtx_unlock(&sc->xbd_io_lock);
493fac3fd80SJustin T. Gibbs 		return;
494fac3fd80SJustin T. Gibbs 	}
495fac3fd80SJustin T. Gibbs 
496fac3fd80SJustin T. Gibbs  again:
497fac3fd80SJustin T. Gibbs 	rp = sc->xbd_ring.sring->rsp_prod;
498fac3fd80SJustin T. Gibbs 	rmb(); /* Ensure we see queued responses up to 'rp'. */
499fac3fd80SJustin T. Gibbs 
500fac3fd80SJustin T. Gibbs 	for (i = sc->xbd_ring.rsp_cons; i != rp;) {
501fac3fd80SJustin T. Gibbs 		bret = RING_GET_RESPONSE(&sc->xbd_ring, i);
502fac3fd80SJustin T. Gibbs 		cm   = &sc->xbd_shadow[bret->id];
503fac3fd80SJustin T. Gibbs 
504e2c1fe90SJustin T. Gibbs 		xbd_remove_cm(cm, XBD_Q_BUSY);
505112cacaeSRoger Pau Monné 		gnttab_end_foreign_access_references(cm->cm_nseg,
506112cacaeSRoger Pau Monné 		    cm->cm_sg_refs);
507112cacaeSRoger Pau Monné 		i++;
508fac3fd80SJustin T. Gibbs 
509fac3fd80SJustin T. Gibbs 		if (cm->cm_operation == BLKIF_OP_READ)
510fac3fd80SJustin T. Gibbs 			op = BUS_DMASYNC_POSTREAD;
5119985113bSJustin T. Gibbs 		else if (cm->cm_operation == BLKIF_OP_WRITE ||
5129985113bSJustin T. Gibbs 		    cm->cm_operation == BLKIF_OP_WRITE_BARRIER)
513fac3fd80SJustin T. Gibbs 			op = BUS_DMASYNC_POSTWRITE;
514fac3fd80SJustin T. Gibbs 		else
515fac3fd80SJustin T. Gibbs 			op = 0;
516fac3fd80SJustin T. Gibbs 		bus_dmamap_sync(sc->xbd_io_dmat, cm->cm_map, op);
517fac3fd80SJustin T. Gibbs 		bus_dmamap_unload(sc->xbd_io_dmat, cm->cm_map);
518fac3fd80SJustin T. Gibbs 
519fac3fd80SJustin T. Gibbs 		/*
520127a9483SJustin T. Gibbs 		 * Release any hold this command has on future command
521127a9483SJustin T. Gibbs 		 * dispatch.
522fac3fd80SJustin T. Gibbs 		 */
5239985113bSJustin T. Gibbs 		xbd_cm_thaw(sc, cm);
524fac3fd80SJustin T. Gibbs 
525fac3fd80SJustin T. Gibbs 		/*
526fac3fd80SJustin T. Gibbs 		 * Directly call the i/o complete routine to save an
527fac3fd80SJustin T. Gibbs 		 * an indirection in the common case.
528fac3fd80SJustin T. Gibbs 		 */
529fac3fd80SJustin T. Gibbs 		cm->cm_status = bret->status;
530fac3fd80SJustin T. Gibbs 		if (cm->cm_bp)
531fac3fd80SJustin T. Gibbs 			xbd_bio_complete(sc, cm);
532fac3fd80SJustin T. Gibbs 		else if (cm->cm_complete != NULL)
533fac3fd80SJustin T. Gibbs 			cm->cm_complete(cm);
534fac3fd80SJustin T. Gibbs 		else
535fac3fd80SJustin T. Gibbs 			xbd_free_command(cm);
536fac3fd80SJustin T. Gibbs 	}
537fac3fd80SJustin T. Gibbs 
538fac3fd80SJustin T. Gibbs 	sc->xbd_ring.rsp_cons = i;
539fac3fd80SJustin T. Gibbs 
540fac3fd80SJustin T. Gibbs 	if (i != sc->xbd_ring.req_prod_pvt) {
541fac3fd80SJustin T. Gibbs 		int more_to_do;
542fac3fd80SJustin T. Gibbs 		RING_FINAL_CHECK_FOR_RESPONSES(&sc->xbd_ring, more_to_do);
543fac3fd80SJustin T. Gibbs 		if (more_to_do)
544fac3fd80SJustin T. Gibbs 			goto again;
545fac3fd80SJustin T. Gibbs 	} else {
546fac3fd80SJustin T. Gibbs 		sc->xbd_ring.sring->rsp_event = i + 1;
547fac3fd80SJustin T. Gibbs 	}
548fac3fd80SJustin T. Gibbs 
5499985113bSJustin T. Gibbs 	if (xbd_queue_length(sc, XBD_Q_BUSY) == 0)
5509985113bSJustin T. Gibbs 		xbd_thaw(sc, XBDF_WAIT_IDLE);
5519985113bSJustin T. Gibbs 
552fac3fd80SJustin T. Gibbs 	xbd_startio(sc);
553fac3fd80SJustin T. Gibbs 
55476acc41fSJustin T. Gibbs 	if (__predict_false(sc->xbd_state == XBD_STATE_SUSPENDED))
555e2c1fe90SJustin T. Gibbs 		wakeup(&sc->xbd_cm_q[XBD_Q_BUSY]);
556fac3fd80SJustin T. Gibbs 
557fac3fd80SJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
558fac3fd80SJustin T. Gibbs }
559fac3fd80SJustin T. Gibbs 
560fac3fd80SJustin T. Gibbs /*------------------------------- Dump Support -------------------------------*/
561fac3fd80SJustin T. Gibbs /**
562fac3fd80SJustin T. Gibbs  * Quiesce the disk writes for a dump file before allowing the next buffer.
563fac3fd80SJustin T. Gibbs  */
5648698b76cSKip Macy static void
56533eebb6aSJustin T. Gibbs xbd_quiesce(struct xbd_softc *sc)
5668698b76cSKip Macy {
5678698b76cSKip Macy 	int mtd;
5688698b76cSKip Macy 
5698698b76cSKip Macy 	// While there are outstanding requests
5709985113bSJustin T. Gibbs 	while (xbd_queue_length(sc, XBD_Q_BUSY) != 0) {
57133eebb6aSJustin T. Gibbs 		RING_FINAL_CHECK_FOR_RESPONSES(&sc->xbd_ring, mtd);
5728698b76cSKip Macy 		if (mtd) {
573453130d9SPedro F. Giffuni 			/* Received request completions, update queue. */
57433eebb6aSJustin T. Gibbs 			xbd_int(sc);
5758698b76cSKip Macy 		}
5769985113bSJustin T. Gibbs 		if (xbd_queue_length(sc, XBD_Q_BUSY) != 0) {
577e4808c4bSKip Macy 			/*
578e4808c4bSKip Macy 			 * Still pending requests, wait for the disk i/o
579e4808c4bSKip Macy 			 * to complete.
580e4808c4bSKip Macy 			 */
581be7747b4SKip Macy 			HYPERVISOR_yield();
5828698b76cSKip Macy 		}
5838698b76cSKip Macy 	}
5848698b76cSKip Macy }
5858698b76cSKip Macy 
586e4808c4bSKip Macy /* Kernel dump function for a paravirtualized disk device */
587e4808c4bSKip Macy static void
58833eebb6aSJustin T. Gibbs xbd_dump_complete(struct xbd_command *cm)
589e4808c4bSKip Macy {
5908698b76cSKip Macy 
591e2c1fe90SJustin T. Gibbs 	xbd_enqueue_cm(cm, XBD_Q_COMPLETE);
592e4808c4bSKip Macy }
593e4808c4bSKip Macy 
5948698b76cSKip Macy static int
59533eebb6aSJustin T. Gibbs xbd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset,
5968698b76cSKip Macy     size_t length)
5978698b76cSKip Macy {
5988698b76cSKip Macy 	struct disk *dp = arg;
59933eebb6aSJustin T. Gibbs 	struct xbd_softc *sc = dp->d_drv1;
60033eebb6aSJustin T. Gibbs 	struct xbd_command *cm;
601e4808c4bSKip Macy 	size_t chunk;
602e4808c4bSKip Macy 	int sbp;
6038698b76cSKip Macy 	int rc = 0;
6048698b76cSKip Macy 
6058b19549bSRoger Pau Monné 	if (length == 0)
6068b19549bSRoger Pau Monné 		return (0);
6078698b76cSKip Macy 
60833eebb6aSJustin T. Gibbs 	xbd_quiesce(sc);	/* All quiet on the western front. */
609e4808c4bSKip Macy 
610e4808c4bSKip Macy 	/*
611e4808c4bSKip Macy 	 * If this lock is held, then this module is failing, and a
612e4808c4bSKip Macy 	 * successful kernel dump is highly unlikely anyway.
613e4808c4bSKip Macy 	 */
61433eebb6aSJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
615e4808c4bSKip Macy 
616e4808c4bSKip Macy 	/* Split the 64KB block as needed */
617e4808c4bSKip Macy 	for (sbp=0; length > 0; sbp++) {
618e2c1fe90SJustin T. Gibbs 		cm = xbd_dequeue_cm(sc, XBD_Q_FREE);
619e4808c4bSKip Macy 		if (cm == NULL) {
62033eebb6aSJustin T. Gibbs 			mtx_unlock(&sc->xbd_io_lock);
62133eebb6aSJustin T. Gibbs 			device_printf(sc->xbd_dev, "dump: no more commands?\n");
622e4808c4bSKip Macy 			return (EBUSY);
623e4808c4bSKip Macy 		}
624e4808c4bSKip Macy 
62533eebb6aSJustin T. Gibbs 		if (gnttab_alloc_grant_references(sc->xbd_max_request_segments,
62633eebb6aSJustin T. Gibbs 		    &cm->cm_gref_head) != 0) {
62733eebb6aSJustin T. Gibbs 			xbd_free_command(cm);
62833eebb6aSJustin T. Gibbs 			mtx_unlock(&sc->xbd_io_lock);
62933eebb6aSJustin T. Gibbs 			device_printf(sc->xbd_dev, "no more grant allocs?\n");
630e4808c4bSKip Macy 			return (EBUSY);
631e4808c4bSKip Macy 		}
632e4808c4bSKip Macy 
63333eebb6aSJustin T. Gibbs 		chunk = length > sc->xbd_max_request_size ?
63433eebb6aSJustin T. Gibbs 		    sc->xbd_max_request_size : length;
63533eebb6aSJustin T. Gibbs 		cm->cm_data = virtual;
63633eebb6aSJustin T. Gibbs 		cm->cm_datalen = chunk;
63733eebb6aSJustin T. Gibbs 		cm->cm_operation = BLKIF_OP_WRITE;
63833eebb6aSJustin T. Gibbs 		cm->cm_sector_number = offset / dp->d_sectorsize;
63933eebb6aSJustin T. Gibbs 		cm->cm_complete = xbd_dump_complete;
640e4808c4bSKip Macy 
641e2c1fe90SJustin T. Gibbs 		xbd_enqueue_cm(cm, XBD_Q_READY);
6428698b76cSKip Macy 
6438698b76cSKip Macy 		length -= chunk;
6448698b76cSKip Macy 		offset += chunk;
6458698b76cSKip Macy 		virtual = (char *) virtual + chunk;
6468698b76cSKip Macy 	}
6478698b76cSKip Macy 
648e4808c4bSKip Macy 	/* Tell DOM0 to do the I/O */
64933eebb6aSJustin T. Gibbs 	xbd_startio(sc);
65033eebb6aSJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
651e4808c4bSKip Macy 
652e4808c4bSKip Macy 	/* Poll for the completion. */
65333eebb6aSJustin T. Gibbs 	xbd_quiesce(sc);	/* All quite on the eastern front */
654e4808c4bSKip Macy 
655e4808c4bSKip Macy 	/* If there were any errors, bail out... */
656e2c1fe90SJustin T. Gibbs 	while ((cm = xbd_dequeue_cm(sc, XBD_Q_COMPLETE)) != NULL) {
65733eebb6aSJustin T. Gibbs 		if (cm->cm_status != BLKIF_RSP_OKAY) {
65833eebb6aSJustin T. Gibbs 			device_printf(sc->xbd_dev,
659e4808c4bSKip Macy 			    "Dump I/O failed at sector %jd\n",
66033eebb6aSJustin T. Gibbs 			    cm->cm_sector_number);
661e4808c4bSKip Macy 			rc = EIO;
6628698b76cSKip Macy 		}
66333eebb6aSJustin T. Gibbs 		xbd_free_command(cm);
6648698b76cSKip Macy 	}
665e4808c4bSKip Macy 
6668698b76cSKip Macy 	return (rc);
6678698b76cSKip Macy }
6688698b76cSKip Macy 
669fac3fd80SJustin T. Gibbs /*----------------------------- Disk Entrypoints -----------------------------*/
67023dc5621SKip Macy static int
671fac3fd80SJustin T. Gibbs xbd_open(struct disk *dp)
67289e0f4d2SKip Macy {
673fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = dp->d_drv1;
67423dc5621SKip Macy 
675fac3fd80SJustin T. Gibbs 	if (sc == NULL) {
676bcccdfa3SColin Percival 		printf("xbd%d: not found", dp->d_unit);
67723dc5621SKip Macy 		return (ENXIO);
67823dc5621SKip Macy 	}
67923dc5621SKip Macy 
680e2c1fe90SJustin T. Gibbs 	sc->xbd_flags |= XBDF_OPEN;
681fac3fd80SJustin T. Gibbs 	sc->xbd_users++;
682fac3fd80SJustin T. Gibbs 	return (0);
683fac3fd80SJustin T. Gibbs }
684fac3fd80SJustin T. Gibbs 
685fac3fd80SJustin T. Gibbs static int
686fac3fd80SJustin T. Gibbs xbd_close(struct disk *dp)
687fac3fd80SJustin T. Gibbs {
688fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = dp->d_drv1;
689fac3fd80SJustin T. Gibbs 
690fac3fd80SJustin T. Gibbs 	if (sc == NULL)
691fac3fd80SJustin T. Gibbs 		return (ENXIO);
692e2c1fe90SJustin T. Gibbs 	sc->xbd_flags &= ~XBDF_OPEN;
693fac3fd80SJustin T. Gibbs 	if (--(sc->xbd_users) == 0) {
694fac3fd80SJustin T. Gibbs 		/*
695fac3fd80SJustin T. Gibbs 		 * Check whether we have been instructed to close.  We will
696fac3fd80SJustin T. Gibbs 		 * have ignored this request initially, as the device was
697fac3fd80SJustin T. Gibbs 		 * still mounted.
698fac3fd80SJustin T. Gibbs 		 */
699fac3fd80SJustin T. Gibbs 		if (xenbus_get_otherend_state(sc->xbd_dev) ==
700fac3fd80SJustin T. Gibbs 		    XenbusStateClosing)
701fac3fd80SJustin T. Gibbs 			xbd_closing(sc->xbd_dev);
702fac3fd80SJustin T. Gibbs 	}
703fac3fd80SJustin T. Gibbs 	return (0);
704fac3fd80SJustin T. Gibbs }
705fac3fd80SJustin T. Gibbs 
706fac3fd80SJustin T. Gibbs static int
707fac3fd80SJustin T. Gibbs xbd_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
708fac3fd80SJustin T. Gibbs {
709fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = dp->d_drv1;
710fac3fd80SJustin T. Gibbs 
711fac3fd80SJustin T. Gibbs 	if (sc == NULL)
712fac3fd80SJustin T. Gibbs 		return (ENXIO);
713fac3fd80SJustin T. Gibbs 
714fac3fd80SJustin T. Gibbs 	return (ENOTTY);
715fac3fd80SJustin T. Gibbs }
716fac3fd80SJustin T. Gibbs 
717fac3fd80SJustin T. Gibbs /*
718fac3fd80SJustin T. Gibbs  * Read/write routine for a buffer.  Finds the proper unit, place it on
719fac3fd80SJustin T. Gibbs  * the sortq and kick the controller.
720fac3fd80SJustin T. Gibbs  */
721fac3fd80SJustin T. Gibbs static void
722fac3fd80SJustin T. Gibbs xbd_strategy(struct bio *bp)
723fac3fd80SJustin T. Gibbs {
724fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = bp->bio_disk->d_drv1;
725fac3fd80SJustin T. Gibbs 
726fac3fd80SJustin T. Gibbs 	/* bogus disk? */
727fac3fd80SJustin T. Gibbs 	if (sc == NULL) {
728fac3fd80SJustin T. Gibbs 		bp->bio_error = EINVAL;
729fac3fd80SJustin T. Gibbs 		bp->bio_flags |= BIO_ERROR;
730fac3fd80SJustin T. Gibbs 		bp->bio_resid = bp->bio_bcount;
731fac3fd80SJustin T. Gibbs 		biodone(bp);
732fac3fd80SJustin T. Gibbs 		return;
733fac3fd80SJustin T. Gibbs 	}
734fac3fd80SJustin T. Gibbs 
735fac3fd80SJustin T. Gibbs 	/*
736fac3fd80SJustin T. Gibbs 	 * Place it in the queue of disk activities for this disk
737fac3fd80SJustin T. Gibbs 	 */
738fac3fd80SJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
739fac3fd80SJustin T. Gibbs 
740fac3fd80SJustin T. Gibbs 	xbd_enqueue_bio(sc, bp);
741fac3fd80SJustin T. Gibbs 	xbd_startio(sc);
742fac3fd80SJustin T. Gibbs 
743fac3fd80SJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
744fac3fd80SJustin T. Gibbs 	return;
745fac3fd80SJustin T. Gibbs }
746fac3fd80SJustin T. Gibbs 
747fac3fd80SJustin T. Gibbs /*------------------------------ Ring Management -----------------------------*/
748fac3fd80SJustin T. Gibbs static int
749cdf5d66fSJustin T. Gibbs xbd_alloc_ring(struct xbd_softc *sc)
750fac3fd80SJustin T. Gibbs {
751fac3fd80SJustin T. Gibbs 	blkif_sring_t *sring;
752fac3fd80SJustin T. Gibbs 	uintptr_t sring_page_addr;
753fac3fd80SJustin T. Gibbs 	int error;
754fac3fd80SJustin T. Gibbs 	int i;
755fac3fd80SJustin T. Gibbs 
756fac3fd80SJustin T. Gibbs 	sring = malloc(sc->xbd_ring_pages * PAGE_SIZE, M_XENBLOCKFRONT,
757fac3fd80SJustin T. Gibbs 	    M_NOWAIT|M_ZERO);
758fac3fd80SJustin T. Gibbs 	if (sring == NULL) {
759fac3fd80SJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, ENOMEM, "allocating shared ring");
760fac3fd80SJustin T. Gibbs 		return (ENOMEM);
761fac3fd80SJustin T. Gibbs 	}
762fac3fd80SJustin T. Gibbs 	SHARED_RING_INIT(sring);
763fac3fd80SJustin T. Gibbs 	FRONT_RING_INIT(&sc->xbd_ring, sring, sc->xbd_ring_pages * PAGE_SIZE);
764fac3fd80SJustin T. Gibbs 
765fac3fd80SJustin T. Gibbs 	for (i = 0, sring_page_addr = (uintptr_t)sring;
766fac3fd80SJustin T. Gibbs 	     i < sc->xbd_ring_pages;
767fac3fd80SJustin T. Gibbs 	     i++, sring_page_addr += PAGE_SIZE) {
768fac3fd80SJustin T. Gibbs 
769fac3fd80SJustin T. Gibbs 		error = xenbus_grant_ring(sc->xbd_dev,
7703c790178SJohn Baldwin 		    (vtophys(sring_page_addr) >> PAGE_SHIFT),
771fac3fd80SJustin T. Gibbs 		    &sc->xbd_ring_ref[i]);
772fac3fd80SJustin T. Gibbs 		if (error) {
773fac3fd80SJustin T. Gibbs 			xenbus_dev_fatal(sc->xbd_dev, error,
774fac3fd80SJustin T. Gibbs 			    "granting ring_ref(%d)", i);
775fac3fd80SJustin T. Gibbs 			return (error);
776fac3fd80SJustin T. Gibbs 		}
777fac3fd80SJustin T. Gibbs 	}
778fac3fd80SJustin T. Gibbs 	if (sc->xbd_ring_pages == 1) {
779fac3fd80SJustin T. Gibbs 		error = xs_printf(XST_NIL, xenbus_get_node(sc->xbd_dev),
780fac3fd80SJustin T. Gibbs 		    "ring-ref", "%u", sc->xbd_ring_ref[0]);
781fac3fd80SJustin T. Gibbs 		if (error) {
782fac3fd80SJustin T. Gibbs 			xenbus_dev_fatal(sc->xbd_dev, error,
783fac3fd80SJustin T. Gibbs 			    "writing %s/ring-ref",
784fac3fd80SJustin T. Gibbs 			    xenbus_get_node(sc->xbd_dev));
785fac3fd80SJustin T. Gibbs 			return (error);
786fac3fd80SJustin T. Gibbs 		}
787fac3fd80SJustin T. Gibbs 	} else {
788fac3fd80SJustin T. Gibbs 		for (i = 0; i < sc->xbd_ring_pages; i++) {
789fac3fd80SJustin T. Gibbs 			char ring_ref_name[]= "ring_refXX";
790fac3fd80SJustin T. Gibbs 
791fac3fd80SJustin T. Gibbs 			snprintf(ring_ref_name, sizeof(ring_ref_name),
792fac3fd80SJustin T. Gibbs 			    "ring-ref%u", i);
793fac3fd80SJustin T. Gibbs 			error = xs_printf(XST_NIL, xenbus_get_node(sc->xbd_dev),
794fac3fd80SJustin T. Gibbs 			     ring_ref_name, "%u", sc->xbd_ring_ref[i]);
795fac3fd80SJustin T. Gibbs 			if (error) {
796fac3fd80SJustin T. Gibbs 				xenbus_dev_fatal(sc->xbd_dev, error,
797fac3fd80SJustin T. Gibbs 				    "writing %s/%s",
798fac3fd80SJustin T. Gibbs 				    xenbus_get_node(sc->xbd_dev),
799fac3fd80SJustin T. Gibbs 				    ring_ref_name);
800fac3fd80SJustin T. Gibbs 				return (error);
801fac3fd80SJustin T. Gibbs 			}
802fac3fd80SJustin T. Gibbs 		}
803fac3fd80SJustin T. Gibbs 	}
804fac3fd80SJustin T. Gibbs 
80576acc41fSJustin T. Gibbs 	error = xen_intr_alloc_and_bind_local_port(sc->xbd_dev,
80676acc41fSJustin T. Gibbs 	    xenbus_get_otherend_id(sc->xbd_dev), NULL, xbd_int, sc,
80776acc41fSJustin T. Gibbs 	    INTR_TYPE_BIO | INTR_MPSAFE, &sc->xen_intr_handle);
808fac3fd80SJustin T. Gibbs 	if (error) {
809fac3fd80SJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
81076acc41fSJustin T. Gibbs 		    "xen_intr_alloc_and_bind_local_port failed");
811fac3fd80SJustin T. Gibbs 		return (error);
812fac3fd80SJustin T. Gibbs 	}
813fac3fd80SJustin T. Gibbs 
814fac3fd80SJustin T. Gibbs 	return (0);
815fac3fd80SJustin T. Gibbs }
816fac3fd80SJustin T. Gibbs 
817d9fab01dSJustin T. Gibbs static void
818d9fab01dSJustin T. Gibbs xbd_free_ring(struct xbd_softc *sc)
819d9fab01dSJustin T. Gibbs {
820d9fab01dSJustin T. Gibbs 	int i;
821d9fab01dSJustin T. Gibbs 
822d9fab01dSJustin T. Gibbs 	if (sc->xbd_ring.sring == NULL)
823d9fab01dSJustin T. Gibbs 		return;
824d9fab01dSJustin T. Gibbs 
825d9fab01dSJustin T. Gibbs 	for (i = 0; i < sc->xbd_ring_pages; i++) {
826d9fab01dSJustin T. Gibbs 		if (sc->xbd_ring_ref[i] != GRANT_REF_INVALID) {
827d9fab01dSJustin T. Gibbs 			gnttab_end_foreign_access_ref(sc->xbd_ring_ref[i]);
828d9fab01dSJustin T. Gibbs 			sc->xbd_ring_ref[i] = GRANT_REF_INVALID;
829d9fab01dSJustin T. Gibbs 		}
830d9fab01dSJustin T. Gibbs 	}
831d9fab01dSJustin T. Gibbs 	free(sc->xbd_ring.sring, M_XENBLOCKFRONT);
832d9fab01dSJustin T. Gibbs 	sc->xbd_ring.sring = NULL;
833d9fab01dSJustin T. Gibbs }
834d9fab01dSJustin T. Gibbs 
835fac3fd80SJustin T. Gibbs /*-------------------------- Initialization/Teardown -------------------------*/
8369985113bSJustin T. Gibbs static int
8379985113bSJustin T. Gibbs xbd_feature_string(struct xbd_softc *sc, char *features, size_t len)
8389985113bSJustin T. Gibbs {
8399985113bSJustin T. Gibbs 	struct sbuf sb;
8409985113bSJustin T. Gibbs 	int feature_cnt;
8419985113bSJustin T. Gibbs 
8429985113bSJustin T. Gibbs 	sbuf_new(&sb, features, len, SBUF_FIXEDLEN);
8439985113bSJustin T. Gibbs 
8449985113bSJustin T. Gibbs 	feature_cnt = 0;
8459985113bSJustin T. Gibbs 	if ((sc->xbd_flags & XBDF_FLUSH) != 0) {
8469985113bSJustin T. Gibbs 		sbuf_printf(&sb, "flush");
8479985113bSJustin T. Gibbs 		feature_cnt++;
8489985113bSJustin T. Gibbs 	}
8499985113bSJustin T. Gibbs 
8509985113bSJustin T. Gibbs 	if ((sc->xbd_flags & XBDF_BARRIER) != 0) {
8519985113bSJustin T. Gibbs 		if (feature_cnt != 0)
8529985113bSJustin T. Gibbs 			sbuf_printf(&sb, ", ");
8539985113bSJustin T. Gibbs 		sbuf_printf(&sb, "write_barrier");
8549985113bSJustin T. Gibbs 		feature_cnt++;
8559985113bSJustin T. Gibbs 	}
8569985113bSJustin T. Gibbs 
857d5d7399dSAlexander Motin 	if ((sc->xbd_flags & XBDF_DISCARD) != 0) {
858d5d7399dSAlexander Motin 		if (feature_cnt != 0)
859d5d7399dSAlexander Motin 			sbuf_printf(&sb, ", ");
860d5d7399dSAlexander Motin 		sbuf_printf(&sb, "discard");
861d5d7399dSAlexander Motin 		feature_cnt++;
862d5d7399dSAlexander Motin 	}
863d5d7399dSAlexander Motin 
864d5d7399dSAlexander Motin 	if ((sc->xbd_flags & XBDF_PERSISTENT) != 0) {
865d5d7399dSAlexander Motin 		if (feature_cnt != 0)
866d5d7399dSAlexander Motin 			sbuf_printf(&sb, ", ");
867d5d7399dSAlexander Motin 		sbuf_printf(&sb, "persistent_grants");
868d5d7399dSAlexander Motin 		feature_cnt++;
869d5d7399dSAlexander Motin 	}
870d5d7399dSAlexander Motin 
8719985113bSJustin T. Gibbs 	(void) sbuf_finish(&sb);
8729985113bSJustin T. Gibbs 	return (sbuf_len(&sb));
8739985113bSJustin T. Gibbs }
8749985113bSJustin T. Gibbs 
8759985113bSJustin T. Gibbs static int
8769985113bSJustin T. Gibbs xbd_sysctl_features(SYSCTL_HANDLER_ARGS)
8779985113bSJustin T. Gibbs {
8789985113bSJustin T. Gibbs 	char features[80];
8799985113bSJustin T. Gibbs 	struct xbd_softc *sc = arg1;
8809985113bSJustin T. Gibbs 	int error;
8819985113bSJustin T. Gibbs 	int len;
8829985113bSJustin T. Gibbs 
8839985113bSJustin T. Gibbs 	error = sysctl_wire_old_buffer(req, 0);
8849985113bSJustin T. Gibbs 	if (error != 0)
8859985113bSJustin T. Gibbs 		return (error);
8869985113bSJustin T. Gibbs 
8879985113bSJustin T. Gibbs 	len = xbd_feature_string(sc, features, sizeof(features));
8889985113bSJustin T. Gibbs 
8899985113bSJustin T. Gibbs 	/* len is -1 on error, which will make the SYSCTL_OUT a no-op. */
8909985113bSJustin T. Gibbs 	return (SYSCTL_OUT(req, features, len + 1/*NUL*/));
8919985113bSJustin T. Gibbs }
8929985113bSJustin T. Gibbs 
8938b8bfa35SJustin T. Gibbs static void
89433eebb6aSJustin T. Gibbs xbd_setup_sysctl(struct xbd_softc *xbd)
8958b8bfa35SJustin T. Gibbs {
8968b8bfa35SJustin T. Gibbs 	struct sysctl_ctx_list *sysctl_ctx = NULL;
8978b8bfa35SJustin T. Gibbs 	struct sysctl_oid *sysctl_tree = NULL;
8989985113bSJustin T. Gibbs 	struct sysctl_oid_list *children;
8998b8bfa35SJustin T. Gibbs 
90033eebb6aSJustin T. Gibbs 	sysctl_ctx = device_get_sysctl_ctx(xbd->xbd_dev);
9018b8bfa35SJustin T. Gibbs 	if (sysctl_ctx == NULL)
9028b8bfa35SJustin T. Gibbs 		return;
9038b8bfa35SJustin T. Gibbs 
90433eebb6aSJustin T. Gibbs 	sysctl_tree = device_get_sysctl_tree(xbd->xbd_dev);
9058b8bfa35SJustin T. Gibbs 	if (sysctl_tree == NULL)
9068b8bfa35SJustin T. Gibbs 		return;
9078b8bfa35SJustin T. Gibbs 
9089985113bSJustin T. Gibbs 	children = SYSCTL_CHILDREN(sysctl_tree);
9099985113bSJustin T. Gibbs 	SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO,
91033eebb6aSJustin T. Gibbs 	    "max_requests", CTLFLAG_RD, &xbd->xbd_max_requests, -1,
9118b8bfa35SJustin T. Gibbs 	    "maximum outstanding requests (negotiated)");
9128b8bfa35SJustin T. Gibbs 
9139985113bSJustin T. Gibbs 	SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO,
9148b8bfa35SJustin T. Gibbs 	    "max_request_segments", CTLFLAG_RD,
91533eebb6aSJustin T. Gibbs 	    &xbd->xbd_max_request_segments, 0,
9168b8bfa35SJustin T. Gibbs 	    "maximum number of pages per requests (negotiated)");
9178b8bfa35SJustin T. Gibbs 
9189985113bSJustin T. Gibbs 	SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO,
91933eebb6aSJustin T. Gibbs 	    "max_request_size", CTLFLAG_RD, &xbd->xbd_max_request_size, 0,
9208b8bfa35SJustin T. Gibbs 	    "maximum size in bytes of a request (negotiated)");
9218b8bfa35SJustin T. Gibbs 
9229985113bSJustin T. Gibbs 	SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO,
92333eebb6aSJustin T. Gibbs 	    "ring_pages", CTLFLAG_RD, &xbd->xbd_ring_pages, 0,
9248b8bfa35SJustin T. Gibbs 	    "communication channel pages (negotiated)");
9259985113bSJustin T. Gibbs 
9269985113bSJustin T. Gibbs 	SYSCTL_ADD_PROC(sysctl_ctx, children, OID_AUTO,
9279985113bSJustin T. Gibbs 	    "features", CTLTYPE_STRING|CTLFLAG_RD, xbd, 0,
9289985113bSJustin T. Gibbs 	    xbd_sysctl_features, "A", "protocol features (negotiated)");
9298b8bfa35SJustin T. Gibbs }
9308b8bfa35SJustin T. Gibbs 
93123dc5621SKip Macy /*
932fac3fd80SJustin T. Gibbs  * Translate Linux major/minor to an appropriate name and unit
933fac3fd80SJustin T. Gibbs  * number. For HVM guests, this allows us to use the same drive names
934fac3fd80SJustin T. Gibbs  * with blkfront as the emulated drives, easing transition slightly.
93523dc5621SKip Macy  */
936fac3fd80SJustin T. Gibbs static void
937fac3fd80SJustin T. Gibbs xbd_vdevice_to_unit(uint32_t vdevice, int *unit, const char **name)
93823dc5621SKip Macy {
939fac3fd80SJustin T. Gibbs 	static struct vdev_info {
940fac3fd80SJustin T. Gibbs 		int major;
941fac3fd80SJustin T. Gibbs 		int shift;
942fac3fd80SJustin T. Gibbs 		int base;
94323dc5621SKip Macy 		const char *name;
944fac3fd80SJustin T. Gibbs 	} info[] = {
945fac3fd80SJustin T. Gibbs 		{3,	6,	0,	"ada"},	/* ide0 */
946fac3fd80SJustin T. Gibbs 		{22,	6,	2,	"ada"},	/* ide1 */
947fac3fd80SJustin T. Gibbs 		{33,	6,	4,	"ada"},	/* ide2 */
948fac3fd80SJustin T. Gibbs 		{34,	6,	6,	"ada"},	/* ide3 */
949fac3fd80SJustin T. Gibbs 		{56,	6,	8,	"ada"},	/* ide4 */
950fac3fd80SJustin T. Gibbs 		{57,	6,	10,	"ada"},	/* ide5 */
951fac3fd80SJustin T. Gibbs 		{88,	6,	12,	"ada"},	/* ide6 */
952fac3fd80SJustin T. Gibbs 		{89,	6,	14,	"ada"},	/* ide7 */
953fac3fd80SJustin T. Gibbs 		{90,	6,	16,	"ada"},	/* ide8 */
954fac3fd80SJustin T. Gibbs 		{91,	6,	18,	"ada"},	/* ide9 */
95589e0f4d2SKip Macy 
956fac3fd80SJustin T. Gibbs 		{8,	4,	0,	"da"},	/* scsi disk0 */
957fac3fd80SJustin T. Gibbs 		{65,	4,	16,	"da"},	/* scsi disk1 */
958fac3fd80SJustin T. Gibbs 		{66,	4,	32,	"da"},	/* scsi disk2 */
959fac3fd80SJustin T. Gibbs 		{67,	4,	48,	"da"},	/* scsi disk3 */
960fac3fd80SJustin T. Gibbs 		{68,	4,	64,	"da"},	/* scsi disk4 */
961fac3fd80SJustin T. Gibbs 		{69,	4,	80,	"da"},	/* scsi disk5 */
962fac3fd80SJustin T. Gibbs 		{70,	4,	96,	"da"},	/* scsi disk6 */
963fac3fd80SJustin T. Gibbs 		{71,	4,	112,	"da"},	/* scsi disk7 */
964fac3fd80SJustin T. Gibbs 		{128,	4,	128,	"da"},	/* scsi disk8 */
965fac3fd80SJustin T. Gibbs 		{129,	4,	144,	"da"},	/* scsi disk9 */
966fac3fd80SJustin T. Gibbs 		{130,	4,	160,	"da"},	/* scsi disk10 */
967fac3fd80SJustin T. Gibbs 		{131,	4,	176,	"da"},	/* scsi disk11 */
968fac3fd80SJustin T. Gibbs 		{132,	4,	192,	"da"},	/* scsi disk12 */
969fac3fd80SJustin T. Gibbs 		{133,	4,	208,	"da"},	/* scsi disk13 */
970fac3fd80SJustin T. Gibbs 		{134,	4,	224,	"da"},	/* scsi disk14 */
971fac3fd80SJustin T. Gibbs 		{135,	4,	240,	"da"},	/* scsi disk15 */
972fac3fd80SJustin T. Gibbs 
973fac3fd80SJustin T. Gibbs 		{202,	4,	0,	"xbd"},	/* xbd */
974fac3fd80SJustin T. Gibbs 
975fac3fd80SJustin T. Gibbs 		{0,	0,	0,	NULL},
976fac3fd80SJustin T. Gibbs 	};
977fac3fd80SJustin T. Gibbs 	int major = vdevice >> 8;
978fac3fd80SJustin T. Gibbs 	int minor = vdevice & 0xff;
979fac3fd80SJustin T. Gibbs 	int i;
980fac3fd80SJustin T. Gibbs 
981fac3fd80SJustin T. Gibbs 	if (vdevice & (1 << 28)) {
982fac3fd80SJustin T. Gibbs 		*unit = (vdevice & ((1 << 28) - 1)) >> 8;
983fac3fd80SJustin T. Gibbs 		*name = "xbd";
984fac3fd80SJustin T. Gibbs 		return;
98589e0f4d2SKip Macy 	}
98689e0f4d2SKip Macy 
987fac3fd80SJustin T. Gibbs 	for (i = 0; info[i].major; i++) {
988fac3fd80SJustin T. Gibbs 		if (info[i].major == major) {
989fac3fd80SJustin T. Gibbs 			*unit = info[i].base + (minor >> info[i].shift);
990fac3fd80SJustin T. Gibbs 			*name = info[i].name;
991fac3fd80SJustin T. Gibbs 			return;
992fac3fd80SJustin T. Gibbs 		}
993fac3fd80SJustin T. Gibbs 	}
994fac3fd80SJustin T. Gibbs 
995fac3fd80SJustin T. Gibbs 	*unit = minor >> 4;
996fac3fd80SJustin T. Gibbs 	*name = "xbd";
997fac3fd80SJustin T. Gibbs }
998fac3fd80SJustin T. Gibbs 
999fac3fd80SJustin T. Gibbs int
1000fac3fd80SJustin T. Gibbs xbd_instance_create(struct xbd_softc *sc, blkif_sector_t sectors,
1001d5d7399dSAlexander Motin     int vdevice, uint16_t vdisk_info, unsigned long sector_size,
1002d5d7399dSAlexander Motin     unsigned long phys_sector_size)
1003fac3fd80SJustin T. Gibbs {
10049985113bSJustin T. Gibbs 	char features[80];
1005fac3fd80SJustin T. Gibbs 	int unit, error = 0;
1006fac3fd80SJustin T. Gibbs 	const char *name;
1007fac3fd80SJustin T. Gibbs 
100833eebb6aSJustin T. Gibbs 	xbd_vdevice_to_unit(vdevice, &unit, &name);
100923dc5621SKip Macy 
1010fac3fd80SJustin T. Gibbs 	sc->xbd_unit = unit;
101189e0f4d2SKip Macy 
10129985113bSJustin T. Gibbs 	if (strcmp(name, "xbd") != 0)
1013fac3fd80SJustin T. Gibbs 		device_printf(sc->xbd_dev, "attaching as %s%d\n", name, unit);
101489e0f4d2SKip Macy 
10159985113bSJustin T. Gibbs 	if (xbd_feature_string(sc, features, sizeof(features)) > 0) {
10169985113bSJustin T. Gibbs 		device_printf(sc->xbd_dev, "features: %s\n",
10179985113bSJustin T. Gibbs 		    features);
10189985113bSJustin T. Gibbs 	}
10199985113bSJustin T. Gibbs 
1020fac3fd80SJustin T. Gibbs 	sc->xbd_disk = disk_alloc();
1021fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_unit = sc->xbd_unit;
1022fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_open = xbd_open;
1023fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_close = xbd_close;
1024fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_ioctl = xbd_ioctl;
1025fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_strategy = xbd_strategy;
1026fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_dump = xbd_dump;
1027fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_name = name;
1028fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_drv1 = sc;
1029fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_sectorsize = sector_size;
1030d5d7399dSAlexander Motin 	sc->xbd_disk->d_stripesize = phys_sector_size;
1031d5d7399dSAlexander Motin 	sc->xbd_disk->d_stripeoffset = 0;
10328b8bfa35SJustin T. Gibbs 
1033fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_mediasize = sectors * sector_size;
1034fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_maxsize = sc->xbd_max_request_size;
1035a55a04a8SRoger Pau Monné 	sc->xbd_disk->d_flags = DISKFLAG_UNMAPPED_BIO;
10369985113bSJustin T. Gibbs 	if ((sc->xbd_flags & (XBDF_FLUSH|XBDF_BARRIER)) != 0) {
10379985113bSJustin T. Gibbs 		sc->xbd_disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
10389985113bSJustin T. Gibbs 		device_printf(sc->xbd_dev,
10399985113bSJustin T. Gibbs 		    "synchronize cache commands enabled.\n");
10409985113bSJustin T. Gibbs 	}
1041fac3fd80SJustin T. Gibbs 	disk_create(sc->xbd_disk, DISK_VERSION);
104212678024SDoug Rabson 
1043fac3fd80SJustin T. Gibbs 	return error;
104412678024SDoug Rabson }
104512678024SDoug Rabson 
1046fac3fd80SJustin T. Gibbs static void
1047fac3fd80SJustin T. Gibbs xbd_free(struct xbd_softc *sc)
104812678024SDoug Rabson {
1049fac3fd80SJustin T. Gibbs 	int i;
105012678024SDoug Rabson 
105112678024SDoug Rabson 	/* Prevent new requests being issued until we fix things up. */
105233eebb6aSJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
1053e2c1fe90SJustin T. Gibbs 	sc->xbd_state = XBD_STATE_DISCONNECTED;
105433eebb6aSJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
105589e0f4d2SKip Macy 
1056fac3fd80SJustin T. Gibbs 	/* Free resources associated with old device channel. */
1057d9fab01dSJustin T. Gibbs 	xbd_free_ring(sc);
1058fac3fd80SJustin T. Gibbs 	if (sc->xbd_shadow) {
105989e0f4d2SKip Macy 
1060fac3fd80SJustin T. Gibbs 		for (i = 0; i < sc->xbd_max_requests; i++) {
1061fac3fd80SJustin T. Gibbs 			struct xbd_command *cm;
106289e0f4d2SKip Macy 
1063fac3fd80SJustin T. Gibbs 			cm = &sc->xbd_shadow[i];
1064fac3fd80SJustin T. Gibbs 			if (cm->cm_sg_refs != NULL) {
1065fac3fd80SJustin T. Gibbs 				free(cm->cm_sg_refs, M_XENBLOCKFRONT);
1066fac3fd80SJustin T. Gibbs 				cm->cm_sg_refs = NULL;
106789e0f4d2SKip Macy 			}
106889e0f4d2SKip Macy 
1069aaebf690SColin Percival 			if (cm->cm_indirectionpages != NULL) {
1070aaebf690SColin Percival 				gnttab_end_foreign_access_references(
1071aaebf690SColin Percival 				    sc->xbd_max_request_indirectpages,
1072aaebf690SColin Percival 				    &cm->cm_indirectionrefs[0]);
1073aaebf690SColin Percival 				contigfree(cm->cm_indirectionpages, PAGE_SIZE *
1074aaebf690SColin Percival 				    sc->xbd_max_request_indirectpages,
1075aaebf690SColin Percival 				    M_XENBLOCKFRONT);
1076aaebf690SColin Percival 				cm->cm_indirectionpages = NULL;
1077aaebf690SColin Percival 			}
1078aaebf690SColin Percival 
1079fac3fd80SJustin T. Gibbs 			bus_dmamap_destroy(sc->xbd_io_dmat, cm->cm_map);
1080fac3fd80SJustin T. Gibbs 		}
1081fac3fd80SJustin T. Gibbs 		free(sc->xbd_shadow, M_XENBLOCKFRONT);
1082fac3fd80SJustin T. Gibbs 		sc->xbd_shadow = NULL;
1083fac3fd80SJustin T. Gibbs 
1084fac3fd80SJustin T. Gibbs 		bus_dma_tag_destroy(sc->xbd_io_dmat);
1085fac3fd80SJustin T. Gibbs 
1086e2c1fe90SJustin T. Gibbs 		xbd_initq_cm(sc, XBD_Q_FREE);
1087e2c1fe90SJustin T. Gibbs 		xbd_initq_cm(sc, XBD_Q_READY);
1088e2c1fe90SJustin T. Gibbs 		xbd_initq_cm(sc, XBD_Q_COMPLETE);
1089fac3fd80SJustin T. Gibbs 	}
1090fac3fd80SJustin T. Gibbs 
109176acc41fSJustin T. Gibbs 	xen_intr_unbind(&sc->xen_intr_handle);
109276acc41fSJustin T. Gibbs 
1093fac3fd80SJustin T. Gibbs }
1094fac3fd80SJustin T. Gibbs 
1095fac3fd80SJustin T. Gibbs /*--------------------------- State Change Handlers --------------------------*/
1096ff662b5cSJustin T. Gibbs static void
109733eebb6aSJustin T. Gibbs xbd_initialize(struct xbd_softc *sc)
109889e0f4d2SKip Macy {
1099ff662b5cSJustin T. Gibbs 	const char *otherend_path;
1100ff662b5cSJustin T. Gibbs 	const char *node_path;
11018b8bfa35SJustin T. Gibbs 	uint32_t max_ring_page_order;
1102ff662b5cSJustin T. Gibbs 	int error;
110389e0f4d2SKip Macy 
110433eebb6aSJustin T. Gibbs 	if (xenbus_get_state(sc->xbd_dev) != XenbusStateInitialising) {
110506a630f6SJustin T. Gibbs 		/* Initialization has already been performed. */
1106ff662b5cSJustin T. Gibbs 		return;
110706a630f6SJustin T. Gibbs 	}
110889e0f4d2SKip Macy 
1109ff662b5cSJustin T. Gibbs 	/*
1110ff662b5cSJustin T. Gibbs 	 * Protocol defaults valid even if negotiation for a
1111ff662b5cSJustin T. Gibbs 	 * setting fails.
1112ff662b5cSJustin T. Gibbs 	 */
11138b8bfa35SJustin T. Gibbs 	max_ring_page_order = 0;
111433eebb6aSJustin T. Gibbs 	sc->xbd_ring_pages = 1;
1115ff662b5cSJustin T. Gibbs 
1116ff662b5cSJustin T. Gibbs 	/*
1117ff662b5cSJustin T. Gibbs 	 * Protocol negotiation.
1118ff662b5cSJustin T. Gibbs 	 *
1119ff662b5cSJustin T. Gibbs 	 * \note xs_gather() returns on the first encountered error, so
1120453130d9SPedro F. Giffuni 	 *       we must use independent calls in order to guarantee
1121ff662b5cSJustin T. Gibbs 	 *       we don't miss information in a sparsly populated back-end
1122ff662b5cSJustin T. Gibbs 	 *       tree.
11238b8bfa35SJustin T. Gibbs 	 *
11248b8bfa35SJustin T. Gibbs 	 * \note xs_scanf() does not update variables for unmatched
11258b8bfa35SJustin T. Gibbs 	 *	 fields.
1126ff662b5cSJustin T. Gibbs 	 */
112733eebb6aSJustin T. Gibbs 	otherend_path = xenbus_get_otherend_path(sc->xbd_dev);
112833eebb6aSJustin T. Gibbs 	node_path = xenbus_get_node(sc->xbd_dev);
11298b8bfa35SJustin T. Gibbs 
11308b8bfa35SJustin T. Gibbs 	/* Support both backend schemes for relaying ring page limits. */
11318b8bfa35SJustin T. Gibbs 	(void)xs_scanf(XST_NIL, otherend_path,
11328b8bfa35SJustin T. Gibbs 	    "max-ring-page-order", NULL, "%" PRIu32,
11338b8bfa35SJustin T. Gibbs 	    &max_ring_page_order);
113433eebb6aSJustin T. Gibbs 	sc->xbd_ring_pages = 1 << max_ring_page_order;
1135ff662b5cSJustin T. Gibbs 	(void)xs_scanf(XST_NIL, otherend_path,
1136ff662b5cSJustin T. Gibbs 	    "max-ring-pages", NULL, "%" PRIu32,
113733eebb6aSJustin T. Gibbs 	    &sc->xbd_ring_pages);
113833eebb6aSJustin T. Gibbs 	if (sc->xbd_ring_pages < 1)
113933eebb6aSJustin T. Gibbs 		sc->xbd_ring_pages = 1;
1140ff662b5cSJustin T. Gibbs 
114133eebb6aSJustin T. Gibbs 	if (sc->xbd_ring_pages > XBD_MAX_RING_PAGES) {
114233eebb6aSJustin T. Gibbs 		device_printf(sc->xbd_dev,
114333eebb6aSJustin T. Gibbs 		    "Back-end specified ring-pages of %u "
1144112cacaeSRoger Pau Monné 		    "limited to front-end limit of %u.\n",
114533eebb6aSJustin T. Gibbs 		    sc->xbd_ring_pages, XBD_MAX_RING_PAGES);
114633eebb6aSJustin T. Gibbs 		sc->xbd_ring_pages = XBD_MAX_RING_PAGES;
114789e0f4d2SKip Macy 	}
114889e0f4d2SKip Macy 
114933eebb6aSJustin T. Gibbs 	if (powerof2(sc->xbd_ring_pages) == 0) {
11508b8bfa35SJustin T. Gibbs 		uint32_t new_page_limit;
11518b8bfa35SJustin T. Gibbs 
115233eebb6aSJustin T. Gibbs 		new_page_limit = 0x01 << (fls(sc->xbd_ring_pages) - 1);
115333eebb6aSJustin T. Gibbs 		device_printf(sc->xbd_dev,
115433eebb6aSJustin T. Gibbs 		    "Back-end specified ring-pages of %u "
115533eebb6aSJustin T. Gibbs 		    "is not a power of 2. Limited to %u.\n",
115633eebb6aSJustin T. Gibbs 		    sc->xbd_ring_pages, new_page_limit);
115733eebb6aSJustin T. Gibbs 		sc->xbd_ring_pages = new_page_limit;
11588b8bfa35SJustin T. Gibbs 	}
11598b8bfa35SJustin T. Gibbs 
1160112cacaeSRoger Pau Monné 	sc->xbd_max_requests =
1161112cacaeSRoger Pau Monné 	    BLKIF_MAX_RING_REQUESTS(sc->xbd_ring_pages * PAGE_SIZE);
116233eebb6aSJustin T. Gibbs 	if (sc->xbd_max_requests > XBD_MAX_REQUESTS) {
116333eebb6aSJustin T. Gibbs 		device_printf(sc->xbd_dev,
116433eebb6aSJustin T. Gibbs 		    "Back-end specified max_requests of %u "
1165112cacaeSRoger Pau Monné 		    "limited to front-end limit of %zu.\n",
116633eebb6aSJustin T. Gibbs 		    sc->xbd_max_requests, XBD_MAX_REQUESTS);
116733eebb6aSJustin T. Gibbs 		sc->xbd_max_requests = XBD_MAX_REQUESTS;
116889e0f4d2SKip Macy 	}
1169ff662b5cSJustin T. Gibbs 
1170cdf5d66fSJustin T. Gibbs 	if (xbd_alloc_ring(sc) != 0)
1171ff662b5cSJustin T. Gibbs 		return;
1172ff662b5cSJustin T. Gibbs 
11738b8bfa35SJustin T. Gibbs 	/* Support both backend schemes for relaying ring page limits. */
117433eebb6aSJustin T. Gibbs 	if (sc->xbd_ring_pages > 1) {
1175ff662b5cSJustin T. Gibbs 		error = xs_printf(XST_NIL, node_path,
117633eebb6aSJustin T. Gibbs 		    "num-ring-pages","%u",
117733eebb6aSJustin T. Gibbs 		    sc->xbd_ring_pages);
1178ff662b5cSJustin T. Gibbs 		if (error) {
117933eebb6aSJustin T. Gibbs 			xenbus_dev_fatal(sc->xbd_dev, error,
11808b8bfa35SJustin T. Gibbs 			    "writing %s/num-ring-pages",
11818b8bfa35SJustin T. Gibbs 			    node_path);
11828b8bfa35SJustin T. Gibbs 			return;
11838b8bfa35SJustin T. Gibbs 		}
11840d172324SJustin T. Gibbs 
11858b8bfa35SJustin T. Gibbs 		error = xs_printf(XST_NIL, node_path,
11860d172324SJustin T. Gibbs 		    "ring-page-order", "%u",
118733eebb6aSJustin T. Gibbs 		    fls(sc->xbd_ring_pages) - 1);
11888b8bfa35SJustin T. Gibbs 		if (error) {
118933eebb6aSJustin T. Gibbs 			xenbus_dev_fatal(sc->xbd_dev, error,
11908b8bfa35SJustin T. Gibbs 			    "writing %s/ring-page-order",
1191ff662b5cSJustin T. Gibbs 			    node_path);
1192ff662b5cSJustin T. Gibbs 			return;
1193ff662b5cSJustin T. Gibbs 		}
11940d172324SJustin T. Gibbs 	}
1195ff662b5cSJustin T. Gibbs 
1196ff662b5cSJustin T. Gibbs 	error = xs_printf(XST_NIL, node_path, "event-channel",
119776acc41fSJustin T. Gibbs 	    "%u", xen_intr_port(sc->xen_intr_handle));
1198ff662b5cSJustin T. Gibbs 	if (error) {
119933eebb6aSJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
1200ff662b5cSJustin T. Gibbs 		    "writing %s/event-channel",
1201ff662b5cSJustin T. Gibbs 		    node_path);
1202ff662b5cSJustin T. Gibbs 		return;
1203ff662b5cSJustin T. Gibbs 	}
1204ff662b5cSJustin T. Gibbs 
120533eebb6aSJustin T. Gibbs 	error = xs_printf(XST_NIL, node_path, "protocol",
120633eebb6aSJustin T. Gibbs 	    "%s", XEN_IO_PROTO_ABI_NATIVE);
1207ff662b5cSJustin T. Gibbs 	if (error) {
120833eebb6aSJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
1209ff662b5cSJustin T. Gibbs 		    "writing %s/protocol",
1210ff662b5cSJustin T. Gibbs 		    node_path);
1211ff662b5cSJustin T. Gibbs 		return;
121223dc5621SKip Macy 	}
121312678024SDoug Rabson 
121433eebb6aSJustin T. Gibbs 	xenbus_set_state(sc->xbd_dev, XenbusStateInitialised);
121589e0f4d2SKip Macy }
121689e0f4d2SKip Macy 
121789e0f4d2SKip Macy /*
121833eebb6aSJustin T. Gibbs  * Invoked when the backend is finally 'ready' (and has published
121933eebb6aSJustin T. Gibbs  * the details about the physical device - #sectors, size, etc).
122089e0f4d2SKip Macy  */
122189e0f4d2SKip Macy static void
122233eebb6aSJustin T. Gibbs xbd_connect(struct xbd_softc *sc)
122389e0f4d2SKip Macy {
122433eebb6aSJustin T. Gibbs 	device_t dev = sc->xbd_dev;
1225d5d7399dSAlexander Motin 	unsigned long sectors, sector_size, phys_sector_size;
122689e0f4d2SKip Macy 	unsigned int binfo;
12279985113bSJustin T. Gibbs 	int err, feature_barrier, feature_flush;
1228aaebf690SColin Percival 	int i, j;
122989e0f4d2SKip Macy 
1230e2c1fe90SJustin T. Gibbs 	if (sc->xbd_state == XBD_STATE_CONNECTED ||
1231e2c1fe90SJustin T. Gibbs 	    sc->xbd_state == XBD_STATE_SUSPENDED)
123289e0f4d2SKip Macy 		return;
123389e0f4d2SKip Macy 
123423dc5621SKip Macy 	DPRINTK("blkfront.c:connect:%s.\n", xenbus_get_otherend_path(dev));
123589e0f4d2SKip Macy 
1236ff662b5cSJustin T. Gibbs 	err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
123789e0f4d2SKip Macy 	    "sectors", "%lu", &sectors,
123889e0f4d2SKip Macy 	    "info", "%u", &binfo,
123989e0f4d2SKip Macy 	    "sector-size", "%lu", &sector_size,
124089e0f4d2SKip Macy 	    NULL);
124189e0f4d2SKip Macy 	if (err) {
124223dc5621SKip Macy 		xenbus_dev_fatal(dev, err,
124389e0f4d2SKip Macy 		    "reading backend fields at %s",
124423dc5621SKip Macy 		    xenbus_get_otherend_path(dev));
124589e0f4d2SKip Macy 		return;
124689e0f4d2SKip Macy 	}
124793954c2dSColin Percival 	if ((sectors == 0) || (sector_size == 0)) {
124893954c2dSColin Percival 		xenbus_dev_fatal(dev, 0,
124993954c2dSColin Percival 		    "invalid parameters from %s:"
125093954c2dSColin Percival 		    " sectors = %lu, sector_size = %lu",
125193954c2dSColin Percival 		    xenbus_get_otherend_path(dev),
125293954c2dSColin Percival 		    sectors, sector_size);
125393954c2dSColin Percival 		return;
125493954c2dSColin Percival 	}
1255ff662b5cSJustin T. Gibbs 	err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
1256d5d7399dSAlexander Motin 	     "physical-sector-size", "%lu", &phys_sector_size,
1257d5d7399dSAlexander Motin 	     NULL);
1258d5d7399dSAlexander Motin 	if (err || phys_sector_size <= sector_size)
1259d5d7399dSAlexander Motin 		phys_sector_size = 0;
1260d5d7399dSAlexander Motin 	err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
1261085def3fSDimitry Andric 	     "feature-barrier", "%d", &feature_barrier,
126289e0f4d2SKip Macy 	     NULL);
12639985113bSJustin T. Gibbs 	if (err == 0 && feature_barrier != 0)
1264e2c1fe90SJustin T. Gibbs 		sc->xbd_flags |= XBDF_BARRIER;
126589e0f4d2SKip Macy 
12669985113bSJustin T. Gibbs 	err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
1267085def3fSDimitry Andric 	     "feature-flush-cache", "%d", &feature_flush,
12689985113bSJustin T. Gibbs 	     NULL);
12699985113bSJustin T. Gibbs 	if (err == 0 && feature_flush != 0)
12709985113bSJustin T. Gibbs 		sc->xbd_flags |= XBDF_FLUSH;
12719985113bSJustin T. Gibbs 
1272aaebf690SColin Percival 	err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
1273aaebf690SColin Percival 	    "feature-max-indirect-segments", "%" PRIu32,
1274aaebf690SColin Percival 	    &sc->xbd_max_request_segments, NULL);
1275aaebf690SColin Percival 	if ((err != 0) || (xbd_enable_indirect == 0))
1276aaebf690SColin Percival 		sc->xbd_max_request_segments = 0;
1277aaebf690SColin Percival 	if (sc->xbd_max_request_segments > XBD_MAX_INDIRECT_SEGMENTS)
1278aaebf690SColin Percival 		sc->xbd_max_request_segments = XBD_MAX_INDIRECT_SEGMENTS;
1279aaebf690SColin Percival 	if (sc->xbd_max_request_segments > XBD_SIZE_TO_SEGS(MAXPHYS))
1280aaebf690SColin Percival 		sc->xbd_max_request_segments = XBD_SIZE_TO_SEGS(MAXPHYS);
1281aaebf690SColin Percival 	sc->xbd_max_request_indirectpages =
1282aaebf690SColin Percival 	    XBD_INDIRECT_SEGS_TO_PAGES(sc->xbd_max_request_segments);
1283aaebf690SColin Percival 	if (sc->xbd_max_request_segments < BLKIF_MAX_SEGMENTS_PER_REQUEST)
1284aaebf690SColin Percival 		sc->xbd_max_request_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST;
1285aaebf690SColin Percival 	sc->xbd_max_request_size =
1286aaebf690SColin Percival 	    XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments);
1287aaebf690SColin Percival 
128891fb36cfSColin Percival 	/* Allocate datastructures based on negotiated values. */
128991fb36cfSColin Percival 	err = bus_dma_tag_create(
129091fb36cfSColin Percival 	    bus_get_dma_tag(sc->xbd_dev),	/* parent */
129191fb36cfSColin Percival 	    512, PAGE_SIZE,			/* algnmnt, boundary */
129291fb36cfSColin Percival 	    BUS_SPACE_MAXADDR,			/* lowaddr */
129391fb36cfSColin Percival 	    BUS_SPACE_MAXADDR,			/* highaddr */
129491fb36cfSColin Percival 	    NULL, NULL,				/* filter, filterarg */
129591fb36cfSColin Percival 	    sc->xbd_max_request_size,
129691fb36cfSColin Percival 	    sc->xbd_max_request_segments,
129791fb36cfSColin Percival 	    PAGE_SIZE,				/* maxsegsize */
129891fb36cfSColin Percival 	    BUS_DMA_ALLOCNOW,			/* flags */
129991fb36cfSColin Percival 	    busdma_lock_mutex,			/* lockfunc */
130091fb36cfSColin Percival 	    &sc->xbd_io_lock,			/* lockarg */
130191fb36cfSColin Percival 	    &sc->xbd_io_dmat);
130291fb36cfSColin Percival 	if (err != 0) {
130391fb36cfSColin Percival 		xenbus_dev_fatal(sc->xbd_dev, err,
130491fb36cfSColin Percival 		    "Cannot allocate parent DMA tag\n");
130591fb36cfSColin Percival 		return;
130691fb36cfSColin Percival 	}
130791fb36cfSColin Percival 
130891fb36cfSColin Percival 	/* Per-transaction data allocation. */
1309ac2fffa4SPedro F. Giffuni 	sc->xbd_shadow = malloc(sizeof(*sc->xbd_shadow) * sc->xbd_max_requests,
1310ac2fffa4SPedro F. Giffuni 	    M_XENBLOCKFRONT, M_NOWAIT|M_ZERO);
131191fb36cfSColin Percival 	if (sc->xbd_shadow == NULL) {
131291fb36cfSColin Percival 		bus_dma_tag_destroy(sc->xbd_io_dmat);
131391fb36cfSColin Percival 		xenbus_dev_fatal(sc->xbd_dev, ENOMEM,
131491fb36cfSColin Percival 		    "Cannot allocate request structures\n");
131591fb36cfSColin Percival 		return;
131691fb36cfSColin Percival 	}
131791fb36cfSColin Percival 
131891fb36cfSColin Percival 	for (i = 0; i < sc->xbd_max_requests; i++) {
131991fb36cfSColin Percival 		struct xbd_command *cm;
1320aaebf690SColin Percival 		void * indirectpages;
132191fb36cfSColin Percival 
132291fb36cfSColin Percival 		cm = &sc->xbd_shadow[i];
1323ac2fffa4SPedro F. Giffuni 		cm->cm_sg_refs = malloc(
1324ac2fffa4SPedro F. Giffuni 		    sizeof(grant_ref_t) * sc->xbd_max_request_segments,
1325ac2fffa4SPedro F. Giffuni 		    M_XENBLOCKFRONT, M_NOWAIT);
132691fb36cfSColin Percival 		if (cm->cm_sg_refs == NULL)
132791fb36cfSColin Percival 			break;
132891fb36cfSColin Percival 		cm->cm_id = i;
132991fb36cfSColin Percival 		cm->cm_flags = XBDCF_INITIALIZER;
133091fb36cfSColin Percival 		cm->cm_sc = sc;
133191fb36cfSColin Percival 		if (bus_dmamap_create(sc->xbd_io_dmat, 0, &cm->cm_map) != 0)
133291fb36cfSColin Percival 			break;
1333aaebf690SColin Percival 		if (sc->xbd_max_request_indirectpages > 0) {
1334aaebf690SColin Percival 			indirectpages = contigmalloc(
1335aaebf690SColin Percival 			    PAGE_SIZE * sc->xbd_max_request_indirectpages,
1336*83c2fa73SRoger Pau Monné 			    M_XENBLOCKFRONT, M_ZERO | M_NOWAIT, 0, ~0,
1337*83c2fa73SRoger Pau Monné 			    PAGE_SIZE, 0);
1338*83c2fa73SRoger Pau Monné 			if (indirectpages == NULL)
1339*83c2fa73SRoger Pau Monné 				sc->xbd_max_request_indirectpages = 0;
1340aaebf690SColin Percival 		} else {
1341aaebf690SColin Percival 			indirectpages = NULL;
1342aaebf690SColin Percival 		}
1343aaebf690SColin Percival 		for (j = 0; j < sc->xbd_max_request_indirectpages; j++) {
1344aaebf690SColin Percival 			if (gnttab_grant_foreign_access(
1345aaebf690SColin Percival 			    xenbus_get_otherend_id(sc->xbd_dev),
13463c790178SJohn Baldwin 			    (vtophys(indirectpages) >> PAGE_SHIFT) + j,
1347aaebf690SColin Percival 			    1 /* grant read-only access */,
1348aaebf690SColin Percival 			    &cm->cm_indirectionrefs[j]))
1349aaebf690SColin Percival 				break;
1350aaebf690SColin Percival 		}
1351*83c2fa73SRoger Pau Monné 		if (j < sc->xbd_max_request_indirectpages) {
1352*83c2fa73SRoger Pau Monné 			contigfree(indirectpages,
1353*83c2fa73SRoger Pau Monné 			    PAGE_SIZE * sc->xbd_max_request_indirectpages,
1354*83c2fa73SRoger Pau Monné 			    M_XENBLOCKFRONT);
1355aaebf690SColin Percival 			break;
1356*83c2fa73SRoger Pau Monné 		}
1357aaebf690SColin Percival 		cm->cm_indirectionpages = indirectpages;
135891fb36cfSColin Percival 		xbd_free_command(cm);
135991fb36cfSColin Percival 	}
136091fb36cfSColin Percival 
136133eebb6aSJustin T. Gibbs 	if (sc->xbd_disk == NULL) {
136223dc5621SKip Macy 		device_printf(dev, "%juMB <%s> at %s",
136323dc5621SKip Macy 		    (uintmax_t) sectors / (1048576 / sector_size),
136423dc5621SKip Macy 		    device_get_desc(dev),
136523dc5621SKip Macy 		    xenbus_get_node(dev));
136623dc5621SKip Macy 		bus_print_child_footer(device_get_parent(dev), dev);
136789e0f4d2SKip Macy 
136833eebb6aSJustin T. Gibbs 		xbd_instance_create(sc, sectors, sc->xbd_vdevice, binfo,
1369d5d7399dSAlexander Motin 		    sector_size, phys_sector_size);
137006a630f6SJustin T. Gibbs 	}
137123dc5621SKip Macy 
137223dc5621SKip Macy 	(void)xenbus_set_state(dev, XenbusStateConnected);
137389e0f4d2SKip Macy 
137489e0f4d2SKip Macy 	/* Kick pending requests. */
137533eebb6aSJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
1376e2c1fe90SJustin T. Gibbs 	sc->xbd_state = XBD_STATE_CONNECTED;
137733eebb6aSJustin T. Gibbs 	xbd_startio(sc);
1378e2c1fe90SJustin T. Gibbs 	sc->xbd_flags |= XBDF_READY;
137933eebb6aSJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
138089e0f4d2SKip Macy }
138189e0f4d2SKip Macy 
138289e0f4d2SKip Macy /**
138389e0f4d2SKip Macy  * Handle the change of state of the backend to Closing.  We must delete our
138489e0f4d2SKip Macy  * device-layer structures now, to ensure that writes are flushed through to
1385a4660d59SJustin T. Gibbs  * the backend.  Once this is done, we can switch to Closed in
138689e0f4d2SKip Macy  * acknowledgement.
138789e0f4d2SKip Macy  */
138823dc5621SKip Macy static void
138933eebb6aSJustin T. Gibbs xbd_closing(device_t dev)
139089e0f4d2SKip Macy {
139133eebb6aSJustin T. Gibbs 	struct xbd_softc *sc = device_get_softc(dev);
139289e0f4d2SKip Macy 
1393ff662b5cSJustin T. Gibbs 	xenbus_set_state(dev, XenbusStateClosing);
1394ff662b5cSJustin T. Gibbs 
139533eebb6aSJustin T. Gibbs 	DPRINTK("xbd_closing: %s removed\n", xenbus_get_node(dev));
139689e0f4d2SKip Macy 
139733eebb6aSJustin T. Gibbs 	if (sc->xbd_disk != NULL) {
139833eebb6aSJustin T. Gibbs 		disk_destroy(sc->xbd_disk);
139933eebb6aSJustin T. Gibbs 		sc->xbd_disk = NULL;
140089e0f4d2SKip Macy 	}
140189e0f4d2SKip Macy 
140223dc5621SKip Macy 	xenbus_set_state(dev, XenbusStateClosed);
140389e0f4d2SKip Macy }
140489e0f4d2SKip Macy 
1405fac3fd80SJustin T. Gibbs /*---------------------------- NewBus Entrypoints ----------------------------*/
1406fac3fd80SJustin T. Gibbs static int
1407fac3fd80SJustin T. Gibbs xbd_probe(device_t dev)
1408fac3fd80SJustin T. Gibbs {
1409a371f519SJustin T. Gibbs 	if (strcmp(xenbus_get_type(dev), "vbd") != 0)
1410a371f519SJustin T. Gibbs 		return (ENXIO);
1411fac3fd80SJustin T. Gibbs 
1412f8f1bb83SRoger Pau Monné 	if (xen_hvm_domain() && xen_disable_pv_disks != 0)
1413f8f1bb83SRoger Pau Monné 		return (ENXIO);
1414f8f1bb83SRoger Pau Monné 
1415a371f519SJustin T. Gibbs 	if (xen_hvm_domain()) {
1416a371f519SJustin T. Gibbs 		int error;
1417a371f519SJustin T. Gibbs 		char *type;
1418a371f519SJustin T. Gibbs 
1419a371f519SJustin T. Gibbs 		/*
1420a371f519SJustin T. Gibbs 		 * When running in an HVM domain, IDE disk emulation is
1421a371f519SJustin T. Gibbs 		 * disabled early in boot so that native drivers will
1422a371f519SJustin T. Gibbs 		 * not see emulated hardware.  However, CDROM device
1423a371f519SJustin T. Gibbs 		 * emulation cannot be disabled.
1424a371f519SJustin T. Gibbs 		 *
1425a371f519SJustin T. Gibbs 		 * Through use of FreeBSD's vm_guest and xen_hvm_domain()
1426a371f519SJustin T. Gibbs 		 * APIs, we could modify the native CDROM driver to fail its
1427a371f519SJustin T. Gibbs 		 * probe when running under Xen.  Unfortunatlely, the PV
1428a371f519SJustin T. Gibbs 		 * CDROM support in XenServer (up through at least version
1429a371f519SJustin T. Gibbs 		 * 6.2) isn't functional, so we instead rely on the emulated
1430a371f519SJustin T. Gibbs 		 * CDROM instance, and fail to attach the PV one here in
1431a371f519SJustin T. Gibbs 		 * the blkfront driver.
1432a371f519SJustin T. Gibbs 		 */
1433a371f519SJustin T. Gibbs 		error = xs_read(XST_NIL, xenbus_get_node(dev),
1434a371f519SJustin T. Gibbs 		    "device-type", NULL, (void **) &type);
1435a371f519SJustin T. Gibbs 		if (error)
1436a371f519SJustin T. Gibbs 			return (ENXIO);
1437a371f519SJustin T. Gibbs 
1438a371f519SJustin T. Gibbs 		if (strncmp(type, "cdrom", 5) == 0) {
1439a371f519SJustin T. Gibbs 			free(type, M_XENSTORE);
1440a371f519SJustin T. Gibbs 			return (ENXIO);
1441a371f519SJustin T. Gibbs 		}
1442a371f519SJustin T. Gibbs 		free(type, M_XENSTORE);
1443a371f519SJustin T. Gibbs 	}
1444a371f519SJustin T. Gibbs 
1445fac3fd80SJustin T. Gibbs 	device_set_desc(dev, "Virtual Block Device");
1446fac3fd80SJustin T. Gibbs 	device_quiet(dev);
1447fac3fd80SJustin T. Gibbs 	return (0);
1448fac3fd80SJustin T. Gibbs }
1449fac3fd80SJustin T. Gibbs 
1450fac3fd80SJustin T. Gibbs /*
1451fac3fd80SJustin T. Gibbs  * Setup supplies the backend dir, virtual device.  We place an event
1452fac3fd80SJustin T. Gibbs  * channel and shared frame entries.  We watch backend to wait if it's
1453fac3fd80SJustin T. Gibbs  * ok.
1454fac3fd80SJustin T. Gibbs  */
1455fac3fd80SJustin T. Gibbs static int
1456fac3fd80SJustin T. Gibbs xbd_attach(device_t dev)
1457fac3fd80SJustin T. Gibbs {
1458fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc;
1459fac3fd80SJustin T. Gibbs 	const char *name;
1460fac3fd80SJustin T. Gibbs 	uint32_t vdevice;
1461fac3fd80SJustin T. Gibbs 	int error;
1462fac3fd80SJustin T. Gibbs 	int i;
1463fac3fd80SJustin T. Gibbs 	int unit;
1464fac3fd80SJustin T. Gibbs 
1465fac3fd80SJustin T. Gibbs 	/* FIXME: Use dynamic device id if this is not set. */
1466fac3fd80SJustin T. Gibbs 	error = xs_scanf(XST_NIL, xenbus_get_node(dev),
1467fac3fd80SJustin T. Gibbs 	    "virtual-device", NULL, "%" PRIu32, &vdevice);
146848a1ceedSColin Percival 	if (error)
146948a1ceedSColin Percival 		error = xs_scanf(XST_NIL, xenbus_get_node(dev),
147048a1ceedSColin Percival 		    "virtual-device-ext", NULL, "%" PRIu32, &vdevice);
1471fac3fd80SJustin T. Gibbs 	if (error) {
1472fac3fd80SJustin T. Gibbs 		xenbus_dev_fatal(dev, error, "reading virtual-device");
1473fac3fd80SJustin T. Gibbs 		device_printf(dev, "Couldn't determine virtual device.\n");
1474fac3fd80SJustin T. Gibbs 		return (error);
1475fac3fd80SJustin T. Gibbs 	}
1476fac3fd80SJustin T. Gibbs 
1477fac3fd80SJustin T. Gibbs 	xbd_vdevice_to_unit(vdevice, &unit, &name);
1478fac3fd80SJustin T. Gibbs 	if (!strcmp(name, "xbd"))
1479fac3fd80SJustin T. Gibbs 		device_set_unit(dev, unit);
1480fac3fd80SJustin T. Gibbs 
1481fac3fd80SJustin T. Gibbs 	sc = device_get_softc(dev);
1482fac3fd80SJustin T. Gibbs 	mtx_init(&sc->xbd_io_lock, "blkfront i/o lock", NULL, MTX_DEF);
1483e2c1fe90SJustin T. Gibbs 	xbd_initqs(sc);
1484fac3fd80SJustin T. Gibbs 	for (i = 0; i < XBD_MAX_RING_PAGES; i++)
1485d9fab01dSJustin T. Gibbs 		sc->xbd_ring_ref[i] = GRANT_REF_INVALID;
1486fac3fd80SJustin T. Gibbs 
1487fac3fd80SJustin T. Gibbs 	sc->xbd_dev = dev;
1488fac3fd80SJustin T. Gibbs 	sc->xbd_vdevice = vdevice;
1489e2c1fe90SJustin T. Gibbs 	sc->xbd_state = XBD_STATE_DISCONNECTED;
1490fac3fd80SJustin T. Gibbs 
1491fac3fd80SJustin T. Gibbs 	xbd_setup_sysctl(sc);
1492fac3fd80SJustin T. Gibbs 
1493fac3fd80SJustin T. Gibbs 	/* Wait for backend device to publish its protocol capabilities. */
1494fac3fd80SJustin T. Gibbs 	xenbus_set_state(dev, XenbusStateInitialising);
1495fac3fd80SJustin T. Gibbs 
1496fac3fd80SJustin T. Gibbs 	return (0);
1497fac3fd80SJustin T. Gibbs }
149889e0f4d2SKip Macy 
149923dc5621SKip Macy static int
150033eebb6aSJustin T. Gibbs xbd_detach(device_t dev)
150189e0f4d2SKip Macy {
150233eebb6aSJustin T. Gibbs 	struct xbd_softc *sc = device_get_softc(dev);
150389e0f4d2SKip Macy 
1504e2c1fe90SJustin T. Gibbs 	DPRINTK("%s: %s removed\n", __func__, xenbus_get_node(dev));
150589e0f4d2SKip Macy 
150633eebb6aSJustin T. Gibbs 	xbd_free(sc);
150733eebb6aSJustin T. Gibbs 	mtx_destroy(&sc->xbd_io_lock);
150889e0f4d2SKip Macy 
150989e0f4d2SKip Macy 	return 0;
151089e0f4d2SKip Macy }
151189e0f4d2SKip Macy 
151289e0f4d2SKip Macy static int
1513fac3fd80SJustin T. Gibbs xbd_suspend(device_t dev)
151489e0f4d2SKip Macy {
1515fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = device_get_softc(dev);
1516fac3fd80SJustin T. Gibbs 	int retval;
1517fac3fd80SJustin T. Gibbs 	int saved_state;
151889e0f4d2SKip Macy 
151989e0f4d2SKip Macy 	/* Prevent new requests being issued until we fix things up. */
152033eebb6aSJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
1521e2c1fe90SJustin T. Gibbs 	saved_state = sc->xbd_state;
1522e2c1fe90SJustin T. Gibbs 	sc->xbd_state = XBD_STATE_SUSPENDED;
1523fac3fd80SJustin T. Gibbs 
1524fac3fd80SJustin T. Gibbs 	/* Wait for outstanding I/O to drain. */
1525fac3fd80SJustin T. Gibbs 	retval = 0;
15269985113bSJustin T. Gibbs 	while (xbd_queue_length(sc, XBD_Q_BUSY) != 0) {
1527e2c1fe90SJustin T. Gibbs 		if (msleep(&sc->xbd_cm_q[XBD_Q_BUSY], &sc->xbd_io_lock,
1528fac3fd80SJustin T. Gibbs 		    PRIBIO, "blkf_susp", 30 * hz) == EWOULDBLOCK) {
1529fac3fd80SJustin T. Gibbs 			retval = EBUSY;
1530fac3fd80SJustin T. Gibbs 			break;
1531fac3fd80SJustin T. Gibbs 		}
1532fac3fd80SJustin T. Gibbs 	}
153333eebb6aSJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
153489e0f4d2SKip Macy 
1535fac3fd80SJustin T. Gibbs 	if (retval != 0)
1536e2c1fe90SJustin T. Gibbs 		sc->xbd_state = saved_state;
153733eebb6aSJustin T. Gibbs 
1538fac3fd80SJustin T. Gibbs 	return (retval);
153989e0f4d2SKip Macy }
154089e0f4d2SKip Macy 
1541ff662b5cSJustin T. Gibbs static int
1542fac3fd80SJustin T. Gibbs xbd_resume(device_t dev)
154389e0f4d2SKip Macy {
1544fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = device_get_softc(dev);
1545fac3fd80SJustin T. Gibbs 
15468dee0e9bSRoger Pau Monné 	if (xen_suspend_cancelled) {
15478dee0e9bSRoger Pau Monné 		sc->xbd_state = XBD_STATE_CONNECTED;
15488dee0e9bSRoger Pau Monné 		return (0);
15498dee0e9bSRoger Pau Monné 	}
15508dee0e9bSRoger Pau Monné 
1551fac3fd80SJustin T. Gibbs 	DPRINTK("xbd_resume: %s\n", xenbus_get_node(dev));
1552fac3fd80SJustin T. Gibbs 
1553fac3fd80SJustin T. Gibbs 	xbd_free(sc);
1554fac3fd80SJustin T. Gibbs 	xbd_initialize(sc);
1555fac3fd80SJustin T. Gibbs 	return (0);
155689e0f4d2SKip Macy }
155789e0f4d2SKip Macy 
1558fac3fd80SJustin T. Gibbs /**
1559fac3fd80SJustin T. Gibbs  * Callback received when the backend's state changes.
1560fac3fd80SJustin T. Gibbs  */
1561fac3fd80SJustin T. Gibbs static void
1562fac3fd80SJustin T. Gibbs xbd_backend_changed(device_t dev, XenbusState backend_state)
1563fac3fd80SJustin T. Gibbs {
1564fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = device_get_softc(dev);
1565fac3fd80SJustin T. Gibbs 
1566fac3fd80SJustin T. Gibbs 	DPRINTK("backend_state=%d\n", backend_state);
1567fac3fd80SJustin T. Gibbs 
1568fac3fd80SJustin T. Gibbs 	switch (backend_state) {
1569fac3fd80SJustin T. Gibbs 	case XenbusStateUnknown:
1570fac3fd80SJustin T. Gibbs 	case XenbusStateInitialising:
1571fac3fd80SJustin T. Gibbs 	case XenbusStateReconfigured:
1572fac3fd80SJustin T. Gibbs 	case XenbusStateReconfiguring:
1573fac3fd80SJustin T. Gibbs 	case XenbusStateClosed:
1574fac3fd80SJustin T. Gibbs 		break;
1575fac3fd80SJustin T. Gibbs 
1576fac3fd80SJustin T. Gibbs 	case XenbusStateInitWait:
1577fac3fd80SJustin T. Gibbs 	case XenbusStateInitialised:
1578fac3fd80SJustin T. Gibbs 		xbd_initialize(sc);
1579fac3fd80SJustin T. Gibbs 		break;
1580fac3fd80SJustin T. Gibbs 
1581fac3fd80SJustin T. Gibbs 	case XenbusStateConnected:
1582fac3fd80SJustin T. Gibbs 		xbd_initialize(sc);
1583fac3fd80SJustin T. Gibbs 		xbd_connect(sc);
1584fac3fd80SJustin T. Gibbs 		break;
1585fac3fd80SJustin T. Gibbs 
1586fac3fd80SJustin T. Gibbs 	case XenbusStateClosing:
1587e5d27b37SRoger Pau Monné 		if (sc->xbd_users > 0) {
1588e5d27b37SRoger Pau Monné 			device_printf(dev, "detaching with pending users\n");
1589e5d27b37SRoger Pau Monné 			KASSERT(sc->xbd_disk != NULL,
1590e5d27b37SRoger Pau Monné 			    ("NULL disk with pending users\n"));
1591e5d27b37SRoger Pau Monné 			disk_gone(sc->xbd_disk);
1592e5d27b37SRoger Pau Monné 		} else {
1593fac3fd80SJustin T. Gibbs 			xbd_closing(dev);
1594e5d27b37SRoger Pau Monné 		}
1595fac3fd80SJustin T. Gibbs 		break;
1596fac3fd80SJustin T. Gibbs 	}
1597fac3fd80SJustin T. Gibbs }
1598fac3fd80SJustin T. Gibbs 
1599fac3fd80SJustin T. Gibbs /*---------------------------- NewBus Registration ---------------------------*/
160033eebb6aSJustin T. Gibbs static device_method_t xbd_methods[] = {
160123dc5621SKip Macy 	/* Device interface */
160233eebb6aSJustin T. Gibbs 	DEVMETHOD(device_probe,         xbd_probe),
160333eebb6aSJustin T. Gibbs 	DEVMETHOD(device_attach,        xbd_attach),
160433eebb6aSJustin T. Gibbs 	DEVMETHOD(device_detach,        xbd_detach),
160523dc5621SKip Macy 	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
160633eebb6aSJustin T. Gibbs 	DEVMETHOD(device_suspend,       xbd_suspend),
160733eebb6aSJustin T. Gibbs 	DEVMETHOD(device_resume,        xbd_resume),
160889e0f4d2SKip Macy 
160923dc5621SKip Macy 	/* Xenbus interface */
161033eebb6aSJustin T. Gibbs 	DEVMETHOD(xenbus_otherend_changed, xbd_backend_changed),
161189e0f4d2SKip Macy 
161223dc5621SKip Macy 	{ 0, 0 }
161389e0f4d2SKip Macy };
161489e0f4d2SKip Macy 
161533eebb6aSJustin T. Gibbs static driver_t xbd_driver = {
161623dc5621SKip Macy 	"xbd",
161733eebb6aSJustin T. Gibbs 	xbd_methods,
161833eebb6aSJustin T. Gibbs 	sizeof(struct xbd_softc),
161989e0f4d2SKip Macy };
162033eebb6aSJustin T. Gibbs devclass_t xbd_devclass;
162189e0f4d2SKip Macy 
162233eebb6aSJustin T. Gibbs DRIVER_MODULE(xbd, xenbusb_front, xbd_driver, xbd_devclass, 0, 0);
1623