xref: /titanic_53/usr/src/uts/sun4v/io/vds.c (revision 14466a2038ac6d83147652d504c01d04b4f9a9df)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
281ae08745Sheppo 
291ae08745Sheppo /*
301ae08745Sheppo  * Virtual disk server
311ae08745Sheppo  */
321ae08745Sheppo 
331ae08745Sheppo 
341ae08745Sheppo #include <sys/types.h>
351ae08745Sheppo #include <sys/conf.h>
364bac2208Snarayan #include <sys/crc32.h>
371ae08745Sheppo #include <sys/ddi.h>
381ae08745Sheppo #include <sys/dkio.h>
391ae08745Sheppo #include <sys/file.h>
4017cadca8Slm66018 #include <sys/fs/hsfs_isospec.h>
411ae08745Sheppo #include <sys/mdeg.h>
422f5224aeSachartre #include <sys/mhd.h>
431ae08745Sheppo #include <sys/modhash.h>
441ae08745Sheppo #include <sys/note.h>
451ae08745Sheppo #include <sys/pathname.h>
46205eeb1aSlm66018 #include <sys/sdt.h>
471ae08745Sheppo #include <sys/sunddi.h>
481ae08745Sheppo #include <sys/sunldi.h>
491ae08745Sheppo #include <sys/sysmacros.h>
501ae08745Sheppo #include <sys/vio_common.h>
5117cadca8Slm66018 #include <sys/vio_util.h>
521ae08745Sheppo #include <sys/vdsk_mailbox.h>
531ae08745Sheppo #include <sys/vdsk_common.h>
541ae08745Sheppo #include <sys/vtoc.h>
553c96341aSnarayan #include <sys/vfs.h>
563c96341aSnarayan #include <sys/stat.h>
5787a7269eSachartre #include <sys/scsi/impl/uscsi.h>
58bbfa0259Sha137994 #include <sys/ontrap.h>
59690555a1Sachartre #include <vm/seg_map.h>
601ae08745Sheppo 
61bae9e67eSachartre #define	ONE_TERABYTE	(1ULL << 40)
62bae9e67eSachartre 
631ae08745Sheppo /* Virtual disk server initialization flags */
64d10e4ef2Snarayan #define	VDS_LDI			0x01
65d10e4ef2Snarayan #define	VDS_MDEG		0x02
661ae08745Sheppo 
671ae08745Sheppo /* Virtual disk server tunable parameters */
683c96341aSnarayan #define	VDS_RETRIES		5
693c96341aSnarayan #define	VDS_LDC_DELAY		1000 /* 1 msecs */
703c96341aSnarayan #define	VDS_DEV_DELAY		10000000 /* 10 secs */
711ae08745Sheppo #define	VDS_NCHAINS		32
721ae08745Sheppo 
731ae08745Sheppo /* Identification parameters for MD, synthetic dkio(7i) structures, etc. */
741ae08745Sheppo #define	VDS_NAME		"virtual-disk-server"
751ae08745Sheppo 
761ae08745Sheppo #define	VD_NAME			"vd"
771ae08745Sheppo #define	VD_VOLUME_NAME		"vdisk"
781ae08745Sheppo #define	VD_ASCIILABEL		"Virtual Disk"
791ae08745Sheppo 
801ae08745Sheppo #define	VD_CHANNEL_ENDPOINT	"channel-endpoint"
811ae08745Sheppo #define	VD_ID_PROP		"id"
821ae08745Sheppo #define	VD_BLOCK_DEVICE_PROP	"vds-block-device"
83047ba61eSachartre #define	VD_BLOCK_DEVICE_OPTS	"vds-block-device-opts"
84445b4c2eSsb155480 #define	VD_REG_PROP		"reg"
851ae08745Sheppo 
861ae08745Sheppo /* Virtual disk initialization flags */
873c96341aSnarayan #define	VD_DISK_READY		0x01
883c96341aSnarayan #define	VD_LOCKING		0x02
893c96341aSnarayan #define	VD_LDC			0x04
903c96341aSnarayan #define	VD_DRING		0x08
913c96341aSnarayan #define	VD_SID			0x10
923c96341aSnarayan #define	VD_SEQ_NUM		0x20
93047ba61eSachartre #define	VD_SETUP_ERROR		0x40
941ae08745Sheppo 
95eba0cb4eSachartre /* Flags for writing to a vdisk which is a file */
96eba0cb4eSachartre #define	VD_FILE_WRITE_FLAGS	SM_ASYNC
97eba0cb4eSachartre 
9887a7269eSachartre /* Number of backup labels */
9987a7269eSachartre #define	VD_FILE_NUM_BACKUP	5
10087a7269eSachartre 
10187a7269eSachartre /* Timeout for SCSI I/O */
10287a7269eSachartre #define	VD_SCSI_RDWR_TIMEOUT	30	/* 30 secs */
10387a7269eSachartre 
104edcc0754Sachartre /* Maximum number of logical partitions */
105edcc0754Sachartre #define	VD_MAXPART	(NDKMAP + 1)
106edcc0754Sachartre 
1071ae08745Sheppo /*
1081ae08745Sheppo  * By Solaris convention, slice/partition 2 represents the entire disk;
1091ae08745Sheppo  * unfortunately, this convention does not appear to be codified.
1101ae08745Sheppo  */
1111ae08745Sheppo #define	VD_ENTIRE_DISK_SLICE	2
1121ae08745Sheppo 
113bae9e67eSachartre /* Logical block address for EFI */
114bae9e67eSachartre #define	VD_EFI_LBA_GPT		1	/* LBA of the GPT */
115bae9e67eSachartre #define	VD_EFI_LBA_GPE		2	/* LBA of the GPE */
116bae9e67eSachartre 
1178fce2fd6Sachartre /* Driver types */
1188fce2fd6Sachartre typedef enum vd_driver {
1198fce2fd6Sachartre 	VD_DRIVER_UNKNOWN = 0,	/* driver type unknown  */
1208fce2fd6Sachartre 	VD_DRIVER_DISK,		/* disk driver */
1218fce2fd6Sachartre 	VD_DRIVER_VOLUME	/* volume driver */
1228fce2fd6Sachartre } vd_driver_t;
1238fce2fd6Sachartre 
1248fce2fd6Sachartre #define	VD_DRIVER_NAME_LEN	64
1258fce2fd6Sachartre 
1268fce2fd6Sachartre #define	VDS_NUM_DRIVERS	(sizeof (vds_driver_types) / sizeof (vd_driver_type_t))
1278fce2fd6Sachartre 
1288fce2fd6Sachartre typedef struct vd_driver_type {
1298fce2fd6Sachartre 	char name[VD_DRIVER_NAME_LEN];	/* driver name */
1308fce2fd6Sachartre 	vd_driver_t type;		/* driver type (disk or volume) */
1318fce2fd6Sachartre } vd_driver_type_t;
1328fce2fd6Sachartre 
1338fce2fd6Sachartre /*
1348fce2fd6Sachartre  * There is no reliable way to determine if a device is representing a disk
1358fce2fd6Sachartre  * or a volume, especially with pseudo devices. So we maintain a list of well
1368fce2fd6Sachartre  * known drivers and the type of device they represent (either a disk or a
1378fce2fd6Sachartre  * volume).
1388fce2fd6Sachartre  *
1398fce2fd6Sachartre  * The list can be extended by adding a "driver-type-list" entry in vds.conf
1408fce2fd6Sachartre  * with the following syntax:
1418fce2fd6Sachartre  *
1428fce2fd6Sachartre  * 	driver-type-list="<driver>:<type>", ... ,"<driver>:<type>";
1438fce2fd6Sachartre  *
1448fce2fd6Sachartre  * Where:
1458fce2fd6Sachartre  *	<driver> is the name of a driver (limited to 64 characters)
1468fce2fd6Sachartre  *	<type> is either the string "disk" or "volume"
1478fce2fd6Sachartre  *
1488fce2fd6Sachartre  * Invalid entries in "driver-type-list" will be ignored.
1498fce2fd6Sachartre  *
1508fce2fd6Sachartre  * For example, the following line in vds.conf:
1518fce2fd6Sachartre  *
1528fce2fd6Sachartre  * 	driver-type-list="foo:disk","bar:volume";
1538fce2fd6Sachartre  *
1548fce2fd6Sachartre  * defines that "foo" is a disk driver, and driver "bar" is a volume driver.
1558fce2fd6Sachartre  *
1568fce2fd6Sachartre  * When a list is defined in vds.conf, it is checked before the built-in list
1578fce2fd6Sachartre  * (vds_driver_types[]) so that any definition from this list can be overriden
1588fce2fd6Sachartre  * using vds.conf.
1598fce2fd6Sachartre  */
1608fce2fd6Sachartre vd_driver_type_t vds_driver_types[] = {
1618fce2fd6Sachartre 	{ "dad",	VD_DRIVER_DISK },	/* Solaris */
1628fce2fd6Sachartre 	{ "did",	VD_DRIVER_DISK },	/* Sun Cluster */
1635b98b509Sachartre 	{ "emcp",	VD_DRIVER_DISK },	/* EMC Powerpath */
1648fce2fd6Sachartre 	{ "lofi",	VD_DRIVER_VOLUME },	/* Solaris */
1658fce2fd6Sachartre 	{ "md",		VD_DRIVER_VOLUME },	/* Solaris - SVM */
1668fce2fd6Sachartre 	{ "sd",		VD_DRIVER_DISK },	/* Solaris */
1678fce2fd6Sachartre 	{ "ssd",	VD_DRIVER_DISK },	/* Solaris */
1688fce2fd6Sachartre 	{ "vdc",	VD_DRIVER_DISK },	/* Solaris */
1698fce2fd6Sachartre 	{ "vxdmp",	VD_DRIVER_DISK },	/* Veritas */
1708fce2fd6Sachartre 	{ "vxio",	VD_DRIVER_VOLUME },	/* Veritas - VxVM */
1718fce2fd6Sachartre 	{ "zfs",	VD_DRIVER_VOLUME }	/* Solaris */
1728fce2fd6Sachartre };
1738fce2fd6Sachartre 
1741ae08745Sheppo /* Return a cpp token as a string */
1751ae08745Sheppo #define	STRINGIZE(token)	#token
1761ae08745Sheppo 
1771ae08745Sheppo /*
1781ae08745Sheppo  * Print a message prefixed with the current function name to the message log
1791ae08745Sheppo  * (and optionally to the console for verbose boots); these macros use cpp's
1801ae08745Sheppo  * concatenation of string literals and C99 variable-length-argument-list
1811ae08745Sheppo  * macros
1821ae08745Sheppo  */
1831ae08745Sheppo #define	PRN(...)	_PRN("?%s():  "__VA_ARGS__, "")
1841ae08745Sheppo #define	_PRN(format, ...)					\
1851ae08745Sheppo 	cmn_err(CE_CONT, format"%s", __func__, __VA_ARGS__)
1861ae08745Sheppo 
1871ae08745Sheppo /* Return a pointer to the "i"th vdisk dring element */
1881ae08745Sheppo #define	VD_DRING_ELEM(i)	((vd_dring_entry_t *)(void *)	\
1891ae08745Sheppo 	    (vd->dring + (i)*vd->descriptor_size))
1901ae08745Sheppo 
1911ae08745Sheppo /* Return the virtual disk client's type as a string (for use in messages) */
1921ae08745Sheppo #define	VD_CLIENT(vd)							\
1931ae08745Sheppo 	(((vd)->xfer_mode == VIO_DESC_MODE) ? "in-band client" :	\
194f0ca1d9aSsb155480 	    (((vd)->xfer_mode == VIO_DRING_MODE_V1_0) ? "dring client" :    \
1951ae08745Sheppo 		(((vd)->xfer_mode == 0) ? "null client" :		\
1961ae08745Sheppo 		    "unsupported client")))
1971ae08745Sheppo 
198690555a1Sachartre /* Read disk label from a disk on file */
199690555a1Sachartre #define	VD_FILE_LABEL_READ(vd, labelp) \
20087a7269eSachartre 	vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)labelp, \
201690555a1Sachartre 	    0, sizeof (struct dk_label))
202690555a1Sachartre 
203690555a1Sachartre /* Write disk label to a disk on file */
204690555a1Sachartre #define	VD_FILE_LABEL_WRITE(vd, labelp)	\
20587a7269eSachartre 	vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)labelp, \
206690555a1Sachartre 	    0, sizeof (struct dk_label))
207690555a1Sachartre 
2082f5224aeSachartre /* Message for disk access rights reset failure */
2092f5224aeSachartre #define	VD_RESET_ACCESS_FAILURE_MSG \
2102f5224aeSachartre 	"Fail to reset disk access rights for disk %s"
2112f5224aeSachartre 
212445b4c2eSsb155480 /*
213445b4c2eSsb155480  * Specification of an MD node passed to the MDEG to filter any
214445b4c2eSsb155480  * 'vport' nodes that do not belong to the specified node. This
215445b4c2eSsb155480  * template is copied for each vds instance and filled in with
216445b4c2eSsb155480  * the appropriate 'cfg-handle' value before being passed to the MDEG.
217445b4c2eSsb155480  */
218445b4c2eSsb155480 static mdeg_prop_spec_t	vds_prop_template[] = {
219445b4c2eSsb155480 	{ MDET_PROP_STR,	"name",		VDS_NAME },
220445b4c2eSsb155480 	{ MDET_PROP_VAL,	"cfg-handle",	NULL },
221445b4c2eSsb155480 	{ MDET_LIST_END,	NULL, 		NULL }
222445b4c2eSsb155480 };
223445b4c2eSsb155480 
224445b4c2eSsb155480 #define	VDS_SET_MDEG_PROP_INST(specp, val) (specp)[1].ps_val = (val);
225445b4c2eSsb155480 
226445b4c2eSsb155480 /*
227445b4c2eSsb155480  * Matching criteria passed to the MDEG to register interest
228445b4c2eSsb155480  * in changes to 'virtual-device-port' nodes identified by their
229445b4c2eSsb155480  * 'id' property.
230445b4c2eSsb155480  */
231445b4c2eSsb155480 static md_prop_match_t	vd_prop_match[] = {
232445b4c2eSsb155480 	{ MDET_PROP_VAL,	VD_ID_PROP },
233445b4c2eSsb155480 	{ MDET_LIST_END,	NULL }
234445b4c2eSsb155480 };
235445b4c2eSsb155480 
236445b4c2eSsb155480 static mdeg_node_match_t vd_match = {"virtual-device-port",
237445b4c2eSsb155480 				    vd_prop_match};
238445b4c2eSsb155480 
239047ba61eSachartre /*
240047ba61eSachartre  * Options for the VD_BLOCK_DEVICE_OPTS property.
241047ba61eSachartre  */
242047ba61eSachartre #define	VD_OPT_RDONLY		0x1	/* read-only  */
243047ba61eSachartre #define	VD_OPT_SLICE		0x2	/* single slice */
244047ba61eSachartre #define	VD_OPT_EXCLUSIVE	0x4	/* exclusive access */
245047ba61eSachartre 
246047ba61eSachartre #define	VD_OPTION_NLEN	128
247047ba61eSachartre 
248047ba61eSachartre typedef struct vd_option {
249047ba61eSachartre 	char vdo_name[VD_OPTION_NLEN];
250047ba61eSachartre 	uint64_t vdo_value;
251047ba61eSachartre } vd_option_t;
252047ba61eSachartre 
253047ba61eSachartre vd_option_t vd_bdev_options[] = {
254047ba61eSachartre 	{ "ro",		VD_OPT_RDONLY },
255047ba61eSachartre 	{ "slice", 	VD_OPT_SLICE },
256047ba61eSachartre 	{ "excl",	VD_OPT_EXCLUSIVE }
257047ba61eSachartre };
258047ba61eSachartre 
2591ae08745Sheppo /* Debugging macros */
2601ae08745Sheppo #ifdef DEBUG
2613af08d82Slm66018 
2623af08d82Slm66018 static int	vd_msglevel = 0;
2633af08d82Slm66018 
2641ae08745Sheppo #define	PR0 if (vd_msglevel > 0)	PRN
2651ae08745Sheppo #define	PR1 if (vd_msglevel > 1)	PRN
2661ae08745Sheppo #define	PR2 if (vd_msglevel > 2)	PRN
2671ae08745Sheppo 
2681ae08745Sheppo #define	VD_DUMP_DRING_ELEM(elem)					\
2693c96341aSnarayan 	PR0("dst:%x op:%x st:%u nb:%lx addr:%lx ncook:%u\n",		\
2701ae08745Sheppo 	    elem->hdr.dstate,						\
2711ae08745Sheppo 	    elem->payload.operation,					\
2721ae08745Sheppo 	    elem->payload.status,					\
2731ae08745Sheppo 	    elem->payload.nbytes,					\
2741ae08745Sheppo 	    elem->payload.addr,						\
2751ae08745Sheppo 	    elem->payload.ncookies);
2761ae08745Sheppo 
2773af08d82Slm66018 char *
2783af08d82Slm66018 vd_decode_state(int state)
2793af08d82Slm66018 {
2803af08d82Slm66018 	char *str;
2813af08d82Slm66018 
2823af08d82Slm66018 #define	CASE_STATE(_s)	case _s: str = #_s; break;
2833af08d82Slm66018 
2843af08d82Slm66018 	switch (state) {
2853af08d82Slm66018 	CASE_STATE(VD_STATE_INIT)
2863af08d82Slm66018 	CASE_STATE(VD_STATE_VER)
2873af08d82Slm66018 	CASE_STATE(VD_STATE_ATTR)
2883af08d82Slm66018 	CASE_STATE(VD_STATE_DRING)
2893af08d82Slm66018 	CASE_STATE(VD_STATE_RDX)
2903af08d82Slm66018 	CASE_STATE(VD_STATE_DATA)
2913af08d82Slm66018 	default: str = "unknown"; break;
2923af08d82Slm66018 	}
2933af08d82Slm66018 
2943af08d82Slm66018 #undef CASE_STATE
2953af08d82Slm66018 
2963af08d82Slm66018 	return (str);
2973af08d82Slm66018 }
2983af08d82Slm66018 
2993af08d82Slm66018 void
3003af08d82Slm66018 vd_decode_tag(vio_msg_t *msg)
3013af08d82Slm66018 {
3023af08d82Slm66018 	char *tstr, *sstr, *estr;
3033af08d82Slm66018 
3043af08d82Slm66018 #define	CASE_TYPE(_s)	case _s: tstr = #_s; break;
3053af08d82Slm66018 
3063af08d82Slm66018 	switch (msg->tag.vio_msgtype) {
3073af08d82Slm66018 	CASE_TYPE(VIO_TYPE_CTRL)
3083af08d82Slm66018 	CASE_TYPE(VIO_TYPE_DATA)
3093af08d82Slm66018 	CASE_TYPE(VIO_TYPE_ERR)
3103af08d82Slm66018 	default: tstr = "unknown"; break;
3113af08d82Slm66018 	}
3123af08d82Slm66018 
3133af08d82Slm66018 #undef CASE_TYPE
3143af08d82Slm66018 
3153af08d82Slm66018 #define	CASE_SUBTYPE(_s) case _s: sstr = #_s; break;
3163af08d82Slm66018 
3173af08d82Slm66018 	switch (msg->tag.vio_subtype) {
3183af08d82Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_INFO)
3193af08d82Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_ACK)
3203af08d82Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_NACK)
3213af08d82Slm66018 	default: sstr = "unknown"; break;
3223af08d82Slm66018 	}
3233af08d82Slm66018 
3243af08d82Slm66018 #undef CASE_SUBTYPE
3253af08d82Slm66018 
3263af08d82Slm66018 #define	CASE_ENV(_s)	case _s: estr = #_s; break;
3273af08d82Slm66018 
3283af08d82Slm66018 	switch (msg->tag.vio_subtype_env) {
3293af08d82Slm66018 	CASE_ENV(VIO_VER_INFO)
3303af08d82Slm66018 	CASE_ENV(VIO_ATTR_INFO)
3313af08d82Slm66018 	CASE_ENV(VIO_DRING_REG)
3323af08d82Slm66018 	CASE_ENV(VIO_DRING_UNREG)
3333af08d82Slm66018 	CASE_ENV(VIO_RDX)
3343af08d82Slm66018 	CASE_ENV(VIO_PKT_DATA)
3353af08d82Slm66018 	CASE_ENV(VIO_DESC_DATA)
3363af08d82Slm66018 	CASE_ENV(VIO_DRING_DATA)
3373af08d82Slm66018 	default: estr = "unknown"; break;
3383af08d82Slm66018 	}
3393af08d82Slm66018 
3403af08d82Slm66018 #undef CASE_ENV
3413af08d82Slm66018 
3423af08d82Slm66018 	PR1("(%x/%x/%x) message : (%s/%s/%s)",
3433af08d82Slm66018 	    msg->tag.vio_msgtype, msg->tag.vio_subtype,
3443af08d82Slm66018 	    msg->tag.vio_subtype_env, tstr, sstr, estr);
3453af08d82Slm66018 }
3463af08d82Slm66018 
3471ae08745Sheppo #else	/* !DEBUG */
3483af08d82Slm66018 
3491ae08745Sheppo #define	PR0(...)
3501ae08745Sheppo #define	PR1(...)
3511ae08745Sheppo #define	PR2(...)
3521ae08745Sheppo 
3531ae08745Sheppo #define	VD_DUMP_DRING_ELEM(elem)
3541ae08745Sheppo 
3553af08d82Slm66018 #define	vd_decode_state(_s)	(NULL)
3563af08d82Slm66018 #define	vd_decode_tag(_s)	(NULL)
3573af08d82Slm66018 
3581ae08745Sheppo #endif	/* DEBUG */
3591ae08745Sheppo 
3601ae08745Sheppo 
361d10e4ef2Snarayan /*
362d10e4ef2Snarayan  * Soft state structure for a vds instance
363d10e4ef2Snarayan  */
3641ae08745Sheppo typedef struct vds {
3651ae08745Sheppo 	uint_t		initialized;	/* driver inst initialization flags */
3661ae08745Sheppo 	dev_info_t	*dip;		/* driver inst devinfo pointer */
3671ae08745Sheppo 	ldi_ident_t	ldi_ident;	/* driver's identifier for LDI */
3681ae08745Sheppo 	mod_hash_t	*vd_table;	/* table of virtual disks served */
369445b4c2eSsb155480 	mdeg_node_spec_t *ispecp;	/* mdeg node specification */
3701ae08745Sheppo 	mdeg_handle_t	mdeg;		/* handle for MDEG operations  */
3718fce2fd6Sachartre 	vd_driver_type_t *driver_types;	/* extra driver types (from vds.conf) */
3728fce2fd6Sachartre 	int 		num_drivers;	/* num of extra driver types */
3731ae08745Sheppo } vds_t;
3741ae08745Sheppo 
375d10e4ef2Snarayan /*
376d10e4ef2Snarayan  * Types of descriptor-processing tasks
377d10e4ef2Snarayan  */
378d10e4ef2Snarayan typedef enum vd_task_type {
379d10e4ef2Snarayan 	VD_NONFINAL_RANGE_TASK,	/* task for intermediate descriptor in range */
380d10e4ef2Snarayan 	VD_FINAL_RANGE_TASK,	/* task for last in a range of descriptors */
381d10e4ef2Snarayan } vd_task_type_t;
382d10e4ef2Snarayan 
383d10e4ef2Snarayan /*
384d10e4ef2Snarayan  * Structure describing the task for processing a descriptor
385d10e4ef2Snarayan  */
386d10e4ef2Snarayan typedef struct vd_task {
387d10e4ef2Snarayan 	struct vd		*vd;		/* vd instance task is for */
388d10e4ef2Snarayan 	vd_task_type_t		type;		/* type of descriptor task */
389d10e4ef2Snarayan 	int			index;		/* dring elem index for task */
390d10e4ef2Snarayan 	vio_msg_t		*msg;		/* VIO message task is for */
391d10e4ef2Snarayan 	size_t			msglen;		/* length of message content */
392d10e4ef2Snarayan 	vd_dring_payload_t	*request;	/* request task will perform */
393d10e4ef2Snarayan 	struct buf		buf;		/* buf(9s) for I/O request */
3944bac2208Snarayan 	ldc_mem_handle_t	mhdl;		/* task memory handle */
395205eeb1aSlm66018 	int			status;		/* status of processing task */
396205eeb1aSlm66018 	int	(*completef)(struct vd_task *task); /* completion func ptr */
397d10e4ef2Snarayan } vd_task_t;
398d10e4ef2Snarayan 
399d10e4ef2Snarayan /*
400d10e4ef2Snarayan  * Soft state structure for a virtual disk instance
401d10e4ef2Snarayan  */
4021ae08745Sheppo typedef struct vd {
4031ae08745Sheppo 	uint_t			initialized;	/* vdisk initialization flags */
40417cadca8Slm66018 	uint64_t		operations;	/* bitmask of VD_OPs exported */
40517cadca8Slm66018 	vio_ver_t		version;	/* ver negotiated with client */
4061ae08745Sheppo 	vds_t			*vds;		/* server for this vdisk */
407d10e4ef2Snarayan 	ddi_taskq_t		*startq;	/* queue for I/O start tasks */
408d10e4ef2Snarayan 	ddi_taskq_t		*completionq;	/* queue for completion tasks */
4091ae08745Sheppo 	ldi_handle_t		ldi_handle[V_NUMPAR];	/* LDI slice handles */
4103c96341aSnarayan 	char			device_path[MAXPATHLEN + 1]; /* vdisk device */
4111ae08745Sheppo 	dev_t			dev[V_NUMPAR];	/* dev numbers for slices */
412047ba61eSachartre 	int			open_flags;	/* open flags */
413bae9e67eSachartre 	uint_t			nslices;	/* number of slices we export */
4141ae08745Sheppo 	size_t			vdisk_size;	/* number of blocks in vdisk */
41517cadca8Slm66018 	size_t			vdisk_block_size; /* size of each vdisk block */
4161ae08745Sheppo 	vd_disk_type_t		vdisk_type;	/* slice or entire disk */
4174bac2208Snarayan 	vd_disk_label_t		vdisk_label;	/* EFI or VTOC label */
41817cadca8Slm66018 	vd_media_t		vdisk_media;	/* media type of backing dev. */
41917cadca8Slm66018 	boolean_t		is_atapi_dev;	/* Is this an IDE CD-ROM dev? */
420e1ebb9ecSlm66018 	ushort_t		max_xfer_sz;	/* max xfer size in DEV_BSIZE */
42117cadca8Slm66018 	size_t			block_size;	/* blk size of actual device */
4228fce2fd6Sachartre 	boolean_t		volume;		/* is vDisk backed by volume */
42317cadca8Slm66018 	boolean_t		file;		/* is vDisk backed by a file? */
4242f5224aeSachartre 	boolean_t		scsi;		/* is vDisk backed by scsi? */
4253c96341aSnarayan 	vnode_t			*file_vnode;	/* file vnode */
4263c96341aSnarayan 	size_t			file_size;	/* file size */
42787a7269eSachartre 	ddi_devid_t		file_devid;	/* devid for disk image */
428edcc0754Sachartre 	int			efi_reserved;	/* EFI reserved slice */
429bae9e67eSachartre 	caddr_t			flabel;		/* fake label for slice type */
430bae9e67eSachartre 	uint_t			flabel_size;	/* fake label size */
431bae9e67eSachartre 	uint_t			flabel_limit;	/* limit of the fake label */
4321ae08745Sheppo 	struct dk_geom		dk_geom;	/* synthetic for slice type */
4331ae08745Sheppo 	struct vtoc		vtoc;		/* synthetic for slice type */
434edcc0754Sachartre 	vd_slice_t		slices[VD_MAXPART]; /* logical partitions */
4352f5224aeSachartre 	boolean_t		ownership;	/* disk ownership status */
4361ae08745Sheppo 	ldc_status_t		ldc_state;	/* LDC connection state */
4371ae08745Sheppo 	ldc_handle_t		ldc_handle;	/* handle for LDC comm */
4381ae08745Sheppo 	size_t			max_msglen;	/* largest LDC message len */
4391ae08745Sheppo 	vd_state_t		state;		/* client handshake state */
4401ae08745Sheppo 	uint8_t			xfer_mode;	/* transfer mode with client */
4411ae08745Sheppo 	uint32_t		sid;		/* client's session ID */
4421ae08745Sheppo 	uint64_t		seq_num;	/* message sequence number */
4431ae08745Sheppo 	uint64_t		dring_ident;	/* identifier of dring */
4441ae08745Sheppo 	ldc_dring_handle_t	dring_handle;	/* handle for dring ops */
4451ae08745Sheppo 	uint32_t		descriptor_size;	/* num bytes in desc */
4461ae08745Sheppo 	uint32_t		dring_len;	/* number of dring elements */
447bbfa0259Sha137994 	uint8_t			dring_mtype;	/* dring mem map type */
4481ae08745Sheppo 	caddr_t			dring;		/* address of dring */
4493af08d82Slm66018 	caddr_t			vio_msgp;	/* vio msg staging buffer */
450d10e4ef2Snarayan 	vd_task_t		inband_task;	/* task for inband descriptor */
451d10e4ef2Snarayan 	vd_task_t		*dring_task;	/* tasks dring elements */
452d10e4ef2Snarayan 
453d10e4ef2Snarayan 	kmutex_t		lock;		/* protects variables below */
454d10e4ef2Snarayan 	boolean_t		enabled;	/* is vdisk enabled? */
455d10e4ef2Snarayan 	boolean_t		reset_state;	/* reset connection state? */
456d10e4ef2Snarayan 	boolean_t		reset_ldc;	/* reset LDC channel? */
4571ae08745Sheppo } vd_t;
4581ae08745Sheppo 
459bae9e67eSachartre /*
460bae9e67eSachartre  * Macros to manipulate the fake label (flabel) for single slice disks.
461bae9e67eSachartre  *
462bae9e67eSachartre  * If we fake a VTOC label then the fake label consists of only one block
463bae9e67eSachartre  * containing the VTOC label (struct dk_label).
464bae9e67eSachartre  *
465bae9e67eSachartre  * If we fake an EFI label then the fake label consists of a blank block
466bae9e67eSachartre  * followed by a GPT (efi_gpt_t) and a GPE (efi_gpe_t).
467bae9e67eSachartre  *
468bae9e67eSachartre  */
469bae9e67eSachartre #define	VD_LABEL_VTOC_SIZE					\
470bae9e67eSachartre 	P2ROUNDUP(sizeof (struct dk_label), DEV_BSIZE)
471bae9e67eSachartre 
472bae9e67eSachartre #define	VD_LABEL_EFI_SIZE					\
473bae9e67eSachartre 	P2ROUNDUP(DEV_BSIZE + sizeof (efi_gpt_t) + 		\
474bae9e67eSachartre 	    sizeof (efi_gpe_t) * VD_MAXPART, DEV_BSIZE)
475bae9e67eSachartre 
476bae9e67eSachartre #define	VD_LABEL_VTOC(vd)	\
477bae9e67eSachartre 		((struct dk_label *)((vd)->flabel))
478bae9e67eSachartre 
479bae9e67eSachartre #define	VD_LABEL_EFI_GPT(vd)	\
480bae9e67eSachartre 		((efi_gpt_t *)((vd)->flabel + DEV_BSIZE))
481bae9e67eSachartre #define	VD_LABEL_EFI_GPE(vd)	\
482bae9e67eSachartre 		((efi_gpe_t *)((vd)->flabel + DEV_BSIZE + sizeof (efi_gpt_t)))
483bae9e67eSachartre 
484bae9e67eSachartre 
4851ae08745Sheppo typedef struct vds_operation {
4863af08d82Slm66018 	char	*namep;
4871ae08745Sheppo 	uint8_t	operation;
488d10e4ef2Snarayan 	int	(*start)(vd_task_t *task);
489205eeb1aSlm66018 	int	(*complete)(vd_task_t *task);
4901ae08745Sheppo } vds_operation_t;
4911ae08745Sheppo 
4920a55fbb7Slm66018 typedef struct vd_ioctl {
4930a55fbb7Slm66018 	uint8_t		operation;		/* vdisk operation */
4940a55fbb7Slm66018 	const char	*operation_name;	/* vdisk operation name */
4950a55fbb7Slm66018 	size_t		nbytes;			/* size of operation buffer */
4960a55fbb7Slm66018 	int		cmd;			/* corresponding ioctl cmd */
4970a55fbb7Slm66018 	const char	*cmd_name;		/* ioctl cmd name */
4980a55fbb7Slm66018 	void		*arg;			/* ioctl cmd argument */
4990a55fbb7Slm66018 	/* convert input vd_buf to output ioctl_arg */
5002f5224aeSachartre 	int		(*copyin)(void *vd_buf, size_t, void *ioctl_arg);
5010a55fbb7Slm66018 	/* convert input ioctl_arg to output vd_buf */
5020a55fbb7Slm66018 	void		(*copyout)(void *ioctl_arg, void *vd_buf);
503047ba61eSachartre 	/* write is true if the operation writes any data to the backend */
504047ba61eSachartre 	boolean_t	write;
5050a55fbb7Slm66018 } vd_ioctl_t;
5060a55fbb7Slm66018 
5070a55fbb7Slm66018 /* Define trivial copyin/copyout conversion function flag */
5082f5224aeSachartre #define	VD_IDENTITY_IN	((int (*)(void *, size_t, void *))-1)
5092f5224aeSachartre #define	VD_IDENTITY_OUT	((void (*)(void *, void *))-1)
5101ae08745Sheppo 
5111ae08745Sheppo 
5123c96341aSnarayan static int	vds_ldc_retries = VDS_RETRIES;
5133af08d82Slm66018 static int	vds_ldc_delay = VDS_LDC_DELAY;
5143c96341aSnarayan static int	vds_dev_retries = VDS_RETRIES;
5153c96341aSnarayan static int	vds_dev_delay = VDS_DEV_DELAY;
5161ae08745Sheppo static void	*vds_state;
5171ae08745Sheppo 
518eba0cb4eSachartre static uint_t	vd_file_write_flags = VD_FILE_WRITE_FLAGS;
519eba0cb4eSachartre 
52087a7269eSachartre static short	vd_scsi_rdwr_timeout = VD_SCSI_RDWR_TIMEOUT;
5212f5224aeSachartre static int	vd_scsi_debug = USCSI_SILENT;
5222f5224aeSachartre 
5232f5224aeSachartre /*
5242f5224aeSachartre  * Tunable to define the behavior of the service domain if the vdisk server
5252f5224aeSachartre  * fails to reset disk exclusive access when a LDC channel is reset. When a
5262f5224aeSachartre  * LDC channel is reset the vdisk server will try to reset disk exclusive
5272f5224aeSachartre  * access by releasing any SCSI-2 reservation or resetting the disk. If these
5282f5224aeSachartre  * actions fail then the default behavior (vd_reset_access_failure = 0) is to
5292f5224aeSachartre  * print a warning message. This default behavior can be changed by setting
5302f5224aeSachartre  * the vd_reset_access_failure variable to A_REBOOT (= 0x1) and that will
5312f5224aeSachartre  * cause the service domain to reboot, or A_DUMP (= 0x5) and that will cause
5322f5224aeSachartre  * the service domain to panic. In both cases, the reset of the service domain
5332f5224aeSachartre  * should trigger a reset SCSI buses and hopefully clear any SCSI-2 reservation.
5342f5224aeSachartre  */
5352f5224aeSachartre static int 	vd_reset_access_failure = 0;
5362f5224aeSachartre 
5372f5224aeSachartre /*
5382f5224aeSachartre  * Tunable for backward compatibility. When this variable is set to B_TRUE,
5392f5224aeSachartre  * all disk volumes (ZFS, SVM, VxvM volumes) will be exported as single
5402f5224aeSachartre  * slice disks whether or not they have the "slice" option set. This is
5412f5224aeSachartre  * to provide a simple backward compatibility mechanism when upgrading
5422f5224aeSachartre  * the vds driver and using a domain configuration created before the
5432f5224aeSachartre  * "slice" option was available.
5442f5224aeSachartre  */
5452f5224aeSachartre static boolean_t vd_volume_force_slice = B_FALSE;
54687a7269eSachartre 
5470a55fbb7Slm66018 /*
54866cfcfbeSachartre  * The label of disk images created with some earlier versions of the virtual
54966cfcfbeSachartre  * disk software is not entirely correct and have an incorrect v_sanity field
55066cfcfbeSachartre  * (usually 0) instead of VTOC_SANE. This creates a compatibility problem with
55166cfcfbeSachartre  * these images because we are now validating that the disk label (and the
55266cfcfbeSachartre  * sanity) is correct when a disk image is opened.
55366cfcfbeSachartre  *
55466cfcfbeSachartre  * This tunable is set to false to not validate the sanity field and ensure
55566cfcfbeSachartre  * compatibility. If the tunable is set to true, we will do a strict checking
55666cfcfbeSachartre  * of the sanity but this can create compatibility problems with old disk
55766cfcfbeSachartre  * images.
55866cfcfbeSachartre  */
55966cfcfbeSachartre static boolean_t vd_file_validate_sanity = B_FALSE;
56066cfcfbeSachartre 
56166cfcfbeSachartre /*
562bbfa0259Sha137994  * Enables the use of LDC_DIRECT_MAP when mapping in imported descriptor rings.
563bbfa0259Sha137994  */
564bbfa0259Sha137994 static boolean_t vd_direct_mapped_drings = B_TRUE;
565bbfa0259Sha137994 
566bbfa0259Sha137994 /*
567bae9e67eSachartre  * When a backend is exported as a single-slice disk then we entirely fake
568bae9e67eSachartre  * its disk label. So it can be exported either with a VTOC label or with
569bae9e67eSachartre  * an EFI label. If vd_slice_label is set to VD_DISK_LABEL_VTOC then all
570bae9e67eSachartre  * single-slice disks will be exported with a VTOC label; and if it is set
571bae9e67eSachartre  * to VD_DISK_LABEL_EFI then all single-slice disks will be exported with
572bae9e67eSachartre  * an EFI label.
573bae9e67eSachartre  *
574bae9e67eSachartre  * If vd_slice_label is set to VD_DISK_LABEL_UNK and the backend is a disk
575bae9e67eSachartre  * or volume device then it will be exported with the same type of label as
576bae9e67eSachartre  * defined on the device. Otherwise if the backend is a file then it will
577bae9e67eSachartre  * exported with the disk label type set in the vd_file_slice_label variable.
578bae9e67eSachartre  *
579bae9e67eSachartre  * Note that if the backend size is greater than 1TB then it will always be
580bae9e67eSachartre  * exported with an EFI label no matter what the setting is.
581bae9e67eSachartre  */
582bae9e67eSachartre static vd_disk_label_t vd_slice_label = VD_DISK_LABEL_UNK;
583bae9e67eSachartre 
584bae9e67eSachartre static vd_disk_label_t vd_file_slice_label = VD_DISK_LABEL_VTOC;
585bae9e67eSachartre 
586bae9e67eSachartre /*
587bae9e67eSachartre  * Tunable for backward compatibility. If this variable is set to B_TRUE then
588bae9e67eSachartre  * single-slice disks are exported as disks with only one slice instead of
589bae9e67eSachartre  * faking a complete disk partitioning.
590bae9e67eSachartre  */
591bae9e67eSachartre static boolean_t vd_slice_single_slice = B_FALSE;
592bae9e67eSachartre 
593bae9e67eSachartre /*
5940a55fbb7Slm66018  * Supported protocol version pairs, from highest (newest) to lowest (oldest)
5950a55fbb7Slm66018  *
5960a55fbb7Slm66018  * Each supported major version should appear only once, paired with (and only
5970a55fbb7Slm66018  * with) its highest supported minor version number (as the protocol requires
5980a55fbb7Slm66018  * supporting all lower minor version numbers as well)
5990a55fbb7Slm66018  */
60017cadca8Slm66018 static const vio_ver_t	vds_version[] = {{1, 1}};
6010a55fbb7Slm66018 static const size_t	vds_num_versions =
6020a55fbb7Slm66018     sizeof (vds_version)/sizeof (vds_version[0]);
6030a55fbb7Slm66018 
6043af08d82Slm66018 static void vd_free_dring_task(vd_t *vdp);
6053c96341aSnarayan static int vd_setup_vd(vd_t *vd);
606047ba61eSachartre static int vd_setup_single_slice_disk(vd_t *vd);
6072f5224aeSachartre static int vd_setup_mediainfo(vd_t *vd);
6083c96341aSnarayan static boolean_t vd_enabled(vd_t *vd);
60978fcd0a1Sachartre static ushort_t vd_lbl2cksum(struct dk_label *label);
61078fcd0a1Sachartre static int vd_file_validate_geometry(vd_t *vd);
61117cadca8Slm66018 static boolean_t vd_file_is_iso_image(vd_t *vd);
61217cadca8Slm66018 static void vd_set_exported_operations(vd_t *vd);
6132f5224aeSachartre static void vd_reset_access(vd_t *vd);
614edcc0754Sachartre static int vd_backend_ioctl(vd_t *vd, int cmd, caddr_t arg);
615edcc0754Sachartre static int vds_efi_alloc_and_read(vd_t *, efi_gpt_t **, efi_gpe_t **);
616edcc0754Sachartre static void vds_efi_free(vd_t *, efi_gpt_t *, efi_gpe_t *);
6178fce2fd6Sachartre static void vds_driver_types_free(vds_t *vds);
618bae9e67eSachartre static void vd_vtocgeom_to_label(struct vtoc *vtoc, struct dk_geom *geom,
619bae9e67eSachartre     struct dk_label *label);
620bae9e67eSachartre static void vd_label_to_vtocgeom(struct dk_label *label, struct vtoc *vtoc,
621bae9e67eSachartre     struct dk_geom *geom);
622bae9e67eSachartre static boolean_t vd_slice_geom_isvalid(vd_t *vd, struct dk_geom *geom);
623bae9e67eSachartre static boolean_t vd_slice_vtoc_isvalid(vd_t *vd, struct vtoc *vtoc);
624bae9e67eSachartre 
625bae9e67eSachartre extern int is_pseudo_device(dev_info_t *);
626bae9e67eSachartre 
627bae9e67eSachartre /*
628bae9e67eSachartre  * Function:
629bae9e67eSachartre  *	vd_get_readable_size
630bae9e67eSachartre  *
631bae9e67eSachartre  * Description:
632bae9e67eSachartre  * 	Convert a given size in bytes to a human readable format in
633bae9e67eSachartre  * 	kilobytes, megabytes, gigabytes or terabytes.
634bae9e67eSachartre  *
635bae9e67eSachartre  * Parameters:
636bae9e67eSachartre  *	full_size	- the size to convert in bytes.
637bae9e67eSachartre  *	size		- the converted size.
638bae9e67eSachartre  *	unit		- the unit of the converted size: 'K' (kilobyte),
639bae9e67eSachartre  *			  'M' (Megabyte), 'G' (Gigabyte), 'T' (Terabyte).
640bae9e67eSachartre  *
641bae9e67eSachartre  * Return Code:
642bae9e67eSachartre  *	none
643bae9e67eSachartre  */
644bae9e67eSachartre void
645bae9e67eSachartre vd_get_readable_size(size_t full_size, size_t *size, char *unit)
646bae9e67eSachartre {
647bae9e67eSachartre 	if (full_size < (1ULL << 20)) {
648bae9e67eSachartre 		*size = full_size >> 10;
649bae9e67eSachartre 		*unit = 'K'; /* Kilobyte */
650bae9e67eSachartre 	} else if (full_size < (1ULL << 30)) {
651bae9e67eSachartre 		*size = full_size >> 20;
652bae9e67eSachartre 		*unit = 'M'; /* Megabyte */
653bae9e67eSachartre 	} else if (full_size < (1ULL << 40)) {
654bae9e67eSachartre 		*size = full_size >> 30;
655bae9e67eSachartre 		*unit = 'G'; /* Gigabyte */
656bae9e67eSachartre 	} else {
657bae9e67eSachartre 		*size = full_size >> 40;
658bae9e67eSachartre 		*unit = 'T'; /* Terabyte */
659bae9e67eSachartre 	}
660bae9e67eSachartre }
661047ba61eSachartre 
662690555a1Sachartre /*
663690555a1Sachartre  * Function:
664690555a1Sachartre  *	vd_file_rw
665690555a1Sachartre  *
666690555a1Sachartre  * Description:
667690555a1Sachartre  * 	Read or write to a disk on file.
668690555a1Sachartre  *
669690555a1Sachartre  * Parameters:
670690555a1Sachartre  *	vd		- disk on which the operation is performed.
671690555a1Sachartre  *	slice		- slice on which the operation is performed,
67287a7269eSachartre  *			  VD_SLICE_NONE indicates that the operation
67387a7269eSachartre  *			  is done using an absolute disk offset.
674690555a1Sachartre  *	operation	- operation to execute: read (VD_OP_BREAD) or
675690555a1Sachartre  *			  write (VD_OP_BWRITE).
676690555a1Sachartre  *	data		- buffer where data are read to or written from.
677690555a1Sachartre  *	blk		- starting block for the operation.
678690555a1Sachartre  *	len		- number of bytes to read or write.
679690555a1Sachartre  *
680690555a1Sachartre  * Return Code:
681690555a1Sachartre  *	n >= 0		- success, n indicates the number of bytes read
682690555a1Sachartre  *			  or written.
683690555a1Sachartre  *	-1		- error.
684690555a1Sachartre  */
685690555a1Sachartre static ssize_t
686690555a1Sachartre vd_file_rw(vd_t *vd, int slice, int operation, caddr_t data, size_t blk,
687690555a1Sachartre     size_t len)
688690555a1Sachartre {
689690555a1Sachartre 	caddr_t	maddr;
690690555a1Sachartre 	size_t offset, maxlen, moffset, mlen, n;
691690555a1Sachartre 	uint_t smflags;
692690555a1Sachartre 	enum seg_rw srw;
693690555a1Sachartre 
694690555a1Sachartre 	ASSERT(vd->file);
695690555a1Sachartre 	ASSERT(len > 0);
696690555a1Sachartre 
697047ba61eSachartre 	/*
698047ba61eSachartre 	 * If a file is exported as a slice then we don't care about the vtoc.
699047ba61eSachartre 	 * In that case, the vtoc is a fake mainly to make newfs happy and we
700047ba61eSachartre 	 * handle any I/O as a raw disk access so that we can have access to the
701047ba61eSachartre 	 * entire backend.
702047ba61eSachartre 	 */
703047ba61eSachartre 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE || slice == VD_SLICE_NONE) {
704690555a1Sachartre 		/* raw disk access */
705690555a1Sachartre 		offset = blk * DEV_BSIZE;
706bae9e67eSachartre 		if (offset >= vd->file_size) {
707bae9e67eSachartre 			/* offset past the end of the disk */
708bae9e67eSachartre 			PR0("offset (0x%lx) >= fsize (0x%lx)",
709bae9e67eSachartre 			    offset, vd->file_size);
710bae9e67eSachartre 			return (0);
711bae9e67eSachartre 		}
712bae9e67eSachartre 		maxlen = vd->file_size - offset;
713690555a1Sachartre 	} else {
714690555a1Sachartre 		ASSERT(slice >= 0 && slice < V_NUMPAR);
71578fcd0a1Sachartre 
71617cadca8Slm66018 		/*
71717cadca8Slm66018 		 * v1.0 vDisk clients depended on the server not verifying
71817cadca8Slm66018 		 * the label of a unformatted disk.  This "feature" is
71917cadca8Slm66018 		 * maintained for backward compatibility but all versions
72017cadca8Slm66018 		 * from v1.1 onwards must do the right thing.
72117cadca8Slm66018 		 */
72278fcd0a1Sachartre 		if (vd->vdisk_label == VD_DISK_LABEL_UNK &&
723edcc0754Sachartre 		    vio_ver_is_supported(vd->version, 1, 1)) {
724edcc0754Sachartre 			(void) vd_file_validate_geometry(vd);
725edcc0754Sachartre 			if (vd->vdisk_label == VD_DISK_LABEL_UNK) {
726edcc0754Sachartre 				PR0("Unknown disk label, can't do I/O "
727edcc0754Sachartre 				    "from slice %d", slice);
72878fcd0a1Sachartre 				return (-1);
72978fcd0a1Sachartre 			}
730edcc0754Sachartre 		}
73178fcd0a1Sachartre 
732edcc0754Sachartre 		if (vd->vdisk_label == VD_DISK_LABEL_VTOC) {
733edcc0754Sachartre 			ASSERT(vd->vtoc.v_sectorsz == DEV_BSIZE);
734edcc0754Sachartre 		} else {
735edcc0754Sachartre 			ASSERT(vd->vdisk_label == VD_DISK_LABEL_EFI);
736edcc0754Sachartre 			ASSERT(vd->vdisk_block_size == DEV_BSIZE);
737edcc0754Sachartre 		}
738edcc0754Sachartre 
739edcc0754Sachartre 		if (blk >= vd->slices[slice].nblocks) {
740690555a1Sachartre 			/* address past the end of the slice */
741bae9e67eSachartre 			PR0("req_addr (0x%lx) >= psize (0x%lx)",
742edcc0754Sachartre 			    blk, vd->slices[slice].nblocks);
743690555a1Sachartre 			return (0);
744690555a1Sachartre 		}
745690555a1Sachartre 
746edcc0754Sachartre 		offset = (vd->slices[slice].start + blk) * DEV_BSIZE;
747bae9e67eSachartre 		maxlen = (vd->slices[slice].nblocks - blk) * DEV_BSIZE;
748bae9e67eSachartre 	}
749690555a1Sachartre 
750690555a1Sachartre 	/*
751690555a1Sachartre 	 * If the requested size is greater than the size
752690555a1Sachartre 	 * of the partition, truncate the read/write.
753690555a1Sachartre 	 */
754690555a1Sachartre 	if (len > maxlen) {
755690555a1Sachartre 		PR0("I/O size truncated to %lu bytes from %lu bytes",
756690555a1Sachartre 		    maxlen, len);
757690555a1Sachartre 		len = maxlen;
758690555a1Sachartre 	}
759690555a1Sachartre 
760690555a1Sachartre 	/*
761690555a1Sachartre 	 * We have to ensure that we are reading/writing into the mmap
762690555a1Sachartre 	 * range. If we have a partial disk image (e.g. an image of
763690555a1Sachartre 	 * s0 instead s2) the system can try to access slices that
764690555a1Sachartre 	 * are not included into the disk image.
765690555a1Sachartre 	 */
766edcc0754Sachartre 	if ((offset + len) > vd->file_size) {
767edcc0754Sachartre 		PR0("offset + nbytes (0x%lx + 0x%lx) > "
768690555a1Sachartre 		    "file_size (0x%lx)", offset, len, vd->file_size);
769690555a1Sachartre 		return (-1);
770690555a1Sachartre 	}
771690555a1Sachartre 
772690555a1Sachartre 	srw = (operation == VD_OP_BREAD)? S_READ : S_WRITE;
773eba0cb4eSachartre 	smflags = (operation == VD_OP_BREAD)? 0 :
774eba0cb4eSachartre 	    (SM_WRITE | vd_file_write_flags);
775690555a1Sachartre 	n = len;
776690555a1Sachartre 
777690555a1Sachartre 	do {
778690555a1Sachartre 		/*
779690555a1Sachartre 		 * segmap_getmapflt() returns a MAXBSIZE chunk which is
780690555a1Sachartre 		 * MAXBSIZE aligned.
781690555a1Sachartre 		 */
782690555a1Sachartre 		moffset = offset & MAXBOFFSET;
783690555a1Sachartre 		mlen = MIN(MAXBSIZE - moffset, n);
784690555a1Sachartre 		maddr = segmap_getmapflt(segkmap, vd->file_vnode, offset,
785690555a1Sachartre 		    mlen, 1, srw);
786690555a1Sachartre 		/*
787690555a1Sachartre 		 * Fault in the pages so we can check for error and ensure
788690555a1Sachartre 		 * that we can safely used the mapped address.
789690555a1Sachartre 		 */
790690555a1Sachartre 		if (segmap_fault(kas.a_hat, segkmap, maddr, mlen,
791690555a1Sachartre 		    F_SOFTLOCK, srw) != 0) {
792690555a1Sachartre 			(void) segmap_release(segkmap, maddr, 0);
793690555a1Sachartre 			return (-1);
794690555a1Sachartre 		}
795690555a1Sachartre 
796690555a1Sachartre 		if (operation == VD_OP_BREAD)
797690555a1Sachartre 			bcopy(maddr + moffset, data, mlen);
798690555a1Sachartre 		else
799690555a1Sachartre 			bcopy(data, maddr + moffset, mlen);
800690555a1Sachartre 
801690555a1Sachartre 		if (segmap_fault(kas.a_hat, segkmap, maddr, mlen,
802690555a1Sachartre 		    F_SOFTUNLOCK, srw) != 0) {
803690555a1Sachartre 			(void) segmap_release(segkmap, maddr, 0);
804690555a1Sachartre 			return (-1);
805690555a1Sachartre 		}
806690555a1Sachartre 		if (segmap_release(segkmap, maddr, smflags) != 0)
807690555a1Sachartre 			return (-1);
808690555a1Sachartre 		n -= mlen;
809690555a1Sachartre 		offset += mlen;
810690555a1Sachartre 		data += mlen;
811690555a1Sachartre 
812690555a1Sachartre 	} while (n > 0);
813690555a1Sachartre 
814690555a1Sachartre 	return (len);
815690555a1Sachartre }
816690555a1Sachartre 
81787a7269eSachartre /*
81887a7269eSachartre  * Function:
819bae9e67eSachartre  *	vd_build_default_label
82078fcd0a1Sachartre  *
82178fcd0a1Sachartre  * Description:
822bae9e67eSachartre  *	Return a default label for a given disk size. This is used when the disk
82378fcd0a1Sachartre  *	does not have a valid VTOC so that the user can get a valid default
82417cadca8Slm66018  *	configuration. The default label has all slice sizes set to 0 (except
82578fcd0a1Sachartre  *	slice 2 which is the entire disk) to force the user to write a valid
82678fcd0a1Sachartre  *	label onto the disk image.
82778fcd0a1Sachartre  *
82878fcd0a1Sachartre  * Parameters:
829bae9e67eSachartre  *	disk_size	- the disk size in bytes
83078fcd0a1Sachartre  *	label		- the returned default label.
83178fcd0a1Sachartre  *
83278fcd0a1Sachartre  * Return Code:
83378fcd0a1Sachartre  *	none.
83478fcd0a1Sachartre  */
83578fcd0a1Sachartre static void
836bae9e67eSachartre vd_build_default_label(size_t disk_size, struct dk_label *label)
83778fcd0a1Sachartre {
83878fcd0a1Sachartre 	size_t size;
839bae9e67eSachartre 	char unit;
840edcc0754Sachartre 
841edcc0754Sachartre 	bzero(label, sizeof (struct dk_label));
84278fcd0a1Sachartre 
84378fcd0a1Sachartre 	/*
84478fcd0a1Sachartre 	 * We must have a resonable number of cylinders and sectors so
84578fcd0a1Sachartre 	 * that newfs can run using default values.
84678fcd0a1Sachartre 	 *
84778fcd0a1Sachartre 	 * if (disk_size < 2MB)
84878fcd0a1Sachartre 	 * 	phys_cylinders = disk_size / 100K
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 	 *
85878fcd0a1Sachartre 	 * The file size test is an attempt to not have too few cylinders
85978fcd0a1Sachartre 	 * for a small file, or so many on a big file that you waste space
86078fcd0a1Sachartre 	 * for backup superblocks or cylinder group structures.
86178fcd0a1Sachartre 	 */
862bae9e67eSachartre 	if (disk_size < (2 * 1024 * 1024))
863bae9e67eSachartre 		label->dkl_pcyl = disk_size / (100 * 1024);
86478fcd0a1Sachartre 	else
865bae9e67eSachartre 		label->dkl_pcyl = disk_size / (300 * 1024);
86678fcd0a1Sachartre 
86778fcd0a1Sachartre 	if (label->dkl_pcyl == 0)
86878fcd0a1Sachartre 		label->dkl_pcyl = 1;
86978fcd0a1Sachartre 
870047ba61eSachartre 	label->dkl_acyl = 0;
871047ba61eSachartre 
87278fcd0a1Sachartre 	if (label->dkl_pcyl > 2)
87378fcd0a1Sachartre 		label->dkl_acyl = 2;
87478fcd0a1Sachartre 
875bae9e67eSachartre 	label->dkl_nsect = disk_size / (DEV_BSIZE * label->dkl_pcyl);
87678fcd0a1Sachartre 	label->dkl_ncyl = label->dkl_pcyl - label->dkl_acyl;
87778fcd0a1Sachartre 	label->dkl_nhead = 1;
87878fcd0a1Sachartre 	label->dkl_write_reinstruct = 0;
87978fcd0a1Sachartre 	label->dkl_read_reinstruct = 0;
88078fcd0a1Sachartre 	label->dkl_rpm = 7200;
88178fcd0a1Sachartre 	label->dkl_apc = 0;
88278fcd0a1Sachartre 	label->dkl_intrlv = 0;
88378fcd0a1Sachartre 
884bae9e67eSachartre 	PR0("requested disk size: %ld bytes\n", disk_size);
88578fcd0a1Sachartre 	PR0("setup: ncyl=%d nhead=%d nsec=%d\n", label->dkl_pcyl,
88678fcd0a1Sachartre 	    label->dkl_nhead, label->dkl_nsect);
88778fcd0a1Sachartre 	PR0("provided disk size: %ld bytes\n", (uint64_t)
88878fcd0a1Sachartre 	    (label->dkl_pcyl * label->dkl_nhead *
88978fcd0a1Sachartre 	    label->dkl_nsect * DEV_BSIZE));
89078fcd0a1Sachartre 
891bae9e67eSachartre 	vd_get_readable_size(disk_size, &size, &unit);
89278fcd0a1Sachartre 
89378fcd0a1Sachartre 	/*
89478fcd0a1Sachartre 	 * We must have a correct label name otherwise format(1m) will
89578fcd0a1Sachartre 	 * not recognized the disk as labeled.
89678fcd0a1Sachartre 	 */
89778fcd0a1Sachartre 	(void) snprintf(label->dkl_asciilabel, LEN_DKL_ASCII,
89878fcd0a1Sachartre 	    "SUN-DiskImage-%ld%cB cyl %d alt %d hd %d sec %d",
899bae9e67eSachartre 	    size, unit,
90078fcd0a1Sachartre 	    label->dkl_ncyl, label->dkl_acyl, label->dkl_nhead,
90178fcd0a1Sachartre 	    label->dkl_nsect);
90278fcd0a1Sachartre 
90378fcd0a1Sachartre 	/* default VTOC */
90478fcd0a1Sachartre 	label->dkl_vtoc.v_version = V_VERSION;
905edcc0754Sachartre 	label->dkl_vtoc.v_nparts = V_NUMPAR;
90678fcd0a1Sachartre 	label->dkl_vtoc.v_sanity = VTOC_SANE;
907edcc0754Sachartre 	label->dkl_vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_tag = V_BACKUP;
908edcc0754Sachartre 	label->dkl_map[VD_ENTIRE_DISK_SLICE].dkl_cylno = 0;
909edcc0754Sachartre 	label->dkl_map[VD_ENTIRE_DISK_SLICE].dkl_nblk = label->dkl_ncyl *
91078fcd0a1Sachartre 	    label->dkl_nhead * label->dkl_nsect;
911edcc0754Sachartre 	label->dkl_magic = DKL_MAGIC;
91278fcd0a1Sachartre 	label->dkl_cksum = vd_lbl2cksum(label);
91378fcd0a1Sachartre }
91478fcd0a1Sachartre 
91578fcd0a1Sachartre /*
91678fcd0a1Sachartre  * Function:
91787a7269eSachartre  *	vd_file_set_vtoc
91887a7269eSachartre  *
91987a7269eSachartre  * Description:
92087a7269eSachartre  *	Set the vtoc of a disk image by writing the label and backup
92187a7269eSachartre  *	labels into the disk image backend.
92287a7269eSachartre  *
92387a7269eSachartre  * Parameters:
92487a7269eSachartre  *	vd		- disk on which the operation is performed.
92587a7269eSachartre  *	label		- the data to be written.
92687a7269eSachartre  *
92787a7269eSachartre  * Return Code:
92887a7269eSachartre  *	0		- success.
92987a7269eSachartre  *	n > 0		- error, n indicates the errno code.
93087a7269eSachartre  */
93187a7269eSachartre static int
93287a7269eSachartre vd_file_set_vtoc(vd_t *vd, struct dk_label *label)
93387a7269eSachartre {
93487a7269eSachartre 	int blk, sec, cyl, head, cnt;
93587a7269eSachartre 
93687a7269eSachartre 	ASSERT(vd->file);
93787a7269eSachartre 
93887a7269eSachartre 	if (VD_FILE_LABEL_WRITE(vd, label) < 0) {
93987a7269eSachartre 		PR0("fail to write disk label");
94087a7269eSachartre 		return (EIO);
94187a7269eSachartre 	}
94287a7269eSachartre 
94387a7269eSachartre 	/*
94487a7269eSachartre 	 * Backup labels are on the last alternate cylinder's
94587a7269eSachartre 	 * first five odd sectors.
94687a7269eSachartre 	 */
94787a7269eSachartre 	if (label->dkl_acyl == 0) {
94887a7269eSachartre 		PR0("no alternate cylinder, can not store backup labels");
94987a7269eSachartre 		return (0);
95087a7269eSachartre 	}
95187a7269eSachartre 
95287a7269eSachartre 	cyl = label->dkl_ncyl  + label->dkl_acyl - 1;
95387a7269eSachartre 	head = label->dkl_nhead - 1;
95487a7269eSachartre 
95587a7269eSachartre 	blk = (cyl * ((label->dkl_nhead * label->dkl_nsect) - label->dkl_apc)) +
95687a7269eSachartre 	    (head * label->dkl_nsect);
95787a7269eSachartre 
95887a7269eSachartre 	/*
95987a7269eSachartre 	 * Write the backup labels. Make sure we don't try to write past
96087a7269eSachartre 	 * the last cylinder.
96187a7269eSachartre 	 */
96287a7269eSachartre 	sec = 1;
96387a7269eSachartre 
96487a7269eSachartre 	for (cnt = 0; cnt < VD_FILE_NUM_BACKUP; cnt++) {
96587a7269eSachartre 
96687a7269eSachartre 		if (sec >= label->dkl_nsect) {
96787a7269eSachartre 			PR0("not enough sector to store all backup labels");
96887a7269eSachartre 			return (0);
96987a7269eSachartre 		}
97087a7269eSachartre 
97187a7269eSachartre 		if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)label,
97287a7269eSachartre 		    blk + sec, sizeof (struct dk_label)) < 0) {
97387a7269eSachartre 			PR0("error writing backup label at block %d\n",
97487a7269eSachartre 			    blk + sec);
97587a7269eSachartre 			return (EIO);
97687a7269eSachartre 		}
97787a7269eSachartre 
97887a7269eSachartre 		PR1("wrote backup label at block %d\n", blk + sec);
97987a7269eSachartre 
98087a7269eSachartre 		sec += 2;
98187a7269eSachartre 	}
98287a7269eSachartre 
98387a7269eSachartre 	return (0);
98487a7269eSachartre }
98587a7269eSachartre 
98687a7269eSachartre /*
98787a7269eSachartre  * Function:
98887a7269eSachartre  *	vd_file_get_devid_block
98987a7269eSachartre  *
99087a7269eSachartre  * Description:
99187a7269eSachartre  *	Return the block number where the device id is stored.
99287a7269eSachartre  *
99387a7269eSachartre  * Parameters:
99487a7269eSachartre  *	vd		- disk on which the operation is performed.
99587a7269eSachartre  *	blkp		- pointer to the block number
99687a7269eSachartre  *
99787a7269eSachartre  * Return Code:
99887a7269eSachartre  *	0		- success
99987a7269eSachartre  *	ENOSPC		- disk has no space to store a device id
100087a7269eSachartre  */
100187a7269eSachartre static int
100287a7269eSachartre vd_file_get_devid_block(vd_t *vd, size_t *blkp)
100387a7269eSachartre {
100487a7269eSachartre 	diskaddr_t spc, head, cyl;
100587a7269eSachartre 
100687a7269eSachartre 	ASSERT(vd->file);
1007edcc0754Sachartre 
1008edcc0754Sachartre 	if (vd->vdisk_label == VD_DISK_LABEL_UNK) {
1009edcc0754Sachartre 		/*
1010edcc0754Sachartre 		 * If no label is defined we don't know where to find
1011edcc0754Sachartre 		 * a device id.
1012edcc0754Sachartre 		 */
1013edcc0754Sachartre 		return (ENOSPC);
1014edcc0754Sachartre 	}
1015edcc0754Sachartre 
1016edcc0754Sachartre 	if (vd->vdisk_label == VD_DISK_LABEL_EFI) {
1017edcc0754Sachartre 		/*
1018edcc0754Sachartre 		 * For an EFI disk, the devid is at the beginning of
1019edcc0754Sachartre 		 * the reserved slice
1020edcc0754Sachartre 		 */
1021edcc0754Sachartre 		if (vd->efi_reserved == -1) {
1022edcc0754Sachartre 			PR0("EFI disk has no reserved slice");
1023edcc0754Sachartre 			return (ENOSPC);
1024edcc0754Sachartre 		}
1025edcc0754Sachartre 
1026edcc0754Sachartre 		*blkp = vd->slices[vd->efi_reserved].start;
1027edcc0754Sachartre 		return (0);
1028edcc0754Sachartre 	}
1029edcc0754Sachartre 
103087a7269eSachartre 	ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
103187a7269eSachartre 
103287a7269eSachartre 	/* this geometry doesn't allow us to have a devid */
103387a7269eSachartre 	if (vd->dk_geom.dkg_acyl < 2) {
103487a7269eSachartre 		PR0("not enough alternate cylinder available for devid "
103587a7269eSachartre 		    "(acyl=%u)", vd->dk_geom.dkg_acyl);
103687a7269eSachartre 		return (ENOSPC);
103787a7269eSachartre 	}
103887a7269eSachartre 
103987a7269eSachartre 	/* the devid is in on the track next to the last cylinder */
104087a7269eSachartre 	cyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl - 2;
104187a7269eSachartre 	spc = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
104287a7269eSachartre 	head = vd->dk_geom.dkg_nhead - 1;
104387a7269eSachartre 
104487a7269eSachartre 	*blkp = (cyl * (spc - vd->dk_geom.dkg_apc)) +
104587a7269eSachartre 	    (head * vd->dk_geom.dkg_nsect) + 1;
104687a7269eSachartre 
104787a7269eSachartre 	return (0);
104887a7269eSachartre }
104987a7269eSachartre 
105087a7269eSachartre /*
105187a7269eSachartre  * Return the checksum of a disk block containing an on-disk devid.
105287a7269eSachartre  */
105387a7269eSachartre static uint_t
105487a7269eSachartre vd_dkdevid2cksum(struct dk_devid *dkdevid)
105587a7269eSachartre {
105687a7269eSachartre 	uint_t chksum, *ip;
105787a7269eSachartre 	int i;
105887a7269eSachartre 
105987a7269eSachartre 	chksum = 0;
106087a7269eSachartre 	ip = (uint_t *)dkdevid;
106187a7269eSachartre 	for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int)); i++)
106287a7269eSachartre 		chksum ^= ip[i];
106387a7269eSachartre 
106487a7269eSachartre 	return (chksum);
106587a7269eSachartre }
106687a7269eSachartre 
106787a7269eSachartre /*
106887a7269eSachartre  * Function:
106987a7269eSachartre  *	vd_file_read_devid
107087a7269eSachartre  *
107187a7269eSachartre  * Description:
107287a7269eSachartre  *	Read the device id stored on a disk image.
107387a7269eSachartre  *
107487a7269eSachartre  * Parameters:
107587a7269eSachartre  *	vd		- disk on which the operation is performed.
107687a7269eSachartre  *	devid		- the return address of the device ID.
107787a7269eSachartre  *
107887a7269eSachartre  * Return Code:
107987a7269eSachartre  *	0		- success
108087a7269eSachartre  *	EIO		- I/O error while trying to access the disk image
108187a7269eSachartre  *	EINVAL		- no valid device id was found
108287a7269eSachartre  *	ENOSPC		- disk has no space to store a device id
108387a7269eSachartre  */
108487a7269eSachartre static int
108587a7269eSachartre vd_file_read_devid(vd_t *vd, ddi_devid_t *devid)
108687a7269eSachartre {
108787a7269eSachartre 	struct dk_devid *dkdevid;
108887a7269eSachartre 	size_t blk;
108987a7269eSachartre 	uint_t chksum;
109087a7269eSachartre 	int status, sz;
109187a7269eSachartre 
109287a7269eSachartre 	if ((status = vd_file_get_devid_block(vd, &blk)) != 0)
109387a7269eSachartre 		return (status);
109487a7269eSachartre 
109587a7269eSachartre 	dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP);
109687a7269eSachartre 
109787a7269eSachartre 	/* get the devid */
109887a7269eSachartre 	if ((vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)dkdevid, blk,
109987a7269eSachartre 	    DEV_BSIZE)) < 0) {
110087a7269eSachartre 		PR0("error reading devid block at %lu", blk);
110187a7269eSachartre 		status = EIO;
110287a7269eSachartre 		goto done;
110387a7269eSachartre 	}
110487a7269eSachartre 
110587a7269eSachartre 	/* validate the revision */
110687a7269eSachartre 	if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
110787a7269eSachartre 	    (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
110887a7269eSachartre 		PR0("invalid devid found at block %lu (bad revision)", blk);
110987a7269eSachartre 		status = EINVAL;
111087a7269eSachartre 		goto done;
111187a7269eSachartre 	}
111287a7269eSachartre 
111387a7269eSachartre 	/* compute checksum */
111487a7269eSachartre 	chksum = vd_dkdevid2cksum(dkdevid);
111587a7269eSachartre 
111687a7269eSachartre 	/* compare the checksums */
111787a7269eSachartre 	if (DKD_GETCHKSUM(dkdevid) != chksum) {
111887a7269eSachartre 		PR0("invalid devid found at block %lu (bad checksum)", blk);
111987a7269eSachartre 		status = EINVAL;
112087a7269eSachartre 		goto done;
112187a7269eSachartre 	}
112287a7269eSachartre 
112387a7269eSachartre 	/* validate the device id */
112487a7269eSachartre 	if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
112587a7269eSachartre 		PR0("invalid devid found at block %lu", blk);
112687a7269eSachartre 		status = EINVAL;
112787a7269eSachartre 		goto done;
112887a7269eSachartre 	}
112987a7269eSachartre 
113087a7269eSachartre 	PR1("devid read at block %lu", blk);
113187a7269eSachartre 
113287a7269eSachartre 	sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
113387a7269eSachartre 	*devid = kmem_alloc(sz, KM_SLEEP);
113487a7269eSachartre 	bcopy(&dkdevid->dkd_devid, *devid, sz);
113587a7269eSachartre 
113687a7269eSachartre done:
113787a7269eSachartre 	kmem_free(dkdevid, DEV_BSIZE);
113887a7269eSachartre 	return (status);
113987a7269eSachartre 
114087a7269eSachartre }
114187a7269eSachartre 
114287a7269eSachartre /*
114387a7269eSachartre  * Function:
114487a7269eSachartre  *	vd_file_write_devid
114587a7269eSachartre  *
114687a7269eSachartre  * Description:
114787a7269eSachartre  *	Write a device id into disk image.
114887a7269eSachartre  *
114987a7269eSachartre  * Parameters:
115087a7269eSachartre  *	vd		- disk on which the operation is performed.
115187a7269eSachartre  *	devid		- the device ID to store.
115287a7269eSachartre  *
115387a7269eSachartre  * Return Code:
115487a7269eSachartre  *	0		- success
115587a7269eSachartre  *	EIO		- I/O error while trying to access the disk image
115687a7269eSachartre  *	ENOSPC		- disk has no space to store a device id
115787a7269eSachartre  */
115887a7269eSachartre static int
115987a7269eSachartre vd_file_write_devid(vd_t *vd, ddi_devid_t devid)
116087a7269eSachartre {
116187a7269eSachartre 	struct dk_devid *dkdevid;
116287a7269eSachartre 	uint_t chksum;
116387a7269eSachartre 	size_t blk;
116487a7269eSachartre 	int status;
116587a7269eSachartre 
1166edcc0754Sachartre 	if (devid == NULL) {
1167edcc0754Sachartre 		/* nothing to write */
1168edcc0754Sachartre 		return (0);
1169edcc0754Sachartre 	}
1170edcc0754Sachartre 
117187a7269eSachartre 	if ((status = vd_file_get_devid_block(vd, &blk)) != 0)
117287a7269eSachartre 		return (status);
117387a7269eSachartre 
117487a7269eSachartre 	dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP);
117587a7269eSachartre 
117687a7269eSachartre 	/* set revision */
117787a7269eSachartre 	dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
117887a7269eSachartre 	dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
117987a7269eSachartre 
118087a7269eSachartre 	/* copy devid */
118187a7269eSachartre 	bcopy(devid, &dkdevid->dkd_devid, ddi_devid_sizeof(devid));
118287a7269eSachartre 
118387a7269eSachartre 	/* compute checksum */
118487a7269eSachartre 	chksum = vd_dkdevid2cksum(dkdevid);
118587a7269eSachartre 
118687a7269eSachartre 	/* set checksum */
118787a7269eSachartre 	DKD_FORMCHKSUM(chksum, dkdevid);
118887a7269eSachartre 
118987a7269eSachartre 	/* store the devid */
119087a7269eSachartre 	if ((status = vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE,
119187a7269eSachartre 	    (caddr_t)dkdevid, blk, DEV_BSIZE)) < 0) {
119287a7269eSachartre 		PR0("Error writing devid block at %lu", blk);
119387a7269eSachartre 		status = EIO;
119487a7269eSachartre 	} else {
119587a7269eSachartre 		PR1("devid written at block %lu", blk);
119687a7269eSachartre 		status = 0;
119787a7269eSachartre 	}
119887a7269eSachartre 
119987a7269eSachartre 	kmem_free(dkdevid, DEV_BSIZE);
120087a7269eSachartre 	return (status);
120187a7269eSachartre }
120287a7269eSachartre 
120387a7269eSachartre /*
120487a7269eSachartre  * Function:
120517cadca8Slm66018  *	vd_do_scsi_rdwr
120687a7269eSachartre  *
120787a7269eSachartre  * Description:
120887a7269eSachartre  * 	Read or write to a SCSI disk using an absolute disk offset.
120987a7269eSachartre  *
121087a7269eSachartre  * Parameters:
121187a7269eSachartre  *	vd		- disk on which the operation is performed.
121287a7269eSachartre  *	operation	- operation to execute: read (VD_OP_BREAD) or
121387a7269eSachartre  *			  write (VD_OP_BWRITE).
121487a7269eSachartre  *	data		- buffer where data are read to or written from.
121587a7269eSachartre  *	blk		- starting block for the operation.
121687a7269eSachartre  *	len		- number of bytes to read or write.
121787a7269eSachartre  *
121887a7269eSachartre  * Return Code:
121987a7269eSachartre  *	0		- success
122087a7269eSachartre  *	n != 0		- error.
122187a7269eSachartre  */
122287a7269eSachartre static int
122317cadca8Slm66018 vd_do_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t blk, size_t len)
122487a7269eSachartre {
122587a7269eSachartre 	struct uscsi_cmd ucmd;
122687a7269eSachartre 	union scsi_cdb cdb;
122787a7269eSachartre 	int nsectors, nblk;
122887a7269eSachartre 	int max_sectors;
122987a7269eSachartre 	int status, rval;
123087a7269eSachartre 
123187a7269eSachartre 	ASSERT(!vd->file);
123217cadca8Slm66018 	ASSERT(vd->vdisk_block_size > 0);
123387a7269eSachartre 
123487a7269eSachartre 	max_sectors = vd->max_xfer_sz;
123517cadca8Slm66018 	nblk = (len / vd->vdisk_block_size);
123687a7269eSachartre 
123717cadca8Slm66018 	if (len % vd->vdisk_block_size != 0)
123887a7269eSachartre 		return (EINVAL);
123987a7269eSachartre 
124087a7269eSachartre 	/*
124187a7269eSachartre 	 * Build and execute the uscsi ioctl.  We build a group0, group1
124287a7269eSachartre 	 * or group4 command as necessary, since some targets
124387a7269eSachartre 	 * do not support group1 commands.
124487a7269eSachartre 	 */
124587a7269eSachartre 	while (nblk) {
124687a7269eSachartre 
124787a7269eSachartre 		bzero(&ucmd, sizeof (ucmd));
124887a7269eSachartre 		bzero(&cdb, sizeof (cdb));
124987a7269eSachartre 
125087a7269eSachartre 		nsectors = (max_sectors < nblk) ? max_sectors : nblk;
125187a7269eSachartre 
125217cadca8Slm66018 		/*
125317cadca8Slm66018 		 * Some of the optical drives on sun4v machines are ATAPI
125417cadca8Slm66018 		 * devices which use Group 1 Read/Write commands so we need
125517cadca8Slm66018 		 * to explicitly check a flag which is set when a domain
125617cadca8Slm66018 		 * is bound.
125717cadca8Slm66018 		 */
125817cadca8Slm66018 		if (blk < (2 << 20) && nsectors <= 0xff && !vd->is_atapi_dev) {
125987a7269eSachartre 			FORMG0ADDR(&cdb, blk);
126087a7269eSachartre 			FORMG0COUNT(&cdb, nsectors);
126187a7269eSachartre 			ucmd.uscsi_cdblen = CDB_GROUP0;
126287a7269eSachartre 		} else if (blk > 0xffffffff) {
126387a7269eSachartre 			FORMG4LONGADDR(&cdb, blk);
126487a7269eSachartre 			FORMG4COUNT(&cdb, nsectors);
126587a7269eSachartre 			ucmd.uscsi_cdblen = CDB_GROUP4;
126687a7269eSachartre 			cdb.scc_cmd |= SCMD_GROUP4;
126787a7269eSachartre 		} else {
126887a7269eSachartre 			FORMG1ADDR(&cdb, blk);
126987a7269eSachartre 			FORMG1COUNT(&cdb, nsectors);
127087a7269eSachartre 			ucmd.uscsi_cdblen = CDB_GROUP1;
127187a7269eSachartre 			cdb.scc_cmd |= SCMD_GROUP1;
127287a7269eSachartre 		}
127387a7269eSachartre 		ucmd.uscsi_cdb = (caddr_t)&cdb;
127487a7269eSachartre 		ucmd.uscsi_bufaddr = data;
127517cadca8Slm66018 		ucmd.uscsi_buflen = nsectors * vd->block_size;
127687a7269eSachartre 		ucmd.uscsi_timeout = vd_scsi_rdwr_timeout;
127787a7269eSachartre 		/*
127887a7269eSachartre 		 * Set flags so that the command is isolated from normal
127987a7269eSachartre 		 * commands and no error message is printed.
128087a7269eSachartre 		 */
128187a7269eSachartre 		ucmd.uscsi_flags = USCSI_ISOLATE | USCSI_SILENT;
128287a7269eSachartre 
128387a7269eSachartre 		if (operation == VD_OP_BREAD) {
128487a7269eSachartre 			cdb.scc_cmd |= SCMD_READ;
128587a7269eSachartre 			ucmd.uscsi_flags |= USCSI_READ;
128687a7269eSachartre 		} else {
128787a7269eSachartre 			cdb.scc_cmd |= SCMD_WRITE;
128887a7269eSachartre 		}
128987a7269eSachartre 
129087a7269eSachartre 		status = ldi_ioctl(vd->ldi_handle[VD_ENTIRE_DISK_SLICE],
1291047ba61eSachartre 		    USCSICMD, (intptr_t)&ucmd, (vd->open_flags | FKIOCTL),
129287a7269eSachartre 		    kcred, &rval);
129387a7269eSachartre 
129487a7269eSachartre 		if (status == 0)
129587a7269eSachartre 			status = ucmd.uscsi_status;
129687a7269eSachartre 
129787a7269eSachartre 		if (status != 0)
129887a7269eSachartre 			break;
129987a7269eSachartre 
130087a7269eSachartre 		/*
130187a7269eSachartre 		 * Check if partial DMA breakup is required. If so, reduce
130287a7269eSachartre 		 * the request size by half and retry the last request.
130387a7269eSachartre 		 */
130487a7269eSachartre 		if (ucmd.uscsi_resid == ucmd.uscsi_buflen) {
130587a7269eSachartre 			max_sectors >>= 1;
130687a7269eSachartre 			if (max_sectors <= 0) {
130787a7269eSachartre 				status = EIO;
130887a7269eSachartre 				break;
130987a7269eSachartre 			}
131087a7269eSachartre 			continue;
131187a7269eSachartre 		}
131287a7269eSachartre 
131387a7269eSachartre 		if (ucmd.uscsi_resid != 0) {
131487a7269eSachartre 			status = EIO;
131587a7269eSachartre 			break;
131687a7269eSachartre 		}
131787a7269eSachartre 
131887a7269eSachartre 		blk += nsectors;
131987a7269eSachartre 		nblk -= nsectors;
132017cadca8Slm66018 		data += nsectors * vd->vdisk_block_size; /* SECSIZE */
132187a7269eSachartre 	}
132287a7269eSachartre 
132387a7269eSachartre 	return (status);
132487a7269eSachartre }
132587a7269eSachartre 
1326205eeb1aSlm66018 /*
132717cadca8Slm66018  * Function:
132817cadca8Slm66018  *	vd_scsi_rdwr
132917cadca8Slm66018  *
133017cadca8Slm66018  * Description:
133117cadca8Slm66018  * 	Wrapper function to read or write to a SCSI disk using an absolute
133217cadca8Slm66018  *	disk offset. It checks the blocksize of the underlying device and,
133317cadca8Slm66018  *	if necessary, adjusts the buffers accordingly before calling
133417cadca8Slm66018  *	vd_do_scsi_rdwr() to do the actual read or write.
133517cadca8Slm66018  *
133617cadca8Slm66018  * Parameters:
133717cadca8Slm66018  *	vd		- disk on which the operation is performed.
133817cadca8Slm66018  *	operation	- operation to execute: read (VD_OP_BREAD) or
133917cadca8Slm66018  *			  write (VD_OP_BWRITE).
134017cadca8Slm66018  *	data		- buffer where data are read to or written from.
134117cadca8Slm66018  *	blk		- starting block for the operation.
134217cadca8Slm66018  *	len		- number of bytes to read or write.
134317cadca8Slm66018  *
134417cadca8Slm66018  * Return Code:
134517cadca8Slm66018  *	0		- success
134617cadca8Slm66018  *	n != 0		- error.
134717cadca8Slm66018  */
134817cadca8Slm66018 static int
134917cadca8Slm66018 vd_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t vblk, size_t vlen)
135017cadca8Slm66018 {
135117cadca8Slm66018 	int	rv;
135217cadca8Slm66018 
135317cadca8Slm66018 	size_t	pblk;	/* physical device block number of data on device */
135417cadca8Slm66018 	size_t	delta;	/* relative offset between pblk and vblk */
135517cadca8Slm66018 	size_t	pnblk;	/* number of physical blocks to be read from device */
135617cadca8Slm66018 	size_t	plen;	/* length of data to be read from physical device */
135717cadca8Slm66018 	char	*buf;	/* buffer area to fit physical device's block size */
135817cadca8Slm66018 
13592f5224aeSachartre 	if (vd->block_size == 0) {
13602f5224aeSachartre 		/*
13612f5224aeSachartre 		 * The block size was not available during the attach,
13622f5224aeSachartre 		 * try to update it now.
13632f5224aeSachartre 		 */
13642f5224aeSachartre 		if (vd_setup_mediainfo(vd) != 0)
13652f5224aeSachartre 			return (EIO);
13662f5224aeSachartre 	}
13672f5224aeSachartre 
136817cadca8Slm66018 	/*
136917cadca8Slm66018 	 * If the vdisk block size and the block size of the underlying device
137017cadca8Slm66018 	 * match we can skip straight to vd_do_scsi_rdwr(), otherwise we need
137117cadca8Slm66018 	 * to create a buffer large enough to handle the device's block size
137217cadca8Slm66018 	 * and adjust the block to be read from and the amount of data to
137317cadca8Slm66018 	 * read to correspond with the device's block size.
137417cadca8Slm66018 	 */
137517cadca8Slm66018 	if (vd->vdisk_block_size == vd->block_size)
137617cadca8Slm66018 		return (vd_do_scsi_rdwr(vd, operation, data, vblk, vlen));
137717cadca8Slm66018 
137817cadca8Slm66018 	if (vd->vdisk_block_size > vd->block_size)
137917cadca8Slm66018 		return (EINVAL);
138017cadca8Slm66018 
138117cadca8Slm66018 	/*
138217cadca8Slm66018 	 * Writing of physical block sizes larger than the virtual block size
138317cadca8Slm66018 	 * is not supported. This would be added if/when support for guests
138417cadca8Slm66018 	 * writing to DVDs is implemented.
138517cadca8Slm66018 	 */
138617cadca8Slm66018 	if (operation == VD_OP_BWRITE)
138717cadca8Slm66018 		return (ENOTSUP);
138817cadca8Slm66018 
138917cadca8Slm66018 	/* BEGIN CSTYLED */
139017cadca8Slm66018 	/*
139117cadca8Slm66018 	 * Below is a diagram showing the relationship between the physical
139217cadca8Slm66018 	 * and virtual blocks. If the virtual blocks marked by 'X' below are
139317cadca8Slm66018 	 * requested, then the physical blocks denoted by 'Y' are read.
139417cadca8Slm66018 	 *
139517cadca8Slm66018 	 *           vblk
139617cadca8Slm66018 	 *             |      vlen
139717cadca8Slm66018 	 *             |<--------------->|
139817cadca8Slm66018 	 *             v                 v
139917cadca8Slm66018 	 *  --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-   virtual disk:
140017cadca8Slm66018 	 *    |  |  |  |XX|XX|XX|XX|XX|XX|  |  |  |  |  |  } block size is
140117cadca8Slm66018 	 *  --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-  vd->vdisk_block_size
140217cadca8Slm66018 	 *          :  :                 :  :
140317cadca8Slm66018 	 *         >:==:< delta          :  :
140417cadca8Slm66018 	 *          :  :                 :  :
140517cadca8Slm66018 	 *  --+-----+-----+-----+-----+-----+-----+-----+--   physical disk:
140617cadca8Slm66018 	 *    |     |YY:YY|YYYYY|YYYYY|YY:YY|     |     |   } block size is
140717cadca8Slm66018 	 *  --+-----+-----+-----+-----+-----+-----+-----+--   vd->block_size
140817cadca8Slm66018 	 *          ^                       ^
140917cadca8Slm66018 	 *          |<--------------------->|
141017cadca8Slm66018 	 *          |         plen
141117cadca8Slm66018 	 *         pblk
141217cadca8Slm66018 	 */
141317cadca8Slm66018 	/* END CSTYLED */
141417cadca8Slm66018 	pblk = (vblk * vd->vdisk_block_size) / vd->block_size;
141517cadca8Slm66018 	delta = (vblk * vd->vdisk_block_size) - (pblk * vd->block_size);
141617cadca8Slm66018 	pnblk = ((delta + vlen - 1) / vd->block_size) + 1;
141717cadca8Slm66018 	plen = pnblk * vd->block_size;
141817cadca8Slm66018 
141917cadca8Slm66018 	PR2("vblk %lx:pblk %lx: vlen %ld:plen %ld", vblk, pblk, vlen, plen);
142017cadca8Slm66018 
142117cadca8Slm66018 	buf = kmem_zalloc(sizeof (caddr_t) * plen, KM_SLEEP);
142217cadca8Slm66018 	rv = vd_do_scsi_rdwr(vd, operation, (caddr_t)buf, pblk, plen);
142317cadca8Slm66018 	bcopy(buf + delta, data, vlen);
142417cadca8Slm66018 
142517cadca8Slm66018 	kmem_free(buf, sizeof (caddr_t) * plen);
142617cadca8Slm66018 
142717cadca8Slm66018 	return (rv);
142817cadca8Slm66018 }
142917cadca8Slm66018 
143017cadca8Slm66018 /*
1431bae9e67eSachartre  * Function:
1432bae9e67eSachartre  *	vd_slice_flabel_read
1433bae9e67eSachartre  *
1434bae9e67eSachartre  * Description:
1435bae9e67eSachartre  *	This function simulates a read operation from the fake label of
1436bae9e67eSachartre  *	a single-slice disk.
1437bae9e67eSachartre  *
1438bae9e67eSachartre  * Parameters:
1439bae9e67eSachartre  *	vd		- single-slice disk to read from
1440bae9e67eSachartre  *	data		- buffer where data should be read to
1441bae9e67eSachartre  *	offset		- offset in byte where the read should start
1442bae9e67eSachartre  *	length		- number of bytes to read
1443bae9e67eSachartre  *
1444bae9e67eSachartre  * Return Code:
1445bae9e67eSachartre  *	n >= 0		- success, n indicates the number of bytes read
1446bae9e67eSachartre  *	-1		- error
1447bae9e67eSachartre  */
1448bae9e67eSachartre static ssize_t
1449bae9e67eSachartre vd_slice_flabel_read(vd_t *vd, caddr_t data, size_t offset, size_t length)
1450bae9e67eSachartre {
1451bae9e67eSachartre 	size_t n = 0;
1452bae9e67eSachartre 	uint_t limit = vd->flabel_limit * DEV_BSIZE;
1453bae9e67eSachartre 
1454bae9e67eSachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
1455bae9e67eSachartre 	ASSERT(vd->flabel != NULL);
1456bae9e67eSachartre 
1457bae9e67eSachartre 	/* if offset is past the fake label limit there's nothing to read */
1458bae9e67eSachartre 	if (offset >= limit)
1459bae9e67eSachartre 		return (0);
1460bae9e67eSachartre 
1461bae9e67eSachartre 	/* data with offset 0 to flabel_size are read from flabel */
1462bae9e67eSachartre 	if (offset < vd->flabel_size) {
1463bae9e67eSachartre 
1464bae9e67eSachartre 		if (offset + length <= vd->flabel_size) {
1465bae9e67eSachartre 			bcopy(vd->flabel + offset, data, length);
1466bae9e67eSachartre 			return (length);
1467bae9e67eSachartre 		}
1468bae9e67eSachartre 
1469bae9e67eSachartre 		n = vd->flabel_size - offset;
1470bae9e67eSachartre 		bcopy(vd->flabel + offset, data, n);
1471bae9e67eSachartre 		data += n;
1472bae9e67eSachartre 	}
1473bae9e67eSachartre 
1474bae9e67eSachartre 	/* data with offset from flabel_size to flabel_limit are all zeros */
1475bae9e67eSachartre 	if (offset + length <= limit) {
1476bae9e67eSachartre 		bzero(data, length - n);
1477bae9e67eSachartre 		return (length);
1478bae9e67eSachartre 	}
1479bae9e67eSachartre 
1480bae9e67eSachartre 	bzero(data, limit - offset - n);
1481bae9e67eSachartre 	return (limit - offset);
1482bae9e67eSachartre }
1483bae9e67eSachartre 
1484bae9e67eSachartre /*
1485bae9e67eSachartre  * Function:
1486bae9e67eSachartre  *	vd_slice_flabel_write
1487bae9e67eSachartre  *
1488bae9e67eSachartre  * Description:
1489bae9e67eSachartre  *	This function simulates a write operation to the fake label of
1490bae9e67eSachartre  *	a single-slice disk. Write operations are actually faked and return
1491bae9e67eSachartre  *	success although the label is never changed. This is mostly to
1492bae9e67eSachartre  *	simulate a successful label update.
1493bae9e67eSachartre  *
1494bae9e67eSachartre  * Parameters:
1495bae9e67eSachartre  *	vd		- single-slice disk to write to
1496bae9e67eSachartre  *	data		- buffer where data should be written from
1497bae9e67eSachartre  *	offset		- offset in byte where the write should start
1498bae9e67eSachartre  *	length		- number of bytes to written
1499bae9e67eSachartre  *
1500bae9e67eSachartre  * Return Code:
1501bae9e67eSachartre  *	n >= 0		- success, n indicates the number of bytes written
1502bae9e67eSachartre  *	-1		- error
1503bae9e67eSachartre  */
1504bae9e67eSachartre static ssize_t
1505bae9e67eSachartre vd_slice_flabel_write(vd_t *vd, caddr_t data, size_t offset, size_t length)
1506bae9e67eSachartre {
1507bae9e67eSachartre 	uint_t limit = vd->flabel_limit * DEV_BSIZE;
1508bae9e67eSachartre 	struct dk_label *label;
1509bae9e67eSachartre 	struct dk_geom geom;
1510bae9e67eSachartre 	struct vtoc vtoc;
1511bae9e67eSachartre 
1512bae9e67eSachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
1513bae9e67eSachartre 	ASSERT(vd->flabel != NULL);
1514bae9e67eSachartre 
1515bae9e67eSachartre 	if (offset >= limit)
1516bae9e67eSachartre 		return (0);
1517bae9e67eSachartre 
1518bae9e67eSachartre 	/*
1519bae9e67eSachartre 	 * If this is a request to overwrite the VTOC disk label, check that
1520bae9e67eSachartre 	 * the new label is similar to the previous one and return that the
1521bae9e67eSachartre 	 * write was successful, but note that nothing is actually overwritten.
1522bae9e67eSachartre 	 */
1523bae9e67eSachartre 	if (vd->vdisk_label == VD_DISK_LABEL_VTOC &&
1524bae9e67eSachartre 	    offset == 0 && length == DEV_BSIZE) {
1525bae9e67eSachartre 		label = (struct dk_label *)data;
1526bae9e67eSachartre 
1527bae9e67eSachartre 		/* check that this is a valid label */
1528bae9e67eSachartre 		if (label->dkl_magic != DKL_MAGIC ||
1529bae9e67eSachartre 		    label->dkl_cksum != vd_lbl2cksum(label))
1530bae9e67eSachartre 			return (-1);
1531bae9e67eSachartre 
1532bae9e67eSachartre 		/* check the vtoc and geometry */
1533bae9e67eSachartre 		vd_label_to_vtocgeom(label, &vtoc, &geom);
1534bae9e67eSachartre 		if (vd_slice_geom_isvalid(vd, &geom) &&
1535bae9e67eSachartre 		    vd_slice_vtoc_isvalid(vd, &vtoc))
1536bae9e67eSachartre 			return (length);
1537bae9e67eSachartre 	}
1538bae9e67eSachartre 
1539bae9e67eSachartre 	/* fail any other write */
1540bae9e67eSachartre 	return (-1);
1541bae9e67eSachartre }
1542bae9e67eSachartre 
1543bae9e67eSachartre /*
1544bae9e67eSachartre  * Function:
1545bae9e67eSachartre  *	vd_slice_fake_rdwr
1546bae9e67eSachartre  *
1547bae9e67eSachartre  * Description:
1548bae9e67eSachartre  *	This function simulates a raw read or write operation to a single-slice
1549bae9e67eSachartre  *	disk. It only handles the faked part of the operation i.e. I/Os to
1550bae9e67eSachartre  *	blocks which have no mapping with the vdisk backend (I/Os to the
1551bae9e67eSachartre  *	beginning and to the end of the vdisk).
1552bae9e67eSachartre  *
1553bae9e67eSachartre  *	The function returns 0 is the operation	is completed and it has been
1554bae9e67eSachartre  *	entirely handled as a fake read or write. In that case, lengthp points
1555bae9e67eSachartre  *	to the number of bytes not read or written. Values returned by datap
1556bae9e67eSachartre  *	and blkp are undefined.
1557bae9e67eSachartre  *
1558bae9e67eSachartre  *	If the fake operation has succeeded but the read or write is not
1559bae9e67eSachartre  *	complete (i.e. the read/write operation extends beyond the blocks
1560bae9e67eSachartre  *	we fake) then the function returns EAGAIN and datap, blkp and lengthp
1561bae9e67eSachartre  *	pointers points to the parameters for completing the operation.
1562bae9e67eSachartre  *
1563bae9e67eSachartre  *	In case of an error, for example if the slice is empty or parameters
1564bae9e67eSachartre  *	are invalid, then the function returns a non-zero value different
1565bae9e67eSachartre  *	from EAGAIN. In that case, the returned values of datap, blkp and
1566bae9e67eSachartre  *	lengthp are undefined.
1567bae9e67eSachartre  *
1568bae9e67eSachartre  * Parameters:
1569bae9e67eSachartre  *	vd		- single-slice disk on which the operation is performed
1570bae9e67eSachartre  *	slice		- slice on which the operation is performed,
1571bae9e67eSachartre  *			  VD_SLICE_NONE indicates that the operation
1572bae9e67eSachartre  *			  is done using an absolute disk offset.
1573bae9e67eSachartre  *	operation	- operation to execute: read (VD_OP_BREAD) or
1574bae9e67eSachartre  *			  write (VD_OP_BWRITE).
1575bae9e67eSachartre  *	datap		- pointer to the buffer where data are read to
1576bae9e67eSachartre  *			  or written from. Return the pointer where remaining
1577bae9e67eSachartre  *			  data have to be read to or written from.
1578bae9e67eSachartre  *	blkp		- pointer to the starting block for the operation.
1579bae9e67eSachartre  *			  Return the starting block relative to the vdisk
1580bae9e67eSachartre  *			  backend for the remaining operation.
1581bae9e67eSachartre  *	lengthp		- pointer to the number of bytes to read or write.
1582bae9e67eSachartre  *			  This should be a multiple of DEV_BSIZE. Return the
1583bae9e67eSachartre  *			  remaining number of bytes to read or write.
1584bae9e67eSachartre  *
1585bae9e67eSachartre  * Return Code:
1586bae9e67eSachartre  *	0		- read/write operation is completed
1587bae9e67eSachartre  *	EAGAIN		- read/write operation is not completed
1588bae9e67eSachartre  *	other values	- error
1589bae9e67eSachartre  */
1590bae9e67eSachartre static int
1591bae9e67eSachartre vd_slice_fake_rdwr(vd_t *vd, int slice, int operation, caddr_t *datap,
1592bae9e67eSachartre     size_t *blkp, size_t *lengthp)
1593bae9e67eSachartre {
1594bae9e67eSachartre 	struct dk_label *label;
1595bae9e67eSachartre 	caddr_t data;
1596bae9e67eSachartre 	size_t blk, length, csize;
1597bae9e67eSachartre 	size_t ablk, asize, aoff, alen;
1598bae9e67eSachartre 	ssize_t n;
1599bae9e67eSachartre 	int sec, status;
1600bae9e67eSachartre 
1601bae9e67eSachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
1602bae9e67eSachartre 	ASSERT(slice != 0);
1603bae9e67eSachartre 
1604bae9e67eSachartre 	data = *datap;
1605bae9e67eSachartre 	blk = *blkp;
1606bae9e67eSachartre 	length = *lengthp;
1607bae9e67eSachartre 
1608bae9e67eSachartre 	/*
1609bae9e67eSachartre 	 * If this is not a raw I/O or an I/O from a full disk slice then
1610bae9e67eSachartre 	 * this is an I/O to/from an empty slice.
1611bae9e67eSachartre 	 */
1612bae9e67eSachartre 	if (slice != VD_SLICE_NONE &&
1613bae9e67eSachartre 	    (slice != VD_ENTIRE_DISK_SLICE ||
1614bae9e67eSachartre 	    vd->vdisk_label != VD_DISK_LABEL_VTOC) &&
1615bae9e67eSachartre 	    (slice != VD_EFI_WD_SLICE ||
1616bae9e67eSachartre 	    vd->vdisk_label != VD_DISK_LABEL_EFI)) {
1617bae9e67eSachartre 		return (EIO);
1618bae9e67eSachartre 	}
1619bae9e67eSachartre 
1620bae9e67eSachartre 	if (length % DEV_BSIZE != 0)
1621bae9e67eSachartre 		return (EINVAL);
1622bae9e67eSachartre 
1623bae9e67eSachartre 	/* handle any I/O with the fake label */
1624bae9e67eSachartre 	if (operation == VD_OP_BWRITE)
1625bae9e67eSachartre 		n = vd_slice_flabel_write(vd, data, blk * DEV_BSIZE, length);
1626bae9e67eSachartre 	else
1627bae9e67eSachartre 		n = vd_slice_flabel_read(vd, data, blk * DEV_BSIZE, length);
1628bae9e67eSachartre 
1629bae9e67eSachartre 	if (n == -1)
1630bae9e67eSachartre 		return (EINVAL);
1631bae9e67eSachartre 
1632bae9e67eSachartre 	ASSERT(n % DEV_BSIZE == 0);
1633bae9e67eSachartre 
1634bae9e67eSachartre 	/* adjust I/O arguments */
1635bae9e67eSachartre 	data += n;
1636bae9e67eSachartre 	blk += n / DEV_BSIZE;
1637bae9e67eSachartre 	length -= n;
1638bae9e67eSachartre 
1639bae9e67eSachartre 	/* check if there's something else to process */
1640bae9e67eSachartre 	if (length == 0) {
1641bae9e67eSachartre 		status = 0;
1642bae9e67eSachartre 		goto done;
1643bae9e67eSachartre 	}
1644bae9e67eSachartre 
1645bae9e67eSachartre 	if (vd->vdisk_label == VD_DISK_LABEL_VTOC &&
1646bae9e67eSachartre 	    slice == VD_ENTIRE_DISK_SLICE) {
1647bae9e67eSachartre 		status = EAGAIN;
1648bae9e67eSachartre 		goto done;
1649bae9e67eSachartre 	}
1650bae9e67eSachartre 
1651bae9e67eSachartre 	if (vd->vdisk_label == VD_DISK_LABEL_EFI) {
1652bae9e67eSachartre 		asize = EFI_MIN_RESV_SIZE + 33;
1653bae9e67eSachartre 		ablk = vd->vdisk_size - asize;
1654bae9e67eSachartre 	} else {
1655bae9e67eSachartre 		ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
1656bae9e67eSachartre 		ASSERT(vd->dk_geom.dkg_apc == 0);
1657bae9e67eSachartre 
1658bae9e67eSachartre 		csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
1659bae9e67eSachartre 		ablk = vd->dk_geom.dkg_ncyl * csize;
1660bae9e67eSachartre 		asize = vd->dk_geom.dkg_acyl * csize;
1661bae9e67eSachartre 	}
1662bae9e67eSachartre 
1663bae9e67eSachartre 	alen = length / DEV_BSIZE;
1664bae9e67eSachartre 	aoff = blk;
1665bae9e67eSachartre 
1666bae9e67eSachartre 	/* if we have reached the last block then the I/O is completed */
1667bae9e67eSachartre 	if (aoff == ablk + asize) {
1668bae9e67eSachartre 		status = 0;
1669bae9e67eSachartre 		goto done;
1670bae9e67eSachartre 	}
1671bae9e67eSachartre 
1672bae9e67eSachartre 	/* if we are past the last block then return an error */
1673bae9e67eSachartre 	if (aoff > ablk + asize)
1674bae9e67eSachartre 		return (EIO);
1675bae9e67eSachartre 
1676bae9e67eSachartre 	/* check if there is any I/O to end of the disk */
1677bae9e67eSachartre 	if (aoff + alen < ablk) {
1678bae9e67eSachartre 		status = EAGAIN;
1679bae9e67eSachartre 		goto done;
1680bae9e67eSachartre 	}
1681bae9e67eSachartre 
1682bae9e67eSachartre 	/* we don't allow any write to the end of the disk */
1683bae9e67eSachartre 	if (operation == VD_OP_BWRITE)
1684bae9e67eSachartre 		return (EIO);
1685bae9e67eSachartre 
1686bae9e67eSachartre 	if (aoff < ablk) {
1687bae9e67eSachartre 		alen -= (ablk - aoff);
1688bae9e67eSachartre 		aoff = ablk;
1689bae9e67eSachartre 	}
1690bae9e67eSachartre 
1691bae9e67eSachartre 	if (aoff + alen > ablk + asize) {
1692bae9e67eSachartre 		alen = ablk + asize - aoff;
1693bae9e67eSachartre 	}
1694bae9e67eSachartre 
1695bae9e67eSachartre 	alen *= DEV_BSIZE;
1696bae9e67eSachartre 
1697bae9e67eSachartre 	if (operation == VD_OP_BREAD) {
1698bae9e67eSachartre 		bzero(data + (aoff - blk) * DEV_BSIZE, alen);
1699bae9e67eSachartre 
1700bae9e67eSachartre 		if (vd->vdisk_label == VD_DISK_LABEL_VTOC) {
1701bae9e67eSachartre 			/* check if we read backup labels */
1702bae9e67eSachartre 			label = VD_LABEL_VTOC(vd);
1703bae9e67eSachartre 			ablk += (label->dkl_acyl - 1) * csize +
1704bae9e67eSachartre 			    (label->dkl_nhead - 1) * label->dkl_nsect;
1705bae9e67eSachartre 
1706bae9e67eSachartre 			for (sec = 1; (sec < 5 * 2 + 1); sec += 2) {
1707bae9e67eSachartre 
1708bae9e67eSachartre 				if (ablk + sec >= blk &&
1709bae9e67eSachartre 				    ablk + sec < blk + (length / DEV_BSIZE)) {
1710bae9e67eSachartre 					bcopy(label, data +
1711bae9e67eSachartre 					    (ablk + sec - blk) * DEV_BSIZE,
1712bae9e67eSachartre 					    sizeof (struct dk_label));
1713bae9e67eSachartre 				}
1714bae9e67eSachartre 			}
1715bae9e67eSachartre 		}
1716bae9e67eSachartre 	}
1717bae9e67eSachartre 
1718bae9e67eSachartre 	length -= alen;
1719bae9e67eSachartre 
1720bae9e67eSachartre 	status = (length == 0)? 0: EAGAIN;
1721bae9e67eSachartre 
1722bae9e67eSachartre done:
1723bae9e67eSachartre 	ASSERT(length == 0 || blk >= vd->flabel_limit);
1724bae9e67eSachartre 
1725bae9e67eSachartre 	/*
1726bae9e67eSachartre 	 * Return the parameters for the remaining I/O. The starting block is
1727bae9e67eSachartre 	 * adjusted so that it is relative to the vdisk backend.
1728bae9e67eSachartre 	 */
1729bae9e67eSachartre 	*datap = data;
1730bae9e67eSachartre 	*blkp = blk - vd->flabel_limit;
1731bae9e67eSachartre 	*lengthp = length;
1732bae9e67eSachartre 
1733bae9e67eSachartre 	return (status);
1734bae9e67eSachartre }
1735bae9e67eSachartre 
1736bae9e67eSachartre /*
1737205eeb1aSlm66018  * Return Values
1738205eeb1aSlm66018  *	EINPROGRESS	- operation was successfully started
1739205eeb1aSlm66018  *	EIO		- encountered LDC (aka. task error)
1740205eeb1aSlm66018  *	0		- operation completed successfully
1741205eeb1aSlm66018  *
1742205eeb1aSlm66018  * Side Effect
1743205eeb1aSlm66018  *     sets request->status = <disk operation status>
1744205eeb1aSlm66018  */
17451ae08745Sheppo static int
1746d10e4ef2Snarayan vd_start_bio(vd_task_t *task)
17471ae08745Sheppo {
17484bac2208Snarayan 	int			rv, status = 0;
1749d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
1750d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
1751d10e4ef2Snarayan 	struct buf		*buf		= &task->buf;
17524bac2208Snarayan 	uint8_t			mtype;
17533c96341aSnarayan 	int 			slice;
1754047ba61eSachartre 	char			*bufaddr = 0;
1755047ba61eSachartre 	size_t			buflen;
1756bae9e67eSachartre 	size_t			offset, length, nbytes;
1757d10e4ef2Snarayan 
1758d10e4ef2Snarayan 	ASSERT(vd != NULL);
1759d10e4ef2Snarayan 	ASSERT(request != NULL);
17603c96341aSnarayan 
17613c96341aSnarayan 	slice = request->slice;
17623c96341aSnarayan 
176387a7269eSachartre 	ASSERT(slice == VD_SLICE_NONE || slice < vd->nslices);
1764d10e4ef2Snarayan 	ASSERT((request->operation == VD_OP_BREAD) ||
1765d10e4ef2Snarayan 	    (request->operation == VD_OP_BWRITE));
1766d10e4ef2Snarayan 
1767205eeb1aSlm66018 	if (request->nbytes == 0) {
1768205eeb1aSlm66018 		/* no service for trivial requests */
1769205eeb1aSlm66018 		request->status = EINVAL;
1770205eeb1aSlm66018 		return (0);
1771205eeb1aSlm66018 	}
17721ae08745Sheppo 
1773d10e4ef2Snarayan 	PR1("%s %lu bytes at block %lu",
1774d10e4ef2Snarayan 	    (request->operation == VD_OP_BREAD) ? "Read" : "Write",
1775d10e4ef2Snarayan 	    request->nbytes, request->addr);
17761ae08745Sheppo 
1777047ba61eSachartre 	/*
1778047ba61eSachartre 	 * We have to check the open flags because the functions processing
1779047ba61eSachartre 	 * the read/write request will not do it.
1780047ba61eSachartre 	 */
1781047ba61eSachartre 	if (request->operation == VD_OP_BWRITE && !(vd->open_flags & FWRITE)) {
1782047ba61eSachartre 		PR0("write fails because backend is opened read-only");
1783047ba61eSachartre 		request->nbytes = 0;
1784047ba61eSachartre 		request->status = EROFS;
1785047ba61eSachartre 		return (0);
1786047ba61eSachartre 	}
1787d10e4ef2Snarayan 
17884bac2208Snarayan 	mtype = (&vd->inband_task == task) ? LDC_SHADOW_MAP : LDC_DIRECT_MAP;
17894bac2208Snarayan 
17904bac2208Snarayan 	/* Map memory exported by client */
17914bac2208Snarayan 	status = ldc_mem_map(task->mhdl, request->cookie, request->ncookies,
17924bac2208Snarayan 	    mtype, (request->operation == VD_OP_BREAD) ? LDC_MEM_W : LDC_MEM_R,
1793047ba61eSachartre 	    &bufaddr, NULL);
17944bac2208Snarayan 	if (status != 0) {
17953af08d82Slm66018 		PR0("ldc_mem_map() returned err %d ", status);
1796205eeb1aSlm66018 		return (EIO);
1797d10e4ef2Snarayan 	}
1798d10e4ef2Snarayan 
1799bae9e67eSachartre 	/*
1800bae9e67eSachartre 	 * The buffer size has to be 8-byte aligned, so the client should have
1801bae9e67eSachartre 	 * sent a buffer which size is roundup to the next 8-byte aligned value.
1802bae9e67eSachartre 	 */
1803bae9e67eSachartre 	buflen = P2ROUNDUP(request->nbytes, 8);
1804047ba61eSachartre 
1805047ba61eSachartre 	status = ldc_mem_acquire(task->mhdl, 0, buflen);
18064bac2208Snarayan 	if (status != 0) {
18074bac2208Snarayan 		(void) ldc_mem_unmap(task->mhdl);
18083af08d82Slm66018 		PR0("ldc_mem_acquire() returned err %d ", status);
1809205eeb1aSlm66018 		return (EIO);
18104bac2208Snarayan 	}
18114bac2208Snarayan 
1812bae9e67eSachartre 	offset = request->addr;
1813bae9e67eSachartre 	nbytes = request->nbytes;
1814bae9e67eSachartre 	length = nbytes;
1815bae9e67eSachartre 
1816bae9e67eSachartre 	/* default number of byte returned by the I/O */
1817bae9e67eSachartre 	request->nbytes = 0;
1818bae9e67eSachartre 
1819bae9e67eSachartre 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
1820bae9e67eSachartre 
1821bae9e67eSachartre 		if (slice != 0) {
1822bae9e67eSachartre 			/* handle any fake I/O */
1823bae9e67eSachartre 			rv = vd_slice_fake_rdwr(vd, slice, request->operation,
1824bae9e67eSachartre 			    &bufaddr, &offset, &length);
1825bae9e67eSachartre 
1826bae9e67eSachartre 			/* record the number of bytes from the fake I/O */
1827bae9e67eSachartre 			request->nbytes = nbytes - length;
1828bae9e67eSachartre 
1829bae9e67eSachartre 			if (rv == 0) {
1830bae9e67eSachartre 				request->status = 0;
1831bae9e67eSachartre 				goto io_done;
1832bae9e67eSachartre 			}
1833bae9e67eSachartre 
1834bae9e67eSachartre 			if (rv != EAGAIN) {
18353c96341aSnarayan 				request->nbytes = 0;
1836205eeb1aSlm66018 				request->status = EIO;
1837bae9e67eSachartre 				goto io_done;
18383c96341aSnarayan 			}
1839bae9e67eSachartre 
1840bae9e67eSachartre 			/*
1841bae9e67eSachartre 			 * If we return with EAGAIN then this means that there
1842bae9e67eSachartre 			 * are still data to read or write.
1843bae9e67eSachartre 			 */
1844bae9e67eSachartre 			ASSERT(length != 0);
1845bae9e67eSachartre 
1846bae9e67eSachartre 			/*
1847bae9e67eSachartre 			 * We need to continue the I/O from the slice backend to
1848bae9e67eSachartre 			 * complete the request. The variables bufaddr, offset
1849bae9e67eSachartre 			 * and length have been adjusted to have the right
1850bae9e67eSachartre 			 * information to do the remaining I/O from the backend.
1851bae9e67eSachartre 			 * The backend is entirely mapped to slice 0 so we just
1852bae9e67eSachartre 			 * have to complete the I/O from that slice.
1853bae9e67eSachartre 			 */
1854bae9e67eSachartre 			slice = 0;
1855bae9e67eSachartre 		}
1856bae9e67eSachartre 
1857bae9e67eSachartre 	} else if ((slice == VD_SLICE_NONE) && (!vd->file)) {
1858bae9e67eSachartre 
185987a7269eSachartre 		/*
186087a7269eSachartre 		 * This is not a disk image so it is a real disk. We
186187a7269eSachartre 		 * assume that the underlying device driver supports
186287a7269eSachartre 		 * USCSICMD ioctls. This is the case of all SCSI devices
186387a7269eSachartre 		 * (sd, ssd...).
186487a7269eSachartre 		 *
186587a7269eSachartre 		 * In the future if we have non-SCSI disks we would need
186687a7269eSachartre 		 * to invoke the appropriate function to do I/O using an
186717cadca8Slm66018 		 * absolute disk offset (for example using DIOCTL_RWCMD
186887a7269eSachartre 		 * for IDE disks).
186987a7269eSachartre 		 */
1870bae9e67eSachartre 		rv = vd_scsi_rdwr(vd, request->operation, bufaddr, offset,
1871bae9e67eSachartre 		    length);
187287a7269eSachartre 		if (rv != 0) {
1873bae9e67eSachartre 			request->status = EIO;
1874bae9e67eSachartre 		} else {
1875bae9e67eSachartre 			request->nbytes = length;
1876bae9e67eSachartre 			request->status = 0;
1877bae9e67eSachartre 		}
1878bae9e67eSachartre 		goto io_done;
1879bae9e67eSachartre 	}
1880bae9e67eSachartre 
1881bae9e67eSachartre 	/* Start the block I/O */
1882bae9e67eSachartre 	if (vd->file) {
1883bae9e67eSachartre 		rv = vd_file_rw(vd, slice, request->operation, bufaddr, offset,
1884bae9e67eSachartre 		    length);
1885bae9e67eSachartre 		if (rv < 0) {
188687a7269eSachartre 			request->nbytes = 0;
1887205eeb1aSlm66018 			request->status = EIO;
188887a7269eSachartre 		} else {
1889bae9e67eSachartre 			request->nbytes += rv;
1890205eeb1aSlm66018 			request->status = 0;
189187a7269eSachartre 		}
189287a7269eSachartre 	} else {
1893047ba61eSachartre 		bioinit(buf);
1894047ba61eSachartre 		buf->b_flags	= B_BUSY;
1895bae9e67eSachartre 		buf->b_bcount	= length;
1896bae9e67eSachartre 		buf->b_lblkno	= offset;
1897bae9e67eSachartre 		buf->b_bufsize	= buflen;
1898047ba61eSachartre 		buf->b_edev 	= vd->dev[slice];
1899047ba61eSachartre 		buf->b_un.b_addr = bufaddr;
1900047ba61eSachartre 		buf->b_flags 	|= (request->operation == VD_OP_BREAD)?
1901047ba61eSachartre 		    B_READ : B_WRITE;
1902047ba61eSachartre 
1903bae9e67eSachartre 		request->status = ldi_strategy(vd->ldi_handle[slice], buf);
1904205eeb1aSlm66018 
1905205eeb1aSlm66018 		/*
1906205eeb1aSlm66018 		 * This is to indicate to the caller that the request
1907205eeb1aSlm66018 		 * needs to be finished by vd_complete_bio() by calling
1908205eeb1aSlm66018 		 * biowait() there and waiting for that to return before
1909205eeb1aSlm66018 		 * triggering the notification of the vDisk client.
1910205eeb1aSlm66018 		 *
1911205eeb1aSlm66018 		 * This is necessary when writing to real disks as
1912205eeb1aSlm66018 		 * otherwise calls to ldi_strategy() would be serialized
1913205eeb1aSlm66018 		 * behind the calls to biowait() and performance would
1914205eeb1aSlm66018 		 * suffer.
1915205eeb1aSlm66018 		 */
1916205eeb1aSlm66018 		if (request->status == 0)
191787a7269eSachartre 			return (EINPROGRESS);
1918047ba61eSachartre 
1919047ba61eSachartre 		biofini(buf);
192087a7269eSachartre 	}
19213c96341aSnarayan 
1922bae9e67eSachartre io_done:
1923bae9e67eSachartre 	/* Clean up after error or completion */
1924047ba61eSachartre 	rv = ldc_mem_release(task->mhdl, 0, buflen);
19254bac2208Snarayan 	if (rv) {
19263af08d82Slm66018 		PR0("ldc_mem_release() returned err %d ", rv);
1927205eeb1aSlm66018 		status = EIO;
19284bac2208Snarayan 	}
19294bac2208Snarayan 	rv = ldc_mem_unmap(task->mhdl);
19304bac2208Snarayan 	if (rv) {
1931205eeb1aSlm66018 		PR0("ldc_mem_unmap() returned err %d ", rv);
1932205eeb1aSlm66018 		status = EIO;
19334bac2208Snarayan 	}
19344bac2208Snarayan 
1935d10e4ef2Snarayan 	return (status);
1936d10e4ef2Snarayan }
1937d10e4ef2Snarayan 
1938205eeb1aSlm66018 /*
1939205eeb1aSlm66018  * This function should only be called from vd_notify to ensure that requests
1940205eeb1aSlm66018  * are responded to in the order that they are received.
1941205eeb1aSlm66018  */
1942d10e4ef2Snarayan static int
1943d10e4ef2Snarayan send_msg(ldc_handle_t ldc_handle, void *msg, size_t msglen)
1944d10e4ef2Snarayan {
19453af08d82Slm66018 	int	status;
1946d10e4ef2Snarayan 	size_t	nbytes;
1947d10e4ef2Snarayan 
19483af08d82Slm66018 	do {
1949d10e4ef2Snarayan 		nbytes = msglen;
1950d10e4ef2Snarayan 		status = ldc_write(ldc_handle, msg, &nbytes);
19513af08d82Slm66018 		if (status != EWOULDBLOCK)
19523af08d82Slm66018 			break;
19533af08d82Slm66018 		drv_usecwait(vds_ldc_delay);
19543af08d82Slm66018 	} while (status == EWOULDBLOCK);
1955d10e4ef2Snarayan 
1956d10e4ef2Snarayan 	if (status != 0) {
19573af08d82Slm66018 		if (status != ECONNRESET)
19583af08d82Slm66018 			PR0("ldc_write() returned errno %d", status);
1959d10e4ef2Snarayan 		return (status);
1960d10e4ef2Snarayan 	} else if (nbytes != msglen) {
19613af08d82Slm66018 		PR0("ldc_write() performed only partial write");
1962d10e4ef2Snarayan 		return (EIO);
1963d10e4ef2Snarayan 	}
1964d10e4ef2Snarayan 
1965d10e4ef2Snarayan 	PR1("SENT %lu bytes", msglen);
1966d10e4ef2Snarayan 	return (0);
1967d10e4ef2Snarayan }
1968d10e4ef2Snarayan 
1969d10e4ef2Snarayan static void
1970d10e4ef2Snarayan vd_need_reset(vd_t *vd, boolean_t reset_ldc)
1971d10e4ef2Snarayan {
1972d10e4ef2Snarayan 	mutex_enter(&vd->lock);
1973d10e4ef2Snarayan 	vd->reset_state	= B_TRUE;
1974d10e4ef2Snarayan 	vd->reset_ldc	= reset_ldc;
1975d10e4ef2Snarayan 	mutex_exit(&vd->lock);
1976d10e4ef2Snarayan }
1977d10e4ef2Snarayan 
1978d10e4ef2Snarayan /*
1979d10e4ef2Snarayan  * Reset the state of the connection with a client, if needed; reset the LDC
1980d10e4ef2Snarayan  * transport as well, if needed.  This function should only be called from the
19813af08d82Slm66018  * "vd_recv_msg", as it waits for tasks - otherwise a deadlock can occur.
1982d10e4ef2Snarayan  */
1983d10e4ef2Snarayan static void
1984d10e4ef2Snarayan vd_reset_if_needed(vd_t *vd)
1985d10e4ef2Snarayan {
1986d10e4ef2Snarayan 	int	status = 0;
1987d10e4ef2Snarayan 
1988d10e4ef2Snarayan 	mutex_enter(&vd->lock);
1989d10e4ef2Snarayan 	if (!vd->reset_state) {
1990d10e4ef2Snarayan 		ASSERT(!vd->reset_ldc);
1991d10e4ef2Snarayan 		mutex_exit(&vd->lock);
1992d10e4ef2Snarayan 		return;
1993d10e4ef2Snarayan 	}
1994d10e4ef2Snarayan 	mutex_exit(&vd->lock);
1995d10e4ef2Snarayan 
1996d10e4ef2Snarayan 	PR0("Resetting connection state with %s", VD_CLIENT(vd));
1997d10e4ef2Snarayan 
1998d10e4ef2Snarayan 	/*
1999d10e4ef2Snarayan 	 * Let any asynchronous I/O complete before possibly pulling the rug
2000d10e4ef2Snarayan 	 * out from under it; defer checking vd->reset_ldc, as one of the
2001d10e4ef2Snarayan 	 * asynchronous tasks might set it
2002d10e4ef2Snarayan 	 */
2003d10e4ef2Snarayan 	ddi_taskq_wait(vd->completionq);
2004d10e4ef2Snarayan 
20053c96341aSnarayan 	if (vd->file) {
2006da6c28aaSamw 		status = VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL);
20073c96341aSnarayan 		if (status) {
20083c96341aSnarayan 			PR0("VOP_FSYNC returned errno %d", status);
20093c96341aSnarayan 		}
20103c96341aSnarayan 	}
20113c96341aSnarayan 
2012d10e4ef2Snarayan 	if ((vd->initialized & VD_DRING) &&
2013d10e4ef2Snarayan 	    ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0))
20143af08d82Slm66018 		PR0("ldc_mem_dring_unmap() returned errno %d", status);
2015d10e4ef2Snarayan 
20163af08d82Slm66018 	vd_free_dring_task(vd);
20173af08d82Slm66018 
20183af08d82Slm66018 	/* Free the staging buffer for msgs */
20193af08d82Slm66018 	if (vd->vio_msgp != NULL) {
20203af08d82Slm66018 		kmem_free(vd->vio_msgp, vd->max_msglen);
20213af08d82Slm66018 		vd->vio_msgp = NULL;
2022d10e4ef2Snarayan 	}
2023d10e4ef2Snarayan 
20243af08d82Slm66018 	/* Free the inband message buffer */
20253af08d82Slm66018 	if (vd->inband_task.msg != NULL) {
20263af08d82Slm66018 		kmem_free(vd->inband_task.msg, vd->max_msglen);
20273af08d82Slm66018 		vd->inband_task.msg = NULL;
20283af08d82Slm66018 	}
2029d10e4ef2Snarayan 
2030d10e4ef2Snarayan 	mutex_enter(&vd->lock);
20313af08d82Slm66018 
20323af08d82Slm66018 	if (vd->reset_ldc)
20333af08d82Slm66018 		PR0("taking down LDC channel");
2034e1ebb9ecSlm66018 	if (vd->reset_ldc && ((status = ldc_down(vd->ldc_handle)) != 0))
20353af08d82Slm66018 		PR0("ldc_down() returned errno %d", status);
2036d10e4ef2Snarayan 
20372f5224aeSachartre 	/* Reset exclusive access rights */
20382f5224aeSachartre 	vd_reset_access(vd);
20392f5224aeSachartre 
2040d10e4ef2Snarayan 	vd->initialized	&= ~(VD_SID | VD_SEQ_NUM | VD_DRING);
2041d10e4ef2Snarayan 	vd->state	= VD_STATE_INIT;
2042d10e4ef2Snarayan 	vd->max_msglen	= sizeof (vio_msg_t);	/* baseline vio message size */
2043d10e4ef2Snarayan 
20443af08d82Slm66018 	/* Allocate the staging buffer */
20453af08d82Slm66018 	vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
20463af08d82Slm66018 
20473af08d82Slm66018 	PR0("calling ldc_up\n");
20483af08d82Slm66018 	(void) ldc_up(vd->ldc_handle);
20493af08d82Slm66018 
2050d10e4ef2Snarayan 	vd->reset_state	= B_FALSE;
2051d10e4ef2Snarayan 	vd->reset_ldc	= B_FALSE;
20523af08d82Slm66018 
2053d10e4ef2Snarayan 	mutex_exit(&vd->lock);
2054d10e4ef2Snarayan }
2055d10e4ef2Snarayan 
20563af08d82Slm66018 static void vd_recv_msg(void *arg);
20573af08d82Slm66018 
20583af08d82Slm66018 static void
20593af08d82Slm66018 vd_mark_in_reset(vd_t *vd)
20603af08d82Slm66018 {
20613af08d82Slm66018 	int status;
20623af08d82Slm66018 
20633af08d82Slm66018 	PR0("vd_mark_in_reset: marking vd in reset\n");
20643af08d82Slm66018 
20653af08d82Slm66018 	vd_need_reset(vd, B_FALSE);
20663af08d82Slm66018 	status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, DDI_SLEEP);
20673af08d82Slm66018 	if (status == DDI_FAILURE) {
20683af08d82Slm66018 		PR0("cannot schedule task to recv msg\n");
20693af08d82Slm66018 		vd_need_reset(vd, B_TRUE);
20703af08d82Slm66018 		return;
20713af08d82Slm66018 	}
20723af08d82Slm66018 }
20733af08d82Slm66018 
2074d10e4ef2Snarayan static int
20753c96341aSnarayan vd_mark_elem_done(vd_t *vd, int idx, int elem_status, int elem_nbytes)
2076d10e4ef2Snarayan {
2077d10e4ef2Snarayan 	boolean_t		accepted;
2078d10e4ef2Snarayan 	int			status;
2079bbfa0259Sha137994 	on_trap_data_t		otd;
2080d10e4ef2Snarayan 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
2081d10e4ef2Snarayan 
20823af08d82Slm66018 	if (vd->reset_state)
20833af08d82Slm66018 		return (0);
2084d10e4ef2Snarayan 
2085d10e4ef2Snarayan 	/* Acquire the element */
2086bbfa0259Sha137994 	if ((status = VIO_DRING_ACQUIRE(&otd, vd->dring_mtype,
2087bbfa0259Sha137994 	    vd->dring_handle, idx, idx)) != 0) {
20883af08d82Slm66018 		if (status == ECONNRESET) {
20893af08d82Slm66018 			vd_mark_in_reset(vd);
20903af08d82Slm66018 			return (0);
20913af08d82Slm66018 		} else {
2092d10e4ef2Snarayan 			return (status);
2093d10e4ef2Snarayan 		}
20943af08d82Slm66018 	}
2095d10e4ef2Snarayan 
2096d10e4ef2Snarayan 	/* Set the element's status and mark it done */
2097d10e4ef2Snarayan 	accepted = (elem->hdr.dstate == VIO_DESC_ACCEPTED);
2098d10e4ef2Snarayan 	if (accepted) {
20993c96341aSnarayan 		elem->payload.nbytes	= elem_nbytes;
2100d10e4ef2Snarayan 		elem->payload.status	= elem_status;
2101d10e4ef2Snarayan 		elem->hdr.dstate	= VIO_DESC_DONE;
2102d10e4ef2Snarayan 	} else {
2103d10e4ef2Snarayan 		/* Perhaps client timed out waiting for I/O... */
21043af08d82Slm66018 		PR0("element %u no longer \"accepted\"", idx);
2105d10e4ef2Snarayan 		VD_DUMP_DRING_ELEM(elem);
2106d10e4ef2Snarayan 	}
2107d10e4ef2Snarayan 	/* Release the element */
2108bbfa0259Sha137994 	if ((status = VIO_DRING_RELEASE(vd->dring_mtype,
2109bbfa0259Sha137994 	    vd->dring_handle, idx, idx)) != 0) {
21103af08d82Slm66018 		if (status == ECONNRESET) {
21113af08d82Slm66018 			vd_mark_in_reset(vd);
21123af08d82Slm66018 			return (0);
21133af08d82Slm66018 		} else {
2114bbfa0259Sha137994 			PR0("VIO_DRING_RELEASE() returned errno %d",
21153af08d82Slm66018 			    status);
2116d10e4ef2Snarayan 			return (status);
2117d10e4ef2Snarayan 		}
21183af08d82Slm66018 	}
2119d10e4ef2Snarayan 
2120d10e4ef2Snarayan 	return (accepted ? 0 : EINVAL);
2121d10e4ef2Snarayan }
2122d10e4ef2Snarayan 
2123205eeb1aSlm66018 /*
2124205eeb1aSlm66018  * Return Values
2125205eeb1aSlm66018  *	0	- operation completed successfully
2126205eeb1aSlm66018  *	EIO	- encountered LDC / task error
2127205eeb1aSlm66018  *
2128205eeb1aSlm66018  * Side Effect
2129205eeb1aSlm66018  *	sets request->status = <disk operation status>
2130205eeb1aSlm66018  */
2131205eeb1aSlm66018 static int
2132205eeb1aSlm66018 vd_complete_bio(vd_task_t *task)
2133d10e4ef2Snarayan {
2134d10e4ef2Snarayan 	int			status		= 0;
2135205eeb1aSlm66018 	int			rv		= 0;
2136d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
2137d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
2138d10e4ef2Snarayan 	struct buf		*buf		= &task->buf;
2139d10e4ef2Snarayan 
2140d10e4ef2Snarayan 
2141d10e4ef2Snarayan 	ASSERT(vd != NULL);
2142d10e4ef2Snarayan 	ASSERT(request != NULL);
2143d10e4ef2Snarayan 	ASSERT(task->msg != NULL);
2144d10e4ef2Snarayan 	ASSERT(task->msglen >= sizeof (*task->msg));
21453c96341aSnarayan 	ASSERT(!vd->file);
2146bae9e67eSachartre 	ASSERT(request->slice != VD_SLICE_NONE || (!vd_slice_single_slice &&
2147bae9e67eSachartre 	    vd->vdisk_type == VD_DISK_TYPE_SLICE));
2148d10e4ef2Snarayan 
2149205eeb1aSlm66018 	/* Wait for the I/O to complete [ call to ldi_strategy(9f) ] */
2150d10e4ef2Snarayan 	request->status = biowait(buf);
2151d10e4ef2Snarayan 
2152bae9e67eSachartre 	/* Update the number of bytes read/written */
2153bae9e67eSachartre 	request->nbytes += buf->b_bcount - buf->b_resid;
21543c96341aSnarayan 
21554bac2208Snarayan 	/* Release the buffer */
21563af08d82Slm66018 	if (!vd->reset_state)
2157bae9e67eSachartre 		status = ldc_mem_release(task->mhdl, 0, buf->b_bufsize);
21584bac2208Snarayan 	if (status) {
21593af08d82Slm66018 		PR0("ldc_mem_release() returned errno %d copying to "
21603af08d82Slm66018 		    "client", status);
21613af08d82Slm66018 		if (status == ECONNRESET) {
21623af08d82Slm66018 			vd_mark_in_reset(vd);
21633af08d82Slm66018 		}
2164205eeb1aSlm66018 		rv = EIO;
21651ae08745Sheppo 	}
21661ae08745Sheppo 
21673af08d82Slm66018 	/* Unmap the memory, even if in reset */
21684bac2208Snarayan 	status = ldc_mem_unmap(task->mhdl);
21694bac2208Snarayan 	if (status) {
21703af08d82Slm66018 		PR0("ldc_mem_unmap() returned errno %d copying to client",
21714bac2208Snarayan 		    status);
21723af08d82Slm66018 		if (status == ECONNRESET) {
21733af08d82Slm66018 			vd_mark_in_reset(vd);
21743af08d82Slm66018 		}
2175205eeb1aSlm66018 		rv = EIO;
21764bac2208Snarayan 	}
21774bac2208Snarayan 
2178d10e4ef2Snarayan 	biofini(buf);
21791ae08745Sheppo 
2180205eeb1aSlm66018 	return (rv);
2181205eeb1aSlm66018 }
2182205eeb1aSlm66018 
2183205eeb1aSlm66018 /*
2184205eeb1aSlm66018  * Description:
2185205eeb1aSlm66018  *	This function is called by the two functions called by a taskq
2186205eeb1aSlm66018  *	[ vd_complete_notify() and vd_serial_notify()) ] to send the
2187205eeb1aSlm66018  *	message to the client.
2188205eeb1aSlm66018  *
2189205eeb1aSlm66018  * Parameters:
2190205eeb1aSlm66018  *	arg 	- opaque pointer to structure containing task to be completed
2191205eeb1aSlm66018  *
2192205eeb1aSlm66018  * Return Values
2193205eeb1aSlm66018  *	None
2194205eeb1aSlm66018  */
2195205eeb1aSlm66018 static void
2196205eeb1aSlm66018 vd_notify(vd_task_t *task)
2197205eeb1aSlm66018 {
2198205eeb1aSlm66018 	int	status;
2199205eeb1aSlm66018 
2200205eeb1aSlm66018 	ASSERT(task != NULL);
2201205eeb1aSlm66018 	ASSERT(task->vd != NULL);
2202205eeb1aSlm66018 
2203205eeb1aSlm66018 	if (task->vd->reset_state)
2204205eeb1aSlm66018 		return;
2205205eeb1aSlm66018 
2206205eeb1aSlm66018 	/*
2207205eeb1aSlm66018 	 * Send the "ack" or "nack" back to the client; if sending the message
2208205eeb1aSlm66018 	 * via LDC fails, arrange to reset both the connection state and LDC
2209205eeb1aSlm66018 	 * itself
2210205eeb1aSlm66018 	 */
2211205eeb1aSlm66018 	PR2("Sending %s",
2212205eeb1aSlm66018 	    (task->msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK");
2213205eeb1aSlm66018 
2214205eeb1aSlm66018 	status = send_msg(task->vd->ldc_handle, task->msg, task->msglen);
2215205eeb1aSlm66018 	switch (status) {
2216205eeb1aSlm66018 	case 0:
2217205eeb1aSlm66018 		break;
2218205eeb1aSlm66018 	case ECONNRESET:
2219205eeb1aSlm66018 		vd_mark_in_reset(task->vd);
2220205eeb1aSlm66018 		break;
2221205eeb1aSlm66018 	default:
2222205eeb1aSlm66018 		PR0("initiating full reset");
2223205eeb1aSlm66018 		vd_need_reset(task->vd, B_TRUE);
2224205eeb1aSlm66018 		break;
2225205eeb1aSlm66018 	}
2226205eeb1aSlm66018 
2227205eeb1aSlm66018 	DTRACE_PROBE1(task__end, vd_task_t *, task);
2228205eeb1aSlm66018 }
2229205eeb1aSlm66018 
2230205eeb1aSlm66018 /*
2231205eeb1aSlm66018  * Description:
2232205eeb1aSlm66018  *	Mark the Dring entry as Done and (if necessary) send an ACK/NACK to
2233205eeb1aSlm66018  *	the vDisk client
2234205eeb1aSlm66018  *
2235205eeb1aSlm66018  * Parameters:
2236205eeb1aSlm66018  *	task 		- structure containing the request sent from client
2237205eeb1aSlm66018  *
2238205eeb1aSlm66018  * Return Values
2239205eeb1aSlm66018  *	None
2240205eeb1aSlm66018  */
2241205eeb1aSlm66018 static void
2242205eeb1aSlm66018 vd_complete_notify(vd_task_t *task)
2243205eeb1aSlm66018 {
2244205eeb1aSlm66018 	int			status		= 0;
2245205eeb1aSlm66018 	vd_t			*vd		= task->vd;
2246205eeb1aSlm66018 	vd_dring_payload_t	*request	= task->request;
2247205eeb1aSlm66018 
2248d10e4ef2Snarayan 	/* Update the dring element for a dring client */
2249f0ca1d9aSsb155480 	if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE_V1_0)) {
22503c96341aSnarayan 		status = vd_mark_elem_done(vd, task->index,
22513c96341aSnarayan 		    request->status, request->nbytes);
22523af08d82Slm66018 		if (status == ECONNRESET)
22533af08d82Slm66018 			vd_mark_in_reset(vd);
2254bbfa0259Sha137994 		else if (status == EACCES)
2255bbfa0259Sha137994 			vd_need_reset(vd, B_TRUE);
22563af08d82Slm66018 	}
22571ae08745Sheppo 
2258d10e4ef2Snarayan 	/*
2259205eeb1aSlm66018 	 * If a transport error occurred while marking the element done or
2260205eeb1aSlm66018 	 * previously while executing the task, arrange to "nack" the message
2261205eeb1aSlm66018 	 * when the final task in the descriptor element range completes
2262d10e4ef2Snarayan 	 */
2263205eeb1aSlm66018 	if ((status != 0) || (task->status != 0))
2264d10e4ef2Snarayan 		task->msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
22651ae08745Sheppo 
2266d10e4ef2Snarayan 	/*
2267d10e4ef2Snarayan 	 * Only the final task for a range of elements will respond to and
2268d10e4ef2Snarayan 	 * free the message
2269d10e4ef2Snarayan 	 */
22703af08d82Slm66018 	if (task->type == VD_NONFINAL_RANGE_TASK) {
2271d10e4ef2Snarayan 		return;
22723af08d82Slm66018 	}
22731ae08745Sheppo 
2274205eeb1aSlm66018 	vd_notify(task);
2275205eeb1aSlm66018 }
2276205eeb1aSlm66018 
2277d10e4ef2Snarayan /*
2278205eeb1aSlm66018  * Description:
2279205eeb1aSlm66018  *	This is the basic completion function called to handle inband data
2280205eeb1aSlm66018  *	requests and handshake messages. All it needs to do is trigger a
2281205eeb1aSlm66018  *	message to the client that the request is completed.
2282205eeb1aSlm66018  *
2283205eeb1aSlm66018  * Parameters:
2284205eeb1aSlm66018  *	arg 	- opaque pointer to structure containing task to be completed
2285205eeb1aSlm66018  *
2286205eeb1aSlm66018  * Return Values
2287205eeb1aSlm66018  *	None
2288d10e4ef2Snarayan  */
2289205eeb1aSlm66018 static void
2290205eeb1aSlm66018 vd_serial_notify(void *arg)
2291205eeb1aSlm66018 {
2292205eeb1aSlm66018 	vd_task_t		*task = (vd_task_t *)arg;
2293205eeb1aSlm66018 
2294205eeb1aSlm66018 	ASSERT(task != NULL);
2295205eeb1aSlm66018 	vd_notify(task);
22961ae08745Sheppo }
22971ae08745Sheppo 
22982f5224aeSachartre /* ARGSUSED */
22992f5224aeSachartre static int
23002f5224aeSachartre vd_geom2dk_geom(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
23010a55fbb7Slm66018 {
23020a55fbb7Slm66018 	VD_GEOM2DK_GEOM((vd_geom_t *)vd_buf, (struct dk_geom *)ioctl_arg);
23032f5224aeSachartre 	return (0);
23040a55fbb7Slm66018 }
23050a55fbb7Slm66018 
23062f5224aeSachartre /* ARGSUSED */
23072f5224aeSachartre static int
23082f5224aeSachartre vd_vtoc2vtoc(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
23090a55fbb7Slm66018 {
23100a55fbb7Slm66018 	VD_VTOC2VTOC((vd_vtoc_t *)vd_buf, (struct vtoc *)ioctl_arg);
23112f5224aeSachartre 	return (0);
23120a55fbb7Slm66018 }
23130a55fbb7Slm66018 
23140a55fbb7Slm66018 static void
23150a55fbb7Slm66018 dk_geom2vd_geom(void *ioctl_arg, void *vd_buf)
23160a55fbb7Slm66018 {
23170a55fbb7Slm66018 	DK_GEOM2VD_GEOM((struct dk_geom *)ioctl_arg, (vd_geom_t *)vd_buf);
23180a55fbb7Slm66018 }
23190a55fbb7Slm66018 
23200a55fbb7Slm66018 static void
23210a55fbb7Slm66018 vtoc2vd_vtoc(void *ioctl_arg, void *vd_buf)
23220a55fbb7Slm66018 {
23230a55fbb7Slm66018 	VTOC2VD_VTOC((struct vtoc *)ioctl_arg, (vd_vtoc_t *)vd_buf);
23240a55fbb7Slm66018 }
23250a55fbb7Slm66018 
23262f5224aeSachartre static int
23272f5224aeSachartre vd_get_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
23284bac2208Snarayan {
23294bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
23304bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
23312f5224aeSachartre 	size_t data_len;
23322f5224aeSachartre 
23332f5224aeSachartre 	data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t));
23342f5224aeSachartre 	if (vd_efi->length > data_len)
23352f5224aeSachartre 		return (EINVAL);
23364bac2208Snarayan 
23374bac2208Snarayan 	dk_efi->dki_lba = vd_efi->lba;
23384bac2208Snarayan 	dk_efi->dki_length = vd_efi->length;
23394bac2208Snarayan 	dk_efi->dki_data = kmem_zalloc(vd_efi->length, KM_SLEEP);
23402f5224aeSachartre 	return (0);
23414bac2208Snarayan }
23424bac2208Snarayan 
23434bac2208Snarayan static void
23444bac2208Snarayan vd_get_efi_out(void *ioctl_arg, void *vd_buf)
23454bac2208Snarayan {
23464bac2208Snarayan 	int len;
23474bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
23484bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
23494bac2208Snarayan 
23504bac2208Snarayan 	len = vd_efi->length;
23514bac2208Snarayan 	DK_EFI2VD_EFI(dk_efi, vd_efi);
23524bac2208Snarayan 	kmem_free(dk_efi->dki_data, len);
23534bac2208Snarayan }
23544bac2208Snarayan 
23552f5224aeSachartre static int
23562f5224aeSachartre vd_set_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
23574bac2208Snarayan {
23584bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
23594bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
23602f5224aeSachartre 	size_t data_len;
23612f5224aeSachartre 
23622f5224aeSachartre 	data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t));
23632f5224aeSachartre 	if (vd_efi->length > data_len)
23642f5224aeSachartre 		return (EINVAL);
23654bac2208Snarayan 
23664bac2208Snarayan 	dk_efi->dki_data = kmem_alloc(vd_efi->length, KM_SLEEP);
23674bac2208Snarayan 	VD_EFI2DK_EFI(vd_efi, dk_efi);
23682f5224aeSachartre 	return (0);
23694bac2208Snarayan }
23704bac2208Snarayan 
23714bac2208Snarayan static void
23724bac2208Snarayan vd_set_efi_out(void *ioctl_arg, void *vd_buf)
23734bac2208Snarayan {
23744bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
23754bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
23764bac2208Snarayan 
23774bac2208Snarayan 	kmem_free(dk_efi->dki_data, vd_efi->length);
23784bac2208Snarayan }
23794bac2208Snarayan 
23802f5224aeSachartre static int
23812f5224aeSachartre vd_scsicmd_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
23822f5224aeSachartre {
23832f5224aeSachartre 	size_t vd_scsi_len;
23842f5224aeSachartre 	vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf;
23852f5224aeSachartre 	struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg;
23862f5224aeSachartre 
23872f5224aeSachartre 	/* check buffer size */
23882f5224aeSachartre 	vd_scsi_len = VD_SCSI_SIZE;
23892f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(vd_scsi->cdb_len, sizeof (uint64_t));
23902f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(vd_scsi->sense_len, sizeof (uint64_t));
23912f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(vd_scsi->datain_len, sizeof (uint64_t));
23922f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(vd_scsi->dataout_len, sizeof (uint64_t));
23932f5224aeSachartre 
23942f5224aeSachartre 	ASSERT(vd_scsi_len % sizeof (uint64_t) == 0);
23952f5224aeSachartre 
23962f5224aeSachartre 	if (vd_buf_len < vd_scsi_len)
23972f5224aeSachartre 		return (EINVAL);
23982f5224aeSachartre 
23992f5224aeSachartre 	/* set flags */
24002f5224aeSachartre 	uscsi->uscsi_flags = vd_scsi_debug;
24012f5224aeSachartre 
24022f5224aeSachartre 	if (vd_scsi->options & VD_SCSI_OPT_NORETRY) {
24032f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_ISOLATE;
24042f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_DIAGNOSE;
24052f5224aeSachartre 	}
24062f5224aeSachartre 
24072f5224aeSachartre 	/* task attribute */
24082f5224aeSachartre 	switch (vd_scsi->task_attribute) {
24092f5224aeSachartre 	case VD_SCSI_TASK_ACA:
24102f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_HEAD;
24112f5224aeSachartre 		break;
24122f5224aeSachartre 	case VD_SCSI_TASK_HQUEUE:
24132f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_HTAG;
24142f5224aeSachartre 		break;
24152f5224aeSachartre 	case VD_SCSI_TASK_ORDERED:
24162f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_OTAG;
24172f5224aeSachartre 		break;
24182f5224aeSachartre 	default:
24192f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_NOTAG;
24202f5224aeSachartre 		break;
24212f5224aeSachartre 	}
24222f5224aeSachartre 
24232f5224aeSachartre 	/* timeout */
24242f5224aeSachartre 	uscsi->uscsi_timeout = vd_scsi->timeout;
24252f5224aeSachartre 
24262f5224aeSachartre 	/* cdb data */
24272f5224aeSachartre 	uscsi->uscsi_cdb = (caddr_t)VD_SCSI_DATA_CDB(vd_scsi);
24282f5224aeSachartre 	uscsi->uscsi_cdblen = vd_scsi->cdb_len;
24292f5224aeSachartre 
24302f5224aeSachartre 	/* sense buffer */
24312f5224aeSachartre 	if (vd_scsi->sense_len != 0) {
24322f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_RQENABLE;
24332f5224aeSachartre 		uscsi->uscsi_rqbuf = (caddr_t)VD_SCSI_DATA_SENSE(vd_scsi);
24342f5224aeSachartre 		uscsi->uscsi_rqlen = vd_scsi->sense_len;
24352f5224aeSachartre 	}
24362f5224aeSachartre 
24372f5224aeSachartre 	if (vd_scsi->datain_len != 0 && vd_scsi->dataout_len != 0) {
24382f5224aeSachartre 		/* uscsi does not support read/write request */
24392f5224aeSachartre 		return (EINVAL);
24402f5224aeSachartre 	}
24412f5224aeSachartre 
24422f5224aeSachartre 	/* request data-in */
24432f5224aeSachartre 	if (vd_scsi->datain_len != 0) {
24442f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_READ;
24452f5224aeSachartre 		uscsi->uscsi_buflen = vd_scsi->datain_len;
24462f5224aeSachartre 		uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_IN(vd_scsi);
24472f5224aeSachartre 	}
24482f5224aeSachartre 
24492f5224aeSachartre 	/* request data-out */
24502f5224aeSachartre 	if (vd_scsi->dataout_len != 0) {
24512f5224aeSachartre 		uscsi->uscsi_buflen = vd_scsi->dataout_len;
24522f5224aeSachartre 		uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_OUT(vd_scsi);
24532f5224aeSachartre 	}
24542f5224aeSachartre 
24552f5224aeSachartre 	return (0);
24562f5224aeSachartre }
24572f5224aeSachartre 
24582f5224aeSachartre static void
24592f5224aeSachartre vd_scsicmd_out(void *ioctl_arg, void *vd_buf)
24602f5224aeSachartre {
24612f5224aeSachartre 	vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf;
24622f5224aeSachartre 	struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg;
24632f5224aeSachartre 
24642f5224aeSachartre 	/* output fields */
24652f5224aeSachartre 	vd_scsi->cmd_status = uscsi->uscsi_status;
24662f5224aeSachartre 
24672f5224aeSachartre 	/* sense data */
24682f5224aeSachartre 	if ((uscsi->uscsi_flags & USCSI_RQENABLE) &&
24692f5224aeSachartre 	    (uscsi->uscsi_status == STATUS_CHECK ||
24702f5224aeSachartre 	    uscsi->uscsi_status == STATUS_TERMINATED)) {
24712f5224aeSachartre 		vd_scsi->sense_status = uscsi->uscsi_rqstatus;
24722f5224aeSachartre 		if (uscsi->uscsi_rqstatus == STATUS_GOOD)
2473*14466a20Szk194757 			vd_scsi->sense_len -= uscsi->uscsi_rqresid;
24742f5224aeSachartre 		else
24752f5224aeSachartre 			vd_scsi->sense_len = 0;
24762f5224aeSachartre 	} else {
24772f5224aeSachartre 		vd_scsi->sense_len = 0;
24782f5224aeSachartre 	}
24792f5224aeSachartre 
24802f5224aeSachartre 	if (uscsi->uscsi_status != STATUS_GOOD) {
24812f5224aeSachartre 		vd_scsi->dataout_len = 0;
24822f5224aeSachartre 		vd_scsi->datain_len = 0;
24832f5224aeSachartre 		return;
24842f5224aeSachartre 	}
24852f5224aeSachartre 
24862f5224aeSachartre 	if (uscsi->uscsi_flags & USCSI_READ) {
24872f5224aeSachartre 		/* request data (read) */
24882f5224aeSachartre 		vd_scsi->datain_len -= uscsi->uscsi_resid;
24892f5224aeSachartre 		vd_scsi->dataout_len = 0;
24902f5224aeSachartre 	} else {
24912f5224aeSachartre 		/* request data (write) */
24922f5224aeSachartre 		vd_scsi->datain_len = 0;
24932f5224aeSachartre 		vd_scsi->dataout_len -= uscsi->uscsi_resid;
24942f5224aeSachartre 	}
24952f5224aeSachartre }
24962f5224aeSachartre 
2497690555a1Sachartre static ushort_t
24983c96341aSnarayan vd_lbl2cksum(struct dk_label *label)
24993c96341aSnarayan {
25003c96341aSnarayan 	int	count;
2501690555a1Sachartre 	ushort_t sum, *sp;
25023c96341aSnarayan 
25033c96341aSnarayan 	count =	(sizeof (struct dk_label)) / (sizeof (short)) - 1;
2504690555a1Sachartre 	sp = (ushort_t *)label;
25053c96341aSnarayan 	sum = 0;
25063c96341aSnarayan 	while (count--) {
25073c96341aSnarayan 		sum ^= *sp++;
25083c96341aSnarayan 	}
25093c96341aSnarayan 
25103c96341aSnarayan 	return (sum);
25113c96341aSnarayan }
25123c96341aSnarayan 
251387a7269eSachartre /*
2514bae9e67eSachartre  * Copy information from a vtoc and dk_geom structures to a dk_label structure.
2515bae9e67eSachartre  */
2516bae9e67eSachartre static void
2517bae9e67eSachartre vd_vtocgeom_to_label(struct vtoc *vtoc, struct dk_geom *geom,
2518bae9e67eSachartre     struct dk_label *label)
2519bae9e67eSachartre {
2520bae9e67eSachartre 	int i;
2521bae9e67eSachartre 
2522bae9e67eSachartre 	ASSERT(vtoc->v_nparts == V_NUMPAR);
2523bae9e67eSachartre 	ASSERT(vtoc->v_sanity == VTOC_SANE);
2524bae9e67eSachartre 
2525bae9e67eSachartre 	bzero(label, sizeof (struct dk_label));
2526bae9e67eSachartre 
2527bae9e67eSachartre 	label->dkl_ncyl = geom->dkg_ncyl;
2528bae9e67eSachartre 	label->dkl_acyl = geom->dkg_acyl;
2529bae9e67eSachartre 	label->dkl_pcyl = geom->dkg_pcyl;
2530bae9e67eSachartre 	label->dkl_nhead = geom->dkg_nhead;
2531bae9e67eSachartre 	label->dkl_nsect = geom->dkg_nsect;
2532bae9e67eSachartre 	label->dkl_intrlv = geom->dkg_intrlv;
2533bae9e67eSachartre 	label->dkl_apc = geom->dkg_apc;
2534bae9e67eSachartre 	label->dkl_rpm = geom->dkg_rpm;
2535bae9e67eSachartre 	label->dkl_write_reinstruct = geom->dkg_write_reinstruct;
2536bae9e67eSachartre 	label->dkl_read_reinstruct = geom->dkg_read_reinstruct;
2537bae9e67eSachartre 
2538bae9e67eSachartre 	label->dkl_vtoc.v_nparts = V_NUMPAR;
2539bae9e67eSachartre 	label->dkl_vtoc.v_sanity = VTOC_SANE;
2540bae9e67eSachartre 	label->dkl_vtoc.v_version = vtoc->v_version;
2541bae9e67eSachartre 	for (i = 0; i < V_NUMPAR; i++) {
2542bae9e67eSachartre 		label->dkl_vtoc.v_timestamp[i] = vtoc->timestamp[i];
2543bae9e67eSachartre 		label->dkl_vtoc.v_part[i].p_tag = vtoc->v_part[i].p_tag;
2544bae9e67eSachartre 		label->dkl_vtoc.v_part[i].p_flag = vtoc->v_part[i].p_flag;
2545bae9e67eSachartre 		label->dkl_map[i].dkl_cylno = vtoc->v_part[i].p_start /
2546bae9e67eSachartre 		    (label->dkl_nhead * label->dkl_nsect);
2547bae9e67eSachartre 		label->dkl_map[i].dkl_nblk = vtoc->v_part[i].p_size;
2548bae9e67eSachartre 	}
2549bae9e67eSachartre 
2550bae9e67eSachartre 	/*
2551bae9e67eSachartre 	 * The bootinfo array can not be copied with bcopy() because
2552bae9e67eSachartre 	 * elements are of type long in vtoc (so 64-bit) and of type
2553bae9e67eSachartre 	 * int in dk_vtoc (so 32-bit).
2554bae9e67eSachartre 	 */
2555bae9e67eSachartre 	label->dkl_vtoc.v_bootinfo[0] = vtoc->v_bootinfo[0];
2556bae9e67eSachartre 	label->dkl_vtoc.v_bootinfo[1] = vtoc->v_bootinfo[1];
2557bae9e67eSachartre 	label->dkl_vtoc.v_bootinfo[2] = vtoc->v_bootinfo[2];
2558bae9e67eSachartre 	bcopy(vtoc->v_asciilabel, label->dkl_asciilabel, LEN_DKL_ASCII);
2559bae9e67eSachartre 	bcopy(vtoc->v_volume, label->dkl_vtoc.v_volume, LEN_DKL_VVOL);
2560bae9e67eSachartre 
2561bae9e67eSachartre 	/* re-compute checksum */
2562bae9e67eSachartre 	label->dkl_magic = DKL_MAGIC;
2563bae9e67eSachartre 	label->dkl_cksum = vd_lbl2cksum(label);
2564bae9e67eSachartre }
2565bae9e67eSachartre 
2566bae9e67eSachartre /*
2567bae9e67eSachartre  * Copy information from a dk_label structure to a vtoc and dk_geom structures.
2568bae9e67eSachartre  */
2569bae9e67eSachartre static void
2570bae9e67eSachartre vd_label_to_vtocgeom(struct dk_label *label, struct vtoc *vtoc,
2571bae9e67eSachartre     struct dk_geom *geom)
2572bae9e67eSachartre {
2573bae9e67eSachartre 	int i;
2574bae9e67eSachartre 
2575bae9e67eSachartre 	bzero(vtoc, sizeof (struct vtoc));
2576bae9e67eSachartre 	bzero(geom, sizeof (struct dk_geom));
2577bae9e67eSachartre 
2578bae9e67eSachartre 	geom->dkg_ncyl = label->dkl_ncyl;
2579bae9e67eSachartre 	geom->dkg_acyl = label->dkl_acyl;
2580bae9e67eSachartre 	geom->dkg_nhead = label->dkl_nhead;
2581bae9e67eSachartre 	geom->dkg_nsect = label->dkl_nsect;
2582bae9e67eSachartre 	geom->dkg_intrlv = label->dkl_intrlv;
2583bae9e67eSachartre 	geom->dkg_apc = label->dkl_apc;
2584bae9e67eSachartre 	geom->dkg_rpm = label->dkl_rpm;
2585bae9e67eSachartre 	geom->dkg_pcyl = label->dkl_pcyl;
2586bae9e67eSachartre 	geom->dkg_write_reinstruct = label->dkl_write_reinstruct;
2587bae9e67eSachartre 	geom->dkg_read_reinstruct = label->dkl_read_reinstruct;
2588bae9e67eSachartre 
2589bae9e67eSachartre 	vtoc->v_sanity = label->dkl_vtoc.v_sanity;
2590bae9e67eSachartre 	vtoc->v_version = label->dkl_vtoc.v_version;
2591bae9e67eSachartre 	vtoc->v_sectorsz = DEV_BSIZE;
2592bae9e67eSachartre 	vtoc->v_nparts = label->dkl_vtoc.v_nparts;
2593bae9e67eSachartre 
2594bae9e67eSachartre 	for (i = 0; i < vtoc->v_nparts; i++) {
2595bae9e67eSachartre 		vtoc->v_part[i].p_tag = label->dkl_vtoc.v_part[i].p_tag;
2596bae9e67eSachartre 		vtoc->v_part[i].p_flag = label->dkl_vtoc.v_part[i].p_flag;
2597bae9e67eSachartre 		vtoc->v_part[i].p_start = label->dkl_map[i].dkl_cylno *
2598bae9e67eSachartre 		    (label->dkl_nhead * label->dkl_nsect);
2599bae9e67eSachartre 		vtoc->v_part[i].p_size = label->dkl_map[i].dkl_nblk;
2600bae9e67eSachartre 		vtoc->timestamp[i] = label->dkl_vtoc.v_timestamp[i];
2601bae9e67eSachartre 	}
2602bae9e67eSachartre 
2603bae9e67eSachartre 	/*
2604bae9e67eSachartre 	 * The bootinfo array can not be copied with bcopy() because
2605bae9e67eSachartre 	 * elements are of type long in vtoc (so 64-bit) and of type
2606bae9e67eSachartre 	 * int in dk_vtoc (so 32-bit).
2607bae9e67eSachartre 	 */
2608bae9e67eSachartre 	vtoc->v_bootinfo[0] = label->dkl_vtoc.v_bootinfo[0];
2609bae9e67eSachartre 	vtoc->v_bootinfo[1] = label->dkl_vtoc.v_bootinfo[1];
2610bae9e67eSachartre 	vtoc->v_bootinfo[2] = label->dkl_vtoc.v_bootinfo[2];
2611bae9e67eSachartre 	bcopy(label->dkl_asciilabel, vtoc->v_asciilabel, LEN_DKL_ASCII);
2612bae9e67eSachartre 	bcopy(label->dkl_vtoc.v_volume, vtoc->v_volume, LEN_DKL_VVOL);
2613bae9e67eSachartre }
2614bae9e67eSachartre 
2615bae9e67eSachartre /*
2616bae9e67eSachartre  * Check if a geometry is valid for a single-slice disk. A geometry is
2617bae9e67eSachartre  * considered valid if the main attributes of the geometry match with the
2618bae9e67eSachartre  * attributes of the fake geometry we have created.
2619bae9e67eSachartre  */
2620bae9e67eSachartre static boolean_t
2621bae9e67eSachartre vd_slice_geom_isvalid(vd_t *vd, struct dk_geom *geom)
2622bae9e67eSachartre {
2623bae9e67eSachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
2624bae9e67eSachartre 	ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
2625bae9e67eSachartre 
2626bae9e67eSachartre 	if (geom->dkg_ncyl != vd->dk_geom.dkg_ncyl ||
2627bae9e67eSachartre 	    geom->dkg_acyl != vd->dk_geom.dkg_acyl ||
2628bae9e67eSachartre 	    geom->dkg_nsect != vd->dk_geom.dkg_nsect ||
2629bae9e67eSachartre 	    geom->dkg_pcyl != vd->dk_geom.dkg_pcyl)
2630bae9e67eSachartre 		return (B_FALSE);
2631bae9e67eSachartre 
2632bae9e67eSachartre 	return (B_TRUE);
2633bae9e67eSachartre }
2634bae9e67eSachartre 
2635bae9e67eSachartre /*
2636bae9e67eSachartre  * Check if a vtoc is valid for a single-slice disk. A vtoc is considered
2637bae9e67eSachartre  * valid if the main attributes of the vtoc match with the attributes of the
2638bae9e67eSachartre  * fake vtoc we have created.
2639bae9e67eSachartre  */
2640bae9e67eSachartre static boolean_t
2641bae9e67eSachartre vd_slice_vtoc_isvalid(vd_t *vd, struct vtoc *vtoc)
2642bae9e67eSachartre {
2643bae9e67eSachartre 	size_t csize;
2644bae9e67eSachartre 	int i;
2645bae9e67eSachartre 
2646bae9e67eSachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
2647bae9e67eSachartre 	ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
2648bae9e67eSachartre 
2649bae9e67eSachartre 	if (vtoc->v_sanity != vd->vtoc.v_sanity ||
2650bae9e67eSachartre 	    vtoc->v_version != vd->vtoc.v_version ||
2651bae9e67eSachartre 	    vtoc->v_nparts != vd->vtoc.v_nparts ||
2652bae9e67eSachartre 	    strcmp(vtoc->v_volume, vd->vtoc.v_volume) != 0 ||
2653bae9e67eSachartre 	    strcmp(vtoc->v_asciilabel, vd->vtoc.v_asciilabel) != 0)
2654bae9e67eSachartre 		return (B_FALSE);
2655bae9e67eSachartre 
2656bae9e67eSachartre 	/* slice 2 should be unchanged */
2657bae9e67eSachartre 	if (vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_start !=
2658bae9e67eSachartre 	    vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_start ||
2659bae9e67eSachartre 	    vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_size !=
2660bae9e67eSachartre 	    vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_size)
2661bae9e67eSachartre 		return (B_FALSE);
2662bae9e67eSachartre 
2663bae9e67eSachartre 	/*
2664bae9e67eSachartre 	 * Slice 0 should be mostly unchanged and cover most of the disk.
2665bae9e67eSachartre 	 * However we allow some flexibility wrt to the start and the size
2666bae9e67eSachartre 	 * of this slice mainly because we can't exactly know how it will
2667bae9e67eSachartre 	 * be defined by the OS installer.
2668bae9e67eSachartre 	 *
2669bae9e67eSachartre 	 * We allow slice 0 to be defined as starting on any of the first
2670bae9e67eSachartre 	 * 4 cylinders.
2671bae9e67eSachartre 	 */
2672bae9e67eSachartre 	csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
2673bae9e67eSachartre 
2674bae9e67eSachartre 	if (vtoc->v_part[0].p_start > 4 * csize ||
2675bae9e67eSachartre 	    vtoc->v_part[0].p_size > vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_size)
2676bae9e67eSachartre 			return (B_FALSE);
2677bae9e67eSachartre 
2678bae9e67eSachartre 	if (vd->vtoc.v_part[0].p_size >= 4 * csize &&
2679bae9e67eSachartre 	    vtoc->v_part[0].p_size < vd->vtoc.v_part[0].p_size - 4 *csize)
2680bae9e67eSachartre 			return (B_FALSE);
2681bae9e67eSachartre 
2682bae9e67eSachartre 	/* any other slice should have a size of 0 */
2683bae9e67eSachartre 	for (i = 1; i < vtoc->v_nparts; i++) {
2684bae9e67eSachartre 		if (i != VD_ENTIRE_DISK_SLICE &&
2685bae9e67eSachartre 		    vtoc->v_part[i].p_size != 0)
2686bae9e67eSachartre 			return (B_FALSE);
2687bae9e67eSachartre 	}
2688bae9e67eSachartre 
2689bae9e67eSachartre 	return (B_TRUE);
2690bae9e67eSachartre }
2691bae9e67eSachartre 
2692bae9e67eSachartre /*
269387a7269eSachartre  * Handle ioctls to a disk slice.
2694205eeb1aSlm66018  *
2695205eeb1aSlm66018  * Return Values
2696205eeb1aSlm66018  *	0	- Indicates that there are no errors in disk operations
2697205eeb1aSlm66018  *	ENOTSUP	- Unknown disk label type or unsupported DKIO ioctl
2698205eeb1aSlm66018  *	EINVAL	- Not enough room to copy the EFI label
2699205eeb1aSlm66018  *
270087a7269eSachartre  */
27011ae08745Sheppo static int
27020a55fbb7Slm66018 vd_do_slice_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
27031ae08745Sheppo {
27044bac2208Snarayan 	dk_efi_t *dk_ioc;
2705bae9e67eSachartre 	struct vtoc *vtoc;
2706bae9e67eSachartre 	struct dk_geom *geom;
2707bae9e67eSachartre 	size_t len, lba;
2708edcc0754Sachartre 	int rval;
2709edcc0754Sachartre 
2710edcc0754Sachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
2711edcc0754Sachartre 
2712edcc0754Sachartre 	if (cmd == DKIOCFLUSHWRITECACHE) {
2713edcc0754Sachartre 		if (vd->file) {
2714edcc0754Sachartre 			return (VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL));
2715edcc0754Sachartre 		} else {
2716edcc0754Sachartre 			return (ldi_ioctl(vd->ldi_handle[0], cmd,
2717edcc0754Sachartre 			    (intptr_t)ioctl_arg, vd->open_flags | FKIOCTL,
2718edcc0754Sachartre 			    kcred, &rval));
2719edcc0754Sachartre 		}
2720edcc0754Sachartre 	}
27214bac2208Snarayan 
27224bac2208Snarayan 	switch (vd->vdisk_label) {
27234bac2208Snarayan 
2724edcc0754Sachartre 	/* ioctls for a single slice disk with a VTOC label */
27254bac2208Snarayan 	case VD_DISK_LABEL_VTOC:
27264bac2208Snarayan 
27271ae08745Sheppo 		switch (cmd) {
2728bae9e67eSachartre 
27291ae08745Sheppo 		case DKIOCGGEOM:
27300a55fbb7Slm66018 			ASSERT(ioctl_arg != NULL);
27310a55fbb7Slm66018 			bcopy(&vd->dk_geom, ioctl_arg, sizeof (vd->dk_geom));
27321ae08745Sheppo 			return (0);
2733bae9e67eSachartre 
27341ae08745Sheppo 		case DKIOCGVTOC:
27350a55fbb7Slm66018 			ASSERT(ioctl_arg != NULL);
27360a55fbb7Slm66018 			bcopy(&vd->vtoc, ioctl_arg, sizeof (vd->vtoc));
27371ae08745Sheppo 			return (0);
2738bae9e67eSachartre 
2739bae9e67eSachartre 		case DKIOCSGEOM:
2740bae9e67eSachartre 			ASSERT(ioctl_arg != NULL);
2741bae9e67eSachartre 			if (vd_slice_single_slice)
2742bae9e67eSachartre 				return (ENOTSUP);
2743bae9e67eSachartre 
2744bae9e67eSachartre 			/* fake success only if new geometry is valid */
2745bae9e67eSachartre 			geom = (struct dk_geom *)ioctl_arg;
2746bae9e67eSachartre 			if (!vd_slice_geom_isvalid(vd, geom))
2747bae9e67eSachartre 				return (EINVAL);
2748bae9e67eSachartre 
2749bae9e67eSachartre 			return (0);
2750bae9e67eSachartre 
2751bae9e67eSachartre 		case DKIOCSVTOC:
2752bae9e67eSachartre 			ASSERT(ioctl_arg != NULL);
2753bae9e67eSachartre 			if (vd_slice_single_slice)
2754bae9e67eSachartre 				return (ENOTSUP);
2755bae9e67eSachartre 
2756bae9e67eSachartre 			/* fake sucess only if the new vtoc is valid */
2757bae9e67eSachartre 			vtoc = (struct vtoc *)ioctl_arg;
2758bae9e67eSachartre 			if (!vd_slice_vtoc_isvalid(vd, vtoc))
2759bae9e67eSachartre 				return (EINVAL);
2760bae9e67eSachartre 
2761bae9e67eSachartre 			return (0);
2762bae9e67eSachartre 
276387a7269eSachartre 		default:
27643c96341aSnarayan 			return (ENOTSUP);
276587a7269eSachartre 		}
276687a7269eSachartre 
2767edcc0754Sachartre 	/* ioctls for a single slice disk with an EFI label */
276887a7269eSachartre 	case VD_DISK_LABEL_EFI:
276987a7269eSachartre 
2770bae9e67eSachartre 		if (cmd != DKIOCGETEFI && cmd != DKIOCSETEFI)
2771bae9e67eSachartre 			return (ENOTSUP);
2772bae9e67eSachartre 
27733c96341aSnarayan 		ASSERT(ioctl_arg != NULL);
277487a7269eSachartre 		dk_ioc = (dk_efi_t *)ioctl_arg;
2775edcc0754Sachartre 
2776bae9e67eSachartre 		len = dk_ioc->dki_length;
2777bae9e67eSachartre 		lba = dk_ioc->dki_lba;
2778edcc0754Sachartre 
2779bae9e67eSachartre 		if ((lba != VD_EFI_LBA_GPT && lba != VD_EFI_LBA_GPE) ||
2780bae9e67eSachartre 		    (lba == VD_EFI_LBA_GPT && len < sizeof (efi_gpt_t)) ||
2781bae9e67eSachartre 		    (lba == VD_EFI_LBA_GPE && len < sizeof (efi_gpe_t)))
278287a7269eSachartre 			return (EINVAL);
2783edcc0754Sachartre 
2784bae9e67eSachartre 		switch (cmd) {
2785bae9e67eSachartre 		case DKIOCGETEFI:
2786bae9e67eSachartre 			len = vd_slice_flabel_read(vd,
2787bae9e67eSachartre 			    (caddr_t)dk_ioc->dki_data, lba * DEV_BSIZE, len);
2788edcc0754Sachartre 
2789bae9e67eSachartre 			ASSERT(len > 0);
2790edcc0754Sachartre 
279187a7269eSachartre 			return (0);
2792bae9e67eSachartre 
2793bae9e67eSachartre 		case DKIOCSETEFI:
2794bae9e67eSachartre 			if (vd_slice_single_slice)
279587a7269eSachartre 				return (ENOTSUP);
2796bae9e67eSachartre 
2797bae9e67eSachartre 			/* we currently don't support writing EFI */
2798bae9e67eSachartre 			return (EIO);
279987a7269eSachartre 		}
280087a7269eSachartre 
280187a7269eSachartre 	default:
2802205eeb1aSlm66018 		/* Unknown disk label type */
280387a7269eSachartre 		return (ENOTSUP);
280487a7269eSachartre 	}
280587a7269eSachartre }
280687a7269eSachartre 
2807edcc0754Sachartre static int
2808edcc0754Sachartre vds_efi_alloc_and_read(vd_t *vd, efi_gpt_t **gpt, efi_gpe_t **gpe)
2809edcc0754Sachartre {
2810edcc0754Sachartre 	vd_efi_dev_t edev;
2811edcc0754Sachartre 	int status;
2812edcc0754Sachartre 
2813edcc0754Sachartre 	VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl);
2814edcc0754Sachartre 
2815edcc0754Sachartre 	status = vd_efi_alloc_and_read(&edev, gpt, gpe);
2816edcc0754Sachartre 
2817edcc0754Sachartre 	return (status);
2818edcc0754Sachartre }
2819edcc0754Sachartre 
2820edcc0754Sachartre static void
2821edcc0754Sachartre vds_efi_free(vd_t *vd, efi_gpt_t *gpt, efi_gpe_t *gpe)
2822edcc0754Sachartre {
2823edcc0754Sachartre 	vd_efi_dev_t edev;
2824edcc0754Sachartre 
2825edcc0754Sachartre 	VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl);
2826edcc0754Sachartre 
2827edcc0754Sachartre 	vd_efi_free(&edev, gpt, gpe);
2828edcc0754Sachartre }
2829edcc0754Sachartre 
2830edcc0754Sachartre static int
2831edcc0754Sachartre vd_file_validate_efi(vd_t *vd)
2832edcc0754Sachartre {
2833edcc0754Sachartre 	efi_gpt_t *gpt;
2834edcc0754Sachartre 	efi_gpe_t *gpe;
2835edcc0754Sachartre 	int i, nparts, status;
2836edcc0754Sachartre 	struct uuid efi_reserved = EFI_RESERVED;
2837edcc0754Sachartre 
2838edcc0754Sachartre 	if ((status = vds_efi_alloc_and_read(vd, &gpt, &gpe)) != 0)
2839edcc0754Sachartre 		return (status);
2840edcc0754Sachartre 
2841edcc0754Sachartre 	bzero(&vd->vtoc, sizeof (struct vtoc));
2842edcc0754Sachartre 	bzero(&vd->dk_geom, sizeof (struct dk_geom));
2843edcc0754Sachartre 	bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART);
2844edcc0754Sachartre 
2845edcc0754Sachartre 	vd->efi_reserved = -1;
2846edcc0754Sachartre 
2847edcc0754Sachartre 	nparts = gpt->efi_gpt_NumberOfPartitionEntries;
2848edcc0754Sachartre 
2849edcc0754Sachartre 	for (i = 0; i < nparts && i < VD_MAXPART; i++) {
2850edcc0754Sachartre 
2851edcc0754Sachartre 		if (gpe[i].efi_gpe_StartingLBA == 0 ||
2852edcc0754Sachartre 		    gpe[i].efi_gpe_EndingLBA == 0) {
2853edcc0754Sachartre 			continue;
2854edcc0754Sachartre 		}
2855edcc0754Sachartre 
2856edcc0754Sachartre 		vd->slices[i].start = gpe[i].efi_gpe_StartingLBA;
2857edcc0754Sachartre 		vd->slices[i].nblocks = gpe[i].efi_gpe_EndingLBA -
2858edcc0754Sachartre 		    gpe[i].efi_gpe_StartingLBA + 1;
2859edcc0754Sachartre 
2860edcc0754Sachartre 		if (bcmp(&gpe[i].efi_gpe_PartitionTypeGUID, &efi_reserved,
2861edcc0754Sachartre 		    sizeof (struct uuid)) == 0)
2862edcc0754Sachartre 			vd->efi_reserved = i;
2863edcc0754Sachartre 
2864edcc0754Sachartre 	}
2865edcc0754Sachartre 
2866edcc0754Sachartre 	ASSERT(vd->vdisk_size != 0);
2867edcc0754Sachartre 	vd->slices[VD_EFI_WD_SLICE].start = 0;
2868edcc0754Sachartre 	vd->slices[VD_EFI_WD_SLICE].nblocks = vd->vdisk_size;
2869edcc0754Sachartre 
2870edcc0754Sachartre 	vds_efi_free(vd, gpt, gpe);
2871edcc0754Sachartre 
2872edcc0754Sachartre 	return (status);
2873edcc0754Sachartre }
2874edcc0754Sachartre 
287587a7269eSachartre /*
287678fcd0a1Sachartre  * Function:
287778fcd0a1Sachartre  *	vd_file_validate_geometry
2878205eeb1aSlm66018  *
287978fcd0a1Sachartre  * Description:
288078fcd0a1Sachartre  *	Read the label and validate the geometry of a disk image. The driver
288178fcd0a1Sachartre  *	label, vtoc and geometry information are updated according to the
288278fcd0a1Sachartre  *	label read from the disk image.
288378fcd0a1Sachartre  *
288478fcd0a1Sachartre  *	If no valid label is found, the label is set to unknown and the
288578fcd0a1Sachartre  *	function returns EINVAL, but a default vtoc and geometry are provided
2886edcc0754Sachartre  *	to the driver. If an EFI label is found, ENOTSUP is returned.
288778fcd0a1Sachartre  *
288878fcd0a1Sachartre  * Parameters:
288978fcd0a1Sachartre  *	vd	- disk on which the operation is performed.
289078fcd0a1Sachartre  *
289178fcd0a1Sachartre  * Return Code:
289278fcd0a1Sachartre  *	0	- success.
289378fcd0a1Sachartre  *	EIO	- error reading the label from the disk image.
289478fcd0a1Sachartre  *	EINVAL	- unknown disk label.
2895edcc0754Sachartre  *	ENOTSUP	- geometry not applicable (EFI label).
289687a7269eSachartre  */
289787a7269eSachartre static int
289878fcd0a1Sachartre vd_file_validate_geometry(vd_t *vd)
289987a7269eSachartre {
290087a7269eSachartre 	struct dk_label label;
290178fcd0a1Sachartre 	struct dk_geom *geom = &vd->dk_geom;
290278fcd0a1Sachartre 	struct vtoc *vtoc = &vd->vtoc;
290378fcd0a1Sachartre 	int i;
290478fcd0a1Sachartre 	int status = 0;
290587a7269eSachartre 
290687a7269eSachartre 	ASSERT(vd->file);
2907edcc0754Sachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
290887a7269eSachartre 
290987a7269eSachartre 	if (VD_FILE_LABEL_READ(vd, &label) < 0)
291087a7269eSachartre 		return (EIO);
291187a7269eSachartre 
291287a7269eSachartre 	if (label.dkl_magic != DKL_MAGIC ||
291378fcd0a1Sachartre 	    label.dkl_cksum != vd_lbl2cksum(&label) ||
291466cfcfbeSachartre 	    (vd_file_validate_sanity && label.dkl_vtoc.v_sanity != VTOC_SANE) ||
291578fcd0a1Sachartre 	    label.dkl_vtoc.v_nparts != V_NUMPAR) {
2916edcc0754Sachartre 
2917edcc0754Sachartre 		if (vd_file_validate_efi(vd) == 0) {
2918edcc0754Sachartre 			vd->vdisk_label = VD_DISK_LABEL_EFI;
2919edcc0754Sachartre 			return (ENOTSUP);
2920edcc0754Sachartre 		}
2921edcc0754Sachartre 
292278fcd0a1Sachartre 		vd->vdisk_label = VD_DISK_LABEL_UNK;
2923bae9e67eSachartre 		vd_build_default_label(vd->file_size, &label);
292478fcd0a1Sachartre 		status = EINVAL;
292578fcd0a1Sachartre 	} else {
292678fcd0a1Sachartre 		vd->vdisk_label = VD_DISK_LABEL_VTOC;
292778fcd0a1Sachartre 	}
292887a7269eSachartre 
2929bae9e67eSachartre 	/* Update the driver geometry and vtoc */
2930bae9e67eSachartre 	vd_label_to_vtocgeom(&label, vtoc, geom);
293187a7269eSachartre 
2932edcc0754Sachartre 	/* Update logical partitions */
2933edcc0754Sachartre 	bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART);
2934edcc0754Sachartre 	if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
2935edcc0754Sachartre 		for (i = 0; i < vtoc->v_nparts; i++) {
2936edcc0754Sachartre 			vd->slices[i].start = vtoc->v_part[i].p_start;
2937edcc0754Sachartre 			vd->slices[i].nblocks = vtoc->v_part[i].p_size;
2938edcc0754Sachartre 		}
2939edcc0754Sachartre 	}
2940edcc0754Sachartre 
294178fcd0a1Sachartre 	return (status);
294278fcd0a1Sachartre }
294378fcd0a1Sachartre 
294478fcd0a1Sachartre /*
294578fcd0a1Sachartre  * Handle ioctls to a disk image (file-based).
294678fcd0a1Sachartre  *
294778fcd0a1Sachartre  * Return Values
294878fcd0a1Sachartre  *	0	- Indicates that there are no errors
294978fcd0a1Sachartre  *	!= 0	- Disk operation returned an error
295078fcd0a1Sachartre  */
295178fcd0a1Sachartre static int
295278fcd0a1Sachartre vd_do_file_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
295378fcd0a1Sachartre {
295478fcd0a1Sachartre 	struct dk_label label;
295578fcd0a1Sachartre 	struct dk_geom *geom;
295678fcd0a1Sachartre 	struct vtoc *vtoc;
2957edcc0754Sachartre 	dk_efi_t *efi;
2958bae9e67eSachartre 	int rc;
295978fcd0a1Sachartre 
296078fcd0a1Sachartre 	ASSERT(vd->file);
2961edcc0754Sachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
296278fcd0a1Sachartre 
296378fcd0a1Sachartre 	switch (cmd) {
296478fcd0a1Sachartre 
296578fcd0a1Sachartre 	case DKIOCGGEOM:
296678fcd0a1Sachartre 		ASSERT(ioctl_arg != NULL);
296778fcd0a1Sachartre 		geom = (struct dk_geom *)ioctl_arg;
296878fcd0a1Sachartre 
296978fcd0a1Sachartre 		rc = vd_file_validate_geometry(vd);
2970edcc0754Sachartre 		if (rc != 0 && rc != EINVAL)
297178fcd0a1Sachartre 			return (rc);
297278fcd0a1Sachartre 		bcopy(&vd->dk_geom, geom, sizeof (struct dk_geom));
297378fcd0a1Sachartre 		return (0);
297478fcd0a1Sachartre 
297578fcd0a1Sachartre 	case DKIOCGVTOC:
297678fcd0a1Sachartre 		ASSERT(ioctl_arg != NULL);
297778fcd0a1Sachartre 		vtoc = (struct vtoc *)ioctl_arg;
297878fcd0a1Sachartre 
297978fcd0a1Sachartre 		rc = vd_file_validate_geometry(vd);
2980edcc0754Sachartre 		if (rc != 0 && rc != EINVAL)
298178fcd0a1Sachartre 			return (rc);
298278fcd0a1Sachartre 		bcopy(&vd->vtoc, vtoc, sizeof (struct vtoc));
298387a7269eSachartre 		return (0);
298487a7269eSachartre 
298587a7269eSachartre 	case DKIOCSGEOM:
298687a7269eSachartre 		ASSERT(ioctl_arg != NULL);
298787a7269eSachartre 		geom = (struct dk_geom *)ioctl_arg;
298887a7269eSachartre 
298987a7269eSachartre 		if (geom->dkg_nhead == 0 || geom->dkg_nsect == 0)
299087a7269eSachartre 			return (EINVAL);
299187a7269eSachartre 
299287a7269eSachartre 		/*
299387a7269eSachartre 		 * The current device geometry is not updated, just the driver
299487a7269eSachartre 		 * "notion" of it. The device geometry will be effectively
299587a7269eSachartre 		 * updated when a label is written to the device during a next
299687a7269eSachartre 		 * DKIOCSVTOC.
299787a7269eSachartre 		 */
299887a7269eSachartre 		bcopy(ioctl_arg, &vd->dk_geom, sizeof (vd->dk_geom));
299987a7269eSachartre 		return (0);
300087a7269eSachartre 
300187a7269eSachartre 	case DKIOCSVTOC:
300287a7269eSachartre 		ASSERT(ioctl_arg != NULL);
300387a7269eSachartre 		ASSERT(vd->dk_geom.dkg_nhead != 0 &&
300487a7269eSachartre 		    vd->dk_geom.dkg_nsect != 0);
3005690555a1Sachartre 		vtoc = (struct vtoc *)ioctl_arg;
3006690555a1Sachartre 
3007690555a1Sachartre 		if (vtoc->v_sanity != VTOC_SANE ||
3008690555a1Sachartre 		    vtoc->v_sectorsz != DEV_BSIZE ||
3009690555a1Sachartre 		    vtoc->v_nparts != V_NUMPAR)
3010690555a1Sachartre 			return (EINVAL);
3011690555a1Sachartre 
3012bae9e67eSachartre 		vd_vtocgeom_to_label(vtoc, &vd->dk_geom, &label);
3013690555a1Sachartre 
301487a7269eSachartre 		/* write label to the disk image */
301587a7269eSachartre 		if ((rc = vd_file_set_vtoc(vd, &label)) != 0)
301687a7269eSachartre 			return (rc);
3017690555a1Sachartre 
3018edcc0754Sachartre 		break;
3019edcc0754Sachartre 
3020edcc0754Sachartre 	case DKIOCFLUSHWRITECACHE:
3021edcc0754Sachartre 		return (VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL));
3022edcc0754Sachartre 
3023edcc0754Sachartre 	case DKIOCGETEFI:
3024edcc0754Sachartre 		ASSERT(ioctl_arg != NULL);
3025edcc0754Sachartre 		efi = (dk_efi_t *)ioctl_arg;
3026edcc0754Sachartre 
3027edcc0754Sachartre 		if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD,
3028edcc0754Sachartre 		    (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0)
3029edcc0754Sachartre 			return (EIO);
3030edcc0754Sachartre 
3031edcc0754Sachartre 		return (0);
3032edcc0754Sachartre 
3033edcc0754Sachartre 	case DKIOCSETEFI:
3034edcc0754Sachartre 		ASSERT(ioctl_arg != NULL);
3035edcc0754Sachartre 		efi = (dk_efi_t *)ioctl_arg;
3036edcc0754Sachartre 
3037edcc0754Sachartre 		if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE,
3038edcc0754Sachartre 		    (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0)
3039edcc0754Sachartre 			return (EIO);
3040edcc0754Sachartre 
3041edcc0754Sachartre 		break;
3042edcc0754Sachartre 
3043edcc0754Sachartre 
3044edcc0754Sachartre 	default:
3045edcc0754Sachartre 		return (ENOTSUP);
3046edcc0754Sachartre 	}
3047edcc0754Sachartre 
3048edcc0754Sachartre 	ASSERT(cmd == DKIOCSVTOC || cmd == DKIOCSETEFI);
3049edcc0754Sachartre 
3050edcc0754Sachartre 	/* label has changed, revalidate the geometry */
3051edcc0754Sachartre 	(void) vd_file_validate_geometry(vd);
30523c96341aSnarayan 
305387a7269eSachartre 	/*
305487a7269eSachartre 	 * The disk geometry may have changed, so we need to write
305587a7269eSachartre 	 * the devid (if there is one) so that it is stored at the
305687a7269eSachartre 	 * right location.
305787a7269eSachartre 	 */
3058edcc0754Sachartre 	if (vd_file_write_devid(vd, vd->file_devid) != 0) {
305987a7269eSachartre 		PR0("Fail to write devid");
30601ae08745Sheppo 	}
30614bac2208Snarayan 
30624bac2208Snarayan 	return (0);
30634bac2208Snarayan }
3064edcc0754Sachartre 
3065edcc0754Sachartre static int
3066edcc0754Sachartre vd_backend_ioctl(vd_t *vd, int cmd, caddr_t arg)
3067edcc0754Sachartre {
3068edcc0754Sachartre 	int rval = 0, status;
3069edcc0754Sachartre 
3070edcc0754Sachartre 	/*
3071edcc0754Sachartre 	 * Call the appropriate function to execute the ioctl depending
3072edcc0754Sachartre 	 * on the type of vdisk.
3073edcc0754Sachartre 	 */
3074edcc0754Sachartre 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
3075edcc0754Sachartre 
3076edcc0754Sachartre 		/* slice, file or volume exported as a single slice disk */
3077edcc0754Sachartre 		status = vd_do_slice_ioctl(vd, cmd, arg);
3078edcc0754Sachartre 
3079edcc0754Sachartre 	} else if (vd->file) {
3080edcc0754Sachartre 
3081edcc0754Sachartre 		/* file or volume exported as a full disk */
3082edcc0754Sachartre 		status = vd_do_file_ioctl(vd, cmd, arg);
3083edcc0754Sachartre 
3084edcc0754Sachartre 	} else {
3085edcc0754Sachartre 
3086edcc0754Sachartre 		/* disk device exported as a full disk */
3087edcc0754Sachartre 		status = ldi_ioctl(vd->ldi_handle[0], cmd, (intptr_t)arg,
3088edcc0754Sachartre 		    vd->open_flags | FKIOCTL, kcred, &rval);
3089edcc0754Sachartre 	}
3090edcc0754Sachartre 
3091edcc0754Sachartre #ifdef DEBUG
3092edcc0754Sachartre 	if (rval != 0) {
3093edcc0754Sachartre 		PR0("ioctl %x set rval = %d, which is not being returned"
3094edcc0754Sachartre 		    " to caller", cmd, rval);
3095edcc0754Sachartre 	}
3096edcc0754Sachartre #endif /* DEBUG */
3097edcc0754Sachartre 
3098edcc0754Sachartre 	return (status);
30991ae08745Sheppo }
31001ae08745Sheppo 
3101205eeb1aSlm66018 /*
3102205eeb1aSlm66018  * Description:
3103205eeb1aSlm66018  *	This is the function that processes the ioctl requests (farming it
3104205eeb1aSlm66018  *	out to functions that handle slices, files or whole disks)
3105205eeb1aSlm66018  *
3106205eeb1aSlm66018  * Return Values
3107205eeb1aSlm66018  *     0		- ioctl operation completed successfully
3108205eeb1aSlm66018  *     != 0		- The LDC error value encountered
3109205eeb1aSlm66018  *			  (propagated back up the call stack as a task error)
3110205eeb1aSlm66018  *
3111205eeb1aSlm66018  * Side Effect
3112205eeb1aSlm66018  *     sets request->status to the return value of the ioctl function.
3113205eeb1aSlm66018  */
31141ae08745Sheppo static int
31150a55fbb7Slm66018 vd_do_ioctl(vd_t *vd, vd_dring_payload_t *request, void* buf, vd_ioctl_t *ioctl)
31161ae08745Sheppo {
3117edcc0754Sachartre 	int	status = 0;
31181ae08745Sheppo 	size_t	nbytes = request->nbytes;	/* modifiable copy */
31191ae08745Sheppo 
31201ae08745Sheppo 
31211ae08745Sheppo 	ASSERT(request->slice < vd->nslices);
31221ae08745Sheppo 	PR0("Performing %s", ioctl->operation_name);
31231ae08745Sheppo 
31240a55fbb7Slm66018 	/* Get data from client and convert, if necessary */
31250a55fbb7Slm66018 	if (ioctl->copyin != NULL)  {
31261ae08745Sheppo 		ASSERT(nbytes != 0 && buf != NULL);
31271ae08745Sheppo 		PR1("Getting \"arg\" data from client");
31281ae08745Sheppo 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
31291ae08745Sheppo 		    request->cookie, request->ncookies,
31301ae08745Sheppo 		    LDC_COPY_IN)) != 0) {
31313af08d82Slm66018 			PR0("ldc_mem_copy() returned errno %d "
31321ae08745Sheppo 			    "copying from client", status);
31331ae08745Sheppo 			return (status);
31341ae08745Sheppo 		}
31350a55fbb7Slm66018 
31360a55fbb7Slm66018 		/* Convert client's data, if necessary */
31372f5224aeSachartre 		if (ioctl->copyin == VD_IDENTITY_IN) {
31382f5224aeSachartre 			/* use client buffer */
31390a55fbb7Slm66018 			ioctl->arg = buf;
31402f5224aeSachartre 		} else {
31412f5224aeSachartre 			/* convert client vdisk operation data to ioctl data */
31422f5224aeSachartre 			status = (ioctl->copyin)(buf, nbytes,
31432f5224aeSachartre 			    (void *)ioctl->arg);
31442f5224aeSachartre 			if (status != 0) {
31452f5224aeSachartre 				request->status = status;
31462f5224aeSachartre 				return (0);
31472f5224aeSachartre 			}
31482f5224aeSachartre 		}
31492f5224aeSachartre 	}
31502f5224aeSachartre 
31512f5224aeSachartre 	if (ioctl->operation == VD_OP_SCSICMD) {
31522f5224aeSachartre 		struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl->arg;
31532f5224aeSachartre 
31542f5224aeSachartre 		/* check write permission */
31552f5224aeSachartre 		if (!(vd->open_flags & FWRITE) &&
31562f5224aeSachartre 		    !(uscsi->uscsi_flags & USCSI_READ)) {
31572f5224aeSachartre 			PR0("uscsi fails because backend is opened read-only");
31582f5224aeSachartre 			request->status = EROFS;
31592f5224aeSachartre 			return (0);
31602f5224aeSachartre 		}
31611ae08745Sheppo 	}
31621ae08745Sheppo 
31631ae08745Sheppo 	/*
3164edcc0754Sachartre 	 * Send the ioctl to the disk backend.
31651ae08745Sheppo 	 */
3166edcc0754Sachartre 	request->status = vd_backend_ioctl(vd, ioctl->cmd, ioctl->arg);
3167205eeb1aSlm66018 
3168205eeb1aSlm66018 	if (request->status != 0) {
3169205eeb1aSlm66018 		PR0("ioctl(%s) = errno %d", ioctl->cmd_name, request->status);
31702f5224aeSachartre 		if (ioctl->operation == VD_OP_SCSICMD &&
31712f5224aeSachartre 		    ((struct uscsi_cmd *)ioctl->arg)->uscsi_status != 0)
31722f5224aeSachartre 			/*
31732f5224aeSachartre 			 * USCSICMD has reported an error and the uscsi_status
31742f5224aeSachartre 			 * field is not zero. This means that the SCSI command
31752f5224aeSachartre 			 * has completed but it has an error. So we should
31762f5224aeSachartre 			 * mark the VD operation has succesfully completed
31772f5224aeSachartre 			 * and clients can check the SCSI status field for
31782f5224aeSachartre 			 * SCSI errors.
31792f5224aeSachartre 			 */
31802f5224aeSachartre 			request->status = 0;
31812f5224aeSachartre 		else
3182205eeb1aSlm66018 			return (0);
3183205eeb1aSlm66018 	}
31841ae08745Sheppo 
31850a55fbb7Slm66018 	/* Convert data and send to client, if necessary */
31860a55fbb7Slm66018 	if (ioctl->copyout != NULL)  {
31871ae08745Sheppo 		ASSERT(nbytes != 0 && buf != NULL);
31881ae08745Sheppo 		PR1("Sending \"arg\" data to client");
31890a55fbb7Slm66018 
31900a55fbb7Slm66018 		/* Convert ioctl data to vdisk operation data, if necessary */
31912f5224aeSachartre 		if (ioctl->copyout != VD_IDENTITY_OUT)
31920a55fbb7Slm66018 			(ioctl->copyout)((void *)ioctl->arg, buf);
31930a55fbb7Slm66018 
31941ae08745Sheppo 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
31951ae08745Sheppo 		    request->cookie, request->ncookies,
31961ae08745Sheppo 		    LDC_COPY_OUT)) != 0) {
31973af08d82Slm66018 			PR0("ldc_mem_copy() returned errno %d "
31981ae08745Sheppo 			    "copying to client", status);
31991ae08745Sheppo 			return (status);
32001ae08745Sheppo 		}
32011ae08745Sheppo 	}
32021ae08745Sheppo 
32031ae08745Sheppo 	return (status);
32041ae08745Sheppo }
32051ae08745Sheppo 
32061ae08745Sheppo #define	RNDSIZE(expr) P2ROUNDUP(sizeof (expr), sizeof (uint64_t))
3207205eeb1aSlm66018 
3208205eeb1aSlm66018 /*
3209205eeb1aSlm66018  * Description:
3210205eeb1aSlm66018  *	This generic function is called by the task queue to complete
3211205eeb1aSlm66018  *	the processing of the tasks. The specific completion function
3212205eeb1aSlm66018  *	is passed in as a field in the task pointer.
3213205eeb1aSlm66018  *
3214205eeb1aSlm66018  * Parameters:
3215205eeb1aSlm66018  *	arg 	- opaque pointer to structure containing task to be completed
3216205eeb1aSlm66018  *
3217205eeb1aSlm66018  * Return Values
3218205eeb1aSlm66018  *	None
3219205eeb1aSlm66018  */
3220205eeb1aSlm66018 static void
3221205eeb1aSlm66018 vd_complete(void *arg)
3222205eeb1aSlm66018 {
3223205eeb1aSlm66018 	vd_task_t	*task = (vd_task_t *)arg;
3224205eeb1aSlm66018 
3225205eeb1aSlm66018 	ASSERT(task != NULL);
3226205eeb1aSlm66018 	ASSERT(task->status == EINPROGRESS);
3227205eeb1aSlm66018 	ASSERT(task->completef != NULL);
3228205eeb1aSlm66018 
3229205eeb1aSlm66018 	task->status = task->completef(task);
3230205eeb1aSlm66018 	if (task->status)
3231205eeb1aSlm66018 		PR0("%s: Error %d completing task", __func__, task->status);
3232205eeb1aSlm66018 
3233205eeb1aSlm66018 	/* Now notify the vDisk client */
3234205eeb1aSlm66018 	vd_complete_notify(task);
3235205eeb1aSlm66018 }
3236205eeb1aSlm66018 
32371ae08745Sheppo static int
3238d10e4ef2Snarayan vd_ioctl(vd_task_t *task)
32391ae08745Sheppo {
324087a7269eSachartre 	int			i, status;
32411ae08745Sheppo 	void			*buf = NULL;
32420a55fbb7Slm66018 	struct dk_geom		dk_geom = {0};
32430a55fbb7Slm66018 	struct vtoc		vtoc = {0};
32444bac2208Snarayan 	struct dk_efi		dk_efi = {0};
32452f5224aeSachartre 	struct uscsi_cmd	uscsi = {0};
3246d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
3247d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
32480a55fbb7Slm66018 	vd_ioctl_t		ioctl[] = {
32490a55fbb7Slm66018 		/* Command (no-copy) operations */
32500a55fbb7Slm66018 		{VD_OP_FLUSH, STRINGIZE(VD_OP_FLUSH), 0,
32510a55fbb7Slm66018 		    DKIOCFLUSHWRITECACHE, STRINGIZE(DKIOCFLUSHWRITECACHE),
3252047ba61eSachartre 		    NULL, NULL, NULL, B_TRUE},
32530a55fbb7Slm66018 
32540a55fbb7Slm66018 		/* "Get" (copy-out) operations */
32550a55fbb7Slm66018 		{VD_OP_GET_WCE, STRINGIZE(VD_OP_GET_WCE), RNDSIZE(int),
32560a55fbb7Slm66018 		    DKIOCGETWCE, STRINGIZE(DKIOCGETWCE),
32572f5224aeSachartre 		    NULL, VD_IDENTITY_IN, VD_IDENTITY_OUT, B_FALSE},
32580a55fbb7Slm66018 		{VD_OP_GET_DISKGEOM, STRINGIZE(VD_OP_GET_DISKGEOM),
32590a55fbb7Slm66018 		    RNDSIZE(vd_geom_t),
32600a55fbb7Slm66018 		    DKIOCGGEOM, STRINGIZE(DKIOCGGEOM),
3261047ba61eSachartre 		    &dk_geom, NULL, dk_geom2vd_geom, B_FALSE},
32620a55fbb7Slm66018 		{VD_OP_GET_VTOC, STRINGIZE(VD_OP_GET_VTOC), RNDSIZE(vd_vtoc_t),
32630a55fbb7Slm66018 		    DKIOCGVTOC, STRINGIZE(DKIOCGVTOC),
3264047ba61eSachartre 		    &vtoc, NULL, vtoc2vd_vtoc, B_FALSE},
32654bac2208Snarayan 		{VD_OP_GET_EFI, STRINGIZE(VD_OP_GET_EFI), RNDSIZE(vd_efi_t),
32664bac2208Snarayan 		    DKIOCGETEFI, STRINGIZE(DKIOCGETEFI),
3267047ba61eSachartre 		    &dk_efi, vd_get_efi_in, vd_get_efi_out, B_FALSE},
32680a55fbb7Slm66018 
32690a55fbb7Slm66018 		/* "Set" (copy-in) operations */
32700a55fbb7Slm66018 		{VD_OP_SET_WCE, STRINGIZE(VD_OP_SET_WCE), RNDSIZE(int),
32710a55fbb7Slm66018 		    DKIOCSETWCE, STRINGIZE(DKIOCSETWCE),
32722f5224aeSachartre 		    NULL, VD_IDENTITY_IN, VD_IDENTITY_OUT, B_TRUE},
32730a55fbb7Slm66018 		{VD_OP_SET_DISKGEOM, STRINGIZE(VD_OP_SET_DISKGEOM),
32740a55fbb7Slm66018 		    RNDSIZE(vd_geom_t),
32750a55fbb7Slm66018 		    DKIOCSGEOM, STRINGIZE(DKIOCSGEOM),
3276047ba61eSachartre 		    &dk_geom, vd_geom2dk_geom, NULL, B_TRUE},
32770a55fbb7Slm66018 		{VD_OP_SET_VTOC, STRINGIZE(VD_OP_SET_VTOC), RNDSIZE(vd_vtoc_t),
32780a55fbb7Slm66018 		    DKIOCSVTOC, STRINGIZE(DKIOCSVTOC),
3279047ba61eSachartre 		    &vtoc, vd_vtoc2vtoc, NULL, B_TRUE},
32804bac2208Snarayan 		{VD_OP_SET_EFI, STRINGIZE(VD_OP_SET_EFI), RNDSIZE(vd_efi_t),
32814bac2208Snarayan 		    DKIOCSETEFI, STRINGIZE(DKIOCSETEFI),
3282047ba61eSachartre 		    &dk_efi, vd_set_efi_in, vd_set_efi_out, B_TRUE},
32832f5224aeSachartre 
32842f5224aeSachartre 		{VD_OP_SCSICMD, STRINGIZE(VD_OP_SCSICMD), RNDSIZE(vd_scsi_t),
32852f5224aeSachartre 		    USCSICMD, STRINGIZE(USCSICMD),
32862f5224aeSachartre 		    &uscsi, vd_scsicmd_in, vd_scsicmd_out, B_FALSE},
32870a55fbb7Slm66018 	};
32881ae08745Sheppo 	size_t		nioctls = (sizeof (ioctl))/(sizeof (ioctl[0]));
32891ae08745Sheppo 
32901ae08745Sheppo 
3291d10e4ef2Snarayan 	ASSERT(vd != NULL);
3292d10e4ef2Snarayan 	ASSERT(request != NULL);
32931ae08745Sheppo 	ASSERT(request->slice < vd->nslices);
32941ae08745Sheppo 
32951ae08745Sheppo 	/*
32961ae08745Sheppo 	 * Determine ioctl corresponding to caller's "operation" and
32971ae08745Sheppo 	 * validate caller's "nbytes"
32981ae08745Sheppo 	 */
32991ae08745Sheppo 	for (i = 0; i < nioctls; i++) {
33001ae08745Sheppo 		if (request->operation == ioctl[i].operation) {
33010a55fbb7Slm66018 			/* LDC memory operations require 8-byte multiples */
33020a55fbb7Slm66018 			ASSERT(ioctl[i].nbytes % sizeof (uint64_t) == 0);
33030a55fbb7Slm66018 
33044bac2208Snarayan 			if (request->operation == VD_OP_GET_EFI ||
33052f5224aeSachartre 			    request->operation == VD_OP_SET_EFI ||
33062f5224aeSachartre 			    request->operation == VD_OP_SCSICMD) {
33074bac2208Snarayan 				if (request->nbytes >= ioctl[i].nbytes)
33084bac2208Snarayan 					break;
33093af08d82Slm66018 				PR0("%s:  Expected at least nbytes = %lu, "
33104bac2208Snarayan 				    "got %lu", ioctl[i].operation_name,
33114bac2208Snarayan 				    ioctl[i].nbytes, request->nbytes);
33124bac2208Snarayan 				return (EINVAL);
33134bac2208Snarayan 			}
33144bac2208Snarayan 
33150a55fbb7Slm66018 			if (request->nbytes != ioctl[i].nbytes) {
33163af08d82Slm66018 				PR0("%s:  Expected nbytes = %lu, got %lu",
33170a55fbb7Slm66018 				    ioctl[i].operation_name, ioctl[i].nbytes,
33180a55fbb7Slm66018 				    request->nbytes);
33191ae08745Sheppo 				return (EINVAL);
33201ae08745Sheppo 			}
33211ae08745Sheppo 
33221ae08745Sheppo 			break;
33231ae08745Sheppo 		}
33241ae08745Sheppo 	}
33251ae08745Sheppo 	ASSERT(i < nioctls);	/* because "operation" already validated */
33261ae08745Sheppo 
3327047ba61eSachartre 	if (!(vd->open_flags & FWRITE) && ioctl[i].write) {
3328047ba61eSachartre 		PR0("%s fails because backend is opened read-only",
3329047ba61eSachartre 		    ioctl[i].operation_name);
3330047ba61eSachartre 		request->status = EROFS;
3331047ba61eSachartre 		return (0);
3332047ba61eSachartre 	}
3333047ba61eSachartre 
33341ae08745Sheppo 	if (request->nbytes)
33351ae08745Sheppo 		buf = kmem_zalloc(request->nbytes, KM_SLEEP);
33361ae08745Sheppo 	status = vd_do_ioctl(vd, request, buf, &ioctl[i]);
33371ae08745Sheppo 	if (request->nbytes)
33381ae08745Sheppo 		kmem_free(buf, request->nbytes);
333987a7269eSachartre 
33401ae08745Sheppo 	return (status);
33411ae08745Sheppo }
33421ae08745Sheppo 
33434bac2208Snarayan static int
33444bac2208Snarayan vd_get_devid(vd_task_t *task)
33454bac2208Snarayan {
33464bac2208Snarayan 	vd_t *vd = task->vd;
33474bac2208Snarayan 	vd_dring_payload_t *request = task->request;
33484bac2208Snarayan 	vd_devid_t *vd_devid;
33494bac2208Snarayan 	impl_devid_t *devid;
335087a7269eSachartre 	int status, bufid_len, devid_len, len, sz;
33513af08d82Slm66018 	int bufbytes;
33524bac2208Snarayan 
33533af08d82Slm66018 	PR1("Get Device ID, nbytes=%ld", request->nbytes);
33544bac2208Snarayan 
3355bae9e67eSachartre 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
3356bae9e67eSachartre 		/*
3357bae9e67eSachartre 		 * We don't support devid for single-slice disks because we
3358bae9e67eSachartre 		 * have no space to store a fabricated devid and for physical
3359bae9e67eSachartre 		 * disk slices, we can't use the devid of the disk otherwise
3360bae9e67eSachartre 		 * exporting multiple slices from the same disk will produce
3361bae9e67eSachartre 		 * the same devids.
3362bae9e67eSachartre 		 */
3363bae9e67eSachartre 		PR2("No Device ID for slices");
3364bae9e67eSachartre 		request->status = ENOTSUP;
3365bae9e67eSachartre 		return (0);
3366bae9e67eSachartre 	}
3367bae9e67eSachartre 
33683c96341aSnarayan 	if (vd->file) {
336987a7269eSachartre 		if (vd->file_devid == NULL) {
33703af08d82Slm66018 			PR2("No Device ID");
3371205eeb1aSlm66018 			request->status = ENOENT;
3372205eeb1aSlm66018 			return (0);
337387a7269eSachartre 		} else {
337487a7269eSachartre 			sz = ddi_devid_sizeof(vd->file_devid);
337587a7269eSachartre 			devid = kmem_alloc(sz, KM_SLEEP);
337687a7269eSachartre 			bcopy(vd->file_devid, devid, sz);
337787a7269eSachartre 		}
337887a7269eSachartre 	} else {
337987a7269eSachartre 		if (ddi_lyr_get_devid(vd->dev[request->slice],
338087a7269eSachartre 		    (ddi_devid_t *)&devid) != DDI_SUCCESS) {
338187a7269eSachartre 			PR2("No Device ID");
3382205eeb1aSlm66018 			request->status = ENOENT;
3383205eeb1aSlm66018 			return (0);
338487a7269eSachartre 		}
33854bac2208Snarayan 	}
33864bac2208Snarayan 
33874bac2208Snarayan 	bufid_len = request->nbytes - sizeof (vd_devid_t) + 1;
33884bac2208Snarayan 	devid_len = DEVID_GETLEN(devid);
33894bac2208Snarayan 
33903af08d82Slm66018 	/*
33913af08d82Slm66018 	 * Save the buffer size here for use in deallocation.
33923af08d82Slm66018 	 * The actual number of bytes copied is returned in
33933af08d82Slm66018 	 * the 'nbytes' field of the request structure.
33943af08d82Slm66018 	 */
33953af08d82Slm66018 	bufbytes = request->nbytes;
33963af08d82Slm66018 
33973af08d82Slm66018 	vd_devid = kmem_zalloc(bufbytes, KM_SLEEP);
33984bac2208Snarayan 	vd_devid->length = devid_len;
33994bac2208Snarayan 	vd_devid->type = DEVID_GETTYPE(devid);
34004bac2208Snarayan 
34014bac2208Snarayan 	len = (devid_len > bufid_len)? bufid_len : devid_len;
34024bac2208Snarayan 
34034bac2208Snarayan 	bcopy(devid->did_id, vd_devid->id, len);
34044bac2208Snarayan 
340578fcd0a1Sachartre 	request->status = 0;
340678fcd0a1Sachartre 
34074bac2208Snarayan 	/* LDC memory operations require 8-byte multiples */
34084bac2208Snarayan 	ASSERT(request->nbytes % sizeof (uint64_t) == 0);
34094bac2208Snarayan 
34104bac2208Snarayan 	if ((status = ldc_mem_copy(vd->ldc_handle, (caddr_t)vd_devid, 0,
34114bac2208Snarayan 	    &request->nbytes, request->cookie, request->ncookies,
34124bac2208Snarayan 	    LDC_COPY_OUT)) != 0) {
34133af08d82Slm66018 		PR0("ldc_mem_copy() returned errno %d copying to client",
34144bac2208Snarayan 		    status);
34154bac2208Snarayan 	}
34163af08d82Slm66018 	PR1("post mem_copy: nbytes=%ld", request->nbytes);
34174bac2208Snarayan 
34183af08d82Slm66018 	kmem_free(vd_devid, bufbytes);
34194bac2208Snarayan 	ddi_devid_free((ddi_devid_t)devid);
34204bac2208Snarayan 
34214bac2208Snarayan 	return (status);
34224bac2208Snarayan }
34234bac2208Snarayan 
34242f5224aeSachartre static int
34252f5224aeSachartre vd_scsi_reset(vd_t *vd)
34262f5224aeSachartre {
34272f5224aeSachartre 	int rval, status;
34282f5224aeSachartre 	struct uscsi_cmd uscsi = { 0 };
34292f5224aeSachartre 
34302f5224aeSachartre 	uscsi.uscsi_flags = vd_scsi_debug | USCSI_RESET;
34312f5224aeSachartre 	uscsi.uscsi_timeout = vd_scsi_rdwr_timeout;
34322f5224aeSachartre 
34332f5224aeSachartre 	status = ldi_ioctl(vd->ldi_handle[0], USCSICMD, (intptr_t)&uscsi,
34342f5224aeSachartre 	    (vd->open_flags | FKIOCTL), kcred, &rval);
34352f5224aeSachartre 
34362f5224aeSachartre 	return (status);
34372f5224aeSachartre }
34382f5224aeSachartre 
34392f5224aeSachartre static int
34402f5224aeSachartre vd_reset(vd_task_t *task)
34412f5224aeSachartre {
34422f5224aeSachartre 	vd_t *vd = task->vd;
34432f5224aeSachartre 	vd_dring_payload_t *request = task->request;
34442f5224aeSachartre 
34452f5224aeSachartre 	ASSERT(request->operation == VD_OP_RESET);
34462f5224aeSachartre 	ASSERT(vd->scsi);
34472f5224aeSachartre 
34482f5224aeSachartre 	PR0("Performing VD_OP_RESET");
34492f5224aeSachartre 
34502f5224aeSachartre 	if (request->nbytes != 0) {
34512f5224aeSachartre 		PR0("VD_OP_RESET:  Expected nbytes = 0, got %lu",
34522f5224aeSachartre 		    request->nbytes);
34532f5224aeSachartre 		return (EINVAL);
34542f5224aeSachartre 	}
34552f5224aeSachartre 
34562f5224aeSachartre 	request->status = vd_scsi_reset(vd);
34572f5224aeSachartre 
34582f5224aeSachartre 	return (0);
34592f5224aeSachartre }
34602f5224aeSachartre 
34612f5224aeSachartre static int
34622f5224aeSachartre vd_get_capacity(vd_task_t *task)
34632f5224aeSachartre {
34642f5224aeSachartre 	int rv;
34652f5224aeSachartre 	size_t nbytes;
34662f5224aeSachartre 	vd_t *vd = task->vd;
34672f5224aeSachartre 	vd_dring_payload_t *request = task->request;
34682f5224aeSachartre 	vd_capacity_t vd_cap = { 0 };
34692f5224aeSachartre 
34702f5224aeSachartre 	ASSERT(request->operation == VD_OP_GET_CAPACITY);
34712f5224aeSachartre 	ASSERT(vd->scsi);
34722f5224aeSachartre 
34732f5224aeSachartre 	PR0("Performing VD_OP_GET_CAPACITY");
34742f5224aeSachartre 
34752f5224aeSachartre 	nbytes = request->nbytes;
34762f5224aeSachartre 
34772f5224aeSachartre 	if (nbytes != RNDSIZE(vd_capacity_t)) {
34782f5224aeSachartre 		PR0("VD_OP_GET_CAPACITY:  Expected nbytes = %lu, got %lu",
34792f5224aeSachartre 		    RNDSIZE(vd_capacity_t), nbytes);
34802f5224aeSachartre 		return (EINVAL);
34812f5224aeSachartre 	}
34822f5224aeSachartre 
34832f5224aeSachartre 	if (vd->vdisk_size == VD_SIZE_UNKNOWN) {
34842f5224aeSachartre 		if (vd_setup_mediainfo(vd) != 0)
34852f5224aeSachartre 			ASSERT(vd->vdisk_size == VD_SIZE_UNKNOWN);
34862f5224aeSachartre 	}
34872f5224aeSachartre 
34882f5224aeSachartre 	ASSERT(vd->vdisk_size != 0);
34892f5224aeSachartre 
34902f5224aeSachartre 	request->status = 0;
34912f5224aeSachartre 
34922f5224aeSachartre 	vd_cap.vdisk_block_size = vd->vdisk_block_size;
34932f5224aeSachartre 	vd_cap.vdisk_size = vd->vdisk_size;
34942f5224aeSachartre 
34952f5224aeSachartre 	if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&vd_cap, 0, &nbytes,
34962f5224aeSachartre 	    request->cookie, request->ncookies, LDC_COPY_OUT)) != 0) {
34972f5224aeSachartre 		PR0("ldc_mem_copy() returned errno %d copying to client", rv);
34982f5224aeSachartre 		return (rv);
34992f5224aeSachartre 	}
35002f5224aeSachartre 
35012f5224aeSachartre 	return (0);
35022f5224aeSachartre }
35032f5224aeSachartre 
35042f5224aeSachartre static int
35052f5224aeSachartre vd_get_access(vd_task_t *task)
35062f5224aeSachartre {
35072f5224aeSachartre 	uint64_t access;
35082f5224aeSachartre 	int rv, rval = 0;
35092f5224aeSachartre 	size_t nbytes;
35102f5224aeSachartre 	vd_t *vd = task->vd;
35112f5224aeSachartre 	vd_dring_payload_t *request = task->request;
35122f5224aeSachartre 
35132f5224aeSachartre 	ASSERT(request->operation == VD_OP_GET_ACCESS);
35142f5224aeSachartre 	ASSERT(vd->scsi);
35152f5224aeSachartre 
35162f5224aeSachartre 	PR0("Performing VD_OP_GET_ACCESS");
35172f5224aeSachartre 
35182f5224aeSachartre 	nbytes = request->nbytes;
35192f5224aeSachartre 
35202f5224aeSachartre 	if (nbytes != sizeof (uint64_t)) {
35212f5224aeSachartre 		PR0("VD_OP_GET_ACCESS:  Expected nbytes = %lu, got %lu",
35222f5224aeSachartre 		    sizeof (uint64_t), nbytes);
35232f5224aeSachartre 		return (EINVAL);
35242f5224aeSachartre 	}
35252f5224aeSachartre 
35262f5224aeSachartre 	request->status = ldi_ioctl(vd->ldi_handle[request->slice], MHIOCSTATUS,
35272f5224aeSachartre 	    NULL, (vd->open_flags | FKIOCTL), kcred, &rval);
35282f5224aeSachartre 
35292f5224aeSachartre 	if (request->status != 0)
35302f5224aeSachartre 		return (0);
35312f5224aeSachartre 
35322f5224aeSachartre 	access = (rval == 0)? VD_ACCESS_ALLOWED : VD_ACCESS_DENIED;
35332f5224aeSachartre 
35342f5224aeSachartre 	if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&access, 0, &nbytes,
35352f5224aeSachartre 	    request->cookie, request->ncookies, LDC_COPY_OUT)) != 0) {
35362f5224aeSachartre 		PR0("ldc_mem_copy() returned errno %d copying to client", rv);
35372f5224aeSachartre 		return (rv);
35382f5224aeSachartre 	}
35392f5224aeSachartre 
35402f5224aeSachartre 	return (0);
35412f5224aeSachartre }
35422f5224aeSachartre 
35432f5224aeSachartre static int
35442f5224aeSachartre vd_set_access(vd_task_t *task)
35452f5224aeSachartre {
35462f5224aeSachartre 	uint64_t flags;
35472f5224aeSachartre 	int rv, rval;
35482f5224aeSachartre 	size_t nbytes;
35492f5224aeSachartre 	vd_t *vd = task->vd;
35502f5224aeSachartre 	vd_dring_payload_t *request = task->request;
35512f5224aeSachartre 
35522f5224aeSachartre 	ASSERT(request->operation == VD_OP_SET_ACCESS);
35532f5224aeSachartre 	ASSERT(vd->scsi);
35542f5224aeSachartre 
35552f5224aeSachartre 	nbytes = request->nbytes;
35562f5224aeSachartre 
35572f5224aeSachartre 	if (nbytes != sizeof (uint64_t)) {
35582f5224aeSachartre 		PR0("VD_OP_SET_ACCESS:  Expected nbytes = %lu, got %lu",
35592f5224aeSachartre 		    sizeof (uint64_t), nbytes);
35602f5224aeSachartre 		return (EINVAL);
35612f5224aeSachartre 	}
35622f5224aeSachartre 
35632f5224aeSachartre 	if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&flags, 0, &nbytes,
35642f5224aeSachartre 	    request->cookie, request->ncookies, LDC_COPY_IN)) != 0) {
35652f5224aeSachartre 		PR0("ldc_mem_copy() returned errno %d copying from client", rv);
35662f5224aeSachartre 		return (rv);
35672f5224aeSachartre 	}
35682f5224aeSachartre 
35692f5224aeSachartre 	if (flags == VD_ACCESS_SET_CLEAR) {
35702f5224aeSachartre 		PR0("Performing VD_OP_SET_ACCESS (CLEAR)");
35712f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
35722f5224aeSachartre 		    MHIOCRELEASE, NULL, (vd->open_flags | FKIOCTL), kcred,
35732f5224aeSachartre 		    &rval);
35742f5224aeSachartre 		if (request->status == 0)
35752f5224aeSachartre 			vd->ownership = B_FALSE;
35762f5224aeSachartre 		return (0);
35772f5224aeSachartre 	}
35782f5224aeSachartre 
35792f5224aeSachartre 	/*
35802f5224aeSachartre 	 * As per the VIO spec, the PREEMPT and PRESERVE flags are only valid
35812f5224aeSachartre 	 * when the EXCLUSIVE flag is set.
35822f5224aeSachartre 	 */
35832f5224aeSachartre 	if (!(flags & VD_ACCESS_SET_EXCLUSIVE)) {
35842f5224aeSachartre 		PR0("Invalid VD_OP_SET_ACCESS flags: 0x%lx", flags);
35852f5224aeSachartre 		request->status = EINVAL;
35862f5224aeSachartre 		return (0);
35872f5224aeSachartre 	}
35882f5224aeSachartre 
35892f5224aeSachartre 	switch (flags & (VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE)) {
35902f5224aeSachartre 
35912f5224aeSachartre 	case VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE:
35922f5224aeSachartre 		/*
35932f5224aeSachartre 		 * Flags EXCLUSIVE and PREEMPT and PRESERVE. We have to
35942f5224aeSachartre 		 * acquire exclusive access rights, preserve them and we
35952f5224aeSachartre 		 * can use preemption. So we can use the MHIOCTKNOWN ioctl.
35962f5224aeSachartre 		 */
35972f5224aeSachartre 		PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT|PRESERVE)");
35982f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
35992f5224aeSachartre 		    MHIOCTKOWN, NULL, (vd->open_flags | FKIOCTL), kcred, &rval);
36002f5224aeSachartre 		break;
36012f5224aeSachartre 
36022f5224aeSachartre 	case VD_ACCESS_SET_PRESERVE:
36032f5224aeSachartre 		/*
36042f5224aeSachartre 		 * Flags EXCLUSIVE and PRESERVE. We have to acquire exclusive
36052f5224aeSachartre 		 * access rights and preserve them, but not preempt any other
36062f5224aeSachartre 		 * host. So we need to use the MHIOCTKOWN ioctl to enable the
36072f5224aeSachartre 		 * "preserve" feature but we can not called it directly
36082f5224aeSachartre 		 * because it uses preemption. So before that, we use the
36092f5224aeSachartre 		 * MHIOCQRESERVE ioctl to ensure we can get exclusive rights
36102f5224aeSachartre 		 * without preempting anyone.
36112f5224aeSachartre 		 */
36122f5224aeSachartre 		PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PRESERVE)");
36132f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
36142f5224aeSachartre 		    MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
36152f5224aeSachartre 		    &rval);
36162f5224aeSachartre 		if (request->status != 0)
36172f5224aeSachartre 			break;
36182f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
36192f5224aeSachartre 		    MHIOCTKOWN, NULL, (vd->open_flags | FKIOCTL), kcred, &rval);
36202f5224aeSachartre 		break;
36212f5224aeSachartre 
36222f5224aeSachartre 	case VD_ACCESS_SET_PREEMPT:
36232f5224aeSachartre 		/*
36242f5224aeSachartre 		 * Flags EXCLUSIVE and PREEMPT. We have to acquire exclusive
36252f5224aeSachartre 		 * access rights and we can use preemption. So we try to do
36262f5224aeSachartre 		 * a SCSI reservation, if it fails we reset the disk to clear
36272f5224aeSachartre 		 * any reservation and we try to reserve again.
36282f5224aeSachartre 		 */
36292f5224aeSachartre 		PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT)");
36302f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
36312f5224aeSachartre 		    MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
36322f5224aeSachartre 		    &rval);
36332f5224aeSachartre 		if (request->status == 0)
36342f5224aeSachartre 			break;
36352f5224aeSachartre 
36362f5224aeSachartre 		/* reset the disk */
36372f5224aeSachartre 		(void) vd_scsi_reset(vd);
36382f5224aeSachartre 
36392f5224aeSachartre 		/* try again even if the reset has failed */
36402f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
36412f5224aeSachartre 		    MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
36422f5224aeSachartre 		    &rval);
36432f5224aeSachartre 		break;
36442f5224aeSachartre 
36452f5224aeSachartre 	case 0:
36462f5224aeSachartre 		/* Flag EXCLUSIVE only. Just issue a SCSI reservation */
36472f5224aeSachartre 		PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE)");
36482f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
36492f5224aeSachartre 		    MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
36502f5224aeSachartre 		    &rval);
36512f5224aeSachartre 		break;
36522f5224aeSachartre 	}
36532f5224aeSachartre 
36542f5224aeSachartre 	if (request->status == 0)
36552f5224aeSachartre 		vd->ownership = B_TRUE;
36562f5224aeSachartre 	else
36572f5224aeSachartre 		PR0("VD_OP_SET_ACCESS: error %d", request->status);
36582f5224aeSachartre 
36592f5224aeSachartre 	return (0);
36602f5224aeSachartre }
36612f5224aeSachartre 
36622f5224aeSachartre static void
36632f5224aeSachartre vd_reset_access(vd_t *vd)
36642f5224aeSachartre {
36652f5224aeSachartre 	int status, rval;
36662f5224aeSachartre 
36672f5224aeSachartre 	if (vd->file || !vd->ownership)
36682f5224aeSachartre 		return;
36692f5224aeSachartre 
36702f5224aeSachartre 	PR0("Releasing disk ownership");
36712f5224aeSachartre 	status = ldi_ioctl(vd->ldi_handle[0], MHIOCRELEASE, NULL,
36722f5224aeSachartre 	    (vd->open_flags | FKIOCTL), kcred, &rval);
36732f5224aeSachartre 
36742f5224aeSachartre 	/*
36752f5224aeSachartre 	 * An EACCES failure means that there is a reservation conflict,
36762f5224aeSachartre 	 * so we are not the owner of the disk anymore.
36772f5224aeSachartre 	 */
36782f5224aeSachartre 	if (status == 0 || status == EACCES) {
36792f5224aeSachartre 		vd->ownership = B_FALSE;
36802f5224aeSachartre 		return;
36812f5224aeSachartre 	}
36822f5224aeSachartre 
36832f5224aeSachartre 	PR0("Fail to release ownership, error %d", status);
36842f5224aeSachartre 
36852f5224aeSachartre 	/*
36862f5224aeSachartre 	 * We have failed to release the ownership, try to reset the disk
36872f5224aeSachartre 	 * to release reservations.
36882f5224aeSachartre 	 */
36892f5224aeSachartre 	PR0("Resetting disk");
36902f5224aeSachartre 	status = vd_scsi_reset(vd);
36912f5224aeSachartre 
36922f5224aeSachartre 	if (status != 0)
36932f5224aeSachartre 		PR0("Fail to reset disk, error %d", status);
36942f5224aeSachartre 
36952f5224aeSachartre 	/* whatever the result of the reset is, we try the release again */
36962f5224aeSachartre 	status = ldi_ioctl(vd->ldi_handle[0], MHIOCRELEASE, NULL,
36972f5224aeSachartre 	    (vd->open_flags | FKIOCTL), kcred, &rval);
36982f5224aeSachartre 
36992f5224aeSachartre 	if (status == 0 || status == EACCES) {
37002f5224aeSachartre 		vd->ownership = B_FALSE;
37012f5224aeSachartre 		return;
37022f5224aeSachartre 	}
37032f5224aeSachartre 
37042f5224aeSachartre 	PR0("Fail to release ownership, error %d", status);
37052f5224aeSachartre 
37062f5224aeSachartre 	/*
37072f5224aeSachartre 	 * At this point we have done our best to try to reset the
37082f5224aeSachartre 	 * access rights to the disk and we don't know if we still
37092f5224aeSachartre 	 * own a reservation and if any mechanism to preserve the
37102f5224aeSachartre 	 * ownership is still in place. The ultimate solution would
37112f5224aeSachartre 	 * be to reset the system but this is usually not what we
37122f5224aeSachartre 	 * want to happen.
37132f5224aeSachartre 	 */
37142f5224aeSachartre 
37152f5224aeSachartre 	if (vd_reset_access_failure == A_REBOOT) {
37162f5224aeSachartre 		cmn_err(CE_WARN, VD_RESET_ACCESS_FAILURE_MSG
37172f5224aeSachartre 		    ", rebooting the system", vd->device_path);
37182f5224aeSachartre 		(void) uadmin(A_SHUTDOWN, AD_BOOT, NULL);
37192f5224aeSachartre 	} else if (vd_reset_access_failure == A_DUMP) {
37202f5224aeSachartre 		panic(VD_RESET_ACCESS_FAILURE_MSG, vd->device_path);
37212f5224aeSachartre 	}
37222f5224aeSachartre 
37232f5224aeSachartre 	cmn_err(CE_WARN, VD_RESET_ACCESS_FAILURE_MSG, vd->device_path);
37242f5224aeSachartre }
37252f5224aeSachartre 
37261ae08745Sheppo /*
37271ae08745Sheppo  * Define the supported operations once the functions for performing them have
37281ae08745Sheppo  * been defined
37291ae08745Sheppo  */
37301ae08745Sheppo static const vds_operation_t	vds_operation[] = {
37313af08d82Slm66018 #define	X(_s)	#_s, _s
37323af08d82Slm66018 	{X(VD_OP_BREAD),	vd_start_bio,	vd_complete_bio},
37333af08d82Slm66018 	{X(VD_OP_BWRITE),	vd_start_bio,	vd_complete_bio},
37343af08d82Slm66018 	{X(VD_OP_FLUSH),	vd_ioctl,	NULL},
37353af08d82Slm66018 	{X(VD_OP_GET_WCE),	vd_ioctl,	NULL},
37363af08d82Slm66018 	{X(VD_OP_SET_WCE),	vd_ioctl,	NULL},
37373af08d82Slm66018 	{X(VD_OP_GET_VTOC),	vd_ioctl,	NULL},
37383af08d82Slm66018 	{X(VD_OP_SET_VTOC),	vd_ioctl,	NULL},
37393af08d82Slm66018 	{X(VD_OP_GET_DISKGEOM),	vd_ioctl,	NULL},
37403af08d82Slm66018 	{X(VD_OP_SET_DISKGEOM),	vd_ioctl,	NULL},
37413af08d82Slm66018 	{X(VD_OP_GET_EFI),	vd_ioctl,	NULL},
37423af08d82Slm66018 	{X(VD_OP_SET_EFI),	vd_ioctl,	NULL},
37433af08d82Slm66018 	{X(VD_OP_GET_DEVID),	vd_get_devid,	NULL},
37442f5224aeSachartre 	{X(VD_OP_SCSICMD),	vd_ioctl,	NULL},
37452f5224aeSachartre 	{X(VD_OP_RESET),	vd_reset,	NULL},
37462f5224aeSachartre 	{X(VD_OP_GET_CAPACITY),	vd_get_capacity, NULL},
37472f5224aeSachartre 	{X(VD_OP_SET_ACCESS),	vd_set_access,	NULL},
37482f5224aeSachartre 	{X(VD_OP_GET_ACCESS),	vd_get_access,	NULL},
37493af08d82Slm66018 #undef	X
37501ae08745Sheppo };
37511ae08745Sheppo 
37521ae08745Sheppo static const size_t	vds_noperations =
37531ae08745Sheppo 	(sizeof (vds_operation))/(sizeof (vds_operation[0]));
37541ae08745Sheppo 
37551ae08745Sheppo /*
3756d10e4ef2Snarayan  * Process a task specifying a client I/O request
3757205eeb1aSlm66018  *
3758205eeb1aSlm66018  * Parameters:
3759205eeb1aSlm66018  *	task 		- structure containing the request sent from client
3760205eeb1aSlm66018  *
3761205eeb1aSlm66018  * Return Value
3762205eeb1aSlm66018  *	0	- success
3763205eeb1aSlm66018  *	ENOTSUP	- Unknown/Unsupported VD_OP_XXX operation
3764205eeb1aSlm66018  *	EINVAL	- Invalid disk slice
3765205eeb1aSlm66018  *	!= 0	- some other non-zero return value from start function
37661ae08745Sheppo  */
37671ae08745Sheppo static int
3768205eeb1aSlm66018 vd_do_process_task(vd_task_t *task)
37691ae08745Sheppo {
3770205eeb1aSlm66018 	int			i;
3771d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
3772d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
37731ae08745Sheppo 
3774d10e4ef2Snarayan 	ASSERT(vd != NULL);
3775d10e4ef2Snarayan 	ASSERT(request != NULL);
37761ae08745Sheppo 
3777d10e4ef2Snarayan 	/* Find the requested operation */
3778205eeb1aSlm66018 	for (i = 0; i < vds_noperations; i++) {
3779205eeb1aSlm66018 		if (request->operation == vds_operation[i].operation) {
3780205eeb1aSlm66018 			/* all operations should have a start func */
3781205eeb1aSlm66018 			ASSERT(vds_operation[i].start != NULL);
3782205eeb1aSlm66018 
3783205eeb1aSlm66018 			task->completef = vds_operation[i].complete;
3784d10e4ef2Snarayan 			break;
3785205eeb1aSlm66018 		}
3786205eeb1aSlm66018 	}
378717cadca8Slm66018 
378817cadca8Slm66018 	/*
378917cadca8Slm66018 	 * We need to check that the requested operation is permitted
379017cadca8Slm66018 	 * for the particular client that sent it or that the loop above
379117cadca8Slm66018 	 * did not complete without finding the operation type (indicating
379217cadca8Slm66018 	 * that the requested operation is unknown/unimplemented)
379317cadca8Slm66018 	 */
379417cadca8Slm66018 	if ((VD_OP_SUPPORTED(vd->operations, request->operation) == B_FALSE) ||
379517cadca8Slm66018 	    (i == vds_noperations)) {
37963af08d82Slm66018 		PR0("Unsupported operation %u", request->operation);
379717cadca8Slm66018 		request->status = ENOTSUP;
379817cadca8Slm66018 		return (0);
37991ae08745Sheppo 	}
38001ae08745Sheppo 
38017636cb21Slm66018 	/* Range-check slice */
380287a7269eSachartre 	if (request->slice >= vd->nslices &&
3803bae9e67eSachartre 	    ((vd->vdisk_type != VD_DISK_TYPE_DISK && vd_slice_single_slice) ||
380487a7269eSachartre 	    request->slice != VD_SLICE_NONE)) {
38053af08d82Slm66018 		PR0("Invalid \"slice\" %u (max %u) for virtual disk",
38067636cb21Slm66018 		    request->slice, (vd->nslices - 1));
3807bae9e67eSachartre 		request->status = EINVAL;
3808bae9e67eSachartre 		return (0);
38097636cb21Slm66018 	}
38107636cb21Slm66018 
3811205eeb1aSlm66018 	/*
3812205eeb1aSlm66018 	 * Call the function pointer that starts the operation.
3813205eeb1aSlm66018 	 */
3814205eeb1aSlm66018 	return (vds_operation[i].start(task));
38151ae08745Sheppo }
38161ae08745Sheppo 
3817205eeb1aSlm66018 /*
3818205eeb1aSlm66018  * Description:
3819205eeb1aSlm66018  *	This function is called by both the in-band and descriptor ring
3820205eeb1aSlm66018  *	message processing functions paths to actually execute the task
3821205eeb1aSlm66018  *	requested by the vDisk client. It in turn calls its worker
3822205eeb1aSlm66018  *	function, vd_do_process_task(), to carry our the request.
3823205eeb1aSlm66018  *
3824205eeb1aSlm66018  *	Any transport errors (e.g. LDC errors, vDisk protocol errors) are
3825205eeb1aSlm66018  *	saved in the 'status' field of the task and are propagated back
3826205eeb1aSlm66018  *	up the call stack to trigger a NACK
3827205eeb1aSlm66018  *
3828205eeb1aSlm66018  *	Any request errors (e.g. ENOTTY from an ioctl) are saved in
3829205eeb1aSlm66018  *	the 'status' field of the request and result in an ACK being sent
3830205eeb1aSlm66018  *	by the completion handler.
3831205eeb1aSlm66018  *
3832205eeb1aSlm66018  * Parameters:
3833205eeb1aSlm66018  *	task 		- structure containing the request sent from client
3834205eeb1aSlm66018  *
3835205eeb1aSlm66018  * Return Value
3836205eeb1aSlm66018  *	0		- successful synchronous request.
3837205eeb1aSlm66018  *	!= 0		- transport error (e.g. LDC errors, vDisk protocol)
3838205eeb1aSlm66018  *	EINPROGRESS	- task will be finished in a completion handler
3839205eeb1aSlm66018  */
3840205eeb1aSlm66018 static int
3841205eeb1aSlm66018 vd_process_task(vd_task_t *task)
3842205eeb1aSlm66018 {
3843205eeb1aSlm66018 	vd_t	*vd = task->vd;
3844205eeb1aSlm66018 	int	status;
38451ae08745Sheppo 
3846205eeb1aSlm66018 	DTRACE_PROBE1(task__start, vd_task_t *, task);
38473af08d82Slm66018 
3848205eeb1aSlm66018 	task->status =  vd_do_process_task(task);
3849205eeb1aSlm66018 
3850205eeb1aSlm66018 	/*
3851205eeb1aSlm66018 	 * If the task processing function returned EINPROGRESS indicating
3852205eeb1aSlm66018 	 * that the task needs completing then schedule a taskq entry to
3853205eeb1aSlm66018 	 * finish it now.
3854205eeb1aSlm66018 	 *
3855205eeb1aSlm66018 	 * Otherwise the task processing function returned either zero
3856205eeb1aSlm66018 	 * indicating that the task was finished in the start function (and we
3857205eeb1aSlm66018 	 * don't need to wait in a completion function) or the start function
3858205eeb1aSlm66018 	 * returned an error - in both cases all that needs to happen is the
3859205eeb1aSlm66018 	 * notification to the vDisk client higher up the call stack.
3860205eeb1aSlm66018 	 * If the task was using a Descriptor Ring, we need to mark it as done
3861205eeb1aSlm66018 	 * at this stage.
3862205eeb1aSlm66018 	 */
3863205eeb1aSlm66018 	if (task->status == EINPROGRESS) {
3864d10e4ef2Snarayan 		/* Queue a task to complete the operation */
3865205eeb1aSlm66018 		(void) ddi_taskq_dispatch(vd->completionq, vd_complete,
3866d10e4ef2Snarayan 		    task, DDI_SLEEP);
3867d10e4ef2Snarayan 
3868f0ca1d9aSsb155480 	} else if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE_V1_0)) {
3869205eeb1aSlm66018 		/* Update the dring element if it's a dring client */
3870205eeb1aSlm66018 		status = vd_mark_elem_done(vd, task->index,
3871205eeb1aSlm66018 		    task->request->status, task->request->nbytes);
3872205eeb1aSlm66018 		if (status == ECONNRESET)
3873205eeb1aSlm66018 			vd_mark_in_reset(vd);
3874bbfa0259Sha137994 		else if (status == EACCES)
3875bbfa0259Sha137994 			vd_need_reset(vd, B_TRUE);
3876205eeb1aSlm66018 	}
3877205eeb1aSlm66018 
3878205eeb1aSlm66018 	return (task->status);
38791ae08745Sheppo }
38801ae08745Sheppo 
38811ae08745Sheppo /*
38820a55fbb7Slm66018  * Return true if the "type", "subtype", and "env" fields of the "tag" first
38830a55fbb7Slm66018  * argument match the corresponding remaining arguments; otherwise, return false
38841ae08745Sheppo  */
38850a55fbb7Slm66018 boolean_t
38861ae08745Sheppo vd_msgtype(vio_msg_tag_t *tag, int type, int subtype, int env)
38871ae08745Sheppo {
38881ae08745Sheppo 	return ((tag->vio_msgtype == type) &&
38891ae08745Sheppo 	    (tag->vio_subtype == subtype) &&
38900a55fbb7Slm66018 	    (tag->vio_subtype_env == env)) ? B_TRUE : B_FALSE;
38911ae08745Sheppo }
38921ae08745Sheppo 
38930a55fbb7Slm66018 /*
38940a55fbb7Slm66018  * Check whether the major/minor version specified in "ver_msg" is supported
38950a55fbb7Slm66018  * by this server.
38960a55fbb7Slm66018  */
38970a55fbb7Slm66018 static boolean_t
38980a55fbb7Slm66018 vds_supported_version(vio_ver_msg_t *ver_msg)
38990a55fbb7Slm66018 {
39000a55fbb7Slm66018 	for (int i = 0; i < vds_num_versions; i++) {
39010a55fbb7Slm66018 		ASSERT(vds_version[i].major > 0);
39020a55fbb7Slm66018 		ASSERT((i == 0) ||
39030a55fbb7Slm66018 		    (vds_version[i].major < vds_version[i-1].major));
39040a55fbb7Slm66018 
39050a55fbb7Slm66018 		/*
39060a55fbb7Slm66018 		 * If the major versions match, adjust the minor version, if
39070a55fbb7Slm66018 		 * necessary, down to the highest value supported by this
39080a55fbb7Slm66018 		 * server and return true so this message will get "ack"ed;
39090a55fbb7Slm66018 		 * the client should also support all minor versions lower
39100a55fbb7Slm66018 		 * than the value it sent
39110a55fbb7Slm66018 		 */
39120a55fbb7Slm66018 		if (ver_msg->ver_major == vds_version[i].major) {
39130a55fbb7Slm66018 			if (ver_msg->ver_minor > vds_version[i].minor) {
39140a55fbb7Slm66018 				PR0("Adjusting minor version from %u to %u",
39150a55fbb7Slm66018 				    ver_msg->ver_minor, vds_version[i].minor);
39160a55fbb7Slm66018 				ver_msg->ver_minor = vds_version[i].minor;
39170a55fbb7Slm66018 			}
39180a55fbb7Slm66018 			return (B_TRUE);
39190a55fbb7Slm66018 		}
39200a55fbb7Slm66018 
39210a55fbb7Slm66018 		/*
39220a55fbb7Slm66018 		 * If the message contains a higher major version number, set
39230a55fbb7Slm66018 		 * the message's major/minor versions to the current values
39240a55fbb7Slm66018 		 * and return false, so this message will get "nack"ed with
39250a55fbb7Slm66018 		 * these values, and the client will potentially try again
39260a55fbb7Slm66018 		 * with the same or a lower version
39270a55fbb7Slm66018 		 */
39280a55fbb7Slm66018 		if (ver_msg->ver_major > vds_version[i].major) {
39290a55fbb7Slm66018 			ver_msg->ver_major = vds_version[i].major;
39300a55fbb7Slm66018 			ver_msg->ver_minor = vds_version[i].minor;
39310a55fbb7Slm66018 			return (B_FALSE);
39320a55fbb7Slm66018 		}
39330a55fbb7Slm66018 
39340a55fbb7Slm66018 		/*
39350a55fbb7Slm66018 		 * Otherwise, the message's major version is less than the
39360a55fbb7Slm66018 		 * current major version, so continue the loop to the next
39370a55fbb7Slm66018 		 * (lower) supported version
39380a55fbb7Slm66018 		 */
39390a55fbb7Slm66018 	}
39400a55fbb7Slm66018 
39410a55fbb7Slm66018 	/*
39420a55fbb7Slm66018 	 * No common version was found; "ground" the version pair in the
39430a55fbb7Slm66018 	 * message to terminate negotiation
39440a55fbb7Slm66018 	 */
39450a55fbb7Slm66018 	ver_msg->ver_major = 0;
39460a55fbb7Slm66018 	ver_msg->ver_minor = 0;
39470a55fbb7Slm66018 	return (B_FALSE);
39480a55fbb7Slm66018 }
39490a55fbb7Slm66018 
39500a55fbb7Slm66018 /*
39510a55fbb7Slm66018  * Process a version message from a client.  vds expects to receive version
39520a55fbb7Slm66018  * messages from clients seeking service, but never issues version messages
39530a55fbb7Slm66018  * itself; therefore, vds can ACK or NACK client version messages, but does
39540a55fbb7Slm66018  * not expect to receive version-message ACKs or NACKs (and will treat such
39550a55fbb7Slm66018  * messages as invalid).
39560a55fbb7Slm66018  */
39571ae08745Sheppo static int
39580a55fbb7Slm66018 vd_process_ver_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
39591ae08745Sheppo {
39601ae08745Sheppo 	vio_ver_msg_t	*ver_msg = (vio_ver_msg_t *)msg;
39611ae08745Sheppo 
39621ae08745Sheppo 
39631ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
39641ae08745Sheppo 
39651ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
39661ae08745Sheppo 	    VIO_VER_INFO)) {
39671ae08745Sheppo 		return (ENOMSG);	/* not a version message */
39681ae08745Sheppo 	}
39691ae08745Sheppo 
39701ae08745Sheppo 	if (msglen != sizeof (*ver_msg)) {
39713af08d82Slm66018 		PR0("Expected %lu-byte version message; "
39721ae08745Sheppo 		    "received %lu bytes", sizeof (*ver_msg), msglen);
39731ae08745Sheppo 		return (EBADMSG);
39741ae08745Sheppo 	}
39751ae08745Sheppo 
39761ae08745Sheppo 	if (ver_msg->dev_class != VDEV_DISK) {
39773af08d82Slm66018 		PR0("Expected device class %u (disk); received %u",
39781ae08745Sheppo 		    VDEV_DISK, ver_msg->dev_class);
39791ae08745Sheppo 		return (EBADMSG);
39801ae08745Sheppo 	}
39811ae08745Sheppo 
39820a55fbb7Slm66018 	/*
39830a55fbb7Slm66018 	 * We're talking to the expected kind of client; set our device class
39840a55fbb7Slm66018 	 * for "ack/nack" back to the client
39850a55fbb7Slm66018 	 */
39861ae08745Sheppo 	ver_msg->dev_class = VDEV_DISK_SERVER;
39870a55fbb7Slm66018 
39880a55fbb7Slm66018 	/*
39890a55fbb7Slm66018 	 * Check whether the (valid) version message specifies a version
39900a55fbb7Slm66018 	 * supported by this server.  If the version is not supported, return
39910a55fbb7Slm66018 	 * EBADMSG so the message will get "nack"ed; vds_supported_version()
39920a55fbb7Slm66018 	 * will have updated the message with a supported version for the
39930a55fbb7Slm66018 	 * client to consider
39940a55fbb7Slm66018 	 */
39950a55fbb7Slm66018 	if (!vds_supported_version(ver_msg))
39960a55fbb7Slm66018 		return (EBADMSG);
39970a55fbb7Slm66018 
39980a55fbb7Slm66018 
39990a55fbb7Slm66018 	/*
40000a55fbb7Slm66018 	 * A version has been agreed upon; use the client's SID for
40010a55fbb7Slm66018 	 * communication on this channel now
40020a55fbb7Slm66018 	 */
40030a55fbb7Slm66018 	ASSERT(!(vd->initialized & VD_SID));
40040a55fbb7Slm66018 	vd->sid = ver_msg->tag.vio_sid;
40050a55fbb7Slm66018 	vd->initialized |= VD_SID;
40060a55fbb7Slm66018 
40070a55fbb7Slm66018 	/*
400817cadca8Slm66018 	 * Store the negotiated major and minor version values in the "vd" data
400917cadca8Slm66018 	 * structure so that we can check if certain operations are supported
401017cadca8Slm66018 	 * by the client.
40110a55fbb7Slm66018 	 */
401217cadca8Slm66018 	vd->version.major = ver_msg->ver_major;
401317cadca8Slm66018 	vd->version.minor = ver_msg->ver_minor;
40140a55fbb7Slm66018 
40150a55fbb7Slm66018 	PR0("Using major version %u, minor version %u",
40160a55fbb7Slm66018 	    ver_msg->ver_major, ver_msg->ver_minor);
40171ae08745Sheppo 	return (0);
40181ae08745Sheppo }
40191ae08745Sheppo 
402017cadca8Slm66018 static void
402117cadca8Slm66018 vd_set_exported_operations(vd_t *vd)
402217cadca8Slm66018 {
402317cadca8Slm66018 	vd->operations = 0;	/* clear field */
402417cadca8Slm66018 
402517cadca8Slm66018 	/*
402617cadca8Slm66018 	 * We need to check from the highest version supported to the
402717cadca8Slm66018 	 * lowest because versions with a higher minor number implicitly
402817cadca8Slm66018 	 * support versions with a lower minor number.
402917cadca8Slm66018 	 */
403017cadca8Slm66018 	if (vio_ver_is_supported(vd->version, 1, 1)) {
403117cadca8Slm66018 		ASSERT(vd->open_flags & FREAD);
403217cadca8Slm66018 		vd->operations |= VD_OP_MASK_READ;
403317cadca8Slm66018 
403417cadca8Slm66018 		if (vd->open_flags & FWRITE)
403517cadca8Slm66018 			vd->operations |= VD_OP_MASK_WRITE;
403617cadca8Slm66018 
40372f5224aeSachartre 		if (vd->scsi)
40382f5224aeSachartre 			vd->operations |= VD_OP_MASK_SCSI;
40392f5224aeSachartre 
404017cadca8Slm66018 		if (vd->file && vd_file_is_iso_image(vd)) {
404117cadca8Slm66018 			/*
404217cadca8Slm66018 			 * can't write to ISO images, make sure that write
404317cadca8Slm66018 			 * support is not set in case administrator did not
404417cadca8Slm66018 			 * use "options=ro" when doing an ldm add-vdsdev
404517cadca8Slm66018 			 */
404617cadca8Slm66018 			vd->operations &= ~VD_OP_MASK_WRITE;
404717cadca8Slm66018 		}
404817cadca8Slm66018 	} else if (vio_ver_is_supported(vd->version, 1, 0)) {
404917cadca8Slm66018 		vd->operations = VD_OP_MASK_READ | VD_OP_MASK_WRITE;
405017cadca8Slm66018 	}
405117cadca8Slm66018 
405217cadca8Slm66018 	/* we should have already agreed on a version */
405317cadca8Slm66018 	ASSERT(vd->operations != 0);
405417cadca8Slm66018 }
405517cadca8Slm66018 
40561ae08745Sheppo static int
40571ae08745Sheppo vd_process_attr_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
40581ae08745Sheppo {
40591ae08745Sheppo 	vd_attr_msg_t	*attr_msg = (vd_attr_msg_t *)msg;
40603c96341aSnarayan 	int		status, retry = 0;
40611ae08745Sheppo 
40621ae08745Sheppo 
40631ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
40641ae08745Sheppo 
40651ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
40661ae08745Sheppo 	    VIO_ATTR_INFO)) {
4067d10e4ef2Snarayan 		PR0("Message is not an attribute message");
4068d10e4ef2Snarayan 		return (ENOMSG);
40691ae08745Sheppo 	}
40701ae08745Sheppo 
40711ae08745Sheppo 	if (msglen != sizeof (*attr_msg)) {
40723af08d82Slm66018 		PR0("Expected %lu-byte attribute message; "
40731ae08745Sheppo 		    "received %lu bytes", sizeof (*attr_msg), msglen);
40741ae08745Sheppo 		return (EBADMSG);
40751ae08745Sheppo 	}
40761ae08745Sheppo 
40771ae08745Sheppo 	if (attr_msg->max_xfer_sz == 0) {
40783af08d82Slm66018 		PR0("Received maximum transfer size of 0 from client");
40791ae08745Sheppo 		return (EBADMSG);
40801ae08745Sheppo 	}
40811ae08745Sheppo 
40821ae08745Sheppo 	if ((attr_msg->xfer_mode != VIO_DESC_MODE) &&
4083f0ca1d9aSsb155480 	    (attr_msg->xfer_mode != VIO_DRING_MODE_V1_0)) {
40843af08d82Slm66018 		PR0("Client requested unsupported transfer mode");
40851ae08745Sheppo 		return (EBADMSG);
40861ae08745Sheppo 	}
40871ae08745Sheppo 
40883c96341aSnarayan 	/*
40893c96341aSnarayan 	 * check if the underlying disk is ready, if not try accessing
40903c96341aSnarayan 	 * the device again. Open the vdisk device and extract info
40913c96341aSnarayan 	 * about it, as this is needed to respond to the attr info msg
40923c96341aSnarayan 	 */
40933c96341aSnarayan 	if ((vd->initialized & VD_DISK_READY) == 0) {
40943c96341aSnarayan 		PR0("Retry setting up disk (%s)", vd->device_path);
40953c96341aSnarayan 		do {
40963c96341aSnarayan 			status = vd_setup_vd(vd);
40973c96341aSnarayan 			if (status != EAGAIN || ++retry > vds_dev_retries)
40983c96341aSnarayan 				break;
40993c96341aSnarayan 
41003c96341aSnarayan 			/* incremental delay */
41013c96341aSnarayan 			delay(drv_usectohz(vds_dev_delay));
41023c96341aSnarayan 
41033c96341aSnarayan 			/* if vdisk is no longer enabled - return error */
41043c96341aSnarayan 			if (!vd_enabled(vd))
41053c96341aSnarayan 				return (ENXIO);
41063c96341aSnarayan 
41073c96341aSnarayan 		} while (status == EAGAIN);
41083c96341aSnarayan 
41093c96341aSnarayan 		if (status)
41103c96341aSnarayan 			return (ENXIO);
41113c96341aSnarayan 
41123c96341aSnarayan 		vd->initialized |= VD_DISK_READY;
41133c96341aSnarayan 		ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
41148fce2fd6Sachartre 		PR0("vdisk_type = %s, volume = %s, file = %s, nslices = %u",
41153c96341aSnarayan 		    ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
41168fce2fd6Sachartre 		    (vd->volume ? "yes" : "no"),
41173c96341aSnarayan 		    (vd->file ? "yes" : "no"),
41183c96341aSnarayan 		    vd->nslices);
41193c96341aSnarayan 	}
41203c96341aSnarayan 
41211ae08745Sheppo 	/* Success:  valid message and transfer mode */
41221ae08745Sheppo 	vd->xfer_mode = attr_msg->xfer_mode;
41233af08d82Slm66018 
41241ae08745Sheppo 	if (vd->xfer_mode == VIO_DESC_MODE) {
41253af08d82Slm66018 
41261ae08745Sheppo 		/*
41271ae08745Sheppo 		 * The vd_dring_inband_msg_t contains one cookie; need room
41281ae08745Sheppo 		 * for up to n-1 more cookies, where "n" is the number of full
41291ae08745Sheppo 		 * pages plus possibly one partial page required to cover
41301ae08745Sheppo 		 * "max_xfer_sz".  Add room for one more cookie if
41311ae08745Sheppo 		 * "max_xfer_sz" isn't an integral multiple of the page size.
41321ae08745Sheppo 		 * Must first get the maximum transfer size in bytes.
41331ae08745Sheppo 		 */
41341ae08745Sheppo 		size_t	max_xfer_bytes = attr_msg->vdisk_block_size ?
41351ae08745Sheppo 		    attr_msg->vdisk_block_size*attr_msg->max_xfer_sz :
41361ae08745Sheppo 		    attr_msg->max_xfer_sz;
41371ae08745Sheppo 		size_t	max_inband_msglen =
41381ae08745Sheppo 		    sizeof (vd_dring_inband_msg_t) +
41391ae08745Sheppo 		    ((max_xfer_bytes/PAGESIZE +
41401ae08745Sheppo 		    ((max_xfer_bytes % PAGESIZE) ? 1 : 0))*
41411ae08745Sheppo 		    (sizeof (ldc_mem_cookie_t)));
41421ae08745Sheppo 
41431ae08745Sheppo 		/*
41441ae08745Sheppo 		 * Set the maximum expected message length to
41451ae08745Sheppo 		 * accommodate in-band-descriptor messages with all
41461ae08745Sheppo 		 * their cookies
41471ae08745Sheppo 		 */
41481ae08745Sheppo 		vd->max_msglen = MAX(vd->max_msglen, max_inband_msglen);
4149d10e4ef2Snarayan 
4150d10e4ef2Snarayan 		/*
4151d10e4ef2Snarayan 		 * Initialize the data structure for processing in-band I/O
4152d10e4ef2Snarayan 		 * request descriptors
4153d10e4ef2Snarayan 		 */
4154d10e4ef2Snarayan 		vd->inband_task.vd	= vd;
41553af08d82Slm66018 		vd->inband_task.msg	= kmem_alloc(vd->max_msglen, KM_SLEEP);
4156d10e4ef2Snarayan 		vd->inband_task.index	= 0;
4157d10e4ef2Snarayan 		vd->inband_task.type	= VD_FINAL_RANGE_TASK;	/* range == 1 */
41581ae08745Sheppo 	}
41591ae08745Sheppo 
4160e1ebb9ecSlm66018 	/* Return the device's block size and max transfer size to the client */
41612f5224aeSachartre 	attr_msg->vdisk_block_size	= vd->vdisk_block_size;
4162e1ebb9ecSlm66018 	attr_msg->max_xfer_sz		= vd->max_xfer_sz;
4163e1ebb9ecSlm66018 
41641ae08745Sheppo 	attr_msg->vdisk_size = vd->vdisk_size;
4165bae9e67eSachartre 	attr_msg->vdisk_type = (vd_slice_single_slice)? vd->vdisk_type :
4166bae9e67eSachartre 	    VD_DISK_TYPE_DISK;
416717cadca8Slm66018 	attr_msg->vdisk_media = vd->vdisk_media;
416817cadca8Slm66018 
416917cadca8Slm66018 	/* Discover and save the list of supported VD_OP_XXX operations */
417017cadca8Slm66018 	vd_set_exported_operations(vd);
417117cadca8Slm66018 	attr_msg->operations = vd->operations;
417217cadca8Slm66018 
41731ae08745Sheppo 	PR0("%s", VD_CLIENT(vd));
41743af08d82Slm66018 
41753af08d82Slm66018 	ASSERT(vd->dring_task == NULL);
41763af08d82Slm66018 
41771ae08745Sheppo 	return (0);
41781ae08745Sheppo }
41791ae08745Sheppo 
41801ae08745Sheppo static int
41811ae08745Sheppo vd_process_dring_reg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
41821ae08745Sheppo {
41831ae08745Sheppo 	int			status;
41841ae08745Sheppo 	size_t			expected;
41851ae08745Sheppo 	ldc_mem_info_t		dring_minfo;
4186bbfa0259Sha137994 	uint8_t			mtype;
41871ae08745Sheppo 	vio_dring_reg_msg_t	*reg_msg = (vio_dring_reg_msg_t *)msg;
41881ae08745Sheppo 
41891ae08745Sheppo 
41901ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
41911ae08745Sheppo 
41921ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
41931ae08745Sheppo 	    VIO_DRING_REG)) {
4194d10e4ef2Snarayan 		PR0("Message is not a register-dring message");
4195d10e4ef2Snarayan 		return (ENOMSG);
41961ae08745Sheppo 	}
41971ae08745Sheppo 
41981ae08745Sheppo 	if (msglen < sizeof (*reg_msg)) {
41993af08d82Slm66018 		PR0("Expected at least %lu-byte register-dring message; "
42001ae08745Sheppo 		    "received %lu bytes", sizeof (*reg_msg), msglen);
42011ae08745Sheppo 		return (EBADMSG);
42021ae08745Sheppo 	}
42031ae08745Sheppo 
42041ae08745Sheppo 	expected = sizeof (*reg_msg) +
42051ae08745Sheppo 	    (reg_msg->ncookies - 1)*(sizeof (reg_msg->cookie[0]));
42061ae08745Sheppo 	if (msglen != expected) {
42073af08d82Slm66018 		PR0("Expected %lu-byte register-dring message; "
42081ae08745Sheppo 		    "received %lu bytes", expected, msglen);
42091ae08745Sheppo 		return (EBADMSG);
42101ae08745Sheppo 	}
42111ae08745Sheppo 
42121ae08745Sheppo 	if (vd->initialized & VD_DRING) {
42133af08d82Slm66018 		PR0("A dring was previously registered; only support one");
42141ae08745Sheppo 		return (EBADMSG);
42151ae08745Sheppo 	}
42161ae08745Sheppo 
4217d10e4ef2Snarayan 	if (reg_msg->num_descriptors > INT32_MAX) {
42183af08d82Slm66018 		PR0("reg_msg->num_descriptors = %u; must be <= %u (%s)",
4219d10e4ef2Snarayan 		    reg_msg->ncookies, INT32_MAX, STRINGIZE(INT32_MAX));
4220d10e4ef2Snarayan 		return (EBADMSG);
4221d10e4ef2Snarayan 	}
4222d10e4ef2Snarayan 
42231ae08745Sheppo 	if (reg_msg->ncookies != 1) {
42241ae08745Sheppo 		/*
42251ae08745Sheppo 		 * In addition to fixing the assertion in the success case
42261ae08745Sheppo 		 * below, supporting drings which require more than one
42271ae08745Sheppo 		 * "cookie" requires increasing the value of vd->max_msglen
42281ae08745Sheppo 		 * somewhere in the code path prior to receiving the message
42291ae08745Sheppo 		 * which results in calling this function.  Note that without
42301ae08745Sheppo 		 * making this change, the larger message size required to
42311ae08745Sheppo 		 * accommodate multiple cookies cannot be successfully
42321ae08745Sheppo 		 * received, so this function will not even get called.
42331ae08745Sheppo 		 * Gracefully accommodating more dring cookies might
42341ae08745Sheppo 		 * reasonably demand exchanging an additional attribute or
42351ae08745Sheppo 		 * making a minor protocol adjustment
42361ae08745Sheppo 		 */
42373af08d82Slm66018 		PR0("reg_msg->ncookies = %u != 1", reg_msg->ncookies);
42381ae08745Sheppo 		return (EBADMSG);
42391ae08745Sheppo 	}
42401ae08745Sheppo 
4241bbfa0259Sha137994 	if (vd_direct_mapped_drings)
4242bbfa0259Sha137994 		mtype = LDC_DIRECT_MAP;
4243bbfa0259Sha137994 	else
4244bbfa0259Sha137994 		mtype = LDC_SHADOW_MAP;
4245bbfa0259Sha137994 
42461ae08745Sheppo 	status = ldc_mem_dring_map(vd->ldc_handle, reg_msg->cookie,
42471ae08745Sheppo 	    reg_msg->ncookies, reg_msg->num_descriptors,
4248bbfa0259Sha137994 	    reg_msg->descriptor_size, mtype, &vd->dring_handle);
42491ae08745Sheppo 	if (status != 0) {
42503af08d82Slm66018 		PR0("ldc_mem_dring_map() returned errno %d", status);
42511ae08745Sheppo 		return (status);
42521ae08745Sheppo 	}
42531ae08745Sheppo 
42541ae08745Sheppo 	/*
42551ae08745Sheppo 	 * To remove the need for this assertion, must call
42561ae08745Sheppo 	 * ldc_mem_dring_nextcookie() successfully ncookies-1 times after a
42571ae08745Sheppo 	 * successful call to ldc_mem_dring_map()
42581ae08745Sheppo 	 */
42591ae08745Sheppo 	ASSERT(reg_msg->ncookies == 1);
42601ae08745Sheppo 
42611ae08745Sheppo 	if ((status =
42621ae08745Sheppo 	    ldc_mem_dring_info(vd->dring_handle, &dring_minfo)) != 0) {
42633af08d82Slm66018 		PR0("ldc_mem_dring_info() returned errno %d", status);
42641ae08745Sheppo 		if ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0)
42653af08d82Slm66018 			PR0("ldc_mem_dring_unmap() returned errno %d", status);
42661ae08745Sheppo 		return (status);
42671ae08745Sheppo 	}
42681ae08745Sheppo 
42691ae08745Sheppo 	if (dring_minfo.vaddr == NULL) {
42703af08d82Slm66018 		PR0("Descriptor ring virtual address is NULL");
42710a55fbb7Slm66018 		return (ENXIO);
42721ae08745Sheppo 	}
42731ae08745Sheppo 
42741ae08745Sheppo 
4275d10e4ef2Snarayan 	/* Initialize for valid message and mapped dring */
42761ae08745Sheppo 	PR1("descriptor size = %u, dring length = %u",
42771ae08745Sheppo 	    vd->descriptor_size, vd->dring_len);
42781ae08745Sheppo 	vd->initialized |= VD_DRING;
42791ae08745Sheppo 	vd->dring_ident = 1;	/* "There Can Be Only One" */
42801ae08745Sheppo 	vd->dring = dring_minfo.vaddr;
42811ae08745Sheppo 	vd->descriptor_size = reg_msg->descriptor_size;
42821ae08745Sheppo 	vd->dring_len = reg_msg->num_descriptors;
4283bbfa0259Sha137994 	vd->dring_mtype = dring_minfo.mtype;
42841ae08745Sheppo 	reg_msg->dring_ident = vd->dring_ident;
4285d10e4ef2Snarayan 
4286d10e4ef2Snarayan 	/*
4287d10e4ef2Snarayan 	 * Allocate and initialize a "shadow" array of data structures for
4288d10e4ef2Snarayan 	 * tasks to process I/O requests in dring elements
4289d10e4ef2Snarayan 	 */
4290d10e4ef2Snarayan 	vd->dring_task =
4291d10e4ef2Snarayan 	    kmem_zalloc((sizeof (*vd->dring_task)) * vd->dring_len, KM_SLEEP);
4292d10e4ef2Snarayan 	for (int i = 0; i < vd->dring_len; i++) {
4293d10e4ef2Snarayan 		vd->dring_task[i].vd		= vd;
4294d10e4ef2Snarayan 		vd->dring_task[i].index		= i;
4295d10e4ef2Snarayan 		vd->dring_task[i].request	= &VD_DRING_ELEM(i)->payload;
42964bac2208Snarayan 
42974bac2208Snarayan 		status = ldc_mem_alloc_handle(vd->ldc_handle,
42984bac2208Snarayan 		    &(vd->dring_task[i].mhdl));
42994bac2208Snarayan 		if (status) {
43003af08d82Slm66018 			PR0("ldc_mem_alloc_handle() returned err %d ", status);
43014bac2208Snarayan 			return (ENXIO);
43024bac2208Snarayan 		}
43033af08d82Slm66018 
43043af08d82Slm66018 		vd->dring_task[i].msg = kmem_alloc(vd->max_msglen, KM_SLEEP);
4305d10e4ef2Snarayan 	}
4306d10e4ef2Snarayan 
43071ae08745Sheppo 	return (0);
43081ae08745Sheppo }
43091ae08745Sheppo 
43101ae08745Sheppo static int
43111ae08745Sheppo vd_process_dring_unreg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
43121ae08745Sheppo {
43131ae08745Sheppo 	vio_dring_unreg_msg_t	*unreg_msg = (vio_dring_unreg_msg_t *)msg;
43141ae08745Sheppo 
43151ae08745Sheppo 
43161ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
43171ae08745Sheppo 
43181ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
43191ae08745Sheppo 	    VIO_DRING_UNREG)) {
4320d10e4ef2Snarayan 		PR0("Message is not an unregister-dring message");
4321d10e4ef2Snarayan 		return (ENOMSG);
43221ae08745Sheppo 	}
43231ae08745Sheppo 
43241ae08745Sheppo 	if (msglen != sizeof (*unreg_msg)) {
43253af08d82Slm66018 		PR0("Expected %lu-byte unregister-dring message; "
43261ae08745Sheppo 		    "received %lu bytes", sizeof (*unreg_msg), msglen);
43271ae08745Sheppo 		return (EBADMSG);
43281ae08745Sheppo 	}
43291ae08745Sheppo 
43301ae08745Sheppo 	if (unreg_msg->dring_ident != vd->dring_ident) {
43313af08d82Slm66018 		PR0("Expected dring ident %lu; received %lu",
43321ae08745Sheppo 		    vd->dring_ident, unreg_msg->dring_ident);
43331ae08745Sheppo 		return (EBADMSG);
43341ae08745Sheppo 	}
43351ae08745Sheppo 
43361ae08745Sheppo 	return (0);
43371ae08745Sheppo }
43381ae08745Sheppo 
43391ae08745Sheppo static int
43401ae08745Sheppo process_rdx_msg(vio_msg_t *msg, size_t msglen)
43411ae08745Sheppo {
43421ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
43431ae08745Sheppo 
4344d10e4ef2Snarayan 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX)) {
4345d10e4ef2Snarayan 		PR0("Message is not an RDX message");
4346d10e4ef2Snarayan 		return (ENOMSG);
4347d10e4ef2Snarayan 	}
43481ae08745Sheppo 
43491ae08745Sheppo 	if (msglen != sizeof (vio_rdx_msg_t)) {
43503af08d82Slm66018 		PR0("Expected %lu-byte RDX message; received %lu bytes",
43511ae08745Sheppo 		    sizeof (vio_rdx_msg_t), msglen);
43521ae08745Sheppo 		return (EBADMSG);
43531ae08745Sheppo 	}
43541ae08745Sheppo 
4355d10e4ef2Snarayan 	PR0("Valid RDX message");
43561ae08745Sheppo 	return (0);
43571ae08745Sheppo }
43581ae08745Sheppo 
43591ae08745Sheppo static int
43601ae08745Sheppo vd_check_seq_num(vd_t *vd, uint64_t seq_num)
43611ae08745Sheppo {
43621ae08745Sheppo 	if ((vd->initialized & VD_SEQ_NUM) && (seq_num != vd->seq_num + 1)) {
43633af08d82Slm66018 		PR0("Received seq_num %lu; expected %lu",
43641ae08745Sheppo 		    seq_num, (vd->seq_num + 1));
43653af08d82Slm66018 		PR0("initiating soft reset");
4366d10e4ef2Snarayan 		vd_need_reset(vd, B_FALSE);
43671ae08745Sheppo 		return (1);
43681ae08745Sheppo 	}
43691ae08745Sheppo 
43701ae08745Sheppo 	vd->seq_num = seq_num;
43711ae08745Sheppo 	vd->initialized |= VD_SEQ_NUM;	/* superfluous after first time... */
43721ae08745Sheppo 	return (0);
43731ae08745Sheppo }
43741ae08745Sheppo 
43751ae08745Sheppo /*
43761ae08745Sheppo  * Return the expected size of an inband-descriptor message with all the
43771ae08745Sheppo  * cookies it claims to include
43781ae08745Sheppo  */
43791ae08745Sheppo static size_t
43801ae08745Sheppo expected_inband_size(vd_dring_inband_msg_t *msg)
43811ae08745Sheppo {
43821ae08745Sheppo 	return ((sizeof (*msg)) +
43831ae08745Sheppo 	    (msg->payload.ncookies - 1)*(sizeof (msg->payload.cookie[0])));
43841ae08745Sheppo }
43851ae08745Sheppo 
43861ae08745Sheppo /*
43871ae08745Sheppo  * Process an in-band descriptor message:  used with clients like OBP, with
43881ae08745Sheppo  * which vds exchanges descriptors within VIO message payloads, rather than
43891ae08745Sheppo  * operating on them within a descriptor ring
43901ae08745Sheppo  */
43911ae08745Sheppo static int
43923af08d82Slm66018 vd_process_desc_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
43931ae08745Sheppo {
43941ae08745Sheppo 	size_t			expected;
43951ae08745Sheppo 	vd_dring_inband_msg_t	*desc_msg = (vd_dring_inband_msg_t *)msg;
43961ae08745Sheppo 
43971ae08745Sheppo 
43981ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
43991ae08745Sheppo 
44001ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
4401d10e4ef2Snarayan 	    VIO_DESC_DATA)) {
4402d10e4ef2Snarayan 		PR1("Message is not an in-band-descriptor message");
4403d10e4ef2Snarayan 		return (ENOMSG);
4404d10e4ef2Snarayan 	}
44051ae08745Sheppo 
44061ae08745Sheppo 	if (msglen < sizeof (*desc_msg)) {
44073af08d82Slm66018 		PR0("Expected at least %lu-byte descriptor message; "
44081ae08745Sheppo 		    "received %lu bytes", sizeof (*desc_msg), msglen);
44091ae08745Sheppo 		return (EBADMSG);
44101ae08745Sheppo 	}
44111ae08745Sheppo 
44121ae08745Sheppo 	if (msglen != (expected = expected_inband_size(desc_msg))) {
44133af08d82Slm66018 		PR0("Expected %lu-byte descriptor message; "
44141ae08745Sheppo 		    "received %lu bytes", expected, msglen);
44151ae08745Sheppo 		return (EBADMSG);
44161ae08745Sheppo 	}
44171ae08745Sheppo 
4418d10e4ef2Snarayan 	if (vd_check_seq_num(vd, desc_msg->hdr.seq_num) != 0)
44191ae08745Sheppo 		return (EBADMSG);
44201ae08745Sheppo 
4421d10e4ef2Snarayan 	/*
4422d10e4ef2Snarayan 	 * Valid message:  Set up the in-band descriptor task and process the
4423d10e4ef2Snarayan 	 * request.  Arrange to acknowledge the client's message, unless an
4424d10e4ef2Snarayan 	 * error processing the descriptor task results in setting
4425d10e4ef2Snarayan 	 * VIO_SUBTYPE_NACK
4426d10e4ef2Snarayan 	 */
4427d10e4ef2Snarayan 	PR1("Valid in-band-descriptor message");
4428d10e4ef2Snarayan 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
44293af08d82Slm66018 
44303af08d82Slm66018 	ASSERT(vd->inband_task.msg != NULL);
44313af08d82Slm66018 
44323af08d82Slm66018 	bcopy(msg, vd->inband_task.msg, msglen);
4433d10e4ef2Snarayan 	vd->inband_task.msglen	= msglen;
44343af08d82Slm66018 
44353af08d82Slm66018 	/*
44363af08d82Slm66018 	 * The task request is now the payload of the message
44373af08d82Slm66018 	 * that was just copied into the body of the task.
44383af08d82Slm66018 	 */
44393af08d82Slm66018 	desc_msg = (vd_dring_inband_msg_t *)vd->inband_task.msg;
4440d10e4ef2Snarayan 	vd->inband_task.request	= &desc_msg->payload;
44413af08d82Slm66018 
4442d10e4ef2Snarayan 	return (vd_process_task(&vd->inband_task));
44431ae08745Sheppo }
44441ae08745Sheppo 
44451ae08745Sheppo static int
4446d10e4ef2Snarayan vd_process_element(vd_t *vd, vd_task_type_t type, uint32_t idx,
44473af08d82Slm66018     vio_msg_t *msg, size_t msglen)
44481ae08745Sheppo {
44491ae08745Sheppo 	int			status;
4450d10e4ef2Snarayan 	boolean_t		ready;
4451bbfa0259Sha137994 	on_trap_data_t		otd;
4452d10e4ef2Snarayan 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
44531ae08745Sheppo 
4454d10e4ef2Snarayan 	/* Accept the updated dring element */
4455bbfa0259Sha137994 	if ((status = VIO_DRING_ACQUIRE(&otd, vd->dring_mtype,
4456bbfa0259Sha137994 	    vd->dring_handle, idx, idx)) != 0) {
44571ae08745Sheppo 		return (status);
44581ae08745Sheppo 	}
4459d10e4ef2Snarayan 	ready = (elem->hdr.dstate == VIO_DESC_READY);
4460d10e4ef2Snarayan 	if (ready) {
4461d10e4ef2Snarayan 		elem->hdr.dstate = VIO_DESC_ACCEPTED;
4462d10e4ef2Snarayan 	} else {
44633af08d82Slm66018 		PR0("descriptor %u not ready", idx);
4464d10e4ef2Snarayan 		VD_DUMP_DRING_ELEM(elem);
4465d10e4ef2Snarayan 	}
4466bbfa0259Sha137994 	if ((status = VIO_DRING_RELEASE(vd->dring_mtype,
4467bbfa0259Sha137994 	    vd->dring_handle, idx, idx)) != 0) {
4468bbfa0259Sha137994 		PR0("VIO_DRING_RELEASE() returned errno %d", status);
44691ae08745Sheppo 		return (status);
44701ae08745Sheppo 	}
4471d10e4ef2Snarayan 	if (!ready)
4472d10e4ef2Snarayan 		return (EBUSY);
44731ae08745Sheppo 
44741ae08745Sheppo 
4475d10e4ef2Snarayan 	/* Initialize a task and process the accepted element */
4476d10e4ef2Snarayan 	PR1("Processing dring element %u", idx);
4477d10e4ef2Snarayan 	vd->dring_task[idx].type	= type;
44783af08d82Slm66018 
44793af08d82Slm66018 	/* duplicate msg buf for cookies etc. */
44803af08d82Slm66018 	bcopy(msg, vd->dring_task[idx].msg, msglen);
44813af08d82Slm66018 
4482d10e4ef2Snarayan 	vd->dring_task[idx].msglen	= msglen;
4483205eeb1aSlm66018 	return (vd_process_task(&vd->dring_task[idx]));
44841ae08745Sheppo }
44851ae08745Sheppo 
44861ae08745Sheppo static int
4487d10e4ef2Snarayan vd_process_element_range(vd_t *vd, int start, int end,
44883af08d82Slm66018     vio_msg_t *msg, size_t msglen)
4489d10e4ef2Snarayan {
4490d10e4ef2Snarayan 	int		i, n, nelem, status = 0;
4491d10e4ef2Snarayan 	boolean_t	inprogress = B_FALSE;
4492d10e4ef2Snarayan 	vd_task_type_t	type;
4493d10e4ef2Snarayan 
4494d10e4ef2Snarayan 
4495d10e4ef2Snarayan 	ASSERT(start >= 0);
4496d10e4ef2Snarayan 	ASSERT(end >= 0);
4497d10e4ef2Snarayan 
4498d10e4ef2Snarayan 	/*
4499d10e4ef2Snarayan 	 * Arrange to acknowledge the client's message, unless an error
4500d10e4ef2Snarayan 	 * processing one of the dring elements results in setting
4501d10e4ef2Snarayan 	 * VIO_SUBTYPE_NACK
4502d10e4ef2Snarayan 	 */
4503d10e4ef2Snarayan 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
4504d10e4ef2Snarayan 
4505d10e4ef2Snarayan 	/*
4506d10e4ef2Snarayan 	 * Process the dring elements in the range
4507d10e4ef2Snarayan 	 */
4508d10e4ef2Snarayan 	nelem = ((end < start) ? end + vd->dring_len : end) - start + 1;
4509d10e4ef2Snarayan 	for (i = start, n = nelem; n > 0; i = (i + 1) % vd->dring_len, n--) {
4510d10e4ef2Snarayan 		((vio_dring_msg_t *)msg)->end_idx = i;
4511d10e4ef2Snarayan 		type = (n == 1) ? VD_FINAL_RANGE_TASK : VD_NONFINAL_RANGE_TASK;
45123af08d82Slm66018 		status = vd_process_element(vd, type, i, msg, msglen);
4513d10e4ef2Snarayan 		if (status == EINPROGRESS)
4514d10e4ef2Snarayan 			inprogress = B_TRUE;
4515d10e4ef2Snarayan 		else if (status != 0)
4516d10e4ef2Snarayan 			break;
4517d10e4ef2Snarayan 	}
4518d10e4ef2Snarayan 
4519d10e4ef2Snarayan 	/*
4520d10e4ef2Snarayan 	 * If some, but not all, operations of a multi-element range are in
4521d10e4ef2Snarayan 	 * progress, wait for other operations to complete before returning
4522d10e4ef2Snarayan 	 * (which will result in "ack" or "nack" of the message).  Note that
4523d10e4ef2Snarayan 	 * all outstanding operations will need to complete, not just the ones
4524d10e4ef2Snarayan 	 * corresponding to the current range of dring elements; howevever, as
4525d10e4ef2Snarayan 	 * this situation is an error case, performance is less critical.
4526d10e4ef2Snarayan 	 */
4527d10e4ef2Snarayan 	if ((nelem > 1) && (status != EINPROGRESS) && inprogress)
4528d10e4ef2Snarayan 		ddi_taskq_wait(vd->completionq);
4529d10e4ef2Snarayan 
4530d10e4ef2Snarayan 	return (status);
4531d10e4ef2Snarayan }
4532d10e4ef2Snarayan 
4533d10e4ef2Snarayan static int
45343af08d82Slm66018 vd_process_dring_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
45351ae08745Sheppo {
45361ae08745Sheppo 	vio_dring_msg_t	*dring_msg = (vio_dring_msg_t *)msg;
45371ae08745Sheppo 
45381ae08745Sheppo 
45391ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
45401ae08745Sheppo 
45411ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
45421ae08745Sheppo 	    VIO_DRING_DATA)) {
4543d10e4ef2Snarayan 		PR1("Message is not a dring-data message");
4544d10e4ef2Snarayan 		return (ENOMSG);
45451ae08745Sheppo 	}
45461ae08745Sheppo 
45471ae08745Sheppo 	if (msglen != sizeof (*dring_msg)) {
45483af08d82Slm66018 		PR0("Expected %lu-byte dring message; received %lu bytes",
45491ae08745Sheppo 		    sizeof (*dring_msg), msglen);
45501ae08745Sheppo 		return (EBADMSG);
45511ae08745Sheppo 	}
45521ae08745Sheppo 
4553d10e4ef2Snarayan 	if (vd_check_seq_num(vd, dring_msg->seq_num) != 0)
45541ae08745Sheppo 		return (EBADMSG);
45551ae08745Sheppo 
45561ae08745Sheppo 	if (dring_msg->dring_ident != vd->dring_ident) {
45573af08d82Slm66018 		PR0("Expected dring ident %lu; received ident %lu",
45581ae08745Sheppo 		    vd->dring_ident, dring_msg->dring_ident);
45591ae08745Sheppo 		return (EBADMSG);
45601ae08745Sheppo 	}
45611ae08745Sheppo 
4562d10e4ef2Snarayan 	if (dring_msg->start_idx >= vd->dring_len) {
45633af08d82Slm66018 		PR0("\"start_idx\" = %u; must be less than %u",
4564d10e4ef2Snarayan 		    dring_msg->start_idx, vd->dring_len);
4565d10e4ef2Snarayan 		return (EBADMSG);
4566d10e4ef2Snarayan 	}
45671ae08745Sheppo 
4568d10e4ef2Snarayan 	if ((dring_msg->end_idx < 0) ||
4569d10e4ef2Snarayan 	    (dring_msg->end_idx >= vd->dring_len)) {
45703af08d82Slm66018 		PR0("\"end_idx\" = %u; must be >= 0 and less than %u",
4571d10e4ef2Snarayan 		    dring_msg->end_idx, vd->dring_len);
4572d10e4ef2Snarayan 		return (EBADMSG);
4573d10e4ef2Snarayan 	}
4574d10e4ef2Snarayan 
4575d10e4ef2Snarayan 	/* Valid message; process range of updated dring elements */
4576d10e4ef2Snarayan 	PR1("Processing descriptor range, start = %u, end = %u",
4577d10e4ef2Snarayan 	    dring_msg->start_idx, dring_msg->end_idx);
4578d10e4ef2Snarayan 	return (vd_process_element_range(vd, dring_msg->start_idx,
45793af08d82Slm66018 	    dring_msg->end_idx, msg, msglen));
45801ae08745Sheppo }
45811ae08745Sheppo 
45821ae08745Sheppo static int
45831ae08745Sheppo recv_msg(ldc_handle_t ldc_handle, void *msg, size_t *nbytes)
45841ae08745Sheppo {
45851ae08745Sheppo 	int	retry, status;
45861ae08745Sheppo 	size_t	size = *nbytes;
45871ae08745Sheppo 
45881ae08745Sheppo 
45891ae08745Sheppo 	for (retry = 0, status = ETIMEDOUT;
45901ae08745Sheppo 	    retry < vds_ldc_retries && status == ETIMEDOUT;
45911ae08745Sheppo 	    retry++) {
45921ae08745Sheppo 		PR1("ldc_read() attempt %d", (retry + 1));
45931ae08745Sheppo 		*nbytes = size;
45941ae08745Sheppo 		status = ldc_read(ldc_handle, msg, nbytes);
45951ae08745Sheppo 	}
45961ae08745Sheppo 
45973af08d82Slm66018 	if (status) {
45983af08d82Slm66018 		PR0("ldc_read() returned errno %d", status);
45993af08d82Slm66018 		if (status != ECONNRESET)
46003af08d82Slm66018 			return (ENOMSG);
46011ae08745Sheppo 		return (status);
46021ae08745Sheppo 	} else if (*nbytes == 0) {
46031ae08745Sheppo 		PR1("ldc_read() returned 0 and no message read");
46041ae08745Sheppo 		return (ENOMSG);
46051ae08745Sheppo 	}
46061ae08745Sheppo 
46071ae08745Sheppo 	PR1("RCVD %lu-byte message", *nbytes);
46081ae08745Sheppo 	return (0);
46091ae08745Sheppo }
46101ae08745Sheppo 
46111ae08745Sheppo static int
46123af08d82Slm66018 vd_do_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
46131ae08745Sheppo {
46141ae08745Sheppo 	int		status;
46151ae08745Sheppo 
46161ae08745Sheppo 
46171ae08745Sheppo 	PR1("Processing (%x/%x/%x) message", msg->tag.vio_msgtype,
46181ae08745Sheppo 	    msg->tag.vio_subtype, msg->tag.vio_subtype_env);
46193af08d82Slm66018 #ifdef	DEBUG
46203af08d82Slm66018 	vd_decode_tag(msg);
46213af08d82Slm66018 #endif
46221ae08745Sheppo 
46231ae08745Sheppo 	/*
46241ae08745Sheppo 	 * Validate session ID up front, since it applies to all messages
46251ae08745Sheppo 	 * once set
46261ae08745Sheppo 	 */
46271ae08745Sheppo 	if ((msg->tag.vio_sid != vd->sid) && (vd->initialized & VD_SID)) {
46283af08d82Slm66018 		PR0("Expected SID %u, received %u", vd->sid,
46291ae08745Sheppo 		    msg->tag.vio_sid);
46301ae08745Sheppo 		return (EBADMSG);
46311ae08745Sheppo 	}
46321ae08745Sheppo 
46333af08d82Slm66018 	PR1("\tWhile in state %d (%s)", vd->state, vd_decode_state(vd->state));
46341ae08745Sheppo 
46351ae08745Sheppo 	/*
46361ae08745Sheppo 	 * Process the received message based on connection state
46371ae08745Sheppo 	 */
46381ae08745Sheppo 	switch (vd->state) {
46391ae08745Sheppo 	case VD_STATE_INIT:	/* expect version message */
46400a55fbb7Slm66018 		if ((status = vd_process_ver_msg(vd, msg, msglen)) != 0)
46411ae08745Sheppo 			return (status);
46421ae08745Sheppo 
46431ae08745Sheppo 		/* Version negotiated, move to that state */
46441ae08745Sheppo 		vd->state = VD_STATE_VER;
46451ae08745Sheppo 		return (0);
46461ae08745Sheppo 
46471ae08745Sheppo 	case VD_STATE_VER:	/* expect attribute message */
46481ae08745Sheppo 		if ((status = vd_process_attr_msg(vd, msg, msglen)) != 0)
46491ae08745Sheppo 			return (status);
46501ae08745Sheppo 
46511ae08745Sheppo 		/* Attributes exchanged, move to that state */
46521ae08745Sheppo 		vd->state = VD_STATE_ATTR;
46531ae08745Sheppo 		return (0);
46541ae08745Sheppo 
46551ae08745Sheppo 	case VD_STATE_ATTR:
46561ae08745Sheppo 		switch (vd->xfer_mode) {
46571ae08745Sheppo 		case VIO_DESC_MODE:	/* expect RDX message */
46581ae08745Sheppo 			if ((status = process_rdx_msg(msg, msglen)) != 0)
46591ae08745Sheppo 				return (status);
46601ae08745Sheppo 
46611ae08745Sheppo 			/* Ready to receive in-band descriptors */
46621ae08745Sheppo 			vd->state = VD_STATE_DATA;
46631ae08745Sheppo 			return (0);
46641ae08745Sheppo 
4665f0ca1d9aSsb155480 		case VIO_DRING_MODE_V1_0:  /* expect register-dring message */
46661ae08745Sheppo 			if ((status =
46671ae08745Sheppo 			    vd_process_dring_reg_msg(vd, msg, msglen)) != 0)
46681ae08745Sheppo 				return (status);
46691ae08745Sheppo 
46701ae08745Sheppo 			/* One dring negotiated, move to that state */
46711ae08745Sheppo 			vd->state = VD_STATE_DRING;
46721ae08745Sheppo 			return (0);
46731ae08745Sheppo 
46741ae08745Sheppo 		default:
46751ae08745Sheppo 			ASSERT("Unsupported transfer mode");
46763af08d82Slm66018 			PR0("Unsupported transfer mode");
46771ae08745Sheppo 			return (ENOTSUP);
46781ae08745Sheppo 		}
46791ae08745Sheppo 
46801ae08745Sheppo 	case VD_STATE_DRING:	/* expect RDX, register-dring, or unreg-dring */
46811ae08745Sheppo 		if ((status = process_rdx_msg(msg, msglen)) == 0) {
46821ae08745Sheppo 			/* Ready to receive data */
46831ae08745Sheppo 			vd->state = VD_STATE_DATA;
46841ae08745Sheppo 			return (0);
46851ae08745Sheppo 		} else if (status != ENOMSG) {
46861ae08745Sheppo 			return (status);
46871ae08745Sheppo 		}
46881ae08745Sheppo 
46891ae08745Sheppo 
46901ae08745Sheppo 		/*
46911ae08745Sheppo 		 * If another register-dring message is received, stay in
46921ae08745Sheppo 		 * dring state in case the client sends RDX; although the
46931ae08745Sheppo 		 * protocol allows multiple drings, this server does not
46941ae08745Sheppo 		 * support using more than one
46951ae08745Sheppo 		 */
46961ae08745Sheppo 		if ((status =
46971ae08745Sheppo 		    vd_process_dring_reg_msg(vd, msg, msglen)) != ENOMSG)
46981ae08745Sheppo 			return (status);
46991ae08745Sheppo 
47001ae08745Sheppo 		/*
47011ae08745Sheppo 		 * Acknowledge an unregister-dring message, but reset the
47021ae08745Sheppo 		 * connection anyway:  Although the protocol allows
47031ae08745Sheppo 		 * unregistering drings, this server cannot serve a vdisk
47041ae08745Sheppo 		 * without its only dring
47051ae08745Sheppo 		 */
47061ae08745Sheppo 		status = vd_process_dring_unreg_msg(vd, msg, msglen);
47071ae08745Sheppo 		return ((status == 0) ? ENOTSUP : status);
47081ae08745Sheppo 
47091ae08745Sheppo 	case VD_STATE_DATA:
47101ae08745Sheppo 		switch (vd->xfer_mode) {
47111ae08745Sheppo 		case VIO_DESC_MODE:	/* expect in-band-descriptor message */
47123af08d82Slm66018 			return (vd_process_desc_msg(vd, msg, msglen));
47131ae08745Sheppo 
4714f0ca1d9aSsb155480 		case VIO_DRING_MODE_V1_0: /* expect dring-data or unreg-dring */
47151ae08745Sheppo 			/*
47161ae08745Sheppo 			 * Typically expect dring-data messages, so handle
47171ae08745Sheppo 			 * them first
47181ae08745Sheppo 			 */
47191ae08745Sheppo 			if ((status = vd_process_dring_msg(vd, msg,
47203af08d82Slm66018 			    msglen)) != ENOMSG)
47211ae08745Sheppo 				return (status);
47221ae08745Sheppo 
47231ae08745Sheppo 			/*
47241ae08745Sheppo 			 * Acknowledge an unregister-dring message, but reset
47251ae08745Sheppo 			 * the connection anyway:  Although the protocol
47261ae08745Sheppo 			 * allows unregistering drings, this server cannot
47271ae08745Sheppo 			 * serve a vdisk without its only dring
47281ae08745Sheppo 			 */
47291ae08745Sheppo 			status = vd_process_dring_unreg_msg(vd, msg, msglen);
47301ae08745Sheppo 			return ((status == 0) ? ENOTSUP : status);
47311ae08745Sheppo 
47321ae08745Sheppo 		default:
47331ae08745Sheppo 			ASSERT("Unsupported transfer mode");
47343af08d82Slm66018 			PR0("Unsupported transfer mode");
47351ae08745Sheppo 			return (ENOTSUP);
47361ae08745Sheppo 		}
47371ae08745Sheppo 
47381ae08745Sheppo 	default:
47391ae08745Sheppo 		ASSERT("Invalid client connection state");
47403af08d82Slm66018 		PR0("Invalid client connection state");
47411ae08745Sheppo 		return (ENOTSUP);
47421ae08745Sheppo 	}
47431ae08745Sheppo }
47441ae08745Sheppo 
4745d10e4ef2Snarayan static int
47463af08d82Slm66018 vd_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
47471ae08745Sheppo {
47481ae08745Sheppo 	int		status;
47491ae08745Sheppo 	boolean_t	reset_ldc = B_FALSE;
4750205eeb1aSlm66018 	vd_task_t	task;
47511ae08745Sheppo 
47521ae08745Sheppo 	/*
47531ae08745Sheppo 	 * Check that the message is at least big enough for a "tag", so that
47541ae08745Sheppo 	 * message processing can proceed based on tag-specified message type
47551ae08745Sheppo 	 */
47561ae08745Sheppo 	if (msglen < sizeof (vio_msg_tag_t)) {
47573af08d82Slm66018 		PR0("Received short (%lu-byte) message", msglen);
47581ae08745Sheppo 		/* Can't "nack" short message, so drop the big hammer */
47593af08d82Slm66018 		PR0("initiating full reset");
4760d10e4ef2Snarayan 		vd_need_reset(vd, B_TRUE);
4761d10e4ef2Snarayan 		return (EBADMSG);
47621ae08745Sheppo 	}
47631ae08745Sheppo 
47641ae08745Sheppo 	/*
47651ae08745Sheppo 	 * Process the message
47661ae08745Sheppo 	 */
47673af08d82Slm66018 	switch (status = vd_do_process_msg(vd, msg, msglen)) {
47681ae08745Sheppo 	case 0:
47691ae08745Sheppo 		/* "ack" valid, successfully-processed messages */
47701ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
47711ae08745Sheppo 		break;
47721ae08745Sheppo 
4773d10e4ef2Snarayan 	case EINPROGRESS:
4774d10e4ef2Snarayan 		/* The completion handler will "ack" or "nack" the message */
4775d10e4ef2Snarayan 		return (EINPROGRESS);
47761ae08745Sheppo 	case ENOMSG:
47773af08d82Slm66018 		PR0("Received unexpected message");
47781ae08745Sheppo 		_NOTE(FALLTHROUGH);
47791ae08745Sheppo 	case EBADMSG:
47801ae08745Sheppo 	case ENOTSUP:
4781205eeb1aSlm66018 		/* "transport" error will cause NACK of invalid messages */
47821ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
47831ae08745Sheppo 		break;
47841ae08745Sheppo 
47851ae08745Sheppo 	default:
4786205eeb1aSlm66018 		/* "transport" error will cause NACK of invalid messages */
47871ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
47881ae08745Sheppo 		/* An LDC error probably occurred, so try resetting it */
47891ae08745Sheppo 		reset_ldc = B_TRUE;
47901ae08745Sheppo 		break;
47911ae08745Sheppo 	}
47921ae08745Sheppo 
47933af08d82Slm66018 	PR1("\tResulting in state %d (%s)", vd->state,
47943af08d82Slm66018 	    vd_decode_state(vd->state));
47953af08d82Slm66018 
4796205eeb1aSlm66018 	/* populate the task so we can dispatch it on the taskq */
4797205eeb1aSlm66018 	task.vd = vd;
4798205eeb1aSlm66018 	task.msg = msg;
4799205eeb1aSlm66018 	task.msglen = msglen;
4800205eeb1aSlm66018 
4801205eeb1aSlm66018 	/*
4802205eeb1aSlm66018 	 * Queue a task to send the notification that the operation completed.
4803205eeb1aSlm66018 	 * We need to ensure that requests are responded to in the correct
4804205eeb1aSlm66018 	 * order and since the taskq is processed serially this ordering
4805205eeb1aSlm66018 	 * is maintained.
4806205eeb1aSlm66018 	 */
4807205eeb1aSlm66018 	(void) ddi_taskq_dispatch(vd->completionq, vd_serial_notify,
4808205eeb1aSlm66018 	    &task, DDI_SLEEP);
4809205eeb1aSlm66018 
4810205eeb1aSlm66018 	/*
4811205eeb1aSlm66018 	 * To ensure handshake negotiations do not happen out of order, such
4812205eeb1aSlm66018 	 * requests that come through this path should not be done in parallel
4813205eeb1aSlm66018 	 * so we need to wait here until the response is sent to the client.
4814205eeb1aSlm66018 	 */
4815205eeb1aSlm66018 	ddi_taskq_wait(vd->completionq);
48161ae08745Sheppo 
4817d10e4ef2Snarayan 	/* Arrange to reset the connection for nack'ed or failed messages */
48183af08d82Slm66018 	if ((status != 0) || reset_ldc) {
48193af08d82Slm66018 		PR0("initiating %s reset",
48203af08d82Slm66018 		    (reset_ldc) ? "full" : "soft");
4821d10e4ef2Snarayan 		vd_need_reset(vd, reset_ldc);
48223af08d82Slm66018 	}
4823d10e4ef2Snarayan 
4824d10e4ef2Snarayan 	return (status);
4825d10e4ef2Snarayan }
4826d10e4ef2Snarayan 
4827d10e4ef2Snarayan static boolean_t
4828d10e4ef2Snarayan vd_enabled(vd_t *vd)
4829d10e4ef2Snarayan {
4830d10e4ef2Snarayan 	boolean_t	enabled;
4831d10e4ef2Snarayan 
4832d10e4ef2Snarayan 	mutex_enter(&vd->lock);
4833d10e4ef2Snarayan 	enabled = vd->enabled;
4834d10e4ef2Snarayan 	mutex_exit(&vd->lock);
4835d10e4ef2Snarayan 	return (enabled);
48361ae08745Sheppo }
48371ae08745Sheppo 
48381ae08745Sheppo static void
48390a55fbb7Slm66018 vd_recv_msg(void *arg)
48401ae08745Sheppo {
48411ae08745Sheppo 	vd_t	*vd = (vd_t *)arg;
48423af08d82Slm66018 	int	rv = 0, status = 0;
48431ae08745Sheppo 
48441ae08745Sheppo 	ASSERT(vd != NULL);
48453af08d82Slm66018 
4846d10e4ef2Snarayan 	PR2("New task to receive incoming message(s)");
48473af08d82Slm66018 
48483af08d82Slm66018 
4849d10e4ef2Snarayan 	while (vd_enabled(vd) && status == 0) {
4850d10e4ef2Snarayan 		size_t		msglen, msgsize;
48513af08d82Slm66018 		ldc_status_t	lstatus;
4852d10e4ef2Snarayan 
48530a55fbb7Slm66018 		/*
4854d10e4ef2Snarayan 		 * Receive and process a message
48550a55fbb7Slm66018 		 */
4856d10e4ef2Snarayan 		vd_reset_if_needed(vd);	/* can change vd->max_msglen */
48573af08d82Slm66018 
48583af08d82Slm66018 		/*
48593af08d82Slm66018 		 * check if channel is UP - else break out of loop
48603af08d82Slm66018 		 */
48613af08d82Slm66018 		status = ldc_status(vd->ldc_handle, &lstatus);
48623af08d82Slm66018 		if (lstatus != LDC_UP) {
48633af08d82Slm66018 			PR0("channel not up (status=%d), exiting recv loop\n",
48643af08d82Slm66018 			    lstatus);
48653af08d82Slm66018 			break;
48663af08d82Slm66018 		}
48673af08d82Slm66018 
48683af08d82Slm66018 		ASSERT(vd->max_msglen != 0);
48693af08d82Slm66018 
4870d10e4ef2Snarayan 		msgsize = vd->max_msglen; /* stable copy for alloc/free */
48713af08d82Slm66018 		msglen	= msgsize;	  /* actual len after recv_msg() */
48723af08d82Slm66018 
48733af08d82Slm66018 		status = recv_msg(vd->ldc_handle, vd->vio_msgp, &msglen);
48743af08d82Slm66018 		switch (status) {
48753af08d82Slm66018 		case 0:
48763af08d82Slm66018 			rv = vd_process_msg(vd, (vio_msg_t *)vd->vio_msgp,
48773af08d82Slm66018 			    msglen);
48783af08d82Slm66018 			/* check if max_msglen changed */
48793af08d82Slm66018 			if (msgsize != vd->max_msglen) {
48803af08d82Slm66018 				PR0("max_msglen changed 0x%lx to 0x%lx bytes\n",
48813af08d82Slm66018 				    msgsize, vd->max_msglen);
48823af08d82Slm66018 				kmem_free(vd->vio_msgp, msgsize);
48833af08d82Slm66018 				vd->vio_msgp =
48843af08d82Slm66018 				    kmem_alloc(vd->max_msglen, KM_SLEEP);
48853af08d82Slm66018 			}
48863af08d82Slm66018 			if (rv == EINPROGRESS)
48873af08d82Slm66018 				continue;
48883af08d82Slm66018 			break;
48893af08d82Slm66018 
48903af08d82Slm66018 		case ENOMSG:
48913af08d82Slm66018 			break;
48923af08d82Slm66018 
48933af08d82Slm66018 		case ECONNRESET:
48943af08d82Slm66018 			PR0("initiating soft reset (ECONNRESET)\n");
48953af08d82Slm66018 			vd_need_reset(vd, B_FALSE);
48963af08d82Slm66018 			status = 0;
48973af08d82Slm66018 			break;
48983af08d82Slm66018 
48993af08d82Slm66018 		default:
4900d10e4ef2Snarayan 			/* Probably an LDC failure; arrange to reset it */
49013af08d82Slm66018 			PR0("initiating full reset (status=0x%x)", status);
4902d10e4ef2Snarayan 			vd_need_reset(vd, B_TRUE);
49033af08d82Slm66018 			break;
49040a55fbb7Slm66018 		}
49051ae08745Sheppo 	}
49063af08d82Slm66018 
4907d10e4ef2Snarayan 	PR2("Task finished");
49080a55fbb7Slm66018 }
49090a55fbb7Slm66018 
49100a55fbb7Slm66018 static uint_t
49111ae08745Sheppo vd_handle_ldc_events(uint64_t event, caddr_t arg)
49121ae08745Sheppo {
49131ae08745Sheppo 	vd_t	*vd = (vd_t *)(void *)arg;
49143af08d82Slm66018 	int	status;
49151ae08745Sheppo 
49161ae08745Sheppo 	ASSERT(vd != NULL);
4917d10e4ef2Snarayan 
4918d10e4ef2Snarayan 	if (!vd_enabled(vd))
4919d10e4ef2Snarayan 		return (LDC_SUCCESS);
4920d10e4ef2Snarayan 
49213af08d82Slm66018 	if (event & LDC_EVT_DOWN) {
492234683adeSsg70180 		PR0("LDC_EVT_DOWN: LDC channel went down");
49233af08d82Slm66018 
49243af08d82Slm66018 		vd_need_reset(vd, B_TRUE);
49253af08d82Slm66018 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
49263af08d82Slm66018 		    DDI_SLEEP);
49273af08d82Slm66018 		if (status == DDI_FAILURE) {
49283af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
49293af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
49303af08d82Slm66018 		}
49313af08d82Slm66018 	}
49323af08d82Slm66018 
4933d10e4ef2Snarayan 	if (event & LDC_EVT_RESET) {
49343af08d82Slm66018 		PR0("LDC_EVT_RESET: LDC channel was reset");
49353af08d82Slm66018 
49363af08d82Slm66018 		if (vd->state != VD_STATE_INIT) {
49373af08d82Slm66018 			PR0("scheduling full reset");
49383af08d82Slm66018 			vd_need_reset(vd, B_FALSE);
49393af08d82Slm66018 			status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
49403af08d82Slm66018 			    vd, 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 		} else {
49473af08d82Slm66018 			PR0("channel already reset, ignoring...\n");
49483af08d82Slm66018 			PR0("doing ldc up...\n");
49493af08d82Slm66018 			(void) ldc_up(vd->ldc_handle);
49503af08d82Slm66018 		}
49513af08d82Slm66018 
4952d10e4ef2Snarayan 		return (LDC_SUCCESS);
4953d10e4ef2Snarayan 	}
4954d10e4ef2Snarayan 
4955d10e4ef2Snarayan 	if (event & LDC_EVT_UP) {
49563af08d82Slm66018 		PR0("EVT_UP: LDC is up\nResetting client connection state");
49573af08d82Slm66018 		PR0("initiating soft reset");
4958d10e4ef2Snarayan 		vd_need_reset(vd, B_FALSE);
49593af08d82Slm66018 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
49603af08d82Slm66018 		    vd, DDI_SLEEP);
49613af08d82Slm66018 		if (status == DDI_FAILURE) {
49623af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
49633af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
49643af08d82Slm66018 			return (LDC_SUCCESS);
49653af08d82Slm66018 		}
4966d10e4ef2Snarayan 	}
4967d10e4ef2Snarayan 
4968d10e4ef2Snarayan 	if (event & LDC_EVT_READ) {
4969d10e4ef2Snarayan 		int	status;
4970d10e4ef2Snarayan 
4971d10e4ef2Snarayan 		PR1("New data available");
4972d10e4ef2Snarayan 		/* Queue a task to receive the new data */
4973d10e4ef2Snarayan 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
4974d10e4ef2Snarayan 		    DDI_SLEEP);
49753af08d82Slm66018 
49763af08d82Slm66018 		if (status == DDI_FAILURE) {
49773af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
49783af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
49793af08d82Slm66018 		}
4980d10e4ef2Snarayan 	}
4981d10e4ef2Snarayan 
4982d10e4ef2Snarayan 	return (LDC_SUCCESS);
49831ae08745Sheppo }
49841ae08745Sheppo 
49851ae08745Sheppo static uint_t
49861ae08745Sheppo vds_check_for_vd(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
49871ae08745Sheppo {
49881ae08745Sheppo 	_NOTE(ARGUNUSED(key, val))
49891ae08745Sheppo 	(*((uint_t *)arg))++;
49901ae08745Sheppo 	return (MH_WALK_TERMINATE);
49911ae08745Sheppo }
49921ae08745Sheppo 
49931ae08745Sheppo 
49941ae08745Sheppo static int
49951ae08745Sheppo vds_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
49961ae08745Sheppo {
49971ae08745Sheppo 	uint_t	vd_present = 0;
49981ae08745Sheppo 	minor_t	instance;
49991ae08745Sheppo 	vds_t	*vds;
50001ae08745Sheppo 
50011ae08745Sheppo 
50021ae08745Sheppo 	switch (cmd) {
50031ae08745Sheppo 	case DDI_DETACH:
50041ae08745Sheppo 		/* the real work happens below */
50051ae08745Sheppo 		break;
50061ae08745Sheppo 	case DDI_SUSPEND:
5007d10e4ef2Snarayan 		PR0("No action required for DDI_SUSPEND");
50081ae08745Sheppo 		return (DDI_SUCCESS);
50091ae08745Sheppo 	default:
50103af08d82Slm66018 		PR0("Unrecognized \"cmd\"");
50111ae08745Sheppo 		return (DDI_FAILURE);
50121ae08745Sheppo 	}
50131ae08745Sheppo 
50141ae08745Sheppo 	ASSERT(cmd == DDI_DETACH);
50151ae08745Sheppo 	instance = ddi_get_instance(dip);
50161ae08745Sheppo 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
50173af08d82Slm66018 		PR0("Could not get state for instance %u", instance);
50181ae08745Sheppo 		ddi_soft_state_free(vds_state, instance);
50191ae08745Sheppo 		return (DDI_FAILURE);
50201ae08745Sheppo 	}
50211ae08745Sheppo 
50221ae08745Sheppo 	/* Do no detach when serving any vdisks */
50231ae08745Sheppo 	mod_hash_walk(vds->vd_table, vds_check_for_vd, &vd_present);
50241ae08745Sheppo 	if (vd_present) {
50251ae08745Sheppo 		PR0("Not detaching because serving vdisks");
50261ae08745Sheppo 		return (DDI_FAILURE);
50271ae08745Sheppo 	}
50281ae08745Sheppo 
50291ae08745Sheppo 	PR0("Detaching");
5030445b4c2eSsb155480 	if (vds->initialized & VDS_MDEG) {
50311ae08745Sheppo 		(void) mdeg_unregister(vds->mdeg);
5032445b4c2eSsb155480 		kmem_free(vds->ispecp->specp, sizeof (vds_prop_template));
5033445b4c2eSsb155480 		kmem_free(vds->ispecp, sizeof (mdeg_node_spec_t));
5034445b4c2eSsb155480 		vds->ispecp = NULL;
5035445b4c2eSsb155480 		vds->mdeg = NULL;
5036445b4c2eSsb155480 	}
5037445b4c2eSsb155480 
50388fce2fd6Sachartre 	vds_driver_types_free(vds);
50398fce2fd6Sachartre 
50401ae08745Sheppo 	if (vds->initialized & VDS_LDI)
50411ae08745Sheppo 		(void) ldi_ident_release(vds->ldi_ident);
50421ae08745Sheppo 	mod_hash_destroy_hash(vds->vd_table);
50431ae08745Sheppo 	ddi_soft_state_free(vds_state, instance);
50441ae08745Sheppo 	return (DDI_SUCCESS);
50451ae08745Sheppo }
50461ae08745Sheppo 
504717cadca8Slm66018 /*
504817cadca8Slm66018  * Description:
504917cadca8Slm66018  *	This function checks to see if the file being used as a
505017cadca8Slm66018  *	virtual disk is an ISO image. An ISO image is a special
505117cadca8Slm66018  *	case which can be booted/installed from like a CD/DVD
505217cadca8Slm66018  *
505317cadca8Slm66018  * Parameters:
505417cadca8Slm66018  *	vd		- disk on which the operation is performed.
505517cadca8Slm66018  *
505617cadca8Slm66018  * Return Code:
505717cadca8Slm66018  *	B_TRUE		- The file is an ISO 9660 compliant image
505817cadca8Slm66018  *	B_FALSE		- just a regular disk image file
505917cadca8Slm66018  */
506017cadca8Slm66018 static boolean_t
506117cadca8Slm66018 vd_file_is_iso_image(vd_t *vd)
506217cadca8Slm66018 {
506317cadca8Slm66018 	char	iso_buf[ISO_SECTOR_SIZE];
506417cadca8Slm66018 	int	i, rv;
506517cadca8Slm66018 	uint_t	sec;
506617cadca8Slm66018 
506717cadca8Slm66018 	ASSERT(vd->file);
506817cadca8Slm66018 
506917cadca8Slm66018 	/*
507017cadca8Slm66018 	 * If we have already discovered and saved this info we can
507117cadca8Slm66018 	 * short-circuit the check and avoid reading the file.
507217cadca8Slm66018 	 */
507317cadca8Slm66018 	if (vd->vdisk_media == VD_MEDIA_DVD || vd->vdisk_media == VD_MEDIA_CD)
507417cadca8Slm66018 		return (B_TRUE);
507517cadca8Slm66018 
507617cadca8Slm66018 	/*
507717cadca8Slm66018 	 * We wish to read the sector that should contain the 2nd ISO volume
507817cadca8Slm66018 	 * descriptor. The second field in this descriptor is called the
507917cadca8Slm66018 	 * Standard Identifier and is set to CD001 for a CD-ROM compliant
508017cadca8Slm66018 	 * to the ISO 9660 standard.
508117cadca8Slm66018 	 */
508217cadca8Slm66018 	sec = (ISO_VOLDESC_SEC * ISO_SECTOR_SIZE) / vd->vdisk_block_size;
508317cadca8Slm66018 	rv = vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)iso_buf,
508417cadca8Slm66018 	    sec, ISO_SECTOR_SIZE);
508517cadca8Slm66018 
508617cadca8Slm66018 	if (rv < 0)
508717cadca8Slm66018 		return (B_FALSE);
508817cadca8Slm66018 
508917cadca8Slm66018 	for (i = 0; i < ISO_ID_STRLEN; i++) {
509017cadca8Slm66018 		if (ISO_STD_ID(iso_buf)[i] != ISO_ID_STRING[i])
509117cadca8Slm66018 			return (B_FALSE);
509217cadca8Slm66018 	}
509317cadca8Slm66018 
509417cadca8Slm66018 	return (B_TRUE);
509517cadca8Slm66018 }
509617cadca8Slm66018 
509717cadca8Slm66018 /*
509817cadca8Slm66018  * Description:
509917cadca8Slm66018  *	This function checks to see if the virtual device is an ATAPI
510017cadca8Slm66018  *	device. ATAPI devices use Group 1 Read/Write commands, so
510117cadca8Slm66018  *	any USCSI calls vds makes need to take this into account.
510217cadca8Slm66018  *
510317cadca8Slm66018  * Parameters:
510417cadca8Slm66018  *	vd		- disk on which the operation is performed.
510517cadca8Slm66018  *
510617cadca8Slm66018  * Return Code:
510717cadca8Slm66018  *	B_TRUE		- The virtual disk is backed by an ATAPI device
510817cadca8Slm66018  *	B_FALSE		- not an ATAPI device (presumably SCSI)
510917cadca8Slm66018  */
511017cadca8Slm66018 static boolean_t
511117cadca8Slm66018 vd_is_atapi_device(vd_t *vd)
511217cadca8Slm66018 {
511317cadca8Slm66018 	boolean_t	is_atapi = B_FALSE;
511417cadca8Slm66018 	char		*variantp;
511517cadca8Slm66018 	int		rv;
511617cadca8Slm66018 
511717cadca8Slm66018 	ASSERT(vd->ldi_handle[0] != NULL);
511817cadca8Slm66018 	ASSERT(!vd->file);
511917cadca8Slm66018 
512017cadca8Slm66018 	rv = ldi_prop_lookup_string(vd->ldi_handle[0],
512117cadca8Slm66018 	    (LDI_DEV_T_ANY | DDI_PROP_DONTPASS), "variant", &variantp);
512217cadca8Slm66018 	if (rv == DDI_PROP_SUCCESS) {
512317cadca8Slm66018 		PR0("'variant' property exists for %s", vd->device_path);
512417cadca8Slm66018 		if (strcmp(variantp, "atapi") == 0)
512517cadca8Slm66018 			is_atapi = B_TRUE;
512617cadca8Slm66018 		ddi_prop_free(variantp);
512717cadca8Slm66018 	}
512817cadca8Slm66018 
512917cadca8Slm66018 	rv = ldi_prop_exists(vd->ldi_handle[0], LDI_DEV_T_ANY, "atapi");
513017cadca8Slm66018 	if (rv) {
513117cadca8Slm66018 		PR0("'atapi' property exists for %s", vd->device_path);
513217cadca8Slm66018 		is_atapi = B_TRUE;
513317cadca8Slm66018 	}
513417cadca8Slm66018 
513517cadca8Slm66018 	return (is_atapi);
513617cadca8Slm66018 }
513717cadca8Slm66018 
51381ae08745Sheppo static int
51392f5224aeSachartre vd_setup_mediainfo(vd_t *vd)
51400a55fbb7Slm66018 {
51412f5224aeSachartre 	int status, rval;
51424bac2208Snarayan 	struct dk_minfo	dk_minfo;
51430a55fbb7Slm66018 
51442f5224aeSachartre 	ASSERT(vd->ldi_handle[0] != NULL);
51452f5224aeSachartre 	ASSERT(vd->vdisk_block_size != 0);
51462f5224aeSachartre 
51472f5224aeSachartre 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCGMEDIAINFO,
51482f5224aeSachartre 	    (intptr_t)&dk_minfo, (vd->open_flags | FKIOCTL),
51492f5224aeSachartre 	    kcred, &rval)) != 0)
51502f5224aeSachartre 		return (status);
51512f5224aeSachartre 
51522f5224aeSachartre 	ASSERT(dk_minfo.dki_lbsize % vd->vdisk_block_size == 0);
51532f5224aeSachartre 
51542f5224aeSachartre 	vd->block_size = dk_minfo.dki_lbsize;
51552f5224aeSachartre 	vd->vdisk_size = (dk_minfo.dki_capacity * dk_minfo.dki_lbsize) /
51562f5224aeSachartre 	    vd->vdisk_block_size;
51572f5224aeSachartre 	vd->vdisk_media = DK_MEDIATYPE2VD_MEDIATYPE(dk_minfo.dki_media_type);
51582f5224aeSachartre 	return (0);
51592f5224aeSachartre }
51602f5224aeSachartre 
51612f5224aeSachartre static int
51622f5224aeSachartre vd_setup_full_disk(vd_t *vd)
51632f5224aeSachartre {
51642f5224aeSachartre 	int		status;
51652f5224aeSachartre 	major_t		major = getmajor(vd->dev[0]);
51662f5224aeSachartre 	minor_t		minor = getminor(vd->dev[0]) - VD_ENTIRE_DISK_SLICE;
51672f5224aeSachartre 
5168047ba61eSachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
5169047ba61eSachartre 
51702f5224aeSachartre 	vd->vdisk_block_size = DEV_BSIZE;
51712f5224aeSachartre 
51724bac2208Snarayan 	/*
51734bac2208Snarayan 	 * At this point, vdisk_size is set to the size of partition 2 but
51744bac2208Snarayan 	 * this does not represent the size of the disk because partition 2
51754bac2208Snarayan 	 * may not cover the entire disk and its size does not include reserved
51762f5224aeSachartre 	 * blocks. So we call vd_get_mediainfo to udpate this information and
51772f5224aeSachartre 	 * set the block size and the media type of the disk.
51784bac2208Snarayan 	 */
51792f5224aeSachartre 	status = vd_setup_mediainfo(vd);
51802f5224aeSachartre 
51812f5224aeSachartre 	if (status != 0) {
51822f5224aeSachartre 		if (!vd->scsi) {
51832f5224aeSachartre 			/* unexpected failure */
5184690555a1Sachartre 			PRN("ldi_ioctl(DKIOCGMEDIAINFO) returned errno %d",
51854bac2208Snarayan 			    status);
51860a55fbb7Slm66018 			return (status);
51870a55fbb7Slm66018 		}
51882f5224aeSachartre 
51892f5224aeSachartre 		/*
51902f5224aeSachartre 		 * The function can fail for SCSI disks which are present but
51912f5224aeSachartre 		 * reserved by another system. In that case, we don't know the
51922f5224aeSachartre 		 * size of the disk and the block size.
51932f5224aeSachartre 		 */
51942f5224aeSachartre 		vd->vdisk_size = VD_SIZE_UNKNOWN;
51952f5224aeSachartre 		vd->block_size = 0;
51962f5224aeSachartre 		vd->vdisk_media = VD_MEDIA_FIXED;
51972f5224aeSachartre 	}
51980a55fbb7Slm66018 
51990a55fbb7Slm66018 	/* Move dev number and LDI handle to entire-disk-slice array elements */
52000a55fbb7Slm66018 	vd->dev[VD_ENTIRE_DISK_SLICE]		= vd->dev[0];
52010a55fbb7Slm66018 	vd->dev[0]				= 0;
52020a55fbb7Slm66018 	vd->ldi_handle[VD_ENTIRE_DISK_SLICE]	= vd->ldi_handle[0];
52030a55fbb7Slm66018 	vd->ldi_handle[0]			= NULL;
52040a55fbb7Slm66018 
52050a55fbb7Slm66018 	/* Initialize device numbers for remaining slices and open them */
52060a55fbb7Slm66018 	for (int slice = 0; slice < vd->nslices; slice++) {
52070a55fbb7Slm66018 		/*
52080a55fbb7Slm66018 		 * Skip the entire-disk slice, as it's already open and its
52090a55fbb7Slm66018 		 * device known
52100a55fbb7Slm66018 		 */
52110a55fbb7Slm66018 		if (slice == VD_ENTIRE_DISK_SLICE)
52120a55fbb7Slm66018 			continue;
52130a55fbb7Slm66018 		ASSERT(vd->dev[slice] == 0);
52140a55fbb7Slm66018 		ASSERT(vd->ldi_handle[slice] == NULL);
52150a55fbb7Slm66018 
52160a55fbb7Slm66018 		/*
52170a55fbb7Slm66018 		 * Construct the device number for the current slice
52180a55fbb7Slm66018 		 */
52190a55fbb7Slm66018 		vd->dev[slice] = makedevice(major, (minor + slice));
52200a55fbb7Slm66018 
52210a55fbb7Slm66018 		/*
522234683adeSsg70180 		 * Open all slices of the disk to serve them to the client.
522334683adeSsg70180 		 * Slices are opened exclusively to prevent other threads or
522434683adeSsg70180 		 * processes in the service domain from performing I/O to
522534683adeSsg70180 		 * slices being accessed by a client.  Failure to open a slice
522634683adeSsg70180 		 * results in vds not serving this disk, as the client could
522734683adeSsg70180 		 * attempt (and should be able) to access any slice immediately.
522834683adeSsg70180 		 * Any slices successfully opened before a failure will get
522934683adeSsg70180 		 * closed by vds_destroy_vd() as a result of the error returned
523034683adeSsg70180 		 * by this function.
523134683adeSsg70180 		 *
523234683adeSsg70180 		 * We need to do the open with FNDELAY so that opening an empty
523334683adeSsg70180 		 * slice does not fail.
52340a55fbb7Slm66018 		 */
52350a55fbb7Slm66018 		PR0("Opening device major %u, minor %u = slice %u",
52360a55fbb7Slm66018 		    major, minor, slice);
5237047ba61eSachartre 
5238047ba61eSachartre 		/*
5239047ba61eSachartre 		 * Try to open the device. This can fail for example if we are
5240047ba61eSachartre 		 * opening an empty slice. So in case of a failure, we try the
5241047ba61eSachartre 		 * open again but this time with the FNDELAY flag.
5242047ba61eSachartre 		 */
5243047ba61eSachartre 		status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK,
5244047ba61eSachartre 		    vd->open_flags, kcred, &vd->ldi_handle[slice],
5245047ba61eSachartre 		    vd->vds->ldi_ident);
5246047ba61eSachartre 
5247047ba61eSachartre 		if (status != 0) {
5248047ba61eSachartre 			status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK,
5249047ba61eSachartre 			    vd->open_flags | FNDELAY, kcred,
5250047ba61eSachartre 			    &vd->ldi_handle[slice], vd->vds->ldi_ident);
5251047ba61eSachartre 		}
5252047ba61eSachartre 
5253047ba61eSachartre 		if (status != 0) {
5254690555a1Sachartre 			PRN("ldi_open_by_dev() returned errno %d "
52550a55fbb7Slm66018 			    "for slice %u", status, slice);
52560a55fbb7Slm66018 			/* vds_destroy_vd() will close any open slices */
5257690555a1Sachartre 			vd->ldi_handle[slice] = NULL;
52580a55fbb7Slm66018 			return (status);
52590a55fbb7Slm66018 		}
52600a55fbb7Slm66018 	}
52610a55fbb7Slm66018 
52620a55fbb7Slm66018 	return (0);
52630a55fbb7Slm66018 }
52640a55fbb7Slm66018 
5265edcc0754Sachartre /*
5266edcc0754Sachartre  * When a slice or a volume is exported as a single-slice disk, we want
5267edcc0754Sachartre  * the disk backend (i.e. the slice or volume) to be entirely mapped as
5268edcc0754Sachartre  * a slice without the addition of any metadata.
5269edcc0754Sachartre  *
5270edcc0754Sachartre  * So when exporting the disk as a VTOC disk, we fake a disk with the following
5271edcc0754Sachartre  * layout:
5272bae9e67eSachartre  *                flabel +--- flabel_limit
5273bae9e67eSachartre  *                 <->   V
5274bae9e67eSachartre  *                 0 1   C                          D  E
5275bae9e67eSachartre  *                 +-+---+--------------------------+--+
5276bae9e67eSachartre  *  virtual disk:  |L|XXX|           slice 0        |AA|
5277bae9e67eSachartre  *                 +-+---+--------------------------+--+
5278edcc0754Sachartre  *                  ^    :                          :
5279edcc0754Sachartre  *                  |    :                          :
5280edcc0754Sachartre  *      VTOC LABEL--+    :                          :
5281edcc0754Sachartre  *                       +--------------------------+
5282bae9e67eSachartre  *  disk backend:        |     slice/volume/file    |
5283edcc0754Sachartre  *                       +--------------------------+
5284edcc0754Sachartre  *                       0                          N
5285edcc0754Sachartre  *
5286bae9e67eSachartre  * N is the number of blocks in the slice/volume/file.
5287edcc0754Sachartre  *
5288bae9e67eSachartre  * We simulate a disk with N+M blocks, where M is the number of blocks
5289bae9e67eSachartre  * simluated at the beginning and at the end of the disk (blocks 0-C
5290bae9e67eSachartre  * and D-E).
5291edcc0754Sachartre  *
5292bae9e67eSachartre  * The first blocks (0 to C-1) are emulated and can not be changed. Blocks C
5293bae9e67eSachartre  * to D defines slice 0 and are mapped to the backend. Finally we emulate 2
5294bae9e67eSachartre  * alternate cylinders at the end of the disk (blocks D-E). In summary we have:
5295edcc0754Sachartre  *
5296bae9e67eSachartre  * - block 0 (L) returns a fake VTOC label
5297bae9e67eSachartre  * - blocks 1 to C-1 (X) are unused and return 0
5298bae9e67eSachartre  * - blocks C to D-1 are mapped to the exported slice or volume
5299bae9e67eSachartre  * - blocks D and E (A) are blocks defining alternate cylinders (2 cylinders)
5300bae9e67eSachartre  *
5301bae9e67eSachartre  * Note: because we define a fake disk geometry, it is possible that the length
5302bae9e67eSachartre  * of the backend is not a multiple of the size of cylinder, in that case the
5303bae9e67eSachartre  * very end of the backend will not map to any block of the virtual disk.
5304edcc0754Sachartre  */
53050a55fbb7Slm66018 static int
530678fcd0a1Sachartre vd_setup_partition_vtoc(vd_t *vd)
530778fcd0a1Sachartre {
530878fcd0a1Sachartre 	char *device_path = vd->device_path;
5309bae9e67eSachartre 	char unit;
5310bae9e67eSachartre 	size_t size, csize;
531178fcd0a1Sachartre 
531278fcd0a1Sachartre 	/* Initialize dk_geom structure for single-slice device */
531378fcd0a1Sachartre 	if (vd->dk_geom.dkg_nsect == 0) {
531478fcd0a1Sachartre 		PRN("%s geometry claims 0 sectors per track", device_path);
531578fcd0a1Sachartre 		return (EIO);
531678fcd0a1Sachartre 	}
531778fcd0a1Sachartre 	if (vd->dk_geom.dkg_nhead == 0) {
531878fcd0a1Sachartre 		PRN("%s geometry claims 0 heads", device_path);
531978fcd0a1Sachartre 		return (EIO);
532078fcd0a1Sachartre 	}
5321bae9e67eSachartre 
5322bae9e67eSachartre 	/* size of a cylinder in block */
5323bae9e67eSachartre 	csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
5324bae9e67eSachartre 
5325bae9e67eSachartre 	/*
5326bae9e67eSachartre 	 * Add extra cylinders: we emulate the first cylinder (which contains
5327bae9e67eSachartre 	 * the disk label).
5328bae9e67eSachartre 	 */
5329bae9e67eSachartre 	vd->dk_geom.dkg_ncyl = vd->vdisk_size / csize + 1;
5330bae9e67eSachartre 
5331bae9e67eSachartre 	/* we emulate 2 alternate cylinders */
5332bae9e67eSachartre 	vd->dk_geom.dkg_acyl = 2;
533378fcd0a1Sachartre 	vd->dk_geom.dkg_pcyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl;
533478fcd0a1Sachartre 
533578fcd0a1Sachartre 
533678fcd0a1Sachartre 	/* Initialize vtoc structure for single-slice device */
533778fcd0a1Sachartre 	bzero(vd->vtoc.v_part, sizeof (vd->vtoc.v_part));
533878fcd0a1Sachartre 	vd->vtoc.v_part[0].p_tag = V_UNASSIGNED;
533978fcd0a1Sachartre 	vd->vtoc.v_part[0].p_flag = 0;
5340bae9e67eSachartre 	/*
5341bae9e67eSachartre 	 * Partition 0 starts on cylinder 1 and its size has to be
5342bae9e67eSachartre 	 * a multiple of a number of cylinder.
5343bae9e67eSachartre 	 */
5344bae9e67eSachartre 	vd->vtoc.v_part[0].p_start = csize; /* start on cylinder 1 */
5345bae9e67eSachartre 	vd->vtoc.v_part[0].p_size = (vd->vdisk_size / csize) * csize;
534678fcd0a1Sachartre 
5347bae9e67eSachartre 	if (vd_slice_single_slice) {
5348bae9e67eSachartre 		vd->vtoc.v_nparts = 1;
5349bae9e67eSachartre 		bcopy(VD_ASCIILABEL, vd->vtoc.v_asciilabel,
5350bae9e67eSachartre 		    MIN(sizeof (VD_ASCIILABEL),
5351bae9e67eSachartre 		    sizeof (vd->vtoc.v_asciilabel)));
5352bae9e67eSachartre 		bcopy(VD_VOLUME_NAME, vd->vtoc.v_volume,
5353bae9e67eSachartre 		    MIN(sizeof (VD_VOLUME_NAME), sizeof (vd->vtoc.v_volume)));
5354bae9e67eSachartre 	} else {
5355bae9e67eSachartre 		/* adjust the number of slices */
5356bae9e67eSachartre 		vd->nslices = V_NUMPAR;
5357bae9e67eSachartre 		vd->vtoc.v_nparts = V_NUMPAR;
5358bae9e67eSachartre 
5359bae9e67eSachartre 		/* define slice 2 representing the entire disk */
5360bae9e67eSachartre 		vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_tag = V_BACKUP;
5361bae9e67eSachartre 		vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_flag = 0;
5362bae9e67eSachartre 		vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_start = 0;
5363bae9e67eSachartre 		vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_size =
5364bae9e67eSachartre 		    vd->dk_geom.dkg_ncyl * csize;
5365bae9e67eSachartre 
5366bae9e67eSachartre 		vd_get_readable_size(vd->vdisk_size * vd->vdisk_block_size,
5367bae9e67eSachartre 		    &size, &unit);
5368bae9e67eSachartre 
5369bae9e67eSachartre 		/*
5370bae9e67eSachartre 		 * Set some attributes of the geometry to what format(1m) uses
5371bae9e67eSachartre 		 * so that writing a default label using format(1m) does not
5372bae9e67eSachartre 		 * produce any error.
5373bae9e67eSachartre 		 */
5374bae9e67eSachartre 		vd->dk_geom.dkg_bcyl = 0;
5375bae9e67eSachartre 		vd->dk_geom.dkg_intrlv = 1;
5376bae9e67eSachartre 		vd->dk_geom.dkg_write_reinstruct = 0;
5377bae9e67eSachartre 		vd->dk_geom.dkg_read_reinstruct = 0;
5378bae9e67eSachartre 
5379bae9e67eSachartre 		/*
5380bae9e67eSachartre 		 * We must have a correct label name otherwise format(1m) will
5381bae9e67eSachartre 		 * not recognized the disk as labeled.
5382bae9e67eSachartre 		 */
5383bae9e67eSachartre 		(void) snprintf(vd->vtoc.v_asciilabel, LEN_DKL_ASCII,
5384bae9e67eSachartre 		    "SUN-DiskSlice-%ld%cB cyl %d alt %d hd %d sec %d",
5385bae9e67eSachartre 		    size, unit,
5386bae9e67eSachartre 		    vd->dk_geom.dkg_ncyl, vd->dk_geom.dkg_acyl,
5387bae9e67eSachartre 		    vd->dk_geom.dkg_nhead, vd->dk_geom.dkg_nsect);
5388bae9e67eSachartre 		bzero(vd->vtoc.v_volume, sizeof (vd->vtoc.v_volume));
5389bae9e67eSachartre 
5390bae9e67eSachartre 		/* create a fake label from the vtoc and geometry */
5391bae9e67eSachartre 		vd->flabel_limit = csize;
5392bae9e67eSachartre 		vd->flabel_size = VD_LABEL_VTOC_SIZE;
5393bae9e67eSachartre 		vd->flabel = kmem_zalloc(vd->flabel_size, KM_SLEEP);
5394bae9e67eSachartre 		vd_vtocgeom_to_label(&vd->vtoc, &vd->dk_geom,
5395bae9e67eSachartre 		    VD_LABEL_VTOC(vd));
5396bae9e67eSachartre 	}
5397bae9e67eSachartre 
5398bae9e67eSachartre 	/* adjust the vdisk_size, we emulate 3 cylinders */
5399bae9e67eSachartre 	vd->vdisk_size += csize * 3;
5400edcc0754Sachartre 
540178fcd0a1Sachartre 	return (0);
540278fcd0a1Sachartre }
540378fcd0a1Sachartre 
5404edcc0754Sachartre /*
5405edcc0754Sachartre  * When a slice, volume or file is exported as a single-slice disk, we want
5406edcc0754Sachartre  * the disk backend (i.e. the slice, volume or file) to be entirely mapped
5407edcc0754Sachartre  * as a slice without the addition of any metadata.
5408edcc0754Sachartre  *
5409edcc0754Sachartre  * So when exporting the disk as an EFI disk, we fake a disk with the following
5410edcc0754Sachartre  * layout:
5411edcc0754Sachartre  *
5412bae9e67eSachartre  *                  flabel        +--- flabel_limit
5413bae9e67eSachartre  *                 <------>       v
5414bae9e67eSachartre  *                 0 1 2  L      34                        34+N      P
5415bae9e67eSachartre  *                 +-+-+--+-------+--------------------------+-------+
5416bae9e67eSachartre  *  virtual disk:  |X|T|EE|XXXXXXX|           slice 0        |RRRRRRR|
5417bae9e67eSachartre  *                 +-+-+--+-------+--------------------------+-------+
5418edcc0754Sachartre  *                    ^ ^         :                          :
5419edcc0754Sachartre  *                    | |         :                          :
5420edcc0754Sachartre  *                GPT-+ +-GPE     :                          :
5421edcc0754Sachartre  *                                +--------------------------+
5422edcc0754Sachartre  *  disk backend:                 |     slice/volume/file    |
5423edcc0754Sachartre  *                                +--------------------------+
5424edcc0754Sachartre  *                                0                          N
5425edcc0754Sachartre  *
5426edcc0754Sachartre  * N is the number of blocks in the slice/volume/file.
5427edcc0754Sachartre  *
5428bae9e67eSachartre  * We simulate a disk with N+M blocks, where M is the number of blocks
5429bae9e67eSachartre  * simluated at the beginning and at the end of the disk (blocks 0-34
5430bae9e67eSachartre  * and 34+N-P).
5431edcc0754Sachartre  *
5432bae9e67eSachartre  * The first 34 blocks (0 to 33) are emulated and can not be changed. Blocks 34
5433bae9e67eSachartre  * to 34+N defines slice 0 and are mapped to the exported backend, and we
5434bae9e67eSachartre  * emulate some blocks at the end of the disk (blocks 34+N to P) as a the EFI
5435bae9e67eSachartre  * reserved partition.
5436bae9e67eSachartre  *
5437bae9e67eSachartre  * - block 0 (X) is unused and return 0
5438edcc0754Sachartre  * - block 1 (T) returns a fake EFI GPT (via DKIOCGETEFI)
5439bae9e67eSachartre  * - blocks 2 to L-1 (E) defines a fake EFI GPE (via DKIOCGETEFI)
5440bae9e67eSachartre  * - blocks L to 33 (X) are unused and return 0
5441bae9e67eSachartre  * - blocks 34 to 34+N are mapped to the exported slice, volume or file
5442bae9e67eSachartre  * - blocks 34+N+1 to P define a fake reserved partition and backup label, it
5443bae9e67eSachartre  *   returns 0
5444edcc0754Sachartre  *
5445bae9e67eSachartre  * Note: if the backend size is not a multiple of the vdisk block size
5446bae9e67eSachartre  * (DEV_BSIZE = 512 byte) then the very end of the backend will not map to
5447bae9e67eSachartre  * any block of the virtual disk.
5448edcc0754Sachartre  */
544978fcd0a1Sachartre static int
54504bac2208Snarayan vd_setup_partition_efi(vd_t *vd)
54514bac2208Snarayan {
54524bac2208Snarayan 	efi_gpt_t *gpt;
54534bac2208Snarayan 	efi_gpe_t *gpe;
5454edcc0754Sachartre 	struct uuid uuid = EFI_USR;
5455bae9e67eSachartre 	struct uuid efi_reserved = EFI_RESERVED;
54564bac2208Snarayan 	uint32_t crc;
5457bae9e67eSachartre 	uint64_t s0_start, s0_end;
54584bac2208Snarayan 
5459bae9e67eSachartre 	vd->flabel_limit = 34;
5460bae9e67eSachartre 	vd->flabel_size = VD_LABEL_EFI_SIZE;
5461bae9e67eSachartre 	vd->flabel = kmem_zalloc(vd->flabel_size, KM_SLEEP);
5462bae9e67eSachartre 	gpt = VD_LABEL_EFI_GPT(vd);
5463bae9e67eSachartre 	gpe = VD_LABEL_EFI_GPE(vd);
5464edcc0754Sachartre 
5465edcc0754Sachartre 	/* adjust the vdisk_size, we emulate the first 34 blocks */
5466edcc0754Sachartre 	vd->vdisk_size += 34;
5467bae9e67eSachartre 	s0_start = 34;
5468bae9e67eSachartre 	s0_end = vd->vdisk_size - 1;
54694bac2208Snarayan 
54704bac2208Snarayan 	gpt->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
54714bac2208Snarayan 	gpt->efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
54724bac2208Snarayan 	gpt->efi_gpt_HeaderSize = LE_32(sizeof (efi_gpt_t));
5473edcc0754Sachartre 	gpt->efi_gpt_FirstUsableLBA = LE_64(34ULL);
5474edcc0754Sachartre 	gpt->efi_gpt_PartitionEntryLBA = LE_64(2ULL);
54754bac2208Snarayan 	gpt->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (efi_gpe_t));
54764bac2208Snarayan 
5477bae9e67eSachartre 	UUID_LE_CONVERT(gpe[0].efi_gpe_PartitionTypeGUID, uuid);
5478bae9e67eSachartre 	gpe[0].efi_gpe_StartingLBA = LE_64(s0_start);
5479bae9e67eSachartre 	gpe[0].efi_gpe_EndingLBA = LE_64(s0_end);
54804bac2208Snarayan 
5481bae9e67eSachartre 	if (vd_slice_single_slice) {
5482bae9e67eSachartre 		gpt->efi_gpt_NumberOfPartitionEntries = LE_32(1);
5483bae9e67eSachartre 	} else {
5484bae9e67eSachartre 		/* adjust the number of slices */
5485bae9e67eSachartre 		gpt->efi_gpt_NumberOfPartitionEntries = LE_32(VD_MAXPART);
5486bae9e67eSachartre 		vd->nslices = V_NUMPAR;
5487bae9e67eSachartre 
5488bae9e67eSachartre 		/* define a fake reserved partition */
5489bae9e67eSachartre 		UUID_LE_CONVERT(gpe[VD_MAXPART - 1].efi_gpe_PartitionTypeGUID,
5490bae9e67eSachartre 		    efi_reserved);
5491bae9e67eSachartre 		gpe[VD_MAXPART - 1].efi_gpe_StartingLBA =
5492bae9e67eSachartre 		    LE_64(s0_end + 1);
5493bae9e67eSachartre 		gpe[VD_MAXPART - 1].efi_gpe_EndingLBA =
5494bae9e67eSachartre 		    LE_64(s0_end + EFI_MIN_RESV_SIZE);
5495bae9e67eSachartre 
5496bae9e67eSachartre 		/* adjust the vdisk_size to include the reserved slice */
5497bae9e67eSachartre 		vd->vdisk_size += EFI_MIN_RESV_SIZE;
5498bae9e67eSachartre 	}
5499bae9e67eSachartre 
5500bae9e67eSachartre 	gpt->efi_gpt_LastUsableLBA = LE_64(vd->vdisk_size - 1);
5501bae9e67eSachartre 
5502bae9e67eSachartre 	/* adjust the vdisk size for the backup GPT and GPE */
5503bae9e67eSachartre 	vd->vdisk_size += 33;
5504bae9e67eSachartre 
5505bae9e67eSachartre 	CRC32(crc, gpe, sizeof (efi_gpe_t) * VD_MAXPART, -1U, crc32_table);
55064bac2208Snarayan 	gpt->efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
55074bac2208Snarayan 
55084bac2208Snarayan 	CRC32(crc, gpt, sizeof (efi_gpt_t), -1U, crc32_table);
55094bac2208Snarayan 	gpt->efi_gpt_HeaderCRC32 = LE_32(~crc);
55104bac2208Snarayan 
55114bac2208Snarayan 	return (0);
55124bac2208Snarayan }
55134bac2208Snarayan 
5514047ba61eSachartre /*
5515047ba61eSachartre  * Setup for a virtual disk whose backend is a file (exported as a single slice
55168fce2fd6Sachartre  * or as a full disk) or a volume device (for example a ZFS, SVM or VxVM volume)
5517047ba61eSachartre  * exported as a full disk. In these cases, the backend is accessed using the
5518047ba61eSachartre  * vnode interface.
5519047ba61eSachartre  */
55204bac2208Snarayan static int
5521047ba61eSachartre vd_setup_backend_vnode(vd_t *vd)
55223c96341aSnarayan {
552378fcd0a1Sachartre 	int 		rval, status;
55243c96341aSnarayan 	vattr_t		vattr;
55253c96341aSnarayan 	dev_t		dev;
55263c96341aSnarayan 	char		*file_path = vd->device_path;
55273c96341aSnarayan 	char		dev_path[MAXPATHLEN + 1];
55283c96341aSnarayan 	ldi_handle_t	lhandle;
55293c96341aSnarayan 	struct dk_cinfo	dk_cinfo;
5530bae9e67eSachartre 	struct dk_label label;
55313c96341aSnarayan 
5532047ba61eSachartre 	if ((status = vn_open(file_path, UIO_SYSSPACE, vd->open_flags | FOFFMAX,
55333c96341aSnarayan 	    0, &vd->file_vnode, 0, 0)) != 0) {
5534690555a1Sachartre 		PRN("vn_open(%s) = errno %d", file_path, status);
55353c96341aSnarayan 		return (status);
55363c96341aSnarayan 	}
55373c96341aSnarayan 
5538690555a1Sachartre 	/*
5539690555a1Sachartre 	 * We set vd->file now so that vds_destroy_vd will take care of
5540690555a1Sachartre 	 * closing the file and releasing the vnode in case of an error.
5541690555a1Sachartre 	 */
5542690555a1Sachartre 	vd->file = B_TRUE;
5543690555a1Sachartre 
55443c96341aSnarayan 	vattr.va_mask = AT_SIZE;
5545da6c28aaSamw 	if ((status = VOP_GETATTR(vd->file_vnode, &vattr, 0, kcred, NULL))
5546da6c28aaSamw 	    != 0) {
5547690555a1Sachartre 		PRN("VOP_GETATTR(%s) = errno %d", file_path, status);
55483c96341aSnarayan 		return (EIO);
55493c96341aSnarayan 	}
55503c96341aSnarayan 
55513c96341aSnarayan 	vd->file_size = vattr.va_size;
55523c96341aSnarayan 	/* size should be at least sizeof(dk_label) */
55533c96341aSnarayan 	if (vd->file_size < sizeof (struct dk_label)) {
55543c96341aSnarayan 		PRN("Size of file has to be at least %ld bytes",
55553c96341aSnarayan 		    sizeof (struct dk_label));
55563c96341aSnarayan 		return (EIO);
55573c96341aSnarayan 	}
55583c96341aSnarayan 
5559690555a1Sachartre 	if (vd->file_vnode->v_flag & VNOMAP) {
5560690555a1Sachartre 		PRN("File %s cannot be mapped", file_path);
55613c96341aSnarayan 		return (EIO);
55623c96341aSnarayan 	}
55633c96341aSnarayan 
55643c96341aSnarayan 	/* sector size = block size = DEV_BSIZE */
556517cadca8Slm66018 	vd->block_size = DEV_BSIZE;
556617cadca8Slm66018 	vd->vdisk_block_size = DEV_BSIZE;
556787a7269eSachartre 	vd->vdisk_size = vd->file_size / DEV_BSIZE;
55683c96341aSnarayan 	vd->max_xfer_sz = maxphys / DEV_BSIZE; /* default transfer size */
55693c96341aSnarayan 
5570047ba61eSachartre 	/*
5571047ba61eSachartre 	 * Get max_xfer_sz from the device where the file is or from the device
55728fce2fd6Sachartre 	 * itself if we have a volume device.
5573047ba61eSachartre 	 */
5574047ba61eSachartre 	dev_path[0] = '\0';
5575047ba61eSachartre 
55768fce2fd6Sachartre 	if (vd->volume) {
5577047ba61eSachartre 		status = ldi_open_by_name(file_path, FREAD, kcred, &lhandle,
5578047ba61eSachartre 		    vd->vds->ldi_ident);
5579047ba61eSachartre 	} else {
55803c96341aSnarayan 		dev = vd->file_vnode->v_vfsp->vfs_dev;
55813c96341aSnarayan 		if (ddi_dev_pathname(dev, S_IFBLK, dev_path) == DDI_SUCCESS) {
55823c96341aSnarayan 			PR0("underlying device = %s\n", dev_path);
55833c96341aSnarayan 		}
55843c96341aSnarayan 
5585047ba61eSachartre 		status = ldi_open_by_dev(&dev, OTYP_BLK, FREAD, kcred, &lhandle,
5586047ba61eSachartre 		    vd->vds->ldi_ident);
5587047ba61eSachartre 	}
5588047ba61eSachartre 
5589047ba61eSachartre 	if (status != 0) {
5590047ba61eSachartre 		PR0("ldi_open() returned errno %d for device %s",
5591047ba61eSachartre 		    status, (dev_path[0] == '\0')? file_path : dev_path);
55923c96341aSnarayan 	} else {
55933c96341aSnarayan 		if ((status = ldi_ioctl(lhandle, DKIOCINFO,
5594047ba61eSachartre 		    (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred,
55953c96341aSnarayan 		    &rval)) != 0) {
55963c96341aSnarayan 			PR0("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
55973c96341aSnarayan 			    status, dev_path);
55983c96341aSnarayan 		} else {
55993c96341aSnarayan 			/*
56003c96341aSnarayan 			 * Store the device's max transfer size for
56013c96341aSnarayan 			 * return to the client
56023c96341aSnarayan 			 */
56033c96341aSnarayan 			vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
56043c96341aSnarayan 		}
56053c96341aSnarayan 
56063c96341aSnarayan 		PR0("close the device %s", dev_path);
56073c96341aSnarayan 		(void) ldi_close(lhandle, FREAD, kcred);
56083c96341aSnarayan 	}
56093c96341aSnarayan 
5610205eeb1aSlm66018 	PR0("using file %s, dev %s, max_xfer = %u blks",
56113c96341aSnarayan 	    file_path, dev_path, vd->max_xfer_sz);
56123c96341aSnarayan 
5613edcc0754Sachartre 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
56148fce2fd6Sachartre 		ASSERT(!vd->volume);
5615bae9e67eSachartre 		vd->vdisk_media = VD_MEDIA_FIXED;
5616bae9e67eSachartre 		vd->vdisk_label = (vd_slice_label == VD_DISK_LABEL_UNK)?
5617bae9e67eSachartre 		    vd_file_slice_label : vd_slice_label;
5618bae9e67eSachartre 		if (vd->vdisk_label == VD_DISK_LABEL_EFI ||
5619bae9e67eSachartre 		    vd->file_size >= ONE_TERABYTE) {
5620edcc0754Sachartre 			status = vd_setup_partition_efi(vd);
5621bae9e67eSachartre 		} else {
5622bae9e67eSachartre 			/*
5623bae9e67eSachartre 			 * We build a default label to get a geometry for
5624bae9e67eSachartre 			 * the vdisk. Then the partition setup function will
5625bae9e67eSachartre 			 * adjust the vtoc so that it defines a single-slice
5626bae9e67eSachartre 			 * disk.
5627bae9e67eSachartre 			 */
5628bae9e67eSachartre 			vd_build_default_label(vd->file_size, &label);
5629bae9e67eSachartre 			vd_label_to_vtocgeom(&label, &vd->vtoc, &vd->dk_geom);
5630bae9e67eSachartre 			status = vd_setup_partition_vtoc(vd);
5631bae9e67eSachartre 		}
5632bae9e67eSachartre 		return (status);
5633edcc0754Sachartre 	}
5634edcc0754Sachartre 
5635edcc0754Sachartre 	/*
5636edcc0754Sachartre 	 * Find and validate the geometry of a disk image.
5637edcc0754Sachartre 	 */
5638edcc0754Sachartre 	status = vd_file_validate_geometry(vd);
5639edcc0754Sachartre 	if (status != 0 && status != EINVAL && status != ENOTSUP) {
5640edcc0754Sachartre 		PRN("Failed to read label from %s", file_path);
5641edcc0754Sachartre 		return (EIO);
5642edcc0754Sachartre 	}
5643edcc0754Sachartre 
5644edcc0754Sachartre 	if (vd_file_is_iso_image(vd)) {
5645edcc0754Sachartre 		/*
5646edcc0754Sachartre 		 * Indicate whether to call this a CD or DVD from the size
5647edcc0754Sachartre 		 * of the ISO image (images for both drive types are stored
5648edcc0754Sachartre 		 * in the ISO-9600 format). CDs can store up to just under 1Gb
5649edcc0754Sachartre 		 */
5650edcc0754Sachartre 		if ((vd->vdisk_size * vd->vdisk_block_size) >
5651edcc0754Sachartre 		    (1024 * 1024 * 1024))
5652edcc0754Sachartre 			vd->vdisk_media = VD_MEDIA_DVD;
5653edcc0754Sachartre 		else
5654edcc0754Sachartre 			vd->vdisk_media = VD_MEDIA_CD;
5655edcc0754Sachartre 	} else {
5656edcc0754Sachartre 		vd->vdisk_media = VD_MEDIA_FIXED;
5657edcc0754Sachartre 	}
5658edcc0754Sachartre 
5659edcc0754Sachartre 	/* Setup devid for the disk image */
5660047ba61eSachartre 
566178fcd0a1Sachartre 	if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
566278fcd0a1Sachartre 
566387a7269eSachartre 		status = vd_file_read_devid(vd, &vd->file_devid);
566487a7269eSachartre 
566587a7269eSachartre 		if (status == 0) {
566687a7269eSachartre 			/* a valid devid was found */
566787a7269eSachartre 			return (0);
566887a7269eSachartre 		}
566987a7269eSachartre 
567087a7269eSachartre 		if (status != EINVAL) {
567187a7269eSachartre 			/*
567278fcd0a1Sachartre 			 * There was an error while trying to read the devid.
567378fcd0a1Sachartre 			 * So this disk image may have a devid but we are
567478fcd0a1Sachartre 			 * unable to read it.
567587a7269eSachartre 			 */
567687a7269eSachartre 			PR0("can not read devid for %s", file_path);
567787a7269eSachartre 			vd->file_devid = NULL;
567887a7269eSachartre 			return (0);
567987a7269eSachartre 		}
568078fcd0a1Sachartre 	}
568187a7269eSachartre 
568287a7269eSachartre 	/*
568387a7269eSachartre 	 * No valid device id was found so we create one. Note that a failure
568487a7269eSachartre 	 * to create a device id is not fatal and does not prevent the disk
568587a7269eSachartre 	 * image from being attached.
568687a7269eSachartre 	 */
568787a7269eSachartre 	PR1("creating devid for %s", file_path);
568887a7269eSachartre 
568987a7269eSachartre 	if (ddi_devid_init(vd->vds->dip, DEVID_FAB, NULL, 0,
569087a7269eSachartre 	    &vd->file_devid) != DDI_SUCCESS) {
569187a7269eSachartre 		PR0("fail to create devid for %s", file_path);
569287a7269eSachartre 		vd->file_devid = NULL;
569387a7269eSachartre 		return (0);
569487a7269eSachartre 	}
569587a7269eSachartre 
569678fcd0a1Sachartre 	/*
569778fcd0a1Sachartre 	 * Write devid to the disk image. The devid is stored into the disk
569878fcd0a1Sachartre 	 * image if we have a valid label; otherwise the devid will be stored
569978fcd0a1Sachartre 	 * when the user writes a valid label.
570078fcd0a1Sachartre 	 */
570178fcd0a1Sachartre 	if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
570287a7269eSachartre 		if (vd_file_write_devid(vd, vd->file_devid) != 0) {
570387a7269eSachartre 			PR0("fail to write devid for %s", file_path);
570487a7269eSachartre 			ddi_devid_free(vd->file_devid);
570587a7269eSachartre 			vd->file_devid = NULL;
570687a7269eSachartre 		}
570778fcd0a1Sachartre 	}
570887a7269eSachartre 
57093c96341aSnarayan 	return (0);
57103c96341aSnarayan }
57113c96341aSnarayan 
571217cadca8Slm66018 
571317cadca8Slm66018 /*
571417cadca8Slm66018  * Description:
571517cadca8Slm66018  *	Open a device using its device path (supplied by ldm(1m))
571617cadca8Slm66018  *
571717cadca8Slm66018  * Parameters:
571817cadca8Slm66018  *	vd 	- pointer to structure containing the vDisk info
57198fce2fd6Sachartre  *	flags	- open flags
572017cadca8Slm66018  *
572117cadca8Slm66018  * Return Value
572217cadca8Slm66018  *	0	- success
572317cadca8Slm66018  *	!= 0	- some other non-zero return value from ldi(9F) functions
572417cadca8Slm66018  */
572517cadca8Slm66018 static int
57268fce2fd6Sachartre vd_open_using_ldi_by_name(vd_t *vd, int flags)
572717cadca8Slm66018 {
57288fce2fd6Sachartre 	int		status;
572917cadca8Slm66018 	char		*device_path = vd->device_path;
573017cadca8Slm66018 
57318fce2fd6Sachartre 	/* Attempt to open device */
57328fce2fd6Sachartre 	status = ldi_open_by_name(device_path, flags, kcred,
573317cadca8Slm66018 	    &vd->ldi_handle[0], vd->vds->ldi_ident);
573417cadca8Slm66018 
573517cadca8Slm66018 	/*
573617cadca8Slm66018 	 * The open can fail for example if we are opening an empty slice.
573717cadca8Slm66018 	 * In case of a failure, we try the open again but this time with
573817cadca8Slm66018 	 * the FNDELAY flag.
573917cadca8Slm66018 	 */
574017cadca8Slm66018 	if (status != 0)
57418fce2fd6Sachartre 		status = ldi_open_by_name(device_path, flags | FNDELAY,
574217cadca8Slm66018 		    kcred, &vd->ldi_handle[0], vd->vds->ldi_ident);
574317cadca8Slm66018 
574417cadca8Slm66018 	if (status != 0) {
574517cadca8Slm66018 		PR0("ldi_open_by_name(%s) = errno %d", device_path, status);
574617cadca8Slm66018 		vd->ldi_handle[0] = NULL;
574717cadca8Slm66018 		return (status);
574817cadca8Slm66018 	}
574917cadca8Slm66018 
575017cadca8Slm66018 	return (0);
575117cadca8Slm66018 }
575217cadca8Slm66018 
5753047ba61eSachartre /*
5754047ba61eSachartre  * Setup for a virtual disk which backend is a device (a physical disk,
57558fce2fd6Sachartre  * slice or volume device) that is directly exported either as a full disk
57568fce2fd6Sachartre  * for a physical disk or as a slice for a volume device or a disk slice.
5757047ba61eSachartre  * In these cases, the backend is accessed using the LDI interface.
5758047ba61eSachartre  */
57593c96341aSnarayan static int
5760047ba61eSachartre vd_setup_backend_ldi(vd_t *vd)
57611ae08745Sheppo {
5762e1ebb9ecSlm66018 	int		rval, status;
57631ae08745Sheppo 	struct dk_cinfo	dk_cinfo;
57643c96341aSnarayan 	char		*device_path = vd->device_path;
57651ae08745Sheppo 
57668fce2fd6Sachartre 	/* device has been opened by vd_identify_dev() */
57678fce2fd6Sachartre 	ASSERT(vd->ldi_handle[0] != NULL);
57688fce2fd6Sachartre 	ASSERT(vd->dev[0] != NULL);
57690a55fbb7Slm66018 
57703c96341aSnarayan 	vd->file = B_FALSE;
57714bac2208Snarayan 
577278fcd0a1Sachartre 	/* Verify backing device supports dk_cinfo */
5773e1ebb9ecSlm66018 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO,
5774047ba61eSachartre 	    (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred,
5775e1ebb9ecSlm66018 	    &rval)) != 0) {
5776e1ebb9ecSlm66018 		PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
5777e1ebb9ecSlm66018 		    status, device_path);
5778e1ebb9ecSlm66018 		return (status);
5779e1ebb9ecSlm66018 	}
5780e1ebb9ecSlm66018 	if (dk_cinfo.dki_partition >= V_NUMPAR) {
5781e1ebb9ecSlm66018 		PRN("slice %u >= maximum slice %u for %s",
5782e1ebb9ecSlm66018 		    dk_cinfo.dki_partition, V_NUMPAR, device_path);
5783e1ebb9ecSlm66018 		return (EIO);
5784e1ebb9ecSlm66018 	}
57854bac2208Snarayan 
57868fce2fd6Sachartre 	/*
57878fce2fd6Sachartre 	 * The device has been opened read-only by vd_identify_dev(), re-open
57888fce2fd6Sachartre 	 * it read-write if the write flag is set and we don't have an optical
57898fce2fd6Sachartre 	 * device such as a CD-ROM, which, for now, we do not permit writes to
57908fce2fd6Sachartre 	 * and thus should not export write operations to the client.
57918fce2fd6Sachartre 	 *
57928fce2fd6Sachartre 	 * Future: if/when we implement support for guest domains writing to
57938fce2fd6Sachartre 	 * optical devices we will need to do further checking of the media type
57948fce2fd6Sachartre 	 * to distinguish between read-only and writable discs.
57958fce2fd6Sachartre 	 */
57968fce2fd6Sachartre 	if (dk_cinfo.dki_ctype == DKC_CDROM) {
57978fce2fd6Sachartre 
57988fce2fd6Sachartre 		vd->open_flags &= ~FWRITE;
57998fce2fd6Sachartre 
58008fce2fd6Sachartre 	} else if (vd->open_flags & FWRITE) {
58018fce2fd6Sachartre 
58028fce2fd6Sachartre 		(void) ldi_close(vd->ldi_handle[0], vd->open_flags & ~FWRITE,
58038fce2fd6Sachartre 		    kcred);
58048fce2fd6Sachartre 		status = vd_open_using_ldi_by_name(vd, vd->open_flags);
58058fce2fd6Sachartre 		if (status != 0) {
58068fce2fd6Sachartre 			PR0("Failed to open (%s) = errno %d",
58078fce2fd6Sachartre 			    device_path, status);
58088fce2fd6Sachartre 			return (status);
58098fce2fd6Sachartre 		}
58108fce2fd6Sachartre 	}
58118fce2fd6Sachartre 
5812e1ebb9ecSlm66018 	/* Store the device's max transfer size for return to the client */
5813e1ebb9ecSlm66018 	vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
5814e1ebb9ecSlm66018 
5815047ba61eSachartre 	/*
581617cadca8Slm66018 	 * We need to work out if it's an ATAPI (IDE CD-ROM) or SCSI device so
581717cadca8Slm66018 	 * that we can use the correct CDB group when sending USCSI commands.
581817cadca8Slm66018 	 */
581917cadca8Slm66018 	vd->is_atapi_dev = vd_is_atapi_device(vd);
582017cadca8Slm66018 
582117cadca8Slm66018 	/*
5822047ba61eSachartre 	 * Export a full disk.
5823047ba61eSachartre 	 *
5824047ba61eSachartre 	 * When we use the LDI interface, we export a device as a full disk
5825047ba61eSachartre 	 * if we have an entire disk slice (slice 2) and if this slice is
5826047ba61eSachartre 	 * exported as a full disk and not as a single slice disk.
582717cadca8Slm66018 	 * Similarly, we want to use LDI if we are accessing a CD or DVD
582817cadca8Slm66018 	 * device (even if it isn't s2)
5829047ba61eSachartre 	 *
58308fce2fd6Sachartre 	 * Note that volume devices are exported as full disks using the vnode
5831047ba61eSachartre 	 * interface, not the LDI interface.
5832047ba61eSachartre 	 */
583317cadca8Slm66018 	if ((dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE &&
583417cadca8Slm66018 	    vd->vdisk_type == VD_DISK_TYPE_DISK) ||
583517cadca8Slm66018 	    dk_cinfo.dki_ctype == DKC_CDROM) {
58368fce2fd6Sachartre 		ASSERT(!vd->volume);
58372f5224aeSachartre 		if (dk_cinfo.dki_ctype == DKC_SCSI_CCS)
58382f5224aeSachartre 			vd->scsi = B_TRUE;
5839047ba61eSachartre 		return (vd_setup_full_disk(vd));
5840047ba61eSachartre 	}
5841047ba61eSachartre 
5842047ba61eSachartre 	/*
5843047ba61eSachartre 	 * Export a single slice disk.
5844047ba61eSachartre 	 *
58458fce2fd6Sachartre 	 * The exported device can be either a volume device or a disk slice. If
5846047ba61eSachartre 	 * it is a disk slice different from slice 2 then it is always exported
5847047ba61eSachartre 	 * as a single slice disk even if the "slice" option is not specified.
58488fce2fd6Sachartre 	 * If it is disk slice 2 or a volume device then it is exported as a
5849047ba61eSachartre 	 * single slice disk only if the "slice" option is specified.
5850047ba61eSachartre 	 */
5851047ba61eSachartre 	return (vd_setup_single_slice_disk(vd));
5852047ba61eSachartre }
5853047ba61eSachartre 
5854047ba61eSachartre static int
5855047ba61eSachartre vd_setup_single_slice_disk(vd_t *vd)
5856047ba61eSachartre {
5857edcc0754Sachartre 	int status, rval;
5858bae9e67eSachartre 	struct dk_label label;
5859047ba61eSachartre 	char *device_path = vd->device_path;
5860047ba61eSachartre 
5861047ba61eSachartre 	/* Get size of backing device */
5862047ba61eSachartre 	if (ldi_get_size(vd->ldi_handle[0], &vd->vdisk_size) != DDI_SUCCESS) {
5863047ba61eSachartre 		PRN("ldi_get_size() failed for %s", device_path);
58641ae08745Sheppo 		return (EIO);
58651ae08745Sheppo 	}
5866047ba61eSachartre 	vd->vdisk_size = lbtodb(vd->vdisk_size);	/* convert to blocks */
586717cadca8Slm66018 	vd->block_size = DEV_BSIZE;
586817cadca8Slm66018 	vd->vdisk_block_size = DEV_BSIZE;
586917cadca8Slm66018 	vd->vdisk_media = VD_MEDIA_FIXED;
5870047ba61eSachartre 
58718fce2fd6Sachartre 	if (vd->volume) {
5872047ba61eSachartre 		ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
587378fcd0a1Sachartre 	}
58740a55fbb7Slm66018 
5875047ba61eSachartre 	/*
5876047ba61eSachartre 	 * We export the slice as a single slice disk even if the "slice"
5877047ba61eSachartre 	 * option was not specified.
5878047ba61eSachartre 	 */
58791ae08745Sheppo 	vd->vdisk_type  = VD_DISK_TYPE_SLICE;
58801ae08745Sheppo 	vd->nslices	= 1;
58811ae08745Sheppo 
5882edcc0754Sachartre 	/*
5883edcc0754Sachartre 	 * When exporting a slice or a device as a single slice disk, we don't
5884edcc0754Sachartre 	 * care about any partitioning exposed by the backend. The goal is just
5885edcc0754Sachartre 	 * to export the backend as a flat storage. We provide a fake partition
5886edcc0754Sachartre 	 * table (either a VTOC or EFI), which presents only one slice, to
5887bae9e67eSachartre 	 * accommodate tools expecting a disk label. The selection of the label
5888bae9e67eSachartre 	 * type (VTOC or EFI) depends on the value of the vd_slice_label
5889bae9e67eSachartre 	 * variable.
5890edcc0754Sachartre 	 */
5891bae9e67eSachartre 	if (vd_slice_label == VD_DISK_LABEL_EFI ||
5892bae9e67eSachartre 	    vd->vdisk_size >= ONE_TERABYTE / DEV_BSIZE) {
5893bae9e67eSachartre 		vd->vdisk_label = VD_DISK_LABEL_EFI;
5894bae9e67eSachartre 	} else {
5895bae9e67eSachartre 		status = ldi_ioctl(vd->ldi_handle[0], DKIOCGVTOC,
5896bae9e67eSachartre 		    (intptr_t)&vd->vtoc, (vd->open_flags | FKIOCTL),
5897bae9e67eSachartre 		    kcred, &rval);
5898edcc0754Sachartre 
5899edcc0754Sachartre 		if (status == 0) {
5900bae9e67eSachartre 			status = ldi_ioctl(vd->ldi_handle[0], DKIOCGGEOM,
5901bae9e67eSachartre 			    (intptr_t)&vd->dk_geom, (vd->open_flags | FKIOCTL),
5902bae9e67eSachartre 			    kcred, &rval);
5903bae9e67eSachartre 
5904bae9e67eSachartre 			if (status != 0) {
5905bae9e67eSachartre 				PRN("ldi_ioctl(DKIOCGEOM) returned errno %d "
5906bae9e67eSachartre 				    "for %s", status, device_path);
5907bae9e67eSachartre 				return (status);
5908bae9e67eSachartre 			}
5909edcc0754Sachartre 			vd->vdisk_label = VD_DISK_LABEL_VTOC;
5910bae9e67eSachartre 
5911bae9e67eSachartre 		} else if (vd_slice_label == VD_DISK_LABEL_VTOC) {
5912bae9e67eSachartre 
5913bae9e67eSachartre 			vd->vdisk_label = VD_DISK_LABEL_VTOC;
5914bae9e67eSachartre 			vd_build_default_label(vd->vdisk_size * DEV_BSIZE,
5915bae9e67eSachartre 			    &label);
5916bae9e67eSachartre 			vd_label_to_vtocgeom(&label, &vd->vtoc, &vd->dk_geom);
5917bae9e67eSachartre 
5918bae9e67eSachartre 		} else {
5919bae9e67eSachartre 			vd->vdisk_label = VD_DISK_LABEL_EFI;
5920bae9e67eSachartre 		}
5921bae9e67eSachartre 	}
5922bae9e67eSachartre 
5923bae9e67eSachartre 	if (vd->vdisk_label == VD_DISK_LABEL_VTOC) {
5924bae9e67eSachartre 		/* export with a fake VTOC label */
592578fcd0a1Sachartre 		status = vd_setup_partition_vtoc(vd);
5926bae9e67eSachartre 
5927edcc0754Sachartre 	} else {
5928edcc0754Sachartre 		/* export with a fake EFI label */
5929edcc0754Sachartre 		status = vd_setup_partition_efi(vd);
593078fcd0a1Sachartre 	}
593178fcd0a1Sachartre 
59324bac2208Snarayan 	return (status);
59334bac2208Snarayan }
59341ae08745Sheppo 
59358fce2fd6Sachartre /*
59368fce2fd6Sachartre  * Description:
59378fce2fd6Sachartre  *	Open a device using its device path and identify if this is
59388fce2fd6Sachartre  *	a disk device or a volume device.
59398fce2fd6Sachartre  *
59408fce2fd6Sachartre  * Parameters:
59418fce2fd6Sachartre  *	vd 	- pointer to structure containing the vDisk info
59428fce2fd6Sachartre  *	dtype	- return the driver type of the device
59438fce2fd6Sachartre  *
59448fce2fd6Sachartre  * Return Value
59458fce2fd6Sachartre  *	0	- success
59468fce2fd6Sachartre  *	!= 0	- some other non-zero return value from ldi(9F) functions
59478fce2fd6Sachartre  */
59488fce2fd6Sachartre static int
59498fce2fd6Sachartre vd_identify_dev(vd_t *vd, int *dtype)
59508fce2fd6Sachartre {
59518fce2fd6Sachartre 	int status, i;
59528fce2fd6Sachartre 	char *device_path = vd->device_path;
59538fce2fd6Sachartre 	char *drv_name;
59548fce2fd6Sachartre 	int drv_type;
59558fce2fd6Sachartre 	vds_t *vds = vd->vds;
59568fce2fd6Sachartre 
59578fce2fd6Sachartre 	status = vd_open_using_ldi_by_name(vd, vd->open_flags & ~FWRITE);
59588fce2fd6Sachartre 	if (status != 0) {
59598fce2fd6Sachartre 		PR0("Failed to open (%s) = errno %d", device_path, status);
59608fce2fd6Sachartre 		return (status);
59618fce2fd6Sachartre 	}
59628fce2fd6Sachartre 
59638fce2fd6Sachartre 	/* Get device number of backing device */
59648fce2fd6Sachartre 	if ((status = ldi_get_dev(vd->ldi_handle[0], &vd->dev[0])) != 0) {
59658fce2fd6Sachartre 		PRN("ldi_get_dev() returned errno %d for %s",
59668fce2fd6Sachartre 		    status, device_path);
59678fce2fd6Sachartre 		return (status);
59688fce2fd6Sachartre 	}
59698fce2fd6Sachartre 
59708fce2fd6Sachartre 	/*
59718fce2fd6Sachartre 	 * We start by looking if the driver is in the list from vds.conf
59728fce2fd6Sachartre 	 * so that we can override the built-in list using vds.conf.
59738fce2fd6Sachartre 	 */
59748fce2fd6Sachartre 	drv_name = ddi_major_to_name(getmajor(vd->dev[0]));
59758fce2fd6Sachartre 	drv_type = VD_DRIVER_UNKNOWN;
59768fce2fd6Sachartre 
59778fce2fd6Sachartre 	/* check vds.conf list */
59788fce2fd6Sachartre 	for (i = 0; i < vds->num_drivers; i++) {
59798fce2fd6Sachartre 		if (vds->driver_types[i].type == VD_DRIVER_UNKNOWN) {
59808fce2fd6Sachartre 			/* ignore invalid entries */
59818fce2fd6Sachartre 			continue;
59828fce2fd6Sachartre 		}
59838fce2fd6Sachartre 		if (strcmp(drv_name, vds->driver_types[i].name) == 0) {
59848fce2fd6Sachartre 			drv_type = vds->driver_types[i].type;
59858fce2fd6Sachartre 			goto done;
59868fce2fd6Sachartre 		}
59878fce2fd6Sachartre 	}
59888fce2fd6Sachartre 
59898fce2fd6Sachartre 	/* check built-in list */
59908fce2fd6Sachartre 	for (i = 0; i < VDS_NUM_DRIVERS; i++) {
59918fce2fd6Sachartre 		if (strcmp(drv_name, vds_driver_types[i].name) == 0) {
59928fce2fd6Sachartre 			drv_type = vds_driver_types[i].type;
59938fce2fd6Sachartre 			goto done;
59948fce2fd6Sachartre 		}
59958fce2fd6Sachartre 	}
59968fce2fd6Sachartre 
59978fce2fd6Sachartre done:
59988fce2fd6Sachartre 	PR0("driver %s identified as %s", drv_name,
59998fce2fd6Sachartre 	    (drv_type == VD_DRIVER_DISK)? "DISK" :
60008fce2fd6Sachartre 	    (drv_type == VD_DRIVER_VOLUME)? "VOLUME" : "UNKNOWN");
60018fce2fd6Sachartre 
60028fce2fd6Sachartre 	*dtype = drv_type;
60038fce2fd6Sachartre 
60048fce2fd6Sachartre 	return (0);
60058fce2fd6Sachartre }
60068fce2fd6Sachartre 
60071ae08745Sheppo static int
6008047ba61eSachartre vd_setup_vd(vd_t *vd)
6009047ba61eSachartre {
60108fce2fd6Sachartre 	int		status, drv_type, pseudo;
6011047ba61eSachartre 	dev_info_t	*dip;
6012047ba61eSachartre 	vnode_t 	*vnp;
6013047ba61eSachartre 	char		*path = vd->device_path;
6014047ba61eSachartre 
6015047ba61eSachartre 	/* make sure the vdisk backend is valid */
6016047ba61eSachartre 	if ((status = lookupname(path, UIO_SYSSPACE,
6017047ba61eSachartre 	    FOLLOW, NULLVPP, &vnp)) != 0) {
6018047ba61eSachartre 		PR0("Cannot lookup %s errno %d", path, status);
6019047ba61eSachartre 		goto done;
6020047ba61eSachartre 	}
6021047ba61eSachartre 
6022047ba61eSachartre 	switch (vnp->v_type) {
6023047ba61eSachartre 	case VREG:
6024047ba61eSachartre 		/*
6025047ba61eSachartre 		 * Backend is a file so it is exported as a full disk or as a
6026047ba61eSachartre 		 * single slice disk using the vnode interface.
6027047ba61eSachartre 		 */
6028047ba61eSachartre 		VN_RELE(vnp);
60298fce2fd6Sachartre 		vd->volume = B_FALSE;
6030047ba61eSachartre 		status = vd_setup_backend_vnode(vd);
6031047ba61eSachartre 		break;
6032047ba61eSachartre 
6033047ba61eSachartre 	case VBLK:
6034047ba61eSachartre 	case VCHR:
6035047ba61eSachartre 		/*
6036047ba61eSachartre 		 * Backend is a device. The way it is exported depends on the
6037047ba61eSachartre 		 * type of the device.
6038047ba61eSachartre 		 *
60398fce2fd6Sachartre 		 * - A volume device is exported as a full disk using the vnode
6040047ba61eSachartre 		 *   interface or as a single slice disk using the LDI
6041047ba61eSachartre 		 *   interface.
6042047ba61eSachartre 		 *
6043047ba61eSachartre 		 * - A disk (represented by the slice 2 of that disk) is
6044047ba61eSachartre 		 *   exported as a full disk using the LDI interface.
6045047ba61eSachartre 		 *
6046047ba61eSachartre 		 * - A disk slice (different from slice 2) is always exported
6047047ba61eSachartre 		 *   as a single slice disk using the LDI interface.
6048047ba61eSachartre 		 *
6049047ba61eSachartre 		 * - The slice 2 of a disk is exported as a single slice disk
6050047ba61eSachartre 		 *   if the "slice" option is specified, otherwise the entire
6051047ba61eSachartre 		 *   disk will be exported. In any case, the LDI interface is
6052047ba61eSachartre 		 *   used.
6053047ba61eSachartre 		 */
6054047ba61eSachartre 
6055047ba61eSachartre 		/* check if this is a pseudo device */
6056047ba61eSachartre 		if ((dip = ddi_hold_devi_by_instance(getmajor(vnp->v_rdev),
6057047ba61eSachartre 		    dev_to_instance(vnp->v_rdev), 0))  == NULL) {
6058047ba61eSachartre 			PRN("%s is no longer accessible", path);
6059047ba61eSachartre 			VN_RELE(vnp);
6060047ba61eSachartre 			status = EIO;
6061047ba61eSachartre 			break;
6062047ba61eSachartre 		}
60638fce2fd6Sachartre 		pseudo = is_pseudo_device(dip);
6064047ba61eSachartre 		ddi_release_devi(dip);
6065047ba61eSachartre 		VN_RELE(vnp);
6066047ba61eSachartre 
60678fce2fd6Sachartre 		if (vd_identify_dev(vd, &drv_type) != 0) {
60688fce2fd6Sachartre 			PRN("%s identification failed", path);
60698fce2fd6Sachartre 			status = EIO;
60708fce2fd6Sachartre 			break;
60718fce2fd6Sachartre 		}
60728fce2fd6Sachartre 
60738fce2fd6Sachartre 		/*
60748fce2fd6Sachartre 		 * If the driver hasn't been identified then we consider that
60758fce2fd6Sachartre 		 * pseudo devices are volumes and other devices are disks.
60768fce2fd6Sachartre 		 */
60778fce2fd6Sachartre 		if (drv_type == VD_DRIVER_VOLUME ||
60788fce2fd6Sachartre 		    (drv_type == VD_DRIVER_UNKNOWN && pseudo)) {
60798fce2fd6Sachartre 			vd->volume = B_TRUE;
60808fce2fd6Sachartre 		} else {
60812f5224aeSachartre 			status = vd_setup_backend_ldi(vd);
60822f5224aeSachartre 			break;
60832f5224aeSachartre 		}
60842f5224aeSachartre 
6085047ba61eSachartre 		/*
60868fce2fd6Sachartre 		 * If this is a volume device then its usage depends if the
6087047ba61eSachartre 		 * "slice" option is set or not. If the "slice" option is set
60888fce2fd6Sachartre 		 * then the volume device will be exported as a single slice,
6089047ba61eSachartre 		 * otherwise it will be exported as a full disk.
60902f5224aeSachartre 		 *
60912f5224aeSachartre 		 * For backward compatibility, if vd_volume_force_slice is set
60928fce2fd6Sachartre 		 * then we always export volume devices as slices.
6093047ba61eSachartre 		 */
60942f5224aeSachartre 		if (vd_volume_force_slice) {
60952f5224aeSachartre 			vd->vdisk_type = VD_DISK_TYPE_SLICE;
60962f5224aeSachartre 			vd->nslices = 1;
60972f5224aeSachartre 		}
60982f5224aeSachartre 
60998fce2fd6Sachartre 		if (vd->vdisk_type == VD_DISK_TYPE_DISK) {
61008fce2fd6Sachartre 			/* close device opened during identification */
61018fce2fd6Sachartre 			(void) ldi_close(vd->ldi_handle[0],
61028fce2fd6Sachartre 			    vd->open_flags & ~FWRITE, kcred);
61038fce2fd6Sachartre 			vd->ldi_handle[0] = NULL;
61048fce2fd6Sachartre 			vd->dev[0] = 0;
6105047ba61eSachartre 			status = vd_setup_backend_vnode(vd);
61068fce2fd6Sachartre 		} else {
6107047ba61eSachartre 			status = vd_setup_backend_ldi(vd);
61088fce2fd6Sachartre 		}
6109047ba61eSachartre 		break;
6110047ba61eSachartre 
6111047ba61eSachartre 	default:
6112047ba61eSachartre 		PRN("Unsupported vdisk backend %s", path);
6113047ba61eSachartre 		VN_RELE(vnp);
6114047ba61eSachartre 		status = EBADF;
6115047ba61eSachartre 	}
6116047ba61eSachartre 
6117047ba61eSachartre done:
6118047ba61eSachartre 	if (status != 0) {
6119047ba61eSachartre 		/*
6120047ba61eSachartre 		 * If the error is retryable print an error message only
6121047ba61eSachartre 		 * during the first try.
6122047ba61eSachartre 		 */
6123047ba61eSachartre 		if (status == ENXIO || status == ENODEV ||
6124047ba61eSachartre 		    status == ENOENT || status == EROFS) {
6125047ba61eSachartre 			if (!(vd->initialized & VD_SETUP_ERROR)) {
6126047ba61eSachartre 				PRN("%s is currently inaccessible (error %d)",
6127047ba61eSachartre 				    path, status);
6128047ba61eSachartre 			}
6129047ba61eSachartre 			status = EAGAIN;
6130047ba61eSachartre 		} else {
6131047ba61eSachartre 			PRN("%s can not be exported as a virtual disk "
6132047ba61eSachartre 			    "(error %d)", path, status);
6133047ba61eSachartre 		}
6134047ba61eSachartre 		vd->initialized |= VD_SETUP_ERROR;
6135047ba61eSachartre 
6136047ba61eSachartre 	} else if (vd->initialized & VD_SETUP_ERROR) {
6137047ba61eSachartre 		/* print a message only if we previously had an error */
6138047ba61eSachartre 		PRN("%s is now online", path);
6139047ba61eSachartre 		vd->initialized &= ~VD_SETUP_ERROR;
6140047ba61eSachartre 	}
6141047ba61eSachartre 
6142047ba61eSachartre 	return (status);
6143047ba61eSachartre }
6144047ba61eSachartre 
6145047ba61eSachartre static int
6146047ba61eSachartre vds_do_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options,
6147047ba61eSachartre     uint64_t ldc_id, vd_t **vdp)
61481ae08745Sheppo {
61491ae08745Sheppo 	char			tq_name[TASKQ_NAMELEN];
61500a55fbb7Slm66018 	int			status;
61511ae08745Sheppo 	ddi_iblock_cookie_t	iblock = NULL;
61521ae08745Sheppo 	ldc_attr_t		ldc_attr;
61531ae08745Sheppo 	vd_t			*vd;
61541ae08745Sheppo 
61551ae08745Sheppo 
61561ae08745Sheppo 	ASSERT(vds != NULL);
6157e1ebb9ecSlm66018 	ASSERT(device_path != NULL);
61581ae08745Sheppo 	ASSERT(vdp != NULL);
6159e1ebb9ecSlm66018 	PR0("Adding vdisk for %s", device_path);
61601ae08745Sheppo 
61611ae08745Sheppo 	if ((vd = kmem_zalloc(sizeof (*vd), KM_NOSLEEP)) == NULL) {
61621ae08745Sheppo 		PRN("No memory for virtual disk");
61631ae08745Sheppo 		return (EAGAIN);
61641ae08745Sheppo 	}
61651ae08745Sheppo 	*vdp = vd;	/* assign here so vds_destroy_vd() can cleanup later */
61661ae08745Sheppo 	vd->vds = vds;
61673c96341aSnarayan 	(void) strncpy(vd->device_path, device_path, MAXPATHLEN);
61681ae08745Sheppo 
6169047ba61eSachartre 	/* Setup open flags */
6170047ba61eSachartre 	vd->open_flags = FREAD;
6171047ba61eSachartre 
6172047ba61eSachartre 	if (!(options & VD_OPT_RDONLY))
6173047ba61eSachartre 		vd->open_flags |= FWRITE;
6174047ba61eSachartre 
6175047ba61eSachartre 	if (options & VD_OPT_EXCLUSIVE)
6176047ba61eSachartre 		vd->open_flags |= FEXCL;
6177047ba61eSachartre 
6178047ba61eSachartre 	/* Setup disk type */
6179047ba61eSachartre 	if (options & VD_OPT_SLICE) {
6180047ba61eSachartre 		vd->vdisk_type = VD_DISK_TYPE_SLICE;
6181047ba61eSachartre 		vd->nslices = 1;
6182047ba61eSachartre 	} else {
6183047ba61eSachartre 		vd->vdisk_type = VD_DISK_TYPE_DISK;
6184047ba61eSachartre 		vd->nslices = V_NUMPAR;
6185047ba61eSachartre 	}
6186047ba61eSachartre 
6187047ba61eSachartre 	/* default disk label */
6188047ba61eSachartre 	vd->vdisk_label = VD_DISK_LABEL_UNK;
6189047ba61eSachartre 
61900a55fbb7Slm66018 	/* Open vdisk and initialize parameters */
61913c96341aSnarayan 	if ((status = vd_setup_vd(vd)) == 0) {
61923c96341aSnarayan 		vd->initialized |= VD_DISK_READY;
61931ae08745Sheppo 
61943c96341aSnarayan 		ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
61958fce2fd6Sachartre 		PR0("vdisk_type = %s, volume = %s, file = %s, nslices = %u",
61963c96341aSnarayan 		    ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
61978fce2fd6Sachartre 		    (vd->volume ? "yes" : "no"), (vd->file ? "yes" : "no"),
61983c96341aSnarayan 		    vd->nslices);
61993c96341aSnarayan 	} else {
62003c96341aSnarayan 		if (status != EAGAIN)
62013c96341aSnarayan 			return (status);
62023c96341aSnarayan 	}
62031ae08745Sheppo 
62041ae08745Sheppo 	/* Initialize locking */
62051ae08745Sheppo 	if (ddi_get_soft_iblock_cookie(vds->dip, DDI_SOFTINT_MED,
62061ae08745Sheppo 	    &iblock) != DDI_SUCCESS) {
62071ae08745Sheppo 		PRN("Could not get iblock cookie.");
62081ae08745Sheppo 		return (EIO);
62091ae08745Sheppo 	}
62101ae08745Sheppo 
62111ae08745Sheppo 	mutex_init(&vd->lock, NULL, MUTEX_DRIVER, iblock);
62121ae08745Sheppo 	vd->initialized |= VD_LOCKING;
62131ae08745Sheppo 
62141ae08745Sheppo 
6215d10e4ef2Snarayan 	/* Create start and completion task queues for the vdisk */
6216d10e4ef2Snarayan 	(void) snprintf(tq_name, sizeof (tq_name), "vd_startq%lu", id);
62171ae08745Sheppo 	PR1("tq_name = %s", tq_name);
6218d10e4ef2Snarayan 	if ((vd->startq = ddi_taskq_create(vds->dip, tq_name, 1,
62191ae08745Sheppo 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
62201ae08745Sheppo 		PRN("Could not create task queue");
62211ae08745Sheppo 		return (EIO);
62221ae08745Sheppo 	}
6223d10e4ef2Snarayan 	(void) snprintf(tq_name, sizeof (tq_name), "vd_completionq%lu", id);
6224d10e4ef2Snarayan 	PR1("tq_name = %s", tq_name);
6225d10e4ef2Snarayan 	if ((vd->completionq = ddi_taskq_create(vds->dip, tq_name, 1,
6226d10e4ef2Snarayan 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
6227d10e4ef2Snarayan 		PRN("Could not create task queue");
6228d10e4ef2Snarayan 		return (EIO);
6229d10e4ef2Snarayan 	}
62305b98b509Sachartre 
62315b98b509Sachartre 	/* Allocate the staging buffer */
62325b98b509Sachartre 	vd->max_msglen = sizeof (vio_msg_t);	/* baseline vio message size */
62335b98b509Sachartre 	vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
62345b98b509Sachartre 
6235d10e4ef2Snarayan 	vd->enabled = 1;	/* before callback can dispatch to startq */
62361ae08745Sheppo 
62371ae08745Sheppo 
62381ae08745Sheppo 	/* Bring up LDC */
62391ae08745Sheppo 	ldc_attr.devclass	= LDC_DEV_BLK_SVC;
62401ae08745Sheppo 	ldc_attr.instance	= ddi_get_instance(vds->dip);
62411ae08745Sheppo 	ldc_attr.mode		= LDC_MODE_UNRELIABLE;
6242e1ebb9ecSlm66018 	ldc_attr.mtu		= VD_LDC_MTU;
62431ae08745Sheppo 	if ((status = ldc_init(ldc_id, &ldc_attr, &vd->ldc_handle)) != 0) {
624417cadca8Slm66018 		PRN("Could not initialize LDC channel %lx, "
6245690555a1Sachartre 		    "init failed with error %d", ldc_id, status);
62461ae08745Sheppo 		return (status);
62471ae08745Sheppo 	}
62481ae08745Sheppo 	vd->initialized |= VD_LDC;
62491ae08745Sheppo 
62501ae08745Sheppo 	if ((status = ldc_reg_callback(vd->ldc_handle, vd_handle_ldc_events,
62511ae08745Sheppo 	    (caddr_t)vd)) != 0) {
6252690555a1Sachartre 		PRN("Could not initialize LDC channel %lu,"
6253690555a1Sachartre 		    "reg_callback failed with error %d", ldc_id, status);
62541ae08745Sheppo 		return (status);
62551ae08745Sheppo 	}
62561ae08745Sheppo 
62571ae08745Sheppo 	if ((status = ldc_open(vd->ldc_handle)) != 0) {
6258690555a1Sachartre 		PRN("Could not initialize LDC channel %lu,"
6259690555a1Sachartre 		    "open failed with error %d", ldc_id, status);
62601ae08745Sheppo 		return (status);
62611ae08745Sheppo 	}
62621ae08745Sheppo 
62633af08d82Slm66018 	if ((status = ldc_up(vd->ldc_handle)) != 0) {
626434683adeSsg70180 		PR0("ldc_up() returned errno %d", status);
62653af08d82Slm66018 	}
62663af08d82Slm66018 
62674bac2208Snarayan 	/* Allocate the inband task memory handle */
62684bac2208Snarayan 	status = ldc_mem_alloc_handle(vd->ldc_handle, &(vd->inband_task.mhdl));
62694bac2208Snarayan 	if (status) {
6270690555a1Sachartre 		PRN("Could not initialize LDC channel %lu,"
6271690555a1Sachartre 		    "alloc_handle failed with error %d", ldc_id, status);
62724bac2208Snarayan 		return (ENXIO);
62734bac2208Snarayan 	}
62741ae08745Sheppo 
62751ae08745Sheppo 	/* Add the successfully-initialized vdisk to the server's table */
62761ae08745Sheppo 	if (mod_hash_insert(vds->vd_table, (mod_hash_key_t)id, vd) != 0) {
62771ae08745Sheppo 		PRN("Error adding vdisk ID %lu to table", id);
62781ae08745Sheppo 		return (EIO);
62791ae08745Sheppo 	}
62801ae08745Sheppo 
62813af08d82Slm66018 	/* store initial state */
62823af08d82Slm66018 	vd->state = VD_STATE_INIT;
62833af08d82Slm66018 
62841ae08745Sheppo 	return (0);
62851ae08745Sheppo }
62861ae08745Sheppo 
62873af08d82Slm66018 static void
62883af08d82Slm66018 vd_free_dring_task(vd_t *vdp)
62893af08d82Slm66018 {
62903af08d82Slm66018 	if (vdp->dring_task != NULL) {
62913af08d82Slm66018 		ASSERT(vdp->dring_len != 0);
62923af08d82Slm66018 		/* Free all dring_task memory handles */
62933af08d82Slm66018 		for (int i = 0; i < vdp->dring_len; i++) {
62943af08d82Slm66018 			(void) ldc_mem_free_handle(vdp->dring_task[i].mhdl);
62953af08d82Slm66018 			kmem_free(vdp->dring_task[i].msg, vdp->max_msglen);
62963af08d82Slm66018 			vdp->dring_task[i].msg = NULL;
62973af08d82Slm66018 		}
62983af08d82Slm66018 		kmem_free(vdp->dring_task,
62993af08d82Slm66018 		    (sizeof (*vdp->dring_task)) * vdp->dring_len);
63003af08d82Slm66018 		vdp->dring_task = NULL;
63013af08d82Slm66018 	}
63023af08d82Slm66018 }
63033af08d82Slm66018 
63041ae08745Sheppo /*
63051ae08745Sheppo  * Destroy the state associated with a virtual disk
63061ae08745Sheppo  */
63071ae08745Sheppo static void
63081ae08745Sheppo vds_destroy_vd(void *arg)
63091ae08745Sheppo {
63101ae08745Sheppo 	vd_t	*vd = (vd_t *)arg;
631134683adeSsg70180 	int	retry = 0, rv;
63121ae08745Sheppo 
63131ae08745Sheppo 	if (vd == NULL)
63141ae08745Sheppo 		return;
63151ae08745Sheppo 
6316d10e4ef2Snarayan 	PR0("Destroying vdisk state");
6317d10e4ef2Snarayan 
63181ae08745Sheppo 	/* Disable queuing requests for the vdisk */
63191ae08745Sheppo 	if (vd->initialized & VD_LOCKING) {
63201ae08745Sheppo 		mutex_enter(&vd->lock);
63211ae08745Sheppo 		vd->enabled = 0;
63221ae08745Sheppo 		mutex_exit(&vd->lock);
63231ae08745Sheppo 	}
63241ae08745Sheppo 
6325d10e4ef2Snarayan 	/* Drain and destroy start queue (*before* destroying completionq) */
6326d10e4ef2Snarayan 	if (vd->startq != NULL)
6327d10e4ef2Snarayan 		ddi_taskq_destroy(vd->startq);	/* waits for queued tasks */
6328d10e4ef2Snarayan 
6329d10e4ef2Snarayan 	/* Drain and destroy completion queue (*before* shutting down LDC) */
6330d10e4ef2Snarayan 	if (vd->completionq != NULL)
6331d10e4ef2Snarayan 		ddi_taskq_destroy(vd->completionq);	/* waits for tasks */
6332d10e4ef2Snarayan 
63333af08d82Slm66018 	vd_free_dring_task(vd);
63343af08d82Slm66018 
633534683adeSsg70180 	/* Free the inband task memory handle */
633634683adeSsg70180 	(void) ldc_mem_free_handle(vd->inband_task.mhdl);
633734683adeSsg70180 
633834683adeSsg70180 	/* Shut down LDC */
633934683adeSsg70180 	if (vd->initialized & VD_LDC) {
634034683adeSsg70180 		/* unmap the dring */
634134683adeSsg70180 		if (vd->initialized & VD_DRING)
634234683adeSsg70180 			(void) ldc_mem_dring_unmap(vd->dring_handle);
634334683adeSsg70180 
634434683adeSsg70180 		/* close LDC channel - retry on EAGAIN */
634534683adeSsg70180 		while ((rv = ldc_close(vd->ldc_handle)) == EAGAIN) {
634634683adeSsg70180 			if (++retry > vds_ldc_retries) {
634734683adeSsg70180 				PR0("Timed out closing channel");
634834683adeSsg70180 				break;
634934683adeSsg70180 			}
635034683adeSsg70180 			drv_usecwait(vds_ldc_delay);
635134683adeSsg70180 		}
635234683adeSsg70180 		if (rv == 0) {
635334683adeSsg70180 			(void) ldc_unreg_callback(vd->ldc_handle);
635434683adeSsg70180 			(void) ldc_fini(vd->ldc_handle);
635534683adeSsg70180 		} else {
635634683adeSsg70180 			/*
635734683adeSsg70180 			 * Closing the LDC channel has failed. Ideally we should
635834683adeSsg70180 			 * fail here but there is no Zeus level infrastructure
635934683adeSsg70180 			 * to handle this. The MD has already been changed and
636034683adeSsg70180 			 * we have to do the close. So we try to do as much
636134683adeSsg70180 			 * clean up as we can.
636234683adeSsg70180 			 */
636334683adeSsg70180 			(void) ldc_set_cb_mode(vd->ldc_handle, LDC_CB_DISABLE);
636434683adeSsg70180 			while (ldc_unreg_callback(vd->ldc_handle) == EAGAIN)
636534683adeSsg70180 				drv_usecwait(vds_ldc_delay);
636634683adeSsg70180 		}
636734683adeSsg70180 	}
636834683adeSsg70180 
63693af08d82Slm66018 	/* Free the staging buffer for msgs */
63703af08d82Slm66018 	if (vd->vio_msgp != NULL) {
63713af08d82Slm66018 		kmem_free(vd->vio_msgp, vd->max_msglen);
63723af08d82Slm66018 		vd->vio_msgp = NULL;
63733af08d82Slm66018 	}
63743af08d82Slm66018 
63753af08d82Slm66018 	/* Free the inband message buffer */
63763af08d82Slm66018 	if (vd->inband_task.msg != NULL) {
63773af08d82Slm66018 		kmem_free(vd->inband_task.msg, vd->max_msglen);
63783af08d82Slm66018 		vd->inband_task.msg = NULL;
6379d10e4ef2Snarayan 	}
6380da6c28aaSamw 
63813c96341aSnarayan 	if (vd->file) {
6382690555a1Sachartre 		/* Close file */
6383047ba61eSachartre 		(void) VOP_CLOSE(vd->file_vnode, vd->open_flags, 1,
6384da6c28aaSamw 		    0, kcred, NULL);
63853c96341aSnarayan 		VN_RELE(vd->file_vnode);
638687a7269eSachartre 		if (vd->file_devid != NULL)
638787a7269eSachartre 			ddi_devid_free(vd->file_devid);
63883c96341aSnarayan 	} else {
63891ae08745Sheppo 		/* Close any open backing-device slices */
6390bae9e67eSachartre 		for (uint_t slice = 0; slice < V_NUMPAR; slice++) {
63911ae08745Sheppo 			if (vd->ldi_handle[slice] != NULL) {
63921ae08745Sheppo 				PR0("Closing slice %u", slice);
63931ae08745Sheppo 				(void) ldi_close(vd->ldi_handle[slice],
6394047ba61eSachartre 				    vd->open_flags, kcred);
63951ae08745Sheppo 			}
63961ae08745Sheppo 		}
63973c96341aSnarayan 	}
63981ae08745Sheppo 
6399bae9e67eSachartre 	/* Free any fake label */
6400bae9e67eSachartre 	if (vd->flabel) {
6401bae9e67eSachartre 		kmem_free(vd->flabel, vd->flabel_size);
6402bae9e67eSachartre 		vd->flabel = NULL;
6403bae9e67eSachartre 		vd->flabel_size = 0;
6404bae9e67eSachartre 	}
6405bae9e67eSachartre 
64061ae08745Sheppo 	/* Free lock */
64071ae08745Sheppo 	if (vd->initialized & VD_LOCKING)
64081ae08745Sheppo 		mutex_destroy(&vd->lock);
64091ae08745Sheppo 
64101ae08745Sheppo 	/* Finally, free the vdisk structure itself */
64111ae08745Sheppo 	kmem_free(vd, sizeof (*vd));
64121ae08745Sheppo }
64131ae08745Sheppo 
64141ae08745Sheppo static int
6415047ba61eSachartre vds_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options,
6416047ba61eSachartre     uint64_t ldc_id)
64171ae08745Sheppo {
64181ae08745Sheppo 	int	status;
64191ae08745Sheppo 	vd_t	*vd = NULL;
64201ae08745Sheppo 
64211ae08745Sheppo 
6422047ba61eSachartre 	if ((status = vds_do_init_vd(vds, id, device_path, options,
6423047ba61eSachartre 	    ldc_id, &vd)) != 0)
64241ae08745Sheppo 		vds_destroy_vd(vd);
64251ae08745Sheppo 
64261ae08745Sheppo 	return (status);
64271ae08745Sheppo }
64281ae08745Sheppo 
64291ae08745Sheppo static int
64301ae08745Sheppo vds_do_get_ldc_id(md_t *md, mde_cookie_t vd_node, mde_cookie_t *channel,
64311ae08745Sheppo     uint64_t *ldc_id)
64321ae08745Sheppo {
64331ae08745Sheppo 	int	num_channels;
64341ae08745Sheppo 
64351ae08745Sheppo 
64361ae08745Sheppo 	/* Look for channel endpoint child(ren) of the vdisk MD node */
64371ae08745Sheppo 	if ((num_channels = md_scan_dag(md, vd_node,
64381ae08745Sheppo 	    md_find_name(md, VD_CHANNEL_ENDPOINT),
64391ae08745Sheppo 	    md_find_name(md, "fwd"), channel)) <= 0) {
64401ae08745Sheppo 		PRN("No \"%s\" found for virtual disk", VD_CHANNEL_ENDPOINT);
64411ae08745Sheppo 		return (-1);
64421ae08745Sheppo 	}
64431ae08745Sheppo 
64441ae08745Sheppo 	/* Get the "id" value for the first channel endpoint node */
64451ae08745Sheppo 	if (md_get_prop_val(md, channel[0], VD_ID_PROP, ldc_id) != 0) {
64461ae08745Sheppo 		PRN("No \"%s\" property found for \"%s\" of vdisk",
64471ae08745Sheppo 		    VD_ID_PROP, VD_CHANNEL_ENDPOINT);
64481ae08745Sheppo 		return (-1);
64491ae08745Sheppo 	}
64501ae08745Sheppo 
64511ae08745Sheppo 	if (num_channels > 1) {
64521ae08745Sheppo 		PRN("Using ID of first of multiple channels for this vdisk");
64531ae08745Sheppo 	}
64541ae08745Sheppo 
64551ae08745Sheppo 	return (0);
64561ae08745Sheppo }
64571ae08745Sheppo 
64581ae08745Sheppo static int
64591ae08745Sheppo vds_get_ldc_id(md_t *md, mde_cookie_t vd_node, uint64_t *ldc_id)
64601ae08745Sheppo {
64611ae08745Sheppo 	int		num_nodes, status;
64621ae08745Sheppo 	size_t		size;
64631ae08745Sheppo 	mde_cookie_t	*channel;
64641ae08745Sheppo 
64651ae08745Sheppo 
64661ae08745Sheppo 	if ((num_nodes = md_node_count(md)) <= 0) {
64671ae08745Sheppo 		PRN("Invalid node count in Machine Description subtree");
64681ae08745Sheppo 		return (-1);
64691ae08745Sheppo 	}
64701ae08745Sheppo 	size = num_nodes*(sizeof (*channel));
64711ae08745Sheppo 	channel = kmem_zalloc(size, KM_SLEEP);
64721ae08745Sheppo 	status = vds_do_get_ldc_id(md, vd_node, channel, ldc_id);
64731ae08745Sheppo 	kmem_free(channel, size);
64741ae08745Sheppo 
64751ae08745Sheppo 	return (status);
64761ae08745Sheppo }
64771ae08745Sheppo 
6478047ba61eSachartre /*
6479047ba61eSachartre  * Function:
6480047ba61eSachartre  *	vds_get_options
6481047ba61eSachartre  *
6482047ba61eSachartre  * Description:
6483047ba61eSachartre  * 	Parse the options of a vds node. Options are defined as an array
6484047ba61eSachartre  *	of strings in the vds-block-device-opts property of the vds node
6485047ba61eSachartre  *	in the machine description. Options are returned as a bitmask. The
6486047ba61eSachartre  *	mapping between the bitmask options and the options strings from the
6487047ba61eSachartre  *	machine description is defined in the vd_bdev_options[] array.
6488047ba61eSachartre  *
6489047ba61eSachartre  *	The vds-block-device-opts property is optional. If a vds has no such
6490047ba61eSachartre  *	property then no option is defined.
6491047ba61eSachartre  *
6492047ba61eSachartre  * Parameters:
6493047ba61eSachartre  *	md		- machine description.
6494047ba61eSachartre  *	vd_node		- vds node in the machine description for which
6495047ba61eSachartre  *			  options have to be parsed.
6496047ba61eSachartre  *	options		- the returned options.
6497047ba61eSachartre  *
6498047ba61eSachartre  * Return Code:
6499047ba61eSachartre  *	none.
6500047ba61eSachartre  */
6501047ba61eSachartre static void
6502047ba61eSachartre vds_get_options(md_t *md, mde_cookie_t vd_node, uint64_t *options)
6503047ba61eSachartre {
6504047ba61eSachartre 	char	*optstr, *opt;
6505047ba61eSachartre 	int	len, n, i;
6506047ba61eSachartre 
6507047ba61eSachartre 	*options = 0;
6508047ba61eSachartre 
6509047ba61eSachartre 	if (md_get_prop_data(md, vd_node, VD_BLOCK_DEVICE_OPTS,
6510047ba61eSachartre 	    (uint8_t **)&optstr, &len) != 0) {
6511047ba61eSachartre 		PR0("No options found");
6512047ba61eSachartre 		return;
6513047ba61eSachartre 	}
6514047ba61eSachartre 
6515047ba61eSachartre 	/* parse options */
6516047ba61eSachartre 	opt = optstr;
6517047ba61eSachartre 	n = sizeof (vd_bdev_options) / sizeof (vd_option_t);
6518047ba61eSachartre 
6519047ba61eSachartre 	while (opt < optstr + len) {
6520047ba61eSachartre 		for (i = 0; i < n; i++) {
6521047ba61eSachartre 			if (strncmp(vd_bdev_options[i].vdo_name,
6522047ba61eSachartre 			    opt, VD_OPTION_NLEN) == 0) {
6523047ba61eSachartre 				*options |= vd_bdev_options[i].vdo_value;
6524047ba61eSachartre 				break;
6525047ba61eSachartre 			}
6526047ba61eSachartre 		}
6527047ba61eSachartre 
6528047ba61eSachartre 		if (i < n) {
6529047ba61eSachartre 			PR0("option: %s", opt);
6530047ba61eSachartre 		} else {
6531047ba61eSachartre 			PRN("option %s is unknown or unsupported", opt);
6532047ba61eSachartre 		}
6533047ba61eSachartre 
6534047ba61eSachartre 		opt += strlen(opt) + 1;
6535047ba61eSachartre 	}
6536047ba61eSachartre }
6537047ba61eSachartre 
65381ae08745Sheppo static void
65398fce2fd6Sachartre vds_driver_types_free(vds_t *vds)
65408fce2fd6Sachartre {
65418fce2fd6Sachartre 	if (vds->driver_types != NULL) {
65428fce2fd6Sachartre 		kmem_free(vds->driver_types, sizeof (vd_driver_type_t) *
65438fce2fd6Sachartre 		    vds->num_drivers);
65448fce2fd6Sachartre 		vds->driver_types = NULL;
65458fce2fd6Sachartre 		vds->num_drivers = 0;
65468fce2fd6Sachartre 	}
65478fce2fd6Sachartre }
65488fce2fd6Sachartre 
65498fce2fd6Sachartre /*
65508fce2fd6Sachartre  * Update the driver type list with information from vds.conf.
65518fce2fd6Sachartre  */
65528fce2fd6Sachartre static void
65538fce2fd6Sachartre vds_driver_types_update(vds_t *vds)
65548fce2fd6Sachartre {
65558fce2fd6Sachartre 	char **list, *s;
65568fce2fd6Sachartre 	uint_t i, num, count = 0, len;
65578fce2fd6Sachartre 
65588fce2fd6Sachartre 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, vds->dip,
65598fce2fd6Sachartre 	    DDI_PROP_DONTPASS, "driver-type-list", &list, &num) !=
65608fce2fd6Sachartre 	    DDI_PROP_SUCCESS)
65618fce2fd6Sachartre 		return;
65628fce2fd6Sachartre 
65638fce2fd6Sachartre 	/*
65648fce2fd6Sachartre 	 * We create a driver_types list with as many as entries as there
65658fce2fd6Sachartre 	 * is in the driver-type-list from vds.conf. However only valid
65668fce2fd6Sachartre 	 * entries will be populated (i.e. entries from driver-type-list
65678fce2fd6Sachartre 	 * with a valid syntax). Invalid entries will be left blank so
65688fce2fd6Sachartre 	 * they will have no driver name and the driver type will be
65698fce2fd6Sachartre 	 * VD_DRIVER_UNKNOWN (= 0).
65708fce2fd6Sachartre 	 */
65718fce2fd6Sachartre 	vds->num_drivers = num;
65728fce2fd6Sachartre 	vds->driver_types = kmem_zalloc(sizeof (vd_driver_type_t) * num,
65738fce2fd6Sachartre 	    KM_SLEEP);
65748fce2fd6Sachartre 
65758fce2fd6Sachartre 	for (i = 0; i < num; i++) {
65768fce2fd6Sachartre 
65778fce2fd6Sachartre 		s = strchr(list[i], ':');
65788fce2fd6Sachartre 
65798fce2fd6Sachartre 		if (s == NULL) {
65808fce2fd6Sachartre 			PRN("vds.conf: driver-type-list, entry %d (%s): "
65818fce2fd6Sachartre 			    "a colon is expected in the entry",
65828fce2fd6Sachartre 			    i, list[i]);
65838fce2fd6Sachartre 			continue;
65848fce2fd6Sachartre 		}
65858fce2fd6Sachartre 
65868fce2fd6Sachartre 		len = (uintptr_t)s - (uintptr_t)list[i];
65878fce2fd6Sachartre 
65888fce2fd6Sachartre 		if (len == 0) {
65898fce2fd6Sachartre 			PRN("vds.conf: driver-type-list, entry %d (%s): "
65908fce2fd6Sachartre 			    "the driver name is empty",
65918fce2fd6Sachartre 			    i, list[i]);
65928fce2fd6Sachartre 			continue;
65938fce2fd6Sachartre 		}
65948fce2fd6Sachartre 
65958fce2fd6Sachartre 		if (len >= VD_DRIVER_NAME_LEN) {
65968fce2fd6Sachartre 			PRN("vds.conf: driver-type-list, entry %d (%s): "
65978fce2fd6Sachartre 			    "the driver name is too long",
65988fce2fd6Sachartre 			    i, list[i]);
65998fce2fd6Sachartre 			continue;
66008fce2fd6Sachartre 		}
66018fce2fd6Sachartre 
66028fce2fd6Sachartre 		if (strcmp(s + 1, "disk") == 0) {
66038fce2fd6Sachartre 
66048fce2fd6Sachartre 			vds->driver_types[i].type = VD_DRIVER_DISK;
66058fce2fd6Sachartre 
66068fce2fd6Sachartre 		} else if (strcmp(s + 1, "volume") == 0) {
66078fce2fd6Sachartre 
66088fce2fd6Sachartre 			vds->driver_types[i].type = VD_DRIVER_VOLUME;
66098fce2fd6Sachartre 
66108fce2fd6Sachartre 		} else {
66118fce2fd6Sachartre 			PRN("vds.conf: driver-type-list, entry %d (%s): "
66128fce2fd6Sachartre 			    "the driver type is invalid",
66138fce2fd6Sachartre 			    i, list[i]);
66148fce2fd6Sachartre 			continue;
66158fce2fd6Sachartre 		}
66168fce2fd6Sachartre 
66178fce2fd6Sachartre 		(void) strncpy(vds->driver_types[i].name, list[i], len);
66188fce2fd6Sachartre 
66198fce2fd6Sachartre 		PR0("driver-type-list, entry %d (%s) added",
66208fce2fd6Sachartre 		    i, list[i]);
66218fce2fd6Sachartre 
66228fce2fd6Sachartre 		count++;
66238fce2fd6Sachartre 	}
66248fce2fd6Sachartre 
66258fce2fd6Sachartre 	ddi_prop_free(list);
66268fce2fd6Sachartre 
66278fce2fd6Sachartre 	if (count == 0) {
66288fce2fd6Sachartre 		/* nothing was added, clean up */
66298fce2fd6Sachartre 		vds_driver_types_free(vds);
66308fce2fd6Sachartre 	}
66318fce2fd6Sachartre }
66328fce2fd6Sachartre 
66338fce2fd6Sachartre static void
66341ae08745Sheppo vds_add_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
66351ae08745Sheppo {
6636e1ebb9ecSlm66018 	char		*device_path = NULL;
6637047ba61eSachartre 	uint64_t	id = 0, ldc_id = 0, options = 0;
66381ae08745Sheppo 
66391ae08745Sheppo 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
66401ae08745Sheppo 		PRN("Error getting vdisk \"%s\"", VD_ID_PROP);
66411ae08745Sheppo 		return;
66421ae08745Sheppo 	}
66431ae08745Sheppo 	PR0("Adding vdisk ID %lu", id);
66441ae08745Sheppo 	if (md_get_prop_str(md, vd_node, VD_BLOCK_DEVICE_PROP,
6645e1ebb9ecSlm66018 	    &device_path) != 0) {
66461ae08745Sheppo 		PRN("Error getting vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
66471ae08745Sheppo 		return;
66481ae08745Sheppo 	}
66491ae08745Sheppo 
6650047ba61eSachartre 	vds_get_options(md, vd_node, &options);
6651047ba61eSachartre 
66521ae08745Sheppo 	if (vds_get_ldc_id(md, vd_node, &ldc_id) != 0) {
66531ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", id);
66541ae08745Sheppo 		return;
66551ae08745Sheppo 	}
66561ae08745Sheppo 
6657047ba61eSachartre 	if (vds_init_vd(vds, id, device_path, options, ldc_id) != 0) {
66581ae08745Sheppo 		PRN("Failed to add vdisk ID %lu", id);
665917cadca8Slm66018 		if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0)
666017cadca8Slm66018 			PRN("No vDisk entry found for vdisk ID %lu", id);
66611ae08745Sheppo 		return;
66621ae08745Sheppo 	}
66631ae08745Sheppo }
66641ae08745Sheppo 
66651ae08745Sheppo static void
66661ae08745Sheppo vds_remove_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
66671ae08745Sheppo {
66681ae08745Sheppo 	uint64_t	id = 0;
66691ae08745Sheppo 
66701ae08745Sheppo 
66711ae08745Sheppo 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
66721ae08745Sheppo 		PRN("Unable to get \"%s\" property from vdisk's MD node",
66731ae08745Sheppo 		    VD_ID_PROP);
66741ae08745Sheppo 		return;
66751ae08745Sheppo 	}
66761ae08745Sheppo 	PR0("Removing vdisk ID %lu", id);
66771ae08745Sheppo 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0)
66781ae08745Sheppo 		PRN("No vdisk entry found for vdisk ID %lu", id);
66791ae08745Sheppo }
66801ae08745Sheppo 
66811ae08745Sheppo static void
66821ae08745Sheppo vds_change_vd(vds_t *vds, md_t *prev_md, mde_cookie_t prev_vd_node,
66831ae08745Sheppo     md_t *curr_md, mde_cookie_t curr_vd_node)
66841ae08745Sheppo {
66851ae08745Sheppo 	char		*curr_dev, *prev_dev;
6686047ba61eSachartre 	uint64_t	curr_id = 0, curr_ldc_id = 0, curr_options = 0;
6687047ba61eSachartre 	uint64_t	prev_id = 0, prev_ldc_id = 0, prev_options = 0;
66881ae08745Sheppo 	size_t		len;
66891ae08745Sheppo 
66901ae08745Sheppo 
66911ae08745Sheppo 	/* Validate that vdisk ID has not changed */
66921ae08745Sheppo 	if (md_get_prop_val(prev_md, prev_vd_node, VD_ID_PROP, &prev_id) != 0) {
66931ae08745Sheppo 		PRN("Error getting previous vdisk \"%s\" property",
66941ae08745Sheppo 		    VD_ID_PROP);
66951ae08745Sheppo 		return;
66961ae08745Sheppo 	}
66971ae08745Sheppo 	if (md_get_prop_val(curr_md, curr_vd_node, VD_ID_PROP, &curr_id) != 0) {
66981ae08745Sheppo 		PRN("Error getting current vdisk \"%s\" property", VD_ID_PROP);
66991ae08745Sheppo 		return;
67001ae08745Sheppo 	}
67011ae08745Sheppo 	if (curr_id != prev_id) {
67021ae08745Sheppo 		PRN("Not changing vdisk:  ID changed from %lu to %lu",
67031ae08745Sheppo 		    prev_id, curr_id);
67041ae08745Sheppo 		return;
67051ae08745Sheppo 	}
67061ae08745Sheppo 
67071ae08745Sheppo 	/* Validate that LDC ID has not changed */
67081ae08745Sheppo 	if (vds_get_ldc_id(prev_md, prev_vd_node, &prev_ldc_id) != 0) {
67091ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", prev_id);
67101ae08745Sheppo 		return;
67111ae08745Sheppo 	}
67121ae08745Sheppo 
67131ae08745Sheppo 	if (vds_get_ldc_id(curr_md, curr_vd_node, &curr_ldc_id) != 0) {
67141ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", curr_id);
67151ae08745Sheppo 		return;
67161ae08745Sheppo 	}
67171ae08745Sheppo 	if (curr_ldc_id != prev_ldc_id) {
67180a55fbb7Slm66018 		_NOTE(NOTREACHED);	/* lint is confused */
67191ae08745Sheppo 		PRN("Not changing vdisk:  "
67201ae08745Sheppo 		    "LDC ID changed from %lu to %lu", prev_ldc_id, curr_ldc_id);
67211ae08745Sheppo 		return;
67221ae08745Sheppo 	}
67231ae08745Sheppo 
67241ae08745Sheppo 	/* Determine whether device path has changed */
67251ae08745Sheppo 	if (md_get_prop_str(prev_md, prev_vd_node, VD_BLOCK_DEVICE_PROP,
67261ae08745Sheppo 	    &prev_dev) != 0) {
67271ae08745Sheppo 		PRN("Error getting previous vdisk \"%s\"",
67281ae08745Sheppo 		    VD_BLOCK_DEVICE_PROP);
67291ae08745Sheppo 		return;
67301ae08745Sheppo 	}
67311ae08745Sheppo 	if (md_get_prop_str(curr_md, curr_vd_node, VD_BLOCK_DEVICE_PROP,
67321ae08745Sheppo 	    &curr_dev) != 0) {
67331ae08745Sheppo 		PRN("Error getting current vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
67341ae08745Sheppo 		return;
67351ae08745Sheppo 	}
67361ae08745Sheppo 	if (((len = strlen(curr_dev)) == strlen(prev_dev)) &&
67371ae08745Sheppo 	    (strncmp(curr_dev, prev_dev, len) == 0))
67381ae08745Sheppo 		return;	/* no relevant (supported) change */
67391ae08745Sheppo 
6740047ba61eSachartre 	/* Validate that options have not changed */
6741047ba61eSachartre 	vds_get_options(prev_md, prev_vd_node, &prev_options);
6742047ba61eSachartre 	vds_get_options(curr_md, curr_vd_node, &curr_options);
6743047ba61eSachartre 	if (prev_options != curr_options) {
6744047ba61eSachartre 		PRN("Not changing vdisk:  options changed from %lx to %lx",
6745047ba61eSachartre 		    prev_options, curr_options);
6746047ba61eSachartre 		return;
6747047ba61eSachartre 	}
6748047ba61eSachartre 
67491ae08745Sheppo 	PR0("Changing vdisk ID %lu", prev_id);
67503af08d82Slm66018 
67511ae08745Sheppo 	/* Remove old state, which will close vdisk and reset */
67521ae08745Sheppo 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)prev_id) != 0)
67531ae08745Sheppo 		PRN("No entry found for vdisk ID %lu", prev_id);
67543af08d82Slm66018 
67551ae08745Sheppo 	/* Re-initialize vdisk with new state */
6756047ba61eSachartre 	if (vds_init_vd(vds, curr_id, curr_dev, curr_options,
6757047ba61eSachartre 	    curr_ldc_id) != 0) {
67581ae08745Sheppo 		PRN("Failed to change vdisk ID %lu", curr_id);
67591ae08745Sheppo 		return;
67601ae08745Sheppo 	}
67611ae08745Sheppo }
67621ae08745Sheppo 
67631ae08745Sheppo static int
67641ae08745Sheppo vds_process_md(void *arg, mdeg_result_t *md)
67651ae08745Sheppo {
67661ae08745Sheppo 	int	i;
67671ae08745Sheppo 	vds_t	*vds = arg;
67681ae08745Sheppo 
67691ae08745Sheppo 
67701ae08745Sheppo 	if (md == NULL)
67711ae08745Sheppo 		return (MDEG_FAILURE);
67721ae08745Sheppo 	ASSERT(vds != NULL);
67731ae08745Sheppo 
67741ae08745Sheppo 	for (i = 0; i < md->removed.nelem; i++)
67751ae08745Sheppo 		vds_remove_vd(vds, md->removed.mdp, md->removed.mdep[i]);
67761ae08745Sheppo 	for (i = 0; i < md->match_curr.nelem; i++)
67771ae08745Sheppo 		vds_change_vd(vds, md->match_prev.mdp, md->match_prev.mdep[i],
67781ae08745Sheppo 		    md->match_curr.mdp, md->match_curr.mdep[i]);
67791ae08745Sheppo 	for (i = 0; i < md->added.nelem; i++)
67801ae08745Sheppo 		vds_add_vd(vds, md->added.mdp, md->added.mdep[i]);
67811ae08745Sheppo 
67821ae08745Sheppo 	return (MDEG_SUCCESS);
67831ae08745Sheppo }
67841ae08745Sheppo 
67853c96341aSnarayan 
67861ae08745Sheppo static int
67871ae08745Sheppo vds_do_attach(dev_info_t *dip)
67881ae08745Sheppo {
6789445b4c2eSsb155480 	int			status, sz;
6790445b4c2eSsb155480 	int			cfg_handle;
67911ae08745Sheppo 	minor_t			instance = ddi_get_instance(dip);
67921ae08745Sheppo 	vds_t			*vds;
6793445b4c2eSsb155480 	mdeg_prop_spec_t	*pspecp;
6794445b4c2eSsb155480 	mdeg_node_spec_t	*ispecp;
67951ae08745Sheppo 
67961ae08745Sheppo 	/*
67971ae08745Sheppo 	 * The "cfg-handle" property of a vds node in an MD contains the MD's
67981ae08745Sheppo 	 * notion of "instance", or unique identifier, for that node; OBP
67991ae08745Sheppo 	 * stores the value of the "cfg-handle" MD property as the value of
68001ae08745Sheppo 	 * the "reg" property on the node in the device tree it builds from
68011ae08745Sheppo 	 * the MD and passes to Solaris.  Thus, we look up the devinfo node's
68021ae08745Sheppo 	 * "reg" property value to uniquely identify this device instance when
68031ae08745Sheppo 	 * registering with the MD event-generation framework.  If the "reg"
68041ae08745Sheppo 	 * property cannot be found, the device tree state is presumably so
68051ae08745Sheppo 	 * broken that there is no point in continuing.
68061ae08745Sheppo 	 */
6807445b4c2eSsb155480 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
6808445b4c2eSsb155480 	    VD_REG_PROP)) {
6809445b4c2eSsb155480 		PRN("vds \"%s\" property does not exist", VD_REG_PROP);
68101ae08745Sheppo 		return (DDI_FAILURE);
68111ae08745Sheppo 	}
68121ae08745Sheppo 
68131ae08745Sheppo 	/* Get the MD instance for later MDEG registration */
68141ae08745Sheppo 	cfg_handle = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
6815445b4c2eSsb155480 	    VD_REG_PROP, -1);
68161ae08745Sheppo 
68171ae08745Sheppo 	if (ddi_soft_state_zalloc(vds_state, instance) != DDI_SUCCESS) {
68181ae08745Sheppo 		PRN("Could not allocate state for instance %u", instance);
68191ae08745Sheppo 		return (DDI_FAILURE);
68201ae08745Sheppo 	}
68211ae08745Sheppo 
68221ae08745Sheppo 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
68231ae08745Sheppo 		PRN("Could not get state for instance %u", instance);
68241ae08745Sheppo 		ddi_soft_state_free(vds_state, instance);
68251ae08745Sheppo 		return (DDI_FAILURE);
68261ae08745Sheppo 	}
68271ae08745Sheppo 
68281ae08745Sheppo 	vds->dip	= dip;
68291ae08745Sheppo 	vds->vd_table	= mod_hash_create_ptrhash("vds_vd_table", VDS_NCHAINS,
683087a7269eSachartre 	    vds_destroy_vd, sizeof (void *));
683187a7269eSachartre 
68321ae08745Sheppo 	ASSERT(vds->vd_table != NULL);
68331ae08745Sheppo 
68341ae08745Sheppo 	if ((status = ldi_ident_from_dip(dip, &vds->ldi_ident)) != 0) {
68351ae08745Sheppo 		PRN("ldi_ident_from_dip() returned errno %d", status);
68361ae08745Sheppo 		return (DDI_FAILURE);
68371ae08745Sheppo 	}
68381ae08745Sheppo 	vds->initialized |= VDS_LDI;
68391ae08745Sheppo 
68401ae08745Sheppo 	/* Register for MD updates */
6841445b4c2eSsb155480 	sz = sizeof (vds_prop_template);
6842445b4c2eSsb155480 	pspecp = kmem_alloc(sz, KM_SLEEP);
6843445b4c2eSsb155480 	bcopy(vds_prop_template, pspecp, sz);
6844445b4c2eSsb155480 
6845445b4c2eSsb155480 	VDS_SET_MDEG_PROP_INST(pspecp, cfg_handle);
6846445b4c2eSsb155480 
6847445b4c2eSsb155480 	/* initialize the complete prop spec structure */
6848445b4c2eSsb155480 	ispecp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP);
6849445b4c2eSsb155480 	ispecp->namep = "virtual-device";
6850445b4c2eSsb155480 	ispecp->specp = pspecp;
6851445b4c2eSsb155480 
6852445b4c2eSsb155480 	if (mdeg_register(ispecp, &vd_match, vds_process_md, vds,
68531ae08745Sheppo 	    &vds->mdeg) != MDEG_SUCCESS) {
68541ae08745Sheppo 		PRN("Unable to register for MD updates");
6855445b4c2eSsb155480 		kmem_free(ispecp, sizeof (mdeg_node_spec_t));
6856445b4c2eSsb155480 		kmem_free(pspecp, sz);
68571ae08745Sheppo 		return (DDI_FAILURE);
68581ae08745Sheppo 	}
6859445b4c2eSsb155480 
6860445b4c2eSsb155480 	vds->ispecp = ispecp;
68611ae08745Sheppo 	vds->initialized |= VDS_MDEG;
68621ae08745Sheppo 
68630a55fbb7Slm66018 	/* Prevent auto-detaching so driver is available whenever MD changes */
68640a55fbb7Slm66018 	if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1) !=
68650a55fbb7Slm66018 	    DDI_PROP_SUCCESS) {
68660a55fbb7Slm66018 		PRN("failed to set \"%s\" property for instance %u",
68670a55fbb7Slm66018 		    DDI_NO_AUTODETACH, instance);
68680a55fbb7Slm66018 	}
68690a55fbb7Slm66018 
68708fce2fd6Sachartre 	/* read any user defined driver types from conf file and update list */
68718fce2fd6Sachartre 	vds_driver_types_update(vds);
68728fce2fd6Sachartre 
68731ae08745Sheppo 	ddi_report_dev(dip);
68741ae08745Sheppo 	return (DDI_SUCCESS);
68751ae08745Sheppo }
68761ae08745Sheppo 
68771ae08745Sheppo static int
68781ae08745Sheppo vds_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
68791ae08745Sheppo {
68801ae08745Sheppo 	int	status;
68811ae08745Sheppo 
68821ae08745Sheppo 	switch (cmd) {
68831ae08745Sheppo 	case DDI_ATTACH:
6884d10e4ef2Snarayan 		PR0("Attaching");
68851ae08745Sheppo 		if ((status = vds_do_attach(dip)) != DDI_SUCCESS)
68861ae08745Sheppo 			(void) vds_detach(dip, DDI_DETACH);
68871ae08745Sheppo 		return (status);
68881ae08745Sheppo 	case DDI_RESUME:
6889d10e4ef2Snarayan 		PR0("No action required for DDI_RESUME");
68901ae08745Sheppo 		return (DDI_SUCCESS);
68911ae08745Sheppo 	default:
68921ae08745Sheppo 		return (DDI_FAILURE);
68931ae08745Sheppo 	}
68941ae08745Sheppo }
68951ae08745Sheppo 
68961ae08745Sheppo static struct dev_ops vds_ops = {
68971ae08745Sheppo 	DEVO_REV,	/* devo_rev */
68981ae08745Sheppo 	0,		/* devo_refcnt */
68991ae08745Sheppo 	ddi_no_info,	/* devo_getinfo */
69001ae08745Sheppo 	nulldev,	/* devo_identify */
69011ae08745Sheppo 	nulldev,	/* devo_probe */
69021ae08745Sheppo 	vds_attach,	/* devo_attach */
69031ae08745Sheppo 	vds_detach,	/* devo_detach */
69041ae08745Sheppo 	nodev,		/* devo_reset */
69051ae08745Sheppo 	NULL,		/* devo_cb_ops */
69061ae08745Sheppo 	NULL,		/* devo_bus_ops */
69071ae08745Sheppo 	nulldev		/* devo_power */
69081ae08745Sheppo };
69091ae08745Sheppo 
69101ae08745Sheppo static struct modldrv modldrv = {
69111ae08745Sheppo 	&mod_driverops,
6912205eeb1aSlm66018 	"virtual disk server",
69131ae08745Sheppo 	&vds_ops,
69141ae08745Sheppo };
69151ae08745Sheppo 
69161ae08745Sheppo static struct modlinkage modlinkage = {
69171ae08745Sheppo 	MODREV_1,
69181ae08745Sheppo 	&modldrv,
69191ae08745Sheppo 	NULL
69201ae08745Sheppo };
69211ae08745Sheppo 
69221ae08745Sheppo 
69231ae08745Sheppo int
69241ae08745Sheppo _init(void)
69251ae08745Sheppo {
692617cadca8Slm66018 	int		status;
6927d10e4ef2Snarayan 
69281ae08745Sheppo 	if ((status = ddi_soft_state_init(&vds_state, sizeof (vds_t), 1)) != 0)
69291ae08745Sheppo 		return (status);
693017cadca8Slm66018 
69311ae08745Sheppo 	if ((status = mod_install(&modlinkage)) != 0) {
69321ae08745Sheppo 		ddi_soft_state_fini(&vds_state);
69331ae08745Sheppo 		return (status);
69341ae08745Sheppo 	}
69351ae08745Sheppo 
69361ae08745Sheppo 	return (0);
69371ae08745Sheppo }
69381ae08745Sheppo 
69391ae08745Sheppo int
69401ae08745Sheppo _info(struct modinfo *modinfop)
69411ae08745Sheppo {
69421ae08745Sheppo 	return (mod_info(&modlinkage, modinfop));
69431ae08745Sheppo }
69441ae08745Sheppo 
69451ae08745Sheppo int
69461ae08745Sheppo _fini(void)
69471ae08745Sheppo {
69481ae08745Sheppo 	int	status;
69491ae08745Sheppo 
69501ae08745Sheppo 	if ((status = mod_remove(&modlinkage)) != 0)
69511ae08745Sheppo 		return (status);
69521ae08745Sheppo 	ddi_soft_state_fini(&vds_state);
69531ae08745Sheppo 	return (0);
69541ae08745Sheppo }
6955