xref: /titanic_53/usr/src/uts/sun4v/io/vds.c (revision 205eeb1ae627fcf67b5705c443a2b56a8263406b)
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>
401ae08745Sheppo #include <sys/mdeg.h>
411ae08745Sheppo #include <sys/modhash.h>
421ae08745Sheppo #include <sys/note.h>
431ae08745Sheppo #include <sys/pathname.h>
44*205eeb1aSlm66018 #include <sys/sdt.h>
451ae08745Sheppo #include <sys/sunddi.h>
461ae08745Sheppo #include <sys/sunldi.h>
471ae08745Sheppo #include <sys/sysmacros.h>
481ae08745Sheppo #include <sys/vio_common.h>
491ae08745Sheppo #include <sys/vdsk_mailbox.h>
501ae08745Sheppo #include <sys/vdsk_common.h>
511ae08745Sheppo #include <sys/vtoc.h>
523c96341aSnarayan #include <sys/vfs.h>
533c96341aSnarayan #include <sys/stat.h>
5487a7269eSachartre #include <sys/scsi/impl/uscsi.h>
55690555a1Sachartre #include <vm/seg_map.h>
561ae08745Sheppo 
571ae08745Sheppo /* Virtual disk server initialization flags */
58d10e4ef2Snarayan #define	VDS_LDI			0x01
59d10e4ef2Snarayan #define	VDS_MDEG		0x02
601ae08745Sheppo 
611ae08745Sheppo /* Virtual disk server tunable parameters */
623c96341aSnarayan #define	VDS_RETRIES		5
633c96341aSnarayan #define	VDS_LDC_DELAY		1000 /* 1 msecs */
643c96341aSnarayan #define	VDS_DEV_DELAY		10000000 /* 10 secs */
651ae08745Sheppo #define	VDS_NCHAINS		32
661ae08745Sheppo 
671ae08745Sheppo /* Identification parameters for MD, synthetic dkio(7i) structures, etc. */
681ae08745Sheppo #define	VDS_NAME		"virtual-disk-server"
691ae08745Sheppo 
701ae08745Sheppo #define	VD_NAME			"vd"
711ae08745Sheppo #define	VD_VOLUME_NAME		"vdisk"
721ae08745Sheppo #define	VD_ASCIILABEL		"Virtual Disk"
731ae08745Sheppo 
741ae08745Sheppo #define	VD_CHANNEL_ENDPOINT	"channel-endpoint"
751ae08745Sheppo #define	VD_ID_PROP		"id"
761ae08745Sheppo #define	VD_BLOCK_DEVICE_PROP	"vds-block-device"
77445b4c2eSsb155480 #define	VD_REG_PROP		"reg"
781ae08745Sheppo 
791ae08745Sheppo /* Virtual disk initialization flags */
803c96341aSnarayan #define	VD_DISK_READY		0x01
813c96341aSnarayan #define	VD_LOCKING		0x02
823c96341aSnarayan #define	VD_LDC			0x04
833c96341aSnarayan #define	VD_DRING		0x08
843c96341aSnarayan #define	VD_SID			0x10
853c96341aSnarayan #define	VD_SEQ_NUM		0x20
861ae08745Sheppo 
871ae08745Sheppo /* Flags for opening/closing backing devices via LDI */
881ae08745Sheppo #define	VD_OPEN_FLAGS		(FEXCL | FREAD | FWRITE)
891ae08745Sheppo 
90eba0cb4eSachartre /* Flags for writing to a vdisk which is a file */
91eba0cb4eSachartre #define	VD_FILE_WRITE_FLAGS	SM_ASYNC
92eba0cb4eSachartre 
9387a7269eSachartre /* Number of backup labels */
9487a7269eSachartre #define	VD_FILE_NUM_BACKUP	5
9587a7269eSachartre 
9687a7269eSachartre /* Timeout for SCSI I/O */
9787a7269eSachartre #define	VD_SCSI_RDWR_TIMEOUT	30	/* 30 secs */
9887a7269eSachartre 
991ae08745Sheppo /*
1001ae08745Sheppo  * By Solaris convention, slice/partition 2 represents the entire disk;
1011ae08745Sheppo  * unfortunately, this convention does not appear to be codified.
1021ae08745Sheppo  */
1031ae08745Sheppo #define	VD_ENTIRE_DISK_SLICE	2
1041ae08745Sheppo 
1051ae08745Sheppo /* Return a cpp token as a string */
1061ae08745Sheppo #define	STRINGIZE(token)	#token
1071ae08745Sheppo 
1081ae08745Sheppo /*
1091ae08745Sheppo  * Print a message prefixed with the current function name to the message log
1101ae08745Sheppo  * (and optionally to the console for verbose boots); these macros use cpp's
1111ae08745Sheppo  * concatenation of string literals and C99 variable-length-argument-list
1121ae08745Sheppo  * macros
1131ae08745Sheppo  */
1141ae08745Sheppo #define	PRN(...)	_PRN("?%s():  "__VA_ARGS__, "")
1151ae08745Sheppo #define	_PRN(format, ...)					\
1161ae08745Sheppo 	cmn_err(CE_CONT, format"%s", __func__, __VA_ARGS__)
1171ae08745Sheppo 
1181ae08745Sheppo /* Return a pointer to the "i"th vdisk dring element */
1191ae08745Sheppo #define	VD_DRING_ELEM(i)	((vd_dring_entry_t *)(void *)	\
1201ae08745Sheppo 	    (vd->dring + (i)*vd->descriptor_size))
1211ae08745Sheppo 
1221ae08745Sheppo /* Return the virtual disk client's type as a string (for use in messages) */
1231ae08745Sheppo #define	VD_CLIENT(vd)							\
1241ae08745Sheppo 	(((vd)->xfer_mode == VIO_DESC_MODE) ? "in-band client" :	\
1251ae08745Sheppo 	    (((vd)->xfer_mode == VIO_DRING_MODE) ? "dring client" :	\
1261ae08745Sheppo 		(((vd)->xfer_mode == 0) ? "null client" :		\
1271ae08745Sheppo 		    "unsupported client")))
1281ae08745Sheppo 
129690555a1Sachartre /* Read disk label from a disk on file */
130690555a1Sachartre #define	VD_FILE_LABEL_READ(vd, labelp) \
13187a7269eSachartre 	vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)labelp, \
132690555a1Sachartre 	    0, sizeof (struct dk_label))
133690555a1Sachartre 
134690555a1Sachartre /* Write disk label to a disk on file */
135690555a1Sachartre #define	VD_FILE_LABEL_WRITE(vd, labelp)	\
13687a7269eSachartre 	vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)labelp, \
137690555a1Sachartre 	    0, sizeof (struct dk_label))
138690555a1Sachartre 
139445b4c2eSsb155480 /*
140445b4c2eSsb155480  * Specification of an MD node passed to the MDEG to filter any
141445b4c2eSsb155480  * 'vport' nodes that do not belong to the specified node. This
142445b4c2eSsb155480  * template is copied for each vds instance and filled in with
143445b4c2eSsb155480  * the appropriate 'cfg-handle' value before being passed to the MDEG.
144445b4c2eSsb155480  */
145445b4c2eSsb155480 static mdeg_prop_spec_t	vds_prop_template[] = {
146445b4c2eSsb155480 	{ MDET_PROP_STR,	"name",		VDS_NAME },
147445b4c2eSsb155480 	{ MDET_PROP_VAL,	"cfg-handle",	NULL },
148445b4c2eSsb155480 	{ MDET_LIST_END,	NULL, 		NULL }
149445b4c2eSsb155480 };
150445b4c2eSsb155480 
151445b4c2eSsb155480 #define	VDS_SET_MDEG_PROP_INST(specp, val) (specp)[1].ps_val = (val);
152445b4c2eSsb155480 
153445b4c2eSsb155480 /*
154445b4c2eSsb155480  * Matching criteria passed to the MDEG to register interest
155445b4c2eSsb155480  * in changes to 'virtual-device-port' nodes identified by their
156445b4c2eSsb155480  * 'id' property.
157445b4c2eSsb155480  */
158445b4c2eSsb155480 static md_prop_match_t	vd_prop_match[] = {
159445b4c2eSsb155480 	{ MDET_PROP_VAL,	VD_ID_PROP },
160445b4c2eSsb155480 	{ MDET_LIST_END,	NULL }
161445b4c2eSsb155480 };
162445b4c2eSsb155480 
163445b4c2eSsb155480 static mdeg_node_match_t vd_match = {"virtual-device-port",
164445b4c2eSsb155480 				    vd_prop_match};
165445b4c2eSsb155480 
1661ae08745Sheppo /* Debugging macros */
1671ae08745Sheppo #ifdef DEBUG
1683af08d82Slm66018 
1693af08d82Slm66018 static int	vd_msglevel = 0;
1703af08d82Slm66018 
1711ae08745Sheppo #define	PR0 if (vd_msglevel > 0)	PRN
1721ae08745Sheppo #define	PR1 if (vd_msglevel > 1)	PRN
1731ae08745Sheppo #define	PR2 if (vd_msglevel > 2)	PRN
1741ae08745Sheppo 
1751ae08745Sheppo #define	VD_DUMP_DRING_ELEM(elem)					\
1763c96341aSnarayan 	PR0("dst:%x op:%x st:%u nb:%lx addr:%lx ncook:%u\n",		\
1771ae08745Sheppo 	    elem->hdr.dstate,						\
1781ae08745Sheppo 	    elem->payload.operation,					\
1791ae08745Sheppo 	    elem->payload.status,					\
1801ae08745Sheppo 	    elem->payload.nbytes,					\
1811ae08745Sheppo 	    elem->payload.addr,						\
1821ae08745Sheppo 	    elem->payload.ncookies);
1831ae08745Sheppo 
1843af08d82Slm66018 char *
1853af08d82Slm66018 vd_decode_state(int state)
1863af08d82Slm66018 {
1873af08d82Slm66018 	char *str;
1883af08d82Slm66018 
1893af08d82Slm66018 #define	CASE_STATE(_s)	case _s: str = #_s; break;
1903af08d82Slm66018 
1913af08d82Slm66018 	switch (state) {
1923af08d82Slm66018 	CASE_STATE(VD_STATE_INIT)
1933af08d82Slm66018 	CASE_STATE(VD_STATE_VER)
1943af08d82Slm66018 	CASE_STATE(VD_STATE_ATTR)
1953af08d82Slm66018 	CASE_STATE(VD_STATE_DRING)
1963af08d82Slm66018 	CASE_STATE(VD_STATE_RDX)
1973af08d82Slm66018 	CASE_STATE(VD_STATE_DATA)
1983af08d82Slm66018 	default: str = "unknown"; break;
1993af08d82Slm66018 	}
2003af08d82Slm66018 
2013af08d82Slm66018 #undef CASE_STATE
2023af08d82Slm66018 
2033af08d82Slm66018 	return (str);
2043af08d82Slm66018 }
2053af08d82Slm66018 
2063af08d82Slm66018 void
2073af08d82Slm66018 vd_decode_tag(vio_msg_t *msg)
2083af08d82Slm66018 {
2093af08d82Slm66018 	char *tstr, *sstr, *estr;
2103af08d82Slm66018 
2113af08d82Slm66018 #define	CASE_TYPE(_s)	case _s: tstr = #_s; break;
2123af08d82Slm66018 
2133af08d82Slm66018 	switch (msg->tag.vio_msgtype) {
2143af08d82Slm66018 	CASE_TYPE(VIO_TYPE_CTRL)
2153af08d82Slm66018 	CASE_TYPE(VIO_TYPE_DATA)
2163af08d82Slm66018 	CASE_TYPE(VIO_TYPE_ERR)
2173af08d82Slm66018 	default: tstr = "unknown"; break;
2183af08d82Slm66018 	}
2193af08d82Slm66018 
2203af08d82Slm66018 #undef CASE_TYPE
2213af08d82Slm66018 
2223af08d82Slm66018 #define	CASE_SUBTYPE(_s) case _s: sstr = #_s; break;
2233af08d82Slm66018 
2243af08d82Slm66018 	switch (msg->tag.vio_subtype) {
2253af08d82Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_INFO)
2263af08d82Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_ACK)
2273af08d82Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_NACK)
2283af08d82Slm66018 	default: sstr = "unknown"; break;
2293af08d82Slm66018 	}
2303af08d82Slm66018 
2313af08d82Slm66018 #undef CASE_SUBTYPE
2323af08d82Slm66018 
2333af08d82Slm66018 #define	CASE_ENV(_s)	case _s: estr = #_s; break;
2343af08d82Slm66018 
2353af08d82Slm66018 	switch (msg->tag.vio_subtype_env) {
2363af08d82Slm66018 	CASE_ENV(VIO_VER_INFO)
2373af08d82Slm66018 	CASE_ENV(VIO_ATTR_INFO)
2383af08d82Slm66018 	CASE_ENV(VIO_DRING_REG)
2393af08d82Slm66018 	CASE_ENV(VIO_DRING_UNREG)
2403af08d82Slm66018 	CASE_ENV(VIO_RDX)
2413af08d82Slm66018 	CASE_ENV(VIO_PKT_DATA)
2423af08d82Slm66018 	CASE_ENV(VIO_DESC_DATA)
2433af08d82Slm66018 	CASE_ENV(VIO_DRING_DATA)
2443af08d82Slm66018 	default: estr = "unknown"; break;
2453af08d82Slm66018 	}
2463af08d82Slm66018 
2473af08d82Slm66018 #undef CASE_ENV
2483af08d82Slm66018 
2493af08d82Slm66018 	PR1("(%x/%x/%x) message : (%s/%s/%s)",
2503af08d82Slm66018 	    msg->tag.vio_msgtype, msg->tag.vio_subtype,
2513af08d82Slm66018 	    msg->tag.vio_subtype_env, tstr, sstr, estr);
2523af08d82Slm66018 }
2533af08d82Slm66018 
2541ae08745Sheppo #else	/* !DEBUG */
2553af08d82Slm66018 
2561ae08745Sheppo #define	PR0(...)
2571ae08745Sheppo #define	PR1(...)
2581ae08745Sheppo #define	PR2(...)
2591ae08745Sheppo 
2601ae08745Sheppo #define	VD_DUMP_DRING_ELEM(elem)
2611ae08745Sheppo 
2623af08d82Slm66018 #define	vd_decode_state(_s)	(NULL)
2633af08d82Slm66018 #define	vd_decode_tag(_s)	(NULL)
2643af08d82Slm66018 
2651ae08745Sheppo #endif	/* DEBUG */
2661ae08745Sheppo 
2671ae08745Sheppo 
268d10e4ef2Snarayan /*
269d10e4ef2Snarayan  * Soft state structure for a vds instance
270d10e4ef2Snarayan  */
2711ae08745Sheppo typedef struct vds {
2721ae08745Sheppo 	uint_t		initialized;	/* driver inst initialization flags */
2731ae08745Sheppo 	dev_info_t	*dip;		/* driver inst devinfo pointer */
2741ae08745Sheppo 	ldi_ident_t	ldi_ident;	/* driver's identifier for LDI */
2751ae08745Sheppo 	mod_hash_t	*vd_table;	/* table of virtual disks served */
276445b4c2eSsb155480 	mdeg_node_spec_t *ispecp;	/* mdeg node specification */
2771ae08745Sheppo 	mdeg_handle_t	mdeg;		/* handle for MDEG operations  */
2781ae08745Sheppo } vds_t;
2791ae08745Sheppo 
280d10e4ef2Snarayan /*
281d10e4ef2Snarayan  * Types of descriptor-processing tasks
282d10e4ef2Snarayan  */
283d10e4ef2Snarayan typedef enum vd_task_type {
284d10e4ef2Snarayan 	VD_NONFINAL_RANGE_TASK,	/* task for intermediate descriptor in range */
285d10e4ef2Snarayan 	VD_FINAL_RANGE_TASK,	/* task for last in a range of descriptors */
286d10e4ef2Snarayan } vd_task_type_t;
287d10e4ef2Snarayan 
288d10e4ef2Snarayan /*
289d10e4ef2Snarayan  * Structure describing the task for processing a descriptor
290d10e4ef2Snarayan  */
291d10e4ef2Snarayan typedef struct vd_task {
292d10e4ef2Snarayan 	struct vd		*vd;		/* vd instance task is for */
293d10e4ef2Snarayan 	vd_task_type_t		type;		/* type of descriptor task */
294d10e4ef2Snarayan 	int			index;		/* dring elem index for task */
295d10e4ef2Snarayan 	vio_msg_t		*msg;		/* VIO message task is for */
296d10e4ef2Snarayan 	size_t			msglen;		/* length of message content */
297d10e4ef2Snarayan 	vd_dring_payload_t	*request;	/* request task will perform */
298d10e4ef2Snarayan 	struct buf		buf;		/* buf(9s) for I/O request */
2994bac2208Snarayan 	ldc_mem_handle_t	mhdl;		/* task memory handle */
300*205eeb1aSlm66018 	int			status;		/* status of processing task */
301*205eeb1aSlm66018 	int	(*completef)(struct vd_task *task); /* completion func ptr */
302d10e4ef2Snarayan } vd_task_t;
303d10e4ef2Snarayan 
304d10e4ef2Snarayan /*
305d10e4ef2Snarayan  * Soft state structure for a virtual disk instance
306d10e4ef2Snarayan  */
3071ae08745Sheppo typedef struct vd {
3081ae08745Sheppo 	uint_t			initialized;	/* vdisk initialization flags */
3091ae08745Sheppo 	vds_t			*vds;		/* server for this vdisk */
310d10e4ef2Snarayan 	ddi_taskq_t		*startq;	/* queue for I/O start tasks */
311d10e4ef2Snarayan 	ddi_taskq_t		*completionq;	/* queue for completion tasks */
3121ae08745Sheppo 	ldi_handle_t		ldi_handle[V_NUMPAR];	/* LDI slice handles */
3133c96341aSnarayan 	char			device_path[MAXPATHLEN + 1]; /* vdisk device */
3141ae08745Sheppo 	dev_t			dev[V_NUMPAR];	/* dev numbers for slices */
315e1ebb9ecSlm66018 	uint_t			nslices;	/* number of slices */
3161ae08745Sheppo 	size_t			vdisk_size;	/* number of blocks in vdisk */
3171ae08745Sheppo 	vd_disk_type_t		vdisk_type;	/* slice or entire disk */
3184bac2208Snarayan 	vd_disk_label_t		vdisk_label;	/* EFI or VTOC label */
319e1ebb9ecSlm66018 	ushort_t		max_xfer_sz;	/* max xfer size in DEV_BSIZE */
3201ae08745Sheppo 	boolean_t		pseudo;		/* underlying pseudo dev */
3213c96341aSnarayan 	boolean_t		file;		/* underlying file */
3223c96341aSnarayan 	vnode_t			*file_vnode;	/* file vnode */
3233c96341aSnarayan 	size_t			file_size;	/* file size */
32487a7269eSachartre 	ddi_devid_t		file_devid;	/* devid for disk image */
3254bac2208Snarayan 	struct dk_efi		dk_efi;		/* synthetic for slice type */
3261ae08745Sheppo 	struct dk_geom		dk_geom;	/* synthetic for slice type */
3271ae08745Sheppo 	struct vtoc		vtoc;		/* synthetic for slice type */
3281ae08745Sheppo 	ldc_status_t		ldc_state;	/* LDC connection state */
3291ae08745Sheppo 	ldc_handle_t		ldc_handle;	/* handle for LDC comm */
3301ae08745Sheppo 	size_t			max_msglen;	/* largest LDC message len */
3311ae08745Sheppo 	vd_state_t		state;		/* client handshake state */
3321ae08745Sheppo 	uint8_t			xfer_mode;	/* transfer mode with client */
3331ae08745Sheppo 	uint32_t		sid;		/* client's session ID */
3341ae08745Sheppo 	uint64_t		seq_num;	/* message sequence number */
3351ae08745Sheppo 	uint64_t		dring_ident;	/* identifier of dring */
3361ae08745Sheppo 	ldc_dring_handle_t	dring_handle;	/* handle for dring ops */
3371ae08745Sheppo 	uint32_t		descriptor_size;	/* num bytes in desc */
3381ae08745Sheppo 	uint32_t		dring_len;	/* number of dring elements */
3391ae08745Sheppo 	caddr_t			dring;		/* address of dring */
3403af08d82Slm66018 	caddr_t			vio_msgp;	/* vio msg staging buffer */
341d10e4ef2Snarayan 	vd_task_t		inband_task;	/* task for inband descriptor */
342d10e4ef2Snarayan 	vd_task_t		*dring_task;	/* tasks dring elements */
343d10e4ef2Snarayan 
344d10e4ef2Snarayan 	kmutex_t		lock;		/* protects variables below */
345d10e4ef2Snarayan 	boolean_t		enabled;	/* is vdisk enabled? */
346d10e4ef2Snarayan 	boolean_t		reset_state;	/* reset connection state? */
347d10e4ef2Snarayan 	boolean_t		reset_ldc;	/* reset LDC channel? */
3481ae08745Sheppo } vd_t;
3491ae08745Sheppo 
3501ae08745Sheppo typedef struct vds_operation {
3513af08d82Slm66018 	char	*namep;
3521ae08745Sheppo 	uint8_t	operation;
353d10e4ef2Snarayan 	int	(*start)(vd_task_t *task);
354*205eeb1aSlm66018 	int	(*complete)(vd_task_t *task);
3551ae08745Sheppo } vds_operation_t;
3561ae08745Sheppo 
3570a55fbb7Slm66018 typedef struct vd_ioctl {
3580a55fbb7Slm66018 	uint8_t		operation;		/* vdisk operation */
3590a55fbb7Slm66018 	const char	*operation_name;	/* vdisk operation name */
3600a55fbb7Slm66018 	size_t		nbytes;			/* size of operation buffer */
3610a55fbb7Slm66018 	int		cmd;			/* corresponding ioctl cmd */
3620a55fbb7Slm66018 	const char	*cmd_name;		/* ioctl cmd name */
3630a55fbb7Slm66018 	void		*arg;			/* ioctl cmd argument */
3640a55fbb7Slm66018 	/* convert input vd_buf to output ioctl_arg */
3650a55fbb7Slm66018 	void		(*copyin)(void *vd_buf, void *ioctl_arg);
3660a55fbb7Slm66018 	/* convert input ioctl_arg to output vd_buf */
3670a55fbb7Slm66018 	void		(*copyout)(void *ioctl_arg, void *vd_buf);
3680a55fbb7Slm66018 } vd_ioctl_t;
3690a55fbb7Slm66018 
3700a55fbb7Slm66018 /* Define trivial copyin/copyout conversion function flag */
3710a55fbb7Slm66018 #define	VD_IDENTITY	((void (*)(void *, void *))-1)
3721ae08745Sheppo 
3731ae08745Sheppo 
3743c96341aSnarayan static int	vds_ldc_retries = VDS_RETRIES;
3753af08d82Slm66018 static int	vds_ldc_delay = VDS_LDC_DELAY;
3763c96341aSnarayan static int	vds_dev_retries = VDS_RETRIES;
3773c96341aSnarayan static int	vds_dev_delay = VDS_DEV_DELAY;
3781ae08745Sheppo static void	*vds_state;
3791ae08745Sheppo static uint64_t	vds_operations;	/* see vds_operation[] definition below */
3801ae08745Sheppo 
3811ae08745Sheppo static int	vd_open_flags = VD_OPEN_FLAGS;
3821ae08745Sheppo 
383eba0cb4eSachartre static uint_t	vd_file_write_flags = VD_FILE_WRITE_FLAGS;
384eba0cb4eSachartre 
38587a7269eSachartre static short	vd_scsi_rdwr_timeout = VD_SCSI_RDWR_TIMEOUT;
38687a7269eSachartre 
3870a55fbb7Slm66018 /*
3880a55fbb7Slm66018  * Supported protocol version pairs, from highest (newest) to lowest (oldest)
3890a55fbb7Slm66018  *
3900a55fbb7Slm66018  * Each supported major version should appear only once, paired with (and only
3910a55fbb7Slm66018  * with) its highest supported minor version number (as the protocol requires
3920a55fbb7Slm66018  * supporting all lower minor version numbers as well)
3930a55fbb7Slm66018  */
3940a55fbb7Slm66018 static const vio_ver_t	vds_version[] = {{1, 0}};
3950a55fbb7Slm66018 static const size_t	vds_num_versions =
3960a55fbb7Slm66018     sizeof (vds_version)/sizeof (vds_version[0]);
3970a55fbb7Slm66018 
3983af08d82Slm66018 static void vd_free_dring_task(vd_t *vdp);
3993c96341aSnarayan static int vd_setup_vd(vd_t *vd);
4003c96341aSnarayan static boolean_t vd_enabled(vd_t *vd);
4011ae08745Sheppo 
402690555a1Sachartre /*
403690555a1Sachartre  * Function:
404690555a1Sachartre  *	vd_file_rw
405690555a1Sachartre  *
406690555a1Sachartre  * Description:
407690555a1Sachartre  * 	Read or write to a disk on file.
408690555a1Sachartre  *
409690555a1Sachartre  * Parameters:
410690555a1Sachartre  *	vd		- disk on which the operation is performed.
411690555a1Sachartre  *	slice		- slice on which the operation is performed,
41287a7269eSachartre  *			  VD_SLICE_NONE indicates that the operation
41387a7269eSachartre  *			  is done using an absolute disk offset.
414690555a1Sachartre  *	operation	- operation to execute: read (VD_OP_BREAD) or
415690555a1Sachartre  *			  write (VD_OP_BWRITE).
416690555a1Sachartre  *	data		- buffer where data are read to or written from.
417690555a1Sachartre  *	blk		- starting block for the operation.
418690555a1Sachartre  *	len		- number of bytes to read or write.
419690555a1Sachartre  *
420690555a1Sachartre  * Return Code:
421690555a1Sachartre  *	n >= 0		- success, n indicates the number of bytes read
422690555a1Sachartre  *			  or written.
423690555a1Sachartre  *	-1		- error.
424690555a1Sachartre  */
425690555a1Sachartre static ssize_t
426690555a1Sachartre vd_file_rw(vd_t *vd, int slice, int operation, caddr_t data, size_t blk,
427690555a1Sachartre     size_t len)
428690555a1Sachartre {
429690555a1Sachartre 	caddr_t	maddr;
430690555a1Sachartre 	size_t offset, maxlen, moffset, mlen, n;
431690555a1Sachartre 	uint_t smflags;
432690555a1Sachartre 	enum seg_rw srw;
433690555a1Sachartre 
434690555a1Sachartre 	ASSERT(vd->file);
435690555a1Sachartre 	ASSERT(len > 0);
436690555a1Sachartre 
43787a7269eSachartre 	if (slice == VD_SLICE_NONE) {
438690555a1Sachartre 		/* raw disk access */
439690555a1Sachartre 		offset = blk * DEV_BSIZE;
440690555a1Sachartre 	} else {
441690555a1Sachartre 		ASSERT(slice >= 0 && slice < V_NUMPAR);
442690555a1Sachartre 		if (blk >= vd->vtoc.v_part[slice].p_size) {
443690555a1Sachartre 			/* address past the end of the slice */
444690555a1Sachartre 			PR0("req_addr (0x%lx) > psize (0x%lx)",
445690555a1Sachartre 			    blk, vd->vtoc.v_part[slice].p_size);
446690555a1Sachartre 			return (0);
447690555a1Sachartre 		}
448690555a1Sachartre 
449690555a1Sachartre 		offset = (vd->vtoc.v_part[slice].p_start + blk) * DEV_BSIZE;
450690555a1Sachartre 
451690555a1Sachartre 		/*
452690555a1Sachartre 		 * If the requested size is greater than the size
453690555a1Sachartre 		 * of the partition, truncate the read/write.
454690555a1Sachartre 		 */
455690555a1Sachartre 		maxlen = (vd->vtoc.v_part[slice].p_size - blk) * DEV_BSIZE;
456690555a1Sachartre 
457690555a1Sachartre 		if (len > maxlen) {
458690555a1Sachartre 			PR0("I/O size truncated to %lu bytes from %lu bytes",
459690555a1Sachartre 			    maxlen, len);
460690555a1Sachartre 			len = maxlen;
461690555a1Sachartre 		}
462690555a1Sachartre 	}
463690555a1Sachartre 
464690555a1Sachartre 	/*
465690555a1Sachartre 	 * We have to ensure that we are reading/writing into the mmap
466690555a1Sachartre 	 * range. If we have a partial disk image (e.g. an image of
467690555a1Sachartre 	 * s0 instead s2) the system can try to access slices that
468690555a1Sachartre 	 * are not included into the disk image.
469690555a1Sachartre 	 */
470690555a1Sachartre 	if ((offset + len) >= vd->file_size) {
471690555a1Sachartre 		PR0("offset + nbytes (0x%lx + 0x%lx) >= "
472690555a1Sachartre 		    "file_size (0x%lx)", offset, len, vd->file_size);
473690555a1Sachartre 		return (-1);
474690555a1Sachartre 	}
475690555a1Sachartre 
476690555a1Sachartre 	srw = (operation == VD_OP_BREAD)? S_READ : S_WRITE;
477eba0cb4eSachartre 	smflags = (operation == VD_OP_BREAD)? 0 :
478eba0cb4eSachartre 	    (SM_WRITE | vd_file_write_flags);
479690555a1Sachartre 	n = len;
480690555a1Sachartre 
481690555a1Sachartre 	do {
482690555a1Sachartre 		/*
483690555a1Sachartre 		 * segmap_getmapflt() returns a MAXBSIZE chunk which is
484690555a1Sachartre 		 * MAXBSIZE aligned.
485690555a1Sachartre 		 */
486690555a1Sachartre 		moffset = offset & MAXBOFFSET;
487690555a1Sachartre 		mlen = MIN(MAXBSIZE - moffset, n);
488690555a1Sachartre 		maddr = segmap_getmapflt(segkmap, vd->file_vnode, offset,
489690555a1Sachartre 		    mlen, 1, srw);
490690555a1Sachartre 		/*
491690555a1Sachartre 		 * Fault in the pages so we can check for error and ensure
492690555a1Sachartre 		 * that we can safely used the mapped address.
493690555a1Sachartre 		 */
494690555a1Sachartre 		if (segmap_fault(kas.a_hat, segkmap, maddr, mlen,
495690555a1Sachartre 		    F_SOFTLOCK, srw) != 0) {
496690555a1Sachartre 			(void) segmap_release(segkmap, maddr, 0);
497690555a1Sachartre 			return (-1);
498690555a1Sachartre 		}
499690555a1Sachartre 
500690555a1Sachartre 		if (operation == VD_OP_BREAD)
501690555a1Sachartre 			bcopy(maddr + moffset, data, mlen);
502690555a1Sachartre 		else
503690555a1Sachartre 			bcopy(data, maddr + moffset, mlen);
504690555a1Sachartre 
505690555a1Sachartre 		if (segmap_fault(kas.a_hat, segkmap, maddr, mlen,
506690555a1Sachartre 		    F_SOFTUNLOCK, srw) != 0) {
507690555a1Sachartre 			(void) segmap_release(segkmap, maddr, 0);
508690555a1Sachartre 			return (-1);
509690555a1Sachartre 		}
510690555a1Sachartre 		if (segmap_release(segkmap, maddr, smflags) != 0)
511690555a1Sachartre 			return (-1);
512690555a1Sachartre 		n -= mlen;
513690555a1Sachartre 		offset += mlen;
514690555a1Sachartre 		data += mlen;
515690555a1Sachartre 
516690555a1Sachartre 	} while (n > 0);
517690555a1Sachartre 
518690555a1Sachartre 	return (len);
519690555a1Sachartre }
520690555a1Sachartre 
52187a7269eSachartre /*
52287a7269eSachartre  * Function:
52387a7269eSachartre  *	vd_file_set_vtoc
52487a7269eSachartre  *
52587a7269eSachartre  * Description:
52687a7269eSachartre  *	Set the vtoc of a disk image by writing the label and backup
52787a7269eSachartre  *	labels into the disk image backend.
52887a7269eSachartre  *
52987a7269eSachartre  * Parameters:
53087a7269eSachartre  *	vd		- disk on which the operation is performed.
53187a7269eSachartre  *	label		- the data to be written.
53287a7269eSachartre  *
53387a7269eSachartre  * Return Code:
53487a7269eSachartre  *	0		- success.
53587a7269eSachartre  *	n > 0		- error, n indicates the errno code.
53687a7269eSachartre  */
53787a7269eSachartre static int
53887a7269eSachartre vd_file_set_vtoc(vd_t *vd, struct dk_label *label)
53987a7269eSachartre {
54087a7269eSachartre 	int blk, sec, cyl, head, cnt;
54187a7269eSachartre 
54287a7269eSachartre 	ASSERT(vd->file);
54387a7269eSachartre 
54487a7269eSachartre 	if (VD_FILE_LABEL_WRITE(vd, label) < 0) {
54587a7269eSachartre 		PR0("fail to write disk label");
54687a7269eSachartre 		return (EIO);
54787a7269eSachartre 	}
54887a7269eSachartre 
54987a7269eSachartre 	/*
55087a7269eSachartre 	 * Backup labels are on the last alternate cylinder's
55187a7269eSachartre 	 * first five odd sectors.
55287a7269eSachartre 	 */
55387a7269eSachartre 	if (label->dkl_acyl == 0) {
55487a7269eSachartre 		PR0("no alternate cylinder, can not store backup labels");
55587a7269eSachartre 		return (0);
55687a7269eSachartre 	}
55787a7269eSachartre 
55887a7269eSachartre 	cyl = label->dkl_ncyl  + label->dkl_acyl - 1;
55987a7269eSachartre 	head = label->dkl_nhead - 1;
56087a7269eSachartre 
56187a7269eSachartre 	blk = (cyl * ((label->dkl_nhead * label->dkl_nsect) - label->dkl_apc)) +
56287a7269eSachartre 	    (head * label->dkl_nsect);
56387a7269eSachartre 
56487a7269eSachartre 	/*
56587a7269eSachartre 	 * Write the backup labels. Make sure we don't try to write past
56687a7269eSachartre 	 * the last cylinder.
56787a7269eSachartre 	 */
56887a7269eSachartre 	sec = 1;
56987a7269eSachartre 
57087a7269eSachartre 	for (cnt = 0; cnt < VD_FILE_NUM_BACKUP; cnt++) {
57187a7269eSachartre 
57287a7269eSachartre 		if (sec >= label->dkl_nsect) {
57387a7269eSachartre 			PR0("not enough sector to store all backup labels");
57487a7269eSachartre 			return (0);
57587a7269eSachartre 		}
57687a7269eSachartre 
57787a7269eSachartre 		if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)label,
57887a7269eSachartre 		    blk + sec, sizeof (struct dk_label)) < 0) {
57987a7269eSachartre 			PR0("error writing backup label at block %d\n",
58087a7269eSachartre 			    blk + sec);
58187a7269eSachartre 			return (EIO);
58287a7269eSachartre 		}
58387a7269eSachartre 
58487a7269eSachartre 		PR1("wrote backup label at block %d\n", blk + sec);
58587a7269eSachartre 
58687a7269eSachartre 		sec += 2;
58787a7269eSachartre 	}
58887a7269eSachartre 
58987a7269eSachartre 	return (0);
59087a7269eSachartre }
59187a7269eSachartre 
59287a7269eSachartre /*
59387a7269eSachartre  * Function:
59487a7269eSachartre  *	vd_file_get_devid_block
59587a7269eSachartre  *
59687a7269eSachartre  * Description:
59787a7269eSachartre  *	Return the block number where the device id is stored.
59887a7269eSachartre  *
59987a7269eSachartre  * Parameters:
60087a7269eSachartre  *	vd		- disk on which the operation is performed.
60187a7269eSachartre  *	blkp		- pointer to the block number
60287a7269eSachartre  *
60387a7269eSachartre  * Return Code:
60487a7269eSachartre  *	0		- success
60587a7269eSachartre  *	ENOSPC		- disk has no space to store a device id
60687a7269eSachartre  */
60787a7269eSachartre static int
60887a7269eSachartre vd_file_get_devid_block(vd_t *vd, size_t *blkp)
60987a7269eSachartre {
61087a7269eSachartre 	diskaddr_t spc, head, cyl;
61187a7269eSachartre 
61287a7269eSachartre 	ASSERT(vd->file);
61387a7269eSachartre 	ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
61487a7269eSachartre 
61587a7269eSachartre 	/* this geometry doesn't allow us to have a devid */
61687a7269eSachartre 	if (vd->dk_geom.dkg_acyl < 2) {
61787a7269eSachartre 		PR0("not enough alternate cylinder available for devid "
61887a7269eSachartre 		    "(acyl=%u)", vd->dk_geom.dkg_acyl);
61987a7269eSachartre 		return (ENOSPC);
62087a7269eSachartre 	}
62187a7269eSachartre 
62287a7269eSachartre 	/* the devid is in on the track next to the last cylinder */
62387a7269eSachartre 	cyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl - 2;
62487a7269eSachartre 	spc = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
62587a7269eSachartre 	head = vd->dk_geom.dkg_nhead - 1;
62687a7269eSachartre 
62787a7269eSachartre 	*blkp = (cyl * (spc - vd->dk_geom.dkg_apc)) +
62887a7269eSachartre 	    (head * vd->dk_geom.dkg_nsect) + 1;
62987a7269eSachartre 
63087a7269eSachartre 	return (0);
63187a7269eSachartre }
63287a7269eSachartre 
63387a7269eSachartre /*
63487a7269eSachartre  * Return the checksum of a disk block containing an on-disk devid.
63587a7269eSachartre  */
63687a7269eSachartre static uint_t
63787a7269eSachartre vd_dkdevid2cksum(struct dk_devid *dkdevid)
63887a7269eSachartre {
63987a7269eSachartre 	uint_t chksum, *ip;
64087a7269eSachartre 	int i;
64187a7269eSachartre 
64287a7269eSachartre 	chksum = 0;
64387a7269eSachartre 	ip = (uint_t *)dkdevid;
64487a7269eSachartre 	for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int)); i++)
64587a7269eSachartre 		chksum ^= ip[i];
64687a7269eSachartre 
64787a7269eSachartre 	return (chksum);
64887a7269eSachartre }
64987a7269eSachartre 
65087a7269eSachartre /*
65187a7269eSachartre  * Function:
65287a7269eSachartre  *	vd_file_read_devid
65387a7269eSachartre  *
65487a7269eSachartre  * Description:
65587a7269eSachartre  *	Read the device id stored on a disk image.
65687a7269eSachartre  *
65787a7269eSachartre  * Parameters:
65887a7269eSachartre  *	vd		- disk on which the operation is performed.
65987a7269eSachartre  *	devid		- the return address of the device ID.
66087a7269eSachartre  *
66187a7269eSachartre  * Return Code:
66287a7269eSachartre  *	0		- success
66387a7269eSachartre  *	EIO		- I/O error while trying to access the disk image
66487a7269eSachartre  *	EINVAL		- no valid device id was found
66587a7269eSachartre  *	ENOSPC		- disk has no space to store a device id
66687a7269eSachartre  */
66787a7269eSachartre static int
66887a7269eSachartre vd_file_read_devid(vd_t *vd, ddi_devid_t *devid)
66987a7269eSachartre {
67087a7269eSachartre 	struct dk_devid *dkdevid;
67187a7269eSachartre 	size_t blk;
67287a7269eSachartre 	uint_t chksum;
67387a7269eSachartre 	int status, sz;
67487a7269eSachartre 
67587a7269eSachartre 	if ((status = vd_file_get_devid_block(vd, &blk)) != 0)
67687a7269eSachartre 		return (status);
67787a7269eSachartre 
67887a7269eSachartre 	dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP);
67987a7269eSachartre 
68087a7269eSachartre 	/* get the devid */
68187a7269eSachartre 	if ((vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)dkdevid, blk,
68287a7269eSachartre 	    DEV_BSIZE)) < 0) {
68387a7269eSachartre 		PR0("error reading devid block at %lu", blk);
68487a7269eSachartre 		status = EIO;
68587a7269eSachartre 		goto done;
68687a7269eSachartre 	}
68787a7269eSachartre 
68887a7269eSachartre 	/* validate the revision */
68987a7269eSachartre 	if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
69087a7269eSachartre 	    (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
69187a7269eSachartre 		PR0("invalid devid found at block %lu (bad revision)", blk);
69287a7269eSachartre 		status = EINVAL;
69387a7269eSachartre 		goto done;
69487a7269eSachartre 	}
69587a7269eSachartre 
69687a7269eSachartre 	/* compute checksum */
69787a7269eSachartre 	chksum = vd_dkdevid2cksum(dkdevid);
69887a7269eSachartre 
69987a7269eSachartre 	/* compare the checksums */
70087a7269eSachartre 	if (DKD_GETCHKSUM(dkdevid) != chksum) {
70187a7269eSachartre 		PR0("invalid devid found at block %lu (bad checksum)", blk);
70287a7269eSachartre 		status = EINVAL;
70387a7269eSachartre 		goto done;
70487a7269eSachartre 	}
70587a7269eSachartre 
70687a7269eSachartre 	/* validate the device id */
70787a7269eSachartre 	if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
70887a7269eSachartre 		PR0("invalid devid found at block %lu", blk);
70987a7269eSachartre 		status = EINVAL;
71087a7269eSachartre 		goto done;
71187a7269eSachartre 	}
71287a7269eSachartre 
71387a7269eSachartre 	PR1("devid read at block %lu", blk);
71487a7269eSachartre 
71587a7269eSachartre 	sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
71687a7269eSachartre 	*devid = kmem_alloc(sz, KM_SLEEP);
71787a7269eSachartre 	bcopy(&dkdevid->dkd_devid, *devid, sz);
71887a7269eSachartre 
71987a7269eSachartre done:
72087a7269eSachartre 	kmem_free(dkdevid, DEV_BSIZE);
72187a7269eSachartre 	return (status);
72287a7269eSachartre 
72387a7269eSachartre }
72487a7269eSachartre 
72587a7269eSachartre /*
72687a7269eSachartre  * Function:
72787a7269eSachartre  *	vd_file_write_devid
72887a7269eSachartre  *
72987a7269eSachartre  * Description:
73087a7269eSachartre  *	Write a device id into disk image.
73187a7269eSachartre  *
73287a7269eSachartre  * Parameters:
73387a7269eSachartre  *	vd		- disk on which the operation is performed.
73487a7269eSachartre  *	devid		- the device ID to store.
73587a7269eSachartre  *
73687a7269eSachartre  * Return Code:
73787a7269eSachartre  *	0		- success
73887a7269eSachartre  *	EIO		- I/O error while trying to access the disk image
73987a7269eSachartre  *	ENOSPC		- disk has no space to store a device id
74087a7269eSachartre  */
74187a7269eSachartre static int
74287a7269eSachartre vd_file_write_devid(vd_t *vd, ddi_devid_t devid)
74387a7269eSachartre {
74487a7269eSachartre 	struct dk_devid *dkdevid;
74587a7269eSachartre 	uint_t chksum;
74687a7269eSachartre 	size_t blk;
74787a7269eSachartre 	int status;
74887a7269eSachartre 
74987a7269eSachartre 	if ((status = vd_file_get_devid_block(vd, &blk)) != 0)
75087a7269eSachartre 		return (status);
75187a7269eSachartre 
75287a7269eSachartre 	dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP);
75387a7269eSachartre 
75487a7269eSachartre 	/* set revision */
75587a7269eSachartre 	dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
75687a7269eSachartre 	dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
75787a7269eSachartre 
75887a7269eSachartre 	/* copy devid */
75987a7269eSachartre 	bcopy(devid, &dkdevid->dkd_devid, ddi_devid_sizeof(devid));
76087a7269eSachartre 
76187a7269eSachartre 	/* compute checksum */
76287a7269eSachartre 	chksum = vd_dkdevid2cksum(dkdevid);
76387a7269eSachartre 
76487a7269eSachartre 	/* set checksum */
76587a7269eSachartre 	DKD_FORMCHKSUM(chksum, dkdevid);
76687a7269eSachartre 
76787a7269eSachartre 	/* store the devid */
76887a7269eSachartre 	if ((status = vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE,
76987a7269eSachartre 	    (caddr_t)dkdevid, blk, DEV_BSIZE)) < 0) {
77087a7269eSachartre 		PR0("Error writing devid block at %lu", blk);
77187a7269eSachartre 		status = EIO;
77287a7269eSachartre 	} else {
77387a7269eSachartre 		PR1("devid written at block %lu", blk);
77487a7269eSachartre 		status = 0;
77587a7269eSachartre 	}
77687a7269eSachartre 
77787a7269eSachartre 	kmem_free(dkdevid, DEV_BSIZE);
77887a7269eSachartre 	return (status);
77987a7269eSachartre }
78087a7269eSachartre 
78187a7269eSachartre /*
78287a7269eSachartre  * Function:
78387a7269eSachartre  *	vd_scsi_rdwr
78487a7269eSachartre  *
78587a7269eSachartre  * Description:
78687a7269eSachartre  * 	Read or write to a SCSI disk using an absolute disk offset.
78787a7269eSachartre  *
78887a7269eSachartre  * Parameters:
78987a7269eSachartre  *	vd		- disk on which the operation is performed.
79087a7269eSachartre  *	operation	- operation to execute: read (VD_OP_BREAD) or
79187a7269eSachartre  *			  write (VD_OP_BWRITE).
79287a7269eSachartre  *	data		- buffer where data are read to or written from.
79387a7269eSachartre  *	blk		- starting block for the operation.
79487a7269eSachartre  *	len		- number of bytes to read or write.
79587a7269eSachartre  *
79687a7269eSachartre  * Return Code:
79787a7269eSachartre  *	0		- success
79887a7269eSachartre  *	n != 0		- error.
79987a7269eSachartre  */
80087a7269eSachartre static int
80187a7269eSachartre vd_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t blk, size_t len)
80287a7269eSachartre {
80387a7269eSachartre 	struct uscsi_cmd ucmd;
80487a7269eSachartre 	union scsi_cdb cdb;
80587a7269eSachartre 	int nsectors, nblk;
80687a7269eSachartre 	int max_sectors;
80787a7269eSachartre 	int status, rval;
80887a7269eSachartre 
80987a7269eSachartre 	ASSERT(!vd->file);
81087a7269eSachartre 
81187a7269eSachartre 	max_sectors = vd->max_xfer_sz;
81287a7269eSachartre 	nblk = (len / DEV_BSIZE);
81387a7269eSachartre 
81487a7269eSachartre 	if (len % DEV_BSIZE != 0)
81587a7269eSachartre 		return (EINVAL);
81687a7269eSachartre 
81787a7269eSachartre 	/*
81887a7269eSachartre 	 * Build and execute the uscsi ioctl.  We build a group0, group1
81987a7269eSachartre 	 * or group4 command as necessary, since some targets
82087a7269eSachartre 	 * do not support group1 commands.
82187a7269eSachartre 	 */
82287a7269eSachartre 	while (nblk) {
82387a7269eSachartre 
82487a7269eSachartre 		bzero(&ucmd, sizeof (ucmd));
82587a7269eSachartre 		bzero(&cdb, sizeof (cdb));
82687a7269eSachartre 
82787a7269eSachartre 		nsectors = (max_sectors < nblk) ? max_sectors : nblk;
82887a7269eSachartre 
82987a7269eSachartre 		if (blk < (2 << 20) && nsectors <= 0xff) {
83087a7269eSachartre 			FORMG0ADDR(&cdb, blk);
83187a7269eSachartre 			FORMG0COUNT(&cdb, nsectors);
83287a7269eSachartre 			ucmd.uscsi_cdblen = CDB_GROUP0;
83387a7269eSachartre 		} else if (blk > 0xffffffff) {
83487a7269eSachartre 			FORMG4LONGADDR(&cdb, blk);
83587a7269eSachartre 			FORMG4COUNT(&cdb, nsectors);
83687a7269eSachartre 			ucmd.uscsi_cdblen = CDB_GROUP4;
83787a7269eSachartre 			cdb.scc_cmd |= SCMD_GROUP4;
83887a7269eSachartre 		} else {
83987a7269eSachartre 			FORMG1ADDR(&cdb, blk);
84087a7269eSachartre 			FORMG1COUNT(&cdb, nsectors);
84187a7269eSachartre 			ucmd.uscsi_cdblen = CDB_GROUP1;
84287a7269eSachartre 			cdb.scc_cmd |= SCMD_GROUP1;
84387a7269eSachartre 		}
84487a7269eSachartre 
84587a7269eSachartre 		ucmd.uscsi_cdb = (caddr_t)&cdb;
84687a7269eSachartre 		ucmd.uscsi_bufaddr = data;
84787a7269eSachartre 		ucmd.uscsi_buflen = nsectors * DEV_BSIZE;
84887a7269eSachartre 		ucmd.uscsi_timeout = vd_scsi_rdwr_timeout;
84987a7269eSachartre 		/*
85087a7269eSachartre 		 * Set flags so that the command is isolated from normal
85187a7269eSachartre 		 * commands and no error message is printed.
85287a7269eSachartre 		 */
85387a7269eSachartre 		ucmd.uscsi_flags = USCSI_ISOLATE | USCSI_SILENT;
85487a7269eSachartre 
85587a7269eSachartre 		if (operation == VD_OP_BREAD) {
85687a7269eSachartre 			cdb.scc_cmd |= SCMD_READ;
85787a7269eSachartre 			ucmd.uscsi_flags |= USCSI_READ;
85887a7269eSachartre 		} else {
85987a7269eSachartre 			cdb.scc_cmd |= SCMD_WRITE;
86087a7269eSachartre 		}
86187a7269eSachartre 
86287a7269eSachartre 		status = ldi_ioctl(vd->ldi_handle[VD_ENTIRE_DISK_SLICE],
86387a7269eSachartre 		    USCSICMD, (intptr_t)&ucmd, (vd_open_flags | FKIOCTL),
86487a7269eSachartre 		    kcred, &rval);
86587a7269eSachartre 
86687a7269eSachartre 		if (status == 0)
86787a7269eSachartre 			status = ucmd.uscsi_status;
86887a7269eSachartre 
86987a7269eSachartre 		if (status != 0)
87087a7269eSachartre 			break;
87187a7269eSachartre 
87287a7269eSachartre 		/*
87387a7269eSachartre 		 * Check if partial DMA breakup is required. If so, reduce
87487a7269eSachartre 		 * the request size by half and retry the last request.
87587a7269eSachartre 		 */
87687a7269eSachartre 		if (ucmd.uscsi_resid == ucmd.uscsi_buflen) {
87787a7269eSachartre 			max_sectors >>= 1;
87887a7269eSachartre 			if (max_sectors <= 0) {
87987a7269eSachartre 				status = EIO;
88087a7269eSachartre 				break;
88187a7269eSachartre 			}
88287a7269eSachartre 			continue;
88387a7269eSachartre 		}
88487a7269eSachartre 
88587a7269eSachartre 		if (ucmd.uscsi_resid != 0) {
88687a7269eSachartre 			status = EIO;
88787a7269eSachartre 			break;
88887a7269eSachartre 		}
88987a7269eSachartre 
89087a7269eSachartre 		blk += nsectors;
89187a7269eSachartre 		nblk -= nsectors;
89287a7269eSachartre 		data += nsectors * DEV_BSIZE; /* SECSIZE */
89387a7269eSachartre 	}
89487a7269eSachartre 
89587a7269eSachartre 	return (status);
89687a7269eSachartre }
89787a7269eSachartre 
898*205eeb1aSlm66018 /*
899*205eeb1aSlm66018  * Return Values
900*205eeb1aSlm66018  *	EINPROGRESS	- operation was successfully started
901*205eeb1aSlm66018  *	EIO		- encountered LDC (aka. task error)
902*205eeb1aSlm66018  *	0		- operation completed successfully
903*205eeb1aSlm66018  *
904*205eeb1aSlm66018  * Side Effect
905*205eeb1aSlm66018  *     sets request->status = <disk operation status>
906*205eeb1aSlm66018  */
9071ae08745Sheppo static int
908d10e4ef2Snarayan vd_start_bio(vd_task_t *task)
9091ae08745Sheppo {
9104bac2208Snarayan 	int			rv, status = 0;
911d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
912d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
913d10e4ef2Snarayan 	struct buf		*buf		= &task->buf;
9144bac2208Snarayan 	uint8_t			mtype;
9153c96341aSnarayan 	int 			slice;
916d10e4ef2Snarayan 
917d10e4ef2Snarayan 	ASSERT(vd != NULL);
918d10e4ef2Snarayan 	ASSERT(request != NULL);
9193c96341aSnarayan 
9203c96341aSnarayan 	slice = request->slice;
9213c96341aSnarayan 
92287a7269eSachartre 	ASSERT(slice == VD_SLICE_NONE || slice < vd->nslices);
923d10e4ef2Snarayan 	ASSERT((request->operation == VD_OP_BREAD) ||
924d10e4ef2Snarayan 	    (request->operation == VD_OP_BWRITE));
925d10e4ef2Snarayan 
926*205eeb1aSlm66018 	if (request->nbytes == 0) {
927*205eeb1aSlm66018 		/* no service for trivial requests */
928*205eeb1aSlm66018 		request->status = EINVAL;
929*205eeb1aSlm66018 		return (0);
930*205eeb1aSlm66018 	}
9311ae08745Sheppo 
932d10e4ef2Snarayan 	PR1("%s %lu bytes at block %lu",
933d10e4ef2Snarayan 	    (request->operation == VD_OP_BREAD) ? "Read" : "Write",
934d10e4ef2Snarayan 	    request->nbytes, request->addr);
9351ae08745Sheppo 
936d10e4ef2Snarayan 	bioinit(buf);
937d10e4ef2Snarayan 	buf->b_flags		= B_BUSY;
938d10e4ef2Snarayan 	buf->b_bcount		= request->nbytes;
939d10e4ef2Snarayan 	buf->b_lblkno		= request->addr;
94087a7269eSachartre 	buf->b_edev = (slice == VD_SLICE_NONE)? NODEV : vd->dev[slice];
941d10e4ef2Snarayan 
9424bac2208Snarayan 	mtype = (&vd->inband_task == task) ? LDC_SHADOW_MAP : LDC_DIRECT_MAP;
9434bac2208Snarayan 
9444bac2208Snarayan 	/* Map memory exported by client */
9454bac2208Snarayan 	status = ldc_mem_map(task->mhdl, request->cookie, request->ncookies,
9464bac2208Snarayan 	    mtype, (request->operation == VD_OP_BREAD) ? LDC_MEM_W : LDC_MEM_R,
9474bac2208Snarayan 	    &(buf->b_un.b_addr), NULL);
9484bac2208Snarayan 	if (status != 0) {
9493af08d82Slm66018 		PR0("ldc_mem_map() returned err %d ", status);
9504bac2208Snarayan 		biofini(buf);
951*205eeb1aSlm66018 		return (EIO);
952d10e4ef2Snarayan 	}
953d10e4ef2Snarayan 
9544bac2208Snarayan 	status = ldc_mem_acquire(task->mhdl, 0, buf->b_bcount);
9554bac2208Snarayan 	if (status != 0) {
9564bac2208Snarayan 		(void) ldc_mem_unmap(task->mhdl);
9573af08d82Slm66018 		PR0("ldc_mem_acquire() returned err %d ", status);
9584bac2208Snarayan 		biofini(buf);
959*205eeb1aSlm66018 		return (EIO);
9604bac2208Snarayan 	}
9614bac2208Snarayan 
9624bac2208Snarayan 	buf->b_flags |= (request->operation == VD_OP_BREAD) ? B_READ : B_WRITE;
9634bac2208Snarayan 
964d10e4ef2Snarayan 	/* Start the block I/O */
9653c96341aSnarayan 	if (vd->file) {
966690555a1Sachartre 		rv = vd_file_rw(vd, slice, request->operation, buf->b_un.b_addr,
967690555a1Sachartre 		    request->addr, request->nbytes);
968690555a1Sachartre 		if (rv < 0) {
9693c96341aSnarayan 			request->nbytes = 0;
970*205eeb1aSlm66018 			request->status = EIO;
971690555a1Sachartre 		} else {
972690555a1Sachartre 			request->nbytes = rv;
973*205eeb1aSlm66018 			request->status = 0;
9743c96341aSnarayan 		}
9753c96341aSnarayan 	} else {
97687a7269eSachartre 		if (slice == VD_SLICE_NONE) {
97787a7269eSachartre 			/*
97887a7269eSachartre 			 * This is not a disk image so it is a real disk. We
97987a7269eSachartre 			 * assume that the underlying device driver supports
98087a7269eSachartre 			 * USCSICMD ioctls. This is the case of all SCSI devices
98187a7269eSachartre 			 * (sd, ssd...).
98287a7269eSachartre 			 *
98387a7269eSachartre 			 * In the future if we have non-SCSI disks we would need
98487a7269eSachartre 			 * to invoke the appropriate function to do I/O using an
98587a7269eSachartre 			 * absolute disk offset (for example using DKIOCTL_RWCMD
98687a7269eSachartre 			 * for IDE disks).
98787a7269eSachartre 			 */
98887a7269eSachartre 			rv = vd_scsi_rdwr(vd, request->operation,
98987a7269eSachartre 			    buf->b_un.b_addr, request->addr, request->nbytes);
99087a7269eSachartre 			if (rv != 0) {
99187a7269eSachartre 				request->nbytes = 0;
992*205eeb1aSlm66018 				request->status = EIO;
99387a7269eSachartre 			} else {
994*205eeb1aSlm66018 				request->status = 0;
99587a7269eSachartre 			}
99687a7269eSachartre 		} else {
997*205eeb1aSlm66018 			request->status =
998*205eeb1aSlm66018 			    ldi_strategy(vd->ldi_handle[slice], buf);
999*205eeb1aSlm66018 
1000*205eeb1aSlm66018 			/*
1001*205eeb1aSlm66018 			 * This is to indicate to the caller that the request
1002*205eeb1aSlm66018 			 * needs to be finished by vd_complete_bio() by calling
1003*205eeb1aSlm66018 			 * biowait() there and waiting for that to return before
1004*205eeb1aSlm66018 			 * triggering the notification of the vDisk client.
1005*205eeb1aSlm66018 			 *
1006*205eeb1aSlm66018 			 * This is necessary when writing to real disks as
1007*205eeb1aSlm66018 			 * otherwise calls to ldi_strategy() would be serialized
1008*205eeb1aSlm66018 			 * behind the calls to biowait() and performance would
1009*205eeb1aSlm66018 			 * suffer.
1010*205eeb1aSlm66018 			 */
1011*205eeb1aSlm66018 			if (request->status == 0)
101287a7269eSachartre 				return (EINPROGRESS);
101387a7269eSachartre 		}
10143c96341aSnarayan 	}
10153c96341aSnarayan 
1016d10e4ef2Snarayan 	/* Clean up after error */
10174bac2208Snarayan 	rv = ldc_mem_release(task->mhdl, 0, buf->b_bcount);
10184bac2208Snarayan 	if (rv) {
10193af08d82Slm66018 		PR0("ldc_mem_release() returned err %d ", rv);
1020*205eeb1aSlm66018 		status = EIO;
10214bac2208Snarayan 	}
10224bac2208Snarayan 	rv = ldc_mem_unmap(task->mhdl);
10234bac2208Snarayan 	if (rv) {
1024*205eeb1aSlm66018 		PR0("ldc_mem_unmap() returned err %d ", rv);
1025*205eeb1aSlm66018 		status = EIO;
10264bac2208Snarayan 	}
10274bac2208Snarayan 
1028d10e4ef2Snarayan 	biofini(buf);
1029*205eeb1aSlm66018 
1030d10e4ef2Snarayan 	return (status);
1031d10e4ef2Snarayan }
1032d10e4ef2Snarayan 
1033*205eeb1aSlm66018 /*
1034*205eeb1aSlm66018  * This function should only be called from vd_notify to ensure that requests
1035*205eeb1aSlm66018  * are responded to in the order that they are received.
1036*205eeb1aSlm66018  */
1037d10e4ef2Snarayan static int
1038d10e4ef2Snarayan send_msg(ldc_handle_t ldc_handle, void *msg, size_t msglen)
1039d10e4ef2Snarayan {
10403af08d82Slm66018 	int	status;
1041d10e4ef2Snarayan 	size_t	nbytes;
1042d10e4ef2Snarayan 
10433af08d82Slm66018 	do {
1044d10e4ef2Snarayan 		nbytes = msglen;
1045d10e4ef2Snarayan 		status = ldc_write(ldc_handle, msg, &nbytes);
10463af08d82Slm66018 		if (status != EWOULDBLOCK)
10473af08d82Slm66018 			break;
10483af08d82Slm66018 		drv_usecwait(vds_ldc_delay);
10493af08d82Slm66018 	} while (status == EWOULDBLOCK);
1050d10e4ef2Snarayan 
1051d10e4ef2Snarayan 	if (status != 0) {
10523af08d82Slm66018 		if (status != ECONNRESET)
10533af08d82Slm66018 			PR0("ldc_write() returned errno %d", status);
1054d10e4ef2Snarayan 		return (status);
1055d10e4ef2Snarayan 	} else if (nbytes != msglen) {
10563af08d82Slm66018 		PR0("ldc_write() performed only partial write");
1057d10e4ef2Snarayan 		return (EIO);
1058d10e4ef2Snarayan 	}
1059d10e4ef2Snarayan 
1060d10e4ef2Snarayan 	PR1("SENT %lu bytes", msglen);
1061d10e4ef2Snarayan 	return (0);
1062d10e4ef2Snarayan }
1063d10e4ef2Snarayan 
1064d10e4ef2Snarayan static void
1065d10e4ef2Snarayan vd_need_reset(vd_t *vd, boolean_t reset_ldc)
1066d10e4ef2Snarayan {
1067d10e4ef2Snarayan 	mutex_enter(&vd->lock);
1068d10e4ef2Snarayan 	vd->reset_state	= B_TRUE;
1069d10e4ef2Snarayan 	vd->reset_ldc	= reset_ldc;
1070d10e4ef2Snarayan 	mutex_exit(&vd->lock);
1071d10e4ef2Snarayan }
1072d10e4ef2Snarayan 
1073d10e4ef2Snarayan /*
1074d10e4ef2Snarayan  * Reset the state of the connection with a client, if needed; reset the LDC
1075d10e4ef2Snarayan  * transport as well, if needed.  This function should only be called from the
10763af08d82Slm66018  * "vd_recv_msg", as it waits for tasks - otherwise a deadlock can occur.
1077d10e4ef2Snarayan  */
1078d10e4ef2Snarayan static void
1079d10e4ef2Snarayan vd_reset_if_needed(vd_t *vd)
1080d10e4ef2Snarayan {
1081d10e4ef2Snarayan 	int	status = 0;
1082d10e4ef2Snarayan 
1083d10e4ef2Snarayan 	mutex_enter(&vd->lock);
1084d10e4ef2Snarayan 	if (!vd->reset_state) {
1085d10e4ef2Snarayan 		ASSERT(!vd->reset_ldc);
1086d10e4ef2Snarayan 		mutex_exit(&vd->lock);
1087d10e4ef2Snarayan 		return;
1088d10e4ef2Snarayan 	}
1089d10e4ef2Snarayan 	mutex_exit(&vd->lock);
1090d10e4ef2Snarayan 
1091d10e4ef2Snarayan 	PR0("Resetting connection state with %s", VD_CLIENT(vd));
1092d10e4ef2Snarayan 
1093d10e4ef2Snarayan 	/*
1094d10e4ef2Snarayan 	 * Let any asynchronous I/O complete before possibly pulling the rug
1095d10e4ef2Snarayan 	 * out from under it; defer checking vd->reset_ldc, as one of the
1096d10e4ef2Snarayan 	 * asynchronous tasks might set it
1097d10e4ef2Snarayan 	 */
1098d10e4ef2Snarayan 	ddi_taskq_wait(vd->completionq);
1099d10e4ef2Snarayan 
11003c96341aSnarayan 	if (vd->file) {
11013c96341aSnarayan 		status = VOP_FSYNC(vd->file_vnode, FSYNC, kcred);
11023c96341aSnarayan 		if (status) {
11033c96341aSnarayan 			PR0("VOP_FSYNC returned errno %d", status);
11043c96341aSnarayan 		}
11053c96341aSnarayan 	}
11063c96341aSnarayan 
1107d10e4ef2Snarayan 	if ((vd->initialized & VD_DRING) &&
1108d10e4ef2Snarayan 	    ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0))
11093af08d82Slm66018 		PR0("ldc_mem_dring_unmap() returned errno %d", status);
1110d10e4ef2Snarayan 
11113af08d82Slm66018 	vd_free_dring_task(vd);
11123af08d82Slm66018 
11133af08d82Slm66018 	/* Free the staging buffer for msgs */
11143af08d82Slm66018 	if (vd->vio_msgp != NULL) {
11153af08d82Slm66018 		kmem_free(vd->vio_msgp, vd->max_msglen);
11163af08d82Slm66018 		vd->vio_msgp = NULL;
1117d10e4ef2Snarayan 	}
1118d10e4ef2Snarayan 
11193af08d82Slm66018 	/* Free the inband message buffer */
11203af08d82Slm66018 	if (vd->inband_task.msg != NULL) {
11213af08d82Slm66018 		kmem_free(vd->inband_task.msg, vd->max_msglen);
11223af08d82Slm66018 		vd->inband_task.msg = NULL;
11233af08d82Slm66018 	}
1124d10e4ef2Snarayan 
1125d10e4ef2Snarayan 	mutex_enter(&vd->lock);
11263af08d82Slm66018 
11273af08d82Slm66018 	if (vd->reset_ldc)
11283af08d82Slm66018 		PR0("taking down LDC channel");
1129e1ebb9ecSlm66018 	if (vd->reset_ldc && ((status = ldc_down(vd->ldc_handle)) != 0))
11303af08d82Slm66018 		PR0("ldc_down() returned errno %d", status);
1131d10e4ef2Snarayan 
1132d10e4ef2Snarayan 	vd->initialized	&= ~(VD_SID | VD_SEQ_NUM | VD_DRING);
1133d10e4ef2Snarayan 	vd->state	= VD_STATE_INIT;
1134d10e4ef2Snarayan 	vd->max_msglen	= sizeof (vio_msg_t);	/* baseline vio message size */
1135d10e4ef2Snarayan 
11363af08d82Slm66018 	/* Allocate the staging buffer */
11373af08d82Slm66018 	vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
11383af08d82Slm66018 
11393af08d82Slm66018 	PR0("calling ldc_up\n");
11403af08d82Slm66018 	(void) ldc_up(vd->ldc_handle);
11413af08d82Slm66018 
1142d10e4ef2Snarayan 	vd->reset_state	= B_FALSE;
1143d10e4ef2Snarayan 	vd->reset_ldc	= B_FALSE;
11443af08d82Slm66018 
1145d10e4ef2Snarayan 	mutex_exit(&vd->lock);
1146d10e4ef2Snarayan }
1147d10e4ef2Snarayan 
11483af08d82Slm66018 static void vd_recv_msg(void *arg);
11493af08d82Slm66018 
11503af08d82Slm66018 static void
11513af08d82Slm66018 vd_mark_in_reset(vd_t *vd)
11523af08d82Slm66018 {
11533af08d82Slm66018 	int status;
11543af08d82Slm66018 
11553af08d82Slm66018 	PR0("vd_mark_in_reset: marking vd in reset\n");
11563af08d82Slm66018 
11573af08d82Slm66018 	vd_need_reset(vd, B_FALSE);
11583af08d82Slm66018 	status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, DDI_SLEEP);
11593af08d82Slm66018 	if (status == DDI_FAILURE) {
11603af08d82Slm66018 		PR0("cannot schedule task to recv msg\n");
11613af08d82Slm66018 		vd_need_reset(vd, B_TRUE);
11623af08d82Slm66018 		return;
11633af08d82Slm66018 	}
11643af08d82Slm66018 }
11653af08d82Slm66018 
1166d10e4ef2Snarayan static int
11673c96341aSnarayan vd_mark_elem_done(vd_t *vd, int idx, int elem_status, int elem_nbytes)
1168d10e4ef2Snarayan {
1169d10e4ef2Snarayan 	boolean_t		accepted;
1170d10e4ef2Snarayan 	int			status;
1171d10e4ef2Snarayan 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
1172d10e4ef2Snarayan 
11733af08d82Slm66018 	if (vd->reset_state)
11743af08d82Slm66018 		return (0);
1175d10e4ef2Snarayan 
1176d10e4ef2Snarayan 	/* Acquire the element */
11773af08d82Slm66018 	if (!vd->reset_state &&
11783af08d82Slm66018 	    (status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) {
11793af08d82Slm66018 		if (status == ECONNRESET) {
11803af08d82Slm66018 			vd_mark_in_reset(vd);
11813af08d82Slm66018 			return (0);
11823af08d82Slm66018 		} else {
11833af08d82Slm66018 			PR0("ldc_mem_dring_acquire() returned errno %d",
11843af08d82Slm66018 			    status);
1185d10e4ef2Snarayan 			return (status);
1186d10e4ef2Snarayan 		}
11873af08d82Slm66018 	}
1188d10e4ef2Snarayan 
1189d10e4ef2Snarayan 	/* Set the element's status and mark it done */
1190d10e4ef2Snarayan 	accepted = (elem->hdr.dstate == VIO_DESC_ACCEPTED);
1191d10e4ef2Snarayan 	if (accepted) {
11923c96341aSnarayan 		elem->payload.nbytes	= elem_nbytes;
1193d10e4ef2Snarayan 		elem->payload.status	= elem_status;
1194d10e4ef2Snarayan 		elem->hdr.dstate	= VIO_DESC_DONE;
1195d10e4ef2Snarayan 	} else {
1196d10e4ef2Snarayan 		/* Perhaps client timed out waiting for I/O... */
11973af08d82Slm66018 		PR0("element %u no longer \"accepted\"", idx);
1198d10e4ef2Snarayan 		VD_DUMP_DRING_ELEM(elem);
1199d10e4ef2Snarayan 	}
1200d10e4ef2Snarayan 	/* Release the element */
12013af08d82Slm66018 	if (!vd->reset_state &&
12023af08d82Slm66018 	    (status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) {
12033af08d82Slm66018 		if (status == ECONNRESET) {
12043af08d82Slm66018 			vd_mark_in_reset(vd);
12053af08d82Slm66018 			return (0);
12063af08d82Slm66018 		} else {
12073af08d82Slm66018 			PR0("ldc_mem_dring_release() returned errno %d",
12083af08d82Slm66018 			    status);
1209d10e4ef2Snarayan 			return (status);
1210d10e4ef2Snarayan 		}
12113af08d82Slm66018 	}
1212d10e4ef2Snarayan 
1213d10e4ef2Snarayan 	return (accepted ? 0 : EINVAL);
1214d10e4ef2Snarayan }
1215d10e4ef2Snarayan 
1216*205eeb1aSlm66018 /*
1217*205eeb1aSlm66018  * Return Values
1218*205eeb1aSlm66018  *	0	- operation completed successfully
1219*205eeb1aSlm66018  *	EIO	- encountered LDC / task error
1220*205eeb1aSlm66018  *
1221*205eeb1aSlm66018  * Side Effect
1222*205eeb1aSlm66018  *	sets request->status = <disk operation status>
1223*205eeb1aSlm66018  */
1224*205eeb1aSlm66018 static int
1225*205eeb1aSlm66018 vd_complete_bio(vd_task_t *task)
1226d10e4ef2Snarayan {
1227d10e4ef2Snarayan 	int			status		= 0;
1228*205eeb1aSlm66018 	int			rv		= 0;
1229d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
1230d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
1231d10e4ef2Snarayan 	struct buf		*buf		= &task->buf;
1232d10e4ef2Snarayan 
1233d10e4ef2Snarayan 
1234d10e4ef2Snarayan 	ASSERT(vd != NULL);
1235d10e4ef2Snarayan 	ASSERT(request != NULL);
1236d10e4ef2Snarayan 	ASSERT(task->msg != NULL);
1237d10e4ef2Snarayan 	ASSERT(task->msglen >= sizeof (*task->msg));
12383c96341aSnarayan 	ASSERT(!vd->file);
1239*205eeb1aSlm66018 	ASSERT(request->slice != VD_SLICE_NONE);
1240d10e4ef2Snarayan 
1241*205eeb1aSlm66018 	/* Wait for the I/O to complete [ call to ldi_strategy(9f) ] */
1242d10e4ef2Snarayan 	request->status = biowait(buf);
1243d10e4ef2Snarayan 
12443c96341aSnarayan 	/* return back the number of bytes read/written */
12453c96341aSnarayan 	request->nbytes = buf->b_bcount - buf->b_resid;
12463c96341aSnarayan 
12474bac2208Snarayan 	/* Release the buffer */
12483af08d82Slm66018 	if (!vd->reset_state)
12494bac2208Snarayan 		status = ldc_mem_release(task->mhdl, 0, buf->b_bcount);
12504bac2208Snarayan 	if (status) {
12513af08d82Slm66018 		PR0("ldc_mem_release() returned errno %d copying to "
12523af08d82Slm66018 		    "client", status);
12533af08d82Slm66018 		if (status == ECONNRESET) {
12543af08d82Slm66018 			vd_mark_in_reset(vd);
12553af08d82Slm66018 		}
1256*205eeb1aSlm66018 		rv = EIO;
12571ae08745Sheppo 	}
12581ae08745Sheppo 
12593af08d82Slm66018 	/* Unmap the memory, even if in reset */
12604bac2208Snarayan 	status = ldc_mem_unmap(task->mhdl);
12614bac2208Snarayan 	if (status) {
12623af08d82Slm66018 		PR0("ldc_mem_unmap() returned errno %d copying to client",
12634bac2208Snarayan 		    status);
12643af08d82Slm66018 		if (status == ECONNRESET) {
12653af08d82Slm66018 			vd_mark_in_reset(vd);
12663af08d82Slm66018 		}
1267*205eeb1aSlm66018 		rv = EIO;
12684bac2208Snarayan 	}
12694bac2208Snarayan 
1270d10e4ef2Snarayan 	biofini(buf);
12711ae08745Sheppo 
1272*205eeb1aSlm66018 	return (rv);
1273*205eeb1aSlm66018 }
1274*205eeb1aSlm66018 
1275*205eeb1aSlm66018 /*
1276*205eeb1aSlm66018  * Description:
1277*205eeb1aSlm66018  *	This function is called by the two functions called by a taskq
1278*205eeb1aSlm66018  *	[ vd_complete_notify() and vd_serial_notify()) ] to send the
1279*205eeb1aSlm66018  *	message to the client.
1280*205eeb1aSlm66018  *
1281*205eeb1aSlm66018  * Parameters:
1282*205eeb1aSlm66018  *	arg 	- opaque pointer to structure containing task to be completed
1283*205eeb1aSlm66018  *
1284*205eeb1aSlm66018  * Return Values
1285*205eeb1aSlm66018  *	None
1286*205eeb1aSlm66018  */
1287*205eeb1aSlm66018 static void
1288*205eeb1aSlm66018 vd_notify(vd_task_t *task)
1289*205eeb1aSlm66018 {
1290*205eeb1aSlm66018 	int	status;
1291*205eeb1aSlm66018 
1292*205eeb1aSlm66018 	ASSERT(task != NULL);
1293*205eeb1aSlm66018 	ASSERT(task->vd != NULL);
1294*205eeb1aSlm66018 
1295*205eeb1aSlm66018 	if (task->vd->reset_state)
1296*205eeb1aSlm66018 		return;
1297*205eeb1aSlm66018 
1298*205eeb1aSlm66018 	/*
1299*205eeb1aSlm66018 	 * Send the "ack" or "nack" back to the client; if sending the message
1300*205eeb1aSlm66018 	 * via LDC fails, arrange to reset both the connection state and LDC
1301*205eeb1aSlm66018 	 * itself
1302*205eeb1aSlm66018 	 */
1303*205eeb1aSlm66018 	PR2("Sending %s",
1304*205eeb1aSlm66018 	    (task->msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK");
1305*205eeb1aSlm66018 
1306*205eeb1aSlm66018 	status = send_msg(task->vd->ldc_handle, task->msg, task->msglen);
1307*205eeb1aSlm66018 	switch (status) {
1308*205eeb1aSlm66018 	case 0:
1309*205eeb1aSlm66018 		break;
1310*205eeb1aSlm66018 	case ECONNRESET:
1311*205eeb1aSlm66018 		vd_mark_in_reset(task->vd);
1312*205eeb1aSlm66018 		break;
1313*205eeb1aSlm66018 	default:
1314*205eeb1aSlm66018 		PR0("initiating full reset");
1315*205eeb1aSlm66018 		vd_need_reset(task->vd, B_TRUE);
1316*205eeb1aSlm66018 		break;
1317*205eeb1aSlm66018 	}
1318*205eeb1aSlm66018 
1319*205eeb1aSlm66018 	DTRACE_PROBE1(task__end, vd_task_t *, task);
1320*205eeb1aSlm66018 }
1321*205eeb1aSlm66018 
1322*205eeb1aSlm66018 /*
1323*205eeb1aSlm66018  * Description:
1324*205eeb1aSlm66018  *	Mark the Dring entry as Done and (if necessary) send an ACK/NACK to
1325*205eeb1aSlm66018  *	the vDisk client
1326*205eeb1aSlm66018  *
1327*205eeb1aSlm66018  * Parameters:
1328*205eeb1aSlm66018  *	task 		- structure containing the request sent from client
1329*205eeb1aSlm66018  *
1330*205eeb1aSlm66018  * Return Values
1331*205eeb1aSlm66018  *	None
1332*205eeb1aSlm66018  */
1333*205eeb1aSlm66018 static void
1334*205eeb1aSlm66018 vd_complete_notify(vd_task_t *task)
1335*205eeb1aSlm66018 {
1336*205eeb1aSlm66018 	int			status		= 0;
1337*205eeb1aSlm66018 	vd_t			*vd		= task->vd;
1338*205eeb1aSlm66018 	vd_dring_payload_t	*request	= task->request;
1339*205eeb1aSlm66018 
1340d10e4ef2Snarayan 	/* Update the dring element for a dring client */
1341*205eeb1aSlm66018 	if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE)) {
13423c96341aSnarayan 		status = vd_mark_elem_done(vd, task->index,
13433c96341aSnarayan 		    request->status, request->nbytes);
13443af08d82Slm66018 		if (status == ECONNRESET)
13453af08d82Slm66018 			vd_mark_in_reset(vd);
13463af08d82Slm66018 	}
13471ae08745Sheppo 
1348d10e4ef2Snarayan 	/*
1349*205eeb1aSlm66018 	 * If a transport error occurred while marking the element done or
1350*205eeb1aSlm66018 	 * previously while executing the task, arrange to "nack" the message
1351*205eeb1aSlm66018 	 * when the final task in the descriptor element range completes
1352d10e4ef2Snarayan 	 */
1353*205eeb1aSlm66018 	if ((status != 0) || (task->status != 0))
1354d10e4ef2Snarayan 		task->msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
13551ae08745Sheppo 
1356d10e4ef2Snarayan 	/*
1357d10e4ef2Snarayan 	 * Only the final task for a range of elements will respond to and
1358d10e4ef2Snarayan 	 * free the message
1359d10e4ef2Snarayan 	 */
13603af08d82Slm66018 	if (task->type == VD_NONFINAL_RANGE_TASK) {
1361d10e4ef2Snarayan 		return;
13623af08d82Slm66018 	}
13631ae08745Sheppo 
1364*205eeb1aSlm66018 	vd_notify(task);
1365*205eeb1aSlm66018 }
1366*205eeb1aSlm66018 
1367d10e4ef2Snarayan /*
1368*205eeb1aSlm66018  * Description:
1369*205eeb1aSlm66018  *	This is the basic completion function called to handle inband data
1370*205eeb1aSlm66018  *	requests and handshake messages. All it needs to do is trigger a
1371*205eeb1aSlm66018  *	message to the client that the request is completed.
1372*205eeb1aSlm66018  *
1373*205eeb1aSlm66018  * Parameters:
1374*205eeb1aSlm66018  *	arg 	- opaque pointer to structure containing task to be completed
1375*205eeb1aSlm66018  *
1376*205eeb1aSlm66018  * Return Values
1377*205eeb1aSlm66018  *	None
1378d10e4ef2Snarayan  */
1379*205eeb1aSlm66018 static void
1380*205eeb1aSlm66018 vd_serial_notify(void *arg)
1381*205eeb1aSlm66018 {
1382*205eeb1aSlm66018 	vd_task_t		*task = (vd_task_t *)arg;
1383*205eeb1aSlm66018 
1384*205eeb1aSlm66018 	ASSERT(task != NULL);
1385*205eeb1aSlm66018 	vd_notify(task);
13861ae08745Sheppo }
13871ae08745Sheppo 
13880a55fbb7Slm66018 static void
13890a55fbb7Slm66018 vd_geom2dk_geom(void *vd_buf, void *ioctl_arg)
13900a55fbb7Slm66018 {
13910a55fbb7Slm66018 	VD_GEOM2DK_GEOM((vd_geom_t *)vd_buf, (struct dk_geom *)ioctl_arg);
13920a55fbb7Slm66018 }
13930a55fbb7Slm66018 
13940a55fbb7Slm66018 static void
13950a55fbb7Slm66018 vd_vtoc2vtoc(void *vd_buf, void *ioctl_arg)
13960a55fbb7Slm66018 {
13970a55fbb7Slm66018 	VD_VTOC2VTOC((vd_vtoc_t *)vd_buf, (struct vtoc *)ioctl_arg);
13980a55fbb7Slm66018 }
13990a55fbb7Slm66018 
14000a55fbb7Slm66018 static void
14010a55fbb7Slm66018 dk_geom2vd_geom(void *ioctl_arg, void *vd_buf)
14020a55fbb7Slm66018 {
14030a55fbb7Slm66018 	DK_GEOM2VD_GEOM((struct dk_geom *)ioctl_arg, (vd_geom_t *)vd_buf);
14040a55fbb7Slm66018 }
14050a55fbb7Slm66018 
14060a55fbb7Slm66018 static void
14070a55fbb7Slm66018 vtoc2vd_vtoc(void *ioctl_arg, void *vd_buf)
14080a55fbb7Slm66018 {
14090a55fbb7Slm66018 	VTOC2VD_VTOC((struct vtoc *)ioctl_arg, (vd_vtoc_t *)vd_buf);
14100a55fbb7Slm66018 }
14110a55fbb7Slm66018 
14124bac2208Snarayan static void
14134bac2208Snarayan vd_get_efi_in(void *vd_buf, void *ioctl_arg)
14144bac2208Snarayan {
14154bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
14164bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
14174bac2208Snarayan 
14184bac2208Snarayan 	dk_efi->dki_lba = vd_efi->lba;
14194bac2208Snarayan 	dk_efi->dki_length = vd_efi->length;
14204bac2208Snarayan 	dk_efi->dki_data = kmem_zalloc(vd_efi->length, KM_SLEEP);
14214bac2208Snarayan }
14224bac2208Snarayan 
14234bac2208Snarayan static void
14244bac2208Snarayan vd_get_efi_out(void *ioctl_arg, void *vd_buf)
14254bac2208Snarayan {
14264bac2208Snarayan 	int len;
14274bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
14284bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
14294bac2208Snarayan 
14304bac2208Snarayan 	len = vd_efi->length;
14314bac2208Snarayan 	DK_EFI2VD_EFI(dk_efi, vd_efi);
14324bac2208Snarayan 	kmem_free(dk_efi->dki_data, len);
14334bac2208Snarayan }
14344bac2208Snarayan 
14354bac2208Snarayan static void
14364bac2208Snarayan vd_set_efi_in(void *vd_buf, void *ioctl_arg)
14374bac2208Snarayan {
14384bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
14394bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
14404bac2208Snarayan 
14414bac2208Snarayan 	dk_efi->dki_data = kmem_alloc(vd_efi->length, KM_SLEEP);
14424bac2208Snarayan 	VD_EFI2DK_EFI(vd_efi, dk_efi);
14434bac2208Snarayan }
14444bac2208Snarayan 
14454bac2208Snarayan static void
14464bac2208Snarayan vd_set_efi_out(void *ioctl_arg, void *vd_buf)
14474bac2208Snarayan {
14484bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
14494bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
14504bac2208Snarayan 
14514bac2208Snarayan 	kmem_free(dk_efi->dki_data, vd_efi->length);
14524bac2208Snarayan }
14534bac2208Snarayan 
14544bac2208Snarayan static int
14554bac2208Snarayan vd_read_vtoc(ldi_handle_t handle, struct vtoc *vtoc, vd_disk_label_t *label)
14564bac2208Snarayan {
14574bac2208Snarayan 	int status, rval;
14584bac2208Snarayan 	struct dk_gpt *efi;
14594bac2208Snarayan 	size_t efi_len;
14604bac2208Snarayan 
14614bac2208Snarayan 	*label = VD_DISK_LABEL_UNK;
14624bac2208Snarayan 
14634bac2208Snarayan 	status = ldi_ioctl(handle, DKIOCGVTOC, (intptr_t)vtoc,
14644bac2208Snarayan 	    (vd_open_flags | FKIOCTL), kcred, &rval);
14654bac2208Snarayan 
14664bac2208Snarayan 	if (status == 0) {
14674bac2208Snarayan 		*label = VD_DISK_LABEL_VTOC;
14684bac2208Snarayan 		return (0);
14694bac2208Snarayan 	} else if (status != ENOTSUP) {
14703af08d82Slm66018 		PR0("ldi_ioctl(DKIOCGVTOC) returned error %d", status);
14714bac2208Snarayan 		return (status);
14724bac2208Snarayan 	}
14734bac2208Snarayan 
14744bac2208Snarayan 	status = vds_efi_alloc_and_read(handle, &efi, &efi_len);
14754bac2208Snarayan 
14764bac2208Snarayan 	if (status) {
14773af08d82Slm66018 		PR0("vds_efi_alloc_and_read returned error %d", status);
14784bac2208Snarayan 		return (status);
14794bac2208Snarayan 	}
14804bac2208Snarayan 
14814bac2208Snarayan 	*label = VD_DISK_LABEL_EFI;
14824bac2208Snarayan 	vd_efi_to_vtoc(efi, vtoc);
14834bac2208Snarayan 	vd_efi_free(efi, efi_len);
14844bac2208Snarayan 
14854bac2208Snarayan 	return (0);
14864bac2208Snarayan }
14874bac2208Snarayan 
1488690555a1Sachartre static ushort_t
14893c96341aSnarayan vd_lbl2cksum(struct dk_label *label)
14903c96341aSnarayan {
14913c96341aSnarayan 	int	count;
1492690555a1Sachartre 	ushort_t sum, *sp;
14933c96341aSnarayan 
14943c96341aSnarayan 	count =	(sizeof (struct dk_label)) / (sizeof (short)) - 1;
1495690555a1Sachartre 	sp = (ushort_t *)label;
14963c96341aSnarayan 	sum = 0;
14973c96341aSnarayan 	while (count--) {
14983c96341aSnarayan 		sum ^= *sp++;
14993c96341aSnarayan 	}
15003c96341aSnarayan 
15013c96341aSnarayan 	return (sum);
15023c96341aSnarayan }
15033c96341aSnarayan 
150487a7269eSachartre /*
150587a7269eSachartre  * Handle ioctls to a disk slice.
1506*205eeb1aSlm66018  *
1507*205eeb1aSlm66018  * Return Values
1508*205eeb1aSlm66018  *	0	- Indicates that there are no errors in disk operations
1509*205eeb1aSlm66018  *	ENOTSUP	- Unknown disk label type or unsupported DKIO ioctl
1510*205eeb1aSlm66018  *	EINVAL	- Not enough room to copy the EFI label
1511*205eeb1aSlm66018  *
151287a7269eSachartre  */
15131ae08745Sheppo static int
15140a55fbb7Slm66018 vd_do_slice_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
15151ae08745Sheppo {
15164bac2208Snarayan 	dk_efi_t *dk_ioc;
15174bac2208Snarayan 
15184bac2208Snarayan 	switch (vd->vdisk_label) {
15194bac2208Snarayan 
152087a7269eSachartre 	/* ioctls for a slice from a disk with a VTOC label */
15214bac2208Snarayan 	case VD_DISK_LABEL_VTOC:
15224bac2208Snarayan 
15231ae08745Sheppo 		switch (cmd) {
15241ae08745Sheppo 		case DKIOCGGEOM:
15250a55fbb7Slm66018 			ASSERT(ioctl_arg != NULL);
15260a55fbb7Slm66018 			bcopy(&vd->dk_geom, ioctl_arg, sizeof (vd->dk_geom));
15271ae08745Sheppo 			return (0);
15281ae08745Sheppo 		case DKIOCGVTOC:
15290a55fbb7Slm66018 			ASSERT(ioctl_arg != NULL);
15300a55fbb7Slm66018 			bcopy(&vd->vtoc, ioctl_arg, sizeof (vd->vtoc));
15311ae08745Sheppo 			return (0);
153287a7269eSachartre 		default:
15333c96341aSnarayan 			return (ENOTSUP);
153487a7269eSachartre 		}
153587a7269eSachartre 
153687a7269eSachartre 	/* ioctls for a slice from a disk with an EFI label */
153787a7269eSachartre 	case VD_DISK_LABEL_EFI:
153887a7269eSachartre 
153987a7269eSachartre 		switch (cmd) {
154087a7269eSachartre 		case DKIOCGETEFI:
15413c96341aSnarayan 			ASSERT(ioctl_arg != NULL);
154287a7269eSachartre 			dk_ioc = (dk_efi_t *)ioctl_arg;
154387a7269eSachartre 			if (dk_ioc->dki_length < vd->dk_efi.dki_length)
154487a7269eSachartre 				return (EINVAL);
154587a7269eSachartre 			bcopy(vd->dk_efi.dki_data, dk_ioc->dki_data,
154687a7269eSachartre 			    vd->dk_efi.dki_length);
154787a7269eSachartre 			return (0);
154887a7269eSachartre 		default:
154987a7269eSachartre 			return (ENOTSUP);
155087a7269eSachartre 		}
155187a7269eSachartre 
155287a7269eSachartre 	default:
1553*205eeb1aSlm66018 		/* Unknown disk label type */
155487a7269eSachartre 		return (ENOTSUP);
155587a7269eSachartre 	}
155687a7269eSachartre }
155787a7269eSachartre 
155887a7269eSachartre /*
1559*205eeb1aSlm66018  * Handle ioctls to a disk image (file-based).
1560*205eeb1aSlm66018  *
1561*205eeb1aSlm66018  * Return Values
1562*205eeb1aSlm66018  *	0	- Indicates that there are no errors
1563*205eeb1aSlm66018  *	!= 0	- Disk operation returned an error
156487a7269eSachartre  */
156587a7269eSachartre static int
156687a7269eSachartre vd_do_file_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
156787a7269eSachartre {
156887a7269eSachartre 	struct dk_label label;
156987a7269eSachartre 	struct dk_geom *geom;
157087a7269eSachartre 	struct vtoc *vtoc;
157187a7269eSachartre 	int i, rc;
157287a7269eSachartre 
157387a7269eSachartre 	ASSERT(vd->file);
157487a7269eSachartre 	ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
157587a7269eSachartre 
157687a7269eSachartre 	switch (cmd) {
157787a7269eSachartre 
157887a7269eSachartre 	case DKIOCGGEOM:
157987a7269eSachartre 		ASSERT(ioctl_arg != NULL);
158087a7269eSachartre 		geom = (struct dk_geom *)ioctl_arg;
158187a7269eSachartre 
158287a7269eSachartre 		if (VD_FILE_LABEL_READ(vd, &label) < 0)
158387a7269eSachartre 			return (EIO);
158487a7269eSachartre 
158587a7269eSachartre 		if (label.dkl_magic != DKL_MAGIC ||
158687a7269eSachartre 		    label.dkl_cksum != vd_lbl2cksum(&label))
158787a7269eSachartre 			return (EINVAL);
158887a7269eSachartre 
158987a7269eSachartre 		bzero(geom, sizeof (struct dk_geom));
159087a7269eSachartre 		geom->dkg_ncyl = label.dkl_ncyl;
159187a7269eSachartre 		geom->dkg_acyl = label.dkl_acyl;
159287a7269eSachartre 		geom->dkg_nhead = label.dkl_nhead;
159387a7269eSachartre 		geom->dkg_nsect = label.dkl_nsect;
159487a7269eSachartre 		geom->dkg_intrlv = label.dkl_intrlv;
159587a7269eSachartre 		geom->dkg_apc = label.dkl_apc;
159687a7269eSachartre 		geom->dkg_rpm = label.dkl_rpm;
159787a7269eSachartre 		geom->dkg_pcyl = label.dkl_pcyl;
159887a7269eSachartre 		geom->dkg_write_reinstruct = label.dkl_write_reinstruct;
159987a7269eSachartre 		geom->dkg_read_reinstruct = label.dkl_read_reinstruct;
160087a7269eSachartre 
160187a7269eSachartre 		return (0);
160287a7269eSachartre 
160387a7269eSachartre 	case DKIOCGVTOC:
160487a7269eSachartre 		ASSERT(ioctl_arg != NULL);
160587a7269eSachartre 		vtoc = (struct vtoc *)ioctl_arg;
160687a7269eSachartre 
160787a7269eSachartre 		if (VD_FILE_LABEL_READ(vd, &label) < 0)
160887a7269eSachartre 			return (EIO);
160987a7269eSachartre 
161087a7269eSachartre 		if (label.dkl_magic != DKL_MAGIC ||
161187a7269eSachartre 		    label.dkl_cksum != vd_lbl2cksum(&label))
161287a7269eSachartre 			return (EINVAL);
161387a7269eSachartre 
161487a7269eSachartre 		bzero(vtoc, sizeof (struct vtoc));
161587a7269eSachartre 
161687a7269eSachartre 		vtoc->v_sanity = label.dkl_vtoc.v_sanity;
161787a7269eSachartre 		vtoc->v_version = label.dkl_vtoc.v_version;
161887a7269eSachartre 		vtoc->v_sectorsz = DEV_BSIZE;
161987a7269eSachartre 		vtoc->v_nparts = label.dkl_vtoc.v_nparts;
162087a7269eSachartre 
162187a7269eSachartre 		for (i = 0; i < vtoc->v_nparts; i++) {
162287a7269eSachartre 			vtoc->v_part[i].p_tag =
162387a7269eSachartre 			    label.dkl_vtoc.v_part[i].p_tag;
162487a7269eSachartre 			vtoc->v_part[i].p_flag =
162587a7269eSachartre 			    label.dkl_vtoc.v_part[i].p_flag;
162687a7269eSachartre 			vtoc->v_part[i].p_start =
162787a7269eSachartre 			    label.dkl_map[i].dkl_cylno *
162887a7269eSachartre 			    (label.dkl_nhead * label.dkl_nsect);
162987a7269eSachartre 			vtoc->v_part[i].p_size = label.dkl_map[i].dkl_nblk;
163087a7269eSachartre 			vtoc->timestamp[i] =
163187a7269eSachartre 			    label.dkl_vtoc.v_timestamp[i];
163287a7269eSachartre 		}
163387a7269eSachartre 		/*
163487a7269eSachartre 		 * The bootinfo array can not be copied with bcopy() because
163587a7269eSachartre 		 * elements are of type long in vtoc (so 64-bit) and of type
163687a7269eSachartre 		 * int in dk_vtoc (so 32-bit).
163787a7269eSachartre 		 */
163887a7269eSachartre 		vtoc->v_bootinfo[0] = label.dkl_vtoc.v_bootinfo[0];
163987a7269eSachartre 		vtoc->v_bootinfo[1] = label.dkl_vtoc.v_bootinfo[1];
164087a7269eSachartre 		vtoc->v_bootinfo[2] = label.dkl_vtoc.v_bootinfo[2];
164187a7269eSachartre 		bcopy(label.dkl_asciilabel, vtoc->v_asciilabel,
164287a7269eSachartre 		    LEN_DKL_ASCII);
164387a7269eSachartre 		bcopy(label.dkl_vtoc.v_volume, vtoc->v_volume,
164487a7269eSachartre 		    LEN_DKL_VVOL);
164587a7269eSachartre 
164687a7269eSachartre 		return (0);
164787a7269eSachartre 
164887a7269eSachartre 	case DKIOCSGEOM:
164987a7269eSachartre 		ASSERT(ioctl_arg != NULL);
165087a7269eSachartre 		geom = (struct dk_geom *)ioctl_arg;
165187a7269eSachartre 
165287a7269eSachartre 		if (geom->dkg_nhead == 0 || geom->dkg_nsect == 0)
165387a7269eSachartre 			return (EINVAL);
165487a7269eSachartre 
165587a7269eSachartre 		/*
165687a7269eSachartre 		 * The current device geometry is not updated, just the driver
165787a7269eSachartre 		 * "notion" of it. The device geometry will be effectively
165887a7269eSachartre 		 * updated when a label is written to the device during a next
165987a7269eSachartre 		 * DKIOCSVTOC.
166087a7269eSachartre 		 */
166187a7269eSachartre 		bcopy(ioctl_arg, &vd->dk_geom, sizeof (vd->dk_geom));
166287a7269eSachartre 		return (0);
166387a7269eSachartre 
166487a7269eSachartre 	case DKIOCSVTOC:
166587a7269eSachartre 		ASSERT(ioctl_arg != NULL);
166687a7269eSachartre 		ASSERT(vd->dk_geom.dkg_nhead != 0 &&
166787a7269eSachartre 		    vd->dk_geom.dkg_nsect != 0);
1668690555a1Sachartre 		vtoc = (struct vtoc *)ioctl_arg;
1669690555a1Sachartre 
1670690555a1Sachartre 		if (vtoc->v_sanity != VTOC_SANE ||
1671690555a1Sachartre 		    vtoc->v_sectorsz != DEV_BSIZE ||
1672690555a1Sachartre 		    vtoc->v_nparts != V_NUMPAR)
1673690555a1Sachartre 			return (EINVAL);
1674690555a1Sachartre 
1675690555a1Sachartre 		bzero(&label, sizeof (label));
1676690555a1Sachartre 		label.dkl_ncyl = vd->dk_geom.dkg_ncyl;
1677690555a1Sachartre 		label.dkl_acyl = vd->dk_geom.dkg_acyl;
1678690555a1Sachartre 		label.dkl_pcyl = vd->dk_geom.dkg_pcyl;
1679690555a1Sachartre 		label.dkl_nhead = vd->dk_geom.dkg_nhead;
1680690555a1Sachartre 		label.dkl_nsect = vd->dk_geom.dkg_nsect;
1681690555a1Sachartre 		label.dkl_intrlv = vd->dk_geom.dkg_intrlv;
1682690555a1Sachartre 		label.dkl_apc = vd->dk_geom.dkg_apc;
1683690555a1Sachartre 		label.dkl_rpm = vd->dk_geom.dkg_rpm;
168487a7269eSachartre 		label.dkl_write_reinstruct = vd->dk_geom.dkg_write_reinstruct;
168587a7269eSachartre 		label.dkl_read_reinstruct = vd->dk_geom.dkg_read_reinstruct;
1686690555a1Sachartre 
168787a7269eSachartre 		label.dkl_vtoc.v_nparts = V_NUMPAR;
168887a7269eSachartre 		label.dkl_vtoc.v_sanity = VTOC_SANE;
1689690555a1Sachartre 		label.dkl_vtoc.v_version = vtoc->v_version;
169087a7269eSachartre 		for (i = 0; i < V_NUMPAR; i++) {
1691690555a1Sachartre 			label.dkl_vtoc.v_timestamp[i] =
1692690555a1Sachartre 			    vtoc->timestamp[i];
1693690555a1Sachartre 			label.dkl_vtoc.v_part[i].p_tag =
1694690555a1Sachartre 			    vtoc->v_part[i].p_tag;
1695690555a1Sachartre 			label.dkl_vtoc.v_part[i].p_flag =
1696690555a1Sachartre 			    vtoc->v_part[i].p_flag;
1697690555a1Sachartre 			label.dkl_map[i].dkl_cylno =
1698690555a1Sachartre 			    vtoc->v_part[i].p_start /
1699690555a1Sachartre 			    (label.dkl_nhead * label.dkl_nsect);
1700690555a1Sachartre 			label.dkl_map[i].dkl_nblk =
1701690555a1Sachartre 			    vtoc->v_part[i].p_size;
17023c96341aSnarayan 		}
170387a7269eSachartre 		/*
170487a7269eSachartre 		 * The bootinfo array can not be copied with bcopy() because
170587a7269eSachartre 		 * elements are of type long in vtoc (so 64-bit) and of type
170687a7269eSachartre 		 * int in dk_vtoc (so 32-bit).
170787a7269eSachartre 		 */
170887a7269eSachartre 		label.dkl_vtoc.v_bootinfo[0] = vtoc->v_bootinfo[0];
170987a7269eSachartre 		label.dkl_vtoc.v_bootinfo[1] = vtoc->v_bootinfo[1];
171087a7269eSachartre 		label.dkl_vtoc.v_bootinfo[2] = vtoc->v_bootinfo[2];
1711690555a1Sachartre 		bcopy(vtoc->v_asciilabel, label.dkl_asciilabel,
1712690555a1Sachartre 		    LEN_DKL_ASCII);
1713690555a1Sachartre 		bcopy(vtoc->v_volume, label.dkl_vtoc.v_volume,
1714690555a1Sachartre 		    LEN_DKL_VVOL);
17153c96341aSnarayan 
17163c96341aSnarayan 		/* re-compute checksum */
1717690555a1Sachartre 		label.dkl_magic = DKL_MAGIC;
1718690555a1Sachartre 		label.dkl_cksum = vd_lbl2cksum(&label);
1719690555a1Sachartre 
172087a7269eSachartre 		/* write label to the disk image */
172187a7269eSachartre 		if ((rc = vd_file_set_vtoc(vd, &label)) != 0)
172287a7269eSachartre 			return (rc);
1723690555a1Sachartre 
1724690555a1Sachartre 		/* update the cached vdisk VTOC */
1725690555a1Sachartre 		bcopy(vtoc, &vd->vtoc, sizeof (vd->vtoc));
17263c96341aSnarayan 
172787a7269eSachartre 		/*
172887a7269eSachartre 		 * The disk geometry may have changed, so we need to write
172987a7269eSachartre 		 * the devid (if there is one) so that it is stored at the
173087a7269eSachartre 		 * right location.
173187a7269eSachartre 		 */
173287a7269eSachartre 		if (vd->file_devid != NULL &&
173387a7269eSachartre 		    vd_file_write_devid(vd, vd->file_devid) != 0) {
173487a7269eSachartre 			PR0("Fail to write devid");
17351ae08745Sheppo 		}
17364bac2208Snarayan 
17374bac2208Snarayan 		return (0);
17384bac2208Snarayan 
17394bac2208Snarayan 	default:
17404bac2208Snarayan 		return (ENOTSUP);
17414bac2208Snarayan 	}
17421ae08745Sheppo }
17431ae08745Sheppo 
1744*205eeb1aSlm66018 /*
1745*205eeb1aSlm66018  * Description:
1746*205eeb1aSlm66018  *	This is the function that processes the ioctl requests (farming it
1747*205eeb1aSlm66018  *	out to functions that handle slices, files or whole disks)
1748*205eeb1aSlm66018  *
1749*205eeb1aSlm66018  * Return Values
1750*205eeb1aSlm66018  *     0		- ioctl operation completed successfully
1751*205eeb1aSlm66018  *     != 0		- The LDC error value encountered
1752*205eeb1aSlm66018  *			  (propagated back up the call stack as a task error)
1753*205eeb1aSlm66018  *
1754*205eeb1aSlm66018  * Side Effect
1755*205eeb1aSlm66018  *     sets request->status to the return value of the ioctl function.
1756*205eeb1aSlm66018  */
17571ae08745Sheppo static int
17580a55fbb7Slm66018 vd_do_ioctl(vd_t *vd, vd_dring_payload_t *request, void* buf, vd_ioctl_t *ioctl)
17591ae08745Sheppo {
1760*205eeb1aSlm66018 	int	rval = 0, status = 0;
17611ae08745Sheppo 	size_t	nbytes = request->nbytes;	/* modifiable copy */
17621ae08745Sheppo 
17631ae08745Sheppo 
17641ae08745Sheppo 	ASSERT(request->slice < vd->nslices);
17651ae08745Sheppo 	PR0("Performing %s", ioctl->operation_name);
17661ae08745Sheppo 
17670a55fbb7Slm66018 	/* Get data from client and convert, if necessary */
17680a55fbb7Slm66018 	if (ioctl->copyin != NULL)  {
17691ae08745Sheppo 		ASSERT(nbytes != 0 && buf != NULL);
17701ae08745Sheppo 		PR1("Getting \"arg\" data from client");
17711ae08745Sheppo 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
17721ae08745Sheppo 		    request->cookie, request->ncookies,
17731ae08745Sheppo 		    LDC_COPY_IN)) != 0) {
17743af08d82Slm66018 			PR0("ldc_mem_copy() returned errno %d "
17751ae08745Sheppo 			    "copying from client", status);
17761ae08745Sheppo 			return (status);
17771ae08745Sheppo 		}
17780a55fbb7Slm66018 
17790a55fbb7Slm66018 		/* Convert client's data, if necessary */
17800a55fbb7Slm66018 		if (ioctl->copyin == VD_IDENTITY)	/* use client buffer */
17810a55fbb7Slm66018 			ioctl->arg = buf;
17820a55fbb7Slm66018 		else	/* convert client vdisk operation data to ioctl data */
17830a55fbb7Slm66018 			(ioctl->copyin)(buf, (void *)ioctl->arg);
17841ae08745Sheppo 	}
17851ae08745Sheppo 
17861ae08745Sheppo 	/*
17871ae08745Sheppo 	 * Handle single-slice block devices internally; otherwise, have the
17881ae08745Sheppo 	 * real driver perform the ioctl()
17891ae08745Sheppo 	 */
179087a7269eSachartre 	if (vd->file) {
1791*205eeb1aSlm66018 		request->status =
1792*205eeb1aSlm66018 		    vd_do_file_ioctl(vd, ioctl->cmd, (void *)ioctl->arg);
1793*205eeb1aSlm66018 
179487a7269eSachartre 	} else if (vd->vdisk_type == VD_DISK_TYPE_SLICE && !vd->pseudo) {
1795*205eeb1aSlm66018 		request->status =
1796*205eeb1aSlm66018 		    vd_do_slice_ioctl(vd, ioctl->cmd, (void *)ioctl->arg);
1797*205eeb1aSlm66018 
1798*205eeb1aSlm66018 	} else {
1799*205eeb1aSlm66018 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
1800d10e4ef2Snarayan 		    ioctl->cmd, (intptr_t)ioctl->arg, (vd_open_flags | FKIOCTL),
1801*205eeb1aSlm66018 		    kcred, &rval);
1802*205eeb1aSlm66018 
18031ae08745Sheppo #ifdef DEBUG
18041ae08745Sheppo 		if (rval != 0) {
1805*205eeb1aSlm66018 			PR0("%s set rval = %d, which is not being returned to"
1806*205eeb1aSlm66018 			    " client", ioctl->cmd_name, rval);
18071ae08745Sheppo 		}
18081ae08745Sheppo #endif /* DEBUG */
1809*205eeb1aSlm66018 	}
1810*205eeb1aSlm66018 
1811*205eeb1aSlm66018 	if (request->status != 0) {
1812*205eeb1aSlm66018 		PR0("ioctl(%s) = errno %d", ioctl->cmd_name, request->status);
1813*205eeb1aSlm66018 		return (0);
1814*205eeb1aSlm66018 	}
18151ae08745Sheppo 
18160a55fbb7Slm66018 	/* Convert data and send to client, if necessary */
18170a55fbb7Slm66018 	if (ioctl->copyout != NULL)  {
18181ae08745Sheppo 		ASSERT(nbytes != 0 && buf != NULL);
18191ae08745Sheppo 		PR1("Sending \"arg\" data to client");
18200a55fbb7Slm66018 
18210a55fbb7Slm66018 		/* Convert ioctl data to vdisk operation data, if necessary */
18220a55fbb7Slm66018 		if (ioctl->copyout != VD_IDENTITY)
18230a55fbb7Slm66018 			(ioctl->copyout)((void *)ioctl->arg, buf);
18240a55fbb7Slm66018 
18251ae08745Sheppo 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
18261ae08745Sheppo 		    request->cookie, request->ncookies,
18271ae08745Sheppo 		    LDC_COPY_OUT)) != 0) {
18283af08d82Slm66018 			PR0("ldc_mem_copy() returned errno %d "
18291ae08745Sheppo 			    "copying to client", status);
18301ae08745Sheppo 			return (status);
18311ae08745Sheppo 		}
18321ae08745Sheppo 	}
18331ae08745Sheppo 
18341ae08745Sheppo 	return (status);
18351ae08745Sheppo }
18361ae08745Sheppo 
18371ae08745Sheppo #define	RNDSIZE(expr) P2ROUNDUP(sizeof (expr), sizeof (uint64_t))
1838*205eeb1aSlm66018 
1839*205eeb1aSlm66018 /*
1840*205eeb1aSlm66018  * Description:
1841*205eeb1aSlm66018  *	This generic function is called by the task queue to complete
1842*205eeb1aSlm66018  *	the processing of the tasks. The specific completion function
1843*205eeb1aSlm66018  *	is passed in as a field in the task pointer.
1844*205eeb1aSlm66018  *
1845*205eeb1aSlm66018  * Parameters:
1846*205eeb1aSlm66018  *	arg 	- opaque pointer to structure containing task to be completed
1847*205eeb1aSlm66018  *
1848*205eeb1aSlm66018  * Return Values
1849*205eeb1aSlm66018  *	None
1850*205eeb1aSlm66018  */
1851*205eeb1aSlm66018 static void
1852*205eeb1aSlm66018 vd_complete(void *arg)
1853*205eeb1aSlm66018 {
1854*205eeb1aSlm66018 	vd_task_t	*task = (vd_task_t *)arg;
1855*205eeb1aSlm66018 
1856*205eeb1aSlm66018 	ASSERT(task != NULL);
1857*205eeb1aSlm66018 	ASSERT(task->status == EINPROGRESS);
1858*205eeb1aSlm66018 	ASSERT(task->completef != NULL);
1859*205eeb1aSlm66018 
1860*205eeb1aSlm66018 	task->status = task->completef(task);
1861*205eeb1aSlm66018 	if (task->status)
1862*205eeb1aSlm66018 		PR0("%s: Error %d completing task", __func__, task->status);
1863*205eeb1aSlm66018 
1864*205eeb1aSlm66018 	/* Now notify the vDisk client */
1865*205eeb1aSlm66018 	vd_complete_notify(task);
1866*205eeb1aSlm66018 }
1867*205eeb1aSlm66018 
18681ae08745Sheppo static int
1869d10e4ef2Snarayan vd_ioctl(vd_task_t *task)
18701ae08745Sheppo {
187187a7269eSachartre 	int			i, status;
18721ae08745Sheppo 	void			*buf = NULL;
18730a55fbb7Slm66018 	struct dk_geom		dk_geom = {0};
18740a55fbb7Slm66018 	struct vtoc		vtoc = {0};
18754bac2208Snarayan 	struct dk_efi		dk_efi = {0};
1876d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
1877d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
18780a55fbb7Slm66018 	vd_ioctl_t		ioctl[] = {
18790a55fbb7Slm66018 		/* Command (no-copy) operations */
18800a55fbb7Slm66018 		{VD_OP_FLUSH, STRINGIZE(VD_OP_FLUSH), 0,
18810a55fbb7Slm66018 		    DKIOCFLUSHWRITECACHE, STRINGIZE(DKIOCFLUSHWRITECACHE),
18820a55fbb7Slm66018 		    NULL, NULL, NULL},
18830a55fbb7Slm66018 
18840a55fbb7Slm66018 		/* "Get" (copy-out) operations */
18850a55fbb7Slm66018 		{VD_OP_GET_WCE, STRINGIZE(VD_OP_GET_WCE), RNDSIZE(int),
18860a55fbb7Slm66018 		    DKIOCGETWCE, STRINGIZE(DKIOCGETWCE),
18874bac2208Snarayan 		    NULL, VD_IDENTITY, VD_IDENTITY},
18880a55fbb7Slm66018 		{VD_OP_GET_DISKGEOM, STRINGIZE(VD_OP_GET_DISKGEOM),
18890a55fbb7Slm66018 		    RNDSIZE(vd_geom_t),
18900a55fbb7Slm66018 		    DKIOCGGEOM, STRINGIZE(DKIOCGGEOM),
18910a55fbb7Slm66018 		    &dk_geom, NULL, dk_geom2vd_geom},
18920a55fbb7Slm66018 		{VD_OP_GET_VTOC, STRINGIZE(VD_OP_GET_VTOC), RNDSIZE(vd_vtoc_t),
18930a55fbb7Slm66018 		    DKIOCGVTOC, STRINGIZE(DKIOCGVTOC),
18940a55fbb7Slm66018 		    &vtoc, NULL, vtoc2vd_vtoc},
18954bac2208Snarayan 		{VD_OP_GET_EFI, STRINGIZE(VD_OP_GET_EFI), RNDSIZE(vd_efi_t),
18964bac2208Snarayan 		    DKIOCGETEFI, STRINGIZE(DKIOCGETEFI),
18974bac2208Snarayan 		    &dk_efi, vd_get_efi_in, vd_get_efi_out},
18980a55fbb7Slm66018 
18990a55fbb7Slm66018 		/* "Set" (copy-in) operations */
19000a55fbb7Slm66018 		{VD_OP_SET_WCE, STRINGIZE(VD_OP_SET_WCE), RNDSIZE(int),
19010a55fbb7Slm66018 		    DKIOCSETWCE, STRINGIZE(DKIOCSETWCE),
19024bac2208Snarayan 		    NULL, VD_IDENTITY, VD_IDENTITY},
19030a55fbb7Slm66018 		{VD_OP_SET_DISKGEOM, STRINGIZE(VD_OP_SET_DISKGEOM),
19040a55fbb7Slm66018 		    RNDSIZE(vd_geom_t),
19050a55fbb7Slm66018 		    DKIOCSGEOM, STRINGIZE(DKIOCSGEOM),
19060a55fbb7Slm66018 		    &dk_geom, vd_geom2dk_geom, NULL},
19070a55fbb7Slm66018 		{VD_OP_SET_VTOC, STRINGIZE(VD_OP_SET_VTOC), RNDSIZE(vd_vtoc_t),
19080a55fbb7Slm66018 		    DKIOCSVTOC, STRINGIZE(DKIOCSVTOC),
19090a55fbb7Slm66018 		    &vtoc, vd_vtoc2vtoc, NULL},
19104bac2208Snarayan 		{VD_OP_SET_EFI, STRINGIZE(VD_OP_SET_EFI), RNDSIZE(vd_efi_t),
19114bac2208Snarayan 		    DKIOCSETEFI, STRINGIZE(DKIOCSETEFI),
19124bac2208Snarayan 		    &dk_efi, vd_set_efi_in, vd_set_efi_out},
19130a55fbb7Slm66018 	};
19141ae08745Sheppo 	size_t		nioctls = (sizeof (ioctl))/(sizeof (ioctl[0]));
19151ae08745Sheppo 
19161ae08745Sheppo 
1917d10e4ef2Snarayan 	ASSERT(vd != NULL);
1918d10e4ef2Snarayan 	ASSERT(request != NULL);
19191ae08745Sheppo 	ASSERT(request->slice < vd->nslices);
19201ae08745Sheppo 
19211ae08745Sheppo 	/*
19221ae08745Sheppo 	 * Determine ioctl corresponding to caller's "operation" and
19231ae08745Sheppo 	 * validate caller's "nbytes"
19241ae08745Sheppo 	 */
19251ae08745Sheppo 	for (i = 0; i < nioctls; i++) {
19261ae08745Sheppo 		if (request->operation == ioctl[i].operation) {
19270a55fbb7Slm66018 			/* LDC memory operations require 8-byte multiples */
19280a55fbb7Slm66018 			ASSERT(ioctl[i].nbytes % sizeof (uint64_t) == 0);
19290a55fbb7Slm66018 
19304bac2208Snarayan 			if (request->operation == VD_OP_GET_EFI ||
19314bac2208Snarayan 			    request->operation == VD_OP_SET_EFI) {
19324bac2208Snarayan 				if (request->nbytes >= ioctl[i].nbytes)
19334bac2208Snarayan 					break;
19343af08d82Slm66018 				PR0("%s:  Expected at least nbytes = %lu, "
19354bac2208Snarayan 				    "got %lu", ioctl[i].operation_name,
19364bac2208Snarayan 				    ioctl[i].nbytes, request->nbytes);
19374bac2208Snarayan 				return (EINVAL);
19384bac2208Snarayan 			}
19394bac2208Snarayan 
19400a55fbb7Slm66018 			if (request->nbytes != ioctl[i].nbytes) {
19413af08d82Slm66018 				PR0("%s:  Expected nbytes = %lu, got %lu",
19420a55fbb7Slm66018 				    ioctl[i].operation_name, ioctl[i].nbytes,
19430a55fbb7Slm66018 				    request->nbytes);
19441ae08745Sheppo 				return (EINVAL);
19451ae08745Sheppo 			}
19461ae08745Sheppo 
19471ae08745Sheppo 			break;
19481ae08745Sheppo 		}
19491ae08745Sheppo 	}
19501ae08745Sheppo 	ASSERT(i < nioctls);	/* because "operation" already validated */
19511ae08745Sheppo 
19521ae08745Sheppo 	if (request->nbytes)
19531ae08745Sheppo 		buf = kmem_zalloc(request->nbytes, KM_SLEEP);
19541ae08745Sheppo 	status = vd_do_ioctl(vd, request, buf, &ioctl[i]);
19551ae08745Sheppo 	if (request->nbytes)
19561ae08745Sheppo 		kmem_free(buf, request->nbytes);
195787a7269eSachartre 
19581ae08745Sheppo 	return (status);
19591ae08745Sheppo }
19601ae08745Sheppo 
19614bac2208Snarayan static int
19624bac2208Snarayan vd_get_devid(vd_task_t *task)
19634bac2208Snarayan {
19644bac2208Snarayan 	vd_t *vd = task->vd;
19654bac2208Snarayan 	vd_dring_payload_t *request = task->request;
19664bac2208Snarayan 	vd_devid_t *vd_devid;
19674bac2208Snarayan 	impl_devid_t *devid;
196887a7269eSachartre 	int status, bufid_len, devid_len, len, sz;
19693af08d82Slm66018 	int bufbytes;
19704bac2208Snarayan 
19713af08d82Slm66018 	PR1("Get Device ID, nbytes=%ld", request->nbytes);
19724bac2208Snarayan 
19733c96341aSnarayan 	if (vd->file) {
197487a7269eSachartre 		if (vd->file_devid == NULL) {
19753af08d82Slm66018 			PR2("No Device ID");
1976*205eeb1aSlm66018 			request->status = ENOENT;
1977*205eeb1aSlm66018 			return (0);
197887a7269eSachartre 		} else {
197987a7269eSachartre 			sz = ddi_devid_sizeof(vd->file_devid);
198087a7269eSachartre 			devid = kmem_alloc(sz, KM_SLEEP);
198187a7269eSachartre 			bcopy(vd->file_devid, devid, sz);
198287a7269eSachartre 		}
198387a7269eSachartre 	} else {
198487a7269eSachartre 		if (ddi_lyr_get_devid(vd->dev[request->slice],
198587a7269eSachartre 		    (ddi_devid_t *)&devid) != DDI_SUCCESS) {
198687a7269eSachartre 			PR2("No Device ID");
1987*205eeb1aSlm66018 			request->status = ENOENT;
1988*205eeb1aSlm66018 			return (0);
198987a7269eSachartre 		}
19904bac2208Snarayan 	}
19914bac2208Snarayan 
19924bac2208Snarayan 	bufid_len = request->nbytes - sizeof (vd_devid_t) + 1;
19934bac2208Snarayan 	devid_len = DEVID_GETLEN(devid);
19944bac2208Snarayan 
19953af08d82Slm66018 	/*
19963af08d82Slm66018 	 * Save the buffer size here for use in deallocation.
19973af08d82Slm66018 	 * The actual number of bytes copied is returned in
19983af08d82Slm66018 	 * the 'nbytes' field of the request structure.
19993af08d82Slm66018 	 */
20003af08d82Slm66018 	bufbytes = request->nbytes;
20013af08d82Slm66018 
20023af08d82Slm66018 	vd_devid = kmem_zalloc(bufbytes, KM_SLEEP);
20034bac2208Snarayan 	vd_devid->length = devid_len;
20044bac2208Snarayan 	vd_devid->type = DEVID_GETTYPE(devid);
20054bac2208Snarayan 
20064bac2208Snarayan 	len = (devid_len > bufid_len)? bufid_len : devid_len;
20074bac2208Snarayan 
20084bac2208Snarayan 	bcopy(devid->did_id, vd_devid->id, len);
20094bac2208Snarayan 
20104bac2208Snarayan 	/* LDC memory operations require 8-byte multiples */
20114bac2208Snarayan 	ASSERT(request->nbytes % sizeof (uint64_t) == 0);
20124bac2208Snarayan 
20134bac2208Snarayan 	if ((status = ldc_mem_copy(vd->ldc_handle, (caddr_t)vd_devid, 0,
20144bac2208Snarayan 	    &request->nbytes, request->cookie, request->ncookies,
20154bac2208Snarayan 	    LDC_COPY_OUT)) != 0) {
20163af08d82Slm66018 		PR0("ldc_mem_copy() returned errno %d copying to client",
20174bac2208Snarayan 		    status);
20184bac2208Snarayan 	}
20193af08d82Slm66018 	PR1("post mem_copy: nbytes=%ld", request->nbytes);
20204bac2208Snarayan 
20213af08d82Slm66018 	kmem_free(vd_devid, bufbytes);
20224bac2208Snarayan 	ddi_devid_free((ddi_devid_t)devid);
20234bac2208Snarayan 
20244bac2208Snarayan 	return (status);
20254bac2208Snarayan }
20264bac2208Snarayan 
20271ae08745Sheppo /*
20281ae08745Sheppo  * Define the supported operations once the functions for performing them have
20291ae08745Sheppo  * been defined
20301ae08745Sheppo  */
20311ae08745Sheppo static const vds_operation_t	vds_operation[] = {
20323af08d82Slm66018 #define	X(_s)	#_s, _s
20333af08d82Slm66018 	{X(VD_OP_BREAD),	vd_start_bio,	vd_complete_bio},
20343af08d82Slm66018 	{X(VD_OP_BWRITE),	vd_start_bio,	vd_complete_bio},
20353af08d82Slm66018 	{X(VD_OP_FLUSH),	vd_ioctl,	NULL},
20363af08d82Slm66018 	{X(VD_OP_GET_WCE),	vd_ioctl,	NULL},
20373af08d82Slm66018 	{X(VD_OP_SET_WCE),	vd_ioctl,	NULL},
20383af08d82Slm66018 	{X(VD_OP_GET_VTOC),	vd_ioctl,	NULL},
20393af08d82Slm66018 	{X(VD_OP_SET_VTOC),	vd_ioctl,	NULL},
20403af08d82Slm66018 	{X(VD_OP_GET_DISKGEOM),	vd_ioctl,	NULL},
20413af08d82Slm66018 	{X(VD_OP_SET_DISKGEOM),	vd_ioctl,	NULL},
20423af08d82Slm66018 	{X(VD_OP_GET_EFI),	vd_ioctl,	NULL},
20433af08d82Slm66018 	{X(VD_OP_SET_EFI),	vd_ioctl,	NULL},
20443af08d82Slm66018 	{X(VD_OP_GET_DEVID),	vd_get_devid,	NULL},
20453af08d82Slm66018 #undef	X
20461ae08745Sheppo };
20471ae08745Sheppo 
20481ae08745Sheppo static const size_t	vds_noperations =
20491ae08745Sheppo 	(sizeof (vds_operation))/(sizeof (vds_operation[0]));
20501ae08745Sheppo 
20511ae08745Sheppo /*
2052d10e4ef2Snarayan  * Process a task specifying a client I/O request
2053*205eeb1aSlm66018  *
2054*205eeb1aSlm66018  * Parameters:
2055*205eeb1aSlm66018  *	task 		- structure containing the request sent from client
2056*205eeb1aSlm66018  *
2057*205eeb1aSlm66018  * Return Value
2058*205eeb1aSlm66018  *	0	- success
2059*205eeb1aSlm66018  *	ENOTSUP	- Unknown/Unsupported VD_OP_XXX operation
2060*205eeb1aSlm66018  *	EINVAL	- Invalid disk slice
2061*205eeb1aSlm66018  *	!= 0	- some other non-zero return value from start function
20621ae08745Sheppo  */
20631ae08745Sheppo static int
2064*205eeb1aSlm66018 vd_do_process_task(vd_task_t *task)
20651ae08745Sheppo {
2066*205eeb1aSlm66018 	int			i;
2067d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
2068d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
20691ae08745Sheppo 
2070d10e4ef2Snarayan 	ASSERT(vd != NULL);
2071d10e4ef2Snarayan 	ASSERT(request != NULL);
20721ae08745Sheppo 
2073d10e4ef2Snarayan 	/* Find the requested operation */
2074*205eeb1aSlm66018 	for (i = 0; i < vds_noperations; i++) {
2075*205eeb1aSlm66018 		if (request->operation == vds_operation[i].operation) {
2076*205eeb1aSlm66018 			/* all operations should have a start func */
2077*205eeb1aSlm66018 			ASSERT(vds_operation[i].start != NULL);
2078*205eeb1aSlm66018 
2079*205eeb1aSlm66018 			task->completef = vds_operation[i].complete;
2080d10e4ef2Snarayan 			break;
2081*205eeb1aSlm66018 		}
2082*205eeb1aSlm66018 	}
2083d10e4ef2Snarayan 	if (i == vds_noperations) {
20843af08d82Slm66018 		PR0("Unsupported operation %u", request->operation);
20851ae08745Sheppo 		return (ENOTSUP);
20861ae08745Sheppo 	}
20871ae08745Sheppo 
20887636cb21Slm66018 	/* Range-check slice */
208987a7269eSachartre 	if (request->slice >= vd->nslices &&
209087a7269eSachartre 	    (vd->vdisk_type != VD_DISK_TYPE_DISK ||
209187a7269eSachartre 	    request->slice != VD_SLICE_NONE)) {
20923af08d82Slm66018 		PR0("Invalid \"slice\" %u (max %u) for virtual disk",
20937636cb21Slm66018 		    request->slice, (vd->nslices - 1));
20947636cb21Slm66018 		return (EINVAL);
20957636cb21Slm66018 	}
20967636cb21Slm66018 
2097*205eeb1aSlm66018 	/*
2098*205eeb1aSlm66018 	 * Call the function pointer that starts the operation.
2099*205eeb1aSlm66018 	 */
2100*205eeb1aSlm66018 	return (vds_operation[i].start(task));
21011ae08745Sheppo }
21021ae08745Sheppo 
2103*205eeb1aSlm66018 /*
2104*205eeb1aSlm66018  * Description:
2105*205eeb1aSlm66018  *	This function is called by both the in-band and descriptor ring
2106*205eeb1aSlm66018  *	message processing functions paths to actually execute the task
2107*205eeb1aSlm66018  *	requested by the vDisk client. It in turn calls its worker
2108*205eeb1aSlm66018  *	function, vd_do_process_task(), to carry our the request.
2109*205eeb1aSlm66018  *
2110*205eeb1aSlm66018  *	Any transport errors (e.g. LDC errors, vDisk protocol errors) are
2111*205eeb1aSlm66018  *	saved in the 'status' field of the task and are propagated back
2112*205eeb1aSlm66018  *	up the call stack to trigger a NACK
2113*205eeb1aSlm66018  *
2114*205eeb1aSlm66018  *	Any request errors (e.g. ENOTTY from an ioctl) are saved in
2115*205eeb1aSlm66018  *	the 'status' field of the request and result in an ACK being sent
2116*205eeb1aSlm66018  *	by the completion handler.
2117*205eeb1aSlm66018  *
2118*205eeb1aSlm66018  * Parameters:
2119*205eeb1aSlm66018  *	task 		- structure containing the request sent from client
2120*205eeb1aSlm66018  *
2121*205eeb1aSlm66018  * Return Value
2122*205eeb1aSlm66018  *	0		- successful synchronous request.
2123*205eeb1aSlm66018  *	!= 0		- transport error (e.g. LDC errors, vDisk protocol)
2124*205eeb1aSlm66018  *	EINPROGRESS	- task will be finished in a completion handler
2125*205eeb1aSlm66018  */
2126*205eeb1aSlm66018 static int
2127*205eeb1aSlm66018 vd_process_task(vd_task_t *task)
2128*205eeb1aSlm66018 {
2129*205eeb1aSlm66018 	vd_t	*vd = task->vd;
2130*205eeb1aSlm66018 	int	status;
21311ae08745Sheppo 
2132*205eeb1aSlm66018 	DTRACE_PROBE1(task__start, vd_task_t *, task);
21333af08d82Slm66018 
2134*205eeb1aSlm66018 	task->status =  vd_do_process_task(task);
2135*205eeb1aSlm66018 
2136*205eeb1aSlm66018 	/*
2137*205eeb1aSlm66018 	 * If the task processing function returned EINPROGRESS indicating
2138*205eeb1aSlm66018 	 * that the task needs completing then schedule a taskq entry to
2139*205eeb1aSlm66018 	 * finish it now.
2140*205eeb1aSlm66018 	 *
2141*205eeb1aSlm66018 	 * Otherwise the task processing function returned either zero
2142*205eeb1aSlm66018 	 * indicating that the task was finished in the start function (and we
2143*205eeb1aSlm66018 	 * don't need to wait in a completion function) or the start function
2144*205eeb1aSlm66018 	 * returned an error - in both cases all that needs to happen is the
2145*205eeb1aSlm66018 	 * notification to the vDisk client higher up the call stack.
2146*205eeb1aSlm66018 	 * If the task was using a Descriptor Ring, we need to mark it as done
2147*205eeb1aSlm66018 	 * at this stage.
2148*205eeb1aSlm66018 	 */
2149*205eeb1aSlm66018 	if (task->status == EINPROGRESS) {
2150d10e4ef2Snarayan 		/* Queue a task to complete the operation */
2151*205eeb1aSlm66018 		(void) ddi_taskq_dispatch(vd->completionq, vd_complete,
2152d10e4ef2Snarayan 		    task, DDI_SLEEP);
2153d10e4ef2Snarayan 
2154*205eeb1aSlm66018 	} else if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE)) {
2155*205eeb1aSlm66018 		/* Update the dring element if it's a dring client */
2156*205eeb1aSlm66018 		status = vd_mark_elem_done(vd, task->index,
2157*205eeb1aSlm66018 		    task->request->status, task->request->nbytes);
2158*205eeb1aSlm66018 		if (status == ECONNRESET)
2159*205eeb1aSlm66018 			vd_mark_in_reset(vd);
2160*205eeb1aSlm66018 	}
2161*205eeb1aSlm66018 
2162*205eeb1aSlm66018 	return (task->status);
21631ae08745Sheppo }
21641ae08745Sheppo 
21651ae08745Sheppo /*
21660a55fbb7Slm66018  * Return true if the "type", "subtype", and "env" fields of the "tag" first
21670a55fbb7Slm66018  * argument match the corresponding remaining arguments; otherwise, return false
21681ae08745Sheppo  */
21690a55fbb7Slm66018 boolean_t
21701ae08745Sheppo vd_msgtype(vio_msg_tag_t *tag, int type, int subtype, int env)
21711ae08745Sheppo {
21721ae08745Sheppo 	return ((tag->vio_msgtype == type) &&
21731ae08745Sheppo 	    (tag->vio_subtype == subtype) &&
21740a55fbb7Slm66018 	    (tag->vio_subtype_env == env)) ? B_TRUE : B_FALSE;
21751ae08745Sheppo }
21761ae08745Sheppo 
21770a55fbb7Slm66018 /*
21780a55fbb7Slm66018  * Check whether the major/minor version specified in "ver_msg" is supported
21790a55fbb7Slm66018  * by this server.
21800a55fbb7Slm66018  */
21810a55fbb7Slm66018 static boolean_t
21820a55fbb7Slm66018 vds_supported_version(vio_ver_msg_t *ver_msg)
21830a55fbb7Slm66018 {
21840a55fbb7Slm66018 	for (int i = 0; i < vds_num_versions; i++) {
21850a55fbb7Slm66018 		ASSERT(vds_version[i].major > 0);
21860a55fbb7Slm66018 		ASSERT((i == 0) ||
21870a55fbb7Slm66018 		    (vds_version[i].major < vds_version[i-1].major));
21880a55fbb7Slm66018 
21890a55fbb7Slm66018 		/*
21900a55fbb7Slm66018 		 * If the major versions match, adjust the minor version, if
21910a55fbb7Slm66018 		 * necessary, down to the highest value supported by this
21920a55fbb7Slm66018 		 * server and return true so this message will get "ack"ed;
21930a55fbb7Slm66018 		 * the client should also support all minor versions lower
21940a55fbb7Slm66018 		 * than the value it sent
21950a55fbb7Slm66018 		 */
21960a55fbb7Slm66018 		if (ver_msg->ver_major == vds_version[i].major) {
21970a55fbb7Slm66018 			if (ver_msg->ver_minor > vds_version[i].minor) {
21980a55fbb7Slm66018 				PR0("Adjusting minor version from %u to %u",
21990a55fbb7Slm66018 				    ver_msg->ver_minor, vds_version[i].minor);
22000a55fbb7Slm66018 				ver_msg->ver_minor = vds_version[i].minor;
22010a55fbb7Slm66018 			}
22020a55fbb7Slm66018 			return (B_TRUE);
22030a55fbb7Slm66018 		}
22040a55fbb7Slm66018 
22050a55fbb7Slm66018 		/*
22060a55fbb7Slm66018 		 * If the message contains a higher major version number, set
22070a55fbb7Slm66018 		 * the message's major/minor versions to the current values
22080a55fbb7Slm66018 		 * and return false, so this message will get "nack"ed with
22090a55fbb7Slm66018 		 * these values, and the client will potentially try again
22100a55fbb7Slm66018 		 * with the same or a lower version
22110a55fbb7Slm66018 		 */
22120a55fbb7Slm66018 		if (ver_msg->ver_major > vds_version[i].major) {
22130a55fbb7Slm66018 			ver_msg->ver_major = vds_version[i].major;
22140a55fbb7Slm66018 			ver_msg->ver_minor = vds_version[i].minor;
22150a55fbb7Slm66018 			return (B_FALSE);
22160a55fbb7Slm66018 		}
22170a55fbb7Slm66018 
22180a55fbb7Slm66018 		/*
22190a55fbb7Slm66018 		 * Otherwise, the message's major version is less than the
22200a55fbb7Slm66018 		 * current major version, so continue the loop to the next
22210a55fbb7Slm66018 		 * (lower) supported version
22220a55fbb7Slm66018 		 */
22230a55fbb7Slm66018 	}
22240a55fbb7Slm66018 
22250a55fbb7Slm66018 	/*
22260a55fbb7Slm66018 	 * No common version was found; "ground" the version pair in the
22270a55fbb7Slm66018 	 * message to terminate negotiation
22280a55fbb7Slm66018 	 */
22290a55fbb7Slm66018 	ver_msg->ver_major = 0;
22300a55fbb7Slm66018 	ver_msg->ver_minor = 0;
22310a55fbb7Slm66018 	return (B_FALSE);
22320a55fbb7Slm66018 }
22330a55fbb7Slm66018 
22340a55fbb7Slm66018 /*
22350a55fbb7Slm66018  * Process a version message from a client.  vds expects to receive version
22360a55fbb7Slm66018  * messages from clients seeking service, but never issues version messages
22370a55fbb7Slm66018  * itself; therefore, vds can ACK or NACK client version messages, but does
22380a55fbb7Slm66018  * not expect to receive version-message ACKs or NACKs (and will treat such
22390a55fbb7Slm66018  * messages as invalid).
22400a55fbb7Slm66018  */
22411ae08745Sheppo static int
22420a55fbb7Slm66018 vd_process_ver_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
22431ae08745Sheppo {
22441ae08745Sheppo 	vio_ver_msg_t	*ver_msg = (vio_ver_msg_t *)msg;
22451ae08745Sheppo 
22461ae08745Sheppo 
22471ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
22481ae08745Sheppo 
22491ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
22501ae08745Sheppo 	    VIO_VER_INFO)) {
22511ae08745Sheppo 		return (ENOMSG);	/* not a version message */
22521ae08745Sheppo 	}
22531ae08745Sheppo 
22541ae08745Sheppo 	if (msglen != sizeof (*ver_msg)) {
22553af08d82Slm66018 		PR0("Expected %lu-byte version message; "
22561ae08745Sheppo 		    "received %lu bytes", sizeof (*ver_msg), msglen);
22571ae08745Sheppo 		return (EBADMSG);
22581ae08745Sheppo 	}
22591ae08745Sheppo 
22601ae08745Sheppo 	if (ver_msg->dev_class != VDEV_DISK) {
22613af08d82Slm66018 		PR0("Expected device class %u (disk); received %u",
22621ae08745Sheppo 		    VDEV_DISK, ver_msg->dev_class);
22631ae08745Sheppo 		return (EBADMSG);
22641ae08745Sheppo 	}
22651ae08745Sheppo 
22660a55fbb7Slm66018 	/*
22670a55fbb7Slm66018 	 * We're talking to the expected kind of client; set our device class
22680a55fbb7Slm66018 	 * for "ack/nack" back to the client
22690a55fbb7Slm66018 	 */
22701ae08745Sheppo 	ver_msg->dev_class = VDEV_DISK_SERVER;
22710a55fbb7Slm66018 
22720a55fbb7Slm66018 	/*
22730a55fbb7Slm66018 	 * Check whether the (valid) version message specifies a version
22740a55fbb7Slm66018 	 * supported by this server.  If the version is not supported, return
22750a55fbb7Slm66018 	 * EBADMSG so the message will get "nack"ed; vds_supported_version()
22760a55fbb7Slm66018 	 * will have updated the message with a supported version for the
22770a55fbb7Slm66018 	 * client to consider
22780a55fbb7Slm66018 	 */
22790a55fbb7Slm66018 	if (!vds_supported_version(ver_msg))
22800a55fbb7Slm66018 		return (EBADMSG);
22810a55fbb7Slm66018 
22820a55fbb7Slm66018 
22830a55fbb7Slm66018 	/*
22840a55fbb7Slm66018 	 * A version has been agreed upon; use the client's SID for
22850a55fbb7Slm66018 	 * communication on this channel now
22860a55fbb7Slm66018 	 */
22870a55fbb7Slm66018 	ASSERT(!(vd->initialized & VD_SID));
22880a55fbb7Slm66018 	vd->sid = ver_msg->tag.vio_sid;
22890a55fbb7Slm66018 	vd->initialized |= VD_SID;
22900a55fbb7Slm66018 
22910a55fbb7Slm66018 	/*
22920a55fbb7Slm66018 	 * When multiple versions are supported, this function should store
22930a55fbb7Slm66018 	 * the negotiated major and minor version values in the "vd" data
22940a55fbb7Slm66018 	 * structure to govern further communication; in particular, note that
22950a55fbb7Slm66018 	 * the client might have specified a lower minor version for the
22960a55fbb7Slm66018 	 * agreed major version than specifed in the vds_version[] array.  The
22970a55fbb7Slm66018 	 * following assertions should help remind future maintainers to make
22980a55fbb7Slm66018 	 * the appropriate changes to support multiple versions.
22990a55fbb7Slm66018 	 */
23000a55fbb7Slm66018 	ASSERT(vds_num_versions == 1);
23010a55fbb7Slm66018 	ASSERT(ver_msg->ver_major == vds_version[0].major);
23020a55fbb7Slm66018 	ASSERT(ver_msg->ver_minor == vds_version[0].minor);
23030a55fbb7Slm66018 
23040a55fbb7Slm66018 	PR0("Using major version %u, minor version %u",
23050a55fbb7Slm66018 	    ver_msg->ver_major, ver_msg->ver_minor);
23061ae08745Sheppo 	return (0);
23071ae08745Sheppo }
23081ae08745Sheppo 
23091ae08745Sheppo static int
23101ae08745Sheppo vd_process_attr_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
23111ae08745Sheppo {
23121ae08745Sheppo 	vd_attr_msg_t	*attr_msg = (vd_attr_msg_t *)msg;
23133c96341aSnarayan 	int		status, retry = 0;
23141ae08745Sheppo 
23151ae08745Sheppo 
23161ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
23171ae08745Sheppo 
23181ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
23191ae08745Sheppo 	    VIO_ATTR_INFO)) {
2320d10e4ef2Snarayan 		PR0("Message is not an attribute message");
2321d10e4ef2Snarayan 		return (ENOMSG);
23221ae08745Sheppo 	}
23231ae08745Sheppo 
23241ae08745Sheppo 	if (msglen != sizeof (*attr_msg)) {
23253af08d82Slm66018 		PR0("Expected %lu-byte attribute message; "
23261ae08745Sheppo 		    "received %lu bytes", sizeof (*attr_msg), msglen);
23271ae08745Sheppo 		return (EBADMSG);
23281ae08745Sheppo 	}
23291ae08745Sheppo 
23301ae08745Sheppo 	if (attr_msg->max_xfer_sz == 0) {
23313af08d82Slm66018 		PR0("Received maximum transfer size of 0 from client");
23321ae08745Sheppo 		return (EBADMSG);
23331ae08745Sheppo 	}
23341ae08745Sheppo 
23351ae08745Sheppo 	if ((attr_msg->xfer_mode != VIO_DESC_MODE) &&
23361ae08745Sheppo 	    (attr_msg->xfer_mode != VIO_DRING_MODE)) {
23373af08d82Slm66018 		PR0("Client requested unsupported transfer mode");
23381ae08745Sheppo 		return (EBADMSG);
23391ae08745Sheppo 	}
23401ae08745Sheppo 
23413c96341aSnarayan 	/*
23423c96341aSnarayan 	 * check if the underlying disk is ready, if not try accessing
23433c96341aSnarayan 	 * the device again. Open the vdisk device and extract info
23443c96341aSnarayan 	 * about it, as this is needed to respond to the attr info msg
23453c96341aSnarayan 	 */
23463c96341aSnarayan 	if ((vd->initialized & VD_DISK_READY) == 0) {
23473c96341aSnarayan 		PR0("Retry setting up disk (%s)", vd->device_path);
23483c96341aSnarayan 		do {
23493c96341aSnarayan 			status = vd_setup_vd(vd);
23503c96341aSnarayan 			if (status != EAGAIN || ++retry > vds_dev_retries)
23513c96341aSnarayan 				break;
23523c96341aSnarayan 
23533c96341aSnarayan 			/* incremental delay */
23543c96341aSnarayan 			delay(drv_usectohz(vds_dev_delay));
23553c96341aSnarayan 
23563c96341aSnarayan 			/* if vdisk is no longer enabled - return error */
23573c96341aSnarayan 			if (!vd_enabled(vd))
23583c96341aSnarayan 				return (ENXIO);
23593c96341aSnarayan 
23603c96341aSnarayan 		} while (status == EAGAIN);
23613c96341aSnarayan 
23623c96341aSnarayan 		if (status)
23633c96341aSnarayan 			return (ENXIO);
23643c96341aSnarayan 
23653c96341aSnarayan 		vd->initialized |= VD_DISK_READY;
23663c96341aSnarayan 		ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
23673c96341aSnarayan 		PR0("vdisk_type = %s, pseudo = %s, file = %s, nslices = %u",
23683c96341aSnarayan 		    ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
23693c96341aSnarayan 		    (vd->pseudo ? "yes" : "no"),
23703c96341aSnarayan 		    (vd->file ? "yes" : "no"),
23713c96341aSnarayan 		    vd->nslices);
23723c96341aSnarayan 	}
23733c96341aSnarayan 
23741ae08745Sheppo 	/* Success:  valid message and transfer mode */
23751ae08745Sheppo 	vd->xfer_mode = attr_msg->xfer_mode;
23763af08d82Slm66018 
23771ae08745Sheppo 	if (vd->xfer_mode == VIO_DESC_MODE) {
23783af08d82Slm66018 
23791ae08745Sheppo 		/*
23801ae08745Sheppo 		 * The vd_dring_inband_msg_t contains one cookie; need room
23811ae08745Sheppo 		 * for up to n-1 more cookies, where "n" is the number of full
23821ae08745Sheppo 		 * pages plus possibly one partial page required to cover
23831ae08745Sheppo 		 * "max_xfer_sz".  Add room for one more cookie if
23841ae08745Sheppo 		 * "max_xfer_sz" isn't an integral multiple of the page size.
23851ae08745Sheppo 		 * Must first get the maximum transfer size in bytes.
23861ae08745Sheppo 		 */
23871ae08745Sheppo 		size_t	max_xfer_bytes = attr_msg->vdisk_block_size ?
23881ae08745Sheppo 		    attr_msg->vdisk_block_size*attr_msg->max_xfer_sz :
23891ae08745Sheppo 		    attr_msg->max_xfer_sz;
23901ae08745Sheppo 		size_t	max_inband_msglen =
23911ae08745Sheppo 		    sizeof (vd_dring_inband_msg_t) +
23921ae08745Sheppo 		    ((max_xfer_bytes/PAGESIZE +
23931ae08745Sheppo 		    ((max_xfer_bytes % PAGESIZE) ? 1 : 0))*
23941ae08745Sheppo 		    (sizeof (ldc_mem_cookie_t)));
23951ae08745Sheppo 
23961ae08745Sheppo 		/*
23971ae08745Sheppo 		 * Set the maximum expected message length to
23981ae08745Sheppo 		 * accommodate in-band-descriptor messages with all
23991ae08745Sheppo 		 * their cookies
24001ae08745Sheppo 		 */
24011ae08745Sheppo 		vd->max_msglen = MAX(vd->max_msglen, max_inband_msglen);
2402d10e4ef2Snarayan 
2403d10e4ef2Snarayan 		/*
2404d10e4ef2Snarayan 		 * Initialize the data structure for processing in-band I/O
2405d10e4ef2Snarayan 		 * request descriptors
2406d10e4ef2Snarayan 		 */
2407d10e4ef2Snarayan 		vd->inband_task.vd	= vd;
24083af08d82Slm66018 		vd->inband_task.msg	= kmem_alloc(vd->max_msglen, KM_SLEEP);
2409d10e4ef2Snarayan 		vd->inband_task.index	= 0;
2410d10e4ef2Snarayan 		vd->inband_task.type	= VD_FINAL_RANGE_TASK;	/* range == 1 */
24111ae08745Sheppo 	}
24121ae08745Sheppo 
2413e1ebb9ecSlm66018 	/* Return the device's block size and max transfer size to the client */
2414e1ebb9ecSlm66018 	attr_msg->vdisk_block_size	= DEV_BSIZE;
2415e1ebb9ecSlm66018 	attr_msg->max_xfer_sz		= vd->max_xfer_sz;
2416e1ebb9ecSlm66018 
24171ae08745Sheppo 	attr_msg->vdisk_size = vd->vdisk_size;
24181ae08745Sheppo 	attr_msg->vdisk_type = vd->vdisk_type;
24191ae08745Sheppo 	attr_msg->operations = vds_operations;
24201ae08745Sheppo 	PR0("%s", VD_CLIENT(vd));
24213af08d82Slm66018 
24223af08d82Slm66018 	ASSERT(vd->dring_task == NULL);
24233af08d82Slm66018 
24241ae08745Sheppo 	return (0);
24251ae08745Sheppo }
24261ae08745Sheppo 
24271ae08745Sheppo static int
24281ae08745Sheppo vd_process_dring_reg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
24291ae08745Sheppo {
24301ae08745Sheppo 	int			status;
24311ae08745Sheppo 	size_t			expected;
24321ae08745Sheppo 	ldc_mem_info_t		dring_minfo;
24331ae08745Sheppo 	vio_dring_reg_msg_t	*reg_msg = (vio_dring_reg_msg_t *)msg;
24341ae08745Sheppo 
24351ae08745Sheppo 
24361ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
24371ae08745Sheppo 
24381ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
24391ae08745Sheppo 	    VIO_DRING_REG)) {
2440d10e4ef2Snarayan 		PR0("Message is not a register-dring message");
2441d10e4ef2Snarayan 		return (ENOMSG);
24421ae08745Sheppo 	}
24431ae08745Sheppo 
24441ae08745Sheppo 	if (msglen < sizeof (*reg_msg)) {
24453af08d82Slm66018 		PR0("Expected at least %lu-byte register-dring message; "
24461ae08745Sheppo 		    "received %lu bytes", sizeof (*reg_msg), msglen);
24471ae08745Sheppo 		return (EBADMSG);
24481ae08745Sheppo 	}
24491ae08745Sheppo 
24501ae08745Sheppo 	expected = sizeof (*reg_msg) +
24511ae08745Sheppo 	    (reg_msg->ncookies - 1)*(sizeof (reg_msg->cookie[0]));
24521ae08745Sheppo 	if (msglen != expected) {
24533af08d82Slm66018 		PR0("Expected %lu-byte register-dring message; "
24541ae08745Sheppo 		    "received %lu bytes", expected, msglen);
24551ae08745Sheppo 		return (EBADMSG);
24561ae08745Sheppo 	}
24571ae08745Sheppo 
24581ae08745Sheppo 	if (vd->initialized & VD_DRING) {
24593af08d82Slm66018 		PR0("A dring was previously registered; only support one");
24601ae08745Sheppo 		return (EBADMSG);
24611ae08745Sheppo 	}
24621ae08745Sheppo 
2463d10e4ef2Snarayan 	if (reg_msg->num_descriptors > INT32_MAX) {
24643af08d82Slm66018 		PR0("reg_msg->num_descriptors = %u; must be <= %u (%s)",
2465d10e4ef2Snarayan 		    reg_msg->ncookies, INT32_MAX, STRINGIZE(INT32_MAX));
2466d10e4ef2Snarayan 		return (EBADMSG);
2467d10e4ef2Snarayan 	}
2468d10e4ef2Snarayan 
24691ae08745Sheppo 	if (reg_msg->ncookies != 1) {
24701ae08745Sheppo 		/*
24711ae08745Sheppo 		 * In addition to fixing the assertion in the success case
24721ae08745Sheppo 		 * below, supporting drings which require more than one
24731ae08745Sheppo 		 * "cookie" requires increasing the value of vd->max_msglen
24741ae08745Sheppo 		 * somewhere in the code path prior to receiving the message
24751ae08745Sheppo 		 * which results in calling this function.  Note that without
24761ae08745Sheppo 		 * making this change, the larger message size required to
24771ae08745Sheppo 		 * accommodate multiple cookies cannot be successfully
24781ae08745Sheppo 		 * received, so this function will not even get called.
24791ae08745Sheppo 		 * Gracefully accommodating more dring cookies might
24801ae08745Sheppo 		 * reasonably demand exchanging an additional attribute or
24811ae08745Sheppo 		 * making a minor protocol adjustment
24821ae08745Sheppo 		 */
24833af08d82Slm66018 		PR0("reg_msg->ncookies = %u != 1", reg_msg->ncookies);
24841ae08745Sheppo 		return (EBADMSG);
24851ae08745Sheppo 	}
24861ae08745Sheppo 
24871ae08745Sheppo 	status = ldc_mem_dring_map(vd->ldc_handle, reg_msg->cookie,
24881ae08745Sheppo 	    reg_msg->ncookies, reg_msg->num_descriptors,
24894bac2208Snarayan 	    reg_msg->descriptor_size, LDC_DIRECT_MAP, &vd->dring_handle);
24901ae08745Sheppo 	if (status != 0) {
24913af08d82Slm66018 		PR0("ldc_mem_dring_map() returned errno %d", status);
24921ae08745Sheppo 		return (status);
24931ae08745Sheppo 	}
24941ae08745Sheppo 
24951ae08745Sheppo 	/*
24961ae08745Sheppo 	 * To remove the need for this assertion, must call
24971ae08745Sheppo 	 * ldc_mem_dring_nextcookie() successfully ncookies-1 times after a
24981ae08745Sheppo 	 * successful call to ldc_mem_dring_map()
24991ae08745Sheppo 	 */
25001ae08745Sheppo 	ASSERT(reg_msg->ncookies == 1);
25011ae08745Sheppo 
25021ae08745Sheppo 	if ((status =
25031ae08745Sheppo 	    ldc_mem_dring_info(vd->dring_handle, &dring_minfo)) != 0) {
25043af08d82Slm66018 		PR0("ldc_mem_dring_info() returned errno %d", status);
25051ae08745Sheppo 		if ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0)
25063af08d82Slm66018 			PR0("ldc_mem_dring_unmap() returned errno %d", status);
25071ae08745Sheppo 		return (status);
25081ae08745Sheppo 	}
25091ae08745Sheppo 
25101ae08745Sheppo 	if (dring_minfo.vaddr == NULL) {
25113af08d82Slm66018 		PR0("Descriptor ring virtual address is NULL");
25120a55fbb7Slm66018 		return (ENXIO);
25131ae08745Sheppo 	}
25141ae08745Sheppo 
25151ae08745Sheppo 
2516d10e4ef2Snarayan 	/* Initialize for valid message and mapped dring */
25171ae08745Sheppo 	PR1("descriptor size = %u, dring length = %u",
25181ae08745Sheppo 	    vd->descriptor_size, vd->dring_len);
25191ae08745Sheppo 	vd->initialized |= VD_DRING;
25201ae08745Sheppo 	vd->dring_ident = 1;	/* "There Can Be Only One" */
25211ae08745Sheppo 	vd->dring = dring_minfo.vaddr;
25221ae08745Sheppo 	vd->descriptor_size = reg_msg->descriptor_size;
25231ae08745Sheppo 	vd->dring_len = reg_msg->num_descriptors;
25241ae08745Sheppo 	reg_msg->dring_ident = vd->dring_ident;
2525d10e4ef2Snarayan 
2526d10e4ef2Snarayan 	/*
2527d10e4ef2Snarayan 	 * Allocate and initialize a "shadow" array of data structures for
2528d10e4ef2Snarayan 	 * tasks to process I/O requests in dring elements
2529d10e4ef2Snarayan 	 */
2530d10e4ef2Snarayan 	vd->dring_task =
2531d10e4ef2Snarayan 	    kmem_zalloc((sizeof (*vd->dring_task)) * vd->dring_len, KM_SLEEP);
2532d10e4ef2Snarayan 	for (int i = 0; i < vd->dring_len; i++) {
2533d10e4ef2Snarayan 		vd->dring_task[i].vd		= vd;
2534d10e4ef2Snarayan 		vd->dring_task[i].index		= i;
2535d10e4ef2Snarayan 		vd->dring_task[i].request	= &VD_DRING_ELEM(i)->payload;
25364bac2208Snarayan 
25374bac2208Snarayan 		status = ldc_mem_alloc_handle(vd->ldc_handle,
25384bac2208Snarayan 		    &(vd->dring_task[i].mhdl));
25394bac2208Snarayan 		if (status) {
25403af08d82Slm66018 			PR0("ldc_mem_alloc_handle() returned err %d ", status);
25414bac2208Snarayan 			return (ENXIO);
25424bac2208Snarayan 		}
25433af08d82Slm66018 
25443af08d82Slm66018 		vd->dring_task[i].msg = kmem_alloc(vd->max_msglen, KM_SLEEP);
2545d10e4ef2Snarayan 	}
2546d10e4ef2Snarayan 
25471ae08745Sheppo 	return (0);
25481ae08745Sheppo }
25491ae08745Sheppo 
25501ae08745Sheppo static int
25511ae08745Sheppo vd_process_dring_unreg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
25521ae08745Sheppo {
25531ae08745Sheppo 	vio_dring_unreg_msg_t	*unreg_msg = (vio_dring_unreg_msg_t *)msg;
25541ae08745Sheppo 
25551ae08745Sheppo 
25561ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
25571ae08745Sheppo 
25581ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
25591ae08745Sheppo 	    VIO_DRING_UNREG)) {
2560d10e4ef2Snarayan 		PR0("Message is not an unregister-dring message");
2561d10e4ef2Snarayan 		return (ENOMSG);
25621ae08745Sheppo 	}
25631ae08745Sheppo 
25641ae08745Sheppo 	if (msglen != sizeof (*unreg_msg)) {
25653af08d82Slm66018 		PR0("Expected %lu-byte unregister-dring message; "
25661ae08745Sheppo 		    "received %lu bytes", sizeof (*unreg_msg), msglen);
25671ae08745Sheppo 		return (EBADMSG);
25681ae08745Sheppo 	}
25691ae08745Sheppo 
25701ae08745Sheppo 	if (unreg_msg->dring_ident != vd->dring_ident) {
25713af08d82Slm66018 		PR0("Expected dring ident %lu; received %lu",
25721ae08745Sheppo 		    vd->dring_ident, unreg_msg->dring_ident);
25731ae08745Sheppo 		return (EBADMSG);
25741ae08745Sheppo 	}
25751ae08745Sheppo 
25761ae08745Sheppo 	return (0);
25771ae08745Sheppo }
25781ae08745Sheppo 
25791ae08745Sheppo static int
25801ae08745Sheppo process_rdx_msg(vio_msg_t *msg, size_t msglen)
25811ae08745Sheppo {
25821ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
25831ae08745Sheppo 
2584d10e4ef2Snarayan 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX)) {
2585d10e4ef2Snarayan 		PR0("Message is not an RDX message");
2586d10e4ef2Snarayan 		return (ENOMSG);
2587d10e4ef2Snarayan 	}
25881ae08745Sheppo 
25891ae08745Sheppo 	if (msglen != sizeof (vio_rdx_msg_t)) {
25903af08d82Slm66018 		PR0("Expected %lu-byte RDX message; received %lu bytes",
25911ae08745Sheppo 		    sizeof (vio_rdx_msg_t), msglen);
25921ae08745Sheppo 		return (EBADMSG);
25931ae08745Sheppo 	}
25941ae08745Sheppo 
2595d10e4ef2Snarayan 	PR0("Valid RDX message");
25961ae08745Sheppo 	return (0);
25971ae08745Sheppo }
25981ae08745Sheppo 
25991ae08745Sheppo static int
26001ae08745Sheppo vd_check_seq_num(vd_t *vd, uint64_t seq_num)
26011ae08745Sheppo {
26021ae08745Sheppo 	if ((vd->initialized & VD_SEQ_NUM) && (seq_num != vd->seq_num + 1)) {
26033af08d82Slm66018 		PR0("Received seq_num %lu; expected %lu",
26041ae08745Sheppo 		    seq_num, (vd->seq_num + 1));
26053af08d82Slm66018 		PR0("initiating soft reset");
2606d10e4ef2Snarayan 		vd_need_reset(vd, B_FALSE);
26071ae08745Sheppo 		return (1);
26081ae08745Sheppo 	}
26091ae08745Sheppo 
26101ae08745Sheppo 	vd->seq_num = seq_num;
26111ae08745Sheppo 	vd->initialized |= VD_SEQ_NUM;	/* superfluous after first time... */
26121ae08745Sheppo 	return (0);
26131ae08745Sheppo }
26141ae08745Sheppo 
26151ae08745Sheppo /*
26161ae08745Sheppo  * Return the expected size of an inband-descriptor message with all the
26171ae08745Sheppo  * cookies it claims to include
26181ae08745Sheppo  */
26191ae08745Sheppo static size_t
26201ae08745Sheppo expected_inband_size(vd_dring_inband_msg_t *msg)
26211ae08745Sheppo {
26221ae08745Sheppo 	return ((sizeof (*msg)) +
26231ae08745Sheppo 	    (msg->payload.ncookies - 1)*(sizeof (msg->payload.cookie[0])));
26241ae08745Sheppo }
26251ae08745Sheppo 
26261ae08745Sheppo /*
26271ae08745Sheppo  * Process an in-band descriptor message:  used with clients like OBP, with
26281ae08745Sheppo  * which vds exchanges descriptors within VIO message payloads, rather than
26291ae08745Sheppo  * operating on them within a descriptor ring
26301ae08745Sheppo  */
26311ae08745Sheppo static int
26323af08d82Slm66018 vd_process_desc_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
26331ae08745Sheppo {
26341ae08745Sheppo 	size_t			expected;
26351ae08745Sheppo 	vd_dring_inband_msg_t	*desc_msg = (vd_dring_inband_msg_t *)msg;
26361ae08745Sheppo 
26371ae08745Sheppo 
26381ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
26391ae08745Sheppo 
26401ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
2641d10e4ef2Snarayan 	    VIO_DESC_DATA)) {
2642d10e4ef2Snarayan 		PR1("Message is not an in-band-descriptor message");
2643d10e4ef2Snarayan 		return (ENOMSG);
2644d10e4ef2Snarayan 	}
26451ae08745Sheppo 
26461ae08745Sheppo 	if (msglen < sizeof (*desc_msg)) {
26473af08d82Slm66018 		PR0("Expected at least %lu-byte descriptor message; "
26481ae08745Sheppo 		    "received %lu bytes", sizeof (*desc_msg), msglen);
26491ae08745Sheppo 		return (EBADMSG);
26501ae08745Sheppo 	}
26511ae08745Sheppo 
26521ae08745Sheppo 	if (msglen != (expected = expected_inband_size(desc_msg))) {
26533af08d82Slm66018 		PR0("Expected %lu-byte descriptor message; "
26541ae08745Sheppo 		    "received %lu bytes", expected, msglen);
26551ae08745Sheppo 		return (EBADMSG);
26561ae08745Sheppo 	}
26571ae08745Sheppo 
2658d10e4ef2Snarayan 	if (vd_check_seq_num(vd, desc_msg->hdr.seq_num) != 0)
26591ae08745Sheppo 		return (EBADMSG);
26601ae08745Sheppo 
2661d10e4ef2Snarayan 	/*
2662d10e4ef2Snarayan 	 * Valid message:  Set up the in-band descriptor task and process the
2663d10e4ef2Snarayan 	 * request.  Arrange to acknowledge the client's message, unless an
2664d10e4ef2Snarayan 	 * error processing the descriptor task results in setting
2665d10e4ef2Snarayan 	 * VIO_SUBTYPE_NACK
2666d10e4ef2Snarayan 	 */
2667d10e4ef2Snarayan 	PR1("Valid in-band-descriptor message");
2668d10e4ef2Snarayan 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
26693af08d82Slm66018 
26703af08d82Slm66018 	ASSERT(vd->inband_task.msg != NULL);
26713af08d82Slm66018 
26723af08d82Slm66018 	bcopy(msg, vd->inband_task.msg, msglen);
2673d10e4ef2Snarayan 	vd->inband_task.msglen	= msglen;
26743af08d82Slm66018 
26753af08d82Slm66018 	/*
26763af08d82Slm66018 	 * The task request is now the payload of the message
26773af08d82Slm66018 	 * that was just copied into the body of the task.
26783af08d82Slm66018 	 */
26793af08d82Slm66018 	desc_msg = (vd_dring_inband_msg_t *)vd->inband_task.msg;
2680d10e4ef2Snarayan 	vd->inband_task.request	= &desc_msg->payload;
26813af08d82Slm66018 
2682d10e4ef2Snarayan 	return (vd_process_task(&vd->inband_task));
26831ae08745Sheppo }
26841ae08745Sheppo 
26851ae08745Sheppo static int
2686d10e4ef2Snarayan vd_process_element(vd_t *vd, vd_task_type_t type, uint32_t idx,
26873af08d82Slm66018     vio_msg_t *msg, size_t msglen)
26881ae08745Sheppo {
26891ae08745Sheppo 	int			status;
2690d10e4ef2Snarayan 	boolean_t		ready;
2691d10e4ef2Snarayan 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
26921ae08745Sheppo 
26931ae08745Sheppo 
2694d10e4ef2Snarayan 	/* Accept the updated dring element */
2695d10e4ef2Snarayan 	if ((status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) {
26963af08d82Slm66018 		PR0("ldc_mem_dring_acquire() returned errno %d", status);
26971ae08745Sheppo 		return (status);
26981ae08745Sheppo 	}
2699d10e4ef2Snarayan 	ready = (elem->hdr.dstate == VIO_DESC_READY);
2700d10e4ef2Snarayan 	if (ready) {
2701d10e4ef2Snarayan 		elem->hdr.dstate = VIO_DESC_ACCEPTED;
2702d10e4ef2Snarayan 	} else {
27033af08d82Slm66018 		PR0("descriptor %u not ready", idx);
2704d10e4ef2Snarayan 		VD_DUMP_DRING_ELEM(elem);
2705d10e4ef2Snarayan 	}
2706d10e4ef2Snarayan 	if ((status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) {
27073af08d82Slm66018 		PR0("ldc_mem_dring_release() returned errno %d", status);
27081ae08745Sheppo 		return (status);
27091ae08745Sheppo 	}
2710d10e4ef2Snarayan 	if (!ready)
2711d10e4ef2Snarayan 		return (EBUSY);
27121ae08745Sheppo 
27131ae08745Sheppo 
2714d10e4ef2Snarayan 	/* Initialize a task and process the accepted element */
2715d10e4ef2Snarayan 	PR1("Processing dring element %u", idx);
2716d10e4ef2Snarayan 	vd->dring_task[idx].type	= type;
27173af08d82Slm66018 
27183af08d82Slm66018 	/* duplicate msg buf for cookies etc. */
27193af08d82Slm66018 	bcopy(msg, vd->dring_task[idx].msg, msglen);
27203af08d82Slm66018 
2721d10e4ef2Snarayan 	vd->dring_task[idx].msglen	= msglen;
2722*205eeb1aSlm66018 	return (vd_process_task(&vd->dring_task[idx]));
27231ae08745Sheppo }
27241ae08745Sheppo 
27251ae08745Sheppo static int
2726d10e4ef2Snarayan vd_process_element_range(vd_t *vd, int start, int end,
27273af08d82Slm66018     vio_msg_t *msg, size_t msglen)
2728d10e4ef2Snarayan {
2729d10e4ef2Snarayan 	int		i, n, nelem, status = 0;
2730d10e4ef2Snarayan 	boolean_t	inprogress = B_FALSE;
2731d10e4ef2Snarayan 	vd_task_type_t	type;
2732d10e4ef2Snarayan 
2733d10e4ef2Snarayan 
2734d10e4ef2Snarayan 	ASSERT(start >= 0);
2735d10e4ef2Snarayan 	ASSERT(end >= 0);
2736d10e4ef2Snarayan 
2737d10e4ef2Snarayan 	/*
2738d10e4ef2Snarayan 	 * Arrange to acknowledge the client's message, unless an error
2739d10e4ef2Snarayan 	 * processing one of the dring elements results in setting
2740d10e4ef2Snarayan 	 * VIO_SUBTYPE_NACK
2741d10e4ef2Snarayan 	 */
2742d10e4ef2Snarayan 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
2743d10e4ef2Snarayan 
2744d10e4ef2Snarayan 	/*
2745d10e4ef2Snarayan 	 * Process the dring elements in the range
2746d10e4ef2Snarayan 	 */
2747d10e4ef2Snarayan 	nelem = ((end < start) ? end + vd->dring_len : end) - start + 1;
2748d10e4ef2Snarayan 	for (i = start, n = nelem; n > 0; i = (i + 1) % vd->dring_len, n--) {
2749d10e4ef2Snarayan 		((vio_dring_msg_t *)msg)->end_idx = i;
2750d10e4ef2Snarayan 		type = (n == 1) ? VD_FINAL_RANGE_TASK : VD_NONFINAL_RANGE_TASK;
27513af08d82Slm66018 		status = vd_process_element(vd, type, i, msg, msglen);
2752d10e4ef2Snarayan 		if (status == EINPROGRESS)
2753d10e4ef2Snarayan 			inprogress = B_TRUE;
2754d10e4ef2Snarayan 		else if (status != 0)
2755d10e4ef2Snarayan 			break;
2756d10e4ef2Snarayan 	}
2757d10e4ef2Snarayan 
2758d10e4ef2Snarayan 	/*
2759d10e4ef2Snarayan 	 * If some, but not all, operations of a multi-element range are in
2760d10e4ef2Snarayan 	 * progress, wait for other operations to complete before returning
2761d10e4ef2Snarayan 	 * (which will result in "ack" or "nack" of the message).  Note that
2762d10e4ef2Snarayan 	 * all outstanding operations will need to complete, not just the ones
2763d10e4ef2Snarayan 	 * corresponding to the current range of dring elements; howevever, as
2764d10e4ef2Snarayan 	 * this situation is an error case, performance is less critical.
2765d10e4ef2Snarayan 	 */
2766d10e4ef2Snarayan 	if ((nelem > 1) && (status != EINPROGRESS) && inprogress)
2767d10e4ef2Snarayan 		ddi_taskq_wait(vd->completionq);
2768d10e4ef2Snarayan 
2769d10e4ef2Snarayan 	return (status);
2770d10e4ef2Snarayan }
2771d10e4ef2Snarayan 
2772d10e4ef2Snarayan static int
27733af08d82Slm66018 vd_process_dring_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
27741ae08745Sheppo {
27751ae08745Sheppo 	vio_dring_msg_t	*dring_msg = (vio_dring_msg_t *)msg;
27761ae08745Sheppo 
27771ae08745Sheppo 
27781ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
27791ae08745Sheppo 
27801ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
27811ae08745Sheppo 	    VIO_DRING_DATA)) {
2782d10e4ef2Snarayan 		PR1("Message is not a dring-data message");
2783d10e4ef2Snarayan 		return (ENOMSG);
27841ae08745Sheppo 	}
27851ae08745Sheppo 
27861ae08745Sheppo 	if (msglen != sizeof (*dring_msg)) {
27873af08d82Slm66018 		PR0("Expected %lu-byte dring message; received %lu bytes",
27881ae08745Sheppo 		    sizeof (*dring_msg), msglen);
27891ae08745Sheppo 		return (EBADMSG);
27901ae08745Sheppo 	}
27911ae08745Sheppo 
2792d10e4ef2Snarayan 	if (vd_check_seq_num(vd, dring_msg->seq_num) != 0)
27931ae08745Sheppo 		return (EBADMSG);
27941ae08745Sheppo 
27951ae08745Sheppo 	if (dring_msg->dring_ident != vd->dring_ident) {
27963af08d82Slm66018 		PR0("Expected dring ident %lu; received ident %lu",
27971ae08745Sheppo 		    vd->dring_ident, dring_msg->dring_ident);
27981ae08745Sheppo 		return (EBADMSG);
27991ae08745Sheppo 	}
28001ae08745Sheppo 
2801d10e4ef2Snarayan 	if (dring_msg->start_idx >= vd->dring_len) {
28023af08d82Slm66018 		PR0("\"start_idx\" = %u; must be less than %u",
2803d10e4ef2Snarayan 		    dring_msg->start_idx, vd->dring_len);
2804d10e4ef2Snarayan 		return (EBADMSG);
2805d10e4ef2Snarayan 	}
28061ae08745Sheppo 
2807d10e4ef2Snarayan 	if ((dring_msg->end_idx < 0) ||
2808d10e4ef2Snarayan 	    (dring_msg->end_idx >= vd->dring_len)) {
28093af08d82Slm66018 		PR0("\"end_idx\" = %u; must be >= 0 and less than %u",
2810d10e4ef2Snarayan 		    dring_msg->end_idx, vd->dring_len);
2811d10e4ef2Snarayan 		return (EBADMSG);
2812d10e4ef2Snarayan 	}
2813d10e4ef2Snarayan 
2814d10e4ef2Snarayan 	/* Valid message; process range of updated dring elements */
2815d10e4ef2Snarayan 	PR1("Processing descriptor range, start = %u, end = %u",
2816d10e4ef2Snarayan 	    dring_msg->start_idx, dring_msg->end_idx);
2817d10e4ef2Snarayan 	return (vd_process_element_range(vd, dring_msg->start_idx,
28183af08d82Slm66018 	    dring_msg->end_idx, msg, msglen));
28191ae08745Sheppo }
28201ae08745Sheppo 
28211ae08745Sheppo static int
28221ae08745Sheppo recv_msg(ldc_handle_t ldc_handle, void *msg, size_t *nbytes)
28231ae08745Sheppo {
28241ae08745Sheppo 	int	retry, status;
28251ae08745Sheppo 	size_t	size = *nbytes;
28261ae08745Sheppo 
28271ae08745Sheppo 
28281ae08745Sheppo 	for (retry = 0, status = ETIMEDOUT;
28291ae08745Sheppo 	    retry < vds_ldc_retries && status == ETIMEDOUT;
28301ae08745Sheppo 	    retry++) {
28311ae08745Sheppo 		PR1("ldc_read() attempt %d", (retry + 1));
28321ae08745Sheppo 		*nbytes = size;
28331ae08745Sheppo 		status = ldc_read(ldc_handle, msg, nbytes);
28341ae08745Sheppo 	}
28351ae08745Sheppo 
28363af08d82Slm66018 	if (status) {
28373af08d82Slm66018 		PR0("ldc_read() returned errno %d", status);
28383af08d82Slm66018 		if (status != ECONNRESET)
28393af08d82Slm66018 			return (ENOMSG);
28401ae08745Sheppo 		return (status);
28411ae08745Sheppo 	} else if (*nbytes == 0) {
28421ae08745Sheppo 		PR1("ldc_read() returned 0 and no message read");
28431ae08745Sheppo 		return (ENOMSG);
28441ae08745Sheppo 	}
28451ae08745Sheppo 
28461ae08745Sheppo 	PR1("RCVD %lu-byte message", *nbytes);
28471ae08745Sheppo 	return (0);
28481ae08745Sheppo }
28491ae08745Sheppo 
28501ae08745Sheppo static int
28513af08d82Slm66018 vd_do_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
28521ae08745Sheppo {
28531ae08745Sheppo 	int		status;
28541ae08745Sheppo 
28551ae08745Sheppo 
28561ae08745Sheppo 	PR1("Processing (%x/%x/%x) message", msg->tag.vio_msgtype,
28571ae08745Sheppo 	    msg->tag.vio_subtype, msg->tag.vio_subtype_env);
28583af08d82Slm66018 #ifdef	DEBUG
28593af08d82Slm66018 	vd_decode_tag(msg);
28603af08d82Slm66018 #endif
28611ae08745Sheppo 
28621ae08745Sheppo 	/*
28631ae08745Sheppo 	 * Validate session ID up front, since it applies to all messages
28641ae08745Sheppo 	 * once set
28651ae08745Sheppo 	 */
28661ae08745Sheppo 	if ((msg->tag.vio_sid != vd->sid) && (vd->initialized & VD_SID)) {
28673af08d82Slm66018 		PR0("Expected SID %u, received %u", vd->sid,
28681ae08745Sheppo 		    msg->tag.vio_sid);
28691ae08745Sheppo 		return (EBADMSG);
28701ae08745Sheppo 	}
28711ae08745Sheppo 
28723af08d82Slm66018 	PR1("\tWhile in state %d (%s)", vd->state, vd_decode_state(vd->state));
28731ae08745Sheppo 
28741ae08745Sheppo 	/*
28751ae08745Sheppo 	 * Process the received message based on connection state
28761ae08745Sheppo 	 */
28771ae08745Sheppo 	switch (vd->state) {
28781ae08745Sheppo 	case VD_STATE_INIT:	/* expect version message */
28790a55fbb7Slm66018 		if ((status = vd_process_ver_msg(vd, msg, msglen)) != 0)
28801ae08745Sheppo 			return (status);
28811ae08745Sheppo 
28821ae08745Sheppo 		/* Version negotiated, move to that state */
28831ae08745Sheppo 		vd->state = VD_STATE_VER;
28841ae08745Sheppo 		return (0);
28851ae08745Sheppo 
28861ae08745Sheppo 	case VD_STATE_VER:	/* expect attribute message */
28871ae08745Sheppo 		if ((status = vd_process_attr_msg(vd, msg, msglen)) != 0)
28881ae08745Sheppo 			return (status);
28891ae08745Sheppo 
28901ae08745Sheppo 		/* Attributes exchanged, move to that state */
28911ae08745Sheppo 		vd->state = VD_STATE_ATTR;
28921ae08745Sheppo 		return (0);
28931ae08745Sheppo 
28941ae08745Sheppo 	case VD_STATE_ATTR:
28951ae08745Sheppo 		switch (vd->xfer_mode) {
28961ae08745Sheppo 		case VIO_DESC_MODE:	/* expect RDX message */
28971ae08745Sheppo 			if ((status = process_rdx_msg(msg, msglen)) != 0)
28981ae08745Sheppo 				return (status);
28991ae08745Sheppo 
29001ae08745Sheppo 			/* Ready to receive in-band descriptors */
29011ae08745Sheppo 			vd->state = VD_STATE_DATA;
29021ae08745Sheppo 			return (0);
29031ae08745Sheppo 
29041ae08745Sheppo 		case VIO_DRING_MODE:	/* expect register-dring message */
29051ae08745Sheppo 			if ((status =
29061ae08745Sheppo 			    vd_process_dring_reg_msg(vd, msg, msglen)) != 0)
29071ae08745Sheppo 				return (status);
29081ae08745Sheppo 
29091ae08745Sheppo 			/* One dring negotiated, move to that state */
29101ae08745Sheppo 			vd->state = VD_STATE_DRING;
29111ae08745Sheppo 			return (0);
29121ae08745Sheppo 
29131ae08745Sheppo 		default:
29141ae08745Sheppo 			ASSERT("Unsupported transfer mode");
29153af08d82Slm66018 			PR0("Unsupported transfer mode");
29161ae08745Sheppo 			return (ENOTSUP);
29171ae08745Sheppo 		}
29181ae08745Sheppo 
29191ae08745Sheppo 	case VD_STATE_DRING:	/* expect RDX, register-dring, or unreg-dring */
29201ae08745Sheppo 		if ((status = process_rdx_msg(msg, msglen)) == 0) {
29211ae08745Sheppo 			/* Ready to receive data */
29221ae08745Sheppo 			vd->state = VD_STATE_DATA;
29231ae08745Sheppo 			return (0);
29241ae08745Sheppo 		} else if (status != ENOMSG) {
29251ae08745Sheppo 			return (status);
29261ae08745Sheppo 		}
29271ae08745Sheppo 
29281ae08745Sheppo 
29291ae08745Sheppo 		/*
29301ae08745Sheppo 		 * If another register-dring message is received, stay in
29311ae08745Sheppo 		 * dring state in case the client sends RDX; although the
29321ae08745Sheppo 		 * protocol allows multiple drings, this server does not
29331ae08745Sheppo 		 * support using more than one
29341ae08745Sheppo 		 */
29351ae08745Sheppo 		if ((status =
29361ae08745Sheppo 		    vd_process_dring_reg_msg(vd, msg, msglen)) != ENOMSG)
29371ae08745Sheppo 			return (status);
29381ae08745Sheppo 
29391ae08745Sheppo 		/*
29401ae08745Sheppo 		 * Acknowledge an unregister-dring message, but reset the
29411ae08745Sheppo 		 * connection anyway:  Although the protocol allows
29421ae08745Sheppo 		 * unregistering drings, this server cannot serve a vdisk
29431ae08745Sheppo 		 * without its only dring
29441ae08745Sheppo 		 */
29451ae08745Sheppo 		status = vd_process_dring_unreg_msg(vd, msg, msglen);
29461ae08745Sheppo 		return ((status == 0) ? ENOTSUP : status);
29471ae08745Sheppo 
29481ae08745Sheppo 	case VD_STATE_DATA:
29491ae08745Sheppo 		switch (vd->xfer_mode) {
29501ae08745Sheppo 		case VIO_DESC_MODE:	/* expect in-band-descriptor message */
29513af08d82Slm66018 			return (vd_process_desc_msg(vd, msg, msglen));
29521ae08745Sheppo 
29531ae08745Sheppo 		case VIO_DRING_MODE:	/* expect dring-data or unreg-dring */
29541ae08745Sheppo 			/*
29551ae08745Sheppo 			 * Typically expect dring-data messages, so handle
29561ae08745Sheppo 			 * them first
29571ae08745Sheppo 			 */
29581ae08745Sheppo 			if ((status = vd_process_dring_msg(vd, msg,
29593af08d82Slm66018 			    msglen)) != ENOMSG)
29601ae08745Sheppo 				return (status);
29611ae08745Sheppo 
29621ae08745Sheppo 			/*
29631ae08745Sheppo 			 * Acknowledge an unregister-dring message, but reset
29641ae08745Sheppo 			 * the connection anyway:  Although the protocol
29651ae08745Sheppo 			 * allows unregistering drings, this server cannot
29661ae08745Sheppo 			 * serve a vdisk without its only dring
29671ae08745Sheppo 			 */
29681ae08745Sheppo 			status = vd_process_dring_unreg_msg(vd, msg, msglen);
29691ae08745Sheppo 			return ((status == 0) ? ENOTSUP : status);
29701ae08745Sheppo 
29711ae08745Sheppo 		default:
29721ae08745Sheppo 			ASSERT("Unsupported transfer mode");
29733af08d82Slm66018 			PR0("Unsupported transfer mode");
29741ae08745Sheppo 			return (ENOTSUP);
29751ae08745Sheppo 		}
29761ae08745Sheppo 
29771ae08745Sheppo 	default:
29781ae08745Sheppo 		ASSERT("Invalid client connection state");
29793af08d82Slm66018 		PR0("Invalid client connection state");
29801ae08745Sheppo 		return (ENOTSUP);
29811ae08745Sheppo 	}
29821ae08745Sheppo }
29831ae08745Sheppo 
2984d10e4ef2Snarayan static int
29853af08d82Slm66018 vd_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
29861ae08745Sheppo {
29871ae08745Sheppo 	int		status;
29881ae08745Sheppo 	boolean_t	reset_ldc = B_FALSE;
2989*205eeb1aSlm66018 	vd_task_t	task;
29901ae08745Sheppo 
29911ae08745Sheppo 	/*
29921ae08745Sheppo 	 * Check that the message is at least big enough for a "tag", so that
29931ae08745Sheppo 	 * message processing can proceed based on tag-specified message type
29941ae08745Sheppo 	 */
29951ae08745Sheppo 	if (msglen < sizeof (vio_msg_tag_t)) {
29963af08d82Slm66018 		PR0("Received short (%lu-byte) message", msglen);
29971ae08745Sheppo 		/* Can't "nack" short message, so drop the big hammer */
29983af08d82Slm66018 		PR0("initiating full reset");
2999d10e4ef2Snarayan 		vd_need_reset(vd, B_TRUE);
3000d10e4ef2Snarayan 		return (EBADMSG);
30011ae08745Sheppo 	}
30021ae08745Sheppo 
30031ae08745Sheppo 	/*
30041ae08745Sheppo 	 * Process the message
30051ae08745Sheppo 	 */
30063af08d82Slm66018 	switch (status = vd_do_process_msg(vd, msg, msglen)) {
30071ae08745Sheppo 	case 0:
30081ae08745Sheppo 		/* "ack" valid, successfully-processed messages */
30091ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
30101ae08745Sheppo 		break;
30111ae08745Sheppo 
3012d10e4ef2Snarayan 	case EINPROGRESS:
3013d10e4ef2Snarayan 		/* The completion handler will "ack" or "nack" the message */
3014d10e4ef2Snarayan 		return (EINPROGRESS);
30151ae08745Sheppo 	case ENOMSG:
30163af08d82Slm66018 		PR0("Received unexpected message");
30171ae08745Sheppo 		_NOTE(FALLTHROUGH);
30181ae08745Sheppo 	case EBADMSG:
30191ae08745Sheppo 	case ENOTSUP:
3020*205eeb1aSlm66018 		/* "transport" error will cause NACK of invalid messages */
30211ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
30221ae08745Sheppo 		break;
30231ae08745Sheppo 
30241ae08745Sheppo 	default:
3025*205eeb1aSlm66018 		/* "transport" error will cause NACK of invalid messages */
30261ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
30271ae08745Sheppo 		/* An LDC error probably occurred, so try resetting it */
30281ae08745Sheppo 		reset_ldc = B_TRUE;
30291ae08745Sheppo 		break;
30301ae08745Sheppo 	}
30311ae08745Sheppo 
30323af08d82Slm66018 	PR1("\tResulting in state %d (%s)", vd->state,
30333af08d82Slm66018 	    vd_decode_state(vd->state));
30343af08d82Slm66018 
3035*205eeb1aSlm66018 	/* populate the task so we can dispatch it on the taskq */
3036*205eeb1aSlm66018 	task.vd = vd;
3037*205eeb1aSlm66018 	task.msg = msg;
3038*205eeb1aSlm66018 	task.msglen = msglen;
3039*205eeb1aSlm66018 
3040*205eeb1aSlm66018 	/*
3041*205eeb1aSlm66018 	 * Queue a task to send the notification that the operation completed.
3042*205eeb1aSlm66018 	 * We need to ensure that requests are responded to in the correct
3043*205eeb1aSlm66018 	 * order and since the taskq is processed serially this ordering
3044*205eeb1aSlm66018 	 * is maintained.
3045*205eeb1aSlm66018 	 */
3046*205eeb1aSlm66018 	(void) ddi_taskq_dispatch(vd->completionq, vd_serial_notify,
3047*205eeb1aSlm66018 	    &task, DDI_SLEEP);
3048*205eeb1aSlm66018 
3049*205eeb1aSlm66018 	/*
3050*205eeb1aSlm66018 	 * To ensure handshake negotiations do not happen out of order, such
3051*205eeb1aSlm66018 	 * requests that come through this path should not be done in parallel
3052*205eeb1aSlm66018 	 * so we need to wait here until the response is sent to the client.
3053*205eeb1aSlm66018 	 */
3054*205eeb1aSlm66018 	ddi_taskq_wait(vd->completionq);
30551ae08745Sheppo 
3056d10e4ef2Snarayan 	/* Arrange to reset the connection for nack'ed or failed messages */
30573af08d82Slm66018 	if ((status != 0) || reset_ldc) {
30583af08d82Slm66018 		PR0("initiating %s reset",
30593af08d82Slm66018 		    (reset_ldc) ? "full" : "soft");
3060d10e4ef2Snarayan 		vd_need_reset(vd, reset_ldc);
30613af08d82Slm66018 	}
3062d10e4ef2Snarayan 
3063d10e4ef2Snarayan 	return (status);
3064d10e4ef2Snarayan }
3065d10e4ef2Snarayan 
3066d10e4ef2Snarayan static boolean_t
3067d10e4ef2Snarayan vd_enabled(vd_t *vd)
3068d10e4ef2Snarayan {
3069d10e4ef2Snarayan 	boolean_t	enabled;
3070d10e4ef2Snarayan 
3071d10e4ef2Snarayan 	mutex_enter(&vd->lock);
3072d10e4ef2Snarayan 	enabled = vd->enabled;
3073d10e4ef2Snarayan 	mutex_exit(&vd->lock);
3074d10e4ef2Snarayan 	return (enabled);
30751ae08745Sheppo }
30761ae08745Sheppo 
30771ae08745Sheppo static void
30780a55fbb7Slm66018 vd_recv_msg(void *arg)
30791ae08745Sheppo {
30801ae08745Sheppo 	vd_t	*vd = (vd_t *)arg;
30813af08d82Slm66018 	int	rv = 0, status = 0;
30821ae08745Sheppo 
30831ae08745Sheppo 	ASSERT(vd != NULL);
30843af08d82Slm66018 
3085d10e4ef2Snarayan 	PR2("New task to receive incoming message(s)");
30863af08d82Slm66018 
30873af08d82Slm66018 
3088d10e4ef2Snarayan 	while (vd_enabled(vd) && status == 0) {
3089d10e4ef2Snarayan 		size_t		msglen, msgsize;
30903af08d82Slm66018 		ldc_status_t	lstatus;
3091d10e4ef2Snarayan 
30920a55fbb7Slm66018 		/*
3093d10e4ef2Snarayan 		 * Receive and process a message
30940a55fbb7Slm66018 		 */
3095d10e4ef2Snarayan 		vd_reset_if_needed(vd);	/* can change vd->max_msglen */
30963af08d82Slm66018 
30973af08d82Slm66018 		/*
30983af08d82Slm66018 		 * check if channel is UP - else break out of loop
30993af08d82Slm66018 		 */
31003af08d82Slm66018 		status = ldc_status(vd->ldc_handle, &lstatus);
31013af08d82Slm66018 		if (lstatus != LDC_UP) {
31023af08d82Slm66018 			PR0("channel not up (status=%d), exiting recv loop\n",
31033af08d82Slm66018 			    lstatus);
31043af08d82Slm66018 			break;
31053af08d82Slm66018 		}
31063af08d82Slm66018 
31073af08d82Slm66018 		ASSERT(vd->max_msglen != 0);
31083af08d82Slm66018 
3109d10e4ef2Snarayan 		msgsize = vd->max_msglen; /* stable copy for alloc/free */
31103af08d82Slm66018 		msglen	= msgsize;	  /* actual len after recv_msg() */
31113af08d82Slm66018 
31123af08d82Slm66018 		status = recv_msg(vd->ldc_handle, vd->vio_msgp, &msglen);
31133af08d82Slm66018 		switch (status) {
31143af08d82Slm66018 		case 0:
31153af08d82Slm66018 			rv = vd_process_msg(vd, (vio_msg_t *)vd->vio_msgp,
31163af08d82Slm66018 			    msglen);
31173af08d82Slm66018 			/* check if max_msglen changed */
31183af08d82Slm66018 			if (msgsize != vd->max_msglen) {
31193af08d82Slm66018 				PR0("max_msglen changed 0x%lx to 0x%lx bytes\n",
31203af08d82Slm66018 				    msgsize, vd->max_msglen);
31213af08d82Slm66018 				kmem_free(vd->vio_msgp, msgsize);
31223af08d82Slm66018 				vd->vio_msgp =
31233af08d82Slm66018 				    kmem_alloc(vd->max_msglen, KM_SLEEP);
31243af08d82Slm66018 			}
31253af08d82Slm66018 			if (rv == EINPROGRESS)
31263af08d82Slm66018 				continue;
31273af08d82Slm66018 			break;
31283af08d82Slm66018 
31293af08d82Slm66018 		case ENOMSG:
31303af08d82Slm66018 			break;
31313af08d82Slm66018 
31323af08d82Slm66018 		case ECONNRESET:
31333af08d82Slm66018 			PR0("initiating soft reset (ECONNRESET)\n");
31343af08d82Slm66018 			vd_need_reset(vd, B_FALSE);
31353af08d82Slm66018 			status = 0;
31363af08d82Slm66018 			break;
31373af08d82Slm66018 
31383af08d82Slm66018 		default:
3139d10e4ef2Snarayan 			/* Probably an LDC failure; arrange to reset it */
31403af08d82Slm66018 			PR0("initiating full reset (status=0x%x)", status);
3141d10e4ef2Snarayan 			vd_need_reset(vd, B_TRUE);
31423af08d82Slm66018 			break;
31430a55fbb7Slm66018 		}
31441ae08745Sheppo 	}
31453af08d82Slm66018 
3146d10e4ef2Snarayan 	PR2("Task finished");
31470a55fbb7Slm66018 }
31480a55fbb7Slm66018 
31490a55fbb7Slm66018 static uint_t
31501ae08745Sheppo vd_handle_ldc_events(uint64_t event, caddr_t arg)
31511ae08745Sheppo {
31521ae08745Sheppo 	vd_t	*vd = (vd_t *)(void *)arg;
31533af08d82Slm66018 	int	status;
31541ae08745Sheppo 
31551ae08745Sheppo 	ASSERT(vd != NULL);
3156d10e4ef2Snarayan 
3157d10e4ef2Snarayan 	if (!vd_enabled(vd))
3158d10e4ef2Snarayan 		return (LDC_SUCCESS);
3159d10e4ef2Snarayan 
31603af08d82Slm66018 	if (event & LDC_EVT_DOWN) {
316134683adeSsg70180 		PR0("LDC_EVT_DOWN: LDC channel went down");
31623af08d82Slm66018 
31633af08d82Slm66018 		vd_need_reset(vd, B_TRUE);
31643af08d82Slm66018 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
31653af08d82Slm66018 		    DDI_SLEEP);
31663af08d82Slm66018 		if (status == DDI_FAILURE) {
31673af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
31683af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
31693af08d82Slm66018 		}
31703af08d82Slm66018 	}
31713af08d82Slm66018 
3172d10e4ef2Snarayan 	if (event & LDC_EVT_RESET) {
31733af08d82Slm66018 		PR0("LDC_EVT_RESET: LDC channel was reset");
31743af08d82Slm66018 
31753af08d82Slm66018 		if (vd->state != VD_STATE_INIT) {
31763af08d82Slm66018 			PR0("scheduling full reset");
31773af08d82Slm66018 			vd_need_reset(vd, B_FALSE);
31783af08d82Slm66018 			status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
31793af08d82Slm66018 			    vd, DDI_SLEEP);
31803af08d82Slm66018 			if (status == DDI_FAILURE) {
31813af08d82Slm66018 				PR0("cannot schedule task to recv msg\n");
31823af08d82Slm66018 				vd_need_reset(vd, B_TRUE);
31833af08d82Slm66018 			}
31843af08d82Slm66018 
31853af08d82Slm66018 		} else {
31863af08d82Slm66018 			PR0("channel already reset, ignoring...\n");
31873af08d82Slm66018 			PR0("doing ldc up...\n");
31883af08d82Slm66018 			(void) ldc_up(vd->ldc_handle);
31893af08d82Slm66018 		}
31903af08d82Slm66018 
3191d10e4ef2Snarayan 		return (LDC_SUCCESS);
3192d10e4ef2Snarayan 	}
3193d10e4ef2Snarayan 
3194d10e4ef2Snarayan 	if (event & LDC_EVT_UP) {
31953af08d82Slm66018 		PR0("EVT_UP: LDC is up\nResetting client connection state");
31963af08d82Slm66018 		PR0("initiating soft reset");
3197d10e4ef2Snarayan 		vd_need_reset(vd, B_FALSE);
31983af08d82Slm66018 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
31993af08d82Slm66018 		    vd, DDI_SLEEP);
32003af08d82Slm66018 		if (status == DDI_FAILURE) {
32013af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
32023af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
32033af08d82Slm66018 			return (LDC_SUCCESS);
32043af08d82Slm66018 		}
3205d10e4ef2Snarayan 	}
3206d10e4ef2Snarayan 
3207d10e4ef2Snarayan 	if (event & LDC_EVT_READ) {
3208d10e4ef2Snarayan 		int	status;
3209d10e4ef2Snarayan 
3210d10e4ef2Snarayan 		PR1("New data available");
3211d10e4ef2Snarayan 		/* Queue a task to receive the new data */
3212d10e4ef2Snarayan 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
3213d10e4ef2Snarayan 		    DDI_SLEEP);
32143af08d82Slm66018 
32153af08d82Slm66018 		if (status == DDI_FAILURE) {
32163af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
32173af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
32183af08d82Slm66018 		}
3219d10e4ef2Snarayan 	}
3220d10e4ef2Snarayan 
3221d10e4ef2Snarayan 	return (LDC_SUCCESS);
32221ae08745Sheppo }
32231ae08745Sheppo 
32241ae08745Sheppo static uint_t
32251ae08745Sheppo vds_check_for_vd(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
32261ae08745Sheppo {
32271ae08745Sheppo 	_NOTE(ARGUNUSED(key, val))
32281ae08745Sheppo 	(*((uint_t *)arg))++;
32291ae08745Sheppo 	return (MH_WALK_TERMINATE);
32301ae08745Sheppo }
32311ae08745Sheppo 
32321ae08745Sheppo 
32331ae08745Sheppo static int
32341ae08745Sheppo vds_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
32351ae08745Sheppo {
32361ae08745Sheppo 	uint_t	vd_present = 0;
32371ae08745Sheppo 	minor_t	instance;
32381ae08745Sheppo 	vds_t	*vds;
32391ae08745Sheppo 
32401ae08745Sheppo 
32411ae08745Sheppo 	switch (cmd) {
32421ae08745Sheppo 	case DDI_DETACH:
32431ae08745Sheppo 		/* the real work happens below */
32441ae08745Sheppo 		break;
32451ae08745Sheppo 	case DDI_SUSPEND:
3246d10e4ef2Snarayan 		PR0("No action required for DDI_SUSPEND");
32471ae08745Sheppo 		return (DDI_SUCCESS);
32481ae08745Sheppo 	default:
32493af08d82Slm66018 		PR0("Unrecognized \"cmd\"");
32501ae08745Sheppo 		return (DDI_FAILURE);
32511ae08745Sheppo 	}
32521ae08745Sheppo 
32531ae08745Sheppo 	ASSERT(cmd == DDI_DETACH);
32541ae08745Sheppo 	instance = ddi_get_instance(dip);
32551ae08745Sheppo 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
32563af08d82Slm66018 		PR0("Could not get state for instance %u", instance);
32571ae08745Sheppo 		ddi_soft_state_free(vds_state, instance);
32581ae08745Sheppo 		return (DDI_FAILURE);
32591ae08745Sheppo 	}
32601ae08745Sheppo 
32611ae08745Sheppo 	/* Do no detach when serving any vdisks */
32621ae08745Sheppo 	mod_hash_walk(vds->vd_table, vds_check_for_vd, &vd_present);
32631ae08745Sheppo 	if (vd_present) {
32641ae08745Sheppo 		PR0("Not detaching because serving vdisks");
32651ae08745Sheppo 		return (DDI_FAILURE);
32661ae08745Sheppo 	}
32671ae08745Sheppo 
32681ae08745Sheppo 	PR0("Detaching");
3269445b4c2eSsb155480 	if (vds->initialized & VDS_MDEG) {
32701ae08745Sheppo 		(void) mdeg_unregister(vds->mdeg);
3271445b4c2eSsb155480 		kmem_free(vds->ispecp->specp, sizeof (vds_prop_template));
3272445b4c2eSsb155480 		kmem_free(vds->ispecp, sizeof (mdeg_node_spec_t));
3273445b4c2eSsb155480 		vds->ispecp = NULL;
3274445b4c2eSsb155480 		vds->mdeg = NULL;
3275445b4c2eSsb155480 	}
3276445b4c2eSsb155480 
32771ae08745Sheppo 	if (vds->initialized & VDS_LDI)
32781ae08745Sheppo 		(void) ldi_ident_release(vds->ldi_ident);
32791ae08745Sheppo 	mod_hash_destroy_hash(vds->vd_table);
32801ae08745Sheppo 	ddi_soft_state_free(vds_state, instance);
32811ae08745Sheppo 	return (DDI_SUCCESS);
32821ae08745Sheppo }
32831ae08745Sheppo 
32841ae08745Sheppo static boolean_t
32851ae08745Sheppo is_pseudo_device(dev_info_t *dip)
32861ae08745Sheppo {
32871ae08745Sheppo 	dev_info_t	*parent, *root = ddi_root_node();
32881ae08745Sheppo 
32891ae08745Sheppo 
32901ae08745Sheppo 	for (parent = ddi_get_parent(dip); (parent != NULL) && (parent != root);
32911ae08745Sheppo 	    parent = ddi_get_parent(parent)) {
32921ae08745Sheppo 		if (strcmp(ddi_get_name(parent), DEVI_PSEUDO_NEXNAME) == 0)
32931ae08745Sheppo 			return (B_TRUE);
32941ae08745Sheppo 	}
32951ae08745Sheppo 
32961ae08745Sheppo 	return (B_FALSE);
32971ae08745Sheppo }
32981ae08745Sheppo 
32991ae08745Sheppo static int
33000a55fbb7Slm66018 vd_setup_full_disk(vd_t *vd)
33010a55fbb7Slm66018 {
33020a55fbb7Slm66018 	int		rval, status;
33030a55fbb7Slm66018 	major_t		major = getmajor(vd->dev[0]);
33040a55fbb7Slm66018 	minor_t		minor = getminor(vd->dev[0]) - VD_ENTIRE_DISK_SLICE;
33054bac2208Snarayan 	struct dk_minfo	dk_minfo;
33060a55fbb7Slm66018 
33074bac2208Snarayan 	/*
33084bac2208Snarayan 	 * At this point, vdisk_size is set to the size of partition 2 but
33094bac2208Snarayan 	 * this does not represent the size of the disk because partition 2
33104bac2208Snarayan 	 * may not cover the entire disk and its size does not include reserved
33114bac2208Snarayan 	 * blocks. So we update vdisk_size to be the size of the entire disk.
33124bac2208Snarayan 	 */
33134bac2208Snarayan 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCGMEDIAINFO,
33144bac2208Snarayan 	    (intptr_t)&dk_minfo, (vd_open_flags | FKIOCTL),
33154bac2208Snarayan 	    kcred, &rval)) != 0) {
3316690555a1Sachartre 		PRN("ldi_ioctl(DKIOCGMEDIAINFO) returned errno %d",
33174bac2208Snarayan 		    status);
33180a55fbb7Slm66018 		return (status);
33190a55fbb7Slm66018 	}
33204bac2208Snarayan 	vd->vdisk_size = dk_minfo.dki_capacity;
33210a55fbb7Slm66018 
33220a55fbb7Slm66018 	/* Set full-disk parameters */
33230a55fbb7Slm66018 	vd->vdisk_type	= VD_DISK_TYPE_DISK;
33240a55fbb7Slm66018 	vd->nslices	= (sizeof (vd->dev))/(sizeof (vd->dev[0]));
33250a55fbb7Slm66018 
33260a55fbb7Slm66018 	/* Move dev number and LDI handle to entire-disk-slice array elements */
33270a55fbb7Slm66018 	vd->dev[VD_ENTIRE_DISK_SLICE]		= vd->dev[0];
33280a55fbb7Slm66018 	vd->dev[0]				= 0;
33290a55fbb7Slm66018 	vd->ldi_handle[VD_ENTIRE_DISK_SLICE]	= vd->ldi_handle[0];
33300a55fbb7Slm66018 	vd->ldi_handle[0]			= NULL;
33310a55fbb7Slm66018 
33320a55fbb7Slm66018 	/* Initialize device numbers for remaining slices and open them */
33330a55fbb7Slm66018 	for (int slice = 0; slice < vd->nslices; slice++) {
33340a55fbb7Slm66018 		/*
33350a55fbb7Slm66018 		 * Skip the entire-disk slice, as it's already open and its
33360a55fbb7Slm66018 		 * device known
33370a55fbb7Slm66018 		 */
33380a55fbb7Slm66018 		if (slice == VD_ENTIRE_DISK_SLICE)
33390a55fbb7Slm66018 			continue;
33400a55fbb7Slm66018 		ASSERT(vd->dev[slice] == 0);
33410a55fbb7Slm66018 		ASSERT(vd->ldi_handle[slice] == NULL);
33420a55fbb7Slm66018 
33430a55fbb7Slm66018 		/*
33440a55fbb7Slm66018 		 * Construct the device number for the current slice
33450a55fbb7Slm66018 		 */
33460a55fbb7Slm66018 		vd->dev[slice] = makedevice(major, (minor + slice));
33470a55fbb7Slm66018 
33480a55fbb7Slm66018 		/*
334934683adeSsg70180 		 * Open all slices of the disk to serve them to the client.
335034683adeSsg70180 		 * Slices are opened exclusively to prevent other threads or
335134683adeSsg70180 		 * processes in the service domain from performing I/O to
335234683adeSsg70180 		 * slices being accessed by a client.  Failure to open a slice
335334683adeSsg70180 		 * results in vds not serving this disk, as the client could
335434683adeSsg70180 		 * attempt (and should be able) to access any slice immediately.
335534683adeSsg70180 		 * Any slices successfully opened before a failure will get
335634683adeSsg70180 		 * closed by vds_destroy_vd() as a result of the error returned
335734683adeSsg70180 		 * by this function.
335834683adeSsg70180 		 *
335934683adeSsg70180 		 * We need to do the open with FNDELAY so that opening an empty
336034683adeSsg70180 		 * slice does not fail.
33610a55fbb7Slm66018 		 */
33620a55fbb7Slm66018 		PR0("Opening device major %u, minor %u = slice %u",
33630a55fbb7Slm66018 		    major, minor, slice);
33640a55fbb7Slm66018 		if ((status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK,
336534683adeSsg70180 		    vd_open_flags | FNDELAY, kcred, &vd->ldi_handle[slice],
33660a55fbb7Slm66018 		    vd->vds->ldi_ident)) != 0) {
3367690555a1Sachartre 			PRN("ldi_open_by_dev() returned errno %d "
33680a55fbb7Slm66018 			    "for slice %u", status, slice);
33690a55fbb7Slm66018 			/* vds_destroy_vd() will close any open slices */
3370690555a1Sachartre 			vd->ldi_handle[slice] = NULL;
33710a55fbb7Slm66018 			return (status);
33720a55fbb7Slm66018 		}
33730a55fbb7Slm66018 	}
33740a55fbb7Slm66018 
33750a55fbb7Slm66018 	return (0);
33760a55fbb7Slm66018 }
33770a55fbb7Slm66018 
33780a55fbb7Slm66018 static int
33794bac2208Snarayan vd_setup_partition_efi(vd_t *vd)
33804bac2208Snarayan {
33814bac2208Snarayan 	efi_gpt_t *gpt;
33824bac2208Snarayan 	efi_gpe_t *gpe;
33834bac2208Snarayan 	struct uuid uuid = EFI_RESERVED;
33844bac2208Snarayan 	uint32_t crc;
33854bac2208Snarayan 	int length;
33864bac2208Snarayan 
33874bac2208Snarayan 	length = sizeof (efi_gpt_t) + sizeof (efi_gpe_t);
33884bac2208Snarayan 
33894bac2208Snarayan 	gpt = kmem_zalloc(length, KM_SLEEP);
33904bac2208Snarayan 	gpe = (efi_gpe_t *)(gpt + 1);
33914bac2208Snarayan 
33924bac2208Snarayan 	gpt->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
33934bac2208Snarayan 	gpt->efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
33944bac2208Snarayan 	gpt->efi_gpt_HeaderSize = LE_32(sizeof (efi_gpt_t));
33954bac2208Snarayan 	gpt->efi_gpt_FirstUsableLBA = LE_64(0ULL);
33964bac2208Snarayan 	gpt->efi_gpt_LastUsableLBA = LE_64(vd->vdisk_size - 1);
33974bac2208Snarayan 	gpt->efi_gpt_NumberOfPartitionEntries = LE_32(1);
33984bac2208Snarayan 	gpt->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (efi_gpe_t));
33994bac2208Snarayan 
34004bac2208Snarayan 	UUID_LE_CONVERT(gpe->efi_gpe_PartitionTypeGUID, uuid);
34014bac2208Snarayan 	gpe->efi_gpe_StartingLBA = gpt->efi_gpt_FirstUsableLBA;
34024bac2208Snarayan 	gpe->efi_gpe_EndingLBA = gpt->efi_gpt_LastUsableLBA;
34034bac2208Snarayan 
34044bac2208Snarayan 	CRC32(crc, gpe, sizeof (efi_gpe_t), -1U, crc32_table);
34054bac2208Snarayan 	gpt->efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
34064bac2208Snarayan 
34074bac2208Snarayan 	CRC32(crc, gpt, sizeof (efi_gpt_t), -1U, crc32_table);
34084bac2208Snarayan 	gpt->efi_gpt_HeaderCRC32 = LE_32(~crc);
34094bac2208Snarayan 
34104bac2208Snarayan 	vd->dk_efi.dki_lba = 0;
34114bac2208Snarayan 	vd->dk_efi.dki_length = length;
34124bac2208Snarayan 	vd->dk_efi.dki_data = gpt;
34134bac2208Snarayan 
34144bac2208Snarayan 	return (0);
34154bac2208Snarayan }
34164bac2208Snarayan 
34174bac2208Snarayan static int
34183c96341aSnarayan vd_setup_file(vd_t *vd)
34193c96341aSnarayan {
34203c96341aSnarayan 	int 		i, rval, status;
3421690555a1Sachartre 	ushort_t	sum;
34223c96341aSnarayan 	vattr_t		vattr;
34233c96341aSnarayan 	dev_t		dev;
342487a7269eSachartre 	size_t		size;
34253c96341aSnarayan 	char		*file_path = vd->device_path;
34263c96341aSnarayan 	char		dev_path[MAXPATHLEN + 1];
342787a7269eSachartre 	char		prefix;
34283c96341aSnarayan 	ldi_handle_t	lhandle;
34293c96341aSnarayan 	struct dk_cinfo	dk_cinfo;
3430690555a1Sachartre 	struct dk_label label;
34313c96341aSnarayan 
34323c96341aSnarayan 	/* make sure the file is valid */
34333c96341aSnarayan 	if ((status = lookupname(file_path, UIO_SYSSPACE, FOLLOW,
34343c96341aSnarayan 	    NULLVPP, &vd->file_vnode)) != 0) {
3435690555a1Sachartre 		PRN("Cannot lookup file(%s) errno %d", file_path, status);
34363c96341aSnarayan 		return (status);
34373c96341aSnarayan 	}
34383c96341aSnarayan 
34393c96341aSnarayan 	if (vd->file_vnode->v_type != VREG) {
3440690555a1Sachartre 		PRN("Invalid file type (%s)\n", file_path);
34413c96341aSnarayan 		VN_RELE(vd->file_vnode);
34423c96341aSnarayan 		return (EBADF);
34433c96341aSnarayan 	}
34443c96341aSnarayan 	VN_RELE(vd->file_vnode);
34453c96341aSnarayan 
34463c96341aSnarayan 	if ((status = vn_open(file_path, UIO_SYSSPACE, vd_open_flags | FOFFMAX,
34473c96341aSnarayan 	    0, &vd->file_vnode, 0, 0)) != 0) {
3448690555a1Sachartre 		PRN("vn_open(%s) = errno %d", file_path, status);
34493c96341aSnarayan 		return (status);
34503c96341aSnarayan 	}
34513c96341aSnarayan 
3452690555a1Sachartre 	/*
3453690555a1Sachartre 	 * We set vd->file now so that vds_destroy_vd will take care of
3454690555a1Sachartre 	 * closing the file and releasing the vnode in case of an error.
3455690555a1Sachartre 	 */
3456690555a1Sachartre 	vd->file = B_TRUE;
3457690555a1Sachartre 	vd->pseudo = B_FALSE;
3458690555a1Sachartre 
34593c96341aSnarayan 	vattr.va_mask = AT_SIZE;
34603c96341aSnarayan 	if ((status = VOP_GETATTR(vd->file_vnode, &vattr, 0, kcred)) != 0) {
3461690555a1Sachartre 		PRN("VOP_GETATTR(%s) = errno %d", file_path, status);
34623c96341aSnarayan 		return (EIO);
34633c96341aSnarayan 	}
34643c96341aSnarayan 
34653c96341aSnarayan 	vd->file_size = vattr.va_size;
34663c96341aSnarayan 	/* size should be at least sizeof(dk_label) */
34673c96341aSnarayan 	if (vd->file_size < sizeof (struct dk_label)) {
34683c96341aSnarayan 		PRN("Size of file has to be at least %ld bytes",
34693c96341aSnarayan 		    sizeof (struct dk_label));
34703c96341aSnarayan 		return (EIO);
34713c96341aSnarayan 	}
34723c96341aSnarayan 
3473690555a1Sachartre 	if (vd->file_vnode->v_flag & VNOMAP) {
3474690555a1Sachartre 		PRN("File %s cannot be mapped", file_path);
34753c96341aSnarayan 		return (EIO);
34763c96341aSnarayan 	}
34773c96341aSnarayan 
3478690555a1Sachartre 	/* read label from file */
3479690555a1Sachartre 	if (VD_FILE_LABEL_READ(vd, &label) < 0) {
3480690555a1Sachartre 		PRN("Can't read label from %s", file_path);
3481690555a1Sachartre 		return (EIO);
3482690555a1Sachartre 	}
34833c96341aSnarayan 
34843c96341aSnarayan 	/* label checksum */
3485690555a1Sachartre 	sum = vd_lbl2cksum(&label);
34863c96341aSnarayan 
3487690555a1Sachartre 	if (label.dkl_magic != DKL_MAGIC || label.dkl_cksum != sum) {
34883c96341aSnarayan 		PR0("%s has an invalid disk label "
34893c96341aSnarayan 		    "(magic=%x cksum=%x (expect %x))",
3490690555a1Sachartre 		    file_path, label.dkl_magic, label.dkl_cksum, sum);
34913c96341aSnarayan 
34923c96341aSnarayan 		/* default label */
3493690555a1Sachartre 		bzero(&label, sizeof (struct dk_label));
34943c96341aSnarayan 
34953c96341aSnarayan 		/*
34963c96341aSnarayan 		 * We must have a resonable number of cylinders and sectors so
34973c96341aSnarayan 		 * that newfs can run using default values.
34983c96341aSnarayan 		 *
34993c96341aSnarayan 		 * if (disk_size < 2MB)
35003c96341aSnarayan 		 * 	phys_cylinders = disk_size / 100K
35013c96341aSnarayan 		 * else
35023c96341aSnarayan 		 * 	phys_cylinders = disk_size / 300K
35033c96341aSnarayan 		 *
35043c96341aSnarayan 		 * phys_cylinders = (phys_cylinders == 0) ? 1 : phys_cylinders
35053c96341aSnarayan 		 * alt_cylinders = (phys_cylinders > 2) ? 2 : 0;
35063c96341aSnarayan 		 * data_cylinders = phys_cylinders - alt_cylinders
35073c96341aSnarayan 		 *
35083c96341aSnarayan 		 * sectors = disk_size / (phys_cylinders * blk_size)
35093c96341aSnarayan 		 */
35103c96341aSnarayan 		if (vd->file_size < (2 * 1024 * 1024))
3511690555a1Sachartre 			label.dkl_pcyl = vd->file_size / (100 * 1024);
35123c96341aSnarayan 		else
3513690555a1Sachartre 			label.dkl_pcyl = vd->file_size / (300 * 1024);
35143c96341aSnarayan 
3515690555a1Sachartre 		if (label.dkl_pcyl == 0)
3516690555a1Sachartre 			label.dkl_pcyl = 1;
35173c96341aSnarayan 
3518690555a1Sachartre 		if (label.dkl_pcyl > 2)
3519690555a1Sachartre 			label.dkl_acyl = 2;
35203c96341aSnarayan 		else
3521690555a1Sachartre 			label.dkl_acyl = 0;
35223c96341aSnarayan 
3523690555a1Sachartre 		label.dkl_nsect = vd->file_size /
3524690555a1Sachartre 		    (DEV_BSIZE * label.dkl_pcyl);
3525690555a1Sachartre 		label.dkl_ncyl = label.dkl_pcyl - label.dkl_acyl;
3526690555a1Sachartre 		label.dkl_nhead = 1;
3527690555a1Sachartre 		label.dkl_write_reinstruct = 0;
3528690555a1Sachartre 		label.dkl_read_reinstruct = 0;
3529690555a1Sachartre 		label.dkl_rpm = 7200;
3530690555a1Sachartre 		label.dkl_apc = 0;
3531690555a1Sachartre 		label.dkl_intrlv = 0;
3532690555a1Sachartre 		label.dkl_magic = DKL_MAGIC;
35333c96341aSnarayan 
35343c96341aSnarayan 		PR0("requested disk size: %ld bytes\n", vd->file_size);
3535690555a1Sachartre 		PR0("setup: ncyl=%d nhead=%d nsec=%d\n", label.dkl_pcyl,
3536690555a1Sachartre 		    label.dkl_nhead, label.dkl_nsect);
35373c96341aSnarayan 		PR0("provided disk size: %ld bytes\n", (uint64_t)
3538690555a1Sachartre 		    (label.dkl_pcyl *
3539690555a1Sachartre 		    label.dkl_nhead * label.dkl_nsect * DEV_BSIZE));
35403c96341aSnarayan 
354187a7269eSachartre 		if (vd->file_size < (1ULL << 20)) {
354287a7269eSachartre 			size = vd->file_size >> 10;
354387a7269eSachartre 			prefix = 'K'; /* Kilobyte */
354487a7269eSachartre 		} else if (vd->file_size < (1ULL << 30)) {
354587a7269eSachartre 			size = vd->file_size >> 20;
354687a7269eSachartre 			prefix = 'M'; /* Megabyte */
354787a7269eSachartre 		} else if (vd->file_size < (1ULL << 40)) {
354887a7269eSachartre 			size = vd->file_size >> 30;
354987a7269eSachartre 			prefix = 'G'; /* Gigabyte */
355087a7269eSachartre 		} else {
355187a7269eSachartre 			size = vd->file_size >> 40;
355287a7269eSachartre 			prefix = 'T'; /* Terabyte */
355387a7269eSachartre 		}
355487a7269eSachartre 
35553c96341aSnarayan 		/*
35563c96341aSnarayan 		 * We must have a correct label name otherwise format(1m) will
35573c96341aSnarayan 		 * not recognized the disk as labeled.
35583c96341aSnarayan 		 */
3559690555a1Sachartre 		(void) snprintf(label.dkl_asciilabel, LEN_DKL_ASCII,
356087a7269eSachartre 		    "SUN-DiskImage-%ld%cB cyl %d alt %d hd %d sec %d",
356187a7269eSachartre 		    size, prefix,
3562690555a1Sachartre 		    label.dkl_ncyl, label.dkl_acyl, label.dkl_nhead,
3563690555a1Sachartre 		    label.dkl_nsect);
35643c96341aSnarayan 
35653c96341aSnarayan 		/* default VTOC */
3566690555a1Sachartre 		label.dkl_vtoc.v_version = V_VERSION;
3567690555a1Sachartre 		label.dkl_vtoc.v_nparts = V_NUMPAR;
3568690555a1Sachartre 		label.dkl_vtoc.v_sanity = VTOC_SANE;
3569690555a1Sachartre 		label.dkl_vtoc.v_part[2].p_tag = V_BACKUP;
3570690555a1Sachartre 		label.dkl_map[2].dkl_cylno = 0;
3571690555a1Sachartre 		label.dkl_map[2].dkl_nblk = label.dkl_ncyl *
3572690555a1Sachartre 		    label.dkl_nhead * label.dkl_nsect;
3573690555a1Sachartre 		label.dkl_map[0] = label.dkl_map[2];
3574690555a1Sachartre 		label.dkl_map[0] = label.dkl_map[2];
3575690555a1Sachartre 		label.dkl_cksum = vd_lbl2cksum(&label);
3576690555a1Sachartre 
3577690555a1Sachartre 		/* write default label to file */
357887a7269eSachartre 		if ((rval = vd_file_set_vtoc(vd, &label)) != 0) {
3579690555a1Sachartre 			PRN("Can't write label to %s", file_path);
358087a7269eSachartre 			return (rval);
3581690555a1Sachartre 		}
35823c96341aSnarayan 	}
35833c96341aSnarayan 
3584690555a1Sachartre 	vd->nslices = label.dkl_vtoc.v_nparts;
35853c96341aSnarayan 
35863c96341aSnarayan 	/* sector size = block size = DEV_BSIZE */
358787a7269eSachartre 	vd->vdisk_size = vd->file_size / DEV_BSIZE;
35883c96341aSnarayan 	vd->vdisk_type = VD_DISK_TYPE_DISK;
35893c96341aSnarayan 	vd->vdisk_label = VD_DISK_LABEL_VTOC;
35903c96341aSnarayan 	vd->max_xfer_sz = maxphys / DEV_BSIZE; /* default transfer size */
35913c96341aSnarayan 
35923c96341aSnarayan 	/* Get max_xfer_sz from the device where the file is */
35933c96341aSnarayan 	dev = vd->file_vnode->v_vfsp->vfs_dev;
35943c96341aSnarayan 	dev_path[0] = NULL;
35953c96341aSnarayan 	if (ddi_dev_pathname(dev, S_IFBLK, dev_path) == DDI_SUCCESS) {
35963c96341aSnarayan 		PR0("underlying device = %s\n", dev_path);
35973c96341aSnarayan 	}
35983c96341aSnarayan 
35993c96341aSnarayan 	if ((status = ldi_open_by_dev(&dev, OTYP_BLK, FREAD,
36003c96341aSnarayan 	    kcred, &lhandle, vd->vds->ldi_ident)) != 0) {
36013c96341aSnarayan 		PR0("ldi_open_by_dev() returned errno %d for device %s",
36023c96341aSnarayan 		    status, dev_path);
36033c96341aSnarayan 	} else {
36043c96341aSnarayan 		if ((status = ldi_ioctl(lhandle, DKIOCINFO,
36053c96341aSnarayan 		    (intptr_t)&dk_cinfo, (vd_open_flags | FKIOCTL), kcred,
36063c96341aSnarayan 		    &rval)) != 0) {
36073c96341aSnarayan 			PR0("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
36083c96341aSnarayan 			    status, dev_path);
36093c96341aSnarayan 		} else {
36103c96341aSnarayan 			/*
36113c96341aSnarayan 			 * Store the device's max transfer size for
36123c96341aSnarayan 			 * return to the client
36133c96341aSnarayan 			 */
36143c96341aSnarayan 			vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
36153c96341aSnarayan 		}
36163c96341aSnarayan 
36173c96341aSnarayan 		PR0("close the device %s", dev_path);
36183c96341aSnarayan 		(void) ldi_close(lhandle, FREAD, kcred);
36193c96341aSnarayan 	}
36203c96341aSnarayan 
3621*205eeb1aSlm66018 	PR0("using file %s, dev %s, max_xfer = %u blks",
36223c96341aSnarayan 	    file_path, dev_path, vd->max_xfer_sz);
36233c96341aSnarayan 
3624690555a1Sachartre 	vd->dk_geom.dkg_ncyl = label.dkl_ncyl;
3625690555a1Sachartre 	vd->dk_geom.dkg_acyl = label.dkl_acyl;
3626690555a1Sachartre 	vd->dk_geom.dkg_pcyl = label.dkl_pcyl;
3627690555a1Sachartre 	vd->dk_geom.dkg_nhead = label.dkl_nhead;
3628690555a1Sachartre 	vd->dk_geom.dkg_nsect = label.dkl_nsect;
3629690555a1Sachartre 	vd->dk_geom.dkg_intrlv = label.dkl_intrlv;
3630690555a1Sachartre 	vd->dk_geom.dkg_apc = label.dkl_apc;
3631690555a1Sachartre 	vd->dk_geom.dkg_rpm = label.dkl_rpm;
3632690555a1Sachartre 	vd->dk_geom.dkg_write_reinstruct = label.dkl_write_reinstruct;
3633690555a1Sachartre 	vd->dk_geom.dkg_read_reinstruct = label.dkl_read_reinstruct;
36343c96341aSnarayan 
3635690555a1Sachartre 	vd->vtoc.v_sanity = label.dkl_vtoc.v_sanity;
3636690555a1Sachartre 	vd->vtoc.v_version = label.dkl_vtoc.v_version;
36373c96341aSnarayan 	vd->vtoc.v_sectorsz = DEV_BSIZE;
3638690555a1Sachartre 	vd->vtoc.v_nparts = label.dkl_vtoc.v_nparts;
36393c96341aSnarayan 
3640690555a1Sachartre 	bcopy(label.dkl_vtoc.v_volume, vd->vtoc.v_volume,
36413c96341aSnarayan 	    LEN_DKL_VVOL);
3642690555a1Sachartre 	bcopy(label.dkl_asciilabel, vd->vtoc.v_asciilabel,
36433c96341aSnarayan 	    LEN_DKL_ASCII);
36443c96341aSnarayan 
36453c96341aSnarayan 	for (i = 0; i < vd->nslices; i++) {
3646690555a1Sachartre 		vd->vtoc.timestamp[i] = label.dkl_vtoc.v_timestamp[i];
3647690555a1Sachartre 		vd->vtoc.v_part[i].p_tag = label.dkl_vtoc.v_part[i].p_tag;
3648690555a1Sachartre 		vd->vtoc.v_part[i].p_flag = label.dkl_vtoc.v_part[i].p_flag;
3649690555a1Sachartre 		vd->vtoc.v_part[i].p_start = label.dkl_map[i].dkl_cylno *
3650690555a1Sachartre 		    label.dkl_nhead * label.dkl_nsect;
3651690555a1Sachartre 		vd->vtoc.v_part[i].p_size = label.dkl_map[i].dkl_nblk;
36523c96341aSnarayan 		vd->ldi_handle[i] = NULL;
36533c96341aSnarayan 		vd->dev[i] = NULL;
36543c96341aSnarayan 	}
36553c96341aSnarayan 
365687a7269eSachartre 	/* Setup devid for the disk image */
365787a7269eSachartre 
365887a7269eSachartre 	status = vd_file_read_devid(vd, &vd->file_devid);
365987a7269eSachartre 
366087a7269eSachartre 	if (status == 0) {
366187a7269eSachartre 		/* a valid devid was found */
366287a7269eSachartre 		return (0);
366387a7269eSachartre 	}
366487a7269eSachartre 
366587a7269eSachartre 	if (status != EINVAL) {
366687a7269eSachartre 		/*
366787a7269eSachartre 		 * There was an error while trying to read the devid. So this
366887a7269eSachartre 		 * disk image may have a devid but we are unable to read it.
366987a7269eSachartre 		 */
367087a7269eSachartre 		PR0("can not read devid for %s", file_path);
367187a7269eSachartre 		vd->file_devid = NULL;
367287a7269eSachartre 		return (0);
367387a7269eSachartre 	}
367487a7269eSachartre 
367587a7269eSachartre 	/*
367687a7269eSachartre 	 * No valid device id was found so we create one. Note that a failure
367787a7269eSachartre 	 * to create a device id is not fatal and does not prevent the disk
367887a7269eSachartre 	 * image from being attached.
367987a7269eSachartre 	 */
368087a7269eSachartre 	PR1("creating devid for %s", file_path);
368187a7269eSachartre 
368287a7269eSachartre 	if (ddi_devid_init(vd->vds->dip, DEVID_FAB, NULL, 0,
368387a7269eSachartre 	    &vd->file_devid) != DDI_SUCCESS) {
368487a7269eSachartre 		PR0("fail to create devid for %s", file_path);
368587a7269eSachartre 		vd->file_devid = NULL;
368687a7269eSachartre 		return (0);
368787a7269eSachartre 	}
368887a7269eSachartre 
368987a7269eSachartre 	/* write devid to the disk image */
369087a7269eSachartre 	if (vd_file_write_devid(vd, vd->file_devid) != 0) {
369187a7269eSachartre 		PR0("fail to write devid for %s", file_path);
369287a7269eSachartre 		ddi_devid_free(vd->file_devid);
369387a7269eSachartre 		vd->file_devid = NULL;
369487a7269eSachartre 	}
369587a7269eSachartre 
36963c96341aSnarayan 	return (0);
36973c96341aSnarayan }
36983c96341aSnarayan 
36993c96341aSnarayan static int
37003c96341aSnarayan vd_setup_vd(vd_t *vd)
37011ae08745Sheppo {
3702e1ebb9ecSlm66018 	int		rval, status;
37031ae08745Sheppo 	dev_info_t	*dip;
37041ae08745Sheppo 	struct dk_cinfo	dk_cinfo;
37053c96341aSnarayan 	char		*device_path = vd->device_path;
37061ae08745Sheppo 
37074bac2208Snarayan 	/*
37084bac2208Snarayan 	 * We need to open with FNDELAY so that opening an empty partition
37094bac2208Snarayan 	 * does not fail.
37104bac2208Snarayan 	 */
37114bac2208Snarayan 	if ((status = ldi_open_by_name(device_path, vd_open_flags | FNDELAY,
37124bac2208Snarayan 	    kcred, &vd->ldi_handle[0], vd->vds->ldi_ident)) != 0) {
37133c96341aSnarayan 		PR0("ldi_open_by_name(%s) = errno %d", device_path, status);
3714690555a1Sachartre 		vd->ldi_handle[0] = NULL;
37153c96341aSnarayan 
37163c96341aSnarayan 		/* this may not be a device try opening as a file */
37173c96341aSnarayan 		if (status == ENXIO || status == ENODEV)
37183c96341aSnarayan 			status = vd_setup_file(vd);
37193c96341aSnarayan 		if (status) {
3720690555a1Sachartre 			PRN("Cannot use device/file (%s), errno=%d\n",
37213c96341aSnarayan 			    device_path, status);
37223c96341aSnarayan 			if (status == ENXIO || status == ENODEV ||
37233c96341aSnarayan 			    status == ENOENT) {
37243c96341aSnarayan 				return (EAGAIN);
37253c96341aSnarayan 			}
37263c96341aSnarayan 		}
37270a55fbb7Slm66018 		return (status);
37280a55fbb7Slm66018 	}
37290a55fbb7Slm66018 
37304bac2208Snarayan 	/*
37314bac2208Snarayan 	 * nslices must be updated now so that vds_destroy_vd() will close
37324bac2208Snarayan 	 * the slice we have just opened in case of an error.
37334bac2208Snarayan 	 */
37344bac2208Snarayan 	vd->nslices = 1;
37353c96341aSnarayan 	vd->file = B_FALSE;
37364bac2208Snarayan 
3737e1ebb9ecSlm66018 	/* Get device number and size of backing device */
37380a55fbb7Slm66018 	if ((status = ldi_get_dev(vd->ldi_handle[0], &vd->dev[0])) != 0) {
37391ae08745Sheppo 		PRN("ldi_get_dev() returned errno %d for %s",
3740e1ebb9ecSlm66018 		    status, device_path);
37411ae08745Sheppo 		return (status);
37421ae08745Sheppo 	}
37430a55fbb7Slm66018 	if (ldi_get_size(vd->ldi_handle[0], &vd->vdisk_size) != DDI_SUCCESS) {
3744e1ebb9ecSlm66018 		PRN("ldi_get_size() failed for %s", device_path);
37451ae08745Sheppo 		return (EIO);
37461ae08745Sheppo 	}
3747e1ebb9ecSlm66018 	vd->vdisk_size = lbtodb(vd->vdisk_size);	/* convert to blocks */
37481ae08745Sheppo 
3749e1ebb9ecSlm66018 	/* Verify backing device supports dk_cinfo, dk_geom, and vtoc */
3750e1ebb9ecSlm66018 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO,
3751e1ebb9ecSlm66018 	    (intptr_t)&dk_cinfo, (vd_open_flags | FKIOCTL), kcred,
3752e1ebb9ecSlm66018 	    &rval)) != 0) {
3753e1ebb9ecSlm66018 		PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
3754e1ebb9ecSlm66018 		    status, device_path);
3755e1ebb9ecSlm66018 		return (status);
3756e1ebb9ecSlm66018 	}
3757e1ebb9ecSlm66018 	if (dk_cinfo.dki_partition >= V_NUMPAR) {
3758e1ebb9ecSlm66018 		PRN("slice %u >= maximum slice %u for %s",
3759e1ebb9ecSlm66018 		    dk_cinfo.dki_partition, V_NUMPAR, device_path);
3760e1ebb9ecSlm66018 		return (EIO);
3761e1ebb9ecSlm66018 	}
37624bac2208Snarayan 
37634bac2208Snarayan 	status = vd_read_vtoc(vd->ldi_handle[0], &vd->vtoc, &vd->vdisk_label);
37644bac2208Snarayan 
37654bac2208Snarayan 	if (status != 0) {
37664bac2208Snarayan 		PRN("vd_read_vtoc returned errno %d for %s",
3767e1ebb9ecSlm66018 		    status, device_path);
3768e1ebb9ecSlm66018 		return (status);
3769e1ebb9ecSlm66018 	}
37704bac2208Snarayan 
37714bac2208Snarayan 	if (vd->vdisk_label == VD_DISK_LABEL_VTOC &&
37724bac2208Snarayan 	    (status = ldi_ioctl(vd->ldi_handle[0], DKIOCGGEOM,
37734bac2208Snarayan 	    (intptr_t)&vd->dk_geom, (vd_open_flags | FKIOCTL),
37744bac2208Snarayan 	    kcred, &rval)) != 0) {
37754bac2208Snarayan 		PRN("ldi_ioctl(DKIOCGEOM) returned errno %d for %s",
3776e1ebb9ecSlm66018 		    status, device_path);
3777e1ebb9ecSlm66018 		return (status);
3778e1ebb9ecSlm66018 	}
3779e1ebb9ecSlm66018 
3780e1ebb9ecSlm66018 	/* Store the device's max transfer size for return to the client */
3781e1ebb9ecSlm66018 	vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
3782e1ebb9ecSlm66018 
3783e1ebb9ecSlm66018 	/* Determine if backing device is a pseudo device */
37841ae08745Sheppo 	if ((dip = ddi_hold_devi_by_instance(getmajor(vd->dev[0]),
37851ae08745Sheppo 	    dev_to_instance(vd->dev[0]), 0))  == NULL) {
3786e1ebb9ecSlm66018 		PRN("%s is no longer accessible", device_path);
37871ae08745Sheppo 		return (EIO);
37881ae08745Sheppo 	}
37891ae08745Sheppo 	vd->pseudo = is_pseudo_device(dip);
37901ae08745Sheppo 	ddi_release_devi(dip);
37911ae08745Sheppo 	if (vd->pseudo) {
37921ae08745Sheppo 		vd->vdisk_type	= VD_DISK_TYPE_SLICE;
37931ae08745Sheppo 		vd->nslices	= 1;
37941ae08745Sheppo 		return (0);	/* ...and we're done */
37951ae08745Sheppo 	}
37961ae08745Sheppo 
37970a55fbb7Slm66018 	/* If slice is entire-disk slice, initialize for full disk */
37980a55fbb7Slm66018 	if (dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE)
37990a55fbb7Slm66018 		return (vd_setup_full_disk(vd));
38001ae08745Sheppo 
38010a55fbb7Slm66018 
3802e1ebb9ecSlm66018 	/* Otherwise, we have a non-entire slice of a device */
38031ae08745Sheppo 	vd->vdisk_type	= VD_DISK_TYPE_SLICE;
38041ae08745Sheppo 	vd->nslices	= 1;
38051ae08745Sheppo 
38064bac2208Snarayan 	if (vd->vdisk_label == VD_DISK_LABEL_EFI) {
38074bac2208Snarayan 		status = vd_setup_partition_efi(vd);
38084bac2208Snarayan 		return (status);
38094bac2208Snarayan 	}
38101ae08745Sheppo 
3811e1ebb9ecSlm66018 	/* Initialize dk_geom structure for single-slice device */
38121ae08745Sheppo 	if (vd->dk_geom.dkg_nsect == 0) {
3813690555a1Sachartre 		PRN("%s geometry claims 0 sectors per track", device_path);
38141ae08745Sheppo 		return (EIO);
38151ae08745Sheppo 	}
38161ae08745Sheppo 	if (vd->dk_geom.dkg_nhead == 0) {
3817690555a1Sachartre 		PRN("%s geometry claims 0 heads", device_path);
38181ae08745Sheppo 		return (EIO);
38191ae08745Sheppo 	}
38201ae08745Sheppo 	vd->dk_geom.dkg_ncyl =
3821e1ebb9ecSlm66018 	    vd->vdisk_size/vd->dk_geom.dkg_nsect/vd->dk_geom.dkg_nhead;
38221ae08745Sheppo 	vd->dk_geom.dkg_acyl = 0;
38231ae08745Sheppo 	vd->dk_geom.dkg_pcyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl;
38241ae08745Sheppo 
38251ae08745Sheppo 
3826e1ebb9ecSlm66018 	/* Initialize vtoc structure for single-slice device */
38271ae08745Sheppo 	bcopy(VD_VOLUME_NAME, vd->vtoc.v_volume,
38281ae08745Sheppo 	    MIN(sizeof (VD_VOLUME_NAME), sizeof (vd->vtoc.v_volume)));
38291ae08745Sheppo 	bzero(vd->vtoc.v_part, sizeof (vd->vtoc.v_part));
38301ae08745Sheppo 	vd->vtoc.v_nparts = 1;
38311ae08745Sheppo 	vd->vtoc.v_part[0].p_tag = V_UNASSIGNED;
38321ae08745Sheppo 	vd->vtoc.v_part[0].p_flag = 0;
38331ae08745Sheppo 	vd->vtoc.v_part[0].p_start = 0;
3834e1ebb9ecSlm66018 	vd->vtoc.v_part[0].p_size = vd->vdisk_size;
38351ae08745Sheppo 	bcopy(VD_ASCIILABEL, vd->vtoc.v_asciilabel,
38361ae08745Sheppo 	    MIN(sizeof (VD_ASCIILABEL), sizeof (vd->vtoc.v_asciilabel)));
38371ae08745Sheppo 
38381ae08745Sheppo 
38391ae08745Sheppo 	return (0);
38401ae08745Sheppo }
38411ae08745Sheppo 
38421ae08745Sheppo static int
3843e1ebb9ecSlm66018 vds_do_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t ldc_id,
38441ae08745Sheppo     vd_t **vdp)
38451ae08745Sheppo {
38461ae08745Sheppo 	char			tq_name[TASKQ_NAMELEN];
38470a55fbb7Slm66018 	int			status;
38481ae08745Sheppo 	ddi_iblock_cookie_t	iblock = NULL;
38491ae08745Sheppo 	ldc_attr_t		ldc_attr;
38501ae08745Sheppo 	vd_t			*vd;
38511ae08745Sheppo 
38521ae08745Sheppo 
38531ae08745Sheppo 	ASSERT(vds != NULL);
3854e1ebb9ecSlm66018 	ASSERT(device_path != NULL);
38551ae08745Sheppo 	ASSERT(vdp != NULL);
3856e1ebb9ecSlm66018 	PR0("Adding vdisk for %s", device_path);
38571ae08745Sheppo 
38581ae08745Sheppo 	if ((vd = kmem_zalloc(sizeof (*vd), KM_NOSLEEP)) == NULL) {
38591ae08745Sheppo 		PRN("No memory for virtual disk");
38601ae08745Sheppo 		return (EAGAIN);
38611ae08745Sheppo 	}
38621ae08745Sheppo 	*vdp = vd;	/* assign here so vds_destroy_vd() can cleanup later */
38631ae08745Sheppo 	vd->vds = vds;
38643c96341aSnarayan 	(void) strncpy(vd->device_path, device_path, MAXPATHLEN);
38651ae08745Sheppo 
38660a55fbb7Slm66018 	/* Open vdisk and initialize parameters */
38673c96341aSnarayan 	if ((status = vd_setup_vd(vd)) == 0) {
38683c96341aSnarayan 		vd->initialized |= VD_DISK_READY;
38691ae08745Sheppo 
38703c96341aSnarayan 		ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
38713c96341aSnarayan 		PR0("vdisk_type = %s, pseudo = %s, file = %s, nslices = %u",
38723c96341aSnarayan 		    ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
38733c96341aSnarayan 		    (vd->pseudo ? "yes" : "no"), (vd->file ? "yes" : "no"),
38743c96341aSnarayan 		    vd->nslices);
38753c96341aSnarayan 	} else {
38763c96341aSnarayan 		if (status != EAGAIN)
38773c96341aSnarayan 			return (status);
38783c96341aSnarayan 	}
38791ae08745Sheppo 
38801ae08745Sheppo 	/* Initialize locking */
38811ae08745Sheppo 	if (ddi_get_soft_iblock_cookie(vds->dip, DDI_SOFTINT_MED,
38821ae08745Sheppo 	    &iblock) != DDI_SUCCESS) {
38831ae08745Sheppo 		PRN("Could not get iblock cookie.");
38841ae08745Sheppo 		return (EIO);
38851ae08745Sheppo 	}
38861ae08745Sheppo 
38871ae08745Sheppo 	mutex_init(&vd->lock, NULL, MUTEX_DRIVER, iblock);
38881ae08745Sheppo 	vd->initialized |= VD_LOCKING;
38891ae08745Sheppo 
38901ae08745Sheppo 
3891d10e4ef2Snarayan 	/* Create start and completion task queues for the vdisk */
3892d10e4ef2Snarayan 	(void) snprintf(tq_name, sizeof (tq_name), "vd_startq%lu", id);
38931ae08745Sheppo 	PR1("tq_name = %s", tq_name);
3894d10e4ef2Snarayan 	if ((vd->startq = ddi_taskq_create(vds->dip, tq_name, 1,
38951ae08745Sheppo 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
38961ae08745Sheppo 		PRN("Could not create task queue");
38971ae08745Sheppo 		return (EIO);
38981ae08745Sheppo 	}
3899d10e4ef2Snarayan 	(void) snprintf(tq_name, sizeof (tq_name), "vd_completionq%lu", id);
3900d10e4ef2Snarayan 	PR1("tq_name = %s", tq_name);
3901d10e4ef2Snarayan 	if ((vd->completionq = ddi_taskq_create(vds->dip, tq_name, 1,
3902d10e4ef2Snarayan 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
3903d10e4ef2Snarayan 		PRN("Could not create task queue");
3904d10e4ef2Snarayan 		return (EIO);
3905d10e4ef2Snarayan 	}
3906d10e4ef2Snarayan 	vd->enabled = 1;	/* before callback can dispatch to startq */
39071ae08745Sheppo 
39081ae08745Sheppo 
39091ae08745Sheppo 	/* Bring up LDC */
39101ae08745Sheppo 	ldc_attr.devclass	= LDC_DEV_BLK_SVC;
39111ae08745Sheppo 	ldc_attr.instance	= ddi_get_instance(vds->dip);
39121ae08745Sheppo 	ldc_attr.mode		= LDC_MODE_UNRELIABLE;
3913e1ebb9ecSlm66018 	ldc_attr.mtu		= VD_LDC_MTU;
39141ae08745Sheppo 	if ((status = ldc_init(ldc_id, &ldc_attr, &vd->ldc_handle)) != 0) {
3915690555a1Sachartre 		PRN("Could not initialize LDC channel %lu, "
3916690555a1Sachartre 		    "init failed with error %d", ldc_id, status);
39171ae08745Sheppo 		return (status);
39181ae08745Sheppo 	}
39191ae08745Sheppo 	vd->initialized |= VD_LDC;
39201ae08745Sheppo 
39211ae08745Sheppo 	if ((status = ldc_reg_callback(vd->ldc_handle, vd_handle_ldc_events,
39221ae08745Sheppo 	    (caddr_t)vd)) != 0) {
3923690555a1Sachartre 		PRN("Could not initialize LDC channel %lu,"
3924690555a1Sachartre 		    "reg_callback failed with error %d", ldc_id, status);
39251ae08745Sheppo 		return (status);
39261ae08745Sheppo 	}
39271ae08745Sheppo 
39281ae08745Sheppo 	if ((status = ldc_open(vd->ldc_handle)) != 0) {
3929690555a1Sachartre 		PRN("Could not initialize LDC channel %lu,"
3930690555a1Sachartre 		    "open failed with error %d", ldc_id, status);
39311ae08745Sheppo 		return (status);
39321ae08745Sheppo 	}
39331ae08745Sheppo 
39343af08d82Slm66018 	if ((status = ldc_up(vd->ldc_handle)) != 0) {
393534683adeSsg70180 		PR0("ldc_up() returned errno %d", status);
39363af08d82Slm66018 	}
39373af08d82Slm66018 
39384bac2208Snarayan 	/* Allocate the inband task memory handle */
39394bac2208Snarayan 	status = ldc_mem_alloc_handle(vd->ldc_handle, &(vd->inband_task.mhdl));
39404bac2208Snarayan 	if (status) {
3941690555a1Sachartre 		PRN("Could not initialize LDC channel %lu,"
3942690555a1Sachartre 		    "alloc_handle failed with error %d", ldc_id, status);
39434bac2208Snarayan 		return (ENXIO);
39444bac2208Snarayan 	}
39451ae08745Sheppo 
39461ae08745Sheppo 	/* Add the successfully-initialized vdisk to the server's table */
39471ae08745Sheppo 	if (mod_hash_insert(vds->vd_table, (mod_hash_key_t)id, vd) != 0) {
39481ae08745Sheppo 		PRN("Error adding vdisk ID %lu to table", id);
39491ae08745Sheppo 		return (EIO);
39501ae08745Sheppo 	}
39511ae08745Sheppo 
39523af08d82Slm66018 	/* Allocate the staging buffer */
39533af08d82Slm66018 	vd->max_msglen	= sizeof (vio_msg_t);	/* baseline vio message size */
39543af08d82Slm66018 	vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
39553af08d82Slm66018 
39563af08d82Slm66018 	/* store initial state */
39573af08d82Slm66018 	vd->state = VD_STATE_INIT;
39583af08d82Slm66018 
39591ae08745Sheppo 	return (0);
39601ae08745Sheppo }
39611ae08745Sheppo 
39623af08d82Slm66018 static void
39633af08d82Slm66018 vd_free_dring_task(vd_t *vdp)
39643af08d82Slm66018 {
39653af08d82Slm66018 	if (vdp->dring_task != NULL) {
39663af08d82Slm66018 		ASSERT(vdp->dring_len != 0);
39673af08d82Slm66018 		/* Free all dring_task memory handles */
39683af08d82Slm66018 		for (int i = 0; i < vdp->dring_len; i++) {
39693af08d82Slm66018 			(void) ldc_mem_free_handle(vdp->dring_task[i].mhdl);
39703af08d82Slm66018 			kmem_free(vdp->dring_task[i].msg, vdp->max_msglen);
39713af08d82Slm66018 			vdp->dring_task[i].msg = NULL;
39723af08d82Slm66018 		}
39733af08d82Slm66018 		kmem_free(vdp->dring_task,
39743af08d82Slm66018 		    (sizeof (*vdp->dring_task)) * vdp->dring_len);
39753af08d82Slm66018 		vdp->dring_task = NULL;
39763af08d82Slm66018 	}
39773af08d82Slm66018 }
39783af08d82Slm66018 
39791ae08745Sheppo /*
39801ae08745Sheppo  * Destroy the state associated with a virtual disk
39811ae08745Sheppo  */
39821ae08745Sheppo static void
39831ae08745Sheppo vds_destroy_vd(void *arg)
39841ae08745Sheppo {
39851ae08745Sheppo 	vd_t	*vd = (vd_t *)arg;
398634683adeSsg70180 	int	retry = 0, rv;
39871ae08745Sheppo 
39881ae08745Sheppo 	if (vd == NULL)
39891ae08745Sheppo 		return;
39901ae08745Sheppo 
3991d10e4ef2Snarayan 	PR0("Destroying vdisk state");
3992d10e4ef2Snarayan 
39934bac2208Snarayan 	if (vd->dk_efi.dki_data != NULL)
39944bac2208Snarayan 		kmem_free(vd->dk_efi.dki_data, vd->dk_efi.dki_length);
39954bac2208Snarayan 
39961ae08745Sheppo 	/* Disable queuing requests for the vdisk */
39971ae08745Sheppo 	if (vd->initialized & VD_LOCKING) {
39981ae08745Sheppo 		mutex_enter(&vd->lock);
39991ae08745Sheppo 		vd->enabled = 0;
40001ae08745Sheppo 		mutex_exit(&vd->lock);
40011ae08745Sheppo 	}
40021ae08745Sheppo 
4003d10e4ef2Snarayan 	/* Drain and destroy start queue (*before* destroying completionq) */
4004d10e4ef2Snarayan 	if (vd->startq != NULL)
4005d10e4ef2Snarayan 		ddi_taskq_destroy(vd->startq);	/* waits for queued tasks */
4006d10e4ef2Snarayan 
4007d10e4ef2Snarayan 	/* Drain and destroy completion queue (*before* shutting down LDC) */
4008d10e4ef2Snarayan 	if (vd->completionq != NULL)
4009d10e4ef2Snarayan 		ddi_taskq_destroy(vd->completionq);	/* waits for tasks */
4010d10e4ef2Snarayan 
40113af08d82Slm66018 	vd_free_dring_task(vd);
40123af08d82Slm66018 
401334683adeSsg70180 	/* Free the inband task memory handle */
401434683adeSsg70180 	(void) ldc_mem_free_handle(vd->inband_task.mhdl);
401534683adeSsg70180 
401634683adeSsg70180 	/* Shut down LDC */
401734683adeSsg70180 	if (vd->initialized & VD_LDC) {
401834683adeSsg70180 		/* unmap the dring */
401934683adeSsg70180 		if (vd->initialized & VD_DRING)
402034683adeSsg70180 			(void) ldc_mem_dring_unmap(vd->dring_handle);
402134683adeSsg70180 
402234683adeSsg70180 		/* close LDC channel - retry on EAGAIN */
402334683adeSsg70180 		while ((rv = ldc_close(vd->ldc_handle)) == EAGAIN) {
402434683adeSsg70180 			if (++retry > vds_ldc_retries) {
402534683adeSsg70180 				PR0("Timed out closing channel");
402634683adeSsg70180 				break;
402734683adeSsg70180 			}
402834683adeSsg70180 			drv_usecwait(vds_ldc_delay);
402934683adeSsg70180 		}
403034683adeSsg70180 		if (rv == 0) {
403134683adeSsg70180 			(void) ldc_unreg_callback(vd->ldc_handle);
403234683adeSsg70180 			(void) ldc_fini(vd->ldc_handle);
403334683adeSsg70180 		} else {
403434683adeSsg70180 			/*
403534683adeSsg70180 			 * Closing the LDC channel has failed. Ideally we should
403634683adeSsg70180 			 * fail here but there is no Zeus level infrastructure
403734683adeSsg70180 			 * to handle this. The MD has already been changed and
403834683adeSsg70180 			 * we have to do the close. So we try to do as much
403934683adeSsg70180 			 * clean up as we can.
404034683adeSsg70180 			 */
404134683adeSsg70180 			(void) ldc_set_cb_mode(vd->ldc_handle, LDC_CB_DISABLE);
404234683adeSsg70180 			while (ldc_unreg_callback(vd->ldc_handle) == EAGAIN)
404334683adeSsg70180 				drv_usecwait(vds_ldc_delay);
404434683adeSsg70180 		}
404534683adeSsg70180 	}
404634683adeSsg70180 
40473af08d82Slm66018 	/* Free the staging buffer for msgs */
40483af08d82Slm66018 	if (vd->vio_msgp != NULL) {
40493af08d82Slm66018 		kmem_free(vd->vio_msgp, vd->max_msglen);
40503af08d82Slm66018 		vd->vio_msgp = NULL;
40513af08d82Slm66018 	}
40523af08d82Slm66018 
40533af08d82Slm66018 	/* Free the inband message buffer */
40543af08d82Slm66018 	if (vd->inband_task.msg != NULL) {
40553af08d82Slm66018 		kmem_free(vd->inband_task.msg, vd->max_msglen);
40563af08d82Slm66018 		vd->inband_task.msg = NULL;
4057d10e4ef2Snarayan 	}
40583c96341aSnarayan 	if (vd->file) {
4059690555a1Sachartre 		/* Close file */
40603c96341aSnarayan 		(void) VOP_CLOSE(vd->file_vnode, vd_open_flags, 1,
40613c96341aSnarayan 		    0, kcred);
40623c96341aSnarayan 		VN_RELE(vd->file_vnode);
406387a7269eSachartre 		if (vd->file_devid != NULL)
406487a7269eSachartre 			ddi_devid_free(vd->file_devid);
40653c96341aSnarayan 	} else {
40661ae08745Sheppo 		/* Close any open backing-device slices */
40671ae08745Sheppo 		for (uint_t slice = 0; slice < vd->nslices; slice++) {
40681ae08745Sheppo 			if (vd->ldi_handle[slice] != NULL) {
40691ae08745Sheppo 				PR0("Closing slice %u", slice);
40701ae08745Sheppo 				(void) ldi_close(vd->ldi_handle[slice],
40714bac2208Snarayan 				    vd_open_flags | FNDELAY, kcred);
40721ae08745Sheppo 			}
40731ae08745Sheppo 		}
40743c96341aSnarayan 	}
40751ae08745Sheppo 
40761ae08745Sheppo 	/* Free lock */
40771ae08745Sheppo 	if (vd->initialized & VD_LOCKING)
40781ae08745Sheppo 		mutex_destroy(&vd->lock);
40791ae08745Sheppo 
40801ae08745Sheppo 	/* Finally, free the vdisk structure itself */
40811ae08745Sheppo 	kmem_free(vd, sizeof (*vd));
40821ae08745Sheppo }
40831ae08745Sheppo 
40841ae08745Sheppo static int
4085e1ebb9ecSlm66018 vds_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t ldc_id)
40861ae08745Sheppo {
40871ae08745Sheppo 	int	status;
40881ae08745Sheppo 	vd_t	*vd = NULL;
40891ae08745Sheppo 
40901ae08745Sheppo 
4091e1ebb9ecSlm66018 	if ((status = vds_do_init_vd(vds, id, device_path, ldc_id, &vd)) != 0)
40921ae08745Sheppo 		vds_destroy_vd(vd);
40931ae08745Sheppo 
40941ae08745Sheppo 	return (status);
40951ae08745Sheppo }
40961ae08745Sheppo 
40971ae08745Sheppo static int
40981ae08745Sheppo vds_do_get_ldc_id(md_t *md, mde_cookie_t vd_node, mde_cookie_t *channel,
40991ae08745Sheppo     uint64_t *ldc_id)
41001ae08745Sheppo {
41011ae08745Sheppo 	int	num_channels;
41021ae08745Sheppo 
41031ae08745Sheppo 
41041ae08745Sheppo 	/* Look for channel endpoint child(ren) of the vdisk MD node */
41051ae08745Sheppo 	if ((num_channels = md_scan_dag(md, vd_node,
41061ae08745Sheppo 	    md_find_name(md, VD_CHANNEL_ENDPOINT),
41071ae08745Sheppo 	    md_find_name(md, "fwd"), channel)) <= 0) {
41081ae08745Sheppo 		PRN("No \"%s\" found for virtual disk", VD_CHANNEL_ENDPOINT);
41091ae08745Sheppo 		return (-1);
41101ae08745Sheppo 	}
41111ae08745Sheppo 
41121ae08745Sheppo 	/* Get the "id" value for the first channel endpoint node */
41131ae08745Sheppo 	if (md_get_prop_val(md, channel[0], VD_ID_PROP, ldc_id) != 0) {
41141ae08745Sheppo 		PRN("No \"%s\" property found for \"%s\" of vdisk",
41151ae08745Sheppo 		    VD_ID_PROP, VD_CHANNEL_ENDPOINT);
41161ae08745Sheppo 		return (-1);
41171ae08745Sheppo 	}
41181ae08745Sheppo 
41191ae08745Sheppo 	if (num_channels > 1) {
41201ae08745Sheppo 		PRN("Using ID of first of multiple channels for this vdisk");
41211ae08745Sheppo 	}
41221ae08745Sheppo 
41231ae08745Sheppo 	return (0);
41241ae08745Sheppo }
41251ae08745Sheppo 
41261ae08745Sheppo static int
41271ae08745Sheppo vds_get_ldc_id(md_t *md, mde_cookie_t vd_node, uint64_t *ldc_id)
41281ae08745Sheppo {
41291ae08745Sheppo 	int		num_nodes, status;
41301ae08745Sheppo 	size_t		size;
41311ae08745Sheppo 	mde_cookie_t	*channel;
41321ae08745Sheppo 
41331ae08745Sheppo 
41341ae08745Sheppo 	if ((num_nodes = md_node_count(md)) <= 0) {
41351ae08745Sheppo 		PRN("Invalid node count in Machine Description subtree");
41361ae08745Sheppo 		return (-1);
41371ae08745Sheppo 	}
41381ae08745Sheppo 	size = num_nodes*(sizeof (*channel));
41391ae08745Sheppo 	channel = kmem_zalloc(size, KM_SLEEP);
41401ae08745Sheppo 	status = vds_do_get_ldc_id(md, vd_node, channel, ldc_id);
41411ae08745Sheppo 	kmem_free(channel, size);
41421ae08745Sheppo 
41431ae08745Sheppo 	return (status);
41441ae08745Sheppo }
41451ae08745Sheppo 
41461ae08745Sheppo static void
41471ae08745Sheppo vds_add_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
41481ae08745Sheppo {
4149e1ebb9ecSlm66018 	char		*device_path = NULL;
41501ae08745Sheppo 	uint64_t	id = 0, ldc_id = 0;
41511ae08745Sheppo 
41521ae08745Sheppo 
41531ae08745Sheppo 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
41541ae08745Sheppo 		PRN("Error getting vdisk \"%s\"", VD_ID_PROP);
41551ae08745Sheppo 		return;
41561ae08745Sheppo 	}
41571ae08745Sheppo 	PR0("Adding vdisk ID %lu", id);
41581ae08745Sheppo 	if (md_get_prop_str(md, vd_node, VD_BLOCK_DEVICE_PROP,
4159e1ebb9ecSlm66018 	    &device_path) != 0) {
41601ae08745Sheppo 		PRN("Error getting vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
41611ae08745Sheppo 		return;
41621ae08745Sheppo 	}
41631ae08745Sheppo 
41641ae08745Sheppo 	if (vds_get_ldc_id(md, vd_node, &ldc_id) != 0) {
41651ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", id);
41661ae08745Sheppo 		return;
41671ae08745Sheppo 	}
41681ae08745Sheppo 
4169e1ebb9ecSlm66018 	if (vds_init_vd(vds, id, device_path, ldc_id) != 0) {
41701ae08745Sheppo 		PRN("Failed to add vdisk ID %lu", id);
41711ae08745Sheppo 		return;
41721ae08745Sheppo 	}
41731ae08745Sheppo }
41741ae08745Sheppo 
41751ae08745Sheppo static void
41761ae08745Sheppo vds_remove_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
41771ae08745Sheppo {
41781ae08745Sheppo 	uint64_t	id = 0;
41791ae08745Sheppo 
41801ae08745Sheppo 
41811ae08745Sheppo 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
41821ae08745Sheppo 		PRN("Unable to get \"%s\" property from vdisk's MD node",
41831ae08745Sheppo 		    VD_ID_PROP);
41841ae08745Sheppo 		return;
41851ae08745Sheppo 	}
41861ae08745Sheppo 	PR0("Removing vdisk ID %lu", id);
41871ae08745Sheppo 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0)
41881ae08745Sheppo 		PRN("No vdisk entry found for vdisk ID %lu", id);
41891ae08745Sheppo }
41901ae08745Sheppo 
41911ae08745Sheppo static void
41921ae08745Sheppo vds_change_vd(vds_t *vds, md_t *prev_md, mde_cookie_t prev_vd_node,
41931ae08745Sheppo     md_t *curr_md, mde_cookie_t curr_vd_node)
41941ae08745Sheppo {
41951ae08745Sheppo 	char		*curr_dev, *prev_dev;
41961ae08745Sheppo 	uint64_t	curr_id = 0, curr_ldc_id = 0;
41971ae08745Sheppo 	uint64_t	prev_id = 0, prev_ldc_id = 0;
41981ae08745Sheppo 	size_t		len;
41991ae08745Sheppo 
42001ae08745Sheppo 
42011ae08745Sheppo 	/* Validate that vdisk ID has not changed */
42021ae08745Sheppo 	if (md_get_prop_val(prev_md, prev_vd_node, VD_ID_PROP, &prev_id) != 0) {
42031ae08745Sheppo 		PRN("Error getting previous vdisk \"%s\" property",
42041ae08745Sheppo 		    VD_ID_PROP);
42051ae08745Sheppo 		return;
42061ae08745Sheppo 	}
42071ae08745Sheppo 	if (md_get_prop_val(curr_md, curr_vd_node, VD_ID_PROP, &curr_id) != 0) {
42081ae08745Sheppo 		PRN("Error getting current vdisk \"%s\" property", VD_ID_PROP);
42091ae08745Sheppo 		return;
42101ae08745Sheppo 	}
42111ae08745Sheppo 	if (curr_id != prev_id) {
42121ae08745Sheppo 		PRN("Not changing vdisk:  ID changed from %lu to %lu",
42131ae08745Sheppo 		    prev_id, curr_id);
42141ae08745Sheppo 		return;
42151ae08745Sheppo 	}
42161ae08745Sheppo 
42171ae08745Sheppo 	/* Validate that LDC ID has not changed */
42181ae08745Sheppo 	if (vds_get_ldc_id(prev_md, prev_vd_node, &prev_ldc_id) != 0) {
42191ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", prev_id);
42201ae08745Sheppo 		return;
42211ae08745Sheppo 	}
42221ae08745Sheppo 
42231ae08745Sheppo 	if (vds_get_ldc_id(curr_md, curr_vd_node, &curr_ldc_id) != 0) {
42241ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", curr_id);
42251ae08745Sheppo 		return;
42261ae08745Sheppo 	}
42271ae08745Sheppo 	if (curr_ldc_id != prev_ldc_id) {
42280a55fbb7Slm66018 		_NOTE(NOTREACHED);	/* lint is confused */
42291ae08745Sheppo 		PRN("Not changing vdisk:  "
42301ae08745Sheppo 		    "LDC ID changed from %lu to %lu", prev_ldc_id, curr_ldc_id);
42311ae08745Sheppo 		return;
42321ae08745Sheppo 	}
42331ae08745Sheppo 
42341ae08745Sheppo 	/* Determine whether device path has changed */
42351ae08745Sheppo 	if (md_get_prop_str(prev_md, prev_vd_node, VD_BLOCK_DEVICE_PROP,
42361ae08745Sheppo 	    &prev_dev) != 0) {
42371ae08745Sheppo 		PRN("Error getting previous vdisk \"%s\"",
42381ae08745Sheppo 		    VD_BLOCK_DEVICE_PROP);
42391ae08745Sheppo 		return;
42401ae08745Sheppo 	}
42411ae08745Sheppo 	if (md_get_prop_str(curr_md, curr_vd_node, VD_BLOCK_DEVICE_PROP,
42421ae08745Sheppo 	    &curr_dev) != 0) {
42431ae08745Sheppo 		PRN("Error getting current vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
42441ae08745Sheppo 		return;
42451ae08745Sheppo 	}
42461ae08745Sheppo 	if (((len = strlen(curr_dev)) == strlen(prev_dev)) &&
42471ae08745Sheppo 	    (strncmp(curr_dev, prev_dev, len) == 0))
42481ae08745Sheppo 		return;	/* no relevant (supported) change */
42491ae08745Sheppo 
42501ae08745Sheppo 	PR0("Changing vdisk ID %lu", prev_id);
42513af08d82Slm66018 
42521ae08745Sheppo 	/* Remove old state, which will close vdisk and reset */
42531ae08745Sheppo 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)prev_id) != 0)
42541ae08745Sheppo 		PRN("No entry found for vdisk ID %lu", prev_id);
42553af08d82Slm66018 
42561ae08745Sheppo 	/* Re-initialize vdisk with new state */
42571ae08745Sheppo 	if (vds_init_vd(vds, curr_id, curr_dev, curr_ldc_id) != 0) {
42581ae08745Sheppo 		PRN("Failed to change vdisk ID %lu", curr_id);
42591ae08745Sheppo 		return;
42601ae08745Sheppo 	}
42611ae08745Sheppo }
42621ae08745Sheppo 
42631ae08745Sheppo static int
42641ae08745Sheppo vds_process_md(void *arg, mdeg_result_t *md)
42651ae08745Sheppo {
42661ae08745Sheppo 	int	i;
42671ae08745Sheppo 	vds_t	*vds = arg;
42681ae08745Sheppo 
42691ae08745Sheppo 
42701ae08745Sheppo 	if (md == NULL)
42711ae08745Sheppo 		return (MDEG_FAILURE);
42721ae08745Sheppo 	ASSERT(vds != NULL);
42731ae08745Sheppo 
42741ae08745Sheppo 	for (i = 0; i < md->removed.nelem; i++)
42751ae08745Sheppo 		vds_remove_vd(vds, md->removed.mdp, md->removed.mdep[i]);
42761ae08745Sheppo 	for (i = 0; i < md->match_curr.nelem; i++)
42771ae08745Sheppo 		vds_change_vd(vds, md->match_prev.mdp, md->match_prev.mdep[i],
42781ae08745Sheppo 		    md->match_curr.mdp, md->match_curr.mdep[i]);
42791ae08745Sheppo 	for (i = 0; i < md->added.nelem; i++)
42801ae08745Sheppo 		vds_add_vd(vds, md->added.mdp, md->added.mdep[i]);
42811ae08745Sheppo 
42821ae08745Sheppo 	return (MDEG_SUCCESS);
42831ae08745Sheppo }
42841ae08745Sheppo 
42853c96341aSnarayan 
42861ae08745Sheppo static int
42871ae08745Sheppo vds_do_attach(dev_info_t *dip)
42881ae08745Sheppo {
4289445b4c2eSsb155480 	int			status, sz;
4290445b4c2eSsb155480 	int			cfg_handle;
42911ae08745Sheppo 	minor_t			instance = ddi_get_instance(dip);
42921ae08745Sheppo 	vds_t			*vds;
4293445b4c2eSsb155480 	mdeg_prop_spec_t	*pspecp;
4294445b4c2eSsb155480 	mdeg_node_spec_t	*ispecp;
42951ae08745Sheppo 
42961ae08745Sheppo 	/*
42971ae08745Sheppo 	 * The "cfg-handle" property of a vds node in an MD contains the MD's
42981ae08745Sheppo 	 * notion of "instance", or unique identifier, for that node; OBP
42991ae08745Sheppo 	 * stores the value of the "cfg-handle" MD property as the value of
43001ae08745Sheppo 	 * the "reg" property on the node in the device tree it builds from
43011ae08745Sheppo 	 * the MD and passes to Solaris.  Thus, we look up the devinfo node's
43021ae08745Sheppo 	 * "reg" property value to uniquely identify this device instance when
43031ae08745Sheppo 	 * registering with the MD event-generation framework.  If the "reg"
43041ae08745Sheppo 	 * property cannot be found, the device tree state is presumably so
43051ae08745Sheppo 	 * broken that there is no point in continuing.
43061ae08745Sheppo 	 */
4307445b4c2eSsb155480 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
4308445b4c2eSsb155480 	    VD_REG_PROP)) {
4309445b4c2eSsb155480 		PRN("vds \"%s\" property does not exist", VD_REG_PROP);
43101ae08745Sheppo 		return (DDI_FAILURE);
43111ae08745Sheppo 	}
43121ae08745Sheppo 
43131ae08745Sheppo 	/* Get the MD instance for later MDEG registration */
43141ae08745Sheppo 	cfg_handle = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
4315445b4c2eSsb155480 	    VD_REG_PROP, -1);
43161ae08745Sheppo 
43171ae08745Sheppo 	if (ddi_soft_state_zalloc(vds_state, instance) != DDI_SUCCESS) {
43181ae08745Sheppo 		PRN("Could not allocate state for instance %u", instance);
43191ae08745Sheppo 		return (DDI_FAILURE);
43201ae08745Sheppo 	}
43211ae08745Sheppo 
43221ae08745Sheppo 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
43231ae08745Sheppo 		PRN("Could not get state for instance %u", instance);
43241ae08745Sheppo 		ddi_soft_state_free(vds_state, instance);
43251ae08745Sheppo 		return (DDI_FAILURE);
43261ae08745Sheppo 	}
43271ae08745Sheppo 
43281ae08745Sheppo 	vds->dip	= dip;
43291ae08745Sheppo 	vds->vd_table	= mod_hash_create_ptrhash("vds_vd_table", VDS_NCHAINS,
433087a7269eSachartre 	    vds_destroy_vd, sizeof (void *));
433187a7269eSachartre 
43321ae08745Sheppo 	ASSERT(vds->vd_table != NULL);
43331ae08745Sheppo 
43341ae08745Sheppo 	if ((status = ldi_ident_from_dip(dip, &vds->ldi_ident)) != 0) {
43351ae08745Sheppo 		PRN("ldi_ident_from_dip() returned errno %d", status);
43361ae08745Sheppo 		return (DDI_FAILURE);
43371ae08745Sheppo 	}
43381ae08745Sheppo 	vds->initialized |= VDS_LDI;
43391ae08745Sheppo 
43401ae08745Sheppo 	/* Register for MD updates */
4341445b4c2eSsb155480 	sz = sizeof (vds_prop_template);
4342445b4c2eSsb155480 	pspecp = kmem_alloc(sz, KM_SLEEP);
4343445b4c2eSsb155480 	bcopy(vds_prop_template, pspecp, sz);
4344445b4c2eSsb155480 
4345445b4c2eSsb155480 	VDS_SET_MDEG_PROP_INST(pspecp, cfg_handle);
4346445b4c2eSsb155480 
4347445b4c2eSsb155480 	/* initialize the complete prop spec structure */
4348445b4c2eSsb155480 	ispecp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP);
4349445b4c2eSsb155480 	ispecp->namep = "virtual-device";
4350445b4c2eSsb155480 	ispecp->specp = pspecp;
4351445b4c2eSsb155480 
4352445b4c2eSsb155480 	if (mdeg_register(ispecp, &vd_match, vds_process_md, vds,
43531ae08745Sheppo 	    &vds->mdeg) != MDEG_SUCCESS) {
43541ae08745Sheppo 		PRN("Unable to register for MD updates");
4355445b4c2eSsb155480 		kmem_free(ispecp, sizeof (mdeg_node_spec_t));
4356445b4c2eSsb155480 		kmem_free(pspecp, sz);
43571ae08745Sheppo 		return (DDI_FAILURE);
43581ae08745Sheppo 	}
4359445b4c2eSsb155480 
4360445b4c2eSsb155480 	vds->ispecp = ispecp;
43611ae08745Sheppo 	vds->initialized |= VDS_MDEG;
43621ae08745Sheppo 
43630a55fbb7Slm66018 	/* Prevent auto-detaching so driver is available whenever MD changes */
43640a55fbb7Slm66018 	if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1) !=
43650a55fbb7Slm66018 	    DDI_PROP_SUCCESS) {
43660a55fbb7Slm66018 		PRN("failed to set \"%s\" property for instance %u",
43670a55fbb7Slm66018 		    DDI_NO_AUTODETACH, instance);
43680a55fbb7Slm66018 	}
43690a55fbb7Slm66018 
43701ae08745Sheppo 	ddi_report_dev(dip);
43711ae08745Sheppo 	return (DDI_SUCCESS);
43721ae08745Sheppo }
43731ae08745Sheppo 
43741ae08745Sheppo static int
43751ae08745Sheppo vds_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
43761ae08745Sheppo {
43771ae08745Sheppo 	int	status;
43781ae08745Sheppo 
43791ae08745Sheppo 	switch (cmd) {
43801ae08745Sheppo 	case DDI_ATTACH:
4381d10e4ef2Snarayan 		PR0("Attaching");
43821ae08745Sheppo 		if ((status = vds_do_attach(dip)) != DDI_SUCCESS)
43831ae08745Sheppo 			(void) vds_detach(dip, DDI_DETACH);
43841ae08745Sheppo 		return (status);
43851ae08745Sheppo 	case DDI_RESUME:
4386d10e4ef2Snarayan 		PR0("No action required for DDI_RESUME");
43871ae08745Sheppo 		return (DDI_SUCCESS);
43881ae08745Sheppo 	default:
43891ae08745Sheppo 		return (DDI_FAILURE);
43901ae08745Sheppo 	}
43911ae08745Sheppo }
43921ae08745Sheppo 
43931ae08745Sheppo static struct dev_ops vds_ops = {
43941ae08745Sheppo 	DEVO_REV,	/* devo_rev */
43951ae08745Sheppo 	0,		/* devo_refcnt */
43961ae08745Sheppo 	ddi_no_info,	/* devo_getinfo */
43971ae08745Sheppo 	nulldev,	/* devo_identify */
43981ae08745Sheppo 	nulldev,	/* devo_probe */
43991ae08745Sheppo 	vds_attach,	/* devo_attach */
44001ae08745Sheppo 	vds_detach,	/* devo_detach */
44011ae08745Sheppo 	nodev,		/* devo_reset */
44021ae08745Sheppo 	NULL,		/* devo_cb_ops */
44031ae08745Sheppo 	NULL,		/* devo_bus_ops */
44041ae08745Sheppo 	nulldev		/* devo_power */
44051ae08745Sheppo };
44061ae08745Sheppo 
44071ae08745Sheppo static struct modldrv modldrv = {
44081ae08745Sheppo 	&mod_driverops,
4409*205eeb1aSlm66018 	"virtual disk server",
44101ae08745Sheppo 	&vds_ops,
44111ae08745Sheppo };
44121ae08745Sheppo 
44131ae08745Sheppo static struct modlinkage modlinkage = {
44141ae08745Sheppo 	MODREV_1,
44151ae08745Sheppo 	&modldrv,
44161ae08745Sheppo 	NULL
44171ae08745Sheppo };
44181ae08745Sheppo 
44191ae08745Sheppo 
44201ae08745Sheppo int
44211ae08745Sheppo _init(void)
44221ae08745Sheppo {
44231ae08745Sheppo 	int		i, status;
44241ae08745Sheppo 
4425d10e4ef2Snarayan 
44261ae08745Sheppo 	if ((status = ddi_soft_state_init(&vds_state, sizeof (vds_t), 1)) != 0)
44271ae08745Sheppo 		return (status);
44281ae08745Sheppo 	if ((status = mod_install(&modlinkage)) != 0) {
44291ae08745Sheppo 		ddi_soft_state_fini(&vds_state);
44301ae08745Sheppo 		return (status);
44311ae08745Sheppo 	}
44321ae08745Sheppo 
44331ae08745Sheppo 	/* Fill in the bit-mask of server-supported operations */
44341ae08745Sheppo 	for (i = 0; i < vds_noperations; i++)
44351ae08745Sheppo 		vds_operations |= 1 << (vds_operation[i].operation - 1);
44361ae08745Sheppo 
44371ae08745Sheppo 	return (0);
44381ae08745Sheppo }
44391ae08745Sheppo 
44401ae08745Sheppo int
44411ae08745Sheppo _info(struct modinfo *modinfop)
44421ae08745Sheppo {
44431ae08745Sheppo 	return (mod_info(&modlinkage, modinfop));
44441ae08745Sheppo }
44451ae08745Sheppo 
44461ae08745Sheppo int
44471ae08745Sheppo _fini(void)
44481ae08745Sheppo {
44491ae08745Sheppo 	int	status;
44501ae08745Sheppo 
4451d10e4ef2Snarayan 
44521ae08745Sheppo 	if ((status = mod_remove(&modlinkage)) != 0)
44531ae08745Sheppo 		return (status);
44541ae08745Sheppo 	ddi_soft_state_fini(&vds_state);
44551ae08745Sheppo 	return (0);
44561ae08745Sheppo }
4457