xref: /freebsd/sys/dev/xen/blkfront/blkfront.c (revision d1bdc2821fcd416ab9b238580386eb605a6128d0)
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/param.h>
3289e0f4d2SKip Macy #include <sys/systm.h>
3389e0f4d2SKip Macy #include <sys/malloc.h>
3489e0f4d2SKip Macy #include <sys/kernel.h>
3589e0f4d2SKip Macy #include <vm/vm.h>
3689e0f4d2SKip Macy #include <vm/pmap.h>
3789e0f4d2SKip Macy 
3889e0f4d2SKip Macy #include <sys/bio.h>
3989e0f4d2SKip Macy #include <sys/bus.h>
4089e0f4d2SKip Macy #include <sys/conf.h>
4189e0f4d2SKip Macy #include <sys/module.h>
428b8bfa35SJustin T. Gibbs #include <sys/sysctl.h>
4389e0f4d2SKip Macy 
4489e0f4d2SKip Macy #include <machine/bus.h>
4589e0f4d2SKip Macy #include <sys/rman.h>
4689e0f4d2SKip Macy #include <machine/resource.h>
4789e0f4d2SKip Macy #include <machine/vmparam.h>
4889e0f4d2SKip Macy 
4976acc41fSJustin T. Gibbs #include <xen/xen-os.h>
5012678024SDoug Rabson #include <xen/hypervisor.h>
513a6d1fcfSKip Macy #include <xen/xen_intr.h>
5212678024SDoug Rabson #include <xen/gnttab.h>
53ad7dd514SElliott Mitchell #include <contrib/xen/grant_table.h>
54ad7dd514SElliott Mitchell #include <contrib/xen/io/protocols.h>
5523dc5621SKip Macy #include <xen/xenbus/xenbusvar.h>
5689e0f4d2SKip Macy 
5776acc41fSJustin T. Gibbs #include <machine/_inttypes.h>
5876acc41fSJustin T. Gibbs 
5989e0f4d2SKip Macy #include <geom/geom_disk.h>
6089e0f4d2SKip Macy 
6189e0f4d2SKip Macy #include <dev/xen/blkfront/block.h>
6289e0f4d2SKip Macy 
6323dc5621SKip Macy #include "xenbus_if.h"
6423dc5621SKip Macy 
65fac3fd80SJustin T. Gibbs /*--------------------------- Forward Declarations ---------------------------*/
6633eebb6aSJustin T. Gibbs static void xbd_closing(device_t);
67fac3fd80SJustin T. Gibbs static void xbd_startio(struct xbd_softc *sc);
6889e0f4d2SKip Macy 
69fac3fd80SJustin T. Gibbs /*---------------------------------- Macros ----------------------------------*/
70fac3fd80SJustin T. Gibbs #if 0
71fac3fd80SJustin T. Gibbs #define DPRINTK(fmt, args...) printf("[XEN] %s:%d: " fmt ".\n", __func__, __LINE__, ##args)
72fac3fd80SJustin T. Gibbs #else
73fac3fd80SJustin T. Gibbs #define DPRINTK(fmt, args...)
74fac3fd80SJustin T. Gibbs #endif
75fac3fd80SJustin T. Gibbs 
76fac3fd80SJustin T. Gibbs #define XBD_SECTOR_SHFT		9
77ff662b5cSJustin T. Gibbs 
78fac3fd80SJustin T. Gibbs /*---------------------------- Global Static Data ----------------------------*/
79fac3fd80SJustin T. Gibbs static MALLOC_DEFINE(M_XENBLOCKFRONT, "xbd", "Xen Block Front driver data");
8089e0f4d2SKip Macy 
81aaebf690SColin Percival static int xbd_enable_indirect = 1;
8241fc1ce1SPawel Biernacki SYSCTL_NODE(_hw, OID_AUTO, xbd, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
8341fc1ce1SPawel Biernacki     "xbd driver parameters");
84aaebf690SColin Percival SYSCTL_INT(_hw_xbd, OID_AUTO, xbd_enable_indirect, CTLFLAG_RDTUN,
85aaebf690SColin Percival     &xbd_enable_indirect, 0, "Enable xbd indirect segments");
86aaebf690SColin Percival 
87fac3fd80SJustin T. Gibbs /*---------------------------- Command Processing ----------------------------*/
88127a9483SJustin T. Gibbs static void
89127a9483SJustin T. Gibbs xbd_freeze(struct xbd_softc *sc, xbd_flag_t xbd_flag)
90127a9483SJustin T. Gibbs {
91127a9483SJustin T. Gibbs 	if (xbd_flag != XBDF_NONE && (sc->xbd_flags & xbd_flag) != 0)
92127a9483SJustin T. Gibbs 		return;
93127a9483SJustin T. Gibbs 
94127a9483SJustin T. Gibbs 	sc->xbd_flags |= xbd_flag;
95127a9483SJustin T. Gibbs 	sc->xbd_qfrozen_cnt++;
96127a9483SJustin T. Gibbs }
97127a9483SJustin T. Gibbs 
98127a9483SJustin T. Gibbs static void
99127a9483SJustin T. Gibbs xbd_thaw(struct xbd_softc *sc, xbd_flag_t xbd_flag)
100127a9483SJustin T. Gibbs {
101127a9483SJustin T. Gibbs 	if (xbd_flag != XBDF_NONE && (sc->xbd_flags & xbd_flag) == 0)
102127a9483SJustin T. Gibbs 		return;
103127a9483SJustin T. Gibbs 
104b834eea6SJustin T. Gibbs 	if (sc->xbd_qfrozen_cnt == 0)
105127a9483SJustin T. Gibbs 		panic("%s: Thaw with flag 0x%x while not frozen.",
106127a9483SJustin T. Gibbs 		    __func__, xbd_flag);
107127a9483SJustin T. Gibbs 
108127a9483SJustin T. Gibbs 	sc->xbd_flags &= ~xbd_flag;
109127a9483SJustin T. Gibbs 	sc->xbd_qfrozen_cnt--;
110127a9483SJustin T. Gibbs }
111127a9483SJustin T. Gibbs 
1129985113bSJustin T. Gibbs static void
1139985113bSJustin T. Gibbs xbd_cm_freeze(struct xbd_softc *sc, struct xbd_command *cm, xbdc_flag_t cm_flag)
1149985113bSJustin T. Gibbs {
1159985113bSJustin T. Gibbs 	if ((cm->cm_flags & XBDCF_FROZEN) != 0)
1169985113bSJustin T. Gibbs 		return;
1179985113bSJustin T. Gibbs 
1189985113bSJustin T. Gibbs 	cm->cm_flags |= XBDCF_FROZEN|cm_flag;
1199985113bSJustin T. Gibbs 	xbd_freeze(sc, XBDF_NONE);
1209985113bSJustin T. Gibbs }
1219985113bSJustin T. Gibbs 
1229985113bSJustin T. Gibbs static void
1239985113bSJustin T. Gibbs xbd_cm_thaw(struct xbd_softc *sc, struct xbd_command *cm)
1249985113bSJustin T. Gibbs {
1259985113bSJustin T. Gibbs 	if ((cm->cm_flags & XBDCF_FROZEN) == 0)
1269985113bSJustin T. Gibbs 		return;
1279985113bSJustin T. Gibbs 
1289985113bSJustin T. Gibbs 	cm->cm_flags &= ~XBDCF_FROZEN;
1299985113bSJustin T. Gibbs 	xbd_thaw(sc, XBDF_NONE);
1309985113bSJustin T. Gibbs }
1319985113bSJustin T. Gibbs 
132fac3fd80SJustin T. Gibbs static inline void
133cdf5d66fSJustin T. Gibbs xbd_flush_requests(struct xbd_softc *sc)
134fac3fd80SJustin T. Gibbs {
135fac3fd80SJustin T. Gibbs 	int notify;
136e4808c4bSKip Macy 
137fac3fd80SJustin T. Gibbs 	RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&sc->xbd_ring, notify);
13889e0f4d2SKip Macy 
139fac3fd80SJustin T. Gibbs 	if (notify)
14076acc41fSJustin T. Gibbs 		xen_intr_signal(sc->xen_intr_handle);
141fac3fd80SJustin T. Gibbs }
14289e0f4d2SKip Macy 
14323dc5621SKip Macy static void
144fac3fd80SJustin T. Gibbs xbd_free_command(struct xbd_command *cm)
14523dc5621SKip Macy {
14623dc5621SKip Macy 
147e2c1fe90SJustin T. Gibbs 	KASSERT((cm->cm_flags & XBDCF_Q_MASK) == XBD_Q_NONE,
148e2c1fe90SJustin T. Gibbs 	    ("Freeing command that is still on queue %d.",
149e2c1fe90SJustin T. Gibbs 	    cm->cm_flags & XBDCF_Q_MASK));
15023dc5621SKip Macy 
151e2c1fe90SJustin T. Gibbs 	cm->cm_flags = XBDCF_INITIALIZER;
152fac3fd80SJustin T. Gibbs 	cm->cm_bp = NULL;
153fac3fd80SJustin T. Gibbs 	cm->cm_complete = NULL;
154e2c1fe90SJustin T. Gibbs 	xbd_enqueue_cm(cm, XBD_Q_FREE);
155127a9483SJustin T. Gibbs 	xbd_thaw(cm->cm_sc, XBDF_CM_SHORTAGE);
15623dc5621SKip Macy }
15723dc5621SKip Macy 
15889e0f4d2SKip Macy static void
1597a70e152SColin Percival xbd_mksegarray(bus_dma_segment_t *segs, int nsegs,
160d0ecc14dSColin Percival     grant_ref_t * gref_head, int otherend_id, int readonly,
1611a52c105SRoger Pau Monné     grant_ref_t * sg_ref, struct blkif_request_segment *sg)
162d0ecc14dSColin Percival {
163d0ecc14dSColin Percival 	struct blkif_request_segment *last_block_sg = sg + nsegs;
164d0ecc14dSColin Percival 	vm_paddr_t buffer_ma;
165d0ecc14dSColin Percival 	uint64_t fsect, lsect;
166d0ecc14dSColin Percival 	int ref;
167d0ecc14dSColin Percival 
168d0ecc14dSColin Percival 	while (sg < last_block_sg) {
169cbb261aeSColin Percival 		KASSERT(segs->ds_addr % (1 << XBD_SECTOR_SHFT) == 0,
170cbb261aeSColin Percival 		    ("XEN disk driver I/O must be sector aligned"));
171cbb261aeSColin Percival 		KASSERT(segs->ds_len % (1 << XBD_SECTOR_SHFT) == 0,
172cbb261aeSColin Percival 		    ("XEN disk driver I/Os must be a multiple of "
173cbb261aeSColin Percival 		    "the sector length"));
174d0ecc14dSColin Percival 		buffer_ma = segs->ds_addr;
175d0ecc14dSColin Percival 		fsect = (buffer_ma & PAGE_MASK) >> XBD_SECTOR_SHFT;
176d0ecc14dSColin Percival 		lsect = fsect + (segs->ds_len  >> XBD_SECTOR_SHFT) - 1;
177d0ecc14dSColin Percival 
178d0ecc14dSColin Percival 		KASSERT(lsect <= 7, ("XEN disk driver data cannot "
179d0ecc14dSColin Percival 		    "cross a page boundary"));
180d0ecc14dSColin Percival 
181d0ecc14dSColin Percival 		/* install a grant reference. */
182d0ecc14dSColin Percival 		ref = gnttab_claim_grant_reference(gref_head);
183d0ecc14dSColin Percival 
184d0ecc14dSColin Percival 		/*
185d0ecc14dSColin Percival 		 * GNTTAB_LIST_END == 0xffffffff, but it is private
186d0ecc14dSColin Percival 		 * to gnttab.c.
187d0ecc14dSColin Percival 		 */
188d0ecc14dSColin Percival 		KASSERT(ref != ~0, ("grant_reference failed"));
189d0ecc14dSColin Percival 
190d0ecc14dSColin Percival 		gnttab_grant_foreign_access_ref(
191d0ecc14dSColin Percival 		    ref,
192d0ecc14dSColin Percival 		    otherend_id,
193d0ecc14dSColin Percival 		    buffer_ma >> PAGE_SHIFT,
194d0ecc14dSColin Percival 		    readonly);
195d0ecc14dSColin Percival 
196d0ecc14dSColin Percival 		*sg_ref = ref;
197d0ecc14dSColin Percival 		*sg = (struct blkif_request_segment) {
198d0ecc14dSColin Percival 			.gref       = ref,
199d0ecc14dSColin Percival 			.first_sect = fsect,
200d0ecc14dSColin Percival 			.last_sect  = lsect
201d0ecc14dSColin Percival 		};
202d0ecc14dSColin Percival 		sg++;
203d0ecc14dSColin Percival 		sg_ref++;
204d0ecc14dSColin Percival 		segs++;
205d0ecc14dSColin Percival 	}
206d0ecc14dSColin Percival }
207d0ecc14dSColin Percival 
208d0ecc14dSColin Percival static void
209fac3fd80SJustin T. Gibbs xbd_queue_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
21089e0f4d2SKip Macy {
211fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc;
212fac3fd80SJustin T. Gibbs 	struct xbd_command *cm;
213fac3fd80SJustin T. Gibbs 	int op;
21489e0f4d2SKip Macy 
215fac3fd80SJustin T. Gibbs 	cm = arg;
216fac3fd80SJustin T. Gibbs 	sc = cm->cm_sc;
217fac3fd80SJustin T. Gibbs 
218fac3fd80SJustin T. Gibbs 	if (error) {
219fac3fd80SJustin T. Gibbs 		cm->cm_bp->bio_error = EIO;
220fac3fd80SJustin T. Gibbs 		biodone(cm->cm_bp);
221fac3fd80SJustin T. Gibbs 		xbd_free_command(cm);
22289e0f4d2SKip Macy 		return;
22389e0f4d2SKip Macy 	}
22489e0f4d2SKip Macy 
225aaebf690SColin Percival 	KASSERT(nsegs <= sc->xbd_max_request_segments,
226d33a1217SColin Percival 	    ("Too many segments in a blkfront I/O"));
227d33a1217SColin Percival 
228aaebf690SColin Percival 	if (nsegs <= BLKIF_MAX_SEGMENTS_PER_REQUEST) {
229aaebf690SColin Percival 		blkif_request_t	*ring_req;
230aaebf690SColin Percival 
231aaebf690SColin Percival 		/* Fill out a blkif_request_t structure. */
232aaebf690SColin Percival 		ring_req = (blkif_request_t *)
233aaebf690SColin Percival 		    RING_GET_REQUEST(&sc->xbd_ring, sc->xbd_ring.req_prod_pvt);
234fac3fd80SJustin T. Gibbs 		sc->xbd_ring.req_prod_pvt++;
235fac3fd80SJustin T. Gibbs 		ring_req->id = cm->cm_id;
236fac3fd80SJustin T. Gibbs 		ring_req->operation = cm->cm_operation;
237fac3fd80SJustin T. Gibbs 		ring_req->sector_number = cm->cm_sector_number;
238fac3fd80SJustin T. Gibbs 		ring_req->handle = (blkif_vdev_t)(uintptr_t)sc->xbd_disk;
239fac3fd80SJustin T. Gibbs 		ring_req->nr_segments = nsegs;
240fac3fd80SJustin T. Gibbs 		cm->cm_nseg = nsegs;
2417a70e152SColin Percival 		xbd_mksegarray(segs, nsegs, &cm->cm_gref_head,
242fac3fd80SJustin T. Gibbs 		    xenbus_get_otherend_id(sc->xbd_dev),
243d0ecc14dSColin Percival 		    cm->cm_operation == BLKIF_OP_WRITE,
244d0ecc14dSColin Percival 		    cm->cm_sg_refs, ring_req->seg);
245aaebf690SColin Percival 	} else {
246aaebf690SColin Percival 		blkif_request_indirect_t *ring_req;
247aaebf690SColin Percival 
248aaebf690SColin Percival 		/* Fill out a blkif_request_indirect_t structure. */
249aaebf690SColin Percival 		ring_req = (blkif_request_indirect_t *)
250aaebf690SColin Percival 		    RING_GET_REQUEST(&sc->xbd_ring, sc->xbd_ring.req_prod_pvt);
251aaebf690SColin Percival 		sc->xbd_ring.req_prod_pvt++;
252aaebf690SColin Percival 		ring_req->id = cm->cm_id;
253aaebf690SColin Percival 		ring_req->operation = BLKIF_OP_INDIRECT;
254aaebf690SColin Percival 		ring_req->indirect_op = cm->cm_operation;
255aaebf690SColin Percival 		ring_req->sector_number = cm->cm_sector_number;
256aaebf690SColin Percival 		ring_req->handle = (blkif_vdev_t)(uintptr_t)sc->xbd_disk;
257aaebf690SColin Percival 		ring_req->nr_segments = nsegs;
258aaebf690SColin Percival 		cm->cm_nseg = nsegs;
259aaebf690SColin Percival 		xbd_mksegarray(segs, nsegs, &cm->cm_gref_head,
260aaebf690SColin Percival 		    xenbus_get_otherend_id(sc->xbd_dev),
261aaebf690SColin Percival 		    cm->cm_operation == BLKIF_OP_WRITE,
262aaebf690SColin Percival 		    cm->cm_sg_refs, cm->cm_indirectionpages);
263aaebf690SColin Percival 		memcpy(ring_req->indirect_grefs, &cm->cm_indirectionrefs,
264aaebf690SColin Percival 		    sizeof(grant_ref_t) * sc->xbd_max_request_indirectpages);
265aaebf690SColin Percival 	}
266fac3fd80SJustin T. Gibbs 
267fac3fd80SJustin T. Gibbs 	if (cm->cm_operation == BLKIF_OP_READ)
268fac3fd80SJustin T. Gibbs 		op = BUS_DMASYNC_PREREAD;
269fac3fd80SJustin T. Gibbs 	else if (cm->cm_operation == BLKIF_OP_WRITE)
270fac3fd80SJustin T. Gibbs 		op = BUS_DMASYNC_PREWRITE;
271fac3fd80SJustin T. Gibbs 	else
272fac3fd80SJustin T. Gibbs 		op = 0;
273fac3fd80SJustin T. Gibbs 	bus_dmamap_sync(sc->xbd_io_dmat, cm->cm_map, op);
274fac3fd80SJustin T. Gibbs 
275fac3fd80SJustin T. Gibbs 	gnttab_free_grant_references(cm->cm_gref_head);
276fac3fd80SJustin T. Gibbs 
277e2c1fe90SJustin T. Gibbs 	xbd_enqueue_cm(cm, XBD_Q_BUSY);
278fac3fd80SJustin T. Gibbs 
279fac3fd80SJustin T. Gibbs 	/*
280127a9483SJustin T. Gibbs 	 * If bus dma had to asynchronously call us back to dispatch
281127a9483SJustin T. Gibbs 	 * this command, we are no longer executing in the context of
282127a9483SJustin T. Gibbs 	 * xbd_startio().  Thus we cannot rely on xbd_startio()'s call to
283127a9483SJustin T. Gibbs 	 * xbd_flush_requests() to publish this command to the backend
284127a9483SJustin T. Gibbs 	 * along with any other commands that it could batch.
285fac3fd80SJustin T. Gibbs 	 */
286127a9483SJustin T. Gibbs 	if ((cm->cm_flags & XBDCF_ASYNC_MAPPING) != 0)
287cdf5d66fSJustin T. Gibbs 		xbd_flush_requests(sc);
288fac3fd80SJustin T. Gibbs 
289fac3fd80SJustin T. Gibbs 	return;
290fac3fd80SJustin T. Gibbs }
291fac3fd80SJustin T. Gibbs 
292fac3fd80SJustin T. Gibbs static int
293fac3fd80SJustin T. Gibbs xbd_queue_request(struct xbd_softc *sc, struct xbd_command *cm)
294fac3fd80SJustin T. Gibbs {
295fac3fd80SJustin T. Gibbs 	int error;
296fac3fd80SJustin T. Gibbs 
297a55a04a8SRoger Pau Monné 	if (cm->cm_bp != NULL)
298a55a04a8SRoger Pau Monné 		error = bus_dmamap_load_bio(sc->xbd_io_dmat, cm->cm_map,
299a55a04a8SRoger Pau Monné 		    cm->cm_bp, xbd_queue_cb, cm, 0);
300a55a04a8SRoger Pau Monné 	else
301a55a04a8SRoger Pau Monné 		error = bus_dmamap_load(sc->xbd_io_dmat, cm->cm_map,
302a55a04a8SRoger Pau Monné 		    cm->cm_data, cm->cm_datalen, xbd_queue_cb, cm, 0);
303fac3fd80SJustin T. Gibbs 	if (error == EINPROGRESS) {
304127a9483SJustin T. Gibbs 		/*
305127a9483SJustin T. Gibbs 		 * Maintain queuing order by freezing the queue.  The next
306127a9483SJustin T. Gibbs 		 * command may not require as many resources as the command
307127a9483SJustin T. Gibbs 		 * we just attempted to map, so we can't rely on bus dma
308127a9483SJustin T. Gibbs 		 * blocking for it too.
309127a9483SJustin T. Gibbs 		 */
3109985113bSJustin T. Gibbs 		xbd_cm_freeze(sc, cm, XBDCF_ASYNC_MAPPING);
311fac3fd80SJustin T. Gibbs 		return (0);
312fac3fd80SJustin T. Gibbs 	}
313fac3fd80SJustin T. Gibbs 
314fac3fd80SJustin T. Gibbs 	return (error);
315fac3fd80SJustin T. Gibbs }
316fac3fd80SJustin T. Gibbs 
317fac3fd80SJustin T. Gibbs static void
318fac3fd80SJustin T. Gibbs xbd_restart_queue_callback(void *arg)
319fac3fd80SJustin T. Gibbs {
320fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = arg;
321fac3fd80SJustin T. Gibbs 
32233eebb6aSJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
323e4808c4bSKip Macy 
324127a9483SJustin T. Gibbs 	xbd_thaw(sc, XBDF_GNT_SHORTAGE);
325127a9483SJustin T. Gibbs 
32633eebb6aSJustin T. Gibbs 	xbd_startio(sc);
327e4808c4bSKip Macy 
32833eebb6aSJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
329fac3fd80SJustin T. Gibbs }
330fac3fd80SJustin T. Gibbs 
331fac3fd80SJustin T. Gibbs static struct xbd_command *
332fac3fd80SJustin T. Gibbs xbd_bio_command(struct xbd_softc *sc)
333fac3fd80SJustin T. Gibbs {
334fac3fd80SJustin T. Gibbs 	struct xbd_command *cm;
335fac3fd80SJustin T. Gibbs 	struct bio *bp;
336fac3fd80SJustin T. Gibbs 
33776acc41fSJustin T. Gibbs 	if (__predict_false(sc->xbd_state != XBD_STATE_CONNECTED))
338fac3fd80SJustin T. Gibbs 		return (NULL);
339fac3fd80SJustin T. Gibbs 
340fac3fd80SJustin T. Gibbs 	bp = xbd_dequeue_bio(sc);
341fac3fd80SJustin T. Gibbs 	if (bp == NULL)
342fac3fd80SJustin T. Gibbs 		return (NULL);
343fac3fd80SJustin T. Gibbs 
344e2c1fe90SJustin T. Gibbs 	if ((cm = xbd_dequeue_cm(sc, XBD_Q_FREE)) == NULL) {
345127a9483SJustin T. Gibbs 		xbd_freeze(sc, XBDF_CM_SHORTAGE);
346fac3fd80SJustin T. Gibbs 		xbd_requeue_bio(sc, bp);
347fac3fd80SJustin T. Gibbs 		return (NULL);
348fac3fd80SJustin T. Gibbs 	}
349fac3fd80SJustin T. Gibbs 
350fac3fd80SJustin T. Gibbs 	if (gnttab_alloc_grant_references(sc->xbd_max_request_segments,
351fac3fd80SJustin T. Gibbs 	    &cm->cm_gref_head) != 0) {
352fac3fd80SJustin T. Gibbs 		gnttab_request_free_callback(&sc->xbd_callback,
353fac3fd80SJustin T. Gibbs 		    xbd_restart_queue_callback, sc,
354fac3fd80SJustin T. Gibbs 		    sc->xbd_max_request_segments);
355127a9483SJustin T. Gibbs 		xbd_freeze(sc, XBDF_GNT_SHORTAGE);
356fac3fd80SJustin T. Gibbs 		xbd_requeue_bio(sc, bp);
357e2c1fe90SJustin T. Gibbs 		xbd_enqueue_cm(cm, XBD_Q_FREE);
358fac3fd80SJustin T. Gibbs 		return (NULL);
359fac3fd80SJustin T. Gibbs 	}
360fac3fd80SJustin T. Gibbs 
361fac3fd80SJustin T. Gibbs 	cm->cm_bp = bp;
362fac3fd80SJustin T. Gibbs 	cm->cm_sector_number = (blkif_sector_t)bp->bio_pblkno;
363fac3fd80SJustin T. Gibbs 
3649985113bSJustin T. Gibbs 	switch (bp->bio_cmd) {
3659985113bSJustin T. Gibbs 	case BIO_READ:
3669985113bSJustin T. Gibbs 		cm->cm_operation = BLKIF_OP_READ;
3679985113bSJustin T. Gibbs 		break;
3689985113bSJustin T. Gibbs 	case BIO_WRITE:
3699985113bSJustin T. Gibbs 		cm->cm_operation = BLKIF_OP_WRITE;
3709985113bSJustin T. Gibbs 		if ((bp->bio_flags & BIO_ORDERED) != 0) {
3719985113bSJustin T. Gibbs 			if ((sc->xbd_flags & XBDF_BARRIER) != 0) {
3729985113bSJustin T. Gibbs 				cm->cm_operation = BLKIF_OP_WRITE_BARRIER;
3739985113bSJustin T. Gibbs 			} else {
3749985113bSJustin T. Gibbs 				/*
3759985113bSJustin T. Gibbs 				 * Single step this command.
3769985113bSJustin T. Gibbs 				 */
3779985113bSJustin T. Gibbs 				cm->cm_flags |= XBDCF_Q_FREEZE;
3789985113bSJustin T. Gibbs 				if (xbd_queue_length(sc, XBD_Q_BUSY) != 0) {
3799985113bSJustin T. Gibbs 					/*
3809985113bSJustin T. Gibbs 					 * Wait for in-flight requests to
3819985113bSJustin T. Gibbs 					 * finish.
3829985113bSJustin T. Gibbs 					 */
3839985113bSJustin T. Gibbs 					xbd_freeze(sc, XBDF_WAIT_IDLE);
3849985113bSJustin T. Gibbs 					xbd_requeue_cm(cm, XBD_Q_READY);
3859985113bSJustin T. Gibbs 					return (NULL);
3869985113bSJustin T. Gibbs 				}
3879985113bSJustin T. Gibbs 			}
3889985113bSJustin T. Gibbs 		}
3899985113bSJustin T. Gibbs 		break;
3909985113bSJustin T. Gibbs 	case BIO_FLUSH:
3919985113bSJustin T. Gibbs 		if ((sc->xbd_flags & XBDF_FLUSH) != 0)
3929985113bSJustin T. Gibbs 			cm->cm_operation = BLKIF_OP_FLUSH_DISKCACHE;
3939985113bSJustin T. Gibbs 		else if ((sc->xbd_flags & XBDF_BARRIER) != 0)
3949985113bSJustin T. Gibbs 			cm->cm_operation = BLKIF_OP_WRITE_BARRIER;
3959985113bSJustin T. Gibbs 		else
3969985113bSJustin T. Gibbs 			panic("flush request, but no flush support available");
3979985113bSJustin T. Gibbs 		break;
3989985113bSJustin T. Gibbs 	default:
399d176b803SScott Long 		biofinish(bp, NULL, EOPNOTSUPP);
400d176b803SScott Long 		xbd_enqueue_cm(cm, XBD_Q_FREE);
401d176b803SScott Long 		return (NULL);
4029985113bSJustin T. Gibbs 	}
4039985113bSJustin T. Gibbs 
404fac3fd80SJustin T. Gibbs 	return (cm);
405fac3fd80SJustin T. Gibbs }
406fac3fd80SJustin T. Gibbs 
407fac3fd80SJustin T. Gibbs /*
408fac3fd80SJustin T. Gibbs  * Dequeue buffers and place them in the shared communication ring.
409fac3fd80SJustin T. Gibbs  * Return when no more requests can be accepted or all buffers have
410fac3fd80SJustin T. Gibbs  * been queued.
411fac3fd80SJustin T. Gibbs  *
412fac3fd80SJustin T. Gibbs  * Signal XEN once the ring has been filled out.
413fac3fd80SJustin T. Gibbs  */
414fac3fd80SJustin T. Gibbs static void
415fac3fd80SJustin T. Gibbs xbd_startio(struct xbd_softc *sc)
416fac3fd80SJustin T. Gibbs {
417fac3fd80SJustin T. Gibbs 	struct xbd_command *cm;
418fac3fd80SJustin T. Gibbs 	int error, queued = 0;
419fac3fd80SJustin T. Gibbs 
420fac3fd80SJustin T. Gibbs 	mtx_assert(&sc->xbd_io_lock, MA_OWNED);
421fac3fd80SJustin T. Gibbs 
422e2c1fe90SJustin T. Gibbs 	if (sc->xbd_state != XBD_STATE_CONNECTED)
423e4808c4bSKip Macy 		return;
424fac3fd80SJustin T. Gibbs 
425112cacaeSRoger Pau Monné 	while (!RING_FULL(&sc->xbd_ring)) {
426127a9483SJustin T. Gibbs 		if (sc->xbd_qfrozen_cnt != 0)
427fac3fd80SJustin T. Gibbs 			break;
428fac3fd80SJustin T. Gibbs 
429e2c1fe90SJustin T. Gibbs 		cm = xbd_dequeue_cm(sc, XBD_Q_READY);
430fac3fd80SJustin T. Gibbs 
431fac3fd80SJustin T. Gibbs 		if (cm == NULL)
432fac3fd80SJustin T. Gibbs 		    cm = xbd_bio_command(sc);
433fac3fd80SJustin T. Gibbs 
434fac3fd80SJustin T. Gibbs 		if (cm == NULL)
435fac3fd80SJustin T. Gibbs 			break;
436fac3fd80SJustin T. Gibbs 
4379985113bSJustin T. Gibbs 		if ((cm->cm_flags & XBDCF_Q_FREEZE) != 0) {
4389985113bSJustin T. Gibbs 			/*
4399985113bSJustin T. Gibbs 			 * Single step command.  Future work is
4409985113bSJustin T. Gibbs 			 * held off until this command completes.
4419985113bSJustin T. Gibbs 			 */
4429985113bSJustin T. Gibbs 			xbd_cm_freeze(sc, cm, XBDCF_Q_FREEZE);
4439985113bSJustin T. Gibbs 		}
4449985113bSJustin T. Gibbs 
445fac3fd80SJustin T. Gibbs 		if ((error = xbd_queue_request(sc, cm)) != 0) {
446fac3fd80SJustin T. Gibbs 			printf("xbd_queue_request returned %d\n", error);
447fac3fd80SJustin T. Gibbs 			break;
448fac3fd80SJustin T. Gibbs 		}
449fac3fd80SJustin T. Gibbs 		queued++;
450fac3fd80SJustin T. Gibbs 	}
451fac3fd80SJustin T. Gibbs 
452fac3fd80SJustin T. Gibbs 	if (queued != 0)
453cdf5d66fSJustin T. Gibbs 		xbd_flush_requests(sc);
454e4808c4bSKip Macy }
455e4808c4bSKip Macy 
456e4808c4bSKip Macy static void
45733eebb6aSJustin T. Gibbs xbd_bio_complete(struct xbd_softc *sc, struct xbd_command *cm)
458e4808c4bSKip Macy {
459e4808c4bSKip Macy 	struct bio *bp;
460e4808c4bSKip Macy 
46133eebb6aSJustin T. Gibbs 	bp = cm->cm_bp;
462e4808c4bSKip Macy 
46376acc41fSJustin T. Gibbs 	if (__predict_false(cm->cm_status != BLKIF_RSP_OKAY)) {
464e4808c4bSKip Macy 		disk_err(bp, "disk error" , -1, 0);
46533eebb6aSJustin T. Gibbs 		printf(" status: %x\n", cm->cm_status);
466e4808c4bSKip Macy 		bp->bio_flags |= BIO_ERROR;
467e4808c4bSKip Macy 	}
468e4808c4bSKip Macy 
469e4808c4bSKip Macy 	if (bp->bio_flags & BIO_ERROR)
470e4808c4bSKip Macy 		bp->bio_error = EIO;
471e4808c4bSKip Macy 	else
472e4808c4bSKip Macy 		bp->bio_resid = 0;
473e4808c4bSKip Macy 
47433eebb6aSJustin T. Gibbs 	xbd_free_command(cm);
475e4808c4bSKip Macy 	biodone(bp);
476e4808c4bSKip Macy }
477e4808c4bSKip Macy 
478fac3fd80SJustin T. Gibbs static void
479fac3fd80SJustin T. Gibbs xbd_int(void *xsc)
480fac3fd80SJustin T. Gibbs {
481fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = xsc;
482fac3fd80SJustin T. Gibbs 	struct xbd_command *cm;
483fac3fd80SJustin T. Gibbs 	blkif_response_t *bret;
484fac3fd80SJustin T. Gibbs 	RING_IDX i, rp;
485fac3fd80SJustin T. Gibbs 	int op;
486fac3fd80SJustin T. Gibbs 
487fac3fd80SJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
488fac3fd80SJustin T. Gibbs 
48976acc41fSJustin T. Gibbs 	if (__predict_false(sc->xbd_state == XBD_STATE_DISCONNECTED)) {
490fac3fd80SJustin T. Gibbs 		mtx_unlock(&sc->xbd_io_lock);
491fac3fd80SJustin T. Gibbs 		return;
492fac3fd80SJustin T. Gibbs 	}
493fac3fd80SJustin T. Gibbs 
494fac3fd80SJustin T. Gibbs  again:
495fac3fd80SJustin T. Gibbs 	rp = sc->xbd_ring.sring->rsp_prod;
496fac3fd80SJustin T. Gibbs 	rmb(); /* Ensure we see queued responses up to 'rp'. */
497fac3fd80SJustin T. Gibbs 
498fac3fd80SJustin T. Gibbs 	for (i = sc->xbd_ring.rsp_cons; i != rp;) {
499fac3fd80SJustin T. Gibbs 		bret = RING_GET_RESPONSE(&sc->xbd_ring, i);
500fac3fd80SJustin T. Gibbs 		cm   = &sc->xbd_shadow[bret->id];
501fac3fd80SJustin T. Gibbs 
502e2c1fe90SJustin T. Gibbs 		xbd_remove_cm(cm, XBD_Q_BUSY);
503112cacaeSRoger Pau Monné 		gnttab_end_foreign_access_references(cm->cm_nseg,
504112cacaeSRoger Pau Monné 		    cm->cm_sg_refs);
505112cacaeSRoger Pau Monné 		i++;
506fac3fd80SJustin T. Gibbs 
507fac3fd80SJustin T. Gibbs 		if (cm->cm_operation == BLKIF_OP_READ)
508fac3fd80SJustin T. Gibbs 			op = BUS_DMASYNC_POSTREAD;
5099985113bSJustin T. Gibbs 		else if (cm->cm_operation == BLKIF_OP_WRITE ||
5109985113bSJustin T. Gibbs 		    cm->cm_operation == BLKIF_OP_WRITE_BARRIER)
511fac3fd80SJustin T. Gibbs 			op = BUS_DMASYNC_POSTWRITE;
512fac3fd80SJustin T. Gibbs 		else
513fac3fd80SJustin T. Gibbs 			op = 0;
514fac3fd80SJustin T. Gibbs 		bus_dmamap_sync(sc->xbd_io_dmat, cm->cm_map, op);
515fac3fd80SJustin T. Gibbs 		bus_dmamap_unload(sc->xbd_io_dmat, cm->cm_map);
516fac3fd80SJustin T. Gibbs 
517fac3fd80SJustin T. Gibbs 		/*
518127a9483SJustin T. Gibbs 		 * Release any hold this command has on future command
519127a9483SJustin T. Gibbs 		 * dispatch.
520fac3fd80SJustin T. Gibbs 		 */
5219985113bSJustin T. Gibbs 		xbd_cm_thaw(sc, cm);
522fac3fd80SJustin T. Gibbs 
523fac3fd80SJustin T. Gibbs 		/*
524fac3fd80SJustin T. Gibbs 		 * Directly call the i/o complete routine to save an
525fac3fd80SJustin T. Gibbs 		 * an indirection in the common case.
526fac3fd80SJustin T. Gibbs 		 */
527fac3fd80SJustin T. Gibbs 		cm->cm_status = bret->status;
528fac3fd80SJustin T. Gibbs 		if (cm->cm_bp)
529fac3fd80SJustin T. Gibbs 			xbd_bio_complete(sc, cm);
530fac3fd80SJustin T. Gibbs 		else if (cm->cm_complete != NULL)
531fac3fd80SJustin T. Gibbs 			cm->cm_complete(cm);
532fac3fd80SJustin T. Gibbs 		else
533fac3fd80SJustin T. Gibbs 			xbd_free_command(cm);
534fac3fd80SJustin T. Gibbs 	}
535fac3fd80SJustin T. Gibbs 
536fac3fd80SJustin T. Gibbs 	sc->xbd_ring.rsp_cons = i;
537fac3fd80SJustin T. Gibbs 
538fac3fd80SJustin T. Gibbs 	if (i != sc->xbd_ring.req_prod_pvt) {
539fac3fd80SJustin T. Gibbs 		int more_to_do;
540fac3fd80SJustin T. Gibbs 		RING_FINAL_CHECK_FOR_RESPONSES(&sc->xbd_ring, more_to_do);
541fac3fd80SJustin T. Gibbs 		if (more_to_do)
542fac3fd80SJustin T. Gibbs 			goto again;
543fac3fd80SJustin T. Gibbs 	} else {
544fac3fd80SJustin T. Gibbs 		sc->xbd_ring.sring->rsp_event = i + 1;
545fac3fd80SJustin T. Gibbs 	}
546fac3fd80SJustin T. Gibbs 
5479985113bSJustin T. Gibbs 	if (xbd_queue_length(sc, XBD_Q_BUSY) == 0)
5489985113bSJustin T. Gibbs 		xbd_thaw(sc, XBDF_WAIT_IDLE);
5499985113bSJustin T. Gibbs 
550fac3fd80SJustin T. Gibbs 	xbd_startio(sc);
551fac3fd80SJustin T. Gibbs 
55276acc41fSJustin T. Gibbs 	if (__predict_false(sc->xbd_state == XBD_STATE_SUSPENDED))
553e2c1fe90SJustin T. Gibbs 		wakeup(&sc->xbd_cm_q[XBD_Q_BUSY]);
554fac3fd80SJustin T. Gibbs 
555fac3fd80SJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
556fac3fd80SJustin T. Gibbs }
557fac3fd80SJustin T. Gibbs 
558fac3fd80SJustin T. Gibbs /*------------------------------- Dump Support -------------------------------*/
559fac3fd80SJustin T. Gibbs /**
560fac3fd80SJustin T. Gibbs  * Quiesce the disk writes for a dump file before allowing the next buffer.
561fac3fd80SJustin T. Gibbs  */
5628698b76cSKip Macy static void
56333eebb6aSJustin T. Gibbs xbd_quiesce(struct xbd_softc *sc)
5648698b76cSKip Macy {
5658698b76cSKip Macy 	int mtd;
5668698b76cSKip Macy 
5678698b76cSKip Macy 	// While there are outstanding requests
5689985113bSJustin T. Gibbs 	while (xbd_queue_length(sc, XBD_Q_BUSY) != 0) {
56933eebb6aSJustin T. Gibbs 		RING_FINAL_CHECK_FOR_RESPONSES(&sc->xbd_ring, mtd);
5708698b76cSKip Macy 		if (mtd) {
571453130d9SPedro F. Giffuni 			/* Received request completions, update queue. */
57233eebb6aSJustin T. Gibbs 			xbd_int(sc);
5738698b76cSKip Macy 		}
5749985113bSJustin T. Gibbs 		if (xbd_queue_length(sc, XBD_Q_BUSY) != 0) {
575e4808c4bSKip Macy 			/*
576e4808c4bSKip Macy 			 * Still pending requests, wait for the disk i/o
577e4808c4bSKip Macy 			 * to complete.
578e4808c4bSKip Macy 			 */
579be7747b4SKip Macy 			HYPERVISOR_yield();
5808698b76cSKip Macy 		}
5818698b76cSKip Macy 	}
5828698b76cSKip Macy }
5838698b76cSKip Macy 
584e4808c4bSKip Macy /* Kernel dump function for a paravirtualized disk device */
585e4808c4bSKip Macy static void
58633eebb6aSJustin T. Gibbs xbd_dump_complete(struct xbd_command *cm)
587e4808c4bSKip Macy {
5888698b76cSKip Macy 
589e2c1fe90SJustin T. Gibbs 	xbd_enqueue_cm(cm, XBD_Q_COMPLETE);
590e4808c4bSKip Macy }
591e4808c4bSKip Macy 
5928698b76cSKip Macy static int
593489ba222SMitchell Horne xbd_dump(void *arg, void *virtual, off_t offset, size_t length)
5948698b76cSKip Macy {
5958698b76cSKip Macy 	struct disk *dp = arg;
59633eebb6aSJustin T. Gibbs 	struct xbd_softc *sc = dp->d_drv1;
59733eebb6aSJustin T. Gibbs 	struct xbd_command *cm;
598e4808c4bSKip Macy 	size_t chunk;
5998698b76cSKip Macy 	int rc = 0;
6008698b76cSKip Macy 
6018b19549bSRoger Pau Monné 	if (length == 0)
6028b19549bSRoger Pau Monné 		return (0);
6038698b76cSKip Macy 
60433eebb6aSJustin T. Gibbs 	xbd_quiesce(sc);	/* All quiet on the western front. */
605e4808c4bSKip Macy 
606e4808c4bSKip Macy 	/*
607e4808c4bSKip Macy 	 * If this lock is held, then this module is failing, and a
608e4808c4bSKip Macy 	 * successful kernel dump is highly unlikely anyway.
609e4808c4bSKip Macy 	 */
61033eebb6aSJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
611e4808c4bSKip Macy 
612e4808c4bSKip Macy 	/* Split the 64KB block as needed */
613e635220eSDimitry Andric 	while (length > 0) {
614e2c1fe90SJustin T. Gibbs 		cm = xbd_dequeue_cm(sc, XBD_Q_FREE);
615e4808c4bSKip Macy 		if (cm == NULL) {
61633eebb6aSJustin T. Gibbs 			mtx_unlock(&sc->xbd_io_lock);
61733eebb6aSJustin T. Gibbs 			device_printf(sc->xbd_dev, "dump: no more commands?\n");
618e4808c4bSKip Macy 			return (EBUSY);
619e4808c4bSKip Macy 		}
620e4808c4bSKip Macy 
62133eebb6aSJustin T. Gibbs 		if (gnttab_alloc_grant_references(sc->xbd_max_request_segments,
62233eebb6aSJustin T. Gibbs 		    &cm->cm_gref_head) != 0) {
62333eebb6aSJustin T. Gibbs 			xbd_free_command(cm);
62433eebb6aSJustin T. Gibbs 			mtx_unlock(&sc->xbd_io_lock);
62533eebb6aSJustin T. Gibbs 			device_printf(sc->xbd_dev, "no more grant allocs?\n");
626e4808c4bSKip Macy 			return (EBUSY);
627e4808c4bSKip Macy 		}
628e4808c4bSKip Macy 
62933eebb6aSJustin T. Gibbs 		chunk = length > sc->xbd_max_request_size ?
63033eebb6aSJustin T. Gibbs 		    sc->xbd_max_request_size : length;
63133eebb6aSJustin T. Gibbs 		cm->cm_data = virtual;
63233eebb6aSJustin T. Gibbs 		cm->cm_datalen = chunk;
63333eebb6aSJustin T. Gibbs 		cm->cm_operation = BLKIF_OP_WRITE;
63433eebb6aSJustin T. Gibbs 		cm->cm_sector_number = offset / dp->d_sectorsize;
63533eebb6aSJustin T. Gibbs 		cm->cm_complete = xbd_dump_complete;
636e4808c4bSKip Macy 
637e2c1fe90SJustin T. Gibbs 		xbd_enqueue_cm(cm, XBD_Q_READY);
6388698b76cSKip Macy 
6398698b76cSKip Macy 		length -= chunk;
6408698b76cSKip Macy 		offset += chunk;
6418698b76cSKip Macy 		virtual = (char *) virtual + chunk;
6428698b76cSKip Macy 	}
6438698b76cSKip Macy 
644e4808c4bSKip Macy 	/* Tell DOM0 to do the I/O */
64533eebb6aSJustin T. Gibbs 	xbd_startio(sc);
64633eebb6aSJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
647e4808c4bSKip Macy 
648e4808c4bSKip Macy 	/* Poll for the completion. */
64933eebb6aSJustin T. Gibbs 	xbd_quiesce(sc);	/* All quite on the eastern front */
650e4808c4bSKip Macy 
651e4808c4bSKip Macy 	/* If there were any errors, bail out... */
652e2c1fe90SJustin T. Gibbs 	while ((cm = xbd_dequeue_cm(sc, XBD_Q_COMPLETE)) != NULL) {
65333eebb6aSJustin T. Gibbs 		if (cm->cm_status != BLKIF_RSP_OKAY) {
65433eebb6aSJustin T. Gibbs 			device_printf(sc->xbd_dev,
655e4808c4bSKip Macy 			    "Dump I/O failed at sector %jd\n",
65633eebb6aSJustin T. Gibbs 			    cm->cm_sector_number);
657e4808c4bSKip Macy 			rc = EIO;
6588698b76cSKip Macy 		}
65933eebb6aSJustin T. Gibbs 		xbd_free_command(cm);
6608698b76cSKip Macy 	}
661e4808c4bSKip Macy 
6628698b76cSKip Macy 	return (rc);
6638698b76cSKip Macy }
6648698b76cSKip Macy 
665fac3fd80SJustin T. Gibbs /*----------------------------- Disk Entrypoints -----------------------------*/
66623dc5621SKip Macy static int
667fac3fd80SJustin T. Gibbs xbd_open(struct disk *dp)
66889e0f4d2SKip Macy {
669fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = dp->d_drv1;
67023dc5621SKip Macy 
671fac3fd80SJustin T. Gibbs 	if (sc == NULL) {
672bcccdfa3SColin Percival 		printf("xbd%d: not found", dp->d_unit);
67323dc5621SKip Macy 		return (ENXIO);
67423dc5621SKip Macy 	}
67523dc5621SKip Macy 
676e2c1fe90SJustin T. Gibbs 	sc->xbd_flags |= XBDF_OPEN;
677fac3fd80SJustin T. Gibbs 	sc->xbd_users++;
678fac3fd80SJustin T. Gibbs 	return (0);
679fac3fd80SJustin T. Gibbs }
680fac3fd80SJustin T. Gibbs 
681fac3fd80SJustin T. Gibbs static int
682fac3fd80SJustin T. Gibbs xbd_close(struct disk *dp)
683fac3fd80SJustin T. Gibbs {
684fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = dp->d_drv1;
685fac3fd80SJustin T. Gibbs 
686fac3fd80SJustin T. Gibbs 	if (sc == NULL)
687fac3fd80SJustin T. Gibbs 		return (ENXIO);
688e2c1fe90SJustin T. Gibbs 	sc->xbd_flags &= ~XBDF_OPEN;
689fac3fd80SJustin T. Gibbs 	if (--(sc->xbd_users) == 0) {
690fac3fd80SJustin T. Gibbs 		/*
691fac3fd80SJustin T. Gibbs 		 * Check whether we have been instructed to close.  We will
692fac3fd80SJustin T. Gibbs 		 * have ignored this request initially, as the device was
693fac3fd80SJustin T. Gibbs 		 * still mounted.
694fac3fd80SJustin T. Gibbs 		 */
695fac3fd80SJustin T. Gibbs 		if (xenbus_get_otherend_state(sc->xbd_dev) ==
696fac3fd80SJustin T. Gibbs 		    XenbusStateClosing)
697fac3fd80SJustin T. Gibbs 			xbd_closing(sc->xbd_dev);
698fac3fd80SJustin T. Gibbs 	}
699fac3fd80SJustin T. Gibbs 	return (0);
700fac3fd80SJustin T. Gibbs }
701fac3fd80SJustin T. Gibbs 
702fac3fd80SJustin T. Gibbs static int
703fac3fd80SJustin T. Gibbs xbd_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
704fac3fd80SJustin T. Gibbs {
705fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = dp->d_drv1;
706fac3fd80SJustin T. Gibbs 
707fac3fd80SJustin T. Gibbs 	if (sc == NULL)
708fac3fd80SJustin T. Gibbs 		return (ENXIO);
709fac3fd80SJustin T. Gibbs 
710fac3fd80SJustin T. Gibbs 	return (ENOTTY);
711fac3fd80SJustin T. Gibbs }
712fac3fd80SJustin T. Gibbs 
713fac3fd80SJustin T. Gibbs /*
714fac3fd80SJustin T. Gibbs  * Read/write routine for a buffer.  Finds the proper unit, place it on
715fac3fd80SJustin T. Gibbs  * the sortq and kick the controller.
716fac3fd80SJustin T. Gibbs  */
717fac3fd80SJustin T. Gibbs static void
718fac3fd80SJustin T. Gibbs xbd_strategy(struct bio *bp)
719fac3fd80SJustin T. Gibbs {
720fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = bp->bio_disk->d_drv1;
721fac3fd80SJustin T. Gibbs 
722fac3fd80SJustin T. Gibbs 	/* bogus disk? */
723fac3fd80SJustin T. Gibbs 	if (sc == NULL) {
724fac3fd80SJustin T. Gibbs 		bp->bio_error = EINVAL;
725fac3fd80SJustin T. Gibbs 		bp->bio_flags |= BIO_ERROR;
726fac3fd80SJustin T. Gibbs 		bp->bio_resid = bp->bio_bcount;
727fac3fd80SJustin T. Gibbs 		biodone(bp);
728fac3fd80SJustin T. Gibbs 		return;
729fac3fd80SJustin T. Gibbs 	}
730fac3fd80SJustin T. Gibbs 
731fac3fd80SJustin T. Gibbs 	/*
732fac3fd80SJustin T. Gibbs 	 * Place it in the queue of disk activities for this disk
733fac3fd80SJustin T. Gibbs 	 */
734fac3fd80SJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
735fac3fd80SJustin T. Gibbs 
736fac3fd80SJustin T. Gibbs 	xbd_enqueue_bio(sc, bp);
737fac3fd80SJustin T. Gibbs 	xbd_startio(sc);
738fac3fd80SJustin T. Gibbs 
739fac3fd80SJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
740fac3fd80SJustin T. Gibbs 	return;
741fac3fd80SJustin T. Gibbs }
742fac3fd80SJustin T. Gibbs 
743fac3fd80SJustin T. Gibbs /*------------------------------ Ring Management -----------------------------*/
744fac3fd80SJustin T. Gibbs static int
745cdf5d66fSJustin T. Gibbs xbd_alloc_ring(struct xbd_softc *sc)
746fac3fd80SJustin T. Gibbs {
747fac3fd80SJustin T. Gibbs 	blkif_sring_t *sring;
748fac3fd80SJustin T. Gibbs 	uintptr_t sring_page_addr;
749fac3fd80SJustin T. Gibbs 	int error;
750fac3fd80SJustin T. Gibbs 	int i;
751fac3fd80SJustin T. Gibbs 
752fac3fd80SJustin T. Gibbs 	sring = malloc(sc->xbd_ring_pages * PAGE_SIZE, M_XENBLOCKFRONT,
753fac3fd80SJustin T. Gibbs 	    M_NOWAIT|M_ZERO);
754fac3fd80SJustin T. Gibbs 	if (sring == NULL) {
755fac3fd80SJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, ENOMEM, "allocating shared ring");
756fac3fd80SJustin T. Gibbs 		return (ENOMEM);
757fac3fd80SJustin T. Gibbs 	}
758fac3fd80SJustin T. Gibbs 	SHARED_RING_INIT(sring);
759fac3fd80SJustin T. Gibbs 	FRONT_RING_INIT(&sc->xbd_ring, sring, sc->xbd_ring_pages * PAGE_SIZE);
760fac3fd80SJustin T. Gibbs 
761fac3fd80SJustin T. Gibbs 	for (i = 0, sring_page_addr = (uintptr_t)sring;
762fac3fd80SJustin T. Gibbs 	     i < sc->xbd_ring_pages;
763fac3fd80SJustin T. Gibbs 	     i++, sring_page_addr += PAGE_SIZE) {
764fac3fd80SJustin T. Gibbs 		error = xenbus_grant_ring(sc->xbd_dev,
7653c790178SJohn Baldwin 		    (vtophys(sring_page_addr) >> PAGE_SHIFT),
766fac3fd80SJustin T. Gibbs 		    &sc->xbd_ring_ref[i]);
767fac3fd80SJustin T. Gibbs 		if (error) {
768fac3fd80SJustin T. Gibbs 			xenbus_dev_fatal(sc->xbd_dev, error,
769fac3fd80SJustin T. Gibbs 			    "granting ring_ref(%d)", i);
770fac3fd80SJustin T. Gibbs 			return (error);
771fac3fd80SJustin T. Gibbs 		}
772fac3fd80SJustin T. Gibbs 	}
773fac3fd80SJustin T. Gibbs 	if (sc->xbd_ring_pages == 1) {
774fac3fd80SJustin T. Gibbs 		error = xs_printf(XST_NIL, xenbus_get_node(sc->xbd_dev),
775fac3fd80SJustin T. Gibbs 		    "ring-ref", "%u", sc->xbd_ring_ref[0]);
776fac3fd80SJustin T. Gibbs 		if (error) {
777fac3fd80SJustin T. Gibbs 			xenbus_dev_fatal(sc->xbd_dev, error,
778fac3fd80SJustin T. Gibbs 			    "writing %s/ring-ref",
779fac3fd80SJustin T. Gibbs 			    xenbus_get_node(sc->xbd_dev));
780fac3fd80SJustin T. Gibbs 			return (error);
781fac3fd80SJustin T. Gibbs 		}
782fac3fd80SJustin T. Gibbs 	} else {
783fac3fd80SJustin T. Gibbs 		for (i = 0; i < sc->xbd_ring_pages; i++) {
784fac3fd80SJustin T. Gibbs 			char ring_ref_name[]= "ring_refXX";
785fac3fd80SJustin T. Gibbs 
786fac3fd80SJustin T. Gibbs 			snprintf(ring_ref_name, sizeof(ring_ref_name),
787fac3fd80SJustin T. Gibbs 			    "ring-ref%u", i);
788fac3fd80SJustin T. Gibbs 			error = xs_printf(XST_NIL, xenbus_get_node(sc->xbd_dev),
789fac3fd80SJustin T. Gibbs 			     ring_ref_name, "%u", sc->xbd_ring_ref[i]);
790fac3fd80SJustin T. Gibbs 			if (error) {
791fac3fd80SJustin T. Gibbs 				xenbus_dev_fatal(sc->xbd_dev, error,
792fac3fd80SJustin T. Gibbs 				    "writing %s/%s",
793fac3fd80SJustin T. Gibbs 				    xenbus_get_node(sc->xbd_dev),
794fac3fd80SJustin T. Gibbs 				    ring_ref_name);
795fac3fd80SJustin T. Gibbs 				return (error);
796fac3fd80SJustin T. Gibbs 			}
797fac3fd80SJustin T. Gibbs 		}
798fac3fd80SJustin T. Gibbs 	}
799fac3fd80SJustin T. Gibbs 
80076acc41fSJustin T. Gibbs 	error = xen_intr_alloc_and_bind_local_port(sc->xbd_dev,
80176acc41fSJustin T. Gibbs 	    xenbus_get_otherend_id(sc->xbd_dev), NULL, xbd_int, sc,
80276acc41fSJustin T. Gibbs 	    INTR_TYPE_BIO | INTR_MPSAFE, &sc->xen_intr_handle);
803fac3fd80SJustin T. Gibbs 	if (error) {
804fac3fd80SJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
80576acc41fSJustin T. Gibbs 		    "xen_intr_alloc_and_bind_local_port failed");
806fac3fd80SJustin T. Gibbs 		return (error);
807fac3fd80SJustin T. Gibbs 	}
808fac3fd80SJustin T. Gibbs 
809fac3fd80SJustin T. Gibbs 	return (0);
810fac3fd80SJustin T. Gibbs }
811fac3fd80SJustin T. Gibbs 
812d9fab01dSJustin T. Gibbs static void
813d9fab01dSJustin T. Gibbs xbd_free_ring(struct xbd_softc *sc)
814d9fab01dSJustin T. Gibbs {
815d9fab01dSJustin T. Gibbs 	int i;
816d9fab01dSJustin T. Gibbs 
817d9fab01dSJustin T. Gibbs 	if (sc->xbd_ring.sring == NULL)
818d9fab01dSJustin T. Gibbs 		return;
819d9fab01dSJustin T. Gibbs 
820d9fab01dSJustin T. Gibbs 	for (i = 0; i < sc->xbd_ring_pages; i++) {
821d9fab01dSJustin T. Gibbs 		if (sc->xbd_ring_ref[i] != GRANT_REF_INVALID) {
822d9fab01dSJustin T. Gibbs 			gnttab_end_foreign_access_ref(sc->xbd_ring_ref[i]);
823d9fab01dSJustin T. Gibbs 			sc->xbd_ring_ref[i] = GRANT_REF_INVALID;
824d9fab01dSJustin T. Gibbs 		}
825d9fab01dSJustin T. Gibbs 	}
826d9fab01dSJustin T. Gibbs 	free(sc->xbd_ring.sring, M_XENBLOCKFRONT);
827d9fab01dSJustin T. Gibbs 	sc->xbd_ring.sring = NULL;
828d9fab01dSJustin T. Gibbs }
829d9fab01dSJustin T. Gibbs 
830fac3fd80SJustin T. Gibbs /*-------------------------- Initialization/Teardown -------------------------*/
8319985113bSJustin T. Gibbs static int
8329985113bSJustin T. Gibbs xbd_feature_string(struct xbd_softc *sc, char *features, size_t len)
8339985113bSJustin T. Gibbs {
8349985113bSJustin T. Gibbs 	struct sbuf sb;
8359985113bSJustin T. Gibbs 	int feature_cnt;
8369985113bSJustin T. Gibbs 
8379985113bSJustin T. Gibbs 	sbuf_new(&sb, features, len, SBUF_FIXEDLEN);
8389985113bSJustin T. Gibbs 
8399985113bSJustin T. Gibbs 	feature_cnt = 0;
8409985113bSJustin T. Gibbs 	if ((sc->xbd_flags & XBDF_FLUSH) != 0) {
8419985113bSJustin T. Gibbs 		sbuf_printf(&sb, "flush");
8429985113bSJustin T. Gibbs 		feature_cnt++;
8439985113bSJustin T. Gibbs 	}
8449985113bSJustin T. Gibbs 
8459985113bSJustin T. Gibbs 	if ((sc->xbd_flags & XBDF_BARRIER) != 0) {
8469985113bSJustin T. Gibbs 		if (feature_cnt != 0)
8479985113bSJustin T. Gibbs 			sbuf_printf(&sb, ", ");
8489985113bSJustin T. Gibbs 		sbuf_printf(&sb, "write_barrier");
8499985113bSJustin T. Gibbs 		feature_cnt++;
8509985113bSJustin T. Gibbs 	}
8519985113bSJustin T. Gibbs 
852d5d7399dSAlexander Motin 	if ((sc->xbd_flags & XBDF_DISCARD) != 0) {
853d5d7399dSAlexander Motin 		if (feature_cnt != 0)
854d5d7399dSAlexander Motin 			sbuf_printf(&sb, ", ");
855d5d7399dSAlexander Motin 		sbuf_printf(&sb, "discard");
856d5d7399dSAlexander Motin 		feature_cnt++;
857d5d7399dSAlexander Motin 	}
858d5d7399dSAlexander Motin 
859d5d7399dSAlexander Motin 	if ((sc->xbd_flags & XBDF_PERSISTENT) != 0) {
860d5d7399dSAlexander Motin 		if (feature_cnt != 0)
861d5d7399dSAlexander Motin 			sbuf_printf(&sb, ", ");
862d5d7399dSAlexander Motin 		sbuf_printf(&sb, "persistent_grants");
863d5d7399dSAlexander Motin 		feature_cnt++;
864d5d7399dSAlexander Motin 	}
865d5d7399dSAlexander Motin 
8669985113bSJustin T. Gibbs 	(void) sbuf_finish(&sb);
8679985113bSJustin T. Gibbs 	return (sbuf_len(&sb));
8689985113bSJustin T. Gibbs }
8699985113bSJustin T. Gibbs 
8709985113bSJustin T. Gibbs static int
8719985113bSJustin T. Gibbs xbd_sysctl_features(SYSCTL_HANDLER_ARGS)
8729985113bSJustin T. Gibbs {
8739985113bSJustin T. Gibbs 	char features[80];
8749985113bSJustin T. Gibbs 	struct xbd_softc *sc = arg1;
8759985113bSJustin T. Gibbs 	int error;
8769985113bSJustin T. Gibbs 	int len;
8779985113bSJustin T. Gibbs 
8789985113bSJustin T. Gibbs 	error = sysctl_wire_old_buffer(req, 0);
8799985113bSJustin T. Gibbs 	if (error != 0)
8809985113bSJustin T. Gibbs 		return (error);
8819985113bSJustin T. Gibbs 
8829985113bSJustin T. Gibbs 	len = xbd_feature_string(sc, features, sizeof(features));
8839985113bSJustin T. Gibbs 
8849985113bSJustin T. Gibbs 	/* len is -1 on error, which will make the SYSCTL_OUT a no-op. */
8859985113bSJustin T. Gibbs 	return (SYSCTL_OUT(req, features, len + 1/*NUL*/));
8869985113bSJustin T. Gibbs }
8879985113bSJustin T. Gibbs 
8888b8bfa35SJustin T. Gibbs static void
88933eebb6aSJustin T. Gibbs xbd_setup_sysctl(struct xbd_softc *xbd)
8908b8bfa35SJustin T. Gibbs {
8918b8bfa35SJustin T. Gibbs 	struct sysctl_ctx_list *sysctl_ctx = NULL;
8928b8bfa35SJustin T. Gibbs 	struct sysctl_oid *sysctl_tree = NULL;
8939985113bSJustin T. Gibbs 	struct sysctl_oid_list *children;
8948b8bfa35SJustin T. Gibbs 
89533eebb6aSJustin T. Gibbs 	sysctl_ctx = device_get_sysctl_ctx(xbd->xbd_dev);
8968b8bfa35SJustin T. Gibbs 	if (sysctl_ctx == NULL)
8978b8bfa35SJustin T. Gibbs 		return;
8988b8bfa35SJustin T. Gibbs 
89933eebb6aSJustin T. Gibbs 	sysctl_tree = device_get_sysctl_tree(xbd->xbd_dev);
9008b8bfa35SJustin T. Gibbs 	if (sysctl_tree == NULL)
9018b8bfa35SJustin T. Gibbs 		return;
9028b8bfa35SJustin T. Gibbs 
9039985113bSJustin T. Gibbs 	children = SYSCTL_CHILDREN(sysctl_tree);
9049985113bSJustin T. Gibbs 	SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO,
90533eebb6aSJustin T. Gibbs 	    "max_requests", CTLFLAG_RD, &xbd->xbd_max_requests, -1,
9068b8bfa35SJustin T. Gibbs 	    "maximum outstanding requests (negotiated)");
9078b8bfa35SJustin T. Gibbs 
9089985113bSJustin T. Gibbs 	SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO,
9098b8bfa35SJustin T. Gibbs 	    "max_request_segments", CTLFLAG_RD,
91033eebb6aSJustin T. Gibbs 	    &xbd->xbd_max_request_segments, 0,
9118b8bfa35SJustin T. Gibbs 	    "maximum number of pages per requests (negotiated)");
9128b8bfa35SJustin T. Gibbs 
9139985113bSJustin T. Gibbs 	SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO,
91433eebb6aSJustin T. Gibbs 	    "max_request_size", CTLFLAG_RD, &xbd->xbd_max_request_size, 0,
9158b8bfa35SJustin T. Gibbs 	    "maximum size in bytes of a request (negotiated)");
9168b8bfa35SJustin T. Gibbs 
9179985113bSJustin T. Gibbs 	SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO,
91833eebb6aSJustin T. Gibbs 	    "ring_pages", CTLFLAG_RD, &xbd->xbd_ring_pages, 0,
9198b8bfa35SJustin T. Gibbs 	    "communication channel pages (negotiated)");
9209985113bSJustin T. Gibbs 
9219985113bSJustin T. Gibbs 	SYSCTL_ADD_PROC(sysctl_ctx, children, OID_AUTO,
92254daceabSAlexander Motin 	    "features", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, xbd,
92341fc1ce1SPawel Biernacki 	    0, xbd_sysctl_features, "A", "protocol features (negotiated)");
9248b8bfa35SJustin T. Gibbs }
9258b8bfa35SJustin T. Gibbs 
92623dc5621SKip Macy /*
927fac3fd80SJustin T. Gibbs  * Translate Linux major/minor to an appropriate name and unit
928fac3fd80SJustin T. Gibbs  * number. For HVM guests, this allows us to use the same drive names
929fac3fd80SJustin T. Gibbs  * with blkfront as the emulated drives, easing transition slightly.
93023dc5621SKip Macy  */
931fac3fd80SJustin T. Gibbs static void
932fac3fd80SJustin T. Gibbs xbd_vdevice_to_unit(uint32_t vdevice, int *unit, const char **name)
93323dc5621SKip Macy {
934fac3fd80SJustin T. Gibbs 	static struct vdev_info {
935fac3fd80SJustin T. Gibbs 		int major;
936fac3fd80SJustin T. Gibbs 		int shift;
937fac3fd80SJustin T. Gibbs 		int base;
93823dc5621SKip Macy 		const char *name;
939fac3fd80SJustin T. Gibbs 	} info[] = {
940fac3fd80SJustin T. Gibbs 		{3,	6,	0,	"ada"},	/* ide0 */
941fac3fd80SJustin T. Gibbs 		{22,	6,	2,	"ada"},	/* ide1 */
942fac3fd80SJustin T. Gibbs 		{33,	6,	4,	"ada"},	/* ide2 */
943fac3fd80SJustin T. Gibbs 		{34,	6,	6,	"ada"},	/* ide3 */
944fac3fd80SJustin T. Gibbs 		{56,	6,	8,	"ada"},	/* ide4 */
945fac3fd80SJustin T. Gibbs 		{57,	6,	10,	"ada"},	/* ide5 */
946fac3fd80SJustin T. Gibbs 		{88,	6,	12,	"ada"},	/* ide6 */
947fac3fd80SJustin T. Gibbs 		{89,	6,	14,	"ada"},	/* ide7 */
948fac3fd80SJustin T. Gibbs 		{90,	6,	16,	"ada"},	/* ide8 */
949fac3fd80SJustin T. Gibbs 		{91,	6,	18,	"ada"},	/* ide9 */
95089e0f4d2SKip Macy 
951fac3fd80SJustin T. Gibbs 		{8,	4,	0,	"da"},	/* scsi disk0 */
952fac3fd80SJustin T. Gibbs 		{65,	4,	16,	"da"},	/* scsi disk1 */
953fac3fd80SJustin T. Gibbs 		{66,	4,	32,	"da"},	/* scsi disk2 */
954fac3fd80SJustin T. Gibbs 		{67,	4,	48,	"da"},	/* scsi disk3 */
955fac3fd80SJustin T. Gibbs 		{68,	4,	64,	"da"},	/* scsi disk4 */
956fac3fd80SJustin T. Gibbs 		{69,	4,	80,	"da"},	/* scsi disk5 */
957fac3fd80SJustin T. Gibbs 		{70,	4,	96,	"da"},	/* scsi disk6 */
958fac3fd80SJustin T. Gibbs 		{71,	4,	112,	"da"},	/* scsi disk7 */
959fac3fd80SJustin T. Gibbs 		{128,	4,	128,	"da"},	/* scsi disk8 */
960fac3fd80SJustin T. Gibbs 		{129,	4,	144,	"da"},	/* scsi disk9 */
961fac3fd80SJustin T. Gibbs 		{130,	4,	160,	"da"},	/* scsi disk10 */
962fac3fd80SJustin T. Gibbs 		{131,	4,	176,	"da"},	/* scsi disk11 */
963fac3fd80SJustin T. Gibbs 		{132,	4,	192,	"da"},	/* scsi disk12 */
964fac3fd80SJustin T. Gibbs 		{133,	4,	208,	"da"},	/* scsi disk13 */
965fac3fd80SJustin T. Gibbs 		{134,	4,	224,	"da"},	/* scsi disk14 */
966fac3fd80SJustin T. Gibbs 		{135,	4,	240,	"da"},	/* scsi disk15 */
967fac3fd80SJustin T. Gibbs 
968fac3fd80SJustin T. Gibbs 		{202,	4,	0,	"xbd"},	/* xbd */
969fac3fd80SJustin T. Gibbs 
970fac3fd80SJustin T. Gibbs 		{0,	0,	0,	NULL},
971fac3fd80SJustin T. Gibbs 	};
972fac3fd80SJustin T. Gibbs 	int major = vdevice >> 8;
973fac3fd80SJustin T. Gibbs 	int minor = vdevice & 0xff;
974fac3fd80SJustin T. Gibbs 	int i;
975fac3fd80SJustin T. Gibbs 
976fac3fd80SJustin T. Gibbs 	if (vdevice & (1 << 28)) {
977fac3fd80SJustin T. Gibbs 		*unit = (vdevice & ((1 << 28) - 1)) >> 8;
978fac3fd80SJustin T. Gibbs 		*name = "xbd";
979fac3fd80SJustin T. Gibbs 		return;
98089e0f4d2SKip Macy 	}
98189e0f4d2SKip Macy 
982fac3fd80SJustin T. Gibbs 	for (i = 0; info[i].major; i++) {
983fac3fd80SJustin T. Gibbs 		if (info[i].major == major) {
984fac3fd80SJustin T. Gibbs 			*unit = info[i].base + (minor >> info[i].shift);
985fac3fd80SJustin T. Gibbs 			*name = info[i].name;
986fac3fd80SJustin T. Gibbs 			return;
987fac3fd80SJustin T. Gibbs 		}
988fac3fd80SJustin T. Gibbs 	}
989fac3fd80SJustin T. Gibbs 
990fac3fd80SJustin T. Gibbs 	*unit = minor >> 4;
991fac3fd80SJustin T. Gibbs 	*name = "xbd";
992fac3fd80SJustin T. Gibbs }
993fac3fd80SJustin T. Gibbs 
994fac3fd80SJustin T. Gibbs int
995fac3fd80SJustin T. Gibbs xbd_instance_create(struct xbd_softc *sc, blkif_sector_t sectors,
996d5d7399dSAlexander Motin     int vdevice, uint16_t vdisk_info, unsigned long sector_size,
997d5d7399dSAlexander Motin     unsigned long phys_sector_size)
998fac3fd80SJustin T. Gibbs {
9999985113bSJustin T. Gibbs 	char features[80];
1000fac3fd80SJustin T. Gibbs 	int unit, error = 0;
1001fac3fd80SJustin T. Gibbs 	const char *name;
1002fac3fd80SJustin T. Gibbs 
100333eebb6aSJustin T. Gibbs 	xbd_vdevice_to_unit(vdevice, &unit, &name);
100423dc5621SKip Macy 
1005fac3fd80SJustin T. Gibbs 	sc->xbd_unit = unit;
100689e0f4d2SKip Macy 
10079985113bSJustin T. Gibbs 	if (strcmp(name, "xbd") != 0)
1008fac3fd80SJustin T. Gibbs 		device_printf(sc->xbd_dev, "attaching as %s%d\n", name, unit);
100989e0f4d2SKip Macy 
10109985113bSJustin T. Gibbs 	if (xbd_feature_string(sc, features, sizeof(features)) > 0) {
10119985113bSJustin T. Gibbs 		device_printf(sc->xbd_dev, "features: %s\n",
10129985113bSJustin T. Gibbs 		    features);
10139985113bSJustin T. Gibbs 	}
10149985113bSJustin T. Gibbs 
1015fac3fd80SJustin T. Gibbs 	sc->xbd_disk = disk_alloc();
1016fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_unit = sc->xbd_unit;
1017fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_open = xbd_open;
1018fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_close = xbd_close;
1019fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_ioctl = xbd_ioctl;
1020fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_strategy = xbd_strategy;
1021fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_dump = xbd_dump;
1022fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_name = name;
1023fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_drv1 = sc;
1024fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_sectorsize = sector_size;
1025d5d7399dSAlexander Motin 	sc->xbd_disk->d_stripesize = phys_sector_size;
1026d5d7399dSAlexander Motin 	sc->xbd_disk->d_stripeoffset = 0;
10278b8bfa35SJustin T. Gibbs 
1028fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_mediasize = sectors * sector_size;
1029fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_maxsize = sc->xbd_max_request_size;
1030a55a04a8SRoger Pau Monné 	sc->xbd_disk->d_flags = DISKFLAG_UNMAPPED_BIO;
10319985113bSJustin T. Gibbs 	if ((sc->xbd_flags & (XBDF_FLUSH|XBDF_BARRIER)) != 0) {
10329985113bSJustin T. Gibbs 		sc->xbd_disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
10339985113bSJustin T. Gibbs 		device_printf(sc->xbd_dev,
10349985113bSJustin T. Gibbs 		    "synchronize cache commands enabled.\n");
10359985113bSJustin T. Gibbs 	}
1036fac3fd80SJustin T. Gibbs 	disk_create(sc->xbd_disk, DISK_VERSION);
103712678024SDoug Rabson 
1038fac3fd80SJustin T. Gibbs 	return error;
103912678024SDoug Rabson }
104012678024SDoug Rabson 
1041fac3fd80SJustin T. Gibbs static void
1042fac3fd80SJustin T. Gibbs xbd_free(struct xbd_softc *sc)
104312678024SDoug Rabson {
1044fac3fd80SJustin T. Gibbs 	int i;
104512678024SDoug Rabson 
104612678024SDoug Rabson 	/* Prevent new requests being issued until we fix things up. */
104733eebb6aSJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
1048e2c1fe90SJustin T. Gibbs 	sc->xbd_state = XBD_STATE_DISCONNECTED;
104933eebb6aSJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
105089e0f4d2SKip Macy 
1051fac3fd80SJustin T. Gibbs 	/* Free resources associated with old device channel. */
1052d9fab01dSJustin T. Gibbs 	xbd_free_ring(sc);
1053fac3fd80SJustin T. Gibbs 	if (sc->xbd_shadow) {
1054fac3fd80SJustin T. Gibbs 		for (i = 0; i < sc->xbd_max_requests; i++) {
1055fac3fd80SJustin T. Gibbs 			struct xbd_command *cm;
105689e0f4d2SKip Macy 
1057fac3fd80SJustin T. Gibbs 			cm = &sc->xbd_shadow[i];
1058fac3fd80SJustin T. Gibbs 			if (cm->cm_sg_refs != NULL) {
1059fac3fd80SJustin T. Gibbs 				free(cm->cm_sg_refs, M_XENBLOCKFRONT);
1060fac3fd80SJustin T. Gibbs 				cm->cm_sg_refs = NULL;
106189e0f4d2SKip Macy 			}
106289e0f4d2SKip Macy 
1063aaebf690SColin Percival 			if (cm->cm_indirectionpages != NULL) {
1064aaebf690SColin Percival 				gnttab_end_foreign_access_references(
1065aaebf690SColin Percival 				    sc->xbd_max_request_indirectpages,
1066aaebf690SColin Percival 				    &cm->cm_indirectionrefs[0]);
1067*d1bdc282SBjoern A. Zeeb 				free(cm->cm_indirectionpages, M_XENBLOCKFRONT);
1068aaebf690SColin Percival 				cm->cm_indirectionpages = NULL;
1069aaebf690SColin Percival 			}
1070aaebf690SColin Percival 
1071fac3fd80SJustin T. Gibbs 			bus_dmamap_destroy(sc->xbd_io_dmat, cm->cm_map);
1072fac3fd80SJustin T. Gibbs 		}
1073fac3fd80SJustin T. Gibbs 		free(sc->xbd_shadow, M_XENBLOCKFRONT);
1074fac3fd80SJustin T. Gibbs 		sc->xbd_shadow = NULL;
1075fac3fd80SJustin T. Gibbs 
1076fac3fd80SJustin T. Gibbs 		bus_dma_tag_destroy(sc->xbd_io_dmat);
1077fac3fd80SJustin T. Gibbs 
1078e2c1fe90SJustin T. Gibbs 		xbd_initq_cm(sc, XBD_Q_FREE);
1079e2c1fe90SJustin T. Gibbs 		xbd_initq_cm(sc, XBD_Q_READY);
1080e2c1fe90SJustin T. Gibbs 		xbd_initq_cm(sc, XBD_Q_COMPLETE);
1081fac3fd80SJustin T. Gibbs 	}
1082fac3fd80SJustin T. Gibbs 
108376acc41fSJustin T. Gibbs 	xen_intr_unbind(&sc->xen_intr_handle);
108476acc41fSJustin T. Gibbs 
1085fac3fd80SJustin T. Gibbs }
1086fac3fd80SJustin T. Gibbs 
1087fac3fd80SJustin T. Gibbs /*--------------------------- State Change Handlers --------------------------*/
1088ff662b5cSJustin T. Gibbs static void
108933eebb6aSJustin T. Gibbs xbd_initialize(struct xbd_softc *sc)
109089e0f4d2SKip Macy {
1091ff662b5cSJustin T. Gibbs 	const char *otherend_path;
1092ff662b5cSJustin T. Gibbs 	const char *node_path;
10938b8bfa35SJustin T. Gibbs 	uint32_t max_ring_page_order;
1094ff662b5cSJustin T. Gibbs 	int error;
109589e0f4d2SKip Macy 
109633eebb6aSJustin T. Gibbs 	if (xenbus_get_state(sc->xbd_dev) != XenbusStateInitialising) {
109706a630f6SJustin T. Gibbs 		/* Initialization has already been performed. */
1098ff662b5cSJustin T. Gibbs 		return;
109906a630f6SJustin T. Gibbs 	}
110089e0f4d2SKip Macy 
1101ff662b5cSJustin T. Gibbs 	/*
1102ff662b5cSJustin T. Gibbs 	 * Protocol defaults valid even if negotiation for a
1103ff662b5cSJustin T. Gibbs 	 * setting fails.
1104ff662b5cSJustin T. Gibbs 	 */
11058b8bfa35SJustin T. Gibbs 	max_ring_page_order = 0;
110633eebb6aSJustin T. Gibbs 	sc->xbd_ring_pages = 1;
1107ff662b5cSJustin T. Gibbs 
1108ff662b5cSJustin T. Gibbs 	/*
1109ff662b5cSJustin T. Gibbs 	 * Protocol negotiation.
1110ff662b5cSJustin T. Gibbs 	 *
1111ff662b5cSJustin T. Gibbs 	 * \note xs_gather() returns on the first encountered error, so
1112453130d9SPedro F. Giffuni 	 *       we must use independent calls in order to guarantee
1113ff662b5cSJustin T. Gibbs 	 *       we don't miss information in a sparsly populated back-end
1114ff662b5cSJustin T. Gibbs 	 *       tree.
11158b8bfa35SJustin T. Gibbs 	 *
11168b8bfa35SJustin T. Gibbs 	 * \note xs_scanf() does not update variables for unmatched
11178b8bfa35SJustin T. Gibbs 	 *	 fields.
1118ff662b5cSJustin T. Gibbs 	 */
111933eebb6aSJustin T. Gibbs 	otherend_path = xenbus_get_otherend_path(sc->xbd_dev);
112033eebb6aSJustin T. Gibbs 	node_path = xenbus_get_node(sc->xbd_dev);
11218b8bfa35SJustin T. Gibbs 
11228b8bfa35SJustin T. Gibbs 	/* Support both backend schemes for relaying ring page limits. */
11238b8bfa35SJustin T. Gibbs 	(void)xs_scanf(XST_NIL, otherend_path,
11248b8bfa35SJustin T. Gibbs 	    "max-ring-page-order", NULL, "%" PRIu32,
11258b8bfa35SJustin T. Gibbs 	    &max_ring_page_order);
112633eebb6aSJustin T. Gibbs 	sc->xbd_ring_pages = 1 << max_ring_page_order;
1127ff662b5cSJustin T. Gibbs 	(void)xs_scanf(XST_NIL, otherend_path,
1128ff662b5cSJustin T. Gibbs 	    "max-ring-pages", NULL, "%" PRIu32,
112933eebb6aSJustin T. Gibbs 	    &sc->xbd_ring_pages);
113033eebb6aSJustin T. Gibbs 	if (sc->xbd_ring_pages < 1)
113133eebb6aSJustin T. Gibbs 		sc->xbd_ring_pages = 1;
1132ff662b5cSJustin T. Gibbs 
113333eebb6aSJustin T. Gibbs 	if (sc->xbd_ring_pages > XBD_MAX_RING_PAGES) {
113433eebb6aSJustin T. Gibbs 		device_printf(sc->xbd_dev,
113533eebb6aSJustin T. Gibbs 		    "Back-end specified ring-pages of %u "
1136112cacaeSRoger Pau Monné 		    "limited to front-end limit of %u.\n",
113733eebb6aSJustin T. Gibbs 		    sc->xbd_ring_pages, XBD_MAX_RING_PAGES);
113833eebb6aSJustin T. Gibbs 		sc->xbd_ring_pages = XBD_MAX_RING_PAGES;
113989e0f4d2SKip Macy 	}
114089e0f4d2SKip Macy 
114133eebb6aSJustin T. Gibbs 	if (powerof2(sc->xbd_ring_pages) == 0) {
11428b8bfa35SJustin T. Gibbs 		uint32_t new_page_limit;
11438b8bfa35SJustin T. Gibbs 
114433eebb6aSJustin T. Gibbs 		new_page_limit = 0x01 << (fls(sc->xbd_ring_pages) - 1);
114533eebb6aSJustin T. Gibbs 		device_printf(sc->xbd_dev,
114633eebb6aSJustin T. Gibbs 		    "Back-end specified ring-pages of %u "
114733eebb6aSJustin T. Gibbs 		    "is not a power of 2. Limited to %u.\n",
114833eebb6aSJustin T. Gibbs 		    sc->xbd_ring_pages, new_page_limit);
114933eebb6aSJustin T. Gibbs 		sc->xbd_ring_pages = new_page_limit;
11508b8bfa35SJustin T. Gibbs 	}
11518b8bfa35SJustin T. Gibbs 
1152112cacaeSRoger Pau Monné 	sc->xbd_max_requests =
1153112cacaeSRoger Pau Monné 	    BLKIF_MAX_RING_REQUESTS(sc->xbd_ring_pages * PAGE_SIZE);
115433eebb6aSJustin T. Gibbs 	if (sc->xbd_max_requests > XBD_MAX_REQUESTS) {
115533eebb6aSJustin T. Gibbs 		device_printf(sc->xbd_dev,
115633eebb6aSJustin T. Gibbs 		    "Back-end specified max_requests of %u "
1157112cacaeSRoger Pau Monné 		    "limited to front-end limit of %zu.\n",
115833eebb6aSJustin T. Gibbs 		    sc->xbd_max_requests, XBD_MAX_REQUESTS);
115933eebb6aSJustin T. Gibbs 		sc->xbd_max_requests = XBD_MAX_REQUESTS;
116089e0f4d2SKip Macy 	}
1161ff662b5cSJustin T. Gibbs 
1162cdf5d66fSJustin T. Gibbs 	if (xbd_alloc_ring(sc) != 0)
1163ff662b5cSJustin T. Gibbs 		return;
1164ff662b5cSJustin T. Gibbs 
11658b8bfa35SJustin T. Gibbs 	/* Support both backend schemes for relaying ring page limits. */
116633eebb6aSJustin T. Gibbs 	if (sc->xbd_ring_pages > 1) {
1167ff662b5cSJustin T. Gibbs 		error = xs_printf(XST_NIL, node_path,
116833eebb6aSJustin T. Gibbs 		    "num-ring-pages","%u",
116933eebb6aSJustin T. Gibbs 		    sc->xbd_ring_pages);
1170ff662b5cSJustin T. Gibbs 		if (error) {
117133eebb6aSJustin T. Gibbs 			xenbus_dev_fatal(sc->xbd_dev, error,
11728b8bfa35SJustin T. Gibbs 			    "writing %s/num-ring-pages",
11738b8bfa35SJustin T. Gibbs 			    node_path);
11748b8bfa35SJustin T. Gibbs 			return;
11758b8bfa35SJustin T. Gibbs 		}
11760d172324SJustin T. Gibbs 
11778b8bfa35SJustin T. Gibbs 		error = xs_printf(XST_NIL, node_path,
11780d172324SJustin T. Gibbs 		    "ring-page-order", "%u",
117933eebb6aSJustin T. Gibbs 		    fls(sc->xbd_ring_pages) - 1);
11808b8bfa35SJustin T. Gibbs 		if (error) {
118133eebb6aSJustin T. Gibbs 			xenbus_dev_fatal(sc->xbd_dev, error,
11828b8bfa35SJustin T. Gibbs 			    "writing %s/ring-page-order",
1183ff662b5cSJustin T. Gibbs 			    node_path);
1184ff662b5cSJustin T. Gibbs 			return;
1185ff662b5cSJustin T. Gibbs 		}
11860d172324SJustin T. Gibbs 	}
1187ff662b5cSJustin T. Gibbs 
1188ff662b5cSJustin T. Gibbs 	error = xs_printf(XST_NIL, node_path, "event-channel",
118976acc41fSJustin T. Gibbs 	    "%u", xen_intr_port(sc->xen_intr_handle));
1190ff662b5cSJustin T. Gibbs 	if (error) {
119133eebb6aSJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
1192ff662b5cSJustin T. Gibbs 		    "writing %s/event-channel",
1193ff662b5cSJustin T. Gibbs 		    node_path);
1194ff662b5cSJustin T. Gibbs 		return;
1195ff662b5cSJustin T. Gibbs 	}
1196ff662b5cSJustin T. Gibbs 
119733eebb6aSJustin T. Gibbs 	error = xs_printf(XST_NIL, node_path, "protocol",
119833eebb6aSJustin T. Gibbs 	    "%s", XEN_IO_PROTO_ABI_NATIVE);
1199ff662b5cSJustin T. Gibbs 	if (error) {
120033eebb6aSJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
1201ff662b5cSJustin T. Gibbs 		    "writing %s/protocol",
1202ff662b5cSJustin T. Gibbs 		    node_path);
1203ff662b5cSJustin T. Gibbs 		return;
120423dc5621SKip Macy 	}
120512678024SDoug Rabson 
120633eebb6aSJustin T. Gibbs 	xenbus_set_state(sc->xbd_dev, XenbusStateInitialised);
120789e0f4d2SKip Macy }
120889e0f4d2SKip Macy 
120989e0f4d2SKip Macy /*
121033eebb6aSJustin T. Gibbs  * Invoked when the backend is finally 'ready' (and has published
121133eebb6aSJustin T. Gibbs  * the details about the physical device - #sectors, size, etc).
121289e0f4d2SKip Macy  */
121389e0f4d2SKip Macy static void
121433eebb6aSJustin T. Gibbs xbd_connect(struct xbd_softc *sc)
121589e0f4d2SKip Macy {
121633eebb6aSJustin T. Gibbs 	device_t dev = sc->xbd_dev;
12173e7df58dSRoger Pau Monné 	blkif_sector_t sectors;
12183e7df58dSRoger Pau Monné 	unsigned long sector_size, phys_sector_size;
121989e0f4d2SKip Macy 	unsigned int binfo;
12209985113bSJustin T. Gibbs 	int err, feature_barrier, feature_flush;
1221aaebf690SColin Percival 	int i, j;
122289e0f4d2SKip Macy 
122323dc5621SKip Macy 	DPRINTK("blkfront.c:connect:%s.\n", xenbus_get_otherend_path(dev));
122489e0f4d2SKip Macy 
1225e55624c6SPawel Jakub Dawidek 	if (sc->xbd_state == XBD_STATE_SUSPENDED) {
1226e55624c6SPawel Jakub Dawidek 		return;
1227e55624c6SPawel Jakub Dawidek 	}
1228e55624c6SPawel Jakub Dawidek 
1229e55624c6SPawel Jakub Dawidek 	if (sc->xbd_state == XBD_STATE_CONNECTED) {
1230e55624c6SPawel Jakub Dawidek 		struct disk *disk;
1231e55624c6SPawel Jakub Dawidek 
1232e55624c6SPawel Jakub Dawidek 		disk = sc->xbd_disk;
1233e55624c6SPawel Jakub Dawidek 		if (disk == NULL) {
1234e55624c6SPawel Jakub Dawidek 			return;
1235e55624c6SPawel Jakub Dawidek 		}
1236e55624c6SPawel Jakub Dawidek 		err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
12373e7df58dSRoger Pau Monné 		    "sectors", "%"PRIu64, &sectors, NULL);
1238e55624c6SPawel Jakub Dawidek 		if (err != 0) {
1239e55624c6SPawel Jakub Dawidek 			xenbus_dev_error(dev, err,
1240e55624c6SPawel Jakub Dawidek 			    "reading sectors at %s",
1241e55624c6SPawel Jakub Dawidek 			    xenbus_get_otherend_path(dev));
1242e55624c6SPawel Jakub Dawidek 			return;
1243e55624c6SPawel Jakub Dawidek 		}
1244e55624c6SPawel Jakub Dawidek 		disk->d_mediasize = disk->d_sectorsize * sectors;
1245e55624c6SPawel Jakub Dawidek 		err = disk_resize(disk, M_NOWAIT);
1246e55624c6SPawel Jakub Dawidek 		if (err) {
1247e55624c6SPawel Jakub Dawidek 			xenbus_dev_error(dev, err,
1248e55624c6SPawel Jakub Dawidek 			    "unable to resize disk %s%u",
1249e55624c6SPawel Jakub Dawidek 			    disk->d_name, disk->d_unit);
1250e55624c6SPawel Jakub Dawidek 			return;
1251e55624c6SPawel Jakub Dawidek 		}
1252e55624c6SPawel Jakub Dawidek 		device_printf(sc->xbd_dev,
1253e55624c6SPawel Jakub Dawidek 		    "changed capacity to %jd\n",
1254e55624c6SPawel Jakub Dawidek 		    (intmax_t)disk->d_mediasize);
1255e55624c6SPawel Jakub Dawidek 		return;
1256e55624c6SPawel Jakub Dawidek 	}
1257e55624c6SPawel Jakub Dawidek 
1258ff662b5cSJustin T. Gibbs 	err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
12593e7df58dSRoger Pau Monné 	    "sectors", "%"PRIu64, &sectors,
126089e0f4d2SKip Macy 	    "info", "%u", &binfo,
126189e0f4d2SKip Macy 	    "sector-size", "%lu", &sector_size,
126289e0f4d2SKip Macy 	    NULL);
126389e0f4d2SKip Macy 	if (err) {
126423dc5621SKip Macy 		xenbus_dev_fatal(dev, err,
126589e0f4d2SKip Macy 		    "reading backend fields at %s",
126623dc5621SKip Macy 		    xenbus_get_otherend_path(dev));
126789e0f4d2SKip Macy 		return;
126889e0f4d2SKip Macy 	}
126993954c2dSColin Percival 	if ((sectors == 0) || (sector_size == 0)) {
127093954c2dSColin Percival 		xenbus_dev_fatal(dev, 0,
127193954c2dSColin Percival 		    "invalid parameters from %s:"
12723e7df58dSRoger Pau Monné 		    " sectors = %"PRIu64", sector_size = %lu",
127393954c2dSColin Percival 		    xenbus_get_otherend_path(dev),
127493954c2dSColin Percival 		    sectors, sector_size);
127593954c2dSColin Percival 		return;
127693954c2dSColin Percival 	}
1277ff662b5cSJustin T. Gibbs 	err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
1278d5d7399dSAlexander Motin 	     "physical-sector-size", "%lu", &phys_sector_size,
1279d5d7399dSAlexander Motin 	     NULL);
1280d5d7399dSAlexander Motin 	if (err || phys_sector_size <= sector_size)
1281d5d7399dSAlexander Motin 		phys_sector_size = 0;
1282d5d7399dSAlexander Motin 	err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
1283085def3fSDimitry Andric 	     "feature-barrier", "%d", &feature_barrier,
128489e0f4d2SKip Macy 	     NULL);
12859985113bSJustin T. Gibbs 	if (err == 0 && feature_barrier != 0)
1286e2c1fe90SJustin T. Gibbs 		sc->xbd_flags |= XBDF_BARRIER;
128789e0f4d2SKip Macy 
12889985113bSJustin T. Gibbs 	err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
1289085def3fSDimitry Andric 	     "feature-flush-cache", "%d", &feature_flush,
12909985113bSJustin T. Gibbs 	     NULL);
12919985113bSJustin T. Gibbs 	if (err == 0 && feature_flush != 0)
12929985113bSJustin T. Gibbs 		sc->xbd_flags |= XBDF_FLUSH;
12939985113bSJustin T. Gibbs 
1294aaebf690SColin Percival 	err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
1295aaebf690SColin Percival 	    "feature-max-indirect-segments", "%" PRIu32,
1296aaebf690SColin Percival 	    &sc->xbd_max_request_segments, NULL);
1297aaebf690SColin Percival 	if ((err != 0) || (xbd_enable_indirect == 0))
1298aaebf690SColin Percival 		sc->xbd_max_request_segments = 0;
1299aaebf690SColin Percival 	if (sc->xbd_max_request_segments > XBD_MAX_INDIRECT_SEGMENTS)
1300aaebf690SColin Percival 		sc->xbd_max_request_segments = XBD_MAX_INDIRECT_SEGMENTS;
1301cd853791SKonstantin Belousov 	if (sc->xbd_max_request_segments > XBD_SIZE_TO_SEGS(maxphys))
1302cd853791SKonstantin Belousov 		sc->xbd_max_request_segments = XBD_SIZE_TO_SEGS(maxphys);
1303aaebf690SColin Percival 	sc->xbd_max_request_indirectpages =
1304aaebf690SColin Percival 	    XBD_INDIRECT_SEGS_TO_PAGES(sc->xbd_max_request_segments);
1305aaebf690SColin Percival 	if (sc->xbd_max_request_segments < BLKIF_MAX_SEGMENTS_PER_REQUEST)
1306aaebf690SColin Percival 		sc->xbd_max_request_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST;
1307aaebf690SColin Percival 	sc->xbd_max_request_size =
1308aaebf690SColin Percival 	    XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments);
1309aaebf690SColin Percival 
131091fb36cfSColin Percival 	/* Allocate datastructures based on negotiated values. */
131191fb36cfSColin Percival 	err = bus_dma_tag_create(
131291fb36cfSColin Percival 	    bus_get_dma_tag(sc->xbd_dev),	/* parent */
131391fb36cfSColin Percival 	    512, PAGE_SIZE,			/* algnmnt, boundary */
131491fb36cfSColin Percival 	    BUS_SPACE_MAXADDR,			/* lowaddr */
131591fb36cfSColin Percival 	    BUS_SPACE_MAXADDR,			/* highaddr */
131691fb36cfSColin Percival 	    NULL, NULL,				/* filter, filterarg */
131791fb36cfSColin Percival 	    sc->xbd_max_request_size,
131891fb36cfSColin Percival 	    sc->xbd_max_request_segments,
131991fb36cfSColin Percival 	    PAGE_SIZE,				/* maxsegsize */
132091fb36cfSColin Percival 	    BUS_DMA_ALLOCNOW,			/* flags */
132191fb36cfSColin Percival 	    busdma_lock_mutex,			/* lockfunc */
132291fb36cfSColin Percival 	    &sc->xbd_io_lock,			/* lockarg */
132391fb36cfSColin Percival 	    &sc->xbd_io_dmat);
132491fb36cfSColin Percival 	if (err != 0) {
132591fb36cfSColin Percival 		xenbus_dev_fatal(sc->xbd_dev, err,
132691fb36cfSColin Percival 		    "Cannot allocate parent DMA tag\n");
132791fb36cfSColin Percival 		return;
132891fb36cfSColin Percival 	}
132991fb36cfSColin Percival 
133091fb36cfSColin Percival 	/* Per-transaction data allocation. */
1331ac2fffa4SPedro F. Giffuni 	sc->xbd_shadow = malloc(sizeof(*sc->xbd_shadow) * sc->xbd_max_requests,
1332ac2fffa4SPedro F. Giffuni 	    M_XENBLOCKFRONT, M_NOWAIT|M_ZERO);
133391fb36cfSColin Percival 	if (sc->xbd_shadow == NULL) {
133491fb36cfSColin Percival 		bus_dma_tag_destroy(sc->xbd_io_dmat);
133591fb36cfSColin Percival 		xenbus_dev_fatal(sc->xbd_dev, ENOMEM,
133691fb36cfSColin Percival 		    "Cannot allocate request structures\n");
133791fb36cfSColin Percival 		return;
133891fb36cfSColin Percival 	}
133991fb36cfSColin Percival 
134091fb36cfSColin Percival 	for (i = 0; i < sc->xbd_max_requests; i++) {
134191fb36cfSColin Percival 		struct xbd_command *cm;
1342aaebf690SColin Percival 		void * indirectpages;
134391fb36cfSColin Percival 
134491fb36cfSColin Percival 		cm = &sc->xbd_shadow[i];
1345ac2fffa4SPedro F. Giffuni 		cm->cm_sg_refs = malloc(
1346ac2fffa4SPedro F. Giffuni 		    sizeof(grant_ref_t) * sc->xbd_max_request_segments,
1347ac2fffa4SPedro F. Giffuni 		    M_XENBLOCKFRONT, M_NOWAIT);
134891fb36cfSColin Percival 		if (cm->cm_sg_refs == NULL)
134991fb36cfSColin Percival 			break;
135091fb36cfSColin Percival 		cm->cm_id = i;
135191fb36cfSColin Percival 		cm->cm_flags = XBDCF_INITIALIZER;
135291fb36cfSColin Percival 		cm->cm_sc = sc;
135391fb36cfSColin Percival 		if (bus_dmamap_create(sc->xbd_io_dmat, 0, &cm->cm_map) != 0)
135491fb36cfSColin Percival 			break;
1355aaebf690SColin Percival 		if (sc->xbd_max_request_indirectpages > 0) {
1356aaebf690SColin Percival 			indirectpages = contigmalloc(
1357aaebf690SColin Percival 			    PAGE_SIZE * sc->xbd_max_request_indirectpages,
135883c2fa73SRoger Pau Monné 			    M_XENBLOCKFRONT, M_ZERO | M_NOWAIT, 0, ~0,
135983c2fa73SRoger Pau Monné 			    PAGE_SIZE, 0);
136083c2fa73SRoger Pau Monné 			if (indirectpages == NULL)
136183c2fa73SRoger Pau Monné 				sc->xbd_max_request_indirectpages = 0;
1362aaebf690SColin Percival 		} else {
1363aaebf690SColin Percival 			indirectpages = NULL;
1364aaebf690SColin Percival 		}
1365aaebf690SColin Percival 		for (j = 0; j < sc->xbd_max_request_indirectpages; j++) {
1366aaebf690SColin Percival 			if (gnttab_grant_foreign_access(
1367aaebf690SColin Percival 			    xenbus_get_otherend_id(sc->xbd_dev),
13683c790178SJohn Baldwin 			    (vtophys(indirectpages) >> PAGE_SHIFT) + j,
1369aaebf690SColin Percival 			    1 /* grant read-only access */,
1370aaebf690SColin Percival 			    &cm->cm_indirectionrefs[j]))
1371aaebf690SColin Percival 				break;
1372aaebf690SColin Percival 		}
137383c2fa73SRoger Pau Monné 		if (j < sc->xbd_max_request_indirectpages) {
1374*d1bdc282SBjoern A. Zeeb 			free(indirectpages, M_XENBLOCKFRONT);
1375aaebf690SColin Percival 			break;
137683c2fa73SRoger Pau Monné 		}
1377aaebf690SColin Percival 		cm->cm_indirectionpages = indirectpages;
137891fb36cfSColin Percival 		xbd_free_command(cm);
137991fb36cfSColin Percival 	}
138091fb36cfSColin Percival 
138133eebb6aSJustin T. Gibbs 	if (sc->xbd_disk == NULL) {
138223dc5621SKip Macy 		device_printf(dev, "%juMB <%s> at %s",
138323dc5621SKip Macy 		    (uintmax_t) sectors / (1048576 / sector_size),
138423dc5621SKip Macy 		    device_get_desc(dev),
138523dc5621SKip Macy 		    xenbus_get_node(dev));
138623dc5621SKip Macy 		bus_print_child_footer(device_get_parent(dev), dev);
138789e0f4d2SKip Macy 
138833eebb6aSJustin T. Gibbs 		xbd_instance_create(sc, sectors, sc->xbd_vdevice, binfo,
1389d5d7399dSAlexander Motin 		    sector_size, phys_sector_size);
139006a630f6SJustin T. Gibbs 	}
139123dc5621SKip Macy 
139223dc5621SKip Macy 	(void)xenbus_set_state(dev, XenbusStateConnected);
139389e0f4d2SKip Macy 
139489e0f4d2SKip Macy 	/* Kick pending requests. */
139533eebb6aSJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
1396e2c1fe90SJustin T. Gibbs 	sc->xbd_state = XBD_STATE_CONNECTED;
139733eebb6aSJustin T. Gibbs 	xbd_startio(sc);
1398e2c1fe90SJustin T. Gibbs 	sc->xbd_flags |= XBDF_READY;
139933eebb6aSJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
140089e0f4d2SKip Macy }
140189e0f4d2SKip Macy 
140289e0f4d2SKip Macy /**
140389e0f4d2SKip Macy  * Handle the change of state of the backend to Closing.  We must delete our
140489e0f4d2SKip Macy  * device-layer structures now, to ensure that writes are flushed through to
1405a4660d59SJustin T. Gibbs  * the backend.  Once this is done, we can switch to Closed in
140689e0f4d2SKip Macy  * acknowledgement.
140789e0f4d2SKip Macy  */
140823dc5621SKip Macy static void
140933eebb6aSJustin T. Gibbs xbd_closing(device_t dev)
141089e0f4d2SKip Macy {
141133eebb6aSJustin T. Gibbs 	struct xbd_softc *sc = device_get_softc(dev);
141289e0f4d2SKip Macy 
1413ff662b5cSJustin T. Gibbs 	xenbus_set_state(dev, XenbusStateClosing);
1414ff662b5cSJustin T. Gibbs 
141533eebb6aSJustin T. Gibbs 	DPRINTK("xbd_closing: %s removed\n", xenbus_get_node(dev));
141689e0f4d2SKip Macy 
141733eebb6aSJustin T. Gibbs 	if (sc->xbd_disk != NULL) {
141833eebb6aSJustin T. Gibbs 		disk_destroy(sc->xbd_disk);
141933eebb6aSJustin T. Gibbs 		sc->xbd_disk = NULL;
142089e0f4d2SKip Macy 	}
142189e0f4d2SKip Macy 
142223dc5621SKip Macy 	xenbus_set_state(dev, XenbusStateClosed);
142389e0f4d2SKip Macy }
142489e0f4d2SKip Macy 
1425fac3fd80SJustin T. Gibbs /*---------------------------- NewBus Entrypoints ----------------------------*/
1426fac3fd80SJustin T. Gibbs static int
1427fac3fd80SJustin T. Gibbs xbd_probe(device_t dev)
1428fac3fd80SJustin T. Gibbs {
1429a371f519SJustin T. Gibbs 	if (strcmp(xenbus_get_type(dev), "vbd") != 0)
1430a371f519SJustin T. Gibbs 		return (ENXIO);
1431fac3fd80SJustin T. Gibbs 
143269c6eee7SJulien Grall 	if (xen_pv_disks_disabled())
1433f8f1bb83SRoger Pau Monné 		return (ENXIO);
1434f8f1bb83SRoger Pau Monné 
1435a371f519SJustin T. Gibbs 	if (xen_hvm_domain()) {
1436a371f519SJustin T. Gibbs 		int error;
1437a371f519SJustin T. Gibbs 		char *type;
1438a371f519SJustin T. Gibbs 
1439a371f519SJustin T. Gibbs 		/*
1440a371f519SJustin T. Gibbs 		 * When running in an HVM domain, IDE disk emulation is
1441a371f519SJustin T. Gibbs 		 * disabled early in boot so that native drivers will
1442a371f519SJustin T. Gibbs 		 * not see emulated hardware.  However, CDROM device
1443a371f519SJustin T. Gibbs 		 * emulation cannot be disabled.
1444a371f519SJustin T. Gibbs 		 *
1445a371f519SJustin T. Gibbs 		 * Through use of FreeBSD's vm_guest and xen_hvm_domain()
1446a371f519SJustin T. Gibbs 		 * APIs, we could modify the native CDROM driver to fail its
1447a371f519SJustin T. Gibbs 		 * probe when running under Xen.  Unfortunatlely, the PV
1448a371f519SJustin T. Gibbs 		 * CDROM support in XenServer (up through at least version
1449a371f519SJustin T. Gibbs 		 * 6.2) isn't functional, so we instead rely on the emulated
1450a371f519SJustin T. Gibbs 		 * CDROM instance, and fail to attach the PV one here in
1451a371f519SJustin T. Gibbs 		 * the blkfront driver.
1452a371f519SJustin T. Gibbs 		 */
1453a371f519SJustin T. Gibbs 		error = xs_read(XST_NIL, xenbus_get_node(dev),
1454a371f519SJustin T. Gibbs 		    "device-type", NULL, (void **) &type);
1455a371f519SJustin T. Gibbs 		if (error)
1456a371f519SJustin T. Gibbs 			return (ENXIO);
1457a371f519SJustin T. Gibbs 
1458a371f519SJustin T. Gibbs 		if (strncmp(type, "cdrom", 5) == 0) {
1459a371f519SJustin T. Gibbs 			free(type, M_XENSTORE);
1460a371f519SJustin T. Gibbs 			return (ENXIO);
1461a371f519SJustin T. Gibbs 		}
1462a371f519SJustin T. Gibbs 		free(type, M_XENSTORE);
1463a371f519SJustin T. Gibbs 	}
1464a371f519SJustin T. Gibbs 
1465fac3fd80SJustin T. Gibbs 	device_set_desc(dev, "Virtual Block Device");
1466fac3fd80SJustin T. Gibbs 	device_quiet(dev);
1467fac3fd80SJustin T. Gibbs 	return (0);
1468fac3fd80SJustin T. Gibbs }
1469fac3fd80SJustin T. Gibbs 
1470fac3fd80SJustin T. Gibbs /*
1471fac3fd80SJustin T. Gibbs  * Setup supplies the backend dir, virtual device.  We place an event
1472fac3fd80SJustin T. Gibbs  * channel and shared frame entries.  We watch backend to wait if it's
1473fac3fd80SJustin T. Gibbs  * ok.
1474fac3fd80SJustin T. Gibbs  */
1475fac3fd80SJustin T. Gibbs static int
1476fac3fd80SJustin T. Gibbs xbd_attach(device_t dev)
1477fac3fd80SJustin T. Gibbs {
1478fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc;
1479fac3fd80SJustin T. Gibbs 	const char *name;
1480fac3fd80SJustin T. Gibbs 	uint32_t vdevice;
1481fac3fd80SJustin T. Gibbs 	int error;
1482fac3fd80SJustin T. Gibbs 	int i;
1483fac3fd80SJustin T. Gibbs 	int unit;
1484fac3fd80SJustin T. Gibbs 
1485fac3fd80SJustin T. Gibbs 	/* FIXME: Use dynamic device id if this is not set. */
1486fac3fd80SJustin T. Gibbs 	error = xs_scanf(XST_NIL, xenbus_get_node(dev),
1487fac3fd80SJustin T. Gibbs 	    "virtual-device", NULL, "%" PRIu32, &vdevice);
148848a1ceedSColin Percival 	if (error)
148948a1ceedSColin Percival 		error = xs_scanf(XST_NIL, xenbus_get_node(dev),
149048a1ceedSColin Percival 		    "virtual-device-ext", NULL, "%" PRIu32, &vdevice);
1491fac3fd80SJustin T. Gibbs 	if (error) {
1492fac3fd80SJustin T. Gibbs 		xenbus_dev_fatal(dev, error, "reading virtual-device");
1493fac3fd80SJustin T. Gibbs 		device_printf(dev, "Couldn't determine virtual device.\n");
1494fac3fd80SJustin T. Gibbs 		return (error);
1495fac3fd80SJustin T. Gibbs 	}
1496fac3fd80SJustin T. Gibbs 
1497fac3fd80SJustin T. Gibbs 	xbd_vdevice_to_unit(vdevice, &unit, &name);
1498fac3fd80SJustin T. Gibbs 	if (!strcmp(name, "xbd"))
1499fac3fd80SJustin T. Gibbs 		device_set_unit(dev, unit);
1500fac3fd80SJustin T. Gibbs 
1501fac3fd80SJustin T. Gibbs 	sc = device_get_softc(dev);
1502fac3fd80SJustin T. Gibbs 	mtx_init(&sc->xbd_io_lock, "blkfront i/o lock", NULL, MTX_DEF);
1503e2c1fe90SJustin T. Gibbs 	xbd_initqs(sc);
1504fac3fd80SJustin T. Gibbs 	for (i = 0; i < XBD_MAX_RING_PAGES; i++)
1505d9fab01dSJustin T. Gibbs 		sc->xbd_ring_ref[i] = GRANT_REF_INVALID;
1506fac3fd80SJustin T. Gibbs 
1507fac3fd80SJustin T. Gibbs 	sc->xbd_dev = dev;
1508fac3fd80SJustin T. Gibbs 	sc->xbd_vdevice = vdevice;
1509e2c1fe90SJustin T. Gibbs 	sc->xbd_state = XBD_STATE_DISCONNECTED;
1510fac3fd80SJustin T. Gibbs 
1511fac3fd80SJustin T. Gibbs 	xbd_setup_sysctl(sc);
1512fac3fd80SJustin T. Gibbs 
1513fac3fd80SJustin T. Gibbs 	/* Wait for backend device to publish its protocol capabilities. */
1514fac3fd80SJustin T. Gibbs 	xenbus_set_state(dev, XenbusStateInitialising);
1515fac3fd80SJustin T. Gibbs 
1516fac3fd80SJustin T. Gibbs 	return (0);
1517fac3fd80SJustin T. Gibbs }
151889e0f4d2SKip Macy 
151923dc5621SKip Macy static int
152033eebb6aSJustin T. Gibbs xbd_detach(device_t dev)
152189e0f4d2SKip Macy {
152233eebb6aSJustin T. Gibbs 	struct xbd_softc *sc = device_get_softc(dev);
152389e0f4d2SKip Macy 
1524e2c1fe90SJustin T. Gibbs 	DPRINTK("%s: %s removed\n", __func__, xenbus_get_node(dev));
152589e0f4d2SKip Macy 
152633eebb6aSJustin T. Gibbs 	xbd_free(sc);
152733eebb6aSJustin T. Gibbs 	mtx_destroy(&sc->xbd_io_lock);
152889e0f4d2SKip Macy 
152989e0f4d2SKip Macy 	return 0;
153089e0f4d2SKip Macy }
153189e0f4d2SKip Macy 
153289e0f4d2SKip Macy static int
1533fac3fd80SJustin T. Gibbs xbd_suspend(device_t dev)
153489e0f4d2SKip Macy {
1535fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = device_get_softc(dev);
1536fac3fd80SJustin T. Gibbs 	int retval;
1537fac3fd80SJustin T. Gibbs 	int saved_state;
153889e0f4d2SKip Macy 
153989e0f4d2SKip Macy 	/* Prevent new requests being issued until we fix things up. */
154033eebb6aSJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
1541e2c1fe90SJustin T. Gibbs 	saved_state = sc->xbd_state;
1542e2c1fe90SJustin T. Gibbs 	sc->xbd_state = XBD_STATE_SUSPENDED;
1543fac3fd80SJustin T. Gibbs 
1544fac3fd80SJustin T. Gibbs 	/* Wait for outstanding I/O to drain. */
1545fac3fd80SJustin T. Gibbs 	retval = 0;
15469985113bSJustin T. Gibbs 	while (xbd_queue_length(sc, XBD_Q_BUSY) != 0) {
1547e2c1fe90SJustin T. Gibbs 		if (msleep(&sc->xbd_cm_q[XBD_Q_BUSY], &sc->xbd_io_lock,
1548fac3fd80SJustin T. Gibbs 		    PRIBIO, "blkf_susp", 30 * hz) == EWOULDBLOCK) {
1549fac3fd80SJustin T. Gibbs 			retval = EBUSY;
1550fac3fd80SJustin T. Gibbs 			break;
1551fac3fd80SJustin T. Gibbs 		}
1552fac3fd80SJustin T. Gibbs 	}
155333eebb6aSJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
155489e0f4d2SKip Macy 
1555fac3fd80SJustin T. Gibbs 	if (retval != 0)
1556e2c1fe90SJustin T. Gibbs 		sc->xbd_state = saved_state;
155733eebb6aSJustin T. Gibbs 
1558fac3fd80SJustin T. Gibbs 	return (retval);
155989e0f4d2SKip Macy }
156089e0f4d2SKip Macy 
1561ff662b5cSJustin T. Gibbs static int
1562fac3fd80SJustin T. Gibbs xbd_resume(device_t dev)
156389e0f4d2SKip Macy {
1564fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = device_get_softc(dev);
1565fac3fd80SJustin T. Gibbs 
15668dee0e9bSRoger Pau Monné 	if (xen_suspend_cancelled) {
15678dee0e9bSRoger Pau Monné 		sc->xbd_state = XBD_STATE_CONNECTED;
15688dee0e9bSRoger Pau Monné 		return (0);
15698dee0e9bSRoger Pau Monné 	}
15708dee0e9bSRoger Pau Monné 
1571fac3fd80SJustin T. Gibbs 	DPRINTK("xbd_resume: %s\n", xenbus_get_node(dev));
1572fac3fd80SJustin T. Gibbs 
1573fac3fd80SJustin T. Gibbs 	xbd_free(sc);
1574fac3fd80SJustin T. Gibbs 	xbd_initialize(sc);
1575fac3fd80SJustin T. Gibbs 	return (0);
157689e0f4d2SKip Macy }
157789e0f4d2SKip Macy 
1578fac3fd80SJustin T. Gibbs /**
1579fac3fd80SJustin T. Gibbs  * Callback received when the backend's state changes.
1580fac3fd80SJustin T. Gibbs  */
1581fac3fd80SJustin T. Gibbs static void
1582fac3fd80SJustin T. Gibbs xbd_backend_changed(device_t dev, XenbusState backend_state)
1583fac3fd80SJustin T. Gibbs {
1584fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = device_get_softc(dev);
1585fac3fd80SJustin T. Gibbs 
1586fac3fd80SJustin T. Gibbs 	DPRINTK("backend_state=%d\n", backend_state);
1587fac3fd80SJustin T. Gibbs 
1588fac3fd80SJustin T. Gibbs 	switch (backend_state) {
1589fac3fd80SJustin T. Gibbs 	case XenbusStateUnknown:
1590fac3fd80SJustin T. Gibbs 	case XenbusStateInitialising:
1591fac3fd80SJustin T. Gibbs 	case XenbusStateReconfigured:
1592fac3fd80SJustin T. Gibbs 	case XenbusStateReconfiguring:
1593fac3fd80SJustin T. Gibbs 	case XenbusStateClosed:
1594fac3fd80SJustin T. Gibbs 		break;
1595fac3fd80SJustin T. Gibbs 
1596fac3fd80SJustin T. Gibbs 	case XenbusStateInitWait:
1597fac3fd80SJustin T. Gibbs 	case XenbusStateInitialised:
1598fac3fd80SJustin T. Gibbs 		xbd_initialize(sc);
1599fac3fd80SJustin T. Gibbs 		break;
1600fac3fd80SJustin T. Gibbs 
1601fac3fd80SJustin T. Gibbs 	case XenbusStateConnected:
1602fac3fd80SJustin T. Gibbs 		xbd_initialize(sc);
1603fac3fd80SJustin T. Gibbs 		xbd_connect(sc);
1604fac3fd80SJustin T. Gibbs 		break;
1605fac3fd80SJustin T. Gibbs 
1606fac3fd80SJustin T. Gibbs 	case XenbusStateClosing:
1607e5d27b37SRoger Pau Monné 		if (sc->xbd_users > 0) {
1608e5d27b37SRoger Pau Monné 			device_printf(dev, "detaching with pending users\n");
1609e5d27b37SRoger Pau Monné 			KASSERT(sc->xbd_disk != NULL,
1610e5d27b37SRoger Pau Monné 			    ("NULL disk with pending users\n"));
1611e5d27b37SRoger Pau Monné 			disk_gone(sc->xbd_disk);
1612e5d27b37SRoger Pau Monné 		} else {
1613fac3fd80SJustin T. Gibbs 			xbd_closing(dev);
1614e5d27b37SRoger Pau Monné 		}
1615fac3fd80SJustin T. Gibbs 		break;
1616fac3fd80SJustin T. Gibbs 	}
1617fac3fd80SJustin T. Gibbs }
1618fac3fd80SJustin T. Gibbs 
1619fac3fd80SJustin T. Gibbs /*---------------------------- NewBus Registration ---------------------------*/
162033eebb6aSJustin T. Gibbs static device_method_t xbd_methods[] = {
162123dc5621SKip Macy 	/* Device interface */
162233eebb6aSJustin T. Gibbs 	DEVMETHOD(device_probe,         xbd_probe),
162333eebb6aSJustin T. Gibbs 	DEVMETHOD(device_attach,        xbd_attach),
162433eebb6aSJustin T. Gibbs 	DEVMETHOD(device_detach,        xbd_detach),
162523dc5621SKip Macy 	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
162633eebb6aSJustin T. Gibbs 	DEVMETHOD(device_suspend,       xbd_suspend),
162733eebb6aSJustin T. Gibbs 	DEVMETHOD(device_resume,        xbd_resume),
162889e0f4d2SKip Macy 
162923dc5621SKip Macy 	/* Xenbus interface */
163033eebb6aSJustin T. Gibbs 	DEVMETHOD(xenbus_otherend_changed, xbd_backend_changed),
163189e0f4d2SKip Macy 
16323e5e0e2fSElliott Mitchell 	DEVMETHOD_END
163389e0f4d2SKip Macy };
163489e0f4d2SKip Macy 
163533eebb6aSJustin T. Gibbs static driver_t xbd_driver = {
163623dc5621SKip Macy 	"xbd",
163733eebb6aSJustin T. Gibbs 	xbd_methods,
163833eebb6aSJustin T. Gibbs 	sizeof(struct xbd_softc),
163989e0f4d2SKip Macy };
164089e0f4d2SKip Macy 
1641f929eb1eSJohn Baldwin DRIVER_MODULE(xbd, xenbusb_front, xbd_driver, 0, 0);
1642