xref: /freebsd/sys/dev/xen/blkfront/blkfront.c (revision 9985113b61b23f700470e316fe1073bcdd5a964b)
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 ----------------------------*/
90127a9483SJustin T. Gibbs static void
91127a9483SJustin T. Gibbs xbd_freeze(struct xbd_softc *sc, xbd_flag_t xbd_flag)
92127a9483SJustin T. Gibbs {
93127a9483SJustin T. Gibbs 	if (xbd_flag != XBDF_NONE && (sc->xbd_flags & xbd_flag) != 0)
94127a9483SJustin T. Gibbs 		return;
95127a9483SJustin T. Gibbs 
96127a9483SJustin T. Gibbs 	sc->xbd_flags |= xbd_flag;
97127a9483SJustin T. Gibbs 	sc->xbd_qfrozen_cnt++;
98127a9483SJustin T. Gibbs }
99127a9483SJustin T. Gibbs 
100127a9483SJustin T. Gibbs static void
101127a9483SJustin T. Gibbs xbd_thaw(struct xbd_softc *sc, xbd_flag_t xbd_flag)
102127a9483SJustin T. Gibbs {
103127a9483SJustin T. Gibbs 	if (xbd_flag != XBDF_NONE && (sc->xbd_flags & xbd_flag) == 0)
104127a9483SJustin T. Gibbs 		return;
105127a9483SJustin T. Gibbs 
106b834eea6SJustin T. Gibbs 	if (sc->xbd_qfrozen_cnt == 0)
107127a9483SJustin T. Gibbs 		panic("%s: Thaw with flag 0x%x while not frozen.",
108127a9483SJustin T. Gibbs 		    __func__, xbd_flag);
109127a9483SJustin T. Gibbs 
110127a9483SJustin T. Gibbs 	sc->xbd_flags &= ~xbd_flag;
111127a9483SJustin T. Gibbs 	sc->xbd_qfrozen_cnt--;
112127a9483SJustin T. Gibbs }
113127a9483SJustin T. Gibbs 
114*9985113bSJustin T. Gibbs static void
115*9985113bSJustin T. Gibbs xbd_cm_freeze(struct xbd_softc *sc, struct xbd_command *cm, xbdc_flag_t cm_flag)
116*9985113bSJustin T. Gibbs {
117*9985113bSJustin T. Gibbs 	if ((cm->cm_flags & XBDCF_FROZEN) != 0)
118*9985113bSJustin T. Gibbs 		return;
119*9985113bSJustin T. Gibbs 
120*9985113bSJustin T. Gibbs 	cm->cm_flags |= XBDCF_FROZEN|cm_flag;
121*9985113bSJustin T. Gibbs 	xbd_freeze(sc, XBDF_NONE);
122*9985113bSJustin T. Gibbs }
123*9985113bSJustin T. Gibbs 
124*9985113bSJustin T. Gibbs static void
125*9985113bSJustin T. Gibbs xbd_cm_thaw(struct xbd_softc *sc, struct xbd_command *cm)
126*9985113bSJustin T. Gibbs {
127*9985113bSJustin T. Gibbs 	if ((cm->cm_flags & XBDCF_FROZEN) == 0)
128*9985113bSJustin T. Gibbs 		return;
129*9985113bSJustin T. Gibbs 
130*9985113bSJustin T. Gibbs 	cm->cm_flags &= ~XBDCF_FROZEN;
131*9985113bSJustin T. Gibbs 	xbd_thaw(sc, XBDF_NONE);
132*9985113bSJustin T. Gibbs }
133*9985113bSJustin T. Gibbs 
134fac3fd80SJustin T. Gibbs static inline void
135cdf5d66fSJustin T. Gibbs xbd_flush_requests(struct xbd_softc *sc)
136fac3fd80SJustin T. Gibbs {
137fac3fd80SJustin T. Gibbs 	int notify;
138e4808c4bSKip Macy 
139fac3fd80SJustin T. Gibbs 	RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&sc->xbd_ring, notify);
14089e0f4d2SKip Macy 
141fac3fd80SJustin T. Gibbs 	if (notify)
142fac3fd80SJustin T. Gibbs 		notify_remote_via_irq(sc->xbd_irq);
143fac3fd80SJustin T. Gibbs }
14489e0f4d2SKip Macy 
14523dc5621SKip Macy static void
146fac3fd80SJustin T. Gibbs xbd_free_command(struct xbd_command *cm)
14723dc5621SKip Macy {
14823dc5621SKip Macy 
149e2c1fe90SJustin T. Gibbs 	KASSERT((cm->cm_flags & XBDCF_Q_MASK) == XBD_Q_NONE,
150e2c1fe90SJustin T. Gibbs 	    ("Freeing command that is still on queue %d.",
151e2c1fe90SJustin T. Gibbs 	    cm->cm_flags & XBDCF_Q_MASK));
15223dc5621SKip Macy 
153e2c1fe90SJustin T. Gibbs 	cm->cm_flags = XBDCF_INITIALIZER;
154fac3fd80SJustin T. Gibbs 	cm->cm_bp = NULL;
155fac3fd80SJustin T. Gibbs 	cm->cm_complete = NULL;
156e2c1fe90SJustin T. Gibbs 	xbd_enqueue_cm(cm, XBD_Q_FREE);
157127a9483SJustin T. Gibbs 	xbd_thaw(cm->cm_sc, XBDF_CM_SHORTAGE);
15823dc5621SKip Macy }
15923dc5621SKip Macy 
16089e0f4d2SKip Macy static void
161fac3fd80SJustin T. Gibbs xbd_queue_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
16289e0f4d2SKip Macy {
163fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc;
164fac3fd80SJustin T. Gibbs 	struct xbd_command *cm;
165fac3fd80SJustin T. Gibbs 	blkif_request_t	*ring_req;
166fac3fd80SJustin T. Gibbs 	struct blkif_request_segment *sg;
167fac3fd80SJustin T. Gibbs 	struct blkif_request_segment *last_block_sg;
168fac3fd80SJustin T. Gibbs 	grant_ref_t *sg_ref;
169fac3fd80SJustin T. Gibbs 	vm_paddr_t buffer_ma;
170fac3fd80SJustin T. Gibbs 	uint64_t fsect, lsect;
171fac3fd80SJustin T. Gibbs 	int ref;
172fac3fd80SJustin T. Gibbs 	int op;
173fac3fd80SJustin T. Gibbs 	int block_segs;
17489e0f4d2SKip Macy 
175fac3fd80SJustin T. Gibbs 	cm = arg;
176fac3fd80SJustin T. Gibbs 	sc = cm->cm_sc;
177fac3fd80SJustin T. Gibbs 
178fac3fd80SJustin T. Gibbs 	if (error) {
179fac3fd80SJustin T. Gibbs 		printf("error %d in xbd_queue_cb\n", error);
180fac3fd80SJustin T. Gibbs 		cm->cm_bp->bio_error = EIO;
181fac3fd80SJustin T. Gibbs 		biodone(cm->cm_bp);
182fac3fd80SJustin T. Gibbs 		xbd_free_command(cm);
18389e0f4d2SKip Macy 		return;
18489e0f4d2SKip Macy 	}
18589e0f4d2SKip Macy 
186fac3fd80SJustin T. Gibbs 	/* Fill out a communications ring structure. */
187fac3fd80SJustin T. Gibbs 	ring_req = RING_GET_REQUEST(&sc->xbd_ring, sc->xbd_ring.req_prod_pvt);
188fac3fd80SJustin T. Gibbs 	sc->xbd_ring.req_prod_pvt++;
189fac3fd80SJustin T. Gibbs 	ring_req->id = cm->cm_id;
190fac3fd80SJustin T. Gibbs 	ring_req->operation = cm->cm_operation;
191fac3fd80SJustin T. Gibbs 	ring_req->sector_number = cm->cm_sector_number;
192fac3fd80SJustin T. Gibbs 	ring_req->handle = (blkif_vdev_t)(uintptr_t)sc->xbd_disk;
193fac3fd80SJustin T. Gibbs 	ring_req->nr_segments = nsegs;
194fac3fd80SJustin T. Gibbs 	cm->cm_nseg = nsegs;
195fac3fd80SJustin T. Gibbs 
196fac3fd80SJustin T. Gibbs 	block_segs    = MIN(nsegs, BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK);
197fac3fd80SJustin T. Gibbs 	sg            = ring_req->seg;
198fac3fd80SJustin T. Gibbs 	last_block_sg = sg + block_segs;
199fac3fd80SJustin T. Gibbs 	sg_ref        = cm->cm_sg_refs;
200fac3fd80SJustin T. Gibbs 
201fac3fd80SJustin T. Gibbs 	while (1) {
202fac3fd80SJustin T. Gibbs 
203fac3fd80SJustin T. Gibbs 		while (sg < last_block_sg) {
204fac3fd80SJustin T. Gibbs 			buffer_ma = segs->ds_addr;
205fac3fd80SJustin T. Gibbs 			fsect = (buffer_ma & PAGE_MASK) >> XBD_SECTOR_SHFT;
206fac3fd80SJustin T. Gibbs 			lsect = fsect + (segs->ds_len  >> XBD_SECTOR_SHFT) - 1;
207fac3fd80SJustin T. Gibbs 
208fac3fd80SJustin T. Gibbs 			KASSERT(lsect <= 7, ("XEN disk driver data cannot "
209fac3fd80SJustin T. Gibbs 			    "cross a page boundary"));
210fac3fd80SJustin T. Gibbs 
211fac3fd80SJustin T. Gibbs 			/* install a grant reference. */
212fac3fd80SJustin T. Gibbs 			ref = gnttab_claim_grant_reference(&cm->cm_gref_head);
213fac3fd80SJustin T. Gibbs 
214e4808c4bSKip Macy 			/*
215fac3fd80SJustin T. Gibbs 			 * GNTTAB_LIST_END == 0xffffffff, but it is private
216fac3fd80SJustin T. Gibbs 			 * to gnttab.c.
217e4808c4bSKip Macy 			 */
218fac3fd80SJustin T. Gibbs 			KASSERT(ref != ~0, ("grant_reference failed"));
219fac3fd80SJustin T. Gibbs 
220fac3fd80SJustin T. Gibbs 			gnttab_grant_foreign_access_ref(
221fac3fd80SJustin T. Gibbs 			    ref,
222fac3fd80SJustin T. Gibbs 			    xenbus_get_otherend_id(sc->xbd_dev),
223fac3fd80SJustin T. Gibbs 			    buffer_ma >> PAGE_SHIFT,
224fac3fd80SJustin T. Gibbs 			    ring_req->operation == BLKIF_OP_WRITE);
225fac3fd80SJustin T. Gibbs 
226fac3fd80SJustin T. Gibbs 			*sg_ref = ref;
227fac3fd80SJustin T. Gibbs 			*sg = (struct blkif_request_segment) {
228fac3fd80SJustin T. Gibbs 				.gref       = ref,
229fac3fd80SJustin T. Gibbs 				.first_sect = fsect,
230fac3fd80SJustin T. Gibbs 				.last_sect  = lsect
231fac3fd80SJustin T. Gibbs 			};
232fac3fd80SJustin T. Gibbs 			sg++;
233fac3fd80SJustin T. Gibbs 			sg_ref++;
234fac3fd80SJustin T. Gibbs 			segs++;
235fac3fd80SJustin T. Gibbs 			nsegs--;
236fac3fd80SJustin T. Gibbs 		}
237fac3fd80SJustin T. Gibbs 		block_segs = MIN(nsegs, BLKIF_MAX_SEGMENTS_PER_SEGMENT_BLOCK);
238fac3fd80SJustin T. Gibbs 		if (block_segs == 0)
239fac3fd80SJustin T. Gibbs 			break;
240fac3fd80SJustin T. Gibbs 
241fac3fd80SJustin T. Gibbs 		sg = BLKRING_GET_SEG_BLOCK(&sc->xbd_ring,
242fac3fd80SJustin T. Gibbs 		    sc->xbd_ring.req_prod_pvt);
243fac3fd80SJustin T. Gibbs 		sc->xbd_ring.req_prod_pvt++;
244fac3fd80SJustin T. Gibbs 		last_block_sg = sg + block_segs;
245fac3fd80SJustin T. Gibbs 	}
246fac3fd80SJustin T. Gibbs 
247fac3fd80SJustin T. Gibbs 	if (cm->cm_operation == BLKIF_OP_READ)
248fac3fd80SJustin T. Gibbs 		op = BUS_DMASYNC_PREREAD;
249fac3fd80SJustin T. Gibbs 	else if (cm->cm_operation == BLKIF_OP_WRITE)
250fac3fd80SJustin T. Gibbs 		op = BUS_DMASYNC_PREWRITE;
251fac3fd80SJustin T. Gibbs 	else
252fac3fd80SJustin T. Gibbs 		op = 0;
253fac3fd80SJustin T. Gibbs 	bus_dmamap_sync(sc->xbd_io_dmat, cm->cm_map, op);
254fac3fd80SJustin T. Gibbs 
255fac3fd80SJustin T. Gibbs 	gnttab_free_grant_references(cm->cm_gref_head);
256fac3fd80SJustin T. Gibbs 
257e2c1fe90SJustin T. Gibbs 	xbd_enqueue_cm(cm, XBD_Q_BUSY);
258fac3fd80SJustin T. Gibbs 
259fac3fd80SJustin T. Gibbs 	/*
260127a9483SJustin T. Gibbs 	 * If bus dma had to asynchronously call us back to dispatch
261127a9483SJustin T. Gibbs 	 * this command, we are no longer executing in the context of
262127a9483SJustin T. Gibbs 	 * xbd_startio().  Thus we cannot rely on xbd_startio()'s call to
263127a9483SJustin T. Gibbs 	 * xbd_flush_requests() to publish this command to the backend
264127a9483SJustin T. Gibbs 	 * along with any other commands that it could batch.
265fac3fd80SJustin T. Gibbs 	 */
266127a9483SJustin T. Gibbs 	if ((cm->cm_flags & XBDCF_ASYNC_MAPPING) != 0)
267cdf5d66fSJustin T. Gibbs 		xbd_flush_requests(sc);
268fac3fd80SJustin T. Gibbs 
269fac3fd80SJustin T. Gibbs 	return;
270fac3fd80SJustin T. Gibbs }
271fac3fd80SJustin T. Gibbs 
272fac3fd80SJustin T. Gibbs static int
273fac3fd80SJustin T. Gibbs xbd_queue_request(struct xbd_softc *sc, struct xbd_command *cm)
274fac3fd80SJustin T. Gibbs {
275fac3fd80SJustin T. Gibbs 	int error;
276fac3fd80SJustin T. Gibbs 
277fac3fd80SJustin T. Gibbs 	error = bus_dmamap_load(sc->xbd_io_dmat, cm->cm_map, cm->cm_data,
278fac3fd80SJustin T. Gibbs 	    cm->cm_datalen, xbd_queue_cb, cm, 0);
279fac3fd80SJustin T. Gibbs 	if (error == EINPROGRESS) {
280127a9483SJustin T. Gibbs 		/*
281127a9483SJustin T. Gibbs 		 * Maintain queuing order by freezing the queue.  The next
282127a9483SJustin T. Gibbs 		 * command may not require as many resources as the command
283127a9483SJustin T. Gibbs 		 * we just attempted to map, so we can't rely on bus dma
284127a9483SJustin T. Gibbs 		 * blocking for it too.
285127a9483SJustin T. Gibbs 		 */
286*9985113bSJustin T. Gibbs 		xbd_cm_freeze(sc, cm, XBDCF_ASYNC_MAPPING);
287fac3fd80SJustin T. Gibbs 		return (0);
288fac3fd80SJustin T. Gibbs 	}
289fac3fd80SJustin T. Gibbs 
290fac3fd80SJustin T. Gibbs 	return (error);
291fac3fd80SJustin T. Gibbs }
292fac3fd80SJustin T. Gibbs 
293fac3fd80SJustin T. Gibbs static void
294fac3fd80SJustin T. Gibbs xbd_restart_queue_callback(void *arg)
295fac3fd80SJustin T. Gibbs {
296fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = arg;
297fac3fd80SJustin T. Gibbs 
29833eebb6aSJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
299e4808c4bSKip Macy 
300127a9483SJustin T. Gibbs 	xbd_thaw(sc, XBDF_GNT_SHORTAGE);
301127a9483SJustin T. Gibbs 
30233eebb6aSJustin T. Gibbs 	xbd_startio(sc);
303e4808c4bSKip Macy 
30433eebb6aSJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
305fac3fd80SJustin T. Gibbs }
306fac3fd80SJustin T. Gibbs 
307fac3fd80SJustin T. Gibbs static struct xbd_command *
308fac3fd80SJustin T. Gibbs xbd_bio_command(struct xbd_softc *sc)
309fac3fd80SJustin T. Gibbs {
310fac3fd80SJustin T. Gibbs 	struct xbd_command *cm;
311fac3fd80SJustin T. Gibbs 	struct bio *bp;
312fac3fd80SJustin T. Gibbs 
313e2c1fe90SJustin T. Gibbs 	if (unlikely(sc->xbd_state != XBD_STATE_CONNECTED))
314fac3fd80SJustin T. Gibbs 		return (NULL);
315fac3fd80SJustin T. Gibbs 
316fac3fd80SJustin T. Gibbs 	bp = xbd_dequeue_bio(sc);
317fac3fd80SJustin T. Gibbs 	if (bp == NULL)
318fac3fd80SJustin T. Gibbs 		return (NULL);
319fac3fd80SJustin T. Gibbs 
320e2c1fe90SJustin T. Gibbs 	if ((cm = xbd_dequeue_cm(sc, XBD_Q_FREE)) == NULL) {
321127a9483SJustin T. Gibbs 		xbd_freeze(sc, XBDF_CM_SHORTAGE);
322fac3fd80SJustin T. Gibbs 		xbd_requeue_bio(sc, bp);
323fac3fd80SJustin T. Gibbs 		return (NULL);
324fac3fd80SJustin T. Gibbs 	}
325fac3fd80SJustin T. Gibbs 
326fac3fd80SJustin T. Gibbs 	if (gnttab_alloc_grant_references(sc->xbd_max_request_segments,
327fac3fd80SJustin T. Gibbs 	    &cm->cm_gref_head) != 0) {
328fac3fd80SJustin T. Gibbs 		gnttab_request_free_callback(&sc->xbd_callback,
329fac3fd80SJustin T. Gibbs 		    xbd_restart_queue_callback, sc,
330fac3fd80SJustin T. Gibbs 		    sc->xbd_max_request_segments);
331127a9483SJustin T. Gibbs 		xbd_freeze(sc, XBDF_GNT_SHORTAGE);
332fac3fd80SJustin T. Gibbs 		xbd_requeue_bio(sc, bp);
333e2c1fe90SJustin T. Gibbs 		xbd_enqueue_cm(cm, XBD_Q_FREE);
334fac3fd80SJustin T. Gibbs 		return (NULL);
335fac3fd80SJustin T. Gibbs 	}
336fac3fd80SJustin T. Gibbs 
337fac3fd80SJustin T. Gibbs 	cm->cm_bp = bp;
338fac3fd80SJustin T. Gibbs 	cm->cm_data = bp->bio_data;
339fac3fd80SJustin T. Gibbs 	cm->cm_datalen = bp->bio_bcount;
340fac3fd80SJustin T. Gibbs 	cm->cm_sector_number = (blkif_sector_t)bp->bio_pblkno;
341fac3fd80SJustin T. Gibbs 
342*9985113bSJustin T. Gibbs 	switch (bp->bio_cmd) {
343*9985113bSJustin T. Gibbs 	case BIO_READ:
344*9985113bSJustin T. Gibbs 		cm->cm_operation = BLKIF_OP_READ;
345*9985113bSJustin T. Gibbs 		break;
346*9985113bSJustin T. Gibbs 	case BIO_WRITE:
347*9985113bSJustin T. Gibbs 		cm->cm_operation = BLKIF_OP_WRITE;
348*9985113bSJustin T. Gibbs 		if ((bp->bio_flags & BIO_ORDERED) != 0) {
349*9985113bSJustin T. Gibbs 			if ((sc->xbd_flags & XBDF_BARRIER) != 0) {
350*9985113bSJustin T. Gibbs 				cm->cm_operation = BLKIF_OP_WRITE_BARRIER;
351*9985113bSJustin T. Gibbs 			} else {
352*9985113bSJustin T. Gibbs 				/*
353*9985113bSJustin T. Gibbs 				 * Single step this command.
354*9985113bSJustin T. Gibbs 				 */
355*9985113bSJustin T. Gibbs 				cm->cm_flags |= XBDCF_Q_FREEZE;
356*9985113bSJustin T. Gibbs 				if (xbd_queue_length(sc, XBD_Q_BUSY) != 0) {
357*9985113bSJustin T. Gibbs 					/*
358*9985113bSJustin T. Gibbs 					 * Wait for in-flight requests to
359*9985113bSJustin T. Gibbs 					 * finish.
360*9985113bSJustin T. Gibbs 					 */
361*9985113bSJustin T. Gibbs 					xbd_freeze(sc, XBDF_WAIT_IDLE);
362*9985113bSJustin T. Gibbs 					xbd_requeue_cm(cm, XBD_Q_READY);
363*9985113bSJustin T. Gibbs 					return (NULL);
364*9985113bSJustin T. Gibbs 				}
365*9985113bSJustin T. Gibbs 			}
366*9985113bSJustin T. Gibbs 		}
367*9985113bSJustin T. Gibbs 		break;
368*9985113bSJustin T. Gibbs 	case BIO_FLUSH:
369*9985113bSJustin T. Gibbs 		if ((sc->xbd_flags & XBDF_FLUSH) != 0)
370*9985113bSJustin T. Gibbs 			cm->cm_operation = BLKIF_OP_FLUSH_DISKCACHE;
371*9985113bSJustin T. Gibbs 		else if ((sc->xbd_flags & XBDF_BARRIER) != 0)
372*9985113bSJustin T. Gibbs 			cm->cm_operation = BLKIF_OP_WRITE_BARRIER;
373*9985113bSJustin T. Gibbs 		else
374*9985113bSJustin T. Gibbs 			panic("flush request, but no flush support available");
375*9985113bSJustin T. Gibbs 		break;
376*9985113bSJustin T. Gibbs 	default:
377*9985113bSJustin T. Gibbs 		panic("unknown bio command %d", bp->bio_cmd);
378*9985113bSJustin T. Gibbs 	}
379*9985113bSJustin T. Gibbs 
380fac3fd80SJustin T. Gibbs 	return (cm);
381fac3fd80SJustin T. Gibbs }
382fac3fd80SJustin T. Gibbs 
383fac3fd80SJustin T. Gibbs /*
384fac3fd80SJustin T. Gibbs  * Dequeue buffers and place them in the shared communication ring.
385fac3fd80SJustin T. Gibbs  * Return when no more requests can be accepted or all buffers have
386fac3fd80SJustin T. Gibbs  * been queued.
387fac3fd80SJustin T. Gibbs  *
388fac3fd80SJustin T. Gibbs  * Signal XEN once the ring has been filled out.
389fac3fd80SJustin T. Gibbs  */
390fac3fd80SJustin T. Gibbs static void
391fac3fd80SJustin T. Gibbs xbd_startio(struct xbd_softc *sc)
392fac3fd80SJustin T. Gibbs {
393fac3fd80SJustin T. Gibbs 	struct xbd_command *cm;
394fac3fd80SJustin T. Gibbs 	int error, queued = 0;
395fac3fd80SJustin T. Gibbs 
396fac3fd80SJustin T. Gibbs 	mtx_assert(&sc->xbd_io_lock, MA_OWNED);
397fac3fd80SJustin T. Gibbs 
398e2c1fe90SJustin T. Gibbs 	if (sc->xbd_state != XBD_STATE_CONNECTED)
399e4808c4bSKip Macy 		return;
400fac3fd80SJustin T. Gibbs 
401fac3fd80SJustin T. Gibbs 	while (RING_FREE_REQUESTS(&sc->xbd_ring) >=
402fac3fd80SJustin T. Gibbs 	    sc->xbd_max_request_blocks) {
403127a9483SJustin T. Gibbs 		if (sc->xbd_qfrozen_cnt != 0)
404fac3fd80SJustin T. Gibbs 			break;
405fac3fd80SJustin T. Gibbs 
406e2c1fe90SJustin T. Gibbs 		cm = xbd_dequeue_cm(sc, XBD_Q_READY);
407fac3fd80SJustin T. Gibbs 
408fac3fd80SJustin T. Gibbs 		if (cm == NULL)
409fac3fd80SJustin T. Gibbs 		    cm = xbd_bio_command(sc);
410fac3fd80SJustin T. Gibbs 
411fac3fd80SJustin T. Gibbs 		if (cm == NULL)
412fac3fd80SJustin T. Gibbs 			break;
413fac3fd80SJustin T. Gibbs 
414*9985113bSJustin T. Gibbs 		if ((cm->cm_flags & XBDCF_Q_FREEZE) != 0) {
415*9985113bSJustin T. Gibbs 			/*
416*9985113bSJustin T. Gibbs 			 * Single step command.  Future work is
417*9985113bSJustin T. Gibbs 			 * held off until this command completes.
418*9985113bSJustin T. Gibbs 			 */
419*9985113bSJustin T. Gibbs 			xbd_cm_freeze(sc, cm, XBDCF_Q_FREEZE);
420*9985113bSJustin T. Gibbs 		}
421*9985113bSJustin T. Gibbs 
422fac3fd80SJustin T. Gibbs 		if ((error = xbd_queue_request(sc, cm)) != 0) {
423fac3fd80SJustin T. Gibbs 			printf("xbd_queue_request returned %d\n", error);
424fac3fd80SJustin T. Gibbs 			break;
425fac3fd80SJustin T. Gibbs 		}
426fac3fd80SJustin T. Gibbs 		queued++;
427fac3fd80SJustin T. Gibbs 	}
428fac3fd80SJustin T. Gibbs 
429fac3fd80SJustin T. Gibbs 	if (queued != 0)
430cdf5d66fSJustin T. Gibbs 		xbd_flush_requests(sc);
431e4808c4bSKip Macy }
432e4808c4bSKip Macy 
433e4808c4bSKip Macy static void
43433eebb6aSJustin T. Gibbs xbd_bio_complete(struct xbd_softc *sc, struct xbd_command *cm)
435e4808c4bSKip Macy {
436e4808c4bSKip Macy 	struct bio *bp;
437e4808c4bSKip Macy 
43833eebb6aSJustin T. Gibbs 	bp = cm->cm_bp;
439e4808c4bSKip Macy 
44033eebb6aSJustin T. Gibbs 	if (unlikely(cm->cm_status != BLKIF_RSP_OKAY)) {
441e4808c4bSKip Macy 		disk_err(bp, "disk error" , -1, 0);
44233eebb6aSJustin T. Gibbs 		printf(" status: %x\n", cm->cm_status);
443e4808c4bSKip Macy 		bp->bio_flags |= BIO_ERROR;
444e4808c4bSKip Macy 	}
445e4808c4bSKip Macy 
446e4808c4bSKip Macy 	if (bp->bio_flags & BIO_ERROR)
447e4808c4bSKip Macy 		bp->bio_error = EIO;
448e4808c4bSKip Macy 	else
449e4808c4bSKip Macy 		bp->bio_resid = 0;
450e4808c4bSKip Macy 
45133eebb6aSJustin T. Gibbs 	xbd_free_command(cm);
452e4808c4bSKip Macy 	biodone(bp);
453e4808c4bSKip Macy }
454e4808c4bSKip Macy 
455fac3fd80SJustin T. Gibbs static int
456fac3fd80SJustin T. Gibbs xbd_completion(struct xbd_command *cm)
457fac3fd80SJustin T. Gibbs {
458fac3fd80SJustin T. Gibbs 	gnttab_end_foreign_access_references(cm->cm_nseg, cm->cm_sg_refs);
459fac3fd80SJustin T. Gibbs 	return (BLKIF_SEGS_TO_BLOCKS(cm->cm_nseg));
460fac3fd80SJustin T. Gibbs }
461fac3fd80SJustin T. Gibbs 
462fac3fd80SJustin T. Gibbs static void
463fac3fd80SJustin T. Gibbs xbd_int(void *xsc)
464fac3fd80SJustin T. Gibbs {
465fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = xsc;
466fac3fd80SJustin T. Gibbs 	struct xbd_command *cm;
467fac3fd80SJustin T. Gibbs 	blkif_response_t *bret;
468fac3fd80SJustin T. Gibbs 	RING_IDX i, rp;
469fac3fd80SJustin T. Gibbs 	int op;
470fac3fd80SJustin T. Gibbs 
471fac3fd80SJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
472fac3fd80SJustin T. Gibbs 
473e2c1fe90SJustin T. Gibbs 	if (unlikely(sc->xbd_state == XBD_STATE_DISCONNECTED)) {
474fac3fd80SJustin T. Gibbs 		mtx_unlock(&sc->xbd_io_lock);
475fac3fd80SJustin T. Gibbs 		return;
476fac3fd80SJustin T. Gibbs 	}
477fac3fd80SJustin T. Gibbs 
478fac3fd80SJustin T. Gibbs  again:
479fac3fd80SJustin T. Gibbs 	rp = sc->xbd_ring.sring->rsp_prod;
480fac3fd80SJustin T. Gibbs 	rmb(); /* Ensure we see queued responses up to 'rp'. */
481fac3fd80SJustin T. Gibbs 
482fac3fd80SJustin T. Gibbs 	for (i = sc->xbd_ring.rsp_cons; i != rp;) {
483fac3fd80SJustin T. Gibbs 		bret = RING_GET_RESPONSE(&sc->xbd_ring, i);
484fac3fd80SJustin T. Gibbs 		cm   = &sc->xbd_shadow[bret->id];
485fac3fd80SJustin T. Gibbs 
486e2c1fe90SJustin T. Gibbs 		xbd_remove_cm(cm, XBD_Q_BUSY);
487fac3fd80SJustin T. Gibbs 		i += xbd_completion(cm);
488fac3fd80SJustin T. Gibbs 
489fac3fd80SJustin T. Gibbs 		if (cm->cm_operation == BLKIF_OP_READ)
490fac3fd80SJustin T. Gibbs 			op = BUS_DMASYNC_POSTREAD;
491*9985113bSJustin T. Gibbs 		else if (cm->cm_operation == BLKIF_OP_WRITE ||
492*9985113bSJustin T. Gibbs 		    cm->cm_operation == BLKIF_OP_WRITE_BARRIER)
493fac3fd80SJustin T. Gibbs 			op = BUS_DMASYNC_POSTWRITE;
494fac3fd80SJustin T. Gibbs 		else
495fac3fd80SJustin T. Gibbs 			op = 0;
496fac3fd80SJustin T. Gibbs 		bus_dmamap_sync(sc->xbd_io_dmat, cm->cm_map, op);
497fac3fd80SJustin T. Gibbs 		bus_dmamap_unload(sc->xbd_io_dmat, cm->cm_map);
498fac3fd80SJustin T. Gibbs 
499fac3fd80SJustin T. Gibbs 		/*
500127a9483SJustin T. Gibbs 		 * Release any hold this command has on future command
501127a9483SJustin T. Gibbs 		 * dispatch.
502fac3fd80SJustin T. Gibbs 		 */
503*9985113bSJustin T. Gibbs 		xbd_cm_thaw(sc, cm);
504fac3fd80SJustin T. Gibbs 
505fac3fd80SJustin T. Gibbs 		/*
506fac3fd80SJustin T. Gibbs 		 * Directly call the i/o complete routine to save an
507fac3fd80SJustin T. Gibbs 		 * an indirection in the common case.
508fac3fd80SJustin T. Gibbs 		 */
509fac3fd80SJustin T. Gibbs 		cm->cm_status = bret->status;
510fac3fd80SJustin T. Gibbs 		if (cm->cm_bp)
511fac3fd80SJustin T. Gibbs 			xbd_bio_complete(sc, cm);
512fac3fd80SJustin T. Gibbs 		else if (cm->cm_complete != NULL)
513fac3fd80SJustin T. Gibbs 			cm->cm_complete(cm);
514fac3fd80SJustin T. Gibbs 		else
515fac3fd80SJustin T. Gibbs 			xbd_free_command(cm);
516fac3fd80SJustin T. Gibbs 	}
517fac3fd80SJustin T. Gibbs 
518fac3fd80SJustin T. Gibbs 	sc->xbd_ring.rsp_cons = i;
519fac3fd80SJustin T. Gibbs 
520fac3fd80SJustin T. Gibbs 	if (i != sc->xbd_ring.req_prod_pvt) {
521fac3fd80SJustin T. Gibbs 		int more_to_do;
522fac3fd80SJustin T. Gibbs 		RING_FINAL_CHECK_FOR_RESPONSES(&sc->xbd_ring, more_to_do);
523fac3fd80SJustin T. Gibbs 		if (more_to_do)
524fac3fd80SJustin T. Gibbs 			goto again;
525fac3fd80SJustin T. Gibbs 	} else {
526fac3fd80SJustin T. Gibbs 		sc->xbd_ring.sring->rsp_event = i + 1;
527fac3fd80SJustin T. Gibbs 	}
528fac3fd80SJustin T. Gibbs 
529*9985113bSJustin T. Gibbs 	if (xbd_queue_length(sc, XBD_Q_BUSY) == 0)
530*9985113bSJustin T. Gibbs 		xbd_thaw(sc, XBDF_WAIT_IDLE);
531*9985113bSJustin T. Gibbs 
532fac3fd80SJustin T. Gibbs 	xbd_startio(sc);
533fac3fd80SJustin T. Gibbs 
534e2c1fe90SJustin T. Gibbs 	if (unlikely(sc->xbd_state == XBD_STATE_SUSPENDED))
535e2c1fe90SJustin T. Gibbs 		wakeup(&sc->xbd_cm_q[XBD_Q_BUSY]);
536fac3fd80SJustin T. Gibbs 
537fac3fd80SJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
538fac3fd80SJustin T. Gibbs }
539fac3fd80SJustin T. Gibbs 
540fac3fd80SJustin T. Gibbs /*------------------------------- Dump Support -------------------------------*/
541fac3fd80SJustin T. Gibbs /**
542fac3fd80SJustin T. Gibbs  * Quiesce the disk writes for a dump file before allowing the next buffer.
543fac3fd80SJustin T. Gibbs  */
5448698b76cSKip Macy static void
54533eebb6aSJustin T. Gibbs xbd_quiesce(struct xbd_softc *sc)
5468698b76cSKip Macy {
5478698b76cSKip Macy 	int mtd;
5488698b76cSKip Macy 
5498698b76cSKip Macy 	// While there are outstanding requests
550*9985113bSJustin T. Gibbs 	while (xbd_queue_length(sc, XBD_Q_BUSY) != 0) {
55133eebb6aSJustin T. Gibbs 		RING_FINAL_CHECK_FOR_RESPONSES(&sc->xbd_ring, mtd);
5528698b76cSKip Macy 		if (mtd) {
553e4808c4bSKip Macy 			/* Recieved request completions, update queue. */
55433eebb6aSJustin T. Gibbs 			xbd_int(sc);
5558698b76cSKip Macy 		}
556*9985113bSJustin T. Gibbs 		if (xbd_queue_length(sc, XBD_Q_BUSY) != 0) {
557e4808c4bSKip Macy 			/*
558e4808c4bSKip Macy 			 * Still pending requests, wait for the disk i/o
559e4808c4bSKip Macy 			 * to complete.
560e4808c4bSKip Macy 			 */
561be7747b4SKip Macy 			HYPERVISOR_yield();
5628698b76cSKip Macy 		}
5638698b76cSKip Macy 	}
5648698b76cSKip Macy }
5658698b76cSKip Macy 
566e4808c4bSKip Macy /* Kernel dump function for a paravirtualized disk device */
567e4808c4bSKip Macy static void
56833eebb6aSJustin T. Gibbs xbd_dump_complete(struct xbd_command *cm)
569e4808c4bSKip Macy {
5708698b76cSKip Macy 
571e2c1fe90SJustin T. Gibbs 	xbd_enqueue_cm(cm, XBD_Q_COMPLETE);
572e4808c4bSKip Macy }
573e4808c4bSKip Macy 
5748698b76cSKip Macy static int
57533eebb6aSJustin T. Gibbs xbd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset,
5768698b76cSKip Macy     size_t length)
5778698b76cSKip Macy {
5788698b76cSKip Macy 	struct disk *dp = arg;
57933eebb6aSJustin T. Gibbs 	struct xbd_softc *sc = dp->d_drv1;
58033eebb6aSJustin T. Gibbs 	struct xbd_command *cm;
581e4808c4bSKip Macy 	size_t chunk;
582e4808c4bSKip Macy 	int sbp;
5838698b76cSKip Macy 	int rc = 0;
5848698b76cSKip Macy 
585e4808c4bSKip Macy 	if (length <= 0)
586e4808c4bSKip Macy 		return (rc);
5878698b76cSKip Macy 
58833eebb6aSJustin T. Gibbs 	xbd_quiesce(sc);	/* All quiet on the western front. */
589e4808c4bSKip Macy 
590e4808c4bSKip Macy 	/*
591e4808c4bSKip Macy 	 * If this lock is held, then this module is failing, and a
592e4808c4bSKip Macy 	 * successful kernel dump is highly unlikely anyway.
593e4808c4bSKip Macy 	 */
59433eebb6aSJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
595e4808c4bSKip Macy 
596e4808c4bSKip Macy 	/* Split the 64KB block as needed */
597e4808c4bSKip Macy 	for (sbp=0; length > 0; sbp++) {
598e2c1fe90SJustin T. Gibbs 		cm = xbd_dequeue_cm(sc, XBD_Q_FREE);
599e4808c4bSKip Macy 		if (cm == NULL) {
60033eebb6aSJustin T. Gibbs 			mtx_unlock(&sc->xbd_io_lock);
60133eebb6aSJustin T. Gibbs 			device_printf(sc->xbd_dev, "dump: no more commands?\n");
602e4808c4bSKip Macy 			return (EBUSY);
603e4808c4bSKip Macy 		}
604e4808c4bSKip Macy 
60533eebb6aSJustin T. Gibbs 		if (gnttab_alloc_grant_references(sc->xbd_max_request_segments,
60633eebb6aSJustin T. Gibbs 		    &cm->cm_gref_head) != 0) {
60733eebb6aSJustin T. Gibbs 			xbd_free_command(cm);
60833eebb6aSJustin T. Gibbs 			mtx_unlock(&sc->xbd_io_lock);
60933eebb6aSJustin T. Gibbs 			device_printf(sc->xbd_dev, "no more grant allocs?\n");
610e4808c4bSKip Macy 			return (EBUSY);
611e4808c4bSKip Macy 		}
612e4808c4bSKip Macy 
61333eebb6aSJustin T. Gibbs 		chunk = length > sc->xbd_max_request_size ?
61433eebb6aSJustin T. Gibbs 		    sc->xbd_max_request_size : length;
61533eebb6aSJustin T. Gibbs 		cm->cm_data = virtual;
61633eebb6aSJustin T. Gibbs 		cm->cm_datalen = chunk;
61733eebb6aSJustin T. Gibbs 		cm->cm_operation = BLKIF_OP_WRITE;
61833eebb6aSJustin T. Gibbs 		cm->cm_sector_number = offset / dp->d_sectorsize;
61933eebb6aSJustin T. Gibbs 		cm->cm_complete = xbd_dump_complete;
620e4808c4bSKip Macy 
621e2c1fe90SJustin T. Gibbs 		xbd_enqueue_cm(cm, XBD_Q_READY);
6228698b76cSKip Macy 
6238698b76cSKip Macy 		length -= chunk;
6248698b76cSKip Macy 		offset += chunk;
6258698b76cSKip Macy 		virtual = (char *) virtual + chunk;
6268698b76cSKip Macy 	}
6278698b76cSKip Macy 
628e4808c4bSKip Macy 	/* Tell DOM0 to do the I/O */
62933eebb6aSJustin T. Gibbs 	xbd_startio(sc);
63033eebb6aSJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
631e4808c4bSKip Macy 
632e4808c4bSKip Macy 	/* Poll for the completion. */
63333eebb6aSJustin T. Gibbs 	xbd_quiesce(sc);	/* All quite on the eastern front */
634e4808c4bSKip Macy 
635e4808c4bSKip Macy 	/* If there were any errors, bail out... */
636e2c1fe90SJustin T. Gibbs 	while ((cm = xbd_dequeue_cm(sc, XBD_Q_COMPLETE)) != NULL) {
63733eebb6aSJustin T. Gibbs 		if (cm->cm_status != BLKIF_RSP_OKAY) {
63833eebb6aSJustin T. Gibbs 			device_printf(sc->xbd_dev,
639e4808c4bSKip Macy 			    "Dump I/O failed at sector %jd\n",
64033eebb6aSJustin T. Gibbs 			    cm->cm_sector_number);
641e4808c4bSKip Macy 			rc = EIO;
6428698b76cSKip Macy 		}
64333eebb6aSJustin T. Gibbs 		xbd_free_command(cm);
6448698b76cSKip Macy 	}
645e4808c4bSKip Macy 
6468698b76cSKip Macy 	return (rc);
6478698b76cSKip Macy }
6488698b76cSKip Macy 
649fac3fd80SJustin T. Gibbs /*----------------------------- Disk Entrypoints -----------------------------*/
65023dc5621SKip Macy static int
651fac3fd80SJustin T. Gibbs xbd_open(struct disk *dp)
65289e0f4d2SKip Macy {
653fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = dp->d_drv1;
65423dc5621SKip Macy 
655fac3fd80SJustin T. Gibbs 	if (sc == NULL) {
656fac3fd80SJustin T. Gibbs 		printf("xb%d: not found", sc->xbd_unit);
65723dc5621SKip Macy 		return (ENXIO);
65823dc5621SKip Macy 	}
65923dc5621SKip Macy 
660e2c1fe90SJustin T. Gibbs 	sc->xbd_flags |= XBDF_OPEN;
661fac3fd80SJustin T. Gibbs 	sc->xbd_users++;
662fac3fd80SJustin T. Gibbs 	return (0);
663fac3fd80SJustin T. Gibbs }
664fac3fd80SJustin T. Gibbs 
665fac3fd80SJustin T. Gibbs static int
666fac3fd80SJustin T. Gibbs xbd_close(struct disk *dp)
667fac3fd80SJustin T. Gibbs {
668fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = dp->d_drv1;
669fac3fd80SJustin T. Gibbs 
670fac3fd80SJustin T. Gibbs 	if (sc == NULL)
671fac3fd80SJustin T. Gibbs 		return (ENXIO);
672e2c1fe90SJustin T. Gibbs 	sc->xbd_flags &= ~XBDF_OPEN;
673fac3fd80SJustin T. Gibbs 	if (--(sc->xbd_users) == 0) {
674fac3fd80SJustin T. Gibbs 		/*
675fac3fd80SJustin T. Gibbs 		 * Check whether we have been instructed to close.  We will
676fac3fd80SJustin T. Gibbs 		 * have ignored this request initially, as the device was
677fac3fd80SJustin T. Gibbs 		 * still mounted.
678fac3fd80SJustin T. Gibbs 		 */
679fac3fd80SJustin T. Gibbs 		if (xenbus_get_otherend_state(sc->xbd_dev) ==
680fac3fd80SJustin T. Gibbs 		    XenbusStateClosing)
681fac3fd80SJustin T. Gibbs 			xbd_closing(sc->xbd_dev);
682fac3fd80SJustin T. Gibbs 	}
683fac3fd80SJustin T. Gibbs 	return (0);
684fac3fd80SJustin T. Gibbs }
685fac3fd80SJustin T. Gibbs 
686fac3fd80SJustin T. Gibbs static int
687fac3fd80SJustin T. Gibbs xbd_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
688fac3fd80SJustin T. Gibbs {
689fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = dp->d_drv1;
690fac3fd80SJustin T. Gibbs 
691fac3fd80SJustin T. Gibbs 	if (sc == NULL)
692fac3fd80SJustin T. Gibbs 		return (ENXIO);
693fac3fd80SJustin T. Gibbs 
694fac3fd80SJustin T. Gibbs 	return (ENOTTY);
695fac3fd80SJustin T. Gibbs }
696fac3fd80SJustin T. Gibbs 
697fac3fd80SJustin T. Gibbs /*
698fac3fd80SJustin T. Gibbs  * Read/write routine for a buffer.  Finds the proper unit, place it on
699fac3fd80SJustin T. Gibbs  * the sortq and kick the controller.
700fac3fd80SJustin T. Gibbs  */
701fac3fd80SJustin T. Gibbs static void
702fac3fd80SJustin T. Gibbs xbd_strategy(struct bio *bp)
703fac3fd80SJustin T. Gibbs {
704fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = bp->bio_disk->d_drv1;
705fac3fd80SJustin T. Gibbs 
706fac3fd80SJustin T. Gibbs 	/* bogus disk? */
707fac3fd80SJustin T. Gibbs 	if (sc == NULL) {
708fac3fd80SJustin T. Gibbs 		bp->bio_error = EINVAL;
709fac3fd80SJustin T. Gibbs 		bp->bio_flags |= BIO_ERROR;
710fac3fd80SJustin T. Gibbs 		bp->bio_resid = bp->bio_bcount;
711fac3fd80SJustin T. Gibbs 		biodone(bp);
712fac3fd80SJustin T. Gibbs 		return;
713fac3fd80SJustin T. Gibbs 	}
714fac3fd80SJustin T. Gibbs 
715fac3fd80SJustin T. Gibbs 	/*
716fac3fd80SJustin T. Gibbs 	 * Place it in the queue of disk activities for this disk
717fac3fd80SJustin T. Gibbs 	 */
718fac3fd80SJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
719fac3fd80SJustin T. Gibbs 
720fac3fd80SJustin T. Gibbs 	xbd_enqueue_bio(sc, bp);
721fac3fd80SJustin T. Gibbs 	xbd_startio(sc);
722fac3fd80SJustin T. Gibbs 
723fac3fd80SJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
724fac3fd80SJustin T. Gibbs 	return;
725fac3fd80SJustin T. Gibbs }
726fac3fd80SJustin T. Gibbs 
727fac3fd80SJustin T. Gibbs /*------------------------------ Ring Management -----------------------------*/
728fac3fd80SJustin T. Gibbs static int
729cdf5d66fSJustin T. Gibbs xbd_alloc_ring(struct xbd_softc *sc)
730fac3fd80SJustin T. Gibbs {
731fac3fd80SJustin T. Gibbs 	blkif_sring_t *sring;
732fac3fd80SJustin T. Gibbs 	uintptr_t sring_page_addr;
733fac3fd80SJustin T. Gibbs 	int error;
734fac3fd80SJustin T. Gibbs 	int i;
735fac3fd80SJustin T. Gibbs 
736fac3fd80SJustin T. Gibbs 	sring = malloc(sc->xbd_ring_pages * PAGE_SIZE, M_XENBLOCKFRONT,
737fac3fd80SJustin T. Gibbs 	    M_NOWAIT|M_ZERO);
738fac3fd80SJustin T. Gibbs 	if (sring == NULL) {
739fac3fd80SJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, ENOMEM, "allocating shared ring");
740fac3fd80SJustin T. Gibbs 		return (ENOMEM);
741fac3fd80SJustin T. Gibbs 	}
742fac3fd80SJustin T. Gibbs 	SHARED_RING_INIT(sring);
743fac3fd80SJustin T. Gibbs 	FRONT_RING_INIT(&sc->xbd_ring, sring, sc->xbd_ring_pages * PAGE_SIZE);
744fac3fd80SJustin T. Gibbs 
745fac3fd80SJustin T. Gibbs 	for (i = 0, sring_page_addr = (uintptr_t)sring;
746fac3fd80SJustin T. Gibbs 	     i < sc->xbd_ring_pages;
747fac3fd80SJustin T. Gibbs 	     i++, sring_page_addr += PAGE_SIZE) {
748fac3fd80SJustin T. Gibbs 
749fac3fd80SJustin T. Gibbs 		error = xenbus_grant_ring(sc->xbd_dev,
750fac3fd80SJustin T. Gibbs 		    (vtomach(sring_page_addr) >> PAGE_SHIFT),
751fac3fd80SJustin T. Gibbs 		    &sc->xbd_ring_ref[i]);
752fac3fd80SJustin T. Gibbs 		if (error) {
753fac3fd80SJustin T. Gibbs 			xenbus_dev_fatal(sc->xbd_dev, error,
754fac3fd80SJustin T. Gibbs 			    "granting ring_ref(%d)", i);
755fac3fd80SJustin T. Gibbs 			return (error);
756fac3fd80SJustin T. Gibbs 		}
757fac3fd80SJustin T. Gibbs 	}
758fac3fd80SJustin T. Gibbs 	if (sc->xbd_ring_pages == 1) {
759fac3fd80SJustin T. Gibbs 		error = xs_printf(XST_NIL, xenbus_get_node(sc->xbd_dev),
760fac3fd80SJustin T. Gibbs 		    "ring-ref", "%u", sc->xbd_ring_ref[0]);
761fac3fd80SJustin T. Gibbs 		if (error) {
762fac3fd80SJustin T. Gibbs 			xenbus_dev_fatal(sc->xbd_dev, error,
763fac3fd80SJustin T. Gibbs 			    "writing %s/ring-ref",
764fac3fd80SJustin T. Gibbs 			    xenbus_get_node(sc->xbd_dev));
765fac3fd80SJustin T. Gibbs 			return (error);
766fac3fd80SJustin T. Gibbs 		}
767fac3fd80SJustin T. Gibbs 	} else {
768fac3fd80SJustin T. Gibbs 		for (i = 0; i < sc->xbd_ring_pages; i++) {
769fac3fd80SJustin T. Gibbs 			char ring_ref_name[]= "ring_refXX";
770fac3fd80SJustin T. Gibbs 
771fac3fd80SJustin T. Gibbs 			snprintf(ring_ref_name, sizeof(ring_ref_name),
772fac3fd80SJustin T. Gibbs 			    "ring-ref%u", i);
773fac3fd80SJustin T. Gibbs 			error = xs_printf(XST_NIL, xenbus_get_node(sc->xbd_dev),
774fac3fd80SJustin T. Gibbs 			     ring_ref_name, "%u", sc->xbd_ring_ref[i]);
775fac3fd80SJustin T. Gibbs 			if (error) {
776fac3fd80SJustin T. Gibbs 				xenbus_dev_fatal(sc->xbd_dev, error,
777fac3fd80SJustin T. Gibbs 				    "writing %s/%s",
778fac3fd80SJustin T. Gibbs 				    xenbus_get_node(sc->xbd_dev),
779fac3fd80SJustin T. Gibbs 				    ring_ref_name);
780fac3fd80SJustin T. Gibbs 				return (error);
781fac3fd80SJustin T. Gibbs 			}
782fac3fd80SJustin T. Gibbs 		}
783fac3fd80SJustin T. Gibbs 	}
784fac3fd80SJustin T. Gibbs 
785fac3fd80SJustin T. Gibbs 	error = bind_listening_port_to_irqhandler(
786fac3fd80SJustin T. Gibbs 	    xenbus_get_otherend_id(sc->xbd_dev),
787fac3fd80SJustin T. Gibbs 	    "xbd", (driver_intr_t *)xbd_int, sc,
788fac3fd80SJustin T. Gibbs 	    INTR_TYPE_BIO | INTR_MPSAFE, &sc->xbd_irq);
789fac3fd80SJustin T. Gibbs 	if (error) {
790fac3fd80SJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
791fac3fd80SJustin T. Gibbs 		    "bind_evtchn_to_irqhandler failed");
792fac3fd80SJustin T. Gibbs 		return (error);
793fac3fd80SJustin T. Gibbs 	}
794fac3fd80SJustin T. Gibbs 
795fac3fd80SJustin T. Gibbs 	return (0);
796fac3fd80SJustin T. Gibbs }
797fac3fd80SJustin T. Gibbs 
798d9fab01dSJustin T. Gibbs static void
799d9fab01dSJustin T. Gibbs xbd_free_ring(struct xbd_softc *sc)
800d9fab01dSJustin T. Gibbs {
801d9fab01dSJustin T. Gibbs 	int i;
802d9fab01dSJustin T. Gibbs 
803d9fab01dSJustin T. Gibbs 	if (sc->xbd_ring.sring == NULL)
804d9fab01dSJustin T. Gibbs 		return;
805d9fab01dSJustin T. Gibbs 
806d9fab01dSJustin T. Gibbs 	for (i = 0; i < sc->xbd_ring_pages; i++) {
807d9fab01dSJustin T. Gibbs 		if (sc->xbd_ring_ref[i] != GRANT_REF_INVALID) {
808d9fab01dSJustin T. Gibbs 			gnttab_end_foreign_access_ref(sc->xbd_ring_ref[i]);
809d9fab01dSJustin T. Gibbs 			sc->xbd_ring_ref[i] = GRANT_REF_INVALID;
810d9fab01dSJustin T. Gibbs 		}
811d9fab01dSJustin T. Gibbs 	}
812d9fab01dSJustin T. Gibbs 	free(sc->xbd_ring.sring, M_XENBLOCKFRONT);
813d9fab01dSJustin T. Gibbs 	sc->xbd_ring.sring = NULL;
814d9fab01dSJustin T. Gibbs }
815d9fab01dSJustin T. Gibbs 
816fac3fd80SJustin T. Gibbs /*-------------------------- Initialization/Teardown -------------------------*/
817*9985113bSJustin T. Gibbs static int
818*9985113bSJustin T. Gibbs xbd_feature_string(struct xbd_softc *sc, char *features, size_t len)
819*9985113bSJustin T. Gibbs {
820*9985113bSJustin T. Gibbs 	struct sbuf sb;
821*9985113bSJustin T. Gibbs 	int feature_cnt;
822*9985113bSJustin T. Gibbs 
823*9985113bSJustin T. Gibbs 	sbuf_new(&sb, features, len, SBUF_FIXEDLEN);
824*9985113bSJustin T. Gibbs 
825*9985113bSJustin T. Gibbs 	feature_cnt = 0;
826*9985113bSJustin T. Gibbs 	if ((sc->xbd_flags & XBDF_FLUSH) != 0) {
827*9985113bSJustin T. Gibbs 		sbuf_printf(&sb, "flush");
828*9985113bSJustin T. Gibbs 		feature_cnt++;
829*9985113bSJustin T. Gibbs 	}
830*9985113bSJustin T. Gibbs 
831*9985113bSJustin T. Gibbs 	if ((sc->xbd_flags & XBDF_BARRIER) != 0) {
832*9985113bSJustin T. Gibbs 		if (feature_cnt != 0)
833*9985113bSJustin T. Gibbs 			sbuf_printf(&sb, ", ");
834*9985113bSJustin T. Gibbs 		sbuf_printf(&sb, "write_barrier");
835*9985113bSJustin T. Gibbs 		feature_cnt++;
836*9985113bSJustin T. Gibbs 	}
837*9985113bSJustin T. Gibbs 
838*9985113bSJustin T. Gibbs 	(void) sbuf_finish(&sb);
839*9985113bSJustin T. Gibbs 	return (sbuf_len(&sb));
840*9985113bSJustin T. Gibbs }
841*9985113bSJustin T. Gibbs 
842*9985113bSJustin T. Gibbs static int
843*9985113bSJustin T. Gibbs xbd_sysctl_features(SYSCTL_HANDLER_ARGS)
844*9985113bSJustin T. Gibbs {
845*9985113bSJustin T. Gibbs 	char features[80];
846*9985113bSJustin T. Gibbs 	struct xbd_softc *sc = arg1;
847*9985113bSJustin T. Gibbs 	int error;
848*9985113bSJustin T. Gibbs 	int len;
849*9985113bSJustin T. Gibbs 
850*9985113bSJustin T. Gibbs 	error = sysctl_wire_old_buffer(req, 0);
851*9985113bSJustin T. Gibbs 	if (error != 0)
852*9985113bSJustin T. Gibbs 		return (error);
853*9985113bSJustin T. Gibbs 
854*9985113bSJustin T. Gibbs 	len = xbd_feature_string(sc, features, sizeof(features));
855*9985113bSJustin T. Gibbs 
856*9985113bSJustin T. Gibbs 	/* len is -1 on error, which will make the SYSCTL_OUT a no-op. */
857*9985113bSJustin T. Gibbs 	return (SYSCTL_OUT(req, features, len + 1/*NUL*/));
858*9985113bSJustin T. Gibbs }
859*9985113bSJustin T. Gibbs 
8608b8bfa35SJustin T. Gibbs static void
86133eebb6aSJustin T. Gibbs xbd_setup_sysctl(struct xbd_softc *xbd)
8628b8bfa35SJustin T. Gibbs {
8638b8bfa35SJustin T. Gibbs 	struct sysctl_ctx_list *sysctl_ctx = NULL;
8648b8bfa35SJustin T. Gibbs 	struct sysctl_oid *sysctl_tree = NULL;
865*9985113bSJustin T. Gibbs 	struct sysctl_oid_list *children;
8668b8bfa35SJustin T. Gibbs 
86733eebb6aSJustin T. Gibbs 	sysctl_ctx = device_get_sysctl_ctx(xbd->xbd_dev);
8688b8bfa35SJustin T. Gibbs 	if (sysctl_ctx == NULL)
8698b8bfa35SJustin T. Gibbs 		return;
8708b8bfa35SJustin T. Gibbs 
87133eebb6aSJustin T. Gibbs 	sysctl_tree = device_get_sysctl_tree(xbd->xbd_dev);
8728b8bfa35SJustin T. Gibbs 	if (sysctl_tree == NULL)
8738b8bfa35SJustin T. Gibbs 		return;
8748b8bfa35SJustin T. Gibbs 
875*9985113bSJustin T. Gibbs 	children = SYSCTL_CHILDREN(sysctl_tree);
876*9985113bSJustin T. Gibbs 	SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO,
87733eebb6aSJustin T. Gibbs 	    "max_requests", CTLFLAG_RD, &xbd->xbd_max_requests, -1,
8788b8bfa35SJustin T. Gibbs 	    "maximum outstanding requests (negotiated)");
8798b8bfa35SJustin T. Gibbs 
880*9985113bSJustin T. Gibbs 	SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO,
881*9985113bSJustin T. Gibbs 	    "max_requests", CTLFLAG_RD, &xbd->xbd_max_requests, -1,
882*9985113bSJustin T. Gibbs 	    "maximum outstanding requests (negotiated)");
883*9985113bSJustin T. Gibbs 
884*9985113bSJustin T. Gibbs 	SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO,
8858b8bfa35SJustin T. Gibbs 	    "max_request_segments", CTLFLAG_RD,
88633eebb6aSJustin T. Gibbs 	    &xbd->xbd_max_request_segments, 0,
8878b8bfa35SJustin T. Gibbs 	    "maximum number of pages per requests (negotiated)");
8888b8bfa35SJustin T. Gibbs 
889*9985113bSJustin T. Gibbs 	SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO,
89033eebb6aSJustin T. Gibbs 	    "max_request_size", CTLFLAG_RD, &xbd->xbd_max_request_size, 0,
8918b8bfa35SJustin T. Gibbs 	    "maximum size in bytes of a request (negotiated)");
8928b8bfa35SJustin T. Gibbs 
893*9985113bSJustin T. Gibbs 	SYSCTL_ADD_UINT(sysctl_ctx, children, OID_AUTO,
89433eebb6aSJustin T. Gibbs 	    "ring_pages", CTLFLAG_RD, &xbd->xbd_ring_pages, 0,
8958b8bfa35SJustin T. Gibbs 	    "communication channel pages (negotiated)");
896*9985113bSJustin T. Gibbs 
897*9985113bSJustin T. Gibbs 	SYSCTL_ADD_PROC(sysctl_ctx, children, OID_AUTO,
898*9985113bSJustin T. Gibbs 	    "features", CTLTYPE_STRING|CTLFLAG_RD, xbd, 0,
899*9985113bSJustin T. Gibbs 	    xbd_sysctl_features, "A", "protocol features (negotiated)");
9008b8bfa35SJustin T. Gibbs }
9018b8bfa35SJustin T. Gibbs 
90223dc5621SKip Macy /*
903fac3fd80SJustin T. Gibbs  * Translate Linux major/minor to an appropriate name and unit
904fac3fd80SJustin T. Gibbs  * number. For HVM guests, this allows us to use the same drive names
905fac3fd80SJustin T. Gibbs  * with blkfront as the emulated drives, easing transition slightly.
90623dc5621SKip Macy  */
907fac3fd80SJustin T. Gibbs static void
908fac3fd80SJustin T. Gibbs xbd_vdevice_to_unit(uint32_t vdevice, int *unit, const char **name)
90923dc5621SKip Macy {
910fac3fd80SJustin T. Gibbs 	static struct vdev_info {
911fac3fd80SJustin T. Gibbs 		int major;
912fac3fd80SJustin T. Gibbs 		int shift;
913fac3fd80SJustin T. Gibbs 		int base;
91423dc5621SKip Macy 		const char *name;
915fac3fd80SJustin T. Gibbs 	} info[] = {
916fac3fd80SJustin T. Gibbs 		{3,	6,	0,	"ada"},	/* ide0 */
917fac3fd80SJustin T. Gibbs 		{22,	6,	2,	"ada"},	/* ide1 */
918fac3fd80SJustin T. Gibbs 		{33,	6,	4,	"ada"},	/* ide2 */
919fac3fd80SJustin T. Gibbs 		{34,	6,	6,	"ada"},	/* ide3 */
920fac3fd80SJustin T. Gibbs 		{56,	6,	8,	"ada"},	/* ide4 */
921fac3fd80SJustin T. Gibbs 		{57,	6,	10,	"ada"},	/* ide5 */
922fac3fd80SJustin T. Gibbs 		{88,	6,	12,	"ada"},	/* ide6 */
923fac3fd80SJustin T. Gibbs 		{89,	6,	14,	"ada"},	/* ide7 */
924fac3fd80SJustin T. Gibbs 		{90,	6,	16,	"ada"},	/* ide8 */
925fac3fd80SJustin T. Gibbs 		{91,	6,	18,	"ada"},	/* ide9 */
92689e0f4d2SKip Macy 
927fac3fd80SJustin T. Gibbs 		{8,	4,	0,	"da"},	/* scsi disk0 */
928fac3fd80SJustin T. Gibbs 		{65,	4,	16,	"da"},	/* scsi disk1 */
929fac3fd80SJustin T. Gibbs 		{66,	4,	32,	"da"},	/* scsi disk2 */
930fac3fd80SJustin T. Gibbs 		{67,	4,	48,	"da"},	/* scsi disk3 */
931fac3fd80SJustin T. Gibbs 		{68,	4,	64,	"da"},	/* scsi disk4 */
932fac3fd80SJustin T. Gibbs 		{69,	4,	80,	"da"},	/* scsi disk5 */
933fac3fd80SJustin T. Gibbs 		{70,	4,	96,	"da"},	/* scsi disk6 */
934fac3fd80SJustin T. Gibbs 		{71,	4,	112,	"da"},	/* scsi disk7 */
935fac3fd80SJustin T. Gibbs 		{128,	4,	128,	"da"},	/* scsi disk8 */
936fac3fd80SJustin T. Gibbs 		{129,	4,	144,	"da"},	/* scsi disk9 */
937fac3fd80SJustin T. Gibbs 		{130,	4,	160,	"da"},	/* scsi disk10 */
938fac3fd80SJustin T. Gibbs 		{131,	4,	176,	"da"},	/* scsi disk11 */
939fac3fd80SJustin T. Gibbs 		{132,	4,	192,	"da"},	/* scsi disk12 */
940fac3fd80SJustin T. Gibbs 		{133,	4,	208,	"da"},	/* scsi disk13 */
941fac3fd80SJustin T. Gibbs 		{134,	4,	224,	"da"},	/* scsi disk14 */
942fac3fd80SJustin T. Gibbs 		{135,	4,	240,	"da"},	/* scsi disk15 */
943fac3fd80SJustin T. Gibbs 
944fac3fd80SJustin T. Gibbs 		{202,	4,	0,	"xbd"},	/* xbd */
945fac3fd80SJustin T. Gibbs 
946fac3fd80SJustin T. Gibbs 		{0,	0,	0,	NULL},
947fac3fd80SJustin T. Gibbs 	};
948fac3fd80SJustin T. Gibbs 	int major = vdevice >> 8;
949fac3fd80SJustin T. Gibbs 	int minor = vdevice & 0xff;
950fac3fd80SJustin T. Gibbs 	int i;
951fac3fd80SJustin T. Gibbs 
952fac3fd80SJustin T. Gibbs 	if (vdevice & (1 << 28)) {
953fac3fd80SJustin T. Gibbs 		*unit = (vdevice & ((1 << 28) - 1)) >> 8;
954fac3fd80SJustin T. Gibbs 		*name = "xbd";
955fac3fd80SJustin T. Gibbs 		return;
95689e0f4d2SKip Macy 	}
95789e0f4d2SKip Macy 
958fac3fd80SJustin T. Gibbs 	for (i = 0; info[i].major; i++) {
959fac3fd80SJustin T. Gibbs 		if (info[i].major == major) {
960fac3fd80SJustin T. Gibbs 			*unit = info[i].base + (minor >> info[i].shift);
961fac3fd80SJustin T. Gibbs 			*name = info[i].name;
962fac3fd80SJustin T. Gibbs 			return;
963fac3fd80SJustin T. Gibbs 		}
964fac3fd80SJustin T. Gibbs 	}
965fac3fd80SJustin T. Gibbs 
966fac3fd80SJustin T. Gibbs 	*unit = minor >> 4;
967fac3fd80SJustin T. Gibbs 	*name = "xbd";
968fac3fd80SJustin T. Gibbs }
969fac3fd80SJustin T. Gibbs 
970fac3fd80SJustin T. Gibbs int
971fac3fd80SJustin T. Gibbs xbd_instance_create(struct xbd_softc *sc, blkif_sector_t sectors,
972fac3fd80SJustin T. Gibbs     int vdevice, uint16_t vdisk_info, unsigned long sector_size)
973fac3fd80SJustin T. Gibbs {
974*9985113bSJustin T. Gibbs 	char features[80];
975fac3fd80SJustin T. Gibbs 	int unit, error = 0;
976fac3fd80SJustin T. Gibbs 	const char *name;
977fac3fd80SJustin T. Gibbs 
97833eebb6aSJustin T. Gibbs 	xbd_vdevice_to_unit(vdevice, &unit, &name);
97923dc5621SKip Macy 
980fac3fd80SJustin T. Gibbs 	sc->xbd_unit = unit;
98189e0f4d2SKip Macy 
982*9985113bSJustin T. Gibbs 	if (strcmp(name, "xbd") != 0)
983fac3fd80SJustin T. Gibbs 		device_printf(sc->xbd_dev, "attaching as %s%d\n", name, unit);
98489e0f4d2SKip Macy 
985*9985113bSJustin T. Gibbs 	if (xbd_feature_string(sc, features, sizeof(features)) > 0) {
986*9985113bSJustin T. Gibbs 		device_printf(sc->xbd_dev, "features: %s\n",
987*9985113bSJustin T. Gibbs 		    features);
988*9985113bSJustin T. Gibbs 	}
989*9985113bSJustin T. Gibbs 
990fac3fd80SJustin T. Gibbs 	sc->xbd_disk = disk_alloc();
991fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_unit = sc->xbd_unit;
992fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_open = xbd_open;
993fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_close = xbd_close;
994fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_ioctl = xbd_ioctl;
995fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_strategy = xbd_strategy;
996fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_dump = xbd_dump;
997fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_name = name;
998fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_drv1 = sc;
999fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_sectorsize = sector_size;
10008b8bfa35SJustin T. Gibbs 
1001fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_mediasize = sectors * sector_size;
1002fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_maxsize = sc->xbd_max_request_size;
1003fac3fd80SJustin T. Gibbs 	sc->xbd_disk->d_flags = 0;
1004*9985113bSJustin T. Gibbs 	if ((sc->xbd_flags & (XBDF_FLUSH|XBDF_BARRIER)) != 0) {
1005*9985113bSJustin T. Gibbs 		sc->xbd_disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1006*9985113bSJustin T. Gibbs 		device_printf(sc->xbd_dev,
1007*9985113bSJustin T. Gibbs 		    "synchronize cache commands enabled.\n");
1008*9985113bSJustin T. Gibbs 	}
1009fac3fd80SJustin T. Gibbs 	disk_create(sc->xbd_disk, DISK_VERSION);
101012678024SDoug Rabson 
1011fac3fd80SJustin T. Gibbs 	return error;
101212678024SDoug Rabson }
101312678024SDoug Rabson 
1014fac3fd80SJustin T. Gibbs static void
1015fac3fd80SJustin T. Gibbs xbd_free(struct xbd_softc *sc)
101612678024SDoug Rabson {
1017fac3fd80SJustin T. Gibbs 	int i;
101812678024SDoug Rabson 
101912678024SDoug Rabson 	/* Prevent new requests being issued until we fix things up. */
102033eebb6aSJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
1021e2c1fe90SJustin T. Gibbs 	sc->xbd_state = XBD_STATE_DISCONNECTED;
102233eebb6aSJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
102389e0f4d2SKip Macy 
1024fac3fd80SJustin T. Gibbs 	/* Free resources associated with old device channel. */
1025d9fab01dSJustin T. Gibbs 	xbd_free_ring(sc);
1026fac3fd80SJustin T. Gibbs 	if (sc->xbd_shadow) {
102789e0f4d2SKip Macy 
1028fac3fd80SJustin T. Gibbs 		for (i = 0; i < sc->xbd_max_requests; i++) {
1029fac3fd80SJustin T. Gibbs 			struct xbd_command *cm;
103089e0f4d2SKip Macy 
1031fac3fd80SJustin T. Gibbs 			cm = &sc->xbd_shadow[i];
1032fac3fd80SJustin T. Gibbs 			if (cm->cm_sg_refs != NULL) {
1033fac3fd80SJustin T. Gibbs 				free(cm->cm_sg_refs, M_XENBLOCKFRONT);
1034fac3fd80SJustin T. Gibbs 				cm->cm_sg_refs = NULL;
103589e0f4d2SKip Macy 			}
103689e0f4d2SKip Macy 
1037fac3fd80SJustin T. Gibbs 			bus_dmamap_destroy(sc->xbd_io_dmat, cm->cm_map);
1038fac3fd80SJustin T. Gibbs 		}
1039fac3fd80SJustin T. Gibbs 		free(sc->xbd_shadow, M_XENBLOCKFRONT);
1040fac3fd80SJustin T. Gibbs 		sc->xbd_shadow = NULL;
1041fac3fd80SJustin T. Gibbs 
1042fac3fd80SJustin T. Gibbs 		bus_dma_tag_destroy(sc->xbd_io_dmat);
1043fac3fd80SJustin T. Gibbs 
1044e2c1fe90SJustin T. Gibbs 		xbd_initq_cm(sc, XBD_Q_FREE);
1045e2c1fe90SJustin T. Gibbs 		xbd_initq_cm(sc, XBD_Q_READY);
1046e2c1fe90SJustin T. Gibbs 		xbd_initq_cm(sc, XBD_Q_COMPLETE);
1047fac3fd80SJustin T. Gibbs 	}
1048fac3fd80SJustin T. Gibbs 
1049fac3fd80SJustin T. Gibbs 	if (sc->xbd_irq) {
1050fac3fd80SJustin T. Gibbs 		unbind_from_irqhandler(sc->xbd_irq);
1051fac3fd80SJustin T. Gibbs 		sc->xbd_irq = 0;
1052fac3fd80SJustin T. Gibbs 	}
1053fac3fd80SJustin T. Gibbs }
1054fac3fd80SJustin T. Gibbs 
1055fac3fd80SJustin T. Gibbs /*--------------------------- State Change Handlers --------------------------*/
1056ff662b5cSJustin T. Gibbs static void
105733eebb6aSJustin T. Gibbs xbd_initialize(struct xbd_softc *sc)
105889e0f4d2SKip Macy {
1059ff662b5cSJustin T. Gibbs 	const char *otherend_path;
1060ff662b5cSJustin T. Gibbs 	const char *node_path;
10618b8bfa35SJustin T. Gibbs 	uint32_t max_ring_page_order;
1062ff662b5cSJustin T. Gibbs 	int error;
1063ff662b5cSJustin T. Gibbs 	int i;
106489e0f4d2SKip Macy 
106533eebb6aSJustin T. Gibbs 	if (xenbus_get_state(sc->xbd_dev) != XenbusStateInitialising) {
106606a630f6SJustin T. Gibbs 		/* Initialization has already been performed. */
1067ff662b5cSJustin T. Gibbs 		return;
106806a630f6SJustin T. Gibbs 	}
106989e0f4d2SKip Macy 
1070ff662b5cSJustin T. Gibbs 	/*
1071ff662b5cSJustin T. Gibbs 	 * Protocol defaults valid even if negotiation for a
1072ff662b5cSJustin T. Gibbs 	 * setting fails.
1073ff662b5cSJustin T. Gibbs 	 */
10748b8bfa35SJustin T. Gibbs 	max_ring_page_order = 0;
107533eebb6aSJustin T. Gibbs 	sc->xbd_ring_pages = 1;
107633eebb6aSJustin T. Gibbs 	sc->xbd_max_request_segments = BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK;
107733eebb6aSJustin T. Gibbs 	sc->xbd_max_request_size =
107833eebb6aSJustin T. Gibbs 	    XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments);
107933eebb6aSJustin T. Gibbs 	sc->xbd_max_request_blocks =
108033eebb6aSJustin T. Gibbs 	    BLKIF_SEGS_TO_BLOCKS(sc->xbd_max_request_segments);
1081ff662b5cSJustin T. Gibbs 
1082ff662b5cSJustin T. Gibbs 	/*
1083ff662b5cSJustin T. Gibbs 	 * Protocol negotiation.
1084ff662b5cSJustin T. Gibbs 	 *
1085ff662b5cSJustin T. Gibbs 	 * \note xs_gather() returns on the first encountered error, so
1086ff662b5cSJustin T. Gibbs 	 *       we must use independant calls in order to guarantee
1087ff662b5cSJustin T. Gibbs 	 *       we don't miss information in a sparsly populated back-end
1088ff662b5cSJustin T. Gibbs 	 *       tree.
10898b8bfa35SJustin T. Gibbs 	 *
10908b8bfa35SJustin T. Gibbs 	 * \note xs_scanf() does not update variables for unmatched
10918b8bfa35SJustin T. Gibbs 	 *	 fields.
1092ff662b5cSJustin T. Gibbs 	 */
109333eebb6aSJustin T. Gibbs 	otherend_path = xenbus_get_otherend_path(sc->xbd_dev);
109433eebb6aSJustin T. Gibbs 	node_path = xenbus_get_node(sc->xbd_dev);
10958b8bfa35SJustin T. Gibbs 
10968b8bfa35SJustin T. Gibbs 	/* Support both backend schemes for relaying ring page limits. */
10978b8bfa35SJustin T. Gibbs 	(void)xs_scanf(XST_NIL, otherend_path,
10988b8bfa35SJustin T. Gibbs 	    "max-ring-page-order", NULL, "%" PRIu32,
10998b8bfa35SJustin T. Gibbs 	    &max_ring_page_order);
110033eebb6aSJustin T. Gibbs 	sc->xbd_ring_pages = 1 << max_ring_page_order;
1101ff662b5cSJustin T. Gibbs 	(void)xs_scanf(XST_NIL, otherend_path,
1102ff662b5cSJustin T. Gibbs 	    "max-ring-pages", NULL, "%" PRIu32,
110333eebb6aSJustin T. Gibbs 	    &sc->xbd_ring_pages);
110433eebb6aSJustin T. Gibbs 	if (sc->xbd_ring_pages < 1)
110533eebb6aSJustin T. Gibbs 		sc->xbd_ring_pages = 1;
1106ff662b5cSJustin T. Gibbs 
110733eebb6aSJustin T. Gibbs 	sc->xbd_max_requests =
110833eebb6aSJustin T. Gibbs 	    BLKIF_MAX_RING_REQUESTS(sc->xbd_ring_pages * PAGE_SIZE);
1109ff662b5cSJustin T. Gibbs 	(void)xs_scanf(XST_NIL, otherend_path,
1110ff662b5cSJustin T. Gibbs 	    "max-requests", NULL, "%" PRIu32,
111133eebb6aSJustin T. Gibbs 	    &sc->xbd_max_requests);
1112ff662b5cSJustin T. Gibbs 
1113ff662b5cSJustin T. Gibbs 	(void)xs_scanf(XST_NIL, otherend_path,
1114ff662b5cSJustin T. Gibbs 	    "max-request-segments", NULL, "%" PRIu32,
111533eebb6aSJustin T. Gibbs 	    &sc->xbd_max_request_segments);
1116ff662b5cSJustin T. Gibbs 
1117ff662b5cSJustin T. Gibbs 	(void)xs_scanf(XST_NIL, otherend_path,
1118ff662b5cSJustin T. Gibbs 	    "max-request-size", NULL, "%" PRIu32,
111933eebb6aSJustin T. Gibbs 	    &sc->xbd_max_request_size);
1120ff662b5cSJustin T. Gibbs 
112133eebb6aSJustin T. Gibbs 	if (sc->xbd_ring_pages > XBD_MAX_RING_PAGES) {
112233eebb6aSJustin T. Gibbs 		device_printf(sc->xbd_dev,
112333eebb6aSJustin T. Gibbs 		    "Back-end specified ring-pages of %u "
112433eebb6aSJustin T. Gibbs 		    "limited to front-end limit of %zu.\n",
112533eebb6aSJustin T. Gibbs 		    sc->xbd_ring_pages, XBD_MAX_RING_PAGES);
112633eebb6aSJustin T. Gibbs 		sc->xbd_ring_pages = XBD_MAX_RING_PAGES;
112789e0f4d2SKip Macy 	}
112889e0f4d2SKip Macy 
112933eebb6aSJustin T. Gibbs 	if (powerof2(sc->xbd_ring_pages) == 0) {
11308b8bfa35SJustin T. Gibbs 		uint32_t new_page_limit;
11318b8bfa35SJustin T. Gibbs 
113233eebb6aSJustin T. Gibbs 		new_page_limit = 0x01 << (fls(sc->xbd_ring_pages) - 1);
113333eebb6aSJustin T. Gibbs 		device_printf(sc->xbd_dev,
113433eebb6aSJustin T. Gibbs 		    "Back-end specified ring-pages of %u "
113533eebb6aSJustin T. Gibbs 		    "is not a power of 2. Limited to %u.\n",
113633eebb6aSJustin T. Gibbs 		    sc->xbd_ring_pages, new_page_limit);
113733eebb6aSJustin T. Gibbs 		sc->xbd_ring_pages = new_page_limit;
11388b8bfa35SJustin T. Gibbs 	}
11398b8bfa35SJustin T. Gibbs 
114033eebb6aSJustin T. Gibbs 	if (sc->xbd_max_requests > XBD_MAX_REQUESTS) {
114133eebb6aSJustin T. Gibbs 		device_printf(sc->xbd_dev,
114233eebb6aSJustin T. Gibbs 		    "Back-end specified max_requests of %u "
114333eebb6aSJustin T. Gibbs 		    "limited to front-end limit of %u.\n",
114433eebb6aSJustin T. Gibbs 		    sc->xbd_max_requests, XBD_MAX_REQUESTS);
114533eebb6aSJustin T. Gibbs 		sc->xbd_max_requests = XBD_MAX_REQUESTS;
114689e0f4d2SKip Macy 	}
1147ff662b5cSJustin T. Gibbs 
114833eebb6aSJustin T. Gibbs 	if (sc->xbd_max_request_segments > XBD_MAX_SEGMENTS_PER_REQUEST) {
114933eebb6aSJustin T. Gibbs 		device_printf(sc->xbd_dev,
115033eebb6aSJustin T. Gibbs 		    "Back-end specified max_request_segments of %u "
115133eebb6aSJustin T. Gibbs 		    "limited to front-end limit of %u.\n",
115233eebb6aSJustin T. Gibbs 		    sc->xbd_max_request_segments,
115333eebb6aSJustin T. Gibbs 		    XBD_MAX_SEGMENTS_PER_REQUEST);
115433eebb6aSJustin T. Gibbs 		sc->xbd_max_request_segments = XBD_MAX_SEGMENTS_PER_REQUEST;
115589e0f4d2SKip Macy 	}
1156ff662b5cSJustin T. Gibbs 
115733eebb6aSJustin T. Gibbs 	if (sc->xbd_max_request_size > XBD_MAX_REQUEST_SIZE) {
115833eebb6aSJustin T. Gibbs 		device_printf(sc->xbd_dev,
115933eebb6aSJustin T. Gibbs 		    "Back-end specified max_request_size of %u "
116033eebb6aSJustin T. Gibbs 		    "limited to front-end limit of %u.\n",
116133eebb6aSJustin T. Gibbs 		    sc->xbd_max_request_size,
116233eebb6aSJustin T. Gibbs 		    XBD_MAX_REQUEST_SIZE);
116333eebb6aSJustin T. Gibbs 		sc->xbd_max_request_size = XBD_MAX_REQUEST_SIZE;
1164ff662b5cSJustin T. Gibbs 	}
1165443cc4d4SJustin T. Gibbs 
116633eebb6aSJustin T. Gibbs  	if (sc->xbd_max_request_size >
116733eebb6aSJustin T. Gibbs 	    XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments)) {
116833eebb6aSJustin T. Gibbs  		device_printf(sc->xbd_dev,
116933eebb6aSJustin T. Gibbs 		    "Back-end specified max_request_size of %u "
117033eebb6aSJustin T. Gibbs 		    "limited to front-end limit of %u.  (Too few segments.)\n",
117133eebb6aSJustin T. Gibbs 		    sc->xbd_max_request_size,
117233eebb6aSJustin T. Gibbs 		    XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments));
117333eebb6aSJustin T. Gibbs  		sc->xbd_max_request_size =
117433eebb6aSJustin T. Gibbs  		    XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments);
1175443cc4d4SJustin T. Gibbs  	}
1176443cc4d4SJustin T. Gibbs 
117733eebb6aSJustin T. Gibbs 	sc->xbd_max_request_blocks =
117833eebb6aSJustin T. Gibbs 	    BLKIF_SEGS_TO_BLOCKS(sc->xbd_max_request_segments);
1179ff662b5cSJustin T. Gibbs 
1180ff662b5cSJustin T. Gibbs 	/* Allocate datastructures based on negotiated values. */
118133eebb6aSJustin T. Gibbs 	error = bus_dma_tag_create(
118233eebb6aSJustin T. Gibbs 	    bus_get_dma_tag(sc->xbd_dev),	/* parent */
1183ff662b5cSJustin T. Gibbs 	    512, PAGE_SIZE,			/* algnmnt, boundary */
1184ff662b5cSJustin T. Gibbs 	    BUS_SPACE_MAXADDR,			/* lowaddr */
1185ff662b5cSJustin T. Gibbs 	    BUS_SPACE_MAXADDR,			/* highaddr */
1186ff662b5cSJustin T. Gibbs 	    NULL, NULL,				/* filter, filterarg */
118733eebb6aSJustin T. Gibbs 	    sc->xbd_max_request_size,
118833eebb6aSJustin T. Gibbs 	    sc->xbd_max_request_segments,
1189ff662b5cSJustin T. Gibbs 	    PAGE_SIZE,				/* maxsegsize */
1190ff662b5cSJustin T. Gibbs 	    BUS_DMA_ALLOCNOW,			/* flags */
1191ff662b5cSJustin T. Gibbs 	    busdma_lock_mutex,			/* lockfunc */
119233eebb6aSJustin T. Gibbs 	    &sc->xbd_io_lock,			/* lockarg */
119333eebb6aSJustin T. Gibbs 	    &sc->xbd_io_dmat);
1194ff662b5cSJustin T. Gibbs 	if (error != 0) {
119533eebb6aSJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
1196ff662b5cSJustin T. Gibbs 		    "Cannot allocate parent DMA tag\n");
1197ff662b5cSJustin T. Gibbs 		return;
1198ff662b5cSJustin T. Gibbs 	}
1199ff662b5cSJustin T. Gibbs 
1200ff662b5cSJustin T. Gibbs 	/* Per-transaction data allocation. */
120133eebb6aSJustin T. Gibbs 	sc->xbd_shadow = malloc(sizeof(*sc->xbd_shadow) * sc->xbd_max_requests,
1202ff662b5cSJustin T. Gibbs 	    M_XENBLOCKFRONT, M_NOWAIT|M_ZERO);
120333eebb6aSJustin T. Gibbs 	if (sc->xbd_shadow == NULL) {
120433eebb6aSJustin T. Gibbs 		bus_dma_tag_destroy(sc->xbd_io_dmat);
120533eebb6aSJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
1206ff662b5cSJustin T. Gibbs 		    "Cannot allocate request structures\n");
120706a630f6SJustin T. Gibbs 		return;
1208ff662b5cSJustin T. Gibbs 	}
1209ff662b5cSJustin T. Gibbs 
121033eebb6aSJustin T. Gibbs 	for (i = 0; i < sc->xbd_max_requests; i++) {
121133eebb6aSJustin T. Gibbs 		struct xbd_command *cm;
1212ff662b5cSJustin T. Gibbs 
121333eebb6aSJustin T. Gibbs 		cm = &sc->xbd_shadow[i];
121433eebb6aSJustin T. Gibbs 		cm->cm_sg_refs = malloc(
121533eebb6aSJustin T. Gibbs 		    sizeof(grant_ref_t) * sc->xbd_max_request_segments,
1216ff662b5cSJustin T. Gibbs 		    M_XENBLOCKFRONT, M_NOWAIT);
121733eebb6aSJustin T. Gibbs 		if (cm->cm_sg_refs == NULL)
1218ff662b5cSJustin T. Gibbs 			break;
121933eebb6aSJustin T. Gibbs 		cm->cm_id = i;
1220e2c1fe90SJustin T. Gibbs 		cm->cm_flags = XBDCF_INITIALIZER;
1221ff662b5cSJustin T. Gibbs 		cm->cm_sc = sc;
122233eebb6aSJustin T. Gibbs 		if (bus_dmamap_create(sc->xbd_io_dmat, 0, &cm->cm_map) != 0)
1223ff662b5cSJustin T. Gibbs 			break;
122433eebb6aSJustin T. Gibbs 		xbd_free_command(cm);
1225ff662b5cSJustin T. Gibbs 	}
1226ff662b5cSJustin T. Gibbs 
1227cdf5d66fSJustin T. Gibbs 	if (xbd_alloc_ring(sc) != 0)
1228ff662b5cSJustin T. Gibbs 		return;
1229ff662b5cSJustin T. Gibbs 
12308b8bfa35SJustin T. Gibbs 	/* Support both backend schemes for relaying ring page limits. */
123133eebb6aSJustin T. Gibbs 	if (sc->xbd_ring_pages > 1) {
1232ff662b5cSJustin T. Gibbs 		error = xs_printf(XST_NIL, node_path,
123333eebb6aSJustin T. Gibbs 		    "num-ring-pages","%u",
123433eebb6aSJustin T. Gibbs 		    sc->xbd_ring_pages);
1235ff662b5cSJustin T. Gibbs 		if (error) {
123633eebb6aSJustin T. Gibbs 			xenbus_dev_fatal(sc->xbd_dev, error,
12378b8bfa35SJustin T. Gibbs 			    "writing %s/num-ring-pages",
12388b8bfa35SJustin T. Gibbs 			    node_path);
12398b8bfa35SJustin T. Gibbs 			return;
12408b8bfa35SJustin T. Gibbs 		}
12410d172324SJustin T. Gibbs 
12428b8bfa35SJustin T. Gibbs 		error = xs_printf(XST_NIL, node_path,
12430d172324SJustin T. Gibbs 		    "ring-page-order", "%u",
124433eebb6aSJustin T. Gibbs 		    fls(sc->xbd_ring_pages) - 1);
12458b8bfa35SJustin T. Gibbs 		if (error) {
124633eebb6aSJustin T. Gibbs 			xenbus_dev_fatal(sc->xbd_dev, error,
12478b8bfa35SJustin T. Gibbs 			    "writing %s/ring-page-order",
1248ff662b5cSJustin T. Gibbs 			    node_path);
1249ff662b5cSJustin T. Gibbs 			return;
1250ff662b5cSJustin T. Gibbs 		}
12510d172324SJustin T. Gibbs 	}
1252ff662b5cSJustin T. Gibbs 
1253ff662b5cSJustin T. Gibbs 	error = xs_printf(XST_NIL, node_path,
125433eebb6aSJustin T. Gibbs 	    "max-requests","%u",
125533eebb6aSJustin T. Gibbs 	    sc->xbd_max_requests);
1256ff662b5cSJustin T. Gibbs 	if (error) {
125733eebb6aSJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
1258ff662b5cSJustin T. Gibbs 		    "writing %s/max-requests",
1259ff662b5cSJustin T. Gibbs 		    node_path);
1260ff662b5cSJustin T. Gibbs 		return;
1261ff662b5cSJustin T. Gibbs 	}
1262ff662b5cSJustin T. Gibbs 
1263ff662b5cSJustin T. Gibbs 	error = xs_printf(XST_NIL, node_path,
126433eebb6aSJustin T. Gibbs 	    "max-request-segments","%u",
126533eebb6aSJustin T. Gibbs 	    sc->xbd_max_request_segments);
1266ff662b5cSJustin T. Gibbs 	if (error) {
126733eebb6aSJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
1268ff662b5cSJustin T. Gibbs 		    "writing %s/max-request-segments",
1269ff662b5cSJustin T. Gibbs 		    node_path);
1270ff662b5cSJustin T. Gibbs 		return;
1271ff662b5cSJustin T. Gibbs 	}
1272ff662b5cSJustin T. Gibbs 
1273ff662b5cSJustin T. Gibbs 	error = xs_printf(XST_NIL, node_path,
127433eebb6aSJustin T. Gibbs 	    "max-request-size","%u",
127533eebb6aSJustin T. Gibbs 	    sc->xbd_max_request_size);
1276ff662b5cSJustin T. Gibbs 	if (error) {
127733eebb6aSJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
1278ff662b5cSJustin T. Gibbs 		    "writing %s/max-request-size",
1279ff662b5cSJustin T. Gibbs 		    node_path);
1280ff662b5cSJustin T. Gibbs 		return;
1281ff662b5cSJustin T. Gibbs 	}
1282ff662b5cSJustin T. Gibbs 
1283ff662b5cSJustin T. Gibbs 	error = xs_printf(XST_NIL, node_path, "event-channel",
128433eebb6aSJustin T. Gibbs 	    "%u", irq_to_evtchn_port(sc->xbd_irq));
1285ff662b5cSJustin T. Gibbs 	if (error) {
128633eebb6aSJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
1287ff662b5cSJustin T. Gibbs 		    "writing %s/event-channel",
1288ff662b5cSJustin T. Gibbs 		    node_path);
1289ff662b5cSJustin T. Gibbs 		return;
1290ff662b5cSJustin T. Gibbs 	}
1291ff662b5cSJustin T. Gibbs 
129233eebb6aSJustin T. Gibbs 	error = xs_printf(XST_NIL, node_path, "protocol",
129333eebb6aSJustin T. Gibbs 	    "%s", XEN_IO_PROTO_ABI_NATIVE);
1294ff662b5cSJustin T. Gibbs 	if (error) {
129533eebb6aSJustin T. Gibbs 		xenbus_dev_fatal(sc->xbd_dev, error,
1296ff662b5cSJustin T. Gibbs 		    "writing %s/protocol",
1297ff662b5cSJustin T. Gibbs 		    node_path);
1298ff662b5cSJustin T. Gibbs 		return;
129923dc5621SKip Macy 	}
130012678024SDoug Rabson 
130133eebb6aSJustin T. Gibbs 	xenbus_set_state(sc->xbd_dev, XenbusStateInitialised);
130289e0f4d2SKip Macy }
130389e0f4d2SKip Macy 
130489e0f4d2SKip Macy /*
130533eebb6aSJustin T. Gibbs  * Invoked when the backend is finally 'ready' (and has published
130633eebb6aSJustin T. Gibbs  * the details about the physical device - #sectors, size, etc).
130789e0f4d2SKip Macy  */
130889e0f4d2SKip Macy static void
130933eebb6aSJustin T. Gibbs xbd_connect(struct xbd_softc *sc)
131089e0f4d2SKip Macy {
131133eebb6aSJustin T. Gibbs 	device_t dev = sc->xbd_dev;
131289e0f4d2SKip Macy 	unsigned long sectors, sector_size;
131389e0f4d2SKip Macy 	unsigned int binfo;
1314*9985113bSJustin T. Gibbs 	int err, feature_barrier, feature_flush;
131589e0f4d2SKip Macy 
1316e2c1fe90SJustin T. Gibbs 	if (sc->xbd_state == XBD_STATE_CONNECTED ||
1317e2c1fe90SJustin T. Gibbs 	    sc->xbd_state == XBD_STATE_SUSPENDED)
131889e0f4d2SKip Macy 		return;
131989e0f4d2SKip Macy 
132023dc5621SKip Macy 	DPRINTK("blkfront.c:connect:%s.\n", xenbus_get_otherend_path(dev));
132189e0f4d2SKip Macy 
1322ff662b5cSJustin T. Gibbs 	err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
132389e0f4d2SKip Macy 	    "sectors", "%lu", &sectors,
132489e0f4d2SKip Macy 	    "info", "%u", &binfo,
132589e0f4d2SKip Macy 	    "sector-size", "%lu", &sector_size,
132689e0f4d2SKip Macy 	    NULL);
132789e0f4d2SKip Macy 	if (err) {
132823dc5621SKip Macy 		xenbus_dev_fatal(dev, err,
132989e0f4d2SKip Macy 		    "reading backend fields at %s",
133023dc5621SKip Macy 		    xenbus_get_otherend_path(dev));
133189e0f4d2SKip Macy 		return;
133289e0f4d2SKip Macy 	}
1333ff662b5cSJustin T. Gibbs 	err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
1334e4808c4bSKip Macy 	     "feature-barrier", "%lu", &feature_barrier,
133589e0f4d2SKip Macy 	     NULL);
1336*9985113bSJustin T. Gibbs 	if (err == 0 && feature_barrier != 0)
1337e2c1fe90SJustin T. Gibbs 		sc->xbd_flags |= XBDF_BARRIER;
133889e0f4d2SKip Macy 
1339*9985113bSJustin T. Gibbs 	err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
1340*9985113bSJustin T. Gibbs 	     "feature-flush-cache", "%lu", &feature_flush,
1341*9985113bSJustin T. Gibbs 	     NULL);
1342*9985113bSJustin T. Gibbs 	if (err == 0 && feature_flush != 0)
1343*9985113bSJustin T. Gibbs 		sc->xbd_flags |= XBDF_FLUSH;
1344*9985113bSJustin T. Gibbs 
134533eebb6aSJustin T. Gibbs 	if (sc->xbd_disk == NULL) {
134623dc5621SKip Macy 		device_printf(dev, "%juMB <%s> at %s",
134723dc5621SKip Macy 		    (uintmax_t) sectors / (1048576 / sector_size),
134823dc5621SKip Macy 		    device_get_desc(dev),
134923dc5621SKip Macy 		    xenbus_get_node(dev));
135023dc5621SKip Macy 		bus_print_child_footer(device_get_parent(dev), dev);
135189e0f4d2SKip Macy 
135233eebb6aSJustin T. Gibbs 		xbd_instance_create(sc, sectors, sc->xbd_vdevice, binfo,
135333eebb6aSJustin T. Gibbs 		    sector_size);
135406a630f6SJustin T. Gibbs 	}
135523dc5621SKip Macy 
135623dc5621SKip Macy 	(void)xenbus_set_state(dev, XenbusStateConnected);
135789e0f4d2SKip Macy 
135889e0f4d2SKip Macy 	/* Kick pending requests. */
135933eebb6aSJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
1360e2c1fe90SJustin T. Gibbs 	sc->xbd_state = XBD_STATE_CONNECTED;
136133eebb6aSJustin T. Gibbs 	xbd_startio(sc);
1362e2c1fe90SJustin T. Gibbs 	sc->xbd_flags |= XBDF_READY;
136333eebb6aSJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
136489e0f4d2SKip Macy }
136589e0f4d2SKip Macy 
136689e0f4d2SKip Macy /**
136789e0f4d2SKip Macy  * Handle the change of state of the backend to Closing.  We must delete our
136889e0f4d2SKip Macy  * device-layer structures now, to ensure that writes are flushed through to
1369a4660d59SJustin T. Gibbs  * the backend.  Once this is done, we can switch to Closed in
137089e0f4d2SKip Macy  * acknowledgement.
137189e0f4d2SKip Macy  */
137223dc5621SKip Macy static void
137333eebb6aSJustin T. Gibbs xbd_closing(device_t dev)
137489e0f4d2SKip Macy {
137533eebb6aSJustin T. Gibbs 	struct xbd_softc *sc = device_get_softc(dev);
137689e0f4d2SKip Macy 
1377ff662b5cSJustin T. Gibbs 	xenbus_set_state(dev, XenbusStateClosing);
1378ff662b5cSJustin T. Gibbs 
137933eebb6aSJustin T. Gibbs 	DPRINTK("xbd_closing: %s removed\n", xenbus_get_node(dev));
138089e0f4d2SKip Macy 
138133eebb6aSJustin T. Gibbs 	if (sc->xbd_disk != NULL) {
138233eebb6aSJustin T. Gibbs 		disk_destroy(sc->xbd_disk);
138333eebb6aSJustin T. Gibbs 		sc->xbd_disk = NULL;
138489e0f4d2SKip Macy 	}
138589e0f4d2SKip Macy 
138623dc5621SKip Macy 	xenbus_set_state(dev, XenbusStateClosed);
138789e0f4d2SKip Macy }
138889e0f4d2SKip Macy 
1389fac3fd80SJustin T. Gibbs /*---------------------------- NewBus Entrypoints ----------------------------*/
1390fac3fd80SJustin T. Gibbs static int
1391fac3fd80SJustin T. Gibbs xbd_probe(device_t dev)
1392fac3fd80SJustin T. Gibbs {
1393fac3fd80SJustin T. Gibbs 
1394fac3fd80SJustin T. Gibbs 	if (!strcmp(xenbus_get_type(dev), "vbd")) {
1395fac3fd80SJustin T. Gibbs 		device_set_desc(dev, "Virtual Block Device");
1396fac3fd80SJustin T. Gibbs 		device_quiet(dev);
1397fac3fd80SJustin T. Gibbs 		return (0);
1398fac3fd80SJustin T. Gibbs 	}
1399fac3fd80SJustin T. Gibbs 
1400fac3fd80SJustin T. Gibbs 	return (ENXIO);
1401fac3fd80SJustin T. Gibbs }
1402fac3fd80SJustin T. Gibbs 
1403fac3fd80SJustin T. Gibbs /*
1404fac3fd80SJustin T. Gibbs  * Setup supplies the backend dir, virtual device.  We place an event
1405fac3fd80SJustin T. Gibbs  * channel and shared frame entries.  We watch backend to wait if it's
1406fac3fd80SJustin T. Gibbs  * ok.
1407fac3fd80SJustin T. Gibbs  */
1408fac3fd80SJustin T. Gibbs static int
1409fac3fd80SJustin T. Gibbs xbd_attach(device_t dev)
1410fac3fd80SJustin T. Gibbs {
1411fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc;
1412fac3fd80SJustin T. Gibbs 	const char *name;
1413fac3fd80SJustin T. Gibbs 	uint32_t vdevice;
1414fac3fd80SJustin T. Gibbs 	int error;
1415fac3fd80SJustin T. Gibbs 	int i;
1416fac3fd80SJustin T. Gibbs 	int unit;
1417fac3fd80SJustin T. Gibbs 
1418fac3fd80SJustin T. Gibbs 	/* FIXME: Use dynamic device id if this is not set. */
1419fac3fd80SJustin T. Gibbs 	error = xs_scanf(XST_NIL, xenbus_get_node(dev),
1420fac3fd80SJustin T. Gibbs 	    "virtual-device", NULL, "%" PRIu32, &vdevice);
1421fac3fd80SJustin T. Gibbs 	if (error) {
1422fac3fd80SJustin T. Gibbs 		xenbus_dev_fatal(dev, error, "reading virtual-device");
1423fac3fd80SJustin T. Gibbs 		device_printf(dev, "Couldn't determine virtual device.\n");
1424fac3fd80SJustin T. Gibbs 		return (error);
1425fac3fd80SJustin T. Gibbs 	}
1426fac3fd80SJustin T. Gibbs 
1427fac3fd80SJustin T. Gibbs 	xbd_vdevice_to_unit(vdevice, &unit, &name);
1428fac3fd80SJustin T. Gibbs 	if (!strcmp(name, "xbd"))
1429fac3fd80SJustin T. Gibbs 		device_set_unit(dev, unit);
1430fac3fd80SJustin T. Gibbs 
1431fac3fd80SJustin T. Gibbs 	sc = device_get_softc(dev);
1432fac3fd80SJustin T. Gibbs 	mtx_init(&sc->xbd_io_lock, "blkfront i/o lock", NULL, MTX_DEF);
1433e2c1fe90SJustin T. Gibbs 	xbd_initqs(sc);
1434fac3fd80SJustin T. Gibbs 	for (i = 0; i < XBD_MAX_RING_PAGES; i++)
1435d9fab01dSJustin T. Gibbs 		sc->xbd_ring_ref[i] = GRANT_REF_INVALID;
1436fac3fd80SJustin T. Gibbs 
1437fac3fd80SJustin T. Gibbs 	sc->xbd_dev = dev;
1438fac3fd80SJustin T. Gibbs 	sc->xbd_vdevice = vdevice;
1439e2c1fe90SJustin T. Gibbs 	sc->xbd_state = XBD_STATE_DISCONNECTED;
1440fac3fd80SJustin T. Gibbs 
1441fac3fd80SJustin T. Gibbs 	xbd_setup_sysctl(sc);
1442fac3fd80SJustin T. Gibbs 
1443fac3fd80SJustin T. Gibbs 	/* Wait for backend device to publish its protocol capabilities. */
1444fac3fd80SJustin T. Gibbs 	xenbus_set_state(dev, XenbusStateInitialising);
1445fac3fd80SJustin T. Gibbs 
1446fac3fd80SJustin T. Gibbs 	return (0);
1447fac3fd80SJustin T. Gibbs }
144889e0f4d2SKip Macy 
144923dc5621SKip Macy static int
145033eebb6aSJustin T. Gibbs xbd_detach(device_t dev)
145189e0f4d2SKip Macy {
145233eebb6aSJustin T. Gibbs 	struct xbd_softc *sc = device_get_softc(dev);
145389e0f4d2SKip Macy 
1454e2c1fe90SJustin T. Gibbs 	DPRINTK("%s: %s removed\n", __func__, xenbus_get_node(dev));
145589e0f4d2SKip Macy 
145633eebb6aSJustin T. Gibbs 	xbd_free(sc);
145733eebb6aSJustin T. Gibbs 	mtx_destroy(&sc->xbd_io_lock);
145889e0f4d2SKip Macy 
145989e0f4d2SKip Macy 	return 0;
146089e0f4d2SKip Macy }
146189e0f4d2SKip Macy 
146289e0f4d2SKip Macy static int
1463fac3fd80SJustin T. Gibbs xbd_suspend(device_t dev)
146489e0f4d2SKip Macy {
1465fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = device_get_softc(dev);
1466fac3fd80SJustin T. Gibbs 	int retval;
1467fac3fd80SJustin T. Gibbs 	int saved_state;
146889e0f4d2SKip Macy 
146989e0f4d2SKip Macy 	/* Prevent new requests being issued until we fix things up. */
147033eebb6aSJustin T. Gibbs 	mtx_lock(&sc->xbd_io_lock);
1471e2c1fe90SJustin T. Gibbs 	saved_state = sc->xbd_state;
1472e2c1fe90SJustin T. Gibbs 	sc->xbd_state = XBD_STATE_SUSPENDED;
1473fac3fd80SJustin T. Gibbs 
1474fac3fd80SJustin T. Gibbs 	/* Wait for outstanding I/O to drain. */
1475fac3fd80SJustin T. Gibbs 	retval = 0;
1476*9985113bSJustin T. Gibbs 	while (xbd_queue_length(sc, XBD_Q_BUSY) != 0) {
1477e2c1fe90SJustin T. Gibbs 		if (msleep(&sc->xbd_cm_q[XBD_Q_BUSY], &sc->xbd_io_lock,
1478fac3fd80SJustin T. Gibbs 		    PRIBIO, "blkf_susp", 30 * hz) == EWOULDBLOCK) {
1479fac3fd80SJustin T. Gibbs 			retval = EBUSY;
1480fac3fd80SJustin T. Gibbs 			break;
1481fac3fd80SJustin T. Gibbs 		}
1482fac3fd80SJustin T. Gibbs 	}
148333eebb6aSJustin T. Gibbs 	mtx_unlock(&sc->xbd_io_lock);
148489e0f4d2SKip Macy 
1485fac3fd80SJustin T. Gibbs 	if (retval != 0)
1486e2c1fe90SJustin T. Gibbs 		sc->xbd_state = saved_state;
148733eebb6aSJustin T. Gibbs 
1488fac3fd80SJustin T. Gibbs 	return (retval);
148989e0f4d2SKip Macy }
149089e0f4d2SKip Macy 
1491ff662b5cSJustin T. Gibbs static int
1492fac3fd80SJustin T. Gibbs xbd_resume(device_t dev)
149389e0f4d2SKip Macy {
1494fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = device_get_softc(dev);
1495fac3fd80SJustin T. Gibbs 
1496fac3fd80SJustin T. Gibbs 	DPRINTK("xbd_resume: %s\n", xenbus_get_node(dev));
1497fac3fd80SJustin T. Gibbs 
1498fac3fd80SJustin T. Gibbs 	xbd_free(sc);
1499fac3fd80SJustin T. Gibbs 	xbd_initialize(sc);
1500fac3fd80SJustin T. Gibbs 	return (0);
150189e0f4d2SKip Macy }
150289e0f4d2SKip Macy 
1503fac3fd80SJustin T. Gibbs /**
1504fac3fd80SJustin T. Gibbs  * Callback received when the backend's state changes.
1505fac3fd80SJustin T. Gibbs  */
1506fac3fd80SJustin T. Gibbs static void
1507fac3fd80SJustin T. Gibbs xbd_backend_changed(device_t dev, XenbusState backend_state)
1508fac3fd80SJustin T. Gibbs {
1509fac3fd80SJustin T. Gibbs 	struct xbd_softc *sc = device_get_softc(dev);
1510fac3fd80SJustin T. Gibbs 
1511fac3fd80SJustin T. Gibbs 	DPRINTK("backend_state=%d\n", backend_state);
1512fac3fd80SJustin T. Gibbs 
1513fac3fd80SJustin T. Gibbs 	switch (backend_state) {
1514fac3fd80SJustin T. Gibbs 	case XenbusStateUnknown:
1515fac3fd80SJustin T. Gibbs 	case XenbusStateInitialising:
1516fac3fd80SJustin T. Gibbs 	case XenbusStateReconfigured:
1517fac3fd80SJustin T. Gibbs 	case XenbusStateReconfiguring:
1518fac3fd80SJustin T. Gibbs 	case XenbusStateClosed:
1519fac3fd80SJustin T. Gibbs 		break;
1520fac3fd80SJustin T. Gibbs 
1521fac3fd80SJustin T. Gibbs 	case XenbusStateInitWait:
1522fac3fd80SJustin T. Gibbs 	case XenbusStateInitialised:
1523fac3fd80SJustin T. Gibbs 		xbd_initialize(sc);
1524fac3fd80SJustin T. Gibbs 		break;
1525fac3fd80SJustin T. Gibbs 
1526fac3fd80SJustin T. Gibbs 	case XenbusStateConnected:
1527fac3fd80SJustin T. Gibbs 		xbd_initialize(sc);
1528fac3fd80SJustin T. Gibbs 		xbd_connect(sc);
1529fac3fd80SJustin T. Gibbs 		break;
1530fac3fd80SJustin T. Gibbs 
1531fac3fd80SJustin T. Gibbs 	case XenbusStateClosing:
1532fac3fd80SJustin T. Gibbs 		if (sc->xbd_users > 0)
1533fac3fd80SJustin T. Gibbs 			xenbus_dev_error(dev, -EBUSY,
1534fac3fd80SJustin T. Gibbs 			    "Device in use; refusing to close");
1535fac3fd80SJustin T. Gibbs 		else
1536fac3fd80SJustin T. Gibbs 			xbd_closing(dev);
1537fac3fd80SJustin T. Gibbs 		break;
1538fac3fd80SJustin T. Gibbs 	}
1539fac3fd80SJustin T. Gibbs }
1540fac3fd80SJustin T. Gibbs 
1541fac3fd80SJustin T. Gibbs /*---------------------------- NewBus Registration ---------------------------*/
154233eebb6aSJustin T. Gibbs static device_method_t xbd_methods[] = {
154323dc5621SKip Macy 	/* Device interface */
154433eebb6aSJustin T. Gibbs 	DEVMETHOD(device_probe,         xbd_probe),
154533eebb6aSJustin T. Gibbs 	DEVMETHOD(device_attach,        xbd_attach),
154633eebb6aSJustin T. Gibbs 	DEVMETHOD(device_detach,        xbd_detach),
154723dc5621SKip Macy 	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
154833eebb6aSJustin T. Gibbs 	DEVMETHOD(device_suspend,       xbd_suspend),
154933eebb6aSJustin T. Gibbs 	DEVMETHOD(device_resume,        xbd_resume),
155089e0f4d2SKip Macy 
155123dc5621SKip Macy 	/* Xenbus interface */
155233eebb6aSJustin T. Gibbs 	DEVMETHOD(xenbus_otherend_changed, xbd_backend_changed),
155389e0f4d2SKip Macy 
155423dc5621SKip Macy 	{ 0, 0 }
155589e0f4d2SKip Macy };
155689e0f4d2SKip Macy 
155733eebb6aSJustin T. Gibbs static driver_t xbd_driver = {
155823dc5621SKip Macy 	"xbd",
155933eebb6aSJustin T. Gibbs 	xbd_methods,
156033eebb6aSJustin T. Gibbs 	sizeof(struct xbd_softc),
156189e0f4d2SKip Macy };
156233eebb6aSJustin T. Gibbs devclass_t xbd_devclass;
156389e0f4d2SKip Macy 
156433eebb6aSJustin T. Gibbs DRIVER_MODULE(xbd, xenbusb_front, xbd_driver, xbd_devclass, 0, 0);
1565