xref: /freebsd/sys/dev/xen/blkfront/blkfront.c (revision 76acc41fb7c740bc49e2638529b8cc750ff281d5)
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>
52e4808c4bSKip Macy #include <sys/bus_dma.h>
5389e0f4d2SKip Macy 
54*76acc41fSJustin T. Gibbs #include <xen/xen-os.h>
5512678024SDoug Rabson #include <xen/hypervisor.h>
563a6d1fcfSKip Macy #include <xen/xen_intr.h>
5712678024SDoug Rabson #include <xen/gnttab.h>
5889e0f4d2SKip Macy #include <xen/interface/grant_table.h>
5923dc5621SKip Macy #include <xen/interface/io/protocols.h>
6023dc5621SKip Macy #include <xen/xenbus/xenbusvar.h>
6189e0f4d2SKip Macy 
62*76acc41fSJustin T. Gibbs #include <machine/_inttypes.h>
63*76acc41fSJustin T. Gibbs #include <machine/xen/xenvar.h>
64*76acc41fSJustin T. Gibbs 
6589e0f4d2SKip Macy #include <geom/geom_disk.h>
6689e0f4d2SKip Macy 
6789e0f4d2SKip Macy #include <dev/xen/blkfront/block.h>
6889e0f4d2SKip Macy 
6923dc5621SKip Macy #include "xenbus_if.h"
7023dc5621SKip Macy 
71fac3fd80SJustin T. Gibbs /*--------------------------- Forward Declarations ---------------------------*/
7233eebb6aSJustin T. Gibbs static void xbd_closing(device_t);
73fac3fd80SJustin T. Gibbs static void xbd_startio(struct xbd_softc *sc);
7489e0f4d2SKip Macy 
75fac3fd80SJustin T. Gibbs /*---------------------------------- Macros ----------------------------------*/
76fac3fd80SJustin T. Gibbs #if 0
77fac3fd80SJustin T. Gibbs #define DPRINTK(fmt, args...) printf("[XEN] %s:%d: " fmt ".\n", __func__, __LINE__, ##args)
78fac3fd80SJustin T. Gibbs #else
79fac3fd80SJustin T. Gibbs #define DPRINTK(fmt, args...)
80fac3fd80SJustin T. Gibbs #endif
81fac3fd80SJustin T. Gibbs 
82fac3fd80SJustin T. Gibbs #define XBD_SECTOR_SHFT		9
83ff662b5cSJustin T. Gibbs 
84fac3fd80SJustin T. Gibbs /*---------------------------- Global Static Data ----------------------------*/
85fac3fd80SJustin T. Gibbs static MALLOC_DEFINE(M_XENBLOCKFRONT, "xbd", "Xen Block Front driver data");
8689e0f4d2SKip Macy 
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)
140*76acc41fSJustin 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
159fac3fd80SJustin T. Gibbs xbd_queue_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
16089e0f4d2SKip Macy {
161fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc;
162fac3fd80SJustin T. Gibbs 	struct xbd_command *cm;
163fac3fd80SJustin T. Gibbs 	blkif_request_t	*ring_req;
164fac3fd80SJustin T. Gibbs 	struct blkif_request_segment *sg;
165fac3fd80SJustin T. Gibbs 	struct blkif_request_segment *last_block_sg;
166fac3fd80SJustin T. Gibbs 	grant_ref_t *sg_ref;
167fac3fd80SJustin T. Gibbs 	vm_paddr_t buffer_ma;
168fac3fd80SJustin T. Gibbs 	uint64_t fsect, lsect;
169fac3fd80SJustin T. Gibbs 	int ref;
170fac3fd80SJustin T. Gibbs 	int op;
171fac3fd80SJustin T. Gibbs 	int block_segs;
17289e0f4d2SKip Macy 
173fac3fd80SJustin T. Gibbs 	cm = arg;
174fac3fd80SJustin T. Gibbs 	sc = cm->cm_sc;
175fac3fd80SJustin T. Gibbs 
176fac3fd80SJustin T. Gibbs 	if (error) {
177fac3fd80SJustin T. Gibbs 		printf("error %d in xbd_queue_cb\n", error);
178fac3fd80SJustin T. Gibbs 		cm->cm_bp->bio_error = EIO;
179fac3fd80SJustin T. Gibbs 		biodone(cm->cm_bp);
180fac3fd80SJustin T. Gibbs 		xbd_free_command(cm);
18189e0f4d2SKip Macy 		return;
18289e0f4d2SKip Macy 	}
18389e0f4d2SKip Macy 
184fac3fd80SJustin T. Gibbs 	/* Fill out a communications ring structure. */
185fac3fd80SJustin T. Gibbs 	ring_req = RING_GET_REQUEST(&sc->xbd_ring, sc->xbd_ring.req_prod_pvt);
186fac3fd80SJustin T. Gibbs 	sc->xbd_ring.req_prod_pvt++;
187fac3fd80SJustin T. Gibbs 	ring_req->id = cm->cm_id;
188fac3fd80SJustin T. Gibbs 	ring_req->operation = cm->cm_operation;
189fac3fd80SJustin T. Gibbs 	ring_req->sector_number = cm->cm_sector_number;
190fac3fd80SJustin T. Gibbs 	ring_req->handle = (blkif_vdev_t)(uintptr_t)sc->xbd_disk;
191fac3fd80SJustin T. Gibbs 	ring_req->nr_segments = nsegs;
192fac3fd80SJustin T. Gibbs 	cm->cm_nseg = nsegs;
193fac3fd80SJustin T. Gibbs 
194fac3fd80SJustin T. Gibbs 	block_segs    = MIN(nsegs, BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK);
195fac3fd80SJustin T. Gibbs 	sg            = ring_req->seg;
196fac3fd80SJustin T. Gibbs 	last_block_sg = sg + block_segs;
197fac3fd80SJustin T. Gibbs 	sg_ref        = cm->cm_sg_refs;
198fac3fd80SJustin T. Gibbs 
199fac3fd80SJustin T. Gibbs 	while (1) {
200fac3fd80SJustin T. Gibbs 
201fac3fd80SJustin T. Gibbs 		while (sg < last_block_sg) {
202fac3fd80SJustin T. Gibbs 			buffer_ma = segs->ds_addr;
203fac3fd80SJustin T. Gibbs 			fsect = (buffer_ma & PAGE_MASK) >> XBD_SECTOR_SHFT;
204fac3fd80SJustin T. Gibbs 			lsect = fsect + (segs->ds_len  >> XBD_SECTOR_SHFT) - 1;
205fac3fd80SJustin T. Gibbs 
206fac3fd80SJustin T. Gibbs 			KASSERT(lsect <= 7, ("XEN disk driver data cannot "
207fac3fd80SJustin T. Gibbs 			    "cross a page boundary"));
208fac3fd80SJustin T. Gibbs 
209fac3fd80SJustin T. Gibbs 			/* install a grant reference. */
210fac3fd80SJustin T. Gibbs 			ref = gnttab_claim_grant_reference(&cm->cm_gref_head);
211fac3fd80SJustin T. Gibbs 
212e4808c4bSKip Macy 			/*
213fac3fd80SJustin T. Gibbs 			 * GNTTAB_LIST_END == 0xffffffff, but it is private
214fac3fd80SJustin T. Gibbs 			 * to gnttab.c.
215e4808c4bSKip Macy 			 */
216fac3fd80SJustin T. Gibbs 			KASSERT(ref != ~0, ("grant_reference failed"));
217fac3fd80SJustin T. Gibbs 
218fac3fd80SJustin T. Gibbs 			gnttab_grant_foreign_access_ref(
219fac3fd80SJustin T. Gibbs 			    ref,
220fac3fd80SJustin T. Gibbs 			    xenbus_get_otherend_id(sc->xbd_dev),
221fac3fd80SJustin T. Gibbs 			    buffer_ma >> PAGE_SHIFT,
222fac3fd80SJustin T. Gibbs 			    ring_req->operation == BLKIF_OP_WRITE);
223fac3fd80SJustin T. Gibbs 
224fac3fd80SJustin T. Gibbs 			*sg_ref = ref;
225fac3fd80SJustin T. Gibbs 			*sg = (struct blkif_request_segment) {
226fac3fd80SJustin T. Gibbs 				.gref       = ref,
227fac3fd80SJustin T. Gibbs 				.first_sect = fsect,
228fac3fd80SJustin T. Gibbs 				.last_sect  = lsect
229fac3fd80SJustin T. Gibbs 			};
230fac3fd80SJustin T. Gibbs 			sg++;
231fac3fd80SJustin T. Gibbs 			sg_ref++;
232fac3fd80SJustin T. Gibbs 			segs++;
233fac3fd80SJustin T. Gibbs 			nsegs--;
234fac3fd80SJustin T. Gibbs 		}
235fac3fd80SJustin T. Gibbs 		block_segs = MIN(nsegs, BLKIF_MAX_SEGMENTS_PER_SEGMENT_BLOCK);
236fac3fd80SJustin T. Gibbs 		if (block_segs == 0)
237fac3fd80SJustin T. Gibbs 			break;
238fac3fd80SJustin T. Gibbs 
239fac3fd80SJustin T. Gibbs 		sg = BLKRING_GET_SEG_BLOCK(&sc->xbd_ring,
240fac3fd80SJustin T. Gibbs 		    sc->xbd_ring.req_prod_pvt);
241fac3fd80SJustin T. Gibbs 		sc->xbd_ring.req_prod_pvt++;
242fac3fd80SJustin T. Gibbs 		last_block_sg = sg + block_segs;
243fac3fd80SJustin T. Gibbs 	}
244fac3fd80SJustin T. Gibbs 
245fac3fd80SJustin T. Gibbs 	if (cm->cm_operation == BLKIF_OP_READ)
246fac3fd80SJustin T. Gibbs 		op = BUS_DMASYNC_PREREAD;
247fac3fd80SJustin T. Gibbs 	else if (cm->cm_operation == BLKIF_OP_WRITE)
248fac3fd80SJustin T. Gibbs 		op = BUS_DMASYNC_PREWRITE;
249fac3fd80SJustin T. Gibbs 	else
250fac3fd80SJustin T. Gibbs 		op = 0;
251fac3fd80SJustin T. Gibbs 	bus_dmamap_sync(sc->xbd_io_dmat, cm->cm_map, op);
252fac3fd80SJustin T. Gibbs 
253fac3fd80SJustin T. Gibbs 	gnttab_free_grant_references(cm->cm_gref_head);
254fac3fd80SJustin T. Gibbs 
255e2c1fe90SJustin T. Gibbs 	xbd_enqueue_cm(cm, XBD_Q_BUSY);
256fac3fd80SJustin T. Gibbs 
257fac3fd80SJustin T. Gibbs 	/*
258127a9483SJustin T. Gibbs 	 * If bus dma had to asynchronously call us back to dispatch
259127a9483SJustin T. Gibbs 	 * this command, we are no longer executing in the context of
260127a9483SJustin T. Gibbs 	 * xbd_startio().  Thus we cannot rely on xbd_startio()'s call to
261127a9483SJustin T. Gibbs 	 * xbd_flush_requests() to publish this command to the backend
262127a9483SJustin T. Gibbs 	 * along with any other commands that it could batch.
263fac3fd80SJustin T. Gibbs 	 */
264127a9483SJustin T. Gibbs 	if ((cm->cm_flags & XBDCF_ASYNC_MAPPING) != 0)
265cdf5d66fSJustin T. Gibbs 		xbd_flush_requests(sc);
266fac3fd80SJustin T. Gibbs 
267fac3fd80SJustin T. Gibbs 	return;
268fac3fd80SJustin T. Gibbs }
269fac3fd80SJustin T. Gibbs 
270fac3fd80SJustin T. Gibbs static int
271fac3fd80SJustin T. Gibbs xbd_queue_request(struct xbd_softc *sc, struct xbd_command *cm)
272fac3fd80SJustin T. Gibbs {
273fac3fd80SJustin T. Gibbs 	int error;
274fac3fd80SJustin T. Gibbs 
275fac3fd80SJustin T. Gibbs 	error = bus_dmamap_load(sc->xbd_io_dmat, cm->cm_map, cm->cm_data,
276fac3fd80SJustin T. Gibbs 	    cm->cm_datalen, xbd_queue_cb, cm, 0);
277fac3fd80SJustin T. Gibbs 	if (error == EINPROGRESS) {
278127a9483SJustin T. Gibbs 		/*
279127a9483SJustin T. Gibbs 		 * Maintain queuing order by freezing the queue.  The next
280127a9483SJustin T. Gibbs 		 * command may not require as many resources as the command
281127a9483SJustin T. Gibbs 		 * we just attempted to map, so we can't rely on bus dma
282127a9483SJustin T. Gibbs 		 * blocking for it too.
283127a9483SJustin T. Gibbs 		 */
2849985113bSJustin T. Gibbs 		xbd_cm_freeze(sc, cm, XBDCF_ASYNC_MAPPING);
285fac3fd80SJustin T. Gibbs 		return (0);
286fac3fd80SJustin T. Gibbs 	}
287fac3fd80SJustin T. Gibbs 
288fac3fd80SJustin T. Gibbs 	return (error);
289fac3fd80SJustin T. Gibbs }
290fac3fd80SJustin T. Gibbs 
291fac3fd80SJustin T. Gibbs static void
292fac3fd80SJustin T. Gibbs xbd_restart_queue_callback(void *arg)
293fac3fd80SJustin T. Gibbs {
294fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = arg;
295fac3fd80SJustin T. Gibbs 
29633eebb6aSJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
297e4808c4bSKip Macy 
298127a9483SJustin T. Gibbs 	xbd_thaw(sc, XBDF_GNT_SHORTAGE);
299127a9483SJustin T. Gibbs 
30033eebb6aSJustin T. Gibbs 	xbd_startio(sc);
301e4808c4bSKip Macy 
30233eebb6aSJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
303fac3fd80SJustin T. Gibbs }
304fac3fd80SJustin T. Gibbs 
305fac3fd80SJustin T. Gibbs static struct xbd_command *
306fac3fd80SJustin T. Gibbs xbd_bio_command(struct xbd_softc *sc)
307fac3fd80SJustin T. Gibbs {
308fac3fd80SJustin T. Gibbs 	struct xbd_command *cm;
309fac3fd80SJustin T. Gibbs 	struct bio *bp;
310fac3fd80SJustin T. Gibbs 
311*76acc41fSJustin T. Gibbs 	if (__predict_false(sc->xbd_state != XBD_STATE_CONNECTED))
312fac3fd80SJustin T. Gibbs 		return (NULL);
313fac3fd80SJustin T. Gibbs 
314fac3fd80SJustin T. Gibbs 	bp = xbd_dequeue_bio(sc);
315fac3fd80SJustin T. Gibbs 	if (bp == NULL)
316fac3fd80SJustin T. Gibbs 		return (NULL);
317fac3fd80SJustin T. Gibbs 
318e2c1fe90SJustin T. Gibbs 	if ((cm = xbd_dequeue_cm(sc, XBD_Q_FREE)) == NULL) {
319127a9483SJustin T. Gibbs 		xbd_freeze(sc, XBDF_CM_SHORTAGE);
320fac3fd80SJustin T. Gibbs 		xbd_requeue_bio(sc, bp);
321fac3fd80SJustin T. Gibbs 		return (NULL);
322fac3fd80SJustin T. Gibbs 	}
323fac3fd80SJustin T. Gibbs 
324fac3fd80SJustin T. Gibbs 	if (gnttab_alloc_grant_references(sc->xbd_max_request_segments,
325fac3fd80SJustin T. Gibbs 	    &cm->cm_gref_head) != 0) {
326fac3fd80SJustin T. Gibbs 		gnttab_request_free_callback(&sc->xbd_callback,
327fac3fd80SJustin T. Gibbs 		    xbd_restart_queue_callback, sc,
328fac3fd80SJustin T. Gibbs 		    sc->xbd_max_request_segments);
329127a9483SJustin T. Gibbs 		xbd_freeze(sc, XBDF_GNT_SHORTAGE);
330fac3fd80SJustin T. Gibbs 		xbd_requeue_bio(sc, bp);
331e2c1fe90SJustin T. Gibbs 		xbd_enqueue_cm(cm, XBD_Q_FREE);
332fac3fd80SJustin T. Gibbs 		return (NULL);
333fac3fd80SJustin T. Gibbs 	}
334fac3fd80SJustin T. Gibbs 
335fac3fd80SJustin T. Gibbs 	cm->cm_bp = bp;
336fac3fd80SJustin T. Gibbs 	cm->cm_data = bp->bio_data;
337fac3fd80SJustin T. Gibbs 	cm->cm_datalen = bp->bio_bcount;
338fac3fd80SJustin T. Gibbs 	cm->cm_sector_number = (blkif_sector_t)bp->bio_pblkno;
339fac3fd80SJustin T. Gibbs 
3409985113bSJustin T. Gibbs 	switch (bp->bio_cmd) {
3419985113bSJustin T. Gibbs 	case BIO_READ:
3429985113bSJustin T. Gibbs 		cm->cm_operation = BLKIF_OP_READ;
3439985113bSJustin T. Gibbs 		break;
3449985113bSJustin T. Gibbs 	case BIO_WRITE:
3459985113bSJustin T. Gibbs 		cm->cm_operation = BLKIF_OP_WRITE;
3469985113bSJustin T. Gibbs 		if ((bp->bio_flags & BIO_ORDERED) != 0) {
3479985113bSJustin T. Gibbs 			if ((sc->xbd_flags & XBDF_BARRIER) != 0) {
3489985113bSJustin T. Gibbs 				cm->cm_operation = BLKIF_OP_WRITE_BARRIER;
3499985113bSJustin T. Gibbs 			} else {
3509985113bSJustin T. Gibbs 				/*
3519985113bSJustin T. Gibbs 				 * Single step this command.
3529985113bSJustin T. Gibbs 				 */
3539985113bSJustin T. Gibbs 				cm->cm_flags |= XBDCF_Q_FREEZE;
3549985113bSJustin T. Gibbs 				if (xbd_queue_length(sc, XBD_Q_BUSY) != 0) {
3559985113bSJustin T. Gibbs 					/*
3569985113bSJustin T. Gibbs 					 * Wait for in-flight requests to
3579985113bSJustin T. Gibbs 					 * finish.
3589985113bSJustin T. Gibbs 					 */
3599985113bSJustin T. Gibbs 					xbd_freeze(sc, XBDF_WAIT_IDLE);
3609985113bSJustin T. Gibbs 					xbd_requeue_cm(cm, XBD_Q_READY);
3619985113bSJustin T. Gibbs 					return (NULL);
3629985113bSJustin T. Gibbs 				}
3639985113bSJustin T. Gibbs 			}
3649985113bSJustin T. Gibbs 		}
3659985113bSJustin T. Gibbs 		break;
3669985113bSJustin T. Gibbs 	case BIO_FLUSH:
3679985113bSJustin T. Gibbs 		if ((sc->xbd_flags & XBDF_FLUSH) != 0)
3689985113bSJustin T. Gibbs 			cm->cm_operation = BLKIF_OP_FLUSH_DISKCACHE;
3699985113bSJustin T. Gibbs 		else if ((sc->xbd_flags & XBDF_BARRIER) != 0)
3709985113bSJustin T. Gibbs 			cm->cm_operation = BLKIF_OP_WRITE_BARRIER;
3719985113bSJustin T. Gibbs 		else
3729985113bSJustin T. Gibbs 			panic("flush request, but no flush support available");
3739985113bSJustin T. Gibbs 		break;
3749985113bSJustin T. Gibbs 	default:
3759985113bSJustin T. Gibbs 		panic("unknown bio command %d", bp->bio_cmd);
3769985113bSJustin T. Gibbs 	}
3779985113bSJustin T. Gibbs 
378fac3fd80SJustin T. Gibbs 	return (cm);
379fac3fd80SJustin T. Gibbs }
380fac3fd80SJustin T. Gibbs 
381fac3fd80SJustin T. Gibbs /*
382fac3fd80SJustin T. Gibbs  * Dequeue buffers and place them in the shared communication ring.
383fac3fd80SJustin T. Gibbs  * Return when no more requests can be accepted or all buffers have
384fac3fd80SJustin T. Gibbs  * been queued.
385fac3fd80SJustin T. Gibbs  *
386fac3fd80SJustin T. Gibbs  * Signal XEN once the ring has been filled out.
387fac3fd80SJustin T. Gibbs  */
388fac3fd80SJustin T. Gibbs static void
389fac3fd80SJustin T. Gibbs xbd_startio(struct xbd_softc *sc)
390fac3fd80SJustin T. Gibbs {
391fac3fd80SJustin T. Gibbs 	struct xbd_command *cm;
392fac3fd80SJustin T. Gibbs 	int error, queued = 0;
393fac3fd80SJustin T. Gibbs 
394fac3fd80SJustin T. Gibbs 	mtx_assert(&sc->xbd_io_lock, MA_OWNED);
395fac3fd80SJustin T. Gibbs 
396e2c1fe90SJustin T. Gibbs 	if (sc->xbd_state != XBD_STATE_CONNECTED)
397e4808c4bSKip Macy 		return;
398fac3fd80SJustin T. Gibbs 
399fac3fd80SJustin T. Gibbs 	while (RING_FREE_REQUESTS(&sc->xbd_ring) >=
400fac3fd80SJustin T. Gibbs 	    sc->xbd_max_request_blocks) {
401127a9483SJustin T. Gibbs 		if (sc->xbd_qfrozen_cnt != 0)
402fac3fd80SJustin T. Gibbs 			break;
403fac3fd80SJustin T. Gibbs 
404e2c1fe90SJustin T. Gibbs 		cm = xbd_dequeue_cm(sc, XBD_Q_READY);
405fac3fd80SJustin T. Gibbs 
406fac3fd80SJustin T. Gibbs 		if (cm == NULL)
407fac3fd80SJustin T. Gibbs 		    cm = xbd_bio_command(sc);
408fac3fd80SJustin T. Gibbs 
409fac3fd80SJustin T. Gibbs 		if (cm == NULL)
410fac3fd80SJustin T. Gibbs 			break;
411fac3fd80SJustin T. Gibbs 
4129985113bSJustin T. Gibbs 		if ((cm->cm_flags & XBDCF_Q_FREEZE) != 0) {
4139985113bSJustin T. Gibbs 			/*
4149985113bSJustin T. Gibbs 			 * Single step command.  Future work is
4159985113bSJustin T. Gibbs 			 * held off until this command completes.
4169985113bSJustin T. Gibbs 			 */
4179985113bSJustin T. Gibbs 			xbd_cm_freeze(sc, cm, XBDCF_Q_FREEZE);
4189985113bSJustin T. Gibbs 		}
4199985113bSJustin T. Gibbs 
420fac3fd80SJustin T. Gibbs 		if ((error = xbd_queue_request(sc, cm)) != 0) {
421fac3fd80SJustin T. Gibbs 			printf("xbd_queue_request returned %d\n", error);
422fac3fd80SJustin T. Gibbs 			break;
423fac3fd80SJustin T. Gibbs 		}
424fac3fd80SJustin T. Gibbs 		queued++;
425fac3fd80SJustin T. Gibbs 	}
426fac3fd80SJustin T. Gibbs 
427fac3fd80SJustin T. Gibbs 	if (queued != 0)
428cdf5d66fSJustin T. Gibbs 		xbd_flush_requests(sc);
429e4808c4bSKip Macy }
430e4808c4bSKip Macy 
431e4808c4bSKip Macy static void
43233eebb6aSJustin T. Gibbs xbd_bio_complete(struct xbd_softc *sc, struct xbd_command *cm)
433e4808c4bSKip Macy {
434e4808c4bSKip Macy 	struct bio *bp;
435e4808c4bSKip Macy 
43633eebb6aSJustin T. Gibbs 	bp = cm->cm_bp;
437e4808c4bSKip Macy 
438*76acc41fSJustin T. Gibbs 	if (__predict_false(cm->cm_status != BLKIF_RSP_OKAY)) {
439e4808c4bSKip Macy 		disk_err(bp, "disk error" , -1, 0);
44033eebb6aSJustin T. Gibbs 		printf(" status: %x\n", cm->cm_status);
441e4808c4bSKip Macy 		bp->bio_flags |= BIO_ERROR;
442e4808c4bSKip Macy 	}
443e4808c4bSKip Macy 
444e4808c4bSKip Macy 	if (bp->bio_flags & BIO_ERROR)
445e4808c4bSKip Macy 		bp->bio_error = EIO;
446e4808c4bSKip Macy 	else
447e4808c4bSKip Macy 		bp->bio_resid = 0;
448e4808c4bSKip Macy 
44933eebb6aSJustin T. Gibbs 	xbd_free_command(cm);
450e4808c4bSKip Macy 	biodone(bp);
451e4808c4bSKip Macy }
452e4808c4bSKip Macy 
453fac3fd80SJustin T. Gibbs static int
454fac3fd80SJustin T. Gibbs xbd_completion(struct xbd_command *cm)
455fac3fd80SJustin T. Gibbs {
456fac3fd80SJustin T. Gibbs 	gnttab_end_foreign_access_references(cm->cm_nseg, cm->cm_sg_refs);
457fac3fd80SJustin T. Gibbs 	return (BLKIF_SEGS_TO_BLOCKS(cm->cm_nseg));
458fac3fd80SJustin T. Gibbs }
459fac3fd80SJustin T. Gibbs 
460fac3fd80SJustin T. Gibbs static void
461fac3fd80SJustin T. Gibbs xbd_int(void *xsc)
462fac3fd80SJustin T. Gibbs {
463fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = xsc;
464fac3fd80SJustin T. Gibbs 	struct xbd_command *cm;
465fac3fd80SJustin T. Gibbs 	blkif_response_t *bret;
466fac3fd80SJustin T. Gibbs 	RING_IDX i, rp;
467fac3fd80SJustin T. Gibbs 	int op;
468fac3fd80SJustin T. Gibbs 
469fac3fd80SJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
470fac3fd80SJustin T. Gibbs 
471*76acc41fSJustin T. Gibbs 	if (__predict_false(sc->xbd_state == XBD_STATE_DISCONNECTED)) {
472fac3fd80SJustin T. Gibbs 		mtx_unlock(&sc->xbd_io_lock);
473fac3fd80SJustin T. Gibbs 		return;
474fac3fd80SJustin T. Gibbs 	}
475fac3fd80SJustin T. Gibbs 
476fac3fd80SJustin T. Gibbs  again:
477fac3fd80SJustin T. Gibbs 	rp = sc->xbd_ring.sring->rsp_prod;
478fac3fd80SJustin T. Gibbs 	rmb(); /* Ensure we see queued responses up to 'rp'. */
479fac3fd80SJustin T. Gibbs 
480fac3fd80SJustin T. Gibbs 	for (i = sc->xbd_ring.rsp_cons; i != rp;) {
481fac3fd80SJustin T. Gibbs 		bret = RING_GET_RESPONSE(&sc->xbd_ring, i);
482fac3fd80SJustin T. Gibbs 		cm   = &sc->xbd_shadow[bret->id];
483fac3fd80SJustin T. Gibbs 
484e2c1fe90SJustin T. Gibbs 		xbd_remove_cm(cm, XBD_Q_BUSY);
485fac3fd80SJustin T. Gibbs 		i += xbd_completion(cm);
486fac3fd80SJustin T. Gibbs 
487fac3fd80SJustin T. Gibbs 		if (cm->cm_operation == BLKIF_OP_READ)
488fac3fd80SJustin T. Gibbs 			op = BUS_DMASYNC_POSTREAD;
4899985113bSJustin T. Gibbs 		else if (cm->cm_operation == BLKIF_OP_WRITE ||
4909985113bSJustin T. Gibbs 		    cm->cm_operation == BLKIF_OP_WRITE_BARRIER)
491fac3fd80SJustin T. Gibbs 			op = BUS_DMASYNC_POSTWRITE;
492fac3fd80SJustin T. Gibbs 		else
493fac3fd80SJustin T. Gibbs 			op = 0;
494fac3fd80SJustin T. Gibbs 		bus_dmamap_sync(sc->xbd_io_dmat, cm->cm_map, op);
495fac3fd80SJustin T. Gibbs 		bus_dmamap_unload(sc->xbd_io_dmat, cm->cm_map);
496fac3fd80SJustin T. Gibbs 
497fac3fd80SJustin T. Gibbs 		/*
498127a9483SJustin T. Gibbs 		 * Release any hold this command has on future command
499127a9483SJustin T. Gibbs 		 * dispatch.
500fac3fd80SJustin T. Gibbs 		 */
5019985113bSJustin T. Gibbs 		xbd_cm_thaw(sc, cm);
502fac3fd80SJustin T. Gibbs 
503fac3fd80SJustin T. Gibbs 		/*
504fac3fd80SJustin T. Gibbs 		 * Directly call the i/o complete routine to save an
505fac3fd80SJustin T. Gibbs 		 * an indirection in the common case.
506fac3fd80SJustin T. Gibbs 		 */
507fac3fd80SJustin T. Gibbs 		cm->cm_status = bret->status;
508fac3fd80SJustin T. Gibbs 		if (cm->cm_bp)
509fac3fd80SJustin T. Gibbs 			xbd_bio_complete(sc, cm);
510fac3fd80SJustin T. Gibbs 		else if (cm->cm_complete != NULL)
511fac3fd80SJustin T. Gibbs 			cm->cm_complete(cm);
512fac3fd80SJustin T. Gibbs 		else
513fac3fd80SJustin T. Gibbs 			xbd_free_command(cm);
514fac3fd80SJustin T. Gibbs 	}
515fac3fd80SJustin T. Gibbs 
516fac3fd80SJustin T. Gibbs 	sc->xbd_ring.rsp_cons = i;
517fac3fd80SJustin T. Gibbs 
518fac3fd80SJustin T. Gibbs 	if (i != sc->xbd_ring.req_prod_pvt) {
519fac3fd80SJustin T. Gibbs 		int more_to_do;
520fac3fd80SJustin T. Gibbs 		RING_FINAL_CHECK_FOR_RESPONSES(&sc->xbd_ring, more_to_do);
521fac3fd80SJustin T. Gibbs 		if (more_to_do)
522fac3fd80SJustin T. Gibbs 			goto again;
523fac3fd80SJustin T. Gibbs 	} else {
524fac3fd80SJustin T. Gibbs 		sc->xbd_ring.sring->rsp_event = i + 1;
525fac3fd80SJustin T. Gibbs 	}
526fac3fd80SJustin T. Gibbs 
5279985113bSJustin T. Gibbs 	if (xbd_queue_length(sc, XBD_Q_BUSY) == 0)
5289985113bSJustin T. Gibbs 		xbd_thaw(sc, XBDF_WAIT_IDLE);
5299985113bSJustin T. Gibbs 
530fac3fd80SJustin T. Gibbs 	xbd_startio(sc);
531fac3fd80SJustin T. Gibbs 
532*76acc41fSJustin T. Gibbs 	if (__predict_false(sc->xbd_state == XBD_STATE_SUSPENDED))
533e2c1fe90SJustin T. Gibbs 		wakeup(&sc->xbd_cm_q[XBD_Q_BUSY]);
534fac3fd80SJustin T. Gibbs 
535fac3fd80SJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
536fac3fd80SJustin T. Gibbs }
537fac3fd80SJustin T. Gibbs 
538fac3fd80SJustin T. Gibbs /*------------------------------- Dump Support -------------------------------*/
539fac3fd80SJustin T. Gibbs /**
540fac3fd80SJustin T. Gibbs  * Quiesce the disk writes for a dump file before allowing the next buffer.
541fac3fd80SJustin T. Gibbs  */
5428698b76cSKip Macy static void
54333eebb6aSJustin T. Gibbs xbd_quiesce(struct xbd_softc *sc)
5448698b76cSKip Macy {
5458698b76cSKip Macy 	int mtd;
5468698b76cSKip Macy 
5478698b76cSKip Macy 	// While there are outstanding requests
5489985113bSJustin T. Gibbs 	while (xbd_queue_length(sc, XBD_Q_BUSY) != 0) {
54933eebb6aSJustin T. Gibbs 		RING_FINAL_CHECK_FOR_RESPONSES(&sc->xbd_ring, mtd);
5508698b76cSKip Macy 		if (mtd) {
551e4808c4bSKip Macy 			/* Recieved request completions, update queue. */
55233eebb6aSJustin T. Gibbs 			xbd_int(sc);
5538698b76cSKip Macy 		}
5549985113bSJustin T. Gibbs 		if (xbd_queue_length(sc, XBD_Q_BUSY) != 0) {
555e4808c4bSKip Macy 			/*
556e4808c4bSKip Macy 			 * Still pending requests, wait for the disk i/o
557e4808c4bSKip Macy 			 * to complete.
558e4808c4bSKip Macy 			 */
559be7747b4SKip Macy 			HYPERVISOR_yield();
5608698b76cSKip Macy 		}
5618698b76cSKip Macy 	}
5628698b76cSKip Macy }
5638698b76cSKip Macy 
564e4808c4bSKip Macy /* Kernel dump function for a paravirtualized disk device */
565e4808c4bSKip Macy static void
56633eebb6aSJustin T. Gibbs xbd_dump_complete(struct xbd_command *cm)
567e4808c4bSKip Macy {
5688698b76cSKip Macy 
569e2c1fe90SJustin T. Gibbs 	xbd_enqueue_cm(cm, XBD_Q_COMPLETE);
570e4808c4bSKip Macy }
571e4808c4bSKip Macy 
5728698b76cSKip Macy static int
57333eebb6aSJustin T. Gibbs xbd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset,
5748698b76cSKip Macy     size_t length)
5758698b76cSKip Macy {
5768698b76cSKip Macy 	struct disk *dp = arg;
57733eebb6aSJustin T. Gibbs 	struct xbd_softc *sc = dp->d_drv1;
57833eebb6aSJustin T. Gibbs 	struct xbd_command *cm;
579e4808c4bSKip Macy 	size_t chunk;
580e4808c4bSKip Macy 	int sbp;
5818698b76cSKip Macy 	int rc = 0;
5828698b76cSKip Macy 
583e4808c4bSKip Macy 	if (length <= 0)
584e4808c4bSKip Macy 		return (rc);
5858698b76cSKip Macy 
58633eebb6aSJustin T. Gibbs 	xbd_quiesce(sc);	/* All quiet on the western front. */
587e4808c4bSKip Macy 
588e4808c4bSKip Macy 	/*
589e4808c4bSKip Macy 	 * If this lock is held, then this module is failing, and a
590e4808c4bSKip Macy 	 * successful kernel dump is highly unlikely anyway.
591e4808c4bSKip Macy 	 */
59233eebb6aSJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
593e4808c4bSKip Macy 
594e4808c4bSKip Macy 	/* Split the 64KB block as needed */
595e4808c4bSKip Macy 	for (sbp=0; length > 0; sbp++) {
596e2c1fe90SJustin T. Gibbs 		cm = xbd_dequeue_cm(sc, XBD_Q_FREE);
597e4808c4bSKip Macy 		if (cm == NULL) {
59833eebb6aSJustin T. Gibbs 			mtx_unlock(&sc->xbd_io_lock);
59933eebb6aSJustin T. Gibbs 			device_printf(sc->xbd_dev, "dump: no more commands?\n");
600e4808c4bSKip Macy 			return (EBUSY);
601e4808c4bSKip Macy 		}
602e4808c4bSKip Macy 
60333eebb6aSJustin T. Gibbs 		if (gnttab_alloc_grant_references(sc->xbd_max_request_segments,
60433eebb6aSJustin T. Gibbs 		    &cm->cm_gref_head) != 0) {
60533eebb6aSJustin T. Gibbs 			xbd_free_command(cm);
60633eebb6aSJustin T. Gibbs 			mtx_unlock(&sc->xbd_io_lock);
60733eebb6aSJustin T. Gibbs 			device_printf(sc->xbd_dev, "no more grant allocs?\n");
608e4808c4bSKip Macy 			return (EBUSY);
609e4808c4bSKip Macy 		}
610e4808c4bSKip Macy 
61133eebb6aSJustin T. Gibbs 		chunk = length > sc->xbd_max_request_size ?
61233eebb6aSJustin T. Gibbs 		    sc->xbd_max_request_size : length;
61333eebb6aSJustin T. Gibbs 		cm->cm_data = virtual;
61433eebb6aSJustin T. Gibbs 		cm->cm_datalen = chunk;
61533eebb6aSJustin T. Gibbs 		cm->cm_operation = BLKIF_OP_WRITE;
61633eebb6aSJustin T. Gibbs 		cm->cm_sector_number = offset / dp->d_sectorsize;
61733eebb6aSJustin T. Gibbs 		cm->cm_complete = xbd_dump_complete;
618e4808c4bSKip Macy 
619e2c1fe90SJustin T. Gibbs 		xbd_enqueue_cm(cm, XBD_Q_READY);
6208698b76cSKip Macy 
6218698b76cSKip Macy 		length -= chunk;
6228698b76cSKip Macy 		offset += chunk;
6238698b76cSKip Macy 		virtual = (char *) virtual + chunk;
6248698b76cSKip Macy 	}
6258698b76cSKip Macy 
626e4808c4bSKip Macy 	/* Tell DOM0 to do the I/O */
62733eebb6aSJustin T. Gibbs 	xbd_startio(sc);
62833eebb6aSJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
629e4808c4bSKip Macy 
630e4808c4bSKip Macy 	/* Poll for the completion. */
63133eebb6aSJustin T. Gibbs 	xbd_quiesce(sc);	/* All quite on the eastern front */
632e4808c4bSKip Macy 
633e4808c4bSKip Macy 	/* If there were any errors, bail out... */
634e2c1fe90SJustin T. Gibbs 	while ((cm = xbd_dequeue_cm(sc, XBD_Q_COMPLETE)) != NULL) {
63533eebb6aSJustin T. Gibbs 		if (cm->cm_status != BLKIF_RSP_OKAY) {
63633eebb6aSJustin T. Gibbs 			device_printf(sc->xbd_dev,
637e4808c4bSKip Macy 			    "Dump I/O failed at sector %jd\n",
63833eebb6aSJustin T. Gibbs 			    cm->cm_sector_number);
639e4808c4bSKip Macy 			rc = EIO;
6408698b76cSKip Macy 		}
64133eebb6aSJustin T. Gibbs 		xbd_free_command(cm);
6428698b76cSKip Macy 	}
643e4808c4bSKip Macy 
6448698b76cSKip Macy 	return (rc);
6458698b76cSKip Macy }
6468698b76cSKip Macy 
647fac3fd80SJustin T. Gibbs /*----------------------------- Disk Entrypoints -----------------------------*/
64823dc5621SKip Macy static int
649fac3fd80SJustin T. Gibbs xbd_open(struct disk *dp)
65089e0f4d2SKip Macy {
651fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = dp->d_drv1;
65223dc5621SKip Macy 
653fac3fd80SJustin T. Gibbs 	if (sc == NULL) {
654fac3fd80SJustin T. Gibbs 		printf("xb%d: not found", sc->xbd_unit);
65523dc5621SKip Macy 		return (ENXIO);
65623dc5621SKip Macy 	}
65723dc5621SKip Macy 
658e2c1fe90SJustin T. Gibbs 	sc->xbd_flags |= XBDF_OPEN;
659fac3fd80SJustin T. Gibbs 	sc->xbd_users++;
660fac3fd80SJustin T. Gibbs 	return (0);
661fac3fd80SJustin T. Gibbs }
662fac3fd80SJustin T. Gibbs 
663fac3fd80SJustin T. Gibbs static int
664fac3fd80SJustin T. Gibbs xbd_close(struct disk *dp)
665fac3fd80SJustin T. Gibbs {
666fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = dp->d_drv1;
667fac3fd80SJustin T. Gibbs 
668fac3fd80SJustin T. Gibbs 	if (sc == NULL)
669fac3fd80SJustin T. Gibbs 		return (ENXIO);
670e2c1fe90SJustin T. Gibbs 	sc->xbd_flags &= ~XBDF_OPEN;
671fac3fd80SJustin T. Gibbs 	if (--(sc->xbd_users) == 0) {
672fac3fd80SJustin T. Gibbs 		/*
673fac3fd80SJustin T. Gibbs 		 * Check whether we have been instructed to close.  We will
674fac3fd80SJustin T. Gibbs 		 * have ignored this request initially, as the device was
675fac3fd80SJustin T. Gibbs 		 * still mounted.
676fac3fd80SJustin T. Gibbs 		 */
677fac3fd80SJustin T. Gibbs 		if (xenbus_get_otherend_state(sc->xbd_dev) ==
678fac3fd80SJustin T. Gibbs 		    XenbusStateClosing)
679fac3fd80SJustin T. Gibbs 			xbd_closing(sc->xbd_dev);
680fac3fd80SJustin T. Gibbs 	}
681fac3fd80SJustin T. Gibbs 	return (0);
682fac3fd80SJustin T. Gibbs }
683fac3fd80SJustin T. Gibbs 
684fac3fd80SJustin T. Gibbs static int
685fac3fd80SJustin T. Gibbs xbd_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
686fac3fd80SJustin T. Gibbs {
687fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = dp->d_drv1;
688fac3fd80SJustin T. Gibbs 
689fac3fd80SJustin T. Gibbs 	if (sc == NULL)
690fac3fd80SJustin T. Gibbs 		return (ENXIO);
691fac3fd80SJustin T. Gibbs 
692fac3fd80SJustin T. Gibbs 	return (ENOTTY);
693fac3fd80SJustin T. Gibbs }
694fac3fd80SJustin T. Gibbs 
695fac3fd80SJustin T. Gibbs /*
696fac3fd80SJustin T. Gibbs  * Read/write routine for a buffer.  Finds the proper unit, place it on
697fac3fd80SJustin T. Gibbs  * the sortq and kick the controller.
698fac3fd80SJustin T. Gibbs  */
699fac3fd80SJustin T. Gibbs static void
700fac3fd80SJustin T. Gibbs xbd_strategy(struct bio *bp)
701fac3fd80SJustin T. Gibbs {
702fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = bp->bio_disk->d_drv1;
703fac3fd80SJustin T. Gibbs 
704fac3fd80SJustin T. Gibbs 	/* bogus disk? */
705fac3fd80SJustin T. Gibbs 	if (sc == NULL) {
706fac3fd80SJustin T. Gibbs 		bp->bio_error = EINVAL;
707fac3fd80SJustin T. Gibbs 		bp->bio_flags |= BIO_ERROR;
708fac3fd80SJustin T. Gibbs 		bp->bio_resid = bp->bio_bcount;
709fac3fd80SJustin T. Gibbs 		biodone(bp);
710fac3fd80SJustin T. Gibbs 		return;
711fac3fd80SJustin T. Gibbs 	}
712fac3fd80SJustin T. Gibbs 
713fac3fd80SJustin T. Gibbs 	/*
714fac3fd80SJustin T. Gibbs 	 * Place it in the queue of disk activities for this disk
715fac3fd80SJustin T. Gibbs 	 */
716fac3fd80SJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
717fac3fd80SJustin T. Gibbs 
718fac3fd80SJustin T. Gibbs 	xbd_enqueue_bio(sc, bp);
719fac3fd80SJustin T. Gibbs 	xbd_startio(sc);
720fac3fd80SJustin T. Gibbs 
721fac3fd80SJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
722fac3fd80SJustin T. Gibbs 	return;
723fac3fd80SJustin T. Gibbs }
724fac3fd80SJustin T. Gibbs 
725fac3fd80SJustin T. Gibbs /*------------------------------ Ring Management -----------------------------*/
726fac3fd80SJustin T. Gibbs static int
727cdf5d66fSJustin T. Gibbs xbd_alloc_ring(struct xbd_softc *sc)
728fac3fd80SJustin T. Gibbs {
729fac3fd80SJustin T. Gibbs 	blkif_sring_t *sring;
730fac3fd80SJustin T. Gibbs 	uintptr_t sring_page_addr;
731fac3fd80SJustin T. Gibbs 	int error;
732fac3fd80SJustin T. Gibbs 	int i;
733fac3fd80SJustin T. Gibbs 
734fac3fd80SJustin T. Gibbs 	sring = malloc(sc->xbd_ring_pages * PAGE_SIZE, M_XENBLOCKFRONT,
735fac3fd80SJustin T. Gibbs 	    M_NOWAIT|M_ZERO);
736fac3fd80SJustin T. Gibbs 	if (sring == NULL) {
737fac3fd80SJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, ENOMEM, "allocating shared ring");
738fac3fd80SJustin T. Gibbs 		return (ENOMEM);
739fac3fd80SJustin T. Gibbs 	}
740fac3fd80SJustin T. Gibbs 	SHARED_RING_INIT(sring);
741fac3fd80SJustin T. Gibbs 	FRONT_RING_INIT(&sc->xbd_ring, sring, sc->xbd_ring_pages * PAGE_SIZE);
742fac3fd80SJustin T. Gibbs 
743fac3fd80SJustin T. Gibbs 	for (i = 0, sring_page_addr = (uintptr_t)sring;
744fac3fd80SJustin T. Gibbs 	     i < sc->xbd_ring_pages;
745fac3fd80SJustin T. Gibbs 	     i++, sring_page_addr += PAGE_SIZE) {
746fac3fd80SJustin T. Gibbs 
747fac3fd80SJustin T. Gibbs 		error = xenbus_grant_ring(sc->xbd_dev,
748fac3fd80SJustin T. Gibbs 		    (vtomach(sring_page_addr) >> PAGE_SHIFT),
749fac3fd80SJustin T. Gibbs 		    &sc->xbd_ring_ref[i]);
750fac3fd80SJustin T. Gibbs 		if (error) {
751fac3fd80SJustin T. Gibbs 			xenbus_dev_fatal(sc->xbd_dev, error,
752fac3fd80SJustin T. Gibbs 			    "granting ring_ref(%d)", i);
753fac3fd80SJustin T. Gibbs 			return (error);
754fac3fd80SJustin T. Gibbs 		}
755fac3fd80SJustin T. Gibbs 	}
756fac3fd80SJustin T. Gibbs 	if (sc->xbd_ring_pages == 1) {
757fac3fd80SJustin T. Gibbs 		error = xs_printf(XST_NIL, xenbus_get_node(sc->xbd_dev),
758fac3fd80SJustin T. Gibbs 		    "ring-ref", "%u", sc->xbd_ring_ref[0]);
759fac3fd80SJustin T. Gibbs 		if (error) {
760fac3fd80SJustin T. Gibbs 			xenbus_dev_fatal(sc->xbd_dev, error,
761fac3fd80SJustin T. Gibbs 			    "writing %s/ring-ref",
762fac3fd80SJustin T. Gibbs 			    xenbus_get_node(sc->xbd_dev));
763fac3fd80SJustin T. Gibbs 			return (error);
764fac3fd80SJustin T. Gibbs 		}
765fac3fd80SJustin T. Gibbs 	} else {
766fac3fd80SJustin T. Gibbs 		for (i = 0; i < sc->xbd_ring_pages; i++) {
767fac3fd80SJustin T. Gibbs 			char ring_ref_name[]= "ring_refXX";
768fac3fd80SJustin T. Gibbs 
769fac3fd80SJustin T. Gibbs 			snprintf(ring_ref_name, sizeof(ring_ref_name),
770fac3fd80SJustin T. Gibbs 			    "ring-ref%u", i);
771fac3fd80SJustin T. Gibbs 			error = xs_printf(XST_NIL, xenbus_get_node(sc->xbd_dev),
772fac3fd80SJustin T. Gibbs 			     ring_ref_name, "%u", sc->xbd_ring_ref[i]);
773fac3fd80SJustin T. Gibbs 			if (error) {
774fac3fd80SJustin T. Gibbs 				xenbus_dev_fatal(sc->xbd_dev, error,
775fac3fd80SJustin T. Gibbs 				    "writing %s/%s",
776fac3fd80SJustin T. Gibbs 				    xenbus_get_node(sc->xbd_dev),
777fac3fd80SJustin T. Gibbs 				    ring_ref_name);
778fac3fd80SJustin T. Gibbs 				return (error);
779fac3fd80SJustin T. Gibbs 			}
780fac3fd80SJustin T. Gibbs 		}
781fac3fd80SJustin T. Gibbs 	}
782fac3fd80SJustin T. Gibbs 
783*76acc41fSJustin T. Gibbs 	error = xen_intr_alloc_and_bind_local_port(sc->xbd_dev,
784*76acc41fSJustin T. Gibbs 	    xenbus_get_otherend_id(sc->xbd_dev), NULL, xbd_int, sc,
785*76acc41fSJustin T. Gibbs 	    INTR_TYPE_BIO | INTR_MPSAFE, &sc->xen_intr_handle);
786fac3fd80SJustin T. Gibbs 	if (error) {
787fac3fd80SJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
788*76acc41fSJustin T. Gibbs 		    "xen_intr_alloc_and_bind_local_port failed");
789fac3fd80SJustin T. Gibbs 		return (error);
790fac3fd80SJustin T. Gibbs 	}
791fac3fd80SJustin T. Gibbs 
792fac3fd80SJustin T. Gibbs 	return (0);
793fac3fd80SJustin T. Gibbs }
794fac3fd80SJustin T. Gibbs 
795d9fab01dSJustin T. Gibbs static void
796d9fab01dSJustin T. Gibbs xbd_free_ring(struct xbd_softc *sc)
797d9fab01dSJustin T. Gibbs {
798d9fab01dSJustin T. Gibbs 	int i;
799d9fab01dSJustin T. Gibbs 
800d9fab01dSJustin T. Gibbs 	if (sc->xbd_ring.sring == NULL)
801d9fab01dSJustin T. Gibbs 		return;
802d9fab01dSJustin T. Gibbs 
803d9fab01dSJustin T. Gibbs 	for (i = 0; i < sc->xbd_ring_pages; i++) {
804d9fab01dSJustin T. Gibbs 		if (sc->xbd_ring_ref[i] != GRANT_REF_INVALID) {
805d9fab01dSJustin T. Gibbs 			gnttab_end_foreign_access_ref(sc->xbd_ring_ref[i]);
806d9fab01dSJustin T. Gibbs 			sc->xbd_ring_ref[i] = GRANT_REF_INVALID;
807d9fab01dSJustin T. Gibbs 		}
808d9fab01dSJustin T. Gibbs 	}
809d9fab01dSJustin T. Gibbs 	free(sc->xbd_ring.sring, M_XENBLOCKFRONT);
810d9fab01dSJustin T. Gibbs 	sc->xbd_ring.sring = NULL;
811d9fab01dSJustin T. Gibbs }
812d9fab01dSJustin T. Gibbs 
813fac3fd80SJustin T. Gibbs /*-------------------------- Initialization/Teardown -------------------------*/
8149985113bSJustin T. Gibbs static int
8159985113bSJustin T. Gibbs xbd_feature_string(struct xbd_softc *sc, char *features, size_t len)
8169985113bSJustin T. Gibbs {
8179985113bSJustin T. Gibbs 	struct sbuf sb;
8189985113bSJustin T. Gibbs 	int feature_cnt;
8199985113bSJustin T. Gibbs 
8209985113bSJustin T. Gibbs 	sbuf_new(&sb, features, len, SBUF_FIXEDLEN);
8219985113bSJustin T. Gibbs 
8229985113bSJustin T. Gibbs 	feature_cnt = 0;
8239985113bSJustin T. Gibbs 	if ((sc->xbd_flags & XBDF_FLUSH) != 0) {
8249985113bSJustin T. Gibbs 		sbuf_printf(&sb, "flush");
8259985113bSJustin T. Gibbs 		feature_cnt++;
8269985113bSJustin T. Gibbs 	}
8279985113bSJustin T. Gibbs 
8289985113bSJustin T. Gibbs 	if ((sc->xbd_flags & XBDF_BARRIER) != 0) {
8299985113bSJustin T. Gibbs 		if (feature_cnt != 0)
8309985113bSJustin T. Gibbs 			sbuf_printf(&sb, ", ");
8319985113bSJustin T. Gibbs 		sbuf_printf(&sb, "write_barrier");
8329985113bSJustin T. Gibbs 		feature_cnt++;
8339985113bSJustin T. Gibbs 	}
8349985113bSJustin T. Gibbs 
8359985113bSJustin T. Gibbs 	(void) sbuf_finish(&sb);
8369985113bSJustin T. Gibbs 	return (sbuf_len(&sb));
8379985113bSJustin T. Gibbs }
8389985113bSJustin T. Gibbs 
8399985113bSJustin T. Gibbs static int
8409985113bSJustin T. Gibbs xbd_sysctl_features(SYSCTL_HANDLER_ARGS)
8419985113bSJustin T. Gibbs {
8429985113bSJustin T. Gibbs 	char features[80];
8439985113bSJustin T. Gibbs 	struct xbd_softc *sc = arg1;
8449985113bSJustin T. Gibbs 	int error;
8459985113bSJustin T. Gibbs 	int len;
8469985113bSJustin T. Gibbs 
8479985113bSJustin T. Gibbs 	error = sysctl_wire_old_buffer(req, 0);
8489985113bSJustin T. Gibbs 	if (error != 0)
8499985113bSJustin T. Gibbs 		return (error);
8509985113bSJustin T. Gibbs 
8519985113bSJustin T. Gibbs 	len = xbd_feature_string(sc, features, sizeof(features));
8529985113bSJustin T. Gibbs 
8539985113bSJustin T. Gibbs 	/* len is -1 on error, which will make the SYSCTL_OUT a no-op. */
8549985113bSJustin T. Gibbs 	return (SYSCTL_OUT(req, features, len + 1/*NUL*/));
8559985113bSJustin T. Gibbs }
8569985113bSJustin T. Gibbs 
8578b8bfa35SJustin T. Gibbs static void
85833eebb6aSJustin T. Gibbs xbd_setup_sysctl(struct xbd_softc *xbd)
8598b8bfa35SJustin T. Gibbs {
8608b8bfa35SJustin T. Gibbs 	struct sysctl_ctx_list *sysctl_ctx = NULL;
8618b8bfa35SJustin T. Gibbs 	struct sysctl_oid *sysctl_tree = NULL;
8629985113bSJustin T. Gibbs 	struct sysctl_oid_list *children;
8638b8bfa35SJustin T. Gibbs 
86433eebb6aSJustin T. Gibbs 	sysctl_ctx = device_get_sysctl_ctx(xbd->xbd_dev);
8658b8bfa35SJustin T. Gibbs 	if (sysctl_ctx == NULL)
8668b8bfa35SJustin T. Gibbs 		return;
8678b8bfa35SJustin T. Gibbs 
86833eebb6aSJustin T. Gibbs 	sysctl_tree = device_get_sysctl_tree(xbd->xbd_dev);
8698b8bfa35SJustin T. Gibbs 	if (sysctl_tree == NULL)
8708b8bfa35SJustin T. Gibbs 		return;
8718b8bfa35SJustin T. Gibbs 
8729985113bSJustin T. Gibbs 	children = SYSCTL_CHILDREN(sysctl_tree);
8739985113bSJustin T. Gibbs 	SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO,
87433eebb6aSJustin T. Gibbs 	    "max_requests", CTLFLAG_RD, &xbd->xbd_max_requests, -1,
8758b8bfa35SJustin T. Gibbs 	    "maximum outstanding requests (negotiated)");
8768b8bfa35SJustin T. Gibbs 
8779985113bSJustin T. Gibbs 	SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO,
8788b8bfa35SJustin T. Gibbs 	    "max_request_segments", CTLFLAG_RD,
87933eebb6aSJustin T. Gibbs 	    &xbd->xbd_max_request_segments, 0,
8808b8bfa35SJustin T. Gibbs 	    "maximum number of pages per requests (negotiated)");
8818b8bfa35SJustin T. Gibbs 
8829985113bSJustin T. Gibbs 	SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO,
88333eebb6aSJustin T. Gibbs 	    "max_request_size", CTLFLAG_RD, &xbd->xbd_max_request_size, 0,
8848b8bfa35SJustin T. Gibbs 	    "maximum size in bytes of a request (negotiated)");
8858b8bfa35SJustin T. Gibbs 
8869985113bSJustin T. Gibbs 	SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO,
88733eebb6aSJustin T. Gibbs 	    "ring_pages", CTLFLAG_RD, &xbd->xbd_ring_pages, 0,
8888b8bfa35SJustin T. Gibbs 	    "communication channel pages (negotiated)");
8899985113bSJustin T. Gibbs 
8909985113bSJustin T. Gibbs 	SYSCTL_ADD_PROC(sysctl_ctx, children, OID_AUTO,
8919985113bSJustin T. Gibbs 	    "features", CTLTYPE_STRING|CTLFLAG_RD, xbd, 0,
8929985113bSJustin T. Gibbs 	    xbd_sysctl_features, "A", "protocol features (negotiated)");
8938b8bfa35SJustin T. Gibbs }
8948b8bfa35SJustin T. Gibbs 
89523dc5621SKip Macy /*
896fac3fd80SJustin T. Gibbs  * Translate Linux major/minor to an appropriate name and unit
897fac3fd80SJustin T. Gibbs  * number. For HVM guests, this allows us to use the same drive names
898fac3fd80SJustin T. Gibbs  * with blkfront as the emulated drives, easing transition slightly.
89923dc5621SKip Macy  */
900fac3fd80SJustin T. Gibbs static void
901fac3fd80SJustin T. Gibbs xbd_vdevice_to_unit(uint32_t vdevice, int *unit, const char **name)
90223dc5621SKip Macy {
903fac3fd80SJustin T. Gibbs 	static struct vdev_info {
904fac3fd80SJustin T. Gibbs 		int major;
905fac3fd80SJustin T. Gibbs 		int shift;
906fac3fd80SJustin T. Gibbs 		int base;
90723dc5621SKip Macy 		const char *name;
908fac3fd80SJustin T. Gibbs 	} info[] = {
909fac3fd80SJustin T. Gibbs 		{3,	6,	0,	"ada"},	/* ide0 */
910fac3fd80SJustin T. Gibbs 		{22,	6,	2,	"ada"},	/* ide1 */
911fac3fd80SJustin T. Gibbs 		{33,	6,	4,	"ada"},	/* ide2 */
912fac3fd80SJustin T. Gibbs 		{34,	6,	6,	"ada"},	/* ide3 */
913fac3fd80SJustin T. Gibbs 		{56,	6,	8,	"ada"},	/* ide4 */
914fac3fd80SJustin T. Gibbs 		{57,	6,	10,	"ada"},	/* ide5 */
915fac3fd80SJustin T. Gibbs 		{88,	6,	12,	"ada"},	/* ide6 */
916fac3fd80SJustin T. Gibbs 		{89,	6,	14,	"ada"},	/* ide7 */
917fac3fd80SJustin T. Gibbs 		{90,	6,	16,	"ada"},	/* ide8 */
918fac3fd80SJustin T. Gibbs 		{91,	6,	18,	"ada"},	/* ide9 */
91989e0f4d2SKip Macy 
920fac3fd80SJustin T. Gibbs 		{8,	4,	0,	"da"},	/* scsi disk0 */
921fac3fd80SJustin T. Gibbs 		{65,	4,	16,	"da"},	/* scsi disk1 */
922fac3fd80SJustin T. Gibbs 		{66,	4,	32,	"da"},	/* scsi disk2 */
923fac3fd80SJustin T. Gibbs 		{67,	4,	48,	"da"},	/* scsi disk3 */
924fac3fd80SJustin T. Gibbs 		{68,	4,	64,	"da"},	/* scsi disk4 */
925fac3fd80SJustin T. Gibbs 		{69,	4,	80,	"da"},	/* scsi disk5 */
926fac3fd80SJustin T. Gibbs 		{70,	4,	96,	"da"},	/* scsi disk6 */
927fac3fd80SJustin T. Gibbs 		{71,	4,	112,	"da"},	/* scsi disk7 */
928fac3fd80SJustin T. Gibbs 		{128,	4,	128,	"da"},	/* scsi disk8 */
929fac3fd80SJustin T. Gibbs 		{129,	4,	144,	"da"},	/* scsi disk9 */
930fac3fd80SJustin T. Gibbs 		{130,	4,	160,	"da"},	/* scsi disk10 */
931fac3fd80SJustin T. Gibbs 		{131,	4,	176,	"da"},	/* scsi disk11 */
932fac3fd80SJustin T. Gibbs 		{132,	4,	192,	"da"},	/* scsi disk12 */
933fac3fd80SJustin T. Gibbs 		{133,	4,	208,	"da"},	/* scsi disk13 */
934fac3fd80SJustin T. Gibbs 		{134,	4,	224,	"da"},	/* scsi disk14 */
935fac3fd80SJustin T. Gibbs 		{135,	4,	240,	"da"},	/* scsi disk15 */
936fac3fd80SJustin T. Gibbs 
937fac3fd80SJustin T. Gibbs 		{202,	4,	0,	"xbd"},	/* xbd */
938fac3fd80SJustin T. Gibbs 
939fac3fd80SJustin T. Gibbs 		{0,	0,	0,	NULL},
940fac3fd80SJustin T. Gibbs 	};
941fac3fd80SJustin T. Gibbs 	int major = vdevice >> 8;
942fac3fd80SJustin T. Gibbs 	int minor = vdevice & 0xff;
943fac3fd80SJustin T. Gibbs 	int i;
944fac3fd80SJustin T. Gibbs 
945fac3fd80SJustin T. Gibbs 	if (vdevice & (1 << 28)) {
946fac3fd80SJustin T. Gibbs 		*unit = (vdevice & ((1 << 28) - 1)) >> 8;
947fac3fd80SJustin T. Gibbs 		*name = "xbd";
948fac3fd80SJustin T. Gibbs 		return;
94989e0f4d2SKip Macy 	}
95089e0f4d2SKip Macy 
951fac3fd80SJustin T. Gibbs 	for (i = 0; info[i].major; i++) {
952fac3fd80SJustin T. Gibbs 		if (info[i].major == major) {
953fac3fd80SJustin T. Gibbs 			*unit = info[i].base + (minor >> info[i].shift);
954fac3fd80SJustin T. Gibbs 			*name = info[i].name;
955fac3fd80SJustin T. Gibbs 			return;
956fac3fd80SJustin T. Gibbs 		}
957fac3fd80SJustin T. Gibbs 	}
958fac3fd80SJustin T. Gibbs 
959fac3fd80SJustin T. Gibbs 	*unit = minor >> 4;
960fac3fd80SJustin T. Gibbs 	*name = "xbd";
961fac3fd80SJustin T. Gibbs }
962fac3fd80SJustin T. Gibbs 
963fac3fd80SJustin T. Gibbs int
964fac3fd80SJustin T. Gibbs xbd_instance_create(struct xbd_softc *sc, blkif_sector_t sectors,
965fac3fd80SJustin T. Gibbs     int vdevice, uint16_t vdisk_info, unsigned long sector_size)
966fac3fd80SJustin T. Gibbs {
9679985113bSJustin T. Gibbs 	char features[80];
968fac3fd80SJustin T. Gibbs 	int unit, error = 0;
969fac3fd80SJustin T. Gibbs 	const char *name;
970fac3fd80SJustin T. Gibbs 
97133eebb6aSJustin T. Gibbs 	xbd_vdevice_to_unit(vdevice, &unit, &name);
97223dc5621SKip Macy 
973fac3fd80SJustin T. Gibbs 	sc->xbd_unit = unit;
97489e0f4d2SKip Macy 
9759985113bSJustin T. Gibbs 	if (strcmp(name, "xbd") != 0)
976fac3fd80SJustin T. Gibbs 		device_printf(sc->xbd_dev, "attaching as %s%d\n", name, unit);
97789e0f4d2SKip Macy 
9789985113bSJustin T. Gibbs 	if (xbd_feature_string(sc, features, sizeof(features)) > 0) {
9799985113bSJustin T. Gibbs 		device_printf(sc->xbd_dev, "features: %s\n",
9809985113bSJustin T. Gibbs 		    features);
9819985113bSJustin T. Gibbs 	}
9829985113bSJustin T. Gibbs 
983fac3fd80SJustin T. Gibbs 	sc->xbd_disk = disk_alloc();
984fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_unit = sc->xbd_unit;
985fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_open = xbd_open;
986fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_close = xbd_close;
987fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_ioctl = xbd_ioctl;
988fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_strategy = xbd_strategy;
989fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_dump = xbd_dump;
990fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_name = name;
991fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_drv1 = sc;
992fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_sectorsize = sector_size;
9938b8bfa35SJustin T. Gibbs 
994fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_mediasize = sectors * sector_size;
995fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_maxsize = sc->xbd_max_request_size;
996fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_flags = 0;
9979985113bSJustin T. Gibbs 	if ((sc->xbd_flags & (XBDF_FLUSH|XBDF_BARRIER)) != 0) {
9989985113bSJustin T. Gibbs 		sc->xbd_disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
9999985113bSJustin T. Gibbs 		device_printf(sc->xbd_dev,
10009985113bSJustin T. Gibbs 		    "synchronize cache commands enabled.\n");
10019985113bSJustin T. Gibbs 	}
1002fac3fd80SJustin T. Gibbs 	disk_create(sc->xbd_disk, DISK_VERSION);
100312678024SDoug Rabson 
1004fac3fd80SJustin T. Gibbs 	return error;
100512678024SDoug Rabson }
100612678024SDoug Rabson 
1007fac3fd80SJustin T. Gibbs static void
1008fac3fd80SJustin T. Gibbs xbd_free(struct xbd_softc *sc)
100912678024SDoug Rabson {
1010fac3fd80SJustin T. Gibbs 	int i;
101112678024SDoug Rabson 
101212678024SDoug Rabson 	/* Prevent new requests being issued until we fix things up. */
101333eebb6aSJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
1014e2c1fe90SJustin T. Gibbs 	sc->xbd_state = XBD_STATE_DISCONNECTED;
101533eebb6aSJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
101689e0f4d2SKip Macy 
1017fac3fd80SJustin T. Gibbs 	/* Free resources associated with old device channel. */
1018d9fab01dSJustin T. Gibbs 	xbd_free_ring(sc);
1019fac3fd80SJustin T. Gibbs 	if (sc->xbd_shadow) {
102089e0f4d2SKip Macy 
1021fac3fd80SJustin T. Gibbs 		for (i = 0; i < sc->xbd_max_requests; i++) {
1022fac3fd80SJustin T. Gibbs 			struct xbd_command *cm;
102389e0f4d2SKip Macy 
1024fac3fd80SJustin T. Gibbs 			cm = &sc->xbd_shadow[i];
1025fac3fd80SJustin T. Gibbs 			if (cm->cm_sg_refs != NULL) {
1026fac3fd80SJustin T. Gibbs 				free(cm->cm_sg_refs, M_XENBLOCKFRONT);
1027fac3fd80SJustin T. Gibbs 				cm->cm_sg_refs = NULL;
102889e0f4d2SKip Macy 			}
102989e0f4d2SKip Macy 
1030fac3fd80SJustin T. Gibbs 			bus_dmamap_destroy(sc->xbd_io_dmat, cm->cm_map);
1031fac3fd80SJustin T. Gibbs 		}
1032fac3fd80SJustin T. Gibbs 		free(sc->xbd_shadow, M_XENBLOCKFRONT);
1033fac3fd80SJustin T. Gibbs 		sc->xbd_shadow = NULL;
1034fac3fd80SJustin T. Gibbs 
1035fac3fd80SJustin T. Gibbs 		bus_dma_tag_destroy(sc->xbd_io_dmat);
1036fac3fd80SJustin T. Gibbs 
1037e2c1fe90SJustin T. Gibbs 		xbd_initq_cm(sc, XBD_Q_FREE);
1038e2c1fe90SJustin T. Gibbs 		xbd_initq_cm(sc, XBD_Q_READY);
1039e2c1fe90SJustin T. Gibbs 		xbd_initq_cm(sc, XBD_Q_COMPLETE);
1040fac3fd80SJustin T. Gibbs 	}
1041fac3fd80SJustin T. Gibbs 
1042*76acc41fSJustin T. Gibbs 	xen_intr_unbind(&sc->xen_intr_handle);
1043*76acc41fSJustin T. Gibbs 
1044fac3fd80SJustin T. Gibbs }
1045fac3fd80SJustin T. Gibbs 
1046fac3fd80SJustin T. Gibbs /*--------------------------- State Change Handlers --------------------------*/
1047ff662b5cSJustin T. Gibbs static void
104833eebb6aSJustin T. Gibbs xbd_initialize(struct xbd_softc *sc)
104989e0f4d2SKip Macy {
1050ff662b5cSJustin T. Gibbs 	const char *otherend_path;
1051ff662b5cSJustin T. Gibbs 	const char *node_path;
10528b8bfa35SJustin T. Gibbs 	uint32_t max_ring_page_order;
1053ff662b5cSJustin T. Gibbs 	int error;
1054ff662b5cSJustin T. Gibbs 	int i;
105589e0f4d2SKip Macy 
105633eebb6aSJustin T. Gibbs 	if (xenbus_get_state(sc->xbd_dev) != XenbusStateInitialising) {
105706a630f6SJustin T. Gibbs 		/* Initialization has already been performed. */
1058ff662b5cSJustin T. Gibbs 		return;
105906a630f6SJustin T. Gibbs 	}
106089e0f4d2SKip Macy 
1061ff662b5cSJustin T. Gibbs 	/*
1062ff662b5cSJustin T. Gibbs 	 * Protocol defaults valid even if negotiation for a
1063ff662b5cSJustin T. Gibbs 	 * setting fails.
1064ff662b5cSJustin T. Gibbs 	 */
10658b8bfa35SJustin T. Gibbs 	max_ring_page_order = 0;
106633eebb6aSJustin T. Gibbs 	sc->xbd_ring_pages = 1;
106733eebb6aSJustin T. Gibbs 	sc->xbd_max_request_segments = BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK;
106833eebb6aSJustin T. Gibbs 	sc->xbd_max_request_size =
106933eebb6aSJustin T. Gibbs 	    XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments);
107033eebb6aSJustin T. Gibbs 	sc->xbd_max_request_blocks =
107133eebb6aSJustin T. Gibbs 	    BLKIF_SEGS_TO_BLOCKS(sc->xbd_max_request_segments);
1072ff662b5cSJustin T. Gibbs 
1073ff662b5cSJustin T. Gibbs 	/*
1074ff662b5cSJustin T. Gibbs 	 * Protocol negotiation.
1075ff662b5cSJustin T. Gibbs 	 *
1076ff662b5cSJustin T. Gibbs 	 * \note xs_gather() returns on the first encountered error, so
1077ff662b5cSJustin T. Gibbs 	 *       we must use independant calls in order to guarantee
1078ff662b5cSJustin T. Gibbs 	 *       we don't miss information in a sparsly populated back-end
1079ff662b5cSJustin T. Gibbs 	 *       tree.
10808b8bfa35SJustin T. Gibbs 	 *
10818b8bfa35SJustin T. Gibbs 	 * \note xs_scanf() does not update variables for unmatched
10828b8bfa35SJustin T. Gibbs 	 *	 fields.
1083ff662b5cSJustin T. Gibbs 	 */
108433eebb6aSJustin T. Gibbs 	otherend_path = xenbus_get_otherend_path(sc->xbd_dev);
108533eebb6aSJustin T. Gibbs 	node_path = xenbus_get_node(sc->xbd_dev);
10868b8bfa35SJustin T. Gibbs 
10878b8bfa35SJustin T. Gibbs 	/* Support both backend schemes for relaying ring page limits. */
10888b8bfa35SJustin T. Gibbs 	(void)xs_scanf(XST_NIL, otherend_path,
10898b8bfa35SJustin T. Gibbs 	    "max-ring-page-order", NULL, "%" PRIu32,
10908b8bfa35SJustin T. Gibbs 	    &max_ring_page_order);
109133eebb6aSJustin T. Gibbs 	sc->xbd_ring_pages = 1 << max_ring_page_order;
1092ff662b5cSJustin T. Gibbs 	(void)xs_scanf(XST_NIL, otherend_path,
1093ff662b5cSJustin T. Gibbs 	    "max-ring-pages", NULL, "%" PRIu32,
109433eebb6aSJustin T. Gibbs 	    &sc->xbd_ring_pages);
109533eebb6aSJustin T. Gibbs 	if (sc->xbd_ring_pages < 1)
109633eebb6aSJustin T. Gibbs 		sc->xbd_ring_pages = 1;
1097ff662b5cSJustin T. Gibbs 
109833eebb6aSJustin T. Gibbs 	sc->xbd_max_requests =
109933eebb6aSJustin T. Gibbs 	    BLKIF_MAX_RING_REQUESTS(sc->xbd_ring_pages * PAGE_SIZE);
1100ff662b5cSJustin T. Gibbs 	(void)xs_scanf(XST_NIL, otherend_path,
1101ff662b5cSJustin T. Gibbs 	    "max-requests", NULL, "%" PRIu32,
110233eebb6aSJustin T. Gibbs 	    &sc->xbd_max_requests);
1103ff662b5cSJustin T. Gibbs 
1104ff662b5cSJustin T. Gibbs 	(void)xs_scanf(XST_NIL, otherend_path,
1105ff662b5cSJustin T. Gibbs 	    "max-request-segments", NULL, "%" PRIu32,
110633eebb6aSJustin T. Gibbs 	    &sc->xbd_max_request_segments);
1107ff662b5cSJustin T. Gibbs 
1108ff662b5cSJustin T. Gibbs 	(void)xs_scanf(XST_NIL, otherend_path,
1109ff662b5cSJustin T. Gibbs 	    "max-request-size", NULL, "%" PRIu32,
111033eebb6aSJustin T. Gibbs 	    &sc->xbd_max_request_size);
1111ff662b5cSJustin T. Gibbs 
111233eebb6aSJustin T. Gibbs 	if (sc->xbd_ring_pages > XBD_MAX_RING_PAGES) {
111333eebb6aSJustin T. Gibbs 		device_printf(sc->xbd_dev,
111433eebb6aSJustin T. Gibbs 		    "Back-end specified ring-pages of %u "
111533eebb6aSJustin T. Gibbs 		    "limited to front-end limit of %zu.\n",
111633eebb6aSJustin T. Gibbs 		    sc->xbd_ring_pages, XBD_MAX_RING_PAGES);
111733eebb6aSJustin T. Gibbs 		sc->xbd_ring_pages = XBD_MAX_RING_PAGES;
111889e0f4d2SKip Macy 	}
111989e0f4d2SKip Macy 
112033eebb6aSJustin T. Gibbs 	if (powerof2(sc->xbd_ring_pages) == 0) {
11218b8bfa35SJustin T. Gibbs 		uint32_t new_page_limit;
11228b8bfa35SJustin T. Gibbs 
112333eebb6aSJustin T. Gibbs 		new_page_limit = 0x01 << (fls(sc->xbd_ring_pages) - 1);
112433eebb6aSJustin T. Gibbs 		device_printf(sc->xbd_dev,
112533eebb6aSJustin T. Gibbs 		    "Back-end specified ring-pages of %u "
112633eebb6aSJustin T. Gibbs 		    "is not a power of 2. Limited to %u.\n",
112733eebb6aSJustin T. Gibbs 		    sc->xbd_ring_pages, new_page_limit);
112833eebb6aSJustin T. Gibbs 		sc->xbd_ring_pages = new_page_limit;
11298b8bfa35SJustin T. Gibbs 	}
11308b8bfa35SJustin T. Gibbs 
113133eebb6aSJustin T. Gibbs 	if (sc->xbd_max_requests > XBD_MAX_REQUESTS) {
113233eebb6aSJustin T. Gibbs 		device_printf(sc->xbd_dev,
113333eebb6aSJustin T. Gibbs 		    "Back-end specified max_requests of %u "
113433eebb6aSJustin T. Gibbs 		    "limited to front-end limit of %u.\n",
113533eebb6aSJustin T. Gibbs 		    sc->xbd_max_requests, XBD_MAX_REQUESTS);
113633eebb6aSJustin T. Gibbs 		sc->xbd_max_requests = XBD_MAX_REQUESTS;
113789e0f4d2SKip Macy 	}
1138ff662b5cSJustin T. Gibbs 
113933eebb6aSJustin T. Gibbs 	if (sc->xbd_max_request_segments > XBD_MAX_SEGMENTS_PER_REQUEST) {
114033eebb6aSJustin T. Gibbs 		device_printf(sc->xbd_dev,
114133eebb6aSJustin T. Gibbs 		    "Back-end specified max_request_segments of %u "
114233eebb6aSJustin T. Gibbs 		    "limited to front-end limit of %u.\n",
114333eebb6aSJustin T. Gibbs 		    sc->xbd_max_request_segments,
114433eebb6aSJustin T. Gibbs 		    XBD_MAX_SEGMENTS_PER_REQUEST);
114533eebb6aSJustin T. Gibbs 		sc->xbd_max_request_segments = XBD_MAX_SEGMENTS_PER_REQUEST;
114689e0f4d2SKip Macy 	}
1147ff662b5cSJustin T. Gibbs 
114833eebb6aSJustin T. Gibbs 	if (sc->xbd_max_request_size > XBD_MAX_REQUEST_SIZE) {
114933eebb6aSJustin T. Gibbs 		device_printf(sc->xbd_dev,
115033eebb6aSJustin T. Gibbs 		    "Back-end specified max_request_size of %u "
115133eebb6aSJustin T. Gibbs 		    "limited to front-end limit of %u.\n",
115233eebb6aSJustin T. Gibbs 		    sc->xbd_max_request_size,
115333eebb6aSJustin T. Gibbs 		    XBD_MAX_REQUEST_SIZE);
115433eebb6aSJustin T. Gibbs 		sc->xbd_max_request_size = XBD_MAX_REQUEST_SIZE;
1155ff662b5cSJustin T. Gibbs 	}
1156443cc4d4SJustin T. Gibbs 
115733eebb6aSJustin T. Gibbs  	if (sc->xbd_max_request_size >
115833eebb6aSJustin T. Gibbs 	    XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments)) {
115933eebb6aSJustin T. Gibbs  		device_printf(sc->xbd_dev,
116033eebb6aSJustin T. Gibbs 		    "Back-end specified max_request_size of %u "
116133eebb6aSJustin T. Gibbs 		    "limited to front-end limit of %u.  (Too few segments.)\n",
116233eebb6aSJustin T. Gibbs 		    sc->xbd_max_request_size,
116333eebb6aSJustin T. Gibbs 		    XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments));
116433eebb6aSJustin T. Gibbs  		sc->xbd_max_request_size =
116533eebb6aSJustin T. Gibbs  		    XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments);
1166443cc4d4SJustin T. Gibbs  	}
1167443cc4d4SJustin T. Gibbs 
116833eebb6aSJustin T. Gibbs 	sc->xbd_max_request_blocks =
116933eebb6aSJustin T. Gibbs 	    BLKIF_SEGS_TO_BLOCKS(sc->xbd_max_request_segments);
1170ff662b5cSJustin T. Gibbs 
1171ff662b5cSJustin T. Gibbs 	/* Allocate datastructures based on negotiated values. */
117233eebb6aSJustin T. Gibbs 	error = bus_dma_tag_create(
117333eebb6aSJustin T. Gibbs 	    bus_get_dma_tag(sc->xbd_dev),	/* parent */
1174ff662b5cSJustin T. Gibbs 	    512, PAGE_SIZE,			/* algnmnt, boundary */
1175ff662b5cSJustin T. Gibbs 	    BUS_SPACE_MAXADDR,			/* lowaddr */
1176ff662b5cSJustin T. Gibbs 	    BUS_SPACE_MAXADDR,			/* highaddr */
1177ff662b5cSJustin T. Gibbs 	    NULL, NULL,				/* filter, filterarg */
117833eebb6aSJustin T. Gibbs 	    sc->xbd_max_request_size,
117933eebb6aSJustin T. Gibbs 	    sc->xbd_max_request_segments,
1180ff662b5cSJustin T. Gibbs 	    PAGE_SIZE,				/* maxsegsize */
1181ff662b5cSJustin T. Gibbs 	    BUS_DMA_ALLOCNOW,			/* flags */
1182ff662b5cSJustin T. Gibbs 	    busdma_lock_mutex,			/* lockfunc */
118333eebb6aSJustin T. Gibbs 	    &sc->xbd_io_lock,			/* lockarg */
118433eebb6aSJustin T. Gibbs 	    &sc->xbd_io_dmat);
1185ff662b5cSJustin T. Gibbs 	if (error != 0) {
118633eebb6aSJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
1187ff662b5cSJustin T. Gibbs 		    "Cannot allocate parent DMA tag\n");
1188ff662b5cSJustin T. Gibbs 		return;
1189ff662b5cSJustin T. Gibbs 	}
1190ff662b5cSJustin T. Gibbs 
1191ff662b5cSJustin T. Gibbs 	/* Per-transaction data allocation. */
119233eebb6aSJustin T. Gibbs 	sc->xbd_shadow = malloc(sizeof(*sc->xbd_shadow) * sc->xbd_max_requests,
1193ff662b5cSJustin T. Gibbs 	    M_XENBLOCKFRONT, M_NOWAIT|M_ZERO);
119433eebb6aSJustin T. Gibbs 	if (sc->xbd_shadow == NULL) {
119533eebb6aSJustin T. Gibbs 		bus_dma_tag_destroy(sc->xbd_io_dmat);
119633eebb6aSJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
1197ff662b5cSJustin T. Gibbs 		    "Cannot allocate request structures\n");
119806a630f6SJustin T. Gibbs 		return;
1199ff662b5cSJustin T. Gibbs 	}
1200ff662b5cSJustin T. Gibbs 
120133eebb6aSJustin T. Gibbs 	for (i = 0; i < sc->xbd_max_requests; i++) {
120233eebb6aSJustin T. Gibbs 		struct xbd_command *cm;
1203ff662b5cSJustin T. Gibbs 
120433eebb6aSJustin T. Gibbs 		cm = &sc->xbd_shadow[i];
120533eebb6aSJustin T. Gibbs 		cm->cm_sg_refs = malloc(
120633eebb6aSJustin T. Gibbs 		    sizeof(grant_ref_t) * sc->xbd_max_request_segments,
1207ff662b5cSJustin T. Gibbs 		    M_XENBLOCKFRONT, M_NOWAIT);
120833eebb6aSJustin T. Gibbs 		if (cm->cm_sg_refs == NULL)
1209ff662b5cSJustin T. Gibbs 			break;
121033eebb6aSJustin T. Gibbs 		cm->cm_id = i;
1211e2c1fe90SJustin T. Gibbs 		cm->cm_flags = XBDCF_INITIALIZER;
1212ff662b5cSJustin T. Gibbs 		cm->cm_sc = sc;
121333eebb6aSJustin T. Gibbs 		if (bus_dmamap_create(sc->xbd_io_dmat, 0, &cm->cm_map) != 0)
1214ff662b5cSJustin T. Gibbs 			break;
121533eebb6aSJustin T. Gibbs 		xbd_free_command(cm);
1216ff662b5cSJustin T. Gibbs 	}
1217ff662b5cSJustin T. Gibbs 
1218cdf5d66fSJustin T. Gibbs 	if (xbd_alloc_ring(sc) != 0)
1219ff662b5cSJustin T. Gibbs 		return;
1220ff662b5cSJustin T. Gibbs 
12218b8bfa35SJustin T. Gibbs 	/* Support both backend schemes for relaying ring page limits. */
122233eebb6aSJustin T. Gibbs 	if (sc->xbd_ring_pages > 1) {
1223ff662b5cSJustin T. Gibbs 		error = xs_printf(XST_NIL, node_path,
122433eebb6aSJustin T. Gibbs 		    "num-ring-pages","%u",
122533eebb6aSJustin T. Gibbs 		    sc->xbd_ring_pages);
1226ff662b5cSJustin T. Gibbs 		if (error) {
122733eebb6aSJustin T. Gibbs 			xenbus_dev_fatal(sc->xbd_dev, error,
12288b8bfa35SJustin T. Gibbs 			    "writing %s/num-ring-pages",
12298b8bfa35SJustin T. Gibbs 			    node_path);
12308b8bfa35SJustin T. Gibbs 			return;
12318b8bfa35SJustin T. Gibbs 		}
12320d172324SJustin T. Gibbs 
12338b8bfa35SJustin T. Gibbs 		error = xs_printf(XST_NIL, node_path,
12340d172324SJustin T. Gibbs 		    "ring-page-order", "%u",
123533eebb6aSJustin T. Gibbs 		    fls(sc->xbd_ring_pages) - 1);
12368b8bfa35SJustin T. Gibbs 		if (error) {
123733eebb6aSJustin T. Gibbs 			xenbus_dev_fatal(sc->xbd_dev, error,
12388b8bfa35SJustin T. Gibbs 			    "writing %s/ring-page-order",
1239ff662b5cSJustin T. Gibbs 			    node_path);
1240ff662b5cSJustin T. Gibbs 			return;
1241ff662b5cSJustin T. Gibbs 		}
12420d172324SJustin T. Gibbs 	}
1243ff662b5cSJustin T. Gibbs 
1244ff662b5cSJustin T. Gibbs 	error = xs_printf(XST_NIL, node_path,
124533eebb6aSJustin T. Gibbs 	    "max-requests","%u",
124633eebb6aSJustin T. Gibbs 	    sc->xbd_max_requests);
1247ff662b5cSJustin T. Gibbs 	if (error) {
124833eebb6aSJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
1249ff662b5cSJustin T. Gibbs 		    "writing %s/max-requests",
1250ff662b5cSJustin T. Gibbs 		    node_path);
1251ff662b5cSJustin T. Gibbs 		return;
1252ff662b5cSJustin T. Gibbs 	}
1253ff662b5cSJustin T. Gibbs 
1254ff662b5cSJustin T. Gibbs 	error = xs_printf(XST_NIL, node_path,
125533eebb6aSJustin T. Gibbs 	    "max-request-segments","%u",
125633eebb6aSJustin T. Gibbs 	    sc->xbd_max_request_segments);
1257ff662b5cSJustin T. Gibbs 	if (error) {
125833eebb6aSJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
1259ff662b5cSJustin T. Gibbs 		    "writing %s/max-request-segments",
1260ff662b5cSJustin T. Gibbs 		    node_path);
1261ff662b5cSJustin T. Gibbs 		return;
1262ff662b5cSJustin T. Gibbs 	}
1263ff662b5cSJustin T. Gibbs 
1264ff662b5cSJustin T. Gibbs 	error = xs_printf(XST_NIL, node_path,
126533eebb6aSJustin T. Gibbs 	    "max-request-size","%u",
126633eebb6aSJustin T. Gibbs 	    sc->xbd_max_request_size);
1267ff662b5cSJustin T. Gibbs 	if (error) {
126833eebb6aSJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
1269ff662b5cSJustin T. Gibbs 		    "writing %s/max-request-size",
1270ff662b5cSJustin T. Gibbs 		    node_path);
1271ff662b5cSJustin T. Gibbs 		return;
1272ff662b5cSJustin T. Gibbs 	}
1273ff662b5cSJustin T. Gibbs 
1274ff662b5cSJustin T. Gibbs 	error = xs_printf(XST_NIL, node_path, "event-channel",
1275*76acc41fSJustin T. Gibbs 	    "%u", xen_intr_port(sc->xen_intr_handle));
1276ff662b5cSJustin T. Gibbs 	if (error) {
127733eebb6aSJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
1278ff662b5cSJustin T. Gibbs 		    "writing %s/event-channel",
1279ff662b5cSJustin T. Gibbs 		    node_path);
1280ff662b5cSJustin T. Gibbs 		return;
1281ff662b5cSJustin T. Gibbs 	}
1282ff662b5cSJustin T. Gibbs 
128333eebb6aSJustin T. Gibbs 	error = xs_printf(XST_NIL, node_path, "protocol",
128433eebb6aSJustin T. Gibbs 	    "%s", XEN_IO_PROTO_ABI_NATIVE);
1285ff662b5cSJustin T. Gibbs 	if (error) {
128633eebb6aSJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
1287ff662b5cSJustin T. Gibbs 		    "writing %s/protocol",
1288ff662b5cSJustin T. Gibbs 		    node_path);
1289ff662b5cSJustin T. Gibbs 		return;
129023dc5621SKip Macy 	}
129112678024SDoug Rabson 
129233eebb6aSJustin T. Gibbs 	xenbus_set_state(sc->xbd_dev, XenbusStateInitialised);
129389e0f4d2SKip Macy }
129489e0f4d2SKip Macy 
129589e0f4d2SKip Macy /*
129633eebb6aSJustin T. Gibbs  * Invoked when the backend is finally 'ready' (and has published
129733eebb6aSJustin T. Gibbs  * the details about the physical device - #sectors, size, etc).
129889e0f4d2SKip Macy  */
129989e0f4d2SKip Macy static void
130033eebb6aSJustin T. Gibbs xbd_connect(struct xbd_softc *sc)
130189e0f4d2SKip Macy {
130233eebb6aSJustin T. Gibbs 	device_t dev = sc->xbd_dev;
130389e0f4d2SKip Macy 	unsigned long sectors, sector_size;
130489e0f4d2SKip Macy 	unsigned int binfo;
13059985113bSJustin T. Gibbs 	int err, feature_barrier, feature_flush;
130689e0f4d2SKip Macy 
1307e2c1fe90SJustin T. Gibbs 	if (sc->xbd_state == XBD_STATE_CONNECTED ||
1308e2c1fe90SJustin T. Gibbs 	    sc->xbd_state == XBD_STATE_SUSPENDED)
130989e0f4d2SKip Macy 		return;
131089e0f4d2SKip Macy 
131123dc5621SKip Macy 	DPRINTK("blkfront.c:connect:%s.\n", xenbus_get_otherend_path(dev));
131289e0f4d2SKip Macy 
1313ff662b5cSJustin T. Gibbs 	err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
131489e0f4d2SKip Macy 	    "sectors", "%lu", &sectors,
131589e0f4d2SKip Macy 	    "info", "%u", &binfo,
131689e0f4d2SKip Macy 	    "sector-size", "%lu", &sector_size,
131789e0f4d2SKip Macy 	    NULL);
131889e0f4d2SKip Macy 	if (err) {
131923dc5621SKip Macy 		xenbus_dev_fatal(dev, err,
132089e0f4d2SKip Macy 		    "reading backend fields at %s",
132123dc5621SKip Macy 		    xenbus_get_otherend_path(dev));
132289e0f4d2SKip Macy 		return;
132389e0f4d2SKip Macy 	}
1324ff662b5cSJustin T. Gibbs 	err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
1325e4808c4bSKip Macy 	     "feature-barrier", "%lu", &feature_barrier,
132689e0f4d2SKip Macy 	     NULL);
13279985113bSJustin T. Gibbs 	if (err == 0 && feature_barrier != 0)
1328e2c1fe90SJustin T. Gibbs 		sc->xbd_flags |= XBDF_BARRIER;
132989e0f4d2SKip Macy 
13309985113bSJustin T. Gibbs 	err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
13319985113bSJustin T. Gibbs 	     "feature-flush-cache", "%lu", &feature_flush,
13329985113bSJustin T. Gibbs 	     NULL);
13339985113bSJustin T. Gibbs 	if (err == 0 && feature_flush != 0)
13349985113bSJustin T. Gibbs 		sc->xbd_flags |= XBDF_FLUSH;
13359985113bSJustin T. Gibbs 
133633eebb6aSJustin T. Gibbs 	if (sc->xbd_disk == NULL) {
133723dc5621SKip Macy 		device_printf(dev, "%juMB <%s> at %s",
133823dc5621SKip Macy 		    (uintmax_t) sectors / (1048576 / sector_size),
133923dc5621SKip Macy 		    device_get_desc(dev),
134023dc5621SKip Macy 		    xenbus_get_node(dev));
134123dc5621SKip Macy 		bus_print_child_footer(device_get_parent(dev), dev);
134289e0f4d2SKip Macy 
134333eebb6aSJustin T. Gibbs 		xbd_instance_create(sc, sectors, sc->xbd_vdevice, binfo,
134433eebb6aSJustin T. Gibbs 		    sector_size);
134506a630f6SJustin T. Gibbs 	}
134623dc5621SKip Macy 
134723dc5621SKip Macy 	(void)xenbus_set_state(dev, XenbusStateConnected);
134889e0f4d2SKip Macy 
134989e0f4d2SKip Macy 	/* Kick pending requests. */
135033eebb6aSJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
1351e2c1fe90SJustin T. Gibbs 	sc->xbd_state = XBD_STATE_CONNECTED;
135233eebb6aSJustin T. Gibbs 	xbd_startio(sc);
1353e2c1fe90SJustin T. Gibbs 	sc->xbd_flags |= XBDF_READY;
135433eebb6aSJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
135589e0f4d2SKip Macy }
135689e0f4d2SKip Macy 
135789e0f4d2SKip Macy /**
135889e0f4d2SKip Macy  * Handle the change of state of the backend to Closing.  We must delete our
135989e0f4d2SKip Macy  * device-layer structures now, to ensure that writes are flushed through to
1360a4660d59SJustin T. Gibbs  * the backend.  Once this is done, we can switch to Closed in
136189e0f4d2SKip Macy  * acknowledgement.
136289e0f4d2SKip Macy  */
136323dc5621SKip Macy static void
136433eebb6aSJustin T. Gibbs xbd_closing(device_t dev)
136589e0f4d2SKip Macy {
136633eebb6aSJustin T. Gibbs 	struct xbd_softc *sc = device_get_softc(dev);
136789e0f4d2SKip Macy 
1368ff662b5cSJustin T. Gibbs 	xenbus_set_state(dev, XenbusStateClosing);
1369ff662b5cSJustin T. Gibbs 
137033eebb6aSJustin T. Gibbs 	DPRINTK("xbd_closing: %s removed\n", xenbus_get_node(dev));
137189e0f4d2SKip Macy 
137233eebb6aSJustin T. Gibbs 	if (sc->xbd_disk != NULL) {
137333eebb6aSJustin T. Gibbs 		disk_destroy(sc->xbd_disk);
137433eebb6aSJustin T. Gibbs 		sc->xbd_disk = NULL;
137589e0f4d2SKip Macy 	}
137689e0f4d2SKip Macy 
137723dc5621SKip Macy 	xenbus_set_state(dev, XenbusStateClosed);
137889e0f4d2SKip Macy }
137989e0f4d2SKip Macy 
1380fac3fd80SJustin T. Gibbs /*---------------------------- NewBus Entrypoints ----------------------------*/
1381fac3fd80SJustin T. Gibbs static int
1382fac3fd80SJustin T. Gibbs xbd_probe(device_t dev)
1383fac3fd80SJustin T. Gibbs {
1384fac3fd80SJustin T. Gibbs 
1385fac3fd80SJustin T. Gibbs 	if (!strcmp(xenbus_get_type(dev), "vbd")) {
1386fac3fd80SJustin T. Gibbs 		device_set_desc(dev, "Virtual Block Device");
1387fac3fd80SJustin T. Gibbs 		device_quiet(dev);
1388fac3fd80SJustin T. Gibbs 		return (0);
1389fac3fd80SJustin T. Gibbs 	}
1390fac3fd80SJustin T. Gibbs 
1391fac3fd80SJustin T. Gibbs 	return (ENXIO);
1392fac3fd80SJustin T. Gibbs }
1393fac3fd80SJustin T. Gibbs 
1394fac3fd80SJustin T. Gibbs /*
1395fac3fd80SJustin T. Gibbs  * Setup supplies the backend dir, virtual device.  We place an event
1396fac3fd80SJustin T. Gibbs  * channel and shared frame entries.  We watch backend to wait if it's
1397fac3fd80SJustin T. Gibbs  * ok.
1398fac3fd80SJustin T. Gibbs  */
1399fac3fd80SJustin T. Gibbs static int
1400fac3fd80SJustin T. Gibbs xbd_attach(device_t dev)
1401fac3fd80SJustin T. Gibbs {
1402fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc;
1403fac3fd80SJustin T. Gibbs 	const char *name;
1404fac3fd80SJustin T. Gibbs 	uint32_t vdevice;
1405fac3fd80SJustin T. Gibbs 	int error;
1406fac3fd80SJustin T. Gibbs 	int i;
1407fac3fd80SJustin T. Gibbs 	int unit;
1408fac3fd80SJustin T. Gibbs 
1409fac3fd80SJustin T. Gibbs 	/* FIXME: Use dynamic device id if this is not set. */
1410fac3fd80SJustin T. Gibbs 	error = xs_scanf(XST_NIL, xenbus_get_node(dev),
1411fac3fd80SJustin T. Gibbs 	    "virtual-device", NULL, "%" PRIu32, &vdevice);
1412fac3fd80SJustin T. Gibbs 	if (error) {
1413fac3fd80SJustin T. Gibbs 		xenbus_dev_fatal(dev, error, "reading virtual-device");
1414fac3fd80SJustin T. Gibbs 		device_printf(dev, "Couldn't determine virtual device.\n");
1415fac3fd80SJustin T. Gibbs 		return (error);
1416fac3fd80SJustin T. Gibbs 	}
1417fac3fd80SJustin T. Gibbs 
1418fac3fd80SJustin T. Gibbs 	xbd_vdevice_to_unit(vdevice, &unit, &name);
1419fac3fd80SJustin T. Gibbs 	if (!strcmp(name, "xbd"))
1420fac3fd80SJustin T. Gibbs 		device_set_unit(dev, unit);
1421fac3fd80SJustin T. Gibbs 
1422fac3fd80SJustin T. Gibbs 	sc = device_get_softc(dev);
1423fac3fd80SJustin T. Gibbs 	mtx_init(&sc->xbd_io_lock, "blkfront i/o lock", NULL, MTX_DEF);
1424e2c1fe90SJustin T. Gibbs 	xbd_initqs(sc);
1425fac3fd80SJustin T. Gibbs 	for (i = 0; i < XBD_MAX_RING_PAGES; i++)
1426d9fab01dSJustin T. Gibbs 		sc->xbd_ring_ref[i] = GRANT_REF_INVALID;
1427fac3fd80SJustin T. Gibbs 
1428fac3fd80SJustin T. Gibbs 	sc->xbd_dev = dev;
1429fac3fd80SJustin T. Gibbs 	sc->xbd_vdevice = vdevice;
1430e2c1fe90SJustin T. Gibbs 	sc->xbd_state = XBD_STATE_DISCONNECTED;
1431fac3fd80SJustin T. Gibbs 
1432fac3fd80SJustin T. Gibbs 	xbd_setup_sysctl(sc);
1433fac3fd80SJustin T. Gibbs 
1434fac3fd80SJustin T. Gibbs 	/* Wait for backend device to publish its protocol capabilities. */
1435fac3fd80SJustin T. Gibbs 	xenbus_set_state(dev, XenbusStateInitialising);
1436fac3fd80SJustin T. Gibbs 
1437fac3fd80SJustin T. Gibbs 	return (0);
1438fac3fd80SJustin T. Gibbs }
143989e0f4d2SKip Macy 
144023dc5621SKip Macy static int
144133eebb6aSJustin T. Gibbs xbd_detach(device_t dev)
144289e0f4d2SKip Macy {
144333eebb6aSJustin T. Gibbs 	struct xbd_softc *sc = device_get_softc(dev);
144489e0f4d2SKip Macy 
1445e2c1fe90SJustin T. Gibbs 	DPRINTK("%s: %s removed\n", __func__, xenbus_get_node(dev));
144689e0f4d2SKip Macy 
144733eebb6aSJustin T. Gibbs 	xbd_free(sc);
144833eebb6aSJustin T. Gibbs 	mtx_destroy(&sc->xbd_io_lock);
144989e0f4d2SKip Macy 
145089e0f4d2SKip Macy 	return 0;
145189e0f4d2SKip Macy }
145289e0f4d2SKip Macy 
145389e0f4d2SKip Macy static int
1454fac3fd80SJustin T. Gibbs xbd_suspend(device_t dev)
145589e0f4d2SKip Macy {
1456fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = device_get_softc(dev);
1457fac3fd80SJustin T. Gibbs 	int retval;
1458fac3fd80SJustin T. Gibbs 	int saved_state;
145989e0f4d2SKip Macy 
146089e0f4d2SKip Macy 	/* Prevent new requests being issued until we fix things up. */
146133eebb6aSJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
1462e2c1fe90SJustin T. Gibbs 	saved_state = sc->xbd_state;
1463e2c1fe90SJustin T. Gibbs 	sc->xbd_state = XBD_STATE_SUSPENDED;
1464fac3fd80SJustin T. Gibbs 
1465fac3fd80SJustin T. Gibbs 	/* Wait for outstanding I/O to drain. */
1466fac3fd80SJustin T. Gibbs 	retval = 0;
14679985113bSJustin T. Gibbs 	while (xbd_queue_length(sc, XBD_Q_BUSY) != 0) {
1468e2c1fe90SJustin T. Gibbs 		if (msleep(&sc->xbd_cm_q[XBD_Q_BUSY], &sc->xbd_io_lock,
1469fac3fd80SJustin T. Gibbs 		    PRIBIO, "blkf_susp", 30 * hz) == EWOULDBLOCK) {
1470fac3fd80SJustin T. Gibbs 			retval = EBUSY;
1471fac3fd80SJustin T. Gibbs 			break;
1472fac3fd80SJustin T. Gibbs 		}
1473fac3fd80SJustin T. Gibbs 	}
147433eebb6aSJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
147589e0f4d2SKip Macy 
1476fac3fd80SJustin T. Gibbs 	if (retval != 0)
1477e2c1fe90SJustin T. Gibbs 		sc->xbd_state = saved_state;
147833eebb6aSJustin T. Gibbs 
1479fac3fd80SJustin T. Gibbs 	return (retval);
148089e0f4d2SKip Macy }
148189e0f4d2SKip Macy 
1482ff662b5cSJustin T. Gibbs static int
1483fac3fd80SJustin T. Gibbs xbd_resume(device_t dev)
148489e0f4d2SKip Macy {
1485fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = device_get_softc(dev);
1486fac3fd80SJustin T. Gibbs 
1487fac3fd80SJustin T. Gibbs 	DPRINTK("xbd_resume: %s\n", xenbus_get_node(dev));
1488fac3fd80SJustin T. Gibbs 
1489fac3fd80SJustin T. Gibbs 	xbd_free(sc);
1490fac3fd80SJustin T. Gibbs 	xbd_initialize(sc);
1491fac3fd80SJustin T. Gibbs 	return (0);
149289e0f4d2SKip Macy }
149389e0f4d2SKip Macy 
1494fac3fd80SJustin T. Gibbs /**
1495fac3fd80SJustin T. Gibbs  * Callback received when the backend's state changes.
1496fac3fd80SJustin T. Gibbs  */
1497fac3fd80SJustin T. Gibbs static void
1498fac3fd80SJustin T. Gibbs xbd_backend_changed(device_t dev, XenbusState backend_state)
1499fac3fd80SJustin T. Gibbs {
1500fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = device_get_softc(dev);
1501fac3fd80SJustin T. Gibbs 
1502fac3fd80SJustin T. Gibbs 	DPRINTK("backend_state=%d\n", backend_state);
1503fac3fd80SJustin T. Gibbs 
1504fac3fd80SJustin T. Gibbs 	switch (backend_state) {
1505fac3fd80SJustin T. Gibbs 	case XenbusStateUnknown:
1506fac3fd80SJustin T. Gibbs 	case XenbusStateInitialising:
1507fac3fd80SJustin T. Gibbs 	case XenbusStateReconfigured:
1508fac3fd80SJustin T. Gibbs 	case XenbusStateReconfiguring:
1509fac3fd80SJustin T. Gibbs 	case XenbusStateClosed:
1510fac3fd80SJustin T. Gibbs 		break;
1511fac3fd80SJustin T. Gibbs 
1512fac3fd80SJustin T. Gibbs 	case XenbusStateInitWait:
1513fac3fd80SJustin T. Gibbs 	case XenbusStateInitialised:
1514fac3fd80SJustin T. Gibbs 		xbd_initialize(sc);
1515fac3fd80SJustin T. Gibbs 		break;
1516fac3fd80SJustin T. Gibbs 
1517fac3fd80SJustin T. Gibbs 	case XenbusStateConnected:
1518fac3fd80SJustin T. Gibbs 		xbd_initialize(sc);
1519fac3fd80SJustin T. Gibbs 		xbd_connect(sc);
1520fac3fd80SJustin T. Gibbs 		break;
1521fac3fd80SJustin T. Gibbs 
1522fac3fd80SJustin T. Gibbs 	case XenbusStateClosing:
1523fac3fd80SJustin T. Gibbs 		if (sc->xbd_users > 0)
1524fac3fd80SJustin T. Gibbs 			xenbus_dev_error(dev, -EBUSY,
1525fac3fd80SJustin T. Gibbs 			    "Device in use; refusing to close");
1526fac3fd80SJustin T. Gibbs 		else
1527fac3fd80SJustin T. Gibbs 			xbd_closing(dev);
1528fac3fd80SJustin T. Gibbs 		break;
1529fac3fd80SJustin T. Gibbs 	}
1530fac3fd80SJustin T. Gibbs }
1531fac3fd80SJustin T. Gibbs 
1532fac3fd80SJustin T. Gibbs /*---------------------------- NewBus Registration ---------------------------*/
153333eebb6aSJustin T. Gibbs static device_method_t xbd_methods[] = {
153423dc5621SKip Macy 	/* Device interface */
153533eebb6aSJustin T. Gibbs 	DEVMETHOD(device_probe,         xbd_probe),
153633eebb6aSJustin T. Gibbs 	DEVMETHOD(device_attach,        xbd_attach),
153733eebb6aSJustin T. Gibbs 	DEVMETHOD(device_detach,        xbd_detach),
153823dc5621SKip Macy 	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
153933eebb6aSJustin T. Gibbs 	DEVMETHOD(device_suspend,       xbd_suspend),
154033eebb6aSJustin T. Gibbs 	DEVMETHOD(device_resume,        xbd_resume),
154189e0f4d2SKip Macy 
154223dc5621SKip Macy 	/* Xenbus interface */
154333eebb6aSJustin T. Gibbs 	DEVMETHOD(xenbus_otherend_changed, xbd_backend_changed),
154489e0f4d2SKip Macy 
154523dc5621SKip Macy 	{ 0, 0 }
154689e0f4d2SKip Macy };
154789e0f4d2SKip Macy 
154833eebb6aSJustin T. Gibbs static driver_t xbd_driver = {
154923dc5621SKip Macy 	"xbd",
155033eebb6aSJustin T. Gibbs 	xbd_methods,
155133eebb6aSJustin T. Gibbs 	sizeof(struct xbd_softc),
155289e0f4d2SKip Macy };
155333eebb6aSJustin T. Gibbs devclass_t xbd_devclass;
155489e0f4d2SKip Macy 
155533eebb6aSJustin T. Gibbs DRIVER_MODULE(xbd, xenbusb_front, xbd_driver, xbd_devclass, 0, 0);
1556