xref: /freebsd/sys/dev/xen/blkfront/blkfront.c (revision fd962ac6997aba7e64007113ed1354e0a88b6e38)
1 /*
2  * XenBSD block device driver
3  *
4  * Copyright (c) 2010-2013 Spectra Logic Corporation
5  * Copyright (c) 2009 Scott Long, Yahoo!
6  * Copyright (c) 2009 Frank Suchomel, Citrix
7  * Copyright (c) 2009 Doug F. Rabson, Citrix
8  * Copyright (c) 2005 Kip Macy
9  * Copyright (c) 2003-2004, Keir Fraser & Steve Hand
10  * Modifications by Mark A. Williamson are (c) Intel Research Cambridge
11  *
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a copy
14  * of this software and associated documentation files (the "Software"), to
15  * deal in the Software without restriction, including without limitation the
16  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
17  * sell copies of the Software, and to permit persons to whom the Software is
18  * furnished to do so, subject to the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included in
21  * all copies or substantial portions of the Software.
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28  * DEALINGS IN THE SOFTWARE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>
38 #include <vm/vm.h>
39 #include <vm/pmap.h>
40 
41 #include <sys/bio.h>
42 #include <sys/bus.h>
43 #include <sys/conf.h>
44 #include <sys/module.h>
45 #include <sys/sysctl.h>
46 
47 #include <machine/bus.h>
48 #include <sys/rman.h>
49 #include <machine/resource.h>
50 #include <machine/intr_machdep.h>
51 #include <machine/vmparam.h>
52 #include <sys/bus_dma.h>
53 
54 #include <machine/_inttypes.h>
55 #include <machine/xen/xen-os.h>
56 #include <machine/xen/xenvar.h>
57 #include <machine/xen/xenfunc.h>
58 
59 #include <xen/hypervisor.h>
60 #include <xen/xen_intr.h>
61 #include <xen/evtchn.h>
62 #include <xen/gnttab.h>
63 #include <xen/interface/grant_table.h>
64 #include <xen/interface/io/protocols.h>
65 #include <xen/xenbus/xenbusvar.h>
66 
67 #include <geom/geom_disk.h>
68 
69 #include <dev/xen/blkfront/block.h>
70 
71 #include "xenbus_if.h"
72 
73 /*--------------------------- Forward Declarations ---------------------------*/
74 static void xbd_closing(device_t);
75 static void xbd_startio(struct xbd_softc *sc);
76 
77 /*---------------------------------- Macros ----------------------------------*/
78 #if 0
79 #define DPRINTK(fmt, args...) printf("[XEN] %s:%d: " fmt ".\n", __func__, __LINE__, ##args)
80 #else
81 #define DPRINTK(fmt, args...)
82 #endif
83 
84 #define XBD_SECTOR_SHFT		9
85 
86 #define XBD_STATE_DISCONNECTED 0
87 #define XBD_STATE_CONNECTED    1
88 #define XBD_STATE_SUSPENDED    2
89 
90 /*---------------------------- Global Static Data ----------------------------*/
91 static MALLOC_DEFINE(M_XENBLOCKFRONT, "xbd", "Xen Block Front driver data");
92 
93 /*---------------------------- Command Processing ----------------------------*/
94 static inline void
95 xbd_flush_requests(struct xbd_softc *sc)
96 {
97 	int notify;
98 
99 	RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&sc->xbd_ring, notify);
100 
101 	if (notify)
102 		notify_remote_via_irq(sc->xbd_irq);
103 }
104 
105 static void
106 xbd_free_command(struct xbd_command *cm)
107 {
108 
109 	KASSERT((cm->cm_flags & XBD_ON_XBDQ_MASK) == 0,
110 	    ("Freeing command that is still on a queue\n"));
111 
112 	cm->cm_flags = 0;
113 	cm->cm_bp = NULL;
114 	cm->cm_complete = NULL;
115 	xbd_enqueue_free(cm);
116 }
117 
118 static void
119 xbd_queue_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
120 {
121 	struct xbd_softc *sc;
122 	struct xbd_command *cm;
123 	blkif_request_t	*ring_req;
124 	struct blkif_request_segment *sg;
125 	struct blkif_request_segment *last_block_sg;
126 	grant_ref_t *sg_ref;
127 	vm_paddr_t buffer_ma;
128 	uint64_t fsect, lsect;
129 	int ref;
130 	int op;
131 	int block_segs;
132 
133 	cm = arg;
134 	sc = cm->cm_sc;
135 
136 	if (error) {
137 		printf("error %d in xbd_queue_cb\n", error);
138 		cm->cm_bp->bio_error = EIO;
139 		biodone(cm->cm_bp);
140 		xbd_free_command(cm);
141 		return;
142 	}
143 
144 	/* Fill out a communications ring structure. */
145 	ring_req = RING_GET_REQUEST(&sc->xbd_ring, sc->xbd_ring.req_prod_pvt);
146 	sc->xbd_ring.req_prod_pvt++;
147 	ring_req->id = cm->cm_id;
148 	ring_req->operation = cm->cm_operation;
149 	ring_req->sector_number = cm->cm_sector_number;
150 	ring_req->handle = (blkif_vdev_t)(uintptr_t)sc->xbd_disk;
151 	ring_req->nr_segments = nsegs;
152 	cm->cm_nseg = nsegs;
153 
154 	block_segs    = MIN(nsegs, BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK);
155 	sg            = ring_req->seg;
156 	last_block_sg = sg + block_segs;
157 	sg_ref        = cm->cm_sg_refs;
158 
159 	while (1) {
160 
161 		while (sg < last_block_sg) {
162 			buffer_ma = segs->ds_addr;
163 			fsect = (buffer_ma & PAGE_MASK) >> XBD_SECTOR_SHFT;
164 			lsect = fsect + (segs->ds_len  >> XBD_SECTOR_SHFT) - 1;
165 
166 			KASSERT(lsect <= 7, ("XEN disk driver data cannot "
167 			    "cross a page boundary"));
168 
169 			/* install a grant reference. */
170 			ref = gnttab_claim_grant_reference(&cm->cm_gref_head);
171 
172 			/*
173 			 * GNTTAB_LIST_END == 0xffffffff, but it is private
174 			 * to gnttab.c.
175 			 */
176 			KASSERT(ref != ~0, ("grant_reference failed"));
177 
178 			gnttab_grant_foreign_access_ref(
179 			    ref,
180 			    xenbus_get_otherend_id(sc->xbd_dev),
181 			    buffer_ma >> PAGE_SHIFT,
182 			    ring_req->operation == BLKIF_OP_WRITE);
183 
184 			*sg_ref = ref;
185 			*sg = (struct blkif_request_segment) {
186 				.gref       = ref,
187 				.first_sect = fsect,
188 				.last_sect  = lsect
189 			};
190 			sg++;
191 			sg_ref++;
192 			segs++;
193 			nsegs--;
194 		}
195 		block_segs = MIN(nsegs, BLKIF_MAX_SEGMENTS_PER_SEGMENT_BLOCK);
196 		if (block_segs == 0)
197 			break;
198 
199 		sg = BLKRING_GET_SEG_BLOCK(&sc->xbd_ring,
200 		    sc->xbd_ring.req_prod_pvt);
201 		sc->xbd_ring.req_prod_pvt++;
202 		last_block_sg = sg + block_segs;
203 	}
204 
205 	if (cm->cm_operation == BLKIF_OP_READ)
206 		op = BUS_DMASYNC_PREREAD;
207 	else if (cm->cm_operation == BLKIF_OP_WRITE)
208 		op = BUS_DMASYNC_PREWRITE;
209 	else
210 		op = 0;
211 	bus_dmamap_sync(sc->xbd_io_dmat, cm->cm_map, op);
212 
213 	gnttab_free_grant_references(cm->cm_gref_head);
214 
215 	xbd_enqueue_busy(cm);
216 
217 	/*
218 	 * This flag means that we're probably executing in the busdma swi
219 	 * instead of in the startio context, so an explicit flush is needed.
220 	 */
221 	if (cm->cm_flags & XBD_CMD_FROZEN)
222 		xbd_flush_requests(sc);
223 
224 	return;
225 }
226 
227 static int
228 xbd_queue_request(struct xbd_softc *sc, struct xbd_command *cm)
229 {
230 	int error;
231 
232 	error = bus_dmamap_load(sc->xbd_io_dmat, cm->cm_map, cm->cm_data,
233 	    cm->cm_datalen, xbd_queue_cb, cm, 0);
234 	if (error == EINPROGRESS) {
235 		printf("EINPROGRESS\n");
236 		sc->xbd_flags |= XBD_FROZEN;
237 		cm->cm_flags |= XBD_CMD_FROZEN;
238 		return (0);
239 	}
240 
241 	return (error);
242 }
243 
244 static void
245 xbd_restart_queue_callback(void *arg)
246 {
247 	struct xbd_softc *sc = arg;
248 
249 	mtx_lock(&sc->xbd_io_lock);
250 
251 	xbd_startio(sc);
252 
253 	mtx_unlock(&sc->xbd_io_lock);
254 }
255 
256 static struct xbd_command *
257 xbd_bio_command(struct xbd_softc *sc)
258 {
259 	struct xbd_command *cm;
260 	struct bio *bp;
261 
262 	if (unlikely(sc->xbd_connected != XBD_STATE_CONNECTED))
263 		return (NULL);
264 
265 	bp = xbd_dequeue_bio(sc);
266 	if (bp == NULL)
267 		return (NULL);
268 
269 	if ((cm = xbd_dequeue_free(sc)) == NULL) {
270 		xbd_requeue_bio(sc, bp);
271 		return (NULL);
272 	}
273 
274 	if (gnttab_alloc_grant_references(sc->xbd_max_request_segments,
275 	    &cm->cm_gref_head) != 0) {
276 		gnttab_request_free_callback(&sc->xbd_callback,
277 		    xbd_restart_queue_callback, sc,
278 		    sc->xbd_max_request_segments);
279 		xbd_requeue_bio(sc, bp);
280 		xbd_enqueue_free(cm);
281 		sc->xbd_flags |= XBD_FROZEN;
282 		return (NULL);
283 	}
284 
285 	cm->cm_bp = bp;
286 	cm->cm_data = bp->bio_data;
287 	cm->cm_datalen = bp->bio_bcount;
288 	cm->cm_operation = (bp->bio_cmd == BIO_READ) ?
289 	    BLKIF_OP_READ : BLKIF_OP_WRITE;
290 	cm->cm_sector_number = (blkif_sector_t)bp->bio_pblkno;
291 
292 	return (cm);
293 }
294 
295 /*
296  * Dequeue buffers and place them in the shared communication ring.
297  * Return when no more requests can be accepted or all buffers have
298  * been queued.
299  *
300  * Signal XEN once the ring has been filled out.
301  */
302 static void
303 xbd_startio(struct xbd_softc *sc)
304 {
305 	struct xbd_command *cm;
306 	int error, queued = 0;
307 
308 	mtx_assert(&sc->xbd_io_lock, MA_OWNED);
309 
310 	if (sc->xbd_connected != XBD_STATE_CONNECTED)
311 		return;
312 
313 	while (RING_FREE_REQUESTS(&sc->xbd_ring) >=
314 	    sc->xbd_max_request_blocks) {
315 		if (sc->xbd_flags & XBD_FROZEN)
316 			break;
317 
318 		cm = xbd_dequeue_ready(sc);
319 
320 		if (cm == NULL)
321 		    cm = xbd_bio_command(sc);
322 
323 		if (cm == NULL)
324 			break;
325 
326 		if ((error = xbd_queue_request(sc, cm)) != 0) {
327 			printf("xbd_queue_request returned %d\n", error);
328 			break;
329 		}
330 		queued++;
331 	}
332 
333 	if (queued != 0)
334 		xbd_flush_requests(sc);
335 }
336 
337 static void
338 xbd_bio_complete(struct xbd_softc *sc, struct xbd_command *cm)
339 {
340 	struct bio *bp;
341 
342 	bp = cm->cm_bp;
343 
344 	if (unlikely(cm->cm_status != BLKIF_RSP_OKAY)) {
345 		disk_err(bp, "disk error" , -1, 0);
346 		printf(" status: %x\n", cm->cm_status);
347 		bp->bio_flags |= BIO_ERROR;
348 	}
349 
350 	if (bp->bio_flags & BIO_ERROR)
351 		bp->bio_error = EIO;
352 	else
353 		bp->bio_resid = 0;
354 
355 	xbd_free_command(cm);
356 	biodone(bp);
357 }
358 
359 static int
360 xbd_completion(struct xbd_command *cm)
361 {
362 	gnttab_end_foreign_access_references(cm->cm_nseg, cm->cm_sg_refs);
363 	return (BLKIF_SEGS_TO_BLOCKS(cm->cm_nseg));
364 }
365 
366 static void
367 xbd_int(void *xsc)
368 {
369 	struct xbd_softc *sc = xsc;
370 	struct xbd_command *cm;
371 	blkif_response_t *bret;
372 	RING_IDX i, rp;
373 	int op;
374 
375 	mtx_lock(&sc->xbd_io_lock);
376 
377 	if (unlikely(sc->xbd_connected == XBD_STATE_DISCONNECTED)) {
378 		mtx_unlock(&sc->xbd_io_lock);
379 		return;
380 	}
381 
382  again:
383 	rp = sc->xbd_ring.sring->rsp_prod;
384 	rmb(); /* Ensure we see queued responses up to 'rp'. */
385 
386 	for (i = sc->xbd_ring.rsp_cons; i != rp;) {
387 		bret = RING_GET_RESPONSE(&sc->xbd_ring, i);
388 		cm   = &sc->xbd_shadow[bret->id];
389 
390 		xbd_remove_busy(cm);
391 		i += xbd_completion(cm);
392 
393 		if (cm->cm_operation == BLKIF_OP_READ)
394 			op = BUS_DMASYNC_POSTREAD;
395 		else if (cm->cm_operation == BLKIF_OP_WRITE)
396 			op = BUS_DMASYNC_POSTWRITE;
397 		else
398 			op = 0;
399 		bus_dmamap_sync(sc->xbd_io_dmat, cm->cm_map, op);
400 		bus_dmamap_unload(sc->xbd_io_dmat, cm->cm_map);
401 
402 		/*
403 		 * If commands are completing then resources are probably
404 		 * being freed as well.  It's a cheap assumption even when
405 		 * wrong.
406 		 */
407 		sc->xbd_flags &= ~XBD_FROZEN;
408 
409 		/*
410 		 * Directly call the i/o complete routine to save an
411 		 * an indirection in the common case.
412 		 */
413 		cm->cm_status = bret->status;
414 		if (cm->cm_bp)
415 			xbd_bio_complete(sc, cm);
416 		else if (cm->cm_complete != NULL)
417 			cm->cm_complete(cm);
418 		else
419 			xbd_free_command(cm);
420 	}
421 
422 	sc->xbd_ring.rsp_cons = i;
423 
424 	if (i != sc->xbd_ring.req_prod_pvt) {
425 		int more_to_do;
426 		RING_FINAL_CHECK_FOR_RESPONSES(&sc->xbd_ring, more_to_do);
427 		if (more_to_do)
428 			goto again;
429 	} else {
430 		sc->xbd_ring.sring->rsp_event = i + 1;
431 	}
432 
433 	xbd_startio(sc);
434 
435 	if (unlikely(sc->xbd_connected == XBD_STATE_SUSPENDED))
436 		wakeup(&sc->xbd_cm_busy);
437 
438 	mtx_unlock(&sc->xbd_io_lock);
439 }
440 
441 /*------------------------------- Dump Support -------------------------------*/
442 /**
443  * Quiesce the disk writes for a dump file before allowing the next buffer.
444  */
445 static void
446 xbd_quiesce(struct xbd_softc *sc)
447 {
448 	int mtd;
449 
450 	// While there are outstanding requests
451 	while (!TAILQ_EMPTY(&sc->xbd_cm_busy)) {
452 		RING_FINAL_CHECK_FOR_RESPONSES(&sc->xbd_ring, mtd);
453 		if (mtd) {
454 			/* Recieved request completions, update queue. */
455 			xbd_int(sc);
456 		}
457 		if (!TAILQ_EMPTY(&sc->xbd_cm_busy)) {
458 			/*
459 			 * Still pending requests, wait for the disk i/o
460 			 * to complete.
461 			 */
462 			HYPERVISOR_yield();
463 		}
464 	}
465 }
466 
467 /* Kernel dump function for a paravirtualized disk device */
468 static void
469 xbd_dump_complete(struct xbd_command *cm)
470 {
471 
472 	xbd_enqueue_complete(cm);
473 }
474 
475 static int
476 xbd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset,
477     size_t length)
478 {
479 	struct disk *dp = arg;
480 	struct xbd_softc *sc = dp->d_drv1;
481 	struct xbd_command *cm;
482 	size_t chunk;
483 	int sbp;
484 	int rc = 0;
485 
486 	if (length <= 0)
487 		return (rc);
488 
489 	xbd_quiesce(sc);	/* All quiet on the western front. */
490 
491 	/*
492 	 * If this lock is held, then this module is failing, and a
493 	 * successful kernel dump is highly unlikely anyway.
494 	 */
495 	mtx_lock(&sc->xbd_io_lock);
496 
497 	/* Split the 64KB block as needed */
498 	for (sbp=0; length > 0; sbp++) {
499 		cm = xbd_dequeue_free(sc);
500 		if (cm == NULL) {
501 			mtx_unlock(&sc->xbd_io_lock);
502 			device_printf(sc->xbd_dev, "dump: no more commands?\n");
503 			return (EBUSY);
504 		}
505 
506 		if (gnttab_alloc_grant_references(sc->xbd_max_request_segments,
507 		    &cm->cm_gref_head) != 0) {
508 			xbd_free_command(cm);
509 			mtx_unlock(&sc->xbd_io_lock);
510 			device_printf(sc->xbd_dev, "no more grant allocs?\n");
511 			return (EBUSY);
512 		}
513 
514 		chunk = length > sc->xbd_max_request_size ?
515 		    sc->xbd_max_request_size : length;
516 		cm->cm_data = virtual;
517 		cm->cm_datalen = chunk;
518 		cm->cm_operation = BLKIF_OP_WRITE;
519 		cm->cm_sector_number = offset / dp->d_sectorsize;
520 		cm->cm_complete = xbd_dump_complete;
521 
522 		xbd_enqueue_ready(cm);
523 
524 		length -= chunk;
525 		offset += chunk;
526 		virtual = (char *) virtual + chunk;
527 	}
528 
529 	/* Tell DOM0 to do the I/O */
530 	xbd_startio(sc);
531 	mtx_unlock(&sc->xbd_io_lock);
532 
533 	/* Poll for the completion. */
534 	xbd_quiesce(sc);	/* All quite on the eastern front */
535 
536 	/* If there were any errors, bail out... */
537 	while ((cm = xbd_dequeue_complete(sc)) != NULL) {
538 		if (cm->cm_status != BLKIF_RSP_OKAY) {
539 			device_printf(sc->xbd_dev,
540 			    "Dump I/O failed at sector %jd\n",
541 			    cm->cm_sector_number);
542 			rc = EIO;
543 		}
544 		xbd_free_command(cm);
545 	}
546 
547 	return (rc);
548 }
549 
550 /*----------------------------- Disk Entrypoints -----------------------------*/
551 static int
552 xbd_open(struct disk *dp)
553 {
554 	struct xbd_softc *sc = dp->d_drv1;
555 
556 	if (sc == NULL) {
557 		printf("xb%d: not found", sc->xbd_unit);
558 		return (ENXIO);
559 	}
560 
561 	sc->xbd_flags |= XBD_OPEN;
562 	sc->xbd_users++;
563 	return (0);
564 }
565 
566 static int
567 xbd_close(struct disk *dp)
568 {
569 	struct xbd_softc *sc = dp->d_drv1;
570 
571 	if (sc == NULL)
572 		return (ENXIO);
573 	sc->xbd_flags &= ~XBD_OPEN;
574 	if (--(sc->xbd_users) == 0) {
575 		/*
576 		 * Check whether we have been instructed to close.  We will
577 		 * have ignored this request initially, as the device was
578 		 * still mounted.
579 		 */
580 		if (xenbus_get_otherend_state(sc->xbd_dev) ==
581 		    XenbusStateClosing)
582 			xbd_closing(sc->xbd_dev);
583 	}
584 	return (0);
585 }
586 
587 static int
588 xbd_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
589 {
590 	struct xbd_softc *sc = dp->d_drv1;
591 
592 	if (sc == NULL)
593 		return (ENXIO);
594 
595 	return (ENOTTY);
596 }
597 
598 /*
599  * Read/write routine for a buffer.  Finds the proper unit, place it on
600  * the sortq and kick the controller.
601  */
602 static void
603 xbd_strategy(struct bio *bp)
604 {
605 	struct xbd_softc *sc = bp->bio_disk->d_drv1;
606 
607 	/* bogus disk? */
608 	if (sc == NULL) {
609 		bp->bio_error = EINVAL;
610 		bp->bio_flags |= BIO_ERROR;
611 		bp->bio_resid = bp->bio_bcount;
612 		biodone(bp);
613 		return;
614 	}
615 
616 	/*
617 	 * Place it in the queue of disk activities for this disk
618 	 */
619 	mtx_lock(&sc->xbd_io_lock);
620 
621 	xbd_enqueue_bio(sc, bp);
622 	xbd_startio(sc);
623 
624 	mtx_unlock(&sc->xbd_io_lock);
625 	return;
626 }
627 
628 /*------------------------------ Ring Management -----------------------------*/
629 static int
630 xbd_alloc_ring(struct xbd_softc *sc)
631 {
632 	blkif_sring_t *sring;
633 	uintptr_t sring_page_addr;
634 	int error;
635 	int i;
636 
637 	sring = malloc(sc->xbd_ring_pages * PAGE_SIZE, M_XENBLOCKFRONT,
638 	    M_NOWAIT|M_ZERO);
639 	if (sring == NULL) {
640 		xenbus_dev_fatal(sc->xbd_dev, ENOMEM, "allocating shared ring");
641 		return (ENOMEM);
642 	}
643 	SHARED_RING_INIT(sring);
644 	FRONT_RING_INIT(&sc->xbd_ring, sring, sc->xbd_ring_pages * PAGE_SIZE);
645 
646 	for (i = 0, sring_page_addr = (uintptr_t)sring;
647 	     i < sc->xbd_ring_pages;
648 	     i++, sring_page_addr += PAGE_SIZE) {
649 
650 		error = xenbus_grant_ring(sc->xbd_dev,
651 		    (vtomach(sring_page_addr) >> PAGE_SHIFT),
652 		    &sc->xbd_ring_ref[i]);
653 		if (error) {
654 			xenbus_dev_fatal(sc->xbd_dev, error,
655 			    "granting ring_ref(%d)", i);
656 			return (error);
657 		}
658 	}
659 	if (sc->xbd_ring_pages == 1) {
660 		error = xs_printf(XST_NIL, xenbus_get_node(sc->xbd_dev),
661 		    "ring-ref", "%u", sc->xbd_ring_ref[0]);
662 		if (error) {
663 			xenbus_dev_fatal(sc->xbd_dev, error,
664 			    "writing %s/ring-ref",
665 			    xenbus_get_node(sc->xbd_dev));
666 			return (error);
667 		}
668 	} else {
669 		for (i = 0; i < sc->xbd_ring_pages; i++) {
670 			char ring_ref_name[]= "ring_refXX";
671 
672 			snprintf(ring_ref_name, sizeof(ring_ref_name),
673 			    "ring-ref%u", i);
674 			error = xs_printf(XST_NIL, xenbus_get_node(sc->xbd_dev),
675 			     ring_ref_name, "%u", sc->xbd_ring_ref[i]);
676 			if (error) {
677 				xenbus_dev_fatal(sc->xbd_dev, error,
678 				    "writing %s/%s",
679 				    xenbus_get_node(sc->xbd_dev),
680 				    ring_ref_name);
681 				return (error);
682 			}
683 		}
684 	}
685 
686 	error = bind_listening_port_to_irqhandler(
687 	    xenbus_get_otherend_id(sc->xbd_dev),
688 	    "xbd", (driver_intr_t *)xbd_int, sc,
689 	    INTR_TYPE_BIO | INTR_MPSAFE, &sc->xbd_irq);
690 	if (error) {
691 		xenbus_dev_fatal(sc->xbd_dev, error,
692 		    "bind_evtchn_to_irqhandler failed");
693 		return (error);
694 	}
695 
696 	return (0);
697 }
698 
699 static void
700 xbd_free_ring(struct xbd_softc *sc)
701 {
702 	int i;
703 
704 	if (sc->xbd_ring.sring == NULL)
705 		return;
706 
707 	for (i = 0; i < sc->xbd_ring_pages; i++) {
708 		if (sc->xbd_ring_ref[i] != GRANT_REF_INVALID) {
709 			gnttab_end_foreign_access_ref(sc->xbd_ring_ref[i]);
710 			sc->xbd_ring_ref[i] = GRANT_REF_INVALID;
711 		}
712 	}
713 	free(sc->xbd_ring.sring, M_XENBLOCKFRONT);
714 	sc->xbd_ring.sring = NULL;
715 }
716 
717 /*-------------------------- Initialization/Teardown -------------------------*/
718 static void
719 xbd_setup_sysctl(struct xbd_softc *xbd)
720 {
721 	struct sysctl_ctx_list *sysctl_ctx = NULL;
722 	struct sysctl_oid *sysctl_tree = NULL;
723 
724 	sysctl_ctx = device_get_sysctl_ctx(xbd->xbd_dev);
725 	if (sysctl_ctx == NULL)
726 		return;
727 
728 	sysctl_tree = device_get_sysctl_tree(xbd->xbd_dev);
729 	if (sysctl_tree == NULL)
730 		return;
731 
732 	SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
733 	    "max_requests", CTLFLAG_RD, &xbd->xbd_max_requests, -1,
734 	    "maximum outstanding requests (negotiated)");
735 
736 	SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
737 	    "max_request_segments", CTLFLAG_RD,
738 	    &xbd->xbd_max_request_segments, 0,
739 	    "maximum number of pages per requests (negotiated)");
740 
741 	SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
742 	    "max_request_size", CTLFLAG_RD, &xbd->xbd_max_request_size, 0,
743 	    "maximum size in bytes of a request (negotiated)");
744 
745 	SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
746 	    "ring_pages", CTLFLAG_RD, &xbd->xbd_ring_pages, 0,
747 	    "communication channel pages (negotiated)");
748 }
749 
750 /*
751  * Translate Linux major/minor to an appropriate name and unit
752  * number. For HVM guests, this allows us to use the same drive names
753  * with blkfront as the emulated drives, easing transition slightly.
754  */
755 static void
756 xbd_vdevice_to_unit(uint32_t vdevice, int *unit, const char **name)
757 {
758 	static struct vdev_info {
759 		int major;
760 		int shift;
761 		int base;
762 		const char *name;
763 	} info[] = {
764 		{3,	6,	0,	"ada"},	/* ide0 */
765 		{22,	6,	2,	"ada"},	/* ide1 */
766 		{33,	6,	4,	"ada"},	/* ide2 */
767 		{34,	6,	6,	"ada"},	/* ide3 */
768 		{56,	6,	8,	"ada"},	/* ide4 */
769 		{57,	6,	10,	"ada"},	/* ide5 */
770 		{88,	6,	12,	"ada"},	/* ide6 */
771 		{89,	6,	14,	"ada"},	/* ide7 */
772 		{90,	6,	16,	"ada"},	/* ide8 */
773 		{91,	6,	18,	"ada"},	/* ide9 */
774 
775 		{8,	4,	0,	"da"},	/* scsi disk0 */
776 		{65,	4,	16,	"da"},	/* scsi disk1 */
777 		{66,	4,	32,	"da"},	/* scsi disk2 */
778 		{67,	4,	48,	"da"},	/* scsi disk3 */
779 		{68,	4,	64,	"da"},	/* scsi disk4 */
780 		{69,	4,	80,	"da"},	/* scsi disk5 */
781 		{70,	4,	96,	"da"},	/* scsi disk6 */
782 		{71,	4,	112,	"da"},	/* scsi disk7 */
783 		{128,	4,	128,	"da"},	/* scsi disk8 */
784 		{129,	4,	144,	"da"},	/* scsi disk9 */
785 		{130,	4,	160,	"da"},	/* scsi disk10 */
786 		{131,	4,	176,	"da"},	/* scsi disk11 */
787 		{132,	4,	192,	"da"},	/* scsi disk12 */
788 		{133,	4,	208,	"da"},	/* scsi disk13 */
789 		{134,	4,	224,	"da"},	/* scsi disk14 */
790 		{135,	4,	240,	"da"},	/* scsi disk15 */
791 
792 		{202,	4,	0,	"xbd"},	/* xbd */
793 
794 		{0,	0,	0,	NULL},
795 	};
796 	int major = vdevice >> 8;
797 	int minor = vdevice & 0xff;
798 	int i;
799 
800 	if (vdevice & (1 << 28)) {
801 		*unit = (vdevice & ((1 << 28) - 1)) >> 8;
802 		*name = "xbd";
803 		return;
804 	}
805 
806 	for (i = 0; info[i].major; i++) {
807 		if (info[i].major == major) {
808 			*unit = info[i].base + (minor >> info[i].shift);
809 			*name = info[i].name;
810 			return;
811 		}
812 	}
813 
814 	*unit = minor >> 4;
815 	*name = "xbd";
816 }
817 
818 int
819 xbd_instance_create(struct xbd_softc *sc, blkif_sector_t sectors,
820     int vdevice, uint16_t vdisk_info, unsigned long sector_size)
821 {
822 	int unit, error = 0;
823 	const char *name;
824 
825 	xbd_vdevice_to_unit(vdevice, &unit, &name);
826 
827 	sc->xbd_unit = unit;
828 
829 	if (strcmp(name, "xbd"))
830 		device_printf(sc->xbd_dev, "attaching as %s%d\n", name, unit);
831 
832 	sc->xbd_disk = disk_alloc();
833 	sc->xbd_disk->d_unit = sc->xbd_unit;
834 	sc->xbd_disk->d_open = xbd_open;
835 	sc->xbd_disk->d_close = xbd_close;
836 	sc->xbd_disk->d_ioctl = xbd_ioctl;
837 	sc->xbd_disk->d_strategy = xbd_strategy;
838 	sc->xbd_disk->d_dump = xbd_dump;
839 	sc->xbd_disk->d_name = name;
840 	sc->xbd_disk->d_drv1 = sc;
841 	sc->xbd_disk->d_sectorsize = sector_size;
842 
843 	sc->xbd_disk->d_mediasize = sectors * sector_size;
844 	sc->xbd_disk->d_maxsize = sc->xbd_max_request_size;
845 	sc->xbd_disk->d_flags = 0;
846 	disk_create(sc->xbd_disk, DISK_VERSION);
847 
848 	return error;
849 }
850 
851 static void
852 xbd_free(struct xbd_softc *sc)
853 {
854 	int i;
855 
856 	/* Prevent new requests being issued until we fix things up. */
857 	mtx_lock(&sc->xbd_io_lock);
858 	sc->xbd_connected = XBD_STATE_DISCONNECTED;
859 	mtx_unlock(&sc->xbd_io_lock);
860 
861 	/* Free resources associated with old device channel. */
862 	xbd_free_ring(sc);
863 	if (sc->xbd_shadow) {
864 
865 		for (i = 0; i < sc->xbd_max_requests; i++) {
866 			struct xbd_command *cm;
867 
868 			cm = &sc->xbd_shadow[i];
869 			if (cm->cm_sg_refs != NULL) {
870 				free(cm->cm_sg_refs, M_XENBLOCKFRONT);
871 				cm->cm_sg_refs = NULL;
872 			}
873 
874 			bus_dmamap_destroy(sc->xbd_io_dmat, cm->cm_map);
875 		}
876 		free(sc->xbd_shadow, M_XENBLOCKFRONT);
877 		sc->xbd_shadow = NULL;
878 
879 		bus_dma_tag_destroy(sc->xbd_io_dmat);
880 
881 		xbd_initq_free(sc);
882 		xbd_initq_ready(sc);
883 		xbd_initq_complete(sc);
884 	}
885 
886 	if (sc->xbd_irq) {
887 		unbind_from_irqhandler(sc->xbd_irq);
888 		sc->xbd_irq = 0;
889 	}
890 }
891 
892 /*--------------------------- State Change Handlers --------------------------*/
893 static void
894 xbd_initialize(struct xbd_softc *sc)
895 {
896 	const char *otherend_path;
897 	const char *node_path;
898 	uint32_t max_ring_page_order;
899 	int error;
900 	int i;
901 
902 	if (xenbus_get_state(sc->xbd_dev) != XenbusStateInitialising) {
903 		/* Initialization has already been performed. */
904 		return;
905 	}
906 
907 	/*
908 	 * Protocol defaults valid even if negotiation for a
909 	 * setting fails.
910 	 */
911 	max_ring_page_order = 0;
912 	sc->xbd_ring_pages = 1;
913 	sc->xbd_max_request_segments = BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK;
914 	sc->xbd_max_request_size =
915 	    XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments);
916 	sc->xbd_max_request_blocks =
917 	    BLKIF_SEGS_TO_BLOCKS(sc->xbd_max_request_segments);
918 
919 	/*
920 	 * Protocol negotiation.
921 	 *
922 	 * \note xs_gather() returns on the first encountered error, so
923 	 *       we must use independant calls in order to guarantee
924 	 *       we don't miss information in a sparsly populated back-end
925 	 *       tree.
926 	 *
927 	 * \note xs_scanf() does not update variables for unmatched
928 	 *	 fields.
929 	 */
930 	otherend_path = xenbus_get_otherend_path(sc->xbd_dev);
931 	node_path = xenbus_get_node(sc->xbd_dev);
932 
933 	/* Support both backend schemes for relaying ring page limits. */
934 	(void)xs_scanf(XST_NIL, otherend_path,
935 	    "max-ring-page-order", NULL, "%" PRIu32,
936 	    &max_ring_page_order);
937 	sc->xbd_ring_pages = 1 << max_ring_page_order;
938 	(void)xs_scanf(XST_NIL, otherend_path,
939 	    "max-ring-pages", NULL, "%" PRIu32,
940 	    &sc->xbd_ring_pages);
941 	if (sc->xbd_ring_pages < 1)
942 		sc->xbd_ring_pages = 1;
943 
944 	sc->xbd_max_requests =
945 	    BLKIF_MAX_RING_REQUESTS(sc->xbd_ring_pages * PAGE_SIZE);
946 	(void)xs_scanf(XST_NIL, otherend_path,
947 	    "max-requests", NULL, "%" PRIu32,
948 	    &sc->xbd_max_requests);
949 
950 	(void)xs_scanf(XST_NIL, otherend_path,
951 	    "max-request-segments", NULL, "%" PRIu32,
952 	    &sc->xbd_max_request_segments);
953 
954 	(void)xs_scanf(XST_NIL, otherend_path,
955 	    "max-request-size", NULL, "%" PRIu32,
956 	    &sc->xbd_max_request_size);
957 
958 	if (sc->xbd_ring_pages > XBD_MAX_RING_PAGES) {
959 		device_printf(sc->xbd_dev,
960 		    "Back-end specified ring-pages of %u "
961 		    "limited to front-end limit of %zu.\n",
962 		    sc->xbd_ring_pages, XBD_MAX_RING_PAGES);
963 		sc->xbd_ring_pages = XBD_MAX_RING_PAGES;
964 	}
965 
966 	if (powerof2(sc->xbd_ring_pages) == 0) {
967 		uint32_t new_page_limit;
968 
969 		new_page_limit = 0x01 << (fls(sc->xbd_ring_pages) - 1);
970 		device_printf(sc->xbd_dev,
971 		    "Back-end specified ring-pages of %u "
972 		    "is not a power of 2. Limited to %u.\n",
973 		    sc->xbd_ring_pages, new_page_limit);
974 		sc->xbd_ring_pages = new_page_limit;
975 	}
976 
977 	if (sc->xbd_max_requests > XBD_MAX_REQUESTS) {
978 		device_printf(sc->xbd_dev,
979 		    "Back-end specified max_requests of %u "
980 		    "limited to front-end limit of %u.\n",
981 		    sc->xbd_max_requests, XBD_MAX_REQUESTS);
982 		sc->xbd_max_requests = XBD_MAX_REQUESTS;
983 	}
984 
985 	if (sc->xbd_max_request_segments > XBD_MAX_SEGMENTS_PER_REQUEST) {
986 		device_printf(sc->xbd_dev,
987 		    "Back-end specified max_request_segments of %u "
988 		    "limited to front-end limit of %u.\n",
989 		    sc->xbd_max_request_segments,
990 		    XBD_MAX_SEGMENTS_PER_REQUEST);
991 		sc->xbd_max_request_segments = XBD_MAX_SEGMENTS_PER_REQUEST;
992 	}
993 
994 	if (sc->xbd_max_request_size > XBD_MAX_REQUEST_SIZE) {
995 		device_printf(sc->xbd_dev,
996 		    "Back-end specified max_request_size of %u "
997 		    "limited to front-end limit of %u.\n",
998 		    sc->xbd_max_request_size,
999 		    XBD_MAX_REQUEST_SIZE);
1000 		sc->xbd_max_request_size = XBD_MAX_REQUEST_SIZE;
1001 	}
1002 
1003  	if (sc->xbd_max_request_size >
1004 	    XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments)) {
1005  		device_printf(sc->xbd_dev,
1006 		    "Back-end specified max_request_size of %u "
1007 		    "limited to front-end limit of %u.  (Too few segments.)\n",
1008 		    sc->xbd_max_request_size,
1009 		    XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments));
1010  		sc->xbd_max_request_size =
1011  		    XBD_SEGS_TO_SIZE(sc->xbd_max_request_segments);
1012  	}
1013 
1014 	sc->xbd_max_request_blocks =
1015 	    BLKIF_SEGS_TO_BLOCKS(sc->xbd_max_request_segments);
1016 
1017 	/* Allocate datastructures based on negotiated values. */
1018 	error = bus_dma_tag_create(
1019 	    bus_get_dma_tag(sc->xbd_dev),	/* parent */
1020 	    512, PAGE_SIZE,			/* algnmnt, boundary */
1021 	    BUS_SPACE_MAXADDR,			/* lowaddr */
1022 	    BUS_SPACE_MAXADDR,			/* highaddr */
1023 	    NULL, NULL,				/* filter, filterarg */
1024 	    sc->xbd_max_request_size,
1025 	    sc->xbd_max_request_segments,
1026 	    PAGE_SIZE,				/* maxsegsize */
1027 	    BUS_DMA_ALLOCNOW,			/* flags */
1028 	    busdma_lock_mutex,			/* lockfunc */
1029 	    &sc->xbd_io_lock,			/* lockarg */
1030 	    &sc->xbd_io_dmat);
1031 	if (error != 0) {
1032 		xenbus_dev_fatal(sc->xbd_dev, error,
1033 		    "Cannot allocate parent DMA tag\n");
1034 		return;
1035 	}
1036 
1037 	/* Per-transaction data allocation. */
1038 	sc->xbd_shadow = malloc(sizeof(*sc->xbd_shadow) * sc->xbd_max_requests,
1039 	    M_XENBLOCKFRONT, M_NOWAIT|M_ZERO);
1040 	if (sc->xbd_shadow == NULL) {
1041 		bus_dma_tag_destroy(sc->xbd_io_dmat);
1042 		xenbus_dev_fatal(sc->xbd_dev, error,
1043 		    "Cannot allocate request structures\n");
1044 		return;
1045 	}
1046 
1047 	for (i = 0; i < sc->xbd_max_requests; i++) {
1048 		struct xbd_command *cm;
1049 
1050 		cm = &sc->xbd_shadow[i];
1051 		cm->cm_sg_refs = malloc(
1052 		    sizeof(grant_ref_t) * sc->xbd_max_request_segments,
1053 		    M_XENBLOCKFRONT, M_NOWAIT);
1054 		if (cm->cm_sg_refs == NULL)
1055 			break;
1056 		cm->cm_id = i;
1057 		cm->cm_sc = sc;
1058 		if (bus_dmamap_create(sc->xbd_io_dmat, 0, &cm->cm_map) != 0)
1059 			break;
1060 		xbd_free_command(cm);
1061 	}
1062 
1063 	if (xbd_alloc_ring(sc) != 0)
1064 		return;
1065 
1066 	/* Support both backend schemes for relaying ring page limits. */
1067 	if (sc->xbd_ring_pages > 1) {
1068 		error = xs_printf(XST_NIL, node_path,
1069 		    "num-ring-pages","%u",
1070 		    sc->xbd_ring_pages);
1071 		if (error) {
1072 			xenbus_dev_fatal(sc->xbd_dev, error,
1073 			    "writing %s/num-ring-pages",
1074 			    node_path);
1075 			return;
1076 		}
1077 
1078 		error = xs_printf(XST_NIL, node_path,
1079 		    "ring-page-order", "%u",
1080 		    fls(sc->xbd_ring_pages) - 1);
1081 		if (error) {
1082 			xenbus_dev_fatal(sc->xbd_dev, error,
1083 			    "writing %s/ring-page-order",
1084 			    node_path);
1085 			return;
1086 		}
1087 	}
1088 
1089 	error = xs_printf(XST_NIL, node_path,
1090 	    "max-requests","%u",
1091 	    sc->xbd_max_requests);
1092 	if (error) {
1093 		xenbus_dev_fatal(sc->xbd_dev, error,
1094 		    "writing %s/max-requests",
1095 		    node_path);
1096 		return;
1097 	}
1098 
1099 	error = xs_printf(XST_NIL, node_path,
1100 	    "max-request-segments","%u",
1101 	    sc->xbd_max_request_segments);
1102 	if (error) {
1103 		xenbus_dev_fatal(sc->xbd_dev, error,
1104 		    "writing %s/max-request-segments",
1105 		    node_path);
1106 		return;
1107 	}
1108 
1109 	error = xs_printf(XST_NIL, node_path,
1110 	    "max-request-size","%u",
1111 	    sc->xbd_max_request_size);
1112 	if (error) {
1113 		xenbus_dev_fatal(sc->xbd_dev, error,
1114 		    "writing %s/max-request-size",
1115 		    node_path);
1116 		return;
1117 	}
1118 
1119 	error = xs_printf(XST_NIL, node_path, "event-channel",
1120 	    "%u", irq_to_evtchn_port(sc->xbd_irq));
1121 	if (error) {
1122 		xenbus_dev_fatal(sc->xbd_dev, error,
1123 		    "writing %s/event-channel",
1124 		    node_path);
1125 		return;
1126 	}
1127 
1128 	error = xs_printf(XST_NIL, node_path, "protocol",
1129 	    "%s", XEN_IO_PROTO_ABI_NATIVE);
1130 	if (error) {
1131 		xenbus_dev_fatal(sc->xbd_dev, error,
1132 		    "writing %s/protocol",
1133 		    node_path);
1134 		return;
1135 	}
1136 
1137 	xenbus_set_state(sc->xbd_dev, XenbusStateInitialised);
1138 }
1139 
1140 /*
1141  * Invoked when the backend is finally 'ready' (and has published
1142  * the details about the physical device - #sectors, size, etc).
1143  */
1144 static void
1145 xbd_connect(struct xbd_softc *sc)
1146 {
1147 	device_t dev = sc->xbd_dev;
1148 	unsigned long sectors, sector_size;
1149 	unsigned int binfo;
1150 	int err, feature_barrier;
1151 
1152 	if ((sc->xbd_connected == XBD_STATE_CONNECTED) ||
1153 	    (sc->xbd_connected == XBD_STATE_SUSPENDED))
1154 		return;
1155 
1156 	DPRINTK("blkfront.c:connect:%s.\n", xenbus_get_otherend_path(dev));
1157 
1158 	err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
1159 	    "sectors", "%lu", &sectors,
1160 	    "info", "%u", &binfo,
1161 	    "sector-size", "%lu", &sector_size,
1162 	    NULL);
1163 	if (err) {
1164 		xenbus_dev_fatal(dev, err,
1165 		    "reading backend fields at %s",
1166 		    xenbus_get_otherend_path(dev));
1167 		return;
1168 	}
1169 	err = xs_gather(XST_NIL, xenbus_get_otherend_path(dev),
1170 	     "feature-barrier", "%lu", &feature_barrier,
1171 	     NULL);
1172 	if (!err || feature_barrier)
1173 		sc->xbd_flags |= XBD_BARRIER;
1174 
1175 	if (sc->xbd_disk == NULL) {
1176 		device_printf(dev, "%juMB <%s> at %s",
1177 		    (uintmax_t) sectors / (1048576 / sector_size),
1178 		    device_get_desc(dev),
1179 		    xenbus_get_node(dev));
1180 		bus_print_child_footer(device_get_parent(dev), dev);
1181 
1182 		xbd_instance_create(sc, sectors, sc->xbd_vdevice, binfo,
1183 		    sector_size);
1184 	}
1185 
1186 	(void)xenbus_set_state(dev, XenbusStateConnected);
1187 
1188 	/* Kick pending requests. */
1189 	mtx_lock(&sc->xbd_io_lock);
1190 	sc->xbd_connected = XBD_STATE_CONNECTED;
1191 	xbd_startio(sc);
1192 	sc->xbd_flags |= XBD_READY;
1193 	mtx_unlock(&sc->xbd_io_lock);
1194 }
1195 
1196 /**
1197  * Handle the change of state of the backend to Closing.  We must delete our
1198  * device-layer structures now, to ensure that writes are flushed through to
1199  * the backend.  Once this is done, we can switch to Closed in
1200  * acknowledgement.
1201  */
1202 static void
1203 xbd_closing(device_t dev)
1204 {
1205 	struct xbd_softc *sc = device_get_softc(dev);
1206 
1207 	xenbus_set_state(dev, XenbusStateClosing);
1208 
1209 	DPRINTK("xbd_closing: %s removed\n", xenbus_get_node(dev));
1210 
1211 	if (sc->xbd_disk != NULL) {
1212 		disk_destroy(sc->xbd_disk);
1213 		sc->xbd_disk = NULL;
1214 	}
1215 
1216 	xenbus_set_state(dev, XenbusStateClosed);
1217 }
1218 
1219 /*---------------------------- NewBus Entrypoints ----------------------------*/
1220 static int
1221 xbd_probe(device_t dev)
1222 {
1223 
1224 	if (!strcmp(xenbus_get_type(dev), "vbd")) {
1225 		device_set_desc(dev, "Virtual Block Device");
1226 		device_quiet(dev);
1227 		return (0);
1228 	}
1229 
1230 	return (ENXIO);
1231 }
1232 
1233 /*
1234  * Setup supplies the backend dir, virtual device.  We place an event
1235  * channel and shared frame entries.  We watch backend to wait if it's
1236  * ok.
1237  */
1238 static int
1239 xbd_attach(device_t dev)
1240 {
1241 	struct xbd_softc *sc;
1242 	const char *name;
1243 	uint32_t vdevice;
1244 	int error;
1245 	int i;
1246 	int unit;
1247 
1248 	/* FIXME: Use dynamic device id if this is not set. */
1249 	error = xs_scanf(XST_NIL, xenbus_get_node(dev),
1250 	    "virtual-device", NULL, "%" PRIu32, &vdevice);
1251 	if (error) {
1252 		xenbus_dev_fatal(dev, error, "reading virtual-device");
1253 		device_printf(dev, "Couldn't determine virtual device.\n");
1254 		return (error);
1255 	}
1256 
1257 	xbd_vdevice_to_unit(vdevice, &unit, &name);
1258 	if (!strcmp(name, "xbd"))
1259 		device_set_unit(dev, unit);
1260 
1261 	sc = device_get_softc(dev);
1262 	mtx_init(&sc->xbd_io_lock, "blkfront i/o lock", NULL, MTX_DEF);
1263 	xbd_initq_free(sc);
1264 	xbd_initq_busy(sc);
1265 	xbd_initq_ready(sc);
1266 	xbd_initq_complete(sc);
1267 	xbd_initq_bio(sc);
1268 	for (i = 0; i < XBD_MAX_RING_PAGES; i++)
1269 		sc->xbd_ring_ref[i] = GRANT_REF_INVALID;
1270 
1271 	sc->xbd_dev = dev;
1272 	sc->xbd_vdevice = vdevice;
1273 	sc->xbd_connected = XBD_STATE_DISCONNECTED;
1274 
1275 	xbd_setup_sysctl(sc);
1276 
1277 	/* Wait for backend device to publish its protocol capabilities. */
1278 	xenbus_set_state(dev, XenbusStateInitialising);
1279 
1280 	return (0);
1281 }
1282 
1283 static int
1284 xbd_detach(device_t dev)
1285 {
1286 	struct xbd_softc *sc = device_get_softc(dev);
1287 
1288 	DPRINTK("xbd_remove: %s removed\n", xenbus_get_node(dev));
1289 
1290 	xbd_free(sc);
1291 	mtx_destroy(&sc->xbd_io_lock);
1292 
1293 	return 0;
1294 }
1295 
1296 static int
1297 xbd_suspend(device_t dev)
1298 {
1299 	struct xbd_softc *sc = device_get_softc(dev);
1300 	int retval;
1301 	int saved_state;
1302 
1303 	/* Prevent new requests being issued until we fix things up. */
1304 	mtx_lock(&sc->xbd_io_lock);
1305 	saved_state = sc->xbd_connected;
1306 	sc->xbd_connected = XBD_STATE_SUSPENDED;
1307 
1308 	/* Wait for outstanding I/O to drain. */
1309 	retval = 0;
1310 	while (TAILQ_EMPTY(&sc->xbd_cm_busy) == 0) {
1311 		if (msleep(&sc->xbd_cm_busy, &sc->xbd_io_lock,
1312 		    PRIBIO, "blkf_susp", 30 * hz) == EWOULDBLOCK) {
1313 			retval = EBUSY;
1314 			break;
1315 		}
1316 	}
1317 	mtx_unlock(&sc->xbd_io_lock);
1318 
1319 	if (retval != 0)
1320 		sc->xbd_connected = saved_state;
1321 
1322 	return (retval);
1323 }
1324 
1325 static int
1326 xbd_resume(device_t dev)
1327 {
1328 	struct xbd_softc *sc = device_get_softc(dev);
1329 
1330 	DPRINTK("xbd_resume: %s\n", xenbus_get_node(dev));
1331 
1332 	xbd_free(sc);
1333 	xbd_initialize(sc);
1334 	return (0);
1335 }
1336 
1337 /**
1338  * Callback received when the backend's state changes.
1339  */
1340 static void
1341 xbd_backend_changed(device_t dev, XenbusState backend_state)
1342 {
1343 	struct xbd_softc *sc = device_get_softc(dev);
1344 
1345 	DPRINTK("backend_state=%d\n", backend_state);
1346 
1347 	switch (backend_state) {
1348 	case XenbusStateUnknown:
1349 	case XenbusStateInitialising:
1350 	case XenbusStateReconfigured:
1351 	case XenbusStateReconfiguring:
1352 	case XenbusStateClosed:
1353 		break;
1354 
1355 	case XenbusStateInitWait:
1356 	case XenbusStateInitialised:
1357 		xbd_initialize(sc);
1358 		break;
1359 
1360 	case XenbusStateConnected:
1361 		xbd_initialize(sc);
1362 		xbd_connect(sc);
1363 		break;
1364 
1365 	case XenbusStateClosing:
1366 		if (sc->xbd_users > 0)
1367 			xenbus_dev_error(dev, -EBUSY,
1368 			    "Device in use; refusing to close");
1369 		else
1370 			xbd_closing(dev);
1371 		break;
1372 	}
1373 }
1374 
1375 /*---------------------------- NewBus Registration ---------------------------*/
1376 static device_method_t xbd_methods[] = {
1377 	/* Device interface */
1378 	DEVMETHOD(device_probe,         xbd_probe),
1379 	DEVMETHOD(device_attach,        xbd_attach),
1380 	DEVMETHOD(device_detach,        xbd_detach),
1381 	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
1382 	DEVMETHOD(device_suspend,       xbd_suspend),
1383 	DEVMETHOD(device_resume,        xbd_resume),
1384 
1385 	/* Xenbus interface */
1386 	DEVMETHOD(xenbus_otherend_changed, xbd_backend_changed),
1387 
1388 	{ 0, 0 }
1389 };
1390 
1391 static driver_t xbd_driver = {
1392 	"xbd",
1393 	xbd_methods,
1394 	sizeof(struct xbd_softc),
1395 };
1396 devclass_t xbd_devclass;
1397 
1398 DRIVER_MODULE(xbd, xenbusb_front, xbd_driver, xbd_devclass, 0, 0);
1399