xref: /titanic_53/usr/src/uts/sun4v/io/vds.c (revision 78fcd0a1c10bb5a069d27365ac663157b8bac58b)
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>
44205eeb1aSlm66018 #include <sys/sdt.h>
451ae08745Sheppo #include <sys/sunddi.h>
461ae08745Sheppo #include <sys/sunldi.h>
471ae08745Sheppo #include <sys/sysmacros.h>
481ae08745Sheppo #include <sys/vio_common.h>
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 */
300205eeb1aSlm66018 	int			status;		/* status of processing task */
301205eeb1aSlm66018 	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);
354205eeb1aSlm66018 	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);
401*78fcd0a1Sachartre static ushort_t vd_lbl2cksum(struct dk_label *label);
402*78fcd0a1Sachartre static int vd_file_validate_geometry(vd_t *vd);
403690555a1Sachartre /*
404690555a1Sachartre  * Function:
405690555a1Sachartre  *	vd_file_rw
406690555a1Sachartre  *
407690555a1Sachartre  * Description:
408690555a1Sachartre  * 	Read or write to a disk on file.
409690555a1Sachartre  *
410690555a1Sachartre  * Parameters:
411690555a1Sachartre  *	vd		- disk on which the operation is performed.
412690555a1Sachartre  *	slice		- slice on which the operation is performed,
41387a7269eSachartre  *			  VD_SLICE_NONE indicates that the operation
41487a7269eSachartre  *			  is done using an absolute disk offset.
415690555a1Sachartre  *	operation	- operation to execute: read (VD_OP_BREAD) or
416690555a1Sachartre  *			  write (VD_OP_BWRITE).
417690555a1Sachartre  *	data		- buffer where data are read to or written from.
418690555a1Sachartre  *	blk		- starting block for the operation.
419690555a1Sachartre  *	len		- number of bytes to read or write.
420690555a1Sachartre  *
421690555a1Sachartre  * Return Code:
422690555a1Sachartre  *	n >= 0		- success, n indicates the number of bytes read
423690555a1Sachartre  *			  or written.
424690555a1Sachartre  *	-1		- error.
425690555a1Sachartre  */
426690555a1Sachartre static ssize_t
427690555a1Sachartre vd_file_rw(vd_t *vd, int slice, int operation, caddr_t data, size_t blk,
428690555a1Sachartre     size_t len)
429690555a1Sachartre {
430690555a1Sachartre 	caddr_t	maddr;
431690555a1Sachartre 	size_t offset, maxlen, moffset, mlen, n;
432690555a1Sachartre 	uint_t smflags;
433690555a1Sachartre 	enum seg_rw srw;
434690555a1Sachartre 
435690555a1Sachartre 	ASSERT(vd->file);
436690555a1Sachartre 	ASSERT(len > 0);
437690555a1Sachartre 
43887a7269eSachartre 	if (slice == VD_SLICE_NONE) {
439690555a1Sachartre 		/* raw disk access */
440690555a1Sachartre 		offset = blk * DEV_BSIZE;
441690555a1Sachartre 	} else {
442690555a1Sachartre 		ASSERT(slice >= 0 && slice < V_NUMPAR);
443*78fcd0a1Sachartre 
444*78fcd0a1Sachartre 		if (vd->vdisk_label == VD_DISK_LABEL_UNK &&
445*78fcd0a1Sachartre 		    vd_file_validate_geometry(vd) != 0) {
446*78fcd0a1Sachartre 			PR0("Unknown disk label, can't do I/O from slice %d",
447*78fcd0a1Sachartre 			    slice);
448*78fcd0a1Sachartre 			return (-1);
449*78fcd0a1Sachartre 		}
450*78fcd0a1Sachartre 
451690555a1Sachartre 		if (blk >= vd->vtoc.v_part[slice].p_size) {
452690555a1Sachartre 			/* address past the end of the slice */
453690555a1Sachartre 			PR0("req_addr (0x%lx) > psize (0x%lx)",
454690555a1Sachartre 			    blk, vd->vtoc.v_part[slice].p_size);
455690555a1Sachartre 			return (0);
456690555a1Sachartre 		}
457690555a1Sachartre 
458690555a1Sachartre 		offset = (vd->vtoc.v_part[slice].p_start + blk) * DEV_BSIZE;
459690555a1Sachartre 
460690555a1Sachartre 		/*
461690555a1Sachartre 		 * If the requested size is greater than the size
462690555a1Sachartre 		 * of the partition, truncate the read/write.
463690555a1Sachartre 		 */
464690555a1Sachartre 		maxlen = (vd->vtoc.v_part[slice].p_size - blk) * DEV_BSIZE;
465690555a1Sachartre 
466690555a1Sachartre 		if (len > maxlen) {
467690555a1Sachartre 			PR0("I/O size truncated to %lu bytes from %lu bytes",
468690555a1Sachartre 			    maxlen, len);
469690555a1Sachartre 			len = maxlen;
470690555a1Sachartre 		}
471690555a1Sachartre 	}
472690555a1Sachartre 
473690555a1Sachartre 	/*
474690555a1Sachartre 	 * We have to ensure that we are reading/writing into the mmap
475690555a1Sachartre 	 * range. If we have a partial disk image (e.g. an image of
476690555a1Sachartre 	 * s0 instead s2) the system can try to access slices that
477690555a1Sachartre 	 * are not included into the disk image.
478690555a1Sachartre 	 */
479690555a1Sachartre 	if ((offset + len) >= vd->file_size) {
480690555a1Sachartre 		PR0("offset + nbytes (0x%lx + 0x%lx) >= "
481690555a1Sachartre 		    "file_size (0x%lx)", offset, len, vd->file_size);
482690555a1Sachartre 		return (-1);
483690555a1Sachartre 	}
484690555a1Sachartre 
485690555a1Sachartre 	srw = (operation == VD_OP_BREAD)? S_READ : S_WRITE;
486eba0cb4eSachartre 	smflags = (operation == VD_OP_BREAD)? 0 :
487eba0cb4eSachartre 	    (SM_WRITE | vd_file_write_flags);
488690555a1Sachartre 	n = len;
489690555a1Sachartre 
490690555a1Sachartre 	do {
491690555a1Sachartre 		/*
492690555a1Sachartre 		 * segmap_getmapflt() returns a MAXBSIZE chunk which is
493690555a1Sachartre 		 * MAXBSIZE aligned.
494690555a1Sachartre 		 */
495690555a1Sachartre 		moffset = offset & MAXBOFFSET;
496690555a1Sachartre 		mlen = MIN(MAXBSIZE - moffset, n);
497690555a1Sachartre 		maddr = segmap_getmapflt(segkmap, vd->file_vnode, offset,
498690555a1Sachartre 		    mlen, 1, srw);
499690555a1Sachartre 		/*
500690555a1Sachartre 		 * Fault in the pages so we can check for error and ensure
501690555a1Sachartre 		 * that we can safely used the mapped address.
502690555a1Sachartre 		 */
503690555a1Sachartre 		if (segmap_fault(kas.a_hat, segkmap, maddr, mlen,
504690555a1Sachartre 		    F_SOFTLOCK, srw) != 0) {
505690555a1Sachartre 			(void) segmap_release(segkmap, maddr, 0);
506690555a1Sachartre 			return (-1);
507690555a1Sachartre 		}
508690555a1Sachartre 
509690555a1Sachartre 		if (operation == VD_OP_BREAD)
510690555a1Sachartre 			bcopy(maddr + moffset, data, mlen);
511690555a1Sachartre 		else
512690555a1Sachartre 			bcopy(data, maddr + moffset, mlen);
513690555a1Sachartre 
514690555a1Sachartre 		if (segmap_fault(kas.a_hat, segkmap, maddr, mlen,
515690555a1Sachartre 		    F_SOFTUNLOCK, srw) != 0) {
516690555a1Sachartre 			(void) segmap_release(segkmap, maddr, 0);
517690555a1Sachartre 			return (-1);
518690555a1Sachartre 		}
519690555a1Sachartre 		if (segmap_release(segkmap, maddr, smflags) != 0)
520690555a1Sachartre 			return (-1);
521690555a1Sachartre 		n -= mlen;
522690555a1Sachartre 		offset += mlen;
523690555a1Sachartre 		data += mlen;
524690555a1Sachartre 
525690555a1Sachartre 	} while (n > 0);
526690555a1Sachartre 
527690555a1Sachartre 	return (len);
528690555a1Sachartre }
529690555a1Sachartre 
53087a7269eSachartre /*
53187a7269eSachartre  * Function:
532*78fcd0a1Sachartre  *	vd_file_build_default_label
533*78fcd0a1Sachartre  *
534*78fcd0a1Sachartre  * Description:
535*78fcd0a1Sachartre  *	Return a default label for the given disk. This is used when the disk
536*78fcd0a1Sachartre  *	does not have a valid VTOC so that the user can get a valid default
537*78fcd0a1Sachartre  *	configuration. The default label have all slices size set to 0 (except
538*78fcd0a1Sachartre  *	slice 2 which is the entire disk) to force the user to write a valid
539*78fcd0a1Sachartre  *	label onto the disk image.
540*78fcd0a1Sachartre  *
541*78fcd0a1Sachartre  * Parameters:
542*78fcd0a1Sachartre  *	vd		- disk on which the operation is performed.
543*78fcd0a1Sachartre  *	label		- the returned default label.
544*78fcd0a1Sachartre  *
545*78fcd0a1Sachartre  * Return Code:
546*78fcd0a1Sachartre  *	none.
547*78fcd0a1Sachartre  */
548*78fcd0a1Sachartre static void
549*78fcd0a1Sachartre vd_file_build_default_label(vd_t *vd, struct dk_label *label)
550*78fcd0a1Sachartre {
551*78fcd0a1Sachartre 	size_t size;
552*78fcd0a1Sachartre 	char prefix;
553*78fcd0a1Sachartre 
554*78fcd0a1Sachartre 	ASSERT(vd->file);
555*78fcd0a1Sachartre 
556*78fcd0a1Sachartre 	/*
557*78fcd0a1Sachartre 	 * We must have a resonable number of cylinders and sectors so
558*78fcd0a1Sachartre 	 * that newfs can run using default values.
559*78fcd0a1Sachartre 	 *
560*78fcd0a1Sachartre 	 * if (disk_size < 2MB)
561*78fcd0a1Sachartre 	 * 	phys_cylinders = disk_size / 100K
562*78fcd0a1Sachartre 	 * else
563*78fcd0a1Sachartre 	 * 	phys_cylinders = disk_size / 300K
564*78fcd0a1Sachartre 	 *
565*78fcd0a1Sachartre 	 * phys_cylinders = (phys_cylinders == 0) ? 1 : phys_cylinders
566*78fcd0a1Sachartre 	 * alt_cylinders = (phys_cylinders > 2) ? 2 : 0;
567*78fcd0a1Sachartre 	 * data_cylinders = phys_cylinders - alt_cylinders
568*78fcd0a1Sachartre 	 *
569*78fcd0a1Sachartre 	 * sectors = disk_size / (phys_cylinders * blk_size)
570*78fcd0a1Sachartre 	 *
571*78fcd0a1Sachartre 	 * The file size test is an attempt to not have too few cylinders
572*78fcd0a1Sachartre 	 * for a small file, or so many on a big file that you waste space
573*78fcd0a1Sachartre 	 * for backup superblocks or cylinder group structures.
574*78fcd0a1Sachartre 	 */
575*78fcd0a1Sachartre 	if (vd->file_size < (2 * 1024 * 1024))
576*78fcd0a1Sachartre 		label->dkl_pcyl = vd->file_size / (100 * 1024);
577*78fcd0a1Sachartre 	else
578*78fcd0a1Sachartre 		label->dkl_pcyl = vd->file_size / (300 * 1024);
579*78fcd0a1Sachartre 
580*78fcd0a1Sachartre 	if (label->dkl_pcyl == 0)
581*78fcd0a1Sachartre 		label->dkl_pcyl = 1;
582*78fcd0a1Sachartre 
583*78fcd0a1Sachartre 	if (label->dkl_pcyl > 2)
584*78fcd0a1Sachartre 		label->dkl_acyl = 2;
585*78fcd0a1Sachartre 	else
586*78fcd0a1Sachartre 		label->dkl_acyl = 0;
587*78fcd0a1Sachartre 
588*78fcd0a1Sachartre 	label->dkl_nsect = vd->file_size /
589*78fcd0a1Sachartre 	    (DEV_BSIZE * label->dkl_pcyl);
590*78fcd0a1Sachartre 	label->dkl_ncyl = label->dkl_pcyl - label->dkl_acyl;
591*78fcd0a1Sachartre 	label->dkl_nhead = 1;
592*78fcd0a1Sachartre 	label->dkl_write_reinstruct = 0;
593*78fcd0a1Sachartre 	label->dkl_read_reinstruct = 0;
594*78fcd0a1Sachartre 	label->dkl_rpm = 7200;
595*78fcd0a1Sachartre 	label->dkl_apc = 0;
596*78fcd0a1Sachartre 	label->dkl_intrlv = 0;
597*78fcd0a1Sachartre 
598*78fcd0a1Sachartre 	PR0("requested disk size: %ld bytes\n", vd->file_size);
599*78fcd0a1Sachartre 	PR0("setup: ncyl=%d nhead=%d nsec=%d\n", label->dkl_pcyl,
600*78fcd0a1Sachartre 	    label->dkl_nhead, label->dkl_nsect);
601*78fcd0a1Sachartre 	PR0("provided disk size: %ld bytes\n", (uint64_t)
602*78fcd0a1Sachartre 	    (label->dkl_pcyl * label->dkl_nhead *
603*78fcd0a1Sachartre 	    label->dkl_nsect * DEV_BSIZE));
604*78fcd0a1Sachartre 
605*78fcd0a1Sachartre 	if (vd->file_size < (1ULL << 20)) {
606*78fcd0a1Sachartre 		size = vd->file_size >> 10;
607*78fcd0a1Sachartre 		prefix = 'K'; /* Kilobyte */
608*78fcd0a1Sachartre 	} else if (vd->file_size < (1ULL << 30)) {
609*78fcd0a1Sachartre 		size = vd->file_size >> 20;
610*78fcd0a1Sachartre 		prefix = 'M'; /* Megabyte */
611*78fcd0a1Sachartre 	} else if (vd->file_size < (1ULL << 40)) {
612*78fcd0a1Sachartre 		size = vd->file_size >> 30;
613*78fcd0a1Sachartre 		prefix = 'G'; /* Gigabyte */
614*78fcd0a1Sachartre 	} else {
615*78fcd0a1Sachartre 		size = vd->file_size >> 40;
616*78fcd0a1Sachartre 		prefix = 'T'; /* Terabyte */
617*78fcd0a1Sachartre 	}
618*78fcd0a1Sachartre 
619*78fcd0a1Sachartre 	/*
620*78fcd0a1Sachartre 	 * We must have a correct label name otherwise format(1m) will
621*78fcd0a1Sachartre 	 * not recognized the disk as labeled.
622*78fcd0a1Sachartre 	 */
623*78fcd0a1Sachartre 	(void) snprintf(label->dkl_asciilabel, LEN_DKL_ASCII,
624*78fcd0a1Sachartre 	    "SUN-DiskImage-%ld%cB cyl %d alt %d hd %d sec %d",
625*78fcd0a1Sachartre 	    size, prefix,
626*78fcd0a1Sachartre 	    label->dkl_ncyl, label->dkl_acyl, label->dkl_nhead,
627*78fcd0a1Sachartre 	    label->dkl_nsect);
628*78fcd0a1Sachartre 
629*78fcd0a1Sachartre 	/* default VTOC */
630*78fcd0a1Sachartre 	label->dkl_vtoc.v_version = V_VERSION;
631*78fcd0a1Sachartre 	label->dkl_vtoc.v_nparts = V_NUMPAR;
632*78fcd0a1Sachartre 	label->dkl_vtoc.v_sanity = VTOC_SANE;
633*78fcd0a1Sachartre 	label->dkl_vtoc.v_part[2].p_tag = V_BACKUP;
634*78fcd0a1Sachartre 	label->dkl_map[2].dkl_cylno = 0;
635*78fcd0a1Sachartre 	label->dkl_map[2].dkl_nblk = label->dkl_ncyl *
636*78fcd0a1Sachartre 	    label->dkl_nhead * label->dkl_nsect;
637*78fcd0a1Sachartre 	label->dkl_cksum = vd_lbl2cksum(label);
638*78fcd0a1Sachartre }
639*78fcd0a1Sachartre 
640*78fcd0a1Sachartre /*
641*78fcd0a1Sachartre  * Function:
64287a7269eSachartre  *	vd_file_set_vtoc
64387a7269eSachartre  *
64487a7269eSachartre  * Description:
64587a7269eSachartre  *	Set the vtoc of a disk image by writing the label and backup
64687a7269eSachartre  *	labels into the disk image backend.
64787a7269eSachartre  *
64887a7269eSachartre  * Parameters:
64987a7269eSachartre  *	vd		- disk on which the operation is performed.
65087a7269eSachartre  *	label		- the data to be written.
65187a7269eSachartre  *
65287a7269eSachartre  * Return Code:
65387a7269eSachartre  *	0		- success.
65487a7269eSachartre  *	n > 0		- error, n indicates the errno code.
65587a7269eSachartre  */
65687a7269eSachartre static int
65787a7269eSachartre vd_file_set_vtoc(vd_t *vd, struct dk_label *label)
65887a7269eSachartre {
65987a7269eSachartre 	int blk, sec, cyl, head, cnt;
66087a7269eSachartre 
66187a7269eSachartre 	ASSERT(vd->file);
66287a7269eSachartre 
66387a7269eSachartre 	if (VD_FILE_LABEL_WRITE(vd, label) < 0) {
66487a7269eSachartre 		PR0("fail to write disk label");
66587a7269eSachartre 		return (EIO);
66687a7269eSachartre 	}
66787a7269eSachartre 
66887a7269eSachartre 	/*
66987a7269eSachartre 	 * Backup labels are on the last alternate cylinder's
67087a7269eSachartre 	 * first five odd sectors.
67187a7269eSachartre 	 */
67287a7269eSachartre 	if (label->dkl_acyl == 0) {
67387a7269eSachartre 		PR0("no alternate cylinder, can not store backup labels");
67487a7269eSachartre 		return (0);
67587a7269eSachartre 	}
67687a7269eSachartre 
67787a7269eSachartre 	cyl = label->dkl_ncyl  + label->dkl_acyl - 1;
67887a7269eSachartre 	head = label->dkl_nhead - 1;
67987a7269eSachartre 
68087a7269eSachartre 	blk = (cyl * ((label->dkl_nhead * label->dkl_nsect) - label->dkl_apc)) +
68187a7269eSachartre 	    (head * label->dkl_nsect);
68287a7269eSachartre 
68387a7269eSachartre 	/*
68487a7269eSachartre 	 * Write the backup labels. Make sure we don't try to write past
68587a7269eSachartre 	 * the last cylinder.
68687a7269eSachartre 	 */
68787a7269eSachartre 	sec = 1;
68887a7269eSachartre 
68987a7269eSachartre 	for (cnt = 0; cnt < VD_FILE_NUM_BACKUP; cnt++) {
69087a7269eSachartre 
69187a7269eSachartre 		if (sec >= label->dkl_nsect) {
69287a7269eSachartre 			PR0("not enough sector to store all backup labels");
69387a7269eSachartre 			return (0);
69487a7269eSachartre 		}
69587a7269eSachartre 
69687a7269eSachartre 		if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)label,
69787a7269eSachartre 		    blk + sec, sizeof (struct dk_label)) < 0) {
69887a7269eSachartre 			PR0("error writing backup label at block %d\n",
69987a7269eSachartre 			    blk + sec);
70087a7269eSachartre 			return (EIO);
70187a7269eSachartre 		}
70287a7269eSachartre 
70387a7269eSachartre 		PR1("wrote backup label at block %d\n", blk + sec);
70487a7269eSachartre 
70587a7269eSachartre 		sec += 2;
70687a7269eSachartre 	}
70787a7269eSachartre 
70887a7269eSachartre 	return (0);
70987a7269eSachartre }
71087a7269eSachartre 
71187a7269eSachartre /*
71287a7269eSachartre  * Function:
71387a7269eSachartre  *	vd_file_get_devid_block
71487a7269eSachartre  *
71587a7269eSachartre  * Description:
71687a7269eSachartre  *	Return the block number where the device id is stored.
71787a7269eSachartre  *
71887a7269eSachartre  * Parameters:
71987a7269eSachartre  *	vd		- disk on which the operation is performed.
72087a7269eSachartre  *	blkp		- pointer to the block number
72187a7269eSachartre  *
72287a7269eSachartre  * Return Code:
72387a7269eSachartre  *	0		- success
72487a7269eSachartre  *	ENOSPC		- disk has no space to store a device id
72587a7269eSachartre  */
72687a7269eSachartre static int
72787a7269eSachartre vd_file_get_devid_block(vd_t *vd, size_t *blkp)
72887a7269eSachartre {
72987a7269eSachartre 	diskaddr_t spc, head, cyl;
73087a7269eSachartre 
73187a7269eSachartre 	ASSERT(vd->file);
73287a7269eSachartre 	ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
73387a7269eSachartre 
73487a7269eSachartre 	/* this geometry doesn't allow us to have a devid */
73587a7269eSachartre 	if (vd->dk_geom.dkg_acyl < 2) {
73687a7269eSachartre 		PR0("not enough alternate cylinder available for devid "
73787a7269eSachartre 		    "(acyl=%u)", vd->dk_geom.dkg_acyl);
73887a7269eSachartre 		return (ENOSPC);
73987a7269eSachartre 	}
74087a7269eSachartre 
74187a7269eSachartre 	/* the devid is in on the track next to the last cylinder */
74287a7269eSachartre 	cyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl - 2;
74387a7269eSachartre 	spc = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
74487a7269eSachartre 	head = vd->dk_geom.dkg_nhead - 1;
74587a7269eSachartre 
74687a7269eSachartre 	*blkp = (cyl * (spc - vd->dk_geom.dkg_apc)) +
74787a7269eSachartre 	    (head * vd->dk_geom.dkg_nsect) + 1;
74887a7269eSachartre 
74987a7269eSachartre 	return (0);
75087a7269eSachartre }
75187a7269eSachartre 
75287a7269eSachartre /*
75387a7269eSachartre  * Return the checksum of a disk block containing an on-disk devid.
75487a7269eSachartre  */
75587a7269eSachartre static uint_t
75687a7269eSachartre vd_dkdevid2cksum(struct dk_devid *dkdevid)
75787a7269eSachartre {
75887a7269eSachartre 	uint_t chksum, *ip;
75987a7269eSachartre 	int i;
76087a7269eSachartre 
76187a7269eSachartre 	chksum = 0;
76287a7269eSachartre 	ip = (uint_t *)dkdevid;
76387a7269eSachartre 	for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int)); i++)
76487a7269eSachartre 		chksum ^= ip[i];
76587a7269eSachartre 
76687a7269eSachartre 	return (chksum);
76787a7269eSachartre }
76887a7269eSachartre 
76987a7269eSachartre /*
77087a7269eSachartre  * Function:
77187a7269eSachartre  *	vd_file_read_devid
77287a7269eSachartre  *
77387a7269eSachartre  * Description:
77487a7269eSachartre  *	Read the device id stored on a disk image.
77587a7269eSachartre  *
77687a7269eSachartre  * Parameters:
77787a7269eSachartre  *	vd		- disk on which the operation is performed.
77887a7269eSachartre  *	devid		- the return address of the device ID.
77987a7269eSachartre  *
78087a7269eSachartre  * Return Code:
78187a7269eSachartre  *	0		- success
78287a7269eSachartre  *	EIO		- I/O error while trying to access the disk image
78387a7269eSachartre  *	EINVAL		- no valid device id was found
78487a7269eSachartre  *	ENOSPC		- disk has no space to store a device id
78587a7269eSachartre  */
78687a7269eSachartre static int
78787a7269eSachartre vd_file_read_devid(vd_t *vd, ddi_devid_t *devid)
78887a7269eSachartre {
78987a7269eSachartre 	struct dk_devid *dkdevid;
79087a7269eSachartre 	size_t blk;
79187a7269eSachartre 	uint_t chksum;
79287a7269eSachartre 	int status, sz;
79387a7269eSachartre 
79487a7269eSachartre 	if ((status = vd_file_get_devid_block(vd, &blk)) != 0)
79587a7269eSachartre 		return (status);
79687a7269eSachartre 
79787a7269eSachartre 	dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP);
79887a7269eSachartre 
79987a7269eSachartre 	/* get the devid */
80087a7269eSachartre 	if ((vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)dkdevid, blk,
80187a7269eSachartre 	    DEV_BSIZE)) < 0) {
80287a7269eSachartre 		PR0("error reading devid block at %lu", blk);
80387a7269eSachartre 		status = EIO;
80487a7269eSachartre 		goto done;
80587a7269eSachartre 	}
80687a7269eSachartre 
80787a7269eSachartre 	/* validate the revision */
80887a7269eSachartre 	if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
80987a7269eSachartre 	    (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
81087a7269eSachartre 		PR0("invalid devid found at block %lu (bad revision)", blk);
81187a7269eSachartre 		status = EINVAL;
81287a7269eSachartre 		goto done;
81387a7269eSachartre 	}
81487a7269eSachartre 
81587a7269eSachartre 	/* compute checksum */
81687a7269eSachartre 	chksum = vd_dkdevid2cksum(dkdevid);
81787a7269eSachartre 
81887a7269eSachartre 	/* compare the checksums */
81987a7269eSachartre 	if (DKD_GETCHKSUM(dkdevid) != chksum) {
82087a7269eSachartre 		PR0("invalid devid found at block %lu (bad checksum)", blk);
82187a7269eSachartre 		status = EINVAL;
82287a7269eSachartre 		goto done;
82387a7269eSachartre 	}
82487a7269eSachartre 
82587a7269eSachartre 	/* validate the device id */
82687a7269eSachartre 	if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
82787a7269eSachartre 		PR0("invalid devid found at block %lu", blk);
82887a7269eSachartre 		status = EINVAL;
82987a7269eSachartre 		goto done;
83087a7269eSachartre 	}
83187a7269eSachartre 
83287a7269eSachartre 	PR1("devid read at block %lu", blk);
83387a7269eSachartre 
83487a7269eSachartre 	sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
83587a7269eSachartre 	*devid = kmem_alloc(sz, KM_SLEEP);
83687a7269eSachartre 	bcopy(&dkdevid->dkd_devid, *devid, sz);
83787a7269eSachartre 
83887a7269eSachartre done:
83987a7269eSachartre 	kmem_free(dkdevid, DEV_BSIZE);
84087a7269eSachartre 	return (status);
84187a7269eSachartre 
84287a7269eSachartre }
84387a7269eSachartre 
84487a7269eSachartre /*
84587a7269eSachartre  * Function:
84687a7269eSachartre  *	vd_file_write_devid
84787a7269eSachartre  *
84887a7269eSachartre  * Description:
84987a7269eSachartre  *	Write a device id into disk image.
85087a7269eSachartre  *
85187a7269eSachartre  * Parameters:
85287a7269eSachartre  *	vd		- disk on which the operation is performed.
85387a7269eSachartre  *	devid		- the device ID to store.
85487a7269eSachartre  *
85587a7269eSachartre  * Return Code:
85687a7269eSachartre  *	0		- success
85787a7269eSachartre  *	EIO		- I/O error while trying to access the disk image
85887a7269eSachartre  *	ENOSPC		- disk has no space to store a device id
85987a7269eSachartre  */
86087a7269eSachartre static int
86187a7269eSachartre vd_file_write_devid(vd_t *vd, ddi_devid_t devid)
86287a7269eSachartre {
86387a7269eSachartre 	struct dk_devid *dkdevid;
86487a7269eSachartre 	uint_t chksum;
86587a7269eSachartre 	size_t blk;
86687a7269eSachartre 	int status;
86787a7269eSachartre 
86887a7269eSachartre 	if ((status = vd_file_get_devid_block(vd, &blk)) != 0)
86987a7269eSachartre 		return (status);
87087a7269eSachartre 
87187a7269eSachartre 	dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP);
87287a7269eSachartre 
87387a7269eSachartre 	/* set revision */
87487a7269eSachartre 	dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
87587a7269eSachartre 	dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
87687a7269eSachartre 
87787a7269eSachartre 	/* copy devid */
87887a7269eSachartre 	bcopy(devid, &dkdevid->dkd_devid, ddi_devid_sizeof(devid));
87987a7269eSachartre 
88087a7269eSachartre 	/* compute checksum */
88187a7269eSachartre 	chksum = vd_dkdevid2cksum(dkdevid);
88287a7269eSachartre 
88387a7269eSachartre 	/* set checksum */
88487a7269eSachartre 	DKD_FORMCHKSUM(chksum, dkdevid);
88587a7269eSachartre 
88687a7269eSachartre 	/* store the devid */
88787a7269eSachartre 	if ((status = vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE,
88887a7269eSachartre 	    (caddr_t)dkdevid, blk, DEV_BSIZE)) < 0) {
88987a7269eSachartre 		PR0("Error writing devid block at %lu", blk);
89087a7269eSachartre 		status = EIO;
89187a7269eSachartre 	} else {
89287a7269eSachartre 		PR1("devid written at block %lu", blk);
89387a7269eSachartre 		status = 0;
89487a7269eSachartre 	}
89587a7269eSachartre 
89687a7269eSachartre 	kmem_free(dkdevid, DEV_BSIZE);
89787a7269eSachartre 	return (status);
89887a7269eSachartre }
89987a7269eSachartre 
90087a7269eSachartre /*
90187a7269eSachartre  * Function:
90287a7269eSachartre  *	vd_scsi_rdwr
90387a7269eSachartre  *
90487a7269eSachartre  * Description:
90587a7269eSachartre  * 	Read or write to a SCSI disk using an absolute disk offset.
90687a7269eSachartre  *
90787a7269eSachartre  * Parameters:
90887a7269eSachartre  *	vd		- disk on which the operation is performed.
90987a7269eSachartre  *	operation	- operation to execute: read (VD_OP_BREAD) or
91087a7269eSachartre  *			  write (VD_OP_BWRITE).
91187a7269eSachartre  *	data		- buffer where data are read to or written from.
91287a7269eSachartre  *	blk		- starting block for the operation.
91387a7269eSachartre  *	len		- number of bytes to read or write.
91487a7269eSachartre  *
91587a7269eSachartre  * Return Code:
91687a7269eSachartre  *	0		- success
91787a7269eSachartre  *	n != 0		- error.
91887a7269eSachartre  */
91987a7269eSachartre static int
92087a7269eSachartre vd_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t blk, size_t len)
92187a7269eSachartre {
92287a7269eSachartre 	struct uscsi_cmd ucmd;
92387a7269eSachartre 	union scsi_cdb cdb;
92487a7269eSachartre 	int nsectors, nblk;
92587a7269eSachartre 	int max_sectors;
92687a7269eSachartre 	int status, rval;
92787a7269eSachartre 
92887a7269eSachartre 	ASSERT(!vd->file);
92987a7269eSachartre 
93087a7269eSachartre 	max_sectors = vd->max_xfer_sz;
93187a7269eSachartre 	nblk = (len / DEV_BSIZE);
93287a7269eSachartre 
93387a7269eSachartre 	if (len % DEV_BSIZE != 0)
93487a7269eSachartre 		return (EINVAL);
93587a7269eSachartre 
93687a7269eSachartre 	/*
93787a7269eSachartre 	 * Build and execute the uscsi ioctl.  We build a group0, group1
93887a7269eSachartre 	 * or group4 command as necessary, since some targets
93987a7269eSachartre 	 * do not support group1 commands.
94087a7269eSachartre 	 */
94187a7269eSachartre 	while (nblk) {
94287a7269eSachartre 
94387a7269eSachartre 		bzero(&ucmd, sizeof (ucmd));
94487a7269eSachartre 		bzero(&cdb, sizeof (cdb));
94587a7269eSachartre 
94687a7269eSachartre 		nsectors = (max_sectors < nblk) ? max_sectors : nblk;
94787a7269eSachartre 
94887a7269eSachartre 		if (blk < (2 << 20) && nsectors <= 0xff) {
94987a7269eSachartre 			FORMG0ADDR(&cdb, blk);
95087a7269eSachartre 			FORMG0COUNT(&cdb, nsectors);
95187a7269eSachartre 			ucmd.uscsi_cdblen = CDB_GROUP0;
95287a7269eSachartre 		} else if (blk > 0xffffffff) {
95387a7269eSachartre 			FORMG4LONGADDR(&cdb, blk);
95487a7269eSachartre 			FORMG4COUNT(&cdb, nsectors);
95587a7269eSachartre 			ucmd.uscsi_cdblen = CDB_GROUP4;
95687a7269eSachartre 			cdb.scc_cmd |= SCMD_GROUP4;
95787a7269eSachartre 		} else {
95887a7269eSachartre 			FORMG1ADDR(&cdb, blk);
95987a7269eSachartre 			FORMG1COUNT(&cdb, nsectors);
96087a7269eSachartre 			ucmd.uscsi_cdblen = CDB_GROUP1;
96187a7269eSachartre 			cdb.scc_cmd |= SCMD_GROUP1;
96287a7269eSachartre 		}
96387a7269eSachartre 
96487a7269eSachartre 		ucmd.uscsi_cdb = (caddr_t)&cdb;
96587a7269eSachartre 		ucmd.uscsi_bufaddr = data;
96687a7269eSachartre 		ucmd.uscsi_buflen = nsectors * DEV_BSIZE;
96787a7269eSachartre 		ucmd.uscsi_timeout = vd_scsi_rdwr_timeout;
96887a7269eSachartre 		/*
96987a7269eSachartre 		 * Set flags so that the command is isolated from normal
97087a7269eSachartre 		 * commands and no error message is printed.
97187a7269eSachartre 		 */
97287a7269eSachartre 		ucmd.uscsi_flags = USCSI_ISOLATE | USCSI_SILENT;
97387a7269eSachartre 
97487a7269eSachartre 		if (operation == VD_OP_BREAD) {
97587a7269eSachartre 			cdb.scc_cmd |= SCMD_READ;
97687a7269eSachartre 			ucmd.uscsi_flags |= USCSI_READ;
97787a7269eSachartre 		} else {
97887a7269eSachartre 			cdb.scc_cmd |= SCMD_WRITE;
97987a7269eSachartre 		}
98087a7269eSachartre 
98187a7269eSachartre 		status = ldi_ioctl(vd->ldi_handle[VD_ENTIRE_DISK_SLICE],
98287a7269eSachartre 		    USCSICMD, (intptr_t)&ucmd, (vd_open_flags | FKIOCTL),
98387a7269eSachartre 		    kcred, &rval);
98487a7269eSachartre 
98587a7269eSachartre 		if (status == 0)
98687a7269eSachartre 			status = ucmd.uscsi_status;
98787a7269eSachartre 
98887a7269eSachartre 		if (status != 0)
98987a7269eSachartre 			break;
99087a7269eSachartre 
99187a7269eSachartre 		/*
99287a7269eSachartre 		 * Check if partial DMA breakup is required. If so, reduce
99387a7269eSachartre 		 * the request size by half and retry the last request.
99487a7269eSachartre 		 */
99587a7269eSachartre 		if (ucmd.uscsi_resid == ucmd.uscsi_buflen) {
99687a7269eSachartre 			max_sectors >>= 1;
99787a7269eSachartre 			if (max_sectors <= 0) {
99887a7269eSachartre 				status = EIO;
99987a7269eSachartre 				break;
100087a7269eSachartre 			}
100187a7269eSachartre 			continue;
100287a7269eSachartre 		}
100387a7269eSachartre 
100487a7269eSachartre 		if (ucmd.uscsi_resid != 0) {
100587a7269eSachartre 			status = EIO;
100687a7269eSachartre 			break;
100787a7269eSachartre 		}
100887a7269eSachartre 
100987a7269eSachartre 		blk += nsectors;
101087a7269eSachartre 		nblk -= nsectors;
101187a7269eSachartre 		data += nsectors * DEV_BSIZE; /* SECSIZE */
101287a7269eSachartre 	}
101387a7269eSachartre 
101487a7269eSachartre 	return (status);
101587a7269eSachartre }
101687a7269eSachartre 
1017205eeb1aSlm66018 /*
1018205eeb1aSlm66018  * Return Values
1019205eeb1aSlm66018  *	EINPROGRESS	- operation was successfully started
1020205eeb1aSlm66018  *	EIO		- encountered LDC (aka. task error)
1021205eeb1aSlm66018  *	0		- operation completed successfully
1022205eeb1aSlm66018  *
1023205eeb1aSlm66018  * Side Effect
1024205eeb1aSlm66018  *     sets request->status = <disk operation status>
1025205eeb1aSlm66018  */
10261ae08745Sheppo static int
1027d10e4ef2Snarayan vd_start_bio(vd_task_t *task)
10281ae08745Sheppo {
10294bac2208Snarayan 	int			rv, status = 0;
1030d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
1031d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
1032d10e4ef2Snarayan 	struct buf		*buf		= &task->buf;
10334bac2208Snarayan 	uint8_t			mtype;
10343c96341aSnarayan 	int 			slice;
1035d10e4ef2Snarayan 
1036d10e4ef2Snarayan 	ASSERT(vd != NULL);
1037d10e4ef2Snarayan 	ASSERT(request != NULL);
10383c96341aSnarayan 
10393c96341aSnarayan 	slice = request->slice;
10403c96341aSnarayan 
104187a7269eSachartre 	ASSERT(slice == VD_SLICE_NONE || slice < vd->nslices);
1042d10e4ef2Snarayan 	ASSERT((request->operation == VD_OP_BREAD) ||
1043d10e4ef2Snarayan 	    (request->operation == VD_OP_BWRITE));
1044d10e4ef2Snarayan 
1045205eeb1aSlm66018 	if (request->nbytes == 0) {
1046205eeb1aSlm66018 		/* no service for trivial requests */
1047205eeb1aSlm66018 		request->status = EINVAL;
1048205eeb1aSlm66018 		return (0);
1049205eeb1aSlm66018 	}
10501ae08745Sheppo 
1051d10e4ef2Snarayan 	PR1("%s %lu bytes at block %lu",
1052d10e4ef2Snarayan 	    (request->operation == VD_OP_BREAD) ? "Read" : "Write",
1053d10e4ef2Snarayan 	    request->nbytes, request->addr);
10541ae08745Sheppo 
1055d10e4ef2Snarayan 	bioinit(buf);
1056d10e4ef2Snarayan 	buf->b_flags		= B_BUSY;
1057d10e4ef2Snarayan 	buf->b_bcount		= request->nbytes;
1058d10e4ef2Snarayan 	buf->b_lblkno		= request->addr;
105987a7269eSachartre 	buf->b_edev = (slice == VD_SLICE_NONE)? NODEV : vd->dev[slice];
1060d10e4ef2Snarayan 
10614bac2208Snarayan 	mtype = (&vd->inband_task == task) ? LDC_SHADOW_MAP : LDC_DIRECT_MAP;
10624bac2208Snarayan 
10634bac2208Snarayan 	/* Map memory exported by client */
10644bac2208Snarayan 	status = ldc_mem_map(task->mhdl, request->cookie, request->ncookies,
10654bac2208Snarayan 	    mtype, (request->operation == VD_OP_BREAD) ? LDC_MEM_W : LDC_MEM_R,
10664bac2208Snarayan 	    &(buf->b_un.b_addr), NULL);
10674bac2208Snarayan 	if (status != 0) {
10683af08d82Slm66018 		PR0("ldc_mem_map() returned err %d ", status);
10694bac2208Snarayan 		biofini(buf);
1070205eeb1aSlm66018 		return (EIO);
1071d10e4ef2Snarayan 	}
1072d10e4ef2Snarayan 
10734bac2208Snarayan 	status = ldc_mem_acquire(task->mhdl, 0, buf->b_bcount);
10744bac2208Snarayan 	if (status != 0) {
10754bac2208Snarayan 		(void) ldc_mem_unmap(task->mhdl);
10763af08d82Slm66018 		PR0("ldc_mem_acquire() returned err %d ", status);
10774bac2208Snarayan 		biofini(buf);
1078205eeb1aSlm66018 		return (EIO);
10794bac2208Snarayan 	}
10804bac2208Snarayan 
10814bac2208Snarayan 	buf->b_flags |= (request->operation == VD_OP_BREAD) ? B_READ : B_WRITE;
10824bac2208Snarayan 
1083d10e4ef2Snarayan 	/* Start the block I/O */
10843c96341aSnarayan 	if (vd->file) {
1085690555a1Sachartre 		rv = vd_file_rw(vd, slice, request->operation, buf->b_un.b_addr,
1086690555a1Sachartre 		    request->addr, request->nbytes);
1087690555a1Sachartre 		if (rv < 0) {
10883c96341aSnarayan 			request->nbytes = 0;
1089205eeb1aSlm66018 			request->status = EIO;
1090690555a1Sachartre 		} else {
1091690555a1Sachartre 			request->nbytes = rv;
1092205eeb1aSlm66018 			request->status = 0;
10933c96341aSnarayan 		}
10943c96341aSnarayan 	} else {
109587a7269eSachartre 		if (slice == VD_SLICE_NONE) {
109687a7269eSachartre 			/*
109787a7269eSachartre 			 * This is not a disk image so it is a real disk. We
109887a7269eSachartre 			 * assume that the underlying device driver supports
109987a7269eSachartre 			 * USCSICMD ioctls. This is the case of all SCSI devices
110087a7269eSachartre 			 * (sd, ssd...).
110187a7269eSachartre 			 *
110287a7269eSachartre 			 * In the future if we have non-SCSI disks we would need
110387a7269eSachartre 			 * to invoke the appropriate function to do I/O using an
110487a7269eSachartre 			 * absolute disk offset (for example using DKIOCTL_RWCMD
110587a7269eSachartre 			 * for IDE disks).
110687a7269eSachartre 			 */
110787a7269eSachartre 			rv = vd_scsi_rdwr(vd, request->operation,
110887a7269eSachartre 			    buf->b_un.b_addr, request->addr, request->nbytes);
110987a7269eSachartre 			if (rv != 0) {
111087a7269eSachartre 				request->nbytes = 0;
1111205eeb1aSlm66018 				request->status = EIO;
111287a7269eSachartre 			} else {
1113205eeb1aSlm66018 				request->status = 0;
111487a7269eSachartre 			}
111587a7269eSachartre 		} else {
1116205eeb1aSlm66018 			request->status =
1117205eeb1aSlm66018 			    ldi_strategy(vd->ldi_handle[slice], buf);
1118205eeb1aSlm66018 
1119205eeb1aSlm66018 			/*
1120205eeb1aSlm66018 			 * This is to indicate to the caller that the request
1121205eeb1aSlm66018 			 * needs to be finished by vd_complete_bio() by calling
1122205eeb1aSlm66018 			 * biowait() there and waiting for that to return before
1123205eeb1aSlm66018 			 * triggering the notification of the vDisk client.
1124205eeb1aSlm66018 			 *
1125205eeb1aSlm66018 			 * This is necessary when writing to real disks as
1126205eeb1aSlm66018 			 * otherwise calls to ldi_strategy() would be serialized
1127205eeb1aSlm66018 			 * behind the calls to biowait() and performance would
1128205eeb1aSlm66018 			 * suffer.
1129205eeb1aSlm66018 			 */
1130205eeb1aSlm66018 			if (request->status == 0)
113187a7269eSachartre 				return (EINPROGRESS);
113287a7269eSachartre 		}
11333c96341aSnarayan 	}
11343c96341aSnarayan 
1135d10e4ef2Snarayan 	/* Clean up after error */
11364bac2208Snarayan 	rv = ldc_mem_release(task->mhdl, 0, buf->b_bcount);
11374bac2208Snarayan 	if (rv) {
11383af08d82Slm66018 		PR0("ldc_mem_release() returned err %d ", rv);
1139205eeb1aSlm66018 		status = EIO;
11404bac2208Snarayan 	}
11414bac2208Snarayan 	rv = ldc_mem_unmap(task->mhdl);
11424bac2208Snarayan 	if (rv) {
1143205eeb1aSlm66018 		PR0("ldc_mem_unmap() returned err %d ", rv);
1144205eeb1aSlm66018 		status = EIO;
11454bac2208Snarayan 	}
11464bac2208Snarayan 
1147d10e4ef2Snarayan 	biofini(buf);
1148205eeb1aSlm66018 
1149d10e4ef2Snarayan 	return (status);
1150d10e4ef2Snarayan }
1151d10e4ef2Snarayan 
1152205eeb1aSlm66018 /*
1153205eeb1aSlm66018  * This function should only be called from vd_notify to ensure that requests
1154205eeb1aSlm66018  * are responded to in the order that they are received.
1155205eeb1aSlm66018  */
1156d10e4ef2Snarayan static int
1157d10e4ef2Snarayan send_msg(ldc_handle_t ldc_handle, void *msg, size_t msglen)
1158d10e4ef2Snarayan {
11593af08d82Slm66018 	int	status;
1160d10e4ef2Snarayan 	size_t	nbytes;
1161d10e4ef2Snarayan 
11623af08d82Slm66018 	do {
1163d10e4ef2Snarayan 		nbytes = msglen;
1164d10e4ef2Snarayan 		status = ldc_write(ldc_handle, msg, &nbytes);
11653af08d82Slm66018 		if (status != EWOULDBLOCK)
11663af08d82Slm66018 			break;
11673af08d82Slm66018 		drv_usecwait(vds_ldc_delay);
11683af08d82Slm66018 	} while (status == EWOULDBLOCK);
1169d10e4ef2Snarayan 
1170d10e4ef2Snarayan 	if (status != 0) {
11713af08d82Slm66018 		if (status != ECONNRESET)
11723af08d82Slm66018 			PR0("ldc_write() returned errno %d", status);
1173d10e4ef2Snarayan 		return (status);
1174d10e4ef2Snarayan 	} else if (nbytes != msglen) {
11753af08d82Slm66018 		PR0("ldc_write() performed only partial write");
1176d10e4ef2Snarayan 		return (EIO);
1177d10e4ef2Snarayan 	}
1178d10e4ef2Snarayan 
1179d10e4ef2Snarayan 	PR1("SENT %lu bytes", msglen);
1180d10e4ef2Snarayan 	return (0);
1181d10e4ef2Snarayan }
1182d10e4ef2Snarayan 
1183d10e4ef2Snarayan static void
1184d10e4ef2Snarayan vd_need_reset(vd_t *vd, boolean_t reset_ldc)
1185d10e4ef2Snarayan {
1186d10e4ef2Snarayan 	mutex_enter(&vd->lock);
1187d10e4ef2Snarayan 	vd->reset_state	= B_TRUE;
1188d10e4ef2Snarayan 	vd->reset_ldc	= reset_ldc;
1189d10e4ef2Snarayan 	mutex_exit(&vd->lock);
1190d10e4ef2Snarayan }
1191d10e4ef2Snarayan 
1192d10e4ef2Snarayan /*
1193d10e4ef2Snarayan  * Reset the state of the connection with a client, if needed; reset the LDC
1194d10e4ef2Snarayan  * transport as well, if needed.  This function should only be called from the
11953af08d82Slm66018  * "vd_recv_msg", as it waits for tasks - otherwise a deadlock can occur.
1196d10e4ef2Snarayan  */
1197d10e4ef2Snarayan static void
1198d10e4ef2Snarayan vd_reset_if_needed(vd_t *vd)
1199d10e4ef2Snarayan {
1200d10e4ef2Snarayan 	int	status = 0;
1201d10e4ef2Snarayan 
1202d10e4ef2Snarayan 	mutex_enter(&vd->lock);
1203d10e4ef2Snarayan 	if (!vd->reset_state) {
1204d10e4ef2Snarayan 		ASSERT(!vd->reset_ldc);
1205d10e4ef2Snarayan 		mutex_exit(&vd->lock);
1206d10e4ef2Snarayan 		return;
1207d10e4ef2Snarayan 	}
1208d10e4ef2Snarayan 	mutex_exit(&vd->lock);
1209d10e4ef2Snarayan 
1210d10e4ef2Snarayan 	PR0("Resetting connection state with %s", VD_CLIENT(vd));
1211d10e4ef2Snarayan 
1212d10e4ef2Snarayan 	/*
1213d10e4ef2Snarayan 	 * Let any asynchronous I/O complete before possibly pulling the rug
1214d10e4ef2Snarayan 	 * out from under it; defer checking vd->reset_ldc, as one of the
1215d10e4ef2Snarayan 	 * asynchronous tasks might set it
1216d10e4ef2Snarayan 	 */
1217d10e4ef2Snarayan 	ddi_taskq_wait(vd->completionq);
1218d10e4ef2Snarayan 
12193c96341aSnarayan 	if (vd->file) {
12203c96341aSnarayan 		status = VOP_FSYNC(vd->file_vnode, FSYNC, kcred);
12213c96341aSnarayan 		if (status) {
12223c96341aSnarayan 			PR0("VOP_FSYNC returned errno %d", status);
12233c96341aSnarayan 		}
12243c96341aSnarayan 	}
12253c96341aSnarayan 
1226d10e4ef2Snarayan 	if ((vd->initialized & VD_DRING) &&
1227d10e4ef2Snarayan 	    ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0))
12283af08d82Slm66018 		PR0("ldc_mem_dring_unmap() returned errno %d", status);
1229d10e4ef2Snarayan 
12303af08d82Slm66018 	vd_free_dring_task(vd);
12313af08d82Slm66018 
12323af08d82Slm66018 	/* Free the staging buffer for msgs */
12333af08d82Slm66018 	if (vd->vio_msgp != NULL) {
12343af08d82Slm66018 		kmem_free(vd->vio_msgp, vd->max_msglen);
12353af08d82Slm66018 		vd->vio_msgp = NULL;
1236d10e4ef2Snarayan 	}
1237d10e4ef2Snarayan 
12383af08d82Slm66018 	/* Free the inband message buffer */
12393af08d82Slm66018 	if (vd->inband_task.msg != NULL) {
12403af08d82Slm66018 		kmem_free(vd->inband_task.msg, vd->max_msglen);
12413af08d82Slm66018 		vd->inband_task.msg = NULL;
12423af08d82Slm66018 	}
1243d10e4ef2Snarayan 
1244d10e4ef2Snarayan 	mutex_enter(&vd->lock);
12453af08d82Slm66018 
12463af08d82Slm66018 	if (vd->reset_ldc)
12473af08d82Slm66018 		PR0("taking down LDC channel");
1248e1ebb9ecSlm66018 	if (vd->reset_ldc && ((status = ldc_down(vd->ldc_handle)) != 0))
12493af08d82Slm66018 		PR0("ldc_down() returned errno %d", status);
1250d10e4ef2Snarayan 
1251d10e4ef2Snarayan 	vd->initialized	&= ~(VD_SID | VD_SEQ_NUM | VD_DRING);
1252d10e4ef2Snarayan 	vd->state	= VD_STATE_INIT;
1253d10e4ef2Snarayan 	vd->max_msglen	= sizeof (vio_msg_t);	/* baseline vio message size */
1254d10e4ef2Snarayan 
12553af08d82Slm66018 	/* Allocate the staging buffer */
12563af08d82Slm66018 	vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
12573af08d82Slm66018 
12583af08d82Slm66018 	PR0("calling ldc_up\n");
12593af08d82Slm66018 	(void) ldc_up(vd->ldc_handle);
12603af08d82Slm66018 
1261d10e4ef2Snarayan 	vd->reset_state	= B_FALSE;
1262d10e4ef2Snarayan 	vd->reset_ldc	= B_FALSE;
12633af08d82Slm66018 
1264d10e4ef2Snarayan 	mutex_exit(&vd->lock);
1265d10e4ef2Snarayan }
1266d10e4ef2Snarayan 
12673af08d82Slm66018 static void vd_recv_msg(void *arg);
12683af08d82Slm66018 
12693af08d82Slm66018 static void
12703af08d82Slm66018 vd_mark_in_reset(vd_t *vd)
12713af08d82Slm66018 {
12723af08d82Slm66018 	int status;
12733af08d82Slm66018 
12743af08d82Slm66018 	PR0("vd_mark_in_reset: marking vd in reset\n");
12753af08d82Slm66018 
12763af08d82Slm66018 	vd_need_reset(vd, B_FALSE);
12773af08d82Slm66018 	status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, DDI_SLEEP);
12783af08d82Slm66018 	if (status == DDI_FAILURE) {
12793af08d82Slm66018 		PR0("cannot schedule task to recv msg\n");
12803af08d82Slm66018 		vd_need_reset(vd, B_TRUE);
12813af08d82Slm66018 		return;
12823af08d82Slm66018 	}
12833af08d82Slm66018 }
12843af08d82Slm66018 
1285d10e4ef2Snarayan static int
12863c96341aSnarayan vd_mark_elem_done(vd_t *vd, int idx, int elem_status, int elem_nbytes)
1287d10e4ef2Snarayan {
1288d10e4ef2Snarayan 	boolean_t		accepted;
1289d10e4ef2Snarayan 	int			status;
1290d10e4ef2Snarayan 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
1291d10e4ef2Snarayan 
12923af08d82Slm66018 	if (vd->reset_state)
12933af08d82Slm66018 		return (0);
1294d10e4ef2Snarayan 
1295d10e4ef2Snarayan 	/* Acquire the element */
12963af08d82Slm66018 	if (!vd->reset_state &&
12973af08d82Slm66018 	    (status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) {
12983af08d82Slm66018 		if (status == ECONNRESET) {
12993af08d82Slm66018 			vd_mark_in_reset(vd);
13003af08d82Slm66018 			return (0);
13013af08d82Slm66018 		} else {
13023af08d82Slm66018 			PR0("ldc_mem_dring_acquire() returned errno %d",
13033af08d82Slm66018 			    status);
1304d10e4ef2Snarayan 			return (status);
1305d10e4ef2Snarayan 		}
13063af08d82Slm66018 	}
1307d10e4ef2Snarayan 
1308d10e4ef2Snarayan 	/* Set the element's status and mark it done */
1309d10e4ef2Snarayan 	accepted = (elem->hdr.dstate == VIO_DESC_ACCEPTED);
1310d10e4ef2Snarayan 	if (accepted) {
13113c96341aSnarayan 		elem->payload.nbytes	= elem_nbytes;
1312d10e4ef2Snarayan 		elem->payload.status	= elem_status;
1313d10e4ef2Snarayan 		elem->hdr.dstate	= VIO_DESC_DONE;
1314d10e4ef2Snarayan 	} else {
1315d10e4ef2Snarayan 		/* Perhaps client timed out waiting for I/O... */
13163af08d82Slm66018 		PR0("element %u no longer \"accepted\"", idx);
1317d10e4ef2Snarayan 		VD_DUMP_DRING_ELEM(elem);
1318d10e4ef2Snarayan 	}
1319d10e4ef2Snarayan 	/* Release the element */
13203af08d82Slm66018 	if (!vd->reset_state &&
13213af08d82Slm66018 	    (status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) {
13223af08d82Slm66018 		if (status == ECONNRESET) {
13233af08d82Slm66018 			vd_mark_in_reset(vd);
13243af08d82Slm66018 			return (0);
13253af08d82Slm66018 		} else {
13263af08d82Slm66018 			PR0("ldc_mem_dring_release() returned errno %d",
13273af08d82Slm66018 			    status);
1328d10e4ef2Snarayan 			return (status);
1329d10e4ef2Snarayan 		}
13303af08d82Slm66018 	}
1331d10e4ef2Snarayan 
1332d10e4ef2Snarayan 	return (accepted ? 0 : EINVAL);
1333d10e4ef2Snarayan }
1334d10e4ef2Snarayan 
1335205eeb1aSlm66018 /*
1336205eeb1aSlm66018  * Return Values
1337205eeb1aSlm66018  *	0	- operation completed successfully
1338205eeb1aSlm66018  *	EIO	- encountered LDC / task error
1339205eeb1aSlm66018  *
1340205eeb1aSlm66018  * Side Effect
1341205eeb1aSlm66018  *	sets request->status = <disk operation status>
1342205eeb1aSlm66018  */
1343205eeb1aSlm66018 static int
1344205eeb1aSlm66018 vd_complete_bio(vd_task_t *task)
1345d10e4ef2Snarayan {
1346d10e4ef2Snarayan 	int			status		= 0;
1347205eeb1aSlm66018 	int			rv		= 0;
1348d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
1349d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
1350d10e4ef2Snarayan 	struct buf		*buf		= &task->buf;
1351d10e4ef2Snarayan 
1352d10e4ef2Snarayan 
1353d10e4ef2Snarayan 	ASSERT(vd != NULL);
1354d10e4ef2Snarayan 	ASSERT(request != NULL);
1355d10e4ef2Snarayan 	ASSERT(task->msg != NULL);
1356d10e4ef2Snarayan 	ASSERT(task->msglen >= sizeof (*task->msg));
13573c96341aSnarayan 	ASSERT(!vd->file);
1358205eeb1aSlm66018 	ASSERT(request->slice != VD_SLICE_NONE);
1359d10e4ef2Snarayan 
1360205eeb1aSlm66018 	/* Wait for the I/O to complete [ call to ldi_strategy(9f) ] */
1361d10e4ef2Snarayan 	request->status = biowait(buf);
1362d10e4ef2Snarayan 
13633c96341aSnarayan 	/* return back the number of bytes read/written */
13643c96341aSnarayan 	request->nbytes = buf->b_bcount - buf->b_resid;
13653c96341aSnarayan 
13664bac2208Snarayan 	/* Release the buffer */
13673af08d82Slm66018 	if (!vd->reset_state)
13684bac2208Snarayan 		status = ldc_mem_release(task->mhdl, 0, buf->b_bcount);
13694bac2208Snarayan 	if (status) {
13703af08d82Slm66018 		PR0("ldc_mem_release() returned errno %d copying to "
13713af08d82Slm66018 		    "client", status);
13723af08d82Slm66018 		if (status == ECONNRESET) {
13733af08d82Slm66018 			vd_mark_in_reset(vd);
13743af08d82Slm66018 		}
1375205eeb1aSlm66018 		rv = EIO;
13761ae08745Sheppo 	}
13771ae08745Sheppo 
13783af08d82Slm66018 	/* Unmap the memory, even if in reset */
13794bac2208Snarayan 	status = ldc_mem_unmap(task->mhdl);
13804bac2208Snarayan 	if (status) {
13813af08d82Slm66018 		PR0("ldc_mem_unmap() returned errno %d copying to client",
13824bac2208Snarayan 		    status);
13833af08d82Slm66018 		if (status == ECONNRESET) {
13843af08d82Slm66018 			vd_mark_in_reset(vd);
13853af08d82Slm66018 		}
1386205eeb1aSlm66018 		rv = EIO;
13874bac2208Snarayan 	}
13884bac2208Snarayan 
1389d10e4ef2Snarayan 	biofini(buf);
13901ae08745Sheppo 
1391205eeb1aSlm66018 	return (rv);
1392205eeb1aSlm66018 }
1393205eeb1aSlm66018 
1394205eeb1aSlm66018 /*
1395205eeb1aSlm66018  * Description:
1396205eeb1aSlm66018  *	This function is called by the two functions called by a taskq
1397205eeb1aSlm66018  *	[ vd_complete_notify() and vd_serial_notify()) ] to send the
1398205eeb1aSlm66018  *	message to the client.
1399205eeb1aSlm66018  *
1400205eeb1aSlm66018  * Parameters:
1401205eeb1aSlm66018  *	arg 	- opaque pointer to structure containing task to be completed
1402205eeb1aSlm66018  *
1403205eeb1aSlm66018  * Return Values
1404205eeb1aSlm66018  *	None
1405205eeb1aSlm66018  */
1406205eeb1aSlm66018 static void
1407205eeb1aSlm66018 vd_notify(vd_task_t *task)
1408205eeb1aSlm66018 {
1409205eeb1aSlm66018 	int	status;
1410205eeb1aSlm66018 
1411205eeb1aSlm66018 	ASSERT(task != NULL);
1412205eeb1aSlm66018 	ASSERT(task->vd != NULL);
1413205eeb1aSlm66018 
1414205eeb1aSlm66018 	if (task->vd->reset_state)
1415205eeb1aSlm66018 		return;
1416205eeb1aSlm66018 
1417205eeb1aSlm66018 	/*
1418205eeb1aSlm66018 	 * Send the "ack" or "nack" back to the client; if sending the message
1419205eeb1aSlm66018 	 * via LDC fails, arrange to reset both the connection state and LDC
1420205eeb1aSlm66018 	 * itself
1421205eeb1aSlm66018 	 */
1422205eeb1aSlm66018 	PR2("Sending %s",
1423205eeb1aSlm66018 	    (task->msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK");
1424205eeb1aSlm66018 
1425205eeb1aSlm66018 	status = send_msg(task->vd->ldc_handle, task->msg, task->msglen);
1426205eeb1aSlm66018 	switch (status) {
1427205eeb1aSlm66018 	case 0:
1428205eeb1aSlm66018 		break;
1429205eeb1aSlm66018 	case ECONNRESET:
1430205eeb1aSlm66018 		vd_mark_in_reset(task->vd);
1431205eeb1aSlm66018 		break;
1432205eeb1aSlm66018 	default:
1433205eeb1aSlm66018 		PR0("initiating full reset");
1434205eeb1aSlm66018 		vd_need_reset(task->vd, B_TRUE);
1435205eeb1aSlm66018 		break;
1436205eeb1aSlm66018 	}
1437205eeb1aSlm66018 
1438205eeb1aSlm66018 	DTRACE_PROBE1(task__end, vd_task_t *, task);
1439205eeb1aSlm66018 }
1440205eeb1aSlm66018 
1441205eeb1aSlm66018 /*
1442205eeb1aSlm66018  * Description:
1443205eeb1aSlm66018  *	Mark the Dring entry as Done and (if necessary) send an ACK/NACK to
1444205eeb1aSlm66018  *	the vDisk client
1445205eeb1aSlm66018  *
1446205eeb1aSlm66018  * Parameters:
1447205eeb1aSlm66018  *	task 		- structure containing the request sent from client
1448205eeb1aSlm66018  *
1449205eeb1aSlm66018  * Return Values
1450205eeb1aSlm66018  *	None
1451205eeb1aSlm66018  */
1452205eeb1aSlm66018 static void
1453205eeb1aSlm66018 vd_complete_notify(vd_task_t *task)
1454205eeb1aSlm66018 {
1455205eeb1aSlm66018 	int			status		= 0;
1456205eeb1aSlm66018 	vd_t			*vd		= task->vd;
1457205eeb1aSlm66018 	vd_dring_payload_t	*request	= task->request;
1458205eeb1aSlm66018 
1459d10e4ef2Snarayan 	/* Update the dring element for a dring client */
1460205eeb1aSlm66018 	if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE)) {
14613c96341aSnarayan 		status = vd_mark_elem_done(vd, task->index,
14623c96341aSnarayan 		    request->status, request->nbytes);
14633af08d82Slm66018 		if (status == ECONNRESET)
14643af08d82Slm66018 			vd_mark_in_reset(vd);
14653af08d82Slm66018 	}
14661ae08745Sheppo 
1467d10e4ef2Snarayan 	/*
1468205eeb1aSlm66018 	 * If a transport error occurred while marking the element done or
1469205eeb1aSlm66018 	 * previously while executing the task, arrange to "nack" the message
1470205eeb1aSlm66018 	 * when the final task in the descriptor element range completes
1471d10e4ef2Snarayan 	 */
1472205eeb1aSlm66018 	if ((status != 0) || (task->status != 0))
1473d10e4ef2Snarayan 		task->msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
14741ae08745Sheppo 
1475d10e4ef2Snarayan 	/*
1476d10e4ef2Snarayan 	 * Only the final task for a range of elements will respond to and
1477d10e4ef2Snarayan 	 * free the message
1478d10e4ef2Snarayan 	 */
14793af08d82Slm66018 	if (task->type == VD_NONFINAL_RANGE_TASK) {
1480d10e4ef2Snarayan 		return;
14813af08d82Slm66018 	}
14821ae08745Sheppo 
1483205eeb1aSlm66018 	vd_notify(task);
1484205eeb1aSlm66018 }
1485205eeb1aSlm66018 
1486d10e4ef2Snarayan /*
1487205eeb1aSlm66018  * Description:
1488205eeb1aSlm66018  *	This is the basic completion function called to handle inband data
1489205eeb1aSlm66018  *	requests and handshake messages. All it needs to do is trigger a
1490205eeb1aSlm66018  *	message to the client that the request is completed.
1491205eeb1aSlm66018  *
1492205eeb1aSlm66018  * Parameters:
1493205eeb1aSlm66018  *	arg 	- opaque pointer to structure containing task to be completed
1494205eeb1aSlm66018  *
1495205eeb1aSlm66018  * Return Values
1496205eeb1aSlm66018  *	None
1497d10e4ef2Snarayan  */
1498205eeb1aSlm66018 static void
1499205eeb1aSlm66018 vd_serial_notify(void *arg)
1500205eeb1aSlm66018 {
1501205eeb1aSlm66018 	vd_task_t		*task = (vd_task_t *)arg;
1502205eeb1aSlm66018 
1503205eeb1aSlm66018 	ASSERT(task != NULL);
1504205eeb1aSlm66018 	vd_notify(task);
15051ae08745Sheppo }
15061ae08745Sheppo 
15070a55fbb7Slm66018 static void
15080a55fbb7Slm66018 vd_geom2dk_geom(void *vd_buf, void *ioctl_arg)
15090a55fbb7Slm66018 {
15100a55fbb7Slm66018 	VD_GEOM2DK_GEOM((vd_geom_t *)vd_buf, (struct dk_geom *)ioctl_arg);
15110a55fbb7Slm66018 }
15120a55fbb7Slm66018 
15130a55fbb7Slm66018 static void
15140a55fbb7Slm66018 vd_vtoc2vtoc(void *vd_buf, void *ioctl_arg)
15150a55fbb7Slm66018 {
15160a55fbb7Slm66018 	VD_VTOC2VTOC((vd_vtoc_t *)vd_buf, (struct vtoc *)ioctl_arg);
15170a55fbb7Slm66018 }
15180a55fbb7Slm66018 
15190a55fbb7Slm66018 static void
15200a55fbb7Slm66018 dk_geom2vd_geom(void *ioctl_arg, void *vd_buf)
15210a55fbb7Slm66018 {
15220a55fbb7Slm66018 	DK_GEOM2VD_GEOM((struct dk_geom *)ioctl_arg, (vd_geom_t *)vd_buf);
15230a55fbb7Slm66018 }
15240a55fbb7Slm66018 
15250a55fbb7Slm66018 static void
15260a55fbb7Slm66018 vtoc2vd_vtoc(void *ioctl_arg, void *vd_buf)
15270a55fbb7Slm66018 {
15280a55fbb7Slm66018 	VTOC2VD_VTOC((struct vtoc *)ioctl_arg, (vd_vtoc_t *)vd_buf);
15290a55fbb7Slm66018 }
15300a55fbb7Slm66018 
15314bac2208Snarayan static void
15324bac2208Snarayan vd_get_efi_in(void *vd_buf, void *ioctl_arg)
15334bac2208Snarayan {
15344bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
15354bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
15364bac2208Snarayan 
15374bac2208Snarayan 	dk_efi->dki_lba = vd_efi->lba;
15384bac2208Snarayan 	dk_efi->dki_length = vd_efi->length;
15394bac2208Snarayan 	dk_efi->dki_data = kmem_zalloc(vd_efi->length, KM_SLEEP);
15404bac2208Snarayan }
15414bac2208Snarayan 
15424bac2208Snarayan static void
15434bac2208Snarayan vd_get_efi_out(void *ioctl_arg, void *vd_buf)
15444bac2208Snarayan {
15454bac2208Snarayan 	int len;
15464bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
15474bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
15484bac2208Snarayan 
15494bac2208Snarayan 	len = vd_efi->length;
15504bac2208Snarayan 	DK_EFI2VD_EFI(dk_efi, vd_efi);
15514bac2208Snarayan 	kmem_free(dk_efi->dki_data, len);
15524bac2208Snarayan }
15534bac2208Snarayan 
15544bac2208Snarayan static void
15554bac2208Snarayan vd_set_efi_in(void *vd_buf, void *ioctl_arg)
15564bac2208Snarayan {
15574bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
15584bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
15594bac2208Snarayan 
15604bac2208Snarayan 	dk_efi->dki_data = kmem_alloc(vd_efi->length, KM_SLEEP);
15614bac2208Snarayan 	VD_EFI2DK_EFI(vd_efi, dk_efi);
15624bac2208Snarayan }
15634bac2208Snarayan 
15644bac2208Snarayan static void
15654bac2208Snarayan vd_set_efi_out(void *ioctl_arg, void *vd_buf)
15664bac2208Snarayan {
15674bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
15684bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
15694bac2208Snarayan 
15704bac2208Snarayan 	kmem_free(dk_efi->dki_data, vd_efi->length);
15714bac2208Snarayan }
15724bac2208Snarayan 
1573*78fcd0a1Sachartre static vd_disk_label_t
1574*78fcd0a1Sachartre vd_read_vtoc(ldi_handle_t handle, struct vtoc *vtoc)
15754bac2208Snarayan {
15764bac2208Snarayan 	int status, rval;
15774bac2208Snarayan 	struct dk_gpt *efi;
15784bac2208Snarayan 	size_t efi_len;
15794bac2208Snarayan 
15804bac2208Snarayan 	status = ldi_ioctl(handle, DKIOCGVTOC, (intptr_t)vtoc,
15814bac2208Snarayan 	    (vd_open_flags | FKIOCTL), kcred, &rval);
15824bac2208Snarayan 
15834bac2208Snarayan 	if (status == 0) {
1584*78fcd0a1Sachartre 		return (VD_DISK_LABEL_VTOC);
15854bac2208Snarayan 	} else if (status != ENOTSUP) {
15863af08d82Slm66018 		PR0("ldi_ioctl(DKIOCGVTOC) returned error %d", status);
1587*78fcd0a1Sachartre 		return (VD_DISK_LABEL_UNK);
15884bac2208Snarayan 	}
15894bac2208Snarayan 
15904bac2208Snarayan 	status = vds_efi_alloc_and_read(handle, &efi, &efi_len);
15914bac2208Snarayan 
15924bac2208Snarayan 	if (status) {
15933af08d82Slm66018 		PR0("vds_efi_alloc_and_read returned error %d", status);
1594*78fcd0a1Sachartre 		return (VD_DISK_LABEL_UNK);
15954bac2208Snarayan 	}
15964bac2208Snarayan 
15974bac2208Snarayan 	vd_efi_to_vtoc(efi, vtoc);
15984bac2208Snarayan 	vd_efi_free(efi, efi_len);
15994bac2208Snarayan 
1600*78fcd0a1Sachartre 	return (VD_DISK_LABEL_EFI);
16014bac2208Snarayan }
16024bac2208Snarayan 
1603690555a1Sachartre static ushort_t
16043c96341aSnarayan vd_lbl2cksum(struct dk_label *label)
16053c96341aSnarayan {
16063c96341aSnarayan 	int	count;
1607690555a1Sachartre 	ushort_t sum, *sp;
16083c96341aSnarayan 
16093c96341aSnarayan 	count =	(sizeof (struct dk_label)) / (sizeof (short)) - 1;
1610690555a1Sachartre 	sp = (ushort_t *)label;
16113c96341aSnarayan 	sum = 0;
16123c96341aSnarayan 	while (count--) {
16133c96341aSnarayan 		sum ^= *sp++;
16143c96341aSnarayan 	}
16153c96341aSnarayan 
16163c96341aSnarayan 	return (sum);
16173c96341aSnarayan }
16183c96341aSnarayan 
161987a7269eSachartre /*
162087a7269eSachartre  * Handle ioctls to a disk slice.
1621205eeb1aSlm66018  *
1622205eeb1aSlm66018  * Return Values
1623205eeb1aSlm66018  *	0	- Indicates that there are no errors in disk operations
1624205eeb1aSlm66018  *	ENOTSUP	- Unknown disk label type or unsupported DKIO ioctl
1625205eeb1aSlm66018  *	EINVAL	- Not enough room to copy the EFI label
1626205eeb1aSlm66018  *
162787a7269eSachartre  */
16281ae08745Sheppo static int
16290a55fbb7Slm66018 vd_do_slice_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
16301ae08745Sheppo {
16314bac2208Snarayan 	dk_efi_t *dk_ioc;
16324bac2208Snarayan 
16334bac2208Snarayan 	switch (vd->vdisk_label) {
16344bac2208Snarayan 
163587a7269eSachartre 	/* ioctls for a slice from a disk with a VTOC label */
16364bac2208Snarayan 	case VD_DISK_LABEL_VTOC:
16374bac2208Snarayan 
16381ae08745Sheppo 		switch (cmd) {
16391ae08745Sheppo 		case DKIOCGGEOM:
16400a55fbb7Slm66018 			ASSERT(ioctl_arg != NULL);
16410a55fbb7Slm66018 			bcopy(&vd->dk_geom, ioctl_arg, sizeof (vd->dk_geom));
16421ae08745Sheppo 			return (0);
16431ae08745Sheppo 		case DKIOCGVTOC:
16440a55fbb7Slm66018 			ASSERT(ioctl_arg != NULL);
16450a55fbb7Slm66018 			bcopy(&vd->vtoc, ioctl_arg, sizeof (vd->vtoc));
16461ae08745Sheppo 			return (0);
164787a7269eSachartre 		default:
16483c96341aSnarayan 			return (ENOTSUP);
164987a7269eSachartre 		}
165087a7269eSachartre 
165187a7269eSachartre 	/* ioctls for a slice from a disk with an EFI label */
165287a7269eSachartre 	case VD_DISK_LABEL_EFI:
165387a7269eSachartre 
165487a7269eSachartre 		switch (cmd) {
165587a7269eSachartre 		case DKIOCGETEFI:
16563c96341aSnarayan 			ASSERT(ioctl_arg != NULL);
165787a7269eSachartre 			dk_ioc = (dk_efi_t *)ioctl_arg;
165887a7269eSachartre 			if (dk_ioc->dki_length < vd->dk_efi.dki_length)
165987a7269eSachartre 				return (EINVAL);
166087a7269eSachartre 			bcopy(vd->dk_efi.dki_data, dk_ioc->dki_data,
166187a7269eSachartre 			    vd->dk_efi.dki_length);
166287a7269eSachartre 			return (0);
166387a7269eSachartre 		default:
166487a7269eSachartre 			return (ENOTSUP);
166587a7269eSachartre 		}
166687a7269eSachartre 
166787a7269eSachartre 	default:
1668205eeb1aSlm66018 		/* Unknown disk label type */
166987a7269eSachartre 		return (ENOTSUP);
167087a7269eSachartre 	}
167187a7269eSachartre }
167287a7269eSachartre 
167387a7269eSachartre /*
1674*78fcd0a1Sachartre  * Function:
1675*78fcd0a1Sachartre  *	vd_file_validate_geometry
1676205eeb1aSlm66018  *
1677*78fcd0a1Sachartre  * Description:
1678*78fcd0a1Sachartre  *	Read the label and validate the geometry of a disk image. The driver
1679*78fcd0a1Sachartre  *	label, vtoc and geometry information are updated according to the
1680*78fcd0a1Sachartre  *	label read from the disk image.
1681*78fcd0a1Sachartre  *
1682*78fcd0a1Sachartre  *	If no valid label is found, the label is set to unknown and the
1683*78fcd0a1Sachartre  *	function returns EINVAL, but a default vtoc and geometry are provided
1684*78fcd0a1Sachartre  *	to the driver.
1685*78fcd0a1Sachartre  *
1686*78fcd0a1Sachartre  * Parameters:
1687*78fcd0a1Sachartre  *	vd	- disk on which the operation is performed.
1688*78fcd0a1Sachartre  *
1689*78fcd0a1Sachartre  * Return Code:
1690*78fcd0a1Sachartre  *	0	- success.
1691*78fcd0a1Sachartre  *	EIO	- error reading the label from the disk image.
1692*78fcd0a1Sachartre  *	EINVAL	- unknown disk label.
169387a7269eSachartre  */
169487a7269eSachartre static int
1695*78fcd0a1Sachartre vd_file_validate_geometry(vd_t *vd)
169687a7269eSachartre {
169787a7269eSachartre 	struct dk_label label;
1698*78fcd0a1Sachartre 	struct dk_geom *geom = &vd->dk_geom;
1699*78fcd0a1Sachartre 	struct vtoc *vtoc = &vd->vtoc;
1700*78fcd0a1Sachartre 	int i;
1701*78fcd0a1Sachartre 	int status = 0;
170287a7269eSachartre 
170387a7269eSachartre 	ASSERT(vd->file);
170487a7269eSachartre 
170587a7269eSachartre 	if (VD_FILE_LABEL_READ(vd, &label) < 0)
170687a7269eSachartre 		return (EIO);
170787a7269eSachartre 
170887a7269eSachartre 	if (label.dkl_magic != DKL_MAGIC ||
1709*78fcd0a1Sachartre 	    label.dkl_cksum != vd_lbl2cksum(&label) ||
1710*78fcd0a1Sachartre 	    label.dkl_vtoc.v_sanity != VTOC_SANE ||
1711*78fcd0a1Sachartre 	    label.dkl_vtoc.v_nparts != V_NUMPAR) {
1712*78fcd0a1Sachartre 		vd->vdisk_label = VD_DISK_LABEL_UNK;
1713*78fcd0a1Sachartre 		vd_file_build_default_label(vd, &label);
1714*78fcd0a1Sachartre 		status = EINVAL;
1715*78fcd0a1Sachartre 	} else {
1716*78fcd0a1Sachartre 		vd->vdisk_label = VD_DISK_LABEL_VTOC;
1717*78fcd0a1Sachartre 	}
171887a7269eSachartre 
1719*78fcd0a1Sachartre 	/* Update the driver geometry */
172087a7269eSachartre 	bzero(geom, sizeof (struct dk_geom));
1721*78fcd0a1Sachartre 
172287a7269eSachartre 	geom->dkg_ncyl = label.dkl_ncyl;
172387a7269eSachartre 	geom->dkg_acyl = label.dkl_acyl;
172487a7269eSachartre 	geom->dkg_nhead = label.dkl_nhead;
172587a7269eSachartre 	geom->dkg_nsect = label.dkl_nsect;
172687a7269eSachartre 	geom->dkg_intrlv = label.dkl_intrlv;
172787a7269eSachartre 	geom->dkg_apc = label.dkl_apc;
172887a7269eSachartre 	geom->dkg_rpm = label.dkl_rpm;
172987a7269eSachartre 	geom->dkg_pcyl = label.dkl_pcyl;
173087a7269eSachartre 	geom->dkg_write_reinstruct = label.dkl_write_reinstruct;
173187a7269eSachartre 	geom->dkg_read_reinstruct = label.dkl_read_reinstruct;
173287a7269eSachartre 
1733*78fcd0a1Sachartre 	/* Update the driver vtoc */
173487a7269eSachartre 	bzero(vtoc, sizeof (struct vtoc));
173587a7269eSachartre 
173687a7269eSachartre 	vtoc->v_sanity = label.dkl_vtoc.v_sanity;
173787a7269eSachartre 	vtoc->v_version = label.dkl_vtoc.v_version;
173887a7269eSachartre 	vtoc->v_sectorsz = DEV_BSIZE;
173987a7269eSachartre 	vtoc->v_nparts = label.dkl_vtoc.v_nparts;
174087a7269eSachartre 
174187a7269eSachartre 	for (i = 0; i < vtoc->v_nparts; i++) {
174287a7269eSachartre 		vtoc->v_part[i].p_tag =
174387a7269eSachartre 		    label.dkl_vtoc.v_part[i].p_tag;
174487a7269eSachartre 		vtoc->v_part[i].p_flag =
174587a7269eSachartre 		    label.dkl_vtoc.v_part[i].p_flag;
174687a7269eSachartre 		vtoc->v_part[i].p_start =
174787a7269eSachartre 		    label.dkl_map[i].dkl_cylno *
174887a7269eSachartre 		    (label.dkl_nhead * label.dkl_nsect);
174987a7269eSachartre 		vtoc->v_part[i].p_size = label.dkl_map[i].dkl_nblk;
175087a7269eSachartre 		vtoc->timestamp[i] =
175187a7269eSachartre 		    label.dkl_vtoc.v_timestamp[i];
175287a7269eSachartre 	}
175387a7269eSachartre 	/*
175487a7269eSachartre 	 * The bootinfo array can not be copied with bcopy() because
175587a7269eSachartre 	 * elements are of type long in vtoc (so 64-bit) and of type
175687a7269eSachartre 	 * int in dk_vtoc (so 32-bit).
175787a7269eSachartre 	 */
175887a7269eSachartre 	vtoc->v_bootinfo[0] = label.dkl_vtoc.v_bootinfo[0];
175987a7269eSachartre 	vtoc->v_bootinfo[1] = label.dkl_vtoc.v_bootinfo[1];
176087a7269eSachartre 	vtoc->v_bootinfo[2] = label.dkl_vtoc.v_bootinfo[2];
176187a7269eSachartre 	bcopy(label.dkl_asciilabel, vtoc->v_asciilabel,
176287a7269eSachartre 	    LEN_DKL_ASCII);
176387a7269eSachartre 	bcopy(label.dkl_vtoc.v_volume, vtoc->v_volume,
176487a7269eSachartre 	    LEN_DKL_VVOL);
176587a7269eSachartre 
1766*78fcd0a1Sachartre 	return (status);
1767*78fcd0a1Sachartre }
1768*78fcd0a1Sachartre 
1769*78fcd0a1Sachartre /*
1770*78fcd0a1Sachartre  * Handle ioctls to a disk image (file-based).
1771*78fcd0a1Sachartre  *
1772*78fcd0a1Sachartre  * Return Values
1773*78fcd0a1Sachartre  *	0	- Indicates that there are no errors
1774*78fcd0a1Sachartre  *	!= 0	- Disk operation returned an error
1775*78fcd0a1Sachartre  */
1776*78fcd0a1Sachartre static int
1777*78fcd0a1Sachartre vd_do_file_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
1778*78fcd0a1Sachartre {
1779*78fcd0a1Sachartre 	struct dk_label label;
1780*78fcd0a1Sachartre 	struct dk_geom *geom;
1781*78fcd0a1Sachartre 	struct vtoc *vtoc;
1782*78fcd0a1Sachartre 	int i, rc;
1783*78fcd0a1Sachartre 
1784*78fcd0a1Sachartre 	ASSERT(vd->file);
1785*78fcd0a1Sachartre 
1786*78fcd0a1Sachartre 	switch (cmd) {
1787*78fcd0a1Sachartre 
1788*78fcd0a1Sachartre 	case DKIOCGGEOM:
1789*78fcd0a1Sachartre 		ASSERT(ioctl_arg != NULL);
1790*78fcd0a1Sachartre 		geom = (struct dk_geom *)ioctl_arg;
1791*78fcd0a1Sachartre 
1792*78fcd0a1Sachartre 		rc = vd_file_validate_geometry(vd);
1793*78fcd0a1Sachartre 		if (rc != 0 && rc != EINVAL)
1794*78fcd0a1Sachartre 			return (rc);
1795*78fcd0a1Sachartre 
1796*78fcd0a1Sachartre 		bcopy(&vd->dk_geom, geom, sizeof (struct dk_geom));
1797*78fcd0a1Sachartre 		return (0);
1798*78fcd0a1Sachartre 
1799*78fcd0a1Sachartre 	case DKIOCGVTOC:
1800*78fcd0a1Sachartre 		ASSERT(ioctl_arg != NULL);
1801*78fcd0a1Sachartre 		vtoc = (struct vtoc *)ioctl_arg;
1802*78fcd0a1Sachartre 
1803*78fcd0a1Sachartre 		rc = vd_file_validate_geometry(vd);
1804*78fcd0a1Sachartre 		if (rc != 0 && rc != EINVAL)
1805*78fcd0a1Sachartre 			return (rc);
1806*78fcd0a1Sachartre 
1807*78fcd0a1Sachartre 		bcopy(&vd->vtoc, vtoc, sizeof (struct vtoc));
180887a7269eSachartre 		return (0);
180987a7269eSachartre 
181087a7269eSachartre 	case DKIOCSGEOM:
181187a7269eSachartre 		ASSERT(ioctl_arg != NULL);
181287a7269eSachartre 		geom = (struct dk_geom *)ioctl_arg;
181387a7269eSachartre 
181487a7269eSachartre 		if (geom->dkg_nhead == 0 || geom->dkg_nsect == 0)
181587a7269eSachartre 			return (EINVAL);
181687a7269eSachartre 
181787a7269eSachartre 		/*
181887a7269eSachartre 		 * The current device geometry is not updated, just the driver
181987a7269eSachartre 		 * "notion" of it. The device geometry will be effectively
182087a7269eSachartre 		 * updated when a label is written to the device during a next
182187a7269eSachartre 		 * DKIOCSVTOC.
182287a7269eSachartre 		 */
182387a7269eSachartre 		bcopy(ioctl_arg, &vd->dk_geom, sizeof (vd->dk_geom));
182487a7269eSachartre 		return (0);
182587a7269eSachartre 
182687a7269eSachartre 	case DKIOCSVTOC:
182787a7269eSachartre 		ASSERT(ioctl_arg != NULL);
182887a7269eSachartre 		ASSERT(vd->dk_geom.dkg_nhead != 0 &&
182987a7269eSachartre 		    vd->dk_geom.dkg_nsect != 0);
1830690555a1Sachartre 		vtoc = (struct vtoc *)ioctl_arg;
1831690555a1Sachartre 
1832690555a1Sachartre 		if (vtoc->v_sanity != VTOC_SANE ||
1833690555a1Sachartre 		    vtoc->v_sectorsz != DEV_BSIZE ||
1834690555a1Sachartre 		    vtoc->v_nparts != V_NUMPAR)
1835690555a1Sachartre 			return (EINVAL);
1836690555a1Sachartre 
1837690555a1Sachartre 		bzero(&label, sizeof (label));
1838690555a1Sachartre 		label.dkl_ncyl = vd->dk_geom.dkg_ncyl;
1839690555a1Sachartre 		label.dkl_acyl = vd->dk_geom.dkg_acyl;
1840690555a1Sachartre 		label.dkl_pcyl = vd->dk_geom.dkg_pcyl;
1841690555a1Sachartre 		label.dkl_nhead = vd->dk_geom.dkg_nhead;
1842690555a1Sachartre 		label.dkl_nsect = vd->dk_geom.dkg_nsect;
1843690555a1Sachartre 		label.dkl_intrlv = vd->dk_geom.dkg_intrlv;
1844690555a1Sachartre 		label.dkl_apc = vd->dk_geom.dkg_apc;
1845690555a1Sachartre 		label.dkl_rpm = vd->dk_geom.dkg_rpm;
184687a7269eSachartre 		label.dkl_write_reinstruct = vd->dk_geom.dkg_write_reinstruct;
184787a7269eSachartre 		label.dkl_read_reinstruct = vd->dk_geom.dkg_read_reinstruct;
1848690555a1Sachartre 
184987a7269eSachartre 		label.dkl_vtoc.v_nparts = V_NUMPAR;
185087a7269eSachartre 		label.dkl_vtoc.v_sanity = VTOC_SANE;
1851690555a1Sachartre 		label.dkl_vtoc.v_version = vtoc->v_version;
185287a7269eSachartre 		for (i = 0; i < V_NUMPAR; i++) {
1853690555a1Sachartre 			label.dkl_vtoc.v_timestamp[i] =
1854690555a1Sachartre 			    vtoc->timestamp[i];
1855690555a1Sachartre 			label.dkl_vtoc.v_part[i].p_tag =
1856690555a1Sachartre 			    vtoc->v_part[i].p_tag;
1857690555a1Sachartre 			label.dkl_vtoc.v_part[i].p_flag =
1858690555a1Sachartre 			    vtoc->v_part[i].p_flag;
1859690555a1Sachartre 			label.dkl_map[i].dkl_cylno =
1860690555a1Sachartre 			    vtoc->v_part[i].p_start /
1861690555a1Sachartre 			    (label.dkl_nhead * label.dkl_nsect);
1862690555a1Sachartre 			label.dkl_map[i].dkl_nblk =
1863690555a1Sachartre 			    vtoc->v_part[i].p_size;
18643c96341aSnarayan 		}
186587a7269eSachartre 		/*
186687a7269eSachartre 		 * The bootinfo array can not be copied with bcopy() because
186787a7269eSachartre 		 * elements are of type long in vtoc (so 64-bit) and of type
186887a7269eSachartre 		 * int in dk_vtoc (so 32-bit).
186987a7269eSachartre 		 */
187087a7269eSachartre 		label.dkl_vtoc.v_bootinfo[0] = vtoc->v_bootinfo[0];
187187a7269eSachartre 		label.dkl_vtoc.v_bootinfo[1] = vtoc->v_bootinfo[1];
187287a7269eSachartre 		label.dkl_vtoc.v_bootinfo[2] = vtoc->v_bootinfo[2];
1873690555a1Sachartre 		bcopy(vtoc->v_asciilabel, label.dkl_asciilabel,
1874690555a1Sachartre 		    LEN_DKL_ASCII);
1875690555a1Sachartre 		bcopy(vtoc->v_volume, label.dkl_vtoc.v_volume,
1876690555a1Sachartre 		    LEN_DKL_VVOL);
18773c96341aSnarayan 
18783c96341aSnarayan 		/* re-compute checksum */
1879690555a1Sachartre 		label.dkl_magic = DKL_MAGIC;
1880690555a1Sachartre 		label.dkl_cksum = vd_lbl2cksum(&label);
1881690555a1Sachartre 
188287a7269eSachartre 		/* write label to the disk image */
188387a7269eSachartre 		if ((rc = vd_file_set_vtoc(vd, &label)) != 0)
188487a7269eSachartre 			return (rc);
1885690555a1Sachartre 
1886*78fcd0a1Sachartre 		/* check the geometry and update the driver info */
1887*78fcd0a1Sachartre 		if ((rc = vd_file_validate_geometry(vd)) != 0)
1888*78fcd0a1Sachartre 			return (rc);
18893c96341aSnarayan 
189087a7269eSachartre 		/*
189187a7269eSachartre 		 * The disk geometry may have changed, so we need to write
189287a7269eSachartre 		 * the devid (if there is one) so that it is stored at the
189387a7269eSachartre 		 * right location.
189487a7269eSachartre 		 */
189587a7269eSachartre 		if (vd->file_devid != NULL &&
189687a7269eSachartre 		    vd_file_write_devid(vd, vd->file_devid) != 0) {
189787a7269eSachartre 			PR0("Fail to write devid");
18981ae08745Sheppo 		}
18994bac2208Snarayan 
19004bac2208Snarayan 		return (0);
19014bac2208Snarayan 
19024bac2208Snarayan 	default:
19034bac2208Snarayan 		return (ENOTSUP);
19044bac2208Snarayan 	}
19051ae08745Sheppo }
19061ae08745Sheppo 
1907205eeb1aSlm66018 /*
1908205eeb1aSlm66018  * Description:
1909205eeb1aSlm66018  *	This is the function that processes the ioctl requests (farming it
1910205eeb1aSlm66018  *	out to functions that handle slices, files or whole disks)
1911205eeb1aSlm66018  *
1912205eeb1aSlm66018  * Return Values
1913205eeb1aSlm66018  *     0		- ioctl operation completed successfully
1914205eeb1aSlm66018  *     != 0		- The LDC error value encountered
1915205eeb1aSlm66018  *			  (propagated back up the call stack as a task error)
1916205eeb1aSlm66018  *
1917205eeb1aSlm66018  * Side Effect
1918205eeb1aSlm66018  *     sets request->status to the return value of the ioctl function.
1919205eeb1aSlm66018  */
19201ae08745Sheppo static int
19210a55fbb7Slm66018 vd_do_ioctl(vd_t *vd, vd_dring_payload_t *request, void* buf, vd_ioctl_t *ioctl)
19221ae08745Sheppo {
1923205eeb1aSlm66018 	int	rval = 0, status = 0;
19241ae08745Sheppo 	size_t	nbytes = request->nbytes;	/* modifiable copy */
19251ae08745Sheppo 
19261ae08745Sheppo 
19271ae08745Sheppo 	ASSERT(request->slice < vd->nslices);
19281ae08745Sheppo 	PR0("Performing %s", ioctl->operation_name);
19291ae08745Sheppo 
19300a55fbb7Slm66018 	/* Get data from client and convert, if necessary */
19310a55fbb7Slm66018 	if (ioctl->copyin != NULL)  {
19321ae08745Sheppo 		ASSERT(nbytes != 0 && buf != NULL);
19331ae08745Sheppo 		PR1("Getting \"arg\" data from client");
19341ae08745Sheppo 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
19351ae08745Sheppo 		    request->cookie, request->ncookies,
19361ae08745Sheppo 		    LDC_COPY_IN)) != 0) {
19373af08d82Slm66018 			PR0("ldc_mem_copy() returned errno %d "
19381ae08745Sheppo 			    "copying from client", status);
19391ae08745Sheppo 			return (status);
19401ae08745Sheppo 		}
19410a55fbb7Slm66018 
19420a55fbb7Slm66018 		/* Convert client's data, if necessary */
19430a55fbb7Slm66018 		if (ioctl->copyin == VD_IDENTITY)	/* use client buffer */
19440a55fbb7Slm66018 			ioctl->arg = buf;
19450a55fbb7Slm66018 		else	/* convert client vdisk operation data to ioctl data */
19460a55fbb7Slm66018 			(ioctl->copyin)(buf, (void *)ioctl->arg);
19471ae08745Sheppo 	}
19481ae08745Sheppo 
19491ae08745Sheppo 	/*
19501ae08745Sheppo 	 * Handle single-slice block devices internally; otherwise, have the
19511ae08745Sheppo 	 * real driver perform the ioctl()
19521ae08745Sheppo 	 */
195387a7269eSachartre 	if (vd->file) {
1954205eeb1aSlm66018 		request->status =
1955205eeb1aSlm66018 		    vd_do_file_ioctl(vd, ioctl->cmd, (void *)ioctl->arg);
1956205eeb1aSlm66018 
195787a7269eSachartre 	} else if (vd->vdisk_type == VD_DISK_TYPE_SLICE && !vd->pseudo) {
1958205eeb1aSlm66018 		request->status =
1959205eeb1aSlm66018 		    vd_do_slice_ioctl(vd, ioctl->cmd, (void *)ioctl->arg);
1960205eeb1aSlm66018 
1961205eeb1aSlm66018 	} else {
1962205eeb1aSlm66018 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
1963d10e4ef2Snarayan 		    ioctl->cmd, (intptr_t)ioctl->arg, (vd_open_flags | FKIOCTL),
1964205eeb1aSlm66018 		    kcred, &rval);
1965205eeb1aSlm66018 
19661ae08745Sheppo #ifdef DEBUG
19671ae08745Sheppo 		if (rval != 0) {
1968205eeb1aSlm66018 			PR0("%s set rval = %d, which is not being returned to"
1969205eeb1aSlm66018 			    " client", ioctl->cmd_name, rval);
19701ae08745Sheppo 		}
19711ae08745Sheppo #endif /* DEBUG */
1972205eeb1aSlm66018 	}
1973205eeb1aSlm66018 
1974205eeb1aSlm66018 	if (request->status != 0) {
1975205eeb1aSlm66018 		PR0("ioctl(%s) = errno %d", ioctl->cmd_name, request->status);
1976205eeb1aSlm66018 		return (0);
1977205eeb1aSlm66018 	}
19781ae08745Sheppo 
19790a55fbb7Slm66018 	/* Convert data and send to client, if necessary */
19800a55fbb7Slm66018 	if (ioctl->copyout != NULL)  {
19811ae08745Sheppo 		ASSERT(nbytes != 0 && buf != NULL);
19821ae08745Sheppo 		PR1("Sending \"arg\" data to client");
19830a55fbb7Slm66018 
19840a55fbb7Slm66018 		/* Convert ioctl data to vdisk operation data, if necessary */
19850a55fbb7Slm66018 		if (ioctl->copyout != VD_IDENTITY)
19860a55fbb7Slm66018 			(ioctl->copyout)((void *)ioctl->arg, buf);
19870a55fbb7Slm66018 
19881ae08745Sheppo 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
19891ae08745Sheppo 		    request->cookie, request->ncookies,
19901ae08745Sheppo 		    LDC_COPY_OUT)) != 0) {
19913af08d82Slm66018 			PR0("ldc_mem_copy() returned errno %d "
19921ae08745Sheppo 			    "copying to client", status);
19931ae08745Sheppo 			return (status);
19941ae08745Sheppo 		}
19951ae08745Sheppo 	}
19961ae08745Sheppo 
19971ae08745Sheppo 	return (status);
19981ae08745Sheppo }
19991ae08745Sheppo 
20001ae08745Sheppo #define	RNDSIZE(expr) P2ROUNDUP(sizeof (expr), sizeof (uint64_t))
2001205eeb1aSlm66018 
2002205eeb1aSlm66018 /*
2003205eeb1aSlm66018  * Description:
2004205eeb1aSlm66018  *	This generic function is called by the task queue to complete
2005205eeb1aSlm66018  *	the processing of the tasks. The specific completion function
2006205eeb1aSlm66018  *	is passed in as a field in the task pointer.
2007205eeb1aSlm66018  *
2008205eeb1aSlm66018  * Parameters:
2009205eeb1aSlm66018  *	arg 	- opaque pointer to structure containing task to be completed
2010205eeb1aSlm66018  *
2011205eeb1aSlm66018  * Return Values
2012205eeb1aSlm66018  *	None
2013205eeb1aSlm66018  */
2014205eeb1aSlm66018 static void
2015205eeb1aSlm66018 vd_complete(void *arg)
2016205eeb1aSlm66018 {
2017205eeb1aSlm66018 	vd_task_t	*task = (vd_task_t *)arg;
2018205eeb1aSlm66018 
2019205eeb1aSlm66018 	ASSERT(task != NULL);
2020205eeb1aSlm66018 	ASSERT(task->status == EINPROGRESS);
2021205eeb1aSlm66018 	ASSERT(task->completef != NULL);
2022205eeb1aSlm66018 
2023205eeb1aSlm66018 	task->status = task->completef(task);
2024205eeb1aSlm66018 	if (task->status)
2025205eeb1aSlm66018 		PR0("%s: Error %d completing task", __func__, task->status);
2026205eeb1aSlm66018 
2027205eeb1aSlm66018 	/* Now notify the vDisk client */
2028205eeb1aSlm66018 	vd_complete_notify(task);
2029205eeb1aSlm66018 }
2030205eeb1aSlm66018 
20311ae08745Sheppo static int
2032d10e4ef2Snarayan vd_ioctl(vd_task_t *task)
20331ae08745Sheppo {
203487a7269eSachartre 	int			i, status;
20351ae08745Sheppo 	void			*buf = NULL;
20360a55fbb7Slm66018 	struct dk_geom		dk_geom = {0};
20370a55fbb7Slm66018 	struct vtoc		vtoc = {0};
20384bac2208Snarayan 	struct dk_efi		dk_efi = {0};
2039d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
2040d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
20410a55fbb7Slm66018 	vd_ioctl_t		ioctl[] = {
20420a55fbb7Slm66018 		/* Command (no-copy) operations */
20430a55fbb7Slm66018 		{VD_OP_FLUSH, STRINGIZE(VD_OP_FLUSH), 0,
20440a55fbb7Slm66018 		    DKIOCFLUSHWRITECACHE, STRINGIZE(DKIOCFLUSHWRITECACHE),
20450a55fbb7Slm66018 		    NULL, NULL, NULL},
20460a55fbb7Slm66018 
20470a55fbb7Slm66018 		/* "Get" (copy-out) operations */
20480a55fbb7Slm66018 		{VD_OP_GET_WCE, STRINGIZE(VD_OP_GET_WCE), RNDSIZE(int),
20490a55fbb7Slm66018 		    DKIOCGETWCE, STRINGIZE(DKIOCGETWCE),
20504bac2208Snarayan 		    NULL, VD_IDENTITY, VD_IDENTITY},
20510a55fbb7Slm66018 		{VD_OP_GET_DISKGEOM, STRINGIZE(VD_OP_GET_DISKGEOM),
20520a55fbb7Slm66018 		    RNDSIZE(vd_geom_t),
20530a55fbb7Slm66018 		    DKIOCGGEOM, STRINGIZE(DKIOCGGEOM),
20540a55fbb7Slm66018 		    &dk_geom, NULL, dk_geom2vd_geom},
20550a55fbb7Slm66018 		{VD_OP_GET_VTOC, STRINGIZE(VD_OP_GET_VTOC), RNDSIZE(vd_vtoc_t),
20560a55fbb7Slm66018 		    DKIOCGVTOC, STRINGIZE(DKIOCGVTOC),
20570a55fbb7Slm66018 		    &vtoc, NULL, vtoc2vd_vtoc},
20584bac2208Snarayan 		{VD_OP_GET_EFI, STRINGIZE(VD_OP_GET_EFI), RNDSIZE(vd_efi_t),
20594bac2208Snarayan 		    DKIOCGETEFI, STRINGIZE(DKIOCGETEFI),
20604bac2208Snarayan 		    &dk_efi, vd_get_efi_in, vd_get_efi_out},
20610a55fbb7Slm66018 
20620a55fbb7Slm66018 		/* "Set" (copy-in) operations */
20630a55fbb7Slm66018 		{VD_OP_SET_WCE, STRINGIZE(VD_OP_SET_WCE), RNDSIZE(int),
20640a55fbb7Slm66018 		    DKIOCSETWCE, STRINGIZE(DKIOCSETWCE),
20654bac2208Snarayan 		    NULL, VD_IDENTITY, VD_IDENTITY},
20660a55fbb7Slm66018 		{VD_OP_SET_DISKGEOM, STRINGIZE(VD_OP_SET_DISKGEOM),
20670a55fbb7Slm66018 		    RNDSIZE(vd_geom_t),
20680a55fbb7Slm66018 		    DKIOCSGEOM, STRINGIZE(DKIOCSGEOM),
20690a55fbb7Slm66018 		    &dk_geom, vd_geom2dk_geom, NULL},
20700a55fbb7Slm66018 		{VD_OP_SET_VTOC, STRINGIZE(VD_OP_SET_VTOC), RNDSIZE(vd_vtoc_t),
20710a55fbb7Slm66018 		    DKIOCSVTOC, STRINGIZE(DKIOCSVTOC),
20720a55fbb7Slm66018 		    &vtoc, vd_vtoc2vtoc, NULL},
20734bac2208Snarayan 		{VD_OP_SET_EFI, STRINGIZE(VD_OP_SET_EFI), RNDSIZE(vd_efi_t),
20744bac2208Snarayan 		    DKIOCSETEFI, STRINGIZE(DKIOCSETEFI),
20754bac2208Snarayan 		    &dk_efi, vd_set_efi_in, vd_set_efi_out},
20760a55fbb7Slm66018 	};
20771ae08745Sheppo 	size_t		nioctls = (sizeof (ioctl))/(sizeof (ioctl[0]));
20781ae08745Sheppo 
20791ae08745Sheppo 
2080d10e4ef2Snarayan 	ASSERT(vd != NULL);
2081d10e4ef2Snarayan 	ASSERT(request != NULL);
20821ae08745Sheppo 	ASSERT(request->slice < vd->nslices);
20831ae08745Sheppo 
20841ae08745Sheppo 	/*
20851ae08745Sheppo 	 * Determine ioctl corresponding to caller's "operation" and
20861ae08745Sheppo 	 * validate caller's "nbytes"
20871ae08745Sheppo 	 */
20881ae08745Sheppo 	for (i = 0; i < nioctls; i++) {
20891ae08745Sheppo 		if (request->operation == ioctl[i].operation) {
20900a55fbb7Slm66018 			/* LDC memory operations require 8-byte multiples */
20910a55fbb7Slm66018 			ASSERT(ioctl[i].nbytes % sizeof (uint64_t) == 0);
20920a55fbb7Slm66018 
20934bac2208Snarayan 			if (request->operation == VD_OP_GET_EFI ||
20944bac2208Snarayan 			    request->operation == VD_OP_SET_EFI) {
20954bac2208Snarayan 				if (request->nbytes >= ioctl[i].nbytes)
20964bac2208Snarayan 					break;
20973af08d82Slm66018 				PR0("%s:  Expected at least nbytes = %lu, "
20984bac2208Snarayan 				    "got %lu", ioctl[i].operation_name,
20994bac2208Snarayan 				    ioctl[i].nbytes, request->nbytes);
21004bac2208Snarayan 				return (EINVAL);
21014bac2208Snarayan 			}
21024bac2208Snarayan 
21030a55fbb7Slm66018 			if (request->nbytes != ioctl[i].nbytes) {
21043af08d82Slm66018 				PR0("%s:  Expected nbytes = %lu, got %lu",
21050a55fbb7Slm66018 				    ioctl[i].operation_name, ioctl[i].nbytes,
21060a55fbb7Slm66018 				    request->nbytes);
21071ae08745Sheppo 				return (EINVAL);
21081ae08745Sheppo 			}
21091ae08745Sheppo 
21101ae08745Sheppo 			break;
21111ae08745Sheppo 		}
21121ae08745Sheppo 	}
21131ae08745Sheppo 	ASSERT(i < nioctls);	/* because "operation" already validated */
21141ae08745Sheppo 
21151ae08745Sheppo 	if (request->nbytes)
21161ae08745Sheppo 		buf = kmem_zalloc(request->nbytes, KM_SLEEP);
21171ae08745Sheppo 	status = vd_do_ioctl(vd, request, buf, &ioctl[i]);
21181ae08745Sheppo 	if (request->nbytes)
21191ae08745Sheppo 		kmem_free(buf, request->nbytes);
212087a7269eSachartre 
21211ae08745Sheppo 	return (status);
21221ae08745Sheppo }
21231ae08745Sheppo 
21244bac2208Snarayan static int
21254bac2208Snarayan vd_get_devid(vd_task_t *task)
21264bac2208Snarayan {
21274bac2208Snarayan 	vd_t *vd = task->vd;
21284bac2208Snarayan 	vd_dring_payload_t *request = task->request;
21294bac2208Snarayan 	vd_devid_t *vd_devid;
21304bac2208Snarayan 	impl_devid_t *devid;
213187a7269eSachartre 	int status, bufid_len, devid_len, len, sz;
21323af08d82Slm66018 	int bufbytes;
21334bac2208Snarayan 
21343af08d82Slm66018 	PR1("Get Device ID, nbytes=%ld", request->nbytes);
21354bac2208Snarayan 
21363c96341aSnarayan 	if (vd->file) {
213787a7269eSachartre 		if (vd->file_devid == NULL) {
21383af08d82Slm66018 			PR2("No Device ID");
2139205eeb1aSlm66018 			request->status = ENOENT;
2140205eeb1aSlm66018 			return (0);
214187a7269eSachartre 		} else {
214287a7269eSachartre 			sz = ddi_devid_sizeof(vd->file_devid);
214387a7269eSachartre 			devid = kmem_alloc(sz, KM_SLEEP);
214487a7269eSachartre 			bcopy(vd->file_devid, devid, sz);
214587a7269eSachartre 		}
214687a7269eSachartre 	} else {
214787a7269eSachartre 		if (ddi_lyr_get_devid(vd->dev[request->slice],
214887a7269eSachartre 		    (ddi_devid_t *)&devid) != DDI_SUCCESS) {
214987a7269eSachartre 			PR2("No Device ID");
2150205eeb1aSlm66018 			request->status = ENOENT;
2151205eeb1aSlm66018 			return (0);
215287a7269eSachartre 		}
21534bac2208Snarayan 	}
21544bac2208Snarayan 
21554bac2208Snarayan 	bufid_len = request->nbytes - sizeof (vd_devid_t) + 1;
21564bac2208Snarayan 	devid_len = DEVID_GETLEN(devid);
21574bac2208Snarayan 
21583af08d82Slm66018 	/*
21593af08d82Slm66018 	 * Save the buffer size here for use in deallocation.
21603af08d82Slm66018 	 * The actual number of bytes copied is returned in
21613af08d82Slm66018 	 * the 'nbytes' field of the request structure.
21623af08d82Slm66018 	 */
21633af08d82Slm66018 	bufbytes = request->nbytes;
21643af08d82Slm66018 
21653af08d82Slm66018 	vd_devid = kmem_zalloc(bufbytes, KM_SLEEP);
21664bac2208Snarayan 	vd_devid->length = devid_len;
21674bac2208Snarayan 	vd_devid->type = DEVID_GETTYPE(devid);
21684bac2208Snarayan 
21694bac2208Snarayan 	len = (devid_len > bufid_len)? bufid_len : devid_len;
21704bac2208Snarayan 
21714bac2208Snarayan 	bcopy(devid->did_id, vd_devid->id, len);
21724bac2208Snarayan 
2173*78fcd0a1Sachartre 	request->status = 0;
2174*78fcd0a1Sachartre 
21754bac2208Snarayan 	/* LDC memory operations require 8-byte multiples */
21764bac2208Snarayan 	ASSERT(request->nbytes % sizeof (uint64_t) == 0);
21774bac2208Snarayan 
21784bac2208Snarayan 	if ((status = ldc_mem_copy(vd->ldc_handle, (caddr_t)vd_devid, 0,
21794bac2208Snarayan 	    &request->nbytes, request->cookie, request->ncookies,
21804bac2208Snarayan 	    LDC_COPY_OUT)) != 0) {
21813af08d82Slm66018 		PR0("ldc_mem_copy() returned errno %d copying to client",
21824bac2208Snarayan 		    status);
21834bac2208Snarayan 	}
21843af08d82Slm66018 	PR1("post mem_copy: nbytes=%ld", request->nbytes);
21854bac2208Snarayan 
21863af08d82Slm66018 	kmem_free(vd_devid, bufbytes);
21874bac2208Snarayan 	ddi_devid_free((ddi_devid_t)devid);
21884bac2208Snarayan 
21894bac2208Snarayan 	return (status);
21904bac2208Snarayan }
21914bac2208Snarayan 
21921ae08745Sheppo /*
21931ae08745Sheppo  * Define the supported operations once the functions for performing them have
21941ae08745Sheppo  * been defined
21951ae08745Sheppo  */
21961ae08745Sheppo static const vds_operation_t	vds_operation[] = {
21973af08d82Slm66018 #define	X(_s)	#_s, _s
21983af08d82Slm66018 	{X(VD_OP_BREAD),	vd_start_bio,	vd_complete_bio},
21993af08d82Slm66018 	{X(VD_OP_BWRITE),	vd_start_bio,	vd_complete_bio},
22003af08d82Slm66018 	{X(VD_OP_FLUSH),	vd_ioctl,	NULL},
22013af08d82Slm66018 	{X(VD_OP_GET_WCE),	vd_ioctl,	NULL},
22023af08d82Slm66018 	{X(VD_OP_SET_WCE),	vd_ioctl,	NULL},
22033af08d82Slm66018 	{X(VD_OP_GET_VTOC),	vd_ioctl,	NULL},
22043af08d82Slm66018 	{X(VD_OP_SET_VTOC),	vd_ioctl,	NULL},
22053af08d82Slm66018 	{X(VD_OP_GET_DISKGEOM),	vd_ioctl,	NULL},
22063af08d82Slm66018 	{X(VD_OP_SET_DISKGEOM),	vd_ioctl,	NULL},
22073af08d82Slm66018 	{X(VD_OP_GET_EFI),	vd_ioctl,	NULL},
22083af08d82Slm66018 	{X(VD_OP_SET_EFI),	vd_ioctl,	NULL},
22093af08d82Slm66018 	{X(VD_OP_GET_DEVID),	vd_get_devid,	NULL},
22103af08d82Slm66018 #undef	X
22111ae08745Sheppo };
22121ae08745Sheppo 
22131ae08745Sheppo static const size_t	vds_noperations =
22141ae08745Sheppo 	(sizeof (vds_operation))/(sizeof (vds_operation[0]));
22151ae08745Sheppo 
22161ae08745Sheppo /*
2217d10e4ef2Snarayan  * Process a task specifying a client I/O request
2218205eeb1aSlm66018  *
2219205eeb1aSlm66018  * Parameters:
2220205eeb1aSlm66018  *	task 		- structure containing the request sent from client
2221205eeb1aSlm66018  *
2222205eeb1aSlm66018  * Return Value
2223205eeb1aSlm66018  *	0	- success
2224205eeb1aSlm66018  *	ENOTSUP	- Unknown/Unsupported VD_OP_XXX operation
2225205eeb1aSlm66018  *	EINVAL	- Invalid disk slice
2226205eeb1aSlm66018  *	!= 0	- some other non-zero return value from start function
22271ae08745Sheppo  */
22281ae08745Sheppo static int
2229205eeb1aSlm66018 vd_do_process_task(vd_task_t *task)
22301ae08745Sheppo {
2231205eeb1aSlm66018 	int			i;
2232d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
2233d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
22341ae08745Sheppo 
2235d10e4ef2Snarayan 	ASSERT(vd != NULL);
2236d10e4ef2Snarayan 	ASSERT(request != NULL);
22371ae08745Sheppo 
2238d10e4ef2Snarayan 	/* Find the requested operation */
2239205eeb1aSlm66018 	for (i = 0; i < vds_noperations; i++) {
2240205eeb1aSlm66018 		if (request->operation == vds_operation[i].operation) {
2241205eeb1aSlm66018 			/* all operations should have a start func */
2242205eeb1aSlm66018 			ASSERT(vds_operation[i].start != NULL);
2243205eeb1aSlm66018 
2244205eeb1aSlm66018 			task->completef = vds_operation[i].complete;
2245d10e4ef2Snarayan 			break;
2246205eeb1aSlm66018 		}
2247205eeb1aSlm66018 	}
2248d10e4ef2Snarayan 	if (i == vds_noperations) {
22493af08d82Slm66018 		PR0("Unsupported operation %u", request->operation);
22501ae08745Sheppo 		return (ENOTSUP);
22511ae08745Sheppo 	}
22521ae08745Sheppo 
22537636cb21Slm66018 	/* Range-check slice */
225487a7269eSachartre 	if (request->slice >= vd->nslices &&
225587a7269eSachartre 	    (vd->vdisk_type != VD_DISK_TYPE_DISK ||
225687a7269eSachartre 	    request->slice != VD_SLICE_NONE)) {
22573af08d82Slm66018 		PR0("Invalid \"slice\" %u (max %u) for virtual disk",
22587636cb21Slm66018 		    request->slice, (vd->nslices - 1));
22597636cb21Slm66018 		return (EINVAL);
22607636cb21Slm66018 	}
22617636cb21Slm66018 
2262205eeb1aSlm66018 	/*
2263205eeb1aSlm66018 	 * Call the function pointer that starts the operation.
2264205eeb1aSlm66018 	 */
2265205eeb1aSlm66018 	return (vds_operation[i].start(task));
22661ae08745Sheppo }
22671ae08745Sheppo 
2268205eeb1aSlm66018 /*
2269205eeb1aSlm66018  * Description:
2270205eeb1aSlm66018  *	This function is called by both the in-band and descriptor ring
2271205eeb1aSlm66018  *	message processing functions paths to actually execute the task
2272205eeb1aSlm66018  *	requested by the vDisk client. It in turn calls its worker
2273205eeb1aSlm66018  *	function, vd_do_process_task(), to carry our the request.
2274205eeb1aSlm66018  *
2275205eeb1aSlm66018  *	Any transport errors (e.g. LDC errors, vDisk protocol errors) are
2276205eeb1aSlm66018  *	saved in the 'status' field of the task and are propagated back
2277205eeb1aSlm66018  *	up the call stack to trigger a NACK
2278205eeb1aSlm66018  *
2279205eeb1aSlm66018  *	Any request errors (e.g. ENOTTY from an ioctl) are saved in
2280205eeb1aSlm66018  *	the 'status' field of the request and result in an ACK being sent
2281205eeb1aSlm66018  *	by the completion handler.
2282205eeb1aSlm66018  *
2283205eeb1aSlm66018  * Parameters:
2284205eeb1aSlm66018  *	task 		- structure containing the request sent from client
2285205eeb1aSlm66018  *
2286205eeb1aSlm66018  * Return Value
2287205eeb1aSlm66018  *	0		- successful synchronous request.
2288205eeb1aSlm66018  *	!= 0		- transport error (e.g. LDC errors, vDisk protocol)
2289205eeb1aSlm66018  *	EINPROGRESS	- task will be finished in a completion handler
2290205eeb1aSlm66018  */
2291205eeb1aSlm66018 static int
2292205eeb1aSlm66018 vd_process_task(vd_task_t *task)
2293205eeb1aSlm66018 {
2294205eeb1aSlm66018 	vd_t	*vd = task->vd;
2295205eeb1aSlm66018 	int	status;
22961ae08745Sheppo 
2297205eeb1aSlm66018 	DTRACE_PROBE1(task__start, vd_task_t *, task);
22983af08d82Slm66018 
2299205eeb1aSlm66018 	task->status =  vd_do_process_task(task);
2300205eeb1aSlm66018 
2301205eeb1aSlm66018 	/*
2302205eeb1aSlm66018 	 * If the task processing function returned EINPROGRESS indicating
2303205eeb1aSlm66018 	 * that the task needs completing then schedule a taskq entry to
2304205eeb1aSlm66018 	 * finish it now.
2305205eeb1aSlm66018 	 *
2306205eeb1aSlm66018 	 * Otherwise the task processing function returned either zero
2307205eeb1aSlm66018 	 * indicating that the task was finished in the start function (and we
2308205eeb1aSlm66018 	 * don't need to wait in a completion function) or the start function
2309205eeb1aSlm66018 	 * returned an error - in both cases all that needs to happen is the
2310205eeb1aSlm66018 	 * notification to the vDisk client higher up the call stack.
2311205eeb1aSlm66018 	 * If the task was using a Descriptor Ring, we need to mark it as done
2312205eeb1aSlm66018 	 * at this stage.
2313205eeb1aSlm66018 	 */
2314205eeb1aSlm66018 	if (task->status == EINPROGRESS) {
2315d10e4ef2Snarayan 		/* Queue a task to complete the operation */
2316205eeb1aSlm66018 		(void) ddi_taskq_dispatch(vd->completionq, vd_complete,
2317d10e4ef2Snarayan 		    task, DDI_SLEEP);
2318d10e4ef2Snarayan 
2319205eeb1aSlm66018 	} else if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE)) {
2320205eeb1aSlm66018 		/* Update the dring element if it's a dring client */
2321205eeb1aSlm66018 		status = vd_mark_elem_done(vd, task->index,
2322205eeb1aSlm66018 		    task->request->status, task->request->nbytes);
2323205eeb1aSlm66018 		if (status == ECONNRESET)
2324205eeb1aSlm66018 			vd_mark_in_reset(vd);
2325205eeb1aSlm66018 	}
2326205eeb1aSlm66018 
2327205eeb1aSlm66018 	return (task->status);
23281ae08745Sheppo }
23291ae08745Sheppo 
23301ae08745Sheppo /*
23310a55fbb7Slm66018  * Return true if the "type", "subtype", and "env" fields of the "tag" first
23320a55fbb7Slm66018  * argument match the corresponding remaining arguments; otherwise, return false
23331ae08745Sheppo  */
23340a55fbb7Slm66018 boolean_t
23351ae08745Sheppo vd_msgtype(vio_msg_tag_t *tag, int type, int subtype, int env)
23361ae08745Sheppo {
23371ae08745Sheppo 	return ((tag->vio_msgtype == type) &&
23381ae08745Sheppo 	    (tag->vio_subtype == subtype) &&
23390a55fbb7Slm66018 	    (tag->vio_subtype_env == env)) ? B_TRUE : B_FALSE;
23401ae08745Sheppo }
23411ae08745Sheppo 
23420a55fbb7Slm66018 /*
23430a55fbb7Slm66018  * Check whether the major/minor version specified in "ver_msg" is supported
23440a55fbb7Slm66018  * by this server.
23450a55fbb7Slm66018  */
23460a55fbb7Slm66018 static boolean_t
23470a55fbb7Slm66018 vds_supported_version(vio_ver_msg_t *ver_msg)
23480a55fbb7Slm66018 {
23490a55fbb7Slm66018 	for (int i = 0; i < vds_num_versions; i++) {
23500a55fbb7Slm66018 		ASSERT(vds_version[i].major > 0);
23510a55fbb7Slm66018 		ASSERT((i == 0) ||
23520a55fbb7Slm66018 		    (vds_version[i].major < vds_version[i-1].major));
23530a55fbb7Slm66018 
23540a55fbb7Slm66018 		/*
23550a55fbb7Slm66018 		 * If the major versions match, adjust the minor version, if
23560a55fbb7Slm66018 		 * necessary, down to the highest value supported by this
23570a55fbb7Slm66018 		 * server and return true so this message will get "ack"ed;
23580a55fbb7Slm66018 		 * the client should also support all minor versions lower
23590a55fbb7Slm66018 		 * than the value it sent
23600a55fbb7Slm66018 		 */
23610a55fbb7Slm66018 		if (ver_msg->ver_major == vds_version[i].major) {
23620a55fbb7Slm66018 			if (ver_msg->ver_minor > vds_version[i].minor) {
23630a55fbb7Slm66018 				PR0("Adjusting minor version from %u to %u",
23640a55fbb7Slm66018 				    ver_msg->ver_minor, vds_version[i].minor);
23650a55fbb7Slm66018 				ver_msg->ver_minor = vds_version[i].minor;
23660a55fbb7Slm66018 			}
23670a55fbb7Slm66018 			return (B_TRUE);
23680a55fbb7Slm66018 		}
23690a55fbb7Slm66018 
23700a55fbb7Slm66018 		/*
23710a55fbb7Slm66018 		 * If the message contains a higher major version number, set
23720a55fbb7Slm66018 		 * the message's major/minor versions to the current values
23730a55fbb7Slm66018 		 * and return false, so this message will get "nack"ed with
23740a55fbb7Slm66018 		 * these values, and the client will potentially try again
23750a55fbb7Slm66018 		 * with the same or a lower version
23760a55fbb7Slm66018 		 */
23770a55fbb7Slm66018 		if (ver_msg->ver_major > vds_version[i].major) {
23780a55fbb7Slm66018 			ver_msg->ver_major = vds_version[i].major;
23790a55fbb7Slm66018 			ver_msg->ver_minor = vds_version[i].minor;
23800a55fbb7Slm66018 			return (B_FALSE);
23810a55fbb7Slm66018 		}
23820a55fbb7Slm66018 
23830a55fbb7Slm66018 		/*
23840a55fbb7Slm66018 		 * Otherwise, the message's major version is less than the
23850a55fbb7Slm66018 		 * current major version, so continue the loop to the next
23860a55fbb7Slm66018 		 * (lower) supported version
23870a55fbb7Slm66018 		 */
23880a55fbb7Slm66018 	}
23890a55fbb7Slm66018 
23900a55fbb7Slm66018 	/*
23910a55fbb7Slm66018 	 * No common version was found; "ground" the version pair in the
23920a55fbb7Slm66018 	 * message to terminate negotiation
23930a55fbb7Slm66018 	 */
23940a55fbb7Slm66018 	ver_msg->ver_major = 0;
23950a55fbb7Slm66018 	ver_msg->ver_minor = 0;
23960a55fbb7Slm66018 	return (B_FALSE);
23970a55fbb7Slm66018 }
23980a55fbb7Slm66018 
23990a55fbb7Slm66018 /*
24000a55fbb7Slm66018  * Process a version message from a client.  vds expects to receive version
24010a55fbb7Slm66018  * messages from clients seeking service, but never issues version messages
24020a55fbb7Slm66018  * itself; therefore, vds can ACK or NACK client version messages, but does
24030a55fbb7Slm66018  * not expect to receive version-message ACKs or NACKs (and will treat such
24040a55fbb7Slm66018  * messages as invalid).
24050a55fbb7Slm66018  */
24061ae08745Sheppo static int
24070a55fbb7Slm66018 vd_process_ver_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
24081ae08745Sheppo {
24091ae08745Sheppo 	vio_ver_msg_t	*ver_msg = (vio_ver_msg_t *)msg;
24101ae08745Sheppo 
24111ae08745Sheppo 
24121ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
24131ae08745Sheppo 
24141ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
24151ae08745Sheppo 	    VIO_VER_INFO)) {
24161ae08745Sheppo 		return (ENOMSG);	/* not a version message */
24171ae08745Sheppo 	}
24181ae08745Sheppo 
24191ae08745Sheppo 	if (msglen != sizeof (*ver_msg)) {
24203af08d82Slm66018 		PR0("Expected %lu-byte version message; "
24211ae08745Sheppo 		    "received %lu bytes", sizeof (*ver_msg), msglen);
24221ae08745Sheppo 		return (EBADMSG);
24231ae08745Sheppo 	}
24241ae08745Sheppo 
24251ae08745Sheppo 	if (ver_msg->dev_class != VDEV_DISK) {
24263af08d82Slm66018 		PR0("Expected device class %u (disk); received %u",
24271ae08745Sheppo 		    VDEV_DISK, ver_msg->dev_class);
24281ae08745Sheppo 		return (EBADMSG);
24291ae08745Sheppo 	}
24301ae08745Sheppo 
24310a55fbb7Slm66018 	/*
24320a55fbb7Slm66018 	 * We're talking to the expected kind of client; set our device class
24330a55fbb7Slm66018 	 * for "ack/nack" back to the client
24340a55fbb7Slm66018 	 */
24351ae08745Sheppo 	ver_msg->dev_class = VDEV_DISK_SERVER;
24360a55fbb7Slm66018 
24370a55fbb7Slm66018 	/*
24380a55fbb7Slm66018 	 * Check whether the (valid) version message specifies a version
24390a55fbb7Slm66018 	 * supported by this server.  If the version is not supported, return
24400a55fbb7Slm66018 	 * EBADMSG so the message will get "nack"ed; vds_supported_version()
24410a55fbb7Slm66018 	 * will have updated the message with a supported version for the
24420a55fbb7Slm66018 	 * client to consider
24430a55fbb7Slm66018 	 */
24440a55fbb7Slm66018 	if (!vds_supported_version(ver_msg))
24450a55fbb7Slm66018 		return (EBADMSG);
24460a55fbb7Slm66018 
24470a55fbb7Slm66018 
24480a55fbb7Slm66018 	/*
24490a55fbb7Slm66018 	 * A version has been agreed upon; use the client's SID for
24500a55fbb7Slm66018 	 * communication on this channel now
24510a55fbb7Slm66018 	 */
24520a55fbb7Slm66018 	ASSERT(!(vd->initialized & VD_SID));
24530a55fbb7Slm66018 	vd->sid = ver_msg->tag.vio_sid;
24540a55fbb7Slm66018 	vd->initialized |= VD_SID;
24550a55fbb7Slm66018 
24560a55fbb7Slm66018 	/*
24570a55fbb7Slm66018 	 * When multiple versions are supported, this function should store
24580a55fbb7Slm66018 	 * the negotiated major and minor version values in the "vd" data
24590a55fbb7Slm66018 	 * structure to govern further communication; in particular, note that
24600a55fbb7Slm66018 	 * the client might have specified a lower minor version for the
24610a55fbb7Slm66018 	 * agreed major version than specifed in the vds_version[] array.  The
24620a55fbb7Slm66018 	 * following assertions should help remind future maintainers to make
24630a55fbb7Slm66018 	 * the appropriate changes to support multiple versions.
24640a55fbb7Slm66018 	 */
24650a55fbb7Slm66018 	ASSERT(vds_num_versions == 1);
24660a55fbb7Slm66018 	ASSERT(ver_msg->ver_major == vds_version[0].major);
24670a55fbb7Slm66018 	ASSERT(ver_msg->ver_minor == vds_version[0].minor);
24680a55fbb7Slm66018 
24690a55fbb7Slm66018 	PR0("Using major version %u, minor version %u",
24700a55fbb7Slm66018 	    ver_msg->ver_major, ver_msg->ver_minor);
24711ae08745Sheppo 	return (0);
24721ae08745Sheppo }
24731ae08745Sheppo 
24741ae08745Sheppo static int
24751ae08745Sheppo vd_process_attr_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
24761ae08745Sheppo {
24771ae08745Sheppo 	vd_attr_msg_t	*attr_msg = (vd_attr_msg_t *)msg;
24783c96341aSnarayan 	int		status, retry = 0;
24791ae08745Sheppo 
24801ae08745Sheppo 
24811ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
24821ae08745Sheppo 
24831ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
24841ae08745Sheppo 	    VIO_ATTR_INFO)) {
2485d10e4ef2Snarayan 		PR0("Message is not an attribute message");
2486d10e4ef2Snarayan 		return (ENOMSG);
24871ae08745Sheppo 	}
24881ae08745Sheppo 
24891ae08745Sheppo 	if (msglen != sizeof (*attr_msg)) {
24903af08d82Slm66018 		PR0("Expected %lu-byte attribute message; "
24911ae08745Sheppo 		    "received %lu bytes", sizeof (*attr_msg), msglen);
24921ae08745Sheppo 		return (EBADMSG);
24931ae08745Sheppo 	}
24941ae08745Sheppo 
24951ae08745Sheppo 	if (attr_msg->max_xfer_sz == 0) {
24963af08d82Slm66018 		PR0("Received maximum transfer size of 0 from client");
24971ae08745Sheppo 		return (EBADMSG);
24981ae08745Sheppo 	}
24991ae08745Sheppo 
25001ae08745Sheppo 	if ((attr_msg->xfer_mode != VIO_DESC_MODE) &&
25011ae08745Sheppo 	    (attr_msg->xfer_mode != VIO_DRING_MODE)) {
25023af08d82Slm66018 		PR0("Client requested unsupported transfer mode");
25031ae08745Sheppo 		return (EBADMSG);
25041ae08745Sheppo 	}
25051ae08745Sheppo 
25063c96341aSnarayan 	/*
25073c96341aSnarayan 	 * check if the underlying disk is ready, if not try accessing
25083c96341aSnarayan 	 * the device again. Open the vdisk device and extract info
25093c96341aSnarayan 	 * about it, as this is needed to respond to the attr info msg
25103c96341aSnarayan 	 */
25113c96341aSnarayan 	if ((vd->initialized & VD_DISK_READY) == 0) {
25123c96341aSnarayan 		PR0("Retry setting up disk (%s)", vd->device_path);
25133c96341aSnarayan 		do {
25143c96341aSnarayan 			status = vd_setup_vd(vd);
25153c96341aSnarayan 			if (status != EAGAIN || ++retry > vds_dev_retries)
25163c96341aSnarayan 				break;
25173c96341aSnarayan 
25183c96341aSnarayan 			/* incremental delay */
25193c96341aSnarayan 			delay(drv_usectohz(vds_dev_delay));
25203c96341aSnarayan 
25213c96341aSnarayan 			/* if vdisk is no longer enabled - return error */
25223c96341aSnarayan 			if (!vd_enabled(vd))
25233c96341aSnarayan 				return (ENXIO);
25243c96341aSnarayan 
25253c96341aSnarayan 		} while (status == EAGAIN);
25263c96341aSnarayan 
25273c96341aSnarayan 		if (status)
25283c96341aSnarayan 			return (ENXIO);
25293c96341aSnarayan 
25303c96341aSnarayan 		vd->initialized |= VD_DISK_READY;
25313c96341aSnarayan 		ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
25323c96341aSnarayan 		PR0("vdisk_type = %s, pseudo = %s, file = %s, nslices = %u",
25333c96341aSnarayan 		    ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
25343c96341aSnarayan 		    (vd->pseudo ? "yes" : "no"),
25353c96341aSnarayan 		    (vd->file ? "yes" : "no"),
25363c96341aSnarayan 		    vd->nslices);
25373c96341aSnarayan 	}
25383c96341aSnarayan 
25391ae08745Sheppo 	/* Success:  valid message and transfer mode */
25401ae08745Sheppo 	vd->xfer_mode = attr_msg->xfer_mode;
25413af08d82Slm66018 
25421ae08745Sheppo 	if (vd->xfer_mode == VIO_DESC_MODE) {
25433af08d82Slm66018 
25441ae08745Sheppo 		/*
25451ae08745Sheppo 		 * The vd_dring_inband_msg_t contains one cookie; need room
25461ae08745Sheppo 		 * for up to n-1 more cookies, where "n" is the number of full
25471ae08745Sheppo 		 * pages plus possibly one partial page required to cover
25481ae08745Sheppo 		 * "max_xfer_sz".  Add room for one more cookie if
25491ae08745Sheppo 		 * "max_xfer_sz" isn't an integral multiple of the page size.
25501ae08745Sheppo 		 * Must first get the maximum transfer size in bytes.
25511ae08745Sheppo 		 */
25521ae08745Sheppo 		size_t	max_xfer_bytes = attr_msg->vdisk_block_size ?
25531ae08745Sheppo 		    attr_msg->vdisk_block_size*attr_msg->max_xfer_sz :
25541ae08745Sheppo 		    attr_msg->max_xfer_sz;
25551ae08745Sheppo 		size_t	max_inband_msglen =
25561ae08745Sheppo 		    sizeof (vd_dring_inband_msg_t) +
25571ae08745Sheppo 		    ((max_xfer_bytes/PAGESIZE +
25581ae08745Sheppo 		    ((max_xfer_bytes % PAGESIZE) ? 1 : 0))*
25591ae08745Sheppo 		    (sizeof (ldc_mem_cookie_t)));
25601ae08745Sheppo 
25611ae08745Sheppo 		/*
25621ae08745Sheppo 		 * Set the maximum expected message length to
25631ae08745Sheppo 		 * accommodate in-band-descriptor messages with all
25641ae08745Sheppo 		 * their cookies
25651ae08745Sheppo 		 */
25661ae08745Sheppo 		vd->max_msglen = MAX(vd->max_msglen, max_inband_msglen);
2567d10e4ef2Snarayan 
2568d10e4ef2Snarayan 		/*
2569d10e4ef2Snarayan 		 * Initialize the data structure for processing in-band I/O
2570d10e4ef2Snarayan 		 * request descriptors
2571d10e4ef2Snarayan 		 */
2572d10e4ef2Snarayan 		vd->inband_task.vd	= vd;
25733af08d82Slm66018 		vd->inband_task.msg	= kmem_alloc(vd->max_msglen, KM_SLEEP);
2574d10e4ef2Snarayan 		vd->inband_task.index	= 0;
2575d10e4ef2Snarayan 		vd->inband_task.type	= VD_FINAL_RANGE_TASK;	/* range == 1 */
25761ae08745Sheppo 	}
25771ae08745Sheppo 
2578e1ebb9ecSlm66018 	/* Return the device's block size and max transfer size to the client */
2579e1ebb9ecSlm66018 	attr_msg->vdisk_block_size	= DEV_BSIZE;
2580e1ebb9ecSlm66018 	attr_msg->max_xfer_sz		= vd->max_xfer_sz;
2581e1ebb9ecSlm66018 
25821ae08745Sheppo 	attr_msg->vdisk_size = vd->vdisk_size;
25831ae08745Sheppo 	attr_msg->vdisk_type = vd->vdisk_type;
25841ae08745Sheppo 	attr_msg->operations = vds_operations;
25851ae08745Sheppo 	PR0("%s", VD_CLIENT(vd));
25863af08d82Slm66018 
25873af08d82Slm66018 	ASSERT(vd->dring_task == NULL);
25883af08d82Slm66018 
25891ae08745Sheppo 	return (0);
25901ae08745Sheppo }
25911ae08745Sheppo 
25921ae08745Sheppo static int
25931ae08745Sheppo vd_process_dring_reg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
25941ae08745Sheppo {
25951ae08745Sheppo 	int			status;
25961ae08745Sheppo 	size_t			expected;
25971ae08745Sheppo 	ldc_mem_info_t		dring_minfo;
25981ae08745Sheppo 	vio_dring_reg_msg_t	*reg_msg = (vio_dring_reg_msg_t *)msg;
25991ae08745Sheppo 
26001ae08745Sheppo 
26011ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
26021ae08745Sheppo 
26031ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
26041ae08745Sheppo 	    VIO_DRING_REG)) {
2605d10e4ef2Snarayan 		PR0("Message is not a register-dring message");
2606d10e4ef2Snarayan 		return (ENOMSG);
26071ae08745Sheppo 	}
26081ae08745Sheppo 
26091ae08745Sheppo 	if (msglen < sizeof (*reg_msg)) {
26103af08d82Slm66018 		PR0("Expected at least %lu-byte register-dring message; "
26111ae08745Sheppo 		    "received %lu bytes", sizeof (*reg_msg), msglen);
26121ae08745Sheppo 		return (EBADMSG);
26131ae08745Sheppo 	}
26141ae08745Sheppo 
26151ae08745Sheppo 	expected = sizeof (*reg_msg) +
26161ae08745Sheppo 	    (reg_msg->ncookies - 1)*(sizeof (reg_msg->cookie[0]));
26171ae08745Sheppo 	if (msglen != expected) {
26183af08d82Slm66018 		PR0("Expected %lu-byte register-dring message; "
26191ae08745Sheppo 		    "received %lu bytes", expected, msglen);
26201ae08745Sheppo 		return (EBADMSG);
26211ae08745Sheppo 	}
26221ae08745Sheppo 
26231ae08745Sheppo 	if (vd->initialized & VD_DRING) {
26243af08d82Slm66018 		PR0("A dring was previously registered; only support one");
26251ae08745Sheppo 		return (EBADMSG);
26261ae08745Sheppo 	}
26271ae08745Sheppo 
2628d10e4ef2Snarayan 	if (reg_msg->num_descriptors > INT32_MAX) {
26293af08d82Slm66018 		PR0("reg_msg->num_descriptors = %u; must be <= %u (%s)",
2630d10e4ef2Snarayan 		    reg_msg->ncookies, INT32_MAX, STRINGIZE(INT32_MAX));
2631d10e4ef2Snarayan 		return (EBADMSG);
2632d10e4ef2Snarayan 	}
2633d10e4ef2Snarayan 
26341ae08745Sheppo 	if (reg_msg->ncookies != 1) {
26351ae08745Sheppo 		/*
26361ae08745Sheppo 		 * In addition to fixing the assertion in the success case
26371ae08745Sheppo 		 * below, supporting drings which require more than one
26381ae08745Sheppo 		 * "cookie" requires increasing the value of vd->max_msglen
26391ae08745Sheppo 		 * somewhere in the code path prior to receiving the message
26401ae08745Sheppo 		 * which results in calling this function.  Note that without
26411ae08745Sheppo 		 * making this change, the larger message size required to
26421ae08745Sheppo 		 * accommodate multiple cookies cannot be successfully
26431ae08745Sheppo 		 * received, so this function will not even get called.
26441ae08745Sheppo 		 * Gracefully accommodating more dring cookies might
26451ae08745Sheppo 		 * reasonably demand exchanging an additional attribute or
26461ae08745Sheppo 		 * making a minor protocol adjustment
26471ae08745Sheppo 		 */
26483af08d82Slm66018 		PR0("reg_msg->ncookies = %u != 1", reg_msg->ncookies);
26491ae08745Sheppo 		return (EBADMSG);
26501ae08745Sheppo 	}
26511ae08745Sheppo 
26521ae08745Sheppo 	status = ldc_mem_dring_map(vd->ldc_handle, reg_msg->cookie,
26531ae08745Sheppo 	    reg_msg->ncookies, reg_msg->num_descriptors,
26544bac2208Snarayan 	    reg_msg->descriptor_size, LDC_DIRECT_MAP, &vd->dring_handle);
26551ae08745Sheppo 	if (status != 0) {
26563af08d82Slm66018 		PR0("ldc_mem_dring_map() returned errno %d", status);
26571ae08745Sheppo 		return (status);
26581ae08745Sheppo 	}
26591ae08745Sheppo 
26601ae08745Sheppo 	/*
26611ae08745Sheppo 	 * To remove the need for this assertion, must call
26621ae08745Sheppo 	 * ldc_mem_dring_nextcookie() successfully ncookies-1 times after a
26631ae08745Sheppo 	 * successful call to ldc_mem_dring_map()
26641ae08745Sheppo 	 */
26651ae08745Sheppo 	ASSERT(reg_msg->ncookies == 1);
26661ae08745Sheppo 
26671ae08745Sheppo 	if ((status =
26681ae08745Sheppo 	    ldc_mem_dring_info(vd->dring_handle, &dring_minfo)) != 0) {
26693af08d82Slm66018 		PR0("ldc_mem_dring_info() returned errno %d", status);
26701ae08745Sheppo 		if ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0)
26713af08d82Slm66018 			PR0("ldc_mem_dring_unmap() returned errno %d", status);
26721ae08745Sheppo 		return (status);
26731ae08745Sheppo 	}
26741ae08745Sheppo 
26751ae08745Sheppo 	if (dring_minfo.vaddr == NULL) {
26763af08d82Slm66018 		PR0("Descriptor ring virtual address is NULL");
26770a55fbb7Slm66018 		return (ENXIO);
26781ae08745Sheppo 	}
26791ae08745Sheppo 
26801ae08745Sheppo 
2681d10e4ef2Snarayan 	/* Initialize for valid message and mapped dring */
26821ae08745Sheppo 	PR1("descriptor size = %u, dring length = %u",
26831ae08745Sheppo 	    vd->descriptor_size, vd->dring_len);
26841ae08745Sheppo 	vd->initialized |= VD_DRING;
26851ae08745Sheppo 	vd->dring_ident = 1;	/* "There Can Be Only One" */
26861ae08745Sheppo 	vd->dring = dring_minfo.vaddr;
26871ae08745Sheppo 	vd->descriptor_size = reg_msg->descriptor_size;
26881ae08745Sheppo 	vd->dring_len = reg_msg->num_descriptors;
26891ae08745Sheppo 	reg_msg->dring_ident = vd->dring_ident;
2690d10e4ef2Snarayan 
2691d10e4ef2Snarayan 	/*
2692d10e4ef2Snarayan 	 * Allocate and initialize a "shadow" array of data structures for
2693d10e4ef2Snarayan 	 * tasks to process I/O requests in dring elements
2694d10e4ef2Snarayan 	 */
2695d10e4ef2Snarayan 	vd->dring_task =
2696d10e4ef2Snarayan 	    kmem_zalloc((sizeof (*vd->dring_task)) * vd->dring_len, KM_SLEEP);
2697d10e4ef2Snarayan 	for (int i = 0; i < vd->dring_len; i++) {
2698d10e4ef2Snarayan 		vd->dring_task[i].vd		= vd;
2699d10e4ef2Snarayan 		vd->dring_task[i].index		= i;
2700d10e4ef2Snarayan 		vd->dring_task[i].request	= &VD_DRING_ELEM(i)->payload;
27014bac2208Snarayan 
27024bac2208Snarayan 		status = ldc_mem_alloc_handle(vd->ldc_handle,
27034bac2208Snarayan 		    &(vd->dring_task[i].mhdl));
27044bac2208Snarayan 		if (status) {
27053af08d82Slm66018 			PR0("ldc_mem_alloc_handle() returned err %d ", status);
27064bac2208Snarayan 			return (ENXIO);
27074bac2208Snarayan 		}
27083af08d82Slm66018 
27093af08d82Slm66018 		vd->dring_task[i].msg = kmem_alloc(vd->max_msglen, KM_SLEEP);
2710d10e4ef2Snarayan 	}
2711d10e4ef2Snarayan 
27121ae08745Sheppo 	return (0);
27131ae08745Sheppo }
27141ae08745Sheppo 
27151ae08745Sheppo static int
27161ae08745Sheppo vd_process_dring_unreg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
27171ae08745Sheppo {
27181ae08745Sheppo 	vio_dring_unreg_msg_t	*unreg_msg = (vio_dring_unreg_msg_t *)msg;
27191ae08745Sheppo 
27201ae08745Sheppo 
27211ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
27221ae08745Sheppo 
27231ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
27241ae08745Sheppo 	    VIO_DRING_UNREG)) {
2725d10e4ef2Snarayan 		PR0("Message is not an unregister-dring message");
2726d10e4ef2Snarayan 		return (ENOMSG);
27271ae08745Sheppo 	}
27281ae08745Sheppo 
27291ae08745Sheppo 	if (msglen != sizeof (*unreg_msg)) {
27303af08d82Slm66018 		PR0("Expected %lu-byte unregister-dring message; "
27311ae08745Sheppo 		    "received %lu bytes", sizeof (*unreg_msg), msglen);
27321ae08745Sheppo 		return (EBADMSG);
27331ae08745Sheppo 	}
27341ae08745Sheppo 
27351ae08745Sheppo 	if (unreg_msg->dring_ident != vd->dring_ident) {
27363af08d82Slm66018 		PR0("Expected dring ident %lu; received %lu",
27371ae08745Sheppo 		    vd->dring_ident, unreg_msg->dring_ident);
27381ae08745Sheppo 		return (EBADMSG);
27391ae08745Sheppo 	}
27401ae08745Sheppo 
27411ae08745Sheppo 	return (0);
27421ae08745Sheppo }
27431ae08745Sheppo 
27441ae08745Sheppo static int
27451ae08745Sheppo process_rdx_msg(vio_msg_t *msg, size_t msglen)
27461ae08745Sheppo {
27471ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
27481ae08745Sheppo 
2749d10e4ef2Snarayan 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX)) {
2750d10e4ef2Snarayan 		PR0("Message is not an RDX message");
2751d10e4ef2Snarayan 		return (ENOMSG);
2752d10e4ef2Snarayan 	}
27531ae08745Sheppo 
27541ae08745Sheppo 	if (msglen != sizeof (vio_rdx_msg_t)) {
27553af08d82Slm66018 		PR0("Expected %lu-byte RDX message; received %lu bytes",
27561ae08745Sheppo 		    sizeof (vio_rdx_msg_t), msglen);
27571ae08745Sheppo 		return (EBADMSG);
27581ae08745Sheppo 	}
27591ae08745Sheppo 
2760d10e4ef2Snarayan 	PR0("Valid RDX message");
27611ae08745Sheppo 	return (0);
27621ae08745Sheppo }
27631ae08745Sheppo 
27641ae08745Sheppo static int
27651ae08745Sheppo vd_check_seq_num(vd_t *vd, uint64_t seq_num)
27661ae08745Sheppo {
27671ae08745Sheppo 	if ((vd->initialized & VD_SEQ_NUM) && (seq_num != vd->seq_num + 1)) {
27683af08d82Slm66018 		PR0("Received seq_num %lu; expected %lu",
27691ae08745Sheppo 		    seq_num, (vd->seq_num + 1));
27703af08d82Slm66018 		PR0("initiating soft reset");
2771d10e4ef2Snarayan 		vd_need_reset(vd, B_FALSE);
27721ae08745Sheppo 		return (1);
27731ae08745Sheppo 	}
27741ae08745Sheppo 
27751ae08745Sheppo 	vd->seq_num = seq_num;
27761ae08745Sheppo 	vd->initialized |= VD_SEQ_NUM;	/* superfluous after first time... */
27771ae08745Sheppo 	return (0);
27781ae08745Sheppo }
27791ae08745Sheppo 
27801ae08745Sheppo /*
27811ae08745Sheppo  * Return the expected size of an inband-descriptor message with all the
27821ae08745Sheppo  * cookies it claims to include
27831ae08745Sheppo  */
27841ae08745Sheppo static size_t
27851ae08745Sheppo expected_inband_size(vd_dring_inband_msg_t *msg)
27861ae08745Sheppo {
27871ae08745Sheppo 	return ((sizeof (*msg)) +
27881ae08745Sheppo 	    (msg->payload.ncookies - 1)*(sizeof (msg->payload.cookie[0])));
27891ae08745Sheppo }
27901ae08745Sheppo 
27911ae08745Sheppo /*
27921ae08745Sheppo  * Process an in-band descriptor message:  used with clients like OBP, with
27931ae08745Sheppo  * which vds exchanges descriptors within VIO message payloads, rather than
27941ae08745Sheppo  * operating on them within a descriptor ring
27951ae08745Sheppo  */
27961ae08745Sheppo static int
27973af08d82Slm66018 vd_process_desc_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
27981ae08745Sheppo {
27991ae08745Sheppo 	size_t			expected;
28001ae08745Sheppo 	vd_dring_inband_msg_t	*desc_msg = (vd_dring_inband_msg_t *)msg;
28011ae08745Sheppo 
28021ae08745Sheppo 
28031ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
28041ae08745Sheppo 
28051ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
2806d10e4ef2Snarayan 	    VIO_DESC_DATA)) {
2807d10e4ef2Snarayan 		PR1("Message is not an in-band-descriptor message");
2808d10e4ef2Snarayan 		return (ENOMSG);
2809d10e4ef2Snarayan 	}
28101ae08745Sheppo 
28111ae08745Sheppo 	if (msglen < sizeof (*desc_msg)) {
28123af08d82Slm66018 		PR0("Expected at least %lu-byte descriptor message; "
28131ae08745Sheppo 		    "received %lu bytes", sizeof (*desc_msg), msglen);
28141ae08745Sheppo 		return (EBADMSG);
28151ae08745Sheppo 	}
28161ae08745Sheppo 
28171ae08745Sheppo 	if (msglen != (expected = expected_inband_size(desc_msg))) {
28183af08d82Slm66018 		PR0("Expected %lu-byte descriptor message; "
28191ae08745Sheppo 		    "received %lu bytes", expected, msglen);
28201ae08745Sheppo 		return (EBADMSG);
28211ae08745Sheppo 	}
28221ae08745Sheppo 
2823d10e4ef2Snarayan 	if (vd_check_seq_num(vd, desc_msg->hdr.seq_num) != 0)
28241ae08745Sheppo 		return (EBADMSG);
28251ae08745Sheppo 
2826d10e4ef2Snarayan 	/*
2827d10e4ef2Snarayan 	 * Valid message:  Set up the in-band descriptor task and process the
2828d10e4ef2Snarayan 	 * request.  Arrange to acknowledge the client's message, unless an
2829d10e4ef2Snarayan 	 * error processing the descriptor task results in setting
2830d10e4ef2Snarayan 	 * VIO_SUBTYPE_NACK
2831d10e4ef2Snarayan 	 */
2832d10e4ef2Snarayan 	PR1("Valid in-band-descriptor message");
2833d10e4ef2Snarayan 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
28343af08d82Slm66018 
28353af08d82Slm66018 	ASSERT(vd->inband_task.msg != NULL);
28363af08d82Slm66018 
28373af08d82Slm66018 	bcopy(msg, vd->inband_task.msg, msglen);
2838d10e4ef2Snarayan 	vd->inband_task.msglen	= msglen;
28393af08d82Slm66018 
28403af08d82Slm66018 	/*
28413af08d82Slm66018 	 * The task request is now the payload of the message
28423af08d82Slm66018 	 * that was just copied into the body of the task.
28433af08d82Slm66018 	 */
28443af08d82Slm66018 	desc_msg = (vd_dring_inband_msg_t *)vd->inband_task.msg;
2845d10e4ef2Snarayan 	vd->inband_task.request	= &desc_msg->payload;
28463af08d82Slm66018 
2847d10e4ef2Snarayan 	return (vd_process_task(&vd->inband_task));
28481ae08745Sheppo }
28491ae08745Sheppo 
28501ae08745Sheppo static int
2851d10e4ef2Snarayan vd_process_element(vd_t *vd, vd_task_type_t type, uint32_t idx,
28523af08d82Slm66018     vio_msg_t *msg, size_t msglen)
28531ae08745Sheppo {
28541ae08745Sheppo 	int			status;
2855d10e4ef2Snarayan 	boolean_t		ready;
2856d10e4ef2Snarayan 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
28571ae08745Sheppo 
28581ae08745Sheppo 
2859d10e4ef2Snarayan 	/* Accept the updated dring element */
2860d10e4ef2Snarayan 	if ((status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) {
28613af08d82Slm66018 		PR0("ldc_mem_dring_acquire() returned errno %d", status);
28621ae08745Sheppo 		return (status);
28631ae08745Sheppo 	}
2864d10e4ef2Snarayan 	ready = (elem->hdr.dstate == VIO_DESC_READY);
2865d10e4ef2Snarayan 	if (ready) {
2866d10e4ef2Snarayan 		elem->hdr.dstate = VIO_DESC_ACCEPTED;
2867d10e4ef2Snarayan 	} else {
28683af08d82Slm66018 		PR0("descriptor %u not ready", idx);
2869d10e4ef2Snarayan 		VD_DUMP_DRING_ELEM(elem);
2870d10e4ef2Snarayan 	}
2871d10e4ef2Snarayan 	if ((status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) {
28723af08d82Slm66018 		PR0("ldc_mem_dring_release() returned errno %d", status);
28731ae08745Sheppo 		return (status);
28741ae08745Sheppo 	}
2875d10e4ef2Snarayan 	if (!ready)
2876d10e4ef2Snarayan 		return (EBUSY);
28771ae08745Sheppo 
28781ae08745Sheppo 
2879d10e4ef2Snarayan 	/* Initialize a task and process the accepted element */
2880d10e4ef2Snarayan 	PR1("Processing dring element %u", idx);
2881d10e4ef2Snarayan 	vd->dring_task[idx].type	= type;
28823af08d82Slm66018 
28833af08d82Slm66018 	/* duplicate msg buf for cookies etc. */
28843af08d82Slm66018 	bcopy(msg, vd->dring_task[idx].msg, msglen);
28853af08d82Slm66018 
2886d10e4ef2Snarayan 	vd->dring_task[idx].msglen	= msglen;
2887205eeb1aSlm66018 	return (vd_process_task(&vd->dring_task[idx]));
28881ae08745Sheppo }
28891ae08745Sheppo 
28901ae08745Sheppo static int
2891d10e4ef2Snarayan vd_process_element_range(vd_t *vd, int start, int end,
28923af08d82Slm66018     vio_msg_t *msg, size_t msglen)
2893d10e4ef2Snarayan {
2894d10e4ef2Snarayan 	int		i, n, nelem, status = 0;
2895d10e4ef2Snarayan 	boolean_t	inprogress = B_FALSE;
2896d10e4ef2Snarayan 	vd_task_type_t	type;
2897d10e4ef2Snarayan 
2898d10e4ef2Snarayan 
2899d10e4ef2Snarayan 	ASSERT(start >= 0);
2900d10e4ef2Snarayan 	ASSERT(end >= 0);
2901d10e4ef2Snarayan 
2902d10e4ef2Snarayan 	/*
2903d10e4ef2Snarayan 	 * Arrange to acknowledge the client's message, unless an error
2904d10e4ef2Snarayan 	 * processing one of the dring elements results in setting
2905d10e4ef2Snarayan 	 * VIO_SUBTYPE_NACK
2906d10e4ef2Snarayan 	 */
2907d10e4ef2Snarayan 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
2908d10e4ef2Snarayan 
2909d10e4ef2Snarayan 	/*
2910d10e4ef2Snarayan 	 * Process the dring elements in the range
2911d10e4ef2Snarayan 	 */
2912d10e4ef2Snarayan 	nelem = ((end < start) ? end + vd->dring_len : end) - start + 1;
2913d10e4ef2Snarayan 	for (i = start, n = nelem; n > 0; i = (i + 1) % vd->dring_len, n--) {
2914d10e4ef2Snarayan 		((vio_dring_msg_t *)msg)->end_idx = i;
2915d10e4ef2Snarayan 		type = (n == 1) ? VD_FINAL_RANGE_TASK : VD_NONFINAL_RANGE_TASK;
29163af08d82Slm66018 		status = vd_process_element(vd, type, i, msg, msglen);
2917d10e4ef2Snarayan 		if (status == EINPROGRESS)
2918d10e4ef2Snarayan 			inprogress = B_TRUE;
2919d10e4ef2Snarayan 		else if (status != 0)
2920d10e4ef2Snarayan 			break;
2921d10e4ef2Snarayan 	}
2922d10e4ef2Snarayan 
2923d10e4ef2Snarayan 	/*
2924d10e4ef2Snarayan 	 * If some, but not all, operations of a multi-element range are in
2925d10e4ef2Snarayan 	 * progress, wait for other operations to complete before returning
2926d10e4ef2Snarayan 	 * (which will result in "ack" or "nack" of the message).  Note that
2927d10e4ef2Snarayan 	 * all outstanding operations will need to complete, not just the ones
2928d10e4ef2Snarayan 	 * corresponding to the current range of dring elements; howevever, as
2929d10e4ef2Snarayan 	 * this situation is an error case, performance is less critical.
2930d10e4ef2Snarayan 	 */
2931d10e4ef2Snarayan 	if ((nelem > 1) && (status != EINPROGRESS) && inprogress)
2932d10e4ef2Snarayan 		ddi_taskq_wait(vd->completionq);
2933d10e4ef2Snarayan 
2934d10e4ef2Snarayan 	return (status);
2935d10e4ef2Snarayan }
2936d10e4ef2Snarayan 
2937d10e4ef2Snarayan static int
29383af08d82Slm66018 vd_process_dring_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
29391ae08745Sheppo {
29401ae08745Sheppo 	vio_dring_msg_t	*dring_msg = (vio_dring_msg_t *)msg;
29411ae08745Sheppo 
29421ae08745Sheppo 
29431ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
29441ae08745Sheppo 
29451ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
29461ae08745Sheppo 	    VIO_DRING_DATA)) {
2947d10e4ef2Snarayan 		PR1("Message is not a dring-data message");
2948d10e4ef2Snarayan 		return (ENOMSG);
29491ae08745Sheppo 	}
29501ae08745Sheppo 
29511ae08745Sheppo 	if (msglen != sizeof (*dring_msg)) {
29523af08d82Slm66018 		PR0("Expected %lu-byte dring message; received %lu bytes",
29531ae08745Sheppo 		    sizeof (*dring_msg), msglen);
29541ae08745Sheppo 		return (EBADMSG);
29551ae08745Sheppo 	}
29561ae08745Sheppo 
2957d10e4ef2Snarayan 	if (vd_check_seq_num(vd, dring_msg->seq_num) != 0)
29581ae08745Sheppo 		return (EBADMSG);
29591ae08745Sheppo 
29601ae08745Sheppo 	if (dring_msg->dring_ident != vd->dring_ident) {
29613af08d82Slm66018 		PR0("Expected dring ident %lu; received ident %lu",
29621ae08745Sheppo 		    vd->dring_ident, dring_msg->dring_ident);
29631ae08745Sheppo 		return (EBADMSG);
29641ae08745Sheppo 	}
29651ae08745Sheppo 
2966d10e4ef2Snarayan 	if (dring_msg->start_idx >= vd->dring_len) {
29673af08d82Slm66018 		PR0("\"start_idx\" = %u; must be less than %u",
2968d10e4ef2Snarayan 		    dring_msg->start_idx, vd->dring_len);
2969d10e4ef2Snarayan 		return (EBADMSG);
2970d10e4ef2Snarayan 	}
29711ae08745Sheppo 
2972d10e4ef2Snarayan 	if ((dring_msg->end_idx < 0) ||
2973d10e4ef2Snarayan 	    (dring_msg->end_idx >= vd->dring_len)) {
29743af08d82Slm66018 		PR0("\"end_idx\" = %u; must be >= 0 and less than %u",
2975d10e4ef2Snarayan 		    dring_msg->end_idx, vd->dring_len);
2976d10e4ef2Snarayan 		return (EBADMSG);
2977d10e4ef2Snarayan 	}
2978d10e4ef2Snarayan 
2979d10e4ef2Snarayan 	/* Valid message; process range of updated dring elements */
2980d10e4ef2Snarayan 	PR1("Processing descriptor range, start = %u, end = %u",
2981d10e4ef2Snarayan 	    dring_msg->start_idx, dring_msg->end_idx);
2982d10e4ef2Snarayan 	return (vd_process_element_range(vd, dring_msg->start_idx,
29833af08d82Slm66018 	    dring_msg->end_idx, msg, msglen));
29841ae08745Sheppo }
29851ae08745Sheppo 
29861ae08745Sheppo static int
29871ae08745Sheppo recv_msg(ldc_handle_t ldc_handle, void *msg, size_t *nbytes)
29881ae08745Sheppo {
29891ae08745Sheppo 	int	retry, status;
29901ae08745Sheppo 	size_t	size = *nbytes;
29911ae08745Sheppo 
29921ae08745Sheppo 
29931ae08745Sheppo 	for (retry = 0, status = ETIMEDOUT;
29941ae08745Sheppo 	    retry < vds_ldc_retries && status == ETIMEDOUT;
29951ae08745Sheppo 	    retry++) {
29961ae08745Sheppo 		PR1("ldc_read() attempt %d", (retry + 1));
29971ae08745Sheppo 		*nbytes = size;
29981ae08745Sheppo 		status = ldc_read(ldc_handle, msg, nbytes);
29991ae08745Sheppo 	}
30001ae08745Sheppo 
30013af08d82Slm66018 	if (status) {
30023af08d82Slm66018 		PR0("ldc_read() returned errno %d", status);
30033af08d82Slm66018 		if (status != ECONNRESET)
30043af08d82Slm66018 			return (ENOMSG);
30051ae08745Sheppo 		return (status);
30061ae08745Sheppo 	} else if (*nbytes == 0) {
30071ae08745Sheppo 		PR1("ldc_read() returned 0 and no message read");
30081ae08745Sheppo 		return (ENOMSG);
30091ae08745Sheppo 	}
30101ae08745Sheppo 
30111ae08745Sheppo 	PR1("RCVD %lu-byte message", *nbytes);
30121ae08745Sheppo 	return (0);
30131ae08745Sheppo }
30141ae08745Sheppo 
30151ae08745Sheppo static int
30163af08d82Slm66018 vd_do_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
30171ae08745Sheppo {
30181ae08745Sheppo 	int		status;
30191ae08745Sheppo 
30201ae08745Sheppo 
30211ae08745Sheppo 	PR1("Processing (%x/%x/%x) message", msg->tag.vio_msgtype,
30221ae08745Sheppo 	    msg->tag.vio_subtype, msg->tag.vio_subtype_env);
30233af08d82Slm66018 #ifdef	DEBUG
30243af08d82Slm66018 	vd_decode_tag(msg);
30253af08d82Slm66018 #endif
30261ae08745Sheppo 
30271ae08745Sheppo 	/*
30281ae08745Sheppo 	 * Validate session ID up front, since it applies to all messages
30291ae08745Sheppo 	 * once set
30301ae08745Sheppo 	 */
30311ae08745Sheppo 	if ((msg->tag.vio_sid != vd->sid) && (vd->initialized & VD_SID)) {
30323af08d82Slm66018 		PR0("Expected SID %u, received %u", vd->sid,
30331ae08745Sheppo 		    msg->tag.vio_sid);
30341ae08745Sheppo 		return (EBADMSG);
30351ae08745Sheppo 	}
30361ae08745Sheppo 
30373af08d82Slm66018 	PR1("\tWhile in state %d (%s)", vd->state, vd_decode_state(vd->state));
30381ae08745Sheppo 
30391ae08745Sheppo 	/*
30401ae08745Sheppo 	 * Process the received message based on connection state
30411ae08745Sheppo 	 */
30421ae08745Sheppo 	switch (vd->state) {
30431ae08745Sheppo 	case VD_STATE_INIT:	/* expect version message */
30440a55fbb7Slm66018 		if ((status = vd_process_ver_msg(vd, msg, msglen)) != 0)
30451ae08745Sheppo 			return (status);
30461ae08745Sheppo 
30471ae08745Sheppo 		/* Version negotiated, move to that state */
30481ae08745Sheppo 		vd->state = VD_STATE_VER;
30491ae08745Sheppo 		return (0);
30501ae08745Sheppo 
30511ae08745Sheppo 	case VD_STATE_VER:	/* expect attribute message */
30521ae08745Sheppo 		if ((status = vd_process_attr_msg(vd, msg, msglen)) != 0)
30531ae08745Sheppo 			return (status);
30541ae08745Sheppo 
30551ae08745Sheppo 		/* Attributes exchanged, move to that state */
30561ae08745Sheppo 		vd->state = VD_STATE_ATTR;
30571ae08745Sheppo 		return (0);
30581ae08745Sheppo 
30591ae08745Sheppo 	case VD_STATE_ATTR:
30601ae08745Sheppo 		switch (vd->xfer_mode) {
30611ae08745Sheppo 		case VIO_DESC_MODE:	/* expect RDX message */
30621ae08745Sheppo 			if ((status = process_rdx_msg(msg, msglen)) != 0)
30631ae08745Sheppo 				return (status);
30641ae08745Sheppo 
30651ae08745Sheppo 			/* Ready to receive in-band descriptors */
30661ae08745Sheppo 			vd->state = VD_STATE_DATA;
30671ae08745Sheppo 			return (0);
30681ae08745Sheppo 
30691ae08745Sheppo 		case VIO_DRING_MODE:	/* expect register-dring message */
30701ae08745Sheppo 			if ((status =
30711ae08745Sheppo 			    vd_process_dring_reg_msg(vd, msg, msglen)) != 0)
30721ae08745Sheppo 				return (status);
30731ae08745Sheppo 
30741ae08745Sheppo 			/* One dring negotiated, move to that state */
30751ae08745Sheppo 			vd->state = VD_STATE_DRING;
30761ae08745Sheppo 			return (0);
30771ae08745Sheppo 
30781ae08745Sheppo 		default:
30791ae08745Sheppo 			ASSERT("Unsupported transfer mode");
30803af08d82Slm66018 			PR0("Unsupported transfer mode");
30811ae08745Sheppo 			return (ENOTSUP);
30821ae08745Sheppo 		}
30831ae08745Sheppo 
30841ae08745Sheppo 	case VD_STATE_DRING:	/* expect RDX, register-dring, or unreg-dring */
30851ae08745Sheppo 		if ((status = process_rdx_msg(msg, msglen)) == 0) {
30861ae08745Sheppo 			/* Ready to receive data */
30871ae08745Sheppo 			vd->state = VD_STATE_DATA;
30881ae08745Sheppo 			return (0);
30891ae08745Sheppo 		} else if (status != ENOMSG) {
30901ae08745Sheppo 			return (status);
30911ae08745Sheppo 		}
30921ae08745Sheppo 
30931ae08745Sheppo 
30941ae08745Sheppo 		/*
30951ae08745Sheppo 		 * If another register-dring message is received, stay in
30961ae08745Sheppo 		 * dring state in case the client sends RDX; although the
30971ae08745Sheppo 		 * protocol allows multiple drings, this server does not
30981ae08745Sheppo 		 * support using more than one
30991ae08745Sheppo 		 */
31001ae08745Sheppo 		if ((status =
31011ae08745Sheppo 		    vd_process_dring_reg_msg(vd, msg, msglen)) != ENOMSG)
31021ae08745Sheppo 			return (status);
31031ae08745Sheppo 
31041ae08745Sheppo 		/*
31051ae08745Sheppo 		 * Acknowledge an unregister-dring message, but reset the
31061ae08745Sheppo 		 * connection anyway:  Although the protocol allows
31071ae08745Sheppo 		 * unregistering drings, this server cannot serve a vdisk
31081ae08745Sheppo 		 * without its only dring
31091ae08745Sheppo 		 */
31101ae08745Sheppo 		status = vd_process_dring_unreg_msg(vd, msg, msglen);
31111ae08745Sheppo 		return ((status == 0) ? ENOTSUP : status);
31121ae08745Sheppo 
31131ae08745Sheppo 	case VD_STATE_DATA:
31141ae08745Sheppo 		switch (vd->xfer_mode) {
31151ae08745Sheppo 		case VIO_DESC_MODE:	/* expect in-band-descriptor message */
31163af08d82Slm66018 			return (vd_process_desc_msg(vd, msg, msglen));
31171ae08745Sheppo 
31181ae08745Sheppo 		case VIO_DRING_MODE:	/* expect dring-data or unreg-dring */
31191ae08745Sheppo 			/*
31201ae08745Sheppo 			 * Typically expect dring-data messages, so handle
31211ae08745Sheppo 			 * them first
31221ae08745Sheppo 			 */
31231ae08745Sheppo 			if ((status = vd_process_dring_msg(vd, msg,
31243af08d82Slm66018 			    msglen)) != ENOMSG)
31251ae08745Sheppo 				return (status);
31261ae08745Sheppo 
31271ae08745Sheppo 			/*
31281ae08745Sheppo 			 * Acknowledge an unregister-dring message, but reset
31291ae08745Sheppo 			 * the connection anyway:  Although the protocol
31301ae08745Sheppo 			 * allows unregistering drings, this server cannot
31311ae08745Sheppo 			 * serve a vdisk without its only dring
31321ae08745Sheppo 			 */
31331ae08745Sheppo 			status = vd_process_dring_unreg_msg(vd, msg, msglen);
31341ae08745Sheppo 			return ((status == 0) ? ENOTSUP : status);
31351ae08745Sheppo 
31361ae08745Sheppo 		default:
31371ae08745Sheppo 			ASSERT("Unsupported transfer mode");
31383af08d82Slm66018 			PR0("Unsupported transfer mode");
31391ae08745Sheppo 			return (ENOTSUP);
31401ae08745Sheppo 		}
31411ae08745Sheppo 
31421ae08745Sheppo 	default:
31431ae08745Sheppo 		ASSERT("Invalid client connection state");
31443af08d82Slm66018 		PR0("Invalid client connection state");
31451ae08745Sheppo 		return (ENOTSUP);
31461ae08745Sheppo 	}
31471ae08745Sheppo }
31481ae08745Sheppo 
3149d10e4ef2Snarayan static int
31503af08d82Slm66018 vd_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
31511ae08745Sheppo {
31521ae08745Sheppo 	int		status;
31531ae08745Sheppo 	boolean_t	reset_ldc = B_FALSE;
3154205eeb1aSlm66018 	vd_task_t	task;
31551ae08745Sheppo 
31561ae08745Sheppo 	/*
31571ae08745Sheppo 	 * Check that the message is at least big enough for a "tag", so that
31581ae08745Sheppo 	 * message processing can proceed based on tag-specified message type
31591ae08745Sheppo 	 */
31601ae08745Sheppo 	if (msglen < sizeof (vio_msg_tag_t)) {
31613af08d82Slm66018 		PR0("Received short (%lu-byte) message", msglen);
31621ae08745Sheppo 		/* Can't "nack" short message, so drop the big hammer */
31633af08d82Slm66018 		PR0("initiating full reset");
3164d10e4ef2Snarayan 		vd_need_reset(vd, B_TRUE);
3165d10e4ef2Snarayan 		return (EBADMSG);
31661ae08745Sheppo 	}
31671ae08745Sheppo 
31681ae08745Sheppo 	/*
31691ae08745Sheppo 	 * Process the message
31701ae08745Sheppo 	 */
31713af08d82Slm66018 	switch (status = vd_do_process_msg(vd, msg, msglen)) {
31721ae08745Sheppo 	case 0:
31731ae08745Sheppo 		/* "ack" valid, successfully-processed messages */
31741ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
31751ae08745Sheppo 		break;
31761ae08745Sheppo 
3177d10e4ef2Snarayan 	case EINPROGRESS:
3178d10e4ef2Snarayan 		/* The completion handler will "ack" or "nack" the message */
3179d10e4ef2Snarayan 		return (EINPROGRESS);
31801ae08745Sheppo 	case ENOMSG:
31813af08d82Slm66018 		PR0("Received unexpected message");
31821ae08745Sheppo 		_NOTE(FALLTHROUGH);
31831ae08745Sheppo 	case EBADMSG:
31841ae08745Sheppo 	case ENOTSUP:
3185205eeb1aSlm66018 		/* "transport" error will cause NACK of invalid messages */
31861ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
31871ae08745Sheppo 		break;
31881ae08745Sheppo 
31891ae08745Sheppo 	default:
3190205eeb1aSlm66018 		/* "transport" error will cause NACK of invalid messages */
31911ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
31921ae08745Sheppo 		/* An LDC error probably occurred, so try resetting it */
31931ae08745Sheppo 		reset_ldc = B_TRUE;
31941ae08745Sheppo 		break;
31951ae08745Sheppo 	}
31961ae08745Sheppo 
31973af08d82Slm66018 	PR1("\tResulting in state %d (%s)", vd->state,
31983af08d82Slm66018 	    vd_decode_state(vd->state));
31993af08d82Slm66018 
3200205eeb1aSlm66018 	/* populate the task so we can dispatch it on the taskq */
3201205eeb1aSlm66018 	task.vd = vd;
3202205eeb1aSlm66018 	task.msg = msg;
3203205eeb1aSlm66018 	task.msglen = msglen;
3204205eeb1aSlm66018 
3205205eeb1aSlm66018 	/*
3206205eeb1aSlm66018 	 * Queue a task to send the notification that the operation completed.
3207205eeb1aSlm66018 	 * We need to ensure that requests are responded to in the correct
3208205eeb1aSlm66018 	 * order and since the taskq is processed serially this ordering
3209205eeb1aSlm66018 	 * is maintained.
3210205eeb1aSlm66018 	 */
3211205eeb1aSlm66018 	(void) ddi_taskq_dispatch(vd->completionq, vd_serial_notify,
3212205eeb1aSlm66018 	    &task, DDI_SLEEP);
3213205eeb1aSlm66018 
3214205eeb1aSlm66018 	/*
3215205eeb1aSlm66018 	 * To ensure handshake negotiations do not happen out of order, such
3216205eeb1aSlm66018 	 * requests that come through this path should not be done in parallel
3217205eeb1aSlm66018 	 * so we need to wait here until the response is sent to the client.
3218205eeb1aSlm66018 	 */
3219205eeb1aSlm66018 	ddi_taskq_wait(vd->completionq);
32201ae08745Sheppo 
3221d10e4ef2Snarayan 	/* Arrange to reset the connection for nack'ed or failed messages */
32223af08d82Slm66018 	if ((status != 0) || reset_ldc) {
32233af08d82Slm66018 		PR0("initiating %s reset",
32243af08d82Slm66018 		    (reset_ldc) ? "full" : "soft");
3225d10e4ef2Snarayan 		vd_need_reset(vd, reset_ldc);
32263af08d82Slm66018 	}
3227d10e4ef2Snarayan 
3228d10e4ef2Snarayan 	return (status);
3229d10e4ef2Snarayan }
3230d10e4ef2Snarayan 
3231d10e4ef2Snarayan static boolean_t
3232d10e4ef2Snarayan vd_enabled(vd_t *vd)
3233d10e4ef2Snarayan {
3234d10e4ef2Snarayan 	boolean_t	enabled;
3235d10e4ef2Snarayan 
3236d10e4ef2Snarayan 	mutex_enter(&vd->lock);
3237d10e4ef2Snarayan 	enabled = vd->enabled;
3238d10e4ef2Snarayan 	mutex_exit(&vd->lock);
3239d10e4ef2Snarayan 	return (enabled);
32401ae08745Sheppo }
32411ae08745Sheppo 
32421ae08745Sheppo static void
32430a55fbb7Slm66018 vd_recv_msg(void *arg)
32441ae08745Sheppo {
32451ae08745Sheppo 	vd_t	*vd = (vd_t *)arg;
32463af08d82Slm66018 	int	rv = 0, status = 0;
32471ae08745Sheppo 
32481ae08745Sheppo 	ASSERT(vd != NULL);
32493af08d82Slm66018 
3250d10e4ef2Snarayan 	PR2("New task to receive incoming message(s)");
32513af08d82Slm66018 
32523af08d82Slm66018 
3253d10e4ef2Snarayan 	while (vd_enabled(vd) && status == 0) {
3254d10e4ef2Snarayan 		size_t		msglen, msgsize;
32553af08d82Slm66018 		ldc_status_t	lstatus;
3256d10e4ef2Snarayan 
32570a55fbb7Slm66018 		/*
3258d10e4ef2Snarayan 		 * Receive and process a message
32590a55fbb7Slm66018 		 */
3260d10e4ef2Snarayan 		vd_reset_if_needed(vd);	/* can change vd->max_msglen */
32613af08d82Slm66018 
32623af08d82Slm66018 		/*
32633af08d82Slm66018 		 * check if channel is UP - else break out of loop
32643af08d82Slm66018 		 */
32653af08d82Slm66018 		status = ldc_status(vd->ldc_handle, &lstatus);
32663af08d82Slm66018 		if (lstatus != LDC_UP) {
32673af08d82Slm66018 			PR0("channel not up (status=%d), exiting recv loop\n",
32683af08d82Slm66018 			    lstatus);
32693af08d82Slm66018 			break;
32703af08d82Slm66018 		}
32713af08d82Slm66018 
32723af08d82Slm66018 		ASSERT(vd->max_msglen != 0);
32733af08d82Slm66018 
3274d10e4ef2Snarayan 		msgsize = vd->max_msglen; /* stable copy for alloc/free */
32753af08d82Slm66018 		msglen	= msgsize;	  /* actual len after recv_msg() */
32763af08d82Slm66018 
32773af08d82Slm66018 		status = recv_msg(vd->ldc_handle, vd->vio_msgp, &msglen);
32783af08d82Slm66018 		switch (status) {
32793af08d82Slm66018 		case 0:
32803af08d82Slm66018 			rv = vd_process_msg(vd, (vio_msg_t *)vd->vio_msgp,
32813af08d82Slm66018 			    msglen);
32823af08d82Slm66018 			/* check if max_msglen changed */
32833af08d82Slm66018 			if (msgsize != vd->max_msglen) {
32843af08d82Slm66018 				PR0("max_msglen changed 0x%lx to 0x%lx bytes\n",
32853af08d82Slm66018 				    msgsize, vd->max_msglen);
32863af08d82Slm66018 				kmem_free(vd->vio_msgp, msgsize);
32873af08d82Slm66018 				vd->vio_msgp =
32883af08d82Slm66018 				    kmem_alloc(vd->max_msglen, KM_SLEEP);
32893af08d82Slm66018 			}
32903af08d82Slm66018 			if (rv == EINPROGRESS)
32913af08d82Slm66018 				continue;
32923af08d82Slm66018 			break;
32933af08d82Slm66018 
32943af08d82Slm66018 		case ENOMSG:
32953af08d82Slm66018 			break;
32963af08d82Slm66018 
32973af08d82Slm66018 		case ECONNRESET:
32983af08d82Slm66018 			PR0("initiating soft reset (ECONNRESET)\n");
32993af08d82Slm66018 			vd_need_reset(vd, B_FALSE);
33003af08d82Slm66018 			status = 0;
33013af08d82Slm66018 			break;
33023af08d82Slm66018 
33033af08d82Slm66018 		default:
3304d10e4ef2Snarayan 			/* Probably an LDC failure; arrange to reset it */
33053af08d82Slm66018 			PR0("initiating full reset (status=0x%x)", status);
3306d10e4ef2Snarayan 			vd_need_reset(vd, B_TRUE);
33073af08d82Slm66018 			break;
33080a55fbb7Slm66018 		}
33091ae08745Sheppo 	}
33103af08d82Slm66018 
3311d10e4ef2Snarayan 	PR2("Task finished");
33120a55fbb7Slm66018 }
33130a55fbb7Slm66018 
33140a55fbb7Slm66018 static uint_t
33151ae08745Sheppo vd_handle_ldc_events(uint64_t event, caddr_t arg)
33161ae08745Sheppo {
33171ae08745Sheppo 	vd_t	*vd = (vd_t *)(void *)arg;
33183af08d82Slm66018 	int	status;
33191ae08745Sheppo 
33201ae08745Sheppo 	ASSERT(vd != NULL);
3321d10e4ef2Snarayan 
3322d10e4ef2Snarayan 	if (!vd_enabled(vd))
3323d10e4ef2Snarayan 		return (LDC_SUCCESS);
3324d10e4ef2Snarayan 
33253af08d82Slm66018 	if (event & LDC_EVT_DOWN) {
332634683adeSsg70180 		PR0("LDC_EVT_DOWN: LDC channel went down");
33273af08d82Slm66018 
33283af08d82Slm66018 		vd_need_reset(vd, B_TRUE);
33293af08d82Slm66018 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
33303af08d82Slm66018 		    DDI_SLEEP);
33313af08d82Slm66018 		if (status == DDI_FAILURE) {
33323af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
33333af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
33343af08d82Slm66018 		}
33353af08d82Slm66018 	}
33363af08d82Slm66018 
3337d10e4ef2Snarayan 	if (event & LDC_EVT_RESET) {
33383af08d82Slm66018 		PR0("LDC_EVT_RESET: LDC channel was reset");
33393af08d82Slm66018 
33403af08d82Slm66018 		if (vd->state != VD_STATE_INIT) {
33413af08d82Slm66018 			PR0("scheduling full reset");
33423af08d82Slm66018 			vd_need_reset(vd, B_FALSE);
33433af08d82Slm66018 			status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
33443af08d82Slm66018 			    vd, DDI_SLEEP);
33453af08d82Slm66018 			if (status == DDI_FAILURE) {
33463af08d82Slm66018 				PR0("cannot schedule task to recv msg\n");
33473af08d82Slm66018 				vd_need_reset(vd, B_TRUE);
33483af08d82Slm66018 			}
33493af08d82Slm66018 
33503af08d82Slm66018 		} else {
33513af08d82Slm66018 			PR0("channel already reset, ignoring...\n");
33523af08d82Slm66018 			PR0("doing ldc up...\n");
33533af08d82Slm66018 			(void) ldc_up(vd->ldc_handle);
33543af08d82Slm66018 		}
33553af08d82Slm66018 
3356d10e4ef2Snarayan 		return (LDC_SUCCESS);
3357d10e4ef2Snarayan 	}
3358d10e4ef2Snarayan 
3359d10e4ef2Snarayan 	if (event & LDC_EVT_UP) {
33603af08d82Slm66018 		PR0("EVT_UP: LDC is up\nResetting client connection state");
33613af08d82Slm66018 		PR0("initiating soft reset");
3362d10e4ef2Snarayan 		vd_need_reset(vd, B_FALSE);
33633af08d82Slm66018 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
33643af08d82Slm66018 		    vd, DDI_SLEEP);
33653af08d82Slm66018 		if (status == DDI_FAILURE) {
33663af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
33673af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
33683af08d82Slm66018 			return (LDC_SUCCESS);
33693af08d82Slm66018 		}
3370d10e4ef2Snarayan 	}
3371d10e4ef2Snarayan 
3372d10e4ef2Snarayan 	if (event & LDC_EVT_READ) {
3373d10e4ef2Snarayan 		int	status;
3374d10e4ef2Snarayan 
3375d10e4ef2Snarayan 		PR1("New data available");
3376d10e4ef2Snarayan 		/* Queue a task to receive the new data */
3377d10e4ef2Snarayan 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
3378d10e4ef2Snarayan 		    DDI_SLEEP);
33793af08d82Slm66018 
33803af08d82Slm66018 		if (status == DDI_FAILURE) {
33813af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
33823af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
33833af08d82Slm66018 		}
3384d10e4ef2Snarayan 	}
3385d10e4ef2Snarayan 
3386d10e4ef2Snarayan 	return (LDC_SUCCESS);
33871ae08745Sheppo }
33881ae08745Sheppo 
33891ae08745Sheppo static uint_t
33901ae08745Sheppo vds_check_for_vd(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
33911ae08745Sheppo {
33921ae08745Sheppo 	_NOTE(ARGUNUSED(key, val))
33931ae08745Sheppo 	(*((uint_t *)arg))++;
33941ae08745Sheppo 	return (MH_WALK_TERMINATE);
33951ae08745Sheppo }
33961ae08745Sheppo 
33971ae08745Sheppo 
33981ae08745Sheppo static int
33991ae08745Sheppo vds_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
34001ae08745Sheppo {
34011ae08745Sheppo 	uint_t	vd_present = 0;
34021ae08745Sheppo 	minor_t	instance;
34031ae08745Sheppo 	vds_t	*vds;
34041ae08745Sheppo 
34051ae08745Sheppo 
34061ae08745Sheppo 	switch (cmd) {
34071ae08745Sheppo 	case DDI_DETACH:
34081ae08745Sheppo 		/* the real work happens below */
34091ae08745Sheppo 		break;
34101ae08745Sheppo 	case DDI_SUSPEND:
3411d10e4ef2Snarayan 		PR0("No action required for DDI_SUSPEND");
34121ae08745Sheppo 		return (DDI_SUCCESS);
34131ae08745Sheppo 	default:
34143af08d82Slm66018 		PR0("Unrecognized \"cmd\"");
34151ae08745Sheppo 		return (DDI_FAILURE);
34161ae08745Sheppo 	}
34171ae08745Sheppo 
34181ae08745Sheppo 	ASSERT(cmd == DDI_DETACH);
34191ae08745Sheppo 	instance = ddi_get_instance(dip);
34201ae08745Sheppo 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
34213af08d82Slm66018 		PR0("Could not get state for instance %u", instance);
34221ae08745Sheppo 		ddi_soft_state_free(vds_state, instance);
34231ae08745Sheppo 		return (DDI_FAILURE);
34241ae08745Sheppo 	}
34251ae08745Sheppo 
34261ae08745Sheppo 	/* Do no detach when serving any vdisks */
34271ae08745Sheppo 	mod_hash_walk(vds->vd_table, vds_check_for_vd, &vd_present);
34281ae08745Sheppo 	if (vd_present) {
34291ae08745Sheppo 		PR0("Not detaching because serving vdisks");
34301ae08745Sheppo 		return (DDI_FAILURE);
34311ae08745Sheppo 	}
34321ae08745Sheppo 
34331ae08745Sheppo 	PR0("Detaching");
3434445b4c2eSsb155480 	if (vds->initialized & VDS_MDEG) {
34351ae08745Sheppo 		(void) mdeg_unregister(vds->mdeg);
3436445b4c2eSsb155480 		kmem_free(vds->ispecp->specp, sizeof (vds_prop_template));
3437445b4c2eSsb155480 		kmem_free(vds->ispecp, sizeof (mdeg_node_spec_t));
3438445b4c2eSsb155480 		vds->ispecp = NULL;
3439445b4c2eSsb155480 		vds->mdeg = NULL;
3440445b4c2eSsb155480 	}
3441445b4c2eSsb155480 
34421ae08745Sheppo 	if (vds->initialized & VDS_LDI)
34431ae08745Sheppo 		(void) ldi_ident_release(vds->ldi_ident);
34441ae08745Sheppo 	mod_hash_destroy_hash(vds->vd_table);
34451ae08745Sheppo 	ddi_soft_state_free(vds_state, instance);
34461ae08745Sheppo 	return (DDI_SUCCESS);
34471ae08745Sheppo }
34481ae08745Sheppo 
34491ae08745Sheppo static boolean_t
34501ae08745Sheppo is_pseudo_device(dev_info_t *dip)
34511ae08745Sheppo {
34521ae08745Sheppo 	dev_info_t	*parent, *root = ddi_root_node();
34531ae08745Sheppo 
34541ae08745Sheppo 
34551ae08745Sheppo 	for (parent = ddi_get_parent(dip); (parent != NULL) && (parent != root);
34561ae08745Sheppo 	    parent = ddi_get_parent(parent)) {
34571ae08745Sheppo 		if (strcmp(ddi_get_name(parent), DEVI_PSEUDO_NEXNAME) == 0)
34581ae08745Sheppo 			return (B_TRUE);
34591ae08745Sheppo 	}
34601ae08745Sheppo 
34611ae08745Sheppo 	return (B_FALSE);
34621ae08745Sheppo }
34631ae08745Sheppo 
34641ae08745Sheppo static int
34650a55fbb7Slm66018 vd_setup_full_disk(vd_t *vd)
34660a55fbb7Slm66018 {
34670a55fbb7Slm66018 	int		rval, status;
34680a55fbb7Slm66018 	major_t		major = getmajor(vd->dev[0]);
34690a55fbb7Slm66018 	minor_t		minor = getminor(vd->dev[0]) - VD_ENTIRE_DISK_SLICE;
34704bac2208Snarayan 	struct dk_minfo	dk_minfo;
34710a55fbb7Slm66018 
34724bac2208Snarayan 	/*
34734bac2208Snarayan 	 * At this point, vdisk_size is set to the size of partition 2 but
34744bac2208Snarayan 	 * this does not represent the size of the disk because partition 2
34754bac2208Snarayan 	 * may not cover the entire disk and its size does not include reserved
34764bac2208Snarayan 	 * blocks. So we update vdisk_size to be the size of the entire disk.
34774bac2208Snarayan 	 */
34784bac2208Snarayan 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCGMEDIAINFO,
34794bac2208Snarayan 	    (intptr_t)&dk_minfo, (vd_open_flags | FKIOCTL),
34804bac2208Snarayan 	    kcred, &rval)) != 0) {
3481690555a1Sachartre 		PRN("ldi_ioctl(DKIOCGMEDIAINFO) returned errno %d",
34824bac2208Snarayan 		    status);
34830a55fbb7Slm66018 		return (status);
34840a55fbb7Slm66018 	}
34854bac2208Snarayan 	vd->vdisk_size = dk_minfo.dki_capacity;
34860a55fbb7Slm66018 
34870a55fbb7Slm66018 	/* Set full-disk parameters */
34880a55fbb7Slm66018 	vd->vdisk_type	= VD_DISK_TYPE_DISK;
34890a55fbb7Slm66018 	vd->nslices	= (sizeof (vd->dev))/(sizeof (vd->dev[0]));
34900a55fbb7Slm66018 
34910a55fbb7Slm66018 	/* Move dev number and LDI handle to entire-disk-slice array elements */
34920a55fbb7Slm66018 	vd->dev[VD_ENTIRE_DISK_SLICE]		= vd->dev[0];
34930a55fbb7Slm66018 	vd->dev[0]				= 0;
34940a55fbb7Slm66018 	vd->ldi_handle[VD_ENTIRE_DISK_SLICE]	= vd->ldi_handle[0];
34950a55fbb7Slm66018 	vd->ldi_handle[0]			= NULL;
34960a55fbb7Slm66018 
34970a55fbb7Slm66018 	/* Initialize device numbers for remaining slices and open them */
34980a55fbb7Slm66018 	for (int slice = 0; slice < vd->nslices; slice++) {
34990a55fbb7Slm66018 		/*
35000a55fbb7Slm66018 		 * Skip the entire-disk slice, as it's already open and its
35010a55fbb7Slm66018 		 * device known
35020a55fbb7Slm66018 		 */
35030a55fbb7Slm66018 		if (slice == VD_ENTIRE_DISK_SLICE)
35040a55fbb7Slm66018 			continue;
35050a55fbb7Slm66018 		ASSERT(vd->dev[slice] == 0);
35060a55fbb7Slm66018 		ASSERT(vd->ldi_handle[slice] == NULL);
35070a55fbb7Slm66018 
35080a55fbb7Slm66018 		/*
35090a55fbb7Slm66018 		 * Construct the device number for the current slice
35100a55fbb7Slm66018 		 */
35110a55fbb7Slm66018 		vd->dev[slice] = makedevice(major, (minor + slice));
35120a55fbb7Slm66018 
35130a55fbb7Slm66018 		/*
351434683adeSsg70180 		 * Open all slices of the disk to serve them to the client.
351534683adeSsg70180 		 * Slices are opened exclusively to prevent other threads or
351634683adeSsg70180 		 * processes in the service domain from performing I/O to
351734683adeSsg70180 		 * slices being accessed by a client.  Failure to open a slice
351834683adeSsg70180 		 * results in vds not serving this disk, as the client could
351934683adeSsg70180 		 * attempt (and should be able) to access any slice immediately.
352034683adeSsg70180 		 * Any slices successfully opened before a failure will get
352134683adeSsg70180 		 * closed by vds_destroy_vd() as a result of the error returned
352234683adeSsg70180 		 * by this function.
352334683adeSsg70180 		 *
352434683adeSsg70180 		 * We need to do the open with FNDELAY so that opening an empty
352534683adeSsg70180 		 * slice does not fail.
35260a55fbb7Slm66018 		 */
35270a55fbb7Slm66018 		PR0("Opening device major %u, minor %u = slice %u",
35280a55fbb7Slm66018 		    major, minor, slice);
35290a55fbb7Slm66018 		if ((status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK,
353034683adeSsg70180 		    vd_open_flags | FNDELAY, kcred, &vd->ldi_handle[slice],
35310a55fbb7Slm66018 		    vd->vds->ldi_ident)) != 0) {
3532690555a1Sachartre 			PRN("ldi_open_by_dev() returned errno %d "
35330a55fbb7Slm66018 			    "for slice %u", status, slice);
35340a55fbb7Slm66018 			/* vds_destroy_vd() will close any open slices */
3535690555a1Sachartre 			vd->ldi_handle[slice] = NULL;
35360a55fbb7Slm66018 			return (status);
35370a55fbb7Slm66018 		}
35380a55fbb7Slm66018 	}
35390a55fbb7Slm66018 
35400a55fbb7Slm66018 	return (0);
35410a55fbb7Slm66018 }
35420a55fbb7Slm66018 
35430a55fbb7Slm66018 static int
3544*78fcd0a1Sachartre vd_setup_partition_vtoc(vd_t *vd)
3545*78fcd0a1Sachartre {
3546*78fcd0a1Sachartre 	int rval, status;
3547*78fcd0a1Sachartre 	char *device_path = vd->device_path;
3548*78fcd0a1Sachartre 
3549*78fcd0a1Sachartre 	status = ldi_ioctl(vd->ldi_handle[0], DKIOCGGEOM,
3550*78fcd0a1Sachartre 	    (intptr_t)&vd->dk_geom, (vd_open_flags | FKIOCTL), kcred, &rval);
3551*78fcd0a1Sachartre 
3552*78fcd0a1Sachartre 	if (status != 0) {
3553*78fcd0a1Sachartre 		PRN("ldi_ioctl(DKIOCGEOM) returned errno %d for %s",
3554*78fcd0a1Sachartre 		    status, device_path);
3555*78fcd0a1Sachartre 		return (status);
3556*78fcd0a1Sachartre 	}
3557*78fcd0a1Sachartre 
3558*78fcd0a1Sachartre 	/* Initialize dk_geom structure for single-slice device */
3559*78fcd0a1Sachartre 	if (vd->dk_geom.dkg_nsect == 0) {
3560*78fcd0a1Sachartre 		PRN("%s geometry claims 0 sectors per track", device_path);
3561*78fcd0a1Sachartre 		return (EIO);
3562*78fcd0a1Sachartre 	}
3563*78fcd0a1Sachartre 	if (vd->dk_geom.dkg_nhead == 0) {
3564*78fcd0a1Sachartre 		PRN("%s geometry claims 0 heads", device_path);
3565*78fcd0a1Sachartre 		return (EIO);
3566*78fcd0a1Sachartre 	}
3567*78fcd0a1Sachartre 	vd->dk_geom.dkg_ncyl = vd->vdisk_size / vd->dk_geom.dkg_nsect /
3568*78fcd0a1Sachartre 	    vd->dk_geom.dkg_nhead;
3569*78fcd0a1Sachartre 	vd->dk_geom.dkg_acyl = 0;
3570*78fcd0a1Sachartre 	vd->dk_geom.dkg_pcyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl;
3571*78fcd0a1Sachartre 
3572*78fcd0a1Sachartre 
3573*78fcd0a1Sachartre 	/* Initialize vtoc structure for single-slice device */
3574*78fcd0a1Sachartre 	bcopy(VD_VOLUME_NAME, vd->vtoc.v_volume,
3575*78fcd0a1Sachartre 	    MIN(sizeof (VD_VOLUME_NAME), sizeof (vd->vtoc.v_volume)));
3576*78fcd0a1Sachartre 	bzero(vd->vtoc.v_part, sizeof (vd->vtoc.v_part));
3577*78fcd0a1Sachartre 	vd->vtoc.v_nparts = 1;
3578*78fcd0a1Sachartre 	vd->vtoc.v_part[0].p_tag = V_UNASSIGNED;
3579*78fcd0a1Sachartre 	vd->vtoc.v_part[0].p_flag = 0;
3580*78fcd0a1Sachartre 	vd->vtoc.v_part[0].p_start = 0;
3581*78fcd0a1Sachartre 	vd->vtoc.v_part[0].p_size = vd->vdisk_size;
3582*78fcd0a1Sachartre 	bcopy(VD_ASCIILABEL, vd->vtoc.v_asciilabel,
3583*78fcd0a1Sachartre 	    MIN(sizeof (VD_ASCIILABEL), sizeof (vd->vtoc.v_asciilabel)));
3584*78fcd0a1Sachartre 
3585*78fcd0a1Sachartre 	return (0);
3586*78fcd0a1Sachartre }
3587*78fcd0a1Sachartre 
3588*78fcd0a1Sachartre static int
35894bac2208Snarayan vd_setup_partition_efi(vd_t *vd)
35904bac2208Snarayan {
35914bac2208Snarayan 	efi_gpt_t *gpt;
35924bac2208Snarayan 	efi_gpe_t *gpe;
35934bac2208Snarayan 	struct uuid uuid = EFI_RESERVED;
35944bac2208Snarayan 	uint32_t crc;
35954bac2208Snarayan 	int length;
35964bac2208Snarayan 
35974bac2208Snarayan 	length = sizeof (efi_gpt_t) + sizeof (efi_gpe_t);
35984bac2208Snarayan 
35994bac2208Snarayan 	gpt = kmem_zalloc(length, KM_SLEEP);
36004bac2208Snarayan 	gpe = (efi_gpe_t *)(gpt + 1);
36014bac2208Snarayan 
36024bac2208Snarayan 	gpt->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
36034bac2208Snarayan 	gpt->efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
36044bac2208Snarayan 	gpt->efi_gpt_HeaderSize = LE_32(sizeof (efi_gpt_t));
36054bac2208Snarayan 	gpt->efi_gpt_FirstUsableLBA = LE_64(0ULL);
36064bac2208Snarayan 	gpt->efi_gpt_LastUsableLBA = LE_64(vd->vdisk_size - 1);
36074bac2208Snarayan 	gpt->efi_gpt_NumberOfPartitionEntries = LE_32(1);
36084bac2208Snarayan 	gpt->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (efi_gpe_t));
36094bac2208Snarayan 
36104bac2208Snarayan 	UUID_LE_CONVERT(gpe->efi_gpe_PartitionTypeGUID, uuid);
36114bac2208Snarayan 	gpe->efi_gpe_StartingLBA = gpt->efi_gpt_FirstUsableLBA;
36124bac2208Snarayan 	gpe->efi_gpe_EndingLBA = gpt->efi_gpt_LastUsableLBA;
36134bac2208Snarayan 
36144bac2208Snarayan 	CRC32(crc, gpe, sizeof (efi_gpe_t), -1U, crc32_table);
36154bac2208Snarayan 	gpt->efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
36164bac2208Snarayan 
36174bac2208Snarayan 	CRC32(crc, gpt, sizeof (efi_gpt_t), -1U, crc32_table);
36184bac2208Snarayan 	gpt->efi_gpt_HeaderCRC32 = LE_32(~crc);
36194bac2208Snarayan 
36204bac2208Snarayan 	vd->dk_efi.dki_lba = 0;
36214bac2208Snarayan 	vd->dk_efi.dki_length = length;
36224bac2208Snarayan 	vd->dk_efi.dki_data = gpt;
36234bac2208Snarayan 
36244bac2208Snarayan 	return (0);
36254bac2208Snarayan }
36264bac2208Snarayan 
36274bac2208Snarayan static int
36283c96341aSnarayan vd_setup_file(vd_t *vd)
36293c96341aSnarayan {
3630*78fcd0a1Sachartre 	int 		rval, status;
36313c96341aSnarayan 	vattr_t		vattr;
36323c96341aSnarayan 	dev_t		dev;
36333c96341aSnarayan 	char		*file_path = vd->device_path;
36343c96341aSnarayan 	char		dev_path[MAXPATHLEN + 1];
36353c96341aSnarayan 	ldi_handle_t	lhandle;
36363c96341aSnarayan 	struct dk_cinfo	dk_cinfo;
36373c96341aSnarayan 
36383c96341aSnarayan 	/* make sure the file is valid */
36393c96341aSnarayan 	if ((status = lookupname(file_path, UIO_SYSSPACE, FOLLOW,
36403c96341aSnarayan 	    NULLVPP, &vd->file_vnode)) != 0) {
3641690555a1Sachartre 		PRN("Cannot lookup file(%s) errno %d", file_path, status);
36423c96341aSnarayan 		return (status);
36433c96341aSnarayan 	}
36443c96341aSnarayan 
36453c96341aSnarayan 	if (vd->file_vnode->v_type != VREG) {
3646690555a1Sachartre 		PRN("Invalid file type (%s)\n", file_path);
36473c96341aSnarayan 		VN_RELE(vd->file_vnode);
36483c96341aSnarayan 		return (EBADF);
36493c96341aSnarayan 	}
36503c96341aSnarayan 	VN_RELE(vd->file_vnode);
36513c96341aSnarayan 
36523c96341aSnarayan 	if ((status = vn_open(file_path, UIO_SYSSPACE, vd_open_flags | FOFFMAX,
36533c96341aSnarayan 	    0, &vd->file_vnode, 0, 0)) != 0) {
3654690555a1Sachartre 		PRN("vn_open(%s) = errno %d", file_path, status);
36553c96341aSnarayan 		return (status);
36563c96341aSnarayan 	}
36573c96341aSnarayan 
3658690555a1Sachartre 	/*
3659690555a1Sachartre 	 * We set vd->file now so that vds_destroy_vd will take care of
3660690555a1Sachartre 	 * closing the file and releasing the vnode in case of an error.
3661690555a1Sachartre 	 */
3662690555a1Sachartre 	vd->file = B_TRUE;
3663690555a1Sachartre 	vd->pseudo = B_FALSE;
3664690555a1Sachartre 
36653c96341aSnarayan 	vattr.va_mask = AT_SIZE;
36663c96341aSnarayan 	if ((status = VOP_GETATTR(vd->file_vnode, &vattr, 0, kcred)) != 0) {
3667690555a1Sachartre 		PRN("VOP_GETATTR(%s) = errno %d", file_path, status);
36683c96341aSnarayan 		return (EIO);
36693c96341aSnarayan 	}
36703c96341aSnarayan 
36713c96341aSnarayan 	vd->file_size = vattr.va_size;
36723c96341aSnarayan 	/* size should be at least sizeof(dk_label) */
36733c96341aSnarayan 	if (vd->file_size < sizeof (struct dk_label)) {
36743c96341aSnarayan 		PRN("Size of file has to be at least %ld bytes",
36753c96341aSnarayan 		    sizeof (struct dk_label));
36763c96341aSnarayan 		return (EIO);
36773c96341aSnarayan 	}
36783c96341aSnarayan 
3679690555a1Sachartre 	if (vd->file_vnode->v_flag & VNOMAP) {
3680690555a1Sachartre 		PRN("File %s cannot be mapped", file_path);
36813c96341aSnarayan 		return (EIO);
36823c96341aSnarayan 	}
36833c96341aSnarayan 
3684*78fcd0a1Sachartre 	/* find and validate the geometry of the disk image */
3685*78fcd0a1Sachartre 	status = vd_file_validate_geometry(vd);
3686*78fcd0a1Sachartre 	if (status != 0 && status != EINVAL) {
3687*78fcd0a1Sachartre 		PRN("Fail to read label from %s", file_path);
3688690555a1Sachartre 		return (EIO);
3689690555a1Sachartre 	}
36903c96341aSnarayan 
3691*78fcd0a1Sachartre 	vd->nslices = V_NUMPAR;
36923c96341aSnarayan 	/* sector size = block size = DEV_BSIZE */
369387a7269eSachartre 	vd->vdisk_size = vd->file_size / DEV_BSIZE;
36943c96341aSnarayan 	vd->vdisk_type = VD_DISK_TYPE_DISK;
36953c96341aSnarayan 	vd->max_xfer_sz = maxphys / DEV_BSIZE; /* default transfer size */
36963c96341aSnarayan 
36973c96341aSnarayan 	/* Get max_xfer_sz from the device where the file is */
36983c96341aSnarayan 	dev = vd->file_vnode->v_vfsp->vfs_dev;
36993c96341aSnarayan 	dev_path[0] = NULL;
37003c96341aSnarayan 	if (ddi_dev_pathname(dev, S_IFBLK, dev_path) == DDI_SUCCESS) {
37013c96341aSnarayan 		PR0("underlying device = %s\n", dev_path);
37023c96341aSnarayan 	}
37033c96341aSnarayan 
37043c96341aSnarayan 	if ((status = ldi_open_by_dev(&dev, OTYP_BLK, FREAD,
37053c96341aSnarayan 	    kcred, &lhandle, vd->vds->ldi_ident)) != 0) {
37063c96341aSnarayan 		PR0("ldi_open_by_dev() returned errno %d for device %s",
37073c96341aSnarayan 		    status, dev_path);
37083c96341aSnarayan 	} else {
37093c96341aSnarayan 		if ((status = ldi_ioctl(lhandle, DKIOCINFO,
37103c96341aSnarayan 		    (intptr_t)&dk_cinfo, (vd_open_flags | FKIOCTL), kcred,
37113c96341aSnarayan 		    &rval)) != 0) {
37123c96341aSnarayan 			PR0("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
37133c96341aSnarayan 			    status, dev_path);
37143c96341aSnarayan 		} else {
37153c96341aSnarayan 			/*
37163c96341aSnarayan 			 * Store the device's max transfer size for
37173c96341aSnarayan 			 * return to the client
37183c96341aSnarayan 			 */
37193c96341aSnarayan 			vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
37203c96341aSnarayan 		}
37213c96341aSnarayan 
37223c96341aSnarayan 		PR0("close the device %s", dev_path);
37233c96341aSnarayan 		(void) ldi_close(lhandle, FREAD, kcred);
37243c96341aSnarayan 	}
37253c96341aSnarayan 
3726205eeb1aSlm66018 	PR0("using file %s, dev %s, max_xfer = %u blks",
37273c96341aSnarayan 	    file_path, dev_path, vd->max_xfer_sz);
37283c96341aSnarayan 
372987a7269eSachartre 	/* Setup devid for the disk image */
373087a7269eSachartre 
3731*78fcd0a1Sachartre 	if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
3732*78fcd0a1Sachartre 
373387a7269eSachartre 		status = vd_file_read_devid(vd, &vd->file_devid);
373487a7269eSachartre 
373587a7269eSachartre 		if (status == 0) {
373687a7269eSachartre 			/* a valid devid was found */
373787a7269eSachartre 			return (0);
373887a7269eSachartre 		}
373987a7269eSachartre 
374087a7269eSachartre 		if (status != EINVAL) {
374187a7269eSachartre 			/*
3742*78fcd0a1Sachartre 			 * There was an error while trying to read the devid.
3743*78fcd0a1Sachartre 			 * So this disk image may have a devid but we are
3744*78fcd0a1Sachartre 			 * unable to read it.
374587a7269eSachartre 			 */
374687a7269eSachartre 			PR0("can not read devid for %s", file_path);
374787a7269eSachartre 			vd->file_devid = NULL;
374887a7269eSachartre 			return (0);
374987a7269eSachartre 		}
3750*78fcd0a1Sachartre 	}
375187a7269eSachartre 
375287a7269eSachartre 	/*
375387a7269eSachartre 	 * No valid device id was found so we create one. Note that a failure
375487a7269eSachartre 	 * to create a device id is not fatal and does not prevent the disk
375587a7269eSachartre 	 * image from being attached.
375687a7269eSachartre 	 */
375787a7269eSachartre 	PR1("creating devid for %s", file_path);
375887a7269eSachartre 
375987a7269eSachartre 	if (ddi_devid_init(vd->vds->dip, DEVID_FAB, NULL, 0,
376087a7269eSachartre 	    &vd->file_devid) != DDI_SUCCESS) {
376187a7269eSachartre 		PR0("fail to create devid for %s", file_path);
376287a7269eSachartre 		vd->file_devid = NULL;
376387a7269eSachartre 		return (0);
376487a7269eSachartre 	}
376587a7269eSachartre 
3766*78fcd0a1Sachartre 	/*
3767*78fcd0a1Sachartre 	 * Write devid to the disk image. The devid is stored into the disk
3768*78fcd0a1Sachartre 	 * image if we have a valid label; otherwise the devid will be stored
3769*78fcd0a1Sachartre 	 * when the user writes a valid label.
3770*78fcd0a1Sachartre 	 */
3771*78fcd0a1Sachartre 	if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
377287a7269eSachartre 		if (vd_file_write_devid(vd, vd->file_devid) != 0) {
377387a7269eSachartre 			PR0("fail to write devid for %s", file_path);
377487a7269eSachartre 			ddi_devid_free(vd->file_devid);
377587a7269eSachartre 			vd->file_devid = NULL;
377687a7269eSachartre 		}
3777*78fcd0a1Sachartre 	}
377887a7269eSachartre 
37793c96341aSnarayan 	return (0);
37803c96341aSnarayan }
37813c96341aSnarayan 
37823c96341aSnarayan static int
37833c96341aSnarayan vd_setup_vd(vd_t *vd)
37841ae08745Sheppo {
3785e1ebb9ecSlm66018 	int		rval, status;
37861ae08745Sheppo 	dev_info_t	*dip;
37871ae08745Sheppo 	struct dk_cinfo	dk_cinfo;
37883c96341aSnarayan 	char		*device_path = vd->device_path;
37891ae08745Sheppo 
37904bac2208Snarayan 	/*
37914bac2208Snarayan 	 * We need to open with FNDELAY so that opening an empty partition
37924bac2208Snarayan 	 * does not fail.
37934bac2208Snarayan 	 */
37944bac2208Snarayan 	if ((status = ldi_open_by_name(device_path, vd_open_flags | FNDELAY,
37954bac2208Snarayan 	    kcred, &vd->ldi_handle[0], vd->vds->ldi_ident)) != 0) {
37963c96341aSnarayan 		PR0("ldi_open_by_name(%s) = errno %d", device_path, status);
3797690555a1Sachartre 		vd->ldi_handle[0] = NULL;
37983c96341aSnarayan 
37993c96341aSnarayan 		/* this may not be a device try opening as a file */
38003c96341aSnarayan 		if (status == ENXIO || status == ENODEV)
38013c96341aSnarayan 			status = vd_setup_file(vd);
38023c96341aSnarayan 		if (status) {
3803690555a1Sachartre 			PRN("Cannot use device/file (%s), errno=%d\n",
38043c96341aSnarayan 			    device_path, status);
38053c96341aSnarayan 			if (status == ENXIO || status == ENODEV ||
3806*78fcd0a1Sachartre 			    status == ENOENT || status == EROFS) {
38073c96341aSnarayan 				return (EAGAIN);
38083c96341aSnarayan 			}
38093c96341aSnarayan 		}
38100a55fbb7Slm66018 		return (status);
38110a55fbb7Slm66018 	}
38120a55fbb7Slm66018 
38134bac2208Snarayan 	/*
38144bac2208Snarayan 	 * nslices must be updated now so that vds_destroy_vd() will close
38154bac2208Snarayan 	 * the slice we have just opened in case of an error.
38164bac2208Snarayan 	 */
38174bac2208Snarayan 	vd->nslices = 1;
38183c96341aSnarayan 	vd->file = B_FALSE;
38194bac2208Snarayan 
3820e1ebb9ecSlm66018 	/* Get device number and size of backing device */
38210a55fbb7Slm66018 	if ((status = ldi_get_dev(vd->ldi_handle[0], &vd->dev[0])) != 0) {
38221ae08745Sheppo 		PRN("ldi_get_dev() returned errno %d for %s",
3823e1ebb9ecSlm66018 		    status, device_path);
38241ae08745Sheppo 		return (status);
38251ae08745Sheppo 	}
38260a55fbb7Slm66018 	if (ldi_get_size(vd->ldi_handle[0], &vd->vdisk_size) != DDI_SUCCESS) {
3827e1ebb9ecSlm66018 		PRN("ldi_get_size() failed for %s", device_path);
38281ae08745Sheppo 		return (EIO);
38291ae08745Sheppo 	}
3830e1ebb9ecSlm66018 	vd->vdisk_size = lbtodb(vd->vdisk_size);	/* convert to blocks */
38311ae08745Sheppo 
3832*78fcd0a1Sachartre 	/* Verify backing device supports dk_cinfo */
3833e1ebb9ecSlm66018 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO,
3834e1ebb9ecSlm66018 	    (intptr_t)&dk_cinfo, (vd_open_flags | FKIOCTL), kcred,
3835e1ebb9ecSlm66018 	    &rval)) != 0) {
3836e1ebb9ecSlm66018 		PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
3837e1ebb9ecSlm66018 		    status, device_path);
3838e1ebb9ecSlm66018 		return (status);
3839e1ebb9ecSlm66018 	}
3840e1ebb9ecSlm66018 	if (dk_cinfo.dki_partition >= V_NUMPAR) {
3841e1ebb9ecSlm66018 		PRN("slice %u >= maximum slice %u for %s",
3842e1ebb9ecSlm66018 		    dk_cinfo.dki_partition, V_NUMPAR, device_path);
3843e1ebb9ecSlm66018 		return (EIO);
3844e1ebb9ecSlm66018 	}
38454bac2208Snarayan 
3846*78fcd0a1Sachartre 	vd->vdisk_label = vd_read_vtoc(vd->ldi_handle[0], &vd->vtoc);
3847e1ebb9ecSlm66018 
3848e1ebb9ecSlm66018 	/* Store the device's max transfer size for return to the client */
3849e1ebb9ecSlm66018 	vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
3850e1ebb9ecSlm66018 
3851e1ebb9ecSlm66018 	/* Determine if backing device is a pseudo device */
38521ae08745Sheppo 	if ((dip = ddi_hold_devi_by_instance(getmajor(vd->dev[0]),
38531ae08745Sheppo 	    dev_to_instance(vd->dev[0]), 0))  == NULL) {
3854e1ebb9ecSlm66018 		PRN("%s is no longer accessible", device_path);
38551ae08745Sheppo 		return (EIO);
38561ae08745Sheppo 	}
38571ae08745Sheppo 	vd->pseudo = is_pseudo_device(dip);
38581ae08745Sheppo 	ddi_release_devi(dip);
38591ae08745Sheppo 	if (vd->pseudo) {
3860*78fcd0a1Sachartre 		/*
3861*78fcd0a1Sachartre 		 * Currently we only support exporting pseudo devices which
3862*78fcd0a1Sachartre 		 * provide a valid disk label.
3863*78fcd0a1Sachartre 		 */
3864*78fcd0a1Sachartre 		if (vd->vdisk_label == VD_DISK_LABEL_UNK) {
3865*78fcd0a1Sachartre 			PRN("%s is a pseudo device with an invalid disk "
3866*78fcd0a1Sachartre 			    "label\n", device_path);
3867*78fcd0a1Sachartre 			return (EINVAL);
3868*78fcd0a1Sachartre 		}
38691ae08745Sheppo 		vd->vdisk_type	= VD_DISK_TYPE_SLICE;
38701ae08745Sheppo 		vd->nslices	= 1;
38711ae08745Sheppo 		return (0);	/* ...and we're done */
38721ae08745Sheppo 	}
38731ae08745Sheppo 
38740a55fbb7Slm66018 	/* If slice is entire-disk slice, initialize for full disk */
38750a55fbb7Slm66018 	if (dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE)
38760a55fbb7Slm66018 		return (vd_setup_full_disk(vd));
38771ae08745Sheppo 
3878*78fcd0a1Sachartre 	/* We can only export a slice if the disk has a valid label */
3879*78fcd0a1Sachartre 	if (vd->vdisk_label == VD_DISK_LABEL_UNK) {
3880*78fcd0a1Sachartre 		PRN("%s is a slice from a disk with an unknown disk label\n",
3881*78fcd0a1Sachartre 		    device_path);
3882*78fcd0a1Sachartre 		return (EINVAL);
3883*78fcd0a1Sachartre 	}
38840a55fbb7Slm66018 
3885e1ebb9ecSlm66018 	/* Otherwise, we have a non-entire slice of a device */
38861ae08745Sheppo 	vd->vdisk_type	= VD_DISK_TYPE_SLICE;
38871ae08745Sheppo 	vd->nslices	= 1;
38881ae08745Sheppo 
38894bac2208Snarayan 	if (vd->vdisk_label == VD_DISK_LABEL_EFI) {
3890*78fcd0a1Sachartre 		/* Slice from a disk with an EFI label */
38914bac2208Snarayan 		status = vd_setup_partition_efi(vd);
3892*78fcd0a1Sachartre 	} else {
3893*78fcd0a1Sachartre 		/* Slice from a disk with a VTOC label */
3894*78fcd0a1Sachartre 		ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
3895*78fcd0a1Sachartre 		status = vd_setup_partition_vtoc(vd);
3896*78fcd0a1Sachartre 	}
3897*78fcd0a1Sachartre 
38984bac2208Snarayan 	return (status);
38994bac2208Snarayan }
39001ae08745Sheppo 
39011ae08745Sheppo static int
3902e1ebb9ecSlm66018 vds_do_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t ldc_id,
39031ae08745Sheppo     vd_t **vdp)
39041ae08745Sheppo {
39051ae08745Sheppo 	char			tq_name[TASKQ_NAMELEN];
39060a55fbb7Slm66018 	int			status;
39071ae08745Sheppo 	ddi_iblock_cookie_t	iblock = NULL;
39081ae08745Sheppo 	ldc_attr_t		ldc_attr;
39091ae08745Sheppo 	vd_t			*vd;
39101ae08745Sheppo 
39111ae08745Sheppo 
39121ae08745Sheppo 	ASSERT(vds != NULL);
3913e1ebb9ecSlm66018 	ASSERT(device_path != NULL);
39141ae08745Sheppo 	ASSERT(vdp != NULL);
3915e1ebb9ecSlm66018 	PR0("Adding vdisk for %s", device_path);
39161ae08745Sheppo 
39171ae08745Sheppo 	if ((vd = kmem_zalloc(sizeof (*vd), KM_NOSLEEP)) == NULL) {
39181ae08745Sheppo 		PRN("No memory for virtual disk");
39191ae08745Sheppo 		return (EAGAIN);
39201ae08745Sheppo 	}
39211ae08745Sheppo 	*vdp = vd;	/* assign here so vds_destroy_vd() can cleanup later */
39221ae08745Sheppo 	vd->vds = vds;
39233c96341aSnarayan 	(void) strncpy(vd->device_path, device_path, MAXPATHLEN);
39241ae08745Sheppo 
39250a55fbb7Slm66018 	/* Open vdisk and initialize parameters */
39263c96341aSnarayan 	if ((status = vd_setup_vd(vd)) == 0) {
39273c96341aSnarayan 		vd->initialized |= VD_DISK_READY;
39281ae08745Sheppo 
39293c96341aSnarayan 		ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
39303c96341aSnarayan 		PR0("vdisk_type = %s, pseudo = %s, file = %s, nslices = %u",
39313c96341aSnarayan 		    ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
39323c96341aSnarayan 		    (vd->pseudo ? "yes" : "no"), (vd->file ? "yes" : "no"),
39333c96341aSnarayan 		    vd->nslices);
39343c96341aSnarayan 	} else {
39353c96341aSnarayan 		if (status != EAGAIN)
39363c96341aSnarayan 			return (status);
39373c96341aSnarayan 	}
39381ae08745Sheppo 
39391ae08745Sheppo 	/* Initialize locking */
39401ae08745Sheppo 	if (ddi_get_soft_iblock_cookie(vds->dip, DDI_SOFTINT_MED,
39411ae08745Sheppo 	    &iblock) != DDI_SUCCESS) {
39421ae08745Sheppo 		PRN("Could not get iblock cookie.");
39431ae08745Sheppo 		return (EIO);
39441ae08745Sheppo 	}
39451ae08745Sheppo 
39461ae08745Sheppo 	mutex_init(&vd->lock, NULL, MUTEX_DRIVER, iblock);
39471ae08745Sheppo 	vd->initialized |= VD_LOCKING;
39481ae08745Sheppo 
39491ae08745Sheppo 
3950d10e4ef2Snarayan 	/* Create start and completion task queues for the vdisk */
3951d10e4ef2Snarayan 	(void) snprintf(tq_name, sizeof (tq_name), "vd_startq%lu", id);
39521ae08745Sheppo 	PR1("tq_name = %s", tq_name);
3953d10e4ef2Snarayan 	if ((vd->startq = ddi_taskq_create(vds->dip, tq_name, 1,
39541ae08745Sheppo 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
39551ae08745Sheppo 		PRN("Could not create task queue");
39561ae08745Sheppo 		return (EIO);
39571ae08745Sheppo 	}
3958d10e4ef2Snarayan 	(void) snprintf(tq_name, sizeof (tq_name), "vd_completionq%lu", id);
3959d10e4ef2Snarayan 	PR1("tq_name = %s", tq_name);
3960d10e4ef2Snarayan 	if ((vd->completionq = ddi_taskq_create(vds->dip, tq_name, 1,
3961d10e4ef2Snarayan 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
3962d10e4ef2Snarayan 		PRN("Could not create task queue");
3963d10e4ef2Snarayan 		return (EIO);
3964d10e4ef2Snarayan 	}
3965d10e4ef2Snarayan 	vd->enabled = 1;	/* before callback can dispatch to startq */
39661ae08745Sheppo 
39671ae08745Sheppo 
39681ae08745Sheppo 	/* Bring up LDC */
39691ae08745Sheppo 	ldc_attr.devclass	= LDC_DEV_BLK_SVC;
39701ae08745Sheppo 	ldc_attr.instance	= ddi_get_instance(vds->dip);
39711ae08745Sheppo 	ldc_attr.mode		= LDC_MODE_UNRELIABLE;
3972e1ebb9ecSlm66018 	ldc_attr.mtu		= VD_LDC_MTU;
39731ae08745Sheppo 	if ((status = ldc_init(ldc_id, &ldc_attr, &vd->ldc_handle)) != 0) {
3974690555a1Sachartre 		PRN("Could not initialize LDC channel %lu, "
3975690555a1Sachartre 		    "init failed with error %d", ldc_id, status);
39761ae08745Sheppo 		return (status);
39771ae08745Sheppo 	}
39781ae08745Sheppo 	vd->initialized |= VD_LDC;
39791ae08745Sheppo 
39801ae08745Sheppo 	if ((status = ldc_reg_callback(vd->ldc_handle, vd_handle_ldc_events,
39811ae08745Sheppo 	    (caddr_t)vd)) != 0) {
3982690555a1Sachartre 		PRN("Could not initialize LDC channel %lu,"
3983690555a1Sachartre 		    "reg_callback failed with error %d", ldc_id, status);
39841ae08745Sheppo 		return (status);
39851ae08745Sheppo 	}
39861ae08745Sheppo 
39871ae08745Sheppo 	if ((status = ldc_open(vd->ldc_handle)) != 0) {
3988690555a1Sachartre 		PRN("Could not initialize LDC channel %lu,"
3989690555a1Sachartre 		    "open failed with error %d", ldc_id, status);
39901ae08745Sheppo 		return (status);
39911ae08745Sheppo 	}
39921ae08745Sheppo 
39933af08d82Slm66018 	if ((status = ldc_up(vd->ldc_handle)) != 0) {
399434683adeSsg70180 		PR0("ldc_up() returned errno %d", status);
39953af08d82Slm66018 	}
39963af08d82Slm66018 
39974bac2208Snarayan 	/* Allocate the inband task memory handle */
39984bac2208Snarayan 	status = ldc_mem_alloc_handle(vd->ldc_handle, &(vd->inband_task.mhdl));
39994bac2208Snarayan 	if (status) {
4000690555a1Sachartre 		PRN("Could not initialize LDC channel %lu,"
4001690555a1Sachartre 		    "alloc_handle failed with error %d", ldc_id, status);
40024bac2208Snarayan 		return (ENXIO);
40034bac2208Snarayan 	}
40041ae08745Sheppo 
40051ae08745Sheppo 	/* Add the successfully-initialized vdisk to the server's table */
40061ae08745Sheppo 	if (mod_hash_insert(vds->vd_table, (mod_hash_key_t)id, vd) != 0) {
40071ae08745Sheppo 		PRN("Error adding vdisk ID %lu to table", id);
40081ae08745Sheppo 		return (EIO);
40091ae08745Sheppo 	}
40101ae08745Sheppo 
40113af08d82Slm66018 	/* Allocate the staging buffer */
40123af08d82Slm66018 	vd->max_msglen	= sizeof (vio_msg_t);	/* baseline vio message size */
40133af08d82Slm66018 	vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
40143af08d82Slm66018 
40153af08d82Slm66018 	/* store initial state */
40163af08d82Slm66018 	vd->state = VD_STATE_INIT;
40173af08d82Slm66018 
40181ae08745Sheppo 	return (0);
40191ae08745Sheppo }
40201ae08745Sheppo 
40213af08d82Slm66018 static void
40223af08d82Slm66018 vd_free_dring_task(vd_t *vdp)
40233af08d82Slm66018 {
40243af08d82Slm66018 	if (vdp->dring_task != NULL) {
40253af08d82Slm66018 		ASSERT(vdp->dring_len != 0);
40263af08d82Slm66018 		/* Free all dring_task memory handles */
40273af08d82Slm66018 		for (int i = 0; i < vdp->dring_len; i++) {
40283af08d82Slm66018 			(void) ldc_mem_free_handle(vdp->dring_task[i].mhdl);
40293af08d82Slm66018 			kmem_free(vdp->dring_task[i].msg, vdp->max_msglen);
40303af08d82Slm66018 			vdp->dring_task[i].msg = NULL;
40313af08d82Slm66018 		}
40323af08d82Slm66018 		kmem_free(vdp->dring_task,
40333af08d82Slm66018 		    (sizeof (*vdp->dring_task)) * vdp->dring_len);
40343af08d82Slm66018 		vdp->dring_task = NULL;
40353af08d82Slm66018 	}
40363af08d82Slm66018 }
40373af08d82Slm66018 
40381ae08745Sheppo /*
40391ae08745Sheppo  * Destroy the state associated with a virtual disk
40401ae08745Sheppo  */
40411ae08745Sheppo static void
40421ae08745Sheppo vds_destroy_vd(void *arg)
40431ae08745Sheppo {
40441ae08745Sheppo 	vd_t	*vd = (vd_t *)arg;
404534683adeSsg70180 	int	retry = 0, rv;
40461ae08745Sheppo 
40471ae08745Sheppo 	if (vd == NULL)
40481ae08745Sheppo 		return;
40491ae08745Sheppo 
4050d10e4ef2Snarayan 	PR0("Destroying vdisk state");
4051d10e4ef2Snarayan 
40524bac2208Snarayan 	if (vd->dk_efi.dki_data != NULL)
40534bac2208Snarayan 		kmem_free(vd->dk_efi.dki_data, vd->dk_efi.dki_length);
40544bac2208Snarayan 
40551ae08745Sheppo 	/* Disable queuing requests for the vdisk */
40561ae08745Sheppo 	if (vd->initialized & VD_LOCKING) {
40571ae08745Sheppo 		mutex_enter(&vd->lock);
40581ae08745Sheppo 		vd->enabled = 0;
40591ae08745Sheppo 		mutex_exit(&vd->lock);
40601ae08745Sheppo 	}
40611ae08745Sheppo 
4062d10e4ef2Snarayan 	/* Drain and destroy start queue (*before* destroying completionq) */
4063d10e4ef2Snarayan 	if (vd->startq != NULL)
4064d10e4ef2Snarayan 		ddi_taskq_destroy(vd->startq);	/* waits for queued tasks */
4065d10e4ef2Snarayan 
4066d10e4ef2Snarayan 	/* Drain and destroy completion queue (*before* shutting down LDC) */
4067d10e4ef2Snarayan 	if (vd->completionq != NULL)
4068d10e4ef2Snarayan 		ddi_taskq_destroy(vd->completionq);	/* waits for tasks */
4069d10e4ef2Snarayan 
40703af08d82Slm66018 	vd_free_dring_task(vd);
40713af08d82Slm66018 
407234683adeSsg70180 	/* Free the inband task memory handle */
407334683adeSsg70180 	(void) ldc_mem_free_handle(vd->inband_task.mhdl);
407434683adeSsg70180 
407534683adeSsg70180 	/* Shut down LDC */
407634683adeSsg70180 	if (vd->initialized & VD_LDC) {
407734683adeSsg70180 		/* unmap the dring */
407834683adeSsg70180 		if (vd->initialized & VD_DRING)
407934683adeSsg70180 			(void) ldc_mem_dring_unmap(vd->dring_handle);
408034683adeSsg70180 
408134683adeSsg70180 		/* close LDC channel - retry on EAGAIN */
408234683adeSsg70180 		while ((rv = ldc_close(vd->ldc_handle)) == EAGAIN) {
408334683adeSsg70180 			if (++retry > vds_ldc_retries) {
408434683adeSsg70180 				PR0("Timed out closing channel");
408534683adeSsg70180 				break;
408634683adeSsg70180 			}
408734683adeSsg70180 			drv_usecwait(vds_ldc_delay);
408834683adeSsg70180 		}
408934683adeSsg70180 		if (rv == 0) {
409034683adeSsg70180 			(void) ldc_unreg_callback(vd->ldc_handle);
409134683adeSsg70180 			(void) ldc_fini(vd->ldc_handle);
409234683adeSsg70180 		} else {
409334683adeSsg70180 			/*
409434683adeSsg70180 			 * Closing the LDC channel has failed. Ideally we should
409534683adeSsg70180 			 * fail here but there is no Zeus level infrastructure
409634683adeSsg70180 			 * to handle this. The MD has already been changed and
409734683adeSsg70180 			 * we have to do the close. So we try to do as much
409834683adeSsg70180 			 * clean up as we can.
409934683adeSsg70180 			 */
410034683adeSsg70180 			(void) ldc_set_cb_mode(vd->ldc_handle, LDC_CB_DISABLE);
410134683adeSsg70180 			while (ldc_unreg_callback(vd->ldc_handle) == EAGAIN)
410234683adeSsg70180 				drv_usecwait(vds_ldc_delay);
410334683adeSsg70180 		}
410434683adeSsg70180 	}
410534683adeSsg70180 
41063af08d82Slm66018 	/* Free the staging buffer for msgs */
41073af08d82Slm66018 	if (vd->vio_msgp != NULL) {
41083af08d82Slm66018 		kmem_free(vd->vio_msgp, vd->max_msglen);
41093af08d82Slm66018 		vd->vio_msgp = NULL;
41103af08d82Slm66018 	}
41113af08d82Slm66018 
41123af08d82Slm66018 	/* Free the inband message buffer */
41133af08d82Slm66018 	if (vd->inband_task.msg != NULL) {
41143af08d82Slm66018 		kmem_free(vd->inband_task.msg, vd->max_msglen);
41153af08d82Slm66018 		vd->inband_task.msg = NULL;
4116d10e4ef2Snarayan 	}
41173c96341aSnarayan 	if (vd->file) {
4118690555a1Sachartre 		/* Close file */
41193c96341aSnarayan 		(void) VOP_CLOSE(vd->file_vnode, vd_open_flags, 1,
41203c96341aSnarayan 		    0, kcred);
41213c96341aSnarayan 		VN_RELE(vd->file_vnode);
412287a7269eSachartre 		if (vd->file_devid != NULL)
412387a7269eSachartre 			ddi_devid_free(vd->file_devid);
41243c96341aSnarayan 	} else {
41251ae08745Sheppo 		/* Close any open backing-device slices */
41261ae08745Sheppo 		for (uint_t slice = 0; slice < vd->nslices; slice++) {
41271ae08745Sheppo 			if (vd->ldi_handle[slice] != NULL) {
41281ae08745Sheppo 				PR0("Closing slice %u", slice);
41291ae08745Sheppo 				(void) ldi_close(vd->ldi_handle[slice],
41304bac2208Snarayan 				    vd_open_flags | FNDELAY, kcred);
41311ae08745Sheppo 			}
41321ae08745Sheppo 		}
41333c96341aSnarayan 	}
41341ae08745Sheppo 
41351ae08745Sheppo 	/* Free lock */
41361ae08745Sheppo 	if (vd->initialized & VD_LOCKING)
41371ae08745Sheppo 		mutex_destroy(&vd->lock);
41381ae08745Sheppo 
41391ae08745Sheppo 	/* Finally, free the vdisk structure itself */
41401ae08745Sheppo 	kmem_free(vd, sizeof (*vd));
41411ae08745Sheppo }
41421ae08745Sheppo 
41431ae08745Sheppo static int
4144e1ebb9ecSlm66018 vds_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t ldc_id)
41451ae08745Sheppo {
41461ae08745Sheppo 	int	status;
41471ae08745Sheppo 	vd_t	*vd = NULL;
41481ae08745Sheppo 
41491ae08745Sheppo 
4150e1ebb9ecSlm66018 	if ((status = vds_do_init_vd(vds, id, device_path, ldc_id, &vd)) != 0)
41511ae08745Sheppo 		vds_destroy_vd(vd);
41521ae08745Sheppo 
41531ae08745Sheppo 	return (status);
41541ae08745Sheppo }
41551ae08745Sheppo 
41561ae08745Sheppo static int
41571ae08745Sheppo vds_do_get_ldc_id(md_t *md, mde_cookie_t vd_node, mde_cookie_t *channel,
41581ae08745Sheppo     uint64_t *ldc_id)
41591ae08745Sheppo {
41601ae08745Sheppo 	int	num_channels;
41611ae08745Sheppo 
41621ae08745Sheppo 
41631ae08745Sheppo 	/* Look for channel endpoint child(ren) of the vdisk MD node */
41641ae08745Sheppo 	if ((num_channels = md_scan_dag(md, vd_node,
41651ae08745Sheppo 	    md_find_name(md, VD_CHANNEL_ENDPOINT),
41661ae08745Sheppo 	    md_find_name(md, "fwd"), channel)) <= 0) {
41671ae08745Sheppo 		PRN("No \"%s\" found for virtual disk", VD_CHANNEL_ENDPOINT);
41681ae08745Sheppo 		return (-1);
41691ae08745Sheppo 	}
41701ae08745Sheppo 
41711ae08745Sheppo 	/* Get the "id" value for the first channel endpoint node */
41721ae08745Sheppo 	if (md_get_prop_val(md, channel[0], VD_ID_PROP, ldc_id) != 0) {
41731ae08745Sheppo 		PRN("No \"%s\" property found for \"%s\" of vdisk",
41741ae08745Sheppo 		    VD_ID_PROP, VD_CHANNEL_ENDPOINT);
41751ae08745Sheppo 		return (-1);
41761ae08745Sheppo 	}
41771ae08745Sheppo 
41781ae08745Sheppo 	if (num_channels > 1) {
41791ae08745Sheppo 		PRN("Using ID of first of multiple channels for this vdisk");
41801ae08745Sheppo 	}
41811ae08745Sheppo 
41821ae08745Sheppo 	return (0);
41831ae08745Sheppo }
41841ae08745Sheppo 
41851ae08745Sheppo static int
41861ae08745Sheppo vds_get_ldc_id(md_t *md, mde_cookie_t vd_node, uint64_t *ldc_id)
41871ae08745Sheppo {
41881ae08745Sheppo 	int		num_nodes, status;
41891ae08745Sheppo 	size_t		size;
41901ae08745Sheppo 	mde_cookie_t	*channel;
41911ae08745Sheppo 
41921ae08745Sheppo 
41931ae08745Sheppo 	if ((num_nodes = md_node_count(md)) <= 0) {
41941ae08745Sheppo 		PRN("Invalid node count in Machine Description subtree");
41951ae08745Sheppo 		return (-1);
41961ae08745Sheppo 	}
41971ae08745Sheppo 	size = num_nodes*(sizeof (*channel));
41981ae08745Sheppo 	channel = kmem_zalloc(size, KM_SLEEP);
41991ae08745Sheppo 	status = vds_do_get_ldc_id(md, vd_node, channel, ldc_id);
42001ae08745Sheppo 	kmem_free(channel, size);
42011ae08745Sheppo 
42021ae08745Sheppo 	return (status);
42031ae08745Sheppo }
42041ae08745Sheppo 
42051ae08745Sheppo static void
42061ae08745Sheppo vds_add_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
42071ae08745Sheppo {
4208e1ebb9ecSlm66018 	char		*device_path = NULL;
42091ae08745Sheppo 	uint64_t	id = 0, ldc_id = 0;
42101ae08745Sheppo 
42111ae08745Sheppo 
42121ae08745Sheppo 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
42131ae08745Sheppo 		PRN("Error getting vdisk \"%s\"", VD_ID_PROP);
42141ae08745Sheppo 		return;
42151ae08745Sheppo 	}
42161ae08745Sheppo 	PR0("Adding vdisk ID %lu", id);
42171ae08745Sheppo 	if (md_get_prop_str(md, vd_node, VD_BLOCK_DEVICE_PROP,
4218e1ebb9ecSlm66018 	    &device_path) != 0) {
42191ae08745Sheppo 		PRN("Error getting vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
42201ae08745Sheppo 		return;
42211ae08745Sheppo 	}
42221ae08745Sheppo 
42231ae08745Sheppo 	if (vds_get_ldc_id(md, vd_node, &ldc_id) != 0) {
42241ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", id);
42251ae08745Sheppo 		return;
42261ae08745Sheppo 	}
42271ae08745Sheppo 
4228e1ebb9ecSlm66018 	if (vds_init_vd(vds, id, device_path, ldc_id) != 0) {
42291ae08745Sheppo 		PRN("Failed to add vdisk ID %lu", id);
42301ae08745Sheppo 		return;
42311ae08745Sheppo 	}
42321ae08745Sheppo }
42331ae08745Sheppo 
42341ae08745Sheppo static void
42351ae08745Sheppo vds_remove_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
42361ae08745Sheppo {
42371ae08745Sheppo 	uint64_t	id = 0;
42381ae08745Sheppo 
42391ae08745Sheppo 
42401ae08745Sheppo 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
42411ae08745Sheppo 		PRN("Unable to get \"%s\" property from vdisk's MD node",
42421ae08745Sheppo 		    VD_ID_PROP);
42431ae08745Sheppo 		return;
42441ae08745Sheppo 	}
42451ae08745Sheppo 	PR0("Removing vdisk ID %lu", id);
42461ae08745Sheppo 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0)
42471ae08745Sheppo 		PRN("No vdisk entry found for vdisk ID %lu", id);
42481ae08745Sheppo }
42491ae08745Sheppo 
42501ae08745Sheppo static void
42511ae08745Sheppo vds_change_vd(vds_t *vds, md_t *prev_md, mde_cookie_t prev_vd_node,
42521ae08745Sheppo     md_t *curr_md, mde_cookie_t curr_vd_node)
42531ae08745Sheppo {
42541ae08745Sheppo 	char		*curr_dev, *prev_dev;
42551ae08745Sheppo 	uint64_t	curr_id = 0, curr_ldc_id = 0;
42561ae08745Sheppo 	uint64_t	prev_id = 0, prev_ldc_id = 0;
42571ae08745Sheppo 	size_t		len;
42581ae08745Sheppo 
42591ae08745Sheppo 
42601ae08745Sheppo 	/* Validate that vdisk ID has not changed */
42611ae08745Sheppo 	if (md_get_prop_val(prev_md, prev_vd_node, VD_ID_PROP, &prev_id) != 0) {
42621ae08745Sheppo 		PRN("Error getting previous vdisk \"%s\" property",
42631ae08745Sheppo 		    VD_ID_PROP);
42641ae08745Sheppo 		return;
42651ae08745Sheppo 	}
42661ae08745Sheppo 	if (md_get_prop_val(curr_md, curr_vd_node, VD_ID_PROP, &curr_id) != 0) {
42671ae08745Sheppo 		PRN("Error getting current vdisk \"%s\" property", VD_ID_PROP);
42681ae08745Sheppo 		return;
42691ae08745Sheppo 	}
42701ae08745Sheppo 	if (curr_id != prev_id) {
42711ae08745Sheppo 		PRN("Not changing vdisk:  ID changed from %lu to %lu",
42721ae08745Sheppo 		    prev_id, curr_id);
42731ae08745Sheppo 		return;
42741ae08745Sheppo 	}
42751ae08745Sheppo 
42761ae08745Sheppo 	/* Validate that LDC ID has not changed */
42771ae08745Sheppo 	if (vds_get_ldc_id(prev_md, prev_vd_node, &prev_ldc_id) != 0) {
42781ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", prev_id);
42791ae08745Sheppo 		return;
42801ae08745Sheppo 	}
42811ae08745Sheppo 
42821ae08745Sheppo 	if (vds_get_ldc_id(curr_md, curr_vd_node, &curr_ldc_id) != 0) {
42831ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", curr_id);
42841ae08745Sheppo 		return;
42851ae08745Sheppo 	}
42861ae08745Sheppo 	if (curr_ldc_id != prev_ldc_id) {
42870a55fbb7Slm66018 		_NOTE(NOTREACHED);	/* lint is confused */
42881ae08745Sheppo 		PRN("Not changing vdisk:  "
42891ae08745Sheppo 		    "LDC ID changed from %lu to %lu", prev_ldc_id, curr_ldc_id);
42901ae08745Sheppo 		return;
42911ae08745Sheppo 	}
42921ae08745Sheppo 
42931ae08745Sheppo 	/* Determine whether device path has changed */
42941ae08745Sheppo 	if (md_get_prop_str(prev_md, prev_vd_node, VD_BLOCK_DEVICE_PROP,
42951ae08745Sheppo 	    &prev_dev) != 0) {
42961ae08745Sheppo 		PRN("Error getting previous vdisk \"%s\"",
42971ae08745Sheppo 		    VD_BLOCK_DEVICE_PROP);
42981ae08745Sheppo 		return;
42991ae08745Sheppo 	}
43001ae08745Sheppo 	if (md_get_prop_str(curr_md, curr_vd_node, VD_BLOCK_DEVICE_PROP,
43011ae08745Sheppo 	    &curr_dev) != 0) {
43021ae08745Sheppo 		PRN("Error getting current vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
43031ae08745Sheppo 		return;
43041ae08745Sheppo 	}
43051ae08745Sheppo 	if (((len = strlen(curr_dev)) == strlen(prev_dev)) &&
43061ae08745Sheppo 	    (strncmp(curr_dev, prev_dev, len) == 0))
43071ae08745Sheppo 		return;	/* no relevant (supported) change */
43081ae08745Sheppo 
43091ae08745Sheppo 	PR0("Changing vdisk ID %lu", prev_id);
43103af08d82Slm66018 
43111ae08745Sheppo 	/* Remove old state, which will close vdisk and reset */
43121ae08745Sheppo 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)prev_id) != 0)
43131ae08745Sheppo 		PRN("No entry found for vdisk ID %lu", prev_id);
43143af08d82Slm66018 
43151ae08745Sheppo 	/* Re-initialize vdisk with new state */
43161ae08745Sheppo 	if (vds_init_vd(vds, curr_id, curr_dev, curr_ldc_id) != 0) {
43171ae08745Sheppo 		PRN("Failed to change vdisk ID %lu", curr_id);
43181ae08745Sheppo 		return;
43191ae08745Sheppo 	}
43201ae08745Sheppo }
43211ae08745Sheppo 
43221ae08745Sheppo static int
43231ae08745Sheppo vds_process_md(void *arg, mdeg_result_t *md)
43241ae08745Sheppo {
43251ae08745Sheppo 	int	i;
43261ae08745Sheppo 	vds_t	*vds = arg;
43271ae08745Sheppo 
43281ae08745Sheppo 
43291ae08745Sheppo 	if (md == NULL)
43301ae08745Sheppo 		return (MDEG_FAILURE);
43311ae08745Sheppo 	ASSERT(vds != NULL);
43321ae08745Sheppo 
43331ae08745Sheppo 	for (i = 0; i < md->removed.nelem; i++)
43341ae08745Sheppo 		vds_remove_vd(vds, md->removed.mdp, md->removed.mdep[i]);
43351ae08745Sheppo 	for (i = 0; i < md->match_curr.nelem; i++)
43361ae08745Sheppo 		vds_change_vd(vds, md->match_prev.mdp, md->match_prev.mdep[i],
43371ae08745Sheppo 		    md->match_curr.mdp, md->match_curr.mdep[i]);
43381ae08745Sheppo 	for (i = 0; i < md->added.nelem; i++)
43391ae08745Sheppo 		vds_add_vd(vds, md->added.mdp, md->added.mdep[i]);
43401ae08745Sheppo 
43411ae08745Sheppo 	return (MDEG_SUCCESS);
43421ae08745Sheppo }
43431ae08745Sheppo 
43443c96341aSnarayan 
43451ae08745Sheppo static int
43461ae08745Sheppo vds_do_attach(dev_info_t *dip)
43471ae08745Sheppo {
4348445b4c2eSsb155480 	int			status, sz;
4349445b4c2eSsb155480 	int			cfg_handle;
43501ae08745Sheppo 	minor_t			instance = ddi_get_instance(dip);
43511ae08745Sheppo 	vds_t			*vds;
4352445b4c2eSsb155480 	mdeg_prop_spec_t	*pspecp;
4353445b4c2eSsb155480 	mdeg_node_spec_t	*ispecp;
43541ae08745Sheppo 
43551ae08745Sheppo 	/*
43561ae08745Sheppo 	 * The "cfg-handle" property of a vds node in an MD contains the MD's
43571ae08745Sheppo 	 * notion of "instance", or unique identifier, for that node; OBP
43581ae08745Sheppo 	 * stores the value of the "cfg-handle" MD property as the value of
43591ae08745Sheppo 	 * the "reg" property on the node in the device tree it builds from
43601ae08745Sheppo 	 * the MD and passes to Solaris.  Thus, we look up the devinfo node's
43611ae08745Sheppo 	 * "reg" property value to uniquely identify this device instance when
43621ae08745Sheppo 	 * registering with the MD event-generation framework.  If the "reg"
43631ae08745Sheppo 	 * property cannot be found, the device tree state is presumably so
43641ae08745Sheppo 	 * broken that there is no point in continuing.
43651ae08745Sheppo 	 */
4366445b4c2eSsb155480 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
4367445b4c2eSsb155480 	    VD_REG_PROP)) {
4368445b4c2eSsb155480 		PRN("vds \"%s\" property does not exist", VD_REG_PROP);
43691ae08745Sheppo 		return (DDI_FAILURE);
43701ae08745Sheppo 	}
43711ae08745Sheppo 
43721ae08745Sheppo 	/* Get the MD instance for later MDEG registration */
43731ae08745Sheppo 	cfg_handle = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
4374445b4c2eSsb155480 	    VD_REG_PROP, -1);
43751ae08745Sheppo 
43761ae08745Sheppo 	if (ddi_soft_state_zalloc(vds_state, instance) != DDI_SUCCESS) {
43771ae08745Sheppo 		PRN("Could not allocate state for instance %u", instance);
43781ae08745Sheppo 		return (DDI_FAILURE);
43791ae08745Sheppo 	}
43801ae08745Sheppo 
43811ae08745Sheppo 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
43821ae08745Sheppo 		PRN("Could not get state for instance %u", instance);
43831ae08745Sheppo 		ddi_soft_state_free(vds_state, instance);
43841ae08745Sheppo 		return (DDI_FAILURE);
43851ae08745Sheppo 	}
43861ae08745Sheppo 
43871ae08745Sheppo 	vds->dip	= dip;
43881ae08745Sheppo 	vds->vd_table	= mod_hash_create_ptrhash("vds_vd_table", VDS_NCHAINS,
438987a7269eSachartre 	    vds_destroy_vd, sizeof (void *));
439087a7269eSachartre 
43911ae08745Sheppo 	ASSERT(vds->vd_table != NULL);
43921ae08745Sheppo 
43931ae08745Sheppo 	if ((status = ldi_ident_from_dip(dip, &vds->ldi_ident)) != 0) {
43941ae08745Sheppo 		PRN("ldi_ident_from_dip() returned errno %d", status);
43951ae08745Sheppo 		return (DDI_FAILURE);
43961ae08745Sheppo 	}
43971ae08745Sheppo 	vds->initialized |= VDS_LDI;
43981ae08745Sheppo 
43991ae08745Sheppo 	/* Register for MD updates */
4400445b4c2eSsb155480 	sz = sizeof (vds_prop_template);
4401445b4c2eSsb155480 	pspecp = kmem_alloc(sz, KM_SLEEP);
4402445b4c2eSsb155480 	bcopy(vds_prop_template, pspecp, sz);
4403445b4c2eSsb155480 
4404445b4c2eSsb155480 	VDS_SET_MDEG_PROP_INST(pspecp, cfg_handle);
4405445b4c2eSsb155480 
4406445b4c2eSsb155480 	/* initialize the complete prop spec structure */
4407445b4c2eSsb155480 	ispecp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP);
4408445b4c2eSsb155480 	ispecp->namep = "virtual-device";
4409445b4c2eSsb155480 	ispecp->specp = pspecp;
4410445b4c2eSsb155480 
4411445b4c2eSsb155480 	if (mdeg_register(ispecp, &vd_match, vds_process_md, vds,
44121ae08745Sheppo 	    &vds->mdeg) != MDEG_SUCCESS) {
44131ae08745Sheppo 		PRN("Unable to register for MD updates");
4414445b4c2eSsb155480 		kmem_free(ispecp, sizeof (mdeg_node_spec_t));
4415445b4c2eSsb155480 		kmem_free(pspecp, sz);
44161ae08745Sheppo 		return (DDI_FAILURE);
44171ae08745Sheppo 	}
4418445b4c2eSsb155480 
4419445b4c2eSsb155480 	vds->ispecp = ispecp;
44201ae08745Sheppo 	vds->initialized |= VDS_MDEG;
44211ae08745Sheppo 
44220a55fbb7Slm66018 	/* Prevent auto-detaching so driver is available whenever MD changes */
44230a55fbb7Slm66018 	if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1) !=
44240a55fbb7Slm66018 	    DDI_PROP_SUCCESS) {
44250a55fbb7Slm66018 		PRN("failed to set \"%s\" property for instance %u",
44260a55fbb7Slm66018 		    DDI_NO_AUTODETACH, instance);
44270a55fbb7Slm66018 	}
44280a55fbb7Slm66018 
44291ae08745Sheppo 	ddi_report_dev(dip);
44301ae08745Sheppo 	return (DDI_SUCCESS);
44311ae08745Sheppo }
44321ae08745Sheppo 
44331ae08745Sheppo static int
44341ae08745Sheppo vds_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
44351ae08745Sheppo {
44361ae08745Sheppo 	int	status;
44371ae08745Sheppo 
44381ae08745Sheppo 	switch (cmd) {
44391ae08745Sheppo 	case DDI_ATTACH:
4440d10e4ef2Snarayan 		PR0("Attaching");
44411ae08745Sheppo 		if ((status = vds_do_attach(dip)) != DDI_SUCCESS)
44421ae08745Sheppo 			(void) vds_detach(dip, DDI_DETACH);
44431ae08745Sheppo 		return (status);
44441ae08745Sheppo 	case DDI_RESUME:
4445d10e4ef2Snarayan 		PR0("No action required for DDI_RESUME");
44461ae08745Sheppo 		return (DDI_SUCCESS);
44471ae08745Sheppo 	default:
44481ae08745Sheppo 		return (DDI_FAILURE);
44491ae08745Sheppo 	}
44501ae08745Sheppo }
44511ae08745Sheppo 
44521ae08745Sheppo static struct dev_ops vds_ops = {
44531ae08745Sheppo 	DEVO_REV,	/* devo_rev */
44541ae08745Sheppo 	0,		/* devo_refcnt */
44551ae08745Sheppo 	ddi_no_info,	/* devo_getinfo */
44561ae08745Sheppo 	nulldev,	/* devo_identify */
44571ae08745Sheppo 	nulldev,	/* devo_probe */
44581ae08745Sheppo 	vds_attach,	/* devo_attach */
44591ae08745Sheppo 	vds_detach,	/* devo_detach */
44601ae08745Sheppo 	nodev,		/* devo_reset */
44611ae08745Sheppo 	NULL,		/* devo_cb_ops */
44621ae08745Sheppo 	NULL,		/* devo_bus_ops */
44631ae08745Sheppo 	nulldev		/* devo_power */
44641ae08745Sheppo };
44651ae08745Sheppo 
44661ae08745Sheppo static struct modldrv modldrv = {
44671ae08745Sheppo 	&mod_driverops,
4468205eeb1aSlm66018 	"virtual disk server",
44691ae08745Sheppo 	&vds_ops,
44701ae08745Sheppo };
44711ae08745Sheppo 
44721ae08745Sheppo static struct modlinkage modlinkage = {
44731ae08745Sheppo 	MODREV_1,
44741ae08745Sheppo 	&modldrv,
44751ae08745Sheppo 	NULL
44761ae08745Sheppo };
44771ae08745Sheppo 
44781ae08745Sheppo 
44791ae08745Sheppo int
44801ae08745Sheppo _init(void)
44811ae08745Sheppo {
44821ae08745Sheppo 	int		i, status;
44831ae08745Sheppo 
4484d10e4ef2Snarayan 
44851ae08745Sheppo 	if ((status = ddi_soft_state_init(&vds_state, sizeof (vds_t), 1)) != 0)
44861ae08745Sheppo 		return (status);
44871ae08745Sheppo 	if ((status = mod_install(&modlinkage)) != 0) {
44881ae08745Sheppo 		ddi_soft_state_fini(&vds_state);
44891ae08745Sheppo 		return (status);
44901ae08745Sheppo 	}
44911ae08745Sheppo 
44921ae08745Sheppo 	/* Fill in the bit-mask of server-supported operations */
44931ae08745Sheppo 	for (i = 0; i < vds_noperations; i++)
44941ae08745Sheppo 		vds_operations |= 1 << (vds_operation[i].operation - 1);
44951ae08745Sheppo 
44961ae08745Sheppo 	return (0);
44971ae08745Sheppo }
44981ae08745Sheppo 
44991ae08745Sheppo int
45001ae08745Sheppo _info(struct modinfo *modinfop)
45011ae08745Sheppo {
45021ae08745Sheppo 	return (mod_info(&modlinkage, modinfop));
45031ae08745Sheppo }
45041ae08745Sheppo 
45051ae08745Sheppo int
45061ae08745Sheppo _fini(void)
45071ae08745Sheppo {
45081ae08745Sheppo 	int	status;
45091ae08745Sheppo 
4510d10e4ef2Snarayan 
45111ae08745Sheppo 	if ((status = mod_remove(&modlinkage)) != 0)
45121ae08745Sheppo 		return (status);
45131ae08745Sheppo 	ddi_soft_state_fini(&vds_state);
45141ae08745Sheppo 	return (0);
45151ae08745Sheppo }
4516