xref: /titanic_53/usr/src/uts/sun4v/io/vds.c (revision de3a53312897a4ba0d450636226db584e8b09648)
11ae08745Sheppo /*
21ae08745Sheppo  * CDDL HEADER START
31ae08745Sheppo  *
41ae08745Sheppo  * The contents of this file are subject to the terms of the
51ae08745Sheppo  * Common Development and Distribution License (the "License").
61ae08745Sheppo  * You may not use this file except in compliance with the License.
71ae08745Sheppo  *
81ae08745Sheppo  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91ae08745Sheppo  * or http://www.opensolaris.org/os/licensing.
101ae08745Sheppo  * See the License for the specific language governing permissions
111ae08745Sheppo  * and limitations under the License.
121ae08745Sheppo  *
131ae08745Sheppo  * When distributing Covered Code, include this CDDL HEADER in each
141ae08745Sheppo  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151ae08745Sheppo  * If applicable, add the following below this CDDL HEADER, with the
161ae08745Sheppo  * fields enclosed by brackets "[]" replaced with your own identifying
171ae08745Sheppo  * information: Portions Copyright [yyyy] [name of copyright owner]
181ae08745Sheppo  *
191ae08745Sheppo  * CDDL HEADER END
201ae08745Sheppo  */
211ae08745Sheppo 
221ae08745Sheppo /*
23edcc0754Sachartre  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
241ae08745Sheppo  * Use is subject to license terms.
251ae08745Sheppo  */
261ae08745Sheppo 
271ae08745Sheppo /*
281ae08745Sheppo  * Virtual disk server
291ae08745Sheppo  */
301ae08745Sheppo 
311ae08745Sheppo 
321ae08745Sheppo #include <sys/types.h>
331ae08745Sheppo #include <sys/conf.h>
344bac2208Snarayan #include <sys/crc32.h>
351ae08745Sheppo #include <sys/ddi.h>
361ae08745Sheppo #include <sys/dkio.h>
371ae08745Sheppo #include <sys/file.h>
3817cadca8Slm66018 #include <sys/fs/hsfs_isospec.h>
391ae08745Sheppo #include <sys/mdeg.h>
402f5224aeSachartre #include <sys/mhd.h>
411ae08745Sheppo #include <sys/modhash.h>
421ae08745Sheppo #include <sys/note.h>
431ae08745Sheppo #include <sys/pathname.h>
44205eeb1aSlm66018 #include <sys/sdt.h>
451ae08745Sheppo #include <sys/sunddi.h>
461ae08745Sheppo #include <sys/sunldi.h>
471ae08745Sheppo #include <sys/sysmacros.h>
481ae08745Sheppo #include <sys/vio_common.h>
4917cadca8Slm66018 #include <sys/vio_util.h>
501ae08745Sheppo #include <sys/vdsk_mailbox.h>
511ae08745Sheppo #include <sys/vdsk_common.h>
521ae08745Sheppo #include <sys/vtoc.h>
533c96341aSnarayan #include <sys/vfs.h>
543c96341aSnarayan #include <sys/stat.h>
5587a7269eSachartre #include <sys/scsi/impl/uscsi.h>
56bbfa0259Sha137994 #include <sys/ontrap.h>
57690555a1Sachartre #include <vm/seg_map.h>
581ae08745Sheppo 
59bae9e67eSachartre #define	ONE_TERABYTE	(1ULL << 40)
60bae9e67eSachartre 
611ae08745Sheppo /* Virtual disk server initialization flags */
62d10e4ef2Snarayan #define	VDS_LDI			0x01
63d10e4ef2Snarayan #define	VDS_MDEG		0x02
641ae08745Sheppo 
651ae08745Sheppo /* Virtual disk server tunable parameters */
663c96341aSnarayan #define	VDS_RETRIES		5
673c96341aSnarayan #define	VDS_LDC_DELAY		1000 /* 1 msecs */
683c96341aSnarayan #define	VDS_DEV_DELAY		10000000 /* 10 secs */
691ae08745Sheppo #define	VDS_NCHAINS		32
701ae08745Sheppo 
711ae08745Sheppo /* Identification parameters for MD, synthetic dkio(7i) structures, etc. */
721ae08745Sheppo #define	VDS_NAME		"virtual-disk-server"
731ae08745Sheppo 
741ae08745Sheppo #define	VD_NAME			"vd"
751ae08745Sheppo #define	VD_VOLUME_NAME		"vdisk"
761ae08745Sheppo #define	VD_ASCIILABEL		"Virtual Disk"
771ae08745Sheppo 
781ae08745Sheppo #define	VD_CHANNEL_ENDPOINT	"channel-endpoint"
791ae08745Sheppo #define	VD_ID_PROP		"id"
801ae08745Sheppo #define	VD_BLOCK_DEVICE_PROP	"vds-block-device"
81047ba61eSachartre #define	VD_BLOCK_DEVICE_OPTS	"vds-block-device-opts"
82445b4c2eSsb155480 #define	VD_REG_PROP		"reg"
831ae08745Sheppo 
841ae08745Sheppo /* Virtual disk initialization flags */
853c96341aSnarayan #define	VD_DISK_READY		0x01
863c96341aSnarayan #define	VD_LOCKING		0x02
873c96341aSnarayan #define	VD_LDC			0x04
883c96341aSnarayan #define	VD_DRING		0x08
893c96341aSnarayan #define	VD_SID			0x10
903c96341aSnarayan #define	VD_SEQ_NUM		0x20
91047ba61eSachartre #define	VD_SETUP_ERROR		0x40
921ae08745Sheppo 
93eba0cb4eSachartre /* Flags for writing to a vdisk which is a file */
94eba0cb4eSachartre #define	VD_FILE_WRITE_FLAGS	SM_ASYNC
95eba0cb4eSachartre 
9687a7269eSachartre /* Number of backup labels */
9787a7269eSachartre #define	VD_FILE_NUM_BACKUP	5
9887a7269eSachartre 
9987a7269eSachartre /* Timeout for SCSI I/O */
10087a7269eSachartre #define	VD_SCSI_RDWR_TIMEOUT	30	/* 30 secs */
10187a7269eSachartre 
102edcc0754Sachartre /* Maximum number of logical partitions */
103edcc0754Sachartre #define	VD_MAXPART	(NDKMAP + 1)
104edcc0754Sachartre 
1051ae08745Sheppo /*
1061ae08745Sheppo  * By Solaris convention, slice/partition 2 represents the entire disk;
1071ae08745Sheppo  * unfortunately, this convention does not appear to be codified.
1081ae08745Sheppo  */
1091ae08745Sheppo #define	VD_ENTIRE_DISK_SLICE	2
1101ae08745Sheppo 
111bae9e67eSachartre /* Logical block address for EFI */
112bae9e67eSachartre #define	VD_EFI_LBA_GPT		1	/* LBA of the GPT */
113bae9e67eSachartre #define	VD_EFI_LBA_GPE		2	/* LBA of the GPE */
114bae9e67eSachartre 
1158fce2fd6Sachartre /* Driver types */
1168fce2fd6Sachartre typedef enum vd_driver {
1178fce2fd6Sachartre 	VD_DRIVER_UNKNOWN = 0,	/* driver type unknown  */
1188fce2fd6Sachartre 	VD_DRIVER_DISK,		/* disk driver */
1198fce2fd6Sachartre 	VD_DRIVER_VOLUME	/* volume driver */
1208fce2fd6Sachartre } vd_driver_t;
1218fce2fd6Sachartre 
1228fce2fd6Sachartre #define	VD_DRIVER_NAME_LEN	64
1238fce2fd6Sachartre 
1248fce2fd6Sachartre #define	VDS_NUM_DRIVERS	(sizeof (vds_driver_types) / sizeof (vd_driver_type_t))
1258fce2fd6Sachartre 
1268fce2fd6Sachartre typedef struct vd_driver_type {
1278fce2fd6Sachartre 	char name[VD_DRIVER_NAME_LEN];	/* driver name */
1288fce2fd6Sachartre 	vd_driver_t type;		/* driver type (disk or volume) */
1298fce2fd6Sachartre } vd_driver_type_t;
1308fce2fd6Sachartre 
1318fce2fd6Sachartre /*
1328fce2fd6Sachartre  * There is no reliable way to determine if a device is representing a disk
1338fce2fd6Sachartre  * or a volume, especially with pseudo devices. So we maintain a list of well
1348fce2fd6Sachartre  * known drivers and the type of device they represent (either a disk or a
1358fce2fd6Sachartre  * volume).
1368fce2fd6Sachartre  *
1378fce2fd6Sachartre  * The list can be extended by adding a "driver-type-list" entry in vds.conf
1388fce2fd6Sachartre  * with the following syntax:
1398fce2fd6Sachartre  *
1408fce2fd6Sachartre  * 	driver-type-list="<driver>:<type>", ... ,"<driver>:<type>";
1418fce2fd6Sachartre  *
1428fce2fd6Sachartre  * Where:
1438fce2fd6Sachartre  *	<driver> is the name of a driver (limited to 64 characters)
1448fce2fd6Sachartre  *	<type> is either the string "disk" or "volume"
1458fce2fd6Sachartre  *
1468fce2fd6Sachartre  * Invalid entries in "driver-type-list" will be ignored.
1478fce2fd6Sachartre  *
1488fce2fd6Sachartre  * For example, the following line in vds.conf:
1498fce2fd6Sachartre  *
1508fce2fd6Sachartre  * 	driver-type-list="foo:disk","bar:volume";
1518fce2fd6Sachartre  *
1528fce2fd6Sachartre  * defines that "foo" is a disk driver, and driver "bar" is a volume driver.
1538fce2fd6Sachartre  *
1548fce2fd6Sachartre  * When a list is defined in vds.conf, it is checked before the built-in list
1558fce2fd6Sachartre  * (vds_driver_types[]) so that any definition from this list can be overriden
1568fce2fd6Sachartre  * using vds.conf.
1578fce2fd6Sachartre  */
1588fce2fd6Sachartre vd_driver_type_t vds_driver_types[] = {
1598fce2fd6Sachartre 	{ "dad",	VD_DRIVER_DISK },	/* Solaris */
1608fce2fd6Sachartre 	{ "did",	VD_DRIVER_DISK },	/* Sun Cluster */
1615b98b509Sachartre 	{ "emcp",	VD_DRIVER_DISK },	/* EMC Powerpath */
1628fce2fd6Sachartre 	{ "lofi",	VD_DRIVER_VOLUME },	/* Solaris */
1638fce2fd6Sachartre 	{ "md",		VD_DRIVER_VOLUME },	/* Solaris - SVM */
1648fce2fd6Sachartre 	{ "sd",		VD_DRIVER_DISK },	/* Solaris */
1658fce2fd6Sachartre 	{ "ssd",	VD_DRIVER_DISK },	/* Solaris */
1668fce2fd6Sachartre 	{ "vdc",	VD_DRIVER_DISK },	/* Solaris */
1678fce2fd6Sachartre 	{ "vxdmp",	VD_DRIVER_DISK },	/* Veritas */
1688fce2fd6Sachartre 	{ "vxio",	VD_DRIVER_VOLUME },	/* Veritas - VxVM */
1698fce2fd6Sachartre 	{ "zfs",	VD_DRIVER_VOLUME }	/* Solaris */
1708fce2fd6Sachartre };
1718fce2fd6Sachartre 
1721ae08745Sheppo /* Return a cpp token as a string */
1731ae08745Sheppo #define	STRINGIZE(token)	#token
1741ae08745Sheppo 
1751ae08745Sheppo /*
1761ae08745Sheppo  * Print a message prefixed with the current function name to the message log
1771ae08745Sheppo  * (and optionally to the console for verbose boots); these macros use cpp's
1781ae08745Sheppo  * concatenation of string literals and C99 variable-length-argument-list
1791ae08745Sheppo  * macros
1801ae08745Sheppo  */
1811ae08745Sheppo #define	PRN(...)	_PRN("?%s():  "__VA_ARGS__, "")
1821ae08745Sheppo #define	_PRN(format, ...)					\
1831ae08745Sheppo 	cmn_err(CE_CONT, format"%s", __func__, __VA_ARGS__)
1841ae08745Sheppo 
1851ae08745Sheppo /* Return a pointer to the "i"th vdisk dring element */
1861ae08745Sheppo #define	VD_DRING_ELEM(i)	((vd_dring_entry_t *)(void *)	\
1871ae08745Sheppo 	    (vd->dring + (i)*vd->descriptor_size))
1881ae08745Sheppo 
1891ae08745Sheppo /* Return the virtual disk client's type as a string (for use in messages) */
1901ae08745Sheppo #define	VD_CLIENT(vd)							\
1911ae08745Sheppo 	(((vd)->xfer_mode == VIO_DESC_MODE) ? "in-band client" :	\
192f0ca1d9aSsb155480 	    (((vd)->xfer_mode == VIO_DRING_MODE_V1_0) ? "dring client" :    \
1931ae08745Sheppo 		(((vd)->xfer_mode == 0) ? "null client" :		\
1941ae08745Sheppo 		    "unsupported client")))
1951ae08745Sheppo 
196690555a1Sachartre /* Read disk label from a disk on file */
197690555a1Sachartre #define	VD_FILE_LABEL_READ(vd, labelp) \
19887a7269eSachartre 	vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)labelp, \
199690555a1Sachartre 	    0, sizeof (struct dk_label))
200690555a1Sachartre 
201690555a1Sachartre /* Write disk label to a disk on file */
202690555a1Sachartre #define	VD_FILE_LABEL_WRITE(vd, labelp)	\
20387a7269eSachartre 	vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)labelp, \
204690555a1Sachartre 	    0, sizeof (struct dk_label))
205690555a1Sachartre 
2062f5224aeSachartre /* Message for disk access rights reset failure */
2072f5224aeSachartre #define	VD_RESET_ACCESS_FAILURE_MSG \
2082f5224aeSachartre 	"Fail to reset disk access rights for disk %s"
2092f5224aeSachartre 
210445b4c2eSsb155480 /*
211445b4c2eSsb155480  * Specification of an MD node passed to the MDEG to filter any
212445b4c2eSsb155480  * 'vport' nodes that do not belong to the specified node. This
213445b4c2eSsb155480  * template is copied for each vds instance and filled in with
214445b4c2eSsb155480  * the appropriate 'cfg-handle' value before being passed to the MDEG.
215445b4c2eSsb155480  */
216445b4c2eSsb155480 static mdeg_prop_spec_t	vds_prop_template[] = {
217445b4c2eSsb155480 	{ MDET_PROP_STR,	"name",		VDS_NAME },
218445b4c2eSsb155480 	{ MDET_PROP_VAL,	"cfg-handle",	NULL },
219445b4c2eSsb155480 	{ MDET_LIST_END,	NULL, 		NULL }
220445b4c2eSsb155480 };
221445b4c2eSsb155480 
222445b4c2eSsb155480 #define	VDS_SET_MDEG_PROP_INST(specp, val) (specp)[1].ps_val = (val);
223445b4c2eSsb155480 
224445b4c2eSsb155480 /*
225445b4c2eSsb155480  * Matching criteria passed to the MDEG to register interest
226445b4c2eSsb155480  * in changes to 'virtual-device-port' nodes identified by their
227445b4c2eSsb155480  * 'id' property.
228445b4c2eSsb155480  */
229445b4c2eSsb155480 static md_prop_match_t	vd_prop_match[] = {
230445b4c2eSsb155480 	{ MDET_PROP_VAL,	VD_ID_PROP },
231445b4c2eSsb155480 	{ MDET_LIST_END,	NULL }
232445b4c2eSsb155480 };
233445b4c2eSsb155480 
234445b4c2eSsb155480 static mdeg_node_match_t vd_match = {"virtual-device-port",
235445b4c2eSsb155480 				    vd_prop_match};
236445b4c2eSsb155480 
237047ba61eSachartre /*
238047ba61eSachartre  * Options for the VD_BLOCK_DEVICE_OPTS property.
239047ba61eSachartre  */
240047ba61eSachartre #define	VD_OPT_RDONLY		0x1	/* read-only  */
241047ba61eSachartre #define	VD_OPT_SLICE		0x2	/* single slice */
242047ba61eSachartre #define	VD_OPT_EXCLUSIVE	0x4	/* exclusive access */
243047ba61eSachartre 
244047ba61eSachartre #define	VD_OPTION_NLEN	128
245047ba61eSachartre 
246047ba61eSachartre typedef struct vd_option {
247047ba61eSachartre 	char vdo_name[VD_OPTION_NLEN];
248047ba61eSachartre 	uint64_t vdo_value;
249047ba61eSachartre } vd_option_t;
250047ba61eSachartre 
251047ba61eSachartre vd_option_t vd_bdev_options[] = {
252047ba61eSachartre 	{ "ro",		VD_OPT_RDONLY },
253047ba61eSachartre 	{ "slice", 	VD_OPT_SLICE },
254047ba61eSachartre 	{ "excl",	VD_OPT_EXCLUSIVE }
255047ba61eSachartre };
256047ba61eSachartre 
2571ae08745Sheppo /* Debugging macros */
2581ae08745Sheppo #ifdef DEBUG
2593af08d82Slm66018 
2603af08d82Slm66018 static int	vd_msglevel = 0;
2613af08d82Slm66018 
2621ae08745Sheppo #define	PR0 if (vd_msglevel > 0)	PRN
2631ae08745Sheppo #define	PR1 if (vd_msglevel > 1)	PRN
2641ae08745Sheppo #define	PR2 if (vd_msglevel > 2)	PRN
2651ae08745Sheppo 
2661ae08745Sheppo #define	VD_DUMP_DRING_ELEM(elem)					\
2673c96341aSnarayan 	PR0("dst:%x op:%x st:%u nb:%lx addr:%lx ncook:%u\n",		\
2681ae08745Sheppo 	    elem->hdr.dstate,						\
2691ae08745Sheppo 	    elem->payload.operation,					\
2701ae08745Sheppo 	    elem->payload.status,					\
2711ae08745Sheppo 	    elem->payload.nbytes,					\
2721ae08745Sheppo 	    elem->payload.addr,						\
2731ae08745Sheppo 	    elem->payload.ncookies);
2741ae08745Sheppo 
2753af08d82Slm66018 char *
2763af08d82Slm66018 vd_decode_state(int state)
2773af08d82Slm66018 {
2783af08d82Slm66018 	char *str;
2793af08d82Slm66018 
2803af08d82Slm66018 #define	CASE_STATE(_s)	case _s: str = #_s; break;
2813af08d82Slm66018 
2823af08d82Slm66018 	switch (state) {
2833af08d82Slm66018 	CASE_STATE(VD_STATE_INIT)
2843af08d82Slm66018 	CASE_STATE(VD_STATE_VER)
2853af08d82Slm66018 	CASE_STATE(VD_STATE_ATTR)
2863af08d82Slm66018 	CASE_STATE(VD_STATE_DRING)
2873af08d82Slm66018 	CASE_STATE(VD_STATE_RDX)
2883af08d82Slm66018 	CASE_STATE(VD_STATE_DATA)
2893af08d82Slm66018 	default: str = "unknown"; break;
2903af08d82Slm66018 	}
2913af08d82Slm66018 
2923af08d82Slm66018 #undef CASE_STATE
2933af08d82Slm66018 
2943af08d82Slm66018 	return (str);
2953af08d82Slm66018 }
2963af08d82Slm66018 
2973af08d82Slm66018 void
2983af08d82Slm66018 vd_decode_tag(vio_msg_t *msg)
2993af08d82Slm66018 {
3003af08d82Slm66018 	char *tstr, *sstr, *estr;
3013af08d82Slm66018 
3023af08d82Slm66018 #define	CASE_TYPE(_s)	case _s: tstr = #_s; break;
3033af08d82Slm66018 
3043af08d82Slm66018 	switch (msg->tag.vio_msgtype) {
3053af08d82Slm66018 	CASE_TYPE(VIO_TYPE_CTRL)
3063af08d82Slm66018 	CASE_TYPE(VIO_TYPE_DATA)
3073af08d82Slm66018 	CASE_TYPE(VIO_TYPE_ERR)
3083af08d82Slm66018 	default: tstr = "unknown"; break;
3093af08d82Slm66018 	}
3103af08d82Slm66018 
3113af08d82Slm66018 #undef CASE_TYPE
3123af08d82Slm66018 
3133af08d82Slm66018 #define	CASE_SUBTYPE(_s) case _s: sstr = #_s; break;
3143af08d82Slm66018 
3153af08d82Slm66018 	switch (msg->tag.vio_subtype) {
3163af08d82Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_INFO)
3173af08d82Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_ACK)
3183af08d82Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_NACK)
3193af08d82Slm66018 	default: sstr = "unknown"; break;
3203af08d82Slm66018 	}
3213af08d82Slm66018 
3223af08d82Slm66018 #undef CASE_SUBTYPE
3233af08d82Slm66018 
3243af08d82Slm66018 #define	CASE_ENV(_s)	case _s: estr = #_s; break;
3253af08d82Slm66018 
3263af08d82Slm66018 	switch (msg->tag.vio_subtype_env) {
3273af08d82Slm66018 	CASE_ENV(VIO_VER_INFO)
3283af08d82Slm66018 	CASE_ENV(VIO_ATTR_INFO)
3293af08d82Slm66018 	CASE_ENV(VIO_DRING_REG)
3303af08d82Slm66018 	CASE_ENV(VIO_DRING_UNREG)
3313af08d82Slm66018 	CASE_ENV(VIO_RDX)
3323af08d82Slm66018 	CASE_ENV(VIO_PKT_DATA)
3333af08d82Slm66018 	CASE_ENV(VIO_DESC_DATA)
3343af08d82Slm66018 	CASE_ENV(VIO_DRING_DATA)
3353af08d82Slm66018 	default: estr = "unknown"; break;
3363af08d82Slm66018 	}
3373af08d82Slm66018 
3383af08d82Slm66018 #undef CASE_ENV
3393af08d82Slm66018 
3403af08d82Slm66018 	PR1("(%x/%x/%x) message : (%s/%s/%s)",
3413af08d82Slm66018 	    msg->tag.vio_msgtype, msg->tag.vio_subtype,
3423af08d82Slm66018 	    msg->tag.vio_subtype_env, tstr, sstr, estr);
3433af08d82Slm66018 }
3443af08d82Slm66018 
3451ae08745Sheppo #else	/* !DEBUG */
3463af08d82Slm66018 
3471ae08745Sheppo #define	PR0(...)
3481ae08745Sheppo #define	PR1(...)
3491ae08745Sheppo #define	PR2(...)
3501ae08745Sheppo 
3511ae08745Sheppo #define	VD_DUMP_DRING_ELEM(elem)
3521ae08745Sheppo 
3533af08d82Slm66018 #define	vd_decode_state(_s)	(NULL)
3543af08d82Slm66018 #define	vd_decode_tag(_s)	(NULL)
3553af08d82Slm66018 
3561ae08745Sheppo #endif	/* DEBUG */
3571ae08745Sheppo 
3581ae08745Sheppo 
359d10e4ef2Snarayan /*
360d10e4ef2Snarayan  * Soft state structure for a vds instance
361d10e4ef2Snarayan  */
3621ae08745Sheppo typedef struct vds {
3631ae08745Sheppo 	uint_t		initialized;	/* driver inst initialization flags */
3641ae08745Sheppo 	dev_info_t	*dip;		/* driver inst devinfo pointer */
3651ae08745Sheppo 	ldi_ident_t	ldi_ident;	/* driver's identifier for LDI */
3661ae08745Sheppo 	mod_hash_t	*vd_table;	/* table of virtual disks served */
367445b4c2eSsb155480 	mdeg_node_spec_t *ispecp;	/* mdeg node specification */
3681ae08745Sheppo 	mdeg_handle_t	mdeg;		/* handle for MDEG operations  */
3698fce2fd6Sachartre 	vd_driver_type_t *driver_types;	/* extra driver types (from vds.conf) */
3708fce2fd6Sachartre 	int 		num_drivers;	/* num of extra driver types */
3711ae08745Sheppo } vds_t;
3721ae08745Sheppo 
373d10e4ef2Snarayan /*
374d10e4ef2Snarayan  * Types of descriptor-processing tasks
375d10e4ef2Snarayan  */
376d10e4ef2Snarayan typedef enum vd_task_type {
377d10e4ef2Snarayan 	VD_NONFINAL_RANGE_TASK,	/* task for intermediate descriptor in range */
378d10e4ef2Snarayan 	VD_FINAL_RANGE_TASK,	/* task for last in a range of descriptors */
379d10e4ef2Snarayan } vd_task_type_t;
380d10e4ef2Snarayan 
381d10e4ef2Snarayan /*
382d10e4ef2Snarayan  * Structure describing the task for processing a descriptor
383d10e4ef2Snarayan  */
384d10e4ef2Snarayan typedef struct vd_task {
385d10e4ef2Snarayan 	struct vd		*vd;		/* vd instance task is for */
386d10e4ef2Snarayan 	vd_task_type_t		type;		/* type of descriptor task */
387d10e4ef2Snarayan 	int			index;		/* dring elem index for task */
388d10e4ef2Snarayan 	vio_msg_t		*msg;		/* VIO message task is for */
389d10e4ef2Snarayan 	size_t			msglen;		/* length of message content */
390d10e4ef2Snarayan 	vd_dring_payload_t	*request;	/* request task will perform */
391d10e4ef2Snarayan 	struct buf		buf;		/* buf(9s) for I/O request */
3924bac2208Snarayan 	ldc_mem_handle_t	mhdl;		/* task memory handle */
393205eeb1aSlm66018 	int			status;		/* status of processing task */
394205eeb1aSlm66018 	int	(*completef)(struct vd_task *task); /* completion func ptr */
395d10e4ef2Snarayan } vd_task_t;
396d10e4ef2Snarayan 
397d10e4ef2Snarayan /*
398d10e4ef2Snarayan  * Soft state structure for a virtual disk instance
399d10e4ef2Snarayan  */
4001ae08745Sheppo typedef struct vd {
4011ae08745Sheppo 	uint_t			initialized;	/* vdisk initialization flags */
40217cadca8Slm66018 	uint64_t		operations;	/* bitmask of VD_OPs exported */
40317cadca8Slm66018 	vio_ver_t		version;	/* ver negotiated with client */
4041ae08745Sheppo 	vds_t			*vds;		/* server for this vdisk */
405d10e4ef2Snarayan 	ddi_taskq_t		*startq;	/* queue for I/O start tasks */
406d10e4ef2Snarayan 	ddi_taskq_t		*completionq;	/* queue for completion tasks */
4071ae08745Sheppo 	ldi_handle_t		ldi_handle[V_NUMPAR];	/* LDI slice handles */
4083c96341aSnarayan 	char			device_path[MAXPATHLEN + 1]; /* vdisk device */
4091ae08745Sheppo 	dev_t			dev[V_NUMPAR];	/* dev numbers for slices */
410047ba61eSachartre 	int			open_flags;	/* open flags */
411bae9e67eSachartre 	uint_t			nslices;	/* number of slices we export */
4121ae08745Sheppo 	size_t			vdisk_size;	/* number of blocks in vdisk */
41317cadca8Slm66018 	size_t			vdisk_block_size; /* size of each vdisk block */
4141ae08745Sheppo 	vd_disk_type_t		vdisk_type;	/* slice or entire disk */
4154bac2208Snarayan 	vd_disk_label_t		vdisk_label;	/* EFI or VTOC label */
41617cadca8Slm66018 	vd_media_t		vdisk_media;	/* media type of backing dev. */
41717cadca8Slm66018 	boolean_t		is_atapi_dev;	/* Is this an IDE CD-ROM dev? */
418e1ebb9ecSlm66018 	ushort_t		max_xfer_sz;	/* max xfer size in DEV_BSIZE */
41917cadca8Slm66018 	size_t			block_size;	/* blk size of actual device */
4208fce2fd6Sachartre 	boolean_t		volume;		/* is vDisk backed by volume */
42117cadca8Slm66018 	boolean_t		file;		/* is vDisk backed by a file? */
4222f5224aeSachartre 	boolean_t		scsi;		/* is vDisk backed by scsi? */
4233c96341aSnarayan 	vnode_t			*file_vnode;	/* file vnode */
4243c96341aSnarayan 	size_t			file_size;	/* file size */
42587a7269eSachartre 	ddi_devid_t		file_devid;	/* devid for disk image */
426edcc0754Sachartre 	int			efi_reserved;	/* EFI reserved slice */
427bae9e67eSachartre 	caddr_t			flabel;		/* fake label for slice type */
428bae9e67eSachartre 	uint_t			flabel_size;	/* fake label size */
429bae9e67eSachartre 	uint_t			flabel_limit;	/* limit of the fake label */
4301ae08745Sheppo 	struct dk_geom		dk_geom;	/* synthetic for slice type */
4311ae08745Sheppo 	struct vtoc		vtoc;		/* synthetic for slice type */
432edcc0754Sachartre 	vd_slice_t		slices[VD_MAXPART]; /* logical partitions */
4332f5224aeSachartre 	boolean_t		ownership;	/* disk ownership status */
4341ae08745Sheppo 	ldc_status_t		ldc_state;	/* LDC connection state */
4351ae08745Sheppo 	ldc_handle_t		ldc_handle;	/* handle for LDC comm */
4361ae08745Sheppo 	size_t			max_msglen;	/* largest LDC message len */
4371ae08745Sheppo 	vd_state_t		state;		/* client handshake state */
4381ae08745Sheppo 	uint8_t			xfer_mode;	/* transfer mode with client */
4391ae08745Sheppo 	uint32_t		sid;		/* client's session ID */
4401ae08745Sheppo 	uint64_t		seq_num;	/* message sequence number */
4411ae08745Sheppo 	uint64_t		dring_ident;	/* identifier of dring */
4421ae08745Sheppo 	ldc_dring_handle_t	dring_handle;	/* handle for dring ops */
4431ae08745Sheppo 	uint32_t		descriptor_size;	/* num bytes in desc */
4441ae08745Sheppo 	uint32_t		dring_len;	/* number of dring elements */
445bbfa0259Sha137994 	uint8_t			dring_mtype;	/* dring mem map type */
4461ae08745Sheppo 	caddr_t			dring;		/* address of dring */
4473af08d82Slm66018 	caddr_t			vio_msgp;	/* vio msg staging buffer */
448d10e4ef2Snarayan 	vd_task_t		inband_task;	/* task for inband descriptor */
449d10e4ef2Snarayan 	vd_task_t		*dring_task;	/* tasks dring elements */
450d10e4ef2Snarayan 
451d10e4ef2Snarayan 	kmutex_t		lock;		/* protects variables below */
452d10e4ef2Snarayan 	boolean_t		enabled;	/* is vdisk enabled? */
453d10e4ef2Snarayan 	boolean_t		reset_state;	/* reset connection state? */
454d10e4ef2Snarayan 	boolean_t		reset_ldc;	/* reset LDC channel? */
4551ae08745Sheppo } vd_t;
4561ae08745Sheppo 
457bae9e67eSachartre /*
458bae9e67eSachartre  * Macros to manipulate the fake label (flabel) for single slice disks.
459bae9e67eSachartre  *
460bae9e67eSachartre  * If we fake a VTOC label then the fake label consists of only one block
461bae9e67eSachartre  * containing the VTOC label (struct dk_label).
462bae9e67eSachartre  *
463bae9e67eSachartre  * If we fake an EFI label then the fake label consists of a blank block
464bae9e67eSachartre  * followed by a GPT (efi_gpt_t) and a GPE (efi_gpe_t).
465bae9e67eSachartre  *
466bae9e67eSachartre  */
467bae9e67eSachartre #define	VD_LABEL_VTOC_SIZE					\
468bae9e67eSachartre 	P2ROUNDUP(sizeof (struct dk_label), DEV_BSIZE)
469bae9e67eSachartre 
470bae9e67eSachartre #define	VD_LABEL_EFI_SIZE					\
471bae9e67eSachartre 	P2ROUNDUP(DEV_BSIZE + sizeof (efi_gpt_t) + 		\
472bae9e67eSachartre 	    sizeof (efi_gpe_t) * VD_MAXPART, DEV_BSIZE)
473bae9e67eSachartre 
474bae9e67eSachartre #define	VD_LABEL_VTOC(vd)	\
475bae9e67eSachartre 		((struct dk_label *)((vd)->flabel))
476bae9e67eSachartre 
477bae9e67eSachartre #define	VD_LABEL_EFI_GPT(vd)	\
478bae9e67eSachartre 		((efi_gpt_t *)((vd)->flabel + DEV_BSIZE))
479bae9e67eSachartre #define	VD_LABEL_EFI_GPE(vd)	\
480bae9e67eSachartre 		((efi_gpe_t *)((vd)->flabel + DEV_BSIZE + sizeof (efi_gpt_t)))
481bae9e67eSachartre 
482bae9e67eSachartre 
4831ae08745Sheppo typedef struct vds_operation {
4843af08d82Slm66018 	char	*namep;
4851ae08745Sheppo 	uint8_t	operation;
486d10e4ef2Snarayan 	int	(*start)(vd_task_t *task);
487205eeb1aSlm66018 	int	(*complete)(vd_task_t *task);
4881ae08745Sheppo } vds_operation_t;
4891ae08745Sheppo 
4900a55fbb7Slm66018 typedef struct vd_ioctl {
4910a55fbb7Slm66018 	uint8_t		operation;		/* vdisk operation */
4920a55fbb7Slm66018 	const char	*operation_name;	/* vdisk operation name */
4930a55fbb7Slm66018 	size_t		nbytes;			/* size of operation buffer */
4940a55fbb7Slm66018 	int		cmd;			/* corresponding ioctl cmd */
4950a55fbb7Slm66018 	const char	*cmd_name;		/* ioctl cmd name */
4960a55fbb7Slm66018 	void		*arg;			/* ioctl cmd argument */
4970a55fbb7Slm66018 	/* convert input vd_buf to output ioctl_arg */
4982f5224aeSachartre 	int		(*copyin)(void *vd_buf, size_t, void *ioctl_arg);
4990a55fbb7Slm66018 	/* convert input ioctl_arg to output vd_buf */
5000a55fbb7Slm66018 	void		(*copyout)(void *ioctl_arg, void *vd_buf);
501047ba61eSachartre 	/* write is true if the operation writes any data to the backend */
502047ba61eSachartre 	boolean_t	write;
5030a55fbb7Slm66018 } vd_ioctl_t;
5040a55fbb7Slm66018 
5050a55fbb7Slm66018 /* Define trivial copyin/copyout conversion function flag */
5062f5224aeSachartre #define	VD_IDENTITY_IN	((int (*)(void *, size_t, void *))-1)
5072f5224aeSachartre #define	VD_IDENTITY_OUT	((void (*)(void *, void *))-1)
5081ae08745Sheppo 
5091ae08745Sheppo 
5103c96341aSnarayan static int	vds_ldc_retries = VDS_RETRIES;
5113af08d82Slm66018 static int	vds_ldc_delay = VDS_LDC_DELAY;
5123c96341aSnarayan static int	vds_dev_retries = VDS_RETRIES;
5133c96341aSnarayan static int	vds_dev_delay = VDS_DEV_DELAY;
5141ae08745Sheppo static void	*vds_state;
5151ae08745Sheppo 
516eba0cb4eSachartre static uint_t	vd_file_write_flags = VD_FILE_WRITE_FLAGS;
517eba0cb4eSachartre 
51887a7269eSachartre static short	vd_scsi_rdwr_timeout = VD_SCSI_RDWR_TIMEOUT;
5192f5224aeSachartre static int	vd_scsi_debug = USCSI_SILENT;
5202f5224aeSachartre 
5212f5224aeSachartre /*
5222f5224aeSachartre  * Tunable to define the behavior of the service domain if the vdisk server
5232f5224aeSachartre  * fails to reset disk exclusive access when a LDC channel is reset. When a
5242f5224aeSachartre  * LDC channel is reset the vdisk server will try to reset disk exclusive
5252f5224aeSachartre  * access by releasing any SCSI-2 reservation or resetting the disk. If these
5262f5224aeSachartre  * actions fail then the default behavior (vd_reset_access_failure = 0) is to
5272f5224aeSachartre  * print a warning message. This default behavior can be changed by setting
5282f5224aeSachartre  * the vd_reset_access_failure variable to A_REBOOT (= 0x1) and that will
5292f5224aeSachartre  * cause the service domain to reboot, or A_DUMP (= 0x5) and that will cause
5302f5224aeSachartre  * the service domain to panic. In both cases, the reset of the service domain
5312f5224aeSachartre  * should trigger a reset SCSI buses and hopefully clear any SCSI-2 reservation.
5322f5224aeSachartre  */
5332f5224aeSachartre static int 	vd_reset_access_failure = 0;
5342f5224aeSachartre 
5352f5224aeSachartre /*
5362f5224aeSachartre  * Tunable for backward compatibility. When this variable is set to B_TRUE,
5372f5224aeSachartre  * all disk volumes (ZFS, SVM, VxvM volumes) will be exported as single
5382f5224aeSachartre  * slice disks whether or not they have the "slice" option set. This is
5392f5224aeSachartre  * to provide a simple backward compatibility mechanism when upgrading
5402f5224aeSachartre  * the vds driver and using a domain configuration created before the
5412f5224aeSachartre  * "slice" option was available.
5422f5224aeSachartre  */
5432f5224aeSachartre static boolean_t vd_volume_force_slice = B_FALSE;
54487a7269eSachartre 
5450a55fbb7Slm66018 /*
54666cfcfbeSachartre  * The label of disk images created with some earlier versions of the virtual
54766cfcfbeSachartre  * disk software is not entirely correct and have an incorrect v_sanity field
54866cfcfbeSachartre  * (usually 0) instead of VTOC_SANE. This creates a compatibility problem with
54966cfcfbeSachartre  * these images because we are now validating that the disk label (and the
55066cfcfbeSachartre  * sanity) is correct when a disk image is opened.
55166cfcfbeSachartre  *
55266cfcfbeSachartre  * This tunable is set to false to not validate the sanity field and ensure
55366cfcfbeSachartre  * compatibility. If the tunable is set to true, we will do a strict checking
55466cfcfbeSachartre  * of the sanity but this can create compatibility problems with old disk
55566cfcfbeSachartre  * images.
55666cfcfbeSachartre  */
55766cfcfbeSachartre static boolean_t vd_file_validate_sanity = B_FALSE;
55866cfcfbeSachartre 
55966cfcfbeSachartre /*
560bbfa0259Sha137994  * Enables the use of LDC_DIRECT_MAP when mapping in imported descriptor rings.
561bbfa0259Sha137994  */
562bbfa0259Sha137994 static boolean_t vd_direct_mapped_drings = B_TRUE;
563bbfa0259Sha137994 
564bbfa0259Sha137994 /*
565bae9e67eSachartre  * When a backend is exported as a single-slice disk then we entirely fake
566bae9e67eSachartre  * its disk label. So it can be exported either with a VTOC label or with
567bae9e67eSachartre  * an EFI label. If vd_slice_label is set to VD_DISK_LABEL_VTOC then all
568bae9e67eSachartre  * single-slice disks will be exported with a VTOC label; and if it is set
569bae9e67eSachartre  * to VD_DISK_LABEL_EFI then all single-slice disks will be exported with
570bae9e67eSachartre  * an EFI label.
571bae9e67eSachartre  *
572bae9e67eSachartre  * If vd_slice_label is set to VD_DISK_LABEL_UNK and the backend is a disk
573bae9e67eSachartre  * or volume device then it will be exported with the same type of label as
574bae9e67eSachartre  * defined on the device. Otherwise if the backend is a file then it will
575bae9e67eSachartre  * exported with the disk label type set in the vd_file_slice_label variable.
576bae9e67eSachartre  *
577bae9e67eSachartre  * Note that if the backend size is greater than 1TB then it will always be
578bae9e67eSachartre  * exported with an EFI label no matter what the setting is.
579bae9e67eSachartre  */
580bae9e67eSachartre static vd_disk_label_t vd_slice_label = VD_DISK_LABEL_UNK;
581bae9e67eSachartre 
582bae9e67eSachartre static vd_disk_label_t vd_file_slice_label = VD_DISK_LABEL_VTOC;
583bae9e67eSachartre 
584bae9e67eSachartre /*
585bae9e67eSachartre  * Tunable for backward compatibility. If this variable is set to B_TRUE then
586bae9e67eSachartre  * single-slice disks are exported as disks with only one slice instead of
587bae9e67eSachartre  * faking a complete disk partitioning.
588bae9e67eSachartre  */
589bae9e67eSachartre static boolean_t vd_slice_single_slice = B_FALSE;
590bae9e67eSachartre 
591bae9e67eSachartre /*
5920a55fbb7Slm66018  * Supported protocol version pairs, from highest (newest) to lowest (oldest)
5930a55fbb7Slm66018  *
5940a55fbb7Slm66018  * Each supported major version should appear only once, paired with (and only
5950a55fbb7Slm66018  * with) its highest supported minor version number (as the protocol requires
5960a55fbb7Slm66018  * supporting all lower minor version numbers as well)
5970a55fbb7Slm66018  */
59817cadca8Slm66018 static const vio_ver_t	vds_version[] = {{1, 1}};
5990a55fbb7Slm66018 static const size_t	vds_num_versions =
6000a55fbb7Slm66018     sizeof (vds_version)/sizeof (vds_version[0]);
6010a55fbb7Slm66018 
6023af08d82Slm66018 static void vd_free_dring_task(vd_t *vdp);
6033c96341aSnarayan static int vd_setup_vd(vd_t *vd);
604047ba61eSachartre static int vd_setup_single_slice_disk(vd_t *vd);
605*de3a5331SRamesh Chitrothu static int vd_backend_check_size(vd_t *vd);
6063c96341aSnarayan static boolean_t vd_enabled(vd_t *vd);
60778fcd0a1Sachartre static ushort_t vd_lbl2cksum(struct dk_label *label);
60878fcd0a1Sachartre static int vd_file_validate_geometry(vd_t *vd);
60917cadca8Slm66018 static boolean_t vd_file_is_iso_image(vd_t *vd);
61017cadca8Slm66018 static void vd_set_exported_operations(vd_t *vd);
6112f5224aeSachartre static void vd_reset_access(vd_t *vd);
612edcc0754Sachartre static int vd_backend_ioctl(vd_t *vd, int cmd, caddr_t arg);
613edcc0754Sachartre static int vds_efi_alloc_and_read(vd_t *, efi_gpt_t **, efi_gpe_t **);
614edcc0754Sachartre static void vds_efi_free(vd_t *, efi_gpt_t *, efi_gpe_t *);
6158fce2fd6Sachartre static void vds_driver_types_free(vds_t *vds);
616bae9e67eSachartre static void vd_vtocgeom_to_label(struct vtoc *vtoc, struct dk_geom *geom,
617bae9e67eSachartre     struct dk_label *label);
618bae9e67eSachartre static void vd_label_to_vtocgeom(struct dk_label *label, struct vtoc *vtoc,
619bae9e67eSachartre     struct dk_geom *geom);
620bae9e67eSachartre static boolean_t vd_slice_geom_isvalid(vd_t *vd, struct dk_geom *geom);
621bae9e67eSachartre static boolean_t vd_slice_vtoc_isvalid(vd_t *vd, struct vtoc *vtoc);
622bae9e67eSachartre 
623bae9e67eSachartre extern int is_pseudo_device(dev_info_t *);
624bae9e67eSachartre 
625bae9e67eSachartre /*
626bae9e67eSachartre  * Function:
627bae9e67eSachartre  *	vd_get_readable_size
628bae9e67eSachartre  *
629bae9e67eSachartre  * Description:
630bae9e67eSachartre  * 	Convert a given size in bytes to a human readable format in
631bae9e67eSachartre  * 	kilobytes, megabytes, gigabytes or terabytes.
632bae9e67eSachartre  *
633bae9e67eSachartre  * Parameters:
634bae9e67eSachartre  *	full_size	- the size to convert in bytes.
635bae9e67eSachartre  *	size		- the converted size.
636bae9e67eSachartre  *	unit		- the unit of the converted size: 'K' (kilobyte),
637bae9e67eSachartre  *			  'M' (Megabyte), 'G' (Gigabyte), 'T' (Terabyte).
638bae9e67eSachartre  *
639bae9e67eSachartre  * Return Code:
640bae9e67eSachartre  *	none
641bae9e67eSachartre  */
642bae9e67eSachartre void
643bae9e67eSachartre vd_get_readable_size(size_t full_size, size_t *size, char *unit)
644bae9e67eSachartre {
645bae9e67eSachartre 	if (full_size < (1ULL << 20)) {
646bae9e67eSachartre 		*size = full_size >> 10;
647bae9e67eSachartre 		*unit = 'K'; /* Kilobyte */
648bae9e67eSachartre 	} else if (full_size < (1ULL << 30)) {
649bae9e67eSachartre 		*size = full_size >> 20;
650bae9e67eSachartre 		*unit = 'M'; /* Megabyte */
651bae9e67eSachartre 	} else if (full_size < (1ULL << 40)) {
652bae9e67eSachartre 		*size = full_size >> 30;
653bae9e67eSachartre 		*unit = 'G'; /* Gigabyte */
654bae9e67eSachartre 	} else {
655bae9e67eSachartre 		*size = full_size >> 40;
656bae9e67eSachartre 		*unit = 'T'; /* Terabyte */
657bae9e67eSachartre 	}
658bae9e67eSachartre }
659047ba61eSachartre 
660690555a1Sachartre /*
661690555a1Sachartre  * Function:
662690555a1Sachartre  *	vd_file_rw
663690555a1Sachartre  *
664690555a1Sachartre  * Description:
665690555a1Sachartre  * 	Read or write to a disk on file.
666690555a1Sachartre  *
667690555a1Sachartre  * Parameters:
668690555a1Sachartre  *	vd		- disk on which the operation is performed.
669690555a1Sachartre  *	slice		- slice on which the operation is performed,
67087a7269eSachartre  *			  VD_SLICE_NONE indicates that the operation
67187a7269eSachartre  *			  is done using an absolute disk offset.
672690555a1Sachartre  *	operation	- operation to execute: read (VD_OP_BREAD) or
673690555a1Sachartre  *			  write (VD_OP_BWRITE).
674690555a1Sachartre  *	data		- buffer where data are read to or written from.
675690555a1Sachartre  *	blk		- starting block for the operation.
676690555a1Sachartre  *	len		- number of bytes to read or write.
677690555a1Sachartre  *
678690555a1Sachartre  * Return Code:
679690555a1Sachartre  *	n >= 0		- success, n indicates the number of bytes read
680690555a1Sachartre  *			  or written.
681690555a1Sachartre  *	-1		- error.
682690555a1Sachartre  */
683690555a1Sachartre static ssize_t
684690555a1Sachartre vd_file_rw(vd_t *vd, int slice, int operation, caddr_t data, size_t blk,
685690555a1Sachartre     size_t len)
686690555a1Sachartre {
687690555a1Sachartre 	caddr_t	maddr;
688690555a1Sachartre 	size_t offset, maxlen, moffset, mlen, n;
689690555a1Sachartre 	uint_t smflags;
690690555a1Sachartre 	enum seg_rw srw;
691690555a1Sachartre 
692690555a1Sachartre 	ASSERT(vd->file);
693690555a1Sachartre 	ASSERT(len > 0);
694690555a1Sachartre 
695047ba61eSachartre 	/*
696047ba61eSachartre 	 * If a file is exported as a slice then we don't care about the vtoc.
697047ba61eSachartre 	 * In that case, the vtoc is a fake mainly to make newfs happy and we
698047ba61eSachartre 	 * handle any I/O as a raw disk access so that we can have access to the
699047ba61eSachartre 	 * entire backend.
700047ba61eSachartre 	 */
701047ba61eSachartre 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE || slice == VD_SLICE_NONE) {
702690555a1Sachartre 		/* raw disk access */
703690555a1Sachartre 		offset = blk * DEV_BSIZE;
704bae9e67eSachartre 		if (offset >= vd->file_size) {
705bae9e67eSachartre 			/* offset past the end of the disk */
706bae9e67eSachartre 			PR0("offset (0x%lx) >= fsize (0x%lx)",
707bae9e67eSachartre 			    offset, vd->file_size);
708bae9e67eSachartre 			return (0);
709bae9e67eSachartre 		}
710bae9e67eSachartre 		maxlen = vd->file_size - offset;
711690555a1Sachartre 	} else {
712690555a1Sachartre 		ASSERT(slice >= 0 && slice < V_NUMPAR);
71378fcd0a1Sachartre 
71417cadca8Slm66018 		/*
71517cadca8Slm66018 		 * v1.0 vDisk clients depended on the server not verifying
71617cadca8Slm66018 		 * the label of a unformatted disk.  This "feature" is
71717cadca8Slm66018 		 * maintained for backward compatibility but all versions
71817cadca8Slm66018 		 * from v1.1 onwards must do the right thing.
71917cadca8Slm66018 		 */
72078fcd0a1Sachartre 		if (vd->vdisk_label == VD_DISK_LABEL_UNK &&
721edcc0754Sachartre 		    vio_ver_is_supported(vd->version, 1, 1)) {
722edcc0754Sachartre 			(void) vd_file_validate_geometry(vd);
723edcc0754Sachartre 			if (vd->vdisk_label == VD_DISK_LABEL_UNK) {
724edcc0754Sachartre 				PR0("Unknown disk label, can't do I/O "
725edcc0754Sachartre 				    "from slice %d", slice);
72678fcd0a1Sachartre 				return (-1);
72778fcd0a1Sachartre 			}
728edcc0754Sachartre 		}
72978fcd0a1Sachartre 
730edcc0754Sachartre 		if (vd->vdisk_label == VD_DISK_LABEL_VTOC) {
731edcc0754Sachartre 			ASSERT(vd->vtoc.v_sectorsz == DEV_BSIZE);
732edcc0754Sachartre 		} else {
733edcc0754Sachartre 			ASSERT(vd->vdisk_label == VD_DISK_LABEL_EFI);
734edcc0754Sachartre 			ASSERT(vd->vdisk_block_size == DEV_BSIZE);
735edcc0754Sachartre 		}
736edcc0754Sachartre 
737edcc0754Sachartre 		if (blk >= vd->slices[slice].nblocks) {
738690555a1Sachartre 			/* address past the end of the slice */
739bae9e67eSachartre 			PR0("req_addr (0x%lx) >= psize (0x%lx)",
740edcc0754Sachartre 			    blk, vd->slices[slice].nblocks);
741690555a1Sachartre 			return (0);
742690555a1Sachartre 		}
743690555a1Sachartre 
744edcc0754Sachartre 		offset = (vd->slices[slice].start + blk) * DEV_BSIZE;
745bae9e67eSachartre 		maxlen = (vd->slices[slice].nblocks - blk) * DEV_BSIZE;
746bae9e67eSachartre 	}
747690555a1Sachartre 
748690555a1Sachartre 	/*
749690555a1Sachartre 	 * If the requested size is greater than the size
750690555a1Sachartre 	 * of the partition, truncate the read/write.
751690555a1Sachartre 	 */
752690555a1Sachartre 	if (len > maxlen) {
753690555a1Sachartre 		PR0("I/O size truncated to %lu bytes from %lu bytes",
754690555a1Sachartre 		    maxlen, len);
755690555a1Sachartre 		len = maxlen;
756690555a1Sachartre 	}
757690555a1Sachartre 
758690555a1Sachartre 	/*
759690555a1Sachartre 	 * We have to ensure that we are reading/writing into the mmap
760690555a1Sachartre 	 * range. If we have a partial disk image (e.g. an image of
761690555a1Sachartre 	 * s0 instead s2) the system can try to access slices that
762690555a1Sachartre 	 * are not included into the disk image.
763690555a1Sachartre 	 */
764edcc0754Sachartre 	if ((offset + len) > vd->file_size) {
765edcc0754Sachartre 		PR0("offset + nbytes (0x%lx + 0x%lx) > "
766690555a1Sachartre 		    "file_size (0x%lx)", offset, len, vd->file_size);
767690555a1Sachartre 		return (-1);
768690555a1Sachartre 	}
769690555a1Sachartre 
770690555a1Sachartre 	srw = (operation == VD_OP_BREAD)? S_READ : S_WRITE;
771eba0cb4eSachartre 	smflags = (operation == VD_OP_BREAD)? 0 :
772eba0cb4eSachartre 	    (SM_WRITE | vd_file_write_flags);
773690555a1Sachartre 	n = len;
774690555a1Sachartre 
775690555a1Sachartre 	do {
776690555a1Sachartre 		/*
777690555a1Sachartre 		 * segmap_getmapflt() returns a MAXBSIZE chunk which is
778690555a1Sachartre 		 * MAXBSIZE aligned.
779690555a1Sachartre 		 */
780690555a1Sachartre 		moffset = offset & MAXBOFFSET;
781690555a1Sachartre 		mlen = MIN(MAXBSIZE - moffset, n);
782690555a1Sachartre 		maddr = segmap_getmapflt(segkmap, vd->file_vnode, offset,
783690555a1Sachartre 		    mlen, 1, srw);
784690555a1Sachartre 		/*
785690555a1Sachartre 		 * Fault in the pages so we can check for error and ensure
786690555a1Sachartre 		 * that we can safely used the mapped address.
787690555a1Sachartre 		 */
788690555a1Sachartre 		if (segmap_fault(kas.a_hat, segkmap, maddr, mlen,
789690555a1Sachartre 		    F_SOFTLOCK, srw) != 0) {
790690555a1Sachartre 			(void) segmap_release(segkmap, maddr, 0);
791690555a1Sachartre 			return (-1);
792690555a1Sachartre 		}
793690555a1Sachartre 
794690555a1Sachartre 		if (operation == VD_OP_BREAD)
795690555a1Sachartre 			bcopy(maddr + moffset, data, mlen);
796690555a1Sachartre 		else
797690555a1Sachartre 			bcopy(data, maddr + moffset, mlen);
798690555a1Sachartre 
799690555a1Sachartre 		if (segmap_fault(kas.a_hat, segkmap, maddr, mlen,
800690555a1Sachartre 		    F_SOFTUNLOCK, srw) != 0) {
801690555a1Sachartre 			(void) segmap_release(segkmap, maddr, 0);
802690555a1Sachartre 			return (-1);
803690555a1Sachartre 		}
804690555a1Sachartre 		if (segmap_release(segkmap, maddr, smflags) != 0)
805690555a1Sachartre 			return (-1);
806690555a1Sachartre 		n -= mlen;
807690555a1Sachartre 		offset += mlen;
808690555a1Sachartre 		data += mlen;
809690555a1Sachartre 
810690555a1Sachartre 	} while (n > 0);
811690555a1Sachartre 
812690555a1Sachartre 	return (len);
813690555a1Sachartre }
814690555a1Sachartre 
81587a7269eSachartre /*
81687a7269eSachartre  * Function:
817bae9e67eSachartre  *	vd_build_default_label
81878fcd0a1Sachartre  *
81978fcd0a1Sachartre  * Description:
820bae9e67eSachartre  *	Return a default label for a given disk size. This is used when the disk
82178fcd0a1Sachartre  *	does not have a valid VTOC so that the user can get a valid default
82217cadca8Slm66018  *	configuration. The default label has all slice sizes set to 0 (except
82378fcd0a1Sachartre  *	slice 2 which is the entire disk) to force the user to write a valid
82478fcd0a1Sachartre  *	label onto the disk image.
82578fcd0a1Sachartre  *
82678fcd0a1Sachartre  * Parameters:
827bae9e67eSachartre  *	disk_size	- the disk size in bytes
82878fcd0a1Sachartre  *	label		- the returned default label.
82978fcd0a1Sachartre  *
83078fcd0a1Sachartre  * Return Code:
83178fcd0a1Sachartre  *	none.
83278fcd0a1Sachartre  */
83378fcd0a1Sachartre static void
834bae9e67eSachartre vd_build_default_label(size_t disk_size, struct dk_label *label)
83578fcd0a1Sachartre {
83678fcd0a1Sachartre 	size_t size;
837bae9e67eSachartre 	char unit;
838edcc0754Sachartre 
839edcc0754Sachartre 	bzero(label, sizeof (struct dk_label));
84078fcd0a1Sachartre 
84178fcd0a1Sachartre 	/*
84278fcd0a1Sachartre 	 * We must have a resonable number of cylinders and sectors so
84378fcd0a1Sachartre 	 * that newfs can run using default values.
84478fcd0a1Sachartre 	 *
84578fcd0a1Sachartre 	 * if (disk_size < 2MB)
84678fcd0a1Sachartre 	 * 	phys_cylinders = disk_size / 100K
847f745d6a3Sachartre 	 * else if (disk_size >= 18.75GB)
848f745d6a3Sachartre 	 *	phys_cylinders = 65535 (maximum number of cylinders)
84978fcd0a1Sachartre 	 * else
85078fcd0a1Sachartre 	 * 	phys_cylinders = disk_size / 300K
85178fcd0a1Sachartre 	 *
85278fcd0a1Sachartre 	 * phys_cylinders = (phys_cylinders == 0) ? 1 : phys_cylinders
85378fcd0a1Sachartre 	 * alt_cylinders = (phys_cylinders > 2) ? 2 : 0;
85478fcd0a1Sachartre 	 * data_cylinders = phys_cylinders - alt_cylinders
85578fcd0a1Sachartre 	 *
85678fcd0a1Sachartre 	 * sectors = disk_size / (phys_cylinders * blk_size)
85778fcd0a1Sachartre 	 *
858f745d6a3Sachartre 	 * The disk size test is an attempt to not have too few cylinders
859f745d6a3Sachartre 	 * for a small disk image, or so many on a big disk image that you
860f745d6a3Sachartre 	 * waste space for backup superblocks or cylinder group structures.
86178fcd0a1Sachartre 	 */
862bae9e67eSachartre 	if (disk_size < (2 * 1024 * 1024))
863bae9e67eSachartre 		label->dkl_pcyl = disk_size / (100 * 1024);
864f745d6a3Sachartre 	else if (disk_size >= (UINT16_MAX * 300 * 1024ULL))
865f745d6a3Sachartre 		label->dkl_pcyl = UINT16_MAX;
86678fcd0a1Sachartre 	else
867bae9e67eSachartre 		label->dkl_pcyl = disk_size / (300 * 1024);
86878fcd0a1Sachartre 
86978fcd0a1Sachartre 	if (label->dkl_pcyl == 0)
87078fcd0a1Sachartre 		label->dkl_pcyl = 1;
87178fcd0a1Sachartre 
872047ba61eSachartre 	label->dkl_acyl = 0;
873047ba61eSachartre 
87478fcd0a1Sachartre 	if (label->dkl_pcyl > 2)
87578fcd0a1Sachartre 		label->dkl_acyl = 2;
87678fcd0a1Sachartre 
877bae9e67eSachartre 	label->dkl_nsect = disk_size / (DEV_BSIZE * label->dkl_pcyl);
87878fcd0a1Sachartre 	label->dkl_ncyl = label->dkl_pcyl - label->dkl_acyl;
87978fcd0a1Sachartre 	label->dkl_nhead = 1;
88078fcd0a1Sachartre 	label->dkl_write_reinstruct = 0;
88178fcd0a1Sachartre 	label->dkl_read_reinstruct = 0;
88278fcd0a1Sachartre 	label->dkl_rpm = 7200;
88378fcd0a1Sachartre 	label->dkl_apc = 0;
88478fcd0a1Sachartre 	label->dkl_intrlv = 0;
88578fcd0a1Sachartre 
886bae9e67eSachartre 	PR0("requested disk size: %ld bytes\n", disk_size);
88778fcd0a1Sachartre 	PR0("setup: ncyl=%d nhead=%d nsec=%d\n", label->dkl_pcyl,
88878fcd0a1Sachartre 	    label->dkl_nhead, label->dkl_nsect);
88978fcd0a1Sachartre 	PR0("provided disk size: %ld bytes\n", (uint64_t)
89078fcd0a1Sachartre 	    (label->dkl_pcyl * label->dkl_nhead *
89178fcd0a1Sachartre 	    label->dkl_nsect * DEV_BSIZE));
89278fcd0a1Sachartre 
893bae9e67eSachartre 	vd_get_readable_size(disk_size, &size, &unit);
89478fcd0a1Sachartre 
89578fcd0a1Sachartre 	/*
89678fcd0a1Sachartre 	 * We must have a correct label name otherwise format(1m) will
89778fcd0a1Sachartre 	 * not recognized the disk as labeled.
89878fcd0a1Sachartre 	 */
89978fcd0a1Sachartre 	(void) snprintf(label->dkl_asciilabel, LEN_DKL_ASCII,
90078fcd0a1Sachartre 	    "SUN-DiskImage-%ld%cB cyl %d alt %d hd %d sec %d",
901bae9e67eSachartre 	    size, unit,
90278fcd0a1Sachartre 	    label->dkl_ncyl, label->dkl_acyl, label->dkl_nhead,
90378fcd0a1Sachartre 	    label->dkl_nsect);
90478fcd0a1Sachartre 
90578fcd0a1Sachartre 	/* default VTOC */
90678fcd0a1Sachartre 	label->dkl_vtoc.v_version = V_VERSION;
907edcc0754Sachartre 	label->dkl_vtoc.v_nparts = V_NUMPAR;
90878fcd0a1Sachartre 	label->dkl_vtoc.v_sanity = VTOC_SANE;
909edcc0754Sachartre 	label->dkl_vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_tag = V_BACKUP;
910edcc0754Sachartre 	label->dkl_map[VD_ENTIRE_DISK_SLICE].dkl_cylno = 0;
911edcc0754Sachartre 	label->dkl_map[VD_ENTIRE_DISK_SLICE].dkl_nblk = label->dkl_ncyl *
91278fcd0a1Sachartre 	    label->dkl_nhead * label->dkl_nsect;
913edcc0754Sachartre 	label->dkl_magic = DKL_MAGIC;
91478fcd0a1Sachartre 	label->dkl_cksum = vd_lbl2cksum(label);
91578fcd0a1Sachartre }
91678fcd0a1Sachartre 
91778fcd0a1Sachartre /*
91878fcd0a1Sachartre  * Function:
91987a7269eSachartre  *	vd_file_set_vtoc
92087a7269eSachartre  *
92187a7269eSachartre  * Description:
92287a7269eSachartre  *	Set the vtoc of a disk image by writing the label and backup
92387a7269eSachartre  *	labels into the disk image backend.
92487a7269eSachartre  *
92587a7269eSachartre  * Parameters:
92687a7269eSachartre  *	vd		- disk on which the operation is performed.
92787a7269eSachartre  *	label		- the data to be written.
92887a7269eSachartre  *
92987a7269eSachartre  * Return Code:
93087a7269eSachartre  *	0		- success.
93187a7269eSachartre  *	n > 0		- error, n indicates the errno code.
93287a7269eSachartre  */
93387a7269eSachartre static int
93487a7269eSachartre vd_file_set_vtoc(vd_t *vd, struct dk_label *label)
93587a7269eSachartre {
93687a7269eSachartre 	int blk, sec, cyl, head, cnt;
93787a7269eSachartre 
93887a7269eSachartre 	ASSERT(vd->file);
93987a7269eSachartre 
94087a7269eSachartre 	if (VD_FILE_LABEL_WRITE(vd, label) < 0) {
94187a7269eSachartre 		PR0("fail to write disk label");
94287a7269eSachartre 		return (EIO);
94387a7269eSachartre 	}
94487a7269eSachartre 
94587a7269eSachartre 	/*
94687a7269eSachartre 	 * Backup labels are on the last alternate cylinder's
94787a7269eSachartre 	 * first five odd sectors.
94887a7269eSachartre 	 */
94987a7269eSachartre 	if (label->dkl_acyl == 0) {
95087a7269eSachartre 		PR0("no alternate cylinder, can not store backup labels");
95187a7269eSachartre 		return (0);
95287a7269eSachartre 	}
95387a7269eSachartre 
95487a7269eSachartre 	cyl = label->dkl_ncyl  + label->dkl_acyl - 1;
95587a7269eSachartre 	head = label->dkl_nhead - 1;
95687a7269eSachartre 
95787a7269eSachartre 	blk = (cyl * ((label->dkl_nhead * label->dkl_nsect) - label->dkl_apc)) +
95887a7269eSachartre 	    (head * label->dkl_nsect);
95987a7269eSachartre 
96087a7269eSachartre 	/*
96187a7269eSachartre 	 * Write the backup labels. Make sure we don't try to write past
96287a7269eSachartre 	 * the last cylinder.
96387a7269eSachartre 	 */
96487a7269eSachartre 	sec = 1;
96587a7269eSachartre 
96687a7269eSachartre 	for (cnt = 0; cnt < VD_FILE_NUM_BACKUP; cnt++) {
96787a7269eSachartre 
96887a7269eSachartre 		if (sec >= label->dkl_nsect) {
96987a7269eSachartre 			PR0("not enough sector to store all backup labels");
97087a7269eSachartre 			return (0);
97187a7269eSachartre 		}
97287a7269eSachartre 
97387a7269eSachartre 		if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)label,
97487a7269eSachartre 		    blk + sec, sizeof (struct dk_label)) < 0) {
97587a7269eSachartre 			PR0("error writing backup label at block %d\n",
97687a7269eSachartre 			    blk + sec);
97787a7269eSachartre 			return (EIO);
97887a7269eSachartre 		}
97987a7269eSachartre 
98087a7269eSachartre 		PR1("wrote backup label at block %d\n", blk + sec);
98187a7269eSachartre 
98287a7269eSachartre 		sec += 2;
98387a7269eSachartre 	}
98487a7269eSachartre 
98587a7269eSachartre 	return (0);
98687a7269eSachartre }
98787a7269eSachartre 
98887a7269eSachartre /*
98987a7269eSachartre  * Function:
99087a7269eSachartre  *	vd_file_get_devid_block
99187a7269eSachartre  *
99287a7269eSachartre  * Description:
99387a7269eSachartre  *	Return the block number where the device id is stored.
99487a7269eSachartre  *
99587a7269eSachartre  * Parameters:
99687a7269eSachartre  *	vd		- disk on which the operation is performed.
99787a7269eSachartre  *	blkp		- pointer to the block number
99887a7269eSachartre  *
99987a7269eSachartre  * Return Code:
100087a7269eSachartre  *	0		- success
100187a7269eSachartre  *	ENOSPC		- disk has no space to store a device id
100287a7269eSachartre  */
100387a7269eSachartre static int
100487a7269eSachartre vd_file_get_devid_block(vd_t *vd, size_t *blkp)
100587a7269eSachartre {
100687a7269eSachartre 	diskaddr_t spc, head, cyl;
100787a7269eSachartre 
100887a7269eSachartre 	ASSERT(vd->file);
1009edcc0754Sachartre 
1010edcc0754Sachartre 	if (vd->vdisk_label == VD_DISK_LABEL_UNK) {
1011edcc0754Sachartre 		/*
1012edcc0754Sachartre 		 * If no label is defined we don't know where to find
1013edcc0754Sachartre 		 * a device id.
1014edcc0754Sachartre 		 */
1015edcc0754Sachartre 		return (ENOSPC);
1016edcc0754Sachartre 	}
1017edcc0754Sachartre 
1018edcc0754Sachartre 	if (vd->vdisk_label == VD_DISK_LABEL_EFI) {
1019edcc0754Sachartre 		/*
1020edcc0754Sachartre 		 * For an EFI disk, the devid is at the beginning of
1021edcc0754Sachartre 		 * the reserved slice
1022edcc0754Sachartre 		 */
1023edcc0754Sachartre 		if (vd->efi_reserved == -1) {
1024edcc0754Sachartre 			PR0("EFI disk has no reserved slice");
1025edcc0754Sachartre 			return (ENOSPC);
1026edcc0754Sachartre 		}
1027edcc0754Sachartre 
1028edcc0754Sachartre 		*blkp = vd->slices[vd->efi_reserved].start;
1029edcc0754Sachartre 		return (0);
1030edcc0754Sachartre 	}
1031edcc0754Sachartre 
103287a7269eSachartre 	ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
103387a7269eSachartre 
103487a7269eSachartre 	/* this geometry doesn't allow us to have a devid */
103587a7269eSachartre 	if (vd->dk_geom.dkg_acyl < 2) {
103687a7269eSachartre 		PR0("not enough alternate cylinder available for devid "
103787a7269eSachartre 		    "(acyl=%u)", vd->dk_geom.dkg_acyl);
103887a7269eSachartre 		return (ENOSPC);
103987a7269eSachartre 	}
104087a7269eSachartre 
104187a7269eSachartre 	/* the devid is in on the track next to the last cylinder */
104287a7269eSachartre 	cyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl - 2;
104387a7269eSachartre 	spc = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
104487a7269eSachartre 	head = vd->dk_geom.dkg_nhead - 1;
104587a7269eSachartre 
104687a7269eSachartre 	*blkp = (cyl * (spc - vd->dk_geom.dkg_apc)) +
104787a7269eSachartre 	    (head * vd->dk_geom.dkg_nsect) + 1;
104887a7269eSachartre 
104987a7269eSachartre 	return (0);
105087a7269eSachartre }
105187a7269eSachartre 
105287a7269eSachartre /*
105387a7269eSachartre  * Return the checksum of a disk block containing an on-disk devid.
105487a7269eSachartre  */
105587a7269eSachartre static uint_t
105687a7269eSachartre vd_dkdevid2cksum(struct dk_devid *dkdevid)
105787a7269eSachartre {
105887a7269eSachartre 	uint_t chksum, *ip;
105987a7269eSachartre 	int i;
106087a7269eSachartre 
106187a7269eSachartre 	chksum = 0;
106287a7269eSachartre 	ip = (uint_t *)dkdevid;
106387a7269eSachartre 	for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int)); i++)
106487a7269eSachartre 		chksum ^= ip[i];
106587a7269eSachartre 
106687a7269eSachartre 	return (chksum);
106787a7269eSachartre }
106887a7269eSachartre 
106987a7269eSachartre /*
107087a7269eSachartre  * Function:
107187a7269eSachartre  *	vd_file_read_devid
107287a7269eSachartre  *
107387a7269eSachartre  * Description:
107487a7269eSachartre  *	Read the device id stored on a disk image.
107587a7269eSachartre  *
107687a7269eSachartre  * Parameters:
107787a7269eSachartre  *	vd		- disk on which the operation is performed.
107887a7269eSachartre  *	devid		- the return address of the device ID.
107987a7269eSachartre  *
108087a7269eSachartre  * Return Code:
108187a7269eSachartre  *	0		- success
108287a7269eSachartre  *	EIO		- I/O error while trying to access the disk image
108387a7269eSachartre  *	EINVAL		- no valid device id was found
108487a7269eSachartre  *	ENOSPC		- disk has no space to store a device id
108587a7269eSachartre  */
108687a7269eSachartre static int
108787a7269eSachartre vd_file_read_devid(vd_t *vd, ddi_devid_t *devid)
108887a7269eSachartre {
108987a7269eSachartre 	struct dk_devid *dkdevid;
109087a7269eSachartre 	size_t blk;
109187a7269eSachartre 	uint_t chksum;
109287a7269eSachartre 	int status, sz;
109387a7269eSachartre 
109487a7269eSachartre 	if ((status = vd_file_get_devid_block(vd, &blk)) != 0)
109587a7269eSachartre 		return (status);
109687a7269eSachartre 
109787a7269eSachartre 	dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP);
109887a7269eSachartre 
109987a7269eSachartre 	/* get the devid */
110087a7269eSachartre 	if ((vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)dkdevid, blk,
110187a7269eSachartre 	    DEV_BSIZE)) < 0) {
110287a7269eSachartre 		PR0("error reading devid block at %lu", blk);
110387a7269eSachartre 		status = EIO;
110487a7269eSachartre 		goto done;
110587a7269eSachartre 	}
110687a7269eSachartre 
110787a7269eSachartre 	/* validate the revision */
110887a7269eSachartre 	if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
110987a7269eSachartre 	    (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
111087a7269eSachartre 		PR0("invalid devid found at block %lu (bad revision)", blk);
111187a7269eSachartre 		status = EINVAL;
111287a7269eSachartre 		goto done;
111387a7269eSachartre 	}
111487a7269eSachartre 
111587a7269eSachartre 	/* compute checksum */
111687a7269eSachartre 	chksum = vd_dkdevid2cksum(dkdevid);
111787a7269eSachartre 
111887a7269eSachartre 	/* compare the checksums */
111987a7269eSachartre 	if (DKD_GETCHKSUM(dkdevid) != chksum) {
112087a7269eSachartre 		PR0("invalid devid found at block %lu (bad checksum)", blk);
112187a7269eSachartre 		status = EINVAL;
112287a7269eSachartre 		goto done;
112387a7269eSachartre 	}
112487a7269eSachartre 
112587a7269eSachartre 	/* validate the device id */
112687a7269eSachartre 	if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
112787a7269eSachartre 		PR0("invalid devid found at block %lu", blk);
112887a7269eSachartre 		status = EINVAL;
112987a7269eSachartre 		goto done;
113087a7269eSachartre 	}
113187a7269eSachartre 
113287a7269eSachartre 	PR1("devid read at block %lu", blk);
113387a7269eSachartre 
113487a7269eSachartre 	sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
113587a7269eSachartre 	*devid = kmem_alloc(sz, KM_SLEEP);
113687a7269eSachartre 	bcopy(&dkdevid->dkd_devid, *devid, sz);
113787a7269eSachartre 
113887a7269eSachartre done:
113987a7269eSachartre 	kmem_free(dkdevid, DEV_BSIZE);
114087a7269eSachartre 	return (status);
114187a7269eSachartre 
114287a7269eSachartre }
114387a7269eSachartre 
114487a7269eSachartre /*
114587a7269eSachartre  * Function:
114687a7269eSachartre  *	vd_file_write_devid
114787a7269eSachartre  *
114887a7269eSachartre  * Description:
114987a7269eSachartre  *	Write a device id into disk image.
115087a7269eSachartre  *
115187a7269eSachartre  * Parameters:
115287a7269eSachartre  *	vd		- disk on which the operation is performed.
115387a7269eSachartre  *	devid		- the device ID to store.
115487a7269eSachartre  *
115587a7269eSachartre  * Return Code:
115687a7269eSachartre  *	0		- success
115787a7269eSachartre  *	EIO		- I/O error while trying to access the disk image
115887a7269eSachartre  *	ENOSPC		- disk has no space to store a device id
115987a7269eSachartre  */
116087a7269eSachartre static int
116187a7269eSachartre vd_file_write_devid(vd_t *vd, ddi_devid_t devid)
116287a7269eSachartre {
116387a7269eSachartre 	struct dk_devid *dkdevid;
116487a7269eSachartre 	uint_t chksum;
116587a7269eSachartre 	size_t blk;
116687a7269eSachartre 	int status;
116787a7269eSachartre 
1168edcc0754Sachartre 	if (devid == NULL) {
1169edcc0754Sachartre 		/* nothing to write */
1170edcc0754Sachartre 		return (0);
1171edcc0754Sachartre 	}
1172edcc0754Sachartre 
117387a7269eSachartre 	if ((status = vd_file_get_devid_block(vd, &blk)) != 0)
117487a7269eSachartre 		return (status);
117587a7269eSachartre 
117687a7269eSachartre 	dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP);
117787a7269eSachartre 
117887a7269eSachartre 	/* set revision */
117987a7269eSachartre 	dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
118087a7269eSachartre 	dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
118187a7269eSachartre 
118287a7269eSachartre 	/* copy devid */
118387a7269eSachartre 	bcopy(devid, &dkdevid->dkd_devid, ddi_devid_sizeof(devid));
118487a7269eSachartre 
118587a7269eSachartre 	/* compute checksum */
118687a7269eSachartre 	chksum = vd_dkdevid2cksum(dkdevid);
118787a7269eSachartre 
118887a7269eSachartre 	/* set checksum */
118987a7269eSachartre 	DKD_FORMCHKSUM(chksum, dkdevid);
119087a7269eSachartre 
119187a7269eSachartre 	/* store the devid */
119287a7269eSachartre 	if ((status = vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE,
119387a7269eSachartre 	    (caddr_t)dkdevid, blk, DEV_BSIZE)) < 0) {
119487a7269eSachartre 		PR0("Error writing devid block at %lu", blk);
119587a7269eSachartre 		status = EIO;
119687a7269eSachartre 	} else {
119787a7269eSachartre 		PR1("devid written at block %lu", blk);
119887a7269eSachartre 		status = 0;
119987a7269eSachartre 	}
120087a7269eSachartre 
120187a7269eSachartre 	kmem_free(dkdevid, DEV_BSIZE);
120287a7269eSachartre 	return (status);
120387a7269eSachartre }
120487a7269eSachartre 
120587a7269eSachartre /*
120687a7269eSachartre  * Function:
120717cadca8Slm66018  *	vd_do_scsi_rdwr
120887a7269eSachartre  *
120987a7269eSachartre  * Description:
121087a7269eSachartre  * 	Read or write to a SCSI disk using an absolute disk offset.
121187a7269eSachartre  *
121287a7269eSachartre  * Parameters:
121387a7269eSachartre  *	vd		- disk on which the operation is performed.
121487a7269eSachartre  *	operation	- operation to execute: read (VD_OP_BREAD) or
121587a7269eSachartre  *			  write (VD_OP_BWRITE).
121687a7269eSachartre  *	data		- buffer where data are read to or written from.
121787a7269eSachartre  *	blk		- starting block for the operation.
121887a7269eSachartre  *	len		- number of bytes to read or write.
121987a7269eSachartre  *
122087a7269eSachartre  * Return Code:
122187a7269eSachartre  *	0		- success
122287a7269eSachartre  *	n != 0		- error.
122387a7269eSachartre  */
122487a7269eSachartre static int
122517cadca8Slm66018 vd_do_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t blk, size_t len)
122687a7269eSachartre {
122787a7269eSachartre 	struct uscsi_cmd ucmd;
122887a7269eSachartre 	union scsi_cdb cdb;
122987a7269eSachartre 	int nsectors, nblk;
123087a7269eSachartre 	int max_sectors;
123187a7269eSachartre 	int status, rval;
123287a7269eSachartre 
123387a7269eSachartre 	ASSERT(!vd->file);
123417cadca8Slm66018 	ASSERT(vd->vdisk_block_size > 0);
123587a7269eSachartre 
123687a7269eSachartre 	max_sectors = vd->max_xfer_sz;
123717cadca8Slm66018 	nblk = (len / vd->vdisk_block_size);
123887a7269eSachartre 
123917cadca8Slm66018 	if (len % vd->vdisk_block_size != 0)
124087a7269eSachartre 		return (EINVAL);
124187a7269eSachartre 
124287a7269eSachartre 	/*
124387a7269eSachartre 	 * Build and execute the uscsi ioctl.  We build a group0, group1
124487a7269eSachartre 	 * or group4 command as necessary, since some targets
124587a7269eSachartre 	 * do not support group1 commands.
124687a7269eSachartre 	 */
124787a7269eSachartre 	while (nblk) {
124887a7269eSachartre 
124987a7269eSachartre 		bzero(&ucmd, sizeof (ucmd));
125087a7269eSachartre 		bzero(&cdb, sizeof (cdb));
125187a7269eSachartre 
125287a7269eSachartre 		nsectors = (max_sectors < nblk) ? max_sectors : nblk;
125387a7269eSachartre 
125417cadca8Slm66018 		/*
125517cadca8Slm66018 		 * Some of the optical drives on sun4v machines are ATAPI
125617cadca8Slm66018 		 * devices which use Group 1 Read/Write commands so we need
125717cadca8Slm66018 		 * to explicitly check a flag which is set when a domain
125817cadca8Slm66018 		 * is bound.
125917cadca8Slm66018 		 */
126017cadca8Slm66018 		if (blk < (2 << 20) && nsectors <= 0xff && !vd->is_atapi_dev) {
126187a7269eSachartre 			FORMG0ADDR(&cdb, blk);
126287a7269eSachartre 			FORMG0COUNT(&cdb, nsectors);
126387a7269eSachartre 			ucmd.uscsi_cdblen = CDB_GROUP0;
126487a7269eSachartre 		} else if (blk > 0xffffffff) {
126587a7269eSachartre 			FORMG4LONGADDR(&cdb, blk);
126687a7269eSachartre 			FORMG4COUNT(&cdb, nsectors);
126787a7269eSachartre 			ucmd.uscsi_cdblen = CDB_GROUP4;
126887a7269eSachartre 			cdb.scc_cmd |= SCMD_GROUP4;
126987a7269eSachartre 		} else {
127087a7269eSachartre 			FORMG1ADDR(&cdb, blk);
127187a7269eSachartre 			FORMG1COUNT(&cdb, nsectors);
127287a7269eSachartre 			ucmd.uscsi_cdblen = CDB_GROUP1;
127387a7269eSachartre 			cdb.scc_cmd |= SCMD_GROUP1;
127487a7269eSachartre 		}
127587a7269eSachartre 		ucmd.uscsi_cdb = (caddr_t)&cdb;
127687a7269eSachartre 		ucmd.uscsi_bufaddr = data;
127717cadca8Slm66018 		ucmd.uscsi_buflen = nsectors * vd->block_size;
127887a7269eSachartre 		ucmd.uscsi_timeout = vd_scsi_rdwr_timeout;
127987a7269eSachartre 		/*
128087a7269eSachartre 		 * Set flags so that the command is isolated from normal
128187a7269eSachartre 		 * commands and no error message is printed.
128287a7269eSachartre 		 */
128387a7269eSachartre 		ucmd.uscsi_flags = USCSI_ISOLATE | USCSI_SILENT;
128487a7269eSachartre 
128587a7269eSachartre 		if (operation == VD_OP_BREAD) {
128687a7269eSachartre 			cdb.scc_cmd |= SCMD_READ;
128787a7269eSachartre 			ucmd.uscsi_flags |= USCSI_READ;
128887a7269eSachartre 		} else {
128987a7269eSachartre 			cdb.scc_cmd |= SCMD_WRITE;
129087a7269eSachartre 		}
129187a7269eSachartre 
129287a7269eSachartre 		status = ldi_ioctl(vd->ldi_handle[VD_ENTIRE_DISK_SLICE],
1293047ba61eSachartre 		    USCSICMD, (intptr_t)&ucmd, (vd->open_flags | FKIOCTL),
129487a7269eSachartre 		    kcred, &rval);
129587a7269eSachartre 
129687a7269eSachartre 		if (status == 0)
129787a7269eSachartre 			status = ucmd.uscsi_status;
129887a7269eSachartre 
129987a7269eSachartre 		if (status != 0)
130087a7269eSachartre 			break;
130187a7269eSachartre 
130287a7269eSachartre 		/*
130387a7269eSachartre 		 * Check if partial DMA breakup is required. If so, reduce
130487a7269eSachartre 		 * the request size by half and retry the last request.
130587a7269eSachartre 		 */
130687a7269eSachartre 		if (ucmd.uscsi_resid == ucmd.uscsi_buflen) {
130787a7269eSachartre 			max_sectors >>= 1;
130887a7269eSachartre 			if (max_sectors <= 0) {
130987a7269eSachartre 				status = EIO;
131087a7269eSachartre 				break;
131187a7269eSachartre 			}
131287a7269eSachartre 			continue;
131387a7269eSachartre 		}
131487a7269eSachartre 
131587a7269eSachartre 		if (ucmd.uscsi_resid != 0) {
131687a7269eSachartre 			status = EIO;
131787a7269eSachartre 			break;
131887a7269eSachartre 		}
131987a7269eSachartre 
132087a7269eSachartre 		blk += nsectors;
132187a7269eSachartre 		nblk -= nsectors;
132217cadca8Slm66018 		data += nsectors * vd->vdisk_block_size; /* SECSIZE */
132387a7269eSachartre 	}
132487a7269eSachartre 
132587a7269eSachartre 	return (status);
132687a7269eSachartre }
132787a7269eSachartre 
1328205eeb1aSlm66018 /*
132917cadca8Slm66018  * Function:
133017cadca8Slm66018  *	vd_scsi_rdwr
133117cadca8Slm66018  *
133217cadca8Slm66018  * Description:
133317cadca8Slm66018  * 	Wrapper function to read or write to a SCSI disk using an absolute
133417cadca8Slm66018  *	disk offset. It checks the blocksize of the underlying device and,
133517cadca8Slm66018  *	if necessary, adjusts the buffers accordingly before calling
133617cadca8Slm66018  *	vd_do_scsi_rdwr() to do the actual read or write.
133717cadca8Slm66018  *
133817cadca8Slm66018  * Parameters:
133917cadca8Slm66018  *	vd		- disk on which the operation is performed.
134017cadca8Slm66018  *	operation	- operation to execute: read (VD_OP_BREAD) or
134117cadca8Slm66018  *			  write (VD_OP_BWRITE).
134217cadca8Slm66018  *	data		- buffer where data are read to or written from.
134317cadca8Slm66018  *	blk		- starting block for the operation.
134417cadca8Slm66018  *	len		- number of bytes to read or write.
134517cadca8Slm66018  *
134617cadca8Slm66018  * Return Code:
134717cadca8Slm66018  *	0		- success
134817cadca8Slm66018  *	n != 0		- error.
134917cadca8Slm66018  */
135017cadca8Slm66018 static int
135117cadca8Slm66018 vd_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t vblk, size_t vlen)
135217cadca8Slm66018 {
135317cadca8Slm66018 	int	rv;
135417cadca8Slm66018 
135517cadca8Slm66018 	size_t	pblk;	/* physical device block number of data on device */
135617cadca8Slm66018 	size_t	delta;	/* relative offset between pblk and vblk */
135717cadca8Slm66018 	size_t	pnblk;	/* number of physical blocks to be read from device */
135817cadca8Slm66018 	size_t	plen;	/* length of data to be read from physical device */
135917cadca8Slm66018 	char	*buf;	/* buffer area to fit physical device's block size */
136017cadca8Slm66018 
13612f5224aeSachartre 	if (vd->block_size == 0) {
13622f5224aeSachartre 		/*
13632f5224aeSachartre 		 * The block size was not available during the attach,
13642f5224aeSachartre 		 * try to update it now.
13652f5224aeSachartre 		 */
1366*de3a5331SRamesh Chitrothu 		if (vd_backend_check_size(vd) != 0)
13672f5224aeSachartre 			return (EIO);
13682f5224aeSachartre 	}
13692f5224aeSachartre 
137017cadca8Slm66018 	/*
137117cadca8Slm66018 	 * If the vdisk block size and the block size of the underlying device
137217cadca8Slm66018 	 * match we can skip straight to vd_do_scsi_rdwr(), otherwise we need
137317cadca8Slm66018 	 * to create a buffer large enough to handle the device's block size
137417cadca8Slm66018 	 * and adjust the block to be read from and the amount of data to
137517cadca8Slm66018 	 * read to correspond with the device's block size.
137617cadca8Slm66018 	 */
137717cadca8Slm66018 	if (vd->vdisk_block_size == vd->block_size)
137817cadca8Slm66018 		return (vd_do_scsi_rdwr(vd, operation, data, vblk, vlen));
137917cadca8Slm66018 
138017cadca8Slm66018 	if (vd->vdisk_block_size > vd->block_size)
138117cadca8Slm66018 		return (EINVAL);
138217cadca8Slm66018 
138317cadca8Slm66018 	/*
138417cadca8Slm66018 	 * Writing of physical block sizes larger than the virtual block size
138517cadca8Slm66018 	 * is not supported. This would be added if/when support for guests
138617cadca8Slm66018 	 * writing to DVDs is implemented.
138717cadca8Slm66018 	 */
138817cadca8Slm66018 	if (operation == VD_OP_BWRITE)
138917cadca8Slm66018 		return (ENOTSUP);
139017cadca8Slm66018 
139117cadca8Slm66018 	/* BEGIN CSTYLED */
139217cadca8Slm66018 	/*
139317cadca8Slm66018 	 * Below is a diagram showing the relationship between the physical
139417cadca8Slm66018 	 * and virtual blocks. If the virtual blocks marked by 'X' below are
139517cadca8Slm66018 	 * requested, then the physical blocks denoted by 'Y' are read.
139617cadca8Slm66018 	 *
139717cadca8Slm66018 	 *           vblk
139817cadca8Slm66018 	 *             |      vlen
139917cadca8Slm66018 	 *             |<--------------->|
140017cadca8Slm66018 	 *             v                 v
140117cadca8Slm66018 	 *  --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-   virtual disk:
140217cadca8Slm66018 	 *    |  |  |  |XX|XX|XX|XX|XX|XX|  |  |  |  |  |  } block size is
140317cadca8Slm66018 	 *  --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-  vd->vdisk_block_size
140417cadca8Slm66018 	 *          :  :                 :  :
140517cadca8Slm66018 	 *         >:==:< delta          :  :
140617cadca8Slm66018 	 *          :  :                 :  :
140717cadca8Slm66018 	 *  --+-----+-----+-----+-----+-----+-----+-----+--   physical disk:
140817cadca8Slm66018 	 *    |     |YY:YY|YYYYY|YYYYY|YY:YY|     |     |   } block size is
140917cadca8Slm66018 	 *  --+-----+-----+-----+-----+-----+-----+-----+--   vd->block_size
141017cadca8Slm66018 	 *          ^                       ^
141117cadca8Slm66018 	 *          |<--------------------->|
141217cadca8Slm66018 	 *          |         plen
141317cadca8Slm66018 	 *         pblk
141417cadca8Slm66018 	 */
141517cadca8Slm66018 	/* END CSTYLED */
141617cadca8Slm66018 	pblk = (vblk * vd->vdisk_block_size) / vd->block_size;
141717cadca8Slm66018 	delta = (vblk * vd->vdisk_block_size) - (pblk * vd->block_size);
141817cadca8Slm66018 	pnblk = ((delta + vlen - 1) / vd->block_size) + 1;
141917cadca8Slm66018 	plen = pnblk * vd->block_size;
142017cadca8Slm66018 
142117cadca8Slm66018 	PR2("vblk %lx:pblk %lx: vlen %ld:plen %ld", vblk, pblk, vlen, plen);
142217cadca8Slm66018 
142317cadca8Slm66018 	buf = kmem_zalloc(sizeof (caddr_t) * plen, KM_SLEEP);
142417cadca8Slm66018 	rv = vd_do_scsi_rdwr(vd, operation, (caddr_t)buf, pblk, plen);
142517cadca8Slm66018 	bcopy(buf + delta, data, vlen);
142617cadca8Slm66018 
142717cadca8Slm66018 	kmem_free(buf, sizeof (caddr_t) * plen);
142817cadca8Slm66018 
142917cadca8Slm66018 	return (rv);
143017cadca8Slm66018 }
143117cadca8Slm66018 
143217cadca8Slm66018 /*
1433bae9e67eSachartre  * Function:
1434bae9e67eSachartre  *	vd_slice_flabel_read
1435bae9e67eSachartre  *
1436bae9e67eSachartre  * Description:
1437bae9e67eSachartre  *	This function simulates a read operation from the fake label of
1438bae9e67eSachartre  *	a single-slice disk.
1439bae9e67eSachartre  *
1440bae9e67eSachartre  * Parameters:
1441bae9e67eSachartre  *	vd		- single-slice disk to read from
1442bae9e67eSachartre  *	data		- buffer where data should be read to
1443bae9e67eSachartre  *	offset		- offset in byte where the read should start
1444bae9e67eSachartre  *	length		- number of bytes to read
1445bae9e67eSachartre  *
1446bae9e67eSachartre  * Return Code:
1447bae9e67eSachartre  *	n >= 0		- success, n indicates the number of bytes read
1448bae9e67eSachartre  *	-1		- error
1449bae9e67eSachartre  */
1450bae9e67eSachartre static ssize_t
1451bae9e67eSachartre vd_slice_flabel_read(vd_t *vd, caddr_t data, size_t offset, size_t length)
1452bae9e67eSachartre {
1453bae9e67eSachartre 	size_t n = 0;
1454bae9e67eSachartre 	uint_t limit = vd->flabel_limit * DEV_BSIZE;
1455bae9e67eSachartre 
1456bae9e67eSachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
1457bae9e67eSachartre 	ASSERT(vd->flabel != NULL);
1458bae9e67eSachartre 
1459bae9e67eSachartre 	/* if offset is past the fake label limit there's nothing to read */
1460bae9e67eSachartre 	if (offset >= limit)
1461bae9e67eSachartre 		return (0);
1462bae9e67eSachartre 
1463bae9e67eSachartre 	/* data with offset 0 to flabel_size are read from flabel */
1464bae9e67eSachartre 	if (offset < vd->flabel_size) {
1465bae9e67eSachartre 
1466bae9e67eSachartre 		if (offset + length <= vd->flabel_size) {
1467bae9e67eSachartre 			bcopy(vd->flabel + offset, data, length);
1468bae9e67eSachartre 			return (length);
1469bae9e67eSachartre 		}
1470bae9e67eSachartre 
1471bae9e67eSachartre 		n = vd->flabel_size - offset;
1472bae9e67eSachartre 		bcopy(vd->flabel + offset, data, n);
1473bae9e67eSachartre 		data += n;
1474bae9e67eSachartre 	}
1475bae9e67eSachartre 
1476bae9e67eSachartre 	/* data with offset from flabel_size to flabel_limit are all zeros */
1477bae9e67eSachartre 	if (offset + length <= limit) {
1478bae9e67eSachartre 		bzero(data, length - n);
1479bae9e67eSachartre 		return (length);
1480bae9e67eSachartre 	}
1481bae9e67eSachartre 
1482bae9e67eSachartre 	bzero(data, limit - offset - n);
1483bae9e67eSachartre 	return (limit - offset);
1484bae9e67eSachartre }
1485bae9e67eSachartre 
1486bae9e67eSachartre /*
1487bae9e67eSachartre  * Function:
1488bae9e67eSachartre  *	vd_slice_flabel_write
1489bae9e67eSachartre  *
1490bae9e67eSachartre  * Description:
1491bae9e67eSachartre  *	This function simulates a write operation to the fake label of
1492bae9e67eSachartre  *	a single-slice disk. Write operations are actually faked and return
1493bae9e67eSachartre  *	success although the label is never changed. This is mostly to
1494bae9e67eSachartre  *	simulate a successful label update.
1495bae9e67eSachartre  *
1496bae9e67eSachartre  * Parameters:
1497bae9e67eSachartre  *	vd		- single-slice disk to write to
1498bae9e67eSachartre  *	data		- buffer where data should be written from
1499bae9e67eSachartre  *	offset		- offset in byte where the write should start
1500bae9e67eSachartre  *	length		- number of bytes to written
1501bae9e67eSachartre  *
1502bae9e67eSachartre  * Return Code:
1503bae9e67eSachartre  *	n >= 0		- success, n indicates the number of bytes written
1504bae9e67eSachartre  *	-1		- error
1505bae9e67eSachartre  */
1506bae9e67eSachartre static ssize_t
1507bae9e67eSachartre vd_slice_flabel_write(vd_t *vd, caddr_t data, size_t offset, size_t length)
1508bae9e67eSachartre {
1509bae9e67eSachartre 	uint_t limit = vd->flabel_limit * DEV_BSIZE;
1510bae9e67eSachartre 	struct dk_label *label;
1511bae9e67eSachartre 	struct dk_geom geom;
1512bae9e67eSachartre 	struct vtoc vtoc;
1513bae9e67eSachartre 
1514bae9e67eSachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
1515bae9e67eSachartre 	ASSERT(vd->flabel != NULL);
1516bae9e67eSachartre 
1517bae9e67eSachartre 	if (offset >= limit)
1518bae9e67eSachartre 		return (0);
1519bae9e67eSachartre 
1520bae9e67eSachartre 	/*
1521bae9e67eSachartre 	 * If this is a request to overwrite the VTOC disk label, check that
1522bae9e67eSachartre 	 * the new label is similar to the previous one and return that the
1523bae9e67eSachartre 	 * write was successful, but note that nothing is actually overwritten.
1524bae9e67eSachartre 	 */
1525bae9e67eSachartre 	if (vd->vdisk_label == VD_DISK_LABEL_VTOC &&
1526bae9e67eSachartre 	    offset == 0 && length == DEV_BSIZE) {
1527bae9e67eSachartre 		label = (struct dk_label *)data;
1528bae9e67eSachartre 
1529bae9e67eSachartre 		/* check that this is a valid label */
1530bae9e67eSachartre 		if (label->dkl_magic != DKL_MAGIC ||
1531bae9e67eSachartre 		    label->dkl_cksum != vd_lbl2cksum(label))
1532bae9e67eSachartre 			return (-1);
1533bae9e67eSachartre 
1534bae9e67eSachartre 		/* check the vtoc and geometry */
1535bae9e67eSachartre 		vd_label_to_vtocgeom(label, &vtoc, &geom);
1536bae9e67eSachartre 		if (vd_slice_geom_isvalid(vd, &geom) &&
1537bae9e67eSachartre 		    vd_slice_vtoc_isvalid(vd, &vtoc))
1538bae9e67eSachartre 			return (length);
1539bae9e67eSachartre 	}
1540bae9e67eSachartre 
1541bae9e67eSachartre 	/* fail any other write */
1542bae9e67eSachartre 	return (-1);
1543bae9e67eSachartre }
1544bae9e67eSachartre 
1545bae9e67eSachartre /*
1546bae9e67eSachartre  * Function:
1547bae9e67eSachartre  *	vd_slice_fake_rdwr
1548bae9e67eSachartre  *
1549bae9e67eSachartre  * Description:
1550bae9e67eSachartre  *	This function simulates a raw read or write operation to a single-slice
1551bae9e67eSachartre  *	disk. It only handles the faked part of the operation i.e. I/Os to
1552bae9e67eSachartre  *	blocks which have no mapping with the vdisk backend (I/Os to the
1553bae9e67eSachartre  *	beginning and to the end of the vdisk).
1554bae9e67eSachartre  *
1555bae9e67eSachartre  *	The function returns 0 is the operation	is completed and it has been
1556bae9e67eSachartre  *	entirely handled as a fake read or write. In that case, lengthp points
1557bae9e67eSachartre  *	to the number of bytes not read or written. Values returned by datap
1558bae9e67eSachartre  *	and blkp are undefined.
1559bae9e67eSachartre  *
1560bae9e67eSachartre  *	If the fake operation has succeeded but the read or write is not
1561bae9e67eSachartre  *	complete (i.e. the read/write operation extends beyond the blocks
1562bae9e67eSachartre  *	we fake) then the function returns EAGAIN and datap, blkp and lengthp
1563bae9e67eSachartre  *	pointers points to the parameters for completing the operation.
1564bae9e67eSachartre  *
1565bae9e67eSachartre  *	In case of an error, for example if the slice is empty or parameters
1566bae9e67eSachartre  *	are invalid, then the function returns a non-zero value different
1567bae9e67eSachartre  *	from EAGAIN. In that case, the returned values of datap, blkp and
1568bae9e67eSachartre  *	lengthp are undefined.
1569bae9e67eSachartre  *
1570bae9e67eSachartre  * Parameters:
1571bae9e67eSachartre  *	vd		- single-slice disk on which the operation is performed
1572bae9e67eSachartre  *	slice		- slice on which the operation is performed,
1573bae9e67eSachartre  *			  VD_SLICE_NONE indicates that the operation
1574bae9e67eSachartre  *			  is done using an absolute disk offset.
1575bae9e67eSachartre  *	operation	- operation to execute: read (VD_OP_BREAD) or
1576bae9e67eSachartre  *			  write (VD_OP_BWRITE).
1577bae9e67eSachartre  *	datap		- pointer to the buffer where data are read to
1578bae9e67eSachartre  *			  or written from. Return the pointer where remaining
1579bae9e67eSachartre  *			  data have to be read to or written from.
1580bae9e67eSachartre  *	blkp		- pointer to the starting block for the operation.
1581bae9e67eSachartre  *			  Return the starting block relative to the vdisk
1582bae9e67eSachartre  *			  backend for the remaining operation.
1583bae9e67eSachartre  *	lengthp		- pointer to the number of bytes to read or write.
1584bae9e67eSachartre  *			  This should be a multiple of DEV_BSIZE. Return the
1585bae9e67eSachartre  *			  remaining number of bytes to read or write.
1586bae9e67eSachartre  *
1587bae9e67eSachartre  * Return Code:
1588bae9e67eSachartre  *	0		- read/write operation is completed
1589bae9e67eSachartre  *	EAGAIN		- read/write operation is not completed
1590bae9e67eSachartre  *	other values	- error
1591bae9e67eSachartre  */
1592bae9e67eSachartre static int
1593bae9e67eSachartre vd_slice_fake_rdwr(vd_t *vd, int slice, int operation, caddr_t *datap,
1594bae9e67eSachartre     size_t *blkp, size_t *lengthp)
1595bae9e67eSachartre {
1596bae9e67eSachartre 	struct dk_label *label;
1597bae9e67eSachartre 	caddr_t data;
1598bae9e67eSachartre 	size_t blk, length, csize;
1599bae9e67eSachartre 	size_t ablk, asize, aoff, alen;
1600bae9e67eSachartre 	ssize_t n;
1601bae9e67eSachartre 	int sec, status;
1602bae9e67eSachartre 
1603bae9e67eSachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
1604bae9e67eSachartre 	ASSERT(slice != 0);
1605bae9e67eSachartre 
1606bae9e67eSachartre 	data = *datap;
1607bae9e67eSachartre 	blk = *blkp;
1608bae9e67eSachartre 	length = *lengthp;
1609bae9e67eSachartre 
1610bae9e67eSachartre 	/*
1611bae9e67eSachartre 	 * If this is not a raw I/O or an I/O from a full disk slice then
1612bae9e67eSachartre 	 * this is an I/O to/from an empty slice.
1613bae9e67eSachartre 	 */
1614bae9e67eSachartre 	if (slice != VD_SLICE_NONE &&
1615bae9e67eSachartre 	    (slice != VD_ENTIRE_DISK_SLICE ||
1616bae9e67eSachartre 	    vd->vdisk_label != VD_DISK_LABEL_VTOC) &&
1617bae9e67eSachartre 	    (slice != VD_EFI_WD_SLICE ||
1618bae9e67eSachartre 	    vd->vdisk_label != VD_DISK_LABEL_EFI)) {
1619bae9e67eSachartre 		return (EIO);
1620bae9e67eSachartre 	}
1621bae9e67eSachartre 
1622bae9e67eSachartre 	if (length % DEV_BSIZE != 0)
1623bae9e67eSachartre 		return (EINVAL);
1624bae9e67eSachartre 
1625bae9e67eSachartre 	/* handle any I/O with the fake label */
1626bae9e67eSachartre 	if (operation == VD_OP_BWRITE)
1627bae9e67eSachartre 		n = vd_slice_flabel_write(vd, data, blk * DEV_BSIZE, length);
1628bae9e67eSachartre 	else
1629bae9e67eSachartre 		n = vd_slice_flabel_read(vd, data, blk * DEV_BSIZE, length);
1630bae9e67eSachartre 
1631bae9e67eSachartre 	if (n == -1)
1632bae9e67eSachartre 		return (EINVAL);
1633bae9e67eSachartre 
1634bae9e67eSachartre 	ASSERT(n % DEV_BSIZE == 0);
1635bae9e67eSachartre 
1636bae9e67eSachartre 	/* adjust I/O arguments */
1637bae9e67eSachartre 	data += n;
1638bae9e67eSachartre 	blk += n / DEV_BSIZE;
1639bae9e67eSachartre 	length -= n;
1640bae9e67eSachartre 
1641bae9e67eSachartre 	/* check if there's something else to process */
1642bae9e67eSachartre 	if (length == 0) {
1643bae9e67eSachartre 		status = 0;
1644bae9e67eSachartre 		goto done;
1645bae9e67eSachartre 	}
1646bae9e67eSachartre 
1647bae9e67eSachartre 	if (vd->vdisk_label == VD_DISK_LABEL_VTOC &&
1648bae9e67eSachartre 	    slice == VD_ENTIRE_DISK_SLICE) {
1649bae9e67eSachartre 		status = EAGAIN;
1650bae9e67eSachartre 		goto done;
1651bae9e67eSachartre 	}
1652bae9e67eSachartre 
1653bae9e67eSachartre 	if (vd->vdisk_label == VD_DISK_LABEL_EFI) {
1654bae9e67eSachartre 		asize = EFI_MIN_RESV_SIZE + 33;
1655bae9e67eSachartre 		ablk = vd->vdisk_size - asize;
1656bae9e67eSachartre 	} else {
1657bae9e67eSachartre 		ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
1658bae9e67eSachartre 		ASSERT(vd->dk_geom.dkg_apc == 0);
1659bae9e67eSachartre 
1660bae9e67eSachartre 		csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
1661bae9e67eSachartre 		ablk = vd->dk_geom.dkg_ncyl * csize;
1662bae9e67eSachartre 		asize = vd->dk_geom.dkg_acyl * csize;
1663bae9e67eSachartre 	}
1664bae9e67eSachartre 
1665bae9e67eSachartre 	alen = length / DEV_BSIZE;
1666bae9e67eSachartre 	aoff = blk;
1667bae9e67eSachartre 
1668bae9e67eSachartre 	/* if we have reached the last block then the I/O is completed */
1669bae9e67eSachartre 	if (aoff == ablk + asize) {
1670bae9e67eSachartre 		status = 0;
1671bae9e67eSachartre 		goto done;
1672bae9e67eSachartre 	}
1673bae9e67eSachartre 
1674bae9e67eSachartre 	/* if we are past the last block then return an error */
1675bae9e67eSachartre 	if (aoff > ablk + asize)
1676bae9e67eSachartre 		return (EIO);
1677bae9e67eSachartre 
1678bae9e67eSachartre 	/* check if there is any I/O to end of the disk */
1679bae9e67eSachartre 	if (aoff + alen < ablk) {
1680bae9e67eSachartre 		status = EAGAIN;
1681bae9e67eSachartre 		goto done;
1682bae9e67eSachartre 	}
1683bae9e67eSachartre 
1684bae9e67eSachartre 	/* we don't allow any write to the end of the disk */
1685bae9e67eSachartre 	if (operation == VD_OP_BWRITE)
1686bae9e67eSachartre 		return (EIO);
1687bae9e67eSachartre 
1688bae9e67eSachartre 	if (aoff < ablk) {
1689bae9e67eSachartre 		alen -= (ablk - aoff);
1690bae9e67eSachartre 		aoff = ablk;
1691bae9e67eSachartre 	}
1692bae9e67eSachartre 
1693bae9e67eSachartre 	if (aoff + alen > ablk + asize) {
1694bae9e67eSachartre 		alen = ablk + asize - aoff;
1695bae9e67eSachartre 	}
1696bae9e67eSachartre 
1697bae9e67eSachartre 	alen *= DEV_BSIZE;
1698bae9e67eSachartre 
1699bae9e67eSachartre 	if (operation == VD_OP_BREAD) {
1700bae9e67eSachartre 		bzero(data + (aoff - blk) * DEV_BSIZE, alen);
1701bae9e67eSachartre 
1702bae9e67eSachartre 		if (vd->vdisk_label == VD_DISK_LABEL_VTOC) {
1703bae9e67eSachartre 			/* check if we read backup labels */
1704bae9e67eSachartre 			label = VD_LABEL_VTOC(vd);
1705bae9e67eSachartre 			ablk += (label->dkl_acyl - 1) * csize +
1706bae9e67eSachartre 			    (label->dkl_nhead - 1) * label->dkl_nsect;
1707bae9e67eSachartre 
1708bae9e67eSachartre 			for (sec = 1; (sec < 5 * 2 + 1); sec += 2) {
1709bae9e67eSachartre 
1710bae9e67eSachartre 				if (ablk + sec >= blk &&
1711bae9e67eSachartre 				    ablk + sec < blk + (length / DEV_BSIZE)) {
1712bae9e67eSachartre 					bcopy(label, data +
1713bae9e67eSachartre 					    (ablk + sec - blk) * DEV_BSIZE,
1714bae9e67eSachartre 					    sizeof (struct dk_label));
1715bae9e67eSachartre 				}
1716bae9e67eSachartre 			}
1717bae9e67eSachartre 		}
1718bae9e67eSachartre 	}
1719bae9e67eSachartre 
1720bae9e67eSachartre 	length -= alen;
1721bae9e67eSachartre 
1722bae9e67eSachartre 	status = (length == 0)? 0: EAGAIN;
1723bae9e67eSachartre 
1724bae9e67eSachartre done:
1725bae9e67eSachartre 	ASSERT(length == 0 || blk >= vd->flabel_limit);
1726bae9e67eSachartre 
1727bae9e67eSachartre 	/*
1728bae9e67eSachartre 	 * Return the parameters for the remaining I/O. The starting block is
1729bae9e67eSachartre 	 * adjusted so that it is relative to the vdisk backend.
1730bae9e67eSachartre 	 */
1731bae9e67eSachartre 	*datap = data;
1732bae9e67eSachartre 	*blkp = blk - vd->flabel_limit;
1733bae9e67eSachartre 	*lengthp = length;
1734bae9e67eSachartre 
1735bae9e67eSachartre 	return (status);
1736bae9e67eSachartre }
1737bae9e67eSachartre 
1738bae9e67eSachartre /*
1739205eeb1aSlm66018  * Return Values
1740205eeb1aSlm66018  *	EINPROGRESS	- operation was successfully started
1741205eeb1aSlm66018  *	EIO		- encountered LDC (aka. task error)
1742205eeb1aSlm66018  *	0		- operation completed successfully
1743205eeb1aSlm66018  *
1744205eeb1aSlm66018  * Side Effect
1745205eeb1aSlm66018  *     sets request->status = <disk operation status>
1746205eeb1aSlm66018  */
17471ae08745Sheppo static int
1748d10e4ef2Snarayan vd_start_bio(vd_task_t *task)
17491ae08745Sheppo {
17504bac2208Snarayan 	int			rv, status = 0;
1751d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
1752d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
1753d10e4ef2Snarayan 	struct buf		*buf		= &task->buf;
17544bac2208Snarayan 	uint8_t			mtype;
17553c96341aSnarayan 	int 			slice;
1756047ba61eSachartre 	char			*bufaddr = 0;
1757047ba61eSachartre 	size_t			buflen;
1758bae9e67eSachartre 	size_t			offset, length, nbytes;
1759d10e4ef2Snarayan 
1760d10e4ef2Snarayan 	ASSERT(vd != NULL);
1761d10e4ef2Snarayan 	ASSERT(request != NULL);
17623c96341aSnarayan 
17633c96341aSnarayan 	slice = request->slice;
17643c96341aSnarayan 
176587a7269eSachartre 	ASSERT(slice == VD_SLICE_NONE || slice < vd->nslices);
1766d10e4ef2Snarayan 	ASSERT((request->operation == VD_OP_BREAD) ||
1767d10e4ef2Snarayan 	    (request->operation == VD_OP_BWRITE));
1768d10e4ef2Snarayan 
1769205eeb1aSlm66018 	if (request->nbytes == 0) {
1770205eeb1aSlm66018 		/* no service for trivial requests */
1771205eeb1aSlm66018 		request->status = EINVAL;
1772205eeb1aSlm66018 		return (0);
1773205eeb1aSlm66018 	}
17741ae08745Sheppo 
1775d10e4ef2Snarayan 	PR1("%s %lu bytes at block %lu",
1776d10e4ef2Snarayan 	    (request->operation == VD_OP_BREAD) ? "Read" : "Write",
1777d10e4ef2Snarayan 	    request->nbytes, request->addr);
17781ae08745Sheppo 
1779047ba61eSachartre 	/*
1780047ba61eSachartre 	 * We have to check the open flags because the functions processing
1781047ba61eSachartre 	 * the read/write request will not do it.
1782047ba61eSachartre 	 */
1783047ba61eSachartre 	if (request->operation == VD_OP_BWRITE && !(vd->open_flags & FWRITE)) {
1784047ba61eSachartre 		PR0("write fails because backend is opened read-only");
1785047ba61eSachartre 		request->nbytes = 0;
1786047ba61eSachartre 		request->status = EROFS;
1787047ba61eSachartre 		return (0);
1788047ba61eSachartre 	}
1789d10e4ef2Snarayan 
17904bac2208Snarayan 	mtype = (&vd->inband_task == task) ? LDC_SHADOW_MAP : LDC_DIRECT_MAP;
17914bac2208Snarayan 
17924bac2208Snarayan 	/* Map memory exported by client */
17934bac2208Snarayan 	status = ldc_mem_map(task->mhdl, request->cookie, request->ncookies,
17944bac2208Snarayan 	    mtype, (request->operation == VD_OP_BREAD) ? LDC_MEM_W : LDC_MEM_R,
1795047ba61eSachartre 	    &bufaddr, NULL);
17964bac2208Snarayan 	if (status != 0) {
17973af08d82Slm66018 		PR0("ldc_mem_map() returned err %d ", status);
1798205eeb1aSlm66018 		return (EIO);
1799d10e4ef2Snarayan 	}
1800d10e4ef2Snarayan 
1801bae9e67eSachartre 	/*
1802bae9e67eSachartre 	 * The buffer size has to be 8-byte aligned, so the client should have
1803bae9e67eSachartre 	 * sent a buffer which size is roundup to the next 8-byte aligned value.
1804bae9e67eSachartre 	 */
1805bae9e67eSachartre 	buflen = P2ROUNDUP(request->nbytes, 8);
1806047ba61eSachartre 
1807047ba61eSachartre 	status = ldc_mem_acquire(task->mhdl, 0, buflen);
18084bac2208Snarayan 	if (status != 0) {
18094bac2208Snarayan 		(void) ldc_mem_unmap(task->mhdl);
18103af08d82Slm66018 		PR0("ldc_mem_acquire() returned err %d ", status);
1811205eeb1aSlm66018 		return (EIO);
18124bac2208Snarayan 	}
18134bac2208Snarayan 
1814bae9e67eSachartre 	offset = request->addr;
1815bae9e67eSachartre 	nbytes = request->nbytes;
1816bae9e67eSachartre 	length = nbytes;
1817bae9e67eSachartre 
1818bae9e67eSachartre 	/* default number of byte returned by the I/O */
1819bae9e67eSachartre 	request->nbytes = 0;
1820bae9e67eSachartre 
1821bae9e67eSachartre 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
1822bae9e67eSachartre 
1823bae9e67eSachartre 		if (slice != 0) {
1824bae9e67eSachartre 			/* handle any fake I/O */
1825bae9e67eSachartre 			rv = vd_slice_fake_rdwr(vd, slice, request->operation,
1826bae9e67eSachartre 			    &bufaddr, &offset, &length);
1827bae9e67eSachartre 
1828bae9e67eSachartre 			/* record the number of bytes from the fake I/O */
1829bae9e67eSachartre 			request->nbytes = nbytes - length;
1830bae9e67eSachartre 
1831bae9e67eSachartre 			if (rv == 0) {
1832bae9e67eSachartre 				request->status = 0;
1833bae9e67eSachartre 				goto io_done;
1834bae9e67eSachartre 			}
1835bae9e67eSachartre 
1836bae9e67eSachartre 			if (rv != EAGAIN) {
18373c96341aSnarayan 				request->nbytes = 0;
1838205eeb1aSlm66018 				request->status = EIO;
1839bae9e67eSachartre 				goto io_done;
18403c96341aSnarayan 			}
1841bae9e67eSachartre 
1842bae9e67eSachartre 			/*
1843bae9e67eSachartre 			 * If we return with EAGAIN then this means that there
1844bae9e67eSachartre 			 * are still data to read or write.
1845bae9e67eSachartre 			 */
1846bae9e67eSachartre 			ASSERT(length != 0);
1847bae9e67eSachartre 
1848bae9e67eSachartre 			/*
1849bae9e67eSachartre 			 * We need to continue the I/O from the slice backend to
1850bae9e67eSachartre 			 * complete the request. The variables bufaddr, offset
1851bae9e67eSachartre 			 * and length have been adjusted to have the right
1852bae9e67eSachartre 			 * information to do the remaining I/O from the backend.
1853bae9e67eSachartre 			 * The backend is entirely mapped to slice 0 so we just
1854bae9e67eSachartre 			 * have to complete the I/O from that slice.
1855bae9e67eSachartre 			 */
1856bae9e67eSachartre 			slice = 0;
1857bae9e67eSachartre 		}
1858bae9e67eSachartre 
1859bae9e67eSachartre 	} else if ((slice == VD_SLICE_NONE) && (!vd->file)) {
1860bae9e67eSachartre 
186187a7269eSachartre 		/*
186287a7269eSachartre 		 * This is not a disk image so it is a real disk. We
186387a7269eSachartre 		 * assume that the underlying device driver supports
186487a7269eSachartre 		 * USCSICMD ioctls. This is the case of all SCSI devices
186587a7269eSachartre 		 * (sd, ssd...).
186687a7269eSachartre 		 *
186787a7269eSachartre 		 * In the future if we have non-SCSI disks we would need
186887a7269eSachartre 		 * to invoke the appropriate function to do I/O using an
186917cadca8Slm66018 		 * absolute disk offset (for example using DIOCTL_RWCMD
187087a7269eSachartre 		 * for IDE disks).
187187a7269eSachartre 		 */
1872bae9e67eSachartre 		rv = vd_scsi_rdwr(vd, request->operation, bufaddr, offset,
1873bae9e67eSachartre 		    length);
187487a7269eSachartre 		if (rv != 0) {
1875bae9e67eSachartre 			request->status = EIO;
1876bae9e67eSachartre 		} else {
1877bae9e67eSachartre 			request->nbytes = length;
1878bae9e67eSachartre 			request->status = 0;
1879bae9e67eSachartre 		}
1880bae9e67eSachartre 		goto io_done;
1881bae9e67eSachartre 	}
1882bae9e67eSachartre 
1883bae9e67eSachartre 	/* Start the block I/O */
1884bae9e67eSachartre 	if (vd->file) {
1885bae9e67eSachartre 		rv = vd_file_rw(vd, slice, request->operation, bufaddr, offset,
1886bae9e67eSachartre 		    length);
1887bae9e67eSachartre 		if (rv < 0) {
188887a7269eSachartre 			request->nbytes = 0;
1889205eeb1aSlm66018 			request->status = EIO;
189087a7269eSachartre 		} else {
1891bae9e67eSachartre 			request->nbytes += rv;
1892205eeb1aSlm66018 			request->status = 0;
189387a7269eSachartre 		}
189487a7269eSachartre 	} else {
1895047ba61eSachartre 		bioinit(buf);
1896047ba61eSachartre 		buf->b_flags	= B_BUSY;
1897bae9e67eSachartre 		buf->b_bcount	= length;
1898bae9e67eSachartre 		buf->b_lblkno	= offset;
1899bae9e67eSachartre 		buf->b_bufsize	= buflen;
1900047ba61eSachartre 		buf->b_edev 	= vd->dev[slice];
1901047ba61eSachartre 		buf->b_un.b_addr = bufaddr;
1902047ba61eSachartre 		buf->b_flags 	|= (request->operation == VD_OP_BREAD)?
1903047ba61eSachartre 		    B_READ : B_WRITE;
1904047ba61eSachartre 
1905bae9e67eSachartre 		request->status = ldi_strategy(vd->ldi_handle[slice], buf);
1906205eeb1aSlm66018 
1907205eeb1aSlm66018 		/*
1908205eeb1aSlm66018 		 * This is to indicate to the caller that the request
1909205eeb1aSlm66018 		 * needs to be finished by vd_complete_bio() by calling
1910205eeb1aSlm66018 		 * biowait() there and waiting for that to return before
1911205eeb1aSlm66018 		 * triggering the notification of the vDisk client.
1912205eeb1aSlm66018 		 *
1913205eeb1aSlm66018 		 * This is necessary when writing to real disks as
1914205eeb1aSlm66018 		 * otherwise calls to ldi_strategy() would be serialized
1915205eeb1aSlm66018 		 * behind the calls to biowait() and performance would
1916205eeb1aSlm66018 		 * suffer.
1917205eeb1aSlm66018 		 */
1918205eeb1aSlm66018 		if (request->status == 0)
191987a7269eSachartre 			return (EINPROGRESS);
1920047ba61eSachartre 
1921047ba61eSachartre 		biofini(buf);
192287a7269eSachartre 	}
19233c96341aSnarayan 
1924bae9e67eSachartre io_done:
1925bae9e67eSachartre 	/* Clean up after error or completion */
1926047ba61eSachartre 	rv = ldc_mem_release(task->mhdl, 0, buflen);
19274bac2208Snarayan 	if (rv) {
19283af08d82Slm66018 		PR0("ldc_mem_release() returned err %d ", rv);
1929205eeb1aSlm66018 		status = EIO;
19304bac2208Snarayan 	}
19314bac2208Snarayan 	rv = ldc_mem_unmap(task->mhdl);
19324bac2208Snarayan 	if (rv) {
1933205eeb1aSlm66018 		PR0("ldc_mem_unmap() returned err %d ", rv);
1934205eeb1aSlm66018 		status = EIO;
19354bac2208Snarayan 	}
19364bac2208Snarayan 
1937d10e4ef2Snarayan 	return (status);
1938d10e4ef2Snarayan }
1939d10e4ef2Snarayan 
1940205eeb1aSlm66018 /*
1941205eeb1aSlm66018  * This function should only be called from vd_notify to ensure that requests
1942205eeb1aSlm66018  * are responded to in the order that they are received.
1943205eeb1aSlm66018  */
1944d10e4ef2Snarayan static int
1945d10e4ef2Snarayan send_msg(ldc_handle_t ldc_handle, void *msg, size_t msglen)
1946d10e4ef2Snarayan {
19473af08d82Slm66018 	int	status;
1948d10e4ef2Snarayan 	size_t	nbytes;
1949d10e4ef2Snarayan 
19503af08d82Slm66018 	do {
1951d10e4ef2Snarayan 		nbytes = msglen;
1952d10e4ef2Snarayan 		status = ldc_write(ldc_handle, msg, &nbytes);
19533af08d82Slm66018 		if (status != EWOULDBLOCK)
19543af08d82Slm66018 			break;
19553af08d82Slm66018 		drv_usecwait(vds_ldc_delay);
19563af08d82Slm66018 	} while (status == EWOULDBLOCK);
1957d10e4ef2Snarayan 
1958d10e4ef2Snarayan 	if (status != 0) {
19593af08d82Slm66018 		if (status != ECONNRESET)
19603af08d82Slm66018 			PR0("ldc_write() returned errno %d", status);
1961d10e4ef2Snarayan 		return (status);
1962d10e4ef2Snarayan 	} else if (nbytes != msglen) {
19633af08d82Slm66018 		PR0("ldc_write() performed only partial write");
1964d10e4ef2Snarayan 		return (EIO);
1965d10e4ef2Snarayan 	}
1966d10e4ef2Snarayan 
1967d10e4ef2Snarayan 	PR1("SENT %lu bytes", msglen);
1968d10e4ef2Snarayan 	return (0);
1969d10e4ef2Snarayan }
1970d10e4ef2Snarayan 
1971d10e4ef2Snarayan static void
1972d10e4ef2Snarayan vd_need_reset(vd_t *vd, boolean_t reset_ldc)
1973d10e4ef2Snarayan {
1974d10e4ef2Snarayan 	mutex_enter(&vd->lock);
1975d10e4ef2Snarayan 	vd->reset_state	= B_TRUE;
1976d10e4ef2Snarayan 	vd->reset_ldc	= reset_ldc;
1977d10e4ef2Snarayan 	mutex_exit(&vd->lock);
1978d10e4ef2Snarayan }
1979d10e4ef2Snarayan 
1980d10e4ef2Snarayan /*
1981d10e4ef2Snarayan  * Reset the state of the connection with a client, if needed; reset the LDC
1982d10e4ef2Snarayan  * transport as well, if needed.  This function should only be called from the
19833af08d82Slm66018  * "vd_recv_msg", as it waits for tasks - otherwise a deadlock can occur.
1984d10e4ef2Snarayan  */
1985d10e4ef2Snarayan static void
1986d10e4ef2Snarayan vd_reset_if_needed(vd_t *vd)
1987d10e4ef2Snarayan {
1988d10e4ef2Snarayan 	int	status = 0;
1989d10e4ef2Snarayan 
1990d10e4ef2Snarayan 	mutex_enter(&vd->lock);
1991d10e4ef2Snarayan 	if (!vd->reset_state) {
1992d10e4ef2Snarayan 		ASSERT(!vd->reset_ldc);
1993d10e4ef2Snarayan 		mutex_exit(&vd->lock);
1994d10e4ef2Snarayan 		return;
1995d10e4ef2Snarayan 	}
1996d10e4ef2Snarayan 	mutex_exit(&vd->lock);
1997d10e4ef2Snarayan 
1998d10e4ef2Snarayan 	PR0("Resetting connection state with %s", VD_CLIENT(vd));
1999d10e4ef2Snarayan 
2000d10e4ef2Snarayan 	/*
2001d10e4ef2Snarayan 	 * Let any asynchronous I/O complete before possibly pulling the rug
2002d10e4ef2Snarayan 	 * out from under it; defer checking vd->reset_ldc, as one of the
2003d10e4ef2Snarayan 	 * asynchronous tasks might set it
2004d10e4ef2Snarayan 	 */
2005d10e4ef2Snarayan 	ddi_taskq_wait(vd->completionq);
2006d10e4ef2Snarayan 
20073c96341aSnarayan 	if (vd->file) {
2008da6c28aaSamw 		status = VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL);
20093c96341aSnarayan 		if (status) {
20103c96341aSnarayan 			PR0("VOP_FSYNC returned errno %d", status);
20113c96341aSnarayan 		}
20123c96341aSnarayan 	}
20133c96341aSnarayan 
2014d10e4ef2Snarayan 	if ((vd->initialized & VD_DRING) &&
2015d10e4ef2Snarayan 	    ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0))
20163af08d82Slm66018 		PR0("ldc_mem_dring_unmap() returned errno %d", status);
2017d10e4ef2Snarayan 
20183af08d82Slm66018 	vd_free_dring_task(vd);
20193af08d82Slm66018 
20203af08d82Slm66018 	/* Free the staging buffer for msgs */
20213af08d82Slm66018 	if (vd->vio_msgp != NULL) {
20223af08d82Slm66018 		kmem_free(vd->vio_msgp, vd->max_msglen);
20233af08d82Slm66018 		vd->vio_msgp = NULL;
2024d10e4ef2Snarayan 	}
2025d10e4ef2Snarayan 
20263af08d82Slm66018 	/* Free the inband message buffer */
20273af08d82Slm66018 	if (vd->inband_task.msg != NULL) {
20283af08d82Slm66018 		kmem_free(vd->inband_task.msg, vd->max_msglen);
20293af08d82Slm66018 		vd->inband_task.msg = NULL;
20303af08d82Slm66018 	}
2031d10e4ef2Snarayan 
2032d10e4ef2Snarayan 	mutex_enter(&vd->lock);
20333af08d82Slm66018 
20343af08d82Slm66018 	if (vd->reset_ldc)
20353af08d82Slm66018 		PR0("taking down LDC channel");
2036e1ebb9ecSlm66018 	if (vd->reset_ldc && ((status = ldc_down(vd->ldc_handle)) != 0))
20373af08d82Slm66018 		PR0("ldc_down() returned errno %d", status);
2038d10e4ef2Snarayan 
20392f5224aeSachartre 	/* Reset exclusive access rights */
20402f5224aeSachartre 	vd_reset_access(vd);
20412f5224aeSachartre 
2042d10e4ef2Snarayan 	vd->initialized	&= ~(VD_SID | VD_SEQ_NUM | VD_DRING);
2043d10e4ef2Snarayan 	vd->state	= VD_STATE_INIT;
2044d10e4ef2Snarayan 	vd->max_msglen	= sizeof (vio_msg_t);	/* baseline vio message size */
2045d10e4ef2Snarayan 
20463af08d82Slm66018 	/* Allocate the staging buffer */
20473af08d82Slm66018 	vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
20483af08d82Slm66018 
20493af08d82Slm66018 	PR0("calling ldc_up\n");
20503af08d82Slm66018 	(void) ldc_up(vd->ldc_handle);
20513af08d82Slm66018 
2052d10e4ef2Snarayan 	vd->reset_state	= B_FALSE;
2053d10e4ef2Snarayan 	vd->reset_ldc	= B_FALSE;
20543af08d82Slm66018 
2055d10e4ef2Snarayan 	mutex_exit(&vd->lock);
2056d10e4ef2Snarayan }
2057d10e4ef2Snarayan 
20583af08d82Slm66018 static void vd_recv_msg(void *arg);
20593af08d82Slm66018 
20603af08d82Slm66018 static void
20613af08d82Slm66018 vd_mark_in_reset(vd_t *vd)
20623af08d82Slm66018 {
20633af08d82Slm66018 	int status;
20643af08d82Slm66018 
20653af08d82Slm66018 	PR0("vd_mark_in_reset: marking vd in reset\n");
20663af08d82Slm66018 
20673af08d82Slm66018 	vd_need_reset(vd, B_FALSE);
20683af08d82Slm66018 	status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, DDI_SLEEP);
20693af08d82Slm66018 	if (status == DDI_FAILURE) {
20703af08d82Slm66018 		PR0("cannot schedule task to recv msg\n");
20713af08d82Slm66018 		vd_need_reset(vd, B_TRUE);
20723af08d82Slm66018 		return;
20733af08d82Slm66018 	}
20743af08d82Slm66018 }
20753af08d82Slm66018 
2076d10e4ef2Snarayan static int
20773c96341aSnarayan vd_mark_elem_done(vd_t *vd, int idx, int elem_status, int elem_nbytes)
2078d10e4ef2Snarayan {
2079d10e4ef2Snarayan 	boolean_t		accepted;
2080d10e4ef2Snarayan 	int			status;
2081bbfa0259Sha137994 	on_trap_data_t		otd;
2082d10e4ef2Snarayan 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
2083d10e4ef2Snarayan 
20843af08d82Slm66018 	if (vd->reset_state)
20853af08d82Slm66018 		return (0);
2086d10e4ef2Snarayan 
2087d10e4ef2Snarayan 	/* Acquire the element */
2088bbfa0259Sha137994 	if ((status = VIO_DRING_ACQUIRE(&otd, vd->dring_mtype,
2089bbfa0259Sha137994 	    vd->dring_handle, idx, idx)) != 0) {
20903af08d82Slm66018 		if (status == ECONNRESET) {
20913af08d82Slm66018 			vd_mark_in_reset(vd);
20923af08d82Slm66018 			return (0);
20933af08d82Slm66018 		} else {
2094d10e4ef2Snarayan 			return (status);
2095d10e4ef2Snarayan 		}
20963af08d82Slm66018 	}
2097d10e4ef2Snarayan 
2098d10e4ef2Snarayan 	/* Set the element's status and mark it done */
2099d10e4ef2Snarayan 	accepted = (elem->hdr.dstate == VIO_DESC_ACCEPTED);
2100d10e4ef2Snarayan 	if (accepted) {
21013c96341aSnarayan 		elem->payload.nbytes	= elem_nbytes;
2102d10e4ef2Snarayan 		elem->payload.status	= elem_status;
2103d10e4ef2Snarayan 		elem->hdr.dstate	= VIO_DESC_DONE;
2104d10e4ef2Snarayan 	} else {
2105d10e4ef2Snarayan 		/* Perhaps client timed out waiting for I/O... */
21063af08d82Slm66018 		PR0("element %u no longer \"accepted\"", idx);
2107d10e4ef2Snarayan 		VD_DUMP_DRING_ELEM(elem);
2108d10e4ef2Snarayan 	}
2109d10e4ef2Snarayan 	/* Release the element */
2110bbfa0259Sha137994 	if ((status = VIO_DRING_RELEASE(vd->dring_mtype,
2111bbfa0259Sha137994 	    vd->dring_handle, idx, idx)) != 0) {
21123af08d82Slm66018 		if (status == ECONNRESET) {
21133af08d82Slm66018 			vd_mark_in_reset(vd);
21143af08d82Slm66018 			return (0);
21153af08d82Slm66018 		} else {
2116bbfa0259Sha137994 			PR0("VIO_DRING_RELEASE() returned errno %d",
21173af08d82Slm66018 			    status);
2118d10e4ef2Snarayan 			return (status);
2119d10e4ef2Snarayan 		}
21203af08d82Slm66018 	}
2121d10e4ef2Snarayan 
2122d10e4ef2Snarayan 	return (accepted ? 0 : EINVAL);
2123d10e4ef2Snarayan }
2124d10e4ef2Snarayan 
2125205eeb1aSlm66018 /*
2126205eeb1aSlm66018  * Return Values
2127205eeb1aSlm66018  *	0	- operation completed successfully
2128205eeb1aSlm66018  *	EIO	- encountered LDC / task error
2129205eeb1aSlm66018  *
2130205eeb1aSlm66018  * Side Effect
2131205eeb1aSlm66018  *	sets request->status = <disk operation status>
2132205eeb1aSlm66018  */
2133205eeb1aSlm66018 static int
2134205eeb1aSlm66018 vd_complete_bio(vd_task_t *task)
2135d10e4ef2Snarayan {
2136d10e4ef2Snarayan 	int			status		= 0;
2137205eeb1aSlm66018 	int			rv		= 0;
2138d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
2139d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
2140d10e4ef2Snarayan 	struct buf		*buf		= &task->buf;
2141d10e4ef2Snarayan 
2142d10e4ef2Snarayan 
2143d10e4ef2Snarayan 	ASSERT(vd != NULL);
2144d10e4ef2Snarayan 	ASSERT(request != NULL);
2145d10e4ef2Snarayan 	ASSERT(task->msg != NULL);
2146d10e4ef2Snarayan 	ASSERT(task->msglen >= sizeof (*task->msg));
21473c96341aSnarayan 	ASSERT(!vd->file);
2148bae9e67eSachartre 	ASSERT(request->slice != VD_SLICE_NONE || (!vd_slice_single_slice &&
2149bae9e67eSachartre 	    vd->vdisk_type == VD_DISK_TYPE_SLICE));
2150d10e4ef2Snarayan 
2151205eeb1aSlm66018 	/* Wait for the I/O to complete [ call to ldi_strategy(9f) ] */
2152d10e4ef2Snarayan 	request->status = biowait(buf);
2153d10e4ef2Snarayan 
2154bae9e67eSachartre 	/* Update the number of bytes read/written */
2155bae9e67eSachartre 	request->nbytes += buf->b_bcount - buf->b_resid;
21563c96341aSnarayan 
21574bac2208Snarayan 	/* Release the buffer */
21583af08d82Slm66018 	if (!vd->reset_state)
2159bae9e67eSachartre 		status = ldc_mem_release(task->mhdl, 0, buf->b_bufsize);
21604bac2208Snarayan 	if (status) {
21613af08d82Slm66018 		PR0("ldc_mem_release() returned errno %d copying to "
21623af08d82Slm66018 		    "client", status);
21633af08d82Slm66018 		if (status == ECONNRESET) {
21643af08d82Slm66018 			vd_mark_in_reset(vd);
21653af08d82Slm66018 		}
2166205eeb1aSlm66018 		rv = EIO;
21671ae08745Sheppo 	}
21681ae08745Sheppo 
21693af08d82Slm66018 	/* Unmap the memory, even if in reset */
21704bac2208Snarayan 	status = ldc_mem_unmap(task->mhdl);
21714bac2208Snarayan 	if (status) {
21723af08d82Slm66018 		PR0("ldc_mem_unmap() returned errno %d copying to client",
21734bac2208Snarayan 		    status);
21743af08d82Slm66018 		if (status == ECONNRESET) {
21753af08d82Slm66018 			vd_mark_in_reset(vd);
21763af08d82Slm66018 		}
2177205eeb1aSlm66018 		rv = EIO;
21784bac2208Snarayan 	}
21794bac2208Snarayan 
2180d10e4ef2Snarayan 	biofini(buf);
21811ae08745Sheppo 
2182205eeb1aSlm66018 	return (rv);
2183205eeb1aSlm66018 }
2184205eeb1aSlm66018 
2185205eeb1aSlm66018 /*
2186205eeb1aSlm66018  * Description:
2187205eeb1aSlm66018  *	This function is called by the two functions called by a taskq
2188205eeb1aSlm66018  *	[ vd_complete_notify() and vd_serial_notify()) ] to send the
2189205eeb1aSlm66018  *	message to the client.
2190205eeb1aSlm66018  *
2191205eeb1aSlm66018  * Parameters:
2192205eeb1aSlm66018  *	arg 	- opaque pointer to structure containing task to be completed
2193205eeb1aSlm66018  *
2194205eeb1aSlm66018  * Return Values
2195205eeb1aSlm66018  *	None
2196205eeb1aSlm66018  */
2197205eeb1aSlm66018 static void
2198205eeb1aSlm66018 vd_notify(vd_task_t *task)
2199205eeb1aSlm66018 {
2200205eeb1aSlm66018 	int	status;
2201205eeb1aSlm66018 
2202205eeb1aSlm66018 	ASSERT(task != NULL);
2203205eeb1aSlm66018 	ASSERT(task->vd != NULL);
2204205eeb1aSlm66018 
2205205eeb1aSlm66018 	/*
2206205eeb1aSlm66018 	 * Send the "ack" or "nack" back to the client; if sending the message
2207205eeb1aSlm66018 	 * via LDC fails, arrange to reset both the connection state and LDC
2208205eeb1aSlm66018 	 * itself
2209205eeb1aSlm66018 	 */
2210205eeb1aSlm66018 	PR2("Sending %s",
2211205eeb1aSlm66018 	    (task->msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK");
2212205eeb1aSlm66018 
2213205eeb1aSlm66018 	status = send_msg(task->vd->ldc_handle, task->msg, task->msglen);
2214205eeb1aSlm66018 	switch (status) {
2215205eeb1aSlm66018 	case 0:
2216205eeb1aSlm66018 		break;
2217205eeb1aSlm66018 	case ECONNRESET:
2218205eeb1aSlm66018 		vd_mark_in_reset(task->vd);
2219205eeb1aSlm66018 		break;
2220205eeb1aSlm66018 	default:
2221205eeb1aSlm66018 		PR0("initiating full reset");
2222205eeb1aSlm66018 		vd_need_reset(task->vd, B_TRUE);
2223205eeb1aSlm66018 		break;
2224205eeb1aSlm66018 	}
2225205eeb1aSlm66018 
2226205eeb1aSlm66018 	DTRACE_PROBE1(task__end, vd_task_t *, task);
2227205eeb1aSlm66018 }
2228205eeb1aSlm66018 
2229205eeb1aSlm66018 /*
2230205eeb1aSlm66018  * Description:
2231205eeb1aSlm66018  *	Mark the Dring entry as Done and (if necessary) send an ACK/NACK to
2232205eeb1aSlm66018  *	the vDisk client
2233205eeb1aSlm66018  *
2234205eeb1aSlm66018  * Parameters:
2235205eeb1aSlm66018  *	task 		- structure containing the request sent from client
2236205eeb1aSlm66018  *
2237205eeb1aSlm66018  * Return Values
2238205eeb1aSlm66018  *	None
2239205eeb1aSlm66018  */
2240205eeb1aSlm66018 static void
2241205eeb1aSlm66018 vd_complete_notify(vd_task_t *task)
2242205eeb1aSlm66018 {
2243205eeb1aSlm66018 	int			status		= 0;
2244205eeb1aSlm66018 	vd_t			*vd		= task->vd;
2245205eeb1aSlm66018 	vd_dring_payload_t	*request	= task->request;
2246205eeb1aSlm66018 
2247d10e4ef2Snarayan 	/* Update the dring element for a dring client */
2248f0ca1d9aSsb155480 	if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE_V1_0)) {
22493c96341aSnarayan 		status = vd_mark_elem_done(vd, task->index,
22503c96341aSnarayan 		    request->status, request->nbytes);
22513af08d82Slm66018 		if (status == ECONNRESET)
22523af08d82Slm66018 			vd_mark_in_reset(vd);
2253bbfa0259Sha137994 		else if (status == EACCES)
2254bbfa0259Sha137994 			vd_need_reset(vd, B_TRUE);
22553af08d82Slm66018 	}
22561ae08745Sheppo 
2257d10e4ef2Snarayan 	/*
2258205eeb1aSlm66018 	 * If a transport error occurred while marking the element done or
2259205eeb1aSlm66018 	 * previously while executing the task, arrange to "nack" the message
2260205eeb1aSlm66018 	 * when the final task in the descriptor element range completes
2261d10e4ef2Snarayan 	 */
2262205eeb1aSlm66018 	if ((status != 0) || (task->status != 0))
2263d10e4ef2Snarayan 		task->msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
22641ae08745Sheppo 
2265d10e4ef2Snarayan 	/*
2266d10e4ef2Snarayan 	 * Only the final task for a range of elements will respond to and
2267d10e4ef2Snarayan 	 * free the message
2268d10e4ef2Snarayan 	 */
22693af08d82Slm66018 	if (task->type == VD_NONFINAL_RANGE_TASK) {
2270d10e4ef2Snarayan 		return;
22713af08d82Slm66018 	}
22721ae08745Sheppo 
227327ac699dSzk194757 	/*
227427ac699dSzk194757 	 * We should only send an ACK/NACK here if we are not currently in
227527ac699dSzk194757 	 * reset as, depending on how we reset, the dring may have been
227627ac699dSzk194757 	 * blown away and we don't want to ACK/NACK a message that isn't
227727ac699dSzk194757 	 * there.
227827ac699dSzk194757 	 */
227927ac699dSzk194757 	if (!vd->reset_state)
2280205eeb1aSlm66018 		vd_notify(task);
2281205eeb1aSlm66018 }
2282205eeb1aSlm66018 
2283d10e4ef2Snarayan /*
2284205eeb1aSlm66018  * Description:
2285205eeb1aSlm66018  *	This is the basic completion function called to handle inband data
2286205eeb1aSlm66018  *	requests and handshake messages. All it needs to do is trigger a
2287205eeb1aSlm66018  *	message to the client that the request is completed.
2288205eeb1aSlm66018  *
2289205eeb1aSlm66018  * Parameters:
2290205eeb1aSlm66018  *	arg 	- opaque pointer to structure containing task to be completed
2291205eeb1aSlm66018  *
2292205eeb1aSlm66018  * Return Values
2293205eeb1aSlm66018  *	None
2294d10e4ef2Snarayan  */
2295205eeb1aSlm66018 static void
2296205eeb1aSlm66018 vd_serial_notify(void *arg)
2297205eeb1aSlm66018 {
2298205eeb1aSlm66018 	vd_task_t		*task = (vd_task_t *)arg;
2299205eeb1aSlm66018 
2300205eeb1aSlm66018 	ASSERT(task != NULL);
2301205eeb1aSlm66018 	vd_notify(task);
23021ae08745Sheppo }
23031ae08745Sheppo 
23042f5224aeSachartre /* ARGSUSED */
23052f5224aeSachartre static int
23062f5224aeSachartre vd_geom2dk_geom(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
23070a55fbb7Slm66018 {
23080a55fbb7Slm66018 	VD_GEOM2DK_GEOM((vd_geom_t *)vd_buf, (struct dk_geom *)ioctl_arg);
23092f5224aeSachartre 	return (0);
23100a55fbb7Slm66018 }
23110a55fbb7Slm66018 
23122f5224aeSachartre /* ARGSUSED */
23132f5224aeSachartre static int
23142f5224aeSachartre vd_vtoc2vtoc(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
23150a55fbb7Slm66018 {
23160a55fbb7Slm66018 	VD_VTOC2VTOC((vd_vtoc_t *)vd_buf, (struct vtoc *)ioctl_arg);
23172f5224aeSachartre 	return (0);
23180a55fbb7Slm66018 }
23190a55fbb7Slm66018 
23200a55fbb7Slm66018 static void
23210a55fbb7Slm66018 dk_geom2vd_geom(void *ioctl_arg, void *vd_buf)
23220a55fbb7Slm66018 {
23230a55fbb7Slm66018 	DK_GEOM2VD_GEOM((struct dk_geom *)ioctl_arg, (vd_geom_t *)vd_buf);
23240a55fbb7Slm66018 }
23250a55fbb7Slm66018 
23260a55fbb7Slm66018 static void
23270a55fbb7Slm66018 vtoc2vd_vtoc(void *ioctl_arg, void *vd_buf)
23280a55fbb7Slm66018 {
23290a55fbb7Slm66018 	VTOC2VD_VTOC((struct vtoc *)ioctl_arg, (vd_vtoc_t *)vd_buf);
23300a55fbb7Slm66018 }
23310a55fbb7Slm66018 
23322f5224aeSachartre static int
23332f5224aeSachartre vd_get_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
23344bac2208Snarayan {
23354bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
23364bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
23372f5224aeSachartre 	size_t data_len;
23382f5224aeSachartre 
23392f5224aeSachartre 	data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t));
23402f5224aeSachartre 	if (vd_efi->length > data_len)
23412f5224aeSachartre 		return (EINVAL);
23424bac2208Snarayan 
23434bac2208Snarayan 	dk_efi->dki_lba = vd_efi->lba;
23444bac2208Snarayan 	dk_efi->dki_length = vd_efi->length;
23454bac2208Snarayan 	dk_efi->dki_data = kmem_zalloc(vd_efi->length, KM_SLEEP);
23462f5224aeSachartre 	return (0);
23474bac2208Snarayan }
23484bac2208Snarayan 
23494bac2208Snarayan static void
23504bac2208Snarayan vd_get_efi_out(void *ioctl_arg, void *vd_buf)
23514bac2208Snarayan {
23524bac2208Snarayan 	int len;
23534bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
23544bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
23554bac2208Snarayan 
23564bac2208Snarayan 	len = vd_efi->length;
23574bac2208Snarayan 	DK_EFI2VD_EFI(dk_efi, vd_efi);
23584bac2208Snarayan 	kmem_free(dk_efi->dki_data, len);
23594bac2208Snarayan }
23604bac2208Snarayan 
23612f5224aeSachartre static int
23622f5224aeSachartre vd_set_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
23634bac2208Snarayan {
23644bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
23654bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
23662f5224aeSachartre 	size_t data_len;
23672f5224aeSachartre 
23682f5224aeSachartre 	data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t));
23692f5224aeSachartre 	if (vd_efi->length > data_len)
23702f5224aeSachartre 		return (EINVAL);
23714bac2208Snarayan 
23724bac2208Snarayan 	dk_efi->dki_data = kmem_alloc(vd_efi->length, KM_SLEEP);
23734bac2208Snarayan 	VD_EFI2DK_EFI(vd_efi, dk_efi);
23742f5224aeSachartre 	return (0);
23754bac2208Snarayan }
23764bac2208Snarayan 
23774bac2208Snarayan static void
23784bac2208Snarayan vd_set_efi_out(void *ioctl_arg, void *vd_buf)
23794bac2208Snarayan {
23804bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
23814bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
23824bac2208Snarayan 
23834bac2208Snarayan 	kmem_free(dk_efi->dki_data, vd_efi->length);
23844bac2208Snarayan }
23854bac2208Snarayan 
23862f5224aeSachartre static int
23872f5224aeSachartre vd_scsicmd_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
23882f5224aeSachartre {
23892f5224aeSachartre 	size_t vd_scsi_len;
23902f5224aeSachartre 	vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf;
23912f5224aeSachartre 	struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg;
23922f5224aeSachartre 
23932f5224aeSachartre 	/* check buffer size */
23942f5224aeSachartre 	vd_scsi_len = VD_SCSI_SIZE;
23952f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(vd_scsi->cdb_len, sizeof (uint64_t));
23962f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(vd_scsi->sense_len, sizeof (uint64_t));
23972f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(vd_scsi->datain_len, sizeof (uint64_t));
23982f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(vd_scsi->dataout_len, sizeof (uint64_t));
23992f5224aeSachartre 
24002f5224aeSachartre 	ASSERT(vd_scsi_len % sizeof (uint64_t) == 0);
24012f5224aeSachartre 
24022f5224aeSachartre 	if (vd_buf_len < vd_scsi_len)
24032f5224aeSachartre 		return (EINVAL);
24042f5224aeSachartre 
24052f5224aeSachartre 	/* set flags */
24062f5224aeSachartre 	uscsi->uscsi_flags = vd_scsi_debug;
24072f5224aeSachartre 
24082f5224aeSachartre 	if (vd_scsi->options & VD_SCSI_OPT_NORETRY) {
24092f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_ISOLATE;
24102f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_DIAGNOSE;
24112f5224aeSachartre 	}
24122f5224aeSachartre 
24132f5224aeSachartre 	/* task attribute */
24142f5224aeSachartre 	switch (vd_scsi->task_attribute) {
24152f5224aeSachartre 	case VD_SCSI_TASK_ACA:
24162f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_HEAD;
24172f5224aeSachartre 		break;
24182f5224aeSachartre 	case VD_SCSI_TASK_HQUEUE:
24192f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_HTAG;
24202f5224aeSachartre 		break;
24212f5224aeSachartre 	case VD_SCSI_TASK_ORDERED:
24222f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_OTAG;
24232f5224aeSachartre 		break;
24242f5224aeSachartre 	default:
24252f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_NOTAG;
24262f5224aeSachartre 		break;
24272f5224aeSachartre 	}
24282f5224aeSachartre 
24292f5224aeSachartre 	/* timeout */
24302f5224aeSachartre 	uscsi->uscsi_timeout = vd_scsi->timeout;
24312f5224aeSachartre 
24322f5224aeSachartre 	/* cdb data */
24332f5224aeSachartre 	uscsi->uscsi_cdb = (caddr_t)VD_SCSI_DATA_CDB(vd_scsi);
24342f5224aeSachartre 	uscsi->uscsi_cdblen = vd_scsi->cdb_len;
24352f5224aeSachartre 
24362f5224aeSachartre 	/* sense buffer */
24372f5224aeSachartre 	if (vd_scsi->sense_len != 0) {
24382f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_RQENABLE;
24392f5224aeSachartre 		uscsi->uscsi_rqbuf = (caddr_t)VD_SCSI_DATA_SENSE(vd_scsi);
24402f5224aeSachartre 		uscsi->uscsi_rqlen = vd_scsi->sense_len;
24412f5224aeSachartre 	}
24422f5224aeSachartre 
24432f5224aeSachartre 	if (vd_scsi->datain_len != 0 && vd_scsi->dataout_len != 0) {
24442f5224aeSachartre 		/* uscsi does not support read/write request */
24452f5224aeSachartre 		return (EINVAL);
24462f5224aeSachartre 	}
24472f5224aeSachartre 
24482f5224aeSachartre 	/* request data-in */
24492f5224aeSachartre 	if (vd_scsi->datain_len != 0) {
24502f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_READ;
24512f5224aeSachartre 		uscsi->uscsi_buflen = vd_scsi->datain_len;
24522f5224aeSachartre 		uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_IN(vd_scsi);
24532f5224aeSachartre 	}
24542f5224aeSachartre 
24552f5224aeSachartre 	/* request data-out */
24562f5224aeSachartre 	if (vd_scsi->dataout_len != 0) {
24572f5224aeSachartre 		uscsi->uscsi_buflen = vd_scsi->dataout_len;
24582f5224aeSachartre 		uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_OUT(vd_scsi);
24592f5224aeSachartre 	}
24602f5224aeSachartre 
24612f5224aeSachartre 	return (0);
24622f5224aeSachartre }
24632f5224aeSachartre 
24642f5224aeSachartre static void
24652f5224aeSachartre vd_scsicmd_out(void *ioctl_arg, void *vd_buf)
24662f5224aeSachartre {
24672f5224aeSachartre 	vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf;
24682f5224aeSachartre 	struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg;
24692f5224aeSachartre 
24702f5224aeSachartre 	/* output fields */
24712f5224aeSachartre 	vd_scsi->cmd_status = uscsi->uscsi_status;
24722f5224aeSachartre 
24732f5224aeSachartre 	/* sense data */
24742f5224aeSachartre 	if ((uscsi->uscsi_flags & USCSI_RQENABLE) &&
24752f5224aeSachartre 	    (uscsi->uscsi_status == STATUS_CHECK ||
24762f5224aeSachartre 	    uscsi->uscsi_status == STATUS_TERMINATED)) {
24772f5224aeSachartre 		vd_scsi->sense_status = uscsi->uscsi_rqstatus;
24782f5224aeSachartre 		if (uscsi->uscsi_rqstatus == STATUS_GOOD)
247914466a20Szk194757 			vd_scsi->sense_len -= uscsi->uscsi_rqresid;
24802f5224aeSachartre 		else
24812f5224aeSachartre 			vd_scsi->sense_len = 0;
24822f5224aeSachartre 	} else {
24832f5224aeSachartre 		vd_scsi->sense_len = 0;
24842f5224aeSachartre 	}
24852f5224aeSachartre 
24862f5224aeSachartre 	if (uscsi->uscsi_status != STATUS_GOOD) {
24872f5224aeSachartre 		vd_scsi->dataout_len = 0;
24882f5224aeSachartre 		vd_scsi->datain_len = 0;
24892f5224aeSachartre 		return;
24902f5224aeSachartre 	}
24912f5224aeSachartre 
24922f5224aeSachartre 	if (uscsi->uscsi_flags & USCSI_READ) {
24932f5224aeSachartre 		/* request data (read) */
24942f5224aeSachartre 		vd_scsi->datain_len -= uscsi->uscsi_resid;
24952f5224aeSachartre 		vd_scsi->dataout_len = 0;
24962f5224aeSachartre 	} else {
24972f5224aeSachartre 		/* request data (write) */
24982f5224aeSachartre 		vd_scsi->datain_len = 0;
24992f5224aeSachartre 		vd_scsi->dataout_len -= uscsi->uscsi_resid;
25002f5224aeSachartre 	}
25012f5224aeSachartre }
25022f5224aeSachartre 
2503690555a1Sachartre static ushort_t
25043c96341aSnarayan vd_lbl2cksum(struct dk_label *label)
25053c96341aSnarayan {
25063c96341aSnarayan 	int	count;
2507690555a1Sachartre 	ushort_t sum, *sp;
25083c96341aSnarayan 
25093c96341aSnarayan 	count =	(sizeof (struct dk_label)) / (sizeof (short)) - 1;
2510690555a1Sachartre 	sp = (ushort_t *)label;
25113c96341aSnarayan 	sum = 0;
25123c96341aSnarayan 	while (count--) {
25133c96341aSnarayan 		sum ^= *sp++;
25143c96341aSnarayan 	}
25153c96341aSnarayan 
25163c96341aSnarayan 	return (sum);
25173c96341aSnarayan }
25183c96341aSnarayan 
251987a7269eSachartre /*
2520bae9e67eSachartre  * Copy information from a vtoc and dk_geom structures to a dk_label structure.
2521bae9e67eSachartre  */
2522bae9e67eSachartre static void
2523bae9e67eSachartre vd_vtocgeom_to_label(struct vtoc *vtoc, struct dk_geom *geom,
2524bae9e67eSachartre     struct dk_label *label)
2525bae9e67eSachartre {
2526bae9e67eSachartre 	int i;
2527bae9e67eSachartre 
2528bae9e67eSachartre 	ASSERT(vtoc->v_nparts == V_NUMPAR);
2529bae9e67eSachartre 	ASSERT(vtoc->v_sanity == VTOC_SANE);
2530bae9e67eSachartre 
2531bae9e67eSachartre 	bzero(label, sizeof (struct dk_label));
2532bae9e67eSachartre 
2533bae9e67eSachartre 	label->dkl_ncyl = geom->dkg_ncyl;
2534bae9e67eSachartre 	label->dkl_acyl = geom->dkg_acyl;
2535bae9e67eSachartre 	label->dkl_pcyl = geom->dkg_pcyl;
2536bae9e67eSachartre 	label->dkl_nhead = geom->dkg_nhead;
2537bae9e67eSachartre 	label->dkl_nsect = geom->dkg_nsect;
2538bae9e67eSachartre 	label->dkl_intrlv = geom->dkg_intrlv;
2539bae9e67eSachartre 	label->dkl_apc = geom->dkg_apc;
2540bae9e67eSachartre 	label->dkl_rpm = geom->dkg_rpm;
2541bae9e67eSachartre 	label->dkl_write_reinstruct = geom->dkg_write_reinstruct;
2542bae9e67eSachartre 	label->dkl_read_reinstruct = geom->dkg_read_reinstruct;
2543bae9e67eSachartre 
2544bae9e67eSachartre 	label->dkl_vtoc.v_nparts = V_NUMPAR;
2545bae9e67eSachartre 	label->dkl_vtoc.v_sanity = VTOC_SANE;
2546bae9e67eSachartre 	label->dkl_vtoc.v_version = vtoc->v_version;
2547bae9e67eSachartre 	for (i = 0; i < V_NUMPAR; i++) {
2548bae9e67eSachartre 		label->dkl_vtoc.v_timestamp[i] = vtoc->timestamp[i];
2549bae9e67eSachartre 		label->dkl_vtoc.v_part[i].p_tag = vtoc->v_part[i].p_tag;
2550bae9e67eSachartre 		label->dkl_vtoc.v_part[i].p_flag = vtoc->v_part[i].p_flag;
2551bae9e67eSachartre 		label->dkl_map[i].dkl_cylno = vtoc->v_part[i].p_start /
2552bae9e67eSachartre 		    (label->dkl_nhead * label->dkl_nsect);
2553bae9e67eSachartre 		label->dkl_map[i].dkl_nblk = vtoc->v_part[i].p_size;
2554bae9e67eSachartre 	}
2555bae9e67eSachartre 
2556bae9e67eSachartre 	/*
2557bae9e67eSachartre 	 * The bootinfo array can not be copied with bcopy() because
2558bae9e67eSachartre 	 * elements are of type long in vtoc (so 64-bit) and of type
2559bae9e67eSachartre 	 * int in dk_vtoc (so 32-bit).
2560bae9e67eSachartre 	 */
2561bae9e67eSachartre 	label->dkl_vtoc.v_bootinfo[0] = vtoc->v_bootinfo[0];
2562bae9e67eSachartre 	label->dkl_vtoc.v_bootinfo[1] = vtoc->v_bootinfo[1];
2563bae9e67eSachartre 	label->dkl_vtoc.v_bootinfo[2] = vtoc->v_bootinfo[2];
2564bae9e67eSachartre 	bcopy(vtoc->v_asciilabel, label->dkl_asciilabel, LEN_DKL_ASCII);
2565bae9e67eSachartre 	bcopy(vtoc->v_volume, label->dkl_vtoc.v_volume, LEN_DKL_VVOL);
2566bae9e67eSachartre 
2567bae9e67eSachartre 	/* re-compute checksum */
2568bae9e67eSachartre 	label->dkl_magic = DKL_MAGIC;
2569bae9e67eSachartre 	label->dkl_cksum = vd_lbl2cksum(label);
2570bae9e67eSachartre }
2571bae9e67eSachartre 
2572bae9e67eSachartre /*
2573bae9e67eSachartre  * Copy information from a dk_label structure to a vtoc and dk_geom structures.
2574bae9e67eSachartre  */
2575bae9e67eSachartre static void
2576bae9e67eSachartre vd_label_to_vtocgeom(struct dk_label *label, struct vtoc *vtoc,
2577bae9e67eSachartre     struct dk_geom *geom)
2578bae9e67eSachartre {
2579bae9e67eSachartre 	int i;
2580bae9e67eSachartre 
2581bae9e67eSachartre 	bzero(vtoc, sizeof (struct vtoc));
2582bae9e67eSachartre 	bzero(geom, sizeof (struct dk_geom));
2583bae9e67eSachartre 
2584bae9e67eSachartre 	geom->dkg_ncyl = label->dkl_ncyl;
2585bae9e67eSachartre 	geom->dkg_acyl = label->dkl_acyl;
2586bae9e67eSachartre 	geom->dkg_nhead = label->dkl_nhead;
2587bae9e67eSachartre 	geom->dkg_nsect = label->dkl_nsect;
2588bae9e67eSachartre 	geom->dkg_intrlv = label->dkl_intrlv;
2589bae9e67eSachartre 	geom->dkg_apc = label->dkl_apc;
2590bae9e67eSachartre 	geom->dkg_rpm = label->dkl_rpm;
2591bae9e67eSachartre 	geom->dkg_pcyl = label->dkl_pcyl;
2592bae9e67eSachartre 	geom->dkg_write_reinstruct = label->dkl_write_reinstruct;
2593bae9e67eSachartre 	geom->dkg_read_reinstruct = label->dkl_read_reinstruct;
2594bae9e67eSachartre 
2595bae9e67eSachartre 	vtoc->v_sanity = label->dkl_vtoc.v_sanity;
2596bae9e67eSachartre 	vtoc->v_version = label->dkl_vtoc.v_version;
2597bae9e67eSachartre 	vtoc->v_sectorsz = DEV_BSIZE;
2598bae9e67eSachartre 	vtoc->v_nparts = label->dkl_vtoc.v_nparts;
2599bae9e67eSachartre 
2600bae9e67eSachartre 	for (i = 0; i < vtoc->v_nparts; i++) {
2601bae9e67eSachartre 		vtoc->v_part[i].p_tag = label->dkl_vtoc.v_part[i].p_tag;
2602bae9e67eSachartre 		vtoc->v_part[i].p_flag = label->dkl_vtoc.v_part[i].p_flag;
2603bae9e67eSachartre 		vtoc->v_part[i].p_start = label->dkl_map[i].dkl_cylno *
2604bae9e67eSachartre 		    (label->dkl_nhead * label->dkl_nsect);
2605bae9e67eSachartre 		vtoc->v_part[i].p_size = label->dkl_map[i].dkl_nblk;
2606bae9e67eSachartre 		vtoc->timestamp[i] = label->dkl_vtoc.v_timestamp[i];
2607bae9e67eSachartre 	}
2608bae9e67eSachartre 
2609bae9e67eSachartre 	/*
2610bae9e67eSachartre 	 * The bootinfo array can not be copied with bcopy() because
2611bae9e67eSachartre 	 * elements are of type long in vtoc (so 64-bit) and of type
2612bae9e67eSachartre 	 * int in dk_vtoc (so 32-bit).
2613bae9e67eSachartre 	 */
2614bae9e67eSachartre 	vtoc->v_bootinfo[0] = label->dkl_vtoc.v_bootinfo[0];
2615bae9e67eSachartre 	vtoc->v_bootinfo[1] = label->dkl_vtoc.v_bootinfo[1];
2616bae9e67eSachartre 	vtoc->v_bootinfo[2] = label->dkl_vtoc.v_bootinfo[2];
2617bae9e67eSachartre 	bcopy(label->dkl_asciilabel, vtoc->v_asciilabel, LEN_DKL_ASCII);
2618bae9e67eSachartre 	bcopy(label->dkl_vtoc.v_volume, vtoc->v_volume, LEN_DKL_VVOL);
2619bae9e67eSachartre }
2620bae9e67eSachartre 
2621bae9e67eSachartre /*
2622bae9e67eSachartre  * Check if a geometry is valid for a single-slice disk. A geometry is
2623bae9e67eSachartre  * considered valid if the main attributes of the geometry match with the
2624bae9e67eSachartre  * attributes of the fake geometry we have created.
2625bae9e67eSachartre  */
2626bae9e67eSachartre static boolean_t
2627bae9e67eSachartre vd_slice_geom_isvalid(vd_t *vd, struct dk_geom *geom)
2628bae9e67eSachartre {
2629bae9e67eSachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
2630bae9e67eSachartre 	ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
2631bae9e67eSachartre 
2632bae9e67eSachartre 	if (geom->dkg_ncyl != vd->dk_geom.dkg_ncyl ||
2633bae9e67eSachartre 	    geom->dkg_acyl != vd->dk_geom.dkg_acyl ||
2634bae9e67eSachartre 	    geom->dkg_nsect != vd->dk_geom.dkg_nsect ||
2635bae9e67eSachartre 	    geom->dkg_pcyl != vd->dk_geom.dkg_pcyl)
2636bae9e67eSachartre 		return (B_FALSE);
2637bae9e67eSachartre 
2638bae9e67eSachartre 	return (B_TRUE);
2639bae9e67eSachartre }
2640bae9e67eSachartre 
2641bae9e67eSachartre /*
2642bae9e67eSachartre  * Check if a vtoc is valid for a single-slice disk. A vtoc is considered
2643bae9e67eSachartre  * valid if the main attributes of the vtoc match with the attributes of the
2644bae9e67eSachartre  * fake vtoc we have created.
2645bae9e67eSachartre  */
2646bae9e67eSachartre static boolean_t
2647bae9e67eSachartre vd_slice_vtoc_isvalid(vd_t *vd, struct vtoc *vtoc)
2648bae9e67eSachartre {
2649bae9e67eSachartre 	size_t csize;
2650bae9e67eSachartre 	int i;
2651bae9e67eSachartre 
2652bae9e67eSachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
2653bae9e67eSachartre 	ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
2654bae9e67eSachartre 
2655bae9e67eSachartre 	if (vtoc->v_sanity != vd->vtoc.v_sanity ||
2656bae9e67eSachartre 	    vtoc->v_version != vd->vtoc.v_version ||
2657bae9e67eSachartre 	    vtoc->v_nparts != vd->vtoc.v_nparts ||
2658bae9e67eSachartre 	    strcmp(vtoc->v_volume, vd->vtoc.v_volume) != 0 ||
2659bae9e67eSachartre 	    strcmp(vtoc->v_asciilabel, vd->vtoc.v_asciilabel) != 0)
2660bae9e67eSachartre 		return (B_FALSE);
2661bae9e67eSachartre 
2662bae9e67eSachartre 	/* slice 2 should be unchanged */
2663bae9e67eSachartre 	if (vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_start !=
2664bae9e67eSachartre 	    vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_start ||
2665bae9e67eSachartre 	    vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_size !=
2666bae9e67eSachartre 	    vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_size)
2667bae9e67eSachartre 		return (B_FALSE);
2668bae9e67eSachartre 
2669bae9e67eSachartre 	/*
2670bae9e67eSachartre 	 * Slice 0 should be mostly unchanged and cover most of the disk.
2671bae9e67eSachartre 	 * However we allow some flexibility wrt to the start and the size
2672bae9e67eSachartre 	 * of this slice mainly because we can't exactly know how it will
2673bae9e67eSachartre 	 * be defined by the OS installer.
2674bae9e67eSachartre 	 *
2675bae9e67eSachartre 	 * We allow slice 0 to be defined as starting on any of the first
2676bae9e67eSachartre 	 * 4 cylinders.
2677bae9e67eSachartre 	 */
2678bae9e67eSachartre 	csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
2679bae9e67eSachartre 
2680bae9e67eSachartre 	if (vtoc->v_part[0].p_start > 4 * csize ||
2681bae9e67eSachartre 	    vtoc->v_part[0].p_size > vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_size)
2682bae9e67eSachartre 			return (B_FALSE);
2683bae9e67eSachartre 
2684bae9e67eSachartre 	if (vd->vtoc.v_part[0].p_size >= 4 * csize &&
2685bae9e67eSachartre 	    vtoc->v_part[0].p_size < vd->vtoc.v_part[0].p_size - 4 *csize)
2686bae9e67eSachartre 			return (B_FALSE);
2687bae9e67eSachartre 
2688bae9e67eSachartre 	/* any other slice should have a size of 0 */
2689bae9e67eSachartre 	for (i = 1; i < vtoc->v_nparts; i++) {
2690bae9e67eSachartre 		if (i != VD_ENTIRE_DISK_SLICE &&
2691bae9e67eSachartre 		    vtoc->v_part[i].p_size != 0)
2692bae9e67eSachartre 			return (B_FALSE);
2693bae9e67eSachartre 	}
2694bae9e67eSachartre 
2695bae9e67eSachartre 	return (B_TRUE);
2696bae9e67eSachartre }
2697bae9e67eSachartre 
2698bae9e67eSachartre /*
269987a7269eSachartre  * Handle ioctls to a disk slice.
2700205eeb1aSlm66018  *
2701205eeb1aSlm66018  * Return Values
2702205eeb1aSlm66018  *	0	- Indicates that there are no errors in disk operations
2703205eeb1aSlm66018  *	ENOTSUP	- Unknown disk label type or unsupported DKIO ioctl
2704205eeb1aSlm66018  *	EINVAL	- Not enough room to copy the EFI label
2705205eeb1aSlm66018  *
270687a7269eSachartre  */
27071ae08745Sheppo static int
27080a55fbb7Slm66018 vd_do_slice_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
27091ae08745Sheppo {
27104bac2208Snarayan 	dk_efi_t *dk_ioc;
2711bae9e67eSachartre 	struct vtoc *vtoc;
2712bae9e67eSachartre 	struct dk_geom *geom;
2713bae9e67eSachartre 	size_t len, lba;
2714edcc0754Sachartre 	int rval;
2715edcc0754Sachartre 
2716edcc0754Sachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
2717edcc0754Sachartre 
2718edcc0754Sachartre 	if (cmd == DKIOCFLUSHWRITECACHE) {
2719edcc0754Sachartre 		if (vd->file) {
2720edcc0754Sachartre 			return (VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL));
2721edcc0754Sachartre 		} else {
2722edcc0754Sachartre 			return (ldi_ioctl(vd->ldi_handle[0], cmd,
2723edcc0754Sachartre 			    (intptr_t)ioctl_arg, vd->open_flags | FKIOCTL,
2724edcc0754Sachartre 			    kcred, &rval));
2725edcc0754Sachartre 		}
2726edcc0754Sachartre 	}
27274bac2208Snarayan 
27284bac2208Snarayan 	switch (vd->vdisk_label) {
27294bac2208Snarayan 
2730edcc0754Sachartre 	/* ioctls for a single slice disk with a VTOC label */
27314bac2208Snarayan 	case VD_DISK_LABEL_VTOC:
27324bac2208Snarayan 
27331ae08745Sheppo 		switch (cmd) {
2734bae9e67eSachartre 
27351ae08745Sheppo 		case DKIOCGGEOM:
27360a55fbb7Slm66018 			ASSERT(ioctl_arg != NULL);
27370a55fbb7Slm66018 			bcopy(&vd->dk_geom, ioctl_arg, sizeof (vd->dk_geom));
27381ae08745Sheppo 			return (0);
2739bae9e67eSachartre 
27401ae08745Sheppo 		case DKIOCGVTOC:
27410a55fbb7Slm66018 			ASSERT(ioctl_arg != NULL);
27420a55fbb7Slm66018 			bcopy(&vd->vtoc, ioctl_arg, sizeof (vd->vtoc));
27431ae08745Sheppo 			return (0);
2744bae9e67eSachartre 
2745bae9e67eSachartre 		case DKIOCSGEOM:
2746bae9e67eSachartre 			ASSERT(ioctl_arg != NULL);
2747bae9e67eSachartre 			if (vd_slice_single_slice)
2748bae9e67eSachartre 				return (ENOTSUP);
2749bae9e67eSachartre 
2750bae9e67eSachartre 			/* fake success only if new geometry is valid */
2751bae9e67eSachartre 			geom = (struct dk_geom *)ioctl_arg;
2752bae9e67eSachartre 			if (!vd_slice_geom_isvalid(vd, geom))
2753bae9e67eSachartre 				return (EINVAL);
2754bae9e67eSachartre 
2755bae9e67eSachartre 			return (0);
2756bae9e67eSachartre 
2757bae9e67eSachartre 		case DKIOCSVTOC:
2758bae9e67eSachartre 			ASSERT(ioctl_arg != NULL);
2759bae9e67eSachartre 			if (vd_slice_single_slice)
2760bae9e67eSachartre 				return (ENOTSUP);
2761bae9e67eSachartre 
2762bae9e67eSachartre 			/* fake sucess only if the new vtoc is valid */
2763bae9e67eSachartre 			vtoc = (struct vtoc *)ioctl_arg;
2764bae9e67eSachartre 			if (!vd_slice_vtoc_isvalid(vd, vtoc))
2765bae9e67eSachartre 				return (EINVAL);
2766bae9e67eSachartre 
2767bae9e67eSachartre 			return (0);
2768bae9e67eSachartre 
276987a7269eSachartre 		default:
27703c96341aSnarayan 			return (ENOTSUP);
277187a7269eSachartre 		}
277287a7269eSachartre 
2773edcc0754Sachartre 	/* ioctls for a single slice disk with an EFI label */
277487a7269eSachartre 	case VD_DISK_LABEL_EFI:
277587a7269eSachartre 
2776bae9e67eSachartre 		if (cmd != DKIOCGETEFI && cmd != DKIOCSETEFI)
2777bae9e67eSachartre 			return (ENOTSUP);
2778bae9e67eSachartre 
27793c96341aSnarayan 		ASSERT(ioctl_arg != NULL);
278087a7269eSachartre 		dk_ioc = (dk_efi_t *)ioctl_arg;
2781edcc0754Sachartre 
2782bae9e67eSachartre 		len = dk_ioc->dki_length;
2783bae9e67eSachartre 		lba = dk_ioc->dki_lba;
2784edcc0754Sachartre 
2785bae9e67eSachartre 		if ((lba != VD_EFI_LBA_GPT && lba != VD_EFI_LBA_GPE) ||
2786bae9e67eSachartre 		    (lba == VD_EFI_LBA_GPT && len < sizeof (efi_gpt_t)) ||
2787bae9e67eSachartre 		    (lba == VD_EFI_LBA_GPE && len < sizeof (efi_gpe_t)))
278887a7269eSachartre 			return (EINVAL);
2789edcc0754Sachartre 
2790bae9e67eSachartre 		switch (cmd) {
2791bae9e67eSachartre 		case DKIOCGETEFI:
2792bae9e67eSachartre 			len = vd_slice_flabel_read(vd,
2793bae9e67eSachartre 			    (caddr_t)dk_ioc->dki_data, lba * DEV_BSIZE, len);
2794edcc0754Sachartre 
2795bae9e67eSachartre 			ASSERT(len > 0);
2796edcc0754Sachartre 
279787a7269eSachartre 			return (0);
2798bae9e67eSachartre 
2799bae9e67eSachartre 		case DKIOCSETEFI:
2800bae9e67eSachartre 			if (vd_slice_single_slice)
280187a7269eSachartre 				return (ENOTSUP);
2802bae9e67eSachartre 
2803bae9e67eSachartre 			/* we currently don't support writing EFI */
2804bae9e67eSachartre 			return (EIO);
280587a7269eSachartre 		}
280687a7269eSachartre 
280787a7269eSachartre 	default:
2808205eeb1aSlm66018 		/* Unknown disk label type */
280987a7269eSachartre 		return (ENOTSUP);
281087a7269eSachartre 	}
281187a7269eSachartre }
281287a7269eSachartre 
2813edcc0754Sachartre static int
2814edcc0754Sachartre vds_efi_alloc_and_read(vd_t *vd, efi_gpt_t **gpt, efi_gpe_t **gpe)
2815edcc0754Sachartre {
2816edcc0754Sachartre 	vd_efi_dev_t edev;
2817edcc0754Sachartre 	int status;
2818edcc0754Sachartre 
2819edcc0754Sachartre 	VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl);
2820edcc0754Sachartre 
2821edcc0754Sachartre 	status = vd_efi_alloc_and_read(&edev, gpt, gpe);
2822edcc0754Sachartre 
2823edcc0754Sachartre 	return (status);
2824edcc0754Sachartre }
2825edcc0754Sachartre 
2826edcc0754Sachartre static void
2827edcc0754Sachartre vds_efi_free(vd_t *vd, efi_gpt_t *gpt, efi_gpe_t *gpe)
2828edcc0754Sachartre {
2829edcc0754Sachartre 	vd_efi_dev_t edev;
2830edcc0754Sachartre 
2831edcc0754Sachartre 	VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl);
2832edcc0754Sachartre 
2833edcc0754Sachartre 	vd_efi_free(&edev, gpt, gpe);
2834edcc0754Sachartre }
2835edcc0754Sachartre 
2836edcc0754Sachartre static int
2837edcc0754Sachartre vd_file_validate_efi(vd_t *vd)
2838edcc0754Sachartre {
2839edcc0754Sachartre 	efi_gpt_t *gpt;
2840edcc0754Sachartre 	efi_gpe_t *gpe;
2841edcc0754Sachartre 	int i, nparts, status;
2842edcc0754Sachartre 	struct uuid efi_reserved = EFI_RESERVED;
2843edcc0754Sachartre 
2844edcc0754Sachartre 	if ((status = vds_efi_alloc_and_read(vd, &gpt, &gpe)) != 0)
2845edcc0754Sachartre 		return (status);
2846edcc0754Sachartre 
2847edcc0754Sachartre 	bzero(&vd->vtoc, sizeof (struct vtoc));
2848edcc0754Sachartre 	bzero(&vd->dk_geom, sizeof (struct dk_geom));
2849edcc0754Sachartre 	bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART);
2850edcc0754Sachartre 
2851edcc0754Sachartre 	vd->efi_reserved = -1;
2852edcc0754Sachartre 
2853edcc0754Sachartre 	nparts = gpt->efi_gpt_NumberOfPartitionEntries;
2854edcc0754Sachartre 
2855edcc0754Sachartre 	for (i = 0; i < nparts && i < VD_MAXPART; i++) {
2856edcc0754Sachartre 
2857edcc0754Sachartre 		if (gpe[i].efi_gpe_StartingLBA == 0 ||
2858edcc0754Sachartre 		    gpe[i].efi_gpe_EndingLBA == 0) {
2859edcc0754Sachartre 			continue;
2860edcc0754Sachartre 		}
2861edcc0754Sachartre 
2862edcc0754Sachartre 		vd->slices[i].start = gpe[i].efi_gpe_StartingLBA;
2863edcc0754Sachartre 		vd->slices[i].nblocks = gpe[i].efi_gpe_EndingLBA -
2864edcc0754Sachartre 		    gpe[i].efi_gpe_StartingLBA + 1;
2865edcc0754Sachartre 
2866edcc0754Sachartre 		if (bcmp(&gpe[i].efi_gpe_PartitionTypeGUID, &efi_reserved,
2867edcc0754Sachartre 		    sizeof (struct uuid)) == 0)
2868edcc0754Sachartre 			vd->efi_reserved = i;
2869edcc0754Sachartre 
2870edcc0754Sachartre 	}
2871edcc0754Sachartre 
2872edcc0754Sachartre 	ASSERT(vd->vdisk_size != 0);
2873edcc0754Sachartre 	vd->slices[VD_EFI_WD_SLICE].start = 0;
2874edcc0754Sachartre 	vd->slices[VD_EFI_WD_SLICE].nblocks = vd->vdisk_size;
2875edcc0754Sachartre 
2876edcc0754Sachartre 	vds_efi_free(vd, gpt, gpe);
2877edcc0754Sachartre 
2878edcc0754Sachartre 	return (status);
2879edcc0754Sachartre }
2880edcc0754Sachartre 
288187a7269eSachartre /*
288278fcd0a1Sachartre  * Function:
288378fcd0a1Sachartre  *	vd_file_validate_geometry
2884205eeb1aSlm66018  *
288578fcd0a1Sachartre  * Description:
288678fcd0a1Sachartre  *	Read the label and validate the geometry of a disk image. The driver
288778fcd0a1Sachartre  *	label, vtoc and geometry information are updated according to the
288878fcd0a1Sachartre  *	label read from the disk image.
288978fcd0a1Sachartre  *
289078fcd0a1Sachartre  *	If no valid label is found, the label is set to unknown and the
289178fcd0a1Sachartre  *	function returns EINVAL, but a default vtoc and geometry are provided
2892edcc0754Sachartre  *	to the driver. If an EFI label is found, ENOTSUP is returned.
289378fcd0a1Sachartre  *
289478fcd0a1Sachartre  * Parameters:
289578fcd0a1Sachartre  *	vd	- disk on which the operation is performed.
289678fcd0a1Sachartre  *
289778fcd0a1Sachartre  * Return Code:
289878fcd0a1Sachartre  *	0	- success.
289978fcd0a1Sachartre  *	EIO	- error reading the label from the disk image.
290078fcd0a1Sachartre  *	EINVAL	- unknown disk label.
2901edcc0754Sachartre  *	ENOTSUP	- geometry not applicable (EFI label).
290287a7269eSachartre  */
290387a7269eSachartre static int
290478fcd0a1Sachartre vd_file_validate_geometry(vd_t *vd)
290587a7269eSachartre {
290687a7269eSachartre 	struct dk_label label;
290778fcd0a1Sachartre 	struct dk_geom *geom = &vd->dk_geom;
290878fcd0a1Sachartre 	struct vtoc *vtoc = &vd->vtoc;
290978fcd0a1Sachartre 	int i;
291078fcd0a1Sachartre 	int status = 0;
291187a7269eSachartre 
291287a7269eSachartre 	ASSERT(vd->file);
2913edcc0754Sachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
291487a7269eSachartre 
291587a7269eSachartre 	if (VD_FILE_LABEL_READ(vd, &label) < 0)
291687a7269eSachartre 		return (EIO);
291787a7269eSachartre 
291887a7269eSachartre 	if (label.dkl_magic != DKL_MAGIC ||
291978fcd0a1Sachartre 	    label.dkl_cksum != vd_lbl2cksum(&label) ||
292066cfcfbeSachartre 	    (vd_file_validate_sanity && label.dkl_vtoc.v_sanity != VTOC_SANE) ||
292178fcd0a1Sachartre 	    label.dkl_vtoc.v_nparts != V_NUMPAR) {
2922edcc0754Sachartre 
2923edcc0754Sachartre 		if (vd_file_validate_efi(vd) == 0) {
2924edcc0754Sachartre 			vd->vdisk_label = VD_DISK_LABEL_EFI;
2925edcc0754Sachartre 			return (ENOTSUP);
2926edcc0754Sachartre 		}
2927edcc0754Sachartre 
292878fcd0a1Sachartre 		vd->vdisk_label = VD_DISK_LABEL_UNK;
2929bae9e67eSachartre 		vd_build_default_label(vd->file_size, &label);
293078fcd0a1Sachartre 		status = EINVAL;
293178fcd0a1Sachartre 	} else {
293278fcd0a1Sachartre 		vd->vdisk_label = VD_DISK_LABEL_VTOC;
293378fcd0a1Sachartre 	}
293487a7269eSachartre 
2935bae9e67eSachartre 	/* Update the driver geometry and vtoc */
2936bae9e67eSachartre 	vd_label_to_vtocgeom(&label, vtoc, geom);
293787a7269eSachartre 
2938edcc0754Sachartre 	/* Update logical partitions */
2939edcc0754Sachartre 	bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART);
2940edcc0754Sachartre 	if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
2941edcc0754Sachartre 		for (i = 0; i < vtoc->v_nparts; i++) {
2942edcc0754Sachartre 			vd->slices[i].start = vtoc->v_part[i].p_start;
2943edcc0754Sachartre 			vd->slices[i].nblocks = vtoc->v_part[i].p_size;
2944edcc0754Sachartre 		}
2945edcc0754Sachartre 	}
2946edcc0754Sachartre 
294778fcd0a1Sachartre 	return (status);
294878fcd0a1Sachartre }
294978fcd0a1Sachartre 
295078fcd0a1Sachartre /*
295178fcd0a1Sachartre  * Handle ioctls to a disk image (file-based).
295278fcd0a1Sachartre  *
295378fcd0a1Sachartre  * Return Values
295478fcd0a1Sachartre  *	0	- Indicates that there are no errors
295578fcd0a1Sachartre  *	!= 0	- Disk operation returned an error
295678fcd0a1Sachartre  */
295778fcd0a1Sachartre static int
295878fcd0a1Sachartre vd_do_file_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
295978fcd0a1Sachartre {
296078fcd0a1Sachartre 	struct dk_label label;
296178fcd0a1Sachartre 	struct dk_geom *geom;
296278fcd0a1Sachartre 	struct vtoc *vtoc;
2963edcc0754Sachartre 	dk_efi_t *efi;
2964bae9e67eSachartre 	int rc;
296578fcd0a1Sachartre 
296678fcd0a1Sachartre 	ASSERT(vd->file);
2967edcc0754Sachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
296878fcd0a1Sachartre 
296978fcd0a1Sachartre 	switch (cmd) {
297078fcd0a1Sachartre 
297178fcd0a1Sachartre 	case DKIOCGGEOM:
297278fcd0a1Sachartre 		ASSERT(ioctl_arg != NULL);
297378fcd0a1Sachartre 		geom = (struct dk_geom *)ioctl_arg;
297478fcd0a1Sachartre 
297578fcd0a1Sachartre 		rc = vd_file_validate_geometry(vd);
2976edcc0754Sachartre 		if (rc != 0 && rc != EINVAL)
297778fcd0a1Sachartre 			return (rc);
297878fcd0a1Sachartre 		bcopy(&vd->dk_geom, geom, sizeof (struct dk_geom));
297978fcd0a1Sachartre 		return (0);
298078fcd0a1Sachartre 
298178fcd0a1Sachartre 	case DKIOCGVTOC:
298278fcd0a1Sachartre 		ASSERT(ioctl_arg != NULL);
298378fcd0a1Sachartre 		vtoc = (struct vtoc *)ioctl_arg;
298478fcd0a1Sachartre 
298578fcd0a1Sachartre 		rc = vd_file_validate_geometry(vd);
2986edcc0754Sachartre 		if (rc != 0 && rc != EINVAL)
298778fcd0a1Sachartre 			return (rc);
298878fcd0a1Sachartre 		bcopy(&vd->vtoc, vtoc, sizeof (struct vtoc));
298987a7269eSachartre 		return (0);
299087a7269eSachartre 
299187a7269eSachartre 	case DKIOCSGEOM:
299287a7269eSachartre 		ASSERT(ioctl_arg != NULL);
299387a7269eSachartre 		geom = (struct dk_geom *)ioctl_arg;
299487a7269eSachartre 
299587a7269eSachartre 		if (geom->dkg_nhead == 0 || geom->dkg_nsect == 0)
299687a7269eSachartre 			return (EINVAL);
299787a7269eSachartre 
299887a7269eSachartre 		/*
299987a7269eSachartre 		 * The current device geometry is not updated, just the driver
300087a7269eSachartre 		 * "notion" of it. The device geometry will be effectively
300187a7269eSachartre 		 * updated when a label is written to the device during a next
300287a7269eSachartre 		 * DKIOCSVTOC.
300387a7269eSachartre 		 */
300487a7269eSachartre 		bcopy(ioctl_arg, &vd->dk_geom, sizeof (vd->dk_geom));
300587a7269eSachartre 		return (0);
300687a7269eSachartre 
300787a7269eSachartre 	case DKIOCSVTOC:
300887a7269eSachartre 		ASSERT(ioctl_arg != NULL);
300987a7269eSachartre 		ASSERT(vd->dk_geom.dkg_nhead != 0 &&
301087a7269eSachartre 		    vd->dk_geom.dkg_nsect != 0);
3011690555a1Sachartre 		vtoc = (struct vtoc *)ioctl_arg;
3012690555a1Sachartre 
3013690555a1Sachartre 		if (vtoc->v_sanity != VTOC_SANE ||
3014690555a1Sachartre 		    vtoc->v_sectorsz != DEV_BSIZE ||
3015690555a1Sachartre 		    vtoc->v_nparts != V_NUMPAR)
3016690555a1Sachartre 			return (EINVAL);
3017690555a1Sachartre 
3018bae9e67eSachartre 		vd_vtocgeom_to_label(vtoc, &vd->dk_geom, &label);
3019690555a1Sachartre 
302087a7269eSachartre 		/* write label to the disk image */
302187a7269eSachartre 		if ((rc = vd_file_set_vtoc(vd, &label)) != 0)
302287a7269eSachartre 			return (rc);
3023690555a1Sachartre 
3024edcc0754Sachartre 		break;
3025edcc0754Sachartre 
3026edcc0754Sachartre 	case DKIOCFLUSHWRITECACHE:
3027edcc0754Sachartre 		return (VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL));
3028edcc0754Sachartre 
3029edcc0754Sachartre 	case DKIOCGETEFI:
3030edcc0754Sachartre 		ASSERT(ioctl_arg != NULL);
3031edcc0754Sachartre 		efi = (dk_efi_t *)ioctl_arg;
3032edcc0754Sachartre 
3033edcc0754Sachartre 		if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD,
3034edcc0754Sachartre 		    (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0)
3035edcc0754Sachartre 			return (EIO);
3036edcc0754Sachartre 
3037edcc0754Sachartre 		return (0);
3038edcc0754Sachartre 
3039edcc0754Sachartre 	case DKIOCSETEFI:
3040edcc0754Sachartre 		ASSERT(ioctl_arg != NULL);
3041edcc0754Sachartre 		efi = (dk_efi_t *)ioctl_arg;
3042edcc0754Sachartre 
3043edcc0754Sachartre 		if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE,
3044edcc0754Sachartre 		    (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0)
3045edcc0754Sachartre 			return (EIO);
3046edcc0754Sachartre 
3047edcc0754Sachartre 		break;
3048edcc0754Sachartre 
3049edcc0754Sachartre 
3050edcc0754Sachartre 	default:
3051edcc0754Sachartre 		return (ENOTSUP);
3052edcc0754Sachartre 	}
3053edcc0754Sachartre 
3054edcc0754Sachartre 	ASSERT(cmd == DKIOCSVTOC || cmd == DKIOCSETEFI);
3055edcc0754Sachartre 
3056edcc0754Sachartre 	/* label has changed, revalidate the geometry */
3057edcc0754Sachartre 	(void) vd_file_validate_geometry(vd);
30583c96341aSnarayan 
305987a7269eSachartre 	/*
306087a7269eSachartre 	 * The disk geometry may have changed, so we need to write
306187a7269eSachartre 	 * the devid (if there is one) so that it is stored at the
306287a7269eSachartre 	 * right location.
306387a7269eSachartre 	 */
3064edcc0754Sachartre 	if (vd_file_write_devid(vd, vd->file_devid) != 0) {
306587a7269eSachartre 		PR0("Fail to write devid");
30661ae08745Sheppo 	}
30674bac2208Snarayan 
30684bac2208Snarayan 	return (0);
30694bac2208Snarayan }
3070edcc0754Sachartre 
3071edcc0754Sachartre static int
3072edcc0754Sachartre vd_backend_ioctl(vd_t *vd, int cmd, caddr_t arg)
3073edcc0754Sachartre {
3074edcc0754Sachartre 	int rval = 0, status;
3075edcc0754Sachartre 
3076edcc0754Sachartre 	/*
3077edcc0754Sachartre 	 * Call the appropriate function to execute the ioctl depending
3078edcc0754Sachartre 	 * on the type of vdisk.
3079edcc0754Sachartre 	 */
3080edcc0754Sachartre 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
3081edcc0754Sachartre 
3082edcc0754Sachartre 		/* slice, file or volume exported as a single slice disk */
3083edcc0754Sachartre 		status = vd_do_slice_ioctl(vd, cmd, arg);
3084edcc0754Sachartre 
3085edcc0754Sachartre 	} else if (vd->file) {
3086edcc0754Sachartre 
3087edcc0754Sachartre 		/* file or volume exported as a full disk */
3088edcc0754Sachartre 		status = vd_do_file_ioctl(vd, cmd, arg);
3089edcc0754Sachartre 
3090edcc0754Sachartre 	} else {
3091edcc0754Sachartre 
3092edcc0754Sachartre 		/* disk device exported as a full disk */
3093edcc0754Sachartre 		status = ldi_ioctl(vd->ldi_handle[0], cmd, (intptr_t)arg,
3094edcc0754Sachartre 		    vd->open_flags | FKIOCTL, kcred, &rval);
3095edcc0754Sachartre 	}
3096edcc0754Sachartre 
3097edcc0754Sachartre #ifdef DEBUG
3098edcc0754Sachartre 	if (rval != 0) {
3099edcc0754Sachartre 		PR0("ioctl %x set rval = %d, which is not being returned"
3100edcc0754Sachartre 		    " to caller", cmd, rval);
3101edcc0754Sachartre 	}
3102edcc0754Sachartre #endif /* DEBUG */
3103edcc0754Sachartre 
3104edcc0754Sachartre 	return (status);
31051ae08745Sheppo }
31061ae08745Sheppo 
3107205eeb1aSlm66018 /*
3108205eeb1aSlm66018  * Description:
3109205eeb1aSlm66018  *	This is the function that processes the ioctl requests (farming it
3110205eeb1aSlm66018  *	out to functions that handle slices, files or whole disks)
3111205eeb1aSlm66018  *
3112205eeb1aSlm66018  * Return Values
3113205eeb1aSlm66018  *     0		- ioctl operation completed successfully
3114205eeb1aSlm66018  *     != 0		- The LDC error value encountered
3115205eeb1aSlm66018  *			  (propagated back up the call stack as a task error)
3116205eeb1aSlm66018  *
3117205eeb1aSlm66018  * Side Effect
3118205eeb1aSlm66018  *     sets request->status to the return value of the ioctl function.
3119205eeb1aSlm66018  */
31201ae08745Sheppo static int
31210a55fbb7Slm66018 vd_do_ioctl(vd_t *vd, vd_dring_payload_t *request, void* buf, vd_ioctl_t *ioctl)
31221ae08745Sheppo {
3123edcc0754Sachartre 	int	status = 0;
31241ae08745Sheppo 	size_t	nbytes = request->nbytes;	/* modifiable copy */
31251ae08745Sheppo 
31261ae08745Sheppo 
31271ae08745Sheppo 	ASSERT(request->slice < vd->nslices);
31281ae08745Sheppo 	PR0("Performing %s", ioctl->operation_name);
31291ae08745Sheppo 
31300a55fbb7Slm66018 	/* Get data from client and convert, if necessary */
31310a55fbb7Slm66018 	if (ioctl->copyin != NULL)  {
31321ae08745Sheppo 		ASSERT(nbytes != 0 && buf != NULL);
31331ae08745Sheppo 		PR1("Getting \"arg\" data from client");
31341ae08745Sheppo 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
31351ae08745Sheppo 		    request->cookie, request->ncookies,
31361ae08745Sheppo 		    LDC_COPY_IN)) != 0) {
31373af08d82Slm66018 			PR0("ldc_mem_copy() returned errno %d "
31381ae08745Sheppo 			    "copying from client", status);
31391ae08745Sheppo 			return (status);
31401ae08745Sheppo 		}
31410a55fbb7Slm66018 
31420a55fbb7Slm66018 		/* Convert client's data, if necessary */
31432f5224aeSachartre 		if (ioctl->copyin == VD_IDENTITY_IN) {
31442f5224aeSachartre 			/* use client buffer */
31450a55fbb7Slm66018 			ioctl->arg = buf;
31462f5224aeSachartre 		} else {
31472f5224aeSachartre 			/* convert client vdisk operation data to ioctl data */
31482f5224aeSachartre 			status = (ioctl->copyin)(buf, nbytes,
31492f5224aeSachartre 			    (void *)ioctl->arg);
31502f5224aeSachartre 			if (status != 0) {
31512f5224aeSachartre 				request->status = status;
31522f5224aeSachartre 				return (0);
31532f5224aeSachartre 			}
31542f5224aeSachartre 		}
31552f5224aeSachartre 	}
31562f5224aeSachartre 
31572f5224aeSachartre 	if (ioctl->operation == VD_OP_SCSICMD) {
31582f5224aeSachartre 		struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl->arg;
31592f5224aeSachartre 
31602f5224aeSachartre 		/* check write permission */
31612f5224aeSachartre 		if (!(vd->open_flags & FWRITE) &&
31622f5224aeSachartre 		    !(uscsi->uscsi_flags & USCSI_READ)) {
31632f5224aeSachartre 			PR0("uscsi fails because backend is opened read-only");
31642f5224aeSachartre 			request->status = EROFS;
31652f5224aeSachartre 			return (0);
31662f5224aeSachartre 		}
31671ae08745Sheppo 	}
31681ae08745Sheppo 
31691ae08745Sheppo 	/*
3170edcc0754Sachartre 	 * Send the ioctl to the disk backend.
31711ae08745Sheppo 	 */
3172edcc0754Sachartre 	request->status = vd_backend_ioctl(vd, ioctl->cmd, ioctl->arg);
3173205eeb1aSlm66018 
3174205eeb1aSlm66018 	if (request->status != 0) {
3175205eeb1aSlm66018 		PR0("ioctl(%s) = errno %d", ioctl->cmd_name, request->status);
31762f5224aeSachartre 		if (ioctl->operation == VD_OP_SCSICMD &&
31772f5224aeSachartre 		    ((struct uscsi_cmd *)ioctl->arg)->uscsi_status != 0)
31782f5224aeSachartre 			/*
31792f5224aeSachartre 			 * USCSICMD has reported an error and the uscsi_status
31802f5224aeSachartre 			 * field is not zero. This means that the SCSI command
31812f5224aeSachartre 			 * has completed but it has an error. So we should
31822f5224aeSachartre 			 * mark the VD operation has succesfully completed
31832f5224aeSachartre 			 * and clients can check the SCSI status field for
31842f5224aeSachartre 			 * SCSI errors.
31852f5224aeSachartre 			 */
31862f5224aeSachartre 			request->status = 0;
31872f5224aeSachartre 		else
3188205eeb1aSlm66018 			return (0);
3189205eeb1aSlm66018 	}
31901ae08745Sheppo 
31910a55fbb7Slm66018 	/* Convert data and send to client, if necessary */
31920a55fbb7Slm66018 	if (ioctl->copyout != NULL)  {
31931ae08745Sheppo 		ASSERT(nbytes != 0 && buf != NULL);
31941ae08745Sheppo 		PR1("Sending \"arg\" data to client");
31950a55fbb7Slm66018 
31960a55fbb7Slm66018 		/* Convert ioctl data to vdisk operation data, if necessary */
31972f5224aeSachartre 		if (ioctl->copyout != VD_IDENTITY_OUT)
31980a55fbb7Slm66018 			(ioctl->copyout)((void *)ioctl->arg, buf);
31990a55fbb7Slm66018 
32001ae08745Sheppo 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
32011ae08745Sheppo 		    request->cookie, request->ncookies,
32021ae08745Sheppo 		    LDC_COPY_OUT)) != 0) {
32033af08d82Slm66018 			PR0("ldc_mem_copy() returned errno %d "
32041ae08745Sheppo 			    "copying to client", status);
32051ae08745Sheppo 			return (status);
32061ae08745Sheppo 		}
32071ae08745Sheppo 	}
32081ae08745Sheppo 
32091ae08745Sheppo 	return (status);
32101ae08745Sheppo }
32111ae08745Sheppo 
32121ae08745Sheppo #define	RNDSIZE(expr) P2ROUNDUP(sizeof (expr), sizeof (uint64_t))
3213205eeb1aSlm66018 
3214205eeb1aSlm66018 /*
3215205eeb1aSlm66018  * Description:
3216205eeb1aSlm66018  *	This generic function is called by the task queue to complete
3217205eeb1aSlm66018  *	the processing of the tasks. The specific completion function
3218205eeb1aSlm66018  *	is passed in as a field in the task pointer.
3219205eeb1aSlm66018  *
3220205eeb1aSlm66018  * Parameters:
3221205eeb1aSlm66018  *	arg 	- opaque pointer to structure containing task to be completed
3222205eeb1aSlm66018  *
3223205eeb1aSlm66018  * Return Values
3224205eeb1aSlm66018  *	None
3225205eeb1aSlm66018  */
3226205eeb1aSlm66018 static void
3227205eeb1aSlm66018 vd_complete(void *arg)
3228205eeb1aSlm66018 {
3229205eeb1aSlm66018 	vd_task_t	*task = (vd_task_t *)arg;
3230205eeb1aSlm66018 
3231205eeb1aSlm66018 	ASSERT(task != NULL);
3232205eeb1aSlm66018 	ASSERT(task->status == EINPROGRESS);
3233205eeb1aSlm66018 	ASSERT(task->completef != NULL);
3234205eeb1aSlm66018 
3235205eeb1aSlm66018 	task->status = task->completef(task);
3236205eeb1aSlm66018 	if (task->status)
3237205eeb1aSlm66018 		PR0("%s: Error %d completing task", __func__, task->status);
3238205eeb1aSlm66018 
3239205eeb1aSlm66018 	/* Now notify the vDisk client */
3240205eeb1aSlm66018 	vd_complete_notify(task);
3241205eeb1aSlm66018 }
3242205eeb1aSlm66018 
32431ae08745Sheppo static int
3244d10e4ef2Snarayan vd_ioctl(vd_task_t *task)
32451ae08745Sheppo {
324687a7269eSachartre 	int			i, status;
32471ae08745Sheppo 	void			*buf = NULL;
32480a55fbb7Slm66018 	struct dk_geom		dk_geom = {0};
32490a55fbb7Slm66018 	struct vtoc		vtoc = {0};
32504bac2208Snarayan 	struct dk_efi		dk_efi = {0};
32512f5224aeSachartre 	struct uscsi_cmd	uscsi = {0};
3252d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
3253d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
32540a55fbb7Slm66018 	vd_ioctl_t		ioctl[] = {
32550a55fbb7Slm66018 		/* Command (no-copy) operations */
32560a55fbb7Slm66018 		{VD_OP_FLUSH, STRINGIZE(VD_OP_FLUSH), 0,
32570a55fbb7Slm66018 		    DKIOCFLUSHWRITECACHE, STRINGIZE(DKIOCFLUSHWRITECACHE),
3258047ba61eSachartre 		    NULL, NULL, NULL, B_TRUE},
32590a55fbb7Slm66018 
32600a55fbb7Slm66018 		/* "Get" (copy-out) operations */
32610a55fbb7Slm66018 		{VD_OP_GET_WCE, STRINGIZE(VD_OP_GET_WCE), RNDSIZE(int),
32620a55fbb7Slm66018 		    DKIOCGETWCE, STRINGIZE(DKIOCGETWCE),
32632f5224aeSachartre 		    NULL, VD_IDENTITY_IN, VD_IDENTITY_OUT, B_FALSE},
32640a55fbb7Slm66018 		{VD_OP_GET_DISKGEOM, STRINGIZE(VD_OP_GET_DISKGEOM),
32650a55fbb7Slm66018 		    RNDSIZE(vd_geom_t),
32660a55fbb7Slm66018 		    DKIOCGGEOM, STRINGIZE(DKIOCGGEOM),
3267047ba61eSachartre 		    &dk_geom, NULL, dk_geom2vd_geom, B_FALSE},
32680a55fbb7Slm66018 		{VD_OP_GET_VTOC, STRINGIZE(VD_OP_GET_VTOC), RNDSIZE(vd_vtoc_t),
32690a55fbb7Slm66018 		    DKIOCGVTOC, STRINGIZE(DKIOCGVTOC),
3270047ba61eSachartre 		    &vtoc, NULL, vtoc2vd_vtoc, B_FALSE},
32714bac2208Snarayan 		{VD_OP_GET_EFI, STRINGIZE(VD_OP_GET_EFI), RNDSIZE(vd_efi_t),
32724bac2208Snarayan 		    DKIOCGETEFI, STRINGIZE(DKIOCGETEFI),
3273047ba61eSachartre 		    &dk_efi, vd_get_efi_in, vd_get_efi_out, B_FALSE},
32740a55fbb7Slm66018 
32750a55fbb7Slm66018 		/* "Set" (copy-in) operations */
32760a55fbb7Slm66018 		{VD_OP_SET_WCE, STRINGIZE(VD_OP_SET_WCE), RNDSIZE(int),
32770a55fbb7Slm66018 		    DKIOCSETWCE, STRINGIZE(DKIOCSETWCE),
32782f5224aeSachartre 		    NULL, VD_IDENTITY_IN, VD_IDENTITY_OUT, B_TRUE},
32790a55fbb7Slm66018 		{VD_OP_SET_DISKGEOM, STRINGIZE(VD_OP_SET_DISKGEOM),
32800a55fbb7Slm66018 		    RNDSIZE(vd_geom_t),
32810a55fbb7Slm66018 		    DKIOCSGEOM, STRINGIZE(DKIOCSGEOM),
3282047ba61eSachartre 		    &dk_geom, vd_geom2dk_geom, NULL, B_TRUE},
32830a55fbb7Slm66018 		{VD_OP_SET_VTOC, STRINGIZE(VD_OP_SET_VTOC), RNDSIZE(vd_vtoc_t),
32840a55fbb7Slm66018 		    DKIOCSVTOC, STRINGIZE(DKIOCSVTOC),
3285047ba61eSachartre 		    &vtoc, vd_vtoc2vtoc, NULL, B_TRUE},
32864bac2208Snarayan 		{VD_OP_SET_EFI, STRINGIZE(VD_OP_SET_EFI), RNDSIZE(vd_efi_t),
32874bac2208Snarayan 		    DKIOCSETEFI, STRINGIZE(DKIOCSETEFI),
3288047ba61eSachartre 		    &dk_efi, vd_set_efi_in, vd_set_efi_out, B_TRUE},
32892f5224aeSachartre 
32902f5224aeSachartre 		{VD_OP_SCSICMD, STRINGIZE(VD_OP_SCSICMD), RNDSIZE(vd_scsi_t),
32912f5224aeSachartre 		    USCSICMD, STRINGIZE(USCSICMD),
32922f5224aeSachartre 		    &uscsi, vd_scsicmd_in, vd_scsicmd_out, B_FALSE},
32930a55fbb7Slm66018 	};
32941ae08745Sheppo 	size_t		nioctls = (sizeof (ioctl))/(sizeof (ioctl[0]));
32951ae08745Sheppo 
32961ae08745Sheppo 
3297d10e4ef2Snarayan 	ASSERT(vd != NULL);
3298d10e4ef2Snarayan 	ASSERT(request != NULL);
32991ae08745Sheppo 	ASSERT(request->slice < vd->nslices);
33001ae08745Sheppo 
33011ae08745Sheppo 	/*
33021ae08745Sheppo 	 * Determine ioctl corresponding to caller's "operation" and
33031ae08745Sheppo 	 * validate caller's "nbytes"
33041ae08745Sheppo 	 */
33051ae08745Sheppo 	for (i = 0; i < nioctls; i++) {
33061ae08745Sheppo 		if (request->operation == ioctl[i].operation) {
33070a55fbb7Slm66018 			/* LDC memory operations require 8-byte multiples */
33080a55fbb7Slm66018 			ASSERT(ioctl[i].nbytes % sizeof (uint64_t) == 0);
33090a55fbb7Slm66018 
33104bac2208Snarayan 			if (request->operation == VD_OP_GET_EFI ||
33112f5224aeSachartre 			    request->operation == VD_OP_SET_EFI ||
33122f5224aeSachartre 			    request->operation == VD_OP_SCSICMD) {
33134bac2208Snarayan 				if (request->nbytes >= ioctl[i].nbytes)
33144bac2208Snarayan 					break;
33153af08d82Slm66018 				PR0("%s:  Expected at least nbytes = %lu, "
33164bac2208Snarayan 				    "got %lu", ioctl[i].operation_name,
33174bac2208Snarayan 				    ioctl[i].nbytes, request->nbytes);
33184bac2208Snarayan 				return (EINVAL);
33194bac2208Snarayan 			}
33204bac2208Snarayan 
33210a55fbb7Slm66018 			if (request->nbytes != ioctl[i].nbytes) {
33223af08d82Slm66018 				PR0("%s:  Expected nbytes = %lu, got %lu",
33230a55fbb7Slm66018 				    ioctl[i].operation_name, ioctl[i].nbytes,
33240a55fbb7Slm66018 				    request->nbytes);
33251ae08745Sheppo 				return (EINVAL);
33261ae08745Sheppo 			}
33271ae08745Sheppo 
33281ae08745Sheppo 			break;
33291ae08745Sheppo 		}
33301ae08745Sheppo 	}
33311ae08745Sheppo 	ASSERT(i < nioctls);	/* because "operation" already validated */
33321ae08745Sheppo 
3333047ba61eSachartre 	if (!(vd->open_flags & FWRITE) && ioctl[i].write) {
3334047ba61eSachartre 		PR0("%s fails because backend is opened read-only",
3335047ba61eSachartre 		    ioctl[i].operation_name);
3336047ba61eSachartre 		request->status = EROFS;
3337047ba61eSachartre 		return (0);
3338047ba61eSachartre 	}
3339047ba61eSachartre 
33401ae08745Sheppo 	if (request->nbytes)
33411ae08745Sheppo 		buf = kmem_zalloc(request->nbytes, KM_SLEEP);
33421ae08745Sheppo 	status = vd_do_ioctl(vd, request, buf, &ioctl[i]);
33431ae08745Sheppo 	if (request->nbytes)
33441ae08745Sheppo 		kmem_free(buf, request->nbytes);
334587a7269eSachartre 
33461ae08745Sheppo 	return (status);
33471ae08745Sheppo }
33481ae08745Sheppo 
33494bac2208Snarayan static int
33504bac2208Snarayan vd_get_devid(vd_task_t *task)
33514bac2208Snarayan {
33524bac2208Snarayan 	vd_t *vd = task->vd;
33534bac2208Snarayan 	vd_dring_payload_t *request = task->request;
33544bac2208Snarayan 	vd_devid_t *vd_devid;
33554bac2208Snarayan 	impl_devid_t *devid;
335687a7269eSachartre 	int status, bufid_len, devid_len, len, sz;
33573af08d82Slm66018 	int bufbytes;
33584bac2208Snarayan 
33593af08d82Slm66018 	PR1("Get Device ID, nbytes=%ld", request->nbytes);
33604bac2208Snarayan 
3361bae9e67eSachartre 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
3362bae9e67eSachartre 		/*
3363bae9e67eSachartre 		 * We don't support devid for single-slice disks because we
3364bae9e67eSachartre 		 * have no space to store a fabricated devid and for physical
3365bae9e67eSachartre 		 * disk slices, we can't use the devid of the disk otherwise
3366bae9e67eSachartre 		 * exporting multiple slices from the same disk will produce
3367bae9e67eSachartre 		 * the same devids.
3368bae9e67eSachartre 		 */
3369bae9e67eSachartre 		PR2("No Device ID for slices");
3370bae9e67eSachartre 		request->status = ENOTSUP;
3371bae9e67eSachartre 		return (0);
3372bae9e67eSachartre 	}
3373bae9e67eSachartre 
33743c96341aSnarayan 	if (vd->file) {
337587a7269eSachartre 		if (vd->file_devid == NULL) {
33763af08d82Slm66018 			PR2("No Device ID");
3377205eeb1aSlm66018 			request->status = ENOENT;
3378205eeb1aSlm66018 			return (0);
337987a7269eSachartre 		} else {
338087a7269eSachartre 			sz = ddi_devid_sizeof(vd->file_devid);
338187a7269eSachartre 			devid = kmem_alloc(sz, KM_SLEEP);
338287a7269eSachartre 			bcopy(vd->file_devid, devid, sz);
338387a7269eSachartre 		}
338487a7269eSachartre 	} else {
338587a7269eSachartre 		if (ddi_lyr_get_devid(vd->dev[request->slice],
338687a7269eSachartre 		    (ddi_devid_t *)&devid) != DDI_SUCCESS) {
338787a7269eSachartre 			PR2("No Device ID");
3388205eeb1aSlm66018 			request->status = ENOENT;
3389205eeb1aSlm66018 			return (0);
339087a7269eSachartre 		}
33914bac2208Snarayan 	}
33924bac2208Snarayan 
33934bac2208Snarayan 	bufid_len = request->nbytes - sizeof (vd_devid_t) + 1;
33944bac2208Snarayan 	devid_len = DEVID_GETLEN(devid);
33954bac2208Snarayan 
33963af08d82Slm66018 	/*
33973af08d82Slm66018 	 * Save the buffer size here for use in deallocation.
33983af08d82Slm66018 	 * The actual number of bytes copied is returned in
33993af08d82Slm66018 	 * the 'nbytes' field of the request structure.
34003af08d82Slm66018 	 */
34013af08d82Slm66018 	bufbytes = request->nbytes;
34023af08d82Slm66018 
34033af08d82Slm66018 	vd_devid = kmem_zalloc(bufbytes, KM_SLEEP);
34044bac2208Snarayan 	vd_devid->length = devid_len;
34054bac2208Snarayan 	vd_devid->type = DEVID_GETTYPE(devid);
34064bac2208Snarayan 
34074bac2208Snarayan 	len = (devid_len > bufid_len)? bufid_len : devid_len;
34084bac2208Snarayan 
34094bac2208Snarayan 	bcopy(devid->did_id, vd_devid->id, len);
34104bac2208Snarayan 
341178fcd0a1Sachartre 	request->status = 0;
341278fcd0a1Sachartre 
34134bac2208Snarayan 	/* LDC memory operations require 8-byte multiples */
34144bac2208Snarayan 	ASSERT(request->nbytes % sizeof (uint64_t) == 0);
34154bac2208Snarayan 
34164bac2208Snarayan 	if ((status = ldc_mem_copy(vd->ldc_handle, (caddr_t)vd_devid, 0,
34174bac2208Snarayan 	    &request->nbytes, request->cookie, request->ncookies,
34184bac2208Snarayan 	    LDC_COPY_OUT)) != 0) {
34193af08d82Slm66018 		PR0("ldc_mem_copy() returned errno %d copying to client",
34204bac2208Snarayan 		    status);
34214bac2208Snarayan 	}
34223af08d82Slm66018 	PR1("post mem_copy: nbytes=%ld", request->nbytes);
34234bac2208Snarayan 
34243af08d82Slm66018 	kmem_free(vd_devid, bufbytes);
34254bac2208Snarayan 	ddi_devid_free((ddi_devid_t)devid);
34264bac2208Snarayan 
34274bac2208Snarayan 	return (status);
34284bac2208Snarayan }
34294bac2208Snarayan 
34302f5224aeSachartre static int
34312f5224aeSachartre vd_scsi_reset(vd_t *vd)
34322f5224aeSachartre {
34332f5224aeSachartre 	int rval, status;
34342f5224aeSachartre 	struct uscsi_cmd uscsi = { 0 };
34352f5224aeSachartre 
34362f5224aeSachartre 	uscsi.uscsi_flags = vd_scsi_debug | USCSI_RESET;
34372f5224aeSachartre 	uscsi.uscsi_timeout = vd_scsi_rdwr_timeout;
34382f5224aeSachartre 
34392f5224aeSachartre 	status = ldi_ioctl(vd->ldi_handle[0], USCSICMD, (intptr_t)&uscsi,
34402f5224aeSachartre 	    (vd->open_flags | FKIOCTL), kcred, &rval);
34412f5224aeSachartre 
34422f5224aeSachartre 	return (status);
34432f5224aeSachartre }
34442f5224aeSachartre 
34452f5224aeSachartre static int
34462f5224aeSachartre vd_reset(vd_task_t *task)
34472f5224aeSachartre {
34482f5224aeSachartre 	vd_t *vd = task->vd;
34492f5224aeSachartre 	vd_dring_payload_t *request = task->request;
34502f5224aeSachartre 
34512f5224aeSachartre 	ASSERT(request->operation == VD_OP_RESET);
34522f5224aeSachartre 	ASSERT(vd->scsi);
34532f5224aeSachartre 
34542f5224aeSachartre 	PR0("Performing VD_OP_RESET");
34552f5224aeSachartre 
34562f5224aeSachartre 	if (request->nbytes != 0) {
34572f5224aeSachartre 		PR0("VD_OP_RESET:  Expected nbytes = 0, got %lu",
34582f5224aeSachartre 		    request->nbytes);
34592f5224aeSachartre 		return (EINVAL);
34602f5224aeSachartre 	}
34612f5224aeSachartre 
34622f5224aeSachartre 	request->status = vd_scsi_reset(vd);
34632f5224aeSachartre 
34642f5224aeSachartre 	return (0);
34652f5224aeSachartre }
34662f5224aeSachartre 
34672f5224aeSachartre static int
34682f5224aeSachartre vd_get_capacity(vd_task_t *task)
34692f5224aeSachartre {
34702f5224aeSachartre 	int rv;
34712f5224aeSachartre 	size_t nbytes;
34722f5224aeSachartre 	vd_t *vd = task->vd;
34732f5224aeSachartre 	vd_dring_payload_t *request = task->request;
34742f5224aeSachartre 	vd_capacity_t vd_cap = { 0 };
34752f5224aeSachartre 
34762f5224aeSachartre 	ASSERT(request->operation == VD_OP_GET_CAPACITY);
34772f5224aeSachartre 
34782f5224aeSachartre 	PR0("Performing VD_OP_GET_CAPACITY");
34792f5224aeSachartre 
34802f5224aeSachartre 	nbytes = request->nbytes;
34812f5224aeSachartre 
34822f5224aeSachartre 	if (nbytes != RNDSIZE(vd_capacity_t)) {
34832f5224aeSachartre 		PR0("VD_OP_GET_CAPACITY:  Expected nbytes = %lu, got %lu",
34842f5224aeSachartre 		    RNDSIZE(vd_capacity_t), nbytes);
34852f5224aeSachartre 		return (EINVAL);
34862f5224aeSachartre 	}
34872f5224aeSachartre 
3488*de3a5331SRamesh Chitrothu 	/*
3489*de3a5331SRamesh Chitrothu 	 * Check the backend size in case it has changed. If the check fails
3490*de3a5331SRamesh Chitrothu 	 * then we will return the last known size.
3491*de3a5331SRamesh Chitrothu 	 */
34922f5224aeSachartre 
3493*de3a5331SRamesh Chitrothu 	(void) vd_backend_check_size(vd);
34942f5224aeSachartre 	ASSERT(vd->vdisk_size != 0);
34952f5224aeSachartre 
34962f5224aeSachartre 	request->status = 0;
34972f5224aeSachartre 
34982f5224aeSachartre 	vd_cap.vdisk_block_size = vd->vdisk_block_size;
34992f5224aeSachartre 	vd_cap.vdisk_size = vd->vdisk_size;
35002f5224aeSachartre 
35012f5224aeSachartre 	if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&vd_cap, 0, &nbytes,
35022f5224aeSachartre 	    request->cookie, request->ncookies, LDC_COPY_OUT)) != 0) {
35032f5224aeSachartre 		PR0("ldc_mem_copy() returned errno %d copying to client", rv);
35042f5224aeSachartre 		return (rv);
35052f5224aeSachartre 	}
35062f5224aeSachartre 
35072f5224aeSachartre 	return (0);
35082f5224aeSachartre }
35092f5224aeSachartre 
35102f5224aeSachartre static int
35112f5224aeSachartre vd_get_access(vd_task_t *task)
35122f5224aeSachartre {
35132f5224aeSachartre 	uint64_t access;
35142f5224aeSachartre 	int rv, rval = 0;
35152f5224aeSachartre 	size_t nbytes;
35162f5224aeSachartre 	vd_t *vd = task->vd;
35172f5224aeSachartre 	vd_dring_payload_t *request = task->request;
35182f5224aeSachartre 
35192f5224aeSachartre 	ASSERT(request->operation == VD_OP_GET_ACCESS);
35202f5224aeSachartre 	ASSERT(vd->scsi);
35212f5224aeSachartre 
35222f5224aeSachartre 	PR0("Performing VD_OP_GET_ACCESS");
35232f5224aeSachartre 
35242f5224aeSachartre 	nbytes = request->nbytes;
35252f5224aeSachartre 
35262f5224aeSachartre 	if (nbytes != sizeof (uint64_t)) {
35272f5224aeSachartre 		PR0("VD_OP_GET_ACCESS:  Expected nbytes = %lu, got %lu",
35282f5224aeSachartre 		    sizeof (uint64_t), nbytes);
35292f5224aeSachartre 		return (EINVAL);
35302f5224aeSachartre 	}
35312f5224aeSachartre 
35322f5224aeSachartre 	request->status = ldi_ioctl(vd->ldi_handle[request->slice], MHIOCSTATUS,
35332f5224aeSachartre 	    NULL, (vd->open_flags | FKIOCTL), kcred, &rval);
35342f5224aeSachartre 
35352f5224aeSachartre 	if (request->status != 0)
35362f5224aeSachartre 		return (0);
35372f5224aeSachartre 
35382f5224aeSachartre 	access = (rval == 0)? VD_ACCESS_ALLOWED : VD_ACCESS_DENIED;
35392f5224aeSachartre 
35402f5224aeSachartre 	if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&access, 0, &nbytes,
35412f5224aeSachartre 	    request->cookie, request->ncookies, LDC_COPY_OUT)) != 0) {
35422f5224aeSachartre 		PR0("ldc_mem_copy() returned errno %d copying to client", rv);
35432f5224aeSachartre 		return (rv);
35442f5224aeSachartre 	}
35452f5224aeSachartre 
35462f5224aeSachartre 	return (0);
35472f5224aeSachartre }
35482f5224aeSachartre 
35492f5224aeSachartre static int
35502f5224aeSachartre vd_set_access(vd_task_t *task)
35512f5224aeSachartre {
35522f5224aeSachartre 	uint64_t flags;
35532f5224aeSachartre 	int rv, rval;
35542f5224aeSachartre 	size_t nbytes;
35552f5224aeSachartre 	vd_t *vd = task->vd;
35562f5224aeSachartre 	vd_dring_payload_t *request = task->request;
35572f5224aeSachartre 
35582f5224aeSachartre 	ASSERT(request->operation == VD_OP_SET_ACCESS);
35592f5224aeSachartre 	ASSERT(vd->scsi);
35602f5224aeSachartre 
35612f5224aeSachartre 	nbytes = request->nbytes;
35622f5224aeSachartre 
35632f5224aeSachartre 	if (nbytes != sizeof (uint64_t)) {
35642f5224aeSachartre 		PR0("VD_OP_SET_ACCESS:  Expected nbytes = %lu, got %lu",
35652f5224aeSachartre 		    sizeof (uint64_t), nbytes);
35662f5224aeSachartre 		return (EINVAL);
35672f5224aeSachartre 	}
35682f5224aeSachartre 
35692f5224aeSachartre 	if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&flags, 0, &nbytes,
35702f5224aeSachartre 	    request->cookie, request->ncookies, LDC_COPY_IN)) != 0) {
35712f5224aeSachartre 		PR0("ldc_mem_copy() returned errno %d copying from client", rv);
35722f5224aeSachartre 		return (rv);
35732f5224aeSachartre 	}
35742f5224aeSachartre 
35752f5224aeSachartre 	if (flags == VD_ACCESS_SET_CLEAR) {
35762f5224aeSachartre 		PR0("Performing VD_OP_SET_ACCESS (CLEAR)");
35772f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
35782f5224aeSachartre 		    MHIOCRELEASE, NULL, (vd->open_flags | FKIOCTL), kcred,
35792f5224aeSachartre 		    &rval);
35802f5224aeSachartre 		if (request->status == 0)
35812f5224aeSachartre 			vd->ownership = B_FALSE;
35822f5224aeSachartre 		return (0);
35832f5224aeSachartre 	}
35842f5224aeSachartre 
35852f5224aeSachartre 	/*
35862f5224aeSachartre 	 * As per the VIO spec, the PREEMPT and PRESERVE flags are only valid
35872f5224aeSachartre 	 * when the EXCLUSIVE flag is set.
35882f5224aeSachartre 	 */
35892f5224aeSachartre 	if (!(flags & VD_ACCESS_SET_EXCLUSIVE)) {
35902f5224aeSachartre 		PR0("Invalid VD_OP_SET_ACCESS flags: 0x%lx", flags);
35912f5224aeSachartre 		request->status = EINVAL;
35922f5224aeSachartre 		return (0);
35932f5224aeSachartre 	}
35942f5224aeSachartre 
35952f5224aeSachartre 	switch (flags & (VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE)) {
35962f5224aeSachartre 
35972f5224aeSachartre 	case VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE:
35982f5224aeSachartre 		/*
35992f5224aeSachartre 		 * Flags EXCLUSIVE and PREEMPT and PRESERVE. We have to
36002f5224aeSachartre 		 * acquire exclusive access rights, preserve them and we
36012f5224aeSachartre 		 * can use preemption. So we can use the MHIOCTKNOWN ioctl.
36022f5224aeSachartre 		 */
36032f5224aeSachartre 		PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT|PRESERVE)");
36042f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
36052f5224aeSachartre 		    MHIOCTKOWN, NULL, (vd->open_flags | FKIOCTL), kcred, &rval);
36062f5224aeSachartre 		break;
36072f5224aeSachartre 
36082f5224aeSachartre 	case VD_ACCESS_SET_PRESERVE:
36092f5224aeSachartre 		/*
36102f5224aeSachartre 		 * Flags EXCLUSIVE and PRESERVE. We have to acquire exclusive
36112f5224aeSachartre 		 * access rights and preserve them, but not preempt any other
36122f5224aeSachartre 		 * host. So we need to use the MHIOCTKOWN ioctl to enable the
36132f5224aeSachartre 		 * "preserve" feature but we can not called it directly
36142f5224aeSachartre 		 * because it uses preemption. So before that, we use the
36152f5224aeSachartre 		 * MHIOCQRESERVE ioctl to ensure we can get exclusive rights
36162f5224aeSachartre 		 * without preempting anyone.
36172f5224aeSachartre 		 */
36182f5224aeSachartre 		PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PRESERVE)");
36192f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
36202f5224aeSachartre 		    MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
36212f5224aeSachartre 		    &rval);
36222f5224aeSachartre 		if (request->status != 0)
36232f5224aeSachartre 			break;
36242f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
36252f5224aeSachartre 		    MHIOCTKOWN, NULL, (vd->open_flags | FKIOCTL), kcred, &rval);
36262f5224aeSachartre 		break;
36272f5224aeSachartre 
36282f5224aeSachartre 	case VD_ACCESS_SET_PREEMPT:
36292f5224aeSachartre 		/*
36302f5224aeSachartre 		 * Flags EXCLUSIVE and PREEMPT. We have to acquire exclusive
36312f5224aeSachartre 		 * access rights and we can use preemption. So we try to do
36322f5224aeSachartre 		 * a SCSI reservation, if it fails we reset the disk to clear
36332f5224aeSachartre 		 * any reservation and we try to reserve again.
36342f5224aeSachartre 		 */
36352f5224aeSachartre 		PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT)");
36362f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
36372f5224aeSachartre 		    MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
36382f5224aeSachartre 		    &rval);
36392f5224aeSachartre 		if (request->status == 0)
36402f5224aeSachartre 			break;
36412f5224aeSachartre 
36422f5224aeSachartre 		/* reset the disk */
36432f5224aeSachartre 		(void) vd_scsi_reset(vd);
36442f5224aeSachartre 
36452f5224aeSachartre 		/* try again even if the reset has failed */
36462f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
36472f5224aeSachartre 		    MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
36482f5224aeSachartre 		    &rval);
36492f5224aeSachartre 		break;
36502f5224aeSachartre 
36512f5224aeSachartre 	case 0:
36522f5224aeSachartre 		/* Flag EXCLUSIVE only. Just issue a SCSI reservation */
36532f5224aeSachartre 		PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE)");
36542f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
36552f5224aeSachartre 		    MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
36562f5224aeSachartre 		    &rval);
36572f5224aeSachartre 		break;
36582f5224aeSachartre 	}
36592f5224aeSachartre 
36602f5224aeSachartre 	if (request->status == 0)
36612f5224aeSachartre 		vd->ownership = B_TRUE;
36622f5224aeSachartre 	else
36632f5224aeSachartre 		PR0("VD_OP_SET_ACCESS: error %d", request->status);
36642f5224aeSachartre 
36652f5224aeSachartre 	return (0);
36662f5224aeSachartre }
36672f5224aeSachartre 
36682f5224aeSachartre static void
36692f5224aeSachartre vd_reset_access(vd_t *vd)
36702f5224aeSachartre {
36712f5224aeSachartre 	int status, rval;
36722f5224aeSachartre 
36732f5224aeSachartre 	if (vd->file || !vd->ownership)
36742f5224aeSachartre 		return;
36752f5224aeSachartre 
36762f5224aeSachartre 	PR0("Releasing disk ownership");
36772f5224aeSachartre 	status = ldi_ioctl(vd->ldi_handle[0], MHIOCRELEASE, NULL,
36782f5224aeSachartre 	    (vd->open_flags | FKIOCTL), kcred, &rval);
36792f5224aeSachartre 
36802f5224aeSachartre 	/*
36812f5224aeSachartre 	 * An EACCES failure means that there is a reservation conflict,
36822f5224aeSachartre 	 * so we are not the owner of the disk anymore.
36832f5224aeSachartre 	 */
36842f5224aeSachartre 	if (status == 0 || status == EACCES) {
36852f5224aeSachartre 		vd->ownership = B_FALSE;
36862f5224aeSachartre 		return;
36872f5224aeSachartre 	}
36882f5224aeSachartre 
36892f5224aeSachartre 	PR0("Fail to release ownership, error %d", status);
36902f5224aeSachartre 
36912f5224aeSachartre 	/*
36922f5224aeSachartre 	 * We have failed to release the ownership, try to reset the disk
36932f5224aeSachartre 	 * to release reservations.
36942f5224aeSachartre 	 */
36952f5224aeSachartre 	PR0("Resetting disk");
36962f5224aeSachartre 	status = vd_scsi_reset(vd);
36972f5224aeSachartre 
36982f5224aeSachartre 	if (status != 0)
36992f5224aeSachartre 		PR0("Fail to reset disk, error %d", status);
37002f5224aeSachartre 
37012f5224aeSachartre 	/* whatever the result of the reset is, we try the release again */
37022f5224aeSachartre 	status = ldi_ioctl(vd->ldi_handle[0], MHIOCRELEASE, NULL,
37032f5224aeSachartre 	    (vd->open_flags | FKIOCTL), kcred, &rval);
37042f5224aeSachartre 
37052f5224aeSachartre 	if (status == 0 || status == EACCES) {
37062f5224aeSachartre 		vd->ownership = B_FALSE;
37072f5224aeSachartre 		return;
37082f5224aeSachartre 	}
37092f5224aeSachartre 
37102f5224aeSachartre 	PR0("Fail to release ownership, error %d", status);
37112f5224aeSachartre 
37122f5224aeSachartre 	/*
37132f5224aeSachartre 	 * At this point we have done our best to try to reset the
37142f5224aeSachartre 	 * access rights to the disk and we don't know if we still
37152f5224aeSachartre 	 * own a reservation and if any mechanism to preserve the
37162f5224aeSachartre 	 * ownership is still in place. The ultimate solution would
37172f5224aeSachartre 	 * be to reset the system but this is usually not what we
37182f5224aeSachartre 	 * want to happen.
37192f5224aeSachartre 	 */
37202f5224aeSachartre 
37212f5224aeSachartre 	if (vd_reset_access_failure == A_REBOOT) {
37222f5224aeSachartre 		cmn_err(CE_WARN, VD_RESET_ACCESS_FAILURE_MSG
37232f5224aeSachartre 		    ", rebooting the system", vd->device_path);
37242f5224aeSachartre 		(void) uadmin(A_SHUTDOWN, AD_BOOT, NULL);
37252f5224aeSachartre 	} else if (vd_reset_access_failure == A_DUMP) {
37262f5224aeSachartre 		panic(VD_RESET_ACCESS_FAILURE_MSG, vd->device_path);
37272f5224aeSachartre 	}
37282f5224aeSachartre 
37292f5224aeSachartre 	cmn_err(CE_WARN, VD_RESET_ACCESS_FAILURE_MSG, vd->device_path);
37302f5224aeSachartre }
37312f5224aeSachartre 
37321ae08745Sheppo /*
37331ae08745Sheppo  * Define the supported operations once the functions for performing them have
37341ae08745Sheppo  * been defined
37351ae08745Sheppo  */
37361ae08745Sheppo static const vds_operation_t	vds_operation[] = {
37373af08d82Slm66018 #define	X(_s)	#_s, _s
37383af08d82Slm66018 	{X(VD_OP_BREAD),	vd_start_bio,	vd_complete_bio},
37393af08d82Slm66018 	{X(VD_OP_BWRITE),	vd_start_bio,	vd_complete_bio},
37403af08d82Slm66018 	{X(VD_OP_FLUSH),	vd_ioctl,	NULL},
37413af08d82Slm66018 	{X(VD_OP_GET_WCE),	vd_ioctl,	NULL},
37423af08d82Slm66018 	{X(VD_OP_SET_WCE),	vd_ioctl,	NULL},
37433af08d82Slm66018 	{X(VD_OP_GET_VTOC),	vd_ioctl,	NULL},
37443af08d82Slm66018 	{X(VD_OP_SET_VTOC),	vd_ioctl,	NULL},
37453af08d82Slm66018 	{X(VD_OP_GET_DISKGEOM),	vd_ioctl,	NULL},
37463af08d82Slm66018 	{X(VD_OP_SET_DISKGEOM),	vd_ioctl,	NULL},
37473af08d82Slm66018 	{X(VD_OP_GET_EFI),	vd_ioctl,	NULL},
37483af08d82Slm66018 	{X(VD_OP_SET_EFI),	vd_ioctl,	NULL},
37493af08d82Slm66018 	{X(VD_OP_GET_DEVID),	vd_get_devid,	NULL},
37502f5224aeSachartre 	{X(VD_OP_SCSICMD),	vd_ioctl,	NULL},
37512f5224aeSachartre 	{X(VD_OP_RESET),	vd_reset,	NULL},
37522f5224aeSachartre 	{X(VD_OP_GET_CAPACITY),	vd_get_capacity, NULL},
37532f5224aeSachartre 	{X(VD_OP_SET_ACCESS),	vd_set_access,	NULL},
37542f5224aeSachartre 	{X(VD_OP_GET_ACCESS),	vd_get_access,	NULL},
37553af08d82Slm66018 #undef	X
37561ae08745Sheppo };
37571ae08745Sheppo 
37581ae08745Sheppo static const size_t	vds_noperations =
37591ae08745Sheppo 	(sizeof (vds_operation))/(sizeof (vds_operation[0]));
37601ae08745Sheppo 
37611ae08745Sheppo /*
3762d10e4ef2Snarayan  * Process a task specifying a client I/O request
3763205eeb1aSlm66018  *
3764205eeb1aSlm66018  * Parameters:
3765205eeb1aSlm66018  *	task 		- structure containing the request sent from client
3766205eeb1aSlm66018  *
3767205eeb1aSlm66018  * Return Value
3768205eeb1aSlm66018  *	0	- success
3769205eeb1aSlm66018  *	ENOTSUP	- Unknown/Unsupported VD_OP_XXX operation
3770205eeb1aSlm66018  *	EINVAL	- Invalid disk slice
3771205eeb1aSlm66018  *	!= 0	- some other non-zero return value from start function
37721ae08745Sheppo  */
37731ae08745Sheppo static int
3774205eeb1aSlm66018 vd_do_process_task(vd_task_t *task)
37751ae08745Sheppo {
3776205eeb1aSlm66018 	int			i;
3777d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
3778d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
37791ae08745Sheppo 
3780d10e4ef2Snarayan 	ASSERT(vd != NULL);
3781d10e4ef2Snarayan 	ASSERT(request != NULL);
37821ae08745Sheppo 
3783d10e4ef2Snarayan 	/* Find the requested operation */
3784205eeb1aSlm66018 	for (i = 0; i < vds_noperations; i++) {
3785205eeb1aSlm66018 		if (request->operation == vds_operation[i].operation) {
3786205eeb1aSlm66018 			/* all operations should have a start func */
3787205eeb1aSlm66018 			ASSERT(vds_operation[i].start != NULL);
3788205eeb1aSlm66018 
3789205eeb1aSlm66018 			task->completef = vds_operation[i].complete;
3790d10e4ef2Snarayan 			break;
3791205eeb1aSlm66018 		}
3792205eeb1aSlm66018 	}
379317cadca8Slm66018 
379417cadca8Slm66018 	/*
379517cadca8Slm66018 	 * We need to check that the requested operation is permitted
379617cadca8Slm66018 	 * for the particular client that sent it or that the loop above
379717cadca8Slm66018 	 * did not complete without finding the operation type (indicating
379817cadca8Slm66018 	 * that the requested operation is unknown/unimplemented)
379917cadca8Slm66018 	 */
380017cadca8Slm66018 	if ((VD_OP_SUPPORTED(vd->operations, request->operation) == B_FALSE) ||
380117cadca8Slm66018 	    (i == vds_noperations)) {
38023af08d82Slm66018 		PR0("Unsupported operation %u", request->operation);
380317cadca8Slm66018 		request->status = ENOTSUP;
380417cadca8Slm66018 		return (0);
38051ae08745Sheppo 	}
38061ae08745Sheppo 
38077636cb21Slm66018 	/* Range-check slice */
380887a7269eSachartre 	if (request->slice >= vd->nslices &&
3809bae9e67eSachartre 	    ((vd->vdisk_type != VD_DISK_TYPE_DISK && vd_slice_single_slice) ||
381087a7269eSachartre 	    request->slice != VD_SLICE_NONE)) {
38113af08d82Slm66018 		PR0("Invalid \"slice\" %u (max %u) for virtual disk",
38127636cb21Slm66018 		    request->slice, (vd->nslices - 1));
3813bae9e67eSachartre 		request->status = EINVAL;
3814bae9e67eSachartre 		return (0);
38157636cb21Slm66018 	}
38167636cb21Slm66018 
3817205eeb1aSlm66018 	/*
3818205eeb1aSlm66018 	 * Call the function pointer that starts the operation.
3819205eeb1aSlm66018 	 */
3820205eeb1aSlm66018 	return (vds_operation[i].start(task));
38211ae08745Sheppo }
38221ae08745Sheppo 
3823205eeb1aSlm66018 /*
3824205eeb1aSlm66018  * Description:
3825205eeb1aSlm66018  *	This function is called by both the in-band and descriptor ring
3826205eeb1aSlm66018  *	message processing functions paths to actually execute the task
3827205eeb1aSlm66018  *	requested by the vDisk client. It in turn calls its worker
3828205eeb1aSlm66018  *	function, vd_do_process_task(), to carry our the request.
3829205eeb1aSlm66018  *
3830205eeb1aSlm66018  *	Any transport errors (e.g. LDC errors, vDisk protocol errors) are
3831205eeb1aSlm66018  *	saved in the 'status' field of the task and are propagated back
3832205eeb1aSlm66018  *	up the call stack to trigger a NACK
3833205eeb1aSlm66018  *
3834205eeb1aSlm66018  *	Any request errors (e.g. ENOTTY from an ioctl) are saved in
3835205eeb1aSlm66018  *	the 'status' field of the request and result in an ACK being sent
3836205eeb1aSlm66018  *	by the completion handler.
3837205eeb1aSlm66018  *
3838205eeb1aSlm66018  * Parameters:
3839205eeb1aSlm66018  *	task 		- structure containing the request sent from client
3840205eeb1aSlm66018  *
3841205eeb1aSlm66018  * Return Value
3842205eeb1aSlm66018  *	0		- successful synchronous request.
3843205eeb1aSlm66018  *	!= 0		- transport error (e.g. LDC errors, vDisk protocol)
3844205eeb1aSlm66018  *	EINPROGRESS	- task will be finished in a completion handler
3845205eeb1aSlm66018  */
3846205eeb1aSlm66018 static int
3847205eeb1aSlm66018 vd_process_task(vd_task_t *task)
3848205eeb1aSlm66018 {
3849205eeb1aSlm66018 	vd_t	*vd = task->vd;
3850205eeb1aSlm66018 	int	status;
38511ae08745Sheppo 
3852205eeb1aSlm66018 	DTRACE_PROBE1(task__start, vd_task_t *, task);
38533af08d82Slm66018 
3854205eeb1aSlm66018 	task->status =  vd_do_process_task(task);
3855205eeb1aSlm66018 
3856205eeb1aSlm66018 	/*
3857205eeb1aSlm66018 	 * If the task processing function returned EINPROGRESS indicating
3858205eeb1aSlm66018 	 * that the task needs completing then schedule a taskq entry to
3859205eeb1aSlm66018 	 * finish it now.
3860205eeb1aSlm66018 	 *
3861205eeb1aSlm66018 	 * Otherwise the task processing function returned either zero
3862205eeb1aSlm66018 	 * indicating that the task was finished in the start function (and we
3863205eeb1aSlm66018 	 * don't need to wait in a completion function) or the start function
3864205eeb1aSlm66018 	 * returned an error - in both cases all that needs to happen is the
3865205eeb1aSlm66018 	 * notification to the vDisk client higher up the call stack.
3866205eeb1aSlm66018 	 * If the task was using a Descriptor Ring, we need to mark it as done
3867205eeb1aSlm66018 	 * at this stage.
3868205eeb1aSlm66018 	 */
3869205eeb1aSlm66018 	if (task->status == EINPROGRESS) {
3870d10e4ef2Snarayan 		/* Queue a task to complete the operation */
3871205eeb1aSlm66018 		(void) ddi_taskq_dispatch(vd->completionq, vd_complete,
3872d10e4ef2Snarayan 		    task, DDI_SLEEP);
3873d10e4ef2Snarayan 
3874f0ca1d9aSsb155480 	} else if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE_V1_0)) {
3875205eeb1aSlm66018 		/* Update the dring element if it's a dring client */
3876205eeb1aSlm66018 		status = vd_mark_elem_done(vd, task->index,
3877205eeb1aSlm66018 		    task->request->status, task->request->nbytes);
3878205eeb1aSlm66018 		if (status == ECONNRESET)
3879205eeb1aSlm66018 			vd_mark_in_reset(vd);
3880bbfa0259Sha137994 		else if (status == EACCES)
3881bbfa0259Sha137994 			vd_need_reset(vd, B_TRUE);
3882205eeb1aSlm66018 	}
3883205eeb1aSlm66018 
3884205eeb1aSlm66018 	return (task->status);
38851ae08745Sheppo }
38861ae08745Sheppo 
38871ae08745Sheppo /*
38880a55fbb7Slm66018  * Return true if the "type", "subtype", and "env" fields of the "tag" first
38890a55fbb7Slm66018  * argument match the corresponding remaining arguments; otherwise, return false
38901ae08745Sheppo  */
38910a55fbb7Slm66018 boolean_t
38921ae08745Sheppo vd_msgtype(vio_msg_tag_t *tag, int type, int subtype, int env)
38931ae08745Sheppo {
38941ae08745Sheppo 	return ((tag->vio_msgtype == type) &&
38951ae08745Sheppo 	    (tag->vio_subtype == subtype) &&
38960a55fbb7Slm66018 	    (tag->vio_subtype_env == env)) ? B_TRUE : B_FALSE;
38971ae08745Sheppo }
38981ae08745Sheppo 
38990a55fbb7Slm66018 /*
39000a55fbb7Slm66018  * Check whether the major/minor version specified in "ver_msg" is supported
39010a55fbb7Slm66018  * by this server.
39020a55fbb7Slm66018  */
39030a55fbb7Slm66018 static boolean_t
39040a55fbb7Slm66018 vds_supported_version(vio_ver_msg_t *ver_msg)
39050a55fbb7Slm66018 {
39060a55fbb7Slm66018 	for (int i = 0; i < vds_num_versions; i++) {
39070a55fbb7Slm66018 		ASSERT(vds_version[i].major > 0);
39080a55fbb7Slm66018 		ASSERT((i == 0) ||
39090a55fbb7Slm66018 		    (vds_version[i].major < vds_version[i-1].major));
39100a55fbb7Slm66018 
39110a55fbb7Slm66018 		/*
39120a55fbb7Slm66018 		 * If the major versions match, adjust the minor version, if
39130a55fbb7Slm66018 		 * necessary, down to the highest value supported by this
39140a55fbb7Slm66018 		 * server and return true so this message will get "ack"ed;
39150a55fbb7Slm66018 		 * the client should also support all minor versions lower
39160a55fbb7Slm66018 		 * than the value it sent
39170a55fbb7Slm66018 		 */
39180a55fbb7Slm66018 		if (ver_msg->ver_major == vds_version[i].major) {
39190a55fbb7Slm66018 			if (ver_msg->ver_minor > vds_version[i].minor) {
39200a55fbb7Slm66018 				PR0("Adjusting minor version from %u to %u",
39210a55fbb7Slm66018 				    ver_msg->ver_minor, vds_version[i].minor);
39220a55fbb7Slm66018 				ver_msg->ver_minor = vds_version[i].minor;
39230a55fbb7Slm66018 			}
39240a55fbb7Slm66018 			return (B_TRUE);
39250a55fbb7Slm66018 		}
39260a55fbb7Slm66018 
39270a55fbb7Slm66018 		/*
39280a55fbb7Slm66018 		 * If the message contains a higher major version number, set
39290a55fbb7Slm66018 		 * the message's major/minor versions to the current values
39300a55fbb7Slm66018 		 * and return false, so this message will get "nack"ed with
39310a55fbb7Slm66018 		 * these values, and the client will potentially try again
39320a55fbb7Slm66018 		 * with the same or a lower version
39330a55fbb7Slm66018 		 */
39340a55fbb7Slm66018 		if (ver_msg->ver_major > vds_version[i].major) {
39350a55fbb7Slm66018 			ver_msg->ver_major = vds_version[i].major;
39360a55fbb7Slm66018 			ver_msg->ver_minor = vds_version[i].minor;
39370a55fbb7Slm66018 			return (B_FALSE);
39380a55fbb7Slm66018 		}
39390a55fbb7Slm66018 
39400a55fbb7Slm66018 		/*
39410a55fbb7Slm66018 		 * Otherwise, the message's major version is less than the
39420a55fbb7Slm66018 		 * current major version, so continue the loop to the next
39430a55fbb7Slm66018 		 * (lower) supported version
39440a55fbb7Slm66018 		 */
39450a55fbb7Slm66018 	}
39460a55fbb7Slm66018 
39470a55fbb7Slm66018 	/*
39480a55fbb7Slm66018 	 * No common version was found; "ground" the version pair in the
39490a55fbb7Slm66018 	 * message to terminate negotiation
39500a55fbb7Slm66018 	 */
39510a55fbb7Slm66018 	ver_msg->ver_major = 0;
39520a55fbb7Slm66018 	ver_msg->ver_minor = 0;
39530a55fbb7Slm66018 	return (B_FALSE);
39540a55fbb7Slm66018 }
39550a55fbb7Slm66018 
39560a55fbb7Slm66018 /*
39570a55fbb7Slm66018  * Process a version message from a client.  vds expects to receive version
39580a55fbb7Slm66018  * messages from clients seeking service, but never issues version messages
39590a55fbb7Slm66018  * itself; therefore, vds can ACK or NACK client version messages, but does
39600a55fbb7Slm66018  * not expect to receive version-message ACKs or NACKs (and will treat such
39610a55fbb7Slm66018  * messages as invalid).
39620a55fbb7Slm66018  */
39631ae08745Sheppo static int
39640a55fbb7Slm66018 vd_process_ver_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
39651ae08745Sheppo {
39661ae08745Sheppo 	vio_ver_msg_t	*ver_msg = (vio_ver_msg_t *)msg;
39671ae08745Sheppo 
39681ae08745Sheppo 
39691ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
39701ae08745Sheppo 
39711ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
39721ae08745Sheppo 	    VIO_VER_INFO)) {
39731ae08745Sheppo 		return (ENOMSG);	/* not a version message */
39741ae08745Sheppo 	}
39751ae08745Sheppo 
39761ae08745Sheppo 	if (msglen != sizeof (*ver_msg)) {
39773af08d82Slm66018 		PR0("Expected %lu-byte version message; "
39781ae08745Sheppo 		    "received %lu bytes", sizeof (*ver_msg), msglen);
39791ae08745Sheppo 		return (EBADMSG);
39801ae08745Sheppo 	}
39811ae08745Sheppo 
39821ae08745Sheppo 	if (ver_msg->dev_class != VDEV_DISK) {
39833af08d82Slm66018 		PR0("Expected device class %u (disk); received %u",
39841ae08745Sheppo 		    VDEV_DISK, ver_msg->dev_class);
39851ae08745Sheppo 		return (EBADMSG);
39861ae08745Sheppo 	}
39871ae08745Sheppo 
39880a55fbb7Slm66018 	/*
39890a55fbb7Slm66018 	 * We're talking to the expected kind of client; set our device class
39900a55fbb7Slm66018 	 * for "ack/nack" back to the client
39910a55fbb7Slm66018 	 */
39921ae08745Sheppo 	ver_msg->dev_class = VDEV_DISK_SERVER;
39930a55fbb7Slm66018 
39940a55fbb7Slm66018 	/*
39950a55fbb7Slm66018 	 * Check whether the (valid) version message specifies a version
39960a55fbb7Slm66018 	 * supported by this server.  If the version is not supported, return
39970a55fbb7Slm66018 	 * EBADMSG so the message will get "nack"ed; vds_supported_version()
39980a55fbb7Slm66018 	 * will have updated the message with a supported version for the
39990a55fbb7Slm66018 	 * client to consider
40000a55fbb7Slm66018 	 */
40010a55fbb7Slm66018 	if (!vds_supported_version(ver_msg))
40020a55fbb7Slm66018 		return (EBADMSG);
40030a55fbb7Slm66018 
40040a55fbb7Slm66018 
40050a55fbb7Slm66018 	/*
40060a55fbb7Slm66018 	 * A version has been agreed upon; use the client's SID for
40070a55fbb7Slm66018 	 * communication on this channel now
40080a55fbb7Slm66018 	 */
40090a55fbb7Slm66018 	ASSERT(!(vd->initialized & VD_SID));
40100a55fbb7Slm66018 	vd->sid = ver_msg->tag.vio_sid;
40110a55fbb7Slm66018 	vd->initialized |= VD_SID;
40120a55fbb7Slm66018 
40130a55fbb7Slm66018 	/*
401417cadca8Slm66018 	 * Store the negotiated major and minor version values in the "vd" data
401517cadca8Slm66018 	 * structure so that we can check if certain operations are supported
401617cadca8Slm66018 	 * by the client.
40170a55fbb7Slm66018 	 */
401817cadca8Slm66018 	vd->version.major = ver_msg->ver_major;
401917cadca8Slm66018 	vd->version.minor = ver_msg->ver_minor;
40200a55fbb7Slm66018 
40210a55fbb7Slm66018 	PR0("Using major version %u, minor version %u",
40220a55fbb7Slm66018 	    ver_msg->ver_major, ver_msg->ver_minor);
40231ae08745Sheppo 	return (0);
40241ae08745Sheppo }
40251ae08745Sheppo 
402617cadca8Slm66018 static void
402717cadca8Slm66018 vd_set_exported_operations(vd_t *vd)
402817cadca8Slm66018 {
402917cadca8Slm66018 	vd->operations = 0;	/* clear field */
403017cadca8Slm66018 
403117cadca8Slm66018 	/*
403217cadca8Slm66018 	 * We need to check from the highest version supported to the
403317cadca8Slm66018 	 * lowest because versions with a higher minor number implicitly
403417cadca8Slm66018 	 * support versions with a lower minor number.
403517cadca8Slm66018 	 */
403617cadca8Slm66018 	if (vio_ver_is_supported(vd->version, 1, 1)) {
403717cadca8Slm66018 		ASSERT(vd->open_flags & FREAD);
4038*de3a5331SRamesh Chitrothu 		vd->operations |= VD_OP_MASK_READ | (1 << VD_OP_GET_CAPACITY);
403917cadca8Slm66018 
404017cadca8Slm66018 		if (vd->open_flags & FWRITE)
404117cadca8Slm66018 			vd->operations |= VD_OP_MASK_WRITE;
404217cadca8Slm66018 
40432f5224aeSachartre 		if (vd->scsi)
40442f5224aeSachartre 			vd->operations |= VD_OP_MASK_SCSI;
40452f5224aeSachartre 
404617cadca8Slm66018 		if (vd->file && vd_file_is_iso_image(vd)) {
404717cadca8Slm66018 			/*
404817cadca8Slm66018 			 * can't write to ISO images, make sure that write
404917cadca8Slm66018 			 * support is not set in case administrator did not
405017cadca8Slm66018 			 * use "options=ro" when doing an ldm add-vdsdev
405117cadca8Slm66018 			 */
405217cadca8Slm66018 			vd->operations &= ~VD_OP_MASK_WRITE;
405317cadca8Slm66018 		}
405417cadca8Slm66018 	} else if (vio_ver_is_supported(vd->version, 1, 0)) {
405517cadca8Slm66018 		vd->operations = VD_OP_MASK_READ | VD_OP_MASK_WRITE;
405617cadca8Slm66018 	}
405717cadca8Slm66018 
405817cadca8Slm66018 	/* we should have already agreed on a version */
405917cadca8Slm66018 	ASSERT(vd->operations != 0);
406017cadca8Slm66018 }
406117cadca8Slm66018 
40621ae08745Sheppo static int
40631ae08745Sheppo vd_process_attr_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
40641ae08745Sheppo {
40651ae08745Sheppo 	vd_attr_msg_t	*attr_msg = (vd_attr_msg_t *)msg;
40663c96341aSnarayan 	int		status, retry = 0;
40671ae08745Sheppo 
40681ae08745Sheppo 
40691ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
40701ae08745Sheppo 
40711ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
40721ae08745Sheppo 	    VIO_ATTR_INFO)) {
4073d10e4ef2Snarayan 		PR0("Message is not an attribute message");
4074d10e4ef2Snarayan 		return (ENOMSG);
40751ae08745Sheppo 	}
40761ae08745Sheppo 
40771ae08745Sheppo 	if (msglen != sizeof (*attr_msg)) {
40783af08d82Slm66018 		PR0("Expected %lu-byte attribute message; "
40791ae08745Sheppo 		    "received %lu bytes", sizeof (*attr_msg), msglen);
40801ae08745Sheppo 		return (EBADMSG);
40811ae08745Sheppo 	}
40821ae08745Sheppo 
40831ae08745Sheppo 	if (attr_msg->max_xfer_sz == 0) {
40843af08d82Slm66018 		PR0("Received maximum transfer size of 0 from client");
40851ae08745Sheppo 		return (EBADMSG);
40861ae08745Sheppo 	}
40871ae08745Sheppo 
40881ae08745Sheppo 	if ((attr_msg->xfer_mode != VIO_DESC_MODE) &&
4089f0ca1d9aSsb155480 	    (attr_msg->xfer_mode != VIO_DRING_MODE_V1_0)) {
40903af08d82Slm66018 		PR0("Client requested unsupported transfer mode");
40911ae08745Sheppo 		return (EBADMSG);
40921ae08745Sheppo 	}
40931ae08745Sheppo 
40943c96341aSnarayan 	/*
40953c96341aSnarayan 	 * check if the underlying disk is ready, if not try accessing
40963c96341aSnarayan 	 * the device again. Open the vdisk device and extract info
40973c96341aSnarayan 	 * about it, as this is needed to respond to the attr info msg
40983c96341aSnarayan 	 */
40993c96341aSnarayan 	if ((vd->initialized & VD_DISK_READY) == 0) {
41003c96341aSnarayan 		PR0("Retry setting up disk (%s)", vd->device_path);
41013c96341aSnarayan 		do {
41023c96341aSnarayan 			status = vd_setup_vd(vd);
41033c96341aSnarayan 			if (status != EAGAIN || ++retry > vds_dev_retries)
41043c96341aSnarayan 				break;
41053c96341aSnarayan 
41063c96341aSnarayan 			/* incremental delay */
41073c96341aSnarayan 			delay(drv_usectohz(vds_dev_delay));
41083c96341aSnarayan 
41093c96341aSnarayan 			/* if vdisk is no longer enabled - return error */
41103c96341aSnarayan 			if (!vd_enabled(vd))
41113c96341aSnarayan 				return (ENXIO);
41123c96341aSnarayan 
41133c96341aSnarayan 		} while (status == EAGAIN);
41143c96341aSnarayan 
41153c96341aSnarayan 		if (status)
41163c96341aSnarayan 			return (ENXIO);
41173c96341aSnarayan 
41183c96341aSnarayan 		vd->initialized |= VD_DISK_READY;
41193c96341aSnarayan 		ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
41208fce2fd6Sachartre 		PR0("vdisk_type = %s, volume = %s, file = %s, nslices = %u",
41213c96341aSnarayan 		    ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
41228fce2fd6Sachartre 		    (vd->volume ? "yes" : "no"),
41233c96341aSnarayan 		    (vd->file ? "yes" : "no"),
41243c96341aSnarayan 		    vd->nslices);
41253c96341aSnarayan 	}
41263c96341aSnarayan 
41271ae08745Sheppo 	/* Success:  valid message and transfer mode */
41281ae08745Sheppo 	vd->xfer_mode = attr_msg->xfer_mode;
41293af08d82Slm66018 
41301ae08745Sheppo 	if (vd->xfer_mode == VIO_DESC_MODE) {
41313af08d82Slm66018 
41321ae08745Sheppo 		/*
41331ae08745Sheppo 		 * The vd_dring_inband_msg_t contains one cookie; need room
41341ae08745Sheppo 		 * for up to n-1 more cookies, where "n" is the number of full
41351ae08745Sheppo 		 * pages plus possibly one partial page required to cover
41361ae08745Sheppo 		 * "max_xfer_sz".  Add room for one more cookie if
41371ae08745Sheppo 		 * "max_xfer_sz" isn't an integral multiple of the page size.
41381ae08745Sheppo 		 * Must first get the maximum transfer size in bytes.
41391ae08745Sheppo 		 */
41401ae08745Sheppo 		size_t	max_xfer_bytes = attr_msg->vdisk_block_size ?
41411ae08745Sheppo 		    attr_msg->vdisk_block_size*attr_msg->max_xfer_sz :
41421ae08745Sheppo 		    attr_msg->max_xfer_sz;
41431ae08745Sheppo 		size_t	max_inband_msglen =
41441ae08745Sheppo 		    sizeof (vd_dring_inband_msg_t) +
41451ae08745Sheppo 		    ((max_xfer_bytes/PAGESIZE +
41461ae08745Sheppo 		    ((max_xfer_bytes % PAGESIZE) ? 1 : 0))*
41471ae08745Sheppo 		    (sizeof (ldc_mem_cookie_t)));
41481ae08745Sheppo 
41491ae08745Sheppo 		/*
41501ae08745Sheppo 		 * Set the maximum expected message length to
41511ae08745Sheppo 		 * accommodate in-band-descriptor messages with all
41521ae08745Sheppo 		 * their cookies
41531ae08745Sheppo 		 */
41541ae08745Sheppo 		vd->max_msglen = MAX(vd->max_msglen, max_inband_msglen);
4155d10e4ef2Snarayan 
4156d10e4ef2Snarayan 		/*
4157d10e4ef2Snarayan 		 * Initialize the data structure for processing in-band I/O
4158d10e4ef2Snarayan 		 * request descriptors
4159d10e4ef2Snarayan 		 */
4160d10e4ef2Snarayan 		vd->inband_task.vd	= vd;
41613af08d82Slm66018 		vd->inband_task.msg	= kmem_alloc(vd->max_msglen, KM_SLEEP);
4162d10e4ef2Snarayan 		vd->inband_task.index	= 0;
4163d10e4ef2Snarayan 		vd->inband_task.type	= VD_FINAL_RANGE_TASK;	/* range == 1 */
41641ae08745Sheppo 	}
41651ae08745Sheppo 
4166e1ebb9ecSlm66018 	/* Return the device's block size and max transfer size to the client */
41672f5224aeSachartre 	attr_msg->vdisk_block_size	= vd->vdisk_block_size;
4168e1ebb9ecSlm66018 	attr_msg->max_xfer_sz		= vd->max_xfer_sz;
4169e1ebb9ecSlm66018 
41701ae08745Sheppo 	attr_msg->vdisk_size = vd->vdisk_size;
4171bae9e67eSachartre 	attr_msg->vdisk_type = (vd_slice_single_slice)? vd->vdisk_type :
4172bae9e67eSachartre 	    VD_DISK_TYPE_DISK;
417317cadca8Slm66018 	attr_msg->vdisk_media = vd->vdisk_media;
417417cadca8Slm66018 
417517cadca8Slm66018 	/* Discover and save the list of supported VD_OP_XXX operations */
417617cadca8Slm66018 	vd_set_exported_operations(vd);
417717cadca8Slm66018 	attr_msg->operations = vd->operations;
417817cadca8Slm66018 
41791ae08745Sheppo 	PR0("%s", VD_CLIENT(vd));
41803af08d82Slm66018 
41813af08d82Slm66018 	ASSERT(vd->dring_task == NULL);
41823af08d82Slm66018 
41831ae08745Sheppo 	return (0);
41841ae08745Sheppo }
41851ae08745Sheppo 
41861ae08745Sheppo static int
41871ae08745Sheppo vd_process_dring_reg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
41881ae08745Sheppo {
41891ae08745Sheppo 	int			status;
41901ae08745Sheppo 	size_t			expected;
41911ae08745Sheppo 	ldc_mem_info_t		dring_minfo;
4192bbfa0259Sha137994 	uint8_t			mtype;
41931ae08745Sheppo 	vio_dring_reg_msg_t	*reg_msg = (vio_dring_reg_msg_t *)msg;
41941ae08745Sheppo 
41951ae08745Sheppo 
41961ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
41971ae08745Sheppo 
41981ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
41991ae08745Sheppo 	    VIO_DRING_REG)) {
4200d10e4ef2Snarayan 		PR0("Message is not a register-dring message");
4201d10e4ef2Snarayan 		return (ENOMSG);
42021ae08745Sheppo 	}
42031ae08745Sheppo 
42041ae08745Sheppo 	if (msglen < sizeof (*reg_msg)) {
42053af08d82Slm66018 		PR0("Expected at least %lu-byte register-dring message; "
42061ae08745Sheppo 		    "received %lu bytes", sizeof (*reg_msg), msglen);
42071ae08745Sheppo 		return (EBADMSG);
42081ae08745Sheppo 	}
42091ae08745Sheppo 
42101ae08745Sheppo 	expected = sizeof (*reg_msg) +
42111ae08745Sheppo 	    (reg_msg->ncookies - 1)*(sizeof (reg_msg->cookie[0]));
42121ae08745Sheppo 	if (msglen != expected) {
42133af08d82Slm66018 		PR0("Expected %lu-byte register-dring message; "
42141ae08745Sheppo 		    "received %lu bytes", expected, msglen);
42151ae08745Sheppo 		return (EBADMSG);
42161ae08745Sheppo 	}
42171ae08745Sheppo 
42181ae08745Sheppo 	if (vd->initialized & VD_DRING) {
42193af08d82Slm66018 		PR0("A dring was previously registered; only support one");
42201ae08745Sheppo 		return (EBADMSG);
42211ae08745Sheppo 	}
42221ae08745Sheppo 
4223d10e4ef2Snarayan 	if (reg_msg->num_descriptors > INT32_MAX) {
42243af08d82Slm66018 		PR0("reg_msg->num_descriptors = %u; must be <= %u (%s)",
4225d10e4ef2Snarayan 		    reg_msg->ncookies, INT32_MAX, STRINGIZE(INT32_MAX));
4226d10e4ef2Snarayan 		return (EBADMSG);
4227d10e4ef2Snarayan 	}
4228d10e4ef2Snarayan 
42291ae08745Sheppo 	if (reg_msg->ncookies != 1) {
42301ae08745Sheppo 		/*
42311ae08745Sheppo 		 * In addition to fixing the assertion in the success case
42321ae08745Sheppo 		 * below, supporting drings which require more than one
42331ae08745Sheppo 		 * "cookie" requires increasing the value of vd->max_msglen
42341ae08745Sheppo 		 * somewhere in the code path prior to receiving the message
42351ae08745Sheppo 		 * which results in calling this function.  Note that without
42361ae08745Sheppo 		 * making this change, the larger message size required to
42371ae08745Sheppo 		 * accommodate multiple cookies cannot be successfully
42381ae08745Sheppo 		 * received, so this function will not even get called.
42391ae08745Sheppo 		 * Gracefully accommodating more dring cookies might
42401ae08745Sheppo 		 * reasonably demand exchanging an additional attribute or
42411ae08745Sheppo 		 * making a minor protocol adjustment
42421ae08745Sheppo 		 */
42433af08d82Slm66018 		PR0("reg_msg->ncookies = %u != 1", reg_msg->ncookies);
42441ae08745Sheppo 		return (EBADMSG);
42451ae08745Sheppo 	}
42461ae08745Sheppo 
4247bbfa0259Sha137994 	if (vd_direct_mapped_drings)
4248bbfa0259Sha137994 		mtype = LDC_DIRECT_MAP;
4249bbfa0259Sha137994 	else
4250bbfa0259Sha137994 		mtype = LDC_SHADOW_MAP;
4251bbfa0259Sha137994 
42521ae08745Sheppo 	status = ldc_mem_dring_map(vd->ldc_handle, reg_msg->cookie,
42531ae08745Sheppo 	    reg_msg->ncookies, reg_msg->num_descriptors,
4254bbfa0259Sha137994 	    reg_msg->descriptor_size, mtype, &vd->dring_handle);
42551ae08745Sheppo 	if (status != 0) {
42563af08d82Slm66018 		PR0("ldc_mem_dring_map() returned errno %d", status);
42571ae08745Sheppo 		return (status);
42581ae08745Sheppo 	}
42591ae08745Sheppo 
42601ae08745Sheppo 	/*
42611ae08745Sheppo 	 * To remove the need for this assertion, must call
42621ae08745Sheppo 	 * ldc_mem_dring_nextcookie() successfully ncookies-1 times after a
42631ae08745Sheppo 	 * successful call to ldc_mem_dring_map()
42641ae08745Sheppo 	 */
42651ae08745Sheppo 	ASSERT(reg_msg->ncookies == 1);
42661ae08745Sheppo 
42671ae08745Sheppo 	if ((status =
42681ae08745Sheppo 	    ldc_mem_dring_info(vd->dring_handle, &dring_minfo)) != 0) {
42693af08d82Slm66018 		PR0("ldc_mem_dring_info() returned errno %d", status);
42701ae08745Sheppo 		if ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0)
42713af08d82Slm66018 			PR0("ldc_mem_dring_unmap() returned errno %d", status);
42721ae08745Sheppo 		return (status);
42731ae08745Sheppo 	}
42741ae08745Sheppo 
42751ae08745Sheppo 	if (dring_minfo.vaddr == NULL) {
42763af08d82Slm66018 		PR0("Descriptor ring virtual address is NULL");
42770a55fbb7Slm66018 		return (ENXIO);
42781ae08745Sheppo 	}
42791ae08745Sheppo 
42801ae08745Sheppo 
4281d10e4ef2Snarayan 	/* Initialize for valid message and mapped dring */
42821ae08745Sheppo 	vd->initialized |= VD_DRING;
42831ae08745Sheppo 	vd->dring_ident = 1;	/* "There Can Be Only One" */
42841ae08745Sheppo 	vd->dring = dring_minfo.vaddr;
42851ae08745Sheppo 	vd->descriptor_size = reg_msg->descriptor_size;
42861ae08745Sheppo 	vd->dring_len = reg_msg->num_descriptors;
4287bbfa0259Sha137994 	vd->dring_mtype = dring_minfo.mtype;
42881ae08745Sheppo 	reg_msg->dring_ident = vd->dring_ident;
42895b7cb889Sha137994 	PR1("descriptor size = %u, dring length = %u",
42905b7cb889Sha137994 	    vd->descriptor_size, vd->dring_len);
4291d10e4ef2Snarayan 
4292d10e4ef2Snarayan 	/*
4293d10e4ef2Snarayan 	 * Allocate and initialize a "shadow" array of data structures for
4294d10e4ef2Snarayan 	 * tasks to process I/O requests in dring elements
4295d10e4ef2Snarayan 	 */
4296d10e4ef2Snarayan 	vd->dring_task =
4297d10e4ef2Snarayan 	    kmem_zalloc((sizeof (*vd->dring_task)) * vd->dring_len, KM_SLEEP);
4298d10e4ef2Snarayan 	for (int i = 0; i < vd->dring_len; i++) {
4299d10e4ef2Snarayan 		vd->dring_task[i].vd		= vd;
4300d10e4ef2Snarayan 		vd->dring_task[i].index		= i;
43014bac2208Snarayan 
43024bac2208Snarayan 		status = ldc_mem_alloc_handle(vd->ldc_handle,
43034bac2208Snarayan 		    &(vd->dring_task[i].mhdl));
43044bac2208Snarayan 		if (status) {
43053af08d82Slm66018 			PR0("ldc_mem_alloc_handle() returned err %d ", status);
43064bac2208Snarayan 			return (ENXIO);
43074bac2208Snarayan 		}
43083af08d82Slm66018 
43095b7cb889Sha137994 		/*
43105b7cb889Sha137994 		 * The descriptor payload varies in length. Calculate its
43115b7cb889Sha137994 		 * size by subtracting the header size from the total
43125b7cb889Sha137994 		 * descriptor size.
43135b7cb889Sha137994 		 */
43145b7cb889Sha137994 		vd->dring_task[i].request = kmem_zalloc((vd->descriptor_size -
43155b7cb889Sha137994 		    sizeof (vio_dring_entry_hdr_t)), KM_SLEEP);
43163af08d82Slm66018 		vd->dring_task[i].msg = kmem_alloc(vd->max_msglen, KM_SLEEP);
4317d10e4ef2Snarayan 	}
4318d10e4ef2Snarayan 
43191ae08745Sheppo 	return (0);
43201ae08745Sheppo }
43211ae08745Sheppo 
43221ae08745Sheppo static int
43231ae08745Sheppo vd_process_dring_unreg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
43241ae08745Sheppo {
43251ae08745Sheppo 	vio_dring_unreg_msg_t	*unreg_msg = (vio_dring_unreg_msg_t *)msg;
43261ae08745Sheppo 
43271ae08745Sheppo 
43281ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
43291ae08745Sheppo 
43301ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
43311ae08745Sheppo 	    VIO_DRING_UNREG)) {
4332d10e4ef2Snarayan 		PR0("Message is not an unregister-dring message");
4333d10e4ef2Snarayan 		return (ENOMSG);
43341ae08745Sheppo 	}
43351ae08745Sheppo 
43361ae08745Sheppo 	if (msglen != sizeof (*unreg_msg)) {
43373af08d82Slm66018 		PR0("Expected %lu-byte unregister-dring message; "
43381ae08745Sheppo 		    "received %lu bytes", sizeof (*unreg_msg), msglen);
43391ae08745Sheppo 		return (EBADMSG);
43401ae08745Sheppo 	}
43411ae08745Sheppo 
43421ae08745Sheppo 	if (unreg_msg->dring_ident != vd->dring_ident) {
43433af08d82Slm66018 		PR0("Expected dring ident %lu; received %lu",
43441ae08745Sheppo 		    vd->dring_ident, unreg_msg->dring_ident);
43451ae08745Sheppo 		return (EBADMSG);
43461ae08745Sheppo 	}
43471ae08745Sheppo 
43481ae08745Sheppo 	return (0);
43491ae08745Sheppo }
43501ae08745Sheppo 
43511ae08745Sheppo static int
43521ae08745Sheppo process_rdx_msg(vio_msg_t *msg, size_t msglen)
43531ae08745Sheppo {
43541ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
43551ae08745Sheppo 
4356d10e4ef2Snarayan 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX)) {
4357d10e4ef2Snarayan 		PR0("Message is not an RDX message");
4358d10e4ef2Snarayan 		return (ENOMSG);
4359d10e4ef2Snarayan 	}
43601ae08745Sheppo 
43611ae08745Sheppo 	if (msglen != sizeof (vio_rdx_msg_t)) {
43623af08d82Slm66018 		PR0("Expected %lu-byte RDX message; received %lu bytes",
43631ae08745Sheppo 		    sizeof (vio_rdx_msg_t), msglen);
43641ae08745Sheppo 		return (EBADMSG);
43651ae08745Sheppo 	}
43661ae08745Sheppo 
4367d10e4ef2Snarayan 	PR0("Valid RDX message");
43681ae08745Sheppo 	return (0);
43691ae08745Sheppo }
43701ae08745Sheppo 
43711ae08745Sheppo static int
43721ae08745Sheppo vd_check_seq_num(vd_t *vd, uint64_t seq_num)
43731ae08745Sheppo {
43741ae08745Sheppo 	if ((vd->initialized & VD_SEQ_NUM) && (seq_num != vd->seq_num + 1)) {
43753af08d82Slm66018 		PR0("Received seq_num %lu; expected %lu",
43761ae08745Sheppo 		    seq_num, (vd->seq_num + 1));
43773af08d82Slm66018 		PR0("initiating soft reset");
4378d10e4ef2Snarayan 		vd_need_reset(vd, B_FALSE);
43791ae08745Sheppo 		return (1);
43801ae08745Sheppo 	}
43811ae08745Sheppo 
43821ae08745Sheppo 	vd->seq_num = seq_num;
43831ae08745Sheppo 	vd->initialized |= VD_SEQ_NUM;	/* superfluous after first time... */
43841ae08745Sheppo 	return (0);
43851ae08745Sheppo }
43861ae08745Sheppo 
43871ae08745Sheppo /*
43881ae08745Sheppo  * Return the expected size of an inband-descriptor message with all the
43891ae08745Sheppo  * cookies it claims to include
43901ae08745Sheppo  */
43911ae08745Sheppo static size_t
43921ae08745Sheppo expected_inband_size(vd_dring_inband_msg_t *msg)
43931ae08745Sheppo {
43941ae08745Sheppo 	return ((sizeof (*msg)) +
43951ae08745Sheppo 	    (msg->payload.ncookies - 1)*(sizeof (msg->payload.cookie[0])));
43961ae08745Sheppo }
43971ae08745Sheppo 
43981ae08745Sheppo /*
43991ae08745Sheppo  * Process an in-band descriptor message:  used with clients like OBP, with
44001ae08745Sheppo  * which vds exchanges descriptors within VIO message payloads, rather than
44011ae08745Sheppo  * operating on them within a descriptor ring
44021ae08745Sheppo  */
44031ae08745Sheppo static int
44043af08d82Slm66018 vd_process_desc_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
44051ae08745Sheppo {
44061ae08745Sheppo 	size_t			expected;
44071ae08745Sheppo 	vd_dring_inband_msg_t	*desc_msg = (vd_dring_inband_msg_t *)msg;
44081ae08745Sheppo 
44091ae08745Sheppo 
44101ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
44111ae08745Sheppo 
44121ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
4413d10e4ef2Snarayan 	    VIO_DESC_DATA)) {
4414d10e4ef2Snarayan 		PR1("Message is not an in-band-descriptor message");
4415d10e4ef2Snarayan 		return (ENOMSG);
4416d10e4ef2Snarayan 	}
44171ae08745Sheppo 
44181ae08745Sheppo 	if (msglen < sizeof (*desc_msg)) {
44193af08d82Slm66018 		PR0("Expected at least %lu-byte descriptor message; "
44201ae08745Sheppo 		    "received %lu bytes", sizeof (*desc_msg), msglen);
44211ae08745Sheppo 		return (EBADMSG);
44221ae08745Sheppo 	}
44231ae08745Sheppo 
44241ae08745Sheppo 	if (msglen != (expected = expected_inband_size(desc_msg))) {
44253af08d82Slm66018 		PR0("Expected %lu-byte descriptor message; "
44261ae08745Sheppo 		    "received %lu bytes", expected, msglen);
44271ae08745Sheppo 		return (EBADMSG);
44281ae08745Sheppo 	}
44291ae08745Sheppo 
4430d10e4ef2Snarayan 	if (vd_check_seq_num(vd, desc_msg->hdr.seq_num) != 0)
44311ae08745Sheppo 		return (EBADMSG);
44321ae08745Sheppo 
4433d10e4ef2Snarayan 	/*
4434d10e4ef2Snarayan 	 * Valid message:  Set up the in-band descriptor task and process the
4435d10e4ef2Snarayan 	 * request.  Arrange to acknowledge the client's message, unless an
4436d10e4ef2Snarayan 	 * error processing the descriptor task results in setting
4437d10e4ef2Snarayan 	 * VIO_SUBTYPE_NACK
4438d10e4ef2Snarayan 	 */
4439d10e4ef2Snarayan 	PR1("Valid in-band-descriptor message");
4440d10e4ef2Snarayan 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
44413af08d82Slm66018 
44423af08d82Slm66018 	ASSERT(vd->inband_task.msg != NULL);
44433af08d82Slm66018 
44443af08d82Slm66018 	bcopy(msg, vd->inband_task.msg, msglen);
4445d10e4ef2Snarayan 	vd->inband_task.msglen	= msglen;
44463af08d82Slm66018 
44473af08d82Slm66018 	/*
44483af08d82Slm66018 	 * The task request is now the payload of the message
44493af08d82Slm66018 	 * that was just copied into the body of the task.
44503af08d82Slm66018 	 */
44513af08d82Slm66018 	desc_msg = (vd_dring_inband_msg_t *)vd->inband_task.msg;
4452d10e4ef2Snarayan 	vd->inband_task.request	= &desc_msg->payload;
44533af08d82Slm66018 
4454d10e4ef2Snarayan 	return (vd_process_task(&vd->inband_task));
44551ae08745Sheppo }
44561ae08745Sheppo 
44571ae08745Sheppo static int
4458d10e4ef2Snarayan vd_process_element(vd_t *vd, vd_task_type_t type, uint32_t idx,
44593af08d82Slm66018     vio_msg_t *msg, size_t msglen)
44601ae08745Sheppo {
44611ae08745Sheppo 	int			status;
4462d10e4ef2Snarayan 	boolean_t		ready;
4463bbfa0259Sha137994 	on_trap_data_t		otd;
4464d10e4ef2Snarayan 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
44651ae08745Sheppo 
4466d10e4ef2Snarayan 	/* Accept the updated dring element */
4467bbfa0259Sha137994 	if ((status = VIO_DRING_ACQUIRE(&otd, vd->dring_mtype,
4468bbfa0259Sha137994 	    vd->dring_handle, idx, idx)) != 0) {
44691ae08745Sheppo 		return (status);
44701ae08745Sheppo 	}
4471d10e4ef2Snarayan 	ready = (elem->hdr.dstate == VIO_DESC_READY);
4472d10e4ef2Snarayan 	if (ready) {
4473d10e4ef2Snarayan 		elem->hdr.dstate = VIO_DESC_ACCEPTED;
44745b7cb889Sha137994 		bcopy(&elem->payload, vd->dring_task[idx].request,
44755b7cb889Sha137994 		    (vd->descriptor_size - sizeof (vio_dring_entry_hdr_t)));
4476d10e4ef2Snarayan 	} else {
44773af08d82Slm66018 		PR0("descriptor %u not ready", idx);
4478d10e4ef2Snarayan 		VD_DUMP_DRING_ELEM(elem);
4479d10e4ef2Snarayan 	}
4480bbfa0259Sha137994 	if ((status = VIO_DRING_RELEASE(vd->dring_mtype,
4481bbfa0259Sha137994 	    vd->dring_handle, idx, idx)) != 0) {
4482bbfa0259Sha137994 		PR0("VIO_DRING_RELEASE() returned errno %d", status);
44831ae08745Sheppo 		return (status);
44841ae08745Sheppo 	}
4485d10e4ef2Snarayan 	if (!ready)
4486d10e4ef2Snarayan 		return (EBUSY);
44871ae08745Sheppo 
44881ae08745Sheppo 
4489d10e4ef2Snarayan 	/* Initialize a task and process the accepted element */
4490d10e4ef2Snarayan 	PR1("Processing dring element %u", idx);
4491d10e4ef2Snarayan 	vd->dring_task[idx].type	= type;
44923af08d82Slm66018 
44933af08d82Slm66018 	/* duplicate msg buf for cookies etc. */
44943af08d82Slm66018 	bcopy(msg, vd->dring_task[idx].msg, msglen);
44953af08d82Slm66018 
4496d10e4ef2Snarayan 	vd->dring_task[idx].msglen	= msglen;
4497205eeb1aSlm66018 	return (vd_process_task(&vd->dring_task[idx]));
44981ae08745Sheppo }
44991ae08745Sheppo 
45001ae08745Sheppo static int
4501d10e4ef2Snarayan vd_process_element_range(vd_t *vd, int start, int end,
45023af08d82Slm66018     vio_msg_t *msg, size_t msglen)
4503d10e4ef2Snarayan {
4504d10e4ef2Snarayan 	int		i, n, nelem, status = 0;
4505d10e4ef2Snarayan 	boolean_t	inprogress = B_FALSE;
4506d10e4ef2Snarayan 	vd_task_type_t	type;
4507d10e4ef2Snarayan 
4508d10e4ef2Snarayan 
4509d10e4ef2Snarayan 	ASSERT(start >= 0);
4510d10e4ef2Snarayan 	ASSERT(end >= 0);
4511d10e4ef2Snarayan 
4512d10e4ef2Snarayan 	/*
4513d10e4ef2Snarayan 	 * Arrange to acknowledge the client's message, unless an error
4514d10e4ef2Snarayan 	 * processing one of the dring elements results in setting
4515d10e4ef2Snarayan 	 * VIO_SUBTYPE_NACK
4516d10e4ef2Snarayan 	 */
4517d10e4ef2Snarayan 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
4518d10e4ef2Snarayan 
4519d10e4ef2Snarayan 	/*
4520d10e4ef2Snarayan 	 * Process the dring elements in the range
4521d10e4ef2Snarayan 	 */
4522d10e4ef2Snarayan 	nelem = ((end < start) ? end + vd->dring_len : end) - start + 1;
4523d10e4ef2Snarayan 	for (i = start, n = nelem; n > 0; i = (i + 1) % vd->dring_len, n--) {
4524d10e4ef2Snarayan 		((vio_dring_msg_t *)msg)->end_idx = i;
4525d10e4ef2Snarayan 		type = (n == 1) ? VD_FINAL_RANGE_TASK : VD_NONFINAL_RANGE_TASK;
45263af08d82Slm66018 		status = vd_process_element(vd, type, i, msg, msglen);
4527d10e4ef2Snarayan 		if (status == EINPROGRESS)
4528d10e4ef2Snarayan 			inprogress = B_TRUE;
4529d10e4ef2Snarayan 		else if (status != 0)
4530d10e4ef2Snarayan 			break;
4531d10e4ef2Snarayan 	}
4532d10e4ef2Snarayan 
4533d10e4ef2Snarayan 	/*
4534d10e4ef2Snarayan 	 * If some, but not all, operations of a multi-element range are in
4535d10e4ef2Snarayan 	 * progress, wait for other operations to complete before returning
4536d10e4ef2Snarayan 	 * (which will result in "ack" or "nack" of the message).  Note that
4537d10e4ef2Snarayan 	 * all outstanding operations will need to complete, not just the ones
4538d10e4ef2Snarayan 	 * corresponding to the current range of dring elements; howevever, as
4539d10e4ef2Snarayan 	 * this situation is an error case, performance is less critical.
4540d10e4ef2Snarayan 	 */
4541d10e4ef2Snarayan 	if ((nelem > 1) && (status != EINPROGRESS) && inprogress)
4542d10e4ef2Snarayan 		ddi_taskq_wait(vd->completionq);
4543d10e4ef2Snarayan 
4544d10e4ef2Snarayan 	return (status);
4545d10e4ef2Snarayan }
4546d10e4ef2Snarayan 
4547d10e4ef2Snarayan static int
45483af08d82Slm66018 vd_process_dring_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
45491ae08745Sheppo {
45501ae08745Sheppo 	vio_dring_msg_t	*dring_msg = (vio_dring_msg_t *)msg;
45511ae08745Sheppo 
45521ae08745Sheppo 
45531ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
45541ae08745Sheppo 
45551ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
45561ae08745Sheppo 	    VIO_DRING_DATA)) {
4557d10e4ef2Snarayan 		PR1("Message is not a dring-data message");
4558d10e4ef2Snarayan 		return (ENOMSG);
45591ae08745Sheppo 	}
45601ae08745Sheppo 
45611ae08745Sheppo 	if (msglen != sizeof (*dring_msg)) {
45623af08d82Slm66018 		PR0("Expected %lu-byte dring message; received %lu bytes",
45631ae08745Sheppo 		    sizeof (*dring_msg), msglen);
45641ae08745Sheppo 		return (EBADMSG);
45651ae08745Sheppo 	}
45661ae08745Sheppo 
4567d10e4ef2Snarayan 	if (vd_check_seq_num(vd, dring_msg->seq_num) != 0)
45681ae08745Sheppo 		return (EBADMSG);
45691ae08745Sheppo 
45701ae08745Sheppo 	if (dring_msg->dring_ident != vd->dring_ident) {
45713af08d82Slm66018 		PR0("Expected dring ident %lu; received ident %lu",
45721ae08745Sheppo 		    vd->dring_ident, dring_msg->dring_ident);
45731ae08745Sheppo 		return (EBADMSG);
45741ae08745Sheppo 	}
45751ae08745Sheppo 
4576d10e4ef2Snarayan 	if (dring_msg->start_idx >= vd->dring_len) {
45773af08d82Slm66018 		PR0("\"start_idx\" = %u; must be less than %u",
4578d10e4ef2Snarayan 		    dring_msg->start_idx, vd->dring_len);
4579d10e4ef2Snarayan 		return (EBADMSG);
4580d10e4ef2Snarayan 	}
45811ae08745Sheppo 
4582d10e4ef2Snarayan 	if ((dring_msg->end_idx < 0) ||
4583d10e4ef2Snarayan 	    (dring_msg->end_idx >= vd->dring_len)) {
45843af08d82Slm66018 		PR0("\"end_idx\" = %u; must be >= 0 and less than %u",
4585d10e4ef2Snarayan 		    dring_msg->end_idx, vd->dring_len);
4586d10e4ef2Snarayan 		return (EBADMSG);
4587d10e4ef2Snarayan 	}
4588d10e4ef2Snarayan 
4589d10e4ef2Snarayan 	/* Valid message; process range of updated dring elements */
4590d10e4ef2Snarayan 	PR1("Processing descriptor range, start = %u, end = %u",
4591d10e4ef2Snarayan 	    dring_msg->start_idx, dring_msg->end_idx);
4592d10e4ef2Snarayan 	return (vd_process_element_range(vd, dring_msg->start_idx,
45933af08d82Slm66018 	    dring_msg->end_idx, msg, msglen));
45941ae08745Sheppo }
45951ae08745Sheppo 
45961ae08745Sheppo static int
45971ae08745Sheppo recv_msg(ldc_handle_t ldc_handle, void *msg, size_t *nbytes)
45981ae08745Sheppo {
45991ae08745Sheppo 	int	retry, status;
46001ae08745Sheppo 	size_t	size = *nbytes;
46011ae08745Sheppo 
46021ae08745Sheppo 
46031ae08745Sheppo 	for (retry = 0, status = ETIMEDOUT;
46041ae08745Sheppo 	    retry < vds_ldc_retries && status == ETIMEDOUT;
46051ae08745Sheppo 	    retry++) {
46061ae08745Sheppo 		PR1("ldc_read() attempt %d", (retry + 1));
46071ae08745Sheppo 		*nbytes = size;
46081ae08745Sheppo 		status = ldc_read(ldc_handle, msg, nbytes);
46091ae08745Sheppo 	}
46101ae08745Sheppo 
46113af08d82Slm66018 	if (status) {
46123af08d82Slm66018 		PR0("ldc_read() returned errno %d", status);
46133af08d82Slm66018 		if (status != ECONNRESET)
46143af08d82Slm66018 			return (ENOMSG);
46151ae08745Sheppo 		return (status);
46161ae08745Sheppo 	} else if (*nbytes == 0) {
46171ae08745Sheppo 		PR1("ldc_read() returned 0 and no message read");
46181ae08745Sheppo 		return (ENOMSG);
46191ae08745Sheppo 	}
46201ae08745Sheppo 
46211ae08745Sheppo 	PR1("RCVD %lu-byte message", *nbytes);
46221ae08745Sheppo 	return (0);
46231ae08745Sheppo }
46241ae08745Sheppo 
46251ae08745Sheppo static int
46263af08d82Slm66018 vd_do_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
46271ae08745Sheppo {
46281ae08745Sheppo 	int		status;
46291ae08745Sheppo 
46301ae08745Sheppo 
46311ae08745Sheppo 	PR1("Processing (%x/%x/%x) message", msg->tag.vio_msgtype,
46321ae08745Sheppo 	    msg->tag.vio_subtype, msg->tag.vio_subtype_env);
46333af08d82Slm66018 #ifdef	DEBUG
46343af08d82Slm66018 	vd_decode_tag(msg);
46353af08d82Slm66018 #endif
46361ae08745Sheppo 
46371ae08745Sheppo 	/*
46381ae08745Sheppo 	 * Validate session ID up front, since it applies to all messages
46391ae08745Sheppo 	 * once set
46401ae08745Sheppo 	 */
46411ae08745Sheppo 	if ((msg->tag.vio_sid != vd->sid) && (vd->initialized & VD_SID)) {
46423af08d82Slm66018 		PR0("Expected SID %u, received %u", vd->sid,
46431ae08745Sheppo 		    msg->tag.vio_sid);
46441ae08745Sheppo 		return (EBADMSG);
46451ae08745Sheppo 	}
46461ae08745Sheppo 
46473af08d82Slm66018 	PR1("\tWhile in state %d (%s)", vd->state, vd_decode_state(vd->state));
46481ae08745Sheppo 
46491ae08745Sheppo 	/*
46501ae08745Sheppo 	 * Process the received message based on connection state
46511ae08745Sheppo 	 */
46521ae08745Sheppo 	switch (vd->state) {
46531ae08745Sheppo 	case VD_STATE_INIT:	/* expect version message */
46540a55fbb7Slm66018 		if ((status = vd_process_ver_msg(vd, msg, msglen)) != 0)
46551ae08745Sheppo 			return (status);
46561ae08745Sheppo 
46571ae08745Sheppo 		/* Version negotiated, move to that state */
46581ae08745Sheppo 		vd->state = VD_STATE_VER;
46591ae08745Sheppo 		return (0);
46601ae08745Sheppo 
46611ae08745Sheppo 	case VD_STATE_VER:	/* expect attribute message */
46621ae08745Sheppo 		if ((status = vd_process_attr_msg(vd, msg, msglen)) != 0)
46631ae08745Sheppo 			return (status);
46641ae08745Sheppo 
46651ae08745Sheppo 		/* Attributes exchanged, move to that state */
46661ae08745Sheppo 		vd->state = VD_STATE_ATTR;
46671ae08745Sheppo 		return (0);
46681ae08745Sheppo 
46691ae08745Sheppo 	case VD_STATE_ATTR:
46701ae08745Sheppo 		switch (vd->xfer_mode) {
46711ae08745Sheppo 		case VIO_DESC_MODE:	/* expect RDX message */
46721ae08745Sheppo 			if ((status = process_rdx_msg(msg, msglen)) != 0)
46731ae08745Sheppo 				return (status);
46741ae08745Sheppo 
46751ae08745Sheppo 			/* Ready to receive in-band descriptors */
46761ae08745Sheppo 			vd->state = VD_STATE_DATA;
46771ae08745Sheppo 			return (0);
46781ae08745Sheppo 
4679f0ca1d9aSsb155480 		case VIO_DRING_MODE_V1_0:  /* expect register-dring message */
46801ae08745Sheppo 			if ((status =
46811ae08745Sheppo 			    vd_process_dring_reg_msg(vd, msg, msglen)) != 0)
46821ae08745Sheppo 				return (status);
46831ae08745Sheppo 
46841ae08745Sheppo 			/* One dring negotiated, move to that state */
46851ae08745Sheppo 			vd->state = VD_STATE_DRING;
46861ae08745Sheppo 			return (0);
46871ae08745Sheppo 
46881ae08745Sheppo 		default:
46891ae08745Sheppo 			ASSERT("Unsupported transfer mode");
46903af08d82Slm66018 			PR0("Unsupported transfer mode");
46911ae08745Sheppo 			return (ENOTSUP);
46921ae08745Sheppo 		}
46931ae08745Sheppo 
46941ae08745Sheppo 	case VD_STATE_DRING:	/* expect RDX, register-dring, or unreg-dring */
46951ae08745Sheppo 		if ((status = process_rdx_msg(msg, msglen)) == 0) {
46961ae08745Sheppo 			/* Ready to receive data */
46971ae08745Sheppo 			vd->state = VD_STATE_DATA;
46981ae08745Sheppo 			return (0);
46991ae08745Sheppo 		} else if (status != ENOMSG) {
47001ae08745Sheppo 			return (status);
47011ae08745Sheppo 		}
47021ae08745Sheppo 
47031ae08745Sheppo 
47041ae08745Sheppo 		/*
47051ae08745Sheppo 		 * If another register-dring message is received, stay in
47061ae08745Sheppo 		 * dring state in case the client sends RDX; although the
47071ae08745Sheppo 		 * protocol allows multiple drings, this server does not
47081ae08745Sheppo 		 * support using more than one
47091ae08745Sheppo 		 */
47101ae08745Sheppo 		if ((status =
47111ae08745Sheppo 		    vd_process_dring_reg_msg(vd, msg, msglen)) != ENOMSG)
47121ae08745Sheppo 			return (status);
47131ae08745Sheppo 
47141ae08745Sheppo 		/*
47151ae08745Sheppo 		 * Acknowledge an unregister-dring message, but reset the
47161ae08745Sheppo 		 * connection anyway:  Although the protocol allows
47171ae08745Sheppo 		 * unregistering drings, this server cannot serve a vdisk
47181ae08745Sheppo 		 * without its only dring
47191ae08745Sheppo 		 */
47201ae08745Sheppo 		status = vd_process_dring_unreg_msg(vd, msg, msglen);
47211ae08745Sheppo 		return ((status == 0) ? ENOTSUP : status);
47221ae08745Sheppo 
47231ae08745Sheppo 	case VD_STATE_DATA:
47241ae08745Sheppo 		switch (vd->xfer_mode) {
47251ae08745Sheppo 		case VIO_DESC_MODE:	/* expect in-band-descriptor message */
47263af08d82Slm66018 			return (vd_process_desc_msg(vd, msg, msglen));
47271ae08745Sheppo 
4728f0ca1d9aSsb155480 		case VIO_DRING_MODE_V1_0: /* expect dring-data or unreg-dring */
47291ae08745Sheppo 			/*
47301ae08745Sheppo 			 * Typically expect dring-data messages, so handle
47311ae08745Sheppo 			 * them first
47321ae08745Sheppo 			 */
47331ae08745Sheppo 			if ((status = vd_process_dring_msg(vd, msg,
47343af08d82Slm66018 			    msglen)) != ENOMSG)
47351ae08745Sheppo 				return (status);
47361ae08745Sheppo 
47371ae08745Sheppo 			/*
47381ae08745Sheppo 			 * Acknowledge an unregister-dring message, but reset
47391ae08745Sheppo 			 * the connection anyway:  Although the protocol
47401ae08745Sheppo 			 * allows unregistering drings, this server cannot
47411ae08745Sheppo 			 * serve a vdisk without its only dring
47421ae08745Sheppo 			 */
47431ae08745Sheppo 			status = vd_process_dring_unreg_msg(vd, msg, msglen);
47441ae08745Sheppo 			return ((status == 0) ? ENOTSUP : status);
47451ae08745Sheppo 
47461ae08745Sheppo 		default:
47471ae08745Sheppo 			ASSERT("Unsupported transfer mode");
47483af08d82Slm66018 			PR0("Unsupported transfer mode");
47491ae08745Sheppo 			return (ENOTSUP);
47501ae08745Sheppo 		}
47511ae08745Sheppo 
47521ae08745Sheppo 	default:
47531ae08745Sheppo 		ASSERT("Invalid client connection state");
47543af08d82Slm66018 		PR0("Invalid client connection state");
47551ae08745Sheppo 		return (ENOTSUP);
47561ae08745Sheppo 	}
47571ae08745Sheppo }
47581ae08745Sheppo 
4759d10e4ef2Snarayan static int
47603af08d82Slm66018 vd_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
47611ae08745Sheppo {
47621ae08745Sheppo 	int		status;
47631ae08745Sheppo 	boolean_t	reset_ldc = B_FALSE;
4764205eeb1aSlm66018 	vd_task_t	task;
47651ae08745Sheppo 
47661ae08745Sheppo 	/*
47671ae08745Sheppo 	 * Check that the message is at least big enough for a "tag", so that
47681ae08745Sheppo 	 * message processing can proceed based on tag-specified message type
47691ae08745Sheppo 	 */
47701ae08745Sheppo 	if (msglen < sizeof (vio_msg_tag_t)) {
47713af08d82Slm66018 		PR0("Received short (%lu-byte) message", msglen);
47721ae08745Sheppo 		/* Can't "nack" short message, so drop the big hammer */
47733af08d82Slm66018 		PR0("initiating full reset");
4774d10e4ef2Snarayan 		vd_need_reset(vd, B_TRUE);
4775d10e4ef2Snarayan 		return (EBADMSG);
47761ae08745Sheppo 	}
47771ae08745Sheppo 
47781ae08745Sheppo 	/*
47791ae08745Sheppo 	 * Process the message
47801ae08745Sheppo 	 */
47813af08d82Slm66018 	switch (status = vd_do_process_msg(vd, msg, msglen)) {
47821ae08745Sheppo 	case 0:
47831ae08745Sheppo 		/* "ack" valid, successfully-processed messages */
47841ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
47851ae08745Sheppo 		break;
47861ae08745Sheppo 
4787d10e4ef2Snarayan 	case EINPROGRESS:
4788d10e4ef2Snarayan 		/* The completion handler will "ack" or "nack" the message */
4789d10e4ef2Snarayan 		return (EINPROGRESS);
47901ae08745Sheppo 	case ENOMSG:
47913af08d82Slm66018 		PR0("Received unexpected message");
47921ae08745Sheppo 		_NOTE(FALLTHROUGH);
47931ae08745Sheppo 	case EBADMSG:
47941ae08745Sheppo 	case ENOTSUP:
4795205eeb1aSlm66018 		/* "transport" error will cause NACK of invalid messages */
47961ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
47971ae08745Sheppo 		break;
47981ae08745Sheppo 
47991ae08745Sheppo 	default:
4800205eeb1aSlm66018 		/* "transport" error will cause NACK of invalid messages */
48011ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
48021ae08745Sheppo 		/* An LDC error probably occurred, so try resetting it */
48031ae08745Sheppo 		reset_ldc = B_TRUE;
48041ae08745Sheppo 		break;
48051ae08745Sheppo 	}
48061ae08745Sheppo 
48073af08d82Slm66018 	PR1("\tResulting in state %d (%s)", vd->state,
48083af08d82Slm66018 	    vd_decode_state(vd->state));
48093af08d82Slm66018 
4810205eeb1aSlm66018 	/* populate the task so we can dispatch it on the taskq */
4811205eeb1aSlm66018 	task.vd = vd;
4812205eeb1aSlm66018 	task.msg = msg;
4813205eeb1aSlm66018 	task.msglen = msglen;
4814205eeb1aSlm66018 
4815205eeb1aSlm66018 	/*
4816205eeb1aSlm66018 	 * Queue a task to send the notification that the operation completed.
4817205eeb1aSlm66018 	 * We need to ensure that requests are responded to in the correct
4818205eeb1aSlm66018 	 * order and since the taskq is processed serially this ordering
4819205eeb1aSlm66018 	 * is maintained.
4820205eeb1aSlm66018 	 */
4821205eeb1aSlm66018 	(void) ddi_taskq_dispatch(vd->completionq, vd_serial_notify,
4822205eeb1aSlm66018 	    &task, DDI_SLEEP);
4823205eeb1aSlm66018 
4824205eeb1aSlm66018 	/*
4825205eeb1aSlm66018 	 * To ensure handshake negotiations do not happen out of order, such
4826205eeb1aSlm66018 	 * requests that come through this path should not be done in parallel
4827205eeb1aSlm66018 	 * so we need to wait here until the response is sent to the client.
4828205eeb1aSlm66018 	 */
4829205eeb1aSlm66018 	ddi_taskq_wait(vd->completionq);
48301ae08745Sheppo 
4831d10e4ef2Snarayan 	/* Arrange to reset the connection for nack'ed or failed messages */
48323af08d82Slm66018 	if ((status != 0) || reset_ldc) {
48333af08d82Slm66018 		PR0("initiating %s reset",
48343af08d82Slm66018 		    (reset_ldc) ? "full" : "soft");
4835d10e4ef2Snarayan 		vd_need_reset(vd, reset_ldc);
48363af08d82Slm66018 	}
4837d10e4ef2Snarayan 
4838d10e4ef2Snarayan 	return (status);
4839d10e4ef2Snarayan }
4840d10e4ef2Snarayan 
4841d10e4ef2Snarayan static boolean_t
4842d10e4ef2Snarayan vd_enabled(vd_t *vd)
4843d10e4ef2Snarayan {
4844d10e4ef2Snarayan 	boolean_t	enabled;
4845d10e4ef2Snarayan 
4846d10e4ef2Snarayan 	mutex_enter(&vd->lock);
4847d10e4ef2Snarayan 	enabled = vd->enabled;
4848d10e4ef2Snarayan 	mutex_exit(&vd->lock);
4849d10e4ef2Snarayan 	return (enabled);
48501ae08745Sheppo }
48511ae08745Sheppo 
48521ae08745Sheppo static void
48530a55fbb7Slm66018 vd_recv_msg(void *arg)
48541ae08745Sheppo {
48551ae08745Sheppo 	vd_t	*vd = (vd_t *)arg;
48563af08d82Slm66018 	int	rv = 0, status = 0;
48571ae08745Sheppo 
48581ae08745Sheppo 	ASSERT(vd != NULL);
48593af08d82Slm66018 
4860d10e4ef2Snarayan 	PR2("New task to receive incoming message(s)");
48613af08d82Slm66018 
48623af08d82Slm66018 
4863d10e4ef2Snarayan 	while (vd_enabled(vd) && status == 0) {
4864d10e4ef2Snarayan 		size_t		msglen, msgsize;
48653af08d82Slm66018 		ldc_status_t	lstatus;
4866d10e4ef2Snarayan 
48670a55fbb7Slm66018 		/*
4868d10e4ef2Snarayan 		 * Receive and process a message
48690a55fbb7Slm66018 		 */
4870d10e4ef2Snarayan 		vd_reset_if_needed(vd);	/* can change vd->max_msglen */
48713af08d82Slm66018 
48723af08d82Slm66018 		/*
48733af08d82Slm66018 		 * check if channel is UP - else break out of loop
48743af08d82Slm66018 		 */
48753af08d82Slm66018 		status = ldc_status(vd->ldc_handle, &lstatus);
48763af08d82Slm66018 		if (lstatus != LDC_UP) {
48773af08d82Slm66018 			PR0("channel not up (status=%d), exiting recv loop\n",
48783af08d82Slm66018 			    lstatus);
48793af08d82Slm66018 			break;
48803af08d82Slm66018 		}
48813af08d82Slm66018 
48823af08d82Slm66018 		ASSERT(vd->max_msglen != 0);
48833af08d82Slm66018 
4884d10e4ef2Snarayan 		msgsize = vd->max_msglen; /* stable copy for alloc/free */
48853af08d82Slm66018 		msglen	= msgsize;	  /* actual len after recv_msg() */
48863af08d82Slm66018 
48873af08d82Slm66018 		status = recv_msg(vd->ldc_handle, vd->vio_msgp, &msglen);
48883af08d82Slm66018 		switch (status) {
48893af08d82Slm66018 		case 0:
48903af08d82Slm66018 			rv = vd_process_msg(vd, (vio_msg_t *)vd->vio_msgp,
48913af08d82Slm66018 			    msglen);
48923af08d82Slm66018 			/* check if max_msglen changed */
48933af08d82Slm66018 			if (msgsize != vd->max_msglen) {
48943af08d82Slm66018 				PR0("max_msglen changed 0x%lx to 0x%lx bytes\n",
48953af08d82Slm66018 				    msgsize, vd->max_msglen);
48963af08d82Slm66018 				kmem_free(vd->vio_msgp, msgsize);
48973af08d82Slm66018 				vd->vio_msgp =
48983af08d82Slm66018 				    kmem_alloc(vd->max_msglen, KM_SLEEP);
48993af08d82Slm66018 			}
49003af08d82Slm66018 			if (rv == EINPROGRESS)
49013af08d82Slm66018 				continue;
49023af08d82Slm66018 			break;
49033af08d82Slm66018 
49043af08d82Slm66018 		case ENOMSG:
49053af08d82Slm66018 			break;
49063af08d82Slm66018 
49073af08d82Slm66018 		case ECONNRESET:
49083af08d82Slm66018 			PR0("initiating soft reset (ECONNRESET)\n");
49093af08d82Slm66018 			vd_need_reset(vd, B_FALSE);
49103af08d82Slm66018 			status = 0;
49113af08d82Slm66018 			break;
49123af08d82Slm66018 
49133af08d82Slm66018 		default:
4914d10e4ef2Snarayan 			/* Probably an LDC failure; arrange to reset it */
49153af08d82Slm66018 			PR0("initiating full reset (status=0x%x)", status);
4916d10e4ef2Snarayan 			vd_need_reset(vd, B_TRUE);
49173af08d82Slm66018 			break;
49180a55fbb7Slm66018 		}
49191ae08745Sheppo 	}
49203af08d82Slm66018 
4921d10e4ef2Snarayan 	PR2("Task finished");
49220a55fbb7Slm66018 }
49230a55fbb7Slm66018 
49240a55fbb7Slm66018 static uint_t
49251ae08745Sheppo vd_handle_ldc_events(uint64_t event, caddr_t arg)
49261ae08745Sheppo {
49271ae08745Sheppo 	vd_t	*vd = (vd_t *)(void *)arg;
49283af08d82Slm66018 	int	status;
49291ae08745Sheppo 
49301ae08745Sheppo 	ASSERT(vd != NULL);
4931d10e4ef2Snarayan 
4932d10e4ef2Snarayan 	if (!vd_enabled(vd))
4933d10e4ef2Snarayan 		return (LDC_SUCCESS);
4934d10e4ef2Snarayan 
49353af08d82Slm66018 	if (event & LDC_EVT_DOWN) {
493634683adeSsg70180 		PR0("LDC_EVT_DOWN: LDC channel went down");
49373af08d82Slm66018 
49383af08d82Slm66018 		vd_need_reset(vd, B_TRUE);
49393af08d82Slm66018 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
49403af08d82Slm66018 		    DDI_SLEEP);
49413af08d82Slm66018 		if (status == DDI_FAILURE) {
49423af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
49433af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
49443af08d82Slm66018 		}
49453af08d82Slm66018 	}
49463af08d82Slm66018 
4947d10e4ef2Snarayan 	if (event & LDC_EVT_RESET) {
49483af08d82Slm66018 		PR0("LDC_EVT_RESET: LDC channel was reset");
49493af08d82Slm66018 
49503af08d82Slm66018 		if (vd->state != VD_STATE_INIT) {
49513af08d82Slm66018 			PR0("scheduling full reset");
49523af08d82Slm66018 			vd_need_reset(vd, B_FALSE);
49533af08d82Slm66018 			status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
49543af08d82Slm66018 			    vd, DDI_SLEEP);
49553af08d82Slm66018 			if (status == DDI_FAILURE) {
49563af08d82Slm66018 				PR0("cannot schedule task to recv msg\n");
49573af08d82Slm66018 				vd_need_reset(vd, B_TRUE);
49583af08d82Slm66018 			}
49593af08d82Slm66018 
49603af08d82Slm66018 		} else {
49613af08d82Slm66018 			PR0("channel already reset, ignoring...\n");
49623af08d82Slm66018 			PR0("doing ldc up...\n");
49633af08d82Slm66018 			(void) ldc_up(vd->ldc_handle);
49643af08d82Slm66018 		}
49653af08d82Slm66018 
4966d10e4ef2Snarayan 		return (LDC_SUCCESS);
4967d10e4ef2Snarayan 	}
4968d10e4ef2Snarayan 
4969d10e4ef2Snarayan 	if (event & LDC_EVT_UP) {
49703af08d82Slm66018 		PR0("EVT_UP: LDC is up\nResetting client connection state");
49713af08d82Slm66018 		PR0("initiating soft reset");
4972d10e4ef2Snarayan 		vd_need_reset(vd, B_FALSE);
49733af08d82Slm66018 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
49743af08d82Slm66018 		    vd, DDI_SLEEP);
49753af08d82Slm66018 		if (status == DDI_FAILURE) {
49763af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
49773af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
49783af08d82Slm66018 			return (LDC_SUCCESS);
49793af08d82Slm66018 		}
4980d10e4ef2Snarayan 	}
4981d10e4ef2Snarayan 
4982d10e4ef2Snarayan 	if (event & LDC_EVT_READ) {
4983d10e4ef2Snarayan 		int	status;
4984d10e4ef2Snarayan 
4985d10e4ef2Snarayan 		PR1("New data available");
4986d10e4ef2Snarayan 		/* Queue a task to receive the new data */
4987d10e4ef2Snarayan 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
4988d10e4ef2Snarayan 		    DDI_SLEEP);
49893af08d82Slm66018 
49903af08d82Slm66018 		if (status == DDI_FAILURE) {
49913af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
49923af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
49933af08d82Slm66018 		}
4994d10e4ef2Snarayan 	}
4995d10e4ef2Snarayan 
4996d10e4ef2Snarayan 	return (LDC_SUCCESS);
49971ae08745Sheppo }
49981ae08745Sheppo 
49991ae08745Sheppo static uint_t
50001ae08745Sheppo vds_check_for_vd(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
50011ae08745Sheppo {
50021ae08745Sheppo 	_NOTE(ARGUNUSED(key, val))
50031ae08745Sheppo 	(*((uint_t *)arg))++;
50041ae08745Sheppo 	return (MH_WALK_TERMINATE);
50051ae08745Sheppo }
50061ae08745Sheppo 
50071ae08745Sheppo 
50081ae08745Sheppo static int
50091ae08745Sheppo vds_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
50101ae08745Sheppo {
50111ae08745Sheppo 	uint_t	vd_present = 0;
50121ae08745Sheppo 	minor_t	instance;
50131ae08745Sheppo 	vds_t	*vds;
50141ae08745Sheppo 
50151ae08745Sheppo 
50161ae08745Sheppo 	switch (cmd) {
50171ae08745Sheppo 	case DDI_DETACH:
50181ae08745Sheppo 		/* the real work happens below */
50191ae08745Sheppo 		break;
50201ae08745Sheppo 	case DDI_SUSPEND:
5021d10e4ef2Snarayan 		PR0("No action required for DDI_SUSPEND");
50221ae08745Sheppo 		return (DDI_SUCCESS);
50231ae08745Sheppo 	default:
50243af08d82Slm66018 		PR0("Unrecognized \"cmd\"");
50251ae08745Sheppo 		return (DDI_FAILURE);
50261ae08745Sheppo 	}
50271ae08745Sheppo 
50281ae08745Sheppo 	ASSERT(cmd == DDI_DETACH);
50291ae08745Sheppo 	instance = ddi_get_instance(dip);
50301ae08745Sheppo 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
50313af08d82Slm66018 		PR0("Could not get state for instance %u", instance);
50321ae08745Sheppo 		ddi_soft_state_free(vds_state, instance);
50331ae08745Sheppo 		return (DDI_FAILURE);
50341ae08745Sheppo 	}
50351ae08745Sheppo 
50361ae08745Sheppo 	/* Do no detach when serving any vdisks */
50371ae08745Sheppo 	mod_hash_walk(vds->vd_table, vds_check_for_vd, &vd_present);
50381ae08745Sheppo 	if (vd_present) {
50391ae08745Sheppo 		PR0("Not detaching because serving vdisks");
50401ae08745Sheppo 		return (DDI_FAILURE);
50411ae08745Sheppo 	}
50421ae08745Sheppo 
50431ae08745Sheppo 	PR0("Detaching");
5044445b4c2eSsb155480 	if (vds->initialized & VDS_MDEG) {
50451ae08745Sheppo 		(void) mdeg_unregister(vds->mdeg);
5046445b4c2eSsb155480 		kmem_free(vds->ispecp->specp, sizeof (vds_prop_template));
5047445b4c2eSsb155480 		kmem_free(vds->ispecp, sizeof (mdeg_node_spec_t));
5048445b4c2eSsb155480 		vds->ispecp = NULL;
5049445b4c2eSsb155480 		vds->mdeg = NULL;
5050445b4c2eSsb155480 	}
5051445b4c2eSsb155480 
50528fce2fd6Sachartre 	vds_driver_types_free(vds);
50538fce2fd6Sachartre 
50541ae08745Sheppo 	if (vds->initialized & VDS_LDI)
50551ae08745Sheppo 		(void) ldi_ident_release(vds->ldi_ident);
50561ae08745Sheppo 	mod_hash_destroy_hash(vds->vd_table);
50571ae08745Sheppo 	ddi_soft_state_free(vds_state, instance);
50581ae08745Sheppo 	return (DDI_SUCCESS);
50591ae08745Sheppo }
50601ae08745Sheppo 
506117cadca8Slm66018 /*
506217cadca8Slm66018  * Description:
506317cadca8Slm66018  *	This function checks to see if the file being used as a
506417cadca8Slm66018  *	virtual disk is an ISO image. An ISO image is a special
506517cadca8Slm66018  *	case which can be booted/installed from like a CD/DVD
506617cadca8Slm66018  *
506717cadca8Slm66018  * Parameters:
506817cadca8Slm66018  *	vd		- disk on which the operation is performed.
506917cadca8Slm66018  *
507017cadca8Slm66018  * Return Code:
507117cadca8Slm66018  *	B_TRUE		- The file is an ISO 9660 compliant image
507217cadca8Slm66018  *	B_FALSE		- just a regular disk image file
507317cadca8Slm66018  */
507417cadca8Slm66018 static boolean_t
507517cadca8Slm66018 vd_file_is_iso_image(vd_t *vd)
507617cadca8Slm66018 {
507717cadca8Slm66018 	char	iso_buf[ISO_SECTOR_SIZE];
507817cadca8Slm66018 	int	i, rv;
507917cadca8Slm66018 	uint_t	sec;
508017cadca8Slm66018 
508117cadca8Slm66018 	ASSERT(vd->file);
508217cadca8Slm66018 
508317cadca8Slm66018 	/*
508417cadca8Slm66018 	 * If we have already discovered and saved this info we can
508517cadca8Slm66018 	 * short-circuit the check and avoid reading the file.
508617cadca8Slm66018 	 */
508717cadca8Slm66018 	if (vd->vdisk_media == VD_MEDIA_DVD || vd->vdisk_media == VD_MEDIA_CD)
508817cadca8Slm66018 		return (B_TRUE);
508917cadca8Slm66018 
509017cadca8Slm66018 	/*
509117cadca8Slm66018 	 * We wish to read the sector that should contain the 2nd ISO volume
509217cadca8Slm66018 	 * descriptor. The second field in this descriptor is called the
509317cadca8Slm66018 	 * Standard Identifier and is set to CD001 for a CD-ROM compliant
509417cadca8Slm66018 	 * to the ISO 9660 standard.
509517cadca8Slm66018 	 */
509617cadca8Slm66018 	sec = (ISO_VOLDESC_SEC * ISO_SECTOR_SIZE) / vd->vdisk_block_size;
509717cadca8Slm66018 	rv = vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)iso_buf,
509817cadca8Slm66018 	    sec, ISO_SECTOR_SIZE);
509917cadca8Slm66018 
510017cadca8Slm66018 	if (rv < 0)
510117cadca8Slm66018 		return (B_FALSE);
510217cadca8Slm66018 
510317cadca8Slm66018 	for (i = 0; i < ISO_ID_STRLEN; i++) {
510417cadca8Slm66018 		if (ISO_STD_ID(iso_buf)[i] != ISO_ID_STRING[i])
510517cadca8Slm66018 			return (B_FALSE);
510617cadca8Slm66018 	}
510717cadca8Slm66018 
510817cadca8Slm66018 	return (B_TRUE);
510917cadca8Slm66018 }
511017cadca8Slm66018 
511117cadca8Slm66018 /*
511217cadca8Slm66018  * Description:
511317cadca8Slm66018  *	This function checks to see if the virtual device is an ATAPI
511417cadca8Slm66018  *	device. ATAPI devices use Group 1 Read/Write commands, so
511517cadca8Slm66018  *	any USCSI calls vds makes need to take this into account.
511617cadca8Slm66018  *
511717cadca8Slm66018  * Parameters:
511817cadca8Slm66018  *	vd		- disk on which the operation is performed.
511917cadca8Slm66018  *
512017cadca8Slm66018  * Return Code:
512117cadca8Slm66018  *	B_TRUE		- The virtual disk is backed by an ATAPI device
512217cadca8Slm66018  *	B_FALSE		- not an ATAPI device (presumably SCSI)
512317cadca8Slm66018  */
512417cadca8Slm66018 static boolean_t
512517cadca8Slm66018 vd_is_atapi_device(vd_t *vd)
512617cadca8Slm66018 {
512717cadca8Slm66018 	boolean_t	is_atapi = B_FALSE;
512817cadca8Slm66018 	char		*variantp;
512917cadca8Slm66018 	int		rv;
513017cadca8Slm66018 
513117cadca8Slm66018 	ASSERT(vd->ldi_handle[0] != NULL);
513217cadca8Slm66018 	ASSERT(!vd->file);
513317cadca8Slm66018 
513417cadca8Slm66018 	rv = ldi_prop_lookup_string(vd->ldi_handle[0],
513517cadca8Slm66018 	    (LDI_DEV_T_ANY | DDI_PROP_DONTPASS), "variant", &variantp);
513617cadca8Slm66018 	if (rv == DDI_PROP_SUCCESS) {
513717cadca8Slm66018 		PR0("'variant' property exists for %s", vd->device_path);
513817cadca8Slm66018 		if (strcmp(variantp, "atapi") == 0)
513917cadca8Slm66018 			is_atapi = B_TRUE;
514017cadca8Slm66018 		ddi_prop_free(variantp);
514117cadca8Slm66018 	}
514217cadca8Slm66018 
514317cadca8Slm66018 	rv = ldi_prop_exists(vd->ldi_handle[0], LDI_DEV_T_ANY, "atapi");
514417cadca8Slm66018 	if (rv) {
514517cadca8Slm66018 		PR0("'atapi' property exists for %s", vd->device_path);
514617cadca8Slm66018 		is_atapi = B_TRUE;
514717cadca8Slm66018 	}
514817cadca8Slm66018 
514917cadca8Slm66018 	return (is_atapi);
515017cadca8Slm66018 }
515117cadca8Slm66018 
51521ae08745Sheppo static int
51532f5224aeSachartre vd_setup_full_disk(vd_t *vd)
51542f5224aeSachartre {
51552f5224aeSachartre 	int		status;
51562f5224aeSachartre 	major_t		major = getmajor(vd->dev[0]);
51572f5224aeSachartre 	minor_t		minor = getminor(vd->dev[0]) - VD_ENTIRE_DISK_SLICE;
51582f5224aeSachartre 
5159047ba61eSachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
5160047ba61eSachartre 
51612f5224aeSachartre 	vd->vdisk_block_size = DEV_BSIZE;
51622f5224aeSachartre 
5163*de3a5331SRamesh Chitrothu 	/* set the disk size, block size and the media type of the disk */
5164*de3a5331SRamesh Chitrothu 	status = vd_backend_check_size(vd);
51652f5224aeSachartre 
51662f5224aeSachartre 	if (status != 0) {
51672f5224aeSachartre 		if (!vd->scsi) {
51682f5224aeSachartre 			/* unexpected failure */
5169690555a1Sachartre 			PRN("ldi_ioctl(DKIOCGMEDIAINFO) returned errno %d",
51704bac2208Snarayan 			    status);
51710a55fbb7Slm66018 			return (status);
51720a55fbb7Slm66018 		}
51732f5224aeSachartre 
51742f5224aeSachartre 		/*
51752f5224aeSachartre 		 * The function can fail for SCSI disks which are present but
51762f5224aeSachartre 		 * reserved by another system. In that case, we don't know the
51772f5224aeSachartre 		 * size of the disk and the block size.
51782f5224aeSachartre 		 */
51792f5224aeSachartre 		vd->vdisk_size = VD_SIZE_UNKNOWN;
51802f5224aeSachartre 		vd->block_size = 0;
51812f5224aeSachartre 		vd->vdisk_media = VD_MEDIA_FIXED;
51822f5224aeSachartre 	}
51830a55fbb7Slm66018 
51840a55fbb7Slm66018 	/* Move dev number and LDI handle to entire-disk-slice array elements */
51850a55fbb7Slm66018 	vd->dev[VD_ENTIRE_DISK_SLICE]		= vd->dev[0];
51860a55fbb7Slm66018 	vd->dev[0]				= 0;
51870a55fbb7Slm66018 	vd->ldi_handle[VD_ENTIRE_DISK_SLICE]	= vd->ldi_handle[0];
51880a55fbb7Slm66018 	vd->ldi_handle[0]			= NULL;
51890a55fbb7Slm66018 
51900a55fbb7Slm66018 	/* Initialize device numbers for remaining slices and open them */
51910a55fbb7Slm66018 	for (int slice = 0; slice < vd->nslices; slice++) {
51920a55fbb7Slm66018 		/*
51930a55fbb7Slm66018 		 * Skip the entire-disk slice, as it's already open and its
51940a55fbb7Slm66018 		 * device known
51950a55fbb7Slm66018 		 */
51960a55fbb7Slm66018 		if (slice == VD_ENTIRE_DISK_SLICE)
51970a55fbb7Slm66018 			continue;
51980a55fbb7Slm66018 		ASSERT(vd->dev[slice] == 0);
51990a55fbb7Slm66018 		ASSERT(vd->ldi_handle[slice] == NULL);
52000a55fbb7Slm66018 
52010a55fbb7Slm66018 		/*
52020a55fbb7Slm66018 		 * Construct the device number for the current slice
52030a55fbb7Slm66018 		 */
52040a55fbb7Slm66018 		vd->dev[slice] = makedevice(major, (minor + slice));
52050a55fbb7Slm66018 
52060a55fbb7Slm66018 		/*
520734683adeSsg70180 		 * Open all slices of the disk to serve them to the client.
520834683adeSsg70180 		 * Slices are opened exclusively to prevent other threads or
520934683adeSsg70180 		 * processes in the service domain from performing I/O to
521034683adeSsg70180 		 * slices being accessed by a client.  Failure to open a slice
521134683adeSsg70180 		 * results in vds not serving this disk, as the client could
521234683adeSsg70180 		 * attempt (and should be able) to access any slice immediately.
521334683adeSsg70180 		 * Any slices successfully opened before a failure will get
521434683adeSsg70180 		 * closed by vds_destroy_vd() as a result of the error returned
521534683adeSsg70180 		 * by this function.
521634683adeSsg70180 		 *
521734683adeSsg70180 		 * We need to do the open with FNDELAY so that opening an empty
521834683adeSsg70180 		 * slice does not fail.
52190a55fbb7Slm66018 		 */
52200a55fbb7Slm66018 		PR0("Opening device major %u, minor %u = slice %u",
52210a55fbb7Slm66018 		    major, minor, slice);
5222047ba61eSachartre 
5223047ba61eSachartre 		/*
5224047ba61eSachartre 		 * Try to open the device. This can fail for example if we are
5225047ba61eSachartre 		 * opening an empty slice. So in case of a failure, we try the
5226047ba61eSachartre 		 * open again but this time with the FNDELAY flag.
5227047ba61eSachartre 		 */
5228047ba61eSachartre 		status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK,
5229047ba61eSachartre 		    vd->open_flags, kcred, &vd->ldi_handle[slice],
5230047ba61eSachartre 		    vd->vds->ldi_ident);
5231047ba61eSachartre 
5232047ba61eSachartre 		if (status != 0) {
5233047ba61eSachartre 			status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK,
5234047ba61eSachartre 			    vd->open_flags | FNDELAY, kcred,
5235047ba61eSachartre 			    &vd->ldi_handle[slice], vd->vds->ldi_ident);
5236047ba61eSachartre 		}
5237047ba61eSachartre 
5238047ba61eSachartre 		if (status != 0) {
5239690555a1Sachartre 			PRN("ldi_open_by_dev() returned errno %d "
52400a55fbb7Slm66018 			    "for slice %u", status, slice);
52410a55fbb7Slm66018 			/* vds_destroy_vd() will close any open slices */
5242690555a1Sachartre 			vd->ldi_handle[slice] = NULL;
52430a55fbb7Slm66018 			return (status);
52440a55fbb7Slm66018 		}
52450a55fbb7Slm66018 	}
52460a55fbb7Slm66018 
52470a55fbb7Slm66018 	return (0);
52480a55fbb7Slm66018 }
52490a55fbb7Slm66018 
5250edcc0754Sachartre /*
5251edcc0754Sachartre  * When a slice or a volume is exported as a single-slice disk, we want
5252edcc0754Sachartre  * the disk backend (i.e. the slice or volume) to be entirely mapped as
5253edcc0754Sachartre  * a slice without the addition of any metadata.
5254edcc0754Sachartre  *
5255edcc0754Sachartre  * So when exporting the disk as a VTOC disk, we fake a disk with the following
5256edcc0754Sachartre  * layout:
5257bae9e67eSachartre  *                flabel +--- flabel_limit
5258bae9e67eSachartre  *                 <->   V
5259bae9e67eSachartre  *                 0 1   C                          D  E
5260bae9e67eSachartre  *                 +-+---+--------------------------+--+
5261bae9e67eSachartre  *  virtual disk:  |L|XXX|           slice 0        |AA|
5262bae9e67eSachartre  *                 +-+---+--------------------------+--+
5263edcc0754Sachartre  *                  ^    :                          :
5264edcc0754Sachartre  *                  |    :                          :
5265edcc0754Sachartre  *      VTOC LABEL--+    :                          :
5266edcc0754Sachartre  *                       +--------------------------+
5267bae9e67eSachartre  *  disk backend:        |     slice/volume/file    |
5268edcc0754Sachartre  *                       +--------------------------+
5269edcc0754Sachartre  *                       0                          N
5270edcc0754Sachartre  *
5271bae9e67eSachartre  * N is the number of blocks in the slice/volume/file.
5272edcc0754Sachartre  *
5273bae9e67eSachartre  * We simulate a disk with N+M blocks, where M is the number of blocks
5274bae9e67eSachartre  * simluated at the beginning and at the end of the disk (blocks 0-C
5275bae9e67eSachartre  * and D-E).
5276edcc0754Sachartre  *
5277bae9e67eSachartre  * The first blocks (0 to C-1) are emulated and can not be changed. Blocks C
5278bae9e67eSachartre  * to D defines slice 0 and are mapped to the backend. Finally we emulate 2
5279bae9e67eSachartre  * alternate cylinders at the end of the disk (blocks D-E). In summary we have:
5280edcc0754Sachartre  *
5281bae9e67eSachartre  * - block 0 (L) returns a fake VTOC label
5282bae9e67eSachartre  * - blocks 1 to C-1 (X) are unused and return 0
5283bae9e67eSachartre  * - blocks C to D-1 are mapped to the exported slice or volume
5284bae9e67eSachartre  * - blocks D and E (A) are blocks defining alternate cylinders (2 cylinders)
5285bae9e67eSachartre  *
5286bae9e67eSachartre  * Note: because we define a fake disk geometry, it is possible that the length
5287bae9e67eSachartre  * of the backend is not a multiple of the size of cylinder, in that case the
5288bae9e67eSachartre  * very end of the backend will not map to any block of the virtual disk.
5289edcc0754Sachartre  */
52900a55fbb7Slm66018 static int
529178fcd0a1Sachartre vd_setup_partition_vtoc(vd_t *vd)
529278fcd0a1Sachartre {
529378fcd0a1Sachartre 	char *device_path = vd->device_path;
5294bae9e67eSachartre 	char unit;
5295bae9e67eSachartre 	size_t size, csize;
529678fcd0a1Sachartre 
529778fcd0a1Sachartre 	/* Initialize dk_geom structure for single-slice device */
529878fcd0a1Sachartre 	if (vd->dk_geom.dkg_nsect == 0) {
529978fcd0a1Sachartre 		PRN("%s geometry claims 0 sectors per track", device_path);
530078fcd0a1Sachartre 		return (EIO);
530178fcd0a1Sachartre 	}
530278fcd0a1Sachartre 	if (vd->dk_geom.dkg_nhead == 0) {
530378fcd0a1Sachartre 		PRN("%s geometry claims 0 heads", device_path);
530478fcd0a1Sachartre 		return (EIO);
530578fcd0a1Sachartre 	}
5306bae9e67eSachartre 
5307bae9e67eSachartre 	/* size of a cylinder in block */
5308bae9e67eSachartre 	csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
5309bae9e67eSachartre 
5310bae9e67eSachartre 	/*
5311bae9e67eSachartre 	 * Add extra cylinders: we emulate the first cylinder (which contains
5312bae9e67eSachartre 	 * the disk label).
5313bae9e67eSachartre 	 */
5314bae9e67eSachartre 	vd->dk_geom.dkg_ncyl = vd->vdisk_size / csize + 1;
5315bae9e67eSachartre 
5316bae9e67eSachartre 	/* we emulate 2 alternate cylinders */
5317bae9e67eSachartre 	vd->dk_geom.dkg_acyl = 2;
531878fcd0a1Sachartre 	vd->dk_geom.dkg_pcyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl;
531978fcd0a1Sachartre 
532078fcd0a1Sachartre 
532178fcd0a1Sachartre 	/* Initialize vtoc structure for single-slice device */
532278fcd0a1Sachartre 	bzero(vd->vtoc.v_part, sizeof (vd->vtoc.v_part));
532378fcd0a1Sachartre 	vd->vtoc.v_part[0].p_tag = V_UNASSIGNED;
532478fcd0a1Sachartre 	vd->vtoc.v_part[0].p_flag = 0;
5325bae9e67eSachartre 	/*
5326bae9e67eSachartre 	 * Partition 0 starts on cylinder 1 and its size has to be
5327bae9e67eSachartre 	 * a multiple of a number of cylinder.
5328bae9e67eSachartre 	 */
5329bae9e67eSachartre 	vd->vtoc.v_part[0].p_start = csize; /* start on cylinder 1 */
5330bae9e67eSachartre 	vd->vtoc.v_part[0].p_size = (vd->vdisk_size / csize) * csize;
533178fcd0a1Sachartre 
5332bae9e67eSachartre 	if (vd_slice_single_slice) {
5333bae9e67eSachartre 		vd->vtoc.v_nparts = 1;
5334bae9e67eSachartre 		bcopy(VD_ASCIILABEL, vd->vtoc.v_asciilabel,
5335bae9e67eSachartre 		    MIN(sizeof (VD_ASCIILABEL),
5336bae9e67eSachartre 		    sizeof (vd->vtoc.v_asciilabel)));
5337bae9e67eSachartre 		bcopy(VD_VOLUME_NAME, vd->vtoc.v_volume,
5338bae9e67eSachartre 		    MIN(sizeof (VD_VOLUME_NAME), sizeof (vd->vtoc.v_volume)));
5339bae9e67eSachartre 	} else {
5340bae9e67eSachartre 		/* adjust the number of slices */
5341bae9e67eSachartre 		vd->nslices = V_NUMPAR;
5342bae9e67eSachartre 		vd->vtoc.v_nparts = V_NUMPAR;
5343bae9e67eSachartre 
5344bae9e67eSachartre 		/* define slice 2 representing the entire disk */
5345bae9e67eSachartre 		vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_tag = V_BACKUP;
5346bae9e67eSachartre 		vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_flag = 0;
5347bae9e67eSachartre 		vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_start = 0;
5348bae9e67eSachartre 		vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_size =
5349bae9e67eSachartre 		    vd->dk_geom.dkg_ncyl * csize;
5350bae9e67eSachartre 
5351bae9e67eSachartre 		vd_get_readable_size(vd->vdisk_size * vd->vdisk_block_size,
5352bae9e67eSachartre 		    &size, &unit);
5353bae9e67eSachartre 
5354bae9e67eSachartre 		/*
5355bae9e67eSachartre 		 * Set some attributes of the geometry to what format(1m) uses
5356bae9e67eSachartre 		 * so that writing a default label using format(1m) does not
5357bae9e67eSachartre 		 * produce any error.
5358bae9e67eSachartre 		 */
5359bae9e67eSachartre 		vd->dk_geom.dkg_bcyl = 0;
5360bae9e67eSachartre 		vd->dk_geom.dkg_intrlv = 1;
5361bae9e67eSachartre 		vd->dk_geom.dkg_write_reinstruct = 0;
5362bae9e67eSachartre 		vd->dk_geom.dkg_read_reinstruct = 0;
5363bae9e67eSachartre 
5364bae9e67eSachartre 		/*
5365bae9e67eSachartre 		 * We must have a correct label name otherwise format(1m) will
5366bae9e67eSachartre 		 * not recognized the disk as labeled.
5367bae9e67eSachartre 		 */
5368bae9e67eSachartre 		(void) snprintf(vd->vtoc.v_asciilabel, LEN_DKL_ASCII,
5369bae9e67eSachartre 		    "SUN-DiskSlice-%ld%cB cyl %d alt %d hd %d sec %d",
5370bae9e67eSachartre 		    size, unit,
5371bae9e67eSachartre 		    vd->dk_geom.dkg_ncyl, vd->dk_geom.dkg_acyl,
5372bae9e67eSachartre 		    vd->dk_geom.dkg_nhead, vd->dk_geom.dkg_nsect);
5373bae9e67eSachartre 		bzero(vd->vtoc.v_volume, sizeof (vd->vtoc.v_volume));
5374bae9e67eSachartre 
5375bae9e67eSachartre 		/* create a fake label from the vtoc and geometry */
5376bae9e67eSachartre 		vd->flabel_limit = csize;
5377bae9e67eSachartre 		vd->flabel_size = VD_LABEL_VTOC_SIZE;
5378bae9e67eSachartre 		vd->flabel = kmem_zalloc(vd->flabel_size, KM_SLEEP);
5379bae9e67eSachartre 		vd_vtocgeom_to_label(&vd->vtoc, &vd->dk_geom,
5380bae9e67eSachartre 		    VD_LABEL_VTOC(vd));
5381bae9e67eSachartre 	}
5382bae9e67eSachartre 
5383bae9e67eSachartre 	/* adjust the vdisk_size, we emulate 3 cylinders */
5384bae9e67eSachartre 	vd->vdisk_size += csize * 3;
5385edcc0754Sachartre 
538678fcd0a1Sachartre 	return (0);
538778fcd0a1Sachartre }
538878fcd0a1Sachartre 
5389edcc0754Sachartre /*
5390edcc0754Sachartre  * When a slice, volume or file is exported as a single-slice disk, we want
5391edcc0754Sachartre  * the disk backend (i.e. the slice, volume or file) to be entirely mapped
5392edcc0754Sachartre  * as a slice without the addition of any metadata.
5393edcc0754Sachartre  *
5394edcc0754Sachartre  * So when exporting the disk as an EFI disk, we fake a disk with the following
5395edcc0754Sachartre  * layout:
5396edcc0754Sachartre  *
5397bae9e67eSachartre  *                  flabel        +--- flabel_limit
5398bae9e67eSachartre  *                 <------>       v
5399bae9e67eSachartre  *                 0 1 2  L      34                        34+N      P
5400bae9e67eSachartre  *                 +-+-+--+-------+--------------------------+-------+
5401bae9e67eSachartre  *  virtual disk:  |X|T|EE|XXXXXXX|           slice 0        |RRRRRRR|
5402bae9e67eSachartre  *                 +-+-+--+-------+--------------------------+-------+
5403edcc0754Sachartre  *                    ^ ^         :                          :
5404edcc0754Sachartre  *                    | |         :                          :
5405edcc0754Sachartre  *                GPT-+ +-GPE     :                          :
5406edcc0754Sachartre  *                                +--------------------------+
5407edcc0754Sachartre  *  disk backend:                 |     slice/volume/file    |
5408edcc0754Sachartre  *                                +--------------------------+
5409edcc0754Sachartre  *                                0                          N
5410edcc0754Sachartre  *
5411edcc0754Sachartre  * N is the number of blocks in the slice/volume/file.
5412edcc0754Sachartre  *
5413bae9e67eSachartre  * We simulate a disk with N+M blocks, where M is the number of blocks
5414bae9e67eSachartre  * simluated at the beginning and at the end of the disk (blocks 0-34
5415bae9e67eSachartre  * and 34+N-P).
5416edcc0754Sachartre  *
5417bae9e67eSachartre  * The first 34 blocks (0 to 33) are emulated and can not be changed. Blocks 34
5418bae9e67eSachartre  * to 34+N defines slice 0 and are mapped to the exported backend, and we
5419bae9e67eSachartre  * emulate some blocks at the end of the disk (blocks 34+N to P) as a the EFI
5420bae9e67eSachartre  * reserved partition.
5421bae9e67eSachartre  *
5422bae9e67eSachartre  * - block 0 (X) is unused and return 0
5423edcc0754Sachartre  * - block 1 (T) returns a fake EFI GPT (via DKIOCGETEFI)
5424bae9e67eSachartre  * - blocks 2 to L-1 (E) defines a fake EFI GPE (via DKIOCGETEFI)
5425bae9e67eSachartre  * - blocks L to 33 (X) are unused and return 0
5426bae9e67eSachartre  * - blocks 34 to 34+N are mapped to the exported slice, volume or file
5427bae9e67eSachartre  * - blocks 34+N+1 to P define a fake reserved partition and backup label, it
5428bae9e67eSachartre  *   returns 0
5429edcc0754Sachartre  *
5430bae9e67eSachartre  * Note: if the backend size is not a multiple of the vdisk block size
5431bae9e67eSachartre  * (DEV_BSIZE = 512 byte) then the very end of the backend will not map to
5432bae9e67eSachartre  * any block of the virtual disk.
5433edcc0754Sachartre  */
543478fcd0a1Sachartre static int
54354bac2208Snarayan vd_setup_partition_efi(vd_t *vd)
54364bac2208Snarayan {
54374bac2208Snarayan 	efi_gpt_t *gpt;
54384bac2208Snarayan 	efi_gpe_t *gpe;
5439edcc0754Sachartre 	struct uuid uuid = EFI_USR;
5440bae9e67eSachartre 	struct uuid efi_reserved = EFI_RESERVED;
54414bac2208Snarayan 	uint32_t crc;
5442bae9e67eSachartre 	uint64_t s0_start, s0_end;
54434bac2208Snarayan 
5444bae9e67eSachartre 	vd->flabel_limit = 34;
5445bae9e67eSachartre 	vd->flabel_size = VD_LABEL_EFI_SIZE;
5446bae9e67eSachartre 	vd->flabel = kmem_zalloc(vd->flabel_size, KM_SLEEP);
5447bae9e67eSachartre 	gpt = VD_LABEL_EFI_GPT(vd);
5448bae9e67eSachartre 	gpe = VD_LABEL_EFI_GPE(vd);
5449edcc0754Sachartre 
5450edcc0754Sachartre 	/* adjust the vdisk_size, we emulate the first 34 blocks */
5451edcc0754Sachartre 	vd->vdisk_size += 34;
5452bae9e67eSachartre 	s0_start = 34;
5453bae9e67eSachartre 	s0_end = vd->vdisk_size - 1;
54544bac2208Snarayan 
54554bac2208Snarayan 	gpt->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
54564bac2208Snarayan 	gpt->efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
54574bac2208Snarayan 	gpt->efi_gpt_HeaderSize = LE_32(sizeof (efi_gpt_t));
5458edcc0754Sachartre 	gpt->efi_gpt_FirstUsableLBA = LE_64(34ULL);
5459edcc0754Sachartre 	gpt->efi_gpt_PartitionEntryLBA = LE_64(2ULL);
54604bac2208Snarayan 	gpt->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (efi_gpe_t));
54614bac2208Snarayan 
5462bae9e67eSachartre 	UUID_LE_CONVERT(gpe[0].efi_gpe_PartitionTypeGUID, uuid);
5463bae9e67eSachartre 	gpe[0].efi_gpe_StartingLBA = LE_64(s0_start);
5464bae9e67eSachartre 	gpe[0].efi_gpe_EndingLBA = LE_64(s0_end);
54654bac2208Snarayan 
5466bae9e67eSachartre 	if (vd_slice_single_slice) {
5467bae9e67eSachartre 		gpt->efi_gpt_NumberOfPartitionEntries = LE_32(1);
5468bae9e67eSachartre 	} else {
5469bae9e67eSachartre 		/* adjust the number of slices */
5470bae9e67eSachartre 		gpt->efi_gpt_NumberOfPartitionEntries = LE_32(VD_MAXPART);
5471bae9e67eSachartre 		vd->nslices = V_NUMPAR;
5472bae9e67eSachartre 
5473bae9e67eSachartre 		/* define a fake reserved partition */
5474bae9e67eSachartre 		UUID_LE_CONVERT(gpe[VD_MAXPART - 1].efi_gpe_PartitionTypeGUID,
5475bae9e67eSachartre 		    efi_reserved);
5476bae9e67eSachartre 		gpe[VD_MAXPART - 1].efi_gpe_StartingLBA =
5477bae9e67eSachartre 		    LE_64(s0_end + 1);
5478bae9e67eSachartre 		gpe[VD_MAXPART - 1].efi_gpe_EndingLBA =
5479bae9e67eSachartre 		    LE_64(s0_end + EFI_MIN_RESV_SIZE);
5480bae9e67eSachartre 
5481bae9e67eSachartre 		/* adjust the vdisk_size to include the reserved slice */
5482bae9e67eSachartre 		vd->vdisk_size += EFI_MIN_RESV_SIZE;
5483bae9e67eSachartre 	}
5484bae9e67eSachartre 
5485bae9e67eSachartre 	gpt->efi_gpt_LastUsableLBA = LE_64(vd->vdisk_size - 1);
5486bae9e67eSachartre 
5487bae9e67eSachartre 	/* adjust the vdisk size for the backup GPT and GPE */
5488bae9e67eSachartre 	vd->vdisk_size += 33;
5489bae9e67eSachartre 
5490bae9e67eSachartre 	CRC32(crc, gpe, sizeof (efi_gpe_t) * VD_MAXPART, -1U, crc32_table);
54914bac2208Snarayan 	gpt->efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
54924bac2208Snarayan 
54934bac2208Snarayan 	CRC32(crc, gpt, sizeof (efi_gpt_t), -1U, crc32_table);
54944bac2208Snarayan 	gpt->efi_gpt_HeaderCRC32 = LE_32(~crc);
54954bac2208Snarayan 
54964bac2208Snarayan 	return (0);
54974bac2208Snarayan }
54984bac2208Snarayan 
5499047ba61eSachartre /*
5500047ba61eSachartre  * Setup for a virtual disk whose backend is a file (exported as a single slice
55018fce2fd6Sachartre  * or as a full disk) or a volume device (for example a ZFS, SVM or VxVM volume)
5502047ba61eSachartre  * exported as a full disk. In these cases, the backend is accessed using the
5503047ba61eSachartre  * vnode interface.
5504047ba61eSachartre  */
55054bac2208Snarayan static int
5506047ba61eSachartre vd_setup_backend_vnode(vd_t *vd)
55073c96341aSnarayan {
550878fcd0a1Sachartre 	int 		rval, status;
55093c96341aSnarayan 	vattr_t		vattr;
55103c96341aSnarayan 	dev_t		dev;
55113c96341aSnarayan 	char		*file_path = vd->device_path;
55123c96341aSnarayan 	ldi_handle_t	lhandle;
55133c96341aSnarayan 	struct dk_cinfo	dk_cinfo;
5514bae9e67eSachartre 	struct dk_label label;
55153c96341aSnarayan 
5516047ba61eSachartre 	if ((status = vn_open(file_path, UIO_SYSSPACE, vd->open_flags | FOFFMAX,
55173c96341aSnarayan 	    0, &vd->file_vnode, 0, 0)) != 0) {
5518690555a1Sachartre 		PRN("vn_open(%s) = errno %d", file_path, status);
55193c96341aSnarayan 		return (status);
55203c96341aSnarayan 	}
55213c96341aSnarayan 
5522690555a1Sachartre 	/*
5523690555a1Sachartre 	 * We set vd->file now so that vds_destroy_vd will take care of
5524690555a1Sachartre 	 * closing the file and releasing the vnode in case of an error.
5525690555a1Sachartre 	 */
5526690555a1Sachartre 	vd->file = B_TRUE;
5527690555a1Sachartre 
55283c96341aSnarayan 	vattr.va_mask = AT_SIZE;
5529da6c28aaSamw 	if ((status = VOP_GETATTR(vd->file_vnode, &vattr, 0, kcred, NULL))
5530da6c28aaSamw 	    != 0) {
5531690555a1Sachartre 		PRN("VOP_GETATTR(%s) = errno %d", file_path, status);
55323c96341aSnarayan 		return (EIO);
55333c96341aSnarayan 	}
55343c96341aSnarayan 
55353c96341aSnarayan 	vd->file_size = vattr.va_size;
55363c96341aSnarayan 	/* size should be at least sizeof(dk_label) */
55373c96341aSnarayan 	if (vd->file_size < sizeof (struct dk_label)) {
55383c96341aSnarayan 		PRN("Size of file has to be at least %ld bytes",
55393c96341aSnarayan 		    sizeof (struct dk_label));
55403c96341aSnarayan 		return (EIO);
55413c96341aSnarayan 	}
55423c96341aSnarayan 
5543690555a1Sachartre 	if (vd->file_vnode->v_flag & VNOMAP) {
5544690555a1Sachartre 		PRN("File %s cannot be mapped", file_path);
55453c96341aSnarayan 		return (EIO);
55463c96341aSnarayan 	}
55473c96341aSnarayan 
55483c96341aSnarayan 	/* sector size = block size = DEV_BSIZE */
554917cadca8Slm66018 	vd->block_size = DEV_BSIZE;
555017cadca8Slm66018 	vd->vdisk_block_size = DEV_BSIZE;
555187a7269eSachartre 	vd->vdisk_size = vd->file_size / DEV_BSIZE;
55523c96341aSnarayan 	vd->max_xfer_sz = maxphys / DEV_BSIZE; /* default transfer size */
55533c96341aSnarayan 
5554047ba61eSachartre 	/*
5555047ba61eSachartre 	 * Get max_xfer_sz from the device where the file is or from the device
55568fce2fd6Sachartre 	 * itself if we have a volume device.
5557047ba61eSachartre 	 */
55588fce2fd6Sachartre 	if (vd->volume) {
5559047ba61eSachartre 		status = ldi_open_by_name(file_path, FREAD, kcred, &lhandle,
5560047ba61eSachartre 		    vd->vds->ldi_ident);
5561047ba61eSachartre 	} else {
55623c96341aSnarayan 		dev = vd->file_vnode->v_vfsp->vfs_dev;
5563f745d6a3Sachartre 		PR0("underlying device of %s = (%d, %d)\n", file_path,
5564f745d6a3Sachartre 		    getmajor(dev), getminor(dev));
55653c96341aSnarayan 
5566047ba61eSachartre 		status = ldi_open_by_dev(&dev, OTYP_BLK, FREAD, kcred, &lhandle,
5567047ba61eSachartre 		    vd->vds->ldi_ident);
5568047ba61eSachartre 	}
5569047ba61eSachartre 
5570047ba61eSachartre 	if (status != 0) {
5571f745d6a3Sachartre 		PR0("ldi_open() returned errno %d for underlying device",
5572f745d6a3Sachartre 		    status);
55733c96341aSnarayan 	} else {
55743c96341aSnarayan 		if ((status = ldi_ioctl(lhandle, DKIOCINFO,
5575047ba61eSachartre 		    (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred,
55763c96341aSnarayan 		    &rval)) != 0) {
5577f745d6a3Sachartre 			PR0("ldi_ioctl(DKIOCINFO) returned errno %d for "
5578f745d6a3Sachartre 			    "underlying device", status);
55793c96341aSnarayan 		} else {
55803c96341aSnarayan 			/*
55813c96341aSnarayan 			 * Store the device's max transfer size for
55823c96341aSnarayan 			 * return to the client
55833c96341aSnarayan 			 */
55843c96341aSnarayan 			vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
55853c96341aSnarayan 		}
55863c96341aSnarayan 
5587f745d6a3Sachartre 		PR0("close the underlying device");
55883c96341aSnarayan 		(void) ldi_close(lhandle, FREAD, kcred);
55893c96341aSnarayan 	}
55903c96341aSnarayan 
5591f745d6a3Sachartre 	if (vd->volume) {
5592f745d6a3Sachartre 		PR0("using volume %s, max_xfer = %u blks", file_path,
5593f745d6a3Sachartre 		    vd->max_xfer_sz);
5594f745d6a3Sachartre 	} else {
5595f745d6a3Sachartre 		PR0("using file %s on device (%d, %d), max_xfer = %u blks",
5596f745d6a3Sachartre 		    file_path, getmajor(dev), getminor(dev), vd->max_xfer_sz);
5597f745d6a3Sachartre 	}
55983c96341aSnarayan 
5599edcc0754Sachartre 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
56008fce2fd6Sachartre 		ASSERT(!vd->volume);
5601bae9e67eSachartre 		vd->vdisk_media = VD_MEDIA_FIXED;
5602bae9e67eSachartre 		vd->vdisk_label = (vd_slice_label == VD_DISK_LABEL_UNK)?
5603bae9e67eSachartre 		    vd_file_slice_label : vd_slice_label;
5604bae9e67eSachartre 		if (vd->vdisk_label == VD_DISK_LABEL_EFI ||
5605bae9e67eSachartre 		    vd->file_size >= ONE_TERABYTE) {
5606edcc0754Sachartre 			status = vd_setup_partition_efi(vd);
5607bae9e67eSachartre 		} else {
5608bae9e67eSachartre 			/*
5609bae9e67eSachartre 			 * We build a default label to get a geometry for
5610bae9e67eSachartre 			 * the vdisk. Then the partition setup function will
5611bae9e67eSachartre 			 * adjust the vtoc so that it defines a single-slice
5612bae9e67eSachartre 			 * disk.
5613bae9e67eSachartre 			 */
5614bae9e67eSachartre 			vd_build_default_label(vd->file_size, &label);
5615bae9e67eSachartre 			vd_label_to_vtocgeom(&label, &vd->vtoc, &vd->dk_geom);
5616bae9e67eSachartre 			status = vd_setup_partition_vtoc(vd);
5617bae9e67eSachartre 		}
5618bae9e67eSachartre 		return (status);
5619edcc0754Sachartre 	}
5620edcc0754Sachartre 
5621edcc0754Sachartre 	/*
5622edcc0754Sachartre 	 * Find and validate the geometry of a disk image.
5623edcc0754Sachartre 	 */
5624edcc0754Sachartre 	status = vd_file_validate_geometry(vd);
5625edcc0754Sachartre 	if (status != 0 && status != EINVAL && status != ENOTSUP) {
5626edcc0754Sachartre 		PRN("Failed to read label from %s", file_path);
5627edcc0754Sachartre 		return (EIO);
5628edcc0754Sachartre 	}
5629edcc0754Sachartre 
5630edcc0754Sachartre 	if (vd_file_is_iso_image(vd)) {
5631edcc0754Sachartre 		/*
5632edcc0754Sachartre 		 * Indicate whether to call this a CD or DVD from the size
5633edcc0754Sachartre 		 * of the ISO image (images for both drive types are stored
5634edcc0754Sachartre 		 * in the ISO-9600 format). CDs can store up to just under 1Gb
5635edcc0754Sachartre 		 */
5636edcc0754Sachartre 		if ((vd->vdisk_size * vd->vdisk_block_size) >
5637edcc0754Sachartre 		    (1024 * 1024 * 1024))
5638edcc0754Sachartre 			vd->vdisk_media = VD_MEDIA_DVD;
5639edcc0754Sachartre 		else
5640edcc0754Sachartre 			vd->vdisk_media = VD_MEDIA_CD;
5641edcc0754Sachartre 	} else {
5642edcc0754Sachartre 		vd->vdisk_media = VD_MEDIA_FIXED;
5643edcc0754Sachartre 	}
5644edcc0754Sachartre 
5645edcc0754Sachartre 	/* Setup devid for the disk image */
5646047ba61eSachartre 
564778fcd0a1Sachartre 	if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
564878fcd0a1Sachartre 
564987a7269eSachartre 		status = vd_file_read_devid(vd, &vd->file_devid);
565087a7269eSachartre 
565187a7269eSachartre 		if (status == 0) {
565287a7269eSachartre 			/* a valid devid was found */
565387a7269eSachartre 			return (0);
565487a7269eSachartre 		}
565587a7269eSachartre 
565687a7269eSachartre 		if (status != EINVAL) {
565787a7269eSachartre 			/*
565878fcd0a1Sachartre 			 * There was an error while trying to read the devid.
565978fcd0a1Sachartre 			 * So this disk image may have a devid but we are
566078fcd0a1Sachartre 			 * unable to read it.
566187a7269eSachartre 			 */
566287a7269eSachartre 			PR0("can not read devid for %s", file_path);
566387a7269eSachartre 			vd->file_devid = NULL;
566487a7269eSachartre 			return (0);
566587a7269eSachartre 		}
566678fcd0a1Sachartre 	}
566787a7269eSachartre 
566887a7269eSachartre 	/*
566987a7269eSachartre 	 * No valid device id was found so we create one. Note that a failure
567087a7269eSachartre 	 * to create a device id is not fatal and does not prevent the disk
567187a7269eSachartre 	 * image from being attached.
567287a7269eSachartre 	 */
567387a7269eSachartre 	PR1("creating devid for %s", file_path);
567487a7269eSachartre 
567587a7269eSachartre 	if (ddi_devid_init(vd->vds->dip, DEVID_FAB, NULL, 0,
567687a7269eSachartre 	    &vd->file_devid) != DDI_SUCCESS) {
567787a7269eSachartre 		PR0("fail to create devid for %s", file_path);
567887a7269eSachartre 		vd->file_devid = NULL;
567987a7269eSachartre 		return (0);
568087a7269eSachartre 	}
568187a7269eSachartre 
568278fcd0a1Sachartre 	/*
568378fcd0a1Sachartre 	 * Write devid to the disk image. The devid is stored into the disk
568478fcd0a1Sachartre 	 * image if we have a valid label; otherwise the devid will be stored
568578fcd0a1Sachartre 	 * when the user writes a valid label.
568678fcd0a1Sachartre 	 */
568778fcd0a1Sachartre 	if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
568887a7269eSachartre 		if (vd_file_write_devid(vd, vd->file_devid) != 0) {
568987a7269eSachartre 			PR0("fail to write devid for %s", file_path);
569087a7269eSachartre 			ddi_devid_free(vd->file_devid);
569187a7269eSachartre 			vd->file_devid = NULL;
569287a7269eSachartre 		}
569378fcd0a1Sachartre 	}
569487a7269eSachartre 
56953c96341aSnarayan 	return (0);
56963c96341aSnarayan }
56973c96341aSnarayan 
569817cadca8Slm66018 
569917cadca8Slm66018 /*
570017cadca8Slm66018  * Description:
570117cadca8Slm66018  *	Open a device using its device path (supplied by ldm(1m))
570217cadca8Slm66018  *
570317cadca8Slm66018  * Parameters:
570417cadca8Slm66018  *	vd 	- pointer to structure containing the vDisk info
57058fce2fd6Sachartre  *	flags	- open flags
570617cadca8Slm66018  *
570717cadca8Slm66018  * Return Value
570817cadca8Slm66018  *	0	- success
570917cadca8Slm66018  *	!= 0	- some other non-zero return value from ldi(9F) functions
571017cadca8Slm66018  */
571117cadca8Slm66018 static int
57128fce2fd6Sachartre vd_open_using_ldi_by_name(vd_t *vd, int flags)
571317cadca8Slm66018 {
57148fce2fd6Sachartre 	int		status;
571517cadca8Slm66018 	char		*device_path = vd->device_path;
571617cadca8Slm66018 
57178fce2fd6Sachartre 	/* Attempt to open device */
57188fce2fd6Sachartre 	status = ldi_open_by_name(device_path, flags, kcred,
571917cadca8Slm66018 	    &vd->ldi_handle[0], vd->vds->ldi_ident);
572017cadca8Slm66018 
572117cadca8Slm66018 	/*
572217cadca8Slm66018 	 * The open can fail for example if we are opening an empty slice.
572317cadca8Slm66018 	 * In case of a failure, we try the open again but this time with
572417cadca8Slm66018 	 * the FNDELAY flag.
572517cadca8Slm66018 	 */
572617cadca8Slm66018 	if (status != 0)
57278fce2fd6Sachartre 		status = ldi_open_by_name(device_path, flags | FNDELAY,
572817cadca8Slm66018 		    kcred, &vd->ldi_handle[0], vd->vds->ldi_ident);
572917cadca8Slm66018 
573017cadca8Slm66018 	if (status != 0) {
573117cadca8Slm66018 		PR0("ldi_open_by_name(%s) = errno %d", device_path, status);
573217cadca8Slm66018 		vd->ldi_handle[0] = NULL;
573317cadca8Slm66018 		return (status);
573417cadca8Slm66018 	}
573517cadca8Slm66018 
573617cadca8Slm66018 	return (0);
573717cadca8Slm66018 }
573817cadca8Slm66018 
5739047ba61eSachartre /*
5740047ba61eSachartre  * Setup for a virtual disk which backend is a device (a physical disk,
57418fce2fd6Sachartre  * slice or volume device) that is directly exported either as a full disk
57428fce2fd6Sachartre  * for a physical disk or as a slice for a volume device or a disk slice.
5743047ba61eSachartre  * In these cases, the backend is accessed using the LDI interface.
5744047ba61eSachartre  */
57453c96341aSnarayan static int
5746047ba61eSachartre vd_setup_backend_ldi(vd_t *vd)
57471ae08745Sheppo {
5748e1ebb9ecSlm66018 	int		rval, status;
57491ae08745Sheppo 	struct dk_cinfo	dk_cinfo;
57503c96341aSnarayan 	char		*device_path = vd->device_path;
57511ae08745Sheppo 
57528fce2fd6Sachartre 	/* device has been opened by vd_identify_dev() */
57538fce2fd6Sachartre 	ASSERT(vd->ldi_handle[0] != NULL);
57548fce2fd6Sachartre 	ASSERT(vd->dev[0] != NULL);
57550a55fbb7Slm66018 
57563c96341aSnarayan 	vd->file = B_FALSE;
57574bac2208Snarayan 
575878fcd0a1Sachartre 	/* Verify backing device supports dk_cinfo */
5759e1ebb9ecSlm66018 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO,
5760047ba61eSachartre 	    (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred,
5761e1ebb9ecSlm66018 	    &rval)) != 0) {
5762e1ebb9ecSlm66018 		PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
5763e1ebb9ecSlm66018 		    status, device_path);
5764e1ebb9ecSlm66018 		return (status);
5765e1ebb9ecSlm66018 	}
5766e1ebb9ecSlm66018 	if (dk_cinfo.dki_partition >= V_NUMPAR) {
5767e1ebb9ecSlm66018 		PRN("slice %u >= maximum slice %u for %s",
5768e1ebb9ecSlm66018 		    dk_cinfo.dki_partition, V_NUMPAR, device_path);
5769e1ebb9ecSlm66018 		return (EIO);
5770e1ebb9ecSlm66018 	}
57714bac2208Snarayan 
57728fce2fd6Sachartre 	/*
57738fce2fd6Sachartre 	 * The device has been opened read-only by vd_identify_dev(), re-open
57748fce2fd6Sachartre 	 * it read-write if the write flag is set and we don't have an optical
57758fce2fd6Sachartre 	 * device such as a CD-ROM, which, for now, we do not permit writes to
57768fce2fd6Sachartre 	 * and thus should not export write operations to the client.
57778fce2fd6Sachartre 	 *
57788fce2fd6Sachartre 	 * Future: if/when we implement support for guest domains writing to
57798fce2fd6Sachartre 	 * optical devices we will need to do further checking of the media type
57808fce2fd6Sachartre 	 * to distinguish between read-only and writable discs.
57818fce2fd6Sachartre 	 */
57828fce2fd6Sachartre 	if (dk_cinfo.dki_ctype == DKC_CDROM) {
57838fce2fd6Sachartre 
57848fce2fd6Sachartre 		vd->open_flags &= ~FWRITE;
57858fce2fd6Sachartre 
57868fce2fd6Sachartre 	} else if (vd->open_flags & FWRITE) {
57878fce2fd6Sachartre 
57888fce2fd6Sachartre 		(void) ldi_close(vd->ldi_handle[0], vd->open_flags & ~FWRITE,
57898fce2fd6Sachartre 		    kcred);
57908fce2fd6Sachartre 		status = vd_open_using_ldi_by_name(vd, vd->open_flags);
57918fce2fd6Sachartre 		if (status != 0) {
57928fce2fd6Sachartre 			PR0("Failed to open (%s) = errno %d",
57938fce2fd6Sachartre 			    device_path, status);
57948fce2fd6Sachartre 			return (status);
57958fce2fd6Sachartre 		}
57968fce2fd6Sachartre 	}
57978fce2fd6Sachartre 
5798e1ebb9ecSlm66018 	/* Store the device's max transfer size for return to the client */
5799e1ebb9ecSlm66018 	vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
5800e1ebb9ecSlm66018 
5801047ba61eSachartre 	/*
580217cadca8Slm66018 	 * We need to work out if it's an ATAPI (IDE CD-ROM) or SCSI device so
580317cadca8Slm66018 	 * that we can use the correct CDB group when sending USCSI commands.
580417cadca8Slm66018 	 */
580517cadca8Slm66018 	vd->is_atapi_dev = vd_is_atapi_device(vd);
580617cadca8Slm66018 
580717cadca8Slm66018 	/*
5808047ba61eSachartre 	 * Export a full disk.
5809047ba61eSachartre 	 *
5810047ba61eSachartre 	 * When we use the LDI interface, we export a device as a full disk
5811047ba61eSachartre 	 * if we have an entire disk slice (slice 2) and if this slice is
5812047ba61eSachartre 	 * exported as a full disk and not as a single slice disk.
581317cadca8Slm66018 	 * Similarly, we want to use LDI if we are accessing a CD or DVD
581417cadca8Slm66018 	 * device (even if it isn't s2)
5815047ba61eSachartre 	 *
58168fce2fd6Sachartre 	 * Note that volume devices are exported as full disks using the vnode
5817047ba61eSachartre 	 * interface, not the LDI interface.
5818047ba61eSachartre 	 */
581917cadca8Slm66018 	if ((dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE &&
582017cadca8Slm66018 	    vd->vdisk_type == VD_DISK_TYPE_DISK) ||
582117cadca8Slm66018 	    dk_cinfo.dki_ctype == DKC_CDROM) {
58228fce2fd6Sachartre 		ASSERT(!vd->volume);
58232f5224aeSachartre 		if (dk_cinfo.dki_ctype == DKC_SCSI_CCS)
58242f5224aeSachartre 			vd->scsi = B_TRUE;
5825047ba61eSachartre 		return (vd_setup_full_disk(vd));
5826047ba61eSachartre 	}
5827047ba61eSachartre 
5828047ba61eSachartre 	/*
5829047ba61eSachartre 	 * Export a single slice disk.
5830047ba61eSachartre 	 *
58318fce2fd6Sachartre 	 * The exported device can be either a volume device or a disk slice. If
5832047ba61eSachartre 	 * it is a disk slice different from slice 2 then it is always exported
5833047ba61eSachartre 	 * as a single slice disk even if the "slice" option is not specified.
58348fce2fd6Sachartre 	 * If it is disk slice 2 or a volume device then it is exported as a
5835047ba61eSachartre 	 * single slice disk only if the "slice" option is specified.
5836047ba61eSachartre 	 */
5837047ba61eSachartre 	return (vd_setup_single_slice_disk(vd));
5838047ba61eSachartre }
5839047ba61eSachartre 
5840047ba61eSachartre static int
5841047ba61eSachartre vd_setup_single_slice_disk(vd_t *vd)
5842047ba61eSachartre {
5843edcc0754Sachartre 	int status, rval;
5844bae9e67eSachartre 	struct dk_label label;
5845047ba61eSachartre 	char *device_path = vd->device_path;
5846047ba61eSachartre 
5847047ba61eSachartre 	/* Get size of backing device */
5848047ba61eSachartre 	if (ldi_get_size(vd->ldi_handle[0], &vd->vdisk_size) != DDI_SUCCESS) {
5849047ba61eSachartre 		PRN("ldi_get_size() failed for %s", device_path);
58501ae08745Sheppo 		return (EIO);
58511ae08745Sheppo 	}
5852047ba61eSachartre 	vd->vdisk_size = lbtodb(vd->vdisk_size);	/* convert to blocks */
585317cadca8Slm66018 	vd->block_size = DEV_BSIZE;
585417cadca8Slm66018 	vd->vdisk_block_size = DEV_BSIZE;
585517cadca8Slm66018 	vd->vdisk_media = VD_MEDIA_FIXED;
5856047ba61eSachartre 
58578fce2fd6Sachartre 	if (vd->volume) {
5858047ba61eSachartre 		ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
585978fcd0a1Sachartre 	}
58600a55fbb7Slm66018 
5861047ba61eSachartre 	/*
5862047ba61eSachartre 	 * We export the slice as a single slice disk even if the "slice"
5863047ba61eSachartre 	 * option was not specified.
5864047ba61eSachartre 	 */
58651ae08745Sheppo 	vd->vdisk_type  = VD_DISK_TYPE_SLICE;
58661ae08745Sheppo 	vd->nslices	= 1;
58671ae08745Sheppo 
5868edcc0754Sachartre 	/*
5869edcc0754Sachartre 	 * When exporting a slice or a device as a single slice disk, we don't
5870edcc0754Sachartre 	 * care about any partitioning exposed by the backend. The goal is just
5871edcc0754Sachartre 	 * to export the backend as a flat storage. We provide a fake partition
5872edcc0754Sachartre 	 * table (either a VTOC or EFI), which presents only one slice, to
5873bae9e67eSachartre 	 * accommodate tools expecting a disk label. The selection of the label
5874bae9e67eSachartre 	 * type (VTOC or EFI) depends on the value of the vd_slice_label
5875bae9e67eSachartre 	 * variable.
5876edcc0754Sachartre 	 */
5877bae9e67eSachartre 	if (vd_slice_label == VD_DISK_LABEL_EFI ||
5878bae9e67eSachartre 	    vd->vdisk_size >= ONE_TERABYTE / DEV_BSIZE) {
5879bae9e67eSachartre 		vd->vdisk_label = VD_DISK_LABEL_EFI;
5880bae9e67eSachartre 	} else {
5881bae9e67eSachartre 		status = ldi_ioctl(vd->ldi_handle[0], DKIOCGVTOC,
5882bae9e67eSachartre 		    (intptr_t)&vd->vtoc, (vd->open_flags | FKIOCTL),
5883bae9e67eSachartre 		    kcred, &rval);
5884edcc0754Sachartre 
5885edcc0754Sachartre 		if (status == 0) {
5886bae9e67eSachartre 			status = ldi_ioctl(vd->ldi_handle[0], DKIOCGGEOM,
5887bae9e67eSachartre 			    (intptr_t)&vd->dk_geom, (vd->open_flags | FKIOCTL),
5888bae9e67eSachartre 			    kcred, &rval);
5889bae9e67eSachartre 
5890bae9e67eSachartre 			if (status != 0) {
5891bae9e67eSachartre 				PRN("ldi_ioctl(DKIOCGEOM) returned errno %d "
5892bae9e67eSachartre 				    "for %s", status, device_path);
5893bae9e67eSachartre 				return (status);
5894bae9e67eSachartre 			}
5895edcc0754Sachartre 			vd->vdisk_label = VD_DISK_LABEL_VTOC;
5896bae9e67eSachartre 
5897bae9e67eSachartre 		} else if (vd_slice_label == VD_DISK_LABEL_VTOC) {
5898bae9e67eSachartre 
5899bae9e67eSachartre 			vd->vdisk_label = VD_DISK_LABEL_VTOC;
5900bae9e67eSachartre 			vd_build_default_label(vd->vdisk_size * DEV_BSIZE,
5901bae9e67eSachartre 			    &label);
5902bae9e67eSachartre 			vd_label_to_vtocgeom(&label, &vd->vtoc, &vd->dk_geom);
5903bae9e67eSachartre 
5904bae9e67eSachartre 		} else {
5905bae9e67eSachartre 			vd->vdisk_label = VD_DISK_LABEL_EFI;
5906bae9e67eSachartre 		}
5907bae9e67eSachartre 	}
5908bae9e67eSachartre 
5909bae9e67eSachartre 	if (vd->vdisk_label == VD_DISK_LABEL_VTOC) {
5910bae9e67eSachartre 		/* export with a fake VTOC label */
591178fcd0a1Sachartre 		status = vd_setup_partition_vtoc(vd);
5912bae9e67eSachartre 
5913edcc0754Sachartre 	} else {
5914edcc0754Sachartre 		/* export with a fake EFI label */
5915edcc0754Sachartre 		status = vd_setup_partition_efi(vd);
591678fcd0a1Sachartre 	}
591778fcd0a1Sachartre 
59184bac2208Snarayan 	return (status);
59194bac2208Snarayan }
59201ae08745Sheppo 
5921*de3a5331SRamesh Chitrothu static int
5922*de3a5331SRamesh Chitrothu vd_backend_check_size(vd_t *vd)
5923*de3a5331SRamesh Chitrothu {
5924*de3a5331SRamesh Chitrothu 	size_t backend_size, old_size, new_size;
5925*de3a5331SRamesh Chitrothu 	struct dk_minfo minfo;
5926*de3a5331SRamesh Chitrothu 	vattr_t vattr;
5927*de3a5331SRamesh Chitrothu 	int rval, rv;
5928*de3a5331SRamesh Chitrothu 
5929*de3a5331SRamesh Chitrothu 	if (vd->file) {
5930*de3a5331SRamesh Chitrothu 
5931*de3a5331SRamesh Chitrothu 		/* file (slice or full disk) */
5932*de3a5331SRamesh Chitrothu 		vattr.va_mask = AT_SIZE;
5933*de3a5331SRamesh Chitrothu 		rv = VOP_GETATTR(vd->file_vnode, &vattr, 0, kcred, NULL);
5934*de3a5331SRamesh Chitrothu 		if (rv != 0) {
5935*de3a5331SRamesh Chitrothu 			PR0("VOP_GETATTR(%s) = errno %d", vd->device_path, rv);
5936*de3a5331SRamesh Chitrothu 			return (rv);
5937*de3a5331SRamesh Chitrothu 		}
5938*de3a5331SRamesh Chitrothu 		backend_size = vattr.va_size;
5939*de3a5331SRamesh Chitrothu 
5940*de3a5331SRamesh Chitrothu 	} else if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
5941*de3a5331SRamesh Chitrothu 
5942*de3a5331SRamesh Chitrothu 		/* slice or device exported as a slice */
5943*de3a5331SRamesh Chitrothu 		rv = ldi_get_size(vd->ldi_handle[0], &backend_size);
5944*de3a5331SRamesh Chitrothu 		if (rv != DDI_SUCCESS) {
5945*de3a5331SRamesh Chitrothu 			PR0("ldi_get_size() failed for %s", vd->device_path);
5946*de3a5331SRamesh Chitrothu 			return (EIO);
5947*de3a5331SRamesh Chitrothu 		}
5948*de3a5331SRamesh Chitrothu 
5949*de3a5331SRamesh Chitrothu 	} else {
5950*de3a5331SRamesh Chitrothu 
5951*de3a5331SRamesh Chitrothu 		/* disk or device exported as a disk */
5952*de3a5331SRamesh Chitrothu 		ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
5953*de3a5331SRamesh Chitrothu 		rv = ldi_ioctl(vd->ldi_handle[0], DKIOCGMEDIAINFO,
5954*de3a5331SRamesh Chitrothu 		    (intptr_t)&minfo, (vd->open_flags | FKIOCTL),
5955*de3a5331SRamesh Chitrothu 		    kcred, &rval);
5956*de3a5331SRamesh Chitrothu 		if (rv != 0) {
5957*de3a5331SRamesh Chitrothu 			PR0("DKIOCGMEDIAINFO failed for %s (err=%d)",
5958*de3a5331SRamesh Chitrothu 			    vd->device_path, rv);
5959*de3a5331SRamesh Chitrothu 			return (rv);
5960*de3a5331SRamesh Chitrothu 		}
5961*de3a5331SRamesh Chitrothu 		backend_size = minfo.dki_capacity * minfo.dki_lbsize;
5962*de3a5331SRamesh Chitrothu 	}
5963*de3a5331SRamesh Chitrothu 
5964*de3a5331SRamesh Chitrothu 	old_size = vd->vdisk_size;
5965*de3a5331SRamesh Chitrothu 	new_size = backend_size / DEV_BSIZE;
5966*de3a5331SRamesh Chitrothu 
5967*de3a5331SRamesh Chitrothu 	/* check if size has changed */
5968*de3a5331SRamesh Chitrothu 	if (old_size != VD_SIZE_UNKNOWN && old_size == new_size)
5969*de3a5331SRamesh Chitrothu 		return (0);
5970*de3a5331SRamesh Chitrothu 
5971*de3a5331SRamesh Chitrothu 	vd->vdisk_size = new_size;
5972*de3a5331SRamesh Chitrothu 
5973*de3a5331SRamesh Chitrothu 	if (vd->file)
5974*de3a5331SRamesh Chitrothu 		vd->file_size = backend_size;
5975*de3a5331SRamesh Chitrothu 
5976*de3a5331SRamesh Chitrothu 	/*
5977*de3a5331SRamesh Chitrothu 	 * If we are exporting a single-slice disk and the size of the backend
5978*de3a5331SRamesh Chitrothu 	 * has changed then we regenerate the partition setup so that the
5979*de3a5331SRamesh Chitrothu 	 * partitioning matches with the new disk backend size.
5980*de3a5331SRamesh Chitrothu 	 */
5981*de3a5331SRamesh Chitrothu 
5982*de3a5331SRamesh Chitrothu 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
5983*de3a5331SRamesh Chitrothu 		/* slice or file or device exported as a slice */
5984*de3a5331SRamesh Chitrothu 		if (vd->vdisk_label == VD_DISK_LABEL_VTOC) {
5985*de3a5331SRamesh Chitrothu 			rv = vd_setup_partition_vtoc(vd);
5986*de3a5331SRamesh Chitrothu 			if (rv != 0) {
5987*de3a5331SRamesh Chitrothu 				PR0("vd_setup_partition_vtoc() failed for %s "
5988*de3a5331SRamesh Chitrothu 				    "(err = %d)", vd->device_path, rv);
5989*de3a5331SRamesh Chitrothu 				return (rv);
5990*de3a5331SRamesh Chitrothu 			}
5991*de3a5331SRamesh Chitrothu 		} else {
5992*de3a5331SRamesh Chitrothu 			rv = vd_setup_partition_efi(vd);
5993*de3a5331SRamesh Chitrothu 			if (rv != 0) {
5994*de3a5331SRamesh Chitrothu 				PR0("vd_setup_partition_efi() failed for %s "
5995*de3a5331SRamesh Chitrothu 				    "(err = %d)", vd->device_path, rv);
5996*de3a5331SRamesh Chitrothu 				return (rv);
5997*de3a5331SRamesh Chitrothu 			}
5998*de3a5331SRamesh Chitrothu 		}
5999*de3a5331SRamesh Chitrothu 
6000*de3a5331SRamesh Chitrothu 	} else if (!vd->file) {
6001*de3a5331SRamesh Chitrothu 		/* disk or device exported as a disk */
6002*de3a5331SRamesh Chitrothu 		ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
6003*de3a5331SRamesh Chitrothu 		vd->block_size = minfo.dki_lbsize;
6004*de3a5331SRamesh Chitrothu 		vd->vdisk_media =
6005*de3a5331SRamesh Chitrothu 		    DK_MEDIATYPE2VD_MEDIATYPE(minfo.dki_media_type);
6006*de3a5331SRamesh Chitrothu 	}
6007*de3a5331SRamesh Chitrothu 
6008*de3a5331SRamesh Chitrothu 	return (0);
6009*de3a5331SRamesh Chitrothu }
6010*de3a5331SRamesh Chitrothu 
60118fce2fd6Sachartre /*
60128fce2fd6Sachartre  * Description:
60138fce2fd6Sachartre  *	Open a device using its device path and identify if this is
60148fce2fd6Sachartre  *	a disk device or a volume device.
60158fce2fd6Sachartre  *
60168fce2fd6Sachartre  * Parameters:
60178fce2fd6Sachartre  *	vd 	- pointer to structure containing the vDisk info
60188fce2fd6Sachartre  *	dtype	- return the driver type of the device
60198fce2fd6Sachartre  *
60208fce2fd6Sachartre  * Return Value
60218fce2fd6Sachartre  *	0	- success
60228fce2fd6Sachartre  *	!= 0	- some other non-zero return value from ldi(9F) functions
60238fce2fd6Sachartre  */
60248fce2fd6Sachartre static int
60258fce2fd6Sachartre vd_identify_dev(vd_t *vd, int *dtype)
60268fce2fd6Sachartre {
60278fce2fd6Sachartre 	int status, i;
60288fce2fd6Sachartre 	char *device_path = vd->device_path;
60298fce2fd6Sachartre 	char *drv_name;
60308fce2fd6Sachartre 	int drv_type;
60318fce2fd6Sachartre 	vds_t *vds = vd->vds;
60328fce2fd6Sachartre 
60338fce2fd6Sachartre 	status = vd_open_using_ldi_by_name(vd, vd->open_flags & ~FWRITE);
60348fce2fd6Sachartre 	if (status != 0) {
60358fce2fd6Sachartre 		PR0("Failed to open (%s) = errno %d", device_path, status);
60368fce2fd6Sachartre 		return (status);
60378fce2fd6Sachartre 	}
60388fce2fd6Sachartre 
60398fce2fd6Sachartre 	/* Get device number of backing device */
60408fce2fd6Sachartre 	if ((status = ldi_get_dev(vd->ldi_handle[0], &vd->dev[0])) != 0) {
60418fce2fd6Sachartre 		PRN("ldi_get_dev() returned errno %d for %s",
60428fce2fd6Sachartre 		    status, device_path);
60438fce2fd6Sachartre 		return (status);
60448fce2fd6Sachartre 	}
60458fce2fd6Sachartre 
60468fce2fd6Sachartre 	/*
60478fce2fd6Sachartre 	 * We start by looking if the driver is in the list from vds.conf
60488fce2fd6Sachartre 	 * so that we can override the built-in list using vds.conf.
60498fce2fd6Sachartre 	 */
60508fce2fd6Sachartre 	drv_name = ddi_major_to_name(getmajor(vd->dev[0]));
60518fce2fd6Sachartre 	drv_type = VD_DRIVER_UNKNOWN;
60528fce2fd6Sachartre 
60538fce2fd6Sachartre 	/* check vds.conf list */
60548fce2fd6Sachartre 	for (i = 0; i < vds->num_drivers; i++) {
60558fce2fd6Sachartre 		if (vds->driver_types[i].type == VD_DRIVER_UNKNOWN) {
60568fce2fd6Sachartre 			/* ignore invalid entries */
60578fce2fd6Sachartre 			continue;
60588fce2fd6Sachartre 		}
60598fce2fd6Sachartre 		if (strcmp(drv_name, vds->driver_types[i].name) == 0) {
60608fce2fd6Sachartre 			drv_type = vds->driver_types[i].type;
60618fce2fd6Sachartre 			goto done;
60628fce2fd6Sachartre 		}
60638fce2fd6Sachartre 	}
60648fce2fd6Sachartre 
60658fce2fd6Sachartre 	/* check built-in list */
60668fce2fd6Sachartre 	for (i = 0; i < VDS_NUM_DRIVERS; i++) {
60678fce2fd6Sachartre 		if (strcmp(drv_name, vds_driver_types[i].name) == 0) {
60688fce2fd6Sachartre 			drv_type = vds_driver_types[i].type;
60698fce2fd6Sachartre 			goto done;
60708fce2fd6Sachartre 		}
60718fce2fd6Sachartre 	}
60728fce2fd6Sachartre 
60738fce2fd6Sachartre done:
60748fce2fd6Sachartre 	PR0("driver %s identified as %s", drv_name,
60758fce2fd6Sachartre 	    (drv_type == VD_DRIVER_DISK)? "DISK" :
60768fce2fd6Sachartre 	    (drv_type == VD_DRIVER_VOLUME)? "VOLUME" : "UNKNOWN");
60778fce2fd6Sachartre 
60788fce2fd6Sachartre 	*dtype = drv_type;
60798fce2fd6Sachartre 
60808fce2fd6Sachartre 	return (0);
60818fce2fd6Sachartre }
60828fce2fd6Sachartre 
60831ae08745Sheppo static int
6084047ba61eSachartre vd_setup_vd(vd_t *vd)
6085047ba61eSachartre {
60868fce2fd6Sachartre 	int		status, drv_type, pseudo;
6087047ba61eSachartre 	dev_info_t	*dip;
6088047ba61eSachartre 	vnode_t 	*vnp;
6089047ba61eSachartre 	char		*path = vd->device_path;
6090047ba61eSachartre 
6091047ba61eSachartre 	/* make sure the vdisk backend is valid */
6092047ba61eSachartre 	if ((status = lookupname(path, UIO_SYSSPACE,
6093047ba61eSachartre 	    FOLLOW, NULLVPP, &vnp)) != 0) {
6094047ba61eSachartre 		PR0("Cannot lookup %s errno %d", path, status);
6095047ba61eSachartre 		goto done;
6096047ba61eSachartre 	}
6097047ba61eSachartre 
6098047ba61eSachartre 	switch (vnp->v_type) {
6099047ba61eSachartre 	case VREG:
6100047ba61eSachartre 		/*
6101047ba61eSachartre 		 * Backend is a file so it is exported as a full disk or as a
6102047ba61eSachartre 		 * single slice disk using the vnode interface.
6103047ba61eSachartre 		 */
6104047ba61eSachartre 		VN_RELE(vnp);
61058fce2fd6Sachartre 		vd->volume = B_FALSE;
6106047ba61eSachartre 		status = vd_setup_backend_vnode(vd);
6107047ba61eSachartre 		break;
6108047ba61eSachartre 
6109047ba61eSachartre 	case VBLK:
6110047ba61eSachartre 	case VCHR:
6111047ba61eSachartre 		/*
6112047ba61eSachartre 		 * Backend is a device. The way it is exported depends on the
6113047ba61eSachartre 		 * type of the device.
6114047ba61eSachartre 		 *
61158fce2fd6Sachartre 		 * - A volume device is exported as a full disk using the vnode
6116047ba61eSachartre 		 *   interface or as a single slice disk using the LDI
6117047ba61eSachartre 		 *   interface.
6118047ba61eSachartre 		 *
6119047ba61eSachartre 		 * - A disk (represented by the slice 2 of that disk) is
6120047ba61eSachartre 		 *   exported as a full disk using the LDI interface.
6121047ba61eSachartre 		 *
6122047ba61eSachartre 		 * - A disk slice (different from slice 2) is always exported
6123047ba61eSachartre 		 *   as a single slice disk using the LDI interface.
6124047ba61eSachartre 		 *
6125047ba61eSachartre 		 * - The slice 2 of a disk is exported as a single slice disk
6126047ba61eSachartre 		 *   if the "slice" option is specified, otherwise the entire
6127047ba61eSachartre 		 *   disk will be exported. In any case, the LDI interface is
6128047ba61eSachartre 		 *   used.
6129047ba61eSachartre 		 */
6130047ba61eSachartre 
6131047ba61eSachartre 		/* check if this is a pseudo device */
6132047ba61eSachartre 		if ((dip = ddi_hold_devi_by_instance(getmajor(vnp->v_rdev),
6133047ba61eSachartre 		    dev_to_instance(vnp->v_rdev), 0))  == NULL) {
6134047ba61eSachartre 			PRN("%s is no longer accessible", path);
6135047ba61eSachartre 			VN_RELE(vnp);
6136047ba61eSachartre 			status = EIO;
6137047ba61eSachartre 			break;
6138047ba61eSachartre 		}
61398fce2fd6Sachartre 		pseudo = is_pseudo_device(dip);
6140047ba61eSachartre 		ddi_release_devi(dip);
6141047ba61eSachartre 		VN_RELE(vnp);
6142047ba61eSachartre 
61438fce2fd6Sachartre 		if (vd_identify_dev(vd, &drv_type) != 0) {
61448fce2fd6Sachartre 			PRN("%s identification failed", path);
61458fce2fd6Sachartre 			status = EIO;
61468fce2fd6Sachartre 			break;
61478fce2fd6Sachartre 		}
61488fce2fd6Sachartre 
61498fce2fd6Sachartre 		/*
61508fce2fd6Sachartre 		 * If the driver hasn't been identified then we consider that
61518fce2fd6Sachartre 		 * pseudo devices are volumes and other devices are disks.
61528fce2fd6Sachartre 		 */
61538fce2fd6Sachartre 		if (drv_type == VD_DRIVER_VOLUME ||
61548fce2fd6Sachartre 		    (drv_type == VD_DRIVER_UNKNOWN && pseudo)) {
61558fce2fd6Sachartre 			vd->volume = B_TRUE;
61568fce2fd6Sachartre 		} else {
61572f5224aeSachartre 			status = vd_setup_backend_ldi(vd);
61582f5224aeSachartre 			break;
61592f5224aeSachartre 		}
61602f5224aeSachartre 
6161047ba61eSachartre 		/*
61628fce2fd6Sachartre 		 * If this is a volume device then its usage depends if the
6163047ba61eSachartre 		 * "slice" option is set or not. If the "slice" option is set
61648fce2fd6Sachartre 		 * then the volume device will be exported as a single slice,
6165047ba61eSachartre 		 * otherwise it will be exported as a full disk.
61662f5224aeSachartre 		 *
61672f5224aeSachartre 		 * For backward compatibility, if vd_volume_force_slice is set
61688fce2fd6Sachartre 		 * then we always export volume devices as slices.
6169047ba61eSachartre 		 */
61702f5224aeSachartre 		if (vd_volume_force_slice) {
61712f5224aeSachartre 			vd->vdisk_type = VD_DISK_TYPE_SLICE;
61722f5224aeSachartre 			vd->nslices = 1;
61732f5224aeSachartre 		}
61742f5224aeSachartre 
61758fce2fd6Sachartre 		if (vd->vdisk_type == VD_DISK_TYPE_DISK) {
61768fce2fd6Sachartre 			/* close device opened during identification */
61778fce2fd6Sachartre 			(void) ldi_close(vd->ldi_handle[0],
61788fce2fd6Sachartre 			    vd->open_flags & ~FWRITE, kcred);
61798fce2fd6Sachartre 			vd->ldi_handle[0] = NULL;
61808fce2fd6Sachartre 			vd->dev[0] = 0;
6181047ba61eSachartre 			status = vd_setup_backend_vnode(vd);
61828fce2fd6Sachartre 		} else {
6183047ba61eSachartre 			status = vd_setup_backend_ldi(vd);
61848fce2fd6Sachartre 		}
6185047ba61eSachartre 		break;
6186047ba61eSachartre 
6187047ba61eSachartre 	default:
6188047ba61eSachartre 		PRN("Unsupported vdisk backend %s", path);
6189047ba61eSachartre 		VN_RELE(vnp);
6190047ba61eSachartre 		status = EBADF;
6191047ba61eSachartre 	}
6192047ba61eSachartre 
6193047ba61eSachartre done:
6194047ba61eSachartre 	if (status != 0) {
6195047ba61eSachartre 		/*
6196047ba61eSachartre 		 * If the error is retryable print an error message only
6197047ba61eSachartre 		 * during the first try.
6198047ba61eSachartre 		 */
6199047ba61eSachartre 		if (status == ENXIO || status == ENODEV ||
6200047ba61eSachartre 		    status == ENOENT || status == EROFS) {
6201047ba61eSachartre 			if (!(vd->initialized & VD_SETUP_ERROR)) {
6202047ba61eSachartre 				PRN("%s is currently inaccessible (error %d)",
6203047ba61eSachartre 				    path, status);
6204047ba61eSachartre 			}
6205047ba61eSachartre 			status = EAGAIN;
6206047ba61eSachartre 		} else {
6207047ba61eSachartre 			PRN("%s can not be exported as a virtual disk "
6208047ba61eSachartre 			    "(error %d)", path, status);
6209047ba61eSachartre 		}
6210047ba61eSachartre 		vd->initialized |= VD_SETUP_ERROR;
6211047ba61eSachartre 
6212047ba61eSachartre 	} else if (vd->initialized & VD_SETUP_ERROR) {
6213047ba61eSachartre 		/* print a message only if we previously had an error */
6214047ba61eSachartre 		PRN("%s is now online", path);
6215047ba61eSachartre 		vd->initialized &= ~VD_SETUP_ERROR;
6216047ba61eSachartre 	}
6217047ba61eSachartre 
6218047ba61eSachartre 	return (status);
6219047ba61eSachartre }
6220047ba61eSachartre 
6221047ba61eSachartre static int
6222047ba61eSachartre vds_do_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options,
6223047ba61eSachartre     uint64_t ldc_id, vd_t **vdp)
62241ae08745Sheppo {
62251ae08745Sheppo 	char			tq_name[TASKQ_NAMELEN];
62260a55fbb7Slm66018 	int			status;
62271ae08745Sheppo 	ddi_iblock_cookie_t	iblock = NULL;
62281ae08745Sheppo 	ldc_attr_t		ldc_attr;
62291ae08745Sheppo 	vd_t			*vd;
62301ae08745Sheppo 
62311ae08745Sheppo 
62321ae08745Sheppo 	ASSERT(vds != NULL);
6233e1ebb9ecSlm66018 	ASSERT(device_path != NULL);
62341ae08745Sheppo 	ASSERT(vdp != NULL);
6235e1ebb9ecSlm66018 	PR0("Adding vdisk for %s", device_path);
62361ae08745Sheppo 
62371ae08745Sheppo 	if ((vd = kmem_zalloc(sizeof (*vd), KM_NOSLEEP)) == NULL) {
62381ae08745Sheppo 		PRN("No memory for virtual disk");
62391ae08745Sheppo 		return (EAGAIN);
62401ae08745Sheppo 	}
62411ae08745Sheppo 	*vdp = vd;	/* assign here so vds_destroy_vd() can cleanup later */
62421ae08745Sheppo 	vd->vds = vds;
62433c96341aSnarayan 	(void) strncpy(vd->device_path, device_path, MAXPATHLEN);
62441ae08745Sheppo 
6245047ba61eSachartre 	/* Setup open flags */
6246047ba61eSachartre 	vd->open_flags = FREAD;
6247047ba61eSachartre 
6248047ba61eSachartre 	if (!(options & VD_OPT_RDONLY))
6249047ba61eSachartre 		vd->open_flags |= FWRITE;
6250047ba61eSachartre 
6251047ba61eSachartre 	if (options & VD_OPT_EXCLUSIVE)
6252047ba61eSachartre 		vd->open_flags |= FEXCL;
6253047ba61eSachartre 
6254047ba61eSachartre 	/* Setup disk type */
6255047ba61eSachartre 	if (options & VD_OPT_SLICE) {
6256047ba61eSachartre 		vd->vdisk_type = VD_DISK_TYPE_SLICE;
6257047ba61eSachartre 		vd->nslices = 1;
6258047ba61eSachartre 	} else {
6259047ba61eSachartre 		vd->vdisk_type = VD_DISK_TYPE_DISK;
6260047ba61eSachartre 		vd->nslices = V_NUMPAR;
6261047ba61eSachartre 	}
6262047ba61eSachartre 
6263047ba61eSachartre 	/* default disk label */
6264047ba61eSachartre 	vd->vdisk_label = VD_DISK_LABEL_UNK;
6265047ba61eSachartre 
62660a55fbb7Slm66018 	/* Open vdisk and initialize parameters */
62673c96341aSnarayan 	if ((status = vd_setup_vd(vd)) == 0) {
62683c96341aSnarayan 		vd->initialized |= VD_DISK_READY;
62691ae08745Sheppo 
62703c96341aSnarayan 		ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
62718fce2fd6Sachartre 		PR0("vdisk_type = %s, volume = %s, file = %s, nslices = %u",
62723c96341aSnarayan 		    ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
62738fce2fd6Sachartre 		    (vd->volume ? "yes" : "no"), (vd->file ? "yes" : "no"),
62743c96341aSnarayan 		    vd->nslices);
62753c96341aSnarayan 	} else {
62763c96341aSnarayan 		if (status != EAGAIN)
62773c96341aSnarayan 			return (status);
62783c96341aSnarayan 	}
62791ae08745Sheppo 
62801ae08745Sheppo 	/* Initialize locking */
62811ae08745Sheppo 	if (ddi_get_soft_iblock_cookie(vds->dip, DDI_SOFTINT_MED,
62821ae08745Sheppo 	    &iblock) != DDI_SUCCESS) {
62831ae08745Sheppo 		PRN("Could not get iblock cookie.");
62841ae08745Sheppo 		return (EIO);
62851ae08745Sheppo 	}
62861ae08745Sheppo 
62871ae08745Sheppo 	mutex_init(&vd->lock, NULL, MUTEX_DRIVER, iblock);
62881ae08745Sheppo 	vd->initialized |= VD_LOCKING;
62891ae08745Sheppo 
62901ae08745Sheppo 
6291d10e4ef2Snarayan 	/* Create start and completion task queues for the vdisk */
6292d10e4ef2Snarayan 	(void) snprintf(tq_name, sizeof (tq_name), "vd_startq%lu", id);
62931ae08745Sheppo 	PR1("tq_name = %s", tq_name);
6294d10e4ef2Snarayan 	if ((vd->startq = ddi_taskq_create(vds->dip, tq_name, 1,
62951ae08745Sheppo 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
62961ae08745Sheppo 		PRN("Could not create task queue");
62971ae08745Sheppo 		return (EIO);
62981ae08745Sheppo 	}
6299d10e4ef2Snarayan 	(void) snprintf(tq_name, sizeof (tq_name), "vd_completionq%lu", id);
6300d10e4ef2Snarayan 	PR1("tq_name = %s", tq_name);
6301d10e4ef2Snarayan 	if ((vd->completionq = ddi_taskq_create(vds->dip, tq_name, 1,
6302d10e4ef2Snarayan 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
6303d10e4ef2Snarayan 		PRN("Could not create task queue");
6304d10e4ef2Snarayan 		return (EIO);
6305d10e4ef2Snarayan 	}
63065b98b509Sachartre 
63075b98b509Sachartre 	/* Allocate the staging buffer */
63085b98b509Sachartre 	vd->max_msglen = sizeof (vio_msg_t);	/* baseline vio message size */
63095b98b509Sachartre 	vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
63105b98b509Sachartre 
6311d10e4ef2Snarayan 	vd->enabled = 1;	/* before callback can dispatch to startq */
63121ae08745Sheppo 
63131ae08745Sheppo 
63141ae08745Sheppo 	/* Bring up LDC */
63151ae08745Sheppo 	ldc_attr.devclass	= LDC_DEV_BLK_SVC;
63161ae08745Sheppo 	ldc_attr.instance	= ddi_get_instance(vds->dip);
63171ae08745Sheppo 	ldc_attr.mode		= LDC_MODE_UNRELIABLE;
6318e1ebb9ecSlm66018 	ldc_attr.mtu		= VD_LDC_MTU;
63191ae08745Sheppo 	if ((status = ldc_init(ldc_id, &ldc_attr, &vd->ldc_handle)) != 0) {
632017cadca8Slm66018 		PRN("Could not initialize LDC channel %lx, "
6321690555a1Sachartre 		    "init failed with error %d", ldc_id, status);
63221ae08745Sheppo 		return (status);
63231ae08745Sheppo 	}
63241ae08745Sheppo 	vd->initialized |= VD_LDC;
63251ae08745Sheppo 
63261ae08745Sheppo 	if ((status = ldc_reg_callback(vd->ldc_handle, vd_handle_ldc_events,
63271ae08745Sheppo 	    (caddr_t)vd)) != 0) {
6328690555a1Sachartre 		PRN("Could not initialize LDC channel %lu,"
6329690555a1Sachartre 		    "reg_callback failed with error %d", ldc_id, status);
63301ae08745Sheppo 		return (status);
63311ae08745Sheppo 	}
63321ae08745Sheppo 
63331ae08745Sheppo 	if ((status = ldc_open(vd->ldc_handle)) != 0) {
6334690555a1Sachartre 		PRN("Could not initialize LDC channel %lu,"
6335690555a1Sachartre 		    "open failed with error %d", ldc_id, status);
63361ae08745Sheppo 		return (status);
63371ae08745Sheppo 	}
63381ae08745Sheppo 
63393af08d82Slm66018 	if ((status = ldc_up(vd->ldc_handle)) != 0) {
634034683adeSsg70180 		PR0("ldc_up() returned errno %d", status);
63413af08d82Slm66018 	}
63423af08d82Slm66018 
63434bac2208Snarayan 	/* Allocate the inband task memory handle */
63444bac2208Snarayan 	status = ldc_mem_alloc_handle(vd->ldc_handle, &(vd->inband_task.mhdl));
63454bac2208Snarayan 	if (status) {
6346690555a1Sachartre 		PRN("Could not initialize LDC channel %lu,"
6347690555a1Sachartre 		    "alloc_handle failed with error %d", ldc_id, status);
63484bac2208Snarayan 		return (ENXIO);
63494bac2208Snarayan 	}
63501ae08745Sheppo 
63511ae08745Sheppo 	/* Add the successfully-initialized vdisk to the server's table */
63521ae08745Sheppo 	if (mod_hash_insert(vds->vd_table, (mod_hash_key_t)id, vd) != 0) {
63531ae08745Sheppo 		PRN("Error adding vdisk ID %lu to table", id);
63541ae08745Sheppo 		return (EIO);
63551ae08745Sheppo 	}
63561ae08745Sheppo 
63573af08d82Slm66018 	/* store initial state */
63583af08d82Slm66018 	vd->state = VD_STATE_INIT;
63593af08d82Slm66018 
63601ae08745Sheppo 	return (0);
63611ae08745Sheppo }
63621ae08745Sheppo 
63633af08d82Slm66018 static void
63643af08d82Slm66018 vd_free_dring_task(vd_t *vdp)
63653af08d82Slm66018 {
63663af08d82Slm66018 	if (vdp->dring_task != NULL) {
63673af08d82Slm66018 		ASSERT(vdp->dring_len != 0);
63683af08d82Slm66018 		/* Free all dring_task memory handles */
63693af08d82Slm66018 		for (int i = 0; i < vdp->dring_len; i++) {
63703af08d82Slm66018 			(void) ldc_mem_free_handle(vdp->dring_task[i].mhdl);
63715b7cb889Sha137994 			kmem_free(vdp->dring_task[i].request,
63725b7cb889Sha137994 			    (vdp->descriptor_size -
63735b7cb889Sha137994 			    sizeof (vio_dring_entry_hdr_t)));
63745b7cb889Sha137994 			vdp->dring_task[i].request = NULL;
63753af08d82Slm66018 			kmem_free(vdp->dring_task[i].msg, vdp->max_msglen);
63763af08d82Slm66018 			vdp->dring_task[i].msg = NULL;
63773af08d82Slm66018 		}
63783af08d82Slm66018 		kmem_free(vdp->dring_task,
63793af08d82Slm66018 		    (sizeof (*vdp->dring_task)) * vdp->dring_len);
63803af08d82Slm66018 		vdp->dring_task = NULL;
63813af08d82Slm66018 	}
63823af08d82Slm66018 }
63833af08d82Slm66018 
63841ae08745Sheppo /*
63851ae08745Sheppo  * Destroy the state associated with a virtual disk
63861ae08745Sheppo  */
63871ae08745Sheppo static void
63881ae08745Sheppo vds_destroy_vd(void *arg)
63891ae08745Sheppo {
63901ae08745Sheppo 	vd_t	*vd = (vd_t *)arg;
639134683adeSsg70180 	int	retry = 0, rv;
63921ae08745Sheppo 
63931ae08745Sheppo 	if (vd == NULL)
63941ae08745Sheppo 		return;
63951ae08745Sheppo 
6396d10e4ef2Snarayan 	PR0("Destroying vdisk state");
6397d10e4ef2Snarayan 
63981ae08745Sheppo 	/* Disable queuing requests for the vdisk */
63991ae08745Sheppo 	if (vd->initialized & VD_LOCKING) {
64001ae08745Sheppo 		mutex_enter(&vd->lock);
64011ae08745Sheppo 		vd->enabled = 0;
64021ae08745Sheppo 		mutex_exit(&vd->lock);
64031ae08745Sheppo 	}
64041ae08745Sheppo 
6405d10e4ef2Snarayan 	/* Drain and destroy start queue (*before* destroying completionq) */
6406d10e4ef2Snarayan 	if (vd->startq != NULL)
6407d10e4ef2Snarayan 		ddi_taskq_destroy(vd->startq);	/* waits for queued tasks */
6408d10e4ef2Snarayan 
6409d10e4ef2Snarayan 	/* Drain and destroy completion queue (*before* shutting down LDC) */
6410d10e4ef2Snarayan 	if (vd->completionq != NULL)
6411d10e4ef2Snarayan 		ddi_taskq_destroy(vd->completionq);	/* waits for tasks */
6412d10e4ef2Snarayan 
64133af08d82Slm66018 	vd_free_dring_task(vd);
64143af08d82Slm66018 
641534683adeSsg70180 	/* Free the inband task memory handle */
641634683adeSsg70180 	(void) ldc_mem_free_handle(vd->inband_task.mhdl);
641734683adeSsg70180 
641834683adeSsg70180 	/* Shut down LDC */
641934683adeSsg70180 	if (vd->initialized & VD_LDC) {
642034683adeSsg70180 		/* unmap the dring */
642134683adeSsg70180 		if (vd->initialized & VD_DRING)
642234683adeSsg70180 			(void) ldc_mem_dring_unmap(vd->dring_handle);
642334683adeSsg70180 
642434683adeSsg70180 		/* close LDC channel - retry on EAGAIN */
642534683adeSsg70180 		while ((rv = ldc_close(vd->ldc_handle)) == EAGAIN) {
642634683adeSsg70180 			if (++retry > vds_ldc_retries) {
642734683adeSsg70180 				PR0("Timed out closing channel");
642834683adeSsg70180 				break;
642934683adeSsg70180 			}
643034683adeSsg70180 			drv_usecwait(vds_ldc_delay);
643134683adeSsg70180 		}
643234683adeSsg70180 		if (rv == 0) {
643334683adeSsg70180 			(void) ldc_unreg_callback(vd->ldc_handle);
643434683adeSsg70180 			(void) ldc_fini(vd->ldc_handle);
643534683adeSsg70180 		} else {
643634683adeSsg70180 			/*
643734683adeSsg70180 			 * Closing the LDC channel has failed. Ideally we should
643834683adeSsg70180 			 * fail here but there is no Zeus level infrastructure
643934683adeSsg70180 			 * to handle this. The MD has already been changed and
644034683adeSsg70180 			 * we have to do the close. So we try to do as much
644134683adeSsg70180 			 * clean up as we can.
644234683adeSsg70180 			 */
644334683adeSsg70180 			(void) ldc_set_cb_mode(vd->ldc_handle, LDC_CB_DISABLE);
644434683adeSsg70180 			while (ldc_unreg_callback(vd->ldc_handle) == EAGAIN)
644534683adeSsg70180 				drv_usecwait(vds_ldc_delay);
644634683adeSsg70180 		}
644734683adeSsg70180 	}
644834683adeSsg70180 
64493af08d82Slm66018 	/* Free the staging buffer for msgs */
64503af08d82Slm66018 	if (vd->vio_msgp != NULL) {
64513af08d82Slm66018 		kmem_free(vd->vio_msgp, vd->max_msglen);
64523af08d82Slm66018 		vd->vio_msgp = NULL;
64533af08d82Slm66018 	}
64543af08d82Slm66018 
64553af08d82Slm66018 	/* Free the inband message buffer */
64563af08d82Slm66018 	if (vd->inband_task.msg != NULL) {
64573af08d82Slm66018 		kmem_free(vd->inband_task.msg, vd->max_msglen);
64583af08d82Slm66018 		vd->inband_task.msg = NULL;
6459d10e4ef2Snarayan 	}
6460da6c28aaSamw 
64613c96341aSnarayan 	if (vd->file) {
6462690555a1Sachartre 		/* Close file */
6463047ba61eSachartre 		(void) VOP_CLOSE(vd->file_vnode, vd->open_flags, 1,
6464da6c28aaSamw 		    0, kcred, NULL);
64653c96341aSnarayan 		VN_RELE(vd->file_vnode);
646687a7269eSachartre 		if (vd->file_devid != NULL)
646787a7269eSachartre 			ddi_devid_free(vd->file_devid);
64683c96341aSnarayan 	} else {
64691ae08745Sheppo 		/* Close any open backing-device slices */
6470bae9e67eSachartre 		for (uint_t slice = 0; slice < V_NUMPAR; slice++) {
64711ae08745Sheppo 			if (vd->ldi_handle[slice] != NULL) {
64721ae08745Sheppo 				PR0("Closing slice %u", slice);
64731ae08745Sheppo 				(void) ldi_close(vd->ldi_handle[slice],
6474047ba61eSachartre 				    vd->open_flags, kcred);
64751ae08745Sheppo 			}
64761ae08745Sheppo 		}
64773c96341aSnarayan 	}
64781ae08745Sheppo 
6479bae9e67eSachartre 	/* Free any fake label */
6480bae9e67eSachartre 	if (vd->flabel) {
6481bae9e67eSachartre 		kmem_free(vd->flabel, vd->flabel_size);
6482bae9e67eSachartre 		vd->flabel = NULL;
6483bae9e67eSachartre 		vd->flabel_size = 0;
6484bae9e67eSachartre 	}
6485bae9e67eSachartre 
64861ae08745Sheppo 	/* Free lock */
64871ae08745Sheppo 	if (vd->initialized & VD_LOCKING)
64881ae08745Sheppo 		mutex_destroy(&vd->lock);
64891ae08745Sheppo 
64901ae08745Sheppo 	/* Finally, free the vdisk structure itself */
64911ae08745Sheppo 	kmem_free(vd, sizeof (*vd));
64921ae08745Sheppo }
64931ae08745Sheppo 
64941ae08745Sheppo static int
6495047ba61eSachartre vds_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options,
6496047ba61eSachartre     uint64_t ldc_id)
64971ae08745Sheppo {
64981ae08745Sheppo 	int	status;
64991ae08745Sheppo 	vd_t	*vd = NULL;
65001ae08745Sheppo 
65011ae08745Sheppo 
6502047ba61eSachartre 	if ((status = vds_do_init_vd(vds, id, device_path, options,
6503047ba61eSachartre 	    ldc_id, &vd)) != 0)
65041ae08745Sheppo 		vds_destroy_vd(vd);
65051ae08745Sheppo 
65061ae08745Sheppo 	return (status);
65071ae08745Sheppo }
65081ae08745Sheppo 
65091ae08745Sheppo static int
65101ae08745Sheppo vds_do_get_ldc_id(md_t *md, mde_cookie_t vd_node, mde_cookie_t *channel,
65111ae08745Sheppo     uint64_t *ldc_id)
65121ae08745Sheppo {
65131ae08745Sheppo 	int	num_channels;
65141ae08745Sheppo 
65151ae08745Sheppo 
65161ae08745Sheppo 	/* Look for channel endpoint child(ren) of the vdisk MD node */
65171ae08745Sheppo 	if ((num_channels = md_scan_dag(md, vd_node,
65181ae08745Sheppo 	    md_find_name(md, VD_CHANNEL_ENDPOINT),
65191ae08745Sheppo 	    md_find_name(md, "fwd"), channel)) <= 0) {
65201ae08745Sheppo 		PRN("No \"%s\" found for virtual disk", VD_CHANNEL_ENDPOINT);
65211ae08745Sheppo 		return (-1);
65221ae08745Sheppo 	}
65231ae08745Sheppo 
65241ae08745Sheppo 	/* Get the "id" value for the first channel endpoint node */
65251ae08745Sheppo 	if (md_get_prop_val(md, channel[0], VD_ID_PROP, ldc_id) != 0) {
65261ae08745Sheppo 		PRN("No \"%s\" property found for \"%s\" of vdisk",
65271ae08745Sheppo 		    VD_ID_PROP, VD_CHANNEL_ENDPOINT);
65281ae08745Sheppo 		return (-1);
65291ae08745Sheppo 	}
65301ae08745Sheppo 
65311ae08745Sheppo 	if (num_channels > 1) {
65321ae08745Sheppo 		PRN("Using ID of first of multiple channels for this vdisk");
65331ae08745Sheppo 	}
65341ae08745Sheppo 
65351ae08745Sheppo 	return (0);
65361ae08745Sheppo }
65371ae08745Sheppo 
65381ae08745Sheppo static int
65391ae08745Sheppo vds_get_ldc_id(md_t *md, mde_cookie_t vd_node, uint64_t *ldc_id)
65401ae08745Sheppo {
65411ae08745Sheppo 	int		num_nodes, status;
65421ae08745Sheppo 	size_t		size;
65431ae08745Sheppo 	mde_cookie_t	*channel;
65441ae08745Sheppo 
65451ae08745Sheppo 
65461ae08745Sheppo 	if ((num_nodes = md_node_count(md)) <= 0) {
65471ae08745Sheppo 		PRN("Invalid node count in Machine Description subtree");
65481ae08745Sheppo 		return (-1);
65491ae08745Sheppo 	}
65501ae08745Sheppo 	size = num_nodes*(sizeof (*channel));
65511ae08745Sheppo 	channel = kmem_zalloc(size, KM_SLEEP);
65521ae08745Sheppo 	status = vds_do_get_ldc_id(md, vd_node, channel, ldc_id);
65531ae08745Sheppo 	kmem_free(channel, size);
65541ae08745Sheppo 
65551ae08745Sheppo 	return (status);
65561ae08745Sheppo }
65571ae08745Sheppo 
6558047ba61eSachartre /*
6559047ba61eSachartre  * Function:
6560047ba61eSachartre  *	vds_get_options
6561047ba61eSachartre  *
6562047ba61eSachartre  * Description:
6563047ba61eSachartre  * 	Parse the options of a vds node. Options are defined as an array
6564047ba61eSachartre  *	of strings in the vds-block-device-opts property of the vds node
6565047ba61eSachartre  *	in the machine description. Options are returned as a bitmask. The
6566047ba61eSachartre  *	mapping between the bitmask options and the options strings from the
6567047ba61eSachartre  *	machine description is defined in the vd_bdev_options[] array.
6568047ba61eSachartre  *
6569047ba61eSachartre  *	The vds-block-device-opts property is optional. If a vds has no such
6570047ba61eSachartre  *	property then no option is defined.
6571047ba61eSachartre  *
6572047ba61eSachartre  * Parameters:
6573047ba61eSachartre  *	md		- machine description.
6574047ba61eSachartre  *	vd_node		- vds node in the machine description for which
6575047ba61eSachartre  *			  options have to be parsed.
6576047ba61eSachartre  *	options		- the returned options.
6577047ba61eSachartre  *
6578047ba61eSachartre  * Return Code:
6579047ba61eSachartre  *	none.
6580047ba61eSachartre  */
6581047ba61eSachartre static void
6582047ba61eSachartre vds_get_options(md_t *md, mde_cookie_t vd_node, uint64_t *options)
6583047ba61eSachartre {
6584047ba61eSachartre 	char	*optstr, *opt;
6585047ba61eSachartre 	int	len, n, i;
6586047ba61eSachartre 
6587047ba61eSachartre 	*options = 0;
6588047ba61eSachartre 
6589047ba61eSachartre 	if (md_get_prop_data(md, vd_node, VD_BLOCK_DEVICE_OPTS,
6590047ba61eSachartre 	    (uint8_t **)&optstr, &len) != 0) {
6591047ba61eSachartre 		PR0("No options found");
6592047ba61eSachartre 		return;
6593047ba61eSachartre 	}
6594047ba61eSachartre 
6595047ba61eSachartre 	/* parse options */
6596047ba61eSachartre 	opt = optstr;
6597047ba61eSachartre 	n = sizeof (vd_bdev_options) / sizeof (vd_option_t);
6598047ba61eSachartre 
6599047ba61eSachartre 	while (opt < optstr + len) {
6600047ba61eSachartre 		for (i = 0; i < n; i++) {
6601047ba61eSachartre 			if (strncmp(vd_bdev_options[i].vdo_name,
6602047ba61eSachartre 			    opt, VD_OPTION_NLEN) == 0) {
6603047ba61eSachartre 				*options |= vd_bdev_options[i].vdo_value;
6604047ba61eSachartre 				break;
6605047ba61eSachartre 			}
6606047ba61eSachartre 		}
6607047ba61eSachartre 
6608047ba61eSachartre 		if (i < n) {
6609047ba61eSachartre 			PR0("option: %s", opt);
6610047ba61eSachartre 		} else {
6611047ba61eSachartre 			PRN("option %s is unknown or unsupported", opt);
6612047ba61eSachartre 		}
6613047ba61eSachartre 
6614047ba61eSachartre 		opt += strlen(opt) + 1;
6615047ba61eSachartre 	}
6616047ba61eSachartre }
6617047ba61eSachartre 
66181ae08745Sheppo static void
66198fce2fd6Sachartre vds_driver_types_free(vds_t *vds)
66208fce2fd6Sachartre {
66218fce2fd6Sachartre 	if (vds->driver_types != NULL) {
66228fce2fd6Sachartre 		kmem_free(vds->driver_types, sizeof (vd_driver_type_t) *
66238fce2fd6Sachartre 		    vds->num_drivers);
66248fce2fd6Sachartre 		vds->driver_types = NULL;
66258fce2fd6Sachartre 		vds->num_drivers = 0;
66268fce2fd6Sachartre 	}
66278fce2fd6Sachartre }
66288fce2fd6Sachartre 
66298fce2fd6Sachartre /*
66308fce2fd6Sachartre  * Update the driver type list with information from vds.conf.
66318fce2fd6Sachartre  */
66328fce2fd6Sachartre static void
66338fce2fd6Sachartre vds_driver_types_update(vds_t *vds)
66348fce2fd6Sachartre {
66358fce2fd6Sachartre 	char **list, *s;
66368fce2fd6Sachartre 	uint_t i, num, count = 0, len;
66378fce2fd6Sachartre 
66388fce2fd6Sachartre 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, vds->dip,
66398fce2fd6Sachartre 	    DDI_PROP_DONTPASS, "driver-type-list", &list, &num) !=
66408fce2fd6Sachartre 	    DDI_PROP_SUCCESS)
66418fce2fd6Sachartre 		return;
66428fce2fd6Sachartre 
66438fce2fd6Sachartre 	/*
66448fce2fd6Sachartre 	 * We create a driver_types list with as many as entries as there
66458fce2fd6Sachartre 	 * is in the driver-type-list from vds.conf. However only valid
66468fce2fd6Sachartre 	 * entries will be populated (i.e. entries from driver-type-list
66478fce2fd6Sachartre 	 * with a valid syntax). Invalid entries will be left blank so
66488fce2fd6Sachartre 	 * they will have no driver name and the driver type will be
66498fce2fd6Sachartre 	 * VD_DRIVER_UNKNOWN (= 0).
66508fce2fd6Sachartre 	 */
66518fce2fd6Sachartre 	vds->num_drivers = num;
66528fce2fd6Sachartre 	vds->driver_types = kmem_zalloc(sizeof (vd_driver_type_t) * num,
66538fce2fd6Sachartre 	    KM_SLEEP);
66548fce2fd6Sachartre 
66558fce2fd6Sachartre 	for (i = 0; i < num; i++) {
66568fce2fd6Sachartre 
66578fce2fd6Sachartre 		s = strchr(list[i], ':');
66588fce2fd6Sachartre 
66598fce2fd6Sachartre 		if (s == NULL) {
66608fce2fd6Sachartre 			PRN("vds.conf: driver-type-list, entry %d (%s): "
66618fce2fd6Sachartre 			    "a colon is expected in the entry",
66628fce2fd6Sachartre 			    i, list[i]);
66638fce2fd6Sachartre 			continue;
66648fce2fd6Sachartre 		}
66658fce2fd6Sachartre 
66668fce2fd6Sachartre 		len = (uintptr_t)s - (uintptr_t)list[i];
66678fce2fd6Sachartre 
66688fce2fd6Sachartre 		if (len == 0) {
66698fce2fd6Sachartre 			PRN("vds.conf: driver-type-list, entry %d (%s): "
66708fce2fd6Sachartre 			    "the driver name is empty",
66718fce2fd6Sachartre 			    i, list[i]);
66728fce2fd6Sachartre 			continue;
66738fce2fd6Sachartre 		}
66748fce2fd6Sachartre 
66758fce2fd6Sachartre 		if (len >= VD_DRIVER_NAME_LEN) {
66768fce2fd6Sachartre 			PRN("vds.conf: driver-type-list, entry %d (%s): "
66778fce2fd6Sachartre 			    "the driver name is too long",
66788fce2fd6Sachartre 			    i, list[i]);
66798fce2fd6Sachartre 			continue;
66808fce2fd6Sachartre 		}
66818fce2fd6Sachartre 
66828fce2fd6Sachartre 		if (strcmp(s + 1, "disk") == 0) {
66838fce2fd6Sachartre 
66848fce2fd6Sachartre 			vds->driver_types[i].type = VD_DRIVER_DISK;
66858fce2fd6Sachartre 
66868fce2fd6Sachartre 		} else if (strcmp(s + 1, "volume") == 0) {
66878fce2fd6Sachartre 
66888fce2fd6Sachartre 			vds->driver_types[i].type = VD_DRIVER_VOLUME;
66898fce2fd6Sachartre 
66908fce2fd6Sachartre 		} else {
66918fce2fd6Sachartre 			PRN("vds.conf: driver-type-list, entry %d (%s): "
66928fce2fd6Sachartre 			    "the driver type is invalid",
66938fce2fd6Sachartre 			    i, list[i]);
66948fce2fd6Sachartre 			continue;
66958fce2fd6Sachartre 		}
66968fce2fd6Sachartre 
66978fce2fd6Sachartre 		(void) strncpy(vds->driver_types[i].name, list[i], len);
66988fce2fd6Sachartre 
66998fce2fd6Sachartre 		PR0("driver-type-list, entry %d (%s) added",
67008fce2fd6Sachartre 		    i, list[i]);
67018fce2fd6Sachartre 
67028fce2fd6Sachartre 		count++;
67038fce2fd6Sachartre 	}
67048fce2fd6Sachartre 
67058fce2fd6Sachartre 	ddi_prop_free(list);
67068fce2fd6Sachartre 
67078fce2fd6Sachartre 	if (count == 0) {
67088fce2fd6Sachartre 		/* nothing was added, clean up */
67098fce2fd6Sachartre 		vds_driver_types_free(vds);
67108fce2fd6Sachartre 	}
67118fce2fd6Sachartre }
67128fce2fd6Sachartre 
67138fce2fd6Sachartre static void
67141ae08745Sheppo vds_add_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
67151ae08745Sheppo {
6716e1ebb9ecSlm66018 	char		*device_path = NULL;
6717047ba61eSachartre 	uint64_t	id = 0, ldc_id = 0, options = 0;
67181ae08745Sheppo 
67191ae08745Sheppo 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
67201ae08745Sheppo 		PRN("Error getting vdisk \"%s\"", VD_ID_PROP);
67211ae08745Sheppo 		return;
67221ae08745Sheppo 	}
67231ae08745Sheppo 	PR0("Adding vdisk ID %lu", id);
67241ae08745Sheppo 	if (md_get_prop_str(md, vd_node, VD_BLOCK_DEVICE_PROP,
6725e1ebb9ecSlm66018 	    &device_path) != 0) {
67261ae08745Sheppo 		PRN("Error getting vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
67271ae08745Sheppo 		return;
67281ae08745Sheppo 	}
67291ae08745Sheppo 
6730047ba61eSachartre 	vds_get_options(md, vd_node, &options);
6731047ba61eSachartre 
67321ae08745Sheppo 	if (vds_get_ldc_id(md, vd_node, &ldc_id) != 0) {
67331ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", id);
67341ae08745Sheppo 		return;
67351ae08745Sheppo 	}
67361ae08745Sheppo 
6737047ba61eSachartre 	if (vds_init_vd(vds, id, device_path, options, ldc_id) != 0) {
67381ae08745Sheppo 		PRN("Failed to add vdisk ID %lu", id);
673917cadca8Slm66018 		if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0)
674017cadca8Slm66018 			PRN("No vDisk entry found for vdisk ID %lu", id);
67411ae08745Sheppo 		return;
67421ae08745Sheppo 	}
67431ae08745Sheppo }
67441ae08745Sheppo 
67451ae08745Sheppo static void
67461ae08745Sheppo vds_remove_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
67471ae08745Sheppo {
67481ae08745Sheppo 	uint64_t	id = 0;
67491ae08745Sheppo 
67501ae08745Sheppo 
67511ae08745Sheppo 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
67521ae08745Sheppo 		PRN("Unable to get \"%s\" property from vdisk's MD node",
67531ae08745Sheppo 		    VD_ID_PROP);
67541ae08745Sheppo 		return;
67551ae08745Sheppo 	}
67561ae08745Sheppo 	PR0("Removing vdisk ID %lu", id);
67571ae08745Sheppo 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0)
67581ae08745Sheppo 		PRN("No vdisk entry found for vdisk ID %lu", id);
67591ae08745Sheppo }
67601ae08745Sheppo 
67611ae08745Sheppo static void
67621ae08745Sheppo vds_change_vd(vds_t *vds, md_t *prev_md, mde_cookie_t prev_vd_node,
67631ae08745Sheppo     md_t *curr_md, mde_cookie_t curr_vd_node)
67641ae08745Sheppo {
67651ae08745Sheppo 	char		*curr_dev, *prev_dev;
6766047ba61eSachartre 	uint64_t	curr_id = 0, curr_ldc_id = 0, curr_options = 0;
6767047ba61eSachartre 	uint64_t	prev_id = 0, prev_ldc_id = 0, prev_options = 0;
67681ae08745Sheppo 	size_t		len;
67691ae08745Sheppo 
67701ae08745Sheppo 
67711ae08745Sheppo 	/* Validate that vdisk ID has not changed */
67721ae08745Sheppo 	if (md_get_prop_val(prev_md, prev_vd_node, VD_ID_PROP, &prev_id) != 0) {
67731ae08745Sheppo 		PRN("Error getting previous vdisk \"%s\" property",
67741ae08745Sheppo 		    VD_ID_PROP);
67751ae08745Sheppo 		return;
67761ae08745Sheppo 	}
67771ae08745Sheppo 	if (md_get_prop_val(curr_md, curr_vd_node, VD_ID_PROP, &curr_id) != 0) {
67781ae08745Sheppo 		PRN("Error getting current vdisk \"%s\" property", VD_ID_PROP);
67791ae08745Sheppo 		return;
67801ae08745Sheppo 	}
67811ae08745Sheppo 	if (curr_id != prev_id) {
67821ae08745Sheppo 		PRN("Not changing vdisk:  ID changed from %lu to %lu",
67831ae08745Sheppo 		    prev_id, curr_id);
67841ae08745Sheppo 		return;
67851ae08745Sheppo 	}
67861ae08745Sheppo 
67871ae08745Sheppo 	/* Validate that LDC ID has not changed */
67881ae08745Sheppo 	if (vds_get_ldc_id(prev_md, prev_vd_node, &prev_ldc_id) != 0) {
67891ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", prev_id);
67901ae08745Sheppo 		return;
67911ae08745Sheppo 	}
67921ae08745Sheppo 
67931ae08745Sheppo 	if (vds_get_ldc_id(curr_md, curr_vd_node, &curr_ldc_id) != 0) {
67941ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", curr_id);
67951ae08745Sheppo 		return;
67961ae08745Sheppo 	}
67971ae08745Sheppo 	if (curr_ldc_id != prev_ldc_id) {
67980a55fbb7Slm66018 		_NOTE(NOTREACHED);	/* lint is confused */
67991ae08745Sheppo 		PRN("Not changing vdisk:  "
68001ae08745Sheppo 		    "LDC ID changed from %lu to %lu", prev_ldc_id, curr_ldc_id);
68011ae08745Sheppo 		return;
68021ae08745Sheppo 	}
68031ae08745Sheppo 
68041ae08745Sheppo 	/* Determine whether device path has changed */
68051ae08745Sheppo 	if (md_get_prop_str(prev_md, prev_vd_node, VD_BLOCK_DEVICE_PROP,
68061ae08745Sheppo 	    &prev_dev) != 0) {
68071ae08745Sheppo 		PRN("Error getting previous vdisk \"%s\"",
68081ae08745Sheppo 		    VD_BLOCK_DEVICE_PROP);
68091ae08745Sheppo 		return;
68101ae08745Sheppo 	}
68111ae08745Sheppo 	if (md_get_prop_str(curr_md, curr_vd_node, VD_BLOCK_DEVICE_PROP,
68121ae08745Sheppo 	    &curr_dev) != 0) {
68131ae08745Sheppo 		PRN("Error getting current vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
68141ae08745Sheppo 		return;
68151ae08745Sheppo 	}
68161ae08745Sheppo 	if (((len = strlen(curr_dev)) == strlen(prev_dev)) &&
68171ae08745Sheppo 	    (strncmp(curr_dev, prev_dev, len) == 0))
68181ae08745Sheppo 		return;	/* no relevant (supported) change */
68191ae08745Sheppo 
6820047ba61eSachartre 	/* Validate that options have not changed */
6821047ba61eSachartre 	vds_get_options(prev_md, prev_vd_node, &prev_options);
6822047ba61eSachartre 	vds_get_options(curr_md, curr_vd_node, &curr_options);
6823047ba61eSachartre 	if (prev_options != curr_options) {
6824047ba61eSachartre 		PRN("Not changing vdisk:  options changed from %lx to %lx",
6825047ba61eSachartre 		    prev_options, curr_options);
6826047ba61eSachartre 		return;
6827047ba61eSachartre 	}
6828047ba61eSachartre 
68291ae08745Sheppo 	PR0("Changing vdisk ID %lu", prev_id);
68303af08d82Slm66018 
68311ae08745Sheppo 	/* Remove old state, which will close vdisk and reset */
68321ae08745Sheppo 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)prev_id) != 0)
68331ae08745Sheppo 		PRN("No entry found for vdisk ID %lu", prev_id);
68343af08d82Slm66018 
68351ae08745Sheppo 	/* Re-initialize vdisk with new state */
6836047ba61eSachartre 	if (vds_init_vd(vds, curr_id, curr_dev, curr_options,
6837047ba61eSachartre 	    curr_ldc_id) != 0) {
68381ae08745Sheppo 		PRN("Failed to change vdisk ID %lu", curr_id);
68391ae08745Sheppo 		return;
68401ae08745Sheppo 	}
68411ae08745Sheppo }
68421ae08745Sheppo 
68431ae08745Sheppo static int
68441ae08745Sheppo vds_process_md(void *arg, mdeg_result_t *md)
68451ae08745Sheppo {
68461ae08745Sheppo 	int	i;
68471ae08745Sheppo 	vds_t	*vds = arg;
68481ae08745Sheppo 
68491ae08745Sheppo 
68501ae08745Sheppo 	if (md == NULL)
68511ae08745Sheppo 		return (MDEG_FAILURE);
68521ae08745Sheppo 	ASSERT(vds != NULL);
68531ae08745Sheppo 
68541ae08745Sheppo 	for (i = 0; i < md->removed.nelem; i++)
68551ae08745Sheppo 		vds_remove_vd(vds, md->removed.mdp, md->removed.mdep[i]);
68561ae08745Sheppo 	for (i = 0; i < md->match_curr.nelem; i++)
68571ae08745Sheppo 		vds_change_vd(vds, md->match_prev.mdp, md->match_prev.mdep[i],
68581ae08745Sheppo 		    md->match_curr.mdp, md->match_curr.mdep[i]);
68591ae08745Sheppo 	for (i = 0; i < md->added.nelem; i++)
68601ae08745Sheppo 		vds_add_vd(vds, md->added.mdp, md->added.mdep[i]);
68611ae08745Sheppo 
68621ae08745Sheppo 	return (MDEG_SUCCESS);
68631ae08745Sheppo }
68641ae08745Sheppo 
68653c96341aSnarayan 
68661ae08745Sheppo static int
68671ae08745Sheppo vds_do_attach(dev_info_t *dip)
68681ae08745Sheppo {
6869445b4c2eSsb155480 	int			status, sz;
6870445b4c2eSsb155480 	int			cfg_handle;
68711ae08745Sheppo 	minor_t			instance = ddi_get_instance(dip);
68721ae08745Sheppo 	vds_t			*vds;
6873445b4c2eSsb155480 	mdeg_prop_spec_t	*pspecp;
6874445b4c2eSsb155480 	mdeg_node_spec_t	*ispecp;
68751ae08745Sheppo 
68761ae08745Sheppo 	/*
68771ae08745Sheppo 	 * The "cfg-handle" property of a vds node in an MD contains the MD's
68781ae08745Sheppo 	 * notion of "instance", or unique identifier, for that node; OBP
68791ae08745Sheppo 	 * stores the value of the "cfg-handle" MD property as the value of
68801ae08745Sheppo 	 * the "reg" property on the node in the device tree it builds from
68811ae08745Sheppo 	 * the MD and passes to Solaris.  Thus, we look up the devinfo node's
68821ae08745Sheppo 	 * "reg" property value to uniquely identify this device instance when
68831ae08745Sheppo 	 * registering with the MD event-generation framework.  If the "reg"
68841ae08745Sheppo 	 * property cannot be found, the device tree state is presumably so
68851ae08745Sheppo 	 * broken that there is no point in continuing.
68861ae08745Sheppo 	 */
6887445b4c2eSsb155480 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
6888445b4c2eSsb155480 	    VD_REG_PROP)) {
6889445b4c2eSsb155480 		PRN("vds \"%s\" property does not exist", VD_REG_PROP);
68901ae08745Sheppo 		return (DDI_FAILURE);
68911ae08745Sheppo 	}
68921ae08745Sheppo 
68931ae08745Sheppo 	/* Get the MD instance for later MDEG registration */
68941ae08745Sheppo 	cfg_handle = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
6895445b4c2eSsb155480 	    VD_REG_PROP, -1);
68961ae08745Sheppo 
68971ae08745Sheppo 	if (ddi_soft_state_zalloc(vds_state, instance) != DDI_SUCCESS) {
68981ae08745Sheppo 		PRN("Could not allocate state for instance %u", instance);
68991ae08745Sheppo 		return (DDI_FAILURE);
69001ae08745Sheppo 	}
69011ae08745Sheppo 
69021ae08745Sheppo 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
69031ae08745Sheppo 		PRN("Could not get state for instance %u", instance);
69041ae08745Sheppo 		ddi_soft_state_free(vds_state, instance);
69051ae08745Sheppo 		return (DDI_FAILURE);
69061ae08745Sheppo 	}
69071ae08745Sheppo 
69081ae08745Sheppo 	vds->dip	= dip;
69091ae08745Sheppo 	vds->vd_table	= mod_hash_create_ptrhash("vds_vd_table", VDS_NCHAINS,
691087a7269eSachartre 	    vds_destroy_vd, sizeof (void *));
691187a7269eSachartre 
69121ae08745Sheppo 	ASSERT(vds->vd_table != NULL);
69131ae08745Sheppo 
69141ae08745Sheppo 	if ((status = ldi_ident_from_dip(dip, &vds->ldi_ident)) != 0) {
69151ae08745Sheppo 		PRN("ldi_ident_from_dip() returned errno %d", status);
69161ae08745Sheppo 		return (DDI_FAILURE);
69171ae08745Sheppo 	}
69181ae08745Sheppo 	vds->initialized |= VDS_LDI;
69191ae08745Sheppo 
69201ae08745Sheppo 	/* Register for MD updates */
6921445b4c2eSsb155480 	sz = sizeof (vds_prop_template);
6922445b4c2eSsb155480 	pspecp = kmem_alloc(sz, KM_SLEEP);
6923445b4c2eSsb155480 	bcopy(vds_prop_template, pspecp, sz);
6924445b4c2eSsb155480 
6925445b4c2eSsb155480 	VDS_SET_MDEG_PROP_INST(pspecp, cfg_handle);
6926445b4c2eSsb155480 
6927445b4c2eSsb155480 	/* initialize the complete prop spec structure */
6928445b4c2eSsb155480 	ispecp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP);
6929445b4c2eSsb155480 	ispecp->namep = "virtual-device";
6930445b4c2eSsb155480 	ispecp->specp = pspecp;
6931445b4c2eSsb155480 
6932445b4c2eSsb155480 	if (mdeg_register(ispecp, &vd_match, vds_process_md, vds,
69331ae08745Sheppo 	    &vds->mdeg) != MDEG_SUCCESS) {
69341ae08745Sheppo 		PRN("Unable to register for MD updates");
6935445b4c2eSsb155480 		kmem_free(ispecp, sizeof (mdeg_node_spec_t));
6936445b4c2eSsb155480 		kmem_free(pspecp, sz);
69371ae08745Sheppo 		return (DDI_FAILURE);
69381ae08745Sheppo 	}
6939445b4c2eSsb155480 
6940445b4c2eSsb155480 	vds->ispecp = ispecp;
69411ae08745Sheppo 	vds->initialized |= VDS_MDEG;
69421ae08745Sheppo 
69430a55fbb7Slm66018 	/* Prevent auto-detaching so driver is available whenever MD changes */
69440a55fbb7Slm66018 	if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1) !=
69450a55fbb7Slm66018 	    DDI_PROP_SUCCESS) {
69460a55fbb7Slm66018 		PRN("failed to set \"%s\" property for instance %u",
69470a55fbb7Slm66018 		    DDI_NO_AUTODETACH, instance);
69480a55fbb7Slm66018 	}
69490a55fbb7Slm66018 
69508fce2fd6Sachartre 	/* read any user defined driver types from conf file and update list */
69518fce2fd6Sachartre 	vds_driver_types_update(vds);
69528fce2fd6Sachartre 
69531ae08745Sheppo 	ddi_report_dev(dip);
69541ae08745Sheppo 	return (DDI_SUCCESS);
69551ae08745Sheppo }
69561ae08745Sheppo 
69571ae08745Sheppo static int
69581ae08745Sheppo vds_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
69591ae08745Sheppo {
69601ae08745Sheppo 	int	status;
69611ae08745Sheppo 
69621ae08745Sheppo 	switch (cmd) {
69631ae08745Sheppo 	case DDI_ATTACH:
6964d10e4ef2Snarayan 		PR0("Attaching");
69651ae08745Sheppo 		if ((status = vds_do_attach(dip)) != DDI_SUCCESS)
69661ae08745Sheppo 			(void) vds_detach(dip, DDI_DETACH);
69671ae08745Sheppo 		return (status);
69681ae08745Sheppo 	case DDI_RESUME:
6969d10e4ef2Snarayan 		PR0("No action required for DDI_RESUME");
69701ae08745Sheppo 		return (DDI_SUCCESS);
69711ae08745Sheppo 	default:
69721ae08745Sheppo 		return (DDI_FAILURE);
69731ae08745Sheppo 	}
69741ae08745Sheppo }
69751ae08745Sheppo 
69761ae08745Sheppo static struct dev_ops vds_ops = {
69771ae08745Sheppo 	DEVO_REV,	/* devo_rev */
69781ae08745Sheppo 	0,		/* devo_refcnt */
69791ae08745Sheppo 	ddi_no_info,	/* devo_getinfo */
69801ae08745Sheppo 	nulldev,	/* devo_identify */
69811ae08745Sheppo 	nulldev,	/* devo_probe */
69821ae08745Sheppo 	vds_attach,	/* devo_attach */
69831ae08745Sheppo 	vds_detach,	/* devo_detach */
69841ae08745Sheppo 	nodev,		/* devo_reset */
69851ae08745Sheppo 	NULL,		/* devo_cb_ops */
69861ae08745Sheppo 	NULL,		/* devo_bus_ops */
69871ae08745Sheppo 	nulldev		/* devo_power */
69881ae08745Sheppo };
69891ae08745Sheppo 
69901ae08745Sheppo static struct modldrv modldrv = {
69911ae08745Sheppo 	&mod_driverops,
6992205eeb1aSlm66018 	"virtual disk server",
69931ae08745Sheppo 	&vds_ops,
69941ae08745Sheppo };
69951ae08745Sheppo 
69961ae08745Sheppo static struct modlinkage modlinkage = {
69971ae08745Sheppo 	MODREV_1,
69981ae08745Sheppo 	&modldrv,
69991ae08745Sheppo 	NULL
70001ae08745Sheppo };
70011ae08745Sheppo 
70021ae08745Sheppo 
70031ae08745Sheppo int
70041ae08745Sheppo _init(void)
70051ae08745Sheppo {
700617cadca8Slm66018 	int		status;
7007d10e4ef2Snarayan 
70081ae08745Sheppo 	if ((status = ddi_soft_state_init(&vds_state, sizeof (vds_t), 1)) != 0)
70091ae08745Sheppo 		return (status);
701017cadca8Slm66018 
70111ae08745Sheppo 	if ((status = mod_install(&modlinkage)) != 0) {
70121ae08745Sheppo 		ddi_soft_state_fini(&vds_state);
70131ae08745Sheppo 		return (status);
70141ae08745Sheppo 	}
70151ae08745Sheppo 
70161ae08745Sheppo 	return (0);
70171ae08745Sheppo }
70181ae08745Sheppo 
70191ae08745Sheppo int
70201ae08745Sheppo _info(struct modinfo *modinfop)
70211ae08745Sheppo {
70221ae08745Sheppo 	return (mod_info(&modlinkage, modinfop));
70231ae08745Sheppo }
70241ae08745Sheppo 
70251ae08745Sheppo int
70261ae08745Sheppo _fini(void)
70271ae08745Sheppo {
70281ae08745Sheppo 	int	status;
70291ae08745Sheppo 
70301ae08745Sheppo 	if ((status = mod_remove(&modlinkage)) != 0)
70311ae08745Sheppo 		return (status);
70321ae08745Sheppo 	ddi_soft_state_fini(&vds_state);
70331ae08745Sheppo 	return (0);
70341ae08745Sheppo }
7035