xref: /titanic_53/usr/src/uts/sun4v/io/vds.c (revision 445b4c2ed2d52ef648ae6b36e4f5e14ff3d234af)
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 /*
231ae08745Sheppo  * Copyright 2006 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>
441ae08745Sheppo #include <sys/sunddi.h>
451ae08745Sheppo #include <sys/sunldi.h>
461ae08745Sheppo #include <sys/sysmacros.h>
471ae08745Sheppo #include <sys/vio_common.h>
481ae08745Sheppo #include <sys/vdsk_mailbox.h>
491ae08745Sheppo #include <sys/vdsk_common.h>
501ae08745Sheppo #include <sys/vtoc.h>
511ae08745Sheppo 
521ae08745Sheppo 
531ae08745Sheppo /* Virtual disk server initialization flags */
54d10e4ef2Snarayan #define	VDS_LDI			0x01
55d10e4ef2Snarayan #define	VDS_MDEG		0x02
561ae08745Sheppo 
571ae08745Sheppo /* Virtual disk server tunable parameters */
5834683adeSsg70180 #define	VDS_LDC_RETRIES		5
593af08d82Slm66018 #define	VDS_LDC_DELAY		1000 /* usec */
601ae08745Sheppo #define	VDS_NCHAINS		32
611ae08745Sheppo 
621ae08745Sheppo /* Identification parameters for MD, synthetic dkio(7i) structures, etc. */
631ae08745Sheppo #define	VDS_NAME		"virtual-disk-server"
641ae08745Sheppo 
651ae08745Sheppo #define	VD_NAME			"vd"
661ae08745Sheppo #define	VD_VOLUME_NAME		"vdisk"
671ae08745Sheppo #define	VD_ASCIILABEL		"Virtual Disk"
681ae08745Sheppo 
691ae08745Sheppo #define	VD_CHANNEL_ENDPOINT	"channel-endpoint"
701ae08745Sheppo #define	VD_ID_PROP		"id"
711ae08745Sheppo #define	VD_BLOCK_DEVICE_PROP	"vds-block-device"
72*445b4c2eSsb155480 #define	VD_REG_PROP		"reg"
731ae08745Sheppo 
741ae08745Sheppo /* Virtual disk initialization flags */
751ae08745Sheppo #define	VD_LOCKING		0x01
76d10e4ef2Snarayan #define	VD_LDC			0x02
77d10e4ef2Snarayan #define	VD_DRING		0x04
78d10e4ef2Snarayan #define	VD_SID			0x08
79d10e4ef2Snarayan #define	VD_SEQ_NUM		0x10
801ae08745Sheppo 
811ae08745Sheppo /* Flags for opening/closing backing devices via LDI */
821ae08745Sheppo #define	VD_OPEN_FLAGS		(FEXCL | FREAD | FWRITE)
831ae08745Sheppo 
841ae08745Sheppo /*
851ae08745Sheppo  * By Solaris convention, slice/partition 2 represents the entire disk;
861ae08745Sheppo  * unfortunately, this convention does not appear to be codified.
871ae08745Sheppo  */
881ae08745Sheppo #define	VD_ENTIRE_DISK_SLICE	2
891ae08745Sheppo 
901ae08745Sheppo /* Return a cpp token as a string */
911ae08745Sheppo #define	STRINGIZE(token)	#token
921ae08745Sheppo 
931ae08745Sheppo /*
941ae08745Sheppo  * Print a message prefixed with the current function name to the message log
951ae08745Sheppo  * (and optionally to the console for verbose boots); these macros use cpp's
961ae08745Sheppo  * concatenation of string literals and C99 variable-length-argument-list
971ae08745Sheppo  * macros
981ae08745Sheppo  */
991ae08745Sheppo #define	PRN(...)	_PRN("?%s():  "__VA_ARGS__, "")
1001ae08745Sheppo #define	_PRN(format, ...)					\
1011ae08745Sheppo 	cmn_err(CE_CONT, format"%s", __func__, __VA_ARGS__)
1021ae08745Sheppo 
1031ae08745Sheppo /* Return a pointer to the "i"th vdisk dring element */
1041ae08745Sheppo #define	VD_DRING_ELEM(i)	((vd_dring_entry_t *)(void *)	\
1051ae08745Sheppo 	    (vd->dring + (i)*vd->descriptor_size))
1061ae08745Sheppo 
1071ae08745Sheppo /* Return the virtual disk client's type as a string (for use in messages) */
1081ae08745Sheppo #define	VD_CLIENT(vd)							\
1091ae08745Sheppo 	(((vd)->xfer_mode == VIO_DESC_MODE) ? "in-band client" :	\
1101ae08745Sheppo 	    (((vd)->xfer_mode == VIO_DRING_MODE) ? "dring client" :	\
1111ae08745Sheppo 		(((vd)->xfer_mode == 0) ? "null client" :		\
1121ae08745Sheppo 		    "unsupported client")))
1131ae08745Sheppo 
114*445b4c2eSsb155480 /*
115*445b4c2eSsb155480  * Specification of an MD node passed to the MDEG to filter any
116*445b4c2eSsb155480  * 'vport' nodes that do not belong to the specified node. This
117*445b4c2eSsb155480  * template is copied for each vds instance and filled in with
118*445b4c2eSsb155480  * the appropriate 'cfg-handle' value before being passed to the MDEG.
119*445b4c2eSsb155480  */
120*445b4c2eSsb155480 static mdeg_prop_spec_t	vds_prop_template[] = {
121*445b4c2eSsb155480 	{ MDET_PROP_STR,	"name",		VDS_NAME },
122*445b4c2eSsb155480 	{ MDET_PROP_VAL,	"cfg-handle",	NULL },
123*445b4c2eSsb155480 	{ MDET_LIST_END,	NULL, 		NULL }
124*445b4c2eSsb155480 };
125*445b4c2eSsb155480 
126*445b4c2eSsb155480 #define	VDS_SET_MDEG_PROP_INST(specp, val) (specp)[1].ps_val = (val);
127*445b4c2eSsb155480 
128*445b4c2eSsb155480 /*
129*445b4c2eSsb155480  * Matching criteria passed to the MDEG to register interest
130*445b4c2eSsb155480  * in changes to 'virtual-device-port' nodes identified by their
131*445b4c2eSsb155480  * 'id' property.
132*445b4c2eSsb155480  */
133*445b4c2eSsb155480 static md_prop_match_t	vd_prop_match[] = {
134*445b4c2eSsb155480 	{ MDET_PROP_VAL,	VD_ID_PROP },
135*445b4c2eSsb155480 	{ MDET_LIST_END,	NULL }
136*445b4c2eSsb155480 };
137*445b4c2eSsb155480 
138*445b4c2eSsb155480 static mdeg_node_match_t vd_match = {"virtual-device-port",
139*445b4c2eSsb155480 				    vd_prop_match};
140*445b4c2eSsb155480 
1411ae08745Sheppo /* Debugging macros */
1421ae08745Sheppo #ifdef DEBUG
1433af08d82Slm66018 
1443af08d82Slm66018 static int	vd_msglevel = 0;
1453af08d82Slm66018 
1463af08d82Slm66018 
1471ae08745Sheppo #define	PR0 if (vd_msglevel > 0)	PRN
1481ae08745Sheppo #define	PR1 if (vd_msglevel > 1)	PRN
1491ae08745Sheppo #define	PR2 if (vd_msglevel > 2)	PRN
1501ae08745Sheppo 
1511ae08745Sheppo #define	VD_DUMP_DRING_ELEM(elem)					\
1521ae08745Sheppo 	PRN("dst:%x op:%x st:%u nb:%lx addr:%lx ncook:%u\n",		\
1531ae08745Sheppo 	    elem->hdr.dstate,						\
1541ae08745Sheppo 	    elem->payload.operation,					\
1551ae08745Sheppo 	    elem->payload.status,					\
1561ae08745Sheppo 	    elem->payload.nbytes,					\
1571ae08745Sheppo 	    elem->payload.addr,						\
1581ae08745Sheppo 	    elem->payload.ncookies);
1591ae08745Sheppo 
1603af08d82Slm66018 char *
1613af08d82Slm66018 vd_decode_state(int state)
1623af08d82Slm66018 {
1633af08d82Slm66018 	char *str;
1643af08d82Slm66018 
1653af08d82Slm66018 #define	CASE_STATE(_s)	case _s: str = #_s; break;
1663af08d82Slm66018 
1673af08d82Slm66018 	switch (state) {
1683af08d82Slm66018 	CASE_STATE(VD_STATE_INIT)
1693af08d82Slm66018 	CASE_STATE(VD_STATE_VER)
1703af08d82Slm66018 	CASE_STATE(VD_STATE_ATTR)
1713af08d82Slm66018 	CASE_STATE(VD_STATE_DRING)
1723af08d82Slm66018 	CASE_STATE(VD_STATE_RDX)
1733af08d82Slm66018 	CASE_STATE(VD_STATE_DATA)
1743af08d82Slm66018 	default: str = "unknown"; break;
1753af08d82Slm66018 	}
1763af08d82Slm66018 
1773af08d82Slm66018 #undef CASE_STATE
1783af08d82Slm66018 
1793af08d82Slm66018 	return (str);
1803af08d82Slm66018 }
1813af08d82Slm66018 
1823af08d82Slm66018 void
1833af08d82Slm66018 vd_decode_tag(vio_msg_t *msg)
1843af08d82Slm66018 {
1853af08d82Slm66018 	char *tstr, *sstr, *estr;
1863af08d82Slm66018 
1873af08d82Slm66018 #define	CASE_TYPE(_s)	case _s: tstr = #_s; break;
1883af08d82Slm66018 
1893af08d82Slm66018 	switch (msg->tag.vio_msgtype) {
1903af08d82Slm66018 	CASE_TYPE(VIO_TYPE_CTRL)
1913af08d82Slm66018 	CASE_TYPE(VIO_TYPE_DATA)
1923af08d82Slm66018 	CASE_TYPE(VIO_TYPE_ERR)
1933af08d82Slm66018 	default: tstr = "unknown"; break;
1943af08d82Slm66018 	}
1953af08d82Slm66018 
1963af08d82Slm66018 #undef CASE_TYPE
1973af08d82Slm66018 
1983af08d82Slm66018 #define	CASE_SUBTYPE(_s) case _s: sstr = #_s; break;
1993af08d82Slm66018 
2003af08d82Slm66018 	switch (msg->tag.vio_subtype) {
2013af08d82Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_INFO)
2023af08d82Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_ACK)
2033af08d82Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_NACK)
2043af08d82Slm66018 	default: sstr = "unknown"; break;
2053af08d82Slm66018 	}
2063af08d82Slm66018 
2073af08d82Slm66018 #undef CASE_SUBTYPE
2083af08d82Slm66018 
2093af08d82Slm66018 #define	CASE_ENV(_s)	case _s: estr = #_s; break;
2103af08d82Slm66018 
2113af08d82Slm66018 	switch (msg->tag.vio_subtype_env) {
2123af08d82Slm66018 	CASE_ENV(VIO_VER_INFO)
2133af08d82Slm66018 	CASE_ENV(VIO_ATTR_INFO)
2143af08d82Slm66018 	CASE_ENV(VIO_DRING_REG)
2153af08d82Slm66018 	CASE_ENV(VIO_DRING_UNREG)
2163af08d82Slm66018 	CASE_ENV(VIO_RDX)
2173af08d82Slm66018 	CASE_ENV(VIO_PKT_DATA)
2183af08d82Slm66018 	CASE_ENV(VIO_DESC_DATA)
2193af08d82Slm66018 	CASE_ENV(VIO_DRING_DATA)
2203af08d82Slm66018 	default: estr = "unknown"; break;
2213af08d82Slm66018 	}
2223af08d82Slm66018 
2233af08d82Slm66018 #undef CASE_ENV
2243af08d82Slm66018 
2253af08d82Slm66018 	PR1("(%x/%x/%x) message : (%s/%s/%s)",
2263af08d82Slm66018 	    msg->tag.vio_msgtype, msg->tag.vio_subtype,
2273af08d82Slm66018 	    msg->tag.vio_subtype_env, tstr, sstr, estr);
2283af08d82Slm66018 }
2293af08d82Slm66018 
2301ae08745Sheppo #else	/* !DEBUG */
2313af08d82Slm66018 
2321ae08745Sheppo #define	PR0(...)
2331ae08745Sheppo #define	PR1(...)
2341ae08745Sheppo #define	PR2(...)
2351ae08745Sheppo 
2361ae08745Sheppo #define	VD_DUMP_DRING_ELEM(elem)
2371ae08745Sheppo 
2383af08d82Slm66018 #define	vd_decode_state(_s)	(NULL)
2393af08d82Slm66018 #define	vd_decode_tag(_s)	(NULL)
2403af08d82Slm66018 
2411ae08745Sheppo #endif	/* DEBUG */
2421ae08745Sheppo 
2431ae08745Sheppo 
244d10e4ef2Snarayan /*
245d10e4ef2Snarayan  * Soft state structure for a vds instance
246d10e4ef2Snarayan  */
2471ae08745Sheppo typedef struct vds {
2481ae08745Sheppo 	uint_t		initialized;	/* driver inst initialization flags */
2491ae08745Sheppo 	dev_info_t	*dip;		/* driver inst devinfo pointer */
2501ae08745Sheppo 	ldi_ident_t	ldi_ident;	/* driver's identifier for LDI */
2511ae08745Sheppo 	mod_hash_t	*vd_table;	/* table of virtual disks served */
252*445b4c2eSsb155480 	mdeg_node_spec_t *ispecp;	/* mdeg node specification */
2531ae08745Sheppo 	mdeg_handle_t	mdeg;		/* handle for MDEG operations  */
2541ae08745Sheppo } vds_t;
2551ae08745Sheppo 
256d10e4ef2Snarayan /*
257d10e4ef2Snarayan  * Types of descriptor-processing tasks
258d10e4ef2Snarayan  */
259d10e4ef2Snarayan typedef enum vd_task_type {
260d10e4ef2Snarayan 	VD_NONFINAL_RANGE_TASK,	/* task for intermediate descriptor in range */
261d10e4ef2Snarayan 	VD_FINAL_RANGE_TASK,	/* task for last in a range of descriptors */
262d10e4ef2Snarayan } vd_task_type_t;
263d10e4ef2Snarayan 
264d10e4ef2Snarayan /*
265d10e4ef2Snarayan  * Structure describing the task for processing a descriptor
266d10e4ef2Snarayan  */
267d10e4ef2Snarayan typedef struct vd_task {
268d10e4ef2Snarayan 	struct vd		*vd;		/* vd instance task is for */
269d10e4ef2Snarayan 	vd_task_type_t		type;		/* type of descriptor task */
270d10e4ef2Snarayan 	int			index;		/* dring elem index for task */
271d10e4ef2Snarayan 	vio_msg_t		*msg;		/* VIO message task is for */
272d10e4ef2Snarayan 	size_t			msglen;		/* length of message content */
273d10e4ef2Snarayan 	vd_dring_payload_t	*request;	/* request task will perform */
274d10e4ef2Snarayan 	struct buf		buf;		/* buf(9s) for I/O request */
2754bac2208Snarayan 	ldc_mem_handle_t	mhdl;		/* task memory handle */
276d10e4ef2Snarayan } vd_task_t;
277d10e4ef2Snarayan 
278d10e4ef2Snarayan /*
279d10e4ef2Snarayan  * Soft state structure for a virtual disk instance
280d10e4ef2Snarayan  */
2811ae08745Sheppo typedef struct vd {
2821ae08745Sheppo 	uint_t			initialized;	/* vdisk initialization flags */
2831ae08745Sheppo 	vds_t			*vds;		/* server for this vdisk */
284d10e4ef2Snarayan 	ddi_taskq_t		*startq;	/* queue for I/O start tasks */
285d10e4ef2Snarayan 	ddi_taskq_t		*completionq;	/* queue for completion tasks */
2861ae08745Sheppo 	ldi_handle_t		ldi_handle[V_NUMPAR];	/* LDI slice handles */
2871ae08745Sheppo 	dev_t			dev[V_NUMPAR];	/* dev numbers for slices */
288e1ebb9ecSlm66018 	uint_t			nslices;	/* number of slices */
2891ae08745Sheppo 	size_t			vdisk_size;	/* number of blocks in vdisk */
2901ae08745Sheppo 	vd_disk_type_t		vdisk_type;	/* slice or entire disk */
2914bac2208Snarayan 	vd_disk_label_t		vdisk_label;	/* EFI or VTOC label */
292e1ebb9ecSlm66018 	ushort_t		max_xfer_sz;	/* max xfer size in DEV_BSIZE */
2931ae08745Sheppo 	boolean_t		pseudo;		/* underlying pseudo dev */
2944bac2208Snarayan 	struct dk_efi		dk_efi;		/* synthetic for slice type */
2951ae08745Sheppo 	struct dk_geom		dk_geom;	/* synthetic for slice type */
2961ae08745Sheppo 	struct vtoc		vtoc;		/* synthetic for slice type */
2971ae08745Sheppo 	ldc_status_t		ldc_state;	/* LDC connection state */
2981ae08745Sheppo 	ldc_handle_t		ldc_handle;	/* handle for LDC comm */
2991ae08745Sheppo 	size_t			max_msglen;	/* largest LDC message len */
3001ae08745Sheppo 	vd_state_t		state;		/* client handshake state */
3011ae08745Sheppo 	uint8_t			xfer_mode;	/* transfer mode with client */
3021ae08745Sheppo 	uint32_t		sid;		/* client's session ID */
3031ae08745Sheppo 	uint64_t		seq_num;	/* message sequence number */
3041ae08745Sheppo 	uint64_t		dring_ident;	/* identifier of dring */
3051ae08745Sheppo 	ldc_dring_handle_t	dring_handle;	/* handle for dring ops */
3061ae08745Sheppo 	uint32_t		descriptor_size;	/* num bytes in desc */
3071ae08745Sheppo 	uint32_t		dring_len;	/* number of dring elements */
3081ae08745Sheppo 	caddr_t			dring;		/* address of dring */
3093af08d82Slm66018 	caddr_t			vio_msgp;	/* vio msg staging buffer */
310d10e4ef2Snarayan 	vd_task_t		inband_task;	/* task for inband descriptor */
311d10e4ef2Snarayan 	vd_task_t		*dring_task;	/* tasks dring elements */
312d10e4ef2Snarayan 
313d10e4ef2Snarayan 	kmutex_t		lock;		/* protects variables below */
314d10e4ef2Snarayan 	boolean_t		enabled;	/* is vdisk enabled? */
315d10e4ef2Snarayan 	boolean_t		reset_state;	/* reset connection state? */
316d10e4ef2Snarayan 	boolean_t		reset_ldc;	/* reset LDC channel? */
3171ae08745Sheppo } vd_t;
3181ae08745Sheppo 
3191ae08745Sheppo typedef struct vds_operation {
3203af08d82Slm66018 	char	*namep;
3211ae08745Sheppo 	uint8_t	operation;
322d10e4ef2Snarayan 	int	(*start)(vd_task_t *task);
323d10e4ef2Snarayan 	void	(*complete)(void *arg);
3241ae08745Sheppo } vds_operation_t;
3251ae08745Sheppo 
3260a55fbb7Slm66018 typedef struct vd_ioctl {
3270a55fbb7Slm66018 	uint8_t		operation;		/* vdisk operation */
3280a55fbb7Slm66018 	const char	*operation_name;	/* vdisk operation name */
3290a55fbb7Slm66018 	size_t		nbytes;			/* size of operation buffer */
3300a55fbb7Slm66018 	int		cmd;			/* corresponding ioctl cmd */
3310a55fbb7Slm66018 	const char	*cmd_name;		/* ioctl cmd name */
3320a55fbb7Slm66018 	void		*arg;			/* ioctl cmd argument */
3330a55fbb7Slm66018 	/* convert input vd_buf to output ioctl_arg */
3340a55fbb7Slm66018 	void		(*copyin)(void *vd_buf, void *ioctl_arg);
3350a55fbb7Slm66018 	/* convert input ioctl_arg to output vd_buf */
3360a55fbb7Slm66018 	void		(*copyout)(void *ioctl_arg, void *vd_buf);
3370a55fbb7Slm66018 } vd_ioctl_t;
3380a55fbb7Slm66018 
3390a55fbb7Slm66018 /* Define trivial copyin/copyout conversion function flag */
3400a55fbb7Slm66018 #define	VD_IDENTITY	((void (*)(void *, void *))-1)
3411ae08745Sheppo 
3421ae08745Sheppo 
3431ae08745Sheppo static int	vds_ldc_retries = VDS_LDC_RETRIES;
3443af08d82Slm66018 static int	vds_ldc_delay = VDS_LDC_DELAY;
3451ae08745Sheppo static void	*vds_state;
3461ae08745Sheppo static uint64_t	vds_operations;	/* see vds_operation[] definition below */
3471ae08745Sheppo 
3481ae08745Sheppo static int	vd_open_flags = VD_OPEN_FLAGS;
3491ae08745Sheppo 
3500a55fbb7Slm66018 /*
3510a55fbb7Slm66018  * Supported protocol version pairs, from highest (newest) to lowest (oldest)
3520a55fbb7Slm66018  *
3530a55fbb7Slm66018  * Each supported major version should appear only once, paired with (and only
3540a55fbb7Slm66018  * with) its highest supported minor version number (as the protocol requires
3550a55fbb7Slm66018  * supporting all lower minor version numbers as well)
3560a55fbb7Slm66018  */
3570a55fbb7Slm66018 static const vio_ver_t	vds_version[] = {{1, 0}};
3580a55fbb7Slm66018 static const size_t	vds_num_versions =
3590a55fbb7Slm66018     sizeof (vds_version)/sizeof (vds_version[0]);
3600a55fbb7Slm66018 
3613af08d82Slm66018 static void vd_free_dring_task(vd_t *vdp);
3621ae08745Sheppo 
3631ae08745Sheppo static int
364d10e4ef2Snarayan vd_start_bio(vd_task_t *task)
3651ae08745Sheppo {
3664bac2208Snarayan 	int			rv, status = 0;
367d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
368d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
369d10e4ef2Snarayan 	struct buf		*buf		= &task->buf;
3704bac2208Snarayan 	uint8_t			mtype;
3711ae08745Sheppo 
372d10e4ef2Snarayan 
373d10e4ef2Snarayan 	ASSERT(vd != NULL);
374d10e4ef2Snarayan 	ASSERT(request != NULL);
375d10e4ef2Snarayan 	ASSERT(request->slice < vd->nslices);
376d10e4ef2Snarayan 	ASSERT((request->operation == VD_OP_BREAD) ||
377d10e4ef2Snarayan 	    (request->operation == VD_OP_BWRITE));
378d10e4ef2Snarayan 
3791ae08745Sheppo 	if (request->nbytes == 0)
3801ae08745Sheppo 		return (EINVAL);	/* no service for trivial requests */
3811ae08745Sheppo 
382d10e4ef2Snarayan 	PR1("%s %lu bytes at block %lu",
383d10e4ef2Snarayan 	    (request->operation == VD_OP_BREAD) ? "Read" : "Write",
384d10e4ef2Snarayan 	    request->nbytes, request->addr);
3851ae08745Sheppo 
386d10e4ef2Snarayan 	bioinit(buf);
387d10e4ef2Snarayan 	buf->b_flags		= B_BUSY;
388d10e4ef2Snarayan 	buf->b_bcount		= request->nbytes;
389d10e4ef2Snarayan 	buf->b_lblkno		= request->addr;
390d10e4ef2Snarayan 	buf->b_edev		= vd->dev[request->slice];
391d10e4ef2Snarayan 
3924bac2208Snarayan 	mtype = (&vd->inband_task == task) ? LDC_SHADOW_MAP : LDC_DIRECT_MAP;
3934bac2208Snarayan 
3944bac2208Snarayan 	/* Map memory exported by client */
3954bac2208Snarayan 	status = ldc_mem_map(task->mhdl, request->cookie, request->ncookies,
3964bac2208Snarayan 	    mtype, (request->operation == VD_OP_BREAD) ? LDC_MEM_W : LDC_MEM_R,
3974bac2208Snarayan 	    &(buf->b_un.b_addr), NULL);
3984bac2208Snarayan 	if (status != 0) {
3993af08d82Slm66018 		PR0("ldc_mem_map() returned err %d ", status);
4004bac2208Snarayan 		biofini(buf);
4014bac2208Snarayan 		return (status);
402d10e4ef2Snarayan 	}
403d10e4ef2Snarayan 
4044bac2208Snarayan 	status = ldc_mem_acquire(task->mhdl, 0, buf->b_bcount);
4054bac2208Snarayan 	if (status != 0) {
4064bac2208Snarayan 		(void) ldc_mem_unmap(task->mhdl);
4073af08d82Slm66018 		PR0("ldc_mem_acquire() returned err %d ", status);
4084bac2208Snarayan 		biofini(buf);
4094bac2208Snarayan 		return (status);
4104bac2208Snarayan 	}
4114bac2208Snarayan 
4124bac2208Snarayan 	buf->b_flags |= (request->operation == VD_OP_BREAD) ? B_READ : B_WRITE;
4134bac2208Snarayan 
414d10e4ef2Snarayan 	/* Start the block I/O */
4154bac2208Snarayan 	if ((status = ldi_strategy(vd->ldi_handle[request->slice], buf)) == 0)
416d10e4ef2Snarayan 		return (EINPROGRESS);	/* will complete on completionq */
417d10e4ef2Snarayan 
418d10e4ef2Snarayan 	/* Clean up after error */
4194bac2208Snarayan 	rv = ldc_mem_release(task->mhdl, 0, buf->b_bcount);
4204bac2208Snarayan 	if (rv) {
4213af08d82Slm66018 		PR0("ldc_mem_release() returned err %d ", rv);
4224bac2208Snarayan 	}
4234bac2208Snarayan 	rv = ldc_mem_unmap(task->mhdl);
4244bac2208Snarayan 	if (rv) {
4253af08d82Slm66018 		PR0("ldc_mem_unmap() returned err %d ", status);
4264bac2208Snarayan 	}
4274bac2208Snarayan 
428d10e4ef2Snarayan 	biofini(buf);
429d10e4ef2Snarayan 	return (status);
430d10e4ef2Snarayan }
431d10e4ef2Snarayan 
432d10e4ef2Snarayan static int
433d10e4ef2Snarayan send_msg(ldc_handle_t ldc_handle, void *msg, size_t msglen)
434d10e4ef2Snarayan {
4353af08d82Slm66018 	int	status;
436d10e4ef2Snarayan 	size_t	nbytes;
437d10e4ef2Snarayan 
4383af08d82Slm66018 	do {
439d10e4ef2Snarayan 		nbytes = msglen;
440d10e4ef2Snarayan 		status = ldc_write(ldc_handle, msg, &nbytes);
4413af08d82Slm66018 		if (status != EWOULDBLOCK)
4423af08d82Slm66018 			break;
4433af08d82Slm66018 		drv_usecwait(vds_ldc_delay);
4443af08d82Slm66018 	} while (status == EWOULDBLOCK);
445d10e4ef2Snarayan 
446d10e4ef2Snarayan 	if (status != 0) {
4473af08d82Slm66018 		if (status != ECONNRESET)
4483af08d82Slm66018 			PR0("ldc_write() returned errno %d", status);
449d10e4ef2Snarayan 		return (status);
450d10e4ef2Snarayan 	} else if (nbytes != msglen) {
4513af08d82Slm66018 		PR0("ldc_write() performed only partial write");
452d10e4ef2Snarayan 		return (EIO);
453d10e4ef2Snarayan 	}
454d10e4ef2Snarayan 
455d10e4ef2Snarayan 	PR1("SENT %lu bytes", msglen);
456d10e4ef2Snarayan 	return (0);
457d10e4ef2Snarayan }
458d10e4ef2Snarayan 
459d10e4ef2Snarayan static void
460d10e4ef2Snarayan vd_need_reset(vd_t *vd, boolean_t reset_ldc)
461d10e4ef2Snarayan {
462d10e4ef2Snarayan 	mutex_enter(&vd->lock);
463d10e4ef2Snarayan 	vd->reset_state	= B_TRUE;
464d10e4ef2Snarayan 	vd->reset_ldc	= reset_ldc;
465d10e4ef2Snarayan 	mutex_exit(&vd->lock);
466d10e4ef2Snarayan }
467d10e4ef2Snarayan 
468d10e4ef2Snarayan /*
469d10e4ef2Snarayan  * Reset the state of the connection with a client, if needed; reset the LDC
470d10e4ef2Snarayan  * transport as well, if needed.  This function should only be called from the
4713af08d82Slm66018  * "vd_recv_msg", as it waits for tasks - otherwise a deadlock can occur.
472d10e4ef2Snarayan  */
473d10e4ef2Snarayan static void
474d10e4ef2Snarayan vd_reset_if_needed(vd_t *vd)
475d10e4ef2Snarayan {
476d10e4ef2Snarayan 	int	status = 0;
477d10e4ef2Snarayan 
478d10e4ef2Snarayan 	mutex_enter(&vd->lock);
479d10e4ef2Snarayan 	if (!vd->reset_state) {
480d10e4ef2Snarayan 		ASSERT(!vd->reset_ldc);
481d10e4ef2Snarayan 		mutex_exit(&vd->lock);
482d10e4ef2Snarayan 		return;
483d10e4ef2Snarayan 	}
484d10e4ef2Snarayan 	mutex_exit(&vd->lock);
485d10e4ef2Snarayan 
486d10e4ef2Snarayan 	PR0("Resetting connection state with %s", VD_CLIENT(vd));
487d10e4ef2Snarayan 
488d10e4ef2Snarayan 	/*
489d10e4ef2Snarayan 	 * Let any asynchronous I/O complete before possibly pulling the rug
490d10e4ef2Snarayan 	 * out from under it; defer checking vd->reset_ldc, as one of the
491d10e4ef2Snarayan 	 * asynchronous tasks might set it
492d10e4ef2Snarayan 	 */
493d10e4ef2Snarayan 	ddi_taskq_wait(vd->completionq);
494d10e4ef2Snarayan 
495d10e4ef2Snarayan 	if ((vd->initialized & VD_DRING) &&
496d10e4ef2Snarayan 	    ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0))
4973af08d82Slm66018 		PR0("ldc_mem_dring_unmap() returned errno %d", status);
498d10e4ef2Snarayan 
4993af08d82Slm66018 	vd_free_dring_task(vd);
5003af08d82Slm66018 
5013af08d82Slm66018 	/* Free the staging buffer for msgs */
5023af08d82Slm66018 	if (vd->vio_msgp != NULL) {
5033af08d82Slm66018 		kmem_free(vd->vio_msgp, vd->max_msglen);
5043af08d82Slm66018 		vd->vio_msgp = NULL;
505d10e4ef2Snarayan 	}
506d10e4ef2Snarayan 
5073af08d82Slm66018 	/* Free the inband message buffer */
5083af08d82Slm66018 	if (vd->inband_task.msg != NULL) {
5093af08d82Slm66018 		kmem_free(vd->inband_task.msg, vd->max_msglen);
5103af08d82Slm66018 		vd->inband_task.msg = NULL;
5113af08d82Slm66018 	}
512d10e4ef2Snarayan 
513d10e4ef2Snarayan 	mutex_enter(&vd->lock);
5143af08d82Slm66018 
5153af08d82Slm66018 	if (vd->reset_ldc)
5163af08d82Slm66018 		PR0("taking down LDC channel");
517e1ebb9ecSlm66018 	if (vd->reset_ldc && ((status = ldc_down(vd->ldc_handle)) != 0))
5183af08d82Slm66018 		PR0("ldc_down() returned errno %d", status);
519d10e4ef2Snarayan 
520d10e4ef2Snarayan 	vd->initialized	&= ~(VD_SID | VD_SEQ_NUM | VD_DRING);
521d10e4ef2Snarayan 	vd->state	= VD_STATE_INIT;
522d10e4ef2Snarayan 	vd->max_msglen	= sizeof (vio_msg_t);	/* baseline vio message size */
523d10e4ef2Snarayan 
5243af08d82Slm66018 	/* Allocate the staging buffer */
5253af08d82Slm66018 	vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
5263af08d82Slm66018 
5273af08d82Slm66018 	PR0("calling ldc_up\n");
5283af08d82Slm66018 	(void) ldc_up(vd->ldc_handle);
5293af08d82Slm66018 
530d10e4ef2Snarayan 	vd->reset_state	= B_FALSE;
531d10e4ef2Snarayan 	vd->reset_ldc	= B_FALSE;
5323af08d82Slm66018 
533d10e4ef2Snarayan 	mutex_exit(&vd->lock);
534d10e4ef2Snarayan }
535d10e4ef2Snarayan 
5363af08d82Slm66018 static void vd_recv_msg(void *arg);
5373af08d82Slm66018 
5383af08d82Slm66018 static void
5393af08d82Slm66018 vd_mark_in_reset(vd_t *vd)
5403af08d82Slm66018 {
5413af08d82Slm66018 	int status;
5423af08d82Slm66018 
5433af08d82Slm66018 	PR0("vd_mark_in_reset: marking vd in reset\n");
5443af08d82Slm66018 
5453af08d82Slm66018 	vd_need_reset(vd, B_FALSE);
5463af08d82Slm66018 	status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, DDI_SLEEP);
5473af08d82Slm66018 	if (status == DDI_FAILURE) {
5483af08d82Slm66018 		PR0("cannot schedule task to recv msg\n");
5493af08d82Slm66018 		vd_need_reset(vd, B_TRUE);
5503af08d82Slm66018 		return;
5513af08d82Slm66018 	}
5523af08d82Slm66018 }
5533af08d82Slm66018 
554d10e4ef2Snarayan static int
555d10e4ef2Snarayan vd_mark_elem_done(vd_t *vd, int idx, int elem_status)
556d10e4ef2Snarayan {
557d10e4ef2Snarayan 	boolean_t		accepted;
558d10e4ef2Snarayan 	int			status;
559d10e4ef2Snarayan 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
560d10e4ef2Snarayan 
5613af08d82Slm66018 	if (vd->reset_state)
5623af08d82Slm66018 		return (0);
563d10e4ef2Snarayan 
564d10e4ef2Snarayan 	/* Acquire the element */
5653af08d82Slm66018 	if (!vd->reset_state &&
5663af08d82Slm66018 	    (status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) {
5673af08d82Slm66018 		if (status == ECONNRESET) {
5683af08d82Slm66018 			vd_mark_in_reset(vd);
5693af08d82Slm66018 			return (0);
5703af08d82Slm66018 		} else {
5713af08d82Slm66018 			PR0("ldc_mem_dring_acquire() returned errno %d",
5723af08d82Slm66018 			    status);
573d10e4ef2Snarayan 			return (status);
574d10e4ef2Snarayan 		}
5753af08d82Slm66018 	}
576d10e4ef2Snarayan 
577d10e4ef2Snarayan 	/* Set the element's status and mark it done */
578d10e4ef2Snarayan 	accepted = (elem->hdr.dstate == VIO_DESC_ACCEPTED);
579d10e4ef2Snarayan 	if (accepted) {
580d10e4ef2Snarayan 		elem->payload.status	= elem_status;
581d10e4ef2Snarayan 		elem->hdr.dstate	= VIO_DESC_DONE;
582d10e4ef2Snarayan 	} else {
583d10e4ef2Snarayan 		/* Perhaps client timed out waiting for I/O... */
5843af08d82Slm66018 		PR0("element %u no longer \"accepted\"", idx);
585d10e4ef2Snarayan 		VD_DUMP_DRING_ELEM(elem);
586d10e4ef2Snarayan 	}
587d10e4ef2Snarayan 	/* Release the element */
5883af08d82Slm66018 	if (!vd->reset_state &&
5893af08d82Slm66018 	    (status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) {
5903af08d82Slm66018 		if (status == ECONNRESET) {
5913af08d82Slm66018 			vd_mark_in_reset(vd);
5923af08d82Slm66018 			return (0);
5933af08d82Slm66018 		} else {
5943af08d82Slm66018 			PR0("ldc_mem_dring_release() returned errno %d",
5953af08d82Slm66018 			    status);
596d10e4ef2Snarayan 			return (status);
597d10e4ef2Snarayan 		}
5983af08d82Slm66018 	}
599d10e4ef2Snarayan 
600d10e4ef2Snarayan 	return (accepted ? 0 : EINVAL);
601d10e4ef2Snarayan }
602d10e4ef2Snarayan 
603d10e4ef2Snarayan static void
604d10e4ef2Snarayan vd_complete_bio(void *arg)
605d10e4ef2Snarayan {
606d10e4ef2Snarayan 	int			status		= 0;
607d10e4ef2Snarayan 	vd_task_t		*task		= (vd_task_t *)arg;
608d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
609d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
610d10e4ef2Snarayan 	struct buf		*buf		= &task->buf;
611d10e4ef2Snarayan 
612d10e4ef2Snarayan 
613d10e4ef2Snarayan 	ASSERT(vd != NULL);
614d10e4ef2Snarayan 	ASSERT(request != NULL);
615d10e4ef2Snarayan 	ASSERT(task->msg != NULL);
616d10e4ef2Snarayan 	ASSERT(task->msglen >= sizeof (*task->msg));
617d10e4ef2Snarayan 
618d10e4ef2Snarayan 	/* Wait for the I/O to complete */
619d10e4ef2Snarayan 	request->status = biowait(buf);
620d10e4ef2Snarayan 
6214bac2208Snarayan 	/* Release the buffer */
6223af08d82Slm66018 	if (!vd->reset_state)
6234bac2208Snarayan 		status = ldc_mem_release(task->mhdl, 0, buf->b_bcount);
6244bac2208Snarayan 	if (status) {
6253af08d82Slm66018 		PR0("ldc_mem_release() returned errno %d copying to "
6263af08d82Slm66018 		    "client", status);
6273af08d82Slm66018 		if (status == ECONNRESET) {
6283af08d82Slm66018 			vd_mark_in_reset(vd);
6293af08d82Slm66018 		}
6301ae08745Sheppo 	}
6311ae08745Sheppo 
6323af08d82Slm66018 	/* Unmap the memory, even if in reset */
6334bac2208Snarayan 	status = ldc_mem_unmap(task->mhdl);
6344bac2208Snarayan 	if (status) {
6353af08d82Slm66018 		PR0("ldc_mem_unmap() returned errno %d copying to client",
6364bac2208Snarayan 		    status);
6373af08d82Slm66018 		if (status == ECONNRESET) {
6383af08d82Slm66018 			vd_mark_in_reset(vd);
6393af08d82Slm66018 		}
6404bac2208Snarayan 	}
6414bac2208Snarayan 
642d10e4ef2Snarayan 	biofini(buf);
6431ae08745Sheppo 
644d10e4ef2Snarayan 	/* Update the dring element for a dring client */
6453af08d82Slm66018 	if (!vd->reset_state && (status == 0) &&
6463af08d82Slm66018 	    (vd->xfer_mode == VIO_DRING_MODE)) {
647d10e4ef2Snarayan 		status = vd_mark_elem_done(vd, task->index, request->status);
6483af08d82Slm66018 		if (status == ECONNRESET)
6493af08d82Slm66018 			vd_mark_in_reset(vd);
6503af08d82Slm66018 	}
6511ae08745Sheppo 
652d10e4ef2Snarayan 	/*
653d10e4ef2Snarayan 	 * If a transport error occurred, arrange to "nack" the message when
654d10e4ef2Snarayan 	 * the final task in the descriptor element range completes
655d10e4ef2Snarayan 	 */
656d10e4ef2Snarayan 	if (status != 0)
657d10e4ef2Snarayan 		task->msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
6581ae08745Sheppo 
659d10e4ef2Snarayan 	/*
660d10e4ef2Snarayan 	 * Only the final task for a range of elements will respond to and
661d10e4ef2Snarayan 	 * free the message
662d10e4ef2Snarayan 	 */
6633af08d82Slm66018 	if (task->type == VD_NONFINAL_RANGE_TASK) {
664d10e4ef2Snarayan 		return;
6653af08d82Slm66018 	}
6661ae08745Sheppo 
667d10e4ef2Snarayan 	/*
668d10e4ef2Snarayan 	 * Send the "ack" or "nack" back to the client; if sending the message
669d10e4ef2Snarayan 	 * via LDC fails, arrange to reset both the connection state and LDC
670d10e4ef2Snarayan 	 * itself
671d10e4ef2Snarayan 	 */
672d10e4ef2Snarayan 	PR1("Sending %s",
673d10e4ef2Snarayan 	    (task->msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK");
6743af08d82Slm66018 	if (!vd->reset_state) {
6753af08d82Slm66018 		status = send_msg(vd->ldc_handle, task->msg, task->msglen);
6763af08d82Slm66018 		switch (status) {
6773af08d82Slm66018 		case 0:
6783af08d82Slm66018 			break;
6793af08d82Slm66018 		case ECONNRESET:
6803af08d82Slm66018 			vd_mark_in_reset(vd);
6813af08d82Slm66018 			break;
6823af08d82Slm66018 		default:
6833af08d82Slm66018 			PR0("initiating full reset");
684d10e4ef2Snarayan 			vd_need_reset(vd, B_TRUE);
6853af08d82Slm66018 			break;
6863af08d82Slm66018 		}
6873af08d82Slm66018 	}
6881ae08745Sheppo }
6891ae08745Sheppo 
6900a55fbb7Slm66018 static void
6910a55fbb7Slm66018 vd_geom2dk_geom(void *vd_buf, void *ioctl_arg)
6920a55fbb7Slm66018 {
6930a55fbb7Slm66018 	VD_GEOM2DK_GEOM((vd_geom_t *)vd_buf, (struct dk_geom *)ioctl_arg);
6940a55fbb7Slm66018 }
6950a55fbb7Slm66018 
6960a55fbb7Slm66018 static void
6970a55fbb7Slm66018 vd_vtoc2vtoc(void *vd_buf, void *ioctl_arg)
6980a55fbb7Slm66018 {
6990a55fbb7Slm66018 	VD_VTOC2VTOC((vd_vtoc_t *)vd_buf, (struct vtoc *)ioctl_arg);
7000a55fbb7Slm66018 }
7010a55fbb7Slm66018 
7020a55fbb7Slm66018 static void
7030a55fbb7Slm66018 dk_geom2vd_geom(void *ioctl_arg, void *vd_buf)
7040a55fbb7Slm66018 {
7050a55fbb7Slm66018 	DK_GEOM2VD_GEOM((struct dk_geom *)ioctl_arg, (vd_geom_t *)vd_buf);
7060a55fbb7Slm66018 }
7070a55fbb7Slm66018 
7080a55fbb7Slm66018 static void
7090a55fbb7Slm66018 vtoc2vd_vtoc(void *ioctl_arg, void *vd_buf)
7100a55fbb7Slm66018 {
7110a55fbb7Slm66018 	VTOC2VD_VTOC((struct vtoc *)ioctl_arg, (vd_vtoc_t *)vd_buf);
7120a55fbb7Slm66018 }
7130a55fbb7Slm66018 
7144bac2208Snarayan static void
7154bac2208Snarayan vd_get_efi_in(void *vd_buf, void *ioctl_arg)
7164bac2208Snarayan {
7174bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
7184bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
7194bac2208Snarayan 
7204bac2208Snarayan 	dk_efi->dki_lba = vd_efi->lba;
7214bac2208Snarayan 	dk_efi->dki_length = vd_efi->length;
7224bac2208Snarayan 	dk_efi->dki_data = kmem_zalloc(vd_efi->length, KM_SLEEP);
7234bac2208Snarayan }
7244bac2208Snarayan 
7254bac2208Snarayan static void
7264bac2208Snarayan vd_get_efi_out(void *ioctl_arg, void *vd_buf)
7274bac2208Snarayan {
7284bac2208Snarayan 	int len;
7294bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
7304bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
7314bac2208Snarayan 
7324bac2208Snarayan 	len = vd_efi->length;
7334bac2208Snarayan 	DK_EFI2VD_EFI(dk_efi, vd_efi);
7344bac2208Snarayan 	kmem_free(dk_efi->dki_data, len);
7354bac2208Snarayan }
7364bac2208Snarayan 
7374bac2208Snarayan static void
7384bac2208Snarayan vd_set_efi_in(void *vd_buf, void *ioctl_arg)
7394bac2208Snarayan {
7404bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
7414bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
7424bac2208Snarayan 
7434bac2208Snarayan 	dk_efi->dki_data = kmem_alloc(vd_efi->length, KM_SLEEP);
7444bac2208Snarayan 	VD_EFI2DK_EFI(vd_efi, dk_efi);
7454bac2208Snarayan }
7464bac2208Snarayan 
7474bac2208Snarayan static void
7484bac2208Snarayan vd_set_efi_out(void *ioctl_arg, void *vd_buf)
7494bac2208Snarayan {
7504bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
7514bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
7524bac2208Snarayan 
7534bac2208Snarayan 	kmem_free(dk_efi->dki_data, vd_efi->length);
7544bac2208Snarayan }
7554bac2208Snarayan 
7564bac2208Snarayan static int
7574bac2208Snarayan vd_read_vtoc(ldi_handle_t handle, struct vtoc *vtoc, vd_disk_label_t *label)
7584bac2208Snarayan {
7594bac2208Snarayan 	int status, rval;
7604bac2208Snarayan 	struct dk_gpt *efi;
7614bac2208Snarayan 	size_t efi_len;
7624bac2208Snarayan 
7634bac2208Snarayan 	*label = VD_DISK_LABEL_UNK;
7644bac2208Snarayan 
7654bac2208Snarayan 	status = ldi_ioctl(handle, DKIOCGVTOC, (intptr_t)vtoc,
7664bac2208Snarayan 	    (vd_open_flags | FKIOCTL), kcred, &rval);
7674bac2208Snarayan 
7684bac2208Snarayan 	if (status == 0) {
7694bac2208Snarayan 		*label = VD_DISK_LABEL_VTOC;
7704bac2208Snarayan 		return (0);
7714bac2208Snarayan 	} else if (status != ENOTSUP) {
7723af08d82Slm66018 		PR0("ldi_ioctl(DKIOCGVTOC) returned error %d", status);
7734bac2208Snarayan 		return (status);
7744bac2208Snarayan 	}
7754bac2208Snarayan 
7764bac2208Snarayan 	status = vds_efi_alloc_and_read(handle, &efi, &efi_len);
7774bac2208Snarayan 
7784bac2208Snarayan 	if (status) {
7793af08d82Slm66018 		PR0("vds_efi_alloc_and_read returned error %d", status);
7804bac2208Snarayan 		return (status);
7814bac2208Snarayan 	}
7824bac2208Snarayan 
7834bac2208Snarayan 	*label = VD_DISK_LABEL_EFI;
7844bac2208Snarayan 	vd_efi_to_vtoc(efi, vtoc);
7854bac2208Snarayan 	vd_efi_free(efi, efi_len);
7864bac2208Snarayan 
7874bac2208Snarayan 	return (0);
7884bac2208Snarayan }
7894bac2208Snarayan 
7901ae08745Sheppo static int
7910a55fbb7Slm66018 vd_do_slice_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
7921ae08745Sheppo {
7934bac2208Snarayan 	dk_efi_t *dk_ioc;
7944bac2208Snarayan 
7954bac2208Snarayan 	switch (vd->vdisk_label) {
7964bac2208Snarayan 
7974bac2208Snarayan 	case VD_DISK_LABEL_VTOC:
7984bac2208Snarayan 
7991ae08745Sheppo 		switch (cmd) {
8001ae08745Sheppo 		case DKIOCGGEOM:
8010a55fbb7Slm66018 			ASSERT(ioctl_arg != NULL);
8020a55fbb7Slm66018 			bcopy(&vd->dk_geom, ioctl_arg, sizeof (vd->dk_geom));
8031ae08745Sheppo 			return (0);
8041ae08745Sheppo 		case DKIOCGVTOC:
8050a55fbb7Slm66018 			ASSERT(ioctl_arg != NULL);
8060a55fbb7Slm66018 			bcopy(&vd->vtoc, ioctl_arg, sizeof (vd->vtoc));
8071ae08745Sheppo 			return (0);
8081ae08745Sheppo 		default:
8091ae08745Sheppo 			return (ENOTSUP);
8101ae08745Sheppo 		}
8114bac2208Snarayan 
8124bac2208Snarayan 	case VD_DISK_LABEL_EFI:
8134bac2208Snarayan 
8144bac2208Snarayan 		switch (cmd) {
8154bac2208Snarayan 		case DKIOCGETEFI:
8164bac2208Snarayan 			ASSERT(ioctl_arg != NULL);
8174bac2208Snarayan 			dk_ioc = (dk_efi_t *)ioctl_arg;
8184bac2208Snarayan 			if (dk_ioc->dki_length < vd->dk_efi.dki_length)
8194bac2208Snarayan 				return (EINVAL);
8204bac2208Snarayan 			bcopy(vd->dk_efi.dki_data, dk_ioc->dki_data,
8214bac2208Snarayan 			    vd->dk_efi.dki_length);
8224bac2208Snarayan 			return (0);
8234bac2208Snarayan 		default:
8244bac2208Snarayan 			return (ENOTSUP);
8254bac2208Snarayan 		}
8264bac2208Snarayan 
8274bac2208Snarayan 	default:
8284bac2208Snarayan 		return (ENOTSUP);
8294bac2208Snarayan 	}
8301ae08745Sheppo }
8311ae08745Sheppo 
8321ae08745Sheppo static int
8330a55fbb7Slm66018 vd_do_ioctl(vd_t *vd, vd_dring_payload_t *request, void* buf, vd_ioctl_t *ioctl)
8341ae08745Sheppo {
8351ae08745Sheppo 	int	rval = 0, status;
8361ae08745Sheppo 	size_t	nbytes = request->nbytes;	/* modifiable copy */
8371ae08745Sheppo 
8381ae08745Sheppo 
8391ae08745Sheppo 	ASSERT(request->slice < vd->nslices);
8401ae08745Sheppo 	PR0("Performing %s", ioctl->operation_name);
8411ae08745Sheppo 
8420a55fbb7Slm66018 	/* Get data from client and convert, if necessary */
8430a55fbb7Slm66018 	if (ioctl->copyin != NULL)  {
8441ae08745Sheppo 		ASSERT(nbytes != 0 && buf != NULL);
8451ae08745Sheppo 		PR1("Getting \"arg\" data from client");
8461ae08745Sheppo 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
8471ae08745Sheppo 			    request->cookie, request->ncookies,
8481ae08745Sheppo 			    LDC_COPY_IN)) != 0) {
8493af08d82Slm66018 			PR0("ldc_mem_copy() returned errno %d "
8501ae08745Sheppo 			    "copying from client", status);
8511ae08745Sheppo 			return (status);
8521ae08745Sheppo 		}
8530a55fbb7Slm66018 
8540a55fbb7Slm66018 		/* Convert client's data, if necessary */
8550a55fbb7Slm66018 		if (ioctl->copyin == VD_IDENTITY)	/* use client buffer */
8560a55fbb7Slm66018 			ioctl->arg = buf;
8570a55fbb7Slm66018 		else	/* convert client vdisk operation data to ioctl data */
8580a55fbb7Slm66018 			(ioctl->copyin)(buf, (void *)ioctl->arg);
8591ae08745Sheppo 	}
8601ae08745Sheppo 
8611ae08745Sheppo 	/*
8621ae08745Sheppo 	 * Handle single-slice block devices internally; otherwise, have the
8631ae08745Sheppo 	 * real driver perform the ioctl()
8641ae08745Sheppo 	 */
8651ae08745Sheppo 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE && !vd->pseudo) {
8660a55fbb7Slm66018 		if ((status = vd_do_slice_ioctl(vd, ioctl->cmd,
8670a55fbb7Slm66018 			    (void *)ioctl->arg)) != 0)
8681ae08745Sheppo 			return (status);
8691ae08745Sheppo 	} else if ((status = ldi_ioctl(vd->ldi_handle[request->slice],
870d10e4ef2Snarayan 		    ioctl->cmd, (intptr_t)ioctl->arg, (vd_open_flags | FKIOCTL),
871d10e4ef2Snarayan 		    kcred, &rval)) != 0) {
8721ae08745Sheppo 		PR0("ldi_ioctl(%s) = errno %d", ioctl->cmd_name, status);
8731ae08745Sheppo 		return (status);
8741ae08745Sheppo 	}
8751ae08745Sheppo #ifdef DEBUG
8761ae08745Sheppo 	if (rval != 0) {
8773af08d82Slm66018 		PR0("%s set rval = %d, which is not being returned to client",
8781ae08745Sheppo 		    ioctl->cmd_name, rval);
8791ae08745Sheppo 	}
8801ae08745Sheppo #endif /* DEBUG */
8811ae08745Sheppo 
8820a55fbb7Slm66018 	/* Convert data and send to client, if necessary */
8830a55fbb7Slm66018 	if (ioctl->copyout != NULL)  {
8841ae08745Sheppo 		ASSERT(nbytes != 0 && buf != NULL);
8851ae08745Sheppo 		PR1("Sending \"arg\" data to client");
8860a55fbb7Slm66018 
8870a55fbb7Slm66018 		/* Convert ioctl data to vdisk operation data, if necessary */
8880a55fbb7Slm66018 		if (ioctl->copyout != VD_IDENTITY)
8890a55fbb7Slm66018 			(ioctl->copyout)((void *)ioctl->arg, buf);
8900a55fbb7Slm66018 
8911ae08745Sheppo 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
8921ae08745Sheppo 			    request->cookie, request->ncookies,
8931ae08745Sheppo 			    LDC_COPY_OUT)) != 0) {
8943af08d82Slm66018 			PR0("ldc_mem_copy() returned errno %d "
8951ae08745Sheppo 			    "copying to client", status);
8961ae08745Sheppo 			return (status);
8971ae08745Sheppo 		}
8981ae08745Sheppo 	}
8991ae08745Sheppo 
9001ae08745Sheppo 	return (status);
9011ae08745Sheppo }
9021ae08745Sheppo 
9031ae08745Sheppo #define	RNDSIZE(expr) P2ROUNDUP(sizeof (expr), sizeof (uint64_t))
9041ae08745Sheppo static int
905d10e4ef2Snarayan vd_ioctl(vd_task_t *task)
9061ae08745Sheppo {
90734683adeSsg70180 	int			i, status, rc;
9081ae08745Sheppo 	void			*buf = NULL;
9090a55fbb7Slm66018 	struct dk_geom		dk_geom = {0};
9100a55fbb7Slm66018 	struct vtoc		vtoc = {0};
9114bac2208Snarayan 	struct dk_efi		dk_efi = {0};
912d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
913d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
9140a55fbb7Slm66018 	vd_ioctl_t		ioctl[] = {
9150a55fbb7Slm66018 		/* Command (no-copy) operations */
9160a55fbb7Slm66018 		{VD_OP_FLUSH, STRINGIZE(VD_OP_FLUSH), 0,
9170a55fbb7Slm66018 		    DKIOCFLUSHWRITECACHE, STRINGIZE(DKIOCFLUSHWRITECACHE),
9180a55fbb7Slm66018 		    NULL, NULL, NULL},
9190a55fbb7Slm66018 
9200a55fbb7Slm66018 		/* "Get" (copy-out) operations */
9210a55fbb7Slm66018 		{VD_OP_GET_WCE, STRINGIZE(VD_OP_GET_WCE), RNDSIZE(int),
9220a55fbb7Slm66018 		    DKIOCGETWCE, STRINGIZE(DKIOCGETWCE),
9234bac2208Snarayan 		    NULL, VD_IDENTITY, VD_IDENTITY},
9240a55fbb7Slm66018 		{VD_OP_GET_DISKGEOM, STRINGIZE(VD_OP_GET_DISKGEOM),
9250a55fbb7Slm66018 		    RNDSIZE(vd_geom_t),
9260a55fbb7Slm66018 		    DKIOCGGEOM, STRINGIZE(DKIOCGGEOM),
9270a55fbb7Slm66018 		    &dk_geom, NULL, dk_geom2vd_geom},
9280a55fbb7Slm66018 		{VD_OP_GET_VTOC, STRINGIZE(VD_OP_GET_VTOC), RNDSIZE(vd_vtoc_t),
9290a55fbb7Slm66018 		    DKIOCGVTOC, STRINGIZE(DKIOCGVTOC),
9300a55fbb7Slm66018 		    &vtoc, NULL, vtoc2vd_vtoc},
9314bac2208Snarayan 		{VD_OP_GET_EFI, STRINGIZE(VD_OP_GET_EFI), RNDSIZE(vd_efi_t),
9324bac2208Snarayan 		    DKIOCGETEFI, STRINGIZE(DKIOCGETEFI),
9334bac2208Snarayan 		    &dk_efi, vd_get_efi_in, vd_get_efi_out},
9340a55fbb7Slm66018 
9350a55fbb7Slm66018 		/* "Set" (copy-in) operations */
9360a55fbb7Slm66018 		{VD_OP_SET_WCE, STRINGIZE(VD_OP_SET_WCE), RNDSIZE(int),
9370a55fbb7Slm66018 		    DKIOCSETWCE, STRINGIZE(DKIOCSETWCE),
9384bac2208Snarayan 		    NULL, VD_IDENTITY, VD_IDENTITY},
9390a55fbb7Slm66018 		{VD_OP_SET_DISKGEOM, STRINGIZE(VD_OP_SET_DISKGEOM),
9400a55fbb7Slm66018 		    RNDSIZE(vd_geom_t),
9410a55fbb7Slm66018 		    DKIOCSGEOM, STRINGIZE(DKIOCSGEOM),
9420a55fbb7Slm66018 		    &dk_geom, vd_geom2dk_geom, NULL},
9430a55fbb7Slm66018 		{VD_OP_SET_VTOC, STRINGIZE(VD_OP_SET_VTOC), RNDSIZE(vd_vtoc_t),
9440a55fbb7Slm66018 		    DKIOCSVTOC, STRINGIZE(DKIOCSVTOC),
9450a55fbb7Slm66018 		    &vtoc, vd_vtoc2vtoc, NULL},
9464bac2208Snarayan 		{VD_OP_SET_EFI, STRINGIZE(VD_OP_SET_EFI), RNDSIZE(vd_efi_t),
9474bac2208Snarayan 		    DKIOCSETEFI, STRINGIZE(DKIOCSETEFI),
9484bac2208Snarayan 		    &dk_efi, vd_set_efi_in, vd_set_efi_out},
9490a55fbb7Slm66018 	};
9501ae08745Sheppo 	size_t		nioctls = (sizeof (ioctl))/(sizeof (ioctl[0]));
9511ae08745Sheppo 
9521ae08745Sheppo 
953d10e4ef2Snarayan 	ASSERT(vd != NULL);
954d10e4ef2Snarayan 	ASSERT(request != NULL);
9551ae08745Sheppo 	ASSERT(request->slice < vd->nslices);
9561ae08745Sheppo 
9571ae08745Sheppo 	/*
9581ae08745Sheppo 	 * Determine ioctl corresponding to caller's "operation" and
9591ae08745Sheppo 	 * validate caller's "nbytes"
9601ae08745Sheppo 	 */
9611ae08745Sheppo 	for (i = 0; i < nioctls; i++) {
9621ae08745Sheppo 		if (request->operation == ioctl[i].operation) {
9630a55fbb7Slm66018 			/* LDC memory operations require 8-byte multiples */
9640a55fbb7Slm66018 			ASSERT(ioctl[i].nbytes % sizeof (uint64_t) == 0);
9650a55fbb7Slm66018 
9664bac2208Snarayan 			if (request->operation == VD_OP_GET_EFI ||
9674bac2208Snarayan 			    request->operation == VD_OP_SET_EFI) {
9684bac2208Snarayan 				if (request->nbytes >= ioctl[i].nbytes)
9694bac2208Snarayan 					break;
9703af08d82Slm66018 				PR0("%s:  Expected at least nbytes = %lu, "
9714bac2208Snarayan 				    "got %lu", ioctl[i].operation_name,
9724bac2208Snarayan 				    ioctl[i].nbytes, request->nbytes);
9734bac2208Snarayan 				return (EINVAL);
9744bac2208Snarayan 			}
9754bac2208Snarayan 
9760a55fbb7Slm66018 			if (request->nbytes != ioctl[i].nbytes) {
9773af08d82Slm66018 				PR0("%s:  Expected nbytes = %lu, got %lu",
9780a55fbb7Slm66018 				    ioctl[i].operation_name, ioctl[i].nbytes,
9790a55fbb7Slm66018 				    request->nbytes);
9801ae08745Sheppo 				return (EINVAL);
9811ae08745Sheppo 			}
9821ae08745Sheppo 
9831ae08745Sheppo 			break;
9841ae08745Sheppo 		}
9851ae08745Sheppo 	}
9861ae08745Sheppo 	ASSERT(i < nioctls);	/* because "operation" already validated */
9871ae08745Sheppo 
9881ae08745Sheppo 	if (request->nbytes)
9891ae08745Sheppo 		buf = kmem_zalloc(request->nbytes, KM_SLEEP);
9901ae08745Sheppo 	status = vd_do_ioctl(vd, request, buf, &ioctl[i]);
9911ae08745Sheppo 	if (request->nbytes)
9921ae08745Sheppo 		kmem_free(buf, request->nbytes);
9934bac2208Snarayan 	if (vd->vdisk_type == VD_DISK_TYPE_DISK &&
9944bac2208Snarayan 	    (request->operation == VD_OP_SET_VTOC ||
99534683adeSsg70180 	    request->operation == VD_OP_SET_EFI)) {
99634683adeSsg70180 		/* update disk information */
99734683adeSsg70180 		rc = vd_read_vtoc(vd->ldi_handle[0], &vd->vtoc,
99834683adeSsg70180 		    &vd->vdisk_label);
99934683adeSsg70180 		if (rc != 0)
100034683adeSsg70180 			PR0("vd_read_vtoc return error %d", rc);
100134683adeSsg70180 	}
1002d10e4ef2Snarayan 	PR0("Returning %d", status);
10031ae08745Sheppo 	return (status);
10041ae08745Sheppo }
10051ae08745Sheppo 
10064bac2208Snarayan static int
10074bac2208Snarayan vd_get_devid(vd_task_t *task)
10084bac2208Snarayan {
10094bac2208Snarayan 	vd_t *vd = task->vd;
10104bac2208Snarayan 	vd_dring_payload_t *request = task->request;
10114bac2208Snarayan 	vd_devid_t *vd_devid;
10124bac2208Snarayan 	impl_devid_t *devid;
10134bac2208Snarayan 	int status, bufid_len, devid_len, len;
10143af08d82Slm66018 	int bufbytes;
10154bac2208Snarayan 
10163af08d82Slm66018 	PR1("Get Device ID, nbytes=%ld", request->nbytes);
10174bac2208Snarayan 
10184bac2208Snarayan 	if (ddi_lyr_get_devid(vd->dev[request->slice],
10194bac2208Snarayan 	    (ddi_devid_t *)&devid) != DDI_SUCCESS) {
10204bac2208Snarayan 		/* the most common failure is that no devid is available */
10213af08d82Slm66018 		PR2("No Device ID");
10224bac2208Snarayan 		return (ENOENT);
10234bac2208Snarayan 	}
10244bac2208Snarayan 
10254bac2208Snarayan 	bufid_len = request->nbytes - sizeof (vd_devid_t) + 1;
10264bac2208Snarayan 	devid_len = DEVID_GETLEN(devid);
10274bac2208Snarayan 
10283af08d82Slm66018 	/*
10293af08d82Slm66018 	 * Save the buffer size here for use in deallocation.
10303af08d82Slm66018 	 * The actual number of bytes copied is returned in
10313af08d82Slm66018 	 * the 'nbytes' field of the request structure.
10323af08d82Slm66018 	 */
10333af08d82Slm66018 	bufbytes = request->nbytes;
10343af08d82Slm66018 
10353af08d82Slm66018 	vd_devid = kmem_zalloc(bufbytes, KM_SLEEP);
10364bac2208Snarayan 	vd_devid->length = devid_len;
10374bac2208Snarayan 	vd_devid->type = DEVID_GETTYPE(devid);
10384bac2208Snarayan 
10394bac2208Snarayan 	len = (devid_len > bufid_len)? bufid_len : devid_len;
10404bac2208Snarayan 
10414bac2208Snarayan 	bcopy(devid->did_id, vd_devid->id, len);
10424bac2208Snarayan 
10434bac2208Snarayan 	/* LDC memory operations require 8-byte multiples */
10444bac2208Snarayan 	ASSERT(request->nbytes % sizeof (uint64_t) == 0);
10454bac2208Snarayan 
10464bac2208Snarayan 	if ((status = ldc_mem_copy(vd->ldc_handle, (caddr_t)vd_devid, 0,
10474bac2208Snarayan 	    &request->nbytes, request->cookie, request->ncookies,
10484bac2208Snarayan 	    LDC_COPY_OUT)) != 0) {
10493af08d82Slm66018 		PR0("ldc_mem_copy() returned errno %d copying to client",
10504bac2208Snarayan 		    status);
10514bac2208Snarayan 	}
10523af08d82Slm66018 	PR1("post mem_copy: nbytes=%ld", request->nbytes);
10534bac2208Snarayan 
10543af08d82Slm66018 	kmem_free(vd_devid, bufbytes);
10554bac2208Snarayan 	ddi_devid_free((ddi_devid_t)devid);
10564bac2208Snarayan 
10574bac2208Snarayan 	return (status);
10584bac2208Snarayan }
10594bac2208Snarayan 
10601ae08745Sheppo /*
10611ae08745Sheppo  * Define the supported operations once the functions for performing them have
10621ae08745Sheppo  * been defined
10631ae08745Sheppo  */
10641ae08745Sheppo static const vds_operation_t	vds_operation[] = {
10653af08d82Slm66018 #define	X(_s)	#_s, _s
10663af08d82Slm66018 	{X(VD_OP_BREAD),	vd_start_bio,	vd_complete_bio},
10673af08d82Slm66018 	{X(VD_OP_BWRITE),	vd_start_bio,	vd_complete_bio},
10683af08d82Slm66018 	{X(VD_OP_FLUSH),	vd_ioctl,	NULL},
10693af08d82Slm66018 	{X(VD_OP_GET_WCE),	vd_ioctl,	NULL},
10703af08d82Slm66018 	{X(VD_OP_SET_WCE),	vd_ioctl,	NULL},
10713af08d82Slm66018 	{X(VD_OP_GET_VTOC),	vd_ioctl,	NULL},
10723af08d82Slm66018 	{X(VD_OP_SET_VTOC),	vd_ioctl,	NULL},
10733af08d82Slm66018 	{X(VD_OP_GET_DISKGEOM),	vd_ioctl,	NULL},
10743af08d82Slm66018 	{X(VD_OP_SET_DISKGEOM),	vd_ioctl,	NULL},
10753af08d82Slm66018 	{X(VD_OP_GET_EFI),	vd_ioctl,	NULL},
10763af08d82Slm66018 	{X(VD_OP_SET_EFI),	vd_ioctl,	NULL},
10773af08d82Slm66018 	{X(VD_OP_GET_DEVID),	vd_get_devid,	NULL},
10783af08d82Slm66018 #undef	X
10791ae08745Sheppo };
10801ae08745Sheppo 
10811ae08745Sheppo static const size_t	vds_noperations =
10821ae08745Sheppo 	(sizeof (vds_operation))/(sizeof (vds_operation[0]));
10831ae08745Sheppo 
10841ae08745Sheppo /*
1085d10e4ef2Snarayan  * Process a task specifying a client I/O request
10861ae08745Sheppo  */
10871ae08745Sheppo static int
1088d10e4ef2Snarayan vd_process_task(vd_task_t *task)
10891ae08745Sheppo {
1090d10e4ef2Snarayan 	int			i, status;
1091d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
1092d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
10931ae08745Sheppo 
10941ae08745Sheppo 
1095d10e4ef2Snarayan 	ASSERT(vd != NULL);
1096d10e4ef2Snarayan 	ASSERT(request != NULL);
10971ae08745Sheppo 
1098d10e4ef2Snarayan 	/* Find the requested operation */
10991ae08745Sheppo 	for (i = 0; i < vds_noperations; i++)
11001ae08745Sheppo 		if (request->operation == vds_operation[i].operation)
1101d10e4ef2Snarayan 			break;
1102d10e4ef2Snarayan 	if (i == vds_noperations) {
11033af08d82Slm66018 		PR0("Unsupported operation %u", request->operation);
11041ae08745Sheppo 		return (ENOTSUP);
11051ae08745Sheppo 	}
11061ae08745Sheppo 
11077636cb21Slm66018 	/* Handle client using absolute disk offsets */
11087636cb21Slm66018 	if ((vd->vdisk_type == VD_DISK_TYPE_DISK) &&
11097636cb21Slm66018 	    (request->slice == UINT8_MAX))
11107636cb21Slm66018 		request->slice = VD_ENTIRE_DISK_SLICE;
11117636cb21Slm66018 
11127636cb21Slm66018 	/* Range-check slice */
11137636cb21Slm66018 	if (request->slice >= vd->nslices) {
11143af08d82Slm66018 		PR0("Invalid \"slice\" %u (max %u) for virtual disk",
11157636cb21Slm66018 		    request->slice, (vd->nslices - 1));
11167636cb21Slm66018 		return (EINVAL);
11177636cb21Slm66018 	}
11187636cb21Slm66018 
11193af08d82Slm66018 	PR1("operation : %s", vds_operation[i].namep);
11203af08d82Slm66018 
1121d10e4ef2Snarayan 	/* Start the operation */
1122d10e4ef2Snarayan 	if ((status = vds_operation[i].start(task)) != EINPROGRESS) {
11233af08d82Slm66018 		PR0("operation : %s returned status %d",
11243af08d82Slm66018 			vds_operation[i].namep, status);
1125d10e4ef2Snarayan 		request->status = status;	/* op succeeded or failed */
1126d10e4ef2Snarayan 		return (0);			/* but request completed */
11271ae08745Sheppo 	}
11281ae08745Sheppo 
1129d10e4ef2Snarayan 	ASSERT(vds_operation[i].complete != NULL);	/* debug case */
1130d10e4ef2Snarayan 	if (vds_operation[i].complete == NULL) {	/* non-debug case */
11313af08d82Slm66018 		PR0("Unexpected return of EINPROGRESS "
1132d10e4ef2Snarayan 		    "with no I/O completion handler");
1133d10e4ef2Snarayan 		request->status = EIO;	/* operation failed */
1134d10e4ef2Snarayan 		return (0);		/* but request completed */
11351ae08745Sheppo 	}
11361ae08745Sheppo 
11373af08d82Slm66018 	PR1("operation : kick off taskq entry for %s", vds_operation[i].namep);
11383af08d82Slm66018 
1139d10e4ef2Snarayan 	/* Queue a task to complete the operation */
1140d10e4ef2Snarayan 	status = ddi_taskq_dispatch(vd->completionq, vds_operation[i].complete,
1141d10e4ef2Snarayan 	    task, DDI_SLEEP);
1142d10e4ef2Snarayan 	/* ddi_taskq_dispatch(9f) guarantees success with DDI_SLEEP */
1143d10e4ef2Snarayan 	ASSERT(status == DDI_SUCCESS);
1144d10e4ef2Snarayan 
1145d10e4ef2Snarayan 	PR1("Operation in progress");
1146d10e4ef2Snarayan 	return (EINPROGRESS);	/* completion handler will finish request */
11471ae08745Sheppo }
11481ae08745Sheppo 
11491ae08745Sheppo /*
11500a55fbb7Slm66018  * Return true if the "type", "subtype", and "env" fields of the "tag" first
11510a55fbb7Slm66018  * argument match the corresponding remaining arguments; otherwise, return false
11521ae08745Sheppo  */
11530a55fbb7Slm66018 boolean_t
11541ae08745Sheppo vd_msgtype(vio_msg_tag_t *tag, int type, int subtype, int env)
11551ae08745Sheppo {
11561ae08745Sheppo 	return ((tag->vio_msgtype == type) &&
11571ae08745Sheppo 		(tag->vio_subtype == subtype) &&
11580a55fbb7Slm66018 		(tag->vio_subtype_env == env)) ? B_TRUE : B_FALSE;
11591ae08745Sheppo }
11601ae08745Sheppo 
11610a55fbb7Slm66018 /*
11620a55fbb7Slm66018  * Check whether the major/minor version specified in "ver_msg" is supported
11630a55fbb7Slm66018  * by this server.
11640a55fbb7Slm66018  */
11650a55fbb7Slm66018 static boolean_t
11660a55fbb7Slm66018 vds_supported_version(vio_ver_msg_t *ver_msg)
11670a55fbb7Slm66018 {
11680a55fbb7Slm66018 	for (int i = 0; i < vds_num_versions; i++) {
11690a55fbb7Slm66018 		ASSERT(vds_version[i].major > 0);
11700a55fbb7Slm66018 		ASSERT((i == 0) ||
11710a55fbb7Slm66018 		    (vds_version[i].major < vds_version[i-1].major));
11720a55fbb7Slm66018 
11730a55fbb7Slm66018 		/*
11740a55fbb7Slm66018 		 * If the major versions match, adjust the minor version, if
11750a55fbb7Slm66018 		 * necessary, down to the highest value supported by this
11760a55fbb7Slm66018 		 * server and return true so this message will get "ack"ed;
11770a55fbb7Slm66018 		 * the client should also support all minor versions lower
11780a55fbb7Slm66018 		 * than the value it sent
11790a55fbb7Slm66018 		 */
11800a55fbb7Slm66018 		if (ver_msg->ver_major == vds_version[i].major) {
11810a55fbb7Slm66018 			if (ver_msg->ver_minor > vds_version[i].minor) {
11820a55fbb7Slm66018 				PR0("Adjusting minor version from %u to %u",
11830a55fbb7Slm66018 				    ver_msg->ver_minor, vds_version[i].minor);
11840a55fbb7Slm66018 				ver_msg->ver_minor = vds_version[i].minor;
11850a55fbb7Slm66018 			}
11860a55fbb7Slm66018 			return (B_TRUE);
11870a55fbb7Slm66018 		}
11880a55fbb7Slm66018 
11890a55fbb7Slm66018 		/*
11900a55fbb7Slm66018 		 * If the message contains a higher major version number, set
11910a55fbb7Slm66018 		 * the message's major/minor versions to the current values
11920a55fbb7Slm66018 		 * and return false, so this message will get "nack"ed with
11930a55fbb7Slm66018 		 * these values, and the client will potentially try again
11940a55fbb7Slm66018 		 * with the same or a lower version
11950a55fbb7Slm66018 		 */
11960a55fbb7Slm66018 		if (ver_msg->ver_major > vds_version[i].major) {
11970a55fbb7Slm66018 			ver_msg->ver_major = vds_version[i].major;
11980a55fbb7Slm66018 			ver_msg->ver_minor = vds_version[i].minor;
11990a55fbb7Slm66018 			return (B_FALSE);
12000a55fbb7Slm66018 		}
12010a55fbb7Slm66018 
12020a55fbb7Slm66018 		/*
12030a55fbb7Slm66018 		 * Otherwise, the message's major version is less than the
12040a55fbb7Slm66018 		 * current major version, so continue the loop to the next
12050a55fbb7Slm66018 		 * (lower) supported version
12060a55fbb7Slm66018 		 */
12070a55fbb7Slm66018 	}
12080a55fbb7Slm66018 
12090a55fbb7Slm66018 	/*
12100a55fbb7Slm66018 	 * No common version was found; "ground" the version pair in the
12110a55fbb7Slm66018 	 * message to terminate negotiation
12120a55fbb7Slm66018 	 */
12130a55fbb7Slm66018 	ver_msg->ver_major = 0;
12140a55fbb7Slm66018 	ver_msg->ver_minor = 0;
12150a55fbb7Slm66018 	return (B_FALSE);
12160a55fbb7Slm66018 }
12170a55fbb7Slm66018 
12180a55fbb7Slm66018 /*
12190a55fbb7Slm66018  * Process a version message from a client.  vds expects to receive version
12200a55fbb7Slm66018  * messages from clients seeking service, but never issues version messages
12210a55fbb7Slm66018  * itself; therefore, vds can ACK or NACK client version messages, but does
12220a55fbb7Slm66018  * not expect to receive version-message ACKs or NACKs (and will treat such
12230a55fbb7Slm66018  * messages as invalid).
12240a55fbb7Slm66018  */
12251ae08745Sheppo static int
12260a55fbb7Slm66018 vd_process_ver_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
12271ae08745Sheppo {
12281ae08745Sheppo 	vio_ver_msg_t	*ver_msg = (vio_ver_msg_t *)msg;
12291ae08745Sheppo 
12301ae08745Sheppo 
12311ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
12321ae08745Sheppo 
12331ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
12341ae08745Sheppo 		VIO_VER_INFO)) {
12351ae08745Sheppo 		return (ENOMSG);	/* not a version message */
12361ae08745Sheppo 	}
12371ae08745Sheppo 
12381ae08745Sheppo 	if (msglen != sizeof (*ver_msg)) {
12393af08d82Slm66018 		PR0("Expected %lu-byte version message; "
12401ae08745Sheppo 		    "received %lu bytes", sizeof (*ver_msg), msglen);
12411ae08745Sheppo 		return (EBADMSG);
12421ae08745Sheppo 	}
12431ae08745Sheppo 
12441ae08745Sheppo 	if (ver_msg->dev_class != VDEV_DISK) {
12453af08d82Slm66018 		PR0("Expected device class %u (disk); received %u",
12461ae08745Sheppo 		    VDEV_DISK, ver_msg->dev_class);
12471ae08745Sheppo 		return (EBADMSG);
12481ae08745Sheppo 	}
12491ae08745Sheppo 
12500a55fbb7Slm66018 	/*
12510a55fbb7Slm66018 	 * We're talking to the expected kind of client; set our device class
12520a55fbb7Slm66018 	 * for "ack/nack" back to the client
12530a55fbb7Slm66018 	 */
12541ae08745Sheppo 	ver_msg->dev_class = VDEV_DISK_SERVER;
12550a55fbb7Slm66018 
12560a55fbb7Slm66018 	/*
12570a55fbb7Slm66018 	 * Check whether the (valid) version message specifies a version
12580a55fbb7Slm66018 	 * supported by this server.  If the version is not supported, return
12590a55fbb7Slm66018 	 * EBADMSG so the message will get "nack"ed; vds_supported_version()
12600a55fbb7Slm66018 	 * will have updated the message with a supported version for the
12610a55fbb7Slm66018 	 * client to consider
12620a55fbb7Slm66018 	 */
12630a55fbb7Slm66018 	if (!vds_supported_version(ver_msg))
12640a55fbb7Slm66018 		return (EBADMSG);
12650a55fbb7Slm66018 
12660a55fbb7Slm66018 
12670a55fbb7Slm66018 	/*
12680a55fbb7Slm66018 	 * A version has been agreed upon; use the client's SID for
12690a55fbb7Slm66018 	 * communication on this channel now
12700a55fbb7Slm66018 	 */
12710a55fbb7Slm66018 	ASSERT(!(vd->initialized & VD_SID));
12720a55fbb7Slm66018 	vd->sid = ver_msg->tag.vio_sid;
12730a55fbb7Slm66018 	vd->initialized |= VD_SID;
12740a55fbb7Slm66018 
12750a55fbb7Slm66018 	/*
12760a55fbb7Slm66018 	 * When multiple versions are supported, this function should store
12770a55fbb7Slm66018 	 * the negotiated major and minor version values in the "vd" data
12780a55fbb7Slm66018 	 * structure to govern further communication; in particular, note that
12790a55fbb7Slm66018 	 * the client might have specified a lower minor version for the
12800a55fbb7Slm66018 	 * agreed major version than specifed in the vds_version[] array.  The
12810a55fbb7Slm66018 	 * following assertions should help remind future maintainers to make
12820a55fbb7Slm66018 	 * the appropriate changes to support multiple versions.
12830a55fbb7Slm66018 	 */
12840a55fbb7Slm66018 	ASSERT(vds_num_versions == 1);
12850a55fbb7Slm66018 	ASSERT(ver_msg->ver_major == vds_version[0].major);
12860a55fbb7Slm66018 	ASSERT(ver_msg->ver_minor == vds_version[0].minor);
12870a55fbb7Slm66018 
12880a55fbb7Slm66018 	PR0("Using major version %u, minor version %u",
12890a55fbb7Slm66018 	    ver_msg->ver_major, ver_msg->ver_minor);
12901ae08745Sheppo 	return (0);
12911ae08745Sheppo }
12921ae08745Sheppo 
12931ae08745Sheppo static int
12941ae08745Sheppo vd_process_attr_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
12951ae08745Sheppo {
12961ae08745Sheppo 	vd_attr_msg_t	*attr_msg = (vd_attr_msg_t *)msg;
12971ae08745Sheppo 
12981ae08745Sheppo 
12991ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
13001ae08745Sheppo 
13011ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
13021ae08745Sheppo 		VIO_ATTR_INFO)) {
1303d10e4ef2Snarayan 		PR0("Message is not an attribute message");
1304d10e4ef2Snarayan 		return (ENOMSG);
13051ae08745Sheppo 	}
13061ae08745Sheppo 
13071ae08745Sheppo 	if (msglen != sizeof (*attr_msg)) {
13083af08d82Slm66018 		PR0("Expected %lu-byte attribute message; "
13091ae08745Sheppo 		    "received %lu bytes", sizeof (*attr_msg), msglen);
13101ae08745Sheppo 		return (EBADMSG);
13111ae08745Sheppo 	}
13121ae08745Sheppo 
13131ae08745Sheppo 	if (attr_msg->max_xfer_sz == 0) {
13143af08d82Slm66018 		PR0("Received maximum transfer size of 0 from client");
13151ae08745Sheppo 		return (EBADMSG);
13161ae08745Sheppo 	}
13171ae08745Sheppo 
13181ae08745Sheppo 	if ((attr_msg->xfer_mode != VIO_DESC_MODE) &&
13191ae08745Sheppo 	    (attr_msg->xfer_mode != VIO_DRING_MODE)) {
13203af08d82Slm66018 		PR0("Client requested unsupported transfer mode");
13211ae08745Sheppo 		return (EBADMSG);
13221ae08745Sheppo 	}
13231ae08745Sheppo 
13241ae08745Sheppo 	/* Success:  valid message and transfer mode */
13251ae08745Sheppo 	vd->xfer_mode = attr_msg->xfer_mode;
13263af08d82Slm66018 
13271ae08745Sheppo 	if (vd->xfer_mode == VIO_DESC_MODE) {
13283af08d82Slm66018 
13291ae08745Sheppo 		/*
13301ae08745Sheppo 		 * The vd_dring_inband_msg_t contains one cookie; need room
13311ae08745Sheppo 		 * for up to n-1 more cookies, where "n" is the number of full
13321ae08745Sheppo 		 * pages plus possibly one partial page required to cover
13331ae08745Sheppo 		 * "max_xfer_sz".  Add room for one more cookie if
13341ae08745Sheppo 		 * "max_xfer_sz" isn't an integral multiple of the page size.
13351ae08745Sheppo 		 * Must first get the maximum transfer size in bytes.
13361ae08745Sheppo 		 */
13371ae08745Sheppo 		size_t	max_xfer_bytes = attr_msg->vdisk_block_size ?
13381ae08745Sheppo 		    attr_msg->vdisk_block_size*attr_msg->max_xfer_sz :
13391ae08745Sheppo 		    attr_msg->max_xfer_sz;
13401ae08745Sheppo 		size_t	max_inband_msglen =
13411ae08745Sheppo 		    sizeof (vd_dring_inband_msg_t) +
13421ae08745Sheppo 		    ((max_xfer_bytes/PAGESIZE +
13431ae08745Sheppo 			((max_xfer_bytes % PAGESIZE) ? 1 : 0))*
13441ae08745Sheppo 			(sizeof (ldc_mem_cookie_t)));
13451ae08745Sheppo 
13461ae08745Sheppo 		/*
13471ae08745Sheppo 		 * Set the maximum expected message length to
13481ae08745Sheppo 		 * accommodate in-band-descriptor messages with all
13491ae08745Sheppo 		 * their cookies
13501ae08745Sheppo 		 */
13511ae08745Sheppo 		vd->max_msglen = MAX(vd->max_msglen, max_inband_msglen);
1352d10e4ef2Snarayan 
1353d10e4ef2Snarayan 		/*
1354d10e4ef2Snarayan 		 * Initialize the data structure for processing in-band I/O
1355d10e4ef2Snarayan 		 * request descriptors
1356d10e4ef2Snarayan 		 */
1357d10e4ef2Snarayan 		vd->inband_task.vd	= vd;
13583af08d82Slm66018 		vd->inband_task.msg	= kmem_alloc(vd->max_msglen, KM_SLEEP);
1359d10e4ef2Snarayan 		vd->inband_task.index	= 0;
1360d10e4ef2Snarayan 		vd->inband_task.type	= VD_FINAL_RANGE_TASK;	/* range == 1 */
13611ae08745Sheppo 	}
13621ae08745Sheppo 
1363e1ebb9ecSlm66018 	/* Return the device's block size and max transfer size to the client */
1364e1ebb9ecSlm66018 	attr_msg->vdisk_block_size	= DEV_BSIZE;
1365e1ebb9ecSlm66018 	attr_msg->max_xfer_sz		= vd->max_xfer_sz;
1366e1ebb9ecSlm66018 
13671ae08745Sheppo 	attr_msg->vdisk_size = vd->vdisk_size;
13681ae08745Sheppo 	attr_msg->vdisk_type = vd->vdisk_type;
13691ae08745Sheppo 	attr_msg->operations = vds_operations;
13701ae08745Sheppo 	PR0("%s", VD_CLIENT(vd));
13713af08d82Slm66018 
13723af08d82Slm66018 	ASSERT(vd->dring_task == NULL);
13733af08d82Slm66018 
13741ae08745Sheppo 	return (0);
13751ae08745Sheppo }
13761ae08745Sheppo 
13771ae08745Sheppo static int
13781ae08745Sheppo vd_process_dring_reg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
13791ae08745Sheppo {
13801ae08745Sheppo 	int			status;
13811ae08745Sheppo 	size_t			expected;
13821ae08745Sheppo 	ldc_mem_info_t		dring_minfo;
13831ae08745Sheppo 	vio_dring_reg_msg_t	*reg_msg = (vio_dring_reg_msg_t *)msg;
13841ae08745Sheppo 
13851ae08745Sheppo 
13861ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
13871ae08745Sheppo 
13881ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
13891ae08745Sheppo 		VIO_DRING_REG)) {
1390d10e4ef2Snarayan 		PR0("Message is not a register-dring message");
1391d10e4ef2Snarayan 		return (ENOMSG);
13921ae08745Sheppo 	}
13931ae08745Sheppo 
13941ae08745Sheppo 	if (msglen < sizeof (*reg_msg)) {
13953af08d82Slm66018 		PR0("Expected at least %lu-byte register-dring message; "
13961ae08745Sheppo 		    "received %lu bytes", sizeof (*reg_msg), msglen);
13971ae08745Sheppo 		return (EBADMSG);
13981ae08745Sheppo 	}
13991ae08745Sheppo 
14001ae08745Sheppo 	expected = sizeof (*reg_msg) +
14011ae08745Sheppo 	    (reg_msg->ncookies - 1)*(sizeof (reg_msg->cookie[0]));
14021ae08745Sheppo 	if (msglen != expected) {
14033af08d82Slm66018 		PR0("Expected %lu-byte register-dring message; "
14041ae08745Sheppo 		    "received %lu bytes", expected, msglen);
14051ae08745Sheppo 		return (EBADMSG);
14061ae08745Sheppo 	}
14071ae08745Sheppo 
14081ae08745Sheppo 	if (vd->initialized & VD_DRING) {
14093af08d82Slm66018 		PR0("A dring was previously registered; only support one");
14101ae08745Sheppo 		return (EBADMSG);
14111ae08745Sheppo 	}
14121ae08745Sheppo 
1413d10e4ef2Snarayan 	if (reg_msg->num_descriptors > INT32_MAX) {
14143af08d82Slm66018 		PR0("reg_msg->num_descriptors = %u; must be <= %u (%s)",
1415d10e4ef2Snarayan 		    reg_msg->ncookies, INT32_MAX, STRINGIZE(INT32_MAX));
1416d10e4ef2Snarayan 		return (EBADMSG);
1417d10e4ef2Snarayan 	}
1418d10e4ef2Snarayan 
14191ae08745Sheppo 	if (reg_msg->ncookies != 1) {
14201ae08745Sheppo 		/*
14211ae08745Sheppo 		 * In addition to fixing the assertion in the success case
14221ae08745Sheppo 		 * below, supporting drings which require more than one
14231ae08745Sheppo 		 * "cookie" requires increasing the value of vd->max_msglen
14241ae08745Sheppo 		 * somewhere in the code path prior to receiving the message
14251ae08745Sheppo 		 * which results in calling this function.  Note that without
14261ae08745Sheppo 		 * making this change, the larger message size required to
14271ae08745Sheppo 		 * accommodate multiple cookies cannot be successfully
14281ae08745Sheppo 		 * received, so this function will not even get called.
14291ae08745Sheppo 		 * Gracefully accommodating more dring cookies might
14301ae08745Sheppo 		 * reasonably demand exchanging an additional attribute or
14311ae08745Sheppo 		 * making a minor protocol adjustment
14321ae08745Sheppo 		 */
14333af08d82Slm66018 		PR0("reg_msg->ncookies = %u != 1", reg_msg->ncookies);
14341ae08745Sheppo 		return (EBADMSG);
14351ae08745Sheppo 	}
14361ae08745Sheppo 
14371ae08745Sheppo 	status = ldc_mem_dring_map(vd->ldc_handle, reg_msg->cookie,
14381ae08745Sheppo 	    reg_msg->ncookies, reg_msg->num_descriptors,
14394bac2208Snarayan 	    reg_msg->descriptor_size, LDC_DIRECT_MAP, &vd->dring_handle);
14401ae08745Sheppo 	if (status != 0) {
14413af08d82Slm66018 		PR0("ldc_mem_dring_map() returned errno %d", status);
14421ae08745Sheppo 		return (status);
14431ae08745Sheppo 	}
14441ae08745Sheppo 
14451ae08745Sheppo 	/*
14461ae08745Sheppo 	 * To remove the need for this assertion, must call
14471ae08745Sheppo 	 * ldc_mem_dring_nextcookie() successfully ncookies-1 times after a
14481ae08745Sheppo 	 * successful call to ldc_mem_dring_map()
14491ae08745Sheppo 	 */
14501ae08745Sheppo 	ASSERT(reg_msg->ncookies == 1);
14511ae08745Sheppo 
14521ae08745Sheppo 	if ((status =
14531ae08745Sheppo 		ldc_mem_dring_info(vd->dring_handle, &dring_minfo)) != 0) {
14543af08d82Slm66018 		PR0("ldc_mem_dring_info() returned errno %d", status);
14551ae08745Sheppo 		if ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0)
14563af08d82Slm66018 			PR0("ldc_mem_dring_unmap() returned errno %d", status);
14571ae08745Sheppo 		return (status);
14581ae08745Sheppo 	}
14591ae08745Sheppo 
14601ae08745Sheppo 	if (dring_minfo.vaddr == NULL) {
14613af08d82Slm66018 		PR0("Descriptor ring virtual address is NULL");
14620a55fbb7Slm66018 		return (ENXIO);
14631ae08745Sheppo 	}
14641ae08745Sheppo 
14651ae08745Sheppo 
1466d10e4ef2Snarayan 	/* Initialize for valid message and mapped dring */
14671ae08745Sheppo 	PR1("descriptor size = %u, dring length = %u",
14681ae08745Sheppo 	    vd->descriptor_size, vd->dring_len);
14691ae08745Sheppo 	vd->initialized |= VD_DRING;
14701ae08745Sheppo 	vd->dring_ident = 1;	/* "There Can Be Only One" */
14711ae08745Sheppo 	vd->dring = dring_minfo.vaddr;
14721ae08745Sheppo 	vd->descriptor_size = reg_msg->descriptor_size;
14731ae08745Sheppo 	vd->dring_len = reg_msg->num_descriptors;
14741ae08745Sheppo 	reg_msg->dring_ident = vd->dring_ident;
1475d10e4ef2Snarayan 
1476d10e4ef2Snarayan 	/*
1477d10e4ef2Snarayan 	 * Allocate and initialize a "shadow" array of data structures for
1478d10e4ef2Snarayan 	 * tasks to process I/O requests in dring elements
1479d10e4ef2Snarayan 	 */
1480d10e4ef2Snarayan 	vd->dring_task =
1481d10e4ef2Snarayan 	    kmem_zalloc((sizeof (*vd->dring_task)) * vd->dring_len, KM_SLEEP);
1482d10e4ef2Snarayan 	for (int i = 0; i < vd->dring_len; i++) {
1483d10e4ef2Snarayan 		vd->dring_task[i].vd		= vd;
1484d10e4ef2Snarayan 		vd->dring_task[i].index		= i;
1485d10e4ef2Snarayan 		vd->dring_task[i].request	= &VD_DRING_ELEM(i)->payload;
14864bac2208Snarayan 
14874bac2208Snarayan 		status = ldc_mem_alloc_handle(vd->ldc_handle,
14884bac2208Snarayan 		    &(vd->dring_task[i].mhdl));
14894bac2208Snarayan 		if (status) {
14903af08d82Slm66018 			PR0("ldc_mem_alloc_handle() returned err %d ", status);
14914bac2208Snarayan 			return (ENXIO);
14924bac2208Snarayan 		}
14933af08d82Slm66018 
14943af08d82Slm66018 		vd->dring_task[i].msg = kmem_alloc(vd->max_msglen, KM_SLEEP);
1495d10e4ef2Snarayan 	}
1496d10e4ef2Snarayan 
14971ae08745Sheppo 	return (0);
14981ae08745Sheppo }
14991ae08745Sheppo 
15001ae08745Sheppo static int
15011ae08745Sheppo vd_process_dring_unreg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
15021ae08745Sheppo {
15031ae08745Sheppo 	vio_dring_unreg_msg_t	*unreg_msg = (vio_dring_unreg_msg_t *)msg;
15041ae08745Sheppo 
15051ae08745Sheppo 
15061ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
15071ae08745Sheppo 
15081ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
15091ae08745Sheppo 		VIO_DRING_UNREG)) {
1510d10e4ef2Snarayan 		PR0("Message is not an unregister-dring message");
1511d10e4ef2Snarayan 		return (ENOMSG);
15121ae08745Sheppo 	}
15131ae08745Sheppo 
15141ae08745Sheppo 	if (msglen != sizeof (*unreg_msg)) {
15153af08d82Slm66018 		PR0("Expected %lu-byte unregister-dring message; "
15161ae08745Sheppo 		    "received %lu bytes", sizeof (*unreg_msg), msglen);
15171ae08745Sheppo 		return (EBADMSG);
15181ae08745Sheppo 	}
15191ae08745Sheppo 
15201ae08745Sheppo 	if (unreg_msg->dring_ident != vd->dring_ident) {
15213af08d82Slm66018 		PR0("Expected dring ident %lu; received %lu",
15221ae08745Sheppo 		    vd->dring_ident, unreg_msg->dring_ident);
15231ae08745Sheppo 		return (EBADMSG);
15241ae08745Sheppo 	}
15251ae08745Sheppo 
15261ae08745Sheppo 	return (0);
15271ae08745Sheppo }
15281ae08745Sheppo 
15291ae08745Sheppo static int
15301ae08745Sheppo process_rdx_msg(vio_msg_t *msg, size_t msglen)
15311ae08745Sheppo {
15321ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
15331ae08745Sheppo 
1534d10e4ef2Snarayan 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX)) {
1535d10e4ef2Snarayan 		PR0("Message is not an RDX message");
1536d10e4ef2Snarayan 		return (ENOMSG);
1537d10e4ef2Snarayan 	}
15381ae08745Sheppo 
15391ae08745Sheppo 	if (msglen != sizeof (vio_rdx_msg_t)) {
15403af08d82Slm66018 		PR0("Expected %lu-byte RDX message; received %lu bytes",
15411ae08745Sheppo 		    sizeof (vio_rdx_msg_t), msglen);
15421ae08745Sheppo 		return (EBADMSG);
15431ae08745Sheppo 	}
15441ae08745Sheppo 
1545d10e4ef2Snarayan 	PR0("Valid RDX message");
15461ae08745Sheppo 	return (0);
15471ae08745Sheppo }
15481ae08745Sheppo 
15491ae08745Sheppo static int
15501ae08745Sheppo vd_check_seq_num(vd_t *vd, uint64_t seq_num)
15511ae08745Sheppo {
15521ae08745Sheppo 	if ((vd->initialized & VD_SEQ_NUM) && (seq_num != vd->seq_num + 1)) {
15533af08d82Slm66018 		PR0("Received seq_num %lu; expected %lu",
15541ae08745Sheppo 		    seq_num, (vd->seq_num + 1));
15553af08d82Slm66018 		PR0("initiating soft reset");
1556d10e4ef2Snarayan 		vd_need_reset(vd, B_FALSE);
15571ae08745Sheppo 		return (1);
15581ae08745Sheppo 	}
15591ae08745Sheppo 
15601ae08745Sheppo 	vd->seq_num = seq_num;
15611ae08745Sheppo 	vd->initialized |= VD_SEQ_NUM;	/* superfluous after first time... */
15621ae08745Sheppo 	return (0);
15631ae08745Sheppo }
15641ae08745Sheppo 
15651ae08745Sheppo /*
15661ae08745Sheppo  * Return the expected size of an inband-descriptor message with all the
15671ae08745Sheppo  * cookies it claims to include
15681ae08745Sheppo  */
15691ae08745Sheppo static size_t
15701ae08745Sheppo expected_inband_size(vd_dring_inband_msg_t *msg)
15711ae08745Sheppo {
15721ae08745Sheppo 	return ((sizeof (*msg)) +
15731ae08745Sheppo 	    (msg->payload.ncookies - 1)*(sizeof (msg->payload.cookie[0])));
15741ae08745Sheppo }
15751ae08745Sheppo 
15761ae08745Sheppo /*
15771ae08745Sheppo  * Process an in-band descriptor message:  used with clients like OBP, with
15781ae08745Sheppo  * which vds exchanges descriptors within VIO message payloads, rather than
15791ae08745Sheppo  * operating on them within a descriptor ring
15801ae08745Sheppo  */
15811ae08745Sheppo static int
15823af08d82Slm66018 vd_process_desc_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
15831ae08745Sheppo {
15841ae08745Sheppo 	size_t			expected;
15851ae08745Sheppo 	vd_dring_inband_msg_t	*desc_msg = (vd_dring_inband_msg_t *)msg;
15861ae08745Sheppo 
15871ae08745Sheppo 
15881ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
15891ae08745Sheppo 
15901ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
1591d10e4ef2Snarayan 		VIO_DESC_DATA)) {
1592d10e4ef2Snarayan 		PR1("Message is not an in-band-descriptor message");
1593d10e4ef2Snarayan 		return (ENOMSG);
1594d10e4ef2Snarayan 	}
15951ae08745Sheppo 
15961ae08745Sheppo 	if (msglen < sizeof (*desc_msg)) {
15973af08d82Slm66018 		PR0("Expected at least %lu-byte descriptor message; "
15981ae08745Sheppo 		    "received %lu bytes", sizeof (*desc_msg), msglen);
15991ae08745Sheppo 		return (EBADMSG);
16001ae08745Sheppo 	}
16011ae08745Sheppo 
16021ae08745Sheppo 	if (msglen != (expected = expected_inband_size(desc_msg))) {
16033af08d82Slm66018 		PR0("Expected %lu-byte descriptor message; "
16041ae08745Sheppo 		    "received %lu bytes", expected, msglen);
16051ae08745Sheppo 		return (EBADMSG);
16061ae08745Sheppo 	}
16071ae08745Sheppo 
1608d10e4ef2Snarayan 	if (vd_check_seq_num(vd, desc_msg->hdr.seq_num) != 0)
16091ae08745Sheppo 		return (EBADMSG);
16101ae08745Sheppo 
1611d10e4ef2Snarayan 	/*
1612d10e4ef2Snarayan 	 * Valid message:  Set up the in-band descriptor task and process the
1613d10e4ef2Snarayan 	 * request.  Arrange to acknowledge the client's message, unless an
1614d10e4ef2Snarayan 	 * error processing the descriptor task results in setting
1615d10e4ef2Snarayan 	 * VIO_SUBTYPE_NACK
1616d10e4ef2Snarayan 	 */
1617d10e4ef2Snarayan 	PR1("Valid in-band-descriptor message");
1618d10e4ef2Snarayan 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
16193af08d82Slm66018 
16203af08d82Slm66018 	ASSERT(vd->inband_task.msg != NULL);
16213af08d82Slm66018 
16223af08d82Slm66018 	bcopy(msg, vd->inband_task.msg, msglen);
1623d10e4ef2Snarayan 	vd->inband_task.msglen	= msglen;
16243af08d82Slm66018 
16253af08d82Slm66018 	/*
16263af08d82Slm66018 	 * The task request is now the payload of the message
16273af08d82Slm66018 	 * that was just copied into the body of the task.
16283af08d82Slm66018 	 */
16293af08d82Slm66018 	desc_msg = (vd_dring_inband_msg_t *)vd->inband_task.msg;
1630d10e4ef2Snarayan 	vd->inband_task.request	= &desc_msg->payload;
16313af08d82Slm66018 
1632d10e4ef2Snarayan 	return (vd_process_task(&vd->inband_task));
16331ae08745Sheppo }
16341ae08745Sheppo 
16351ae08745Sheppo static int
1636d10e4ef2Snarayan vd_process_element(vd_t *vd, vd_task_type_t type, uint32_t idx,
16373af08d82Slm66018     vio_msg_t *msg, size_t msglen)
16381ae08745Sheppo {
16391ae08745Sheppo 	int			status;
1640d10e4ef2Snarayan 	boolean_t		ready;
1641d10e4ef2Snarayan 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
16421ae08745Sheppo 
16431ae08745Sheppo 
1644d10e4ef2Snarayan 	/* Accept the updated dring element */
1645d10e4ef2Snarayan 	if ((status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) {
16463af08d82Slm66018 		PR0("ldc_mem_dring_acquire() returned errno %d", status);
16471ae08745Sheppo 		return (status);
16481ae08745Sheppo 	}
1649d10e4ef2Snarayan 	ready = (elem->hdr.dstate == VIO_DESC_READY);
1650d10e4ef2Snarayan 	if (ready) {
1651d10e4ef2Snarayan 		elem->hdr.dstate = VIO_DESC_ACCEPTED;
1652d10e4ef2Snarayan 	} else {
16533af08d82Slm66018 		PR0("descriptor %u not ready", idx);
1654d10e4ef2Snarayan 		VD_DUMP_DRING_ELEM(elem);
1655d10e4ef2Snarayan 	}
1656d10e4ef2Snarayan 	if ((status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) {
16573af08d82Slm66018 		PR0("ldc_mem_dring_release() returned errno %d", status);
16581ae08745Sheppo 		return (status);
16591ae08745Sheppo 	}
1660d10e4ef2Snarayan 	if (!ready)
1661d10e4ef2Snarayan 		return (EBUSY);
16621ae08745Sheppo 
16631ae08745Sheppo 
1664d10e4ef2Snarayan 	/* Initialize a task and process the accepted element */
1665d10e4ef2Snarayan 	PR1("Processing dring element %u", idx);
1666d10e4ef2Snarayan 	vd->dring_task[idx].type	= type;
16673af08d82Slm66018 
16683af08d82Slm66018 	/* duplicate msg buf for cookies etc. */
16693af08d82Slm66018 	bcopy(msg, vd->dring_task[idx].msg, msglen);
16703af08d82Slm66018 
1671d10e4ef2Snarayan 	vd->dring_task[idx].msglen	= msglen;
1672d10e4ef2Snarayan 	if ((status = vd_process_task(&vd->dring_task[idx])) != EINPROGRESS)
1673d10e4ef2Snarayan 		status = vd_mark_elem_done(vd, idx, elem->payload.status);
16741ae08745Sheppo 
16751ae08745Sheppo 	return (status);
16761ae08745Sheppo }
16771ae08745Sheppo 
16781ae08745Sheppo static int
1679d10e4ef2Snarayan vd_process_element_range(vd_t *vd, int start, int end,
16803af08d82Slm66018     vio_msg_t *msg, size_t msglen)
1681d10e4ef2Snarayan {
1682d10e4ef2Snarayan 	int		i, n, nelem, status = 0;
1683d10e4ef2Snarayan 	boolean_t	inprogress = B_FALSE;
1684d10e4ef2Snarayan 	vd_task_type_t	type;
1685d10e4ef2Snarayan 
1686d10e4ef2Snarayan 
1687d10e4ef2Snarayan 	ASSERT(start >= 0);
1688d10e4ef2Snarayan 	ASSERT(end >= 0);
1689d10e4ef2Snarayan 
1690d10e4ef2Snarayan 	/*
1691d10e4ef2Snarayan 	 * Arrange to acknowledge the client's message, unless an error
1692d10e4ef2Snarayan 	 * processing one of the dring elements results in setting
1693d10e4ef2Snarayan 	 * VIO_SUBTYPE_NACK
1694d10e4ef2Snarayan 	 */
1695d10e4ef2Snarayan 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
1696d10e4ef2Snarayan 
1697d10e4ef2Snarayan 	/*
1698d10e4ef2Snarayan 	 * Process the dring elements in the range
1699d10e4ef2Snarayan 	 */
1700d10e4ef2Snarayan 	nelem = ((end < start) ? end + vd->dring_len : end) - start + 1;
1701d10e4ef2Snarayan 	for (i = start, n = nelem; n > 0; i = (i + 1) % vd->dring_len, n--) {
1702d10e4ef2Snarayan 		((vio_dring_msg_t *)msg)->end_idx = i;
1703d10e4ef2Snarayan 		type = (n == 1) ? VD_FINAL_RANGE_TASK : VD_NONFINAL_RANGE_TASK;
17043af08d82Slm66018 		status = vd_process_element(vd, type, i, msg, msglen);
1705d10e4ef2Snarayan 		if (status == EINPROGRESS)
1706d10e4ef2Snarayan 			inprogress = B_TRUE;
1707d10e4ef2Snarayan 		else if (status != 0)
1708d10e4ef2Snarayan 			break;
1709d10e4ef2Snarayan 	}
1710d10e4ef2Snarayan 
1711d10e4ef2Snarayan 	/*
1712d10e4ef2Snarayan 	 * If some, but not all, operations of a multi-element range are in
1713d10e4ef2Snarayan 	 * progress, wait for other operations to complete before returning
1714d10e4ef2Snarayan 	 * (which will result in "ack" or "nack" of the message).  Note that
1715d10e4ef2Snarayan 	 * all outstanding operations will need to complete, not just the ones
1716d10e4ef2Snarayan 	 * corresponding to the current range of dring elements; howevever, as
1717d10e4ef2Snarayan 	 * this situation is an error case, performance is less critical.
1718d10e4ef2Snarayan 	 */
1719d10e4ef2Snarayan 	if ((nelem > 1) && (status != EINPROGRESS) && inprogress)
1720d10e4ef2Snarayan 		ddi_taskq_wait(vd->completionq);
1721d10e4ef2Snarayan 
1722d10e4ef2Snarayan 	return (status);
1723d10e4ef2Snarayan }
1724d10e4ef2Snarayan 
1725d10e4ef2Snarayan static int
17263af08d82Slm66018 vd_process_dring_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
17271ae08745Sheppo {
17281ae08745Sheppo 	vio_dring_msg_t	*dring_msg = (vio_dring_msg_t *)msg;
17291ae08745Sheppo 
17301ae08745Sheppo 
17311ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
17321ae08745Sheppo 
17331ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
17341ae08745Sheppo 		VIO_DRING_DATA)) {
1735d10e4ef2Snarayan 		PR1("Message is not a dring-data message");
1736d10e4ef2Snarayan 		return (ENOMSG);
17371ae08745Sheppo 	}
17381ae08745Sheppo 
17391ae08745Sheppo 	if (msglen != sizeof (*dring_msg)) {
17403af08d82Slm66018 		PR0("Expected %lu-byte dring message; received %lu bytes",
17411ae08745Sheppo 		    sizeof (*dring_msg), msglen);
17421ae08745Sheppo 		return (EBADMSG);
17431ae08745Sheppo 	}
17441ae08745Sheppo 
1745d10e4ef2Snarayan 	if (vd_check_seq_num(vd, dring_msg->seq_num) != 0)
17461ae08745Sheppo 		return (EBADMSG);
17471ae08745Sheppo 
17481ae08745Sheppo 	if (dring_msg->dring_ident != vd->dring_ident) {
17493af08d82Slm66018 		PR0("Expected dring ident %lu; received ident %lu",
17501ae08745Sheppo 		    vd->dring_ident, dring_msg->dring_ident);
17511ae08745Sheppo 		return (EBADMSG);
17521ae08745Sheppo 	}
17531ae08745Sheppo 
1754d10e4ef2Snarayan 	if (dring_msg->start_idx >= vd->dring_len) {
17553af08d82Slm66018 		PR0("\"start_idx\" = %u; must be less than %u",
1756d10e4ef2Snarayan 		    dring_msg->start_idx, vd->dring_len);
1757d10e4ef2Snarayan 		return (EBADMSG);
1758d10e4ef2Snarayan 	}
17591ae08745Sheppo 
1760d10e4ef2Snarayan 	if ((dring_msg->end_idx < 0) ||
1761d10e4ef2Snarayan 	    (dring_msg->end_idx >= vd->dring_len)) {
17623af08d82Slm66018 		PR0("\"end_idx\" = %u; must be >= 0 and less than %u",
1763d10e4ef2Snarayan 		    dring_msg->end_idx, vd->dring_len);
1764d10e4ef2Snarayan 		return (EBADMSG);
1765d10e4ef2Snarayan 	}
1766d10e4ef2Snarayan 
1767d10e4ef2Snarayan 	/* Valid message; process range of updated dring elements */
1768d10e4ef2Snarayan 	PR1("Processing descriptor range, start = %u, end = %u",
1769d10e4ef2Snarayan 	    dring_msg->start_idx, dring_msg->end_idx);
1770d10e4ef2Snarayan 	return (vd_process_element_range(vd, dring_msg->start_idx,
17713af08d82Slm66018 		dring_msg->end_idx, msg, msglen));
17721ae08745Sheppo }
17731ae08745Sheppo 
17741ae08745Sheppo static int
17751ae08745Sheppo recv_msg(ldc_handle_t ldc_handle, void *msg, size_t *nbytes)
17761ae08745Sheppo {
17771ae08745Sheppo 	int	retry, status;
17781ae08745Sheppo 	size_t	size = *nbytes;
17791ae08745Sheppo 
17801ae08745Sheppo 
17811ae08745Sheppo 	for (retry = 0, status = ETIMEDOUT;
17821ae08745Sheppo 	    retry < vds_ldc_retries && status == ETIMEDOUT;
17831ae08745Sheppo 	    retry++) {
17841ae08745Sheppo 		PR1("ldc_read() attempt %d", (retry + 1));
17851ae08745Sheppo 		*nbytes = size;
17861ae08745Sheppo 		status = ldc_read(ldc_handle, msg, nbytes);
17871ae08745Sheppo 	}
17881ae08745Sheppo 
17893af08d82Slm66018 	if (status) {
17903af08d82Slm66018 		PR0("ldc_read() returned errno %d", status);
17913af08d82Slm66018 		if (status != ECONNRESET)
17923af08d82Slm66018 			return (ENOMSG);
17931ae08745Sheppo 		return (status);
17941ae08745Sheppo 	} else if (*nbytes == 0) {
17951ae08745Sheppo 		PR1("ldc_read() returned 0 and no message read");
17961ae08745Sheppo 		return (ENOMSG);
17971ae08745Sheppo 	}
17981ae08745Sheppo 
17991ae08745Sheppo 	PR1("RCVD %lu-byte message", *nbytes);
18001ae08745Sheppo 	return (0);
18011ae08745Sheppo }
18021ae08745Sheppo 
18031ae08745Sheppo static int
18043af08d82Slm66018 vd_do_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
18051ae08745Sheppo {
18061ae08745Sheppo 	int		status;
18071ae08745Sheppo 
18081ae08745Sheppo 
18091ae08745Sheppo 	PR1("Processing (%x/%x/%x) message", msg->tag.vio_msgtype,
18101ae08745Sheppo 	    msg->tag.vio_subtype, msg->tag.vio_subtype_env);
18113af08d82Slm66018 #ifdef	DEBUG
18123af08d82Slm66018 	vd_decode_tag(msg);
18133af08d82Slm66018 #endif
18141ae08745Sheppo 
18151ae08745Sheppo 	/*
18161ae08745Sheppo 	 * Validate session ID up front, since it applies to all messages
18171ae08745Sheppo 	 * once set
18181ae08745Sheppo 	 */
18191ae08745Sheppo 	if ((msg->tag.vio_sid != vd->sid) && (vd->initialized & VD_SID)) {
18203af08d82Slm66018 		PR0("Expected SID %u, received %u", vd->sid,
18211ae08745Sheppo 		    msg->tag.vio_sid);
18221ae08745Sheppo 		return (EBADMSG);
18231ae08745Sheppo 	}
18241ae08745Sheppo 
18253af08d82Slm66018 	PR1("\tWhile in state %d (%s)", vd->state, vd_decode_state(vd->state));
18261ae08745Sheppo 
18271ae08745Sheppo 	/*
18281ae08745Sheppo 	 * Process the received message based on connection state
18291ae08745Sheppo 	 */
18301ae08745Sheppo 	switch (vd->state) {
18311ae08745Sheppo 	case VD_STATE_INIT:	/* expect version message */
18320a55fbb7Slm66018 		if ((status = vd_process_ver_msg(vd, msg, msglen)) != 0)
18331ae08745Sheppo 			return (status);
18341ae08745Sheppo 
18351ae08745Sheppo 		/* Version negotiated, move to that state */
18361ae08745Sheppo 		vd->state = VD_STATE_VER;
18371ae08745Sheppo 		return (0);
18381ae08745Sheppo 
18391ae08745Sheppo 	case VD_STATE_VER:	/* expect attribute message */
18401ae08745Sheppo 		if ((status = vd_process_attr_msg(vd, msg, msglen)) != 0)
18411ae08745Sheppo 			return (status);
18421ae08745Sheppo 
18431ae08745Sheppo 		/* Attributes exchanged, move to that state */
18441ae08745Sheppo 		vd->state = VD_STATE_ATTR;
18451ae08745Sheppo 		return (0);
18461ae08745Sheppo 
18471ae08745Sheppo 	case VD_STATE_ATTR:
18481ae08745Sheppo 		switch (vd->xfer_mode) {
18491ae08745Sheppo 		case VIO_DESC_MODE:	/* expect RDX message */
18501ae08745Sheppo 			if ((status = process_rdx_msg(msg, msglen)) != 0)
18511ae08745Sheppo 				return (status);
18521ae08745Sheppo 
18531ae08745Sheppo 			/* Ready to receive in-band descriptors */
18541ae08745Sheppo 			vd->state = VD_STATE_DATA;
18551ae08745Sheppo 			return (0);
18561ae08745Sheppo 
18571ae08745Sheppo 		case VIO_DRING_MODE:	/* expect register-dring message */
18581ae08745Sheppo 			if ((status =
18591ae08745Sheppo 				vd_process_dring_reg_msg(vd, msg, msglen)) != 0)
18601ae08745Sheppo 				return (status);
18611ae08745Sheppo 
18621ae08745Sheppo 			/* One dring negotiated, move to that state */
18631ae08745Sheppo 			vd->state = VD_STATE_DRING;
18641ae08745Sheppo 			return (0);
18651ae08745Sheppo 
18661ae08745Sheppo 		default:
18671ae08745Sheppo 			ASSERT("Unsupported transfer mode");
18683af08d82Slm66018 			PR0("Unsupported transfer mode");
18691ae08745Sheppo 			return (ENOTSUP);
18701ae08745Sheppo 		}
18711ae08745Sheppo 
18721ae08745Sheppo 	case VD_STATE_DRING:	/* expect RDX, register-dring, or unreg-dring */
18731ae08745Sheppo 		if ((status = process_rdx_msg(msg, msglen)) == 0) {
18741ae08745Sheppo 			/* Ready to receive data */
18751ae08745Sheppo 			vd->state = VD_STATE_DATA;
18761ae08745Sheppo 			return (0);
18771ae08745Sheppo 		} else if (status != ENOMSG) {
18781ae08745Sheppo 			return (status);
18791ae08745Sheppo 		}
18801ae08745Sheppo 
18811ae08745Sheppo 
18821ae08745Sheppo 		/*
18831ae08745Sheppo 		 * If another register-dring message is received, stay in
18841ae08745Sheppo 		 * dring state in case the client sends RDX; although the
18851ae08745Sheppo 		 * protocol allows multiple drings, this server does not
18861ae08745Sheppo 		 * support using more than one
18871ae08745Sheppo 		 */
18881ae08745Sheppo 		if ((status =
18891ae08745Sheppo 			vd_process_dring_reg_msg(vd, msg, msglen)) != ENOMSG)
18901ae08745Sheppo 			return (status);
18911ae08745Sheppo 
18921ae08745Sheppo 		/*
18931ae08745Sheppo 		 * Acknowledge an unregister-dring message, but reset the
18941ae08745Sheppo 		 * connection anyway:  Although the protocol allows
18951ae08745Sheppo 		 * unregistering drings, this server cannot serve a vdisk
18961ae08745Sheppo 		 * without its only dring
18971ae08745Sheppo 		 */
18981ae08745Sheppo 		status = vd_process_dring_unreg_msg(vd, msg, msglen);
18991ae08745Sheppo 		return ((status == 0) ? ENOTSUP : status);
19001ae08745Sheppo 
19011ae08745Sheppo 	case VD_STATE_DATA:
19021ae08745Sheppo 		switch (vd->xfer_mode) {
19031ae08745Sheppo 		case VIO_DESC_MODE:	/* expect in-band-descriptor message */
19043af08d82Slm66018 			return (vd_process_desc_msg(vd, msg, msglen));
19051ae08745Sheppo 
19061ae08745Sheppo 		case VIO_DRING_MODE:	/* expect dring-data or unreg-dring */
19071ae08745Sheppo 			/*
19081ae08745Sheppo 			 * Typically expect dring-data messages, so handle
19091ae08745Sheppo 			 * them first
19101ae08745Sheppo 			 */
19111ae08745Sheppo 			if ((status = vd_process_dring_msg(vd, msg,
19123af08d82Slm66018 				    msglen)) != ENOMSG)
19131ae08745Sheppo 				return (status);
19141ae08745Sheppo 
19151ae08745Sheppo 			/*
19161ae08745Sheppo 			 * Acknowledge an unregister-dring message, but reset
19171ae08745Sheppo 			 * the connection anyway:  Although the protocol
19181ae08745Sheppo 			 * allows unregistering drings, this server cannot
19191ae08745Sheppo 			 * serve a vdisk without its only dring
19201ae08745Sheppo 			 */
19211ae08745Sheppo 			status = vd_process_dring_unreg_msg(vd, msg, msglen);
19221ae08745Sheppo 			return ((status == 0) ? ENOTSUP : status);
19231ae08745Sheppo 
19241ae08745Sheppo 		default:
19251ae08745Sheppo 			ASSERT("Unsupported transfer mode");
19263af08d82Slm66018 			PR0("Unsupported transfer mode");
19271ae08745Sheppo 			return (ENOTSUP);
19281ae08745Sheppo 		}
19291ae08745Sheppo 
19301ae08745Sheppo 	default:
19311ae08745Sheppo 		ASSERT("Invalid client connection state");
19323af08d82Slm66018 		PR0("Invalid client connection state");
19331ae08745Sheppo 		return (ENOTSUP);
19341ae08745Sheppo 	}
19351ae08745Sheppo }
19361ae08745Sheppo 
1937d10e4ef2Snarayan static int
19383af08d82Slm66018 vd_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
19391ae08745Sheppo {
19401ae08745Sheppo 	int		status;
19411ae08745Sheppo 	boolean_t	reset_ldc = B_FALSE;
19421ae08745Sheppo 
19431ae08745Sheppo 
19441ae08745Sheppo 	/*
19451ae08745Sheppo 	 * Check that the message is at least big enough for a "tag", so that
19461ae08745Sheppo 	 * message processing can proceed based on tag-specified message type
19471ae08745Sheppo 	 */
19481ae08745Sheppo 	if (msglen < sizeof (vio_msg_tag_t)) {
19493af08d82Slm66018 		PR0("Received short (%lu-byte) message", msglen);
19501ae08745Sheppo 		/* Can't "nack" short message, so drop the big hammer */
19513af08d82Slm66018 		PR0("initiating full reset");
1952d10e4ef2Snarayan 		vd_need_reset(vd, B_TRUE);
1953d10e4ef2Snarayan 		return (EBADMSG);
19541ae08745Sheppo 	}
19551ae08745Sheppo 
19561ae08745Sheppo 	/*
19571ae08745Sheppo 	 * Process the message
19581ae08745Sheppo 	 */
19593af08d82Slm66018 	switch (status = vd_do_process_msg(vd, msg, msglen)) {
19601ae08745Sheppo 	case 0:
19611ae08745Sheppo 		/* "ack" valid, successfully-processed messages */
19621ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
19631ae08745Sheppo 		break;
19641ae08745Sheppo 
1965d10e4ef2Snarayan 	case EINPROGRESS:
1966d10e4ef2Snarayan 		/* The completion handler will "ack" or "nack" the message */
1967d10e4ef2Snarayan 		return (EINPROGRESS);
19681ae08745Sheppo 	case ENOMSG:
19693af08d82Slm66018 		PR0("Received unexpected message");
19701ae08745Sheppo 		_NOTE(FALLTHROUGH);
19711ae08745Sheppo 	case EBADMSG:
19721ae08745Sheppo 	case ENOTSUP:
19731ae08745Sheppo 		/* "nack" invalid messages */
19741ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
19751ae08745Sheppo 		break;
19761ae08745Sheppo 
19771ae08745Sheppo 	default:
19781ae08745Sheppo 		/* "nack" failed messages */
19791ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
19801ae08745Sheppo 		/* An LDC error probably occurred, so try resetting it */
19811ae08745Sheppo 		reset_ldc = B_TRUE;
19821ae08745Sheppo 		break;
19831ae08745Sheppo 	}
19841ae08745Sheppo 
19853af08d82Slm66018 	PR1("\tResulting in state %d (%s)", vd->state,
19863af08d82Slm66018 		vd_decode_state(vd->state));
19873af08d82Slm66018 
1988d10e4ef2Snarayan 	/* Send the "ack" or "nack" to the client */
19891ae08745Sheppo 	PR1("Sending %s",
19901ae08745Sheppo 	    (msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK");
19911ae08745Sheppo 	if (send_msg(vd->ldc_handle, msg, msglen) != 0)
19921ae08745Sheppo 		reset_ldc = B_TRUE;
19931ae08745Sheppo 
1994d10e4ef2Snarayan 	/* Arrange to reset the connection for nack'ed or failed messages */
19953af08d82Slm66018 	if ((status != 0) || reset_ldc) {
19963af08d82Slm66018 		PR0("initiating %s reset",
19973af08d82Slm66018 		    (reset_ldc) ? "full" : "soft");
1998d10e4ef2Snarayan 		vd_need_reset(vd, reset_ldc);
19993af08d82Slm66018 	}
2000d10e4ef2Snarayan 
2001d10e4ef2Snarayan 	return (status);
2002d10e4ef2Snarayan }
2003d10e4ef2Snarayan 
2004d10e4ef2Snarayan static boolean_t
2005d10e4ef2Snarayan vd_enabled(vd_t *vd)
2006d10e4ef2Snarayan {
2007d10e4ef2Snarayan 	boolean_t	enabled;
2008d10e4ef2Snarayan 
2009d10e4ef2Snarayan 
2010d10e4ef2Snarayan 	mutex_enter(&vd->lock);
2011d10e4ef2Snarayan 	enabled = vd->enabled;
2012d10e4ef2Snarayan 	mutex_exit(&vd->lock);
2013d10e4ef2Snarayan 	return (enabled);
20141ae08745Sheppo }
20151ae08745Sheppo 
20161ae08745Sheppo static void
20170a55fbb7Slm66018 vd_recv_msg(void *arg)
20181ae08745Sheppo {
20191ae08745Sheppo 	vd_t	*vd = (vd_t *)arg;
20203af08d82Slm66018 	int	rv = 0, status = 0;
20211ae08745Sheppo 
20221ae08745Sheppo 	ASSERT(vd != NULL);
20233af08d82Slm66018 
2024d10e4ef2Snarayan 	PR2("New task to receive incoming message(s)");
20253af08d82Slm66018 
20263af08d82Slm66018 
2027d10e4ef2Snarayan 	while (vd_enabled(vd) && status == 0) {
2028d10e4ef2Snarayan 		size_t		msglen, msgsize;
20293af08d82Slm66018 		ldc_status_t	lstatus;
2030d10e4ef2Snarayan 
20310a55fbb7Slm66018 		/*
2032d10e4ef2Snarayan 		 * Receive and process a message
20330a55fbb7Slm66018 		 */
2034d10e4ef2Snarayan 		vd_reset_if_needed(vd);	/* can change vd->max_msglen */
20353af08d82Slm66018 
20363af08d82Slm66018 		/*
20373af08d82Slm66018 		 * check if channel is UP - else break out of loop
20383af08d82Slm66018 		 */
20393af08d82Slm66018 		status = ldc_status(vd->ldc_handle, &lstatus);
20403af08d82Slm66018 		if (lstatus != LDC_UP) {
20413af08d82Slm66018 			PR0("channel not up (status=%d), exiting recv loop\n",
20423af08d82Slm66018 			    lstatus);
20433af08d82Slm66018 			break;
20443af08d82Slm66018 		}
20453af08d82Slm66018 
20463af08d82Slm66018 		ASSERT(vd->max_msglen != 0);
20473af08d82Slm66018 
2048d10e4ef2Snarayan 		msgsize = vd->max_msglen; /* stable copy for alloc/free */
20493af08d82Slm66018 		msglen	= msgsize;	  /* actual len after recv_msg() */
20503af08d82Slm66018 
20513af08d82Slm66018 		status = recv_msg(vd->ldc_handle, vd->vio_msgp, &msglen);
20523af08d82Slm66018 		switch (status) {
20533af08d82Slm66018 		case 0:
20543af08d82Slm66018 			rv = vd_process_msg(vd, (vio_msg_t *)vd->vio_msgp,
20553af08d82Slm66018 				msglen);
20563af08d82Slm66018 			/* check if max_msglen changed */
20573af08d82Slm66018 			if (msgsize != vd->max_msglen) {
20583af08d82Slm66018 				PR0("max_msglen changed 0x%lx to 0x%lx bytes\n",
20593af08d82Slm66018 				    msgsize, vd->max_msglen);
20603af08d82Slm66018 				kmem_free(vd->vio_msgp, msgsize);
20613af08d82Slm66018 				vd->vio_msgp =
20623af08d82Slm66018 					kmem_alloc(vd->max_msglen, KM_SLEEP);
20633af08d82Slm66018 			}
20643af08d82Slm66018 			if (rv == EINPROGRESS)
20653af08d82Slm66018 				continue;
20663af08d82Slm66018 			break;
20673af08d82Slm66018 
20683af08d82Slm66018 		case ENOMSG:
20693af08d82Slm66018 			break;
20703af08d82Slm66018 
20713af08d82Slm66018 		case ECONNRESET:
20723af08d82Slm66018 			PR0("initiating soft reset (ECONNRESET)\n");
20733af08d82Slm66018 			vd_need_reset(vd, B_FALSE);
20743af08d82Slm66018 			status = 0;
20753af08d82Slm66018 			break;
20763af08d82Slm66018 
20773af08d82Slm66018 		default:
2078d10e4ef2Snarayan 			/* Probably an LDC failure; arrange to reset it */
20793af08d82Slm66018 			PR0("initiating full reset (status=0x%x)", status);
2080d10e4ef2Snarayan 			vd_need_reset(vd, B_TRUE);
20813af08d82Slm66018 			break;
20820a55fbb7Slm66018 		}
20831ae08745Sheppo 	}
20843af08d82Slm66018 
2085d10e4ef2Snarayan 	PR2("Task finished");
20860a55fbb7Slm66018 }
20870a55fbb7Slm66018 
20880a55fbb7Slm66018 static uint_t
20891ae08745Sheppo vd_handle_ldc_events(uint64_t event, caddr_t arg)
20901ae08745Sheppo {
20911ae08745Sheppo 	vd_t	*vd = (vd_t *)(void *)arg;
20923af08d82Slm66018 	int	status;
20931ae08745Sheppo 
20941ae08745Sheppo 	ASSERT(vd != NULL);
2095d10e4ef2Snarayan 
2096d10e4ef2Snarayan 	if (!vd_enabled(vd))
2097d10e4ef2Snarayan 		return (LDC_SUCCESS);
2098d10e4ef2Snarayan 
20993af08d82Slm66018 	if (event & LDC_EVT_DOWN) {
210034683adeSsg70180 		PR0("LDC_EVT_DOWN: LDC channel went down");
21013af08d82Slm66018 
21023af08d82Slm66018 		vd_need_reset(vd, B_TRUE);
21033af08d82Slm66018 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
21043af08d82Slm66018 		    DDI_SLEEP);
21053af08d82Slm66018 		if (status == DDI_FAILURE) {
21063af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
21073af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
21083af08d82Slm66018 		}
21093af08d82Slm66018 	}
21103af08d82Slm66018 
2111d10e4ef2Snarayan 	if (event & LDC_EVT_RESET) {
21123af08d82Slm66018 		PR0("LDC_EVT_RESET: LDC channel was reset");
21133af08d82Slm66018 
21143af08d82Slm66018 		if (vd->state != VD_STATE_INIT) {
21153af08d82Slm66018 			PR0("scheduling full reset");
21163af08d82Slm66018 			vd_need_reset(vd, B_FALSE);
21173af08d82Slm66018 			status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
21183af08d82Slm66018 			    vd, DDI_SLEEP);
21193af08d82Slm66018 			if (status == DDI_FAILURE) {
21203af08d82Slm66018 				PR0("cannot schedule task to recv msg\n");
21213af08d82Slm66018 				vd_need_reset(vd, B_TRUE);
21223af08d82Slm66018 			}
21233af08d82Slm66018 
21243af08d82Slm66018 		} else {
21253af08d82Slm66018 			PR0("channel already reset, ignoring...\n");
21263af08d82Slm66018 			PR0("doing ldc up...\n");
21273af08d82Slm66018 			(void) ldc_up(vd->ldc_handle);
21283af08d82Slm66018 		}
21293af08d82Slm66018 
2130d10e4ef2Snarayan 		return (LDC_SUCCESS);
2131d10e4ef2Snarayan 	}
2132d10e4ef2Snarayan 
2133d10e4ef2Snarayan 	if (event & LDC_EVT_UP) {
21343af08d82Slm66018 		PR0("EVT_UP: LDC is up\nResetting client connection state");
21353af08d82Slm66018 		PR0("initiating soft reset");
2136d10e4ef2Snarayan 		vd_need_reset(vd, B_FALSE);
21373af08d82Slm66018 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
21383af08d82Slm66018 		    vd, DDI_SLEEP);
21393af08d82Slm66018 		if (status == DDI_FAILURE) {
21403af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
21413af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
21423af08d82Slm66018 			return (LDC_SUCCESS);
21433af08d82Slm66018 		}
2144d10e4ef2Snarayan 	}
2145d10e4ef2Snarayan 
2146d10e4ef2Snarayan 	if (event & LDC_EVT_READ) {
2147d10e4ef2Snarayan 		int	status;
2148d10e4ef2Snarayan 
2149d10e4ef2Snarayan 		PR1("New data available");
2150d10e4ef2Snarayan 		/* Queue a task to receive the new data */
2151d10e4ef2Snarayan 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
2152d10e4ef2Snarayan 		    DDI_SLEEP);
21533af08d82Slm66018 
21543af08d82Slm66018 		if (status == DDI_FAILURE) {
21553af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
21563af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
21573af08d82Slm66018 		}
2158d10e4ef2Snarayan 	}
2159d10e4ef2Snarayan 
2160d10e4ef2Snarayan 	return (LDC_SUCCESS);
21611ae08745Sheppo }
21621ae08745Sheppo 
21631ae08745Sheppo static uint_t
21641ae08745Sheppo vds_check_for_vd(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
21651ae08745Sheppo {
21661ae08745Sheppo 	_NOTE(ARGUNUSED(key, val))
21671ae08745Sheppo 	(*((uint_t *)arg))++;
21681ae08745Sheppo 	return (MH_WALK_TERMINATE);
21691ae08745Sheppo }
21701ae08745Sheppo 
21711ae08745Sheppo 
21721ae08745Sheppo static int
21731ae08745Sheppo vds_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
21741ae08745Sheppo {
21751ae08745Sheppo 	uint_t	vd_present = 0;
21761ae08745Sheppo 	minor_t	instance;
21771ae08745Sheppo 	vds_t	*vds;
21781ae08745Sheppo 
21791ae08745Sheppo 
21801ae08745Sheppo 	switch (cmd) {
21811ae08745Sheppo 	case DDI_DETACH:
21821ae08745Sheppo 		/* the real work happens below */
21831ae08745Sheppo 		break;
21841ae08745Sheppo 	case DDI_SUSPEND:
2185d10e4ef2Snarayan 		PR0("No action required for DDI_SUSPEND");
21861ae08745Sheppo 		return (DDI_SUCCESS);
21871ae08745Sheppo 	default:
21883af08d82Slm66018 		PR0("Unrecognized \"cmd\"");
21891ae08745Sheppo 		return (DDI_FAILURE);
21901ae08745Sheppo 	}
21911ae08745Sheppo 
21921ae08745Sheppo 	ASSERT(cmd == DDI_DETACH);
21931ae08745Sheppo 	instance = ddi_get_instance(dip);
21941ae08745Sheppo 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
21953af08d82Slm66018 		PR0("Could not get state for instance %u", instance);
21961ae08745Sheppo 		ddi_soft_state_free(vds_state, instance);
21971ae08745Sheppo 		return (DDI_FAILURE);
21981ae08745Sheppo 	}
21991ae08745Sheppo 
22001ae08745Sheppo 	/* Do no detach when serving any vdisks */
22011ae08745Sheppo 	mod_hash_walk(vds->vd_table, vds_check_for_vd, &vd_present);
22021ae08745Sheppo 	if (vd_present) {
22031ae08745Sheppo 		PR0("Not detaching because serving vdisks");
22041ae08745Sheppo 		return (DDI_FAILURE);
22051ae08745Sheppo 	}
22061ae08745Sheppo 
22071ae08745Sheppo 	PR0("Detaching");
2208*445b4c2eSsb155480 	if (vds->initialized & VDS_MDEG) {
22091ae08745Sheppo 		(void) mdeg_unregister(vds->mdeg);
2210*445b4c2eSsb155480 		kmem_free(vds->ispecp->specp, sizeof (vds_prop_template));
2211*445b4c2eSsb155480 		kmem_free(vds->ispecp, sizeof (mdeg_node_spec_t));
2212*445b4c2eSsb155480 		vds->ispecp = NULL;
2213*445b4c2eSsb155480 		vds->mdeg = NULL;
2214*445b4c2eSsb155480 	}
2215*445b4c2eSsb155480 
22161ae08745Sheppo 	if (vds->initialized & VDS_LDI)
22171ae08745Sheppo 		(void) ldi_ident_release(vds->ldi_ident);
22181ae08745Sheppo 	mod_hash_destroy_hash(vds->vd_table);
22191ae08745Sheppo 	ddi_soft_state_free(vds_state, instance);
22201ae08745Sheppo 	return (DDI_SUCCESS);
22211ae08745Sheppo }
22221ae08745Sheppo 
22231ae08745Sheppo static boolean_t
22241ae08745Sheppo is_pseudo_device(dev_info_t *dip)
22251ae08745Sheppo {
22261ae08745Sheppo 	dev_info_t	*parent, *root = ddi_root_node();
22271ae08745Sheppo 
22281ae08745Sheppo 
22291ae08745Sheppo 	for (parent = ddi_get_parent(dip); (parent != NULL) && (parent != root);
22301ae08745Sheppo 	    parent = ddi_get_parent(parent)) {
22311ae08745Sheppo 		if (strcmp(ddi_get_name(parent), DEVI_PSEUDO_NEXNAME) == 0)
22321ae08745Sheppo 			return (B_TRUE);
22331ae08745Sheppo 	}
22341ae08745Sheppo 
22351ae08745Sheppo 	return (B_FALSE);
22361ae08745Sheppo }
22371ae08745Sheppo 
22381ae08745Sheppo static int
22390a55fbb7Slm66018 vd_setup_full_disk(vd_t *vd)
22400a55fbb7Slm66018 {
22410a55fbb7Slm66018 	int		rval, status;
22420a55fbb7Slm66018 	major_t		major = getmajor(vd->dev[0]);
22430a55fbb7Slm66018 	minor_t		minor = getminor(vd->dev[0]) - VD_ENTIRE_DISK_SLICE;
22444bac2208Snarayan 	struct dk_minfo	dk_minfo;
22450a55fbb7Slm66018 
22464bac2208Snarayan 	/*
22474bac2208Snarayan 	 * At this point, vdisk_size is set to the size of partition 2 but
22484bac2208Snarayan 	 * this does not represent the size of the disk because partition 2
22494bac2208Snarayan 	 * may not cover the entire disk and its size does not include reserved
22504bac2208Snarayan 	 * blocks. So we update vdisk_size to be the size of the entire disk.
22514bac2208Snarayan 	 */
22524bac2208Snarayan 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCGMEDIAINFO,
22534bac2208Snarayan 	    (intptr_t)&dk_minfo, (vd_open_flags | FKIOCTL),
22544bac2208Snarayan 	    kcred, &rval)) != 0) {
225534683adeSsg70180 		PR0("ldi_ioctl(DKIOCGMEDIAINFO) returned errno %d",
22564bac2208Snarayan 		    status);
22570a55fbb7Slm66018 		return (status);
22580a55fbb7Slm66018 	}
22594bac2208Snarayan 	vd->vdisk_size = dk_minfo.dki_capacity;
22600a55fbb7Slm66018 
22610a55fbb7Slm66018 	/* Set full-disk parameters */
22620a55fbb7Slm66018 	vd->vdisk_type	= VD_DISK_TYPE_DISK;
22630a55fbb7Slm66018 	vd->nslices	= (sizeof (vd->dev))/(sizeof (vd->dev[0]));
22640a55fbb7Slm66018 
22650a55fbb7Slm66018 	/* Move dev number and LDI handle to entire-disk-slice array elements */
22660a55fbb7Slm66018 	vd->dev[VD_ENTIRE_DISK_SLICE]		= vd->dev[0];
22670a55fbb7Slm66018 	vd->dev[0]				= 0;
22680a55fbb7Slm66018 	vd->ldi_handle[VD_ENTIRE_DISK_SLICE]	= vd->ldi_handle[0];
22690a55fbb7Slm66018 	vd->ldi_handle[0]			= NULL;
22700a55fbb7Slm66018 
22710a55fbb7Slm66018 	/* Initialize device numbers for remaining slices and open them */
22720a55fbb7Slm66018 	for (int slice = 0; slice < vd->nslices; slice++) {
22730a55fbb7Slm66018 		/*
22740a55fbb7Slm66018 		 * Skip the entire-disk slice, as it's already open and its
22750a55fbb7Slm66018 		 * device known
22760a55fbb7Slm66018 		 */
22770a55fbb7Slm66018 		if (slice == VD_ENTIRE_DISK_SLICE)
22780a55fbb7Slm66018 			continue;
22790a55fbb7Slm66018 		ASSERT(vd->dev[slice] == 0);
22800a55fbb7Slm66018 		ASSERT(vd->ldi_handle[slice] == NULL);
22810a55fbb7Slm66018 
22820a55fbb7Slm66018 		/*
22830a55fbb7Slm66018 		 * Construct the device number for the current slice
22840a55fbb7Slm66018 		 */
22850a55fbb7Slm66018 		vd->dev[slice] = makedevice(major, (minor + slice));
22860a55fbb7Slm66018 
22870a55fbb7Slm66018 		/*
228834683adeSsg70180 		 * Open all slices of the disk to serve them to the client.
228934683adeSsg70180 		 * Slices are opened exclusively to prevent other threads or
229034683adeSsg70180 		 * processes in the service domain from performing I/O to
229134683adeSsg70180 		 * slices being accessed by a client.  Failure to open a slice
229234683adeSsg70180 		 * results in vds not serving this disk, as the client could
229334683adeSsg70180 		 * attempt (and should be able) to access any slice immediately.
229434683adeSsg70180 		 * Any slices successfully opened before a failure will get
229534683adeSsg70180 		 * closed by vds_destroy_vd() as a result of the error returned
229634683adeSsg70180 		 * by this function.
229734683adeSsg70180 		 *
229834683adeSsg70180 		 * We need to do the open with FNDELAY so that opening an empty
229934683adeSsg70180 		 * slice does not fail.
23000a55fbb7Slm66018 		 */
23010a55fbb7Slm66018 		PR0("Opening device major %u, minor %u = slice %u",
23020a55fbb7Slm66018 		    major, minor, slice);
23030a55fbb7Slm66018 		if ((status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK,
230434683adeSsg70180 		    vd_open_flags | FNDELAY, kcred, &vd->ldi_handle[slice],
23050a55fbb7Slm66018 		    vd->vds->ldi_ident)) != 0) {
230634683adeSsg70180 			PR0("ldi_open_by_dev() returned errno %d "
23070a55fbb7Slm66018 			    "for slice %u", status, slice);
23080a55fbb7Slm66018 			/* vds_destroy_vd() will close any open slices */
23090a55fbb7Slm66018 			return (status);
23100a55fbb7Slm66018 		}
23110a55fbb7Slm66018 	}
23120a55fbb7Slm66018 
23130a55fbb7Slm66018 	return (0);
23140a55fbb7Slm66018 }
23150a55fbb7Slm66018 
23160a55fbb7Slm66018 static int
23174bac2208Snarayan vd_setup_partition_efi(vd_t *vd)
23184bac2208Snarayan {
23194bac2208Snarayan 	efi_gpt_t *gpt;
23204bac2208Snarayan 	efi_gpe_t *gpe;
23214bac2208Snarayan 	struct uuid uuid = EFI_RESERVED;
23224bac2208Snarayan 	uint32_t crc;
23234bac2208Snarayan 	int length;
23244bac2208Snarayan 
23254bac2208Snarayan 	length = sizeof (efi_gpt_t) + sizeof (efi_gpe_t);
23264bac2208Snarayan 
23274bac2208Snarayan 	gpt = kmem_zalloc(length, KM_SLEEP);
23284bac2208Snarayan 	gpe = (efi_gpe_t *)(gpt + 1);
23294bac2208Snarayan 
23304bac2208Snarayan 	gpt->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
23314bac2208Snarayan 	gpt->efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
23324bac2208Snarayan 	gpt->efi_gpt_HeaderSize = LE_32(sizeof (efi_gpt_t));
23334bac2208Snarayan 	gpt->efi_gpt_FirstUsableLBA = LE_64(0ULL);
23344bac2208Snarayan 	gpt->efi_gpt_LastUsableLBA = LE_64(vd->vdisk_size - 1);
23354bac2208Snarayan 	gpt->efi_gpt_NumberOfPartitionEntries = LE_32(1);
23364bac2208Snarayan 	gpt->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (efi_gpe_t));
23374bac2208Snarayan 
23384bac2208Snarayan 	UUID_LE_CONVERT(gpe->efi_gpe_PartitionTypeGUID, uuid);
23394bac2208Snarayan 	gpe->efi_gpe_StartingLBA = gpt->efi_gpt_FirstUsableLBA;
23404bac2208Snarayan 	gpe->efi_gpe_EndingLBA = gpt->efi_gpt_LastUsableLBA;
23414bac2208Snarayan 
23424bac2208Snarayan 	CRC32(crc, gpe, sizeof (efi_gpe_t), -1U, crc32_table);
23434bac2208Snarayan 	gpt->efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
23444bac2208Snarayan 
23454bac2208Snarayan 	CRC32(crc, gpt, sizeof (efi_gpt_t), -1U, crc32_table);
23464bac2208Snarayan 	gpt->efi_gpt_HeaderCRC32 = LE_32(~crc);
23474bac2208Snarayan 
23484bac2208Snarayan 	vd->dk_efi.dki_lba = 0;
23494bac2208Snarayan 	vd->dk_efi.dki_length = length;
23504bac2208Snarayan 	vd->dk_efi.dki_data = gpt;
23514bac2208Snarayan 
23524bac2208Snarayan 	return (0);
23534bac2208Snarayan }
23544bac2208Snarayan 
23554bac2208Snarayan static int
2356e1ebb9ecSlm66018 vd_setup_vd(char *device_path, vd_t *vd)
23571ae08745Sheppo {
2358e1ebb9ecSlm66018 	int		rval, status;
23591ae08745Sheppo 	dev_info_t	*dip;
23601ae08745Sheppo 	struct dk_cinfo	dk_cinfo;
23611ae08745Sheppo 
23624bac2208Snarayan 	/*
23634bac2208Snarayan 	 * We need to open with FNDELAY so that opening an empty partition
23644bac2208Snarayan 	 * does not fail.
23654bac2208Snarayan 	 */
23664bac2208Snarayan 	if ((status = ldi_open_by_name(device_path, vd_open_flags | FNDELAY,
23674bac2208Snarayan 	    kcred, &vd->ldi_handle[0], vd->vds->ldi_ident)) != 0) {
2368e1ebb9ecSlm66018 		PRN("ldi_open_by_name(%s) = errno %d", device_path, status);
23690a55fbb7Slm66018 		return (status);
23700a55fbb7Slm66018 	}
23710a55fbb7Slm66018 
23724bac2208Snarayan 	/*
23734bac2208Snarayan 	 * nslices must be updated now so that vds_destroy_vd() will close
23744bac2208Snarayan 	 * the slice we have just opened in case of an error.
23754bac2208Snarayan 	 */
23764bac2208Snarayan 	vd->nslices = 1;
23774bac2208Snarayan 
2378e1ebb9ecSlm66018 	/* Get device number and size of backing device */
23790a55fbb7Slm66018 	if ((status = ldi_get_dev(vd->ldi_handle[0], &vd->dev[0])) != 0) {
23801ae08745Sheppo 		PRN("ldi_get_dev() returned errno %d for %s",
2381e1ebb9ecSlm66018 		    status, device_path);
23821ae08745Sheppo 		return (status);
23831ae08745Sheppo 	}
23840a55fbb7Slm66018 	if (ldi_get_size(vd->ldi_handle[0], &vd->vdisk_size) != DDI_SUCCESS) {
2385e1ebb9ecSlm66018 		PRN("ldi_get_size() failed for %s", device_path);
23861ae08745Sheppo 		return (EIO);
23871ae08745Sheppo 	}
2388e1ebb9ecSlm66018 	vd->vdisk_size = lbtodb(vd->vdisk_size);	/* convert to blocks */
23891ae08745Sheppo 
2390e1ebb9ecSlm66018 	/* Verify backing device supports dk_cinfo, dk_geom, and vtoc */
2391e1ebb9ecSlm66018 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO,
2392e1ebb9ecSlm66018 		    (intptr_t)&dk_cinfo, (vd_open_flags | FKIOCTL), kcred,
2393e1ebb9ecSlm66018 		    &rval)) != 0) {
2394e1ebb9ecSlm66018 		PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
2395e1ebb9ecSlm66018 		    status, device_path);
2396e1ebb9ecSlm66018 		return (status);
2397e1ebb9ecSlm66018 	}
2398e1ebb9ecSlm66018 	if (dk_cinfo.dki_partition >= V_NUMPAR) {
2399e1ebb9ecSlm66018 		PRN("slice %u >= maximum slice %u for %s",
2400e1ebb9ecSlm66018 		    dk_cinfo.dki_partition, V_NUMPAR, device_path);
2401e1ebb9ecSlm66018 		return (EIO);
2402e1ebb9ecSlm66018 	}
24034bac2208Snarayan 
24044bac2208Snarayan 	status = vd_read_vtoc(vd->ldi_handle[0], &vd->vtoc, &vd->vdisk_label);
24054bac2208Snarayan 
24064bac2208Snarayan 	if (status != 0) {
24074bac2208Snarayan 		PRN("vd_read_vtoc returned errno %d for %s",
2408e1ebb9ecSlm66018 		    status, device_path);
2409e1ebb9ecSlm66018 		return (status);
2410e1ebb9ecSlm66018 	}
24114bac2208Snarayan 
24124bac2208Snarayan 	if (vd->vdisk_label == VD_DISK_LABEL_VTOC &&
24134bac2208Snarayan 	    (status = ldi_ioctl(vd->ldi_handle[0], DKIOCGGEOM,
24144bac2208Snarayan 	    (intptr_t)&vd->dk_geom, (vd_open_flags | FKIOCTL),
24154bac2208Snarayan 	    kcred, &rval)) != 0) {
24164bac2208Snarayan 		    PRN("ldi_ioctl(DKIOCGEOM) returned errno %d for %s",
2417e1ebb9ecSlm66018 			status, device_path);
2418e1ebb9ecSlm66018 		    return (status);
2419e1ebb9ecSlm66018 	}
2420e1ebb9ecSlm66018 
2421e1ebb9ecSlm66018 	/* Store the device's max transfer size for return to the client */
2422e1ebb9ecSlm66018 	vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
2423e1ebb9ecSlm66018 
2424e1ebb9ecSlm66018 
2425e1ebb9ecSlm66018 	/* Determine if backing device is a pseudo device */
24261ae08745Sheppo 	if ((dip = ddi_hold_devi_by_instance(getmajor(vd->dev[0]),
24271ae08745Sheppo 		    dev_to_instance(vd->dev[0]), 0))  == NULL) {
2428e1ebb9ecSlm66018 		PRN("%s is no longer accessible", device_path);
24291ae08745Sheppo 		return (EIO);
24301ae08745Sheppo 	}
24311ae08745Sheppo 	vd->pseudo = is_pseudo_device(dip);
24321ae08745Sheppo 	ddi_release_devi(dip);
24331ae08745Sheppo 	if (vd->pseudo) {
24341ae08745Sheppo 		vd->vdisk_type	= VD_DISK_TYPE_SLICE;
24351ae08745Sheppo 		vd->nslices	= 1;
24361ae08745Sheppo 		return (0);	/* ...and we're done */
24371ae08745Sheppo 	}
24381ae08745Sheppo 
24391ae08745Sheppo 
24400a55fbb7Slm66018 	/* If slice is entire-disk slice, initialize for full disk */
24410a55fbb7Slm66018 	if (dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE)
24420a55fbb7Slm66018 		return (vd_setup_full_disk(vd));
24431ae08745Sheppo 
24440a55fbb7Slm66018 
2445e1ebb9ecSlm66018 	/* Otherwise, we have a non-entire slice of a device */
24461ae08745Sheppo 	vd->vdisk_type	= VD_DISK_TYPE_SLICE;
24471ae08745Sheppo 	vd->nslices	= 1;
24481ae08745Sheppo 
24494bac2208Snarayan 	if (vd->vdisk_label == VD_DISK_LABEL_EFI) {
24504bac2208Snarayan 		status = vd_setup_partition_efi(vd);
24514bac2208Snarayan 		return (status);
24524bac2208Snarayan 	}
24531ae08745Sheppo 
2454e1ebb9ecSlm66018 	/* Initialize dk_geom structure for single-slice device */
24551ae08745Sheppo 	if (vd->dk_geom.dkg_nsect == 0) {
24563af08d82Slm66018 		PR0("%s geometry claims 0 sectors per track", device_path);
24571ae08745Sheppo 		return (EIO);
24581ae08745Sheppo 	}
24591ae08745Sheppo 	if (vd->dk_geom.dkg_nhead == 0) {
24603af08d82Slm66018 		PR0("%s geometry claims 0 heads", device_path);
24611ae08745Sheppo 		return (EIO);
24621ae08745Sheppo 	}
24631ae08745Sheppo 	vd->dk_geom.dkg_ncyl =
2464e1ebb9ecSlm66018 	    vd->vdisk_size/vd->dk_geom.dkg_nsect/vd->dk_geom.dkg_nhead;
24651ae08745Sheppo 	vd->dk_geom.dkg_acyl = 0;
24661ae08745Sheppo 	vd->dk_geom.dkg_pcyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl;
24671ae08745Sheppo 
24681ae08745Sheppo 
2469e1ebb9ecSlm66018 	/* Initialize vtoc structure for single-slice device */
24701ae08745Sheppo 	bcopy(VD_VOLUME_NAME, vd->vtoc.v_volume,
24711ae08745Sheppo 	    MIN(sizeof (VD_VOLUME_NAME), sizeof (vd->vtoc.v_volume)));
24721ae08745Sheppo 	bzero(vd->vtoc.v_part, sizeof (vd->vtoc.v_part));
24731ae08745Sheppo 	vd->vtoc.v_nparts = 1;
24741ae08745Sheppo 	vd->vtoc.v_part[0].p_tag = V_UNASSIGNED;
24751ae08745Sheppo 	vd->vtoc.v_part[0].p_flag = 0;
24761ae08745Sheppo 	vd->vtoc.v_part[0].p_start = 0;
2477e1ebb9ecSlm66018 	vd->vtoc.v_part[0].p_size = vd->vdisk_size;
24781ae08745Sheppo 	bcopy(VD_ASCIILABEL, vd->vtoc.v_asciilabel,
24791ae08745Sheppo 	    MIN(sizeof (VD_ASCIILABEL), sizeof (vd->vtoc.v_asciilabel)));
24801ae08745Sheppo 
24811ae08745Sheppo 
24821ae08745Sheppo 	return (0);
24831ae08745Sheppo }
24841ae08745Sheppo 
24851ae08745Sheppo static int
2486e1ebb9ecSlm66018 vds_do_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t ldc_id,
24871ae08745Sheppo     vd_t **vdp)
24881ae08745Sheppo {
24891ae08745Sheppo 	char			tq_name[TASKQ_NAMELEN];
24900a55fbb7Slm66018 	int			status;
24911ae08745Sheppo 	ddi_iblock_cookie_t	iblock = NULL;
24921ae08745Sheppo 	ldc_attr_t		ldc_attr;
24931ae08745Sheppo 	vd_t			*vd;
24941ae08745Sheppo 
24951ae08745Sheppo 
24961ae08745Sheppo 	ASSERT(vds != NULL);
2497e1ebb9ecSlm66018 	ASSERT(device_path != NULL);
24981ae08745Sheppo 	ASSERT(vdp != NULL);
2499e1ebb9ecSlm66018 	PR0("Adding vdisk for %s", device_path);
25001ae08745Sheppo 
25011ae08745Sheppo 	if ((vd = kmem_zalloc(sizeof (*vd), KM_NOSLEEP)) == NULL) {
25021ae08745Sheppo 		PRN("No memory for virtual disk");
25031ae08745Sheppo 		return (EAGAIN);
25041ae08745Sheppo 	}
25051ae08745Sheppo 	*vdp = vd;	/* assign here so vds_destroy_vd() can cleanup later */
25061ae08745Sheppo 	vd->vds = vds;
25071ae08745Sheppo 
25081ae08745Sheppo 
25090a55fbb7Slm66018 	/* Open vdisk and initialize parameters */
2510e1ebb9ecSlm66018 	if ((status = vd_setup_vd(device_path, vd)) != 0)
25111ae08745Sheppo 		return (status);
25121ae08745Sheppo 	ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
25131ae08745Sheppo 	PR0("vdisk_type = %s, pseudo = %s, nslices = %u",
25141ae08745Sheppo 	    ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
25151ae08745Sheppo 	    (vd->pseudo ? "yes" : "no"), vd->nslices);
25161ae08745Sheppo 
25171ae08745Sheppo 
25181ae08745Sheppo 	/* Initialize locking */
25191ae08745Sheppo 	if (ddi_get_soft_iblock_cookie(vds->dip, DDI_SOFTINT_MED,
25201ae08745Sheppo 		&iblock) != DDI_SUCCESS) {
25211ae08745Sheppo 		PRN("Could not get iblock cookie.");
25221ae08745Sheppo 		return (EIO);
25231ae08745Sheppo 	}
25241ae08745Sheppo 
25251ae08745Sheppo 	mutex_init(&vd->lock, NULL, MUTEX_DRIVER, iblock);
25261ae08745Sheppo 	vd->initialized |= VD_LOCKING;
25271ae08745Sheppo 
25281ae08745Sheppo 
2529d10e4ef2Snarayan 	/* Create start and completion task queues for the vdisk */
2530d10e4ef2Snarayan 	(void) snprintf(tq_name, sizeof (tq_name), "vd_startq%lu", id);
25311ae08745Sheppo 	PR1("tq_name = %s", tq_name);
2532d10e4ef2Snarayan 	if ((vd->startq = ddi_taskq_create(vds->dip, tq_name, 1,
25331ae08745Sheppo 		    TASKQ_DEFAULTPRI, 0)) == NULL) {
25341ae08745Sheppo 		PRN("Could not create task queue");
25351ae08745Sheppo 		return (EIO);
25361ae08745Sheppo 	}
2537d10e4ef2Snarayan 	(void) snprintf(tq_name, sizeof (tq_name), "vd_completionq%lu", id);
2538d10e4ef2Snarayan 	PR1("tq_name = %s", tq_name);
2539d10e4ef2Snarayan 	if ((vd->completionq = ddi_taskq_create(vds->dip, tq_name, 1,
2540d10e4ef2Snarayan 		    TASKQ_DEFAULTPRI, 0)) == NULL) {
2541d10e4ef2Snarayan 		PRN("Could not create task queue");
2542d10e4ef2Snarayan 		return (EIO);
2543d10e4ef2Snarayan 	}
2544d10e4ef2Snarayan 	vd->enabled = 1;	/* before callback can dispatch to startq */
25451ae08745Sheppo 
25461ae08745Sheppo 
25471ae08745Sheppo 	/* Bring up LDC */
25481ae08745Sheppo 	ldc_attr.devclass	= LDC_DEV_BLK_SVC;
25491ae08745Sheppo 	ldc_attr.instance	= ddi_get_instance(vds->dip);
25501ae08745Sheppo 	ldc_attr.mode		= LDC_MODE_UNRELIABLE;
2551e1ebb9ecSlm66018 	ldc_attr.mtu		= VD_LDC_MTU;
25521ae08745Sheppo 	if ((status = ldc_init(ldc_id, &ldc_attr, &vd->ldc_handle)) != 0) {
25533af08d82Slm66018 		PR0("ldc_init(%lu) = errno %d", ldc_id, status);
25541ae08745Sheppo 		return (status);
25551ae08745Sheppo 	}
25561ae08745Sheppo 	vd->initialized |= VD_LDC;
25571ae08745Sheppo 
25581ae08745Sheppo 	if ((status = ldc_reg_callback(vd->ldc_handle, vd_handle_ldc_events,
25591ae08745Sheppo 		(caddr_t)vd)) != 0) {
25603af08d82Slm66018 		PR0("ldc_reg_callback() returned errno %d", status);
25611ae08745Sheppo 		return (status);
25621ae08745Sheppo 	}
25631ae08745Sheppo 
25641ae08745Sheppo 	if ((status = ldc_open(vd->ldc_handle)) != 0) {
25653af08d82Slm66018 		PR0("ldc_open() returned errno %d", status);
25661ae08745Sheppo 		return (status);
25671ae08745Sheppo 	}
25681ae08745Sheppo 
25693af08d82Slm66018 	if ((status = ldc_up(vd->ldc_handle)) != 0) {
257034683adeSsg70180 		PR0("ldc_up() returned errno %d", status);
25713af08d82Slm66018 	}
25723af08d82Slm66018 
25734bac2208Snarayan 	/* Allocate the inband task memory handle */
25744bac2208Snarayan 	status = ldc_mem_alloc_handle(vd->ldc_handle, &(vd->inband_task.mhdl));
25754bac2208Snarayan 	if (status) {
257634683adeSsg70180 		PR0("ldc_mem_alloc_handle() returned err %d ", status);
25774bac2208Snarayan 		return (ENXIO);
25784bac2208Snarayan 	}
25791ae08745Sheppo 
25801ae08745Sheppo 	/* Add the successfully-initialized vdisk to the server's table */
25811ae08745Sheppo 	if (mod_hash_insert(vds->vd_table, (mod_hash_key_t)id, vd) != 0) {
25821ae08745Sheppo 		PRN("Error adding vdisk ID %lu to table", id);
25831ae08745Sheppo 		return (EIO);
25841ae08745Sheppo 	}
25851ae08745Sheppo 
25863af08d82Slm66018 	/* Allocate the staging buffer */
25873af08d82Slm66018 	vd->max_msglen	= sizeof (vio_msg_t);	/* baseline vio message size */
25883af08d82Slm66018 	vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
25893af08d82Slm66018 
25903af08d82Slm66018 	/* store initial state */
25913af08d82Slm66018 	vd->state = VD_STATE_INIT;
25923af08d82Slm66018 
25931ae08745Sheppo 	return (0);
25941ae08745Sheppo }
25951ae08745Sheppo 
25963af08d82Slm66018 static void
25973af08d82Slm66018 vd_free_dring_task(vd_t *vdp)
25983af08d82Slm66018 {
25993af08d82Slm66018 	if (vdp->dring_task != NULL) {
26003af08d82Slm66018 		ASSERT(vdp->dring_len != 0);
26013af08d82Slm66018 		/* Free all dring_task memory handles */
26023af08d82Slm66018 		for (int i = 0; i < vdp->dring_len; i++) {
26033af08d82Slm66018 			(void) ldc_mem_free_handle(vdp->dring_task[i].mhdl);
26043af08d82Slm66018 			kmem_free(vdp->dring_task[i].msg, vdp->max_msglen);
26053af08d82Slm66018 			vdp->dring_task[i].msg = NULL;
26063af08d82Slm66018 		}
26073af08d82Slm66018 		kmem_free(vdp->dring_task,
26083af08d82Slm66018 		    (sizeof (*vdp->dring_task)) * vdp->dring_len);
26093af08d82Slm66018 		vdp->dring_task = NULL;
26103af08d82Slm66018 	}
26113af08d82Slm66018 }
26123af08d82Slm66018 
26131ae08745Sheppo /*
26141ae08745Sheppo  * Destroy the state associated with a virtual disk
26151ae08745Sheppo  */
26161ae08745Sheppo static void
26171ae08745Sheppo vds_destroy_vd(void *arg)
26181ae08745Sheppo {
26191ae08745Sheppo 	vd_t	*vd = (vd_t *)arg;
262034683adeSsg70180 	int	retry = 0, rv;
26211ae08745Sheppo 
26221ae08745Sheppo 	if (vd == NULL)
26231ae08745Sheppo 		return;
26241ae08745Sheppo 
2625d10e4ef2Snarayan 	PR0("Destroying vdisk state");
2626d10e4ef2Snarayan 
26274bac2208Snarayan 	if (vd->dk_efi.dki_data != NULL)
26284bac2208Snarayan 		kmem_free(vd->dk_efi.dki_data, vd->dk_efi.dki_length);
26294bac2208Snarayan 
26301ae08745Sheppo 	/* Disable queuing requests for the vdisk */
26311ae08745Sheppo 	if (vd->initialized & VD_LOCKING) {
26321ae08745Sheppo 		mutex_enter(&vd->lock);
26331ae08745Sheppo 		vd->enabled = 0;
26341ae08745Sheppo 		mutex_exit(&vd->lock);
26351ae08745Sheppo 	}
26361ae08745Sheppo 
2637d10e4ef2Snarayan 	/* Drain and destroy start queue (*before* destroying completionq) */
2638d10e4ef2Snarayan 	if (vd->startq != NULL)
2639d10e4ef2Snarayan 		ddi_taskq_destroy(vd->startq);	/* waits for queued tasks */
2640d10e4ef2Snarayan 
2641d10e4ef2Snarayan 	/* Drain and destroy completion queue (*before* shutting down LDC) */
2642d10e4ef2Snarayan 	if (vd->completionq != NULL)
2643d10e4ef2Snarayan 		ddi_taskq_destroy(vd->completionq);	/* waits for tasks */
2644d10e4ef2Snarayan 
26453af08d82Slm66018 	vd_free_dring_task(vd);
26463af08d82Slm66018 
264734683adeSsg70180 	/* Free the inband task memory handle */
264834683adeSsg70180 	(void) ldc_mem_free_handle(vd->inband_task.mhdl);
264934683adeSsg70180 
265034683adeSsg70180 	/* Shut down LDC */
265134683adeSsg70180 	if (vd->initialized & VD_LDC) {
265234683adeSsg70180 		/* unmap the dring */
265334683adeSsg70180 		if (vd->initialized & VD_DRING)
265434683adeSsg70180 			(void) ldc_mem_dring_unmap(vd->dring_handle);
265534683adeSsg70180 
265634683adeSsg70180 		/* close LDC channel - retry on EAGAIN */
265734683adeSsg70180 		while ((rv = ldc_close(vd->ldc_handle)) == EAGAIN) {
265834683adeSsg70180 			if (++retry > vds_ldc_retries) {
265934683adeSsg70180 				PR0("Timed out closing channel");
266034683adeSsg70180 				break;
266134683adeSsg70180 			}
266234683adeSsg70180 			drv_usecwait(vds_ldc_delay);
266334683adeSsg70180 		}
266434683adeSsg70180 		if (rv == 0) {
266534683adeSsg70180 			(void) ldc_unreg_callback(vd->ldc_handle);
266634683adeSsg70180 			(void) ldc_fini(vd->ldc_handle);
266734683adeSsg70180 		} else {
266834683adeSsg70180 			/*
266934683adeSsg70180 			 * Closing the LDC channel has failed. Ideally we should
267034683adeSsg70180 			 * fail here but there is no Zeus level infrastructure
267134683adeSsg70180 			 * to handle this. The MD has already been changed and
267234683adeSsg70180 			 * we have to do the close. So we try to do as much
267334683adeSsg70180 			 * clean up as we can.
267434683adeSsg70180 			 */
267534683adeSsg70180 			(void) ldc_set_cb_mode(vd->ldc_handle, LDC_CB_DISABLE);
267634683adeSsg70180 			while (ldc_unreg_callback(vd->ldc_handle) == EAGAIN)
267734683adeSsg70180 				drv_usecwait(vds_ldc_delay);
267834683adeSsg70180 		}
267934683adeSsg70180 	}
268034683adeSsg70180 
26813af08d82Slm66018 	/* Free the staging buffer for msgs */
26823af08d82Slm66018 	if (vd->vio_msgp != NULL) {
26833af08d82Slm66018 		kmem_free(vd->vio_msgp, vd->max_msglen);
26843af08d82Slm66018 		vd->vio_msgp = NULL;
26853af08d82Slm66018 	}
26863af08d82Slm66018 
26873af08d82Slm66018 	/* Free the inband message buffer */
26883af08d82Slm66018 	if (vd->inband_task.msg != NULL) {
26893af08d82Slm66018 		kmem_free(vd->inband_task.msg, vd->max_msglen);
26903af08d82Slm66018 		vd->inband_task.msg = NULL;
2691d10e4ef2Snarayan 	}
26921ae08745Sheppo 
26931ae08745Sheppo 	/* Close any open backing-device slices */
26941ae08745Sheppo 	for (uint_t slice = 0; slice < vd->nslices; slice++) {
26951ae08745Sheppo 		if (vd->ldi_handle[slice] != NULL) {
26961ae08745Sheppo 			PR0("Closing slice %u", slice);
26971ae08745Sheppo 			(void) ldi_close(vd->ldi_handle[slice],
26984bac2208Snarayan 			    vd_open_flags | FNDELAY, kcred);
26991ae08745Sheppo 		}
27001ae08745Sheppo 	}
27011ae08745Sheppo 
27021ae08745Sheppo 	/* Free lock */
27031ae08745Sheppo 	if (vd->initialized & VD_LOCKING)
27041ae08745Sheppo 		mutex_destroy(&vd->lock);
27051ae08745Sheppo 
27061ae08745Sheppo 	/* Finally, free the vdisk structure itself */
27071ae08745Sheppo 	kmem_free(vd, sizeof (*vd));
27081ae08745Sheppo }
27091ae08745Sheppo 
27101ae08745Sheppo static int
2711e1ebb9ecSlm66018 vds_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t ldc_id)
27121ae08745Sheppo {
27131ae08745Sheppo 	int	status;
27141ae08745Sheppo 	vd_t	*vd = NULL;
27151ae08745Sheppo 
27161ae08745Sheppo 
2717e1ebb9ecSlm66018 	if ((status = vds_do_init_vd(vds, id, device_path, ldc_id, &vd)) != 0)
27181ae08745Sheppo 		vds_destroy_vd(vd);
27191ae08745Sheppo 
27201ae08745Sheppo 	return (status);
27211ae08745Sheppo }
27221ae08745Sheppo 
27231ae08745Sheppo static int
27241ae08745Sheppo vds_do_get_ldc_id(md_t *md, mde_cookie_t vd_node, mde_cookie_t *channel,
27251ae08745Sheppo     uint64_t *ldc_id)
27261ae08745Sheppo {
27271ae08745Sheppo 	int	num_channels;
27281ae08745Sheppo 
27291ae08745Sheppo 
27301ae08745Sheppo 	/* Look for channel endpoint child(ren) of the vdisk MD node */
27311ae08745Sheppo 	if ((num_channels = md_scan_dag(md, vd_node,
27321ae08745Sheppo 		    md_find_name(md, VD_CHANNEL_ENDPOINT),
27331ae08745Sheppo 		    md_find_name(md, "fwd"), channel)) <= 0) {
27341ae08745Sheppo 		PRN("No \"%s\" found for virtual disk", VD_CHANNEL_ENDPOINT);
27351ae08745Sheppo 		return (-1);
27361ae08745Sheppo 	}
27371ae08745Sheppo 
27381ae08745Sheppo 	/* Get the "id" value for the first channel endpoint node */
27391ae08745Sheppo 	if (md_get_prop_val(md, channel[0], VD_ID_PROP, ldc_id) != 0) {
27401ae08745Sheppo 		PRN("No \"%s\" property found for \"%s\" of vdisk",
27411ae08745Sheppo 		    VD_ID_PROP, VD_CHANNEL_ENDPOINT);
27421ae08745Sheppo 		return (-1);
27431ae08745Sheppo 	}
27441ae08745Sheppo 
27451ae08745Sheppo 	if (num_channels > 1) {
27461ae08745Sheppo 		PRN("Using ID of first of multiple channels for this vdisk");
27471ae08745Sheppo 	}
27481ae08745Sheppo 
27491ae08745Sheppo 	return (0);
27501ae08745Sheppo }
27511ae08745Sheppo 
27521ae08745Sheppo static int
27531ae08745Sheppo vds_get_ldc_id(md_t *md, mde_cookie_t vd_node, uint64_t *ldc_id)
27541ae08745Sheppo {
27551ae08745Sheppo 	int		num_nodes, status;
27561ae08745Sheppo 	size_t		size;
27571ae08745Sheppo 	mde_cookie_t	*channel;
27581ae08745Sheppo 
27591ae08745Sheppo 
27601ae08745Sheppo 	if ((num_nodes = md_node_count(md)) <= 0) {
27611ae08745Sheppo 		PRN("Invalid node count in Machine Description subtree");
27621ae08745Sheppo 		return (-1);
27631ae08745Sheppo 	}
27641ae08745Sheppo 	size = num_nodes*(sizeof (*channel));
27651ae08745Sheppo 	channel = kmem_zalloc(size, KM_SLEEP);
27661ae08745Sheppo 	status = vds_do_get_ldc_id(md, vd_node, channel, ldc_id);
27671ae08745Sheppo 	kmem_free(channel, size);
27681ae08745Sheppo 
27691ae08745Sheppo 	return (status);
27701ae08745Sheppo }
27711ae08745Sheppo 
27721ae08745Sheppo static void
27731ae08745Sheppo vds_add_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
27741ae08745Sheppo {
2775e1ebb9ecSlm66018 	char		*device_path = NULL;
27761ae08745Sheppo 	uint64_t	id = 0, ldc_id = 0;
27771ae08745Sheppo 
27781ae08745Sheppo 
27791ae08745Sheppo 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
27801ae08745Sheppo 		PRN("Error getting vdisk \"%s\"", VD_ID_PROP);
27811ae08745Sheppo 		return;
27821ae08745Sheppo 	}
27831ae08745Sheppo 	PR0("Adding vdisk ID %lu", id);
27841ae08745Sheppo 	if (md_get_prop_str(md, vd_node, VD_BLOCK_DEVICE_PROP,
2785e1ebb9ecSlm66018 		&device_path) != 0) {
27861ae08745Sheppo 		PRN("Error getting vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
27871ae08745Sheppo 		return;
27881ae08745Sheppo 	}
27891ae08745Sheppo 
27901ae08745Sheppo 	if (vds_get_ldc_id(md, vd_node, &ldc_id) != 0) {
27911ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", id);
27921ae08745Sheppo 		return;
27931ae08745Sheppo 	}
27941ae08745Sheppo 
2795e1ebb9ecSlm66018 	if (vds_init_vd(vds, id, device_path, ldc_id) != 0) {
27961ae08745Sheppo 		PRN("Failed to add vdisk ID %lu", id);
27971ae08745Sheppo 		return;
27981ae08745Sheppo 	}
27991ae08745Sheppo }
28001ae08745Sheppo 
28011ae08745Sheppo static void
28021ae08745Sheppo vds_remove_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
28031ae08745Sheppo {
28041ae08745Sheppo 	uint64_t	id = 0;
28051ae08745Sheppo 
28061ae08745Sheppo 
28071ae08745Sheppo 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
28081ae08745Sheppo 		PRN("Unable to get \"%s\" property from vdisk's MD node",
28091ae08745Sheppo 		    VD_ID_PROP);
28101ae08745Sheppo 		return;
28111ae08745Sheppo 	}
28121ae08745Sheppo 	PR0("Removing vdisk ID %lu", id);
28131ae08745Sheppo 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0)
28141ae08745Sheppo 		PRN("No vdisk entry found for vdisk ID %lu", id);
28151ae08745Sheppo }
28161ae08745Sheppo 
28171ae08745Sheppo static void
28181ae08745Sheppo vds_change_vd(vds_t *vds, md_t *prev_md, mde_cookie_t prev_vd_node,
28191ae08745Sheppo     md_t *curr_md, mde_cookie_t curr_vd_node)
28201ae08745Sheppo {
28211ae08745Sheppo 	char		*curr_dev, *prev_dev;
28221ae08745Sheppo 	uint64_t	curr_id = 0, curr_ldc_id = 0;
28231ae08745Sheppo 	uint64_t	prev_id = 0, prev_ldc_id = 0;
28241ae08745Sheppo 	size_t		len;
28251ae08745Sheppo 
28261ae08745Sheppo 
28271ae08745Sheppo 	/* Validate that vdisk ID has not changed */
28281ae08745Sheppo 	if (md_get_prop_val(prev_md, prev_vd_node, VD_ID_PROP, &prev_id) != 0) {
28291ae08745Sheppo 		PRN("Error getting previous vdisk \"%s\" property",
28301ae08745Sheppo 		    VD_ID_PROP);
28311ae08745Sheppo 		return;
28321ae08745Sheppo 	}
28331ae08745Sheppo 	if (md_get_prop_val(curr_md, curr_vd_node, VD_ID_PROP, &curr_id) != 0) {
28341ae08745Sheppo 		PRN("Error getting current vdisk \"%s\" property", VD_ID_PROP);
28351ae08745Sheppo 		return;
28361ae08745Sheppo 	}
28371ae08745Sheppo 	if (curr_id != prev_id) {
28381ae08745Sheppo 		PRN("Not changing vdisk:  ID changed from %lu to %lu",
28391ae08745Sheppo 		    prev_id, curr_id);
28401ae08745Sheppo 		return;
28411ae08745Sheppo 	}
28421ae08745Sheppo 
28431ae08745Sheppo 	/* Validate that LDC ID has not changed */
28441ae08745Sheppo 	if (vds_get_ldc_id(prev_md, prev_vd_node, &prev_ldc_id) != 0) {
28451ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", prev_id);
28461ae08745Sheppo 		return;
28471ae08745Sheppo 	}
28481ae08745Sheppo 
28491ae08745Sheppo 	if (vds_get_ldc_id(curr_md, curr_vd_node, &curr_ldc_id) != 0) {
28501ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", curr_id);
28511ae08745Sheppo 		return;
28521ae08745Sheppo 	}
28531ae08745Sheppo 	if (curr_ldc_id != prev_ldc_id) {
28540a55fbb7Slm66018 		_NOTE(NOTREACHED);	/* lint is confused */
28551ae08745Sheppo 		PRN("Not changing vdisk:  "
28561ae08745Sheppo 		    "LDC ID changed from %lu to %lu", prev_ldc_id, curr_ldc_id);
28571ae08745Sheppo 		return;
28581ae08745Sheppo 	}
28591ae08745Sheppo 
28601ae08745Sheppo 	/* Determine whether device path has changed */
28611ae08745Sheppo 	if (md_get_prop_str(prev_md, prev_vd_node, VD_BLOCK_DEVICE_PROP,
28621ae08745Sheppo 		&prev_dev) != 0) {
28631ae08745Sheppo 		PRN("Error getting previous vdisk \"%s\"",
28641ae08745Sheppo 		    VD_BLOCK_DEVICE_PROP);
28651ae08745Sheppo 		return;
28661ae08745Sheppo 	}
28671ae08745Sheppo 	if (md_get_prop_str(curr_md, curr_vd_node, VD_BLOCK_DEVICE_PROP,
28681ae08745Sheppo 		&curr_dev) != 0) {
28691ae08745Sheppo 		PRN("Error getting current vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
28701ae08745Sheppo 		return;
28711ae08745Sheppo 	}
28721ae08745Sheppo 	if (((len = strlen(curr_dev)) == strlen(prev_dev)) &&
28731ae08745Sheppo 	    (strncmp(curr_dev, prev_dev, len) == 0))
28741ae08745Sheppo 		return;	/* no relevant (supported) change */
28751ae08745Sheppo 
28761ae08745Sheppo 	PR0("Changing vdisk ID %lu", prev_id);
28773af08d82Slm66018 
28781ae08745Sheppo 	/* Remove old state, which will close vdisk and reset */
28791ae08745Sheppo 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)prev_id) != 0)
28801ae08745Sheppo 		PRN("No entry found for vdisk ID %lu", prev_id);
28813af08d82Slm66018 
28821ae08745Sheppo 	/* Re-initialize vdisk with new state */
28831ae08745Sheppo 	if (vds_init_vd(vds, curr_id, curr_dev, curr_ldc_id) != 0) {
28841ae08745Sheppo 		PRN("Failed to change vdisk ID %lu", curr_id);
28851ae08745Sheppo 		return;
28861ae08745Sheppo 	}
28871ae08745Sheppo }
28881ae08745Sheppo 
28891ae08745Sheppo static int
28901ae08745Sheppo vds_process_md(void *arg, mdeg_result_t *md)
28911ae08745Sheppo {
28921ae08745Sheppo 	int	i;
28931ae08745Sheppo 	vds_t	*vds = arg;
28941ae08745Sheppo 
28951ae08745Sheppo 
28961ae08745Sheppo 	if (md == NULL)
28971ae08745Sheppo 		return (MDEG_FAILURE);
28981ae08745Sheppo 	ASSERT(vds != NULL);
28991ae08745Sheppo 
29001ae08745Sheppo 	for (i = 0; i < md->removed.nelem; i++)
29011ae08745Sheppo 		vds_remove_vd(vds, md->removed.mdp, md->removed.mdep[i]);
29021ae08745Sheppo 	for (i = 0; i < md->match_curr.nelem; i++)
29031ae08745Sheppo 		vds_change_vd(vds, md->match_prev.mdp, md->match_prev.mdep[i],
29041ae08745Sheppo 		    md->match_curr.mdp, md->match_curr.mdep[i]);
29051ae08745Sheppo 	for (i = 0; i < md->added.nelem; i++)
29061ae08745Sheppo 		vds_add_vd(vds, md->added.mdp, md->added.mdep[i]);
29071ae08745Sheppo 
29081ae08745Sheppo 	return (MDEG_SUCCESS);
29091ae08745Sheppo }
29101ae08745Sheppo 
29111ae08745Sheppo static int
29121ae08745Sheppo vds_do_attach(dev_info_t *dip)
29131ae08745Sheppo {
2914*445b4c2eSsb155480 	int			status, sz;
2915*445b4c2eSsb155480 	int			cfg_handle;
29161ae08745Sheppo 	minor_t			instance = ddi_get_instance(dip);
29171ae08745Sheppo 	vds_t			*vds;
2918*445b4c2eSsb155480 	mdeg_prop_spec_t	*pspecp;
2919*445b4c2eSsb155480 	mdeg_node_spec_t	*ispecp;
29201ae08745Sheppo 
29211ae08745Sheppo 	/*
29221ae08745Sheppo 	 * The "cfg-handle" property of a vds node in an MD contains the MD's
29231ae08745Sheppo 	 * notion of "instance", or unique identifier, for that node; OBP
29241ae08745Sheppo 	 * stores the value of the "cfg-handle" MD property as the value of
29251ae08745Sheppo 	 * the "reg" property on the node in the device tree it builds from
29261ae08745Sheppo 	 * the MD and passes to Solaris.  Thus, we look up the devinfo node's
29271ae08745Sheppo 	 * "reg" property value to uniquely identify this device instance when
29281ae08745Sheppo 	 * registering with the MD event-generation framework.  If the "reg"
29291ae08745Sheppo 	 * property cannot be found, the device tree state is presumably so
29301ae08745Sheppo 	 * broken that there is no point in continuing.
29311ae08745Sheppo 	 */
2932*445b4c2eSsb155480 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2933*445b4c2eSsb155480 		VD_REG_PROP)) {
2934*445b4c2eSsb155480 		PRN("vds \"%s\" property does not exist", VD_REG_PROP);
29351ae08745Sheppo 		return (DDI_FAILURE);
29361ae08745Sheppo 	}
29371ae08745Sheppo 
29381ae08745Sheppo 	/* Get the MD instance for later MDEG registration */
29391ae08745Sheppo 	cfg_handle = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2940*445b4c2eSsb155480 	    VD_REG_PROP, -1);
29411ae08745Sheppo 
29421ae08745Sheppo 	if (ddi_soft_state_zalloc(vds_state, instance) != DDI_SUCCESS) {
29431ae08745Sheppo 		PRN("Could not allocate state for instance %u", instance);
29441ae08745Sheppo 		return (DDI_FAILURE);
29451ae08745Sheppo 	}
29461ae08745Sheppo 
29471ae08745Sheppo 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
29481ae08745Sheppo 		PRN("Could not get state for instance %u", instance);
29491ae08745Sheppo 		ddi_soft_state_free(vds_state, instance);
29501ae08745Sheppo 		return (DDI_FAILURE);
29511ae08745Sheppo 	}
29521ae08745Sheppo 
29531ae08745Sheppo 	vds->dip	= dip;
29541ae08745Sheppo 	vds->vd_table	= mod_hash_create_ptrhash("vds_vd_table", VDS_NCHAINS,
29551ae08745Sheppo 							vds_destroy_vd,
29561ae08745Sheppo 							sizeof (void *));
29571ae08745Sheppo 	ASSERT(vds->vd_table != NULL);
29581ae08745Sheppo 
29591ae08745Sheppo 	if ((status = ldi_ident_from_dip(dip, &vds->ldi_ident)) != 0) {
29601ae08745Sheppo 		PRN("ldi_ident_from_dip() returned errno %d", status);
29611ae08745Sheppo 		return (DDI_FAILURE);
29621ae08745Sheppo 	}
29631ae08745Sheppo 	vds->initialized |= VDS_LDI;
29641ae08745Sheppo 
29651ae08745Sheppo 	/* Register for MD updates */
2966*445b4c2eSsb155480 	sz = sizeof (vds_prop_template);
2967*445b4c2eSsb155480 	pspecp = kmem_alloc(sz, KM_SLEEP);
2968*445b4c2eSsb155480 	bcopy(vds_prop_template, pspecp, sz);
2969*445b4c2eSsb155480 
2970*445b4c2eSsb155480 	VDS_SET_MDEG_PROP_INST(pspecp, cfg_handle);
2971*445b4c2eSsb155480 
2972*445b4c2eSsb155480 	/* initialize the complete prop spec structure */
2973*445b4c2eSsb155480 	ispecp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP);
2974*445b4c2eSsb155480 	ispecp->namep = "virtual-device";
2975*445b4c2eSsb155480 	ispecp->specp = pspecp;
2976*445b4c2eSsb155480 
2977*445b4c2eSsb155480 	if (mdeg_register(ispecp, &vd_match, vds_process_md, vds,
29781ae08745Sheppo 		&vds->mdeg) != MDEG_SUCCESS) {
29791ae08745Sheppo 		PRN("Unable to register for MD updates");
2980*445b4c2eSsb155480 		kmem_free(ispecp, sizeof (mdeg_node_spec_t));
2981*445b4c2eSsb155480 		kmem_free(pspecp, sz);
29821ae08745Sheppo 		return (DDI_FAILURE);
29831ae08745Sheppo 	}
2984*445b4c2eSsb155480 
2985*445b4c2eSsb155480 	vds->ispecp = ispecp;
29861ae08745Sheppo 	vds->initialized |= VDS_MDEG;
29871ae08745Sheppo 
29880a55fbb7Slm66018 	/* Prevent auto-detaching so driver is available whenever MD changes */
29890a55fbb7Slm66018 	if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1) !=
29900a55fbb7Slm66018 	    DDI_PROP_SUCCESS) {
29910a55fbb7Slm66018 		PRN("failed to set \"%s\" property for instance %u",
29920a55fbb7Slm66018 		    DDI_NO_AUTODETACH, instance);
29930a55fbb7Slm66018 	}
29940a55fbb7Slm66018 
29951ae08745Sheppo 	ddi_report_dev(dip);
29961ae08745Sheppo 	return (DDI_SUCCESS);
29971ae08745Sheppo }
29981ae08745Sheppo 
29991ae08745Sheppo static int
30001ae08745Sheppo vds_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
30011ae08745Sheppo {
30021ae08745Sheppo 	int	status;
30031ae08745Sheppo 
30041ae08745Sheppo 	switch (cmd) {
30051ae08745Sheppo 	case DDI_ATTACH:
3006d10e4ef2Snarayan 		PR0("Attaching");
30071ae08745Sheppo 		if ((status = vds_do_attach(dip)) != DDI_SUCCESS)
30081ae08745Sheppo 			(void) vds_detach(dip, DDI_DETACH);
30091ae08745Sheppo 		return (status);
30101ae08745Sheppo 	case DDI_RESUME:
3011d10e4ef2Snarayan 		PR0("No action required for DDI_RESUME");
30121ae08745Sheppo 		return (DDI_SUCCESS);
30131ae08745Sheppo 	default:
30141ae08745Sheppo 		return (DDI_FAILURE);
30151ae08745Sheppo 	}
30161ae08745Sheppo }
30171ae08745Sheppo 
30181ae08745Sheppo static struct dev_ops vds_ops = {
30191ae08745Sheppo 	DEVO_REV,	/* devo_rev */
30201ae08745Sheppo 	0,		/* devo_refcnt */
30211ae08745Sheppo 	ddi_no_info,	/* devo_getinfo */
30221ae08745Sheppo 	nulldev,	/* devo_identify */
30231ae08745Sheppo 	nulldev,	/* devo_probe */
30241ae08745Sheppo 	vds_attach,	/* devo_attach */
30251ae08745Sheppo 	vds_detach,	/* devo_detach */
30261ae08745Sheppo 	nodev,		/* devo_reset */
30271ae08745Sheppo 	NULL,		/* devo_cb_ops */
30281ae08745Sheppo 	NULL,		/* devo_bus_ops */
30291ae08745Sheppo 	nulldev		/* devo_power */
30301ae08745Sheppo };
30311ae08745Sheppo 
30321ae08745Sheppo static struct modldrv modldrv = {
30331ae08745Sheppo 	&mod_driverops,
30341ae08745Sheppo 	"virtual disk server v%I%",
30351ae08745Sheppo 	&vds_ops,
30361ae08745Sheppo };
30371ae08745Sheppo 
30381ae08745Sheppo static struct modlinkage modlinkage = {
30391ae08745Sheppo 	MODREV_1,
30401ae08745Sheppo 	&modldrv,
30411ae08745Sheppo 	NULL
30421ae08745Sheppo };
30431ae08745Sheppo 
30441ae08745Sheppo 
30451ae08745Sheppo int
30461ae08745Sheppo _init(void)
30471ae08745Sheppo {
30481ae08745Sheppo 	int		i, status;
30491ae08745Sheppo 
3050d10e4ef2Snarayan 
30511ae08745Sheppo 	if ((status = ddi_soft_state_init(&vds_state, sizeof (vds_t), 1)) != 0)
30521ae08745Sheppo 		return (status);
30531ae08745Sheppo 	if ((status = mod_install(&modlinkage)) != 0) {
30541ae08745Sheppo 		ddi_soft_state_fini(&vds_state);
30551ae08745Sheppo 		return (status);
30561ae08745Sheppo 	}
30571ae08745Sheppo 
30581ae08745Sheppo 	/* Fill in the bit-mask of server-supported operations */
30591ae08745Sheppo 	for (i = 0; i < vds_noperations; i++)
30601ae08745Sheppo 		vds_operations |= 1 << (vds_operation[i].operation - 1);
30611ae08745Sheppo 
30621ae08745Sheppo 	return (0);
30631ae08745Sheppo }
30641ae08745Sheppo 
30651ae08745Sheppo int
30661ae08745Sheppo _info(struct modinfo *modinfop)
30671ae08745Sheppo {
30681ae08745Sheppo 	return (mod_info(&modlinkage, modinfop));
30691ae08745Sheppo }
30701ae08745Sheppo 
30711ae08745Sheppo int
30721ae08745Sheppo _fini(void)
30731ae08745Sheppo {
30741ae08745Sheppo 	int	status;
30751ae08745Sheppo 
3076d10e4ef2Snarayan 
30771ae08745Sheppo 	if ((status = mod_remove(&modlinkage)) != 0)
30781ae08745Sheppo 		return (status);
30791ae08745Sheppo 	ddi_soft_state_fini(&vds_state);
30801ae08745Sheppo 	return (0);
30811ae08745Sheppo }
3082