xref: /titanic_53/usr/src/uts/sun4v/io/vds.c (revision 1aff8f07c7bb7f79d8c2363db21c57e53d5df2da)
11ae08745Sheppo /*
21ae08745Sheppo  * CDDL HEADER START
31ae08745Sheppo  *
41ae08745Sheppo  * The contents of this file are subject to the terms of the
51ae08745Sheppo  * Common Development and Distribution License (the "License").
61ae08745Sheppo  * You may not use this file except in compliance with the License.
71ae08745Sheppo  *
81ae08745Sheppo  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91ae08745Sheppo  * or http://www.opensolaris.org/os/licensing.
101ae08745Sheppo  * See the License for the specific language governing permissions
111ae08745Sheppo  * and limitations under the License.
121ae08745Sheppo  *
131ae08745Sheppo  * When distributing Covered Code, include this CDDL HEADER in each
141ae08745Sheppo  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151ae08745Sheppo  * If applicable, add the following below this CDDL HEADER, with the
161ae08745Sheppo  * fields enclosed by brackets "[]" replaced with your own identifying
171ae08745Sheppo  * information: Portions Copyright [yyyy] [name of copyright owner]
181ae08745Sheppo  *
191ae08745Sheppo  * CDDL HEADER END
201ae08745Sheppo  */
211ae08745Sheppo 
221ae08745Sheppo /*
23edcc0754Sachartre  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
241ae08745Sheppo  * Use is subject to license terms.
251ae08745Sheppo  */
261ae08745Sheppo 
271ae08745Sheppo /*
281ae08745Sheppo  * Virtual disk server
291ae08745Sheppo  */
301ae08745Sheppo 
311ae08745Sheppo 
321ae08745Sheppo #include <sys/types.h>
331ae08745Sheppo #include <sys/conf.h>
344bac2208Snarayan #include <sys/crc32.h>
351ae08745Sheppo #include <sys/ddi.h>
361ae08745Sheppo #include <sys/dkio.h>
371ae08745Sheppo #include <sys/file.h>
3817cadca8Slm66018 #include <sys/fs/hsfs_isospec.h>
391ae08745Sheppo #include <sys/mdeg.h>
402f5224aeSachartre #include <sys/mhd.h>
411ae08745Sheppo #include <sys/modhash.h>
421ae08745Sheppo #include <sys/note.h>
431ae08745Sheppo #include <sys/pathname.h>
44205eeb1aSlm66018 #include <sys/sdt.h>
451ae08745Sheppo #include <sys/sunddi.h>
461ae08745Sheppo #include <sys/sunldi.h>
471ae08745Sheppo #include <sys/sysmacros.h>
481ae08745Sheppo #include <sys/vio_common.h>
4917cadca8Slm66018 #include <sys/vio_util.h>
501ae08745Sheppo #include <sys/vdsk_mailbox.h>
511ae08745Sheppo #include <sys/vdsk_common.h>
521ae08745Sheppo #include <sys/vtoc.h>
533c96341aSnarayan #include <sys/vfs.h>
543c96341aSnarayan #include <sys/stat.h>
5587a7269eSachartre #include <sys/scsi/impl/uscsi.h>
56bbfa0259Sha137994 #include <sys/ontrap.h>
57690555a1Sachartre #include <vm/seg_map.h>
581ae08745Sheppo 
59342440ecSPrasad Singamsetty #define	ONE_MEGABYTE	(1ULL << 20)
60342440ecSPrasad Singamsetty #define	ONE_GIGABYTE	(1ULL << 30)
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 */
99*1aff8f07SAlexandre Chartre #define	VD_DSKIMG_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 
198*1aff8f07SAlexandre Chartre /* Read disk label from a disk image */
199*1aff8f07SAlexandre Chartre #define	VD_DSKIMG_LABEL_READ(vd, labelp) \
200*1aff8f07SAlexandre Chartre 	vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)labelp, \
201690555a1Sachartre 	    0, sizeof (struct dk_label))
202690555a1Sachartre 
203*1aff8f07SAlexandre Chartre /* Write disk label to a disk image */
204*1aff8f07SAlexandre Chartre #define	VD_DSKIMG_LABEL_WRITE(vd, labelp)	\
205*1aff8f07SAlexandre Chartre 	vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)labelp, \
206690555a1Sachartre 	    0, sizeof (struct dk_label))
207690555a1Sachartre 
208*1aff8f07SAlexandre Chartre /* Identify if a backend is a disk image */
209*1aff8f07SAlexandre Chartre #define	VD_DSKIMG(vd)	((vd)->vdisk_type == VD_DISK_TYPE_DISK &&	\
210*1aff8f07SAlexandre Chartre 	((vd)->file || (vd)->volume))
211*1aff8f07SAlexandre Chartre 
2122f5224aeSachartre /* Message for disk access rights reset failure */
2132f5224aeSachartre #define	VD_RESET_ACCESS_FAILURE_MSG \
2142f5224aeSachartre 	"Fail to reset disk access rights for disk %s"
2152f5224aeSachartre 
216445b4c2eSsb155480 /*
217445b4c2eSsb155480  * Specification of an MD node passed to the MDEG to filter any
218445b4c2eSsb155480  * 'vport' nodes that do not belong to the specified node. This
219445b4c2eSsb155480  * template is copied for each vds instance and filled in with
220445b4c2eSsb155480  * the appropriate 'cfg-handle' value before being passed to the MDEG.
221445b4c2eSsb155480  */
222445b4c2eSsb155480 static mdeg_prop_spec_t	vds_prop_template[] = {
223445b4c2eSsb155480 	{ MDET_PROP_STR,	"name",		VDS_NAME },
224445b4c2eSsb155480 	{ MDET_PROP_VAL,	"cfg-handle",	NULL },
225445b4c2eSsb155480 	{ MDET_LIST_END,	NULL, 		NULL }
226445b4c2eSsb155480 };
227445b4c2eSsb155480 
228445b4c2eSsb155480 #define	VDS_SET_MDEG_PROP_INST(specp, val) (specp)[1].ps_val = (val);
229445b4c2eSsb155480 
230445b4c2eSsb155480 /*
231445b4c2eSsb155480  * Matching criteria passed to the MDEG to register interest
232445b4c2eSsb155480  * in changes to 'virtual-device-port' nodes identified by their
233445b4c2eSsb155480  * 'id' property.
234445b4c2eSsb155480  */
235445b4c2eSsb155480 static md_prop_match_t	vd_prop_match[] = {
236445b4c2eSsb155480 	{ MDET_PROP_VAL,	VD_ID_PROP },
237445b4c2eSsb155480 	{ MDET_LIST_END,	NULL }
238445b4c2eSsb155480 };
239445b4c2eSsb155480 
240445b4c2eSsb155480 static mdeg_node_match_t vd_match = {"virtual-device-port",
241445b4c2eSsb155480 				    vd_prop_match};
242445b4c2eSsb155480 
243047ba61eSachartre /*
244047ba61eSachartre  * Options for the VD_BLOCK_DEVICE_OPTS property.
245047ba61eSachartre  */
246047ba61eSachartre #define	VD_OPT_RDONLY		0x1	/* read-only  */
247047ba61eSachartre #define	VD_OPT_SLICE		0x2	/* single slice */
248047ba61eSachartre #define	VD_OPT_EXCLUSIVE	0x4	/* exclusive access */
249047ba61eSachartre 
250047ba61eSachartre #define	VD_OPTION_NLEN	128
251047ba61eSachartre 
252047ba61eSachartre typedef struct vd_option {
253047ba61eSachartre 	char vdo_name[VD_OPTION_NLEN];
254047ba61eSachartre 	uint64_t vdo_value;
255047ba61eSachartre } vd_option_t;
256047ba61eSachartre 
257047ba61eSachartre vd_option_t vd_bdev_options[] = {
258047ba61eSachartre 	{ "ro",		VD_OPT_RDONLY },
259047ba61eSachartre 	{ "slice", 	VD_OPT_SLICE },
260047ba61eSachartre 	{ "excl",	VD_OPT_EXCLUSIVE }
261047ba61eSachartre };
262047ba61eSachartre 
2631ae08745Sheppo /* Debugging macros */
2641ae08745Sheppo #ifdef DEBUG
2653af08d82Slm66018 
2663af08d82Slm66018 static int	vd_msglevel = 0;
2673af08d82Slm66018 
2681ae08745Sheppo #define	PR0 if (vd_msglevel > 0)	PRN
2691ae08745Sheppo #define	PR1 if (vd_msglevel > 1)	PRN
2701ae08745Sheppo #define	PR2 if (vd_msglevel > 2)	PRN
2711ae08745Sheppo 
2721ae08745Sheppo #define	VD_DUMP_DRING_ELEM(elem)					\
2733c96341aSnarayan 	PR0("dst:%x op:%x st:%u nb:%lx addr:%lx ncook:%u\n",		\
2741ae08745Sheppo 	    elem->hdr.dstate,						\
2751ae08745Sheppo 	    elem->payload.operation,					\
2761ae08745Sheppo 	    elem->payload.status,					\
2771ae08745Sheppo 	    elem->payload.nbytes,					\
2781ae08745Sheppo 	    elem->payload.addr,						\
2791ae08745Sheppo 	    elem->payload.ncookies);
2801ae08745Sheppo 
2813af08d82Slm66018 char *
2823af08d82Slm66018 vd_decode_state(int state)
2833af08d82Slm66018 {
2843af08d82Slm66018 	char *str;
2853af08d82Slm66018 
2863af08d82Slm66018 #define	CASE_STATE(_s)	case _s: str = #_s; break;
2873af08d82Slm66018 
2883af08d82Slm66018 	switch (state) {
2893af08d82Slm66018 	CASE_STATE(VD_STATE_INIT)
2903af08d82Slm66018 	CASE_STATE(VD_STATE_VER)
2913af08d82Slm66018 	CASE_STATE(VD_STATE_ATTR)
2923af08d82Slm66018 	CASE_STATE(VD_STATE_DRING)
2933af08d82Slm66018 	CASE_STATE(VD_STATE_RDX)
2943af08d82Slm66018 	CASE_STATE(VD_STATE_DATA)
2953af08d82Slm66018 	default: str = "unknown"; break;
2963af08d82Slm66018 	}
2973af08d82Slm66018 
2983af08d82Slm66018 #undef CASE_STATE
2993af08d82Slm66018 
3003af08d82Slm66018 	return (str);
3013af08d82Slm66018 }
3023af08d82Slm66018 
3033af08d82Slm66018 void
3043af08d82Slm66018 vd_decode_tag(vio_msg_t *msg)
3053af08d82Slm66018 {
3063af08d82Slm66018 	char *tstr, *sstr, *estr;
3073af08d82Slm66018 
3083af08d82Slm66018 #define	CASE_TYPE(_s)	case _s: tstr = #_s; break;
3093af08d82Slm66018 
3103af08d82Slm66018 	switch (msg->tag.vio_msgtype) {
3113af08d82Slm66018 	CASE_TYPE(VIO_TYPE_CTRL)
3123af08d82Slm66018 	CASE_TYPE(VIO_TYPE_DATA)
3133af08d82Slm66018 	CASE_TYPE(VIO_TYPE_ERR)
3143af08d82Slm66018 	default: tstr = "unknown"; break;
3153af08d82Slm66018 	}
3163af08d82Slm66018 
3173af08d82Slm66018 #undef CASE_TYPE
3183af08d82Slm66018 
3193af08d82Slm66018 #define	CASE_SUBTYPE(_s) case _s: sstr = #_s; break;
3203af08d82Slm66018 
3213af08d82Slm66018 	switch (msg->tag.vio_subtype) {
3223af08d82Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_INFO)
3233af08d82Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_ACK)
3243af08d82Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_NACK)
3253af08d82Slm66018 	default: sstr = "unknown"; break;
3263af08d82Slm66018 	}
3273af08d82Slm66018 
3283af08d82Slm66018 #undef CASE_SUBTYPE
3293af08d82Slm66018 
3303af08d82Slm66018 #define	CASE_ENV(_s)	case _s: estr = #_s; break;
3313af08d82Slm66018 
3323af08d82Slm66018 	switch (msg->tag.vio_subtype_env) {
3333af08d82Slm66018 	CASE_ENV(VIO_VER_INFO)
3343af08d82Slm66018 	CASE_ENV(VIO_ATTR_INFO)
3353af08d82Slm66018 	CASE_ENV(VIO_DRING_REG)
3363af08d82Slm66018 	CASE_ENV(VIO_DRING_UNREG)
3373af08d82Slm66018 	CASE_ENV(VIO_RDX)
3383af08d82Slm66018 	CASE_ENV(VIO_PKT_DATA)
3393af08d82Slm66018 	CASE_ENV(VIO_DESC_DATA)
3403af08d82Slm66018 	CASE_ENV(VIO_DRING_DATA)
3413af08d82Slm66018 	default: estr = "unknown"; break;
3423af08d82Slm66018 	}
3433af08d82Slm66018 
3443af08d82Slm66018 #undef CASE_ENV
3453af08d82Slm66018 
3463af08d82Slm66018 	PR1("(%x/%x/%x) message : (%s/%s/%s)",
3473af08d82Slm66018 	    msg->tag.vio_msgtype, msg->tag.vio_subtype,
3483af08d82Slm66018 	    msg->tag.vio_subtype_env, tstr, sstr, estr);
3493af08d82Slm66018 }
3503af08d82Slm66018 
3511ae08745Sheppo #else	/* !DEBUG */
3523af08d82Slm66018 
3531ae08745Sheppo #define	PR0(...)
3541ae08745Sheppo #define	PR1(...)
3551ae08745Sheppo #define	PR2(...)
3561ae08745Sheppo 
3571ae08745Sheppo #define	VD_DUMP_DRING_ELEM(elem)
3581ae08745Sheppo 
3593af08d82Slm66018 #define	vd_decode_state(_s)	(NULL)
3603af08d82Slm66018 #define	vd_decode_tag(_s)	(NULL)
3613af08d82Slm66018 
3621ae08745Sheppo #endif	/* DEBUG */
3631ae08745Sheppo 
3641ae08745Sheppo 
365d10e4ef2Snarayan /*
366d10e4ef2Snarayan  * Soft state structure for a vds instance
367d10e4ef2Snarayan  */
3681ae08745Sheppo typedef struct vds {
3691ae08745Sheppo 	uint_t		initialized;	/* driver inst initialization flags */
3701ae08745Sheppo 	dev_info_t	*dip;		/* driver inst devinfo pointer */
3711ae08745Sheppo 	ldi_ident_t	ldi_ident;	/* driver's identifier for LDI */
3721ae08745Sheppo 	mod_hash_t	*vd_table;	/* table of virtual disks served */
373445b4c2eSsb155480 	mdeg_node_spec_t *ispecp;	/* mdeg node specification */
3741ae08745Sheppo 	mdeg_handle_t	mdeg;		/* handle for MDEG operations  */
3758fce2fd6Sachartre 	vd_driver_type_t *driver_types;	/* extra driver types (from vds.conf) */
3768fce2fd6Sachartre 	int 		num_drivers;	/* num of extra driver types */
3771ae08745Sheppo } vds_t;
3781ae08745Sheppo 
379d10e4ef2Snarayan /*
380d10e4ef2Snarayan  * Types of descriptor-processing tasks
381d10e4ef2Snarayan  */
382d10e4ef2Snarayan typedef enum vd_task_type {
383d10e4ef2Snarayan 	VD_NONFINAL_RANGE_TASK,	/* task for intermediate descriptor in range */
384d10e4ef2Snarayan 	VD_FINAL_RANGE_TASK,	/* task for last in a range of descriptors */
385d10e4ef2Snarayan } vd_task_type_t;
386d10e4ef2Snarayan 
387d10e4ef2Snarayan /*
388d10e4ef2Snarayan  * Structure describing the task for processing a descriptor
389d10e4ef2Snarayan  */
390d10e4ef2Snarayan typedef struct vd_task {
391d10e4ef2Snarayan 	struct vd		*vd;		/* vd instance task is for */
392d10e4ef2Snarayan 	vd_task_type_t		type;		/* type of descriptor task */
393d10e4ef2Snarayan 	int			index;		/* dring elem index for task */
394d10e4ef2Snarayan 	vio_msg_t		*msg;		/* VIO message task is for */
395d10e4ef2Snarayan 	size_t			msglen;		/* length of message content */
396d10e4ef2Snarayan 	vd_dring_payload_t	*request;	/* request task will perform */
397d10e4ef2Snarayan 	struct buf		buf;		/* buf(9s) for I/O request */
3984bac2208Snarayan 	ldc_mem_handle_t	mhdl;		/* task memory handle */
399205eeb1aSlm66018 	int			status;		/* status of processing task */
400205eeb1aSlm66018 	int	(*completef)(struct vd_task *task); /* completion func ptr */
401d10e4ef2Snarayan } vd_task_t;
402d10e4ef2Snarayan 
403d10e4ef2Snarayan /*
404d10e4ef2Snarayan  * Soft state structure for a virtual disk instance
405d10e4ef2Snarayan  */
4061ae08745Sheppo typedef struct vd {
4071ae08745Sheppo 	uint_t			initialized;	/* vdisk initialization flags */
40817cadca8Slm66018 	uint64_t		operations;	/* bitmask of VD_OPs exported */
40917cadca8Slm66018 	vio_ver_t		version;	/* ver negotiated with client */
4101ae08745Sheppo 	vds_t			*vds;		/* server for this vdisk */
411d10e4ef2Snarayan 	ddi_taskq_t		*startq;	/* queue for I/O start tasks */
412d10e4ef2Snarayan 	ddi_taskq_t		*completionq;	/* queue for completion tasks */
4131ae08745Sheppo 	ldi_handle_t		ldi_handle[V_NUMPAR];	/* LDI slice handles */
4143c96341aSnarayan 	char			device_path[MAXPATHLEN + 1]; /* vdisk device */
4151ae08745Sheppo 	dev_t			dev[V_NUMPAR];	/* dev numbers for slices */
416047ba61eSachartre 	int			open_flags;	/* open flags */
417bae9e67eSachartre 	uint_t			nslices;	/* number of slices we export */
4181ae08745Sheppo 	size_t			vdisk_size;	/* number of blocks in vdisk */
41917cadca8Slm66018 	size_t			vdisk_block_size; /* size of each vdisk block */
4201ae08745Sheppo 	vd_disk_type_t		vdisk_type;	/* slice or entire disk */
4214bac2208Snarayan 	vd_disk_label_t		vdisk_label;	/* EFI or VTOC label */
42217cadca8Slm66018 	vd_media_t		vdisk_media;	/* media type of backing dev. */
42317cadca8Slm66018 	boolean_t		is_atapi_dev;	/* Is this an IDE CD-ROM dev? */
424e1ebb9ecSlm66018 	ushort_t		max_xfer_sz;	/* max xfer size in DEV_BSIZE */
42517cadca8Slm66018 	size_t			block_size;	/* blk size of actual device */
4268fce2fd6Sachartre 	boolean_t		volume;		/* is vDisk backed by volume */
427*1aff8f07SAlexandre Chartre 	boolean_t		zvol;		/* is vDisk backed by a zvol */
42817cadca8Slm66018 	boolean_t		file;		/* is vDisk backed by a file? */
4292f5224aeSachartre 	boolean_t		scsi;		/* is vDisk backed by scsi? */
4303c96341aSnarayan 	vnode_t			*file_vnode;	/* file vnode */
431*1aff8f07SAlexandre Chartre 	size_t			dskimg_size;	/* size of disk image */
432*1aff8f07SAlexandre Chartre 	ddi_devid_t		dskimg_devid;	/* devid for disk image */
433edcc0754Sachartre 	int			efi_reserved;	/* EFI reserved slice */
434bae9e67eSachartre 	caddr_t			flabel;		/* fake label for slice type */
435bae9e67eSachartre 	uint_t			flabel_size;	/* fake label size */
436bae9e67eSachartre 	uint_t			flabel_limit;	/* limit of the fake label */
4371ae08745Sheppo 	struct dk_geom		dk_geom;	/* synthetic for slice type */
438342440ecSPrasad Singamsetty 	struct extvtoc		vtoc;		/* synthetic for slice type */
439edcc0754Sachartre 	vd_slice_t		slices[VD_MAXPART]; /* logical partitions */
4402f5224aeSachartre 	boolean_t		ownership;	/* disk ownership status */
4411ae08745Sheppo 	ldc_status_t		ldc_state;	/* LDC connection state */
4421ae08745Sheppo 	ldc_handle_t		ldc_handle;	/* handle for LDC comm */
4431ae08745Sheppo 	size_t			max_msglen;	/* largest LDC message len */
4441ae08745Sheppo 	vd_state_t		state;		/* client handshake state */
4451ae08745Sheppo 	uint8_t			xfer_mode;	/* transfer mode with client */
4461ae08745Sheppo 	uint32_t		sid;		/* client's session ID */
4471ae08745Sheppo 	uint64_t		seq_num;	/* message sequence number */
4481ae08745Sheppo 	uint64_t		dring_ident;	/* identifier of dring */
4491ae08745Sheppo 	ldc_dring_handle_t	dring_handle;	/* handle for dring ops */
4501ae08745Sheppo 	uint32_t		descriptor_size;	/* num bytes in desc */
4511ae08745Sheppo 	uint32_t		dring_len;	/* number of dring elements */
452bbfa0259Sha137994 	uint8_t			dring_mtype;	/* dring mem map type */
4531ae08745Sheppo 	caddr_t			dring;		/* address of dring */
4543af08d82Slm66018 	caddr_t			vio_msgp;	/* vio msg staging buffer */
455d10e4ef2Snarayan 	vd_task_t		inband_task;	/* task for inband descriptor */
456d10e4ef2Snarayan 	vd_task_t		*dring_task;	/* tasks dring elements */
457d10e4ef2Snarayan 
458d10e4ef2Snarayan 	kmutex_t		lock;		/* protects variables below */
459d10e4ef2Snarayan 	boolean_t		enabled;	/* is vdisk enabled? */
460d10e4ef2Snarayan 	boolean_t		reset_state;	/* reset connection state? */
461d10e4ef2Snarayan 	boolean_t		reset_ldc;	/* reset LDC channel? */
4621ae08745Sheppo } vd_t;
4631ae08745Sheppo 
464bae9e67eSachartre /*
465bae9e67eSachartre  * Macros to manipulate the fake label (flabel) for single slice disks.
466bae9e67eSachartre  *
467bae9e67eSachartre  * If we fake a VTOC label then the fake label consists of only one block
468bae9e67eSachartre  * containing the VTOC label (struct dk_label).
469bae9e67eSachartre  *
470bae9e67eSachartre  * If we fake an EFI label then the fake label consists of a blank block
471bae9e67eSachartre  * followed by a GPT (efi_gpt_t) and a GPE (efi_gpe_t).
472bae9e67eSachartre  *
473bae9e67eSachartre  */
474bae9e67eSachartre #define	VD_LABEL_VTOC_SIZE					\
475bae9e67eSachartre 	P2ROUNDUP(sizeof (struct dk_label), DEV_BSIZE)
476bae9e67eSachartre 
477bae9e67eSachartre #define	VD_LABEL_EFI_SIZE					\
478bae9e67eSachartre 	P2ROUNDUP(DEV_BSIZE + sizeof (efi_gpt_t) + 		\
479bae9e67eSachartre 	    sizeof (efi_gpe_t) * VD_MAXPART, DEV_BSIZE)
480bae9e67eSachartre 
481bae9e67eSachartre #define	VD_LABEL_VTOC(vd)	\
482342440ecSPrasad Singamsetty 		((struct dk_label *)(void *)((vd)->flabel))
483bae9e67eSachartre 
484bae9e67eSachartre #define	VD_LABEL_EFI_GPT(vd)	\
485342440ecSPrasad Singamsetty 		((efi_gpt_t *)(void *)((vd)->flabel + DEV_BSIZE))
486bae9e67eSachartre #define	VD_LABEL_EFI_GPE(vd)	\
487342440ecSPrasad Singamsetty 		((efi_gpe_t *)(void *)((vd)->flabel + DEV_BSIZE + \
488342440ecSPrasad Singamsetty 		sizeof (efi_gpt_t)))
489bae9e67eSachartre 
490bae9e67eSachartre 
4911ae08745Sheppo typedef struct vds_operation {
4923af08d82Slm66018 	char	*namep;
4931ae08745Sheppo 	uint8_t	operation;
494d10e4ef2Snarayan 	int	(*start)(vd_task_t *task);
495205eeb1aSlm66018 	int	(*complete)(vd_task_t *task);
4961ae08745Sheppo } vds_operation_t;
4971ae08745Sheppo 
4980a55fbb7Slm66018 typedef struct vd_ioctl {
4990a55fbb7Slm66018 	uint8_t		operation;		/* vdisk operation */
5000a55fbb7Slm66018 	const char	*operation_name;	/* vdisk operation name */
5010a55fbb7Slm66018 	size_t		nbytes;			/* size of operation buffer */
5020a55fbb7Slm66018 	int		cmd;			/* corresponding ioctl cmd */
5030a55fbb7Slm66018 	const char	*cmd_name;		/* ioctl cmd name */
5040a55fbb7Slm66018 	void		*arg;			/* ioctl cmd argument */
5050a55fbb7Slm66018 	/* convert input vd_buf to output ioctl_arg */
5062f5224aeSachartre 	int		(*copyin)(void *vd_buf, size_t, void *ioctl_arg);
5070a55fbb7Slm66018 	/* convert input ioctl_arg to output vd_buf */
5080a55fbb7Slm66018 	void		(*copyout)(void *ioctl_arg, void *vd_buf);
509047ba61eSachartre 	/* write is true if the operation writes any data to the backend */
510047ba61eSachartre 	boolean_t	write;
5110a55fbb7Slm66018 } vd_ioctl_t;
5120a55fbb7Slm66018 
5130a55fbb7Slm66018 /* Define trivial copyin/copyout conversion function flag */
5142f5224aeSachartre #define	VD_IDENTITY_IN	((int (*)(void *, size_t, void *))-1)
5152f5224aeSachartre #define	VD_IDENTITY_OUT	((void (*)(void *, void *))-1)
5161ae08745Sheppo 
5171ae08745Sheppo 
5183c96341aSnarayan static int	vds_ldc_retries = VDS_RETRIES;
5193af08d82Slm66018 static int	vds_ldc_delay = VDS_LDC_DELAY;
5203c96341aSnarayan static int	vds_dev_retries = VDS_RETRIES;
5213c96341aSnarayan static int	vds_dev_delay = VDS_DEV_DELAY;
5221ae08745Sheppo static void	*vds_state;
5231ae08745Sheppo 
524eba0cb4eSachartre static uint_t	vd_file_write_flags = VD_FILE_WRITE_FLAGS;
525eba0cb4eSachartre 
52687a7269eSachartre static short	vd_scsi_rdwr_timeout = VD_SCSI_RDWR_TIMEOUT;
5272f5224aeSachartre static int	vd_scsi_debug = USCSI_SILENT;
5282f5224aeSachartre 
5292f5224aeSachartre /*
5302f5224aeSachartre  * Tunable to define the behavior of the service domain if the vdisk server
5312f5224aeSachartre  * fails to reset disk exclusive access when a LDC channel is reset. When a
5322f5224aeSachartre  * LDC channel is reset the vdisk server will try to reset disk exclusive
5332f5224aeSachartre  * access by releasing any SCSI-2 reservation or resetting the disk. If these
5342f5224aeSachartre  * actions fail then the default behavior (vd_reset_access_failure = 0) is to
5352f5224aeSachartre  * print a warning message. This default behavior can be changed by setting
5362f5224aeSachartre  * the vd_reset_access_failure variable to A_REBOOT (= 0x1) and that will
5372f5224aeSachartre  * cause the service domain to reboot, or A_DUMP (= 0x5) and that will cause
5382f5224aeSachartre  * the service domain to panic. In both cases, the reset of the service domain
5392f5224aeSachartre  * should trigger a reset SCSI buses and hopefully clear any SCSI-2 reservation.
5402f5224aeSachartre  */
5412f5224aeSachartre static int 	vd_reset_access_failure = 0;
5422f5224aeSachartre 
5432f5224aeSachartre /*
5442f5224aeSachartre  * Tunable for backward compatibility. When this variable is set to B_TRUE,
5452f5224aeSachartre  * all disk volumes (ZFS, SVM, VxvM volumes) will be exported as single
5462f5224aeSachartre  * slice disks whether or not they have the "slice" option set. This is
5472f5224aeSachartre  * to provide a simple backward compatibility mechanism when upgrading
5482f5224aeSachartre  * the vds driver and using a domain configuration created before the
5492f5224aeSachartre  * "slice" option was available.
5502f5224aeSachartre  */
5512f5224aeSachartre static boolean_t vd_volume_force_slice = B_FALSE;
55287a7269eSachartre 
5530a55fbb7Slm66018 /*
55466cfcfbeSachartre  * The label of disk images created with some earlier versions of the virtual
55566cfcfbeSachartre  * disk software is not entirely correct and have an incorrect v_sanity field
55666cfcfbeSachartre  * (usually 0) instead of VTOC_SANE. This creates a compatibility problem with
55766cfcfbeSachartre  * these images because we are now validating that the disk label (and the
55866cfcfbeSachartre  * sanity) is correct when a disk image is opened.
55966cfcfbeSachartre  *
56066cfcfbeSachartre  * This tunable is set to false to not validate the sanity field and ensure
56166cfcfbeSachartre  * compatibility. If the tunable is set to true, we will do a strict checking
56266cfcfbeSachartre  * of the sanity but this can create compatibility problems with old disk
56366cfcfbeSachartre  * images.
56466cfcfbeSachartre  */
565*1aff8f07SAlexandre Chartre static boolean_t vd_dskimg_validate_sanity = B_FALSE;
56666cfcfbeSachartre 
56766cfcfbeSachartre /*
568bbfa0259Sha137994  * Enables the use of LDC_DIRECT_MAP when mapping in imported descriptor rings.
569bbfa0259Sha137994  */
570bbfa0259Sha137994 static boolean_t vd_direct_mapped_drings = B_TRUE;
571bbfa0259Sha137994 
572bbfa0259Sha137994 /*
573bae9e67eSachartre  * When a backend is exported as a single-slice disk then we entirely fake
574bae9e67eSachartre  * its disk label. So it can be exported either with a VTOC label or with
575bae9e67eSachartre  * an EFI label. If vd_slice_label is set to VD_DISK_LABEL_VTOC then all
576bae9e67eSachartre  * single-slice disks will be exported with a VTOC label; and if it is set
577bae9e67eSachartre  * to VD_DISK_LABEL_EFI then all single-slice disks will be exported with
578bae9e67eSachartre  * an EFI label.
579bae9e67eSachartre  *
580bae9e67eSachartre  * If vd_slice_label is set to VD_DISK_LABEL_UNK and the backend is a disk
581bae9e67eSachartre  * or volume device then it will be exported with the same type of label as
582bae9e67eSachartre  * defined on the device. Otherwise if the backend is a file then it will
583bae9e67eSachartre  * exported with the disk label type set in the vd_file_slice_label variable.
584bae9e67eSachartre  *
585bae9e67eSachartre  * Note that if the backend size is greater than 1TB then it will always be
586bae9e67eSachartre  * exported with an EFI label no matter what the setting is.
587bae9e67eSachartre  */
588bae9e67eSachartre static vd_disk_label_t vd_slice_label = VD_DISK_LABEL_UNK;
589bae9e67eSachartre 
590bae9e67eSachartre static vd_disk_label_t vd_file_slice_label = VD_DISK_LABEL_VTOC;
591bae9e67eSachartre 
592bae9e67eSachartre /*
593bae9e67eSachartre  * Tunable for backward compatibility. If this variable is set to B_TRUE then
594bae9e67eSachartre  * single-slice disks are exported as disks with only one slice instead of
595bae9e67eSachartre  * faking a complete disk partitioning.
596bae9e67eSachartre  */
597bae9e67eSachartre static boolean_t vd_slice_single_slice = B_FALSE;
598bae9e67eSachartre 
599bae9e67eSachartre /*
6000a55fbb7Slm66018  * Supported protocol version pairs, from highest (newest) to lowest (oldest)
6010a55fbb7Slm66018  *
6020a55fbb7Slm66018  * Each supported major version should appear only once, paired with (and only
6030a55fbb7Slm66018  * with) its highest supported minor version number (as the protocol requires
6040a55fbb7Slm66018  * supporting all lower minor version numbers as well)
6050a55fbb7Slm66018  */
60617cadca8Slm66018 static const vio_ver_t	vds_version[] = {{1, 1}};
6070a55fbb7Slm66018 static const size_t	vds_num_versions =
6080a55fbb7Slm66018     sizeof (vds_version)/sizeof (vds_version[0]);
6090a55fbb7Slm66018 
6103af08d82Slm66018 static void vd_free_dring_task(vd_t *vdp);
6113c96341aSnarayan static int vd_setup_vd(vd_t *vd);
612047ba61eSachartre static int vd_setup_single_slice_disk(vd_t *vd);
613*1aff8f07SAlexandre Chartre static int vd_setup_slice_image(vd_t *vd);
614*1aff8f07SAlexandre Chartre static int vd_setup_disk_image(vd_t *vd);
615de3a5331SRamesh Chitrothu static int vd_backend_check_size(vd_t *vd);
6163c96341aSnarayan static boolean_t vd_enabled(vd_t *vd);
61778fcd0a1Sachartre static ushort_t vd_lbl2cksum(struct dk_label *label);
618*1aff8f07SAlexandre Chartre static int vd_dskimg_validate_geometry(vd_t *vd);
619*1aff8f07SAlexandre Chartre static boolean_t vd_dskimg_is_iso_image(vd_t *vd);
62017cadca8Slm66018 static void vd_set_exported_operations(vd_t *vd);
6212f5224aeSachartre static void vd_reset_access(vd_t *vd);
622edcc0754Sachartre static int vd_backend_ioctl(vd_t *vd, int cmd, caddr_t arg);
623edcc0754Sachartre static int vds_efi_alloc_and_read(vd_t *, efi_gpt_t **, efi_gpe_t **);
624edcc0754Sachartre static void vds_efi_free(vd_t *, efi_gpt_t *, efi_gpe_t *);
6258fce2fd6Sachartre static void vds_driver_types_free(vds_t *vds);
626342440ecSPrasad Singamsetty static void vd_vtocgeom_to_label(struct extvtoc *vtoc, struct dk_geom *geom,
627bae9e67eSachartre     struct dk_label *label);
628342440ecSPrasad Singamsetty static void vd_label_to_vtocgeom(struct dk_label *label, struct extvtoc *vtoc,
629bae9e67eSachartre     struct dk_geom *geom);
630bae9e67eSachartre static boolean_t vd_slice_geom_isvalid(vd_t *vd, struct dk_geom *geom);
631342440ecSPrasad Singamsetty static boolean_t vd_slice_vtoc_isvalid(vd_t *vd, struct extvtoc *vtoc);
632bae9e67eSachartre 
633bae9e67eSachartre extern int is_pseudo_device(dev_info_t *);
634bae9e67eSachartre 
635bae9e67eSachartre /*
636bae9e67eSachartre  * Function:
637bae9e67eSachartre  *	vd_get_readable_size
638bae9e67eSachartre  *
639bae9e67eSachartre  * Description:
640bae9e67eSachartre  * 	Convert a given size in bytes to a human readable format in
641bae9e67eSachartre  * 	kilobytes, megabytes, gigabytes or terabytes.
642bae9e67eSachartre  *
643bae9e67eSachartre  * Parameters:
644bae9e67eSachartre  *	full_size	- the size to convert in bytes.
645bae9e67eSachartre  *	size		- the converted size.
646bae9e67eSachartre  *	unit		- the unit of the converted size: 'K' (kilobyte),
647bae9e67eSachartre  *			  'M' (Megabyte), 'G' (Gigabyte), 'T' (Terabyte).
648bae9e67eSachartre  *
649bae9e67eSachartre  * Return Code:
650bae9e67eSachartre  *	none
651bae9e67eSachartre  */
652*1aff8f07SAlexandre Chartre static void
653bae9e67eSachartre vd_get_readable_size(size_t full_size, size_t *size, char *unit)
654bae9e67eSachartre {
655bae9e67eSachartre 	if (full_size < (1ULL << 20)) {
656bae9e67eSachartre 		*size = full_size >> 10;
657bae9e67eSachartre 		*unit = 'K'; /* Kilobyte */
658bae9e67eSachartre 	} else if (full_size < (1ULL << 30)) {
659bae9e67eSachartre 		*size = full_size >> 20;
660bae9e67eSachartre 		*unit = 'M'; /* Megabyte */
661bae9e67eSachartre 	} else if (full_size < (1ULL << 40)) {
662bae9e67eSachartre 		*size = full_size >> 30;
663bae9e67eSachartre 		*unit = 'G'; /* Gigabyte */
664bae9e67eSachartre 	} else {
665bae9e67eSachartre 		*size = full_size >> 40;
666bae9e67eSachartre 		*unit = 'T'; /* Terabyte */
667bae9e67eSachartre 	}
668bae9e67eSachartre }
669047ba61eSachartre 
670690555a1Sachartre /*
671690555a1Sachartre  * Function:
672*1aff8f07SAlexandre Chartre  *	vd_dskimg_io_params
673690555a1Sachartre  *
674690555a1Sachartre  * Description:
675*1aff8f07SAlexandre Chartre  * 	Convert virtual disk I/O parameters (slice, block, length) to
676*1aff8f07SAlexandre Chartre  *	(offset, length) relative to the disk image and according to
677*1aff8f07SAlexandre Chartre  *	the virtual disk partitioning.
678690555a1Sachartre  *
679690555a1Sachartre  * Parameters:
680690555a1Sachartre  *	vd		- disk on which the operation is performed.
681*1aff8f07SAlexandre Chartre  *	slice		- slice to which is the I/O parameters apply.
682*1aff8f07SAlexandre Chartre  *			  VD_SLICE_NONE indicates that parameters are
683*1aff8f07SAlexandre Chartre  *			  are relative to the entire virtual disk.
684*1aff8f07SAlexandre Chartre  *	blkp		- pointer to the starting block relative to the
685*1aff8f07SAlexandre Chartre  *			  slice; return the starting block relative to
686*1aff8f07SAlexandre Chartre  *			  the disk image.
687*1aff8f07SAlexandre Chartre  *	lenp		- pointer to the number of bytes requested; return
688*1aff8f07SAlexandre Chartre  *			  the number of bytes that can effectively be used.
689690555a1Sachartre  *
690690555a1Sachartre  * Return Code:
691*1aff8f07SAlexandre Chartre  *	0		- I/O parameters have been successfully converted;
692*1aff8f07SAlexandre Chartre  *			  blkp and lenp point to the converted values.
693*1aff8f07SAlexandre Chartre  *	ENODATA		- no data are available for the given I/O parameters;
694*1aff8f07SAlexandre Chartre  *			  This occurs if the starting block is past the limit
695*1aff8f07SAlexandre Chartre  *			  of the slice.
696*1aff8f07SAlexandre Chartre  *	EINVAL		- I/O parameters are invalid.
697690555a1Sachartre  */
698*1aff8f07SAlexandre Chartre static int
699*1aff8f07SAlexandre Chartre vd_dskimg_io_params(vd_t *vd, int slice, size_t *blkp, size_t *lenp)
700690555a1Sachartre {
701*1aff8f07SAlexandre Chartre 	size_t blk = *blkp;
702*1aff8f07SAlexandre Chartre 	size_t len = *lenp;
703*1aff8f07SAlexandre Chartre 	size_t offset, maxlen;
704690555a1Sachartre 
705*1aff8f07SAlexandre Chartre 	ASSERT(vd->file || VD_DSKIMG(vd));
706690555a1Sachartre 	ASSERT(len > 0);
707690555a1Sachartre 
708047ba61eSachartre 	/*
709047ba61eSachartre 	 * If a file is exported as a slice then we don't care about the vtoc.
710047ba61eSachartre 	 * In that case, the vtoc is a fake mainly to make newfs happy and we
711047ba61eSachartre 	 * handle any I/O as a raw disk access so that we can have access to the
712047ba61eSachartre 	 * entire backend.
713047ba61eSachartre 	 */
714047ba61eSachartre 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE || slice == VD_SLICE_NONE) {
715690555a1Sachartre 		/* raw disk access */
716690555a1Sachartre 		offset = blk * DEV_BSIZE;
717*1aff8f07SAlexandre Chartre 		if (offset >= vd->dskimg_size) {
718bae9e67eSachartre 			/* offset past the end of the disk */
719*1aff8f07SAlexandre Chartre 			PR0("offset (0x%lx) >= size (0x%lx)",
720*1aff8f07SAlexandre Chartre 			    offset, vd->dskimg_size);
721*1aff8f07SAlexandre Chartre 			return (ENODATA);
722bae9e67eSachartre 		}
723*1aff8f07SAlexandre Chartre 		maxlen = vd->dskimg_size - offset;
724690555a1Sachartre 	} else {
725690555a1Sachartre 		ASSERT(slice >= 0 && slice < V_NUMPAR);
72678fcd0a1Sachartre 
72717cadca8Slm66018 		/*
72817cadca8Slm66018 		 * v1.0 vDisk clients depended on the server not verifying
72917cadca8Slm66018 		 * the label of a unformatted disk.  This "feature" is
73017cadca8Slm66018 		 * maintained for backward compatibility but all versions
73117cadca8Slm66018 		 * from v1.1 onwards must do the right thing.
73217cadca8Slm66018 		 */
73378fcd0a1Sachartre 		if (vd->vdisk_label == VD_DISK_LABEL_UNK &&
734edcc0754Sachartre 		    vio_ver_is_supported(vd->version, 1, 1)) {
735*1aff8f07SAlexandre Chartre 			(void) vd_dskimg_validate_geometry(vd);
736edcc0754Sachartre 			if (vd->vdisk_label == VD_DISK_LABEL_UNK) {
737edcc0754Sachartre 				PR0("Unknown disk label, can't do I/O "
738edcc0754Sachartre 				    "from slice %d", slice);
739*1aff8f07SAlexandre Chartre 				return (EINVAL);
74078fcd0a1Sachartre 			}
741edcc0754Sachartre 		}
74278fcd0a1Sachartre 
743edcc0754Sachartre 		if (vd->vdisk_label == VD_DISK_LABEL_VTOC) {
744edcc0754Sachartre 			ASSERT(vd->vtoc.v_sectorsz == DEV_BSIZE);
745edcc0754Sachartre 		} else {
746edcc0754Sachartre 			ASSERT(vd->vdisk_label == VD_DISK_LABEL_EFI);
747edcc0754Sachartre 			ASSERT(vd->vdisk_block_size == DEV_BSIZE);
748edcc0754Sachartre 		}
749edcc0754Sachartre 
750edcc0754Sachartre 		if (blk >= vd->slices[slice].nblocks) {
751690555a1Sachartre 			/* address past the end of the slice */
752bae9e67eSachartre 			PR0("req_addr (0x%lx) >= psize (0x%lx)",
753edcc0754Sachartre 			    blk, vd->slices[slice].nblocks);
754*1aff8f07SAlexandre Chartre 			return (ENODATA);
755690555a1Sachartre 		}
756690555a1Sachartre 
757edcc0754Sachartre 		offset = (vd->slices[slice].start + blk) * DEV_BSIZE;
758bae9e67eSachartre 		maxlen = (vd->slices[slice].nblocks - blk) * DEV_BSIZE;
759bae9e67eSachartre 	}
760690555a1Sachartre 
761690555a1Sachartre 	/*
762690555a1Sachartre 	 * If the requested size is greater than the size
763690555a1Sachartre 	 * of the partition, truncate the read/write.
764690555a1Sachartre 	 */
765690555a1Sachartre 	if (len > maxlen) {
766690555a1Sachartre 		PR0("I/O size truncated to %lu bytes from %lu bytes",
767690555a1Sachartre 		    maxlen, len);
768690555a1Sachartre 		len = maxlen;
769690555a1Sachartre 	}
770690555a1Sachartre 
771690555a1Sachartre 	/*
772690555a1Sachartre 	 * We have to ensure that we are reading/writing into the mmap
773690555a1Sachartre 	 * range. If we have a partial disk image (e.g. an image of
774690555a1Sachartre 	 * s0 instead s2) the system can try to access slices that
775690555a1Sachartre 	 * are not included into the disk image.
776690555a1Sachartre 	 */
777*1aff8f07SAlexandre Chartre 	if ((offset + len) > vd->dskimg_size) {
778edcc0754Sachartre 		PR0("offset + nbytes (0x%lx + 0x%lx) > "
779*1aff8f07SAlexandre Chartre 		    "dskimg_size (0x%lx)", offset, len, vd->dskimg_size);
780*1aff8f07SAlexandre Chartre 		return (EINVAL);
781*1aff8f07SAlexandre Chartre 	}
782*1aff8f07SAlexandre Chartre 
783*1aff8f07SAlexandre Chartre 	*blkp = offset / DEV_BSIZE;
784*1aff8f07SAlexandre Chartre 	*lenp = len;
785*1aff8f07SAlexandre Chartre 
786*1aff8f07SAlexandre Chartre 	return (0);
787*1aff8f07SAlexandre Chartre }
788*1aff8f07SAlexandre Chartre 
789*1aff8f07SAlexandre Chartre /*
790*1aff8f07SAlexandre Chartre  * Function:
791*1aff8f07SAlexandre Chartre  *	vd_dskimg_rw
792*1aff8f07SAlexandre Chartre  *
793*1aff8f07SAlexandre Chartre  * Description:
794*1aff8f07SAlexandre Chartre  * 	Read or write to a disk image. It handles the case where the disk
795*1aff8f07SAlexandre Chartre  *	image is a file or a volume exported as a full disk or a file
796*1aff8f07SAlexandre Chartre  *	exported as single-slice disk. Read or write to volumes exported as
797*1aff8f07SAlexandre Chartre  *	single slice disks are done by directly using the ldi interface.
798*1aff8f07SAlexandre Chartre  *
799*1aff8f07SAlexandre Chartre  * Parameters:
800*1aff8f07SAlexandre Chartre  *	vd		- disk on which the operation is performed.
801*1aff8f07SAlexandre Chartre  *	slice		- slice on which the operation is performed,
802*1aff8f07SAlexandre Chartre  *			  VD_SLICE_NONE indicates that the operation
803*1aff8f07SAlexandre Chartre  *			  is done using an absolute disk offset.
804*1aff8f07SAlexandre Chartre  *	operation	- operation to execute: read (VD_OP_BREAD) or
805*1aff8f07SAlexandre Chartre  *			  write (VD_OP_BWRITE).
806*1aff8f07SAlexandre Chartre  *	data		- buffer where data are read to or written from.
807*1aff8f07SAlexandre Chartre  *	blk		- starting block for the operation.
808*1aff8f07SAlexandre Chartre  *	len		- number of bytes to read or write.
809*1aff8f07SAlexandre Chartre  *
810*1aff8f07SAlexandre Chartre  * Return Code:
811*1aff8f07SAlexandre Chartre  *	n >= 0		- success, n indicates the number of bytes read
812*1aff8f07SAlexandre Chartre  *			  or written.
813*1aff8f07SAlexandre Chartre  *	-1		- error.
814*1aff8f07SAlexandre Chartre  */
815*1aff8f07SAlexandre Chartre static ssize_t
816*1aff8f07SAlexandre Chartre vd_dskimg_rw(vd_t *vd, int slice, int operation, caddr_t data, size_t offset,
817*1aff8f07SAlexandre Chartre     size_t len)
818*1aff8f07SAlexandre Chartre {
819*1aff8f07SAlexandre Chartre 	caddr_t	maddr;
820*1aff8f07SAlexandre Chartre 	size_t moffset, mlen, n;
821*1aff8f07SAlexandre Chartre 	uint_t smflags;
822*1aff8f07SAlexandre Chartre 	enum seg_rw srw;
823*1aff8f07SAlexandre Chartre 	ssize_t resid;
824*1aff8f07SAlexandre Chartre 	struct buf buf;
825*1aff8f07SAlexandre Chartre 	int status;
826*1aff8f07SAlexandre Chartre 
827*1aff8f07SAlexandre Chartre 	ASSERT(vd->file || VD_DSKIMG(vd));
828*1aff8f07SAlexandre Chartre 	ASSERT(len > 0);
829*1aff8f07SAlexandre Chartre 
830*1aff8f07SAlexandre Chartre 	if ((status = vd_dskimg_io_params(vd, slice, &offset, &len)) != 0)
831*1aff8f07SAlexandre Chartre 		return ((status == ENODATA)? 0: -1);
832*1aff8f07SAlexandre Chartre 
833*1aff8f07SAlexandre Chartre 	offset *= DEV_BSIZE;
834*1aff8f07SAlexandre Chartre 
835*1aff8f07SAlexandre Chartre 	if (vd->volume) {
836*1aff8f07SAlexandre Chartre 		ASSERT(offset % DEV_BSIZE == 0);
837*1aff8f07SAlexandre Chartre 
838*1aff8f07SAlexandre Chartre 		bioinit(&buf);
839*1aff8f07SAlexandre Chartre 		buf.b_flags	= B_BUSY |
840*1aff8f07SAlexandre Chartre 		    ((operation == VD_OP_BREAD)? B_READ : B_WRITE);
841*1aff8f07SAlexandre Chartre 		buf.b_bcount	= len;
842*1aff8f07SAlexandre Chartre 		buf.b_lblkno	= offset / DEV_BSIZE;
843*1aff8f07SAlexandre Chartre 		buf.b_edev 	= vd->dev[0];
844*1aff8f07SAlexandre Chartre 		buf.b_un.b_addr = data;
845*1aff8f07SAlexandre Chartre 
846*1aff8f07SAlexandre Chartre 		/*
847*1aff8f07SAlexandre Chartre 		 * We use ldi_strategy() and not ldi_read()/ldi_write() because
848*1aff8f07SAlexandre Chartre 		 * the read/write functions of the underlying driver may try to
849*1aff8f07SAlexandre Chartre 		 * lock pages of the data buffer, and this requires the data
850*1aff8f07SAlexandre Chartre 		 * buffer to be kmem_alloc'ed (and not allocated on the stack).
851*1aff8f07SAlexandre Chartre 		 *
852*1aff8f07SAlexandre Chartre 		 * Also using ldi_strategy() ensures that writes are immediatly
853*1aff8f07SAlexandre Chartre 		 * commited and not cached as this may be the case with
854*1aff8f07SAlexandre Chartre 		 * ldi_write() (for example with a ZFS volume).
855*1aff8f07SAlexandre Chartre 		 */
856*1aff8f07SAlexandre Chartre 		if (ldi_strategy(vd->ldi_handle[0], &buf) != 0) {
857*1aff8f07SAlexandre Chartre 			biofini(&buf);
858690555a1Sachartre 			return (-1);
859690555a1Sachartre 		}
860690555a1Sachartre 
861*1aff8f07SAlexandre Chartre 		if (biowait(&buf) != 0) {
862*1aff8f07SAlexandre Chartre 			biofini(&buf);
863*1aff8f07SAlexandre Chartre 			return (-1);
864*1aff8f07SAlexandre Chartre 		}
865*1aff8f07SAlexandre Chartre 
866*1aff8f07SAlexandre Chartre 		resid = buf.b_resid;
867*1aff8f07SAlexandre Chartre 		biofini(&buf);
868*1aff8f07SAlexandre Chartre 
869*1aff8f07SAlexandre Chartre 		ASSERT(resid <= len);
870*1aff8f07SAlexandre Chartre 		return (len - resid);
871*1aff8f07SAlexandre Chartre 	}
872*1aff8f07SAlexandre Chartre 
873*1aff8f07SAlexandre Chartre 	ASSERT(vd->file);
874*1aff8f07SAlexandre Chartre 
875690555a1Sachartre 	srw = (operation == VD_OP_BREAD)? S_READ : S_WRITE;
876eba0cb4eSachartre 	smflags = (operation == VD_OP_BREAD)? 0 :
877eba0cb4eSachartre 	    (SM_WRITE | vd_file_write_flags);
878690555a1Sachartre 	n = len;
879690555a1Sachartre 
880690555a1Sachartre 	do {
881690555a1Sachartre 		/*
882690555a1Sachartre 		 * segmap_getmapflt() returns a MAXBSIZE chunk which is
883690555a1Sachartre 		 * MAXBSIZE aligned.
884690555a1Sachartre 		 */
885690555a1Sachartre 		moffset = offset & MAXBOFFSET;
886690555a1Sachartre 		mlen = MIN(MAXBSIZE - moffset, n);
887690555a1Sachartre 		maddr = segmap_getmapflt(segkmap, vd->file_vnode, offset,
888690555a1Sachartre 		    mlen, 1, srw);
889690555a1Sachartre 		/*
890690555a1Sachartre 		 * Fault in the pages so we can check for error and ensure
891690555a1Sachartre 		 * that we can safely used the mapped address.
892690555a1Sachartre 		 */
893690555a1Sachartre 		if (segmap_fault(kas.a_hat, segkmap, maddr, mlen,
894690555a1Sachartre 		    F_SOFTLOCK, srw) != 0) {
895690555a1Sachartre 			(void) segmap_release(segkmap, maddr, 0);
896690555a1Sachartre 			return (-1);
897690555a1Sachartre 		}
898690555a1Sachartre 
899690555a1Sachartre 		if (operation == VD_OP_BREAD)
900690555a1Sachartre 			bcopy(maddr + moffset, data, mlen);
901690555a1Sachartre 		else
902690555a1Sachartre 			bcopy(data, maddr + moffset, mlen);
903690555a1Sachartre 
904690555a1Sachartre 		if (segmap_fault(kas.a_hat, segkmap, maddr, mlen,
905690555a1Sachartre 		    F_SOFTUNLOCK, srw) != 0) {
906690555a1Sachartre 			(void) segmap_release(segkmap, maddr, 0);
907690555a1Sachartre 			return (-1);
908690555a1Sachartre 		}
909690555a1Sachartre 		if (segmap_release(segkmap, maddr, smflags) != 0)
910690555a1Sachartre 			return (-1);
911690555a1Sachartre 		n -= mlen;
912690555a1Sachartre 		offset += mlen;
913690555a1Sachartre 		data += mlen;
914690555a1Sachartre 
915690555a1Sachartre 	} while (n > 0);
916690555a1Sachartre 
917690555a1Sachartre 	return (len);
918690555a1Sachartre }
919690555a1Sachartre 
92087a7269eSachartre /*
92187a7269eSachartre  * Function:
922bae9e67eSachartre  *	vd_build_default_label
92378fcd0a1Sachartre  *
92478fcd0a1Sachartre  * Description:
925bae9e67eSachartre  *	Return a default label for a given disk size. This is used when the disk
92678fcd0a1Sachartre  *	does not have a valid VTOC so that the user can get a valid default
92717cadca8Slm66018  *	configuration. The default label has all slice sizes set to 0 (except
92878fcd0a1Sachartre  *	slice 2 which is the entire disk) to force the user to write a valid
92978fcd0a1Sachartre  *	label onto the disk image.
93078fcd0a1Sachartre  *
93178fcd0a1Sachartre  * Parameters:
932bae9e67eSachartre  *	disk_size	- the disk size in bytes
93378fcd0a1Sachartre  *	label		- the returned default label.
93478fcd0a1Sachartre  *
93578fcd0a1Sachartre  * Return Code:
93678fcd0a1Sachartre  *	none.
93778fcd0a1Sachartre  */
93878fcd0a1Sachartre static void
939bae9e67eSachartre vd_build_default_label(size_t disk_size, struct dk_label *label)
94078fcd0a1Sachartre {
94178fcd0a1Sachartre 	size_t size;
942bae9e67eSachartre 	char unit;
943edcc0754Sachartre 
944edcc0754Sachartre 	bzero(label, sizeof (struct dk_label));
94578fcd0a1Sachartre 
94678fcd0a1Sachartre 	/*
947342440ecSPrasad Singamsetty 	 * Ideally we would like the cylinder size (nsect * nhead) to be the
948342440ecSPrasad Singamsetty 	 * same whatever the disk size is. That way the VTOC label could be
949342440ecSPrasad Singamsetty 	 * easily updated in case the disk size is increased (keeping the
950342440ecSPrasad Singamsetty 	 * same cylinder size allows to preserve the existing partitioning
951342440ecSPrasad Singamsetty 	 * when updating the VTOC label). But it is not possible to have
952342440ecSPrasad Singamsetty 	 * a fixed cylinder size and to cover all disk size.
95378fcd0a1Sachartre 	 *
954342440ecSPrasad Singamsetty 	 * So we define different cylinder sizes depending on the disk size.
955342440ecSPrasad Singamsetty 	 * The cylinder size is chosen so that we don't have too few cylinders
956f745d6a3Sachartre 	 * for a small disk image, or so many on a big disk image that you
957f745d6a3Sachartre 	 * waste space for backup superblocks or cylinder group structures.
958342440ecSPrasad Singamsetty 	 * Also we must have a resonable number of cylinders and sectors so
959342440ecSPrasad Singamsetty 	 * that newfs can run using default values.
960342440ecSPrasad Singamsetty 	 *
961342440ecSPrasad Singamsetty 	 *	+-----------+--------+---------+--------+
962342440ecSPrasad Singamsetty 	 *	| disk_size |  < 2MB | 2MB-4GB | >= 8GB |
963342440ecSPrasad Singamsetty 	 *	+-----------+--------+---------+--------+
964342440ecSPrasad Singamsetty 	 *	| nhead	    |	 1   |	   1   |    96  |
965342440ecSPrasad Singamsetty 	 *	| nsect	    |  200   |   600   |   768  |
966342440ecSPrasad Singamsetty 	 *	+-----------+--------+---------+--------+
967342440ecSPrasad Singamsetty 	 *
968342440ecSPrasad Singamsetty 	 * Other parameters are computed from these values:
969342440ecSPrasad Singamsetty 	 *
970342440ecSPrasad Singamsetty 	 * 	pcyl = disk_size / (nhead * nsect * 512)
971342440ecSPrasad Singamsetty 	 * 	acyl = (pcyl > 2)? 2 : 0
972342440ecSPrasad Singamsetty 	 * 	ncyl = pcyl - acyl
973342440ecSPrasad Singamsetty 	 *
974342440ecSPrasad Singamsetty 	 * The maximum number of cylinder is 65535 so this allows to define a
975342440ecSPrasad Singamsetty 	 * geometry for a disk size up to 65535 * 96 * 768 * 512 = 2.24 TB
976342440ecSPrasad Singamsetty 	 * which is more than enough to cover the maximum size allowed by the
977342440ecSPrasad Singamsetty 	 * extended VTOC format (2TB).
97878fcd0a1Sachartre 	 */
979342440ecSPrasad Singamsetty 
980342440ecSPrasad Singamsetty 	if (disk_size >= 8 * ONE_GIGABYTE) {
981342440ecSPrasad Singamsetty 
982342440ecSPrasad Singamsetty 		label->dkl_nhead = 96;
983342440ecSPrasad Singamsetty 		label->dkl_nsect = 768;
984342440ecSPrasad Singamsetty 
985342440ecSPrasad Singamsetty 	} else if (disk_size >= 2 * ONE_MEGABYTE) {
986342440ecSPrasad Singamsetty 
987342440ecSPrasad Singamsetty 		label->dkl_nhead = 1;
988342440ecSPrasad Singamsetty 		label->dkl_nsect = 600;
989342440ecSPrasad Singamsetty 
990342440ecSPrasad Singamsetty 	} else {
991342440ecSPrasad Singamsetty 
992342440ecSPrasad Singamsetty 		label->dkl_nhead = 1;
993342440ecSPrasad Singamsetty 		label->dkl_nsect = 200;
994342440ecSPrasad Singamsetty 	}
995342440ecSPrasad Singamsetty 
996342440ecSPrasad Singamsetty 	label->dkl_pcyl = disk_size /
997342440ecSPrasad Singamsetty 	    (label->dkl_nsect * label->dkl_nhead * DEV_BSIZE);
99878fcd0a1Sachartre 
99978fcd0a1Sachartre 	if (label->dkl_pcyl == 0)
100078fcd0a1Sachartre 		label->dkl_pcyl = 1;
100178fcd0a1Sachartre 
1002047ba61eSachartre 	label->dkl_acyl = 0;
1003047ba61eSachartre 
100478fcd0a1Sachartre 	if (label->dkl_pcyl > 2)
100578fcd0a1Sachartre 		label->dkl_acyl = 2;
100678fcd0a1Sachartre 
100778fcd0a1Sachartre 	label->dkl_ncyl = label->dkl_pcyl - label->dkl_acyl;
100878fcd0a1Sachartre 	label->dkl_write_reinstruct = 0;
100978fcd0a1Sachartre 	label->dkl_read_reinstruct = 0;
101078fcd0a1Sachartre 	label->dkl_rpm = 7200;
101178fcd0a1Sachartre 	label->dkl_apc = 0;
101278fcd0a1Sachartre 	label->dkl_intrlv = 0;
101378fcd0a1Sachartre 
1014bae9e67eSachartre 	PR0("requested disk size: %ld bytes\n", disk_size);
101578fcd0a1Sachartre 	PR0("setup: ncyl=%d nhead=%d nsec=%d\n", label->dkl_pcyl,
101678fcd0a1Sachartre 	    label->dkl_nhead, label->dkl_nsect);
101778fcd0a1Sachartre 	PR0("provided disk size: %ld bytes\n", (uint64_t)
101878fcd0a1Sachartre 	    (label->dkl_pcyl * label->dkl_nhead *
101978fcd0a1Sachartre 	    label->dkl_nsect * DEV_BSIZE));
102078fcd0a1Sachartre 
1021bae9e67eSachartre 	vd_get_readable_size(disk_size, &size, &unit);
102278fcd0a1Sachartre 
102378fcd0a1Sachartre 	/*
102478fcd0a1Sachartre 	 * We must have a correct label name otherwise format(1m) will
102578fcd0a1Sachartre 	 * not recognized the disk as labeled.
102678fcd0a1Sachartre 	 */
102778fcd0a1Sachartre 	(void) snprintf(label->dkl_asciilabel, LEN_DKL_ASCII,
102878fcd0a1Sachartre 	    "SUN-DiskImage-%ld%cB cyl %d alt %d hd %d sec %d",
1029bae9e67eSachartre 	    size, unit,
103078fcd0a1Sachartre 	    label->dkl_ncyl, label->dkl_acyl, label->dkl_nhead,
103178fcd0a1Sachartre 	    label->dkl_nsect);
103278fcd0a1Sachartre 
103378fcd0a1Sachartre 	/* default VTOC */
1034342440ecSPrasad Singamsetty 	label->dkl_vtoc.v_version = V_EXTVERSION;
1035edcc0754Sachartre 	label->dkl_vtoc.v_nparts = V_NUMPAR;
103678fcd0a1Sachartre 	label->dkl_vtoc.v_sanity = VTOC_SANE;
1037edcc0754Sachartre 	label->dkl_vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_tag = V_BACKUP;
1038edcc0754Sachartre 	label->dkl_map[VD_ENTIRE_DISK_SLICE].dkl_cylno = 0;
1039edcc0754Sachartre 	label->dkl_map[VD_ENTIRE_DISK_SLICE].dkl_nblk = label->dkl_ncyl *
104078fcd0a1Sachartre 	    label->dkl_nhead * label->dkl_nsect;
1041edcc0754Sachartre 	label->dkl_magic = DKL_MAGIC;
104278fcd0a1Sachartre 	label->dkl_cksum = vd_lbl2cksum(label);
104378fcd0a1Sachartre }
104478fcd0a1Sachartre 
104578fcd0a1Sachartre /*
104678fcd0a1Sachartre  * Function:
1047*1aff8f07SAlexandre Chartre  *	vd_dskimg_set_vtoc
104887a7269eSachartre  *
104987a7269eSachartre  * Description:
105087a7269eSachartre  *	Set the vtoc of a disk image by writing the label and backup
105187a7269eSachartre  *	labels into the disk image backend.
105287a7269eSachartre  *
105387a7269eSachartre  * Parameters:
105487a7269eSachartre  *	vd		- disk on which the operation is performed.
105587a7269eSachartre  *	label		- the data to be written.
105687a7269eSachartre  *
105787a7269eSachartre  * Return Code:
105887a7269eSachartre  *	0		- success.
105987a7269eSachartre  *	n > 0		- error, n indicates the errno code.
106087a7269eSachartre  */
106187a7269eSachartre static int
1062*1aff8f07SAlexandre Chartre vd_dskimg_set_vtoc(vd_t *vd, struct dk_label *label)
106387a7269eSachartre {
1064342440ecSPrasad Singamsetty 	size_t blk, sec, cyl, head, cnt;
106587a7269eSachartre 
1066*1aff8f07SAlexandre Chartre 	ASSERT(VD_DSKIMG(vd));
106787a7269eSachartre 
1068*1aff8f07SAlexandre Chartre 	if (VD_DSKIMG_LABEL_WRITE(vd, label) < 0) {
106987a7269eSachartre 		PR0("fail to write disk label");
107087a7269eSachartre 		return (EIO);
107187a7269eSachartre 	}
107287a7269eSachartre 
107387a7269eSachartre 	/*
107487a7269eSachartre 	 * Backup labels are on the last alternate cylinder's
107587a7269eSachartre 	 * first five odd sectors.
107687a7269eSachartre 	 */
107787a7269eSachartre 	if (label->dkl_acyl == 0) {
107887a7269eSachartre 		PR0("no alternate cylinder, can not store backup labels");
107987a7269eSachartre 		return (0);
108087a7269eSachartre 	}
108187a7269eSachartre 
108287a7269eSachartre 	cyl = label->dkl_ncyl  + label->dkl_acyl - 1;
108387a7269eSachartre 	head = label->dkl_nhead - 1;
108487a7269eSachartre 
108587a7269eSachartre 	blk = (cyl * ((label->dkl_nhead * label->dkl_nsect) - label->dkl_apc)) +
108687a7269eSachartre 	    (head * label->dkl_nsect);
108787a7269eSachartre 
108887a7269eSachartre 	/*
108987a7269eSachartre 	 * Write the backup labels. Make sure we don't try to write past
109087a7269eSachartre 	 * the last cylinder.
109187a7269eSachartre 	 */
109287a7269eSachartre 	sec = 1;
109387a7269eSachartre 
1094*1aff8f07SAlexandre Chartre 	for (cnt = 0; cnt < VD_DSKIMG_NUM_BACKUP; cnt++) {
109587a7269eSachartre 
109687a7269eSachartre 		if (sec >= label->dkl_nsect) {
109787a7269eSachartre 			PR0("not enough sector to store all backup labels");
109887a7269eSachartre 			return (0);
109987a7269eSachartre 		}
110087a7269eSachartre 
1101*1aff8f07SAlexandre Chartre 		if (vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE,
1102*1aff8f07SAlexandre Chartre 		    (caddr_t)label, blk + sec, sizeof (struct dk_label)) < 0) {
1103342440ecSPrasad Singamsetty 			PR0("error writing backup label at block %lu\n",
110487a7269eSachartre 			    blk + sec);
110587a7269eSachartre 			return (EIO);
110687a7269eSachartre 		}
110787a7269eSachartre 
1108342440ecSPrasad Singamsetty 		PR1("wrote backup label at block %lu\n", blk + sec);
110987a7269eSachartre 
111087a7269eSachartre 		sec += 2;
111187a7269eSachartre 	}
111287a7269eSachartre 
111387a7269eSachartre 	return (0);
111487a7269eSachartre }
111587a7269eSachartre 
111687a7269eSachartre /*
111787a7269eSachartre  * Function:
1118*1aff8f07SAlexandre Chartre  *	vd_dskimg_get_devid_block
111987a7269eSachartre  *
112087a7269eSachartre  * Description:
112187a7269eSachartre  *	Return the block number where the device id is stored.
112287a7269eSachartre  *
112387a7269eSachartre  * Parameters:
112487a7269eSachartre  *	vd		- disk on which the operation is performed.
112587a7269eSachartre  *	blkp		- pointer to the block number
112687a7269eSachartre  *
112787a7269eSachartre  * Return Code:
112887a7269eSachartre  *	0		- success
112987a7269eSachartre  *	ENOSPC		- disk has no space to store a device id
113087a7269eSachartre  */
113187a7269eSachartre static int
1132*1aff8f07SAlexandre Chartre vd_dskimg_get_devid_block(vd_t *vd, size_t *blkp)
113387a7269eSachartre {
113487a7269eSachartre 	diskaddr_t spc, head, cyl;
113587a7269eSachartre 
1136*1aff8f07SAlexandre Chartre 	ASSERT(VD_DSKIMG(vd));
1137edcc0754Sachartre 
1138edcc0754Sachartre 	if (vd->vdisk_label == VD_DISK_LABEL_UNK) {
1139edcc0754Sachartre 		/*
1140edcc0754Sachartre 		 * If no label is defined we don't know where to find
1141edcc0754Sachartre 		 * a device id.
1142edcc0754Sachartre 		 */
1143edcc0754Sachartre 		return (ENOSPC);
1144edcc0754Sachartre 	}
1145edcc0754Sachartre 
1146edcc0754Sachartre 	if (vd->vdisk_label == VD_DISK_LABEL_EFI) {
1147edcc0754Sachartre 		/*
1148edcc0754Sachartre 		 * For an EFI disk, the devid is at the beginning of
1149edcc0754Sachartre 		 * the reserved slice
1150edcc0754Sachartre 		 */
1151edcc0754Sachartre 		if (vd->efi_reserved == -1) {
1152edcc0754Sachartre 			PR0("EFI disk has no reserved slice");
1153edcc0754Sachartre 			return (ENOSPC);
1154edcc0754Sachartre 		}
1155edcc0754Sachartre 
1156edcc0754Sachartre 		*blkp = vd->slices[vd->efi_reserved].start;
1157edcc0754Sachartre 		return (0);
1158edcc0754Sachartre 	}
1159edcc0754Sachartre 
116087a7269eSachartre 	ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
116187a7269eSachartre 
116287a7269eSachartre 	/* this geometry doesn't allow us to have a devid */
116387a7269eSachartre 	if (vd->dk_geom.dkg_acyl < 2) {
116487a7269eSachartre 		PR0("not enough alternate cylinder available for devid "
116587a7269eSachartre 		    "(acyl=%u)", vd->dk_geom.dkg_acyl);
116687a7269eSachartre 		return (ENOSPC);
116787a7269eSachartre 	}
116887a7269eSachartre 
116987a7269eSachartre 	/* the devid is in on the track next to the last cylinder */
117087a7269eSachartre 	cyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl - 2;
117187a7269eSachartre 	spc = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
117287a7269eSachartre 	head = vd->dk_geom.dkg_nhead - 1;
117387a7269eSachartre 
117487a7269eSachartre 	*blkp = (cyl * (spc - vd->dk_geom.dkg_apc)) +
117587a7269eSachartre 	    (head * vd->dk_geom.dkg_nsect) + 1;
117687a7269eSachartre 
117787a7269eSachartre 	return (0);
117887a7269eSachartre }
117987a7269eSachartre 
118087a7269eSachartre /*
118187a7269eSachartre  * Return the checksum of a disk block containing an on-disk devid.
118287a7269eSachartre  */
118387a7269eSachartre static uint_t
118487a7269eSachartre vd_dkdevid2cksum(struct dk_devid *dkdevid)
118587a7269eSachartre {
118687a7269eSachartre 	uint_t chksum, *ip;
118787a7269eSachartre 	int i;
118887a7269eSachartre 
118987a7269eSachartre 	chksum = 0;
1190342440ecSPrasad Singamsetty 	ip = (void *)dkdevid;
119187a7269eSachartre 	for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int)); i++)
119287a7269eSachartre 		chksum ^= ip[i];
119387a7269eSachartre 
119487a7269eSachartre 	return (chksum);
119587a7269eSachartre }
119687a7269eSachartre 
119787a7269eSachartre /*
119887a7269eSachartre  * Function:
1199*1aff8f07SAlexandre Chartre  *	vd_dskimg_read_devid
120087a7269eSachartre  *
120187a7269eSachartre  * Description:
120287a7269eSachartre  *	Read the device id stored on a disk image.
120387a7269eSachartre  *
120487a7269eSachartre  * Parameters:
120587a7269eSachartre  *	vd		- disk on which the operation is performed.
120687a7269eSachartre  *	devid		- the return address of the device ID.
120787a7269eSachartre  *
120887a7269eSachartre  * Return Code:
120987a7269eSachartre  *	0		- success
121087a7269eSachartre  *	EIO		- I/O error while trying to access the disk image
121187a7269eSachartre  *	EINVAL		- no valid device id was found
121287a7269eSachartre  *	ENOSPC		- disk has no space to store a device id
121387a7269eSachartre  */
121487a7269eSachartre static int
1215*1aff8f07SAlexandre Chartre vd_dskimg_read_devid(vd_t *vd, ddi_devid_t *devid)
121687a7269eSachartre {
121787a7269eSachartre 	struct dk_devid *dkdevid;
121887a7269eSachartre 	size_t blk;
121987a7269eSachartre 	uint_t chksum;
122087a7269eSachartre 	int status, sz;
122187a7269eSachartre 
1222*1aff8f07SAlexandre Chartre 	if ((status = vd_dskimg_get_devid_block(vd, &blk)) != 0)
122387a7269eSachartre 		return (status);
122487a7269eSachartre 
122587a7269eSachartre 	dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP);
122687a7269eSachartre 
122787a7269eSachartre 	/* get the devid */
1228*1aff8f07SAlexandre Chartre 	if ((vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)dkdevid, blk,
122987a7269eSachartre 	    DEV_BSIZE)) < 0) {
123087a7269eSachartre 		PR0("error reading devid block at %lu", blk);
123187a7269eSachartre 		status = EIO;
123287a7269eSachartre 		goto done;
123387a7269eSachartre 	}
123487a7269eSachartre 
123587a7269eSachartre 	/* validate the revision */
123687a7269eSachartre 	if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
123787a7269eSachartre 	    (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
123887a7269eSachartre 		PR0("invalid devid found at block %lu (bad revision)", blk);
123987a7269eSachartre 		status = EINVAL;
124087a7269eSachartre 		goto done;
124187a7269eSachartre 	}
124287a7269eSachartre 
124387a7269eSachartre 	/* compute checksum */
124487a7269eSachartre 	chksum = vd_dkdevid2cksum(dkdevid);
124587a7269eSachartre 
124687a7269eSachartre 	/* compare the checksums */
124787a7269eSachartre 	if (DKD_GETCHKSUM(dkdevid) != chksum) {
124887a7269eSachartre 		PR0("invalid devid found at block %lu (bad checksum)", blk);
124987a7269eSachartre 		status = EINVAL;
125087a7269eSachartre 		goto done;
125187a7269eSachartre 	}
125287a7269eSachartre 
125387a7269eSachartre 	/* validate the device id */
125487a7269eSachartre 	if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
125587a7269eSachartre 		PR0("invalid devid found at block %lu", blk);
125687a7269eSachartre 		status = EINVAL;
125787a7269eSachartre 		goto done;
125887a7269eSachartre 	}
125987a7269eSachartre 
126087a7269eSachartre 	PR1("devid read at block %lu", blk);
126187a7269eSachartre 
126287a7269eSachartre 	sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
126387a7269eSachartre 	*devid = kmem_alloc(sz, KM_SLEEP);
126487a7269eSachartre 	bcopy(&dkdevid->dkd_devid, *devid, sz);
126587a7269eSachartre 
126687a7269eSachartre done:
126787a7269eSachartre 	kmem_free(dkdevid, DEV_BSIZE);
126887a7269eSachartre 	return (status);
126987a7269eSachartre 
127087a7269eSachartre }
127187a7269eSachartre 
127287a7269eSachartre /*
127387a7269eSachartre  * Function:
1274*1aff8f07SAlexandre Chartre  *	vd_dskimg_write_devid
127587a7269eSachartre  *
127687a7269eSachartre  * Description:
127787a7269eSachartre  *	Write a device id into disk image.
127887a7269eSachartre  *
127987a7269eSachartre  * Parameters:
128087a7269eSachartre  *	vd		- disk on which the operation is performed.
128187a7269eSachartre  *	devid		- the device ID to store.
128287a7269eSachartre  *
128387a7269eSachartre  * Return Code:
128487a7269eSachartre  *	0		- success
128587a7269eSachartre  *	EIO		- I/O error while trying to access the disk image
128687a7269eSachartre  *	ENOSPC		- disk has no space to store a device id
128787a7269eSachartre  */
128887a7269eSachartre static int
1289*1aff8f07SAlexandre Chartre vd_dskimg_write_devid(vd_t *vd, ddi_devid_t devid)
129087a7269eSachartre {
129187a7269eSachartre 	struct dk_devid *dkdevid;
129287a7269eSachartre 	uint_t chksum;
129387a7269eSachartre 	size_t blk;
129487a7269eSachartre 	int status;
129587a7269eSachartre 
1296edcc0754Sachartre 	if (devid == NULL) {
1297edcc0754Sachartre 		/* nothing to write */
1298edcc0754Sachartre 		return (0);
1299edcc0754Sachartre 	}
1300edcc0754Sachartre 
1301*1aff8f07SAlexandre Chartre 	if ((status = vd_dskimg_get_devid_block(vd, &blk)) != 0)
130287a7269eSachartre 		return (status);
130387a7269eSachartre 
130487a7269eSachartre 	dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP);
130587a7269eSachartre 
130687a7269eSachartre 	/* set revision */
130787a7269eSachartre 	dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
130887a7269eSachartre 	dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
130987a7269eSachartre 
131087a7269eSachartre 	/* copy devid */
131187a7269eSachartre 	bcopy(devid, &dkdevid->dkd_devid, ddi_devid_sizeof(devid));
131287a7269eSachartre 
131387a7269eSachartre 	/* compute checksum */
131487a7269eSachartre 	chksum = vd_dkdevid2cksum(dkdevid);
131587a7269eSachartre 
131687a7269eSachartre 	/* set checksum */
131787a7269eSachartre 	DKD_FORMCHKSUM(chksum, dkdevid);
131887a7269eSachartre 
131987a7269eSachartre 	/* store the devid */
1320*1aff8f07SAlexandre Chartre 	if ((status = vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE,
132187a7269eSachartre 	    (caddr_t)dkdevid, blk, DEV_BSIZE)) < 0) {
132287a7269eSachartre 		PR0("Error writing devid block at %lu", blk);
132387a7269eSachartre 		status = EIO;
132487a7269eSachartre 	} else {
132587a7269eSachartre 		PR1("devid written at block %lu", blk);
132687a7269eSachartre 		status = 0;
132787a7269eSachartre 	}
132887a7269eSachartre 
132987a7269eSachartre 	kmem_free(dkdevid, DEV_BSIZE);
133087a7269eSachartre 	return (status);
133187a7269eSachartre }
133287a7269eSachartre 
133387a7269eSachartre /*
133487a7269eSachartre  * Function:
133517cadca8Slm66018  *	vd_do_scsi_rdwr
133687a7269eSachartre  *
133787a7269eSachartre  * Description:
133887a7269eSachartre  * 	Read or write to a SCSI disk using an absolute disk offset.
133987a7269eSachartre  *
134087a7269eSachartre  * Parameters:
134187a7269eSachartre  *	vd		- disk on which the operation is performed.
134287a7269eSachartre  *	operation	- operation to execute: read (VD_OP_BREAD) or
134387a7269eSachartre  *			  write (VD_OP_BWRITE).
134487a7269eSachartre  *	data		- buffer where data are read to or written from.
134587a7269eSachartre  *	blk		- starting block for the operation.
134687a7269eSachartre  *	len		- number of bytes to read or write.
134787a7269eSachartre  *
134887a7269eSachartre  * Return Code:
134987a7269eSachartre  *	0		- success
135087a7269eSachartre  *	n != 0		- error.
135187a7269eSachartre  */
135287a7269eSachartre static int
135317cadca8Slm66018 vd_do_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t blk, size_t len)
135487a7269eSachartre {
135587a7269eSachartre 	struct uscsi_cmd ucmd;
135687a7269eSachartre 	union scsi_cdb cdb;
135787a7269eSachartre 	int nsectors, nblk;
135887a7269eSachartre 	int max_sectors;
135987a7269eSachartre 	int status, rval;
136087a7269eSachartre 
136187a7269eSachartre 	ASSERT(!vd->file);
1362*1aff8f07SAlexandre Chartre 	ASSERT(!vd->volume);
136317cadca8Slm66018 	ASSERT(vd->vdisk_block_size > 0);
136487a7269eSachartre 
136587a7269eSachartre 	max_sectors = vd->max_xfer_sz;
136617cadca8Slm66018 	nblk = (len / vd->vdisk_block_size);
136787a7269eSachartre 
136817cadca8Slm66018 	if (len % vd->vdisk_block_size != 0)
136987a7269eSachartre 		return (EINVAL);
137087a7269eSachartre 
137187a7269eSachartre 	/*
137287a7269eSachartre 	 * Build and execute the uscsi ioctl.  We build a group0, group1
137387a7269eSachartre 	 * or group4 command as necessary, since some targets
137487a7269eSachartre 	 * do not support group1 commands.
137587a7269eSachartre 	 */
137687a7269eSachartre 	while (nblk) {
137787a7269eSachartre 
137887a7269eSachartre 		bzero(&ucmd, sizeof (ucmd));
137987a7269eSachartre 		bzero(&cdb, sizeof (cdb));
138087a7269eSachartre 
138187a7269eSachartre 		nsectors = (max_sectors < nblk) ? max_sectors : nblk;
138287a7269eSachartre 
138317cadca8Slm66018 		/*
138417cadca8Slm66018 		 * Some of the optical drives on sun4v machines are ATAPI
138517cadca8Slm66018 		 * devices which use Group 1 Read/Write commands so we need
138617cadca8Slm66018 		 * to explicitly check a flag which is set when a domain
138717cadca8Slm66018 		 * is bound.
138817cadca8Slm66018 		 */
138917cadca8Slm66018 		if (blk < (2 << 20) && nsectors <= 0xff && !vd->is_atapi_dev) {
139087a7269eSachartre 			FORMG0ADDR(&cdb, blk);
1391342440ecSPrasad Singamsetty 			FORMG0COUNT(&cdb, (uchar_t)nsectors);
139287a7269eSachartre 			ucmd.uscsi_cdblen = CDB_GROUP0;
139387a7269eSachartre 		} else if (blk > 0xffffffff) {
139487a7269eSachartre 			FORMG4LONGADDR(&cdb, blk);
139587a7269eSachartre 			FORMG4COUNT(&cdb, nsectors);
139687a7269eSachartre 			ucmd.uscsi_cdblen = CDB_GROUP4;
139787a7269eSachartre 			cdb.scc_cmd |= SCMD_GROUP4;
139887a7269eSachartre 		} else {
139987a7269eSachartre 			FORMG1ADDR(&cdb, blk);
140087a7269eSachartre 			FORMG1COUNT(&cdb, nsectors);
140187a7269eSachartre 			ucmd.uscsi_cdblen = CDB_GROUP1;
140287a7269eSachartre 			cdb.scc_cmd |= SCMD_GROUP1;
140387a7269eSachartre 		}
140487a7269eSachartre 		ucmd.uscsi_cdb = (caddr_t)&cdb;
140587a7269eSachartre 		ucmd.uscsi_bufaddr = data;
140617cadca8Slm66018 		ucmd.uscsi_buflen = nsectors * vd->block_size;
140787a7269eSachartre 		ucmd.uscsi_timeout = vd_scsi_rdwr_timeout;
140887a7269eSachartre 		/*
140987a7269eSachartre 		 * Set flags so that the command is isolated from normal
141087a7269eSachartre 		 * commands and no error message is printed.
141187a7269eSachartre 		 */
141287a7269eSachartre 		ucmd.uscsi_flags = USCSI_ISOLATE | USCSI_SILENT;
141387a7269eSachartre 
141487a7269eSachartre 		if (operation == VD_OP_BREAD) {
141587a7269eSachartre 			cdb.scc_cmd |= SCMD_READ;
141687a7269eSachartre 			ucmd.uscsi_flags |= USCSI_READ;
141787a7269eSachartre 		} else {
141887a7269eSachartre 			cdb.scc_cmd |= SCMD_WRITE;
141987a7269eSachartre 		}
142087a7269eSachartre 
142187a7269eSachartre 		status = ldi_ioctl(vd->ldi_handle[VD_ENTIRE_DISK_SLICE],
1422047ba61eSachartre 		    USCSICMD, (intptr_t)&ucmd, (vd->open_flags | FKIOCTL),
142387a7269eSachartre 		    kcred, &rval);
142487a7269eSachartre 
142587a7269eSachartre 		if (status == 0)
142687a7269eSachartre 			status = ucmd.uscsi_status;
142787a7269eSachartre 
142887a7269eSachartre 		if (status != 0)
142987a7269eSachartre 			break;
143087a7269eSachartre 
143187a7269eSachartre 		/*
143287a7269eSachartre 		 * Check if partial DMA breakup is required. If so, reduce
143387a7269eSachartre 		 * the request size by half and retry the last request.
143487a7269eSachartre 		 */
143587a7269eSachartre 		if (ucmd.uscsi_resid == ucmd.uscsi_buflen) {
143687a7269eSachartre 			max_sectors >>= 1;
143787a7269eSachartre 			if (max_sectors <= 0) {
143887a7269eSachartre 				status = EIO;
143987a7269eSachartre 				break;
144087a7269eSachartre 			}
144187a7269eSachartre 			continue;
144287a7269eSachartre 		}
144387a7269eSachartre 
144487a7269eSachartre 		if (ucmd.uscsi_resid != 0) {
144587a7269eSachartre 			status = EIO;
144687a7269eSachartre 			break;
144787a7269eSachartre 		}
144887a7269eSachartre 
144987a7269eSachartre 		blk += nsectors;
145087a7269eSachartre 		nblk -= nsectors;
145117cadca8Slm66018 		data += nsectors * vd->vdisk_block_size; /* SECSIZE */
145287a7269eSachartre 	}
145387a7269eSachartre 
145487a7269eSachartre 	return (status);
145587a7269eSachartre }
145687a7269eSachartre 
1457205eeb1aSlm66018 /*
145817cadca8Slm66018  * Function:
145917cadca8Slm66018  *	vd_scsi_rdwr
146017cadca8Slm66018  *
146117cadca8Slm66018  * Description:
146217cadca8Slm66018  * 	Wrapper function to read or write to a SCSI disk using an absolute
146317cadca8Slm66018  *	disk offset. It checks the blocksize of the underlying device and,
146417cadca8Slm66018  *	if necessary, adjusts the buffers accordingly before calling
146517cadca8Slm66018  *	vd_do_scsi_rdwr() to do the actual read or write.
146617cadca8Slm66018  *
146717cadca8Slm66018  * Parameters:
146817cadca8Slm66018  *	vd		- disk on which the operation is performed.
146917cadca8Slm66018  *	operation	- operation to execute: read (VD_OP_BREAD) or
147017cadca8Slm66018  *			  write (VD_OP_BWRITE).
147117cadca8Slm66018  *	data		- buffer where data are read to or written from.
147217cadca8Slm66018  *	blk		- starting block for the operation.
147317cadca8Slm66018  *	len		- number of bytes to read or write.
147417cadca8Slm66018  *
147517cadca8Slm66018  * Return Code:
147617cadca8Slm66018  *	0		- success
147717cadca8Slm66018  *	n != 0		- error.
147817cadca8Slm66018  */
147917cadca8Slm66018 static int
148017cadca8Slm66018 vd_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t vblk, size_t vlen)
148117cadca8Slm66018 {
148217cadca8Slm66018 	int	rv;
148317cadca8Slm66018 
148417cadca8Slm66018 	size_t	pblk;	/* physical device block number of data on device */
148517cadca8Slm66018 	size_t	delta;	/* relative offset between pblk and vblk */
148617cadca8Slm66018 	size_t	pnblk;	/* number of physical blocks to be read from device */
148717cadca8Slm66018 	size_t	plen;	/* length of data to be read from physical device */
148817cadca8Slm66018 	char	*buf;	/* buffer area to fit physical device's block size */
148917cadca8Slm66018 
14902f5224aeSachartre 	if (vd->block_size == 0) {
14912f5224aeSachartre 		/*
14922f5224aeSachartre 		 * The block size was not available during the attach,
14932f5224aeSachartre 		 * try to update it now.
14942f5224aeSachartre 		 */
1495de3a5331SRamesh Chitrothu 		if (vd_backend_check_size(vd) != 0)
14962f5224aeSachartre 			return (EIO);
14972f5224aeSachartre 	}
14982f5224aeSachartre 
149917cadca8Slm66018 	/*
150017cadca8Slm66018 	 * If the vdisk block size and the block size of the underlying device
150117cadca8Slm66018 	 * match we can skip straight to vd_do_scsi_rdwr(), otherwise we need
150217cadca8Slm66018 	 * to create a buffer large enough to handle the device's block size
150317cadca8Slm66018 	 * and adjust the block to be read from and the amount of data to
150417cadca8Slm66018 	 * read to correspond with the device's block size.
150517cadca8Slm66018 	 */
150617cadca8Slm66018 	if (vd->vdisk_block_size == vd->block_size)
150717cadca8Slm66018 		return (vd_do_scsi_rdwr(vd, operation, data, vblk, vlen));
150817cadca8Slm66018 
150917cadca8Slm66018 	if (vd->vdisk_block_size > vd->block_size)
151017cadca8Slm66018 		return (EINVAL);
151117cadca8Slm66018 
151217cadca8Slm66018 	/*
151317cadca8Slm66018 	 * Writing of physical block sizes larger than the virtual block size
151417cadca8Slm66018 	 * is not supported. This would be added if/when support for guests
151517cadca8Slm66018 	 * writing to DVDs is implemented.
151617cadca8Slm66018 	 */
151717cadca8Slm66018 	if (operation == VD_OP_BWRITE)
151817cadca8Slm66018 		return (ENOTSUP);
151917cadca8Slm66018 
152017cadca8Slm66018 	/* BEGIN CSTYLED */
152117cadca8Slm66018 	/*
152217cadca8Slm66018 	 * Below is a diagram showing the relationship between the physical
152317cadca8Slm66018 	 * and virtual blocks. If the virtual blocks marked by 'X' below are
152417cadca8Slm66018 	 * requested, then the physical blocks denoted by 'Y' are read.
152517cadca8Slm66018 	 *
152617cadca8Slm66018 	 *           vblk
152717cadca8Slm66018 	 *             |      vlen
152817cadca8Slm66018 	 *             |<--------------->|
152917cadca8Slm66018 	 *             v                 v
153017cadca8Slm66018 	 *  --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-   virtual disk:
153117cadca8Slm66018 	 *    |  |  |  |XX|XX|XX|XX|XX|XX|  |  |  |  |  |  } block size is
153217cadca8Slm66018 	 *  --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-  vd->vdisk_block_size
153317cadca8Slm66018 	 *          :  :                 :  :
153417cadca8Slm66018 	 *         >:==:< delta          :  :
153517cadca8Slm66018 	 *          :  :                 :  :
153617cadca8Slm66018 	 *  --+-----+-----+-----+-----+-----+-----+-----+--   physical disk:
153717cadca8Slm66018 	 *    |     |YY:YY|YYYYY|YYYYY|YY:YY|     |     |   } block size is
153817cadca8Slm66018 	 *  --+-----+-----+-----+-----+-----+-----+-----+--   vd->block_size
153917cadca8Slm66018 	 *          ^                       ^
154017cadca8Slm66018 	 *          |<--------------------->|
154117cadca8Slm66018 	 *          |         plen
154217cadca8Slm66018 	 *         pblk
154317cadca8Slm66018 	 */
154417cadca8Slm66018 	/* END CSTYLED */
154517cadca8Slm66018 	pblk = (vblk * vd->vdisk_block_size) / vd->block_size;
154617cadca8Slm66018 	delta = (vblk * vd->vdisk_block_size) - (pblk * vd->block_size);
154717cadca8Slm66018 	pnblk = ((delta + vlen - 1) / vd->block_size) + 1;
154817cadca8Slm66018 	plen = pnblk * vd->block_size;
154917cadca8Slm66018 
155017cadca8Slm66018 	PR2("vblk %lx:pblk %lx: vlen %ld:plen %ld", vblk, pblk, vlen, plen);
155117cadca8Slm66018 
155217cadca8Slm66018 	buf = kmem_zalloc(sizeof (caddr_t) * plen, KM_SLEEP);
155317cadca8Slm66018 	rv = vd_do_scsi_rdwr(vd, operation, (caddr_t)buf, pblk, plen);
155417cadca8Slm66018 	bcopy(buf + delta, data, vlen);
155517cadca8Slm66018 
155617cadca8Slm66018 	kmem_free(buf, sizeof (caddr_t) * plen);
155717cadca8Slm66018 
155817cadca8Slm66018 	return (rv);
155917cadca8Slm66018 }
156017cadca8Slm66018 
156117cadca8Slm66018 /*
1562bae9e67eSachartre  * Function:
1563bae9e67eSachartre  *	vd_slice_flabel_read
1564bae9e67eSachartre  *
1565bae9e67eSachartre  * Description:
1566bae9e67eSachartre  *	This function simulates a read operation from the fake label of
1567bae9e67eSachartre  *	a single-slice disk.
1568bae9e67eSachartre  *
1569bae9e67eSachartre  * Parameters:
1570bae9e67eSachartre  *	vd		- single-slice disk to read from
1571bae9e67eSachartre  *	data		- buffer where data should be read to
1572bae9e67eSachartre  *	offset		- offset in byte where the read should start
1573bae9e67eSachartre  *	length		- number of bytes to read
1574bae9e67eSachartre  *
1575bae9e67eSachartre  * Return Code:
1576bae9e67eSachartre  *	n >= 0		- success, n indicates the number of bytes read
1577bae9e67eSachartre  *	-1		- error
1578bae9e67eSachartre  */
1579bae9e67eSachartre static ssize_t
1580bae9e67eSachartre vd_slice_flabel_read(vd_t *vd, caddr_t data, size_t offset, size_t length)
1581bae9e67eSachartre {
1582bae9e67eSachartre 	size_t n = 0;
1583bae9e67eSachartre 	uint_t limit = vd->flabel_limit * DEV_BSIZE;
1584bae9e67eSachartre 
1585bae9e67eSachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
1586bae9e67eSachartre 	ASSERT(vd->flabel != NULL);
1587bae9e67eSachartre 
1588bae9e67eSachartre 	/* if offset is past the fake label limit there's nothing to read */
1589bae9e67eSachartre 	if (offset >= limit)
1590bae9e67eSachartre 		return (0);
1591bae9e67eSachartre 
1592bae9e67eSachartre 	/* data with offset 0 to flabel_size are read from flabel */
1593bae9e67eSachartre 	if (offset < vd->flabel_size) {
1594bae9e67eSachartre 
1595bae9e67eSachartre 		if (offset + length <= vd->flabel_size) {
1596bae9e67eSachartre 			bcopy(vd->flabel + offset, data, length);
1597bae9e67eSachartre 			return (length);
1598bae9e67eSachartre 		}
1599bae9e67eSachartre 
1600bae9e67eSachartre 		n = vd->flabel_size - offset;
1601bae9e67eSachartre 		bcopy(vd->flabel + offset, data, n);
1602bae9e67eSachartre 		data += n;
1603bae9e67eSachartre 	}
1604bae9e67eSachartre 
1605bae9e67eSachartre 	/* data with offset from flabel_size to flabel_limit are all zeros */
1606bae9e67eSachartre 	if (offset + length <= limit) {
1607bae9e67eSachartre 		bzero(data, length - n);
1608bae9e67eSachartre 		return (length);
1609bae9e67eSachartre 	}
1610bae9e67eSachartre 
1611bae9e67eSachartre 	bzero(data, limit - offset - n);
1612bae9e67eSachartre 	return (limit - offset);
1613bae9e67eSachartre }
1614bae9e67eSachartre 
1615bae9e67eSachartre /*
1616bae9e67eSachartre  * Function:
1617bae9e67eSachartre  *	vd_slice_flabel_write
1618bae9e67eSachartre  *
1619bae9e67eSachartre  * Description:
1620bae9e67eSachartre  *	This function simulates a write operation to the fake label of
1621bae9e67eSachartre  *	a single-slice disk. Write operations are actually faked and return
1622bae9e67eSachartre  *	success although the label is never changed. This is mostly to
1623bae9e67eSachartre  *	simulate a successful label update.
1624bae9e67eSachartre  *
1625bae9e67eSachartre  * Parameters:
1626bae9e67eSachartre  *	vd		- single-slice disk to write to
1627bae9e67eSachartre  *	data		- buffer where data should be written from
1628bae9e67eSachartre  *	offset		- offset in byte where the write should start
1629bae9e67eSachartre  *	length		- number of bytes to written
1630bae9e67eSachartre  *
1631bae9e67eSachartre  * Return Code:
1632bae9e67eSachartre  *	n >= 0		- success, n indicates the number of bytes written
1633bae9e67eSachartre  *	-1		- error
1634bae9e67eSachartre  */
1635bae9e67eSachartre static ssize_t
1636bae9e67eSachartre vd_slice_flabel_write(vd_t *vd, caddr_t data, size_t offset, size_t length)
1637bae9e67eSachartre {
1638bae9e67eSachartre 	uint_t limit = vd->flabel_limit * DEV_BSIZE;
1639bae9e67eSachartre 	struct dk_label *label;
1640bae9e67eSachartre 	struct dk_geom geom;
1641342440ecSPrasad Singamsetty 	struct extvtoc vtoc;
1642bae9e67eSachartre 
1643bae9e67eSachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
1644bae9e67eSachartre 	ASSERT(vd->flabel != NULL);
1645bae9e67eSachartre 
1646bae9e67eSachartre 	if (offset >= limit)
1647bae9e67eSachartre 		return (0);
1648bae9e67eSachartre 
1649bae9e67eSachartre 	/*
1650bae9e67eSachartre 	 * If this is a request to overwrite the VTOC disk label, check that
1651bae9e67eSachartre 	 * the new label is similar to the previous one and return that the
1652bae9e67eSachartre 	 * write was successful, but note that nothing is actually overwritten.
1653bae9e67eSachartre 	 */
1654bae9e67eSachartre 	if (vd->vdisk_label == VD_DISK_LABEL_VTOC &&
1655bae9e67eSachartre 	    offset == 0 && length == DEV_BSIZE) {
1656342440ecSPrasad Singamsetty 		label = (void *)data;
1657bae9e67eSachartre 
1658bae9e67eSachartre 		/* check that this is a valid label */
1659bae9e67eSachartre 		if (label->dkl_magic != DKL_MAGIC ||
1660bae9e67eSachartre 		    label->dkl_cksum != vd_lbl2cksum(label))
1661bae9e67eSachartre 			return (-1);
1662bae9e67eSachartre 
1663bae9e67eSachartre 		/* check the vtoc and geometry */
1664bae9e67eSachartre 		vd_label_to_vtocgeom(label, &vtoc, &geom);
1665bae9e67eSachartre 		if (vd_slice_geom_isvalid(vd, &geom) &&
1666bae9e67eSachartre 		    vd_slice_vtoc_isvalid(vd, &vtoc))
1667bae9e67eSachartre 			return (length);
1668bae9e67eSachartre 	}
1669bae9e67eSachartre 
1670bae9e67eSachartre 	/* fail any other write */
1671bae9e67eSachartre 	return (-1);
1672bae9e67eSachartre }
1673bae9e67eSachartre 
1674bae9e67eSachartre /*
1675bae9e67eSachartre  * Function:
1676bae9e67eSachartre  *	vd_slice_fake_rdwr
1677bae9e67eSachartre  *
1678bae9e67eSachartre  * Description:
1679bae9e67eSachartre  *	This function simulates a raw read or write operation to a single-slice
1680bae9e67eSachartre  *	disk. It only handles the faked part of the operation i.e. I/Os to
1681bae9e67eSachartre  *	blocks which have no mapping with the vdisk backend (I/Os to the
1682bae9e67eSachartre  *	beginning and to the end of the vdisk).
1683bae9e67eSachartre  *
1684bae9e67eSachartre  *	The function returns 0 is the operation	is completed and it has been
1685bae9e67eSachartre  *	entirely handled as a fake read or write. In that case, lengthp points
1686bae9e67eSachartre  *	to the number of bytes not read or written. Values returned by datap
1687bae9e67eSachartre  *	and blkp are undefined.
1688bae9e67eSachartre  *
1689bae9e67eSachartre  *	If the fake operation has succeeded but the read or write is not
1690bae9e67eSachartre  *	complete (i.e. the read/write operation extends beyond the blocks
1691bae9e67eSachartre  *	we fake) then the function returns EAGAIN and datap, blkp and lengthp
1692bae9e67eSachartre  *	pointers points to the parameters for completing the operation.
1693bae9e67eSachartre  *
1694bae9e67eSachartre  *	In case of an error, for example if the slice is empty or parameters
1695bae9e67eSachartre  *	are invalid, then the function returns a non-zero value different
1696bae9e67eSachartre  *	from EAGAIN. In that case, the returned values of datap, blkp and
1697bae9e67eSachartre  *	lengthp are undefined.
1698bae9e67eSachartre  *
1699bae9e67eSachartre  * Parameters:
1700bae9e67eSachartre  *	vd		- single-slice disk on which the operation is performed
1701bae9e67eSachartre  *	slice		- slice on which the operation is performed,
1702bae9e67eSachartre  *			  VD_SLICE_NONE indicates that the operation
1703bae9e67eSachartre  *			  is done using an absolute disk offset.
1704bae9e67eSachartre  *	operation	- operation to execute: read (VD_OP_BREAD) or
1705bae9e67eSachartre  *			  write (VD_OP_BWRITE).
1706bae9e67eSachartre  *	datap		- pointer to the buffer where data are read to
1707bae9e67eSachartre  *			  or written from. Return the pointer where remaining
1708bae9e67eSachartre  *			  data have to be read to or written from.
1709bae9e67eSachartre  *	blkp		- pointer to the starting block for the operation.
1710bae9e67eSachartre  *			  Return the starting block relative to the vdisk
1711bae9e67eSachartre  *			  backend for the remaining operation.
1712bae9e67eSachartre  *	lengthp		- pointer to the number of bytes to read or write.
1713bae9e67eSachartre  *			  This should be a multiple of DEV_BSIZE. Return the
1714bae9e67eSachartre  *			  remaining number of bytes to read or write.
1715bae9e67eSachartre  *
1716bae9e67eSachartre  * Return Code:
1717bae9e67eSachartre  *	0		- read/write operation is completed
1718bae9e67eSachartre  *	EAGAIN		- read/write operation is not completed
1719bae9e67eSachartre  *	other values	- error
1720bae9e67eSachartre  */
1721bae9e67eSachartre static int
1722bae9e67eSachartre vd_slice_fake_rdwr(vd_t *vd, int slice, int operation, caddr_t *datap,
1723bae9e67eSachartre     size_t *blkp, size_t *lengthp)
1724bae9e67eSachartre {
1725bae9e67eSachartre 	struct dk_label *label;
1726bae9e67eSachartre 	caddr_t data;
1727bae9e67eSachartre 	size_t blk, length, csize;
1728bae9e67eSachartre 	size_t ablk, asize, aoff, alen;
1729bae9e67eSachartre 	ssize_t n;
1730bae9e67eSachartre 	int sec, status;
1731bae9e67eSachartre 
1732bae9e67eSachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
1733bae9e67eSachartre 	ASSERT(slice != 0);
1734bae9e67eSachartre 
1735bae9e67eSachartre 	data = *datap;
1736bae9e67eSachartre 	blk = *blkp;
1737bae9e67eSachartre 	length = *lengthp;
1738bae9e67eSachartre 
1739bae9e67eSachartre 	/*
1740bae9e67eSachartre 	 * If this is not a raw I/O or an I/O from a full disk slice then
1741bae9e67eSachartre 	 * this is an I/O to/from an empty slice.
1742bae9e67eSachartre 	 */
1743bae9e67eSachartre 	if (slice != VD_SLICE_NONE &&
1744bae9e67eSachartre 	    (slice != VD_ENTIRE_DISK_SLICE ||
1745bae9e67eSachartre 	    vd->vdisk_label != VD_DISK_LABEL_VTOC) &&
1746bae9e67eSachartre 	    (slice != VD_EFI_WD_SLICE ||
1747bae9e67eSachartre 	    vd->vdisk_label != VD_DISK_LABEL_EFI)) {
1748bae9e67eSachartre 		return (EIO);
1749bae9e67eSachartre 	}
1750bae9e67eSachartre 
1751bae9e67eSachartre 	if (length % DEV_BSIZE != 0)
1752bae9e67eSachartre 		return (EINVAL);
1753bae9e67eSachartre 
1754bae9e67eSachartre 	/* handle any I/O with the fake label */
1755bae9e67eSachartre 	if (operation == VD_OP_BWRITE)
1756bae9e67eSachartre 		n = vd_slice_flabel_write(vd, data, blk * DEV_BSIZE, length);
1757bae9e67eSachartre 	else
1758bae9e67eSachartre 		n = vd_slice_flabel_read(vd, data, blk * DEV_BSIZE, length);
1759bae9e67eSachartre 
1760bae9e67eSachartre 	if (n == -1)
1761bae9e67eSachartre 		return (EINVAL);
1762bae9e67eSachartre 
1763bae9e67eSachartre 	ASSERT(n % DEV_BSIZE == 0);
1764bae9e67eSachartre 
1765bae9e67eSachartre 	/* adjust I/O arguments */
1766bae9e67eSachartre 	data += n;
1767bae9e67eSachartre 	blk += n / DEV_BSIZE;
1768bae9e67eSachartre 	length -= n;
1769bae9e67eSachartre 
1770bae9e67eSachartre 	/* check if there's something else to process */
1771bae9e67eSachartre 	if (length == 0) {
1772bae9e67eSachartre 		status = 0;
1773bae9e67eSachartre 		goto done;
1774bae9e67eSachartre 	}
1775bae9e67eSachartre 
1776bae9e67eSachartre 	if (vd->vdisk_label == VD_DISK_LABEL_VTOC &&
1777bae9e67eSachartre 	    slice == VD_ENTIRE_DISK_SLICE) {
1778bae9e67eSachartre 		status = EAGAIN;
1779bae9e67eSachartre 		goto done;
1780bae9e67eSachartre 	}
1781bae9e67eSachartre 
1782bae9e67eSachartre 	if (vd->vdisk_label == VD_DISK_LABEL_EFI) {
1783bae9e67eSachartre 		asize = EFI_MIN_RESV_SIZE + 33;
1784bae9e67eSachartre 		ablk = vd->vdisk_size - asize;
1785bae9e67eSachartre 	} else {
1786bae9e67eSachartre 		ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
1787bae9e67eSachartre 		ASSERT(vd->dk_geom.dkg_apc == 0);
1788bae9e67eSachartre 
1789bae9e67eSachartre 		csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
1790bae9e67eSachartre 		ablk = vd->dk_geom.dkg_ncyl * csize;
1791bae9e67eSachartre 		asize = vd->dk_geom.dkg_acyl * csize;
1792bae9e67eSachartre 	}
1793bae9e67eSachartre 
1794bae9e67eSachartre 	alen = length / DEV_BSIZE;
1795bae9e67eSachartre 	aoff = blk;
1796bae9e67eSachartre 
1797bae9e67eSachartre 	/* if we have reached the last block then the I/O is completed */
1798bae9e67eSachartre 	if (aoff == ablk + asize) {
1799bae9e67eSachartre 		status = 0;
1800bae9e67eSachartre 		goto done;
1801bae9e67eSachartre 	}
1802bae9e67eSachartre 
1803bae9e67eSachartre 	/* if we are past the last block then return an error */
1804bae9e67eSachartre 	if (aoff > ablk + asize)
1805bae9e67eSachartre 		return (EIO);
1806bae9e67eSachartre 
1807bae9e67eSachartre 	/* check if there is any I/O to end of the disk */
1808bae9e67eSachartre 	if (aoff + alen < ablk) {
1809bae9e67eSachartre 		status = EAGAIN;
1810bae9e67eSachartre 		goto done;
1811bae9e67eSachartre 	}
1812bae9e67eSachartre 
1813bae9e67eSachartre 	/* we don't allow any write to the end of the disk */
1814bae9e67eSachartre 	if (operation == VD_OP_BWRITE)
1815bae9e67eSachartre 		return (EIO);
1816bae9e67eSachartre 
1817bae9e67eSachartre 	if (aoff < ablk) {
1818bae9e67eSachartre 		alen -= (ablk - aoff);
1819bae9e67eSachartre 		aoff = ablk;
1820bae9e67eSachartre 	}
1821bae9e67eSachartre 
1822bae9e67eSachartre 	if (aoff + alen > ablk + asize) {
1823bae9e67eSachartre 		alen = ablk + asize - aoff;
1824bae9e67eSachartre 	}
1825bae9e67eSachartre 
1826bae9e67eSachartre 	alen *= DEV_BSIZE;
1827bae9e67eSachartre 
1828bae9e67eSachartre 	if (operation == VD_OP_BREAD) {
1829bae9e67eSachartre 		bzero(data + (aoff - blk) * DEV_BSIZE, alen);
1830bae9e67eSachartre 
1831bae9e67eSachartre 		if (vd->vdisk_label == VD_DISK_LABEL_VTOC) {
1832bae9e67eSachartre 			/* check if we read backup labels */
1833bae9e67eSachartre 			label = VD_LABEL_VTOC(vd);
1834bae9e67eSachartre 			ablk += (label->dkl_acyl - 1) * csize +
1835bae9e67eSachartre 			    (label->dkl_nhead - 1) * label->dkl_nsect;
1836bae9e67eSachartre 
1837bae9e67eSachartre 			for (sec = 1; (sec < 5 * 2 + 1); sec += 2) {
1838bae9e67eSachartre 
1839bae9e67eSachartre 				if (ablk + sec >= blk &&
1840bae9e67eSachartre 				    ablk + sec < blk + (length / DEV_BSIZE)) {
1841bae9e67eSachartre 					bcopy(label, data +
1842bae9e67eSachartre 					    (ablk + sec - blk) * DEV_BSIZE,
1843bae9e67eSachartre 					    sizeof (struct dk_label));
1844bae9e67eSachartre 				}
1845bae9e67eSachartre 			}
1846bae9e67eSachartre 		}
1847bae9e67eSachartre 	}
1848bae9e67eSachartre 
1849bae9e67eSachartre 	length -= alen;
1850bae9e67eSachartre 
1851bae9e67eSachartre 	status = (length == 0)? 0: EAGAIN;
1852bae9e67eSachartre 
1853bae9e67eSachartre done:
1854bae9e67eSachartre 	ASSERT(length == 0 || blk >= vd->flabel_limit);
1855bae9e67eSachartre 
1856bae9e67eSachartre 	/*
1857bae9e67eSachartre 	 * Return the parameters for the remaining I/O. The starting block is
1858bae9e67eSachartre 	 * adjusted so that it is relative to the vdisk backend.
1859bae9e67eSachartre 	 */
1860bae9e67eSachartre 	*datap = data;
1861bae9e67eSachartre 	*blkp = blk - vd->flabel_limit;
1862bae9e67eSachartre 	*lengthp = length;
1863bae9e67eSachartre 
1864bae9e67eSachartre 	return (status);
1865bae9e67eSachartre }
1866bae9e67eSachartre 
1867bae9e67eSachartre /*
1868*1aff8f07SAlexandre Chartre  * We define our own biodone function so that buffers used for
1869*1aff8f07SAlexandre Chartre  * asynchronous writes are not released when biodone() is called.
1870*1aff8f07SAlexandre Chartre  */
1871*1aff8f07SAlexandre Chartre static int
1872*1aff8f07SAlexandre Chartre vd_biodone(struct buf *bp)
1873*1aff8f07SAlexandre Chartre {
1874*1aff8f07SAlexandre Chartre 	ASSERT((bp->b_flags & B_DONE) == 0);
1875*1aff8f07SAlexandre Chartre 	ASSERT(SEMA_HELD(&bp->b_sem));
1876*1aff8f07SAlexandre Chartre 
1877*1aff8f07SAlexandre Chartre 	bp->b_flags |= B_DONE;
1878*1aff8f07SAlexandre Chartre 	if (!(bp->b_flags & B_ASYNC))
1879*1aff8f07SAlexandre Chartre 		sema_v(&bp->b_io);
1880*1aff8f07SAlexandre Chartre 
1881*1aff8f07SAlexandre Chartre 	return (0);
1882*1aff8f07SAlexandre Chartre }
1883*1aff8f07SAlexandre Chartre 
1884*1aff8f07SAlexandre Chartre /*
1885205eeb1aSlm66018  * Return Values
1886205eeb1aSlm66018  *	EINPROGRESS	- operation was successfully started
1887205eeb1aSlm66018  *	EIO		- encountered LDC (aka. task error)
1888205eeb1aSlm66018  *	0		- operation completed successfully
1889205eeb1aSlm66018  *
1890205eeb1aSlm66018  * Side Effect
1891205eeb1aSlm66018  *     sets request->status = <disk operation status>
1892205eeb1aSlm66018  */
18931ae08745Sheppo static int
1894d10e4ef2Snarayan vd_start_bio(vd_task_t *task)
18951ae08745Sheppo {
18964bac2208Snarayan 	int			rv, status = 0;
1897d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
1898d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
1899d10e4ef2Snarayan 	struct buf		*buf		= &task->buf;
19004bac2208Snarayan 	uint8_t			mtype;
19013c96341aSnarayan 	int 			slice;
1902047ba61eSachartre 	char			*bufaddr = 0;
1903047ba61eSachartre 	size_t			buflen;
1904bae9e67eSachartre 	size_t			offset, length, nbytes;
1905d10e4ef2Snarayan 
1906d10e4ef2Snarayan 	ASSERT(vd != NULL);
1907d10e4ef2Snarayan 	ASSERT(request != NULL);
19083c96341aSnarayan 
19093c96341aSnarayan 	slice = request->slice;
19103c96341aSnarayan 
191187a7269eSachartre 	ASSERT(slice == VD_SLICE_NONE || slice < vd->nslices);
1912d10e4ef2Snarayan 	ASSERT((request->operation == VD_OP_BREAD) ||
1913d10e4ef2Snarayan 	    (request->operation == VD_OP_BWRITE));
1914d10e4ef2Snarayan 
1915205eeb1aSlm66018 	if (request->nbytes == 0) {
1916205eeb1aSlm66018 		/* no service for trivial requests */
1917205eeb1aSlm66018 		request->status = EINVAL;
1918205eeb1aSlm66018 		return (0);
1919205eeb1aSlm66018 	}
19201ae08745Sheppo 
1921d10e4ef2Snarayan 	PR1("%s %lu bytes at block %lu",
1922d10e4ef2Snarayan 	    (request->operation == VD_OP_BREAD) ? "Read" : "Write",
1923d10e4ef2Snarayan 	    request->nbytes, request->addr);
19241ae08745Sheppo 
1925047ba61eSachartre 	/*
1926047ba61eSachartre 	 * We have to check the open flags because the functions processing
1927047ba61eSachartre 	 * the read/write request will not do it.
1928047ba61eSachartre 	 */
1929047ba61eSachartre 	if (request->operation == VD_OP_BWRITE && !(vd->open_flags & FWRITE)) {
1930047ba61eSachartre 		PR0("write fails because backend is opened read-only");
1931047ba61eSachartre 		request->nbytes = 0;
1932047ba61eSachartre 		request->status = EROFS;
1933047ba61eSachartre 		return (0);
1934047ba61eSachartre 	}
1935d10e4ef2Snarayan 
19364bac2208Snarayan 	mtype = (&vd->inband_task == task) ? LDC_SHADOW_MAP : LDC_DIRECT_MAP;
19374bac2208Snarayan 
19384bac2208Snarayan 	/* Map memory exported by client */
19394bac2208Snarayan 	status = ldc_mem_map(task->mhdl, request->cookie, request->ncookies,
19404bac2208Snarayan 	    mtype, (request->operation == VD_OP_BREAD) ? LDC_MEM_W : LDC_MEM_R,
1941047ba61eSachartre 	    &bufaddr, NULL);
19424bac2208Snarayan 	if (status != 0) {
19433af08d82Slm66018 		PR0("ldc_mem_map() returned err %d ", status);
1944205eeb1aSlm66018 		return (EIO);
1945d10e4ef2Snarayan 	}
1946d10e4ef2Snarayan 
1947bae9e67eSachartre 	/*
1948bae9e67eSachartre 	 * The buffer size has to be 8-byte aligned, so the client should have
1949bae9e67eSachartre 	 * sent a buffer which size is roundup to the next 8-byte aligned value.
1950bae9e67eSachartre 	 */
1951bae9e67eSachartre 	buflen = P2ROUNDUP(request->nbytes, 8);
1952047ba61eSachartre 
1953047ba61eSachartre 	status = ldc_mem_acquire(task->mhdl, 0, buflen);
19544bac2208Snarayan 	if (status != 0) {
19554bac2208Snarayan 		(void) ldc_mem_unmap(task->mhdl);
19563af08d82Slm66018 		PR0("ldc_mem_acquire() returned err %d ", status);
1957205eeb1aSlm66018 		return (EIO);
19584bac2208Snarayan 	}
19594bac2208Snarayan 
1960bae9e67eSachartre 	offset = request->addr;
1961bae9e67eSachartre 	nbytes = request->nbytes;
1962bae9e67eSachartre 	length = nbytes;
1963bae9e67eSachartre 
1964bae9e67eSachartre 	/* default number of byte returned by the I/O */
1965bae9e67eSachartre 	request->nbytes = 0;
1966bae9e67eSachartre 
1967bae9e67eSachartre 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
1968bae9e67eSachartre 
1969bae9e67eSachartre 		if (slice != 0) {
1970bae9e67eSachartre 			/* handle any fake I/O */
1971bae9e67eSachartre 			rv = vd_slice_fake_rdwr(vd, slice, request->operation,
1972bae9e67eSachartre 			    &bufaddr, &offset, &length);
1973bae9e67eSachartre 
1974bae9e67eSachartre 			/* record the number of bytes from the fake I/O */
1975bae9e67eSachartre 			request->nbytes = nbytes - length;
1976bae9e67eSachartre 
1977bae9e67eSachartre 			if (rv == 0) {
1978bae9e67eSachartre 				request->status = 0;
1979bae9e67eSachartre 				goto io_done;
1980bae9e67eSachartre 			}
1981bae9e67eSachartre 
1982bae9e67eSachartre 			if (rv != EAGAIN) {
19833c96341aSnarayan 				request->nbytes = 0;
1984205eeb1aSlm66018 				request->status = EIO;
1985bae9e67eSachartre 				goto io_done;
19863c96341aSnarayan 			}
1987bae9e67eSachartre 
1988bae9e67eSachartre 			/*
1989bae9e67eSachartre 			 * If we return with EAGAIN then this means that there
1990bae9e67eSachartre 			 * are still data to read or write.
1991bae9e67eSachartre 			 */
1992bae9e67eSachartre 			ASSERT(length != 0);
1993bae9e67eSachartre 
1994bae9e67eSachartre 			/*
1995bae9e67eSachartre 			 * We need to continue the I/O from the slice backend to
1996bae9e67eSachartre 			 * complete the request. The variables bufaddr, offset
1997bae9e67eSachartre 			 * and length have been adjusted to have the right
1998bae9e67eSachartre 			 * information to do the remaining I/O from the backend.
1999bae9e67eSachartre 			 * The backend is entirely mapped to slice 0 so we just
2000bae9e67eSachartre 			 * have to complete the I/O from that slice.
2001bae9e67eSachartre 			 */
2002bae9e67eSachartre 			slice = 0;
2003bae9e67eSachartre 		}
2004bae9e67eSachartre 
2005*1aff8f07SAlexandre Chartre 	} else if (vd->volume) {
2006*1aff8f07SAlexandre Chartre 
2007*1aff8f07SAlexandre Chartre 		rv = vd_dskimg_io_params(vd, slice, &offset, &length);
2008*1aff8f07SAlexandre Chartre 		if (rv != 0) {
2009*1aff8f07SAlexandre Chartre 			request->status = (rv == ENODATA)? 0: EIO;
2010*1aff8f07SAlexandre Chartre 			goto io_done;
2011*1aff8f07SAlexandre Chartre 		}
2012*1aff8f07SAlexandre Chartre 		slice = 0;
2013*1aff8f07SAlexandre Chartre 
2014*1aff8f07SAlexandre Chartre 	} else if ((slice == VD_SLICE_NONE) && !vd->file) {
2015bae9e67eSachartre 
201687a7269eSachartre 		/*
201787a7269eSachartre 		 * This is not a disk image so it is a real disk. We
201887a7269eSachartre 		 * assume that the underlying device driver supports
201987a7269eSachartre 		 * USCSICMD ioctls. This is the case of all SCSI devices
202087a7269eSachartre 		 * (sd, ssd...).
202187a7269eSachartre 		 *
202287a7269eSachartre 		 * In the future if we have non-SCSI disks we would need
202387a7269eSachartre 		 * to invoke the appropriate function to do I/O using an
202417cadca8Slm66018 		 * absolute disk offset (for example using DIOCTL_RWCMD
202587a7269eSachartre 		 * for IDE disks).
202687a7269eSachartre 		 */
2027bae9e67eSachartre 		rv = vd_scsi_rdwr(vd, request->operation, bufaddr, offset,
2028bae9e67eSachartre 		    length);
202987a7269eSachartre 		if (rv != 0) {
2030bae9e67eSachartre 			request->status = EIO;
2031bae9e67eSachartre 		} else {
2032bae9e67eSachartre 			request->nbytes = length;
2033bae9e67eSachartre 			request->status = 0;
2034bae9e67eSachartre 		}
2035bae9e67eSachartre 		goto io_done;
2036bae9e67eSachartre 	}
2037bae9e67eSachartre 
2038bae9e67eSachartre 	/* Start the block I/O */
2039bae9e67eSachartre 	if (vd->file) {
2040*1aff8f07SAlexandre Chartre 		rv = vd_dskimg_rw(vd, slice, request->operation, bufaddr,
2041*1aff8f07SAlexandre Chartre 		    offset, length);
2042bae9e67eSachartre 		if (rv < 0) {
204387a7269eSachartre 			request->nbytes = 0;
2044205eeb1aSlm66018 			request->status = EIO;
204587a7269eSachartre 		} else {
2046bae9e67eSachartre 			request->nbytes += rv;
2047205eeb1aSlm66018 			request->status = 0;
204887a7269eSachartre 		}
204987a7269eSachartre 	} else {
2050047ba61eSachartre 		bioinit(buf);
2051047ba61eSachartre 		buf->b_flags	= B_BUSY;
2052bae9e67eSachartre 		buf->b_bcount	= length;
2053bae9e67eSachartre 		buf->b_lblkno	= offset;
2054bae9e67eSachartre 		buf->b_bufsize	= buflen;
2055047ba61eSachartre 		buf->b_edev 	= vd->dev[slice];
2056047ba61eSachartre 		buf->b_un.b_addr = bufaddr;
2057*1aff8f07SAlexandre Chartre 		buf->b_iodone	= vd_biodone;
2058*1aff8f07SAlexandre Chartre 
2059*1aff8f07SAlexandre Chartre 		if (request->operation == VD_OP_BREAD) {
2060*1aff8f07SAlexandre Chartre 			buf->b_flags |= B_READ;
2061*1aff8f07SAlexandre Chartre 		} else {
2062*1aff8f07SAlexandre Chartre 			/*
2063*1aff8f07SAlexandre Chartre 			 * If we have a ZFS volume then we do an
2064*1aff8f07SAlexandre Chartre 			 * asynchronous write and we will wait for the
2065*1aff8f07SAlexandre Chartre 			 * completion of the write in vd_complete_bio()
2066*1aff8f07SAlexandre Chartre 			 * using the DKIOCFLUSHWRITECACHE ioctl. We
2067*1aff8f07SAlexandre Chartre 			 * do so for performance reason because, for a
2068*1aff8f07SAlexandre Chartre 			 * synchronous write, the ZFS volume strategy()
2069*1aff8f07SAlexandre Chartre 			 * function would only return after the write is
2070*1aff8f07SAlexandre Chartre 			 * commited and this prevents starting multiple
2071*1aff8f07SAlexandre Chartre 			 * writes in parallel.
2072*1aff8f07SAlexandre Chartre 			 */
2073*1aff8f07SAlexandre Chartre 			if (vd->zvol)
2074*1aff8f07SAlexandre Chartre 				buf->b_flags |= B_WRITE | B_ASYNC;
2075*1aff8f07SAlexandre Chartre 			else
2076*1aff8f07SAlexandre Chartre 				buf->b_flags |= B_WRITE;
2077*1aff8f07SAlexandre Chartre 		}
2078047ba61eSachartre 
2079bae9e67eSachartre 		request->status = ldi_strategy(vd->ldi_handle[slice], buf);
2080205eeb1aSlm66018 
2081205eeb1aSlm66018 		/*
2082205eeb1aSlm66018 		 * This is to indicate to the caller that the request
2083205eeb1aSlm66018 		 * needs to be finished by vd_complete_bio() by calling
2084205eeb1aSlm66018 		 * biowait() there and waiting for that to return before
2085205eeb1aSlm66018 		 * triggering the notification of the vDisk client.
2086205eeb1aSlm66018 		 *
2087205eeb1aSlm66018 		 * This is necessary when writing to real disks as
2088205eeb1aSlm66018 		 * otherwise calls to ldi_strategy() would be serialized
2089205eeb1aSlm66018 		 * behind the calls to biowait() and performance would
2090205eeb1aSlm66018 		 * suffer.
2091205eeb1aSlm66018 		 */
2092205eeb1aSlm66018 		if (request->status == 0)
209387a7269eSachartre 			return (EINPROGRESS);
2094047ba61eSachartre 
2095047ba61eSachartre 		biofini(buf);
209687a7269eSachartre 	}
20973c96341aSnarayan 
2098bae9e67eSachartre io_done:
2099bae9e67eSachartre 	/* Clean up after error or completion */
2100047ba61eSachartre 	rv = ldc_mem_release(task->mhdl, 0, buflen);
21014bac2208Snarayan 	if (rv) {
21023af08d82Slm66018 		PR0("ldc_mem_release() returned err %d ", rv);
2103205eeb1aSlm66018 		status = EIO;
21044bac2208Snarayan 	}
21054bac2208Snarayan 	rv = ldc_mem_unmap(task->mhdl);
21064bac2208Snarayan 	if (rv) {
2107205eeb1aSlm66018 		PR0("ldc_mem_unmap() returned err %d ", rv);
2108205eeb1aSlm66018 		status = EIO;
21094bac2208Snarayan 	}
21104bac2208Snarayan 
2111d10e4ef2Snarayan 	return (status);
2112d10e4ef2Snarayan }
2113d10e4ef2Snarayan 
2114205eeb1aSlm66018 /*
2115205eeb1aSlm66018  * This function should only be called from vd_notify to ensure that requests
2116205eeb1aSlm66018  * are responded to in the order that they are received.
2117205eeb1aSlm66018  */
2118d10e4ef2Snarayan static int
2119d10e4ef2Snarayan send_msg(ldc_handle_t ldc_handle, void *msg, size_t msglen)
2120d10e4ef2Snarayan {
21213af08d82Slm66018 	int	status;
2122d10e4ef2Snarayan 	size_t	nbytes;
2123d10e4ef2Snarayan 
21243af08d82Slm66018 	do {
2125d10e4ef2Snarayan 		nbytes = msglen;
2126d10e4ef2Snarayan 		status = ldc_write(ldc_handle, msg, &nbytes);
21273af08d82Slm66018 		if (status != EWOULDBLOCK)
21283af08d82Slm66018 			break;
21293af08d82Slm66018 		drv_usecwait(vds_ldc_delay);
21303af08d82Slm66018 	} while (status == EWOULDBLOCK);
2131d10e4ef2Snarayan 
2132d10e4ef2Snarayan 	if (status != 0) {
21333af08d82Slm66018 		if (status != ECONNRESET)
21343af08d82Slm66018 			PR0("ldc_write() returned errno %d", status);
2135d10e4ef2Snarayan 		return (status);
2136d10e4ef2Snarayan 	} else if (nbytes != msglen) {
21373af08d82Slm66018 		PR0("ldc_write() performed only partial write");
2138d10e4ef2Snarayan 		return (EIO);
2139d10e4ef2Snarayan 	}
2140d10e4ef2Snarayan 
2141d10e4ef2Snarayan 	PR1("SENT %lu bytes", msglen);
2142d10e4ef2Snarayan 	return (0);
2143d10e4ef2Snarayan }
2144d10e4ef2Snarayan 
2145d10e4ef2Snarayan static void
2146d10e4ef2Snarayan vd_need_reset(vd_t *vd, boolean_t reset_ldc)
2147d10e4ef2Snarayan {
2148d10e4ef2Snarayan 	mutex_enter(&vd->lock);
2149d10e4ef2Snarayan 	vd->reset_state	= B_TRUE;
2150d10e4ef2Snarayan 	vd->reset_ldc	= reset_ldc;
2151d10e4ef2Snarayan 	mutex_exit(&vd->lock);
2152d10e4ef2Snarayan }
2153d10e4ef2Snarayan 
2154d10e4ef2Snarayan /*
2155d10e4ef2Snarayan  * Reset the state of the connection with a client, if needed; reset the LDC
2156d10e4ef2Snarayan  * transport as well, if needed.  This function should only be called from the
21573af08d82Slm66018  * "vd_recv_msg", as it waits for tasks - otherwise a deadlock can occur.
2158d10e4ef2Snarayan  */
2159d10e4ef2Snarayan static void
2160d10e4ef2Snarayan vd_reset_if_needed(vd_t *vd)
2161d10e4ef2Snarayan {
2162d10e4ef2Snarayan 	int	status = 0;
2163d10e4ef2Snarayan 
2164d10e4ef2Snarayan 	mutex_enter(&vd->lock);
2165d10e4ef2Snarayan 	if (!vd->reset_state) {
2166d10e4ef2Snarayan 		ASSERT(!vd->reset_ldc);
2167d10e4ef2Snarayan 		mutex_exit(&vd->lock);
2168d10e4ef2Snarayan 		return;
2169d10e4ef2Snarayan 	}
2170d10e4ef2Snarayan 	mutex_exit(&vd->lock);
2171d10e4ef2Snarayan 
2172d10e4ef2Snarayan 	PR0("Resetting connection state with %s", VD_CLIENT(vd));
2173d10e4ef2Snarayan 
2174d10e4ef2Snarayan 	/*
2175d10e4ef2Snarayan 	 * Let any asynchronous I/O complete before possibly pulling the rug
2176d10e4ef2Snarayan 	 * out from under it; defer checking vd->reset_ldc, as one of the
2177d10e4ef2Snarayan 	 * asynchronous tasks might set it
2178d10e4ef2Snarayan 	 */
2179d10e4ef2Snarayan 	ddi_taskq_wait(vd->completionq);
2180d10e4ef2Snarayan 
21813c96341aSnarayan 	if (vd->file) {
2182da6c28aaSamw 		status = VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL);
21833c96341aSnarayan 		if (status) {
21843c96341aSnarayan 			PR0("VOP_FSYNC returned errno %d", status);
21853c96341aSnarayan 		}
21863c96341aSnarayan 	}
21873c96341aSnarayan 
2188d10e4ef2Snarayan 	if ((vd->initialized & VD_DRING) &&
2189d10e4ef2Snarayan 	    ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0))
21903af08d82Slm66018 		PR0("ldc_mem_dring_unmap() returned errno %d", status);
2191d10e4ef2Snarayan 
21923af08d82Slm66018 	vd_free_dring_task(vd);
21933af08d82Slm66018 
21943af08d82Slm66018 	/* Free the staging buffer for msgs */
21953af08d82Slm66018 	if (vd->vio_msgp != NULL) {
21963af08d82Slm66018 		kmem_free(vd->vio_msgp, vd->max_msglen);
21973af08d82Slm66018 		vd->vio_msgp = NULL;
2198d10e4ef2Snarayan 	}
2199d10e4ef2Snarayan 
22003af08d82Slm66018 	/* Free the inband message buffer */
22013af08d82Slm66018 	if (vd->inband_task.msg != NULL) {
22023af08d82Slm66018 		kmem_free(vd->inband_task.msg, vd->max_msglen);
22033af08d82Slm66018 		vd->inband_task.msg = NULL;
22043af08d82Slm66018 	}
2205d10e4ef2Snarayan 
2206d10e4ef2Snarayan 	mutex_enter(&vd->lock);
22073af08d82Slm66018 
22083af08d82Slm66018 	if (vd->reset_ldc)
22093af08d82Slm66018 		PR0("taking down LDC channel");
2210e1ebb9ecSlm66018 	if (vd->reset_ldc && ((status = ldc_down(vd->ldc_handle)) != 0))
22113af08d82Slm66018 		PR0("ldc_down() returned errno %d", status);
2212d10e4ef2Snarayan 
22132f5224aeSachartre 	/* Reset exclusive access rights */
22142f5224aeSachartre 	vd_reset_access(vd);
22152f5224aeSachartre 
2216d10e4ef2Snarayan 	vd->initialized	&= ~(VD_SID | VD_SEQ_NUM | VD_DRING);
2217d10e4ef2Snarayan 	vd->state	= VD_STATE_INIT;
2218d10e4ef2Snarayan 	vd->max_msglen	= sizeof (vio_msg_t);	/* baseline vio message size */
2219d10e4ef2Snarayan 
22203af08d82Slm66018 	/* Allocate the staging buffer */
22213af08d82Slm66018 	vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
22223af08d82Slm66018 
22233af08d82Slm66018 	PR0("calling ldc_up\n");
22243af08d82Slm66018 	(void) ldc_up(vd->ldc_handle);
22253af08d82Slm66018 
2226d10e4ef2Snarayan 	vd->reset_state	= B_FALSE;
2227d10e4ef2Snarayan 	vd->reset_ldc	= B_FALSE;
22283af08d82Slm66018 
2229d10e4ef2Snarayan 	mutex_exit(&vd->lock);
2230d10e4ef2Snarayan }
2231d10e4ef2Snarayan 
22323af08d82Slm66018 static void vd_recv_msg(void *arg);
22333af08d82Slm66018 
22343af08d82Slm66018 static void
22353af08d82Slm66018 vd_mark_in_reset(vd_t *vd)
22363af08d82Slm66018 {
22373af08d82Slm66018 	int status;
22383af08d82Slm66018 
22393af08d82Slm66018 	PR0("vd_mark_in_reset: marking vd in reset\n");
22403af08d82Slm66018 
22413af08d82Slm66018 	vd_need_reset(vd, B_FALSE);
22423af08d82Slm66018 	status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, DDI_SLEEP);
22433af08d82Slm66018 	if (status == DDI_FAILURE) {
22443af08d82Slm66018 		PR0("cannot schedule task to recv msg\n");
22453af08d82Slm66018 		vd_need_reset(vd, B_TRUE);
22463af08d82Slm66018 		return;
22473af08d82Slm66018 	}
22483af08d82Slm66018 }
22493af08d82Slm66018 
2250d10e4ef2Snarayan static int
22513c96341aSnarayan vd_mark_elem_done(vd_t *vd, int idx, int elem_status, int elem_nbytes)
2252d10e4ef2Snarayan {
2253d10e4ef2Snarayan 	boolean_t		accepted;
2254d10e4ef2Snarayan 	int			status;
2255bbfa0259Sha137994 	on_trap_data_t		otd;
2256d10e4ef2Snarayan 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
2257d10e4ef2Snarayan 
22583af08d82Slm66018 	if (vd->reset_state)
22593af08d82Slm66018 		return (0);
2260d10e4ef2Snarayan 
2261d10e4ef2Snarayan 	/* Acquire the element */
2262bbfa0259Sha137994 	if ((status = VIO_DRING_ACQUIRE(&otd, vd->dring_mtype,
2263bbfa0259Sha137994 	    vd->dring_handle, idx, idx)) != 0) {
22643af08d82Slm66018 		if (status == ECONNRESET) {
22653af08d82Slm66018 			vd_mark_in_reset(vd);
22663af08d82Slm66018 			return (0);
22673af08d82Slm66018 		} else {
2268d10e4ef2Snarayan 			return (status);
2269d10e4ef2Snarayan 		}
22703af08d82Slm66018 	}
2271d10e4ef2Snarayan 
2272d10e4ef2Snarayan 	/* Set the element's status and mark it done */
2273d10e4ef2Snarayan 	accepted = (elem->hdr.dstate == VIO_DESC_ACCEPTED);
2274d10e4ef2Snarayan 	if (accepted) {
22753c96341aSnarayan 		elem->payload.nbytes	= elem_nbytes;
2276d10e4ef2Snarayan 		elem->payload.status	= elem_status;
2277d10e4ef2Snarayan 		elem->hdr.dstate	= VIO_DESC_DONE;
2278d10e4ef2Snarayan 	} else {
2279d10e4ef2Snarayan 		/* Perhaps client timed out waiting for I/O... */
22803af08d82Slm66018 		PR0("element %u no longer \"accepted\"", idx);
2281d10e4ef2Snarayan 		VD_DUMP_DRING_ELEM(elem);
2282d10e4ef2Snarayan 	}
2283d10e4ef2Snarayan 	/* Release the element */
2284bbfa0259Sha137994 	if ((status = VIO_DRING_RELEASE(vd->dring_mtype,
2285bbfa0259Sha137994 	    vd->dring_handle, idx, idx)) != 0) {
22863af08d82Slm66018 		if (status == ECONNRESET) {
22873af08d82Slm66018 			vd_mark_in_reset(vd);
22883af08d82Slm66018 			return (0);
22893af08d82Slm66018 		} else {
2290bbfa0259Sha137994 			PR0("VIO_DRING_RELEASE() returned errno %d",
22913af08d82Slm66018 			    status);
2292d10e4ef2Snarayan 			return (status);
2293d10e4ef2Snarayan 		}
22943af08d82Slm66018 	}
2295d10e4ef2Snarayan 
2296d10e4ef2Snarayan 	return (accepted ? 0 : EINVAL);
2297d10e4ef2Snarayan }
2298d10e4ef2Snarayan 
2299205eeb1aSlm66018 /*
2300205eeb1aSlm66018  * Return Values
2301205eeb1aSlm66018  *	0	- operation completed successfully
2302205eeb1aSlm66018  *	EIO	- encountered LDC / task error
2303205eeb1aSlm66018  *
2304205eeb1aSlm66018  * Side Effect
2305205eeb1aSlm66018  *	sets request->status = <disk operation status>
2306205eeb1aSlm66018  */
2307205eeb1aSlm66018 static int
2308205eeb1aSlm66018 vd_complete_bio(vd_task_t *task)
2309d10e4ef2Snarayan {
2310d10e4ef2Snarayan 	int			status		= 0;
2311205eeb1aSlm66018 	int			rv		= 0;
2312d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
2313d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
2314d10e4ef2Snarayan 	struct buf		*buf		= &task->buf;
2315*1aff8f07SAlexandre Chartre 	int			rval;
2316d10e4ef2Snarayan 
2317d10e4ef2Snarayan 
2318d10e4ef2Snarayan 	ASSERT(vd != NULL);
2319d10e4ef2Snarayan 	ASSERT(request != NULL);
2320d10e4ef2Snarayan 	ASSERT(task->msg != NULL);
2321d10e4ef2Snarayan 	ASSERT(task->msglen >= sizeof (*task->msg));
23223c96341aSnarayan 	ASSERT(!vd->file);
2323bae9e67eSachartre 	ASSERT(request->slice != VD_SLICE_NONE || (!vd_slice_single_slice &&
2324*1aff8f07SAlexandre Chartre 	    vd->vdisk_type == VD_DISK_TYPE_SLICE) || vd->volume);
2325d10e4ef2Snarayan 
2326*1aff8f07SAlexandre Chartre 	if (vd->zvol && request->operation == VD_OP_BWRITE) {
2327*1aff8f07SAlexandre Chartre 		/*
2328*1aff8f07SAlexandre Chartre 		 * For a ZFS volume, we use asynchronous writes so we have to
2329*1aff8f07SAlexandre Chartre 		 * ensure that writes have been commited before marking the
2330*1aff8f07SAlexandre Chartre 		 * I/O as completed.
2331*1aff8f07SAlexandre Chartre 		 */
2332*1aff8f07SAlexandre Chartre 		request->status = ldi_ioctl(vd->ldi_handle[0],
2333*1aff8f07SAlexandre Chartre 		    DKIOCFLUSHWRITECACHE, NULL, vd->open_flags | FKIOCTL,
2334*1aff8f07SAlexandre Chartre 		    kcred, &rval);
2335*1aff8f07SAlexandre Chartre 	} else {
2336205eeb1aSlm66018 		/* Wait for the I/O to complete [ call to ldi_strategy(9f) ] */
2337d10e4ef2Snarayan 		request->status = biowait(buf);
2338*1aff8f07SAlexandre Chartre 	}
2339d10e4ef2Snarayan 
2340bae9e67eSachartre 	/* Update the number of bytes read/written */
2341bae9e67eSachartre 	request->nbytes += buf->b_bcount - buf->b_resid;
23423c96341aSnarayan 
23434bac2208Snarayan 	/* Release the buffer */
23443af08d82Slm66018 	if (!vd->reset_state)
2345bae9e67eSachartre 		status = ldc_mem_release(task->mhdl, 0, buf->b_bufsize);
23464bac2208Snarayan 	if (status) {
23473af08d82Slm66018 		PR0("ldc_mem_release() returned errno %d copying to "
23483af08d82Slm66018 		    "client", status);
23493af08d82Slm66018 		if (status == ECONNRESET) {
23503af08d82Slm66018 			vd_mark_in_reset(vd);
23513af08d82Slm66018 		}
2352205eeb1aSlm66018 		rv = EIO;
23531ae08745Sheppo 	}
23541ae08745Sheppo 
23553af08d82Slm66018 	/* Unmap the memory, even if in reset */
23564bac2208Snarayan 	status = ldc_mem_unmap(task->mhdl);
23574bac2208Snarayan 	if (status) {
23583af08d82Slm66018 		PR0("ldc_mem_unmap() returned errno %d copying to client",
23594bac2208Snarayan 		    status);
23603af08d82Slm66018 		if (status == ECONNRESET) {
23613af08d82Slm66018 			vd_mark_in_reset(vd);
23623af08d82Slm66018 		}
2363205eeb1aSlm66018 		rv = EIO;
23644bac2208Snarayan 	}
23654bac2208Snarayan 
2366d10e4ef2Snarayan 	biofini(buf);
23671ae08745Sheppo 
2368205eeb1aSlm66018 	return (rv);
2369205eeb1aSlm66018 }
2370205eeb1aSlm66018 
2371205eeb1aSlm66018 /*
2372205eeb1aSlm66018  * Description:
2373205eeb1aSlm66018  *	This function is called by the two functions called by a taskq
2374205eeb1aSlm66018  *	[ vd_complete_notify() and vd_serial_notify()) ] to send the
2375205eeb1aSlm66018  *	message to the client.
2376205eeb1aSlm66018  *
2377205eeb1aSlm66018  * Parameters:
2378205eeb1aSlm66018  *	arg 	- opaque pointer to structure containing task to be completed
2379205eeb1aSlm66018  *
2380205eeb1aSlm66018  * Return Values
2381205eeb1aSlm66018  *	None
2382205eeb1aSlm66018  */
2383205eeb1aSlm66018 static void
2384205eeb1aSlm66018 vd_notify(vd_task_t *task)
2385205eeb1aSlm66018 {
2386205eeb1aSlm66018 	int	status;
2387205eeb1aSlm66018 
2388205eeb1aSlm66018 	ASSERT(task != NULL);
2389205eeb1aSlm66018 	ASSERT(task->vd != NULL);
2390205eeb1aSlm66018 
2391205eeb1aSlm66018 	/*
2392205eeb1aSlm66018 	 * Send the "ack" or "nack" back to the client; if sending the message
2393205eeb1aSlm66018 	 * via LDC fails, arrange to reset both the connection state and LDC
2394205eeb1aSlm66018 	 * itself
2395205eeb1aSlm66018 	 */
2396205eeb1aSlm66018 	PR2("Sending %s",
2397205eeb1aSlm66018 	    (task->msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK");
2398205eeb1aSlm66018 
2399205eeb1aSlm66018 	status = send_msg(task->vd->ldc_handle, task->msg, task->msglen);
2400205eeb1aSlm66018 	switch (status) {
2401205eeb1aSlm66018 	case 0:
2402205eeb1aSlm66018 		break;
2403205eeb1aSlm66018 	case ECONNRESET:
2404205eeb1aSlm66018 		vd_mark_in_reset(task->vd);
2405205eeb1aSlm66018 		break;
2406205eeb1aSlm66018 	default:
2407205eeb1aSlm66018 		PR0("initiating full reset");
2408205eeb1aSlm66018 		vd_need_reset(task->vd, B_TRUE);
2409205eeb1aSlm66018 		break;
2410205eeb1aSlm66018 	}
2411205eeb1aSlm66018 
2412205eeb1aSlm66018 	DTRACE_PROBE1(task__end, vd_task_t *, task);
2413205eeb1aSlm66018 }
2414205eeb1aSlm66018 
2415205eeb1aSlm66018 /*
2416205eeb1aSlm66018  * Description:
2417205eeb1aSlm66018  *	Mark the Dring entry as Done and (if necessary) send an ACK/NACK to
2418205eeb1aSlm66018  *	the vDisk client
2419205eeb1aSlm66018  *
2420205eeb1aSlm66018  * Parameters:
2421205eeb1aSlm66018  *	task 		- structure containing the request sent from client
2422205eeb1aSlm66018  *
2423205eeb1aSlm66018  * Return Values
2424205eeb1aSlm66018  *	None
2425205eeb1aSlm66018  */
2426205eeb1aSlm66018 static void
2427205eeb1aSlm66018 vd_complete_notify(vd_task_t *task)
2428205eeb1aSlm66018 {
2429205eeb1aSlm66018 	int			status		= 0;
2430205eeb1aSlm66018 	vd_t			*vd		= task->vd;
2431205eeb1aSlm66018 	vd_dring_payload_t	*request	= task->request;
2432205eeb1aSlm66018 
2433d10e4ef2Snarayan 	/* Update the dring element for a dring client */
2434f0ca1d9aSsb155480 	if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE_V1_0)) {
24353c96341aSnarayan 		status = vd_mark_elem_done(vd, task->index,
24363c96341aSnarayan 		    request->status, request->nbytes);
24373af08d82Slm66018 		if (status == ECONNRESET)
24383af08d82Slm66018 			vd_mark_in_reset(vd);
2439bbfa0259Sha137994 		else if (status == EACCES)
2440bbfa0259Sha137994 			vd_need_reset(vd, B_TRUE);
24413af08d82Slm66018 	}
24421ae08745Sheppo 
2443d10e4ef2Snarayan 	/*
2444205eeb1aSlm66018 	 * If a transport error occurred while marking the element done or
2445205eeb1aSlm66018 	 * previously while executing the task, arrange to "nack" the message
2446205eeb1aSlm66018 	 * when the final task in the descriptor element range completes
2447d10e4ef2Snarayan 	 */
2448205eeb1aSlm66018 	if ((status != 0) || (task->status != 0))
2449d10e4ef2Snarayan 		task->msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
24501ae08745Sheppo 
2451d10e4ef2Snarayan 	/*
2452d10e4ef2Snarayan 	 * Only the final task for a range of elements will respond to and
2453d10e4ef2Snarayan 	 * free the message
2454d10e4ef2Snarayan 	 */
24553af08d82Slm66018 	if (task->type == VD_NONFINAL_RANGE_TASK) {
2456d10e4ef2Snarayan 		return;
24573af08d82Slm66018 	}
24581ae08745Sheppo 
245927ac699dSzk194757 	/*
246027ac699dSzk194757 	 * We should only send an ACK/NACK here if we are not currently in
246127ac699dSzk194757 	 * reset as, depending on how we reset, the dring may have been
246227ac699dSzk194757 	 * blown away and we don't want to ACK/NACK a message that isn't
246327ac699dSzk194757 	 * there.
246427ac699dSzk194757 	 */
246527ac699dSzk194757 	if (!vd->reset_state)
2466205eeb1aSlm66018 		vd_notify(task);
2467205eeb1aSlm66018 }
2468205eeb1aSlm66018 
2469d10e4ef2Snarayan /*
2470205eeb1aSlm66018  * Description:
2471205eeb1aSlm66018  *	This is the basic completion function called to handle inband data
2472205eeb1aSlm66018  *	requests and handshake messages. All it needs to do is trigger a
2473205eeb1aSlm66018  *	message to the client that the request is completed.
2474205eeb1aSlm66018  *
2475205eeb1aSlm66018  * Parameters:
2476205eeb1aSlm66018  *	arg 	- opaque pointer to structure containing task to be completed
2477205eeb1aSlm66018  *
2478205eeb1aSlm66018  * Return Values
2479205eeb1aSlm66018  *	None
2480d10e4ef2Snarayan  */
2481205eeb1aSlm66018 static void
2482205eeb1aSlm66018 vd_serial_notify(void *arg)
2483205eeb1aSlm66018 {
2484205eeb1aSlm66018 	vd_task_t		*task = (vd_task_t *)arg;
2485205eeb1aSlm66018 
2486205eeb1aSlm66018 	ASSERT(task != NULL);
2487205eeb1aSlm66018 	vd_notify(task);
24881ae08745Sheppo }
24891ae08745Sheppo 
24902f5224aeSachartre /* ARGSUSED */
24912f5224aeSachartre static int
24922f5224aeSachartre vd_geom2dk_geom(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
24930a55fbb7Slm66018 {
24940a55fbb7Slm66018 	VD_GEOM2DK_GEOM((vd_geom_t *)vd_buf, (struct dk_geom *)ioctl_arg);
24952f5224aeSachartre 	return (0);
24960a55fbb7Slm66018 }
24970a55fbb7Slm66018 
24982f5224aeSachartre /* ARGSUSED */
24992f5224aeSachartre static int
25002f5224aeSachartre vd_vtoc2vtoc(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
25010a55fbb7Slm66018 {
2502342440ecSPrasad Singamsetty 	VD_VTOC2VTOC((vd_vtoc_t *)vd_buf, (struct extvtoc *)ioctl_arg);
25032f5224aeSachartre 	return (0);
25040a55fbb7Slm66018 }
25050a55fbb7Slm66018 
25060a55fbb7Slm66018 static void
25070a55fbb7Slm66018 dk_geom2vd_geom(void *ioctl_arg, void *vd_buf)
25080a55fbb7Slm66018 {
25090a55fbb7Slm66018 	DK_GEOM2VD_GEOM((struct dk_geom *)ioctl_arg, (vd_geom_t *)vd_buf);
25100a55fbb7Slm66018 }
25110a55fbb7Slm66018 
25120a55fbb7Slm66018 static void
25130a55fbb7Slm66018 vtoc2vd_vtoc(void *ioctl_arg, void *vd_buf)
25140a55fbb7Slm66018 {
2515342440ecSPrasad Singamsetty 	VTOC2VD_VTOC((struct extvtoc *)ioctl_arg, (vd_vtoc_t *)vd_buf);
25160a55fbb7Slm66018 }
25170a55fbb7Slm66018 
25182f5224aeSachartre static int
25192f5224aeSachartre vd_get_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
25204bac2208Snarayan {
25214bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
25224bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
25232f5224aeSachartre 	size_t data_len;
25242f5224aeSachartre 
25252f5224aeSachartre 	data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t));
25262f5224aeSachartre 	if (vd_efi->length > data_len)
25272f5224aeSachartre 		return (EINVAL);
25284bac2208Snarayan 
25294bac2208Snarayan 	dk_efi->dki_lba = vd_efi->lba;
25304bac2208Snarayan 	dk_efi->dki_length = vd_efi->length;
25314bac2208Snarayan 	dk_efi->dki_data = kmem_zalloc(vd_efi->length, KM_SLEEP);
25322f5224aeSachartre 	return (0);
25334bac2208Snarayan }
25344bac2208Snarayan 
25354bac2208Snarayan static void
25364bac2208Snarayan vd_get_efi_out(void *ioctl_arg, void *vd_buf)
25374bac2208Snarayan {
25384bac2208Snarayan 	int len;
25394bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
25404bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
25414bac2208Snarayan 
25424bac2208Snarayan 	len = vd_efi->length;
25434bac2208Snarayan 	DK_EFI2VD_EFI(dk_efi, vd_efi);
25444bac2208Snarayan 	kmem_free(dk_efi->dki_data, len);
25454bac2208Snarayan }
25464bac2208Snarayan 
25472f5224aeSachartre static int
25482f5224aeSachartre vd_set_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
25494bac2208Snarayan {
25504bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
25514bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
25522f5224aeSachartre 	size_t data_len;
25532f5224aeSachartre 
25542f5224aeSachartre 	data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t));
25552f5224aeSachartre 	if (vd_efi->length > data_len)
25562f5224aeSachartre 		return (EINVAL);
25574bac2208Snarayan 
25584bac2208Snarayan 	dk_efi->dki_data = kmem_alloc(vd_efi->length, KM_SLEEP);
25594bac2208Snarayan 	VD_EFI2DK_EFI(vd_efi, dk_efi);
25602f5224aeSachartre 	return (0);
25614bac2208Snarayan }
25624bac2208Snarayan 
25634bac2208Snarayan static void
25644bac2208Snarayan vd_set_efi_out(void *ioctl_arg, void *vd_buf)
25654bac2208Snarayan {
25664bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
25674bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
25684bac2208Snarayan 
25694bac2208Snarayan 	kmem_free(dk_efi->dki_data, vd_efi->length);
25704bac2208Snarayan }
25714bac2208Snarayan 
25722f5224aeSachartre static int
25732f5224aeSachartre vd_scsicmd_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
25742f5224aeSachartre {
25752f5224aeSachartre 	size_t vd_scsi_len;
25762f5224aeSachartre 	vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf;
25772f5224aeSachartre 	struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg;
25782f5224aeSachartre 
25792f5224aeSachartre 	/* check buffer size */
25802f5224aeSachartre 	vd_scsi_len = VD_SCSI_SIZE;
25812f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(vd_scsi->cdb_len, sizeof (uint64_t));
25822f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(vd_scsi->sense_len, sizeof (uint64_t));
25832f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(vd_scsi->datain_len, sizeof (uint64_t));
25842f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(vd_scsi->dataout_len, sizeof (uint64_t));
25852f5224aeSachartre 
25862f5224aeSachartre 	ASSERT(vd_scsi_len % sizeof (uint64_t) == 0);
25872f5224aeSachartre 
25882f5224aeSachartre 	if (vd_buf_len < vd_scsi_len)
25892f5224aeSachartre 		return (EINVAL);
25902f5224aeSachartre 
25912f5224aeSachartre 	/* set flags */
25922f5224aeSachartre 	uscsi->uscsi_flags = vd_scsi_debug;
25932f5224aeSachartre 
25942f5224aeSachartre 	if (vd_scsi->options & VD_SCSI_OPT_NORETRY) {
25952f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_ISOLATE;
25962f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_DIAGNOSE;
25972f5224aeSachartre 	}
25982f5224aeSachartre 
25992f5224aeSachartre 	/* task attribute */
26002f5224aeSachartre 	switch (vd_scsi->task_attribute) {
26012f5224aeSachartre 	case VD_SCSI_TASK_ACA:
26022f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_HEAD;
26032f5224aeSachartre 		break;
26042f5224aeSachartre 	case VD_SCSI_TASK_HQUEUE:
26052f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_HTAG;
26062f5224aeSachartre 		break;
26072f5224aeSachartre 	case VD_SCSI_TASK_ORDERED:
26082f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_OTAG;
26092f5224aeSachartre 		break;
26102f5224aeSachartre 	default:
26112f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_NOTAG;
26122f5224aeSachartre 		break;
26132f5224aeSachartre 	}
26142f5224aeSachartre 
26152f5224aeSachartre 	/* timeout */
26162f5224aeSachartre 	uscsi->uscsi_timeout = vd_scsi->timeout;
26172f5224aeSachartre 
26182f5224aeSachartre 	/* cdb data */
26192f5224aeSachartre 	uscsi->uscsi_cdb = (caddr_t)VD_SCSI_DATA_CDB(vd_scsi);
26202f5224aeSachartre 	uscsi->uscsi_cdblen = vd_scsi->cdb_len;
26212f5224aeSachartre 
26222f5224aeSachartre 	/* sense buffer */
26232f5224aeSachartre 	if (vd_scsi->sense_len != 0) {
26242f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_RQENABLE;
26252f5224aeSachartre 		uscsi->uscsi_rqbuf = (caddr_t)VD_SCSI_DATA_SENSE(vd_scsi);
26262f5224aeSachartre 		uscsi->uscsi_rqlen = vd_scsi->sense_len;
26272f5224aeSachartre 	}
26282f5224aeSachartre 
26292f5224aeSachartre 	if (vd_scsi->datain_len != 0 && vd_scsi->dataout_len != 0) {
26302f5224aeSachartre 		/* uscsi does not support read/write request */
26312f5224aeSachartre 		return (EINVAL);
26322f5224aeSachartre 	}
26332f5224aeSachartre 
26342f5224aeSachartre 	/* request data-in */
26352f5224aeSachartre 	if (vd_scsi->datain_len != 0) {
26362f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_READ;
26372f5224aeSachartre 		uscsi->uscsi_buflen = vd_scsi->datain_len;
26382f5224aeSachartre 		uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_IN(vd_scsi);
26392f5224aeSachartre 	}
26402f5224aeSachartre 
26412f5224aeSachartre 	/* request data-out */
26422f5224aeSachartre 	if (vd_scsi->dataout_len != 0) {
26432f5224aeSachartre 		uscsi->uscsi_buflen = vd_scsi->dataout_len;
26442f5224aeSachartre 		uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_OUT(vd_scsi);
26452f5224aeSachartre 	}
26462f5224aeSachartre 
26472f5224aeSachartre 	return (0);
26482f5224aeSachartre }
26492f5224aeSachartre 
26502f5224aeSachartre static void
26512f5224aeSachartre vd_scsicmd_out(void *ioctl_arg, void *vd_buf)
26522f5224aeSachartre {
26532f5224aeSachartre 	vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf;
26542f5224aeSachartre 	struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg;
26552f5224aeSachartre 
26562f5224aeSachartre 	/* output fields */
26572f5224aeSachartre 	vd_scsi->cmd_status = uscsi->uscsi_status;
26582f5224aeSachartre 
26592f5224aeSachartre 	/* sense data */
26602f5224aeSachartre 	if ((uscsi->uscsi_flags & USCSI_RQENABLE) &&
26612f5224aeSachartre 	    (uscsi->uscsi_status == STATUS_CHECK ||
26622f5224aeSachartre 	    uscsi->uscsi_status == STATUS_TERMINATED)) {
26632f5224aeSachartre 		vd_scsi->sense_status = uscsi->uscsi_rqstatus;
26642f5224aeSachartre 		if (uscsi->uscsi_rqstatus == STATUS_GOOD)
266514466a20Szk194757 			vd_scsi->sense_len -= uscsi->uscsi_rqresid;
26662f5224aeSachartre 		else
26672f5224aeSachartre 			vd_scsi->sense_len = 0;
26682f5224aeSachartre 	} else {
26692f5224aeSachartre 		vd_scsi->sense_len = 0;
26702f5224aeSachartre 	}
26712f5224aeSachartre 
26722f5224aeSachartre 	if (uscsi->uscsi_status != STATUS_GOOD) {
26732f5224aeSachartre 		vd_scsi->dataout_len = 0;
26742f5224aeSachartre 		vd_scsi->datain_len = 0;
26752f5224aeSachartre 		return;
26762f5224aeSachartre 	}
26772f5224aeSachartre 
26782f5224aeSachartre 	if (uscsi->uscsi_flags & USCSI_READ) {
26792f5224aeSachartre 		/* request data (read) */
26802f5224aeSachartre 		vd_scsi->datain_len -= uscsi->uscsi_resid;
26812f5224aeSachartre 		vd_scsi->dataout_len = 0;
26822f5224aeSachartre 	} else {
26832f5224aeSachartre 		/* request data (write) */
26842f5224aeSachartre 		vd_scsi->datain_len = 0;
26852f5224aeSachartre 		vd_scsi->dataout_len -= uscsi->uscsi_resid;
26862f5224aeSachartre 	}
26872f5224aeSachartre }
26882f5224aeSachartre 
2689690555a1Sachartre static ushort_t
26903c96341aSnarayan vd_lbl2cksum(struct dk_label *label)
26913c96341aSnarayan {
26923c96341aSnarayan 	int	count;
2693690555a1Sachartre 	ushort_t sum, *sp;
26943c96341aSnarayan 
26953c96341aSnarayan 	count =	(sizeof (struct dk_label)) / (sizeof (short)) - 1;
2696690555a1Sachartre 	sp = (ushort_t *)label;
26973c96341aSnarayan 	sum = 0;
26983c96341aSnarayan 	while (count--) {
26993c96341aSnarayan 		sum ^= *sp++;
27003c96341aSnarayan 	}
27013c96341aSnarayan 
27023c96341aSnarayan 	return (sum);
27033c96341aSnarayan }
27043c96341aSnarayan 
270587a7269eSachartre /*
2706bae9e67eSachartre  * Copy information from a vtoc and dk_geom structures to a dk_label structure.
2707bae9e67eSachartre  */
2708bae9e67eSachartre static void
2709342440ecSPrasad Singamsetty vd_vtocgeom_to_label(struct extvtoc *vtoc, struct dk_geom *geom,
2710bae9e67eSachartre     struct dk_label *label)
2711bae9e67eSachartre {
2712bae9e67eSachartre 	int i;
2713bae9e67eSachartre 
2714bae9e67eSachartre 	ASSERT(vtoc->v_nparts == V_NUMPAR);
2715bae9e67eSachartre 	ASSERT(vtoc->v_sanity == VTOC_SANE);
2716bae9e67eSachartre 
2717bae9e67eSachartre 	bzero(label, sizeof (struct dk_label));
2718bae9e67eSachartre 
2719bae9e67eSachartre 	label->dkl_ncyl = geom->dkg_ncyl;
2720bae9e67eSachartre 	label->dkl_acyl = geom->dkg_acyl;
2721bae9e67eSachartre 	label->dkl_pcyl = geom->dkg_pcyl;
2722bae9e67eSachartre 	label->dkl_nhead = geom->dkg_nhead;
2723bae9e67eSachartre 	label->dkl_nsect = geom->dkg_nsect;
2724bae9e67eSachartre 	label->dkl_intrlv = geom->dkg_intrlv;
2725bae9e67eSachartre 	label->dkl_apc = geom->dkg_apc;
2726bae9e67eSachartre 	label->dkl_rpm = geom->dkg_rpm;
2727bae9e67eSachartre 	label->dkl_write_reinstruct = geom->dkg_write_reinstruct;
2728bae9e67eSachartre 	label->dkl_read_reinstruct = geom->dkg_read_reinstruct;
2729bae9e67eSachartre 
2730bae9e67eSachartre 	label->dkl_vtoc.v_nparts = V_NUMPAR;
2731bae9e67eSachartre 	label->dkl_vtoc.v_sanity = VTOC_SANE;
2732bae9e67eSachartre 	label->dkl_vtoc.v_version = vtoc->v_version;
2733bae9e67eSachartre 	for (i = 0; i < V_NUMPAR; i++) {
2734bae9e67eSachartre 		label->dkl_vtoc.v_timestamp[i] = vtoc->timestamp[i];
2735bae9e67eSachartre 		label->dkl_vtoc.v_part[i].p_tag = vtoc->v_part[i].p_tag;
2736bae9e67eSachartre 		label->dkl_vtoc.v_part[i].p_flag = vtoc->v_part[i].p_flag;
2737bae9e67eSachartre 		label->dkl_map[i].dkl_cylno = vtoc->v_part[i].p_start /
2738bae9e67eSachartre 		    (label->dkl_nhead * label->dkl_nsect);
2739bae9e67eSachartre 		label->dkl_map[i].dkl_nblk = vtoc->v_part[i].p_size;
2740bae9e67eSachartre 	}
2741bae9e67eSachartre 
2742bae9e67eSachartre 	/*
2743bae9e67eSachartre 	 * The bootinfo array can not be copied with bcopy() because
2744bae9e67eSachartre 	 * elements are of type long in vtoc (so 64-bit) and of type
2745bae9e67eSachartre 	 * int in dk_vtoc (so 32-bit).
2746bae9e67eSachartre 	 */
2747bae9e67eSachartre 	label->dkl_vtoc.v_bootinfo[0] = vtoc->v_bootinfo[0];
2748bae9e67eSachartre 	label->dkl_vtoc.v_bootinfo[1] = vtoc->v_bootinfo[1];
2749bae9e67eSachartre 	label->dkl_vtoc.v_bootinfo[2] = vtoc->v_bootinfo[2];
2750bae9e67eSachartre 	bcopy(vtoc->v_asciilabel, label->dkl_asciilabel, LEN_DKL_ASCII);
2751bae9e67eSachartre 	bcopy(vtoc->v_volume, label->dkl_vtoc.v_volume, LEN_DKL_VVOL);
2752bae9e67eSachartre 
2753bae9e67eSachartre 	/* re-compute checksum */
2754bae9e67eSachartre 	label->dkl_magic = DKL_MAGIC;
2755bae9e67eSachartre 	label->dkl_cksum = vd_lbl2cksum(label);
2756bae9e67eSachartre }
2757bae9e67eSachartre 
2758bae9e67eSachartre /*
2759bae9e67eSachartre  * Copy information from a dk_label structure to a vtoc and dk_geom structures.
2760bae9e67eSachartre  */
2761bae9e67eSachartre static void
2762342440ecSPrasad Singamsetty vd_label_to_vtocgeom(struct dk_label *label, struct extvtoc *vtoc,
2763bae9e67eSachartre     struct dk_geom *geom)
2764bae9e67eSachartre {
2765bae9e67eSachartre 	int i;
2766bae9e67eSachartre 
2767bae9e67eSachartre 	bzero(vtoc, sizeof (struct vtoc));
2768bae9e67eSachartre 	bzero(geom, sizeof (struct dk_geom));
2769bae9e67eSachartre 
2770bae9e67eSachartre 	geom->dkg_ncyl = label->dkl_ncyl;
2771bae9e67eSachartre 	geom->dkg_acyl = label->dkl_acyl;
2772bae9e67eSachartre 	geom->dkg_nhead = label->dkl_nhead;
2773bae9e67eSachartre 	geom->dkg_nsect = label->dkl_nsect;
2774bae9e67eSachartre 	geom->dkg_intrlv = label->dkl_intrlv;
2775bae9e67eSachartre 	geom->dkg_apc = label->dkl_apc;
2776bae9e67eSachartre 	geom->dkg_rpm = label->dkl_rpm;
2777bae9e67eSachartre 	geom->dkg_pcyl = label->dkl_pcyl;
2778bae9e67eSachartre 	geom->dkg_write_reinstruct = label->dkl_write_reinstruct;
2779bae9e67eSachartre 	geom->dkg_read_reinstruct = label->dkl_read_reinstruct;
2780bae9e67eSachartre 
2781bae9e67eSachartre 	vtoc->v_sanity = label->dkl_vtoc.v_sanity;
2782bae9e67eSachartre 	vtoc->v_version = label->dkl_vtoc.v_version;
2783bae9e67eSachartre 	vtoc->v_sectorsz = DEV_BSIZE;
2784bae9e67eSachartre 	vtoc->v_nparts = label->dkl_vtoc.v_nparts;
2785bae9e67eSachartre 
2786bae9e67eSachartre 	for (i = 0; i < vtoc->v_nparts; i++) {
2787bae9e67eSachartre 		vtoc->v_part[i].p_tag = label->dkl_vtoc.v_part[i].p_tag;
2788bae9e67eSachartre 		vtoc->v_part[i].p_flag = label->dkl_vtoc.v_part[i].p_flag;
2789bae9e67eSachartre 		vtoc->v_part[i].p_start = label->dkl_map[i].dkl_cylno *
2790bae9e67eSachartre 		    (label->dkl_nhead * label->dkl_nsect);
2791bae9e67eSachartre 		vtoc->v_part[i].p_size = label->dkl_map[i].dkl_nblk;
2792bae9e67eSachartre 		vtoc->timestamp[i] = label->dkl_vtoc.v_timestamp[i];
2793bae9e67eSachartre 	}
2794bae9e67eSachartre 
2795bae9e67eSachartre 	/*
2796bae9e67eSachartre 	 * The bootinfo array can not be copied with bcopy() because
2797bae9e67eSachartre 	 * elements are of type long in vtoc (so 64-bit) and of type
2798bae9e67eSachartre 	 * int in dk_vtoc (so 32-bit).
2799bae9e67eSachartre 	 */
2800bae9e67eSachartre 	vtoc->v_bootinfo[0] = label->dkl_vtoc.v_bootinfo[0];
2801bae9e67eSachartre 	vtoc->v_bootinfo[1] = label->dkl_vtoc.v_bootinfo[1];
2802bae9e67eSachartre 	vtoc->v_bootinfo[2] = label->dkl_vtoc.v_bootinfo[2];
2803bae9e67eSachartre 	bcopy(label->dkl_asciilabel, vtoc->v_asciilabel, LEN_DKL_ASCII);
2804bae9e67eSachartre 	bcopy(label->dkl_vtoc.v_volume, vtoc->v_volume, LEN_DKL_VVOL);
2805bae9e67eSachartre }
2806bae9e67eSachartre 
2807bae9e67eSachartre /*
2808bae9e67eSachartre  * Check if a geometry is valid for a single-slice disk. A geometry is
2809bae9e67eSachartre  * considered valid if the main attributes of the geometry match with the
2810bae9e67eSachartre  * attributes of the fake geometry we have created.
2811bae9e67eSachartre  */
2812bae9e67eSachartre static boolean_t
2813bae9e67eSachartre vd_slice_geom_isvalid(vd_t *vd, struct dk_geom *geom)
2814bae9e67eSachartre {
2815bae9e67eSachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
2816bae9e67eSachartre 	ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
2817bae9e67eSachartre 
2818bae9e67eSachartre 	if (geom->dkg_ncyl != vd->dk_geom.dkg_ncyl ||
2819bae9e67eSachartre 	    geom->dkg_acyl != vd->dk_geom.dkg_acyl ||
2820bae9e67eSachartre 	    geom->dkg_nsect != vd->dk_geom.dkg_nsect ||
2821bae9e67eSachartre 	    geom->dkg_pcyl != vd->dk_geom.dkg_pcyl)
2822bae9e67eSachartre 		return (B_FALSE);
2823bae9e67eSachartre 
2824bae9e67eSachartre 	return (B_TRUE);
2825bae9e67eSachartre }
2826bae9e67eSachartre 
2827bae9e67eSachartre /*
2828bae9e67eSachartre  * Check if a vtoc is valid for a single-slice disk. A vtoc is considered
2829bae9e67eSachartre  * valid if the main attributes of the vtoc match with the attributes of the
2830bae9e67eSachartre  * fake vtoc we have created.
2831bae9e67eSachartre  */
2832bae9e67eSachartre static boolean_t
2833342440ecSPrasad Singamsetty vd_slice_vtoc_isvalid(vd_t *vd, struct extvtoc *vtoc)
2834bae9e67eSachartre {
2835bae9e67eSachartre 	size_t csize;
2836bae9e67eSachartre 	int i;
2837bae9e67eSachartre 
2838bae9e67eSachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
2839bae9e67eSachartre 	ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
2840bae9e67eSachartre 
2841bae9e67eSachartre 	if (vtoc->v_sanity != vd->vtoc.v_sanity ||
2842bae9e67eSachartre 	    vtoc->v_version != vd->vtoc.v_version ||
2843bae9e67eSachartre 	    vtoc->v_nparts != vd->vtoc.v_nparts ||
2844bae9e67eSachartre 	    strcmp(vtoc->v_volume, vd->vtoc.v_volume) != 0 ||
2845bae9e67eSachartre 	    strcmp(vtoc->v_asciilabel, vd->vtoc.v_asciilabel) != 0)
2846bae9e67eSachartre 		return (B_FALSE);
2847bae9e67eSachartre 
2848bae9e67eSachartre 	/* slice 2 should be unchanged */
2849bae9e67eSachartre 	if (vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_start !=
2850bae9e67eSachartre 	    vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_start ||
2851bae9e67eSachartre 	    vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_size !=
2852bae9e67eSachartre 	    vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_size)
2853bae9e67eSachartre 		return (B_FALSE);
2854bae9e67eSachartre 
2855bae9e67eSachartre 	/*
2856bae9e67eSachartre 	 * Slice 0 should be mostly unchanged and cover most of the disk.
2857bae9e67eSachartre 	 * However we allow some flexibility wrt to the start and the size
2858bae9e67eSachartre 	 * of this slice mainly because we can't exactly know how it will
2859bae9e67eSachartre 	 * be defined by the OS installer.
2860bae9e67eSachartre 	 *
2861bae9e67eSachartre 	 * We allow slice 0 to be defined as starting on any of the first
2862bae9e67eSachartre 	 * 4 cylinders.
2863bae9e67eSachartre 	 */
2864bae9e67eSachartre 	csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
2865bae9e67eSachartre 
2866bae9e67eSachartre 	if (vtoc->v_part[0].p_start > 4 * csize ||
2867bae9e67eSachartre 	    vtoc->v_part[0].p_size > vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_size)
2868bae9e67eSachartre 			return (B_FALSE);
2869bae9e67eSachartre 
2870bae9e67eSachartre 	if (vd->vtoc.v_part[0].p_size >= 4 * csize &&
2871bae9e67eSachartre 	    vtoc->v_part[0].p_size < vd->vtoc.v_part[0].p_size - 4 *csize)
2872bae9e67eSachartre 			return (B_FALSE);
2873bae9e67eSachartre 
2874bae9e67eSachartre 	/* any other slice should have a size of 0 */
2875bae9e67eSachartre 	for (i = 1; i < vtoc->v_nparts; i++) {
2876bae9e67eSachartre 		if (i != VD_ENTIRE_DISK_SLICE &&
2877bae9e67eSachartre 		    vtoc->v_part[i].p_size != 0)
2878bae9e67eSachartre 			return (B_FALSE);
2879bae9e67eSachartre 	}
2880bae9e67eSachartre 
2881bae9e67eSachartre 	return (B_TRUE);
2882bae9e67eSachartre }
2883bae9e67eSachartre 
2884bae9e67eSachartre /*
288587a7269eSachartre  * Handle ioctls to a disk slice.
2886205eeb1aSlm66018  *
2887205eeb1aSlm66018  * Return Values
2888205eeb1aSlm66018  *	0	- Indicates that there are no errors in disk operations
2889205eeb1aSlm66018  *	ENOTSUP	- Unknown disk label type or unsupported DKIO ioctl
2890205eeb1aSlm66018  *	EINVAL	- Not enough room to copy the EFI label
2891205eeb1aSlm66018  *
289287a7269eSachartre  */
28931ae08745Sheppo static int
28940a55fbb7Slm66018 vd_do_slice_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
28951ae08745Sheppo {
28964bac2208Snarayan 	dk_efi_t *dk_ioc;
2897342440ecSPrasad Singamsetty 	struct extvtoc *vtoc;
2898bae9e67eSachartre 	struct dk_geom *geom;
2899bae9e67eSachartre 	size_t len, lba;
2900edcc0754Sachartre 	int rval;
2901edcc0754Sachartre 
2902edcc0754Sachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
2903edcc0754Sachartre 
2904edcc0754Sachartre 	if (cmd == DKIOCFLUSHWRITECACHE) {
2905edcc0754Sachartre 		if (vd->file) {
2906edcc0754Sachartre 			return (VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL));
2907edcc0754Sachartre 		} else {
2908edcc0754Sachartre 			return (ldi_ioctl(vd->ldi_handle[0], cmd,
2909edcc0754Sachartre 			    (intptr_t)ioctl_arg, vd->open_flags | FKIOCTL,
2910edcc0754Sachartre 			    kcred, &rval));
2911edcc0754Sachartre 		}
2912edcc0754Sachartre 	}
29134bac2208Snarayan 
29144bac2208Snarayan 	switch (vd->vdisk_label) {
29154bac2208Snarayan 
2916edcc0754Sachartre 	/* ioctls for a single slice disk with a VTOC label */
29174bac2208Snarayan 	case VD_DISK_LABEL_VTOC:
29184bac2208Snarayan 
29191ae08745Sheppo 		switch (cmd) {
2920bae9e67eSachartre 
29211ae08745Sheppo 		case DKIOCGGEOM:
29220a55fbb7Slm66018 			ASSERT(ioctl_arg != NULL);
29230a55fbb7Slm66018 			bcopy(&vd->dk_geom, ioctl_arg, sizeof (vd->dk_geom));
29241ae08745Sheppo 			return (0);
2925bae9e67eSachartre 
2926342440ecSPrasad Singamsetty 		case DKIOCGEXTVTOC:
29270a55fbb7Slm66018 			ASSERT(ioctl_arg != NULL);
29280a55fbb7Slm66018 			bcopy(&vd->vtoc, ioctl_arg, sizeof (vd->vtoc));
29291ae08745Sheppo 			return (0);
2930bae9e67eSachartre 
2931bae9e67eSachartre 		case DKIOCSGEOM:
2932bae9e67eSachartre 			ASSERT(ioctl_arg != NULL);
2933bae9e67eSachartre 			if (vd_slice_single_slice)
2934bae9e67eSachartre 				return (ENOTSUP);
2935bae9e67eSachartre 
2936bae9e67eSachartre 			/* fake success only if new geometry is valid */
2937bae9e67eSachartre 			geom = (struct dk_geom *)ioctl_arg;
2938bae9e67eSachartre 			if (!vd_slice_geom_isvalid(vd, geom))
2939bae9e67eSachartre 				return (EINVAL);
2940bae9e67eSachartre 
2941bae9e67eSachartre 			return (0);
2942bae9e67eSachartre 
2943342440ecSPrasad Singamsetty 		case DKIOCSEXTVTOC:
2944bae9e67eSachartre 			ASSERT(ioctl_arg != NULL);
2945bae9e67eSachartre 			if (vd_slice_single_slice)
2946bae9e67eSachartre 				return (ENOTSUP);
2947bae9e67eSachartre 
2948bae9e67eSachartre 			/* fake sucess only if the new vtoc is valid */
2949342440ecSPrasad Singamsetty 			vtoc = (struct extvtoc *)ioctl_arg;
2950bae9e67eSachartre 			if (!vd_slice_vtoc_isvalid(vd, vtoc))
2951bae9e67eSachartre 				return (EINVAL);
2952bae9e67eSachartre 
2953bae9e67eSachartre 			return (0);
2954bae9e67eSachartre 
295587a7269eSachartre 		default:
29563c96341aSnarayan 			return (ENOTSUP);
295787a7269eSachartre 		}
295887a7269eSachartre 
2959edcc0754Sachartre 	/* ioctls for a single slice disk with an EFI label */
296087a7269eSachartre 	case VD_DISK_LABEL_EFI:
296187a7269eSachartre 
2962bae9e67eSachartre 		if (cmd != DKIOCGETEFI && cmd != DKIOCSETEFI)
2963bae9e67eSachartre 			return (ENOTSUP);
2964bae9e67eSachartre 
29653c96341aSnarayan 		ASSERT(ioctl_arg != NULL);
296687a7269eSachartre 		dk_ioc = (dk_efi_t *)ioctl_arg;
2967edcc0754Sachartre 
2968bae9e67eSachartre 		len = dk_ioc->dki_length;
2969bae9e67eSachartre 		lba = dk_ioc->dki_lba;
2970edcc0754Sachartre 
2971bae9e67eSachartre 		if ((lba != VD_EFI_LBA_GPT && lba != VD_EFI_LBA_GPE) ||
2972bae9e67eSachartre 		    (lba == VD_EFI_LBA_GPT && len < sizeof (efi_gpt_t)) ||
2973bae9e67eSachartre 		    (lba == VD_EFI_LBA_GPE && len < sizeof (efi_gpe_t)))
297487a7269eSachartre 			return (EINVAL);
2975edcc0754Sachartre 
2976bae9e67eSachartre 		switch (cmd) {
2977bae9e67eSachartre 		case DKIOCGETEFI:
2978bae9e67eSachartre 			len = vd_slice_flabel_read(vd,
2979bae9e67eSachartre 			    (caddr_t)dk_ioc->dki_data, lba * DEV_BSIZE, len);
2980edcc0754Sachartre 
2981bae9e67eSachartre 			ASSERT(len > 0);
2982edcc0754Sachartre 
298387a7269eSachartre 			return (0);
2984bae9e67eSachartre 
2985bae9e67eSachartre 		case DKIOCSETEFI:
2986bae9e67eSachartre 			if (vd_slice_single_slice)
298787a7269eSachartre 				return (ENOTSUP);
2988bae9e67eSachartre 
2989bae9e67eSachartre 			/* we currently don't support writing EFI */
2990bae9e67eSachartre 			return (EIO);
299187a7269eSachartre 		}
299287a7269eSachartre 
299387a7269eSachartre 	default:
2994205eeb1aSlm66018 		/* Unknown disk label type */
299587a7269eSachartre 		return (ENOTSUP);
299687a7269eSachartre 	}
299787a7269eSachartre }
299887a7269eSachartre 
2999edcc0754Sachartre static int
3000edcc0754Sachartre vds_efi_alloc_and_read(vd_t *vd, efi_gpt_t **gpt, efi_gpe_t **gpe)
3001edcc0754Sachartre {
3002edcc0754Sachartre 	vd_efi_dev_t edev;
3003edcc0754Sachartre 	int status;
3004edcc0754Sachartre 
3005edcc0754Sachartre 	VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl);
3006edcc0754Sachartre 
3007edcc0754Sachartre 	status = vd_efi_alloc_and_read(&edev, gpt, gpe);
3008edcc0754Sachartre 
3009edcc0754Sachartre 	return (status);
3010edcc0754Sachartre }
3011edcc0754Sachartre 
3012edcc0754Sachartre static void
3013edcc0754Sachartre vds_efi_free(vd_t *vd, efi_gpt_t *gpt, efi_gpe_t *gpe)
3014edcc0754Sachartre {
3015edcc0754Sachartre 	vd_efi_dev_t edev;
3016edcc0754Sachartre 
3017edcc0754Sachartre 	VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl);
3018edcc0754Sachartre 
3019edcc0754Sachartre 	vd_efi_free(&edev, gpt, gpe);
3020edcc0754Sachartre }
3021edcc0754Sachartre 
3022edcc0754Sachartre static int
3023*1aff8f07SAlexandre Chartre vd_dskimg_validate_efi(vd_t *vd)
3024edcc0754Sachartre {
3025edcc0754Sachartre 	efi_gpt_t *gpt;
3026edcc0754Sachartre 	efi_gpe_t *gpe;
3027edcc0754Sachartre 	int i, nparts, status;
3028edcc0754Sachartre 	struct uuid efi_reserved = EFI_RESERVED;
3029edcc0754Sachartre 
3030edcc0754Sachartre 	if ((status = vds_efi_alloc_and_read(vd, &gpt, &gpe)) != 0)
3031edcc0754Sachartre 		return (status);
3032edcc0754Sachartre 
3033342440ecSPrasad Singamsetty 	bzero(&vd->vtoc, sizeof (struct extvtoc));
3034edcc0754Sachartre 	bzero(&vd->dk_geom, sizeof (struct dk_geom));
3035edcc0754Sachartre 	bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART);
3036edcc0754Sachartre 
3037edcc0754Sachartre 	vd->efi_reserved = -1;
3038edcc0754Sachartre 
3039edcc0754Sachartre 	nparts = gpt->efi_gpt_NumberOfPartitionEntries;
3040edcc0754Sachartre 
3041edcc0754Sachartre 	for (i = 0; i < nparts && i < VD_MAXPART; i++) {
3042edcc0754Sachartre 
3043edcc0754Sachartre 		if (gpe[i].efi_gpe_StartingLBA == 0 ||
3044edcc0754Sachartre 		    gpe[i].efi_gpe_EndingLBA == 0) {
3045edcc0754Sachartre 			continue;
3046edcc0754Sachartre 		}
3047edcc0754Sachartre 
3048edcc0754Sachartre 		vd->slices[i].start = gpe[i].efi_gpe_StartingLBA;
3049edcc0754Sachartre 		vd->slices[i].nblocks = gpe[i].efi_gpe_EndingLBA -
3050edcc0754Sachartre 		    gpe[i].efi_gpe_StartingLBA + 1;
3051edcc0754Sachartre 
3052edcc0754Sachartre 		if (bcmp(&gpe[i].efi_gpe_PartitionTypeGUID, &efi_reserved,
3053edcc0754Sachartre 		    sizeof (struct uuid)) == 0)
3054edcc0754Sachartre 			vd->efi_reserved = i;
3055edcc0754Sachartre 
3056edcc0754Sachartre 	}
3057edcc0754Sachartre 
3058edcc0754Sachartre 	ASSERT(vd->vdisk_size != 0);
3059edcc0754Sachartre 	vd->slices[VD_EFI_WD_SLICE].start = 0;
3060edcc0754Sachartre 	vd->slices[VD_EFI_WD_SLICE].nblocks = vd->vdisk_size;
3061edcc0754Sachartre 
3062edcc0754Sachartre 	vds_efi_free(vd, gpt, gpe);
3063edcc0754Sachartre 
3064edcc0754Sachartre 	return (status);
3065edcc0754Sachartre }
3066edcc0754Sachartre 
306787a7269eSachartre /*
306878fcd0a1Sachartre  * Function:
3069*1aff8f07SAlexandre Chartre  *	vd_dskimg_validate_geometry
3070205eeb1aSlm66018  *
307178fcd0a1Sachartre  * Description:
307278fcd0a1Sachartre  *	Read the label and validate the geometry of a disk image. The driver
307378fcd0a1Sachartre  *	label, vtoc and geometry information are updated according to the
307478fcd0a1Sachartre  *	label read from the disk image.
307578fcd0a1Sachartre  *
307678fcd0a1Sachartre  *	If no valid label is found, the label is set to unknown and the
307778fcd0a1Sachartre  *	function returns EINVAL, but a default vtoc and geometry are provided
3078edcc0754Sachartre  *	to the driver. If an EFI label is found, ENOTSUP is returned.
307978fcd0a1Sachartre  *
308078fcd0a1Sachartre  * Parameters:
308178fcd0a1Sachartre  *	vd	- disk on which the operation is performed.
308278fcd0a1Sachartre  *
308378fcd0a1Sachartre  * Return Code:
308478fcd0a1Sachartre  *	0	- success.
308578fcd0a1Sachartre  *	EIO	- error reading the label from the disk image.
308678fcd0a1Sachartre  *	EINVAL	- unknown disk label.
3087edcc0754Sachartre  *	ENOTSUP	- geometry not applicable (EFI label).
308887a7269eSachartre  */
308987a7269eSachartre static int
3090*1aff8f07SAlexandre Chartre vd_dskimg_validate_geometry(vd_t *vd)
309187a7269eSachartre {
309287a7269eSachartre 	struct dk_label label;
309378fcd0a1Sachartre 	struct dk_geom *geom = &vd->dk_geom;
3094342440ecSPrasad Singamsetty 	struct extvtoc *vtoc = &vd->vtoc;
309578fcd0a1Sachartre 	int i;
309678fcd0a1Sachartre 	int status = 0;
309787a7269eSachartre 
3098*1aff8f07SAlexandre Chartre 	ASSERT(VD_DSKIMG(vd));
309987a7269eSachartre 
3100*1aff8f07SAlexandre Chartre 	if (VD_DSKIMG_LABEL_READ(vd, &label) < 0)
310187a7269eSachartre 		return (EIO);
310287a7269eSachartre 
310387a7269eSachartre 	if (label.dkl_magic != DKL_MAGIC ||
310478fcd0a1Sachartre 	    label.dkl_cksum != vd_lbl2cksum(&label) ||
3105*1aff8f07SAlexandre Chartre 	    (vd_dskimg_validate_sanity &&
3106*1aff8f07SAlexandre Chartre 	    label.dkl_vtoc.v_sanity != VTOC_SANE) ||
310778fcd0a1Sachartre 	    label.dkl_vtoc.v_nparts != V_NUMPAR) {
3108edcc0754Sachartre 
3109*1aff8f07SAlexandre Chartre 		if (vd_dskimg_validate_efi(vd) == 0) {
3110edcc0754Sachartre 			vd->vdisk_label = VD_DISK_LABEL_EFI;
3111edcc0754Sachartre 			return (ENOTSUP);
3112edcc0754Sachartre 		}
3113edcc0754Sachartre 
311478fcd0a1Sachartre 		vd->vdisk_label = VD_DISK_LABEL_UNK;
3115*1aff8f07SAlexandre Chartre 		vd_build_default_label(vd->dskimg_size, &label);
311678fcd0a1Sachartre 		status = EINVAL;
311778fcd0a1Sachartre 	} else {
311878fcd0a1Sachartre 		vd->vdisk_label = VD_DISK_LABEL_VTOC;
311978fcd0a1Sachartre 	}
312087a7269eSachartre 
3121bae9e67eSachartre 	/* Update the driver geometry and vtoc */
3122bae9e67eSachartre 	vd_label_to_vtocgeom(&label, vtoc, geom);
312387a7269eSachartre 
3124edcc0754Sachartre 	/* Update logical partitions */
3125edcc0754Sachartre 	bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART);
3126edcc0754Sachartre 	if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
3127edcc0754Sachartre 		for (i = 0; i < vtoc->v_nparts; i++) {
3128edcc0754Sachartre 			vd->slices[i].start = vtoc->v_part[i].p_start;
3129edcc0754Sachartre 			vd->slices[i].nblocks = vtoc->v_part[i].p_size;
3130edcc0754Sachartre 		}
3131edcc0754Sachartre 	}
3132edcc0754Sachartre 
313378fcd0a1Sachartre 	return (status);
313478fcd0a1Sachartre }
313578fcd0a1Sachartre 
313678fcd0a1Sachartre /*
3137*1aff8f07SAlexandre Chartre  * Handle ioctls to a disk image.
313878fcd0a1Sachartre  *
313978fcd0a1Sachartre  * Return Values
314078fcd0a1Sachartre  *	0	- Indicates that there are no errors
314178fcd0a1Sachartre  *	!= 0	- Disk operation returned an error
314278fcd0a1Sachartre  */
314378fcd0a1Sachartre static int
3144*1aff8f07SAlexandre Chartre vd_do_dskimg_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
314578fcd0a1Sachartre {
314678fcd0a1Sachartre 	struct dk_label label;
314778fcd0a1Sachartre 	struct dk_geom *geom;
3148342440ecSPrasad Singamsetty 	struct extvtoc *vtoc;
3149edcc0754Sachartre 	dk_efi_t *efi;
3150*1aff8f07SAlexandre Chartre 	int rc, rval;
315178fcd0a1Sachartre 
3152*1aff8f07SAlexandre Chartre 	ASSERT(VD_DSKIMG(vd));
315378fcd0a1Sachartre 
315478fcd0a1Sachartre 	switch (cmd) {
315578fcd0a1Sachartre 
315678fcd0a1Sachartre 	case DKIOCGGEOM:
315778fcd0a1Sachartre 		ASSERT(ioctl_arg != NULL);
315878fcd0a1Sachartre 		geom = (struct dk_geom *)ioctl_arg;
315978fcd0a1Sachartre 
3160*1aff8f07SAlexandre Chartre 		rc = vd_dskimg_validate_geometry(vd);
3161edcc0754Sachartre 		if (rc != 0 && rc != EINVAL)
316278fcd0a1Sachartre 			return (rc);
316378fcd0a1Sachartre 		bcopy(&vd->dk_geom, geom, sizeof (struct dk_geom));
316478fcd0a1Sachartre 		return (0);
316578fcd0a1Sachartre 
3166342440ecSPrasad Singamsetty 	case DKIOCGEXTVTOC:
316778fcd0a1Sachartre 		ASSERT(ioctl_arg != NULL);
3168342440ecSPrasad Singamsetty 		vtoc = (struct extvtoc *)ioctl_arg;
316978fcd0a1Sachartre 
3170*1aff8f07SAlexandre Chartre 		rc = vd_dskimg_validate_geometry(vd);
3171edcc0754Sachartre 		if (rc != 0 && rc != EINVAL)
317278fcd0a1Sachartre 			return (rc);
3173342440ecSPrasad Singamsetty 		bcopy(&vd->vtoc, vtoc, sizeof (struct extvtoc));
317487a7269eSachartre 		return (0);
317587a7269eSachartre 
317687a7269eSachartre 	case DKIOCSGEOM:
317787a7269eSachartre 		ASSERT(ioctl_arg != NULL);
317887a7269eSachartre 		geom = (struct dk_geom *)ioctl_arg;
317987a7269eSachartre 
318087a7269eSachartre 		if (geom->dkg_nhead == 0 || geom->dkg_nsect == 0)
318187a7269eSachartre 			return (EINVAL);
318287a7269eSachartre 
318387a7269eSachartre 		/*
318487a7269eSachartre 		 * The current device geometry is not updated, just the driver
318587a7269eSachartre 		 * "notion" of it. The device geometry will be effectively
318687a7269eSachartre 		 * updated when a label is written to the device during a next
3187342440ecSPrasad Singamsetty 		 * DKIOCSEXTVTOC.
318887a7269eSachartre 		 */
318987a7269eSachartre 		bcopy(ioctl_arg, &vd->dk_geom, sizeof (vd->dk_geom));
319087a7269eSachartre 		return (0);
319187a7269eSachartre 
3192342440ecSPrasad Singamsetty 	case DKIOCSEXTVTOC:
319387a7269eSachartre 		ASSERT(ioctl_arg != NULL);
319487a7269eSachartre 		ASSERT(vd->dk_geom.dkg_nhead != 0 &&
319587a7269eSachartre 		    vd->dk_geom.dkg_nsect != 0);
3196342440ecSPrasad Singamsetty 		vtoc = (struct extvtoc *)ioctl_arg;
3197690555a1Sachartre 
3198690555a1Sachartre 		if (vtoc->v_sanity != VTOC_SANE ||
3199690555a1Sachartre 		    vtoc->v_sectorsz != DEV_BSIZE ||
3200690555a1Sachartre 		    vtoc->v_nparts != V_NUMPAR)
3201690555a1Sachartre 			return (EINVAL);
3202690555a1Sachartre 
3203bae9e67eSachartre 		vd_vtocgeom_to_label(vtoc, &vd->dk_geom, &label);
3204690555a1Sachartre 
320587a7269eSachartre 		/* write label to the disk image */
3206*1aff8f07SAlexandre Chartre 		if ((rc = vd_dskimg_set_vtoc(vd, &label)) != 0)
320787a7269eSachartre 			return (rc);
3208690555a1Sachartre 
3209edcc0754Sachartre 		break;
3210edcc0754Sachartre 
3211edcc0754Sachartre 	case DKIOCFLUSHWRITECACHE:
3212*1aff8f07SAlexandre Chartre 		if (vd->file)
3213edcc0754Sachartre 			return (VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL));
3214*1aff8f07SAlexandre Chartre 		else
3215*1aff8f07SAlexandre Chartre 			return (ldi_ioctl(vd->ldi_handle[0], cmd,
3216*1aff8f07SAlexandre Chartre 			    (intptr_t)ioctl_arg, vd->open_flags | FKIOCTL,
3217*1aff8f07SAlexandre Chartre 			    kcred, &rval));
3218edcc0754Sachartre 
3219edcc0754Sachartre 	case DKIOCGETEFI:
3220edcc0754Sachartre 		ASSERT(ioctl_arg != NULL);
3221edcc0754Sachartre 		efi = (dk_efi_t *)ioctl_arg;
3222edcc0754Sachartre 
3223*1aff8f07SAlexandre Chartre 		if (vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BREAD,
3224edcc0754Sachartre 		    (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0)
3225edcc0754Sachartre 			return (EIO);
3226edcc0754Sachartre 
3227edcc0754Sachartre 		return (0);
3228edcc0754Sachartre 
3229edcc0754Sachartre 	case DKIOCSETEFI:
3230edcc0754Sachartre 		ASSERT(ioctl_arg != NULL);
3231edcc0754Sachartre 		efi = (dk_efi_t *)ioctl_arg;
3232edcc0754Sachartre 
3233*1aff8f07SAlexandre Chartre 		if (vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE,
3234edcc0754Sachartre 		    (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0)
3235edcc0754Sachartre 			return (EIO);
3236edcc0754Sachartre 
3237edcc0754Sachartre 		break;
3238edcc0754Sachartre 
3239edcc0754Sachartre 
3240edcc0754Sachartre 	default:
3241edcc0754Sachartre 		return (ENOTSUP);
3242edcc0754Sachartre 	}
3243edcc0754Sachartre 
3244342440ecSPrasad Singamsetty 	ASSERT(cmd == DKIOCSEXTVTOC || cmd == DKIOCSETEFI);
3245edcc0754Sachartre 
3246edcc0754Sachartre 	/* label has changed, revalidate the geometry */
3247*1aff8f07SAlexandre Chartre 	(void) vd_dskimg_validate_geometry(vd);
32483c96341aSnarayan 
324987a7269eSachartre 	/*
325087a7269eSachartre 	 * The disk geometry may have changed, so we need to write
325187a7269eSachartre 	 * the devid (if there is one) so that it is stored at the
325287a7269eSachartre 	 * right location.
325387a7269eSachartre 	 */
3254*1aff8f07SAlexandre Chartre 	if (vd_dskimg_write_devid(vd, vd->dskimg_devid) != 0) {
325587a7269eSachartre 		PR0("Fail to write devid");
32561ae08745Sheppo 	}
32574bac2208Snarayan 
32584bac2208Snarayan 	return (0);
32594bac2208Snarayan }
3260edcc0754Sachartre 
3261edcc0754Sachartre static int
3262edcc0754Sachartre vd_backend_ioctl(vd_t *vd, int cmd, caddr_t arg)
3263edcc0754Sachartre {
3264edcc0754Sachartre 	int rval = 0, status;
3265342440ecSPrasad Singamsetty 	struct vtoc vtoc;
3266edcc0754Sachartre 
3267edcc0754Sachartre 	/*
3268edcc0754Sachartre 	 * Call the appropriate function to execute the ioctl depending
3269edcc0754Sachartre 	 * on the type of vdisk.
3270edcc0754Sachartre 	 */
3271edcc0754Sachartre 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
3272edcc0754Sachartre 
3273edcc0754Sachartre 		/* slice, file or volume exported as a single slice disk */
3274edcc0754Sachartre 		status = vd_do_slice_ioctl(vd, cmd, arg);
3275edcc0754Sachartre 
3276*1aff8f07SAlexandre Chartre 	} else if (VD_DSKIMG(vd)) {
3277edcc0754Sachartre 
3278edcc0754Sachartre 		/* file or volume exported as a full disk */
3279*1aff8f07SAlexandre Chartre 		status = vd_do_dskimg_ioctl(vd, cmd, arg);
3280edcc0754Sachartre 
3281edcc0754Sachartre 	} else {
3282edcc0754Sachartre 
3283edcc0754Sachartre 		/* disk device exported as a full disk */
3284edcc0754Sachartre 		status = ldi_ioctl(vd->ldi_handle[0], cmd, (intptr_t)arg,
3285edcc0754Sachartre 		    vd->open_flags | FKIOCTL, kcred, &rval);
3286342440ecSPrasad Singamsetty 
3287342440ecSPrasad Singamsetty 		/*
3288342440ecSPrasad Singamsetty 		 * By default VTOC ioctls are done using ioctls for the
3289342440ecSPrasad Singamsetty 		 * extended VTOC. Some drivers (in particular non-Sun drivers)
3290342440ecSPrasad Singamsetty 		 * may not support these ioctls. In that case, we fallback to
3291342440ecSPrasad Singamsetty 		 * the regular VTOC ioctls.
3292342440ecSPrasad Singamsetty 		 */
3293342440ecSPrasad Singamsetty 		if (status == ENOTTY) {
3294342440ecSPrasad Singamsetty 			switch (cmd) {
3295342440ecSPrasad Singamsetty 
3296342440ecSPrasad Singamsetty 			case DKIOCGEXTVTOC:
3297342440ecSPrasad Singamsetty 				cmd = DKIOCGVTOC;
3298342440ecSPrasad Singamsetty 				status = ldi_ioctl(vd->ldi_handle[0], cmd,
3299342440ecSPrasad Singamsetty 				    (intptr_t)&vtoc, vd->open_flags | FKIOCTL,
3300342440ecSPrasad Singamsetty 				    kcred, &rval);
3301342440ecSPrasad Singamsetty 				vtoctoextvtoc(vtoc,
3302342440ecSPrasad Singamsetty 				    (*(struct extvtoc *)(void *)arg));
3303342440ecSPrasad Singamsetty 				break;
3304342440ecSPrasad Singamsetty 
3305342440ecSPrasad Singamsetty 			case DKIOCSEXTVTOC:
3306342440ecSPrasad Singamsetty 				cmd = DKIOCSVTOC;
3307342440ecSPrasad Singamsetty 				extvtoctovtoc((*(struct extvtoc *)(void *)arg),
3308342440ecSPrasad Singamsetty 				    vtoc);
3309342440ecSPrasad Singamsetty 				status = ldi_ioctl(vd->ldi_handle[0], cmd,
3310342440ecSPrasad Singamsetty 				    (intptr_t)&vtoc, vd->open_flags | FKIOCTL,
3311342440ecSPrasad Singamsetty 				    kcred, &rval);
3312342440ecSPrasad Singamsetty 				break;
3313342440ecSPrasad Singamsetty 			}
3314342440ecSPrasad Singamsetty 		}
3315edcc0754Sachartre 	}
3316edcc0754Sachartre 
3317edcc0754Sachartre #ifdef DEBUG
3318edcc0754Sachartre 	if (rval != 0) {
3319edcc0754Sachartre 		PR0("ioctl %x set rval = %d, which is not being returned"
3320edcc0754Sachartre 		    " to caller", cmd, rval);
3321edcc0754Sachartre 	}
3322edcc0754Sachartre #endif /* DEBUG */
3323edcc0754Sachartre 
3324edcc0754Sachartre 	return (status);
33251ae08745Sheppo }
33261ae08745Sheppo 
3327205eeb1aSlm66018 /*
3328205eeb1aSlm66018  * Description:
3329205eeb1aSlm66018  *	This is the function that processes the ioctl requests (farming it
3330205eeb1aSlm66018  *	out to functions that handle slices, files or whole disks)
3331205eeb1aSlm66018  *
3332205eeb1aSlm66018  * Return Values
3333205eeb1aSlm66018  *     0		- ioctl operation completed successfully
3334205eeb1aSlm66018  *     != 0		- The LDC error value encountered
3335205eeb1aSlm66018  *			  (propagated back up the call stack as a task error)
3336205eeb1aSlm66018  *
3337205eeb1aSlm66018  * Side Effect
3338205eeb1aSlm66018  *     sets request->status to the return value of the ioctl function.
3339205eeb1aSlm66018  */
33401ae08745Sheppo static int
33410a55fbb7Slm66018 vd_do_ioctl(vd_t *vd, vd_dring_payload_t *request, void* buf, vd_ioctl_t *ioctl)
33421ae08745Sheppo {
3343edcc0754Sachartre 	int	status = 0;
33441ae08745Sheppo 	size_t	nbytes = request->nbytes;	/* modifiable copy */
33451ae08745Sheppo 
33461ae08745Sheppo 
33471ae08745Sheppo 	ASSERT(request->slice < vd->nslices);
33481ae08745Sheppo 	PR0("Performing %s", ioctl->operation_name);
33491ae08745Sheppo 
33500a55fbb7Slm66018 	/* Get data from client and convert, if necessary */
33510a55fbb7Slm66018 	if (ioctl->copyin != NULL)  {
33521ae08745Sheppo 		ASSERT(nbytes != 0 && buf != NULL);
33531ae08745Sheppo 		PR1("Getting \"arg\" data from client");
33541ae08745Sheppo 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
33551ae08745Sheppo 		    request->cookie, request->ncookies,
33561ae08745Sheppo 		    LDC_COPY_IN)) != 0) {
33573af08d82Slm66018 			PR0("ldc_mem_copy() returned errno %d "
33581ae08745Sheppo 			    "copying from client", status);
33591ae08745Sheppo 			return (status);
33601ae08745Sheppo 		}
33610a55fbb7Slm66018 
33620a55fbb7Slm66018 		/* Convert client's data, if necessary */
33632f5224aeSachartre 		if (ioctl->copyin == VD_IDENTITY_IN) {
33642f5224aeSachartre 			/* use client buffer */
33650a55fbb7Slm66018 			ioctl->arg = buf;
33662f5224aeSachartre 		} else {
33672f5224aeSachartre 			/* convert client vdisk operation data to ioctl data */
33682f5224aeSachartre 			status = (ioctl->copyin)(buf, nbytes,
33692f5224aeSachartre 			    (void *)ioctl->arg);
33702f5224aeSachartre 			if (status != 0) {
33712f5224aeSachartre 				request->status = status;
33722f5224aeSachartre 				return (0);
33732f5224aeSachartre 			}
33742f5224aeSachartre 		}
33752f5224aeSachartre 	}
33762f5224aeSachartre 
33772f5224aeSachartre 	if (ioctl->operation == VD_OP_SCSICMD) {
33782f5224aeSachartre 		struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl->arg;
33792f5224aeSachartre 
33802f5224aeSachartre 		/* check write permission */
33812f5224aeSachartre 		if (!(vd->open_flags & FWRITE) &&
33822f5224aeSachartre 		    !(uscsi->uscsi_flags & USCSI_READ)) {
33832f5224aeSachartre 			PR0("uscsi fails because backend is opened read-only");
33842f5224aeSachartre 			request->status = EROFS;
33852f5224aeSachartre 			return (0);
33862f5224aeSachartre 		}
33871ae08745Sheppo 	}
33881ae08745Sheppo 
33891ae08745Sheppo 	/*
3390edcc0754Sachartre 	 * Send the ioctl to the disk backend.
33911ae08745Sheppo 	 */
3392edcc0754Sachartre 	request->status = vd_backend_ioctl(vd, ioctl->cmd, ioctl->arg);
3393205eeb1aSlm66018 
3394205eeb1aSlm66018 	if (request->status != 0) {
3395205eeb1aSlm66018 		PR0("ioctl(%s) = errno %d", ioctl->cmd_name, request->status);
33962f5224aeSachartre 		if (ioctl->operation == VD_OP_SCSICMD &&
33972f5224aeSachartre 		    ((struct uscsi_cmd *)ioctl->arg)->uscsi_status != 0)
33982f5224aeSachartre 			/*
33992f5224aeSachartre 			 * USCSICMD has reported an error and the uscsi_status
34002f5224aeSachartre 			 * field is not zero. This means that the SCSI command
34012f5224aeSachartre 			 * has completed but it has an error. So we should
34022f5224aeSachartre 			 * mark the VD operation has succesfully completed
34032f5224aeSachartre 			 * and clients can check the SCSI status field for
34042f5224aeSachartre 			 * SCSI errors.
34052f5224aeSachartre 			 */
34062f5224aeSachartre 			request->status = 0;
34072f5224aeSachartre 		else
3408205eeb1aSlm66018 			return (0);
3409205eeb1aSlm66018 	}
34101ae08745Sheppo 
34110a55fbb7Slm66018 	/* Convert data and send to client, if necessary */
34120a55fbb7Slm66018 	if (ioctl->copyout != NULL)  {
34131ae08745Sheppo 		ASSERT(nbytes != 0 && buf != NULL);
34141ae08745Sheppo 		PR1("Sending \"arg\" data to client");
34150a55fbb7Slm66018 
34160a55fbb7Slm66018 		/* Convert ioctl data to vdisk operation data, if necessary */
34172f5224aeSachartre 		if (ioctl->copyout != VD_IDENTITY_OUT)
34180a55fbb7Slm66018 			(ioctl->copyout)((void *)ioctl->arg, buf);
34190a55fbb7Slm66018 
34201ae08745Sheppo 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
34211ae08745Sheppo 		    request->cookie, request->ncookies,
34221ae08745Sheppo 		    LDC_COPY_OUT)) != 0) {
34233af08d82Slm66018 			PR0("ldc_mem_copy() returned errno %d "
34241ae08745Sheppo 			    "copying to client", status);
34251ae08745Sheppo 			return (status);
34261ae08745Sheppo 		}
34271ae08745Sheppo 	}
34281ae08745Sheppo 
34291ae08745Sheppo 	return (status);
34301ae08745Sheppo }
34311ae08745Sheppo 
34321ae08745Sheppo #define	RNDSIZE(expr) P2ROUNDUP(sizeof (expr), sizeof (uint64_t))
3433205eeb1aSlm66018 
3434205eeb1aSlm66018 /*
3435205eeb1aSlm66018  * Description:
3436205eeb1aSlm66018  *	This generic function is called by the task queue to complete
3437205eeb1aSlm66018  *	the processing of the tasks. The specific completion function
3438205eeb1aSlm66018  *	is passed in as a field in the task pointer.
3439205eeb1aSlm66018  *
3440205eeb1aSlm66018  * Parameters:
3441205eeb1aSlm66018  *	arg 	- opaque pointer to structure containing task to be completed
3442205eeb1aSlm66018  *
3443205eeb1aSlm66018  * Return Values
3444205eeb1aSlm66018  *	None
3445205eeb1aSlm66018  */
3446205eeb1aSlm66018 static void
3447205eeb1aSlm66018 vd_complete(void *arg)
3448205eeb1aSlm66018 {
3449205eeb1aSlm66018 	vd_task_t	*task = (vd_task_t *)arg;
3450205eeb1aSlm66018 
3451205eeb1aSlm66018 	ASSERT(task != NULL);
3452205eeb1aSlm66018 	ASSERT(task->status == EINPROGRESS);
3453205eeb1aSlm66018 	ASSERT(task->completef != NULL);
3454205eeb1aSlm66018 
3455205eeb1aSlm66018 	task->status = task->completef(task);
3456205eeb1aSlm66018 	if (task->status)
3457205eeb1aSlm66018 		PR0("%s: Error %d completing task", __func__, task->status);
3458205eeb1aSlm66018 
3459205eeb1aSlm66018 	/* Now notify the vDisk client */
3460205eeb1aSlm66018 	vd_complete_notify(task);
3461205eeb1aSlm66018 }
3462205eeb1aSlm66018 
34631ae08745Sheppo static int
3464d10e4ef2Snarayan vd_ioctl(vd_task_t *task)
34651ae08745Sheppo {
346687a7269eSachartre 	int			i, status;
34671ae08745Sheppo 	void			*buf = NULL;
34680a55fbb7Slm66018 	struct dk_geom		dk_geom = {0};
3469342440ecSPrasad Singamsetty 	struct extvtoc		vtoc = {0};
34704bac2208Snarayan 	struct dk_efi		dk_efi = {0};
34712f5224aeSachartre 	struct uscsi_cmd	uscsi = {0};
3472d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
3473d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
34740a55fbb7Slm66018 	vd_ioctl_t		ioctl[] = {
34750a55fbb7Slm66018 		/* Command (no-copy) operations */
34760a55fbb7Slm66018 		{VD_OP_FLUSH, STRINGIZE(VD_OP_FLUSH), 0,
34770a55fbb7Slm66018 		    DKIOCFLUSHWRITECACHE, STRINGIZE(DKIOCFLUSHWRITECACHE),
3478047ba61eSachartre 		    NULL, NULL, NULL, B_TRUE},
34790a55fbb7Slm66018 
34800a55fbb7Slm66018 		/* "Get" (copy-out) operations */
34810a55fbb7Slm66018 		{VD_OP_GET_WCE, STRINGIZE(VD_OP_GET_WCE), RNDSIZE(int),
34820a55fbb7Slm66018 		    DKIOCGETWCE, STRINGIZE(DKIOCGETWCE),
34832f5224aeSachartre 		    NULL, VD_IDENTITY_IN, VD_IDENTITY_OUT, B_FALSE},
34840a55fbb7Slm66018 		{VD_OP_GET_DISKGEOM, STRINGIZE(VD_OP_GET_DISKGEOM),
34850a55fbb7Slm66018 		    RNDSIZE(vd_geom_t),
34860a55fbb7Slm66018 		    DKIOCGGEOM, STRINGIZE(DKIOCGGEOM),
3487047ba61eSachartre 		    &dk_geom, NULL, dk_geom2vd_geom, B_FALSE},
34880a55fbb7Slm66018 		{VD_OP_GET_VTOC, STRINGIZE(VD_OP_GET_VTOC), RNDSIZE(vd_vtoc_t),
3489342440ecSPrasad Singamsetty 		    DKIOCGEXTVTOC, STRINGIZE(DKIOCGEXTVTOC),
3490047ba61eSachartre 		    &vtoc, NULL, vtoc2vd_vtoc, B_FALSE},
34914bac2208Snarayan 		{VD_OP_GET_EFI, STRINGIZE(VD_OP_GET_EFI), RNDSIZE(vd_efi_t),
34924bac2208Snarayan 		    DKIOCGETEFI, STRINGIZE(DKIOCGETEFI),
3493047ba61eSachartre 		    &dk_efi, vd_get_efi_in, vd_get_efi_out, B_FALSE},
34940a55fbb7Slm66018 
34950a55fbb7Slm66018 		/* "Set" (copy-in) operations */
34960a55fbb7Slm66018 		{VD_OP_SET_WCE, STRINGIZE(VD_OP_SET_WCE), RNDSIZE(int),
34970a55fbb7Slm66018 		    DKIOCSETWCE, STRINGIZE(DKIOCSETWCE),
34982f5224aeSachartre 		    NULL, VD_IDENTITY_IN, VD_IDENTITY_OUT, B_TRUE},
34990a55fbb7Slm66018 		{VD_OP_SET_DISKGEOM, STRINGIZE(VD_OP_SET_DISKGEOM),
35000a55fbb7Slm66018 		    RNDSIZE(vd_geom_t),
35010a55fbb7Slm66018 		    DKIOCSGEOM, STRINGIZE(DKIOCSGEOM),
3502047ba61eSachartre 		    &dk_geom, vd_geom2dk_geom, NULL, B_TRUE},
35030a55fbb7Slm66018 		{VD_OP_SET_VTOC, STRINGIZE(VD_OP_SET_VTOC), RNDSIZE(vd_vtoc_t),
3504342440ecSPrasad Singamsetty 		    DKIOCSEXTVTOC, STRINGIZE(DKIOCSEXTVTOC),
3505047ba61eSachartre 		    &vtoc, vd_vtoc2vtoc, NULL, B_TRUE},
35064bac2208Snarayan 		{VD_OP_SET_EFI, STRINGIZE(VD_OP_SET_EFI), RNDSIZE(vd_efi_t),
35074bac2208Snarayan 		    DKIOCSETEFI, STRINGIZE(DKIOCSETEFI),
3508047ba61eSachartre 		    &dk_efi, vd_set_efi_in, vd_set_efi_out, B_TRUE},
35092f5224aeSachartre 
35102f5224aeSachartre 		{VD_OP_SCSICMD, STRINGIZE(VD_OP_SCSICMD), RNDSIZE(vd_scsi_t),
35112f5224aeSachartre 		    USCSICMD, STRINGIZE(USCSICMD),
35122f5224aeSachartre 		    &uscsi, vd_scsicmd_in, vd_scsicmd_out, B_FALSE},
35130a55fbb7Slm66018 	};
35141ae08745Sheppo 	size_t		nioctls = (sizeof (ioctl))/(sizeof (ioctl[0]));
35151ae08745Sheppo 
35161ae08745Sheppo 
3517d10e4ef2Snarayan 	ASSERT(vd != NULL);
3518d10e4ef2Snarayan 	ASSERT(request != NULL);
35191ae08745Sheppo 	ASSERT(request->slice < vd->nslices);
35201ae08745Sheppo 
35211ae08745Sheppo 	/*
35221ae08745Sheppo 	 * Determine ioctl corresponding to caller's "operation" and
35231ae08745Sheppo 	 * validate caller's "nbytes"
35241ae08745Sheppo 	 */
35251ae08745Sheppo 	for (i = 0; i < nioctls; i++) {
35261ae08745Sheppo 		if (request->operation == ioctl[i].operation) {
35270a55fbb7Slm66018 			/* LDC memory operations require 8-byte multiples */
35280a55fbb7Slm66018 			ASSERT(ioctl[i].nbytes % sizeof (uint64_t) == 0);
35290a55fbb7Slm66018 
35304bac2208Snarayan 			if (request->operation == VD_OP_GET_EFI ||
35312f5224aeSachartre 			    request->operation == VD_OP_SET_EFI ||
35322f5224aeSachartre 			    request->operation == VD_OP_SCSICMD) {
35334bac2208Snarayan 				if (request->nbytes >= ioctl[i].nbytes)
35344bac2208Snarayan 					break;
35353af08d82Slm66018 				PR0("%s:  Expected at least nbytes = %lu, "
35364bac2208Snarayan 				    "got %lu", ioctl[i].operation_name,
35374bac2208Snarayan 				    ioctl[i].nbytes, request->nbytes);
35384bac2208Snarayan 				return (EINVAL);
35394bac2208Snarayan 			}
35404bac2208Snarayan 
35410a55fbb7Slm66018 			if (request->nbytes != ioctl[i].nbytes) {
35423af08d82Slm66018 				PR0("%s:  Expected nbytes = %lu, got %lu",
35430a55fbb7Slm66018 				    ioctl[i].operation_name, ioctl[i].nbytes,
35440a55fbb7Slm66018 				    request->nbytes);
35451ae08745Sheppo 				return (EINVAL);
35461ae08745Sheppo 			}
35471ae08745Sheppo 
35481ae08745Sheppo 			break;
35491ae08745Sheppo 		}
35501ae08745Sheppo 	}
35511ae08745Sheppo 	ASSERT(i < nioctls);	/* because "operation" already validated */
35521ae08745Sheppo 
3553047ba61eSachartre 	if (!(vd->open_flags & FWRITE) && ioctl[i].write) {
3554047ba61eSachartre 		PR0("%s fails because backend is opened read-only",
3555047ba61eSachartre 		    ioctl[i].operation_name);
3556047ba61eSachartre 		request->status = EROFS;
3557047ba61eSachartre 		return (0);
3558047ba61eSachartre 	}
3559047ba61eSachartre 
35601ae08745Sheppo 	if (request->nbytes)
35611ae08745Sheppo 		buf = kmem_zalloc(request->nbytes, KM_SLEEP);
35621ae08745Sheppo 	status = vd_do_ioctl(vd, request, buf, &ioctl[i]);
35631ae08745Sheppo 	if (request->nbytes)
35641ae08745Sheppo 		kmem_free(buf, request->nbytes);
356587a7269eSachartre 
35661ae08745Sheppo 	return (status);
35671ae08745Sheppo }
35681ae08745Sheppo 
35694bac2208Snarayan static int
35704bac2208Snarayan vd_get_devid(vd_task_t *task)
35714bac2208Snarayan {
35724bac2208Snarayan 	vd_t *vd = task->vd;
35734bac2208Snarayan 	vd_dring_payload_t *request = task->request;
35744bac2208Snarayan 	vd_devid_t *vd_devid;
35754bac2208Snarayan 	impl_devid_t *devid;
357687a7269eSachartre 	int status, bufid_len, devid_len, len, sz;
35773af08d82Slm66018 	int bufbytes;
35784bac2208Snarayan 
35793af08d82Slm66018 	PR1("Get Device ID, nbytes=%ld", request->nbytes);
35804bac2208Snarayan 
3581bae9e67eSachartre 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
3582bae9e67eSachartre 		/*
3583bae9e67eSachartre 		 * We don't support devid for single-slice disks because we
3584bae9e67eSachartre 		 * have no space to store a fabricated devid and for physical
3585bae9e67eSachartre 		 * disk slices, we can't use the devid of the disk otherwise
3586bae9e67eSachartre 		 * exporting multiple slices from the same disk will produce
3587bae9e67eSachartre 		 * the same devids.
3588bae9e67eSachartre 		 */
3589bae9e67eSachartre 		PR2("No Device ID for slices");
3590bae9e67eSachartre 		request->status = ENOTSUP;
3591bae9e67eSachartre 		return (0);
3592bae9e67eSachartre 	}
3593bae9e67eSachartre 
3594*1aff8f07SAlexandre Chartre 	if (VD_DSKIMG(vd)) {
3595*1aff8f07SAlexandre Chartre 		if (vd->dskimg_devid == NULL) {
35963af08d82Slm66018 			PR2("No Device ID");
3597205eeb1aSlm66018 			request->status = ENOENT;
3598205eeb1aSlm66018 			return (0);
359987a7269eSachartre 		} else {
3600*1aff8f07SAlexandre Chartre 			sz = ddi_devid_sizeof(vd->dskimg_devid);
360187a7269eSachartre 			devid = kmem_alloc(sz, KM_SLEEP);
3602*1aff8f07SAlexandre Chartre 			bcopy(vd->dskimg_devid, devid, sz);
360387a7269eSachartre 		}
360487a7269eSachartre 	} else {
360587a7269eSachartre 		if (ddi_lyr_get_devid(vd->dev[request->slice],
360687a7269eSachartre 		    (ddi_devid_t *)&devid) != DDI_SUCCESS) {
360787a7269eSachartre 			PR2("No Device ID");
3608205eeb1aSlm66018 			request->status = ENOENT;
3609205eeb1aSlm66018 			return (0);
361087a7269eSachartre 		}
36114bac2208Snarayan 	}
36124bac2208Snarayan 
36134bac2208Snarayan 	bufid_len = request->nbytes - sizeof (vd_devid_t) + 1;
36144bac2208Snarayan 	devid_len = DEVID_GETLEN(devid);
36154bac2208Snarayan 
36163af08d82Slm66018 	/*
36173af08d82Slm66018 	 * Save the buffer size here for use in deallocation.
36183af08d82Slm66018 	 * The actual number of bytes copied is returned in
36193af08d82Slm66018 	 * the 'nbytes' field of the request structure.
36203af08d82Slm66018 	 */
36213af08d82Slm66018 	bufbytes = request->nbytes;
36223af08d82Slm66018 
36233af08d82Slm66018 	vd_devid = kmem_zalloc(bufbytes, KM_SLEEP);
36244bac2208Snarayan 	vd_devid->length = devid_len;
36254bac2208Snarayan 	vd_devid->type = DEVID_GETTYPE(devid);
36264bac2208Snarayan 
36274bac2208Snarayan 	len = (devid_len > bufid_len)? bufid_len : devid_len;
36284bac2208Snarayan 
36294bac2208Snarayan 	bcopy(devid->did_id, vd_devid->id, len);
36304bac2208Snarayan 
363178fcd0a1Sachartre 	request->status = 0;
363278fcd0a1Sachartre 
36334bac2208Snarayan 	/* LDC memory operations require 8-byte multiples */
36344bac2208Snarayan 	ASSERT(request->nbytes % sizeof (uint64_t) == 0);
36354bac2208Snarayan 
36364bac2208Snarayan 	if ((status = ldc_mem_copy(vd->ldc_handle, (caddr_t)vd_devid, 0,
36374bac2208Snarayan 	    &request->nbytes, request->cookie, request->ncookies,
36384bac2208Snarayan 	    LDC_COPY_OUT)) != 0) {
36393af08d82Slm66018 		PR0("ldc_mem_copy() returned errno %d copying to client",
36404bac2208Snarayan 		    status);
36414bac2208Snarayan 	}
36423af08d82Slm66018 	PR1("post mem_copy: nbytes=%ld", request->nbytes);
36434bac2208Snarayan 
36443af08d82Slm66018 	kmem_free(vd_devid, bufbytes);
36454bac2208Snarayan 	ddi_devid_free((ddi_devid_t)devid);
36464bac2208Snarayan 
36474bac2208Snarayan 	return (status);
36484bac2208Snarayan }
36494bac2208Snarayan 
36502f5224aeSachartre static int
36512f5224aeSachartre vd_scsi_reset(vd_t *vd)
36522f5224aeSachartre {
36532f5224aeSachartre 	int rval, status;
36542f5224aeSachartre 	struct uscsi_cmd uscsi = { 0 };
36552f5224aeSachartre 
36562f5224aeSachartre 	uscsi.uscsi_flags = vd_scsi_debug | USCSI_RESET;
36572f5224aeSachartre 	uscsi.uscsi_timeout = vd_scsi_rdwr_timeout;
36582f5224aeSachartre 
36592f5224aeSachartre 	status = ldi_ioctl(vd->ldi_handle[0], USCSICMD, (intptr_t)&uscsi,
36602f5224aeSachartre 	    (vd->open_flags | FKIOCTL), kcred, &rval);
36612f5224aeSachartre 
36622f5224aeSachartre 	return (status);
36632f5224aeSachartre }
36642f5224aeSachartre 
36652f5224aeSachartre static int
36662f5224aeSachartre vd_reset(vd_task_t *task)
36672f5224aeSachartre {
36682f5224aeSachartre 	vd_t *vd = task->vd;
36692f5224aeSachartre 	vd_dring_payload_t *request = task->request;
36702f5224aeSachartre 
36712f5224aeSachartre 	ASSERT(request->operation == VD_OP_RESET);
36722f5224aeSachartre 	ASSERT(vd->scsi);
36732f5224aeSachartre 
36742f5224aeSachartre 	PR0("Performing VD_OP_RESET");
36752f5224aeSachartre 
36762f5224aeSachartre 	if (request->nbytes != 0) {
36772f5224aeSachartre 		PR0("VD_OP_RESET:  Expected nbytes = 0, got %lu",
36782f5224aeSachartre 		    request->nbytes);
36792f5224aeSachartre 		return (EINVAL);
36802f5224aeSachartre 	}
36812f5224aeSachartre 
36822f5224aeSachartre 	request->status = vd_scsi_reset(vd);
36832f5224aeSachartre 
36842f5224aeSachartre 	return (0);
36852f5224aeSachartre }
36862f5224aeSachartre 
36872f5224aeSachartre static int
36882f5224aeSachartre vd_get_capacity(vd_task_t *task)
36892f5224aeSachartre {
36902f5224aeSachartre 	int rv;
36912f5224aeSachartre 	size_t nbytes;
36922f5224aeSachartre 	vd_t *vd = task->vd;
36932f5224aeSachartre 	vd_dring_payload_t *request = task->request;
36942f5224aeSachartre 	vd_capacity_t vd_cap = { 0 };
36952f5224aeSachartre 
36962f5224aeSachartre 	ASSERT(request->operation == VD_OP_GET_CAPACITY);
36972f5224aeSachartre 
36982f5224aeSachartre 	PR0("Performing VD_OP_GET_CAPACITY");
36992f5224aeSachartre 
37002f5224aeSachartre 	nbytes = request->nbytes;
37012f5224aeSachartre 
37022f5224aeSachartre 	if (nbytes != RNDSIZE(vd_capacity_t)) {
37032f5224aeSachartre 		PR0("VD_OP_GET_CAPACITY:  Expected nbytes = %lu, got %lu",
37042f5224aeSachartre 		    RNDSIZE(vd_capacity_t), nbytes);
37052f5224aeSachartre 		return (EINVAL);
37062f5224aeSachartre 	}
37072f5224aeSachartre 
3708de3a5331SRamesh Chitrothu 	/*
3709de3a5331SRamesh Chitrothu 	 * Check the backend size in case it has changed. If the check fails
3710de3a5331SRamesh Chitrothu 	 * then we will return the last known size.
3711de3a5331SRamesh Chitrothu 	 */
37122f5224aeSachartre 
3713de3a5331SRamesh Chitrothu 	(void) vd_backend_check_size(vd);
37142f5224aeSachartre 	ASSERT(vd->vdisk_size != 0);
37152f5224aeSachartre 
37162f5224aeSachartre 	request->status = 0;
37172f5224aeSachartre 
37182f5224aeSachartre 	vd_cap.vdisk_block_size = vd->vdisk_block_size;
37192f5224aeSachartre 	vd_cap.vdisk_size = vd->vdisk_size;
37202f5224aeSachartre 
37212f5224aeSachartre 	if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&vd_cap, 0, &nbytes,
37222f5224aeSachartre 	    request->cookie, request->ncookies, LDC_COPY_OUT)) != 0) {
37232f5224aeSachartre 		PR0("ldc_mem_copy() returned errno %d copying to client", rv);
37242f5224aeSachartre 		return (rv);
37252f5224aeSachartre 	}
37262f5224aeSachartre 
37272f5224aeSachartre 	return (0);
37282f5224aeSachartre }
37292f5224aeSachartre 
37302f5224aeSachartre static int
37312f5224aeSachartre vd_get_access(vd_task_t *task)
37322f5224aeSachartre {
37332f5224aeSachartre 	uint64_t access;
37342f5224aeSachartre 	int rv, rval = 0;
37352f5224aeSachartre 	size_t nbytes;
37362f5224aeSachartre 	vd_t *vd = task->vd;
37372f5224aeSachartre 	vd_dring_payload_t *request = task->request;
37382f5224aeSachartre 
37392f5224aeSachartre 	ASSERT(request->operation == VD_OP_GET_ACCESS);
37402f5224aeSachartre 	ASSERT(vd->scsi);
37412f5224aeSachartre 
37422f5224aeSachartre 	PR0("Performing VD_OP_GET_ACCESS");
37432f5224aeSachartre 
37442f5224aeSachartre 	nbytes = request->nbytes;
37452f5224aeSachartre 
37462f5224aeSachartre 	if (nbytes != sizeof (uint64_t)) {
37472f5224aeSachartre 		PR0("VD_OP_GET_ACCESS:  Expected nbytes = %lu, got %lu",
37482f5224aeSachartre 		    sizeof (uint64_t), nbytes);
37492f5224aeSachartre 		return (EINVAL);
37502f5224aeSachartre 	}
37512f5224aeSachartre 
37522f5224aeSachartre 	request->status = ldi_ioctl(vd->ldi_handle[request->slice], MHIOCSTATUS,
37532f5224aeSachartre 	    NULL, (vd->open_flags | FKIOCTL), kcred, &rval);
37542f5224aeSachartre 
37552f5224aeSachartre 	if (request->status != 0)
37562f5224aeSachartre 		return (0);
37572f5224aeSachartre 
37582f5224aeSachartre 	access = (rval == 0)? VD_ACCESS_ALLOWED : VD_ACCESS_DENIED;
37592f5224aeSachartre 
37602f5224aeSachartre 	if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&access, 0, &nbytes,
37612f5224aeSachartre 	    request->cookie, request->ncookies, LDC_COPY_OUT)) != 0) {
37622f5224aeSachartre 		PR0("ldc_mem_copy() returned errno %d copying to client", rv);
37632f5224aeSachartre 		return (rv);
37642f5224aeSachartre 	}
37652f5224aeSachartre 
37662f5224aeSachartre 	return (0);
37672f5224aeSachartre }
37682f5224aeSachartre 
37692f5224aeSachartre static int
37702f5224aeSachartre vd_set_access(vd_task_t *task)
37712f5224aeSachartre {
37722f5224aeSachartre 	uint64_t flags;
37732f5224aeSachartre 	int rv, rval;
37742f5224aeSachartre 	size_t nbytes;
37752f5224aeSachartre 	vd_t *vd = task->vd;
37762f5224aeSachartre 	vd_dring_payload_t *request = task->request;
37772f5224aeSachartre 
37782f5224aeSachartre 	ASSERT(request->operation == VD_OP_SET_ACCESS);
37792f5224aeSachartre 	ASSERT(vd->scsi);
37802f5224aeSachartre 
37812f5224aeSachartre 	nbytes = request->nbytes;
37822f5224aeSachartre 
37832f5224aeSachartre 	if (nbytes != sizeof (uint64_t)) {
37842f5224aeSachartre 		PR0("VD_OP_SET_ACCESS:  Expected nbytes = %lu, got %lu",
37852f5224aeSachartre 		    sizeof (uint64_t), nbytes);
37862f5224aeSachartre 		return (EINVAL);
37872f5224aeSachartre 	}
37882f5224aeSachartre 
37892f5224aeSachartre 	if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&flags, 0, &nbytes,
37902f5224aeSachartre 	    request->cookie, request->ncookies, LDC_COPY_IN)) != 0) {
37912f5224aeSachartre 		PR0("ldc_mem_copy() returned errno %d copying from client", rv);
37922f5224aeSachartre 		return (rv);
37932f5224aeSachartre 	}
37942f5224aeSachartre 
37952f5224aeSachartre 	if (flags == VD_ACCESS_SET_CLEAR) {
37962f5224aeSachartre 		PR0("Performing VD_OP_SET_ACCESS (CLEAR)");
37972f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
37982f5224aeSachartre 		    MHIOCRELEASE, NULL, (vd->open_flags | FKIOCTL), kcred,
37992f5224aeSachartre 		    &rval);
38002f5224aeSachartre 		if (request->status == 0)
38012f5224aeSachartre 			vd->ownership = B_FALSE;
38022f5224aeSachartre 		return (0);
38032f5224aeSachartre 	}
38042f5224aeSachartre 
38052f5224aeSachartre 	/*
38062f5224aeSachartre 	 * As per the VIO spec, the PREEMPT and PRESERVE flags are only valid
38072f5224aeSachartre 	 * when the EXCLUSIVE flag is set.
38082f5224aeSachartre 	 */
38092f5224aeSachartre 	if (!(flags & VD_ACCESS_SET_EXCLUSIVE)) {
38102f5224aeSachartre 		PR0("Invalid VD_OP_SET_ACCESS flags: 0x%lx", flags);
38112f5224aeSachartre 		request->status = EINVAL;
38122f5224aeSachartre 		return (0);
38132f5224aeSachartre 	}
38142f5224aeSachartre 
38152f5224aeSachartre 	switch (flags & (VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE)) {
38162f5224aeSachartre 
38172f5224aeSachartre 	case VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE:
38182f5224aeSachartre 		/*
38192f5224aeSachartre 		 * Flags EXCLUSIVE and PREEMPT and PRESERVE. We have to
38202f5224aeSachartre 		 * acquire exclusive access rights, preserve them and we
38212f5224aeSachartre 		 * can use preemption. So we can use the MHIOCTKNOWN ioctl.
38222f5224aeSachartre 		 */
38232f5224aeSachartre 		PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT|PRESERVE)");
38242f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
38252f5224aeSachartre 		    MHIOCTKOWN, NULL, (vd->open_flags | FKIOCTL), kcred, &rval);
38262f5224aeSachartre 		break;
38272f5224aeSachartre 
38282f5224aeSachartre 	case VD_ACCESS_SET_PRESERVE:
38292f5224aeSachartre 		/*
38302f5224aeSachartre 		 * Flags EXCLUSIVE and PRESERVE. We have to acquire exclusive
38312f5224aeSachartre 		 * access rights and preserve them, but not preempt any other
38322f5224aeSachartre 		 * host. So we need to use the MHIOCTKOWN ioctl to enable the
38332f5224aeSachartre 		 * "preserve" feature but we can not called it directly
38342f5224aeSachartre 		 * because it uses preemption. So before that, we use the
38352f5224aeSachartre 		 * MHIOCQRESERVE ioctl to ensure we can get exclusive rights
38362f5224aeSachartre 		 * without preempting anyone.
38372f5224aeSachartre 		 */
38382f5224aeSachartre 		PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PRESERVE)");
38392f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
38402f5224aeSachartre 		    MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
38412f5224aeSachartre 		    &rval);
38422f5224aeSachartre 		if (request->status != 0)
38432f5224aeSachartre 			break;
38442f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
38452f5224aeSachartre 		    MHIOCTKOWN, NULL, (vd->open_flags | FKIOCTL), kcred, &rval);
38462f5224aeSachartre 		break;
38472f5224aeSachartre 
38482f5224aeSachartre 	case VD_ACCESS_SET_PREEMPT:
38492f5224aeSachartre 		/*
38502f5224aeSachartre 		 * Flags EXCLUSIVE and PREEMPT. We have to acquire exclusive
38512f5224aeSachartre 		 * access rights and we can use preemption. So we try to do
38522f5224aeSachartre 		 * a SCSI reservation, if it fails we reset the disk to clear
38532f5224aeSachartre 		 * any reservation and we try to reserve again.
38542f5224aeSachartre 		 */
38552f5224aeSachartre 		PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT)");
38562f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
38572f5224aeSachartre 		    MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
38582f5224aeSachartre 		    &rval);
38592f5224aeSachartre 		if (request->status == 0)
38602f5224aeSachartre 			break;
38612f5224aeSachartre 
38622f5224aeSachartre 		/* reset the disk */
38632f5224aeSachartre 		(void) vd_scsi_reset(vd);
38642f5224aeSachartre 
38652f5224aeSachartre 		/* try again even if the reset has failed */
38662f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
38672f5224aeSachartre 		    MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
38682f5224aeSachartre 		    &rval);
38692f5224aeSachartre 		break;
38702f5224aeSachartre 
38712f5224aeSachartre 	case 0:
38722f5224aeSachartre 		/* Flag EXCLUSIVE only. Just issue a SCSI reservation */
38732f5224aeSachartre 		PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE)");
38742f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
38752f5224aeSachartre 		    MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
38762f5224aeSachartre 		    &rval);
38772f5224aeSachartre 		break;
38782f5224aeSachartre 	}
38792f5224aeSachartre 
38802f5224aeSachartre 	if (request->status == 0)
38812f5224aeSachartre 		vd->ownership = B_TRUE;
38822f5224aeSachartre 	else
38832f5224aeSachartre 		PR0("VD_OP_SET_ACCESS: error %d", request->status);
38842f5224aeSachartre 
38852f5224aeSachartre 	return (0);
38862f5224aeSachartre }
38872f5224aeSachartre 
38882f5224aeSachartre static void
38892f5224aeSachartre vd_reset_access(vd_t *vd)
38902f5224aeSachartre {
38912f5224aeSachartre 	int status, rval;
38922f5224aeSachartre 
3893*1aff8f07SAlexandre Chartre 	if (vd->file || vd->volume || !vd->ownership)
38942f5224aeSachartre 		return;
38952f5224aeSachartre 
38962f5224aeSachartre 	PR0("Releasing disk ownership");
38972f5224aeSachartre 	status = ldi_ioctl(vd->ldi_handle[0], MHIOCRELEASE, NULL,
38982f5224aeSachartre 	    (vd->open_flags | FKIOCTL), kcred, &rval);
38992f5224aeSachartre 
39002f5224aeSachartre 	/*
39012f5224aeSachartre 	 * An EACCES failure means that there is a reservation conflict,
39022f5224aeSachartre 	 * so we are not the owner of the disk anymore.
39032f5224aeSachartre 	 */
39042f5224aeSachartre 	if (status == 0 || status == EACCES) {
39052f5224aeSachartre 		vd->ownership = B_FALSE;
39062f5224aeSachartre 		return;
39072f5224aeSachartre 	}
39082f5224aeSachartre 
39092f5224aeSachartre 	PR0("Fail to release ownership, error %d", status);
39102f5224aeSachartre 
39112f5224aeSachartre 	/*
39122f5224aeSachartre 	 * We have failed to release the ownership, try to reset the disk
39132f5224aeSachartre 	 * to release reservations.
39142f5224aeSachartre 	 */
39152f5224aeSachartre 	PR0("Resetting disk");
39162f5224aeSachartre 	status = vd_scsi_reset(vd);
39172f5224aeSachartre 
39182f5224aeSachartre 	if (status != 0)
39192f5224aeSachartre 		PR0("Fail to reset disk, error %d", status);
39202f5224aeSachartre 
39212f5224aeSachartre 	/* whatever the result of the reset is, we try the release again */
39222f5224aeSachartre 	status = ldi_ioctl(vd->ldi_handle[0], MHIOCRELEASE, NULL,
39232f5224aeSachartre 	    (vd->open_flags | FKIOCTL), kcred, &rval);
39242f5224aeSachartre 
39252f5224aeSachartre 	if (status == 0 || status == EACCES) {
39262f5224aeSachartre 		vd->ownership = B_FALSE;
39272f5224aeSachartre 		return;
39282f5224aeSachartre 	}
39292f5224aeSachartre 
39302f5224aeSachartre 	PR0("Fail to release ownership, error %d", status);
39312f5224aeSachartre 
39322f5224aeSachartre 	/*
39332f5224aeSachartre 	 * At this point we have done our best to try to reset the
39342f5224aeSachartre 	 * access rights to the disk and we don't know if we still
39352f5224aeSachartre 	 * own a reservation and if any mechanism to preserve the
39362f5224aeSachartre 	 * ownership is still in place. The ultimate solution would
39372f5224aeSachartre 	 * be to reset the system but this is usually not what we
39382f5224aeSachartre 	 * want to happen.
39392f5224aeSachartre 	 */
39402f5224aeSachartre 
39412f5224aeSachartre 	if (vd_reset_access_failure == A_REBOOT) {
39422f5224aeSachartre 		cmn_err(CE_WARN, VD_RESET_ACCESS_FAILURE_MSG
39432f5224aeSachartre 		    ", rebooting the system", vd->device_path);
39442f5224aeSachartre 		(void) uadmin(A_SHUTDOWN, AD_BOOT, NULL);
39452f5224aeSachartre 	} else if (vd_reset_access_failure == A_DUMP) {
39462f5224aeSachartre 		panic(VD_RESET_ACCESS_FAILURE_MSG, vd->device_path);
39472f5224aeSachartre 	}
39482f5224aeSachartre 
39492f5224aeSachartre 	cmn_err(CE_WARN, VD_RESET_ACCESS_FAILURE_MSG, vd->device_path);
39502f5224aeSachartre }
39512f5224aeSachartre 
39521ae08745Sheppo /*
39531ae08745Sheppo  * Define the supported operations once the functions for performing them have
39541ae08745Sheppo  * been defined
39551ae08745Sheppo  */
39561ae08745Sheppo static const vds_operation_t	vds_operation[] = {
39573af08d82Slm66018 #define	X(_s)	#_s, _s
39583af08d82Slm66018 	{X(VD_OP_BREAD),	vd_start_bio,	vd_complete_bio},
39593af08d82Slm66018 	{X(VD_OP_BWRITE),	vd_start_bio,	vd_complete_bio},
39603af08d82Slm66018 	{X(VD_OP_FLUSH),	vd_ioctl,	NULL},
39613af08d82Slm66018 	{X(VD_OP_GET_WCE),	vd_ioctl,	NULL},
39623af08d82Slm66018 	{X(VD_OP_SET_WCE),	vd_ioctl,	NULL},
39633af08d82Slm66018 	{X(VD_OP_GET_VTOC),	vd_ioctl,	NULL},
39643af08d82Slm66018 	{X(VD_OP_SET_VTOC),	vd_ioctl,	NULL},
39653af08d82Slm66018 	{X(VD_OP_GET_DISKGEOM),	vd_ioctl,	NULL},
39663af08d82Slm66018 	{X(VD_OP_SET_DISKGEOM),	vd_ioctl,	NULL},
39673af08d82Slm66018 	{X(VD_OP_GET_EFI),	vd_ioctl,	NULL},
39683af08d82Slm66018 	{X(VD_OP_SET_EFI),	vd_ioctl,	NULL},
39693af08d82Slm66018 	{X(VD_OP_GET_DEVID),	vd_get_devid,	NULL},
39702f5224aeSachartre 	{X(VD_OP_SCSICMD),	vd_ioctl,	NULL},
39712f5224aeSachartre 	{X(VD_OP_RESET),	vd_reset,	NULL},
39722f5224aeSachartre 	{X(VD_OP_GET_CAPACITY),	vd_get_capacity, NULL},
39732f5224aeSachartre 	{X(VD_OP_SET_ACCESS),	vd_set_access,	NULL},
39742f5224aeSachartre 	{X(VD_OP_GET_ACCESS),	vd_get_access,	NULL},
39753af08d82Slm66018 #undef	X
39761ae08745Sheppo };
39771ae08745Sheppo 
39781ae08745Sheppo static const size_t	vds_noperations =
39791ae08745Sheppo 	(sizeof (vds_operation))/(sizeof (vds_operation[0]));
39801ae08745Sheppo 
39811ae08745Sheppo /*
3982d10e4ef2Snarayan  * Process a task specifying a client I/O request
3983205eeb1aSlm66018  *
3984205eeb1aSlm66018  * Parameters:
3985205eeb1aSlm66018  *	task 		- structure containing the request sent from client
3986205eeb1aSlm66018  *
3987205eeb1aSlm66018  * Return Value
3988205eeb1aSlm66018  *	0	- success
3989205eeb1aSlm66018  *	ENOTSUP	- Unknown/Unsupported VD_OP_XXX operation
3990205eeb1aSlm66018  *	EINVAL	- Invalid disk slice
3991205eeb1aSlm66018  *	!= 0	- some other non-zero return value from start function
39921ae08745Sheppo  */
39931ae08745Sheppo static int
3994205eeb1aSlm66018 vd_do_process_task(vd_task_t *task)
39951ae08745Sheppo {
3996205eeb1aSlm66018 	int			i;
3997d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
3998d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
39991ae08745Sheppo 
4000d10e4ef2Snarayan 	ASSERT(vd != NULL);
4001d10e4ef2Snarayan 	ASSERT(request != NULL);
40021ae08745Sheppo 
4003d10e4ef2Snarayan 	/* Find the requested operation */
4004205eeb1aSlm66018 	for (i = 0; i < vds_noperations; i++) {
4005205eeb1aSlm66018 		if (request->operation == vds_operation[i].operation) {
4006205eeb1aSlm66018 			/* all operations should have a start func */
4007205eeb1aSlm66018 			ASSERT(vds_operation[i].start != NULL);
4008205eeb1aSlm66018 
4009205eeb1aSlm66018 			task->completef = vds_operation[i].complete;
4010d10e4ef2Snarayan 			break;
4011205eeb1aSlm66018 		}
4012205eeb1aSlm66018 	}
401317cadca8Slm66018 
401417cadca8Slm66018 	/*
401517cadca8Slm66018 	 * We need to check that the requested operation is permitted
401617cadca8Slm66018 	 * for the particular client that sent it or that the loop above
401717cadca8Slm66018 	 * did not complete without finding the operation type (indicating
401817cadca8Slm66018 	 * that the requested operation is unknown/unimplemented)
401917cadca8Slm66018 	 */
402017cadca8Slm66018 	if ((VD_OP_SUPPORTED(vd->operations, request->operation) == B_FALSE) ||
402117cadca8Slm66018 	    (i == vds_noperations)) {
40223af08d82Slm66018 		PR0("Unsupported operation %u", request->operation);
402317cadca8Slm66018 		request->status = ENOTSUP;
402417cadca8Slm66018 		return (0);
40251ae08745Sheppo 	}
40261ae08745Sheppo 
40277636cb21Slm66018 	/* Range-check slice */
402887a7269eSachartre 	if (request->slice >= vd->nslices &&
4029bae9e67eSachartre 	    ((vd->vdisk_type != VD_DISK_TYPE_DISK && vd_slice_single_slice) ||
403087a7269eSachartre 	    request->slice != VD_SLICE_NONE)) {
40313af08d82Slm66018 		PR0("Invalid \"slice\" %u (max %u) for virtual disk",
40327636cb21Slm66018 		    request->slice, (vd->nslices - 1));
4033bae9e67eSachartre 		request->status = EINVAL;
4034bae9e67eSachartre 		return (0);
40357636cb21Slm66018 	}
40367636cb21Slm66018 
4037205eeb1aSlm66018 	/*
4038205eeb1aSlm66018 	 * Call the function pointer that starts the operation.
4039205eeb1aSlm66018 	 */
4040205eeb1aSlm66018 	return (vds_operation[i].start(task));
40411ae08745Sheppo }
40421ae08745Sheppo 
4043205eeb1aSlm66018 /*
4044205eeb1aSlm66018  * Description:
4045205eeb1aSlm66018  *	This function is called by both the in-band and descriptor ring
4046205eeb1aSlm66018  *	message processing functions paths to actually execute the task
4047205eeb1aSlm66018  *	requested by the vDisk client. It in turn calls its worker
4048205eeb1aSlm66018  *	function, vd_do_process_task(), to carry our the request.
4049205eeb1aSlm66018  *
4050205eeb1aSlm66018  *	Any transport errors (e.g. LDC errors, vDisk protocol errors) are
4051205eeb1aSlm66018  *	saved in the 'status' field of the task and are propagated back
4052205eeb1aSlm66018  *	up the call stack to trigger a NACK
4053205eeb1aSlm66018  *
4054205eeb1aSlm66018  *	Any request errors (e.g. ENOTTY from an ioctl) are saved in
4055205eeb1aSlm66018  *	the 'status' field of the request and result in an ACK being sent
4056205eeb1aSlm66018  *	by the completion handler.
4057205eeb1aSlm66018  *
4058205eeb1aSlm66018  * Parameters:
4059205eeb1aSlm66018  *	task 		- structure containing the request sent from client
4060205eeb1aSlm66018  *
4061205eeb1aSlm66018  * Return Value
4062205eeb1aSlm66018  *	0		- successful synchronous request.
4063205eeb1aSlm66018  *	!= 0		- transport error (e.g. LDC errors, vDisk protocol)
4064205eeb1aSlm66018  *	EINPROGRESS	- task will be finished in a completion handler
4065205eeb1aSlm66018  */
4066205eeb1aSlm66018 static int
4067205eeb1aSlm66018 vd_process_task(vd_task_t *task)
4068205eeb1aSlm66018 {
4069205eeb1aSlm66018 	vd_t	*vd = task->vd;
4070205eeb1aSlm66018 	int	status;
40711ae08745Sheppo 
4072205eeb1aSlm66018 	DTRACE_PROBE1(task__start, vd_task_t *, task);
40733af08d82Slm66018 
4074205eeb1aSlm66018 	task->status =  vd_do_process_task(task);
4075205eeb1aSlm66018 
4076205eeb1aSlm66018 	/*
4077205eeb1aSlm66018 	 * If the task processing function returned EINPROGRESS indicating
4078205eeb1aSlm66018 	 * that the task needs completing then schedule a taskq entry to
4079205eeb1aSlm66018 	 * finish it now.
4080205eeb1aSlm66018 	 *
4081205eeb1aSlm66018 	 * Otherwise the task processing function returned either zero
4082205eeb1aSlm66018 	 * indicating that the task was finished in the start function (and we
4083205eeb1aSlm66018 	 * don't need to wait in a completion function) or the start function
4084205eeb1aSlm66018 	 * returned an error - in both cases all that needs to happen is the
4085205eeb1aSlm66018 	 * notification to the vDisk client higher up the call stack.
4086205eeb1aSlm66018 	 * If the task was using a Descriptor Ring, we need to mark it as done
4087205eeb1aSlm66018 	 * at this stage.
4088205eeb1aSlm66018 	 */
4089205eeb1aSlm66018 	if (task->status == EINPROGRESS) {
4090d10e4ef2Snarayan 		/* Queue a task to complete the operation */
4091205eeb1aSlm66018 		(void) ddi_taskq_dispatch(vd->completionq, vd_complete,
4092d10e4ef2Snarayan 		    task, DDI_SLEEP);
4093d10e4ef2Snarayan 
4094f0ca1d9aSsb155480 	} else if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE_V1_0)) {
4095205eeb1aSlm66018 		/* Update the dring element if it's a dring client */
4096205eeb1aSlm66018 		status = vd_mark_elem_done(vd, task->index,
4097205eeb1aSlm66018 		    task->request->status, task->request->nbytes);
4098205eeb1aSlm66018 		if (status == ECONNRESET)
4099205eeb1aSlm66018 			vd_mark_in_reset(vd);
4100bbfa0259Sha137994 		else if (status == EACCES)
4101bbfa0259Sha137994 			vd_need_reset(vd, B_TRUE);
4102205eeb1aSlm66018 	}
4103205eeb1aSlm66018 
4104205eeb1aSlm66018 	return (task->status);
41051ae08745Sheppo }
41061ae08745Sheppo 
41071ae08745Sheppo /*
41080a55fbb7Slm66018  * Return true if the "type", "subtype", and "env" fields of the "tag" first
41090a55fbb7Slm66018  * argument match the corresponding remaining arguments; otherwise, return false
41101ae08745Sheppo  */
41110a55fbb7Slm66018 boolean_t
41121ae08745Sheppo vd_msgtype(vio_msg_tag_t *tag, int type, int subtype, int env)
41131ae08745Sheppo {
41141ae08745Sheppo 	return ((tag->vio_msgtype == type) &&
41151ae08745Sheppo 	    (tag->vio_subtype == subtype) &&
41160a55fbb7Slm66018 	    (tag->vio_subtype_env == env)) ? B_TRUE : B_FALSE;
41171ae08745Sheppo }
41181ae08745Sheppo 
41190a55fbb7Slm66018 /*
41200a55fbb7Slm66018  * Check whether the major/minor version specified in "ver_msg" is supported
41210a55fbb7Slm66018  * by this server.
41220a55fbb7Slm66018  */
41230a55fbb7Slm66018 static boolean_t
41240a55fbb7Slm66018 vds_supported_version(vio_ver_msg_t *ver_msg)
41250a55fbb7Slm66018 {
41260a55fbb7Slm66018 	for (int i = 0; i < vds_num_versions; i++) {
41270a55fbb7Slm66018 		ASSERT(vds_version[i].major > 0);
41280a55fbb7Slm66018 		ASSERT((i == 0) ||
41290a55fbb7Slm66018 		    (vds_version[i].major < vds_version[i-1].major));
41300a55fbb7Slm66018 
41310a55fbb7Slm66018 		/*
41320a55fbb7Slm66018 		 * If the major versions match, adjust the minor version, if
41330a55fbb7Slm66018 		 * necessary, down to the highest value supported by this
41340a55fbb7Slm66018 		 * server and return true so this message will get "ack"ed;
41350a55fbb7Slm66018 		 * the client should also support all minor versions lower
41360a55fbb7Slm66018 		 * than the value it sent
41370a55fbb7Slm66018 		 */
41380a55fbb7Slm66018 		if (ver_msg->ver_major == vds_version[i].major) {
41390a55fbb7Slm66018 			if (ver_msg->ver_minor > vds_version[i].minor) {
41400a55fbb7Slm66018 				PR0("Adjusting minor version from %u to %u",
41410a55fbb7Slm66018 				    ver_msg->ver_minor, vds_version[i].minor);
41420a55fbb7Slm66018 				ver_msg->ver_minor = vds_version[i].minor;
41430a55fbb7Slm66018 			}
41440a55fbb7Slm66018 			return (B_TRUE);
41450a55fbb7Slm66018 		}
41460a55fbb7Slm66018 
41470a55fbb7Slm66018 		/*
41480a55fbb7Slm66018 		 * If the message contains a higher major version number, set
41490a55fbb7Slm66018 		 * the message's major/minor versions to the current values
41500a55fbb7Slm66018 		 * and return false, so this message will get "nack"ed with
41510a55fbb7Slm66018 		 * these values, and the client will potentially try again
41520a55fbb7Slm66018 		 * with the same or a lower version
41530a55fbb7Slm66018 		 */
41540a55fbb7Slm66018 		if (ver_msg->ver_major > vds_version[i].major) {
41550a55fbb7Slm66018 			ver_msg->ver_major = vds_version[i].major;
41560a55fbb7Slm66018 			ver_msg->ver_minor = vds_version[i].minor;
41570a55fbb7Slm66018 			return (B_FALSE);
41580a55fbb7Slm66018 		}
41590a55fbb7Slm66018 
41600a55fbb7Slm66018 		/*
41610a55fbb7Slm66018 		 * Otherwise, the message's major version is less than the
41620a55fbb7Slm66018 		 * current major version, so continue the loop to the next
41630a55fbb7Slm66018 		 * (lower) supported version
41640a55fbb7Slm66018 		 */
41650a55fbb7Slm66018 	}
41660a55fbb7Slm66018 
41670a55fbb7Slm66018 	/*
41680a55fbb7Slm66018 	 * No common version was found; "ground" the version pair in the
41690a55fbb7Slm66018 	 * message to terminate negotiation
41700a55fbb7Slm66018 	 */
41710a55fbb7Slm66018 	ver_msg->ver_major = 0;
41720a55fbb7Slm66018 	ver_msg->ver_minor = 0;
41730a55fbb7Slm66018 	return (B_FALSE);
41740a55fbb7Slm66018 }
41750a55fbb7Slm66018 
41760a55fbb7Slm66018 /*
41770a55fbb7Slm66018  * Process a version message from a client.  vds expects to receive version
41780a55fbb7Slm66018  * messages from clients seeking service, but never issues version messages
41790a55fbb7Slm66018  * itself; therefore, vds can ACK or NACK client version messages, but does
41800a55fbb7Slm66018  * not expect to receive version-message ACKs or NACKs (and will treat such
41810a55fbb7Slm66018  * messages as invalid).
41820a55fbb7Slm66018  */
41831ae08745Sheppo static int
41840a55fbb7Slm66018 vd_process_ver_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
41851ae08745Sheppo {
41861ae08745Sheppo 	vio_ver_msg_t	*ver_msg = (vio_ver_msg_t *)msg;
41871ae08745Sheppo 
41881ae08745Sheppo 
41891ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
41901ae08745Sheppo 
41911ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
41921ae08745Sheppo 	    VIO_VER_INFO)) {
41931ae08745Sheppo 		return (ENOMSG);	/* not a version message */
41941ae08745Sheppo 	}
41951ae08745Sheppo 
41961ae08745Sheppo 	if (msglen != sizeof (*ver_msg)) {
41973af08d82Slm66018 		PR0("Expected %lu-byte version message; "
41981ae08745Sheppo 		    "received %lu bytes", sizeof (*ver_msg), msglen);
41991ae08745Sheppo 		return (EBADMSG);
42001ae08745Sheppo 	}
42011ae08745Sheppo 
42021ae08745Sheppo 	if (ver_msg->dev_class != VDEV_DISK) {
42033af08d82Slm66018 		PR0("Expected device class %u (disk); received %u",
42041ae08745Sheppo 		    VDEV_DISK, ver_msg->dev_class);
42051ae08745Sheppo 		return (EBADMSG);
42061ae08745Sheppo 	}
42071ae08745Sheppo 
42080a55fbb7Slm66018 	/*
42090a55fbb7Slm66018 	 * We're talking to the expected kind of client; set our device class
42100a55fbb7Slm66018 	 * for "ack/nack" back to the client
42110a55fbb7Slm66018 	 */
42121ae08745Sheppo 	ver_msg->dev_class = VDEV_DISK_SERVER;
42130a55fbb7Slm66018 
42140a55fbb7Slm66018 	/*
42150a55fbb7Slm66018 	 * Check whether the (valid) version message specifies a version
42160a55fbb7Slm66018 	 * supported by this server.  If the version is not supported, return
42170a55fbb7Slm66018 	 * EBADMSG so the message will get "nack"ed; vds_supported_version()
42180a55fbb7Slm66018 	 * will have updated the message with a supported version for the
42190a55fbb7Slm66018 	 * client to consider
42200a55fbb7Slm66018 	 */
42210a55fbb7Slm66018 	if (!vds_supported_version(ver_msg))
42220a55fbb7Slm66018 		return (EBADMSG);
42230a55fbb7Slm66018 
42240a55fbb7Slm66018 
42250a55fbb7Slm66018 	/*
42260a55fbb7Slm66018 	 * A version has been agreed upon; use the client's SID for
42270a55fbb7Slm66018 	 * communication on this channel now
42280a55fbb7Slm66018 	 */
42290a55fbb7Slm66018 	ASSERT(!(vd->initialized & VD_SID));
42300a55fbb7Slm66018 	vd->sid = ver_msg->tag.vio_sid;
42310a55fbb7Slm66018 	vd->initialized |= VD_SID;
42320a55fbb7Slm66018 
42330a55fbb7Slm66018 	/*
423417cadca8Slm66018 	 * Store the negotiated major and minor version values in the "vd" data
423517cadca8Slm66018 	 * structure so that we can check if certain operations are supported
423617cadca8Slm66018 	 * by the client.
42370a55fbb7Slm66018 	 */
423817cadca8Slm66018 	vd->version.major = ver_msg->ver_major;
423917cadca8Slm66018 	vd->version.minor = ver_msg->ver_minor;
42400a55fbb7Slm66018 
42410a55fbb7Slm66018 	PR0("Using major version %u, minor version %u",
42420a55fbb7Slm66018 	    ver_msg->ver_major, ver_msg->ver_minor);
42431ae08745Sheppo 	return (0);
42441ae08745Sheppo }
42451ae08745Sheppo 
424617cadca8Slm66018 static void
424717cadca8Slm66018 vd_set_exported_operations(vd_t *vd)
424817cadca8Slm66018 {
424917cadca8Slm66018 	vd->operations = 0;	/* clear field */
425017cadca8Slm66018 
425117cadca8Slm66018 	/*
425217cadca8Slm66018 	 * We need to check from the highest version supported to the
425317cadca8Slm66018 	 * lowest because versions with a higher minor number implicitly
425417cadca8Slm66018 	 * support versions with a lower minor number.
425517cadca8Slm66018 	 */
425617cadca8Slm66018 	if (vio_ver_is_supported(vd->version, 1, 1)) {
425717cadca8Slm66018 		ASSERT(vd->open_flags & FREAD);
4258de3a5331SRamesh Chitrothu 		vd->operations |= VD_OP_MASK_READ | (1 << VD_OP_GET_CAPACITY);
425917cadca8Slm66018 
426017cadca8Slm66018 		if (vd->open_flags & FWRITE)
426117cadca8Slm66018 			vd->operations |= VD_OP_MASK_WRITE;
426217cadca8Slm66018 
42632f5224aeSachartre 		if (vd->scsi)
42642f5224aeSachartre 			vd->operations |= VD_OP_MASK_SCSI;
42652f5224aeSachartre 
4266*1aff8f07SAlexandre Chartre 		if (VD_DSKIMG(vd) && vd_dskimg_is_iso_image(vd)) {
426717cadca8Slm66018 			/*
426817cadca8Slm66018 			 * can't write to ISO images, make sure that write
426917cadca8Slm66018 			 * support is not set in case administrator did not
427017cadca8Slm66018 			 * use "options=ro" when doing an ldm add-vdsdev
427117cadca8Slm66018 			 */
427217cadca8Slm66018 			vd->operations &= ~VD_OP_MASK_WRITE;
427317cadca8Slm66018 		}
427417cadca8Slm66018 	} else if (vio_ver_is_supported(vd->version, 1, 0)) {
427517cadca8Slm66018 		vd->operations = VD_OP_MASK_READ | VD_OP_MASK_WRITE;
427617cadca8Slm66018 	}
427717cadca8Slm66018 
427817cadca8Slm66018 	/* we should have already agreed on a version */
427917cadca8Slm66018 	ASSERT(vd->operations != 0);
428017cadca8Slm66018 }
428117cadca8Slm66018 
42821ae08745Sheppo static int
42831ae08745Sheppo vd_process_attr_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
42841ae08745Sheppo {
42851ae08745Sheppo 	vd_attr_msg_t	*attr_msg = (vd_attr_msg_t *)msg;
42863c96341aSnarayan 	int		status, retry = 0;
42871ae08745Sheppo 
42881ae08745Sheppo 
42891ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
42901ae08745Sheppo 
42911ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
42921ae08745Sheppo 	    VIO_ATTR_INFO)) {
4293d10e4ef2Snarayan 		PR0("Message is not an attribute message");
4294d10e4ef2Snarayan 		return (ENOMSG);
42951ae08745Sheppo 	}
42961ae08745Sheppo 
42971ae08745Sheppo 	if (msglen != sizeof (*attr_msg)) {
42983af08d82Slm66018 		PR0("Expected %lu-byte attribute message; "
42991ae08745Sheppo 		    "received %lu bytes", sizeof (*attr_msg), msglen);
43001ae08745Sheppo 		return (EBADMSG);
43011ae08745Sheppo 	}
43021ae08745Sheppo 
43031ae08745Sheppo 	if (attr_msg->max_xfer_sz == 0) {
43043af08d82Slm66018 		PR0("Received maximum transfer size of 0 from client");
43051ae08745Sheppo 		return (EBADMSG);
43061ae08745Sheppo 	}
43071ae08745Sheppo 
43081ae08745Sheppo 	if ((attr_msg->xfer_mode != VIO_DESC_MODE) &&
4309f0ca1d9aSsb155480 	    (attr_msg->xfer_mode != VIO_DRING_MODE_V1_0)) {
43103af08d82Slm66018 		PR0("Client requested unsupported transfer mode");
43111ae08745Sheppo 		return (EBADMSG);
43121ae08745Sheppo 	}
43131ae08745Sheppo 
43143c96341aSnarayan 	/*
43153c96341aSnarayan 	 * check if the underlying disk is ready, if not try accessing
43163c96341aSnarayan 	 * the device again. Open the vdisk device and extract info
43173c96341aSnarayan 	 * about it, as this is needed to respond to the attr info msg
43183c96341aSnarayan 	 */
43193c96341aSnarayan 	if ((vd->initialized & VD_DISK_READY) == 0) {
43203c96341aSnarayan 		PR0("Retry setting up disk (%s)", vd->device_path);
43213c96341aSnarayan 		do {
43223c96341aSnarayan 			status = vd_setup_vd(vd);
43233c96341aSnarayan 			if (status != EAGAIN || ++retry > vds_dev_retries)
43243c96341aSnarayan 				break;
43253c96341aSnarayan 
43263c96341aSnarayan 			/* incremental delay */
43273c96341aSnarayan 			delay(drv_usectohz(vds_dev_delay));
43283c96341aSnarayan 
43293c96341aSnarayan 			/* if vdisk is no longer enabled - return error */
43303c96341aSnarayan 			if (!vd_enabled(vd))
43313c96341aSnarayan 				return (ENXIO);
43323c96341aSnarayan 
43333c96341aSnarayan 		} while (status == EAGAIN);
43343c96341aSnarayan 
43353c96341aSnarayan 		if (status)
43363c96341aSnarayan 			return (ENXIO);
43373c96341aSnarayan 
43383c96341aSnarayan 		vd->initialized |= VD_DISK_READY;
43393c96341aSnarayan 		ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
43408fce2fd6Sachartre 		PR0("vdisk_type = %s, volume = %s, file = %s, nslices = %u",
43413c96341aSnarayan 		    ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
43428fce2fd6Sachartre 		    (vd->volume ? "yes" : "no"),
43433c96341aSnarayan 		    (vd->file ? "yes" : "no"),
43443c96341aSnarayan 		    vd->nslices);
43453c96341aSnarayan 	}
43463c96341aSnarayan 
43471ae08745Sheppo 	/* Success:  valid message and transfer mode */
43481ae08745Sheppo 	vd->xfer_mode = attr_msg->xfer_mode;
43493af08d82Slm66018 
43501ae08745Sheppo 	if (vd->xfer_mode == VIO_DESC_MODE) {
43513af08d82Slm66018 
43521ae08745Sheppo 		/*
43531ae08745Sheppo 		 * The vd_dring_inband_msg_t contains one cookie; need room
43541ae08745Sheppo 		 * for up to n-1 more cookies, where "n" is the number of full
43551ae08745Sheppo 		 * pages plus possibly one partial page required to cover
43561ae08745Sheppo 		 * "max_xfer_sz".  Add room for one more cookie if
43571ae08745Sheppo 		 * "max_xfer_sz" isn't an integral multiple of the page size.
43581ae08745Sheppo 		 * Must first get the maximum transfer size in bytes.
43591ae08745Sheppo 		 */
43601ae08745Sheppo 		size_t	max_xfer_bytes = attr_msg->vdisk_block_size ?
43611ae08745Sheppo 		    attr_msg->vdisk_block_size*attr_msg->max_xfer_sz :
43621ae08745Sheppo 		    attr_msg->max_xfer_sz;
43631ae08745Sheppo 		size_t	max_inband_msglen =
43641ae08745Sheppo 		    sizeof (vd_dring_inband_msg_t) +
43651ae08745Sheppo 		    ((max_xfer_bytes/PAGESIZE +
43661ae08745Sheppo 		    ((max_xfer_bytes % PAGESIZE) ? 1 : 0))*
43671ae08745Sheppo 		    (sizeof (ldc_mem_cookie_t)));
43681ae08745Sheppo 
43691ae08745Sheppo 		/*
43701ae08745Sheppo 		 * Set the maximum expected message length to
43711ae08745Sheppo 		 * accommodate in-band-descriptor messages with all
43721ae08745Sheppo 		 * their cookies
43731ae08745Sheppo 		 */
43741ae08745Sheppo 		vd->max_msglen = MAX(vd->max_msglen, max_inband_msglen);
4375d10e4ef2Snarayan 
4376d10e4ef2Snarayan 		/*
4377d10e4ef2Snarayan 		 * Initialize the data structure for processing in-band I/O
4378d10e4ef2Snarayan 		 * request descriptors
4379d10e4ef2Snarayan 		 */
4380d10e4ef2Snarayan 		vd->inband_task.vd	= vd;
43813af08d82Slm66018 		vd->inband_task.msg	= kmem_alloc(vd->max_msglen, KM_SLEEP);
4382d10e4ef2Snarayan 		vd->inband_task.index	= 0;
4383d10e4ef2Snarayan 		vd->inband_task.type	= VD_FINAL_RANGE_TASK;	/* range == 1 */
43841ae08745Sheppo 	}
43851ae08745Sheppo 
4386e1ebb9ecSlm66018 	/* Return the device's block size and max transfer size to the client */
43872f5224aeSachartre 	attr_msg->vdisk_block_size	= vd->vdisk_block_size;
4388e1ebb9ecSlm66018 	attr_msg->max_xfer_sz		= vd->max_xfer_sz;
4389e1ebb9ecSlm66018 
43901ae08745Sheppo 	attr_msg->vdisk_size = vd->vdisk_size;
4391bae9e67eSachartre 	attr_msg->vdisk_type = (vd_slice_single_slice)? vd->vdisk_type :
4392bae9e67eSachartre 	    VD_DISK_TYPE_DISK;
439317cadca8Slm66018 	attr_msg->vdisk_media = vd->vdisk_media;
439417cadca8Slm66018 
439517cadca8Slm66018 	/* Discover and save the list of supported VD_OP_XXX operations */
439617cadca8Slm66018 	vd_set_exported_operations(vd);
439717cadca8Slm66018 	attr_msg->operations = vd->operations;
439817cadca8Slm66018 
43991ae08745Sheppo 	PR0("%s", VD_CLIENT(vd));
44003af08d82Slm66018 
44013af08d82Slm66018 	ASSERT(vd->dring_task == NULL);
44023af08d82Slm66018 
44031ae08745Sheppo 	return (0);
44041ae08745Sheppo }
44051ae08745Sheppo 
44061ae08745Sheppo static int
44071ae08745Sheppo vd_process_dring_reg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
44081ae08745Sheppo {
44091ae08745Sheppo 	int			status;
44101ae08745Sheppo 	size_t			expected;
44111ae08745Sheppo 	ldc_mem_info_t		dring_minfo;
4412bbfa0259Sha137994 	uint8_t			mtype;
44131ae08745Sheppo 	vio_dring_reg_msg_t	*reg_msg = (vio_dring_reg_msg_t *)msg;
44141ae08745Sheppo 
44151ae08745Sheppo 
44161ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
44171ae08745Sheppo 
44181ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
44191ae08745Sheppo 	    VIO_DRING_REG)) {
4420d10e4ef2Snarayan 		PR0("Message is not a register-dring message");
4421d10e4ef2Snarayan 		return (ENOMSG);
44221ae08745Sheppo 	}
44231ae08745Sheppo 
44241ae08745Sheppo 	if (msglen < sizeof (*reg_msg)) {
44253af08d82Slm66018 		PR0("Expected at least %lu-byte register-dring message; "
44261ae08745Sheppo 		    "received %lu bytes", sizeof (*reg_msg), msglen);
44271ae08745Sheppo 		return (EBADMSG);
44281ae08745Sheppo 	}
44291ae08745Sheppo 
44301ae08745Sheppo 	expected = sizeof (*reg_msg) +
44311ae08745Sheppo 	    (reg_msg->ncookies - 1)*(sizeof (reg_msg->cookie[0]));
44321ae08745Sheppo 	if (msglen != expected) {
44333af08d82Slm66018 		PR0("Expected %lu-byte register-dring message; "
44341ae08745Sheppo 		    "received %lu bytes", expected, msglen);
44351ae08745Sheppo 		return (EBADMSG);
44361ae08745Sheppo 	}
44371ae08745Sheppo 
44381ae08745Sheppo 	if (vd->initialized & VD_DRING) {
44393af08d82Slm66018 		PR0("A dring was previously registered; only support one");
44401ae08745Sheppo 		return (EBADMSG);
44411ae08745Sheppo 	}
44421ae08745Sheppo 
4443d10e4ef2Snarayan 	if (reg_msg->num_descriptors > INT32_MAX) {
44443af08d82Slm66018 		PR0("reg_msg->num_descriptors = %u; must be <= %u (%s)",
4445d10e4ef2Snarayan 		    reg_msg->ncookies, INT32_MAX, STRINGIZE(INT32_MAX));
4446d10e4ef2Snarayan 		return (EBADMSG);
4447d10e4ef2Snarayan 	}
4448d10e4ef2Snarayan 
44491ae08745Sheppo 	if (reg_msg->ncookies != 1) {
44501ae08745Sheppo 		/*
44511ae08745Sheppo 		 * In addition to fixing the assertion in the success case
44521ae08745Sheppo 		 * below, supporting drings which require more than one
44531ae08745Sheppo 		 * "cookie" requires increasing the value of vd->max_msglen
44541ae08745Sheppo 		 * somewhere in the code path prior to receiving the message
44551ae08745Sheppo 		 * which results in calling this function.  Note that without
44561ae08745Sheppo 		 * making this change, the larger message size required to
44571ae08745Sheppo 		 * accommodate multiple cookies cannot be successfully
44581ae08745Sheppo 		 * received, so this function will not even get called.
44591ae08745Sheppo 		 * Gracefully accommodating more dring cookies might
44601ae08745Sheppo 		 * reasonably demand exchanging an additional attribute or
44611ae08745Sheppo 		 * making a minor protocol adjustment
44621ae08745Sheppo 		 */
44633af08d82Slm66018 		PR0("reg_msg->ncookies = %u != 1", reg_msg->ncookies);
44641ae08745Sheppo 		return (EBADMSG);
44651ae08745Sheppo 	}
44661ae08745Sheppo 
4467bbfa0259Sha137994 	if (vd_direct_mapped_drings)
4468bbfa0259Sha137994 		mtype = LDC_DIRECT_MAP;
4469bbfa0259Sha137994 	else
4470bbfa0259Sha137994 		mtype = LDC_SHADOW_MAP;
4471bbfa0259Sha137994 
44721ae08745Sheppo 	status = ldc_mem_dring_map(vd->ldc_handle, reg_msg->cookie,
44731ae08745Sheppo 	    reg_msg->ncookies, reg_msg->num_descriptors,
4474bbfa0259Sha137994 	    reg_msg->descriptor_size, mtype, &vd->dring_handle);
44751ae08745Sheppo 	if (status != 0) {
44763af08d82Slm66018 		PR0("ldc_mem_dring_map() returned errno %d", status);
44771ae08745Sheppo 		return (status);
44781ae08745Sheppo 	}
44791ae08745Sheppo 
44801ae08745Sheppo 	/*
44811ae08745Sheppo 	 * To remove the need for this assertion, must call
44821ae08745Sheppo 	 * ldc_mem_dring_nextcookie() successfully ncookies-1 times after a
44831ae08745Sheppo 	 * successful call to ldc_mem_dring_map()
44841ae08745Sheppo 	 */
44851ae08745Sheppo 	ASSERT(reg_msg->ncookies == 1);
44861ae08745Sheppo 
44871ae08745Sheppo 	if ((status =
44881ae08745Sheppo 	    ldc_mem_dring_info(vd->dring_handle, &dring_minfo)) != 0) {
44893af08d82Slm66018 		PR0("ldc_mem_dring_info() returned errno %d", status);
44901ae08745Sheppo 		if ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0)
44913af08d82Slm66018 			PR0("ldc_mem_dring_unmap() returned errno %d", status);
44921ae08745Sheppo 		return (status);
44931ae08745Sheppo 	}
44941ae08745Sheppo 
44951ae08745Sheppo 	if (dring_minfo.vaddr == NULL) {
44963af08d82Slm66018 		PR0("Descriptor ring virtual address is NULL");
44970a55fbb7Slm66018 		return (ENXIO);
44981ae08745Sheppo 	}
44991ae08745Sheppo 
45001ae08745Sheppo 
4501d10e4ef2Snarayan 	/* Initialize for valid message and mapped dring */
45021ae08745Sheppo 	vd->initialized |= VD_DRING;
45031ae08745Sheppo 	vd->dring_ident = 1;	/* "There Can Be Only One" */
45041ae08745Sheppo 	vd->dring = dring_minfo.vaddr;
45051ae08745Sheppo 	vd->descriptor_size = reg_msg->descriptor_size;
45061ae08745Sheppo 	vd->dring_len = reg_msg->num_descriptors;
4507bbfa0259Sha137994 	vd->dring_mtype = dring_minfo.mtype;
45081ae08745Sheppo 	reg_msg->dring_ident = vd->dring_ident;
45095b7cb889Sha137994 	PR1("descriptor size = %u, dring length = %u",
45105b7cb889Sha137994 	    vd->descriptor_size, vd->dring_len);
4511d10e4ef2Snarayan 
4512d10e4ef2Snarayan 	/*
4513d10e4ef2Snarayan 	 * Allocate and initialize a "shadow" array of data structures for
4514d10e4ef2Snarayan 	 * tasks to process I/O requests in dring elements
4515d10e4ef2Snarayan 	 */
4516d10e4ef2Snarayan 	vd->dring_task =
4517d10e4ef2Snarayan 	    kmem_zalloc((sizeof (*vd->dring_task)) * vd->dring_len, KM_SLEEP);
4518d10e4ef2Snarayan 	for (int i = 0; i < vd->dring_len; i++) {
4519d10e4ef2Snarayan 		vd->dring_task[i].vd		= vd;
4520d10e4ef2Snarayan 		vd->dring_task[i].index		= i;
45214bac2208Snarayan 
45224bac2208Snarayan 		status = ldc_mem_alloc_handle(vd->ldc_handle,
45234bac2208Snarayan 		    &(vd->dring_task[i].mhdl));
45244bac2208Snarayan 		if (status) {
45253af08d82Slm66018 			PR0("ldc_mem_alloc_handle() returned err %d ", status);
45264bac2208Snarayan 			return (ENXIO);
45274bac2208Snarayan 		}
45283af08d82Slm66018 
45295b7cb889Sha137994 		/*
45305b7cb889Sha137994 		 * The descriptor payload varies in length. Calculate its
45315b7cb889Sha137994 		 * size by subtracting the header size from the total
45325b7cb889Sha137994 		 * descriptor size.
45335b7cb889Sha137994 		 */
45345b7cb889Sha137994 		vd->dring_task[i].request = kmem_zalloc((vd->descriptor_size -
45355b7cb889Sha137994 		    sizeof (vio_dring_entry_hdr_t)), KM_SLEEP);
45363af08d82Slm66018 		vd->dring_task[i].msg = kmem_alloc(vd->max_msglen, KM_SLEEP);
4537d10e4ef2Snarayan 	}
4538d10e4ef2Snarayan 
45391ae08745Sheppo 	return (0);
45401ae08745Sheppo }
45411ae08745Sheppo 
45421ae08745Sheppo static int
45431ae08745Sheppo vd_process_dring_unreg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
45441ae08745Sheppo {
45451ae08745Sheppo 	vio_dring_unreg_msg_t	*unreg_msg = (vio_dring_unreg_msg_t *)msg;
45461ae08745Sheppo 
45471ae08745Sheppo 
45481ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
45491ae08745Sheppo 
45501ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
45511ae08745Sheppo 	    VIO_DRING_UNREG)) {
4552d10e4ef2Snarayan 		PR0("Message is not an unregister-dring message");
4553d10e4ef2Snarayan 		return (ENOMSG);
45541ae08745Sheppo 	}
45551ae08745Sheppo 
45561ae08745Sheppo 	if (msglen != sizeof (*unreg_msg)) {
45573af08d82Slm66018 		PR0("Expected %lu-byte unregister-dring message; "
45581ae08745Sheppo 		    "received %lu bytes", sizeof (*unreg_msg), msglen);
45591ae08745Sheppo 		return (EBADMSG);
45601ae08745Sheppo 	}
45611ae08745Sheppo 
45621ae08745Sheppo 	if (unreg_msg->dring_ident != vd->dring_ident) {
45633af08d82Slm66018 		PR0("Expected dring ident %lu; received %lu",
45641ae08745Sheppo 		    vd->dring_ident, unreg_msg->dring_ident);
45651ae08745Sheppo 		return (EBADMSG);
45661ae08745Sheppo 	}
45671ae08745Sheppo 
45681ae08745Sheppo 	return (0);
45691ae08745Sheppo }
45701ae08745Sheppo 
45711ae08745Sheppo static int
45721ae08745Sheppo process_rdx_msg(vio_msg_t *msg, size_t msglen)
45731ae08745Sheppo {
45741ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
45751ae08745Sheppo 
4576d10e4ef2Snarayan 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX)) {
4577d10e4ef2Snarayan 		PR0("Message is not an RDX message");
4578d10e4ef2Snarayan 		return (ENOMSG);
4579d10e4ef2Snarayan 	}
45801ae08745Sheppo 
45811ae08745Sheppo 	if (msglen != sizeof (vio_rdx_msg_t)) {
45823af08d82Slm66018 		PR0("Expected %lu-byte RDX message; received %lu bytes",
45831ae08745Sheppo 		    sizeof (vio_rdx_msg_t), msglen);
45841ae08745Sheppo 		return (EBADMSG);
45851ae08745Sheppo 	}
45861ae08745Sheppo 
4587d10e4ef2Snarayan 	PR0("Valid RDX message");
45881ae08745Sheppo 	return (0);
45891ae08745Sheppo }
45901ae08745Sheppo 
45911ae08745Sheppo static int
45921ae08745Sheppo vd_check_seq_num(vd_t *vd, uint64_t seq_num)
45931ae08745Sheppo {
45941ae08745Sheppo 	if ((vd->initialized & VD_SEQ_NUM) && (seq_num != vd->seq_num + 1)) {
45953af08d82Slm66018 		PR0("Received seq_num %lu; expected %lu",
45961ae08745Sheppo 		    seq_num, (vd->seq_num + 1));
45973af08d82Slm66018 		PR0("initiating soft reset");
4598d10e4ef2Snarayan 		vd_need_reset(vd, B_FALSE);
45991ae08745Sheppo 		return (1);
46001ae08745Sheppo 	}
46011ae08745Sheppo 
46021ae08745Sheppo 	vd->seq_num = seq_num;
46031ae08745Sheppo 	vd->initialized |= VD_SEQ_NUM;	/* superfluous after first time... */
46041ae08745Sheppo 	return (0);
46051ae08745Sheppo }
46061ae08745Sheppo 
46071ae08745Sheppo /*
46081ae08745Sheppo  * Return the expected size of an inband-descriptor message with all the
46091ae08745Sheppo  * cookies it claims to include
46101ae08745Sheppo  */
46111ae08745Sheppo static size_t
46121ae08745Sheppo expected_inband_size(vd_dring_inband_msg_t *msg)
46131ae08745Sheppo {
46141ae08745Sheppo 	return ((sizeof (*msg)) +
46151ae08745Sheppo 	    (msg->payload.ncookies - 1)*(sizeof (msg->payload.cookie[0])));
46161ae08745Sheppo }
46171ae08745Sheppo 
46181ae08745Sheppo /*
46191ae08745Sheppo  * Process an in-band descriptor message:  used with clients like OBP, with
46201ae08745Sheppo  * which vds exchanges descriptors within VIO message payloads, rather than
46211ae08745Sheppo  * operating on them within a descriptor ring
46221ae08745Sheppo  */
46231ae08745Sheppo static int
46243af08d82Slm66018 vd_process_desc_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
46251ae08745Sheppo {
46261ae08745Sheppo 	size_t			expected;
46271ae08745Sheppo 	vd_dring_inband_msg_t	*desc_msg = (vd_dring_inband_msg_t *)msg;
46281ae08745Sheppo 
46291ae08745Sheppo 
46301ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
46311ae08745Sheppo 
46321ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
4633d10e4ef2Snarayan 	    VIO_DESC_DATA)) {
4634d10e4ef2Snarayan 		PR1("Message is not an in-band-descriptor message");
4635d10e4ef2Snarayan 		return (ENOMSG);
4636d10e4ef2Snarayan 	}
46371ae08745Sheppo 
46381ae08745Sheppo 	if (msglen < sizeof (*desc_msg)) {
46393af08d82Slm66018 		PR0("Expected at least %lu-byte descriptor message; "
46401ae08745Sheppo 		    "received %lu bytes", sizeof (*desc_msg), msglen);
46411ae08745Sheppo 		return (EBADMSG);
46421ae08745Sheppo 	}
46431ae08745Sheppo 
46441ae08745Sheppo 	if (msglen != (expected = expected_inband_size(desc_msg))) {
46453af08d82Slm66018 		PR0("Expected %lu-byte descriptor message; "
46461ae08745Sheppo 		    "received %lu bytes", expected, msglen);
46471ae08745Sheppo 		return (EBADMSG);
46481ae08745Sheppo 	}
46491ae08745Sheppo 
4650d10e4ef2Snarayan 	if (vd_check_seq_num(vd, desc_msg->hdr.seq_num) != 0)
46511ae08745Sheppo 		return (EBADMSG);
46521ae08745Sheppo 
4653d10e4ef2Snarayan 	/*
4654d10e4ef2Snarayan 	 * Valid message:  Set up the in-band descriptor task and process the
4655d10e4ef2Snarayan 	 * request.  Arrange to acknowledge the client's message, unless an
4656d10e4ef2Snarayan 	 * error processing the descriptor task results in setting
4657d10e4ef2Snarayan 	 * VIO_SUBTYPE_NACK
4658d10e4ef2Snarayan 	 */
4659d10e4ef2Snarayan 	PR1("Valid in-band-descriptor message");
4660d10e4ef2Snarayan 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
46613af08d82Slm66018 
46623af08d82Slm66018 	ASSERT(vd->inband_task.msg != NULL);
46633af08d82Slm66018 
46643af08d82Slm66018 	bcopy(msg, vd->inband_task.msg, msglen);
4665d10e4ef2Snarayan 	vd->inband_task.msglen	= msglen;
46663af08d82Slm66018 
46673af08d82Slm66018 	/*
46683af08d82Slm66018 	 * The task request is now the payload of the message
46693af08d82Slm66018 	 * that was just copied into the body of the task.
46703af08d82Slm66018 	 */
46713af08d82Slm66018 	desc_msg = (vd_dring_inband_msg_t *)vd->inband_task.msg;
4672d10e4ef2Snarayan 	vd->inband_task.request	= &desc_msg->payload;
46733af08d82Slm66018 
4674d10e4ef2Snarayan 	return (vd_process_task(&vd->inband_task));
46751ae08745Sheppo }
46761ae08745Sheppo 
46771ae08745Sheppo static int
4678d10e4ef2Snarayan vd_process_element(vd_t *vd, vd_task_type_t type, uint32_t idx,
46793af08d82Slm66018     vio_msg_t *msg, size_t msglen)
46801ae08745Sheppo {
46811ae08745Sheppo 	int			status;
4682d10e4ef2Snarayan 	boolean_t		ready;
4683bbfa0259Sha137994 	on_trap_data_t		otd;
4684d10e4ef2Snarayan 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
46851ae08745Sheppo 
4686d10e4ef2Snarayan 	/* Accept the updated dring element */
4687bbfa0259Sha137994 	if ((status = VIO_DRING_ACQUIRE(&otd, vd->dring_mtype,
4688bbfa0259Sha137994 	    vd->dring_handle, idx, idx)) != 0) {
46891ae08745Sheppo 		return (status);
46901ae08745Sheppo 	}
4691d10e4ef2Snarayan 	ready = (elem->hdr.dstate == VIO_DESC_READY);
4692d10e4ef2Snarayan 	if (ready) {
4693d10e4ef2Snarayan 		elem->hdr.dstate = VIO_DESC_ACCEPTED;
46945b7cb889Sha137994 		bcopy(&elem->payload, vd->dring_task[idx].request,
46955b7cb889Sha137994 		    (vd->descriptor_size - sizeof (vio_dring_entry_hdr_t)));
4696d10e4ef2Snarayan 	} else {
46973af08d82Slm66018 		PR0("descriptor %u not ready", idx);
4698d10e4ef2Snarayan 		VD_DUMP_DRING_ELEM(elem);
4699d10e4ef2Snarayan 	}
4700bbfa0259Sha137994 	if ((status = VIO_DRING_RELEASE(vd->dring_mtype,
4701bbfa0259Sha137994 	    vd->dring_handle, idx, idx)) != 0) {
4702bbfa0259Sha137994 		PR0("VIO_DRING_RELEASE() returned errno %d", status);
47031ae08745Sheppo 		return (status);
47041ae08745Sheppo 	}
4705d10e4ef2Snarayan 	if (!ready)
4706d10e4ef2Snarayan 		return (EBUSY);
47071ae08745Sheppo 
47081ae08745Sheppo 
4709d10e4ef2Snarayan 	/* Initialize a task and process the accepted element */
4710d10e4ef2Snarayan 	PR1("Processing dring element %u", idx);
4711d10e4ef2Snarayan 	vd->dring_task[idx].type	= type;
47123af08d82Slm66018 
47133af08d82Slm66018 	/* duplicate msg buf for cookies etc. */
47143af08d82Slm66018 	bcopy(msg, vd->dring_task[idx].msg, msglen);
47153af08d82Slm66018 
4716d10e4ef2Snarayan 	vd->dring_task[idx].msglen	= msglen;
4717205eeb1aSlm66018 	return (vd_process_task(&vd->dring_task[idx]));
47181ae08745Sheppo }
47191ae08745Sheppo 
47201ae08745Sheppo static int
4721d10e4ef2Snarayan vd_process_element_range(vd_t *vd, int start, int end,
47223af08d82Slm66018     vio_msg_t *msg, size_t msglen)
4723d10e4ef2Snarayan {
4724d10e4ef2Snarayan 	int		i, n, nelem, status = 0;
4725d10e4ef2Snarayan 	boolean_t	inprogress = B_FALSE;
4726d10e4ef2Snarayan 	vd_task_type_t	type;
4727d10e4ef2Snarayan 
4728d10e4ef2Snarayan 
4729d10e4ef2Snarayan 	ASSERT(start >= 0);
4730d10e4ef2Snarayan 	ASSERT(end >= 0);
4731d10e4ef2Snarayan 
4732d10e4ef2Snarayan 	/*
4733d10e4ef2Snarayan 	 * Arrange to acknowledge the client's message, unless an error
4734d10e4ef2Snarayan 	 * processing one of the dring elements results in setting
4735d10e4ef2Snarayan 	 * VIO_SUBTYPE_NACK
4736d10e4ef2Snarayan 	 */
4737d10e4ef2Snarayan 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
4738d10e4ef2Snarayan 
4739d10e4ef2Snarayan 	/*
4740d10e4ef2Snarayan 	 * Process the dring elements in the range
4741d10e4ef2Snarayan 	 */
4742d10e4ef2Snarayan 	nelem = ((end < start) ? end + vd->dring_len : end) - start + 1;
4743d10e4ef2Snarayan 	for (i = start, n = nelem; n > 0; i = (i + 1) % vd->dring_len, n--) {
4744d10e4ef2Snarayan 		((vio_dring_msg_t *)msg)->end_idx = i;
4745d10e4ef2Snarayan 		type = (n == 1) ? VD_FINAL_RANGE_TASK : VD_NONFINAL_RANGE_TASK;
47463af08d82Slm66018 		status = vd_process_element(vd, type, i, msg, msglen);
4747d10e4ef2Snarayan 		if (status == EINPROGRESS)
4748d10e4ef2Snarayan 			inprogress = B_TRUE;
4749d10e4ef2Snarayan 		else if (status != 0)
4750d10e4ef2Snarayan 			break;
4751d10e4ef2Snarayan 	}
4752d10e4ef2Snarayan 
4753d10e4ef2Snarayan 	/*
4754d10e4ef2Snarayan 	 * If some, but not all, operations of a multi-element range are in
4755d10e4ef2Snarayan 	 * progress, wait for other operations to complete before returning
4756d10e4ef2Snarayan 	 * (which will result in "ack" or "nack" of the message).  Note that
4757d10e4ef2Snarayan 	 * all outstanding operations will need to complete, not just the ones
4758d10e4ef2Snarayan 	 * corresponding to the current range of dring elements; howevever, as
4759d10e4ef2Snarayan 	 * this situation is an error case, performance is less critical.
4760d10e4ef2Snarayan 	 */
4761d10e4ef2Snarayan 	if ((nelem > 1) && (status != EINPROGRESS) && inprogress)
4762d10e4ef2Snarayan 		ddi_taskq_wait(vd->completionq);
4763d10e4ef2Snarayan 
4764d10e4ef2Snarayan 	return (status);
4765d10e4ef2Snarayan }
4766d10e4ef2Snarayan 
4767d10e4ef2Snarayan static int
47683af08d82Slm66018 vd_process_dring_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
47691ae08745Sheppo {
47701ae08745Sheppo 	vio_dring_msg_t	*dring_msg = (vio_dring_msg_t *)msg;
47711ae08745Sheppo 
47721ae08745Sheppo 
47731ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
47741ae08745Sheppo 
47751ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
47761ae08745Sheppo 	    VIO_DRING_DATA)) {
4777d10e4ef2Snarayan 		PR1("Message is not a dring-data message");
4778d10e4ef2Snarayan 		return (ENOMSG);
47791ae08745Sheppo 	}
47801ae08745Sheppo 
47811ae08745Sheppo 	if (msglen != sizeof (*dring_msg)) {
47823af08d82Slm66018 		PR0("Expected %lu-byte dring message; received %lu bytes",
47831ae08745Sheppo 		    sizeof (*dring_msg), msglen);
47841ae08745Sheppo 		return (EBADMSG);
47851ae08745Sheppo 	}
47861ae08745Sheppo 
4787d10e4ef2Snarayan 	if (vd_check_seq_num(vd, dring_msg->seq_num) != 0)
47881ae08745Sheppo 		return (EBADMSG);
47891ae08745Sheppo 
47901ae08745Sheppo 	if (dring_msg->dring_ident != vd->dring_ident) {
47913af08d82Slm66018 		PR0("Expected dring ident %lu; received ident %lu",
47921ae08745Sheppo 		    vd->dring_ident, dring_msg->dring_ident);
47931ae08745Sheppo 		return (EBADMSG);
47941ae08745Sheppo 	}
47951ae08745Sheppo 
4796d10e4ef2Snarayan 	if (dring_msg->start_idx >= vd->dring_len) {
47973af08d82Slm66018 		PR0("\"start_idx\" = %u; must be less than %u",
4798d10e4ef2Snarayan 		    dring_msg->start_idx, vd->dring_len);
4799d10e4ef2Snarayan 		return (EBADMSG);
4800d10e4ef2Snarayan 	}
48011ae08745Sheppo 
4802d10e4ef2Snarayan 	if ((dring_msg->end_idx < 0) ||
4803d10e4ef2Snarayan 	    (dring_msg->end_idx >= vd->dring_len)) {
48043af08d82Slm66018 		PR0("\"end_idx\" = %u; must be >= 0 and less than %u",
4805d10e4ef2Snarayan 		    dring_msg->end_idx, vd->dring_len);
4806d10e4ef2Snarayan 		return (EBADMSG);
4807d10e4ef2Snarayan 	}
4808d10e4ef2Snarayan 
4809d10e4ef2Snarayan 	/* Valid message; process range of updated dring elements */
4810d10e4ef2Snarayan 	PR1("Processing descriptor range, start = %u, end = %u",
4811d10e4ef2Snarayan 	    dring_msg->start_idx, dring_msg->end_idx);
4812d10e4ef2Snarayan 	return (vd_process_element_range(vd, dring_msg->start_idx,
48133af08d82Slm66018 	    dring_msg->end_idx, msg, msglen));
48141ae08745Sheppo }
48151ae08745Sheppo 
48161ae08745Sheppo static int
48171ae08745Sheppo recv_msg(ldc_handle_t ldc_handle, void *msg, size_t *nbytes)
48181ae08745Sheppo {
48191ae08745Sheppo 	int	retry, status;
48201ae08745Sheppo 	size_t	size = *nbytes;
48211ae08745Sheppo 
48221ae08745Sheppo 
48231ae08745Sheppo 	for (retry = 0, status = ETIMEDOUT;
48241ae08745Sheppo 	    retry < vds_ldc_retries && status == ETIMEDOUT;
48251ae08745Sheppo 	    retry++) {
48261ae08745Sheppo 		PR1("ldc_read() attempt %d", (retry + 1));
48271ae08745Sheppo 		*nbytes = size;
48281ae08745Sheppo 		status = ldc_read(ldc_handle, msg, nbytes);
48291ae08745Sheppo 	}
48301ae08745Sheppo 
48313af08d82Slm66018 	if (status) {
48323af08d82Slm66018 		PR0("ldc_read() returned errno %d", status);
48333af08d82Slm66018 		if (status != ECONNRESET)
48343af08d82Slm66018 			return (ENOMSG);
48351ae08745Sheppo 		return (status);
48361ae08745Sheppo 	} else if (*nbytes == 0) {
48371ae08745Sheppo 		PR1("ldc_read() returned 0 and no message read");
48381ae08745Sheppo 		return (ENOMSG);
48391ae08745Sheppo 	}
48401ae08745Sheppo 
48411ae08745Sheppo 	PR1("RCVD %lu-byte message", *nbytes);
48421ae08745Sheppo 	return (0);
48431ae08745Sheppo }
48441ae08745Sheppo 
48451ae08745Sheppo static int
48463af08d82Slm66018 vd_do_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
48471ae08745Sheppo {
48481ae08745Sheppo 	int		status;
48491ae08745Sheppo 
48501ae08745Sheppo 
48511ae08745Sheppo 	PR1("Processing (%x/%x/%x) message", msg->tag.vio_msgtype,
48521ae08745Sheppo 	    msg->tag.vio_subtype, msg->tag.vio_subtype_env);
48533af08d82Slm66018 #ifdef	DEBUG
48543af08d82Slm66018 	vd_decode_tag(msg);
48553af08d82Slm66018 #endif
48561ae08745Sheppo 
48571ae08745Sheppo 	/*
48581ae08745Sheppo 	 * Validate session ID up front, since it applies to all messages
48591ae08745Sheppo 	 * once set
48601ae08745Sheppo 	 */
48611ae08745Sheppo 	if ((msg->tag.vio_sid != vd->sid) && (vd->initialized & VD_SID)) {
48623af08d82Slm66018 		PR0("Expected SID %u, received %u", vd->sid,
48631ae08745Sheppo 		    msg->tag.vio_sid);
48641ae08745Sheppo 		return (EBADMSG);
48651ae08745Sheppo 	}
48661ae08745Sheppo 
48673af08d82Slm66018 	PR1("\tWhile in state %d (%s)", vd->state, vd_decode_state(vd->state));
48681ae08745Sheppo 
48691ae08745Sheppo 	/*
48701ae08745Sheppo 	 * Process the received message based on connection state
48711ae08745Sheppo 	 */
48721ae08745Sheppo 	switch (vd->state) {
48731ae08745Sheppo 	case VD_STATE_INIT:	/* expect version message */
48740a55fbb7Slm66018 		if ((status = vd_process_ver_msg(vd, msg, msglen)) != 0)
48751ae08745Sheppo 			return (status);
48761ae08745Sheppo 
48771ae08745Sheppo 		/* Version negotiated, move to that state */
48781ae08745Sheppo 		vd->state = VD_STATE_VER;
48791ae08745Sheppo 		return (0);
48801ae08745Sheppo 
48811ae08745Sheppo 	case VD_STATE_VER:	/* expect attribute message */
48821ae08745Sheppo 		if ((status = vd_process_attr_msg(vd, msg, msglen)) != 0)
48831ae08745Sheppo 			return (status);
48841ae08745Sheppo 
48851ae08745Sheppo 		/* Attributes exchanged, move to that state */
48861ae08745Sheppo 		vd->state = VD_STATE_ATTR;
48871ae08745Sheppo 		return (0);
48881ae08745Sheppo 
48891ae08745Sheppo 	case VD_STATE_ATTR:
48901ae08745Sheppo 		switch (vd->xfer_mode) {
48911ae08745Sheppo 		case VIO_DESC_MODE:	/* expect RDX message */
48921ae08745Sheppo 			if ((status = process_rdx_msg(msg, msglen)) != 0)
48931ae08745Sheppo 				return (status);
48941ae08745Sheppo 
48951ae08745Sheppo 			/* Ready to receive in-band descriptors */
48961ae08745Sheppo 			vd->state = VD_STATE_DATA;
48971ae08745Sheppo 			return (0);
48981ae08745Sheppo 
4899f0ca1d9aSsb155480 		case VIO_DRING_MODE_V1_0:  /* expect register-dring message */
49001ae08745Sheppo 			if ((status =
49011ae08745Sheppo 			    vd_process_dring_reg_msg(vd, msg, msglen)) != 0)
49021ae08745Sheppo 				return (status);
49031ae08745Sheppo 
49041ae08745Sheppo 			/* One dring negotiated, move to that state */
49051ae08745Sheppo 			vd->state = VD_STATE_DRING;
49061ae08745Sheppo 			return (0);
49071ae08745Sheppo 
49081ae08745Sheppo 		default:
49091ae08745Sheppo 			ASSERT("Unsupported transfer mode");
49103af08d82Slm66018 			PR0("Unsupported transfer mode");
49111ae08745Sheppo 			return (ENOTSUP);
49121ae08745Sheppo 		}
49131ae08745Sheppo 
49141ae08745Sheppo 	case VD_STATE_DRING:	/* expect RDX, register-dring, or unreg-dring */
49151ae08745Sheppo 		if ((status = process_rdx_msg(msg, msglen)) == 0) {
49161ae08745Sheppo 			/* Ready to receive data */
49171ae08745Sheppo 			vd->state = VD_STATE_DATA;
49181ae08745Sheppo 			return (0);
49191ae08745Sheppo 		} else if (status != ENOMSG) {
49201ae08745Sheppo 			return (status);
49211ae08745Sheppo 		}
49221ae08745Sheppo 
49231ae08745Sheppo 
49241ae08745Sheppo 		/*
49251ae08745Sheppo 		 * If another register-dring message is received, stay in
49261ae08745Sheppo 		 * dring state in case the client sends RDX; although the
49271ae08745Sheppo 		 * protocol allows multiple drings, this server does not
49281ae08745Sheppo 		 * support using more than one
49291ae08745Sheppo 		 */
49301ae08745Sheppo 		if ((status =
49311ae08745Sheppo 		    vd_process_dring_reg_msg(vd, msg, msglen)) != ENOMSG)
49321ae08745Sheppo 			return (status);
49331ae08745Sheppo 
49341ae08745Sheppo 		/*
49351ae08745Sheppo 		 * Acknowledge an unregister-dring message, but reset the
49361ae08745Sheppo 		 * connection anyway:  Although the protocol allows
49371ae08745Sheppo 		 * unregistering drings, this server cannot serve a vdisk
49381ae08745Sheppo 		 * without its only dring
49391ae08745Sheppo 		 */
49401ae08745Sheppo 		status = vd_process_dring_unreg_msg(vd, msg, msglen);
49411ae08745Sheppo 		return ((status == 0) ? ENOTSUP : status);
49421ae08745Sheppo 
49431ae08745Sheppo 	case VD_STATE_DATA:
49441ae08745Sheppo 		switch (vd->xfer_mode) {
49451ae08745Sheppo 		case VIO_DESC_MODE:	/* expect in-band-descriptor message */
49463af08d82Slm66018 			return (vd_process_desc_msg(vd, msg, msglen));
49471ae08745Sheppo 
4948f0ca1d9aSsb155480 		case VIO_DRING_MODE_V1_0: /* expect dring-data or unreg-dring */
49491ae08745Sheppo 			/*
49501ae08745Sheppo 			 * Typically expect dring-data messages, so handle
49511ae08745Sheppo 			 * them first
49521ae08745Sheppo 			 */
49531ae08745Sheppo 			if ((status = vd_process_dring_msg(vd, msg,
49543af08d82Slm66018 			    msglen)) != ENOMSG)
49551ae08745Sheppo 				return (status);
49561ae08745Sheppo 
49571ae08745Sheppo 			/*
49581ae08745Sheppo 			 * Acknowledge an unregister-dring message, but reset
49591ae08745Sheppo 			 * the connection anyway:  Although the protocol
49601ae08745Sheppo 			 * allows unregistering drings, this server cannot
49611ae08745Sheppo 			 * serve a vdisk without its only dring
49621ae08745Sheppo 			 */
49631ae08745Sheppo 			status = vd_process_dring_unreg_msg(vd, msg, msglen);
49641ae08745Sheppo 			return ((status == 0) ? ENOTSUP : status);
49651ae08745Sheppo 
49661ae08745Sheppo 		default:
49671ae08745Sheppo 			ASSERT("Unsupported transfer mode");
49683af08d82Slm66018 			PR0("Unsupported transfer mode");
49691ae08745Sheppo 			return (ENOTSUP);
49701ae08745Sheppo 		}
49711ae08745Sheppo 
49721ae08745Sheppo 	default:
49731ae08745Sheppo 		ASSERT("Invalid client connection state");
49743af08d82Slm66018 		PR0("Invalid client connection state");
49751ae08745Sheppo 		return (ENOTSUP);
49761ae08745Sheppo 	}
49771ae08745Sheppo }
49781ae08745Sheppo 
4979d10e4ef2Snarayan static int
49803af08d82Slm66018 vd_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
49811ae08745Sheppo {
49821ae08745Sheppo 	int		status;
49831ae08745Sheppo 	boolean_t	reset_ldc = B_FALSE;
4984205eeb1aSlm66018 	vd_task_t	task;
49851ae08745Sheppo 
49861ae08745Sheppo 	/*
49871ae08745Sheppo 	 * Check that the message is at least big enough for a "tag", so that
49881ae08745Sheppo 	 * message processing can proceed based on tag-specified message type
49891ae08745Sheppo 	 */
49901ae08745Sheppo 	if (msglen < sizeof (vio_msg_tag_t)) {
49913af08d82Slm66018 		PR0("Received short (%lu-byte) message", msglen);
49921ae08745Sheppo 		/* Can't "nack" short message, so drop the big hammer */
49933af08d82Slm66018 		PR0("initiating full reset");
4994d10e4ef2Snarayan 		vd_need_reset(vd, B_TRUE);
4995d10e4ef2Snarayan 		return (EBADMSG);
49961ae08745Sheppo 	}
49971ae08745Sheppo 
49981ae08745Sheppo 	/*
49991ae08745Sheppo 	 * Process the message
50001ae08745Sheppo 	 */
50013af08d82Slm66018 	switch (status = vd_do_process_msg(vd, msg, msglen)) {
50021ae08745Sheppo 	case 0:
50031ae08745Sheppo 		/* "ack" valid, successfully-processed messages */
50041ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
50051ae08745Sheppo 		break;
50061ae08745Sheppo 
5007d10e4ef2Snarayan 	case EINPROGRESS:
5008d10e4ef2Snarayan 		/* The completion handler will "ack" or "nack" the message */
5009d10e4ef2Snarayan 		return (EINPROGRESS);
50101ae08745Sheppo 	case ENOMSG:
50113af08d82Slm66018 		PR0("Received unexpected message");
50121ae08745Sheppo 		_NOTE(FALLTHROUGH);
50131ae08745Sheppo 	case EBADMSG:
50141ae08745Sheppo 	case ENOTSUP:
5015205eeb1aSlm66018 		/* "transport" error will cause NACK of invalid messages */
50161ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
50171ae08745Sheppo 		break;
50181ae08745Sheppo 
50191ae08745Sheppo 	default:
5020205eeb1aSlm66018 		/* "transport" error will cause NACK of invalid messages */
50211ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
50221ae08745Sheppo 		/* An LDC error probably occurred, so try resetting it */
50231ae08745Sheppo 		reset_ldc = B_TRUE;
50241ae08745Sheppo 		break;
50251ae08745Sheppo 	}
50261ae08745Sheppo 
50273af08d82Slm66018 	PR1("\tResulting in state %d (%s)", vd->state,
50283af08d82Slm66018 	    vd_decode_state(vd->state));
50293af08d82Slm66018 
5030205eeb1aSlm66018 	/* populate the task so we can dispatch it on the taskq */
5031205eeb1aSlm66018 	task.vd = vd;
5032205eeb1aSlm66018 	task.msg = msg;
5033205eeb1aSlm66018 	task.msglen = msglen;
5034205eeb1aSlm66018 
5035205eeb1aSlm66018 	/*
5036205eeb1aSlm66018 	 * Queue a task to send the notification that the operation completed.
5037205eeb1aSlm66018 	 * We need to ensure that requests are responded to in the correct
5038205eeb1aSlm66018 	 * order and since the taskq is processed serially this ordering
5039205eeb1aSlm66018 	 * is maintained.
5040205eeb1aSlm66018 	 */
5041205eeb1aSlm66018 	(void) ddi_taskq_dispatch(vd->completionq, vd_serial_notify,
5042205eeb1aSlm66018 	    &task, DDI_SLEEP);
5043205eeb1aSlm66018 
5044205eeb1aSlm66018 	/*
5045205eeb1aSlm66018 	 * To ensure handshake negotiations do not happen out of order, such
5046205eeb1aSlm66018 	 * requests that come through this path should not be done in parallel
5047205eeb1aSlm66018 	 * so we need to wait here until the response is sent to the client.
5048205eeb1aSlm66018 	 */
5049205eeb1aSlm66018 	ddi_taskq_wait(vd->completionq);
50501ae08745Sheppo 
5051d10e4ef2Snarayan 	/* Arrange to reset the connection for nack'ed or failed messages */
50523af08d82Slm66018 	if ((status != 0) || reset_ldc) {
50533af08d82Slm66018 		PR0("initiating %s reset",
50543af08d82Slm66018 		    (reset_ldc) ? "full" : "soft");
5055d10e4ef2Snarayan 		vd_need_reset(vd, reset_ldc);
50563af08d82Slm66018 	}
5057d10e4ef2Snarayan 
5058d10e4ef2Snarayan 	return (status);
5059d10e4ef2Snarayan }
5060d10e4ef2Snarayan 
5061d10e4ef2Snarayan static boolean_t
5062d10e4ef2Snarayan vd_enabled(vd_t *vd)
5063d10e4ef2Snarayan {
5064d10e4ef2Snarayan 	boolean_t	enabled;
5065d10e4ef2Snarayan 
5066d10e4ef2Snarayan 	mutex_enter(&vd->lock);
5067d10e4ef2Snarayan 	enabled = vd->enabled;
5068d10e4ef2Snarayan 	mutex_exit(&vd->lock);
5069d10e4ef2Snarayan 	return (enabled);
50701ae08745Sheppo }
50711ae08745Sheppo 
50721ae08745Sheppo static void
50730a55fbb7Slm66018 vd_recv_msg(void *arg)
50741ae08745Sheppo {
50751ae08745Sheppo 	vd_t	*vd = (vd_t *)arg;
50763af08d82Slm66018 	int	rv = 0, status = 0;
50771ae08745Sheppo 
50781ae08745Sheppo 	ASSERT(vd != NULL);
50793af08d82Slm66018 
5080d10e4ef2Snarayan 	PR2("New task to receive incoming message(s)");
50813af08d82Slm66018 
50823af08d82Slm66018 
5083d10e4ef2Snarayan 	while (vd_enabled(vd) && status == 0) {
5084d10e4ef2Snarayan 		size_t		msglen, msgsize;
50853af08d82Slm66018 		ldc_status_t	lstatus;
5086d10e4ef2Snarayan 
50870a55fbb7Slm66018 		/*
5088d10e4ef2Snarayan 		 * Receive and process a message
50890a55fbb7Slm66018 		 */
5090d10e4ef2Snarayan 		vd_reset_if_needed(vd);	/* can change vd->max_msglen */
50913af08d82Slm66018 
50923af08d82Slm66018 		/*
50933af08d82Slm66018 		 * check if channel is UP - else break out of loop
50943af08d82Slm66018 		 */
50953af08d82Slm66018 		status = ldc_status(vd->ldc_handle, &lstatus);
50963af08d82Slm66018 		if (lstatus != LDC_UP) {
50973af08d82Slm66018 			PR0("channel not up (status=%d), exiting recv loop\n",
50983af08d82Slm66018 			    lstatus);
50993af08d82Slm66018 			break;
51003af08d82Slm66018 		}
51013af08d82Slm66018 
51023af08d82Slm66018 		ASSERT(vd->max_msglen != 0);
51033af08d82Slm66018 
5104d10e4ef2Snarayan 		msgsize = vd->max_msglen; /* stable copy for alloc/free */
51053af08d82Slm66018 		msglen	= msgsize;	  /* actual len after recv_msg() */
51063af08d82Slm66018 
51073af08d82Slm66018 		status = recv_msg(vd->ldc_handle, vd->vio_msgp, &msglen);
51083af08d82Slm66018 		switch (status) {
51093af08d82Slm66018 		case 0:
5110342440ecSPrasad Singamsetty 			rv = vd_process_msg(vd, (void *)vd->vio_msgp, msglen);
51113af08d82Slm66018 			/* check if max_msglen changed */
51123af08d82Slm66018 			if (msgsize != vd->max_msglen) {
51133af08d82Slm66018 				PR0("max_msglen changed 0x%lx to 0x%lx bytes\n",
51143af08d82Slm66018 				    msgsize, vd->max_msglen);
51153af08d82Slm66018 				kmem_free(vd->vio_msgp, msgsize);
51163af08d82Slm66018 				vd->vio_msgp =
51173af08d82Slm66018 				    kmem_alloc(vd->max_msglen, KM_SLEEP);
51183af08d82Slm66018 			}
51193af08d82Slm66018 			if (rv == EINPROGRESS)
51203af08d82Slm66018 				continue;
51213af08d82Slm66018 			break;
51223af08d82Slm66018 
51233af08d82Slm66018 		case ENOMSG:
51243af08d82Slm66018 			break;
51253af08d82Slm66018 
51263af08d82Slm66018 		case ECONNRESET:
51273af08d82Slm66018 			PR0("initiating soft reset (ECONNRESET)\n");
51283af08d82Slm66018 			vd_need_reset(vd, B_FALSE);
51293af08d82Slm66018 			status = 0;
51303af08d82Slm66018 			break;
51313af08d82Slm66018 
51323af08d82Slm66018 		default:
5133d10e4ef2Snarayan 			/* Probably an LDC failure; arrange to reset it */
51343af08d82Slm66018 			PR0("initiating full reset (status=0x%x)", status);
5135d10e4ef2Snarayan 			vd_need_reset(vd, B_TRUE);
51363af08d82Slm66018 			break;
51370a55fbb7Slm66018 		}
51381ae08745Sheppo 	}
51393af08d82Slm66018 
5140d10e4ef2Snarayan 	PR2("Task finished");
51410a55fbb7Slm66018 }
51420a55fbb7Slm66018 
51430a55fbb7Slm66018 static uint_t
51441ae08745Sheppo vd_handle_ldc_events(uint64_t event, caddr_t arg)
51451ae08745Sheppo {
51461ae08745Sheppo 	vd_t	*vd = (vd_t *)(void *)arg;
51473af08d82Slm66018 	int	status;
51481ae08745Sheppo 
51491ae08745Sheppo 	ASSERT(vd != NULL);
5150d10e4ef2Snarayan 
5151d10e4ef2Snarayan 	if (!vd_enabled(vd))
5152d10e4ef2Snarayan 		return (LDC_SUCCESS);
5153d10e4ef2Snarayan 
51543af08d82Slm66018 	if (event & LDC_EVT_DOWN) {
515534683adeSsg70180 		PR0("LDC_EVT_DOWN: LDC channel went down");
51563af08d82Slm66018 
51573af08d82Slm66018 		vd_need_reset(vd, B_TRUE);
51583af08d82Slm66018 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
51593af08d82Slm66018 		    DDI_SLEEP);
51603af08d82Slm66018 		if (status == DDI_FAILURE) {
51613af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
51623af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
51633af08d82Slm66018 		}
51643af08d82Slm66018 	}
51653af08d82Slm66018 
5166d10e4ef2Snarayan 	if (event & LDC_EVT_RESET) {
51673af08d82Slm66018 		PR0("LDC_EVT_RESET: LDC channel was reset");
51683af08d82Slm66018 
51693af08d82Slm66018 		if (vd->state != VD_STATE_INIT) {
51703af08d82Slm66018 			PR0("scheduling full reset");
51713af08d82Slm66018 			vd_need_reset(vd, B_FALSE);
51723af08d82Slm66018 			status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
51733af08d82Slm66018 			    vd, DDI_SLEEP);
51743af08d82Slm66018 			if (status == DDI_FAILURE) {
51753af08d82Slm66018 				PR0("cannot schedule task to recv msg\n");
51763af08d82Slm66018 				vd_need_reset(vd, B_TRUE);
51773af08d82Slm66018 			}
51783af08d82Slm66018 
51793af08d82Slm66018 		} else {
51803af08d82Slm66018 			PR0("channel already reset, ignoring...\n");
51813af08d82Slm66018 			PR0("doing ldc up...\n");
51823af08d82Slm66018 			(void) ldc_up(vd->ldc_handle);
51833af08d82Slm66018 		}
51843af08d82Slm66018 
5185d10e4ef2Snarayan 		return (LDC_SUCCESS);
5186d10e4ef2Snarayan 	}
5187d10e4ef2Snarayan 
5188d10e4ef2Snarayan 	if (event & LDC_EVT_UP) {
51893af08d82Slm66018 		PR0("EVT_UP: LDC is up\nResetting client connection state");
51903af08d82Slm66018 		PR0("initiating soft reset");
5191d10e4ef2Snarayan 		vd_need_reset(vd, B_FALSE);
51923af08d82Slm66018 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
51933af08d82Slm66018 		    vd, DDI_SLEEP);
51943af08d82Slm66018 		if (status == DDI_FAILURE) {
51953af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
51963af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
51973af08d82Slm66018 			return (LDC_SUCCESS);
51983af08d82Slm66018 		}
5199d10e4ef2Snarayan 	}
5200d10e4ef2Snarayan 
5201d10e4ef2Snarayan 	if (event & LDC_EVT_READ) {
5202d10e4ef2Snarayan 		int	status;
5203d10e4ef2Snarayan 
5204d10e4ef2Snarayan 		PR1("New data available");
5205d10e4ef2Snarayan 		/* Queue a task to receive the new data */
5206d10e4ef2Snarayan 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
5207d10e4ef2Snarayan 		    DDI_SLEEP);
52083af08d82Slm66018 
52093af08d82Slm66018 		if (status == DDI_FAILURE) {
52103af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
52113af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
52123af08d82Slm66018 		}
5213d10e4ef2Snarayan 	}
5214d10e4ef2Snarayan 
5215d10e4ef2Snarayan 	return (LDC_SUCCESS);
52161ae08745Sheppo }
52171ae08745Sheppo 
52181ae08745Sheppo static uint_t
52191ae08745Sheppo vds_check_for_vd(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
52201ae08745Sheppo {
52211ae08745Sheppo 	_NOTE(ARGUNUSED(key, val))
52221ae08745Sheppo 	(*((uint_t *)arg))++;
52231ae08745Sheppo 	return (MH_WALK_TERMINATE);
52241ae08745Sheppo }
52251ae08745Sheppo 
52261ae08745Sheppo 
52271ae08745Sheppo static int
52281ae08745Sheppo vds_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
52291ae08745Sheppo {
52301ae08745Sheppo 	uint_t	vd_present = 0;
52311ae08745Sheppo 	minor_t	instance;
52321ae08745Sheppo 	vds_t	*vds;
52331ae08745Sheppo 
52341ae08745Sheppo 
52351ae08745Sheppo 	switch (cmd) {
52361ae08745Sheppo 	case DDI_DETACH:
52371ae08745Sheppo 		/* the real work happens below */
52381ae08745Sheppo 		break;
52391ae08745Sheppo 	case DDI_SUSPEND:
5240d10e4ef2Snarayan 		PR0("No action required for DDI_SUSPEND");
52411ae08745Sheppo 		return (DDI_SUCCESS);
52421ae08745Sheppo 	default:
52433af08d82Slm66018 		PR0("Unrecognized \"cmd\"");
52441ae08745Sheppo 		return (DDI_FAILURE);
52451ae08745Sheppo 	}
52461ae08745Sheppo 
52471ae08745Sheppo 	ASSERT(cmd == DDI_DETACH);
52481ae08745Sheppo 	instance = ddi_get_instance(dip);
52491ae08745Sheppo 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
52503af08d82Slm66018 		PR0("Could not get state for instance %u", instance);
52511ae08745Sheppo 		ddi_soft_state_free(vds_state, instance);
52521ae08745Sheppo 		return (DDI_FAILURE);
52531ae08745Sheppo 	}
52541ae08745Sheppo 
52551ae08745Sheppo 	/* Do no detach when serving any vdisks */
52561ae08745Sheppo 	mod_hash_walk(vds->vd_table, vds_check_for_vd, &vd_present);
52571ae08745Sheppo 	if (vd_present) {
52581ae08745Sheppo 		PR0("Not detaching because serving vdisks");
52591ae08745Sheppo 		return (DDI_FAILURE);
52601ae08745Sheppo 	}
52611ae08745Sheppo 
52621ae08745Sheppo 	PR0("Detaching");
5263445b4c2eSsb155480 	if (vds->initialized & VDS_MDEG) {
52641ae08745Sheppo 		(void) mdeg_unregister(vds->mdeg);
5265445b4c2eSsb155480 		kmem_free(vds->ispecp->specp, sizeof (vds_prop_template));
5266445b4c2eSsb155480 		kmem_free(vds->ispecp, sizeof (mdeg_node_spec_t));
5267445b4c2eSsb155480 		vds->ispecp = NULL;
5268445b4c2eSsb155480 		vds->mdeg = NULL;
5269445b4c2eSsb155480 	}
5270445b4c2eSsb155480 
52718fce2fd6Sachartre 	vds_driver_types_free(vds);
52728fce2fd6Sachartre 
52731ae08745Sheppo 	if (vds->initialized & VDS_LDI)
52741ae08745Sheppo 		(void) ldi_ident_release(vds->ldi_ident);
52751ae08745Sheppo 	mod_hash_destroy_hash(vds->vd_table);
52761ae08745Sheppo 	ddi_soft_state_free(vds_state, instance);
52771ae08745Sheppo 	return (DDI_SUCCESS);
52781ae08745Sheppo }
52791ae08745Sheppo 
528017cadca8Slm66018 /*
528117cadca8Slm66018  * Description:
5282*1aff8f07SAlexandre Chartre  *	This function checks to see if the disk image being used as a
5283*1aff8f07SAlexandre Chartre  *	virtual disk is an ISO image. An ISO image is a special case
5284*1aff8f07SAlexandre Chartre  *	which can be booted/installed from like a CD/DVD.
528517cadca8Slm66018  *
528617cadca8Slm66018  * Parameters:
528717cadca8Slm66018  *	vd		- disk on which the operation is performed.
528817cadca8Slm66018  *
528917cadca8Slm66018  * Return Code:
5290*1aff8f07SAlexandre Chartre  *	B_TRUE		- The disk image is an ISO 9660 compliant image
5291*1aff8f07SAlexandre Chartre  *	B_FALSE		- just a regular disk image
529217cadca8Slm66018  */
529317cadca8Slm66018 static boolean_t
5294*1aff8f07SAlexandre Chartre vd_dskimg_is_iso_image(vd_t *vd)
529517cadca8Slm66018 {
529617cadca8Slm66018 	char	iso_buf[ISO_SECTOR_SIZE];
529717cadca8Slm66018 	int	i, rv;
529817cadca8Slm66018 	uint_t	sec;
529917cadca8Slm66018 
5300*1aff8f07SAlexandre Chartre 	ASSERT(VD_DSKIMG(vd));
530117cadca8Slm66018 
530217cadca8Slm66018 	/*
530317cadca8Slm66018 	 * If we have already discovered and saved this info we can
5304*1aff8f07SAlexandre Chartre 	 * short-circuit the check and avoid reading the disk image.
530517cadca8Slm66018 	 */
530617cadca8Slm66018 	if (vd->vdisk_media == VD_MEDIA_DVD || vd->vdisk_media == VD_MEDIA_CD)
530717cadca8Slm66018 		return (B_TRUE);
530817cadca8Slm66018 
530917cadca8Slm66018 	/*
531017cadca8Slm66018 	 * We wish to read the sector that should contain the 2nd ISO volume
531117cadca8Slm66018 	 * descriptor. The second field in this descriptor is called the
531217cadca8Slm66018 	 * Standard Identifier and is set to CD001 for a CD-ROM compliant
531317cadca8Slm66018 	 * to the ISO 9660 standard.
531417cadca8Slm66018 	 */
531517cadca8Slm66018 	sec = (ISO_VOLDESC_SEC * ISO_SECTOR_SIZE) / vd->vdisk_block_size;
5316*1aff8f07SAlexandre Chartre 	rv = vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)iso_buf,
531717cadca8Slm66018 	    sec, ISO_SECTOR_SIZE);
531817cadca8Slm66018 
531917cadca8Slm66018 	if (rv < 0)
532017cadca8Slm66018 		return (B_FALSE);
532117cadca8Slm66018 
532217cadca8Slm66018 	for (i = 0; i < ISO_ID_STRLEN; i++) {
532317cadca8Slm66018 		if (ISO_STD_ID(iso_buf)[i] != ISO_ID_STRING[i])
532417cadca8Slm66018 			return (B_FALSE);
532517cadca8Slm66018 	}
532617cadca8Slm66018 
532717cadca8Slm66018 	return (B_TRUE);
532817cadca8Slm66018 }
532917cadca8Slm66018 
533017cadca8Slm66018 /*
533117cadca8Slm66018  * Description:
533217cadca8Slm66018  *	This function checks to see if the virtual device is an ATAPI
533317cadca8Slm66018  *	device. ATAPI devices use Group 1 Read/Write commands, so
533417cadca8Slm66018  *	any USCSI calls vds makes need to take this into account.
533517cadca8Slm66018  *
533617cadca8Slm66018  * Parameters:
533717cadca8Slm66018  *	vd		- disk on which the operation is performed.
533817cadca8Slm66018  *
533917cadca8Slm66018  * Return Code:
534017cadca8Slm66018  *	B_TRUE		- The virtual disk is backed by an ATAPI device
534117cadca8Slm66018  *	B_FALSE		- not an ATAPI device (presumably SCSI)
534217cadca8Slm66018  */
534317cadca8Slm66018 static boolean_t
534417cadca8Slm66018 vd_is_atapi_device(vd_t *vd)
534517cadca8Slm66018 {
534617cadca8Slm66018 	boolean_t	is_atapi = B_FALSE;
534717cadca8Slm66018 	char		*variantp;
534817cadca8Slm66018 	int		rv;
534917cadca8Slm66018 
535017cadca8Slm66018 	ASSERT(vd->ldi_handle[0] != NULL);
535117cadca8Slm66018 	ASSERT(!vd->file);
535217cadca8Slm66018 
535317cadca8Slm66018 	rv = ldi_prop_lookup_string(vd->ldi_handle[0],
535417cadca8Slm66018 	    (LDI_DEV_T_ANY | DDI_PROP_DONTPASS), "variant", &variantp);
535517cadca8Slm66018 	if (rv == DDI_PROP_SUCCESS) {
535617cadca8Slm66018 		PR0("'variant' property exists for %s", vd->device_path);
535717cadca8Slm66018 		if (strcmp(variantp, "atapi") == 0)
535817cadca8Slm66018 			is_atapi = B_TRUE;
535917cadca8Slm66018 		ddi_prop_free(variantp);
536017cadca8Slm66018 	}
536117cadca8Slm66018 
536217cadca8Slm66018 	rv = ldi_prop_exists(vd->ldi_handle[0], LDI_DEV_T_ANY, "atapi");
536317cadca8Slm66018 	if (rv) {
536417cadca8Slm66018 		PR0("'atapi' property exists for %s", vd->device_path);
536517cadca8Slm66018 		is_atapi = B_TRUE;
536617cadca8Slm66018 	}
536717cadca8Slm66018 
536817cadca8Slm66018 	return (is_atapi);
536917cadca8Slm66018 }
537017cadca8Slm66018 
53711ae08745Sheppo static int
53722f5224aeSachartre vd_setup_full_disk(vd_t *vd)
53732f5224aeSachartre {
53742f5224aeSachartre 	int		status;
53752f5224aeSachartre 	major_t		major = getmajor(vd->dev[0]);
53762f5224aeSachartre 	minor_t		minor = getminor(vd->dev[0]) - VD_ENTIRE_DISK_SLICE;
53772f5224aeSachartre 
5378047ba61eSachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
5379047ba61eSachartre 
53802f5224aeSachartre 	vd->vdisk_block_size = DEV_BSIZE;
53812f5224aeSachartre 
5382de3a5331SRamesh Chitrothu 	/* set the disk size, block size and the media type of the disk */
5383de3a5331SRamesh Chitrothu 	status = vd_backend_check_size(vd);
53842f5224aeSachartre 
53852f5224aeSachartre 	if (status != 0) {
53862f5224aeSachartre 		if (!vd->scsi) {
53872f5224aeSachartre 			/* unexpected failure */
5388690555a1Sachartre 			PRN("ldi_ioctl(DKIOCGMEDIAINFO) returned errno %d",
53894bac2208Snarayan 			    status);
53900a55fbb7Slm66018 			return (status);
53910a55fbb7Slm66018 		}
53922f5224aeSachartre 
53932f5224aeSachartre 		/*
53942f5224aeSachartre 		 * The function can fail for SCSI disks which are present but
53952f5224aeSachartre 		 * reserved by another system. In that case, we don't know the
53962f5224aeSachartre 		 * size of the disk and the block size.
53972f5224aeSachartre 		 */
53982f5224aeSachartre 		vd->vdisk_size = VD_SIZE_UNKNOWN;
53992f5224aeSachartre 		vd->block_size = 0;
54002f5224aeSachartre 		vd->vdisk_media = VD_MEDIA_FIXED;
54012f5224aeSachartre 	}
54020a55fbb7Slm66018 
54030a55fbb7Slm66018 	/* Move dev number and LDI handle to entire-disk-slice array elements */
54040a55fbb7Slm66018 	vd->dev[VD_ENTIRE_DISK_SLICE]		= vd->dev[0];
54050a55fbb7Slm66018 	vd->dev[0]				= 0;
54060a55fbb7Slm66018 	vd->ldi_handle[VD_ENTIRE_DISK_SLICE]	= vd->ldi_handle[0];
54070a55fbb7Slm66018 	vd->ldi_handle[0]			= NULL;
54080a55fbb7Slm66018 
54090a55fbb7Slm66018 	/* Initialize device numbers for remaining slices and open them */
54100a55fbb7Slm66018 	for (int slice = 0; slice < vd->nslices; slice++) {
54110a55fbb7Slm66018 		/*
54120a55fbb7Slm66018 		 * Skip the entire-disk slice, as it's already open and its
54130a55fbb7Slm66018 		 * device known
54140a55fbb7Slm66018 		 */
54150a55fbb7Slm66018 		if (slice == VD_ENTIRE_DISK_SLICE)
54160a55fbb7Slm66018 			continue;
54170a55fbb7Slm66018 		ASSERT(vd->dev[slice] == 0);
54180a55fbb7Slm66018 		ASSERT(vd->ldi_handle[slice] == NULL);
54190a55fbb7Slm66018 
54200a55fbb7Slm66018 		/*
54210a55fbb7Slm66018 		 * Construct the device number for the current slice
54220a55fbb7Slm66018 		 */
54230a55fbb7Slm66018 		vd->dev[slice] = makedevice(major, (minor + slice));
54240a55fbb7Slm66018 
54250a55fbb7Slm66018 		/*
542634683adeSsg70180 		 * Open all slices of the disk to serve them to the client.
542734683adeSsg70180 		 * Slices are opened exclusively to prevent other threads or
542834683adeSsg70180 		 * processes in the service domain from performing I/O to
542934683adeSsg70180 		 * slices being accessed by a client.  Failure to open a slice
543034683adeSsg70180 		 * results in vds not serving this disk, as the client could
543134683adeSsg70180 		 * attempt (and should be able) to access any slice immediately.
543234683adeSsg70180 		 * Any slices successfully opened before a failure will get
543334683adeSsg70180 		 * closed by vds_destroy_vd() as a result of the error returned
543434683adeSsg70180 		 * by this function.
543534683adeSsg70180 		 *
543634683adeSsg70180 		 * We need to do the open with FNDELAY so that opening an empty
543734683adeSsg70180 		 * slice does not fail.
54380a55fbb7Slm66018 		 */
54390a55fbb7Slm66018 		PR0("Opening device major %u, minor %u = slice %u",
54400a55fbb7Slm66018 		    major, minor, slice);
5441047ba61eSachartre 
5442047ba61eSachartre 		/*
5443047ba61eSachartre 		 * Try to open the device. This can fail for example if we are
5444047ba61eSachartre 		 * opening an empty slice. So in case of a failure, we try the
5445047ba61eSachartre 		 * open again but this time with the FNDELAY flag.
5446047ba61eSachartre 		 */
5447047ba61eSachartre 		status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK,
5448047ba61eSachartre 		    vd->open_flags, kcred, &vd->ldi_handle[slice],
5449047ba61eSachartre 		    vd->vds->ldi_ident);
5450047ba61eSachartre 
5451047ba61eSachartre 		if (status != 0) {
5452047ba61eSachartre 			status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK,
5453047ba61eSachartre 			    vd->open_flags | FNDELAY, kcred,
5454047ba61eSachartre 			    &vd->ldi_handle[slice], vd->vds->ldi_ident);
5455047ba61eSachartre 		}
5456047ba61eSachartre 
5457047ba61eSachartre 		if (status != 0) {
5458690555a1Sachartre 			PRN("ldi_open_by_dev() returned errno %d "
54590a55fbb7Slm66018 			    "for slice %u", status, slice);
54600a55fbb7Slm66018 			/* vds_destroy_vd() will close any open slices */
5461690555a1Sachartre 			vd->ldi_handle[slice] = NULL;
54620a55fbb7Slm66018 			return (status);
54630a55fbb7Slm66018 		}
54640a55fbb7Slm66018 	}
54650a55fbb7Slm66018 
54660a55fbb7Slm66018 	return (0);
54670a55fbb7Slm66018 }
54680a55fbb7Slm66018 
5469edcc0754Sachartre /*
5470edcc0754Sachartre  * When a slice or a volume is exported as a single-slice disk, we want
5471edcc0754Sachartre  * the disk backend (i.e. the slice or volume) to be entirely mapped as
5472edcc0754Sachartre  * a slice without the addition of any metadata.
5473edcc0754Sachartre  *
5474edcc0754Sachartre  * So when exporting the disk as a VTOC disk, we fake a disk with the following
5475edcc0754Sachartre  * layout:
5476bae9e67eSachartre  *                flabel +--- flabel_limit
5477bae9e67eSachartre  *                 <->   V
5478bae9e67eSachartre  *                 0 1   C                          D  E
5479bae9e67eSachartre  *                 +-+---+--------------------------+--+
5480bae9e67eSachartre  *  virtual disk:  |L|XXX|           slice 0        |AA|
5481bae9e67eSachartre  *                 +-+---+--------------------------+--+
5482edcc0754Sachartre  *                  ^    :                          :
5483edcc0754Sachartre  *                  |    :                          :
5484edcc0754Sachartre  *      VTOC LABEL--+    :                          :
5485edcc0754Sachartre  *                       +--------------------------+
5486bae9e67eSachartre  *  disk backend:        |     slice/volume/file    |
5487edcc0754Sachartre  *                       +--------------------------+
5488edcc0754Sachartre  *                       0                          N
5489edcc0754Sachartre  *
5490bae9e67eSachartre  * N is the number of blocks in the slice/volume/file.
5491edcc0754Sachartre  *
5492bae9e67eSachartre  * We simulate a disk with N+M blocks, where M is the number of blocks
5493bae9e67eSachartre  * simluated at the beginning and at the end of the disk (blocks 0-C
5494bae9e67eSachartre  * and D-E).
5495edcc0754Sachartre  *
5496bae9e67eSachartre  * The first blocks (0 to C-1) are emulated and can not be changed. Blocks C
5497bae9e67eSachartre  * to D defines slice 0 and are mapped to the backend. Finally we emulate 2
5498bae9e67eSachartre  * alternate cylinders at the end of the disk (blocks D-E). In summary we have:
5499edcc0754Sachartre  *
5500bae9e67eSachartre  * - block 0 (L) returns a fake VTOC label
5501bae9e67eSachartre  * - blocks 1 to C-1 (X) are unused and return 0
5502bae9e67eSachartre  * - blocks C to D-1 are mapped to the exported slice or volume
5503bae9e67eSachartre  * - blocks D and E (A) are blocks defining alternate cylinders (2 cylinders)
5504bae9e67eSachartre  *
5505bae9e67eSachartre  * Note: because we define a fake disk geometry, it is possible that the length
5506bae9e67eSachartre  * of the backend is not a multiple of the size of cylinder, in that case the
5507bae9e67eSachartre  * very end of the backend will not map to any block of the virtual disk.
5508edcc0754Sachartre  */
55090a55fbb7Slm66018 static int
551078fcd0a1Sachartre vd_setup_partition_vtoc(vd_t *vd)
551178fcd0a1Sachartre {
551278fcd0a1Sachartre 	char *device_path = vd->device_path;
5513bae9e67eSachartre 	char unit;
5514bae9e67eSachartre 	size_t size, csize;
551578fcd0a1Sachartre 
551678fcd0a1Sachartre 	/* Initialize dk_geom structure for single-slice device */
551778fcd0a1Sachartre 	if (vd->dk_geom.dkg_nsect == 0) {
551878fcd0a1Sachartre 		PRN("%s geometry claims 0 sectors per track", device_path);
551978fcd0a1Sachartre 		return (EIO);
552078fcd0a1Sachartre 	}
552178fcd0a1Sachartre 	if (vd->dk_geom.dkg_nhead == 0) {
552278fcd0a1Sachartre 		PRN("%s geometry claims 0 heads", device_path);
552378fcd0a1Sachartre 		return (EIO);
552478fcd0a1Sachartre 	}
5525bae9e67eSachartre 
5526bae9e67eSachartre 	/* size of a cylinder in block */
5527bae9e67eSachartre 	csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
5528bae9e67eSachartre 
5529bae9e67eSachartre 	/*
5530bae9e67eSachartre 	 * Add extra cylinders: we emulate the first cylinder (which contains
5531bae9e67eSachartre 	 * the disk label).
5532bae9e67eSachartre 	 */
5533bae9e67eSachartre 	vd->dk_geom.dkg_ncyl = vd->vdisk_size / csize + 1;
5534bae9e67eSachartre 
5535bae9e67eSachartre 	/* we emulate 2 alternate cylinders */
5536bae9e67eSachartre 	vd->dk_geom.dkg_acyl = 2;
553778fcd0a1Sachartre 	vd->dk_geom.dkg_pcyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl;
553878fcd0a1Sachartre 
553978fcd0a1Sachartre 
554078fcd0a1Sachartre 	/* Initialize vtoc structure for single-slice device */
554178fcd0a1Sachartre 	bzero(vd->vtoc.v_part, sizeof (vd->vtoc.v_part));
554278fcd0a1Sachartre 	vd->vtoc.v_part[0].p_tag = V_UNASSIGNED;
554378fcd0a1Sachartre 	vd->vtoc.v_part[0].p_flag = 0;
5544bae9e67eSachartre 	/*
5545bae9e67eSachartre 	 * Partition 0 starts on cylinder 1 and its size has to be
5546bae9e67eSachartre 	 * a multiple of a number of cylinder.
5547bae9e67eSachartre 	 */
5548bae9e67eSachartre 	vd->vtoc.v_part[0].p_start = csize; /* start on cylinder 1 */
5549bae9e67eSachartre 	vd->vtoc.v_part[0].p_size = (vd->vdisk_size / csize) * csize;
555078fcd0a1Sachartre 
5551bae9e67eSachartre 	if (vd_slice_single_slice) {
5552bae9e67eSachartre 		vd->vtoc.v_nparts = 1;
5553bae9e67eSachartre 		bcopy(VD_ASCIILABEL, vd->vtoc.v_asciilabel,
5554bae9e67eSachartre 		    MIN(sizeof (VD_ASCIILABEL),
5555bae9e67eSachartre 		    sizeof (vd->vtoc.v_asciilabel)));
5556bae9e67eSachartre 		bcopy(VD_VOLUME_NAME, vd->vtoc.v_volume,
5557bae9e67eSachartre 		    MIN(sizeof (VD_VOLUME_NAME), sizeof (vd->vtoc.v_volume)));
5558bae9e67eSachartre 	} else {
5559bae9e67eSachartre 		/* adjust the number of slices */
5560bae9e67eSachartre 		vd->nslices = V_NUMPAR;
5561bae9e67eSachartre 		vd->vtoc.v_nparts = V_NUMPAR;
5562bae9e67eSachartre 
5563bae9e67eSachartre 		/* define slice 2 representing the entire disk */
5564bae9e67eSachartre 		vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_tag = V_BACKUP;
5565bae9e67eSachartre 		vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_flag = 0;
5566bae9e67eSachartre 		vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_start = 0;
5567bae9e67eSachartre 		vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_size =
5568bae9e67eSachartre 		    vd->dk_geom.dkg_ncyl * csize;
5569bae9e67eSachartre 
5570bae9e67eSachartre 		vd_get_readable_size(vd->vdisk_size * vd->vdisk_block_size,
5571bae9e67eSachartre 		    &size, &unit);
5572bae9e67eSachartre 
5573bae9e67eSachartre 		/*
5574bae9e67eSachartre 		 * Set some attributes of the geometry to what format(1m) uses
5575bae9e67eSachartre 		 * so that writing a default label using format(1m) does not
5576bae9e67eSachartre 		 * produce any error.
5577bae9e67eSachartre 		 */
5578bae9e67eSachartre 		vd->dk_geom.dkg_bcyl = 0;
5579bae9e67eSachartre 		vd->dk_geom.dkg_intrlv = 1;
5580bae9e67eSachartre 		vd->dk_geom.dkg_write_reinstruct = 0;
5581bae9e67eSachartre 		vd->dk_geom.dkg_read_reinstruct = 0;
5582bae9e67eSachartre 
5583bae9e67eSachartre 		/*
5584bae9e67eSachartre 		 * We must have a correct label name otherwise format(1m) will
5585bae9e67eSachartre 		 * not recognized the disk as labeled.
5586bae9e67eSachartre 		 */
5587bae9e67eSachartre 		(void) snprintf(vd->vtoc.v_asciilabel, LEN_DKL_ASCII,
5588bae9e67eSachartre 		    "SUN-DiskSlice-%ld%cB cyl %d alt %d hd %d sec %d",
5589bae9e67eSachartre 		    size, unit,
5590bae9e67eSachartre 		    vd->dk_geom.dkg_ncyl, vd->dk_geom.dkg_acyl,
5591bae9e67eSachartre 		    vd->dk_geom.dkg_nhead, vd->dk_geom.dkg_nsect);
5592bae9e67eSachartre 		bzero(vd->vtoc.v_volume, sizeof (vd->vtoc.v_volume));
5593bae9e67eSachartre 
5594bae9e67eSachartre 		/* create a fake label from the vtoc and geometry */
5595342440ecSPrasad Singamsetty 		vd->flabel_limit = (uint_t)csize;
5596bae9e67eSachartre 		vd->flabel_size = VD_LABEL_VTOC_SIZE;
5597bae9e67eSachartre 		vd->flabel = kmem_zalloc(vd->flabel_size, KM_SLEEP);
5598bae9e67eSachartre 		vd_vtocgeom_to_label(&vd->vtoc, &vd->dk_geom,
5599bae9e67eSachartre 		    VD_LABEL_VTOC(vd));
5600bae9e67eSachartre 	}
5601bae9e67eSachartre 
5602bae9e67eSachartre 	/* adjust the vdisk_size, we emulate 3 cylinders */
5603bae9e67eSachartre 	vd->vdisk_size += csize * 3;
5604edcc0754Sachartre 
560578fcd0a1Sachartre 	return (0);
560678fcd0a1Sachartre }
560778fcd0a1Sachartre 
5608edcc0754Sachartre /*
5609edcc0754Sachartre  * When a slice, volume or file is exported as a single-slice disk, we want
5610edcc0754Sachartre  * the disk backend (i.e. the slice, volume or file) to be entirely mapped
5611edcc0754Sachartre  * as a slice without the addition of any metadata.
5612edcc0754Sachartre  *
5613edcc0754Sachartre  * So when exporting the disk as an EFI disk, we fake a disk with the following
5614edcc0754Sachartre  * layout:
5615edcc0754Sachartre  *
5616bae9e67eSachartre  *                  flabel        +--- flabel_limit
5617bae9e67eSachartre  *                 <------>       v
5618bae9e67eSachartre  *                 0 1 2  L      34                        34+N      P
5619bae9e67eSachartre  *                 +-+-+--+-------+--------------------------+-------+
5620bae9e67eSachartre  *  virtual disk:  |X|T|EE|XXXXXXX|           slice 0        |RRRRRRR|
5621bae9e67eSachartre  *                 +-+-+--+-------+--------------------------+-------+
5622edcc0754Sachartre  *                    ^ ^         :                          :
5623edcc0754Sachartre  *                    | |         :                          :
5624edcc0754Sachartre  *                GPT-+ +-GPE     :                          :
5625edcc0754Sachartre  *                                +--------------------------+
5626edcc0754Sachartre  *  disk backend:                 |     slice/volume/file    |
5627edcc0754Sachartre  *                                +--------------------------+
5628edcc0754Sachartre  *                                0                          N
5629edcc0754Sachartre  *
5630edcc0754Sachartre  * N is the number of blocks in the slice/volume/file.
5631edcc0754Sachartre  *
5632bae9e67eSachartre  * We simulate a disk with N+M blocks, where M is the number of blocks
5633bae9e67eSachartre  * simluated at the beginning and at the end of the disk (blocks 0-34
5634bae9e67eSachartre  * and 34+N-P).
5635edcc0754Sachartre  *
5636bae9e67eSachartre  * The first 34 blocks (0 to 33) are emulated and can not be changed. Blocks 34
5637bae9e67eSachartre  * to 34+N defines slice 0 and are mapped to the exported backend, and we
5638bae9e67eSachartre  * emulate some blocks at the end of the disk (blocks 34+N to P) as a the EFI
5639bae9e67eSachartre  * reserved partition.
5640bae9e67eSachartre  *
5641bae9e67eSachartre  * - block 0 (X) is unused and return 0
5642edcc0754Sachartre  * - block 1 (T) returns a fake EFI GPT (via DKIOCGETEFI)
5643bae9e67eSachartre  * - blocks 2 to L-1 (E) defines a fake EFI GPE (via DKIOCGETEFI)
5644bae9e67eSachartre  * - blocks L to 33 (X) are unused and return 0
5645bae9e67eSachartre  * - blocks 34 to 34+N are mapped to the exported slice, volume or file
5646bae9e67eSachartre  * - blocks 34+N+1 to P define a fake reserved partition and backup label, it
5647bae9e67eSachartre  *   returns 0
5648edcc0754Sachartre  *
5649bae9e67eSachartre  * Note: if the backend size is not a multiple of the vdisk block size
5650bae9e67eSachartre  * (DEV_BSIZE = 512 byte) then the very end of the backend will not map to
5651bae9e67eSachartre  * any block of the virtual disk.
5652edcc0754Sachartre  */
565378fcd0a1Sachartre static int
56544bac2208Snarayan vd_setup_partition_efi(vd_t *vd)
56554bac2208Snarayan {
56564bac2208Snarayan 	efi_gpt_t *gpt;
56574bac2208Snarayan 	efi_gpe_t *gpe;
5658edcc0754Sachartre 	struct uuid uuid = EFI_USR;
5659bae9e67eSachartre 	struct uuid efi_reserved = EFI_RESERVED;
56604bac2208Snarayan 	uint32_t crc;
5661bae9e67eSachartre 	uint64_t s0_start, s0_end;
56624bac2208Snarayan 
5663bae9e67eSachartre 	vd->flabel_limit = 34;
5664bae9e67eSachartre 	vd->flabel_size = VD_LABEL_EFI_SIZE;
5665bae9e67eSachartre 	vd->flabel = kmem_zalloc(vd->flabel_size, KM_SLEEP);
5666bae9e67eSachartre 	gpt = VD_LABEL_EFI_GPT(vd);
5667bae9e67eSachartre 	gpe = VD_LABEL_EFI_GPE(vd);
5668edcc0754Sachartre 
5669edcc0754Sachartre 	/* adjust the vdisk_size, we emulate the first 34 blocks */
5670edcc0754Sachartre 	vd->vdisk_size += 34;
5671bae9e67eSachartre 	s0_start = 34;
5672bae9e67eSachartre 	s0_end = vd->vdisk_size - 1;
56734bac2208Snarayan 
56744bac2208Snarayan 	gpt->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
56754bac2208Snarayan 	gpt->efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
56764bac2208Snarayan 	gpt->efi_gpt_HeaderSize = LE_32(sizeof (efi_gpt_t));
5677edcc0754Sachartre 	gpt->efi_gpt_FirstUsableLBA = LE_64(34ULL);
5678edcc0754Sachartre 	gpt->efi_gpt_PartitionEntryLBA = LE_64(2ULL);
56794bac2208Snarayan 	gpt->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (efi_gpe_t));
56804bac2208Snarayan 
5681bae9e67eSachartre 	UUID_LE_CONVERT(gpe[0].efi_gpe_PartitionTypeGUID, uuid);
5682bae9e67eSachartre 	gpe[0].efi_gpe_StartingLBA = LE_64(s0_start);
5683bae9e67eSachartre 	gpe[0].efi_gpe_EndingLBA = LE_64(s0_end);
56844bac2208Snarayan 
5685bae9e67eSachartre 	if (vd_slice_single_slice) {
5686bae9e67eSachartre 		gpt->efi_gpt_NumberOfPartitionEntries = LE_32(1);
5687bae9e67eSachartre 	} else {
5688bae9e67eSachartre 		/* adjust the number of slices */
5689bae9e67eSachartre 		gpt->efi_gpt_NumberOfPartitionEntries = LE_32(VD_MAXPART);
5690bae9e67eSachartre 		vd->nslices = V_NUMPAR;
5691bae9e67eSachartre 
5692bae9e67eSachartre 		/* define a fake reserved partition */
5693bae9e67eSachartre 		UUID_LE_CONVERT(gpe[VD_MAXPART - 1].efi_gpe_PartitionTypeGUID,
5694bae9e67eSachartre 		    efi_reserved);
5695bae9e67eSachartre 		gpe[VD_MAXPART - 1].efi_gpe_StartingLBA =
5696bae9e67eSachartre 		    LE_64(s0_end + 1);
5697bae9e67eSachartre 		gpe[VD_MAXPART - 1].efi_gpe_EndingLBA =
5698bae9e67eSachartre 		    LE_64(s0_end + EFI_MIN_RESV_SIZE);
5699bae9e67eSachartre 
5700bae9e67eSachartre 		/* adjust the vdisk_size to include the reserved slice */
5701bae9e67eSachartre 		vd->vdisk_size += EFI_MIN_RESV_SIZE;
5702bae9e67eSachartre 	}
5703bae9e67eSachartre 
5704bae9e67eSachartre 	gpt->efi_gpt_LastUsableLBA = LE_64(vd->vdisk_size - 1);
5705bae9e67eSachartre 
5706bae9e67eSachartre 	/* adjust the vdisk size for the backup GPT and GPE */
5707bae9e67eSachartre 	vd->vdisk_size += 33;
5708bae9e67eSachartre 
5709bae9e67eSachartre 	CRC32(crc, gpe, sizeof (efi_gpe_t) * VD_MAXPART, -1U, crc32_table);
57104bac2208Snarayan 	gpt->efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
57114bac2208Snarayan 
57124bac2208Snarayan 	CRC32(crc, gpt, sizeof (efi_gpt_t), -1U, crc32_table);
57134bac2208Snarayan 	gpt->efi_gpt_HeaderCRC32 = LE_32(~crc);
57144bac2208Snarayan 
57154bac2208Snarayan 	return (0);
57164bac2208Snarayan }
57174bac2208Snarayan 
5718047ba61eSachartre /*
5719047ba61eSachartre  * Setup for a virtual disk whose backend is a file (exported as a single slice
5720*1aff8f07SAlexandre Chartre  * or as a full disk). In that case, the backend is accessed using the vnode
5721*1aff8f07SAlexandre Chartre  * interface.
5722047ba61eSachartre  */
57234bac2208Snarayan static int
5724047ba61eSachartre vd_setup_backend_vnode(vd_t *vd)
57253c96341aSnarayan {
572678fcd0a1Sachartre 	int 		rval, status;
57273c96341aSnarayan 	vattr_t		vattr;
57283c96341aSnarayan 	dev_t		dev;
57293c96341aSnarayan 	char		*file_path = vd->device_path;
57303c96341aSnarayan 	ldi_handle_t	lhandle;
57313c96341aSnarayan 	struct dk_cinfo	dk_cinfo;
5732*1aff8f07SAlexandre Chartre 
5733*1aff8f07SAlexandre Chartre 	ASSERT(!vd->volume);
57343c96341aSnarayan 
5735047ba61eSachartre 	if ((status = vn_open(file_path, UIO_SYSSPACE, vd->open_flags | FOFFMAX,
57363c96341aSnarayan 	    0, &vd->file_vnode, 0, 0)) != 0) {
5737690555a1Sachartre 		PRN("vn_open(%s) = errno %d", file_path, status);
57383c96341aSnarayan 		return (status);
57393c96341aSnarayan 	}
57403c96341aSnarayan 
5741690555a1Sachartre 	/*
5742690555a1Sachartre 	 * We set vd->file now so that vds_destroy_vd will take care of
5743690555a1Sachartre 	 * closing the file and releasing the vnode in case of an error.
5744690555a1Sachartre 	 */
5745690555a1Sachartre 	vd->file = B_TRUE;
5746690555a1Sachartre 
57473c96341aSnarayan 	vattr.va_mask = AT_SIZE;
5748da6c28aaSamw 	if ((status = VOP_GETATTR(vd->file_vnode, &vattr, 0, kcred, NULL))
5749da6c28aaSamw 	    != 0) {
5750690555a1Sachartre 		PRN("VOP_GETATTR(%s) = errno %d", file_path, status);
57513c96341aSnarayan 		return (EIO);
57523c96341aSnarayan 	}
57533c96341aSnarayan 
5754*1aff8f07SAlexandre Chartre 	vd->dskimg_size = vattr.va_size;
57553c96341aSnarayan 
5756690555a1Sachartre 	if (vd->file_vnode->v_flag & VNOMAP) {
5757690555a1Sachartre 		PRN("File %s cannot be mapped", file_path);
57583c96341aSnarayan 		return (EIO);
57593c96341aSnarayan 	}
57603c96341aSnarayan 
57613c96341aSnarayan 	vd->max_xfer_sz = maxphys / DEV_BSIZE; /* default transfer size */
57623c96341aSnarayan 
5763047ba61eSachartre 	/*
5764*1aff8f07SAlexandre Chartre 	 * Get max_xfer_sz from the device where the file is.
5765047ba61eSachartre 	 */
57663c96341aSnarayan 	dev = vd->file_vnode->v_vfsp->vfs_dev;
5767f745d6a3Sachartre 	PR0("underlying device of %s = (%d, %d)\n", file_path,
5768f745d6a3Sachartre 	    getmajor(dev), getminor(dev));
57693c96341aSnarayan 
5770047ba61eSachartre 	status = ldi_open_by_dev(&dev, OTYP_BLK, FREAD, kcred, &lhandle,
5771047ba61eSachartre 	    vd->vds->ldi_ident);
5772047ba61eSachartre 
5773047ba61eSachartre 	if (status != 0) {
5774f745d6a3Sachartre 		PR0("ldi_open() returned errno %d for underlying device",
5775f745d6a3Sachartre 		    status);
57763c96341aSnarayan 	} else {
57773c96341aSnarayan 		if ((status = ldi_ioctl(lhandle, DKIOCINFO,
5778047ba61eSachartre 		    (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred,
57793c96341aSnarayan 		    &rval)) != 0) {
5780f745d6a3Sachartre 			PR0("ldi_ioctl(DKIOCINFO) returned errno %d for "
5781f745d6a3Sachartre 			    "underlying device", status);
57823c96341aSnarayan 		} else {
57833c96341aSnarayan 			/*
57843c96341aSnarayan 			 * Store the device's max transfer size for
57853c96341aSnarayan 			 * return to the client
57863c96341aSnarayan 			 */
57873c96341aSnarayan 			vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
57883c96341aSnarayan 		}
57893c96341aSnarayan 
5790f745d6a3Sachartre 		PR0("close the underlying device");
57913c96341aSnarayan 		(void) ldi_close(lhandle, FREAD, kcred);
57923c96341aSnarayan 	}
57933c96341aSnarayan 
5794f745d6a3Sachartre 	PR0("using file %s on device (%d, %d), max_xfer = %u blks",
5795f745d6a3Sachartre 	    file_path, getmajor(dev), getminor(dev), vd->max_xfer_sz);
5796*1aff8f07SAlexandre Chartre 
5797*1aff8f07SAlexandre Chartre 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE)
5798*1aff8f07SAlexandre Chartre 		status = vd_setup_slice_image(vd);
5799*1aff8f07SAlexandre Chartre 	else
5800*1aff8f07SAlexandre Chartre 		status = vd_setup_disk_image(vd);
5801*1aff8f07SAlexandre Chartre 
5802*1aff8f07SAlexandre Chartre 	return (status);
5803f745d6a3Sachartre }
58043c96341aSnarayan 
5805*1aff8f07SAlexandre Chartre static int
5806*1aff8f07SAlexandre Chartre vd_setup_slice_image(vd_t *vd)
5807*1aff8f07SAlexandre Chartre {
5808*1aff8f07SAlexandre Chartre 	struct dk_label label;
5809*1aff8f07SAlexandre Chartre 	int status;
5810*1aff8f07SAlexandre Chartre 
5811*1aff8f07SAlexandre Chartre 	/* sector size = block size = DEV_BSIZE */
5812*1aff8f07SAlexandre Chartre 	vd->block_size = DEV_BSIZE;
5813*1aff8f07SAlexandre Chartre 	vd->vdisk_block_size = DEV_BSIZE;
5814*1aff8f07SAlexandre Chartre 	vd->vdisk_size = vd->dskimg_size / DEV_BSIZE;
5815bae9e67eSachartre 	vd->vdisk_media = VD_MEDIA_FIXED;
5816bae9e67eSachartre 	vd->vdisk_label = (vd_slice_label == VD_DISK_LABEL_UNK)?
5817bae9e67eSachartre 	    vd_file_slice_label : vd_slice_label;
5818*1aff8f07SAlexandre Chartre 
5819bae9e67eSachartre 	if (vd->vdisk_label == VD_DISK_LABEL_EFI ||
5820*1aff8f07SAlexandre Chartre 	    vd->dskimg_size >= 2 * ONE_TERABYTE) {
5821edcc0754Sachartre 		status = vd_setup_partition_efi(vd);
5822bae9e67eSachartre 	} else {
5823bae9e67eSachartre 		/*
5824bae9e67eSachartre 		 * We build a default label to get a geometry for
5825bae9e67eSachartre 		 * the vdisk. Then the partition setup function will
5826bae9e67eSachartre 		 * adjust the vtoc so that it defines a single-slice
5827bae9e67eSachartre 		 * disk.
5828bae9e67eSachartre 		 */
5829*1aff8f07SAlexandre Chartre 		vd_build_default_label(vd->dskimg_size, &label);
5830bae9e67eSachartre 		vd_label_to_vtocgeom(&label, &vd->vtoc, &vd->dk_geom);
5831bae9e67eSachartre 		status = vd_setup_partition_vtoc(vd);
5832bae9e67eSachartre 	}
5833*1aff8f07SAlexandre Chartre 
5834bae9e67eSachartre 	return (status);
5835edcc0754Sachartre }
5836edcc0754Sachartre 
5837*1aff8f07SAlexandre Chartre static int
5838*1aff8f07SAlexandre Chartre vd_setup_disk_image(vd_t *vd)
5839*1aff8f07SAlexandre Chartre {
5840*1aff8f07SAlexandre Chartre 	int status;
5841*1aff8f07SAlexandre Chartre 	char *backend_path = vd->device_path;
5842*1aff8f07SAlexandre Chartre 
5843*1aff8f07SAlexandre Chartre 	/* size should be at least sizeof(dk_label) */
5844*1aff8f07SAlexandre Chartre 	if (vd->dskimg_size < sizeof (struct dk_label)) {
5845*1aff8f07SAlexandre Chartre 		PRN("Size of file has to be at least %ld bytes",
5846*1aff8f07SAlexandre Chartre 		    sizeof (struct dk_label));
5847*1aff8f07SAlexandre Chartre 		return (EIO);
5848*1aff8f07SAlexandre Chartre 	}
5849*1aff8f07SAlexandre Chartre 
5850*1aff8f07SAlexandre Chartre 	/* sector size = block size = DEV_BSIZE */
5851*1aff8f07SAlexandre Chartre 	vd->block_size = DEV_BSIZE;
5852*1aff8f07SAlexandre Chartre 	vd->vdisk_block_size = DEV_BSIZE;
5853*1aff8f07SAlexandre Chartre 	vd->vdisk_size = vd->dskimg_size / DEV_BSIZE;
5854*1aff8f07SAlexandre Chartre 
5855edcc0754Sachartre 	/*
5856edcc0754Sachartre 	 * Find and validate the geometry of a disk image.
5857edcc0754Sachartre 	 */
5858*1aff8f07SAlexandre Chartre 	status = vd_dskimg_validate_geometry(vd);
5859edcc0754Sachartre 	if (status != 0 && status != EINVAL && status != ENOTSUP) {
5860*1aff8f07SAlexandre Chartre 		PRN("Failed to read label from %s", backend_path);
5861edcc0754Sachartre 		return (EIO);
5862edcc0754Sachartre 	}
5863edcc0754Sachartre 
5864*1aff8f07SAlexandre Chartre 	if (vd_dskimg_is_iso_image(vd)) {
5865edcc0754Sachartre 		/*
5866edcc0754Sachartre 		 * Indicate whether to call this a CD or DVD from the size
5867edcc0754Sachartre 		 * of the ISO image (images for both drive types are stored
5868edcc0754Sachartre 		 * in the ISO-9600 format). CDs can store up to just under 1Gb
5869edcc0754Sachartre 		 */
5870342440ecSPrasad Singamsetty 		if ((vd->vdisk_size * vd->vdisk_block_size) > ONE_GIGABYTE)
5871edcc0754Sachartre 			vd->vdisk_media = VD_MEDIA_DVD;
5872edcc0754Sachartre 		else
5873edcc0754Sachartre 			vd->vdisk_media = VD_MEDIA_CD;
5874edcc0754Sachartre 	} else {
5875edcc0754Sachartre 		vd->vdisk_media = VD_MEDIA_FIXED;
5876edcc0754Sachartre 	}
5877edcc0754Sachartre 
5878edcc0754Sachartre 	/* Setup devid for the disk image */
5879047ba61eSachartre 
588078fcd0a1Sachartre 	if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
588178fcd0a1Sachartre 
5882*1aff8f07SAlexandre Chartre 		status = vd_dskimg_read_devid(vd, &vd->dskimg_devid);
588387a7269eSachartre 
588487a7269eSachartre 		if (status == 0) {
588587a7269eSachartre 			/* a valid devid was found */
588687a7269eSachartre 			return (0);
588787a7269eSachartre 		}
588887a7269eSachartre 
588987a7269eSachartre 		if (status != EINVAL) {
589087a7269eSachartre 			/*
589178fcd0a1Sachartre 			 * There was an error while trying to read the devid.
589278fcd0a1Sachartre 			 * So this disk image may have a devid but we are
589378fcd0a1Sachartre 			 * unable to read it.
589487a7269eSachartre 			 */
5895*1aff8f07SAlexandre Chartre 			PR0("can not read devid for %s", backend_path);
5896*1aff8f07SAlexandre Chartre 			vd->dskimg_devid = NULL;
589787a7269eSachartre 			return (0);
589887a7269eSachartre 		}
589978fcd0a1Sachartre 	}
590087a7269eSachartre 
590187a7269eSachartre 	/*
590287a7269eSachartre 	 * No valid device id was found so we create one. Note that a failure
590387a7269eSachartre 	 * to create a device id is not fatal and does not prevent the disk
590487a7269eSachartre 	 * image from being attached.
590587a7269eSachartre 	 */
5906*1aff8f07SAlexandre Chartre 	PR1("creating devid for %s", backend_path);
590787a7269eSachartre 
590887a7269eSachartre 	if (ddi_devid_init(vd->vds->dip, DEVID_FAB, NULL, 0,
5909*1aff8f07SAlexandre Chartre 	    &vd->dskimg_devid) != DDI_SUCCESS) {
5910*1aff8f07SAlexandre Chartre 		PR0("fail to create devid for %s", backend_path);
5911*1aff8f07SAlexandre Chartre 		vd->dskimg_devid = NULL;
591287a7269eSachartre 		return (0);
591387a7269eSachartre 	}
591487a7269eSachartre 
591578fcd0a1Sachartre 	/*
591678fcd0a1Sachartre 	 * Write devid to the disk image. The devid is stored into the disk
591778fcd0a1Sachartre 	 * image if we have a valid label; otherwise the devid will be stored
591878fcd0a1Sachartre 	 * when the user writes a valid label.
591978fcd0a1Sachartre 	 */
592078fcd0a1Sachartre 	if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
5921*1aff8f07SAlexandre Chartre 		if (vd_dskimg_write_devid(vd, vd->dskimg_devid) != 0) {
5922*1aff8f07SAlexandre Chartre 			PR0("fail to write devid for %s", backend_path);
5923*1aff8f07SAlexandre Chartre 			ddi_devid_free(vd->dskimg_devid);
5924*1aff8f07SAlexandre Chartre 			vd->dskimg_devid = NULL;
592587a7269eSachartre 		}
592678fcd0a1Sachartre 	}
592787a7269eSachartre 
59283c96341aSnarayan 	return (0);
59293c96341aSnarayan }
59303c96341aSnarayan 
593117cadca8Slm66018 
593217cadca8Slm66018 /*
593317cadca8Slm66018  * Description:
593417cadca8Slm66018  *	Open a device using its device path (supplied by ldm(1m))
593517cadca8Slm66018  *
593617cadca8Slm66018  * Parameters:
593717cadca8Slm66018  *	vd 	- pointer to structure containing the vDisk info
59388fce2fd6Sachartre  *	flags	- open flags
593917cadca8Slm66018  *
594017cadca8Slm66018  * Return Value
594117cadca8Slm66018  *	0	- success
594217cadca8Slm66018  *	!= 0	- some other non-zero return value from ldi(9F) functions
594317cadca8Slm66018  */
594417cadca8Slm66018 static int
59458fce2fd6Sachartre vd_open_using_ldi_by_name(vd_t *vd, int flags)
594617cadca8Slm66018 {
59478fce2fd6Sachartre 	int		status;
594817cadca8Slm66018 	char		*device_path = vd->device_path;
594917cadca8Slm66018 
59508fce2fd6Sachartre 	/* Attempt to open device */
59518fce2fd6Sachartre 	status = ldi_open_by_name(device_path, flags, kcred,
595217cadca8Slm66018 	    &vd->ldi_handle[0], vd->vds->ldi_ident);
595317cadca8Slm66018 
595417cadca8Slm66018 	/*
595517cadca8Slm66018 	 * The open can fail for example if we are opening an empty slice.
595617cadca8Slm66018 	 * In case of a failure, we try the open again but this time with
595717cadca8Slm66018 	 * the FNDELAY flag.
595817cadca8Slm66018 	 */
595917cadca8Slm66018 	if (status != 0)
59608fce2fd6Sachartre 		status = ldi_open_by_name(device_path, flags | FNDELAY,
596117cadca8Slm66018 		    kcred, &vd->ldi_handle[0], vd->vds->ldi_ident);
596217cadca8Slm66018 
596317cadca8Slm66018 	if (status != 0) {
596417cadca8Slm66018 		PR0("ldi_open_by_name(%s) = errno %d", device_path, status);
596517cadca8Slm66018 		vd->ldi_handle[0] = NULL;
596617cadca8Slm66018 		return (status);
596717cadca8Slm66018 	}
596817cadca8Slm66018 
596917cadca8Slm66018 	return (0);
597017cadca8Slm66018 }
597117cadca8Slm66018 
5972047ba61eSachartre /*
5973047ba61eSachartre  * Setup for a virtual disk which backend is a device (a physical disk,
5974*1aff8f07SAlexandre Chartre  * slice or volume device) exported as a full disk or as a slice. In these
5975*1aff8f07SAlexandre Chartre  * cases, the backend is accessed using the LDI interface.
5976047ba61eSachartre  */
59773c96341aSnarayan static int
5978047ba61eSachartre vd_setup_backend_ldi(vd_t *vd)
59791ae08745Sheppo {
5980e1ebb9ecSlm66018 	int		rval, status;
59811ae08745Sheppo 	struct dk_cinfo	dk_cinfo;
59823c96341aSnarayan 	char		*device_path = vd->device_path;
59831ae08745Sheppo 
59848fce2fd6Sachartre 	/* device has been opened by vd_identify_dev() */
59858fce2fd6Sachartre 	ASSERT(vd->ldi_handle[0] != NULL);
59868fce2fd6Sachartre 	ASSERT(vd->dev[0] != NULL);
59870a55fbb7Slm66018 
59883c96341aSnarayan 	vd->file = B_FALSE;
59894bac2208Snarayan 
599078fcd0a1Sachartre 	/* Verify backing device supports dk_cinfo */
5991e1ebb9ecSlm66018 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO,
5992047ba61eSachartre 	    (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred,
5993e1ebb9ecSlm66018 	    &rval)) != 0) {
5994e1ebb9ecSlm66018 		PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
5995e1ebb9ecSlm66018 		    status, device_path);
5996e1ebb9ecSlm66018 		return (status);
5997e1ebb9ecSlm66018 	}
5998e1ebb9ecSlm66018 	if (dk_cinfo.dki_partition >= V_NUMPAR) {
5999e1ebb9ecSlm66018 		PRN("slice %u >= maximum slice %u for %s",
6000e1ebb9ecSlm66018 		    dk_cinfo.dki_partition, V_NUMPAR, device_path);
6001e1ebb9ecSlm66018 		return (EIO);
6002e1ebb9ecSlm66018 	}
60034bac2208Snarayan 
60048fce2fd6Sachartre 	/*
60058fce2fd6Sachartre 	 * The device has been opened read-only by vd_identify_dev(), re-open
60068fce2fd6Sachartre 	 * it read-write if the write flag is set and we don't have an optical
60078fce2fd6Sachartre 	 * device such as a CD-ROM, which, for now, we do not permit writes to
60088fce2fd6Sachartre 	 * and thus should not export write operations to the client.
60098fce2fd6Sachartre 	 *
60108fce2fd6Sachartre 	 * Future: if/when we implement support for guest domains writing to
60118fce2fd6Sachartre 	 * optical devices we will need to do further checking of the media type
60128fce2fd6Sachartre 	 * to distinguish between read-only and writable discs.
60138fce2fd6Sachartre 	 */
60148fce2fd6Sachartre 	if (dk_cinfo.dki_ctype == DKC_CDROM) {
60158fce2fd6Sachartre 
60168fce2fd6Sachartre 		vd->open_flags &= ~FWRITE;
60178fce2fd6Sachartre 
60188fce2fd6Sachartre 	} else if (vd->open_flags & FWRITE) {
60198fce2fd6Sachartre 
60208fce2fd6Sachartre 		(void) ldi_close(vd->ldi_handle[0], vd->open_flags & ~FWRITE,
60218fce2fd6Sachartre 		    kcred);
60228fce2fd6Sachartre 		status = vd_open_using_ldi_by_name(vd, vd->open_flags);
60238fce2fd6Sachartre 		if (status != 0) {
60248fce2fd6Sachartre 			PR0("Failed to open (%s) = errno %d",
60258fce2fd6Sachartre 			    device_path, status);
60268fce2fd6Sachartre 			return (status);
60278fce2fd6Sachartre 		}
60288fce2fd6Sachartre 	}
60298fce2fd6Sachartre 
6030e1ebb9ecSlm66018 	/* Store the device's max transfer size for return to the client */
6031e1ebb9ecSlm66018 	vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
6032e1ebb9ecSlm66018 
6033047ba61eSachartre 	/*
603417cadca8Slm66018 	 * We need to work out if it's an ATAPI (IDE CD-ROM) or SCSI device so
603517cadca8Slm66018 	 * that we can use the correct CDB group when sending USCSI commands.
603617cadca8Slm66018 	 */
603717cadca8Slm66018 	vd->is_atapi_dev = vd_is_atapi_device(vd);
603817cadca8Slm66018 
603917cadca8Slm66018 	/*
6040047ba61eSachartre 	 * Export a full disk.
6041047ba61eSachartre 	 *
6042*1aff8f07SAlexandre Chartre 	 * The exported device can be either a volume, a disk or a CD/DVD
6043*1aff8f07SAlexandre Chartre 	 * device.  We export a device as a full disk if we have an entire
6044*1aff8f07SAlexandre Chartre 	 * disk slice (slice 2) and if this slice is exported as a full disk
6045*1aff8f07SAlexandre Chartre 	 * and not as a single slice disk. A CD or DVD device is exported
6046*1aff8f07SAlexandre Chartre 	 * as a full disk (even if it isn't s2). A volume is exported as a
6047*1aff8f07SAlexandre Chartre 	 * full disk as long as the "slice" option is not specified.
6048047ba61eSachartre 	 */
6049*1aff8f07SAlexandre Chartre 	if (vd->vdisk_type == VD_DISK_TYPE_DISK) {
6050*1aff8f07SAlexandre Chartre 
6051*1aff8f07SAlexandre Chartre 		if (vd->volume) {
6052*1aff8f07SAlexandre Chartre 			/* get size of backing device */
6053*1aff8f07SAlexandre Chartre 			if (ldi_get_size(vd->ldi_handle[0], &vd->dskimg_size) !=
6054*1aff8f07SAlexandre Chartre 			    DDI_SUCCESS) {
6055*1aff8f07SAlexandre Chartre 				PRN("ldi_get_size() failed for %s",
6056*1aff8f07SAlexandre Chartre 				    device_path);
6057*1aff8f07SAlexandre Chartre 				return (EIO);
6058*1aff8f07SAlexandre Chartre 			}
6059*1aff8f07SAlexandre Chartre 
6060*1aff8f07SAlexandre Chartre 			/* setup disk image */
6061*1aff8f07SAlexandre Chartre 			return (vd_setup_disk_image(vd));
6062*1aff8f07SAlexandre Chartre 		}
6063*1aff8f07SAlexandre Chartre 
6064*1aff8f07SAlexandre Chartre 		if (dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE ||
606517cadca8Slm66018 		    dk_cinfo.dki_ctype == DKC_CDROM) {
60668fce2fd6Sachartre 			ASSERT(!vd->volume);
60672f5224aeSachartre 			if (dk_cinfo.dki_ctype == DKC_SCSI_CCS)
60682f5224aeSachartre 				vd->scsi = B_TRUE;
6069047ba61eSachartre 			return (vd_setup_full_disk(vd));
6070047ba61eSachartre 		}
6071*1aff8f07SAlexandre Chartre 	}
6072047ba61eSachartre 
6073047ba61eSachartre 	/*
6074047ba61eSachartre 	 * Export a single slice disk.
6075047ba61eSachartre 	 *
60768fce2fd6Sachartre 	 * The exported device can be either a volume device or a disk slice. If
6077047ba61eSachartre 	 * it is a disk slice different from slice 2 then it is always exported
6078047ba61eSachartre 	 * as a single slice disk even if the "slice" option is not specified.
60798fce2fd6Sachartre 	 * If it is disk slice 2 or a volume device then it is exported as a
6080047ba61eSachartre 	 * single slice disk only if the "slice" option is specified.
6081047ba61eSachartre 	 */
6082047ba61eSachartre 	return (vd_setup_single_slice_disk(vd));
6083047ba61eSachartre }
6084047ba61eSachartre 
6085047ba61eSachartre static int
6086047ba61eSachartre vd_setup_single_slice_disk(vd_t *vd)
6087047ba61eSachartre {
6088edcc0754Sachartre 	int status, rval;
6089bae9e67eSachartre 	struct dk_label label;
6090047ba61eSachartre 	char *device_path = vd->device_path;
6091342440ecSPrasad Singamsetty 	struct vtoc vtoc;
6092047ba61eSachartre 
6093047ba61eSachartre 	/* Get size of backing device */
6094047ba61eSachartre 	if (ldi_get_size(vd->ldi_handle[0], &vd->vdisk_size) != DDI_SUCCESS) {
6095047ba61eSachartre 		PRN("ldi_get_size() failed for %s", device_path);
60961ae08745Sheppo 		return (EIO);
60971ae08745Sheppo 	}
6098047ba61eSachartre 	vd->vdisk_size = lbtodb(vd->vdisk_size);	/* convert to blocks */
609917cadca8Slm66018 	vd->block_size = DEV_BSIZE;
610017cadca8Slm66018 	vd->vdisk_block_size = DEV_BSIZE;
610117cadca8Slm66018 	vd->vdisk_media = VD_MEDIA_FIXED;
6102047ba61eSachartre 
61038fce2fd6Sachartre 	if (vd->volume) {
6104047ba61eSachartre 		ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
610578fcd0a1Sachartre 	}
61060a55fbb7Slm66018 
6107047ba61eSachartre 	/*
6108047ba61eSachartre 	 * We export the slice as a single slice disk even if the "slice"
6109047ba61eSachartre 	 * option was not specified.
6110047ba61eSachartre 	 */
61111ae08745Sheppo 	vd->vdisk_type  = VD_DISK_TYPE_SLICE;
61121ae08745Sheppo 	vd->nslices	= 1;
61131ae08745Sheppo 
6114edcc0754Sachartre 	/*
6115edcc0754Sachartre 	 * When exporting a slice or a device as a single slice disk, we don't
6116edcc0754Sachartre 	 * care about any partitioning exposed by the backend. The goal is just
6117edcc0754Sachartre 	 * to export the backend as a flat storage. We provide a fake partition
6118edcc0754Sachartre 	 * table (either a VTOC or EFI), which presents only one slice, to
6119bae9e67eSachartre 	 * accommodate tools expecting a disk label. The selection of the label
6120bae9e67eSachartre 	 * type (VTOC or EFI) depends on the value of the vd_slice_label
6121bae9e67eSachartre 	 * variable.
6122edcc0754Sachartre 	 */
6123bae9e67eSachartre 	if (vd_slice_label == VD_DISK_LABEL_EFI ||
6124bae9e67eSachartre 	    vd->vdisk_size >= ONE_TERABYTE / DEV_BSIZE) {
6125bae9e67eSachartre 		vd->vdisk_label = VD_DISK_LABEL_EFI;
6126bae9e67eSachartre 	} else {
6127342440ecSPrasad Singamsetty 		status = ldi_ioctl(vd->ldi_handle[0], DKIOCGEXTVTOC,
6128bae9e67eSachartre 		    (intptr_t)&vd->vtoc, (vd->open_flags | FKIOCTL),
6129bae9e67eSachartre 		    kcred, &rval);
6130edcc0754Sachartre 
6131342440ecSPrasad Singamsetty 		if (status == ENOTTY) {
6132342440ecSPrasad Singamsetty 			/* try with the non-extended vtoc ioctl */
6133342440ecSPrasad Singamsetty 			status = ldi_ioctl(vd->ldi_handle[0], DKIOCGVTOC,
6134342440ecSPrasad Singamsetty 			    (intptr_t)&vtoc, (vd->open_flags | FKIOCTL),
6135342440ecSPrasad Singamsetty 			    kcred, &rval);
6136342440ecSPrasad Singamsetty 			vtoctoextvtoc(vtoc, vd->vtoc);
6137342440ecSPrasad Singamsetty 		}
6138342440ecSPrasad Singamsetty 
6139edcc0754Sachartre 		if (status == 0) {
6140bae9e67eSachartre 			status = ldi_ioctl(vd->ldi_handle[0], DKIOCGGEOM,
6141bae9e67eSachartre 			    (intptr_t)&vd->dk_geom, (vd->open_flags | FKIOCTL),
6142bae9e67eSachartre 			    kcred, &rval);
6143bae9e67eSachartre 
6144bae9e67eSachartre 			if (status != 0) {
6145bae9e67eSachartre 				PRN("ldi_ioctl(DKIOCGEOM) returned errno %d "
6146bae9e67eSachartre 				    "for %s", status, device_path);
6147bae9e67eSachartre 				return (status);
6148bae9e67eSachartre 			}
6149edcc0754Sachartre 			vd->vdisk_label = VD_DISK_LABEL_VTOC;
6150bae9e67eSachartre 
6151bae9e67eSachartre 		} else if (vd_slice_label == VD_DISK_LABEL_VTOC) {
6152bae9e67eSachartre 
6153bae9e67eSachartre 			vd->vdisk_label = VD_DISK_LABEL_VTOC;
6154bae9e67eSachartre 			vd_build_default_label(vd->vdisk_size * DEV_BSIZE,
6155bae9e67eSachartre 			    &label);
6156bae9e67eSachartre 			vd_label_to_vtocgeom(&label, &vd->vtoc, &vd->dk_geom);
6157bae9e67eSachartre 
6158bae9e67eSachartre 		} else {
6159bae9e67eSachartre 			vd->vdisk_label = VD_DISK_LABEL_EFI;
6160bae9e67eSachartre 		}
6161bae9e67eSachartre 	}
6162bae9e67eSachartre 
6163bae9e67eSachartre 	if (vd->vdisk_label == VD_DISK_LABEL_VTOC) {
6164bae9e67eSachartre 		/* export with a fake VTOC label */
616578fcd0a1Sachartre 		status = vd_setup_partition_vtoc(vd);
6166bae9e67eSachartre 
6167edcc0754Sachartre 	} else {
6168edcc0754Sachartre 		/* export with a fake EFI label */
6169edcc0754Sachartre 		status = vd_setup_partition_efi(vd);
617078fcd0a1Sachartre 	}
617178fcd0a1Sachartre 
61724bac2208Snarayan 	return (status);
61734bac2208Snarayan }
61741ae08745Sheppo 
6175de3a5331SRamesh Chitrothu static int
6176de3a5331SRamesh Chitrothu vd_backend_check_size(vd_t *vd)
6177de3a5331SRamesh Chitrothu {
6178de3a5331SRamesh Chitrothu 	size_t backend_size, old_size, new_size;
6179de3a5331SRamesh Chitrothu 	struct dk_minfo minfo;
6180de3a5331SRamesh Chitrothu 	vattr_t vattr;
6181de3a5331SRamesh Chitrothu 	int rval, rv;
6182de3a5331SRamesh Chitrothu 
6183de3a5331SRamesh Chitrothu 	if (vd->file) {
6184de3a5331SRamesh Chitrothu 
6185de3a5331SRamesh Chitrothu 		/* file (slice or full disk) */
6186de3a5331SRamesh Chitrothu 		vattr.va_mask = AT_SIZE;
6187de3a5331SRamesh Chitrothu 		rv = VOP_GETATTR(vd->file_vnode, &vattr, 0, kcred, NULL);
6188de3a5331SRamesh Chitrothu 		if (rv != 0) {
6189de3a5331SRamesh Chitrothu 			PR0("VOP_GETATTR(%s) = errno %d", vd->device_path, rv);
6190de3a5331SRamesh Chitrothu 			return (rv);
6191de3a5331SRamesh Chitrothu 		}
6192de3a5331SRamesh Chitrothu 		backend_size = vattr.va_size;
6193de3a5331SRamesh Chitrothu 
6194*1aff8f07SAlexandre Chartre 	} else if (vd->volume || vd->vdisk_type == VD_DISK_TYPE_SLICE) {
6195de3a5331SRamesh Chitrothu 
6196*1aff8f07SAlexandre Chartre 		/* physical slice or volume (slice or full disk) */
6197de3a5331SRamesh Chitrothu 		rv = ldi_get_size(vd->ldi_handle[0], &backend_size);
6198de3a5331SRamesh Chitrothu 		if (rv != DDI_SUCCESS) {
6199de3a5331SRamesh Chitrothu 			PR0("ldi_get_size() failed for %s", vd->device_path);
6200de3a5331SRamesh Chitrothu 			return (EIO);
6201de3a5331SRamesh Chitrothu 		}
6202de3a5331SRamesh Chitrothu 
6203de3a5331SRamesh Chitrothu 	} else {
6204de3a5331SRamesh Chitrothu 
6205*1aff8f07SAlexandre Chartre 		/* physical disk */
6206de3a5331SRamesh Chitrothu 		ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
6207de3a5331SRamesh Chitrothu 		rv = ldi_ioctl(vd->ldi_handle[0], DKIOCGMEDIAINFO,
6208de3a5331SRamesh Chitrothu 		    (intptr_t)&minfo, (vd->open_flags | FKIOCTL),
6209de3a5331SRamesh Chitrothu 		    kcred, &rval);
6210de3a5331SRamesh Chitrothu 		if (rv != 0) {
6211de3a5331SRamesh Chitrothu 			PR0("DKIOCGMEDIAINFO failed for %s (err=%d)",
6212de3a5331SRamesh Chitrothu 			    vd->device_path, rv);
6213de3a5331SRamesh Chitrothu 			return (rv);
6214de3a5331SRamesh Chitrothu 		}
6215de3a5331SRamesh Chitrothu 		backend_size = minfo.dki_capacity * minfo.dki_lbsize;
6216de3a5331SRamesh Chitrothu 	}
6217de3a5331SRamesh Chitrothu 
6218de3a5331SRamesh Chitrothu 	old_size = vd->vdisk_size;
6219de3a5331SRamesh Chitrothu 	new_size = backend_size / DEV_BSIZE;
6220de3a5331SRamesh Chitrothu 
6221de3a5331SRamesh Chitrothu 	/* check if size has changed */
6222de3a5331SRamesh Chitrothu 	if (old_size != VD_SIZE_UNKNOWN && old_size == new_size)
6223de3a5331SRamesh Chitrothu 		return (0);
6224de3a5331SRamesh Chitrothu 
6225de3a5331SRamesh Chitrothu 	vd->vdisk_size = new_size;
6226de3a5331SRamesh Chitrothu 
6227*1aff8f07SAlexandre Chartre 	if (vd->file || vd->volume)
6228*1aff8f07SAlexandre Chartre 		vd->dskimg_size = backend_size;
6229de3a5331SRamesh Chitrothu 
6230de3a5331SRamesh Chitrothu 	/*
6231de3a5331SRamesh Chitrothu 	 * If we are exporting a single-slice disk and the size of the backend
6232de3a5331SRamesh Chitrothu 	 * has changed then we regenerate the partition setup so that the
6233de3a5331SRamesh Chitrothu 	 * partitioning matches with the new disk backend size.
6234de3a5331SRamesh Chitrothu 	 */
6235de3a5331SRamesh Chitrothu 
6236de3a5331SRamesh Chitrothu 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
6237de3a5331SRamesh Chitrothu 		/* slice or file or device exported as a slice */
6238de3a5331SRamesh Chitrothu 		if (vd->vdisk_label == VD_DISK_LABEL_VTOC) {
6239de3a5331SRamesh Chitrothu 			rv = vd_setup_partition_vtoc(vd);
6240de3a5331SRamesh Chitrothu 			if (rv != 0) {
6241de3a5331SRamesh Chitrothu 				PR0("vd_setup_partition_vtoc() failed for %s "
6242de3a5331SRamesh Chitrothu 				    "(err = %d)", vd->device_path, rv);
6243de3a5331SRamesh Chitrothu 				return (rv);
6244de3a5331SRamesh Chitrothu 			}
6245de3a5331SRamesh Chitrothu 		} else {
6246de3a5331SRamesh Chitrothu 			rv = vd_setup_partition_efi(vd);
6247de3a5331SRamesh Chitrothu 			if (rv != 0) {
6248de3a5331SRamesh Chitrothu 				PR0("vd_setup_partition_efi() failed for %s "
6249de3a5331SRamesh Chitrothu 				    "(err = %d)", vd->device_path, rv);
6250de3a5331SRamesh Chitrothu 				return (rv);
6251de3a5331SRamesh Chitrothu 			}
6252de3a5331SRamesh Chitrothu 		}
6253de3a5331SRamesh Chitrothu 
6254*1aff8f07SAlexandre Chartre 	} else if (!vd->file && !vd->volume) {
6255*1aff8f07SAlexandre Chartre 		/* physical disk */
6256de3a5331SRamesh Chitrothu 		ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
6257de3a5331SRamesh Chitrothu 		vd->block_size = minfo.dki_lbsize;
6258de3a5331SRamesh Chitrothu 		vd->vdisk_media =
6259de3a5331SRamesh Chitrothu 		    DK_MEDIATYPE2VD_MEDIATYPE(minfo.dki_media_type);
6260de3a5331SRamesh Chitrothu 	}
6261de3a5331SRamesh Chitrothu 
6262de3a5331SRamesh Chitrothu 	return (0);
6263de3a5331SRamesh Chitrothu }
6264de3a5331SRamesh Chitrothu 
62658fce2fd6Sachartre /*
62668fce2fd6Sachartre  * Description:
62678fce2fd6Sachartre  *	Open a device using its device path and identify if this is
62688fce2fd6Sachartre  *	a disk device or a volume device.
62698fce2fd6Sachartre  *
62708fce2fd6Sachartre  * Parameters:
62718fce2fd6Sachartre  *	vd 	- pointer to structure containing the vDisk info
62728fce2fd6Sachartre  *	dtype	- return the driver type of the device
62738fce2fd6Sachartre  *
62748fce2fd6Sachartre  * Return Value
62758fce2fd6Sachartre  *	0	- success
62768fce2fd6Sachartre  *	!= 0	- some other non-zero return value from ldi(9F) functions
62778fce2fd6Sachartre  */
62788fce2fd6Sachartre static int
62798fce2fd6Sachartre vd_identify_dev(vd_t *vd, int *dtype)
62808fce2fd6Sachartre {
62818fce2fd6Sachartre 	int status, i;
62828fce2fd6Sachartre 	char *device_path = vd->device_path;
62838fce2fd6Sachartre 	char *drv_name;
62848fce2fd6Sachartre 	int drv_type;
62858fce2fd6Sachartre 	vds_t *vds = vd->vds;
62868fce2fd6Sachartre 
62878fce2fd6Sachartre 	status = vd_open_using_ldi_by_name(vd, vd->open_flags & ~FWRITE);
62888fce2fd6Sachartre 	if (status != 0) {
62898fce2fd6Sachartre 		PR0("Failed to open (%s) = errno %d", device_path, status);
62908fce2fd6Sachartre 		return (status);
62918fce2fd6Sachartre 	}
62928fce2fd6Sachartre 
62938fce2fd6Sachartre 	/* Get device number of backing device */
62948fce2fd6Sachartre 	if ((status = ldi_get_dev(vd->ldi_handle[0], &vd->dev[0])) != 0) {
62958fce2fd6Sachartre 		PRN("ldi_get_dev() returned errno %d for %s",
62968fce2fd6Sachartre 		    status, device_path);
62978fce2fd6Sachartre 		return (status);
62988fce2fd6Sachartre 	}
62998fce2fd6Sachartre 
63008fce2fd6Sachartre 	/*
63018fce2fd6Sachartre 	 * We start by looking if the driver is in the list from vds.conf
63028fce2fd6Sachartre 	 * so that we can override the built-in list using vds.conf.
63038fce2fd6Sachartre 	 */
63048fce2fd6Sachartre 	drv_name = ddi_major_to_name(getmajor(vd->dev[0]));
63058fce2fd6Sachartre 	drv_type = VD_DRIVER_UNKNOWN;
63068fce2fd6Sachartre 
63078fce2fd6Sachartre 	/* check vds.conf list */
63088fce2fd6Sachartre 	for (i = 0; i < vds->num_drivers; i++) {
63098fce2fd6Sachartre 		if (vds->driver_types[i].type == VD_DRIVER_UNKNOWN) {
63108fce2fd6Sachartre 			/* ignore invalid entries */
63118fce2fd6Sachartre 			continue;
63128fce2fd6Sachartre 		}
63138fce2fd6Sachartre 		if (strcmp(drv_name, vds->driver_types[i].name) == 0) {
63148fce2fd6Sachartre 			drv_type = vds->driver_types[i].type;
63158fce2fd6Sachartre 			goto done;
63168fce2fd6Sachartre 		}
63178fce2fd6Sachartre 	}
63188fce2fd6Sachartre 
63198fce2fd6Sachartre 	/* check built-in list */
63208fce2fd6Sachartre 	for (i = 0; i < VDS_NUM_DRIVERS; i++) {
63218fce2fd6Sachartre 		if (strcmp(drv_name, vds_driver_types[i].name) == 0) {
63228fce2fd6Sachartre 			drv_type = vds_driver_types[i].type;
63238fce2fd6Sachartre 			goto done;
63248fce2fd6Sachartre 		}
63258fce2fd6Sachartre 	}
63268fce2fd6Sachartre 
63278fce2fd6Sachartre done:
63288fce2fd6Sachartre 	PR0("driver %s identified as %s", drv_name,
63298fce2fd6Sachartre 	    (drv_type == VD_DRIVER_DISK)? "DISK" :
63308fce2fd6Sachartre 	    (drv_type == VD_DRIVER_VOLUME)? "VOLUME" : "UNKNOWN");
63318fce2fd6Sachartre 
6332*1aff8f07SAlexandre Chartre 	if (strcmp(drv_name, "zfs") == 0)
6333*1aff8f07SAlexandre Chartre 		vd->zvol = B_TRUE;
6334*1aff8f07SAlexandre Chartre 
63358fce2fd6Sachartre 	*dtype = drv_type;
63368fce2fd6Sachartre 
63378fce2fd6Sachartre 	return (0);
63388fce2fd6Sachartre }
63398fce2fd6Sachartre 
63401ae08745Sheppo static int
6341047ba61eSachartre vd_setup_vd(vd_t *vd)
6342047ba61eSachartre {
63438fce2fd6Sachartre 	int		status, drv_type, pseudo;
6344047ba61eSachartre 	dev_info_t	*dip;
6345047ba61eSachartre 	vnode_t 	*vnp;
6346047ba61eSachartre 	char		*path = vd->device_path;
6347047ba61eSachartre 
6348047ba61eSachartre 	/* make sure the vdisk backend is valid */
6349047ba61eSachartre 	if ((status = lookupname(path, UIO_SYSSPACE,
6350047ba61eSachartre 	    FOLLOW, NULLVPP, &vnp)) != 0) {
6351047ba61eSachartre 		PR0("Cannot lookup %s errno %d", path, status);
6352047ba61eSachartre 		goto done;
6353047ba61eSachartre 	}
6354047ba61eSachartre 
6355047ba61eSachartre 	switch (vnp->v_type) {
6356047ba61eSachartre 	case VREG:
6357047ba61eSachartre 		/*
6358047ba61eSachartre 		 * Backend is a file so it is exported as a full disk or as a
6359047ba61eSachartre 		 * single slice disk using the vnode interface.
6360047ba61eSachartre 		 */
6361047ba61eSachartre 		VN_RELE(vnp);
63628fce2fd6Sachartre 		vd->volume = B_FALSE;
6363047ba61eSachartre 		status = vd_setup_backend_vnode(vd);
6364047ba61eSachartre 		break;
6365047ba61eSachartre 
6366047ba61eSachartre 	case VBLK:
6367047ba61eSachartre 	case VCHR:
6368047ba61eSachartre 		/*
6369*1aff8f07SAlexandre Chartre 		 * Backend is a device. In that case, it is exported using the
6370*1aff8f07SAlexandre Chartre 		 * LDI interface, and it is exported either as a single-slice
6371*1aff8f07SAlexandre Chartre 		 * disk or as a full disk depending on the "slice" option and
6372*1aff8f07SAlexandre Chartre 		 * on the type of device.
6373047ba61eSachartre 		 *
6374*1aff8f07SAlexandre Chartre 		 * - A volume device is exported as a single-slice disk if the
6375*1aff8f07SAlexandre Chartre 		 *   "slice" is specified, otherwise it is exported as a full
6376*1aff8f07SAlexandre Chartre 		 *   disk.
6377047ba61eSachartre 		 *
6378047ba61eSachartre 		 * - A disk slice (different from slice 2) is always exported
6379047ba61eSachartre 		 *   as a single slice disk using the LDI interface.
6380047ba61eSachartre 		 *
6381047ba61eSachartre 		 * - The slice 2 of a disk is exported as a single slice disk
6382047ba61eSachartre 		 *   if the "slice" option is specified, otherwise the entire
6383*1aff8f07SAlexandre Chartre 		 *   disk will be exported.
6384*1aff8f07SAlexandre Chartre 		 *
6385*1aff8f07SAlexandre Chartre 		 * - The slice of a CD or DVD is exported as single slice disk
6386*1aff8f07SAlexandre Chartre 		 *   if the "slice" option is specified, otherwise the entire
6387*1aff8f07SAlexandre Chartre 		 *   disk will be exported.
6388047ba61eSachartre 		 */
6389047ba61eSachartre 
6390047ba61eSachartre 		/* check if this is a pseudo device */
6391047ba61eSachartre 		if ((dip = ddi_hold_devi_by_instance(getmajor(vnp->v_rdev),
6392047ba61eSachartre 		    dev_to_instance(vnp->v_rdev), 0))  == NULL) {
6393047ba61eSachartre 			PRN("%s is no longer accessible", path);
6394047ba61eSachartre 			VN_RELE(vnp);
6395047ba61eSachartre 			status = EIO;
6396047ba61eSachartre 			break;
6397047ba61eSachartre 		}
63988fce2fd6Sachartre 		pseudo = is_pseudo_device(dip);
6399047ba61eSachartre 		ddi_release_devi(dip);
6400047ba61eSachartre 		VN_RELE(vnp);
6401047ba61eSachartre 
64028fce2fd6Sachartre 		if (vd_identify_dev(vd, &drv_type) != 0) {
64038fce2fd6Sachartre 			PRN("%s identification failed", path);
64048fce2fd6Sachartre 			status = EIO;
64058fce2fd6Sachartre 			break;
64068fce2fd6Sachartre 		}
64078fce2fd6Sachartre 
64088fce2fd6Sachartre 		/*
64098fce2fd6Sachartre 		 * If the driver hasn't been identified then we consider that
64108fce2fd6Sachartre 		 * pseudo devices are volumes and other devices are disks.
64118fce2fd6Sachartre 		 */
64128fce2fd6Sachartre 		if (drv_type == VD_DRIVER_VOLUME ||
64138fce2fd6Sachartre 		    (drv_type == VD_DRIVER_UNKNOWN && pseudo)) {
64148fce2fd6Sachartre 			vd->volume = B_TRUE;
64152f5224aeSachartre 		}
64162f5224aeSachartre 
6417047ba61eSachartre 		/*
64188fce2fd6Sachartre 		 * If this is a volume device then its usage depends if the
6419047ba61eSachartre 		 * "slice" option is set or not. If the "slice" option is set
64208fce2fd6Sachartre 		 * then the volume device will be exported as a single slice,
6421047ba61eSachartre 		 * otherwise it will be exported as a full disk.
64222f5224aeSachartre 		 *
64232f5224aeSachartre 		 * For backward compatibility, if vd_volume_force_slice is set
64248fce2fd6Sachartre 		 * then we always export volume devices as slices.
6425047ba61eSachartre 		 */
6426*1aff8f07SAlexandre Chartre 		if (vd->volume && vd_volume_force_slice) {
64272f5224aeSachartre 			vd->vdisk_type = VD_DISK_TYPE_SLICE;
64282f5224aeSachartre 			vd->nslices = 1;
64292f5224aeSachartre 		}
64302f5224aeSachartre 
6431047ba61eSachartre 		status = vd_setup_backend_ldi(vd);
6432047ba61eSachartre 		break;
6433047ba61eSachartre 
6434047ba61eSachartre 	default:
6435047ba61eSachartre 		PRN("Unsupported vdisk backend %s", path);
6436047ba61eSachartre 		VN_RELE(vnp);
6437047ba61eSachartre 		status = EBADF;
6438047ba61eSachartre 	}
6439047ba61eSachartre 
6440047ba61eSachartre done:
6441047ba61eSachartre 	if (status != 0) {
6442047ba61eSachartre 		/*
6443047ba61eSachartre 		 * If the error is retryable print an error message only
6444047ba61eSachartre 		 * during the first try.
6445047ba61eSachartre 		 */
6446047ba61eSachartre 		if (status == ENXIO || status == ENODEV ||
6447047ba61eSachartre 		    status == ENOENT || status == EROFS) {
6448047ba61eSachartre 			if (!(vd->initialized & VD_SETUP_ERROR)) {
6449047ba61eSachartre 				PRN("%s is currently inaccessible (error %d)",
6450047ba61eSachartre 				    path, status);
6451047ba61eSachartre 			}
6452047ba61eSachartre 			status = EAGAIN;
6453047ba61eSachartre 		} else {
6454047ba61eSachartre 			PRN("%s can not be exported as a virtual disk "
6455047ba61eSachartre 			    "(error %d)", path, status);
6456047ba61eSachartre 		}
6457047ba61eSachartre 		vd->initialized |= VD_SETUP_ERROR;
6458047ba61eSachartre 
6459047ba61eSachartre 	} else if (vd->initialized & VD_SETUP_ERROR) {
6460047ba61eSachartre 		/* print a message only if we previously had an error */
6461047ba61eSachartre 		PRN("%s is now online", path);
6462047ba61eSachartre 		vd->initialized &= ~VD_SETUP_ERROR;
6463047ba61eSachartre 	}
6464047ba61eSachartre 
6465047ba61eSachartre 	return (status);
6466047ba61eSachartre }
6467047ba61eSachartre 
6468047ba61eSachartre static int
6469047ba61eSachartre vds_do_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options,
6470047ba61eSachartre     uint64_t ldc_id, vd_t **vdp)
64711ae08745Sheppo {
64721ae08745Sheppo 	char			tq_name[TASKQ_NAMELEN];
64730a55fbb7Slm66018 	int			status;
64741ae08745Sheppo 	ddi_iblock_cookie_t	iblock = NULL;
64751ae08745Sheppo 	ldc_attr_t		ldc_attr;
64761ae08745Sheppo 	vd_t			*vd;
64771ae08745Sheppo 
64781ae08745Sheppo 
64791ae08745Sheppo 	ASSERT(vds != NULL);
6480e1ebb9ecSlm66018 	ASSERT(device_path != NULL);
64811ae08745Sheppo 	ASSERT(vdp != NULL);
6482e1ebb9ecSlm66018 	PR0("Adding vdisk for %s", device_path);
64831ae08745Sheppo 
64841ae08745Sheppo 	if ((vd = kmem_zalloc(sizeof (*vd), KM_NOSLEEP)) == NULL) {
64851ae08745Sheppo 		PRN("No memory for virtual disk");
64861ae08745Sheppo 		return (EAGAIN);
64871ae08745Sheppo 	}
64881ae08745Sheppo 	*vdp = vd;	/* assign here so vds_destroy_vd() can cleanup later */
64891ae08745Sheppo 	vd->vds = vds;
64903c96341aSnarayan 	(void) strncpy(vd->device_path, device_path, MAXPATHLEN);
64911ae08745Sheppo 
6492047ba61eSachartre 	/* Setup open flags */
6493047ba61eSachartre 	vd->open_flags = FREAD;
6494047ba61eSachartre 
6495047ba61eSachartre 	if (!(options & VD_OPT_RDONLY))
6496047ba61eSachartre 		vd->open_flags |= FWRITE;
6497047ba61eSachartre 
6498047ba61eSachartre 	if (options & VD_OPT_EXCLUSIVE)
6499047ba61eSachartre 		vd->open_flags |= FEXCL;
6500047ba61eSachartre 
6501047ba61eSachartre 	/* Setup disk type */
6502047ba61eSachartre 	if (options & VD_OPT_SLICE) {
6503047ba61eSachartre 		vd->vdisk_type = VD_DISK_TYPE_SLICE;
6504047ba61eSachartre 		vd->nslices = 1;
6505047ba61eSachartre 	} else {
6506047ba61eSachartre 		vd->vdisk_type = VD_DISK_TYPE_DISK;
6507047ba61eSachartre 		vd->nslices = V_NUMPAR;
6508047ba61eSachartre 	}
6509047ba61eSachartre 
6510047ba61eSachartre 	/* default disk label */
6511047ba61eSachartre 	vd->vdisk_label = VD_DISK_LABEL_UNK;
6512047ba61eSachartre 
65130a55fbb7Slm66018 	/* Open vdisk and initialize parameters */
65143c96341aSnarayan 	if ((status = vd_setup_vd(vd)) == 0) {
65153c96341aSnarayan 		vd->initialized |= VD_DISK_READY;
65161ae08745Sheppo 
65173c96341aSnarayan 		ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
65188fce2fd6Sachartre 		PR0("vdisk_type = %s, volume = %s, file = %s, nslices = %u",
65193c96341aSnarayan 		    ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
65208fce2fd6Sachartre 		    (vd->volume ? "yes" : "no"), (vd->file ? "yes" : "no"),
65213c96341aSnarayan 		    vd->nslices);
65223c96341aSnarayan 	} else {
65233c96341aSnarayan 		if (status != EAGAIN)
65243c96341aSnarayan 			return (status);
65253c96341aSnarayan 	}
65261ae08745Sheppo 
65271ae08745Sheppo 	/* Initialize locking */
65281ae08745Sheppo 	if (ddi_get_soft_iblock_cookie(vds->dip, DDI_SOFTINT_MED,
65291ae08745Sheppo 	    &iblock) != DDI_SUCCESS) {
65301ae08745Sheppo 		PRN("Could not get iblock cookie.");
65311ae08745Sheppo 		return (EIO);
65321ae08745Sheppo 	}
65331ae08745Sheppo 
65341ae08745Sheppo 	mutex_init(&vd->lock, NULL, MUTEX_DRIVER, iblock);
65351ae08745Sheppo 	vd->initialized |= VD_LOCKING;
65361ae08745Sheppo 
65371ae08745Sheppo 
6538d10e4ef2Snarayan 	/* Create start and completion task queues for the vdisk */
6539d10e4ef2Snarayan 	(void) snprintf(tq_name, sizeof (tq_name), "vd_startq%lu", id);
65401ae08745Sheppo 	PR1("tq_name = %s", tq_name);
6541d10e4ef2Snarayan 	if ((vd->startq = ddi_taskq_create(vds->dip, tq_name, 1,
65421ae08745Sheppo 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
65431ae08745Sheppo 		PRN("Could not create task queue");
65441ae08745Sheppo 		return (EIO);
65451ae08745Sheppo 	}
6546d10e4ef2Snarayan 	(void) snprintf(tq_name, sizeof (tq_name), "vd_completionq%lu", id);
6547d10e4ef2Snarayan 	PR1("tq_name = %s", tq_name);
6548d10e4ef2Snarayan 	if ((vd->completionq = ddi_taskq_create(vds->dip, tq_name, 1,
6549d10e4ef2Snarayan 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
6550d10e4ef2Snarayan 		PRN("Could not create task queue");
6551d10e4ef2Snarayan 		return (EIO);
6552d10e4ef2Snarayan 	}
65535b98b509Sachartre 
65545b98b509Sachartre 	/* Allocate the staging buffer */
65555b98b509Sachartre 	vd->max_msglen = sizeof (vio_msg_t);	/* baseline vio message size */
65565b98b509Sachartre 	vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
65575b98b509Sachartre 
6558d10e4ef2Snarayan 	vd->enabled = 1;	/* before callback can dispatch to startq */
65591ae08745Sheppo 
65601ae08745Sheppo 
65611ae08745Sheppo 	/* Bring up LDC */
65621ae08745Sheppo 	ldc_attr.devclass	= LDC_DEV_BLK_SVC;
65631ae08745Sheppo 	ldc_attr.instance	= ddi_get_instance(vds->dip);
65641ae08745Sheppo 	ldc_attr.mode		= LDC_MODE_UNRELIABLE;
6565e1ebb9ecSlm66018 	ldc_attr.mtu		= VD_LDC_MTU;
65661ae08745Sheppo 	if ((status = ldc_init(ldc_id, &ldc_attr, &vd->ldc_handle)) != 0) {
656717cadca8Slm66018 		PRN("Could not initialize LDC channel %lx, "
6568690555a1Sachartre 		    "init failed with error %d", ldc_id, status);
65691ae08745Sheppo 		return (status);
65701ae08745Sheppo 	}
65711ae08745Sheppo 	vd->initialized |= VD_LDC;
65721ae08745Sheppo 
65731ae08745Sheppo 	if ((status = ldc_reg_callback(vd->ldc_handle, vd_handle_ldc_events,
65741ae08745Sheppo 	    (caddr_t)vd)) != 0) {
6575690555a1Sachartre 		PRN("Could not initialize LDC channel %lu,"
6576690555a1Sachartre 		    "reg_callback failed with error %d", ldc_id, status);
65771ae08745Sheppo 		return (status);
65781ae08745Sheppo 	}
65791ae08745Sheppo 
65801ae08745Sheppo 	if ((status = ldc_open(vd->ldc_handle)) != 0) {
6581690555a1Sachartre 		PRN("Could not initialize LDC channel %lu,"
6582690555a1Sachartre 		    "open failed with error %d", ldc_id, status);
65831ae08745Sheppo 		return (status);
65841ae08745Sheppo 	}
65851ae08745Sheppo 
65863af08d82Slm66018 	if ((status = ldc_up(vd->ldc_handle)) != 0) {
658734683adeSsg70180 		PR0("ldc_up() returned errno %d", status);
65883af08d82Slm66018 	}
65893af08d82Slm66018 
65904bac2208Snarayan 	/* Allocate the inband task memory handle */
65914bac2208Snarayan 	status = ldc_mem_alloc_handle(vd->ldc_handle, &(vd->inband_task.mhdl));
65924bac2208Snarayan 	if (status) {
6593690555a1Sachartre 		PRN("Could not initialize LDC channel %lu,"
6594690555a1Sachartre 		    "alloc_handle failed with error %d", ldc_id, status);
65954bac2208Snarayan 		return (ENXIO);
65964bac2208Snarayan 	}
65971ae08745Sheppo 
65981ae08745Sheppo 	/* Add the successfully-initialized vdisk to the server's table */
65991ae08745Sheppo 	if (mod_hash_insert(vds->vd_table, (mod_hash_key_t)id, vd) != 0) {
66001ae08745Sheppo 		PRN("Error adding vdisk ID %lu to table", id);
66011ae08745Sheppo 		return (EIO);
66021ae08745Sheppo 	}
66031ae08745Sheppo 
66043af08d82Slm66018 	/* store initial state */
66053af08d82Slm66018 	vd->state = VD_STATE_INIT;
66063af08d82Slm66018 
66071ae08745Sheppo 	return (0);
66081ae08745Sheppo }
66091ae08745Sheppo 
66103af08d82Slm66018 static void
66113af08d82Slm66018 vd_free_dring_task(vd_t *vdp)
66123af08d82Slm66018 {
66133af08d82Slm66018 	if (vdp->dring_task != NULL) {
66143af08d82Slm66018 		ASSERT(vdp->dring_len != 0);
66153af08d82Slm66018 		/* Free all dring_task memory handles */
66163af08d82Slm66018 		for (int i = 0; i < vdp->dring_len; i++) {
66173af08d82Slm66018 			(void) ldc_mem_free_handle(vdp->dring_task[i].mhdl);
66185b7cb889Sha137994 			kmem_free(vdp->dring_task[i].request,
66195b7cb889Sha137994 			    (vdp->descriptor_size -
66205b7cb889Sha137994 			    sizeof (vio_dring_entry_hdr_t)));
66215b7cb889Sha137994 			vdp->dring_task[i].request = NULL;
66223af08d82Slm66018 			kmem_free(vdp->dring_task[i].msg, vdp->max_msglen);
66233af08d82Slm66018 			vdp->dring_task[i].msg = NULL;
66243af08d82Slm66018 		}
66253af08d82Slm66018 		kmem_free(vdp->dring_task,
66263af08d82Slm66018 		    (sizeof (*vdp->dring_task)) * vdp->dring_len);
66273af08d82Slm66018 		vdp->dring_task = NULL;
66283af08d82Slm66018 	}
66293af08d82Slm66018 }
66303af08d82Slm66018 
66311ae08745Sheppo /*
66321ae08745Sheppo  * Destroy the state associated with a virtual disk
66331ae08745Sheppo  */
66341ae08745Sheppo static void
66351ae08745Sheppo vds_destroy_vd(void *arg)
66361ae08745Sheppo {
66371ae08745Sheppo 	vd_t	*vd = (vd_t *)arg;
663834683adeSsg70180 	int	retry = 0, rv;
66391ae08745Sheppo 
66401ae08745Sheppo 	if (vd == NULL)
66411ae08745Sheppo 		return;
66421ae08745Sheppo 
6643d10e4ef2Snarayan 	PR0("Destroying vdisk state");
6644d10e4ef2Snarayan 
66451ae08745Sheppo 	/* Disable queuing requests for the vdisk */
66461ae08745Sheppo 	if (vd->initialized & VD_LOCKING) {
66471ae08745Sheppo 		mutex_enter(&vd->lock);
66481ae08745Sheppo 		vd->enabled = 0;
66491ae08745Sheppo 		mutex_exit(&vd->lock);
66501ae08745Sheppo 	}
66511ae08745Sheppo 
6652d10e4ef2Snarayan 	/* Drain and destroy start queue (*before* destroying completionq) */
6653d10e4ef2Snarayan 	if (vd->startq != NULL)
6654d10e4ef2Snarayan 		ddi_taskq_destroy(vd->startq);	/* waits for queued tasks */
6655d10e4ef2Snarayan 
6656d10e4ef2Snarayan 	/* Drain and destroy completion queue (*before* shutting down LDC) */
6657d10e4ef2Snarayan 	if (vd->completionq != NULL)
6658d10e4ef2Snarayan 		ddi_taskq_destroy(vd->completionq);	/* waits for tasks */
6659d10e4ef2Snarayan 
66603af08d82Slm66018 	vd_free_dring_task(vd);
66613af08d82Slm66018 
666234683adeSsg70180 	/* Free the inband task memory handle */
666334683adeSsg70180 	(void) ldc_mem_free_handle(vd->inband_task.mhdl);
666434683adeSsg70180 
666534683adeSsg70180 	/* Shut down LDC */
666634683adeSsg70180 	if (vd->initialized & VD_LDC) {
666734683adeSsg70180 		/* unmap the dring */
666834683adeSsg70180 		if (vd->initialized & VD_DRING)
666934683adeSsg70180 			(void) ldc_mem_dring_unmap(vd->dring_handle);
667034683adeSsg70180 
667134683adeSsg70180 		/* close LDC channel - retry on EAGAIN */
667234683adeSsg70180 		while ((rv = ldc_close(vd->ldc_handle)) == EAGAIN) {
667334683adeSsg70180 			if (++retry > vds_ldc_retries) {
667434683adeSsg70180 				PR0("Timed out closing channel");
667534683adeSsg70180 				break;
667634683adeSsg70180 			}
667734683adeSsg70180 			drv_usecwait(vds_ldc_delay);
667834683adeSsg70180 		}
667934683adeSsg70180 		if (rv == 0) {
668034683adeSsg70180 			(void) ldc_unreg_callback(vd->ldc_handle);
668134683adeSsg70180 			(void) ldc_fini(vd->ldc_handle);
668234683adeSsg70180 		} else {
668334683adeSsg70180 			/*
668434683adeSsg70180 			 * Closing the LDC channel has failed. Ideally we should
668534683adeSsg70180 			 * fail here but there is no Zeus level infrastructure
668634683adeSsg70180 			 * to handle this. The MD has already been changed and
668734683adeSsg70180 			 * we have to do the close. So we try to do as much
668834683adeSsg70180 			 * clean up as we can.
668934683adeSsg70180 			 */
669034683adeSsg70180 			(void) ldc_set_cb_mode(vd->ldc_handle, LDC_CB_DISABLE);
669134683adeSsg70180 			while (ldc_unreg_callback(vd->ldc_handle) == EAGAIN)
669234683adeSsg70180 				drv_usecwait(vds_ldc_delay);
669334683adeSsg70180 		}
669434683adeSsg70180 	}
669534683adeSsg70180 
66963af08d82Slm66018 	/* Free the staging buffer for msgs */
66973af08d82Slm66018 	if (vd->vio_msgp != NULL) {
66983af08d82Slm66018 		kmem_free(vd->vio_msgp, vd->max_msglen);
66993af08d82Slm66018 		vd->vio_msgp = NULL;
67003af08d82Slm66018 	}
67013af08d82Slm66018 
67023af08d82Slm66018 	/* Free the inband message buffer */
67033af08d82Slm66018 	if (vd->inband_task.msg != NULL) {
67043af08d82Slm66018 		kmem_free(vd->inband_task.msg, vd->max_msglen);
67053af08d82Slm66018 		vd->inband_task.msg = NULL;
6706d10e4ef2Snarayan 	}
6707da6c28aaSamw 
67083c96341aSnarayan 	if (vd->file) {
6709690555a1Sachartre 		/* Close file */
6710047ba61eSachartre 		(void) VOP_CLOSE(vd->file_vnode, vd->open_flags, 1,
6711da6c28aaSamw 		    0, kcred, NULL);
67123c96341aSnarayan 		VN_RELE(vd->file_vnode);
67133c96341aSnarayan 	} else {
67141ae08745Sheppo 		/* Close any open backing-device slices */
6715bae9e67eSachartre 		for (uint_t slice = 0; slice < V_NUMPAR; slice++) {
67161ae08745Sheppo 			if (vd->ldi_handle[slice] != NULL) {
67171ae08745Sheppo 				PR0("Closing slice %u", slice);
67181ae08745Sheppo 				(void) ldi_close(vd->ldi_handle[slice],
6719047ba61eSachartre 				    vd->open_flags, kcred);
67201ae08745Sheppo 			}
67211ae08745Sheppo 		}
67223c96341aSnarayan 	}
67231ae08745Sheppo 
6724*1aff8f07SAlexandre Chartre 	/* Free disk image devid */
6725*1aff8f07SAlexandre Chartre 	if (vd->dskimg_devid != NULL)
6726*1aff8f07SAlexandre Chartre 		ddi_devid_free(vd->dskimg_devid);
6727*1aff8f07SAlexandre Chartre 
6728bae9e67eSachartre 	/* Free any fake label */
6729bae9e67eSachartre 	if (vd->flabel) {
6730bae9e67eSachartre 		kmem_free(vd->flabel, vd->flabel_size);
6731bae9e67eSachartre 		vd->flabel = NULL;
6732bae9e67eSachartre 		vd->flabel_size = 0;
6733bae9e67eSachartre 	}
6734bae9e67eSachartre 
67351ae08745Sheppo 	/* Free lock */
67361ae08745Sheppo 	if (vd->initialized & VD_LOCKING)
67371ae08745Sheppo 		mutex_destroy(&vd->lock);
67381ae08745Sheppo 
67391ae08745Sheppo 	/* Finally, free the vdisk structure itself */
67401ae08745Sheppo 	kmem_free(vd, sizeof (*vd));
67411ae08745Sheppo }
67421ae08745Sheppo 
67431ae08745Sheppo static int
6744047ba61eSachartre vds_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options,
6745047ba61eSachartre     uint64_t ldc_id)
67461ae08745Sheppo {
67471ae08745Sheppo 	int	status;
67481ae08745Sheppo 	vd_t	*vd = NULL;
67491ae08745Sheppo 
67501ae08745Sheppo 
6751047ba61eSachartre 	if ((status = vds_do_init_vd(vds, id, device_path, options,
6752047ba61eSachartre 	    ldc_id, &vd)) != 0)
67531ae08745Sheppo 		vds_destroy_vd(vd);
67541ae08745Sheppo 
67551ae08745Sheppo 	return (status);
67561ae08745Sheppo }
67571ae08745Sheppo 
67581ae08745Sheppo static int
67591ae08745Sheppo vds_do_get_ldc_id(md_t *md, mde_cookie_t vd_node, mde_cookie_t *channel,
67601ae08745Sheppo     uint64_t *ldc_id)
67611ae08745Sheppo {
67621ae08745Sheppo 	int	num_channels;
67631ae08745Sheppo 
67641ae08745Sheppo 
67651ae08745Sheppo 	/* Look for channel endpoint child(ren) of the vdisk MD node */
67661ae08745Sheppo 	if ((num_channels = md_scan_dag(md, vd_node,
67671ae08745Sheppo 	    md_find_name(md, VD_CHANNEL_ENDPOINT),
67681ae08745Sheppo 	    md_find_name(md, "fwd"), channel)) <= 0) {
67691ae08745Sheppo 		PRN("No \"%s\" found for virtual disk", VD_CHANNEL_ENDPOINT);
67701ae08745Sheppo 		return (-1);
67711ae08745Sheppo 	}
67721ae08745Sheppo 
67731ae08745Sheppo 	/* Get the "id" value for the first channel endpoint node */
67741ae08745Sheppo 	if (md_get_prop_val(md, channel[0], VD_ID_PROP, ldc_id) != 0) {
67751ae08745Sheppo 		PRN("No \"%s\" property found for \"%s\" of vdisk",
67761ae08745Sheppo 		    VD_ID_PROP, VD_CHANNEL_ENDPOINT);
67771ae08745Sheppo 		return (-1);
67781ae08745Sheppo 	}
67791ae08745Sheppo 
67801ae08745Sheppo 	if (num_channels > 1) {
67811ae08745Sheppo 		PRN("Using ID of first of multiple channels for this vdisk");
67821ae08745Sheppo 	}
67831ae08745Sheppo 
67841ae08745Sheppo 	return (0);
67851ae08745Sheppo }
67861ae08745Sheppo 
67871ae08745Sheppo static int
67881ae08745Sheppo vds_get_ldc_id(md_t *md, mde_cookie_t vd_node, uint64_t *ldc_id)
67891ae08745Sheppo {
67901ae08745Sheppo 	int		num_nodes, status;
67911ae08745Sheppo 	size_t		size;
67921ae08745Sheppo 	mde_cookie_t	*channel;
67931ae08745Sheppo 
67941ae08745Sheppo 
67951ae08745Sheppo 	if ((num_nodes = md_node_count(md)) <= 0) {
67961ae08745Sheppo 		PRN("Invalid node count in Machine Description subtree");
67971ae08745Sheppo 		return (-1);
67981ae08745Sheppo 	}
67991ae08745Sheppo 	size = num_nodes*(sizeof (*channel));
68001ae08745Sheppo 	channel = kmem_zalloc(size, KM_SLEEP);
68011ae08745Sheppo 	status = vds_do_get_ldc_id(md, vd_node, channel, ldc_id);
68021ae08745Sheppo 	kmem_free(channel, size);
68031ae08745Sheppo 
68041ae08745Sheppo 	return (status);
68051ae08745Sheppo }
68061ae08745Sheppo 
6807047ba61eSachartre /*
6808047ba61eSachartre  * Function:
6809047ba61eSachartre  *	vds_get_options
6810047ba61eSachartre  *
6811047ba61eSachartre  * Description:
6812047ba61eSachartre  * 	Parse the options of a vds node. Options are defined as an array
6813047ba61eSachartre  *	of strings in the vds-block-device-opts property of the vds node
6814047ba61eSachartre  *	in the machine description. Options are returned as a bitmask. The
6815047ba61eSachartre  *	mapping between the bitmask options and the options strings from the
6816047ba61eSachartre  *	machine description is defined in the vd_bdev_options[] array.
6817047ba61eSachartre  *
6818047ba61eSachartre  *	The vds-block-device-opts property is optional. If a vds has no such
6819047ba61eSachartre  *	property then no option is defined.
6820047ba61eSachartre  *
6821047ba61eSachartre  * Parameters:
6822047ba61eSachartre  *	md		- machine description.
6823047ba61eSachartre  *	vd_node		- vds node in the machine description for which
6824047ba61eSachartre  *			  options have to be parsed.
6825047ba61eSachartre  *	options		- the returned options.
6826047ba61eSachartre  *
6827047ba61eSachartre  * Return Code:
6828047ba61eSachartre  *	none.
6829047ba61eSachartre  */
6830047ba61eSachartre static void
6831047ba61eSachartre vds_get_options(md_t *md, mde_cookie_t vd_node, uint64_t *options)
6832047ba61eSachartre {
6833047ba61eSachartre 	char	*optstr, *opt;
6834047ba61eSachartre 	int	len, n, i;
6835047ba61eSachartre 
6836047ba61eSachartre 	*options = 0;
6837047ba61eSachartre 
6838047ba61eSachartre 	if (md_get_prop_data(md, vd_node, VD_BLOCK_DEVICE_OPTS,
6839047ba61eSachartre 	    (uint8_t **)&optstr, &len) != 0) {
6840047ba61eSachartre 		PR0("No options found");
6841047ba61eSachartre 		return;
6842047ba61eSachartre 	}
6843047ba61eSachartre 
6844047ba61eSachartre 	/* parse options */
6845047ba61eSachartre 	opt = optstr;
6846047ba61eSachartre 	n = sizeof (vd_bdev_options) / sizeof (vd_option_t);
6847047ba61eSachartre 
6848047ba61eSachartre 	while (opt < optstr + len) {
6849047ba61eSachartre 		for (i = 0; i < n; i++) {
6850047ba61eSachartre 			if (strncmp(vd_bdev_options[i].vdo_name,
6851047ba61eSachartre 			    opt, VD_OPTION_NLEN) == 0) {
6852047ba61eSachartre 				*options |= vd_bdev_options[i].vdo_value;
6853047ba61eSachartre 				break;
6854047ba61eSachartre 			}
6855047ba61eSachartre 		}
6856047ba61eSachartre 
6857047ba61eSachartre 		if (i < n) {
6858047ba61eSachartre 			PR0("option: %s", opt);
6859047ba61eSachartre 		} else {
6860047ba61eSachartre 			PRN("option %s is unknown or unsupported", opt);
6861047ba61eSachartre 		}
6862047ba61eSachartre 
6863047ba61eSachartre 		opt += strlen(opt) + 1;
6864047ba61eSachartre 	}
6865047ba61eSachartre }
6866047ba61eSachartre 
68671ae08745Sheppo static void
68688fce2fd6Sachartre vds_driver_types_free(vds_t *vds)
68698fce2fd6Sachartre {
68708fce2fd6Sachartre 	if (vds->driver_types != NULL) {
68718fce2fd6Sachartre 		kmem_free(vds->driver_types, sizeof (vd_driver_type_t) *
68728fce2fd6Sachartre 		    vds->num_drivers);
68738fce2fd6Sachartre 		vds->driver_types = NULL;
68748fce2fd6Sachartre 		vds->num_drivers = 0;
68758fce2fd6Sachartre 	}
68768fce2fd6Sachartre }
68778fce2fd6Sachartre 
68788fce2fd6Sachartre /*
68798fce2fd6Sachartre  * Update the driver type list with information from vds.conf.
68808fce2fd6Sachartre  */
68818fce2fd6Sachartre static void
68828fce2fd6Sachartre vds_driver_types_update(vds_t *vds)
68838fce2fd6Sachartre {
68848fce2fd6Sachartre 	char **list, *s;
68858fce2fd6Sachartre 	uint_t i, num, count = 0, len;
68868fce2fd6Sachartre 
68878fce2fd6Sachartre 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, vds->dip,
68888fce2fd6Sachartre 	    DDI_PROP_DONTPASS, "driver-type-list", &list, &num) !=
68898fce2fd6Sachartre 	    DDI_PROP_SUCCESS)
68908fce2fd6Sachartre 		return;
68918fce2fd6Sachartre 
68928fce2fd6Sachartre 	/*
68938fce2fd6Sachartre 	 * We create a driver_types list with as many as entries as there
68948fce2fd6Sachartre 	 * is in the driver-type-list from vds.conf. However only valid
68958fce2fd6Sachartre 	 * entries will be populated (i.e. entries from driver-type-list
68968fce2fd6Sachartre 	 * with a valid syntax). Invalid entries will be left blank so
68978fce2fd6Sachartre 	 * they will have no driver name and the driver type will be
68988fce2fd6Sachartre 	 * VD_DRIVER_UNKNOWN (= 0).
68998fce2fd6Sachartre 	 */
69008fce2fd6Sachartre 	vds->num_drivers = num;
69018fce2fd6Sachartre 	vds->driver_types = kmem_zalloc(sizeof (vd_driver_type_t) * num,
69028fce2fd6Sachartre 	    KM_SLEEP);
69038fce2fd6Sachartre 
69048fce2fd6Sachartre 	for (i = 0; i < num; i++) {
69058fce2fd6Sachartre 
69068fce2fd6Sachartre 		s = strchr(list[i], ':');
69078fce2fd6Sachartre 
69088fce2fd6Sachartre 		if (s == NULL) {
69098fce2fd6Sachartre 			PRN("vds.conf: driver-type-list, entry %d (%s): "
69108fce2fd6Sachartre 			    "a colon is expected in the entry",
69118fce2fd6Sachartre 			    i, list[i]);
69128fce2fd6Sachartre 			continue;
69138fce2fd6Sachartre 		}
69148fce2fd6Sachartre 
69158fce2fd6Sachartre 		len = (uintptr_t)s - (uintptr_t)list[i];
69168fce2fd6Sachartre 
69178fce2fd6Sachartre 		if (len == 0) {
69188fce2fd6Sachartre 			PRN("vds.conf: driver-type-list, entry %d (%s): "
69198fce2fd6Sachartre 			    "the driver name is empty",
69208fce2fd6Sachartre 			    i, list[i]);
69218fce2fd6Sachartre 			continue;
69228fce2fd6Sachartre 		}
69238fce2fd6Sachartre 
69248fce2fd6Sachartre 		if (len >= VD_DRIVER_NAME_LEN) {
69258fce2fd6Sachartre 			PRN("vds.conf: driver-type-list, entry %d (%s): "
69268fce2fd6Sachartre 			    "the driver name is too long",
69278fce2fd6Sachartre 			    i, list[i]);
69288fce2fd6Sachartre 			continue;
69298fce2fd6Sachartre 		}
69308fce2fd6Sachartre 
69318fce2fd6Sachartre 		if (strcmp(s + 1, "disk") == 0) {
69328fce2fd6Sachartre 
69338fce2fd6Sachartre 			vds->driver_types[i].type = VD_DRIVER_DISK;
69348fce2fd6Sachartre 
69358fce2fd6Sachartre 		} else if (strcmp(s + 1, "volume") == 0) {
69368fce2fd6Sachartre 
69378fce2fd6Sachartre 			vds->driver_types[i].type = VD_DRIVER_VOLUME;
69388fce2fd6Sachartre 
69398fce2fd6Sachartre 		} else {
69408fce2fd6Sachartre 			PRN("vds.conf: driver-type-list, entry %d (%s): "
69418fce2fd6Sachartre 			    "the driver type is invalid",
69428fce2fd6Sachartre 			    i, list[i]);
69438fce2fd6Sachartre 			continue;
69448fce2fd6Sachartre 		}
69458fce2fd6Sachartre 
69468fce2fd6Sachartre 		(void) strncpy(vds->driver_types[i].name, list[i], len);
69478fce2fd6Sachartre 
69488fce2fd6Sachartre 		PR0("driver-type-list, entry %d (%s) added",
69498fce2fd6Sachartre 		    i, list[i]);
69508fce2fd6Sachartre 
69518fce2fd6Sachartre 		count++;
69528fce2fd6Sachartre 	}
69538fce2fd6Sachartre 
69548fce2fd6Sachartre 	ddi_prop_free(list);
69558fce2fd6Sachartre 
69568fce2fd6Sachartre 	if (count == 0) {
69578fce2fd6Sachartre 		/* nothing was added, clean up */
69588fce2fd6Sachartre 		vds_driver_types_free(vds);
69598fce2fd6Sachartre 	}
69608fce2fd6Sachartre }
69618fce2fd6Sachartre 
69628fce2fd6Sachartre static void
69631ae08745Sheppo vds_add_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
69641ae08745Sheppo {
6965e1ebb9ecSlm66018 	char		*device_path = NULL;
6966047ba61eSachartre 	uint64_t	id = 0, ldc_id = 0, options = 0;
69671ae08745Sheppo 
69681ae08745Sheppo 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
69691ae08745Sheppo 		PRN("Error getting vdisk \"%s\"", VD_ID_PROP);
69701ae08745Sheppo 		return;
69711ae08745Sheppo 	}
69721ae08745Sheppo 	PR0("Adding vdisk ID %lu", id);
69731ae08745Sheppo 	if (md_get_prop_str(md, vd_node, VD_BLOCK_DEVICE_PROP,
6974e1ebb9ecSlm66018 	    &device_path) != 0) {
69751ae08745Sheppo 		PRN("Error getting vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
69761ae08745Sheppo 		return;
69771ae08745Sheppo 	}
69781ae08745Sheppo 
6979047ba61eSachartre 	vds_get_options(md, vd_node, &options);
6980047ba61eSachartre 
69811ae08745Sheppo 	if (vds_get_ldc_id(md, vd_node, &ldc_id) != 0) {
69821ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", id);
69831ae08745Sheppo 		return;
69841ae08745Sheppo 	}
69851ae08745Sheppo 
6986047ba61eSachartre 	if (vds_init_vd(vds, id, device_path, options, ldc_id) != 0) {
69871ae08745Sheppo 		PRN("Failed to add vdisk ID %lu", id);
698817cadca8Slm66018 		if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0)
698917cadca8Slm66018 			PRN("No vDisk entry found for vdisk ID %lu", id);
69901ae08745Sheppo 		return;
69911ae08745Sheppo 	}
69921ae08745Sheppo }
69931ae08745Sheppo 
69941ae08745Sheppo static void
69951ae08745Sheppo vds_remove_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
69961ae08745Sheppo {
69971ae08745Sheppo 	uint64_t	id = 0;
69981ae08745Sheppo 
69991ae08745Sheppo 
70001ae08745Sheppo 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
70011ae08745Sheppo 		PRN("Unable to get \"%s\" property from vdisk's MD node",
70021ae08745Sheppo 		    VD_ID_PROP);
70031ae08745Sheppo 		return;
70041ae08745Sheppo 	}
70051ae08745Sheppo 	PR0("Removing vdisk ID %lu", id);
70061ae08745Sheppo 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0)
70071ae08745Sheppo 		PRN("No vdisk entry found for vdisk ID %lu", id);
70081ae08745Sheppo }
70091ae08745Sheppo 
70101ae08745Sheppo static void
70111ae08745Sheppo vds_change_vd(vds_t *vds, md_t *prev_md, mde_cookie_t prev_vd_node,
70121ae08745Sheppo     md_t *curr_md, mde_cookie_t curr_vd_node)
70131ae08745Sheppo {
70141ae08745Sheppo 	char		*curr_dev, *prev_dev;
7015047ba61eSachartre 	uint64_t	curr_id = 0, curr_ldc_id = 0, curr_options = 0;
7016047ba61eSachartre 	uint64_t	prev_id = 0, prev_ldc_id = 0, prev_options = 0;
70171ae08745Sheppo 	size_t		len;
70181ae08745Sheppo 
70191ae08745Sheppo 
70201ae08745Sheppo 	/* Validate that vdisk ID has not changed */
70211ae08745Sheppo 	if (md_get_prop_val(prev_md, prev_vd_node, VD_ID_PROP, &prev_id) != 0) {
70221ae08745Sheppo 		PRN("Error getting previous vdisk \"%s\" property",
70231ae08745Sheppo 		    VD_ID_PROP);
70241ae08745Sheppo 		return;
70251ae08745Sheppo 	}
70261ae08745Sheppo 	if (md_get_prop_val(curr_md, curr_vd_node, VD_ID_PROP, &curr_id) != 0) {
70271ae08745Sheppo 		PRN("Error getting current vdisk \"%s\" property", VD_ID_PROP);
70281ae08745Sheppo 		return;
70291ae08745Sheppo 	}
70301ae08745Sheppo 	if (curr_id != prev_id) {
70311ae08745Sheppo 		PRN("Not changing vdisk:  ID changed from %lu to %lu",
70321ae08745Sheppo 		    prev_id, curr_id);
70331ae08745Sheppo 		return;
70341ae08745Sheppo 	}
70351ae08745Sheppo 
70361ae08745Sheppo 	/* Validate that LDC ID has not changed */
70371ae08745Sheppo 	if (vds_get_ldc_id(prev_md, prev_vd_node, &prev_ldc_id) != 0) {
70381ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", prev_id);
70391ae08745Sheppo 		return;
70401ae08745Sheppo 	}
70411ae08745Sheppo 
70421ae08745Sheppo 	if (vds_get_ldc_id(curr_md, curr_vd_node, &curr_ldc_id) != 0) {
70431ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", curr_id);
70441ae08745Sheppo 		return;
70451ae08745Sheppo 	}
70461ae08745Sheppo 	if (curr_ldc_id != prev_ldc_id) {
70470a55fbb7Slm66018 		_NOTE(NOTREACHED);	/* lint is confused */
70481ae08745Sheppo 		PRN("Not changing vdisk:  "
70491ae08745Sheppo 		    "LDC ID changed from %lu to %lu", prev_ldc_id, curr_ldc_id);
70501ae08745Sheppo 		return;
70511ae08745Sheppo 	}
70521ae08745Sheppo 
70531ae08745Sheppo 	/* Determine whether device path has changed */
70541ae08745Sheppo 	if (md_get_prop_str(prev_md, prev_vd_node, VD_BLOCK_DEVICE_PROP,
70551ae08745Sheppo 	    &prev_dev) != 0) {
70561ae08745Sheppo 		PRN("Error getting previous vdisk \"%s\"",
70571ae08745Sheppo 		    VD_BLOCK_DEVICE_PROP);
70581ae08745Sheppo 		return;
70591ae08745Sheppo 	}
70601ae08745Sheppo 	if (md_get_prop_str(curr_md, curr_vd_node, VD_BLOCK_DEVICE_PROP,
70611ae08745Sheppo 	    &curr_dev) != 0) {
70621ae08745Sheppo 		PRN("Error getting current vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
70631ae08745Sheppo 		return;
70641ae08745Sheppo 	}
70651ae08745Sheppo 	if (((len = strlen(curr_dev)) == strlen(prev_dev)) &&
70661ae08745Sheppo 	    (strncmp(curr_dev, prev_dev, len) == 0))
70671ae08745Sheppo 		return;	/* no relevant (supported) change */
70681ae08745Sheppo 
7069047ba61eSachartre 	/* Validate that options have not changed */
7070047ba61eSachartre 	vds_get_options(prev_md, prev_vd_node, &prev_options);
7071047ba61eSachartre 	vds_get_options(curr_md, curr_vd_node, &curr_options);
7072047ba61eSachartre 	if (prev_options != curr_options) {
7073047ba61eSachartre 		PRN("Not changing vdisk:  options changed from %lx to %lx",
7074047ba61eSachartre 		    prev_options, curr_options);
7075047ba61eSachartre 		return;
7076047ba61eSachartre 	}
7077047ba61eSachartre 
70781ae08745Sheppo 	PR0("Changing vdisk ID %lu", prev_id);
70793af08d82Slm66018 
70801ae08745Sheppo 	/* Remove old state, which will close vdisk and reset */
70811ae08745Sheppo 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)prev_id) != 0)
70821ae08745Sheppo 		PRN("No entry found for vdisk ID %lu", prev_id);
70833af08d82Slm66018 
70841ae08745Sheppo 	/* Re-initialize vdisk with new state */
7085047ba61eSachartre 	if (vds_init_vd(vds, curr_id, curr_dev, curr_options,
7086047ba61eSachartre 	    curr_ldc_id) != 0) {
70871ae08745Sheppo 		PRN("Failed to change vdisk ID %lu", curr_id);
70881ae08745Sheppo 		return;
70891ae08745Sheppo 	}
70901ae08745Sheppo }
70911ae08745Sheppo 
70921ae08745Sheppo static int
70931ae08745Sheppo vds_process_md(void *arg, mdeg_result_t *md)
70941ae08745Sheppo {
70951ae08745Sheppo 	int	i;
70961ae08745Sheppo 	vds_t	*vds = arg;
70971ae08745Sheppo 
70981ae08745Sheppo 
70991ae08745Sheppo 	if (md == NULL)
71001ae08745Sheppo 		return (MDEG_FAILURE);
71011ae08745Sheppo 	ASSERT(vds != NULL);
71021ae08745Sheppo 
71031ae08745Sheppo 	for (i = 0; i < md->removed.nelem; i++)
71041ae08745Sheppo 		vds_remove_vd(vds, md->removed.mdp, md->removed.mdep[i]);
71051ae08745Sheppo 	for (i = 0; i < md->match_curr.nelem; i++)
71061ae08745Sheppo 		vds_change_vd(vds, md->match_prev.mdp, md->match_prev.mdep[i],
71071ae08745Sheppo 		    md->match_curr.mdp, md->match_curr.mdep[i]);
71081ae08745Sheppo 	for (i = 0; i < md->added.nelem; i++)
71091ae08745Sheppo 		vds_add_vd(vds, md->added.mdp, md->added.mdep[i]);
71101ae08745Sheppo 
71111ae08745Sheppo 	return (MDEG_SUCCESS);
71121ae08745Sheppo }
71131ae08745Sheppo 
71143c96341aSnarayan 
71151ae08745Sheppo static int
71161ae08745Sheppo vds_do_attach(dev_info_t *dip)
71171ae08745Sheppo {
7118445b4c2eSsb155480 	int			status, sz;
7119445b4c2eSsb155480 	int			cfg_handle;
71201ae08745Sheppo 	minor_t			instance = ddi_get_instance(dip);
71211ae08745Sheppo 	vds_t			*vds;
7122445b4c2eSsb155480 	mdeg_prop_spec_t	*pspecp;
7123445b4c2eSsb155480 	mdeg_node_spec_t	*ispecp;
71241ae08745Sheppo 
71251ae08745Sheppo 	/*
71261ae08745Sheppo 	 * The "cfg-handle" property of a vds node in an MD contains the MD's
71271ae08745Sheppo 	 * notion of "instance", or unique identifier, for that node; OBP
71281ae08745Sheppo 	 * stores the value of the "cfg-handle" MD property as the value of
71291ae08745Sheppo 	 * the "reg" property on the node in the device tree it builds from
71301ae08745Sheppo 	 * the MD and passes to Solaris.  Thus, we look up the devinfo node's
71311ae08745Sheppo 	 * "reg" property value to uniquely identify this device instance when
71321ae08745Sheppo 	 * registering with the MD event-generation framework.  If the "reg"
71331ae08745Sheppo 	 * property cannot be found, the device tree state is presumably so
71341ae08745Sheppo 	 * broken that there is no point in continuing.
71351ae08745Sheppo 	 */
7136445b4c2eSsb155480 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
7137445b4c2eSsb155480 	    VD_REG_PROP)) {
7138445b4c2eSsb155480 		PRN("vds \"%s\" property does not exist", VD_REG_PROP);
71391ae08745Sheppo 		return (DDI_FAILURE);
71401ae08745Sheppo 	}
71411ae08745Sheppo 
71421ae08745Sheppo 	/* Get the MD instance for later MDEG registration */
71431ae08745Sheppo 	cfg_handle = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
7144445b4c2eSsb155480 	    VD_REG_PROP, -1);
71451ae08745Sheppo 
71461ae08745Sheppo 	if (ddi_soft_state_zalloc(vds_state, instance) != DDI_SUCCESS) {
71471ae08745Sheppo 		PRN("Could not allocate state for instance %u", instance);
71481ae08745Sheppo 		return (DDI_FAILURE);
71491ae08745Sheppo 	}
71501ae08745Sheppo 
71511ae08745Sheppo 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
71521ae08745Sheppo 		PRN("Could not get state for instance %u", instance);
71531ae08745Sheppo 		ddi_soft_state_free(vds_state, instance);
71541ae08745Sheppo 		return (DDI_FAILURE);
71551ae08745Sheppo 	}
71561ae08745Sheppo 
71571ae08745Sheppo 	vds->dip	= dip;
71581ae08745Sheppo 	vds->vd_table	= mod_hash_create_ptrhash("vds_vd_table", VDS_NCHAINS,
715987a7269eSachartre 	    vds_destroy_vd, sizeof (void *));
716087a7269eSachartre 
71611ae08745Sheppo 	ASSERT(vds->vd_table != NULL);
71621ae08745Sheppo 
71631ae08745Sheppo 	if ((status = ldi_ident_from_dip(dip, &vds->ldi_ident)) != 0) {
71641ae08745Sheppo 		PRN("ldi_ident_from_dip() returned errno %d", status);
71651ae08745Sheppo 		return (DDI_FAILURE);
71661ae08745Sheppo 	}
71671ae08745Sheppo 	vds->initialized |= VDS_LDI;
71681ae08745Sheppo 
71691ae08745Sheppo 	/* Register for MD updates */
7170445b4c2eSsb155480 	sz = sizeof (vds_prop_template);
7171445b4c2eSsb155480 	pspecp = kmem_alloc(sz, KM_SLEEP);
7172445b4c2eSsb155480 	bcopy(vds_prop_template, pspecp, sz);
7173445b4c2eSsb155480 
7174445b4c2eSsb155480 	VDS_SET_MDEG_PROP_INST(pspecp, cfg_handle);
7175445b4c2eSsb155480 
7176445b4c2eSsb155480 	/* initialize the complete prop spec structure */
7177445b4c2eSsb155480 	ispecp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP);
7178445b4c2eSsb155480 	ispecp->namep = "virtual-device";
7179445b4c2eSsb155480 	ispecp->specp = pspecp;
7180445b4c2eSsb155480 
7181445b4c2eSsb155480 	if (mdeg_register(ispecp, &vd_match, vds_process_md, vds,
71821ae08745Sheppo 	    &vds->mdeg) != MDEG_SUCCESS) {
71831ae08745Sheppo 		PRN("Unable to register for MD updates");
7184445b4c2eSsb155480 		kmem_free(ispecp, sizeof (mdeg_node_spec_t));
7185445b4c2eSsb155480 		kmem_free(pspecp, sz);
71861ae08745Sheppo 		return (DDI_FAILURE);
71871ae08745Sheppo 	}
7188445b4c2eSsb155480 
7189445b4c2eSsb155480 	vds->ispecp = ispecp;
71901ae08745Sheppo 	vds->initialized |= VDS_MDEG;
71911ae08745Sheppo 
71920a55fbb7Slm66018 	/* Prevent auto-detaching so driver is available whenever MD changes */
71930a55fbb7Slm66018 	if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1) !=
71940a55fbb7Slm66018 	    DDI_PROP_SUCCESS) {
71950a55fbb7Slm66018 		PRN("failed to set \"%s\" property for instance %u",
71960a55fbb7Slm66018 		    DDI_NO_AUTODETACH, instance);
71970a55fbb7Slm66018 	}
71980a55fbb7Slm66018 
71998fce2fd6Sachartre 	/* read any user defined driver types from conf file and update list */
72008fce2fd6Sachartre 	vds_driver_types_update(vds);
72018fce2fd6Sachartre 
72021ae08745Sheppo 	ddi_report_dev(dip);
72031ae08745Sheppo 	return (DDI_SUCCESS);
72041ae08745Sheppo }
72051ae08745Sheppo 
72061ae08745Sheppo static int
72071ae08745Sheppo vds_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
72081ae08745Sheppo {
72091ae08745Sheppo 	int	status;
72101ae08745Sheppo 
72111ae08745Sheppo 	switch (cmd) {
72121ae08745Sheppo 	case DDI_ATTACH:
7213d10e4ef2Snarayan 		PR0("Attaching");
72141ae08745Sheppo 		if ((status = vds_do_attach(dip)) != DDI_SUCCESS)
72151ae08745Sheppo 			(void) vds_detach(dip, DDI_DETACH);
72161ae08745Sheppo 		return (status);
72171ae08745Sheppo 	case DDI_RESUME:
7218d10e4ef2Snarayan 		PR0("No action required for DDI_RESUME");
72191ae08745Sheppo 		return (DDI_SUCCESS);
72201ae08745Sheppo 	default:
72211ae08745Sheppo 		return (DDI_FAILURE);
72221ae08745Sheppo 	}
72231ae08745Sheppo }
72241ae08745Sheppo 
72251ae08745Sheppo static struct dev_ops vds_ops = {
72261ae08745Sheppo 	DEVO_REV,	/* devo_rev */
72271ae08745Sheppo 	0,		/* devo_refcnt */
72281ae08745Sheppo 	ddi_no_info,	/* devo_getinfo */
72291ae08745Sheppo 	nulldev,	/* devo_identify */
72301ae08745Sheppo 	nulldev,	/* devo_probe */
72311ae08745Sheppo 	vds_attach,	/* devo_attach */
72321ae08745Sheppo 	vds_detach,	/* devo_detach */
72331ae08745Sheppo 	nodev,		/* devo_reset */
72341ae08745Sheppo 	NULL,		/* devo_cb_ops */
72351ae08745Sheppo 	NULL,		/* devo_bus_ops */
72361ae08745Sheppo 	nulldev		/* devo_power */
72371ae08745Sheppo };
72381ae08745Sheppo 
72391ae08745Sheppo static struct modldrv modldrv = {
72401ae08745Sheppo 	&mod_driverops,
7241205eeb1aSlm66018 	"virtual disk server",
72421ae08745Sheppo 	&vds_ops,
72431ae08745Sheppo };
72441ae08745Sheppo 
72451ae08745Sheppo static struct modlinkage modlinkage = {
72461ae08745Sheppo 	MODREV_1,
72471ae08745Sheppo 	&modldrv,
72481ae08745Sheppo 	NULL
72491ae08745Sheppo };
72501ae08745Sheppo 
72511ae08745Sheppo 
72521ae08745Sheppo int
72531ae08745Sheppo _init(void)
72541ae08745Sheppo {
725517cadca8Slm66018 	int		status;
7256d10e4ef2Snarayan 
72571ae08745Sheppo 	if ((status = ddi_soft_state_init(&vds_state, sizeof (vds_t), 1)) != 0)
72581ae08745Sheppo 		return (status);
725917cadca8Slm66018 
72601ae08745Sheppo 	if ((status = mod_install(&modlinkage)) != 0) {
72611ae08745Sheppo 		ddi_soft_state_fini(&vds_state);
72621ae08745Sheppo 		return (status);
72631ae08745Sheppo 	}
72641ae08745Sheppo 
72651ae08745Sheppo 	return (0);
72661ae08745Sheppo }
72671ae08745Sheppo 
72681ae08745Sheppo int
72691ae08745Sheppo _info(struct modinfo *modinfop)
72701ae08745Sheppo {
72711ae08745Sheppo 	return (mod_info(&modlinkage, modinfop));
72721ae08745Sheppo }
72731ae08745Sheppo 
72741ae08745Sheppo int
72751ae08745Sheppo _fini(void)
72761ae08745Sheppo {
72771ae08745Sheppo 	int	status;
72781ae08745Sheppo 
72791ae08745Sheppo 	if ((status = mod_remove(&modlinkage)) != 0)
72801ae08745Sheppo 		return (status);
72811ae08745Sheppo 	ddi_soft_state_fini(&vds_state);
72821ae08745Sheppo 	return (0);
72831ae08745Sheppo }
7284