xref: /titanic_53/usr/src/uts/sun4v/io/vds.c (revision 8fce2fd6e90ed9f282bb13d9bf5be9648b8421e0)
11ae08745Sheppo /*
21ae08745Sheppo  * CDDL HEADER START
31ae08745Sheppo  *
41ae08745Sheppo  * The contents of this file are subject to the terms of the
51ae08745Sheppo  * Common Development and Distribution License (the "License").
61ae08745Sheppo  * You may not use this file except in compliance with the License.
71ae08745Sheppo  *
81ae08745Sheppo  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91ae08745Sheppo  * or http://www.opensolaris.org/os/licensing.
101ae08745Sheppo  * See the License for the specific language governing permissions
111ae08745Sheppo  * and limitations under the License.
121ae08745Sheppo  *
131ae08745Sheppo  * When distributing Covered Code, include this CDDL HEADER in each
141ae08745Sheppo  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151ae08745Sheppo  * If applicable, add the following below this CDDL HEADER, with the
161ae08745Sheppo  * fields enclosed by brackets "[]" replaced with your own identifying
171ae08745Sheppo  * information: Portions Copyright [yyyy] [name of copyright owner]
181ae08745Sheppo  *
191ae08745Sheppo  * CDDL HEADER END
201ae08745Sheppo  */
211ae08745Sheppo 
221ae08745Sheppo /*
23edcc0754Sachartre  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
241ae08745Sheppo  * Use is subject to license terms.
251ae08745Sheppo  */
261ae08745Sheppo 
271ae08745Sheppo #pragma ident	"%Z%%M%	%I%	%E% SMI"
281ae08745Sheppo 
291ae08745Sheppo /*
301ae08745Sheppo  * Virtual disk server
311ae08745Sheppo  */
321ae08745Sheppo 
331ae08745Sheppo 
341ae08745Sheppo #include <sys/types.h>
351ae08745Sheppo #include <sys/conf.h>
364bac2208Snarayan #include <sys/crc32.h>
371ae08745Sheppo #include <sys/ddi.h>
381ae08745Sheppo #include <sys/dkio.h>
391ae08745Sheppo #include <sys/file.h>
4017cadca8Slm66018 #include <sys/fs/hsfs_isospec.h>
411ae08745Sheppo #include <sys/mdeg.h>
422f5224aeSachartre #include <sys/mhd.h>
431ae08745Sheppo #include <sys/modhash.h>
441ae08745Sheppo #include <sys/note.h>
451ae08745Sheppo #include <sys/pathname.h>
46205eeb1aSlm66018 #include <sys/sdt.h>
471ae08745Sheppo #include <sys/sunddi.h>
481ae08745Sheppo #include <sys/sunldi.h>
491ae08745Sheppo #include <sys/sysmacros.h>
501ae08745Sheppo #include <sys/vio_common.h>
5117cadca8Slm66018 #include <sys/vio_util.h>
521ae08745Sheppo #include <sys/vdsk_mailbox.h>
531ae08745Sheppo #include <sys/vdsk_common.h>
541ae08745Sheppo #include <sys/vtoc.h>
553c96341aSnarayan #include <sys/vfs.h>
563c96341aSnarayan #include <sys/stat.h>
5787a7269eSachartre #include <sys/scsi/impl/uscsi.h>
58690555a1Sachartre #include <vm/seg_map.h>
591ae08745Sheppo 
601ae08745Sheppo /* Virtual disk server initialization flags */
61d10e4ef2Snarayan #define	VDS_LDI			0x01
62d10e4ef2Snarayan #define	VDS_MDEG		0x02
631ae08745Sheppo 
641ae08745Sheppo /* Virtual disk server tunable parameters */
653c96341aSnarayan #define	VDS_RETRIES		5
663c96341aSnarayan #define	VDS_LDC_DELAY		1000 /* 1 msecs */
673c96341aSnarayan #define	VDS_DEV_DELAY		10000000 /* 10 secs */
681ae08745Sheppo #define	VDS_NCHAINS		32
691ae08745Sheppo 
701ae08745Sheppo /* Identification parameters for MD, synthetic dkio(7i) structures, etc. */
711ae08745Sheppo #define	VDS_NAME		"virtual-disk-server"
721ae08745Sheppo 
731ae08745Sheppo #define	VD_NAME			"vd"
741ae08745Sheppo #define	VD_VOLUME_NAME		"vdisk"
751ae08745Sheppo #define	VD_ASCIILABEL		"Virtual Disk"
761ae08745Sheppo 
771ae08745Sheppo #define	VD_CHANNEL_ENDPOINT	"channel-endpoint"
781ae08745Sheppo #define	VD_ID_PROP		"id"
791ae08745Sheppo #define	VD_BLOCK_DEVICE_PROP	"vds-block-device"
80047ba61eSachartre #define	VD_BLOCK_DEVICE_OPTS	"vds-block-device-opts"
81445b4c2eSsb155480 #define	VD_REG_PROP		"reg"
821ae08745Sheppo 
831ae08745Sheppo /* Virtual disk initialization flags */
843c96341aSnarayan #define	VD_DISK_READY		0x01
853c96341aSnarayan #define	VD_LOCKING		0x02
863c96341aSnarayan #define	VD_LDC			0x04
873c96341aSnarayan #define	VD_DRING		0x08
883c96341aSnarayan #define	VD_SID			0x10
893c96341aSnarayan #define	VD_SEQ_NUM		0x20
90047ba61eSachartre #define	VD_SETUP_ERROR		0x40
911ae08745Sheppo 
92eba0cb4eSachartre /* Flags for writing to a vdisk which is a file */
93eba0cb4eSachartre #define	VD_FILE_WRITE_FLAGS	SM_ASYNC
94eba0cb4eSachartre 
9587a7269eSachartre /* Number of backup labels */
9687a7269eSachartre #define	VD_FILE_NUM_BACKUP	5
9787a7269eSachartre 
9887a7269eSachartre /* Timeout for SCSI I/O */
9987a7269eSachartre #define	VD_SCSI_RDWR_TIMEOUT	30	/* 30 secs */
10087a7269eSachartre 
101edcc0754Sachartre /* Maximum number of logical partitions */
102edcc0754Sachartre #define	VD_MAXPART	(NDKMAP + 1)
103edcc0754Sachartre 
1041ae08745Sheppo /*
1051ae08745Sheppo  * By Solaris convention, slice/partition 2 represents the entire disk;
1061ae08745Sheppo  * unfortunately, this convention does not appear to be codified.
1071ae08745Sheppo  */
1081ae08745Sheppo #define	VD_ENTIRE_DISK_SLICE	2
1091ae08745Sheppo 
110*8fce2fd6Sachartre /* Driver types */
111*8fce2fd6Sachartre typedef enum vd_driver {
112*8fce2fd6Sachartre 	VD_DRIVER_UNKNOWN = 0,	/* driver type unknown  */
113*8fce2fd6Sachartre 	VD_DRIVER_DISK,		/* disk driver */
114*8fce2fd6Sachartre 	VD_DRIVER_VOLUME	/* volume driver */
115*8fce2fd6Sachartre } vd_driver_t;
116*8fce2fd6Sachartre 
117*8fce2fd6Sachartre #define	VD_DRIVER_NAME_LEN	64
118*8fce2fd6Sachartre 
119*8fce2fd6Sachartre #define	VDS_NUM_DRIVERS	(sizeof (vds_driver_types) / sizeof (vd_driver_type_t))
120*8fce2fd6Sachartre 
121*8fce2fd6Sachartre typedef struct vd_driver_type {
122*8fce2fd6Sachartre 	char name[VD_DRIVER_NAME_LEN];	/* driver name */
123*8fce2fd6Sachartre 	vd_driver_t type;		/* driver type (disk or volume) */
124*8fce2fd6Sachartre } vd_driver_type_t;
125*8fce2fd6Sachartre 
126*8fce2fd6Sachartre /*
127*8fce2fd6Sachartre  * There is no reliable way to determine if a device is representing a disk
128*8fce2fd6Sachartre  * or a volume, especially with pseudo devices. So we maintain a list of well
129*8fce2fd6Sachartre  * known drivers and the type of device they represent (either a disk or a
130*8fce2fd6Sachartre  * volume).
131*8fce2fd6Sachartre  *
132*8fce2fd6Sachartre  * The list can be extended by adding a "driver-type-list" entry in vds.conf
133*8fce2fd6Sachartre  * with the following syntax:
134*8fce2fd6Sachartre  *
135*8fce2fd6Sachartre  * 	driver-type-list="<driver>:<type>", ... ,"<driver>:<type>";
136*8fce2fd6Sachartre  *
137*8fce2fd6Sachartre  * Where:
138*8fce2fd6Sachartre  *	<driver> is the name of a driver (limited to 64 characters)
139*8fce2fd6Sachartre  *	<type> is either the string "disk" or "volume"
140*8fce2fd6Sachartre  *
141*8fce2fd6Sachartre  * Invalid entries in "driver-type-list" will be ignored.
142*8fce2fd6Sachartre  *
143*8fce2fd6Sachartre  * For example, the following line in vds.conf:
144*8fce2fd6Sachartre  *
145*8fce2fd6Sachartre  * 	driver-type-list="foo:disk","bar:volume";
146*8fce2fd6Sachartre  *
147*8fce2fd6Sachartre  * defines that "foo" is a disk driver, and driver "bar" is a volume driver.
148*8fce2fd6Sachartre  *
149*8fce2fd6Sachartre  * When a list is defined in vds.conf, it is checked before the built-in list
150*8fce2fd6Sachartre  * (vds_driver_types[]) so that any definition from this list can be overriden
151*8fce2fd6Sachartre  * using vds.conf.
152*8fce2fd6Sachartre  */
153*8fce2fd6Sachartre vd_driver_type_t vds_driver_types[] = {
154*8fce2fd6Sachartre 	{ "dad",	VD_DRIVER_DISK },	/* Solaris */
155*8fce2fd6Sachartre 	{ "did",	VD_DRIVER_DISK },	/* Sun Cluster */
156*8fce2fd6Sachartre 	{ "lofi",	VD_DRIVER_VOLUME },	/* Solaris */
157*8fce2fd6Sachartre 	{ "md",		VD_DRIVER_VOLUME },	/* Solaris - SVM */
158*8fce2fd6Sachartre 	{ "sd",		VD_DRIVER_DISK },	/* Solaris */
159*8fce2fd6Sachartre 	{ "ssd",	VD_DRIVER_DISK },	/* Solaris */
160*8fce2fd6Sachartre 	{ "vdc",	VD_DRIVER_DISK },	/* Solaris */
161*8fce2fd6Sachartre 	{ "vxdmp",	VD_DRIVER_DISK },	/* Veritas */
162*8fce2fd6Sachartre 	{ "vxio",	VD_DRIVER_VOLUME },	/* Veritas - VxVM */
163*8fce2fd6Sachartre 	{ "zfs",	VD_DRIVER_VOLUME }	/* Solaris */
164*8fce2fd6Sachartre };
165*8fce2fd6Sachartre 
1661ae08745Sheppo /* Return a cpp token as a string */
1671ae08745Sheppo #define	STRINGIZE(token)	#token
1681ae08745Sheppo 
1691ae08745Sheppo /*
1701ae08745Sheppo  * Print a message prefixed with the current function name to the message log
1711ae08745Sheppo  * (and optionally to the console for verbose boots); these macros use cpp's
1721ae08745Sheppo  * concatenation of string literals and C99 variable-length-argument-list
1731ae08745Sheppo  * macros
1741ae08745Sheppo  */
1751ae08745Sheppo #define	PRN(...)	_PRN("?%s():  "__VA_ARGS__, "")
1761ae08745Sheppo #define	_PRN(format, ...)					\
1771ae08745Sheppo 	cmn_err(CE_CONT, format"%s", __func__, __VA_ARGS__)
1781ae08745Sheppo 
1791ae08745Sheppo /* Return a pointer to the "i"th vdisk dring element */
1801ae08745Sheppo #define	VD_DRING_ELEM(i)	((vd_dring_entry_t *)(void *)	\
1811ae08745Sheppo 	    (vd->dring + (i)*vd->descriptor_size))
1821ae08745Sheppo 
1831ae08745Sheppo /* Return the virtual disk client's type as a string (for use in messages) */
1841ae08745Sheppo #define	VD_CLIENT(vd)							\
1851ae08745Sheppo 	(((vd)->xfer_mode == VIO_DESC_MODE) ? "in-band client" :	\
186f0ca1d9aSsb155480 	    (((vd)->xfer_mode == VIO_DRING_MODE_V1_0) ? "dring client" :    \
1871ae08745Sheppo 		(((vd)->xfer_mode == 0) ? "null client" :		\
1881ae08745Sheppo 		    "unsupported client")))
1891ae08745Sheppo 
190690555a1Sachartre /* Read disk label from a disk on file */
191690555a1Sachartre #define	VD_FILE_LABEL_READ(vd, labelp) \
19287a7269eSachartre 	vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)labelp, \
193690555a1Sachartre 	    0, sizeof (struct dk_label))
194690555a1Sachartre 
195690555a1Sachartre /* Write disk label to a disk on file */
196690555a1Sachartre #define	VD_FILE_LABEL_WRITE(vd, labelp)	\
19787a7269eSachartre 	vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)labelp, \
198690555a1Sachartre 	    0, sizeof (struct dk_label))
199690555a1Sachartre 
2002f5224aeSachartre /* Message for disk access rights reset failure */
2012f5224aeSachartre #define	VD_RESET_ACCESS_FAILURE_MSG \
2022f5224aeSachartre 	"Fail to reset disk access rights for disk %s"
2032f5224aeSachartre 
204445b4c2eSsb155480 /*
205445b4c2eSsb155480  * Specification of an MD node passed to the MDEG to filter any
206445b4c2eSsb155480  * 'vport' nodes that do not belong to the specified node. This
207445b4c2eSsb155480  * template is copied for each vds instance and filled in with
208445b4c2eSsb155480  * the appropriate 'cfg-handle' value before being passed to the MDEG.
209445b4c2eSsb155480  */
210445b4c2eSsb155480 static mdeg_prop_spec_t	vds_prop_template[] = {
211445b4c2eSsb155480 	{ MDET_PROP_STR,	"name",		VDS_NAME },
212445b4c2eSsb155480 	{ MDET_PROP_VAL,	"cfg-handle",	NULL },
213445b4c2eSsb155480 	{ MDET_LIST_END,	NULL, 		NULL }
214445b4c2eSsb155480 };
215445b4c2eSsb155480 
216445b4c2eSsb155480 #define	VDS_SET_MDEG_PROP_INST(specp, val) (specp)[1].ps_val = (val);
217445b4c2eSsb155480 
218445b4c2eSsb155480 /*
219445b4c2eSsb155480  * Matching criteria passed to the MDEG to register interest
220445b4c2eSsb155480  * in changes to 'virtual-device-port' nodes identified by their
221445b4c2eSsb155480  * 'id' property.
222445b4c2eSsb155480  */
223445b4c2eSsb155480 static md_prop_match_t	vd_prop_match[] = {
224445b4c2eSsb155480 	{ MDET_PROP_VAL,	VD_ID_PROP },
225445b4c2eSsb155480 	{ MDET_LIST_END,	NULL }
226445b4c2eSsb155480 };
227445b4c2eSsb155480 
228445b4c2eSsb155480 static mdeg_node_match_t vd_match = {"virtual-device-port",
229445b4c2eSsb155480 				    vd_prop_match};
230445b4c2eSsb155480 
231047ba61eSachartre /*
232047ba61eSachartre  * Options for the VD_BLOCK_DEVICE_OPTS property.
233047ba61eSachartre  */
234047ba61eSachartre #define	VD_OPT_RDONLY		0x1	/* read-only  */
235047ba61eSachartre #define	VD_OPT_SLICE		0x2	/* single slice */
236047ba61eSachartre #define	VD_OPT_EXCLUSIVE	0x4	/* exclusive access */
237047ba61eSachartre 
238047ba61eSachartre #define	VD_OPTION_NLEN	128
239047ba61eSachartre 
240047ba61eSachartre typedef struct vd_option {
241047ba61eSachartre 	char vdo_name[VD_OPTION_NLEN];
242047ba61eSachartre 	uint64_t vdo_value;
243047ba61eSachartre } vd_option_t;
244047ba61eSachartre 
245047ba61eSachartre vd_option_t vd_bdev_options[] = {
246047ba61eSachartre 	{ "ro",		VD_OPT_RDONLY },
247047ba61eSachartre 	{ "slice", 	VD_OPT_SLICE },
248047ba61eSachartre 	{ "excl",	VD_OPT_EXCLUSIVE }
249047ba61eSachartre };
250047ba61eSachartre 
2511ae08745Sheppo /* Debugging macros */
2521ae08745Sheppo #ifdef DEBUG
2533af08d82Slm66018 
2543af08d82Slm66018 static int	vd_msglevel = 0;
2553af08d82Slm66018 
2561ae08745Sheppo #define	PR0 if (vd_msglevel > 0)	PRN
2571ae08745Sheppo #define	PR1 if (vd_msglevel > 1)	PRN
2581ae08745Sheppo #define	PR2 if (vd_msglevel > 2)	PRN
2591ae08745Sheppo 
2601ae08745Sheppo #define	VD_DUMP_DRING_ELEM(elem)					\
2613c96341aSnarayan 	PR0("dst:%x op:%x st:%u nb:%lx addr:%lx ncook:%u\n",		\
2621ae08745Sheppo 	    elem->hdr.dstate,						\
2631ae08745Sheppo 	    elem->payload.operation,					\
2641ae08745Sheppo 	    elem->payload.status,					\
2651ae08745Sheppo 	    elem->payload.nbytes,					\
2661ae08745Sheppo 	    elem->payload.addr,						\
2671ae08745Sheppo 	    elem->payload.ncookies);
2681ae08745Sheppo 
2693af08d82Slm66018 char *
2703af08d82Slm66018 vd_decode_state(int state)
2713af08d82Slm66018 {
2723af08d82Slm66018 	char *str;
2733af08d82Slm66018 
2743af08d82Slm66018 #define	CASE_STATE(_s)	case _s: str = #_s; break;
2753af08d82Slm66018 
2763af08d82Slm66018 	switch (state) {
2773af08d82Slm66018 	CASE_STATE(VD_STATE_INIT)
2783af08d82Slm66018 	CASE_STATE(VD_STATE_VER)
2793af08d82Slm66018 	CASE_STATE(VD_STATE_ATTR)
2803af08d82Slm66018 	CASE_STATE(VD_STATE_DRING)
2813af08d82Slm66018 	CASE_STATE(VD_STATE_RDX)
2823af08d82Slm66018 	CASE_STATE(VD_STATE_DATA)
2833af08d82Slm66018 	default: str = "unknown"; break;
2843af08d82Slm66018 	}
2853af08d82Slm66018 
2863af08d82Slm66018 #undef CASE_STATE
2873af08d82Slm66018 
2883af08d82Slm66018 	return (str);
2893af08d82Slm66018 }
2903af08d82Slm66018 
2913af08d82Slm66018 void
2923af08d82Slm66018 vd_decode_tag(vio_msg_t *msg)
2933af08d82Slm66018 {
2943af08d82Slm66018 	char *tstr, *sstr, *estr;
2953af08d82Slm66018 
2963af08d82Slm66018 #define	CASE_TYPE(_s)	case _s: tstr = #_s; break;
2973af08d82Slm66018 
2983af08d82Slm66018 	switch (msg->tag.vio_msgtype) {
2993af08d82Slm66018 	CASE_TYPE(VIO_TYPE_CTRL)
3003af08d82Slm66018 	CASE_TYPE(VIO_TYPE_DATA)
3013af08d82Slm66018 	CASE_TYPE(VIO_TYPE_ERR)
3023af08d82Slm66018 	default: tstr = "unknown"; break;
3033af08d82Slm66018 	}
3043af08d82Slm66018 
3053af08d82Slm66018 #undef CASE_TYPE
3063af08d82Slm66018 
3073af08d82Slm66018 #define	CASE_SUBTYPE(_s) case _s: sstr = #_s; break;
3083af08d82Slm66018 
3093af08d82Slm66018 	switch (msg->tag.vio_subtype) {
3103af08d82Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_INFO)
3113af08d82Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_ACK)
3123af08d82Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_NACK)
3133af08d82Slm66018 	default: sstr = "unknown"; break;
3143af08d82Slm66018 	}
3153af08d82Slm66018 
3163af08d82Slm66018 #undef CASE_SUBTYPE
3173af08d82Slm66018 
3183af08d82Slm66018 #define	CASE_ENV(_s)	case _s: estr = #_s; break;
3193af08d82Slm66018 
3203af08d82Slm66018 	switch (msg->tag.vio_subtype_env) {
3213af08d82Slm66018 	CASE_ENV(VIO_VER_INFO)
3223af08d82Slm66018 	CASE_ENV(VIO_ATTR_INFO)
3233af08d82Slm66018 	CASE_ENV(VIO_DRING_REG)
3243af08d82Slm66018 	CASE_ENV(VIO_DRING_UNREG)
3253af08d82Slm66018 	CASE_ENV(VIO_RDX)
3263af08d82Slm66018 	CASE_ENV(VIO_PKT_DATA)
3273af08d82Slm66018 	CASE_ENV(VIO_DESC_DATA)
3283af08d82Slm66018 	CASE_ENV(VIO_DRING_DATA)
3293af08d82Slm66018 	default: estr = "unknown"; break;
3303af08d82Slm66018 	}
3313af08d82Slm66018 
3323af08d82Slm66018 #undef CASE_ENV
3333af08d82Slm66018 
3343af08d82Slm66018 	PR1("(%x/%x/%x) message : (%s/%s/%s)",
3353af08d82Slm66018 	    msg->tag.vio_msgtype, msg->tag.vio_subtype,
3363af08d82Slm66018 	    msg->tag.vio_subtype_env, tstr, sstr, estr);
3373af08d82Slm66018 }
3383af08d82Slm66018 
3391ae08745Sheppo #else	/* !DEBUG */
3403af08d82Slm66018 
3411ae08745Sheppo #define	PR0(...)
3421ae08745Sheppo #define	PR1(...)
3431ae08745Sheppo #define	PR2(...)
3441ae08745Sheppo 
3451ae08745Sheppo #define	VD_DUMP_DRING_ELEM(elem)
3461ae08745Sheppo 
3473af08d82Slm66018 #define	vd_decode_state(_s)	(NULL)
3483af08d82Slm66018 #define	vd_decode_tag(_s)	(NULL)
3493af08d82Slm66018 
3501ae08745Sheppo #endif	/* DEBUG */
3511ae08745Sheppo 
3521ae08745Sheppo 
353d10e4ef2Snarayan /*
354d10e4ef2Snarayan  * Soft state structure for a vds instance
355d10e4ef2Snarayan  */
3561ae08745Sheppo typedef struct vds {
3571ae08745Sheppo 	uint_t		initialized;	/* driver inst initialization flags */
3581ae08745Sheppo 	dev_info_t	*dip;		/* driver inst devinfo pointer */
3591ae08745Sheppo 	ldi_ident_t	ldi_ident;	/* driver's identifier for LDI */
3601ae08745Sheppo 	mod_hash_t	*vd_table;	/* table of virtual disks served */
361445b4c2eSsb155480 	mdeg_node_spec_t *ispecp;	/* mdeg node specification */
3621ae08745Sheppo 	mdeg_handle_t	mdeg;		/* handle for MDEG operations  */
363*8fce2fd6Sachartre 	vd_driver_type_t *driver_types;	/* extra driver types (from vds.conf) */
364*8fce2fd6Sachartre 	int 		num_drivers;	/* num of extra driver types */
3651ae08745Sheppo } vds_t;
3661ae08745Sheppo 
367d10e4ef2Snarayan /*
368d10e4ef2Snarayan  * Types of descriptor-processing tasks
369d10e4ef2Snarayan  */
370d10e4ef2Snarayan typedef enum vd_task_type {
371d10e4ef2Snarayan 	VD_NONFINAL_RANGE_TASK,	/* task for intermediate descriptor in range */
372d10e4ef2Snarayan 	VD_FINAL_RANGE_TASK,	/* task for last in a range of descriptors */
373d10e4ef2Snarayan } vd_task_type_t;
374d10e4ef2Snarayan 
375d10e4ef2Snarayan /*
376d10e4ef2Snarayan  * Structure describing the task for processing a descriptor
377d10e4ef2Snarayan  */
378d10e4ef2Snarayan typedef struct vd_task {
379d10e4ef2Snarayan 	struct vd		*vd;		/* vd instance task is for */
380d10e4ef2Snarayan 	vd_task_type_t		type;		/* type of descriptor task */
381d10e4ef2Snarayan 	int			index;		/* dring elem index for task */
382d10e4ef2Snarayan 	vio_msg_t		*msg;		/* VIO message task is for */
383d10e4ef2Snarayan 	size_t			msglen;		/* length of message content */
384d10e4ef2Snarayan 	vd_dring_payload_t	*request;	/* request task will perform */
385d10e4ef2Snarayan 	struct buf		buf;		/* buf(9s) for I/O request */
3864bac2208Snarayan 	ldc_mem_handle_t	mhdl;		/* task memory handle */
387205eeb1aSlm66018 	int			status;		/* status of processing task */
388205eeb1aSlm66018 	int	(*completef)(struct vd_task *task); /* completion func ptr */
389d10e4ef2Snarayan } vd_task_t;
390d10e4ef2Snarayan 
391d10e4ef2Snarayan /*
392d10e4ef2Snarayan  * Soft state structure for a virtual disk instance
393d10e4ef2Snarayan  */
3941ae08745Sheppo typedef struct vd {
3951ae08745Sheppo 	uint_t			initialized;	/* vdisk initialization flags */
39617cadca8Slm66018 	uint64_t		operations;	/* bitmask of VD_OPs exported */
39717cadca8Slm66018 	vio_ver_t		version;	/* ver negotiated with client */
3981ae08745Sheppo 	vds_t			*vds;		/* server for this vdisk */
399d10e4ef2Snarayan 	ddi_taskq_t		*startq;	/* queue for I/O start tasks */
400d10e4ef2Snarayan 	ddi_taskq_t		*completionq;	/* queue for completion tasks */
4011ae08745Sheppo 	ldi_handle_t		ldi_handle[V_NUMPAR];	/* LDI slice handles */
4023c96341aSnarayan 	char			device_path[MAXPATHLEN + 1]; /* vdisk device */
4031ae08745Sheppo 	dev_t			dev[V_NUMPAR];	/* dev numbers for slices */
404047ba61eSachartre 	int			open_flags;	/* open flags */
405e1ebb9ecSlm66018 	uint_t			nslices;	/* number of slices */
4061ae08745Sheppo 	size_t			vdisk_size;	/* number of blocks in vdisk */
40717cadca8Slm66018 	size_t			vdisk_block_size; /* size of each vdisk block */
4081ae08745Sheppo 	vd_disk_type_t		vdisk_type;	/* slice or entire disk */
4094bac2208Snarayan 	vd_disk_label_t		vdisk_label;	/* EFI or VTOC label */
41017cadca8Slm66018 	vd_media_t		vdisk_media;	/* media type of backing dev. */
41117cadca8Slm66018 	boolean_t		is_atapi_dev;	/* Is this an IDE CD-ROM dev? */
412e1ebb9ecSlm66018 	ushort_t		max_xfer_sz;	/* max xfer size in DEV_BSIZE */
41317cadca8Slm66018 	size_t			block_size;	/* blk size of actual device */
414*8fce2fd6Sachartre 	boolean_t		volume;		/* is vDisk backed by volume */
41517cadca8Slm66018 	boolean_t		file;		/* is vDisk backed by a file? */
4162f5224aeSachartre 	boolean_t		scsi;		/* is vDisk backed by scsi? */
4173c96341aSnarayan 	vnode_t			*file_vnode;	/* file vnode */
4183c96341aSnarayan 	size_t			file_size;	/* file size */
41987a7269eSachartre 	ddi_devid_t		file_devid;	/* devid for disk image */
420edcc0754Sachartre 	efi_gpt_t		efi_gpt;	/* EFI GPT for slice type */
421edcc0754Sachartre 	efi_gpe_t		efi_gpe;	/* EFI GPE for slice type */
422edcc0754Sachartre 	int			efi_reserved;	/* EFI reserved slice */
4231ae08745Sheppo 	struct dk_geom		dk_geom;	/* synthetic for slice type */
4241ae08745Sheppo 	struct vtoc		vtoc;		/* synthetic for slice type */
425edcc0754Sachartre 	vd_slice_t		slices[VD_MAXPART]; /* logical partitions */
4262f5224aeSachartre 	boolean_t		ownership;	/* disk ownership status */
4271ae08745Sheppo 	ldc_status_t		ldc_state;	/* LDC connection state */
4281ae08745Sheppo 	ldc_handle_t		ldc_handle;	/* handle for LDC comm */
4291ae08745Sheppo 	size_t			max_msglen;	/* largest LDC message len */
4301ae08745Sheppo 	vd_state_t		state;		/* client handshake state */
4311ae08745Sheppo 	uint8_t			xfer_mode;	/* transfer mode with client */
4321ae08745Sheppo 	uint32_t		sid;		/* client's session ID */
4331ae08745Sheppo 	uint64_t		seq_num;	/* message sequence number */
4341ae08745Sheppo 	uint64_t		dring_ident;	/* identifier of dring */
4351ae08745Sheppo 	ldc_dring_handle_t	dring_handle;	/* handle for dring ops */
4361ae08745Sheppo 	uint32_t		descriptor_size;	/* num bytes in desc */
4371ae08745Sheppo 	uint32_t		dring_len;	/* number of dring elements */
4381ae08745Sheppo 	caddr_t			dring;		/* address of dring */
4393af08d82Slm66018 	caddr_t			vio_msgp;	/* vio msg staging buffer */
440d10e4ef2Snarayan 	vd_task_t		inband_task;	/* task for inband descriptor */
441d10e4ef2Snarayan 	vd_task_t		*dring_task;	/* tasks dring elements */
442d10e4ef2Snarayan 
443d10e4ef2Snarayan 	kmutex_t		lock;		/* protects variables below */
444d10e4ef2Snarayan 	boolean_t		enabled;	/* is vdisk enabled? */
445d10e4ef2Snarayan 	boolean_t		reset_state;	/* reset connection state? */
446d10e4ef2Snarayan 	boolean_t		reset_ldc;	/* reset LDC channel? */
4471ae08745Sheppo } vd_t;
4481ae08745Sheppo 
4491ae08745Sheppo typedef struct vds_operation {
4503af08d82Slm66018 	char	*namep;
4511ae08745Sheppo 	uint8_t	operation;
452d10e4ef2Snarayan 	int	(*start)(vd_task_t *task);
453205eeb1aSlm66018 	int	(*complete)(vd_task_t *task);
4541ae08745Sheppo } vds_operation_t;
4551ae08745Sheppo 
4560a55fbb7Slm66018 typedef struct vd_ioctl {
4570a55fbb7Slm66018 	uint8_t		operation;		/* vdisk operation */
4580a55fbb7Slm66018 	const char	*operation_name;	/* vdisk operation name */
4590a55fbb7Slm66018 	size_t		nbytes;			/* size of operation buffer */
4600a55fbb7Slm66018 	int		cmd;			/* corresponding ioctl cmd */
4610a55fbb7Slm66018 	const char	*cmd_name;		/* ioctl cmd name */
4620a55fbb7Slm66018 	void		*arg;			/* ioctl cmd argument */
4630a55fbb7Slm66018 	/* convert input vd_buf to output ioctl_arg */
4642f5224aeSachartre 	int		(*copyin)(void *vd_buf, size_t, void *ioctl_arg);
4650a55fbb7Slm66018 	/* convert input ioctl_arg to output vd_buf */
4660a55fbb7Slm66018 	void		(*copyout)(void *ioctl_arg, void *vd_buf);
467047ba61eSachartre 	/* write is true if the operation writes any data to the backend */
468047ba61eSachartre 	boolean_t	write;
4690a55fbb7Slm66018 } vd_ioctl_t;
4700a55fbb7Slm66018 
4710a55fbb7Slm66018 /* Define trivial copyin/copyout conversion function flag */
4722f5224aeSachartre #define	VD_IDENTITY_IN	((int (*)(void *, size_t, void *))-1)
4732f5224aeSachartre #define	VD_IDENTITY_OUT	((void (*)(void *, void *))-1)
4741ae08745Sheppo 
4751ae08745Sheppo 
4763c96341aSnarayan static int	vds_ldc_retries = VDS_RETRIES;
4773af08d82Slm66018 static int	vds_ldc_delay = VDS_LDC_DELAY;
4783c96341aSnarayan static int	vds_dev_retries = VDS_RETRIES;
4793c96341aSnarayan static int	vds_dev_delay = VDS_DEV_DELAY;
4801ae08745Sheppo static void	*vds_state;
4811ae08745Sheppo 
482eba0cb4eSachartre static uint_t	vd_file_write_flags = VD_FILE_WRITE_FLAGS;
483eba0cb4eSachartre 
48487a7269eSachartre static short	vd_scsi_rdwr_timeout = VD_SCSI_RDWR_TIMEOUT;
4852f5224aeSachartre static int	vd_scsi_debug = USCSI_SILENT;
4862f5224aeSachartre 
4872f5224aeSachartre /*
4882f5224aeSachartre  * Tunable to define the behavior of the service domain if the vdisk server
4892f5224aeSachartre  * fails to reset disk exclusive access when a LDC channel is reset. When a
4902f5224aeSachartre  * LDC channel is reset the vdisk server will try to reset disk exclusive
4912f5224aeSachartre  * access by releasing any SCSI-2 reservation or resetting the disk. If these
4922f5224aeSachartre  * actions fail then the default behavior (vd_reset_access_failure = 0) is to
4932f5224aeSachartre  * print a warning message. This default behavior can be changed by setting
4942f5224aeSachartre  * the vd_reset_access_failure variable to A_REBOOT (= 0x1) and that will
4952f5224aeSachartre  * cause the service domain to reboot, or A_DUMP (= 0x5) and that will cause
4962f5224aeSachartre  * the service domain to panic. In both cases, the reset of the service domain
4972f5224aeSachartre  * should trigger a reset SCSI buses and hopefully clear any SCSI-2 reservation.
4982f5224aeSachartre  */
4992f5224aeSachartre static int 	vd_reset_access_failure = 0;
5002f5224aeSachartre 
5012f5224aeSachartre /*
5022f5224aeSachartre  * Tunable for backward compatibility. When this variable is set to B_TRUE,
5032f5224aeSachartre  * all disk volumes (ZFS, SVM, VxvM volumes) will be exported as single
5042f5224aeSachartre  * slice disks whether or not they have the "slice" option set. This is
5052f5224aeSachartre  * to provide a simple backward compatibility mechanism when upgrading
5062f5224aeSachartre  * the vds driver and using a domain configuration created before the
5072f5224aeSachartre  * "slice" option was available.
5082f5224aeSachartre  */
5092f5224aeSachartre static boolean_t vd_volume_force_slice = B_FALSE;
51087a7269eSachartre 
5110a55fbb7Slm66018 /*
51266cfcfbeSachartre  * The label of disk images created with some earlier versions of the virtual
51366cfcfbeSachartre  * disk software is not entirely correct and have an incorrect v_sanity field
51466cfcfbeSachartre  * (usually 0) instead of VTOC_SANE. This creates a compatibility problem with
51566cfcfbeSachartre  * these images because we are now validating that the disk label (and the
51666cfcfbeSachartre  * sanity) is correct when a disk image is opened.
51766cfcfbeSachartre  *
51866cfcfbeSachartre  * This tunable is set to false to not validate the sanity field and ensure
51966cfcfbeSachartre  * compatibility. If the tunable is set to true, we will do a strict checking
52066cfcfbeSachartre  * of the sanity but this can create compatibility problems with old disk
52166cfcfbeSachartre  * images.
52266cfcfbeSachartre  */
52366cfcfbeSachartre static boolean_t vd_file_validate_sanity = B_FALSE;
52466cfcfbeSachartre 
52566cfcfbeSachartre /*
5260a55fbb7Slm66018  * Supported protocol version pairs, from highest (newest) to lowest (oldest)
5270a55fbb7Slm66018  *
5280a55fbb7Slm66018  * Each supported major version should appear only once, paired with (and only
5290a55fbb7Slm66018  * with) its highest supported minor version number (as the protocol requires
5300a55fbb7Slm66018  * supporting all lower minor version numbers as well)
5310a55fbb7Slm66018  */
53217cadca8Slm66018 static const vio_ver_t	vds_version[] = {{1, 1}};
5330a55fbb7Slm66018 static const size_t	vds_num_versions =
5340a55fbb7Slm66018     sizeof (vds_version)/sizeof (vds_version[0]);
5350a55fbb7Slm66018 
5363af08d82Slm66018 static void vd_free_dring_task(vd_t *vdp);
5373c96341aSnarayan static int vd_setup_vd(vd_t *vd);
538047ba61eSachartre static int vd_setup_single_slice_disk(vd_t *vd);
5392f5224aeSachartre static int vd_setup_mediainfo(vd_t *vd);
5403c96341aSnarayan static boolean_t vd_enabled(vd_t *vd);
54178fcd0a1Sachartre static ushort_t vd_lbl2cksum(struct dk_label *label);
54278fcd0a1Sachartre static int vd_file_validate_geometry(vd_t *vd);
54317cadca8Slm66018 static boolean_t vd_file_is_iso_image(vd_t *vd);
54417cadca8Slm66018 static void vd_set_exported_operations(vd_t *vd);
5452f5224aeSachartre static void vd_reset_access(vd_t *vd);
546edcc0754Sachartre static int vd_backend_ioctl(vd_t *vd, int cmd, caddr_t arg);
547edcc0754Sachartre static int vds_efi_alloc_and_read(vd_t *, efi_gpt_t **, efi_gpe_t **);
548edcc0754Sachartre static void vds_efi_free(vd_t *, efi_gpt_t *, efi_gpe_t *);
549*8fce2fd6Sachartre static void vds_driver_types_free(vds_t *vds);
550047ba61eSachartre 
551690555a1Sachartre /*
552690555a1Sachartre  * Function:
553690555a1Sachartre  *	vd_file_rw
554690555a1Sachartre  *
555690555a1Sachartre  * Description:
556690555a1Sachartre  * 	Read or write to a disk on file.
557690555a1Sachartre  *
558690555a1Sachartre  * Parameters:
559690555a1Sachartre  *	vd		- disk on which the operation is performed.
560690555a1Sachartre  *	slice		- slice on which the operation is performed,
56187a7269eSachartre  *			  VD_SLICE_NONE indicates that the operation
56287a7269eSachartre  *			  is done using an absolute disk offset.
563690555a1Sachartre  *	operation	- operation to execute: read (VD_OP_BREAD) or
564690555a1Sachartre  *			  write (VD_OP_BWRITE).
565690555a1Sachartre  *	data		- buffer where data are read to or written from.
566690555a1Sachartre  *	blk		- starting block for the operation.
567690555a1Sachartre  *	len		- number of bytes to read or write.
568690555a1Sachartre  *
569690555a1Sachartre  * Return Code:
570690555a1Sachartre  *	n >= 0		- success, n indicates the number of bytes read
571690555a1Sachartre  *			  or written.
572690555a1Sachartre  *	-1		- error.
573690555a1Sachartre  */
574690555a1Sachartre static ssize_t
575690555a1Sachartre vd_file_rw(vd_t *vd, int slice, int operation, caddr_t data, size_t blk,
576690555a1Sachartre     size_t len)
577690555a1Sachartre {
578690555a1Sachartre 	caddr_t	maddr;
579690555a1Sachartre 	size_t offset, maxlen, moffset, mlen, n;
580690555a1Sachartre 	uint_t smflags;
581690555a1Sachartre 	enum seg_rw srw;
582690555a1Sachartre 
583690555a1Sachartre 	ASSERT(vd->file);
584690555a1Sachartre 	ASSERT(len > 0);
585690555a1Sachartre 
586047ba61eSachartre 	/*
587047ba61eSachartre 	 * If a file is exported as a slice then we don't care about the vtoc.
588047ba61eSachartre 	 * In that case, the vtoc is a fake mainly to make newfs happy and we
589047ba61eSachartre 	 * handle any I/O as a raw disk access so that we can have access to the
590047ba61eSachartre 	 * entire backend.
591047ba61eSachartre 	 */
592047ba61eSachartre 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE || slice == VD_SLICE_NONE) {
593690555a1Sachartre 		/* raw disk access */
594690555a1Sachartre 		offset = blk * DEV_BSIZE;
595690555a1Sachartre 	} else {
596690555a1Sachartre 		ASSERT(slice >= 0 && slice < V_NUMPAR);
59778fcd0a1Sachartre 
59817cadca8Slm66018 		/*
59917cadca8Slm66018 		 * v1.0 vDisk clients depended on the server not verifying
60017cadca8Slm66018 		 * the label of a unformatted disk.  This "feature" is
60117cadca8Slm66018 		 * maintained for backward compatibility but all versions
60217cadca8Slm66018 		 * from v1.1 onwards must do the right thing.
60317cadca8Slm66018 		 */
60478fcd0a1Sachartre 		if (vd->vdisk_label == VD_DISK_LABEL_UNK &&
605edcc0754Sachartre 		    vio_ver_is_supported(vd->version, 1, 1)) {
606edcc0754Sachartre 			(void) vd_file_validate_geometry(vd);
607edcc0754Sachartre 			if (vd->vdisk_label == VD_DISK_LABEL_UNK) {
608edcc0754Sachartre 				PR0("Unknown disk label, can't do I/O "
609edcc0754Sachartre 				    "from slice %d", slice);
61078fcd0a1Sachartre 				return (-1);
61178fcd0a1Sachartre 			}
612edcc0754Sachartre 		}
61378fcd0a1Sachartre 
614edcc0754Sachartre 		if (vd->vdisk_label == VD_DISK_LABEL_VTOC) {
615edcc0754Sachartre 			ASSERT(vd->vtoc.v_sectorsz == DEV_BSIZE);
616edcc0754Sachartre 		} else {
617edcc0754Sachartre 			ASSERT(vd->vdisk_label == VD_DISK_LABEL_EFI);
618edcc0754Sachartre 			ASSERT(vd->vdisk_block_size == DEV_BSIZE);
619edcc0754Sachartre 		}
620edcc0754Sachartre 
621edcc0754Sachartre 		if (blk >= vd->slices[slice].nblocks) {
622690555a1Sachartre 			/* address past the end of the slice */
623690555a1Sachartre 			PR0("req_addr (0x%lx) > psize (0x%lx)",
624edcc0754Sachartre 			    blk, vd->slices[slice].nblocks);
625690555a1Sachartre 			return (0);
626690555a1Sachartre 		}
627690555a1Sachartre 
628edcc0754Sachartre 		offset = (vd->slices[slice].start + blk) * DEV_BSIZE;
629690555a1Sachartre 
630690555a1Sachartre 		/*
631690555a1Sachartre 		 * If the requested size is greater than the size
632690555a1Sachartre 		 * of the partition, truncate the read/write.
633690555a1Sachartre 		 */
634edcc0754Sachartre 		maxlen = (vd->slices[slice].nblocks - blk) * DEV_BSIZE;
635690555a1Sachartre 
636690555a1Sachartre 		if (len > maxlen) {
637690555a1Sachartre 			PR0("I/O size truncated to %lu bytes from %lu bytes",
638690555a1Sachartre 			    maxlen, len);
639690555a1Sachartre 			len = maxlen;
640690555a1Sachartre 		}
641690555a1Sachartre 	}
642690555a1Sachartre 
643690555a1Sachartre 	/*
644690555a1Sachartre 	 * We have to ensure that we are reading/writing into the mmap
645690555a1Sachartre 	 * range. If we have a partial disk image (e.g. an image of
646690555a1Sachartre 	 * s0 instead s2) the system can try to access slices that
647690555a1Sachartre 	 * are not included into the disk image.
648690555a1Sachartre 	 */
649edcc0754Sachartre 	if ((offset + len) > vd->file_size) {
650edcc0754Sachartre 		PR0("offset + nbytes (0x%lx + 0x%lx) > "
651690555a1Sachartre 		    "file_size (0x%lx)", offset, len, vd->file_size);
652690555a1Sachartre 		return (-1);
653690555a1Sachartre 	}
654690555a1Sachartre 
655690555a1Sachartre 	srw = (operation == VD_OP_BREAD)? S_READ : S_WRITE;
656eba0cb4eSachartre 	smflags = (operation == VD_OP_BREAD)? 0 :
657eba0cb4eSachartre 	    (SM_WRITE | vd_file_write_flags);
658690555a1Sachartre 	n = len;
659690555a1Sachartre 
660690555a1Sachartre 	do {
661690555a1Sachartre 		/*
662690555a1Sachartre 		 * segmap_getmapflt() returns a MAXBSIZE chunk which is
663690555a1Sachartre 		 * MAXBSIZE aligned.
664690555a1Sachartre 		 */
665690555a1Sachartre 		moffset = offset & MAXBOFFSET;
666690555a1Sachartre 		mlen = MIN(MAXBSIZE - moffset, n);
667690555a1Sachartre 		maddr = segmap_getmapflt(segkmap, vd->file_vnode, offset,
668690555a1Sachartre 		    mlen, 1, srw);
669690555a1Sachartre 		/*
670690555a1Sachartre 		 * Fault in the pages so we can check for error and ensure
671690555a1Sachartre 		 * that we can safely used the mapped address.
672690555a1Sachartre 		 */
673690555a1Sachartre 		if (segmap_fault(kas.a_hat, segkmap, maddr, mlen,
674690555a1Sachartre 		    F_SOFTLOCK, srw) != 0) {
675690555a1Sachartre 			(void) segmap_release(segkmap, maddr, 0);
676690555a1Sachartre 			return (-1);
677690555a1Sachartre 		}
678690555a1Sachartre 
679690555a1Sachartre 		if (operation == VD_OP_BREAD)
680690555a1Sachartre 			bcopy(maddr + moffset, data, mlen);
681690555a1Sachartre 		else
682690555a1Sachartre 			bcopy(data, maddr + moffset, mlen);
683690555a1Sachartre 
684690555a1Sachartre 		if (segmap_fault(kas.a_hat, segkmap, maddr, mlen,
685690555a1Sachartre 		    F_SOFTUNLOCK, srw) != 0) {
686690555a1Sachartre 			(void) segmap_release(segkmap, maddr, 0);
687690555a1Sachartre 			return (-1);
688690555a1Sachartre 		}
689690555a1Sachartre 		if (segmap_release(segkmap, maddr, smflags) != 0)
690690555a1Sachartre 			return (-1);
691690555a1Sachartre 		n -= mlen;
692690555a1Sachartre 		offset += mlen;
693690555a1Sachartre 		data += mlen;
694690555a1Sachartre 
695690555a1Sachartre 	} while (n > 0);
696690555a1Sachartre 
697690555a1Sachartre 	return (len);
698690555a1Sachartre }
699690555a1Sachartre 
70087a7269eSachartre /*
70187a7269eSachartre  * Function:
70278fcd0a1Sachartre  *	vd_file_build_default_label
70378fcd0a1Sachartre  *
70478fcd0a1Sachartre  * Description:
70578fcd0a1Sachartre  *	Return a default label for the given disk. This is used when the disk
70678fcd0a1Sachartre  *	does not have a valid VTOC so that the user can get a valid default
70717cadca8Slm66018  *	configuration. The default label has all slice sizes set to 0 (except
70878fcd0a1Sachartre  *	slice 2 which is the entire disk) to force the user to write a valid
70978fcd0a1Sachartre  *	label onto the disk image.
71078fcd0a1Sachartre  *
71178fcd0a1Sachartre  * Parameters:
71278fcd0a1Sachartre  *	vd		- disk on which the operation is performed.
71378fcd0a1Sachartre  *	label		- the returned default label.
71478fcd0a1Sachartre  *
71578fcd0a1Sachartre  * Return Code:
71678fcd0a1Sachartre  *	none.
71778fcd0a1Sachartre  */
71878fcd0a1Sachartre static void
71978fcd0a1Sachartre vd_file_build_default_label(vd_t *vd, struct dk_label *label)
72078fcd0a1Sachartre {
72178fcd0a1Sachartre 	size_t size;
72278fcd0a1Sachartre 	char prefix;
72378fcd0a1Sachartre 
72478fcd0a1Sachartre 	ASSERT(vd->file);
725edcc0754Sachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
726edcc0754Sachartre 
727edcc0754Sachartre 	bzero(label, sizeof (struct dk_label));
72878fcd0a1Sachartre 
72978fcd0a1Sachartre 	/*
73078fcd0a1Sachartre 	 * We must have a resonable number of cylinders and sectors so
73178fcd0a1Sachartre 	 * that newfs can run using default values.
73278fcd0a1Sachartre 	 *
73378fcd0a1Sachartre 	 * if (disk_size < 2MB)
73478fcd0a1Sachartre 	 * 	phys_cylinders = disk_size / 100K
73578fcd0a1Sachartre 	 * else
73678fcd0a1Sachartre 	 * 	phys_cylinders = disk_size / 300K
73778fcd0a1Sachartre 	 *
73878fcd0a1Sachartre 	 * phys_cylinders = (phys_cylinders == 0) ? 1 : phys_cylinders
73978fcd0a1Sachartre 	 * alt_cylinders = (phys_cylinders > 2) ? 2 : 0;
74078fcd0a1Sachartre 	 * data_cylinders = phys_cylinders - alt_cylinders
74178fcd0a1Sachartre 	 *
74278fcd0a1Sachartre 	 * sectors = disk_size / (phys_cylinders * blk_size)
74378fcd0a1Sachartre 	 *
74478fcd0a1Sachartre 	 * The file size test is an attempt to not have too few cylinders
74578fcd0a1Sachartre 	 * for a small file, or so many on a big file that you waste space
74678fcd0a1Sachartre 	 * for backup superblocks or cylinder group structures.
74778fcd0a1Sachartre 	 */
74878fcd0a1Sachartre 	if (vd->file_size < (2 * 1024 * 1024))
74978fcd0a1Sachartre 		label->dkl_pcyl = vd->file_size / (100 * 1024);
75078fcd0a1Sachartre 	else
75178fcd0a1Sachartre 		label->dkl_pcyl = vd->file_size / (300 * 1024);
75278fcd0a1Sachartre 
75378fcd0a1Sachartre 	if (label->dkl_pcyl == 0)
75478fcd0a1Sachartre 		label->dkl_pcyl = 1;
75578fcd0a1Sachartre 
756047ba61eSachartre 	label->dkl_acyl = 0;
757047ba61eSachartre 
75878fcd0a1Sachartre 	if (label->dkl_pcyl > 2)
75978fcd0a1Sachartre 		label->dkl_acyl = 2;
76078fcd0a1Sachartre 
76178fcd0a1Sachartre 	label->dkl_nsect = vd->file_size /
76278fcd0a1Sachartre 	    (DEV_BSIZE * label->dkl_pcyl);
76378fcd0a1Sachartre 	label->dkl_ncyl = label->dkl_pcyl - label->dkl_acyl;
76478fcd0a1Sachartre 	label->dkl_nhead = 1;
76578fcd0a1Sachartre 	label->dkl_write_reinstruct = 0;
76678fcd0a1Sachartre 	label->dkl_read_reinstruct = 0;
76778fcd0a1Sachartre 	label->dkl_rpm = 7200;
76878fcd0a1Sachartre 	label->dkl_apc = 0;
76978fcd0a1Sachartre 	label->dkl_intrlv = 0;
77078fcd0a1Sachartre 
77178fcd0a1Sachartre 	PR0("requested disk size: %ld bytes\n", vd->file_size);
77278fcd0a1Sachartre 	PR0("setup: ncyl=%d nhead=%d nsec=%d\n", label->dkl_pcyl,
77378fcd0a1Sachartre 	    label->dkl_nhead, label->dkl_nsect);
77478fcd0a1Sachartre 	PR0("provided disk size: %ld bytes\n", (uint64_t)
77578fcd0a1Sachartre 	    (label->dkl_pcyl * label->dkl_nhead *
77678fcd0a1Sachartre 	    label->dkl_nsect * DEV_BSIZE));
77778fcd0a1Sachartre 
77878fcd0a1Sachartre 	if (vd->file_size < (1ULL << 20)) {
77978fcd0a1Sachartre 		size = vd->file_size >> 10;
78078fcd0a1Sachartre 		prefix = 'K'; /* Kilobyte */
78178fcd0a1Sachartre 	} else if (vd->file_size < (1ULL << 30)) {
78278fcd0a1Sachartre 		size = vd->file_size >> 20;
78378fcd0a1Sachartre 		prefix = 'M'; /* Megabyte */
78478fcd0a1Sachartre 	} else if (vd->file_size < (1ULL << 40)) {
78578fcd0a1Sachartre 		size = vd->file_size >> 30;
78678fcd0a1Sachartre 		prefix = 'G'; /* Gigabyte */
78778fcd0a1Sachartre 	} else {
78878fcd0a1Sachartre 		size = vd->file_size >> 40;
78978fcd0a1Sachartre 		prefix = 'T'; /* Terabyte */
79078fcd0a1Sachartre 	}
79178fcd0a1Sachartre 
79278fcd0a1Sachartre 	/*
79378fcd0a1Sachartre 	 * We must have a correct label name otherwise format(1m) will
79478fcd0a1Sachartre 	 * not recognized the disk as labeled.
79578fcd0a1Sachartre 	 */
79678fcd0a1Sachartre 	(void) snprintf(label->dkl_asciilabel, LEN_DKL_ASCII,
79778fcd0a1Sachartre 	    "SUN-DiskImage-%ld%cB cyl %d alt %d hd %d sec %d",
79878fcd0a1Sachartre 	    size, prefix,
79978fcd0a1Sachartre 	    label->dkl_ncyl, label->dkl_acyl, label->dkl_nhead,
80078fcd0a1Sachartre 	    label->dkl_nsect);
80178fcd0a1Sachartre 
80278fcd0a1Sachartre 	/* default VTOC */
80378fcd0a1Sachartre 	label->dkl_vtoc.v_version = V_VERSION;
804edcc0754Sachartre 	label->dkl_vtoc.v_nparts = V_NUMPAR;
80578fcd0a1Sachartre 	label->dkl_vtoc.v_sanity = VTOC_SANE;
806edcc0754Sachartre 	label->dkl_vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_tag = V_BACKUP;
807edcc0754Sachartre 	label->dkl_map[VD_ENTIRE_DISK_SLICE].dkl_cylno = 0;
808edcc0754Sachartre 	label->dkl_map[VD_ENTIRE_DISK_SLICE].dkl_nblk = label->dkl_ncyl *
80978fcd0a1Sachartre 	    label->dkl_nhead * label->dkl_nsect;
810edcc0754Sachartre 	label->dkl_magic = DKL_MAGIC;
81178fcd0a1Sachartre 	label->dkl_cksum = vd_lbl2cksum(label);
81278fcd0a1Sachartre }
81378fcd0a1Sachartre 
81478fcd0a1Sachartre /*
81578fcd0a1Sachartre  * Function:
81687a7269eSachartre  *	vd_file_set_vtoc
81787a7269eSachartre  *
81887a7269eSachartre  * Description:
81987a7269eSachartre  *	Set the vtoc of a disk image by writing the label and backup
82087a7269eSachartre  *	labels into the disk image backend.
82187a7269eSachartre  *
82287a7269eSachartre  * Parameters:
82387a7269eSachartre  *	vd		- disk on which the operation is performed.
82487a7269eSachartre  *	label		- the data to be written.
82587a7269eSachartre  *
82687a7269eSachartre  * Return Code:
82787a7269eSachartre  *	0		- success.
82887a7269eSachartre  *	n > 0		- error, n indicates the errno code.
82987a7269eSachartre  */
83087a7269eSachartre static int
83187a7269eSachartre vd_file_set_vtoc(vd_t *vd, struct dk_label *label)
83287a7269eSachartre {
83387a7269eSachartre 	int blk, sec, cyl, head, cnt;
83487a7269eSachartre 
83587a7269eSachartre 	ASSERT(vd->file);
83687a7269eSachartre 
83787a7269eSachartre 	if (VD_FILE_LABEL_WRITE(vd, label) < 0) {
83887a7269eSachartre 		PR0("fail to write disk label");
83987a7269eSachartre 		return (EIO);
84087a7269eSachartre 	}
84187a7269eSachartre 
84287a7269eSachartre 	/*
84387a7269eSachartre 	 * Backup labels are on the last alternate cylinder's
84487a7269eSachartre 	 * first five odd sectors.
84587a7269eSachartre 	 */
84687a7269eSachartre 	if (label->dkl_acyl == 0) {
84787a7269eSachartre 		PR0("no alternate cylinder, can not store backup labels");
84887a7269eSachartre 		return (0);
84987a7269eSachartre 	}
85087a7269eSachartre 
85187a7269eSachartre 	cyl = label->dkl_ncyl  + label->dkl_acyl - 1;
85287a7269eSachartre 	head = label->dkl_nhead - 1;
85387a7269eSachartre 
85487a7269eSachartre 	blk = (cyl * ((label->dkl_nhead * label->dkl_nsect) - label->dkl_apc)) +
85587a7269eSachartre 	    (head * label->dkl_nsect);
85687a7269eSachartre 
85787a7269eSachartre 	/*
85887a7269eSachartre 	 * Write the backup labels. Make sure we don't try to write past
85987a7269eSachartre 	 * the last cylinder.
86087a7269eSachartre 	 */
86187a7269eSachartre 	sec = 1;
86287a7269eSachartre 
86387a7269eSachartre 	for (cnt = 0; cnt < VD_FILE_NUM_BACKUP; cnt++) {
86487a7269eSachartre 
86587a7269eSachartre 		if (sec >= label->dkl_nsect) {
86687a7269eSachartre 			PR0("not enough sector to store all backup labels");
86787a7269eSachartre 			return (0);
86887a7269eSachartre 		}
86987a7269eSachartre 
87087a7269eSachartre 		if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)label,
87187a7269eSachartre 		    blk + sec, sizeof (struct dk_label)) < 0) {
87287a7269eSachartre 			PR0("error writing backup label at block %d\n",
87387a7269eSachartre 			    blk + sec);
87487a7269eSachartre 			return (EIO);
87587a7269eSachartre 		}
87687a7269eSachartre 
87787a7269eSachartre 		PR1("wrote backup label at block %d\n", blk + sec);
87887a7269eSachartre 
87987a7269eSachartre 		sec += 2;
88087a7269eSachartre 	}
88187a7269eSachartre 
88287a7269eSachartre 	return (0);
88387a7269eSachartre }
88487a7269eSachartre 
88587a7269eSachartre /*
88687a7269eSachartre  * Function:
88787a7269eSachartre  *	vd_file_get_devid_block
88887a7269eSachartre  *
88987a7269eSachartre  * Description:
89087a7269eSachartre  *	Return the block number where the device id is stored.
89187a7269eSachartre  *
89287a7269eSachartre  * Parameters:
89387a7269eSachartre  *	vd		- disk on which the operation is performed.
89487a7269eSachartre  *	blkp		- pointer to the block number
89587a7269eSachartre  *
89687a7269eSachartre  * Return Code:
89787a7269eSachartre  *	0		- success
89887a7269eSachartre  *	ENOSPC		- disk has no space to store a device id
89987a7269eSachartre  */
90087a7269eSachartre static int
90187a7269eSachartre vd_file_get_devid_block(vd_t *vd, size_t *blkp)
90287a7269eSachartre {
90387a7269eSachartre 	diskaddr_t spc, head, cyl;
90487a7269eSachartre 
90587a7269eSachartre 	ASSERT(vd->file);
906edcc0754Sachartre 
907edcc0754Sachartre 	if (vd->vdisk_label == VD_DISK_LABEL_UNK) {
908edcc0754Sachartre 		/*
909edcc0754Sachartre 		 * If no label is defined we don't know where to find
910edcc0754Sachartre 		 * a device id.
911edcc0754Sachartre 		 */
912edcc0754Sachartre 		return (ENOSPC);
913edcc0754Sachartre 	}
914edcc0754Sachartre 
915edcc0754Sachartre 	if (vd->vdisk_label == VD_DISK_LABEL_EFI) {
916edcc0754Sachartre 		/*
917edcc0754Sachartre 		 * For an EFI disk, the devid is at the beginning of
918edcc0754Sachartre 		 * the reserved slice
919edcc0754Sachartre 		 */
920edcc0754Sachartre 		if (vd->efi_reserved == -1) {
921edcc0754Sachartre 			PR0("EFI disk has no reserved slice");
922edcc0754Sachartre 			return (ENOSPC);
923edcc0754Sachartre 		}
924edcc0754Sachartre 
925edcc0754Sachartre 		*blkp = vd->slices[vd->efi_reserved].start;
926edcc0754Sachartre 		return (0);
927edcc0754Sachartre 	}
928edcc0754Sachartre 
92987a7269eSachartre 	ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
93087a7269eSachartre 
93187a7269eSachartre 	/* this geometry doesn't allow us to have a devid */
93287a7269eSachartre 	if (vd->dk_geom.dkg_acyl < 2) {
93387a7269eSachartre 		PR0("not enough alternate cylinder available for devid "
93487a7269eSachartre 		    "(acyl=%u)", vd->dk_geom.dkg_acyl);
93587a7269eSachartre 		return (ENOSPC);
93687a7269eSachartre 	}
93787a7269eSachartre 
93887a7269eSachartre 	/* the devid is in on the track next to the last cylinder */
93987a7269eSachartre 	cyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl - 2;
94087a7269eSachartre 	spc = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
94187a7269eSachartre 	head = vd->dk_geom.dkg_nhead - 1;
94287a7269eSachartre 
94387a7269eSachartre 	*blkp = (cyl * (spc - vd->dk_geom.dkg_apc)) +
94487a7269eSachartre 	    (head * vd->dk_geom.dkg_nsect) + 1;
94587a7269eSachartre 
94687a7269eSachartre 	return (0);
94787a7269eSachartre }
94887a7269eSachartre 
94987a7269eSachartre /*
95087a7269eSachartre  * Return the checksum of a disk block containing an on-disk devid.
95187a7269eSachartre  */
95287a7269eSachartre static uint_t
95387a7269eSachartre vd_dkdevid2cksum(struct dk_devid *dkdevid)
95487a7269eSachartre {
95587a7269eSachartre 	uint_t chksum, *ip;
95687a7269eSachartre 	int i;
95787a7269eSachartre 
95887a7269eSachartre 	chksum = 0;
95987a7269eSachartre 	ip = (uint_t *)dkdevid;
96087a7269eSachartre 	for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int)); i++)
96187a7269eSachartre 		chksum ^= ip[i];
96287a7269eSachartre 
96387a7269eSachartre 	return (chksum);
96487a7269eSachartre }
96587a7269eSachartre 
96687a7269eSachartre /*
96787a7269eSachartre  * Function:
96887a7269eSachartre  *	vd_file_read_devid
96987a7269eSachartre  *
97087a7269eSachartre  * Description:
97187a7269eSachartre  *	Read the device id stored on a disk image.
97287a7269eSachartre  *
97387a7269eSachartre  * Parameters:
97487a7269eSachartre  *	vd		- disk on which the operation is performed.
97587a7269eSachartre  *	devid		- the return address of the device ID.
97687a7269eSachartre  *
97787a7269eSachartre  * Return Code:
97887a7269eSachartre  *	0		- success
97987a7269eSachartre  *	EIO		- I/O error while trying to access the disk image
98087a7269eSachartre  *	EINVAL		- no valid device id was found
98187a7269eSachartre  *	ENOSPC		- disk has no space to store a device id
98287a7269eSachartre  */
98387a7269eSachartre static int
98487a7269eSachartre vd_file_read_devid(vd_t *vd, ddi_devid_t *devid)
98587a7269eSachartre {
98687a7269eSachartre 	struct dk_devid *dkdevid;
98787a7269eSachartre 	size_t blk;
98887a7269eSachartre 	uint_t chksum;
98987a7269eSachartre 	int status, sz;
99087a7269eSachartre 
99187a7269eSachartre 	if ((status = vd_file_get_devid_block(vd, &blk)) != 0)
99287a7269eSachartre 		return (status);
99387a7269eSachartre 
99487a7269eSachartre 	dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP);
99587a7269eSachartre 
99687a7269eSachartre 	/* get the devid */
99787a7269eSachartre 	if ((vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)dkdevid, blk,
99887a7269eSachartre 	    DEV_BSIZE)) < 0) {
99987a7269eSachartre 		PR0("error reading devid block at %lu", blk);
100087a7269eSachartre 		status = EIO;
100187a7269eSachartre 		goto done;
100287a7269eSachartre 	}
100387a7269eSachartre 
100487a7269eSachartre 	/* validate the revision */
100587a7269eSachartre 	if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
100687a7269eSachartre 	    (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
100787a7269eSachartre 		PR0("invalid devid found at block %lu (bad revision)", blk);
100887a7269eSachartre 		status = EINVAL;
100987a7269eSachartre 		goto done;
101087a7269eSachartre 	}
101187a7269eSachartre 
101287a7269eSachartre 	/* compute checksum */
101387a7269eSachartre 	chksum = vd_dkdevid2cksum(dkdevid);
101487a7269eSachartre 
101587a7269eSachartre 	/* compare the checksums */
101687a7269eSachartre 	if (DKD_GETCHKSUM(dkdevid) != chksum) {
101787a7269eSachartre 		PR0("invalid devid found at block %lu (bad checksum)", blk);
101887a7269eSachartre 		status = EINVAL;
101987a7269eSachartre 		goto done;
102087a7269eSachartre 	}
102187a7269eSachartre 
102287a7269eSachartre 	/* validate the device id */
102387a7269eSachartre 	if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
102487a7269eSachartre 		PR0("invalid devid found at block %lu", blk);
102587a7269eSachartre 		status = EINVAL;
102687a7269eSachartre 		goto done;
102787a7269eSachartre 	}
102887a7269eSachartre 
102987a7269eSachartre 	PR1("devid read at block %lu", blk);
103087a7269eSachartre 
103187a7269eSachartre 	sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
103287a7269eSachartre 	*devid = kmem_alloc(sz, KM_SLEEP);
103387a7269eSachartre 	bcopy(&dkdevid->dkd_devid, *devid, sz);
103487a7269eSachartre 
103587a7269eSachartre done:
103687a7269eSachartre 	kmem_free(dkdevid, DEV_BSIZE);
103787a7269eSachartre 	return (status);
103887a7269eSachartre 
103987a7269eSachartre }
104087a7269eSachartre 
104187a7269eSachartre /*
104287a7269eSachartre  * Function:
104387a7269eSachartre  *	vd_file_write_devid
104487a7269eSachartre  *
104587a7269eSachartre  * Description:
104687a7269eSachartre  *	Write a device id into disk image.
104787a7269eSachartre  *
104887a7269eSachartre  * Parameters:
104987a7269eSachartre  *	vd		- disk on which the operation is performed.
105087a7269eSachartre  *	devid		- the device ID to store.
105187a7269eSachartre  *
105287a7269eSachartre  * Return Code:
105387a7269eSachartre  *	0		- success
105487a7269eSachartre  *	EIO		- I/O error while trying to access the disk image
105587a7269eSachartre  *	ENOSPC		- disk has no space to store a device id
105687a7269eSachartre  */
105787a7269eSachartre static int
105887a7269eSachartre vd_file_write_devid(vd_t *vd, ddi_devid_t devid)
105987a7269eSachartre {
106087a7269eSachartre 	struct dk_devid *dkdevid;
106187a7269eSachartre 	uint_t chksum;
106287a7269eSachartre 	size_t blk;
106387a7269eSachartre 	int status;
106487a7269eSachartre 
1065edcc0754Sachartre 	if (devid == NULL) {
1066edcc0754Sachartre 		/* nothing to write */
1067edcc0754Sachartre 		return (0);
1068edcc0754Sachartre 	}
1069edcc0754Sachartre 
107087a7269eSachartre 	if ((status = vd_file_get_devid_block(vd, &blk)) != 0)
107187a7269eSachartre 		return (status);
107287a7269eSachartre 
107387a7269eSachartre 	dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP);
107487a7269eSachartre 
107587a7269eSachartre 	/* set revision */
107687a7269eSachartre 	dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
107787a7269eSachartre 	dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
107887a7269eSachartre 
107987a7269eSachartre 	/* copy devid */
108087a7269eSachartre 	bcopy(devid, &dkdevid->dkd_devid, ddi_devid_sizeof(devid));
108187a7269eSachartre 
108287a7269eSachartre 	/* compute checksum */
108387a7269eSachartre 	chksum = vd_dkdevid2cksum(dkdevid);
108487a7269eSachartre 
108587a7269eSachartre 	/* set checksum */
108687a7269eSachartre 	DKD_FORMCHKSUM(chksum, dkdevid);
108787a7269eSachartre 
108887a7269eSachartre 	/* store the devid */
108987a7269eSachartre 	if ((status = vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE,
109087a7269eSachartre 	    (caddr_t)dkdevid, blk, DEV_BSIZE)) < 0) {
109187a7269eSachartre 		PR0("Error writing devid block at %lu", blk);
109287a7269eSachartre 		status = EIO;
109387a7269eSachartre 	} else {
109487a7269eSachartre 		PR1("devid written at block %lu", blk);
109587a7269eSachartre 		status = 0;
109687a7269eSachartre 	}
109787a7269eSachartre 
109887a7269eSachartre 	kmem_free(dkdevid, DEV_BSIZE);
109987a7269eSachartre 	return (status);
110087a7269eSachartre }
110187a7269eSachartre 
110287a7269eSachartre /*
110387a7269eSachartre  * Function:
110417cadca8Slm66018  *	vd_do_scsi_rdwr
110587a7269eSachartre  *
110687a7269eSachartre  * Description:
110787a7269eSachartre  * 	Read or write to a SCSI disk using an absolute disk offset.
110887a7269eSachartre  *
110987a7269eSachartre  * Parameters:
111087a7269eSachartre  *	vd		- disk on which the operation is performed.
111187a7269eSachartre  *	operation	- operation to execute: read (VD_OP_BREAD) or
111287a7269eSachartre  *			  write (VD_OP_BWRITE).
111387a7269eSachartre  *	data		- buffer where data are read to or written from.
111487a7269eSachartre  *	blk		- starting block for the operation.
111587a7269eSachartre  *	len		- number of bytes to read or write.
111687a7269eSachartre  *
111787a7269eSachartre  * Return Code:
111887a7269eSachartre  *	0		- success
111987a7269eSachartre  *	n != 0		- error.
112087a7269eSachartre  */
112187a7269eSachartre static int
112217cadca8Slm66018 vd_do_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t blk, size_t len)
112387a7269eSachartre {
112487a7269eSachartre 	struct uscsi_cmd ucmd;
112587a7269eSachartre 	union scsi_cdb cdb;
112687a7269eSachartre 	int nsectors, nblk;
112787a7269eSachartre 	int max_sectors;
112887a7269eSachartre 	int status, rval;
112987a7269eSachartre 
113087a7269eSachartre 	ASSERT(!vd->file);
113117cadca8Slm66018 	ASSERT(vd->vdisk_block_size > 0);
113287a7269eSachartre 
113387a7269eSachartre 	max_sectors = vd->max_xfer_sz;
113417cadca8Slm66018 	nblk = (len / vd->vdisk_block_size);
113587a7269eSachartre 
113617cadca8Slm66018 	if (len % vd->vdisk_block_size != 0)
113787a7269eSachartre 		return (EINVAL);
113887a7269eSachartre 
113987a7269eSachartre 	/*
114087a7269eSachartre 	 * Build and execute the uscsi ioctl.  We build a group0, group1
114187a7269eSachartre 	 * or group4 command as necessary, since some targets
114287a7269eSachartre 	 * do not support group1 commands.
114387a7269eSachartre 	 */
114487a7269eSachartre 	while (nblk) {
114587a7269eSachartre 
114687a7269eSachartre 		bzero(&ucmd, sizeof (ucmd));
114787a7269eSachartre 		bzero(&cdb, sizeof (cdb));
114887a7269eSachartre 
114987a7269eSachartre 		nsectors = (max_sectors < nblk) ? max_sectors : nblk;
115087a7269eSachartre 
115117cadca8Slm66018 		/*
115217cadca8Slm66018 		 * Some of the optical drives on sun4v machines are ATAPI
115317cadca8Slm66018 		 * devices which use Group 1 Read/Write commands so we need
115417cadca8Slm66018 		 * to explicitly check a flag which is set when a domain
115517cadca8Slm66018 		 * is bound.
115617cadca8Slm66018 		 */
115717cadca8Slm66018 		if (blk < (2 << 20) && nsectors <= 0xff && !vd->is_atapi_dev) {
115887a7269eSachartre 			FORMG0ADDR(&cdb, blk);
115987a7269eSachartre 			FORMG0COUNT(&cdb, nsectors);
116087a7269eSachartre 			ucmd.uscsi_cdblen = CDB_GROUP0;
116187a7269eSachartre 		} else if (blk > 0xffffffff) {
116287a7269eSachartre 			FORMG4LONGADDR(&cdb, blk);
116387a7269eSachartre 			FORMG4COUNT(&cdb, nsectors);
116487a7269eSachartre 			ucmd.uscsi_cdblen = CDB_GROUP4;
116587a7269eSachartre 			cdb.scc_cmd |= SCMD_GROUP4;
116687a7269eSachartre 		} else {
116787a7269eSachartre 			FORMG1ADDR(&cdb, blk);
116887a7269eSachartre 			FORMG1COUNT(&cdb, nsectors);
116987a7269eSachartre 			ucmd.uscsi_cdblen = CDB_GROUP1;
117087a7269eSachartre 			cdb.scc_cmd |= SCMD_GROUP1;
117187a7269eSachartre 		}
117287a7269eSachartre 		ucmd.uscsi_cdb = (caddr_t)&cdb;
117387a7269eSachartre 		ucmd.uscsi_bufaddr = data;
117417cadca8Slm66018 		ucmd.uscsi_buflen = nsectors * vd->block_size;
117587a7269eSachartre 		ucmd.uscsi_timeout = vd_scsi_rdwr_timeout;
117687a7269eSachartre 		/*
117787a7269eSachartre 		 * Set flags so that the command is isolated from normal
117887a7269eSachartre 		 * commands and no error message is printed.
117987a7269eSachartre 		 */
118087a7269eSachartre 		ucmd.uscsi_flags = USCSI_ISOLATE | USCSI_SILENT;
118187a7269eSachartre 
118287a7269eSachartre 		if (operation == VD_OP_BREAD) {
118387a7269eSachartre 			cdb.scc_cmd |= SCMD_READ;
118487a7269eSachartre 			ucmd.uscsi_flags |= USCSI_READ;
118587a7269eSachartre 		} else {
118687a7269eSachartre 			cdb.scc_cmd |= SCMD_WRITE;
118787a7269eSachartre 		}
118887a7269eSachartre 
118987a7269eSachartre 		status = ldi_ioctl(vd->ldi_handle[VD_ENTIRE_DISK_SLICE],
1190047ba61eSachartre 		    USCSICMD, (intptr_t)&ucmd, (vd->open_flags | FKIOCTL),
119187a7269eSachartre 		    kcred, &rval);
119287a7269eSachartre 
119387a7269eSachartre 		if (status == 0)
119487a7269eSachartre 			status = ucmd.uscsi_status;
119587a7269eSachartre 
119687a7269eSachartre 		if (status != 0)
119787a7269eSachartre 			break;
119887a7269eSachartre 
119987a7269eSachartre 		/*
120087a7269eSachartre 		 * Check if partial DMA breakup is required. If so, reduce
120187a7269eSachartre 		 * the request size by half and retry the last request.
120287a7269eSachartre 		 */
120387a7269eSachartre 		if (ucmd.uscsi_resid == ucmd.uscsi_buflen) {
120487a7269eSachartre 			max_sectors >>= 1;
120587a7269eSachartre 			if (max_sectors <= 0) {
120687a7269eSachartre 				status = EIO;
120787a7269eSachartre 				break;
120887a7269eSachartre 			}
120987a7269eSachartre 			continue;
121087a7269eSachartre 		}
121187a7269eSachartre 
121287a7269eSachartre 		if (ucmd.uscsi_resid != 0) {
121387a7269eSachartre 			status = EIO;
121487a7269eSachartre 			break;
121587a7269eSachartre 		}
121687a7269eSachartre 
121787a7269eSachartre 		blk += nsectors;
121887a7269eSachartre 		nblk -= nsectors;
121917cadca8Slm66018 		data += nsectors * vd->vdisk_block_size; /* SECSIZE */
122087a7269eSachartre 	}
122187a7269eSachartre 
122287a7269eSachartre 	return (status);
122387a7269eSachartre }
122487a7269eSachartre 
1225205eeb1aSlm66018 /*
122617cadca8Slm66018  * Function:
122717cadca8Slm66018  *	vd_scsi_rdwr
122817cadca8Slm66018  *
122917cadca8Slm66018  * Description:
123017cadca8Slm66018  * 	Wrapper function to read or write to a SCSI disk using an absolute
123117cadca8Slm66018  *	disk offset. It checks the blocksize of the underlying device and,
123217cadca8Slm66018  *	if necessary, adjusts the buffers accordingly before calling
123317cadca8Slm66018  *	vd_do_scsi_rdwr() to do the actual read or write.
123417cadca8Slm66018  *
123517cadca8Slm66018  * Parameters:
123617cadca8Slm66018  *	vd		- disk on which the operation is performed.
123717cadca8Slm66018  *	operation	- operation to execute: read (VD_OP_BREAD) or
123817cadca8Slm66018  *			  write (VD_OP_BWRITE).
123917cadca8Slm66018  *	data		- buffer where data are read to or written from.
124017cadca8Slm66018  *	blk		- starting block for the operation.
124117cadca8Slm66018  *	len		- number of bytes to read or write.
124217cadca8Slm66018  *
124317cadca8Slm66018  * Return Code:
124417cadca8Slm66018  *	0		- success
124517cadca8Slm66018  *	n != 0		- error.
124617cadca8Slm66018  */
124717cadca8Slm66018 static int
124817cadca8Slm66018 vd_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t vblk, size_t vlen)
124917cadca8Slm66018 {
125017cadca8Slm66018 	int	rv;
125117cadca8Slm66018 
125217cadca8Slm66018 	size_t	pblk;	/* physical device block number of data on device */
125317cadca8Slm66018 	size_t	delta;	/* relative offset between pblk and vblk */
125417cadca8Slm66018 	size_t	pnblk;	/* number of physical blocks to be read from device */
125517cadca8Slm66018 	size_t	plen;	/* length of data to be read from physical device */
125617cadca8Slm66018 	char	*buf;	/* buffer area to fit physical device's block size */
125717cadca8Slm66018 
12582f5224aeSachartre 	if (vd->block_size == 0) {
12592f5224aeSachartre 		/*
12602f5224aeSachartre 		 * The block size was not available during the attach,
12612f5224aeSachartre 		 * try to update it now.
12622f5224aeSachartre 		 */
12632f5224aeSachartre 		if (vd_setup_mediainfo(vd) != 0)
12642f5224aeSachartre 			return (EIO);
12652f5224aeSachartre 	}
12662f5224aeSachartre 
126717cadca8Slm66018 	/*
126817cadca8Slm66018 	 * If the vdisk block size and the block size of the underlying device
126917cadca8Slm66018 	 * match we can skip straight to vd_do_scsi_rdwr(), otherwise we need
127017cadca8Slm66018 	 * to create a buffer large enough to handle the device's block size
127117cadca8Slm66018 	 * and adjust the block to be read from and the amount of data to
127217cadca8Slm66018 	 * read to correspond with the device's block size.
127317cadca8Slm66018 	 */
127417cadca8Slm66018 	if (vd->vdisk_block_size == vd->block_size)
127517cadca8Slm66018 		return (vd_do_scsi_rdwr(vd, operation, data, vblk, vlen));
127617cadca8Slm66018 
127717cadca8Slm66018 	if (vd->vdisk_block_size > vd->block_size)
127817cadca8Slm66018 		return (EINVAL);
127917cadca8Slm66018 
128017cadca8Slm66018 	/*
128117cadca8Slm66018 	 * Writing of physical block sizes larger than the virtual block size
128217cadca8Slm66018 	 * is not supported. This would be added if/when support for guests
128317cadca8Slm66018 	 * writing to DVDs is implemented.
128417cadca8Slm66018 	 */
128517cadca8Slm66018 	if (operation == VD_OP_BWRITE)
128617cadca8Slm66018 		return (ENOTSUP);
128717cadca8Slm66018 
128817cadca8Slm66018 	/* BEGIN CSTYLED */
128917cadca8Slm66018 	/*
129017cadca8Slm66018 	 * Below is a diagram showing the relationship between the physical
129117cadca8Slm66018 	 * and virtual blocks. If the virtual blocks marked by 'X' below are
129217cadca8Slm66018 	 * requested, then the physical blocks denoted by 'Y' are read.
129317cadca8Slm66018 	 *
129417cadca8Slm66018 	 *           vblk
129517cadca8Slm66018 	 *             |      vlen
129617cadca8Slm66018 	 *             |<--------------->|
129717cadca8Slm66018 	 *             v                 v
129817cadca8Slm66018 	 *  --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-   virtual disk:
129917cadca8Slm66018 	 *    |  |  |  |XX|XX|XX|XX|XX|XX|  |  |  |  |  |  } block size is
130017cadca8Slm66018 	 *  --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-  vd->vdisk_block_size
130117cadca8Slm66018 	 *          :  :                 :  :
130217cadca8Slm66018 	 *         >:==:< delta          :  :
130317cadca8Slm66018 	 *          :  :                 :  :
130417cadca8Slm66018 	 *  --+-----+-----+-----+-----+-----+-----+-----+--   physical disk:
130517cadca8Slm66018 	 *    |     |YY:YY|YYYYY|YYYYY|YY:YY|     |     |   } block size is
130617cadca8Slm66018 	 *  --+-----+-----+-----+-----+-----+-----+-----+--   vd->block_size
130717cadca8Slm66018 	 *          ^                       ^
130817cadca8Slm66018 	 *          |<--------------------->|
130917cadca8Slm66018 	 *          |         plen
131017cadca8Slm66018 	 *         pblk
131117cadca8Slm66018 	 */
131217cadca8Slm66018 	/* END CSTYLED */
131317cadca8Slm66018 	pblk = (vblk * vd->vdisk_block_size) / vd->block_size;
131417cadca8Slm66018 	delta = (vblk * vd->vdisk_block_size) - (pblk * vd->block_size);
131517cadca8Slm66018 	pnblk = ((delta + vlen - 1) / vd->block_size) + 1;
131617cadca8Slm66018 	plen = pnblk * vd->block_size;
131717cadca8Slm66018 
131817cadca8Slm66018 	PR2("vblk %lx:pblk %lx: vlen %ld:plen %ld", vblk, pblk, vlen, plen);
131917cadca8Slm66018 
132017cadca8Slm66018 	buf = kmem_zalloc(sizeof (caddr_t) * plen, KM_SLEEP);
132117cadca8Slm66018 	rv = vd_do_scsi_rdwr(vd, operation, (caddr_t)buf, pblk, plen);
132217cadca8Slm66018 	bcopy(buf + delta, data, vlen);
132317cadca8Slm66018 
132417cadca8Slm66018 	kmem_free(buf, sizeof (caddr_t) * plen);
132517cadca8Slm66018 
132617cadca8Slm66018 	return (rv);
132717cadca8Slm66018 }
132817cadca8Slm66018 
132917cadca8Slm66018 /*
1330205eeb1aSlm66018  * Return Values
1331205eeb1aSlm66018  *	EINPROGRESS	- operation was successfully started
1332205eeb1aSlm66018  *	EIO		- encountered LDC (aka. task error)
1333205eeb1aSlm66018  *	0		- operation completed successfully
1334205eeb1aSlm66018  *
1335205eeb1aSlm66018  * Side Effect
1336205eeb1aSlm66018  *     sets request->status = <disk operation status>
1337205eeb1aSlm66018  */
13381ae08745Sheppo static int
1339d10e4ef2Snarayan vd_start_bio(vd_task_t *task)
13401ae08745Sheppo {
13414bac2208Snarayan 	int			rv, status = 0;
1342d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
1343d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
1344d10e4ef2Snarayan 	struct buf		*buf		= &task->buf;
13454bac2208Snarayan 	uint8_t			mtype;
13463c96341aSnarayan 	int 			slice;
1347047ba61eSachartre 	char			*bufaddr = 0;
1348047ba61eSachartre 	size_t			buflen;
1349d10e4ef2Snarayan 
1350d10e4ef2Snarayan 	ASSERT(vd != NULL);
1351d10e4ef2Snarayan 	ASSERT(request != NULL);
13523c96341aSnarayan 
13533c96341aSnarayan 	slice = request->slice;
13543c96341aSnarayan 
135587a7269eSachartre 	ASSERT(slice == VD_SLICE_NONE || slice < vd->nslices);
1356d10e4ef2Snarayan 	ASSERT((request->operation == VD_OP_BREAD) ||
1357d10e4ef2Snarayan 	    (request->operation == VD_OP_BWRITE));
1358d10e4ef2Snarayan 
1359205eeb1aSlm66018 	if (request->nbytes == 0) {
1360205eeb1aSlm66018 		/* no service for trivial requests */
1361205eeb1aSlm66018 		request->status = EINVAL;
1362205eeb1aSlm66018 		return (0);
1363205eeb1aSlm66018 	}
13641ae08745Sheppo 
1365d10e4ef2Snarayan 	PR1("%s %lu bytes at block %lu",
1366d10e4ef2Snarayan 	    (request->operation == VD_OP_BREAD) ? "Read" : "Write",
1367d10e4ef2Snarayan 	    request->nbytes, request->addr);
13681ae08745Sheppo 
1369047ba61eSachartre 	/*
1370047ba61eSachartre 	 * We have to check the open flags because the functions processing
1371047ba61eSachartre 	 * the read/write request will not do it.
1372047ba61eSachartre 	 */
1373047ba61eSachartre 	if (request->operation == VD_OP_BWRITE && !(vd->open_flags & FWRITE)) {
1374047ba61eSachartre 		PR0("write fails because backend is opened read-only");
1375047ba61eSachartre 		request->nbytes = 0;
1376047ba61eSachartre 		request->status = EROFS;
1377047ba61eSachartre 		return (0);
1378047ba61eSachartre 	}
1379d10e4ef2Snarayan 
13804bac2208Snarayan 	mtype = (&vd->inband_task == task) ? LDC_SHADOW_MAP : LDC_DIRECT_MAP;
13814bac2208Snarayan 
13824bac2208Snarayan 	/* Map memory exported by client */
13834bac2208Snarayan 	status = ldc_mem_map(task->mhdl, request->cookie, request->ncookies,
13844bac2208Snarayan 	    mtype, (request->operation == VD_OP_BREAD) ? LDC_MEM_W : LDC_MEM_R,
1385047ba61eSachartre 	    &bufaddr, NULL);
13864bac2208Snarayan 	if (status != 0) {
13873af08d82Slm66018 		PR0("ldc_mem_map() returned err %d ", status);
1388205eeb1aSlm66018 		return (EIO);
1389d10e4ef2Snarayan 	}
1390d10e4ef2Snarayan 
1391047ba61eSachartre 	buflen = request->nbytes;
1392047ba61eSachartre 
1393047ba61eSachartre 	status = ldc_mem_acquire(task->mhdl, 0, buflen);
13944bac2208Snarayan 	if (status != 0) {
13954bac2208Snarayan 		(void) ldc_mem_unmap(task->mhdl);
13963af08d82Slm66018 		PR0("ldc_mem_acquire() returned err %d ", status);
1397205eeb1aSlm66018 		return (EIO);
13984bac2208Snarayan 	}
13994bac2208Snarayan 
1400d10e4ef2Snarayan 	/* Start the block I/O */
14013c96341aSnarayan 	if (vd->file) {
1402047ba61eSachartre 		rv = vd_file_rw(vd, slice, request->operation, bufaddr,
1403690555a1Sachartre 		    request->addr, request->nbytes);
1404690555a1Sachartre 		if (rv < 0) {
14053c96341aSnarayan 			request->nbytes = 0;
1406205eeb1aSlm66018 			request->status = EIO;
1407690555a1Sachartre 		} else {
1408690555a1Sachartre 			request->nbytes = rv;
1409205eeb1aSlm66018 			request->status = 0;
14103c96341aSnarayan 		}
14113c96341aSnarayan 	} else {
141287a7269eSachartre 		if (slice == VD_SLICE_NONE) {
141387a7269eSachartre 			/*
141487a7269eSachartre 			 * This is not a disk image so it is a real disk. We
141587a7269eSachartre 			 * assume that the underlying device driver supports
141687a7269eSachartre 			 * USCSICMD ioctls. This is the case of all SCSI devices
141787a7269eSachartre 			 * (sd, ssd...).
141887a7269eSachartre 			 *
141987a7269eSachartre 			 * In the future if we have non-SCSI disks we would need
142087a7269eSachartre 			 * to invoke the appropriate function to do I/O using an
142117cadca8Slm66018 			 * absolute disk offset (for example using DIOCTL_RWCMD
142287a7269eSachartre 			 * for IDE disks).
142387a7269eSachartre 			 */
1424047ba61eSachartre 			rv = vd_scsi_rdwr(vd, request->operation, bufaddr,
1425047ba61eSachartre 			    request->addr, request->nbytes);
142687a7269eSachartre 			if (rv != 0) {
142787a7269eSachartre 				request->nbytes = 0;
1428205eeb1aSlm66018 				request->status = EIO;
142987a7269eSachartre 			} else {
1430205eeb1aSlm66018 				request->status = 0;
143187a7269eSachartre 			}
143287a7269eSachartre 		} else {
1433047ba61eSachartre 			bioinit(buf);
1434047ba61eSachartre 			buf->b_flags	= B_BUSY;
1435047ba61eSachartre 			buf->b_bcount	= request->nbytes;
1436047ba61eSachartre 			buf->b_lblkno	= request->addr;
1437047ba61eSachartre 			buf->b_edev 	= vd->dev[slice];
1438047ba61eSachartre 			buf->b_un.b_addr = bufaddr;
1439047ba61eSachartre 			buf->b_flags 	|= (request->operation == VD_OP_BREAD)?
1440047ba61eSachartre 			    B_READ : B_WRITE;
1441047ba61eSachartre 
1442205eeb1aSlm66018 			request->status =
1443205eeb1aSlm66018 			    ldi_strategy(vd->ldi_handle[slice], buf);
1444205eeb1aSlm66018 
1445205eeb1aSlm66018 			/*
1446205eeb1aSlm66018 			 * This is to indicate to the caller that the request
1447205eeb1aSlm66018 			 * needs to be finished by vd_complete_bio() by calling
1448205eeb1aSlm66018 			 * biowait() there and waiting for that to return before
1449205eeb1aSlm66018 			 * triggering the notification of the vDisk client.
1450205eeb1aSlm66018 			 *
1451205eeb1aSlm66018 			 * This is necessary when writing to real disks as
1452205eeb1aSlm66018 			 * otherwise calls to ldi_strategy() would be serialized
1453205eeb1aSlm66018 			 * behind the calls to biowait() and performance would
1454205eeb1aSlm66018 			 * suffer.
1455205eeb1aSlm66018 			 */
1456205eeb1aSlm66018 			if (request->status == 0)
145787a7269eSachartre 				return (EINPROGRESS);
1458047ba61eSachartre 
1459047ba61eSachartre 			biofini(buf);
146087a7269eSachartre 		}
14613c96341aSnarayan 	}
14623c96341aSnarayan 
1463d10e4ef2Snarayan 	/* Clean up after error */
1464047ba61eSachartre 	rv = ldc_mem_release(task->mhdl, 0, buflen);
14654bac2208Snarayan 	if (rv) {
14663af08d82Slm66018 		PR0("ldc_mem_release() returned err %d ", rv);
1467205eeb1aSlm66018 		status = EIO;
14684bac2208Snarayan 	}
14694bac2208Snarayan 	rv = ldc_mem_unmap(task->mhdl);
14704bac2208Snarayan 	if (rv) {
1471205eeb1aSlm66018 		PR0("ldc_mem_unmap() returned err %d ", rv);
1472205eeb1aSlm66018 		status = EIO;
14734bac2208Snarayan 	}
14744bac2208Snarayan 
1475d10e4ef2Snarayan 	return (status);
1476d10e4ef2Snarayan }
1477d10e4ef2Snarayan 
1478205eeb1aSlm66018 /*
1479205eeb1aSlm66018  * This function should only be called from vd_notify to ensure that requests
1480205eeb1aSlm66018  * are responded to in the order that they are received.
1481205eeb1aSlm66018  */
1482d10e4ef2Snarayan static int
1483d10e4ef2Snarayan send_msg(ldc_handle_t ldc_handle, void *msg, size_t msglen)
1484d10e4ef2Snarayan {
14853af08d82Slm66018 	int	status;
1486d10e4ef2Snarayan 	size_t	nbytes;
1487d10e4ef2Snarayan 
14883af08d82Slm66018 	do {
1489d10e4ef2Snarayan 		nbytes = msglen;
1490d10e4ef2Snarayan 		status = ldc_write(ldc_handle, msg, &nbytes);
14913af08d82Slm66018 		if (status != EWOULDBLOCK)
14923af08d82Slm66018 			break;
14933af08d82Slm66018 		drv_usecwait(vds_ldc_delay);
14943af08d82Slm66018 	} while (status == EWOULDBLOCK);
1495d10e4ef2Snarayan 
1496d10e4ef2Snarayan 	if (status != 0) {
14973af08d82Slm66018 		if (status != ECONNRESET)
14983af08d82Slm66018 			PR0("ldc_write() returned errno %d", status);
1499d10e4ef2Snarayan 		return (status);
1500d10e4ef2Snarayan 	} else if (nbytes != msglen) {
15013af08d82Slm66018 		PR0("ldc_write() performed only partial write");
1502d10e4ef2Snarayan 		return (EIO);
1503d10e4ef2Snarayan 	}
1504d10e4ef2Snarayan 
1505d10e4ef2Snarayan 	PR1("SENT %lu bytes", msglen);
1506d10e4ef2Snarayan 	return (0);
1507d10e4ef2Snarayan }
1508d10e4ef2Snarayan 
1509d10e4ef2Snarayan static void
1510d10e4ef2Snarayan vd_need_reset(vd_t *vd, boolean_t reset_ldc)
1511d10e4ef2Snarayan {
1512d10e4ef2Snarayan 	mutex_enter(&vd->lock);
1513d10e4ef2Snarayan 	vd->reset_state	= B_TRUE;
1514d10e4ef2Snarayan 	vd->reset_ldc	= reset_ldc;
1515d10e4ef2Snarayan 	mutex_exit(&vd->lock);
1516d10e4ef2Snarayan }
1517d10e4ef2Snarayan 
1518d10e4ef2Snarayan /*
1519d10e4ef2Snarayan  * Reset the state of the connection with a client, if needed; reset the LDC
1520d10e4ef2Snarayan  * transport as well, if needed.  This function should only be called from the
15213af08d82Slm66018  * "vd_recv_msg", as it waits for tasks - otherwise a deadlock can occur.
1522d10e4ef2Snarayan  */
1523d10e4ef2Snarayan static void
1524d10e4ef2Snarayan vd_reset_if_needed(vd_t *vd)
1525d10e4ef2Snarayan {
1526d10e4ef2Snarayan 	int	status = 0;
1527d10e4ef2Snarayan 
1528d10e4ef2Snarayan 	mutex_enter(&vd->lock);
1529d10e4ef2Snarayan 	if (!vd->reset_state) {
1530d10e4ef2Snarayan 		ASSERT(!vd->reset_ldc);
1531d10e4ef2Snarayan 		mutex_exit(&vd->lock);
1532d10e4ef2Snarayan 		return;
1533d10e4ef2Snarayan 	}
1534d10e4ef2Snarayan 	mutex_exit(&vd->lock);
1535d10e4ef2Snarayan 
1536d10e4ef2Snarayan 	PR0("Resetting connection state with %s", VD_CLIENT(vd));
1537d10e4ef2Snarayan 
1538d10e4ef2Snarayan 	/*
1539d10e4ef2Snarayan 	 * Let any asynchronous I/O complete before possibly pulling the rug
1540d10e4ef2Snarayan 	 * out from under it; defer checking vd->reset_ldc, as one of the
1541d10e4ef2Snarayan 	 * asynchronous tasks might set it
1542d10e4ef2Snarayan 	 */
1543d10e4ef2Snarayan 	ddi_taskq_wait(vd->completionq);
1544d10e4ef2Snarayan 
15453c96341aSnarayan 	if (vd->file) {
1546da6c28aaSamw 		status = VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL);
15473c96341aSnarayan 		if (status) {
15483c96341aSnarayan 			PR0("VOP_FSYNC returned errno %d", status);
15493c96341aSnarayan 		}
15503c96341aSnarayan 	}
15513c96341aSnarayan 
1552d10e4ef2Snarayan 	if ((vd->initialized & VD_DRING) &&
1553d10e4ef2Snarayan 	    ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0))
15543af08d82Slm66018 		PR0("ldc_mem_dring_unmap() returned errno %d", status);
1555d10e4ef2Snarayan 
15563af08d82Slm66018 	vd_free_dring_task(vd);
15573af08d82Slm66018 
15583af08d82Slm66018 	/* Free the staging buffer for msgs */
15593af08d82Slm66018 	if (vd->vio_msgp != NULL) {
15603af08d82Slm66018 		kmem_free(vd->vio_msgp, vd->max_msglen);
15613af08d82Slm66018 		vd->vio_msgp = NULL;
1562d10e4ef2Snarayan 	}
1563d10e4ef2Snarayan 
15643af08d82Slm66018 	/* Free the inband message buffer */
15653af08d82Slm66018 	if (vd->inband_task.msg != NULL) {
15663af08d82Slm66018 		kmem_free(vd->inband_task.msg, vd->max_msglen);
15673af08d82Slm66018 		vd->inband_task.msg = NULL;
15683af08d82Slm66018 	}
1569d10e4ef2Snarayan 
1570d10e4ef2Snarayan 	mutex_enter(&vd->lock);
15713af08d82Slm66018 
15723af08d82Slm66018 	if (vd->reset_ldc)
15733af08d82Slm66018 		PR0("taking down LDC channel");
1574e1ebb9ecSlm66018 	if (vd->reset_ldc && ((status = ldc_down(vd->ldc_handle)) != 0))
15753af08d82Slm66018 		PR0("ldc_down() returned errno %d", status);
1576d10e4ef2Snarayan 
15772f5224aeSachartre 	/* Reset exclusive access rights */
15782f5224aeSachartre 	vd_reset_access(vd);
15792f5224aeSachartre 
1580d10e4ef2Snarayan 	vd->initialized	&= ~(VD_SID | VD_SEQ_NUM | VD_DRING);
1581d10e4ef2Snarayan 	vd->state	= VD_STATE_INIT;
1582d10e4ef2Snarayan 	vd->max_msglen	= sizeof (vio_msg_t);	/* baseline vio message size */
1583d10e4ef2Snarayan 
15843af08d82Slm66018 	/* Allocate the staging buffer */
15853af08d82Slm66018 	vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
15863af08d82Slm66018 
15873af08d82Slm66018 	PR0("calling ldc_up\n");
15883af08d82Slm66018 	(void) ldc_up(vd->ldc_handle);
15893af08d82Slm66018 
1590d10e4ef2Snarayan 	vd->reset_state	= B_FALSE;
1591d10e4ef2Snarayan 	vd->reset_ldc	= B_FALSE;
15923af08d82Slm66018 
1593d10e4ef2Snarayan 	mutex_exit(&vd->lock);
1594d10e4ef2Snarayan }
1595d10e4ef2Snarayan 
15963af08d82Slm66018 static void vd_recv_msg(void *arg);
15973af08d82Slm66018 
15983af08d82Slm66018 static void
15993af08d82Slm66018 vd_mark_in_reset(vd_t *vd)
16003af08d82Slm66018 {
16013af08d82Slm66018 	int status;
16023af08d82Slm66018 
16033af08d82Slm66018 	PR0("vd_mark_in_reset: marking vd in reset\n");
16043af08d82Slm66018 
16053af08d82Slm66018 	vd_need_reset(vd, B_FALSE);
16063af08d82Slm66018 	status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, DDI_SLEEP);
16073af08d82Slm66018 	if (status == DDI_FAILURE) {
16083af08d82Slm66018 		PR0("cannot schedule task to recv msg\n");
16093af08d82Slm66018 		vd_need_reset(vd, B_TRUE);
16103af08d82Slm66018 		return;
16113af08d82Slm66018 	}
16123af08d82Slm66018 }
16133af08d82Slm66018 
1614d10e4ef2Snarayan static int
16153c96341aSnarayan vd_mark_elem_done(vd_t *vd, int idx, int elem_status, int elem_nbytes)
1616d10e4ef2Snarayan {
1617d10e4ef2Snarayan 	boolean_t		accepted;
1618d10e4ef2Snarayan 	int			status;
1619d10e4ef2Snarayan 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
1620d10e4ef2Snarayan 
16213af08d82Slm66018 	if (vd->reset_state)
16223af08d82Slm66018 		return (0);
1623d10e4ef2Snarayan 
1624d10e4ef2Snarayan 	/* Acquire the element */
16253af08d82Slm66018 	if (!vd->reset_state &&
16263af08d82Slm66018 	    (status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) {
16273af08d82Slm66018 		if (status == ECONNRESET) {
16283af08d82Slm66018 			vd_mark_in_reset(vd);
16293af08d82Slm66018 			return (0);
16303af08d82Slm66018 		} else {
16313af08d82Slm66018 			PR0("ldc_mem_dring_acquire() returned errno %d",
16323af08d82Slm66018 			    status);
1633d10e4ef2Snarayan 			return (status);
1634d10e4ef2Snarayan 		}
16353af08d82Slm66018 	}
1636d10e4ef2Snarayan 
1637d10e4ef2Snarayan 	/* Set the element's status and mark it done */
1638d10e4ef2Snarayan 	accepted = (elem->hdr.dstate == VIO_DESC_ACCEPTED);
1639d10e4ef2Snarayan 	if (accepted) {
16403c96341aSnarayan 		elem->payload.nbytes	= elem_nbytes;
1641d10e4ef2Snarayan 		elem->payload.status	= elem_status;
1642d10e4ef2Snarayan 		elem->hdr.dstate	= VIO_DESC_DONE;
1643d10e4ef2Snarayan 	} else {
1644d10e4ef2Snarayan 		/* Perhaps client timed out waiting for I/O... */
16453af08d82Slm66018 		PR0("element %u no longer \"accepted\"", idx);
1646d10e4ef2Snarayan 		VD_DUMP_DRING_ELEM(elem);
1647d10e4ef2Snarayan 	}
1648d10e4ef2Snarayan 	/* Release the element */
16493af08d82Slm66018 	if (!vd->reset_state &&
16503af08d82Slm66018 	    (status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) {
16513af08d82Slm66018 		if (status == ECONNRESET) {
16523af08d82Slm66018 			vd_mark_in_reset(vd);
16533af08d82Slm66018 			return (0);
16543af08d82Slm66018 		} else {
16553af08d82Slm66018 			PR0("ldc_mem_dring_release() returned errno %d",
16563af08d82Slm66018 			    status);
1657d10e4ef2Snarayan 			return (status);
1658d10e4ef2Snarayan 		}
16593af08d82Slm66018 	}
1660d10e4ef2Snarayan 
1661d10e4ef2Snarayan 	return (accepted ? 0 : EINVAL);
1662d10e4ef2Snarayan }
1663d10e4ef2Snarayan 
1664205eeb1aSlm66018 /*
1665205eeb1aSlm66018  * Return Values
1666205eeb1aSlm66018  *	0	- operation completed successfully
1667205eeb1aSlm66018  *	EIO	- encountered LDC / task error
1668205eeb1aSlm66018  *
1669205eeb1aSlm66018  * Side Effect
1670205eeb1aSlm66018  *	sets request->status = <disk operation status>
1671205eeb1aSlm66018  */
1672205eeb1aSlm66018 static int
1673205eeb1aSlm66018 vd_complete_bio(vd_task_t *task)
1674d10e4ef2Snarayan {
1675d10e4ef2Snarayan 	int			status		= 0;
1676205eeb1aSlm66018 	int			rv		= 0;
1677d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
1678d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
1679d10e4ef2Snarayan 	struct buf		*buf		= &task->buf;
1680d10e4ef2Snarayan 
1681d10e4ef2Snarayan 
1682d10e4ef2Snarayan 	ASSERT(vd != NULL);
1683d10e4ef2Snarayan 	ASSERT(request != NULL);
1684d10e4ef2Snarayan 	ASSERT(task->msg != NULL);
1685d10e4ef2Snarayan 	ASSERT(task->msglen >= sizeof (*task->msg));
16863c96341aSnarayan 	ASSERT(!vd->file);
1687205eeb1aSlm66018 	ASSERT(request->slice != VD_SLICE_NONE);
1688d10e4ef2Snarayan 
1689205eeb1aSlm66018 	/* Wait for the I/O to complete [ call to ldi_strategy(9f) ] */
1690d10e4ef2Snarayan 	request->status = biowait(buf);
1691d10e4ef2Snarayan 
16923c96341aSnarayan 	/* return back the number of bytes read/written */
16933c96341aSnarayan 	request->nbytes = buf->b_bcount - buf->b_resid;
16943c96341aSnarayan 
16954bac2208Snarayan 	/* Release the buffer */
16963af08d82Slm66018 	if (!vd->reset_state)
16974bac2208Snarayan 		status = ldc_mem_release(task->mhdl, 0, buf->b_bcount);
16984bac2208Snarayan 	if (status) {
16993af08d82Slm66018 		PR0("ldc_mem_release() returned errno %d copying to "
17003af08d82Slm66018 		    "client", status);
17013af08d82Slm66018 		if (status == ECONNRESET) {
17023af08d82Slm66018 			vd_mark_in_reset(vd);
17033af08d82Slm66018 		}
1704205eeb1aSlm66018 		rv = EIO;
17051ae08745Sheppo 	}
17061ae08745Sheppo 
17073af08d82Slm66018 	/* Unmap the memory, even if in reset */
17084bac2208Snarayan 	status = ldc_mem_unmap(task->mhdl);
17094bac2208Snarayan 	if (status) {
17103af08d82Slm66018 		PR0("ldc_mem_unmap() returned errno %d copying to client",
17114bac2208Snarayan 		    status);
17123af08d82Slm66018 		if (status == ECONNRESET) {
17133af08d82Slm66018 			vd_mark_in_reset(vd);
17143af08d82Slm66018 		}
1715205eeb1aSlm66018 		rv = EIO;
17164bac2208Snarayan 	}
17174bac2208Snarayan 
1718d10e4ef2Snarayan 	biofini(buf);
17191ae08745Sheppo 
1720205eeb1aSlm66018 	return (rv);
1721205eeb1aSlm66018 }
1722205eeb1aSlm66018 
1723205eeb1aSlm66018 /*
1724205eeb1aSlm66018  * Description:
1725205eeb1aSlm66018  *	This function is called by the two functions called by a taskq
1726205eeb1aSlm66018  *	[ vd_complete_notify() and vd_serial_notify()) ] to send the
1727205eeb1aSlm66018  *	message to the client.
1728205eeb1aSlm66018  *
1729205eeb1aSlm66018  * Parameters:
1730205eeb1aSlm66018  *	arg 	- opaque pointer to structure containing task to be completed
1731205eeb1aSlm66018  *
1732205eeb1aSlm66018  * Return Values
1733205eeb1aSlm66018  *	None
1734205eeb1aSlm66018  */
1735205eeb1aSlm66018 static void
1736205eeb1aSlm66018 vd_notify(vd_task_t *task)
1737205eeb1aSlm66018 {
1738205eeb1aSlm66018 	int	status;
1739205eeb1aSlm66018 
1740205eeb1aSlm66018 	ASSERT(task != NULL);
1741205eeb1aSlm66018 	ASSERT(task->vd != NULL);
1742205eeb1aSlm66018 
1743205eeb1aSlm66018 	if (task->vd->reset_state)
1744205eeb1aSlm66018 		return;
1745205eeb1aSlm66018 
1746205eeb1aSlm66018 	/*
1747205eeb1aSlm66018 	 * Send the "ack" or "nack" back to the client; if sending the message
1748205eeb1aSlm66018 	 * via LDC fails, arrange to reset both the connection state and LDC
1749205eeb1aSlm66018 	 * itself
1750205eeb1aSlm66018 	 */
1751205eeb1aSlm66018 	PR2("Sending %s",
1752205eeb1aSlm66018 	    (task->msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK");
1753205eeb1aSlm66018 
1754205eeb1aSlm66018 	status = send_msg(task->vd->ldc_handle, task->msg, task->msglen);
1755205eeb1aSlm66018 	switch (status) {
1756205eeb1aSlm66018 	case 0:
1757205eeb1aSlm66018 		break;
1758205eeb1aSlm66018 	case ECONNRESET:
1759205eeb1aSlm66018 		vd_mark_in_reset(task->vd);
1760205eeb1aSlm66018 		break;
1761205eeb1aSlm66018 	default:
1762205eeb1aSlm66018 		PR0("initiating full reset");
1763205eeb1aSlm66018 		vd_need_reset(task->vd, B_TRUE);
1764205eeb1aSlm66018 		break;
1765205eeb1aSlm66018 	}
1766205eeb1aSlm66018 
1767205eeb1aSlm66018 	DTRACE_PROBE1(task__end, vd_task_t *, task);
1768205eeb1aSlm66018 }
1769205eeb1aSlm66018 
1770205eeb1aSlm66018 /*
1771205eeb1aSlm66018  * Description:
1772205eeb1aSlm66018  *	Mark the Dring entry as Done and (if necessary) send an ACK/NACK to
1773205eeb1aSlm66018  *	the vDisk client
1774205eeb1aSlm66018  *
1775205eeb1aSlm66018  * Parameters:
1776205eeb1aSlm66018  *	task 		- structure containing the request sent from client
1777205eeb1aSlm66018  *
1778205eeb1aSlm66018  * Return Values
1779205eeb1aSlm66018  *	None
1780205eeb1aSlm66018  */
1781205eeb1aSlm66018 static void
1782205eeb1aSlm66018 vd_complete_notify(vd_task_t *task)
1783205eeb1aSlm66018 {
1784205eeb1aSlm66018 	int			status		= 0;
1785205eeb1aSlm66018 	vd_t			*vd		= task->vd;
1786205eeb1aSlm66018 	vd_dring_payload_t	*request	= task->request;
1787205eeb1aSlm66018 
1788d10e4ef2Snarayan 	/* Update the dring element for a dring client */
1789f0ca1d9aSsb155480 	if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE_V1_0)) {
17903c96341aSnarayan 		status = vd_mark_elem_done(vd, task->index,
17913c96341aSnarayan 		    request->status, request->nbytes);
17923af08d82Slm66018 		if (status == ECONNRESET)
17933af08d82Slm66018 			vd_mark_in_reset(vd);
17943af08d82Slm66018 	}
17951ae08745Sheppo 
1796d10e4ef2Snarayan 	/*
1797205eeb1aSlm66018 	 * If a transport error occurred while marking the element done or
1798205eeb1aSlm66018 	 * previously while executing the task, arrange to "nack" the message
1799205eeb1aSlm66018 	 * when the final task in the descriptor element range completes
1800d10e4ef2Snarayan 	 */
1801205eeb1aSlm66018 	if ((status != 0) || (task->status != 0))
1802d10e4ef2Snarayan 		task->msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
18031ae08745Sheppo 
1804d10e4ef2Snarayan 	/*
1805d10e4ef2Snarayan 	 * Only the final task for a range of elements will respond to and
1806d10e4ef2Snarayan 	 * free the message
1807d10e4ef2Snarayan 	 */
18083af08d82Slm66018 	if (task->type == VD_NONFINAL_RANGE_TASK) {
1809d10e4ef2Snarayan 		return;
18103af08d82Slm66018 	}
18111ae08745Sheppo 
1812205eeb1aSlm66018 	vd_notify(task);
1813205eeb1aSlm66018 }
1814205eeb1aSlm66018 
1815d10e4ef2Snarayan /*
1816205eeb1aSlm66018  * Description:
1817205eeb1aSlm66018  *	This is the basic completion function called to handle inband data
1818205eeb1aSlm66018  *	requests and handshake messages. All it needs to do is trigger a
1819205eeb1aSlm66018  *	message to the client that the request is completed.
1820205eeb1aSlm66018  *
1821205eeb1aSlm66018  * Parameters:
1822205eeb1aSlm66018  *	arg 	- opaque pointer to structure containing task to be completed
1823205eeb1aSlm66018  *
1824205eeb1aSlm66018  * Return Values
1825205eeb1aSlm66018  *	None
1826d10e4ef2Snarayan  */
1827205eeb1aSlm66018 static void
1828205eeb1aSlm66018 vd_serial_notify(void *arg)
1829205eeb1aSlm66018 {
1830205eeb1aSlm66018 	vd_task_t		*task = (vd_task_t *)arg;
1831205eeb1aSlm66018 
1832205eeb1aSlm66018 	ASSERT(task != NULL);
1833205eeb1aSlm66018 	vd_notify(task);
18341ae08745Sheppo }
18351ae08745Sheppo 
18362f5224aeSachartre /* ARGSUSED */
18372f5224aeSachartre static int
18382f5224aeSachartre vd_geom2dk_geom(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
18390a55fbb7Slm66018 {
18400a55fbb7Slm66018 	VD_GEOM2DK_GEOM((vd_geom_t *)vd_buf, (struct dk_geom *)ioctl_arg);
18412f5224aeSachartre 	return (0);
18420a55fbb7Slm66018 }
18430a55fbb7Slm66018 
18442f5224aeSachartre /* ARGSUSED */
18452f5224aeSachartre static int
18462f5224aeSachartre vd_vtoc2vtoc(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
18470a55fbb7Slm66018 {
18480a55fbb7Slm66018 	VD_VTOC2VTOC((vd_vtoc_t *)vd_buf, (struct vtoc *)ioctl_arg);
18492f5224aeSachartre 	return (0);
18500a55fbb7Slm66018 }
18510a55fbb7Slm66018 
18520a55fbb7Slm66018 static void
18530a55fbb7Slm66018 dk_geom2vd_geom(void *ioctl_arg, void *vd_buf)
18540a55fbb7Slm66018 {
18550a55fbb7Slm66018 	DK_GEOM2VD_GEOM((struct dk_geom *)ioctl_arg, (vd_geom_t *)vd_buf);
18560a55fbb7Slm66018 }
18570a55fbb7Slm66018 
18580a55fbb7Slm66018 static void
18590a55fbb7Slm66018 vtoc2vd_vtoc(void *ioctl_arg, void *vd_buf)
18600a55fbb7Slm66018 {
18610a55fbb7Slm66018 	VTOC2VD_VTOC((struct vtoc *)ioctl_arg, (vd_vtoc_t *)vd_buf);
18620a55fbb7Slm66018 }
18630a55fbb7Slm66018 
18642f5224aeSachartre static int
18652f5224aeSachartre vd_get_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
18664bac2208Snarayan {
18674bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
18684bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
18692f5224aeSachartre 	size_t data_len;
18702f5224aeSachartre 
18712f5224aeSachartre 	data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t));
18722f5224aeSachartre 	if (vd_efi->length > data_len)
18732f5224aeSachartre 		return (EINVAL);
18744bac2208Snarayan 
18754bac2208Snarayan 	dk_efi->dki_lba = vd_efi->lba;
18764bac2208Snarayan 	dk_efi->dki_length = vd_efi->length;
18774bac2208Snarayan 	dk_efi->dki_data = kmem_zalloc(vd_efi->length, KM_SLEEP);
18782f5224aeSachartre 	return (0);
18794bac2208Snarayan }
18804bac2208Snarayan 
18814bac2208Snarayan static void
18824bac2208Snarayan vd_get_efi_out(void *ioctl_arg, void *vd_buf)
18834bac2208Snarayan {
18844bac2208Snarayan 	int len;
18854bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
18864bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
18874bac2208Snarayan 
18884bac2208Snarayan 	len = vd_efi->length;
18894bac2208Snarayan 	DK_EFI2VD_EFI(dk_efi, vd_efi);
18904bac2208Snarayan 	kmem_free(dk_efi->dki_data, len);
18914bac2208Snarayan }
18924bac2208Snarayan 
18932f5224aeSachartre static int
18942f5224aeSachartre vd_set_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
18954bac2208Snarayan {
18964bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
18974bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
18982f5224aeSachartre 	size_t data_len;
18992f5224aeSachartre 
19002f5224aeSachartre 	data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t));
19012f5224aeSachartre 	if (vd_efi->length > data_len)
19022f5224aeSachartre 		return (EINVAL);
19034bac2208Snarayan 
19044bac2208Snarayan 	dk_efi->dki_data = kmem_alloc(vd_efi->length, KM_SLEEP);
19054bac2208Snarayan 	VD_EFI2DK_EFI(vd_efi, dk_efi);
19062f5224aeSachartre 	return (0);
19074bac2208Snarayan }
19084bac2208Snarayan 
19094bac2208Snarayan static void
19104bac2208Snarayan vd_set_efi_out(void *ioctl_arg, void *vd_buf)
19114bac2208Snarayan {
19124bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
19134bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
19144bac2208Snarayan 
19154bac2208Snarayan 	kmem_free(dk_efi->dki_data, vd_efi->length);
19164bac2208Snarayan }
19174bac2208Snarayan 
19182f5224aeSachartre static int
19192f5224aeSachartre vd_scsicmd_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
19202f5224aeSachartre {
19212f5224aeSachartre 	size_t vd_scsi_len;
19222f5224aeSachartre 	vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf;
19232f5224aeSachartre 	struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg;
19242f5224aeSachartre 
19252f5224aeSachartre 	/* check buffer size */
19262f5224aeSachartre 	vd_scsi_len = VD_SCSI_SIZE;
19272f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(vd_scsi->cdb_len, sizeof (uint64_t));
19282f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(vd_scsi->sense_len, sizeof (uint64_t));
19292f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(vd_scsi->datain_len, sizeof (uint64_t));
19302f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(vd_scsi->dataout_len, sizeof (uint64_t));
19312f5224aeSachartre 
19322f5224aeSachartre 	ASSERT(vd_scsi_len % sizeof (uint64_t) == 0);
19332f5224aeSachartre 
19342f5224aeSachartre 	if (vd_buf_len < vd_scsi_len)
19352f5224aeSachartre 		return (EINVAL);
19362f5224aeSachartre 
19372f5224aeSachartre 	/* set flags */
19382f5224aeSachartre 	uscsi->uscsi_flags = vd_scsi_debug;
19392f5224aeSachartre 
19402f5224aeSachartre 	if (vd_scsi->options & VD_SCSI_OPT_NORETRY) {
19412f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_ISOLATE;
19422f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_DIAGNOSE;
19432f5224aeSachartre 	}
19442f5224aeSachartre 
19452f5224aeSachartre 	/* task attribute */
19462f5224aeSachartre 	switch (vd_scsi->task_attribute) {
19472f5224aeSachartre 	case VD_SCSI_TASK_ACA:
19482f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_HEAD;
19492f5224aeSachartre 		break;
19502f5224aeSachartre 	case VD_SCSI_TASK_HQUEUE:
19512f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_HTAG;
19522f5224aeSachartre 		break;
19532f5224aeSachartre 	case VD_SCSI_TASK_ORDERED:
19542f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_OTAG;
19552f5224aeSachartre 		break;
19562f5224aeSachartre 	default:
19572f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_NOTAG;
19582f5224aeSachartre 		break;
19592f5224aeSachartre 	}
19602f5224aeSachartre 
19612f5224aeSachartre 	/* timeout */
19622f5224aeSachartre 	uscsi->uscsi_timeout = vd_scsi->timeout;
19632f5224aeSachartre 
19642f5224aeSachartre 	/* cdb data */
19652f5224aeSachartre 	uscsi->uscsi_cdb = (caddr_t)VD_SCSI_DATA_CDB(vd_scsi);
19662f5224aeSachartre 	uscsi->uscsi_cdblen = vd_scsi->cdb_len;
19672f5224aeSachartre 
19682f5224aeSachartre 	/* sense buffer */
19692f5224aeSachartre 	if (vd_scsi->sense_len != 0) {
19702f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_RQENABLE;
19712f5224aeSachartre 		uscsi->uscsi_rqbuf = (caddr_t)VD_SCSI_DATA_SENSE(vd_scsi);
19722f5224aeSachartre 		uscsi->uscsi_rqlen = vd_scsi->sense_len;
19732f5224aeSachartre 	}
19742f5224aeSachartre 
19752f5224aeSachartre 	if (vd_scsi->datain_len != 0 && vd_scsi->dataout_len != 0) {
19762f5224aeSachartre 		/* uscsi does not support read/write request */
19772f5224aeSachartre 		return (EINVAL);
19782f5224aeSachartre 	}
19792f5224aeSachartre 
19802f5224aeSachartre 	/* request data-in */
19812f5224aeSachartre 	if (vd_scsi->datain_len != 0) {
19822f5224aeSachartre 		uscsi->uscsi_flags |= USCSI_READ;
19832f5224aeSachartre 		uscsi->uscsi_buflen = vd_scsi->datain_len;
19842f5224aeSachartre 		uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_IN(vd_scsi);
19852f5224aeSachartre 	}
19862f5224aeSachartre 
19872f5224aeSachartre 	/* request data-out */
19882f5224aeSachartre 	if (vd_scsi->dataout_len != 0) {
19892f5224aeSachartre 		uscsi->uscsi_buflen = vd_scsi->dataout_len;
19902f5224aeSachartre 		uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_OUT(vd_scsi);
19912f5224aeSachartre 	}
19922f5224aeSachartre 
19932f5224aeSachartre 	return (0);
19942f5224aeSachartre }
19952f5224aeSachartre 
19962f5224aeSachartre static void
19972f5224aeSachartre vd_scsicmd_out(void *ioctl_arg, void *vd_buf)
19982f5224aeSachartre {
19992f5224aeSachartre 	vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf;
20002f5224aeSachartre 	struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg;
20012f5224aeSachartre 
20022f5224aeSachartre 	/* output fields */
20032f5224aeSachartre 	vd_scsi->cmd_status = uscsi->uscsi_status;
20042f5224aeSachartre 
20052f5224aeSachartre 	/* sense data */
20062f5224aeSachartre 	if ((uscsi->uscsi_flags & USCSI_RQENABLE) &&
20072f5224aeSachartre 	    (uscsi->uscsi_status == STATUS_CHECK ||
20082f5224aeSachartre 	    uscsi->uscsi_status == STATUS_TERMINATED)) {
20092f5224aeSachartre 		vd_scsi->sense_status = uscsi->uscsi_rqstatus;
20102f5224aeSachartre 		if (uscsi->uscsi_rqstatus == STATUS_GOOD)
20112f5224aeSachartre 			vd_scsi->sense_len -= uscsi->uscsi_resid;
20122f5224aeSachartre 		else
20132f5224aeSachartre 			vd_scsi->sense_len = 0;
20142f5224aeSachartre 	} else {
20152f5224aeSachartre 		vd_scsi->sense_len = 0;
20162f5224aeSachartre 	}
20172f5224aeSachartre 
20182f5224aeSachartre 	if (uscsi->uscsi_status != STATUS_GOOD) {
20192f5224aeSachartre 		vd_scsi->dataout_len = 0;
20202f5224aeSachartre 		vd_scsi->datain_len = 0;
20212f5224aeSachartre 		return;
20222f5224aeSachartre 	}
20232f5224aeSachartre 
20242f5224aeSachartre 	if (uscsi->uscsi_flags & USCSI_READ) {
20252f5224aeSachartre 		/* request data (read) */
20262f5224aeSachartre 		vd_scsi->datain_len -= uscsi->uscsi_resid;
20272f5224aeSachartre 		vd_scsi->dataout_len = 0;
20282f5224aeSachartre 	} else {
20292f5224aeSachartre 		/* request data (write) */
20302f5224aeSachartre 		vd_scsi->datain_len = 0;
20312f5224aeSachartre 		vd_scsi->dataout_len -= uscsi->uscsi_resid;
20322f5224aeSachartre 	}
20332f5224aeSachartre }
20342f5224aeSachartre 
2035690555a1Sachartre static ushort_t
20363c96341aSnarayan vd_lbl2cksum(struct dk_label *label)
20373c96341aSnarayan {
20383c96341aSnarayan 	int	count;
2039690555a1Sachartre 	ushort_t sum, *sp;
20403c96341aSnarayan 
20413c96341aSnarayan 	count =	(sizeof (struct dk_label)) / (sizeof (short)) - 1;
2042690555a1Sachartre 	sp = (ushort_t *)label;
20433c96341aSnarayan 	sum = 0;
20443c96341aSnarayan 	while (count--) {
20453c96341aSnarayan 		sum ^= *sp++;
20463c96341aSnarayan 	}
20473c96341aSnarayan 
20483c96341aSnarayan 	return (sum);
20493c96341aSnarayan }
20503c96341aSnarayan 
205187a7269eSachartre /*
205287a7269eSachartre  * Handle ioctls to a disk slice.
2053205eeb1aSlm66018  *
2054205eeb1aSlm66018  * Return Values
2055205eeb1aSlm66018  *	0	- Indicates that there are no errors in disk operations
2056205eeb1aSlm66018  *	ENOTSUP	- Unknown disk label type or unsupported DKIO ioctl
2057205eeb1aSlm66018  *	EINVAL	- Not enough room to copy the EFI label
2058205eeb1aSlm66018  *
205987a7269eSachartre  */
20601ae08745Sheppo static int
20610a55fbb7Slm66018 vd_do_slice_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
20621ae08745Sheppo {
20634bac2208Snarayan 	dk_efi_t *dk_ioc;
2064edcc0754Sachartre 	int rval;
2065edcc0754Sachartre 
2066edcc0754Sachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
2067edcc0754Sachartre 
2068edcc0754Sachartre 	if (cmd == DKIOCFLUSHWRITECACHE) {
2069edcc0754Sachartre 		if (vd->file) {
2070edcc0754Sachartre 			return (VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL));
2071edcc0754Sachartre 		} else {
2072edcc0754Sachartre 			return (ldi_ioctl(vd->ldi_handle[0], cmd,
2073edcc0754Sachartre 			    (intptr_t)ioctl_arg, vd->open_flags | FKIOCTL,
2074edcc0754Sachartre 			    kcred, &rval));
2075edcc0754Sachartre 		}
2076edcc0754Sachartre 	}
20774bac2208Snarayan 
20784bac2208Snarayan 	switch (vd->vdisk_label) {
20794bac2208Snarayan 
2080edcc0754Sachartre 	/* ioctls for a single slice disk with a VTOC label */
20814bac2208Snarayan 	case VD_DISK_LABEL_VTOC:
20824bac2208Snarayan 
20831ae08745Sheppo 		switch (cmd) {
20841ae08745Sheppo 		case DKIOCGGEOM:
20850a55fbb7Slm66018 			ASSERT(ioctl_arg != NULL);
20860a55fbb7Slm66018 			bcopy(&vd->dk_geom, ioctl_arg, sizeof (vd->dk_geom));
20871ae08745Sheppo 			return (0);
20881ae08745Sheppo 		case DKIOCGVTOC:
20890a55fbb7Slm66018 			ASSERT(ioctl_arg != NULL);
20900a55fbb7Slm66018 			bcopy(&vd->vtoc, ioctl_arg, sizeof (vd->vtoc));
20911ae08745Sheppo 			return (0);
209287a7269eSachartre 		default:
20933c96341aSnarayan 			return (ENOTSUP);
209487a7269eSachartre 		}
209587a7269eSachartre 
2096edcc0754Sachartre 	/* ioctls for a single slice disk with an EFI label */
209787a7269eSachartre 	case VD_DISK_LABEL_EFI:
209887a7269eSachartre 
209987a7269eSachartre 		switch (cmd) {
210087a7269eSachartre 		case DKIOCGETEFI:
21013c96341aSnarayan 			ASSERT(ioctl_arg != NULL);
210287a7269eSachartre 			dk_ioc = (dk_efi_t *)ioctl_arg;
2103edcc0754Sachartre 
2104edcc0754Sachartre 			/*
2105edcc0754Sachartre 			 * For a single slice disk with an EFI label, we define
2106edcc0754Sachartre 			 * a fake EFI label with the GPT at LBA 1 and one GPE
2107edcc0754Sachartre 			 * at LBA 2. So we return the GPT or the GPE depending
2108edcc0754Sachartre 			 * on which LBA is requested.
2109edcc0754Sachartre 			 */
2110edcc0754Sachartre 			if (dk_ioc->dki_lba == 1) {
2111edcc0754Sachartre 
2112edcc0754Sachartre 				/* return the EFI GPT */
2113edcc0754Sachartre 				if (dk_ioc->dki_length < sizeof (efi_gpt_t))
211487a7269eSachartre 					return (EINVAL);
2115edcc0754Sachartre 
2116edcc0754Sachartre 				bcopy(&vd->efi_gpt, dk_ioc->dki_data,
2117edcc0754Sachartre 				    sizeof (efi_gpt_t));
2118edcc0754Sachartre 
2119edcc0754Sachartre 				/* also return the GPE if possible */
2120edcc0754Sachartre 				if (dk_ioc->dki_length >= sizeof (efi_gpt_t) +
2121edcc0754Sachartre 				    sizeof (efi_gpe_t)) {
2122edcc0754Sachartre 					bcopy(&vd->efi_gpe, dk_ioc->dki_data +
2123edcc0754Sachartre 					    1, sizeof (efi_gpe_t));
2124edcc0754Sachartre 				}
2125edcc0754Sachartre 
2126edcc0754Sachartre 			} else if (dk_ioc->dki_lba == 2) {
2127edcc0754Sachartre 
2128edcc0754Sachartre 				/* return the EFI GPE */
2129edcc0754Sachartre 				if (dk_ioc->dki_length < sizeof (efi_gpe_t))
2130edcc0754Sachartre 					return (EINVAL);
2131edcc0754Sachartre 
2132edcc0754Sachartre 				bcopy(&vd->efi_gpe, dk_ioc->dki_data,
2133edcc0754Sachartre 				    sizeof (efi_gpe_t));
2134edcc0754Sachartre 
2135edcc0754Sachartre 			} else {
2136edcc0754Sachartre 				return (EINVAL);
2137edcc0754Sachartre 			}
2138edcc0754Sachartre 
213987a7269eSachartre 			return (0);
214087a7269eSachartre 		default:
214187a7269eSachartre 			return (ENOTSUP);
214287a7269eSachartre 		}
214387a7269eSachartre 
214487a7269eSachartre 	default:
2145205eeb1aSlm66018 		/* Unknown disk label type */
214687a7269eSachartre 		return (ENOTSUP);
214787a7269eSachartre 	}
214887a7269eSachartre }
214987a7269eSachartre 
2150edcc0754Sachartre static int
2151edcc0754Sachartre vds_efi_alloc_and_read(vd_t *vd, efi_gpt_t **gpt, efi_gpe_t **gpe)
2152edcc0754Sachartre {
2153edcc0754Sachartre 	vd_efi_dev_t edev;
2154edcc0754Sachartre 	int status;
2155edcc0754Sachartre 
2156edcc0754Sachartre 	VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl);
2157edcc0754Sachartre 
2158edcc0754Sachartre 	status = vd_efi_alloc_and_read(&edev, gpt, gpe);
2159edcc0754Sachartre 
2160edcc0754Sachartre 	return (status);
2161edcc0754Sachartre }
2162edcc0754Sachartre 
2163edcc0754Sachartre static void
2164edcc0754Sachartre vds_efi_free(vd_t *vd, efi_gpt_t *gpt, efi_gpe_t *gpe)
2165edcc0754Sachartre {
2166edcc0754Sachartre 	vd_efi_dev_t edev;
2167edcc0754Sachartre 
2168edcc0754Sachartre 	VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl);
2169edcc0754Sachartre 
2170edcc0754Sachartre 	vd_efi_free(&edev, gpt, gpe);
2171edcc0754Sachartre }
2172edcc0754Sachartre 
2173edcc0754Sachartre static int
2174edcc0754Sachartre vd_file_validate_efi(vd_t *vd)
2175edcc0754Sachartre {
2176edcc0754Sachartre 	efi_gpt_t *gpt;
2177edcc0754Sachartre 	efi_gpe_t *gpe;
2178edcc0754Sachartre 	int i, nparts, status;
2179edcc0754Sachartre 	struct uuid efi_reserved = EFI_RESERVED;
2180edcc0754Sachartre 
2181edcc0754Sachartre 	if ((status = vds_efi_alloc_and_read(vd, &gpt, &gpe)) != 0)
2182edcc0754Sachartre 		return (status);
2183edcc0754Sachartre 
2184edcc0754Sachartre 	bzero(&vd->vtoc, sizeof (struct vtoc));
2185edcc0754Sachartre 	bzero(&vd->dk_geom, sizeof (struct dk_geom));
2186edcc0754Sachartre 	bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART);
2187edcc0754Sachartre 
2188edcc0754Sachartre 	vd->efi_reserved = -1;
2189edcc0754Sachartre 
2190edcc0754Sachartre 	nparts = gpt->efi_gpt_NumberOfPartitionEntries;
2191edcc0754Sachartre 
2192edcc0754Sachartre 	for (i = 0; i < nparts && i < VD_MAXPART; i++) {
2193edcc0754Sachartre 
2194edcc0754Sachartre 		if (gpe[i].efi_gpe_StartingLBA == 0 ||
2195edcc0754Sachartre 		    gpe[i].efi_gpe_EndingLBA == 0) {
2196edcc0754Sachartre 			continue;
2197edcc0754Sachartre 		}
2198edcc0754Sachartre 
2199edcc0754Sachartre 		vd->slices[i].start = gpe[i].efi_gpe_StartingLBA;
2200edcc0754Sachartre 		vd->slices[i].nblocks = gpe[i].efi_gpe_EndingLBA -
2201edcc0754Sachartre 		    gpe[i].efi_gpe_StartingLBA + 1;
2202edcc0754Sachartre 
2203edcc0754Sachartre 		if (bcmp(&gpe[i].efi_gpe_PartitionTypeGUID, &efi_reserved,
2204edcc0754Sachartre 		    sizeof (struct uuid)) == 0)
2205edcc0754Sachartre 			vd->efi_reserved = i;
2206edcc0754Sachartre 
2207edcc0754Sachartre 	}
2208edcc0754Sachartre 
2209edcc0754Sachartre 	ASSERT(vd->vdisk_size != 0);
2210edcc0754Sachartre 	vd->slices[VD_EFI_WD_SLICE].start = 0;
2211edcc0754Sachartre 	vd->slices[VD_EFI_WD_SLICE].nblocks = vd->vdisk_size;
2212edcc0754Sachartre 
2213edcc0754Sachartre 	vds_efi_free(vd, gpt, gpe);
2214edcc0754Sachartre 
2215edcc0754Sachartre 	return (status);
2216edcc0754Sachartre }
2217edcc0754Sachartre 
221887a7269eSachartre /*
221978fcd0a1Sachartre  * Function:
222078fcd0a1Sachartre  *	vd_file_validate_geometry
2221205eeb1aSlm66018  *
222278fcd0a1Sachartre  * Description:
222378fcd0a1Sachartre  *	Read the label and validate the geometry of a disk image. The driver
222478fcd0a1Sachartre  *	label, vtoc and geometry information are updated according to the
222578fcd0a1Sachartre  *	label read from the disk image.
222678fcd0a1Sachartre  *
222778fcd0a1Sachartre  *	If no valid label is found, the label is set to unknown and the
222878fcd0a1Sachartre  *	function returns EINVAL, but a default vtoc and geometry are provided
2229edcc0754Sachartre  *	to the driver. If an EFI label is found, ENOTSUP is returned.
223078fcd0a1Sachartre  *
223178fcd0a1Sachartre  * Parameters:
223278fcd0a1Sachartre  *	vd	- disk on which the operation is performed.
223378fcd0a1Sachartre  *
223478fcd0a1Sachartre  * Return Code:
223578fcd0a1Sachartre  *	0	- success.
223678fcd0a1Sachartre  *	EIO	- error reading the label from the disk image.
223778fcd0a1Sachartre  *	EINVAL	- unknown disk label.
2238edcc0754Sachartre  *	ENOTSUP	- geometry not applicable (EFI label).
223987a7269eSachartre  */
224087a7269eSachartre static int
224178fcd0a1Sachartre vd_file_validate_geometry(vd_t *vd)
224287a7269eSachartre {
224387a7269eSachartre 	struct dk_label label;
224478fcd0a1Sachartre 	struct dk_geom *geom = &vd->dk_geom;
224578fcd0a1Sachartre 	struct vtoc *vtoc = &vd->vtoc;
224678fcd0a1Sachartre 	int i;
224778fcd0a1Sachartre 	int status = 0;
224887a7269eSachartre 
224987a7269eSachartre 	ASSERT(vd->file);
2250edcc0754Sachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
225187a7269eSachartre 
225287a7269eSachartre 	if (VD_FILE_LABEL_READ(vd, &label) < 0)
225387a7269eSachartre 		return (EIO);
225487a7269eSachartre 
225587a7269eSachartre 	if (label.dkl_magic != DKL_MAGIC ||
225678fcd0a1Sachartre 	    label.dkl_cksum != vd_lbl2cksum(&label) ||
225766cfcfbeSachartre 	    (vd_file_validate_sanity && label.dkl_vtoc.v_sanity != VTOC_SANE) ||
225878fcd0a1Sachartre 	    label.dkl_vtoc.v_nparts != V_NUMPAR) {
2259edcc0754Sachartre 
2260edcc0754Sachartre 		if (vd_file_validate_efi(vd) == 0) {
2261edcc0754Sachartre 			vd->vdisk_label = VD_DISK_LABEL_EFI;
2262edcc0754Sachartre 			return (ENOTSUP);
2263edcc0754Sachartre 		}
2264edcc0754Sachartre 
226578fcd0a1Sachartre 		vd->vdisk_label = VD_DISK_LABEL_UNK;
226678fcd0a1Sachartre 		vd_file_build_default_label(vd, &label);
226778fcd0a1Sachartre 		status = EINVAL;
226878fcd0a1Sachartre 	} else {
226978fcd0a1Sachartre 		vd->vdisk_label = VD_DISK_LABEL_VTOC;
227078fcd0a1Sachartre 	}
227187a7269eSachartre 
227278fcd0a1Sachartre 	/* Update the driver geometry */
227387a7269eSachartre 	bzero(geom, sizeof (struct dk_geom));
227478fcd0a1Sachartre 
227587a7269eSachartre 	geom->dkg_ncyl = label.dkl_ncyl;
227687a7269eSachartre 	geom->dkg_acyl = label.dkl_acyl;
227787a7269eSachartre 	geom->dkg_nhead = label.dkl_nhead;
227887a7269eSachartre 	geom->dkg_nsect = label.dkl_nsect;
227987a7269eSachartre 	geom->dkg_intrlv = label.dkl_intrlv;
228087a7269eSachartre 	geom->dkg_apc = label.dkl_apc;
228187a7269eSachartre 	geom->dkg_rpm = label.dkl_rpm;
228287a7269eSachartre 	geom->dkg_pcyl = label.dkl_pcyl;
228387a7269eSachartre 	geom->dkg_write_reinstruct = label.dkl_write_reinstruct;
228487a7269eSachartre 	geom->dkg_read_reinstruct = label.dkl_read_reinstruct;
228587a7269eSachartre 
228678fcd0a1Sachartre 	/* Update the driver vtoc */
228787a7269eSachartre 	bzero(vtoc, sizeof (struct vtoc));
228887a7269eSachartre 
228987a7269eSachartre 	vtoc->v_sanity = label.dkl_vtoc.v_sanity;
229087a7269eSachartre 	vtoc->v_version = label.dkl_vtoc.v_version;
229187a7269eSachartre 	vtoc->v_sectorsz = DEV_BSIZE;
229287a7269eSachartre 	vtoc->v_nparts = label.dkl_vtoc.v_nparts;
229387a7269eSachartre 
229487a7269eSachartre 	for (i = 0; i < vtoc->v_nparts; i++) {
229587a7269eSachartre 		vtoc->v_part[i].p_tag =
229687a7269eSachartre 		    label.dkl_vtoc.v_part[i].p_tag;
229787a7269eSachartre 		vtoc->v_part[i].p_flag =
229887a7269eSachartre 		    label.dkl_vtoc.v_part[i].p_flag;
229987a7269eSachartre 		vtoc->v_part[i].p_start =
230087a7269eSachartre 		    label.dkl_map[i].dkl_cylno *
230187a7269eSachartre 		    (label.dkl_nhead * label.dkl_nsect);
230287a7269eSachartre 		vtoc->v_part[i].p_size = label.dkl_map[i].dkl_nblk;
230387a7269eSachartre 		vtoc->timestamp[i] =
230487a7269eSachartre 		    label.dkl_vtoc.v_timestamp[i];
230587a7269eSachartre 	}
230687a7269eSachartre 	/*
230787a7269eSachartre 	 * The bootinfo array can not be copied with bcopy() because
230887a7269eSachartre 	 * elements are of type long in vtoc (so 64-bit) and of type
230987a7269eSachartre 	 * int in dk_vtoc (so 32-bit).
231087a7269eSachartre 	 */
231187a7269eSachartre 	vtoc->v_bootinfo[0] = label.dkl_vtoc.v_bootinfo[0];
231287a7269eSachartre 	vtoc->v_bootinfo[1] = label.dkl_vtoc.v_bootinfo[1];
231387a7269eSachartre 	vtoc->v_bootinfo[2] = label.dkl_vtoc.v_bootinfo[2];
231487a7269eSachartre 	bcopy(label.dkl_asciilabel, vtoc->v_asciilabel,
231587a7269eSachartre 	    LEN_DKL_ASCII);
231687a7269eSachartre 	bcopy(label.dkl_vtoc.v_volume, vtoc->v_volume,
231787a7269eSachartre 	    LEN_DKL_VVOL);
231887a7269eSachartre 
2319edcc0754Sachartre 	/* Update logical partitions */
2320edcc0754Sachartre 	bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART);
2321edcc0754Sachartre 	if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
2322edcc0754Sachartre 		for (i = 0; i < vtoc->v_nparts; i++) {
2323edcc0754Sachartre 			vd->slices[i].start = vtoc->v_part[i].p_start;
2324edcc0754Sachartre 			vd->slices[i].nblocks = vtoc->v_part[i].p_size;
2325edcc0754Sachartre 		}
2326edcc0754Sachartre 	}
2327edcc0754Sachartre 
232878fcd0a1Sachartre 	return (status);
232978fcd0a1Sachartre }
233078fcd0a1Sachartre 
233178fcd0a1Sachartre /*
233278fcd0a1Sachartre  * Handle ioctls to a disk image (file-based).
233378fcd0a1Sachartre  *
233478fcd0a1Sachartre  * Return Values
233578fcd0a1Sachartre  *	0	- Indicates that there are no errors
233678fcd0a1Sachartre  *	!= 0	- Disk operation returned an error
233778fcd0a1Sachartre  */
233878fcd0a1Sachartre static int
233978fcd0a1Sachartre vd_do_file_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
234078fcd0a1Sachartre {
234178fcd0a1Sachartre 	struct dk_label label;
234278fcd0a1Sachartre 	struct dk_geom *geom;
234378fcd0a1Sachartre 	struct vtoc *vtoc;
2344edcc0754Sachartre 	dk_efi_t *efi;
234578fcd0a1Sachartre 	int i, rc;
234678fcd0a1Sachartre 
234778fcd0a1Sachartre 	ASSERT(vd->file);
2348edcc0754Sachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
234978fcd0a1Sachartre 
235078fcd0a1Sachartre 	switch (cmd) {
235178fcd0a1Sachartre 
235278fcd0a1Sachartre 	case DKIOCGGEOM:
235378fcd0a1Sachartre 		ASSERT(ioctl_arg != NULL);
235478fcd0a1Sachartre 		geom = (struct dk_geom *)ioctl_arg;
235578fcd0a1Sachartre 
235678fcd0a1Sachartre 		rc = vd_file_validate_geometry(vd);
2357edcc0754Sachartre 		if (rc != 0 && rc != EINVAL)
235878fcd0a1Sachartre 			return (rc);
235978fcd0a1Sachartre 		bcopy(&vd->dk_geom, geom, sizeof (struct dk_geom));
236078fcd0a1Sachartre 		return (0);
236178fcd0a1Sachartre 
236278fcd0a1Sachartre 	case DKIOCGVTOC:
236378fcd0a1Sachartre 		ASSERT(ioctl_arg != NULL);
236478fcd0a1Sachartre 		vtoc = (struct vtoc *)ioctl_arg;
236578fcd0a1Sachartre 
236678fcd0a1Sachartre 		rc = vd_file_validate_geometry(vd);
2367edcc0754Sachartre 		if (rc != 0 && rc != EINVAL)
236878fcd0a1Sachartre 			return (rc);
236978fcd0a1Sachartre 		bcopy(&vd->vtoc, vtoc, sizeof (struct vtoc));
237087a7269eSachartre 		return (0);
237187a7269eSachartre 
237287a7269eSachartre 	case DKIOCSGEOM:
237387a7269eSachartre 		ASSERT(ioctl_arg != NULL);
237487a7269eSachartre 		geom = (struct dk_geom *)ioctl_arg;
237587a7269eSachartre 
237687a7269eSachartre 		if (geom->dkg_nhead == 0 || geom->dkg_nsect == 0)
237787a7269eSachartre 			return (EINVAL);
237887a7269eSachartre 
237987a7269eSachartre 		/*
238087a7269eSachartre 		 * The current device geometry is not updated, just the driver
238187a7269eSachartre 		 * "notion" of it. The device geometry will be effectively
238287a7269eSachartre 		 * updated when a label is written to the device during a next
238387a7269eSachartre 		 * DKIOCSVTOC.
238487a7269eSachartre 		 */
238587a7269eSachartre 		bcopy(ioctl_arg, &vd->dk_geom, sizeof (vd->dk_geom));
238687a7269eSachartre 		return (0);
238787a7269eSachartre 
238887a7269eSachartre 	case DKIOCSVTOC:
238987a7269eSachartre 		ASSERT(ioctl_arg != NULL);
239087a7269eSachartre 		ASSERT(vd->dk_geom.dkg_nhead != 0 &&
239187a7269eSachartre 		    vd->dk_geom.dkg_nsect != 0);
2392690555a1Sachartre 		vtoc = (struct vtoc *)ioctl_arg;
2393690555a1Sachartre 
2394690555a1Sachartre 		if (vtoc->v_sanity != VTOC_SANE ||
2395690555a1Sachartre 		    vtoc->v_sectorsz != DEV_BSIZE ||
2396690555a1Sachartre 		    vtoc->v_nparts != V_NUMPAR)
2397690555a1Sachartre 			return (EINVAL);
2398690555a1Sachartre 
2399690555a1Sachartre 		bzero(&label, sizeof (label));
2400690555a1Sachartre 		label.dkl_ncyl = vd->dk_geom.dkg_ncyl;
2401690555a1Sachartre 		label.dkl_acyl = vd->dk_geom.dkg_acyl;
2402690555a1Sachartre 		label.dkl_pcyl = vd->dk_geom.dkg_pcyl;
2403690555a1Sachartre 		label.dkl_nhead = vd->dk_geom.dkg_nhead;
2404690555a1Sachartre 		label.dkl_nsect = vd->dk_geom.dkg_nsect;
2405690555a1Sachartre 		label.dkl_intrlv = vd->dk_geom.dkg_intrlv;
2406690555a1Sachartre 		label.dkl_apc = vd->dk_geom.dkg_apc;
2407690555a1Sachartre 		label.dkl_rpm = vd->dk_geom.dkg_rpm;
240887a7269eSachartre 		label.dkl_write_reinstruct = vd->dk_geom.dkg_write_reinstruct;
240987a7269eSachartre 		label.dkl_read_reinstruct = vd->dk_geom.dkg_read_reinstruct;
2410690555a1Sachartre 
241187a7269eSachartre 		label.dkl_vtoc.v_nparts = V_NUMPAR;
241287a7269eSachartre 		label.dkl_vtoc.v_sanity = VTOC_SANE;
2413690555a1Sachartre 		label.dkl_vtoc.v_version = vtoc->v_version;
241487a7269eSachartre 		for (i = 0; i < V_NUMPAR; i++) {
2415690555a1Sachartre 			label.dkl_vtoc.v_timestamp[i] =
2416690555a1Sachartre 			    vtoc->timestamp[i];
2417690555a1Sachartre 			label.dkl_vtoc.v_part[i].p_tag =
2418690555a1Sachartre 			    vtoc->v_part[i].p_tag;
2419690555a1Sachartre 			label.dkl_vtoc.v_part[i].p_flag =
2420690555a1Sachartre 			    vtoc->v_part[i].p_flag;
2421690555a1Sachartre 			label.dkl_map[i].dkl_cylno =
2422690555a1Sachartre 			    vtoc->v_part[i].p_start /
2423690555a1Sachartre 			    (label.dkl_nhead * label.dkl_nsect);
2424690555a1Sachartre 			label.dkl_map[i].dkl_nblk =
2425690555a1Sachartre 			    vtoc->v_part[i].p_size;
24263c96341aSnarayan 		}
242787a7269eSachartre 		/*
242887a7269eSachartre 		 * The bootinfo array can not be copied with bcopy() because
242987a7269eSachartre 		 * elements are of type long in vtoc (so 64-bit) and of type
243087a7269eSachartre 		 * int in dk_vtoc (so 32-bit).
243187a7269eSachartre 		 */
243287a7269eSachartre 		label.dkl_vtoc.v_bootinfo[0] = vtoc->v_bootinfo[0];
243387a7269eSachartre 		label.dkl_vtoc.v_bootinfo[1] = vtoc->v_bootinfo[1];
243487a7269eSachartre 		label.dkl_vtoc.v_bootinfo[2] = vtoc->v_bootinfo[2];
2435690555a1Sachartre 		bcopy(vtoc->v_asciilabel, label.dkl_asciilabel,
2436690555a1Sachartre 		    LEN_DKL_ASCII);
2437690555a1Sachartre 		bcopy(vtoc->v_volume, label.dkl_vtoc.v_volume,
2438690555a1Sachartre 		    LEN_DKL_VVOL);
24393c96341aSnarayan 
24403c96341aSnarayan 		/* re-compute checksum */
2441690555a1Sachartre 		label.dkl_magic = DKL_MAGIC;
2442690555a1Sachartre 		label.dkl_cksum = vd_lbl2cksum(&label);
2443690555a1Sachartre 
244487a7269eSachartre 		/* write label to the disk image */
244587a7269eSachartre 		if ((rc = vd_file_set_vtoc(vd, &label)) != 0)
244687a7269eSachartre 			return (rc);
2447690555a1Sachartre 
2448edcc0754Sachartre 		break;
2449edcc0754Sachartre 
2450edcc0754Sachartre 	case DKIOCFLUSHWRITECACHE:
2451edcc0754Sachartre 		return (VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL));
2452edcc0754Sachartre 
2453edcc0754Sachartre 	case DKIOCGETEFI:
2454edcc0754Sachartre 		ASSERT(ioctl_arg != NULL);
2455edcc0754Sachartre 		efi = (dk_efi_t *)ioctl_arg;
2456edcc0754Sachartre 
2457edcc0754Sachartre 		if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD,
2458edcc0754Sachartre 		    (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0)
2459edcc0754Sachartre 			return (EIO);
2460edcc0754Sachartre 
2461edcc0754Sachartre 		return (0);
2462edcc0754Sachartre 
2463edcc0754Sachartre 	case DKIOCSETEFI:
2464edcc0754Sachartre 		ASSERT(ioctl_arg != NULL);
2465edcc0754Sachartre 		efi = (dk_efi_t *)ioctl_arg;
2466edcc0754Sachartre 
2467edcc0754Sachartre 		if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE,
2468edcc0754Sachartre 		    (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0)
2469edcc0754Sachartre 			return (EIO);
2470edcc0754Sachartre 
2471edcc0754Sachartre 		break;
2472edcc0754Sachartre 
2473edcc0754Sachartre 
2474edcc0754Sachartre 	default:
2475edcc0754Sachartre 		return (ENOTSUP);
2476edcc0754Sachartre 	}
2477edcc0754Sachartre 
2478edcc0754Sachartre 	ASSERT(cmd == DKIOCSVTOC || cmd == DKIOCSETEFI);
2479edcc0754Sachartre 
2480edcc0754Sachartre 	/* label has changed, revalidate the geometry */
2481edcc0754Sachartre 	(void) vd_file_validate_geometry(vd);
24823c96341aSnarayan 
248387a7269eSachartre 	/*
248487a7269eSachartre 	 * The disk geometry may have changed, so we need to write
248587a7269eSachartre 	 * the devid (if there is one) so that it is stored at the
248687a7269eSachartre 	 * right location.
248787a7269eSachartre 	 */
2488edcc0754Sachartre 	if (vd_file_write_devid(vd, vd->file_devid) != 0) {
248987a7269eSachartre 		PR0("Fail to write devid");
24901ae08745Sheppo 	}
24914bac2208Snarayan 
24924bac2208Snarayan 	return (0);
24934bac2208Snarayan }
2494edcc0754Sachartre 
2495edcc0754Sachartre static int
2496edcc0754Sachartre vd_backend_ioctl(vd_t *vd, int cmd, caddr_t arg)
2497edcc0754Sachartre {
2498edcc0754Sachartre 	int rval = 0, status;
2499edcc0754Sachartre 
2500edcc0754Sachartre 	/*
2501edcc0754Sachartre 	 * Call the appropriate function to execute the ioctl depending
2502edcc0754Sachartre 	 * on the type of vdisk.
2503edcc0754Sachartre 	 */
2504edcc0754Sachartre 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
2505edcc0754Sachartre 
2506edcc0754Sachartre 		/* slice, file or volume exported as a single slice disk */
2507edcc0754Sachartre 		status = vd_do_slice_ioctl(vd, cmd, arg);
2508edcc0754Sachartre 
2509edcc0754Sachartre 	} else if (vd->file) {
2510edcc0754Sachartre 
2511edcc0754Sachartre 		/* file or volume exported as a full disk */
2512edcc0754Sachartre 		status = vd_do_file_ioctl(vd, cmd, arg);
2513edcc0754Sachartre 
2514edcc0754Sachartre 	} else {
2515edcc0754Sachartre 
2516edcc0754Sachartre 		/* disk device exported as a full disk */
2517edcc0754Sachartre 		status = ldi_ioctl(vd->ldi_handle[0], cmd, (intptr_t)arg,
2518edcc0754Sachartre 		    vd->open_flags | FKIOCTL, kcred, &rval);
2519edcc0754Sachartre 	}
2520edcc0754Sachartre 
2521edcc0754Sachartre #ifdef DEBUG
2522edcc0754Sachartre 	if (rval != 0) {
2523edcc0754Sachartre 		PR0("ioctl %x set rval = %d, which is not being returned"
2524edcc0754Sachartre 		    " to caller", cmd, rval);
2525edcc0754Sachartre 	}
2526edcc0754Sachartre #endif /* DEBUG */
2527edcc0754Sachartre 
2528edcc0754Sachartre 	return (status);
25291ae08745Sheppo }
25301ae08745Sheppo 
2531205eeb1aSlm66018 /*
2532205eeb1aSlm66018  * Description:
2533205eeb1aSlm66018  *	This is the function that processes the ioctl requests (farming it
2534205eeb1aSlm66018  *	out to functions that handle slices, files or whole disks)
2535205eeb1aSlm66018  *
2536205eeb1aSlm66018  * Return Values
2537205eeb1aSlm66018  *     0		- ioctl operation completed successfully
2538205eeb1aSlm66018  *     != 0		- The LDC error value encountered
2539205eeb1aSlm66018  *			  (propagated back up the call stack as a task error)
2540205eeb1aSlm66018  *
2541205eeb1aSlm66018  * Side Effect
2542205eeb1aSlm66018  *     sets request->status to the return value of the ioctl function.
2543205eeb1aSlm66018  */
25441ae08745Sheppo static int
25450a55fbb7Slm66018 vd_do_ioctl(vd_t *vd, vd_dring_payload_t *request, void* buf, vd_ioctl_t *ioctl)
25461ae08745Sheppo {
2547edcc0754Sachartre 	int	status = 0;
25481ae08745Sheppo 	size_t	nbytes = request->nbytes;	/* modifiable copy */
25491ae08745Sheppo 
25501ae08745Sheppo 
25511ae08745Sheppo 	ASSERT(request->slice < vd->nslices);
25521ae08745Sheppo 	PR0("Performing %s", ioctl->operation_name);
25531ae08745Sheppo 
25540a55fbb7Slm66018 	/* Get data from client and convert, if necessary */
25550a55fbb7Slm66018 	if (ioctl->copyin != NULL)  {
25561ae08745Sheppo 		ASSERT(nbytes != 0 && buf != NULL);
25571ae08745Sheppo 		PR1("Getting \"arg\" data from client");
25581ae08745Sheppo 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
25591ae08745Sheppo 		    request->cookie, request->ncookies,
25601ae08745Sheppo 		    LDC_COPY_IN)) != 0) {
25613af08d82Slm66018 			PR0("ldc_mem_copy() returned errno %d "
25621ae08745Sheppo 			    "copying from client", status);
25631ae08745Sheppo 			return (status);
25641ae08745Sheppo 		}
25650a55fbb7Slm66018 
25660a55fbb7Slm66018 		/* Convert client's data, if necessary */
25672f5224aeSachartre 		if (ioctl->copyin == VD_IDENTITY_IN) {
25682f5224aeSachartre 			/* use client buffer */
25690a55fbb7Slm66018 			ioctl->arg = buf;
25702f5224aeSachartre 		} else {
25712f5224aeSachartre 			/* convert client vdisk operation data to ioctl data */
25722f5224aeSachartre 			status = (ioctl->copyin)(buf, nbytes,
25732f5224aeSachartre 			    (void *)ioctl->arg);
25742f5224aeSachartre 			if (status != 0) {
25752f5224aeSachartre 				request->status = status;
25762f5224aeSachartre 				return (0);
25772f5224aeSachartre 			}
25782f5224aeSachartre 		}
25792f5224aeSachartre 	}
25802f5224aeSachartre 
25812f5224aeSachartre 	if (ioctl->operation == VD_OP_SCSICMD) {
25822f5224aeSachartre 		struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl->arg;
25832f5224aeSachartre 
25842f5224aeSachartre 		/* check write permission */
25852f5224aeSachartre 		if (!(vd->open_flags & FWRITE) &&
25862f5224aeSachartre 		    !(uscsi->uscsi_flags & USCSI_READ)) {
25872f5224aeSachartre 			PR0("uscsi fails because backend is opened read-only");
25882f5224aeSachartre 			request->status = EROFS;
25892f5224aeSachartre 			return (0);
25902f5224aeSachartre 		}
25911ae08745Sheppo 	}
25921ae08745Sheppo 
25931ae08745Sheppo 	/*
2594edcc0754Sachartre 	 * Send the ioctl to the disk backend.
25951ae08745Sheppo 	 */
2596edcc0754Sachartre 	request->status = vd_backend_ioctl(vd, ioctl->cmd, ioctl->arg);
2597205eeb1aSlm66018 
2598205eeb1aSlm66018 	if (request->status != 0) {
2599205eeb1aSlm66018 		PR0("ioctl(%s) = errno %d", ioctl->cmd_name, request->status);
26002f5224aeSachartre 		if (ioctl->operation == VD_OP_SCSICMD &&
26012f5224aeSachartre 		    ((struct uscsi_cmd *)ioctl->arg)->uscsi_status != 0)
26022f5224aeSachartre 			/*
26032f5224aeSachartre 			 * USCSICMD has reported an error and the uscsi_status
26042f5224aeSachartre 			 * field is not zero. This means that the SCSI command
26052f5224aeSachartre 			 * has completed but it has an error. So we should
26062f5224aeSachartre 			 * mark the VD operation has succesfully completed
26072f5224aeSachartre 			 * and clients can check the SCSI status field for
26082f5224aeSachartre 			 * SCSI errors.
26092f5224aeSachartre 			 */
26102f5224aeSachartre 			request->status = 0;
26112f5224aeSachartre 		else
2612205eeb1aSlm66018 			return (0);
2613205eeb1aSlm66018 	}
26141ae08745Sheppo 
26150a55fbb7Slm66018 	/* Convert data and send to client, if necessary */
26160a55fbb7Slm66018 	if (ioctl->copyout != NULL)  {
26171ae08745Sheppo 		ASSERT(nbytes != 0 && buf != NULL);
26181ae08745Sheppo 		PR1("Sending \"arg\" data to client");
26190a55fbb7Slm66018 
26200a55fbb7Slm66018 		/* Convert ioctl data to vdisk operation data, if necessary */
26212f5224aeSachartre 		if (ioctl->copyout != VD_IDENTITY_OUT)
26220a55fbb7Slm66018 			(ioctl->copyout)((void *)ioctl->arg, buf);
26230a55fbb7Slm66018 
26241ae08745Sheppo 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
26251ae08745Sheppo 		    request->cookie, request->ncookies,
26261ae08745Sheppo 		    LDC_COPY_OUT)) != 0) {
26273af08d82Slm66018 			PR0("ldc_mem_copy() returned errno %d "
26281ae08745Sheppo 			    "copying to client", status);
26291ae08745Sheppo 			return (status);
26301ae08745Sheppo 		}
26311ae08745Sheppo 	}
26321ae08745Sheppo 
26331ae08745Sheppo 	return (status);
26341ae08745Sheppo }
26351ae08745Sheppo 
26361ae08745Sheppo #define	RNDSIZE(expr) P2ROUNDUP(sizeof (expr), sizeof (uint64_t))
2637205eeb1aSlm66018 
2638205eeb1aSlm66018 /*
2639205eeb1aSlm66018  * Description:
2640205eeb1aSlm66018  *	This generic function is called by the task queue to complete
2641205eeb1aSlm66018  *	the processing of the tasks. The specific completion function
2642205eeb1aSlm66018  *	is passed in as a field in the task pointer.
2643205eeb1aSlm66018  *
2644205eeb1aSlm66018  * Parameters:
2645205eeb1aSlm66018  *	arg 	- opaque pointer to structure containing task to be completed
2646205eeb1aSlm66018  *
2647205eeb1aSlm66018  * Return Values
2648205eeb1aSlm66018  *	None
2649205eeb1aSlm66018  */
2650205eeb1aSlm66018 static void
2651205eeb1aSlm66018 vd_complete(void *arg)
2652205eeb1aSlm66018 {
2653205eeb1aSlm66018 	vd_task_t	*task = (vd_task_t *)arg;
2654205eeb1aSlm66018 
2655205eeb1aSlm66018 	ASSERT(task != NULL);
2656205eeb1aSlm66018 	ASSERT(task->status == EINPROGRESS);
2657205eeb1aSlm66018 	ASSERT(task->completef != NULL);
2658205eeb1aSlm66018 
2659205eeb1aSlm66018 	task->status = task->completef(task);
2660205eeb1aSlm66018 	if (task->status)
2661205eeb1aSlm66018 		PR0("%s: Error %d completing task", __func__, task->status);
2662205eeb1aSlm66018 
2663205eeb1aSlm66018 	/* Now notify the vDisk client */
2664205eeb1aSlm66018 	vd_complete_notify(task);
2665205eeb1aSlm66018 }
2666205eeb1aSlm66018 
26671ae08745Sheppo static int
2668d10e4ef2Snarayan vd_ioctl(vd_task_t *task)
26691ae08745Sheppo {
267087a7269eSachartre 	int			i, status;
26711ae08745Sheppo 	void			*buf = NULL;
26720a55fbb7Slm66018 	struct dk_geom		dk_geom = {0};
26730a55fbb7Slm66018 	struct vtoc		vtoc = {0};
26744bac2208Snarayan 	struct dk_efi		dk_efi = {0};
26752f5224aeSachartre 	struct uscsi_cmd	uscsi = {0};
2676d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
2677d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
26780a55fbb7Slm66018 	vd_ioctl_t		ioctl[] = {
26790a55fbb7Slm66018 		/* Command (no-copy) operations */
26800a55fbb7Slm66018 		{VD_OP_FLUSH, STRINGIZE(VD_OP_FLUSH), 0,
26810a55fbb7Slm66018 		    DKIOCFLUSHWRITECACHE, STRINGIZE(DKIOCFLUSHWRITECACHE),
2682047ba61eSachartre 		    NULL, NULL, NULL, B_TRUE},
26830a55fbb7Slm66018 
26840a55fbb7Slm66018 		/* "Get" (copy-out) operations */
26850a55fbb7Slm66018 		{VD_OP_GET_WCE, STRINGIZE(VD_OP_GET_WCE), RNDSIZE(int),
26860a55fbb7Slm66018 		    DKIOCGETWCE, STRINGIZE(DKIOCGETWCE),
26872f5224aeSachartre 		    NULL, VD_IDENTITY_IN, VD_IDENTITY_OUT, B_FALSE},
26880a55fbb7Slm66018 		{VD_OP_GET_DISKGEOM, STRINGIZE(VD_OP_GET_DISKGEOM),
26890a55fbb7Slm66018 		    RNDSIZE(vd_geom_t),
26900a55fbb7Slm66018 		    DKIOCGGEOM, STRINGIZE(DKIOCGGEOM),
2691047ba61eSachartre 		    &dk_geom, NULL, dk_geom2vd_geom, B_FALSE},
26920a55fbb7Slm66018 		{VD_OP_GET_VTOC, STRINGIZE(VD_OP_GET_VTOC), RNDSIZE(vd_vtoc_t),
26930a55fbb7Slm66018 		    DKIOCGVTOC, STRINGIZE(DKIOCGVTOC),
2694047ba61eSachartre 		    &vtoc, NULL, vtoc2vd_vtoc, B_FALSE},
26954bac2208Snarayan 		{VD_OP_GET_EFI, STRINGIZE(VD_OP_GET_EFI), RNDSIZE(vd_efi_t),
26964bac2208Snarayan 		    DKIOCGETEFI, STRINGIZE(DKIOCGETEFI),
2697047ba61eSachartre 		    &dk_efi, vd_get_efi_in, vd_get_efi_out, B_FALSE},
26980a55fbb7Slm66018 
26990a55fbb7Slm66018 		/* "Set" (copy-in) operations */
27000a55fbb7Slm66018 		{VD_OP_SET_WCE, STRINGIZE(VD_OP_SET_WCE), RNDSIZE(int),
27010a55fbb7Slm66018 		    DKIOCSETWCE, STRINGIZE(DKIOCSETWCE),
27022f5224aeSachartre 		    NULL, VD_IDENTITY_IN, VD_IDENTITY_OUT, B_TRUE},
27030a55fbb7Slm66018 		{VD_OP_SET_DISKGEOM, STRINGIZE(VD_OP_SET_DISKGEOM),
27040a55fbb7Slm66018 		    RNDSIZE(vd_geom_t),
27050a55fbb7Slm66018 		    DKIOCSGEOM, STRINGIZE(DKIOCSGEOM),
2706047ba61eSachartre 		    &dk_geom, vd_geom2dk_geom, NULL, B_TRUE},
27070a55fbb7Slm66018 		{VD_OP_SET_VTOC, STRINGIZE(VD_OP_SET_VTOC), RNDSIZE(vd_vtoc_t),
27080a55fbb7Slm66018 		    DKIOCSVTOC, STRINGIZE(DKIOCSVTOC),
2709047ba61eSachartre 		    &vtoc, vd_vtoc2vtoc, NULL, B_TRUE},
27104bac2208Snarayan 		{VD_OP_SET_EFI, STRINGIZE(VD_OP_SET_EFI), RNDSIZE(vd_efi_t),
27114bac2208Snarayan 		    DKIOCSETEFI, STRINGIZE(DKIOCSETEFI),
2712047ba61eSachartre 		    &dk_efi, vd_set_efi_in, vd_set_efi_out, B_TRUE},
27132f5224aeSachartre 
27142f5224aeSachartre 		{VD_OP_SCSICMD, STRINGIZE(VD_OP_SCSICMD), RNDSIZE(vd_scsi_t),
27152f5224aeSachartre 		    USCSICMD, STRINGIZE(USCSICMD),
27162f5224aeSachartre 		    &uscsi, vd_scsicmd_in, vd_scsicmd_out, B_FALSE},
27170a55fbb7Slm66018 	};
27181ae08745Sheppo 	size_t		nioctls = (sizeof (ioctl))/(sizeof (ioctl[0]));
27191ae08745Sheppo 
27201ae08745Sheppo 
2721d10e4ef2Snarayan 	ASSERT(vd != NULL);
2722d10e4ef2Snarayan 	ASSERT(request != NULL);
27231ae08745Sheppo 	ASSERT(request->slice < vd->nslices);
27241ae08745Sheppo 
27251ae08745Sheppo 	/*
27261ae08745Sheppo 	 * Determine ioctl corresponding to caller's "operation" and
27271ae08745Sheppo 	 * validate caller's "nbytes"
27281ae08745Sheppo 	 */
27291ae08745Sheppo 	for (i = 0; i < nioctls; i++) {
27301ae08745Sheppo 		if (request->operation == ioctl[i].operation) {
27310a55fbb7Slm66018 			/* LDC memory operations require 8-byte multiples */
27320a55fbb7Slm66018 			ASSERT(ioctl[i].nbytes % sizeof (uint64_t) == 0);
27330a55fbb7Slm66018 
27344bac2208Snarayan 			if (request->operation == VD_OP_GET_EFI ||
27352f5224aeSachartre 			    request->operation == VD_OP_SET_EFI ||
27362f5224aeSachartre 			    request->operation == VD_OP_SCSICMD) {
27374bac2208Snarayan 				if (request->nbytes >= ioctl[i].nbytes)
27384bac2208Snarayan 					break;
27393af08d82Slm66018 				PR0("%s:  Expected at least nbytes = %lu, "
27404bac2208Snarayan 				    "got %lu", ioctl[i].operation_name,
27414bac2208Snarayan 				    ioctl[i].nbytes, request->nbytes);
27424bac2208Snarayan 				return (EINVAL);
27434bac2208Snarayan 			}
27444bac2208Snarayan 
27450a55fbb7Slm66018 			if (request->nbytes != ioctl[i].nbytes) {
27463af08d82Slm66018 				PR0("%s:  Expected nbytes = %lu, got %lu",
27470a55fbb7Slm66018 				    ioctl[i].operation_name, ioctl[i].nbytes,
27480a55fbb7Slm66018 				    request->nbytes);
27491ae08745Sheppo 				return (EINVAL);
27501ae08745Sheppo 			}
27511ae08745Sheppo 
27521ae08745Sheppo 			break;
27531ae08745Sheppo 		}
27541ae08745Sheppo 	}
27551ae08745Sheppo 	ASSERT(i < nioctls);	/* because "operation" already validated */
27561ae08745Sheppo 
2757047ba61eSachartre 	if (!(vd->open_flags & FWRITE) && ioctl[i].write) {
2758047ba61eSachartre 		PR0("%s fails because backend is opened read-only",
2759047ba61eSachartre 		    ioctl[i].operation_name);
2760047ba61eSachartre 		request->status = EROFS;
2761047ba61eSachartre 		return (0);
2762047ba61eSachartre 	}
2763047ba61eSachartre 
27641ae08745Sheppo 	if (request->nbytes)
27651ae08745Sheppo 		buf = kmem_zalloc(request->nbytes, KM_SLEEP);
27661ae08745Sheppo 	status = vd_do_ioctl(vd, request, buf, &ioctl[i]);
27671ae08745Sheppo 	if (request->nbytes)
27681ae08745Sheppo 		kmem_free(buf, request->nbytes);
276987a7269eSachartre 
27701ae08745Sheppo 	return (status);
27711ae08745Sheppo }
27721ae08745Sheppo 
27734bac2208Snarayan static int
27744bac2208Snarayan vd_get_devid(vd_task_t *task)
27754bac2208Snarayan {
27764bac2208Snarayan 	vd_t *vd = task->vd;
27774bac2208Snarayan 	vd_dring_payload_t *request = task->request;
27784bac2208Snarayan 	vd_devid_t *vd_devid;
27794bac2208Snarayan 	impl_devid_t *devid;
278087a7269eSachartre 	int status, bufid_len, devid_len, len, sz;
27813af08d82Slm66018 	int bufbytes;
27824bac2208Snarayan 
27833af08d82Slm66018 	PR1("Get Device ID, nbytes=%ld", request->nbytes);
27844bac2208Snarayan 
27853c96341aSnarayan 	if (vd->file) {
278687a7269eSachartre 		if (vd->file_devid == NULL) {
27873af08d82Slm66018 			PR2("No Device ID");
2788205eeb1aSlm66018 			request->status = ENOENT;
2789205eeb1aSlm66018 			return (0);
279087a7269eSachartre 		} else {
279187a7269eSachartre 			sz = ddi_devid_sizeof(vd->file_devid);
279287a7269eSachartre 			devid = kmem_alloc(sz, KM_SLEEP);
279387a7269eSachartre 			bcopy(vd->file_devid, devid, sz);
279487a7269eSachartre 		}
279587a7269eSachartre 	} else {
279687a7269eSachartre 		if (ddi_lyr_get_devid(vd->dev[request->slice],
279787a7269eSachartre 		    (ddi_devid_t *)&devid) != DDI_SUCCESS) {
279887a7269eSachartre 			PR2("No Device ID");
2799205eeb1aSlm66018 			request->status = ENOENT;
2800205eeb1aSlm66018 			return (0);
280187a7269eSachartre 		}
28024bac2208Snarayan 	}
28034bac2208Snarayan 
28044bac2208Snarayan 	bufid_len = request->nbytes - sizeof (vd_devid_t) + 1;
28054bac2208Snarayan 	devid_len = DEVID_GETLEN(devid);
28064bac2208Snarayan 
28073af08d82Slm66018 	/*
28083af08d82Slm66018 	 * Save the buffer size here for use in deallocation.
28093af08d82Slm66018 	 * The actual number of bytes copied is returned in
28103af08d82Slm66018 	 * the 'nbytes' field of the request structure.
28113af08d82Slm66018 	 */
28123af08d82Slm66018 	bufbytes = request->nbytes;
28133af08d82Slm66018 
28143af08d82Slm66018 	vd_devid = kmem_zalloc(bufbytes, KM_SLEEP);
28154bac2208Snarayan 	vd_devid->length = devid_len;
28164bac2208Snarayan 	vd_devid->type = DEVID_GETTYPE(devid);
28174bac2208Snarayan 
28184bac2208Snarayan 	len = (devid_len > bufid_len)? bufid_len : devid_len;
28194bac2208Snarayan 
28204bac2208Snarayan 	bcopy(devid->did_id, vd_devid->id, len);
28214bac2208Snarayan 
282278fcd0a1Sachartre 	request->status = 0;
282378fcd0a1Sachartre 
28244bac2208Snarayan 	/* LDC memory operations require 8-byte multiples */
28254bac2208Snarayan 	ASSERT(request->nbytes % sizeof (uint64_t) == 0);
28264bac2208Snarayan 
28274bac2208Snarayan 	if ((status = ldc_mem_copy(vd->ldc_handle, (caddr_t)vd_devid, 0,
28284bac2208Snarayan 	    &request->nbytes, request->cookie, request->ncookies,
28294bac2208Snarayan 	    LDC_COPY_OUT)) != 0) {
28303af08d82Slm66018 		PR0("ldc_mem_copy() returned errno %d copying to client",
28314bac2208Snarayan 		    status);
28324bac2208Snarayan 	}
28333af08d82Slm66018 	PR1("post mem_copy: nbytes=%ld", request->nbytes);
28344bac2208Snarayan 
28353af08d82Slm66018 	kmem_free(vd_devid, bufbytes);
28364bac2208Snarayan 	ddi_devid_free((ddi_devid_t)devid);
28374bac2208Snarayan 
28384bac2208Snarayan 	return (status);
28394bac2208Snarayan }
28404bac2208Snarayan 
28412f5224aeSachartre static int
28422f5224aeSachartre vd_scsi_reset(vd_t *vd)
28432f5224aeSachartre {
28442f5224aeSachartre 	int rval, status;
28452f5224aeSachartre 	struct uscsi_cmd uscsi = { 0 };
28462f5224aeSachartre 
28472f5224aeSachartre 	uscsi.uscsi_flags = vd_scsi_debug | USCSI_RESET;
28482f5224aeSachartre 	uscsi.uscsi_timeout = vd_scsi_rdwr_timeout;
28492f5224aeSachartre 
28502f5224aeSachartre 	status = ldi_ioctl(vd->ldi_handle[0], USCSICMD, (intptr_t)&uscsi,
28512f5224aeSachartre 	    (vd->open_flags | FKIOCTL), kcred, &rval);
28522f5224aeSachartre 
28532f5224aeSachartre 	return (status);
28542f5224aeSachartre }
28552f5224aeSachartre 
28562f5224aeSachartre static int
28572f5224aeSachartre vd_reset(vd_task_t *task)
28582f5224aeSachartre {
28592f5224aeSachartre 	vd_t *vd = task->vd;
28602f5224aeSachartre 	vd_dring_payload_t *request = task->request;
28612f5224aeSachartre 
28622f5224aeSachartre 	ASSERT(request->operation == VD_OP_RESET);
28632f5224aeSachartre 	ASSERT(vd->scsi);
28642f5224aeSachartre 
28652f5224aeSachartre 	PR0("Performing VD_OP_RESET");
28662f5224aeSachartre 
28672f5224aeSachartre 	if (request->nbytes != 0) {
28682f5224aeSachartre 		PR0("VD_OP_RESET:  Expected nbytes = 0, got %lu",
28692f5224aeSachartre 		    request->nbytes);
28702f5224aeSachartre 		return (EINVAL);
28712f5224aeSachartre 	}
28722f5224aeSachartre 
28732f5224aeSachartre 	request->status = vd_scsi_reset(vd);
28742f5224aeSachartre 
28752f5224aeSachartre 	return (0);
28762f5224aeSachartre }
28772f5224aeSachartre 
28782f5224aeSachartre static int
28792f5224aeSachartre vd_get_capacity(vd_task_t *task)
28802f5224aeSachartre {
28812f5224aeSachartre 	int rv;
28822f5224aeSachartre 	size_t nbytes;
28832f5224aeSachartre 	vd_t *vd = task->vd;
28842f5224aeSachartre 	vd_dring_payload_t *request = task->request;
28852f5224aeSachartre 	vd_capacity_t vd_cap = { 0 };
28862f5224aeSachartre 
28872f5224aeSachartre 	ASSERT(request->operation == VD_OP_GET_CAPACITY);
28882f5224aeSachartre 	ASSERT(vd->scsi);
28892f5224aeSachartre 
28902f5224aeSachartre 	PR0("Performing VD_OP_GET_CAPACITY");
28912f5224aeSachartre 
28922f5224aeSachartre 	nbytes = request->nbytes;
28932f5224aeSachartre 
28942f5224aeSachartre 	if (nbytes != RNDSIZE(vd_capacity_t)) {
28952f5224aeSachartre 		PR0("VD_OP_GET_CAPACITY:  Expected nbytes = %lu, got %lu",
28962f5224aeSachartre 		    RNDSIZE(vd_capacity_t), nbytes);
28972f5224aeSachartre 		return (EINVAL);
28982f5224aeSachartre 	}
28992f5224aeSachartre 
29002f5224aeSachartre 	if (vd->vdisk_size == VD_SIZE_UNKNOWN) {
29012f5224aeSachartre 		if (vd_setup_mediainfo(vd) != 0)
29022f5224aeSachartre 			ASSERT(vd->vdisk_size == VD_SIZE_UNKNOWN);
29032f5224aeSachartre 	}
29042f5224aeSachartre 
29052f5224aeSachartre 	ASSERT(vd->vdisk_size != 0);
29062f5224aeSachartre 
29072f5224aeSachartre 	request->status = 0;
29082f5224aeSachartre 
29092f5224aeSachartre 	vd_cap.vdisk_block_size = vd->vdisk_block_size;
29102f5224aeSachartre 	vd_cap.vdisk_size = vd->vdisk_size;
29112f5224aeSachartre 
29122f5224aeSachartre 	if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&vd_cap, 0, &nbytes,
29132f5224aeSachartre 	    request->cookie, request->ncookies, LDC_COPY_OUT)) != 0) {
29142f5224aeSachartre 		PR0("ldc_mem_copy() returned errno %d copying to client", rv);
29152f5224aeSachartre 		return (rv);
29162f5224aeSachartre 	}
29172f5224aeSachartre 
29182f5224aeSachartre 	return (0);
29192f5224aeSachartre }
29202f5224aeSachartre 
29212f5224aeSachartre static int
29222f5224aeSachartre vd_get_access(vd_task_t *task)
29232f5224aeSachartre {
29242f5224aeSachartre 	uint64_t access;
29252f5224aeSachartre 	int rv, rval = 0;
29262f5224aeSachartre 	size_t nbytes;
29272f5224aeSachartre 	vd_t *vd = task->vd;
29282f5224aeSachartre 	vd_dring_payload_t *request = task->request;
29292f5224aeSachartre 
29302f5224aeSachartre 	ASSERT(request->operation == VD_OP_GET_ACCESS);
29312f5224aeSachartre 	ASSERT(vd->scsi);
29322f5224aeSachartre 
29332f5224aeSachartre 	PR0("Performing VD_OP_GET_ACCESS");
29342f5224aeSachartre 
29352f5224aeSachartre 	nbytes = request->nbytes;
29362f5224aeSachartre 
29372f5224aeSachartre 	if (nbytes != sizeof (uint64_t)) {
29382f5224aeSachartre 		PR0("VD_OP_GET_ACCESS:  Expected nbytes = %lu, got %lu",
29392f5224aeSachartre 		    sizeof (uint64_t), nbytes);
29402f5224aeSachartre 		return (EINVAL);
29412f5224aeSachartre 	}
29422f5224aeSachartre 
29432f5224aeSachartre 	request->status = ldi_ioctl(vd->ldi_handle[request->slice], MHIOCSTATUS,
29442f5224aeSachartre 	    NULL, (vd->open_flags | FKIOCTL), kcred, &rval);
29452f5224aeSachartre 
29462f5224aeSachartre 	if (request->status != 0)
29472f5224aeSachartre 		return (0);
29482f5224aeSachartre 
29492f5224aeSachartre 	access = (rval == 0)? VD_ACCESS_ALLOWED : VD_ACCESS_DENIED;
29502f5224aeSachartre 
29512f5224aeSachartre 	if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&access, 0, &nbytes,
29522f5224aeSachartre 	    request->cookie, request->ncookies, LDC_COPY_OUT)) != 0) {
29532f5224aeSachartre 		PR0("ldc_mem_copy() returned errno %d copying to client", rv);
29542f5224aeSachartre 		return (rv);
29552f5224aeSachartre 	}
29562f5224aeSachartre 
29572f5224aeSachartre 	return (0);
29582f5224aeSachartre }
29592f5224aeSachartre 
29602f5224aeSachartre static int
29612f5224aeSachartre vd_set_access(vd_task_t *task)
29622f5224aeSachartre {
29632f5224aeSachartre 	uint64_t flags;
29642f5224aeSachartre 	int rv, rval;
29652f5224aeSachartre 	size_t nbytes;
29662f5224aeSachartre 	vd_t *vd = task->vd;
29672f5224aeSachartre 	vd_dring_payload_t *request = task->request;
29682f5224aeSachartre 
29692f5224aeSachartre 	ASSERT(request->operation == VD_OP_SET_ACCESS);
29702f5224aeSachartre 	ASSERT(vd->scsi);
29712f5224aeSachartre 
29722f5224aeSachartre 	nbytes = request->nbytes;
29732f5224aeSachartre 
29742f5224aeSachartre 	if (nbytes != sizeof (uint64_t)) {
29752f5224aeSachartre 		PR0("VD_OP_SET_ACCESS:  Expected nbytes = %lu, got %lu",
29762f5224aeSachartre 		    sizeof (uint64_t), nbytes);
29772f5224aeSachartre 		return (EINVAL);
29782f5224aeSachartre 	}
29792f5224aeSachartre 
29802f5224aeSachartre 	if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&flags, 0, &nbytes,
29812f5224aeSachartre 	    request->cookie, request->ncookies, LDC_COPY_IN)) != 0) {
29822f5224aeSachartre 		PR0("ldc_mem_copy() returned errno %d copying from client", rv);
29832f5224aeSachartre 		return (rv);
29842f5224aeSachartre 	}
29852f5224aeSachartre 
29862f5224aeSachartre 	if (flags == VD_ACCESS_SET_CLEAR) {
29872f5224aeSachartre 		PR0("Performing VD_OP_SET_ACCESS (CLEAR)");
29882f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
29892f5224aeSachartre 		    MHIOCRELEASE, NULL, (vd->open_flags | FKIOCTL), kcred,
29902f5224aeSachartre 		    &rval);
29912f5224aeSachartre 		if (request->status == 0)
29922f5224aeSachartre 			vd->ownership = B_FALSE;
29932f5224aeSachartre 		return (0);
29942f5224aeSachartre 	}
29952f5224aeSachartre 
29962f5224aeSachartre 	/*
29972f5224aeSachartre 	 * As per the VIO spec, the PREEMPT and PRESERVE flags are only valid
29982f5224aeSachartre 	 * when the EXCLUSIVE flag is set.
29992f5224aeSachartre 	 */
30002f5224aeSachartre 	if (!(flags & VD_ACCESS_SET_EXCLUSIVE)) {
30012f5224aeSachartre 		PR0("Invalid VD_OP_SET_ACCESS flags: 0x%lx", flags);
30022f5224aeSachartre 		request->status = EINVAL;
30032f5224aeSachartre 		return (0);
30042f5224aeSachartre 	}
30052f5224aeSachartre 
30062f5224aeSachartre 	switch (flags & (VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE)) {
30072f5224aeSachartre 
30082f5224aeSachartre 	case VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE:
30092f5224aeSachartre 		/*
30102f5224aeSachartre 		 * Flags EXCLUSIVE and PREEMPT and PRESERVE. We have to
30112f5224aeSachartre 		 * acquire exclusive access rights, preserve them and we
30122f5224aeSachartre 		 * can use preemption. So we can use the MHIOCTKNOWN ioctl.
30132f5224aeSachartre 		 */
30142f5224aeSachartre 		PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT|PRESERVE)");
30152f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
30162f5224aeSachartre 		    MHIOCTKOWN, NULL, (vd->open_flags | FKIOCTL), kcred, &rval);
30172f5224aeSachartre 		break;
30182f5224aeSachartre 
30192f5224aeSachartre 	case VD_ACCESS_SET_PRESERVE:
30202f5224aeSachartre 		/*
30212f5224aeSachartre 		 * Flags EXCLUSIVE and PRESERVE. We have to acquire exclusive
30222f5224aeSachartre 		 * access rights and preserve them, but not preempt any other
30232f5224aeSachartre 		 * host. So we need to use the MHIOCTKOWN ioctl to enable the
30242f5224aeSachartre 		 * "preserve" feature but we can not called it directly
30252f5224aeSachartre 		 * because it uses preemption. So before that, we use the
30262f5224aeSachartre 		 * MHIOCQRESERVE ioctl to ensure we can get exclusive rights
30272f5224aeSachartre 		 * without preempting anyone.
30282f5224aeSachartre 		 */
30292f5224aeSachartre 		PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PRESERVE)");
30302f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
30312f5224aeSachartre 		    MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
30322f5224aeSachartre 		    &rval);
30332f5224aeSachartre 		if (request->status != 0)
30342f5224aeSachartre 			break;
30352f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
30362f5224aeSachartre 		    MHIOCTKOWN, NULL, (vd->open_flags | FKIOCTL), kcred, &rval);
30372f5224aeSachartre 		break;
30382f5224aeSachartre 
30392f5224aeSachartre 	case VD_ACCESS_SET_PREEMPT:
30402f5224aeSachartre 		/*
30412f5224aeSachartre 		 * Flags EXCLUSIVE and PREEMPT. We have to acquire exclusive
30422f5224aeSachartre 		 * access rights and we can use preemption. So we try to do
30432f5224aeSachartre 		 * a SCSI reservation, if it fails we reset the disk to clear
30442f5224aeSachartre 		 * any reservation and we try to reserve again.
30452f5224aeSachartre 		 */
30462f5224aeSachartre 		PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT)");
30472f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
30482f5224aeSachartre 		    MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
30492f5224aeSachartre 		    &rval);
30502f5224aeSachartre 		if (request->status == 0)
30512f5224aeSachartre 			break;
30522f5224aeSachartre 
30532f5224aeSachartre 		/* reset the disk */
30542f5224aeSachartre 		(void) vd_scsi_reset(vd);
30552f5224aeSachartre 
30562f5224aeSachartre 		/* try again even if the reset has failed */
30572f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
30582f5224aeSachartre 		    MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
30592f5224aeSachartre 		    &rval);
30602f5224aeSachartre 		break;
30612f5224aeSachartre 
30622f5224aeSachartre 	case 0:
30632f5224aeSachartre 		/* Flag EXCLUSIVE only. Just issue a SCSI reservation */
30642f5224aeSachartre 		PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE)");
30652f5224aeSachartre 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
30662f5224aeSachartre 		    MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
30672f5224aeSachartre 		    &rval);
30682f5224aeSachartre 		break;
30692f5224aeSachartre 	}
30702f5224aeSachartre 
30712f5224aeSachartre 	if (request->status == 0)
30722f5224aeSachartre 		vd->ownership = B_TRUE;
30732f5224aeSachartre 	else
30742f5224aeSachartre 		PR0("VD_OP_SET_ACCESS: error %d", request->status);
30752f5224aeSachartre 
30762f5224aeSachartre 	return (0);
30772f5224aeSachartre }
30782f5224aeSachartre 
30792f5224aeSachartre static void
30802f5224aeSachartre vd_reset_access(vd_t *vd)
30812f5224aeSachartre {
30822f5224aeSachartre 	int status, rval;
30832f5224aeSachartre 
30842f5224aeSachartre 	if (vd->file || !vd->ownership)
30852f5224aeSachartre 		return;
30862f5224aeSachartre 
30872f5224aeSachartre 	PR0("Releasing disk ownership");
30882f5224aeSachartre 	status = ldi_ioctl(vd->ldi_handle[0], MHIOCRELEASE, NULL,
30892f5224aeSachartre 	    (vd->open_flags | FKIOCTL), kcred, &rval);
30902f5224aeSachartre 
30912f5224aeSachartre 	/*
30922f5224aeSachartre 	 * An EACCES failure means that there is a reservation conflict,
30932f5224aeSachartre 	 * so we are not the owner of the disk anymore.
30942f5224aeSachartre 	 */
30952f5224aeSachartre 	if (status == 0 || status == EACCES) {
30962f5224aeSachartre 		vd->ownership = B_FALSE;
30972f5224aeSachartre 		return;
30982f5224aeSachartre 	}
30992f5224aeSachartre 
31002f5224aeSachartre 	PR0("Fail to release ownership, error %d", status);
31012f5224aeSachartre 
31022f5224aeSachartre 	/*
31032f5224aeSachartre 	 * We have failed to release the ownership, try to reset the disk
31042f5224aeSachartre 	 * to release reservations.
31052f5224aeSachartre 	 */
31062f5224aeSachartre 	PR0("Resetting disk");
31072f5224aeSachartre 	status = vd_scsi_reset(vd);
31082f5224aeSachartre 
31092f5224aeSachartre 	if (status != 0)
31102f5224aeSachartre 		PR0("Fail to reset disk, error %d", status);
31112f5224aeSachartre 
31122f5224aeSachartre 	/* whatever the result of the reset is, we try the release again */
31132f5224aeSachartre 	status = ldi_ioctl(vd->ldi_handle[0], MHIOCRELEASE, NULL,
31142f5224aeSachartre 	    (vd->open_flags | FKIOCTL), kcred, &rval);
31152f5224aeSachartre 
31162f5224aeSachartre 	if (status == 0 || status == EACCES) {
31172f5224aeSachartre 		vd->ownership = B_FALSE;
31182f5224aeSachartre 		return;
31192f5224aeSachartre 	}
31202f5224aeSachartre 
31212f5224aeSachartre 	PR0("Fail to release ownership, error %d", status);
31222f5224aeSachartre 
31232f5224aeSachartre 	/*
31242f5224aeSachartre 	 * At this point we have done our best to try to reset the
31252f5224aeSachartre 	 * access rights to the disk and we don't know if we still
31262f5224aeSachartre 	 * own a reservation and if any mechanism to preserve the
31272f5224aeSachartre 	 * ownership is still in place. The ultimate solution would
31282f5224aeSachartre 	 * be to reset the system but this is usually not what we
31292f5224aeSachartre 	 * want to happen.
31302f5224aeSachartre 	 */
31312f5224aeSachartre 
31322f5224aeSachartre 	if (vd_reset_access_failure == A_REBOOT) {
31332f5224aeSachartre 		cmn_err(CE_WARN, VD_RESET_ACCESS_FAILURE_MSG
31342f5224aeSachartre 		    ", rebooting the system", vd->device_path);
31352f5224aeSachartre 		(void) uadmin(A_SHUTDOWN, AD_BOOT, NULL);
31362f5224aeSachartre 	} else if (vd_reset_access_failure == A_DUMP) {
31372f5224aeSachartre 		panic(VD_RESET_ACCESS_FAILURE_MSG, vd->device_path);
31382f5224aeSachartre 	}
31392f5224aeSachartre 
31402f5224aeSachartre 	cmn_err(CE_WARN, VD_RESET_ACCESS_FAILURE_MSG, vd->device_path);
31412f5224aeSachartre }
31422f5224aeSachartre 
31431ae08745Sheppo /*
31441ae08745Sheppo  * Define the supported operations once the functions for performing them have
31451ae08745Sheppo  * been defined
31461ae08745Sheppo  */
31471ae08745Sheppo static const vds_operation_t	vds_operation[] = {
31483af08d82Slm66018 #define	X(_s)	#_s, _s
31493af08d82Slm66018 	{X(VD_OP_BREAD),	vd_start_bio,	vd_complete_bio},
31503af08d82Slm66018 	{X(VD_OP_BWRITE),	vd_start_bio,	vd_complete_bio},
31513af08d82Slm66018 	{X(VD_OP_FLUSH),	vd_ioctl,	NULL},
31523af08d82Slm66018 	{X(VD_OP_GET_WCE),	vd_ioctl,	NULL},
31533af08d82Slm66018 	{X(VD_OP_SET_WCE),	vd_ioctl,	NULL},
31543af08d82Slm66018 	{X(VD_OP_GET_VTOC),	vd_ioctl,	NULL},
31553af08d82Slm66018 	{X(VD_OP_SET_VTOC),	vd_ioctl,	NULL},
31563af08d82Slm66018 	{X(VD_OP_GET_DISKGEOM),	vd_ioctl,	NULL},
31573af08d82Slm66018 	{X(VD_OP_SET_DISKGEOM),	vd_ioctl,	NULL},
31583af08d82Slm66018 	{X(VD_OP_GET_EFI),	vd_ioctl,	NULL},
31593af08d82Slm66018 	{X(VD_OP_SET_EFI),	vd_ioctl,	NULL},
31603af08d82Slm66018 	{X(VD_OP_GET_DEVID),	vd_get_devid,	NULL},
31612f5224aeSachartre 	{X(VD_OP_SCSICMD),	vd_ioctl,	NULL},
31622f5224aeSachartre 	{X(VD_OP_RESET),	vd_reset,	NULL},
31632f5224aeSachartre 	{X(VD_OP_GET_CAPACITY),	vd_get_capacity, NULL},
31642f5224aeSachartre 	{X(VD_OP_SET_ACCESS),	vd_set_access,	NULL},
31652f5224aeSachartre 	{X(VD_OP_GET_ACCESS),	vd_get_access,	NULL},
31663af08d82Slm66018 #undef	X
31671ae08745Sheppo };
31681ae08745Sheppo 
31691ae08745Sheppo static const size_t	vds_noperations =
31701ae08745Sheppo 	(sizeof (vds_operation))/(sizeof (vds_operation[0]));
31711ae08745Sheppo 
31721ae08745Sheppo /*
3173d10e4ef2Snarayan  * Process a task specifying a client I/O request
3174205eeb1aSlm66018  *
3175205eeb1aSlm66018  * Parameters:
3176205eeb1aSlm66018  *	task 		- structure containing the request sent from client
3177205eeb1aSlm66018  *
3178205eeb1aSlm66018  * Return Value
3179205eeb1aSlm66018  *	0	- success
3180205eeb1aSlm66018  *	ENOTSUP	- Unknown/Unsupported VD_OP_XXX operation
3181205eeb1aSlm66018  *	EINVAL	- Invalid disk slice
3182205eeb1aSlm66018  *	!= 0	- some other non-zero return value from start function
31831ae08745Sheppo  */
31841ae08745Sheppo static int
3185205eeb1aSlm66018 vd_do_process_task(vd_task_t *task)
31861ae08745Sheppo {
3187205eeb1aSlm66018 	int			i;
3188d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
3189d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
31901ae08745Sheppo 
3191d10e4ef2Snarayan 	ASSERT(vd != NULL);
3192d10e4ef2Snarayan 	ASSERT(request != NULL);
31931ae08745Sheppo 
3194d10e4ef2Snarayan 	/* Find the requested operation */
3195205eeb1aSlm66018 	for (i = 0; i < vds_noperations; i++) {
3196205eeb1aSlm66018 		if (request->operation == vds_operation[i].operation) {
3197205eeb1aSlm66018 			/* all operations should have a start func */
3198205eeb1aSlm66018 			ASSERT(vds_operation[i].start != NULL);
3199205eeb1aSlm66018 
3200205eeb1aSlm66018 			task->completef = vds_operation[i].complete;
3201d10e4ef2Snarayan 			break;
3202205eeb1aSlm66018 		}
3203205eeb1aSlm66018 	}
320417cadca8Slm66018 
320517cadca8Slm66018 	/*
320617cadca8Slm66018 	 * We need to check that the requested operation is permitted
320717cadca8Slm66018 	 * for the particular client that sent it or that the loop above
320817cadca8Slm66018 	 * did not complete without finding the operation type (indicating
320917cadca8Slm66018 	 * that the requested operation is unknown/unimplemented)
321017cadca8Slm66018 	 */
321117cadca8Slm66018 	if ((VD_OP_SUPPORTED(vd->operations, request->operation) == B_FALSE) ||
321217cadca8Slm66018 	    (i == vds_noperations)) {
32133af08d82Slm66018 		PR0("Unsupported operation %u", request->operation);
321417cadca8Slm66018 		request->status = ENOTSUP;
321517cadca8Slm66018 		return (0);
32161ae08745Sheppo 	}
32171ae08745Sheppo 
32187636cb21Slm66018 	/* Range-check slice */
321987a7269eSachartre 	if (request->slice >= vd->nslices &&
322087a7269eSachartre 	    (vd->vdisk_type != VD_DISK_TYPE_DISK ||
322187a7269eSachartre 	    request->slice != VD_SLICE_NONE)) {
32223af08d82Slm66018 		PR0("Invalid \"slice\" %u (max %u) for virtual disk",
32237636cb21Slm66018 		    request->slice, (vd->nslices - 1));
32247636cb21Slm66018 		return (EINVAL);
32257636cb21Slm66018 	}
32267636cb21Slm66018 
3227205eeb1aSlm66018 	/*
3228205eeb1aSlm66018 	 * Call the function pointer that starts the operation.
3229205eeb1aSlm66018 	 */
3230205eeb1aSlm66018 	return (vds_operation[i].start(task));
32311ae08745Sheppo }
32321ae08745Sheppo 
3233205eeb1aSlm66018 /*
3234205eeb1aSlm66018  * Description:
3235205eeb1aSlm66018  *	This function is called by both the in-band and descriptor ring
3236205eeb1aSlm66018  *	message processing functions paths to actually execute the task
3237205eeb1aSlm66018  *	requested by the vDisk client. It in turn calls its worker
3238205eeb1aSlm66018  *	function, vd_do_process_task(), to carry our the request.
3239205eeb1aSlm66018  *
3240205eeb1aSlm66018  *	Any transport errors (e.g. LDC errors, vDisk protocol errors) are
3241205eeb1aSlm66018  *	saved in the 'status' field of the task and are propagated back
3242205eeb1aSlm66018  *	up the call stack to trigger a NACK
3243205eeb1aSlm66018  *
3244205eeb1aSlm66018  *	Any request errors (e.g. ENOTTY from an ioctl) are saved in
3245205eeb1aSlm66018  *	the 'status' field of the request and result in an ACK being sent
3246205eeb1aSlm66018  *	by the completion handler.
3247205eeb1aSlm66018  *
3248205eeb1aSlm66018  * Parameters:
3249205eeb1aSlm66018  *	task 		- structure containing the request sent from client
3250205eeb1aSlm66018  *
3251205eeb1aSlm66018  * Return Value
3252205eeb1aSlm66018  *	0		- successful synchronous request.
3253205eeb1aSlm66018  *	!= 0		- transport error (e.g. LDC errors, vDisk protocol)
3254205eeb1aSlm66018  *	EINPROGRESS	- task will be finished in a completion handler
3255205eeb1aSlm66018  */
3256205eeb1aSlm66018 static int
3257205eeb1aSlm66018 vd_process_task(vd_task_t *task)
3258205eeb1aSlm66018 {
3259205eeb1aSlm66018 	vd_t	*vd = task->vd;
3260205eeb1aSlm66018 	int	status;
32611ae08745Sheppo 
3262205eeb1aSlm66018 	DTRACE_PROBE1(task__start, vd_task_t *, task);
32633af08d82Slm66018 
3264205eeb1aSlm66018 	task->status =  vd_do_process_task(task);
3265205eeb1aSlm66018 
3266205eeb1aSlm66018 	/*
3267205eeb1aSlm66018 	 * If the task processing function returned EINPROGRESS indicating
3268205eeb1aSlm66018 	 * that the task needs completing then schedule a taskq entry to
3269205eeb1aSlm66018 	 * finish it now.
3270205eeb1aSlm66018 	 *
3271205eeb1aSlm66018 	 * Otherwise the task processing function returned either zero
3272205eeb1aSlm66018 	 * indicating that the task was finished in the start function (and we
3273205eeb1aSlm66018 	 * don't need to wait in a completion function) or the start function
3274205eeb1aSlm66018 	 * returned an error - in both cases all that needs to happen is the
3275205eeb1aSlm66018 	 * notification to the vDisk client higher up the call stack.
3276205eeb1aSlm66018 	 * If the task was using a Descriptor Ring, we need to mark it as done
3277205eeb1aSlm66018 	 * at this stage.
3278205eeb1aSlm66018 	 */
3279205eeb1aSlm66018 	if (task->status == EINPROGRESS) {
3280d10e4ef2Snarayan 		/* Queue a task to complete the operation */
3281205eeb1aSlm66018 		(void) ddi_taskq_dispatch(vd->completionq, vd_complete,
3282d10e4ef2Snarayan 		    task, DDI_SLEEP);
3283d10e4ef2Snarayan 
3284f0ca1d9aSsb155480 	} else if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE_V1_0)) {
3285205eeb1aSlm66018 		/* Update the dring element if it's a dring client */
3286205eeb1aSlm66018 		status = vd_mark_elem_done(vd, task->index,
3287205eeb1aSlm66018 		    task->request->status, task->request->nbytes);
3288205eeb1aSlm66018 		if (status == ECONNRESET)
3289205eeb1aSlm66018 			vd_mark_in_reset(vd);
3290205eeb1aSlm66018 	}
3291205eeb1aSlm66018 
3292205eeb1aSlm66018 	return (task->status);
32931ae08745Sheppo }
32941ae08745Sheppo 
32951ae08745Sheppo /*
32960a55fbb7Slm66018  * Return true if the "type", "subtype", and "env" fields of the "tag" first
32970a55fbb7Slm66018  * argument match the corresponding remaining arguments; otherwise, return false
32981ae08745Sheppo  */
32990a55fbb7Slm66018 boolean_t
33001ae08745Sheppo vd_msgtype(vio_msg_tag_t *tag, int type, int subtype, int env)
33011ae08745Sheppo {
33021ae08745Sheppo 	return ((tag->vio_msgtype == type) &&
33031ae08745Sheppo 	    (tag->vio_subtype == subtype) &&
33040a55fbb7Slm66018 	    (tag->vio_subtype_env == env)) ? B_TRUE : B_FALSE;
33051ae08745Sheppo }
33061ae08745Sheppo 
33070a55fbb7Slm66018 /*
33080a55fbb7Slm66018  * Check whether the major/minor version specified in "ver_msg" is supported
33090a55fbb7Slm66018  * by this server.
33100a55fbb7Slm66018  */
33110a55fbb7Slm66018 static boolean_t
33120a55fbb7Slm66018 vds_supported_version(vio_ver_msg_t *ver_msg)
33130a55fbb7Slm66018 {
33140a55fbb7Slm66018 	for (int i = 0; i < vds_num_versions; i++) {
33150a55fbb7Slm66018 		ASSERT(vds_version[i].major > 0);
33160a55fbb7Slm66018 		ASSERT((i == 0) ||
33170a55fbb7Slm66018 		    (vds_version[i].major < vds_version[i-1].major));
33180a55fbb7Slm66018 
33190a55fbb7Slm66018 		/*
33200a55fbb7Slm66018 		 * If the major versions match, adjust the minor version, if
33210a55fbb7Slm66018 		 * necessary, down to the highest value supported by this
33220a55fbb7Slm66018 		 * server and return true so this message will get "ack"ed;
33230a55fbb7Slm66018 		 * the client should also support all minor versions lower
33240a55fbb7Slm66018 		 * than the value it sent
33250a55fbb7Slm66018 		 */
33260a55fbb7Slm66018 		if (ver_msg->ver_major == vds_version[i].major) {
33270a55fbb7Slm66018 			if (ver_msg->ver_minor > vds_version[i].minor) {
33280a55fbb7Slm66018 				PR0("Adjusting minor version from %u to %u",
33290a55fbb7Slm66018 				    ver_msg->ver_minor, vds_version[i].minor);
33300a55fbb7Slm66018 				ver_msg->ver_minor = vds_version[i].minor;
33310a55fbb7Slm66018 			}
33320a55fbb7Slm66018 			return (B_TRUE);
33330a55fbb7Slm66018 		}
33340a55fbb7Slm66018 
33350a55fbb7Slm66018 		/*
33360a55fbb7Slm66018 		 * If the message contains a higher major version number, set
33370a55fbb7Slm66018 		 * the message's major/minor versions to the current values
33380a55fbb7Slm66018 		 * and return false, so this message will get "nack"ed with
33390a55fbb7Slm66018 		 * these values, and the client will potentially try again
33400a55fbb7Slm66018 		 * with the same or a lower version
33410a55fbb7Slm66018 		 */
33420a55fbb7Slm66018 		if (ver_msg->ver_major > vds_version[i].major) {
33430a55fbb7Slm66018 			ver_msg->ver_major = vds_version[i].major;
33440a55fbb7Slm66018 			ver_msg->ver_minor = vds_version[i].minor;
33450a55fbb7Slm66018 			return (B_FALSE);
33460a55fbb7Slm66018 		}
33470a55fbb7Slm66018 
33480a55fbb7Slm66018 		/*
33490a55fbb7Slm66018 		 * Otherwise, the message's major version is less than the
33500a55fbb7Slm66018 		 * current major version, so continue the loop to the next
33510a55fbb7Slm66018 		 * (lower) supported version
33520a55fbb7Slm66018 		 */
33530a55fbb7Slm66018 	}
33540a55fbb7Slm66018 
33550a55fbb7Slm66018 	/*
33560a55fbb7Slm66018 	 * No common version was found; "ground" the version pair in the
33570a55fbb7Slm66018 	 * message to terminate negotiation
33580a55fbb7Slm66018 	 */
33590a55fbb7Slm66018 	ver_msg->ver_major = 0;
33600a55fbb7Slm66018 	ver_msg->ver_minor = 0;
33610a55fbb7Slm66018 	return (B_FALSE);
33620a55fbb7Slm66018 }
33630a55fbb7Slm66018 
33640a55fbb7Slm66018 /*
33650a55fbb7Slm66018  * Process a version message from a client.  vds expects to receive version
33660a55fbb7Slm66018  * messages from clients seeking service, but never issues version messages
33670a55fbb7Slm66018  * itself; therefore, vds can ACK or NACK client version messages, but does
33680a55fbb7Slm66018  * not expect to receive version-message ACKs or NACKs (and will treat such
33690a55fbb7Slm66018  * messages as invalid).
33700a55fbb7Slm66018  */
33711ae08745Sheppo static int
33720a55fbb7Slm66018 vd_process_ver_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
33731ae08745Sheppo {
33741ae08745Sheppo 	vio_ver_msg_t	*ver_msg = (vio_ver_msg_t *)msg;
33751ae08745Sheppo 
33761ae08745Sheppo 
33771ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
33781ae08745Sheppo 
33791ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
33801ae08745Sheppo 	    VIO_VER_INFO)) {
33811ae08745Sheppo 		return (ENOMSG);	/* not a version message */
33821ae08745Sheppo 	}
33831ae08745Sheppo 
33841ae08745Sheppo 	if (msglen != sizeof (*ver_msg)) {
33853af08d82Slm66018 		PR0("Expected %lu-byte version message; "
33861ae08745Sheppo 		    "received %lu bytes", sizeof (*ver_msg), msglen);
33871ae08745Sheppo 		return (EBADMSG);
33881ae08745Sheppo 	}
33891ae08745Sheppo 
33901ae08745Sheppo 	if (ver_msg->dev_class != VDEV_DISK) {
33913af08d82Slm66018 		PR0("Expected device class %u (disk); received %u",
33921ae08745Sheppo 		    VDEV_DISK, ver_msg->dev_class);
33931ae08745Sheppo 		return (EBADMSG);
33941ae08745Sheppo 	}
33951ae08745Sheppo 
33960a55fbb7Slm66018 	/*
33970a55fbb7Slm66018 	 * We're talking to the expected kind of client; set our device class
33980a55fbb7Slm66018 	 * for "ack/nack" back to the client
33990a55fbb7Slm66018 	 */
34001ae08745Sheppo 	ver_msg->dev_class = VDEV_DISK_SERVER;
34010a55fbb7Slm66018 
34020a55fbb7Slm66018 	/*
34030a55fbb7Slm66018 	 * Check whether the (valid) version message specifies a version
34040a55fbb7Slm66018 	 * supported by this server.  If the version is not supported, return
34050a55fbb7Slm66018 	 * EBADMSG so the message will get "nack"ed; vds_supported_version()
34060a55fbb7Slm66018 	 * will have updated the message with a supported version for the
34070a55fbb7Slm66018 	 * client to consider
34080a55fbb7Slm66018 	 */
34090a55fbb7Slm66018 	if (!vds_supported_version(ver_msg))
34100a55fbb7Slm66018 		return (EBADMSG);
34110a55fbb7Slm66018 
34120a55fbb7Slm66018 
34130a55fbb7Slm66018 	/*
34140a55fbb7Slm66018 	 * A version has been agreed upon; use the client's SID for
34150a55fbb7Slm66018 	 * communication on this channel now
34160a55fbb7Slm66018 	 */
34170a55fbb7Slm66018 	ASSERT(!(vd->initialized & VD_SID));
34180a55fbb7Slm66018 	vd->sid = ver_msg->tag.vio_sid;
34190a55fbb7Slm66018 	vd->initialized |= VD_SID;
34200a55fbb7Slm66018 
34210a55fbb7Slm66018 	/*
342217cadca8Slm66018 	 * Store the negotiated major and minor version values in the "vd" data
342317cadca8Slm66018 	 * structure so that we can check if certain operations are supported
342417cadca8Slm66018 	 * by the client.
34250a55fbb7Slm66018 	 */
342617cadca8Slm66018 	vd->version.major = ver_msg->ver_major;
342717cadca8Slm66018 	vd->version.minor = ver_msg->ver_minor;
34280a55fbb7Slm66018 
34290a55fbb7Slm66018 	PR0("Using major version %u, minor version %u",
34300a55fbb7Slm66018 	    ver_msg->ver_major, ver_msg->ver_minor);
34311ae08745Sheppo 	return (0);
34321ae08745Sheppo }
34331ae08745Sheppo 
343417cadca8Slm66018 static void
343517cadca8Slm66018 vd_set_exported_operations(vd_t *vd)
343617cadca8Slm66018 {
343717cadca8Slm66018 	vd->operations = 0;	/* clear field */
343817cadca8Slm66018 
343917cadca8Slm66018 	/*
344017cadca8Slm66018 	 * We need to check from the highest version supported to the
344117cadca8Slm66018 	 * lowest because versions with a higher minor number implicitly
344217cadca8Slm66018 	 * support versions with a lower minor number.
344317cadca8Slm66018 	 */
344417cadca8Slm66018 	if (vio_ver_is_supported(vd->version, 1, 1)) {
344517cadca8Slm66018 		ASSERT(vd->open_flags & FREAD);
344617cadca8Slm66018 		vd->operations |= VD_OP_MASK_READ;
344717cadca8Slm66018 
344817cadca8Slm66018 		if (vd->open_flags & FWRITE)
344917cadca8Slm66018 			vd->operations |= VD_OP_MASK_WRITE;
345017cadca8Slm66018 
34512f5224aeSachartre 		if (vd->scsi)
34522f5224aeSachartre 			vd->operations |= VD_OP_MASK_SCSI;
34532f5224aeSachartre 
345417cadca8Slm66018 		if (vd->file && vd_file_is_iso_image(vd)) {
345517cadca8Slm66018 			/*
345617cadca8Slm66018 			 * can't write to ISO images, make sure that write
345717cadca8Slm66018 			 * support is not set in case administrator did not
345817cadca8Slm66018 			 * use "options=ro" when doing an ldm add-vdsdev
345917cadca8Slm66018 			 */
346017cadca8Slm66018 			vd->operations &= ~VD_OP_MASK_WRITE;
346117cadca8Slm66018 		}
346217cadca8Slm66018 	} else if (vio_ver_is_supported(vd->version, 1, 0)) {
346317cadca8Slm66018 		vd->operations = VD_OP_MASK_READ | VD_OP_MASK_WRITE;
346417cadca8Slm66018 	}
346517cadca8Slm66018 
346617cadca8Slm66018 	/* we should have already agreed on a version */
346717cadca8Slm66018 	ASSERT(vd->operations != 0);
346817cadca8Slm66018 }
346917cadca8Slm66018 
34701ae08745Sheppo static int
34711ae08745Sheppo vd_process_attr_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
34721ae08745Sheppo {
34731ae08745Sheppo 	vd_attr_msg_t	*attr_msg = (vd_attr_msg_t *)msg;
34743c96341aSnarayan 	int		status, retry = 0;
34751ae08745Sheppo 
34761ae08745Sheppo 
34771ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
34781ae08745Sheppo 
34791ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
34801ae08745Sheppo 	    VIO_ATTR_INFO)) {
3481d10e4ef2Snarayan 		PR0("Message is not an attribute message");
3482d10e4ef2Snarayan 		return (ENOMSG);
34831ae08745Sheppo 	}
34841ae08745Sheppo 
34851ae08745Sheppo 	if (msglen != sizeof (*attr_msg)) {
34863af08d82Slm66018 		PR0("Expected %lu-byte attribute message; "
34871ae08745Sheppo 		    "received %lu bytes", sizeof (*attr_msg), msglen);
34881ae08745Sheppo 		return (EBADMSG);
34891ae08745Sheppo 	}
34901ae08745Sheppo 
34911ae08745Sheppo 	if (attr_msg->max_xfer_sz == 0) {
34923af08d82Slm66018 		PR0("Received maximum transfer size of 0 from client");
34931ae08745Sheppo 		return (EBADMSG);
34941ae08745Sheppo 	}
34951ae08745Sheppo 
34961ae08745Sheppo 	if ((attr_msg->xfer_mode != VIO_DESC_MODE) &&
3497f0ca1d9aSsb155480 	    (attr_msg->xfer_mode != VIO_DRING_MODE_V1_0)) {
34983af08d82Slm66018 		PR0("Client requested unsupported transfer mode");
34991ae08745Sheppo 		return (EBADMSG);
35001ae08745Sheppo 	}
35011ae08745Sheppo 
35023c96341aSnarayan 	/*
35033c96341aSnarayan 	 * check if the underlying disk is ready, if not try accessing
35043c96341aSnarayan 	 * the device again. Open the vdisk device and extract info
35053c96341aSnarayan 	 * about it, as this is needed to respond to the attr info msg
35063c96341aSnarayan 	 */
35073c96341aSnarayan 	if ((vd->initialized & VD_DISK_READY) == 0) {
35083c96341aSnarayan 		PR0("Retry setting up disk (%s)", vd->device_path);
35093c96341aSnarayan 		do {
35103c96341aSnarayan 			status = vd_setup_vd(vd);
35113c96341aSnarayan 			if (status != EAGAIN || ++retry > vds_dev_retries)
35123c96341aSnarayan 				break;
35133c96341aSnarayan 
35143c96341aSnarayan 			/* incremental delay */
35153c96341aSnarayan 			delay(drv_usectohz(vds_dev_delay));
35163c96341aSnarayan 
35173c96341aSnarayan 			/* if vdisk is no longer enabled - return error */
35183c96341aSnarayan 			if (!vd_enabled(vd))
35193c96341aSnarayan 				return (ENXIO);
35203c96341aSnarayan 
35213c96341aSnarayan 		} while (status == EAGAIN);
35223c96341aSnarayan 
35233c96341aSnarayan 		if (status)
35243c96341aSnarayan 			return (ENXIO);
35253c96341aSnarayan 
35263c96341aSnarayan 		vd->initialized |= VD_DISK_READY;
35273c96341aSnarayan 		ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
3528*8fce2fd6Sachartre 		PR0("vdisk_type = %s, volume = %s, file = %s, nslices = %u",
35293c96341aSnarayan 		    ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
3530*8fce2fd6Sachartre 		    (vd->volume ? "yes" : "no"),
35313c96341aSnarayan 		    (vd->file ? "yes" : "no"),
35323c96341aSnarayan 		    vd->nslices);
35333c96341aSnarayan 	}
35343c96341aSnarayan 
35351ae08745Sheppo 	/* Success:  valid message and transfer mode */
35361ae08745Sheppo 	vd->xfer_mode = attr_msg->xfer_mode;
35373af08d82Slm66018 
35381ae08745Sheppo 	if (vd->xfer_mode == VIO_DESC_MODE) {
35393af08d82Slm66018 
35401ae08745Sheppo 		/*
35411ae08745Sheppo 		 * The vd_dring_inband_msg_t contains one cookie; need room
35421ae08745Sheppo 		 * for up to n-1 more cookies, where "n" is the number of full
35431ae08745Sheppo 		 * pages plus possibly one partial page required to cover
35441ae08745Sheppo 		 * "max_xfer_sz".  Add room for one more cookie if
35451ae08745Sheppo 		 * "max_xfer_sz" isn't an integral multiple of the page size.
35461ae08745Sheppo 		 * Must first get the maximum transfer size in bytes.
35471ae08745Sheppo 		 */
35481ae08745Sheppo 		size_t	max_xfer_bytes = attr_msg->vdisk_block_size ?
35491ae08745Sheppo 		    attr_msg->vdisk_block_size*attr_msg->max_xfer_sz :
35501ae08745Sheppo 		    attr_msg->max_xfer_sz;
35511ae08745Sheppo 		size_t	max_inband_msglen =
35521ae08745Sheppo 		    sizeof (vd_dring_inband_msg_t) +
35531ae08745Sheppo 		    ((max_xfer_bytes/PAGESIZE +
35541ae08745Sheppo 		    ((max_xfer_bytes % PAGESIZE) ? 1 : 0))*
35551ae08745Sheppo 		    (sizeof (ldc_mem_cookie_t)));
35561ae08745Sheppo 
35571ae08745Sheppo 		/*
35581ae08745Sheppo 		 * Set the maximum expected message length to
35591ae08745Sheppo 		 * accommodate in-band-descriptor messages with all
35601ae08745Sheppo 		 * their cookies
35611ae08745Sheppo 		 */
35621ae08745Sheppo 		vd->max_msglen = MAX(vd->max_msglen, max_inband_msglen);
3563d10e4ef2Snarayan 
3564d10e4ef2Snarayan 		/*
3565d10e4ef2Snarayan 		 * Initialize the data structure for processing in-band I/O
3566d10e4ef2Snarayan 		 * request descriptors
3567d10e4ef2Snarayan 		 */
3568d10e4ef2Snarayan 		vd->inband_task.vd	= vd;
35693af08d82Slm66018 		vd->inband_task.msg	= kmem_alloc(vd->max_msglen, KM_SLEEP);
3570d10e4ef2Snarayan 		vd->inband_task.index	= 0;
3571d10e4ef2Snarayan 		vd->inband_task.type	= VD_FINAL_RANGE_TASK;	/* range == 1 */
35721ae08745Sheppo 	}
35731ae08745Sheppo 
3574e1ebb9ecSlm66018 	/* Return the device's block size and max transfer size to the client */
35752f5224aeSachartre 	attr_msg->vdisk_block_size	= vd->vdisk_block_size;
3576e1ebb9ecSlm66018 	attr_msg->max_xfer_sz		= vd->max_xfer_sz;
3577e1ebb9ecSlm66018 
35781ae08745Sheppo 	attr_msg->vdisk_size = vd->vdisk_size;
35791ae08745Sheppo 	attr_msg->vdisk_type = vd->vdisk_type;
358017cadca8Slm66018 	attr_msg->vdisk_media = vd->vdisk_media;
358117cadca8Slm66018 
358217cadca8Slm66018 	/* Discover and save the list of supported VD_OP_XXX operations */
358317cadca8Slm66018 	vd_set_exported_operations(vd);
358417cadca8Slm66018 	attr_msg->operations = vd->operations;
358517cadca8Slm66018 
35861ae08745Sheppo 	PR0("%s", VD_CLIENT(vd));
35873af08d82Slm66018 
35883af08d82Slm66018 	ASSERT(vd->dring_task == NULL);
35893af08d82Slm66018 
35901ae08745Sheppo 	return (0);
35911ae08745Sheppo }
35921ae08745Sheppo 
35931ae08745Sheppo static int
35941ae08745Sheppo vd_process_dring_reg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
35951ae08745Sheppo {
35961ae08745Sheppo 	int			status;
35971ae08745Sheppo 	size_t			expected;
35981ae08745Sheppo 	ldc_mem_info_t		dring_minfo;
35991ae08745Sheppo 	vio_dring_reg_msg_t	*reg_msg = (vio_dring_reg_msg_t *)msg;
36001ae08745Sheppo 
36011ae08745Sheppo 
36021ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
36031ae08745Sheppo 
36041ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
36051ae08745Sheppo 	    VIO_DRING_REG)) {
3606d10e4ef2Snarayan 		PR0("Message is not a register-dring message");
3607d10e4ef2Snarayan 		return (ENOMSG);
36081ae08745Sheppo 	}
36091ae08745Sheppo 
36101ae08745Sheppo 	if (msglen < sizeof (*reg_msg)) {
36113af08d82Slm66018 		PR0("Expected at least %lu-byte register-dring message; "
36121ae08745Sheppo 		    "received %lu bytes", sizeof (*reg_msg), msglen);
36131ae08745Sheppo 		return (EBADMSG);
36141ae08745Sheppo 	}
36151ae08745Sheppo 
36161ae08745Sheppo 	expected = sizeof (*reg_msg) +
36171ae08745Sheppo 	    (reg_msg->ncookies - 1)*(sizeof (reg_msg->cookie[0]));
36181ae08745Sheppo 	if (msglen != expected) {
36193af08d82Slm66018 		PR0("Expected %lu-byte register-dring message; "
36201ae08745Sheppo 		    "received %lu bytes", expected, msglen);
36211ae08745Sheppo 		return (EBADMSG);
36221ae08745Sheppo 	}
36231ae08745Sheppo 
36241ae08745Sheppo 	if (vd->initialized & VD_DRING) {
36253af08d82Slm66018 		PR0("A dring was previously registered; only support one");
36261ae08745Sheppo 		return (EBADMSG);
36271ae08745Sheppo 	}
36281ae08745Sheppo 
3629d10e4ef2Snarayan 	if (reg_msg->num_descriptors > INT32_MAX) {
36303af08d82Slm66018 		PR0("reg_msg->num_descriptors = %u; must be <= %u (%s)",
3631d10e4ef2Snarayan 		    reg_msg->ncookies, INT32_MAX, STRINGIZE(INT32_MAX));
3632d10e4ef2Snarayan 		return (EBADMSG);
3633d10e4ef2Snarayan 	}
3634d10e4ef2Snarayan 
36351ae08745Sheppo 	if (reg_msg->ncookies != 1) {
36361ae08745Sheppo 		/*
36371ae08745Sheppo 		 * In addition to fixing the assertion in the success case
36381ae08745Sheppo 		 * below, supporting drings which require more than one
36391ae08745Sheppo 		 * "cookie" requires increasing the value of vd->max_msglen
36401ae08745Sheppo 		 * somewhere in the code path prior to receiving the message
36411ae08745Sheppo 		 * which results in calling this function.  Note that without
36421ae08745Sheppo 		 * making this change, the larger message size required to
36431ae08745Sheppo 		 * accommodate multiple cookies cannot be successfully
36441ae08745Sheppo 		 * received, so this function will not even get called.
36451ae08745Sheppo 		 * Gracefully accommodating more dring cookies might
36461ae08745Sheppo 		 * reasonably demand exchanging an additional attribute or
36471ae08745Sheppo 		 * making a minor protocol adjustment
36481ae08745Sheppo 		 */
36493af08d82Slm66018 		PR0("reg_msg->ncookies = %u != 1", reg_msg->ncookies);
36501ae08745Sheppo 		return (EBADMSG);
36511ae08745Sheppo 	}
36521ae08745Sheppo 
36531ae08745Sheppo 	status = ldc_mem_dring_map(vd->ldc_handle, reg_msg->cookie,
36541ae08745Sheppo 	    reg_msg->ncookies, reg_msg->num_descriptors,
36554bac2208Snarayan 	    reg_msg->descriptor_size, LDC_DIRECT_MAP, &vd->dring_handle);
36561ae08745Sheppo 	if (status != 0) {
36573af08d82Slm66018 		PR0("ldc_mem_dring_map() returned errno %d", status);
36581ae08745Sheppo 		return (status);
36591ae08745Sheppo 	}
36601ae08745Sheppo 
36611ae08745Sheppo 	/*
36621ae08745Sheppo 	 * To remove the need for this assertion, must call
36631ae08745Sheppo 	 * ldc_mem_dring_nextcookie() successfully ncookies-1 times after a
36641ae08745Sheppo 	 * successful call to ldc_mem_dring_map()
36651ae08745Sheppo 	 */
36661ae08745Sheppo 	ASSERT(reg_msg->ncookies == 1);
36671ae08745Sheppo 
36681ae08745Sheppo 	if ((status =
36691ae08745Sheppo 	    ldc_mem_dring_info(vd->dring_handle, &dring_minfo)) != 0) {
36703af08d82Slm66018 		PR0("ldc_mem_dring_info() returned errno %d", status);
36711ae08745Sheppo 		if ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0)
36723af08d82Slm66018 			PR0("ldc_mem_dring_unmap() returned errno %d", status);
36731ae08745Sheppo 		return (status);
36741ae08745Sheppo 	}
36751ae08745Sheppo 
36761ae08745Sheppo 	if (dring_minfo.vaddr == NULL) {
36773af08d82Slm66018 		PR0("Descriptor ring virtual address is NULL");
36780a55fbb7Slm66018 		return (ENXIO);
36791ae08745Sheppo 	}
36801ae08745Sheppo 
36811ae08745Sheppo 
3682d10e4ef2Snarayan 	/* Initialize for valid message and mapped dring */
36831ae08745Sheppo 	PR1("descriptor size = %u, dring length = %u",
36841ae08745Sheppo 	    vd->descriptor_size, vd->dring_len);
36851ae08745Sheppo 	vd->initialized |= VD_DRING;
36861ae08745Sheppo 	vd->dring_ident = 1;	/* "There Can Be Only One" */
36871ae08745Sheppo 	vd->dring = dring_minfo.vaddr;
36881ae08745Sheppo 	vd->descriptor_size = reg_msg->descriptor_size;
36891ae08745Sheppo 	vd->dring_len = reg_msg->num_descriptors;
36901ae08745Sheppo 	reg_msg->dring_ident = vd->dring_ident;
3691d10e4ef2Snarayan 
3692d10e4ef2Snarayan 	/*
3693d10e4ef2Snarayan 	 * Allocate and initialize a "shadow" array of data structures for
3694d10e4ef2Snarayan 	 * tasks to process I/O requests in dring elements
3695d10e4ef2Snarayan 	 */
3696d10e4ef2Snarayan 	vd->dring_task =
3697d10e4ef2Snarayan 	    kmem_zalloc((sizeof (*vd->dring_task)) * vd->dring_len, KM_SLEEP);
3698d10e4ef2Snarayan 	for (int i = 0; i < vd->dring_len; i++) {
3699d10e4ef2Snarayan 		vd->dring_task[i].vd		= vd;
3700d10e4ef2Snarayan 		vd->dring_task[i].index		= i;
3701d10e4ef2Snarayan 		vd->dring_task[i].request	= &VD_DRING_ELEM(i)->payload;
37024bac2208Snarayan 
37034bac2208Snarayan 		status = ldc_mem_alloc_handle(vd->ldc_handle,
37044bac2208Snarayan 		    &(vd->dring_task[i].mhdl));
37054bac2208Snarayan 		if (status) {
37063af08d82Slm66018 			PR0("ldc_mem_alloc_handle() returned err %d ", status);
37074bac2208Snarayan 			return (ENXIO);
37084bac2208Snarayan 		}
37093af08d82Slm66018 
37103af08d82Slm66018 		vd->dring_task[i].msg = kmem_alloc(vd->max_msglen, KM_SLEEP);
3711d10e4ef2Snarayan 	}
3712d10e4ef2Snarayan 
37131ae08745Sheppo 	return (0);
37141ae08745Sheppo }
37151ae08745Sheppo 
37161ae08745Sheppo static int
37171ae08745Sheppo vd_process_dring_unreg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
37181ae08745Sheppo {
37191ae08745Sheppo 	vio_dring_unreg_msg_t	*unreg_msg = (vio_dring_unreg_msg_t *)msg;
37201ae08745Sheppo 
37211ae08745Sheppo 
37221ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
37231ae08745Sheppo 
37241ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
37251ae08745Sheppo 	    VIO_DRING_UNREG)) {
3726d10e4ef2Snarayan 		PR0("Message is not an unregister-dring message");
3727d10e4ef2Snarayan 		return (ENOMSG);
37281ae08745Sheppo 	}
37291ae08745Sheppo 
37301ae08745Sheppo 	if (msglen != sizeof (*unreg_msg)) {
37313af08d82Slm66018 		PR0("Expected %lu-byte unregister-dring message; "
37321ae08745Sheppo 		    "received %lu bytes", sizeof (*unreg_msg), msglen);
37331ae08745Sheppo 		return (EBADMSG);
37341ae08745Sheppo 	}
37351ae08745Sheppo 
37361ae08745Sheppo 	if (unreg_msg->dring_ident != vd->dring_ident) {
37373af08d82Slm66018 		PR0("Expected dring ident %lu; received %lu",
37381ae08745Sheppo 		    vd->dring_ident, unreg_msg->dring_ident);
37391ae08745Sheppo 		return (EBADMSG);
37401ae08745Sheppo 	}
37411ae08745Sheppo 
37421ae08745Sheppo 	return (0);
37431ae08745Sheppo }
37441ae08745Sheppo 
37451ae08745Sheppo static int
37461ae08745Sheppo process_rdx_msg(vio_msg_t *msg, size_t msglen)
37471ae08745Sheppo {
37481ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
37491ae08745Sheppo 
3750d10e4ef2Snarayan 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX)) {
3751d10e4ef2Snarayan 		PR0("Message is not an RDX message");
3752d10e4ef2Snarayan 		return (ENOMSG);
3753d10e4ef2Snarayan 	}
37541ae08745Sheppo 
37551ae08745Sheppo 	if (msglen != sizeof (vio_rdx_msg_t)) {
37563af08d82Slm66018 		PR0("Expected %lu-byte RDX message; received %lu bytes",
37571ae08745Sheppo 		    sizeof (vio_rdx_msg_t), msglen);
37581ae08745Sheppo 		return (EBADMSG);
37591ae08745Sheppo 	}
37601ae08745Sheppo 
3761d10e4ef2Snarayan 	PR0("Valid RDX message");
37621ae08745Sheppo 	return (0);
37631ae08745Sheppo }
37641ae08745Sheppo 
37651ae08745Sheppo static int
37661ae08745Sheppo vd_check_seq_num(vd_t *vd, uint64_t seq_num)
37671ae08745Sheppo {
37681ae08745Sheppo 	if ((vd->initialized & VD_SEQ_NUM) && (seq_num != vd->seq_num + 1)) {
37693af08d82Slm66018 		PR0("Received seq_num %lu; expected %lu",
37701ae08745Sheppo 		    seq_num, (vd->seq_num + 1));
37713af08d82Slm66018 		PR0("initiating soft reset");
3772d10e4ef2Snarayan 		vd_need_reset(vd, B_FALSE);
37731ae08745Sheppo 		return (1);
37741ae08745Sheppo 	}
37751ae08745Sheppo 
37761ae08745Sheppo 	vd->seq_num = seq_num;
37771ae08745Sheppo 	vd->initialized |= VD_SEQ_NUM;	/* superfluous after first time... */
37781ae08745Sheppo 	return (0);
37791ae08745Sheppo }
37801ae08745Sheppo 
37811ae08745Sheppo /*
37821ae08745Sheppo  * Return the expected size of an inband-descriptor message with all the
37831ae08745Sheppo  * cookies it claims to include
37841ae08745Sheppo  */
37851ae08745Sheppo static size_t
37861ae08745Sheppo expected_inband_size(vd_dring_inband_msg_t *msg)
37871ae08745Sheppo {
37881ae08745Sheppo 	return ((sizeof (*msg)) +
37891ae08745Sheppo 	    (msg->payload.ncookies - 1)*(sizeof (msg->payload.cookie[0])));
37901ae08745Sheppo }
37911ae08745Sheppo 
37921ae08745Sheppo /*
37931ae08745Sheppo  * Process an in-band descriptor message:  used with clients like OBP, with
37941ae08745Sheppo  * which vds exchanges descriptors within VIO message payloads, rather than
37951ae08745Sheppo  * operating on them within a descriptor ring
37961ae08745Sheppo  */
37971ae08745Sheppo static int
37983af08d82Slm66018 vd_process_desc_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
37991ae08745Sheppo {
38001ae08745Sheppo 	size_t			expected;
38011ae08745Sheppo 	vd_dring_inband_msg_t	*desc_msg = (vd_dring_inband_msg_t *)msg;
38021ae08745Sheppo 
38031ae08745Sheppo 
38041ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
38051ae08745Sheppo 
38061ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
3807d10e4ef2Snarayan 	    VIO_DESC_DATA)) {
3808d10e4ef2Snarayan 		PR1("Message is not an in-band-descriptor message");
3809d10e4ef2Snarayan 		return (ENOMSG);
3810d10e4ef2Snarayan 	}
38111ae08745Sheppo 
38121ae08745Sheppo 	if (msglen < sizeof (*desc_msg)) {
38133af08d82Slm66018 		PR0("Expected at least %lu-byte descriptor message; "
38141ae08745Sheppo 		    "received %lu bytes", sizeof (*desc_msg), msglen);
38151ae08745Sheppo 		return (EBADMSG);
38161ae08745Sheppo 	}
38171ae08745Sheppo 
38181ae08745Sheppo 	if (msglen != (expected = expected_inband_size(desc_msg))) {
38193af08d82Slm66018 		PR0("Expected %lu-byte descriptor message; "
38201ae08745Sheppo 		    "received %lu bytes", expected, msglen);
38211ae08745Sheppo 		return (EBADMSG);
38221ae08745Sheppo 	}
38231ae08745Sheppo 
3824d10e4ef2Snarayan 	if (vd_check_seq_num(vd, desc_msg->hdr.seq_num) != 0)
38251ae08745Sheppo 		return (EBADMSG);
38261ae08745Sheppo 
3827d10e4ef2Snarayan 	/*
3828d10e4ef2Snarayan 	 * Valid message:  Set up the in-band descriptor task and process the
3829d10e4ef2Snarayan 	 * request.  Arrange to acknowledge the client's message, unless an
3830d10e4ef2Snarayan 	 * error processing the descriptor task results in setting
3831d10e4ef2Snarayan 	 * VIO_SUBTYPE_NACK
3832d10e4ef2Snarayan 	 */
3833d10e4ef2Snarayan 	PR1("Valid in-band-descriptor message");
3834d10e4ef2Snarayan 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
38353af08d82Slm66018 
38363af08d82Slm66018 	ASSERT(vd->inband_task.msg != NULL);
38373af08d82Slm66018 
38383af08d82Slm66018 	bcopy(msg, vd->inband_task.msg, msglen);
3839d10e4ef2Snarayan 	vd->inband_task.msglen	= msglen;
38403af08d82Slm66018 
38413af08d82Slm66018 	/*
38423af08d82Slm66018 	 * The task request is now the payload of the message
38433af08d82Slm66018 	 * that was just copied into the body of the task.
38443af08d82Slm66018 	 */
38453af08d82Slm66018 	desc_msg = (vd_dring_inband_msg_t *)vd->inband_task.msg;
3846d10e4ef2Snarayan 	vd->inband_task.request	= &desc_msg->payload;
38473af08d82Slm66018 
3848d10e4ef2Snarayan 	return (vd_process_task(&vd->inband_task));
38491ae08745Sheppo }
38501ae08745Sheppo 
38511ae08745Sheppo static int
3852d10e4ef2Snarayan vd_process_element(vd_t *vd, vd_task_type_t type, uint32_t idx,
38533af08d82Slm66018     vio_msg_t *msg, size_t msglen)
38541ae08745Sheppo {
38551ae08745Sheppo 	int			status;
3856d10e4ef2Snarayan 	boolean_t		ready;
3857d10e4ef2Snarayan 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
38581ae08745Sheppo 
38591ae08745Sheppo 
3860d10e4ef2Snarayan 	/* Accept the updated dring element */
3861d10e4ef2Snarayan 	if ((status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) {
38623af08d82Slm66018 		PR0("ldc_mem_dring_acquire() returned errno %d", status);
38631ae08745Sheppo 		return (status);
38641ae08745Sheppo 	}
3865d10e4ef2Snarayan 	ready = (elem->hdr.dstate == VIO_DESC_READY);
3866d10e4ef2Snarayan 	if (ready) {
3867d10e4ef2Snarayan 		elem->hdr.dstate = VIO_DESC_ACCEPTED;
3868d10e4ef2Snarayan 	} else {
38693af08d82Slm66018 		PR0("descriptor %u not ready", idx);
3870d10e4ef2Snarayan 		VD_DUMP_DRING_ELEM(elem);
3871d10e4ef2Snarayan 	}
3872d10e4ef2Snarayan 	if ((status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) {
38733af08d82Slm66018 		PR0("ldc_mem_dring_release() returned errno %d", status);
38741ae08745Sheppo 		return (status);
38751ae08745Sheppo 	}
3876d10e4ef2Snarayan 	if (!ready)
3877d10e4ef2Snarayan 		return (EBUSY);
38781ae08745Sheppo 
38791ae08745Sheppo 
3880d10e4ef2Snarayan 	/* Initialize a task and process the accepted element */
3881d10e4ef2Snarayan 	PR1("Processing dring element %u", idx);
3882d10e4ef2Snarayan 	vd->dring_task[idx].type	= type;
38833af08d82Slm66018 
38843af08d82Slm66018 	/* duplicate msg buf for cookies etc. */
38853af08d82Slm66018 	bcopy(msg, vd->dring_task[idx].msg, msglen);
38863af08d82Slm66018 
3887d10e4ef2Snarayan 	vd->dring_task[idx].msglen	= msglen;
3888205eeb1aSlm66018 	return (vd_process_task(&vd->dring_task[idx]));
38891ae08745Sheppo }
38901ae08745Sheppo 
38911ae08745Sheppo static int
3892d10e4ef2Snarayan vd_process_element_range(vd_t *vd, int start, int end,
38933af08d82Slm66018     vio_msg_t *msg, size_t msglen)
3894d10e4ef2Snarayan {
3895d10e4ef2Snarayan 	int		i, n, nelem, status = 0;
3896d10e4ef2Snarayan 	boolean_t	inprogress = B_FALSE;
3897d10e4ef2Snarayan 	vd_task_type_t	type;
3898d10e4ef2Snarayan 
3899d10e4ef2Snarayan 
3900d10e4ef2Snarayan 	ASSERT(start >= 0);
3901d10e4ef2Snarayan 	ASSERT(end >= 0);
3902d10e4ef2Snarayan 
3903d10e4ef2Snarayan 	/*
3904d10e4ef2Snarayan 	 * Arrange to acknowledge the client's message, unless an error
3905d10e4ef2Snarayan 	 * processing one of the dring elements results in setting
3906d10e4ef2Snarayan 	 * VIO_SUBTYPE_NACK
3907d10e4ef2Snarayan 	 */
3908d10e4ef2Snarayan 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
3909d10e4ef2Snarayan 
3910d10e4ef2Snarayan 	/*
3911d10e4ef2Snarayan 	 * Process the dring elements in the range
3912d10e4ef2Snarayan 	 */
3913d10e4ef2Snarayan 	nelem = ((end < start) ? end + vd->dring_len : end) - start + 1;
3914d10e4ef2Snarayan 	for (i = start, n = nelem; n > 0; i = (i + 1) % vd->dring_len, n--) {
3915d10e4ef2Snarayan 		((vio_dring_msg_t *)msg)->end_idx = i;
3916d10e4ef2Snarayan 		type = (n == 1) ? VD_FINAL_RANGE_TASK : VD_NONFINAL_RANGE_TASK;
39173af08d82Slm66018 		status = vd_process_element(vd, type, i, msg, msglen);
3918d10e4ef2Snarayan 		if (status == EINPROGRESS)
3919d10e4ef2Snarayan 			inprogress = B_TRUE;
3920d10e4ef2Snarayan 		else if (status != 0)
3921d10e4ef2Snarayan 			break;
3922d10e4ef2Snarayan 	}
3923d10e4ef2Snarayan 
3924d10e4ef2Snarayan 	/*
3925d10e4ef2Snarayan 	 * If some, but not all, operations of a multi-element range are in
3926d10e4ef2Snarayan 	 * progress, wait for other operations to complete before returning
3927d10e4ef2Snarayan 	 * (which will result in "ack" or "nack" of the message).  Note that
3928d10e4ef2Snarayan 	 * all outstanding operations will need to complete, not just the ones
3929d10e4ef2Snarayan 	 * corresponding to the current range of dring elements; howevever, as
3930d10e4ef2Snarayan 	 * this situation is an error case, performance is less critical.
3931d10e4ef2Snarayan 	 */
3932d10e4ef2Snarayan 	if ((nelem > 1) && (status != EINPROGRESS) && inprogress)
3933d10e4ef2Snarayan 		ddi_taskq_wait(vd->completionq);
3934d10e4ef2Snarayan 
3935d10e4ef2Snarayan 	return (status);
3936d10e4ef2Snarayan }
3937d10e4ef2Snarayan 
3938d10e4ef2Snarayan static int
39393af08d82Slm66018 vd_process_dring_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
39401ae08745Sheppo {
39411ae08745Sheppo 	vio_dring_msg_t	*dring_msg = (vio_dring_msg_t *)msg;
39421ae08745Sheppo 
39431ae08745Sheppo 
39441ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
39451ae08745Sheppo 
39461ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
39471ae08745Sheppo 	    VIO_DRING_DATA)) {
3948d10e4ef2Snarayan 		PR1("Message is not a dring-data message");
3949d10e4ef2Snarayan 		return (ENOMSG);
39501ae08745Sheppo 	}
39511ae08745Sheppo 
39521ae08745Sheppo 	if (msglen != sizeof (*dring_msg)) {
39533af08d82Slm66018 		PR0("Expected %lu-byte dring message; received %lu bytes",
39541ae08745Sheppo 		    sizeof (*dring_msg), msglen);
39551ae08745Sheppo 		return (EBADMSG);
39561ae08745Sheppo 	}
39571ae08745Sheppo 
3958d10e4ef2Snarayan 	if (vd_check_seq_num(vd, dring_msg->seq_num) != 0)
39591ae08745Sheppo 		return (EBADMSG);
39601ae08745Sheppo 
39611ae08745Sheppo 	if (dring_msg->dring_ident != vd->dring_ident) {
39623af08d82Slm66018 		PR0("Expected dring ident %lu; received ident %lu",
39631ae08745Sheppo 		    vd->dring_ident, dring_msg->dring_ident);
39641ae08745Sheppo 		return (EBADMSG);
39651ae08745Sheppo 	}
39661ae08745Sheppo 
3967d10e4ef2Snarayan 	if (dring_msg->start_idx >= vd->dring_len) {
39683af08d82Slm66018 		PR0("\"start_idx\" = %u; must be less than %u",
3969d10e4ef2Snarayan 		    dring_msg->start_idx, vd->dring_len);
3970d10e4ef2Snarayan 		return (EBADMSG);
3971d10e4ef2Snarayan 	}
39721ae08745Sheppo 
3973d10e4ef2Snarayan 	if ((dring_msg->end_idx < 0) ||
3974d10e4ef2Snarayan 	    (dring_msg->end_idx >= vd->dring_len)) {
39753af08d82Slm66018 		PR0("\"end_idx\" = %u; must be >= 0 and less than %u",
3976d10e4ef2Snarayan 		    dring_msg->end_idx, vd->dring_len);
3977d10e4ef2Snarayan 		return (EBADMSG);
3978d10e4ef2Snarayan 	}
3979d10e4ef2Snarayan 
3980d10e4ef2Snarayan 	/* Valid message; process range of updated dring elements */
3981d10e4ef2Snarayan 	PR1("Processing descriptor range, start = %u, end = %u",
3982d10e4ef2Snarayan 	    dring_msg->start_idx, dring_msg->end_idx);
3983d10e4ef2Snarayan 	return (vd_process_element_range(vd, dring_msg->start_idx,
39843af08d82Slm66018 	    dring_msg->end_idx, msg, msglen));
39851ae08745Sheppo }
39861ae08745Sheppo 
39871ae08745Sheppo static int
39881ae08745Sheppo recv_msg(ldc_handle_t ldc_handle, void *msg, size_t *nbytes)
39891ae08745Sheppo {
39901ae08745Sheppo 	int	retry, status;
39911ae08745Sheppo 	size_t	size = *nbytes;
39921ae08745Sheppo 
39931ae08745Sheppo 
39941ae08745Sheppo 	for (retry = 0, status = ETIMEDOUT;
39951ae08745Sheppo 	    retry < vds_ldc_retries && status == ETIMEDOUT;
39961ae08745Sheppo 	    retry++) {
39971ae08745Sheppo 		PR1("ldc_read() attempt %d", (retry + 1));
39981ae08745Sheppo 		*nbytes = size;
39991ae08745Sheppo 		status = ldc_read(ldc_handle, msg, nbytes);
40001ae08745Sheppo 	}
40011ae08745Sheppo 
40023af08d82Slm66018 	if (status) {
40033af08d82Slm66018 		PR0("ldc_read() returned errno %d", status);
40043af08d82Slm66018 		if (status != ECONNRESET)
40053af08d82Slm66018 			return (ENOMSG);
40061ae08745Sheppo 		return (status);
40071ae08745Sheppo 	} else if (*nbytes == 0) {
40081ae08745Sheppo 		PR1("ldc_read() returned 0 and no message read");
40091ae08745Sheppo 		return (ENOMSG);
40101ae08745Sheppo 	}
40111ae08745Sheppo 
40121ae08745Sheppo 	PR1("RCVD %lu-byte message", *nbytes);
40131ae08745Sheppo 	return (0);
40141ae08745Sheppo }
40151ae08745Sheppo 
40161ae08745Sheppo static int
40173af08d82Slm66018 vd_do_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
40181ae08745Sheppo {
40191ae08745Sheppo 	int		status;
40201ae08745Sheppo 
40211ae08745Sheppo 
40221ae08745Sheppo 	PR1("Processing (%x/%x/%x) message", msg->tag.vio_msgtype,
40231ae08745Sheppo 	    msg->tag.vio_subtype, msg->tag.vio_subtype_env);
40243af08d82Slm66018 #ifdef	DEBUG
40253af08d82Slm66018 	vd_decode_tag(msg);
40263af08d82Slm66018 #endif
40271ae08745Sheppo 
40281ae08745Sheppo 	/*
40291ae08745Sheppo 	 * Validate session ID up front, since it applies to all messages
40301ae08745Sheppo 	 * once set
40311ae08745Sheppo 	 */
40321ae08745Sheppo 	if ((msg->tag.vio_sid != vd->sid) && (vd->initialized & VD_SID)) {
40333af08d82Slm66018 		PR0("Expected SID %u, received %u", vd->sid,
40341ae08745Sheppo 		    msg->tag.vio_sid);
40351ae08745Sheppo 		return (EBADMSG);
40361ae08745Sheppo 	}
40371ae08745Sheppo 
40383af08d82Slm66018 	PR1("\tWhile in state %d (%s)", vd->state, vd_decode_state(vd->state));
40391ae08745Sheppo 
40401ae08745Sheppo 	/*
40411ae08745Sheppo 	 * Process the received message based on connection state
40421ae08745Sheppo 	 */
40431ae08745Sheppo 	switch (vd->state) {
40441ae08745Sheppo 	case VD_STATE_INIT:	/* expect version message */
40450a55fbb7Slm66018 		if ((status = vd_process_ver_msg(vd, msg, msglen)) != 0)
40461ae08745Sheppo 			return (status);
40471ae08745Sheppo 
40481ae08745Sheppo 		/* Version negotiated, move to that state */
40491ae08745Sheppo 		vd->state = VD_STATE_VER;
40501ae08745Sheppo 		return (0);
40511ae08745Sheppo 
40521ae08745Sheppo 	case VD_STATE_VER:	/* expect attribute message */
40531ae08745Sheppo 		if ((status = vd_process_attr_msg(vd, msg, msglen)) != 0)
40541ae08745Sheppo 			return (status);
40551ae08745Sheppo 
40561ae08745Sheppo 		/* Attributes exchanged, move to that state */
40571ae08745Sheppo 		vd->state = VD_STATE_ATTR;
40581ae08745Sheppo 		return (0);
40591ae08745Sheppo 
40601ae08745Sheppo 	case VD_STATE_ATTR:
40611ae08745Sheppo 		switch (vd->xfer_mode) {
40621ae08745Sheppo 		case VIO_DESC_MODE:	/* expect RDX message */
40631ae08745Sheppo 			if ((status = process_rdx_msg(msg, msglen)) != 0)
40641ae08745Sheppo 				return (status);
40651ae08745Sheppo 
40661ae08745Sheppo 			/* Ready to receive in-band descriptors */
40671ae08745Sheppo 			vd->state = VD_STATE_DATA;
40681ae08745Sheppo 			return (0);
40691ae08745Sheppo 
4070f0ca1d9aSsb155480 		case VIO_DRING_MODE_V1_0:  /* expect register-dring message */
40711ae08745Sheppo 			if ((status =
40721ae08745Sheppo 			    vd_process_dring_reg_msg(vd, msg, msglen)) != 0)
40731ae08745Sheppo 				return (status);
40741ae08745Sheppo 
40751ae08745Sheppo 			/* One dring negotiated, move to that state */
40761ae08745Sheppo 			vd->state = VD_STATE_DRING;
40771ae08745Sheppo 			return (0);
40781ae08745Sheppo 
40791ae08745Sheppo 		default:
40801ae08745Sheppo 			ASSERT("Unsupported transfer mode");
40813af08d82Slm66018 			PR0("Unsupported transfer mode");
40821ae08745Sheppo 			return (ENOTSUP);
40831ae08745Sheppo 		}
40841ae08745Sheppo 
40851ae08745Sheppo 	case VD_STATE_DRING:	/* expect RDX, register-dring, or unreg-dring */
40861ae08745Sheppo 		if ((status = process_rdx_msg(msg, msglen)) == 0) {
40871ae08745Sheppo 			/* Ready to receive data */
40881ae08745Sheppo 			vd->state = VD_STATE_DATA;
40891ae08745Sheppo 			return (0);
40901ae08745Sheppo 		} else if (status != ENOMSG) {
40911ae08745Sheppo 			return (status);
40921ae08745Sheppo 		}
40931ae08745Sheppo 
40941ae08745Sheppo 
40951ae08745Sheppo 		/*
40961ae08745Sheppo 		 * If another register-dring message is received, stay in
40971ae08745Sheppo 		 * dring state in case the client sends RDX; although the
40981ae08745Sheppo 		 * protocol allows multiple drings, this server does not
40991ae08745Sheppo 		 * support using more than one
41001ae08745Sheppo 		 */
41011ae08745Sheppo 		if ((status =
41021ae08745Sheppo 		    vd_process_dring_reg_msg(vd, msg, msglen)) != ENOMSG)
41031ae08745Sheppo 			return (status);
41041ae08745Sheppo 
41051ae08745Sheppo 		/*
41061ae08745Sheppo 		 * Acknowledge an unregister-dring message, but reset the
41071ae08745Sheppo 		 * connection anyway:  Although the protocol allows
41081ae08745Sheppo 		 * unregistering drings, this server cannot serve a vdisk
41091ae08745Sheppo 		 * without its only dring
41101ae08745Sheppo 		 */
41111ae08745Sheppo 		status = vd_process_dring_unreg_msg(vd, msg, msglen);
41121ae08745Sheppo 		return ((status == 0) ? ENOTSUP : status);
41131ae08745Sheppo 
41141ae08745Sheppo 	case VD_STATE_DATA:
41151ae08745Sheppo 		switch (vd->xfer_mode) {
41161ae08745Sheppo 		case VIO_DESC_MODE:	/* expect in-band-descriptor message */
41173af08d82Slm66018 			return (vd_process_desc_msg(vd, msg, msglen));
41181ae08745Sheppo 
4119f0ca1d9aSsb155480 		case VIO_DRING_MODE_V1_0: /* expect dring-data or unreg-dring */
41201ae08745Sheppo 			/*
41211ae08745Sheppo 			 * Typically expect dring-data messages, so handle
41221ae08745Sheppo 			 * them first
41231ae08745Sheppo 			 */
41241ae08745Sheppo 			if ((status = vd_process_dring_msg(vd, msg,
41253af08d82Slm66018 			    msglen)) != ENOMSG)
41261ae08745Sheppo 				return (status);
41271ae08745Sheppo 
41281ae08745Sheppo 			/*
41291ae08745Sheppo 			 * Acknowledge an unregister-dring message, but reset
41301ae08745Sheppo 			 * the connection anyway:  Although the protocol
41311ae08745Sheppo 			 * allows unregistering drings, this server cannot
41321ae08745Sheppo 			 * serve a vdisk without its only dring
41331ae08745Sheppo 			 */
41341ae08745Sheppo 			status = vd_process_dring_unreg_msg(vd, msg, msglen);
41351ae08745Sheppo 			return ((status == 0) ? ENOTSUP : status);
41361ae08745Sheppo 
41371ae08745Sheppo 		default:
41381ae08745Sheppo 			ASSERT("Unsupported transfer mode");
41393af08d82Slm66018 			PR0("Unsupported transfer mode");
41401ae08745Sheppo 			return (ENOTSUP);
41411ae08745Sheppo 		}
41421ae08745Sheppo 
41431ae08745Sheppo 	default:
41441ae08745Sheppo 		ASSERT("Invalid client connection state");
41453af08d82Slm66018 		PR0("Invalid client connection state");
41461ae08745Sheppo 		return (ENOTSUP);
41471ae08745Sheppo 	}
41481ae08745Sheppo }
41491ae08745Sheppo 
4150d10e4ef2Snarayan static int
41513af08d82Slm66018 vd_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
41521ae08745Sheppo {
41531ae08745Sheppo 	int		status;
41541ae08745Sheppo 	boolean_t	reset_ldc = B_FALSE;
4155205eeb1aSlm66018 	vd_task_t	task;
41561ae08745Sheppo 
41571ae08745Sheppo 	/*
41581ae08745Sheppo 	 * Check that the message is at least big enough for a "tag", so that
41591ae08745Sheppo 	 * message processing can proceed based on tag-specified message type
41601ae08745Sheppo 	 */
41611ae08745Sheppo 	if (msglen < sizeof (vio_msg_tag_t)) {
41623af08d82Slm66018 		PR0("Received short (%lu-byte) message", msglen);
41631ae08745Sheppo 		/* Can't "nack" short message, so drop the big hammer */
41643af08d82Slm66018 		PR0("initiating full reset");
4165d10e4ef2Snarayan 		vd_need_reset(vd, B_TRUE);
4166d10e4ef2Snarayan 		return (EBADMSG);
41671ae08745Sheppo 	}
41681ae08745Sheppo 
41691ae08745Sheppo 	/*
41701ae08745Sheppo 	 * Process the message
41711ae08745Sheppo 	 */
41723af08d82Slm66018 	switch (status = vd_do_process_msg(vd, msg, msglen)) {
41731ae08745Sheppo 	case 0:
41741ae08745Sheppo 		/* "ack" valid, successfully-processed messages */
41751ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
41761ae08745Sheppo 		break;
41771ae08745Sheppo 
4178d10e4ef2Snarayan 	case EINPROGRESS:
4179d10e4ef2Snarayan 		/* The completion handler will "ack" or "nack" the message */
4180d10e4ef2Snarayan 		return (EINPROGRESS);
41811ae08745Sheppo 	case ENOMSG:
41823af08d82Slm66018 		PR0("Received unexpected message");
41831ae08745Sheppo 		_NOTE(FALLTHROUGH);
41841ae08745Sheppo 	case EBADMSG:
41851ae08745Sheppo 	case ENOTSUP:
4186205eeb1aSlm66018 		/* "transport" error will cause NACK of invalid messages */
41871ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
41881ae08745Sheppo 		break;
41891ae08745Sheppo 
41901ae08745Sheppo 	default:
4191205eeb1aSlm66018 		/* "transport" error will cause NACK of invalid messages */
41921ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
41931ae08745Sheppo 		/* An LDC error probably occurred, so try resetting it */
41941ae08745Sheppo 		reset_ldc = B_TRUE;
41951ae08745Sheppo 		break;
41961ae08745Sheppo 	}
41971ae08745Sheppo 
41983af08d82Slm66018 	PR1("\tResulting in state %d (%s)", vd->state,
41993af08d82Slm66018 	    vd_decode_state(vd->state));
42003af08d82Slm66018 
4201205eeb1aSlm66018 	/* populate the task so we can dispatch it on the taskq */
4202205eeb1aSlm66018 	task.vd = vd;
4203205eeb1aSlm66018 	task.msg = msg;
4204205eeb1aSlm66018 	task.msglen = msglen;
4205205eeb1aSlm66018 
4206205eeb1aSlm66018 	/*
4207205eeb1aSlm66018 	 * Queue a task to send the notification that the operation completed.
4208205eeb1aSlm66018 	 * We need to ensure that requests are responded to in the correct
4209205eeb1aSlm66018 	 * order and since the taskq is processed serially this ordering
4210205eeb1aSlm66018 	 * is maintained.
4211205eeb1aSlm66018 	 */
4212205eeb1aSlm66018 	(void) ddi_taskq_dispatch(vd->completionq, vd_serial_notify,
4213205eeb1aSlm66018 	    &task, DDI_SLEEP);
4214205eeb1aSlm66018 
4215205eeb1aSlm66018 	/*
4216205eeb1aSlm66018 	 * To ensure handshake negotiations do not happen out of order, such
4217205eeb1aSlm66018 	 * requests that come through this path should not be done in parallel
4218205eeb1aSlm66018 	 * so we need to wait here until the response is sent to the client.
4219205eeb1aSlm66018 	 */
4220205eeb1aSlm66018 	ddi_taskq_wait(vd->completionq);
42211ae08745Sheppo 
4222d10e4ef2Snarayan 	/* Arrange to reset the connection for nack'ed or failed messages */
42233af08d82Slm66018 	if ((status != 0) || reset_ldc) {
42243af08d82Slm66018 		PR0("initiating %s reset",
42253af08d82Slm66018 		    (reset_ldc) ? "full" : "soft");
4226d10e4ef2Snarayan 		vd_need_reset(vd, reset_ldc);
42273af08d82Slm66018 	}
4228d10e4ef2Snarayan 
4229d10e4ef2Snarayan 	return (status);
4230d10e4ef2Snarayan }
4231d10e4ef2Snarayan 
4232d10e4ef2Snarayan static boolean_t
4233d10e4ef2Snarayan vd_enabled(vd_t *vd)
4234d10e4ef2Snarayan {
4235d10e4ef2Snarayan 	boolean_t	enabled;
4236d10e4ef2Snarayan 
4237d10e4ef2Snarayan 	mutex_enter(&vd->lock);
4238d10e4ef2Snarayan 	enabled = vd->enabled;
4239d10e4ef2Snarayan 	mutex_exit(&vd->lock);
4240d10e4ef2Snarayan 	return (enabled);
42411ae08745Sheppo }
42421ae08745Sheppo 
42431ae08745Sheppo static void
42440a55fbb7Slm66018 vd_recv_msg(void *arg)
42451ae08745Sheppo {
42461ae08745Sheppo 	vd_t	*vd = (vd_t *)arg;
42473af08d82Slm66018 	int	rv = 0, status = 0;
42481ae08745Sheppo 
42491ae08745Sheppo 	ASSERT(vd != NULL);
42503af08d82Slm66018 
4251d10e4ef2Snarayan 	PR2("New task to receive incoming message(s)");
42523af08d82Slm66018 
42533af08d82Slm66018 
4254d10e4ef2Snarayan 	while (vd_enabled(vd) && status == 0) {
4255d10e4ef2Snarayan 		size_t		msglen, msgsize;
42563af08d82Slm66018 		ldc_status_t	lstatus;
4257d10e4ef2Snarayan 
42580a55fbb7Slm66018 		/*
4259d10e4ef2Snarayan 		 * Receive and process a message
42600a55fbb7Slm66018 		 */
4261d10e4ef2Snarayan 		vd_reset_if_needed(vd);	/* can change vd->max_msglen */
42623af08d82Slm66018 
42633af08d82Slm66018 		/*
42643af08d82Slm66018 		 * check if channel is UP - else break out of loop
42653af08d82Slm66018 		 */
42663af08d82Slm66018 		status = ldc_status(vd->ldc_handle, &lstatus);
42673af08d82Slm66018 		if (lstatus != LDC_UP) {
42683af08d82Slm66018 			PR0("channel not up (status=%d), exiting recv loop\n",
42693af08d82Slm66018 			    lstatus);
42703af08d82Slm66018 			break;
42713af08d82Slm66018 		}
42723af08d82Slm66018 
42733af08d82Slm66018 		ASSERT(vd->max_msglen != 0);
42743af08d82Slm66018 
4275d10e4ef2Snarayan 		msgsize = vd->max_msglen; /* stable copy for alloc/free */
42763af08d82Slm66018 		msglen	= msgsize;	  /* actual len after recv_msg() */
42773af08d82Slm66018 
42783af08d82Slm66018 		status = recv_msg(vd->ldc_handle, vd->vio_msgp, &msglen);
42793af08d82Slm66018 		switch (status) {
42803af08d82Slm66018 		case 0:
42813af08d82Slm66018 			rv = vd_process_msg(vd, (vio_msg_t *)vd->vio_msgp,
42823af08d82Slm66018 			    msglen);
42833af08d82Slm66018 			/* check if max_msglen changed */
42843af08d82Slm66018 			if (msgsize != vd->max_msglen) {
42853af08d82Slm66018 				PR0("max_msglen changed 0x%lx to 0x%lx bytes\n",
42863af08d82Slm66018 				    msgsize, vd->max_msglen);
42873af08d82Slm66018 				kmem_free(vd->vio_msgp, msgsize);
42883af08d82Slm66018 				vd->vio_msgp =
42893af08d82Slm66018 				    kmem_alloc(vd->max_msglen, KM_SLEEP);
42903af08d82Slm66018 			}
42913af08d82Slm66018 			if (rv == EINPROGRESS)
42923af08d82Slm66018 				continue;
42933af08d82Slm66018 			break;
42943af08d82Slm66018 
42953af08d82Slm66018 		case ENOMSG:
42963af08d82Slm66018 			break;
42973af08d82Slm66018 
42983af08d82Slm66018 		case ECONNRESET:
42993af08d82Slm66018 			PR0("initiating soft reset (ECONNRESET)\n");
43003af08d82Slm66018 			vd_need_reset(vd, B_FALSE);
43013af08d82Slm66018 			status = 0;
43023af08d82Slm66018 			break;
43033af08d82Slm66018 
43043af08d82Slm66018 		default:
4305d10e4ef2Snarayan 			/* Probably an LDC failure; arrange to reset it */
43063af08d82Slm66018 			PR0("initiating full reset (status=0x%x)", status);
4307d10e4ef2Snarayan 			vd_need_reset(vd, B_TRUE);
43083af08d82Slm66018 			break;
43090a55fbb7Slm66018 		}
43101ae08745Sheppo 	}
43113af08d82Slm66018 
4312d10e4ef2Snarayan 	PR2("Task finished");
43130a55fbb7Slm66018 }
43140a55fbb7Slm66018 
43150a55fbb7Slm66018 static uint_t
43161ae08745Sheppo vd_handle_ldc_events(uint64_t event, caddr_t arg)
43171ae08745Sheppo {
43181ae08745Sheppo 	vd_t	*vd = (vd_t *)(void *)arg;
43193af08d82Slm66018 	int	status;
43201ae08745Sheppo 
43211ae08745Sheppo 	ASSERT(vd != NULL);
4322d10e4ef2Snarayan 
4323d10e4ef2Snarayan 	if (!vd_enabled(vd))
4324d10e4ef2Snarayan 		return (LDC_SUCCESS);
4325d10e4ef2Snarayan 
43263af08d82Slm66018 	if (event & LDC_EVT_DOWN) {
432734683adeSsg70180 		PR0("LDC_EVT_DOWN: LDC channel went down");
43283af08d82Slm66018 
43293af08d82Slm66018 		vd_need_reset(vd, B_TRUE);
43303af08d82Slm66018 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
43313af08d82Slm66018 		    DDI_SLEEP);
43323af08d82Slm66018 		if (status == DDI_FAILURE) {
43333af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
43343af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
43353af08d82Slm66018 		}
43363af08d82Slm66018 	}
43373af08d82Slm66018 
4338d10e4ef2Snarayan 	if (event & LDC_EVT_RESET) {
43393af08d82Slm66018 		PR0("LDC_EVT_RESET: LDC channel was reset");
43403af08d82Slm66018 
43413af08d82Slm66018 		if (vd->state != VD_STATE_INIT) {
43423af08d82Slm66018 			PR0("scheduling full reset");
43433af08d82Slm66018 			vd_need_reset(vd, B_FALSE);
43443af08d82Slm66018 			status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
43453af08d82Slm66018 			    vd, DDI_SLEEP);
43463af08d82Slm66018 			if (status == DDI_FAILURE) {
43473af08d82Slm66018 				PR0("cannot schedule task to recv msg\n");
43483af08d82Slm66018 				vd_need_reset(vd, B_TRUE);
43493af08d82Slm66018 			}
43503af08d82Slm66018 
43513af08d82Slm66018 		} else {
43523af08d82Slm66018 			PR0("channel already reset, ignoring...\n");
43533af08d82Slm66018 			PR0("doing ldc up...\n");
43543af08d82Slm66018 			(void) ldc_up(vd->ldc_handle);
43553af08d82Slm66018 		}
43563af08d82Slm66018 
4357d10e4ef2Snarayan 		return (LDC_SUCCESS);
4358d10e4ef2Snarayan 	}
4359d10e4ef2Snarayan 
4360d10e4ef2Snarayan 	if (event & LDC_EVT_UP) {
43613af08d82Slm66018 		PR0("EVT_UP: LDC is up\nResetting client connection state");
43623af08d82Slm66018 		PR0("initiating soft reset");
4363d10e4ef2Snarayan 		vd_need_reset(vd, B_FALSE);
43643af08d82Slm66018 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
43653af08d82Slm66018 		    vd, DDI_SLEEP);
43663af08d82Slm66018 		if (status == DDI_FAILURE) {
43673af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
43683af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
43693af08d82Slm66018 			return (LDC_SUCCESS);
43703af08d82Slm66018 		}
4371d10e4ef2Snarayan 	}
4372d10e4ef2Snarayan 
4373d10e4ef2Snarayan 	if (event & LDC_EVT_READ) {
4374d10e4ef2Snarayan 		int	status;
4375d10e4ef2Snarayan 
4376d10e4ef2Snarayan 		PR1("New data available");
4377d10e4ef2Snarayan 		/* Queue a task to receive the new data */
4378d10e4ef2Snarayan 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
4379d10e4ef2Snarayan 		    DDI_SLEEP);
43803af08d82Slm66018 
43813af08d82Slm66018 		if (status == DDI_FAILURE) {
43823af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
43833af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
43843af08d82Slm66018 		}
4385d10e4ef2Snarayan 	}
4386d10e4ef2Snarayan 
4387d10e4ef2Snarayan 	return (LDC_SUCCESS);
43881ae08745Sheppo }
43891ae08745Sheppo 
43901ae08745Sheppo static uint_t
43911ae08745Sheppo vds_check_for_vd(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
43921ae08745Sheppo {
43931ae08745Sheppo 	_NOTE(ARGUNUSED(key, val))
43941ae08745Sheppo 	(*((uint_t *)arg))++;
43951ae08745Sheppo 	return (MH_WALK_TERMINATE);
43961ae08745Sheppo }
43971ae08745Sheppo 
43981ae08745Sheppo 
43991ae08745Sheppo static int
44001ae08745Sheppo vds_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
44011ae08745Sheppo {
44021ae08745Sheppo 	uint_t	vd_present = 0;
44031ae08745Sheppo 	minor_t	instance;
44041ae08745Sheppo 	vds_t	*vds;
44051ae08745Sheppo 
44061ae08745Sheppo 
44071ae08745Sheppo 	switch (cmd) {
44081ae08745Sheppo 	case DDI_DETACH:
44091ae08745Sheppo 		/* the real work happens below */
44101ae08745Sheppo 		break;
44111ae08745Sheppo 	case DDI_SUSPEND:
4412d10e4ef2Snarayan 		PR0("No action required for DDI_SUSPEND");
44131ae08745Sheppo 		return (DDI_SUCCESS);
44141ae08745Sheppo 	default:
44153af08d82Slm66018 		PR0("Unrecognized \"cmd\"");
44161ae08745Sheppo 		return (DDI_FAILURE);
44171ae08745Sheppo 	}
44181ae08745Sheppo 
44191ae08745Sheppo 	ASSERT(cmd == DDI_DETACH);
44201ae08745Sheppo 	instance = ddi_get_instance(dip);
44211ae08745Sheppo 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
44223af08d82Slm66018 		PR0("Could not get state for instance %u", instance);
44231ae08745Sheppo 		ddi_soft_state_free(vds_state, instance);
44241ae08745Sheppo 		return (DDI_FAILURE);
44251ae08745Sheppo 	}
44261ae08745Sheppo 
44271ae08745Sheppo 	/* Do no detach when serving any vdisks */
44281ae08745Sheppo 	mod_hash_walk(vds->vd_table, vds_check_for_vd, &vd_present);
44291ae08745Sheppo 	if (vd_present) {
44301ae08745Sheppo 		PR0("Not detaching because serving vdisks");
44311ae08745Sheppo 		return (DDI_FAILURE);
44321ae08745Sheppo 	}
44331ae08745Sheppo 
44341ae08745Sheppo 	PR0("Detaching");
4435445b4c2eSsb155480 	if (vds->initialized & VDS_MDEG) {
44361ae08745Sheppo 		(void) mdeg_unregister(vds->mdeg);
4437445b4c2eSsb155480 		kmem_free(vds->ispecp->specp, sizeof (vds_prop_template));
4438445b4c2eSsb155480 		kmem_free(vds->ispecp, sizeof (mdeg_node_spec_t));
4439445b4c2eSsb155480 		vds->ispecp = NULL;
4440445b4c2eSsb155480 		vds->mdeg = NULL;
4441445b4c2eSsb155480 	}
4442445b4c2eSsb155480 
4443*8fce2fd6Sachartre 	vds_driver_types_free(vds);
4444*8fce2fd6Sachartre 
44451ae08745Sheppo 	if (vds->initialized & VDS_LDI)
44461ae08745Sheppo 		(void) ldi_ident_release(vds->ldi_ident);
44471ae08745Sheppo 	mod_hash_destroy_hash(vds->vd_table);
44481ae08745Sheppo 	ddi_soft_state_free(vds_state, instance);
44491ae08745Sheppo 	return (DDI_SUCCESS);
44501ae08745Sheppo }
44511ae08745Sheppo 
44521ae08745Sheppo static boolean_t
44531ae08745Sheppo is_pseudo_device(dev_info_t *dip)
44541ae08745Sheppo {
44551ae08745Sheppo 	dev_info_t	*parent, *root = ddi_root_node();
44561ae08745Sheppo 
44571ae08745Sheppo 
44581ae08745Sheppo 	for (parent = ddi_get_parent(dip); (parent != NULL) && (parent != root);
44591ae08745Sheppo 	    parent = ddi_get_parent(parent)) {
44601ae08745Sheppo 		if (strcmp(ddi_get_name(parent), DEVI_PSEUDO_NEXNAME) == 0)
44611ae08745Sheppo 			return (B_TRUE);
44621ae08745Sheppo 	}
44631ae08745Sheppo 
44641ae08745Sheppo 	return (B_FALSE);
44651ae08745Sheppo }
44661ae08745Sheppo 
446717cadca8Slm66018 /*
446817cadca8Slm66018  * Description:
446917cadca8Slm66018  *	This function checks to see if the file being used as a
447017cadca8Slm66018  *	virtual disk is an ISO image. An ISO image is a special
447117cadca8Slm66018  *	case which can be booted/installed from like a CD/DVD
447217cadca8Slm66018  *
447317cadca8Slm66018  * Parameters:
447417cadca8Slm66018  *	vd		- disk on which the operation is performed.
447517cadca8Slm66018  *
447617cadca8Slm66018  * Return Code:
447717cadca8Slm66018  *	B_TRUE		- The file is an ISO 9660 compliant image
447817cadca8Slm66018  *	B_FALSE		- just a regular disk image file
447917cadca8Slm66018  */
448017cadca8Slm66018 static boolean_t
448117cadca8Slm66018 vd_file_is_iso_image(vd_t *vd)
448217cadca8Slm66018 {
448317cadca8Slm66018 	char	iso_buf[ISO_SECTOR_SIZE];
448417cadca8Slm66018 	int	i, rv;
448517cadca8Slm66018 	uint_t	sec;
448617cadca8Slm66018 
448717cadca8Slm66018 	ASSERT(vd->file);
448817cadca8Slm66018 
448917cadca8Slm66018 	/*
449017cadca8Slm66018 	 * If we have already discovered and saved this info we can
449117cadca8Slm66018 	 * short-circuit the check and avoid reading the file.
449217cadca8Slm66018 	 */
449317cadca8Slm66018 	if (vd->vdisk_media == VD_MEDIA_DVD || vd->vdisk_media == VD_MEDIA_CD)
449417cadca8Slm66018 		return (B_TRUE);
449517cadca8Slm66018 
449617cadca8Slm66018 	/*
449717cadca8Slm66018 	 * We wish to read the sector that should contain the 2nd ISO volume
449817cadca8Slm66018 	 * descriptor. The second field in this descriptor is called the
449917cadca8Slm66018 	 * Standard Identifier and is set to CD001 for a CD-ROM compliant
450017cadca8Slm66018 	 * to the ISO 9660 standard.
450117cadca8Slm66018 	 */
450217cadca8Slm66018 	sec = (ISO_VOLDESC_SEC * ISO_SECTOR_SIZE) / vd->vdisk_block_size;
450317cadca8Slm66018 	rv = vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)iso_buf,
450417cadca8Slm66018 	    sec, ISO_SECTOR_SIZE);
450517cadca8Slm66018 
450617cadca8Slm66018 	if (rv < 0)
450717cadca8Slm66018 		return (B_FALSE);
450817cadca8Slm66018 
450917cadca8Slm66018 	for (i = 0; i < ISO_ID_STRLEN; i++) {
451017cadca8Slm66018 		if (ISO_STD_ID(iso_buf)[i] != ISO_ID_STRING[i])
451117cadca8Slm66018 			return (B_FALSE);
451217cadca8Slm66018 	}
451317cadca8Slm66018 
451417cadca8Slm66018 	return (B_TRUE);
451517cadca8Slm66018 }
451617cadca8Slm66018 
451717cadca8Slm66018 /*
451817cadca8Slm66018  * Description:
451917cadca8Slm66018  *	This function checks to see if the virtual device is an ATAPI
452017cadca8Slm66018  *	device. ATAPI devices use Group 1 Read/Write commands, so
452117cadca8Slm66018  *	any USCSI calls vds makes need to take this into account.
452217cadca8Slm66018  *
452317cadca8Slm66018  * Parameters:
452417cadca8Slm66018  *	vd		- disk on which the operation is performed.
452517cadca8Slm66018  *
452617cadca8Slm66018  * Return Code:
452717cadca8Slm66018  *	B_TRUE		- The virtual disk is backed by an ATAPI device
452817cadca8Slm66018  *	B_FALSE		- not an ATAPI device (presumably SCSI)
452917cadca8Slm66018  */
453017cadca8Slm66018 static boolean_t
453117cadca8Slm66018 vd_is_atapi_device(vd_t *vd)
453217cadca8Slm66018 {
453317cadca8Slm66018 	boolean_t	is_atapi = B_FALSE;
453417cadca8Slm66018 	char		*variantp;
453517cadca8Slm66018 	int		rv;
453617cadca8Slm66018 
453717cadca8Slm66018 	ASSERT(vd->ldi_handle[0] != NULL);
453817cadca8Slm66018 	ASSERT(!vd->file);
453917cadca8Slm66018 
454017cadca8Slm66018 	rv = ldi_prop_lookup_string(vd->ldi_handle[0],
454117cadca8Slm66018 	    (LDI_DEV_T_ANY | DDI_PROP_DONTPASS), "variant", &variantp);
454217cadca8Slm66018 	if (rv == DDI_PROP_SUCCESS) {
454317cadca8Slm66018 		PR0("'variant' property exists for %s", vd->device_path);
454417cadca8Slm66018 		if (strcmp(variantp, "atapi") == 0)
454517cadca8Slm66018 			is_atapi = B_TRUE;
454617cadca8Slm66018 		ddi_prop_free(variantp);
454717cadca8Slm66018 	}
454817cadca8Slm66018 
454917cadca8Slm66018 	rv = ldi_prop_exists(vd->ldi_handle[0], LDI_DEV_T_ANY, "atapi");
455017cadca8Slm66018 	if (rv) {
455117cadca8Slm66018 		PR0("'atapi' property exists for %s", vd->device_path);
455217cadca8Slm66018 		is_atapi = B_TRUE;
455317cadca8Slm66018 	}
455417cadca8Slm66018 
455517cadca8Slm66018 	return (is_atapi);
455617cadca8Slm66018 }
455717cadca8Slm66018 
45581ae08745Sheppo static int
45592f5224aeSachartre vd_setup_mediainfo(vd_t *vd)
45600a55fbb7Slm66018 {
45612f5224aeSachartre 	int status, rval;
45624bac2208Snarayan 	struct dk_minfo	dk_minfo;
45630a55fbb7Slm66018 
45642f5224aeSachartre 	ASSERT(vd->ldi_handle[0] != NULL);
45652f5224aeSachartre 	ASSERT(vd->vdisk_block_size != 0);
45662f5224aeSachartre 
45672f5224aeSachartre 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCGMEDIAINFO,
45682f5224aeSachartre 	    (intptr_t)&dk_minfo, (vd->open_flags | FKIOCTL),
45692f5224aeSachartre 	    kcred, &rval)) != 0)
45702f5224aeSachartre 		return (status);
45712f5224aeSachartre 
45722f5224aeSachartre 	ASSERT(dk_minfo.dki_lbsize % vd->vdisk_block_size == 0);
45732f5224aeSachartre 
45742f5224aeSachartre 	vd->block_size = dk_minfo.dki_lbsize;
45752f5224aeSachartre 	vd->vdisk_size = (dk_minfo.dki_capacity * dk_minfo.dki_lbsize) /
45762f5224aeSachartre 	    vd->vdisk_block_size;
45772f5224aeSachartre 	vd->vdisk_media = DK_MEDIATYPE2VD_MEDIATYPE(dk_minfo.dki_media_type);
45782f5224aeSachartre 	return (0);
45792f5224aeSachartre }
45802f5224aeSachartre 
45812f5224aeSachartre static int
45822f5224aeSachartre vd_setup_full_disk(vd_t *vd)
45832f5224aeSachartre {
45842f5224aeSachartre 	int		status;
45852f5224aeSachartre 	major_t		major = getmajor(vd->dev[0]);
45862f5224aeSachartre 	minor_t		minor = getminor(vd->dev[0]) - VD_ENTIRE_DISK_SLICE;
45872f5224aeSachartre 
4588047ba61eSachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
4589047ba61eSachartre 
45902f5224aeSachartre 	vd->vdisk_block_size = DEV_BSIZE;
45912f5224aeSachartre 
45924bac2208Snarayan 	/*
45934bac2208Snarayan 	 * At this point, vdisk_size is set to the size of partition 2 but
45944bac2208Snarayan 	 * this does not represent the size of the disk because partition 2
45954bac2208Snarayan 	 * may not cover the entire disk and its size does not include reserved
45962f5224aeSachartre 	 * blocks. So we call vd_get_mediainfo to udpate this information and
45972f5224aeSachartre 	 * set the block size and the media type of the disk.
45984bac2208Snarayan 	 */
45992f5224aeSachartre 	status = vd_setup_mediainfo(vd);
46002f5224aeSachartre 
46012f5224aeSachartre 	if (status != 0) {
46022f5224aeSachartre 		if (!vd->scsi) {
46032f5224aeSachartre 			/* unexpected failure */
4604690555a1Sachartre 			PRN("ldi_ioctl(DKIOCGMEDIAINFO) returned errno %d",
46054bac2208Snarayan 			    status);
46060a55fbb7Slm66018 			return (status);
46070a55fbb7Slm66018 		}
46082f5224aeSachartre 
46092f5224aeSachartre 		/*
46102f5224aeSachartre 		 * The function can fail for SCSI disks which are present but
46112f5224aeSachartre 		 * reserved by another system. In that case, we don't know the
46122f5224aeSachartre 		 * size of the disk and the block size.
46132f5224aeSachartre 		 */
46142f5224aeSachartre 		vd->vdisk_size = VD_SIZE_UNKNOWN;
46152f5224aeSachartre 		vd->block_size = 0;
46162f5224aeSachartre 		vd->vdisk_media = VD_MEDIA_FIXED;
46172f5224aeSachartre 	}
46180a55fbb7Slm66018 
46190a55fbb7Slm66018 	/* Move dev number and LDI handle to entire-disk-slice array elements */
46200a55fbb7Slm66018 	vd->dev[VD_ENTIRE_DISK_SLICE]		= vd->dev[0];
46210a55fbb7Slm66018 	vd->dev[0]				= 0;
46220a55fbb7Slm66018 	vd->ldi_handle[VD_ENTIRE_DISK_SLICE]	= vd->ldi_handle[0];
46230a55fbb7Slm66018 	vd->ldi_handle[0]			= NULL;
46240a55fbb7Slm66018 
46250a55fbb7Slm66018 	/* Initialize device numbers for remaining slices and open them */
46260a55fbb7Slm66018 	for (int slice = 0; slice < vd->nslices; slice++) {
46270a55fbb7Slm66018 		/*
46280a55fbb7Slm66018 		 * Skip the entire-disk slice, as it's already open and its
46290a55fbb7Slm66018 		 * device known
46300a55fbb7Slm66018 		 */
46310a55fbb7Slm66018 		if (slice == VD_ENTIRE_DISK_SLICE)
46320a55fbb7Slm66018 			continue;
46330a55fbb7Slm66018 		ASSERT(vd->dev[slice] == 0);
46340a55fbb7Slm66018 		ASSERT(vd->ldi_handle[slice] == NULL);
46350a55fbb7Slm66018 
46360a55fbb7Slm66018 		/*
46370a55fbb7Slm66018 		 * Construct the device number for the current slice
46380a55fbb7Slm66018 		 */
46390a55fbb7Slm66018 		vd->dev[slice] = makedevice(major, (minor + slice));
46400a55fbb7Slm66018 
46410a55fbb7Slm66018 		/*
464234683adeSsg70180 		 * Open all slices of the disk to serve them to the client.
464334683adeSsg70180 		 * Slices are opened exclusively to prevent other threads or
464434683adeSsg70180 		 * processes in the service domain from performing I/O to
464534683adeSsg70180 		 * slices being accessed by a client.  Failure to open a slice
464634683adeSsg70180 		 * results in vds not serving this disk, as the client could
464734683adeSsg70180 		 * attempt (and should be able) to access any slice immediately.
464834683adeSsg70180 		 * Any slices successfully opened before a failure will get
464934683adeSsg70180 		 * closed by vds_destroy_vd() as a result of the error returned
465034683adeSsg70180 		 * by this function.
465134683adeSsg70180 		 *
465234683adeSsg70180 		 * We need to do the open with FNDELAY so that opening an empty
465334683adeSsg70180 		 * slice does not fail.
46540a55fbb7Slm66018 		 */
46550a55fbb7Slm66018 		PR0("Opening device major %u, minor %u = slice %u",
46560a55fbb7Slm66018 		    major, minor, slice);
4657047ba61eSachartre 
4658047ba61eSachartre 		/*
4659047ba61eSachartre 		 * Try to open the device. This can fail for example if we are
4660047ba61eSachartre 		 * opening an empty slice. So in case of a failure, we try the
4661047ba61eSachartre 		 * open again but this time with the FNDELAY flag.
4662047ba61eSachartre 		 */
4663047ba61eSachartre 		status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK,
4664047ba61eSachartre 		    vd->open_flags, kcred, &vd->ldi_handle[slice],
4665047ba61eSachartre 		    vd->vds->ldi_ident);
4666047ba61eSachartre 
4667047ba61eSachartre 		if (status != 0) {
4668047ba61eSachartre 			status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK,
4669047ba61eSachartre 			    vd->open_flags | FNDELAY, kcred,
4670047ba61eSachartre 			    &vd->ldi_handle[slice], vd->vds->ldi_ident);
4671047ba61eSachartre 		}
4672047ba61eSachartre 
4673047ba61eSachartre 		if (status != 0) {
4674690555a1Sachartre 			PRN("ldi_open_by_dev() returned errno %d "
46750a55fbb7Slm66018 			    "for slice %u", status, slice);
46760a55fbb7Slm66018 			/* vds_destroy_vd() will close any open slices */
4677690555a1Sachartre 			vd->ldi_handle[slice] = NULL;
46780a55fbb7Slm66018 			return (status);
46790a55fbb7Slm66018 		}
46800a55fbb7Slm66018 	}
46810a55fbb7Slm66018 
46820a55fbb7Slm66018 	return (0);
46830a55fbb7Slm66018 }
46840a55fbb7Slm66018 
4685edcc0754Sachartre /*
4686edcc0754Sachartre  * When a slice or a volume is exported as a single-slice disk, we want
4687edcc0754Sachartre  * the disk backend (i.e. the slice or volume) to be entirely mapped as
4688edcc0754Sachartre  * a slice without the addition of any metadata.
4689edcc0754Sachartre  *
4690edcc0754Sachartre  * So when exporting the disk as a VTOC disk, we fake a disk with the following
4691edcc0754Sachartre  * layout:
4692edcc0754Sachartre  *
4693edcc0754Sachartre  *                 0 1                         N+1
4694edcc0754Sachartre  *                 +-+--------------------------+
4695edcc0754Sachartre  *  virtual disk:  |L|           slice 0        |
4696edcc0754Sachartre  *                 +-+--------------------------+
4697edcc0754Sachartre  *                  ^:                          :
4698edcc0754Sachartre  *                  |:                          :
4699edcc0754Sachartre  *      VTOC LABEL--+:                          :
4700edcc0754Sachartre  *                   +--------------------------+
4701edcc0754Sachartre  *  disk backend:    |       slice/volume       |
4702edcc0754Sachartre  *                   +--------------------------+
4703edcc0754Sachartre  *                   0                          N
4704edcc0754Sachartre  *
4705edcc0754Sachartre  * N is the number of blocks in the slice/volume.
4706edcc0754Sachartre  *
4707edcc0754Sachartre  * We simulate a disk with N+1 blocks. The first block (block 0) is faked and
4708edcc0754Sachartre  * can not be changed. The remaining blocks (1 to N+1) defines slice 0 and are
4709edcc0754Sachartre  * mapped to the exported slice or volume:
4710edcc0754Sachartre  *
4711edcc0754Sachartre  * - block 0 (L) can return a fake VTOC label if raw read was implemented.
4712edcc0754Sachartre  * - block 1 to N+1 is mapped to the exported slice or volume.
4713edcc0754Sachartre  *
4714edcc0754Sachartre  */
47150a55fbb7Slm66018 static int
471678fcd0a1Sachartre vd_setup_partition_vtoc(vd_t *vd)
471778fcd0a1Sachartre {
471878fcd0a1Sachartre 	int rval, status;
471978fcd0a1Sachartre 	char *device_path = vd->device_path;
472078fcd0a1Sachartre 
472178fcd0a1Sachartre 	status = ldi_ioctl(vd->ldi_handle[0], DKIOCGGEOM,
4722047ba61eSachartre 	    (intptr_t)&vd->dk_geom, (vd->open_flags | FKIOCTL), kcred, &rval);
472378fcd0a1Sachartre 
472478fcd0a1Sachartre 	if (status != 0) {
472578fcd0a1Sachartre 		PRN("ldi_ioctl(DKIOCGEOM) returned errno %d for %s",
472678fcd0a1Sachartre 		    status, device_path);
472778fcd0a1Sachartre 		return (status);
472878fcd0a1Sachartre 	}
472978fcd0a1Sachartre 
473078fcd0a1Sachartre 	/* Initialize dk_geom structure for single-slice device */
473178fcd0a1Sachartre 	if (vd->dk_geom.dkg_nsect == 0) {
473278fcd0a1Sachartre 		PRN("%s geometry claims 0 sectors per track", device_path);
473378fcd0a1Sachartre 		return (EIO);
473478fcd0a1Sachartre 	}
473578fcd0a1Sachartre 	if (vd->dk_geom.dkg_nhead == 0) {
473678fcd0a1Sachartre 		PRN("%s geometry claims 0 heads", device_path);
473778fcd0a1Sachartre 		return (EIO);
473878fcd0a1Sachartre 	}
4739edcc0754Sachartre 	vd->dk_geom.dkg_ncyl = (vd->vdisk_size + 1) / vd->dk_geom.dkg_nsect /
474078fcd0a1Sachartre 	    vd->dk_geom.dkg_nhead;
474178fcd0a1Sachartre 	vd->dk_geom.dkg_acyl = 0;
474278fcd0a1Sachartre 	vd->dk_geom.dkg_pcyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl;
474378fcd0a1Sachartre 
474478fcd0a1Sachartre 
474578fcd0a1Sachartre 	/* Initialize vtoc structure for single-slice device */
474678fcd0a1Sachartre 	bcopy(VD_VOLUME_NAME, vd->vtoc.v_volume,
474778fcd0a1Sachartre 	    MIN(sizeof (VD_VOLUME_NAME), sizeof (vd->vtoc.v_volume)));
474878fcd0a1Sachartre 	bzero(vd->vtoc.v_part, sizeof (vd->vtoc.v_part));
474978fcd0a1Sachartre 	vd->vtoc.v_nparts = 1;
475078fcd0a1Sachartre 	vd->vtoc.v_part[0].p_tag = V_UNASSIGNED;
475178fcd0a1Sachartre 	vd->vtoc.v_part[0].p_flag = 0;
4752edcc0754Sachartre 	vd->vtoc.v_part[0].p_start = 1;
475378fcd0a1Sachartre 	vd->vtoc.v_part[0].p_size = vd->vdisk_size;
475478fcd0a1Sachartre 	bcopy(VD_ASCIILABEL, vd->vtoc.v_asciilabel,
475578fcd0a1Sachartre 	    MIN(sizeof (VD_ASCIILABEL), sizeof (vd->vtoc.v_asciilabel)));
475678fcd0a1Sachartre 
4757edcc0754Sachartre 	/* adjust the vdisk_size, we emulate the first block */
4758edcc0754Sachartre 	vd->vdisk_size += 1;
4759edcc0754Sachartre 
476078fcd0a1Sachartre 	return (0);
476178fcd0a1Sachartre }
476278fcd0a1Sachartre 
4763edcc0754Sachartre /*
4764edcc0754Sachartre  * When a slice, volume or file is exported as a single-slice disk, we want
4765edcc0754Sachartre  * the disk backend (i.e. the slice, volume or file) to be entirely mapped
4766edcc0754Sachartre  * as a slice without the addition of any metadata.
4767edcc0754Sachartre  *
4768edcc0754Sachartre  * So when exporting the disk as an EFI disk, we fake a disk with the following
4769edcc0754Sachartre  * layout:
4770edcc0754Sachartre  *
4771edcc0754Sachartre  *                 0 1 2 3      34                        34+N
4772edcc0754Sachartre  *                 +-+-+-+-------+--------------------------+
4773edcc0754Sachartre  *  virtual disk:  |X|T|E|XXXXXXX|           slice 0        |
4774edcc0754Sachartre  *                 +-+-+-+-------+--------------------------+
4775edcc0754Sachartre  *                    ^ ^        :                          :
4776edcc0754Sachartre  *                    | |        :                          :
4777edcc0754Sachartre  *                GPT-+ +-GPE    :                          :
4778edcc0754Sachartre  *                               +--------------------------+
4779edcc0754Sachartre  *  disk backend:                |     slice/volume/file    |
4780edcc0754Sachartre  *                               +--------------------------+
4781edcc0754Sachartre  *                               0                          N
4782edcc0754Sachartre  *
4783edcc0754Sachartre  * N is the number of blocks in the slice/volume/file.
4784edcc0754Sachartre  *
4785edcc0754Sachartre  * We simulate a disk with 34+N blocks. The first 34 blocks (0 to 33) are
4786edcc0754Sachartre  * emulated and can not be changed. The remaining blocks (34 to 34+N) defines
4787edcc0754Sachartre  * slice 0 and are mapped to the exported slice, volume or file:
4788edcc0754Sachartre  *
4789edcc0754Sachartre  * - block 0 (X) is unused and can return 0 if raw read was implemented.
4790edcc0754Sachartre  * - block 1 (T) returns a fake EFI GPT (via DKIOCGETEFI)
4791edcc0754Sachartre  * - block 2 (E) returns a fake EFI GPE (via DKIOCGETEFI)
4792edcc0754Sachartre  * - block 3 to 33 (X) are unused and return 0 if raw read is implemented.
4793edcc0754Sachartre  * - block 34 to 34+N is mapped to the exported slice, volume or file.
4794edcc0754Sachartre  *
4795edcc0754Sachartre  */
479678fcd0a1Sachartre static int
47974bac2208Snarayan vd_setup_partition_efi(vd_t *vd)
47984bac2208Snarayan {
47994bac2208Snarayan 	efi_gpt_t *gpt;
48004bac2208Snarayan 	efi_gpe_t *gpe;
4801edcc0754Sachartre 	struct uuid uuid = EFI_USR;
48024bac2208Snarayan 	uint32_t crc;
48034bac2208Snarayan 
4804edcc0754Sachartre 	gpt = &vd->efi_gpt;
4805edcc0754Sachartre 	gpe = &vd->efi_gpe;
48064bac2208Snarayan 
4807edcc0754Sachartre 	bzero(gpt, sizeof (efi_gpt_t));
4808edcc0754Sachartre 	bzero(gpe, sizeof (efi_gpe_t));
4809edcc0754Sachartre 
4810edcc0754Sachartre 	/* adjust the vdisk_size, we emulate the first 34 blocks */
4811edcc0754Sachartre 	vd->vdisk_size += 34;
48124bac2208Snarayan 
48134bac2208Snarayan 	gpt->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
48144bac2208Snarayan 	gpt->efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
48154bac2208Snarayan 	gpt->efi_gpt_HeaderSize = LE_32(sizeof (efi_gpt_t));
4816edcc0754Sachartre 	gpt->efi_gpt_FirstUsableLBA = LE_64(34ULL);
48174bac2208Snarayan 	gpt->efi_gpt_LastUsableLBA = LE_64(vd->vdisk_size - 1);
48184bac2208Snarayan 	gpt->efi_gpt_NumberOfPartitionEntries = LE_32(1);
4819edcc0754Sachartre 	gpt->efi_gpt_PartitionEntryLBA = LE_64(2ULL);
48204bac2208Snarayan 	gpt->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (efi_gpe_t));
48214bac2208Snarayan 
48224bac2208Snarayan 	UUID_LE_CONVERT(gpe->efi_gpe_PartitionTypeGUID, uuid);
48234bac2208Snarayan 	gpe->efi_gpe_StartingLBA = gpt->efi_gpt_FirstUsableLBA;
48244bac2208Snarayan 	gpe->efi_gpe_EndingLBA = gpt->efi_gpt_LastUsableLBA;
48254bac2208Snarayan 
48264bac2208Snarayan 	CRC32(crc, gpe, sizeof (efi_gpe_t), -1U, crc32_table);
48274bac2208Snarayan 	gpt->efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
48284bac2208Snarayan 
48294bac2208Snarayan 	CRC32(crc, gpt, sizeof (efi_gpt_t), -1U, crc32_table);
48304bac2208Snarayan 	gpt->efi_gpt_HeaderCRC32 = LE_32(~crc);
48314bac2208Snarayan 
48324bac2208Snarayan 	return (0);
48334bac2208Snarayan }
48344bac2208Snarayan 
4835047ba61eSachartre /*
4836047ba61eSachartre  * Setup for a virtual disk whose backend is a file (exported as a single slice
4837*8fce2fd6Sachartre  * or as a full disk) or a volume device (for example a ZFS, SVM or VxVM volume)
4838047ba61eSachartre  * exported as a full disk. In these cases, the backend is accessed using the
4839047ba61eSachartre  * vnode interface.
4840047ba61eSachartre  */
48414bac2208Snarayan static int
4842047ba61eSachartre vd_setup_backend_vnode(vd_t *vd)
48433c96341aSnarayan {
484478fcd0a1Sachartre 	int 		rval, status;
48453c96341aSnarayan 	vattr_t		vattr;
48463c96341aSnarayan 	dev_t		dev;
48473c96341aSnarayan 	char		*file_path = vd->device_path;
48483c96341aSnarayan 	char		dev_path[MAXPATHLEN + 1];
48493c96341aSnarayan 	ldi_handle_t	lhandle;
48503c96341aSnarayan 	struct dk_cinfo	dk_cinfo;
48513c96341aSnarayan 
4852047ba61eSachartre 	if ((status = vn_open(file_path, UIO_SYSSPACE, vd->open_flags | FOFFMAX,
48533c96341aSnarayan 	    0, &vd->file_vnode, 0, 0)) != 0) {
4854690555a1Sachartre 		PRN("vn_open(%s) = errno %d", file_path, status);
48553c96341aSnarayan 		return (status);
48563c96341aSnarayan 	}
48573c96341aSnarayan 
4858690555a1Sachartre 	/*
4859690555a1Sachartre 	 * We set vd->file now so that vds_destroy_vd will take care of
4860690555a1Sachartre 	 * closing the file and releasing the vnode in case of an error.
4861690555a1Sachartre 	 */
4862690555a1Sachartre 	vd->file = B_TRUE;
4863690555a1Sachartre 
48643c96341aSnarayan 	vattr.va_mask = AT_SIZE;
4865da6c28aaSamw 	if ((status = VOP_GETATTR(vd->file_vnode, &vattr, 0, kcred, NULL))
4866da6c28aaSamw 	    != 0) {
4867690555a1Sachartre 		PRN("VOP_GETATTR(%s) = errno %d", file_path, status);
48683c96341aSnarayan 		return (EIO);
48693c96341aSnarayan 	}
48703c96341aSnarayan 
48713c96341aSnarayan 	vd->file_size = vattr.va_size;
48723c96341aSnarayan 	/* size should be at least sizeof(dk_label) */
48733c96341aSnarayan 	if (vd->file_size < sizeof (struct dk_label)) {
48743c96341aSnarayan 		PRN("Size of file has to be at least %ld bytes",
48753c96341aSnarayan 		    sizeof (struct dk_label));
48763c96341aSnarayan 		return (EIO);
48773c96341aSnarayan 	}
48783c96341aSnarayan 
4879690555a1Sachartre 	if (vd->file_vnode->v_flag & VNOMAP) {
4880690555a1Sachartre 		PRN("File %s cannot be mapped", file_path);
48813c96341aSnarayan 		return (EIO);
48823c96341aSnarayan 	}
48833c96341aSnarayan 
48843c96341aSnarayan 	/* sector size = block size = DEV_BSIZE */
488517cadca8Slm66018 	vd->block_size = DEV_BSIZE;
488617cadca8Slm66018 	vd->vdisk_block_size = DEV_BSIZE;
488787a7269eSachartre 	vd->vdisk_size = vd->file_size / DEV_BSIZE;
48883c96341aSnarayan 	vd->max_xfer_sz = maxphys / DEV_BSIZE; /* default transfer size */
48893c96341aSnarayan 
4890047ba61eSachartre 	/*
4891047ba61eSachartre 	 * Get max_xfer_sz from the device where the file is or from the device
4892*8fce2fd6Sachartre 	 * itself if we have a volume device.
4893047ba61eSachartre 	 */
4894047ba61eSachartre 	dev_path[0] = '\0';
4895047ba61eSachartre 
4896*8fce2fd6Sachartre 	if (vd->volume) {
4897047ba61eSachartre 		status = ldi_open_by_name(file_path, FREAD, kcred, &lhandle,
4898047ba61eSachartre 		    vd->vds->ldi_ident);
4899047ba61eSachartre 	} else {
49003c96341aSnarayan 		dev = vd->file_vnode->v_vfsp->vfs_dev;
49013c96341aSnarayan 		if (ddi_dev_pathname(dev, S_IFBLK, dev_path) == DDI_SUCCESS) {
49023c96341aSnarayan 			PR0("underlying device = %s\n", dev_path);
49033c96341aSnarayan 		}
49043c96341aSnarayan 
4905047ba61eSachartre 		status = ldi_open_by_dev(&dev, OTYP_BLK, FREAD, kcred, &lhandle,
4906047ba61eSachartre 		    vd->vds->ldi_ident);
4907047ba61eSachartre 	}
4908047ba61eSachartre 
4909047ba61eSachartre 	if (status != 0) {
4910047ba61eSachartre 		PR0("ldi_open() returned errno %d for device %s",
4911047ba61eSachartre 		    status, (dev_path[0] == '\0')? file_path : dev_path);
49123c96341aSnarayan 	} else {
49133c96341aSnarayan 		if ((status = ldi_ioctl(lhandle, DKIOCINFO,
4914047ba61eSachartre 		    (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred,
49153c96341aSnarayan 		    &rval)) != 0) {
49163c96341aSnarayan 			PR0("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
49173c96341aSnarayan 			    status, dev_path);
49183c96341aSnarayan 		} else {
49193c96341aSnarayan 			/*
49203c96341aSnarayan 			 * Store the device's max transfer size for
49213c96341aSnarayan 			 * return to the client
49223c96341aSnarayan 			 */
49233c96341aSnarayan 			vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
49243c96341aSnarayan 		}
49253c96341aSnarayan 
49263c96341aSnarayan 		PR0("close the device %s", dev_path);
49273c96341aSnarayan 		(void) ldi_close(lhandle, FREAD, kcred);
49283c96341aSnarayan 	}
49293c96341aSnarayan 
4930205eeb1aSlm66018 	PR0("using file %s, dev %s, max_xfer = %u blks",
49313c96341aSnarayan 	    file_path, dev_path, vd->max_xfer_sz);
49323c96341aSnarayan 
4933edcc0754Sachartre 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
4934*8fce2fd6Sachartre 		ASSERT(!vd->volume);
4935edcc0754Sachartre 		vd->vdisk_label = VD_DISK_LABEL_EFI;
4936edcc0754Sachartre 		status = vd_setup_partition_efi(vd);
4937047ba61eSachartre 		return (0);
4938edcc0754Sachartre 	}
4939edcc0754Sachartre 
4940edcc0754Sachartre 	/*
4941edcc0754Sachartre 	 * Find and validate the geometry of a disk image.
4942edcc0754Sachartre 	 */
4943edcc0754Sachartre 	status = vd_file_validate_geometry(vd);
4944edcc0754Sachartre 	if (status != 0 && status != EINVAL && status != ENOTSUP) {
4945edcc0754Sachartre 		PRN("Failed to read label from %s", file_path);
4946edcc0754Sachartre 		return (EIO);
4947edcc0754Sachartre 	}
4948edcc0754Sachartre 
4949edcc0754Sachartre 	if (vd_file_is_iso_image(vd)) {
4950edcc0754Sachartre 		/*
4951edcc0754Sachartre 		 * Indicate whether to call this a CD or DVD from the size
4952edcc0754Sachartre 		 * of the ISO image (images for both drive types are stored
4953edcc0754Sachartre 		 * in the ISO-9600 format). CDs can store up to just under 1Gb
4954edcc0754Sachartre 		 */
4955edcc0754Sachartre 		if ((vd->vdisk_size * vd->vdisk_block_size) >
4956edcc0754Sachartre 		    (1024 * 1024 * 1024))
4957edcc0754Sachartre 			vd->vdisk_media = VD_MEDIA_DVD;
4958edcc0754Sachartre 		else
4959edcc0754Sachartre 			vd->vdisk_media = VD_MEDIA_CD;
4960edcc0754Sachartre 	} else {
4961edcc0754Sachartre 		vd->vdisk_media = VD_MEDIA_FIXED;
4962edcc0754Sachartre 	}
4963edcc0754Sachartre 
4964edcc0754Sachartre 	/* Setup devid for the disk image */
4965047ba61eSachartre 
496678fcd0a1Sachartre 	if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
496778fcd0a1Sachartre 
496887a7269eSachartre 		status = vd_file_read_devid(vd, &vd->file_devid);
496987a7269eSachartre 
497087a7269eSachartre 		if (status == 0) {
497187a7269eSachartre 			/* a valid devid was found */
497287a7269eSachartre 			return (0);
497387a7269eSachartre 		}
497487a7269eSachartre 
497587a7269eSachartre 		if (status != EINVAL) {
497687a7269eSachartre 			/*
497778fcd0a1Sachartre 			 * There was an error while trying to read the devid.
497878fcd0a1Sachartre 			 * So this disk image may have a devid but we are
497978fcd0a1Sachartre 			 * unable to read it.
498087a7269eSachartre 			 */
498187a7269eSachartre 			PR0("can not read devid for %s", file_path);
498287a7269eSachartre 			vd->file_devid = NULL;
498387a7269eSachartre 			return (0);
498487a7269eSachartre 		}
498578fcd0a1Sachartre 	}
498687a7269eSachartre 
498787a7269eSachartre 	/*
498887a7269eSachartre 	 * No valid device id was found so we create one. Note that a failure
498987a7269eSachartre 	 * to create a device id is not fatal and does not prevent the disk
499087a7269eSachartre 	 * image from being attached.
499187a7269eSachartre 	 */
499287a7269eSachartre 	PR1("creating devid for %s", file_path);
499387a7269eSachartre 
499487a7269eSachartre 	if (ddi_devid_init(vd->vds->dip, DEVID_FAB, NULL, 0,
499587a7269eSachartre 	    &vd->file_devid) != DDI_SUCCESS) {
499687a7269eSachartre 		PR0("fail to create devid for %s", file_path);
499787a7269eSachartre 		vd->file_devid = NULL;
499887a7269eSachartre 		return (0);
499987a7269eSachartre 	}
500087a7269eSachartre 
500178fcd0a1Sachartre 	/*
500278fcd0a1Sachartre 	 * Write devid to the disk image. The devid is stored into the disk
500378fcd0a1Sachartre 	 * image if we have a valid label; otherwise the devid will be stored
500478fcd0a1Sachartre 	 * when the user writes a valid label.
500578fcd0a1Sachartre 	 */
500678fcd0a1Sachartre 	if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
500787a7269eSachartre 		if (vd_file_write_devid(vd, vd->file_devid) != 0) {
500887a7269eSachartre 			PR0("fail to write devid for %s", file_path);
500987a7269eSachartre 			ddi_devid_free(vd->file_devid);
501087a7269eSachartre 			vd->file_devid = NULL;
501187a7269eSachartre 		}
501278fcd0a1Sachartre 	}
501387a7269eSachartre 
50143c96341aSnarayan 	return (0);
50153c96341aSnarayan }
50163c96341aSnarayan 
501717cadca8Slm66018 
501817cadca8Slm66018 /*
501917cadca8Slm66018  * Description:
502017cadca8Slm66018  *	Open a device using its device path (supplied by ldm(1m))
502117cadca8Slm66018  *
502217cadca8Slm66018  * Parameters:
502317cadca8Slm66018  *	vd 	- pointer to structure containing the vDisk info
5024*8fce2fd6Sachartre  *	flags	- open flags
502517cadca8Slm66018  *
502617cadca8Slm66018  * Return Value
502717cadca8Slm66018  *	0	- success
502817cadca8Slm66018  *	!= 0	- some other non-zero return value from ldi(9F) functions
502917cadca8Slm66018  */
503017cadca8Slm66018 static int
5031*8fce2fd6Sachartre vd_open_using_ldi_by_name(vd_t *vd, int flags)
503217cadca8Slm66018 {
5033*8fce2fd6Sachartre 	int		status;
503417cadca8Slm66018 	char		*device_path = vd->device_path;
503517cadca8Slm66018 
5036*8fce2fd6Sachartre 	/* Attempt to open device */
5037*8fce2fd6Sachartre 	status = ldi_open_by_name(device_path, flags, kcred,
503817cadca8Slm66018 	    &vd->ldi_handle[0], vd->vds->ldi_ident);
503917cadca8Slm66018 
504017cadca8Slm66018 	/*
504117cadca8Slm66018 	 * The open can fail for example if we are opening an empty slice.
504217cadca8Slm66018 	 * In case of a failure, we try the open again but this time with
504317cadca8Slm66018 	 * the FNDELAY flag.
504417cadca8Slm66018 	 */
504517cadca8Slm66018 	if (status != 0)
5046*8fce2fd6Sachartre 		status = ldi_open_by_name(device_path, flags | FNDELAY,
504717cadca8Slm66018 		    kcred, &vd->ldi_handle[0], vd->vds->ldi_ident);
504817cadca8Slm66018 
504917cadca8Slm66018 	if (status != 0) {
505017cadca8Slm66018 		PR0("ldi_open_by_name(%s) = errno %d", device_path, status);
505117cadca8Slm66018 		vd->ldi_handle[0] = NULL;
505217cadca8Slm66018 		return (status);
505317cadca8Slm66018 	}
505417cadca8Slm66018 
505517cadca8Slm66018 	return (0);
505617cadca8Slm66018 }
505717cadca8Slm66018 
5058047ba61eSachartre /*
5059047ba61eSachartre  * Setup for a virtual disk which backend is a device (a physical disk,
5060*8fce2fd6Sachartre  * slice or volume device) that is directly exported either as a full disk
5061*8fce2fd6Sachartre  * for a physical disk or as a slice for a volume device or a disk slice.
5062047ba61eSachartre  * In these cases, the backend is accessed using the LDI interface.
5063047ba61eSachartre  */
50643c96341aSnarayan static int
5065047ba61eSachartre vd_setup_backend_ldi(vd_t *vd)
50661ae08745Sheppo {
5067e1ebb9ecSlm66018 	int		rval, status;
50681ae08745Sheppo 	struct dk_cinfo	dk_cinfo;
50693c96341aSnarayan 	char		*device_path = vd->device_path;
50701ae08745Sheppo 
5071*8fce2fd6Sachartre 	/* device has been opened by vd_identify_dev() */
5072*8fce2fd6Sachartre 	ASSERT(vd->ldi_handle[0] != NULL);
5073*8fce2fd6Sachartre 	ASSERT(vd->dev[0] != NULL);
50740a55fbb7Slm66018 
50753c96341aSnarayan 	vd->file = B_FALSE;
50764bac2208Snarayan 
507778fcd0a1Sachartre 	/* Verify backing device supports dk_cinfo */
5078e1ebb9ecSlm66018 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO,
5079047ba61eSachartre 	    (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred,
5080e1ebb9ecSlm66018 	    &rval)) != 0) {
5081e1ebb9ecSlm66018 		PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
5082e1ebb9ecSlm66018 		    status, device_path);
5083e1ebb9ecSlm66018 		return (status);
5084e1ebb9ecSlm66018 	}
5085e1ebb9ecSlm66018 	if (dk_cinfo.dki_partition >= V_NUMPAR) {
5086e1ebb9ecSlm66018 		PRN("slice %u >= maximum slice %u for %s",
5087e1ebb9ecSlm66018 		    dk_cinfo.dki_partition, V_NUMPAR, device_path);
5088e1ebb9ecSlm66018 		return (EIO);
5089e1ebb9ecSlm66018 	}
50904bac2208Snarayan 
5091*8fce2fd6Sachartre 	/*
5092*8fce2fd6Sachartre 	 * The device has been opened read-only by vd_identify_dev(), re-open
5093*8fce2fd6Sachartre 	 * it read-write if the write flag is set and we don't have an optical
5094*8fce2fd6Sachartre 	 * device such as a CD-ROM, which, for now, we do not permit writes to
5095*8fce2fd6Sachartre 	 * and thus should not export write operations to the client.
5096*8fce2fd6Sachartre 	 *
5097*8fce2fd6Sachartre 	 * Future: if/when we implement support for guest domains writing to
5098*8fce2fd6Sachartre 	 * optical devices we will need to do further checking of the media type
5099*8fce2fd6Sachartre 	 * to distinguish between read-only and writable discs.
5100*8fce2fd6Sachartre 	 */
5101*8fce2fd6Sachartre 	if (dk_cinfo.dki_ctype == DKC_CDROM) {
5102*8fce2fd6Sachartre 
5103*8fce2fd6Sachartre 		vd->open_flags &= ~FWRITE;
5104*8fce2fd6Sachartre 
5105*8fce2fd6Sachartre 	} else if (vd->open_flags & FWRITE) {
5106*8fce2fd6Sachartre 
5107*8fce2fd6Sachartre 		(void) ldi_close(vd->ldi_handle[0], vd->open_flags & ~FWRITE,
5108*8fce2fd6Sachartre 		    kcred);
5109*8fce2fd6Sachartre 		status = vd_open_using_ldi_by_name(vd, vd->open_flags);
5110*8fce2fd6Sachartre 		if (status != 0) {
5111*8fce2fd6Sachartre 			PR0("Failed to open (%s) = errno %d",
5112*8fce2fd6Sachartre 			    device_path, status);
5113*8fce2fd6Sachartre 			return (status);
5114*8fce2fd6Sachartre 		}
5115*8fce2fd6Sachartre 	}
5116*8fce2fd6Sachartre 
5117e1ebb9ecSlm66018 	/* Store the device's max transfer size for return to the client */
5118e1ebb9ecSlm66018 	vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
5119e1ebb9ecSlm66018 
5120047ba61eSachartre 	/*
512117cadca8Slm66018 	 * We need to work out if it's an ATAPI (IDE CD-ROM) or SCSI device so
512217cadca8Slm66018 	 * that we can use the correct CDB group when sending USCSI commands.
512317cadca8Slm66018 	 */
512417cadca8Slm66018 	vd->is_atapi_dev = vd_is_atapi_device(vd);
512517cadca8Slm66018 
512617cadca8Slm66018 	/*
5127047ba61eSachartre 	 * Export a full disk.
5128047ba61eSachartre 	 *
5129047ba61eSachartre 	 * When we use the LDI interface, we export a device as a full disk
5130047ba61eSachartre 	 * if we have an entire disk slice (slice 2) and if this slice is
5131047ba61eSachartre 	 * exported as a full disk and not as a single slice disk.
513217cadca8Slm66018 	 * Similarly, we want to use LDI if we are accessing a CD or DVD
513317cadca8Slm66018 	 * device (even if it isn't s2)
5134047ba61eSachartre 	 *
5135*8fce2fd6Sachartre 	 * Note that volume devices are exported as full disks using the vnode
5136047ba61eSachartre 	 * interface, not the LDI interface.
5137047ba61eSachartre 	 */
513817cadca8Slm66018 	if ((dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE &&
513917cadca8Slm66018 	    vd->vdisk_type == VD_DISK_TYPE_DISK) ||
514017cadca8Slm66018 	    dk_cinfo.dki_ctype == DKC_CDROM) {
5141*8fce2fd6Sachartre 		ASSERT(!vd->volume);
51422f5224aeSachartre 		if (dk_cinfo.dki_ctype == DKC_SCSI_CCS)
51432f5224aeSachartre 			vd->scsi = B_TRUE;
5144047ba61eSachartre 		return (vd_setup_full_disk(vd));
5145047ba61eSachartre 	}
5146047ba61eSachartre 
5147047ba61eSachartre 	/*
5148047ba61eSachartre 	 * Export a single slice disk.
5149047ba61eSachartre 	 *
5150*8fce2fd6Sachartre 	 * The exported device can be either a volume device or a disk slice. If
5151047ba61eSachartre 	 * it is a disk slice different from slice 2 then it is always exported
5152047ba61eSachartre 	 * as a single slice disk even if the "slice" option is not specified.
5153*8fce2fd6Sachartre 	 * If it is disk slice 2 or a volume device then it is exported as a
5154047ba61eSachartre 	 * single slice disk only if the "slice" option is specified.
5155047ba61eSachartre 	 */
5156047ba61eSachartre 	return (vd_setup_single_slice_disk(vd));
5157047ba61eSachartre }
5158047ba61eSachartre 
5159047ba61eSachartre static int
5160047ba61eSachartre vd_setup_single_slice_disk(vd_t *vd)
5161047ba61eSachartre {
5162edcc0754Sachartre 	int status, rval;
5163047ba61eSachartre 	char *device_path = vd->device_path;
5164047ba61eSachartre 
5165047ba61eSachartre 	/* Get size of backing device */
5166047ba61eSachartre 	if (ldi_get_size(vd->ldi_handle[0], &vd->vdisk_size) != DDI_SUCCESS) {
5167047ba61eSachartre 		PRN("ldi_get_size() failed for %s", device_path);
51681ae08745Sheppo 		return (EIO);
51691ae08745Sheppo 	}
5170047ba61eSachartre 	vd->vdisk_size = lbtodb(vd->vdisk_size);	/* convert to blocks */
517117cadca8Slm66018 	vd->block_size = DEV_BSIZE;
517217cadca8Slm66018 	vd->vdisk_block_size = DEV_BSIZE;
517317cadca8Slm66018 	vd->vdisk_media = VD_MEDIA_FIXED;
5174047ba61eSachartre 
5175*8fce2fd6Sachartre 	if (vd->volume) {
5176047ba61eSachartre 		ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
517778fcd0a1Sachartre 	}
51780a55fbb7Slm66018 
5179047ba61eSachartre 	/*
5180047ba61eSachartre 	 * We export the slice as a single slice disk even if the "slice"
5181047ba61eSachartre 	 * option was not specified.
5182047ba61eSachartre 	 */
51831ae08745Sheppo 	vd->vdisk_type  = VD_DISK_TYPE_SLICE;
51841ae08745Sheppo 	vd->nslices	= 1;
51851ae08745Sheppo 
5186edcc0754Sachartre 	/*
5187edcc0754Sachartre 	 * When exporting a slice or a device as a single slice disk, we don't
5188edcc0754Sachartre 	 * care about any partitioning exposed by the backend. The goal is just
5189edcc0754Sachartre 	 * to export the backend as a flat storage. We provide a fake partition
5190edcc0754Sachartre 	 * table (either a VTOC or EFI), which presents only one slice, to
5191edcc0754Sachartre 	 * accommodate tools expecting a disk label.
5192edcc0754Sachartre 	 *
5193edcc0754Sachartre 	 * We check the label of the backend to export the device as a slice
5194edcc0754Sachartre 	 * using the same type of label (VTOC or EFI). If there is no label
5195edcc0754Sachartre 	 * then we create a fake EFI label.
5196edcc0754Sachartre 	 *
5197edcc0754Sachartre 	 * Note that the partition table we are creating could also be faked
5198edcc0754Sachartre 	 * by the client based on the size of the backend device.
5199edcc0754Sachartre 	 */
5200edcc0754Sachartre 	status = ldi_ioctl(vd->ldi_handle[0], DKIOCGVTOC, (intptr_t)&vd->vtoc,
5201edcc0754Sachartre 	    (vd->open_flags | FKIOCTL), kcred, &rval);
5202edcc0754Sachartre 
5203edcc0754Sachartre 	if (status == 0) {
5204edcc0754Sachartre 		/* export with a fake VTOC label */
5205edcc0754Sachartre 		vd->vdisk_label = VD_DISK_LABEL_VTOC;
520678fcd0a1Sachartre 		status = vd_setup_partition_vtoc(vd);
5207edcc0754Sachartre 	} else {
5208edcc0754Sachartre 		/* export with a fake EFI label */
5209edcc0754Sachartre 		vd->vdisk_label = VD_DISK_LABEL_EFI;
5210edcc0754Sachartre 		status = vd_setup_partition_efi(vd);
521178fcd0a1Sachartre 	}
521278fcd0a1Sachartre 
52134bac2208Snarayan 	return (status);
52144bac2208Snarayan }
52151ae08745Sheppo 
5216*8fce2fd6Sachartre /*
5217*8fce2fd6Sachartre  * Description:
5218*8fce2fd6Sachartre  *	Open a device using its device path and identify if this is
5219*8fce2fd6Sachartre  *	a disk device or a volume device.
5220*8fce2fd6Sachartre  *
5221*8fce2fd6Sachartre  * Parameters:
5222*8fce2fd6Sachartre  *	vd 	- pointer to structure containing the vDisk info
5223*8fce2fd6Sachartre  *	dtype	- return the driver type of the device
5224*8fce2fd6Sachartre  *
5225*8fce2fd6Sachartre  * Return Value
5226*8fce2fd6Sachartre  *	0	- success
5227*8fce2fd6Sachartre  *	!= 0	- some other non-zero return value from ldi(9F) functions
5228*8fce2fd6Sachartre  */
5229*8fce2fd6Sachartre static int
5230*8fce2fd6Sachartre vd_identify_dev(vd_t *vd, int *dtype)
5231*8fce2fd6Sachartre {
5232*8fce2fd6Sachartre 	int status, i;
5233*8fce2fd6Sachartre 	char *device_path = vd->device_path;
5234*8fce2fd6Sachartre 	char *drv_name;
5235*8fce2fd6Sachartre 	int drv_type;
5236*8fce2fd6Sachartre 	vds_t *vds = vd->vds;
5237*8fce2fd6Sachartre 
5238*8fce2fd6Sachartre 	status = vd_open_using_ldi_by_name(vd, vd->open_flags & ~FWRITE);
5239*8fce2fd6Sachartre 	if (status != 0) {
5240*8fce2fd6Sachartre 		PR0("Failed to open (%s) = errno %d", device_path, status);
5241*8fce2fd6Sachartre 		return (status);
5242*8fce2fd6Sachartre 	}
5243*8fce2fd6Sachartre 
5244*8fce2fd6Sachartre 	/* Get device number of backing device */
5245*8fce2fd6Sachartre 	if ((status = ldi_get_dev(vd->ldi_handle[0], &vd->dev[0])) != 0) {
5246*8fce2fd6Sachartre 		PRN("ldi_get_dev() returned errno %d for %s",
5247*8fce2fd6Sachartre 		    status, device_path);
5248*8fce2fd6Sachartre 		return (status);
5249*8fce2fd6Sachartre 	}
5250*8fce2fd6Sachartre 
5251*8fce2fd6Sachartre 	/*
5252*8fce2fd6Sachartre 	 * We start by looking if the driver is in the list from vds.conf
5253*8fce2fd6Sachartre 	 * so that we can override the built-in list using vds.conf.
5254*8fce2fd6Sachartre 	 */
5255*8fce2fd6Sachartre 	drv_name = ddi_major_to_name(getmajor(vd->dev[0]));
5256*8fce2fd6Sachartre 	drv_type = VD_DRIVER_UNKNOWN;
5257*8fce2fd6Sachartre 
5258*8fce2fd6Sachartre 	/* check vds.conf list */
5259*8fce2fd6Sachartre 	for (i = 0; i < vds->num_drivers; i++) {
5260*8fce2fd6Sachartre 		if (vds->driver_types[i].type == VD_DRIVER_UNKNOWN) {
5261*8fce2fd6Sachartre 			/* ignore invalid entries */
5262*8fce2fd6Sachartre 			continue;
5263*8fce2fd6Sachartre 		}
5264*8fce2fd6Sachartre 		if (strcmp(drv_name, vds->driver_types[i].name) == 0) {
5265*8fce2fd6Sachartre 			drv_type = vds->driver_types[i].type;
5266*8fce2fd6Sachartre 			goto done;
5267*8fce2fd6Sachartre 		}
5268*8fce2fd6Sachartre 	}
5269*8fce2fd6Sachartre 
5270*8fce2fd6Sachartre 	/* check built-in list */
5271*8fce2fd6Sachartre 	for (i = 0; i < VDS_NUM_DRIVERS; i++) {
5272*8fce2fd6Sachartre 		if (strcmp(drv_name, vds_driver_types[i].name) == 0) {
5273*8fce2fd6Sachartre 			drv_type = vds_driver_types[i].type;
5274*8fce2fd6Sachartre 			goto done;
5275*8fce2fd6Sachartre 		}
5276*8fce2fd6Sachartre 	}
5277*8fce2fd6Sachartre 
5278*8fce2fd6Sachartre done:
5279*8fce2fd6Sachartre 	PR0("driver %s identified as %s", drv_name,
5280*8fce2fd6Sachartre 	    (drv_type == VD_DRIVER_DISK)? "DISK" :
5281*8fce2fd6Sachartre 	    (drv_type == VD_DRIVER_VOLUME)? "VOLUME" : "UNKNOWN");
5282*8fce2fd6Sachartre 
5283*8fce2fd6Sachartre 	*dtype = drv_type;
5284*8fce2fd6Sachartre 
5285*8fce2fd6Sachartre 	return (0);
5286*8fce2fd6Sachartre }
5287*8fce2fd6Sachartre 
52881ae08745Sheppo static int
5289047ba61eSachartre vd_setup_vd(vd_t *vd)
5290047ba61eSachartre {
5291*8fce2fd6Sachartre 	int		status, drv_type, pseudo;
5292047ba61eSachartre 	dev_info_t	*dip;
5293047ba61eSachartre 	vnode_t 	*vnp;
5294047ba61eSachartre 	char		*path = vd->device_path;
5295047ba61eSachartre 
5296047ba61eSachartre 	/* make sure the vdisk backend is valid */
5297047ba61eSachartre 	if ((status = lookupname(path, UIO_SYSSPACE,
5298047ba61eSachartre 	    FOLLOW, NULLVPP, &vnp)) != 0) {
5299047ba61eSachartre 		PR0("Cannot lookup %s errno %d", path, status);
5300047ba61eSachartre 		goto done;
5301047ba61eSachartre 	}
5302047ba61eSachartre 
5303047ba61eSachartre 	switch (vnp->v_type) {
5304047ba61eSachartre 	case VREG:
5305047ba61eSachartre 		/*
5306047ba61eSachartre 		 * Backend is a file so it is exported as a full disk or as a
5307047ba61eSachartre 		 * single slice disk using the vnode interface.
5308047ba61eSachartre 		 */
5309047ba61eSachartre 		VN_RELE(vnp);
5310*8fce2fd6Sachartre 		vd->volume = B_FALSE;
5311047ba61eSachartre 		status = vd_setup_backend_vnode(vd);
5312047ba61eSachartre 		break;
5313047ba61eSachartre 
5314047ba61eSachartre 	case VBLK:
5315047ba61eSachartre 	case VCHR:
5316047ba61eSachartre 		/*
5317047ba61eSachartre 		 * Backend is a device. The way it is exported depends on the
5318047ba61eSachartre 		 * type of the device.
5319047ba61eSachartre 		 *
5320*8fce2fd6Sachartre 		 * - A volume device is exported as a full disk using the vnode
5321047ba61eSachartre 		 *   interface or as a single slice disk using the LDI
5322047ba61eSachartre 		 *   interface.
5323047ba61eSachartre 		 *
5324047ba61eSachartre 		 * - A disk (represented by the slice 2 of that disk) is
5325047ba61eSachartre 		 *   exported as a full disk using the LDI interface.
5326047ba61eSachartre 		 *
5327047ba61eSachartre 		 * - A disk slice (different from slice 2) is always exported
5328047ba61eSachartre 		 *   as a single slice disk using the LDI interface.
5329047ba61eSachartre 		 *
5330047ba61eSachartre 		 * - The slice 2 of a disk is exported as a single slice disk
5331047ba61eSachartre 		 *   if the "slice" option is specified, otherwise the entire
5332047ba61eSachartre 		 *   disk will be exported. In any case, the LDI interface is
5333047ba61eSachartre 		 *   used.
5334047ba61eSachartre 		 */
5335047ba61eSachartre 
5336047ba61eSachartre 		/* check if this is a pseudo device */
5337047ba61eSachartre 		if ((dip = ddi_hold_devi_by_instance(getmajor(vnp->v_rdev),
5338047ba61eSachartre 		    dev_to_instance(vnp->v_rdev), 0))  == NULL) {
5339047ba61eSachartre 			PRN("%s is no longer accessible", path);
5340047ba61eSachartre 			VN_RELE(vnp);
5341047ba61eSachartre 			status = EIO;
5342047ba61eSachartre 			break;
5343047ba61eSachartre 		}
5344*8fce2fd6Sachartre 		pseudo = is_pseudo_device(dip);
5345047ba61eSachartre 		ddi_release_devi(dip);
5346047ba61eSachartre 		VN_RELE(vnp);
5347047ba61eSachartre 
5348*8fce2fd6Sachartre 		if (vd_identify_dev(vd, &drv_type) != 0) {
5349*8fce2fd6Sachartre 			PRN("%s identification failed", path);
5350*8fce2fd6Sachartre 			status = EIO;
5351*8fce2fd6Sachartre 			break;
5352*8fce2fd6Sachartre 		}
5353*8fce2fd6Sachartre 
5354*8fce2fd6Sachartre 		/*
5355*8fce2fd6Sachartre 		 * If the driver hasn't been identified then we consider that
5356*8fce2fd6Sachartre 		 * pseudo devices are volumes and other devices are disks.
5357*8fce2fd6Sachartre 		 */
5358*8fce2fd6Sachartre 		if (drv_type == VD_DRIVER_VOLUME ||
5359*8fce2fd6Sachartre 		    (drv_type == VD_DRIVER_UNKNOWN && pseudo)) {
5360*8fce2fd6Sachartre 			vd->volume = B_TRUE;
5361*8fce2fd6Sachartre 		} else {
53622f5224aeSachartre 			status = vd_setup_backend_ldi(vd);
53632f5224aeSachartre 			break;
53642f5224aeSachartre 		}
53652f5224aeSachartre 
5366047ba61eSachartre 		/*
5367*8fce2fd6Sachartre 		 * If this is a volume device then its usage depends if the
5368047ba61eSachartre 		 * "slice" option is set or not. If the "slice" option is set
5369*8fce2fd6Sachartre 		 * then the volume device will be exported as a single slice,
5370047ba61eSachartre 		 * otherwise it will be exported as a full disk.
53712f5224aeSachartre 		 *
53722f5224aeSachartre 		 * For backward compatibility, if vd_volume_force_slice is set
5373*8fce2fd6Sachartre 		 * then we always export volume devices as slices.
5374047ba61eSachartre 		 */
53752f5224aeSachartre 		if (vd_volume_force_slice) {
53762f5224aeSachartre 			vd->vdisk_type = VD_DISK_TYPE_SLICE;
53772f5224aeSachartre 			vd->nslices = 1;
53782f5224aeSachartre 		}
53792f5224aeSachartre 
5380*8fce2fd6Sachartre 		if (vd->vdisk_type == VD_DISK_TYPE_DISK) {
5381*8fce2fd6Sachartre 			/* close device opened during identification */
5382*8fce2fd6Sachartre 			(void) ldi_close(vd->ldi_handle[0],
5383*8fce2fd6Sachartre 			    vd->open_flags & ~FWRITE, kcred);
5384*8fce2fd6Sachartre 			vd->ldi_handle[0] = NULL;
5385*8fce2fd6Sachartre 			vd->dev[0] = 0;
5386047ba61eSachartre 			status = vd_setup_backend_vnode(vd);
5387*8fce2fd6Sachartre 		} else {
5388047ba61eSachartre 			status = vd_setup_backend_ldi(vd);
5389*8fce2fd6Sachartre 		}
5390047ba61eSachartre 		break;
5391047ba61eSachartre 
5392047ba61eSachartre 	default:
5393047ba61eSachartre 		PRN("Unsupported vdisk backend %s", path);
5394047ba61eSachartre 		VN_RELE(vnp);
5395047ba61eSachartre 		status = EBADF;
5396047ba61eSachartre 	}
5397047ba61eSachartre 
5398047ba61eSachartre done:
5399047ba61eSachartre 	if (status != 0) {
5400047ba61eSachartre 		/*
5401047ba61eSachartre 		 * If the error is retryable print an error message only
5402047ba61eSachartre 		 * during the first try.
5403047ba61eSachartre 		 */
5404047ba61eSachartre 		if (status == ENXIO || status == ENODEV ||
5405047ba61eSachartre 		    status == ENOENT || status == EROFS) {
5406047ba61eSachartre 			if (!(vd->initialized & VD_SETUP_ERROR)) {
5407047ba61eSachartre 				PRN("%s is currently inaccessible (error %d)",
5408047ba61eSachartre 				    path, status);
5409047ba61eSachartre 			}
5410047ba61eSachartre 			status = EAGAIN;
5411047ba61eSachartre 		} else {
5412047ba61eSachartre 			PRN("%s can not be exported as a virtual disk "
5413047ba61eSachartre 			    "(error %d)", path, status);
5414047ba61eSachartre 		}
5415047ba61eSachartre 		vd->initialized |= VD_SETUP_ERROR;
5416047ba61eSachartre 
5417047ba61eSachartre 	} else if (vd->initialized & VD_SETUP_ERROR) {
5418047ba61eSachartre 		/* print a message only if we previously had an error */
5419047ba61eSachartre 		PRN("%s is now online", path);
5420047ba61eSachartre 		vd->initialized &= ~VD_SETUP_ERROR;
5421047ba61eSachartre 	}
5422047ba61eSachartre 
5423047ba61eSachartre 	return (status);
5424047ba61eSachartre }
5425047ba61eSachartre 
5426047ba61eSachartre static int
5427047ba61eSachartre vds_do_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options,
5428047ba61eSachartre     uint64_t ldc_id, vd_t **vdp)
54291ae08745Sheppo {
54301ae08745Sheppo 	char			tq_name[TASKQ_NAMELEN];
54310a55fbb7Slm66018 	int			status;
54321ae08745Sheppo 	ddi_iblock_cookie_t	iblock = NULL;
54331ae08745Sheppo 	ldc_attr_t		ldc_attr;
54341ae08745Sheppo 	vd_t			*vd;
54351ae08745Sheppo 
54361ae08745Sheppo 
54371ae08745Sheppo 	ASSERT(vds != NULL);
5438e1ebb9ecSlm66018 	ASSERT(device_path != NULL);
54391ae08745Sheppo 	ASSERT(vdp != NULL);
5440e1ebb9ecSlm66018 	PR0("Adding vdisk for %s", device_path);
54411ae08745Sheppo 
54421ae08745Sheppo 	if ((vd = kmem_zalloc(sizeof (*vd), KM_NOSLEEP)) == NULL) {
54431ae08745Sheppo 		PRN("No memory for virtual disk");
54441ae08745Sheppo 		return (EAGAIN);
54451ae08745Sheppo 	}
54461ae08745Sheppo 	*vdp = vd;	/* assign here so vds_destroy_vd() can cleanup later */
54471ae08745Sheppo 	vd->vds = vds;
54483c96341aSnarayan 	(void) strncpy(vd->device_path, device_path, MAXPATHLEN);
54491ae08745Sheppo 
5450047ba61eSachartre 	/* Setup open flags */
5451047ba61eSachartre 	vd->open_flags = FREAD;
5452047ba61eSachartre 
5453047ba61eSachartre 	if (!(options & VD_OPT_RDONLY))
5454047ba61eSachartre 		vd->open_flags |= FWRITE;
5455047ba61eSachartre 
5456047ba61eSachartre 	if (options & VD_OPT_EXCLUSIVE)
5457047ba61eSachartre 		vd->open_flags |= FEXCL;
5458047ba61eSachartre 
5459047ba61eSachartre 	/* Setup disk type */
5460047ba61eSachartre 	if (options & VD_OPT_SLICE) {
5461047ba61eSachartre 		vd->vdisk_type = VD_DISK_TYPE_SLICE;
5462047ba61eSachartre 		vd->nslices = 1;
5463047ba61eSachartre 	} else {
5464047ba61eSachartre 		vd->vdisk_type = VD_DISK_TYPE_DISK;
5465047ba61eSachartre 		vd->nslices = V_NUMPAR;
5466047ba61eSachartre 	}
5467047ba61eSachartre 
5468047ba61eSachartre 	/* default disk label */
5469047ba61eSachartre 	vd->vdisk_label = VD_DISK_LABEL_UNK;
5470047ba61eSachartre 
54710a55fbb7Slm66018 	/* Open vdisk and initialize parameters */
54723c96341aSnarayan 	if ((status = vd_setup_vd(vd)) == 0) {
54733c96341aSnarayan 		vd->initialized |= VD_DISK_READY;
54741ae08745Sheppo 
54753c96341aSnarayan 		ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
5476*8fce2fd6Sachartre 		PR0("vdisk_type = %s, volume = %s, file = %s, nslices = %u",
54773c96341aSnarayan 		    ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
5478*8fce2fd6Sachartre 		    (vd->volume ? "yes" : "no"), (vd->file ? "yes" : "no"),
54793c96341aSnarayan 		    vd->nslices);
54803c96341aSnarayan 	} else {
54813c96341aSnarayan 		if (status != EAGAIN)
54823c96341aSnarayan 			return (status);
54833c96341aSnarayan 	}
54841ae08745Sheppo 
54851ae08745Sheppo 	/* Initialize locking */
54861ae08745Sheppo 	if (ddi_get_soft_iblock_cookie(vds->dip, DDI_SOFTINT_MED,
54871ae08745Sheppo 	    &iblock) != DDI_SUCCESS) {
54881ae08745Sheppo 		PRN("Could not get iblock cookie.");
54891ae08745Sheppo 		return (EIO);
54901ae08745Sheppo 	}
54911ae08745Sheppo 
54921ae08745Sheppo 	mutex_init(&vd->lock, NULL, MUTEX_DRIVER, iblock);
54931ae08745Sheppo 	vd->initialized |= VD_LOCKING;
54941ae08745Sheppo 
54951ae08745Sheppo 
5496d10e4ef2Snarayan 	/* Create start and completion task queues for the vdisk */
5497d10e4ef2Snarayan 	(void) snprintf(tq_name, sizeof (tq_name), "vd_startq%lu", id);
54981ae08745Sheppo 	PR1("tq_name = %s", tq_name);
5499d10e4ef2Snarayan 	if ((vd->startq = ddi_taskq_create(vds->dip, tq_name, 1,
55001ae08745Sheppo 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
55011ae08745Sheppo 		PRN("Could not create task queue");
55021ae08745Sheppo 		return (EIO);
55031ae08745Sheppo 	}
5504d10e4ef2Snarayan 	(void) snprintf(tq_name, sizeof (tq_name), "vd_completionq%lu", id);
5505d10e4ef2Snarayan 	PR1("tq_name = %s", tq_name);
5506d10e4ef2Snarayan 	if ((vd->completionq = ddi_taskq_create(vds->dip, tq_name, 1,
5507d10e4ef2Snarayan 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
5508d10e4ef2Snarayan 		PRN("Could not create task queue");
5509d10e4ef2Snarayan 		return (EIO);
5510d10e4ef2Snarayan 	}
5511d10e4ef2Snarayan 	vd->enabled = 1;	/* before callback can dispatch to startq */
55121ae08745Sheppo 
55131ae08745Sheppo 
55141ae08745Sheppo 	/* Bring up LDC */
55151ae08745Sheppo 	ldc_attr.devclass	= LDC_DEV_BLK_SVC;
55161ae08745Sheppo 	ldc_attr.instance	= ddi_get_instance(vds->dip);
55171ae08745Sheppo 	ldc_attr.mode		= LDC_MODE_UNRELIABLE;
5518e1ebb9ecSlm66018 	ldc_attr.mtu		= VD_LDC_MTU;
55191ae08745Sheppo 	if ((status = ldc_init(ldc_id, &ldc_attr, &vd->ldc_handle)) != 0) {
552017cadca8Slm66018 		PRN("Could not initialize LDC channel %lx, "
5521690555a1Sachartre 		    "init failed with error %d", ldc_id, status);
55221ae08745Sheppo 		return (status);
55231ae08745Sheppo 	}
55241ae08745Sheppo 	vd->initialized |= VD_LDC;
55251ae08745Sheppo 
55261ae08745Sheppo 	if ((status = ldc_reg_callback(vd->ldc_handle, vd_handle_ldc_events,
55271ae08745Sheppo 	    (caddr_t)vd)) != 0) {
5528690555a1Sachartre 		PRN("Could not initialize LDC channel %lu,"
5529690555a1Sachartre 		    "reg_callback failed with error %d", ldc_id, status);
55301ae08745Sheppo 		return (status);
55311ae08745Sheppo 	}
55321ae08745Sheppo 
55331ae08745Sheppo 	if ((status = ldc_open(vd->ldc_handle)) != 0) {
5534690555a1Sachartre 		PRN("Could not initialize LDC channel %lu,"
5535690555a1Sachartre 		    "open failed with error %d", ldc_id, status);
55361ae08745Sheppo 		return (status);
55371ae08745Sheppo 	}
55381ae08745Sheppo 
55393af08d82Slm66018 	if ((status = ldc_up(vd->ldc_handle)) != 0) {
554034683adeSsg70180 		PR0("ldc_up() returned errno %d", status);
55413af08d82Slm66018 	}
55423af08d82Slm66018 
55434bac2208Snarayan 	/* Allocate the inband task memory handle */
55444bac2208Snarayan 	status = ldc_mem_alloc_handle(vd->ldc_handle, &(vd->inband_task.mhdl));
55454bac2208Snarayan 	if (status) {
5546690555a1Sachartre 		PRN("Could not initialize LDC channel %lu,"
5547690555a1Sachartre 		    "alloc_handle failed with error %d", ldc_id, status);
55484bac2208Snarayan 		return (ENXIO);
55494bac2208Snarayan 	}
55501ae08745Sheppo 
55511ae08745Sheppo 	/* Add the successfully-initialized vdisk to the server's table */
55521ae08745Sheppo 	if (mod_hash_insert(vds->vd_table, (mod_hash_key_t)id, vd) != 0) {
55531ae08745Sheppo 		PRN("Error adding vdisk ID %lu to table", id);
55541ae08745Sheppo 		return (EIO);
55551ae08745Sheppo 	}
55561ae08745Sheppo 
55573af08d82Slm66018 	/* Allocate the staging buffer */
55583af08d82Slm66018 	vd->max_msglen	= sizeof (vio_msg_t);	/* baseline vio message size */
55593af08d82Slm66018 	vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
55603af08d82Slm66018 
55613af08d82Slm66018 	/* store initial state */
55623af08d82Slm66018 	vd->state = VD_STATE_INIT;
55633af08d82Slm66018 
55641ae08745Sheppo 	return (0);
55651ae08745Sheppo }
55661ae08745Sheppo 
55673af08d82Slm66018 static void
55683af08d82Slm66018 vd_free_dring_task(vd_t *vdp)
55693af08d82Slm66018 {
55703af08d82Slm66018 	if (vdp->dring_task != NULL) {
55713af08d82Slm66018 		ASSERT(vdp->dring_len != 0);
55723af08d82Slm66018 		/* Free all dring_task memory handles */
55733af08d82Slm66018 		for (int i = 0; i < vdp->dring_len; i++) {
55743af08d82Slm66018 			(void) ldc_mem_free_handle(vdp->dring_task[i].mhdl);
55753af08d82Slm66018 			kmem_free(vdp->dring_task[i].msg, vdp->max_msglen);
55763af08d82Slm66018 			vdp->dring_task[i].msg = NULL;
55773af08d82Slm66018 		}
55783af08d82Slm66018 		kmem_free(vdp->dring_task,
55793af08d82Slm66018 		    (sizeof (*vdp->dring_task)) * vdp->dring_len);
55803af08d82Slm66018 		vdp->dring_task = NULL;
55813af08d82Slm66018 	}
55823af08d82Slm66018 }
55833af08d82Slm66018 
55841ae08745Sheppo /*
55851ae08745Sheppo  * Destroy the state associated with a virtual disk
55861ae08745Sheppo  */
55871ae08745Sheppo static void
55881ae08745Sheppo vds_destroy_vd(void *arg)
55891ae08745Sheppo {
55901ae08745Sheppo 	vd_t	*vd = (vd_t *)arg;
559134683adeSsg70180 	int	retry = 0, rv;
55921ae08745Sheppo 
55931ae08745Sheppo 	if (vd == NULL)
55941ae08745Sheppo 		return;
55951ae08745Sheppo 
5596d10e4ef2Snarayan 	PR0("Destroying vdisk state");
5597d10e4ef2Snarayan 
55981ae08745Sheppo 	/* Disable queuing requests for the vdisk */
55991ae08745Sheppo 	if (vd->initialized & VD_LOCKING) {
56001ae08745Sheppo 		mutex_enter(&vd->lock);
56011ae08745Sheppo 		vd->enabled = 0;
56021ae08745Sheppo 		mutex_exit(&vd->lock);
56031ae08745Sheppo 	}
56041ae08745Sheppo 
5605d10e4ef2Snarayan 	/* Drain and destroy start queue (*before* destroying completionq) */
5606d10e4ef2Snarayan 	if (vd->startq != NULL)
5607d10e4ef2Snarayan 		ddi_taskq_destroy(vd->startq);	/* waits for queued tasks */
5608d10e4ef2Snarayan 
5609d10e4ef2Snarayan 	/* Drain and destroy completion queue (*before* shutting down LDC) */
5610d10e4ef2Snarayan 	if (vd->completionq != NULL)
5611d10e4ef2Snarayan 		ddi_taskq_destroy(vd->completionq);	/* waits for tasks */
5612d10e4ef2Snarayan 
56133af08d82Slm66018 	vd_free_dring_task(vd);
56143af08d82Slm66018 
561534683adeSsg70180 	/* Free the inband task memory handle */
561634683adeSsg70180 	(void) ldc_mem_free_handle(vd->inband_task.mhdl);
561734683adeSsg70180 
561834683adeSsg70180 	/* Shut down LDC */
561934683adeSsg70180 	if (vd->initialized & VD_LDC) {
562034683adeSsg70180 		/* unmap the dring */
562134683adeSsg70180 		if (vd->initialized & VD_DRING)
562234683adeSsg70180 			(void) ldc_mem_dring_unmap(vd->dring_handle);
562334683adeSsg70180 
562434683adeSsg70180 		/* close LDC channel - retry on EAGAIN */
562534683adeSsg70180 		while ((rv = ldc_close(vd->ldc_handle)) == EAGAIN) {
562634683adeSsg70180 			if (++retry > vds_ldc_retries) {
562734683adeSsg70180 				PR0("Timed out closing channel");
562834683adeSsg70180 				break;
562934683adeSsg70180 			}
563034683adeSsg70180 			drv_usecwait(vds_ldc_delay);
563134683adeSsg70180 		}
563234683adeSsg70180 		if (rv == 0) {
563334683adeSsg70180 			(void) ldc_unreg_callback(vd->ldc_handle);
563434683adeSsg70180 			(void) ldc_fini(vd->ldc_handle);
563534683adeSsg70180 		} else {
563634683adeSsg70180 			/*
563734683adeSsg70180 			 * Closing the LDC channel has failed. Ideally we should
563834683adeSsg70180 			 * fail here but there is no Zeus level infrastructure
563934683adeSsg70180 			 * to handle this. The MD has already been changed and
564034683adeSsg70180 			 * we have to do the close. So we try to do as much
564134683adeSsg70180 			 * clean up as we can.
564234683adeSsg70180 			 */
564334683adeSsg70180 			(void) ldc_set_cb_mode(vd->ldc_handle, LDC_CB_DISABLE);
564434683adeSsg70180 			while (ldc_unreg_callback(vd->ldc_handle) == EAGAIN)
564534683adeSsg70180 				drv_usecwait(vds_ldc_delay);
564634683adeSsg70180 		}
564734683adeSsg70180 	}
564834683adeSsg70180 
56493af08d82Slm66018 	/* Free the staging buffer for msgs */
56503af08d82Slm66018 	if (vd->vio_msgp != NULL) {
56513af08d82Slm66018 		kmem_free(vd->vio_msgp, vd->max_msglen);
56523af08d82Slm66018 		vd->vio_msgp = NULL;
56533af08d82Slm66018 	}
56543af08d82Slm66018 
56553af08d82Slm66018 	/* Free the inband message buffer */
56563af08d82Slm66018 	if (vd->inband_task.msg != NULL) {
56573af08d82Slm66018 		kmem_free(vd->inband_task.msg, vd->max_msglen);
56583af08d82Slm66018 		vd->inband_task.msg = NULL;
5659d10e4ef2Snarayan 	}
5660da6c28aaSamw 
56613c96341aSnarayan 	if (vd->file) {
5662690555a1Sachartre 		/* Close file */
5663047ba61eSachartre 		(void) VOP_CLOSE(vd->file_vnode, vd->open_flags, 1,
5664da6c28aaSamw 		    0, kcred, NULL);
56653c96341aSnarayan 		VN_RELE(vd->file_vnode);
566687a7269eSachartre 		if (vd->file_devid != NULL)
566787a7269eSachartre 			ddi_devid_free(vd->file_devid);
56683c96341aSnarayan 	} else {
56691ae08745Sheppo 		/* Close any open backing-device slices */
56701ae08745Sheppo 		for (uint_t slice = 0; slice < vd->nslices; slice++) {
56711ae08745Sheppo 			if (vd->ldi_handle[slice] != NULL) {
56721ae08745Sheppo 				PR0("Closing slice %u", slice);
56731ae08745Sheppo 				(void) ldi_close(vd->ldi_handle[slice],
5674047ba61eSachartre 				    vd->open_flags, kcred);
56751ae08745Sheppo 			}
56761ae08745Sheppo 		}
56773c96341aSnarayan 	}
56781ae08745Sheppo 
56791ae08745Sheppo 	/* Free lock */
56801ae08745Sheppo 	if (vd->initialized & VD_LOCKING)
56811ae08745Sheppo 		mutex_destroy(&vd->lock);
56821ae08745Sheppo 
56831ae08745Sheppo 	/* Finally, free the vdisk structure itself */
56841ae08745Sheppo 	kmem_free(vd, sizeof (*vd));
56851ae08745Sheppo }
56861ae08745Sheppo 
56871ae08745Sheppo static int
5688047ba61eSachartre vds_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options,
5689047ba61eSachartre     uint64_t ldc_id)
56901ae08745Sheppo {
56911ae08745Sheppo 	int	status;
56921ae08745Sheppo 	vd_t	*vd = NULL;
56931ae08745Sheppo 
56941ae08745Sheppo 
5695047ba61eSachartre 	if ((status = vds_do_init_vd(vds, id, device_path, options,
5696047ba61eSachartre 	    ldc_id, &vd)) != 0)
56971ae08745Sheppo 		vds_destroy_vd(vd);
56981ae08745Sheppo 
56991ae08745Sheppo 	return (status);
57001ae08745Sheppo }
57011ae08745Sheppo 
57021ae08745Sheppo static int
57031ae08745Sheppo vds_do_get_ldc_id(md_t *md, mde_cookie_t vd_node, mde_cookie_t *channel,
57041ae08745Sheppo     uint64_t *ldc_id)
57051ae08745Sheppo {
57061ae08745Sheppo 	int	num_channels;
57071ae08745Sheppo 
57081ae08745Sheppo 
57091ae08745Sheppo 	/* Look for channel endpoint child(ren) of the vdisk MD node */
57101ae08745Sheppo 	if ((num_channels = md_scan_dag(md, vd_node,
57111ae08745Sheppo 	    md_find_name(md, VD_CHANNEL_ENDPOINT),
57121ae08745Sheppo 	    md_find_name(md, "fwd"), channel)) <= 0) {
57131ae08745Sheppo 		PRN("No \"%s\" found for virtual disk", VD_CHANNEL_ENDPOINT);
57141ae08745Sheppo 		return (-1);
57151ae08745Sheppo 	}
57161ae08745Sheppo 
57171ae08745Sheppo 	/* Get the "id" value for the first channel endpoint node */
57181ae08745Sheppo 	if (md_get_prop_val(md, channel[0], VD_ID_PROP, ldc_id) != 0) {
57191ae08745Sheppo 		PRN("No \"%s\" property found for \"%s\" of vdisk",
57201ae08745Sheppo 		    VD_ID_PROP, VD_CHANNEL_ENDPOINT);
57211ae08745Sheppo 		return (-1);
57221ae08745Sheppo 	}
57231ae08745Sheppo 
57241ae08745Sheppo 	if (num_channels > 1) {
57251ae08745Sheppo 		PRN("Using ID of first of multiple channels for this vdisk");
57261ae08745Sheppo 	}
57271ae08745Sheppo 
57281ae08745Sheppo 	return (0);
57291ae08745Sheppo }
57301ae08745Sheppo 
57311ae08745Sheppo static int
57321ae08745Sheppo vds_get_ldc_id(md_t *md, mde_cookie_t vd_node, uint64_t *ldc_id)
57331ae08745Sheppo {
57341ae08745Sheppo 	int		num_nodes, status;
57351ae08745Sheppo 	size_t		size;
57361ae08745Sheppo 	mde_cookie_t	*channel;
57371ae08745Sheppo 
57381ae08745Sheppo 
57391ae08745Sheppo 	if ((num_nodes = md_node_count(md)) <= 0) {
57401ae08745Sheppo 		PRN("Invalid node count in Machine Description subtree");
57411ae08745Sheppo 		return (-1);
57421ae08745Sheppo 	}
57431ae08745Sheppo 	size = num_nodes*(sizeof (*channel));
57441ae08745Sheppo 	channel = kmem_zalloc(size, KM_SLEEP);
57451ae08745Sheppo 	status = vds_do_get_ldc_id(md, vd_node, channel, ldc_id);
57461ae08745Sheppo 	kmem_free(channel, size);
57471ae08745Sheppo 
57481ae08745Sheppo 	return (status);
57491ae08745Sheppo }
57501ae08745Sheppo 
5751047ba61eSachartre /*
5752047ba61eSachartre  * Function:
5753047ba61eSachartre  *	vds_get_options
5754047ba61eSachartre  *
5755047ba61eSachartre  * Description:
5756047ba61eSachartre  * 	Parse the options of a vds node. Options are defined as an array
5757047ba61eSachartre  *	of strings in the vds-block-device-opts property of the vds node
5758047ba61eSachartre  *	in the machine description. Options are returned as a bitmask. The
5759047ba61eSachartre  *	mapping between the bitmask options and the options strings from the
5760047ba61eSachartre  *	machine description is defined in the vd_bdev_options[] array.
5761047ba61eSachartre  *
5762047ba61eSachartre  *	The vds-block-device-opts property is optional. If a vds has no such
5763047ba61eSachartre  *	property then no option is defined.
5764047ba61eSachartre  *
5765047ba61eSachartre  * Parameters:
5766047ba61eSachartre  *	md		- machine description.
5767047ba61eSachartre  *	vd_node		- vds node in the machine description for which
5768047ba61eSachartre  *			  options have to be parsed.
5769047ba61eSachartre  *	options		- the returned options.
5770047ba61eSachartre  *
5771047ba61eSachartre  * Return Code:
5772047ba61eSachartre  *	none.
5773047ba61eSachartre  */
5774047ba61eSachartre static void
5775047ba61eSachartre vds_get_options(md_t *md, mde_cookie_t vd_node, uint64_t *options)
5776047ba61eSachartre {
5777047ba61eSachartre 	char	*optstr, *opt;
5778047ba61eSachartre 	int	len, n, i;
5779047ba61eSachartre 
5780047ba61eSachartre 	*options = 0;
5781047ba61eSachartre 
5782047ba61eSachartre 	if (md_get_prop_data(md, vd_node, VD_BLOCK_DEVICE_OPTS,
5783047ba61eSachartre 	    (uint8_t **)&optstr, &len) != 0) {
5784047ba61eSachartre 		PR0("No options found");
5785047ba61eSachartre 		return;
5786047ba61eSachartre 	}
5787047ba61eSachartre 
5788047ba61eSachartre 	/* parse options */
5789047ba61eSachartre 	opt = optstr;
5790047ba61eSachartre 	n = sizeof (vd_bdev_options) / sizeof (vd_option_t);
5791047ba61eSachartre 
5792047ba61eSachartre 	while (opt < optstr + len) {
5793047ba61eSachartre 		for (i = 0; i < n; i++) {
5794047ba61eSachartre 			if (strncmp(vd_bdev_options[i].vdo_name,
5795047ba61eSachartre 			    opt, VD_OPTION_NLEN) == 0) {
5796047ba61eSachartre 				*options |= vd_bdev_options[i].vdo_value;
5797047ba61eSachartre 				break;
5798047ba61eSachartre 			}
5799047ba61eSachartre 		}
5800047ba61eSachartre 
5801047ba61eSachartre 		if (i < n) {
5802047ba61eSachartre 			PR0("option: %s", opt);
5803047ba61eSachartre 		} else {
5804047ba61eSachartre 			PRN("option %s is unknown or unsupported", opt);
5805047ba61eSachartre 		}
5806047ba61eSachartre 
5807047ba61eSachartre 		opt += strlen(opt) + 1;
5808047ba61eSachartre 	}
5809047ba61eSachartre }
5810047ba61eSachartre 
58111ae08745Sheppo static void
5812*8fce2fd6Sachartre vds_driver_types_free(vds_t *vds)
5813*8fce2fd6Sachartre {
5814*8fce2fd6Sachartre 	if (vds->driver_types != NULL) {
5815*8fce2fd6Sachartre 		kmem_free(vds->driver_types, sizeof (vd_driver_type_t) *
5816*8fce2fd6Sachartre 		    vds->num_drivers);
5817*8fce2fd6Sachartre 		vds->driver_types = NULL;
5818*8fce2fd6Sachartre 		vds->num_drivers = 0;
5819*8fce2fd6Sachartre 	}
5820*8fce2fd6Sachartre }
5821*8fce2fd6Sachartre 
5822*8fce2fd6Sachartre /*
5823*8fce2fd6Sachartre  * Update the driver type list with information from vds.conf.
5824*8fce2fd6Sachartre  */
5825*8fce2fd6Sachartre static void
5826*8fce2fd6Sachartre vds_driver_types_update(vds_t *vds)
5827*8fce2fd6Sachartre {
5828*8fce2fd6Sachartre 	char **list, *s;
5829*8fce2fd6Sachartre 	uint_t i, num, count = 0, len;
5830*8fce2fd6Sachartre 
5831*8fce2fd6Sachartre 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, vds->dip,
5832*8fce2fd6Sachartre 	    DDI_PROP_DONTPASS, "driver-type-list", &list, &num) !=
5833*8fce2fd6Sachartre 	    DDI_PROP_SUCCESS)
5834*8fce2fd6Sachartre 		return;
5835*8fce2fd6Sachartre 
5836*8fce2fd6Sachartre 	/*
5837*8fce2fd6Sachartre 	 * We create a driver_types list with as many as entries as there
5838*8fce2fd6Sachartre 	 * is in the driver-type-list from vds.conf. However only valid
5839*8fce2fd6Sachartre 	 * entries will be populated (i.e. entries from driver-type-list
5840*8fce2fd6Sachartre 	 * with a valid syntax). Invalid entries will be left blank so
5841*8fce2fd6Sachartre 	 * they will have no driver name and the driver type will be
5842*8fce2fd6Sachartre 	 * VD_DRIVER_UNKNOWN (= 0).
5843*8fce2fd6Sachartre 	 */
5844*8fce2fd6Sachartre 	vds->num_drivers = num;
5845*8fce2fd6Sachartre 	vds->driver_types = kmem_zalloc(sizeof (vd_driver_type_t) * num,
5846*8fce2fd6Sachartre 	    KM_SLEEP);
5847*8fce2fd6Sachartre 
5848*8fce2fd6Sachartre 	for (i = 0; i < num; i++) {
5849*8fce2fd6Sachartre 
5850*8fce2fd6Sachartre 		s = strchr(list[i], ':');
5851*8fce2fd6Sachartre 
5852*8fce2fd6Sachartre 		if (s == NULL) {
5853*8fce2fd6Sachartre 			PRN("vds.conf: driver-type-list, entry %d (%s): "
5854*8fce2fd6Sachartre 			    "a colon is expected in the entry",
5855*8fce2fd6Sachartre 			    i, list[i]);
5856*8fce2fd6Sachartre 			continue;
5857*8fce2fd6Sachartre 		}
5858*8fce2fd6Sachartre 
5859*8fce2fd6Sachartre 		len = (uintptr_t)s - (uintptr_t)list[i];
5860*8fce2fd6Sachartre 
5861*8fce2fd6Sachartre 		if (len == 0) {
5862*8fce2fd6Sachartre 			PRN("vds.conf: driver-type-list, entry %d (%s): "
5863*8fce2fd6Sachartre 			    "the driver name is empty",
5864*8fce2fd6Sachartre 			    i, list[i]);
5865*8fce2fd6Sachartre 			continue;
5866*8fce2fd6Sachartre 		}
5867*8fce2fd6Sachartre 
5868*8fce2fd6Sachartre 		if (len >= VD_DRIVER_NAME_LEN) {
5869*8fce2fd6Sachartre 			PRN("vds.conf: driver-type-list, entry %d (%s): "
5870*8fce2fd6Sachartre 			    "the driver name is too long",
5871*8fce2fd6Sachartre 			    i, list[i]);
5872*8fce2fd6Sachartre 			continue;
5873*8fce2fd6Sachartre 		}
5874*8fce2fd6Sachartre 
5875*8fce2fd6Sachartre 		if (strcmp(s + 1, "disk") == 0) {
5876*8fce2fd6Sachartre 
5877*8fce2fd6Sachartre 			vds->driver_types[i].type = VD_DRIVER_DISK;
5878*8fce2fd6Sachartre 
5879*8fce2fd6Sachartre 		} else if (strcmp(s + 1, "volume") == 0) {
5880*8fce2fd6Sachartre 
5881*8fce2fd6Sachartre 			vds->driver_types[i].type = VD_DRIVER_VOLUME;
5882*8fce2fd6Sachartre 
5883*8fce2fd6Sachartre 		} else {
5884*8fce2fd6Sachartre 			PRN("vds.conf: driver-type-list, entry %d (%s): "
5885*8fce2fd6Sachartre 			    "the driver type is invalid",
5886*8fce2fd6Sachartre 			    i, list[i]);
5887*8fce2fd6Sachartre 			continue;
5888*8fce2fd6Sachartre 		}
5889*8fce2fd6Sachartre 
5890*8fce2fd6Sachartre 		(void) strncpy(vds->driver_types[i].name, list[i], len);
5891*8fce2fd6Sachartre 
5892*8fce2fd6Sachartre 		PR0("driver-type-list, entry %d (%s) added",
5893*8fce2fd6Sachartre 		    i, list[i]);
5894*8fce2fd6Sachartre 
5895*8fce2fd6Sachartre 		count++;
5896*8fce2fd6Sachartre 	}
5897*8fce2fd6Sachartre 
5898*8fce2fd6Sachartre 	ddi_prop_free(list);
5899*8fce2fd6Sachartre 
5900*8fce2fd6Sachartre 	if (count == 0) {
5901*8fce2fd6Sachartre 		/* nothing was added, clean up */
5902*8fce2fd6Sachartre 		vds_driver_types_free(vds);
5903*8fce2fd6Sachartre 	}
5904*8fce2fd6Sachartre }
5905*8fce2fd6Sachartre 
5906*8fce2fd6Sachartre static void
59071ae08745Sheppo vds_add_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
59081ae08745Sheppo {
5909e1ebb9ecSlm66018 	char		*device_path = NULL;
5910047ba61eSachartre 	uint64_t	id = 0, ldc_id = 0, options = 0;
59111ae08745Sheppo 
59121ae08745Sheppo 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
59131ae08745Sheppo 		PRN("Error getting vdisk \"%s\"", VD_ID_PROP);
59141ae08745Sheppo 		return;
59151ae08745Sheppo 	}
59161ae08745Sheppo 	PR0("Adding vdisk ID %lu", id);
59171ae08745Sheppo 	if (md_get_prop_str(md, vd_node, VD_BLOCK_DEVICE_PROP,
5918e1ebb9ecSlm66018 	    &device_path) != 0) {
59191ae08745Sheppo 		PRN("Error getting vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
59201ae08745Sheppo 		return;
59211ae08745Sheppo 	}
59221ae08745Sheppo 
5923047ba61eSachartre 	vds_get_options(md, vd_node, &options);
5924047ba61eSachartre 
59251ae08745Sheppo 	if (vds_get_ldc_id(md, vd_node, &ldc_id) != 0) {
59261ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", id);
59271ae08745Sheppo 		return;
59281ae08745Sheppo 	}
59291ae08745Sheppo 
5930047ba61eSachartre 	if (vds_init_vd(vds, id, device_path, options, ldc_id) != 0) {
59311ae08745Sheppo 		PRN("Failed to add vdisk ID %lu", id);
593217cadca8Slm66018 		if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0)
593317cadca8Slm66018 			PRN("No vDisk entry found for vdisk ID %lu", id);
59341ae08745Sheppo 		return;
59351ae08745Sheppo 	}
59361ae08745Sheppo }
59371ae08745Sheppo 
59381ae08745Sheppo static void
59391ae08745Sheppo vds_remove_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
59401ae08745Sheppo {
59411ae08745Sheppo 	uint64_t	id = 0;
59421ae08745Sheppo 
59431ae08745Sheppo 
59441ae08745Sheppo 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
59451ae08745Sheppo 		PRN("Unable to get \"%s\" property from vdisk's MD node",
59461ae08745Sheppo 		    VD_ID_PROP);
59471ae08745Sheppo 		return;
59481ae08745Sheppo 	}
59491ae08745Sheppo 	PR0("Removing vdisk ID %lu", id);
59501ae08745Sheppo 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0)
59511ae08745Sheppo 		PRN("No vdisk entry found for vdisk ID %lu", id);
59521ae08745Sheppo }
59531ae08745Sheppo 
59541ae08745Sheppo static void
59551ae08745Sheppo vds_change_vd(vds_t *vds, md_t *prev_md, mde_cookie_t prev_vd_node,
59561ae08745Sheppo     md_t *curr_md, mde_cookie_t curr_vd_node)
59571ae08745Sheppo {
59581ae08745Sheppo 	char		*curr_dev, *prev_dev;
5959047ba61eSachartre 	uint64_t	curr_id = 0, curr_ldc_id = 0, curr_options = 0;
5960047ba61eSachartre 	uint64_t	prev_id = 0, prev_ldc_id = 0, prev_options = 0;
59611ae08745Sheppo 	size_t		len;
59621ae08745Sheppo 
59631ae08745Sheppo 
59641ae08745Sheppo 	/* Validate that vdisk ID has not changed */
59651ae08745Sheppo 	if (md_get_prop_val(prev_md, prev_vd_node, VD_ID_PROP, &prev_id) != 0) {
59661ae08745Sheppo 		PRN("Error getting previous vdisk \"%s\" property",
59671ae08745Sheppo 		    VD_ID_PROP);
59681ae08745Sheppo 		return;
59691ae08745Sheppo 	}
59701ae08745Sheppo 	if (md_get_prop_val(curr_md, curr_vd_node, VD_ID_PROP, &curr_id) != 0) {
59711ae08745Sheppo 		PRN("Error getting current vdisk \"%s\" property", VD_ID_PROP);
59721ae08745Sheppo 		return;
59731ae08745Sheppo 	}
59741ae08745Sheppo 	if (curr_id != prev_id) {
59751ae08745Sheppo 		PRN("Not changing vdisk:  ID changed from %lu to %lu",
59761ae08745Sheppo 		    prev_id, curr_id);
59771ae08745Sheppo 		return;
59781ae08745Sheppo 	}
59791ae08745Sheppo 
59801ae08745Sheppo 	/* Validate that LDC ID has not changed */
59811ae08745Sheppo 	if (vds_get_ldc_id(prev_md, prev_vd_node, &prev_ldc_id) != 0) {
59821ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", prev_id);
59831ae08745Sheppo 		return;
59841ae08745Sheppo 	}
59851ae08745Sheppo 
59861ae08745Sheppo 	if (vds_get_ldc_id(curr_md, curr_vd_node, &curr_ldc_id) != 0) {
59871ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", curr_id);
59881ae08745Sheppo 		return;
59891ae08745Sheppo 	}
59901ae08745Sheppo 	if (curr_ldc_id != prev_ldc_id) {
59910a55fbb7Slm66018 		_NOTE(NOTREACHED);	/* lint is confused */
59921ae08745Sheppo 		PRN("Not changing vdisk:  "
59931ae08745Sheppo 		    "LDC ID changed from %lu to %lu", prev_ldc_id, curr_ldc_id);
59941ae08745Sheppo 		return;
59951ae08745Sheppo 	}
59961ae08745Sheppo 
59971ae08745Sheppo 	/* Determine whether device path has changed */
59981ae08745Sheppo 	if (md_get_prop_str(prev_md, prev_vd_node, VD_BLOCK_DEVICE_PROP,
59991ae08745Sheppo 	    &prev_dev) != 0) {
60001ae08745Sheppo 		PRN("Error getting previous vdisk \"%s\"",
60011ae08745Sheppo 		    VD_BLOCK_DEVICE_PROP);
60021ae08745Sheppo 		return;
60031ae08745Sheppo 	}
60041ae08745Sheppo 	if (md_get_prop_str(curr_md, curr_vd_node, VD_BLOCK_DEVICE_PROP,
60051ae08745Sheppo 	    &curr_dev) != 0) {
60061ae08745Sheppo 		PRN("Error getting current vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
60071ae08745Sheppo 		return;
60081ae08745Sheppo 	}
60091ae08745Sheppo 	if (((len = strlen(curr_dev)) == strlen(prev_dev)) &&
60101ae08745Sheppo 	    (strncmp(curr_dev, prev_dev, len) == 0))
60111ae08745Sheppo 		return;	/* no relevant (supported) change */
60121ae08745Sheppo 
6013047ba61eSachartre 	/* Validate that options have not changed */
6014047ba61eSachartre 	vds_get_options(prev_md, prev_vd_node, &prev_options);
6015047ba61eSachartre 	vds_get_options(curr_md, curr_vd_node, &curr_options);
6016047ba61eSachartre 	if (prev_options != curr_options) {
6017047ba61eSachartre 		PRN("Not changing vdisk:  options changed from %lx to %lx",
6018047ba61eSachartre 		    prev_options, curr_options);
6019047ba61eSachartre 		return;
6020047ba61eSachartre 	}
6021047ba61eSachartre 
60221ae08745Sheppo 	PR0("Changing vdisk ID %lu", prev_id);
60233af08d82Slm66018 
60241ae08745Sheppo 	/* Remove old state, which will close vdisk and reset */
60251ae08745Sheppo 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)prev_id) != 0)
60261ae08745Sheppo 		PRN("No entry found for vdisk ID %lu", prev_id);
60273af08d82Slm66018 
60281ae08745Sheppo 	/* Re-initialize vdisk with new state */
6029047ba61eSachartre 	if (vds_init_vd(vds, curr_id, curr_dev, curr_options,
6030047ba61eSachartre 	    curr_ldc_id) != 0) {
60311ae08745Sheppo 		PRN("Failed to change vdisk ID %lu", curr_id);
60321ae08745Sheppo 		return;
60331ae08745Sheppo 	}
60341ae08745Sheppo }
60351ae08745Sheppo 
60361ae08745Sheppo static int
60371ae08745Sheppo vds_process_md(void *arg, mdeg_result_t *md)
60381ae08745Sheppo {
60391ae08745Sheppo 	int	i;
60401ae08745Sheppo 	vds_t	*vds = arg;
60411ae08745Sheppo 
60421ae08745Sheppo 
60431ae08745Sheppo 	if (md == NULL)
60441ae08745Sheppo 		return (MDEG_FAILURE);
60451ae08745Sheppo 	ASSERT(vds != NULL);
60461ae08745Sheppo 
60471ae08745Sheppo 	for (i = 0; i < md->removed.nelem; i++)
60481ae08745Sheppo 		vds_remove_vd(vds, md->removed.mdp, md->removed.mdep[i]);
60491ae08745Sheppo 	for (i = 0; i < md->match_curr.nelem; i++)
60501ae08745Sheppo 		vds_change_vd(vds, md->match_prev.mdp, md->match_prev.mdep[i],
60511ae08745Sheppo 		    md->match_curr.mdp, md->match_curr.mdep[i]);
60521ae08745Sheppo 	for (i = 0; i < md->added.nelem; i++)
60531ae08745Sheppo 		vds_add_vd(vds, md->added.mdp, md->added.mdep[i]);
60541ae08745Sheppo 
60551ae08745Sheppo 	return (MDEG_SUCCESS);
60561ae08745Sheppo }
60571ae08745Sheppo 
60583c96341aSnarayan 
60591ae08745Sheppo static int
60601ae08745Sheppo vds_do_attach(dev_info_t *dip)
60611ae08745Sheppo {
6062445b4c2eSsb155480 	int			status, sz;
6063445b4c2eSsb155480 	int			cfg_handle;
60641ae08745Sheppo 	minor_t			instance = ddi_get_instance(dip);
60651ae08745Sheppo 	vds_t			*vds;
6066445b4c2eSsb155480 	mdeg_prop_spec_t	*pspecp;
6067445b4c2eSsb155480 	mdeg_node_spec_t	*ispecp;
60681ae08745Sheppo 
60691ae08745Sheppo 	/*
60701ae08745Sheppo 	 * The "cfg-handle" property of a vds node in an MD contains the MD's
60711ae08745Sheppo 	 * notion of "instance", or unique identifier, for that node; OBP
60721ae08745Sheppo 	 * stores the value of the "cfg-handle" MD property as the value of
60731ae08745Sheppo 	 * the "reg" property on the node in the device tree it builds from
60741ae08745Sheppo 	 * the MD and passes to Solaris.  Thus, we look up the devinfo node's
60751ae08745Sheppo 	 * "reg" property value to uniquely identify this device instance when
60761ae08745Sheppo 	 * registering with the MD event-generation framework.  If the "reg"
60771ae08745Sheppo 	 * property cannot be found, the device tree state is presumably so
60781ae08745Sheppo 	 * broken that there is no point in continuing.
60791ae08745Sheppo 	 */
6080445b4c2eSsb155480 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
6081445b4c2eSsb155480 	    VD_REG_PROP)) {
6082445b4c2eSsb155480 		PRN("vds \"%s\" property does not exist", VD_REG_PROP);
60831ae08745Sheppo 		return (DDI_FAILURE);
60841ae08745Sheppo 	}
60851ae08745Sheppo 
60861ae08745Sheppo 	/* Get the MD instance for later MDEG registration */
60871ae08745Sheppo 	cfg_handle = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
6088445b4c2eSsb155480 	    VD_REG_PROP, -1);
60891ae08745Sheppo 
60901ae08745Sheppo 	if (ddi_soft_state_zalloc(vds_state, instance) != DDI_SUCCESS) {
60911ae08745Sheppo 		PRN("Could not allocate state for instance %u", instance);
60921ae08745Sheppo 		return (DDI_FAILURE);
60931ae08745Sheppo 	}
60941ae08745Sheppo 
60951ae08745Sheppo 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
60961ae08745Sheppo 		PRN("Could not get state for instance %u", instance);
60971ae08745Sheppo 		ddi_soft_state_free(vds_state, instance);
60981ae08745Sheppo 		return (DDI_FAILURE);
60991ae08745Sheppo 	}
61001ae08745Sheppo 
61011ae08745Sheppo 	vds->dip	= dip;
61021ae08745Sheppo 	vds->vd_table	= mod_hash_create_ptrhash("vds_vd_table", VDS_NCHAINS,
610387a7269eSachartre 	    vds_destroy_vd, sizeof (void *));
610487a7269eSachartre 
61051ae08745Sheppo 	ASSERT(vds->vd_table != NULL);
61061ae08745Sheppo 
61071ae08745Sheppo 	if ((status = ldi_ident_from_dip(dip, &vds->ldi_ident)) != 0) {
61081ae08745Sheppo 		PRN("ldi_ident_from_dip() returned errno %d", status);
61091ae08745Sheppo 		return (DDI_FAILURE);
61101ae08745Sheppo 	}
61111ae08745Sheppo 	vds->initialized |= VDS_LDI;
61121ae08745Sheppo 
61131ae08745Sheppo 	/* Register for MD updates */
6114445b4c2eSsb155480 	sz = sizeof (vds_prop_template);
6115445b4c2eSsb155480 	pspecp = kmem_alloc(sz, KM_SLEEP);
6116445b4c2eSsb155480 	bcopy(vds_prop_template, pspecp, sz);
6117445b4c2eSsb155480 
6118445b4c2eSsb155480 	VDS_SET_MDEG_PROP_INST(pspecp, cfg_handle);
6119445b4c2eSsb155480 
6120445b4c2eSsb155480 	/* initialize the complete prop spec structure */
6121445b4c2eSsb155480 	ispecp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP);
6122445b4c2eSsb155480 	ispecp->namep = "virtual-device";
6123445b4c2eSsb155480 	ispecp->specp = pspecp;
6124445b4c2eSsb155480 
6125445b4c2eSsb155480 	if (mdeg_register(ispecp, &vd_match, vds_process_md, vds,
61261ae08745Sheppo 	    &vds->mdeg) != MDEG_SUCCESS) {
61271ae08745Sheppo 		PRN("Unable to register for MD updates");
6128445b4c2eSsb155480 		kmem_free(ispecp, sizeof (mdeg_node_spec_t));
6129445b4c2eSsb155480 		kmem_free(pspecp, sz);
61301ae08745Sheppo 		return (DDI_FAILURE);
61311ae08745Sheppo 	}
6132445b4c2eSsb155480 
6133445b4c2eSsb155480 	vds->ispecp = ispecp;
61341ae08745Sheppo 	vds->initialized |= VDS_MDEG;
61351ae08745Sheppo 
61360a55fbb7Slm66018 	/* Prevent auto-detaching so driver is available whenever MD changes */
61370a55fbb7Slm66018 	if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1) !=
61380a55fbb7Slm66018 	    DDI_PROP_SUCCESS) {
61390a55fbb7Slm66018 		PRN("failed to set \"%s\" property for instance %u",
61400a55fbb7Slm66018 		    DDI_NO_AUTODETACH, instance);
61410a55fbb7Slm66018 	}
61420a55fbb7Slm66018 
6143*8fce2fd6Sachartre 	/* read any user defined driver types from conf file and update list */
6144*8fce2fd6Sachartre 	vds_driver_types_update(vds);
6145*8fce2fd6Sachartre 
61461ae08745Sheppo 	ddi_report_dev(dip);
61471ae08745Sheppo 	return (DDI_SUCCESS);
61481ae08745Sheppo }
61491ae08745Sheppo 
61501ae08745Sheppo static int
61511ae08745Sheppo vds_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
61521ae08745Sheppo {
61531ae08745Sheppo 	int	status;
61541ae08745Sheppo 
61551ae08745Sheppo 	switch (cmd) {
61561ae08745Sheppo 	case DDI_ATTACH:
6157d10e4ef2Snarayan 		PR0("Attaching");
61581ae08745Sheppo 		if ((status = vds_do_attach(dip)) != DDI_SUCCESS)
61591ae08745Sheppo 			(void) vds_detach(dip, DDI_DETACH);
61601ae08745Sheppo 		return (status);
61611ae08745Sheppo 	case DDI_RESUME:
6162d10e4ef2Snarayan 		PR0("No action required for DDI_RESUME");
61631ae08745Sheppo 		return (DDI_SUCCESS);
61641ae08745Sheppo 	default:
61651ae08745Sheppo 		return (DDI_FAILURE);
61661ae08745Sheppo 	}
61671ae08745Sheppo }
61681ae08745Sheppo 
61691ae08745Sheppo static struct dev_ops vds_ops = {
61701ae08745Sheppo 	DEVO_REV,	/* devo_rev */
61711ae08745Sheppo 	0,		/* devo_refcnt */
61721ae08745Sheppo 	ddi_no_info,	/* devo_getinfo */
61731ae08745Sheppo 	nulldev,	/* devo_identify */
61741ae08745Sheppo 	nulldev,	/* devo_probe */
61751ae08745Sheppo 	vds_attach,	/* devo_attach */
61761ae08745Sheppo 	vds_detach,	/* devo_detach */
61771ae08745Sheppo 	nodev,		/* devo_reset */
61781ae08745Sheppo 	NULL,		/* devo_cb_ops */
61791ae08745Sheppo 	NULL,		/* devo_bus_ops */
61801ae08745Sheppo 	nulldev		/* devo_power */
61811ae08745Sheppo };
61821ae08745Sheppo 
61831ae08745Sheppo static struct modldrv modldrv = {
61841ae08745Sheppo 	&mod_driverops,
6185205eeb1aSlm66018 	"virtual disk server",
61861ae08745Sheppo 	&vds_ops,
61871ae08745Sheppo };
61881ae08745Sheppo 
61891ae08745Sheppo static struct modlinkage modlinkage = {
61901ae08745Sheppo 	MODREV_1,
61911ae08745Sheppo 	&modldrv,
61921ae08745Sheppo 	NULL
61931ae08745Sheppo };
61941ae08745Sheppo 
61951ae08745Sheppo 
61961ae08745Sheppo int
61971ae08745Sheppo _init(void)
61981ae08745Sheppo {
619917cadca8Slm66018 	int		status;
6200d10e4ef2Snarayan 
62011ae08745Sheppo 	if ((status = ddi_soft_state_init(&vds_state, sizeof (vds_t), 1)) != 0)
62021ae08745Sheppo 		return (status);
620317cadca8Slm66018 
62041ae08745Sheppo 	if ((status = mod_install(&modlinkage)) != 0) {
62051ae08745Sheppo 		ddi_soft_state_fini(&vds_state);
62061ae08745Sheppo 		return (status);
62071ae08745Sheppo 	}
62081ae08745Sheppo 
62091ae08745Sheppo 	return (0);
62101ae08745Sheppo }
62111ae08745Sheppo 
62121ae08745Sheppo int
62131ae08745Sheppo _info(struct modinfo *modinfop)
62141ae08745Sheppo {
62151ae08745Sheppo 	return (mod_info(&modlinkage, modinfop));
62161ae08745Sheppo }
62171ae08745Sheppo 
62181ae08745Sheppo int
62191ae08745Sheppo _fini(void)
62201ae08745Sheppo {
62211ae08745Sheppo 	int	status;
62221ae08745Sheppo 
62231ae08745Sheppo 	if ((status = mod_remove(&modlinkage)) != 0)
62241ae08745Sheppo 		return (status);
62251ae08745Sheppo 	ddi_soft_state_fini(&vds_state);
62261ae08745Sheppo 	return (0);
62271ae08745Sheppo }
6228