xref: /titanic_51/usr/src/uts/common/xen/io/xdf.c (revision 8793b36b40d14ad0a0fecc97738dc118a928f46c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * xdf.c - Xen Virtual Block Device Driver
29  * TODO:
30  *	- support alternate block size (currently only DEV_BSIZE supported)
31  *	- revalidate geometry for removable devices
32  */
33 
34 #include <sys/ddi.h>
35 #include <sys/sunddi.h>
36 #include <sys/conf.h>
37 #include <sys/cmlb.h>
38 #include <sys/dkio.h>
39 #include <sys/promif.h>
40 #include <sys/sysmacros.h>
41 #include <sys/kstat.h>
42 #include <sys/mach_mmu.h>
43 #ifdef XPV_HVM_DRIVER
44 #include <sys/xpv_support.h>
45 #include <sys/sunndi.h>
46 #endif /* XPV_HVM_DRIVER */
47 #include <public/io/xenbus.h>
48 #include <xen/sys/xenbus_impl.h>
49 #include <xen/sys/xendev.h>
50 #include <sys/gnttab.h>
51 #include <sys/scsi/generic/inquiry.h>
52 #include <xen/io/blkif_impl.h>
53 #include <io/xdf.h>
54 
55 #define	FLUSH_DISKCACHE	0x1
56 #define	WRITE_BARRIER	0x2
57 #define	DEFAULT_FLUSH_BLOCK	156 /* block to write to cause a cache flush */
58 #define	USE_WRITE_BARRIER(vdp)				\
59 	((vdp)->xdf_feature_barrier && !(vdp)->xdf_flush_supported)
60 #define	USE_FLUSH_DISKCACHE(vdp)			\
61 	((vdp)->xdf_feature_barrier && (vdp)->xdf_flush_supported)
62 #define	IS_WRITE_BARRIER(vdp, bp)			\
63 	(!IS_READ(bp) && USE_WRITE_BARRIER(vdp) &&	\
64 	((bp)->b_un.b_addr == (vdp)->xdf_cache_flush_block))
65 #define	IS_FLUSH_DISKCACHE(bp)				\
66 	(!IS_READ(bp) && USE_FLUSH_DISKCACHE(vdp) && ((bp)->b_bcount == 0))
67 
68 static void *vbd_ss;
69 static kmem_cache_t *xdf_vreq_cache;
70 static kmem_cache_t *xdf_gs_cache;
71 static int xdf_maxphys = XB_MAXPHYS;
72 int xdfdebug = 0;
73 extern int do_polled_io;
74 diskaddr_t xdf_flush_block = DEFAULT_FLUSH_BLOCK;
75 int	xdf_barrier_flush_disable = 0;
76 
77 /*
78  * dev_ops and cb_ops entrypoints
79  */
80 static int xdf_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
81 static int xdf_attach(dev_info_t *, ddi_attach_cmd_t);
82 static int xdf_detach(dev_info_t *, ddi_detach_cmd_t);
83 static int xdf_reset(dev_info_t *, ddi_reset_cmd_t);
84 static int xdf_open(dev_t *, int, int, cred_t *);
85 static int xdf_close(dev_t, int, int, struct cred *);
86 static int xdf_strategy(struct buf *);
87 static int xdf_read(dev_t, struct uio *, cred_t *);
88 static int xdf_aread(dev_t, struct aio_req *, cred_t *);
89 static int xdf_write(dev_t, struct uio *, cred_t *);
90 static int xdf_awrite(dev_t, struct aio_req *, cred_t *);
91 static int xdf_dump(dev_t, caddr_t, daddr_t, int);
92 static int xdf_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
93 static uint_t xdf_intr(caddr_t);
94 static int xdf_prop_op(dev_t, dev_info_t *, ddi_prop_op_t, int, char *,
95     caddr_t, int *);
96 
97 /*
98  * misc private functions
99  */
100 static int xdf_suspend(dev_info_t *);
101 static int xdf_resume(dev_info_t *);
102 static int xdf_start_connect(xdf_t *);
103 static int xdf_start_disconnect(xdf_t *);
104 static int xdf_post_connect(xdf_t *);
105 static void xdf_post_disconnect(xdf_t *);
106 static void xdf_oe_change(dev_info_t *, ddi_eventcookie_t, void *, void *);
107 static void xdf_iostart(xdf_t *);
108 static void xdf_iofini(xdf_t *, uint64_t, int);
109 static int xdf_prepare_rreq(xdf_t *, struct buf *, blkif_request_t *);
110 static int xdf_drain_io(xdf_t *);
111 static boolean_t xdf_isopen(xdf_t *, int);
112 static int xdf_check_state_transition(xdf_t *, XenbusState);
113 static int xdf_connect(xdf_t *, boolean_t);
114 static int xdf_dmacallback(caddr_t);
115 static void xdf_timeout_handler(void *);
116 static uint_t xdf_iorestart(caddr_t);
117 static v_req_t *vreq_get(xdf_t *, buf_t *);
118 static void vreq_free(xdf_t *, v_req_t *);
119 static int vreq_setup(xdf_t *, v_req_t *);
120 static ge_slot_t *gs_get(xdf_t *, int);
121 static void gs_free(xdf_t *, ge_slot_t *);
122 static grant_ref_t gs_grant(ge_slot_t *, mfn_t);
123 static void unexpectedie(xdf_t *);
124 static void xdfmin(struct buf *);
125 static void xdf_synthetic_pgeom(dev_info_t *, cmlb_geom_t *);
126 extern int xdf_kstat_create(dev_info_t *, char *, int);
127 extern void xdf_kstat_delete(dev_info_t *);
128 
129 #if defined(XPV_HVM_DRIVER)
130 static void xdf_hvm_add(dev_info_t *);
131 static void xdf_hvm_rm(dev_info_t *);
132 static void xdf_hvm_init(void);
133 static void xdf_hvm_fini(void);
134 #endif /* XPV_HVM_DRIVER */
135 
136 static 	struct cb_ops xdf_cbops = {
137 	xdf_open,
138 	xdf_close,
139 	xdf_strategy,
140 	nodev,
141 	xdf_dump,
142 	xdf_read,
143 	xdf_write,
144 	xdf_ioctl,
145 	nodev,
146 	nodev,
147 	nodev,
148 	nochpoll,
149 	xdf_prop_op,
150 	NULL,
151 	D_MP | D_NEW | D_64BIT,
152 	CB_REV,
153 	xdf_aread,
154 	xdf_awrite
155 };
156 
157 struct dev_ops xdf_devops = {
158 	DEVO_REV,		/* devo_rev */
159 	0,			/* devo_refcnt */
160 	xdf_getinfo,		/* devo_getinfo */
161 	nulldev,		/* devo_identify */
162 	nulldev,		/* devo_probe */
163 	xdf_attach,		/* devo_attach */
164 	xdf_detach,		/* devo_detach */
165 	xdf_reset,		/* devo_reset */
166 	&xdf_cbops,		/* devo_cb_ops */
167 	(struct bus_ops *)NULL	/* devo_bus_ops */
168 };
169 
170 static struct modldrv modldrv = {
171 	&mod_driverops,		/* Type of module.  This one is a driver */
172 	"virtual block driver",	/* short description */
173 	&xdf_devops		/* driver specific ops */
174 };
175 
176 static struct modlinkage xdf_modlinkage = {
177 	MODREV_1, (void *)&modldrv, NULL
178 };
179 
180 /*
181  * I/O buffer DMA attributes
182  * Make sure: one DMA window contains BLKIF_MAX_SEGMENTS_PER_REQUEST at most
183  */
184 static ddi_dma_attr_t xb_dma_attr = {
185 	DMA_ATTR_V0,
186 	(uint64_t)0,			/* lowest address */
187 	(uint64_t)0xffffffffffffffff,	/* highest usable address */
188 	(uint64_t)0xffffff,		/* DMA counter limit max */
189 	(uint64_t)XB_BSIZE,		/* alignment in bytes */
190 	XB_BSIZE - 1,			/* bitmap of burst sizes */
191 	XB_BSIZE,			/* min transfer */
192 	(uint64_t)XB_MAX_XFER, 		/* maximum transfer */
193 	(uint64_t)PAGEOFFSET,		/* 1 page segment length  */
194 	BLKIF_MAX_SEGMENTS_PER_REQUEST,	/* maximum number of segments */
195 	XB_BSIZE,			/* granularity */
196 	0,				/* flags (reserved) */
197 };
198 
199 static ddi_device_acc_attr_t xc_acc_attr = {
200 	DDI_DEVICE_ATTR_V0,
201 	DDI_NEVERSWAP_ACC,
202 	DDI_STRICTORDER_ACC
203 };
204 
205 /* callbacks from commmon label */
206 
207 int xdf_lb_rdwr(dev_info_t *, uchar_t, void *, diskaddr_t, size_t, void *);
208 int xdf_lb_getinfo(dev_info_t *, int, void *, void *);
209 
210 static cmlb_tg_ops_t xdf_lb_ops = {
211 	TG_DK_OPS_VERSION_1,
212 	xdf_lb_rdwr,
213 	xdf_lb_getinfo
214 };
215 
216 int
217 _init(void)
218 {
219 	int rc;
220 
221 	if ((rc = ddi_soft_state_init(&vbd_ss, sizeof (xdf_t), 0)) != 0)
222 		return (rc);
223 
224 	xdf_vreq_cache = kmem_cache_create("xdf_vreq_cache",
225 	    sizeof (v_req_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
226 	xdf_gs_cache = kmem_cache_create("xdf_gs_cache",
227 	    sizeof (ge_slot_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
228 
229 #if defined(XPV_HVM_DRIVER)
230 	xdf_hvm_init();
231 #endif /* XPV_HVM_DRIVER */
232 
233 	if ((rc = mod_install(&xdf_modlinkage)) != 0) {
234 #if defined(XPV_HVM_DRIVER)
235 		xdf_hvm_fini();
236 #endif /* XPV_HVM_DRIVER */
237 		kmem_cache_destroy(xdf_vreq_cache);
238 		kmem_cache_destroy(xdf_gs_cache);
239 		ddi_soft_state_fini(&vbd_ss);
240 		return (rc);
241 	}
242 
243 	return (rc);
244 }
245 
246 int
247 _fini(void)
248 {
249 
250 	int err;
251 	if ((err = mod_remove(&xdf_modlinkage)) != 0)
252 		return (err);
253 
254 #if defined(XPV_HVM_DRIVER)
255 	xdf_hvm_fini();
256 #endif /* XPV_HVM_DRIVER */
257 
258 	kmem_cache_destroy(xdf_vreq_cache);
259 	kmem_cache_destroy(xdf_gs_cache);
260 	ddi_soft_state_fini(&vbd_ss);
261 
262 	return (0);
263 }
264 
265 int
266 _info(struct modinfo *modinfop)
267 {
268 	return (mod_info(&xdf_modlinkage, modinfop));
269 }
270 
271 /*ARGSUSED*/
272 static int
273 xdf_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **rp)
274 {
275 	int instance;
276 	xdf_t *vbdp;
277 
278 	instance = XDF_INST(getminor((dev_t)arg));
279 
280 	switch (cmd) {
281 	case DDI_INFO_DEVT2DEVINFO:
282 		if ((vbdp = ddi_get_soft_state(vbd_ss, instance)) == NULL) {
283 			*rp = NULL;
284 			return (DDI_FAILURE);
285 		}
286 		*rp = vbdp->xdf_dip;
287 		return (DDI_SUCCESS);
288 
289 	case DDI_INFO_DEVT2INSTANCE:
290 		*rp = (void *)(uintptr_t)instance;
291 		return (DDI_SUCCESS);
292 
293 	default:
294 		return (DDI_FAILURE);
295 	}
296 }
297 
298 static int
299 xdf_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
300 	char *name, caddr_t valuep, int *lengthp)
301 {
302 	xdf_t	*vdp;
303 
304 	if ((vdp = ddi_get_soft_state(vbd_ss, ddi_get_instance(dip))) == NULL)
305 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
306 		    name, valuep, lengthp));
307 
308 	return (cmlb_prop_op(vdp->xdf_vd_lbl,
309 	    dev, dip, prop_op, mod_flags, name, valuep, lengthp,
310 	    XDF_PART(getminor(dev)), NULL));
311 }
312 
313 static int
314 xdf_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
315 {
316 	xdf_t *vdp;
317 	ddi_iblock_cookie_t softibc;
318 	int instance;
319 
320 	xdfdebug = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_NOTPROM,
321 	    "xdfdebug", 0);
322 
323 	switch (cmd) {
324 		case DDI_ATTACH:
325 			break;
326 
327 		case DDI_RESUME:
328 			return (xdf_resume(devi));
329 
330 		default:
331 			return (DDI_FAILURE);
332 	}
333 
334 	instance = ddi_get_instance(devi);
335 	if (ddi_soft_state_zalloc(vbd_ss, instance) != DDI_SUCCESS)
336 		return (DDI_FAILURE);
337 
338 	DPRINTF(DDI_DBG, ("xdf%d: attaching\n", instance));
339 	vdp = ddi_get_soft_state(vbd_ss, instance);
340 	ddi_set_driver_private(devi, vdp);
341 	vdp->xdf_dip = devi;
342 	cv_init(&vdp->xdf_dev_cv, NULL, CV_DEFAULT, NULL);
343 
344 	if (ddi_get_iblock_cookie(devi, 0, &vdp->xdf_ibc) != DDI_SUCCESS) {
345 		cmn_err(CE_WARN, "xdf@%s: failed to get iblock cookie",
346 		    ddi_get_name_addr(devi));
347 		goto errout0;
348 	}
349 	mutex_init(&vdp->xdf_dev_lk, NULL, MUTEX_DRIVER, (void *)vdp->xdf_ibc);
350 	mutex_init(&vdp->xdf_cb_lk, NULL, MUTEX_DRIVER, (void *)vdp->xdf_ibc);
351 	mutex_init(&vdp->xdf_iostat_lk, NULL, MUTEX_DRIVER,
352 	    (void *)vdp->xdf_ibc);
353 
354 	if (ddi_get_soft_iblock_cookie(devi, DDI_SOFTINT_LOW, &softibc)
355 	    != DDI_SUCCESS) {
356 		cmn_err(CE_WARN, "xdf@%s: failed to get softintr iblock cookie",
357 		    ddi_get_name_addr(devi));
358 		goto errout0;
359 	}
360 	if (ddi_add_softintr(devi, DDI_SOFTINT_LOW, &vdp->xdf_softintr_id,
361 	    &softibc, NULL, xdf_iorestart, (caddr_t)vdp) != DDI_SUCCESS) {
362 		cmn_err(CE_WARN, "xdf@%s: failed to add softintr",
363 		    ddi_get_name_addr(devi));
364 		goto errout0;
365 	}
366 
367 #if !defined(XPV_HVM_DRIVER)
368 	/* create kstat for iostat(1M) */
369 	if (xdf_kstat_create(devi, "xdf", instance) != 0) {
370 		cmn_err(CE_WARN, "xdf@%s: failed to create kstat",
371 		    ddi_get_name_addr(devi));
372 		goto errout0;
373 	}
374 #endif /* !XPV_HVM_DRIVER */
375 
376 	/* driver handles kernel-issued IOCTLs */
377 	if (ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
378 	    DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
379 		cmn_err(CE_WARN, "xdf@%s: cannot create DDI_KERNEL_IOCTL prop",
380 		    ddi_get_name_addr(devi));
381 		goto errout0;
382 	}
383 
384 	/*
385 	 * Initialize the physical geometry stucture.  Note that currently
386 	 * we don't know the size of the backend device so the number
387 	 * of blocks on the device will be initialized to zero.  Once
388 	 * we connect to the backend device we'll update the physical
389 	 * geometry to reflect the real size of the device.
390 	 */
391 	xdf_synthetic_pgeom(devi, &vdp->xdf_pgeom);
392 
393 	/*
394 	 * create default device minor nodes: non-removable disk
395 	 * we will adjust minor nodes after we are connected w/ backend
396 	 */
397 	cmlb_alloc_handle(&vdp->xdf_vd_lbl);
398 	if (cmlb_attach(devi, &xdf_lb_ops, DTYPE_DIRECT, 0, 1,
399 	    DDI_NT_BLOCK_XVMD,
400 #if defined(XPV_HVM_DRIVER)
401 	    CMLB_CREATE_ALTSLICE_VTOC_16_DTYPE_DIRECT |
402 	    CMLB_INTERNAL_MINOR_NODES,
403 #else /* !XPV_HVM_DRIVER */
404 	    CMLB_FAKE_LABEL_ONE_PARTITION,
405 #endif /* !XPV_HVM_DRIVER */
406 	    vdp->xdf_vd_lbl, NULL) != 0) {
407 		cmn_err(CE_WARN, "xdf@%s: default cmlb attach failed",
408 		    ddi_get_name_addr(devi));
409 		goto errout0;
410 	}
411 
412 	/*
413 	 * We ship with cache-enabled disks
414 	 */
415 	vdp->xdf_wce = 1;
416 
417 	mutex_enter(&vdp->xdf_cb_lk);
418 
419 	/* Watch backend XenbusState change */
420 	if (xvdi_add_event_handler(devi, XS_OE_STATE,
421 	    xdf_oe_change) != DDI_SUCCESS) {
422 		mutex_exit(&vdp->xdf_cb_lk);
423 		goto errout0;
424 	}
425 
426 	if (xdf_start_connect(vdp) != DDI_SUCCESS) {
427 		cmn_err(CE_WARN, "xdf@%s: start connection failed",
428 		    ddi_get_name_addr(devi));
429 		(void) xdf_start_disconnect(vdp);
430 		mutex_exit(&vdp->xdf_cb_lk);
431 		goto errout1;
432 	}
433 
434 	mutex_exit(&vdp->xdf_cb_lk);
435 
436 	list_create(&vdp->xdf_vreq_act, sizeof (v_req_t),
437 	    offsetof(v_req_t, v_link));
438 	list_create(&vdp->xdf_gs_act, sizeof (ge_slot_t),
439 	    offsetof(ge_slot_t, link));
440 
441 #if defined(XPV_HVM_DRIVER)
442 	xdf_hvm_add(devi);
443 
444 	(void) ddi_prop_update_int(DDI_DEV_T_NONE, devi, DDI_NO_AUTODETACH, 1);
445 
446 	/*
447 	 * Report our version to dom0.
448 	 */
449 	if (xenbus_printf(XBT_NULL, "hvmpv/xdf", "version", "%d",
450 	    HVMPV_XDF_VERS))
451 		cmn_err(CE_WARN, "xdf: couldn't write version\n");
452 #endif /* XPV_HVM_DRIVER */
453 
454 	ddi_report_dev(devi);
455 
456 	DPRINTF(DDI_DBG, ("xdf%d: attached\n", instance));
457 
458 	return (DDI_SUCCESS);
459 
460 errout1:
461 	xvdi_remove_event_handler(devi, XS_OE_STATE);
462 errout0:
463 	if (vdp->xdf_vd_lbl != NULL) {
464 		cmlb_detach(vdp->xdf_vd_lbl, NULL);
465 		cmlb_free_handle(&vdp->xdf_vd_lbl);
466 		vdp->xdf_vd_lbl = NULL;
467 	}
468 #if !defined(XPV_HVM_DRIVER)
469 	xdf_kstat_delete(devi);
470 #endif /* !XPV_HVM_DRIVER */
471 	if (vdp->xdf_softintr_id != NULL)
472 		ddi_remove_softintr(vdp->xdf_softintr_id);
473 	if (vdp->xdf_ibc != NULL) {
474 		mutex_destroy(&vdp->xdf_cb_lk);
475 		mutex_destroy(&vdp->xdf_dev_lk);
476 	}
477 	cv_destroy(&vdp->xdf_dev_cv);
478 	ddi_soft_state_free(vbd_ss, instance);
479 	ddi_set_driver_private(devi, NULL);
480 	ddi_prop_remove_all(devi);
481 	cmn_err(CE_WARN, "xdf@%s: attach failed", ddi_get_name_addr(devi));
482 	return (DDI_FAILURE);
483 }
484 
485 static int
486 xdf_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
487 {
488 	xdf_t *vdp;
489 	int instance;
490 
491 	switch (cmd) {
492 
493 	case DDI_PM_SUSPEND:
494 		break;
495 
496 	case DDI_SUSPEND:
497 		return (xdf_suspend(devi));
498 
499 	case DDI_DETACH:
500 		break;
501 
502 	default:
503 		return (DDI_FAILURE);
504 	}
505 
506 	instance = ddi_get_instance(devi);
507 	DPRINTF(DDI_DBG, ("xdf%d: detaching\n", instance));
508 	vdp = ddi_get_soft_state(vbd_ss, instance);
509 
510 	if (vdp == NULL)
511 		return (DDI_FAILURE);
512 
513 	mutex_enter(&vdp->xdf_dev_lk);
514 	if (xdf_isopen(vdp, -1)) {
515 		mutex_exit(&vdp->xdf_dev_lk);
516 		return (DDI_FAILURE);
517 	}
518 
519 	if (vdp->xdf_status != XD_CLOSED) {
520 		mutex_exit(&vdp->xdf_dev_lk);
521 		return (DDI_FAILURE);
522 	}
523 
524 #if defined(XPV_HVM_DRIVER)
525 	xdf_hvm_rm(devi);
526 #endif /* XPV_HVM_DRIVER */
527 
528 	ASSERT(!ISDMACBON(vdp));
529 	mutex_exit(&vdp->xdf_dev_lk);
530 
531 	if (vdp->xdf_timeout_id != 0)
532 		(void) untimeout(vdp->xdf_timeout_id);
533 
534 	xvdi_remove_event_handler(devi, XS_OE_STATE);
535 
536 	/* we'll support backend running in domU later */
537 #ifdef	DOMU_BACKEND
538 	(void) xvdi_post_event(devi, XEN_HP_REMOVE);
539 #endif
540 
541 	list_destroy(&vdp->xdf_vreq_act);
542 	list_destroy(&vdp->xdf_gs_act);
543 	ddi_prop_remove_all(devi);
544 	xdf_kstat_delete(devi);
545 	ddi_remove_softintr(vdp->xdf_softintr_id);
546 	ddi_set_driver_private(devi, NULL);
547 	cv_destroy(&vdp->xdf_dev_cv);
548 	mutex_destroy(&vdp->xdf_cb_lk);
549 	mutex_destroy(&vdp->xdf_dev_lk);
550 	if (vdp->xdf_cache_flush_block != NULL)
551 		kmem_free(vdp->xdf_flush_mem, 2 * DEV_BSIZE);
552 	ddi_soft_state_free(vbd_ss, instance);
553 	return (DDI_SUCCESS);
554 }
555 
556 static int
557 xdf_suspend(dev_info_t *devi)
558 {
559 	xdf_t *vdp;
560 	int instance;
561 	enum xdf_state st;
562 
563 	instance = ddi_get_instance(devi);
564 
565 	if (xdfdebug & SUSRES_DBG)
566 		xen_printf("xdf_suspend: xdf#%d\n", instance);
567 
568 	if ((vdp = ddi_get_soft_state(vbd_ss, instance)) == NULL)
569 		return (DDI_FAILURE);
570 
571 	xvdi_suspend(devi);
572 
573 	mutex_enter(&vdp->xdf_cb_lk);
574 	mutex_enter(&vdp->xdf_dev_lk);
575 	st = vdp->xdf_status;
576 	/* change status to stop further I/O requests */
577 	if (st == XD_READY)
578 		vdp->xdf_status = XD_SUSPEND;
579 	mutex_exit(&vdp->xdf_dev_lk);
580 	mutex_exit(&vdp->xdf_cb_lk);
581 
582 	/* make sure no more I/O responses left in the ring buffer */
583 	if ((st == XD_INIT) || (st == XD_READY)) {
584 #ifdef XPV_HVM_DRIVER
585 		ec_unbind_evtchn(vdp->xdf_evtchn);
586 		xvdi_free_evtchn(devi);
587 #else /* !XPV_HVM_DRIVER */
588 		(void) ddi_remove_intr(devi, 0, NULL);
589 #endif /* !XPV_HVM_DRIVER */
590 		(void) xdf_drain_io(vdp);
591 		/*
592 		 * no need to teardown the ring buffer here
593 		 * it will be simply re-init'ed during resume when
594 		 * we call xvdi_alloc_ring
595 		 */
596 	}
597 
598 	if (xdfdebug & SUSRES_DBG)
599 		xen_printf("xdf_suspend: SUCCESS\n");
600 
601 	return (DDI_SUCCESS);
602 }
603 
604 /*ARGSUSED*/
605 static int
606 xdf_resume(dev_info_t *devi)
607 {
608 	xdf_t *vdp;
609 	int instance;
610 
611 	instance = ddi_get_instance(devi);
612 	if (xdfdebug & SUSRES_DBG)
613 		xen_printf("xdf_resume: xdf%d\n", instance);
614 
615 	if ((vdp = ddi_get_soft_state(vbd_ss, instance)) == NULL)
616 		return (DDI_FAILURE);
617 
618 	mutex_enter(&vdp->xdf_cb_lk);
619 
620 	if (xvdi_resume(devi) != DDI_SUCCESS) {
621 		mutex_exit(&vdp->xdf_cb_lk);
622 		return (DDI_FAILURE);
623 	}
624 
625 	mutex_enter(&vdp->xdf_dev_lk);
626 	ASSERT(vdp->xdf_status != XD_READY);
627 	vdp->xdf_status = XD_UNKNOWN;
628 	mutex_exit(&vdp->xdf_dev_lk);
629 
630 	if (xdf_start_connect(vdp) != DDI_SUCCESS) {
631 		mutex_exit(&vdp->xdf_cb_lk);
632 		return (DDI_FAILURE);
633 	}
634 
635 	mutex_exit(&vdp->xdf_cb_lk);
636 
637 	if (xdfdebug & SUSRES_DBG)
638 		xen_printf("xdf_resume: done\n");
639 	return (DDI_SUCCESS);
640 }
641 
642 /*ARGSUSED*/
643 static int
644 xdf_reset(dev_info_t *devi, ddi_reset_cmd_t cmd)
645 {
646 	xdf_t *vdp;
647 	int instance;
648 
649 	instance = ddi_get_instance(devi);
650 	DPRINTF(DDI_DBG, ("xdf%d: resetting\n", instance));
651 	if ((vdp = ddi_get_soft_state(vbd_ss, instance)) == NULL)
652 		return (DDI_FAILURE);
653 
654 	/*
655 	 * wait for any outstanding I/O to complete
656 	 */
657 	(void) xdf_drain_io(vdp);
658 
659 	DPRINTF(DDI_DBG, ("xdf%d: reset complete\n", instance));
660 	return (DDI_SUCCESS);
661 }
662 
663 static int
664 xdf_open(dev_t *devp, int flag, int otyp, cred_t *credp)
665 {
666 	minor_t	minor;
667 	xdf_t	*vdp;
668 	int part;
669 	ulong_t parbit;
670 	diskaddr_t p_blkct = 0;
671 	boolean_t firstopen;
672 	boolean_t nodelay;
673 
674 	minor = getminor(*devp);
675 	if ((vdp = ddi_get_soft_state(vbd_ss, XDF_INST(minor))) == NULL)
676 		return (ENXIO);
677 
678 	nodelay = (flag & (FNDELAY | FNONBLOCK));
679 
680 	DPRINTF(DDI_DBG, ("xdf%d: opening\n", XDF_INST(minor)));
681 
682 	/* do cv_wait until connected or failed */
683 	mutex_enter(&vdp->xdf_dev_lk);
684 	if (!nodelay && (xdf_connect(vdp, B_TRUE) != XD_READY)) {
685 		mutex_exit(&vdp->xdf_dev_lk);
686 		return (ENXIO);
687 	}
688 
689 	if ((flag & FWRITE) && XD_IS_RO(vdp)) {
690 		mutex_exit(&vdp->xdf_dev_lk);
691 		return (EROFS);
692 	}
693 
694 	part = XDF_PART(minor);
695 	parbit = 1 << part;
696 	if ((vdp->xdf_vd_exclopen & parbit) ||
697 	    ((flag & FEXCL) && xdf_isopen(vdp, part))) {
698 		mutex_exit(&vdp->xdf_dev_lk);
699 		return (EBUSY);
700 	}
701 
702 	/* are we the first one to open this node? */
703 	firstopen = !xdf_isopen(vdp, -1);
704 
705 	if (otyp == OTYP_LYR)
706 		vdp->xdf_vd_lyropen[part]++;
707 
708 	vdp->xdf_vd_open[otyp] |= parbit;
709 
710 	if (flag & FEXCL)
711 		vdp->xdf_vd_exclopen |= parbit;
712 
713 	mutex_exit(&vdp->xdf_dev_lk);
714 
715 	/* force a re-validation */
716 	if (firstopen)
717 		cmlb_invalidate(vdp->xdf_vd_lbl, NULL);
718 
719 	/*
720 	 * check size
721 	 * ignore CD/DVD which contains a zero-sized s0
722 	 */
723 	if (!nodelay && !XD_IS_CD(vdp) &&
724 	    ((cmlb_partinfo(vdp->xdf_vd_lbl, part, &p_blkct,
725 	    NULL, NULL, NULL, NULL) != 0) || (p_blkct == 0))) {
726 		(void) xdf_close(*devp, flag, otyp, credp);
727 		return (ENXIO);
728 	}
729 
730 	return (0);
731 }
732 
733 /*ARGSUSED*/
734 static int
735 xdf_close(dev_t dev, int flag, int otyp, struct cred *credp)
736 {
737 	minor_t	minor;
738 	xdf_t	*vdp;
739 	int part;
740 	ulong_t parbit;
741 
742 	minor = getminor(dev);
743 	if ((vdp = ddi_get_soft_state(vbd_ss, XDF_INST(minor))) == NULL)
744 		return (ENXIO);
745 
746 	mutex_enter(&vdp->xdf_dev_lk);
747 	part = XDF_PART(minor);
748 	if (!xdf_isopen(vdp, part)) {
749 		mutex_exit(&vdp->xdf_dev_lk);
750 		return (ENXIO);
751 	}
752 	parbit = 1 << part;
753 
754 	ASSERT((vdp->xdf_vd_open[otyp] & parbit) != 0);
755 	if (otyp == OTYP_LYR) {
756 		ASSERT(vdp->xdf_vd_lyropen[part] > 0);
757 		if (--vdp->xdf_vd_lyropen[part] == 0)
758 			vdp->xdf_vd_open[otyp] &= ~parbit;
759 	} else {
760 		vdp->xdf_vd_open[otyp] &= ~parbit;
761 	}
762 	vdp->xdf_vd_exclopen &= ~parbit;
763 
764 	mutex_exit(&vdp->xdf_dev_lk);
765 	return (0);
766 }
767 
768 static int
769 xdf_strategy(struct buf *bp)
770 {
771 	xdf_t	*vdp;
772 	minor_t minor;
773 	diskaddr_t p_blkct, p_blkst;
774 	ulong_t nblks;
775 	int part;
776 
777 	minor = getminor(bp->b_edev);
778 	part = XDF_PART(minor);
779 
780 	vdp = ddi_get_soft_state(vbd_ss, XDF_INST(minor));
781 	if ((vdp == NULL) || !xdf_isopen(vdp, part)) {
782 		bioerror(bp, ENXIO);
783 		bp->b_resid = bp->b_bcount;
784 		biodone(bp);
785 		return (0);
786 	}
787 
788 	/* Check for writes to a read only device */
789 	if (!IS_READ(bp) && XD_IS_RO(vdp)) {
790 		bioerror(bp, EROFS);
791 		bp->b_resid = bp->b_bcount;
792 		biodone(bp);
793 		return (0);
794 	}
795 
796 	/* Check if this I/O is accessing a partition or the entire disk */
797 	if ((long)bp->b_private == XB_SLICE_NONE) {
798 		/* This I/O is using an absolute offset */
799 		p_blkct = vdp->xdf_xdev_nblocks;
800 		p_blkst = 0;
801 	} else {
802 		/* This I/O is using a partition relative offset */
803 		if (cmlb_partinfo(vdp->xdf_vd_lbl, part, &p_blkct,
804 		    &p_blkst, NULL, NULL, NULL)) {
805 			bioerror(bp, ENXIO);
806 			bp->b_resid = bp->b_bcount;
807 			biodone(bp);
808 			return (0);
809 		}
810 	}
811 
812 	/* check for a starting block beyond the disk or partition limit */
813 	if (bp->b_blkno > p_blkct) {
814 		DPRINTF(IO_DBG, ("xdf: block %lld exceeds VBD size %"PRIu64,
815 		    (longlong_t)bp->b_blkno, (uint64_t)p_blkct));
816 		bioerror(bp, EINVAL);
817 		bp->b_resid = bp->b_bcount;
818 		biodone(bp);
819 		return (0);
820 	}
821 
822 	/* Legacy: don't set error flag at this case */
823 	if (bp->b_blkno == p_blkct) {
824 		bp->b_resid = bp->b_bcount;
825 		biodone(bp);
826 		return (0);
827 	}
828 
829 	/* Adjust for partial transfer */
830 	nblks = bp->b_bcount >> XB_BSHIFT;
831 	if ((bp->b_blkno + nblks) > p_blkct) {
832 		bp->b_resid = ((bp->b_blkno + nblks) - p_blkct) << XB_BSHIFT;
833 		bp->b_bcount -= bp->b_resid;
834 	}
835 
836 	DPRINTF(IO_DBG, ("xdf: strategy blk %lld len %lu\n",
837 	    (longlong_t)bp->b_blkno, (ulong_t)bp->b_bcount));
838 
839 	/* Fix up the buf struct */
840 	bp->b_flags |= B_BUSY;
841 	bp->av_forw = bp->av_back = NULL; /* not tagged with a v_req */
842 	bp->b_private = (void *)(uintptr_t)p_blkst;
843 
844 	mutex_enter(&vdp->xdf_dev_lk);
845 	if (vdp->xdf_xdev_iostat != NULL)
846 		kstat_waitq_enter(KSTAT_IO_PTR(vdp->xdf_xdev_iostat));
847 	if (vdp->xdf_f_act == NULL) {
848 		vdp->xdf_f_act = vdp->xdf_l_act = bp;
849 	} else {
850 		vdp->xdf_l_act->av_forw = bp;
851 		vdp->xdf_l_act = bp;
852 	}
853 	mutex_exit(&vdp->xdf_dev_lk);
854 
855 	xdf_iostart(vdp);
856 	if (do_polled_io)
857 		(void) xdf_drain_io(vdp);
858 	return (0);
859 }
860 
861 /*ARGSUSED*/
862 static int
863 xdf_read(dev_t dev, struct uio *uiop, cred_t *credp)
864 {
865 
866 	xdf_t	*vdp;
867 	minor_t minor;
868 	diskaddr_t p_blkcnt;
869 	int part;
870 
871 	minor = getminor(dev);
872 	if ((vdp = ddi_get_soft_state(vbd_ss, XDF_INST(minor))) == NULL)
873 		return (ENXIO);
874 
875 	DPRINTF(IO_DBG, ("xdf: read offset 0x%"PRIx64"\n",
876 	    (int64_t)uiop->uio_offset));
877 
878 	part = XDF_PART(minor);
879 	if (!xdf_isopen(vdp, part))
880 		return (ENXIO);
881 
882 	if (cmlb_partinfo(vdp->xdf_vd_lbl, part, &p_blkcnt,
883 	    NULL, NULL, NULL, NULL))
884 		return (ENXIO);
885 
886 	if (U_INVAL(uiop))
887 		return (EINVAL);
888 
889 	return (physio(xdf_strategy, NULL, dev, B_READ, xdfmin, uiop));
890 }
891 
892 /*ARGSUSED*/
893 static int
894 xdf_write(dev_t dev, struct uio *uiop, cred_t *credp)
895 {
896 	xdf_t *vdp;
897 	minor_t minor;
898 	diskaddr_t p_blkcnt;
899 	int part;
900 
901 	minor = getminor(dev);
902 	if ((vdp = ddi_get_soft_state(vbd_ss, XDF_INST(minor))) == NULL)
903 		return (ENXIO);
904 
905 	DPRINTF(IO_DBG, ("xdf: write offset 0x%"PRIx64"\n",
906 	    (int64_t)uiop->uio_offset));
907 
908 	part = XDF_PART(minor);
909 	if (!xdf_isopen(vdp, part))
910 		return (ENXIO);
911 
912 	if (cmlb_partinfo(vdp->xdf_vd_lbl, part, &p_blkcnt,
913 	    NULL, NULL, NULL, NULL))
914 		return (ENXIO);
915 
916 	if (uiop->uio_loffset >= XB_DTOB(p_blkcnt))
917 		return (ENOSPC);
918 
919 	if (U_INVAL(uiop))
920 		return (EINVAL);
921 
922 	return (physio(xdf_strategy, NULL, dev, B_WRITE, minphys, uiop));
923 }
924 
925 /*ARGSUSED*/
926 static int
927 xdf_aread(dev_t dev, struct aio_req *aiop, cred_t *credp)
928 {
929 	xdf_t	*vdp;
930 	minor_t minor;
931 	struct uio *uiop = aiop->aio_uio;
932 	diskaddr_t p_blkcnt;
933 	int part;
934 
935 	minor = getminor(dev);
936 	if ((vdp = ddi_get_soft_state(vbd_ss, XDF_INST(minor))) == NULL)
937 		return (ENXIO);
938 
939 	part = XDF_PART(minor);
940 	if (!xdf_isopen(vdp, part))
941 		return (ENXIO);
942 
943 	if (cmlb_partinfo(vdp->xdf_vd_lbl, part, &p_blkcnt,
944 	    NULL, NULL, NULL, NULL))
945 		return (ENXIO);
946 
947 	if (uiop->uio_loffset >= XB_DTOB(p_blkcnt))
948 		return (ENOSPC);
949 
950 	if (U_INVAL(uiop))
951 		return (EINVAL);
952 
953 	return (aphysio(xdf_strategy, anocancel, dev, B_READ, minphys, aiop));
954 }
955 
956 /*ARGSUSED*/
957 static int
958 xdf_awrite(dev_t dev, struct aio_req *aiop, cred_t *credp)
959 {
960 	xdf_t *vdp;
961 	minor_t minor;
962 	struct uio *uiop = aiop->aio_uio;
963 	diskaddr_t p_blkcnt;
964 	int part;
965 
966 	minor = getminor(dev);
967 	if ((vdp = ddi_get_soft_state(vbd_ss, XDF_INST(minor))) == NULL)
968 		return (ENXIO);
969 
970 	part = XDF_PART(minor);
971 	if (!xdf_isopen(vdp, part))
972 		return (ENXIO);
973 
974 	if (cmlb_partinfo(vdp->xdf_vd_lbl, part, &p_blkcnt,
975 	    NULL, NULL, NULL, NULL))
976 		return (ENXIO);
977 
978 	if (uiop->uio_loffset >= XB_DTOB(p_blkcnt))
979 		return (ENOSPC);
980 
981 	if (U_INVAL(uiop))
982 		return (EINVAL);
983 
984 	return (aphysio(xdf_strategy, anocancel, dev, B_WRITE, minphys, aiop));
985 }
986 
987 static int
988 xdf_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
989 {
990 	struct buf dumpbuf, *dbp;
991 	xdf_t	*vdp;
992 	minor_t minor;
993 	int err = 0;
994 	int part;
995 	diskaddr_t p_blkcnt, p_blkst;
996 
997 	minor = getminor(dev);
998 	if ((vdp = ddi_get_soft_state(vbd_ss, XDF_INST(minor))) == NULL)
999 		return (ENXIO);
1000 
1001 	DPRINTF(IO_DBG, ("xdf: dump addr (0x%p) blk (%ld) nblks (%d)\n",
1002 	    (void *)addr, blkno, nblk));
1003 
1004 	part = XDF_PART(minor);
1005 	if (!xdf_isopen(vdp, part))
1006 		return (ENXIO);
1007 
1008 	if (cmlb_partinfo(vdp->xdf_vd_lbl, part, &p_blkcnt, &p_blkst,
1009 	    NULL, NULL, NULL))
1010 		return (ENXIO);
1011 
1012 	if ((blkno + nblk) > p_blkcnt) {
1013 		cmn_err(CE_WARN, "xdf: block %ld exceeds VBD size %"PRIu64,
1014 		    blkno + nblk, (uint64_t)p_blkcnt);
1015 		return (EINVAL);
1016 	}
1017 
1018 	dbp = &dumpbuf;
1019 	bioinit(dbp);
1020 	dbp->b_flags = B_BUSY;
1021 	dbp->b_un.b_addr = addr;
1022 	dbp->b_bcount = nblk << DEV_BSHIFT;
1023 	dbp->b_blkno = blkno;
1024 	dbp->b_edev = dev;
1025 	dbp->b_private = (void *)(uintptr_t)p_blkst;
1026 
1027 	mutex_enter(&vdp->xdf_dev_lk);
1028 	if (vdp->xdf_xdev_iostat != NULL)
1029 		kstat_waitq_enter(KSTAT_IO_PTR(vdp->xdf_xdev_iostat));
1030 	if (vdp->xdf_f_act == NULL) {
1031 		vdp->xdf_f_act = vdp->xdf_l_act = dbp;
1032 	} else {
1033 		vdp->xdf_l_act->av_forw = dbp;
1034 		vdp->xdf_l_act = dbp;
1035 	}
1036 	dbp->av_forw = NULL;
1037 	dbp->av_back = NULL;
1038 	mutex_exit(&vdp->xdf_dev_lk);
1039 	xdf_iostart(vdp);
1040 	err = xdf_drain_io(vdp);
1041 	biofini(dbp);
1042 	return (err);
1043 }
1044 
1045 /*ARGSUSED*/
1046 static int
1047 xdf_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1048     int *rvalp)
1049 {
1050 	int instance;
1051 	xdf_t	*vdp;
1052 	minor_t minor;
1053 	int part;
1054 
1055 	minor = getminor(dev);
1056 	instance = XDF_INST(minor);
1057 
1058 	if ((vdp = ddi_get_soft_state(vbd_ss, instance)) == NULL)
1059 		return (ENXIO);
1060 
1061 	DPRINTF(IOCTL_DBG, ("xdf%d:ioctl: cmd %d (0x%x)\n",
1062 	    instance, cmd, cmd));
1063 
1064 	part = XDF_PART(minor);
1065 	if (!xdf_isopen(vdp, part))
1066 		return (ENXIO);
1067 
1068 	switch (cmd) {
1069 	case DKIOCGMEDIAINFO: {
1070 		struct dk_minfo	media_info;
1071 
1072 		media_info.dki_lbsize = DEV_BSIZE;
1073 		media_info.dki_capacity = vdp->xdf_pgeom.g_capacity;
1074 		media_info.dki_media_type = DK_FIXED_DISK;
1075 
1076 		if (ddi_copyout(&media_info, (void *)arg,
1077 		    sizeof (struct dk_minfo), mode)) {
1078 			return (EFAULT);
1079 		} else {
1080 			return (0);
1081 		}
1082 	}
1083 
1084 	case DKIOCINFO: {
1085 		struct dk_cinfo info;
1086 
1087 		/* controller information */
1088 		if (XD_IS_CD(vdp))
1089 			info.dki_ctype = DKC_CDROM;
1090 		else
1091 			info.dki_ctype = DKC_VBD;
1092 
1093 		info.dki_cnum = 0;
1094 		(void) strncpy((char *)(&info.dki_cname), "xdf", 8);
1095 
1096 		/* unit information */
1097 		info.dki_unit = ddi_get_instance(vdp->xdf_dip);
1098 		(void) strncpy((char *)(&info.dki_dname), "xdf", 8);
1099 		info.dki_flags = DKI_FMTVOL;
1100 		info.dki_partition = part;
1101 		info.dki_maxtransfer = maxphys / DEV_BSIZE;
1102 		info.dki_addr = 0;
1103 		info.dki_space = 0;
1104 		info.dki_prio = 0;
1105 		info.dki_vec = 0;
1106 
1107 		if (ddi_copyout(&info, (void *)arg, sizeof (info), mode))
1108 			return (EFAULT);
1109 		else
1110 			return (0);
1111 	}
1112 
1113 	case DKIOCSTATE: {
1114 		enum dkio_state	dkstate = DKIO_INSERTED;
1115 		if (ddi_copyout(&dkstate, (void *)arg, sizeof (dkstate),
1116 		    mode) != 0)
1117 			return (EFAULT);
1118 		return (0);
1119 	}
1120 
1121 	/*
1122 	 * is media removable?
1123 	 */
1124 	case DKIOCREMOVABLE: {
1125 		int i = XD_IS_RM(vdp) ? 1 : 0;
1126 		if (ddi_copyout(&i, (caddr_t)arg, sizeof (int), mode))
1127 			return (EFAULT);
1128 		return (0);
1129 	}
1130 
1131 	case DKIOCG_PHYGEOM:
1132 	case DKIOCG_VIRTGEOM:
1133 	case DKIOCGGEOM:
1134 	case DKIOCSGEOM:
1135 	case DKIOCGAPART:
1136 	case DKIOCSAPART:
1137 	case DKIOCGVTOC:
1138 	case DKIOCSVTOC:
1139 	case DKIOCPARTINFO:
1140 	case DKIOCGEXTVTOC:
1141 	case DKIOCSEXTVTOC:
1142 	case DKIOCEXTPARTINFO:
1143 	case DKIOCGMBOOT:
1144 	case DKIOCSMBOOT:
1145 	case DKIOCGETEFI:
1146 	case DKIOCSETEFI:
1147 	case DKIOCPARTITION: {
1148 		int rc;
1149 
1150 		rc = cmlb_ioctl(vdp->xdf_vd_lbl, dev, cmd, arg, mode, credp,
1151 		    rvalp, NULL);
1152 		return (rc);
1153 	}
1154 
1155 	case DKIOCGETWCE:
1156 		if (ddi_copyout(&vdp->xdf_wce, (void *)arg,
1157 		    sizeof (vdp->xdf_wce), mode))
1158 			return (EFAULT);
1159 		return (0);
1160 	case DKIOCSETWCE:
1161 		if (ddi_copyin((void *)arg, &vdp->xdf_wce,
1162 		    sizeof (vdp->xdf_wce), mode))
1163 			return (EFAULT);
1164 		return (0);
1165 	case DKIOCFLUSHWRITECACHE: {
1166 		int rc;
1167 		struct dk_callback *dkc = (struct dk_callback *)arg;
1168 
1169 		if (vdp->xdf_flush_supported) {
1170 			rc = xdf_lb_rdwr(vdp->xdf_dip, TG_WRITE,
1171 			    NULL, 0, 0, (void *)dev);
1172 		} else if (vdp->xdf_feature_barrier &&
1173 		    !xdf_barrier_flush_disable) {
1174 			rc = xdf_lb_rdwr(vdp->xdf_dip, TG_WRITE,
1175 			    vdp->xdf_cache_flush_block, xdf_flush_block,
1176 			    DEV_BSIZE, (void *)dev);
1177 		} else {
1178 			return (ENOTTY);
1179 		}
1180 		if ((mode & FKIOCTL) && (dkc != NULL) &&
1181 		    (dkc->dkc_callback != NULL)) {
1182 			(*dkc->dkc_callback)(dkc->dkc_cookie, rc);
1183 			/* need to return 0 after calling callback */
1184 			rc = 0;
1185 		}
1186 		return (rc);
1187 	}
1188 
1189 	default:
1190 		return (ENOTTY);
1191 	}
1192 }
1193 
1194 /*
1195  * xdf interrupt handler
1196  */
1197 static uint_t
1198 xdf_intr(caddr_t arg)
1199 {
1200 	xdf_t *vdp = (xdf_t *)arg;
1201 	xendev_ring_t *xbr;
1202 	blkif_response_t *resp;
1203 	int bioerr;
1204 	uint64_t id;
1205 	extern int do_polled_io;
1206 	uint8_t op;
1207 	uint16_t status;
1208 	ddi_acc_handle_t acchdl;
1209 
1210 	mutex_enter(&vdp->xdf_dev_lk);
1211 
1212 	if ((xbr = vdp->xdf_xb_ring) == NULL) {
1213 		mutex_exit(&vdp->xdf_dev_lk);
1214 		return (DDI_INTR_UNCLAIMED);
1215 	}
1216 
1217 	acchdl = vdp->xdf_xb_ring_hdl;
1218 
1219 	/*
1220 	 * complete all requests which have a response
1221 	 */
1222 	while (resp = xvdi_ring_get_response(xbr)) {
1223 		id = ddi_get64(acchdl, &resp->id);
1224 		op = ddi_get8(acchdl, &resp->operation);
1225 		status = ddi_get16(acchdl, (uint16_t *)&resp->status);
1226 		DPRINTF(INTR_DBG, ("resp: op %d id %"PRIu64" status %d\n",
1227 		    op, id, status));
1228 
1229 		/*
1230 		 * XXPV - close connection to the backend and restart
1231 		 */
1232 		if (status != BLKIF_RSP_OKAY) {
1233 			DPRINTF(IO_DBG, ("xdf@%s: I/O error while %s",
1234 			    ddi_get_name_addr(vdp->xdf_dip),
1235 			    (op == BLKIF_OP_READ) ? "reading" : "writing"));
1236 			bioerr = EIO;
1237 		} else {
1238 			bioerr = 0;
1239 		}
1240 
1241 		xdf_iofini(vdp, id, bioerr);
1242 	}
1243 
1244 	mutex_exit(&vdp->xdf_dev_lk);
1245 
1246 	if (!do_polled_io)
1247 		xdf_iostart(vdp);
1248 
1249 	return (DDI_INTR_CLAIMED);
1250 }
1251 
1252 int xdf_fbrewrites;	/* how many times was our flush block rewritten */
1253 
1254 /*
1255  * Snarf new data if our flush block was re-written
1256  */
1257 static void
1258 check_fbwrite(xdf_t *vdp, buf_t *bp, daddr_t blkno)
1259 {
1260 	int nblks;
1261 	boolean_t mapin;
1262 
1263 	if (IS_WRITE_BARRIER(vdp, bp))
1264 		return; /* write was a flush write */
1265 
1266 	mapin = B_FALSE;
1267 	nblks = bp->b_bcount >> DEV_BSHIFT;
1268 	if (xdf_flush_block >= blkno && xdf_flush_block < (blkno + nblks)) {
1269 		xdf_fbrewrites++;
1270 		if (bp->b_flags & (B_PAGEIO | B_PHYS)) {
1271 			mapin = B_TRUE;
1272 			bp_mapin(bp);
1273 		}
1274 		bcopy(bp->b_un.b_addr +
1275 		    ((xdf_flush_block - blkno) << DEV_BSHIFT),
1276 		    vdp->xdf_cache_flush_block, DEV_BSIZE);
1277 		if (mapin)
1278 			bp_mapout(bp);
1279 	}
1280 }
1281 
1282 static void
1283 xdf_iofini(xdf_t *vdp, uint64_t id, int bioerr)
1284 {
1285 	ge_slot_t *gs = (ge_slot_t *)(uintptr_t)id;
1286 	v_req_t *vreq = gs->vreq;
1287 	buf_t *bp = vreq->v_buf;
1288 
1289 	gs_free(vdp, gs);
1290 	if (bioerr)
1291 		bioerror(bp, bioerr);
1292 	vreq->v_nslots--;
1293 	if (vreq->v_nslots != 0)
1294 		return;
1295 
1296 	XDF_UPDATE_IO_STAT(vdp, bp);
1297 	if (vdp->xdf_xdev_iostat != NULL)
1298 		kstat_runq_exit(KSTAT_IO_PTR(vdp->xdf_xdev_iostat));
1299 
1300 	if (IS_ERROR(bp))
1301 		bp->b_resid = bp->b_bcount;
1302 
1303 	vreq_free(vdp, vreq);
1304 	biodone(bp);
1305 }
1306 
1307 /*
1308  * return value of xdf_prepare_rreq()
1309  * used in xdf_iostart()
1310  */
1311 #define	XF_PARTIAL	0 /* rreq is full, not all I/O in buf transferred */
1312 #define	XF_COMP		1 /* no more I/O left in buf */
1313 
1314 static void
1315 xdf_iostart(xdf_t *vdp)
1316 {
1317 	xendev_ring_t *xbr;
1318 	struct buf *bp;
1319 	blkif_request_t *rreq;
1320 	int retval;
1321 	int rreqready = 0;
1322 
1323 	xbr = vdp->xdf_xb_ring;
1324 
1325 	/*
1326 	 * populate the ring request(s)
1327 	 *
1328 	 * loop until there is no buf to transfer or no free slot
1329 	 * available in I/O ring
1330 	 */
1331 	mutex_enter(&vdp->xdf_dev_lk);
1332 
1333 	for (;;) {
1334 		if (vdp->xdf_status != XD_READY)
1335 			break;
1336 
1337 		/* active buf queue empty? */
1338 		if ((bp = vdp->xdf_f_act) == NULL)
1339 			break;
1340 
1341 		/* try to grab a vreq for this bp */
1342 		if ((BP2VREQ(bp) == NULL) && (vreq_get(vdp, bp) == NULL))
1343 				break;
1344 		/* alloc DMA/GTE resources */
1345 		if (vreq_setup(vdp, BP2VREQ(bp)) != DDI_SUCCESS)
1346 			break;
1347 
1348 		/* get next blkif_request in the ring */
1349 		if ((rreq = xvdi_ring_get_request(xbr)) == NULL)
1350 			break;
1351 		bzero(rreq, sizeof (blkif_request_t));
1352 
1353 		/* populate blkif_request with this buf */
1354 		rreqready++;
1355 		retval = xdf_prepare_rreq(vdp, bp, rreq);
1356 		if (retval == XF_COMP) {
1357 			/* finish this bp, switch to next one */
1358 			if (vdp->xdf_xdev_iostat != NULL)
1359 				kstat_waitq_to_runq(
1360 				    KSTAT_IO_PTR(vdp->xdf_xdev_iostat));
1361 			vdp->xdf_f_act = bp->av_forw;
1362 			bp->av_forw = NULL;
1363 		}
1364 	}
1365 
1366 	/*
1367 	 * Send the request(s) to the backend
1368 	 */
1369 	if (rreqready) {
1370 		if (xvdi_ring_push_request(xbr)) {
1371 			DPRINTF(IO_DBG, ("xdf_iostart: "
1372 			    "sent request(s) to backend\n"));
1373 			xvdi_notify_oe(vdp->xdf_dip);
1374 		}
1375 	}
1376 
1377 	mutex_exit(&vdp->xdf_dev_lk);
1378 }
1379 
1380 /*
1381  * populate a single blkif_request_t w/ a buf
1382  */
1383 static int
1384 xdf_prepare_rreq(xdf_t *vdp, struct buf *bp, blkif_request_t *rreq)
1385 {
1386 	int		rval;
1387 	grant_ref_t	gr;
1388 	uint8_t		fsect, lsect;
1389 	size_t		bcnt;
1390 	paddr_t		dma_addr;
1391 	off_t		blk_off;
1392 	dev_info_t	*dip = vdp->xdf_dip;
1393 	blkif_vdev_t	vdev = xvdi_get_vdevnum(dip);
1394 	v_req_t		*vreq = BP2VREQ(bp);
1395 	uint64_t	blkno = vreq->v_blkno;
1396 	uint_t		ndmacs = vreq->v_ndmacs;
1397 	ddi_acc_handle_t acchdl = vdp->xdf_xb_ring_hdl;
1398 	int		seg = 0;
1399 	int		isread = IS_READ(bp);
1400 
1401 	if (isread)
1402 		ddi_put8(acchdl, &rreq->operation, BLKIF_OP_READ);
1403 	else {
1404 		switch (vreq->v_flush_diskcache) {
1405 		case FLUSH_DISKCACHE:
1406 			ddi_put8(acchdl, &rreq->operation,
1407 			    BLKIF_OP_FLUSH_DISKCACHE);
1408 			ddi_put16(acchdl, &rreq->handle, vdev);
1409 			ddi_put64(acchdl, &rreq->id,
1410 			    (uint64_t)(uintptr_t)(vreq->v_gs));
1411 			ddi_put8(acchdl, &rreq->nr_segments, 0);
1412 			return (XF_COMP);
1413 		case WRITE_BARRIER:
1414 			ddi_put8(acchdl, &rreq->operation,
1415 			    BLKIF_OP_WRITE_BARRIER);
1416 			break;
1417 		default:
1418 			if (!vdp->xdf_wce)
1419 				ddi_put8(acchdl, &rreq->operation,
1420 				    BLKIF_OP_WRITE_BARRIER);
1421 			else
1422 				ddi_put8(acchdl, &rreq->operation,
1423 				    BLKIF_OP_WRITE);
1424 			break;
1425 		}
1426 	}
1427 
1428 	ddi_put16(acchdl, &rreq->handle, vdev);
1429 	ddi_put64(acchdl, &rreq->sector_number, blkno);
1430 	ddi_put64(acchdl, &rreq->id, (uint64_t)(uintptr_t)(vreq->v_gs));
1431 
1432 	/*
1433 	 * loop until all segments are populated or no more dma cookie in buf
1434 	 */
1435 	for (;;) {
1436 	/*
1437 	 * Each segment of a blkif request can transfer up to
1438 	 * one 4K page of data.
1439 	 */
1440 		bcnt = vreq->v_dmac.dmac_size;
1441 		ASSERT(bcnt <= PAGESIZE);
1442 		ASSERT((bcnt % XB_BSIZE) == 0);
1443 		dma_addr = vreq->v_dmac.dmac_laddress;
1444 		blk_off = (uint_t)((paddr_t)XB_SEGOFFSET & dma_addr);
1445 		ASSERT((blk_off & XB_BMASK) == 0);
1446 		fsect = blk_off >> XB_BSHIFT;
1447 		lsect = fsect + (bcnt >> XB_BSHIFT) - 1;
1448 		ASSERT(fsect < XB_MAX_SEGLEN / XB_BSIZE &&
1449 		    lsect < XB_MAX_SEGLEN / XB_BSIZE);
1450 		DPRINTF(IO_DBG, ("  ""seg%d: dmacS %lu blk_off %ld\n",
1451 		    seg, vreq->v_dmac.dmac_size, blk_off));
1452 		gr = gs_grant(vreq->v_gs, PATOMA(dma_addr) >> PAGESHIFT);
1453 		ddi_put32(acchdl, &rreq->seg[seg].gref, gr);
1454 		ddi_put8(acchdl, &rreq->seg[seg].first_sect, fsect);
1455 		ddi_put8(acchdl, &rreq->seg[seg].last_sect, lsect);
1456 		DPRINTF(IO_DBG, ("  ""seg%d: fs %d ls %d gr %d dma 0x%"PRIx64
1457 		    "\n", seg, fsect, lsect, gr, dma_addr));
1458 
1459 		blkno += (bcnt >> XB_BSHIFT);
1460 		seg++;
1461 		ASSERT(seg <= BLKIF_MAX_SEGMENTS_PER_REQUEST);
1462 		if (--ndmacs) {
1463 			ddi_dma_nextcookie(vreq->v_dmahdl, &vreq->v_dmac);
1464 			continue;
1465 		}
1466 
1467 		vreq->v_status = VREQ_DMAWIN_DONE;
1468 		vreq->v_blkno = blkno;
1469 		if (vreq->v_dmaw + 1 == vreq->v_ndmaws)
1470 			/* last win */
1471 			rval = XF_COMP;
1472 		else
1473 			rval = XF_PARTIAL;
1474 		break;
1475 	}
1476 	ddi_put8(acchdl,  &rreq->nr_segments, seg);
1477 	DPRINTF(IO_DBG, ("xdf_prepare_rreq: request id=%"PRIx64" ready\n",
1478 	    rreq->id));
1479 
1480 	return (rval);
1481 }
1482 
1483 #define	XDF_QSEC	50000	/* .005 second */
1484 #define	XDF_POLLCNT	12	/* loop for 12 times before time out */
1485 
1486 static int
1487 xdf_drain_io(xdf_t *vdp)
1488 {
1489 	int pollc, rval;
1490 	xendev_ring_t *xbr;
1491 
1492 	if (xdfdebug & SUSRES_DBG)
1493 		xen_printf("xdf_drain_io: start\n");
1494 
1495 	mutex_enter(&vdp->xdf_dev_lk);
1496 
1497 	if ((vdp->xdf_status != XD_READY) && (vdp->xdf_status != XD_SUSPEND))
1498 		goto out;
1499 
1500 	rval = 0;
1501 	xbr = vdp->xdf_xb_ring;
1502 	ASSERT(xbr != NULL);
1503 
1504 	for (pollc = 0; pollc < XDF_POLLCNT; pollc++) {
1505 		if (xvdi_ring_has_unconsumed_responses(xbr)) {
1506 			mutex_exit(&vdp->xdf_dev_lk);
1507 			(void) xdf_intr((caddr_t)vdp);
1508 			mutex_enter(&vdp->xdf_dev_lk);
1509 		}
1510 		if (!xvdi_ring_has_incomp_request(xbr))
1511 			goto out;
1512 
1513 #ifndef	XPV_HVM_DRIVER
1514 		(void) HYPERVISOR_yield();
1515 #endif /* XPV_HVM_DRIVER */
1516 		/*
1517 		 * file-backed devices can be slow
1518 		 */
1519 		drv_usecwait(XDF_QSEC << pollc);
1520 	}
1521 	cmn_err(CE_WARN, "xdf_polled_io: timeout");
1522 	rval = EIO;
1523 out:
1524 	mutex_exit(&vdp->xdf_dev_lk);
1525 	if (xdfdebug & SUSRES_DBG)
1526 		xen_printf("xdf_drain_io: end, err=%d\n", rval);
1527 	return (rval);
1528 }
1529 
1530 /* ARGSUSED5 */
1531 int
1532 xdf_lb_rdwr(dev_info_t *devi, uchar_t cmd, void *bufp,
1533     diskaddr_t start, size_t reqlen, void *tg_cookie)
1534 {
1535 	xdf_t *vdp;
1536 	struct buf *bp;
1537 	int err = 0;
1538 
1539 	vdp = ddi_get_soft_state(vbd_ss, ddi_get_instance(devi));
1540 	if (vdp == NULL)
1541 		return (ENXIO);
1542 
1543 	if ((start + (reqlen >> DEV_BSHIFT)) > vdp->xdf_pgeom.g_capacity)
1544 		return (EINVAL);
1545 
1546 	bp = getrbuf(KM_SLEEP);
1547 	if (cmd == TG_READ)
1548 		bp->b_flags = B_BUSY | B_READ;
1549 	else
1550 		bp->b_flags = B_BUSY | B_WRITE;
1551 	bp->b_un.b_addr = bufp;
1552 	bp->b_bcount = reqlen;
1553 	bp->b_blkno = start;
1554 	bp->b_edev = DDI_DEV_T_NONE; /* don't have dev_t */
1555 
1556 	mutex_enter(&vdp->xdf_dev_lk);
1557 	if (vdp->xdf_xdev_iostat != NULL)
1558 		kstat_waitq_enter(KSTAT_IO_PTR(vdp->xdf_xdev_iostat));
1559 	if (vdp->xdf_f_act == NULL) {
1560 		vdp->xdf_f_act = vdp->xdf_l_act = bp;
1561 	} else {
1562 		vdp->xdf_l_act->av_forw = bp;
1563 		vdp->xdf_l_act = bp;
1564 	}
1565 	mutex_exit(&vdp->xdf_dev_lk);
1566 	xdf_iostart(vdp);
1567 	err = biowait(bp);
1568 
1569 	ASSERT(bp->b_flags & B_DONE);
1570 
1571 	freerbuf(bp);
1572 	return (err);
1573 }
1574 
1575 /*
1576  * synthetic geometry
1577  */
1578 #define	XDF_NSECTS	256
1579 #define	XDF_NHEADS	16
1580 
1581 static void
1582 xdf_synthetic_pgeom(dev_info_t *devi, cmlb_geom_t *geomp)
1583 {
1584 	xdf_t *vdp;
1585 	uint_t ncyl;
1586 
1587 	vdp = ddi_get_soft_state(vbd_ss, ddi_get_instance(devi));
1588 
1589 	ncyl = vdp->xdf_xdev_nblocks / (XDF_NHEADS * XDF_NSECTS);
1590 
1591 	geomp->g_ncyl = ncyl == 0 ? 1 : ncyl;
1592 	geomp->g_acyl = 0;
1593 	geomp->g_nhead = XDF_NHEADS;
1594 	geomp->g_secsize = XB_BSIZE;
1595 	geomp->g_nsect = XDF_NSECTS;
1596 	geomp->g_intrlv = 0;
1597 	geomp->g_rpm = 7200;
1598 	geomp->g_capacity = vdp->xdf_xdev_nblocks;
1599 }
1600 
1601 static int
1602 xdf_lb_getcap(dev_info_t *devi, diskaddr_t *capp)
1603 {
1604 	xdf_t *vdp;
1605 
1606 	vdp = ddi_get_soft_state(vbd_ss, ddi_get_instance(devi));
1607 
1608 	if (vdp == NULL)
1609 		return (ENXIO);
1610 
1611 	mutex_enter(&vdp->xdf_dev_lk);
1612 	*capp = vdp->xdf_pgeom.g_capacity;
1613 	DPRINTF(LBL_DBG, ("capacity %llu\n", *capp));
1614 	mutex_exit(&vdp->xdf_dev_lk);
1615 	return (0);
1616 }
1617 
1618 static int
1619 xdf_lb_getpgeom(dev_info_t *devi, cmlb_geom_t *geomp)
1620 {
1621 	xdf_t *vdp;
1622 
1623 	if ((vdp = ddi_get_soft_state(vbd_ss, ddi_get_instance(devi))) == NULL)
1624 		return (ENXIO);
1625 	*geomp = vdp->xdf_pgeom;
1626 	return (0);
1627 }
1628 
1629 /*
1630  * No real HBA, no geometry available from it
1631  */
1632 /*ARGSUSED*/
1633 static int
1634 xdf_lb_getvgeom(dev_info_t *devi, cmlb_geom_t *geomp)
1635 {
1636 	return (EINVAL);
1637 }
1638 
1639 static int
1640 xdf_lb_getattribute(dev_info_t *devi, tg_attribute_t *tgattributep)
1641 {
1642 	xdf_t *vdp;
1643 
1644 	if (!(vdp = ddi_get_soft_state(vbd_ss, ddi_get_instance(devi))))
1645 		return (ENXIO);
1646 
1647 	if (XD_IS_RO(vdp))
1648 		tgattributep->media_is_writable = 0;
1649 	else
1650 		tgattributep->media_is_writable = 1;
1651 	return (0);
1652 }
1653 
1654 /* ARGSUSED3 */
1655 int
1656 xdf_lb_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie)
1657 {
1658 	switch (cmd) {
1659 	case TG_GETPHYGEOM:
1660 		return (xdf_lb_getpgeom(devi, (cmlb_geom_t *)arg));
1661 	case TG_GETVIRTGEOM:
1662 		return (xdf_lb_getvgeom(devi, (cmlb_geom_t *)arg));
1663 	case TG_GETCAPACITY:
1664 		return (xdf_lb_getcap(devi, (diskaddr_t *)arg));
1665 	case TG_GETBLOCKSIZE:
1666 		*(uint32_t *)arg = XB_BSIZE;
1667 		return (0);
1668 	case TG_GETATTR:
1669 		return (xdf_lb_getattribute(devi, (tg_attribute_t *)arg));
1670 	default:
1671 		return (ENOTTY);
1672 	}
1673 }
1674 
1675 /*
1676  * Kick-off connect process
1677  * Status should be XD_UNKNOWN or XD_CLOSED
1678  * On success, status will be changed to XD_INIT
1679  * On error, status won't be changed
1680  */
1681 static int
1682 xdf_start_connect(xdf_t *vdp)
1683 {
1684 	char *xsnode;
1685 	grant_ref_t gref;
1686 	xenbus_transaction_t xbt;
1687 	int rv;
1688 	dev_info_t *dip = vdp->xdf_dip;
1689 
1690 	if ((vdp->xdf_peer = xvdi_get_oeid(dip)) == (domid_t)-1)
1691 		goto errout;
1692 
1693 	if (xvdi_alloc_evtchn(dip) != DDI_SUCCESS) {
1694 		cmn_err(CE_WARN, "xdf@%s: failed to alloc event channel",
1695 		    ddi_get_name_addr(dip));
1696 		goto errout;
1697 	}
1698 	vdp->xdf_evtchn = xvdi_get_evtchn(dip);
1699 #ifdef XPV_HVM_DRIVER
1700 	ec_bind_evtchn_to_handler(vdp->xdf_evtchn, IPL_VBD, xdf_intr, vdp);
1701 #else /* !XPV_HVM_DRIVER */
1702 	if (ddi_add_intr(dip, 0, NULL, NULL, xdf_intr, (caddr_t)vdp) !=
1703 	    DDI_SUCCESS) {
1704 		cmn_err(CE_WARN, "xdf_start_connect: xdf@%s: "
1705 		    "failed to add intr handler", ddi_get_name_addr(dip));
1706 		goto errout1;
1707 	}
1708 #endif /* !XPV_HVM_DRIVER */
1709 
1710 	if (xvdi_alloc_ring(dip, BLKIF_RING_SIZE,
1711 	    sizeof (union blkif_sring_entry), &gref, &vdp->xdf_xb_ring) !=
1712 	    DDI_SUCCESS) {
1713 		cmn_err(CE_WARN, "xdf@%s: failed to alloc comm ring",
1714 		    ddi_get_name_addr(dip));
1715 		goto errout2;
1716 	}
1717 	vdp->xdf_xb_ring_hdl = vdp->xdf_xb_ring->xr_acc_hdl; /* ugly!! */
1718 
1719 	/*
1720 	 * Write into xenstore the info needed by backend
1721 	 */
1722 	if ((xsnode = xvdi_get_xsname(dip)) == NULL) {
1723 		cmn_err(CE_WARN, "xdf@%s: "
1724 		    "failed to get xenstore node path",
1725 		    ddi_get_name_addr(dip));
1726 		goto fail_trans;
1727 	}
1728 trans_retry:
1729 	if (xenbus_transaction_start(&xbt)) {
1730 		cmn_err(CE_WARN, "xdf@%s: failed to start transaction",
1731 		    ddi_get_name_addr(dip));
1732 		xvdi_fatal_error(dip, EIO, "transaction start");
1733 		goto fail_trans;
1734 	}
1735 
1736 	if (rv = xenbus_printf(xbt, xsnode, "ring-ref", "%u", gref)) {
1737 		cmn_err(CE_WARN, "xdf@%s: failed to write ring-ref",
1738 		    ddi_get_name_addr(dip));
1739 		xvdi_fatal_error(dip, rv, "writing ring-ref");
1740 		goto abort_trans;
1741 	}
1742 
1743 	if (rv = xenbus_printf(xbt, xsnode, "event-channel", "%u",
1744 	    vdp->xdf_evtchn)) {
1745 		cmn_err(CE_WARN, "xdf@%s: failed to write event-channel",
1746 		    ddi_get_name_addr(dip));
1747 		xvdi_fatal_error(dip, rv, "writing event-channel");
1748 		goto abort_trans;
1749 	}
1750 
1751 	/*
1752 	 * "protocol" is written by the domain builder in the case of PV
1753 	 * domains. However, it is not written for HVM domains, so let's
1754 	 * write it here.
1755 	 */
1756 	if (rv = xenbus_printf(xbt, xsnode, "protocol", "%s",
1757 	    XEN_IO_PROTO_ABI_NATIVE)) {
1758 		cmn_err(CE_WARN, "xdf@%s: failed to write protocol",
1759 		    ddi_get_name_addr(dip));
1760 		xvdi_fatal_error(dip, rv, "writing protocol");
1761 		goto abort_trans;
1762 	}
1763 
1764 	if ((rv = xvdi_switch_state(dip, xbt, XenbusStateInitialised)) > 0) {
1765 		cmn_err(CE_WARN, "xdf@%s: "
1766 		    "failed to switch state to XenbusStateInitialised",
1767 		    ddi_get_name_addr(dip));
1768 		xvdi_fatal_error(dip, rv, "writing state");
1769 		goto abort_trans;
1770 	}
1771 
1772 	/* kick-off connect process */
1773 	if (rv = xenbus_transaction_end(xbt, 0)) {
1774 		if (rv == EAGAIN)
1775 			goto trans_retry;
1776 		cmn_err(CE_WARN, "xdf@%s: failed to end transaction",
1777 		    ddi_get_name_addr(dip));
1778 		xvdi_fatal_error(dip, rv, "completing transaction");
1779 		goto fail_trans;
1780 	}
1781 
1782 	ASSERT(mutex_owned(&vdp->xdf_cb_lk));
1783 	mutex_enter(&vdp->xdf_dev_lk);
1784 	vdp->xdf_status = XD_INIT;
1785 	mutex_exit(&vdp->xdf_dev_lk);
1786 
1787 	return (DDI_SUCCESS);
1788 
1789 abort_trans:
1790 	(void) xenbus_transaction_end(xbt, 1);
1791 fail_trans:
1792 	xvdi_free_ring(vdp->xdf_xb_ring);
1793 errout2:
1794 #ifdef XPV_HVM_DRIVER
1795 	ec_unbind_evtchn(vdp->xdf_evtchn);
1796 #else /* !XPV_HVM_DRIVER */
1797 	(void) ddi_remove_intr(vdp->xdf_dip, 0, NULL);
1798 #endif /* !XPV_HVM_DRIVER */
1799 errout1:
1800 	xvdi_free_evtchn(dip);
1801 errout:
1802 	cmn_err(CE_WARN, "xdf@%s: fail to kick-off connecting",
1803 	    ddi_get_name_addr(dip));
1804 	return (DDI_FAILURE);
1805 }
1806 
1807 /*
1808  * Kick-off disconnect process
1809  * Status won't be changed
1810  */
1811 static int
1812 xdf_start_disconnect(xdf_t *vdp)
1813 {
1814 	if (xvdi_switch_state(vdp->xdf_dip, XBT_NULL, XenbusStateClosed) > 0) {
1815 		cmn_err(CE_WARN, "xdf@%s: fail to kick-off disconnecting",
1816 		    ddi_get_name_addr(vdp->xdf_dip));
1817 		return (DDI_FAILURE);
1818 	}
1819 
1820 	return (DDI_SUCCESS);
1821 }
1822 
1823 int
1824 xdf_get_flush_block(xdf_t *vdp)
1825 {
1826 	/*
1827 	 * Get a DEV_BSIZE aligned bufer
1828 	 */
1829 	vdp->xdf_flush_mem = kmem_alloc(DEV_BSIZE * 2, KM_SLEEP);
1830 	vdp->xdf_cache_flush_block =
1831 	    (char *)P2ROUNDUP((uintptr_t)(vdp->xdf_flush_mem), DEV_BSIZE);
1832 	if (xdf_lb_rdwr(vdp->xdf_dip, TG_READ, vdp->xdf_cache_flush_block,
1833 	    xdf_flush_block, DEV_BSIZE, NULL) != 0)
1834 		return (DDI_FAILURE);
1835 	return (DDI_SUCCESS);
1836 }
1837 
1838 /*
1839  * Finish other initialization after we've connected to backend
1840  * Status should be XD_INIT before calling this routine
1841  * On success, status should be changed to XD_READY
1842  * On error, status should stay XD_INIT
1843  */
1844 static int
1845 xdf_post_connect(xdf_t *vdp)
1846 {
1847 	int rv;
1848 	uint_t len;
1849 	char *type;
1850 	char *barrier;
1851 	dev_info_t *devi = vdp->xdf_dip;
1852 
1853 	/*
1854 	 * Determine if feature barrier is supported by backend
1855 	 */
1856 	if (xenbus_read(XBT_NULL, xvdi_get_oename(devi),
1857 	    "feature-barrier", (void **)&barrier, &len) == 0) {
1858 		vdp->xdf_feature_barrier = 1;
1859 		kmem_free(barrier, len);
1860 	} else {
1861 		cmn_err(CE_NOTE, "xdf@%s: failed to read feature-barrier",
1862 		    ddi_get_name_addr(vdp->xdf_dip));
1863 		vdp->xdf_feature_barrier = 0;
1864 	}
1865 
1866 	/* probe backend */
1867 	if (rv = xenbus_gather(XBT_NULL, xvdi_get_oename(devi),
1868 	    "sectors", "%"SCNu64, &vdp->xdf_xdev_nblocks,
1869 	    "info", "%u", &vdp->xdf_xdev_info, NULL)) {
1870 		cmn_err(CE_WARN, "xdf_post_connect: xdf@%s: "
1871 		    "cannot read backend info", ddi_get_name_addr(devi));
1872 		xvdi_fatal_error(devi, rv, "reading backend info");
1873 		return (DDI_FAILURE);
1874 	}
1875 
1876 	/*
1877 	 * Make sure that the device we're connecting isn't smaller than
1878 	 * the old connected device.
1879 	 */
1880 	if (vdp->xdf_xdev_nblocks < vdp->xdf_pgeom.g_capacity) {
1881 		cmn_err(CE_WARN, "xdf_post_connect: xdf@%s: "
1882 		    "backend disk device shrank", ddi_get_name_addr(devi));
1883 		/* XXX:  call xvdi_fatal_error() here? */
1884 		xvdi_fatal_error(devi, rv, "reading backend info");
1885 		return (DDI_FAILURE);
1886 	}
1887 
1888 #ifdef _ILP32
1889 	if (vdp->xdf_xdev_nblocks > DK_MAX_BLOCKS) {
1890 		cmn_err(CE_WARN, "xdf_post_connect: xdf@%s: "
1891 		    "backend disk device too large with %llu blocks for"
1892 		    " 32-bit kernel", ddi_get_name_addr(devi),
1893 		    vdp->xdf_xdev_nblocks);
1894 		xvdi_fatal_error(devi, rv, "reading backend info");
1895 		return (DDI_FAILURE);
1896 	}
1897 #endif
1898 
1899 
1900 	/*
1901 	 * Only update the physical geometry to reflect the new device
1902 	 * size if this is the first time we're connecting to the backend
1903 	 * device.  Once we assign a physical geometry to a device it stays
1904 	 * fixed until:
1905 	 *	- we get detach and re-attached (at which point we
1906 	 *	  automatically assign a new physical geometry).
1907 	 *	- someone calls TG_SETPHYGEOM to explicity set the
1908 	 *	  physical geometry.
1909 	 */
1910 	if (vdp->xdf_pgeom.g_capacity == 0)
1911 		xdf_synthetic_pgeom(devi, &vdp->xdf_pgeom);
1912 
1913 	/* fix disk type */
1914 	if (xenbus_read(XBT_NULL, xvdi_get_xsname(devi), "device-type",
1915 	    (void **)&type, &len) != 0) {
1916 		cmn_err(CE_WARN, "xdf_post_connect: xdf@%s: "
1917 		    "cannot read device-type", ddi_get_name_addr(devi));
1918 		xvdi_fatal_error(devi, rv, "reading device-type");
1919 		return (DDI_FAILURE);
1920 	}
1921 	if (strcmp(type, "cdrom") == 0)
1922 		vdp->xdf_xdev_info |= VDISK_CDROM;
1923 	kmem_free(type, len);
1924 
1925 	/*
1926 	 * We've created all the minor nodes via cmlb_attach() using default
1927 	 * value in xdf_attach() to make it possible to block in xdf_open(),
1928 	 * in case there's anyone (say, booting thread) ever trying to open
1929 	 * it before connected to backend. We will refresh all those minor
1930 	 * nodes w/ latest info we've got now when we are almost connected.
1931 	 *
1932 	 * Don't do this when xdf is already opened by someone (could happen
1933 	 * during resume), for that cmlb_attach() will invalid the label info
1934 	 * and confuse those who has already opened the node, which is bad.
1935 	 */
1936 	if (!xdf_isopen(vdp, -1) && (XD_IS_CD(vdp) || XD_IS_RM(vdp))) {
1937 		/* re-init cmlb w/ latest info we got from backend */
1938 		if (cmlb_attach(devi, &xdf_lb_ops,
1939 		    XD_IS_CD(vdp) ? DTYPE_RODIRECT : DTYPE_DIRECT,
1940 		    XD_IS_RM(vdp), 1,
1941 		    XD_IS_CD(vdp) ? DDI_NT_CD_XVMD : DDI_NT_BLOCK_XVMD,
1942 #if defined(XPV_HVM_DRIVER)
1943 		    CMLB_CREATE_ALTSLICE_VTOC_16_DTYPE_DIRECT |
1944 		    CMLB_INTERNAL_MINOR_NODES,
1945 #else /* !XPV_HVM_DRIVER */
1946 		    CMLB_FAKE_LABEL_ONE_PARTITION,
1947 #endif /* !XPV_HVM_DRIVER */
1948 		    vdp->xdf_vd_lbl, NULL) != 0) {
1949 			cmn_err(CE_WARN, "xdf@%s: cmlb attach failed",
1950 			    ddi_get_name_addr(devi));
1951 			return (DDI_FAILURE);
1952 		}
1953 	}
1954 
1955 	/* mark vbd is ready for I/O */
1956 	ASSERT(mutex_owned(&vdp->xdf_cb_lk));
1957 	mutex_enter(&vdp->xdf_dev_lk);
1958 	vdp->xdf_status = XD_READY;
1959 	mutex_exit(&vdp->xdf_dev_lk);
1960 	/*
1961 	 * If backend has feature-barrier, see if it supports disk
1962 	 * cache flush op.
1963 	 */
1964 	vdp->xdf_flush_supported = 0;
1965 	if (vdp->xdf_feature_barrier) {
1966 		/*
1967 		 * Pretend we already know flush is supported so probe
1968 		 * will attempt the correct op.
1969 		 */
1970 		vdp->xdf_flush_supported = 1;
1971 		if (xdf_lb_rdwr(vdp->xdf_dip, TG_WRITE, NULL, 0, 0, 0) == 0) {
1972 			vdp->xdf_flush_supported = 1;
1973 		} else {
1974 			vdp->xdf_flush_supported = 0;
1975 			/*
1976 			 * If the other end does not support the cache flush op
1977 			 * then we must use a barrier-write to force disk
1978 			 * cache flushing.  Barrier writes require that a data
1979 			 * block actually be written.
1980 			 * Cache a block to barrier-write when we are
1981 			 * asked to perform a flush.
1982 			 * XXX - would it be better to just copy 1 block
1983 			 * (512 bytes) from whatever write we did last
1984 			 * and rewrite that block?
1985 			 */
1986 			if (xdf_get_flush_block(vdp) != DDI_SUCCESS)
1987 				return (DDI_FAILURE);
1988 		}
1989 	}
1990 
1991 	cmn_err(CE_CONT, "?xdf@%s: %"PRIu64" blocks", ddi_get_name_addr(devi),
1992 	    (uint64_t)vdp->xdf_xdev_nblocks);
1993 
1994 	return (DDI_SUCCESS);
1995 }
1996 
1997 /*
1998  * Finish other uninitialization after we've disconnected from backend
1999  * when status is XD_CLOSING or XD_INIT. After returns, status is XD_CLOSED
2000  */
2001 static void
2002 xdf_post_disconnect(xdf_t *vdp)
2003 {
2004 #ifdef XPV_HVM_DRIVER
2005 	ec_unbind_evtchn(vdp->xdf_evtchn);
2006 #else /* !XPV_HVM_DRIVER */
2007 	(void) ddi_remove_intr(vdp->xdf_dip, 0, NULL);
2008 #endif /* !XPV_HVM_DRIVER */
2009 	xvdi_free_evtchn(vdp->xdf_dip);
2010 	xvdi_free_ring(vdp->xdf_xb_ring);
2011 	vdp->xdf_xb_ring = NULL;
2012 	vdp->xdf_xb_ring_hdl = NULL;
2013 	vdp->xdf_peer = (domid_t)-1;
2014 
2015 	ASSERT(mutex_owned(&vdp->xdf_cb_lk));
2016 	mutex_enter(&vdp->xdf_dev_lk);
2017 	vdp->xdf_status = XD_CLOSED;
2018 	mutex_exit(&vdp->xdf_dev_lk);
2019 }
2020 
2021 /*ARGSUSED*/
2022 static void
2023 xdf_oe_change(dev_info_t *dip, ddi_eventcookie_t id, void *arg, void *impl_data)
2024 {
2025 	XenbusState new_state = *(XenbusState *)impl_data;
2026 	xdf_t *vdp = (xdf_t *)ddi_get_driver_private(dip);
2027 	boolean_t unexpect_die = B_FALSE;
2028 	int status;
2029 
2030 	DPRINTF(DDI_DBG, ("xdf@%s: otherend state change to %d!\n",
2031 	    ddi_get_name_addr(dip), new_state));
2032 
2033 	mutex_enter(&vdp->xdf_cb_lk);
2034 
2035 	if (xdf_check_state_transition(vdp, new_state) == DDI_FAILURE) {
2036 		mutex_exit(&vdp->xdf_cb_lk);
2037 		return;
2038 	}
2039 
2040 	switch (new_state) {
2041 	case XenbusStateInitialising:
2042 		ASSERT(vdp->xdf_status == XD_CLOSED);
2043 		/*
2044 		 * backend recovered from a previous failure,
2045 		 * kick-off connect process again
2046 		 */
2047 		if (xdf_start_connect(vdp) != DDI_SUCCESS) {
2048 			cmn_err(CE_WARN, "xdf@%s:"
2049 			    " failed to start reconnecting to backend",
2050 			    ddi_get_name_addr(dip));
2051 		}
2052 		break;
2053 	case XenbusStateConnected:
2054 		ASSERT(vdp->xdf_status == XD_INIT);
2055 		(void) xvdi_switch_state(dip, XBT_NULL, XenbusStateConnected);
2056 		/* finish final init after connect */
2057 		if (xdf_post_connect(vdp) != DDI_SUCCESS)
2058 			(void) xdf_start_disconnect(vdp);
2059 		break;
2060 	case XenbusStateClosing:
2061 		if (vdp->xdf_status == XD_READY) {
2062 			mutex_enter(&vdp->xdf_dev_lk);
2063 			if (xdf_isopen(vdp, -1)) {
2064 				cmn_err(CE_NOTE, "xdf@%s: hot-unplug failed, "
2065 				    "still in use", ddi_get_name_addr(dip));
2066 				mutex_exit(&vdp->xdf_dev_lk);
2067 				break;
2068 			} else {
2069 				vdp->xdf_status = XD_CLOSING;
2070 			}
2071 			mutex_exit(&vdp->xdf_dev_lk);
2072 		}
2073 		(void) xdf_start_disconnect(vdp);
2074 		break;
2075 	case XenbusStateClosed:
2076 		/* first check if BE closed unexpectedly */
2077 		mutex_enter(&vdp->xdf_dev_lk);
2078 		if (xdf_isopen(vdp, -1)) {
2079 			unexpect_die = B_TRUE;
2080 			unexpectedie(vdp);
2081 			cmn_err(CE_WARN, "xdf@%s: backend closed, "
2082 			    "reconnecting...", ddi_get_name_addr(dip));
2083 		}
2084 		mutex_exit(&vdp->xdf_dev_lk);
2085 
2086 		if (vdp->xdf_status == XD_READY) {
2087 			mutex_enter(&vdp->xdf_dev_lk);
2088 			vdp->xdf_status = XD_CLOSING;
2089 			mutex_exit(&vdp->xdf_dev_lk);
2090 
2091 #ifdef	DOMU_BACKEND
2092 			(void) xvdi_post_event(dip, XEN_HP_REMOVE);
2093 #endif
2094 
2095 			xdf_post_disconnect(vdp);
2096 			(void) xvdi_switch_state(dip, XBT_NULL,
2097 			    XenbusStateClosed);
2098 		} else if ((vdp->xdf_status == XD_INIT) ||
2099 		    (vdp->xdf_status == XD_CLOSING)) {
2100 			xdf_post_disconnect(vdp);
2101 		} else {
2102 			mutex_enter(&vdp->xdf_dev_lk);
2103 			vdp->xdf_status = XD_CLOSED;
2104 			mutex_exit(&vdp->xdf_dev_lk);
2105 		}
2106 	}
2107 
2108 	/* notify anybody waiting for oe state change */
2109 	mutex_enter(&vdp->xdf_dev_lk);
2110 	cv_broadcast(&vdp->xdf_dev_cv);
2111 	mutex_exit(&vdp->xdf_dev_lk);
2112 
2113 	status = vdp->xdf_status;
2114 	mutex_exit(&vdp->xdf_cb_lk);
2115 
2116 	if (status == XD_READY) {
2117 		xdf_iostart(vdp);
2118 	} else if ((status == XD_CLOSED) && !unexpect_die) {
2119 		/* interface is closed successfully, remove all minor nodes */
2120 		if (vdp->xdf_vd_lbl != NULL) {
2121 			cmlb_detach(vdp->xdf_vd_lbl, NULL);
2122 			cmlb_free_handle(&vdp->xdf_vd_lbl);
2123 			vdp->xdf_vd_lbl = NULL;
2124 		}
2125 	}
2126 }
2127 
2128 /* check if partition is open, -1 - check all partitions on the disk */
2129 static boolean_t
2130 xdf_isopen(xdf_t *vdp, int partition)
2131 {
2132 	int i;
2133 	ulong_t parbit;
2134 	boolean_t rval = B_FALSE;
2135 
2136 	ASSERT((partition == -1) ||
2137 	    ((partition >= 0) || (partition < XDF_PEXT)));
2138 
2139 	if (partition == -1)
2140 		parbit = (ulong_t)-1;
2141 	else
2142 		parbit = 1 << partition;
2143 
2144 	for (i = 0; i < OTYPCNT; i++) {
2145 		if (vdp->xdf_vd_open[i] & parbit)
2146 			rval = B_TRUE;
2147 	}
2148 
2149 	return (rval);
2150 }
2151 
2152 /*
2153  * Xdf_check_state_transition will check the XenbusState change to see
2154  * if the change is a valid transition or not.
2155  * The new state is written by backend domain, or by running xenstore-write
2156  * to change it manually in dom0
2157  */
2158 static int
2159 xdf_check_state_transition(xdf_t *vdp, XenbusState oestate)
2160 {
2161 	int status;
2162 	int stcheck;
2163 #define	STOK	0 /* need further process */
2164 #define	STNOP	1 /* no action need taking */
2165 #define	STBUG	2 /* unexpected state change, could be a bug */
2166 
2167 	status = vdp->xdf_status;
2168 	stcheck = STOK;
2169 
2170 	switch (status) {
2171 	case XD_UNKNOWN:
2172 		if ((oestate == XenbusStateUnknown)		||
2173 		    (oestate == XenbusStateConnected))
2174 			stcheck = STBUG;
2175 		else if ((oestate == XenbusStateInitialising)	||
2176 		    (oestate == XenbusStateInitWait)		||
2177 		    (oestate == XenbusStateInitialised))
2178 			stcheck = STNOP;
2179 		break;
2180 	case XD_INIT:
2181 		if (oestate == XenbusStateUnknown)
2182 			stcheck = STBUG;
2183 		else if ((oestate == XenbusStateInitialising)	||
2184 		    (oestate == XenbusStateInitWait)		||
2185 		    (oestate == XenbusStateInitialised))
2186 			stcheck = STNOP;
2187 		break;
2188 	case XD_READY:
2189 		if ((oestate == XenbusStateUnknown)		||
2190 		    (oestate == XenbusStateInitialising)	||
2191 		    (oestate == XenbusStateInitWait)		||
2192 		    (oestate == XenbusStateInitialised))
2193 			stcheck = STBUG;
2194 		else if (oestate == XenbusStateConnected)
2195 			stcheck = STNOP;
2196 		break;
2197 	case XD_CLOSING:
2198 		if ((oestate == XenbusStateUnknown)		||
2199 		    (oestate == XenbusStateInitialising)	||
2200 		    (oestate == XenbusStateInitWait)		||
2201 		    (oestate == XenbusStateInitialised)		||
2202 		    (oestate == XenbusStateConnected))
2203 			stcheck = STBUG;
2204 		else if (oestate == XenbusStateClosing)
2205 			stcheck = STNOP;
2206 		break;
2207 	case XD_CLOSED:
2208 		if ((oestate == XenbusStateUnknown)		||
2209 		    (oestate == XenbusStateConnected))
2210 			stcheck = STBUG;
2211 		else if ((oestate == XenbusStateInitWait)	||
2212 		    (oestate == XenbusStateInitialised)		||
2213 		    (oestate == XenbusStateClosing)		||
2214 		    (oestate == XenbusStateClosed))
2215 			stcheck = STNOP;
2216 		break;
2217 	case XD_SUSPEND:
2218 	default:
2219 			stcheck = STBUG;
2220 	}
2221 
2222 	if (stcheck == STOK)
2223 		return (DDI_SUCCESS);
2224 
2225 	if (stcheck == STBUG)
2226 		cmn_err(CE_NOTE, "xdf@%s: unexpected otherend "
2227 		    "state change to %d!, when status is %d",
2228 		    ddi_get_name_addr(vdp->xdf_dip), oestate, status);
2229 
2230 	return (DDI_FAILURE);
2231 }
2232 
2233 static int
2234 xdf_connect(xdf_t *vdp, boolean_t wait)
2235 {
2236 	ASSERT(mutex_owned(&vdp->xdf_dev_lk));
2237 	while (vdp->xdf_status != XD_READY) {
2238 		if (!wait || (vdp->xdf_status > XD_READY))
2239 			break;
2240 
2241 		if (cv_wait_sig(&vdp->xdf_dev_cv, &vdp->xdf_dev_lk) == 0)
2242 			break;
2243 	}
2244 
2245 	return (vdp->xdf_status);
2246 }
2247 
2248 /*
2249  * callback func when DMA/GTE resources is available
2250  *
2251  * Note: we only register one callback function to grant table subsystem
2252  * since we only have one 'struct gnttab_free_callback' in xdf_t.
2253  */
2254 static int
2255 xdf_dmacallback(caddr_t arg)
2256 {
2257 	xdf_t *vdp = (xdf_t *)arg;
2258 	ASSERT(vdp != NULL);
2259 
2260 	DPRINTF(DMA_DBG, ("xdf@%s: DMA callback started\n",
2261 	    ddi_get_name_addr(vdp->xdf_dip)));
2262 
2263 	ddi_trigger_softintr(vdp->xdf_softintr_id);
2264 	return (DDI_DMA_CALLBACK_DONE);
2265 }
2266 
2267 static uint_t
2268 xdf_iorestart(caddr_t arg)
2269 {
2270 	xdf_t *vdp = (xdf_t *)arg;
2271 
2272 	ASSERT(vdp != NULL);
2273 
2274 	mutex_enter(&vdp->xdf_dev_lk);
2275 	ASSERT(ISDMACBON(vdp));
2276 	SETDMACBOFF(vdp);
2277 	mutex_exit(&vdp->xdf_dev_lk);
2278 
2279 	xdf_iostart(vdp);
2280 
2281 	return (DDI_INTR_CLAIMED);
2282 }
2283 
2284 static void
2285 xdf_timeout_handler(void *arg)
2286 {
2287 	xdf_t *vdp = arg;
2288 
2289 	mutex_enter(&vdp->xdf_dev_lk);
2290 	vdp->xdf_timeout_id = 0;
2291 	mutex_exit(&vdp->xdf_dev_lk);
2292 
2293 	/* new timeout thread could be re-scheduled */
2294 	xdf_iostart(vdp);
2295 }
2296 
2297 /*
2298  * Alloc a vreq for this bp
2299  * bp->av_back contains the pointer to the vreq upon return
2300  */
2301 static v_req_t *
2302 vreq_get(xdf_t *vdp, buf_t *bp)
2303 {
2304 	v_req_t *vreq = NULL;
2305 
2306 	ASSERT(BP2VREQ(bp) == NULL);
2307 
2308 	vreq = kmem_cache_alloc(xdf_vreq_cache, KM_NOSLEEP);
2309 	if (vreq == NULL) {
2310 		if (vdp->xdf_timeout_id == 0)
2311 			/* restart I/O after one second */
2312 			vdp->xdf_timeout_id =
2313 			    timeout(xdf_timeout_handler, vdp, hz);
2314 		return (NULL);
2315 	}
2316 	bzero(vreq, sizeof (v_req_t));
2317 
2318 	list_insert_head(&vdp->xdf_vreq_act, (void *)vreq);
2319 	bp->av_back = (buf_t *)vreq;
2320 	vreq->v_buf = bp;
2321 	vreq->v_status = VREQ_INIT;
2322 	/* init of other fields in vreq is up to the caller */
2323 
2324 	return (vreq);
2325 }
2326 
2327 static void
2328 vreq_free(xdf_t *vdp, v_req_t *vreq)
2329 {
2330 	buf_t *bp = vreq->v_buf;
2331 
2332 	list_remove(&vdp->xdf_vreq_act, (void *)vreq);
2333 
2334 	if (vreq->v_flush_diskcache == FLUSH_DISKCACHE)
2335 		goto done;
2336 
2337 	switch (vreq->v_status) {
2338 	case VREQ_DMAWIN_DONE:
2339 	case VREQ_GS_ALLOCED:
2340 	case VREQ_DMABUF_BOUND:
2341 		(void) ddi_dma_unbind_handle(vreq->v_dmahdl);
2342 		/*FALLTHRU*/
2343 	case VREQ_DMAMEM_ALLOCED:
2344 		if (!ALIGNED_XFER(bp)) {
2345 			ASSERT(vreq->v_abuf != NULL);
2346 			if (!IS_ERROR(bp) && IS_READ(bp))
2347 				bcopy(vreq->v_abuf, bp->b_un.b_addr,
2348 				    bp->b_bcount);
2349 			ddi_dma_mem_free(&vreq->v_align);
2350 		}
2351 		/*FALLTHRU*/
2352 	case VREQ_MEMDMAHDL_ALLOCED:
2353 		if (!ALIGNED_XFER(bp))
2354 			ddi_dma_free_handle(&vreq->v_memdmahdl);
2355 		/*FALLTHRU*/
2356 	case VREQ_DMAHDL_ALLOCED:
2357 		ddi_dma_free_handle(&vreq->v_dmahdl);
2358 		break;
2359 	default:
2360 		break;
2361 	}
2362 done:
2363 	vreq->v_buf->av_back = NULL;
2364 	kmem_cache_free(xdf_vreq_cache, vreq);
2365 }
2366 
2367 /*
2368  * Initalize the DMA and grant table resources for the buf
2369  */
2370 static int
2371 vreq_setup(xdf_t *vdp, v_req_t *vreq)
2372 {
2373 	int rc;
2374 	ddi_dma_attr_t dmaattr;
2375 	uint_t ndcs, ndws;
2376 	ddi_dma_handle_t dh;
2377 	ddi_dma_handle_t mdh;
2378 	ddi_dma_cookie_t dc;
2379 	ddi_acc_handle_t abh;
2380 	caddr_t	aba;
2381 	ge_slot_t *gs;
2382 	size_t bufsz;
2383 	off_t off;
2384 	size_t sz;
2385 	buf_t *bp = vreq->v_buf;
2386 	int dma_flags = (IS_READ(bp) ? DDI_DMA_READ : DDI_DMA_WRITE) |
2387 	    DDI_DMA_STREAMING | DDI_DMA_PARTIAL;
2388 
2389 	switch (vreq->v_status) {
2390 	case VREQ_INIT:
2391 		if (IS_FLUSH_DISKCACHE(bp)) {
2392 			if ((gs = gs_get(vdp, IS_READ(bp))) == NULL) {
2393 				DPRINTF(DMA_DBG, (
2394 				    "xdf@%s: get ge_slotfailed\n",
2395 				    ddi_get_name_addr(vdp->xdf_dip)));
2396 				return (DDI_FAILURE);
2397 			}
2398 			vreq->v_blkno = 0;
2399 			vreq->v_nslots = 1;
2400 			vreq->v_gs = gs;
2401 			vreq->v_flush_diskcache = FLUSH_DISKCACHE;
2402 			vreq->v_status = VREQ_GS_ALLOCED;
2403 			gs->vreq = vreq;
2404 			return (DDI_SUCCESS);
2405 		}
2406 
2407 		if (IS_WRITE_BARRIER(vdp, bp))
2408 			vreq->v_flush_diskcache = WRITE_BARRIER;
2409 		vreq->v_blkno = bp->b_blkno +
2410 		    (diskaddr_t)(uintptr_t)bp->b_private;
2411 		bp->b_private = NULL;
2412 		/* See if we wrote new data to our flush block */
2413 		if (!IS_READ(bp) && USE_WRITE_BARRIER(vdp))
2414 			check_fbwrite(vdp, bp, vreq->v_blkno);
2415 		vreq->v_status = VREQ_INIT_DONE;
2416 		/*FALLTHRU*/
2417 
2418 	case VREQ_INIT_DONE:
2419 		/*
2420 		 * alloc DMA handle
2421 		 */
2422 		rc = ddi_dma_alloc_handle(vdp->xdf_dip, &xb_dma_attr,
2423 		    xdf_dmacallback, (caddr_t)vdp, &dh);
2424 		if (rc != DDI_SUCCESS) {
2425 			SETDMACBON(vdp);
2426 			DPRINTF(DMA_DBG, ("xdf@%s: DMA handle alloc failed\n",
2427 			    ddi_get_name_addr(vdp->xdf_dip)));
2428 			return (DDI_FAILURE);
2429 		}
2430 
2431 		vreq->v_dmahdl = dh;
2432 		vreq->v_status = VREQ_DMAHDL_ALLOCED;
2433 		/*FALLTHRU*/
2434 
2435 	case VREQ_DMAHDL_ALLOCED:
2436 		/*
2437 		 * alloc dma handle for 512-byte aligned buf
2438 		 */
2439 		if (!ALIGNED_XFER(bp)) {
2440 			/*
2441 			 * XXPV: we need to temporarily enlarge the seg
2442 			 * boundary and s/g length to work round CR6381968
2443 			 */
2444 			dmaattr = xb_dma_attr;
2445 			dmaattr.dma_attr_seg = (uint64_t)-1;
2446 			dmaattr.dma_attr_sgllen = INT_MAX;
2447 			rc = ddi_dma_alloc_handle(vdp->xdf_dip, &dmaattr,
2448 			    xdf_dmacallback, (caddr_t)vdp, &mdh);
2449 			if (rc != DDI_SUCCESS) {
2450 				SETDMACBON(vdp);
2451 				DPRINTF(DMA_DBG, ("xdf@%s: unaligned buf DMA"
2452 				    "handle alloc failed\n",
2453 				    ddi_get_name_addr(vdp->xdf_dip)));
2454 				return (DDI_FAILURE);
2455 			}
2456 			vreq->v_memdmahdl = mdh;
2457 			vreq->v_status = VREQ_MEMDMAHDL_ALLOCED;
2458 		}
2459 		/*FALLTHRU*/
2460 
2461 	case VREQ_MEMDMAHDL_ALLOCED:
2462 		/*
2463 		 * alloc 512-byte aligned buf
2464 		 */
2465 		if (!ALIGNED_XFER(bp)) {
2466 			if (bp->b_flags & (B_PAGEIO | B_PHYS))
2467 				bp_mapin(bp);
2468 
2469 			rc = ddi_dma_mem_alloc(vreq->v_memdmahdl,
2470 			    roundup(bp->b_bcount, XB_BSIZE), &xc_acc_attr,
2471 			    DDI_DMA_STREAMING, xdf_dmacallback, (caddr_t)vdp,
2472 			    &aba, &bufsz, &abh);
2473 			if (rc != DDI_SUCCESS) {
2474 				SETDMACBON(vdp);
2475 				DPRINTF(DMA_DBG, (
2476 				    "xdf@%s: DMA mem allocation failed\n",
2477 				    ddi_get_name_addr(vdp->xdf_dip)));
2478 				return (DDI_FAILURE);
2479 			}
2480 
2481 			vreq->v_abuf = aba;
2482 			vreq->v_align = abh;
2483 			vreq->v_status = VREQ_DMAMEM_ALLOCED;
2484 
2485 			ASSERT(bufsz >= bp->b_bcount);
2486 			if (!IS_READ(bp))
2487 				bcopy(bp->b_un.b_addr, vreq->v_abuf,
2488 				    bp->b_bcount);
2489 		}
2490 		/*FALLTHRU*/
2491 
2492 	case VREQ_DMAMEM_ALLOCED:
2493 		/*
2494 		 * dma bind
2495 		 */
2496 		if (ALIGNED_XFER(bp)) {
2497 			rc = ddi_dma_buf_bind_handle(vreq->v_dmahdl, bp,
2498 			    dma_flags, xdf_dmacallback, (caddr_t)vdp,
2499 			    &dc, &ndcs);
2500 		} else {
2501 			rc = ddi_dma_addr_bind_handle(vreq->v_dmahdl,
2502 			    NULL, vreq->v_abuf, bp->b_bcount, dma_flags,
2503 			    xdf_dmacallback, (caddr_t)vdp, &dc, &ndcs);
2504 		}
2505 		if (rc == DDI_DMA_MAPPED || rc == DDI_DMA_PARTIAL_MAP) {
2506 			/* get num of dma windows */
2507 			if (rc == DDI_DMA_PARTIAL_MAP) {
2508 				rc = ddi_dma_numwin(vreq->v_dmahdl, &ndws);
2509 				ASSERT(rc == DDI_SUCCESS);
2510 			} else {
2511 				ndws = 1;
2512 			}
2513 		} else {
2514 			SETDMACBON(vdp);
2515 			DPRINTF(DMA_DBG, ("xdf@%s: DMA bind failed\n",
2516 			    ddi_get_name_addr(vdp->xdf_dip)));
2517 			return (DDI_FAILURE);
2518 		}
2519 
2520 		vreq->v_dmac = dc;
2521 		vreq->v_dmaw = 0;
2522 		vreq->v_ndmacs = ndcs;
2523 		vreq->v_ndmaws = ndws;
2524 		vreq->v_nslots = ndws;
2525 		vreq->v_status = VREQ_DMABUF_BOUND;
2526 		/*FALLTHRU*/
2527 
2528 	case VREQ_DMABUF_BOUND:
2529 		/*
2530 		 * get ge_slot, callback is set upon failure from gs_get(),
2531 		 * if not set previously
2532 		 */
2533 		if ((gs = gs_get(vdp, IS_READ(bp))) == NULL) {
2534 			DPRINTF(DMA_DBG, ("xdf@%s: get ge_slot failed\n",
2535 			    ddi_get_name_addr(vdp->xdf_dip)));
2536 			return (DDI_FAILURE);
2537 		}
2538 
2539 		vreq->v_gs = gs;
2540 		gs->vreq = vreq;
2541 		vreq->v_status = VREQ_GS_ALLOCED;
2542 		break;
2543 
2544 	case VREQ_GS_ALLOCED:
2545 		/* nothing need to be done */
2546 		break;
2547 
2548 	case VREQ_DMAWIN_DONE:
2549 		/*
2550 		 * move to the next dma window
2551 		 */
2552 		ASSERT((vreq->v_dmaw + 1) < vreq->v_ndmaws);
2553 
2554 		/* get a ge_slot for this DMA window */
2555 		if ((gs = gs_get(vdp, IS_READ(bp))) == NULL) {
2556 			DPRINTF(DMA_DBG, ("xdf@%s: get ge_slot failed\n",
2557 			    ddi_get_name_addr(vdp->xdf_dip)));
2558 			return (DDI_FAILURE);
2559 		}
2560 
2561 		vreq->v_gs = gs;
2562 		gs->vreq = vreq;
2563 		vreq->v_dmaw++;
2564 		rc = ddi_dma_getwin(vreq->v_dmahdl, vreq->v_dmaw, &off, &sz,
2565 		    &vreq->v_dmac, &vreq->v_ndmacs);
2566 		ASSERT(rc == DDI_SUCCESS);
2567 		vreq->v_status = VREQ_GS_ALLOCED;
2568 		break;
2569 
2570 	default:
2571 		return (DDI_FAILURE);
2572 	}
2573 
2574 	return (DDI_SUCCESS);
2575 }
2576 
2577 static ge_slot_t *
2578 gs_get(xdf_t *vdp, int isread)
2579 {
2580 	grant_ref_t gh;
2581 	ge_slot_t *gs;
2582 
2583 	/* try to alloc GTEs needed in this slot, first */
2584 	if (gnttab_alloc_grant_references(
2585 	    BLKIF_MAX_SEGMENTS_PER_REQUEST, &gh) == -1) {
2586 		if (vdp->xdf_gnt_callback.next == NULL) {
2587 			SETDMACBON(vdp);
2588 			gnttab_request_free_callback(
2589 			    &vdp->xdf_gnt_callback,
2590 			    (void (*)(void *))xdf_dmacallback,
2591 			    (void *)vdp,
2592 			    BLKIF_MAX_SEGMENTS_PER_REQUEST);
2593 		}
2594 		return (NULL);
2595 	}
2596 
2597 	gs = kmem_cache_alloc(xdf_gs_cache, KM_NOSLEEP);
2598 	if (gs == NULL) {
2599 		gnttab_free_grant_references(gh);
2600 		if (vdp->xdf_timeout_id == 0)
2601 			/* restart I/O after one second */
2602 			vdp->xdf_timeout_id =
2603 			    timeout(xdf_timeout_handler, vdp, hz);
2604 		return (NULL);
2605 	}
2606 
2607 	/* init gs_slot */
2608 	list_insert_head(&vdp->xdf_gs_act, (void *)gs);
2609 	gs->oeid = vdp->xdf_peer;
2610 	gs->isread = isread;
2611 	gs->ghead = gh;
2612 	gs->ngrefs = 0;
2613 
2614 	return (gs);
2615 }
2616 
2617 static void
2618 gs_free(xdf_t *vdp, ge_slot_t *gs)
2619 {
2620 	int i;
2621 	grant_ref_t *gp = gs->ge;
2622 	int ngrefs = gs->ngrefs;
2623 	boolean_t isread = gs->isread;
2624 
2625 	list_remove(&vdp->xdf_gs_act, (void *)gs);
2626 
2627 	/* release all grant table entry resources used in this slot */
2628 	for (i = 0; i < ngrefs; i++, gp++)
2629 		gnttab_end_foreign_access(*gp, !isread, 0);
2630 	gnttab_free_grant_references(gs->ghead);
2631 
2632 	kmem_cache_free(xdf_gs_cache, (void *)gs);
2633 }
2634 
2635 static grant_ref_t
2636 gs_grant(ge_slot_t *gs, mfn_t mfn)
2637 {
2638 	grant_ref_t gr = gnttab_claim_grant_reference(&gs->ghead);
2639 
2640 	ASSERT(gr != -1);
2641 	ASSERT(gs->ngrefs < BLKIF_MAX_SEGMENTS_PER_REQUEST);
2642 	gs->ge[gs->ngrefs++] = gr;
2643 	gnttab_grant_foreign_access_ref(gr, gs->oeid, mfn, !gs->isread);
2644 
2645 	return (gr);
2646 }
2647 
2648 static void
2649 unexpectedie(xdf_t *vdp)
2650 {
2651 	/* clean up I/Os in ring that have responses */
2652 	if (xvdi_ring_has_unconsumed_responses(vdp->xdf_xb_ring)) {
2653 		mutex_exit(&vdp->xdf_dev_lk);
2654 		(void) xdf_intr((caddr_t)vdp);
2655 		mutex_enter(&vdp->xdf_dev_lk);
2656 	}
2657 
2658 	/* free up all grant table entries */
2659 	while (!list_is_empty(&vdp->xdf_gs_act))
2660 		gs_free(vdp, list_head(&vdp->xdf_gs_act));
2661 
2662 	/*
2663 	 * move bp back to active list orderly
2664 	 * vreq_busy is updated in vreq_free()
2665 	 */
2666 	while (!list_is_empty(&vdp->xdf_vreq_act)) {
2667 		v_req_t *vreq = list_head(&vdp->xdf_vreq_act);
2668 		buf_t *bp = vreq->v_buf;
2669 
2670 		bp->av_back = NULL;
2671 		bp->b_resid = bp->b_bcount;
2672 		if (vdp->xdf_f_act == NULL) {
2673 			vdp->xdf_f_act = vdp->xdf_l_act = bp;
2674 		} else {
2675 			/* move to the head of list */
2676 			bp->av_forw = vdp->xdf_f_act;
2677 			vdp->xdf_f_act = bp;
2678 		}
2679 		if (vdp->xdf_xdev_iostat != NULL)
2680 			kstat_runq_back_to_waitq(
2681 			    KSTAT_IO_PTR(vdp->xdf_xdev_iostat));
2682 		vreq_free(vdp, vreq);
2683 	}
2684 }
2685 
2686 static void
2687 xdfmin(struct buf *bp)
2688 {
2689 	if (bp->b_bcount > xdf_maxphys)
2690 		bp->b_bcount = xdf_maxphys;
2691 }
2692 
2693 void
2694 xdf_kstat_delete(dev_info_t *dip)
2695 {
2696 	xdf_t	*vdp = (xdf_t *)ddi_get_driver_private(dip);
2697 	kstat_t	*kstat;
2698 
2699 	/*
2700 	 * The locking order here is xdf_iostat_lk and then xdf_dev_lk.
2701 	 * xdf_dev_lk is used to protect the xdf_xdev_iostat pointer
2702 	 * and the contents of the our kstat.  xdf_iostat_lk is used
2703 	 * to protect the allocation and freeing of the actual kstat.
2704 	 * xdf_dev_lk can't be used for this purpose because kstat
2705 	 * readers use it to access the contents of the kstat and
2706 	 * hence it can't be held when calling kstat_delete().
2707 	 */
2708 	mutex_enter(&vdp->xdf_iostat_lk);
2709 	mutex_enter(&vdp->xdf_dev_lk);
2710 
2711 	if (vdp->xdf_xdev_iostat == NULL) {
2712 		mutex_exit(&vdp->xdf_dev_lk);
2713 		mutex_exit(&vdp->xdf_iostat_lk);
2714 		return;
2715 	}
2716 
2717 	kstat = vdp->xdf_xdev_iostat;
2718 	vdp->xdf_xdev_iostat = NULL;
2719 	mutex_exit(&vdp->xdf_dev_lk);
2720 
2721 	kstat_delete(kstat);
2722 	mutex_exit(&vdp->xdf_iostat_lk);
2723 }
2724 
2725 int
2726 xdf_kstat_create(dev_info_t *dip, char *ks_module, int ks_instance)
2727 {
2728 	xdf_t	*vdp = (xdf_t *)ddi_get_driver_private(dip);
2729 
2730 	/* See comment about locking in xdf_kstat_delete(). */
2731 	mutex_enter(&vdp->xdf_iostat_lk);
2732 	mutex_enter(&vdp->xdf_dev_lk);
2733 
2734 	if (vdp->xdf_xdev_iostat != NULL) {
2735 		mutex_exit(&vdp->xdf_dev_lk);
2736 		mutex_exit(&vdp->xdf_iostat_lk);
2737 		return (-1);
2738 	}
2739 
2740 	if ((vdp->xdf_xdev_iostat = kstat_create(
2741 	    ks_module, ks_instance, NULL, "disk",
2742 	    KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT)) == NULL) {
2743 		mutex_exit(&vdp->xdf_dev_lk);
2744 		mutex_exit(&vdp->xdf_iostat_lk);
2745 		return (-1);
2746 	}
2747 
2748 	vdp->xdf_xdev_iostat->ks_lock = &vdp->xdf_dev_lk;
2749 	kstat_install(vdp->xdf_xdev_iostat);
2750 	mutex_exit(&vdp->xdf_dev_lk);
2751 	mutex_exit(&vdp->xdf_iostat_lk);
2752 
2753 	return (0);
2754 }
2755 
2756 #if defined(XPV_HVM_DRIVER)
2757 
2758 typedef struct xdf_hvm_entry {
2759 	list_node_t	xdf_he_list;
2760 	char		*xdf_he_path;
2761 	dev_info_t	*xdf_he_dip;
2762 } xdf_hvm_entry_t;
2763 
2764 static list_t xdf_hvm_list;
2765 static kmutex_t xdf_hvm_list_lock;
2766 
2767 static xdf_hvm_entry_t *
2768 i_xdf_hvm_find(char *path, dev_info_t *dip)
2769 {
2770 	xdf_hvm_entry_t	*i;
2771 
2772 	ASSERT((path != NULL) || (dip != NULL));
2773 	ASSERT(MUTEX_HELD(&xdf_hvm_list_lock));
2774 
2775 	i = list_head(&xdf_hvm_list);
2776 	while (i != NULL) {
2777 		if ((path != NULL) && strcmp(i->xdf_he_path, path) != 0) {
2778 			i = list_next(&xdf_hvm_list, i);
2779 			continue;
2780 		}
2781 		if ((dip != NULL) && (i->xdf_he_dip != dip)) {
2782 			i = list_next(&xdf_hvm_list, i);
2783 			continue;
2784 		}
2785 		break;
2786 	}
2787 	return (i);
2788 }
2789 
2790 dev_info_t *
2791 xdf_hvm_hold(char *path)
2792 {
2793 	xdf_hvm_entry_t	*i;
2794 	dev_info_t	*dip;
2795 
2796 	mutex_enter(&xdf_hvm_list_lock);
2797 	i = i_xdf_hvm_find(path, NULL);
2798 	if (i == NULL) {
2799 		mutex_exit(&xdf_hvm_list_lock);
2800 		return (B_FALSE);
2801 	}
2802 	ndi_hold_devi(dip = i->xdf_he_dip);
2803 	mutex_exit(&xdf_hvm_list_lock);
2804 	return (dip);
2805 }
2806 
2807 static void
2808 xdf_hvm_add(dev_info_t *dip)
2809 {
2810 	xdf_hvm_entry_t	*i;
2811 	char		*path;
2812 
2813 	/* figure out the path for the dip */
2814 	path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
2815 	(void) ddi_pathname(dip, path);
2816 
2817 	i = kmem_alloc(sizeof (*i), KM_SLEEP);
2818 	i->xdf_he_dip = dip;
2819 	i->xdf_he_path = i_ddi_strdup(path, KM_SLEEP);
2820 
2821 	mutex_enter(&xdf_hvm_list_lock);
2822 	ASSERT(i_xdf_hvm_find(path, NULL) == NULL);
2823 	ASSERT(i_xdf_hvm_find(NULL, dip) == NULL);
2824 	list_insert_head(&xdf_hvm_list, i);
2825 	mutex_exit(&xdf_hvm_list_lock);
2826 
2827 	kmem_free(path, MAXPATHLEN);
2828 }
2829 
2830 static void
2831 xdf_hvm_rm(dev_info_t *dip)
2832 {
2833 	xdf_hvm_entry_t	*i;
2834 
2835 	mutex_enter(&xdf_hvm_list_lock);
2836 	VERIFY((i = i_xdf_hvm_find(NULL, dip)) != NULL);
2837 	list_remove(&xdf_hvm_list, i);
2838 	mutex_exit(&xdf_hvm_list_lock);
2839 
2840 	kmem_free(i->xdf_he_path, strlen(i->xdf_he_path) + 1);
2841 	kmem_free(i, sizeof (*i));
2842 }
2843 
2844 static void
2845 xdf_hvm_init(void)
2846 {
2847 	list_create(&xdf_hvm_list, sizeof (xdf_hvm_entry_t),
2848 	    offsetof(xdf_hvm_entry_t, xdf_he_list));
2849 	mutex_init(&xdf_hvm_list_lock, NULL, MUTEX_DEFAULT, NULL);
2850 }
2851 
2852 static void
2853 xdf_hvm_fini(void)
2854 {
2855 	ASSERT(list_head(&xdf_hvm_list) == NULL);
2856 	list_destroy(&xdf_hvm_list);
2857 	mutex_destroy(&xdf_hvm_list_lock);
2858 }
2859 
2860 int
2861 xdf_hvm_connect(dev_info_t *dip)
2862 {
2863 	xdf_t	*vdp = (xdf_t *)ddi_get_driver_private(dip);
2864 	int	rv;
2865 
2866 	/* do cv_wait until connected or failed */
2867 	mutex_enter(&vdp->xdf_dev_lk);
2868 	rv = xdf_connect(vdp, B_TRUE);
2869 	mutex_exit(&vdp->xdf_dev_lk);
2870 	return ((rv == XD_READY) ? 0 : -1);
2871 }
2872 
2873 int
2874 xdf_hvm_setpgeom(dev_info_t *dip, cmlb_geom_t *geomp)
2875 {
2876 	xdf_t	*vdp = (xdf_t *)ddi_get_driver_private(dip);
2877 
2878 	/* sanity check the requested physical geometry */
2879 	mutex_enter(&vdp->xdf_dev_lk);
2880 	if ((geomp->g_secsize != XB_BSIZE) ||
2881 	    (geomp->g_capacity == 0)) {
2882 		mutex_exit(&vdp->xdf_dev_lk);
2883 		return (EINVAL);
2884 	}
2885 
2886 	/*
2887 	 * If we've already connected to the backend device then make sure
2888 	 * we're not defining a physical geometry larger than our backend
2889 	 * device.
2890 	 */
2891 	if ((vdp->xdf_xdev_nblocks != 0) &&
2892 	    (geomp->g_capacity > vdp->xdf_xdev_nblocks)) {
2893 		mutex_exit(&vdp->xdf_dev_lk);
2894 		return (EINVAL);
2895 	}
2896 
2897 	vdp->xdf_pgeom = *geomp;
2898 	mutex_exit(&vdp->xdf_dev_lk);
2899 
2900 	/* force a re-validation */
2901 	cmlb_invalidate(vdp->xdf_vd_lbl, NULL);
2902 
2903 	return (0);
2904 }
2905 
2906 #endif /* XPV_HVM_DRIVER */
2907