xref: /freebsd/sys/dev/xen/blkfront/block.h (revision 9985113b61b23f700470e316fe1073bcdd5a964b)
189e0f4d2SKip Macy /*
29999d2cbSKip Macy  * XenBSD block device driver
39999d2cbSKip Macy  *
433eebb6aSJustin T. Gibbs  * Copyright (c) 2010-2013 Spectra Logic Corporation
5e4808c4bSKip Macy  * Copyright (c) 2009 Scott Long, Yahoo!
69999d2cbSKip 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.
2289e0f4d2SKip Macy  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2389e0f4d2SKip Macy  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2489e0f4d2SKip Macy  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2589e0f4d2SKip Macy  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2689e0f4d2SKip Macy  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27e4808c4bSKip Macy  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28e4808c4bSKip Macy  * DEALINGS IN THE SOFTWARE.
2989e0f4d2SKip Macy  *
3089e0f4d2SKip Macy  * $FreeBSD$
3189e0f4d2SKip Macy  */
3289e0f4d2SKip Macy 
3333eebb6aSJustin T. Gibbs #ifndef __XEN_BLKFRONT_BLOCK_H__
3433eebb6aSJustin T. Gibbs #define __XEN_BLKFRONT_BLOCK_H__
35ff662b5cSJustin T. Gibbs #include <xen/blkif.h>
36ff662b5cSJustin T. Gibbs 
37ff662b5cSJustin T. Gibbs /**
38443cc4d4SJustin T. Gibbs  * Given a number of blkif segments, compute the maximum I/O size supported.
39443cc4d4SJustin T. Gibbs  *
40443cc4d4SJustin T. Gibbs  * \note This calculation assumes that all but the first and last segments
41443cc4d4SJustin T. Gibbs  *       of the I/O are fully utilized.
42443cc4d4SJustin T. Gibbs  *
43443cc4d4SJustin T. Gibbs  * \note We reserve a segement from the maximum supported by the transport to
44443cc4d4SJustin T. Gibbs  *       guarantee we can handle an unaligned transfer without the need to
45443cc4d4SJustin T. Gibbs  *       use a bounce buffer.
46443cc4d4SJustin T. Gibbs  */
4733eebb6aSJustin T. Gibbs #define	XBD_SEGS_TO_SIZE(segs)						\
48443cc4d4SJustin T. Gibbs 	(((segs) - 1) * PAGE_SIZE)
49443cc4d4SJustin T. Gibbs 
50443cc4d4SJustin T. Gibbs /**
51443cc4d4SJustin T. Gibbs  * Compute the maximum number of blkif segments requried to represent
52443cc4d4SJustin T. Gibbs  * an I/O of the given size.
53443cc4d4SJustin T. Gibbs  *
54443cc4d4SJustin T. Gibbs  * \note This calculation assumes that all but the first and last segments
55443cc4d4SJustin T. Gibbs  *       of the I/O are fully utilized.
56443cc4d4SJustin T. Gibbs  *
57443cc4d4SJustin T. Gibbs  * \note We reserve a segement to guarantee we can handle an unaligned
58443cc4d4SJustin T. Gibbs  *       transfer without the need to use a bounce buffer.
59443cc4d4SJustin T. Gibbs  */
6033eebb6aSJustin T. Gibbs #define	XBD_SIZE_TO_SEGS(size)						\
61443cc4d4SJustin T. Gibbs 	((size / PAGE_SIZE) + 1)
62443cc4d4SJustin T. Gibbs 
63443cc4d4SJustin T. Gibbs /**
64ff662b5cSJustin T. Gibbs  * The maximum number of outstanding requests blocks (request headers plus
65ff662b5cSJustin T. Gibbs  * additional segment blocks) we will allow in a negotiated block-front/back
66ff662b5cSJustin T. Gibbs  * communication channel.
67ff662b5cSJustin T. Gibbs  */
6833eebb6aSJustin T. Gibbs #define XBD_MAX_REQUESTS		256
69ff662b5cSJustin T. Gibbs 
70ff662b5cSJustin T. Gibbs /**
71ff662b5cSJustin T. Gibbs  * The maximum mapped region size per request we will allow in a negotiated
72ff662b5cSJustin T. Gibbs  * block-front/back communication channel.
73ff662b5cSJustin T. Gibbs  */
7433eebb6aSJustin T. Gibbs #define	XBD_MAX_REQUEST_SIZE						\
7533eebb6aSJustin T. Gibbs 	MIN(MAXPHYS, XBD_SEGS_TO_SIZE(BLKIF_MAX_SEGMENTS_PER_REQUEST))
76ff662b5cSJustin T. Gibbs 
77ff662b5cSJustin T. Gibbs /**
78ff662b5cSJustin T. Gibbs  * The maximum number of segments (within a request header and accompanying
79ff662b5cSJustin T. Gibbs  * segment blocks) per request we will allow in a negotiated block-front/back
80ff662b5cSJustin T. Gibbs  * communication channel.
81ff662b5cSJustin T. Gibbs  */
8233eebb6aSJustin T. Gibbs #define	XBD_MAX_SEGMENTS_PER_REQUEST					\
83ff662b5cSJustin T. Gibbs 	(MIN(BLKIF_MAX_SEGMENTS_PER_REQUEST,				\
8433eebb6aSJustin T. Gibbs 	     XBD_SIZE_TO_SEGS(XBD_MAX_REQUEST_SIZE)))
85ff662b5cSJustin T. Gibbs 
86ff662b5cSJustin T. Gibbs /**
87ff662b5cSJustin T. Gibbs  * The maximum number of shared memory ring pages we will allow in a
88ff662b5cSJustin T. Gibbs  * negotiated block-front/back communication channel.  Allow enough
8933eebb6aSJustin T. Gibbs  * ring space for all requests to be  XBD_MAX_REQUEST_SIZE'd.
90ff662b5cSJustin T. Gibbs  */
9133eebb6aSJustin T. Gibbs #define XBD_MAX_RING_PAGES						    \
9233eebb6aSJustin T. Gibbs 	BLKIF_RING_PAGES(BLKIF_SEGS_TO_BLOCKS(XBD_MAX_SEGMENTS_PER_REQUEST) \
9333eebb6aSJustin T. Gibbs 		       * XBD_MAX_REQUESTS)
9489e0f4d2SKip Macy 
95e2c1fe90SJustin T. Gibbs typedef enum {
96e2c1fe90SJustin T. Gibbs 	XBDCF_Q_MASK		= 0xFF,
97*9985113bSJustin T. Gibbs 	/* This command has contributed to xbd_qfrozen_cnt. */
98e2c1fe90SJustin T. Gibbs 	XBDCF_FROZEN		= 1<<8,
99*9985113bSJustin T. Gibbs 	/* Freeze the command queue on dispatch (i.e. single step command). */
100*9985113bSJustin T. Gibbs 	XBDCF_Q_FREEZE		= 1<<9,
101*9985113bSJustin T. Gibbs 	/* Bus DMA returned EINPROGRESS for this command. */
102127a9483SJustin T. Gibbs 	XBDCF_ASYNC_MAPPING	= 1<<10,
103e2c1fe90SJustin T. Gibbs 	XBDCF_INITIALIZER	= XBDCF_Q_MASK
104e2c1fe90SJustin T. Gibbs } xbdc_flag_t;
105e2c1fe90SJustin T. Gibbs 
10633eebb6aSJustin T. Gibbs struct xbd_command;
10733eebb6aSJustin T. Gibbs typedef void xbd_cbcf_t(struct xbd_command *);
10889e0f4d2SKip Macy 
10933eebb6aSJustin T. Gibbs struct xbd_command {
11033eebb6aSJustin T. Gibbs 	TAILQ_ENTRY(xbd_command) cm_link;
11133eebb6aSJustin T. Gibbs 	struct xbd_softc	*cm_sc;
112e2c1fe90SJustin T. Gibbs 	xbdc_flag_t		 cm_flags;
11333eebb6aSJustin T. Gibbs 	bus_dmamap_t		 cm_map;
11433eebb6aSJustin T. Gibbs 	uint64_t		 cm_id;
11533eebb6aSJustin T. Gibbs 	grant_ref_t		*cm_sg_refs;
11633eebb6aSJustin T. Gibbs 	struct bio		*cm_bp;
11733eebb6aSJustin T. Gibbs 	grant_ref_t		 cm_gref_head;
11833eebb6aSJustin T. Gibbs 	void			*cm_data;
11933eebb6aSJustin T. Gibbs 	size_t			 cm_datalen;
12033eebb6aSJustin T. Gibbs 	u_int			 cm_nseg;
12133eebb6aSJustin T. Gibbs 	int			 cm_operation;
12233eebb6aSJustin T. Gibbs 	blkif_sector_t		 cm_sector_number;
12333eebb6aSJustin T. Gibbs 	int			 cm_status;
12433eebb6aSJustin T. Gibbs 	xbd_cbcf_t		*cm_complete;
12589e0f4d2SKip Macy };
12689e0f4d2SKip Macy 
127e2c1fe90SJustin T. Gibbs typedef enum {
128e2c1fe90SJustin T. Gibbs 	XBD_Q_FREE,
129e2c1fe90SJustin T. Gibbs 	XBD_Q_READY,
130e2c1fe90SJustin T. Gibbs 	XBD_Q_BUSY,
131e2c1fe90SJustin T. Gibbs 	XBD_Q_COMPLETE,
132e2c1fe90SJustin T. Gibbs 	XBD_Q_BIO,
133e2c1fe90SJustin T. Gibbs 	XBD_Q_COUNT,
134e2c1fe90SJustin T. Gibbs 	XBD_Q_NONE = XBDCF_Q_MASK
135e2c1fe90SJustin T. Gibbs } xbd_q_index_t;
13689e0f4d2SKip Macy 
137e2c1fe90SJustin T. Gibbs typedef struct xbd_cm_q {
138e2c1fe90SJustin T. Gibbs 	TAILQ_HEAD(, xbd_command) q_tailq;
139e4808c4bSKip Macy 	uint32_t		  q_length;
140e4808c4bSKip Macy 	uint32_t		  q_max;
141e2c1fe90SJustin T. Gibbs } xbd_cm_q_t;
142e4808c4bSKip Macy 
143e2c1fe90SJustin T. Gibbs typedef enum {
144e2c1fe90SJustin T. Gibbs 	XBD_STATE_DISCONNECTED,
145e2c1fe90SJustin T. Gibbs 	XBD_STATE_CONNECTED,
146e2c1fe90SJustin T. Gibbs 	XBD_STATE_SUSPENDED
147e2c1fe90SJustin T. Gibbs } xbd_state_t;
148e2c1fe90SJustin T. Gibbs 
149e2c1fe90SJustin T. Gibbs typedef enum {
150127a9483SJustin T. Gibbs 	XBDF_NONE	  = 0,
151e2c1fe90SJustin T. Gibbs 	XBDF_OPEN	  = 1 << 0, /* drive is open (can't shut down) */
152e2c1fe90SJustin T. Gibbs 	XBDF_BARRIER	  = 1 << 1, /* backend supports barriers */
153*9985113bSJustin T. Gibbs 	XBDF_FLUSH	  = 1 << 2, /* backend supports flush */
154*9985113bSJustin T. Gibbs 	XBDF_READY	  = 1 << 3, /* Is ready */
155*9985113bSJustin T. Gibbs 	XBDF_CM_SHORTAGE  = 1 << 4, /* Free cm resource shortage active. */
156*9985113bSJustin T. Gibbs 	XBDF_GNT_SHORTAGE = 1 << 5, /* Grant ref resource shortage active */
157*9985113bSJustin T. Gibbs 	XBDF_WAIT_IDLE	  = 1 << 6  /*
158*9985113bSJustin T. Gibbs 				     * No new work until oustanding work
159*9985113bSJustin T. Gibbs 				     * completes.
160*9985113bSJustin T. Gibbs 				     */
161e2c1fe90SJustin T. Gibbs } xbd_flag_t;
162e4808c4bSKip Macy 
163e4808c4bSKip Macy /*
164e4808c4bSKip Macy  * We have one of these per vbd, whether ide, scsi or 'other'.
165e4808c4bSKip Macy  */
16633eebb6aSJustin T. Gibbs struct xbd_softc {
16733eebb6aSJustin T. Gibbs 	device_t			 xbd_dev;
16833eebb6aSJustin T. Gibbs 	struct disk			*xbd_disk;	/* disk params */
16933eebb6aSJustin T. Gibbs 	struct bio_queue_head 		 xbd_bioq;	/* sort queue */
17033eebb6aSJustin T. Gibbs 	int				 xbd_unit;
171e2c1fe90SJustin T. Gibbs 	xbd_flag_t			 xbd_flags;
172127a9483SJustin T. Gibbs 	int				 xbd_qfrozen_cnt;
17333eebb6aSJustin T. Gibbs 	int				 xbd_vdevice;
174e2c1fe90SJustin T. Gibbs 	xbd_state_t			 xbd_state;
17533eebb6aSJustin T. Gibbs 	u_int				 xbd_ring_pages;
17633eebb6aSJustin T. Gibbs 	uint32_t			 xbd_max_requests;
17733eebb6aSJustin T. Gibbs 	uint32_t			 xbd_max_request_segments;
17833eebb6aSJustin T. Gibbs 	uint32_t			 xbd_max_request_blocks;
17933eebb6aSJustin T. Gibbs 	uint32_t			 xbd_max_request_size;
18033eebb6aSJustin T. Gibbs 	grant_ref_t			 xbd_ring_ref[XBD_MAX_RING_PAGES];
18133eebb6aSJustin T. Gibbs 	blkif_front_ring_t		 xbd_ring;
18233eebb6aSJustin T. Gibbs 	unsigned int			 xbd_irq;
18333eebb6aSJustin T. Gibbs 	struct gnttab_free_callback	 xbd_callback;
184e2c1fe90SJustin T. Gibbs 	xbd_cm_q_t			 xbd_cm_q[XBD_Q_COUNT];
18533eebb6aSJustin T. Gibbs 	bus_dma_tag_t			 xbd_io_dmat;
186e4808c4bSKip Macy 
18789e0f4d2SKip Macy 	/**
18889e0f4d2SKip Macy 	 * The number of people holding this device open.  We won't allow a
18989e0f4d2SKip Macy 	 * hot-unplug unless this is 0.
19089e0f4d2SKip Macy 	 */
19133eebb6aSJustin T. Gibbs 	int				 xbd_users;
19233eebb6aSJustin T. Gibbs 	struct mtx			 xbd_io_lock;
193ff662b5cSJustin T. Gibbs 
19433eebb6aSJustin T. Gibbs 	struct xbd_command		*xbd_shadow;
19589e0f4d2SKip Macy };
196e4808c4bSKip Macy 
19733eebb6aSJustin T. Gibbs int xbd_instance_create(struct xbd_softc *, blkif_sector_t sectors, int device,
198ff662b5cSJustin T. Gibbs 			uint16_t vdisk_info, unsigned long sector_size);
199e4808c4bSKip Macy 
200e2c1fe90SJustin T. Gibbs static inline void
201e2c1fe90SJustin T. Gibbs xbd_added_qentry(struct xbd_softc *sc, xbd_q_index_t index)
202e2c1fe90SJustin T. Gibbs {
203e2c1fe90SJustin T. Gibbs 	struct xbd_cm_q *cmq;
204e4808c4bSKip Macy 
205e2c1fe90SJustin T. Gibbs 	cmq = &sc->xbd_cm_q[index];
206e2c1fe90SJustin T. Gibbs 	cmq->q_length++;
207e2c1fe90SJustin T. Gibbs 	if (cmq->q_length > cmq->q_max)
208e2c1fe90SJustin T. Gibbs 		cmq->q_max = cmq->q_length;
209e2c1fe90SJustin T. Gibbs }
210e4808c4bSKip Macy 
211e2c1fe90SJustin T. Gibbs static inline void
212e2c1fe90SJustin T. Gibbs xbd_removed_qentry(struct xbd_softc *sc, xbd_q_index_t index)
213e2c1fe90SJustin T. Gibbs {
214e2c1fe90SJustin T. Gibbs 	sc->xbd_cm_q[index].q_length--;
215e2c1fe90SJustin T. Gibbs }
216e4808c4bSKip Macy 
217*9985113bSJustin T. Gibbs static inline uint32_t
218*9985113bSJustin T. Gibbs xbd_queue_length(struct xbd_softc *sc, xbd_q_index_t index)
219*9985113bSJustin T. Gibbs {
220*9985113bSJustin T. Gibbs 	return (sc->xbd_cm_q[index].q_length);
221*9985113bSJustin T. Gibbs }
222*9985113bSJustin T. Gibbs 
223e2c1fe90SJustin T. Gibbs static inline void
224e2c1fe90SJustin T. Gibbs xbd_initq_cm(struct xbd_softc *sc, xbd_q_index_t index)
225e2c1fe90SJustin T. Gibbs {
226e2c1fe90SJustin T. Gibbs 	struct xbd_cm_q *cmq;
227e4808c4bSKip Macy 
228e2c1fe90SJustin T. Gibbs 	cmq = &sc->xbd_cm_q[index];
229e2c1fe90SJustin T. Gibbs 	TAILQ_INIT(&cmq->q_tailq);
230e2c1fe90SJustin T. Gibbs 	cmq->q_length = 0;
231e2c1fe90SJustin T. Gibbs 	cmq->q_max = 0;
232e2c1fe90SJustin T. Gibbs }
233e2c1fe90SJustin T. Gibbs 
234e2c1fe90SJustin T. Gibbs static inline void
235e2c1fe90SJustin T. Gibbs xbd_enqueue_cm(struct xbd_command *cm, xbd_q_index_t index)
236e2c1fe90SJustin T. Gibbs {
237e2c1fe90SJustin T. Gibbs 	KASSERT(index != XBD_Q_BIO,
238e2c1fe90SJustin T. Gibbs 	    ("%s: Commands cannot access the bio queue.", __func__));
239e2c1fe90SJustin T. Gibbs 	if ((cm->cm_flags & XBDCF_Q_MASK) != XBD_Q_NONE)
240e2c1fe90SJustin T. Gibbs 		panic("%s: command %p is already on queue %d.",
241e2c1fe90SJustin T. Gibbs 		    __func__, cm, cm->cm_flags & XBDCF_Q_MASK);
242e2c1fe90SJustin T. Gibbs 	TAILQ_INSERT_TAIL(&cm->cm_sc->xbd_cm_q[index].q_tailq, cm, cm_link);
243e2c1fe90SJustin T. Gibbs 	cm->cm_flags &= ~XBDCF_Q_MASK;
244e2c1fe90SJustin T. Gibbs 	cm->cm_flags |= index;
245e2c1fe90SJustin T. Gibbs 	xbd_added_qentry(cm->cm_sc, index);
246e2c1fe90SJustin T. Gibbs }
247e2c1fe90SJustin T. Gibbs 
248e2c1fe90SJustin T. Gibbs static inline void
249e2c1fe90SJustin T. Gibbs xbd_requeue_cm(struct xbd_command *cm, xbd_q_index_t index)
250e2c1fe90SJustin T. Gibbs {
251e2c1fe90SJustin T. Gibbs 	KASSERT(index != XBD_Q_BIO,
252e2c1fe90SJustin T. Gibbs 	    ("%s: Commands cannot access the bio queue.", __func__));
253e2c1fe90SJustin T. Gibbs 	if ((cm->cm_flags & XBDCF_Q_MASK) != XBD_Q_NONE)
254e2c1fe90SJustin T. Gibbs 		panic("%s: command %p is already on queue %d.",
255e2c1fe90SJustin T. Gibbs 		    __func__, cm, cm->cm_flags & XBDCF_Q_MASK);
256e2c1fe90SJustin T. Gibbs 	TAILQ_INSERT_HEAD(&cm->cm_sc->xbd_cm_q[index].q_tailq, cm, cm_link);
257e2c1fe90SJustin T. Gibbs 	cm->cm_flags &= ~XBDCF_Q_MASK;
258e2c1fe90SJustin T. Gibbs 	cm->cm_flags |= index;
259e2c1fe90SJustin T. Gibbs 	xbd_added_qentry(cm->cm_sc, index);
260e2c1fe90SJustin T. Gibbs }
261e2c1fe90SJustin T. Gibbs 
262e2c1fe90SJustin T. Gibbs static inline struct xbd_command *
263e2c1fe90SJustin T. Gibbs xbd_dequeue_cm(struct xbd_softc *sc, xbd_q_index_t index)
264e2c1fe90SJustin T. Gibbs {
265e2c1fe90SJustin T. Gibbs 	struct xbd_command *cm;
266e2c1fe90SJustin T. Gibbs 
267e2c1fe90SJustin T. Gibbs 	KASSERT(index != XBD_Q_BIO,
268e2c1fe90SJustin T. Gibbs 	    ("%s: Commands cannot access the bio queue.", __func__));
269e2c1fe90SJustin T. Gibbs 
270e2c1fe90SJustin T. Gibbs 	if ((cm = TAILQ_FIRST(&sc->xbd_cm_q[index].q_tailq)) != NULL) {
271e2c1fe90SJustin T. Gibbs 		if ((cm->cm_flags & XBDCF_Q_MASK) != index) {
272e2c1fe90SJustin T. Gibbs 			panic("%s: command %p is on queue %d, "
273e2c1fe90SJustin T. Gibbs 			    "not specified queue %d",
274e2c1fe90SJustin T. Gibbs 			    __func__, cm,
275e2c1fe90SJustin T. Gibbs 			    cm->cm_flags & XBDCF_Q_MASK,
276e2c1fe90SJustin T. Gibbs 			    index);
277e2c1fe90SJustin T. Gibbs 		}
278e2c1fe90SJustin T. Gibbs 		TAILQ_REMOVE(&sc->xbd_cm_q[index].q_tailq, cm, cm_link);
279e2c1fe90SJustin T. Gibbs 		cm->cm_flags &= ~XBDCF_Q_MASK;
280e2c1fe90SJustin T. Gibbs 		cm->cm_flags |= XBD_Q_NONE;
281e2c1fe90SJustin T. Gibbs 		xbd_removed_qentry(cm->cm_sc, index);
282e2c1fe90SJustin T. Gibbs 	}
283e2c1fe90SJustin T. Gibbs 	return (cm);
284e2c1fe90SJustin T. Gibbs }
285e2c1fe90SJustin T. Gibbs 
286e2c1fe90SJustin T. Gibbs static inline void
287e2c1fe90SJustin T. Gibbs xbd_remove_cm(struct xbd_command *cm, xbd_q_index_t expected_index)
288e2c1fe90SJustin T. Gibbs {
289e2c1fe90SJustin T. Gibbs 	xbd_q_index_t index;
290e2c1fe90SJustin T. Gibbs 
291e2c1fe90SJustin T. Gibbs 	index = cm->cm_flags & XBDCF_Q_MASK;
292e2c1fe90SJustin T. Gibbs 
293e2c1fe90SJustin T. Gibbs 	KASSERT(index != XBD_Q_BIO,
294e2c1fe90SJustin T. Gibbs 	    ("%s: Commands cannot access the bio queue.", __func__));
295e2c1fe90SJustin T. Gibbs 
296e2c1fe90SJustin T. Gibbs 	if (index != expected_index) {
297e2c1fe90SJustin T. Gibbs 		panic("%s: command %p is on queue %d, not specified queue %d",
298e2c1fe90SJustin T. Gibbs 		    __func__, cm, index, expected_index);
299e2c1fe90SJustin T. Gibbs 	}
300e2c1fe90SJustin T. Gibbs 	TAILQ_REMOVE(&cm->cm_sc->xbd_cm_q[index].q_tailq, cm, cm_link);
301e2c1fe90SJustin T. Gibbs 	cm->cm_flags &= ~XBDCF_Q_MASK;
302e2c1fe90SJustin T. Gibbs 	cm->cm_flags |= XBD_Q_NONE;
303e2c1fe90SJustin T. Gibbs 	xbd_removed_qentry(cm->cm_sc, index);
304e2c1fe90SJustin T. Gibbs }
305e4808c4bSKip Macy 
306*9985113bSJustin T. Gibbs static inline void
30733eebb6aSJustin T. Gibbs xbd_initq_bio(struct xbd_softc *sc)
308e4808c4bSKip Macy {
30933eebb6aSJustin T. Gibbs 	bioq_init(&sc->xbd_bioq);
310e4808c4bSKip Macy }
311e4808c4bSKip Macy 
312*9985113bSJustin T. Gibbs static inline void
31333eebb6aSJustin T. Gibbs xbd_enqueue_bio(struct xbd_softc *sc, struct bio *bp)
314e4808c4bSKip Macy {
31533eebb6aSJustin T. Gibbs 	bioq_insert_tail(&sc->xbd_bioq, bp);
316e2c1fe90SJustin T. Gibbs 	xbd_added_qentry(sc, XBD_Q_BIO);
317e4808c4bSKip Macy }
318e4808c4bSKip Macy 
319*9985113bSJustin T. Gibbs static inline void
32033eebb6aSJustin T. Gibbs xbd_requeue_bio(struct xbd_softc *sc, struct bio *bp)
321e4808c4bSKip Macy {
32233eebb6aSJustin T. Gibbs 	bioq_insert_head(&sc->xbd_bioq, bp);
323e2c1fe90SJustin T. Gibbs 	xbd_added_qentry(sc, XBD_Q_BIO);
324e4808c4bSKip Macy }
325e4808c4bSKip Macy 
326*9985113bSJustin T. Gibbs static inline struct bio *
32733eebb6aSJustin T. Gibbs xbd_dequeue_bio(struct xbd_softc *sc)
328e4808c4bSKip Macy {
329e4808c4bSKip Macy 	struct bio *bp;
330e4808c4bSKip Macy 
33133eebb6aSJustin T. Gibbs 	if ((bp = bioq_first(&sc->xbd_bioq)) != NULL) {
33233eebb6aSJustin T. Gibbs 		bioq_remove(&sc->xbd_bioq, bp);
333e2c1fe90SJustin T. Gibbs 		xbd_removed_qentry(sc, XBD_Q_BIO);
334e4808c4bSKip Macy 	}
335e4808c4bSKip Macy 	return (bp);
336e4808c4bSKip Macy }
33789e0f4d2SKip Macy 
338e2c1fe90SJustin T. Gibbs static inline void
339e2c1fe90SJustin T. Gibbs xbd_initqs(struct xbd_softc *sc)
340e2c1fe90SJustin T. Gibbs {
341e2c1fe90SJustin T. Gibbs 	u_int index;
342e2c1fe90SJustin T. Gibbs 
343e2c1fe90SJustin T. Gibbs 	for (index = 0; index < XBD_Q_COUNT; index++)
344e2c1fe90SJustin T. Gibbs 		xbd_initq_cm(sc, index);
345e2c1fe90SJustin T. Gibbs 
346e2c1fe90SJustin T. Gibbs 	xbd_initq_bio(sc);
347e2c1fe90SJustin T. Gibbs }
348e2c1fe90SJustin T. Gibbs 
34933eebb6aSJustin T. Gibbs #endif /* __XEN_BLKFRONT_BLOCK_H__ */
350