xref: /titanic_53/usr/src/uts/sun4v/io/vds.c (revision 27ac699d6a5b15c676e0106195cecf7d3c1eea0c)
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 	/*
2204205eeb1aSlm66018 	 * Send the "ack" or "nack" back to the client; if sending the message
2205205eeb1aSlm66018 	 * via LDC fails, arrange to reset both the connection state and LDC
2206205eeb1aSlm66018 	 * itself
2207205eeb1aSlm66018 	 */
2208205eeb1aSlm66018 	PR2("Sending %s",
2209205eeb1aSlm66018 	    (task->msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK");
2210205eeb1aSlm66018 
2211205eeb1aSlm66018 	status = send_msg(task->vd->ldc_handle, task->msg, task->msglen);
2212205eeb1aSlm66018 	switch (status) {
2213205eeb1aSlm66018 	case 0:
2214205eeb1aSlm66018 		break;
2215205eeb1aSlm66018 	case ECONNRESET:
2216205eeb1aSlm66018 		vd_mark_in_reset(task->vd);
2217205eeb1aSlm66018 		break;
2218205eeb1aSlm66018 	default:
2219205eeb1aSlm66018 		PR0("initiating full reset");
2220205eeb1aSlm66018 		vd_need_reset(task->vd, B_TRUE);
2221205eeb1aSlm66018 		break;
2222205eeb1aSlm66018 	}
2223205eeb1aSlm66018 
2224205eeb1aSlm66018 	DTRACE_PROBE1(task__end, vd_task_t *, task);
2225205eeb1aSlm66018 }
2226205eeb1aSlm66018 
2227205eeb1aSlm66018 /*
2228205eeb1aSlm66018  * Description:
2229205eeb1aSlm66018  *	Mark the Dring entry as Done and (if necessary) send an ACK/NACK to
2230205eeb1aSlm66018  *	the vDisk client
2231205eeb1aSlm66018  *
2232205eeb1aSlm66018  * Parameters:
2233205eeb1aSlm66018  *	task 		- structure containing the request sent from client
2234205eeb1aSlm66018  *
2235205eeb1aSlm66018  * Return Values
2236205eeb1aSlm66018  *	None
2237205eeb1aSlm66018  */
2238205eeb1aSlm66018 static void
2239205eeb1aSlm66018 vd_complete_notify(vd_task_t *task)
2240205eeb1aSlm66018 {
2241205eeb1aSlm66018 	int			status		= 0;
2242205eeb1aSlm66018 	vd_t			*vd		= task->vd;
2243205eeb1aSlm66018 	vd_dring_payload_t	*request	= task->request;
2244205eeb1aSlm66018 
2245d10e4ef2Snarayan 	/* Update the dring element for a dring client */
2246f0ca1d9aSsb155480 	if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE_V1_0)) {
22473c96341aSnarayan 		status = vd_mark_elem_done(vd, task->index,
22483c96341aSnarayan 		    request->status, request->nbytes);
22493af08d82Slm66018 		if (status == ECONNRESET)
22503af08d82Slm66018 			vd_mark_in_reset(vd);
2251bbfa0259Sha137994 		else if (status == EACCES)
2252bbfa0259Sha137994 			vd_need_reset(vd, B_TRUE);
22533af08d82Slm66018 	}
22541ae08745Sheppo 
2255d10e4ef2Snarayan 	/*
2256205eeb1aSlm66018 	 * If a transport error occurred while marking the element done or
2257205eeb1aSlm66018 	 * previously while executing the task, arrange to "nack" the message
2258205eeb1aSlm66018 	 * when the final task in the descriptor element range completes
2259d10e4ef2Snarayan 	 */
2260205eeb1aSlm66018 	if ((status != 0) || (task->status != 0))
2261d10e4ef2Snarayan 		task->msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
22621ae08745Sheppo 
2263d10e4ef2Snarayan 	/*
2264d10e4ef2Snarayan 	 * Only the final task for a range of elements will respond to and
2265d10e4ef2Snarayan 	 * free the message
2266d10e4ef2Snarayan 	 */
22673af08d82Slm66018 	if (task->type == VD_NONFINAL_RANGE_TASK) {
2268d10e4ef2Snarayan 		return;
22693af08d82Slm66018 	}
22701ae08745Sheppo 
2271*27ac699dSzk194757 	/*
2272*27ac699dSzk194757 	 * We should only send an ACK/NACK here if we are not currently in
2273*27ac699dSzk194757 	 * reset as, depending on how we reset, the dring may have been
2274*27ac699dSzk194757 	 * blown away and we don't want to ACK/NACK a message that isn't
2275*27ac699dSzk194757 	 * there.
2276*27ac699dSzk194757 	 */
2277*27ac699dSzk194757 	if (!vd->reset_state)
2278205eeb1aSlm66018 		vd_notify(task);
2279205eeb1aSlm66018 }
2280205eeb1aSlm66018 
2281d10e4ef2Snarayan /*
2282205eeb1aSlm66018  * Description:
2283205eeb1aSlm66018  *	This is the basic completion function called to handle inband data
2284205eeb1aSlm66018  *	requests and handshake messages. All it needs to do is trigger a
2285205eeb1aSlm66018  *	message to the client that the request is completed.
2286205eeb1aSlm66018  *
2287205eeb1aSlm66018  * Parameters:
2288205eeb1aSlm66018  *	arg 	- opaque pointer to structure containing task to be completed
2289205eeb1aSlm66018  *
2290205eeb1aSlm66018  * Return Values
2291205eeb1aSlm66018  *	None
2292d10e4ef2Snarayan  */
2293205eeb1aSlm66018 static void
2294205eeb1aSlm66018 vd_serial_notify(void *arg)
2295205eeb1aSlm66018 {
2296205eeb1aSlm66018 	vd_task_t		*task = (vd_task_t *)arg;
2297205eeb1aSlm66018 
2298205eeb1aSlm66018 	ASSERT(task != NULL);
2299205eeb1aSlm66018 	vd_notify(task);
23001ae08745Sheppo }
23011ae08745Sheppo 
23022f5224aeSachartre /* ARGSUSED */
23032f5224aeSachartre static int
23042f5224aeSachartre vd_geom2dk_geom(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
23050a55fbb7Slm66018 {
23060a55fbb7Slm66018 	VD_GEOM2DK_GEOM((vd_geom_t *)vd_buf, (struct dk_geom *)ioctl_arg);
23072f5224aeSachartre 	return (0);
23080a55fbb7Slm66018 }
23090a55fbb7Slm66018 
23102f5224aeSachartre /* ARGSUSED */
23112f5224aeSachartre static int
23122f5224aeSachartre vd_vtoc2vtoc(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
23130a55fbb7Slm66018 {
23140a55fbb7Slm66018 	VD_VTOC2VTOC((vd_vtoc_t *)vd_buf, (struct vtoc *)ioctl_arg);
23152f5224aeSachartre 	return (0);
23160a55fbb7Slm66018 }
23170a55fbb7Slm66018 
23180a55fbb7Slm66018 static void
23190a55fbb7Slm66018 dk_geom2vd_geom(void *ioctl_arg, void *vd_buf)
23200a55fbb7Slm66018 {
23210a55fbb7Slm66018 	DK_GEOM2VD_GEOM((struct dk_geom *)ioctl_arg, (vd_geom_t *)vd_buf);
23220a55fbb7Slm66018 }
23230a55fbb7Slm66018 
23240a55fbb7Slm66018 static void
23250a55fbb7Slm66018 vtoc2vd_vtoc(void *ioctl_arg, void *vd_buf)
23260a55fbb7Slm66018 {
23270a55fbb7Slm66018 	VTOC2VD_VTOC((struct vtoc *)ioctl_arg, (vd_vtoc_t *)vd_buf);
23280a55fbb7Slm66018 }
23290a55fbb7Slm66018 
23302f5224aeSachartre static int
23312f5224aeSachartre vd_get_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
23324bac2208Snarayan {
23334bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
23344bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
23352f5224aeSachartre 	size_t data_len;
23362f5224aeSachartre 
23372f5224aeSachartre 	data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t));
23382f5224aeSachartre 	if (vd_efi->length > data_len)
23392f5224aeSachartre 		return (EINVAL);
23404bac2208Snarayan 
23414bac2208Snarayan 	dk_efi->dki_lba = vd_efi->lba;
23424bac2208Snarayan 	dk_efi->dki_length = vd_efi->length;
23434bac2208Snarayan 	dk_efi->dki_data = kmem_zalloc(vd_efi->length, KM_SLEEP);
23442f5224aeSachartre 	return (0);
23454bac2208Snarayan }
23464bac2208Snarayan 
23474bac2208Snarayan static void
23484bac2208Snarayan vd_get_efi_out(void *ioctl_arg, void *vd_buf)
23494bac2208Snarayan {
23504bac2208Snarayan 	int len;
23514bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
23524bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
23534bac2208Snarayan 
23544bac2208Snarayan 	len = vd_efi->length;
23554bac2208Snarayan 	DK_EFI2VD_EFI(dk_efi, vd_efi);
23564bac2208Snarayan 	kmem_free(dk_efi->dki_data, len);
23574bac2208Snarayan }
23584bac2208Snarayan 
23592f5224aeSachartre static int
23602f5224aeSachartre vd_set_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
23614bac2208Snarayan {
23624bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
23634bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
23642f5224aeSachartre 	size_t data_len;
23652f5224aeSachartre 
23662f5224aeSachartre 	data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t));
23672f5224aeSachartre 	if (vd_efi->length > data_len)
23682f5224aeSachartre 		return (EINVAL);
23694bac2208Snarayan 
23704bac2208Snarayan 	dk_efi->dki_data = kmem_alloc(vd_efi->length, KM_SLEEP);
23714bac2208Snarayan 	VD_EFI2DK_EFI(vd_efi, dk_efi);
23722f5224aeSachartre 	return (0);
23734bac2208Snarayan }
23744bac2208Snarayan 
23754bac2208Snarayan static void
23764bac2208Snarayan vd_set_efi_out(void *ioctl_arg, void *vd_buf)
23774bac2208Snarayan {
23784bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
23794bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
23804bac2208Snarayan 
23814bac2208Snarayan 	kmem_free(dk_efi->dki_data, vd_efi->length);
23824bac2208Snarayan }
23834bac2208Snarayan 
23842f5224aeSachartre static int
23852f5224aeSachartre vd_scsicmd_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
23862f5224aeSachartre {
23872f5224aeSachartre 	size_t vd_scsi_len;
23882f5224aeSachartre 	vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf;
23892f5224aeSachartre 	struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg;
23902f5224aeSachartre 
23912f5224aeSachartre 	/* check buffer size */
23922f5224aeSachartre 	vd_scsi_len = VD_SCSI_SIZE;
23932f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(vd_scsi->cdb_len, sizeof (uint64_t));
23942f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(vd_scsi->sense_len, sizeof (uint64_t));
23952f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(vd_scsi->datain_len, sizeof (uint64_t));
23962f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(vd_scsi->dataout_len, sizeof (uint64_t));
23972f5224aeSachartre 
23982f5224aeSachartre 	ASSERT(vd_scsi_len % sizeof (uint64_t) == 0);
23992f5224aeSachartre 
24002f5224aeSachartre 	if (vd_buf_len < vd_scsi_len)
24012f5224aeSachartre 		return (EINVAL);
24022f5224aeSachartre 
24032f5224aeSachartre 	/* set flags */
24042f5224aeSachartre 	uscsi->uscsi_flags = vd_scsi_debug;
24052f5224aeSachartre 
24062f5224aeSachartre 	if (vd_scsi->options & VD_SCSI_OPT_NORETRY) {
24072f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_ISOLATE;
24082f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_DIAGNOSE;
24092f5224aeSachartre 	}
24102f5224aeSachartre 
24112f5224aeSachartre 	/* task attribute */
24122f5224aeSachartre 	switch (vd_scsi->task_attribute) {
24132f5224aeSachartre 	case VD_SCSI_TASK_ACA:
24142f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_HEAD;
24152f5224aeSachartre 		break;
24162f5224aeSachartre 	case VD_SCSI_TASK_HQUEUE:
24172f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_HTAG;
24182f5224aeSachartre 		break;
24192f5224aeSachartre 	case VD_SCSI_TASK_ORDERED:
24202f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_OTAG;
24212f5224aeSachartre 		break;
24222f5224aeSachartre 	default:
24232f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_NOTAG;
24242f5224aeSachartre 		break;
24252f5224aeSachartre 	}
24262f5224aeSachartre 
24272f5224aeSachartre 	/* timeout */
24282f5224aeSachartre 	uscsi->uscsi_timeout = vd_scsi->timeout;
24292f5224aeSachartre 
24302f5224aeSachartre 	/* cdb data */
24312f5224aeSachartre 	uscsi->uscsi_cdb = (caddr_t)VD_SCSI_DATA_CDB(vd_scsi);
24322f5224aeSachartre 	uscsi->uscsi_cdblen = vd_scsi->cdb_len;
24332f5224aeSachartre 
24342f5224aeSachartre 	/* sense buffer */
24352f5224aeSachartre 	if (vd_scsi->sense_len != 0) {
24362f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_RQENABLE;
24372f5224aeSachartre 		uscsi->uscsi_rqbuf = (caddr_t)VD_SCSI_DATA_SENSE(vd_scsi);
24382f5224aeSachartre 		uscsi->uscsi_rqlen = vd_scsi->sense_len;
24392f5224aeSachartre 	}
24402f5224aeSachartre 
24412f5224aeSachartre 	if (vd_scsi->datain_len != 0 && vd_scsi->dataout_len != 0) {
24422f5224aeSachartre 		/* uscsi does not support read/write request */
24432f5224aeSachartre 		return (EINVAL);
24442f5224aeSachartre 	}
24452f5224aeSachartre 
24462f5224aeSachartre 	/* request data-in */
24472f5224aeSachartre 	if (vd_scsi->datain_len != 0) {
24482f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_READ;
24492f5224aeSachartre 		uscsi->uscsi_buflen = vd_scsi->datain_len;
24502f5224aeSachartre 		uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_IN(vd_scsi);
24512f5224aeSachartre 	}
24522f5224aeSachartre 
24532f5224aeSachartre 	/* request data-out */
24542f5224aeSachartre 	if (vd_scsi->dataout_len != 0) {
24552f5224aeSachartre 		uscsi->uscsi_buflen = vd_scsi->dataout_len;
24562f5224aeSachartre 		uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_OUT(vd_scsi);
24572f5224aeSachartre 	}
24582f5224aeSachartre 
24592f5224aeSachartre 	return (0);
24602f5224aeSachartre }
24612f5224aeSachartre 
24622f5224aeSachartre static void
24632f5224aeSachartre vd_scsicmd_out(void *ioctl_arg, void *vd_buf)
24642f5224aeSachartre {
24652f5224aeSachartre 	vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf;
24662f5224aeSachartre 	struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg;
24672f5224aeSachartre 
24682f5224aeSachartre 	/* output fields */
24692f5224aeSachartre 	vd_scsi->cmd_status = uscsi->uscsi_status;
24702f5224aeSachartre 
24712f5224aeSachartre 	/* sense data */
24722f5224aeSachartre 	if ((uscsi->uscsi_flags & USCSI_RQENABLE) &&
24732f5224aeSachartre 	    (uscsi->uscsi_status == STATUS_CHECK ||
24742f5224aeSachartre 	    uscsi->uscsi_status == STATUS_TERMINATED)) {
24752f5224aeSachartre 		vd_scsi->sense_status = uscsi->uscsi_rqstatus;
24762f5224aeSachartre 		if (uscsi->uscsi_rqstatus == STATUS_GOOD)
247714466a20Szk194757 			vd_scsi->sense_len -= uscsi->uscsi_rqresid;
24782f5224aeSachartre 		else
24792f5224aeSachartre 			vd_scsi->sense_len = 0;
24802f5224aeSachartre 	} else {
24812f5224aeSachartre 		vd_scsi->sense_len = 0;
24822f5224aeSachartre 	}
24832f5224aeSachartre 
24842f5224aeSachartre 	if (uscsi->uscsi_status != STATUS_GOOD) {
24852f5224aeSachartre 		vd_scsi->dataout_len = 0;
24862f5224aeSachartre 		vd_scsi->datain_len = 0;
24872f5224aeSachartre 		return;
24882f5224aeSachartre 	}
24892f5224aeSachartre 
24902f5224aeSachartre 	if (uscsi->uscsi_flags & USCSI_READ) {
24912f5224aeSachartre 		/* request data (read) */
24922f5224aeSachartre 		vd_scsi->datain_len -= uscsi->uscsi_resid;
24932f5224aeSachartre 		vd_scsi->dataout_len = 0;
24942f5224aeSachartre 	} else {
24952f5224aeSachartre 		/* request data (write) */
24962f5224aeSachartre 		vd_scsi->datain_len = 0;
24972f5224aeSachartre 		vd_scsi->dataout_len -= uscsi->uscsi_resid;
24982f5224aeSachartre 	}
24992f5224aeSachartre }
25002f5224aeSachartre 
2501690555a1Sachartre static ushort_t
25023c96341aSnarayan vd_lbl2cksum(struct dk_label *label)
25033c96341aSnarayan {
25043c96341aSnarayan 	int	count;
2505690555a1Sachartre 	ushort_t sum, *sp;
25063c96341aSnarayan 
25073c96341aSnarayan 	count =	(sizeof (struct dk_label)) / (sizeof (short)) - 1;
2508690555a1Sachartre 	sp = (ushort_t *)label;
25093c96341aSnarayan 	sum = 0;
25103c96341aSnarayan 	while (count--) {
25113c96341aSnarayan 		sum ^= *sp++;
25123c96341aSnarayan 	}
25133c96341aSnarayan 
25143c96341aSnarayan 	return (sum);
25153c96341aSnarayan }
25163c96341aSnarayan 
251787a7269eSachartre /*
2518bae9e67eSachartre  * Copy information from a vtoc and dk_geom structures to a dk_label structure.
2519bae9e67eSachartre  */
2520bae9e67eSachartre static void
2521bae9e67eSachartre vd_vtocgeom_to_label(struct vtoc *vtoc, struct dk_geom *geom,
2522bae9e67eSachartre     struct dk_label *label)
2523bae9e67eSachartre {
2524bae9e67eSachartre 	int i;
2525bae9e67eSachartre 
2526bae9e67eSachartre 	ASSERT(vtoc->v_nparts == V_NUMPAR);
2527bae9e67eSachartre 	ASSERT(vtoc->v_sanity == VTOC_SANE);
2528bae9e67eSachartre 
2529bae9e67eSachartre 	bzero(label, sizeof (struct dk_label));
2530bae9e67eSachartre 
2531bae9e67eSachartre 	label->dkl_ncyl = geom->dkg_ncyl;
2532bae9e67eSachartre 	label->dkl_acyl = geom->dkg_acyl;
2533bae9e67eSachartre 	label->dkl_pcyl = geom->dkg_pcyl;
2534bae9e67eSachartre 	label->dkl_nhead = geom->dkg_nhead;
2535bae9e67eSachartre 	label->dkl_nsect = geom->dkg_nsect;
2536bae9e67eSachartre 	label->dkl_intrlv = geom->dkg_intrlv;
2537bae9e67eSachartre 	label->dkl_apc = geom->dkg_apc;
2538bae9e67eSachartre 	label->dkl_rpm = geom->dkg_rpm;
2539bae9e67eSachartre 	label->dkl_write_reinstruct = geom->dkg_write_reinstruct;
2540bae9e67eSachartre 	label->dkl_read_reinstruct = geom->dkg_read_reinstruct;
2541bae9e67eSachartre 
2542bae9e67eSachartre 	label->dkl_vtoc.v_nparts = V_NUMPAR;
2543bae9e67eSachartre 	label->dkl_vtoc.v_sanity = VTOC_SANE;
2544bae9e67eSachartre 	label->dkl_vtoc.v_version = vtoc->v_version;
2545bae9e67eSachartre 	for (i = 0; i < V_NUMPAR; i++) {
2546bae9e67eSachartre 		label->dkl_vtoc.v_timestamp[i] = vtoc->timestamp[i];
2547bae9e67eSachartre 		label->dkl_vtoc.v_part[i].p_tag = vtoc->v_part[i].p_tag;
2548bae9e67eSachartre 		label->dkl_vtoc.v_part[i].p_flag = vtoc->v_part[i].p_flag;
2549bae9e67eSachartre 		label->dkl_map[i].dkl_cylno = vtoc->v_part[i].p_start /
2550bae9e67eSachartre 		    (label->dkl_nhead * label->dkl_nsect);
2551bae9e67eSachartre 		label->dkl_map[i].dkl_nblk = vtoc->v_part[i].p_size;
2552bae9e67eSachartre 	}
2553bae9e67eSachartre 
2554bae9e67eSachartre 	/*
2555bae9e67eSachartre 	 * The bootinfo array can not be copied with bcopy() because
2556bae9e67eSachartre 	 * elements are of type long in vtoc (so 64-bit) and of type
2557bae9e67eSachartre 	 * int in dk_vtoc (so 32-bit).
2558bae9e67eSachartre 	 */
2559bae9e67eSachartre 	label->dkl_vtoc.v_bootinfo[0] = vtoc->v_bootinfo[0];
2560bae9e67eSachartre 	label->dkl_vtoc.v_bootinfo[1] = vtoc->v_bootinfo[1];
2561bae9e67eSachartre 	label->dkl_vtoc.v_bootinfo[2] = vtoc->v_bootinfo[2];
2562bae9e67eSachartre 	bcopy(vtoc->v_asciilabel, label->dkl_asciilabel, LEN_DKL_ASCII);
2563bae9e67eSachartre 	bcopy(vtoc->v_volume, label->dkl_vtoc.v_volume, LEN_DKL_VVOL);
2564bae9e67eSachartre 
2565bae9e67eSachartre 	/* re-compute checksum */
2566bae9e67eSachartre 	label->dkl_magic = DKL_MAGIC;
2567bae9e67eSachartre 	label->dkl_cksum = vd_lbl2cksum(label);
2568bae9e67eSachartre }
2569bae9e67eSachartre 
2570bae9e67eSachartre /*
2571bae9e67eSachartre  * Copy information from a dk_label structure to a vtoc and dk_geom structures.
2572bae9e67eSachartre  */
2573bae9e67eSachartre static void
2574bae9e67eSachartre vd_label_to_vtocgeom(struct dk_label *label, struct vtoc *vtoc,
2575bae9e67eSachartre     struct dk_geom *geom)
2576bae9e67eSachartre {
2577bae9e67eSachartre 	int i;
2578bae9e67eSachartre 
2579bae9e67eSachartre 	bzero(vtoc, sizeof (struct vtoc));
2580bae9e67eSachartre 	bzero(geom, sizeof (struct dk_geom));
2581bae9e67eSachartre 
2582bae9e67eSachartre 	geom->dkg_ncyl = label->dkl_ncyl;
2583bae9e67eSachartre 	geom->dkg_acyl = label->dkl_acyl;
2584bae9e67eSachartre 	geom->dkg_nhead = label->dkl_nhead;
2585bae9e67eSachartre 	geom->dkg_nsect = label->dkl_nsect;
2586bae9e67eSachartre 	geom->dkg_intrlv = label->dkl_intrlv;
2587bae9e67eSachartre 	geom->dkg_apc = label->dkl_apc;
2588bae9e67eSachartre 	geom->dkg_rpm = label->dkl_rpm;
2589bae9e67eSachartre 	geom->dkg_pcyl = label->dkl_pcyl;
2590bae9e67eSachartre 	geom->dkg_write_reinstruct = label->dkl_write_reinstruct;
2591bae9e67eSachartre 	geom->dkg_read_reinstruct = label->dkl_read_reinstruct;
2592bae9e67eSachartre 
2593bae9e67eSachartre 	vtoc->v_sanity = label->dkl_vtoc.v_sanity;
2594bae9e67eSachartre 	vtoc->v_version = label->dkl_vtoc.v_version;
2595bae9e67eSachartre 	vtoc->v_sectorsz = DEV_BSIZE;
2596bae9e67eSachartre 	vtoc->v_nparts = label->dkl_vtoc.v_nparts;
2597bae9e67eSachartre 
2598bae9e67eSachartre 	for (i = 0; i < vtoc->v_nparts; i++) {
2599bae9e67eSachartre 		vtoc->v_part[i].p_tag = label->dkl_vtoc.v_part[i].p_tag;
2600bae9e67eSachartre 		vtoc->v_part[i].p_flag = label->dkl_vtoc.v_part[i].p_flag;
2601bae9e67eSachartre 		vtoc->v_part[i].p_start = label->dkl_map[i].dkl_cylno *
2602bae9e67eSachartre 		    (label->dkl_nhead * label->dkl_nsect);
2603bae9e67eSachartre 		vtoc->v_part[i].p_size = label->dkl_map[i].dkl_nblk;
2604bae9e67eSachartre 		vtoc->timestamp[i] = label->dkl_vtoc.v_timestamp[i];
2605bae9e67eSachartre 	}
2606bae9e67eSachartre 
2607bae9e67eSachartre 	/*
2608bae9e67eSachartre 	 * The bootinfo array can not be copied with bcopy() because
2609bae9e67eSachartre 	 * elements are of type long in vtoc (so 64-bit) and of type
2610bae9e67eSachartre 	 * int in dk_vtoc (so 32-bit).
2611bae9e67eSachartre 	 */
2612bae9e67eSachartre 	vtoc->v_bootinfo[0] = label->dkl_vtoc.v_bootinfo[0];
2613bae9e67eSachartre 	vtoc->v_bootinfo[1] = label->dkl_vtoc.v_bootinfo[1];
2614bae9e67eSachartre 	vtoc->v_bootinfo[2] = label->dkl_vtoc.v_bootinfo[2];
2615bae9e67eSachartre 	bcopy(label->dkl_asciilabel, vtoc->v_asciilabel, LEN_DKL_ASCII);
2616bae9e67eSachartre 	bcopy(label->dkl_vtoc.v_volume, vtoc->v_volume, LEN_DKL_VVOL);
2617bae9e67eSachartre }
2618bae9e67eSachartre 
2619bae9e67eSachartre /*
2620bae9e67eSachartre  * Check if a geometry is valid for a single-slice disk. A geometry is
2621bae9e67eSachartre  * considered valid if the main attributes of the geometry match with the
2622bae9e67eSachartre  * attributes of the fake geometry we have created.
2623bae9e67eSachartre  */
2624bae9e67eSachartre static boolean_t
2625bae9e67eSachartre vd_slice_geom_isvalid(vd_t *vd, struct dk_geom *geom)
2626bae9e67eSachartre {
2627bae9e67eSachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
2628bae9e67eSachartre 	ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
2629bae9e67eSachartre 
2630bae9e67eSachartre 	if (geom->dkg_ncyl != vd->dk_geom.dkg_ncyl ||
2631bae9e67eSachartre 	    geom->dkg_acyl != vd->dk_geom.dkg_acyl ||
2632bae9e67eSachartre 	    geom->dkg_nsect != vd->dk_geom.dkg_nsect ||
2633bae9e67eSachartre 	    geom->dkg_pcyl != vd->dk_geom.dkg_pcyl)
2634bae9e67eSachartre 		return (B_FALSE);
2635bae9e67eSachartre 
2636bae9e67eSachartre 	return (B_TRUE);
2637bae9e67eSachartre }
2638bae9e67eSachartre 
2639bae9e67eSachartre /*
2640bae9e67eSachartre  * Check if a vtoc is valid for a single-slice disk. A vtoc is considered
2641bae9e67eSachartre  * valid if the main attributes of the vtoc match with the attributes of the
2642bae9e67eSachartre  * fake vtoc we have created.
2643bae9e67eSachartre  */
2644bae9e67eSachartre static boolean_t
2645bae9e67eSachartre vd_slice_vtoc_isvalid(vd_t *vd, struct vtoc *vtoc)
2646bae9e67eSachartre {
2647bae9e67eSachartre 	size_t csize;
2648bae9e67eSachartre 	int i;
2649bae9e67eSachartre 
2650bae9e67eSachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
2651bae9e67eSachartre 	ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
2652bae9e67eSachartre 
2653bae9e67eSachartre 	if (vtoc->v_sanity != vd->vtoc.v_sanity ||
2654bae9e67eSachartre 	    vtoc->v_version != vd->vtoc.v_version ||
2655bae9e67eSachartre 	    vtoc->v_nparts != vd->vtoc.v_nparts ||
2656bae9e67eSachartre 	    strcmp(vtoc->v_volume, vd->vtoc.v_volume) != 0 ||
2657bae9e67eSachartre 	    strcmp(vtoc->v_asciilabel, vd->vtoc.v_asciilabel) != 0)
2658bae9e67eSachartre 		return (B_FALSE);
2659bae9e67eSachartre 
2660bae9e67eSachartre 	/* slice 2 should be unchanged */
2661bae9e67eSachartre 	if (vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_start !=
2662bae9e67eSachartre 	    vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_start ||
2663bae9e67eSachartre 	    vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_size !=
2664bae9e67eSachartre 	    vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_size)
2665bae9e67eSachartre 		return (B_FALSE);
2666bae9e67eSachartre 
2667bae9e67eSachartre 	/*
2668bae9e67eSachartre 	 * Slice 0 should be mostly unchanged and cover most of the disk.
2669bae9e67eSachartre 	 * However we allow some flexibility wrt to the start and the size
2670bae9e67eSachartre 	 * of this slice mainly because we can't exactly know how it will
2671bae9e67eSachartre 	 * be defined by the OS installer.
2672bae9e67eSachartre 	 *
2673bae9e67eSachartre 	 * We allow slice 0 to be defined as starting on any of the first
2674bae9e67eSachartre 	 * 4 cylinders.
2675bae9e67eSachartre 	 */
2676bae9e67eSachartre 	csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
2677bae9e67eSachartre 
2678bae9e67eSachartre 	if (vtoc->v_part[0].p_start > 4 * csize ||
2679bae9e67eSachartre 	    vtoc->v_part[0].p_size > vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_size)
2680bae9e67eSachartre 			return (B_FALSE);
2681bae9e67eSachartre 
2682bae9e67eSachartre 	if (vd->vtoc.v_part[0].p_size >= 4 * csize &&
2683bae9e67eSachartre 	    vtoc->v_part[0].p_size < vd->vtoc.v_part[0].p_size - 4 *csize)
2684bae9e67eSachartre 			return (B_FALSE);
2685bae9e67eSachartre 
2686bae9e67eSachartre 	/* any other slice should have a size of 0 */
2687bae9e67eSachartre 	for (i = 1; i < vtoc->v_nparts; i++) {
2688bae9e67eSachartre 		if (i != VD_ENTIRE_DISK_SLICE &&
2689bae9e67eSachartre 		    vtoc->v_part[i].p_size != 0)
2690bae9e67eSachartre 			return (B_FALSE);
2691bae9e67eSachartre 	}
2692bae9e67eSachartre 
2693bae9e67eSachartre 	return (B_TRUE);
2694bae9e67eSachartre }
2695bae9e67eSachartre 
2696bae9e67eSachartre /*
269787a7269eSachartre  * Handle ioctls to a disk slice.
2698205eeb1aSlm66018  *
2699205eeb1aSlm66018  * Return Values
2700205eeb1aSlm66018  *	0	- Indicates that there are no errors in disk operations
2701205eeb1aSlm66018  *	ENOTSUP	- Unknown disk label type or unsupported DKIO ioctl
2702205eeb1aSlm66018  *	EINVAL	- Not enough room to copy the EFI label
2703205eeb1aSlm66018  *
270487a7269eSachartre  */
27051ae08745Sheppo static int
27060a55fbb7Slm66018 vd_do_slice_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
27071ae08745Sheppo {
27084bac2208Snarayan 	dk_efi_t *dk_ioc;
2709bae9e67eSachartre 	struct vtoc *vtoc;
2710bae9e67eSachartre 	struct dk_geom *geom;
2711bae9e67eSachartre 	size_t len, lba;
2712edcc0754Sachartre 	int rval;
2713edcc0754Sachartre 
2714edcc0754Sachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
2715edcc0754Sachartre 
2716edcc0754Sachartre 	if (cmd == DKIOCFLUSHWRITECACHE) {
2717edcc0754Sachartre 		if (vd->file) {
2718edcc0754Sachartre 			return (VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL));
2719edcc0754Sachartre 		} else {
2720edcc0754Sachartre 			return (ldi_ioctl(vd->ldi_handle[0], cmd,
2721edcc0754Sachartre 			    (intptr_t)ioctl_arg, vd->open_flags | FKIOCTL,
2722edcc0754Sachartre 			    kcred, &rval));
2723edcc0754Sachartre 		}
2724edcc0754Sachartre 	}
27254bac2208Snarayan 
27264bac2208Snarayan 	switch (vd->vdisk_label) {
27274bac2208Snarayan 
2728edcc0754Sachartre 	/* ioctls for a single slice disk with a VTOC label */
27294bac2208Snarayan 	case VD_DISK_LABEL_VTOC:
27304bac2208Snarayan 
27311ae08745Sheppo 		switch (cmd) {
2732bae9e67eSachartre 
27331ae08745Sheppo 		case DKIOCGGEOM:
27340a55fbb7Slm66018 			ASSERT(ioctl_arg != NULL);
27350a55fbb7Slm66018 			bcopy(&vd->dk_geom, ioctl_arg, sizeof (vd->dk_geom));
27361ae08745Sheppo 			return (0);
2737bae9e67eSachartre 
27381ae08745Sheppo 		case DKIOCGVTOC:
27390a55fbb7Slm66018 			ASSERT(ioctl_arg != NULL);
27400a55fbb7Slm66018 			bcopy(&vd->vtoc, ioctl_arg, sizeof (vd->vtoc));
27411ae08745Sheppo 			return (0);
2742bae9e67eSachartre 
2743bae9e67eSachartre 		case DKIOCSGEOM:
2744bae9e67eSachartre 			ASSERT(ioctl_arg != NULL);
2745bae9e67eSachartre 			if (vd_slice_single_slice)
2746bae9e67eSachartre 				return (ENOTSUP);
2747bae9e67eSachartre 
2748bae9e67eSachartre 			/* fake success only if new geometry is valid */
2749bae9e67eSachartre 			geom = (struct dk_geom *)ioctl_arg;
2750bae9e67eSachartre 			if (!vd_slice_geom_isvalid(vd, geom))
2751bae9e67eSachartre 				return (EINVAL);
2752bae9e67eSachartre 
2753bae9e67eSachartre 			return (0);
2754bae9e67eSachartre 
2755bae9e67eSachartre 		case DKIOCSVTOC:
2756bae9e67eSachartre 			ASSERT(ioctl_arg != NULL);
2757bae9e67eSachartre 			if (vd_slice_single_slice)
2758bae9e67eSachartre 				return (ENOTSUP);
2759bae9e67eSachartre 
2760bae9e67eSachartre 			/* fake sucess only if the new vtoc is valid */
2761bae9e67eSachartre 			vtoc = (struct vtoc *)ioctl_arg;
2762bae9e67eSachartre 			if (!vd_slice_vtoc_isvalid(vd, vtoc))
2763bae9e67eSachartre 				return (EINVAL);
2764bae9e67eSachartre 
2765bae9e67eSachartre 			return (0);
2766bae9e67eSachartre 
276787a7269eSachartre 		default:
27683c96341aSnarayan 			return (ENOTSUP);
276987a7269eSachartre 		}
277087a7269eSachartre 
2771edcc0754Sachartre 	/* ioctls for a single slice disk with an EFI label */
277287a7269eSachartre 	case VD_DISK_LABEL_EFI:
277387a7269eSachartre 
2774bae9e67eSachartre 		if (cmd != DKIOCGETEFI && cmd != DKIOCSETEFI)
2775bae9e67eSachartre 			return (ENOTSUP);
2776bae9e67eSachartre 
27773c96341aSnarayan 		ASSERT(ioctl_arg != NULL);
277887a7269eSachartre 		dk_ioc = (dk_efi_t *)ioctl_arg;
2779edcc0754Sachartre 
2780bae9e67eSachartre 		len = dk_ioc->dki_length;
2781bae9e67eSachartre 		lba = dk_ioc->dki_lba;
2782edcc0754Sachartre 
2783bae9e67eSachartre 		if ((lba != VD_EFI_LBA_GPT && lba != VD_EFI_LBA_GPE) ||
2784bae9e67eSachartre 		    (lba == VD_EFI_LBA_GPT && len < sizeof (efi_gpt_t)) ||
2785bae9e67eSachartre 		    (lba == VD_EFI_LBA_GPE && len < sizeof (efi_gpe_t)))
278687a7269eSachartre 			return (EINVAL);
2787edcc0754Sachartre 
2788bae9e67eSachartre 		switch (cmd) {
2789bae9e67eSachartre 		case DKIOCGETEFI:
2790bae9e67eSachartre 			len = vd_slice_flabel_read(vd,
2791bae9e67eSachartre 			    (caddr_t)dk_ioc->dki_data, lba * DEV_BSIZE, len);
2792edcc0754Sachartre 
2793bae9e67eSachartre 			ASSERT(len > 0);
2794edcc0754Sachartre 
279587a7269eSachartre 			return (0);
2796bae9e67eSachartre 
2797bae9e67eSachartre 		case DKIOCSETEFI:
2798bae9e67eSachartre 			if (vd_slice_single_slice)
279987a7269eSachartre 				return (ENOTSUP);
2800bae9e67eSachartre 
2801bae9e67eSachartre 			/* we currently don't support writing EFI */
2802bae9e67eSachartre 			return (EIO);
280387a7269eSachartre 		}
280487a7269eSachartre 
280587a7269eSachartre 	default:
2806205eeb1aSlm66018 		/* Unknown disk label type */
280787a7269eSachartre 		return (ENOTSUP);
280887a7269eSachartre 	}
280987a7269eSachartre }
281087a7269eSachartre 
2811edcc0754Sachartre static int
2812edcc0754Sachartre vds_efi_alloc_and_read(vd_t *vd, efi_gpt_t **gpt, efi_gpe_t **gpe)
2813edcc0754Sachartre {
2814edcc0754Sachartre 	vd_efi_dev_t edev;
2815edcc0754Sachartre 	int status;
2816edcc0754Sachartre 
2817edcc0754Sachartre 	VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl);
2818edcc0754Sachartre 
2819edcc0754Sachartre 	status = vd_efi_alloc_and_read(&edev, gpt, gpe);
2820edcc0754Sachartre 
2821edcc0754Sachartre 	return (status);
2822edcc0754Sachartre }
2823edcc0754Sachartre 
2824edcc0754Sachartre static void
2825edcc0754Sachartre vds_efi_free(vd_t *vd, efi_gpt_t *gpt, efi_gpe_t *gpe)
2826edcc0754Sachartre {
2827edcc0754Sachartre 	vd_efi_dev_t edev;
2828edcc0754Sachartre 
2829edcc0754Sachartre 	VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl);
2830edcc0754Sachartre 
2831edcc0754Sachartre 	vd_efi_free(&edev, gpt, gpe);
2832edcc0754Sachartre }
2833edcc0754Sachartre 
2834edcc0754Sachartre static int
2835edcc0754Sachartre vd_file_validate_efi(vd_t *vd)
2836edcc0754Sachartre {
2837edcc0754Sachartre 	efi_gpt_t *gpt;
2838edcc0754Sachartre 	efi_gpe_t *gpe;
2839edcc0754Sachartre 	int i, nparts, status;
2840edcc0754Sachartre 	struct uuid efi_reserved = EFI_RESERVED;
2841edcc0754Sachartre 
2842edcc0754Sachartre 	if ((status = vds_efi_alloc_and_read(vd, &gpt, &gpe)) != 0)
2843edcc0754Sachartre 		return (status);
2844edcc0754Sachartre 
2845edcc0754Sachartre 	bzero(&vd->vtoc, sizeof (struct vtoc));
2846edcc0754Sachartre 	bzero(&vd->dk_geom, sizeof (struct dk_geom));
2847edcc0754Sachartre 	bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART);
2848edcc0754Sachartre 
2849edcc0754Sachartre 	vd->efi_reserved = -1;
2850edcc0754Sachartre 
2851edcc0754Sachartre 	nparts = gpt->efi_gpt_NumberOfPartitionEntries;
2852edcc0754Sachartre 
2853edcc0754Sachartre 	for (i = 0; i < nparts && i < VD_MAXPART; i++) {
2854edcc0754Sachartre 
2855edcc0754Sachartre 		if (gpe[i].efi_gpe_StartingLBA == 0 ||
2856edcc0754Sachartre 		    gpe[i].efi_gpe_EndingLBA == 0) {
2857edcc0754Sachartre 			continue;
2858edcc0754Sachartre 		}
2859edcc0754Sachartre 
2860edcc0754Sachartre 		vd->slices[i].start = gpe[i].efi_gpe_StartingLBA;
2861edcc0754Sachartre 		vd->slices[i].nblocks = gpe[i].efi_gpe_EndingLBA -
2862edcc0754Sachartre 		    gpe[i].efi_gpe_StartingLBA + 1;
2863edcc0754Sachartre 
2864edcc0754Sachartre 		if (bcmp(&gpe[i].efi_gpe_PartitionTypeGUID, &efi_reserved,
2865edcc0754Sachartre 		    sizeof (struct uuid)) == 0)
2866edcc0754Sachartre 			vd->efi_reserved = i;
2867edcc0754Sachartre 
2868edcc0754Sachartre 	}
2869edcc0754Sachartre 
2870edcc0754Sachartre 	ASSERT(vd->vdisk_size != 0);
2871edcc0754Sachartre 	vd->slices[VD_EFI_WD_SLICE].start = 0;
2872edcc0754Sachartre 	vd->slices[VD_EFI_WD_SLICE].nblocks = vd->vdisk_size;
2873edcc0754Sachartre 
2874edcc0754Sachartre 	vds_efi_free(vd, gpt, gpe);
2875edcc0754Sachartre 
2876edcc0754Sachartre 	return (status);
2877edcc0754Sachartre }
2878edcc0754Sachartre 
287987a7269eSachartre /*
288078fcd0a1Sachartre  * Function:
288178fcd0a1Sachartre  *	vd_file_validate_geometry
2882205eeb1aSlm66018  *
288378fcd0a1Sachartre  * Description:
288478fcd0a1Sachartre  *	Read the label and validate the geometry of a disk image. The driver
288578fcd0a1Sachartre  *	label, vtoc and geometry information are updated according to the
288678fcd0a1Sachartre  *	label read from the disk image.
288778fcd0a1Sachartre  *
288878fcd0a1Sachartre  *	If no valid label is found, the label is set to unknown and the
288978fcd0a1Sachartre  *	function returns EINVAL, but a default vtoc and geometry are provided
2890edcc0754Sachartre  *	to the driver. If an EFI label is found, ENOTSUP is returned.
289178fcd0a1Sachartre  *
289278fcd0a1Sachartre  * Parameters:
289378fcd0a1Sachartre  *	vd	- disk on which the operation is performed.
289478fcd0a1Sachartre  *
289578fcd0a1Sachartre  * Return Code:
289678fcd0a1Sachartre  *	0	- success.
289778fcd0a1Sachartre  *	EIO	- error reading the label from the disk image.
289878fcd0a1Sachartre  *	EINVAL	- unknown disk label.
2899edcc0754Sachartre  *	ENOTSUP	- geometry not applicable (EFI label).
290087a7269eSachartre  */
290187a7269eSachartre static int
290278fcd0a1Sachartre vd_file_validate_geometry(vd_t *vd)
290387a7269eSachartre {
290487a7269eSachartre 	struct dk_label label;
290578fcd0a1Sachartre 	struct dk_geom *geom = &vd->dk_geom;
290678fcd0a1Sachartre 	struct vtoc *vtoc = &vd->vtoc;
290778fcd0a1Sachartre 	int i;
290878fcd0a1Sachartre 	int status = 0;
290987a7269eSachartre 
291087a7269eSachartre 	ASSERT(vd->file);
2911edcc0754Sachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
291287a7269eSachartre 
291387a7269eSachartre 	if (VD_FILE_LABEL_READ(vd, &label) < 0)
291487a7269eSachartre 		return (EIO);
291587a7269eSachartre 
291687a7269eSachartre 	if (label.dkl_magic != DKL_MAGIC ||
291778fcd0a1Sachartre 	    label.dkl_cksum != vd_lbl2cksum(&label) ||
291866cfcfbeSachartre 	    (vd_file_validate_sanity && label.dkl_vtoc.v_sanity != VTOC_SANE) ||
291978fcd0a1Sachartre 	    label.dkl_vtoc.v_nparts != V_NUMPAR) {
2920edcc0754Sachartre 
2921edcc0754Sachartre 		if (vd_file_validate_efi(vd) == 0) {
2922edcc0754Sachartre 			vd->vdisk_label = VD_DISK_LABEL_EFI;
2923edcc0754Sachartre 			return (ENOTSUP);
2924edcc0754Sachartre 		}
2925edcc0754Sachartre 
292678fcd0a1Sachartre 		vd->vdisk_label = VD_DISK_LABEL_UNK;
2927bae9e67eSachartre 		vd_build_default_label(vd->file_size, &label);
292878fcd0a1Sachartre 		status = EINVAL;
292978fcd0a1Sachartre 	} else {
293078fcd0a1Sachartre 		vd->vdisk_label = VD_DISK_LABEL_VTOC;
293178fcd0a1Sachartre 	}
293287a7269eSachartre 
2933bae9e67eSachartre 	/* Update the driver geometry and vtoc */
2934bae9e67eSachartre 	vd_label_to_vtocgeom(&label, vtoc, geom);
293587a7269eSachartre 
2936edcc0754Sachartre 	/* Update logical partitions */
2937edcc0754Sachartre 	bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART);
2938edcc0754Sachartre 	if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
2939edcc0754Sachartre 		for (i = 0; i < vtoc->v_nparts; i++) {
2940edcc0754Sachartre 			vd->slices[i].start = vtoc->v_part[i].p_start;
2941edcc0754Sachartre 			vd->slices[i].nblocks = vtoc->v_part[i].p_size;
2942edcc0754Sachartre 		}
2943edcc0754Sachartre 	}
2944edcc0754Sachartre 
294578fcd0a1Sachartre 	return (status);
294678fcd0a1Sachartre }
294778fcd0a1Sachartre 
294878fcd0a1Sachartre /*
294978fcd0a1Sachartre  * Handle ioctls to a disk image (file-based).
295078fcd0a1Sachartre  *
295178fcd0a1Sachartre  * Return Values
295278fcd0a1Sachartre  *	0	- Indicates that there are no errors
295378fcd0a1Sachartre  *	!= 0	- Disk operation returned an error
295478fcd0a1Sachartre  */
295578fcd0a1Sachartre static int
295678fcd0a1Sachartre vd_do_file_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
295778fcd0a1Sachartre {
295878fcd0a1Sachartre 	struct dk_label label;
295978fcd0a1Sachartre 	struct dk_geom *geom;
296078fcd0a1Sachartre 	struct vtoc *vtoc;
2961edcc0754Sachartre 	dk_efi_t *efi;
2962bae9e67eSachartre 	int rc;
296378fcd0a1Sachartre 
296478fcd0a1Sachartre 	ASSERT(vd->file);
2965edcc0754Sachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
296678fcd0a1Sachartre 
296778fcd0a1Sachartre 	switch (cmd) {
296878fcd0a1Sachartre 
296978fcd0a1Sachartre 	case DKIOCGGEOM:
297078fcd0a1Sachartre 		ASSERT(ioctl_arg != NULL);
297178fcd0a1Sachartre 		geom = (struct dk_geom *)ioctl_arg;
297278fcd0a1Sachartre 
297378fcd0a1Sachartre 		rc = vd_file_validate_geometry(vd);
2974edcc0754Sachartre 		if (rc != 0 && rc != EINVAL)
297578fcd0a1Sachartre 			return (rc);
297678fcd0a1Sachartre 		bcopy(&vd->dk_geom, geom, sizeof (struct dk_geom));
297778fcd0a1Sachartre 		return (0);
297878fcd0a1Sachartre 
297978fcd0a1Sachartre 	case DKIOCGVTOC:
298078fcd0a1Sachartre 		ASSERT(ioctl_arg != NULL);
298178fcd0a1Sachartre 		vtoc = (struct vtoc *)ioctl_arg;
298278fcd0a1Sachartre 
298378fcd0a1Sachartre 		rc = vd_file_validate_geometry(vd);
2984edcc0754Sachartre 		if (rc != 0 && rc != EINVAL)
298578fcd0a1Sachartre 			return (rc);
298678fcd0a1Sachartre 		bcopy(&vd->vtoc, vtoc, sizeof (struct vtoc));
298787a7269eSachartre 		return (0);
298887a7269eSachartre 
298987a7269eSachartre 	case DKIOCSGEOM:
299087a7269eSachartre 		ASSERT(ioctl_arg != NULL);
299187a7269eSachartre 		geom = (struct dk_geom *)ioctl_arg;
299287a7269eSachartre 
299387a7269eSachartre 		if (geom->dkg_nhead == 0 || geom->dkg_nsect == 0)
299487a7269eSachartre 			return (EINVAL);
299587a7269eSachartre 
299687a7269eSachartre 		/*
299787a7269eSachartre 		 * The current device geometry is not updated, just the driver
299887a7269eSachartre 		 * "notion" of it. The device geometry will be effectively
299987a7269eSachartre 		 * updated when a label is written to the device during a next
300087a7269eSachartre 		 * DKIOCSVTOC.
300187a7269eSachartre 		 */
300287a7269eSachartre 		bcopy(ioctl_arg, &vd->dk_geom, sizeof (vd->dk_geom));
300387a7269eSachartre 		return (0);
300487a7269eSachartre 
300587a7269eSachartre 	case DKIOCSVTOC:
300687a7269eSachartre 		ASSERT(ioctl_arg != NULL);
300787a7269eSachartre 		ASSERT(vd->dk_geom.dkg_nhead != 0 &&
300887a7269eSachartre 		    vd->dk_geom.dkg_nsect != 0);
3009690555a1Sachartre 		vtoc = (struct vtoc *)ioctl_arg;
3010690555a1Sachartre 
3011690555a1Sachartre 		if (vtoc->v_sanity != VTOC_SANE ||
3012690555a1Sachartre 		    vtoc->v_sectorsz != DEV_BSIZE ||
3013690555a1Sachartre 		    vtoc->v_nparts != V_NUMPAR)
3014690555a1Sachartre 			return (EINVAL);
3015690555a1Sachartre 
3016bae9e67eSachartre 		vd_vtocgeom_to_label(vtoc, &vd->dk_geom, &label);
3017690555a1Sachartre 
301887a7269eSachartre 		/* write label to the disk image */
301987a7269eSachartre 		if ((rc = vd_file_set_vtoc(vd, &label)) != 0)
302087a7269eSachartre 			return (rc);
3021690555a1Sachartre 
3022edcc0754Sachartre 		break;
3023edcc0754Sachartre 
3024edcc0754Sachartre 	case DKIOCFLUSHWRITECACHE:
3025edcc0754Sachartre 		return (VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL));
3026edcc0754Sachartre 
3027edcc0754Sachartre 	case DKIOCGETEFI:
3028edcc0754Sachartre 		ASSERT(ioctl_arg != NULL);
3029edcc0754Sachartre 		efi = (dk_efi_t *)ioctl_arg;
3030edcc0754Sachartre 
3031edcc0754Sachartre 		if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD,
3032edcc0754Sachartre 		    (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0)
3033edcc0754Sachartre 			return (EIO);
3034edcc0754Sachartre 
3035edcc0754Sachartre 		return (0);
3036edcc0754Sachartre 
3037edcc0754Sachartre 	case DKIOCSETEFI:
3038edcc0754Sachartre 		ASSERT(ioctl_arg != NULL);
3039edcc0754Sachartre 		efi = (dk_efi_t *)ioctl_arg;
3040edcc0754Sachartre 
3041edcc0754Sachartre 		if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE,
3042edcc0754Sachartre 		    (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0)
3043edcc0754Sachartre 			return (EIO);
3044edcc0754Sachartre 
3045edcc0754Sachartre 		break;
3046edcc0754Sachartre 
3047edcc0754Sachartre 
3048edcc0754Sachartre 	default:
3049edcc0754Sachartre 		return (ENOTSUP);
3050edcc0754Sachartre 	}
3051edcc0754Sachartre 
3052edcc0754Sachartre 	ASSERT(cmd == DKIOCSVTOC || cmd == DKIOCSETEFI);
3053edcc0754Sachartre 
3054edcc0754Sachartre 	/* label has changed, revalidate the geometry */
3055edcc0754Sachartre 	(void) vd_file_validate_geometry(vd);
30563c96341aSnarayan 
305787a7269eSachartre 	/*
305887a7269eSachartre 	 * The disk geometry may have changed, so we need to write
305987a7269eSachartre 	 * the devid (if there is one) so that it is stored at the
306087a7269eSachartre 	 * right location.
306187a7269eSachartre 	 */
3062edcc0754Sachartre 	if (vd_file_write_devid(vd, vd->file_devid) != 0) {
306387a7269eSachartre 		PR0("Fail to write devid");
30641ae08745Sheppo 	}
30654bac2208Snarayan 
30664bac2208Snarayan 	return (0);
30674bac2208Snarayan }
3068edcc0754Sachartre 
3069edcc0754Sachartre static int
3070edcc0754Sachartre vd_backend_ioctl(vd_t *vd, int cmd, caddr_t arg)
3071edcc0754Sachartre {
3072edcc0754Sachartre 	int rval = 0, status;
3073edcc0754Sachartre 
3074edcc0754Sachartre 	/*
3075edcc0754Sachartre 	 * Call the appropriate function to execute the ioctl depending
3076edcc0754Sachartre 	 * on the type of vdisk.
3077edcc0754Sachartre 	 */
3078edcc0754Sachartre 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
3079edcc0754Sachartre 
3080edcc0754Sachartre 		/* slice, file or volume exported as a single slice disk */
3081edcc0754Sachartre 		status = vd_do_slice_ioctl(vd, cmd, arg);
3082edcc0754Sachartre 
3083edcc0754Sachartre 	} else if (vd->file) {
3084edcc0754Sachartre 
3085edcc0754Sachartre 		/* file or volume exported as a full disk */
3086edcc0754Sachartre 		status = vd_do_file_ioctl(vd, cmd, arg);
3087edcc0754Sachartre 
3088edcc0754Sachartre 	} else {
3089edcc0754Sachartre 
3090edcc0754Sachartre 		/* disk device exported as a full disk */
3091edcc0754Sachartre 		status = ldi_ioctl(vd->ldi_handle[0], cmd, (intptr_t)arg,
3092edcc0754Sachartre 		    vd->open_flags | FKIOCTL, kcred, &rval);
3093edcc0754Sachartre 	}
3094edcc0754Sachartre 
3095edcc0754Sachartre #ifdef DEBUG
3096edcc0754Sachartre 	if (rval != 0) {
3097edcc0754Sachartre 		PR0("ioctl %x set rval = %d, which is not being returned"
3098edcc0754Sachartre 		    " to caller", cmd, rval);
3099edcc0754Sachartre 	}
3100edcc0754Sachartre #endif /* DEBUG */
3101edcc0754Sachartre 
3102edcc0754Sachartre 	return (status);
31031ae08745Sheppo }
31041ae08745Sheppo 
3105205eeb1aSlm66018 /*
3106205eeb1aSlm66018  * Description:
3107205eeb1aSlm66018  *	This is the function that processes the ioctl requests (farming it
3108205eeb1aSlm66018  *	out to functions that handle slices, files or whole disks)
3109205eeb1aSlm66018  *
3110205eeb1aSlm66018  * Return Values
3111205eeb1aSlm66018  *     0		- ioctl operation completed successfully
3112205eeb1aSlm66018  *     != 0		- The LDC error value encountered
3113205eeb1aSlm66018  *			  (propagated back up the call stack as a task error)
3114205eeb1aSlm66018  *
3115205eeb1aSlm66018  * Side Effect
3116205eeb1aSlm66018  *     sets request->status to the return value of the ioctl function.
3117205eeb1aSlm66018  */
31181ae08745Sheppo static int
31190a55fbb7Slm66018 vd_do_ioctl(vd_t *vd, vd_dring_payload_t *request, void* buf, vd_ioctl_t *ioctl)
31201ae08745Sheppo {
3121edcc0754Sachartre 	int	status = 0;
31221ae08745Sheppo 	size_t	nbytes = request->nbytes;	/* modifiable copy */
31231ae08745Sheppo 
31241ae08745Sheppo 
31251ae08745Sheppo 	ASSERT(request->slice < vd->nslices);
31261ae08745Sheppo 	PR0("Performing %s", ioctl->operation_name);
31271ae08745Sheppo 
31280a55fbb7Slm66018 	/* Get data from client and convert, if necessary */
31290a55fbb7Slm66018 	if (ioctl->copyin != NULL)  {
31301ae08745Sheppo 		ASSERT(nbytes != 0 && buf != NULL);
31311ae08745Sheppo 		PR1("Getting \"arg\" data from client");
31321ae08745Sheppo 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
31331ae08745Sheppo 		    request->cookie, request->ncookies,
31341ae08745Sheppo 		    LDC_COPY_IN)) != 0) {
31353af08d82Slm66018 			PR0("ldc_mem_copy() returned errno %d "
31361ae08745Sheppo 			    "copying from client", status);
31371ae08745Sheppo 			return (status);
31381ae08745Sheppo 		}
31390a55fbb7Slm66018 
31400a55fbb7Slm66018 		/* Convert client's data, if necessary */
31412f5224aeSachartre 		if (ioctl->copyin == VD_IDENTITY_IN) {
31422f5224aeSachartre 			/* use client buffer */
31430a55fbb7Slm66018 			ioctl->arg = buf;
31442f5224aeSachartre 		} else {
31452f5224aeSachartre 			/* convert client vdisk operation data to ioctl data */
31462f5224aeSachartre 			status = (ioctl->copyin)(buf, nbytes,
31472f5224aeSachartre 			    (void *)ioctl->arg);
31482f5224aeSachartre 			if (status != 0) {
31492f5224aeSachartre 				request->status = status;
31502f5224aeSachartre 				return (0);
31512f5224aeSachartre 			}
31522f5224aeSachartre 		}
31532f5224aeSachartre 	}
31542f5224aeSachartre 
31552f5224aeSachartre 	if (ioctl->operation == VD_OP_SCSICMD) {
31562f5224aeSachartre 		struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl->arg;
31572f5224aeSachartre 
31582f5224aeSachartre 		/* check write permission */
31592f5224aeSachartre 		if (!(vd->open_flags & FWRITE) &&
31602f5224aeSachartre 		    !(uscsi->uscsi_flags & USCSI_READ)) {
31612f5224aeSachartre 			PR0("uscsi fails because backend is opened read-only");
31622f5224aeSachartre 			request->status = EROFS;
31632f5224aeSachartre 			return (0);
31642f5224aeSachartre 		}
31651ae08745Sheppo 	}
31661ae08745Sheppo 
31671ae08745Sheppo 	/*
3168edcc0754Sachartre 	 * Send the ioctl to the disk backend.
31691ae08745Sheppo 	 */
3170edcc0754Sachartre 	request->status = vd_backend_ioctl(vd, ioctl->cmd, ioctl->arg);
3171205eeb1aSlm66018 
3172205eeb1aSlm66018 	if (request->status != 0) {
3173205eeb1aSlm66018 		PR0("ioctl(%s) = errno %d", ioctl->cmd_name, request->status);
31742f5224aeSachartre 		if (ioctl->operation == VD_OP_SCSICMD &&
31752f5224aeSachartre 		    ((struct uscsi_cmd *)ioctl->arg)->uscsi_status != 0)
31762f5224aeSachartre 			/*
31772f5224aeSachartre 			 * USCSICMD has reported an error and the uscsi_status
31782f5224aeSachartre 			 * field is not zero. This means that the SCSI command
31792f5224aeSachartre 			 * has completed but it has an error. So we should
31802f5224aeSachartre 			 * mark the VD operation has succesfully completed
31812f5224aeSachartre 			 * and clients can check the SCSI status field for
31822f5224aeSachartre 			 * SCSI errors.
31832f5224aeSachartre 			 */
31842f5224aeSachartre 			request->status = 0;
31852f5224aeSachartre 		else
3186205eeb1aSlm66018 			return (0);
3187205eeb1aSlm66018 	}
31881ae08745Sheppo 
31890a55fbb7Slm66018 	/* Convert data and send to client, if necessary */
31900a55fbb7Slm66018 	if (ioctl->copyout != NULL)  {
31911ae08745Sheppo 		ASSERT(nbytes != 0 && buf != NULL);
31921ae08745Sheppo 		PR1("Sending \"arg\" data to client");
31930a55fbb7Slm66018 
31940a55fbb7Slm66018 		/* Convert ioctl data to vdisk operation data, if necessary */
31952f5224aeSachartre 		if (ioctl->copyout != VD_IDENTITY_OUT)
31960a55fbb7Slm66018 			(ioctl->copyout)((void *)ioctl->arg, buf);
31970a55fbb7Slm66018 
31981ae08745Sheppo 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
31991ae08745Sheppo 		    request->cookie, request->ncookies,
32001ae08745Sheppo 		    LDC_COPY_OUT)) != 0) {
32013af08d82Slm66018 			PR0("ldc_mem_copy() returned errno %d "
32021ae08745Sheppo 			    "copying to client", status);
32031ae08745Sheppo 			return (status);
32041ae08745Sheppo 		}
32051ae08745Sheppo 	}
32061ae08745Sheppo 
32071ae08745Sheppo 	return (status);
32081ae08745Sheppo }
32091ae08745Sheppo 
32101ae08745Sheppo #define	RNDSIZE(expr) P2ROUNDUP(sizeof (expr), sizeof (uint64_t))
3211205eeb1aSlm66018 
3212205eeb1aSlm66018 /*
3213205eeb1aSlm66018  * Description:
3214205eeb1aSlm66018  *	This generic function is called by the task queue to complete
3215205eeb1aSlm66018  *	the processing of the tasks. The specific completion function
3216205eeb1aSlm66018  *	is passed in as a field in the task pointer.
3217205eeb1aSlm66018  *
3218205eeb1aSlm66018  * Parameters:
3219205eeb1aSlm66018  *	arg 	- opaque pointer to structure containing task to be completed
3220205eeb1aSlm66018  *
3221205eeb1aSlm66018  * Return Values
3222205eeb1aSlm66018  *	None
3223205eeb1aSlm66018  */
3224205eeb1aSlm66018 static void
3225205eeb1aSlm66018 vd_complete(void *arg)
3226205eeb1aSlm66018 {
3227205eeb1aSlm66018 	vd_task_t	*task = (vd_task_t *)arg;
3228205eeb1aSlm66018 
3229205eeb1aSlm66018 	ASSERT(task != NULL);
3230205eeb1aSlm66018 	ASSERT(task->status == EINPROGRESS);
3231205eeb1aSlm66018 	ASSERT(task->completef != NULL);
3232205eeb1aSlm66018 
3233205eeb1aSlm66018 	task->status = task->completef(task);
3234205eeb1aSlm66018 	if (task->status)
3235205eeb1aSlm66018 		PR0("%s: Error %d completing task", __func__, task->status);
3236205eeb1aSlm66018 
3237205eeb1aSlm66018 	/* Now notify the vDisk client */
3238205eeb1aSlm66018 	vd_complete_notify(task);
3239205eeb1aSlm66018 }
3240205eeb1aSlm66018 
32411ae08745Sheppo static int
3242d10e4ef2Snarayan vd_ioctl(vd_task_t *task)
32431ae08745Sheppo {
324487a7269eSachartre 	int			i, status;
32451ae08745Sheppo 	void			*buf = NULL;
32460a55fbb7Slm66018 	struct dk_geom		dk_geom = {0};
32470a55fbb7Slm66018 	struct vtoc		vtoc = {0};
32484bac2208Snarayan 	struct dk_efi		dk_efi = {0};
32492f5224aeSachartre 	struct uscsi_cmd	uscsi = {0};
3250d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
3251d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
32520a55fbb7Slm66018 	vd_ioctl_t		ioctl[] = {
32530a55fbb7Slm66018 		/* Command (no-copy) operations */
32540a55fbb7Slm66018 		{VD_OP_FLUSH, STRINGIZE(VD_OP_FLUSH), 0,
32550a55fbb7Slm66018 		    DKIOCFLUSHWRITECACHE, STRINGIZE(DKIOCFLUSHWRITECACHE),
3256047ba61eSachartre 		    NULL, NULL, NULL, B_TRUE},
32570a55fbb7Slm66018 
32580a55fbb7Slm66018 		/* "Get" (copy-out) operations */
32590a55fbb7Slm66018 		{VD_OP_GET_WCE, STRINGIZE(VD_OP_GET_WCE), RNDSIZE(int),
32600a55fbb7Slm66018 		    DKIOCGETWCE, STRINGIZE(DKIOCGETWCE),
32612f5224aeSachartre 		    NULL, VD_IDENTITY_IN, VD_IDENTITY_OUT, B_FALSE},
32620a55fbb7Slm66018 		{VD_OP_GET_DISKGEOM, STRINGIZE(VD_OP_GET_DISKGEOM),
32630a55fbb7Slm66018 		    RNDSIZE(vd_geom_t),
32640a55fbb7Slm66018 		    DKIOCGGEOM, STRINGIZE(DKIOCGGEOM),
3265047ba61eSachartre 		    &dk_geom, NULL, dk_geom2vd_geom, B_FALSE},
32660a55fbb7Slm66018 		{VD_OP_GET_VTOC, STRINGIZE(VD_OP_GET_VTOC), RNDSIZE(vd_vtoc_t),
32670a55fbb7Slm66018 		    DKIOCGVTOC, STRINGIZE(DKIOCGVTOC),
3268047ba61eSachartre 		    &vtoc, NULL, vtoc2vd_vtoc, B_FALSE},
32694bac2208Snarayan 		{VD_OP_GET_EFI, STRINGIZE(VD_OP_GET_EFI), RNDSIZE(vd_efi_t),
32704bac2208Snarayan 		    DKIOCGETEFI, STRINGIZE(DKIOCGETEFI),
3271047ba61eSachartre 		    &dk_efi, vd_get_efi_in, vd_get_efi_out, B_FALSE},
32720a55fbb7Slm66018 
32730a55fbb7Slm66018 		/* "Set" (copy-in) operations */
32740a55fbb7Slm66018 		{VD_OP_SET_WCE, STRINGIZE(VD_OP_SET_WCE), RNDSIZE(int),
32750a55fbb7Slm66018 		    DKIOCSETWCE, STRINGIZE(DKIOCSETWCE),
32762f5224aeSachartre 		    NULL, VD_IDENTITY_IN, VD_IDENTITY_OUT, B_TRUE},
32770a55fbb7Slm66018 		{VD_OP_SET_DISKGEOM, STRINGIZE(VD_OP_SET_DISKGEOM),
32780a55fbb7Slm66018 		    RNDSIZE(vd_geom_t),
32790a55fbb7Slm66018 		    DKIOCSGEOM, STRINGIZE(DKIOCSGEOM),
3280047ba61eSachartre 		    &dk_geom, vd_geom2dk_geom, NULL, B_TRUE},
32810a55fbb7Slm66018 		{VD_OP_SET_VTOC, STRINGIZE(VD_OP_SET_VTOC), RNDSIZE(vd_vtoc_t),
32820a55fbb7Slm66018 		    DKIOCSVTOC, STRINGIZE(DKIOCSVTOC),
3283047ba61eSachartre 		    &vtoc, vd_vtoc2vtoc, NULL, B_TRUE},
32844bac2208Snarayan 		{VD_OP_SET_EFI, STRINGIZE(VD_OP_SET_EFI), RNDSIZE(vd_efi_t),
32854bac2208Snarayan 		    DKIOCSETEFI, STRINGIZE(DKIOCSETEFI),
3286047ba61eSachartre 		    &dk_efi, vd_set_efi_in, vd_set_efi_out, B_TRUE},
32872f5224aeSachartre 
32882f5224aeSachartre 		{VD_OP_SCSICMD, STRINGIZE(VD_OP_SCSICMD), RNDSIZE(vd_scsi_t),
32892f5224aeSachartre 		    USCSICMD, STRINGIZE(USCSICMD),
32902f5224aeSachartre 		    &uscsi, vd_scsicmd_in, vd_scsicmd_out, B_FALSE},
32910a55fbb7Slm66018 	};
32921ae08745Sheppo 	size_t		nioctls = (sizeof (ioctl))/(sizeof (ioctl[0]));
32931ae08745Sheppo 
32941ae08745Sheppo 
3295d10e4ef2Snarayan 	ASSERT(vd != NULL);
3296d10e4ef2Snarayan 	ASSERT(request != NULL);
32971ae08745Sheppo 	ASSERT(request->slice < vd->nslices);
32981ae08745Sheppo 
32991ae08745Sheppo 	/*
33001ae08745Sheppo 	 * Determine ioctl corresponding to caller's "operation" and
33011ae08745Sheppo 	 * validate caller's "nbytes"
33021ae08745Sheppo 	 */
33031ae08745Sheppo 	for (i = 0; i < nioctls; i++) {
33041ae08745Sheppo 		if (request->operation == ioctl[i].operation) {
33050a55fbb7Slm66018 			/* LDC memory operations require 8-byte multiples */
33060a55fbb7Slm66018 			ASSERT(ioctl[i].nbytes % sizeof (uint64_t) == 0);
33070a55fbb7Slm66018 
33084bac2208Snarayan 			if (request->operation == VD_OP_GET_EFI ||
33092f5224aeSachartre 			    request->operation == VD_OP_SET_EFI ||
33102f5224aeSachartre 			    request->operation == VD_OP_SCSICMD) {
33114bac2208Snarayan 				if (request->nbytes >= ioctl[i].nbytes)
33124bac2208Snarayan 					break;
33133af08d82Slm66018 				PR0("%s:  Expected at least nbytes = %lu, "
33144bac2208Snarayan 				    "got %lu", ioctl[i].operation_name,
33154bac2208Snarayan 				    ioctl[i].nbytes, request->nbytes);
33164bac2208Snarayan 				return (EINVAL);
33174bac2208Snarayan 			}
33184bac2208Snarayan 
33190a55fbb7Slm66018 			if (request->nbytes != ioctl[i].nbytes) {
33203af08d82Slm66018 				PR0("%s:  Expected nbytes = %lu, got %lu",
33210a55fbb7Slm66018 				    ioctl[i].operation_name, ioctl[i].nbytes,
33220a55fbb7Slm66018 				    request->nbytes);
33231ae08745Sheppo 				return (EINVAL);
33241ae08745Sheppo 			}
33251ae08745Sheppo 
33261ae08745Sheppo 			break;
33271ae08745Sheppo 		}
33281ae08745Sheppo 	}
33291ae08745Sheppo 	ASSERT(i < nioctls);	/* because "operation" already validated */
33301ae08745Sheppo 
3331047ba61eSachartre 	if (!(vd->open_flags & FWRITE) && ioctl[i].write) {
3332047ba61eSachartre 		PR0("%s fails because backend is opened read-only",
3333047ba61eSachartre 		    ioctl[i].operation_name);
3334047ba61eSachartre 		request->status = EROFS;
3335047ba61eSachartre 		return (0);
3336047ba61eSachartre 	}
3337047ba61eSachartre 
33381ae08745Sheppo 	if (request->nbytes)
33391ae08745Sheppo 		buf = kmem_zalloc(request->nbytes, KM_SLEEP);
33401ae08745Sheppo 	status = vd_do_ioctl(vd, request, buf, &ioctl[i]);
33411ae08745Sheppo 	if (request->nbytes)
33421ae08745Sheppo 		kmem_free(buf, request->nbytes);
334387a7269eSachartre 
33441ae08745Sheppo 	return (status);
33451ae08745Sheppo }
33461ae08745Sheppo 
33474bac2208Snarayan static int
33484bac2208Snarayan vd_get_devid(vd_task_t *task)
33494bac2208Snarayan {
33504bac2208Snarayan 	vd_t *vd = task->vd;
33514bac2208Snarayan 	vd_dring_payload_t *request = task->request;
33524bac2208Snarayan 	vd_devid_t *vd_devid;
33534bac2208Snarayan 	impl_devid_t *devid;
335487a7269eSachartre 	int status, bufid_len, devid_len, len, sz;
33553af08d82Slm66018 	int bufbytes;
33564bac2208Snarayan 
33573af08d82Slm66018 	PR1("Get Device ID, nbytes=%ld", request->nbytes);
33584bac2208Snarayan 
3359bae9e67eSachartre 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
3360bae9e67eSachartre 		/*
3361bae9e67eSachartre 		 * We don't support devid for single-slice disks because we
3362bae9e67eSachartre 		 * have no space to store a fabricated devid and for physical
3363bae9e67eSachartre 		 * disk slices, we can't use the devid of the disk otherwise
3364bae9e67eSachartre 		 * exporting multiple slices from the same disk will produce
3365bae9e67eSachartre 		 * the same devids.
3366bae9e67eSachartre 		 */
3367bae9e67eSachartre 		PR2("No Device ID for slices");
3368bae9e67eSachartre 		request->status = ENOTSUP;
3369bae9e67eSachartre 		return (0);
3370bae9e67eSachartre 	}
3371bae9e67eSachartre 
33723c96341aSnarayan 	if (vd->file) {
337387a7269eSachartre 		if (vd->file_devid == NULL) {
33743af08d82Slm66018 			PR2("No Device ID");
3375205eeb1aSlm66018 			request->status = ENOENT;
3376205eeb1aSlm66018 			return (0);
337787a7269eSachartre 		} else {
337887a7269eSachartre 			sz = ddi_devid_sizeof(vd->file_devid);
337987a7269eSachartre 			devid = kmem_alloc(sz, KM_SLEEP);
338087a7269eSachartre 			bcopy(vd->file_devid, devid, sz);
338187a7269eSachartre 		}
338287a7269eSachartre 	} else {
338387a7269eSachartre 		if (ddi_lyr_get_devid(vd->dev[request->slice],
338487a7269eSachartre 		    (ddi_devid_t *)&devid) != DDI_SUCCESS) {
338587a7269eSachartre 			PR2("No Device ID");
3386205eeb1aSlm66018 			request->status = ENOENT;
3387205eeb1aSlm66018 			return (0);
338887a7269eSachartre 		}
33894bac2208Snarayan 	}
33904bac2208Snarayan 
33914bac2208Snarayan 	bufid_len = request->nbytes - sizeof (vd_devid_t) + 1;
33924bac2208Snarayan 	devid_len = DEVID_GETLEN(devid);
33934bac2208Snarayan 
33943af08d82Slm66018 	/*
33953af08d82Slm66018 	 * Save the buffer size here for use in deallocation.
33963af08d82Slm66018 	 * The actual number of bytes copied is returned in
33973af08d82Slm66018 	 * the 'nbytes' field of the request structure.
33983af08d82Slm66018 	 */
33993af08d82Slm66018 	bufbytes = request->nbytes;
34003af08d82Slm66018 
34013af08d82Slm66018 	vd_devid = kmem_zalloc(bufbytes, KM_SLEEP);
34024bac2208Snarayan 	vd_devid->length = devid_len;
34034bac2208Snarayan 	vd_devid->type = DEVID_GETTYPE(devid);
34044bac2208Snarayan 
34054bac2208Snarayan 	len = (devid_len > bufid_len)? bufid_len : devid_len;
34064bac2208Snarayan 
34074bac2208Snarayan 	bcopy(devid->did_id, vd_devid->id, len);
34084bac2208Snarayan 
340978fcd0a1Sachartre 	request->status = 0;
341078fcd0a1Sachartre 
34114bac2208Snarayan 	/* LDC memory operations require 8-byte multiples */
34124bac2208Snarayan 	ASSERT(request->nbytes % sizeof (uint64_t) == 0);
34134bac2208Snarayan 
34144bac2208Snarayan 	if ((status = ldc_mem_copy(vd->ldc_handle, (caddr_t)vd_devid, 0,
34154bac2208Snarayan 	    &request->nbytes, request->cookie, request->ncookies,
34164bac2208Snarayan 	    LDC_COPY_OUT)) != 0) {
34173af08d82Slm66018 		PR0("ldc_mem_copy() returned errno %d copying to client",
34184bac2208Snarayan 		    status);
34194bac2208Snarayan 	}
34203af08d82Slm66018 	PR1("post mem_copy: nbytes=%ld", request->nbytes);
34214bac2208Snarayan 
34223af08d82Slm66018 	kmem_free(vd_devid, bufbytes);
34234bac2208Snarayan 	ddi_devid_free((ddi_devid_t)devid);
34244bac2208Snarayan 
34254bac2208Snarayan 	return (status);
34264bac2208Snarayan }
34274bac2208Snarayan 
34282f5224aeSachartre static int
34292f5224aeSachartre vd_scsi_reset(vd_t *vd)
34302f5224aeSachartre {
34312f5224aeSachartre 	int rval, status;
34322f5224aeSachartre 	struct uscsi_cmd uscsi = { 0 };
34332f5224aeSachartre 
34342f5224aeSachartre 	uscsi.uscsi_flags = vd_scsi_debug | USCSI_RESET;
34352f5224aeSachartre 	uscsi.uscsi_timeout = vd_scsi_rdwr_timeout;
34362f5224aeSachartre 
34372f5224aeSachartre 	status = ldi_ioctl(vd->ldi_handle[0], USCSICMD, (intptr_t)&uscsi,
34382f5224aeSachartre 	    (vd->open_flags | FKIOCTL), kcred, &rval);
34392f5224aeSachartre 
34402f5224aeSachartre 	return (status);
34412f5224aeSachartre }
34422f5224aeSachartre 
34432f5224aeSachartre static int
34442f5224aeSachartre vd_reset(vd_task_t *task)
34452f5224aeSachartre {
34462f5224aeSachartre 	vd_t *vd = task->vd;
34472f5224aeSachartre 	vd_dring_payload_t *request = task->request;
34482f5224aeSachartre 
34492f5224aeSachartre 	ASSERT(request->operation == VD_OP_RESET);
34502f5224aeSachartre 	ASSERT(vd->scsi);
34512f5224aeSachartre 
34522f5224aeSachartre 	PR0("Performing VD_OP_RESET");
34532f5224aeSachartre 
34542f5224aeSachartre 	if (request->nbytes != 0) {
34552f5224aeSachartre 		PR0("VD_OP_RESET:  Expected nbytes = 0, got %lu",
34562f5224aeSachartre 		    request->nbytes);
34572f5224aeSachartre 		return (EINVAL);
34582f5224aeSachartre 	}
34592f5224aeSachartre 
34602f5224aeSachartre 	request->status = vd_scsi_reset(vd);
34612f5224aeSachartre 
34622f5224aeSachartre 	return (0);
34632f5224aeSachartre }
34642f5224aeSachartre 
34652f5224aeSachartre static int
34662f5224aeSachartre vd_get_capacity(vd_task_t *task)
34672f5224aeSachartre {
34682f5224aeSachartre 	int rv;
34692f5224aeSachartre 	size_t nbytes;
34702f5224aeSachartre 	vd_t *vd = task->vd;
34712f5224aeSachartre 	vd_dring_payload_t *request = task->request;
34722f5224aeSachartre 	vd_capacity_t vd_cap = { 0 };
34732f5224aeSachartre 
34742f5224aeSachartre 	ASSERT(request->operation == VD_OP_GET_CAPACITY);
34752f5224aeSachartre 	ASSERT(vd->scsi);
34762f5224aeSachartre 
34772f5224aeSachartre 	PR0("Performing VD_OP_GET_CAPACITY");
34782f5224aeSachartre 
34792f5224aeSachartre 	nbytes = request->nbytes;
34802f5224aeSachartre 
34812f5224aeSachartre 	if (nbytes != RNDSIZE(vd_capacity_t)) {
34822f5224aeSachartre 		PR0("VD_OP_GET_CAPACITY:  Expected nbytes = %lu, got %lu",
34832f5224aeSachartre 		    RNDSIZE(vd_capacity_t), nbytes);
34842f5224aeSachartre 		return (EINVAL);
34852f5224aeSachartre 	}
34862f5224aeSachartre 
34872f5224aeSachartre 	if (vd->vdisk_size == VD_SIZE_UNKNOWN) {
34882f5224aeSachartre 		if (vd_setup_mediainfo(vd) != 0)
34892f5224aeSachartre 			ASSERT(vd->vdisk_size == VD_SIZE_UNKNOWN);
34902f5224aeSachartre 	}
34912f5224aeSachartre 
34922f5224aeSachartre 	ASSERT(vd->vdisk_size != 0);
34932f5224aeSachartre 
34942f5224aeSachartre 	request->status = 0;
34952f5224aeSachartre 
34962f5224aeSachartre 	vd_cap.vdisk_block_size = vd->vdisk_block_size;
34972f5224aeSachartre 	vd_cap.vdisk_size = vd->vdisk_size;
34982f5224aeSachartre 
34992f5224aeSachartre 	if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&vd_cap, 0, &nbytes,
35002f5224aeSachartre 	    request->cookie, request->ncookies, LDC_COPY_OUT)) != 0) {
35012f5224aeSachartre 		PR0("ldc_mem_copy() returned errno %d copying to client", rv);
35022f5224aeSachartre 		return (rv);
35032f5224aeSachartre 	}
35042f5224aeSachartre 
35052f5224aeSachartre 	return (0);
35062f5224aeSachartre }
35072f5224aeSachartre 
35082f5224aeSachartre static int
35092f5224aeSachartre vd_get_access(vd_task_t *task)
35102f5224aeSachartre {
35112f5224aeSachartre 	uint64_t access;
35122f5224aeSachartre 	int rv, rval = 0;
35132f5224aeSachartre 	size_t nbytes;
35142f5224aeSachartre 	vd_t *vd = task->vd;
35152f5224aeSachartre 	vd_dring_payload_t *request = task->request;
35162f5224aeSachartre 
35172f5224aeSachartre 	ASSERT(request->operation == VD_OP_GET_ACCESS);
35182f5224aeSachartre 	ASSERT(vd->scsi);
35192f5224aeSachartre 
35202f5224aeSachartre 	PR0("Performing VD_OP_GET_ACCESS");
35212f5224aeSachartre 
35222f5224aeSachartre 	nbytes = request->nbytes;
35232f5224aeSachartre 
35242f5224aeSachartre 	if (nbytes != sizeof (uint64_t)) {
35252f5224aeSachartre 		PR0("VD_OP_GET_ACCESS:  Expected nbytes = %lu, got %lu",
35262f5224aeSachartre 		    sizeof (uint64_t), nbytes);
35272f5224aeSachartre 		return (EINVAL);
35282f5224aeSachartre 	}
35292f5224aeSachartre 
35302f5224aeSachartre 	request->status = ldi_ioctl(vd->ldi_handle[request->slice], MHIOCSTATUS,
35312f5224aeSachartre 	    NULL, (vd->open_flags | FKIOCTL), kcred, &rval);
35322f5224aeSachartre 
35332f5224aeSachartre 	if (request->status != 0)
35342f5224aeSachartre 		return (0);
35352f5224aeSachartre 
35362f5224aeSachartre 	access = (rval == 0)? VD_ACCESS_ALLOWED : VD_ACCESS_DENIED;
35372f5224aeSachartre 
35382f5224aeSachartre 	if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&access, 0, &nbytes,
35392f5224aeSachartre 	    request->cookie, request->ncookies, LDC_COPY_OUT)) != 0) {
35402f5224aeSachartre 		PR0("ldc_mem_copy() returned errno %d copying to client", rv);
35412f5224aeSachartre 		return (rv);
35422f5224aeSachartre 	}
35432f5224aeSachartre 
35442f5224aeSachartre 	return (0);
35452f5224aeSachartre }
35462f5224aeSachartre 
35472f5224aeSachartre static int
35482f5224aeSachartre vd_set_access(vd_task_t *task)
35492f5224aeSachartre {
35502f5224aeSachartre 	uint64_t flags;
35512f5224aeSachartre 	int rv, rval;
35522f5224aeSachartre 	size_t nbytes;
35532f5224aeSachartre 	vd_t *vd = task->vd;
35542f5224aeSachartre 	vd_dring_payload_t *request = task->request;
35552f5224aeSachartre 
35562f5224aeSachartre 	ASSERT(request->operation == VD_OP_SET_ACCESS);
35572f5224aeSachartre 	ASSERT(vd->scsi);
35582f5224aeSachartre 
35592f5224aeSachartre 	nbytes = request->nbytes;
35602f5224aeSachartre 
35612f5224aeSachartre 	if (nbytes != sizeof (uint64_t)) {
35622f5224aeSachartre 		PR0("VD_OP_SET_ACCESS:  Expected nbytes = %lu, got %lu",
35632f5224aeSachartre 		    sizeof (uint64_t), nbytes);
35642f5224aeSachartre 		return (EINVAL);
35652f5224aeSachartre 	}
35662f5224aeSachartre 
35672f5224aeSachartre 	if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&flags, 0, &nbytes,
35682f5224aeSachartre 	    request->cookie, request->ncookies, LDC_COPY_IN)) != 0) {
35692f5224aeSachartre 		PR0("ldc_mem_copy() returned errno %d copying from client", rv);
35702f5224aeSachartre 		return (rv);
35712f5224aeSachartre 	}
35722f5224aeSachartre 
35732f5224aeSachartre 	if (flags == VD_ACCESS_SET_CLEAR) {
35742f5224aeSachartre 		PR0("Performing VD_OP_SET_ACCESS (CLEAR)");
35752f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
35762f5224aeSachartre 		    MHIOCRELEASE, NULL, (vd->open_flags | FKIOCTL), kcred,
35772f5224aeSachartre 		    &rval);
35782f5224aeSachartre 		if (request->status == 0)
35792f5224aeSachartre 			vd->ownership = B_FALSE;
35802f5224aeSachartre 		return (0);
35812f5224aeSachartre 	}
35822f5224aeSachartre 
35832f5224aeSachartre 	/*
35842f5224aeSachartre 	 * As per the VIO spec, the PREEMPT and PRESERVE flags are only valid
35852f5224aeSachartre 	 * when the EXCLUSIVE flag is set.
35862f5224aeSachartre 	 */
35872f5224aeSachartre 	if (!(flags & VD_ACCESS_SET_EXCLUSIVE)) {
35882f5224aeSachartre 		PR0("Invalid VD_OP_SET_ACCESS flags: 0x%lx", flags);
35892f5224aeSachartre 		request->status = EINVAL;
35902f5224aeSachartre 		return (0);
35912f5224aeSachartre 	}
35922f5224aeSachartre 
35932f5224aeSachartre 	switch (flags & (VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE)) {
35942f5224aeSachartre 
35952f5224aeSachartre 	case VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE:
35962f5224aeSachartre 		/*
35972f5224aeSachartre 		 * Flags EXCLUSIVE and PREEMPT and PRESERVE. We have to
35982f5224aeSachartre 		 * acquire exclusive access rights, preserve them and we
35992f5224aeSachartre 		 * can use preemption. So we can use the MHIOCTKNOWN ioctl.
36002f5224aeSachartre 		 */
36012f5224aeSachartre 		PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT|PRESERVE)");
36022f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
36032f5224aeSachartre 		    MHIOCTKOWN, NULL, (vd->open_flags | FKIOCTL), kcred, &rval);
36042f5224aeSachartre 		break;
36052f5224aeSachartre 
36062f5224aeSachartre 	case VD_ACCESS_SET_PRESERVE:
36072f5224aeSachartre 		/*
36082f5224aeSachartre 		 * Flags EXCLUSIVE and PRESERVE. We have to acquire exclusive
36092f5224aeSachartre 		 * access rights and preserve them, but not preempt any other
36102f5224aeSachartre 		 * host. So we need to use the MHIOCTKOWN ioctl to enable the
36112f5224aeSachartre 		 * "preserve" feature but we can not called it directly
36122f5224aeSachartre 		 * because it uses preemption. So before that, we use the
36132f5224aeSachartre 		 * MHIOCQRESERVE ioctl to ensure we can get exclusive rights
36142f5224aeSachartre 		 * without preempting anyone.
36152f5224aeSachartre 		 */
36162f5224aeSachartre 		PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PRESERVE)");
36172f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
36182f5224aeSachartre 		    MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
36192f5224aeSachartre 		    &rval);
36202f5224aeSachartre 		if (request->status != 0)
36212f5224aeSachartre 			break;
36222f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
36232f5224aeSachartre 		    MHIOCTKOWN, NULL, (vd->open_flags | FKIOCTL), kcred, &rval);
36242f5224aeSachartre 		break;
36252f5224aeSachartre 
36262f5224aeSachartre 	case VD_ACCESS_SET_PREEMPT:
36272f5224aeSachartre 		/*
36282f5224aeSachartre 		 * Flags EXCLUSIVE and PREEMPT. We have to acquire exclusive
36292f5224aeSachartre 		 * access rights and we can use preemption. So we try to do
36302f5224aeSachartre 		 * a SCSI reservation, if it fails we reset the disk to clear
36312f5224aeSachartre 		 * any reservation and we try to reserve again.
36322f5224aeSachartre 		 */
36332f5224aeSachartre 		PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT)");
36342f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
36352f5224aeSachartre 		    MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
36362f5224aeSachartre 		    &rval);
36372f5224aeSachartre 		if (request->status == 0)
36382f5224aeSachartre 			break;
36392f5224aeSachartre 
36402f5224aeSachartre 		/* reset the disk */
36412f5224aeSachartre 		(void) vd_scsi_reset(vd);
36422f5224aeSachartre 
36432f5224aeSachartre 		/* try again even if the reset has failed */
36442f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
36452f5224aeSachartre 		    MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
36462f5224aeSachartre 		    &rval);
36472f5224aeSachartre 		break;
36482f5224aeSachartre 
36492f5224aeSachartre 	case 0:
36502f5224aeSachartre 		/* Flag EXCLUSIVE only. Just issue a SCSI reservation */
36512f5224aeSachartre 		PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE)");
36522f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
36532f5224aeSachartre 		    MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
36542f5224aeSachartre 		    &rval);
36552f5224aeSachartre 		break;
36562f5224aeSachartre 	}
36572f5224aeSachartre 
36582f5224aeSachartre 	if (request->status == 0)
36592f5224aeSachartre 		vd->ownership = B_TRUE;
36602f5224aeSachartre 	else
36612f5224aeSachartre 		PR0("VD_OP_SET_ACCESS: error %d", request->status);
36622f5224aeSachartre 
36632f5224aeSachartre 	return (0);
36642f5224aeSachartre }
36652f5224aeSachartre 
36662f5224aeSachartre static void
36672f5224aeSachartre vd_reset_access(vd_t *vd)
36682f5224aeSachartre {
36692f5224aeSachartre 	int status, rval;
36702f5224aeSachartre 
36712f5224aeSachartre 	if (vd->file || !vd->ownership)
36722f5224aeSachartre 		return;
36732f5224aeSachartre 
36742f5224aeSachartre 	PR0("Releasing disk ownership");
36752f5224aeSachartre 	status = ldi_ioctl(vd->ldi_handle[0], MHIOCRELEASE, NULL,
36762f5224aeSachartre 	    (vd->open_flags | FKIOCTL), kcred, &rval);
36772f5224aeSachartre 
36782f5224aeSachartre 	/*
36792f5224aeSachartre 	 * An EACCES failure means that there is a reservation conflict,
36802f5224aeSachartre 	 * so we are not the owner of the disk anymore.
36812f5224aeSachartre 	 */
36822f5224aeSachartre 	if (status == 0 || status == EACCES) {
36832f5224aeSachartre 		vd->ownership = B_FALSE;
36842f5224aeSachartre 		return;
36852f5224aeSachartre 	}
36862f5224aeSachartre 
36872f5224aeSachartre 	PR0("Fail to release ownership, error %d", status);
36882f5224aeSachartre 
36892f5224aeSachartre 	/*
36902f5224aeSachartre 	 * We have failed to release the ownership, try to reset the disk
36912f5224aeSachartre 	 * to release reservations.
36922f5224aeSachartre 	 */
36932f5224aeSachartre 	PR0("Resetting disk");
36942f5224aeSachartre 	status = vd_scsi_reset(vd);
36952f5224aeSachartre 
36962f5224aeSachartre 	if (status != 0)
36972f5224aeSachartre 		PR0("Fail to reset disk, error %d", status);
36982f5224aeSachartre 
36992f5224aeSachartre 	/* whatever the result of the reset is, we try the release again */
37002f5224aeSachartre 	status = ldi_ioctl(vd->ldi_handle[0], MHIOCRELEASE, NULL,
37012f5224aeSachartre 	    (vd->open_flags | FKIOCTL), kcred, &rval);
37022f5224aeSachartre 
37032f5224aeSachartre 	if (status == 0 || status == EACCES) {
37042f5224aeSachartre 		vd->ownership = B_FALSE;
37052f5224aeSachartre 		return;
37062f5224aeSachartre 	}
37072f5224aeSachartre 
37082f5224aeSachartre 	PR0("Fail to release ownership, error %d", status);
37092f5224aeSachartre 
37102f5224aeSachartre 	/*
37112f5224aeSachartre 	 * At this point we have done our best to try to reset the
37122f5224aeSachartre 	 * access rights to the disk and we don't know if we still
37132f5224aeSachartre 	 * own a reservation and if any mechanism to preserve the
37142f5224aeSachartre 	 * ownership is still in place. The ultimate solution would
37152f5224aeSachartre 	 * be to reset the system but this is usually not what we
37162f5224aeSachartre 	 * want to happen.
37172f5224aeSachartre 	 */
37182f5224aeSachartre 
37192f5224aeSachartre 	if (vd_reset_access_failure == A_REBOOT) {
37202f5224aeSachartre 		cmn_err(CE_WARN, VD_RESET_ACCESS_FAILURE_MSG
37212f5224aeSachartre 		    ", rebooting the system", vd->device_path);
37222f5224aeSachartre 		(void) uadmin(A_SHUTDOWN, AD_BOOT, NULL);
37232f5224aeSachartre 	} else if (vd_reset_access_failure == A_DUMP) {
37242f5224aeSachartre 		panic(VD_RESET_ACCESS_FAILURE_MSG, vd->device_path);
37252f5224aeSachartre 	}
37262f5224aeSachartre 
37272f5224aeSachartre 	cmn_err(CE_WARN, VD_RESET_ACCESS_FAILURE_MSG, vd->device_path);
37282f5224aeSachartre }
37292f5224aeSachartre 
37301ae08745Sheppo /*
37311ae08745Sheppo  * Define the supported operations once the functions for performing them have
37321ae08745Sheppo  * been defined
37331ae08745Sheppo  */
37341ae08745Sheppo static const vds_operation_t	vds_operation[] = {
37353af08d82Slm66018 #define	X(_s)	#_s, _s
37363af08d82Slm66018 	{X(VD_OP_BREAD),	vd_start_bio,	vd_complete_bio},
37373af08d82Slm66018 	{X(VD_OP_BWRITE),	vd_start_bio,	vd_complete_bio},
37383af08d82Slm66018 	{X(VD_OP_FLUSH),	vd_ioctl,	NULL},
37393af08d82Slm66018 	{X(VD_OP_GET_WCE),	vd_ioctl,	NULL},
37403af08d82Slm66018 	{X(VD_OP_SET_WCE),	vd_ioctl,	NULL},
37413af08d82Slm66018 	{X(VD_OP_GET_VTOC),	vd_ioctl,	NULL},
37423af08d82Slm66018 	{X(VD_OP_SET_VTOC),	vd_ioctl,	NULL},
37433af08d82Slm66018 	{X(VD_OP_GET_DISKGEOM),	vd_ioctl,	NULL},
37443af08d82Slm66018 	{X(VD_OP_SET_DISKGEOM),	vd_ioctl,	NULL},
37453af08d82Slm66018 	{X(VD_OP_GET_EFI),	vd_ioctl,	NULL},
37463af08d82Slm66018 	{X(VD_OP_SET_EFI),	vd_ioctl,	NULL},
37473af08d82Slm66018 	{X(VD_OP_GET_DEVID),	vd_get_devid,	NULL},
37482f5224aeSachartre 	{X(VD_OP_SCSICMD),	vd_ioctl,	NULL},
37492f5224aeSachartre 	{X(VD_OP_RESET),	vd_reset,	NULL},
37502f5224aeSachartre 	{X(VD_OP_GET_CAPACITY),	vd_get_capacity, NULL},
37512f5224aeSachartre 	{X(VD_OP_SET_ACCESS),	vd_set_access,	NULL},
37522f5224aeSachartre 	{X(VD_OP_GET_ACCESS),	vd_get_access,	NULL},
37533af08d82Slm66018 #undef	X
37541ae08745Sheppo };
37551ae08745Sheppo 
37561ae08745Sheppo static const size_t	vds_noperations =
37571ae08745Sheppo 	(sizeof (vds_operation))/(sizeof (vds_operation[0]));
37581ae08745Sheppo 
37591ae08745Sheppo /*
3760d10e4ef2Snarayan  * Process a task specifying a client I/O request
3761205eeb1aSlm66018  *
3762205eeb1aSlm66018  * Parameters:
3763205eeb1aSlm66018  *	task 		- structure containing the request sent from client
3764205eeb1aSlm66018  *
3765205eeb1aSlm66018  * Return Value
3766205eeb1aSlm66018  *	0	- success
3767205eeb1aSlm66018  *	ENOTSUP	- Unknown/Unsupported VD_OP_XXX operation
3768205eeb1aSlm66018  *	EINVAL	- Invalid disk slice
3769205eeb1aSlm66018  *	!= 0	- some other non-zero return value from start function
37701ae08745Sheppo  */
37711ae08745Sheppo static int
3772205eeb1aSlm66018 vd_do_process_task(vd_task_t *task)
37731ae08745Sheppo {
3774205eeb1aSlm66018 	int			i;
3775d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
3776d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
37771ae08745Sheppo 
3778d10e4ef2Snarayan 	ASSERT(vd != NULL);
3779d10e4ef2Snarayan 	ASSERT(request != NULL);
37801ae08745Sheppo 
3781d10e4ef2Snarayan 	/* Find the requested operation */
3782205eeb1aSlm66018 	for (i = 0; i < vds_noperations; i++) {
3783205eeb1aSlm66018 		if (request->operation == vds_operation[i].operation) {
3784205eeb1aSlm66018 			/* all operations should have a start func */
3785205eeb1aSlm66018 			ASSERT(vds_operation[i].start != NULL);
3786205eeb1aSlm66018 
3787205eeb1aSlm66018 			task->completef = vds_operation[i].complete;
3788d10e4ef2Snarayan 			break;
3789205eeb1aSlm66018 		}
3790205eeb1aSlm66018 	}
379117cadca8Slm66018 
379217cadca8Slm66018 	/*
379317cadca8Slm66018 	 * We need to check that the requested operation is permitted
379417cadca8Slm66018 	 * for the particular client that sent it or that the loop above
379517cadca8Slm66018 	 * did not complete without finding the operation type (indicating
379617cadca8Slm66018 	 * that the requested operation is unknown/unimplemented)
379717cadca8Slm66018 	 */
379817cadca8Slm66018 	if ((VD_OP_SUPPORTED(vd->operations, request->operation) == B_FALSE) ||
379917cadca8Slm66018 	    (i == vds_noperations)) {
38003af08d82Slm66018 		PR0("Unsupported operation %u", request->operation);
380117cadca8Slm66018 		request->status = ENOTSUP;
380217cadca8Slm66018 		return (0);
38031ae08745Sheppo 	}
38041ae08745Sheppo 
38057636cb21Slm66018 	/* Range-check slice */
380687a7269eSachartre 	if (request->slice >= vd->nslices &&
3807bae9e67eSachartre 	    ((vd->vdisk_type != VD_DISK_TYPE_DISK && vd_slice_single_slice) ||
380887a7269eSachartre 	    request->slice != VD_SLICE_NONE)) {
38093af08d82Slm66018 		PR0("Invalid \"slice\" %u (max %u) for virtual disk",
38107636cb21Slm66018 		    request->slice, (vd->nslices - 1));
3811bae9e67eSachartre 		request->status = EINVAL;
3812bae9e67eSachartre 		return (0);
38137636cb21Slm66018 	}
38147636cb21Slm66018 
3815205eeb1aSlm66018 	/*
3816205eeb1aSlm66018 	 * Call the function pointer that starts the operation.
3817205eeb1aSlm66018 	 */
3818205eeb1aSlm66018 	return (vds_operation[i].start(task));
38191ae08745Sheppo }
38201ae08745Sheppo 
3821205eeb1aSlm66018 /*
3822205eeb1aSlm66018  * Description:
3823205eeb1aSlm66018  *	This function is called by both the in-band and descriptor ring
3824205eeb1aSlm66018  *	message processing functions paths to actually execute the task
3825205eeb1aSlm66018  *	requested by the vDisk client. It in turn calls its worker
3826205eeb1aSlm66018  *	function, vd_do_process_task(), to carry our the request.
3827205eeb1aSlm66018  *
3828205eeb1aSlm66018  *	Any transport errors (e.g. LDC errors, vDisk protocol errors) are
3829205eeb1aSlm66018  *	saved in the 'status' field of the task and are propagated back
3830205eeb1aSlm66018  *	up the call stack to trigger a NACK
3831205eeb1aSlm66018  *
3832205eeb1aSlm66018  *	Any request errors (e.g. ENOTTY from an ioctl) are saved in
3833205eeb1aSlm66018  *	the 'status' field of the request and result in an ACK being sent
3834205eeb1aSlm66018  *	by the completion handler.
3835205eeb1aSlm66018  *
3836205eeb1aSlm66018  * Parameters:
3837205eeb1aSlm66018  *	task 		- structure containing the request sent from client
3838205eeb1aSlm66018  *
3839205eeb1aSlm66018  * Return Value
3840205eeb1aSlm66018  *	0		- successful synchronous request.
3841205eeb1aSlm66018  *	!= 0		- transport error (e.g. LDC errors, vDisk protocol)
3842205eeb1aSlm66018  *	EINPROGRESS	- task will be finished in a completion handler
3843205eeb1aSlm66018  */
3844205eeb1aSlm66018 static int
3845205eeb1aSlm66018 vd_process_task(vd_task_t *task)
3846205eeb1aSlm66018 {
3847205eeb1aSlm66018 	vd_t	*vd = task->vd;
3848205eeb1aSlm66018 	int	status;
38491ae08745Sheppo 
3850205eeb1aSlm66018 	DTRACE_PROBE1(task__start, vd_task_t *, task);
38513af08d82Slm66018 
3852205eeb1aSlm66018 	task->status =  vd_do_process_task(task);
3853205eeb1aSlm66018 
3854205eeb1aSlm66018 	/*
3855205eeb1aSlm66018 	 * If the task processing function returned EINPROGRESS indicating
3856205eeb1aSlm66018 	 * that the task needs completing then schedule a taskq entry to
3857205eeb1aSlm66018 	 * finish it now.
3858205eeb1aSlm66018 	 *
3859205eeb1aSlm66018 	 * Otherwise the task processing function returned either zero
3860205eeb1aSlm66018 	 * indicating that the task was finished in the start function (and we
3861205eeb1aSlm66018 	 * don't need to wait in a completion function) or the start function
3862205eeb1aSlm66018 	 * returned an error - in both cases all that needs to happen is the
3863205eeb1aSlm66018 	 * notification to the vDisk client higher up the call stack.
3864205eeb1aSlm66018 	 * If the task was using a Descriptor Ring, we need to mark it as done
3865205eeb1aSlm66018 	 * at this stage.
3866205eeb1aSlm66018 	 */
3867205eeb1aSlm66018 	if (task->status == EINPROGRESS) {
3868d10e4ef2Snarayan 		/* Queue a task to complete the operation */
3869205eeb1aSlm66018 		(void) ddi_taskq_dispatch(vd->completionq, vd_complete,
3870d10e4ef2Snarayan 		    task, DDI_SLEEP);
3871d10e4ef2Snarayan 
3872f0ca1d9aSsb155480 	} else if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE_V1_0)) {
3873205eeb1aSlm66018 		/* Update the dring element if it's a dring client */
3874205eeb1aSlm66018 		status = vd_mark_elem_done(vd, task->index,
3875205eeb1aSlm66018 		    task->request->status, task->request->nbytes);
3876205eeb1aSlm66018 		if (status == ECONNRESET)
3877205eeb1aSlm66018 			vd_mark_in_reset(vd);
3878bbfa0259Sha137994 		else if (status == EACCES)
3879bbfa0259Sha137994 			vd_need_reset(vd, B_TRUE);
3880205eeb1aSlm66018 	}
3881205eeb1aSlm66018 
3882205eeb1aSlm66018 	return (task->status);
38831ae08745Sheppo }
38841ae08745Sheppo 
38851ae08745Sheppo /*
38860a55fbb7Slm66018  * Return true if the "type", "subtype", and "env" fields of the "tag" first
38870a55fbb7Slm66018  * argument match the corresponding remaining arguments; otherwise, return false
38881ae08745Sheppo  */
38890a55fbb7Slm66018 boolean_t
38901ae08745Sheppo vd_msgtype(vio_msg_tag_t *tag, int type, int subtype, int env)
38911ae08745Sheppo {
38921ae08745Sheppo 	return ((tag->vio_msgtype == type) &&
38931ae08745Sheppo 	    (tag->vio_subtype == subtype) &&
38940a55fbb7Slm66018 	    (tag->vio_subtype_env == env)) ? B_TRUE : B_FALSE;
38951ae08745Sheppo }
38961ae08745Sheppo 
38970a55fbb7Slm66018 /*
38980a55fbb7Slm66018  * Check whether the major/minor version specified in "ver_msg" is supported
38990a55fbb7Slm66018  * by this server.
39000a55fbb7Slm66018  */
39010a55fbb7Slm66018 static boolean_t
39020a55fbb7Slm66018 vds_supported_version(vio_ver_msg_t *ver_msg)
39030a55fbb7Slm66018 {
39040a55fbb7Slm66018 	for (int i = 0; i < vds_num_versions; i++) {
39050a55fbb7Slm66018 		ASSERT(vds_version[i].major > 0);
39060a55fbb7Slm66018 		ASSERT((i == 0) ||
39070a55fbb7Slm66018 		    (vds_version[i].major < vds_version[i-1].major));
39080a55fbb7Slm66018 
39090a55fbb7Slm66018 		/*
39100a55fbb7Slm66018 		 * If the major versions match, adjust the minor version, if
39110a55fbb7Slm66018 		 * necessary, down to the highest value supported by this
39120a55fbb7Slm66018 		 * server and return true so this message will get "ack"ed;
39130a55fbb7Slm66018 		 * the client should also support all minor versions lower
39140a55fbb7Slm66018 		 * than the value it sent
39150a55fbb7Slm66018 		 */
39160a55fbb7Slm66018 		if (ver_msg->ver_major == vds_version[i].major) {
39170a55fbb7Slm66018 			if (ver_msg->ver_minor > vds_version[i].minor) {
39180a55fbb7Slm66018 				PR0("Adjusting minor version from %u to %u",
39190a55fbb7Slm66018 				    ver_msg->ver_minor, vds_version[i].minor);
39200a55fbb7Slm66018 				ver_msg->ver_minor = vds_version[i].minor;
39210a55fbb7Slm66018 			}
39220a55fbb7Slm66018 			return (B_TRUE);
39230a55fbb7Slm66018 		}
39240a55fbb7Slm66018 
39250a55fbb7Slm66018 		/*
39260a55fbb7Slm66018 		 * If the message contains a higher major version number, set
39270a55fbb7Slm66018 		 * the message's major/minor versions to the current values
39280a55fbb7Slm66018 		 * and return false, so this message will get "nack"ed with
39290a55fbb7Slm66018 		 * these values, and the client will potentially try again
39300a55fbb7Slm66018 		 * with the same or a lower version
39310a55fbb7Slm66018 		 */
39320a55fbb7Slm66018 		if (ver_msg->ver_major > vds_version[i].major) {
39330a55fbb7Slm66018 			ver_msg->ver_major = vds_version[i].major;
39340a55fbb7Slm66018 			ver_msg->ver_minor = vds_version[i].minor;
39350a55fbb7Slm66018 			return (B_FALSE);
39360a55fbb7Slm66018 		}
39370a55fbb7Slm66018 
39380a55fbb7Slm66018 		/*
39390a55fbb7Slm66018 		 * Otherwise, the message's major version is less than the
39400a55fbb7Slm66018 		 * current major version, so continue the loop to the next
39410a55fbb7Slm66018 		 * (lower) supported version
39420a55fbb7Slm66018 		 */
39430a55fbb7Slm66018 	}
39440a55fbb7Slm66018 
39450a55fbb7Slm66018 	/*
39460a55fbb7Slm66018 	 * No common version was found; "ground" the version pair in the
39470a55fbb7Slm66018 	 * message to terminate negotiation
39480a55fbb7Slm66018 	 */
39490a55fbb7Slm66018 	ver_msg->ver_major = 0;
39500a55fbb7Slm66018 	ver_msg->ver_minor = 0;
39510a55fbb7Slm66018 	return (B_FALSE);
39520a55fbb7Slm66018 }
39530a55fbb7Slm66018 
39540a55fbb7Slm66018 /*
39550a55fbb7Slm66018  * Process a version message from a client.  vds expects to receive version
39560a55fbb7Slm66018  * messages from clients seeking service, but never issues version messages
39570a55fbb7Slm66018  * itself; therefore, vds can ACK or NACK client version messages, but does
39580a55fbb7Slm66018  * not expect to receive version-message ACKs or NACKs (and will treat such
39590a55fbb7Slm66018  * messages as invalid).
39600a55fbb7Slm66018  */
39611ae08745Sheppo static int
39620a55fbb7Slm66018 vd_process_ver_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
39631ae08745Sheppo {
39641ae08745Sheppo 	vio_ver_msg_t	*ver_msg = (vio_ver_msg_t *)msg;
39651ae08745Sheppo 
39661ae08745Sheppo 
39671ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
39681ae08745Sheppo 
39691ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
39701ae08745Sheppo 	    VIO_VER_INFO)) {
39711ae08745Sheppo 		return (ENOMSG);	/* not a version message */
39721ae08745Sheppo 	}
39731ae08745Sheppo 
39741ae08745Sheppo 	if (msglen != sizeof (*ver_msg)) {
39753af08d82Slm66018 		PR0("Expected %lu-byte version message; "
39761ae08745Sheppo 		    "received %lu bytes", sizeof (*ver_msg), msglen);
39771ae08745Sheppo 		return (EBADMSG);
39781ae08745Sheppo 	}
39791ae08745Sheppo 
39801ae08745Sheppo 	if (ver_msg->dev_class != VDEV_DISK) {
39813af08d82Slm66018 		PR0("Expected device class %u (disk); received %u",
39821ae08745Sheppo 		    VDEV_DISK, ver_msg->dev_class);
39831ae08745Sheppo 		return (EBADMSG);
39841ae08745Sheppo 	}
39851ae08745Sheppo 
39860a55fbb7Slm66018 	/*
39870a55fbb7Slm66018 	 * We're talking to the expected kind of client; set our device class
39880a55fbb7Slm66018 	 * for "ack/nack" back to the client
39890a55fbb7Slm66018 	 */
39901ae08745Sheppo 	ver_msg->dev_class = VDEV_DISK_SERVER;
39910a55fbb7Slm66018 
39920a55fbb7Slm66018 	/*
39930a55fbb7Slm66018 	 * Check whether the (valid) version message specifies a version
39940a55fbb7Slm66018 	 * supported by this server.  If the version is not supported, return
39950a55fbb7Slm66018 	 * EBADMSG so the message will get "nack"ed; vds_supported_version()
39960a55fbb7Slm66018 	 * will have updated the message with a supported version for the
39970a55fbb7Slm66018 	 * client to consider
39980a55fbb7Slm66018 	 */
39990a55fbb7Slm66018 	if (!vds_supported_version(ver_msg))
40000a55fbb7Slm66018 		return (EBADMSG);
40010a55fbb7Slm66018 
40020a55fbb7Slm66018 
40030a55fbb7Slm66018 	/*
40040a55fbb7Slm66018 	 * A version has been agreed upon; use the client's SID for
40050a55fbb7Slm66018 	 * communication on this channel now
40060a55fbb7Slm66018 	 */
40070a55fbb7Slm66018 	ASSERT(!(vd->initialized & VD_SID));
40080a55fbb7Slm66018 	vd->sid = ver_msg->tag.vio_sid;
40090a55fbb7Slm66018 	vd->initialized |= VD_SID;
40100a55fbb7Slm66018 
40110a55fbb7Slm66018 	/*
401217cadca8Slm66018 	 * Store the negotiated major and minor version values in the "vd" data
401317cadca8Slm66018 	 * structure so that we can check if certain operations are supported
401417cadca8Slm66018 	 * by the client.
40150a55fbb7Slm66018 	 */
401617cadca8Slm66018 	vd->version.major = ver_msg->ver_major;
401717cadca8Slm66018 	vd->version.minor = ver_msg->ver_minor;
40180a55fbb7Slm66018 
40190a55fbb7Slm66018 	PR0("Using major version %u, minor version %u",
40200a55fbb7Slm66018 	    ver_msg->ver_major, ver_msg->ver_minor);
40211ae08745Sheppo 	return (0);
40221ae08745Sheppo }
40231ae08745Sheppo 
402417cadca8Slm66018 static void
402517cadca8Slm66018 vd_set_exported_operations(vd_t *vd)
402617cadca8Slm66018 {
402717cadca8Slm66018 	vd->operations = 0;	/* clear field */
402817cadca8Slm66018 
402917cadca8Slm66018 	/*
403017cadca8Slm66018 	 * We need to check from the highest version supported to the
403117cadca8Slm66018 	 * lowest because versions with a higher minor number implicitly
403217cadca8Slm66018 	 * support versions with a lower minor number.
403317cadca8Slm66018 	 */
403417cadca8Slm66018 	if (vio_ver_is_supported(vd->version, 1, 1)) {
403517cadca8Slm66018 		ASSERT(vd->open_flags & FREAD);
403617cadca8Slm66018 		vd->operations |= VD_OP_MASK_READ;
403717cadca8Slm66018 
403817cadca8Slm66018 		if (vd->open_flags & FWRITE)
403917cadca8Slm66018 			vd->operations |= VD_OP_MASK_WRITE;
404017cadca8Slm66018 
40412f5224aeSachartre 		if (vd->scsi)
40422f5224aeSachartre 			vd->operations |= VD_OP_MASK_SCSI;
40432f5224aeSachartre 
404417cadca8Slm66018 		if (vd->file && vd_file_is_iso_image(vd)) {
404517cadca8Slm66018 			/*
404617cadca8Slm66018 			 * can't write to ISO images, make sure that write
404717cadca8Slm66018 			 * support is not set in case administrator did not
404817cadca8Slm66018 			 * use "options=ro" when doing an ldm add-vdsdev
404917cadca8Slm66018 			 */
405017cadca8Slm66018 			vd->operations &= ~VD_OP_MASK_WRITE;
405117cadca8Slm66018 		}
405217cadca8Slm66018 	} else if (vio_ver_is_supported(vd->version, 1, 0)) {
405317cadca8Slm66018 		vd->operations = VD_OP_MASK_READ | VD_OP_MASK_WRITE;
405417cadca8Slm66018 	}
405517cadca8Slm66018 
405617cadca8Slm66018 	/* we should have already agreed on a version */
405717cadca8Slm66018 	ASSERT(vd->operations != 0);
405817cadca8Slm66018 }
405917cadca8Slm66018 
40601ae08745Sheppo static int
40611ae08745Sheppo vd_process_attr_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
40621ae08745Sheppo {
40631ae08745Sheppo 	vd_attr_msg_t	*attr_msg = (vd_attr_msg_t *)msg;
40643c96341aSnarayan 	int		status, retry = 0;
40651ae08745Sheppo 
40661ae08745Sheppo 
40671ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
40681ae08745Sheppo 
40691ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
40701ae08745Sheppo 	    VIO_ATTR_INFO)) {
4071d10e4ef2Snarayan 		PR0("Message is not an attribute message");
4072d10e4ef2Snarayan 		return (ENOMSG);
40731ae08745Sheppo 	}
40741ae08745Sheppo 
40751ae08745Sheppo 	if (msglen != sizeof (*attr_msg)) {
40763af08d82Slm66018 		PR0("Expected %lu-byte attribute message; "
40771ae08745Sheppo 		    "received %lu bytes", sizeof (*attr_msg), msglen);
40781ae08745Sheppo 		return (EBADMSG);
40791ae08745Sheppo 	}
40801ae08745Sheppo 
40811ae08745Sheppo 	if (attr_msg->max_xfer_sz == 0) {
40823af08d82Slm66018 		PR0("Received maximum transfer size of 0 from client");
40831ae08745Sheppo 		return (EBADMSG);
40841ae08745Sheppo 	}
40851ae08745Sheppo 
40861ae08745Sheppo 	if ((attr_msg->xfer_mode != VIO_DESC_MODE) &&
4087f0ca1d9aSsb155480 	    (attr_msg->xfer_mode != VIO_DRING_MODE_V1_0)) {
40883af08d82Slm66018 		PR0("Client requested unsupported transfer mode");
40891ae08745Sheppo 		return (EBADMSG);
40901ae08745Sheppo 	}
40911ae08745Sheppo 
40923c96341aSnarayan 	/*
40933c96341aSnarayan 	 * check if the underlying disk is ready, if not try accessing
40943c96341aSnarayan 	 * the device again. Open the vdisk device and extract info
40953c96341aSnarayan 	 * about it, as this is needed to respond to the attr info msg
40963c96341aSnarayan 	 */
40973c96341aSnarayan 	if ((vd->initialized & VD_DISK_READY) == 0) {
40983c96341aSnarayan 		PR0("Retry setting up disk (%s)", vd->device_path);
40993c96341aSnarayan 		do {
41003c96341aSnarayan 			status = vd_setup_vd(vd);
41013c96341aSnarayan 			if (status != EAGAIN || ++retry > vds_dev_retries)
41023c96341aSnarayan 				break;
41033c96341aSnarayan 
41043c96341aSnarayan 			/* incremental delay */
41053c96341aSnarayan 			delay(drv_usectohz(vds_dev_delay));
41063c96341aSnarayan 
41073c96341aSnarayan 			/* if vdisk is no longer enabled - return error */
41083c96341aSnarayan 			if (!vd_enabled(vd))
41093c96341aSnarayan 				return (ENXIO);
41103c96341aSnarayan 
41113c96341aSnarayan 		} while (status == EAGAIN);
41123c96341aSnarayan 
41133c96341aSnarayan 		if (status)
41143c96341aSnarayan 			return (ENXIO);
41153c96341aSnarayan 
41163c96341aSnarayan 		vd->initialized |= VD_DISK_READY;
41173c96341aSnarayan 		ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
41188fce2fd6Sachartre 		PR0("vdisk_type = %s, volume = %s, file = %s, nslices = %u",
41193c96341aSnarayan 		    ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
41208fce2fd6Sachartre 		    (vd->volume ? "yes" : "no"),
41213c96341aSnarayan 		    (vd->file ? "yes" : "no"),
41223c96341aSnarayan 		    vd->nslices);
41233c96341aSnarayan 	}
41243c96341aSnarayan 
41251ae08745Sheppo 	/* Success:  valid message and transfer mode */
41261ae08745Sheppo 	vd->xfer_mode = attr_msg->xfer_mode;
41273af08d82Slm66018 
41281ae08745Sheppo 	if (vd->xfer_mode == VIO_DESC_MODE) {
41293af08d82Slm66018 
41301ae08745Sheppo 		/*
41311ae08745Sheppo 		 * The vd_dring_inband_msg_t contains one cookie; need room
41321ae08745Sheppo 		 * for up to n-1 more cookies, where "n" is the number of full
41331ae08745Sheppo 		 * pages plus possibly one partial page required to cover
41341ae08745Sheppo 		 * "max_xfer_sz".  Add room for one more cookie if
41351ae08745Sheppo 		 * "max_xfer_sz" isn't an integral multiple of the page size.
41361ae08745Sheppo 		 * Must first get the maximum transfer size in bytes.
41371ae08745Sheppo 		 */
41381ae08745Sheppo 		size_t	max_xfer_bytes = attr_msg->vdisk_block_size ?
41391ae08745Sheppo 		    attr_msg->vdisk_block_size*attr_msg->max_xfer_sz :
41401ae08745Sheppo 		    attr_msg->max_xfer_sz;
41411ae08745Sheppo 		size_t	max_inband_msglen =
41421ae08745Sheppo 		    sizeof (vd_dring_inband_msg_t) +
41431ae08745Sheppo 		    ((max_xfer_bytes/PAGESIZE +
41441ae08745Sheppo 		    ((max_xfer_bytes % PAGESIZE) ? 1 : 0))*
41451ae08745Sheppo 		    (sizeof (ldc_mem_cookie_t)));
41461ae08745Sheppo 
41471ae08745Sheppo 		/*
41481ae08745Sheppo 		 * Set the maximum expected message length to
41491ae08745Sheppo 		 * accommodate in-band-descriptor messages with all
41501ae08745Sheppo 		 * their cookies
41511ae08745Sheppo 		 */
41521ae08745Sheppo 		vd->max_msglen = MAX(vd->max_msglen, max_inband_msglen);
4153d10e4ef2Snarayan 
4154d10e4ef2Snarayan 		/*
4155d10e4ef2Snarayan 		 * Initialize the data structure for processing in-band I/O
4156d10e4ef2Snarayan 		 * request descriptors
4157d10e4ef2Snarayan 		 */
4158d10e4ef2Snarayan 		vd->inband_task.vd	= vd;
41593af08d82Slm66018 		vd->inband_task.msg	= kmem_alloc(vd->max_msglen, KM_SLEEP);
4160d10e4ef2Snarayan 		vd->inband_task.index	= 0;
4161d10e4ef2Snarayan 		vd->inband_task.type	= VD_FINAL_RANGE_TASK;	/* range == 1 */
41621ae08745Sheppo 	}
41631ae08745Sheppo 
4164e1ebb9ecSlm66018 	/* Return the device's block size and max transfer size to the client */
41652f5224aeSachartre 	attr_msg->vdisk_block_size	= vd->vdisk_block_size;
4166e1ebb9ecSlm66018 	attr_msg->max_xfer_sz		= vd->max_xfer_sz;
4167e1ebb9ecSlm66018 
41681ae08745Sheppo 	attr_msg->vdisk_size = vd->vdisk_size;
4169bae9e67eSachartre 	attr_msg->vdisk_type = (vd_slice_single_slice)? vd->vdisk_type :
4170bae9e67eSachartre 	    VD_DISK_TYPE_DISK;
417117cadca8Slm66018 	attr_msg->vdisk_media = vd->vdisk_media;
417217cadca8Slm66018 
417317cadca8Slm66018 	/* Discover and save the list of supported VD_OP_XXX operations */
417417cadca8Slm66018 	vd_set_exported_operations(vd);
417517cadca8Slm66018 	attr_msg->operations = vd->operations;
417617cadca8Slm66018 
41771ae08745Sheppo 	PR0("%s", VD_CLIENT(vd));
41783af08d82Slm66018 
41793af08d82Slm66018 	ASSERT(vd->dring_task == NULL);
41803af08d82Slm66018 
41811ae08745Sheppo 	return (0);
41821ae08745Sheppo }
41831ae08745Sheppo 
41841ae08745Sheppo static int
41851ae08745Sheppo vd_process_dring_reg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
41861ae08745Sheppo {
41871ae08745Sheppo 	int			status;
41881ae08745Sheppo 	size_t			expected;
41891ae08745Sheppo 	ldc_mem_info_t		dring_minfo;
4190bbfa0259Sha137994 	uint8_t			mtype;
41911ae08745Sheppo 	vio_dring_reg_msg_t	*reg_msg = (vio_dring_reg_msg_t *)msg;
41921ae08745Sheppo 
41931ae08745Sheppo 
41941ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
41951ae08745Sheppo 
41961ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
41971ae08745Sheppo 	    VIO_DRING_REG)) {
4198d10e4ef2Snarayan 		PR0("Message is not a register-dring message");
4199d10e4ef2Snarayan 		return (ENOMSG);
42001ae08745Sheppo 	}
42011ae08745Sheppo 
42021ae08745Sheppo 	if (msglen < sizeof (*reg_msg)) {
42033af08d82Slm66018 		PR0("Expected at least %lu-byte register-dring message; "
42041ae08745Sheppo 		    "received %lu bytes", sizeof (*reg_msg), msglen);
42051ae08745Sheppo 		return (EBADMSG);
42061ae08745Sheppo 	}
42071ae08745Sheppo 
42081ae08745Sheppo 	expected = sizeof (*reg_msg) +
42091ae08745Sheppo 	    (reg_msg->ncookies - 1)*(sizeof (reg_msg->cookie[0]));
42101ae08745Sheppo 	if (msglen != expected) {
42113af08d82Slm66018 		PR0("Expected %lu-byte register-dring message; "
42121ae08745Sheppo 		    "received %lu bytes", expected, msglen);
42131ae08745Sheppo 		return (EBADMSG);
42141ae08745Sheppo 	}
42151ae08745Sheppo 
42161ae08745Sheppo 	if (vd->initialized & VD_DRING) {
42173af08d82Slm66018 		PR0("A dring was previously registered; only support one");
42181ae08745Sheppo 		return (EBADMSG);
42191ae08745Sheppo 	}
42201ae08745Sheppo 
4221d10e4ef2Snarayan 	if (reg_msg->num_descriptors > INT32_MAX) {
42223af08d82Slm66018 		PR0("reg_msg->num_descriptors = %u; must be <= %u (%s)",
4223d10e4ef2Snarayan 		    reg_msg->ncookies, INT32_MAX, STRINGIZE(INT32_MAX));
4224d10e4ef2Snarayan 		return (EBADMSG);
4225d10e4ef2Snarayan 	}
4226d10e4ef2Snarayan 
42271ae08745Sheppo 	if (reg_msg->ncookies != 1) {
42281ae08745Sheppo 		/*
42291ae08745Sheppo 		 * In addition to fixing the assertion in the success case
42301ae08745Sheppo 		 * below, supporting drings which require more than one
42311ae08745Sheppo 		 * "cookie" requires increasing the value of vd->max_msglen
42321ae08745Sheppo 		 * somewhere in the code path prior to receiving the message
42331ae08745Sheppo 		 * which results in calling this function.  Note that without
42341ae08745Sheppo 		 * making this change, the larger message size required to
42351ae08745Sheppo 		 * accommodate multiple cookies cannot be successfully
42361ae08745Sheppo 		 * received, so this function will not even get called.
42371ae08745Sheppo 		 * Gracefully accommodating more dring cookies might
42381ae08745Sheppo 		 * reasonably demand exchanging an additional attribute or
42391ae08745Sheppo 		 * making a minor protocol adjustment
42401ae08745Sheppo 		 */
42413af08d82Slm66018 		PR0("reg_msg->ncookies = %u != 1", reg_msg->ncookies);
42421ae08745Sheppo 		return (EBADMSG);
42431ae08745Sheppo 	}
42441ae08745Sheppo 
4245bbfa0259Sha137994 	if (vd_direct_mapped_drings)
4246bbfa0259Sha137994 		mtype = LDC_DIRECT_MAP;
4247bbfa0259Sha137994 	else
4248bbfa0259Sha137994 		mtype = LDC_SHADOW_MAP;
4249bbfa0259Sha137994 
42501ae08745Sheppo 	status = ldc_mem_dring_map(vd->ldc_handle, reg_msg->cookie,
42511ae08745Sheppo 	    reg_msg->ncookies, reg_msg->num_descriptors,
4252bbfa0259Sha137994 	    reg_msg->descriptor_size, mtype, &vd->dring_handle);
42531ae08745Sheppo 	if (status != 0) {
42543af08d82Slm66018 		PR0("ldc_mem_dring_map() returned errno %d", status);
42551ae08745Sheppo 		return (status);
42561ae08745Sheppo 	}
42571ae08745Sheppo 
42581ae08745Sheppo 	/*
42591ae08745Sheppo 	 * To remove the need for this assertion, must call
42601ae08745Sheppo 	 * ldc_mem_dring_nextcookie() successfully ncookies-1 times after a
42611ae08745Sheppo 	 * successful call to ldc_mem_dring_map()
42621ae08745Sheppo 	 */
42631ae08745Sheppo 	ASSERT(reg_msg->ncookies == 1);
42641ae08745Sheppo 
42651ae08745Sheppo 	if ((status =
42661ae08745Sheppo 	    ldc_mem_dring_info(vd->dring_handle, &dring_minfo)) != 0) {
42673af08d82Slm66018 		PR0("ldc_mem_dring_info() returned errno %d", status);
42681ae08745Sheppo 		if ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0)
42693af08d82Slm66018 			PR0("ldc_mem_dring_unmap() returned errno %d", status);
42701ae08745Sheppo 		return (status);
42711ae08745Sheppo 	}
42721ae08745Sheppo 
42731ae08745Sheppo 	if (dring_minfo.vaddr == NULL) {
42743af08d82Slm66018 		PR0("Descriptor ring virtual address is NULL");
42750a55fbb7Slm66018 		return (ENXIO);
42761ae08745Sheppo 	}
42771ae08745Sheppo 
42781ae08745Sheppo 
4279d10e4ef2Snarayan 	/* Initialize for valid message and mapped dring */
42801ae08745Sheppo 	PR1("descriptor size = %u, dring length = %u",
42811ae08745Sheppo 	    vd->descriptor_size, vd->dring_len);
42821ae08745Sheppo 	vd->initialized |= VD_DRING;
42831ae08745Sheppo 	vd->dring_ident = 1;	/* "There Can Be Only One" */
42841ae08745Sheppo 	vd->dring = dring_minfo.vaddr;
42851ae08745Sheppo 	vd->descriptor_size = reg_msg->descriptor_size;
42861ae08745Sheppo 	vd->dring_len = reg_msg->num_descriptors;
4287bbfa0259Sha137994 	vd->dring_mtype = dring_minfo.mtype;
42881ae08745Sheppo 	reg_msg->dring_ident = vd->dring_ident;
4289d10e4ef2Snarayan 
4290d10e4ef2Snarayan 	/*
4291d10e4ef2Snarayan 	 * Allocate and initialize a "shadow" array of data structures for
4292d10e4ef2Snarayan 	 * tasks to process I/O requests in dring elements
4293d10e4ef2Snarayan 	 */
4294d10e4ef2Snarayan 	vd->dring_task =
4295d10e4ef2Snarayan 	    kmem_zalloc((sizeof (*vd->dring_task)) * vd->dring_len, KM_SLEEP);
4296d10e4ef2Snarayan 	for (int i = 0; i < vd->dring_len; i++) {
4297d10e4ef2Snarayan 		vd->dring_task[i].vd		= vd;
4298d10e4ef2Snarayan 		vd->dring_task[i].index		= i;
4299d10e4ef2Snarayan 		vd->dring_task[i].request	= &VD_DRING_ELEM(i)->payload;
43004bac2208Snarayan 
43014bac2208Snarayan 		status = ldc_mem_alloc_handle(vd->ldc_handle,
43024bac2208Snarayan 		    &(vd->dring_task[i].mhdl));
43034bac2208Snarayan 		if (status) {
43043af08d82Slm66018 			PR0("ldc_mem_alloc_handle() returned err %d ", status);
43054bac2208Snarayan 			return (ENXIO);
43064bac2208Snarayan 		}
43073af08d82Slm66018 
43083af08d82Slm66018 		vd->dring_task[i].msg = kmem_alloc(vd->max_msglen, KM_SLEEP);
4309d10e4ef2Snarayan 	}
4310d10e4ef2Snarayan 
43111ae08745Sheppo 	return (0);
43121ae08745Sheppo }
43131ae08745Sheppo 
43141ae08745Sheppo static int
43151ae08745Sheppo vd_process_dring_unreg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
43161ae08745Sheppo {
43171ae08745Sheppo 	vio_dring_unreg_msg_t	*unreg_msg = (vio_dring_unreg_msg_t *)msg;
43181ae08745Sheppo 
43191ae08745Sheppo 
43201ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
43211ae08745Sheppo 
43221ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
43231ae08745Sheppo 	    VIO_DRING_UNREG)) {
4324d10e4ef2Snarayan 		PR0("Message is not an unregister-dring message");
4325d10e4ef2Snarayan 		return (ENOMSG);
43261ae08745Sheppo 	}
43271ae08745Sheppo 
43281ae08745Sheppo 	if (msglen != sizeof (*unreg_msg)) {
43293af08d82Slm66018 		PR0("Expected %lu-byte unregister-dring message; "
43301ae08745Sheppo 		    "received %lu bytes", sizeof (*unreg_msg), msglen);
43311ae08745Sheppo 		return (EBADMSG);
43321ae08745Sheppo 	}
43331ae08745Sheppo 
43341ae08745Sheppo 	if (unreg_msg->dring_ident != vd->dring_ident) {
43353af08d82Slm66018 		PR0("Expected dring ident %lu; received %lu",
43361ae08745Sheppo 		    vd->dring_ident, unreg_msg->dring_ident);
43371ae08745Sheppo 		return (EBADMSG);
43381ae08745Sheppo 	}
43391ae08745Sheppo 
43401ae08745Sheppo 	return (0);
43411ae08745Sheppo }
43421ae08745Sheppo 
43431ae08745Sheppo static int
43441ae08745Sheppo process_rdx_msg(vio_msg_t *msg, size_t msglen)
43451ae08745Sheppo {
43461ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
43471ae08745Sheppo 
4348d10e4ef2Snarayan 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX)) {
4349d10e4ef2Snarayan 		PR0("Message is not an RDX message");
4350d10e4ef2Snarayan 		return (ENOMSG);
4351d10e4ef2Snarayan 	}
43521ae08745Sheppo 
43531ae08745Sheppo 	if (msglen != sizeof (vio_rdx_msg_t)) {
43543af08d82Slm66018 		PR0("Expected %lu-byte RDX message; received %lu bytes",
43551ae08745Sheppo 		    sizeof (vio_rdx_msg_t), msglen);
43561ae08745Sheppo 		return (EBADMSG);
43571ae08745Sheppo 	}
43581ae08745Sheppo 
4359d10e4ef2Snarayan 	PR0("Valid RDX message");
43601ae08745Sheppo 	return (0);
43611ae08745Sheppo }
43621ae08745Sheppo 
43631ae08745Sheppo static int
43641ae08745Sheppo vd_check_seq_num(vd_t *vd, uint64_t seq_num)
43651ae08745Sheppo {
43661ae08745Sheppo 	if ((vd->initialized & VD_SEQ_NUM) && (seq_num != vd->seq_num + 1)) {
43673af08d82Slm66018 		PR0("Received seq_num %lu; expected %lu",
43681ae08745Sheppo 		    seq_num, (vd->seq_num + 1));
43693af08d82Slm66018 		PR0("initiating soft reset");
4370d10e4ef2Snarayan 		vd_need_reset(vd, B_FALSE);
43711ae08745Sheppo 		return (1);
43721ae08745Sheppo 	}
43731ae08745Sheppo 
43741ae08745Sheppo 	vd->seq_num = seq_num;
43751ae08745Sheppo 	vd->initialized |= VD_SEQ_NUM;	/* superfluous after first time... */
43761ae08745Sheppo 	return (0);
43771ae08745Sheppo }
43781ae08745Sheppo 
43791ae08745Sheppo /*
43801ae08745Sheppo  * Return the expected size of an inband-descriptor message with all the
43811ae08745Sheppo  * cookies it claims to include
43821ae08745Sheppo  */
43831ae08745Sheppo static size_t
43841ae08745Sheppo expected_inband_size(vd_dring_inband_msg_t *msg)
43851ae08745Sheppo {
43861ae08745Sheppo 	return ((sizeof (*msg)) +
43871ae08745Sheppo 	    (msg->payload.ncookies - 1)*(sizeof (msg->payload.cookie[0])));
43881ae08745Sheppo }
43891ae08745Sheppo 
43901ae08745Sheppo /*
43911ae08745Sheppo  * Process an in-band descriptor message:  used with clients like OBP, with
43921ae08745Sheppo  * which vds exchanges descriptors within VIO message payloads, rather than
43931ae08745Sheppo  * operating on them within a descriptor ring
43941ae08745Sheppo  */
43951ae08745Sheppo static int
43963af08d82Slm66018 vd_process_desc_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
43971ae08745Sheppo {
43981ae08745Sheppo 	size_t			expected;
43991ae08745Sheppo 	vd_dring_inband_msg_t	*desc_msg = (vd_dring_inband_msg_t *)msg;
44001ae08745Sheppo 
44011ae08745Sheppo 
44021ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
44031ae08745Sheppo 
44041ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
4405d10e4ef2Snarayan 	    VIO_DESC_DATA)) {
4406d10e4ef2Snarayan 		PR1("Message is not an in-band-descriptor message");
4407d10e4ef2Snarayan 		return (ENOMSG);
4408d10e4ef2Snarayan 	}
44091ae08745Sheppo 
44101ae08745Sheppo 	if (msglen < sizeof (*desc_msg)) {
44113af08d82Slm66018 		PR0("Expected at least %lu-byte descriptor message; "
44121ae08745Sheppo 		    "received %lu bytes", sizeof (*desc_msg), msglen);
44131ae08745Sheppo 		return (EBADMSG);
44141ae08745Sheppo 	}
44151ae08745Sheppo 
44161ae08745Sheppo 	if (msglen != (expected = expected_inband_size(desc_msg))) {
44173af08d82Slm66018 		PR0("Expected %lu-byte descriptor message; "
44181ae08745Sheppo 		    "received %lu bytes", expected, msglen);
44191ae08745Sheppo 		return (EBADMSG);
44201ae08745Sheppo 	}
44211ae08745Sheppo 
4422d10e4ef2Snarayan 	if (vd_check_seq_num(vd, desc_msg->hdr.seq_num) != 0)
44231ae08745Sheppo 		return (EBADMSG);
44241ae08745Sheppo 
4425d10e4ef2Snarayan 	/*
4426d10e4ef2Snarayan 	 * Valid message:  Set up the in-band descriptor task and process the
4427d10e4ef2Snarayan 	 * request.  Arrange to acknowledge the client's message, unless an
4428d10e4ef2Snarayan 	 * error processing the descriptor task results in setting
4429d10e4ef2Snarayan 	 * VIO_SUBTYPE_NACK
4430d10e4ef2Snarayan 	 */
4431d10e4ef2Snarayan 	PR1("Valid in-band-descriptor message");
4432d10e4ef2Snarayan 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
44333af08d82Slm66018 
44343af08d82Slm66018 	ASSERT(vd->inband_task.msg != NULL);
44353af08d82Slm66018 
44363af08d82Slm66018 	bcopy(msg, vd->inband_task.msg, msglen);
4437d10e4ef2Snarayan 	vd->inband_task.msglen	= msglen;
44383af08d82Slm66018 
44393af08d82Slm66018 	/*
44403af08d82Slm66018 	 * The task request is now the payload of the message
44413af08d82Slm66018 	 * that was just copied into the body of the task.
44423af08d82Slm66018 	 */
44433af08d82Slm66018 	desc_msg = (vd_dring_inband_msg_t *)vd->inband_task.msg;
4444d10e4ef2Snarayan 	vd->inband_task.request	= &desc_msg->payload;
44453af08d82Slm66018 
4446d10e4ef2Snarayan 	return (vd_process_task(&vd->inband_task));
44471ae08745Sheppo }
44481ae08745Sheppo 
44491ae08745Sheppo static int
4450d10e4ef2Snarayan vd_process_element(vd_t *vd, vd_task_type_t type, uint32_t idx,
44513af08d82Slm66018     vio_msg_t *msg, size_t msglen)
44521ae08745Sheppo {
44531ae08745Sheppo 	int			status;
4454d10e4ef2Snarayan 	boolean_t		ready;
4455bbfa0259Sha137994 	on_trap_data_t		otd;
4456d10e4ef2Snarayan 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
44571ae08745Sheppo 
4458d10e4ef2Snarayan 	/* Accept the updated dring element */
4459bbfa0259Sha137994 	if ((status = VIO_DRING_ACQUIRE(&otd, vd->dring_mtype,
4460bbfa0259Sha137994 	    vd->dring_handle, idx, idx)) != 0) {
44611ae08745Sheppo 		return (status);
44621ae08745Sheppo 	}
4463d10e4ef2Snarayan 	ready = (elem->hdr.dstate == VIO_DESC_READY);
4464d10e4ef2Snarayan 	if (ready) {
4465d10e4ef2Snarayan 		elem->hdr.dstate = VIO_DESC_ACCEPTED;
4466d10e4ef2Snarayan 	} else {
44673af08d82Slm66018 		PR0("descriptor %u not ready", idx);
4468d10e4ef2Snarayan 		VD_DUMP_DRING_ELEM(elem);
4469d10e4ef2Snarayan 	}
4470bbfa0259Sha137994 	if ((status = VIO_DRING_RELEASE(vd->dring_mtype,
4471bbfa0259Sha137994 	    vd->dring_handle, idx, idx)) != 0) {
4472bbfa0259Sha137994 		PR0("VIO_DRING_RELEASE() returned errno %d", status);
44731ae08745Sheppo 		return (status);
44741ae08745Sheppo 	}
4475d10e4ef2Snarayan 	if (!ready)
4476d10e4ef2Snarayan 		return (EBUSY);
44771ae08745Sheppo 
44781ae08745Sheppo 
4479d10e4ef2Snarayan 	/* Initialize a task and process the accepted element */
4480d10e4ef2Snarayan 	PR1("Processing dring element %u", idx);
4481d10e4ef2Snarayan 	vd->dring_task[idx].type	= type;
44823af08d82Slm66018 
44833af08d82Slm66018 	/* duplicate msg buf for cookies etc. */
44843af08d82Slm66018 	bcopy(msg, vd->dring_task[idx].msg, msglen);
44853af08d82Slm66018 
4486d10e4ef2Snarayan 	vd->dring_task[idx].msglen	= msglen;
4487205eeb1aSlm66018 	return (vd_process_task(&vd->dring_task[idx]));
44881ae08745Sheppo }
44891ae08745Sheppo 
44901ae08745Sheppo static int
4491d10e4ef2Snarayan vd_process_element_range(vd_t *vd, int start, int end,
44923af08d82Slm66018     vio_msg_t *msg, size_t msglen)
4493d10e4ef2Snarayan {
4494d10e4ef2Snarayan 	int		i, n, nelem, status = 0;
4495d10e4ef2Snarayan 	boolean_t	inprogress = B_FALSE;
4496d10e4ef2Snarayan 	vd_task_type_t	type;
4497d10e4ef2Snarayan 
4498d10e4ef2Snarayan 
4499d10e4ef2Snarayan 	ASSERT(start >= 0);
4500d10e4ef2Snarayan 	ASSERT(end >= 0);
4501d10e4ef2Snarayan 
4502d10e4ef2Snarayan 	/*
4503d10e4ef2Snarayan 	 * Arrange to acknowledge the client's message, unless an error
4504d10e4ef2Snarayan 	 * processing one of the dring elements results in setting
4505d10e4ef2Snarayan 	 * VIO_SUBTYPE_NACK
4506d10e4ef2Snarayan 	 */
4507d10e4ef2Snarayan 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
4508d10e4ef2Snarayan 
4509d10e4ef2Snarayan 	/*
4510d10e4ef2Snarayan 	 * Process the dring elements in the range
4511d10e4ef2Snarayan 	 */
4512d10e4ef2Snarayan 	nelem = ((end < start) ? end + vd->dring_len : end) - start + 1;
4513d10e4ef2Snarayan 	for (i = start, n = nelem; n > 0; i = (i + 1) % vd->dring_len, n--) {
4514d10e4ef2Snarayan 		((vio_dring_msg_t *)msg)->end_idx = i;
4515d10e4ef2Snarayan 		type = (n == 1) ? VD_FINAL_RANGE_TASK : VD_NONFINAL_RANGE_TASK;
45163af08d82Slm66018 		status = vd_process_element(vd, type, i, msg, msglen);
4517d10e4ef2Snarayan 		if (status == EINPROGRESS)
4518d10e4ef2Snarayan 			inprogress = B_TRUE;
4519d10e4ef2Snarayan 		else if (status != 0)
4520d10e4ef2Snarayan 			break;
4521d10e4ef2Snarayan 	}
4522d10e4ef2Snarayan 
4523d10e4ef2Snarayan 	/*
4524d10e4ef2Snarayan 	 * If some, but not all, operations of a multi-element range are in
4525d10e4ef2Snarayan 	 * progress, wait for other operations to complete before returning
4526d10e4ef2Snarayan 	 * (which will result in "ack" or "nack" of the message).  Note that
4527d10e4ef2Snarayan 	 * all outstanding operations will need to complete, not just the ones
4528d10e4ef2Snarayan 	 * corresponding to the current range of dring elements; howevever, as
4529d10e4ef2Snarayan 	 * this situation is an error case, performance is less critical.
4530d10e4ef2Snarayan 	 */
4531d10e4ef2Snarayan 	if ((nelem > 1) && (status != EINPROGRESS) && inprogress)
4532d10e4ef2Snarayan 		ddi_taskq_wait(vd->completionq);
4533d10e4ef2Snarayan 
4534d10e4ef2Snarayan 	return (status);
4535d10e4ef2Snarayan }
4536d10e4ef2Snarayan 
4537d10e4ef2Snarayan static int
45383af08d82Slm66018 vd_process_dring_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
45391ae08745Sheppo {
45401ae08745Sheppo 	vio_dring_msg_t	*dring_msg = (vio_dring_msg_t *)msg;
45411ae08745Sheppo 
45421ae08745Sheppo 
45431ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
45441ae08745Sheppo 
45451ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
45461ae08745Sheppo 	    VIO_DRING_DATA)) {
4547d10e4ef2Snarayan 		PR1("Message is not a dring-data message");
4548d10e4ef2Snarayan 		return (ENOMSG);
45491ae08745Sheppo 	}
45501ae08745Sheppo 
45511ae08745Sheppo 	if (msglen != sizeof (*dring_msg)) {
45523af08d82Slm66018 		PR0("Expected %lu-byte dring message; received %lu bytes",
45531ae08745Sheppo 		    sizeof (*dring_msg), msglen);
45541ae08745Sheppo 		return (EBADMSG);
45551ae08745Sheppo 	}
45561ae08745Sheppo 
4557d10e4ef2Snarayan 	if (vd_check_seq_num(vd, dring_msg->seq_num) != 0)
45581ae08745Sheppo 		return (EBADMSG);
45591ae08745Sheppo 
45601ae08745Sheppo 	if (dring_msg->dring_ident != vd->dring_ident) {
45613af08d82Slm66018 		PR0("Expected dring ident %lu; received ident %lu",
45621ae08745Sheppo 		    vd->dring_ident, dring_msg->dring_ident);
45631ae08745Sheppo 		return (EBADMSG);
45641ae08745Sheppo 	}
45651ae08745Sheppo 
4566d10e4ef2Snarayan 	if (dring_msg->start_idx >= vd->dring_len) {
45673af08d82Slm66018 		PR0("\"start_idx\" = %u; must be less than %u",
4568d10e4ef2Snarayan 		    dring_msg->start_idx, vd->dring_len);
4569d10e4ef2Snarayan 		return (EBADMSG);
4570d10e4ef2Snarayan 	}
45711ae08745Sheppo 
4572d10e4ef2Snarayan 	if ((dring_msg->end_idx < 0) ||
4573d10e4ef2Snarayan 	    (dring_msg->end_idx >= vd->dring_len)) {
45743af08d82Slm66018 		PR0("\"end_idx\" = %u; must be >= 0 and less than %u",
4575d10e4ef2Snarayan 		    dring_msg->end_idx, vd->dring_len);
4576d10e4ef2Snarayan 		return (EBADMSG);
4577d10e4ef2Snarayan 	}
4578d10e4ef2Snarayan 
4579d10e4ef2Snarayan 	/* Valid message; process range of updated dring elements */
4580d10e4ef2Snarayan 	PR1("Processing descriptor range, start = %u, end = %u",
4581d10e4ef2Snarayan 	    dring_msg->start_idx, dring_msg->end_idx);
4582d10e4ef2Snarayan 	return (vd_process_element_range(vd, dring_msg->start_idx,
45833af08d82Slm66018 	    dring_msg->end_idx, msg, msglen));
45841ae08745Sheppo }
45851ae08745Sheppo 
45861ae08745Sheppo static int
45871ae08745Sheppo recv_msg(ldc_handle_t ldc_handle, void *msg, size_t *nbytes)
45881ae08745Sheppo {
45891ae08745Sheppo 	int	retry, status;
45901ae08745Sheppo 	size_t	size = *nbytes;
45911ae08745Sheppo 
45921ae08745Sheppo 
45931ae08745Sheppo 	for (retry = 0, status = ETIMEDOUT;
45941ae08745Sheppo 	    retry < vds_ldc_retries && status == ETIMEDOUT;
45951ae08745Sheppo 	    retry++) {
45961ae08745Sheppo 		PR1("ldc_read() attempt %d", (retry + 1));
45971ae08745Sheppo 		*nbytes = size;
45981ae08745Sheppo 		status = ldc_read(ldc_handle, msg, nbytes);
45991ae08745Sheppo 	}
46001ae08745Sheppo 
46013af08d82Slm66018 	if (status) {
46023af08d82Slm66018 		PR0("ldc_read() returned errno %d", status);
46033af08d82Slm66018 		if (status != ECONNRESET)
46043af08d82Slm66018 			return (ENOMSG);
46051ae08745Sheppo 		return (status);
46061ae08745Sheppo 	} else if (*nbytes == 0) {
46071ae08745Sheppo 		PR1("ldc_read() returned 0 and no message read");
46081ae08745Sheppo 		return (ENOMSG);
46091ae08745Sheppo 	}
46101ae08745Sheppo 
46111ae08745Sheppo 	PR1("RCVD %lu-byte message", *nbytes);
46121ae08745Sheppo 	return (0);
46131ae08745Sheppo }
46141ae08745Sheppo 
46151ae08745Sheppo static int
46163af08d82Slm66018 vd_do_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
46171ae08745Sheppo {
46181ae08745Sheppo 	int		status;
46191ae08745Sheppo 
46201ae08745Sheppo 
46211ae08745Sheppo 	PR1("Processing (%x/%x/%x) message", msg->tag.vio_msgtype,
46221ae08745Sheppo 	    msg->tag.vio_subtype, msg->tag.vio_subtype_env);
46233af08d82Slm66018 #ifdef	DEBUG
46243af08d82Slm66018 	vd_decode_tag(msg);
46253af08d82Slm66018 #endif
46261ae08745Sheppo 
46271ae08745Sheppo 	/*
46281ae08745Sheppo 	 * Validate session ID up front, since it applies to all messages
46291ae08745Sheppo 	 * once set
46301ae08745Sheppo 	 */
46311ae08745Sheppo 	if ((msg->tag.vio_sid != vd->sid) && (vd->initialized & VD_SID)) {
46323af08d82Slm66018 		PR0("Expected SID %u, received %u", vd->sid,
46331ae08745Sheppo 		    msg->tag.vio_sid);
46341ae08745Sheppo 		return (EBADMSG);
46351ae08745Sheppo 	}
46361ae08745Sheppo 
46373af08d82Slm66018 	PR1("\tWhile in state %d (%s)", vd->state, vd_decode_state(vd->state));
46381ae08745Sheppo 
46391ae08745Sheppo 	/*
46401ae08745Sheppo 	 * Process the received message based on connection state
46411ae08745Sheppo 	 */
46421ae08745Sheppo 	switch (vd->state) {
46431ae08745Sheppo 	case VD_STATE_INIT:	/* expect version message */
46440a55fbb7Slm66018 		if ((status = vd_process_ver_msg(vd, msg, msglen)) != 0)
46451ae08745Sheppo 			return (status);
46461ae08745Sheppo 
46471ae08745Sheppo 		/* Version negotiated, move to that state */
46481ae08745Sheppo 		vd->state = VD_STATE_VER;
46491ae08745Sheppo 		return (0);
46501ae08745Sheppo 
46511ae08745Sheppo 	case VD_STATE_VER:	/* expect attribute message */
46521ae08745Sheppo 		if ((status = vd_process_attr_msg(vd, msg, msglen)) != 0)
46531ae08745Sheppo 			return (status);
46541ae08745Sheppo 
46551ae08745Sheppo 		/* Attributes exchanged, move to that state */
46561ae08745Sheppo 		vd->state = VD_STATE_ATTR;
46571ae08745Sheppo 		return (0);
46581ae08745Sheppo 
46591ae08745Sheppo 	case VD_STATE_ATTR:
46601ae08745Sheppo 		switch (vd->xfer_mode) {
46611ae08745Sheppo 		case VIO_DESC_MODE:	/* expect RDX message */
46621ae08745Sheppo 			if ((status = process_rdx_msg(msg, msglen)) != 0)
46631ae08745Sheppo 				return (status);
46641ae08745Sheppo 
46651ae08745Sheppo 			/* Ready to receive in-band descriptors */
46661ae08745Sheppo 			vd->state = VD_STATE_DATA;
46671ae08745Sheppo 			return (0);
46681ae08745Sheppo 
4669f0ca1d9aSsb155480 		case VIO_DRING_MODE_V1_0:  /* expect register-dring message */
46701ae08745Sheppo 			if ((status =
46711ae08745Sheppo 			    vd_process_dring_reg_msg(vd, msg, msglen)) != 0)
46721ae08745Sheppo 				return (status);
46731ae08745Sheppo 
46741ae08745Sheppo 			/* One dring negotiated, move to that state */
46751ae08745Sheppo 			vd->state = VD_STATE_DRING;
46761ae08745Sheppo 			return (0);
46771ae08745Sheppo 
46781ae08745Sheppo 		default:
46791ae08745Sheppo 			ASSERT("Unsupported transfer mode");
46803af08d82Slm66018 			PR0("Unsupported transfer mode");
46811ae08745Sheppo 			return (ENOTSUP);
46821ae08745Sheppo 		}
46831ae08745Sheppo 
46841ae08745Sheppo 	case VD_STATE_DRING:	/* expect RDX, register-dring, or unreg-dring */
46851ae08745Sheppo 		if ((status = process_rdx_msg(msg, msglen)) == 0) {
46861ae08745Sheppo 			/* Ready to receive data */
46871ae08745Sheppo 			vd->state = VD_STATE_DATA;
46881ae08745Sheppo 			return (0);
46891ae08745Sheppo 		} else if (status != ENOMSG) {
46901ae08745Sheppo 			return (status);
46911ae08745Sheppo 		}
46921ae08745Sheppo 
46931ae08745Sheppo 
46941ae08745Sheppo 		/*
46951ae08745Sheppo 		 * If another register-dring message is received, stay in
46961ae08745Sheppo 		 * dring state in case the client sends RDX; although the
46971ae08745Sheppo 		 * protocol allows multiple drings, this server does not
46981ae08745Sheppo 		 * support using more than one
46991ae08745Sheppo 		 */
47001ae08745Sheppo 		if ((status =
47011ae08745Sheppo 		    vd_process_dring_reg_msg(vd, msg, msglen)) != ENOMSG)
47021ae08745Sheppo 			return (status);
47031ae08745Sheppo 
47041ae08745Sheppo 		/*
47051ae08745Sheppo 		 * Acknowledge an unregister-dring message, but reset the
47061ae08745Sheppo 		 * connection anyway:  Although the protocol allows
47071ae08745Sheppo 		 * unregistering drings, this server cannot serve a vdisk
47081ae08745Sheppo 		 * without its only dring
47091ae08745Sheppo 		 */
47101ae08745Sheppo 		status = vd_process_dring_unreg_msg(vd, msg, msglen);
47111ae08745Sheppo 		return ((status == 0) ? ENOTSUP : status);
47121ae08745Sheppo 
47131ae08745Sheppo 	case VD_STATE_DATA:
47141ae08745Sheppo 		switch (vd->xfer_mode) {
47151ae08745Sheppo 		case VIO_DESC_MODE:	/* expect in-band-descriptor message */
47163af08d82Slm66018 			return (vd_process_desc_msg(vd, msg, msglen));
47171ae08745Sheppo 
4718f0ca1d9aSsb155480 		case VIO_DRING_MODE_V1_0: /* expect dring-data or unreg-dring */
47191ae08745Sheppo 			/*
47201ae08745Sheppo 			 * Typically expect dring-data messages, so handle
47211ae08745Sheppo 			 * them first
47221ae08745Sheppo 			 */
47231ae08745Sheppo 			if ((status = vd_process_dring_msg(vd, msg,
47243af08d82Slm66018 			    msglen)) != ENOMSG)
47251ae08745Sheppo 				return (status);
47261ae08745Sheppo 
47271ae08745Sheppo 			/*
47281ae08745Sheppo 			 * Acknowledge an unregister-dring message, but reset
47291ae08745Sheppo 			 * the connection anyway:  Although the protocol
47301ae08745Sheppo 			 * allows unregistering drings, this server cannot
47311ae08745Sheppo 			 * serve a vdisk without its only dring
47321ae08745Sheppo 			 */
47331ae08745Sheppo 			status = vd_process_dring_unreg_msg(vd, msg, msglen);
47341ae08745Sheppo 			return ((status == 0) ? ENOTSUP : status);
47351ae08745Sheppo 
47361ae08745Sheppo 		default:
47371ae08745Sheppo 			ASSERT("Unsupported transfer mode");
47383af08d82Slm66018 			PR0("Unsupported transfer mode");
47391ae08745Sheppo 			return (ENOTSUP);
47401ae08745Sheppo 		}
47411ae08745Sheppo 
47421ae08745Sheppo 	default:
47431ae08745Sheppo 		ASSERT("Invalid client connection state");
47443af08d82Slm66018 		PR0("Invalid client connection state");
47451ae08745Sheppo 		return (ENOTSUP);
47461ae08745Sheppo 	}
47471ae08745Sheppo }
47481ae08745Sheppo 
4749d10e4ef2Snarayan static int
47503af08d82Slm66018 vd_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
47511ae08745Sheppo {
47521ae08745Sheppo 	int		status;
47531ae08745Sheppo 	boolean_t	reset_ldc = B_FALSE;
4754205eeb1aSlm66018 	vd_task_t	task;
47551ae08745Sheppo 
47561ae08745Sheppo 	/*
47571ae08745Sheppo 	 * Check that the message is at least big enough for a "tag", so that
47581ae08745Sheppo 	 * message processing can proceed based on tag-specified message type
47591ae08745Sheppo 	 */
47601ae08745Sheppo 	if (msglen < sizeof (vio_msg_tag_t)) {
47613af08d82Slm66018 		PR0("Received short (%lu-byte) message", msglen);
47621ae08745Sheppo 		/* Can't "nack" short message, so drop the big hammer */
47633af08d82Slm66018 		PR0("initiating full reset");
4764d10e4ef2Snarayan 		vd_need_reset(vd, B_TRUE);
4765d10e4ef2Snarayan 		return (EBADMSG);
47661ae08745Sheppo 	}
47671ae08745Sheppo 
47681ae08745Sheppo 	/*
47691ae08745Sheppo 	 * Process the message
47701ae08745Sheppo 	 */
47713af08d82Slm66018 	switch (status = vd_do_process_msg(vd, msg, msglen)) {
47721ae08745Sheppo 	case 0:
47731ae08745Sheppo 		/* "ack" valid, successfully-processed messages */
47741ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
47751ae08745Sheppo 		break;
47761ae08745Sheppo 
4777d10e4ef2Snarayan 	case EINPROGRESS:
4778d10e4ef2Snarayan 		/* The completion handler will "ack" or "nack" the message */
4779d10e4ef2Snarayan 		return (EINPROGRESS);
47801ae08745Sheppo 	case ENOMSG:
47813af08d82Slm66018 		PR0("Received unexpected message");
47821ae08745Sheppo 		_NOTE(FALLTHROUGH);
47831ae08745Sheppo 	case EBADMSG:
47841ae08745Sheppo 	case ENOTSUP:
4785205eeb1aSlm66018 		/* "transport" error will cause NACK of invalid messages */
47861ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
47871ae08745Sheppo 		break;
47881ae08745Sheppo 
47891ae08745Sheppo 	default:
4790205eeb1aSlm66018 		/* "transport" error will cause NACK of invalid messages */
47911ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
47921ae08745Sheppo 		/* An LDC error probably occurred, so try resetting it */
47931ae08745Sheppo 		reset_ldc = B_TRUE;
47941ae08745Sheppo 		break;
47951ae08745Sheppo 	}
47961ae08745Sheppo 
47973af08d82Slm66018 	PR1("\tResulting in state %d (%s)", vd->state,
47983af08d82Slm66018 	    vd_decode_state(vd->state));
47993af08d82Slm66018 
4800205eeb1aSlm66018 	/* populate the task so we can dispatch it on the taskq */
4801205eeb1aSlm66018 	task.vd = vd;
4802205eeb1aSlm66018 	task.msg = msg;
4803205eeb1aSlm66018 	task.msglen = msglen;
4804205eeb1aSlm66018 
4805205eeb1aSlm66018 	/*
4806205eeb1aSlm66018 	 * Queue a task to send the notification that the operation completed.
4807205eeb1aSlm66018 	 * We need to ensure that requests are responded to in the correct
4808205eeb1aSlm66018 	 * order and since the taskq is processed serially this ordering
4809205eeb1aSlm66018 	 * is maintained.
4810205eeb1aSlm66018 	 */
4811205eeb1aSlm66018 	(void) ddi_taskq_dispatch(vd->completionq, vd_serial_notify,
4812205eeb1aSlm66018 	    &task, DDI_SLEEP);
4813205eeb1aSlm66018 
4814205eeb1aSlm66018 	/*
4815205eeb1aSlm66018 	 * To ensure handshake negotiations do not happen out of order, such
4816205eeb1aSlm66018 	 * requests that come through this path should not be done in parallel
4817205eeb1aSlm66018 	 * so we need to wait here until the response is sent to the client.
4818205eeb1aSlm66018 	 */
4819205eeb1aSlm66018 	ddi_taskq_wait(vd->completionq);
48201ae08745Sheppo 
4821d10e4ef2Snarayan 	/* Arrange to reset the connection for nack'ed or failed messages */
48223af08d82Slm66018 	if ((status != 0) || reset_ldc) {
48233af08d82Slm66018 		PR0("initiating %s reset",
48243af08d82Slm66018 		    (reset_ldc) ? "full" : "soft");
4825d10e4ef2Snarayan 		vd_need_reset(vd, reset_ldc);
48263af08d82Slm66018 	}
4827d10e4ef2Snarayan 
4828d10e4ef2Snarayan 	return (status);
4829d10e4ef2Snarayan }
4830d10e4ef2Snarayan 
4831d10e4ef2Snarayan static boolean_t
4832d10e4ef2Snarayan vd_enabled(vd_t *vd)
4833d10e4ef2Snarayan {
4834d10e4ef2Snarayan 	boolean_t	enabled;
4835d10e4ef2Snarayan 
4836d10e4ef2Snarayan 	mutex_enter(&vd->lock);
4837d10e4ef2Snarayan 	enabled = vd->enabled;
4838d10e4ef2Snarayan 	mutex_exit(&vd->lock);
4839d10e4ef2Snarayan 	return (enabled);
48401ae08745Sheppo }
48411ae08745Sheppo 
48421ae08745Sheppo static void
48430a55fbb7Slm66018 vd_recv_msg(void *arg)
48441ae08745Sheppo {
48451ae08745Sheppo 	vd_t	*vd = (vd_t *)arg;
48463af08d82Slm66018 	int	rv = 0, status = 0;
48471ae08745Sheppo 
48481ae08745Sheppo 	ASSERT(vd != NULL);
48493af08d82Slm66018 
4850d10e4ef2Snarayan 	PR2("New task to receive incoming message(s)");
48513af08d82Slm66018 
48523af08d82Slm66018 
4853d10e4ef2Snarayan 	while (vd_enabled(vd) && status == 0) {
4854d10e4ef2Snarayan 		size_t		msglen, msgsize;
48553af08d82Slm66018 		ldc_status_t	lstatus;
4856d10e4ef2Snarayan 
48570a55fbb7Slm66018 		/*
4858d10e4ef2Snarayan 		 * Receive and process a message
48590a55fbb7Slm66018 		 */
4860d10e4ef2Snarayan 		vd_reset_if_needed(vd);	/* can change vd->max_msglen */
48613af08d82Slm66018 
48623af08d82Slm66018 		/*
48633af08d82Slm66018 		 * check if channel is UP - else break out of loop
48643af08d82Slm66018 		 */
48653af08d82Slm66018 		status = ldc_status(vd->ldc_handle, &lstatus);
48663af08d82Slm66018 		if (lstatus != LDC_UP) {
48673af08d82Slm66018 			PR0("channel not up (status=%d), exiting recv loop\n",
48683af08d82Slm66018 			    lstatus);
48693af08d82Slm66018 			break;
48703af08d82Slm66018 		}
48713af08d82Slm66018 
48723af08d82Slm66018 		ASSERT(vd->max_msglen != 0);
48733af08d82Slm66018 
4874d10e4ef2Snarayan 		msgsize = vd->max_msglen; /* stable copy for alloc/free */
48753af08d82Slm66018 		msglen	= msgsize;	  /* actual len after recv_msg() */
48763af08d82Slm66018 
48773af08d82Slm66018 		status = recv_msg(vd->ldc_handle, vd->vio_msgp, &msglen);
48783af08d82Slm66018 		switch (status) {
48793af08d82Slm66018 		case 0:
48803af08d82Slm66018 			rv = vd_process_msg(vd, (vio_msg_t *)vd->vio_msgp,
48813af08d82Slm66018 			    msglen);
48823af08d82Slm66018 			/* check if max_msglen changed */
48833af08d82Slm66018 			if (msgsize != vd->max_msglen) {
48843af08d82Slm66018 				PR0("max_msglen changed 0x%lx to 0x%lx bytes\n",
48853af08d82Slm66018 				    msgsize, vd->max_msglen);
48863af08d82Slm66018 				kmem_free(vd->vio_msgp, msgsize);
48873af08d82Slm66018 				vd->vio_msgp =
48883af08d82Slm66018 				    kmem_alloc(vd->max_msglen, KM_SLEEP);
48893af08d82Slm66018 			}
48903af08d82Slm66018 			if (rv == EINPROGRESS)
48913af08d82Slm66018 				continue;
48923af08d82Slm66018 			break;
48933af08d82Slm66018 
48943af08d82Slm66018 		case ENOMSG:
48953af08d82Slm66018 			break;
48963af08d82Slm66018 
48973af08d82Slm66018 		case ECONNRESET:
48983af08d82Slm66018 			PR0("initiating soft reset (ECONNRESET)\n");
48993af08d82Slm66018 			vd_need_reset(vd, B_FALSE);
49003af08d82Slm66018 			status = 0;
49013af08d82Slm66018 			break;
49023af08d82Slm66018 
49033af08d82Slm66018 		default:
4904d10e4ef2Snarayan 			/* Probably an LDC failure; arrange to reset it */
49053af08d82Slm66018 			PR0("initiating full reset (status=0x%x)", status);
4906d10e4ef2Snarayan 			vd_need_reset(vd, B_TRUE);
49073af08d82Slm66018 			break;
49080a55fbb7Slm66018 		}
49091ae08745Sheppo 	}
49103af08d82Slm66018 
4911d10e4ef2Snarayan 	PR2("Task finished");
49120a55fbb7Slm66018 }
49130a55fbb7Slm66018 
49140a55fbb7Slm66018 static uint_t
49151ae08745Sheppo vd_handle_ldc_events(uint64_t event, caddr_t arg)
49161ae08745Sheppo {
49171ae08745Sheppo 	vd_t	*vd = (vd_t *)(void *)arg;
49183af08d82Slm66018 	int	status;
49191ae08745Sheppo 
49201ae08745Sheppo 	ASSERT(vd != NULL);
4921d10e4ef2Snarayan 
4922d10e4ef2Snarayan 	if (!vd_enabled(vd))
4923d10e4ef2Snarayan 		return (LDC_SUCCESS);
4924d10e4ef2Snarayan 
49253af08d82Slm66018 	if (event & LDC_EVT_DOWN) {
492634683adeSsg70180 		PR0("LDC_EVT_DOWN: LDC channel went down");
49273af08d82Slm66018 
49283af08d82Slm66018 		vd_need_reset(vd, B_TRUE);
49293af08d82Slm66018 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
49303af08d82Slm66018 		    DDI_SLEEP);
49313af08d82Slm66018 		if (status == DDI_FAILURE) {
49323af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
49333af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
49343af08d82Slm66018 		}
49353af08d82Slm66018 	}
49363af08d82Slm66018 
4937d10e4ef2Snarayan 	if (event & LDC_EVT_RESET) {
49383af08d82Slm66018 		PR0("LDC_EVT_RESET: LDC channel was reset");
49393af08d82Slm66018 
49403af08d82Slm66018 		if (vd->state != VD_STATE_INIT) {
49413af08d82Slm66018 			PR0("scheduling full reset");
49423af08d82Slm66018 			vd_need_reset(vd, B_FALSE);
49433af08d82Slm66018 			status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
49443af08d82Slm66018 			    vd, DDI_SLEEP);
49453af08d82Slm66018 			if (status == DDI_FAILURE) {
49463af08d82Slm66018 				PR0("cannot schedule task to recv msg\n");
49473af08d82Slm66018 				vd_need_reset(vd, B_TRUE);
49483af08d82Slm66018 			}
49493af08d82Slm66018 
49503af08d82Slm66018 		} else {
49513af08d82Slm66018 			PR0("channel already reset, ignoring...\n");
49523af08d82Slm66018 			PR0("doing ldc up...\n");
49533af08d82Slm66018 			(void) ldc_up(vd->ldc_handle);
49543af08d82Slm66018 		}
49553af08d82Slm66018 
4956d10e4ef2Snarayan 		return (LDC_SUCCESS);
4957d10e4ef2Snarayan 	}
4958d10e4ef2Snarayan 
4959d10e4ef2Snarayan 	if (event & LDC_EVT_UP) {
49603af08d82Slm66018 		PR0("EVT_UP: LDC is up\nResetting client connection state");
49613af08d82Slm66018 		PR0("initiating soft reset");
4962d10e4ef2Snarayan 		vd_need_reset(vd, B_FALSE);
49633af08d82Slm66018 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
49643af08d82Slm66018 		    vd, DDI_SLEEP);
49653af08d82Slm66018 		if (status == DDI_FAILURE) {
49663af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
49673af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
49683af08d82Slm66018 			return (LDC_SUCCESS);
49693af08d82Slm66018 		}
4970d10e4ef2Snarayan 	}
4971d10e4ef2Snarayan 
4972d10e4ef2Snarayan 	if (event & LDC_EVT_READ) {
4973d10e4ef2Snarayan 		int	status;
4974d10e4ef2Snarayan 
4975d10e4ef2Snarayan 		PR1("New data available");
4976d10e4ef2Snarayan 		/* Queue a task to receive the new data */
4977d10e4ef2Snarayan 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
4978d10e4ef2Snarayan 		    DDI_SLEEP);
49793af08d82Slm66018 
49803af08d82Slm66018 		if (status == DDI_FAILURE) {
49813af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
49823af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
49833af08d82Slm66018 		}
4984d10e4ef2Snarayan 	}
4985d10e4ef2Snarayan 
4986d10e4ef2Snarayan 	return (LDC_SUCCESS);
49871ae08745Sheppo }
49881ae08745Sheppo 
49891ae08745Sheppo static uint_t
49901ae08745Sheppo vds_check_for_vd(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
49911ae08745Sheppo {
49921ae08745Sheppo 	_NOTE(ARGUNUSED(key, val))
49931ae08745Sheppo 	(*((uint_t *)arg))++;
49941ae08745Sheppo 	return (MH_WALK_TERMINATE);
49951ae08745Sheppo }
49961ae08745Sheppo 
49971ae08745Sheppo 
49981ae08745Sheppo static int
49991ae08745Sheppo vds_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
50001ae08745Sheppo {
50011ae08745Sheppo 	uint_t	vd_present = 0;
50021ae08745Sheppo 	minor_t	instance;
50031ae08745Sheppo 	vds_t	*vds;
50041ae08745Sheppo 
50051ae08745Sheppo 
50061ae08745Sheppo 	switch (cmd) {
50071ae08745Sheppo 	case DDI_DETACH:
50081ae08745Sheppo 		/* the real work happens below */
50091ae08745Sheppo 		break;
50101ae08745Sheppo 	case DDI_SUSPEND:
5011d10e4ef2Snarayan 		PR0("No action required for DDI_SUSPEND");
50121ae08745Sheppo 		return (DDI_SUCCESS);
50131ae08745Sheppo 	default:
50143af08d82Slm66018 		PR0("Unrecognized \"cmd\"");
50151ae08745Sheppo 		return (DDI_FAILURE);
50161ae08745Sheppo 	}
50171ae08745Sheppo 
50181ae08745Sheppo 	ASSERT(cmd == DDI_DETACH);
50191ae08745Sheppo 	instance = ddi_get_instance(dip);
50201ae08745Sheppo 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
50213af08d82Slm66018 		PR0("Could not get state for instance %u", instance);
50221ae08745Sheppo 		ddi_soft_state_free(vds_state, instance);
50231ae08745Sheppo 		return (DDI_FAILURE);
50241ae08745Sheppo 	}
50251ae08745Sheppo 
50261ae08745Sheppo 	/* Do no detach when serving any vdisks */
50271ae08745Sheppo 	mod_hash_walk(vds->vd_table, vds_check_for_vd, &vd_present);
50281ae08745Sheppo 	if (vd_present) {
50291ae08745Sheppo 		PR0("Not detaching because serving vdisks");
50301ae08745Sheppo 		return (DDI_FAILURE);
50311ae08745Sheppo 	}
50321ae08745Sheppo 
50331ae08745Sheppo 	PR0("Detaching");
5034445b4c2eSsb155480 	if (vds->initialized & VDS_MDEG) {
50351ae08745Sheppo 		(void) mdeg_unregister(vds->mdeg);
5036445b4c2eSsb155480 		kmem_free(vds->ispecp->specp, sizeof (vds_prop_template));
5037445b4c2eSsb155480 		kmem_free(vds->ispecp, sizeof (mdeg_node_spec_t));
5038445b4c2eSsb155480 		vds->ispecp = NULL;
5039445b4c2eSsb155480 		vds->mdeg = NULL;
5040445b4c2eSsb155480 	}
5041445b4c2eSsb155480 
50428fce2fd6Sachartre 	vds_driver_types_free(vds);
50438fce2fd6Sachartre 
50441ae08745Sheppo 	if (vds->initialized & VDS_LDI)
50451ae08745Sheppo 		(void) ldi_ident_release(vds->ldi_ident);
50461ae08745Sheppo 	mod_hash_destroy_hash(vds->vd_table);
50471ae08745Sheppo 	ddi_soft_state_free(vds_state, instance);
50481ae08745Sheppo 	return (DDI_SUCCESS);
50491ae08745Sheppo }
50501ae08745Sheppo 
505117cadca8Slm66018 /*
505217cadca8Slm66018  * Description:
505317cadca8Slm66018  *	This function checks to see if the file being used as a
505417cadca8Slm66018  *	virtual disk is an ISO image. An ISO image is a special
505517cadca8Slm66018  *	case which can be booted/installed from like a CD/DVD
505617cadca8Slm66018  *
505717cadca8Slm66018  * Parameters:
505817cadca8Slm66018  *	vd		- disk on which the operation is performed.
505917cadca8Slm66018  *
506017cadca8Slm66018  * Return Code:
506117cadca8Slm66018  *	B_TRUE		- The file is an ISO 9660 compliant image
506217cadca8Slm66018  *	B_FALSE		- just a regular disk image file
506317cadca8Slm66018  */
506417cadca8Slm66018 static boolean_t
506517cadca8Slm66018 vd_file_is_iso_image(vd_t *vd)
506617cadca8Slm66018 {
506717cadca8Slm66018 	char	iso_buf[ISO_SECTOR_SIZE];
506817cadca8Slm66018 	int	i, rv;
506917cadca8Slm66018 	uint_t	sec;
507017cadca8Slm66018 
507117cadca8Slm66018 	ASSERT(vd->file);
507217cadca8Slm66018 
507317cadca8Slm66018 	/*
507417cadca8Slm66018 	 * If we have already discovered and saved this info we can
507517cadca8Slm66018 	 * short-circuit the check and avoid reading the file.
507617cadca8Slm66018 	 */
507717cadca8Slm66018 	if (vd->vdisk_media == VD_MEDIA_DVD || vd->vdisk_media == VD_MEDIA_CD)
507817cadca8Slm66018 		return (B_TRUE);
507917cadca8Slm66018 
508017cadca8Slm66018 	/*
508117cadca8Slm66018 	 * We wish to read the sector that should contain the 2nd ISO volume
508217cadca8Slm66018 	 * descriptor. The second field in this descriptor is called the
508317cadca8Slm66018 	 * Standard Identifier and is set to CD001 for a CD-ROM compliant
508417cadca8Slm66018 	 * to the ISO 9660 standard.
508517cadca8Slm66018 	 */
508617cadca8Slm66018 	sec = (ISO_VOLDESC_SEC * ISO_SECTOR_SIZE) / vd->vdisk_block_size;
508717cadca8Slm66018 	rv = vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)iso_buf,
508817cadca8Slm66018 	    sec, ISO_SECTOR_SIZE);
508917cadca8Slm66018 
509017cadca8Slm66018 	if (rv < 0)
509117cadca8Slm66018 		return (B_FALSE);
509217cadca8Slm66018 
509317cadca8Slm66018 	for (i = 0; i < ISO_ID_STRLEN; i++) {
509417cadca8Slm66018 		if (ISO_STD_ID(iso_buf)[i] != ISO_ID_STRING[i])
509517cadca8Slm66018 			return (B_FALSE);
509617cadca8Slm66018 	}
509717cadca8Slm66018 
509817cadca8Slm66018 	return (B_TRUE);
509917cadca8Slm66018 }
510017cadca8Slm66018 
510117cadca8Slm66018 /*
510217cadca8Slm66018  * Description:
510317cadca8Slm66018  *	This function checks to see if the virtual device is an ATAPI
510417cadca8Slm66018  *	device. ATAPI devices use Group 1 Read/Write commands, so
510517cadca8Slm66018  *	any USCSI calls vds makes need to take this into account.
510617cadca8Slm66018  *
510717cadca8Slm66018  * Parameters:
510817cadca8Slm66018  *	vd		- disk on which the operation is performed.
510917cadca8Slm66018  *
511017cadca8Slm66018  * Return Code:
511117cadca8Slm66018  *	B_TRUE		- The virtual disk is backed by an ATAPI device
511217cadca8Slm66018  *	B_FALSE		- not an ATAPI device (presumably SCSI)
511317cadca8Slm66018  */
511417cadca8Slm66018 static boolean_t
511517cadca8Slm66018 vd_is_atapi_device(vd_t *vd)
511617cadca8Slm66018 {
511717cadca8Slm66018 	boolean_t	is_atapi = B_FALSE;
511817cadca8Slm66018 	char		*variantp;
511917cadca8Slm66018 	int		rv;
512017cadca8Slm66018 
512117cadca8Slm66018 	ASSERT(vd->ldi_handle[0] != NULL);
512217cadca8Slm66018 	ASSERT(!vd->file);
512317cadca8Slm66018 
512417cadca8Slm66018 	rv = ldi_prop_lookup_string(vd->ldi_handle[0],
512517cadca8Slm66018 	    (LDI_DEV_T_ANY | DDI_PROP_DONTPASS), "variant", &variantp);
512617cadca8Slm66018 	if (rv == DDI_PROP_SUCCESS) {
512717cadca8Slm66018 		PR0("'variant' property exists for %s", vd->device_path);
512817cadca8Slm66018 		if (strcmp(variantp, "atapi") == 0)
512917cadca8Slm66018 			is_atapi = B_TRUE;
513017cadca8Slm66018 		ddi_prop_free(variantp);
513117cadca8Slm66018 	}
513217cadca8Slm66018 
513317cadca8Slm66018 	rv = ldi_prop_exists(vd->ldi_handle[0], LDI_DEV_T_ANY, "atapi");
513417cadca8Slm66018 	if (rv) {
513517cadca8Slm66018 		PR0("'atapi' property exists for %s", vd->device_path);
513617cadca8Slm66018 		is_atapi = B_TRUE;
513717cadca8Slm66018 	}
513817cadca8Slm66018 
513917cadca8Slm66018 	return (is_atapi);
514017cadca8Slm66018 }
514117cadca8Slm66018 
51421ae08745Sheppo static int
51432f5224aeSachartre vd_setup_mediainfo(vd_t *vd)
51440a55fbb7Slm66018 {
51452f5224aeSachartre 	int status, rval;
51464bac2208Snarayan 	struct dk_minfo	dk_minfo;
51470a55fbb7Slm66018 
51482f5224aeSachartre 	ASSERT(vd->ldi_handle[0] != NULL);
51492f5224aeSachartre 	ASSERT(vd->vdisk_block_size != 0);
51502f5224aeSachartre 
51512f5224aeSachartre 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCGMEDIAINFO,
51522f5224aeSachartre 	    (intptr_t)&dk_minfo, (vd->open_flags | FKIOCTL),
51532f5224aeSachartre 	    kcred, &rval)) != 0)
51542f5224aeSachartre 		return (status);
51552f5224aeSachartre 
51562f5224aeSachartre 	ASSERT(dk_minfo.dki_lbsize % vd->vdisk_block_size == 0);
51572f5224aeSachartre 
51582f5224aeSachartre 	vd->block_size = dk_minfo.dki_lbsize;
51592f5224aeSachartre 	vd->vdisk_size = (dk_minfo.dki_capacity * dk_minfo.dki_lbsize) /
51602f5224aeSachartre 	    vd->vdisk_block_size;
51612f5224aeSachartre 	vd->vdisk_media = DK_MEDIATYPE2VD_MEDIATYPE(dk_minfo.dki_media_type);
51622f5224aeSachartre 	return (0);
51632f5224aeSachartre }
51642f5224aeSachartre 
51652f5224aeSachartre static int
51662f5224aeSachartre vd_setup_full_disk(vd_t *vd)
51672f5224aeSachartre {
51682f5224aeSachartre 	int		status;
51692f5224aeSachartre 	major_t		major = getmajor(vd->dev[0]);
51702f5224aeSachartre 	minor_t		minor = getminor(vd->dev[0]) - VD_ENTIRE_DISK_SLICE;
51712f5224aeSachartre 
5172047ba61eSachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
5173047ba61eSachartre 
51742f5224aeSachartre 	vd->vdisk_block_size = DEV_BSIZE;
51752f5224aeSachartre 
51764bac2208Snarayan 	/*
51774bac2208Snarayan 	 * At this point, vdisk_size is set to the size of partition 2 but
51784bac2208Snarayan 	 * this does not represent the size of the disk because partition 2
51794bac2208Snarayan 	 * may not cover the entire disk and its size does not include reserved
51802f5224aeSachartre 	 * blocks. So we call vd_get_mediainfo to udpate this information and
51812f5224aeSachartre 	 * set the block size and the media type of the disk.
51824bac2208Snarayan 	 */
51832f5224aeSachartre 	status = vd_setup_mediainfo(vd);
51842f5224aeSachartre 
51852f5224aeSachartre 	if (status != 0) {
51862f5224aeSachartre 		if (!vd->scsi) {
51872f5224aeSachartre 			/* unexpected failure */
5188690555a1Sachartre 			PRN("ldi_ioctl(DKIOCGMEDIAINFO) returned errno %d",
51894bac2208Snarayan 			    status);
51900a55fbb7Slm66018 			return (status);
51910a55fbb7Slm66018 		}
51922f5224aeSachartre 
51932f5224aeSachartre 		/*
51942f5224aeSachartre 		 * The function can fail for SCSI disks which are present but
51952f5224aeSachartre 		 * reserved by another system. In that case, we don't know the
51962f5224aeSachartre 		 * size of the disk and the block size.
51972f5224aeSachartre 		 */
51982f5224aeSachartre 		vd->vdisk_size = VD_SIZE_UNKNOWN;
51992f5224aeSachartre 		vd->block_size = 0;
52002f5224aeSachartre 		vd->vdisk_media = VD_MEDIA_FIXED;
52012f5224aeSachartre 	}
52020a55fbb7Slm66018 
52030a55fbb7Slm66018 	/* Move dev number and LDI handle to entire-disk-slice array elements */
52040a55fbb7Slm66018 	vd->dev[VD_ENTIRE_DISK_SLICE]		= vd->dev[0];
52050a55fbb7Slm66018 	vd->dev[0]				= 0;
52060a55fbb7Slm66018 	vd->ldi_handle[VD_ENTIRE_DISK_SLICE]	= vd->ldi_handle[0];
52070a55fbb7Slm66018 	vd->ldi_handle[0]			= NULL;
52080a55fbb7Slm66018 
52090a55fbb7Slm66018 	/* Initialize device numbers for remaining slices and open them */
52100a55fbb7Slm66018 	for (int slice = 0; slice < vd->nslices; slice++) {
52110a55fbb7Slm66018 		/*
52120a55fbb7Slm66018 		 * Skip the entire-disk slice, as it's already open and its
52130a55fbb7Slm66018 		 * device known
52140a55fbb7Slm66018 		 */
52150a55fbb7Slm66018 		if (slice == VD_ENTIRE_DISK_SLICE)
52160a55fbb7Slm66018 			continue;
52170a55fbb7Slm66018 		ASSERT(vd->dev[slice] == 0);
52180a55fbb7Slm66018 		ASSERT(vd->ldi_handle[slice] == NULL);
52190a55fbb7Slm66018 
52200a55fbb7Slm66018 		/*
52210a55fbb7Slm66018 		 * Construct the device number for the current slice
52220a55fbb7Slm66018 		 */
52230a55fbb7Slm66018 		vd->dev[slice] = makedevice(major, (minor + slice));
52240a55fbb7Slm66018 
52250a55fbb7Slm66018 		/*
522634683adeSsg70180 		 * Open all slices of the disk to serve them to the client.
522734683adeSsg70180 		 * Slices are opened exclusively to prevent other threads or
522834683adeSsg70180 		 * processes in the service domain from performing I/O to
522934683adeSsg70180 		 * slices being accessed by a client.  Failure to open a slice
523034683adeSsg70180 		 * results in vds not serving this disk, as the client could
523134683adeSsg70180 		 * attempt (and should be able) to access any slice immediately.
523234683adeSsg70180 		 * Any slices successfully opened before a failure will get
523334683adeSsg70180 		 * closed by vds_destroy_vd() as a result of the error returned
523434683adeSsg70180 		 * by this function.
523534683adeSsg70180 		 *
523634683adeSsg70180 		 * We need to do the open with FNDELAY so that opening an empty
523734683adeSsg70180 		 * slice does not fail.
52380a55fbb7Slm66018 		 */
52390a55fbb7Slm66018 		PR0("Opening device major %u, minor %u = slice %u",
52400a55fbb7Slm66018 		    major, minor, slice);
5241047ba61eSachartre 
5242047ba61eSachartre 		/*
5243047ba61eSachartre 		 * Try to open the device. This can fail for example if we are
5244047ba61eSachartre 		 * opening an empty slice. So in case of a failure, we try the
5245047ba61eSachartre 		 * open again but this time with the FNDELAY flag.
5246047ba61eSachartre 		 */
5247047ba61eSachartre 		status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK,
5248047ba61eSachartre 		    vd->open_flags, kcred, &vd->ldi_handle[slice],
5249047ba61eSachartre 		    vd->vds->ldi_ident);
5250047ba61eSachartre 
5251047ba61eSachartre 		if (status != 0) {
5252047ba61eSachartre 			status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK,
5253047ba61eSachartre 			    vd->open_flags | FNDELAY, kcred,
5254047ba61eSachartre 			    &vd->ldi_handle[slice], vd->vds->ldi_ident);
5255047ba61eSachartre 		}
5256047ba61eSachartre 
5257047ba61eSachartre 		if (status != 0) {
5258690555a1Sachartre 			PRN("ldi_open_by_dev() returned errno %d "
52590a55fbb7Slm66018 			    "for slice %u", status, slice);
52600a55fbb7Slm66018 			/* vds_destroy_vd() will close any open slices */
5261690555a1Sachartre 			vd->ldi_handle[slice] = NULL;
52620a55fbb7Slm66018 			return (status);
52630a55fbb7Slm66018 		}
52640a55fbb7Slm66018 	}
52650a55fbb7Slm66018 
52660a55fbb7Slm66018 	return (0);
52670a55fbb7Slm66018 }
52680a55fbb7Slm66018 
5269edcc0754Sachartre /*
5270edcc0754Sachartre  * When a slice or a volume is exported as a single-slice disk, we want
5271edcc0754Sachartre  * the disk backend (i.e. the slice or volume) to be entirely mapped as
5272edcc0754Sachartre  * a slice without the addition of any metadata.
5273edcc0754Sachartre  *
5274edcc0754Sachartre  * So when exporting the disk as a VTOC disk, we fake a disk with the following
5275edcc0754Sachartre  * layout:
5276bae9e67eSachartre  *                flabel +--- flabel_limit
5277bae9e67eSachartre  *                 <->   V
5278bae9e67eSachartre  *                 0 1   C                          D  E
5279bae9e67eSachartre  *                 +-+---+--------------------------+--+
5280bae9e67eSachartre  *  virtual disk:  |L|XXX|           slice 0        |AA|
5281bae9e67eSachartre  *                 +-+---+--------------------------+--+
5282edcc0754Sachartre  *                  ^    :                          :
5283edcc0754Sachartre  *                  |    :                          :
5284edcc0754Sachartre  *      VTOC LABEL--+    :                          :
5285edcc0754Sachartre  *                       +--------------------------+
5286bae9e67eSachartre  *  disk backend:        |     slice/volume/file    |
5287edcc0754Sachartre  *                       +--------------------------+
5288edcc0754Sachartre  *                       0                          N
5289edcc0754Sachartre  *
5290bae9e67eSachartre  * N is the number of blocks in the slice/volume/file.
5291edcc0754Sachartre  *
5292bae9e67eSachartre  * We simulate a disk with N+M blocks, where M is the number of blocks
5293bae9e67eSachartre  * simluated at the beginning and at the end of the disk (blocks 0-C
5294bae9e67eSachartre  * and D-E).
5295edcc0754Sachartre  *
5296bae9e67eSachartre  * The first blocks (0 to C-1) are emulated and can not be changed. Blocks C
5297bae9e67eSachartre  * to D defines slice 0 and are mapped to the backend. Finally we emulate 2
5298bae9e67eSachartre  * alternate cylinders at the end of the disk (blocks D-E). In summary we have:
5299edcc0754Sachartre  *
5300bae9e67eSachartre  * - block 0 (L) returns a fake VTOC label
5301bae9e67eSachartre  * - blocks 1 to C-1 (X) are unused and return 0
5302bae9e67eSachartre  * - blocks C to D-1 are mapped to the exported slice or volume
5303bae9e67eSachartre  * - blocks D and E (A) are blocks defining alternate cylinders (2 cylinders)
5304bae9e67eSachartre  *
5305bae9e67eSachartre  * Note: because we define a fake disk geometry, it is possible that the length
5306bae9e67eSachartre  * of the backend is not a multiple of the size of cylinder, in that case the
5307bae9e67eSachartre  * very end of the backend will not map to any block of the virtual disk.
5308edcc0754Sachartre  */
53090a55fbb7Slm66018 static int
531078fcd0a1Sachartre vd_setup_partition_vtoc(vd_t *vd)
531178fcd0a1Sachartre {
531278fcd0a1Sachartre 	char *device_path = vd->device_path;
5313bae9e67eSachartre 	char unit;
5314bae9e67eSachartre 	size_t size, csize;
531578fcd0a1Sachartre 
531678fcd0a1Sachartre 	/* Initialize dk_geom structure for single-slice device */
531778fcd0a1Sachartre 	if (vd->dk_geom.dkg_nsect == 0) {
531878fcd0a1Sachartre 		PRN("%s geometry claims 0 sectors per track", device_path);
531978fcd0a1Sachartre 		return (EIO);
532078fcd0a1Sachartre 	}
532178fcd0a1Sachartre 	if (vd->dk_geom.dkg_nhead == 0) {
532278fcd0a1Sachartre 		PRN("%s geometry claims 0 heads", device_path);
532378fcd0a1Sachartre 		return (EIO);
532478fcd0a1Sachartre 	}
5325bae9e67eSachartre 
5326bae9e67eSachartre 	/* size of a cylinder in block */
5327bae9e67eSachartre 	csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
5328bae9e67eSachartre 
5329bae9e67eSachartre 	/*
5330bae9e67eSachartre 	 * Add extra cylinders: we emulate the first cylinder (which contains
5331bae9e67eSachartre 	 * the disk label).
5332bae9e67eSachartre 	 */
5333bae9e67eSachartre 	vd->dk_geom.dkg_ncyl = vd->vdisk_size / csize + 1;
5334bae9e67eSachartre 
5335bae9e67eSachartre 	/* we emulate 2 alternate cylinders */
5336bae9e67eSachartre 	vd->dk_geom.dkg_acyl = 2;
533778fcd0a1Sachartre 	vd->dk_geom.dkg_pcyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl;
533878fcd0a1Sachartre 
533978fcd0a1Sachartre 
534078fcd0a1Sachartre 	/* Initialize vtoc structure for single-slice device */
534178fcd0a1Sachartre 	bzero(vd->vtoc.v_part, sizeof (vd->vtoc.v_part));
534278fcd0a1Sachartre 	vd->vtoc.v_part[0].p_tag = V_UNASSIGNED;
534378fcd0a1Sachartre 	vd->vtoc.v_part[0].p_flag = 0;
5344bae9e67eSachartre 	/*
5345bae9e67eSachartre 	 * Partition 0 starts on cylinder 1 and its size has to be
5346bae9e67eSachartre 	 * a multiple of a number of cylinder.
5347bae9e67eSachartre 	 */
5348bae9e67eSachartre 	vd->vtoc.v_part[0].p_start = csize; /* start on cylinder 1 */
5349bae9e67eSachartre 	vd->vtoc.v_part[0].p_size = (vd->vdisk_size / csize) * csize;
535078fcd0a1Sachartre 
5351bae9e67eSachartre 	if (vd_slice_single_slice) {
5352bae9e67eSachartre 		vd->vtoc.v_nparts = 1;
5353bae9e67eSachartre 		bcopy(VD_ASCIILABEL, vd->vtoc.v_asciilabel,
5354bae9e67eSachartre 		    MIN(sizeof (VD_ASCIILABEL),
5355bae9e67eSachartre 		    sizeof (vd->vtoc.v_asciilabel)));
5356bae9e67eSachartre 		bcopy(VD_VOLUME_NAME, vd->vtoc.v_volume,
5357bae9e67eSachartre 		    MIN(sizeof (VD_VOLUME_NAME), sizeof (vd->vtoc.v_volume)));
5358bae9e67eSachartre 	} else {
5359bae9e67eSachartre 		/* adjust the number of slices */
5360bae9e67eSachartre 		vd->nslices = V_NUMPAR;
5361bae9e67eSachartre 		vd->vtoc.v_nparts = V_NUMPAR;
5362bae9e67eSachartre 
5363bae9e67eSachartre 		/* define slice 2 representing the entire disk */
5364bae9e67eSachartre 		vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_tag = V_BACKUP;
5365bae9e67eSachartre 		vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_flag = 0;
5366bae9e67eSachartre 		vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_start = 0;
5367bae9e67eSachartre 		vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_size =
5368bae9e67eSachartre 		    vd->dk_geom.dkg_ncyl * csize;
5369bae9e67eSachartre 
5370bae9e67eSachartre 		vd_get_readable_size(vd->vdisk_size * vd->vdisk_block_size,
5371bae9e67eSachartre 		    &size, &unit);
5372bae9e67eSachartre 
5373bae9e67eSachartre 		/*
5374bae9e67eSachartre 		 * Set some attributes of the geometry to what format(1m) uses
5375bae9e67eSachartre 		 * so that writing a default label using format(1m) does not
5376bae9e67eSachartre 		 * produce any error.
5377bae9e67eSachartre 		 */
5378bae9e67eSachartre 		vd->dk_geom.dkg_bcyl = 0;
5379bae9e67eSachartre 		vd->dk_geom.dkg_intrlv = 1;
5380bae9e67eSachartre 		vd->dk_geom.dkg_write_reinstruct = 0;
5381bae9e67eSachartre 		vd->dk_geom.dkg_read_reinstruct = 0;
5382bae9e67eSachartre 
5383bae9e67eSachartre 		/*
5384bae9e67eSachartre 		 * We must have a correct label name otherwise format(1m) will
5385bae9e67eSachartre 		 * not recognized the disk as labeled.
5386bae9e67eSachartre 		 */
5387bae9e67eSachartre 		(void) snprintf(vd->vtoc.v_asciilabel, LEN_DKL_ASCII,
5388bae9e67eSachartre 		    "SUN-DiskSlice-%ld%cB cyl %d alt %d hd %d sec %d",
5389bae9e67eSachartre 		    size, unit,
5390bae9e67eSachartre 		    vd->dk_geom.dkg_ncyl, vd->dk_geom.dkg_acyl,
5391bae9e67eSachartre 		    vd->dk_geom.dkg_nhead, vd->dk_geom.dkg_nsect);
5392bae9e67eSachartre 		bzero(vd->vtoc.v_volume, sizeof (vd->vtoc.v_volume));
5393bae9e67eSachartre 
5394bae9e67eSachartre 		/* create a fake label from the vtoc and geometry */
5395bae9e67eSachartre 		vd->flabel_limit = csize;
5396bae9e67eSachartre 		vd->flabel_size = VD_LABEL_VTOC_SIZE;
5397bae9e67eSachartre 		vd->flabel = kmem_zalloc(vd->flabel_size, KM_SLEEP);
5398bae9e67eSachartre 		vd_vtocgeom_to_label(&vd->vtoc, &vd->dk_geom,
5399bae9e67eSachartre 		    VD_LABEL_VTOC(vd));
5400bae9e67eSachartre 	}
5401bae9e67eSachartre 
5402bae9e67eSachartre 	/* adjust the vdisk_size, we emulate 3 cylinders */
5403bae9e67eSachartre 	vd->vdisk_size += csize * 3;
5404edcc0754Sachartre 
540578fcd0a1Sachartre 	return (0);
540678fcd0a1Sachartre }
540778fcd0a1Sachartre 
5408edcc0754Sachartre /*
5409edcc0754Sachartre  * When a slice, volume or file is exported as a single-slice disk, we want
5410edcc0754Sachartre  * the disk backend (i.e. the slice, volume or file) to be entirely mapped
5411edcc0754Sachartre  * as a slice without the addition of any metadata.
5412edcc0754Sachartre  *
5413edcc0754Sachartre  * So when exporting the disk as an EFI disk, we fake a disk with the following
5414edcc0754Sachartre  * layout:
5415edcc0754Sachartre  *
5416bae9e67eSachartre  *                  flabel        +--- flabel_limit
5417bae9e67eSachartre  *                 <------>       v
5418bae9e67eSachartre  *                 0 1 2  L      34                        34+N      P
5419bae9e67eSachartre  *                 +-+-+--+-------+--------------------------+-------+
5420bae9e67eSachartre  *  virtual disk:  |X|T|EE|XXXXXXX|           slice 0        |RRRRRRR|
5421bae9e67eSachartre  *                 +-+-+--+-------+--------------------------+-------+
5422edcc0754Sachartre  *                    ^ ^         :                          :
5423edcc0754Sachartre  *                    | |         :                          :
5424edcc0754Sachartre  *                GPT-+ +-GPE     :                          :
5425edcc0754Sachartre  *                                +--------------------------+
5426edcc0754Sachartre  *  disk backend:                 |     slice/volume/file    |
5427edcc0754Sachartre  *                                +--------------------------+
5428edcc0754Sachartre  *                                0                          N
5429edcc0754Sachartre  *
5430edcc0754Sachartre  * N is the number of blocks in the slice/volume/file.
5431edcc0754Sachartre  *
5432bae9e67eSachartre  * We simulate a disk with N+M blocks, where M is the number of blocks
5433bae9e67eSachartre  * simluated at the beginning and at the end of the disk (blocks 0-34
5434bae9e67eSachartre  * and 34+N-P).
5435edcc0754Sachartre  *
5436bae9e67eSachartre  * The first 34 blocks (0 to 33) are emulated and can not be changed. Blocks 34
5437bae9e67eSachartre  * to 34+N defines slice 0 and are mapped to the exported backend, and we
5438bae9e67eSachartre  * emulate some blocks at the end of the disk (blocks 34+N to P) as a the EFI
5439bae9e67eSachartre  * reserved partition.
5440bae9e67eSachartre  *
5441bae9e67eSachartre  * - block 0 (X) is unused and return 0
5442edcc0754Sachartre  * - block 1 (T) returns a fake EFI GPT (via DKIOCGETEFI)
5443bae9e67eSachartre  * - blocks 2 to L-1 (E) defines a fake EFI GPE (via DKIOCGETEFI)
5444bae9e67eSachartre  * - blocks L to 33 (X) are unused and return 0
5445bae9e67eSachartre  * - blocks 34 to 34+N are mapped to the exported slice, volume or file
5446bae9e67eSachartre  * - blocks 34+N+1 to P define a fake reserved partition and backup label, it
5447bae9e67eSachartre  *   returns 0
5448edcc0754Sachartre  *
5449bae9e67eSachartre  * Note: if the backend size is not a multiple of the vdisk block size
5450bae9e67eSachartre  * (DEV_BSIZE = 512 byte) then the very end of the backend will not map to
5451bae9e67eSachartre  * any block of the virtual disk.
5452edcc0754Sachartre  */
545378fcd0a1Sachartre static int
54544bac2208Snarayan vd_setup_partition_efi(vd_t *vd)
54554bac2208Snarayan {
54564bac2208Snarayan 	efi_gpt_t *gpt;
54574bac2208Snarayan 	efi_gpe_t *gpe;
5458edcc0754Sachartre 	struct uuid uuid = EFI_USR;
5459bae9e67eSachartre 	struct uuid efi_reserved = EFI_RESERVED;
54604bac2208Snarayan 	uint32_t crc;
5461bae9e67eSachartre 	uint64_t s0_start, s0_end;
54624bac2208Snarayan 
5463bae9e67eSachartre 	vd->flabel_limit = 34;
5464bae9e67eSachartre 	vd->flabel_size = VD_LABEL_EFI_SIZE;
5465bae9e67eSachartre 	vd->flabel = kmem_zalloc(vd->flabel_size, KM_SLEEP);
5466bae9e67eSachartre 	gpt = VD_LABEL_EFI_GPT(vd);
5467bae9e67eSachartre 	gpe = VD_LABEL_EFI_GPE(vd);
5468edcc0754Sachartre 
5469edcc0754Sachartre 	/* adjust the vdisk_size, we emulate the first 34 blocks */
5470edcc0754Sachartre 	vd->vdisk_size += 34;
5471bae9e67eSachartre 	s0_start = 34;
5472bae9e67eSachartre 	s0_end = vd->vdisk_size - 1;
54734bac2208Snarayan 
54744bac2208Snarayan 	gpt->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
54754bac2208Snarayan 	gpt->efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
54764bac2208Snarayan 	gpt->efi_gpt_HeaderSize = LE_32(sizeof (efi_gpt_t));
5477edcc0754Sachartre 	gpt->efi_gpt_FirstUsableLBA = LE_64(34ULL);
5478edcc0754Sachartre 	gpt->efi_gpt_PartitionEntryLBA = LE_64(2ULL);
54794bac2208Snarayan 	gpt->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (efi_gpe_t));
54804bac2208Snarayan 
5481bae9e67eSachartre 	UUID_LE_CONVERT(gpe[0].efi_gpe_PartitionTypeGUID, uuid);
5482bae9e67eSachartre 	gpe[0].efi_gpe_StartingLBA = LE_64(s0_start);
5483bae9e67eSachartre 	gpe[0].efi_gpe_EndingLBA = LE_64(s0_end);
54844bac2208Snarayan 
5485bae9e67eSachartre 	if (vd_slice_single_slice) {
5486bae9e67eSachartre 		gpt->efi_gpt_NumberOfPartitionEntries = LE_32(1);
5487bae9e67eSachartre 	} else {
5488bae9e67eSachartre 		/* adjust the number of slices */
5489bae9e67eSachartre 		gpt->efi_gpt_NumberOfPartitionEntries = LE_32(VD_MAXPART);
5490bae9e67eSachartre 		vd->nslices = V_NUMPAR;
5491bae9e67eSachartre 
5492bae9e67eSachartre 		/* define a fake reserved partition */
5493bae9e67eSachartre 		UUID_LE_CONVERT(gpe[VD_MAXPART - 1].efi_gpe_PartitionTypeGUID,
5494bae9e67eSachartre 		    efi_reserved);
5495bae9e67eSachartre 		gpe[VD_MAXPART - 1].efi_gpe_StartingLBA =
5496bae9e67eSachartre 		    LE_64(s0_end + 1);
5497bae9e67eSachartre 		gpe[VD_MAXPART - 1].efi_gpe_EndingLBA =
5498bae9e67eSachartre 		    LE_64(s0_end + EFI_MIN_RESV_SIZE);
5499bae9e67eSachartre 
5500bae9e67eSachartre 		/* adjust the vdisk_size to include the reserved slice */
5501bae9e67eSachartre 		vd->vdisk_size += EFI_MIN_RESV_SIZE;
5502bae9e67eSachartre 	}
5503bae9e67eSachartre 
5504bae9e67eSachartre 	gpt->efi_gpt_LastUsableLBA = LE_64(vd->vdisk_size - 1);
5505bae9e67eSachartre 
5506bae9e67eSachartre 	/* adjust the vdisk size for the backup GPT and GPE */
5507bae9e67eSachartre 	vd->vdisk_size += 33;
5508bae9e67eSachartre 
5509bae9e67eSachartre 	CRC32(crc, gpe, sizeof (efi_gpe_t) * VD_MAXPART, -1U, crc32_table);
55104bac2208Snarayan 	gpt->efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
55114bac2208Snarayan 
55124bac2208Snarayan 	CRC32(crc, gpt, sizeof (efi_gpt_t), -1U, crc32_table);
55134bac2208Snarayan 	gpt->efi_gpt_HeaderCRC32 = LE_32(~crc);
55144bac2208Snarayan 
55154bac2208Snarayan 	return (0);
55164bac2208Snarayan }
55174bac2208Snarayan 
5518047ba61eSachartre /*
5519047ba61eSachartre  * Setup for a virtual disk whose backend is a file (exported as a single slice
55208fce2fd6Sachartre  * or as a full disk) or a volume device (for example a ZFS, SVM or VxVM volume)
5521047ba61eSachartre  * exported as a full disk. In these cases, the backend is accessed using the
5522047ba61eSachartre  * vnode interface.
5523047ba61eSachartre  */
55244bac2208Snarayan static int
5525047ba61eSachartre vd_setup_backend_vnode(vd_t *vd)
55263c96341aSnarayan {
552778fcd0a1Sachartre 	int 		rval, status;
55283c96341aSnarayan 	vattr_t		vattr;
55293c96341aSnarayan 	dev_t		dev;
55303c96341aSnarayan 	char		*file_path = vd->device_path;
55313c96341aSnarayan 	char		dev_path[MAXPATHLEN + 1];
55323c96341aSnarayan 	ldi_handle_t	lhandle;
55333c96341aSnarayan 	struct dk_cinfo	dk_cinfo;
5534bae9e67eSachartre 	struct dk_label label;
55353c96341aSnarayan 
5536047ba61eSachartre 	if ((status = vn_open(file_path, UIO_SYSSPACE, vd->open_flags | FOFFMAX,
55373c96341aSnarayan 	    0, &vd->file_vnode, 0, 0)) != 0) {
5538690555a1Sachartre 		PRN("vn_open(%s) = errno %d", file_path, status);
55393c96341aSnarayan 		return (status);
55403c96341aSnarayan 	}
55413c96341aSnarayan 
5542690555a1Sachartre 	/*
5543690555a1Sachartre 	 * We set vd->file now so that vds_destroy_vd will take care of
5544690555a1Sachartre 	 * closing the file and releasing the vnode in case of an error.
5545690555a1Sachartre 	 */
5546690555a1Sachartre 	vd->file = B_TRUE;
5547690555a1Sachartre 
55483c96341aSnarayan 	vattr.va_mask = AT_SIZE;
5549da6c28aaSamw 	if ((status = VOP_GETATTR(vd->file_vnode, &vattr, 0, kcred, NULL))
5550da6c28aaSamw 	    != 0) {
5551690555a1Sachartre 		PRN("VOP_GETATTR(%s) = errno %d", file_path, status);
55523c96341aSnarayan 		return (EIO);
55533c96341aSnarayan 	}
55543c96341aSnarayan 
55553c96341aSnarayan 	vd->file_size = vattr.va_size;
55563c96341aSnarayan 	/* size should be at least sizeof(dk_label) */
55573c96341aSnarayan 	if (vd->file_size < sizeof (struct dk_label)) {
55583c96341aSnarayan 		PRN("Size of file has to be at least %ld bytes",
55593c96341aSnarayan 		    sizeof (struct dk_label));
55603c96341aSnarayan 		return (EIO);
55613c96341aSnarayan 	}
55623c96341aSnarayan 
5563690555a1Sachartre 	if (vd->file_vnode->v_flag & VNOMAP) {
5564690555a1Sachartre 		PRN("File %s cannot be mapped", file_path);
55653c96341aSnarayan 		return (EIO);
55663c96341aSnarayan 	}
55673c96341aSnarayan 
55683c96341aSnarayan 	/* sector size = block size = DEV_BSIZE */
556917cadca8Slm66018 	vd->block_size = DEV_BSIZE;
557017cadca8Slm66018 	vd->vdisk_block_size = DEV_BSIZE;
557187a7269eSachartre 	vd->vdisk_size = vd->file_size / DEV_BSIZE;
55723c96341aSnarayan 	vd->max_xfer_sz = maxphys / DEV_BSIZE; /* default transfer size */
55733c96341aSnarayan 
5574047ba61eSachartre 	/*
5575047ba61eSachartre 	 * Get max_xfer_sz from the device where the file is or from the device
55768fce2fd6Sachartre 	 * itself if we have a volume device.
5577047ba61eSachartre 	 */
5578047ba61eSachartre 	dev_path[0] = '\0';
5579047ba61eSachartre 
55808fce2fd6Sachartre 	if (vd->volume) {
5581047ba61eSachartre 		status = ldi_open_by_name(file_path, FREAD, kcred, &lhandle,
5582047ba61eSachartre 		    vd->vds->ldi_ident);
5583047ba61eSachartre 	} else {
55843c96341aSnarayan 		dev = vd->file_vnode->v_vfsp->vfs_dev;
55853c96341aSnarayan 		if (ddi_dev_pathname(dev, S_IFBLK, dev_path) == DDI_SUCCESS) {
55863c96341aSnarayan 			PR0("underlying device = %s\n", dev_path);
55873c96341aSnarayan 		}
55883c96341aSnarayan 
5589047ba61eSachartre 		status = ldi_open_by_dev(&dev, OTYP_BLK, FREAD, kcred, &lhandle,
5590047ba61eSachartre 		    vd->vds->ldi_ident);
5591047ba61eSachartre 	}
5592047ba61eSachartre 
5593047ba61eSachartre 	if (status != 0) {
5594047ba61eSachartre 		PR0("ldi_open() returned errno %d for device %s",
5595047ba61eSachartre 		    status, (dev_path[0] == '\0')? file_path : dev_path);
55963c96341aSnarayan 	} else {
55973c96341aSnarayan 		if ((status = ldi_ioctl(lhandle, DKIOCINFO,
5598047ba61eSachartre 		    (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred,
55993c96341aSnarayan 		    &rval)) != 0) {
56003c96341aSnarayan 			PR0("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
56013c96341aSnarayan 			    status, dev_path);
56023c96341aSnarayan 		} else {
56033c96341aSnarayan 			/*
56043c96341aSnarayan 			 * Store the device's max transfer size for
56053c96341aSnarayan 			 * return to the client
56063c96341aSnarayan 			 */
56073c96341aSnarayan 			vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
56083c96341aSnarayan 		}
56093c96341aSnarayan 
56103c96341aSnarayan 		PR0("close the device %s", dev_path);
56113c96341aSnarayan 		(void) ldi_close(lhandle, FREAD, kcred);
56123c96341aSnarayan 	}
56133c96341aSnarayan 
5614205eeb1aSlm66018 	PR0("using file %s, dev %s, max_xfer = %u blks",
56153c96341aSnarayan 	    file_path, dev_path, vd->max_xfer_sz);
56163c96341aSnarayan 
5617edcc0754Sachartre 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
56188fce2fd6Sachartre 		ASSERT(!vd->volume);
5619bae9e67eSachartre 		vd->vdisk_media = VD_MEDIA_FIXED;
5620bae9e67eSachartre 		vd->vdisk_label = (vd_slice_label == VD_DISK_LABEL_UNK)?
5621bae9e67eSachartre 		    vd_file_slice_label : vd_slice_label;
5622bae9e67eSachartre 		if (vd->vdisk_label == VD_DISK_LABEL_EFI ||
5623bae9e67eSachartre 		    vd->file_size >= ONE_TERABYTE) {
5624edcc0754Sachartre 			status = vd_setup_partition_efi(vd);
5625bae9e67eSachartre 		} else {
5626bae9e67eSachartre 			/*
5627bae9e67eSachartre 			 * We build a default label to get a geometry for
5628bae9e67eSachartre 			 * the vdisk. Then the partition setup function will
5629bae9e67eSachartre 			 * adjust the vtoc so that it defines a single-slice
5630bae9e67eSachartre 			 * disk.
5631bae9e67eSachartre 			 */
5632bae9e67eSachartre 			vd_build_default_label(vd->file_size, &label);
5633bae9e67eSachartre 			vd_label_to_vtocgeom(&label, &vd->vtoc, &vd->dk_geom);
5634bae9e67eSachartre 			status = vd_setup_partition_vtoc(vd);
5635bae9e67eSachartre 		}
5636bae9e67eSachartre 		return (status);
5637edcc0754Sachartre 	}
5638edcc0754Sachartre 
5639edcc0754Sachartre 	/*
5640edcc0754Sachartre 	 * Find and validate the geometry of a disk image.
5641edcc0754Sachartre 	 */
5642edcc0754Sachartre 	status = vd_file_validate_geometry(vd);
5643edcc0754Sachartre 	if (status != 0 && status != EINVAL && status != ENOTSUP) {
5644edcc0754Sachartre 		PRN("Failed to read label from %s", file_path);
5645edcc0754Sachartre 		return (EIO);
5646edcc0754Sachartre 	}
5647edcc0754Sachartre 
5648edcc0754Sachartre 	if (vd_file_is_iso_image(vd)) {
5649edcc0754Sachartre 		/*
5650edcc0754Sachartre 		 * Indicate whether to call this a CD or DVD from the size
5651edcc0754Sachartre 		 * of the ISO image (images for both drive types are stored
5652edcc0754Sachartre 		 * in the ISO-9600 format). CDs can store up to just under 1Gb
5653edcc0754Sachartre 		 */
5654edcc0754Sachartre 		if ((vd->vdisk_size * vd->vdisk_block_size) >
5655edcc0754Sachartre 		    (1024 * 1024 * 1024))
5656edcc0754Sachartre 			vd->vdisk_media = VD_MEDIA_DVD;
5657edcc0754Sachartre 		else
5658edcc0754Sachartre 			vd->vdisk_media = VD_MEDIA_CD;
5659edcc0754Sachartre 	} else {
5660edcc0754Sachartre 		vd->vdisk_media = VD_MEDIA_FIXED;
5661edcc0754Sachartre 	}
5662edcc0754Sachartre 
5663edcc0754Sachartre 	/* Setup devid for the disk image */
5664047ba61eSachartre 
566578fcd0a1Sachartre 	if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
566678fcd0a1Sachartre 
566787a7269eSachartre 		status = vd_file_read_devid(vd, &vd->file_devid);
566887a7269eSachartre 
566987a7269eSachartre 		if (status == 0) {
567087a7269eSachartre 			/* a valid devid was found */
567187a7269eSachartre 			return (0);
567287a7269eSachartre 		}
567387a7269eSachartre 
567487a7269eSachartre 		if (status != EINVAL) {
567587a7269eSachartre 			/*
567678fcd0a1Sachartre 			 * There was an error while trying to read the devid.
567778fcd0a1Sachartre 			 * So this disk image may have a devid but we are
567878fcd0a1Sachartre 			 * unable to read it.
567987a7269eSachartre 			 */
568087a7269eSachartre 			PR0("can not read devid for %s", file_path);
568187a7269eSachartre 			vd->file_devid = NULL;
568287a7269eSachartre 			return (0);
568387a7269eSachartre 		}
568478fcd0a1Sachartre 	}
568587a7269eSachartre 
568687a7269eSachartre 	/*
568787a7269eSachartre 	 * No valid device id was found so we create one. Note that a failure
568887a7269eSachartre 	 * to create a device id is not fatal and does not prevent the disk
568987a7269eSachartre 	 * image from being attached.
569087a7269eSachartre 	 */
569187a7269eSachartre 	PR1("creating devid for %s", file_path);
569287a7269eSachartre 
569387a7269eSachartre 	if (ddi_devid_init(vd->vds->dip, DEVID_FAB, NULL, 0,
569487a7269eSachartre 	    &vd->file_devid) != DDI_SUCCESS) {
569587a7269eSachartre 		PR0("fail to create devid for %s", file_path);
569687a7269eSachartre 		vd->file_devid = NULL;
569787a7269eSachartre 		return (0);
569887a7269eSachartre 	}
569987a7269eSachartre 
570078fcd0a1Sachartre 	/*
570178fcd0a1Sachartre 	 * Write devid to the disk image. The devid is stored into the disk
570278fcd0a1Sachartre 	 * image if we have a valid label; otherwise the devid will be stored
570378fcd0a1Sachartre 	 * when the user writes a valid label.
570478fcd0a1Sachartre 	 */
570578fcd0a1Sachartre 	if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
570687a7269eSachartre 		if (vd_file_write_devid(vd, vd->file_devid) != 0) {
570787a7269eSachartre 			PR0("fail to write devid for %s", file_path);
570887a7269eSachartre 			ddi_devid_free(vd->file_devid);
570987a7269eSachartre 			vd->file_devid = NULL;
571087a7269eSachartre 		}
571178fcd0a1Sachartre 	}
571287a7269eSachartre 
57133c96341aSnarayan 	return (0);
57143c96341aSnarayan }
57153c96341aSnarayan 
571617cadca8Slm66018 
571717cadca8Slm66018 /*
571817cadca8Slm66018  * Description:
571917cadca8Slm66018  *	Open a device using its device path (supplied by ldm(1m))
572017cadca8Slm66018  *
572117cadca8Slm66018  * Parameters:
572217cadca8Slm66018  *	vd 	- pointer to structure containing the vDisk info
57238fce2fd6Sachartre  *	flags	- open flags
572417cadca8Slm66018  *
572517cadca8Slm66018  * Return Value
572617cadca8Slm66018  *	0	- success
572717cadca8Slm66018  *	!= 0	- some other non-zero return value from ldi(9F) functions
572817cadca8Slm66018  */
572917cadca8Slm66018 static int
57308fce2fd6Sachartre vd_open_using_ldi_by_name(vd_t *vd, int flags)
573117cadca8Slm66018 {
57328fce2fd6Sachartre 	int		status;
573317cadca8Slm66018 	char		*device_path = vd->device_path;
573417cadca8Slm66018 
57358fce2fd6Sachartre 	/* Attempt to open device */
57368fce2fd6Sachartre 	status = ldi_open_by_name(device_path, flags, kcred,
573717cadca8Slm66018 	    &vd->ldi_handle[0], vd->vds->ldi_ident);
573817cadca8Slm66018 
573917cadca8Slm66018 	/*
574017cadca8Slm66018 	 * The open can fail for example if we are opening an empty slice.
574117cadca8Slm66018 	 * In case of a failure, we try the open again but this time with
574217cadca8Slm66018 	 * the FNDELAY flag.
574317cadca8Slm66018 	 */
574417cadca8Slm66018 	if (status != 0)
57458fce2fd6Sachartre 		status = ldi_open_by_name(device_path, flags | FNDELAY,
574617cadca8Slm66018 		    kcred, &vd->ldi_handle[0], vd->vds->ldi_ident);
574717cadca8Slm66018 
574817cadca8Slm66018 	if (status != 0) {
574917cadca8Slm66018 		PR0("ldi_open_by_name(%s) = errno %d", device_path, status);
575017cadca8Slm66018 		vd->ldi_handle[0] = NULL;
575117cadca8Slm66018 		return (status);
575217cadca8Slm66018 	}
575317cadca8Slm66018 
575417cadca8Slm66018 	return (0);
575517cadca8Slm66018 }
575617cadca8Slm66018 
5757047ba61eSachartre /*
5758047ba61eSachartre  * Setup for a virtual disk which backend is a device (a physical disk,
57598fce2fd6Sachartre  * slice or volume device) that is directly exported either as a full disk
57608fce2fd6Sachartre  * for a physical disk or as a slice for a volume device or a disk slice.
5761047ba61eSachartre  * In these cases, the backend is accessed using the LDI interface.
5762047ba61eSachartre  */
57633c96341aSnarayan static int
5764047ba61eSachartre vd_setup_backend_ldi(vd_t *vd)
57651ae08745Sheppo {
5766e1ebb9ecSlm66018 	int		rval, status;
57671ae08745Sheppo 	struct dk_cinfo	dk_cinfo;
57683c96341aSnarayan 	char		*device_path = vd->device_path;
57691ae08745Sheppo 
57708fce2fd6Sachartre 	/* device has been opened by vd_identify_dev() */
57718fce2fd6Sachartre 	ASSERT(vd->ldi_handle[0] != NULL);
57728fce2fd6Sachartre 	ASSERT(vd->dev[0] != NULL);
57730a55fbb7Slm66018 
57743c96341aSnarayan 	vd->file = B_FALSE;
57754bac2208Snarayan 
577678fcd0a1Sachartre 	/* Verify backing device supports dk_cinfo */
5777e1ebb9ecSlm66018 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO,
5778047ba61eSachartre 	    (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred,
5779e1ebb9ecSlm66018 	    &rval)) != 0) {
5780e1ebb9ecSlm66018 		PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
5781e1ebb9ecSlm66018 		    status, device_path);
5782e1ebb9ecSlm66018 		return (status);
5783e1ebb9ecSlm66018 	}
5784e1ebb9ecSlm66018 	if (dk_cinfo.dki_partition >= V_NUMPAR) {
5785e1ebb9ecSlm66018 		PRN("slice %u >= maximum slice %u for %s",
5786e1ebb9ecSlm66018 		    dk_cinfo.dki_partition, V_NUMPAR, device_path);
5787e1ebb9ecSlm66018 		return (EIO);
5788e1ebb9ecSlm66018 	}
57894bac2208Snarayan 
57908fce2fd6Sachartre 	/*
57918fce2fd6Sachartre 	 * The device has been opened read-only by vd_identify_dev(), re-open
57928fce2fd6Sachartre 	 * it read-write if the write flag is set and we don't have an optical
57938fce2fd6Sachartre 	 * device such as a CD-ROM, which, for now, we do not permit writes to
57948fce2fd6Sachartre 	 * and thus should not export write operations to the client.
57958fce2fd6Sachartre 	 *
57968fce2fd6Sachartre 	 * Future: if/when we implement support for guest domains writing to
57978fce2fd6Sachartre 	 * optical devices we will need to do further checking of the media type
57988fce2fd6Sachartre 	 * to distinguish between read-only and writable discs.
57998fce2fd6Sachartre 	 */
58008fce2fd6Sachartre 	if (dk_cinfo.dki_ctype == DKC_CDROM) {
58018fce2fd6Sachartre 
58028fce2fd6Sachartre 		vd->open_flags &= ~FWRITE;
58038fce2fd6Sachartre 
58048fce2fd6Sachartre 	} else if (vd->open_flags & FWRITE) {
58058fce2fd6Sachartre 
58068fce2fd6Sachartre 		(void) ldi_close(vd->ldi_handle[0], vd->open_flags & ~FWRITE,
58078fce2fd6Sachartre 		    kcred);
58088fce2fd6Sachartre 		status = vd_open_using_ldi_by_name(vd, vd->open_flags);
58098fce2fd6Sachartre 		if (status != 0) {
58108fce2fd6Sachartre 			PR0("Failed to open (%s) = errno %d",
58118fce2fd6Sachartre 			    device_path, status);
58128fce2fd6Sachartre 			return (status);
58138fce2fd6Sachartre 		}
58148fce2fd6Sachartre 	}
58158fce2fd6Sachartre 
5816e1ebb9ecSlm66018 	/* Store the device's max transfer size for return to the client */
5817e1ebb9ecSlm66018 	vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
5818e1ebb9ecSlm66018 
5819047ba61eSachartre 	/*
582017cadca8Slm66018 	 * We need to work out if it's an ATAPI (IDE CD-ROM) or SCSI device so
582117cadca8Slm66018 	 * that we can use the correct CDB group when sending USCSI commands.
582217cadca8Slm66018 	 */
582317cadca8Slm66018 	vd->is_atapi_dev = vd_is_atapi_device(vd);
582417cadca8Slm66018 
582517cadca8Slm66018 	/*
5826047ba61eSachartre 	 * Export a full disk.
5827047ba61eSachartre 	 *
5828047ba61eSachartre 	 * When we use the LDI interface, we export a device as a full disk
5829047ba61eSachartre 	 * if we have an entire disk slice (slice 2) and if this slice is
5830047ba61eSachartre 	 * exported as a full disk and not as a single slice disk.
583117cadca8Slm66018 	 * Similarly, we want to use LDI if we are accessing a CD or DVD
583217cadca8Slm66018 	 * device (even if it isn't s2)
5833047ba61eSachartre 	 *
58348fce2fd6Sachartre 	 * Note that volume devices are exported as full disks using the vnode
5835047ba61eSachartre 	 * interface, not the LDI interface.
5836047ba61eSachartre 	 */
583717cadca8Slm66018 	if ((dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE &&
583817cadca8Slm66018 	    vd->vdisk_type == VD_DISK_TYPE_DISK) ||
583917cadca8Slm66018 	    dk_cinfo.dki_ctype == DKC_CDROM) {
58408fce2fd6Sachartre 		ASSERT(!vd->volume);
58412f5224aeSachartre 		if (dk_cinfo.dki_ctype == DKC_SCSI_CCS)
58422f5224aeSachartre 			vd->scsi = B_TRUE;
5843047ba61eSachartre 		return (vd_setup_full_disk(vd));
5844047ba61eSachartre 	}
5845047ba61eSachartre 
5846047ba61eSachartre 	/*
5847047ba61eSachartre 	 * Export a single slice disk.
5848047ba61eSachartre 	 *
58498fce2fd6Sachartre 	 * The exported device can be either a volume device or a disk slice. If
5850047ba61eSachartre 	 * it is a disk slice different from slice 2 then it is always exported
5851047ba61eSachartre 	 * as a single slice disk even if the "slice" option is not specified.
58528fce2fd6Sachartre 	 * If it is disk slice 2 or a volume device then it is exported as a
5853047ba61eSachartre 	 * single slice disk only if the "slice" option is specified.
5854047ba61eSachartre 	 */
5855047ba61eSachartre 	return (vd_setup_single_slice_disk(vd));
5856047ba61eSachartre }
5857047ba61eSachartre 
5858047ba61eSachartre static int
5859047ba61eSachartre vd_setup_single_slice_disk(vd_t *vd)
5860047ba61eSachartre {
5861edcc0754Sachartre 	int status, rval;
5862bae9e67eSachartre 	struct dk_label label;
5863047ba61eSachartre 	char *device_path = vd->device_path;
5864047ba61eSachartre 
5865047ba61eSachartre 	/* Get size of backing device */
5866047ba61eSachartre 	if (ldi_get_size(vd->ldi_handle[0], &vd->vdisk_size) != DDI_SUCCESS) {
5867047ba61eSachartre 		PRN("ldi_get_size() failed for %s", device_path);
58681ae08745Sheppo 		return (EIO);
58691ae08745Sheppo 	}
5870047ba61eSachartre 	vd->vdisk_size = lbtodb(vd->vdisk_size);	/* convert to blocks */
587117cadca8Slm66018 	vd->block_size = DEV_BSIZE;
587217cadca8Slm66018 	vd->vdisk_block_size = DEV_BSIZE;
587317cadca8Slm66018 	vd->vdisk_media = VD_MEDIA_FIXED;
5874047ba61eSachartre 
58758fce2fd6Sachartre 	if (vd->volume) {
5876047ba61eSachartre 		ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
587778fcd0a1Sachartre 	}
58780a55fbb7Slm66018 
5879047ba61eSachartre 	/*
5880047ba61eSachartre 	 * We export the slice as a single slice disk even if the "slice"
5881047ba61eSachartre 	 * option was not specified.
5882047ba61eSachartre 	 */
58831ae08745Sheppo 	vd->vdisk_type  = VD_DISK_TYPE_SLICE;
58841ae08745Sheppo 	vd->nslices	= 1;
58851ae08745Sheppo 
5886edcc0754Sachartre 	/*
5887edcc0754Sachartre 	 * When exporting a slice or a device as a single slice disk, we don't
5888edcc0754Sachartre 	 * care about any partitioning exposed by the backend. The goal is just
5889edcc0754Sachartre 	 * to export the backend as a flat storage. We provide a fake partition
5890edcc0754Sachartre 	 * table (either a VTOC or EFI), which presents only one slice, to
5891bae9e67eSachartre 	 * accommodate tools expecting a disk label. The selection of the label
5892bae9e67eSachartre 	 * type (VTOC or EFI) depends on the value of the vd_slice_label
5893bae9e67eSachartre 	 * variable.
5894edcc0754Sachartre 	 */
5895bae9e67eSachartre 	if (vd_slice_label == VD_DISK_LABEL_EFI ||
5896bae9e67eSachartre 	    vd->vdisk_size >= ONE_TERABYTE / DEV_BSIZE) {
5897bae9e67eSachartre 		vd->vdisk_label = VD_DISK_LABEL_EFI;
5898bae9e67eSachartre 	} else {
5899bae9e67eSachartre 		status = ldi_ioctl(vd->ldi_handle[0], DKIOCGVTOC,
5900bae9e67eSachartre 		    (intptr_t)&vd->vtoc, (vd->open_flags | FKIOCTL),
5901bae9e67eSachartre 		    kcred, &rval);
5902edcc0754Sachartre 
5903edcc0754Sachartre 		if (status == 0) {
5904bae9e67eSachartre 			status = ldi_ioctl(vd->ldi_handle[0], DKIOCGGEOM,
5905bae9e67eSachartre 			    (intptr_t)&vd->dk_geom, (vd->open_flags | FKIOCTL),
5906bae9e67eSachartre 			    kcred, &rval);
5907bae9e67eSachartre 
5908bae9e67eSachartre 			if (status != 0) {
5909bae9e67eSachartre 				PRN("ldi_ioctl(DKIOCGEOM) returned errno %d "
5910bae9e67eSachartre 				    "for %s", status, device_path);
5911bae9e67eSachartre 				return (status);
5912bae9e67eSachartre 			}
5913edcc0754Sachartre 			vd->vdisk_label = VD_DISK_LABEL_VTOC;
5914bae9e67eSachartre 
5915bae9e67eSachartre 		} else if (vd_slice_label == VD_DISK_LABEL_VTOC) {
5916bae9e67eSachartre 
5917bae9e67eSachartre 			vd->vdisk_label = VD_DISK_LABEL_VTOC;
5918bae9e67eSachartre 			vd_build_default_label(vd->vdisk_size * DEV_BSIZE,
5919bae9e67eSachartre 			    &label);
5920bae9e67eSachartre 			vd_label_to_vtocgeom(&label, &vd->vtoc, &vd->dk_geom);
5921bae9e67eSachartre 
5922bae9e67eSachartre 		} else {
5923bae9e67eSachartre 			vd->vdisk_label = VD_DISK_LABEL_EFI;
5924bae9e67eSachartre 		}
5925bae9e67eSachartre 	}
5926bae9e67eSachartre 
5927bae9e67eSachartre 	if (vd->vdisk_label == VD_DISK_LABEL_VTOC) {
5928bae9e67eSachartre 		/* export with a fake VTOC label */
592978fcd0a1Sachartre 		status = vd_setup_partition_vtoc(vd);
5930bae9e67eSachartre 
5931edcc0754Sachartre 	} else {
5932edcc0754Sachartre 		/* export with a fake EFI label */
5933edcc0754Sachartre 		status = vd_setup_partition_efi(vd);
593478fcd0a1Sachartre 	}
593578fcd0a1Sachartre 
59364bac2208Snarayan 	return (status);
59374bac2208Snarayan }
59381ae08745Sheppo 
59398fce2fd6Sachartre /*
59408fce2fd6Sachartre  * Description:
59418fce2fd6Sachartre  *	Open a device using its device path and identify if this is
59428fce2fd6Sachartre  *	a disk device or a volume device.
59438fce2fd6Sachartre  *
59448fce2fd6Sachartre  * Parameters:
59458fce2fd6Sachartre  *	vd 	- pointer to structure containing the vDisk info
59468fce2fd6Sachartre  *	dtype	- return the driver type of the device
59478fce2fd6Sachartre  *
59488fce2fd6Sachartre  * Return Value
59498fce2fd6Sachartre  *	0	- success
59508fce2fd6Sachartre  *	!= 0	- some other non-zero return value from ldi(9F) functions
59518fce2fd6Sachartre  */
59528fce2fd6Sachartre static int
59538fce2fd6Sachartre vd_identify_dev(vd_t *vd, int *dtype)
59548fce2fd6Sachartre {
59558fce2fd6Sachartre 	int status, i;
59568fce2fd6Sachartre 	char *device_path = vd->device_path;
59578fce2fd6Sachartre 	char *drv_name;
59588fce2fd6Sachartre 	int drv_type;
59598fce2fd6Sachartre 	vds_t *vds = vd->vds;
59608fce2fd6Sachartre 
59618fce2fd6Sachartre 	status = vd_open_using_ldi_by_name(vd, vd->open_flags & ~FWRITE);
59628fce2fd6Sachartre 	if (status != 0) {
59638fce2fd6Sachartre 		PR0("Failed to open (%s) = errno %d", device_path, status);
59648fce2fd6Sachartre 		return (status);
59658fce2fd6Sachartre 	}
59668fce2fd6Sachartre 
59678fce2fd6Sachartre 	/* Get device number of backing device */
59688fce2fd6Sachartre 	if ((status = ldi_get_dev(vd->ldi_handle[0], &vd->dev[0])) != 0) {
59698fce2fd6Sachartre 		PRN("ldi_get_dev() returned errno %d for %s",
59708fce2fd6Sachartre 		    status, device_path);
59718fce2fd6Sachartre 		return (status);
59728fce2fd6Sachartre 	}
59738fce2fd6Sachartre 
59748fce2fd6Sachartre 	/*
59758fce2fd6Sachartre 	 * We start by looking if the driver is in the list from vds.conf
59768fce2fd6Sachartre 	 * so that we can override the built-in list using vds.conf.
59778fce2fd6Sachartre 	 */
59788fce2fd6Sachartre 	drv_name = ddi_major_to_name(getmajor(vd->dev[0]));
59798fce2fd6Sachartre 	drv_type = VD_DRIVER_UNKNOWN;
59808fce2fd6Sachartre 
59818fce2fd6Sachartre 	/* check vds.conf list */
59828fce2fd6Sachartre 	for (i = 0; i < vds->num_drivers; i++) {
59838fce2fd6Sachartre 		if (vds->driver_types[i].type == VD_DRIVER_UNKNOWN) {
59848fce2fd6Sachartre 			/* ignore invalid entries */
59858fce2fd6Sachartre 			continue;
59868fce2fd6Sachartre 		}
59878fce2fd6Sachartre 		if (strcmp(drv_name, vds->driver_types[i].name) == 0) {
59888fce2fd6Sachartre 			drv_type = vds->driver_types[i].type;
59898fce2fd6Sachartre 			goto done;
59908fce2fd6Sachartre 		}
59918fce2fd6Sachartre 	}
59928fce2fd6Sachartre 
59938fce2fd6Sachartre 	/* check built-in list */
59948fce2fd6Sachartre 	for (i = 0; i < VDS_NUM_DRIVERS; i++) {
59958fce2fd6Sachartre 		if (strcmp(drv_name, vds_driver_types[i].name) == 0) {
59968fce2fd6Sachartre 			drv_type = vds_driver_types[i].type;
59978fce2fd6Sachartre 			goto done;
59988fce2fd6Sachartre 		}
59998fce2fd6Sachartre 	}
60008fce2fd6Sachartre 
60018fce2fd6Sachartre done:
60028fce2fd6Sachartre 	PR0("driver %s identified as %s", drv_name,
60038fce2fd6Sachartre 	    (drv_type == VD_DRIVER_DISK)? "DISK" :
60048fce2fd6Sachartre 	    (drv_type == VD_DRIVER_VOLUME)? "VOLUME" : "UNKNOWN");
60058fce2fd6Sachartre 
60068fce2fd6Sachartre 	*dtype = drv_type;
60078fce2fd6Sachartre 
60088fce2fd6Sachartre 	return (0);
60098fce2fd6Sachartre }
60108fce2fd6Sachartre 
60111ae08745Sheppo static int
6012047ba61eSachartre vd_setup_vd(vd_t *vd)
6013047ba61eSachartre {
60148fce2fd6Sachartre 	int		status, drv_type, pseudo;
6015047ba61eSachartre 	dev_info_t	*dip;
6016047ba61eSachartre 	vnode_t 	*vnp;
6017047ba61eSachartre 	char		*path = vd->device_path;
6018047ba61eSachartre 
6019047ba61eSachartre 	/* make sure the vdisk backend is valid */
6020047ba61eSachartre 	if ((status = lookupname(path, UIO_SYSSPACE,
6021047ba61eSachartre 	    FOLLOW, NULLVPP, &vnp)) != 0) {
6022047ba61eSachartre 		PR0("Cannot lookup %s errno %d", path, status);
6023047ba61eSachartre 		goto done;
6024047ba61eSachartre 	}
6025047ba61eSachartre 
6026047ba61eSachartre 	switch (vnp->v_type) {
6027047ba61eSachartre 	case VREG:
6028047ba61eSachartre 		/*
6029047ba61eSachartre 		 * Backend is a file so it is exported as a full disk or as a
6030047ba61eSachartre 		 * single slice disk using the vnode interface.
6031047ba61eSachartre 		 */
6032047ba61eSachartre 		VN_RELE(vnp);
60338fce2fd6Sachartre 		vd->volume = B_FALSE;
6034047ba61eSachartre 		status = vd_setup_backend_vnode(vd);
6035047ba61eSachartre 		break;
6036047ba61eSachartre 
6037047ba61eSachartre 	case VBLK:
6038047ba61eSachartre 	case VCHR:
6039047ba61eSachartre 		/*
6040047ba61eSachartre 		 * Backend is a device. The way it is exported depends on the
6041047ba61eSachartre 		 * type of the device.
6042047ba61eSachartre 		 *
60438fce2fd6Sachartre 		 * - A volume device is exported as a full disk using the vnode
6044047ba61eSachartre 		 *   interface or as a single slice disk using the LDI
6045047ba61eSachartre 		 *   interface.
6046047ba61eSachartre 		 *
6047047ba61eSachartre 		 * - A disk (represented by the slice 2 of that disk) is
6048047ba61eSachartre 		 *   exported as a full disk using the LDI interface.
6049047ba61eSachartre 		 *
6050047ba61eSachartre 		 * - A disk slice (different from slice 2) is always exported
6051047ba61eSachartre 		 *   as a single slice disk using the LDI interface.
6052047ba61eSachartre 		 *
6053047ba61eSachartre 		 * - The slice 2 of a disk is exported as a single slice disk
6054047ba61eSachartre 		 *   if the "slice" option is specified, otherwise the entire
6055047ba61eSachartre 		 *   disk will be exported. In any case, the LDI interface is
6056047ba61eSachartre 		 *   used.
6057047ba61eSachartre 		 */
6058047ba61eSachartre 
6059047ba61eSachartre 		/* check if this is a pseudo device */
6060047ba61eSachartre 		if ((dip = ddi_hold_devi_by_instance(getmajor(vnp->v_rdev),
6061047ba61eSachartre 		    dev_to_instance(vnp->v_rdev), 0))  == NULL) {
6062047ba61eSachartre 			PRN("%s is no longer accessible", path);
6063047ba61eSachartre 			VN_RELE(vnp);
6064047ba61eSachartre 			status = EIO;
6065047ba61eSachartre 			break;
6066047ba61eSachartre 		}
60678fce2fd6Sachartre 		pseudo = is_pseudo_device(dip);
6068047ba61eSachartre 		ddi_release_devi(dip);
6069047ba61eSachartre 		VN_RELE(vnp);
6070047ba61eSachartre 
60718fce2fd6Sachartre 		if (vd_identify_dev(vd, &drv_type) != 0) {
60728fce2fd6Sachartre 			PRN("%s identification failed", path);
60738fce2fd6Sachartre 			status = EIO;
60748fce2fd6Sachartre 			break;
60758fce2fd6Sachartre 		}
60768fce2fd6Sachartre 
60778fce2fd6Sachartre 		/*
60788fce2fd6Sachartre 		 * If the driver hasn't been identified then we consider that
60798fce2fd6Sachartre 		 * pseudo devices are volumes and other devices are disks.
60808fce2fd6Sachartre 		 */
60818fce2fd6Sachartre 		if (drv_type == VD_DRIVER_VOLUME ||
60828fce2fd6Sachartre 		    (drv_type == VD_DRIVER_UNKNOWN && pseudo)) {
60838fce2fd6Sachartre 			vd->volume = B_TRUE;
60848fce2fd6Sachartre 		} else {
60852f5224aeSachartre 			status = vd_setup_backend_ldi(vd);
60862f5224aeSachartre 			break;
60872f5224aeSachartre 		}
60882f5224aeSachartre 
6089047ba61eSachartre 		/*
60908fce2fd6Sachartre 		 * If this is a volume device then its usage depends if the
6091047ba61eSachartre 		 * "slice" option is set or not. If the "slice" option is set
60928fce2fd6Sachartre 		 * then the volume device will be exported as a single slice,
6093047ba61eSachartre 		 * otherwise it will be exported as a full disk.
60942f5224aeSachartre 		 *
60952f5224aeSachartre 		 * For backward compatibility, if vd_volume_force_slice is set
60968fce2fd6Sachartre 		 * then we always export volume devices as slices.
6097047ba61eSachartre 		 */
60982f5224aeSachartre 		if (vd_volume_force_slice) {
60992f5224aeSachartre 			vd->vdisk_type = VD_DISK_TYPE_SLICE;
61002f5224aeSachartre 			vd->nslices = 1;
61012f5224aeSachartre 		}
61022f5224aeSachartre 
61038fce2fd6Sachartre 		if (vd->vdisk_type == VD_DISK_TYPE_DISK) {
61048fce2fd6Sachartre 			/* close device opened during identification */
61058fce2fd6Sachartre 			(void) ldi_close(vd->ldi_handle[0],
61068fce2fd6Sachartre 			    vd->open_flags & ~FWRITE, kcred);
61078fce2fd6Sachartre 			vd->ldi_handle[0] = NULL;
61088fce2fd6Sachartre 			vd->dev[0] = 0;
6109047ba61eSachartre 			status = vd_setup_backend_vnode(vd);
61108fce2fd6Sachartre 		} else {
6111047ba61eSachartre 			status = vd_setup_backend_ldi(vd);
61128fce2fd6Sachartre 		}
6113047ba61eSachartre 		break;
6114047ba61eSachartre 
6115047ba61eSachartre 	default:
6116047ba61eSachartre 		PRN("Unsupported vdisk backend %s", path);
6117047ba61eSachartre 		VN_RELE(vnp);
6118047ba61eSachartre 		status = EBADF;
6119047ba61eSachartre 	}
6120047ba61eSachartre 
6121047ba61eSachartre done:
6122047ba61eSachartre 	if (status != 0) {
6123047ba61eSachartre 		/*
6124047ba61eSachartre 		 * If the error is retryable print an error message only
6125047ba61eSachartre 		 * during the first try.
6126047ba61eSachartre 		 */
6127047ba61eSachartre 		if (status == ENXIO || status == ENODEV ||
6128047ba61eSachartre 		    status == ENOENT || status == EROFS) {
6129047ba61eSachartre 			if (!(vd->initialized & VD_SETUP_ERROR)) {
6130047ba61eSachartre 				PRN("%s is currently inaccessible (error %d)",
6131047ba61eSachartre 				    path, status);
6132047ba61eSachartre 			}
6133047ba61eSachartre 			status = EAGAIN;
6134047ba61eSachartre 		} else {
6135047ba61eSachartre 			PRN("%s can not be exported as a virtual disk "
6136047ba61eSachartre 			    "(error %d)", path, status);
6137047ba61eSachartre 		}
6138047ba61eSachartre 		vd->initialized |= VD_SETUP_ERROR;
6139047ba61eSachartre 
6140047ba61eSachartre 	} else if (vd->initialized & VD_SETUP_ERROR) {
6141047ba61eSachartre 		/* print a message only if we previously had an error */
6142047ba61eSachartre 		PRN("%s is now online", path);
6143047ba61eSachartre 		vd->initialized &= ~VD_SETUP_ERROR;
6144047ba61eSachartre 	}
6145047ba61eSachartre 
6146047ba61eSachartre 	return (status);
6147047ba61eSachartre }
6148047ba61eSachartre 
6149047ba61eSachartre static int
6150047ba61eSachartre vds_do_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options,
6151047ba61eSachartre     uint64_t ldc_id, vd_t **vdp)
61521ae08745Sheppo {
61531ae08745Sheppo 	char			tq_name[TASKQ_NAMELEN];
61540a55fbb7Slm66018 	int			status;
61551ae08745Sheppo 	ddi_iblock_cookie_t	iblock = NULL;
61561ae08745Sheppo 	ldc_attr_t		ldc_attr;
61571ae08745Sheppo 	vd_t			*vd;
61581ae08745Sheppo 
61591ae08745Sheppo 
61601ae08745Sheppo 	ASSERT(vds != NULL);
6161e1ebb9ecSlm66018 	ASSERT(device_path != NULL);
61621ae08745Sheppo 	ASSERT(vdp != NULL);
6163e1ebb9ecSlm66018 	PR0("Adding vdisk for %s", device_path);
61641ae08745Sheppo 
61651ae08745Sheppo 	if ((vd = kmem_zalloc(sizeof (*vd), KM_NOSLEEP)) == NULL) {
61661ae08745Sheppo 		PRN("No memory for virtual disk");
61671ae08745Sheppo 		return (EAGAIN);
61681ae08745Sheppo 	}
61691ae08745Sheppo 	*vdp = vd;	/* assign here so vds_destroy_vd() can cleanup later */
61701ae08745Sheppo 	vd->vds = vds;
61713c96341aSnarayan 	(void) strncpy(vd->device_path, device_path, MAXPATHLEN);
61721ae08745Sheppo 
6173047ba61eSachartre 	/* Setup open flags */
6174047ba61eSachartre 	vd->open_flags = FREAD;
6175047ba61eSachartre 
6176047ba61eSachartre 	if (!(options & VD_OPT_RDONLY))
6177047ba61eSachartre 		vd->open_flags |= FWRITE;
6178047ba61eSachartre 
6179047ba61eSachartre 	if (options & VD_OPT_EXCLUSIVE)
6180047ba61eSachartre 		vd->open_flags |= FEXCL;
6181047ba61eSachartre 
6182047ba61eSachartre 	/* Setup disk type */
6183047ba61eSachartre 	if (options & VD_OPT_SLICE) {
6184047ba61eSachartre 		vd->vdisk_type = VD_DISK_TYPE_SLICE;
6185047ba61eSachartre 		vd->nslices = 1;
6186047ba61eSachartre 	} else {
6187047ba61eSachartre 		vd->vdisk_type = VD_DISK_TYPE_DISK;
6188047ba61eSachartre 		vd->nslices = V_NUMPAR;
6189047ba61eSachartre 	}
6190047ba61eSachartre 
6191047ba61eSachartre 	/* default disk label */
6192047ba61eSachartre 	vd->vdisk_label = VD_DISK_LABEL_UNK;
6193047ba61eSachartre 
61940a55fbb7Slm66018 	/* Open vdisk and initialize parameters */
61953c96341aSnarayan 	if ((status = vd_setup_vd(vd)) == 0) {
61963c96341aSnarayan 		vd->initialized |= VD_DISK_READY;
61971ae08745Sheppo 
61983c96341aSnarayan 		ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
61998fce2fd6Sachartre 		PR0("vdisk_type = %s, volume = %s, file = %s, nslices = %u",
62003c96341aSnarayan 		    ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
62018fce2fd6Sachartre 		    (vd->volume ? "yes" : "no"), (vd->file ? "yes" : "no"),
62023c96341aSnarayan 		    vd->nslices);
62033c96341aSnarayan 	} else {
62043c96341aSnarayan 		if (status != EAGAIN)
62053c96341aSnarayan 			return (status);
62063c96341aSnarayan 	}
62071ae08745Sheppo 
62081ae08745Sheppo 	/* Initialize locking */
62091ae08745Sheppo 	if (ddi_get_soft_iblock_cookie(vds->dip, DDI_SOFTINT_MED,
62101ae08745Sheppo 	    &iblock) != DDI_SUCCESS) {
62111ae08745Sheppo 		PRN("Could not get iblock cookie.");
62121ae08745Sheppo 		return (EIO);
62131ae08745Sheppo 	}
62141ae08745Sheppo 
62151ae08745Sheppo 	mutex_init(&vd->lock, NULL, MUTEX_DRIVER, iblock);
62161ae08745Sheppo 	vd->initialized |= VD_LOCKING;
62171ae08745Sheppo 
62181ae08745Sheppo 
6219d10e4ef2Snarayan 	/* Create start and completion task queues for the vdisk */
6220d10e4ef2Snarayan 	(void) snprintf(tq_name, sizeof (tq_name), "vd_startq%lu", id);
62211ae08745Sheppo 	PR1("tq_name = %s", tq_name);
6222d10e4ef2Snarayan 	if ((vd->startq = ddi_taskq_create(vds->dip, tq_name, 1,
62231ae08745Sheppo 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
62241ae08745Sheppo 		PRN("Could not create task queue");
62251ae08745Sheppo 		return (EIO);
62261ae08745Sheppo 	}
6227d10e4ef2Snarayan 	(void) snprintf(tq_name, sizeof (tq_name), "vd_completionq%lu", id);
6228d10e4ef2Snarayan 	PR1("tq_name = %s", tq_name);
6229d10e4ef2Snarayan 	if ((vd->completionq = ddi_taskq_create(vds->dip, tq_name, 1,
6230d10e4ef2Snarayan 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
6231d10e4ef2Snarayan 		PRN("Could not create task queue");
6232d10e4ef2Snarayan 		return (EIO);
6233d10e4ef2Snarayan 	}
62345b98b509Sachartre 
62355b98b509Sachartre 	/* Allocate the staging buffer */
62365b98b509Sachartre 	vd->max_msglen = sizeof (vio_msg_t);	/* baseline vio message size */
62375b98b509Sachartre 	vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
62385b98b509Sachartre 
6239d10e4ef2Snarayan 	vd->enabled = 1;	/* before callback can dispatch to startq */
62401ae08745Sheppo 
62411ae08745Sheppo 
62421ae08745Sheppo 	/* Bring up LDC */
62431ae08745Sheppo 	ldc_attr.devclass	= LDC_DEV_BLK_SVC;
62441ae08745Sheppo 	ldc_attr.instance	= ddi_get_instance(vds->dip);
62451ae08745Sheppo 	ldc_attr.mode		= LDC_MODE_UNRELIABLE;
6246e1ebb9ecSlm66018 	ldc_attr.mtu		= VD_LDC_MTU;
62471ae08745Sheppo 	if ((status = ldc_init(ldc_id, &ldc_attr, &vd->ldc_handle)) != 0) {
624817cadca8Slm66018 		PRN("Could not initialize LDC channel %lx, "
6249690555a1Sachartre 		    "init failed with error %d", ldc_id, status);
62501ae08745Sheppo 		return (status);
62511ae08745Sheppo 	}
62521ae08745Sheppo 	vd->initialized |= VD_LDC;
62531ae08745Sheppo 
62541ae08745Sheppo 	if ((status = ldc_reg_callback(vd->ldc_handle, vd_handle_ldc_events,
62551ae08745Sheppo 	    (caddr_t)vd)) != 0) {
6256690555a1Sachartre 		PRN("Could not initialize LDC channel %lu,"
6257690555a1Sachartre 		    "reg_callback failed with error %d", ldc_id, status);
62581ae08745Sheppo 		return (status);
62591ae08745Sheppo 	}
62601ae08745Sheppo 
62611ae08745Sheppo 	if ((status = ldc_open(vd->ldc_handle)) != 0) {
6262690555a1Sachartre 		PRN("Could not initialize LDC channel %lu,"
6263690555a1Sachartre 		    "open failed with error %d", ldc_id, status);
62641ae08745Sheppo 		return (status);
62651ae08745Sheppo 	}
62661ae08745Sheppo 
62673af08d82Slm66018 	if ((status = ldc_up(vd->ldc_handle)) != 0) {
626834683adeSsg70180 		PR0("ldc_up() returned errno %d", status);
62693af08d82Slm66018 	}
62703af08d82Slm66018 
62714bac2208Snarayan 	/* Allocate the inband task memory handle */
62724bac2208Snarayan 	status = ldc_mem_alloc_handle(vd->ldc_handle, &(vd->inband_task.mhdl));
62734bac2208Snarayan 	if (status) {
6274690555a1Sachartre 		PRN("Could not initialize LDC channel %lu,"
6275690555a1Sachartre 		    "alloc_handle failed with error %d", ldc_id, status);
62764bac2208Snarayan 		return (ENXIO);
62774bac2208Snarayan 	}
62781ae08745Sheppo 
62791ae08745Sheppo 	/* Add the successfully-initialized vdisk to the server's table */
62801ae08745Sheppo 	if (mod_hash_insert(vds->vd_table, (mod_hash_key_t)id, vd) != 0) {
62811ae08745Sheppo 		PRN("Error adding vdisk ID %lu to table", id);
62821ae08745Sheppo 		return (EIO);
62831ae08745Sheppo 	}
62841ae08745Sheppo 
62853af08d82Slm66018 	/* store initial state */
62863af08d82Slm66018 	vd->state = VD_STATE_INIT;
62873af08d82Slm66018 
62881ae08745Sheppo 	return (0);
62891ae08745Sheppo }
62901ae08745Sheppo 
62913af08d82Slm66018 static void
62923af08d82Slm66018 vd_free_dring_task(vd_t *vdp)
62933af08d82Slm66018 {
62943af08d82Slm66018 	if (vdp->dring_task != NULL) {
62953af08d82Slm66018 		ASSERT(vdp->dring_len != 0);
62963af08d82Slm66018 		/* Free all dring_task memory handles */
62973af08d82Slm66018 		for (int i = 0; i < vdp->dring_len; i++) {
62983af08d82Slm66018 			(void) ldc_mem_free_handle(vdp->dring_task[i].mhdl);
62993af08d82Slm66018 			kmem_free(vdp->dring_task[i].msg, vdp->max_msglen);
63003af08d82Slm66018 			vdp->dring_task[i].msg = NULL;
63013af08d82Slm66018 		}
63023af08d82Slm66018 		kmem_free(vdp->dring_task,
63033af08d82Slm66018 		    (sizeof (*vdp->dring_task)) * vdp->dring_len);
63043af08d82Slm66018 		vdp->dring_task = NULL;
63053af08d82Slm66018 	}
63063af08d82Slm66018 }
63073af08d82Slm66018 
63081ae08745Sheppo /*
63091ae08745Sheppo  * Destroy the state associated with a virtual disk
63101ae08745Sheppo  */
63111ae08745Sheppo static void
63121ae08745Sheppo vds_destroy_vd(void *arg)
63131ae08745Sheppo {
63141ae08745Sheppo 	vd_t	*vd = (vd_t *)arg;
631534683adeSsg70180 	int	retry = 0, rv;
63161ae08745Sheppo 
63171ae08745Sheppo 	if (vd == NULL)
63181ae08745Sheppo 		return;
63191ae08745Sheppo 
6320d10e4ef2Snarayan 	PR0("Destroying vdisk state");
6321d10e4ef2Snarayan 
63221ae08745Sheppo 	/* Disable queuing requests for the vdisk */
63231ae08745Sheppo 	if (vd->initialized & VD_LOCKING) {
63241ae08745Sheppo 		mutex_enter(&vd->lock);
63251ae08745Sheppo 		vd->enabled = 0;
63261ae08745Sheppo 		mutex_exit(&vd->lock);
63271ae08745Sheppo 	}
63281ae08745Sheppo 
6329d10e4ef2Snarayan 	/* Drain and destroy start queue (*before* destroying completionq) */
6330d10e4ef2Snarayan 	if (vd->startq != NULL)
6331d10e4ef2Snarayan 		ddi_taskq_destroy(vd->startq);	/* waits for queued tasks */
6332d10e4ef2Snarayan 
6333d10e4ef2Snarayan 	/* Drain and destroy completion queue (*before* shutting down LDC) */
6334d10e4ef2Snarayan 	if (vd->completionq != NULL)
6335d10e4ef2Snarayan 		ddi_taskq_destroy(vd->completionq);	/* waits for tasks */
6336d10e4ef2Snarayan 
63373af08d82Slm66018 	vd_free_dring_task(vd);
63383af08d82Slm66018 
633934683adeSsg70180 	/* Free the inband task memory handle */
634034683adeSsg70180 	(void) ldc_mem_free_handle(vd->inband_task.mhdl);
634134683adeSsg70180 
634234683adeSsg70180 	/* Shut down LDC */
634334683adeSsg70180 	if (vd->initialized & VD_LDC) {
634434683adeSsg70180 		/* unmap the dring */
634534683adeSsg70180 		if (vd->initialized & VD_DRING)
634634683adeSsg70180 			(void) ldc_mem_dring_unmap(vd->dring_handle);
634734683adeSsg70180 
634834683adeSsg70180 		/* close LDC channel - retry on EAGAIN */
634934683adeSsg70180 		while ((rv = ldc_close(vd->ldc_handle)) == EAGAIN) {
635034683adeSsg70180 			if (++retry > vds_ldc_retries) {
635134683adeSsg70180 				PR0("Timed out closing channel");
635234683adeSsg70180 				break;
635334683adeSsg70180 			}
635434683adeSsg70180 			drv_usecwait(vds_ldc_delay);
635534683adeSsg70180 		}
635634683adeSsg70180 		if (rv == 0) {
635734683adeSsg70180 			(void) ldc_unreg_callback(vd->ldc_handle);
635834683adeSsg70180 			(void) ldc_fini(vd->ldc_handle);
635934683adeSsg70180 		} else {
636034683adeSsg70180 			/*
636134683adeSsg70180 			 * Closing the LDC channel has failed. Ideally we should
636234683adeSsg70180 			 * fail here but there is no Zeus level infrastructure
636334683adeSsg70180 			 * to handle this. The MD has already been changed and
636434683adeSsg70180 			 * we have to do the close. So we try to do as much
636534683adeSsg70180 			 * clean up as we can.
636634683adeSsg70180 			 */
636734683adeSsg70180 			(void) ldc_set_cb_mode(vd->ldc_handle, LDC_CB_DISABLE);
636834683adeSsg70180 			while (ldc_unreg_callback(vd->ldc_handle) == EAGAIN)
636934683adeSsg70180 				drv_usecwait(vds_ldc_delay);
637034683adeSsg70180 		}
637134683adeSsg70180 	}
637234683adeSsg70180 
63733af08d82Slm66018 	/* Free the staging buffer for msgs */
63743af08d82Slm66018 	if (vd->vio_msgp != NULL) {
63753af08d82Slm66018 		kmem_free(vd->vio_msgp, vd->max_msglen);
63763af08d82Slm66018 		vd->vio_msgp = NULL;
63773af08d82Slm66018 	}
63783af08d82Slm66018 
63793af08d82Slm66018 	/* Free the inband message buffer */
63803af08d82Slm66018 	if (vd->inband_task.msg != NULL) {
63813af08d82Slm66018 		kmem_free(vd->inband_task.msg, vd->max_msglen);
63823af08d82Slm66018 		vd->inband_task.msg = NULL;
6383d10e4ef2Snarayan 	}
6384da6c28aaSamw 
63853c96341aSnarayan 	if (vd->file) {
6386690555a1Sachartre 		/* Close file */
6387047ba61eSachartre 		(void) VOP_CLOSE(vd->file_vnode, vd->open_flags, 1,
6388da6c28aaSamw 		    0, kcred, NULL);
63893c96341aSnarayan 		VN_RELE(vd->file_vnode);
639087a7269eSachartre 		if (vd->file_devid != NULL)
639187a7269eSachartre 			ddi_devid_free(vd->file_devid);
63923c96341aSnarayan 	} else {
63931ae08745Sheppo 		/* Close any open backing-device slices */
6394bae9e67eSachartre 		for (uint_t slice = 0; slice < V_NUMPAR; slice++) {
63951ae08745Sheppo 			if (vd->ldi_handle[slice] != NULL) {
63961ae08745Sheppo 				PR0("Closing slice %u", slice);
63971ae08745Sheppo 				(void) ldi_close(vd->ldi_handle[slice],
6398047ba61eSachartre 				    vd->open_flags, kcred);
63991ae08745Sheppo 			}
64001ae08745Sheppo 		}
64013c96341aSnarayan 	}
64021ae08745Sheppo 
6403bae9e67eSachartre 	/* Free any fake label */
6404bae9e67eSachartre 	if (vd->flabel) {
6405bae9e67eSachartre 		kmem_free(vd->flabel, vd->flabel_size);
6406bae9e67eSachartre 		vd->flabel = NULL;
6407bae9e67eSachartre 		vd->flabel_size = 0;
6408bae9e67eSachartre 	}
6409bae9e67eSachartre 
64101ae08745Sheppo 	/* Free lock */
64111ae08745Sheppo 	if (vd->initialized & VD_LOCKING)
64121ae08745Sheppo 		mutex_destroy(&vd->lock);
64131ae08745Sheppo 
64141ae08745Sheppo 	/* Finally, free the vdisk structure itself */
64151ae08745Sheppo 	kmem_free(vd, sizeof (*vd));
64161ae08745Sheppo }
64171ae08745Sheppo 
64181ae08745Sheppo static int
6419047ba61eSachartre vds_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options,
6420047ba61eSachartre     uint64_t ldc_id)
64211ae08745Sheppo {
64221ae08745Sheppo 	int	status;
64231ae08745Sheppo 	vd_t	*vd = NULL;
64241ae08745Sheppo 
64251ae08745Sheppo 
6426047ba61eSachartre 	if ((status = vds_do_init_vd(vds, id, device_path, options,
6427047ba61eSachartre 	    ldc_id, &vd)) != 0)
64281ae08745Sheppo 		vds_destroy_vd(vd);
64291ae08745Sheppo 
64301ae08745Sheppo 	return (status);
64311ae08745Sheppo }
64321ae08745Sheppo 
64331ae08745Sheppo static int
64341ae08745Sheppo vds_do_get_ldc_id(md_t *md, mde_cookie_t vd_node, mde_cookie_t *channel,
64351ae08745Sheppo     uint64_t *ldc_id)
64361ae08745Sheppo {
64371ae08745Sheppo 	int	num_channels;
64381ae08745Sheppo 
64391ae08745Sheppo 
64401ae08745Sheppo 	/* Look for channel endpoint child(ren) of the vdisk MD node */
64411ae08745Sheppo 	if ((num_channels = md_scan_dag(md, vd_node,
64421ae08745Sheppo 	    md_find_name(md, VD_CHANNEL_ENDPOINT),
64431ae08745Sheppo 	    md_find_name(md, "fwd"), channel)) <= 0) {
64441ae08745Sheppo 		PRN("No \"%s\" found for virtual disk", VD_CHANNEL_ENDPOINT);
64451ae08745Sheppo 		return (-1);
64461ae08745Sheppo 	}
64471ae08745Sheppo 
64481ae08745Sheppo 	/* Get the "id" value for the first channel endpoint node */
64491ae08745Sheppo 	if (md_get_prop_val(md, channel[0], VD_ID_PROP, ldc_id) != 0) {
64501ae08745Sheppo 		PRN("No \"%s\" property found for \"%s\" of vdisk",
64511ae08745Sheppo 		    VD_ID_PROP, VD_CHANNEL_ENDPOINT);
64521ae08745Sheppo 		return (-1);
64531ae08745Sheppo 	}
64541ae08745Sheppo 
64551ae08745Sheppo 	if (num_channels > 1) {
64561ae08745Sheppo 		PRN("Using ID of first of multiple channels for this vdisk");
64571ae08745Sheppo 	}
64581ae08745Sheppo 
64591ae08745Sheppo 	return (0);
64601ae08745Sheppo }
64611ae08745Sheppo 
64621ae08745Sheppo static int
64631ae08745Sheppo vds_get_ldc_id(md_t *md, mde_cookie_t vd_node, uint64_t *ldc_id)
64641ae08745Sheppo {
64651ae08745Sheppo 	int		num_nodes, status;
64661ae08745Sheppo 	size_t		size;
64671ae08745Sheppo 	mde_cookie_t	*channel;
64681ae08745Sheppo 
64691ae08745Sheppo 
64701ae08745Sheppo 	if ((num_nodes = md_node_count(md)) <= 0) {
64711ae08745Sheppo 		PRN("Invalid node count in Machine Description subtree");
64721ae08745Sheppo 		return (-1);
64731ae08745Sheppo 	}
64741ae08745Sheppo 	size = num_nodes*(sizeof (*channel));
64751ae08745Sheppo 	channel = kmem_zalloc(size, KM_SLEEP);
64761ae08745Sheppo 	status = vds_do_get_ldc_id(md, vd_node, channel, ldc_id);
64771ae08745Sheppo 	kmem_free(channel, size);
64781ae08745Sheppo 
64791ae08745Sheppo 	return (status);
64801ae08745Sheppo }
64811ae08745Sheppo 
6482047ba61eSachartre /*
6483047ba61eSachartre  * Function:
6484047ba61eSachartre  *	vds_get_options
6485047ba61eSachartre  *
6486047ba61eSachartre  * Description:
6487047ba61eSachartre  * 	Parse the options of a vds node. Options are defined as an array
6488047ba61eSachartre  *	of strings in the vds-block-device-opts property of the vds node
6489047ba61eSachartre  *	in the machine description. Options are returned as a bitmask. The
6490047ba61eSachartre  *	mapping between the bitmask options and the options strings from the
6491047ba61eSachartre  *	machine description is defined in the vd_bdev_options[] array.
6492047ba61eSachartre  *
6493047ba61eSachartre  *	The vds-block-device-opts property is optional. If a vds has no such
6494047ba61eSachartre  *	property then no option is defined.
6495047ba61eSachartre  *
6496047ba61eSachartre  * Parameters:
6497047ba61eSachartre  *	md		- machine description.
6498047ba61eSachartre  *	vd_node		- vds node in the machine description for which
6499047ba61eSachartre  *			  options have to be parsed.
6500047ba61eSachartre  *	options		- the returned options.
6501047ba61eSachartre  *
6502047ba61eSachartre  * Return Code:
6503047ba61eSachartre  *	none.
6504047ba61eSachartre  */
6505047ba61eSachartre static void
6506047ba61eSachartre vds_get_options(md_t *md, mde_cookie_t vd_node, uint64_t *options)
6507047ba61eSachartre {
6508047ba61eSachartre 	char	*optstr, *opt;
6509047ba61eSachartre 	int	len, n, i;
6510047ba61eSachartre 
6511047ba61eSachartre 	*options = 0;
6512047ba61eSachartre 
6513047ba61eSachartre 	if (md_get_prop_data(md, vd_node, VD_BLOCK_DEVICE_OPTS,
6514047ba61eSachartre 	    (uint8_t **)&optstr, &len) != 0) {
6515047ba61eSachartre 		PR0("No options found");
6516047ba61eSachartre 		return;
6517047ba61eSachartre 	}
6518047ba61eSachartre 
6519047ba61eSachartre 	/* parse options */
6520047ba61eSachartre 	opt = optstr;
6521047ba61eSachartre 	n = sizeof (vd_bdev_options) / sizeof (vd_option_t);
6522047ba61eSachartre 
6523047ba61eSachartre 	while (opt < optstr + len) {
6524047ba61eSachartre 		for (i = 0; i < n; i++) {
6525047ba61eSachartre 			if (strncmp(vd_bdev_options[i].vdo_name,
6526047ba61eSachartre 			    opt, VD_OPTION_NLEN) == 0) {
6527047ba61eSachartre 				*options |= vd_bdev_options[i].vdo_value;
6528047ba61eSachartre 				break;
6529047ba61eSachartre 			}
6530047ba61eSachartre 		}
6531047ba61eSachartre 
6532047ba61eSachartre 		if (i < n) {
6533047ba61eSachartre 			PR0("option: %s", opt);
6534047ba61eSachartre 		} else {
6535047ba61eSachartre 			PRN("option %s is unknown or unsupported", opt);
6536047ba61eSachartre 		}
6537047ba61eSachartre 
6538047ba61eSachartre 		opt += strlen(opt) + 1;
6539047ba61eSachartre 	}
6540047ba61eSachartre }
6541047ba61eSachartre 
65421ae08745Sheppo static void
65438fce2fd6Sachartre vds_driver_types_free(vds_t *vds)
65448fce2fd6Sachartre {
65458fce2fd6Sachartre 	if (vds->driver_types != NULL) {
65468fce2fd6Sachartre 		kmem_free(vds->driver_types, sizeof (vd_driver_type_t) *
65478fce2fd6Sachartre 		    vds->num_drivers);
65488fce2fd6Sachartre 		vds->driver_types = NULL;
65498fce2fd6Sachartre 		vds->num_drivers = 0;
65508fce2fd6Sachartre 	}
65518fce2fd6Sachartre }
65528fce2fd6Sachartre 
65538fce2fd6Sachartre /*
65548fce2fd6Sachartre  * Update the driver type list with information from vds.conf.
65558fce2fd6Sachartre  */
65568fce2fd6Sachartre static void
65578fce2fd6Sachartre vds_driver_types_update(vds_t *vds)
65588fce2fd6Sachartre {
65598fce2fd6Sachartre 	char **list, *s;
65608fce2fd6Sachartre 	uint_t i, num, count = 0, len;
65618fce2fd6Sachartre 
65628fce2fd6Sachartre 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, vds->dip,
65638fce2fd6Sachartre 	    DDI_PROP_DONTPASS, "driver-type-list", &list, &num) !=
65648fce2fd6Sachartre 	    DDI_PROP_SUCCESS)
65658fce2fd6Sachartre 		return;
65668fce2fd6Sachartre 
65678fce2fd6Sachartre 	/*
65688fce2fd6Sachartre 	 * We create a driver_types list with as many as entries as there
65698fce2fd6Sachartre 	 * is in the driver-type-list from vds.conf. However only valid
65708fce2fd6Sachartre 	 * entries will be populated (i.e. entries from driver-type-list
65718fce2fd6Sachartre 	 * with a valid syntax). Invalid entries will be left blank so
65728fce2fd6Sachartre 	 * they will have no driver name and the driver type will be
65738fce2fd6Sachartre 	 * VD_DRIVER_UNKNOWN (= 0).
65748fce2fd6Sachartre 	 */
65758fce2fd6Sachartre 	vds->num_drivers = num;
65768fce2fd6Sachartre 	vds->driver_types = kmem_zalloc(sizeof (vd_driver_type_t) * num,
65778fce2fd6Sachartre 	    KM_SLEEP);
65788fce2fd6Sachartre 
65798fce2fd6Sachartre 	for (i = 0; i < num; i++) {
65808fce2fd6Sachartre 
65818fce2fd6Sachartre 		s = strchr(list[i], ':');
65828fce2fd6Sachartre 
65838fce2fd6Sachartre 		if (s == NULL) {
65848fce2fd6Sachartre 			PRN("vds.conf: driver-type-list, entry %d (%s): "
65858fce2fd6Sachartre 			    "a colon is expected in the entry",
65868fce2fd6Sachartre 			    i, list[i]);
65878fce2fd6Sachartre 			continue;
65888fce2fd6Sachartre 		}
65898fce2fd6Sachartre 
65908fce2fd6Sachartre 		len = (uintptr_t)s - (uintptr_t)list[i];
65918fce2fd6Sachartre 
65928fce2fd6Sachartre 		if (len == 0) {
65938fce2fd6Sachartre 			PRN("vds.conf: driver-type-list, entry %d (%s): "
65948fce2fd6Sachartre 			    "the driver name is empty",
65958fce2fd6Sachartre 			    i, list[i]);
65968fce2fd6Sachartre 			continue;
65978fce2fd6Sachartre 		}
65988fce2fd6Sachartre 
65998fce2fd6Sachartre 		if (len >= VD_DRIVER_NAME_LEN) {
66008fce2fd6Sachartre 			PRN("vds.conf: driver-type-list, entry %d (%s): "
66018fce2fd6Sachartre 			    "the driver name is too long",
66028fce2fd6Sachartre 			    i, list[i]);
66038fce2fd6Sachartre 			continue;
66048fce2fd6Sachartre 		}
66058fce2fd6Sachartre 
66068fce2fd6Sachartre 		if (strcmp(s + 1, "disk") == 0) {
66078fce2fd6Sachartre 
66088fce2fd6Sachartre 			vds->driver_types[i].type = VD_DRIVER_DISK;
66098fce2fd6Sachartre 
66108fce2fd6Sachartre 		} else if (strcmp(s + 1, "volume") == 0) {
66118fce2fd6Sachartre 
66128fce2fd6Sachartre 			vds->driver_types[i].type = VD_DRIVER_VOLUME;
66138fce2fd6Sachartre 
66148fce2fd6Sachartre 		} else {
66158fce2fd6Sachartre 			PRN("vds.conf: driver-type-list, entry %d (%s): "
66168fce2fd6Sachartre 			    "the driver type is invalid",
66178fce2fd6Sachartre 			    i, list[i]);
66188fce2fd6Sachartre 			continue;
66198fce2fd6Sachartre 		}
66208fce2fd6Sachartre 
66218fce2fd6Sachartre 		(void) strncpy(vds->driver_types[i].name, list[i], len);
66228fce2fd6Sachartre 
66238fce2fd6Sachartre 		PR0("driver-type-list, entry %d (%s) added",
66248fce2fd6Sachartre 		    i, list[i]);
66258fce2fd6Sachartre 
66268fce2fd6Sachartre 		count++;
66278fce2fd6Sachartre 	}
66288fce2fd6Sachartre 
66298fce2fd6Sachartre 	ddi_prop_free(list);
66308fce2fd6Sachartre 
66318fce2fd6Sachartre 	if (count == 0) {
66328fce2fd6Sachartre 		/* nothing was added, clean up */
66338fce2fd6Sachartre 		vds_driver_types_free(vds);
66348fce2fd6Sachartre 	}
66358fce2fd6Sachartre }
66368fce2fd6Sachartre 
66378fce2fd6Sachartre static void
66381ae08745Sheppo vds_add_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
66391ae08745Sheppo {
6640e1ebb9ecSlm66018 	char		*device_path = NULL;
6641047ba61eSachartre 	uint64_t	id = 0, ldc_id = 0, options = 0;
66421ae08745Sheppo 
66431ae08745Sheppo 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
66441ae08745Sheppo 		PRN("Error getting vdisk \"%s\"", VD_ID_PROP);
66451ae08745Sheppo 		return;
66461ae08745Sheppo 	}
66471ae08745Sheppo 	PR0("Adding vdisk ID %lu", id);
66481ae08745Sheppo 	if (md_get_prop_str(md, vd_node, VD_BLOCK_DEVICE_PROP,
6649e1ebb9ecSlm66018 	    &device_path) != 0) {
66501ae08745Sheppo 		PRN("Error getting vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
66511ae08745Sheppo 		return;
66521ae08745Sheppo 	}
66531ae08745Sheppo 
6654047ba61eSachartre 	vds_get_options(md, vd_node, &options);
6655047ba61eSachartre 
66561ae08745Sheppo 	if (vds_get_ldc_id(md, vd_node, &ldc_id) != 0) {
66571ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", id);
66581ae08745Sheppo 		return;
66591ae08745Sheppo 	}
66601ae08745Sheppo 
6661047ba61eSachartre 	if (vds_init_vd(vds, id, device_path, options, ldc_id) != 0) {
66621ae08745Sheppo 		PRN("Failed to add vdisk ID %lu", id);
666317cadca8Slm66018 		if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0)
666417cadca8Slm66018 			PRN("No vDisk entry found for vdisk ID %lu", id);
66651ae08745Sheppo 		return;
66661ae08745Sheppo 	}
66671ae08745Sheppo }
66681ae08745Sheppo 
66691ae08745Sheppo static void
66701ae08745Sheppo vds_remove_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
66711ae08745Sheppo {
66721ae08745Sheppo 	uint64_t	id = 0;
66731ae08745Sheppo 
66741ae08745Sheppo 
66751ae08745Sheppo 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
66761ae08745Sheppo 		PRN("Unable to get \"%s\" property from vdisk's MD node",
66771ae08745Sheppo 		    VD_ID_PROP);
66781ae08745Sheppo 		return;
66791ae08745Sheppo 	}
66801ae08745Sheppo 	PR0("Removing vdisk ID %lu", id);
66811ae08745Sheppo 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0)
66821ae08745Sheppo 		PRN("No vdisk entry found for vdisk ID %lu", id);
66831ae08745Sheppo }
66841ae08745Sheppo 
66851ae08745Sheppo static void
66861ae08745Sheppo vds_change_vd(vds_t *vds, md_t *prev_md, mde_cookie_t prev_vd_node,
66871ae08745Sheppo     md_t *curr_md, mde_cookie_t curr_vd_node)
66881ae08745Sheppo {
66891ae08745Sheppo 	char		*curr_dev, *prev_dev;
6690047ba61eSachartre 	uint64_t	curr_id = 0, curr_ldc_id = 0, curr_options = 0;
6691047ba61eSachartre 	uint64_t	prev_id = 0, prev_ldc_id = 0, prev_options = 0;
66921ae08745Sheppo 	size_t		len;
66931ae08745Sheppo 
66941ae08745Sheppo 
66951ae08745Sheppo 	/* Validate that vdisk ID has not changed */
66961ae08745Sheppo 	if (md_get_prop_val(prev_md, prev_vd_node, VD_ID_PROP, &prev_id) != 0) {
66971ae08745Sheppo 		PRN("Error getting previous vdisk \"%s\" property",
66981ae08745Sheppo 		    VD_ID_PROP);
66991ae08745Sheppo 		return;
67001ae08745Sheppo 	}
67011ae08745Sheppo 	if (md_get_prop_val(curr_md, curr_vd_node, VD_ID_PROP, &curr_id) != 0) {
67021ae08745Sheppo 		PRN("Error getting current vdisk \"%s\" property", VD_ID_PROP);
67031ae08745Sheppo 		return;
67041ae08745Sheppo 	}
67051ae08745Sheppo 	if (curr_id != prev_id) {
67061ae08745Sheppo 		PRN("Not changing vdisk:  ID changed from %lu to %lu",
67071ae08745Sheppo 		    prev_id, curr_id);
67081ae08745Sheppo 		return;
67091ae08745Sheppo 	}
67101ae08745Sheppo 
67111ae08745Sheppo 	/* Validate that LDC ID has not changed */
67121ae08745Sheppo 	if (vds_get_ldc_id(prev_md, prev_vd_node, &prev_ldc_id) != 0) {
67131ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", prev_id);
67141ae08745Sheppo 		return;
67151ae08745Sheppo 	}
67161ae08745Sheppo 
67171ae08745Sheppo 	if (vds_get_ldc_id(curr_md, curr_vd_node, &curr_ldc_id) != 0) {
67181ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", curr_id);
67191ae08745Sheppo 		return;
67201ae08745Sheppo 	}
67211ae08745Sheppo 	if (curr_ldc_id != prev_ldc_id) {
67220a55fbb7Slm66018 		_NOTE(NOTREACHED);	/* lint is confused */
67231ae08745Sheppo 		PRN("Not changing vdisk:  "
67241ae08745Sheppo 		    "LDC ID changed from %lu to %lu", prev_ldc_id, curr_ldc_id);
67251ae08745Sheppo 		return;
67261ae08745Sheppo 	}
67271ae08745Sheppo 
67281ae08745Sheppo 	/* Determine whether device path has changed */
67291ae08745Sheppo 	if (md_get_prop_str(prev_md, prev_vd_node, VD_BLOCK_DEVICE_PROP,
67301ae08745Sheppo 	    &prev_dev) != 0) {
67311ae08745Sheppo 		PRN("Error getting previous vdisk \"%s\"",
67321ae08745Sheppo 		    VD_BLOCK_DEVICE_PROP);
67331ae08745Sheppo 		return;
67341ae08745Sheppo 	}
67351ae08745Sheppo 	if (md_get_prop_str(curr_md, curr_vd_node, VD_BLOCK_DEVICE_PROP,
67361ae08745Sheppo 	    &curr_dev) != 0) {
67371ae08745Sheppo 		PRN("Error getting current vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
67381ae08745Sheppo 		return;
67391ae08745Sheppo 	}
67401ae08745Sheppo 	if (((len = strlen(curr_dev)) == strlen(prev_dev)) &&
67411ae08745Sheppo 	    (strncmp(curr_dev, prev_dev, len) == 0))
67421ae08745Sheppo 		return;	/* no relevant (supported) change */
67431ae08745Sheppo 
6744047ba61eSachartre 	/* Validate that options have not changed */
6745047ba61eSachartre 	vds_get_options(prev_md, prev_vd_node, &prev_options);
6746047ba61eSachartre 	vds_get_options(curr_md, curr_vd_node, &curr_options);
6747047ba61eSachartre 	if (prev_options != curr_options) {
6748047ba61eSachartre 		PRN("Not changing vdisk:  options changed from %lx to %lx",
6749047ba61eSachartre 		    prev_options, curr_options);
6750047ba61eSachartre 		return;
6751047ba61eSachartre 	}
6752047ba61eSachartre 
67531ae08745Sheppo 	PR0("Changing vdisk ID %lu", prev_id);
67543af08d82Slm66018 
67551ae08745Sheppo 	/* Remove old state, which will close vdisk and reset */
67561ae08745Sheppo 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)prev_id) != 0)
67571ae08745Sheppo 		PRN("No entry found for vdisk ID %lu", prev_id);
67583af08d82Slm66018 
67591ae08745Sheppo 	/* Re-initialize vdisk with new state */
6760047ba61eSachartre 	if (vds_init_vd(vds, curr_id, curr_dev, curr_options,
6761047ba61eSachartre 	    curr_ldc_id) != 0) {
67621ae08745Sheppo 		PRN("Failed to change vdisk ID %lu", curr_id);
67631ae08745Sheppo 		return;
67641ae08745Sheppo 	}
67651ae08745Sheppo }
67661ae08745Sheppo 
67671ae08745Sheppo static int
67681ae08745Sheppo vds_process_md(void *arg, mdeg_result_t *md)
67691ae08745Sheppo {
67701ae08745Sheppo 	int	i;
67711ae08745Sheppo 	vds_t	*vds = arg;
67721ae08745Sheppo 
67731ae08745Sheppo 
67741ae08745Sheppo 	if (md == NULL)
67751ae08745Sheppo 		return (MDEG_FAILURE);
67761ae08745Sheppo 	ASSERT(vds != NULL);
67771ae08745Sheppo 
67781ae08745Sheppo 	for (i = 0; i < md->removed.nelem; i++)
67791ae08745Sheppo 		vds_remove_vd(vds, md->removed.mdp, md->removed.mdep[i]);
67801ae08745Sheppo 	for (i = 0; i < md->match_curr.nelem; i++)
67811ae08745Sheppo 		vds_change_vd(vds, md->match_prev.mdp, md->match_prev.mdep[i],
67821ae08745Sheppo 		    md->match_curr.mdp, md->match_curr.mdep[i]);
67831ae08745Sheppo 	for (i = 0; i < md->added.nelem; i++)
67841ae08745Sheppo 		vds_add_vd(vds, md->added.mdp, md->added.mdep[i]);
67851ae08745Sheppo 
67861ae08745Sheppo 	return (MDEG_SUCCESS);
67871ae08745Sheppo }
67881ae08745Sheppo 
67893c96341aSnarayan 
67901ae08745Sheppo static int
67911ae08745Sheppo vds_do_attach(dev_info_t *dip)
67921ae08745Sheppo {
6793445b4c2eSsb155480 	int			status, sz;
6794445b4c2eSsb155480 	int			cfg_handle;
67951ae08745Sheppo 	minor_t			instance = ddi_get_instance(dip);
67961ae08745Sheppo 	vds_t			*vds;
6797445b4c2eSsb155480 	mdeg_prop_spec_t	*pspecp;
6798445b4c2eSsb155480 	mdeg_node_spec_t	*ispecp;
67991ae08745Sheppo 
68001ae08745Sheppo 	/*
68011ae08745Sheppo 	 * The "cfg-handle" property of a vds node in an MD contains the MD's
68021ae08745Sheppo 	 * notion of "instance", or unique identifier, for that node; OBP
68031ae08745Sheppo 	 * stores the value of the "cfg-handle" MD property as the value of
68041ae08745Sheppo 	 * the "reg" property on the node in the device tree it builds from
68051ae08745Sheppo 	 * the MD and passes to Solaris.  Thus, we look up the devinfo node's
68061ae08745Sheppo 	 * "reg" property value to uniquely identify this device instance when
68071ae08745Sheppo 	 * registering with the MD event-generation framework.  If the "reg"
68081ae08745Sheppo 	 * property cannot be found, the device tree state is presumably so
68091ae08745Sheppo 	 * broken that there is no point in continuing.
68101ae08745Sheppo 	 */
6811445b4c2eSsb155480 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
6812445b4c2eSsb155480 	    VD_REG_PROP)) {
6813445b4c2eSsb155480 		PRN("vds \"%s\" property does not exist", VD_REG_PROP);
68141ae08745Sheppo 		return (DDI_FAILURE);
68151ae08745Sheppo 	}
68161ae08745Sheppo 
68171ae08745Sheppo 	/* Get the MD instance for later MDEG registration */
68181ae08745Sheppo 	cfg_handle = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
6819445b4c2eSsb155480 	    VD_REG_PROP, -1);
68201ae08745Sheppo 
68211ae08745Sheppo 	if (ddi_soft_state_zalloc(vds_state, instance) != DDI_SUCCESS) {
68221ae08745Sheppo 		PRN("Could not allocate state for instance %u", instance);
68231ae08745Sheppo 		return (DDI_FAILURE);
68241ae08745Sheppo 	}
68251ae08745Sheppo 
68261ae08745Sheppo 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
68271ae08745Sheppo 		PRN("Could not get state for instance %u", instance);
68281ae08745Sheppo 		ddi_soft_state_free(vds_state, instance);
68291ae08745Sheppo 		return (DDI_FAILURE);
68301ae08745Sheppo 	}
68311ae08745Sheppo 
68321ae08745Sheppo 	vds->dip	= dip;
68331ae08745Sheppo 	vds->vd_table	= mod_hash_create_ptrhash("vds_vd_table", VDS_NCHAINS,
683487a7269eSachartre 	    vds_destroy_vd, sizeof (void *));
683587a7269eSachartre 
68361ae08745Sheppo 	ASSERT(vds->vd_table != NULL);
68371ae08745Sheppo 
68381ae08745Sheppo 	if ((status = ldi_ident_from_dip(dip, &vds->ldi_ident)) != 0) {
68391ae08745Sheppo 		PRN("ldi_ident_from_dip() returned errno %d", status);
68401ae08745Sheppo 		return (DDI_FAILURE);
68411ae08745Sheppo 	}
68421ae08745Sheppo 	vds->initialized |= VDS_LDI;
68431ae08745Sheppo 
68441ae08745Sheppo 	/* Register for MD updates */
6845445b4c2eSsb155480 	sz = sizeof (vds_prop_template);
6846445b4c2eSsb155480 	pspecp = kmem_alloc(sz, KM_SLEEP);
6847445b4c2eSsb155480 	bcopy(vds_prop_template, pspecp, sz);
6848445b4c2eSsb155480 
6849445b4c2eSsb155480 	VDS_SET_MDEG_PROP_INST(pspecp, cfg_handle);
6850445b4c2eSsb155480 
6851445b4c2eSsb155480 	/* initialize the complete prop spec structure */
6852445b4c2eSsb155480 	ispecp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP);
6853445b4c2eSsb155480 	ispecp->namep = "virtual-device";
6854445b4c2eSsb155480 	ispecp->specp = pspecp;
6855445b4c2eSsb155480 
6856445b4c2eSsb155480 	if (mdeg_register(ispecp, &vd_match, vds_process_md, vds,
68571ae08745Sheppo 	    &vds->mdeg) != MDEG_SUCCESS) {
68581ae08745Sheppo 		PRN("Unable to register for MD updates");
6859445b4c2eSsb155480 		kmem_free(ispecp, sizeof (mdeg_node_spec_t));
6860445b4c2eSsb155480 		kmem_free(pspecp, sz);
68611ae08745Sheppo 		return (DDI_FAILURE);
68621ae08745Sheppo 	}
6863445b4c2eSsb155480 
6864445b4c2eSsb155480 	vds->ispecp = ispecp;
68651ae08745Sheppo 	vds->initialized |= VDS_MDEG;
68661ae08745Sheppo 
68670a55fbb7Slm66018 	/* Prevent auto-detaching so driver is available whenever MD changes */
68680a55fbb7Slm66018 	if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1) !=
68690a55fbb7Slm66018 	    DDI_PROP_SUCCESS) {
68700a55fbb7Slm66018 		PRN("failed to set \"%s\" property for instance %u",
68710a55fbb7Slm66018 		    DDI_NO_AUTODETACH, instance);
68720a55fbb7Slm66018 	}
68730a55fbb7Slm66018 
68748fce2fd6Sachartre 	/* read any user defined driver types from conf file and update list */
68758fce2fd6Sachartre 	vds_driver_types_update(vds);
68768fce2fd6Sachartre 
68771ae08745Sheppo 	ddi_report_dev(dip);
68781ae08745Sheppo 	return (DDI_SUCCESS);
68791ae08745Sheppo }
68801ae08745Sheppo 
68811ae08745Sheppo static int
68821ae08745Sheppo vds_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
68831ae08745Sheppo {
68841ae08745Sheppo 	int	status;
68851ae08745Sheppo 
68861ae08745Sheppo 	switch (cmd) {
68871ae08745Sheppo 	case DDI_ATTACH:
6888d10e4ef2Snarayan 		PR0("Attaching");
68891ae08745Sheppo 		if ((status = vds_do_attach(dip)) != DDI_SUCCESS)
68901ae08745Sheppo 			(void) vds_detach(dip, DDI_DETACH);
68911ae08745Sheppo 		return (status);
68921ae08745Sheppo 	case DDI_RESUME:
6893d10e4ef2Snarayan 		PR0("No action required for DDI_RESUME");
68941ae08745Sheppo 		return (DDI_SUCCESS);
68951ae08745Sheppo 	default:
68961ae08745Sheppo 		return (DDI_FAILURE);
68971ae08745Sheppo 	}
68981ae08745Sheppo }
68991ae08745Sheppo 
69001ae08745Sheppo static struct dev_ops vds_ops = {
69011ae08745Sheppo 	DEVO_REV,	/* devo_rev */
69021ae08745Sheppo 	0,		/* devo_refcnt */
69031ae08745Sheppo 	ddi_no_info,	/* devo_getinfo */
69041ae08745Sheppo 	nulldev,	/* devo_identify */
69051ae08745Sheppo 	nulldev,	/* devo_probe */
69061ae08745Sheppo 	vds_attach,	/* devo_attach */
69071ae08745Sheppo 	vds_detach,	/* devo_detach */
69081ae08745Sheppo 	nodev,		/* devo_reset */
69091ae08745Sheppo 	NULL,		/* devo_cb_ops */
69101ae08745Sheppo 	NULL,		/* devo_bus_ops */
69111ae08745Sheppo 	nulldev		/* devo_power */
69121ae08745Sheppo };
69131ae08745Sheppo 
69141ae08745Sheppo static struct modldrv modldrv = {
69151ae08745Sheppo 	&mod_driverops,
6916205eeb1aSlm66018 	"virtual disk server",
69171ae08745Sheppo 	&vds_ops,
69181ae08745Sheppo };
69191ae08745Sheppo 
69201ae08745Sheppo static struct modlinkage modlinkage = {
69211ae08745Sheppo 	MODREV_1,
69221ae08745Sheppo 	&modldrv,
69231ae08745Sheppo 	NULL
69241ae08745Sheppo };
69251ae08745Sheppo 
69261ae08745Sheppo 
69271ae08745Sheppo int
69281ae08745Sheppo _init(void)
69291ae08745Sheppo {
693017cadca8Slm66018 	int		status;
6931d10e4ef2Snarayan 
69321ae08745Sheppo 	if ((status = ddi_soft_state_init(&vds_state, sizeof (vds_t), 1)) != 0)
69331ae08745Sheppo 		return (status);
693417cadca8Slm66018 
69351ae08745Sheppo 	if ((status = mod_install(&modlinkage)) != 0) {
69361ae08745Sheppo 		ddi_soft_state_fini(&vds_state);
69371ae08745Sheppo 		return (status);
69381ae08745Sheppo 	}
69391ae08745Sheppo 
69401ae08745Sheppo 	return (0);
69411ae08745Sheppo }
69421ae08745Sheppo 
69431ae08745Sheppo int
69441ae08745Sheppo _info(struct modinfo *modinfop)
69451ae08745Sheppo {
69461ae08745Sheppo 	return (mod_info(&modlinkage, modinfop));
69471ae08745Sheppo }
69481ae08745Sheppo 
69491ae08745Sheppo int
69501ae08745Sheppo _fini(void)
69511ae08745Sheppo {
69521ae08745Sheppo 	int	status;
69531ae08745Sheppo 
69541ae08745Sheppo 	if ((status = mod_remove(&modlinkage)) != 0)
69551ae08745Sheppo 		return (status);
69561ae08745Sheppo 	ddi_soft_state_fini(&vds_state);
69571ae08745Sheppo 	return (0);
69581ae08745Sheppo }
6959