xref: /titanic_53/usr/src/uts/sun4v/io/vds.c (revision 17cadca83cc82e37ff517ea2783eb4bfcc07b950)
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 /*
233c96341aSnarayan  * Copyright 2007 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>
40*17cadca8Slm66018 #include <sys/fs/hsfs_isospec.h>
411ae08745Sheppo #include <sys/mdeg.h>
421ae08745Sheppo #include <sys/modhash.h>
431ae08745Sheppo #include <sys/note.h>
441ae08745Sheppo #include <sys/pathname.h>
45205eeb1aSlm66018 #include <sys/sdt.h>
461ae08745Sheppo #include <sys/sunddi.h>
471ae08745Sheppo #include <sys/sunldi.h>
481ae08745Sheppo #include <sys/sysmacros.h>
491ae08745Sheppo #include <sys/vio_common.h>
50*17cadca8Slm66018 #include <sys/vio_util.h>
511ae08745Sheppo #include <sys/vdsk_mailbox.h>
521ae08745Sheppo #include <sys/vdsk_common.h>
531ae08745Sheppo #include <sys/vtoc.h>
543c96341aSnarayan #include <sys/vfs.h>
553c96341aSnarayan #include <sys/stat.h>
5687a7269eSachartre #include <sys/scsi/impl/uscsi.h>
57690555a1Sachartre #include <vm/seg_map.h>
581ae08745Sheppo 
591ae08745Sheppo /* Virtual disk server initialization flags */
60d10e4ef2Snarayan #define	VDS_LDI			0x01
61d10e4ef2Snarayan #define	VDS_MDEG		0x02
621ae08745Sheppo 
631ae08745Sheppo /* Virtual disk server tunable parameters */
643c96341aSnarayan #define	VDS_RETRIES		5
653c96341aSnarayan #define	VDS_LDC_DELAY		1000 /* 1 msecs */
663c96341aSnarayan #define	VDS_DEV_DELAY		10000000 /* 10 secs */
671ae08745Sheppo #define	VDS_NCHAINS		32
681ae08745Sheppo 
691ae08745Sheppo /* Identification parameters for MD, synthetic dkio(7i) structures, etc. */
701ae08745Sheppo #define	VDS_NAME		"virtual-disk-server"
711ae08745Sheppo 
721ae08745Sheppo #define	VD_NAME			"vd"
731ae08745Sheppo #define	VD_VOLUME_NAME		"vdisk"
741ae08745Sheppo #define	VD_ASCIILABEL		"Virtual Disk"
751ae08745Sheppo 
761ae08745Sheppo #define	VD_CHANNEL_ENDPOINT	"channel-endpoint"
771ae08745Sheppo #define	VD_ID_PROP		"id"
781ae08745Sheppo #define	VD_BLOCK_DEVICE_PROP	"vds-block-device"
79047ba61eSachartre #define	VD_BLOCK_DEVICE_OPTS	"vds-block-device-opts"
80445b4c2eSsb155480 #define	VD_REG_PROP		"reg"
811ae08745Sheppo 
821ae08745Sheppo /* Virtual disk initialization flags */
833c96341aSnarayan #define	VD_DISK_READY		0x01
843c96341aSnarayan #define	VD_LOCKING		0x02
853c96341aSnarayan #define	VD_LDC			0x04
863c96341aSnarayan #define	VD_DRING		0x08
873c96341aSnarayan #define	VD_SID			0x10
883c96341aSnarayan #define	VD_SEQ_NUM		0x20
89047ba61eSachartre #define	VD_SETUP_ERROR		0x40
901ae08745Sheppo 
91eba0cb4eSachartre /* Flags for writing to a vdisk which is a file */
92eba0cb4eSachartre #define	VD_FILE_WRITE_FLAGS	SM_ASYNC
93eba0cb4eSachartre 
9487a7269eSachartre /* Number of backup labels */
9587a7269eSachartre #define	VD_FILE_NUM_BACKUP	5
9687a7269eSachartre 
9787a7269eSachartre /* Timeout for SCSI I/O */
9887a7269eSachartre #define	VD_SCSI_RDWR_TIMEOUT	30	/* 30 secs */
9987a7269eSachartre 
1001ae08745Sheppo /*
1011ae08745Sheppo  * By Solaris convention, slice/partition 2 represents the entire disk;
1021ae08745Sheppo  * unfortunately, this convention does not appear to be codified.
1031ae08745Sheppo  */
1041ae08745Sheppo #define	VD_ENTIRE_DISK_SLICE	2
1051ae08745Sheppo 
1061ae08745Sheppo /* Return a cpp token as a string */
1071ae08745Sheppo #define	STRINGIZE(token)	#token
1081ae08745Sheppo 
1091ae08745Sheppo /*
1101ae08745Sheppo  * Print a message prefixed with the current function name to the message log
1111ae08745Sheppo  * (and optionally to the console for verbose boots); these macros use cpp's
1121ae08745Sheppo  * concatenation of string literals and C99 variable-length-argument-list
1131ae08745Sheppo  * macros
1141ae08745Sheppo  */
1151ae08745Sheppo #define	PRN(...)	_PRN("?%s():  "__VA_ARGS__, "")
1161ae08745Sheppo #define	_PRN(format, ...)					\
1171ae08745Sheppo 	cmn_err(CE_CONT, format"%s", __func__, __VA_ARGS__)
1181ae08745Sheppo 
1191ae08745Sheppo /* Return a pointer to the "i"th vdisk dring element */
1201ae08745Sheppo #define	VD_DRING_ELEM(i)	((vd_dring_entry_t *)(void *)	\
1211ae08745Sheppo 	    (vd->dring + (i)*vd->descriptor_size))
1221ae08745Sheppo 
1231ae08745Sheppo /* Return the virtual disk client's type as a string (for use in messages) */
1241ae08745Sheppo #define	VD_CLIENT(vd)							\
1251ae08745Sheppo 	(((vd)->xfer_mode == VIO_DESC_MODE) ? "in-band client" :	\
1261ae08745Sheppo 	    (((vd)->xfer_mode == VIO_DRING_MODE) ? "dring client" :	\
1271ae08745Sheppo 		(((vd)->xfer_mode == 0) ? "null client" :		\
1281ae08745Sheppo 		    "unsupported client")))
1291ae08745Sheppo 
130690555a1Sachartre /* Read disk label from a disk on file */
131690555a1Sachartre #define	VD_FILE_LABEL_READ(vd, labelp) \
13287a7269eSachartre 	vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)labelp, \
133690555a1Sachartre 	    0, sizeof (struct dk_label))
134690555a1Sachartre 
135690555a1Sachartre /* Write disk label to a disk on file */
136690555a1Sachartre #define	VD_FILE_LABEL_WRITE(vd, labelp)	\
13787a7269eSachartre 	vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)labelp, \
138690555a1Sachartre 	    0, sizeof (struct dk_label))
139690555a1Sachartre 
140445b4c2eSsb155480 /*
141445b4c2eSsb155480  * Specification of an MD node passed to the MDEG to filter any
142445b4c2eSsb155480  * 'vport' nodes that do not belong to the specified node. This
143445b4c2eSsb155480  * template is copied for each vds instance and filled in with
144445b4c2eSsb155480  * the appropriate 'cfg-handle' value before being passed to the MDEG.
145445b4c2eSsb155480  */
146445b4c2eSsb155480 static mdeg_prop_spec_t	vds_prop_template[] = {
147445b4c2eSsb155480 	{ MDET_PROP_STR,	"name",		VDS_NAME },
148445b4c2eSsb155480 	{ MDET_PROP_VAL,	"cfg-handle",	NULL },
149445b4c2eSsb155480 	{ MDET_LIST_END,	NULL, 		NULL }
150445b4c2eSsb155480 };
151445b4c2eSsb155480 
152445b4c2eSsb155480 #define	VDS_SET_MDEG_PROP_INST(specp, val) (specp)[1].ps_val = (val);
153445b4c2eSsb155480 
154445b4c2eSsb155480 /*
155445b4c2eSsb155480  * Matching criteria passed to the MDEG to register interest
156445b4c2eSsb155480  * in changes to 'virtual-device-port' nodes identified by their
157445b4c2eSsb155480  * 'id' property.
158445b4c2eSsb155480  */
159445b4c2eSsb155480 static md_prop_match_t	vd_prop_match[] = {
160445b4c2eSsb155480 	{ MDET_PROP_VAL,	VD_ID_PROP },
161445b4c2eSsb155480 	{ MDET_LIST_END,	NULL }
162445b4c2eSsb155480 };
163445b4c2eSsb155480 
164445b4c2eSsb155480 static mdeg_node_match_t vd_match = {"virtual-device-port",
165445b4c2eSsb155480 				    vd_prop_match};
166445b4c2eSsb155480 
167047ba61eSachartre /*
168047ba61eSachartre  * Options for the VD_BLOCK_DEVICE_OPTS property.
169047ba61eSachartre  */
170047ba61eSachartre #define	VD_OPT_RDONLY		0x1	/* read-only  */
171047ba61eSachartre #define	VD_OPT_SLICE		0x2	/* single slice */
172047ba61eSachartre #define	VD_OPT_EXCLUSIVE	0x4	/* exclusive access */
173047ba61eSachartre 
174047ba61eSachartre #define	VD_OPTION_NLEN	128
175047ba61eSachartre 
176047ba61eSachartre typedef struct vd_option {
177047ba61eSachartre 	char vdo_name[VD_OPTION_NLEN];
178047ba61eSachartre 	uint64_t vdo_value;
179047ba61eSachartre } vd_option_t;
180047ba61eSachartre 
181047ba61eSachartre vd_option_t vd_bdev_options[] = {
182047ba61eSachartre 	{ "ro",		VD_OPT_RDONLY },
183047ba61eSachartre 	{ "slice", 	VD_OPT_SLICE },
184047ba61eSachartre 	{ "excl",	VD_OPT_EXCLUSIVE }
185047ba61eSachartre };
186047ba61eSachartre 
1871ae08745Sheppo /* Debugging macros */
1881ae08745Sheppo #ifdef DEBUG
1893af08d82Slm66018 
1903af08d82Slm66018 static int	vd_msglevel = 0;
1913af08d82Slm66018 
1921ae08745Sheppo #define	PR0 if (vd_msglevel > 0)	PRN
1931ae08745Sheppo #define	PR1 if (vd_msglevel > 1)	PRN
1941ae08745Sheppo #define	PR2 if (vd_msglevel > 2)	PRN
1951ae08745Sheppo 
1961ae08745Sheppo #define	VD_DUMP_DRING_ELEM(elem)					\
1973c96341aSnarayan 	PR0("dst:%x op:%x st:%u nb:%lx addr:%lx ncook:%u\n",		\
1981ae08745Sheppo 	    elem->hdr.dstate,						\
1991ae08745Sheppo 	    elem->payload.operation,					\
2001ae08745Sheppo 	    elem->payload.status,					\
2011ae08745Sheppo 	    elem->payload.nbytes,					\
2021ae08745Sheppo 	    elem->payload.addr,						\
2031ae08745Sheppo 	    elem->payload.ncookies);
2041ae08745Sheppo 
2053af08d82Slm66018 char *
2063af08d82Slm66018 vd_decode_state(int state)
2073af08d82Slm66018 {
2083af08d82Slm66018 	char *str;
2093af08d82Slm66018 
2103af08d82Slm66018 #define	CASE_STATE(_s)	case _s: str = #_s; break;
2113af08d82Slm66018 
2123af08d82Slm66018 	switch (state) {
2133af08d82Slm66018 	CASE_STATE(VD_STATE_INIT)
2143af08d82Slm66018 	CASE_STATE(VD_STATE_VER)
2153af08d82Slm66018 	CASE_STATE(VD_STATE_ATTR)
2163af08d82Slm66018 	CASE_STATE(VD_STATE_DRING)
2173af08d82Slm66018 	CASE_STATE(VD_STATE_RDX)
2183af08d82Slm66018 	CASE_STATE(VD_STATE_DATA)
2193af08d82Slm66018 	default: str = "unknown"; break;
2203af08d82Slm66018 	}
2213af08d82Slm66018 
2223af08d82Slm66018 #undef CASE_STATE
2233af08d82Slm66018 
2243af08d82Slm66018 	return (str);
2253af08d82Slm66018 }
2263af08d82Slm66018 
2273af08d82Slm66018 void
2283af08d82Slm66018 vd_decode_tag(vio_msg_t *msg)
2293af08d82Slm66018 {
2303af08d82Slm66018 	char *tstr, *sstr, *estr;
2313af08d82Slm66018 
2323af08d82Slm66018 #define	CASE_TYPE(_s)	case _s: tstr = #_s; break;
2333af08d82Slm66018 
2343af08d82Slm66018 	switch (msg->tag.vio_msgtype) {
2353af08d82Slm66018 	CASE_TYPE(VIO_TYPE_CTRL)
2363af08d82Slm66018 	CASE_TYPE(VIO_TYPE_DATA)
2373af08d82Slm66018 	CASE_TYPE(VIO_TYPE_ERR)
2383af08d82Slm66018 	default: tstr = "unknown"; break;
2393af08d82Slm66018 	}
2403af08d82Slm66018 
2413af08d82Slm66018 #undef CASE_TYPE
2423af08d82Slm66018 
2433af08d82Slm66018 #define	CASE_SUBTYPE(_s) case _s: sstr = #_s; break;
2443af08d82Slm66018 
2453af08d82Slm66018 	switch (msg->tag.vio_subtype) {
2463af08d82Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_INFO)
2473af08d82Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_ACK)
2483af08d82Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_NACK)
2493af08d82Slm66018 	default: sstr = "unknown"; break;
2503af08d82Slm66018 	}
2513af08d82Slm66018 
2523af08d82Slm66018 #undef CASE_SUBTYPE
2533af08d82Slm66018 
2543af08d82Slm66018 #define	CASE_ENV(_s)	case _s: estr = #_s; break;
2553af08d82Slm66018 
2563af08d82Slm66018 	switch (msg->tag.vio_subtype_env) {
2573af08d82Slm66018 	CASE_ENV(VIO_VER_INFO)
2583af08d82Slm66018 	CASE_ENV(VIO_ATTR_INFO)
2593af08d82Slm66018 	CASE_ENV(VIO_DRING_REG)
2603af08d82Slm66018 	CASE_ENV(VIO_DRING_UNREG)
2613af08d82Slm66018 	CASE_ENV(VIO_RDX)
2623af08d82Slm66018 	CASE_ENV(VIO_PKT_DATA)
2633af08d82Slm66018 	CASE_ENV(VIO_DESC_DATA)
2643af08d82Slm66018 	CASE_ENV(VIO_DRING_DATA)
2653af08d82Slm66018 	default: estr = "unknown"; break;
2663af08d82Slm66018 	}
2673af08d82Slm66018 
2683af08d82Slm66018 #undef CASE_ENV
2693af08d82Slm66018 
2703af08d82Slm66018 	PR1("(%x/%x/%x) message : (%s/%s/%s)",
2713af08d82Slm66018 	    msg->tag.vio_msgtype, msg->tag.vio_subtype,
2723af08d82Slm66018 	    msg->tag.vio_subtype_env, tstr, sstr, estr);
2733af08d82Slm66018 }
2743af08d82Slm66018 
2751ae08745Sheppo #else	/* !DEBUG */
2763af08d82Slm66018 
2771ae08745Sheppo #define	PR0(...)
2781ae08745Sheppo #define	PR1(...)
2791ae08745Sheppo #define	PR2(...)
2801ae08745Sheppo 
2811ae08745Sheppo #define	VD_DUMP_DRING_ELEM(elem)
2821ae08745Sheppo 
2833af08d82Slm66018 #define	vd_decode_state(_s)	(NULL)
2843af08d82Slm66018 #define	vd_decode_tag(_s)	(NULL)
2853af08d82Slm66018 
2861ae08745Sheppo #endif	/* DEBUG */
2871ae08745Sheppo 
2881ae08745Sheppo 
289d10e4ef2Snarayan /*
290d10e4ef2Snarayan  * Soft state structure for a vds instance
291d10e4ef2Snarayan  */
2921ae08745Sheppo typedef struct vds {
2931ae08745Sheppo 	uint_t		initialized;	/* driver inst initialization flags */
2941ae08745Sheppo 	dev_info_t	*dip;		/* driver inst devinfo pointer */
2951ae08745Sheppo 	ldi_ident_t	ldi_ident;	/* driver's identifier for LDI */
2961ae08745Sheppo 	mod_hash_t	*vd_table;	/* table of virtual disks served */
297445b4c2eSsb155480 	mdeg_node_spec_t *ispecp;	/* mdeg node specification */
2981ae08745Sheppo 	mdeg_handle_t	mdeg;		/* handle for MDEG operations  */
2991ae08745Sheppo } vds_t;
3001ae08745Sheppo 
301d10e4ef2Snarayan /*
302d10e4ef2Snarayan  * Types of descriptor-processing tasks
303d10e4ef2Snarayan  */
304d10e4ef2Snarayan typedef enum vd_task_type {
305d10e4ef2Snarayan 	VD_NONFINAL_RANGE_TASK,	/* task for intermediate descriptor in range */
306d10e4ef2Snarayan 	VD_FINAL_RANGE_TASK,	/* task for last in a range of descriptors */
307d10e4ef2Snarayan } vd_task_type_t;
308d10e4ef2Snarayan 
309d10e4ef2Snarayan /*
310d10e4ef2Snarayan  * Structure describing the task for processing a descriptor
311d10e4ef2Snarayan  */
312d10e4ef2Snarayan typedef struct vd_task {
313d10e4ef2Snarayan 	struct vd		*vd;		/* vd instance task is for */
314d10e4ef2Snarayan 	vd_task_type_t		type;		/* type of descriptor task */
315d10e4ef2Snarayan 	int			index;		/* dring elem index for task */
316d10e4ef2Snarayan 	vio_msg_t		*msg;		/* VIO message task is for */
317d10e4ef2Snarayan 	size_t			msglen;		/* length of message content */
318d10e4ef2Snarayan 	vd_dring_payload_t	*request;	/* request task will perform */
319d10e4ef2Snarayan 	struct buf		buf;		/* buf(9s) for I/O request */
3204bac2208Snarayan 	ldc_mem_handle_t	mhdl;		/* task memory handle */
321205eeb1aSlm66018 	int			status;		/* status of processing task */
322205eeb1aSlm66018 	int	(*completef)(struct vd_task *task); /* completion func ptr */
323d10e4ef2Snarayan } vd_task_t;
324d10e4ef2Snarayan 
325d10e4ef2Snarayan /*
326d10e4ef2Snarayan  * Soft state structure for a virtual disk instance
327d10e4ef2Snarayan  */
3281ae08745Sheppo typedef struct vd {
3291ae08745Sheppo 	uint_t			initialized;	/* vdisk initialization flags */
330*17cadca8Slm66018 	uint64_t		operations;	/* bitmask of VD_OPs exported */
331*17cadca8Slm66018 	vio_ver_t		version;	/* ver negotiated with client */
3321ae08745Sheppo 	vds_t			*vds;		/* server for this vdisk */
333d10e4ef2Snarayan 	ddi_taskq_t		*startq;	/* queue for I/O start tasks */
334d10e4ef2Snarayan 	ddi_taskq_t		*completionq;	/* queue for completion tasks */
3351ae08745Sheppo 	ldi_handle_t		ldi_handle[V_NUMPAR];	/* LDI slice handles */
3363c96341aSnarayan 	char			device_path[MAXPATHLEN + 1]; /* vdisk device */
3371ae08745Sheppo 	dev_t			dev[V_NUMPAR];	/* dev numbers for slices */
338047ba61eSachartre 	int			open_flags;	/* open flags */
339e1ebb9ecSlm66018 	uint_t			nslices;	/* number of slices */
3401ae08745Sheppo 	size_t			vdisk_size;	/* number of blocks in vdisk */
341*17cadca8Slm66018 	size_t			vdisk_block_size; /* size of each vdisk block */
3421ae08745Sheppo 	vd_disk_type_t		vdisk_type;	/* slice or entire disk */
3434bac2208Snarayan 	vd_disk_label_t		vdisk_label;	/* EFI or VTOC label */
344*17cadca8Slm66018 	vd_media_t		vdisk_media;	/* media type of backing dev. */
345*17cadca8Slm66018 	boolean_t		is_atapi_dev;	/* Is this an IDE CD-ROM dev? */
346e1ebb9ecSlm66018 	ushort_t		max_xfer_sz;	/* max xfer size in DEV_BSIZE */
347*17cadca8Slm66018 	size_t			block_size;	/* blk size of actual device */
3481ae08745Sheppo 	boolean_t		pseudo;		/* underlying pseudo dev */
349*17cadca8Slm66018 	boolean_t		file;		/* is vDisk backed by a file? */
3503c96341aSnarayan 	vnode_t			*file_vnode;	/* file vnode */
3513c96341aSnarayan 	size_t			file_size;	/* file size */
35287a7269eSachartre 	ddi_devid_t		file_devid;	/* devid for disk image */
3534bac2208Snarayan 	struct dk_efi		dk_efi;		/* synthetic for slice type */
3541ae08745Sheppo 	struct dk_geom		dk_geom;	/* synthetic for slice type */
355*17cadca8Slm66018 	struct dk_minfo		dk_minfo;	/* synthetic for slice type */
3561ae08745Sheppo 	struct vtoc		vtoc;		/* synthetic for slice type */
3571ae08745Sheppo 	ldc_status_t		ldc_state;	/* LDC connection state */
3581ae08745Sheppo 	ldc_handle_t		ldc_handle;	/* handle for LDC comm */
3591ae08745Sheppo 	size_t			max_msglen;	/* largest LDC message len */
3601ae08745Sheppo 	vd_state_t		state;		/* client handshake state */
3611ae08745Sheppo 	uint8_t			xfer_mode;	/* transfer mode with client */
3621ae08745Sheppo 	uint32_t		sid;		/* client's session ID */
3631ae08745Sheppo 	uint64_t		seq_num;	/* message sequence number */
3641ae08745Sheppo 	uint64_t		dring_ident;	/* identifier of dring */
3651ae08745Sheppo 	ldc_dring_handle_t	dring_handle;	/* handle for dring ops */
3661ae08745Sheppo 	uint32_t		descriptor_size;	/* num bytes in desc */
3671ae08745Sheppo 	uint32_t		dring_len;	/* number of dring elements */
3681ae08745Sheppo 	caddr_t			dring;		/* address of dring */
3693af08d82Slm66018 	caddr_t			vio_msgp;	/* vio msg staging buffer */
370d10e4ef2Snarayan 	vd_task_t		inband_task;	/* task for inband descriptor */
371d10e4ef2Snarayan 	vd_task_t		*dring_task;	/* tasks dring elements */
372d10e4ef2Snarayan 
373d10e4ef2Snarayan 	kmutex_t		lock;		/* protects variables below */
374d10e4ef2Snarayan 	boolean_t		enabled;	/* is vdisk enabled? */
375d10e4ef2Snarayan 	boolean_t		reset_state;	/* reset connection state? */
376d10e4ef2Snarayan 	boolean_t		reset_ldc;	/* reset LDC channel? */
3771ae08745Sheppo } vd_t;
3781ae08745Sheppo 
3791ae08745Sheppo typedef struct vds_operation {
3803af08d82Slm66018 	char	*namep;
3811ae08745Sheppo 	uint8_t	operation;
382d10e4ef2Snarayan 	int	(*start)(vd_task_t *task);
383205eeb1aSlm66018 	int	(*complete)(vd_task_t *task);
3841ae08745Sheppo } vds_operation_t;
3851ae08745Sheppo 
3860a55fbb7Slm66018 typedef struct vd_ioctl {
3870a55fbb7Slm66018 	uint8_t		operation;		/* vdisk operation */
3880a55fbb7Slm66018 	const char	*operation_name;	/* vdisk operation name */
3890a55fbb7Slm66018 	size_t		nbytes;			/* size of operation buffer */
3900a55fbb7Slm66018 	int		cmd;			/* corresponding ioctl cmd */
3910a55fbb7Slm66018 	const char	*cmd_name;		/* ioctl cmd name */
3920a55fbb7Slm66018 	void		*arg;			/* ioctl cmd argument */
3930a55fbb7Slm66018 	/* convert input vd_buf to output ioctl_arg */
3940a55fbb7Slm66018 	void		(*copyin)(void *vd_buf, void *ioctl_arg);
3950a55fbb7Slm66018 	/* convert input ioctl_arg to output vd_buf */
3960a55fbb7Slm66018 	void		(*copyout)(void *ioctl_arg, void *vd_buf);
397047ba61eSachartre 	/* write is true if the operation writes any data to the backend */
398047ba61eSachartre 	boolean_t	write;
3990a55fbb7Slm66018 } vd_ioctl_t;
4000a55fbb7Slm66018 
4010a55fbb7Slm66018 /* Define trivial copyin/copyout conversion function flag */
4020a55fbb7Slm66018 #define	VD_IDENTITY	((void (*)(void *, void *))-1)
4031ae08745Sheppo 
4041ae08745Sheppo 
4053c96341aSnarayan static int	vds_ldc_retries = VDS_RETRIES;
4063af08d82Slm66018 static int	vds_ldc_delay = VDS_LDC_DELAY;
4073c96341aSnarayan static int	vds_dev_retries = VDS_RETRIES;
4083c96341aSnarayan static int	vds_dev_delay = VDS_DEV_DELAY;
4091ae08745Sheppo static void	*vds_state;
4101ae08745Sheppo 
411eba0cb4eSachartre static uint_t	vd_file_write_flags = VD_FILE_WRITE_FLAGS;
412eba0cb4eSachartre 
41387a7269eSachartre static short	vd_scsi_rdwr_timeout = VD_SCSI_RDWR_TIMEOUT;
41487a7269eSachartre 
4150a55fbb7Slm66018 /*
4160a55fbb7Slm66018  * Supported protocol version pairs, from highest (newest) to lowest (oldest)
4170a55fbb7Slm66018  *
4180a55fbb7Slm66018  * Each supported major version should appear only once, paired with (and only
4190a55fbb7Slm66018  * with) its highest supported minor version number (as the protocol requires
4200a55fbb7Slm66018  * supporting all lower minor version numbers as well)
4210a55fbb7Slm66018  */
422*17cadca8Slm66018 static const vio_ver_t	vds_version[] = {{1, 1}};
4230a55fbb7Slm66018 static const size_t	vds_num_versions =
4240a55fbb7Slm66018     sizeof (vds_version)/sizeof (vds_version[0]);
4250a55fbb7Slm66018 
4263af08d82Slm66018 static void vd_free_dring_task(vd_t *vdp);
4273c96341aSnarayan static int vd_setup_vd(vd_t *vd);
428047ba61eSachartre static int vd_setup_single_slice_disk(vd_t *vd);
4293c96341aSnarayan static boolean_t vd_enabled(vd_t *vd);
43078fcd0a1Sachartre static ushort_t vd_lbl2cksum(struct dk_label *label);
43178fcd0a1Sachartre static int vd_file_validate_geometry(vd_t *vd);
432*17cadca8Slm66018 static boolean_t vd_file_is_iso_image(vd_t *vd);
433*17cadca8Slm66018 static void vd_set_exported_operations(vd_t *vd);
434047ba61eSachartre 
435690555a1Sachartre /*
436690555a1Sachartre  * Function:
437690555a1Sachartre  *	vd_file_rw
438690555a1Sachartre  *
439690555a1Sachartre  * Description:
440690555a1Sachartre  * 	Read or write to a disk on file.
441690555a1Sachartre  *
442690555a1Sachartre  * Parameters:
443690555a1Sachartre  *	vd		- disk on which the operation is performed.
444690555a1Sachartre  *	slice		- slice on which the operation is performed,
44587a7269eSachartre  *			  VD_SLICE_NONE indicates that the operation
44687a7269eSachartre  *			  is done using an absolute disk offset.
447690555a1Sachartre  *	operation	- operation to execute: read (VD_OP_BREAD) or
448690555a1Sachartre  *			  write (VD_OP_BWRITE).
449690555a1Sachartre  *	data		- buffer where data are read to or written from.
450690555a1Sachartre  *	blk		- starting block for the operation.
451690555a1Sachartre  *	len		- number of bytes to read or write.
452690555a1Sachartre  *
453690555a1Sachartre  * Return Code:
454690555a1Sachartre  *	n >= 0		- success, n indicates the number of bytes read
455690555a1Sachartre  *			  or written.
456690555a1Sachartre  *	-1		- error.
457690555a1Sachartre  */
458690555a1Sachartre static ssize_t
459690555a1Sachartre vd_file_rw(vd_t *vd, int slice, int operation, caddr_t data, size_t blk,
460690555a1Sachartre     size_t len)
461690555a1Sachartre {
462690555a1Sachartre 	caddr_t	maddr;
463690555a1Sachartre 	size_t offset, maxlen, moffset, mlen, n;
464690555a1Sachartre 	uint_t smflags;
465690555a1Sachartre 	enum seg_rw srw;
466690555a1Sachartre 
467690555a1Sachartre 	ASSERT(vd->file);
468690555a1Sachartre 	ASSERT(len > 0);
469690555a1Sachartre 
470047ba61eSachartre 	/*
471047ba61eSachartre 	 * If a file is exported as a slice then we don't care about the vtoc.
472047ba61eSachartre 	 * In that case, the vtoc is a fake mainly to make newfs happy and we
473047ba61eSachartre 	 * handle any I/O as a raw disk access so that we can have access to the
474047ba61eSachartre 	 * entire backend.
475047ba61eSachartre 	 */
476047ba61eSachartre 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE || slice == VD_SLICE_NONE) {
477690555a1Sachartre 		/* raw disk access */
478690555a1Sachartre 		offset = blk * DEV_BSIZE;
479690555a1Sachartre 	} else {
480690555a1Sachartre 		ASSERT(slice >= 0 && slice < V_NUMPAR);
48178fcd0a1Sachartre 
482*17cadca8Slm66018 		/*
483*17cadca8Slm66018 		 * v1.0 vDisk clients depended on the server not verifying
484*17cadca8Slm66018 		 * the label of a unformatted disk.  This "feature" is
485*17cadca8Slm66018 		 * maintained for backward compatibility but all versions
486*17cadca8Slm66018 		 * from v1.1 onwards must do the right thing.
487*17cadca8Slm66018 		 */
48878fcd0a1Sachartre 		if (vd->vdisk_label == VD_DISK_LABEL_UNK &&
489*17cadca8Slm66018 		    vio_ver_is_supported(vd->version, 1, 1) &&
49078fcd0a1Sachartre 		    vd_file_validate_geometry(vd) != 0) {
49178fcd0a1Sachartre 			PR0("Unknown disk label, can't do I/O from slice %d",
49278fcd0a1Sachartre 			    slice);
49378fcd0a1Sachartre 			return (-1);
49478fcd0a1Sachartre 		}
49578fcd0a1Sachartre 
496690555a1Sachartre 		if (blk >= vd->vtoc.v_part[slice].p_size) {
497690555a1Sachartre 			/* address past the end of the slice */
498690555a1Sachartre 			PR0("req_addr (0x%lx) > psize (0x%lx)",
499690555a1Sachartre 			    blk, vd->vtoc.v_part[slice].p_size);
500690555a1Sachartre 			return (0);
501690555a1Sachartre 		}
502690555a1Sachartre 
503690555a1Sachartre 		offset = (vd->vtoc.v_part[slice].p_start + blk) * DEV_BSIZE;
504690555a1Sachartre 
505690555a1Sachartre 		/*
506690555a1Sachartre 		 * If the requested size is greater than the size
507690555a1Sachartre 		 * of the partition, truncate the read/write.
508690555a1Sachartre 		 */
509690555a1Sachartre 		maxlen = (vd->vtoc.v_part[slice].p_size - blk) * DEV_BSIZE;
510690555a1Sachartre 
511690555a1Sachartre 		if (len > maxlen) {
512690555a1Sachartre 			PR0("I/O size truncated to %lu bytes from %lu bytes",
513690555a1Sachartre 			    maxlen, len);
514690555a1Sachartre 			len = maxlen;
515690555a1Sachartre 		}
516690555a1Sachartre 	}
517690555a1Sachartre 
518690555a1Sachartre 	/*
519690555a1Sachartre 	 * We have to ensure that we are reading/writing into the mmap
520690555a1Sachartre 	 * range. If we have a partial disk image (e.g. an image of
521690555a1Sachartre 	 * s0 instead s2) the system can try to access slices that
522690555a1Sachartre 	 * are not included into the disk image.
523690555a1Sachartre 	 */
524690555a1Sachartre 	if ((offset + len) >= vd->file_size) {
525690555a1Sachartre 		PR0("offset + nbytes (0x%lx + 0x%lx) >= "
526690555a1Sachartre 		    "file_size (0x%lx)", offset, len, vd->file_size);
527690555a1Sachartre 		return (-1);
528690555a1Sachartre 	}
529690555a1Sachartre 
530690555a1Sachartre 	srw = (operation == VD_OP_BREAD)? S_READ : S_WRITE;
531eba0cb4eSachartre 	smflags = (operation == VD_OP_BREAD)? 0 :
532eba0cb4eSachartre 	    (SM_WRITE | vd_file_write_flags);
533690555a1Sachartre 	n = len;
534690555a1Sachartre 
535690555a1Sachartre 	do {
536690555a1Sachartre 		/*
537690555a1Sachartre 		 * segmap_getmapflt() returns a MAXBSIZE chunk which is
538690555a1Sachartre 		 * MAXBSIZE aligned.
539690555a1Sachartre 		 */
540690555a1Sachartre 		moffset = offset & MAXBOFFSET;
541690555a1Sachartre 		mlen = MIN(MAXBSIZE - moffset, n);
542690555a1Sachartre 		maddr = segmap_getmapflt(segkmap, vd->file_vnode, offset,
543690555a1Sachartre 		    mlen, 1, srw);
544690555a1Sachartre 		/*
545690555a1Sachartre 		 * Fault in the pages so we can check for error and ensure
546690555a1Sachartre 		 * that we can safely used the mapped address.
547690555a1Sachartre 		 */
548690555a1Sachartre 		if (segmap_fault(kas.a_hat, segkmap, maddr, mlen,
549690555a1Sachartre 		    F_SOFTLOCK, srw) != 0) {
550690555a1Sachartre 			(void) segmap_release(segkmap, maddr, 0);
551690555a1Sachartre 			return (-1);
552690555a1Sachartre 		}
553690555a1Sachartre 
554690555a1Sachartre 		if (operation == VD_OP_BREAD)
555690555a1Sachartre 			bcopy(maddr + moffset, data, mlen);
556690555a1Sachartre 		else
557690555a1Sachartre 			bcopy(data, maddr + moffset, mlen);
558690555a1Sachartre 
559690555a1Sachartre 		if (segmap_fault(kas.a_hat, segkmap, maddr, mlen,
560690555a1Sachartre 		    F_SOFTUNLOCK, srw) != 0) {
561690555a1Sachartre 			(void) segmap_release(segkmap, maddr, 0);
562690555a1Sachartre 			return (-1);
563690555a1Sachartre 		}
564690555a1Sachartre 		if (segmap_release(segkmap, maddr, smflags) != 0)
565690555a1Sachartre 			return (-1);
566690555a1Sachartre 		n -= mlen;
567690555a1Sachartre 		offset += mlen;
568690555a1Sachartre 		data += mlen;
569690555a1Sachartre 
570690555a1Sachartre 	} while (n > 0);
571690555a1Sachartre 
572690555a1Sachartre 	return (len);
573690555a1Sachartre }
574690555a1Sachartre 
57587a7269eSachartre /*
57687a7269eSachartre  * Function:
57778fcd0a1Sachartre  *	vd_file_build_default_label
57878fcd0a1Sachartre  *
57978fcd0a1Sachartre  * Description:
58078fcd0a1Sachartre  *	Return a default label for the given disk. This is used when the disk
58178fcd0a1Sachartre  *	does not have a valid VTOC so that the user can get a valid default
582*17cadca8Slm66018  *	configuration. The default label has all slice sizes set to 0 (except
58378fcd0a1Sachartre  *	slice 2 which is the entire disk) to force the user to write a valid
58478fcd0a1Sachartre  *	label onto the disk image.
58578fcd0a1Sachartre  *
58678fcd0a1Sachartre  * Parameters:
58778fcd0a1Sachartre  *	vd		- disk on which the operation is performed.
58878fcd0a1Sachartre  *	label		- the returned default label.
58978fcd0a1Sachartre  *
59078fcd0a1Sachartre  * Return Code:
59178fcd0a1Sachartre  *	none.
59278fcd0a1Sachartre  */
59378fcd0a1Sachartre static void
59478fcd0a1Sachartre vd_file_build_default_label(vd_t *vd, struct dk_label *label)
59578fcd0a1Sachartre {
59678fcd0a1Sachartre 	size_t size;
59778fcd0a1Sachartre 	char prefix;
598047ba61eSachartre 	int slice, nparts;
599047ba61eSachartre 	uint16_t tag;
60078fcd0a1Sachartre 
60178fcd0a1Sachartre 	ASSERT(vd->file);
60278fcd0a1Sachartre 
60378fcd0a1Sachartre 	/*
60478fcd0a1Sachartre 	 * We must have a resonable number of cylinders and sectors so
60578fcd0a1Sachartre 	 * that newfs can run using default values.
60678fcd0a1Sachartre 	 *
60778fcd0a1Sachartre 	 * if (disk_size < 2MB)
60878fcd0a1Sachartre 	 * 	phys_cylinders = disk_size / 100K
60978fcd0a1Sachartre 	 * else
61078fcd0a1Sachartre 	 * 	phys_cylinders = disk_size / 300K
61178fcd0a1Sachartre 	 *
61278fcd0a1Sachartre 	 * phys_cylinders = (phys_cylinders == 0) ? 1 : phys_cylinders
61378fcd0a1Sachartre 	 * alt_cylinders = (phys_cylinders > 2) ? 2 : 0;
61478fcd0a1Sachartre 	 * data_cylinders = phys_cylinders - alt_cylinders
61578fcd0a1Sachartre 	 *
61678fcd0a1Sachartre 	 * sectors = disk_size / (phys_cylinders * blk_size)
61778fcd0a1Sachartre 	 *
61878fcd0a1Sachartre 	 * The file size test is an attempt to not have too few cylinders
61978fcd0a1Sachartre 	 * for a small file, or so many on a big file that you waste space
62078fcd0a1Sachartre 	 * for backup superblocks or cylinder group structures.
62178fcd0a1Sachartre 	 */
62278fcd0a1Sachartre 	if (vd->file_size < (2 * 1024 * 1024))
62378fcd0a1Sachartre 		label->dkl_pcyl = vd->file_size / (100 * 1024);
62478fcd0a1Sachartre 	else
62578fcd0a1Sachartre 		label->dkl_pcyl = vd->file_size / (300 * 1024);
62678fcd0a1Sachartre 
62778fcd0a1Sachartre 	if (label->dkl_pcyl == 0)
62878fcd0a1Sachartre 		label->dkl_pcyl = 1;
62978fcd0a1Sachartre 
630047ba61eSachartre 	label->dkl_acyl = 0;
631047ba61eSachartre 
632047ba61eSachartre 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
633047ba61eSachartre 		nparts = 1;
634047ba61eSachartre 		slice = 0;
635047ba61eSachartre 		tag = V_UNASSIGNED;
636047ba61eSachartre 	} else {
63778fcd0a1Sachartre 		if (label->dkl_pcyl > 2)
63878fcd0a1Sachartre 			label->dkl_acyl = 2;
639047ba61eSachartre 		nparts = V_NUMPAR;
640047ba61eSachartre 		slice = VD_ENTIRE_DISK_SLICE;
641047ba61eSachartre 		tag = V_BACKUP;
642047ba61eSachartre 	}
64378fcd0a1Sachartre 
64478fcd0a1Sachartre 	label->dkl_nsect = vd->file_size /
64578fcd0a1Sachartre 	    (DEV_BSIZE * label->dkl_pcyl);
64678fcd0a1Sachartre 	label->dkl_ncyl = label->dkl_pcyl - label->dkl_acyl;
64778fcd0a1Sachartre 	label->dkl_nhead = 1;
64878fcd0a1Sachartre 	label->dkl_write_reinstruct = 0;
64978fcd0a1Sachartre 	label->dkl_read_reinstruct = 0;
65078fcd0a1Sachartre 	label->dkl_rpm = 7200;
65178fcd0a1Sachartre 	label->dkl_apc = 0;
65278fcd0a1Sachartre 	label->dkl_intrlv = 0;
65378fcd0a1Sachartre 
65478fcd0a1Sachartre 	PR0("requested disk size: %ld bytes\n", vd->file_size);
65578fcd0a1Sachartre 	PR0("setup: ncyl=%d nhead=%d nsec=%d\n", label->dkl_pcyl,
65678fcd0a1Sachartre 	    label->dkl_nhead, label->dkl_nsect);
65778fcd0a1Sachartre 	PR0("provided disk size: %ld bytes\n", (uint64_t)
65878fcd0a1Sachartre 	    (label->dkl_pcyl * label->dkl_nhead *
65978fcd0a1Sachartre 	    label->dkl_nsect * DEV_BSIZE));
66078fcd0a1Sachartre 
66178fcd0a1Sachartre 	if (vd->file_size < (1ULL << 20)) {
66278fcd0a1Sachartre 		size = vd->file_size >> 10;
66378fcd0a1Sachartre 		prefix = 'K'; /* Kilobyte */
66478fcd0a1Sachartre 	} else if (vd->file_size < (1ULL << 30)) {
66578fcd0a1Sachartre 		size = vd->file_size >> 20;
66678fcd0a1Sachartre 		prefix = 'M'; /* Megabyte */
66778fcd0a1Sachartre 	} else if (vd->file_size < (1ULL << 40)) {
66878fcd0a1Sachartre 		size = vd->file_size >> 30;
66978fcd0a1Sachartre 		prefix = 'G'; /* Gigabyte */
67078fcd0a1Sachartre 	} else {
67178fcd0a1Sachartre 		size = vd->file_size >> 40;
67278fcd0a1Sachartre 		prefix = 'T'; /* Terabyte */
67378fcd0a1Sachartre 	}
67478fcd0a1Sachartre 
67578fcd0a1Sachartre 	/*
67678fcd0a1Sachartre 	 * We must have a correct label name otherwise format(1m) will
67778fcd0a1Sachartre 	 * not recognized the disk as labeled.
67878fcd0a1Sachartre 	 */
67978fcd0a1Sachartre 	(void) snprintf(label->dkl_asciilabel, LEN_DKL_ASCII,
68078fcd0a1Sachartre 	    "SUN-DiskImage-%ld%cB cyl %d alt %d hd %d sec %d",
68178fcd0a1Sachartre 	    size, prefix,
68278fcd0a1Sachartre 	    label->dkl_ncyl, label->dkl_acyl, label->dkl_nhead,
68378fcd0a1Sachartre 	    label->dkl_nsect);
68478fcd0a1Sachartre 
68578fcd0a1Sachartre 	/* default VTOC */
68678fcd0a1Sachartre 	label->dkl_vtoc.v_version = V_VERSION;
687047ba61eSachartre 	label->dkl_vtoc.v_nparts = nparts;
68878fcd0a1Sachartre 	label->dkl_vtoc.v_sanity = VTOC_SANE;
689047ba61eSachartre 	label->dkl_vtoc.v_part[slice].p_tag = tag;
690047ba61eSachartre 	label->dkl_map[slice].dkl_cylno = 0;
691047ba61eSachartre 	label->dkl_map[slice].dkl_nblk = label->dkl_ncyl *
69278fcd0a1Sachartre 	    label->dkl_nhead * label->dkl_nsect;
69378fcd0a1Sachartre 	label->dkl_cksum = vd_lbl2cksum(label);
69478fcd0a1Sachartre }
69578fcd0a1Sachartre 
69678fcd0a1Sachartre /*
69778fcd0a1Sachartre  * Function:
69887a7269eSachartre  *	vd_file_set_vtoc
69987a7269eSachartre  *
70087a7269eSachartre  * Description:
70187a7269eSachartre  *	Set the vtoc of a disk image by writing the label and backup
70287a7269eSachartre  *	labels into the disk image backend.
70387a7269eSachartre  *
70487a7269eSachartre  * Parameters:
70587a7269eSachartre  *	vd		- disk on which the operation is performed.
70687a7269eSachartre  *	label		- the data to be written.
70787a7269eSachartre  *
70887a7269eSachartre  * Return Code:
70987a7269eSachartre  *	0		- success.
71087a7269eSachartre  *	n > 0		- error, n indicates the errno code.
71187a7269eSachartre  */
71287a7269eSachartre static int
71387a7269eSachartre vd_file_set_vtoc(vd_t *vd, struct dk_label *label)
71487a7269eSachartre {
71587a7269eSachartre 	int blk, sec, cyl, head, cnt;
71687a7269eSachartre 
71787a7269eSachartre 	ASSERT(vd->file);
71887a7269eSachartre 
71987a7269eSachartre 	if (VD_FILE_LABEL_WRITE(vd, label) < 0) {
72087a7269eSachartre 		PR0("fail to write disk label");
72187a7269eSachartre 		return (EIO);
72287a7269eSachartre 	}
72387a7269eSachartre 
72487a7269eSachartre 	/*
72587a7269eSachartre 	 * Backup labels are on the last alternate cylinder's
72687a7269eSachartre 	 * first five odd sectors.
72787a7269eSachartre 	 */
72887a7269eSachartre 	if (label->dkl_acyl == 0) {
72987a7269eSachartre 		PR0("no alternate cylinder, can not store backup labels");
73087a7269eSachartre 		return (0);
73187a7269eSachartre 	}
73287a7269eSachartre 
73387a7269eSachartre 	cyl = label->dkl_ncyl  + label->dkl_acyl - 1;
73487a7269eSachartre 	head = label->dkl_nhead - 1;
73587a7269eSachartre 
73687a7269eSachartre 	blk = (cyl * ((label->dkl_nhead * label->dkl_nsect) - label->dkl_apc)) +
73787a7269eSachartre 	    (head * label->dkl_nsect);
73887a7269eSachartre 
73987a7269eSachartre 	/*
74087a7269eSachartre 	 * Write the backup labels. Make sure we don't try to write past
74187a7269eSachartre 	 * the last cylinder.
74287a7269eSachartre 	 */
74387a7269eSachartre 	sec = 1;
74487a7269eSachartre 
74587a7269eSachartre 	for (cnt = 0; cnt < VD_FILE_NUM_BACKUP; cnt++) {
74687a7269eSachartre 
74787a7269eSachartre 		if (sec >= label->dkl_nsect) {
74887a7269eSachartre 			PR0("not enough sector to store all backup labels");
74987a7269eSachartre 			return (0);
75087a7269eSachartre 		}
75187a7269eSachartre 
75287a7269eSachartre 		if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)label,
75387a7269eSachartre 		    blk + sec, sizeof (struct dk_label)) < 0) {
75487a7269eSachartre 			PR0("error writing backup label at block %d\n",
75587a7269eSachartre 			    blk + sec);
75687a7269eSachartre 			return (EIO);
75787a7269eSachartre 		}
75887a7269eSachartre 
75987a7269eSachartre 		PR1("wrote backup label at block %d\n", blk + sec);
76087a7269eSachartre 
76187a7269eSachartre 		sec += 2;
76287a7269eSachartre 	}
76387a7269eSachartre 
76487a7269eSachartre 	return (0);
76587a7269eSachartre }
76687a7269eSachartre 
76787a7269eSachartre /*
76887a7269eSachartre  * Function:
76987a7269eSachartre  *	vd_file_get_devid_block
77087a7269eSachartre  *
77187a7269eSachartre  * Description:
77287a7269eSachartre  *	Return the block number where the device id is stored.
77387a7269eSachartre  *
77487a7269eSachartre  * Parameters:
77587a7269eSachartre  *	vd		- disk on which the operation is performed.
77687a7269eSachartre  *	blkp		- pointer to the block number
77787a7269eSachartre  *
77887a7269eSachartre  * Return Code:
77987a7269eSachartre  *	0		- success
78087a7269eSachartre  *	ENOSPC		- disk has no space to store a device id
78187a7269eSachartre  */
78287a7269eSachartre static int
78387a7269eSachartre vd_file_get_devid_block(vd_t *vd, size_t *blkp)
78487a7269eSachartre {
78587a7269eSachartre 	diskaddr_t spc, head, cyl;
78687a7269eSachartre 
78787a7269eSachartre 	ASSERT(vd->file);
78887a7269eSachartre 	ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
78987a7269eSachartre 
79087a7269eSachartre 	/* this geometry doesn't allow us to have a devid */
79187a7269eSachartre 	if (vd->dk_geom.dkg_acyl < 2) {
79287a7269eSachartre 		PR0("not enough alternate cylinder available for devid "
79387a7269eSachartre 		    "(acyl=%u)", vd->dk_geom.dkg_acyl);
79487a7269eSachartre 		return (ENOSPC);
79587a7269eSachartre 	}
79687a7269eSachartre 
79787a7269eSachartre 	/* the devid is in on the track next to the last cylinder */
79887a7269eSachartre 	cyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl - 2;
79987a7269eSachartre 	spc = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
80087a7269eSachartre 	head = vd->dk_geom.dkg_nhead - 1;
80187a7269eSachartre 
80287a7269eSachartre 	*blkp = (cyl * (spc - vd->dk_geom.dkg_apc)) +
80387a7269eSachartre 	    (head * vd->dk_geom.dkg_nsect) + 1;
80487a7269eSachartre 
80587a7269eSachartre 	return (0);
80687a7269eSachartre }
80787a7269eSachartre 
80887a7269eSachartre /*
80987a7269eSachartre  * Return the checksum of a disk block containing an on-disk devid.
81087a7269eSachartre  */
81187a7269eSachartre static uint_t
81287a7269eSachartre vd_dkdevid2cksum(struct dk_devid *dkdevid)
81387a7269eSachartre {
81487a7269eSachartre 	uint_t chksum, *ip;
81587a7269eSachartre 	int i;
81687a7269eSachartre 
81787a7269eSachartre 	chksum = 0;
81887a7269eSachartre 	ip = (uint_t *)dkdevid;
81987a7269eSachartre 	for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int)); i++)
82087a7269eSachartre 		chksum ^= ip[i];
82187a7269eSachartre 
82287a7269eSachartre 	return (chksum);
82387a7269eSachartre }
82487a7269eSachartre 
82587a7269eSachartre /*
82687a7269eSachartre  * Function:
82787a7269eSachartre  *	vd_file_read_devid
82887a7269eSachartre  *
82987a7269eSachartre  * Description:
83087a7269eSachartre  *	Read the device id stored on a disk image.
83187a7269eSachartre  *
83287a7269eSachartre  * Parameters:
83387a7269eSachartre  *	vd		- disk on which the operation is performed.
83487a7269eSachartre  *	devid		- the return address of the device ID.
83587a7269eSachartre  *
83687a7269eSachartre  * Return Code:
83787a7269eSachartre  *	0		- success
83887a7269eSachartre  *	EIO		- I/O error while trying to access the disk image
83987a7269eSachartre  *	EINVAL		- no valid device id was found
84087a7269eSachartre  *	ENOSPC		- disk has no space to store a device id
84187a7269eSachartre  */
84287a7269eSachartre static int
84387a7269eSachartre vd_file_read_devid(vd_t *vd, ddi_devid_t *devid)
84487a7269eSachartre {
84587a7269eSachartre 	struct dk_devid *dkdevid;
84687a7269eSachartre 	size_t blk;
84787a7269eSachartre 	uint_t chksum;
84887a7269eSachartre 	int status, sz;
84987a7269eSachartre 
85087a7269eSachartre 	if ((status = vd_file_get_devid_block(vd, &blk)) != 0)
85187a7269eSachartre 		return (status);
85287a7269eSachartre 
85387a7269eSachartre 	dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP);
85487a7269eSachartre 
85587a7269eSachartre 	/* get the devid */
85687a7269eSachartre 	if ((vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)dkdevid, blk,
85787a7269eSachartre 	    DEV_BSIZE)) < 0) {
85887a7269eSachartre 		PR0("error reading devid block at %lu", blk);
85987a7269eSachartre 		status = EIO;
86087a7269eSachartre 		goto done;
86187a7269eSachartre 	}
86287a7269eSachartre 
86387a7269eSachartre 	/* validate the revision */
86487a7269eSachartre 	if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
86587a7269eSachartre 	    (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
86687a7269eSachartre 		PR0("invalid devid found at block %lu (bad revision)", blk);
86787a7269eSachartre 		status = EINVAL;
86887a7269eSachartre 		goto done;
86987a7269eSachartre 	}
87087a7269eSachartre 
87187a7269eSachartre 	/* compute checksum */
87287a7269eSachartre 	chksum = vd_dkdevid2cksum(dkdevid);
87387a7269eSachartre 
87487a7269eSachartre 	/* compare the checksums */
87587a7269eSachartre 	if (DKD_GETCHKSUM(dkdevid) != chksum) {
87687a7269eSachartre 		PR0("invalid devid found at block %lu (bad checksum)", blk);
87787a7269eSachartre 		status = EINVAL;
87887a7269eSachartre 		goto done;
87987a7269eSachartre 	}
88087a7269eSachartre 
88187a7269eSachartre 	/* validate the device id */
88287a7269eSachartre 	if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
88387a7269eSachartre 		PR0("invalid devid found at block %lu", blk);
88487a7269eSachartre 		status = EINVAL;
88587a7269eSachartre 		goto done;
88687a7269eSachartre 	}
88787a7269eSachartre 
88887a7269eSachartre 	PR1("devid read at block %lu", blk);
88987a7269eSachartre 
89087a7269eSachartre 	sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
89187a7269eSachartre 	*devid = kmem_alloc(sz, KM_SLEEP);
89287a7269eSachartre 	bcopy(&dkdevid->dkd_devid, *devid, sz);
89387a7269eSachartre 
89487a7269eSachartre done:
89587a7269eSachartre 	kmem_free(dkdevid, DEV_BSIZE);
89687a7269eSachartre 	return (status);
89787a7269eSachartre 
89887a7269eSachartre }
89987a7269eSachartre 
90087a7269eSachartre /*
90187a7269eSachartre  * Function:
90287a7269eSachartre  *	vd_file_write_devid
90387a7269eSachartre  *
90487a7269eSachartre  * Description:
90587a7269eSachartre  *	Write a device id into disk image.
90687a7269eSachartre  *
90787a7269eSachartre  * Parameters:
90887a7269eSachartre  *	vd		- disk on which the operation is performed.
90987a7269eSachartre  *	devid		- the device ID to store.
91087a7269eSachartre  *
91187a7269eSachartre  * Return Code:
91287a7269eSachartre  *	0		- success
91387a7269eSachartre  *	EIO		- I/O error while trying to access the disk image
91487a7269eSachartre  *	ENOSPC		- disk has no space to store a device id
91587a7269eSachartre  */
91687a7269eSachartre static int
91787a7269eSachartre vd_file_write_devid(vd_t *vd, ddi_devid_t devid)
91887a7269eSachartre {
91987a7269eSachartre 	struct dk_devid *dkdevid;
92087a7269eSachartre 	uint_t chksum;
92187a7269eSachartre 	size_t blk;
92287a7269eSachartre 	int status;
92387a7269eSachartre 
92487a7269eSachartre 	if ((status = vd_file_get_devid_block(vd, &blk)) != 0)
92587a7269eSachartre 		return (status);
92687a7269eSachartre 
92787a7269eSachartre 	dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP);
92887a7269eSachartre 
92987a7269eSachartre 	/* set revision */
93087a7269eSachartre 	dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
93187a7269eSachartre 	dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
93287a7269eSachartre 
93387a7269eSachartre 	/* copy devid */
93487a7269eSachartre 	bcopy(devid, &dkdevid->dkd_devid, ddi_devid_sizeof(devid));
93587a7269eSachartre 
93687a7269eSachartre 	/* compute checksum */
93787a7269eSachartre 	chksum = vd_dkdevid2cksum(dkdevid);
93887a7269eSachartre 
93987a7269eSachartre 	/* set checksum */
94087a7269eSachartre 	DKD_FORMCHKSUM(chksum, dkdevid);
94187a7269eSachartre 
94287a7269eSachartre 	/* store the devid */
94387a7269eSachartre 	if ((status = vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE,
94487a7269eSachartre 	    (caddr_t)dkdevid, blk, DEV_BSIZE)) < 0) {
94587a7269eSachartre 		PR0("Error writing devid block at %lu", blk);
94687a7269eSachartre 		status = EIO;
94787a7269eSachartre 	} else {
94887a7269eSachartre 		PR1("devid written at block %lu", blk);
94987a7269eSachartre 		status = 0;
95087a7269eSachartre 	}
95187a7269eSachartre 
95287a7269eSachartre 	kmem_free(dkdevid, DEV_BSIZE);
95387a7269eSachartre 	return (status);
95487a7269eSachartre }
95587a7269eSachartre 
95687a7269eSachartre /*
95787a7269eSachartre  * Function:
958*17cadca8Slm66018  *	vd_do_scsi_rdwr
95987a7269eSachartre  *
96087a7269eSachartre  * Description:
96187a7269eSachartre  * 	Read or write to a SCSI disk using an absolute disk offset.
96287a7269eSachartre  *
96387a7269eSachartre  * Parameters:
96487a7269eSachartre  *	vd		- disk on which the operation is performed.
96587a7269eSachartre  *	operation	- operation to execute: read (VD_OP_BREAD) or
96687a7269eSachartre  *			  write (VD_OP_BWRITE).
96787a7269eSachartre  *	data		- buffer where data are read to or written from.
96887a7269eSachartre  *	blk		- starting block for the operation.
96987a7269eSachartre  *	len		- number of bytes to read or write.
97087a7269eSachartre  *
97187a7269eSachartre  * Return Code:
97287a7269eSachartre  *	0		- success
97387a7269eSachartre  *	n != 0		- error.
97487a7269eSachartre  */
97587a7269eSachartre static int
976*17cadca8Slm66018 vd_do_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t blk, size_t len)
97787a7269eSachartre {
97887a7269eSachartre 	struct uscsi_cmd ucmd;
97987a7269eSachartre 	union scsi_cdb cdb;
98087a7269eSachartre 	int nsectors, nblk;
98187a7269eSachartre 	int max_sectors;
98287a7269eSachartre 	int status, rval;
98387a7269eSachartre 
98487a7269eSachartre 	ASSERT(!vd->file);
985*17cadca8Slm66018 	ASSERT(vd->vdisk_block_size > 0);
98687a7269eSachartre 
98787a7269eSachartre 	max_sectors = vd->max_xfer_sz;
988*17cadca8Slm66018 	nblk = (len / vd->vdisk_block_size);
98987a7269eSachartre 
990*17cadca8Slm66018 	if (len % vd->vdisk_block_size != 0)
99187a7269eSachartre 		return (EINVAL);
99287a7269eSachartre 
99387a7269eSachartre 	/*
99487a7269eSachartre 	 * Build and execute the uscsi ioctl.  We build a group0, group1
99587a7269eSachartre 	 * or group4 command as necessary, since some targets
99687a7269eSachartre 	 * do not support group1 commands.
99787a7269eSachartre 	 */
99887a7269eSachartre 	while (nblk) {
99987a7269eSachartre 
100087a7269eSachartre 		bzero(&ucmd, sizeof (ucmd));
100187a7269eSachartre 		bzero(&cdb, sizeof (cdb));
100287a7269eSachartre 
100387a7269eSachartre 		nsectors = (max_sectors < nblk) ? max_sectors : nblk;
100487a7269eSachartre 
1005*17cadca8Slm66018 		/*
1006*17cadca8Slm66018 		 * Some of the optical drives on sun4v machines are ATAPI
1007*17cadca8Slm66018 		 * devices which use Group 1 Read/Write commands so we need
1008*17cadca8Slm66018 		 * to explicitly check a flag which is set when a domain
1009*17cadca8Slm66018 		 * is bound.
1010*17cadca8Slm66018 		 */
1011*17cadca8Slm66018 		if (blk < (2 << 20) && nsectors <= 0xff && !vd->is_atapi_dev) {
101287a7269eSachartre 			FORMG0ADDR(&cdb, blk);
101387a7269eSachartre 			FORMG0COUNT(&cdb, nsectors);
101487a7269eSachartre 			ucmd.uscsi_cdblen = CDB_GROUP0;
101587a7269eSachartre 		} else if (blk > 0xffffffff) {
101687a7269eSachartre 			FORMG4LONGADDR(&cdb, blk);
101787a7269eSachartre 			FORMG4COUNT(&cdb, nsectors);
101887a7269eSachartre 			ucmd.uscsi_cdblen = CDB_GROUP4;
101987a7269eSachartre 			cdb.scc_cmd |= SCMD_GROUP4;
102087a7269eSachartre 		} else {
102187a7269eSachartre 			FORMG1ADDR(&cdb, blk);
102287a7269eSachartre 			FORMG1COUNT(&cdb, nsectors);
102387a7269eSachartre 			ucmd.uscsi_cdblen = CDB_GROUP1;
102487a7269eSachartre 			cdb.scc_cmd |= SCMD_GROUP1;
102587a7269eSachartre 		}
102687a7269eSachartre 		ucmd.uscsi_cdb = (caddr_t)&cdb;
102787a7269eSachartre 		ucmd.uscsi_bufaddr = data;
1028*17cadca8Slm66018 		ucmd.uscsi_buflen = nsectors * vd->block_size;
102987a7269eSachartre 		ucmd.uscsi_timeout = vd_scsi_rdwr_timeout;
103087a7269eSachartre 		/*
103187a7269eSachartre 		 * Set flags so that the command is isolated from normal
103287a7269eSachartre 		 * commands and no error message is printed.
103387a7269eSachartre 		 */
103487a7269eSachartre 		ucmd.uscsi_flags = USCSI_ISOLATE | USCSI_SILENT;
103587a7269eSachartre 
103687a7269eSachartre 		if (operation == VD_OP_BREAD) {
103787a7269eSachartre 			cdb.scc_cmd |= SCMD_READ;
103887a7269eSachartre 			ucmd.uscsi_flags |= USCSI_READ;
103987a7269eSachartre 		} else {
104087a7269eSachartre 			cdb.scc_cmd |= SCMD_WRITE;
104187a7269eSachartre 		}
104287a7269eSachartre 
104387a7269eSachartre 		status = ldi_ioctl(vd->ldi_handle[VD_ENTIRE_DISK_SLICE],
1044047ba61eSachartre 		    USCSICMD, (intptr_t)&ucmd, (vd->open_flags | FKIOCTL),
104587a7269eSachartre 		    kcred, &rval);
104687a7269eSachartre 
104787a7269eSachartre 		if (status == 0)
104887a7269eSachartre 			status = ucmd.uscsi_status;
104987a7269eSachartre 
105087a7269eSachartre 		if (status != 0)
105187a7269eSachartre 			break;
105287a7269eSachartre 
105387a7269eSachartre 		/*
105487a7269eSachartre 		 * Check if partial DMA breakup is required. If so, reduce
105587a7269eSachartre 		 * the request size by half and retry the last request.
105687a7269eSachartre 		 */
105787a7269eSachartre 		if (ucmd.uscsi_resid == ucmd.uscsi_buflen) {
105887a7269eSachartre 			max_sectors >>= 1;
105987a7269eSachartre 			if (max_sectors <= 0) {
106087a7269eSachartre 				status = EIO;
106187a7269eSachartre 				break;
106287a7269eSachartre 			}
106387a7269eSachartre 			continue;
106487a7269eSachartre 		}
106587a7269eSachartre 
106687a7269eSachartre 		if (ucmd.uscsi_resid != 0) {
106787a7269eSachartre 			status = EIO;
106887a7269eSachartre 			break;
106987a7269eSachartre 		}
107087a7269eSachartre 
107187a7269eSachartre 		blk += nsectors;
107287a7269eSachartre 		nblk -= nsectors;
1073*17cadca8Slm66018 		data += nsectors * vd->vdisk_block_size; /* SECSIZE */
107487a7269eSachartre 	}
107587a7269eSachartre 
107687a7269eSachartre 	return (status);
107787a7269eSachartre }
107887a7269eSachartre 
1079205eeb1aSlm66018 /*
1080*17cadca8Slm66018  * Function:
1081*17cadca8Slm66018  *	vd_scsi_rdwr
1082*17cadca8Slm66018  *
1083*17cadca8Slm66018  * Description:
1084*17cadca8Slm66018  * 	Wrapper function to read or write to a SCSI disk using an absolute
1085*17cadca8Slm66018  *	disk offset. It checks the blocksize of the underlying device and,
1086*17cadca8Slm66018  *	if necessary, adjusts the buffers accordingly before calling
1087*17cadca8Slm66018  *	vd_do_scsi_rdwr() to do the actual read or write.
1088*17cadca8Slm66018  *
1089*17cadca8Slm66018  * Parameters:
1090*17cadca8Slm66018  *	vd		- disk on which the operation is performed.
1091*17cadca8Slm66018  *	operation	- operation to execute: read (VD_OP_BREAD) or
1092*17cadca8Slm66018  *			  write (VD_OP_BWRITE).
1093*17cadca8Slm66018  *	data		- buffer where data are read to or written from.
1094*17cadca8Slm66018  *	blk		- starting block for the operation.
1095*17cadca8Slm66018  *	len		- number of bytes to read or write.
1096*17cadca8Slm66018  *
1097*17cadca8Slm66018  * Return Code:
1098*17cadca8Slm66018  *	0		- success
1099*17cadca8Slm66018  *	n != 0		- error.
1100*17cadca8Slm66018  */
1101*17cadca8Slm66018 static int
1102*17cadca8Slm66018 vd_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t vblk, size_t vlen)
1103*17cadca8Slm66018 {
1104*17cadca8Slm66018 	int	rv;
1105*17cadca8Slm66018 
1106*17cadca8Slm66018 	size_t	pblk;	/* physical device block number of data on device */
1107*17cadca8Slm66018 	size_t	delta;	/* relative offset between pblk and vblk */
1108*17cadca8Slm66018 	size_t	pnblk;	/* number of physical blocks to be read from device */
1109*17cadca8Slm66018 	size_t	plen;	/* length of data to be read from physical device */
1110*17cadca8Slm66018 	char	*buf;	/* buffer area to fit physical device's block size */
1111*17cadca8Slm66018 
1112*17cadca8Slm66018 	/*
1113*17cadca8Slm66018 	 * If the vdisk block size and the block size of the underlying device
1114*17cadca8Slm66018 	 * match we can skip straight to vd_do_scsi_rdwr(), otherwise we need
1115*17cadca8Slm66018 	 * to create a buffer large enough to handle the device's block size
1116*17cadca8Slm66018 	 * and adjust the block to be read from and the amount of data to
1117*17cadca8Slm66018 	 * read to correspond with the device's block size.
1118*17cadca8Slm66018 	 */
1119*17cadca8Slm66018 	if (vd->vdisk_block_size == vd->block_size)
1120*17cadca8Slm66018 		return (vd_do_scsi_rdwr(vd, operation, data, vblk, vlen));
1121*17cadca8Slm66018 
1122*17cadca8Slm66018 	if (vd->vdisk_block_size > vd->block_size)
1123*17cadca8Slm66018 		return (EINVAL);
1124*17cadca8Slm66018 
1125*17cadca8Slm66018 	/*
1126*17cadca8Slm66018 	 * Writing of physical block sizes larger than the virtual block size
1127*17cadca8Slm66018 	 * is not supported. This would be added if/when support for guests
1128*17cadca8Slm66018 	 * writing to DVDs is implemented.
1129*17cadca8Slm66018 	 */
1130*17cadca8Slm66018 	if (operation == VD_OP_BWRITE)
1131*17cadca8Slm66018 		return (ENOTSUP);
1132*17cadca8Slm66018 
1133*17cadca8Slm66018 	/* BEGIN CSTYLED */
1134*17cadca8Slm66018 	/*
1135*17cadca8Slm66018 	 * Below is a diagram showing the relationship between the physical
1136*17cadca8Slm66018 	 * and virtual blocks. If the virtual blocks marked by 'X' below are
1137*17cadca8Slm66018 	 * requested, then the physical blocks denoted by 'Y' are read.
1138*17cadca8Slm66018 	 *
1139*17cadca8Slm66018 	 *           vblk
1140*17cadca8Slm66018 	 *             |      vlen
1141*17cadca8Slm66018 	 *             |<--------------->|
1142*17cadca8Slm66018 	 *             v                 v
1143*17cadca8Slm66018 	 *  --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-   virtual disk:
1144*17cadca8Slm66018 	 *    |  |  |  |XX|XX|XX|XX|XX|XX|  |  |  |  |  |  } block size is
1145*17cadca8Slm66018 	 *  --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-  vd->vdisk_block_size
1146*17cadca8Slm66018 	 *          :  :                 :  :
1147*17cadca8Slm66018 	 *         >:==:< delta          :  :
1148*17cadca8Slm66018 	 *          :  :                 :  :
1149*17cadca8Slm66018 	 *  --+-----+-----+-----+-----+-----+-----+-----+--   physical disk:
1150*17cadca8Slm66018 	 *    |     |YY:YY|YYYYY|YYYYY|YY:YY|     |     |   } block size is
1151*17cadca8Slm66018 	 *  --+-----+-----+-----+-----+-----+-----+-----+--   vd->block_size
1152*17cadca8Slm66018 	 *          ^                       ^
1153*17cadca8Slm66018 	 *          |<--------------------->|
1154*17cadca8Slm66018 	 *          |         plen
1155*17cadca8Slm66018 	 *         pblk
1156*17cadca8Slm66018 	 */
1157*17cadca8Slm66018 	/* END CSTYLED */
1158*17cadca8Slm66018 	pblk = (vblk * vd->vdisk_block_size) / vd->block_size;
1159*17cadca8Slm66018 	delta = (vblk * vd->vdisk_block_size) - (pblk * vd->block_size);
1160*17cadca8Slm66018 	pnblk = ((delta + vlen - 1) / vd->block_size) + 1;
1161*17cadca8Slm66018 	plen = pnblk * vd->block_size;
1162*17cadca8Slm66018 
1163*17cadca8Slm66018 	PR2("vblk %lx:pblk %lx: vlen %ld:plen %ld", vblk, pblk, vlen, plen);
1164*17cadca8Slm66018 
1165*17cadca8Slm66018 	buf = kmem_zalloc(sizeof (caddr_t) * plen, KM_SLEEP);
1166*17cadca8Slm66018 	rv = vd_do_scsi_rdwr(vd, operation, (caddr_t)buf, pblk, plen);
1167*17cadca8Slm66018 	bcopy(buf + delta, data, vlen);
1168*17cadca8Slm66018 
1169*17cadca8Slm66018 	kmem_free(buf, sizeof (caddr_t) * plen);
1170*17cadca8Slm66018 
1171*17cadca8Slm66018 	return (rv);
1172*17cadca8Slm66018 }
1173*17cadca8Slm66018 
1174*17cadca8Slm66018 /*
1175205eeb1aSlm66018  * Return Values
1176205eeb1aSlm66018  *	EINPROGRESS	- operation was successfully started
1177205eeb1aSlm66018  *	EIO		- encountered LDC (aka. task error)
1178205eeb1aSlm66018  *	0		- operation completed successfully
1179205eeb1aSlm66018  *
1180205eeb1aSlm66018  * Side Effect
1181205eeb1aSlm66018  *     sets request->status = <disk operation status>
1182205eeb1aSlm66018  */
11831ae08745Sheppo static int
1184d10e4ef2Snarayan vd_start_bio(vd_task_t *task)
11851ae08745Sheppo {
11864bac2208Snarayan 	int			rv, status = 0;
1187d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
1188d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
1189d10e4ef2Snarayan 	struct buf		*buf		= &task->buf;
11904bac2208Snarayan 	uint8_t			mtype;
11913c96341aSnarayan 	int 			slice;
1192047ba61eSachartre 	char			*bufaddr = 0;
1193047ba61eSachartre 	size_t			buflen;
1194d10e4ef2Snarayan 
1195d10e4ef2Snarayan 	ASSERT(vd != NULL);
1196d10e4ef2Snarayan 	ASSERT(request != NULL);
11973c96341aSnarayan 
11983c96341aSnarayan 	slice = request->slice;
11993c96341aSnarayan 
120087a7269eSachartre 	ASSERT(slice == VD_SLICE_NONE || slice < vd->nslices);
1201d10e4ef2Snarayan 	ASSERT((request->operation == VD_OP_BREAD) ||
1202d10e4ef2Snarayan 	    (request->operation == VD_OP_BWRITE));
1203d10e4ef2Snarayan 
1204205eeb1aSlm66018 	if (request->nbytes == 0) {
1205205eeb1aSlm66018 		/* no service for trivial requests */
1206205eeb1aSlm66018 		request->status = EINVAL;
1207205eeb1aSlm66018 		return (0);
1208205eeb1aSlm66018 	}
12091ae08745Sheppo 
1210d10e4ef2Snarayan 	PR1("%s %lu bytes at block %lu",
1211d10e4ef2Snarayan 	    (request->operation == VD_OP_BREAD) ? "Read" : "Write",
1212d10e4ef2Snarayan 	    request->nbytes, request->addr);
12131ae08745Sheppo 
1214047ba61eSachartre 	/*
1215047ba61eSachartre 	 * We have to check the open flags because the functions processing
1216047ba61eSachartre 	 * the read/write request will not do it.
1217047ba61eSachartre 	 */
1218047ba61eSachartre 	if (request->operation == VD_OP_BWRITE && !(vd->open_flags & FWRITE)) {
1219047ba61eSachartre 		PR0("write fails because backend is opened read-only");
1220047ba61eSachartre 		request->nbytes = 0;
1221047ba61eSachartre 		request->status = EROFS;
1222047ba61eSachartre 		return (0);
1223047ba61eSachartre 	}
1224d10e4ef2Snarayan 
12254bac2208Snarayan 	mtype = (&vd->inband_task == task) ? LDC_SHADOW_MAP : LDC_DIRECT_MAP;
12264bac2208Snarayan 
12274bac2208Snarayan 	/* Map memory exported by client */
12284bac2208Snarayan 	status = ldc_mem_map(task->mhdl, request->cookie, request->ncookies,
12294bac2208Snarayan 	    mtype, (request->operation == VD_OP_BREAD) ? LDC_MEM_W : LDC_MEM_R,
1230047ba61eSachartre 	    &bufaddr, NULL);
12314bac2208Snarayan 	if (status != 0) {
12323af08d82Slm66018 		PR0("ldc_mem_map() returned err %d ", status);
1233205eeb1aSlm66018 		return (EIO);
1234d10e4ef2Snarayan 	}
1235d10e4ef2Snarayan 
1236047ba61eSachartre 	buflen = request->nbytes;
1237047ba61eSachartre 
1238047ba61eSachartre 	status = ldc_mem_acquire(task->mhdl, 0, buflen);
12394bac2208Snarayan 	if (status != 0) {
12404bac2208Snarayan 		(void) ldc_mem_unmap(task->mhdl);
12413af08d82Slm66018 		PR0("ldc_mem_acquire() returned err %d ", status);
1242205eeb1aSlm66018 		return (EIO);
12434bac2208Snarayan 	}
12444bac2208Snarayan 
1245d10e4ef2Snarayan 	/* Start the block I/O */
12463c96341aSnarayan 	if (vd->file) {
1247047ba61eSachartre 		rv = vd_file_rw(vd, slice, request->operation, bufaddr,
1248690555a1Sachartre 		    request->addr, request->nbytes);
1249690555a1Sachartre 		if (rv < 0) {
12503c96341aSnarayan 			request->nbytes = 0;
1251205eeb1aSlm66018 			request->status = EIO;
1252690555a1Sachartre 		} else {
1253690555a1Sachartre 			request->nbytes = rv;
1254205eeb1aSlm66018 			request->status = 0;
12553c96341aSnarayan 		}
12563c96341aSnarayan 	} else {
125787a7269eSachartre 		if (slice == VD_SLICE_NONE) {
125887a7269eSachartre 			/*
125987a7269eSachartre 			 * This is not a disk image so it is a real disk. We
126087a7269eSachartre 			 * assume that the underlying device driver supports
126187a7269eSachartre 			 * USCSICMD ioctls. This is the case of all SCSI devices
126287a7269eSachartre 			 * (sd, ssd...).
126387a7269eSachartre 			 *
126487a7269eSachartre 			 * In the future if we have non-SCSI disks we would need
126587a7269eSachartre 			 * to invoke the appropriate function to do I/O using an
1266*17cadca8Slm66018 			 * absolute disk offset (for example using DIOCTL_RWCMD
126787a7269eSachartre 			 * for IDE disks).
126887a7269eSachartre 			 */
1269047ba61eSachartre 			rv = vd_scsi_rdwr(vd, request->operation, bufaddr,
1270047ba61eSachartre 			    request->addr, request->nbytes);
127187a7269eSachartre 			if (rv != 0) {
127287a7269eSachartre 				request->nbytes = 0;
1273205eeb1aSlm66018 				request->status = EIO;
127487a7269eSachartre 			} else {
1275205eeb1aSlm66018 				request->status = 0;
127687a7269eSachartre 			}
127787a7269eSachartre 		} else {
1278047ba61eSachartre 			bioinit(buf);
1279047ba61eSachartre 			buf->b_flags	= B_BUSY;
1280047ba61eSachartre 			buf->b_bcount	= request->nbytes;
1281047ba61eSachartre 			buf->b_lblkno	= request->addr;
1282047ba61eSachartre 			buf->b_edev 	= vd->dev[slice];
1283047ba61eSachartre 			buf->b_un.b_addr = bufaddr;
1284047ba61eSachartre 			buf->b_flags 	|= (request->operation == VD_OP_BREAD)?
1285047ba61eSachartre 			    B_READ : B_WRITE;
1286047ba61eSachartre 
1287205eeb1aSlm66018 			request->status =
1288205eeb1aSlm66018 			    ldi_strategy(vd->ldi_handle[slice], buf);
1289205eeb1aSlm66018 
1290205eeb1aSlm66018 			/*
1291205eeb1aSlm66018 			 * This is to indicate to the caller that the request
1292205eeb1aSlm66018 			 * needs to be finished by vd_complete_bio() by calling
1293205eeb1aSlm66018 			 * biowait() there and waiting for that to return before
1294205eeb1aSlm66018 			 * triggering the notification of the vDisk client.
1295205eeb1aSlm66018 			 *
1296205eeb1aSlm66018 			 * This is necessary when writing to real disks as
1297205eeb1aSlm66018 			 * otherwise calls to ldi_strategy() would be serialized
1298205eeb1aSlm66018 			 * behind the calls to biowait() and performance would
1299205eeb1aSlm66018 			 * suffer.
1300205eeb1aSlm66018 			 */
1301205eeb1aSlm66018 			if (request->status == 0)
130287a7269eSachartre 				return (EINPROGRESS);
1303047ba61eSachartre 
1304047ba61eSachartre 			biofini(buf);
130587a7269eSachartre 		}
13063c96341aSnarayan 	}
13073c96341aSnarayan 
1308d10e4ef2Snarayan 	/* Clean up after error */
1309047ba61eSachartre 	rv = ldc_mem_release(task->mhdl, 0, buflen);
13104bac2208Snarayan 	if (rv) {
13113af08d82Slm66018 		PR0("ldc_mem_release() returned err %d ", rv);
1312205eeb1aSlm66018 		status = EIO;
13134bac2208Snarayan 	}
13144bac2208Snarayan 	rv = ldc_mem_unmap(task->mhdl);
13154bac2208Snarayan 	if (rv) {
1316205eeb1aSlm66018 		PR0("ldc_mem_unmap() returned err %d ", rv);
1317205eeb1aSlm66018 		status = EIO;
13184bac2208Snarayan 	}
13194bac2208Snarayan 
1320d10e4ef2Snarayan 	return (status);
1321d10e4ef2Snarayan }
1322d10e4ef2Snarayan 
1323205eeb1aSlm66018 /*
1324205eeb1aSlm66018  * This function should only be called from vd_notify to ensure that requests
1325205eeb1aSlm66018  * are responded to in the order that they are received.
1326205eeb1aSlm66018  */
1327d10e4ef2Snarayan static int
1328d10e4ef2Snarayan send_msg(ldc_handle_t ldc_handle, void *msg, size_t msglen)
1329d10e4ef2Snarayan {
13303af08d82Slm66018 	int	status;
1331d10e4ef2Snarayan 	size_t	nbytes;
1332d10e4ef2Snarayan 
13333af08d82Slm66018 	do {
1334d10e4ef2Snarayan 		nbytes = msglen;
1335d10e4ef2Snarayan 		status = ldc_write(ldc_handle, msg, &nbytes);
13363af08d82Slm66018 		if (status != EWOULDBLOCK)
13373af08d82Slm66018 			break;
13383af08d82Slm66018 		drv_usecwait(vds_ldc_delay);
13393af08d82Slm66018 	} while (status == EWOULDBLOCK);
1340d10e4ef2Snarayan 
1341d10e4ef2Snarayan 	if (status != 0) {
13423af08d82Slm66018 		if (status != ECONNRESET)
13433af08d82Slm66018 			PR0("ldc_write() returned errno %d", status);
1344d10e4ef2Snarayan 		return (status);
1345d10e4ef2Snarayan 	} else if (nbytes != msglen) {
13463af08d82Slm66018 		PR0("ldc_write() performed only partial write");
1347d10e4ef2Snarayan 		return (EIO);
1348d10e4ef2Snarayan 	}
1349d10e4ef2Snarayan 
1350d10e4ef2Snarayan 	PR1("SENT %lu bytes", msglen);
1351d10e4ef2Snarayan 	return (0);
1352d10e4ef2Snarayan }
1353d10e4ef2Snarayan 
1354d10e4ef2Snarayan static void
1355d10e4ef2Snarayan vd_need_reset(vd_t *vd, boolean_t reset_ldc)
1356d10e4ef2Snarayan {
1357d10e4ef2Snarayan 	mutex_enter(&vd->lock);
1358d10e4ef2Snarayan 	vd->reset_state	= B_TRUE;
1359d10e4ef2Snarayan 	vd->reset_ldc	= reset_ldc;
1360d10e4ef2Snarayan 	mutex_exit(&vd->lock);
1361d10e4ef2Snarayan }
1362d10e4ef2Snarayan 
1363d10e4ef2Snarayan /*
1364d10e4ef2Snarayan  * Reset the state of the connection with a client, if needed; reset the LDC
1365d10e4ef2Snarayan  * transport as well, if needed.  This function should only be called from the
13663af08d82Slm66018  * "vd_recv_msg", as it waits for tasks - otherwise a deadlock can occur.
1367d10e4ef2Snarayan  */
1368d10e4ef2Snarayan static void
1369d10e4ef2Snarayan vd_reset_if_needed(vd_t *vd)
1370d10e4ef2Snarayan {
1371d10e4ef2Snarayan 	int	status = 0;
1372d10e4ef2Snarayan 
1373d10e4ef2Snarayan 	mutex_enter(&vd->lock);
1374d10e4ef2Snarayan 	if (!vd->reset_state) {
1375d10e4ef2Snarayan 		ASSERT(!vd->reset_ldc);
1376d10e4ef2Snarayan 		mutex_exit(&vd->lock);
1377d10e4ef2Snarayan 		return;
1378d10e4ef2Snarayan 	}
1379d10e4ef2Snarayan 	mutex_exit(&vd->lock);
1380d10e4ef2Snarayan 
1381d10e4ef2Snarayan 	PR0("Resetting connection state with %s", VD_CLIENT(vd));
1382d10e4ef2Snarayan 
1383d10e4ef2Snarayan 	/*
1384d10e4ef2Snarayan 	 * Let any asynchronous I/O complete before possibly pulling the rug
1385d10e4ef2Snarayan 	 * out from under it; defer checking vd->reset_ldc, as one of the
1386d10e4ef2Snarayan 	 * asynchronous tasks might set it
1387d10e4ef2Snarayan 	 */
1388d10e4ef2Snarayan 	ddi_taskq_wait(vd->completionq);
1389d10e4ef2Snarayan 
13903c96341aSnarayan 	if (vd->file) {
1391da6c28aaSamw 		status = VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL);
13923c96341aSnarayan 		if (status) {
13933c96341aSnarayan 			PR0("VOP_FSYNC returned errno %d", status);
13943c96341aSnarayan 		}
13953c96341aSnarayan 	}
13963c96341aSnarayan 
1397d10e4ef2Snarayan 	if ((vd->initialized & VD_DRING) &&
1398d10e4ef2Snarayan 	    ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0))
13993af08d82Slm66018 		PR0("ldc_mem_dring_unmap() returned errno %d", status);
1400d10e4ef2Snarayan 
14013af08d82Slm66018 	vd_free_dring_task(vd);
14023af08d82Slm66018 
14033af08d82Slm66018 	/* Free the staging buffer for msgs */
14043af08d82Slm66018 	if (vd->vio_msgp != NULL) {
14053af08d82Slm66018 		kmem_free(vd->vio_msgp, vd->max_msglen);
14063af08d82Slm66018 		vd->vio_msgp = NULL;
1407d10e4ef2Snarayan 	}
1408d10e4ef2Snarayan 
14093af08d82Slm66018 	/* Free the inband message buffer */
14103af08d82Slm66018 	if (vd->inband_task.msg != NULL) {
14113af08d82Slm66018 		kmem_free(vd->inband_task.msg, vd->max_msglen);
14123af08d82Slm66018 		vd->inband_task.msg = NULL;
14133af08d82Slm66018 	}
1414d10e4ef2Snarayan 
1415d10e4ef2Snarayan 	mutex_enter(&vd->lock);
14163af08d82Slm66018 
14173af08d82Slm66018 	if (vd->reset_ldc)
14183af08d82Slm66018 		PR0("taking down LDC channel");
1419e1ebb9ecSlm66018 	if (vd->reset_ldc && ((status = ldc_down(vd->ldc_handle)) != 0))
14203af08d82Slm66018 		PR0("ldc_down() returned errno %d", status);
1421d10e4ef2Snarayan 
1422d10e4ef2Snarayan 	vd->initialized	&= ~(VD_SID | VD_SEQ_NUM | VD_DRING);
1423d10e4ef2Snarayan 	vd->state	= VD_STATE_INIT;
1424d10e4ef2Snarayan 	vd->max_msglen	= sizeof (vio_msg_t);	/* baseline vio message size */
1425d10e4ef2Snarayan 
14263af08d82Slm66018 	/* Allocate the staging buffer */
14273af08d82Slm66018 	vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
14283af08d82Slm66018 
14293af08d82Slm66018 	PR0("calling ldc_up\n");
14303af08d82Slm66018 	(void) ldc_up(vd->ldc_handle);
14313af08d82Slm66018 
1432d10e4ef2Snarayan 	vd->reset_state	= B_FALSE;
1433d10e4ef2Snarayan 	vd->reset_ldc	= B_FALSE;
14343af08d82Slm66018 
1435d10e4ef2Snarayan 	mutex_exit(&vd->lock);
1436d10e4ef2Snarayan }
1437d10e4ef2Snarayan 
14383af08d82Slm66018 static void vd_recv_msg(void *arg);
14393af08d82Slm66018 
14403af08d82Slm66018 static void
14413af08d82Slm66018 vd_mark_in_reset(vd_t *vd)
14423af08d82Slm66018 {
14433af08d82Slm66018 	int status;
14443af08d82Slm66018 
14453af08d82Slm66018 	PR0("vd_mark_in_reset: marking vd in reset\n");
14463af08d82Slm66018 
14473af08d82Slm66018 	vd_need_reset(vd, B_FALSE);
14483af08d82Slm66018 	status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, DDI_SLEEP);
14493af08d82Slm66018 	if (status == DDI_FAILURE) {
14503af08d82Slm66018 		PR0("cannot schedule task to recv msg\n");
14513af08d82Slm66018 		vd_need_reset(vd, B_TRUE);
14523af08d82Slm66018 		return;
14533af08d82Slm66018 	}
14543af08d82Slm66018 }
14553af08d82Slm66018 
1456d10e4ef2Snarayan static int
14573c96341aSnarayan vd_mark_elem_done(vd_t *vd, int idx, int elem_status, int elem_nbytes)
1458d10e4ef2Snarayan {
1459d10e4ef2Snarayan 	boolean_t		accepted;
1460d10e4ef2Snarayan 	int			status;
1461d10e4ef2Snarayan 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
1462d10e4ef2Snarayan 
14633af08d82Slm66018 	if (vd->reset_state)
14643af08d82Slm66018 		return (0);
1465d10e4ef2Snarayan 
1466d10e4ef2Snarayan 	/* Acquire the element */
14673af08d82Slm66018 	if (!vd->reset_state &&
14683af08d82Slm66018 	    (status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) {
14693af08d82Slm66018 		if (status == ECONNRESET) {
14703af08d82Slm66018 			vd_mark_in_reset(vd);
14713af08d82Slm66018 			return (0);
14723af08d82Slm66018 		} else {
14733af08d82Slm66018 			PR0("ldc_mem_dring_acquire() returned errno %d",
14743af08d82Slm66018 			    status);
1475d10e4ef2Snarayan 			return (status);
1476d10e4ef2Snarayan 		}
14773af08d82Slm66018 	}
1478d10e4ef2Snarayan 
1479d10e4ef2Snarayan 	/* Set the element's status and mark it done */
1480d10e4ef2Snarayan 	accepted = (elem->hdr.dstate == VIO_DESC_ACCEPTED);
1481d10e4ef2Snarayan 	if (accepted) {
14823c96341aSnarayan 		elem->payload.nbytes	= elem_nbytes;
1483d10e4ef2Snarayan 		elem->payload.status	= elem_status;
1484d10e4ef2Snarayan 		elem->hdr.dstate	= VIO_DESC_DONE;
1485d10e4ef2Snarayan 	} else {
1486d10e4ef2Snarayan 		/* Perhaps client timed out waiting for I/O... */
14873af08d82Slm66018 		PR0("element %u no longer \"accepted\"", idx);
1488d10e4ef2Snarayan 		VD_DUMP_DRING_ELEM(elem);
1489d10e4ef2Snarayan 	}
1490d10e4ef2Snarayan 	/* Release the element */
14913af08d82Slm66018 	if (!vd->reset_state &&
14923af08d82Slm66018 	    (status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) {
14933af08d82Slm66018 		if (status == ECONNRESET) {
14943af08d82Slm66018 			vd_mark_in_reset(vd);
14953af08d82Slm66018 			return (0);
14963af08d82Slm66018 		} else {
14973af08d82Slm66018 			PR0("ldc_mem_dring_release() returned errno %d",
14983af08d82Slm66018 			    status);
1499d10e4ef2Snarayan 			return (status);
1500d10e4ef2Snarayan 		}
15013af08d82Slm66018 	}
1502d10e4ef2Snarayan 
1503d10e4ef2Snarayan 	return (accepted ? 0 : EINVAL);
1504d10e4ef2Snarayan }
1505d10e4ef2Snarayan 
1506205eeb1aSlm66018 /*
1507205eeb1aSlm66018  * Return Values
1508205eeb1aSlm66018  *	0	- operation completed successfully
1509205eeb1aSlm66018  *	EIO	- encountered LDC / task error
1510205eeb1aSlm66018  *
1511205eeb1aSlm66018  * Side Effect
1512205eeb1aSlm66018  *	sets request->status = <disk operation status>
1513205eeb1aSlm66018  */
1514205eeb1aSlm66018 static int
1515205eeb1aSlm66018 vd_complete_bio(vd_task_t *task)
1516d10e4ef2Snarayan {
1517d10e4ef2Snarayan 	int			status		= 0;
1518205eeb1aSlm66018 	int			rv		= 0;
1519d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
1520d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
1521d10e4ef2Snarayan 	struct buf		*buf		= &task->buf;
1522d10e4ef2Snarayan 
1523d10e4ef2Snarayan 
1524d10e4ef2Snarayan 	ASSERT(vd != NULL);
1525d10e4ef2Snarayan 	ASSERT(request != NULL);
1526d10e4ef2Snarayan 	ASSERT(task->msg != NULL);
1527d10e4ef2Snarayan 	ASSERT(task->msglen >= sizeof (*task->msg));
15283c96341aSnarayan 	ASSERT(!vd->file);
1529205eeb1aSlm66018 	ASSERT(request->slice != VD_SLICE_NONE);
1530d10e4ef2Snarayan 
1531205eeb1aSlm66018 	/* Wait for the I/O to complete [ call to ldi_strategy(9f) ] */
1532d10e4ef2Snarayan 	request->status = biowait(buf);
1533d10e4ef2Snarayan 
15343c96341aSnarayan 	/* return back the number of bytes read/written */
15353c96341aSnarayan 	request->nbytes = buf->b_bcount - buf->b_resid;
15363c96341aSnarayan 
15374bac2208Snarayan 	/* Release the buffer */
15383af08d82Slm66018 	if (!vd->reset_state)
15394bac2208Snarayan 		status = ldc_mem_release(task->mhdl, 0, buf->b_bcount);
15404bac2208Snarayan 	if (status) {
15413af08d82Slm66018 		PR0("ldc_mem_release() returned errno %d copying to "
15423af08d82Slm66018 		    "client", status);
15433af08d82Slm66018 		if (status == ECONNRESET) {
15443af08d82Slm66018 			vd_mark_in_reset(vd);
15453af08d82Slm66018 		}
1546205eeb1aSlm66018 		rv = EIO;
15471ae08745Sheppo 	}
15481ae08745Sheppo 
15493af08d82Slm66018 	/* Unmap the memory, even if in reset */
15504bac2208Snarayan 	status = ldc_mem_unmap(task->mhdl);
15514bac2208Snarayan 	if (status) {
15523af08d82Slm66018 		PR0("ldc_mem_unmap() returned errno %d copying to client",
15534bac2208Snarayan 		    status);
15543af08d82Slm66018 		if (status == ECONNRESET) {
15553af08d82Slm66018 			vd_mark_in_reset(vd);
15563af08d82Slm66018 		}
1557205eeb1aSlm66018 		rv = EIO;
15584bac2208Snarayan 	}
15594bac2208Snarayan 
1560d10e4ef2Snarayan 	biofini(buf);
15611ae08745Sheppo 
1562205eeb1aSlm66018 	return (rv);
1563205eeb1aSlm66018 }
1564205eeb1aSlm66018 
1565205eeb1aSlm66018 /*
1566205eeb1aSlm66018  * Description:
1567205eeb1aSlm66018  *	This function is called by the two functions called by a taskq
1568205eeb1aSlm66018  *	[ vd_complete_notify() and vd_serial_notify()) ] to send the
1569205eeb1aSlm66018  *	message to the client.
1570205eeb1aSlm66018  *
1571205eeb1aSlm66018  * Parameters:
1572205eeb1aSlm66018  *	arg 	- opaque pointer to structure containing task to be completed
1573205eeb1aSlm66018  *
1574205eeb1aSlm66018  * Return Values
1575205eeb1aSlm66018  *	None
1576205eeb1aSlm66018  */
1577205eeb1aSlm66018 static void
1578205eeb1aSlm66018 vd_notify(vd_task_t *task)
1579205eeb1aSlm66018 {
1580205eeb1aSlm66018 	int	status;
1581205eeb1aSlm66018 
1582205eeb1aSlm66018 	ASSERT(task != NULL);
1583205eeb1aSlm66018 	ASSERT(task->vd != NULL);
1584205eeb1aSlm66018 
1585205eeb1aSlm66018 	if (task->vd->reset_state)
1586205eeb1aSlm66018 		return;
1587205eeb1aSlm66018 
1588205eeb1aSlm66018 	/*
1589205eeb1aSlm66018 	 * Send the "ack" or "nack" back to the client; if sending the message
1590205eeb1aSlm66018 	 * via LDC fails, arrange to reset both the connection state and LDC
1591205eeb1aSlm66018 	 * itself
1592205eeb1aSlm66018 	 */
1593205eeb1aSlm66018 	PR2("Sending %s",
1594205eeb1aSlm66018 	    (task->msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK");
1595205eeb1aSlm66018 
1596205eeb1aSlm66018 	status = send_msg(task->vd->ldc_handle, task->msg, task->msglen);
1597205eeb1aSlm66018 	switch (status) {
1598205eeb1aSlm66018 	case 0:
1599205eeb1aSlm66018 		break;
1600205eeb1aSlm66018 	case ECONNRESET:
1601205eeb1aSlm66018 		vd_mark_in_reset(task->vd);
1602205eeb1aSlm66018 		break;
1603205eeb1aSlm66018 	default:
1604205eeb1aSlm66018 		PR0("initiating full reset");
1605205eeb1aSlm66018 		vd_need_reset(task->vd, B_TRUE);
1606205eeb1aSlm66018 		break;
1607205eeb1aSlm66018 	}
1608205eeb1aSlm66018 
1609205eeb1aSlm66018 	DTRACE_PROBE1(task__end, vd_task_t *, task);
1610205eeb1aSlm66018 }
1611205eeb1aSlm66018 
1612205eeb1aSlm66018 /*
1613205eeb1aSlm66018  * Description:
1614205eeb1aSlm66018  *	Mark the Dring entry as Done and (if necessary) send an ACK/NACK to
1615205eeb1aSlm66018  *	the vDisk client
1616205eeb1aSlm66018  *
1617205eeb1aSlm66018  * Parameters:
1618205eeb1aSlm66018  *	task 		- structure containing the request sent from client
1619205eeb1aSlm66018  *
1620205eeb1aSlm66018  * Return Values
1621205eeb1aSlm66018  *	None
1622205eeb1aSlm66018  */
1623205eeb1aSlm66018 static void
1624205eeb1aSlm66018 vd_complete_notify(vd_task_t *task)
1625205eeb1aSlm66018 {
1626205eeb1aSlm66018 	int			status		= 0;
1627205eeb1aSlm66018 	vd_t			*vd		= task->vd;
1628205eeb1aSlm66018 	vd_dring_payload_t	*request	= task->request;
1629205eeb1aSlm66018 
1630d10e4ef2Snarayan 	/* Update the dring element for a dring client */
1631205eeb1aSlm66018 	if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE)) {
16323c96341aSnarayan 		status = vd_mark_elem_done(vd, task->index,
16333c96341aSnarayan 		    request->status, request->nbytes);
16343af08d82Slm66018 		if (status == ECONNRESET)
16353af08d82Slm66018 			vd_mark_in_reset(vd);
16363af08d82Slm66018 	}
16371ae08745Sheppo 
1638d10e4ef2Snarayan 	/*
1639205eeb1aSlm66018 	 * If a transport error occurred while marking the element done or
1640205eeb1aSlm66018 	 * previously while executing the task, arrange to "nack" the message
1641205eeb1aSlm66018 	 * when the final task in the descriptor element range completes
1642d10e4ef2Snarayan 	 */
1643205eeb1aSlm66018 	if ((status != 0) || (task->status != 0))
1644d10e4ef2Snarayan 		task->msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
16451ae08745Sheppo 
1646d10e4ef2Snarayan 	/*
1647d10e4ef2Snarayan 	 * Only the final task for a range of elements will respond to and
1648d10e4ef2Snarayan 	 * free the message
1649d10e4ef2Snarayan 	 */
16503af08d82Slm66018 	if (task->type == VD_NONFINAL_RANGE_TASK) {
1651d10e4ef2Snarayan 		return;
16523af08d82Slm66018 	}
16531ae08745Sheppo 
1654205eeb1aSlm66018 	vd_notify(task);
1655205eeb1aSlm66018 }
1656205eeb1aSlm66018 
1657d10e4ef2Snarayan /*
1658205eeb1aSlm66018  * Description:
1659205eeb1aSlm66018  *	This is the basic completion function called to handle inband data
1660205eeb1aSlm66018  *	requests and handshake messages. All it needs to do is trigger a
1661205eeb1aSlm66018  *	message to the client that the request is completed.
1662205eeb1aSlm66018  *
1663205eeb1aSlm66018  * Parameters:
1664205eeb1aSlm66018  *	arg 	- opaque pointer to structure containing task to be completed
1665205eeb1aSlm66018  *
1666205eeb1aSlm66018  * Return Values
1667205eeb1aSlm66018  *	None
1668d10e4ef2Snarayan  */
1669205eeb1aSlm66018 static void
1670205eeb1aSlm66018 vd_serial_notify(void *arg)
1671205eeb1aSlm66018 {
1672205eeb1aSlm66018 	vd_task_t		*task = (vd_task_t *)arg;
1673205eeb1aSlm66018 
1674205eeb1aSlm66018 	ASSERT(task != NULL);
1675205eeb1aSlm66018 	vd_notify(task);
16761ae08745Sheppo }
16771ae08745Sheppo 
16780a55fbb7Slm66018 static void
16790a55fbb7Slm66018 vd_geom2dk_geom(void *vd_buf, void *ioctl_arg)
16800a55fbb7Slm66018 {
16810a55fbb7Slm66018 	VD_GEOM2DK_GEOM((vd_geom_t *)vd_buf, (struct dk_geom *)ioctl_arg);
16820a55fbb7Slm66018 }
16830a55fbb7Slm66018 
16840a55fbb7Slm66018 static void
16850a55fbb7Slm66018 vd_vtoc2vtoc(void *vd_buf, void *ioctl_arg)
16860a55fbb7Slm66018 {
16870a55fbb7Slm66018 	VD_VTOC2VTOC((vd_vtoc_t *)vd_buf, (struct vtoc *)ioctl_arg);
16880a55fbb7Slm66018 }
16890a55fbb7Slm66018 
16900a55fbb7Slm66018 static void
16910a55fbb7Slm66018 dk_geom2vd_geom(void *ioctl_arg, void *vd_buf)
16920a55fbb7Slm66018 {
16930a55fbb7Slm66018 	DK_GEOM2VD_GEOM((struct dk_geom *)ioctl_arg, (vd_geom_t *)vd_buf);
16940a55fbb7Slm66018 }
16950a55fbb7Slm66018 
16960a55fbb7Slm66018 static void
16970a55fbb7Slm66018 vtoc2vd_vtoc(void *ioctl_arg, void *vd_buf)
16980a55fbb7Slm66018 {
16990a55fbb7Slm66018 	VTOC2VD_VTOC((struct vtoc *)ioctl_arg, (vd_vtoc_t *)vd_buf);
17000a55fbb7Slm66018 }
17010a55fbb7Slm66018 
17024bac2208Snarayan static void
17034bac2208Snarayan vd_get_efi_in(void *vd_buf, void *ioctl_arg)
17044bac2208Snarayan {
17054bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
17064bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
17074bac2208Snarayan 
17084bac2208Snarayan 	dk_efi->dki_lba = vd_efi->lba;
17094bac2208Snarayan 	dk_efi->dki_length = vd_efi->length;
17104bac2208Snarayan 	dk_efi->dki_data = kmem_zalloc(vd_efi->length, KM_SLEEP);
17114bac2208Snarayan }
17124bac2208Snarayan 
17134bac2208Snarayan static void
17144bac2208Snarayan vd_get_efi_out(void *ioctl_arg, void *vd_buf)
17154bac2208Snarayan {
17164bac2208Snarayan 	int len;
17174bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
17184bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
17194bac2208Snarayan 
17204bac2208Snarayan 	len = vd_efi->length;
17214bac2208Snarayan 	DK_EFI2VD_EFI(dk_efi, vd_efi);
17224bac2208Snarayan 	kmem_free(dk_efi->dki_data, len);
17234bac2208Snarayan }
17244bac2208Snarayan 
17254bac2208Snarayan static void
17264bac2208Snarayan vd_set_efi_in(void *vd_buf, void *ioctl_arg)
17274bac2208Snarayan {
17284bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
17294bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
17304bac2208Snarayan 
17314bac2208Snarayan 	dk_efi->dki_data = kmem_alloc(vd_efi->length, KM_SLEEP);
17324bac2208Snarayan 	VD_EFI2DK_EFI(vd_efi, dk_efi);
17334bac2208Snarayan }
17344bac2208Snarayan 
17354bac2208Snarayan static void
17364bac2208Snarayan vd_set_efi_out(void *ioctl_arg, void *vd_buf)
17374bac2208Snarayan {
17384bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
17394bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
17404bac2208Snarayan 
17414bac2208Snarayan 	kmem_free(dk_efi->dki_data, vd_efi->length);
17424bac2208Snarayan }
17434bac2208Snarayan 
174478fcd0a1Sachartre static vd_disk_label_t
1745047ba61eSachartre vd_read_vtoc(vd_t *vd, struct vtoc *vtoc)
17464bac2208Snarayan {
17474bac2208Snarayan 	int status, rval;
17484bac2208Snarayan 	struct dk_gpt *efi;
17494bac2208Snarayan 	size_t efi_len;
17504bac2208Snarayan 
1751047ba61eSachartre 	ASSERT(vd->ldi_handle[0] != NULL);
1752047ba61eSachartre 
1753047ba61eSachartre 	status = ldi_ioctl(vd->ldi_handle[0], DKIOCGVTOC, (intptr_t)vtoc,
1754047ba61eSachartre 	    (vd->open_flags | FKIOCTL), kcred, &rval);
17554bac2208Snarayan 
17564bac2208Snarayan 	if (status == 0) {
175778fcd0a1Sachartre 		return (VD_DISK_LABEL_VTOC);
17584bac2208Snarayan 	} else if (status != ENOTSUP) {
17593af08d82Slm66018 		PR0("ldi_ioctl(DKIOCGVTOC) returned error %d", status);
176078fcd0a1Sachartre 		return (VD_DISK_LABEL_UNK);
17614bac2208Snarayan 	}
17624bac2208Snarayan 
1763047ba61eSachartre 	status = vds_efi_alloc_and_read(vd->ldi_handle[0], &efi, &efi_len);
17644bac2208Snarayan 
17654bac2208Snarayan 	if (status) {
17663af08d82Slm66018 		PR0("vds_efi_alloc_and_read returned error %d", status);
176778fcd0a1Sachartre 		return (VD_DISK_LABEL_UNK);
17684bac2208Snarayan 	}
17694bac2208Snarayan 
17704bac2208Snarayan 	vd_efi_to_vtoc(efi, vtoc);
17714bac2208Snarayan 	vd_efi_free(efi, efi_len);
17724bac2208Snarayan 
177378fcd0a1Sachartre 	return (VD_DISK_LABEL_EFI);
17744bac2208Snarayan }
17754bac2208Snarayan 
1776690555a1Sachartre static ushort_t
17773c96341aSnarayan vd_lbl2cksum(struct dk_label *label)
17783c96341aSnarayan {
17793c96341aSnarayan 	int	count;
1780690555a1Sachartre 	ushort_t sum, *sp;
17813c96341aSnarayan 
17823c96341aSnarayan 	count =	(sizeof (struct dk_label)) / (sizeof (short)) - 1;
1783690555a1Sachartre 	sp = (ushort_t *)label;
17843c96341aSnarayan 	sum = 0;
17853c96341aSnarayan 	while (count--) {
17863c96341aSnarayan 		sum ^= *sp++;
17873c96341aSnarayan 	}
17883c96341aSnarayan 
17893c96341aSnarayan 	return (sum);
17903c96341aSnarayan }
17913c96341aSnarayan 
179287a7269eSachartre /*
179387a7269eSachartre  * Handle ioctls to a disk slice.
1794205eeb1aSlm66018  *
1795205eeb1aSlm66018  * Return Values
1796205eeb1aSlm66018  *	0	- Indicates that there are no errors in disk operations
1797205eeb1aSlm66018  *	ENOTSUP	- Unknown disk label type or unsupported DKIO ioctl
1798205eeb1aSlm66018  *	EINVAL	- Not enough room to copy the EFI label
1799205eeb1aSlm66018  *
180087a7269eSachartre  */
18011ae08745Sheppo static int
18020a55fbb7Slm66018 vd_do_slice_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
18031ae08745Sheppo {
18044bac2208Snarayan 	dk_efi_t *dk_ioc;
18054bac2208Snarayan 
18064bac2208Snarayan 	switch (vd->vdisk_label) {
18074bac2208Snarayan 
180887a7269eSachartre 	/* ioctls for a slice from a disk with a VTOC label */
18094bac2208Snarayan 	case VD_DISK_LABEL_VTOC:
18104bac2208Snarayan 
18111ae08745Sheppo 		switch (cmd) {
18121ae08745Sheppo 		case DKIOCGGEOM:
18130a55fbb7Slm66018 			ASSERT(ioctl_arg != NULL);
18140a55fbb7Slm66018 			bcopy(&vd->dk_geom, ioctl_arg, sizeof (vd->dk_geom));
18151ae08745Sheppo 			return (0);
18161ae08745Sheppo 		case DKIOCGVTOC:
18170a55fbb7Slm66018 			ASSERT(ioctl_arg != NULL);
18180a55fbb7Slm66018 			bcopy(&vd->vtoc, ioctl_arg, sizeof (vd->vtoc));
18191ae08745Sheppo 			return (0);
182087a7269eSachartre 		default:
18213c96341aSnarayan 			return (ENOTSUP);
182287a7269eSachartre 		}
182387a7269eSachartre 
182487a7269eSachartre 	/* ioctls for a slice from a disk with an EFI label */
182587a7269eSachartre 	case VD_DISK_LABEL_EFI:
182687a7269eSachartre 
182787a7269eSachartre 		switch (cmd) {
182887a7269eSachartre 		case DKIOCGETEFI:
18293c96341aSnarayan 			ASSERT(ioctl_arg != NULL);
183087a7269eSachartre 			dk_ioc = (dk_efi_t *)ioctl_arg;
183187a7269eSachartre 			if (dk_ioc->dki_length < vd->dk_efi.dki_length)
183287a7269eSachartre 				return (EINVAL);
183387a7269eSachartre 			bcopy(vd->dk_efi.dki_data, dk_ioc->dki_data,
183487a7269eSachartre 			    vd->dk_efi.dki_length);
183587a7269eSachartre 			return (0);
183687a7269eSachartre 		default:
183787a7269eSachartre 			return (ENOTSUP);
183887a7269eSachartre 		}
183987a7269eSachartre 
184087a7269eSachartre 	default:
1841205eeb1aSlm66018 		/* Unknown disk label type */
184287a7269eSachartre 		return (ENOTSUP);
184387a7269eSachartre 	}
184487a7269eSachartre }
184587a7269eSachartre 
184687a7269eSachartre /*
184778fcd0a1Sachartre  * Function:
184878fcd0a1Sachartre  *	vd_file_validate_geometry
1849205eeb1aSlm66018  *
185078fcd0a1Sachartre  * Description:
185178fcd0a1Sachartre  *	Read the label and validate the geometry of a disk image. The driver
185278fcd0a1Sachartre  *	label, vtoc and geometry information are updated according to the
185378fcd0a1Sachartre  *	label read from the disk image.
185478fcd0a1Sachartre  *
185578fcd0a1Sachartre  *	If no valid label is found, the label is set to unknown and the
185678fcd0a1Sachartre  *	function returns EINVAL, but a default vtoc and geometry are provided
185778fcd0a1Sachartre  *	to the driver.
185878fcd0a1Sachartre  *
185978fcd0a1Sachartre  * Parameters:
186078fcd0a1Sachartre  *	vd	- disk on which the operation is performed.
186178fcd0a1Sachartre  *
186278fcd0a1Sachartre  * Return Code:
186378fcd0a1Sachartre  *	0	- success.
186478fcd0a1Sachartre  *	EIO	- error reading the label from the disk image.
186578fcd0a1Sachartre  *	EINVAL	- unknown disk label.
186687a7269eSachartre  */
186787a7269eSachartre static int
186878fcd0a1Sachartre vd_file_validate_geometry(vd_t *vd)
186987a7269eSachartre {
187087a7269eSachartre 	struct dk_label label;
187178fcd0a1Sachartre 	struct dk_geom *geom = &vd->dk_geom;
187278fcd0a1Sachartre 	struct vtoc *vtoc = &vd->vtoc;
187378fcd0a1Sachartre 	int i;
187478fcd0a1Sachartre 	int status = 0;
187587a7269eSachartre 
187687a7269eSachartre 	ASSERT(vd->file);
187787a7269eSachartre 
1878047ba61eSachartre 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
1879047ba61eSachartre 		/*
1880047ba61eSachartre 		 * For single slice disk we always fake the geometry, and we
1881047ba61eSachartre 		 * only need to do it once because the geometry will never
1882047ba61eSachartre 		 * change.
1883047ba61eSachartre 		 */
1884047ba61eSachartre 		if (vd->vdisk_label == VD_DISK_LABEL_VTOC)
1885047ba61eSachartre 			/* geometry was already validated */
1886047ba61eSachartre 			return (0);
1887047ba61eSachartre 
1888047ba61eSachartre 		ASSERT(vd->vdisk_label == VD_DISK_LABEL_UNK);
1889047ba61eSachartre 		vd_file_build_default_label(vd, &label);
1890047ba61eSachartre 		vd->vdisk_label = VD_DISK_LABEL_VTOC;
1891047ba61eSachartre 	} else {
189287a7269eSachartre 		if (VD_FILE_LABEL_READ(vd, &label) < 0)
189387a7269eSachartre 			return (EIO);
189487a7269eSachartre 
189587a7269eSachartre 		if (label.dkl_magic != DKL_MAGIC ||
189678fcd0a1Sachartre 		    label.dkl_cksum != vd_lbl2cksum(&label) ||
189778fcd0a1Sachartre 		    label.dkl_vtoc.v_sanity != VTOC_SANE ||
189878fcd0a1Sachartre 		    label.dkl_vtoc.v_nparts != V_NUMPAR) {
189978fcd0a1Sachartre 			vd->vdisk_label = VD_DISK_LABEL_UNK;
190078fcd0a1Sachartre 			vd_file_build_default_label(vd, &label);
190178fcd0a1Sachartre 			status = EINVAL;
190278fcd0a1Sachartre 		} else {
190378fcd0a1Sachartre 			vd->vdisk_label = VD_DISK_LABEL_VTOC;
190478fcd0a1Sachartre 		}
1905047ba61eSachartre 	}
190687a7269eSachartre 
190778fcd0a1Sachartre 	/* Update the driver geometry */
190887a7269eSachartre 	bzero(geom, sizeof (struct dk_geom));
190978fcd0a1Sachartre 
191087a7269eSachartre 	geom->dkg_ncyl = label.dkl_ncyl;
191187a7269eSachartre 	geom->dkg_acyl = label.dkl_acyl;
191287a7269eSachartre 	geom->dkg_nhead = label.dkl_nhead;
191387a7269eSachartre 	geom->dkg_nsect = label.dkl_nsect;
191487a7269eSachartre 	geom->dkg_intrlv = label.dkl_intrlv;
191587a7269eSachartre 	geom->dkg_apc = label.dkl_apc;
191687a7269eSachartre 	geom->dkg_rpm = label.dkl_rpm;
191787a7269eSachartre 	geom->dkg_pcyl = label.dkl_pcyl;
191887a7269eSachartre 	geom->dkg_write_reinstruct = label.dkl_write_reinstruct;
191987a7269eSachartre 	geom->dkg_read_reinstruct = label.dkl_read_reinstruct;
192087a7269eSachartre 
192178fcd0a1Sachartre 	/* Update the driver vtoc */
192287a7269eSachartre 	bzero(vtoc, sizeof (struct vtoc));
192387a7269eSachartre 
192487a7269eSachartre 	vtoc->v_sanity = label.dkl_vtoc.v_sanity;
192587a7269eSachartre 	vtoc->v_version = label.dkl_vtoc.v_version;
192687a7269eSachartre 	vtoc->v_sectorsz = DEV_BSIZE;
192787a7269eSachartre 	vtoc->v_nparts = label.dkl_vtoc.v_nparts;
192887a7269eSachartre 
192987a7269eSachartre 	for (i = 0; i < vtoc->v_nparts; i++) {
193087a7269eSachartre 		vtoc->v_part[i].p_tag =
193187a7269eSachartre 		    label.dkl_vtoc.v_part[i].p_tag;
193287a7269eSachartre 		vtoc->v_part[i].p_flag =
193387a7269eSachartre 		    label.dkl_vtoc.v_part[i].p_flag;
193487a7269eSachartre 		vtoc->v_part[i].p_start =
193587a7269eSachartre 		    label.dkl_map[i].dkl_cylno *
193687a7269eSachartre 		    (label.dkl_nhead * label.dkl_nsect);
193787a7269eSachartre 		vtoc->v_part[i].p_size = label.dkl_map[i].dkl_nblk;
193887a7269eSachartre 		vtoc->timestamp[i] =
193987a7269eSachartre 		    label.dkl_vtoc.v_timestamp[i];
194087a7269eSachartre 	}
194187a7269eSachartre 	/*
194287a7269eSachartre 	 * The bootinfo array can not be copied with bcopy() because
194387a7269eSachartre 	 * elements are of type long in vtoc (so 64-bit) and of type
194487a7269eSachartre 	 * int in dk_vtoc (so 32-bit).
194587a7269eSachartre 	 */
194687a7269eSachartre 	vtoc->v_bootinfo[0] = label.dkl_vtoc.v_bootinfo[0];
194787a7269eSachartre 	vtoc->v_bootinfo[1] = label.dkl_vtoc.v_bootinfo[1];
194887a7269eSachartre 	vtoc->v_bootinfo[2] = label.dkl_vtoc.v_bootinfo[2];
194987a7269eSachartre 	bcopy(label.dkl_asciilabel, vtoc->v_asciilabel,
195087a7269eSachartre 	    LEN_DKL_ASCII);
195187a7269eSachartre 	bcopy(label.dkl_vtoc.v_volume, vtoc->v_volume,
195287a7269eSachartre 	    LEN_DKL_VVOL);
195387a7269eSachartre 
195478fcd0a1Sachartre 	return (status);
195578fcd0a1Sachartre }
195678fcd0a1Sachartre 
195778fcd0a1Sachartre /*
195878fcd0a1Sachartre  * Handle ioctls to a disk image (file-based).
195978fcd0a1Sachartre  *
196078fcd0a1Sachartre  * Return Values
196178fcd0a1Sachartre  *	0	- Indicates that there are no errors
196278fcd0a1Sachartre  *	!= 0	- Disk operation returned an error
196378fcd0a1Sachartre  */
196478fcd0a1Sachartre static int
196578fcd0a1Sachartre vd_do_file_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
196678fcd0a1Sachartre {
196778fcd0a1Sachartre 	struct dk_label label;
196878fcd0a1Sachartre 	struct dk_geom *geom;
196978fcd0a1Sachartre 	struct vtoc *vtoc;
197078fcd0a1Sachartre 	int i, rc;
197178fcd0a1Sachartre 
197278fcd0a1Sachartre 	ASSERT(vd->file);
197378fcd0a1Sachartre 
197478fcd0a1Sachartre 	switch (cmd) {
197578fcd0a1Sachartre 
197678fcd0a1Sachartre 	case DKIOCGGEOM:
197778fcd0a1Sachartre 		ASSERT(ioctl_arg != NULL);
197878fcd0a1Sachartre 		geom = (struct dk_geom *)ioctl_arg;
197978fcd0a1Sachartre 
198078fcd0a1Sachartre 		rc = vd_file_validate_geometry(vd);
1981047ba61eSachartre 		if (rc != 0 && rc != EINVAL) {
1982047ba61eSachartre 			ASSERT(vd->vdisk_type != VD_DISK_TYPE_SLICE);
198378fcd0a1Sachartre 			return (rc);
1984047ba61eSachartre 		}
198578fcd0a1Sachartre 
198678fcd0a1Sachartre 		bcopy(&vd->dk_geom, geom, sizeof (struct dk_geom));
198778fcd0a1Sachartre 		return (0);
198878fcd0a1Sachartre 
198978fcd0a1Sachartre 	case DKIOCGVTOC:
199078fcd0a1Sachartre 		ASSERT(ioctl_arg != NULL);
199178fcd0a1Sachartre 		vtoc = (struct vtoc *)ioctl_arg;
199278fcd0a1Sachartre 
199378fcd0a1Sachartre 		rc = vd_file_validate_geometry(vd);
1994047ba61eSachartre 		if (rc != 0 && rc != EINVAL) {
1995047ba61eSachartre 			ASSERT(vd->vdisk_type != VD_DISK_TYPE_SLICE);
199678fcd0a1Sachartre 			return (rc);
1997047ba61eSachartre 		}
199878fcd0a1Sachartre 
199978fcd0a1Sachartre 		bcopy(&vd->vtoc, vtoc, sizeof (struct vtoc));
200087a7269eSachartre 		return (0);
200187a7269eSachartre 
200287a7269eSachartre 	case DKIOCSGEOM:
200387a7269eSachartre 		ASSERT(ioctl_arg != NULL);
200487a7269eSachartre 		geom = (struct dk_geom *)ioctl_arg;
200587a7269eSachartre 
2006047ba61eSachartre 		/* geometry can only be changed for full disk */
2007047ba61eSachartre 		if (vd->vdisk_type != VD_DISK_TYPE_DISK)
2008047ba61eSachartre 			return (ENOTSUP);
2009047ba61eSachartre 
201087a7269eSachartre 		if (geom->dkg_nhead == 0 || geom->dkg_nsect == 0)
201187a7269eSachartre 			return (EINVAL);
201287a7269eSachartre 
201387a7269eSachartre 		/*
201487a7269eSachartre 		 * The current device geometry is not updated, just the driver
201587a7269eSachartre 		 * "notion" of it. The device geometry will be effectively
201687a7269eSachartre 		 * updated when a label is written to the device during a next
201787a7269eSachartre 		 * DKIOCSVTOC.
201887a7269eSachartre 		 */
201987a7269eSachartre 		bcopy(ioctl_arg, &vd->dk_geom, sizeof (vd->dk_geom));
202087a7269eSachartre 		return (0);
202187a7269eSachartre 
202287a7269eSachartre 	case DKIOCSVTOC:
202387a7269eSachartre 		ASSERT(ioctl_arg != NULL);
202487a7269eSachartre 		ASSERT(vd->dk_geom.dkg_nhead != 0 &&
202587a7269eSachartre 		    vd->dk_geom.dkg_nsect != 0);
2026690555a1Sachartre 		vtoc = (struct vtoc *)ioctl_arg;
2027690555a1Sachartre 
2028047ba61eSachartre 		/* vtoc can only be changed for full disk */
2029047ba61eSachartre 		if (vd->vdisk_type != VD_DISK_TYPE_DISK)
2030047ba61eSachartre 			return (ENOTSUP);
2031047ba61eSachartre 
2032690555a1Sachartre 		if (vtoc->v_sanity != VTOC_SANE ||
2033690555a1Sachartre 		    vtoc->v_sectorsz != DEV_BSIZE ||
2034690555a1Sachartre 		    vtoc->v_nparts != V_NUMPAR)
2035690555a1Sachartre 			return (EINVAL);
2036690555a1Sachartre 
2037690555a1Sachartre 		bzero(&label, sizeof (label));
2038690555a1Sachartre 		label.dkl_ncyl = vd->dk_geom.dkg_ncyl;
2039690555a1Sachartre 		label.dkl_acyl = vd->dk_geom.dkg_acyl;
2040690555a1Sachartre 		label.dkl_pcyl = vd->dk_geom.dkg_pcyl;
2041690555a1Sachartre 		label.dkl_nhead = vd->dk_geom.dkg_nhead;
2042690555a1Sachartre 		label.dkl_nsect = vd->dk_geom.dkg_nsect;
2043690555a1Sachartre 		label.dkl_intrlv = vd->dk_geom.dkg_intrlv;
2044690555a1Sachartre 		label.dkl_apc = vd->dk_geom.dkg_apc;
2045690555a1Sachartre 		label.dkl_rpm = vd->dk_geom.dkg_rpm;
204687a7269eSachartre 		label.dkl_write_reinstruct = vd->dk_geom.dkg_write_reinstruct;
204787a7269eSachartre 		label.dkl_read_reinstruct = vd->dk_geom.dkg_read_reinstruct;
2048690555a1Sachartre 
204987a7269eSachartre 		label.dkl_vtoc.v_nparts = V_NUMPAR;
205087a7269eSachartre 		label.dkl_vtoc.v_sanity = VTOC_SANE;
2051690555a1Sachartre 		label.dkl_vtoc.v_version = vtoc->v_version;
205287a7269eSachartre 		for (i = 0; i < V_NUMPAR; i++) {
2053690555a1Sachartre 			label.dkl_vtoc.v_timestamp[i] =
2054690555a1Sachartre 			    vtoc->timestamp[i];
2055690555a1Sachartre 			label.dkl_vtoc.v_part[i].p_tag =
2056690555a1Sachartre 			    vtoc->v_part[i].p_tag;
2057690555a1Sachartre 			label.dkl_vtoc.v_part[i].p_flag =
2058690555a1Sachartre 			    vtoc->v_part[i].p_flag;
2059690555a1Sachartre 			label.dkl_map[i].dkl_cylno =
2060690555a1Sachartre 			    vtoc->v_part[i].p_start /
2061690555a1Sachartre 			    (label.dkl_nhead * label.dkl_nsect);
2062690555a1Sachartre 			label.dkl_map[i].dkl_nblk =
2063690555a1Sachartre 			    vtoc->v_part[i].p_size;
20643c96341aSnarayan 		}
206587a7269eSachartre 		/*
206687a7269eSachartre 		 * The bootinfo array can not be copied with bcopy() because
206787a7269eSachartre 		 * elements are of type long in vtoc (so 64-bit) and of type
206887a7269eSachartre 		 * int in dk_vtoc (so 32-bit).
206987a7269eSachartre 		 */
207087a7269eSachartre 		label.dkl_vtoc.v_bootinfo[0] = vtoc->v_bootinfo[0];
207187a7269eSachartre 		label.dkl_vtoc.v_bootinfo[1] = vtoc->v_bootinfo[1];
207287a7269eSachartre 		label.dkl_vtoc.v_bootinfo[2] = vtoc->v_bootinfo[2];
2073690555a1Sachartre 		bcopy(vtoc->v_asciilabel, label.dkl_asciilabel,
2074690555a1Sachartre 		    LEN_DKL_ASCII);
2075690555a1Sachartre 		bcopy(vtoc->v_volume, label.dkl_vtoc.v_volume,
2076690555a1Sachartre 		    LEN_DKL_VVOL);
20773c96341aSnarayan 
20783c96341aSnarayan 		/* re-compute checksum */
2079690555a1Sachartre 		label.dkl_magic = DKL_MAGIC;
2080690555a1Sachartre 		label.dkl_cksum = vd_lbl2cksum(&label);
2081690555a1Sachartre 
208287a7269eSachartre 		/* write label to the disk image */
208387a7269eSachartre 		if ((rc = vd_file_set_vtoc(vd, &label)) != 0)
208487a7269eSachartre 			return (rc);
2085690555a1Sachartre 
208678fcd0a1Sachartre 		/* check the geometry and update the driver info */
208778fcd0a1Sachartre 		if ((rc = vd_file_validate_geometry(vd)) != 0)
208878fcd0a1Sachartre 			return (rc);
20893c96341aSnarayan 
209087a7269eSachartre 		/*
209187a7269eSachartre 		 * The disk geometry may have changed, so we need to write
209287a7269eSachartre 		 * the devid (if there is one) so that it is stored at the
209387a7269eSachartre 		 * right location.
209487a7269eSachartre 		 */
209587a7269eSachartre 		if (vd->file_devid != NULL &&
209687a7269eSachartre 		    vd_file_write_devid(vd, vd->file_devid) != 0) {
209787a7269eSachartre 			PR0("Fail to write devid");
20981ae08745Sheppo 		}
20994bac2208Snarayan 
21004bac2208Snarayan 		return (0);
21014bac2208Snarayan 
21028259acd8Szk194757 	case DKIOCFLUSHWRITECACHE:
2103da6c28aaSamw 		return (VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL));
21048259acd8Szk194757 
21054bac2208Snarayan 	default:
21064bac2208Snarayan 		return (ENOTSUP);
21074bac2208Snarayan 	}
21081ae08745Sheppo }
21091ae08745Sheppo 
2110205eeb1aSlm66018 /*
2111205eeb1aSlm66018  * Description:
2112205eeb1aSlm66018  *	This is the function that processes the ioctl requests (farming it
2113205eeb1aSlm66018  *	out to functions that handle slices, files or whole disks)
2114205eeb1aSlm66018  *
2115205eeb1aSlm66018  * Return Values
2116205eeb1aSlm66018  *     0		- ioctl operation completed successfully
2117205eeb1aSlm66018  *     != 0		- The LDC error value encountered
2118205eeb1aSlm66018  *			  (propagated back up the call stack as a task error)
2119205eeb1aSlm66018  *
2120205eeb1aSlm66018  * Side Effect
2121205eeb1aSlm66018  *     sets request->status to the return value of the ioctl function.
2122205eeb1aSlm66018  */
21231ae08745Sheppo static int
21240a55fbb7Slm66018 vd_do_ioctl(vd_t *vd, vd_dring_payload_t *request, void* buf, vd_ioctl_t *ioctl)
21251ae08745Sheppo {
2126205eeb1aSlm66018 	int	rval = 0, status = 0;
21271ae08745Sheppo 	size_t	nbytes = request->nbytes;	/* modifiable copy */
21281ae08745Sheppo 
21291ae08745Sheppo 
21301ae08745Sheppo 	ASSERT(request->slice < vd->nslices);
21311ae08745Sheppo 	PR0("Performing %s", ioctl->operation_name);
21321ae08745Sheppo 
21330a55fbb7Slm66018 	/* Get data from client and convert, if necessary */
21340a55fbb7Slm66018 	if (ioctl->copyin != NULL)  {
21351ae08745Sheppo 		ASSERT(nbytes != 0 && buf != NULL);
21361ae08745Sheppo 		PR1("Getting \"arg\" data from client");
21371ae08745Sheppo 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
21381ae08745Sheppo 		    request->cookie, request->ncookies,
21391ae08745Sheppo 		    LDC_COPY_IN)) != 0) {
21403af08d82Slm66018 			PR0("ldc_mem_copy() returned errno %d "
21411ae08745Sheppo 			    "copying from client", status);
21421ae08745Sheppo 			return (status);
21431ae08745Sheppo 		}
21440a55fbb7Slm66018 
21450a55fbb7Slm66018 		/* Convert client's data, if necessary */
21460a55fbb7Slm66018 		if (ioctl->copyin == VD_IDENTITY)	/* use client buffer */
21470a55fbb7Slm66018 			ioctl->arg = buf;
21480a55fbb7Slm66018 		else	/* convert client vdisk operation data to ioctl data */
21490a55fbb7Slm66018 			(ioctl->copyin)(buf, (void *)ioctl->arg);
21501ae08745Sheppo 	}
21511ae08745Sheppo 
21521ae08745Sheppo 	/*
21531ae08745Sheppo 	 * Handle single-slice block devices internally; otherwise, have the
21541ae08745Sheppo 	 * real driver perform the ioctl()
21551ae08745Sheppo 	 */
215687a7269eSachartre 	if (vd->file) {
2157205eeb1aSlm66018 		request->status =
2158205eeb1aSlm66018 		    vd_do_file_ioctl(vd, ioctl->cmd, (void *)ioctl->arg);
2159205eeb1aSlm66018 
216087a7269eSachartre 	} else if (vd->vdisk_type == VD_DISK_TYPE_SLICE && !vd->pseudo) {
2161205eeb1aSlm66018 		request->status =
2162205eeb1aSlm66018 		    vd_do_slice_ioctl(vd, ioctl->cmd, (void *)ioctl->arg);
2163205eeb1aSlm66018 
2164205eeb1aSlm66018 	} else {
2165205eeb1aSlm66018 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
2166047ba61eSachartre 		    ioctl->cmd, (intptr_t)ioctl->arg, vd->open_flags | FKIOCTL,
2167205eeb1aSlm66018 		    kcred, &rval);
2168205eeb1aSlm66018 
21691ae08745Sheppo #ifdef DEBUG
21701ae08745Sheppo 		if (rval != 0) {
2171205eeb1aSlm66018 			PR0("%s set rval = %d, which is not being returned to"
2172205eeb1aSlm66018 			    " client", ioctl->cmd_name, rval);
21731ae08745Sheppo 		}
21741ae08745Sheppo #endif /* DEBUG */
2175205eeb1aSlm66018 	}
2176205eeb1aSlm66018 
2177205eeb1aSlm66018 	if (request->status != 0) {
2178205eeb1aSlm66018 		PR0("ioctl(%s) = errno %d", ioctl->cmd_name, request->status);
2179205eeb1aSlm66018 		return (0);
2180205eeb1aSlm66018 	}
21811ae08745Sheppo 
21820a55fbb7Slm66018 	/* Convert data and send to client, if necessary */
21830a55fbb7Slm66018 	if (ioctl->copyout != NULL)  {
21841ae08745Sheppo 		ASSERT(nbytes != 0 && buf != NULL);
21851ae08745Sheppo 		PR1("Sending \"arg\" data to client");
21860a55fbb7Slm66018 
21870a55fbb7Slm66018 		/* Convert ioctl data to vdisk operation data, if necessary */
21880a55fbb7Slm66018 		if (ioctl->copyout != VD_IDENTITY)
21890a55fbb7Slm66018 			(ioctl->copyout)((void *)ioctl->arg, buf);
21900a55fbb7Slm66018 
21911ae08745Sheppo 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
21921ae08745Sheppo 		    request->cookie, request->ncookies,
21931ae08745Sheppo 		    LDC_COPY_OUT)) != 0) {
21943af08d82Slm66018 			PR0("ldc_mem_copy() returned errno %d "
21951ae08745Sheppo 			    "copying to client", status);
21961ae08745Sheppo 			return (status);
21971ae08745Sheppo 		}
21981ae08745Sheppo 	}
21991ae08745Sheppo 
22001ae08745Sheppo 	return (status);
22011ae08745Sheppo }
22021ae08745Sheppo 
22031ae08745Sheppo #define	RNDSIZE(expr) P2ROUNDUP(sizeof (expr), sizeof (uint64_t))
2204205eeb1aSlm66018 
2205205eeb1aSlm66018 /*
2206205eeb1aSlm66018  * Description:
2207205eeb1aSlm66018  *	This generic function is called by the task queue to complete
2208205eeb1aSlm66018  *	the processing of the tasks. The specific completion function
2209205eeb1aSlm66018  *	is passed in as a field in the task pointer.
2210205eeb1aSlm66018  *
2211205eeb1aSlm66018  * Parameters:
2212205eeb1aSlm66018  *	arg 	- opaque pointer to structure containing task to be completed
2213205eeb1aSlm66018  *
2214205eeb1aSlm66018  * Return Values
2215205eeb1aSlm66018  *	None
2216205eeb1aSlm66018  */
2217205eeb1aSlm66018 static void
2218205eeb1aSlm66018 vd_complete(void *arg)
2219205eeb1aSlm66018 {
2220205eeb1aSlm66018 	vd_task_t	*task = (vd_task_t *)arg;
2221205eeb1aSlm66018 
2222205eeb1aSlm66018 	ASSERT(task != NULL);
2223205eeb1aSlm66018 	ASSERT(task->status == EINPROGRESS);
2224205eeb1aSlm66018 	ASSERT(task->completef != NULL);
2225205eeb1aSlm66018 
2226205eeb1aSlm66018 	task->status = task->completef(task);
2227205eeb1aSlm66018 	if (task->status)
2228205eeb1aSlm66018 		PR0("%s: Error %d completing task", __func__, task->status);
2229205eeb1aSlm66018 
2230205eeb1aSlm66018 	/* Now notify the vDisk client */
2231205eeb1aSlm66018 	vd_complete_notify(task);
2232205eeb1aSlm66018 }
2233205eeb1aSlm66018 
22341ae08745Sheppo static int
2235d10e4ef2Snarayan vd_ioctl(vd_task_t *task)
22361ae08745Sheppo {
223787a7269eSachartre 	int			i, status;
22381ae08745Sheppo 	void			*buf = NULL;
22390a55fbb7Slm66018 	struct dk_geom		dk_geom = {0};
22400a55fbb7Slm66018 	struct vtoc		vtoc = {0};
22414bac2208Snarayan 	struct dk_efi		dk_efi = {0};
2242d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
2243d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
22440a55fbb7Slm66018 	vd_ioctl_t		ioctl[] = {
22450a55fbb7Slm66018 		/* Command (no-copy) operations */
22460a55fbb7Slm66018 		{VD_OP_FLUSH, STRINGIZE(VD_OP_FLUSH), 0,
22470a55fbb7Slm66018 		    DKIOCFLUSHWRITECACHE, STRINGIZE(DKIOCFLUSHWRITECACHE),
2248047ba61eSachartre 		    NULL, NULL, NULL, B_TRUE},
22490a55fbb7Slm66018 
22500a55fbb7Slm66018 		/* "Get" (copy-out) operations */
22510a55fbb7Slm66018 		{VD_OP_GET_WCE, STRINGIZE(VD_OP_GET_WCE), RNDSIZE(int),
22520a55fbb7Slm66018 		    DKIOCGETWCE, STRINGIZE(DKIOCGETWCE),
2253047ba61eSachartre 		    NULL, VD_IDENTITY, VD_IDENTITY, B_FALSE},
22540a55fbb7Slm66018 		{VD_OP_GET_DISKGEOM, STRINGIZE(VD_OP_GET_DISKGEOM),
22550a55fbb7Slm66018 		    RNDSIZE(vd_geom_t),
22560a55fbb7Slm66018 		    DKIOCGGEOM, STRINGIZE(DKIOCGGEOM),
2257047ba61eSachartre 		    &dk_geom, NULL, dk_geom2vd_geom, B_FALSE},
22580a55fbb7Slm66018 		{VD_OP_GET_VTOC, STRINGIZE(VD_OP_GET_VTOC), RNDSIZE(vd_vtoc_t),
22590a55fbb7Slm66018 		    DKIOCGVTOC, STRINGIZE(DKIOCGVTOC),
2260047ba61eSachartre 		    &vtoc, NULL, vtoc2vd_vtoc, B_FALSE},
22614bac2208Snarayan 		{VD_OP_GET_EFI, STRINGIZE(VD_OP_GET_EFI), RNDSIZE(vd_efi_t),
22624bac2208Snarayan 		    DKIOCGETEFI, STRINGIZE(DKIOCGETEFI),
2263047ba61eSachartre 		    &dk_efi, vd_get_efi_in, vd_get_efi_out, B_FALSE},
22640a55fbb7Slm66018 
22650a55fbb7Slm66018 		/* "Set" (copy-in) operations */
22660a55fbb7Slm66018 		{VD_OP_SET_WCE, STRINGIZE(VD_OP_SET_WCE), RNDSIZE(int),
22670a55fbb7Slm66018 		    DKIOCSETWCE, STRINGIZE(DKIOCSETWCE),
2268047ba61eSachartre 		    NULL, VD_IDENTITY, VD_IDENTITY, B_TRUE},
22690a55fbb7Slm66018 		{VD_OP_SET_DISKGEOM, STRINGIZE(VD_OP_SET_DISKGEOM),
22700a55fbb7Slm66018 		    RNDSIZE(vd_geom_t),
22710a55fbb7Slm66018 		    DKIOCSGEOM, STRINGIZE(DKIOCSGEOM),
2272047ba61eSachartre 		    &dk_geom, vd_geom2dk_geom, NULL, B_TRUE},
22730a55fbb7Slm66018 		{VD_OP_SET_VTOC, STRINGIZE(VD_OP_SET_VTOC), RNDSIZE(vd_vtoc_t),
22740a55fbb7Slm66018 		    DKIOCSVTOC, STRINGIZE(DKIOCSVTOC),
2275047ba61eSachartre 		    &vtoc, vd_vtoc2vtoc, NULL, B_TRUE},
22764bac2208Snarayan 		{VD_OP_SET_EFI, STRINGIZE(VD_OP_SET_EFI), RNDSIZE(vd_efi_t),
22774bac2208Snarayan 		    DKIOCSETEFI, STRINGIZE(DKIOCSETEFI),
2278047ba61eSachartre 		    &dk_efi, vd_set_efi_in, vd_set_efi_out, B_TRUE},
22790a55fbb7Slm66018 	};
22801ae08745Sheppo 	size_t		nioctls = (sizeof (ioctl))/(sizeof (ioctl[0]));
22811ae08745Sheppo 
22821ae08745Sheppo 
2283d10e4ef2Snarayan 	ASSERT(vd != NULL);
2284d10e4ef2Snarayan 	ASSERT(request != NULL);
22851ae08745Sheppo 	ASSERT(request->slice < vd->nslices);
22861ae08745Sheppo 
22871ae08745Sheppo 	/*
22881ae08745Sheppo 	 * Determine ioctl corresponding to caller's "operation" and
22891ae08745Sheppo 	 * validate caller's "nbytes"
22901ae08745Sheppo 	 */
22911ae08745Sheppo 	for (i = 0; i < nioctls; i++) {
22921ae08745Sheppo 		if (request->operation == ioctl[i].operation) {
22930a55fbb7Slm66018 			/* LDC memory operations require 8-byte multiples */
22940a55fbb7Slm66018 			ASSERT(ioctl[i].nbytes % sizeof (uint64_t) == 0);
22950a55fbb7Slm66018 
22964bac2208Snarayan 			if (request->operation == VD_OP_GET_EFI ||
22974bac2208Snarayan 			    request->operation == VD_OP_SET_EFI) {
22984bac2208Snarayan 				if (request->nbytes >= ioctl[i].nbytes)
22994bac2208Snarayan 					break;
23003af08d82Slm66018 				PR0("%s:  Expected at least nbytes = %lu, "
23014bac2208Snarayan 				    "got %lu", ioctl[i].operation_name,
23024bac2208Snarayan 				    ioctl[i].nbytes, request->nbytes);
23034bac2208Snarayan 				return (EINVAL);
23044bac2208Snarayan 			}
23054bac2208Snarayan 
23060a55fbb7Slm66018 			if (request->nbytes != ioctl[i].nbytes) {
23073af08d82Slm66018 				PR0("%s:  Expected nbytes = %lu, got %lu",
23080a55fbb7Slm66018 				    ioctl[i].operation_name, ioctl[i].nbytes,
23090a55fbb7Slm66018 				    request->nbytes);
23101ae08745Sheppo 				return (EINVAL);
23111ae08745Sheppo 			}
23121ae08745Sheppo 
23131ae08745Sheppo 			break;
23141ae08745Sheppo 		}
23151ae08745Sheppo 	}
23161ae08745Sheppo 	ASSERT(i < nioctls);	/* because "operation" already validated */
23171ae08745Sheppo 
2318047ba61eSachartre 	if (!(vd->open_flags & FWRITE) && ioctl[i].write) {
2319047ba61eSachartre 		PR0("%s fails because backend is opened read-only",
2320047ba61eSachartre 		    ioctl[i].operation_name);
2321047ba61eSachartre 		request->status = EROFS;
2322047ba61eSachartre 		return (0);
2323047ba61eSachartre 	}
2324047ba61eSachartre 
23251ae08745Sheppo 	if (request->nbytes)
23261ae08745Sheppo 		buf = kmem_zalloc(request->nbytes, KM_SLEEP);
23271ae08745Sheppo 	status = vd_do_ioctl(vd, request, buf, &ioctl[i]);
23281ae08745Sheppo 	if (request->nbytes)
23291ae08745Sheppo 		kmem_free(buf, request->nbytes);
233087a7269eSachartre 
23311ae08745Sheppo 	return (status);
23321ae08745Sheppo }
23331ae08745Sheppo 
23344bac2208Snarayan static int
23354bac2208Snarayan vd_get_devid(vd_task_t *task)
23364bac2208Snarayan {
23374bac2208Snarayan 	vd_t *vd = task->vd;
23384bac2208Snarayan 	vd_dring_payload_t *request = task->request;
23394bac2208Snarayan 	vd_devid_t *vd_devid;
23404bac2208Snarayan 	impl_devid_t *devid;
234187a7269eSachartre 	int status, bufid_len, devid_len, len, sz;
23423af08d82Slm66018 	int bufbytes;
23434bac2208Snarayan 
23443af08d82Slm66018 	PR1("Get Device ID, nbytes=%ld", request->nbytes);
23454bac2208Snarayan 
23463c96341aSnarayan 	if (vd->file) {
234787a7269eSachartre 		if (vd->file_devid == NULL) {
23483af08d82Slm66018 			PR2("No Device ID");
2349205eeb1aSlm66018 			request->status = ENOENT;
2350205eeb1aSlm66018 			return (0);
235187a7269eSachartre 		} else {
235287a7269eSachartre 			sz = ddi_devid_sizeof(vd->file_devid);
235387a7269eSachartre 			devid = kmem_alloc(sz, KM_SLEEP);
235487a7269eSachartre 			bcopy(vd->file_devid, devid, sz);
235587a7269eSachartre 		}
235687a7269eSachartre 	} else {
235787a7269eSachartre 		if (ddi_lyr_get_devid(vd->dev[request->slice],
235887a7269eSachartre 		    (ddi_devid_t *)&devid) != DDI_SUCCESS) {
235987a7269eSachartre 			PR2("No Device ID");
2360205eeb1aSlm66018 			request->status = ENOENT;
2361205eeb1aSlm66018 			return (0);
236287a7269eSachartre 		}
23634bac2208Snarayan 	}
23644bac2208Snarayan 
23654bac2208Snarayan 	bufid_len = request->nbytes - sizeof (vd_devid_t) + 1;
23664bac2208Snarayan 	devid_len = DEVID_GETLEN(devid);
23674bac2208Snarayan 
23683af08d82Slm66018 	/*
23693af08d82Slm66018 	 * Save the buffer size here for use in deallocation.
23703af08d82Slm66018 	 * The actual number of bytes copied is returned in
23713af08d82Slm66018 	 * the 'nbytes' field of the request structure.
23723af08d82Slm66018 	 */
23733af08d82Slm66018 	bufbytes = request->nbytes;
23743af08d82Slm66018 
23753af08d82Slm66018 	vd_devid = kmem_zalloc(bufbytes, KM_SLEEP);
23764bac2208Snarayan 	vd_devid->length = devid_len;
23774bac2208Snarayan 	vd_devid->type = DEVID_GETTYPE(devid);
23784bac2208Snarayan 
23794bac2208Snarayan 	len = (devid_len > bufid_len)? bufid_len : devid_len;
23804bac2208Snarayan 
23814bac2208Snarayan 	bcopy(devid->did_id, vd_devid->id, len);
23824bac2208Snarayan 
238378fcd0a1Sachartre 	request->status = 0;
238478fcd0a1Sachartre 
23854bac2208Snarayan 	/* LDC memory operations require 8-byte multiples */
23864bac2208Snarayan 	ASSERT(request->nbytes % sizeof (uint64_t) == 0);
23874bac2208Snarayan 
23884bac2208Snarayan 	if ((status = ldc_mem_copy(vd->ldc_handle, (caddr_t)vd_devid, 0,
23894bac2208Snarayan 	    &request->nbytes, request->cookie, request->ncookies,
23904bac2208Snarayan 	    LDC_COPY_OUT)) != 0) {
23913af08d82Slm66018 		PR0("ldc_mem_copy() returned errno %d copying to client",
23924bac2208Snarayan 		    status);
23934bac2208Snarayan 	}
23943af08d82Slm66018 	PR1("post mem_copy: nbytes=%ld", request->nbytes);
23954bac2208Snarayan 
23963af08d82Slm66018 	kmem_free(vd_devid, bufbytes);
23974bac2208Snarayan 	ddi_devid_free((ddi_devid_t)devid);
23984bac2208Snarayan 
23994bac2208Snarayan 	return (status);
24004bac2208Snarayan }
24014bac2208Snarayan 
24021ae08745Sheppo /*
24031ae08745Sheppo  * Define the supported operations once the functions for performing them have
24041ae08745Sheppo  * been defined
24051ae08745Sheppo  */
24061ae08745Sheppo static const vds_operation_t	vds_operation[] = {
24073af08d82Slm66018 #define	X(_s)	#_s, _s
24083af08d82Slm66018 	{X(VD_OP_BREAD),	vd_start_bio,	vd_complete_bio},
24093af08d82Slm66018 	{X(VD_OP_BWRITE),	vd_start_bio,	vd_complete_bio},
24103af08d82Slm66018 	{X(VD_OP_FLUSH),	vd_ioctl,	NULL},
24113af08d82Slm66018 	{X(VD_OP_GET_WCE),	vd_ioctl,	NULL},
24123af08d82Slm66018 	{X(VD_OP_SET_WCE),	vd_ioctl,	NULL},
24133af08d82Slm66018 	{X(VD_OP_GET_VTOC),	vd_ioctl,	NULL},
24143af08d82Slm66018 	{X(VD_OP_SET_VTOC),	vd_ioctl,	NULL},
24153af08d82Slm66018 	{X(VD_OP_GET_DISKGEOM),	vd_ioctl,	NULL},
24163af08d82Slm66018 	{X(VD_OP_SET_DISKGEOM),	vd_ioctl,	NULL},
24173af08d82Slm66018 	{X(VD_OP_GET_EFI),	vd_ioctl,	NULL},
24183af08d82Slm66018 	{X(VD_OP_SET_EFI),	vd_ioctl,	NULL},
24193af08d82Slm66018 	{X(VD_OP_GET_DEVID),	vd_get_devid,	NULL},
24203af08d82Slm66018 #undef	X
24211ae08745Sheppo };
24221ae08745Sheppo 
24231ae08745Sheppo static const size_t	vds_noperations =
24241ae08745Sheppo 	(sizeof (vds_operation))/(sizeof (vds_operation[0]));
24251ae08745Sheppo 
24261ae08745Sheppo /*
2427d10e4ef2Snarayan  * Process a task specifying a client I/O request
2428205eeb1aSlm66018  *
2429205eeb1aSlm66018  * Parameters:
2430205eeb1aSlm66018  *	task 		- structure containing the request sent from client
2431205eeb1aSlm66018  *
2432205eeb1aSlm66018  * Return Value
2433205eeb1aSlm66018  *	0	- success
2434205eeb1aSlm66018  *	ENOTSUP	- Unknown/Unsupported VD_OP_XXX operation
2435205eeb1aSlm66018  *	EINVAL	- Invalid disk slice
2436205eeb1aSlm66018  *	!= 0	- some other non-zero return value from start function
24371ae08745Sheppo  */
24381ae08745Sheppo static int
2439205eeb1aSlm66018 vd_do_process_task(vd_task_t *task)
24401ae08745Sheppo {
2441205eeb1aSlm66018 	int			i;
2442d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
2443d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
24441ae08745Sheppo 
2445d10e4ef2Snarayan 	ASSERT(vd != NULL);
2446d10e4ef2Snarayan 	ASSERT(request != NULL);
24471ae08745Sheppo 
2448d10e4ef2Snarayan 	/* Find the requested operation */
2449205eeb1aSlm66018 	for (i = 0; i < vds_noperations; i++) {
2450205eeb1aSlm66018 		if (request->operation == vds_operation[i].operation) {
2451205eeb1aSlm66018 			/* all operations should have a start func */
2452205eeb1aSlm66018 			ASSERT(vds_operation[i].start != NULL);
2453205eeb1aSlm66018 
2454205eeb1aSlm66018 			task->completef = vds_operation[i].complete;
2455d10e4ef2Snarayan 			break;
2456205eeb1aSlm66018 		}
2457205eeb1aSlm66018 	}
2458*17cadca8Slm66018 
2459*17cadca8Slm66018 	/*
2460*17cadca8Slm66018 	 * We need to check that the requested operation is permitted
2461*17cadca8Slm66018 	 * for the particular client that sent it or that the loop above
2462*17cadca8Slm66018 	 * did not complete without finding the operation type (indicating
2463*17cadca8Slm66018 	 * that the requested operation is unknown/unimplemented)
2464*17cadca8Slm66018 	 */
2465*17cadca8Slm66018 	if ((VD_OP_SUPPORTED(vd->operations, request->operation) == B_FALSE) ||
2466*17cadca8Slm66018 	    (i == vds_noperations)) {
24673af08d82Slm66018 		PR0("Unsupported operation %u", request->operation);
2468*17cadca8Slm66018 		request->status = ENOTSUP;
2469*17cadca8Slm66018 		return (0);
24701ae08745Sheppo 	}
24711ae08745Sheppo 
24727636cb21Slm66018 	/* Range-check slice */
247387a7269eSachartre 	if (request->slice >= vd->nslices &&
247487a7269eSachartre 	    (vd->vdisk_type != VD_DISK_TYPE_DISK ||
247587a7269eSachartre 	    request->slice != VD_SLICE_NONE)) {
24763af08d82Slm66018 		PR0("Invalid \"slice\" %u (max %u) for virtual disk",
24777636cb21Slm66018 		    request->slice, (vd->nslices - 1));
24787636cb21Slm66018 		return (EINVAL);
24797636cb21Slm66018 	}
24807636cb21Slm66018 
2481205eeb1aSlm66018 	/*
2482205eeb1aSlm66018 	 * Call the function pointer that starts the operation.
2483205eeb1aSlm66018 	 */
2484205eeb1aSlm66018 	return (vds_operation[i].start(task));
24851ae08745Sheppo }
24861ae08745Sheppo 
2487205eeb1aSlm66018 /*
2488205eeb1aSlm66018  * Description:
2489205eeb1aSlm66018  *	This function is called by both the in-band and descriptor ring
2490205eeb1aSlm66018  *	message processing functions paths to actually execute the task
2491205eeb1aSlm66018  *	requested by the vDisk client. It in turn calls its worker
2492205eeb1aSlm66018  *	function, vd_do_process_task(), to carry our the request.
2493205eeb1aSlm66018  *
2494205eeb1aSlm66018  *	Any transport errors (e.g. LDC errors, vDisk protocol errors) are
2495205eeb1aSlm66018  *	saved in the 'status' field of the task and are propagated back
2496205eeb1aSlm66018  *	up the call stack to trigger a NACK
2497205eeb1aSlm66018  *
2498205eeb1aSlm66018  *	Any request errors (e.g. ENOTTY from an ioctl) are saved in
2499205eeb1aSlm66018  *	the 'status' field of the request and result in an ACK being sent
2500205eeb1aSlm66018  *	by the completion handler.
2501205eeb1aSlm66018  *
2502205eeb1aSlm66018  * Parameters:
2503205eeb1aSlm66018  *	task 		- structure containing the request sent from client
2504205eeb1aSlm66018  *
2505205eeb1aSlm66018  * Return Value
2506205eeb1aSlm66018  *	0		- successful synchronous request.
2507205eeb1aSlm66018  *	!= 0		- transport error (e.g. LDC errors, vDisk protocol)
2508205eeb1aSlm66018  *	EINPROGRESS	- task will be finished in a completion handler
2509205eeb1aSlm66018  */
2510205eeb1aSlm66018 static int
2511205eeb1aSlm66018 vd_process_task(vd_task_t *task)
2512205eeb1aSlm66018 {
2513205eeb1aSlm66018 	vd_t	*vd = task->vd;
2514205eeb1aSlm66018 	int	status;
25151ae08745Sheppo 
2516205eeb1aSlm66018 	DTRACE_PROBE1(task__start, vd_task_t *, task);
25173af08d82Slm66018 
2518205eeb1aSlm66018 	task->status =  vd_do_process_task(task);
2519205eeb1aSlm66018 
2520205eeb1aSlm66018 	/*
2521205eeb1aSlm66018 	 * If the task processing function returned EINPROGRESS indicating
2522205eeb1aSlm66018 	 * that the task needs completing then schedule a taskq entry to
2523205eeb1aSlm66018 	 * finish it now.
2524205eeb1aSlm66018 	 *
2525205eeb1aSlm66018 	 * Otherwise the task processing function returned either zero
2526205eeb1aSlm66018 	 * indicating that the task was finished in the start function (and we
2527205eeb1aSlm66018 	 * don't need to wait in a completion function) or the start function
2528205eeb1aSlm66018 	 * returned an error - in both cases all that needs to happen is the
2529205eeb1aSlm66018 	 * notification to the vDisk client higher up the call stack.
2530205eeb1aSlm66018 	 * If the task was using a Descriptor Ring, we need to mark it as done
2531205eeb1aSlm66018 	 * at this stage.
2532205eeb1aSlm66018 	 */
2533205eeb1aSlm66018 	if (task->status == EINPROGRESS) {
2534d10e4ef2Snarayan 		/* Queue a task to complete the operation */
2535205eeb1aSlm66018 		(void) ddi_taskq_dispatch(vd->completionq, vd_complete,
2536d10e4ef2Snarayan 		    task, DDI_SLEEP);
2537d10e4ef2Snarayan 
2538205eeb1aSlm66018 	} else if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE)) {
2539205eeb1aSlm66018 		/* Update the dring element if it's a dring client */
2540205eeb1aSlm66018 		status = vd_mark_elem_done(vd, task->index,
2541205eeb1aSlm66018 		    task->request->status, task->request->nbytes);
2542205eeb1aSlm66018 		if (status == ECONNRESET)
2543205eeb1aSlm66018 			vd_mark_in_reset(vd);
2544205eeb1aSlm66018 	}
2545205eeb1aSlm66018 
2546205eeb1aSlm66018 	return (task->status);
25471ae08745Sheppo }
25481ae08745Sheppo 
25491ae08745Sheppo /*
25500a55fbb7Slm66018  * Return true if the "type", "subtype", and "env" fields of the "tag" first
25510a55fbb7Slm66018  * argument match the corresponding remaining arguments; otherwise, return false
25521ae08745Sheppo  */
25530a55fbb7Slm66018 boolean_t
25541ae08745Sheppo vd_msgtype(vio_msg_tag_t *tag, int type, int subtype, int env)
25551ae08745Sheppo {
25561ae08745Sheppo 	return ((tag->vio_msgtype == type) &&
25571ae08745Sheppo 	    (tag->vio_subtype == subtype) &&
25580a55fbb7Slm66018 	    (tag->vio_subtype_env == env)) ? B_TRUE : B_FALSE;
25591ae08745Sheppo }
25601ae08745Sheppo 
25610a55fbb7Slm66018 /*
25620a55fbb7Slm66018  * Check whether the major/minor version specified in "ver_msg" is supported
25630a55fbb7Slm66018  * by this server.
25640a55fbb7Slm66018  */
25650a55fbb7Slm66018 static boolean_t
25660a55fbb7Slm66018 vds_supported_version(vio_ver_msg_t *ver_msg)
25670a55fbb7Slm66018 {
25680a55fbb7Slm66018 	for (int i = 0; i < vds_num_versions; i++) {
25690a55fbb7Slm66018 		ASSERT(vds_version[i].major > 0);
25700a55fbb7Slm66018 		ASSERT((i == 0) ||
25710a55fbb7Slm66018 		    (vds_version[i].major < vds_version[i-1].major));
25720a55fbb7Slm66018 
25730a55fbb7Slm66018 		/*
25740a55fbb7Slm66018 		 * If the major versions match, adjust the minor version, if
25750a55fbb7Slm66018 		 * necessary, down to the highest value supported by this
25760a55fbb7Slm66018 		 * server and return true so this message will get "ack"ed;
25770a55fbb7Slm66018 		 * the client should also support all minor versions lower
25780a55fbb7Slm66018 		 * than the value it sent
25790a55fbb7Slm66018 		 */
25800a55fbb7Slm66018 		if (ver_msg->ver_major == vds_version[i].major) {
25810a55fbb7Slm66018 			if (ver_msg->ver_minor > vds_version[i].minor) {
25820a55fbb7Slm66018 				PR0("Adjusting minor version from %u to %u",
25830a55fbb7Slm66018 				    ver_msg->ver_minor, vds_version[i].minor);
25840a55fbb7Slm66018 				ver_msg->ver_minor = vds_version[i].minor;
25850a55fbb7Slm66018 			}
25860a55fbb7Slm66018 			return (B_TRUE);
25870a55fbb7Slm66018 		}
25880a55fbb7Slm66018 
25890a55fbb7Slm66018 		/*
25900a55fbb7Slm66018 		 * If the message contains a higher major version number, set
25910a55fbb7Slm66018 		 * the message's major/minor versions to the current values
25920a55fbb7Slm66018 		 * and return false, so this message will get "nack"ed with
25930a55fbb7Slm66018 		 * these values, and the client will potentially try again
25940a55fbb7Slm66018 		 * with the same or a lower version
25950a55fbb7Slm66018 		 */
25960a55fbb7Slm66018 		if (ver_msg->ver_major > vds_version[i].major) {
25970a55fbb7Slm66018 			ver_msg->ver_major = vds_version[i].major;
25980a55fbb7Slm66018 			ver_msg->ver_minor = vds_version[i].minor;
25990a55fbb7Slm66018 			return (B_FALSE);
26000a55fbb7Slm66018 		}
26010a55fbb7Slm66018 
26020a55fbb7Slm66018 		/*
26030a55fbb7Slm66018 		 * Otherwise, the message's major version is less than the
26040a55fbb7Slm66018 		 * current major version, so continue the loop to the next
26050a55fbb7Slm66018 		 * (lower) supported version
26060a55fbb7Slm66018 		 */
26070a55fbb7Slm66018 	}
26080a55fbb7Slm66018 
26090a55fbb7Slm66018 	/*
26100a55fbb7Slm66018 	 * No common version was found; "ground" the version pair in the
26110a55fbb7Slm66018 	 * message to terminate negotiation
26120a55fbb7Slm66018 	 */
26130a55fbb7Slm66018 	ver_msg->ver_major = 0;
26140a55fbb7Slm66018 	ver_msg->ver_minor = 0;
26150a55fbb7Slm66018 	return (B_FALSE);
26160a55fbb7Slm66018 }
26170a55fbb7Slm66018 
26180a55fbb7Slm66018 /*
26190a55fbb7Slm66018  * Process a version message from a client.  vds expects to receive version
26200a55fbb7Slm66018  * messages from clients seeking service, but never issues version messages
26210a55fbb7Slm66018  * itself; therefore, vds can ACK or NACK client version messages, but does
26220a55fbb7Slm66018  * not expect to receive version-message ACKs or NACKs (and will treat such
26230a55fbb7Slm66018  * messages as invalid).
26240a55fbb7Slm66018  */
26251ae08745Sheppo static int
26260a55fbb7Slm66018 vd_process_ver_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
26271ae08745Sheppo {
26281ae08745Sheppo 	vio_ver_msg_t	*ver_msg = (vio_ver_msg_t *)msg;
26291ae08745Sheppo 
26301ae08745Sheppo 
26311ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
26321ae08745Sheppo 
26331ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
26341ae08745Sheppo 	    VIO_VER_INFO)) {
26351ae08745Sheppo 		return (ENOMSG);	/* not a version message */
26361ae08745Sheppo 	}
26371ae08745Sheppo 
26381ae08745Sheppo 	if (msglen != sizeof (*ver_msg)) {
26393af08d82Slm66018 		PR0("Expected %lu-byte version message; "
26401ae08745Sheppo 		    "received %lu bytes", sizeof (*ver_msg), msglen);
26411ae08745Sheppo 		return (EBADMSG);
26421ae08745Sheppo 	}
26431ae08745Sheppo 
26441ae08745Sheppo 	if (ver_msg->dev_class != VDEV_DISK) {
26453af08d82Slm66018 		PR0("Expected device class %u (disk); received %u",
26461ae08745Sheppo 		    VDEV_DISK, ver_msg->dev_class);
26471ae08745Sheppo 		return (EBADMSG);
26481ae08745Sheppo 	}
26491ae08745Sheppo 
26500a55fbb7Slm66018 	/*
26510a55fbb7Slm66018 	 * We're talking to the expected kind of client; set our device class
26520a55fbb7Slm66018 	 * for "ack/nack" back to the client
26530a55fbb7Slm66018 	 */
26541ae08745Sheppo 	ver_msg->dev_class = VDEV_DISK_SERVER;
26550a55fbb7Slm66018 
26560a55fbb7Slm66018 	/*
26570a55fbb7Slm66018 	 * Check whether the (valid) version message specifies a version
26580a55fbb7Slm66018 	 * supported by this server.  If the version is not supported, return
26590a55fbb7Slm66018 	 * EBADMSG so the message will get "nack"ed; vds_supported_version()
26600a55fbb7Slm66018 	 * will have updated the message with a supported version for the
26610a55fbb7Slm66018 	 * client to consider
26620a55fbb7Slm66018 	 */
26630a55fbb7Slm66018 	if (!vds_supported_version(ver_msg))
26640a55fbb7Slm66018 		return (EBADMSG);
26650a55fbb7Slm66018 
26660a55fbb7Slm66018 
26670a55fbb7Slm66018 	/*
26680a55fbb7Slm66018 	 * A version has been agreed upon; use the client's SID for
26690a55fbb7Slm66018 	 * communication on this channel now
26700a55fbb7Slm66018 	 */
26710a55fbb7Slm66018 	ASSERT(!(vd->initialized & VD_SID));
26720a55fbb7Slm66018 	vd->sid = ver_msg->tag.vio_sid;
26730a55fbb7Slm66018 	vd->initialized |= VD_SID;
26740a55fbb7Slm66018 
26750a55fbb7Slm66018 	/*
2676*17cadca8Slm66018 	 * Store the negotiated major and minor version values in the "vd" data
2677*17cadca8Slm66018 	 * structure so that we can check if certain operations are supported
2678*17cadca8Slm66018 	 * by the client.
26790a55fbb7Slm66018 	 */
2680*17cadca8Slm66018 	vd->version.major = ver_msg->ver_major;
2681*17cadca8Slm66018 	vd->version.minor = ver_msg->ver_minor;
26820a55fbb7Slm66018 
26830a55fbb7Slm66018 	PR0("Using major version %u, minor version %u",
26840a55fbb7Slm66018 	    ver_msg->ver_major, ver_msg->ver_minor);
26851ae08745Sheppo 	return (0);
26861ae08745Sheppo }
26871ae08745Sheppo 
2688*17cadca8Slm66018 static void
2689*17cadca8Slm66018 vd_set_exported_operations(vd_t *vd)
2690*17cadca8Slm66018 {
2691*17cadca8Slm66018 	vd->operations = 0;	/* clear field */
2692*17cadca8Slm66018 
2693*17cadca8Slm66018 	/*
2694*17cadca8Slm66018 	 * We need to check from the highest version supported to the
2695*17cadca8Slm66018 	 * lowest because versions with a higher minor number implicitly
2696*17cadca8Slm66018 	 * support versions with a lower minor number.
2697*17cadca8Slm66018 	 */
2698*17cadca8Slm66018 	if (vio_ver_is_supported(vd->version, 1, 1)) {
2699*17cadca8Slm66018 		ASSERT(vd->open_flags & FREAD);
2700*17cadca8Slm66018 		vd->operations |= VD_OP_MASK_READ;
2701*17cadca8Slm66018 
2702*17cadca8Slm66018 		if (vd->open_flags & FWRITE)
2703*17cadca8Slm66018 			vd->operations |= VD_OP_MASK_WRITE;
2704*17cadca8Slm66018 
2705*17cadca8Slm66018 		if (vd->file && vd_file_is_iso_image(vd)) {
2706*17cadca8Slm66018 			/*
2707*17cadca8Slm66018 			 * can't write to ISO images, make sure that write
2708*17cadca8Slm66018 			 * support is not set in case administrator did not
2709*17cadca8Slm66018 			 * use "options=ro" when doing an ldm add-vdsdev
2710*17cadca8Slm66018 			 */
2711*17cadca8Slm66018 			vd->operations &= ~VD_OP_MASK_WRITE;
2712*17cadca8Slm66018 		}
2713*17cadca8Slm66018 	} else if (vio_ver_is_supported(vd->version, 1, 0)) {
2714*17cadca8Slm66018 		vd->operations = VD_OP_MASK_READ | VD_OP_MASK_WRITE;
2715*17cadca8Slm66018 	}
2716*17cadca8Slm66018 
2717*17cadca8Slm66018 	/* we should have already agreed on a version */
2718*17cadca8Slm66018 	ASSERT(vd->operations != 0);
2719*17cadca8Slm66018 }
2720*17cadca8Slm66018 
27211ae08745Sheppo static int
27221ae08745Sheppo vd_process_attr_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
27231ae08745Sheppo {
27241ae08745Sheppo 	vd_attr_msg_t	*attr_msg = (vd_attr_msg_t *)msg;
27253c96341aSnarayan 	int		status, retry = 0;
27261ae08745Sheppo 
27271ae08745Sheppo 
27281ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
27291ae08745Sheppo 
27301ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
27311ae08745Sheppo 	    VIO_ATTR_INFO)) {
2732d10e4ef2Snarayan 		PR0("Message is not an attribute message");
2733d10e4ef2Snarayan 		return (ENOMSG);
27341ae08745Sheppo 	}
27351ae08745Sheppo 
27361ae08745Sheppo 	if (msglen != sizeof (*attr_msg)) {
27373af08d82Slm66018 		PR0("Expected %lu-byte attribute message; "
27381ae08745Sheppo 		    "received %lu bytes", sizeof (*attr_msg), msglen);
27391ae08745Sheppo 		return (EBADMSG);
27401ae08745Sheppo 	}
27411ae08745Sheppo 
27421ae08745Sheppo 	if (attr_msg->max_xfer_sz == 0) {
27433af08d82Slm66018 		PR0("Received maximum transfer size of 0 from client");
27441ae08745Sheppo 		return (EBADMSG);
27451ae08745Sheppo 	}
27461ae08745Sheppo 
27471ae08745Sheppo 	if ((attr_msg->xfer_mode != VIO_DESC_MODE) &&
27481ae08745Sheppo 	    (attr_msg->xfer_mode != VIO_DRING_MODE)) {
27493af08d82Slm66018 		PR0("Client requested unsupported transfer mode");
27501ae08745Sheppo 		return (EBADMSG);
27511ae08745Sheppo 	}
27521ae08745Sheppo 
27533c96341aSnarayan 	/*
27543c96341aSnarayan 	 * check if the underlying disk is ready, if not try accessing
27553c96341aSnarayan 	 * the device again. Open the vdisk device and extract info
27563c96341aSnarayan 	 * about it, as this is needed to respond to the attr info msg
27573c96341aSnarayan 	 */
27583c96341aSnarayan 	if ((vd->initialized & VD_DISK_READY) == 0) {
27593c96341aSnarayan 		PR0("Retry setting up disk (%s)", vd->device_path);
27603c96341aSnarayan 		do {
27613c96341aSnarayan 			status = vd_setup_vd(vd);
27623c96341aSnarayan 			if (status != EAGAIN || ++retry > vds_dev_retries)
27633c96341aSnarayan 				break;
27643c96341aSnarayan 
27653c96341aSnarayan 			/* incremental delay */
27663c96341aSnarayan 			delay(drv_usectohz(vds_dev_delay));
27673c96341aSnarayan 
27683c96341aSnarayan 			/* if vdisk is no longer enabled - return error */
27693c96341aSnarayan 			if (!vd_enabled(vd))
27703c96341aSnarayan 				return (ENXIO);
27713c96341aSnarayan 
27723c96341aSnarayan 		} while (status == EAGAIN);
27733c96341aSnarayan 
27743c96341aSnarayan 		if (status)
27753c96341aSnarayan 			return (ENXIO);
27763c96341aSnarayan 
27773c96341aSnarayan 		vd->initialized |= VD_DISK_READY;
27783c96341aSnarayan 		ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
27793c96341aSnarayan 		PR0("vdisk_type = %s, pseudo = %s, file = %s, nslices = %u",
27803c96341aSnarayan 		    ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
27813c96341aSnarayan 		    (vd->pseudo ? "yes" : "no"),
27823c96341aSnarayan 		    (vd->file ? "yes" : "no"),
27833c96341aSnarayan 		    vd->nslices);
27843c96341aSnarayan 	}
27853c96341aSnarayan 
27861ae08745Sheppo 	/* Success:  valid message and transfer mode */
27871ae08745Sheppo 	vd->xfer_mode = attr_msg->xfer_mode;
27883af08d82Slm66018 
27891ae08745Sheppo 	if (vd->xfer_mode == VIO_DESC_MODE) {
27903af08d82Slm66018 
27911ae08745Sheppo 		/*
27921ae08745Sheppo 		 * The vd_dring_inband_msg_t contains one cookie; need room
27931ae08745Sheppo 		 * for up to n-1 more cookies, where "n" is the number of full
27941ae08745Sheppo 		 * pages plus possibly one partial page required to cover
27951ae08745Sheppo 		 * "max_xfer_sz".  Add room for one more cookie if
27961ae08745Sheppo 		 * "max_xfer_sz" isn't an integral multiple of the page size.
27971ae08745Sheppo 		 * Must first get the maximum transfer size in bytes.
27981ae08745Sheppo 		 */
27991ae08745Sheppo 		size_t	max_xfer_bytes = attr_msg->vdisk_block_size ?
28001ae08745Sheppo 		    attr_msg->vdisk_block_size*attr_msg->max_xfer_sz :
28011ae08745Sheppo 		    attr_msg->max_xfer_sz;
28021ae08745Sheppo 		size_t	max_inband_msglen =
28031ae08745Sheppo 		    sizeof (vd_dring_inband_msg_t) +
28041ae08745Sheppo 		    ((max_xfer_bytes/PAGESIZE +
28051ae08745Sheppo 		    ((max_xfer_bytes % PAGESIZE) ? 1 : 0))*
28061ae08745Sheppo 		    (sizeof (ldc_mem_cookie_t)));
28071ae08745Sheppo 
28081ae08745Sheppo 		/*
28091ae08745Sheppo 		 * Set the maximum expected message length to
28101ae08745Sheppo 		 * accommodate in-band-descriptor messages with all
28111ae08745Sheppo 		 * their cookies
28121ae08745Sheppo 		 */
28131ae08745Sheppo 		vd->max_msglen = MAX(vd->max_msglen, max_inband_msglen);
2814d10e4ef2Snarayan 
2815d10e4ef2Snarayan 		/*
2816d10e4ef2Snarayan 		 * Initialize the data structure for processing in-band I/O
2817d10e4ef2Snarayan 		 * request descriptors
2818d10e4ef2Snarayan 		 */
2819d10e4ef2Snarayan 		vd->inband_task.vd	= vd;
28203af08d82Slm66018 		vd->inband_task.msg	= kmem_alloc(vd->max_msglen, KM_SLEEP);
2821d10e4ef2Snarayan 		vd->inband_task.index	= 0;
2822d10e4ef2Snarayan 		vd->inband_task.type	= VD_FINAL_RANGE_TASK;	/* range == 1 */
28231ae08745Sheppo 	}
28241ae08745Sheppo 
2825e1ebb9ecSlm66018 	/* Return the device's block size and max transfer size to the client */
2826e1ebb9ecSlm66018 	attr_msg->vdisk_block_size	= DEV_BSIZE;
2827*17cadca8Slm66018 	attr_msg->vdisk_block_size	= vd->block_size;
2828e1ebb9ecSlm66018 	attr_msg->max_xfer_sz		= vd->max_xfer_sz;
2829e1ebb9ecSlm66018 
28301ae08745Sheppo 	attr_msg->vdisk_size = vd->vdisk_size;
28311ae08745Sheppo 	attr_msg->vdisk_type = vd->vdisk_type;
2832*17cadca8Slm66018 	attr_msg->vdisk_media = vd->vdisk_media;
2833*17cadca8Slm66018 
2834*17cadca8Slm66018 	/* Discover and save the list of supported VD_OP_XXX operations */
2835*17cadca8Slm66018 	vd_set_exported_operations(vd);
2836*17cadca8Slm66018 	attr_msg->operations = vd->operations;
2837*17cadca8Slm66018 
28381ae08745Sheppo 	PR0("%s", VD_CLIENT(vd));
28393af08d82Slm66018 
28403af08d82Slm66018 	ASSERT(vd->dring_task == NULL);
28413af08d82Slm66018 
28421ae08745Sheppo 	return (0);
28431ae08745Sheppo }
28441ae08745Sheppo 
28451ae08745Sheppo static int
28461ae08745Sheppo vd_process_dring_reg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
28471ae08745Sheppo {
28481ae08745Sheppo 	int			status;
28491ae08745Sheppo 	size_t			expected;
28501ae08745Sheppo 	ldc_mem_info_t		dring_minfo;
28511ae08745Sheppo 	vio_dring_reg_msg_t	*reg_msg = (vio_dring_reg_msg_t *)msg;
28521ae08745Sheppo 
28531ae08745Sheppo 
28541ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
28551ae08745Sheppo 
28561ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
28571ae08745Sheppo 	    VIO_DRING_REG)) {
2858d10e4ef2Snarayan 		PR0("Message is not a register-dring message");
2859d10e4ef2Snarayan 		return (ENOMSG);
28601ae08745Sheppo 	}
28611ae08745Sheppo 
28621ae08745Sheppo 	if (msglen < sizeof (*reg_msg)) {
28633af08d82Slm66018 		PR0("Expected at least %lu-byte register-dring message; "
28641ae08745Sheppo 		    "received %lu bytes", sizeof (*reg_msg), msglen);
28651ae08745Sheppo 		return (EBADMSG);
28661ae08745Sheppo 	}
28671ae08745Sheppo 
28681ae08745Sheppo 	expected = sizeof (*reg_msg) +
28691ae08745Sheppo 	    (reg_msg->ncookies - 1)*(sizeof (reg_msg->cookie[0]));
28701ae08745Sheppo 	if (msglen != expected) {
28713af08d82Slm66018 		PR0("Expected %lu-byte register-dring message; "
28721ae08745Sheppo 		    "received %lu bytes", expected, msglen);
28731ae08745Sheppo 		return (EBADMSG);
28741ae08745Sheppo 	}
28751ae08745Sheppo 
28761ae08745Sheppo 	if (vd->initialized & VD_DRING) {
28773af08d82Slm66018 		PR0("A dring was previously registered; only support one");
28781ae08745Sheppo 		return (EBADMSG);
28791ae08745Sheppo 	}
28801ae08745Sheppo 
2881d10e4ef2Snarayan 	if (reg_msg->num_descriptors > INT32_MAX) {
28823af08d82Slm66018 		PR0("reg_msg->num_descriptors = %u; must be <= %u (%s)",
2883d10e4ef2Snarayan 		    reg_msg->ncookies, INT32_MAX, STRINGIZE(INT32_MAX));
2884d10e4ef2Snarayan 		return (EBADMSG);
2885d10e4ef2Snarayan 	}
2886d10e4ef2Snarayan 
28871ae08745Sheppo 	if (reg_msg->ncookies != 1) {
28881ae08745Sheppo 		/*
28891ae08745Sheppo 		 * In addition to fixing the assertion in the success case
28901ae08745Sheppo 		 * below, supporting drings which require more than one
28911ae08745Sheppo 		 * "cookie" requires increasing the value of vd->max_msglen
28921ae08745Sheppo 		 * somewhere in the code path prior to receiving the message
28931ae08745Sheppo 		 * which results in calling this function.  Note that without
28941ae08745Sheppo 		 * making this change, the larger message size required to
28951ae08745Sheppo 		 * accommodate multiple cookies cannot be successfully
28961ae08745Sheppo 		 * received, so this function will not even get called.
28971ae08745Sheppo 		 * Gracefully accommodating more dring cookies might
28981ae08745Sheppo 		 * reasonably demand exchanging an additional attribute or
28991ae08745Sheppo 		 * making a minor protocol adjustment
29001ae08745Sheppo 		 */
29013af08d82Slm66018 		PR0("reg_msg->ncookies = %u != 1", reg_msg->ncookies);
29021ae08745Sheppo 		return (EBADMSG);
29031ae08745Sheppo 	}
29041ae08745Sheppo 
29051ae08745Sheppo 	status = ldc_mem_dring_map(vd->ldc_handle, reg_msg->cookie,
29061ae08745Sheppo 	    reg_msg->ncookies, reg_msg->num_descriptors,
29074bac2208Snarayan 	    reg_msg->descriptor_size, LDC_DIRECT_MAP, &vd->dring_handle);
29081ae08745Sheppo 	if (status != 0) {
29093af08d82Slm66018 		PR0("ldc_mem_dring_map() returned errno %d", status);
29101ae08745Sheppo 		return (status);
29111ae08745Sheppo 	}
29121ae08745Sheppo 
29131ae08745Sheppo 	/*
29141ae08745Sheppo 	 * To remove the need for this assertion, must call
29151ae08745Sheppo 	 * ldc_mem_dring_nextcookie() successfully ncookies-1 times after a
29161ae08745Sheppo 	 * successful call to ldc_mem_dring_map()
29171ae08745Sheppo 	 */
29181ae08745Sheppo 	ASSERT(reg_msg->ncookies == 1);
29191ae08745Sheppo 
29201ae08745Sheppo 	if ((status =
29211ae08745Sheppo 	    ldc_mem_dring_info(vd->dring_handle, &dring_minfo)) != 0) {
29223af08d82Slm66018 		PR0("ldc_mem_dring_info() returned errno %d", status);
29231ae08745Sheppo 		if ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0)
29243af08d82Slm66018 			PR0("ldc_mem_dring_unmap() returned errno %d", status);
29251ae08745Sheppo 		return (status);
29261ae08745Sheppo 	}
29271ae08745Sheppo 
29281ae08745Sheppo 	if (dring_minfo.vaddr == NULL) {
29293af08d82Slm66018 		PR0("Descriptor ring virtual address is NULL");
29300a55fbb7Slm66018 		return (ENXIO);
29311ae08745Sheppo 	}
29321ae08745Sheppo 
29331ae08745Sheppo 
2934d10e4ef2Snarayan 	/* Initialize for valid message and mapped dring */
29351ae08745Sheppo 	PR1("descriptor size = %u, dring length = %u",
29361ae08745Sheppo 	    vd->descriptor_size, vd->dring_len);
29371ae08745Sheppo 	vd->initialized |= VD_DRING;
29381ae08745Sheppo 	vd->dring_ident = 1;	/* "There Can Be Only One" */
29391ae08745Sheppo 	vd->dring = dring_minfo.vaddr;
29401ae08745Sheppo 	vd->descriptor_size = reg_msg->descriptor_size;
29411ae08745Sheppo 	vd->dring_len = reg_msg->num_descriptors;
29421ae08745Sheppo 	reg_msg->dring_ident = vd->dring_ident;
2943d10e4ef2Snarayan 
2944d10e4ef2Snarayan 	/*
2945d10e4ef2Snarayan 	 * Allocate and initialize a "shadow" array of data structures for
2946d10e4ef2Snarayan 	 * tasks to process I/O requests in dring elements
2947d10e4ef2Snarayan 	 */
2948d10e4ef2Snarayan 	vd->dring_task =
2949d10e4ef2Snarayan 	    kmem_zalloc((sizeof (*vd->dring_task)) * vd->dring_len, KM_SLEEP);
2950d10e4ef2Snarayan 	for (int i = 0; i < vd->dring_len; i++) {
2951d10e4ef2Snarayan 		vd->dring_task[i].vd		= vd;
2952d10e4ef2Snarayan 		vd->dring_task[i].index		= i;
2953d10e4ef2Snarayan 		vd->dring_task[i].request	= &VD_DRING_ELEM(i)->payload;
29544bac2208Snarayan 
29554bac2208Snarayan 		status = ldc_mem_alloc_handle(vd->ldc_handle,
29564bac2208Snarayan 		    &(vd->dring_task[i].mhdl));
29574bac2208Snarayan 		if (status) {
29583af08d82Slm66018 			PR0("ldc_mem_alloc_handle() returned err %d ", status);
29594bac2208Snarayan 			return (ENXIO);
29604bac2208Snarayan 		}
29613af08d82Slm66018 
29623af08d82Slm66018 		vd->dring_task[i].msg = kmem_alloc(vd->max_msglen, KM_SLEEP);
2963d10e4ef2Snarayan 	}
2964d10e4ef2Snarayan 
29651ae08745Sheppo 	return (0);
29661ae08745Sheppo }
29671ae08745Sheppo 
29681ae08745Sheppo static int
29691ae08745Sheppo vd_process_dring_unreg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
29701ae08745Sheppo {
29711ae08745Sheppo 	vio_dring_unreg_msg_t	*unreg_msg = (vio_dring_unreg_msg_t *)msg;
29721ae08745Sheppo 
29731ae08745Sheppo 
29741ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
29751ae08745Sheppo 
29761ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
29771ae08745Sheppo 	    VIO_DRING_UNREG)) {
2978d10e4ef2Snarayan 		PR0("Message is not an unregister-dring message");
2979d10e4ef2Snarayan 		return (ENOMSG);
29801ae08745Sheppo 	}
29811ae08745Sheppo 
29821ae08745Sheppo 	if (msglen != sizeof (*unreg_msg)) {
29833af08d82Slm66018 		PR0("Expected %lu-byte unregister-dring message; "
29841ae08745Sheppo 		    "received %lu bytes", sizeof (*unreg_msg), msglen);
29851ae08745Sheppo 		return (EBADMSG);
29861ae08745Sheppo 	}
29871ae08745Sheppo 
29881ae08745Sheppo 	if (unreg_msg->dring_ident != vd->dring_ident) {
29893af08d82Slm66018 		PR0("Expected dring ident %lu; received %lu",
29901ae08745Sheppo 		    vd->dring_ident, unreg_msg->dring_ident);
29911ae08745Sheppo 		return (EBADMSG);
29921ae08745Sheppo 	}
29931ae08745Sheppo 
29941ae08745Sheppo 	return (0);
29951ae08745Sheppo }
29961ae08745Sheppo 
29971ae08745Sheppo static int
29981ae08745Sheppo process_rdx_msg(vio_msg_t *msg, size_t msglen)
29991ae08745Sheppo {
30001ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
30011ae08745Sheppo 
3002d10e4ef2Snarayan 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX)) {
3003d10e4ef2Snarayan 		PR0("Message is not an RDX message");
3004d10e4ef2Snarayan 		return (ENOMSG);
3005d10e4ef2Snarayan 	}
30061ae08745Sheppo 
30071ae08745Sheppo 	if (msglen != sizeof (vio_rdx_msg_t)) {
30083af08d82Slm66018 		PR0("Expected %lu-byte RDX message; received %lu bytes",
30091ae08745Sheppo 		    sizeof (vio_rdx_msg_t), msglen);
30101ae08745Sheppo 		return (EBADMSG);
30111ae08745Sheppo 	}
30121ae08745Sheppo 
3013d10e4ef2Snarayan 	PR0("Valid RDX message");
30141ae08745Sheppo 	return (0);
30151ae08745Sheppo }
30161ae08745Sheppo 
30171ae08745Sheppo static int
30181ae08745Sheppo vd_check_seq_num(vd_t *vd, uint64_t seq_num)
30191ae08745Sheppo {
30201ae08745Sheppo 	if ((vd->initialized & VD_SEQ_NUM) && (seq_num != vd->seq_num + 1)) {
30213af08d82Slm66018 		PR0("Received seq_num %lu; expected %lu",
30221ae08745Sheppo 		    seq_num, (vd->seq_num + 1));
30233af08d82Slm66018 		PR0("initiating soft reset");
3024d10e4ef2Snarayan 		vd_need_reset(vd, B_FALSE);
30251ae08745Sheppo 		return (1);
30261ae08745Sheppo 	}
30271ae08745Sheppo 
30281ae08745Sheppo 	vd->seq_num = seq_num;
30291ae08745Sheppo 	vd->initialized |= VD_SEQ_NUM;	/* superfluous after first time... */
30301ae08745Sheppo 	return (0);
30311ae08745Sheppo }
30321ae08745Sheppo 
30331ae08745Sheppo /*
30341ae08745Sheppo  * Return the expected size of an inband-descriptor message with all the
30351ae08745Sheppo  * cookies it claims to include
30361ae08745Sheppo  */
30371ae08745Sheppo static size_t
30381ae08745Sheppo expected_inband_size(vd_dring_inband_msg_t *msg)
30391ae08745Sheppo {
30401ae08745Sheppo 	return ((sizeof (*msg)) +
30411ae08745Sheppo 	    (msg->payload.ncookies - 1)*(sizeof (msg->payload.cookie[0])));
30421ae08745Sheppo }
30431ae08745Sheppo 
30441ae08745Sheppo /*
30451ae08745Sheppo  * Process an in-band descriptor message:  used with clients like OBP, with
30461ae08745Sheppo  * which vds exchanges descriptors within VIO message payloads, rather than
30471ae08745Sheppo  * operating on them within a descriptor ring
30481ae08745Sheppo  */
30491ae08745Sheppo static int
30503af08d82Slm66018 vd_process_desc_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
30511ae08745Sheppo {
30521ae08745Sheppo 	size_t			expected;
30531ae08745Sheppo 	vd_dring_inband_msg_t	*desc_msg = (vd_dring_inband_msg_t *)msg;
30541ae08745Sheppo 
30551ae08745Sheppo 
30561ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
30571ae08745Sheppo 
30581ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
3059d10e4ef2Snarayan 	    VIO_DESC_DATA)) {
3060d10e4ef2Snarayan 		PR1("Message is not an in-band-descriptor message");
3061d10e4ef2Snarayan 		return (ENOMSG);
3062d10e4ef2Snarayan 	}
30631ae08745Sheppo 
30641ae08745Sheppo 	if (msglen < sizeof (*desc_msg)) {
30653af08d82Slm66018 		PR0("Expected at least %lu-byte descriptor message; "
30661ae08745Sheppo 		    "received %lu bytes", sizeof (*desc_msg), msglen);
30671ae08745Sheppo 		return (EBADMSG);
30681ae08745Sheppo 	}
30691ae08745Sheppo 
30701ae08745Sheppo 	if (msglen != (expected = expected_inband_size(desc_msg))) {
30713af08d82Slm66018 		PR0("Expected %lu-byte descriptor message; "
30721ae08745Sheppo 		    "received %lu bytes", expected, msglen);
30731ae08745Sheppo 		return (EBADMSG);
30741ae08745Sheppo 	}
30751ae08745Sheppo 
3076d10e4ef2Snarayan 	if (vd_check_seq_num(vd, desc_msg->hdr.seq_num) != 0)
30771ae08745Sheppo 		return (EBADMSG);
30781ae08745Sheppo 
3079d10e4ef2Snarayan 	/*
3080d10e4ef2Snarayan 	 * Valid message:  Set up the in-band descriptor task and process the
3081d10e4ef2Snarayan 	 * request.  Arrange to acknowledge the client's message, unless an
3082d10e4ef2Snarayan 	 * error processing the descriptor task results in setting
3083d10e4ef2Snarayan 	 * VIO_SUBTYPE_NACK
3084d10e4ef2Snarayan 	 */
3085d10e4ef2Snarayan 	PR1("Valid in-band-descriptor message");
3086d10e4ef2Snarayan 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
30873af08d82Slm66018 
30883af08d82Slm66018 	ASSERT(vd->inband_task.msg != NULL);
30893af08d82Slm66018 
30903af08d82Slm66018 	bcopy(msg, vd->inband_task.msg, msglen);
3091d10e4ef2Snarayan 	vd->inband_task.msglen	= msglen;
30923af08d82Slm66018 
30933af08d82Slm66018 	/*
30943af08d82Slm66018 	 * The task request is now the payload of the message
30953af08d82Slm66018 	 * that was just copied into the body of the task.
30963af08d82Slm66018 	 */
30973af08d82Slm66018 	desc_msg = (vd_dring_inband_msg_t *)vd->inband_task.msg;
3098d10e4ef2Snarayan 	vd->inband_task.request	= &desc_msg->payload;
30993af08d82Slm66018 
3100d10e4ef2Snarayan 	return (vd_process_task(&vd->inband_task));
31011ae08745Sheppo }
31021ae08745Sheppo 
31031ae08745Sheppo static int
3104d10e4ef2Snarayan vd_process_element(vd_t *vd, vd_task_type_t type, uint32_t idx,
31053af08d82Slm66018     vio_msg_t *msg, size_t msglen)
31061ae08745Sheppo {
31071ae08745Sheppo 	int			status;
3108d10e4ef2Snarayan 	boolean_t		ready;
3109d10e4ef2Snarayan 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
31101ae08745Sheppo 
31111ae08745Sheppo 
3112d10e4ef2Snarayan 	/* Accept the updated dring element */
3113d10e4ef2Snarayan 	if ((status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) {
31143af08d82Slm66018 		PR0("ldc_mem_dring_acquire() returned errno %d", status);
31151ae08745Sheppo 		return (status);
31161ae08745Sheppo 	}
3117d10e4ef2Snarayan 	ready = (elem->hdr.dstate == VIO_DESC_READY);
3118d10e4ef2Snarayan 	if (ready) {
3119d10e4ef2Snarayan 		elem->hdr.dstate = VIO_DESC_ACCEPTED;
3120d10e4ef2Snarayan 	} else {
31213af08d82Slm66018 		PR0("descriptor %u not ready", idx);
3122d10e4ef2Snarayan 		VD_DUMP_DRING_ELEM(elem);
3123d10e4ef2Snarayan 	}
3124d10e4ef2Snarayan 	if ((status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) {
31253af08d82Slm66018 		PR0("ldc_mem_dring_release() returned errno %d", status);
31261ae08745Sheppo 		return (status);
31271ae08745Sheppo 	}
3128d10e4ef2Snarayan 	if (!ready)
3129d10e4ef2Snarayan 		return (EBUSY);
31301ae08745Sheppo 
31311ae08745Sheppo 
3132d10e4ef2Snarayan 	/* Initialize a task and process the accepted element */
3133d10e4ef2Snarayan 	PR1("Processing dring element %u", idx);
3134d10e4ef2Snarayan 	vd->dring_task[idx].type	= type;
31353af08d82Slm66018 
31363af08d82Slm66018 	/* duplicate msg buf for cookies etc. */
31373af08d82Slm66018 	bcopy(msg, vd->dring_task[idx].msg, msglen);
31383af08d82Slm66018 
3139d10e4ef2Snarayan 	vd->dring_task[idx].msglen	= msglen;
3140205eeb1aSlm66018 	return (vd_process_task(&vd->dring_task[idx]));
31411ae08745Sheppo }
31421ae08745Sheppo 
31431ae08745Sheppo static int
3144d10e4ef2Snarayan vd_process_element_range(vd_t *vd, int start, int end,
31453af08d82Slm66018     vio_msg_t *msg, size_t msglen)
3146d10e4ef2Snarayan {
3147d10e4ef2Snarayan 	int		i, n, nelem, status = 0;
3148d10e4ef2Snarayan 	boolean_t	inprogress = B_FALSE;
3149d10e4ef2Snarayan 	vd_task_type_t	type;
3150d10e4ef2Snarayan 
3151d10e4ef2Snarayan 
3152d10e4ef2Snarayan 	ASSERT(start >= 0);
3153d10e4ef2Snarayan 	ASSERT(end >= 0);
3154d10e4ef2Snarayan 
3155d10e4ef2Snarayan 	/*
3156d10e4ef2Snarayan 	 * Arrange to acknowledge the client's message, unless an error
3157d10e4ef2Snarayan 	 * processing one of the dring elements results in setting
3158d10e4ef2Snarayan 	 * VIO_SUBTYPE_NACK
3159d10e4ef2Snarayan 	 */
3160d10e4ef2Snarayan 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
3161d10e4ef2Snarayan 
3162d10e4ef2Snarayan 	/*
3163d10e4ef2Snarayan 	 * Process the dring elements in the range
3164d10e4ef2Snarayan 	 */
3165d10e4ef2Snarayan 	nelem = ((end < start) ? end + vd->dring_len : end) - start + 1;
3166d10e4ef2Snarayan 	for (i = start, n = nelem; n > 0; i = (i + 1) % vd->dring_len, n--) {
3167d10e4ef2Snarayan 		((vio_dring_msg_t *)msg)->end_idx = i;
3168d10e4ef2Snarayan 		type = (n == 1) ? VD_FINAL_RANGE_TASK : VD_NONFINAL_RANGE_TASK;
31693af08d82Slm66018 		status = vd_process_element(vd, type, i, msg, msglen);
3170d10e4ef2Snarayan 		if (status == EINPROGRESS)
3171d10e4ef2Snarayan 			inprogress = B_TRUE;
3172d10e4ef2Snarayan 		else if (status != 0)
3173d10e4ef2Snarayan 			break;
3174d10e4ef2Snarayan 	}
3175d10e4ef2Snarayan 
3176d10e4ef2Snarayan 	/*
3177d10e4ef2Snarayan 	 * If some, but not all, operations of a multi-element range are in
3178d10e4ef2Snarayan 	 * progress, wait for other operations to complete before returning
3179d10e4ef2Snarayan 	 * (which will result in "ack" or "nack" of the message).  Note that
3180d10e4ef2Snarayan 	 * all outstanding operations will need to complete, not just the ones
3181d10e4ef2Snarayan 	 * corresponding to the current range of dring elements; howevever, as
3182d10e4ef2Snarayan 	 * this situation is an error case, performance is less critical.
3183d10e4ef2Snarayan 	 */
3184d10e4ef2Snarayan 	if ((nelem > 1) && (status != EINPROGRESS) && inprogress)
3185d10e4ef2Snarayan 		ddi_taskq_wait(vd->completionq);
3186d10e4ef2Snarayan 
3187d10e4ef2Snarayan 	return (status);
3188d10e4ef2Snarayan }
3189d10e4ef2Snarayan 
3190d10e4ef2Snarayan static int
31913af08d82Slm66018 vd_process_dring_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
31921ae08745Sheppo {
31931ae08745Sheppo 	vio_dring_msg_t	*dring_msg = (vio_dring_msg_t *)msg;
31941ae08745Sheppo 
31951ae08745Sheppo 
31961ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
31971ae08745Sheppo 
31981ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
31991ae08745Sheppo 	    VIO_DRING_DATA)) {
3200d10e4ef2Snarayan 		PR1("Message is not a dring-data message");
3201d10e4ef2Snarayan 		return (ENOMSG);
32021ae08745Sheppo 	}
32031ae08745Sheppo 
32041ae08745Sheppo 	if (msglen != sizeof (*dring_msg)) {
32053af08d82Slm66018 		PR0("Expected %lu-byte dring message; received %lu bytes",
32061ae08745Sheppo 		    sizeof (*dring_msg), msglen);
32071ae08745Sheppo 		return (EBADMSG);
32081ae08745Sheppo 	}
32091ae08745Sheppo 
3210d10e4ef2Snarayan 	if (vd_check_seq_num(vd, dring_msg->seq_num) != 0)
32111ae08745Sheppo 		return (EBADMSG);
32121ae08745Sheppo 
32131ae08745Sheppo 	if (dring_msg->dring_ident != vd->dring_ident) {
32143af08d82Slm66018 		PR0("Expected dring ident %lu; received ident %lu",
32151ae08745Sheppo 		    vd->dring_ident, dring_msg->dring_ident);
32161ae08745Sheppo 		return (EBADMSG);
32171ae08745Sheppo 	}
32181ae08745Sheppo 
3219d10e4ef2Snarayan 	if (dring_msg->start_idx >= vd->dring_len) {
32203af08d82Slm66018 		PR0("\"start_idx\" = %u; must be less than %u",
3221d10e4ef2Snarayan 		    dring_msg->start_idx, vd->dring_len);
3222d10e4ef2Snarayan 		return (EBADMSG);
3223d10e4ef2Snarayan 	}
32241ae08745Sheppo 
3225d10e4ef2Snarayan 	if ((dring_msg->end_idx < 0) ||
3226d10e4ef2Snarayan 	    (dring_msg->end_idx >= vd->dring_len)) {
32273af08d82Slm66018 		PR0("\"end_idx\" = %u; must be >= 0 and less than %u",
3228d10e4ef2Snarayan 		    dring_msg->end_idx, vd->dring_len);
3229d10e4ef2Snarayan 		return (EBADMSG);
3230d10e4ef2Snarayan 	}
3231d10e4ef2Snarayan 
3232d10e4ef2Snarayan 	/* Valid message; process range of updated dring elements */
3233d10e4ef2Snarayan 	PR1("Processing descriptor range, start = %u, end = %u",
3234d10e4ef2Snarayan 	    dring_msg->start_idx, dring_msg->end_idx);
3235d10e4ef2Snarayan 	return (vd_process_element_range(vd, dring_msg->start_idx,
32363af08d82Slm66018 	    dring_msg->end_idx, msg, msglen));
32371ae08745Sheppo }
32381ae08745Sheppo 
32391ae08745Sheppo static int
32401ae08745Sheppo recv_msg(ldc_handle_t ldc_handle, void *msg, size_t *nbytes)
32411ae08745Sheppo {
32421ae08745Sheppo 	int	retry, status;
32431ae08745Sheppo 	size_t	size = *nbytes;
32441ae08745Sheppo 
32451ae08745Sheppo 
32461ae08745Sheppo 	for (retry = 0, status = ETIMEDOUT;
32471ae08745Sheppo 	    retry < vds_ldc_retries && status == ETIMEDOUT;
32481ae08745Sheppo 	    retry++) {
32491ae08745Sheppo 		PR1("ldc_read() attempt %d", (retry + 1));
32501ae08745Sheppo 		*nbytes = size;
32511ae08745Sheppo 		status = ldc_read(ldc_handle, msg, nbytes);
32521ae08745Sheppo 	}
32531ae08745Sheppo 
32543af08d82Slm66018 	if (status) {
32553af08d82Slm66018 		PR0("ldc_read() returned errno %d", status);
32563af08d82Slm66018 		if (status != ECONNRESET)
32573af08d82Slm66018 			return (ENOMSG);
32581ae08745Sheppo 		return (status);
32591ae08745Sheppo 	} else if (*nbytes == 0) {
32601ae08745Sheppo 		PR1("ldc_read() returned 0 and no message read");
32611ae08745Sheppo 		return (ENOMSG);
32621ae08745Sheppo 	}
32631ae08745Sheppo 
32641ae08745Sheppo 	PR1("RCVD %lu-byte message", *nbytes);
32651ae08745Sheppo 	return (0);
32661ae08745Sheppo }
32671ae08745Sheppo 
32681ae08745Sheppo static int
32693af08d82Slm66018 vd_do_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
32701ae08745Sheppo {
32711ae08745Sheppo 	int		status;
32721ae08745Sheppo 
32731ae08745Sheppo 
32741ae08745Sheppo 	PR1("Processing (%x/%x/%x) message", msg->tag.vio_msgtype,
32751ae08745Sheppo 	    msg->tag.vio_subtype, msg->tag.vio_subtype_env);
32763af08d82Slm66018 #ifdef	DEBUG
32773af08d82Slm66018 	vd_decode_tag(msg);
32783af08d82Slm66018 #endif
32791ae08745Sheppo 
32801ae08745Sheppo 	/*
32811ae08745Sheppo 	 * Validate session ID up front, since it applies to all messages
32821ae08745Sheppo 	 * once set
32831ae08745Sheppo 	 */
32841ae08745Sheppo 	if ((msg->tag.vio_sid != vd->sid) && (vd->initialized & VD_SID)) {
32853af08d82Slm66018 		PR0("Expected SID %u, received %u", vd->sid,
32861ae08745Sheppo 		    msg->tag.vio_sid);
32871ae08745Sheppo 		return (EBADMSG);
32881ae08745Sheppo 	}
32891ae08745Sheppo 
32903af08d82Slm66018 	PR1("\tWhile in state %d (%s)", vd->state, vd_decode_state(vd->state));
32911ae08745Sheppo 
32921ae08745Sheppo 	/*
32931ae08745Sheppo 	 * Process the received message based on connection state
32941ae08745Sheppo 	 */
32951ae08745Sheppo 	switch (vd->state) {
32961ae08745Sheppo 	case VD_STATE_INIT:	/* expect version message */
32970a55fbb7Slm66018 		if ((status = vd_process_ver_msg(vd, msg, msglen)) != 0)
32981ae08745Sheppo 			return (status);
32991ae08745Sheppo 
33001ae08745Sheppo 		/* Version negotiated, move to that state */
33011ae08745Sheppo 		vd->state = VD_STATE_VER;
33021ae08745Sheppo 		return (0);
33031ae08745Sheppo 
33041ae08745Sheppo 	case VD_STATE_VER:	/* expect attribute message */
33051ae08745Sheppo 		if ((status = vd_process_attr_msg(vd, msg, msglen)) != 0)
33061ae08745Sheppo 			return (status);
33071ae08745Sheppo 
33081ae08745Sheppo 		/* Attributes exchanged, move to that state */
33091ae08745Sheppo 		vd->state = VD_STATE_ATTR;
33101ae08745Sheppo 		return (0);
33111ae08745Sheppo 
33121ae08745Sheppo 	case VD_STATE_ATTR:
33131ae08745Sheppo 		switch (vd->xfer_mode) {
33141ae08745Sheppo 		case VIO_DESC_MODE:	/* expect RDX message */
33151ae08745Sheppo 			if ((status = process_rdx_msg(msg, msglen)) != 0)
33161ae08745Sheppo 				return (status);
33171ae08745Sheppo 
33181ae08745Sheppo 			/* Ready to receive in-band descriptors */
33191ae08745Sheppo 			vd->state = VD_STATE_DATA;
33201ae08745Sheppo 			return (0);
33211ae08745Sheppo 
33221ae08745Sheppo 		case VIO_DRING_MODE:	/* expect register-dring message */
33231ae08745Sheppo 			if ((status =
33241ae08745Sheppo 			    vd_process_dring_reg_msg(vd, msg, msglen)) != 0)
33251ae08745Sheppo 				return (status);
33261ae08745Sheppo 
33271ae08745Sheppo 			/* One dring negotiated, move to that state */
33281ae08745Sheppo 			vd->state = VD_STATE_DRING;
33291ae08745Sheppo 			return (0);
33301ae08745Sheppo 
33311ae08745Sheppo 		default:
33321ae08745Sheppo 			ASSERT("Unsupported transfer mode");
33333af08d82Slm66018 			PR0("Unsupported transfer mode");
33341ae08745Sheppo 			return (ENOTSUP);
33351ae08745Sheppo 		}
33361ae08745Sheppo 
33371ae08745Sheppo 	case VD_STATE_DRING:	/* expect RDX, register-dring, or unreg-dring */
33381ae08745Sheppo 		if ((status = process_rdx_msg(msg, msglen)) == 0) {
33391ae08745Sheppo 			/* Ready to receive data */
33401ae08745Sheppo 			vd->state = VD_STATE_DATA;
33411ae08745Sheppo 			return (0);
33421ae08745Sheppo 		} else if (status != ENOMSG) {
33431ae08745Sheppo 			return (status);
33441ae08745Sheppo 		}
33451ae08745Sheppo 
33461ae08745Sheppo 
33471ae08745Sheppo 		/*
33481ae08745Sheppo 		 * If another register-dring message is received, stay in
33491ae08745Sheppo 		 * dring state in case the client sends RDX; although the
33501ae08745Sheppo 		 * protocol allows multiple drings, this server does not
33511ae08745Sheppo 		 * support using more than one
33521ae08745Sheppo 		 */
33531ae08745Sheppo 		if ((status =
33541ae08745Sheppo 		    vd_process_dring_reg_msg(vd, msg, msglen)) != ENOMSG)
33551ae08745Sheppo 			return (status);
33561ae08745Sheppo 
33571ae08745Sheppo 		/*
33581ae08745Sheppo 		 * Acknowledge an unregister-dring message, but reset the
33591ae08745Sheppo 		 * connection anyway:  Although the protocol allows
33601ae08745Sheppo 		 * unregistering drings, this server cannot serve a vdisk
33611ae08745Sheppo 		 * without its only dring
33621ae08745Sheppo 		 */
33631ae08745Sheppo 		status = vd_process_dring_unreg_msg(vd, msg, msglen);
33641ae08745Sheppo 		return ((status == 0) ? ENOTSUP : status);
33651ae08745Sheppo 
33661ae08745Sheppo 	case VD_STATE_DATA:
33671ae08745Sheppo 		switch (vd->xfer_mode) {
33681ae08745Sheppo 		case VIO_DESC_MODE:	/* expect in-band-descriptor message */
33693af08d82Slm66018 			return (vd_process_desc_msg(vd, msg, msglen));
33701ae08745Sheppo 
33711ae08745Sheppo 		case VIO_DRING_MODE:	/* expect dring-data or unreg-dring */
33721ae08745Sheppo 			/*
33731ae08745Sheppo 			 * Typically expect dring-data messages, so handle
33741ae08745Sheppo 			 * them first
33751ae08745Sheppo 			 */
33761ae08745Sheppo 			if ((status = vd_process_dring_msg(vd, msg,
33773af08d82Slm66018 			    msglen)) != ENOMSG)
33781ae08745Sheppo 				return (status);
33791ae08745Sheppo 
33801ae08745Sheppo 			/*
33811ae08745Sheppo 			 * Acknowledge an unregister-dring message, but reset
33821ae08745Sheppo 			 * the connection anyway:  Although the protocol
33831ae08745Sheppo 			 * allows unregistering drings, this server cannot
33841ae08745Sheppo 			 * serve a vdisk without its only dring
33851ae08745Sheppo 			 */
33861ae08745Sheppo 			status = vd_process_dring_unreg_msg(vd, msg, msglen);
33871ae08745Sheppo 			return ((status == 0) ? ENOTSUP : status);
33881ae08745Sheppo 
33891ae08745Sheppo 		default:
33901ae08745Sheppo 			ASSERT("Unsupported transfer mode");
33913af08d82Slm66018 			PR0("Unsupported transfer mode");
33921ae08745Sheppo 			return (ENOTSUP);
33931ae08745Sheppo 		}
33941ae08745Sheppo 
33951ae08745Sheppo 	default:
33961ae08745Sheppo 		ASSERT("Invalid client connection state");
33973af08d82Slm66018 		PR0("Invalid client connection state");
33981ae08745Sheppo 		return (ENOTSUP);
33991ae08745Sheppo 	}
34001ae08745Sheppo }
34011ae08745Sheppo 
3402d10e4ef2Snarayan static int
34033af08d82Slm66018 vd_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
34041ae08745Sheppo {
34051ae08745Sheppo 	int		status;
34061ae08745Sheppo 	boolean_t	reset_ldc = B_FALSE;
3407205eeb1aSlm66018 	vd_task_t	task;
34081ae08745Sheppo 
34091ae08745Sheppo 	/*
34101ae08745Sheppo 	 * Check that the message is at least big enough for a "tag", so that
34111ae08745Sheppo 	 * message processing can proceed based on tag-specified message type
34121ae08745Sheppo 	 */
34131ae08745Sheppo 	if (msglen < sizeof (vio_msg_tag_t)) {
34143af08d82Slm66018 		PR0("Received short (%lu-byte) message", msglen);
34151ae08745Sheppo 		/* Can't "nack" short message, so drop the big hammer */
34163af08d82Slm66018 		PR0("initiating full reset");
3417d10e4ef2Snarayan 		vd_need_reset(vd, B_TRUE);
3418d10e4ef2Snarayan 		return (EBADMSG);
34191ae08745Sheppo 	}
34201ae08745Sheppo 
34211ae08745Sheppo 	/*
34221ae08745Sheppo 	 * Process the message
34231ae08745Sheppo 	 */
34243af08d82Slm66018 	switch (status = vd_do_process_msg(vd, msg, msglen)) {
34251ae08745Sheppo 	case 0:
34261ae08745Sheppo 		/* "ack" valid, successfully-processed messages */
34271ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
34281ae08745Sheppo 		break;
34291ae08745Sheppo 
3430d10e4ef2Snarayan 	case EINPROGRESS:
3431d10e4ef2Snarayan 		/* The completion handler will "ack" or "nack" the message */
3432d10e4ef2Snarayan 		return (EINPROGRESS);
34331ae08745Sheppo 	case ENOMSG:
34343af08d82Slm66018 		PR0("Received unexpected message");
34351ae08745Sheppo 		_NOTE(FALLTHROUGH);
34361ae08745Sheppo 	case EBADMSG:
34371ae08745Sheppo 	case ENOTSUP:
3438205eeb1aSlm66018 		/* "transport" error will cause NACK of invalid messages */
34391ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
34401ae08745Sheppo 		break;
34411ae08745Sheppo 
34421ae08745Sheppo 	default:
3443205eeb1aSlm66018 		/* "transport" error will cause NACK of invalid messages */
34441ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
34451ae08745Sheppo 		/* An LDC error probably occurred, so try resetting it */
34461ae08745Sheppo 		reset_ldc = B_TRUE;
34471ae08745Sheppo 		break;
34481ae08745Sheppo 	}
34491ae08745Sheppo 
34503af08d82Slm66018 	PR1("\tResulting in state %d (%s)", vd->state,
34513af08d82Slm66018 	    vd_decode_state(vd->state));
34523af08d82Slm66018 
3453205eeb1aSlm66018 	/* populate the task so we can dispatch it on the taskq */
3454205eeb1aSlm66018 	task.vd = vd;
3455205eeb1aSlm66018 	task.msg = msg;
3456205eeb1aSlm66018 	task.msglen = msglen;
3457205eeb1aSlm66018 
3458205eeb1aSlm66018 	/*
3459205eeb1aSlm66018 	 * Queue a task to send the notification that the operation completed.
3460205eeb1aSlm66018 	 * We need to ensure that requests are responded to in the correct
3461205eeb1aSlm66018 	 * order and since the taskq is processed serially this ordering
3462205eeb1aSlm66018 	 * is maintained.
3463205eeb1aSlm66018 	 */
3464205eeb1aSlm66018 	(void) ddi_taskq_dispatch(vd->completionq, vd_serial_notify,
3465205eeb1aSlm66018 	    &task, DDI_SLEEP);
3466205eeb1aSlm66018 
3467205eeb1aSlm66018 	/*
3468205eeb1aSlm66018 	 * To ensure handshake negotiations do not happen out of order, such
3469205eeb1aSlm66018 	 * requests that come through this path should not be done in parallel
3470205eeb1aSlm66018 	 * so we need to wait here until the response is sent to the client.
3471205eeb1aSlm66018 	 */
3472205eeb1aSlm66018 	ddi_taskq_wait(vd->completionq);
34731ae08745Sheppo 
3474d10e4ef2Snarayan 	/* Arrange to reset the connection for nack'ed or failed messages */
34753af08d82Slm66018 	if ((status != 0) || reset_ldc) {
34763af08d82Slm66018 		PR0("initiating %s reset",
34773af08d82Slm66018 		    (reset_ldc) ? "full" : "soft");
3478d10e4ef2Snarayan 		vd_need_reset(vd, reset_ldc);
34793af08d82Slm66018 	}
3480d10e4ef2Snarayan 
3481d10e4ef2Snarayan 	return (status);
3482d10e4ef2Snarayan }
3483d10e4ef2Snarayan 
3484d10e4ef2Snarayan static boolean_t
3485d10e4ef2Snarayan vd_enabled(vd_t *vd)
3486d10e4ef2Snarayan {
3487d10e4ef2Snarayan 	boolean_t	enabled;
3488d10e4ef2Snarayan 
3489d10e4ef2Snarayan 	mutex_enter(&vd->lock);
3490d10e4ef2Snarayan 	enabled = vd->enabled;
3491d10e4ef2Snarayan 	mutex_exit(&vd->lock);
3492d10e4ef2Snarayan 	return (enabled);
34931ae08745Sheppo }
34941ae08745Sheppo 
34951ae08745Sheppo static void
34960a55fbb7Slm66018 vd_recv_msg(void *arg)
34971ae08745Sheppo {
34981ae08745Sheppo 	vd_t	*vd = (vd_t *)arg;
34993af08d82Slm66018 	int	rv = 0, status = 0;
35001ae08745Sheppo 
35011ae08745Sheppo 	ASSERT(vd != NULL);
35023af08d82Slm66018 
3503d10e4ef2Snarayan 	PR2("New task to receive incoming message(s)");
35043af08d82Slm66018 
35053af08d82Slm66018 
3506d10e4ef2Snarayan 	while (vd_enabled(vd) && status == 0) {
3507d10e4ef2Snarayan 		size_t		msglen, msgsize;
35083af08d82Slm66018 		ldc_status_t	lstatus;
3509d10e4ef2Snarayan 
35100a55fbb7Slm66018 		/*
3511d10e4ef2Snarayan 		 * Receive and process a message
35120a55fbb7Slm66018 		 */
3513d10e4ef2Snarayan 		vd_reset_if_needed(vd);	/* can change vd->max_msglen */
35143af08d82Slm66018 
35153af08d82Slm66018 		/*
35163af08d82Slm66018 		 * check if channel is UP - else break out of loop
35173af08d82Slm66018 		 */
35183af08d82Slm66018 		status = ldc_status(vd->ldc_handle, &lstatus);
35193af08d82Slm66018 		if (lstatus != LDC_UP) {
35203af08d82Slm66018 			PR0("channel not up (status=%d), exiting recv loop\n",
35213af08d82Slm66018 			    lstatus);
35223af08d82Slm66018 			break;
35233af08d82Slm66018 		}
35243af08d82Slm66018 
35253af08d82Slm66018 		ASSERT(vd->max_msglen != 0);
35263af08d82Slm66018 
3527d10e4ef2Snarayan 		msgsize = vd->max_msglen; /* stable copy for alloc/free */
35283af08d82Slm66018 		msglen	= msgsize;	  /* actual len after recv_msg() */
35293af08d82Slm66018 
35303af08d82Slm66018 		status = recv_msg(vd->ldc_handle, vd->vio_msgp, &msglen);
35313af08d82Slm66018 		switch (status) {
35323af08d82Slm66018 		case 0:
35333af08d82Slm66018 			rv = vd_process_msg(vd, (vio_msg_t *)vd->vio_msgp,
35343af08d82Slm66018 			    msglen);
35353af08d82Slm66018 			/* check if max_msglen changed */
35363af08d82Slm66018 			if (msgsize != vd->max_msglen) {
35373af08d82Slm66018 				PR0("max_msglen changed 0x%lx to 0x%lx bytes\n",
35383af08d82Slm66018 				    msgsize, vd->max_msglen);
35393af08d82Slm66018 				kmem_free(vd->vio_msgp, msgsize);
35403af08d82Slm66018 				vd->vio_msgp =
35413af08d82Slm66018 				    kmem_alloc(vd->max_msglen, KM_SLEEP);
35423af08d82Slm66018 			}
35433af08d82Slm66018 			if (rv == EINPROGRESS)
35443af08d82Slm66018 				continue;
35453af08d82Slm66018 			break;
35463af08d82Slm66018 
35473af08d82Slm66018 		case ENOMSG:
35483af08d82Slm66018 			break;
35493af08d82Slm66018 
35503af08d82Slm66018 		case ECONNRESET:
35513af08d82Slm66018 			PR0("initiating soft reset (ECONNRESET)\n");
35523af08d82Slm66018 			vd_need_reset(vd, B_FALSE);
35533af08d82Slm66018 			status = 0;
35543af08d82Slm66018 			break;
35553af08d82Slm66018 
35563af08d82Slm66018 		default:
3557d10e4ef2Snarayan 			/* Probably an LDC failure; arrange to reset it */
35583af08d82Slm66018 			PR0("initiating full reset (status=0x%x)", status);
3559d10e4ef2Snarayan 			vd_need_reset(vd, B_TRUE);
35603af08d82Slm66018 			break;
35610a55fbb7Slm66018 		}
35621ae08745Sheppo 	}
35633af08d82Slm66018 
3564d10e4ef2Snarayan 	PR2("Task finished");
35650a55fbb7Slm66018 }
35660a55fbb7Slm66018 
35670a55fbb7Slm66018 static uint_t
35681ae08745Sheppo vd_handle_ldc_events(uint64_t event, caddr_t arg)
35691ae08745Sheppo {
35701ae08745Sheppo 	vd_t	*vd = (vd_t *)(void *)arg;
35713af08d82Slm66018 	int	status;
35721ae08745Sheppo 
35731ae08745Sheppo 	ASSERT(vd != NULL);
3574d10e4ef2Snarayan 
3575d10e4ef2Snarayan 	if (!vd_enabled(vd))
3576d10e4ef2Snarayan 		return (LDC_SUCCESS);
3577d10e4ef2Snarayan 
35783af08d82Slm66018 	if (event & LDC_EVT_DOWN) {
357934683adeSsg70180 		PR0("LDC_EVT_DOWN: LDC channel went down");
35803af08d82Slm66018 
35813af08d82Slm66018 		vd_need_reset(vd, B_TRUE);
35823af08d82Slm66018 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
35833af08d82Slm66018 		    DDI_SLEEP);
35843af08d82Slm66018 		if (status == DDI_FAILURE) {
35853af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
35863af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
35873af08d82Slm66018 		}
35883af08d82Slm66018 	}
35893af08d82Slm66018 
3590d10e4ef2Snarayan 	if (event & LDC_EVT_RESET) {
35913af08d82Slm66018 		PR0("LDC_EVT_RESET: LDC channel was reset");
35923af08d82Slm66018 
35933af08d82Slm66018 		if (vd->state != VD_STATE_INIT) {
35943af08d82Slm66018 			PR0("scheduling full reset");
35953af08d82Slm66018 			vd_need_reset(vd, B_FALSE);
35963af08d82Slm66018 			status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
35973af08d82Slm66018 			    vd, DDI_SLEEP);
35983af08d82Slm66018 			if (status == DDI_FAILURE) {
35993af08d82Slm66018 				PR0("cannot schedule task to recv msg\n");
36003af08d82Slm66018 				vd_need_reset(vd, B_TRUE);
36013af08d82Slm66018 			}
36023af08d82Slm66018 
36033af08d82Slm66018 		} else {
36043af08d82Slm66018 			PR0("channel already reset, ignoring...\n");
36053af08d82Slm66018 			PR0("doing ldc up...\n");
36063af08d82Slm66018 			(void) ldc_up(vd->ldc_handle);
36073af08d82Slm66018 		}
36083af08d82Slm66018 
3609d10e4ef2Snarayan 		return (LDC_SUCCESS);
3610d10e4ef2Snarayan 	}
3611d10e4ef2Snarayan 
3612d10e4ef2Snarayan 	if (event & LDC_EVT_UP) {
36133af08d82Slm66018 		PR0("EVT_UP: LDC is up\nResetting client connection state");
36143af08d82Slm66018 		PR0("initiating soft reset");
3615d10e4ef2Snarayan 		vd_need_reset(vd, B_FALSE);
36163af08d82Slm66018 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
36173af08d82Slm66018 		    vd, DDI_SLEEP);
36183af08d82Slm66018 		if (status == DDI_FAILURE) {
36193af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
36203af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
36213af08d82Slm66018 			return (LDC_SUCCESS);
36223af08d82Slm66018 		}
3623d10e4ef2Snarayan 	}
3624d10e4ef2Snarayan 
3625d10e4ef2Snarayan 	if (event & LDC_EVT_READ) {
3626d10e4ef2Snarayan 		int	status;
3627d10e4ef2Snarayan 
3628d10e4ef2Snarayan 		PR1("New data available");
3629d10e4ef2Snarayan 		/* Queue a task to receive the new data */
3630d10e4ef2Snarayan 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
3631d10e4ef2Snarayan 		    DDI_SLEEP);
36323af08d82Slm66018 
36333af08d82Slm66018 		if (status == DDI_FAILURE) {
36343af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
36353af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
36363af08d82Slm66018 		}
3637d10e4ef2Snarayan 	}
3638d10e4ef2Snarayan 
3639d10e4ef2Snarayan 	return (LDC_SUCCESS);
36401ae08745Sheppo }
36411ae08745Sheppo 
36421ae08745Sheppo static uint_t
36431ae08745Sheppo vds_check_for_vd(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
36441ae08745Sheppo {
36451ae08745Sheppo 	_NOTE(ARGUNUSED(key, val))
36461ae08745Sheppo 	(*((uint_t *)arg))++;
36471ae08745Sheppo 	return (MH_WALK_TERMINATE);
36481ae08745Sheppo }
36491ae08745Sheppo 
36501ae08745Sheppo 
36511ae08745Sheppo static int
36521ae08745Sheppo vds_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
36531ae08745Sheppo {
36541ae08745Sheppo 	uint_t	vd_present = 0;
36551ae08745Sheppo 	minor_t	instance;
36561ae08745Sheppo 	vds_t	*vds;
36571ae08745Sheppo 
36581ae08745Sheppo 
36591ae08745Sheppo 	switch (cmd) {
36601ae08745Sheppo 	case DDI_DETACH:
36611ae08745Sheppo 		/* the real work happens below */
36621ae08745Sheppo 		break;
36631ae08745Sheppo 	case DDI_SUSPEND:
3664d10e4ef2Snarayan 		PR0("No action required for DDI_SUSPEND");
36651ae08745Sheppo 		return (DDI_SUCCESS);
36661ae08745Sheppo 	default:
36673af08d82Slm66018 		PR0("Unrecognized \"cmd\"");
36681ae08745Sheppo 		return (DDI_FAILURE);
36691ae08745Sheppo 	}
36701ae08745Sheppo 
36711ae08745Sheppo 	ASSERT(cmd == DDI_DETACH);
36721ae08745Sheppo 	instance = ddi_get_instance(dip);
36731ae08745Sheppo 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
36743af08d82Slm66018 		PR0("Could not get state for instance %u", instance);
36751ae08745Sheppo 		ddi_soft_state_free(vds_state, instance);
36761ae08745Sheppo 		return (DDI_FAILURE);
36771ae08745Sheppo 	}
36781ae08745Sheppo 
36791ae08745Sheppo 	/* Do no detach when serving any vdisks */
36801ae08745Sheppo 	mod_hash_walk(vds->vd_table, vds_check_for_vd, &vd_present);
36811ae08745Sheppo 	if (vd_present) {
36821ae08745Sheppo 		PR0("Not detaching because serving vdisks");
36831ae08745Sheppo 		return (DDI_FAILURE);
36841ae08745Sheppo 	}
36851ae08745Sheppo 
36861ae08745Sheppo 	PR0("Detaching");
3687445b4c2eSsb155480 	if (vds->initialized & VDS_MDEG) {
36881ae08745Sheppo 		(void) mdeg_unregister(vds->mdeg);
3689445b4c2eSsb155480 		kmem_free(vds->ispecp->specp, sizeof (vds_prop_template));
3690445b4c2eSsb155480 		kmem_free(vds->ispecp, sizeof (mdeg_node_spec_t));
3691445b4c2eSsb155480 		vds->ispecp = NULL;
3692445b4c2eSsb155480 		vds->mdeg = NULL;
3693445b4c2eSsb155480 	}
3694445b4c2eSsb155480 
36951ae08745Sheppo 	if (vds->initialized & VDS_LDI)
36961ae08745Sheppo 		(void) ldi_ident_release(vds->ldi_ident);
36971ae08745Sheppo 	mod_hash_destroy_hash(vds->vd_table);
36981ae08745Sheppo 	ddi_soft_state_free(vds_state, instance);
36991ae08745Sheppo 	return (DDI_SUCCESS);
37001ae08745Sheppo }
37011ae08745Sheppo 
37021ae08745Sheppo static boolean_t
37031ae08745Sheppo is_pseudo_device(dev_info_t *dip)
37041ae08745Sheppo {
37051ae08745Sheppo 	dev_info_t	*parent, *root = ddi_root_node();
37061ae08745Sheppo 
37071ae08745Sheppo 
37081ae08745Sheppo 	for (parent = ddi_get_parent(dip); (parent != NULL) && (parent != root);
37091ae08745Sheppo 	    parent = ddi_get_parent(parent)) {
37101ae08745Sheppo 		if (strcmp(ddi_get_name(parent), DEVI_PSEUDO_NEXNAME) == 0)
37111ae08745Sheppo 			return (B_TRUE);
37121ae08745Sheppo 	}
37131ae08745Sheppo 
37141ae08745Sheppo 	return (B_FALSE);
37151ae08745Sheppo }
37161ae08745Sheppo 
3717*17cadca8Slm66018 /*
3718*17cadca8Slm66018  * Description:
3719*17cadca8Slm66018  *	This function checks to see if the file being used as a
3720*17cadca8Slm66018  *	virtual disk is an ISO image. An ISO image is a special
3721*17cadca8Slm66018  *	case which can be booted/installed from like a CD/DVD
3722*17cadca8Slm66018  *
3723*17cadca8Slm66018  * Parameters:
3724*17cadca8Slm66018  *	vd		- disk on which the operation is performed.
3725*17cadca8Slm66018  *
3726*17cadca8Slm66018  * Return Code:
3727*17cadca8Slm66018  *	B_TRUE		- The file is an ISO 9660 compliant image
3728*17cadca8Slm66018  *	B_FALSE		- just a regular disk image file
3729*17cadca8Slm66018  */
3730*17cadca8Slm66018 static boolean_t
3731*17cadca8Slm66018 vd_file_is_iso_image(vd_t *vd)
3732*17cadca8Slm66018 {
3733*17cadca8Slm66018 	char	iso_buf[ISO_SECTOR_SIZE];
3734*17cadca8Slm66018 	int	i, rv;
3735*17cadca8Slm66018 	uint_t	sec;
3736*17cadca8Slm66018 
3737*17cadca8Slm66018 	ASSERT(vd->file);
3738*17cadca8Slm66018 
3739*17cadca8Slm66018 	/*
3740*17cadca8Slm66018 	 * If we have already discovered and saved this info we can
3741*17cadca8Slm66018 	 * short-circuit the check and avoid reading the file.
3742*17cadca8Slm66018 	 */
3743*17cadca8Slm66018 	if (vd->vdisk_media == VD_MEDIA_DVD || vd->vdisk_media == VD_MEDIA_CD)
3744*17cadca8Slm66018 		return (B_TRUE);
3745*17cadca8Slm66018 
3746*17cadca8Slm66018 	/*
3747*17cadca8Slm66018 	 * We wish to read the sector that should contain the 2nd ISO volume
3748*17cadca8Slm66018 	 * descriptor. The second field in this descriptor is called the
3749*17cadca8Slm66018 	 * Standard Identifier and is set to CD001 for a CD-ROM compliant
3750*17cadca8Slm66018 	 * to the ISO 9660 standard.
3751*17cadca8Slm66018 	 */
3752*17cadca8Slm66018 	sec = (ISO_VOLDESC_SEC * ISO_SECTOR_SIZE) / vd->vdisk_block_size;
3753*17cadca8Slm66018 	rv = vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)iso_buf,
3754*17cadca8Slm66018 	    sec, ISO_SECTOR_SIZE);
3755*17cadca8Slm66018 
3756*17cadca8Slm66018 	if (rv < 0)
3757*17cadca8Slm66018 		return (B_FALSE);
3758*17cadca8Slm66018 
3759*17cadca8Slm66018 	for (i = 0; i < ISO_ID_STRLEN; i++) {
3760*17cadca8Slm66018 		if (ISO_STD_ID(iso_buf)[i] != ISO_ID_STRING[i])
3761*17cadca8Slm66018 			return (B_FALSE);
3762*17cadca8Slm66018 	}
3763*17cadca8Slm66018 
3764*17cadca8Slm66018 	return (B_TRUE);
3765*17cadca8Slm66018 }
3766*17cadca8Slm66018 
3767*17cadca8Slm66018 /*
3768*17cadca8Slm66018  * Description:
3769*17cadca8Slm66018  *	This function checks to see if the virtual device is an ATAPI
3770*17cadca8Slm66018  *	device. ATAPI devices use Group 1 Read/Write commands, so
3771*17cadca8Slm66018  *	any USCSI calls vds makes need to take this into account.
3772*17cadca8Slm66018  *
3773*17cadca8Slm66018  * Parameters:
3774*17cadca8Slm66018  *	vd		- disk on which the operation is performed.
3775*17cadca8Slm66018  *
3776*17cadca8Slm66018  * Return Code:
3777*17cadca8Slm66018  *	B_TRUE		- The virtual disk is backed by an ATAPI device
3778*17cadca8Slm66018  *	B_FALSE		- not an ATAPI device (presumably SCSI)
3779*17cadca8Slm66018  */
3780*17cadca8Slm66018 static boolean_t
3781*17cadca8Slm66018 vd_is_atapi_device(vd_t *vd)
3782*17cadca8Slm66018 {
3783*17cadca8Slm66018 	boolean_t	is_atapi = B_FALSE;
3784*17cadca8Slm66018 	char		*variantp;
3785*17cadca8Slm66018 	int		rv;
3786*17cadca8Slm66018 
3787*17cadca8Slm66018 	ASSERT(vd->ldi_handle[0] != NULL);
3788*17cadca8Slm66018 	ASSERT(!vd->file);
3789*17cadca8Slm66018 
3790*17cadca8Slm66018 	rv = ldi_prop_lookup_string(vd->ldi_handle[0],
3791*17cadca8Slm66018 	    (LDI_DEV_T_ANY | DDI_PROP_DONTPASS), "variant", &variantp);
3792*17cadca8Slm66018 	if (rv == DDI_PROP_SUCCESS) {
3793*17cadca8Slm66018 		PR0("'variant' property exists for %s", vd->device_path);
3794*17cadca8Slm66018 		if (strcmp(variantp, "atapi") == 0)
3795*17cadca8Slm66018 			is_atapi = B_TRUE;
3796*17cadca8Slm66018 		ddi_prop_free(variantp);
3797*17cadca8Slm66018 	}
3798*17cadca8Slm66018 
3799*17cadca8Slm66018 	rv = ldi_prop_exists(vd->ldi_handle[0], LDI_DEV_T_ANY, "atapi");
3800*17cadca8Slm66018 	if (rv) {
3801*17cadca8Slm66018 		PR0("'atapi' property exists for %s", vd->device_path);
3802*17cadca8Slm66018 		is_atapi = B_TRUE;
3803*17cadca8Slm66018 	}
3804*17cadca8Slm66018 
3805*17cadca8Slm66018 	return (is_atapi);
3806*17cadca8Slm66018 }
3807*17cadca8Slm66018 
38081ae08745Sheppo static int
38090a55fbb7Slm66018 vd_setup_full_disk(vd_t *vd)
38100a55fbb7Slm66018 {
38110a55fbb7Slm66018 	int		rval, status;
38120a55fbb7Slm66018 	major_t		major = getmajor(vd->dev[0]);
38130a55fbb7Slm66018 	minor_t		minor = getminor(vd->dev[0]) - VD_ENTIRE_DISK_SLICE;
38144bac2208Snarayan 	struct dk_minfo	dk_minfo;
38150a55fbb7Slm66018 
3816047ba61eSachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
3817047ba61eSachartre 
38184bac2208Snarayan 	/*
38194bac2208Snarayan 	 * At this point, vdisk_size is set to the size of partition 2 but
38204bac2208Snarayan 	 * this does not represent the size of the disk because partition 2
38214bac2208Snarayan 	 * may not cover the entire disk and its size does not include reserved
38224bac2208Snarayan 	 * blocks. So we update vdisk_size to be the size of the entire disk.
38234bac2208Snarayan 	 */
38244bac2208Snarayan 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCGMEDIAINFO,
3825047ba61eSachartre 	    (intptr_t)&dk_minfo, (vd->open_flags | FKIOCTL),
38264bac2208Snarayan 	    kcred, &rval)) != 0) {
3827690555a1Sachartre 		PRN("ldi_ioctl(DKIOCGMEDIAINFO) returned errno %d",
38284bac2208Snarayan 		    status);
38290a55fbb7Slm66018 		return (status);
38300a55fbb7Slm66018 	}
38314bac2208Snarayan 	vd->vdisk_size = dk_minfo.dki_capacity;
3832*17cadca8Slm66018 	vd->block_size = dk_minfo.dki_lbsize;
3833*17cadca8Slm66018 	vd->vdisk_media = DK_MEDIATYPE2VD_MEDIATYPE(dk_minfo.dki_media_type);
3834*17cadca8Slm66018 	vd->vdisk_block_size = DEV_BSIZE;
38350a55fbb7Slm66018 
38360a55fbb7Slm66018 	/* Move dev number and LDI handle to entire-disk-slice array elements */
38370a55fbb7Slm66018 	vd->dev[VD_ENTIRE_DISK_SLICE]		= vd->dev[0];
38380a55fbb7Slm66018 	vd->dev[0]				= 0;
38390a55fbb7Slm66018 	vd->ldi_handle[VD_ENTIRE_DISK_SLICE]	= vd->ldi_handle[0];
38400a55fbb7Slm66018 	vd->ldi_handle[0]			= NULL;
38410a55fbb7Slm66018 
38420a55fbb7Slm66018 	/* Initialize device numbers for remaining slices and open them */
38430a55fbb7Slm66018 	for (int slice = 0; slice < vd->nslices; slice++) {
38440a55fbb7Slm66018 		/*
38450a55fbb7Slm66018 		 * Skip the entire-disk slice, as it's already open and its
38460a55fbb7Slm66018 		 * device known
38470a55fbb7Slm66018 		 */
38480a55fbb7Slm66018 		if (slice == VD_ENTIRE_DISK_SLICE)
38490a55fbb7Slm66018 			continue;
38500a55fbb7Slm66018 		ASSERT(vd->dev[slice] == 0);
38510a55fbb7Slm66018 		ASSERT(vd->ldi_handle[slice] == NULL);
38520a55fbb7Slm66018 
38530a55fbb7Slm66018 		/*
38540a55fbb7Slm66018 		 * Construct the device number for the current slice
38550a55fbb7Slm66018 		 */
38560a55fbb7Slm66018 		vd->dev[slice] = makedevice(major, (minor + slice));
38570a55fbb7Slm66018 
38580a55fbb7Slm66018 		/*
385934683adeSsg70180 		 * Open all slices of the disk to serve them to the client.
386034683adeSsg70180 		 * Slices are opened exclusively to prevent other threads or
386134683adeSsg70180 		 * processes in the service domain from performing I/O to
386234683adeSsg70180 		 * slices being accessed by a client.  Failure to open a slice
386334683adeSsg70180 		 * results in vds not serving this disk, as the client could
386434683adeSsg70180 		 * attempt (and should be able) to access any slice immediately.
386534683adeSsg70180 		 * Any slices successfully opened before a failure will get
386634683adeSsg70180 		 * closed by vds_destroy_vd() as a result of the error returned
386734683adeSsg70180 		 * by this function.
386834683adeSsg70180 		 *
386934683adeSsg70180 		 * We need to do the open with FNDELAY so that opening an empty
387034683adeSsg70180 		 * slice does not fail.
38710a55fbb7Slm66018 		 */
38720a55fbb7Slm66018 		PR0("Opening device major %u, minor %u = slice %u",
38730a55fbb7Slm66018 		    major, minor, slice);
3874047ba61eSachartre 
3875047ba61eSachartre 		/*
3876047ba61eSachartre 		 * Try to open the device. This can fail for example if we are
3877047ba61eSachartre 		 * opening an empty slice. So in case of a failure, we try the
3878047ba61eSachartre 		 * open again but this time with the FNDELAY flag.
3879047ba61eSachartre 		 */
3880047ba61eSachartre 		status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK,
3881047ba61eSachartre 		    vd->open_flags, kcred, &vd->ldi_handle[slice],
3882047ba61eSachartre 		    vd->vds->ldi_ident);
3883047ba61eSachartre 
3884047ba61eSachartre 		if (status != 0) {
3885047ba61eSachartre 			status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK,
3886047ba61eSachartre 			    vd->open_flags | FNDELAY, kcred,
3887047ba61eSachartre 			    &vd->ldi_handle[slice], vd->vds->ldi_ident);
3888047ba61eSachartre 		}
3889047ba61eSachartre 
3890047ba61eSachartre 		if (status != 0) {
3891690555a1Sachartre 			PRN("ldi_open_by_dev() returned errno %d "
38920a55fbb7Slm66018 			    "for slice %u", status, slice);
38930a55fbb7Slm66018 			/* vds_destroy_vd() will close any open slices */
3894690555a1Sachartre 			vd->ldi_handle[slice] = NULL;
38950a55fbb7Slm66018 			return (status);
38960a55fbb7Slm66018 		}
38970a55fbb7Slm66018 	}
38980a55fbb7Slm66018 
38990a55fbb7Slm66018 	return (0);
39000a55fbb7Slm66018 }
39010a55fbb7Slm66018 
39020a55fbb7Slm66018 static int
390378fcd0a1Sachartre vd_setup_partition_vtoc(vd_t *vd)
390478fcd0a1Sachartre {
390578fcd0a1Sachartre 	int rval, status;
390678fcd0a1Sachartre 	char *device_path = vd->device_path;
390778fcd0a1Sachartre 
390878fcd0a1Sachartre 	status = ldi_ioctl(vd->ldi_handle[0], DKIOCGGEOM,
3909047ba61eSachartre 	    (intptr_t)&vd->dk_geom, (vd->open_flags | FKIOCTL), kcred, &rval);
391078fcd0a1Sachartre 
391178fcd0a1Sachartre 	if (status != 0) {
391278fcd0a1Sachartre 		PRN("ldi_ioctl(DKIOCGEOM) returned errno %d for %s",
391378fcd0a1Sachartre 		    status, device_path);
391478fcd0a1Sachartre 		return (status);
391578fcd0a1Sachartre 	}
391678fcd0a1Sachartre 
391778fcd0a1Sachartre 	/* Initialize dk_geom structure for single-slice device */
391878fcd0a1Sachartre 	if (vd->dk_geom.dkg_nsect == 0) {
391978fcd0a1Sachartre 		PRN("%s geometry claims 0 sectors per track", device_path);
392078fcd0a1Sachartre 		return (EIO);
392178fcd0a1Sachartre 	}
392278fcd0a1Sachartre 	if (vd->dk_geom.dkg_nhead == 0) {
392378fcd0a1Sachartre 		PRN("%s geometry claims 0 heads", device_path);
392478fcd0a1Sachartre 		return (EIO);
392578fcd0a1Sachartre 	}
392678fcd0a1Sachartre 	vd->dk_geom.dkg_ncyl = vd->vdisk_size / vd->dk_geom.dkg_nsect /
392778fcd0a1Sachartre 	    vd->dk_geom.dkg_nhead;
392878fcd0a1Sachartre 	vd->dk_geom.dkg_acyl = 0;
392978fcd0a1Sachartre 	vd->dk_geom.dkg_pcyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl;
393078fcd0a1Sachartre 
393178fcd0a1Sachartre 
393278fcd0a1Sachartre 	/* Initialize vtoc structure for single-slice device */
393378fcd0a1Sachartre 	bcopy(VD_VOLUME_NAME, vd->vtoc.v_volume,
393478fcd0a1Sachartre 	    MIN(sizeof (VD_VOLUME_NAME), sizeof (vd->vtoc.v_volume)));
393578fcd0a1Sachartre 	bzero(vd->vtoc.v_part, sizeof (vd->vtoc.v_part));
393678fcd0a1Sachartre 	vd->vtoc.v_nparts = 1;
393778fcd0a1Sachartre 	vd->vtoc.v_part[0].p_tag = V_UNASSIGNED;
393878fcd0a1Sachartre 	vd->vtoc.v_part[0].p_flag = 0;
393978fcd0a1Sachartre 	vd->vtoc.v_part[0].p_start = 0;
394078fcd0a1Sachartre 	vd->vtoc.v_part[0].p_size = vd->vdisk_size;
394178fcd0a1Sachartre 	bcopy(VD_ASCIILABEL, vd->vtoc.v_asciilabel,
394278fcd0a1Sachartre 	    MIN(sizeof (VD_ASCIILABEL), sizeof (vd->vtoc.v_asciilabel)));
394378fcd0a1Sachartre 
394478fcd0a1Sachartre 	return (0);
394578fcd0a1Sachartre }
394678fcd0a1Sachartre 
394778fcd0a1Sachartre static int
39484bac2208Snarayan vd_setup_partition_efi(vd_t *vd)
39494bac2208Snarayan {
39504bac2208Snarayan 	efi_gpt_t *gpt;
39514bac2208Snarayan 	efi_gpe_t *gpe;
39524bac2208Snarayan 	struct uuid uuid = EFI_RESERVED;
39534bac2208Snarayan 	uint32_t crc;
39544bac2208Snarayan 	int length;
39554bac2208Snarayan 
39564bac2208Snarayan 	length = sizeof (efi_gpt_t) + sizeof (efi_gpe_t);
39574bac2208Snarayan 
39584bac2208Snarayan 	gpt = kmem_zalloc(length, KM_SLEEP);
39594bac2208Snarayan 	gpe = (efi_gpe_t *)(gpt + 1);
39604bac2208Snarayan 
39614bac2208Snarayan 	gpt->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
39624bac2208Snarayan 	gpt->efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
39634bac2208Snarayan 	gpt->efi_gpt_HeaderSize = LE_32(sizeof (efi_gpt_t));
39644bac2208Snarayan 	gpt->efi_gpt_FirstUsableLBA = LE_64(0ULL);
39654bac2208Snarayan 	gpt->efi_gpt_LastUsableLBA = LE_64(vd->vdisk_size - 1);
39664bac2208Snarayan 	gpt->efi_gpt_NumberOfPartitionEntries = LE_32(1);
39674bac2208Snarayan 	gpt->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (efi_gpe_t));
39684bac2208Snarayan 
39694bac2208Snarayan 	UUID_LE_CONVERT(gpe->efi_gpe_PartitionTypeGUID, uuid);
39704bac2208Snarayan 	gpe->efi_gpe_StartingLBA = gpt->efi_gpt_FirstUsableLBA;
39714bac2208Snarayan 	gpe->efi_gpe_EndingLBA = gpt->efi_gpt_LastUsableLBA;
39724bac2208Snarayan 
39734bac2208Snarayan 	CRC32(crc, gpe, sizeof (efi_gpe_t), -1U, crc32_table);
39744bac2208Snarayan 	gpt->efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
39754bac2208Snarayan 
39764bac2208Snarayan 	CRC32(crc, gpt, sizeof (efi_gpt_t), -1U, crc32_table);
39774bac2208Snarayan 	gpt->efi_gpt_HeaderCRC32 = LE_32(~crc);
39784bac2208Snarayan 
39794bac2208Snarayan 	vd->dk_efi.dki_lba = 0;
39804bac2208Snarayan 	vd->dk_efi.dki_length = length;
39814bac2208Snarayan 	vd->dk_efi.dki_data = gpt;
39824bac2208Snarayan 
39834bac2208Snarayan 	return (0);
39844bac2208Snarayan }
39854bac2208Snarayan 
3986047ba61eSachartre /*
3987047ba61eSachartre  * Setup for a virtual disk whose backend is a file (exported as a single slice
3988047ba61eSachartre  * or as a full disk) or a pseudo device (for example a ZFS, SVM or VxVM volume)
3989047ba61eSachartre  * exported as a full disk. In these cases, the backend is accessed using the
3990047ba61eSachartre  * vnode interface.
3991047ba61eSachartre  */
39924bac2208Snarayan static int
3993047ba61eSachartre vd_setup_backend_vnode(vd_t *vd)
39943c96341aSnarayan {
399578fcd0a1Sachartre 	int 		rval, status;
39963c96341aSnarayan 	vattr_t		vattr;
39973c96341aSnarayan 	dev_t		dev;
39983c96341aSnarayan 	char		*file_path = vd->device_path;
39993c96341aSnarayan 	char		dev_path[MAXPATHLEN + 1];
40003c96341aSnarayan 	ldi_handle_t	lhandle;
40013c96341aSnarayan 	struct dk_cinfo	dk_cinfo;
40023c96341aSnarayan 
4003047ba61eSachartre 	if ((status = vn_open(file_path, UIO_SYSSPACE, vd->open_flags | FOFFMAX,
40043c96341aSnarayan 	    0, &vd->file_vnode, 0, 0)) != 0) {
4005690555a1Sachartre 		PRN("vn_open(%s) = errno %d", file_path, status);
40063c96341aSnarayan 		return (status);
40073c96341aSnarayan 	}
40083c96341aSnarayan 
4009690555a1Sachartre 	/*
4010690555a1Sachartre 	 * We set vd->file now so that vds_destroy_vd will take care of
4011690555a1Sachartre 	 * closing the file and releasing the vnode in case of an error.
4012690555a1Sachartre 	 */
4013690555a1Sachartre 	vd->file = B_TRUE;
4014690555a1Sachartre 
40153c96341aSnarayan 	vattr.va_mask = AT_SIZE;
4016da6c28aaSamw 	if ((status = VOP_GETATTR(vd->file_vnode, &vattr, 0, kcred, NULL))
4017da6c28aaSamw 	    != 0) {
4018690555a1Sachartre 		PRN("VOP_GETATTR(%s) = errno %d", file_path, status);
40193c96341aSnarayan 		return (EIO);
40203c96341aSnarayan 	}
40213c96341aSnarayan 
40223c96341aSnarayan 	vd->file_size = vattr.va_size;
40233c96341aSnarayan 	/* size should be at least sizeof(dk_label) */
40243c96341aSnarayan 	if (vd->file_size < sizeof (struct dk_label)) {
40253c96341aSnarayan 		PRN("Size of file has to be at least %ld bytes",
40263c96341aSnarayan 		    sizeof (struct dk_label));
40273c96341aSnarayan 		return (EIO);
40283c96341aSnarayan 	}
40293c96341aSnarayan 
4030690555a1Sachartre 	if (vd->file_vnode->v_flag & VNOMAP) {
4031690555a1Sachartre 		PRN("File %s cannot be mapped", file_path);
40323c96341aSnarayan 		return (EIO);
40333c96341aSnarayan 	}
40343c96341aSnarayan 
4035047ba61eSachartre 	/*
4036047ba61eSachartre 	 * Find and validate the geometry of a disk image. For a single slice
4037047ba61eSachartre 	 * disk image, this will build a fake geometry and vtoc.
4038047ba61eSachartre 	 */
403978fcd0a1Sachartre 	status = vd_file_validate_geometry(vd);
404078fcd0a1Sachartre 	if (status != 0 && status != EINVAL) {
4041*17cadca8Slm66018 		PRN("Failed to read label from %s", file_path);
4042690555a1Sachartre 		return (EIO);
4043690555a1Sachartre 	}
40443c96341aSnarayan 
40453c96341aSnarayan 	/* sector size = block size = DEV_BSIZE */
4046*17cadca8Slm66018 	vd->block_size = DEV_BSIZE;
4047*17cadca8Slm66018 	vd->vdisk_block_size = DEV_BSIZE;
404887a7269eSachartre 	vd->vdisk_size = vd->file_size / DEV_BSIZE;
40493c96341aSnarayan 	vd->max_xfer_sz = maxphys / DEV_BSIZE; /* default transfer size */
40503c96341aSnarayan 
4051*17cadca8Slm66018 	if (vd_file_is_iso_image(vd)) {
4052*17cadca8Slm66018 		/*
4053*17cadca8Slm66018 		 * Indicate whether to call this a CD or DVD from the size
4054*17cadca8Slm66018 		 * of the ISO image (images for both drive types are stored
4055*17cadca8Slm66018 		 * in the ISO-9600 format). CDs can store up to just under 1Gb
4056*17cadca8Slm66018 		 */
4057*17cadca8Slm66018 		if ((vd->vdisk_size * vd->vdisk_block_size) >
4058*17cadca8Slm66018 		    (1024 * 1024 * 1024))
4059*17cadca8Slm66018 			vd->vdisk_media = VD_MEDIA_DVD;
4060*17cadca8Slm66018 		else
4061*17cadca8Slm66018 			vd->vdisk_media = VD_MEDIA_CD;
4062*17cadca8Slm66018 	} else {
4063*17cadca8Slm66018 		vd->vdisk_media = VD_MEDIA_FIXED;
4064*17cadca8Slm66018 	}
4065*17cadca8Slm66018 
4066047ba61eSachartre 	/*
4067047ba61eSachartre 	 * Get max_xfer_sz from the device where the file is or from the device
4068047ba61eSachartre 	 * itself if we have a pseudo device.
4069047ba61eSachartre 	 */
4070047ba61eSachartre 	dev_path[0] = '\0';
4071047ba61eSachartre 
4072047ba61eSachartre 	if (vd->pseudo) {
4073047ba61eSachartre 		status = ldi_open_by_name(file_path, FREAD, kcred, &lhandle,
4074047ba61eSachartre 		    vd->vds->ldi_ident);
4075047ba61eSachartre 	} else {
40763c96341aSnarayan 		dev = vd->file_vnode->v_vfsp->vfs_dev;
40773c96341aSnarayan 		if (ddi_dev_pathname(dev, S_IFBLK, dev_path) == DDI_SUCCESS) {
40783c96341aSnarayan 			PR0("underlying device = %s\n", dev_path);
40793c96341aSnarayan 		}
40803c96341aSnarayan 
4081047ba61eSachartre 		status = ldi_open_by_dev(&dev, OTYP_BLK, FREAD, kcred, &lhandle,
4082047ba61eSachartre 		    vd->vds->ldi_ident);
4083047ba61eSachartre 	}
4084047ba61eSachartre 
4085047ba61eSachartre 	if (status != 0) {
4086047ba61eSachartre 		PR0("ldi_open() returned errno %d for device %s",
4087047ba61eSachartre 		    status, (dev_path[0] == '\0')? file_path : dev_path);
40883c96341aSnarayan 	} else {
40893c96341aSnarayan 		if ((status = ldi_ioctl(lhandle, DKIOCINFO,
4090047ba61eSachartre 		    (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred,
40913c96341aSnarayan 		    &rval)) != 0) {
40923c96341aSnarayan 			PR0("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
40933c96341aSnarayan 			    status, dev_path);
40943c96341aSnarayan 		} else {
40953c96341aSnarayan 			/*
40963c96341aSnarayan 			 * Store the device's max transfer size for
40973c96341aSnarayan 			 * return to the client
40983c96341aSnarayan 			 */
40993c96341aSnarayan 			vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
41003c96341aSnarayan 		}
41013c96341aSnarayan 
41023c96341aSnarayan 		PR0("close the device %s", dev_path);
41033c96341aSnarayan 		(void) ldi_close(lhandle, FREAD, kcred);
41043c96341aSnarayan 	}
41053c96341aSnarayan 
4106205eeb1aSlm66018 	PR0("using file %s, dev %s, max_xfer = %u blks",
41073c96341aSnarayan 	    file_path, dev_path, vd->max_xfer_sz);
41083c96341aSnarayan 
410987a7269eSachartre 	/* Setup devid for the disk image */
411087a7269eSachartre 
4111047ba61eSachartre 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE)
4112047ba61eSachartre 		return (0);
4113047ba61eSachartre 
411478fcd0a1Sachartre 	if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
411578fcd0a1Sachartre 
411687a7269eSachartre 		status = vd_file_read_devid(vd, &vd->file_devid);
411787a7269eSachartre 
411887a7269eSachartre 		if (status == 0) {
411987a7269eSachartre 			/* a valid devid was found */
412087a7269eSachartre 			return (0);
412187a7269eSachartre 		}
412287a7269eSachartre 
412387a7269eSachartre 		if (status != EINVAL) {
412487a7269eSachartre 			/*
412578fcd0a1Sachartre 			 * There was an error while trying to read the devid.
412678fcd0a1Sachartre 			 * So this disk image may have a devid but we are
412778fcd0a1Sachartre 			 * unable to read it.
412887a7269eSachartre 			 */
412987a7269eSachartre 			PR0("can not read devid for %s", file_path);
413087a7269eSachartre 			vd->file_devid = NULL;
413187a7269eSachartre 			return (0);
413287a7269eSachartre 		}
413378fcd0a1Sachartre 	}
413487a7269eSachartre 
413587a7269eSachartre 	/*
413687a7269eSachartre 	 * No valid device id was found so we create one. Note that a failure
413787a7269eSachartre 	 * to create a device id is not fatal and does not prevent the disk
413887a7269eSachartre 	 * image from being attached.
413987a7269eSachartre 	 */
414087a7269eSachartre 	PR1("creating devid for %s", file_path);
414187a7269eSachartre 
414287a7269eSachartre 	if (ddi_devid_init(vd->vds->dip, DEVID_FAB, NULL, 0,
414387a7269eSachartre 	    &vd->file_devid) != DDI_SUCCESS) {
414487a7269eSachartre 		PR0("fail to create devid for %s", file_path);
414587a7269eSachartre 		vd->file_devid = NULL;
414687a7269eSachartre 		return (0);
414787a7269eSachartre 	}
414887a7269eSachartre 
414978fcd0a1Sachartre 	/*
415078fcd0a1Sachartre 	 * Write devid to the disk image. The devid is stored into the disk
415178fcd0a1Sachartre 	 * image if we have a valid label; otherwise the devid will be stored
415278fcd0a1Sachartre 	 * when the user writes a valid label.
415378fcd0a1Sachartre 	 */
415478fcd0a1Sachartre 	if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
415587a7269eSachartre 		if (vd_file_write_devid(vd, vd->file_devid) != 0) {
415687a7269eSachartre 			PR0("fail to write devid for %s", file_path);
415787a7269eSachartre 			ddi_devid_free(vd->file_devid);
415887a7269eSachartre 			vd->file_devid = NULL;
415987a7269eSachartre 		}
416078fcd0a1Sachartre 	}
416187a7269eSachartre 
41623c96341aSnarayan 	return (0);
41633c96341aSnarayan }
41643c96341aSnarayan 
4165*17cadca8Slm66018 
4166*17cadca8Slm66018 /*
4167*17cadca8Slm66018  * Description:
4168*17cadca8Slm66018  *	Open a device using its device path (supplied by ldm(1m))
4169*17cadca8Slm66018  *
4170*17cadca8Slm66018  * Parameters:
4171*17cadca8Slm66018  *	vd 	- pointer to structure containing the vDisk info
4172*17cadca8Slm66018  *
4173*17cadca8Slm66018  * Return Value
4174*17cadca8Slm66018  *	0	- success
4175*17cadca8Slm66018  *	EIO	- Invalid number of partitions
4176*17cadca8Slm66018  *	!= 0	- some other non-zero return value from ldi(9F) functions
4177*17cadca8Slm66018  */
4178*17cadca8Slm66018 static int
4179*17cadca8Slm66018 vd_open_using_ldi_by_name(vd_t *vd)
4180*17cadca8Slm66018 {
4181*17cadca8Slm66018 	int		rval, status, open_flags;
4182*17cadca8Slm66018 	struct dk_cinfo	dk_cinfo;
4183*17cadca8Slm66018 	char		*device_path = vd->device_path;
4184*17cadca8Slm66018 
4185*17cadca8Slm66018 	/*
4186*17cadca8Slm66018 	 * Try to open the device. If the flags indicate that the device should
4187*17cadca8Slm66018 	 * be opened write-enabled, we first we try to open it "read-only"
4188*17cadca8Slm66018 	 * to see if we have an optical device such as a CD-ROM which, for
4189*17cadca8Slm66018 	 * now, we do not permit writes to and thus should not export write
4190*17cadca8Slm66018 	 * operations to the client.
4191*17cadca8Slm66018 	 *
4192*17cadca8Slm66018 	 * Future: if/when we implement support for guest domains writing to
4193*17cadca8Slm66018 	 * optical devices we will need to do further checking of the media type
4194*17cadca8Slm66018 	 * to distinguish between read-only and writable discs.
4195*17cadca8Slm66018 	 */
4196*17cadca8Slm66018 	if (vd->open_flags & FWRITE) {
4197*17cadca8Slm66018 		open_flags = vd->open_flags & ~FWRITE;
4198*17cadca8Slm66018 		status = ldi_open_by_name(device_path, open_flags, kcred,
4199*17cadca8Slm66018 		    &vd->ldi_handle[0], vd->vds->ldi_ident);
4200*17cadca8Slm66018 
4201*17cadca8Slm66018 		if (status == 0) {
4202*17cadca8Slm66018 			/* Verify backing device supports dk_cinfo */
4203*17cadca8Slm66018 			status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO,
4204*17cadca8Slm66018 			    (intptr_t)&dk_cinfo, (open_flags | FKIOCTL),
4205*17cadca8Slm66018 			    kcred, &rval);
4206*17cadca8Slm66018 			if (status != 0) {
4207*17cadca8Slm66018 				PRN("ldi_ioctl(DKIOCINFO) returned errno %d for"
4208*17cadca8Slm66018 				    " %s opened as RO", status, device_path);
4209*17cadca8Slm66018 				return (status);
4210*17cadca8Slm66018 			}
4211*17cadca8Slm66018 
4212*17cadca8Slm66018 			if (dk_cinfo.dki_partition >= V_NUMPAR) {
4213*17cadca8Slm66018 				PRN("slice %u >= maximum slice %u for %s",
4214*17cadca8Slm66018 				    dk_cinfo.dki_partition, V_NUMPAR,
4215*17cadca8Slm66018 				    device_path);
4216*17cadca8Slm66018 				return (EIO);
4217*17cadca8Slm66018 			}
4218*17cadca8Slm66018 
4219*17cadca8Slm66018 			/*
4220*17cadca8Slm66018 			 * If this is an optical device then we disable
4221*17cadca8Slm66018 			 * write access and return, otherwise we close
4222*17cadca8Slm66018 			 * the device and try again with writes enabled.
4223*17cadca8Slm66018 			 */
4224*17cadca8Slm66018 			if (dk_cinfo.dki_ctype == DKC_CDROM) {
4225*17cadca8Slm66018 				vd->open_flags = open_flags;
4226*17cadca8Slm66018 				return (0);
4227*17cadca8Slm66018 			} else {
4228*17cadca8Slm66018 				(void) ldi_close(vd->ldi_handle[0],
4229*17cadca8Slm66018 				    open_flags, kcred);
4230*17cadca8Slm66018 			}
4231*17cadca8Slm66018 		}
4232*17cadca8Slm66018 	}
4233*17cadca8Slm66018 
4234*17cadca8Slm66018 	/* Attempt to (re)open device */
4235*17cadca8Slm66018 	status = ldi_open_by_name(device_path, open_flags, kcred,
4236*17cadca8Slm66018 	    &vd->ldi_handle[0], vd->vds->ldi_ident);
4237*17cadca8Slm66018 
4238*17cadca8Slm66018 	/*
4239*17cadca8Slm66018 	 * The open can fail for example if we are opening an empty slice.
4240*17cadca8Slm66018 	 * In case of a failure, we try the open again but this time with
4241*17cadca8Slm66018 	 * the FNDELAY flag.
4242*17cadca8Slm66018 	 */
4243*17cadca8Slm66018 	if (status != 0)
4244*17cadca8Slm66018 		status = ldi_open_by_name(device_path, vd->open_flags | FNDELAY,
4245*17cadca8Slm66018 		    kcred, &vd->ldi_handle[0], vd->vds->ldi_ident);
4246*17cadca8Slm66018 
4247*17cadca8Slm66018 	if (status != 0) {
4248*17cadca8Slm66018 		PR0("ldi_open_by_name(%s) = errno %d", device_path, status);
4249*17cadca8Slm66018 		vd->ldi_handle[0] = NULL;
4250*17cadca8Slm66018 		return (status);
4251*17cadca8Slm66018 	}
4252*17cadca8Slm66018 
4253*17cadca8Slm66018 	/* Verify backing device supports dk_cinfo */
4254*17cadca8Slm66018 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO,
4255*17cadca8Slm66018 	    (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred,
4256*17cadca8Slm66018 	    &rval)) != 0) {
4257*17cadca8Slm66018 		PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
4258*17cadca8Slm66018 		    status, device_path);
4259*17cadca8Slm66018 		return (status);
4260*17cadca8Slm66018 	}
4261*17cadca8Slm66018 	if (dk_cinfo.dki_partition >= V_NUMPAR) {
4262*17cadca8Slm66018 		PRN("slice %u >= maximum slice %u for %s",
4263*17cadca8Slm66018 		    dk_cinfo.dki_partition, V_NUMPAR, device_path);
4264*17cadca8Slm66018 		return (EIO);
4265*17cadca8Slm66018 	}
4266*17cadca8Slm66018 
4267*17cadca8Slm66018 	return (0);
4268*17cadca8Slm66018 }
4269*17cadca8Slm66018 
4270*17cadca8Slm66018 
4271047ba61eSachartre /*
4272047ba61eSachartre  * Setup for a virtual disk which backend is a device (a physical disk,
4273047ba61eSachartre  * slice or pseudo device) that is directly exported either as a full disk
4274047ba61eSachartre  * for a physical disk or as a slice for a pseudo device or a disk slice.
4275047ba61eSachartre  * In these cases, the backend is accessed using the LDI interface.
4276047ba61eSachartre  */
42773c96341aSnarayan static int
4278047ba61eSachartre vd_setup_backend_ldi(vd_t *vd)
42791ae08745Sheppo {
4280e1ebb9ecSlm66018 	int		rval, status;
42811ae08745Sheppo 	struct dk_cinfo	dk_cinfo;
42823c96341aSnarayan 	char		*device_path = vd->device_path;
42831ae08745Sheppo 
4284*17cadca8Slm66018 	status = vd_open_using_ldi_by_name(vd);
4285047ba61eSachartre 	if (status != 0) {
4286*17cadca8Slm66018 		PR0("Failed to open (%s) = errno %d", device_path, status);
42870a55fbb7Slm66018 		return (status);
42880a55fbb7Slm66018 	}
42890a55fbb7Slm66018 
42903c96341aSnarayan 	vd->file = B_FALSE;
42914bac2208Snarayan 
4292047ba61eSachartre 	/* Get device number of backing device */
42930a55fbb7Slm66018 	if ((status = ldi_get_dev(vd->ldi_handle[0], &vd->dev[0])) != 0) {
42941ae08745Sheppo 		PRN("ldi_get_dev() returned errno %d for %s",
4295e1ebb9ecSlm66018 		    status, device_path);
42961ae08745Sheppo 		return (status);
42971ae08745Sheppo 	}
42981ae08745Sheppo 
429978fcd0a1Sachartre 	/* Verify backing device supports dk_cinfo */
4300e1ebb9ecSlm66018 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO,
4301047ba61eSachartre 	    (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred,
4302e1ebb9ecSlm66018 	    &rval)) != 0) {
4303e1ebb9ecSlm66018 		PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
4304e1ebb9ecSlm66018 		    status, device_path);
4305e1ebb9ecSlm66018 		return (status);
4306e1ebb9ecSlm66018 	}
4307e1ebb9ecSlm66018 	if (dk_cinfo.dki_partition >= V_NUMPAR) {
4308e1ebb9ecSlm66018 		PRN("slice %u >= maximum slice %u for %s",
4309e1ebb9ecSlm66018 		    dk_cinfo.dki_partition, V_NUMPAR, device_path);
4310e1ebb9ecSlm66018 		return (EIO);
4311e1ebb9ecSlm66018 	}
43124bac2208Snarayan 
4313047ba61eSachartre 	vd->vdisk_label = vd_read_vtoc(vd, &vd->vtoc);
4314e1ebb9ecSlm66018 
4315e1ebb9ecSlm66018 	/* Store the device's max transfer size for return to the client */
4316e1ebb9ecSlm66018 	vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
4317e1ebb9ecSlm66018 
4318047ba61eSachartre 	/*
4319*17cadca8Slm66018 	 * We need to work out if it's an ATAPI (IDE CD-ROM) or SCSI device so
4320*17cadca8Slm66018 	 * that we can use the correct CDB group when sending USCSI commands.
4321*17cadca8Slm66018 	 */
4322*17cadca8Slm66018 	vd->is_atapi_dev = vd_is_atapi_device(vd);
4323*17cadca8Slm66018 
4324*17cadca8Slm66018 	/*
4325047ba61eSachartre 	 * Export a full disk.
4326047ba61eSachartre 	 *
4327047ba61eSachartre 	 * When we use the LDI interface, we export a device as a full disk
4328047ba61eSachartre 	 * if we have an entire disk slice (slice 2) and if this slice is
4329047ba61eSachartre 	 * exported as a full disk and not as a single slice disk.
4330*17cadca8Slm66018 	 * Similarly, we want to use LDI if we are accessing a CD or DVD
4331*17cadca8Slm66018 	 * device (even if it isn't s2)
4332047ba61eSachartre 	 *
4333047ba61eSachartre 	 * Note that pseudo devices are exported as full disks using the vnode
4334047ba61eSachartre 	 * interface, not the LDI interface.
4335047ba61eSachartre 	 */
4336*17cadca8Slm66018 	if ((dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE &&
4337*17cadca8Slm66018 	    vd->vdisk_type == VD_DISK_TYPE_DISK) ||
4338*17cadca8Slm66018 	    dk_cinfo.dki_ctype == DKC_CDROM) {
4339047ba61eSachartre 		ASSERT(!vd->pseudo);
4340047ba61eSachartre 		return (vd_setup_full_disk(vd));
4341047ba61eSachartre 	}
4342047ba61eSachartre 
4343047ba61eSachartre 	/*
4344047ba61eSachartre 	 * Export a single slice disk.
4345047ba61eSachartre 	 *
4346047ba61eSachartre 	 * The exported device can be either a pseudo device or a disk slice. If
4347047ba61eSachartre 	 * it is a disk slice different from slice 2 then it is always exported
4348047ba61eSachartre 	 * as a single slice disk even if the "slice" option is not specified.
4349047ba61eSachartre 	 * If it is disk slice 2 or a pseudo device then it is exported as a
4350047ba61eSachartre 	 * single slice disk only if the "slice" option is specified.
4351047ba61eSachartre 	 */
4352047ba61eSachartre 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE ||
4353047ba61eSachartre 	    dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE);
4354047ba61eSachartre 	return (vd_setup_single_slice_disk(vd));
4355047ba61eSachartre }
4356047ba61eSachartre 
4357047ba61eSachartre static int
4358047ba61eSachartre vd_setup_single_slice_disk(vd_t *vd)
4359047ba61eSachartre {
4360047ba61eSachartre 	int status;
4361047ba61eSachartre 	char *device_path = vd->device_path;
4362047ba61eSachartre 
4363047ba61eSachartre 	/* Get size of backing device */
4364047ba61eSachartre 	if (ldi_get_size(vd->ldi_handle[0], &vd->vdisk_size) != DDI_SUCCESS) {
4365047ba61eSachartre 		PRN("ldi_get_size() failed for %s", device_path);
43661ae08745Sheppo 		return (EIO);
43671ae08745Sheppo 	}
4368047ba61eSachartre 	vd->vdisk_size = lbtodb(vd->vdisk_size);	/* convert to blocks */
4369*17cadca8Slm66018 	vd->block_size = DEV_BSIZE;
4370*17cadca8Slm66018 	vd->vdisk_block_size = DEV_BSIZE;
4371*17cadca8Slm66018 	vd->vdisk_media = VD_MEDIA_FIXED;
4372047ba61eSachartre 
43731ae08745Sheppo 	if (vd->pseudo) {
4374047ba61eSachartre 
4375047ba61eSachartre 		ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
4376047ba61eSachartre 
437778fcd0a1Sachartre 		/*
437878fcd0a1Sachartre 		 * Currently we only support exporting pseudo devices which
437978fcd0a1Sachartre 		 * provide a valid disk label.
438078fcd0a1Sachartre 		 */
438178fcd0a1Sachartre 		if (vd->vdisk_label == VD_DISK_LABEL_UNK) {
438278fcd0a1Sachartre 			PRN("%s is a pseudo device with an invalid disk "
438378fcd0a1Sachartre 			    "label\n", device_path);
438478fcd0a1Sachartre 			return (EINVAL);
438578fcd0a1Sachartre 		}
43861ae08745Sheppo 		return (0);	/* ...and we're done */
43871ae08745Sheppo 	}
43881ae08745Sheppo 
438978fcd0a1Sachartre 	/* We can only export a slice if the disk has a valid label */
439078fcd0a1Sachartre 	if (vd->vdisk_label == VD_DISK_LABEL_UNK) {
439178fcd0a1Sachartre 		PRN("%s is a slice from a disk with an unknown disk label\n",
439278fcd0a1Sachartre 		    device_path);
439378fcd0a1Sachartre 		return (EINVAL);
439478fcd0a1Sachartre 	}
43950a55fbb7Slm66018 
4396047ba61eSachartre 	/*
4397047ba61eSachartre 	 * We export the slice as a single slice disk even if the "slice"
4398047ba61eSachartre 	 * option was not specified.
4399047ba61eSachartre 	 */
44001ae08745Sheppo 	vd->vdisk_type	= VD_DISK_TYPE_SLICE;
44011ae08745Sheppo 	vd->nslices	= 1;
44021ae08745Sheppo 
44034bac2208Snarayan 	if (vd->vdisk_label == VD_DISK_LABEL_EFI) {
440478fcd0a1Sachartre 		/* Slice from a disk with an EFI label */
44054bac2208Snarayan 		status = vd_setup_partition_efi(vd);
440678fcd0a1Sachartre 	} else {
440778fcd0a1Sachartre 		/* Slice from a disk with a VTOC label */
440878fcd0a1Sachartre 		ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
440978fcd0a1Sachartre 		status = vd_setup_partition_vtoc(vd);
441078fcd0a1Sachartre 	}
441178fcd0a1Sachartre 
44124bac2208Snarayan 	return (status);
44134bac2208Snarayan }
44141ae08745Sheppo 
44151ae08745Sheppo static int
4416047ba61eSachartre vd_setup_vd(vd_t *vd)
4417047ba61eSachartre {
4418047ba61eSachartre 	int		status;
4419047ba61eSachartre 	dev_info_t	*dip;
4420047ba61eSachartre 	vnode_t 	*vnp;
4421047ba61eSachartre 	char		*path = vd->device_path;
4422047ba61eSachartre 
4423047ba61eSachartre 	/* make sure the vdisk backend is valid */
4424047ba61eSachartre 	if ((status = lookupname(path, UIO_SYSSPACE,
4425047ba61eSachartre 	    FOLLOW, NULLVPP, &vnp)) != 0) {
4426047ba61eSachartre 		PR0("Cannot lookup %s errno %d", path, status);
4427047ba61eSachartre 		goto done;
4428047ba61eSachartre 	}
4429047ba61eSachartre 
4430047ba61eSachartre 	switch (vnp->v_type) {
4431047ba61eSachartre 	case VREG:
4432047ba61eSachartre 		/*
4433047ba61eSachartre 		 * Backend is a file so it is exported as a full disk or as a
4434047ba61eSachartre 		 * single slice disk using the vnode interface.
4435047ba61eSachartre 		 */
4436047ba61eSachartre 		VN_RELE(vnp);
4437047ba61eSachartre 		vd->pseudo = B_FALSE;
4438047ba61eSachartre 		status = vd_setup_backend_vnode(vd);
4439047ba61eSachartre 		break;
4440047ba61eSachartre 
4441047ba61eSachartre 	case VBLK:
4442047ba61eSachartre 	case VCHR:
4443047ba61eSachartre 		/*
4444047ba61eSachartre 		 * Backend is a device. The way it is exported depends on the
4445047ba61eSachartre 		 * type of the device.
4446047ba61eSachartre 		 *
4447047ba61eSachartre 		 * - A pseudo device is exported as a full disk using the vnode
4448047ba61eSachartre 		 *   interface or as a single slice disk using the LDI
4449047ba61eSachartre 		 *   interface.
4450047ba61eSachartre 		 *
4451047ba61eSachartre 		 * - A disk (represented by the slice 2 of that disk) is
4452047ba61eSachartre 		 *   exported as a full disk using the LDI interface.
4453047ba61eSachartre 		 *
4454047ba61eSachartre 		 * - A disk slice (different from slice 2) is always exported
4455047ba61eSachartre 		 *   as a single slice disk using the LDI interface.
4456047ba61eSachartre 		 *
4457047ba61eSachartre 		 * - The slice 2 of a disk is exported as a single slice disk
4458047ba61eSachartre 		 *   if the "slice" option is specified, otherwise the entire
4459047ba61eSachartre 		 *   disk will be exported. In any case, the LDI interface is
4460047ba61eSachartre 		 *   used.
4461047ba61eSachartre 		 */
4462047ba61eSachartre 
4463047ba61eSachartre 		/* check if this is a pseudo device */
4464047ba61eSachartre 		if ((dip = ddi_hold_devi_by_instance(getmajor(vnp->v_rdev),
4465047ba61eSachartre 		    dev_to_instance(vnp->v_rdev), 0))  == NULL) {
4466047ba61eSachartre 			PRN("%s is no longer accessible", path);
4467047ba61eSachartre 			VN_RELE(vnp);
4468047ba61eSachartre 			status = EIO;
4469047ba61eSachartre 			break;
4470047ba61eSachartre 		}
4471047ba61eSachartre 		vd->pseudo = is_pseudo_device(dip);
4472047ba61eSachartre 		ddi_release_devi(dip);
4473047ba61eSachartre 		VN_RELE(vnp);
4474047ba61eSachartre 
4475047ba61eSachartre 		/*
4476047ba61eSachartre 		 * If this is a pseudo device then its usage depends if the
4477047ba61eSachartre 		 * "slice" option is set or not. If the "slice" option is set
4478047ba61eSachartre 		 * then the pseudo device will be exported as a single slice,
4479047ba61eSachartre 		 * otherwise it will be exported as a full disk.
4480047ba61eSachartre 		 */
4481047ba61eSachartre 		if (vd->pseudo && vd->vdisk_type == VD_DISK_TYPE_DISK)
4482047ba61eSachartre 			status = vd_setup_backend_vnode(vd);
4483047ba61eSachartre 		else
4484047ba61eSachartre 			status = vd_setup_backend_ldi(vd);
4485047ba61eSachartre 		break;
4486047ba61eSachartre 
4487047ba61eSachartre 	default:
4488047ba61eSachartre 		PRN("Unsupported vdisk backend %s", path);
4489047ba61eSachartre 		VN_RELE(vnp);
4490047ba61eSachartre 		status = EBADF;
4491047ba61eSachartre 	}
4492047ba61eSachartre 
4493047ba61eSachartre done:
4494047ba61eSachartre 	if (status != 0) {
4495047ba61eSachartre 		/*
4496047ba61eSachartre 		 * If the error is retryable print an error message only
4497047ba61eSachartre 		 * during the first try.
4498047ba61eSachartre 		 */
4499047ba61eSachartre 		if (status == ENXIO || status == ENODEV ||
4500047ba61eSachartre 		    status == ENOENT || status == EROFS) {
4501047ba61eSachartre 			if (!(vd->initialized & VD_SETUP_ERROR)) {
4502047ba61eSachartre 				PRN("%s is currently inaccessible (error %d)",
4503047ba61eSachartre 				    path, status);
4504047ba61eSachartre 			}
4505047ba61eSachartre 			status = EAGAIN;
4506047ba61eSachartre 		} else {
4507047ba61eSachartre 			PRN("%s can not be exported as a virtual disk "
4508047ba61eSachartre 			    "(error %d)", path, status);
4509047ba61eSachartre 		}
4510047ba61eSachartre 		vd->initialized |= VD_SETUP_ERROR;
4511047ba61eSachartre 
4512047ba61eSachartre 	} else if (vd->initialized & VD_SETUP_ERROR) {
4513047ba61eSachartre 		/* print a message only if we previously had an error */
4514047ba61eSachartre 		PRN("%s is now online", path);
4515047ba61eSachartre 		vd->initialized &= ~VD_SETUP_ERROR;
4516047ba61eSachartre 	}
4517047ba61eSachartre 
4518047ba61eSachartre 	return (status);
4519047ba61eSachartre }
4520047ba61eSachartre 
4521047ba61eSachartre static int
4522047ba61eSachartre vds_do_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options,
4523047ba61eSachartre     uint64_t ldc_id, vd_t **vdp)
45241ae08745Sheppo {
45251ae08745Sheppo 	char			tq_name[TASKQ_NAMELEN];
45260a55fbb7Slm66018 	int			status;
45271ae08745Sheppo 	ddi_iblock_cookie_t	iblock = NULL;
45281ae08745Sheppo 	ldc_attr_t		ldc_attr;
45291ae08745Sheppo 	vd_t			*vd;
45301ae08745Sheppo 
45311ae08745Sheppo 
45321ae08745Sheppo 	ASSERT(vds != NULL);
4533e1ebb9ecSlm66018 	ASSERT(device_path != NULL);
45341ae08745Sheppo 	ASSERT(vdp != NULL);
4535e1ebb9ecSlm66018 	PR0("Adding vdisk for %s", device_path);
45361ae08745Sheppo 
45371ae08745Sheppo 	if ((vd = kmem_zalloc(sizeof (*vd), KM_NOSLEEP)) == NULL) {
45381ae08745Sheppo 		PRN("No memory for virtual disk");
45391ae08745Sheppo 		return (EAGAIN);
45401ae08745Sheppo 	}
45411ae08745Sheppo 	*vdp = vd;	/* assign here so vds_destroy_vd() can cleanup later */
45421ae08745Sheppo 	vd->vds = vds;
45433c96341aSnarayan 	(void) strncpy(vd->device_path, device_path, MAXPATHLEN);
45441ae08745Sheppo 
4545047ba61eSachartre 	/* Setup open flags */
4546047ba61eSachartre 	vd->open_flags = FREAD;
4547047ba61eSachartre 
4548047ba61eSachartre 	if (!(options & VD_OPT_RDONLY))
4549047ba61eSachartre 		vd->open_flags |= FWRITE;
4550047ba61eSachartre 
4551047ba61eSachartre 	if (options & VD_OPT_EXCLUSIVE)
4552047ba61eSachartre 		vd->open_flags |= FEXCL;
4553047ba61eSachartre 
4554047ba61eSachartre 	/* Setup disk type */
4555047ba61eSachartre 	if (options & VD_OPT_SLICE) {
4556047ba61eSachartre 		vd->vdisk_type = VD_DISK_TYPE_SLICE;
4557047ba61eSachartre 		vd->nslices = 1;
4558047ba61eSachartre 	} else {
4559047ba61eSachartre 		vd->vdisk_type = VD_DISK_TYPE_DISK;
4560047ba61eSachartre 		vd->nslices = V_NUMPAR;
4561047ba61eSachartre 	}
4562047ba61eSachartre 
4563047ba61eSachartre 	/* default disk label */
4564047ba61eSachartre 	vd->vdisk_label = VD_DISK_LABEL_UNK;
4565047ba61eSachartre 
45660a55fbb7Slm66018 	/* Open vdisk and initialize parameters */
45673c96341aSnarayan 	if ((status = vd_setup_vd(vd)) == 0) {
45683c96341aSnarayan 		vd->initialized |= VD_DISK_READY;
45691ae08745Sheppo 
45703c96341aSnarayan 		ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
45713c96341aSnarayan 		PR0("vdisk_type = %s, pseudo = %s, file = %s, nslices = %u",
45723c96341aSnarayan 		    ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
45733c96341aSnarayan 		    (vd->pseudo ? "yes" : "no"), (vd->file ? "yes" : "no"),
45743c96341aSnarayan 		    vd->nslices);
45753c96341aSnarayan 	} else {
45763c96341aSnarayan 		if (status != EAGAIN)
45773c96341aSnarayan 			return (status);
45783c96341aSnarayan 	}
45791ae08745Sheppo 
45801ae08745Sheppo 	/* Initialize locking */
45811ae08745Sheppo 	if (ddi_get_soft_iblock_cookie(vds->dip, DDI_SOFTINT_MED,
45821ae08745Sheppo 	    &iblock) != DDI_SUCCESS) {
45831ae08745Sheppo 		PRN("Could not get iblock cookie.");
45841ae08745Sheppo 		return (EIO);
45851ae08745Sheppo 	}
45861ae08745Sheppo 
45871ae08745Sheppo 	mutex_init(&vd->lock, NULL, MUTEX_DRIVER, iblock);
45881ae08745Sheppo 	vd->initialized |= VD_LOCKING;
45891ae08745Sheppo 
45901ae08745Sheppo 
4591d10e4ef2Snarayan 	/* Create start and completion task queues for the vdisk */
4592d10e4ef2Snarayan 	(void) snprintf(tq_name, sizeof (tq_name), "vd_startq%lu", id);
45931ae08745Sheppo 	PR1("tq_name = %s", tq_name);
4594d10e4ef2Snarayan 	if ((vd->startq = ddi_taskq_create(vds->dip, tq_name, 1,
45951ae08745Sheppo 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
45961ae08745Sheppo 		PRN("Could not create task queue");
45971ae08745Sheppo 		return (EIO);
45981ae08745Sheppo 	}
4599d10e4ef2Snarayan 	(void) snprintf(tq_name, sizeof (tq_name), "vd_completionq%lu", id);
4600d10e4ef2Snarayan 	PR1("tq_name = %s", tq_name);
4601d10e4ef2Snarayan 	if ((vd->completionq = ddi_taskq_create(vds->dip, tq_name, 1,
4602d10e4ef2Snarayan 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
4603d10e4ef2Snarayan 		PRN("Could not create task queue");
4604d10e4ef2Snarayan 		return (EIO);
4605d10e4ef2Snarayan 	}
4606d10e4ef2Snarayan 	vd->enabled = 1;	/* before callback can dispatch to startq */
46071ae08745Sheppo 
46081ae08745Sheppo 
46091ae08745Sheppo 	/* Bring up LDC */
46101ae08745Sheppo 	ldc_attr.devclass	= LDC_DEV_BLK_SVC;
46111ae08745Sheppo 	ldc_attr.instance	= ddi_get_instance(vds->dip);
46121ae08745Sheppo 	ldc_attr.mode		= LDC_MODE_UNRELIABLE;
4613e1ebb9ecSlm66018 	ldc_attr.mtu		= VD_LDC_MTU;
46141ae08745Sheppo 	if ((status = ldc_init(ldc_id, &ldc_attr, &vd->ldc_handle)) != 0) {
4615*17cadca8Slm66018 		PRN("Could not initialize LDC channel %lx, "
4616690555a1Sachartre 		    "init failed with error %d", ldc_id, status);
46171ae08745Sheppo 		return (status);
46181ae08745Sheppo 	}
46191ae08745Sheppo 	vd->initialized |= VD_LDC;
46201ae08745Sheppo 
46211ae08745Sheppo 	if ((status = ldc_reg_callback(vd->ldc_handle, vd_handle_ldc_events,
46221ae08745Sheppo 	    (caddr_t)vd)) != 0) {
4623690555a1Sachartre 		PRN("Could not initialize LDC channel %lu,"
4624690555a1Sachartre 		    "reg_callback failed with error %d", ldc_id, status);
46251ae08745Sheppo 		return (status);
46261ae08745Sheppo 	}
46271ae08745Sheppo 
46281ae08745Sheppo 	if ((status = ldc_open(vd->ldc_handle)) != 0) {
4629690555a1Sachartre 		PRN("Could not initialize LDC channel %lu,"
4630690555a1Sachartre 		    "open failed with error %d", ldc_id, status);
46311ae08745Sheppo 		return (status);
46321ae08745Sheppo 	}
46331ae08745Sheppo 
46343af08d82Slm66018 	if ((status = ldc_up(vd->ldc_handle)) != 0) {
463534683adeSsg70180 		PR0("ldc_up() returned errno %d", status);
46363af08d82Slm66018 	}
46373af08d82Slm66018 
46384bac2208Snarayan 	/* Allocate the inband task memory handle */
46394bac2208Snarayan 	status = ldc_mem_alloc_handle(vd->ldc_handle, &(vd->inband_task.mhdl));
46404bac2208Snarayan 	if (status) {
4641690555a1Sachartre 		PRN("Could not initialize LDC channel %lu,"
4642690555a1Sachartre 		    "alloc_handle failed with error %d", ldc_id, status);
46434bac2208Snarayan 		return (ENXIO);
46444bac2208Snarayan 	}
46451ae08745Sheppo 
46461ae08745Sheppo 	/* Add the successfully-initialized vdisk to the server's table */
46471ae08745Sheppo 	if (mod_hash_insert(vds->vd_table, (mod_hash_key_t)id, vd) != 0) {
46481ae08745Sheppo 		PRN("Error adding vdisk ID %lu to table", id);
46491ae08745Sheppo 		return (EIO);
46501ae08745Sheppo 	}
46511ae08745Sheppo 
46523af08d82Slm66018 	/* Allocate the staging buffer */
46533af08d82Slm66018 	vd->max_msglen	= sizeof (vio_msg_t);	/* baseline vio message size */
46543af08d82Slm66018 	vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
46553af08d82Slm66018 
46563af08d82Slm66018 	/* store initial state */
46573af08d82Slm66018 	vd->state = VD_STATE_INIT;
46583af08d82Slm66018 
46591ae08745Sheppo 	return (0);
46601ae08745Sheppo }
46611ae08745Sheppo 
46623af08d82Slm66018 static void
46633af08d82Slm66018 vd_free_dring_task(vd_t *vdp)
46643af08d82Slm66018 {
46653af08d82Slm66018 	if (vdp->dring_task != NULL) {
46663af08d82Slm66018 		ASSERT(vdp->dring_len != 0);
46673af08d82Slm66018 		/* Free all dring_task memory handles */
46683af08d82Slm66018 		for (int i = 0; i < vdp->dring_len; i++) {
46693af08d82Slm66018 			(void) ldc_mem_free_handle(vdp->dring_task[i].mhdl);
46703af08d82Slm66018 			kmem_free(vdp->dring_task[i].msg, vdp->max_msglen);
46713af08d82Slm66018 			vdp->dring_task[i].msg = NULL;
46723af08d82Slm66018 		}
46733af08d82Slm66018 		kmem_free(vdp->dring_task,
46743af08d82Slm66018 		    (sizeof (*vdp->dring_task)) * vdp->dring_len);
46753af08d82Slm66018 		vdp->dring_task = NULL;
46763af08d82Slm66018 	}
46773af08d82Slm66018 }
46783af08d82Slm66018 
46791ae08745Sheppo /*
46801ae08745Sheppo  * Destroy the state associated with a virtual disk
46811ae08745Sheppo  */
46821ae08745Sheppo static void
46831ae08745Sheppo vds_destroy_vd(void *arg)
46841ae08745Sheppo {
46851ae08745Sheppo 	vd_t	*vd = (vd_t *)arg;
468634683adeSsg70180 	int	retry = 0, rv;
46871ae08745Sheppo 
46881ae08745Sheppo 	if (vd == NULL)
46891ae08745Sheppo 		return;
46901ae08745Sheppo 
4691d10e4ef2Snarayan 	PR0("Destroying vdisk state");
4692d10e4ef2Snarayan 
46934bac2208Snarayan 	if (vd->dk_efi.dki_data != NULL)
46944bac2208Snarayan 		kmem_free(vd->dk_efi.dki_data, vd->dk_efi.dki_length);
46954bac2208Snarayan 
46961ae08745Sheppo 	/* Disable queuing requests for the vdisk */
46971ae08745Sheppo 	if (vd->initialized & VD_LOCKING) {
46981ae08745Sheppo 		mutex_enter(&vd->lock);
46991ae08745Sheppo 		vd->enabled = 0;
47001ae08745Sheppo 		mutex_exit(&vd->lock);
47011ae08745Sheppo 	}
47021ae08745Sheppo 
4703d10e4ef2Snarayan 	/* Drain and destroy start queue (*before* destroying completionq) */
4704d10e4ef2Snarayan 	if (vd->startq != NULL)
4705d10e4ef2Snarayan 		ddi_taskq_destroy(vd->startq);	/* waits for queued tasks */
4706d10e4ef2Snarayan 
4707d10e4ef2Snarayan 	/* Drain and destroy completion queue (*before* shutting down LDC) */
4708d10e4ef2Snarayan 	if (vd->completionq != NULL)
4709d10e4ef2Snarayan 		ddi_taskq_destroy(vd->completionq);	/* waits for tasks */
4710d10e4ef2Snarayan 
47113af08d82Slm66018 	vd_free_dring_task(vd);
47123af08d82Slm66018 
471334683adeSsg70180 	/* Free the inband task memory handle */
471434683adeSsg70180 	(void) ldc_mem_free_handle(vd->inband_task.mhdl);
471534683adeSsg70180 
471634683adeSsg70180 	/* Shut down LDC */
471734683adeSsg70180 	if (vd->initialized & VD_LDC) {
471834683adeSsg70180 		/* unmap the dring */
471934683adeSsg70180 		if (vd->initialized & VD_DRING)
472034683adeSsg70180 			(void) ldc_mem_dring_unmap(vd->dring_handle);
472134683adeSsg70180 
472234683adeSsg70180 		/* close LDC channel - retry on EAGAIN */
472334683adeSsg70180 		while ((rv = ldc_close(vd->ldc_handle)) == EAGAIN) {
472434683adeSsg70180 			if (++retry > vds_ldc_retries) {
472534683adeSsg70180 				PR0("Timed out closing channel");
472634683adeSsg70180 				break;
472734683adeSsg70180 			}
472834683adeSsg70180 			drv_usecwait(vds_ldc_delay);
472934683adeSsg70180 		}
473034683adeSsg70180 		if (rv == 0) {
473134683adeSsg70180 			(void) ldc_unreg_callback(vd->ldc_handle);
473234683adeSsg70180 			(void) ldc_fini(vd->ldc_handle);
473334683adeSsg70180 		} else {
473434683adeSsg70180 			/*
473534683adeSsg70180 			 * Closing the LDC channel has failed. Ideally we should
473634683adeSsg70180 			 * fail here but there is no Zeus level infrastructure
473734683adeSsg70180 			 * to handle this. The MD has already been changed and
473834683adeSsg70180 			 * we have to do the close. So we try to do as much
473934683adeSsg70180 			 * clean up as we can.
474034683adeSsg70180 			 */
474134683adeSsg70180 			(void) ldc_set_cb_mode(vd->ldc_handle, LDC_CB_DISABLE);
474234683adeSsg70180 			while (ldc_unreg_callback(vd->ldc_handle) == EAGAIN)
474334683adeSsg70180 				drv_usecwait(vds_ldc_delay);
474434683adeSsg70180 		}
474534683adeSsg70180 	}
474634683adeSsg70180 
47473af08d82Slm66018 	/* Free the staging buffer for msgs */
47483af08d82Slm66018 	if (vd->vio_msgp != NULL) {
47493af08d82Slm66018 		kmem_free(vd->vio_msgp, vd->max_msglen);
47503af08d82Slm66018 		vd->vio_msgp = NULL;
47513af08d82Slm66018 	}
47523af08d82Slm66018 
47533af08d82Slm66018 	/* Free the inband message buffer */
47543af08d82Slm66018 	if (vd->inband_task.msg != NULL) {
47553af08d82Slm66018 		kmem_free(vd->inband_task.msg, vd->max_msglen);
47563af08d82Slm66018 		vd->inband_task.msg = NULL;
4757d10e4ef2Snarayan 	}
4758da6c28aaSamw 
47593c96341aSnarayan 	if (vd->file) {
4760690555a1Sachartre 		/* Close file */
4761047ba61eSachartre 		(void) VOP_CLOSE(vd->file_vnode, vd->open_flags, 1,
4762da6c28aaSamw 		    0, kcred, NULL);
47633c96341aSnarayan 		VN_RELE(vd->file_vnode);
476487a7269eSachartre 		if (vd->file_devid != NULL)
476587a7269eSachartre 			ddi_devid_free(vd->file_devid);
47663c96341aSnarayan 	} else {
47671ae08745Sheppo 		/* Close any open backing-device slices */
47681ae08745Sheppo 		for (uint_t slice = 0; slice < vd->nslices; slice++) {
47691ae08745Sheppo 			if (vd->ldi_handle[slice] != NULL) {
47701ae08745Sheppo 				PR0("Closing slice %u", slice);
47711ae08745Sheppo 				(void) ldi_close(vd->ldi_handle[slice],
4772047ba61eSachartre 				    vd->open_flags, kcred);
47731ae08745Sheppo 			}
47741ae08745Sheppo 		}
47753c96341aSnarayan 	}
47761ae08745Sheppo 
47771ae08745Sheppo 	/* Free lock */
47781ae08745Sheppo 	if (vd->initialized & VD_LOCKING)
47791ae08745Sheppo 		mutex_destroy(&vd->lock);
47801ae08745Sheppo 
47811ae08745Sheppo 	/* Finally, free the vdisk structure itself */
47821ae08745Sheppo 	kmem_free(vd, sizeof (*vd));
47831ae08745Sheppo }
47841ae08745Sheppo 
47851ae08745Sheppo static int
4786047ba61eSachartre vds_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options,
4787047ba61eSachartre     uint64_t ldc_id)
47881ae08745Sheppo {
47891ae08745Sheppo 	int	status;
47901ae08745Sheppo 	vd_t	*vd = NULL;
47911ae08745Sheppo 
47921ae08745Sheppo 
4793047ba61eSachartre 	if ((status = vds_do_init_vd(vds, id, device_path, options,
4794047ba61eSachartre 	    ldc_id, &vd)) != 0)
47951ae08745Sheppo 		vds_destroy_vd(vd);
47961ae08745Sheppo 
47971ae08745Sheppo 	return (status);
47981ae08745Sheppo }
47991ae08745Sheppo 
48001ae08745Sheppo static int
48011ae08745Sheppo vds_do_get_ldc_id(md_t *md, mde_cookie_t vd_node, mde_cookie_t *channel,
48021ae08745Sheppo     uint64_t *ldc_id)
48031ae08745Sheppo {
48041ae08745Sheppo 	int	num_channels;
48051ae08745Sheppo 
48061ae08745Sheppo 
48071ae08745Sheppo 	/* Look for channel endpoint child(ren) of the vdisk MD node */
48081ae08745Sheppo 	if ((num_channels = md_scan_dag(md, vd_node,
48091ae08745Sheppo 	    md_find_name(md, VD_CHANNEL_ENDPOINT),
48101ae08745Sheppo 	    md_find_name(md, "fwd"), channel)) <= 0) {
48111ae08745Sheppo 		PRN("No \"%s\" found for virtual disk", VD_CHANNEL_ENDPOINT);
48121ae08745Sheppo 		return (-1);
48131ae08745Sheppo 	}
48141ae08745Sheppo 
48151ae08745Sheppo 	/* Get the "id" value for the first channel endpoint node */
48161ae08745Sheppo 	if (md_get_prop_val(md, channel[0], VD_ID_PROP, ldc_id) != 0) {
48171ae08745Sheppo 		PRN("No \"%s\" property found for \"%s\" of vdisk",
48181ae08745Sheppo 		    VD_ID_PROP, VD_CHANNEL_ENDPOINT);
48191ae08745Sheppo 		return (-1);
48201ae08745Sheppo 	}
48211ae08745Sheppo 
48221ae08745Sheppo 	if (num_channels > 1) {
48231ae08745Sheppo 		PRN("Using ID of first of multiple channels for this vdisk");
48241ae08745Sheppo 	}
48251ae08745Sheppo 
48261ae08745Sheppo 	return (0);
48271ae08745Sheppo }
48281ae08745Sheppo 
48291ae08745Sheppo static int
48301ae08745Sheppo vds_get_ldc_id(md_t *md, mde_cookie_t vd_node, uint64_t *ldc_id)
48311ae08745Sheppo {
48321ae08745Sheppo 	int		num_nodes, status;
48331ae08745Sheppo 	size_t		size;
48341ae08745Sheppo 	mde_cookie_t	*channel;
48351ae08745Sheppo 
48361ae08745Sheppo 
48371ae08745Sheppo 	if ((num_nodes = md_node_count(md)) <= 0) {
48381ae08745Sheppo 		PRN("Invalid node count in Machine Description subtree");
48391ae08745Sheppo 		return (-1);
48401ae08745Sheppo 	}
48411ae08745Sheppo 	size = num_nodes*(sizeof (*channel));
48421ae08745Sheppo 	channel = kmem_zalloc(size, KM_SLEEP);
48431ae08745Sheppo 	status = vds_do_get_ldc_id(md, vd_node, channel, ldc_id);
48441ae08745Sheppo 	kmem_free(channel, size);
48451ae08745Sheppo 
48461ae08745Sheppo 	return (status);
48471ae08745Sheppo }
48481ae08745Sheppo 
4849047ba61eSachartre /*
4850047ba61eSachartre  * Function:
4851047ba61eSachartre  *	vds_get_options
4852047ba61eSachartre  *
4853047ba61eSachartre  * Description:
4854047ba61eSachartre  * 	Parse the options of a vds node. Options are defined as an array
4855047ba61eSachartre  *	of strings in the vds-block-device-opts property of the vds node
4856047ba61eSachartre  *	in the machine description. Options are returned as a bitmask. The
4857047ba61eSachartre  *	mapping between the bitmask options and the options strings from the
4858047ba61eSachartre  *	machine description is defined in the vd_bdev_options[] array.
4859047ba61eSachartre  *
4860047ba61eSachartre  *	The vds-block-device-opts property is optional. If a vds has no such
4861047ba61eSachartre  *	property then no option is defined.
4862047ba61eSachartre  *
4863047ba61eSachartre  * Parameters:
4864047ba61eSachartre  *	md		- machine description.
4865047ba61eSachartre  *	vd_node		- vds node in the machine description for which
4866047ba61eSachartre  *			  options have to be parsed.
4867047ba61eSachartre  *	options		- the returned options.
4868047ba61eSachartre  *
4869047ba61eSachartre  * Return Code:
4870047ba61eSachartre  *	none.
4871047ba61eSachartre  */
4872047ba61eSachartre static void
4873047ba61eSachartre vds_get_options(md_t *md, mde_cookie_t vd_node, uint64_t *options)
4874047ba61eSachartre {
4875047ba61eSachartre 	char	*optstr, *opt;
4876047ba61eSachartre 	int	len, n, i;
4877047ba61eSachartre 
4878047ba61eSachartre 	*options = 0;
4879047ba61eSachartre 
4880047ba61eSachartre 	if (md_get_prop_data(md, vd_node, VD_BLOCK_DEVICE_OPTS,
4881047ba61eSachartre 	    (uint8_t **)&optstr, &len) != 0) {
4882047ba61eSachartre 		PR0("No options found");
4883047ba61eSachartre 		return;
4884047ba61eSachartre 	}
4885047ba61eSachartre 
4886047ba61eSachartre 	/* parse options */
4887047ba61eSachartre 	opt = optstr;
4888047ba61eSachartre 	n = sizeof (vd_bdev_options) / sizeof (vd_option_t);
4889047ba61eSachartre 
4890047ba61eSachartre 	while (opt < optstr + len) {
4891047ba61eSachartre 		for (i = 0; i < n; i++) {
4892047ba61eSachartre 			if (strncmp(vd_bdev_options[i].vdo_name,
4893047ba61eSachartre 			    opt, VD_OPTION_NLEN) == 0) {
4894047ba61eSachartre 				*options |= vd_bdev_options[i].vdo_value;
4895047ba61eSachartre 				break;
4896047ba61eSachartre 			}
4897047ba61eSachartre 		}
4898047ba61eSachartre 
4899047ba61eSachartre 		if (i < n) {
4900047ba61eSachartre 			PR0("option: %s", opt);
4901047ba61eSachartre 		} else {
4902047ba61eSachartre 			PRN("option %s is unknown or unsupported", opt);
4903047ba61eSachartre 		}
4904047ba61eSachartre 
4905047ba61eSachartre 		opt += strlen(opt) + 1;
4906047ba61eSachartre 	}
4907047ba61eSachartre }
4908047ba61eSachartre 
49091ae08745Sheppo static void
49101ae08745Sheppo vds_add_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
49111ae08745Sheppo {
4912e1ebb9ecSlm66018 	char		*device_path = NULL;
4913047ba61eSachartre 	uint64_t	id = 0, ldc_id = 0, options = 0;
49141ae08745Sheppo 
49151ae08745Sheppo 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
49161ae08745Sheppo 		PRN("Error getting vdisk \"%s\"", VD_ID_PROP);
49171ae08745Sheppo 		return;
49181ae08745Sheppo 	}
49191ae08745Sheppo 	PR0("Adding vdisk ID %lu", id);
49201ae08745Sheppo 	if (md_get_prop_str(md, vd_node, VD_BLOCK_DEVICE_PROP,
4921e1ebb9ecSlm66018 	    &device_path) != 0) {
49221ae08745Sheppo 		PRN("Error getting vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
49231ae08745Sheppo 		return;
49241ae08745Sheppo 	}
49251ae08745Sheppo 
4926047ba61eSachartre 	vds_get_options(md, vd_node, &options);
4927047ba61eSachartre 
49281ae08745Sheppo 	if (vds_get_ldc_id(md, vd_node, &ldc_id) != 0) {
49291ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", id);
49301ae08745Sheppo 		return;
49311ae08745Sheppo 	}
49321ae08745Sheppo 
4933047ba61eSachartre 	if (vds_init_vd(vds, id, device_path, options, ldc_id) != 0) {
49341ae08745Sheppo 		PRN("Failed to add vdisk ID %lu", id);
4935*17cadca8Slm66018 		if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0)
4936*17cadca8Slm66018 			PRN("No vDisk entry found for vdisk ID %lu", id);
49371ae08745Sheppo 		return;
49381ae08745Sheppo 	}
49391ae08745Sheppo }
49401ae08745Sheppo 
49411ae08745Sheppo static void
49421ae08745Sheppo vds_remove_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
49431ae08745Sheppo {
49441ae08745Sheppo 	uint64_t	id = 0;
49451ae08745Sheppo 
49461ae08745Sheppo 
49471ae08745Sheppo 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
49481ae08745Sheppo 		PRN("Unable to get \"%s\" property from vdisk's MD node",
49491ae08745Sheppo 		    VD_ID_PROP);
49501ae08745Sheppo 		return;
49511ae08745Sheppo 	}
49521ae08745Sheppo 	PR0("Removing vdisk ID %lu", id);
49531ae08745Sheppo 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0)
49541ae08745Sheppo 		PRN("No vdisk entry found for vdisk ID %lu", id);
49551ae08745Sheppo }
49561ae08745Sheppo 
49571ae08745Sheppo static void
49581ae08745Sheppo vds_change_vd(vds_t *vds, md_t *prev_md, mde_cookie_t prev_vd_node,
49591ae08745Sheppo     md_t *curr_md, mde_cookie_t curr_vd_node)
49601ae08745Sheppo {
49611ae08745Sheppo 	char		*curr_dev, *prev_dev;
4962047ba61eSachartre 	uint64_t	curr_id = 0, curr_ldc_id = 0, curr_options = 0;
4963047ba61eSachartre 	uint64_t	prev_id = 0, prev_ldc_id = 0, prev_options = 0;
49641ae08745Sheppo 	size_t		len;
49651ae08745Sheppo 
49661ae08745Sheppo 
49671ae08745Sheppo 	/* Validate that vdisk ID has not changed */
49681ae08745Sheppo 	if (md_get_prop_val(prev_md, prev_vd_node, VD_ID_PROP, &prev_id) != 0) {
49691ae08745Sheppo 		PRN("Error getting previous vdisk \"%s\" property",
49701ae08745Sheppo 		    VD_ID_PROP);
49711ae08745Sheppo 		return;
49721ae08745Sheppo 	}
49731ae08745Sheppo 	if (md_get_prop_val(curr_md, curr_vd_node, VD_ID_PROP, &curr_id) != 0) {
49741ae08745Sheppo 		PRN("Error getting current vdisk \"%s\" property", VD_ID_PROP);
49751ae08745Sheppo 		return;
49761ae08745Sheppo 	}
49771ae08745Sheppo 	if (curr_id != prev_id) {
49781ae08745Sheppo 		PRN("Not changing vdisk:  ID changed from %lu to %lu",
49791ae08745Sheppo 		    prev_id, curr_id);
49801ae08745Sheppo 		return;
49811ae08745Sheppo 	}
49821ae08745Sheppo 
49831ae08745Sheppo 	/* Validate that LDC ID has not changed */
49841ae08745Sheppo 	if (vds_get_ldc_id(prev_md, prev_vd_node, &prev_ldc_id) != 0) {
49851ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", prev_id);
49861ae08745Sheppo 		return;
49871ae08745Sheppo 	}
49881ae08745Sheppo 
49891ae08745Sheppo 	if (vds_get_ldc_id(curr_md, curr_vd_node, &curr_ldc_id) != 0) {
49901ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", curr_id);
49911ae08745Sheppo 		return;
49921ae08745Sheppo 	}
49931ae08745Sheppo 	if (curr_ldc_id != prev_ldc_id) {
49940a55fbb7Slm66018 		_NOTE(NOTREACHED);	/* lint is confused */
49951ae08745Sheppo 		PRN("Not changing vdisk:  "
49961ae08745Sheppo 		    "LDC ID changed from %lu to %lu", prev_ldc_id, curr_ldc_id);
49971ae08745Sheppo 		return;
49981ae08745Sheppo 	}
49991ae08745Sheppo 
50001ae08745Sheppo 	/* Determine whether device path has changed */
50011ae08745Sheppo 	if (md_get_prop_str(prev_md, prev_vd_node, VD_BLOCK_DEVICE_PROP,
50021ae08745Sheppo 	    &prev_dev) != 0) {
50031ae08745Sheppo 		PRN("Error getting previous vdisk \"%s\"",
50041ae08745Sheppo 		    VD_BLOCK_DEVICE_PROP);
50051ae08745Sheppo 		return;
50061ae08745Sheppo 	}
50071ae08745Sheppo 	if (md_get_prop_str(curr_md, curr_vd_node, VD_BLOCK_DEVICE_PROP,
50081ae08745Sheppo 	    &curr_dev) != 0) {
50091ae08745Sheppo 		PRN("Error getting current vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
50101ae08745Sheppo 		return;
50111ae08745Sheppo 	}
50121ae08745Sheppo 	if (((len = strlen(curr_dev)) == strlen(prev_dev)) &&
50131ae08745Sheppo 	    (strncmp(curr_dev, prev_dev, len) == 0))
50141ae08745Sheppo 		return;	/* no relevant (supported) change */
50151ae08745Sheppo 
5016047ba61eSachartre 	/* Validate that options have not changed */
5017047ba61eSachartre 	vds_get_options(prev_md, prev_vd_node, &prev_options);
5018047ba61eSachartre 	vds_get_options(curr_md, curr_vd_node, &curr_options);
5019047ba61eSachartre 	if (prev_options != curr_options) {
5020047ba61eSachartre 		PRN("Not changing vdisk:  options changed from %lx to %lx",
5021047ba61eSachartre 		    prev_options, curr_options);
5022047ba61eSachartre 		return;
5023047ba61eSachartre 	}
5024047ba61eSachartre 
50251ae08745Sheppo 	PR0("Changing vdisk ID %lu", prev_id);
50263af08d82Slm66018 
50271ae08745Sheppo 	/* Remove old state, which will close vdisk and reset */
50281ae08745Sheppo 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)prev_id) != 0)
50291ae08745Sheppo 		PRN("No entry found for vdisk ID %lu", prev_id);
50303af08d82Slm66018 
50311ae08745Sheppo 	/* Re-initialize vdisk with new state */
5032047ba61eSachartre 	if (vds_init_vd(vds, curr_id, curr_dev, curr_options,
5033047ba61eSachartre 	    curr_ldc_id) != 0) {
50341ae08745Sheppo 		PRN("Failed to change vdisk ID %lu", curr_id);
50351ae08745Sheppo 		return;
50361ae08745Sheppo 	}
50371ae08745Sheppo }
50381ae08745Sheppo 
50391ae08745Sheppo static int
50401ae08745Sheppo vds_process_md(void *arg, mdeg_result_t *md)
50411ae08745Sheppo {
50421ae08745Sheppo 	int	i;
50431ae08745Sheppo 	vds_t	*vds = arg;
50441ae08745Sheppo 
50451ae08745Sheppo 
50461ae08745Sheppo 	if (md == NULL)
50471ae08745Sheppo 		return (MDEG_FAILURE);
50481ae08745Sheppo 	ASSERT(vds != NULL);
50491ae08745Sheppo 
50501ae08745Sheppo 	for (i = 0; i < md->removed.nelem; i++)
50511ae08745Sheppo 		vds_remove_vd(vds, md->removed.mdp, md->removed.mdep[i]);
50521ae08745Sheppo 	for (i = 0; i < md->match_curr.nelem; i++)
50531ae08745Sheppo 		vds_change_vd(vds, md->match_prev.mdp, md->match_prev.mdep[i],
50541ae08745Sheppo 		    md->match_curr.mdp, md->match_curr.mdep[i]);
50551ae08745Sheppo 	for (i = 0; i < md->added.nelem; i++)
50561ae08745Sheppo 		vds_add_vd(vds, md->added.mdp, md->added.mdep[i]);
50571ae08745Sheppo 
50581ae08745Sheppo 	return (MDEG_SUCCESS);
50591ae08745Sheppo }
50601ae08745Sheppo 
50613c96341aSnarayan 
50621ae08745Sheppo static int
50631ae08745Sheppo vds_do_attach(dev_info_t *dip)
50641ae08745Sheppo {
5065445b4c2eSsb155480 	int			status, sz;
5066445b4c2eSsb155480 	int			cfg_handle;
50671ae08745Sheppo 	minor_t			instance = ddi_get_instance(dip);
50681ae08745Sheppo 	vds_t			*vds;
5069445b4c2eSsb155480 	mdeg_prop_spec_t	*pspecp;
5070445b4c2eSsb155480 	mdeg_node_spec_t	*ispecp;
50711ae08745Sheppo 
50721ae08745Sheppo 	/*
50731ae08745Sheppo 	 * The "cfg-handle" property of a vds node in an MD contains the MD's
50741ae08745Sheppo 	 * notion of "instance", or unique identifier, for that node; OBP
50751ae08745Sheppo 	 * stores the value of the "cfg-handle" MD property as the value of
50761ae08745Sheppo 	 * the "reg" property on the node in the device tree it builds from
50771ae08745Sheppo 	 * the MD and passes to Solaris.  Thus, we look up the devinfo node's
50781ae08745Sheppo 	 * "reg" property value to uniquely identify this device instance when
50791ae08745Sheppo 	 * registering with the MD event-generation framework.  If the "reg"
50801ae08745Sheppo 	 * property cannot be found, the device tree state is presumably so
50811ae08745Sheppo 	 * broken that there is no point in continuing.
50821ae08745Sheppo 	 */
5083445b4c2eSsb155480 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
5084445b4c2eSsb155480 	    VD_REG_PROP)) {
5085445b4c2eSsb155480 		PRN("vds \"%s\" property does not exist", VD_REG_PROP);
50861ae08745Sheppo 		return (DDI_FAILURE);
50871ae08745Sheppo 	}
50881ae08745Sheppo 
50891ae08745Sheppo 	/* Get the MD instance for later MDEG registration */
50901ae08745Sheppo 	cfg_handle = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
5091445b4c2eSsb155480 	    VD_REG_PROP, -1);
50921ae08745Sheppo 
50931ae08745Sheppo 	if (ddi_soft_state_zalloc(vds_state, instance) != DDI_SUCCESS) {
50941ae08745Sheppo 		PRN("Could not allocate state for instance %u", instance);
50951ae08745Sheppo 		return (DDI_FAILURE);
50961ae08745Sheppo 	}
50971ae08745Sheppo 
50981ae08745Sheppo 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
50991ae08745Sheppo 		PRN("Could not get state for instance %u", instance);
51001ae08745Sheppo 		ddi_soft_state_free(vds_state, instance);
51011ae08745Sheppo 		return (DDI_FAILURE);
51021ae08745Sheppo 	}
51031ae08745Sheppo 
51041ae08745Sheppo 	vds->dip	= dip;
51051ae08745Sheppo 	vds->vd_table	= mod_hash_create_ptrhash("vds_vd_table", VDS_NCHAINS,
510687a7269eSachartre 	    vds_destroy_vd, sizeof (void *));
510787a7269eSachartre 
51081ae08745Sheppo 	ASSERT(vds->vd_table != NULL);
51091ae08745Sheppo 
51101ae08745Sheppo 	if ((status = ldi_ident_from_dip(dip, &vds->ldi_ident)) != 0) {
51111ae08745Sheppo 		PRN("ldi_ident_from_dip() returned errno %d", status);
51121ae08745Sheppo 		return (DDI_FAILURE);
51131ae08745Sheppo 	}
51141ae08745Sheppo 	vds->initialized |= VDS_LDI;
51151ae08745Sheppo 
51161ae08745Sheppo 	/* Register for MD updates */
5117445b4c2eSsb155480 	sz = sizeof (vds_prop_template);
5118445b4c2eSsb155480 	pspecp = kmem_alloc(sz, KM_SLEEP);
5119445b4c2eSsb155480 	bcopy(vds_prop_template, pspecp, sz);
5120445b4c2eSsb155480 
5121445b4c2eSsb155480 	VDS_SET_MDEG_PROP_INST(pspecp, cfg_handle);
5122445b4c2eSsb155480 
5123445b4c2eSsb155480 	/* initialize the complete prop spec structure */
5124445b4c2eSsb155480 	ispecp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP);
5125445b4c2eSsb155480 	ispecp->namep = "virtual-device";
5126445b4c2eSsb155480 	ispecp->specp = pspecp;
5127445b4c2eSsb155480 
5128445b4c2eSsb155480 	if (mdeg_register(ispecp, &vd_match, vds_process_md, vds,
51291ae08745Sheppo 	    &vds->mdeg) != MDEG_SUCCESS) {
51301ae08745Sheppo 		PRN("Unable to register for MD updates");
5131445b4c2eSsb155480 		kmem_free(ispecp, sizeof (mdeg_node_spec_t));
5132445b4c2eSsb155480 		kmem_free(pspecp, sz);
51331ae08745Sheppo 		return (DDI_FAILURE);
51341ae08745Sheppo 	}
5135445b4c2eSsb155480 
5136445b4c2eSsb155480 	vds->ispecp = ispecp;
51371ae08745Sheppo 	vds->initialized |= VDS_MDEG;
51381ae08745Sheppo 
51390a55fbb7Slm66018 	/* Prevent auto-detaching so driver is available whenever MD changes */
51400a55fbb7Slm66018 	if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1) !=
51410a55fbb7Slm66018 	    DDI_PROP_SUCCESS) {
51420a55fbb7Slm66018 		PRN("failed to set \"%s\" property for instance %u",
51430a55fbb7Slm66018 		    DDI_NO_AUTODETACH, instance);
51440a55fbb7Slm66018 	}
51450a55fbb7Slm66018 
51461ae08745Sheppo 	ddi_report_dev(dip);
51471ae08745Sheppo 	return (DDI_SUCCESS);
51481ae08745Sheppo }
51491ae08745Sheppo 
51501ae08745Sheppo static int
51511ae08745Sheppo vds_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
51521ae08745Sheppo {
51531ae08745Sheppo 	int	status;
51541ae08745Sheppo 
51551ae08745Sheppo 	switch (cmd) {
51561ae08745Sheppo 	case DDI_ATTACH:
5157d10e4ef2Snarayan 		PR0("Attaching");
51581ae08745Sheppo 		if ((status = vds_do_attach(dip)) != DDI_SUCCESS)
51591ae08745Sheppo 			(void) vds_detach(dip, DDI_DETACH);
51601ae08745Sheppo 		return (status);
51611ae08745Sheppo 	case DDI_RESUME:
5162d10e4ef2Snarayan 		PR0("No action required for DDI_RESUME");
51631ae08745Sheppo 		return (DDI_SUCCESS);
51641ae08745Sheppo 	default:
51651ae08745Sheppo 		return (DDI_FAILURE);
51661ae08745Sheppo 	}
51671ae08745Sheppo }
51681ae08745Sheppo 
51691ae08745Sheppo static struct dev_ops vds_ops = {
51701ae08745Sheppo 	DEVO_REV,	/* devo_rev */
51711ae08745Sheppo 	0,		/* devo_refcnt */
51721ae08745Sheppo 	ddi_no_info,	/* devo_getinfo */
51731ae08745Sheppo 	nulldev,	/* devo_identify */
51741ae08745Sheppo 	nulldev,	/* devo_probe */
51751ae08745Sheppo 	vds_attach,	/* devo_attach */
51761ae08745Sheppo 	vds_detach,	/* devo_detach */
51771ae08745Sheppo 	nodev,		/* devo_reset */
51781ae08745Sheppo 	NULL,		/* devo_cb_ops */
51791ae08745Sheppo 	NULL,		/* devo_bus_ops */
51801ae08745Sheppo 	nulldev		/* devo_power */
51811ae08745Sheppo };
51821ae08745Sheppo 
51831ae08745Sheppo static struct modldrv modldrv = {
51841ae08745Sheppo 	&mod_driverops,
5185205eeb1aSlm66018 	"virtual disk server",
51861ae08745Sheppo 	&vds_ops,
51871ae08745Sheppo };
51881ae08745Sheppo 
51891ae08745Sheppo static struct modlinkage modlinkage = {
51901ae08745Sheppo 	MODREV_1,
51911ae08745Sheppo 	&modldrv,
51921ae08745Sheppo 	NULL
51931ae08745Sheppo };
51941ae08745Sheppo 
51951ae08745Sheppo 
51961ae08745Sheppo int
51971ae08745Sheppo _init(void)
51981ae08745Sheppo {
5199*17cadca8Slm66018 	int		status;
5200d10e4ef2Snarayan 
52011ae08745Sheppo 	if ((status = ddi_soft_state_init(&vds_state, sizeof (vds_t), 1)) != 0)
52021ae08745Sheppo 		return (status);
5203*17cadca8Slm66018 
52041ae08745Sheppo 	if ((status = mod_install(&modlinkage)) != 0) {
52051ae08745Sheppo 		ddi_soft_state_fini(&vds_state);
52061ae08745Sheppo 		return (status);
52071ae08745Sheppo 	}
52081ae08745Sheppo 
52091ae08745Sheppo 	return (0);
52101ae08745Sheppo }
52111ae08745Sheppo 
52121ae08745Sheppo int
52131ae08745Sheppo _info(struct modinfo *modinfop)
52141ae08745Sheppo {
52151ae08745Sheppo 	return (mod_info(&modlinkage, modinfop));
52161ae08745Sheppo }
52171ae08745Sheppo 
52181ae08745Sheppo int
52191ae08745Sheppo _fini(void)
52201ae08745Sheppo {
52211ae08745Sheppo 	int	status;
52221ae08745Sheppo 
52231ae08745Sheppo 	if ((status = mod_remove(&modlinkage)) != 0)
52241ae08745Sheppo 		return (status);
52251ae08745Sheppo 	ddi_soft_state_fini(&vds_state);
52261ae08745Sheppo 	return (0);
52271ae08745Sheppo }
5228