xref: /freebsd/sys/dev/xen/blkfront/blkfront.c (revision e2c1fe9009991704c5e4e9f85bb8381fe452e31a)
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 
54ff662b5cSJustin T. Gibbs #include <machine/_inttypes.h>
5589e0f4d2SKip Macy #include <machine/xen/xen-os.h>
562913e88cSRobert Watson #include <machine/xen/xenvar.h>
5712678024SDoug Rabson #include <machine/xen/xenfunc.h>
58ff662b5cSJustin T. Gibbs 
5912678024SDoug Rabson #include <xen/hypervisor.h>
603a6d1fcfSKip Macy #include <xen/xen_intr.h>
613a6d1fcfSKip Macy #include <xen/evtchn.h>
6212678024SDoug Rabson #include <xen/gnttab.h>
6389e0f4d2SKip Macy #include <xen/interface/grant_table.h>
6423dc5621SKip Macy #include <xen/interface/io/protocols.h>
6523dc5621SKip Macy #include <xen/xenbus/xenbusvar.h>
6689e0f4d2SKip Macy 
6789e0f4d2SKip Macy #include <geom/geom_disk.h>
6889e0f4d2SKip Macy 
6989e0f4d2SKip Macy #include <dev/xen/blkfront/block.h>
7089e0f4d2SKip Macy 
7123dc5621SKip Macy #include "xenbus_if.h"
7223dc5621SKip Macy 
73fac3fd80SJustin T. Gibbs /*--------------------------- Forward Declarations ---------------------------*/
7433eebb6aSJustin T. Gibbs static void xbd_closing(device_t);
75fac3fd80SJustin T. Gibbs static void xbd_startio(struct xbd_softc *sc);
7689e0f4d2SKip Macy 
77fac3fd80SJustin T. Gibbs /*---------------------------------- Macros ----------------------------------*/
78fac3fd80SJustin T. Gibbs #if 0
79fac3fd80SJustin T. Gibbs #define DPRINTK(fmt, args...) printf("[XEN] %s:%d: " fmt ".\n", __func__, __LINE__, ##args)
80fac3fd80SJustin T. Gibbs #else
81fac3fd80SJustin T. Gibbs #define DPRINTK(fmt, args...)
82fac3fd80SJustin T. Gibbs #endif
83fac3fd80SJustin T. Gibbs 
84fac3fd80SJustin T. Gibbs #define XBD_SECTOR_SHFT		9
85ff662b5cSJustin T. Gibbs 
86fac3fd80SJustin T. Gibbs /*---------------------------- Global Static Data ----------------------------*/
87fac3fd80SJustin T. Gibbs static MALLOC_DEFINE(M_XENBLOCKFRONT, "xbd", "Xen Block Front driver data");
8889e0f4d2SKip Macy 
89fac3fd80SJustin T. Gibbs /*---------------------------- Command Processing ----------------------------*/
90fac3fd80SJustin T. Gibbs static inline void
91cdf5d66fSJustin T. Gibbs xbd_flush_requests(struct xbd_softc *sc)
92fac3fd80SJustin T. Gibbs {
93fac3fd80SJustin T. Gibbs 	int notify;
94e4808c4bSKip Macy 
95fac3fd80SJustin T. Gibbs 	RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&sc->xbd_ring, notify);
9689e0f4d2SKip Macy 
97fac3fd80SJustin T. Gibbs 	if (notify)
98fac3fd80SJustin T. Gibbs 		notify_remote_via_irq(sc->xbd_irq);
99fac3fd80SJustin T. Gibbs }
10089e0f4d2SKip Macy 
10123dc5621SKip Macy static void
102fac3fd80SJustin T. Gibbs xbd_free_command(struct xbd_command *cm)
10323dc5621SKip Macy {
10423dc5621SKip Macy 
105*e2c1fe90SJustin T. Gibbs 	KASSERT((cm->cm_flags & XBDCF_Q_MASK) == XBD_Q_NONE,
106*e2c1fe90SJustin T. Gibbs 	    ("Freeing command that is still on queue %d.",
107*e2c1fe90SJustin T. Gibbs 	    cm->cm_flags & XBDCF_Q_MASK));
10823dc5621SKip Macy 
109*e2c1fe90SJustin T. Gibbs 	cm->cm_flags = XBDCF_INITIALIZER;
110fac3fd80SJustin T. Gibbs 	cm->cm_bp = NULL;
111fac3fd80SJustin T. Gibbs 	cm->cm_complete = NULL;
112*e2c1fe90SJustin T. Gibbs 	xbd_enqueue_cm(cm, XBD_Q_FREE);
11323dc5621SKip Macy }
11423dc5621SKip Macy 
11589e0f4d2SKip Macy static void
116fac3fd80SJustin T. Gibbs xbd_queue_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
11789e0f4d2SKip Macy {
118fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc;
119fac3fd80SJustin T. Gibbs 	struct xbd_command *cm;
120fac3fd80SJustin T. Gibbs 	blkif_request_t	*ring_req;
121fac3fd80SJustin T. Gibbs 	struct blkif_request_segment *sg;
122fac3fd80SJustin T. Gibbs 	struct blkif_request_segment *last_block_sg;
123fac3fd80SJustin T. Gibbs 	grant_ref_t *sg_ref;
124fac3fd80SJustin T. Gibbs 	vm_paddr_t buffer_ma;
125fac3fd80SJustin T. Gibbs 	uint64_t fsect, lsect;
126fac3fd80SJustin T. Gibbs 	int ref;
127fac3fd80SJustin T. Gibbs 	int op;
128fac3fd80SJustin T. Gibbs 	int block_segs;
12989e0f4d2SKip Macy 
130fac3fd80SJustin T. Gibbs 	cm = arg;
131fac3fd80SJustin T. Gibbs 	sc = cm->cm_sc;
132fac3fd80SJustin T. Gibbs 
133fac3fd80SJustin T. Gibbs 	if (error) {
134fac3fd80SJustin T. Gibbs 		printf("error %d in xbd_queue_cb\n", error);
135fac3fd80SJustin T. Gibbs 		cm->cm_bp->bio_error = EIO;
136fac3fd80SJustin T. Gibbs 		biodone(cm->cm_bp);
137fac3fd80SJustin T. Gibbs 		xbd_free_command(cm);
13889e0f4d2SKip Macy 		return;
13989e0f4d2SKip Macy 	}
14089e0f4d2SKip Macy 
141fac3fd80SJustin T. Gibbs 	/* Fill out a communications ring structure. */
142fac3fd80SJustin T. Gibbs 	ring_req = RING_GET_REQUEST(&sc->xbd_ring, sc->xbd_ring.req_prod_pvt);
143fac3fd80SJustin T. Gibbs 	sc->xbd_ring.req_prod_pvt++;
144fac3fd80SJustin T. Gibbs 	ring_req->id = cm->cm_id;
145fac3fd80SJustin T. Gibbs 	ring_req->operation = cm->cm_operation;
146fac3fd80SJustin T. Gibbs 	ring_req->sector_number = cm->cm_sector_number;
147fac3fd80SJustin T. Gibbs 	ring_req->handle = (blkif_vdev_t)(uintptr_t)sc->xbd_disk;
148fac3fd80SJustin T. Gibbs 	ring_req->nr_segments = nsegs;
149fac3fd80SJustin T. Gibbs 	cm->cm_nseg = nsegs;
150fac3fd80SJustin T. Gibbs 
151fac3fd80SJustin T. Gibbs 	block_segs    = MIN(nsegs, BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK);
152fac3fd80SJustin T. Gibbs 	sg            = ring_req->seg;
153fac3fd80SJustin T. Gibbs 	last_block_sg = sg + block_segs;
154fac3fd80SJustin T. Gibbs 	sg_ref        = cm->cm_sg_refs;
155fac3fd80SJustin T. Gibbs 
156fac3fd80SJustin T. Gibbs 	while (1) {
157fac3fd80SJustin T. Gibbs 
158fac3fd80SJustin T. Gibbs 		while (sg < last_block_sg) {
159fac3fd80SJustin T. Gibbs 			buffer_ma = segs->ds_addr;
160fac3fd80SJustin T. Gibbs 			fsect = (buffer_ma & PAGE_MASK) >> XBD_SECTOR_SHFT;
161fac3fd80SJustin T. Gibbs 			lsect = fsect + (segs->ds_len  >> XBD_SECTOR_SHFT) - 1;
162fac3fd80SJustin T. Gibbs 
163fac3fd80SJustin T. Gibbs 			KASSERT(lsect <= 7, ("XEN disk driver data cannot "
164fac3fd80SJustin T. Gibbs 			    "cross a page boundary"));
165fac3fd80SJustin T. Gibbs 
166fac3fd80SJustin T. Gibbs 			/* install a grant reference. */
167fac3fd80SJustin T. Gibbs 			ref = gnttab_claim_grant_reference(&cm->cm_gref_head);
168fac3fd80SJustin T. Gibbs 
169e4808c4bSKip Macy 			/*
170fac3fd80SJustin T. Gibbs 			 * GNTTAB_LIST_END == 0xffffffff, but it is private
171fac3fd80SJustin T. Gibbs 			 * to gnttab.c.
172e4808c4bSKip Macy 			 */
173fac3fd80SJustin T. Gibbs 			KASSERT(ref != ~0, ("grant_reference failed"));
174fac3fd80SJustin T. Gibbs 
175fac3fd80SJustin T. Gibbs 			gnttab_grant_foreign_access_ref(
176fac3fd80SJustin T. Gibbs 			    ref,
177fac3fd80SJustin T. Gibbs 			    xenbus_get_otherend_id(sc->xbd_dev),
178fac3fd80SJustin T. Gibbs 			    buffer_ma >> PAGE_SHIFT,
179fac3fd80SJustin T. Gibbs 			    ring_req->operation == BLKIF_OP_WRITE);
180fac3fd80SJustin T. Gibbs 
181fac3fd80SJustin T. Gibbs 			*sg_ref = ref;
182fac3fd80SJustin T. Gibbs 			*sg = (struct blkif_request_segment) {
183fac3fd80SJustin T. Gibbs 				.gref       = ref,
184fac3fd80SJustin T. Gibbs 				.first_sect = fsect,
185fac3fd80SJustin T. Gibbs 				.last_sect  = lsect
186fac3fd80SJustin T. Gibbs 			};
187fac3fd80SJustin T. Gibbs 			sg++;
188fac3fd80SJustin T. Gibbs 			sg_ref++;
189fac3fd80SJustin T. Gibbs 			segs++;
190fac3fd80SJustin T. Gibbs 			nsegs--;
191fac3fd80SJustin T. Gibbs 		}
192fac3fd80SJustin T. Gibbs 		block_segs = MIN(nsegs, BLKIF_MAX_SEGMENTS_PER_SEGMENT_BLOCK);
193fac3fd80SJustin T. Gibbs 		if (block_segs == 0)
194fac3fd80SJustin T. Gibbs 			break;
195fac3fd80SJustin T. Gibbs 
196fac3fd80SJustin T. Gibbs 		sg = BLKRING_GET_SEG_BLOCK(&sc->xbd_ring,
197fac3fd80SJustin T. Gibbs 		    sc->xbd_ring.req_prod_pvt);
198fac3fd80SJustin T. Gibbs 		sc->xbd_ring.req_prod_pvt++;
199fac3fd80SJustin T. Gibbs 		last_block_sg = sg + block_segs;
200fac3fd80SJustin T. Gibbs 	}
201fac3fd80SJustin T. Gibbs 
202fac3fd80SJustin T. Gibbs 	if (cm->cm_operation == BLKIF_OP_READ)
203fac3fd80SJustin T. Gibbs 		op = BUS_DMASYNC_PREREAD;
204fac3fd80SJustin T. Gibbs 	else if (cm->cm_operation == BLKIF_OP_WRITE)
205fac3fd80SJustin T. Gibbs 		op = BUS_DMASYNC_PREWRITE;
206fac3fd80SJustin T. Gibbs 	else
207fac3fd80SJustin T. Gibbs 		op = 0;
208fac3fd80SJustin T. Gibbs 	bus_dmamap_sync(sc->xbd_io_dmat, cm->cm_map, op);
209fac3fd80SJustin T. Gibbs 
210fac3fd80SJustin T. Gibbs 	gnttab_free_grant_references(cm->cm_gref_head);
211fac3fd80SJustin T. Gibbs 
212*e2c1fe90SJustin T. Gibbs 	xbd_enqueue_cm(cm, XBD_Q_BUSY);
213fac3fd80SJustin T. Gibbs 
214fac3fd80SJustin T. Gibbs 	/*
215fac3fd80SJustin T. Gibbs 	 * This flag means that we're probably executing in the busdma swi
216fac3fd80SJustin T. Gibbs 	 * instead of in the startio context, so an explicit flush is needed.
217fac3fd80SJustin T. Gibbs 	 */
218*e2c1fe90SJustin T. Gibbs 	if (cm->cm_flags & XBDCF_FROZEN)
219cdf5d66fSJustin T. Gibbs 		xbd_flush_requests(sc);
220fac3fd80SJustin T. Gibbs 
221fac3fd80SJustin T. Gibbs 	return;
222fac3fd80SJustin T. Gibbs }
223fac3fd80SJustin T. Gibbs 
224fac3fd80SJustin T. Gibbs static int
225fac3fd80SJustin T. Gibbs xbd_queue_request(struct xbd_softc *sc, struct xbd_command *cm)
226fac3fd80SJustin T. Gibbs {
227fac3fd80SJustin T. Gibbs 	int error;
228fac3fd80SJustin T. Gibbs 
229fac3fd80SJustin T. Gibbs 	error = bus_dmamap_load(sc->xbd_io_dmat, cm->cm_map, cm->cm_data,
230fac3fd80SJustin T. Gibbs 	    cm->cm_datalen, xbd_queue_cb, cm, 0);
231fac3fd80SJustin T. Gibbs 	if (error == EINPROGRESS) {
232fac3fd80SJustin T. Gibbs 		printf("EINPROGRESS\n");
233*e2c1fe90SJustin T. Gibbs 		sc->xbd_flags |= XBDF_FROZEN;
234*e2c1fe90SJustin T. Gibbs 		cm->cm_flags |= XBDCF_FROZEN;
235fac3fd80SJustin T. Gibbs 		return (0);
236fac3fd80SJustin T. Gibbs 	}
237fac3fd80SJustin T. Gibbs 
238fac3fd80SJustin T. Gibbs 	return (error);
239fac3fd80SJustin T. Gibbs }
240fac3fd80SJustin T. Gibbs 
241fac3fd80SJustin T. Gibbs static void
242fac3fd80SJustin T. Gibbs xbd_restart_queue_callback(void *arg)
243fac3fd80SJustin T. Gibbs {
244fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = arg;
245fac3fd80SJustin T. Gibbs 
24633eebb6aSJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
247e4808c4bSKip Macy 
24833eebb6aSJustin T. Gibbs 	xbd_startio(sc);
249e4808c4bSKip Macy 
25033eebb6aSJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
251fac3fd80SJustin T. Gibbs }
252fac3fd80SJustin T. Gibbs 
253fac3fd80SJustin T. Gibbs static struct xbd_command *
254fac3fd80SJustin T. Gibbs xbd_bio_command(struct xbd_softc *sc)
255fac3fd80SJustin T. Gibbs {
256fac3fd80SJustin T. Gibbs 	struct xbd_command *cm;
257fac3fd80SJustin T. Gibbs 	struct bio *bp;
258fac3fd80SJustin T. Gibbs 
259*e2c1fe90SJustin T. Gibbs 	if (unlikely(sc->xbd_state != XBD_STATE_CONNECTED))
260fac3fd80SJustin T. Gibbs 		return (NULL);
261fac3fd80SJustin T. Gibbs 
262fac3fd80SJustin T. Gibbs 	bp = xbd_dequeue_bio(sc);
263fac3fd80SJustin T. Gibbs 	if (bp == NULL)
264fac3fd80SJustin T. Gibbs 		return (NULL);
265fac3fd80SJustin T. Gibbs 
266*e2c1fe90SJustin T. Gibbs 	if ((cm = xbd_dequeue_cm(sc, XBD_Q_FREE)) == NULL) {
267fac3fd80SJustin T. Gibbs 		xbd_requeue_bio(sc, bp);
268fac3fd80SJustin T. Gibbs 		return (NULL);
269fac3fd80SJustin T. Gibbs 	}
270fac3fd80SJustin T. Gibbs 
271fac3fd80SJustin T. Gibbs 	if (gnttab_alloc_grant_references(sc->xbd_max_request_segments,
272fac3fd80SJustin T. Gibbs 	    &cm->cm_gref_head) != 0) {
273fac3fd80SJustin T. Gibbs 		gnttab_request_free_callback(&sc->xbd_callback,
274fac3fd80SJustin T. Gibbs 		    xbd_restart_queue_callback, sc,
275fac3fd80SJustin T. Gibbs 		    sc->xbd_max_request_segments);
276fac3fd80SJustin T. Gibbs 		xbd_requeue_bio(sc, bp);
277*e2c1fe90SJustin T. Gibbs 		xbd_enqueue_cm(cm, XBD_Q_FREE);
278*e2c1fe90SJustin T. Gibbs 		sc->xbd_flags |= XBDF_FROZEN;
279fac3fd80SJustin T. Gibbs 		return (NULL);
280fac3fd80SJustin T. Gibbs 	}
281fac3fd80SJustin T. Gibbs 
282fac3fd80SJustin T. Gibbs 	cm->cm_bp = bp;
283fac3fd80SJustin T. Gibbs 	cm->cm_data = bp->bio_data;
284fac3fd80SJustin T. Gibbs 	cm->cm_datalen = bp->bio_bcount;
285fac3fd80SJustin T. Gibbs 	cm->cm_operation = (bp->bio_cmd == BIO_READ) ?
286fac3fd80SJustin T. Gibbs 	    BLKIF_OP_READ : BLKIF_OP_WRITE;
287fac3fd80SJustin T. Gibbs 	cm->cm_sector_number = (blkif_sector_t)bp->bio_pblkno;
288fac3fd80SJustin T. Gibbs 
289fac3fd80SJustin T. Gibbs 	return (cm);
290fac3fd80SJustin T. Gibbs }
291fac3fd80SJustin T. Gibbs 
292fac3fd80SJustin T. Gibbs /*
293fac3fd80SJustin T. Gibbs  * Dequeue buffers and place them in the shared communication ring.
294fac3fd80SJustin T. Gibbs  * Return when no more requests can be accepted or all buffers have
295fac3fd80SJustin T. Gibbs  * been queued.
296fac3fd80SJustin T. Gibbs  *
297fac3fd80SJustin T. Gibbs  * Signal XEN once the ring has been filled out.
298fac3fd80SJustin T. Gibbs  */
299fac3fd80SJustin T. Gibbs static void
300fac3fd80SJustin T. Gibbs xbd_startio(struct xbd_softc *sc)
301fac3fd80SJustin T. Gibbs {
302fac3fd80SJustin T. Gibbs 	struct xbd_command *cm;
303fac3fd80SJustin T. Gibbs 	int error, queued = 0;
304fac3fd80SJustin T. Gibbs 
305fac3fd80SJustin T. Gibbs 	mtx_assert(&sc->xbd_io_lock, MA_OWNED);
306fac3fd80SJustin T. Gibbs 
307*e2c1fe90SJustin T. Gibbs 	if (sc->xbd_state != XBD_STATE_CONNECTED)
308e4808c4bSKip Macy 		return;
309fac3fd80SJustin T. Gibbs 
310fac3fd80SJustin T. Gibbs 	while (RING_FREE_REQUESTS(&sc->xbd_ring) >=
311fac3fd80SJustin T. Gibbs 	    sc->xbd_max_request_blocks) {
312*e2c1fe90SJustin T. Gibbs 		if (sc->xbd_flags & XBDF_FROZEN)
313fac3fd80SJustin T. Gibbs 			break;
314fac3fd80SJustin T. Gibbs 
315*e2c1fe90SJustin T. Gibbs 		cm = xbd_dequeue_cm(sc, XBD_Q_READY);
316fac3fd80SJustin T. Gibbs 
317fac3fd80SJustin T. Gibbs 		if (cm == NULL)
318fac3fd80SJustin T. Gibbs 		    cm = xbd_bio_command(sc);
319fac3fd80SJustin T. Gibbs 
320fac3fd80SJustin T. Gibbs 		if (cm == NULL)
321fac3fd80SJustin T. Gibbs 			break;
322fac3fd80SJustin T. Gibbs 
323fac3fd80SJustin T. Gibbs 		if ((error = xbd_queue_request(sc, cm)) != 0) {
324fac3fd80SJustin T. Gibbs 			printf("xbd_queue_request returned %d\n", error);
325fac3fd80SJustin T. Gibbs 			break;
326fac3fd80SJustin T. Gibbs 		}
327fac3fd80SJustin T. Gibbs 		queued++;
328fac3fd80SJustin T. Gibbs 	}
329fac3fd80SJustin T. Gibbs 
330fac3fd80SJustin T. Gibbs 	if (queued != 0)
331cdf5d66fSJustin T. Gibbs 		xbd_flush_requests(sc);
332e4808c4bSKip Macy }
333e4808c4bSKip Macy 
334e4808c4bSKip Macy static void
33533eebb6aSJustin T. Gibbs xbd_bio_complete(struct xbd_softc *sc, struct xbd_command *cm)
336e4808c4bSKip Macy {
337e4808c4bSKip Macy 	struct bio *bp;
338e4808c4bSKip Macy 
33933eebb6aSJustin T. Gibbs 	bp = cm->cm_bp;
340e4808c4bSKip Macy 
34133eebb6aSJustin T. Gibbs 	if (unlikely(cm->cm_status != BLKIF_RSP_OKAY)) {
342e4808c4bSKip Macy 		disk_err(bp, "disk error" , -1, 0);
34333eebb6aSJustin T. Gibbs 		printf(" status: %x\n", cm->cm_status);
344e4808c4bSKip Macy 		bp->bio_flags |= BIO_ERROR;
345e4808c4bSKip Macy 	}
346e4808c4bSKip Macy 
347e4808c4bSKip Macy 	if (bp->bio_flags & BIO_ERROR)
348e4808c4bSKip Macy 		bp->bio_error = EIO;
349e4808c4bSKip Macy 	else
350e4808c4bSKip Macy 		bp->bio_resid = 0;
351e4808c4bSKip Macy 
35233eebb6aSJustin T. Gibbs 	xbd_free_command(cm);
353e4808c4bSKip Macy 	biodone(bp);
354e4808c4bSKip Macy }
355e4808c4bSKip Macy 
356fac3fd80SJustin T. Gibbs static int
357fac3fd80SJustin T. Gibbs xbd_completion(struct xbd_command *cm)
358fac3fd80SJustin T. Gibbs {
359fac3fd80SJustin T. Gibbs 	gnttab_end_foreign_access_references(cm->cm_nseg, cm->cm_sg_refs);
360fac3fd80SJustin T. Gibbs 	return (BLKIF_SEGS_TO_BLOCKS(cm->cm_nseg));
361fac3fd80SJustin T. Gibbs }
362fac3fd80SJustin T. Gibbs 
363fac3fd80SJustin T. Gibbs static void
364fac3fd80SJustin T. Gibbs xbd_int(void *xsc)
365fac3fd80SJustin T. Gibbs {
366fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = xsc;
367fac3fd80SJustin T. Gibbs 	struct xbd_command *cm;
368fac3fd80SJustin T. Gibbs 	blkif_response_t *bret;
369fac3fd80SJustin T. Gibbs 	RING_IDX i, rp;
370fac3fd80SJustin T. Gibbs 	int op;
371fac3fd80SJustin T. Gibbs 
372fac3fd80SJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
373fac3fd80SJustin T. Gibbs 
374*e2c1fe90SJustin T. Gibbs 	if (unlikely(sc->xbd_state == XBD_STATE_DISCONNECTED)) {
375fac3fd80SJustin T. Gibbs 		mtx_unlock(&sc->xbd_io_lock);
376fac3fd80SJustin T. Gibbs 		return;
377fac3fd80SJustin T. Gibbs 	}
378fac3fd80SJustin T. Gibbs 
379fac3fd80SJustin T. Gibbs  again:
380fac3fd80SJustin T. Gibbs 	rp = sc->xbd_ring.sring->rsp_prod;
381fac3fd80SJustin T. Gibbs 	rmb(); /* Ensure we see queued responses up to 'rp'. */
382fac3fd80SJustin T. Gibbs 
383fac3fd80SJustin T. Gibbs 	for (i = sc->xbd_ring.rsp_cons; i != rp;) {
384fac3fd80SJustin T. Gibbs 		bret = RING_GET_RESPONSE(&sc->xbd_ring, i);
385fac3fd80SJustin T. Gibbs 		cm   = &sc->xbd_shadow[bret->id];
386fac3fd80SJustin T. Gibbs 
387*e2c1fe90SJustin T. Gibbs 		xbd_remove_cm(cm, XBD_Q_BUSY);
388fac3fd80SJustin T. Gibbs 		i += xbd_completion(cm);
389fac3fd80SJustin T. Gibbs 
390fac3fd80SJustin T. Gibbs 		if (cm->cm_operation == BLKIF_OP_READ)
391fac3fd80SJustin T. Gibbs 			op = BUS_DMASYNC_POSTREAD;
392fac3fd80SJustin T. Gibbs 		else if (cm->cm_operation == BLKIF_OP_WRITE)
393fac3fd80SJustin T. Gibbs 			op = BUS_DMASYNC_POSTWRITE;
394fac3fd80SJustin T. Gibbs 		else
395fac3fd80SJustin T. Gibbs 			op = 0;
396fac3fd80SJustin T. Gibbs 		bus_dmamap_sync(sc->xbd_io_dmat, cm->cm_map, op);
397fac3fd80SJustin T. Gibbs 		bus_dmamap_unload(sc->xbd_io_dmat, cm->cm_map);
398fac3fd80SJustin T. Gibbs 
399fac3fd80SJustin T. Gibbs 		/*
400fac3fd80SJustin T. Gibbs 		 * If commands are completing then resources are probably
401fac3fd80SJustin T. Gibbs 		 * being freed as well.  It's a cheap assumption even when
402fac3fd80SJustin T. Gibbs 		 * wrong.
403fac3fd80SJustin T. Gibbs 		 */
404*e2c1fe90SJustin T. Gibbs 		sc->xbd_flags &= ~XBDF_FROZEN;
405fac3fd80SJustin T. Gibbs 
406fac3fd80SJustin T. Gibbs 		/*
407fac3fd80SJustin T. Gibbs 		 * Directly call the i/o complete routine to save an
408fac3fd80SJustin T. Gibbs 		 * an indirection in the common case.
409fac3fd80SJustin T. Gibbs 		 */
410fac3fd80SJustin T. Gibbs 		cm->cm_status = bret->status;
411fac3fd80SJustin T. Gibbs 		if (cm->cm_bp)
412fac3fd80SJustin T. Gibbs 			xbd_bio_complete(sc, cm);
413fac3fd80SJustin T. Gibbs 		else if (cm->cm_complete != NULL)
414fac3fd80SJustin T. Gibbs 			cm->cm_complete(cm);
415fac3fd80SJustin T. Gibbs 		else
416fac3fd80SJustin T. Gibbs 			xbd_free_command(cm);
417fac3fd80SJustin T. Gibbs 	}
418fac3fd80SJustin T. Gibbs 
419fac3fd80SJustin T. Gibbs 	sc->xbd_ring.rsp_cons = i;
420fac3fd80SJustin T. Gibbs 
421fac3fd80SJustin T. Gibbs 	if (i != sc->xbd_ring.req_prod_pvt) {
422fac3fd80SJustin T. Gibbs 		int more_to_do;
423fac3fd80SJustin T. Gibbs 		RING_FINAL_CHECK_FOR_RESPONSES(&sc->xbd_ring, more_to_do);
424fac3fd80SJustin T. Gibbs 		if (more_to_do)
425fac3fd80SJustin T. Gibbs 			goto again;
426fac3fd80SJustin T. Gibbs 	} else {
427fac3fd80SJustin T. Gibbs 		sc->xbd_ring.sring->rsp_event = i + 1;
428fac3fd80SJustin T. Gibbs 	}
429fac3fd80SJustin T. Gibbs 
430fac3fd80SJustin T. Gibbs 	xbd_startio(sc);
431fac3fd80SJustin T. Gibbs 
432*e2c1fe90SJustin T. Gibbs 	if (unlikely(sc->xbd_state == XBD_STATE_SUSPENDED))
433*e2c1fe90SJustin T. Gibbs 		wakeup(&sc->xbd_cm_q[XBD_Q_BUSY]);
434fac3fd80SJustin T. Gibbs 
435fac3fd80SJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
436fac3fd80SJustin T. Gibbs }
437fac3fd80SJustin T. Gibbs 
438fac3fd80SJustin T. Gibbs /*------------------------------- Dump Support -------------------------------*/
439fac3fd80SJustin T. Gibbs /**
440fac3fd80SJustin T. Gibbs  * Quiesce the disk writes for a dump file before allowing the next buffer.
441fac3fd80SJustin T. Gibbs  */
4428698b76cSKip Macy static void
44333eebb6aSJustin T. Gibbs xbd_quiesce(struct xbd_softc *sc)
4448698b76cSKip Macy {
4458698b76cSKip Macy 	int mtd;
4468698b76cSKip Macy 
4478698b76cSKip Macy 	// While there are outstanding requests
448*e2c1fe90SJustin T. Gibbs 	while (!TAILQ_EMPTY(&sc->xbd_cm_q[XBD_Q_BUSY].q_tailq)) {
44933eebb6aSJustin T. Gibbs 		RING_FINAL_CHECK_FOR_RESPONSES(&sc->xbd_ring, mtd);
4508698b76cSKip Macy 		if (mtd) {
451e4808c4bSKip Macy 			/* Recieved request completions, update queue. */
45233eebb6aSJustin T. Gibbs 			xbd_int(sc);
4538698b76cSKip Macy 		}
454*e2c1fe90SJustin T. Gibbs 		if (!TAILQ_EMPTY(&sc->xbd_cm_q[XBD_Q_BUSY].q_tailq)) {
455e4808c4bSKip Macy 			/*
456e4808c4bSKip Macy 			 * Still pending requests, wait for the disk i/o
457e4808c4bSKip Macy 			 * to complete.
458e4808c4bSKip Macy 			 */
459be7747b4SKip Macy 			HYPERVISOR_yield();
4608698b76cSKip Macy 		}
4618698b76cSKip Macy 	}
4628698b76cSKip Macy }
4638698b76cSKip Macy 
464e4808c4bSKip Macy /* Kernel dump function for a paravirtualized disk device */
465e4808c4bSKip Macy static void
46633eebb6aSJustin T. Gibbs xbd_dump_complete(struct xbd_command *cm)
467e4808c4bSKip Macy {
4688698b76cSKip Macy 
469*e2c1fe90SJustin T. Gibbs 	xbd_enqueue_cm(cm, XBD_Q_COMPLETE);
470e4808c4bSKip Macy }
471e4808c4bSKip Macy 
4728698b76cSKip Macy static int
47333eebb6aSJustin T. Gibbs xbd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset,
4748698b76cSKip Macy     size_t length)
4758698b76cSKip Macy {
4768698b76cSKip Macy 	struct disk *dp = arg;
47733eebb6aSJustin T. Gibbs 	struct xbd_softc *sc = dp->d_drv1;
47833eebb6aSJustin T. Gibbs 	struct xbd_command *cm;
479e4808c4bSKip Macy 	size_t chunk;
480e4808c4bSKip Macy 	int sbp;
4818698b76cSKip Macy 	int rc = 0;
4828698b76cSKip Macy 
483e4808c4bSKip Macy 	if (length <= 0)
484e4808c4bSKip Macy 		return (rc);
4858698b76cSKip Macy 
48633eebb6aSJustin T. Gibbs 	xbd_quiesce(sc);	/* All quiet on the western front. */
487e4808c4bSKip Macy 
488e4808c4bSKip Macy 	/*
489e4808c4bSKip Macy 	 * If this lock is held, then this module is failing, and a
490e4808c4bSKip Macy 	 * successful kernel dump is highly unlikely anyway.
491e4808c4bSKip Macy 	 */
49233eebb6aSJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
493e4808c4bSKip Macy 
494e4808c4bSKip Macy 	/* Split the 64KB block as needed */
495e4808c4bSKip Macy 	for (sbp=0; length > 0; sbp++) {
496*e2c1fe90SJustin T. Gibbs 		cm = xbd_dequeue_cm(sc, XBD_Q_FREE);
497e4808c4bSKip Macy 		if (cm == NULL) {
49833eebb6aSJustin T. Gibbs 			mtx_unlock(&sc->xbd_io_lock);
49933eebb6aSJustin T. Gibbs 			device_printf(sc->xbd_dev, "dump: no more commands?\n");
500e4808c4bSKip Macy 			return (EBUSY);
501e4808c4bSKip Macy 		}
502e4808c4bSKip Macy 
50333eebb6aSJustin T. Gibbs 		if (gnttab_alloc_grant_references(sc->xbd_max_request_segments,
50433eebb6aSJustin T. Gibbs 		    &cm->cm_gref_head) != 0) {
50533eebb6aSJustin T. Gibbs 			xbd_free_command(cm);
50633eebb6aSJustin T. Gibbs 			mtx_unlock(&sc->xbd_io_lock);
50733eebb6aSJustin T. Gibbs 			device_printf(sc->xbd_dev, "no more grant allocs?\n");
508e4808c4bSKip Macy 			return (EBUSY);
509e4808c4bSKip Macy 		}
510e4808c4bSKip Macy 
51133eebb6aSJustin T. Gibbs 		chunk = length > sc->xbd_max_request_size ?
51233eebb6aSJustin T. Gibbs 		    sc->xbd_max_request_size : length;
51333eebb6aSJustin T. Gibbs 		cm->cm_data = virtual;
51433eebb6aSJustin T. Gibbs 		cm->cm_datalen = chunk;
51533eebb6aSJustin T. Gibbs 		cm->cm_operation = BLKIF_OP_WRITE;
51633eebb6aSJustin T. Gibbs 		cm->cm_sector_number = offset / dp->d_sectorsize;
51733eebb6aSJustin T. Gibbs 		cm->cm_complete = xbd_dump_complete;
518e4808c4bSKip Macy 
519*e2c1fe90SJustin T. Gibbs 		xbd_enqueue_cm(cm, XBD_Q_READY);
5208698b76cSKip Macy 
5218698b76cSKip Macy 		length -= chunk;
5228698b76cSKip Macy 		offset += chunk;
5238698b76cSKip Macy 		virtual = (char *) virtual + chunk;
5248698b76cSKip Macy 	}
5258698b76cSKip Macy 
526e4808c4bSKip Macy 	/* Tell DOM0 to do the I/O */
52733eebb6aSJustin T. Gibbs 	xbd_startio(sc);
52833eebb6aSJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
529e4808c4bSKip Macy 
530e4808c4bSKip Macy 	/* Poll for the completion. */
53133eebb6aSJustin T. Gibbs 	xbd_quiesce(sc);	/* All quite on the eastern front */
532e4808c4bSKip Macy 
533e4808c4bSKip Macy 	/* If there were any errors, bail out... */
534*e2c1fe90SJustin T. Gibbs 	while ((cm = xbd_dequeue_cm(sc, XBD_Q_COMPLETE)) != NULL) {
53533eebb6aSJustin T. Gibbs 		if (cm->cm_status != BLKIF_RSP_OKAY) {
53633eebb6aSJustin T. Gibbs 			device_printf(sc->xbd_dev,
537e4808c4bSKip Macy 			    "Dump I/O failed at sector %jd\n",
53833eebb6aSJustin T. Gibbs 			    cm->cm_sector_number);
539e4808c4bSKip Macy 			rc = EIO;
5408698b76cSKip Macy 		}
54133eebb6aSJustin T. Gibbs 		xbd_free_command(cm);
5428698b76cSKip Macy 	}
543e4808c4bSKip Macy 
5448698b76cSKip Macy 	return (rc);
5458698b76cSKip Macy }
5468698b76cSKip Macy 
547fac3fd80SJustin T. Gibbs /*----------------------------- Disk Entrypoints -----------------------------*/
54823dc5621SKip Macy static int
549fac3fd80SJustin T. Gibbs xbd_open(struct disk *dp)
55089e0f4d2SKip Macy {
551fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = dp->d_drv1;
55223dc5621SKip Macy 
553fac3fd80SJustin T. Gibbs 	if (sc == NULL) {
554fac3fd80SJustin T. Gibbs 		printf("xb%d: not found", sc->xbd_unit);
55523dc5621SKip Macy 		return (ENXIO);
55623dc5621SKip Macy 	}
55723dc5621SKip Macy 
558*e2c1fe90SJustin T. Gibbs 	sc->xbd_flags |= XBDF_OPEN;
559fac3fd80SJustin T. Gibbs 	sc->xbd_users++;
560fac3fd80SJustin T. Gibbs 	return (0);
561fac3fd80SJustin T. Gibbs }
562fac3fd80SJustin T. Gibbs 
563fac3fd80SJustin T. Gibbs static int
564fac3fd80SJustin T. Gibbs xbd_close(struct disk *dp)
565fac3fd80SJustin T. Gibbs {
566fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = dp->d_drv1;
567fac3fd80SJustin T. Gibbs 
568fac3fd80SJustin T. Gibbs 	if (sc == NULL)
569fac3fd80SJustin T. Gibbs 		return (ENXIO);
570*e2c1fe90SJustin T. Gibbs 	sc->xbd_flags &= ~XBDF_OPEN;
571fac3fd80SJustin T. Gibbs 	if (--(sc->xbd_users) == 0) {
572fac3fd80SJustin T. Gibbs 		/*
573fac3fd80SJustin T. Gibbs 		 * Check whether we have been instructed to close.  We will
574fac3fd80SJustin T. Gibbs 		 * have ignored this request initially, as the device was
575fac3fd80SJustin T. Gibbs 		 * still mounted.
576fac3fd80SJustin T. Gibbs 		 */
577fac3fd80SJustin T. Gibbs 		if (xenbus_get_otherend_state(sc->xbd_dev) ==
578fac3fd80SJustin T. Gibbs 		    XenbusStateClosing)
579fac3fd80SJustin T. Gibbs 			xbd_closing(sc->xbd_dev);
580fac3fd80SJustin T. Gibbs 	}
581fac3fd80SJustin T. Gibbs 	return (0);
582fac3fd80SJustin T. Gibbs }
583fac3fd80SJustin T. Gibbs 
584fac3fd80SJustin T. Gibbs static int
585fac3fd80SJustin T. Gibbs xbd_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
586fac3fd80SJustin T. Gibbs {
587fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = dp->d_drv1;
588fac3fd80SJustin T. Gibbs 
589fac3fd80SJustin T. Gibbs 	if (sc == NULL)
590fac3fd80SJustin T. Gibbs 		return (ENXIO);
591fac3fd80SJustin T. Gibbs 
592fac3fd80SJustin T. Gibbs 	return (ENOTTY);
593fac3fd80SJustin T. Gibbs }
594fac3fd80SJustin T. Gibbs 
595fac3fd80SJustin T. Gibbs /*
596fac3fd80SJustin T. Gibbs  * Read/write routine for a buffer.  Finds the proper unit, place it on
597fac3fd80SJustin T. Gibbs  * the sortq and kick the controller.
598fac3fd80SJustin T. Gibbs  */
599fac3fd80SJustin T. Gibbs static void
600fac3fd80SJustin T. Gibbs xbd_strategy(struct bio *bp)
601fac3fd80SJustin T. Gibbs {
602fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = bp->bio_disk->d_drv1;
603fac3fd80SJustin T. Gibbs 
604fac3fd80SJustin T. Gibbs 	/* bogus disk? */
605fac3fd80SJustin T. Gibbs 	if (sc == NULL) {
606fac3fd80SJustin T. Gibbs 		bp->bio_error = EINVAL;
607fac3fd80SJustin T. Gibbs 		bp->bio_flags |= BIO_ERROR;
608fac3fd80SJustin T. Gibbs 		bp->bio_resid = bp->bio_bcount;
609fac3fd80SJustin T. Gibbs 		biodone(bp);
610fac3fd80SJustin T. Gibbs 		return;
611fac3fd80SJustin T. Gibbs 	}
612fac3fd80SJustin T. Gibbs 
613fac3fd80SJustin T. Gibbs 	/*
614fac3fd80SJustin T. Gibbs 	 * Place it in the queue of disk activities for this disk
615fac3fd80SJustin T. Gibbs 	 */
616fac3fd80SJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
617fac3fd80SJustin T. Gibbs 
618fac3fd80SJustin T. Gibbs 	xbd_enqueue_bio(sc, bp);
619fac3fd80SJustin T. Gibbs 	xbd_startio(sc);
620fac3fd80SJustin T. Gibbs 
621fac3fd80SJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
622fac3fd80SJustin T. Gibbs 	return;
623fac3fd80SJustin T. Gibbs }
624fac3fd80SJustin T. Gibbs 
625fac3fd80SJustin T. Gibbs /*------------------------------ Ring Management -----------------------------*/
626fac3fd80SJustin T. Gibbs static int
627cdf5d66fSJustin T. Gibbs xbd_alloc_ring(struct xbd_softc *sc)
628fac3fd80SJustin T. Gibbs {
629fac3fd80SJustin T. Gibbs 	blkif_sring_t *sring;
630fac3fd80SJustin T. Gibbs 	uintptr_t sring_page_addr;
631fac3fd80SJustin T. Gibbs 	int error;
632fac3fd80SJustin T. Gibbs 	int i;
633fac3fd80SJustin T. Gibbs 
634fac3fd80SJustin T. Gibbs 	sring = malloc(sc->xbd_ring_pages * PAGE_SIZE, M_XENBLOCKFRONT,
635fac3fd80SJustin T. Gibbs 	    M_NOWAIT|M_ZERO);
636fac3fd80SJustin T. Gibbs 	if (sring == NULL) {
637fac3fd80SJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, ENOMEM, "allocating shared ring");
638fac3fd80SJustin T. Gibbs 		return (ENOMEM);
639fac3fd80SJustin T. Gibbs 	}
640fac3fd80SJustin T. Gibbs 	SHARED_RING_INIT(sring);
641fac3fd80SJustin T. Gibbs 	FRONT_RING_INIT(&sc->xbd_ring, sring, sc->xbd_ring_pages * PAGE_SIZE);
642fac3fd80SJustin T. Gibbs 
643fac3fd80SJustin T. Gibbs 	for (i = 0, sring_page_addr = (uintptr_t)sring;
644fac3fd80SJustin T. Gibbs 	     i < sc->xbd_ring_pages;
645fac3fd80SJustin T. Gibbs 	     i++, sring_page_addr += PAGE_SIZE) {
646fac3fd80SJustin T. Gibbs 
647fac3fd80SJustin T. Gibbs 		error = xenbus_grant_ring(sc->xbd_dev,
648fac3fd80SJustin T. Gibbs 		    (vtomach(sring_page_addr) >> PAGE_SHIFT),
649fac3fd80SJustin T. Gibbs 		    &sc->xbd_ring_ref[i]);
650fac3fd80SJustin T. Gibbs 		if (error) {
651fac3fd80SJustin T. Gibbs 			xenbus_dev_fatal(sc->xbd_dev, error,
652fac3fd80SJustin T. Gibbs 			    "granting ring_ref(%d)", i);
653fac3fd80SJustin T. Gibbs 			return (error);
654fac3fd80SJustin T. Gibbs 		}
655fac3fd80SJustin T. Gibbs 	}
656fac3fd80SJustin T. Gibbs 	if (sc->xbd_ring_pages == 1) {
657fac3fd80SJustin T. Gibbs 		error = xs_printf(XST_NIL, xenbus_get_node(sc->xbd_dev),
658fac3fd80SJustin T. Gibbs 		    "ring-ref", "%u", sc->xbd_ring_ref[0]);
659fac3fd80SJustin T. Gibbs 		if (error) {
660fac3fd80SJustin T. Gibbs 			xenbus_dev_fatal(sc->xbd_dev, error,
661fac3fd80SJustin T. Gibbs 			    "writing %s/ring-ref",
662fac3fd80SJustin T. Gibbs 			    xenbus_get_node(sc->xbd_dev));
663fac3fd80SJustin T. Gibbs 			return (error);
664fac3fd80SJustin T. Gibbs 		}
665fac3fd80SJustin T. Gibbs 	} else {
666fac3fd80SJustin T. Gibbs 		for (i = 0; i < sc->xbd_ring_pages; i++) {
667fac3fd80SJustin T. Gibbs 			char ring_ref_name[]= "ring_refXX";
668fac3fd80SJustin T. Gibbs 
669fac3fd80SJustin T. Gibbs 			snprintf(ring_ref_name, sizeof(ring_ref_name),
670fac3fd80SJustin T. Gibbs 			    "ring-ref%u", i);
671fac3fd80SJustin T. Gibbs 			error = xs_printf(XST_NIL, xenbus_get_node(sc->xbd_dev),
672fac3fd80SJustin T. Gibbs 			     ring_ref_name, "%u", sc->xbd_ring_ref[i]);
673fac3fd80SJustin T. Gibbs 			if (error) {
674fac3fd80SJustin T. Gibbs 				xenbus_dev_fatal(sc->xbd_dev, error,
675fac3fd80SJustin T. Gibbs 				    "writing %s/%s",
676fac3fd80SJustin T. Gibbs 				    xenbus_get_node(sc->xbd_dev),
677fac3fd80SJustin T. Gibbs 				    ring_ref_name);
678fac3fd80SJustin T. Gibbs 				return (error);
679fac3fd80SJustin T. Gibbs 			}
680fac3fd80SJustin T. Gibbs 		}
681fac3fd80SJustin T. Gibbs 	}
682fac3fd80SJustin T. Gibbs 
683fac3fd80SJustin T. Gibbs 	error = bind_listening_port_to_irqhandler(
684fac3fd80SJustin T. Gibbs 	    xenbus_get_otherend_id(sc->xbd_dev),
685fac3fd80SJustin T. Gibbs 	    "xbd", (driver_intr_t *)xbd_int, sc,
686fac3fd80SJustin T. Gibbs 	    INTR_TYPE_BIO | INTR_MPSAFE, &sc->xbd_irq);
687fac3fd80SJustin T. Gibbs 	if (error) {
688fac3fd80SJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
689fac3fd80SJustin T. Gibbs 		    "bind_evtchn_to_irqhandler failed");
690fac3fd80SJustin T. Gibbs 		return (error);
691fac3fd80SJustin T. Gibbs 	}
692fac3fd80SJustin T. Gibbs 
693fac3fd80SJustin T. Gibbs 	return (0);
694fac3fd80SJustin T. Gibbs }
695fac3fd80SJustin T. Gibbs 
696d9fab01dSJustin T. Gibbs static void
697d9fab01dSJustin T. Gibbs xbd_free_ring(struct xbd_softc *sc)
698d9fab01dSJustin T. Gibbs {
699d9fab01dSJustin T. Gibbs 	int i;
700d9fab01dSJustin T. Gibbs 
701d9fab01dSJustin T. Gibbs 	if (sc->xbd_ring.sring == NULL)
702d9fab01dSJustin T. Gibbs 		return;
703d9fab01dSJustin T. Gibbs 
704d9fab01dSJustin T. Gibbs 	for (i = 0; i < sc->xbd_ring_pages; i++) {
705d9fab01dSJustin T. Gibbs 		if (sc->xbd_ring_ref[i] != GRANT_REF_INVALID) {
706d9fab01dSJustin T. Gibbs 			gnttab_end_foreign_access_ref(sc->xbd_ring_ref[i]);
707d9fab01dSJustin T. Gibbs 			sc->xbd_ring_ref[i] = GRANT_REF_INVALID;
708d9fab01dSJustin T. Gibbs 		}
709d9fab01dSJustin T. Gibbs 	}
710d9fab01dSJustin T. Gibbs 	free(sc->xbd_ring.sring, M_XENBLOCKFRONT);
711d9fab01dSJustin T. Gibbs 	sc->xbd_ring.sring = NULL;
712d9fab01dSJustin T. Gibbs }
713d9fab01dSJustin T. Gibbs 
714fac3fd80SJustin T. Gibbs /*-------------------------- Initialization/Teardown -------------------------*/
7158b8bfa35SJustin T. Gibbs static void
71633eebb6aSJustin T. Gibbs xbd_setup_sysctl(struct xbd_softc *xbd)
7178b8bfa35SJustin T. Gibbs {
7188b8bfa35SJustin T. Gibbs 	struct sysctl_ctx_list *sysctl_ctx = NULL;
7198b8bfa35SJustin T. Gibbs 	struct sysctl_oid *sysctl_tree = NULL;
7208b8bfa35SJustin T. Gibbs 
72133eebb6aSJustin T. Gibbs 	sysctl_ctx = device_get_sysctl_ctx(xbd->xbd_dev);
7228b8bfa35SJustin T. Gibbs 	if (sysctl_ctx == NULL)
7238b8bfa35SJustin T. Gibbs 		return;
7248b8bfa35SJustin T. Gibbs 
72533eebb6aSJustin T. Gibbs 	sysctl_tree = device_get_sysctl_tree(xbd->xbd_dev);
7268b8bfa35SJustin T. Gibbs 	if (sysctl_tree == NULL)
7278b8bfa35SJustin T. Gibbs 		return;
7288b8bfa35SJustin T. Gibbs 
7298b8bfa35SJustin T. Gibbs 	SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
73033eebb6aSJustin T. Gibbs 	    "max_requests", CTLFLAG_RD, &xbd->xbd_max_requests, -1,
7318b8bfa35SJustin T. Gibbs 	    "maximum outstanding requests (negotiated)");
7328b8bfa35SJustin T. Gibbs 
7338b8bfa35SJustin T. Gibbs 	SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
7348b8bfa35SJustin T. Gibbs 	    "max_request_segments", CTLFLAG_RD,
73533eebb6aSJustin T. Gibbs 	    &xbd->xbd_max_request_segments, 0,
7368b8bfa35SJustin T. Gibbs 	    "maximum number of pages per requests (negotiated)");
7378b8bfa35SJustin T. Gibbs 
7388b8bfa35SJustin T. Gibbs 	SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
73933eebb6aSJustin T. Gibbs 	    "max_request_size", CTLFLAG_RD, &xbd->xbd_max_request_size, 0,
7408b8bfa35SJustin T. Gibbs 	    "maximum size in bytes of a request (negotiated)");
7418b8bfa35SJustin T. Gibbs 
7428b8bfa35SJustin T. Gibbs 	SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
74333eebb6aSJustin T. Gibbs 	    "ring_pages", CTLFLAG_RD, &xbd->xbd_ring_pages, 0,
7448b8bfa35SJustin T. Gibbs 	    "communication channel pages (negotiated)");
7458b8bfa35SJustin T. Gibbs }
7468b8bfa35SJustin T. Gibbs 
74723dc5621SKip Macy /*
748fac3fd80SJustin T. Gibbs  * Translate Linux major/minor to an appropriate name and unit
749fac3fd80SJustin T. Gibbs  * number. For HVM guests, this allows us to use the same drive names
750fac3fd80SJustin T. Gibbs  * with blkfront as the emulated drives, easing transition slightly.
75123dc5621SKip Macy  */
752fac3fd80SJustin T. Gibbs static void
753fac3fd80SJustin T. Gibbs xbd_vdevice_to_unit(uint32_t vdevice, int *unit, const char **name)
75423dc5621SKip Macy {
755fac3fd80SJustin T. Gibbs 	static struct vdev_info {
756fac3fd80SJustin T. Gibbs 		int major;
757fac3fd80SJustin T. Gibbs 		int shift;
758fac3fd80SJustin T. Gibbs 		int base;
75923dc5621SKip Macy 		const char *name;
760fac3fd80SJustin T. Gibbs 	} info[] = {
761fac3fd80SJustin T. Gibbs 		{3,	6,	0,	"ada"},	/* ide0 */
762fac3fd80SJustin T. Gibbs 		{22,	6,	2,	"ada"},	/* ide1 */
763fac3fd80SJustin T. Gibbs 		{33,	6,	4,	"ada"},	/* ide2 */
764fac3fd80SJustin T. Gibbs 		{34,	6,	6,	"ada"},	/* ide3 */
765fac3fd80SJustin T. Gibbs 		{56,	6,	8,	"ada"},	/* ide4 */
766fac3fd80SJustin T. Gibbs 		{57,	6,	10,	"ada"},	/* ide5 */
767fac3fd80SJustin T. Gibbs 		{88,	6,	12,	"ada"},	/* ide6 */
768fac3fd80SJustin T. Gibbs 		{89,	6,	14,	"ada"},	/* ide7 */
769fac3fd80SJustin T. Gibbs 		{90,	6,	16,	"ada"},	/* ide8 */
770fac3fd80SJustin T. Gibbs 		{91,	6,	18,	"ada"},	/* ide9 */
77189e0f4d2SKip Macy 
772fac3fd80SJustin T. Gibbs 		{8,	4,	0,	"da"},	/* scsi disk0 */
773fac3fd80SJustin T. Gibbs 		{65,	4,	16,	"da"},	/* scsi disk1 */
774fac3fd80SJustin T. Gibbs 		{66,	4,	32,	"da"},	/* scsi disk2 */
775fac3fd80SJustin T. Gibbs 		{67,	4,	48,	"da"},	/* scsi disk3 */
776fac3fd80SJustin T. Gibbs 		{68,	4,	64,	"da"},	/* scsi disk4 */
777fac3fd80SJustin T. Gibbs 		{69,	4,	80,	"da"},	/* scsi disk5 */
778fac3fd80SJustin T. Gibbs 		{70,	4,	96,	"da"},	/* scsi disk6 */
779fac3fd80SJustin T. Gibbs 		{71,	4,	112,	"da"},	/* scsi disk7 */
780fac3fd80SJustin T. Gibbs 		{128,	4,	128,	"da"},	/* scsi disk8 */
781fac3fd80SJustin T. Gibbs 		{129,	4,	144,	"da"},	/* scsi disk9 */
782fac3fd80SJustin T. Gibbs 		{130,	4,	160,	"da"},	/* scsi disk10 */
783fac3fd80SJustin T. Gibbs 		{131,	4,	176,	"da"},	/* scsi disk11 */
784fac3fd80SJustin T. Gibbs 		{132,	4,	192,	"da"},	/* scsi disk12 */
785fac3fd80SJustin T. Gibbs 		{133,	4,	208,	"da"},	/* scsi disk13 */
786fac3fd80SJustin T. Gibbs 		{134,	4,	224,	"da"},	/* scsi disk14 */
787fac3fd80SJustin T. Gibbs 		{135,	4,	240,	"da"},	/* scsi disk15 */
788fac3fd80SJustin T. Gibbs 
789fac3fd80SJustin T. Gibbs 		{202,	4,	0,	"xbd"},	/* xbd */
790fac3fd80SJustin T. Gibbs 
791fac3fd80SJustin T. Gibbs 		{0,	0,	0,	NULL},
792fac3fd80SJustin T. Gibbs 	};
793fac3fd80SJustin T. Gibbs 	int major = vdevice >> 8;
794fac3fd80SJustin T. Gibbs 	int minor = vdevice & 0xff;
795fac3fd80SJustin T. Gibbs 	int i;
796fac3fd80SJustin T. Gibbs 
797fac3fd80SJustin T. Gibbs 	if (vdevice & (1 << 28)) {
798fac3fd80SJustin T. Gibbs 		*unit = (vdevice & ((1 << 28) - 1)) >> 8;
799fac3fd80SJustin T. Gibbs 		*name = "xbd";
800fac3fd80SJustin T. Gibbs 		return;
80189e0f4d2SKip Macy 	}
80289e0f4d2SKip Macy 
803fac3fd80SJustin T. Gibbs 	for (i = 0; info[i].major; i++) {
804fac3fd80SJustin T. Gibbs 		if (info[i].major == major) {
805fac3fd80SJustin T. Gibbs 			*unit = info[i].base + (minor >> info[i].shift);
806fac3fd80SJustin T. Gibbs 			*name = info[i].name;
807fac3fd80SJustin T. Gibbs 			return;
808fac3fd80SJustin T. Gibbs 		}
809fac3fd80SJustin T. Gibbs 	}
810fac3fd80SJustin T. Gibbs 
811fac3fd80SJustin T. Gibbs 	*unit = minor >> 4;
812fac3fd80SJustin T. Gibbs 	*name = "xbd";
813fac3fd80SJustin T. Gibbs }
814fac3fd80SJustin T. Gibbs 
815fac3fd80SJustin T. Gibbs int
816fac3fd80SJustin T. Gibbs xbd_instance_create(struct xbd_softc *sc, blkif_sector_t sectors,
817fac3fd80SJustin T. Gibbs     int vdevice, uint16_t vdisk_info, unsigned long sector_size)
818fac3fd80SJustin T. Gibbs {
819fac3fd80SJustin T. Gibbs 	int unit, error = 0;
820fac3fd80SJustin T. Gibbs 	const char *name;
821fac3fd80SJustin T. Gibbs 
82233eebb6aSJustin T. Gibbs 	xbd_vdevice_to_unit(vdevice, &unit, &name);
82323dc5621SKip Macy 
824fac3fd80SJustin T. Gibbs 	sc->xbd_unit = unit;
82589e0f4d2SKip Macy 
826fac3fd80SJustin T. Gibbs 	if (strcmp(name, "xbd"))
827fac3fd80SJustin T. Gibbs 		device_printf(sc->xbd_dev, "attaching as %s%d\n", name, unit);
82889e0f4d2SKip Macy 
829fac3fd80SJustin T. Gibbs 	sc->xbd_disk = disk_alloc();
830fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_unit = sc->xbd_unit;
831fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_open = xbd_open;
832fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_close = xbd_close;
833fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_ioctl = xbd_ioctl;
834fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_strategy = xbd_strategy;
835fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_dump = xbd_dump;
836fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_name = name;
837fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_drv1 = sc;
838fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_sectorsize = sector_size;
8398b8bfa35SJustin T. Gibbs 
840fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_mediasize = sectors * sector_size;
841fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_maxsize = sc->xbd_max_request_size;
842fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_flags = 0;
843fac3fd80SJustin T. Gibbs 	disk_create(sc->xbd_disk, DISK_VERSION);
84412678024SDoug Rabson 
845fac3fd80SJustin T. Gibbs 	return error;
84612678024SDoug Rabson }
84712678024SDoug Rabson 
848fac3fd80SJustin T. Gibbs static void
849fac3fd80SJustin T. Gibbs xbd_free(struct xbd_softc *sc)
85012678024SDoug Rabson {
851fac3fd80SJustin T. Gibbs 	int i;
85212678024SDoug Rabson 
85312678024SDoug Rabson 	/* Prevent new requests being issued until we fix things up. */
85433eebb6aSJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
855*e2c1fe90SJustin T. Gibbs 	sc->xbd_state = XBD_STATE_DISCONNECTED;
85633eebb6aSJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
85789e0f4d2SKip Macy 
858fac3fd80SJustin T. Gibbs 	/* Free resources associated with old device channel. */
859d9fab01dSJustin T. Gibbs 	xbd_free_ring(sc);
860fac3fd80SJustin T. Gibbs 	if (sc->xbd_shadow) {
86189e0f4d2SKip Macy 
862fac3fd80SJustin T. Gibbs 		for (i = 0; i < sc->xbd_max_requests; i++) {
863fac3fd80SJustin T. Gibbs 			struct xbd_command *cm;
86489e0f4d2SKip Macy 
865fac3fd80SJustin T. Gibbs 			cm = &sc->xbd_shadow[i];
866fac3fd80SJustin T. Gibbs 			if (cm->cm_sg_refs != NULL) {
867fac3fd80SJustin T. Gibbs 				free(cm->cm_sg_refs, M_XENBLOCKFRONT);
868fac3fd80SJustin T. Gibbs 				cm->cm_sg_refs = NULL;
86989e0f4d2SKip Macy 			}
87089e0f4d2SKip Macy 
871fac3fd80SJustin T. Gibbs 			bus_dmamap_destroy(sc->xbd_io_dmat, cm->cm_map);
872fac3fd80SJustin T. Gibbs 		}
873fac3fd80SJustin T. Gibbs 		free(sc->xbd_shadow, M_XENBLOCKFRONT);
874fac3fd80SJustin T. Gibbs 		sc->xbd_shadow = NULL;
875fac3fd80SJustin T. Gibbs 
876fac3fd80SJustin T. Gibbs 		bus_dma_tag_destroy(sc->xbd_io_dmat);
877fac3fd80SJustin T. Gibbs 
878*e2c1fe90SJustin T. Gibbs 		xbd_initq_cm(sc, XBD_Q_FREE);
879*e2c1fe90SJustin T. Gibbs 		xbd_initq_cm(sc, XBD_Q_READY);
880*e2c1fe90SJustin T. Gibbs 		xbd_initq_cm(sc, XBD_Q_COMPLETE);
881fac3fd80SJustin T. Gibbs 	}
882fac3fd80SJustin T. Gibbs 
883fac3fd80SJustin T. Gibbs 	if (sc->xbd_irq) {
884fac3fd80SJustin T. Gibbs 		unbind_from_irqhandler(sc->xbd_irq);
885fac3fd80SJustin T. Gibbs 		sc->xbd_irq = 0;
886fac3fd80SJustin T. Gibbs 	}
887fac3fd80SJustin T. Gibbs }
888fac3fd80SJustin T. Gibbs 
889fac3fd80SJustin T. Gibbs /*--------------------------- State Change Handlers --------------------------*/
890ff662b5cSJustin T. Gibbs static void
89133eebb6aSJustin T. Gibbs xbd_initialize(struct xbd_softc *sc)
89289e0f4d2SKip Macy {
893ff662b5cSJustin T. Gibbs 	const char *otherend_path;
894ff662b5cSJustin T. Gibbs 	const char *node_path;
8958b8bfa35SJustin T. Gibbs 	uint32_t max_ring_page_order;
896ff662b5cSJustin T. Gibbs 	int error;
897ff662b5cSJustin T. Gibbs 	int i;
89889e0f4d2SKip Macy 
89933eebb6aSJustin T. Gibbs 	if (xenbus_get_state(sc->xbd_dev) != XenbusStateInitialising) {
90006a630f6SJustin T. Gibbs 		/* Initialization has already been performed. */
901ff662b5cSJustin T. Gibbs 		return;
90206a630f6SJustin T. Gibbs 	}
90389e0f4d2SKip Macy 
904ff662b5cSJustin T. Gibbs 	/*
905ff662b5cSJustin T. Gibbs 	 * Protocol defaults valid even if negotiation for a
906ff662b5cSJustin T. Gibbs 	 * setting fails.
907ff662b5cSJustin T. Gibbs 	 */
9088b8bfa35SJustin T. Gibbs 	max_ring_page_order = 0;
90933eebb6aSJustin T. Gibbs 	sc->xbd_ring_pages = 1;
91033eebb6aSJustin T. Gibbs 	sc->xbd_max_request_segments = BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK;
91133eebb6aSJustin T. Gibbs 	sc->xbd_max_request_size =
91233eebb6aSJustin T. Gibbs 	    XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments);
91333eebb6aSJustin T. Gibbs 	sc->xbd_max_request_blocks =
91433eebb6aSJustin T. Gibbs 	    BLKIF_SEGS_TO_BLOCKS(sc->xbd_max_request_segments);
915ff662b5cSJustin T. Gibbs 
916ff662b5cSJustin T. Gibbs 	/*
917ff662b5cSJustin T. Gibbs 	 * Protocol negotiation.
918ff662b5cSJustin T. Gibbs 	 *
919ff662b5cSJustin T. Gibbs 	 * \note xs_gather() returns on the first encountered error, so
920ff662b5cSJustin T. Gibbs 	 *       we must use independant calls in order to guarantee
921ff662b5cSJustin T. Gibbs 	 *       we don't miss information in a sparsly populated back-end
922ff662b5cSJustin T. Gibbs 	 *       tree.
9238b8bfa35SJustin T. Gibbs 	 *
9248b8bfa35SJustin T. Gibbs 	 * \note xs_scanf() does not update variables for unmatched
9258b8bfa35SJustin T. Gibbs 	 *	 fields.
926ff662b5cSJustin T. Gibbs 	 */
92733eebb6aSJustin T. Gibbs 	otherend_path = xenbus_get_otherend_path(sc->xbd_dev);
92833eebb6aSJustin T. Gibbs 	node_path = xenbus_get_node(sc->xbd_dev);
9298b8bfa35SJustin T. Gibbs 
9308b8bfa35SJustin T. Gibbs 	/* Support both backend schemes for relaying ring page limits. */
9318b8bfa35SJustin T. Gibbs 	(void)xs_scanf(XST_NIL, otherend_path,
9328b8bfa35SJustin T. Gibbs 	    "max-ring-page-order", NULL, "%" PRIu32,
9338b8bfa35SJustin T. Gibbs 	    &max_ring_page_order);
93433eebb6aSJustin T. Gibbs 	sc->xbd_ring_pages = 1 << max_ring_page_order;
935ff662b5cSJustin T. Gibbs 	(void)xs_scanf(XST_NIL, otherend_path,
936ff662b5cSJustin T. Gibbs 	    "max-ring-pages", NULL, "%" PRIu32,
93733eebb6aSJustin T. Gibbs 	    &sc->xbd_ring_pages);
93833eebb6aSJustin T. Gibbs 	if (sc->xbd_ring_pages < 1)
93933eebb6aSJustin T. Gibbs 		sc->xbd_ring_pages = 1;
940ff662b5cSJustin T. Gibbs 
94133eebb6aSJustin T. Gibbs 	sc->xbd_max_requests =
94233eebb6aSJustin T. Gibbs 	    BLKIF_MAX_RING_REQUESTS(sc->xbd_ring_pages * PAGE_SIZE);
943ff662b5cSJustin T. Gibbs 	(void)xs_scanf(XST_NIL, otherend_path,
944ff662b5cSJustin T. Gibbs 	    "max-requests", NULL, "%" PRIu32,
94533eebb6aSJustin T. Gibbs 	    &sc->xbd_max_requests);
946ff662b5cSJustin T. Gibbs 
947ff662b5cSJustin T. Gibbs 	(void)xs_scanf(XST_NIL, otherend_path,
948ff662b5cSJustin T. Gibbs 	    "max-request-segments", NULL, "%" PRIu32,
94933eebb6aSJustin T. Gibbs 	    &sc->xbd_max_request_segments);
950ff662b5cSJustin T. Gibbs 
951ff662b5cSJustin T. Gibbs 	(void)xs_scanf(XST_NIL, otherend_path,
952ff662b5cSJustin T. Gibbs 	    "max-request-size", NULL, "%" PRIu32,
95333eebb6aSJustin T. Gibbs 	    &sc->xbd_max_request_size);
954ff662b5cSJustin T. Gibbs 
95533eebb6aSJustin T. Gibbs 	if (sc->xbd_ring_pages > XBD_MAX_RING_PAGES) {
95633eebb6aSJustin T. Gibbs 		device_printf(sc->xbd_dev,
95733eebb6aSJustin T. Gibbs 		    "Back-end specified ring-pages of %u "
95833eebb6aSJustin T. Gibbs 		    "limited to front-end limit of %zu.\n",
95933eebb6aSJustin T. Gibbs 		    sc->xbd_ring_pages, XBD_MAX_RING_PAGES);
96033eebb6aSJustin T. Gibbs 		sc->xbd_ring_pages = XBD_MAX_RING_PAGES;
96189e0f4d2SKip Macy 	}
96289e0f4d2SKip Macy 
96333eebb6aSJustin T. Gibbs 	if (powerof2(sc->xbd_ring_pages) == 0) {
9648b8bfa35SJustin T. Gibbs 		uint32_t new_page_limit;
9658b8bfa35SJustin T. Gibbs 
96633eebb6aSJustin T. Gibbs 		new_page_limit = 0x01 << (fls(sc->xbd_ring_pages) - 1);
96733eebb6aSJustin T. Gibbs 		device_printf(sc->xbd_dev,
96833eebb6aSJustin T. Gibbs 		    "Back-end specified ring-pages of %u "
96933eebb6aSJustin T. Gibbs 		    "is not a power of 2. Limited to %u.\n",
97033eebb6aSJustin T. Gibbs 		    sc->xbd_ring_pages, new_page_limit);
97133eebb6aSJustin T. Gibbs 		sc->xbd_ring_pages = new_page_limit;
9728b8bfa35SJustin T. Gibbs 	}
9738b8bfa35SJustin T. Gibbs 
97433eebb6aSJustin T. Gibbs 	if (sc->xbd_max_requests > XBD_MAX_REQUESTS) {
97533eebb6aSJustin T. Gibbs 		device_printf(sc->xbd_dev,
97633eebb6aSJustin T. Gibbs 		    "Back-end specified max_requests of %u "
97733eebb6aSJustin T. Gibbs 		    "limited to front-end limit of %u.\n",
97833eebb6aSJustin T. Gibbs 		    sc->xbd_max_requests, XBD_MAX_REQUESTS);
97933eebb6aSJustin T. Gibbs 		sc->xbd_max_requests = XBD_MAX_REQUESTS;
98089e0f4d2SKip Macy 	}
981ff662b5cSJustin T. Gibbs 
98233eebb6aSJustin T. Gibbs 	if (sc->xbd_max_request_segments > XBD_MAX_SEGMENTS_PER_REQUEST) {
98333eebb6aSJustin T. Gibbs 		device_printf(sc->xbd_dev,
98433eebb6aSJustin T. Gibbs 		    "Back-end specified max_request_segments of %u "
98533eebb6aSJustin T. Gibbs 		    "limited to front-end limit of %u.\n",
98633eebb6aSJustin T. Gibbs 		    sc->xbd_max_request_segments,
98733eebb6aSJustin T. Gibbs 		    XBD_MAX_SEGMENTS_PER_REQUEST);
98833eebb6aSJustin T. Gibbs 		sc->xbd_max_request_segments = XBD_MAX_SEGMENTS_PER_REQUEST;
98989e0f4d2SKip Macy 	}
990ff662b5cSJustin T. Gibbs 
99133eebb6aSJustin T. Gibbs 	if (sc->xbd_max_request_size > XBD_MAX_REQUEST_SIZE) {
99233eebb6aSJustin T. Gibbs 		device_printf(sc->xbd_dev,
99333eebb6aSJustin T. Gibbs 		    "Back-end specified max_request_size of %u "
99433eebb6aSJustin T. Gibbs 		    "limited to front-end limit of %u.\n",
99533eebb6aSJustin T. Gibbs 		    sc->xbd_max_request_size,
99633eebb6aSJustin T. Gibbs 		    XBD_MAX_REQUEST_SIZE);
99733eebb6aSJustin T. Gibbs 		sc->xbd_max_request_size = XBD_MAX_REQUEST_SIZE;
998ff662b5cSJustin T. Gibbs 	}
999443cc4d4SJustin T. Gibbs 
100033eebb6aSJustin T. Gibbs  	if (sc->xbd_max_request_size >
100133eebb6aSJustin T. Gibbs 	    XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments)) {
100233eebb6aSJustin T. Gibbs  		device_printf(sc->xbd_dev,
100333eebb6aSJustin T. Gibbs 		    "Back-end specified max_request_size of %u "
100433eebb6aSJustin T. Gibbs 		    "limited to front-end limit of %u.  (Too few segments.)\n",
100533eebb6aSJustin T. Gibbs 		    sc->xbd_max_request_size,
100633eebb6aSJustin T. Gibbs 		    XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments));
100733eebb6aSJustin T. Gibbs  		sc->xbd_max_request_size =
100833eebb6aSJustin T. Gibbs  		    XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments);
1009443cc4d4SJustin T. Gibbs  	}
1010443cc4d4SJustin T. Gibbs 
101133eebb6aSJustin T. Gibbs 	sc->xbd_max_request_blocks =
101233eebb6aSJustin T. Gibbs 	    BLKIF_SEGS_TO_BLOCKS(sc->xbd_max_request_segments);
1013ff662b5cSJustin T. Gibbs 
1014ff662b5cSJustin T. Gibbs 	/* Allocate datastructures based on negotiated values. */
101533eebb6aSJustin T. Gibbs 	error = bus_dma_tag_create(
101633eebb6aSJustin T. Gibbs 	    bus_get_dma_tag(sc->xbd_dev),	/* parent */
1017ff662b5cSJustin T. Gibbs 	    512, PAGE_SIZE,			/* algnmnt, boundary */
1018ff662b5cSJustin T. Gibbs 	    BUS_SPACE_MAXADDR,			/* lowaddr */
1019ff662b5cSJustin T. Gibbs 	    BUS_SPACE_MAXADDR,			/* highaddr */
1020ff662b5cSJustin T. Gibbs 	    NULL, NULL,				/* filter, filterarg */
102133eebb6aSJustin T. Gibbs 	    sc->xbd_max_request_size,
102233eebb6aSJustin T. Gibbs 	    sc->xbd_max_request_segments,
1023ff662b5cSJustin T. Gibbs 	    PAGE_SIZE,				/* maxsegsize */
1024ff662b5cSJustin T. Gibbs 	    BUS_DMA_ALLOCNOW,			/* flags */
1025ff662b5cSJustin T. Gibbs 	    busdma_lock_mutex,			/* lockfunc */
102633eebb6aSJustin T. Gibbs 	    &sc->xbd_io_lock,			/* lockarg */
102733eebb6aSJustin T. Gibbs 	    &sc->xbd_io_dmat);
1028ff662b5cSJustin T. Gibbs 	if (error != 0) {
102933eebb6aSJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
1030ff662b5cSJustin T. Gibbs 		    "Cannot allocate parent DMA tag\n");
1031ff662b5cSJustin T. Gibbs 		return;
1032ff662b5cSJustin T. Gibbs 	}
1033ff662b5cSJustin T. Gibbs 
1034ff662b5cSJustin T. Gibbs 	/* Per-transaction data allocation. */
103533eebb6aSJustin T. Gibbs 	sc->xbd_shadow = malloc(sizeof(*sc->xbd_shadow) * sc->xbd_max_requests,
1036ff662b5cSJustin T. Gibbs 	    M_XENBLOCKFRONT, M_NOWAIT|M_ZERO);
103733eebb6aSJustin T. Gibbs 	if (sc->xbd_shadow == NULL) {
103833eebb6aSJustin T. Gibbs 		bus_dma_tag_destroy(sc->xbd_io_dmat);
103933eebb6aSJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
1040ff662b5cSJustin T. Gibbs 		    "Cannot allocate request structures\n");
104106a630f6SJustin T. Gibbs 		return;
1042ff662b5cSJustin T. Gibbs 	}
1043ff662b5cSJustin T. Gibbs 
104433eebb6aSJustin T. Gibbs 	for (i = 0; i < sc->xbd_max_requests; i++) {
104533eebb6aSJustin T. Gibbs 		struct xbd_command *cm;
1046ff662b5cSJustin T. Gibbs 
104733eebb6aSJustin T. Gibbs 		cm = &sc->xbd_shadow[i];
104833eebb6aSJustin T. Gibbs 		cm->cm_sg_refs = malloc(
104933eebb6aSJustin T. Gibbs 		    sizeof(grant_ref_t) * sc->xbd_max_request_segments,
1050ff662b5cSJustin T. Gibbs 		    M_XENBLOCKFRONT, M_NOWAIT);
105133eebb6aSJustin T. Gibbs 		if (cm->cm_sg_refs == NULL)
1052ff662b5cSJustin T. Gibbs 			break;
105333eebb6aSJustin T. Gibbs 		cm->cm_id = i;
1054*e2c1fe90SJustin T. Gibbs 		cm->cm_flags = XBDCF_INITIALIZER;
1055ff662b5cSJustin T. Gibbs 		cm->cm_sc = sc;
105633eebb6aSJustin T. Gibbs 		if (bus_dmamap_create(sc->xbd_io_dmat, 0, &cm->cm_map) != 0)
1057ff662b5cSJustin T. Gibbs 			break;
105833eebb6aSJustin T. Gibbs 		xbd_free_command(cm);
1059ff662b5cSJustin T. Gibbs 	}
1060ff662b5cSJustin T. Gibbs 
1061cdf5d66fSJustin T. Gibbs 	if (xbd_alloc_ring(sc) != 0)
1062ff662b5cSJustin T. Gibbs 		return;
1063ff662b5cSJustin T. Gibbs 
10648b8bfa35SJustin T. Gibbs 	/* Support both backend schemes for relaying ring page limits. */
106533eebb6aSJustin T. Gibbs 	if (sc->xbd_ring_pages > 1) {
1066ff662b5cSJustin T. Gibbs 		error = xs_printf(XST_NIL, node_path,
106733eebb6aSJustin T. Gibbs 		    "num-ring-pages","%u",
106833eebb6aSJustin T. Gibbs 		    sc->xbd_ring_pages);
1069ff662b5cSJustin T. Gibbs 		if (error) {
107033eebb6aSJustin T. Gibbs 			xenbus_dev_fatal(sc->xbd_dev, error,
10718b8bfa35SJustin T. Gibbs 			    "writing %s/num-ring-pages",
10728b8bfa35SJustin T. Gibbs 			    node_path);
10738b8bfa35SJustin T. Gibbs 			return;
10748b8bfa35SJustin T. Gibbs 		}
10750d172324SJustin T. Gibbs 
10768b8bfa35SJustin T. Gibbs 		error = xs_printf(XST_NIL, node_path,
10770d172324SJustin T. Gibbs 		    "ring-page-order", "%u",
107833eebb6aSJustin T. Gibbs 		    fls(sc->xbd_ring_pages) - 1);
10798b8bfa35SJustin T. Gibbs 		if (error) {
108033eebb6aSJustin T. Gibbs 			xenbus_dev_fatal(sc->xbd_dev, error,
10818b8bfa35SJustin T. Gibbs 			    "writing %s/ring-page-order",
1082ff662b5cSJustin T. Gibbs 			    node_path);
1083ff662b5cSJustin T. Gibbs 			return;
1084ff662b5cSJustin T. Gibbs 		}
10850d172324SJustin T. Gibbs 	}
1086ff662b5cSJustin T. Gibbs 
1087ff662b5cSJustin T. Gibbs 	error = xs_printf(XST_NIL, node_path,
108833eebb6aSJustin T. Gibbs 	    "max-requests","%u",
108933eebb6aSJustin T. Gibbs 	    sc->xbd_max_requests);
1090ff662b5cSJustin T. Gibbs 	if (error) {
109133eebb6aSJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
1092ff662b5cSJustin T. Gibbs 		    "writing %s/max-requests",
1093ff662b5cSJustin T. Gibbs 		    node_path);
1094ff662b5cSJustin T. Gibbs 		return;
1095ff662b5cSJustin T. Gibbs 	}
1096ff662b5cSJustin T. Gibbs 
1097ff662b5cSJustin T. Gibbs 	error = xs_printf(XST_NIL, node_path,
109833eebb6aSJustin T. Gibbs 	    "max-request-segments","%u",
109933eebb6aSJustin T. Gibbs 	    sc->xbd_max_request_segments);
1100ff662b5cSJustin T. Gibbs 	if (error) {
110133eebb6aSJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
1102ff662b5cSJustin T. Gibbs 		    "writing %s/max-request-segments",
1103ff662b5cSJustin T. Gibbs 		    node_path);
1104ff662b5cSJustin T. Gibbs 		return;
1105ff662b5cSJustin T. Gibbs 	}
1106ff662b5cSJustin T. Gibbs 
1107ff662b5cSJustin T. Gibbs 	error = xs_printf(XST_NIL, node_path,
110833eebb6aSJustin T. Gibbs 	    "max-request-size","%u",
110933eebb6aSJustin T. Gibbs 	    sc->xbd_max_request_size);
1110ff662b5cSJustin T. Gibbs 	if (error) {
111133eebb6aSJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
1112ff662b5cSJustin T. Gibbs 		    "writing %s/max-request-size",
1113ff662b5cSJustin T. Gibbs 		    node_path);
1114ff662b5cSJustin T. Gibbs 		return;
1115ff662b5cSJustin T. Gibbs 	}
1116ff662b5cSJustin T. Gibbs 
1117ff662b5cSJustin T. Gibbs 	error = xs_printf(XST_NIL, node_path, "event-channel",
111833eebb6aSJustin T. Gibbs 	    "%u", irq_to_evtchn_port(sc->xbd_irq));
1119ff662b5cSJustin T. Gibbs 	if (error) {
112033eebb6aSJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
1121ff662b5cSJustin T. Gibbs 		    "writing %s/event-channel",
1122ff662b5cSJustin T. Gibbs 		    node_path);
1123ff662b5cSJustin T. Gibbs 		return;
1124ff662b5cSJustin T. Gibbs 	}
1125ff662b5cSJustin T. Gibbs 
112633eebb6aSJustin T. Gibbs 	error = xs_printf(XST_NIL, node_path, "protocol",
112733eebb6aSJustin T. Gibbs 	    "%s", XEN_IO_PROTO_ABI_NATIVE);
1128ff662b5cSJustin T. Gibbs 	if (error) {
112933eebb6aSJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
1130ff662b5cSJustin T. Gibbs 		    "writing %s/protocol",
1131ff662b5cSJustin T. Gibbs 		    node_path);
1132ff662b5cSJustin T. Gibbs 		return;
113323dc5621SKip Macy 	}
113412678024SDoug Rabson 
113533eebb6aSJustin T. Gibbs 	xenbus_set_state(sc->xbd_dev, XenbusStateInitialised);
113689e0f4d2SKip Macy }
113789e0f4d2SKip Macy 
113889e0f4d2SKip Macy /*
113933eebb6aSJustin T. Gibbs  * Invoked when the backend is finally 'ready' (and has published
114033eebb6aSJustin T. Gibbs  * the details about the physical device - #sectors, size, etc).
114189e0f4d2SKip Macy  */
114289e0f4d2SKip Macy static void
114333eebb6aSJustin T. Gibbs xbd_connect(struct xbd_softc *sc)
114489e0f4d2SKip Macy {
114533eebb6aSJustin T. Gibbs 	device_t dev = sc->xbd_dev;
114689e0f4d2SKip Macy 	unsigned long sectors, sector_size;
114789e0f4d2SKip Macy 	unsigned int binfo;
1148e4808c4bSKip Macy 	int err, feature_barrier;
114989e0f4d2SKip Macy 
1150*e2c1fe90SJustin T. Gibbs 	if (sc->xbd_state == XBD_STATE_CONNECTED ||
1151*e2c1fe90SJustin T. Gibbs 	    sc->xbd_state == XBD_STATE_SUSPENDED)
115289e0f4d2SKip Macy 		return;
115389e0f4d2SKip Macy 
115423dc5621SKip Macy 	DPRINTK("blkfront.c:connect:%s.\n", xenbus_get_otherend_path(dev));
115589e0f4d2SKip Macy 
1156ff662b5cSJustin T. Gibbs 	err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
115789e0f4d2SKip Macy 	    "sectors", "%lu", &sectors,
115889e0f4d2SKip Macy 	    "info", "%u", &binfo,
115989e0f4d2SKip Macy 	    "sector-size", "%lu", &sector_size,
116089e0f4d2SKip Macy 	    NULL);
116189e0f4d2SKip Macy 	if (err) {
116223dc5621SKip Macy 		xenbus_dev_fatal(dev, err,
116389e0f4d2SKip Macy 		    "reading backend fields at %s",
116423dc5621SKip Macy 		    xenbus_get_otherend_path(dev));
116589e0f4d2SKip Macy 		return;
116689e0f4d2SKip Macy 	}
1167ff662b5cSJustin T. Gibbs 	err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
1168e4808c4bSKip Macy 	     "feature-barrier", "%lu", &feature_barrier,
116989e0f4d2SKip Macy 	     NULL);
1170e4808c4bSKip Macy 	if (!err || feature_barrier)
1171*e2c1fe90SJustin T. Gibbs 		sc->xbd_flags |= XBDF_BARRIER;
117289e0f4d2SKip Macy 
117333eebb6aSJustin T. Gibbs 	if (sc->xbd_disk == NULL) {
117423dc5621SKip Macy 		device_printf(dev, "%juMB <%s> at %s",
117523dc5621SKip Macy 		    (uintmax_t) sectors / (1048576 / sector_size),
117623dc5621SKip Macy 		    device_get_desc(dev),
117723dc5621SKip Macy 		    xenbus_get_node(dev));
117823dc5621SKip Macy 		bus_print_child_footer(device_get_parent(dev), dev);
117989e0f4d2SKip Macy 
118033eebb6aSJustin T. Gibbs 		xbd_instance_create(sc, sectors, sc->xbd_vdevice, binfo,
118133eebb6aSJustin T. Gibbs 		    sector_size);
118206a630f6SJustin T. Gibbs 	}
118323dc5621SKip Macy 
118423dc5621SKip Macy 	(void)xenbus_set_state(dev, XenbusStateConnected);
118589e0f4d2SKip Macy 
118689e0f4d2SKip Macy 	/* Kick pending requests. */
118733eebb6aSJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
1188*e2c1fe90SJustin T. Gibbs 	sc->xbd_state = XBD_STATE_CONNECTED;
118933eebb6aSJustin T. Gibbs 	xbd_startio(sc);
1190*e2c1fe90SJustin T. Gibbs 	sc->xbd_flags |= XBDF_READY;
119133eebb6aSJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
119289e0f4d2SKip Macy }
119389e0f4d2SKip Macy 
119489e0f4d2SKip Macy /**
119589e0f4d2SKip Macy  * Handle the change of state of the backend to Closing.  We must delete our
119689e0f4d2SKip Macy  * device-layer structures now, to ensure that writes are flushed through to
1197a4660d59SJustin T. Gibbs  * the backend.  Once this is done, we can switch to Closed in
119889e0f4d2SKip Macy  * acknowledgement.
119989e0f4d2SKip Macy  */
120023dc5621SKip Macy static void
120133eebb6aSJustin T. Gibbs xbd_closing(device_t dev)
120289e0f4d2SKip Macy {
120333eebb6aSJustin T. Gibbs 	struct xbd_softc *sc = device_get_softc(dev);
120489e0f4d2SKip Macy 
1205ff662b5cSJustin T. Gibbs 	xenbus_set_state(dev, XenbusStateClosing);
1206ff662b5cSJustin T. Gibbs 
120733eebb6aSJustin T. Gibbs 	DPRINTK("xbd_closing: %s removed\n", xenbus_get_node(dev));
120889e0f4d2SKip Macy 
120933eebb6aSJustin T. Gibbs 	if (sc->xbd_disk != NULL) {
121033eebb6aSJustin T. Gibbs 		disk_destroy(sc->xbd_disk);
121133eebb6aSJustin T. Gibbs 		sc->xbd_disk = NULL;
121289e0f4d2SKip Macy 	}
121389e0f4d2SKip Macy 
121423dc5621SKip Macy 	xenbus_set_state(dev, XenbusStateClosed);
121589e0f4d2SKip Macy }
121689e0f4d2SKip Macy 
1217fac3fd80SJustin T. Gibbs /*---------------------------- NewBus Entrypoints ----------------------------*/
1218fac3fd80SJustin T. Gibbs static int
1219fac3fd80SJustin T. Gibbs xbd_probe(device_t dev)
1220fac3fd80SJustin T. Gibbs {
1221fac3fd80SJustin T. Gibbs 
1222fac3fd80SJustin T. Gibbs 	if (!strcmp(xenbus_get_type(dev), "vbd")) {
1223fac3fd80SJustin T. Gibbs 		device_set_desc(dev, "Virtual Block Device");
1224fac3fd80SJustin T. Gibbs 		device_quiet(dev);
1225fac3fd80SJustin T. Gibbs 		return (0);
1226fac3fd80SJustin T. Gibbs 	}
1227fac3fd80SJustin T. Gibbs 
1228fac3fd80SJustin T. Gibbs 	return (ENXIO);
1229fac3fd80SJustin T. Gibbs }
1230fac3fd80SJustin T. Gibbs 
1231fac3fd80SJustin T. Gibbs /*
1232fac3fd80SJustin T. Gibbs  * Setup supplies the backend dir, virtual device.  We place an event
1233fac3fd80SJustin T. Gibbs  * channel and shared frame entries.  We watch backend to wait if it's
1234fac3fd80SJustin T. Gibbs  * ok.
1235fac3fd80SJustin T. Gibbs  */
1236fac3fd80SJustin T. Gibbs static int
1237fac3fd80SJustin T. Gibbs xbd_attach(device_t dev)
1238fac3fd80SJustin T. Gibbs {
1239fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc;
1240fac3fd80SJustin T. Gibbs 	const char *name;
1241fac3fd80SJustin T. Gibbs 	uint32_t vdevice;
1242fac3fd80SJustin T. Gibbs 	int error;
1243fac3fd80SJustin T. Gibbs 	int i;
1244fac3fd80SJustin T. Gibbs 	int unit;
1245fac3fd80SJustin T. Gibbs 
1246fac3fd80SJustin T. Gibbs 	/* FIXME: Use dynamic device id if this is not set. */
1247fac3fd80SJustin T. Gibbs 	error = xs_scanf(XST_NIL, xenbus_get_node(dev),
1248fac3fd80SJustin T. Gibbs 	    "virtual-device", NULL, "%" PRIu32, &vdevice);
1249fac3fd80SJustin T. Gibbs 	if (error) {
1250fac3fd80SJustin T. Gibbs 		xenbus_dev_fatal(dev, error, "reading virtual-device");
1251fac3fd80SJustin T. Gibbs 		device_printf(dev, "Couldn't determine virtual device.\n");
1252fac3fd80SJustin T. Gibbs 		return (error);
1253fac3fd80SJustin T. Gibbs 	}
1254fac3fd80SJustin T. Gibbs 
1255fac3fd80SJustin T. Gibbs 	xbd_vdevice_to_unit(vdevice, &unit, &name);
1256fac3fd80SJustin T. Gibbs 	if (!strcmp(name, "xbd"))
1257fac3fd80SJustin T. Gibbs 		device_set_unit(dev, unit);
1258fac3fd80SJustin T. Gibbs 
1259fac3fd80SJustin T. Gibbs 	sc = device_get_softc(dev);
1260fac3fd80SJustin T. Gibbs 	mtx_init(&sc->xbd_io_lock, "blkfront i/o lock", NULL, MTX_DEF);
1261*e2c1fe90SJustin T. Gibbs 	xbd_initqs(sc);
1262fac3fd80SJustin T. Gibbs 	for (i = 0; i < XBD_MAX_RING_PAGES; i++)
1263d9fab01dSJustin T. Gibbs 		sc->xbd_ring_ref[i] = GRANT_REF_INVALID;
1264fac3fd80SJustin T. Gibbs 
1265fac3fd80SJustin T. Gibbs 	sc->xbd_dev = dev;
1266fac3fd80SJustin T. Gibbs 	sc->xbd_vdevice = vdevice;
1267*e2c1fe90SJustin T. Gibbs 	sc->xbd_state = XBD_STATE_DISCONNECTED;
1268fac3fd80SJustin T. Gibbs 
1269fac3fd80SJustin T. Gibbs 	xbd_setup_sysctl(sc);
1270fac3fd80SJustin T. Gibbs 
1271fac3fd80SJustin T. Gibbs 	/* Wait for backend device to publish its protocol capabilities. */
1272fac3fd80SJustin T. Gibbs 	xenbus_set_state(dev, XenbusStateInitialising);
1273fac3fd80SJustin T. Gibbs 
1274fac3fd80SJustin T. Gibbs 	return (0);
1275fac3fd80SJustin T. Gibbs }
127689e0f4d2SKip Macy 
127723dc5621SKip Macy static int
127833eebb6aSJustin T. Gibbs xbd_detach(device_t dev)
127989e0f4d2SKip Macy {
128033eebb6aSJustin T. Gibbs 	struct xbd_softc *sc = device_get_softc(dev);
128189e0f4d2SKip Macy 
1282*e2c1fe90SJustin T. Gibbs 	DPRINTK("%s: %s removed\n", __func__, xenbus_get_node(dev));
128389e0f4d2SKip Macy 
128433eebb6aSJustin T. Gibbs 	xbd_free(sc);
128533eebb6aSJustin T. Gibbs 	mtx_destroy(&sc->xbd_io_lock);
128689e0f4d2SKip Macy 
128789e0f4d2SKip Macy 	return 0;
128889e0f4d2SKip Macy }
128989e0f4d2SKip Macy 
129089e0f4d2SKip Macy static int
1291fac3fd80SJustin T. Gibbs xbd_suspend(device_t dev)
129289e0f4d2SKip Macy {
1293fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = device_get_softc(dev);
1294fac3fd80SJustin T. Gibbs 	int retval;
1295fac3fd80SJustin T. Gibbs 	int saved_state;
129689e0f4d2SKip Macy 
129789e0f4d2SKip Macy 	/* Prevent new requests being issued until we fix things up. */
129833eebb6aSJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
1299*e2c1fe90SJustin T. Gibbs 	saved_state = sc->xbd_state;
1300*e2c1fe90SJustin T. Gibbs 	sc->xbd_state = XBD_STATE_SUSPENDED;
1301fac3fd80SJustin T. Gibbs 
1302fac3fd80SJustin T. Gibbs 	/* Wait for outstanding I/O to drain. */
1303fac3fd80SJustin T. Gibbs 	retval = 0;
1304*e2c1fe90SJustin T. Gibbs 	while (TAILQ_EMPTY(&sc->xbd_cm_q[XBD_Q_BUSY].q_tailq) == 0) {
1305*e2c1fe90SJustin T. Gibbs 		if (msleep(&sc->xbd_cm_q[XBD_Q_BUSY], &sc->xbd_io_lock,
1306fac3fd80SJustin T. Gibbs 		    PRIBIO, "blkf_susp", 30 * hz) == EWOULDBLOCK) {
1307fac3fd80SJustin T. Gibbs 			retval = EBUSY;
1308fac3fd80SJustin T. Gibbs 			break;
1309fac3fd80SJustin T. Gibbs 		}
1310fac3fd80SJustin T. Gibbs 	}
131133eebb6aSJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
131289e0f4d2SKip Macy 
1313fac3fd80SJustin T. Gibbs 	if (retval != 0)
1314*e2c1fe90SJustin T. Gibbs 		sc->xbd_state = saved_state;
131533eebb6aSJustin T. Gibbs 
1316fac3fd80SJustin T. Gibbs 	return (retval);
131789e0f4d2SKip Macy }
131889e0f4d2SKip Macy 
1319ff662b5cSJustin T. Gibbs static int
1320fac3fd80SJustin T. Gibbs xbd_resume(device_t dev)
132189e0f4d2SKip Macy {
1322fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = device_get_softc(dev);
1323fac3fd80SJustin T. Gibbs 
1324fac3fd80SJustin T. Gibbs 	DPRINTK("xbd_resume: %s\n", xenbus_get_node(dev));
1325fac3fd80SJustin T. Gibbs 
1326fac3fd80SJustin T. Gibbs 	xbd_free(sc);
1327fac3fd80SJustin T. Gibbs 	xbd_initialize(sc);
1328fac3fd80SJustin T. Gibbs 	return (0);
132989e0f4d2SKip Macy }
133089e0f4d2SKip Macy 
1331fac3fd80SJustin T. Gibbs /**
1332fac3fd80SJustin T. Gibbs  * Callback received when the backend's state changes.
1333fac3fd80SJustin T. Gibbs  */
1334fac3fd80SJustin T. Gibbs static void
1335fac3fd80SJustin T. Gibbs xbd_backend_changed(device_t dev, XenbusState backend_state)
1336fac3fd80SJustin T. Gibbs {
1337fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = device_get_softc(dev);
1338fac3fd80SJustin T. Gibbs 
1339fac3fd80SJustin T. Gibbs 	DPRINTK("backend_state=%d\n", backend_state);
1340fac3fd80SJustin T. Gibbs 
1341fac3fd80SJustin T. Gibbs 	switch (backend_state) {
1342fac3fd80SJustin T. Gibbs 	case XenbusStateUnknown:
1343fac3fd80SJustin T. Gibbs 	case XenbusStateInitialising:
1344fac3fd80SJustin T. Gibbs 	case XenbusStateReconfigured:
1345fac3fd80SJustin T. Gibbs 	case XenbusStateReconfiguring:
1346fac3fd80SJustin T. Gibbs 	case XenbusStateClosed:
1347fac3fd80SJustin T. Gibbs 		break;
1348fac3fd80SJustin T. Gibbs 
1349fac3fd80SJustin T. Gibbs 	case XenbusStateInitWait:
1350fac3fd80SJustin T. Gibbs 	case XenbusStateInitialised:
1351fac3fd80SJustin T. Gibbs 		xbd_initialize(sc);
1352fac3fd80SJustin T. Gibbs 		break;
1353fac3fd80SJustin T. Gibbs 
1354fac3fd80SJustin T. Gibbs 	case XenbusStateConnected:
1355fac3fd80SJustin T. Gibbs 		xbd_initialize(sc);
1356fac3fd80SJustin T. Gibbs 		xbd_connect(sc);
1357fac3fd80SJustin T. Gibbs 		break;
1358fac3fd80SJustin T. Gibbs 
1359fac3fd80SJustin T. Gibbs 	case XenbusStateClosing:
1360fac3fd80SJustin T. Gibbs 		if (sc->xbd_users > 0)
1361fac3fd80SJustin T. Gibbs 			xenbus_dev_error(dev, -EBUSY,
1362fac3fd80SJustin T. Gibbs 			    "Device in use; refusing to close");
1363fac3fd80SJustin T. Gibbs 		else
1364fac3fd80SJustin T. Gibbs 			xbd_closing(dev);
1365fac3fd80SJustin T. Gibbs 		break;
1366fac3fd80SJustin T. Gibbs 	}
1367fac3fd80SJustin T. Gibbs }
1368fac3fd80SJustin T. Gibbs 
1369fac3fd80SJustin T. Gibbs /*---------------------------- NewBus Registration ---------------------------*/
137033eebb6aSJustin T. Gibbs static device_method_t xbd_methods[] = {
137123dc5621SKip Macy 	/* Device interface */
137233eebb6aSJustin T. Gibbs 	DEVMETHOD(device_probe,         xbd_probe),
137333eebb6aSJustin T. Gibbs 	DEVMETHOD(device_attach,        xbd_attach),
137433eebb6aSJustin T. Gibbs 	DEVMETHOD(device_detach,        xbd_detach),
137523dc5621SKip Macy 	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
137633eebb6aSJustin T. Gibbs 	DEVMETHOD(device_suspend,       xbd_suspend),
137733eebb6aSJustin T. Gibbs 	DEVMETHOD(device_resume,        xbd_resume),
137889e0f4d2SKip Macy 
137923dc5621SKip Macy 	/* Xenbus interface */
138033eebb6aSJustin T. Gibbs 	DEVMETHOD(xenbus_otherend_changed, xbd_backend_changed),
138189e0f4d2SKip Macy 
138223dc5621SKip Macy 	{ 0, 0 }
138389e0f4d2SKip Macy };
138489e0f4d2SKip Macy 
138533eebb6aSJustin T. Gibbs static driver_t xbd_driver = {
138623dc5621SKip Macy 	"xbd",
138733eebb6aSJustin T. Gibbs 	xbd_methods,
138833eebb6aSJustin T. Gibbs 	sizeof(struct xbd_softc),
138989e0f4d2SKip Macy };
139033eebb6aSJustin T. Gibbs devclass_t xbd_devclass;
139189e0f4d2SKip Macy 
139233eebb6aSJustin T. Gibbs DRIVER_MODULE(xbd, xenbusb_front, xbd_driver, xbd_devclass, 0, 0);
1393