xref: /titanic_53/usr/src/uts/sun4v/io/vds.c (revision 34683adecebe88ca2c857e28be4749f3a083f9fc)
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 */
58*34683adeSsg70180 #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"
721ae08745Sheppo 
731ae08745Sheppo /* Virtual disk initialization flags */
741ae08745Sheppo #define	VD_LOCKING		0x01
75d10e4ef2Snarayan #define	VD_LDC			0x02
76d10e4ef2Snarayan #define	VD_DRING		0x04
77d10e4ef2Snarayan #define	VD_SID			0x08
78d10e4ef2Snarayan #define	VD_SEQ_NUM		0x10
791ae08745Sheppo 
801ae08745Sheppo /* Flags for opening/closing backing devices via LDI */
811ae08745Sheppo #define	VD_OPEN_FLAGS		(FEXCL | FREAD | FWRITE)
821ae08745Sheppo 
831ae08745Sheppo /*
841ae08745Sheppo  * By Solaris convention, slice/partition 2 represents the entire disk;
851ae08745Sheppo  * unfortunately, this convention does not appear to be codified.
861ae08745Sheppo  */
871ae08745Sheppo #define	VD_ENTIRE_DISK_SLICE	2
881ae08745Sheppo 
891ae08745Sheppo /* Return a cpp token as a string */
901ae08745Sheppo #define	STRINGIZE(token)	#token
911ae08745Sheppo 
921ae08745Sheppo /*
931ae08745Sheppo  * Print a message prefixed with the current function name to the message log
941ae08745Sheppo  * (and optionally to the console for verbose boots); these macros use cpp's
951ae08745Sheppo  * concatenation of string literals and C99 variable-length-argument-list
961ae08745Sheppo  * macros
971ae08745Sheppo  */
981ae08745Sheppo #define	PRN(...)	_PRN("?%s():  "__VA_ARGS__, "")
991ae08745Sheppo #define	_PRN(format, ...)					\
1001ae08745Sheppo 	cmn_err(CE_CONT, format"%s", __func__, __VA_ARGS__)
1011ae08745Sheppo 
1021ae08745Sheppo /* Return a pointer to the "i"th vdisk dring element */
1031ae08745Sheppo #define	VD_DRING_ELEM(i)	((vd_dring_entry_t *)(void *)	\
1041ae08745Sheppo 	    (vd->dring + (i)*vd->descriptor_size))
1051ae08745Sheppo 
1061ae08745Sheppo /* Return the virtual disk client's type as a string (for use in messages) */
1071ae08745Sheppo #define	VD_CLIENT(vd)							\
1081ae08745Sheppo 	(((vd)->xfer_mode == VIO_DESC_MODE) ? "in-band client" :	\
1091ae08745Sheppo 	    (((vd)->xfer_mode == VIO_DRING_MODE) ? "dring client" :	\
1101ae08745Sheppo 		(((vd)->xfer_mode == 0) ? "null client" :		\
1111ae08745Sheppo 		    "unsupported client")))
1121ae08745Sheppo 
1131ae08745Sheppo /* Debugging macros */
1141ae08745Sheppo #ifdef DEBUG
1153af08d82Slm66018 
1163af08d82Slm66018 static int	vd_msglevel = 0;
1173af08d82Slm66018 
1183af08d82Slm66018 
1191ae08745Sheppo #define	PR0 if (vd_msglevel > 0)	PRN
1201ae08745Sheppo #define	PR1 if (vd_msglevel > 1)	PRN
1211ae08745Sheppo #define	PR2 if (vd_msglevel > 2)	PRN
1221ae08745Sheppo 
1231ae08745Sheppo #define	VD_DUMP_DRING_ELEM(elem)					\
1241ae08745Sheppo 	PRN("dst:%x op:%x st:%u nb:%lx addr:%lx ncook:%u\n",		\
1251ae08745Sheppo 	    elem->hdr.dstate,						\
1261ae08745Sheppo 	    elem->payload.operation,					\
1271ae08745Sheppo 	    elem->payload.status,					\
1281ae08745Sheppo 	    elem->payload.nbytes,					\
1291ae08745Sheppo 	    elem->payload.addr,						\
1301ae08745Sheppo 	    elem->payload.ncookies);
1311ae08745Sheppo 
1323af08d82Slm66018 char *
1333af08d82Slm66018 vd_decode_state(int state)
1343af08d82Slm66018 {
1353af08d82Slm66018 	char *str;
1363af08d82Slm66018 
1373af08d82Slm66018 #define	CASE_STATE(_s)	case _s: str = #_s; break;
1383af08d82Slm66018 
1393af08d82Slm66018 	switch (state) {
1403af08d82Slm66018 	CASE_STATE(VD_STATE_INIT)
1413af08d82Slm66018 	CASE_STATE(VD_STATE_VER)
1423af08d82Slm66018 	CASE_STATE(VD_STATE_ATTR)
1433af08d82Slm66018 	CASE_STATE(VD_STATE_DRING)
1443af08d82Slm66018 	CASE_STATE(VD_STATE_RDX)
1453af08d82Slm66018 	CASE_STATE(VD_STATE_DATA)
1463af08d82Slm66018 	default: str = "unknown"; break;
1473af08d82Slm66018 	}
1483af08d82Slm66018 
1493af08d82Slm66018 #undef CASE_STATE
1503af08d82Slm66018 
1513af08d82Slm66018 	return (str);
1523af08d82Slm66018 }
1533af08d82Slm66018 
1543af08d82Slm66018 void
1553af08d82Slm66018 vd_decode_tag(vio_msg_t *msg)
1563af08d82Slm66018 {
1573af08d82Slm66018 	char *tstr, *sstr, *estr;
1583af08d82Slm66018 
1593af08d82Slm66018 #define	CASE_TYPE(_s)	case _s: tstr = #_s; break;
1603af08d82Slm66018 
1613af08d82Slm66018 	switch (msg->tag.vio_msgtype) {
1623af08d82Slm66018 	CASE_TYPE(VIO_TYPE_CTRL)
1633af08d82Slm66018 	CASE_TYPE(VIO_TYPE_DATA)
1643af08d82Slm66018 	CASE_TYPE(VIO_TYPE_ERR)
1653af08d82Slm66018 	default: tstr = "unknown"; break;
1663af08d82Slm66018 	}
1673af08d82Slm66018 
1683af08d82Slm66018 #undef CASE_TYPE
1693af08d82Slm66018 
1703af08d82Slm66018 #define	CASE_SUBTYPE(_s) case _s: sstr = #_s; break;
1713af08d82Slm66018 
1723af08d82Slm66018 	switch (msg->tag.vio_subtype) {
1733af08d82Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_INFO)
1743af08d82Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_ACK)
1753af08d82Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_NACK)
1763af08d82Slm66018 	default: sstr = "unknown"; break;
1773af08d82Slm66018 	}
1783af08d82Slm66018 
1793af08d82Slm66018 #undef CASE_SUBTYPE
1803af08d82Slm66018 
1813af08d82Slm66018 #define	CASE_ENV(_s)	case _s: estr = #_s; break;
1823af08d82Slm66018 
1833af08d82Slm66018 	switch (msg->tag.vio_subtype_env) {
1843af08d82Slm66018 	CASE_ENV(VIO_VER_INFO)
1853af08d82Slm66018 	CASE_ENV(VIO_ATTR_INFO)
1863af08d82Slm66018 	CASE_ENV(VIO_DRING_REG)
1873af08d82Slm66018 	CASE_ENV(VIO_DRING_UNREG)
1883af08d82Slm66018 	CASE_ENV(VIO_RDX)
1893af08d82Slm66018 	CASE_ENV(VIO_PKT_DATA)
1903af08d82Slm66018 	CASE_ENV(VIO_DESC_DATA)
1913af08d82Slm66018 	CASE_ENV(VIO_DRING_DATA)
1923af08d82Slm66018 	default: estr = "unknown"; break;
1933af08d82Slm66018 	}
1943af08d82Slm66018 
1953af08d82Slm66018 #undef CASE_ENV
1963af08d82Slm66018 
1973af08d82Slm66018 	PR1("(%x/%x/%x) message : (%s/%s/%s)",
1983af08d82Slm66018 	    msg->tag.vio_msgtype, msg->tag.vio_subtype,
1993af08d82Slm66018 	    msg->tag.vio_subtype_env, tstr, sstr, estr);
2003af08d82Slm66018 }
2013af08d82Slm66018 
2021ae08745Sheppo #else	/* !DEBUG */
2033af08d82Slm66018 
2041ae08745Sheppo #define	PR0(...)
2051ae08745Sheppo #define	PR1(...)
2061ae08745Sheppo #define	PR2(...)
2071ae08745Sheppo 
2081ae08745Sheppo #define	VD_DUMP_DRING_ELEM(elem)
2091ae08745Sheppo 
2103af08d82Slm66018 #define	vd_decode_state(_s)	(NULL)
2113af08d82Slm66018 #define	vd_decode_tag(_s)	(NULL)
2123af08d82Slm66018 
2131ae08745Sheppo #endif	/* DEBUG */
2141ae08745Sheppo 
2151ae08745Sheppo 
216d10e4ef2Snarayan /*
217d10e4ef2Snarayan  * Soft state structure for a vds instance
218d10e4ef2Snarayan  */
2191ae08745Sheppo typedef struct vds {
2201ae08745Sheppo 	uint_t		initialized;	/* driver inst initialization flags */
2211ae08745Sheppo 	dev_info_t	*dip;		/* driver inst devinfo pointer */
2221ae08745Sheppo 	ldi_ident_t	ldi_ident;	/* driver's identifier for LDI */
2231ae08745Sheppo 	mod_hash_t	*vd_table;	/* table of virtual disks served */
2241ae08745Sheppo 	mdeg_handle_t	mdeg;		/* handle for MDEG operations  */
2251ae08745Sheppo } vds_t;
2261ae08745Sheppo 
227d10e4ef2Snarayan /*
228d10e4ef2Snarayan  * Types of descriptor-processing tasks
229d10e4ef2Snarayan  */
230d10e4ef2Snarayan typedef enum vd_task_type {
231d10e4ef2Snarayan 	VD_NONFINAL_RANGE_TASK,	/* task for intermediate descriptor in range */
232d10e4ef2Snarayan 	VD_FINAL_RANGE_TASK,	/* task for last in a range of descriptors */
233d10e4ef2Snarayan } vd_task_type_t;
234d10e4ef2Snarayan 
235d10e4ef2Snarayan /*
236d10e4ef2Snarayan  * Structure describing the task for processing a descriptor
237d10e4ef2Snarayan  */
238d10e4ef2Snarayan typedef struct vd_task {
239d10e4ef2Snarayan 	struct vd		*vd;		/* vd instance task is for */
240d10e4ef2Snarayan 	vd_task_type_t		type;		/* type of descriptor task */
241d10e4ef2Snarayan 	int			index;		/* dring elem index for task */
242d10e4ef2Snarayan 	vio_msg_t		*msg;		/* VIO message task is for */
243d10e4ef2Snarayan 	size_t			msglen;		/* length of message content */
244d10e4ef2Snarayan 	vd_dring_payload_t	*request;	/* request task will perform */
245d10e4ef2Snarayan 	struct buf		buf;		/* buf(9s) for I/O request */
2464bac2208Snarayan 	ldc_mem_handle_t	mhdl;		/* task memory handle */
247d10e4ef2Snarayan } vd_task_t;
248d10e4ef2Snarayan 
249d10e4ef2Snarayan /*
250d10e4ef2Snarayan  * Soft state structure for a virtual disk instance
251d10e4ef2Snarayan  */
2521ae08745Sheppo typedef struct vd {
2531ae08745Sheppo 	uint_t			initialized;	/* vdisk initialization flags */
2541ae08745Sheppo 	vds_t			*vds;		/* server for this vdisk */
255d10e4ef2Snarayan 	ddi_taskq_t		*startq;	/* queue for I/O start tasks */
256d10e4ef2Snarayan 	ddi_taskq_t		*completionq;	/* queue for completion tasks */
2571ae08745Sheppo 	ldi_handle_t		ldi_handle[V_NUMPAR];	/* LDI slice handles */
2581ae08745Sheppo 	dev_t			dev[V_NUMPAR];	/* dev numbers for slices */
259e1ebb9ecSlm66018 	uint_t			nslices;	/* number of slices */
2601ae08745Sheppo 	size_t			vdisk_size;	/* number of blocks in vdisk */
2611ae08745Sheppo 	vd_disk_type_t		vdisk_type;	/* slice or entire disk */
2624bac2208Snarayan 	vd_disk_label_t		vdisk_label;	/* EFI or VTOC label */
263e1ebb9ecSlm66018 	ushort_t		max_xfer_sz;	/* max xfer size in DEV_BSIZE */
2641ae08745Sheppo 	boolean_t		pseudo;		/* underlying pseudo dev */
2654bac2208Snarayan 	struct dk_efi		dk_efi;		/* synthetic for slice type */
2661ae08745Sheppo 	struct dk_geom		dk_geom;	/* synthetic for slice type */
2671ae08745Sheppo 	struct vtoc		vtoc;		/* synthetic for slice type */
2681ae08745Sheppo 	ldc_status_t		ldc_state;	/* LDC connection state */
2691ae08745Sheppo 	ldc_handle_t		ldc_handle;	/* handle for LDC comm */
2701ae08745Sheppo 	size_t			max_msglen;	/* largest LDC message len */
2711ae08745Sheppo 	vd_state_t		state;		/* client handshake state */
2721ae08745Sheppo 	uint8_t			xfer_mode;	/* transfer mode with client */
2731ae08745Sheppo 	uint32_t		sid;		/* client's session ID */
2741ae08745Sheppo 	uint64_t		seq_num;	/* message sequence number */
2751ae08745Sheppo 	uint64_t		dring_ident;	/* identifier of dring */
2761ae08745Sheppo 	ldc_dring_handle_t	dring_handle;	/* handle for dring ops */
2771ae08745Sheppo 	uint32_t		descriptor_size;	/* num bytes in desc */
2781ae08745Sheppo 	uint32_t		dring_len;	/* number of dring elements */
2791ae08745Sheppo 	caddr_t			dring;		/* address of dring */
2803af08d82Slm66018 	caddr_t			vio_msgp;	/* vio msg staging buffer */
281d10e4ef2Snarayan 	vd_task_t		inband_task;	/* task for inband descriptor */
282d10e4ef2Snarayan 	vd_task_t		*dring_task;	/* tasks dring elements */
283d10e4ef2Snarayan 
284d10e4ef2Snarayan 	kmutex_t		lock;		/* protects variables below */
285d10e4ef2Snarayan 	boolean_t		enabled;	/* is vdisk enabled? */
286d10e4ef2Snarayan 	boolean_t		reset_state;	/* reset connection state? */
287d10e4ef2Snarayan 	boolean_t		reset_ldc;	/* reset LDC channel? */
2881ae08745Sheppo } vd_t;
2891ae08745Sheppo 
2901ae08745Sheppo typedef struct vds_operation {
2913af08d82Slm66018 	char	*namep;
2921ae08745Sheppo 	uint8_t	operation;
293d10e4ef2Snarayan 	int	(*start)(vd_task_t *task);
294d10e4ef2Snarayan 	void	(*complete)(void *arg);
2951ae08745Sheppo } vds_operation_t;
2961ae08745Sheppo 
2970a55fbb7Slm66018 typedef struct vd_ioctl {
2980a55fbb7Slm66018 	uint8_t		operation;		/* vdisk operation */
2990a55fbb7Slm66018 	const char	*operation_name;	/* vdisk operation name */
3000a55fbb7Slm66018 	size_t		nbytes;			/* size of operation buffer */
3010a55fbb7Slm66018 	int		cmd;			/* corresponding ioctl cmd */
3020a55fbb7Slm66018 	const char	*cmd_name;		/* ioctl cmd name */
3030a55fbb7Slm66018 	void		*arg;			/* ioctl cmd argument */
3040a55fbb7Slm66018 	/* convert input vd_buf to output ioctl_arg */
3050a55fbb7Slm66018 	void		(*copyin)(void *vd_buf, void *ioctl_arg);
3060a55fbb7Slm66018 	/* convert input ioctl_arg to output vd_buf */
3070a55fbb7Slm66018 	void		(*copyout)(void *ioctl_arg, void *vd_buf);
3080a55fbb7Slm66018 } vd_ioctl_t;
3090a55fbb7Slm66018 
3100a55fbb7Slm66018 /* Define trivial copyin/copyout conversion function flag */
3110a55fbb7Slm66018 #define	VD_IDENTITY	((void (*)(void *, void *))-1)
3121ae08745Sheppo 
3131ae08745Sheppo 
3141ae08745Sheppo static int	vds_ldc_retries = VDS_LDC_RETRIES;
3153af08d82Slm66018 static int	vds_ldc_delay = VDS_LDC_DELAY;
3161ae08745Sheppo static void	*vds_state;
3171ae08745Sheppo static uint64_t	vds_operations;	/* see vds_operation[] definition below */
3181ae08745Sheppo 
3191ae08745Sheppo static int	vd_open_flags = VD_OPEN_FLAGS;
3201ae08745Sheppo 
3210a55fbb7Slm66018 /*
3220a55fbb7Slm66018  * Supported protocol version pairs, from highest (newest) to lowest (oldest)
3230a55fbb7Slm66018  *
3240a55fbb7Slm66018  * Each supported major version should appear only once, paired with (and only
3250a55fbb7Slm66018  * with) its highest supported minor version number (as the protocol requires
3260a55fbb7Slm66018  * supporting all lower minor version numbers as well)
3270a55fbb7Slm66018  */
3280a55fbb7Slm66018 static const vio_ver_t	vds_version[] = {{1, 0}};
3290a55fbb7Slm66018 static const size_t	vds_num_versions =
3300a55fbb7Slm66018     sizeof (vds_version)/sizeof (vds_version[0]);
3310a55fbb7Slm66018 
3323af08d82Slm66018 static void vd_free_dring_task(vd_t *vdp);
3331ae08745Sheppo 
3341ae08745Sheppo static int
335d10e4ef2Snarayan vd_start_bio(vd_task_t *task)
3361ae08745Sheppo {
3374bac2208Snarayan 	int			rv, status = 0;
338d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
339d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
340d10e4ef2Snarayan 	struct buf		*buf		= &task->buf;
3414bac2208Snarayan 	uint8_t			mtype;
3421ae08745Sheppo 
343d10e4ef2Snarayan 
344d10e4ef2Snarayan 	ASSERT(vd != NULL);
345d10e4ef2Snarayan 	ASSERT(request != NULL);
346d10e4ef2Snarayan 	ASSERT(request->slice < vd->nslices);
347d10e4ef2Snarayan 	ASSERT((request->operation == VD_OP_BREAD) ||
348d10e4ef2Snarayan 	    (request->operation == VD_OP_BWRITE));
349d10e4ef2Snarayan 
3501ae08745Sheppo 	if (request->nbytes == 0)
3511ae08745Sheppo 		return (EINVAL);	/* no service for trivial requests */
3521ae08745Sheppo 
353d10e4ef2Snarayan 	PR1("%s %lu bytes at block %lu",
354d10e4ef2Snarayan 	    (request->operation == VD_OP_BREAD) ? "Read" : "Write",
355d10e4ef2Snarayan 	    request->nbytes, request->addr);
3561ae08745Sheppo 
357d10e4ef2Snarayan 	bioinit(buf);
358d10e4ef2Snarayan 	buf->b_flags		= B_BUSY;
359d10e4ef2Snarayan 	buf->b_bcount		= request->nbytes;
360d10e4ef2Snarayan 	buf->b_lblkno		= request->addr;
361d10e4ef2Snarayan 	buf->b_edev		= vd->dev[request->slice];
362d10e4ef2Snarayan 
3634bac2208Snarayan 	mtype = (&vd->inband_task == task) ? LDC_SHADOW_MAP : LDC_DIRECT_MAP;
3644bac2208Snarayan 
3654bac2208Snarayan 	/* Map memory exported by client */
3664bac2208Snarayan 	status = ldc_mem_map(task->mhdl, request->cookie, request->ncookies,
3674bac2208Snarayan 	    mtype, (request->operation == VD_OP_BREAD) ? LDC_MEM_W : LDC_MEM_R,
3684bac2208Snarayan 	    &(buf->b_un.b_addr), NULL);
3694bac2208Snarayan 	if (status != 0) {
3703af08d82Slm66018 		PR0("ldc_mem_map() returned err %d ", status);
3714bac2208Snarayan 		biofini(buf);
3724bac2208Snarayan 		return (status);
373d10e4ef2Snarayan 	}
374d10e4ef2Snarayan 
3754bac2208Snarayan 	status = ldc_mem_acquire(task->mhdl, 0, buf->b_bcount);
3764bac2208Snarayan 	if (status != 0) {
3774bac2208Snarayan 		(void) ldc_mem_unmap(task->mhdl);
3783af08d82Slm66018 		PR0("ldc_mem_acquire() returned err %d ", status);
3794bac2208Snarayan 		biofini(buf);
3804bac2208Snarayan 		return (status);
3814bac2208Snarayan 	}
3824bac2208Snarayan 
3834bac2208Snarayan 	buf->b_flags |= (request->operation == VD_OP_BREAD) ? B_READ : B_WRITE;
3844bac2208Snarayan 
385d10e4ef2Snarayan 	/* Start the block I/O */
3864bac2208Snarayan 	if ((status = ldi_strategy(vd->ldi_handle[request->slice], buf)) == 0)
387d10e4ef2Snarayan 		return (EINPROGRESS);	/* will complete on completionq */
388d10e4ef2Snarayan 
389d10e4ef2Snarayan 	/* Clean up after error */
3904bac2208Snarayan 	rv = ldc_mem_release(task->mhdl, 0, buf->b_bcount);
3914bac2208Snarayan 	if (rv) {
3923af08d82Slm66018 		PR0("ldc_mem_release() returned err %d ", rv);
3934bac2208Snarayan 	}
3944bac2208Snarayan 	rv = ldc_mem_unmap(task->mhdl);
3954bac2208Snarayan 	if (rv) {
3963af08d82Slm66018 		PR0("ldc_mem_unmap() returned err %d ", status);
3974bac2208Snarayan 	}
3984bac2208Snarayan 
399d10e4ef2Snarayan 	biofini(buf);
400d10e4ef2Snarayan 	return (status);
401d10e4ef2Snarayan }
402d10e4ef2Snarayan 
403d10e4ef2Snarayan static int
404d10e4ef2Snarayan send_msg(ldc_handle_t ldc_handle, void *msg, size_t msglen)
405d10e4ef2Snarayan {
4063af08d82Slm66018 	int	status;
407d10e4ef2Snarayan 	size_t	nbytes;
408d10e4ef2Snarayan 
4093af08d82Slm66018 	do {
410d10e4ef2Snarayan 		nbytes = msglen;
411d10e4ef2Snarayan 		status = ldc_write(ldc_handle, msg, &nbytes);
4123af08d82Slm66018 		if (status != EWOULDBLOCK)
4133af08d82Slm66018 			break;
4143af08d82Slm66018 		drv_usecwait(vds_ldc_delay);
4153af08d82Slm66018 	} while (status == EWOULDBLOCK);
416d10e4ef2Snarayan 
417d10e4ef2Snarayan 	if (status != 0) {
4183af08d82Slm66018 		if (status != ECONNRESET)
4193af08d82Slm66018 			PR0("ldc_write() returned errno %d", status);
420d10e4ef2Snarayan 		return (status);
421d10e4ef2Snarayan 	} else if (nbytes != msglen) {
4223af08d82Slm66018 		PR0("ldc_write() performed only partial write");
423d10e4ef2Snarayan 		return (EIO);
424d10e4ef2Snarayan 	}
425d10e4ef2Snarayan 
426d10e4ef2Snarayan 	PR1("SENT %lu bytes", msglen);
427d10e4ef2Snarayan 	return (0);
428d10e4ef2Snarayan }
429d10e4ef2Snarayan 
430d10e4ef2Snarayan static void
431d10e4ef2Snarayan vd_need_reset(vd_t *vd, boolean_t reset_ldc)
432d10e4ef2Snarayan {
433d10e4ef2Snarayan 	mutex_enter(&vd->lock);
434d10e4ef2Snarayan 	vd->reset_state	= B_TRUE;
435d10e4ef2Snarayan 	vd->reset_ldc	= reset_ldc;
436d10e4ef2Snarayan 	mutex_exit(&vd->lock);
437d10e4ef2Snarayan }
438d10e4ef2Snarayan 
439d10e4ef2Snarayan /*
440d10e4ef2Snarayan  * Reset the state of the connection with a client, if needed; reset the LDC
441d10e4ef2Snarayan  * transport as well, if needed.  This function should only be called from the
4423af08d82Slm66018  * "vd_recv_msg", as it waits for tasks - otherwise a deadlock can occur.
443d10e4ef2Snarayan  */
444d10e4ef2Snarayan static void
445d10e4ef2Snarayan vd_reset_if_needed(vd_t *vd)
446d10e4ef2Snarayan {
447d10e4ef2Snarayan 	int	status = 0;
448d10e4ef2Snarayan 
449d10e4ef2Snarayan 	mutex_enter(&vd->lock);
450d10e4ef2Snarayan 	if (!vd->reset_state) {
451d10e4ef2Snarayan 		ASSERT(!vd->reset_ldc);
452d10e4ef2Snarayan 		mutex_exit(&vd->lock);
453d10e4ef2Snarayan 		return;
454d10e4ef2Snarayan 	}
455d10e4ef2Snarayan 	mutex_exit(&vd->lock);
456d10e4ef2Snarayan 
457d10e4ef2Snarayan 	PR0("Resetting connection state with %s", VD_CLIENT(vd));
458d10e4ef2Snarayan 
459d10e4ef2Snarayan 	/*
460d10e4ef2Snarayan 	 * Let any asynchronous I/O complete before possibly pulling the rug
461d10e4ef2Snarayan 	 * out from under it; defer checking vd->reset_ldc, as one of the
462d10e4ef2Snarayan 	 * asynchronous tasks might set it
463d10e4ef2Snarayan 	 */
464d10e4ef2Snarayan 	ddi_taskq_wait(vd->completionq);
465d10e4ef2Snarayan 
466d10e4ef2Snarayan 	if ((vd->initialized & VD_DRING) &&
467d10e4ef2Snarayan 	    ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0))
4683af08d82Slm66018 		PR0("ldc_mem_dring_unmap() returned errno %d", status);
469d10e4ef2Snarayan 
4703af08d82Slm66018 	vd_free_dring_task(vd);
4713af08d82Slm66018 
4723af08d82Slm66018 	/* Free the staging buffer for msgs */
4733af08d82Slm66018 	if (vd->vio_msgp != NULL) {
4743af08d82Slm66018 		kmem_free(vd->vio_msgp, vd->max_msglen);
4753af08d82Slm66018 		vd->vio_msgp = NULL;
476d10e4ef2Snarayan 	}
477d10e4ef2Snarayan 
4783af08d82Slm66018 	/* Free the inband message buffer */
4793af08d82Slm66018 	if (vd->inband_task.msg != NULL) {
4803af08d82Slm66018 		kmem_free(vd->inband_task.msg, vd->max_msglen);
4813af08d82Slm66018 		vd->inband_task.msg = NULL;
4823af08d82Slm66018 	}
483d10e4ef2Snarayan 
484d10e4ef2Snarayan 	mutex_enter(&vd->lock);
4853af08d82Slm66018 
4863af08d82Slm66018 	if (vd->reset_ldc)
4873af08d82Slm66018 		PR0("taking down LDC channel");
488e1ebb9ecSlm66018 	if (vd->reset_ldc && ((status = ldc_down(vd->ldc_handle)) != 0))
4893af08d82Slm66018 		PR0("ldc_down() returned errno %d", status);
490d10e4ef2Snarayan 
491d10e4ef2Snarayan 	vd->initialized	&= ~(VD_SID | VD_SEQ_NUM | VD_DRING);
492d10e4ef2Snarayan 	vd->state	= VD_STATE_INIT;
493d10e4ef2Snarayan 	vd->max_msglen	= sizeof (vio_msg_t);	/* baseline vio message size */
494d10e4ef2Snarayan 
4953af08d82Slm66018 	/* Allocate the staging buffer */
4963af08d82Slm66018 	vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
4973af08d82Slm66018 
4983af08d82Slm66018 	PR0("calling ldc_up\n");
4993af08d82Slm66018 	(void) ldc_up(vd->ldc_handle);
5003af08d82Slm66018 
501d10e4ef2Snarayan 	vd->reset_state	= B_FALSE;
502d10e4ef2Snarayan 	vd->reset_ldc	= B_FALSE;
5033af08d82Slm66018 
504d10e4ef2Snarayan 	mutex_exit(&vd->lock);
505d10e4ef2Snarayan }
506d10e4ef2Snarayan 
5073af08d82Slm66018 static void vd_recv_msg(void *arg);
5083af08d82Slm66018 
5093af08d82Slm66018 static void
5103af08d82Slm66018 vd_mark_in_reset(vd_t *vd)
5113af08d82Slm66018 {
5123af08d82Slm66018 	int status;
5133af08d82Slm66018 
5143af08d82Slm66018 	PR0("vd_mark_in_reset: marking vd in reset\n");
5153af08d82Slm66018 
5163af08d82Slm66018 	vd_need_reset(vd, B_FALSE);
5173af08d82Slm66018 	status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, DDI_SLEEP);
5183af08d82Slm66018 	if (status == DDI_FAILURE) {
5193af08d82Slm66018 		PR0("cannot schedule task to recv msg\n");
5203af08d82Slm66018 		vd_need_reset(vd, B_TRUE);
5213af08d82Slm66018 		return;
5223af08d82Slm66018 	}
5233af08d82Slm66018 }
5243af08d82Slm66018 
525d10e4ef2Snarayan static int
526d10e4ef2Snarayan vd_mark_elem_done(vd_t *vd, int idx, int elem_status)
527d10e4ef2Snarayan {
528d10e4ef2Snarayan 	boolean_t		accepted;
529d10e4ef2Snarayan 	int			status;
530d10e4ef2Snarayan 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
531d10e4ef2Snarayan 
5323af08d82Slm66018 	if (vd->reset_state)
5333af08d82Slm66018 		return (0);
534d10e4ef2Snarayan 
535d10e4ef2Snarayan 	/* Acquire the element */
5363af08d82Slm66018 	if (!vd->reset_state &&
5373af08d82Slm66018 	    (status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) {
5383af08d82Slm66018 		if (status == ECONNRESET) {
5393af08d82Slm66018 			vd_mark_in_reset(vd);
5403af08d82Slm66018 			return (0);
5413af08d82Slm66018 		} else {
5423af08d82Slm66018 			PR0("ldc_mem_dring_acquire() returned errno %d",
5433af08d82Slm66018 			    status);
544d10e4ef2Snarayan 			return (status);
545d10e4ef2Snarayan 		}
5463af08d82Slm66018 	}
547d10e4ef2Snarayan 
548d10e4ef2Snarayan 	/* Set the element's status and mark it done */
549d10e4ef2Snarayan 	accepted = (elem->hdr.dstate == VIO_DESC_ACCEPTED);
550d10e4ef2Snarayan 	if (accepted) {
551d10e4ef2Snarayan 		elem->payload.status	= elem_status;
552d10e4ef2Snarayan 		elem->hdr.dstate	= VIO_DESC_DONE;
553d10e4ef2Snarayan 	} else {
554d10e4ef2Snarayan 		/* Perhaps client timed out waiting for I/O... */
5553af08d82Slm66018 		PR0("element %u no longer \"accepted\"", idx);
556d10e4ef2Snarayan 		VD_DUMP_DRING_ELEM(elem);
557d10e4ef2Snarayan 	}
558d10e4ef2Snarayan 	/* Release the element */
5593af08d82Slm66018 	if (!vd->reset_state &&
5603af08d82Slm66018 	    (status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) {
5613af08d82Slm66018 		if (status == ECONNRESET) {
5623af08d82Slm66018 			vd_mark_in_reset(vd);
5633af08d82Slm66018 			return (0);
5643af08d82Slm66018 		} else {
5653af08d82Slm66018 			PR0("ldc_mem_dring_release() returned errno %d",
5663af08d82Slm66018 			    status);
567d10e4ef2Snarayan 			return (status);
568d10e4ef2Snarayan 		}
5693af08d82Slm66018 	}
570d10e4ef2Snarayan 
571d10e4ef2Snarayan 	return (accepted ? 0 : EINVAL);
572d10e4ef2Snarayan }
573d10e4ef2Snarayan 
574d10e4ef2Snarayan static void
575d10e4ef2Snarayan vd_complete_bio(void *arg)
576d10e4ef2Snarayan {
577d10e4ef2Snarayan 	int			status		= 0;
578d10e4ef2Snarayan 	vd_task_t		*task		= (vd_task_t *)arg;
579d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
580d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
581d10e4ef2Snarayan 	struct buf		*buf		= &task->buf;
582d10e4ef2Snarayan 
583d10e4ef2Snarayan 
584d10e4ef2Snarayan 	ASSERT(vd != NULL);
585d10e4ef2Snarayan 	ASSERT(request != NULL);
586d10e4ef2Snarayan 	ASSERT(task->msg != NULL);
587d10e4ef2Snarayan 	ASSERT(task->msglen >= sizeof (*task->msg));
588d10e4ef2Snarayan 
589d10e4ef2Snarayan 	/* Wait for the I/O to complete */
590d10e4ef2Snarayan 	request->status = biowait(buf);
591d10e4ef2Snarayan 
5924bac2208Snarayan 	/* Release the buffer */
5933af08d82Slm66018 	if (!vd->reset_state)
5944bac2208Snarayan 		status = ldc_mem_release(task->mhdl, 0, buf->b_bcount);
5954bac2208Snarayan 	if (status) {
5963af08d82Slm66018 		PR0("ldc_mem_release() returned errno %d copying to "
5973af08d82Slm66018 		    "client", status);
5983af08d82Slm66018 		if (status == ECONNRESET) {
5993af08d82Slm66018 			vd_mark_in_reset(vd);
6003af08d82Slm66018 		}
6011ae08745Sheppo 	}
6021ae08745Sheppo 
6033af08d82Slm66018 	/* Unmap the memory, even if in reset */
6044bac2208Snarayan 	status = ldc_mem_unmap(task->mhdl);
6054bac2208Snarayan 	if (status) {
6063af08d82Slm66018 		PR0("ldc_mem_unmap() returned errno %d copying to client",
6074bac2208Snarayan 		    status);
6083af08d82Slm66018 		if (status == ECONNRESET) {
6093af08d82Slm66018 			vd_mark_in_reset(vd);
6103af08d82Slm66018 		}
6114bac2208Snarayan 	}
6124bac2208Snarayan 
613d10e4ef2Snarayan 	biofini(buf);
6141ae08745Sheppo 
615d10e4ef2Snarayan 	/* Update the dring element for a dring client */
6163af08d82Slm66018 	if (!vd->reset_state && (status == 0) &&
6173af08d82Slm66018 	    (vd->xfer_mode == VIO_DRING_MODE)) {
618d10e4ef2Snarayan 		status = vd_mark_elem_done(vd, task->index, request->status);
6193af08d82Slm66018 		if (status == ECONNRESET)
6203af08d82Slm66018 			vd_mark_in_reset(vd);
6213af08d82Slm66018 	}
6221ae08745Sheppo 
623d10e4ef2Snarayan 	/*
624d10e4ef2Snarayan 	 * If a transport error occurred, arrange to "nack" the message when
625d10e4ef2Snarayan 	 * the final task in the descriptor element range completes
626d10e4ef2Snarayan 	 */
627d10e4ef2Snarayan 	if (status != 0)
628d10e4ef2Snarayan 		task->msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
6291ae08745Sheppo 
630d10e4ef2Snarayan 	/*
631d10e4ef2Snarayan 	 * Only the final task for a range of elements will respond to and
632d10e4ef2Snarayan 	 * free the message
633d10e4ef2Snarayan 	 */
6343af08d82Slm66018 	if (task->type == VD_NONFINAL_RANGE_TASK) {
635d10e4ef2Snarayan 		return;
6363af08d82Slm66018 	}
6371ae08745Sheppo 
638d10e4ef2Snarayan 	/*
639d10e4ef2Snarayan 	 * Send the "ack" or "nack" back to the client; if sending the message
640d10e4ef2Snarayan 	 * via LDC fails, arrange to reset both the connection state and LDC
641d10e4ef2Snarayan 	 * itself
642d10e4ef2Snarayan 	 */
643d10e4ef2Snarayan 	PR1("Sending %s",
644d10e4ef2Snarayan 	    (task->msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK");
6453af08d82Slm66018 	if (!vd->reset_state) {
6463af08d82Slm66018 		status = send_msg(vd->ldc_handle, task->msg, task->msglen);
6473af08d82Slm66018 		switch (status) {
6483af08d82Slm66018 		case 0:
6493af08d82Slm66018 			break;
6503af08d82Slm66018 		case ECONNRESET:
6513af08d82Slm66018 			vd_mark_in_reset(vd);
6523af08d82Slm66018 			break;
6533af08d82Slm66018 		default:
6543af08d82Slm66018 			PR0("initiating full reset");
655d10e4ef2Snarayan 			vd_need_reset(vd, B_TRUE);
6563af08d82Slm66018 			break;
6573af08d82Slm66018 		}
6583af08d82Slm66018 	}
6591ae08745Sheppo }
6601ae08745Sheppo 
6610a55fbb7Slm66018 static void
6620a55fbb7Slm66018 vd_geom2dk_geom(void *vd_buf, void *ioctl_arg)
6630a55fbb7Slm66018 {
6640a55fbb7Slm66018 	VD_GEOM2DK_GEOM((vd_geom_t *)vd_buf, (struct dk_geom *)ioctl_arg);
6650a55fbb7Slm66018 }
6660a55fbb7Slm66018 
6670a55fbb7Slm66018 static void
6680a55fbb7Slm66018 vd_vtoc2vtoc(void *vd_buf, void *ioctl_arg)
6690a55fbb7Slm66018 {
6700a55fbb7Slm66018 	VD_VTOC2VTOC((vd_vtoc_t *)vd_buf, (struct vtoc *)ioctl_arg);
6710a55fbb7Slm66018 }
6720a55fbb7Slm66018 
6730a55fbb7Slm66018 static void
6740a55fbb7Slm66018 dk_geom2vd_geom(void *ioctl_arg, void *vd_buf)
6750a55fbb7Slm66018 {
6760a55fbb7Slm66018 	DK_GEOM2VD_GEOM((struct dk_geom *)ioctl_arg, (vd_geom_t *)vd_buf);
6770a55fbb7Slm66018 }
6780a55fbb7Slm66018 
6790a55fbb7Slm66018 static void
6800a55fbb7Slm66018 vtoc2vd_vtoc(void *ioctl_arg, void *vd_buf)
6810a55fbb7Slm66018 {
6820a55fbb7Slm66018 	VTOC2VD_VTOC((struct vtoc *)ioctl_arg, (vd_vtoc_t *)vd_buf);
6830a55fbb7Slm66018 }
6840a55fbb7Slm66018 
6854bac2208Snarayan static void
6864bac2208Snarayan vd_get_efi_in(void *vd_buf, void *ioctl_arg)
6874bac2208Snarayan {
6884bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
6894bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
6904bac2208Snarayan 
6914bac2208Snarayan 	dk_efi->dki_lba = vd_efi->lba;
6924bac2208Snarayan 	dk_efi->dki_length = vd_efi->length;
6934bac2208Snarayan 	dk_efi->dki_data = kmem_zalloc(vd_efi->length, KM_SLEEP);
6944bac2208Snarayan }
6954bac2208Snarayan 
6964bac2208Snarayan static void
6974bac2208Snarayan vd_get_efi_out(void *ioctl_arg, void *vd_buf)
6984bac2208Snarayan {
6994bac2208Snarayan 	int len;
7004bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
7014bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
7024bac2208Snarayan 
7034bac2208Snarayan 	len = vd_efi->length;
7044bac2208Snarayan 	DK_EFI2VD_EFI(dk_efi, vd_efi);
7054bac2208Snarayan 	kmem_free(dk_efi->dki_data, len);
7064bac2208Snarayan }
7074bac2208Snarayan 
7084bac2208Snarayan static void
7094bac2208Snarayan vd_set_efi_in(void *vd_buf, void *ioctl_arg)
7104bac2208Snarayan {
7114bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
7124bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
7134bac2208Snarayan 
7144bac2208Snarayan 	dk_efi->dki_data = kmem_alloc(vd_efi->length, KM_SLEEP);
7154bac2208Snarayan 	VD_EFI2DK_EFI(vd_efi, dk_efi);
7164bac2208Snarayan }
7174bac2208Snarayan 
7184bac2208Snarayan static void
7194bac2208Snarayan vd_set_efi_out(void *ioctl_arg, void *vd_buf)
7204bac2208Snarayan {
7214bac2208Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
7224bac2208Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
7234bac2208Snarayan 
7244bac2208Snarayan 	kmem_free(dk_efi->dki_data, vd_efi->length);
7254bac2208Snarayan }
7264bac2208Snarayan 
7274bac2208Snarayan static int
7284bac2208Snarayan vd_read_vtoc(ldi_handle_t handle, struct vtoc *vtoc, vd_disk_label_t *label)
7294bac2208Snarayan {
7304bac2208Snarayan 	int status, rval;
7314bac2208Snarayan 	struct dk_gpt *efi;
7324bac2208Snarayan 	size_t efi_len;
7334bac2208Snarayan 
7344bac2208Snarayan 	*label = VD_DISK_LABEL_UNK;
7354bac2208Snarayan 
7364bac2208Snarayan 	status = ldi_ioctl(handle, DKIOCGVTOC, (intptr_t)vtoc,
7374bac2208Snarayan 	    (vd_open_flags | FKIOCTL), kcred, &rval);
7384bac2208Snarayan 
7394bac2208Snarayan 	if (status == 0) {
7404bac2208Snarayan 		*label = VD_DISK_LABEL_VTOC;
7414bac2208Snarayan 		return (0);
7424bac2208Snarayan 	} else if (status != ENOTSUP) {
7433af08d82Slm66018 		PR0("ldi_ioctl(DKIOCGVTOC) returned error %d", status);
7444bac2208Snarayan 		return (status);
7454bac2208Snarayan 	}
7464bac2208Snarayan 
7474bac2208Snarayan 	status = vds_efi_alloc_and_read(handle, &efi, &efi_len);
7484bac2208Snarayan 
7494bac2208Snarayan 	if (status) {
7503af08d82Slm66018 		PR0("vds_efi_alloc_and_read returned error %d", status);
7514bac2208Snarayan 		return (status);
7524bac2208Snarayan 	}
7534bac2208Snarayan 
7544bac2208Snarayan 	*label = VD_DISK_LABEL_EFI;
7554bac2208Snarayan 	vd_efi_to_vtoc(efi, vtoc);
7564bac2208Snarayan 	vd_efi_free(efi, efi_len);
7574bac2208Snarayan 
7584bac2208Snarayan 	return (0);
7594bac2208Snarayan }
7604bac2208Snarayan 
7611ae08745Sheppo static int
7620a55fbb7Slm66018 vd_do_slice_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
7631ae08745Sheppo {
7644bac2208Snarayan 	dk_efi_t *dk_ioc;
7654bac2208Snarayan 
7664bac2208Snarayan 	switch (vd->vdisk_label) {
7674bac2208Snarayan 
7684bac2208Snarayan 	case VD_DISK_LABEL_VTOC:
7694bac2208Snarayan 
7701ae08745Sheppo 		switch (cmd) {
7711ae08745Sheppo 		case DKIOCGGEOM:
7720a55fbb7Slm66018 			ASSERT(ioctl_arg != NULL);
7730a55fbb7Slm66018 			bcopy(&vd->dk_geom, ioctl_arg, sizeof (vd->dk_geom));
7741ae08745Sheppo 			return (0);
7751ae08745Sheppo 		case DKIOCGVTOC:
7760a55fbb7Slm66018 			ASSERT(ioctl_arg != NULL);
7770a55fbb7Slm66018 			bcopy(&vd->vtoc, ioctl_arg, sizeof (vd->vtoc));
7781ae08745Sheppo 			return (0);
7791ae08745Sheppo 		default:
7801ae08745Sheppo 			return (ENOTSUP);
7811ae08745Sheppo 		}
7824bac2208Snarayan 
7834bac2208Snarayan 	case VD_DISK_LABEL_EFI:
7844bac2208Snarayan 
7854bac2208Snarayan 		switch (cmd) {
7864bac2208Snarayan 		case DKIOCGETEFI:
7874bac2208Snarayan 			ASSERT(ioctl_arg != NULL);
7884bac2208Snarayan 			dk_ioc = (dk_efi_t *)ioctl_arg;
7894bac2208Snarayan 			if (dk_ioc->dki_length < vd->dk_efi.dki_length)
7904bac2208Snarayan 				return (EINVAL);
7914bac2208Snarayan 			bcopy(vd->dk_efi.dki_data, dk_ioc->dki_data,
7924bac2208Snarayan 			    vd->dk_efi.dki_length);
7934bac2208Snarayan 			return (0);
7944bac2208Snarayan 		default:
7954bac2208Snarayan 			return (ENOTSUP);
7964bac2208Snarayan 		}
7974bac2208Snarayan 
7984bac2208Snarayan 	default:
7994bac2208Snarayan 		return (ENOTSUP);
8004bac2208Snarayan 	}
8011ae08745Sheppo }
8021ae08745Sheppo 
8031ae08745Sheppo static int
8040a55fbb7Slm66018 vd_do_ioctl(vd_t *vd, vd_dring_payload_t *request, void* buf, vd_ioctl_t *ioctl)
8051ae08745Sheppo {
8061ae08745Sheppo 	int	rval = 0, status;
8071ae08745Sheppo 	size_t	nbytes = request->nbytes;	/* modifiable copy */
8081ae08745Sheppo 
8091ae08745Sheppo 
8101ae08745Sheppo 	ASSERT(request->slice < vd->nslices);
8111ae08745Sheppo 	PR0("Performing %s", ioctl->operation_name);
8121ae08745Sheppo 
8130a55fbb7Slm66018 	/* Get data from client and convert, if necessary */
8140a55fbb7Slm66018 	if (ioctl->copyin != NULL)  {
8151ae08745Sheppo 		ASSERT(nbytes != 0 && buf != NULL);
8161ae08745Sheppo 		PR1("Getting \"arg\" data from client");
8171ae08745Sheppo 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
8181ae08745Sheppo 			    request->cookie, request->ncookies,
8191ae08745Sheppo 			    LDC_COPY_IN)) != 0) {
8203af08d82Slm66018 			PR0("ldc_mem_copy() returned errno %d "
8211ae08745Sheppo 			    "copying from client", status);
8221ae08745Sheppo 			return (status);
8231ae08745Sheppo 		}
8240a55fbb7Slm66018 
8250a55fbb7Slm66018 		/* Convert client's data, if necessary */
8260a55fbb7Slm66018 		if (ioctl->copyin == VD_IDENTITY)	/* use client buffer */
8270a55fbb7Slm66018 			ioctl->arg = buf;
8280a55fbb7Slm66018 		else	/* convert client vdisk operation data to ioctl data */
8290a55fbb7Slm66018 			(ioctl->copyin)(buf, (void *)ioctl->arg);
8301ae08745Sheppo 	}
8311ae08745Sheppo 
8321ae08745Sheppo 	/*
8331ae08745Sheppo 	 * Handle single-slice block devices internally; otherwise, have the
8341ae08745Sheppo 	 * real driver perform the ioctl()
8351ae08745Sheppo 	 */
8361ae08745Sheppo 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE && !vd->pseudo) {
8370a55fbb7Slm66018 		if ((status = vd_do_slice_ioctl(vd, ioctl->cmd,
8380a55fbb7Slm66018 			    (void *)ioctl->arg)) != 0)
8391ae08745Sheppo 			return (status);
8401ae08745Sheppo 	} else if ((status = ldi_ioctl(vd->ldi_handle[request->slice],
841d10e4ef2Snarayan 		    ioctl->cmd, (intptr_t)ioctl->arg, (vd_open_flags | FKIOCTL),
842d10e4ef2Snarayan 		    kcred, &rval)) != 0) {
8431ae08745Sheppo 		PR0("ldi_ioctl(%s) = errno %d", ioctl->cmd_name, status);
8441ae08745Sheppo 		return (status);
8451ae08745Sheppo 	}
8461ae08745Sheppo #ifdef DEBUG
8471ae08745Sheppo 	if (rval != 0) {
8483af08d82Slm66018 		PR0("%s set rval = %d, which is not being returned to client",
8491ae08745Sheppo 		    ioctl->cmd_name, rval);
8501ae08745Sheppo 	}
8511ae08745Sheppo #endif /* DEBUG */
8521ae08745Sheppo 
8530a55fbb7Slm66018 	/* Convert data and send to client, if necessary */
8540a55fbb7Slm66018 	if (ioctl->copyout != NULL)  {
8551ae08745Sheppo 		ASSERT(nbytes != 0 && buf != NULL);
8561ae08745Sheppo 		PR1("Sending \"arg\" data to client");
8570a55fbb7Slm66018 
8580a55fbb7Slm66018 		/* Convert ioctl data to vdisk operation data, if necessary */
8590a55fbb7Slm66018 		if (ioctl->copyout != VD_IDENTITY)
8600a55fbb7Slm66018 			(ioctl->copyout)((void *)ioctl->arg, buf);
8610a55fbb7Slm66018 
8621ae08745Sheppo 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
8631ae08745Sheppo 			    request->cookie, request->ncookies,
8641ae08745Sheppo 			    LDC_COPY_OUT)) != 0) {
8653af08d82Slm66018 			PR0("ldc_mem_copy() returned errno %d "
8661ae08745Sheppo 			    "copying to client", status);
8671ae08745Sheppo 			return (status);
8681ae08745Sheppo 		}
8691ae08745Sheppo 	}
8701ae08745Sheppo 
8711ae08745Sheppo 	return (status);
8721ae08745Sheppo }
8731ae08745Sheppo 
8741ae08745Sheppo #define	RNDSIZE(expr) P2ROUNDUP(sizeof (expr), sizeof (uint64_t))
8751ae08745Sheppo static int
876d10e4ef2Snarayan vd_ioctl(vd_task_t *task)
8771ae08745Sheppo {
878*34683adeSsg70180 	int			i, status, rc;
8791ae08745Sheppo 	void			*buf = NULL;
8800a55fbb7Slm66018 	struct dk_geom		dk_geom = {0};
8810a55fbb7Slm66018 	struct vtoc		vtoc = {0};
8824bac2208Snarayan 	struct dk_efi		dk_efi = {0};
883d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
884d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
8850a55fbb7Slm66018 	vd_ioctl_t		ioctl[] = {
8860a55fbb7Slm66018 		/* Command (no-copy) operations */
8870a55fbb7Slm66018 		{VD_OP_FLUSH, STRINGIZE(VD_OP_FLUSH), 0,
8880a55fbb7Slm66018 		    DKIOCFLUSHWRITECACHE, STRINGIZE(DKIOCFLUSHWRITECACHE),
8890a55fbb7Slm66018 		    NULL, NULL, NULL},
8900a55fbb7Slm66018 
8910a55fbb7Slm66018 		/* "Get" (copy-out) operations */
8920a55fbb7Slm66018 		{VD_OP_GET_WCE, STRINGIZE(VD_OP_GET_WCE), RNDSIZE(int),
8930a55fbb7Slm66018 		    DKIOCGETWCE, STRINGIZE(DKIOCGETWCE),
8944bac2208Snarayan 		    NULL, VD_IDENTITY, VD_IDENTITY},
8950a55fbb7Slm66018 		{VD_OP_GET_DISKGEOM, STRINGIZE(VD_OP_GET_DISKGEOM),
8960a55fbb7Slm66018 		    RNDSIZE(vd_geom_t),
8970a55fbb7Slm66018 		    DKIOCGGEOM, STRINGIZE(DKIOCGGEOM),
8980a55fbb7Slm66018 		    &dk_geom, NULL, dk_geom2vd_geom},
8990a55fbb7Slm66018 		{VD_OP_GET_VTOC, STRINGIZE(VD_OP_GET_VTOC), RNDSIZE(vd_vtoc_t),
9000a55fbb7Slm66018 		    DKIOCGVTOC, STRINGIZE(DKIOCGVTOC),
9010a55fbb7Slm66018 		    &vtoc, NULL, vtoc2vd_vtoc},
9024bac2208Snarayan 		{VD_OP_GET_EFI, STRINGIZE(VD_OP_GET_EFI), RNDSIZE(vd_efi_t),
9034bac2208Snarayan 		    DKIOCGETEFI, STRINGIZE(DKIOCGETEFI),
9044bac2208Snarayan 		    &dk_efi, vd_get_efi_in, vd_get_efi_out},
9050a55fbb7Slm66018 
9060a55fbb7Slm66018 		/* "Set" (copy-in) operations */
9070a55fbb7Slm66018 		{VD_OP_SET_WCE, STRINGIZE(VD_OP_SET_WCE), RNDSIZE(int),
9080a55fbb7Slm66018 		    DKIOCSETWCE, STRINGIZE(DKIOCSETWCE),
9094bac2208Snarayan 		    NULL, VD_IDENTITY, VD_IDENTITY},
9100a55fbb7Slm66018 		{VD_OP_SET_DISKGEOM, STRINGIZE(VD_OP_SET_DISKGEOM),
9110a55fbb7Slm66018 		    RNDSIZE(vd_geom_t),
9120a55fbb7Slm66018 		    DKIOCSGEOM, STRINGIZE(DKIOCSGEOM),
9130a55fbb7Slm66018 		    &dk_geom, vd_geom2dk_geom, NULL},
9140a55fbb7Slm66018 		{VD_OP_SET_VTOC, STRINGIZE(VD_OP_SET_VTOC), RNDSIZE(vd_vtoc_t),
9150a55fbb7Slm66018 		    DKIOCSVTOC, STRINGIZE(DKIOCSVTOC),
9160a55fbb7Slm66018 		    &vtoc, vd_vtoc2vtoc, NULL},
9174bac2208Snarayan 		{VD_OP_SET_EFI, STRINGIZE(VD_OP_SET_EFI), RNDSIZE(vd_efi_t),
9184bac2208Snarayan 		    DKIOCSETEFI, STRINGIZE(DKIOCSETEFI),
9194bac2208Snarayan 		    &dk_efi, vd_set_efi_in, vd_set_efi_out},
9200a55fbb7Slm66018 	};
9211ae08745Sheppo 	size_t		nioctls = (sizeof (ioctl))/(sizeof (ioctl[0]));
9221ae08745Sheppo 
9231ae08745Sheppo 
924d10e4ef2Snarayan 	ASSERT(vd != NULL);
925d10e4ef2Snarayan 	ASSERT(request != NULL);
9261ae08745Sheppo 	ASSERT(request->slice < vd->nslices);
9271ae08745Sheppo 
9281ae08745Sheppo 	/*
9291ae08745Sheppo 	 * Determine ioctl corresponding to caller's "operation" and
9301ae08745Sheppo 	 * validate caller's "nbytes"
9311ae08745Sheppo 	 */
9321ae08745Sheppo 	for (i = 0; i < nioctls; i++) {
9331ae08745Sheppo 		if (request->operation == ioctl[i].operation) {
9340a55fbb7Slm66018 			/* LDC memory operations require 8-byte multiples */
9350a55fbb7Slm66018 			ASSERT(ioctl[i].nbytes % sizeof (uint64_t) == 0);
9360a55fbb7Slm66018 
9374bac2208Snarayan 			if (request->operation == VD_OP_GET_EFI ||
9384bac2208Snarayan 			    request->operation == VD_OP_SET_EFI) {
9394bac2208Snarayan 				if (request->nbytes >= ioctl[i].nbytes)
9404bac2208Snarayan 					break;
9413af08d82Slm66018 				PR0("%s:  Expected at least nbytes = %lu, "
9424bac2208Snarayan 				    "got %lu", ioctl[i].operation_name,
9434bac2208Snarayan 				    ioctl[i].nbytes, request->nbytes);
9444bac2208Snarayan 				return (EINVAL);
9454bac2208Snarayan 			}
9464bac2208Snarayan 
9470a55fbb7Slm66018 			if (request->nbytes != ioctl[i].nbytes) {
9483af08d82Slm66018 				PR0("%s:  Expected nbytes = %lu, got %lu",
9490a55fbb7Slm66018 				    ioctl[i].operation_name, ioctl[i].nbytes,
9500a55fbb7Slm66018 				    request->nbytes);
9511ae08745Sheppo 				return (EINVAL);
9521ae08745Sheppo 			}
9531ae08745Sheppo 
9541ae08745Sheppo 			break;
9551ae08745Sheppo 		}
9561ae08745Sheppo 	}
9571ae08745Sheppo 	ASSERT(i < nioctls);	/* because "operation" already validated */
9581ae08745Sheppo 
9591ae08745Sheppo 	if (request->nbytes)
9601ae08745Sheppo 		buf = kmem_zalloc(request->nbytes, KM_SLEEP);
9611ae08745Sheppo 	status = vd_do_ioctl(vd, request, buf, &ioctl[i]);
9621ae08745Sheppo 	if (request->nbytes)
9631ae08745Sheppo 		kmem_free(buf, request->nbytes);
9644bac2208Snarayan 	if (vd->vdisk_type == VD_DISK_TYPE_DISK &&
9654bac2208Snarayan 	    (request->operation == VD_OP_SET_VTOC ||
966*34683adeSsg70180 	    request->operation == VD_OP_SET_EFI)) {
967*34683adeSsg70180 		/* update disk information */
968*34683adeSsg70180 		rc = vd_read_vtoc(vd->ldi_handle[0], &vd->vtoc,
969*34683adeSsg70180 		    &vd->vdisk_label);
970*34683adeSsg70180 		if (rc != 0)
971*34683adeSsg70180 			PR0("vd_read_vtoc return error %d", rc);
972*34683adeSsg70180 	}
973d10e4ef2Snarayan 	PR0("Returning %d", status);
9741ae08745Sheppo 	return (status);
9751ae08745Sheppo }
9761ae08745Sheppo 
9774bac2208Snarayan static int
9784bac2208Snarayan vd_get_devid(vd_task_t *task)
9794bac2208Snarayan {
9804bac2208Snarayan 	vd_t *vd = task->vd;
9814bac2208Snarayan 	vd_dring_payload_t *request = task->request;
9824bac2208Snarayan 	vd_devid_t *vd_devid;
9834bac2208Snarayan 	impl_devid_t *devid;
9844bac2208Snarayan 	int status, bufid_len, devid_len, len;
9853af08d82Slm66018 	int bufbytes;
9864bac2208Snarayan 
9873af08d82Slm66018 	PR1("Get Device ID, nbytes=%ld", request->nbytes);
9884bac2208Snarayan 
9894bac2208Snarayan 	if (ddi_lyr_get_devid(vd->dev[request->slice],
9904bac2208Snarayan 	    (ddi_devid_t *)&devid) != DDI_SUCCESS) {
9914bac2208Snarayan 		/* the most common failure is that no devid is available */
9923af08d82Slm66018 		PR2("No Device ID");
9934bac2208Snarayan 		return (ENOENT);
9944bac2208Snarayan 	}
9954bac2208Snarayan 
9964bac2208Snarayan 	bufid_len = request->nbytes - sizeof (vd_devid_t) + 1;
9974bac2208Snarayan 	devid_len = DEVID_GETLEN(devid);
9984bac2208Snarayan 
9993af08d82Slm66018 	/*
10003af08d82Slm66018 	 * Save the buffer size here for use in deallocation.
10013af08d82Slm66018 	 * The actual number of bytes copied is returned in
10023af08d82Slm66018 	 * the 'nbytes' field of the request structure.
10033af08d82Slm66018 	 */
10043af08d82Slm66018 	bufbytes = request->nbytes;
10053af08d82Slm66018 
10063af08d82Slm66018 	vd_devid = kmem_zalloc(bufbytes, KM_SLEEP);
10074bac2208Snarayan 	vd_devid->length = devid_len;
10084bac2208Snarayan 	vd_devid->type = DEVID_GETTYPE(devid);
10094bac2208Snarayan 
10104bac2208Snarayan 	len = (devid_len > bufid_len)? bufid_len : devid_len;
10114bac2208Snarayan 
10124bac2208Snarayan 	bcopy(devid->did_id, vd_devid->id, len);
10134bac2208Snarayan 
10144bac2208Snarayan 	/* LDC memory operations require 8-byte multiples */
10154bac2208Snarayan 	ASSERT(request->nbytes % sizeof (uint64_t) == 0);
10164bac2208Snarayan 
10174bac2208Snarayan 	if ((status = ldc_mem_copy(vd->ldc_handle, (caddr_t)vd_devid, 0,
10184bac2208Snarayan 	    &request->nbytes, request->cookie, request->ncookies,
10194bac2208Snarayan 	    LDC_COPY_OUT)) != 0) {
10203af08d82Slm66018 		PR0("ldc_mem_copy() returned errno %d copying to client",
10214bac2208Snarayan 		    status);
10224bac2208Snarayan 	}
10233af08d82Slm66018 	PR1("post mem_copy: nbytes=%ld", request->nbytes);
10244bac2208Snarayan 
10253af08d82Slm66018 	kmem_free(vd_devid, bufbytes);
10264bac2208Snarayan 	ddi_devid_free((ddi_devid_t)devid);
10274bac2208Snarayan 
10284bac2208Snarayan 	return (status);
10294bac2208Snarayan }
10304bac2208Snarayan 
10311ae08745Sheppo /*
10321ae08745Sheppo  * Define the supported operations once the functions for performing them have
10331ae08745Sheppo  * been defined
10341ae08745Sheppo  */
10351ae08745Sheppo static const vds_operation_t	vds_operation[] = {
10363af08d82Slm66018 #define	X(_s)	#_s, _s
10373af08d82Slm66018 	{X(VD_OP_BREAD),	vd_start_bio,	vd_complete_bio},
10383af08d82Slm66018 	{X(VD_OP_BWRITE),	vd_start_bio,	vd_complete_bio},
10393af08d82Slm66018 	{X(VD_OP_FLUSH),	vd_ioctl,	NULL},
10403af08d82Slm66018 	{X(VD_OP_GET_WCE),	vd_ioctl,	NULL},
10413af08d82Slm66018 	{X(VD_OP_SET_WCE),	vd_ioctl,	NULL},
10423af08d82Slm66018 	{X(VD_OP_GET_VTOC),	vd_ioctl,	NULL},
10433af08d82Slm66018 	{X(VD_OP_SET_VTOC),	vd_ioctl,	NULL},
10443af08d82Slm66018 	{X(VD_OP_GET_DISKGEOM),	vd_ioctl,	NULL},
10453af08d82Slm66018 	{X(VD_OP_SET_DISKGEOM),	vd_ioctl,	NULL},
10463af08d82Slm66018 	{X(VD_OP_GET_EFI),	vd_ioctl,	NULL},
10473af08d82Slm66018 	{X(VD_OP_SET_EFI),	vd_ioctl,	NULL},
10483af08d82Slm66018 	{X(VD_OP_GET_DEVID),	vd_get_devid,	NULL},
10493af08d82Slm66018 #undef	X
10501ae08745Sheppo };
10511ae08745Sheppo 
10521ae08745Sheppo static const size_t	vds_noperations =
10531ae08745Sheppo 	(sizeof (vds_operation))/(sizeof (vds_operation[0]));
10541ae08745Sheppo 
10551ae08745Sheppo /*
1056d10e4ef2Snarayan  * Process a task specifying a client I/O request
10571ae08745Sheppo  */
10581ae08745Sheppo static int
1059d10e4ef2Snarayan vd_process_task(vd_task_t *task)
10601ae08745Sheppo {
1061d10e4ef2Snarayan 	int			i, status;
1062d10e4ef2Snarayan 	vd_t			*vd		= task->vd;
1063d10e4ef2Snarayan 	vd_dring_payload_t	*request	= task->request;
10641ae08745Sheppo 
10651ae08745Sheppo 
1066d10e4ef2Snarayan 	ASSERT(vd != NULL);
1067d10e4ef2Snarayan 	ASSERT(request != NULL);
10681ae08745Sheppo 
1069d10e4ef2Snarayan 	/* Find the requested operation */
10701ae08745Sheppo 	for (i = 0; i < vds_noperations; i++)
10711ae08745Sheppo 		if (request->operation == vds_operation[i].operation)
1072d10e4ef2Snarayan 			break;
1073d10e4ef2Snarayan 	if (i == vds_noperations) {
10743af08d82Slm66018 		PR0("Unsupported operation %u", request->operation);
10751ae08745Sheppo 		return (ENOTSUP);
10761ae08745Sheppo 	}
10771ae08745Sheppo 
10787636cb21Slm66018 	/* Handle client using absolute disk offsets */
10797636cb21Slm66018 	if ((vd->vdisk_type == VD_DISK_TYPE_DISK) &&
10807636cb21Slm66018 	    (request->slice == UINT8_MAX))
10817636cb21Slm66018 		request->slice = VD_ENTIRE_DISK_SLICE;
10827636cb21Slm66018 
10837636cb21Slm66018 	/* Range-check slice */
10847636cb21Slm66018 	if (request->slice >= vd->nslices) {
10853af08d82Slm66018 		PR0("Invalid \"slice\" %u (max %u) for virtual disk",
10867636cb21Slm66018 		    request->slice, (vd->nslices - 1));
10877636cb21Slm66018 		return (EINVAL);
10887636cb21Slm66018 	}
10897636cb21Slm66018 
10903af08d82Slm66018 	PR1("operation : %s", vds_operation[i].namep);
10913af08d82Slm66018 
1092d10e4ef2Snarayan 	/* Start the operation */
1093d10e4ef2Snarayan 	if ((status = vds_operation[i].start(task)) != EINPROGRESS) {
10943af08d82Slm66018 		PR0("operation : %s returned status %d",
10953af08d82Slm66018 			vds_operation[i].namep, status);
1096d10e4ef2Snarayan 		request->status = status;	/* op succeeded or failed */
1097d10e4ef2Snarayan 		return (0);			/* but request completed */
10981ae08745Sheppo 	}
10991ae08745Sheppo 
1100d10e4ef2Snarayan 	ASSERT(vds_operation[i].complete != NULL);	/* debug case */
1101d10e4ef2Snarayan 	if (vds_operation[i].complete == NULL) {	/* non-debug case */
11023af08d82Slm66018 		PR0("Unexpected return of EINPROGRESS "
1103d10e4ef2Snarayan 		    "with no I/O completion handler");
1104d10e4ef2Snarayan 		request->status = EIO;	/* operation failed */
1105d10e4ef2Snarayan 		return (0);		/* but request completed */
11061ae08745Sheppo 	}
11071ae08745Sheppo 
11083af08d82Slm66018 	PR1("operation : kick off taskq entry for %s", vds_operation[i].namep);
11093af08d82Slm66018 
1110d10e4ef2Snarayan 	/* Queue a task to complete the operation */
1111d10e4ef2Snarayan 	status = ddi_taskq_dispatch(vd->completionq, vds_operation[i].complete,
1112d10e4ef2Snarayan 	    task, DDI_SLEEP);
1113d10e4ef2Snarayan 	/* ddi_taskq_dispatch(9f) guarantees success with DDI_SLEEP */
1114d10e4ef2Snarayan 	ASSERT(status == DDI_SUCCESS);
1115d10e4ef2Snarayan 
1116d10e4ef2Snarayan 	PR1("Operation in progress");
1117d10e4ef2Snarayan 	return (EINPROGRESS);	/* completion handler will finish request */
11181ae08745Sheppo }
11191ae08745Sheppo 
11201ae08745Sheppo /*
11210a55fbb7Slm66018  * Return true if the "type", "subtype", and "env" fields of the "tag" first
11220a55fbb7Slm66018  * argument match the corresponding remaining arguments; otherwise, return false
11231ae08745Sheppo  */
11240a55fbb7Slm66018 boolean_t
11251ae08745Sheppo vd_msgtype(vio_msg_tag_t *tag, int type, int subtype, int env)
11261ae08745Sheppo {
11271ae08745Sheppo 	return ((tag->vio_msgtype == type) &&
11281ae08745Sheppo 		(tag->vio_subtype == subtype) &&
11290a55fbb7Slm66018 		(tag->vio_subtype_env == env)) ? B_TRUE : B_FALSE;
11301ae08745Sheppo }
11311ae08745Sheppo 
11320a55fbb7Slm66018 /*
11330a55fbb7Slm66018  * Check whether the major/minor version specified in "ver_msg" is supported
11340a55fbb7Slm66018  * by this server.
11350a55fbb7Slm66018  */
11360a55fbb7Slm66018 static boolean_t
11370a55fbb7Slm66018 vds_supported_version(vio_ver_msg_t *ver_msg)
11380a55fbb7Slm66018 {
11390a55fbb7Slm66018 	for (int i = 0; i < vds_num_versions; i++) {
11400a55fbb7Slm66018 		ASSERT(vds_version[i].major > 0);
11410a55fbb7Slm66018 		ASSERT((i == 0) ||
11420a55fbb7Slm66018 		    (vds_version[i].major < vds_version[i-1].major));
11430a55fbb7Slm66018 
11440a55fbb7Slm66018 		/*
11450a55fbb7Slm66018 		 * If the major versions match, adjust the minor version, if
11460a55fbb7Slm66018 		 * necessary, down to the highest value supported by this
11470a55fbb7Slm66018 		 * server and return true so this message will get "ack"ed;
11480a55fbb7Slm66018 		 * the client should also support all minor versions lower
11490a55fbb7Slm66018 		 * than the value it sent
11500a55fbb7Slm66018 		 */
11510a55fbb7Slm66018 		if (ver_msg->ver_major == vds_version[i].major) {
11520a55fbb7Slm66018 			if (ver_msg->ver_minor > vds_version[i].minor) {
11530a55fbb7Slm66018 				PR0("Adjusting minor version from %u to %u",
11540a55fbb7Slm66018 				    ver_msg->ver_minor, vds_version[i].minor);
11550a55fbb7Slm66018 				ver_msg->ver_minor = vds_version[i].minor;
11560a55fbb7Slm66018 			}
11570a55fbb7Slm66018 			return (B_TRUE);
11580a55fbb7Slm66018 		}
11590a55fbb7Slm66018 
11600a55fbb7Slm66018 		/*
11610a55fbb7Slm66018 		 * If the message contains a higher major version number, set
11620a55fbb7Slm66018 		 * the message's major/minor versions to the current values
11630a55fbb7Slm66018 		 * and return false, so this message will get "nack"ed with
11640a55fbb7Slm66018 		 * these values, and the client will potentially try again
11650a55fbb7Slm66018 		 * with the same or a lower version
11660a55fbb7Slm66018 		 */
11670a55fbb7Slm66018 		if (ver_msg->ver_major > vds_version[i].major) {
11680a55fbb7Slm66018 			ver_msg->ver_major = vds_version[i].major;
11690a55fbb7Slm66018 			ver_msg->ver_minor = vds_version[i].minor;
11700a55fbb7Slm66018 			return (B_FALSE);
11710a55fbb7Slm66018 		}
11720a55fbb7Slm66018 
11730a55fbb7Slm66018 		/*
11740a55fbb7Slm66018 		 * Otherwise, the message's major version is less than the
11750a55fbb7Slm66018 		 * current major version, so continue the loop to the next
11760a55fbb7Slm66018 		 * (lower) supported version
11770a55fbb7Slm66018 		 */
11780a55fbb7Slm66018 	}
11790a55fbb7Slm66018 
11800a55fbb7Slm66018 	/*
11810a55fbb7Slm66018 	 * No common version was found; "ground" the version pair in the
11820a55fbb7Slm66018 	 * message to terminate negotiation
11830a55fbb7Slm66018 	 */
11840a55fbb7Slm66018 	ver_msg->ver_major = 0;
11850a55fbb7Slm66018 	ver_msg->ver_minor = 0;
11860a55fbb7Slm66018 	return (B_FALSE);
11870a55fbb7Slm66018 }
11880a55fbb7Slm66018 
11890a55fbb7Slm66018 /*
11900a55fbb7Slm66018  * Process a version message from a client.  vds expects to receive version
11910a55fbb7Slm66018  * messages from clients seeking service, but never issues version messages
11920a55fbb7Slm66018  * itself; therefore, vds can ACK or NACK client version messages, but does
11930a55fbb7Slm66018  * not expect to receive version-message ACKs or NACKs (and will treat such
11940a55fbb7Slm66018  * messages as invalid).
11950a55fbb7Slm66018  */
11961ae08745Sheppo static int
11970a55fbb7Slm66018 vd_process_ver_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
11981ae08745Sheppo {
11991ae08745Sheppo 	vio_ver_msg_t	*ver_msg = (vio_ver_msg_t *)msg;
12001ae08745Sheppo 
12011ae08745Sheppo 
12021ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
12031ae08745Sheppo 
12041ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
12051ae08745Sheppo 		VIO_VER_INFO)) {
12061ae08745Sheppo 		return (ENOMSG);	/* not a version message */
12071ae08745Sheppo 	}
12081ae08745Sheppo 
12091ae08745Sheppo 	if (msglen != sizeof (*ver_msg)) {
12103af08d82Slm66018 		PR0("Expected %lu-byte version message; "
12111ae08745Sheppo 		    "received %lu bytes", sizeof (*ver_msg), msglen);
12121ae08745Sheppo 		return (EBADMSG);
12131ae08745Sheppo 	}
12141ae08745Sheppo 
12151ae08745Sheppo 	if (ver_msg->dev_class != VDEV_DISK) {
12163af08d82Slm66018 		PR0("Expected device class %u (disk); received %u",
12171ae08745Sheppo 		    VDEV_DISK, ver_msg->dev_class);
12181ae08745Sheppo 		return (EBADMSG);
12191ae08745Sheppo 	}
12201ae08745Sheppo 
12210a55fbb7Slm66018 	/*
12220a55fbb7Slm66018 	 * We're talking to the expected kind of client; set our device class
12230a55fbb7Slm66018 	 * for "ack/nack" back to the client
12240a55fbb7Slm66018 	 */
12251ae08745Sheppo 	ver_msg->dev_class = VDEV_DISK_SERVER;
12260a55fbb7Slm66018 
12270a55fbb7Slm66018 	/*
12280a55fbb7Slm66018 	 * Check whether the (valid) version message specifies a version
12290a55fbb7Slm66018 	 * supported by this server.  If the version is not supported, return
12300a55fbb7Slm66018 	 * EBADMSG so the message will get "nack"ed; vds_supported_version()
12310a55fbb7Slm66018 	 * will have updated the message with a supported version for the
12320a55fbb7Slm66018 	 * client to consider
12330a55fbb7Slm66018 	 */
12340a55fbb7Slm66018 	if (!vds_supported_version(ver_msg))
12350a55fbb7Slm66018 		return (EBADMSG);
12360a55fbb7Slm66018 
12370a55fbb7Slm66018 
12380a55fbb7Slm66018 	/*
12390a55fbb7Slm66018 	 * A version has been agreed upon; use the client's SID for
12400a55fbb7Slm66018 	 * communication on this channel now
12410a55fbb7Slm66018 	 */
12420a55fbb7Slm66018 	ASSERT(!(vd->initialized & VD_SID));
12430a55fbb7Slm66018 	vd->sid = ver_msg->tag.vio_sid;
12440a55fbb7Slm66018 	vd->initialized |= VD_SID;
12450a55fbb7Slm66018 
12460a55fbb7Slm66018 	/*
12470a55fbb7Slm66018 	 * When multiple versions are supported, this function should store
12480a55fbb7Slm66018 	 * the negotiated major and minor version values in the "vd" data
12490a55fbb7Slm66018 	 * structure to govern further communication; in particular, note that
12500a55fbb7Slm66018 	 * the client might have specified a lower minor version for the
12510a55fbb7Slm66018 	 * agreed major version than specifed in the vds_version[] array.  The
12520a55fbb7Slm66018 	 * following assertions should help remind future maintainers to make
12530a55fbb7Slm66018 	 * the appropriate changes to support multiple versions.
12540a55fbb7Slm66018 	 */
12550a55fbb7Slm66018 	ASSERT(vds_num_versions == 1);
12560a55fbb7Slm66018 	ASSERT(ver_msg->ver_major == vds_version[0].major);
12570a55fbb7Slm66018 	ASSERT(ver_msg->ver_minor == vds_version[0].minor);
12580a55fbb7Slm66018 
12590a55fbb7Slm66018 	PR0("Using major version %u, minor version %u",
12600a55fbb7Slm66018 	    ver_msg->ver_major, ver_msg->ver_minor);
12611ae08745Sheppo 	return (0);
12621ae08745Sheppo }
12631ae08745Sheppo 
12641ae08745Sheppo static int
12651ae08745Sheppo vd_process_attr_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
12661ae08745Sheppo {
12671ae08745Sheppo 	vd_attr_msg_t	*attr_msg = (vd_attr_msg_t *)msg;
12681ae08745Sheppo 
12691ae08745Sheppo 
12701ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
12711ae08745Sheppo 
12721ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
12731ae08745Sheppo 		VIO_ATTR_INFO)) {
1274d10e4ef2Snarayan 		PR0("Message is not an attribute message");
1275d10e4ef2Snarayan 		return (ENOMSG);
12761ae08745Sheppo 	}
12771ae08745Sheppo 
12781ae08745Sheppo 	if (msglen != sizeof (*attr_msg)) {
12793af08d82Slm66018 		PR0("Expected %lu-byte attribute message; "
12801ae08745Sheppo 		    "received %lu bytes", sizeof (*attr_msg), msglen);
12811ae08745Sheppo 		return (EBADMSG);
12821ae08745Sheppo 	}
12831ae08745Sheppo 
12841ae08745Sheppo 	if (attr_msg->max_xfer_sz == 0) {
12853af08d82Slm66018 		PR0("Received maximum transfer size of 0 from client");
12861ae08745Sheppo 		return (EBADMSG);
12871ae08745Sheppo 	}
12881ae08745Sheppo 
12891ae08745Sheppo 	if ((attr_msg->xfer_mode != VIO_DESC_MODE) &&
12901ae08745Sheppo 	    (attr_msg->xfer_mode != VIO_DRING_MODE)) {
12913af08d82Slm66018 		PR0("Client requested unsupported transfer mode");
12921ae08745Sheppo 		return (EBADMSG);
12931ae08745Sheppo 	}
12941ae08745Sheppo 
12951ae08745Sheppo 	/* Success:  valid message and transfer mode */
12961ae08745Sheppo 	vd->xfer_mode = attr_msg->xfer_mode;
12973af08d82Slm66018 
12981ae08745Sheppo 	if (vd->xfer_mode == VIO_DESC_MODE) {
12993af08d82Slm66018 
13001ae08745Sheppo 		/*
13011ae08745Sheppo 		 * The vd_dring_inband_msg_t contains one cookie; need room
13021ae08745Sheppo 		 * for up to n-1 more cookies, where "n" is the number of full
13031ae08745Sheppo 		 * pages plus possibly one partial page required to cover
13041ae08745Sheppo 		 * "max_xfer_sz".  Add room for one more cookie if
13051ae08745Sheppo 		 * "max_xfer_sz" isn't an integral multiple of the page size.
13061ae08745Sheppo 		 * Must first get the maximum transfer size in bytes.
13071ae08745Sheppo 		 */
13081ae08745Sheppo 		size_t	max_xfer_bytes = attr_msg->vdisk_block_size ?
13091ae08745Sheppo 		    attr_msg->vdisk_block_size*attr_msg->max_xfer_sz :
13101ae08745Sheppo 		    attr_msg->max_xfer_sz;
13111ae08745Sheppo 		size_t	max_inband_msglen =
13121ae08745Sheppo 		    sizeof (vd_dring_inband_msg_t) +
13131ae08745Sheppo 		    ((max_xfer_bytes/PAGESIZE +
13141ae08745Sheppo 			((max_xfer_bytes % PAGESIZE) ? 1 : 0))*
13151ae08745Sheppo 			(sizeof (ldc_mem_cookie_t)));
13161ae08745Sheppo 
13171ae08745Sheppo 		/*
13181ae08745Sheppo 		 * Set the maximum expected message length to
13191ae08745Sheppo 		 * accommodate in-band-descriptor messages with all
13201ae08745Sheppo 		 * their cookies
13211ae08745Sheppo 		 */
13221ae08745Sheppo 		vd->max_msglen = MAX(vd->max_msglen, max_inband_msglen);
1323d10e4ef2Snarayan 
1324d10e4ef2Snarayan 		/*
1325d10e4ef2Snarayan 		 * Initialize the data structure for processing in-band I/O
1326d10e4ef2Snarayan 		 * request descriptors
1327d10e4ef2Snarayan 		 */
1328d10e4ef2Snarayan 		vd->inband_task.vd	= vd;
13293af08d82Slm66018 		vd->inband_task.msg	= kmem_alloc(vd->max_msglen, KM_SLEEP);
1330d10e4ef2Snarayan 		vd->inband_task.index	= 0;
1331d10e4ef2Snarayan 		vd->inband_task.type	= VD_FINAL_RANGE_TASK;	/* range == 1 */
13321ae08745Sheppo 	}
13331ae08745Sheppo 
1334e1ebb9ecSlm66018 	/* Return the device's block size and max transfer size to the client */
1335e1ebb9ecSlm66018 	attr_msg->vdisk_block_size	= DEV_BSIZE;
1336e1ebb9ecSlm66018 	attr_msg->max_xfer_sz		= vd->max_xfer_sz;
1337e1ebb9ecSlm66018 
13381ae08745Sheppo 	attr_msg->vdisk_size = vd->vdisk_size;
13391ae08745Sheppo 	attr_msg->vdisk_type = vd->vdisk_type;
13401ae08745Sheppo 	attr_msg->operations = vds_operations;
13411ae08745Sheppo 	PR0("%s", VD_CLIENT(vd));
13423af08d82Slm66018 
13433af08d82Slm66018 	ASSERT(vd->dring_task == NULL);
13443af08d82Slm66018 
13451ae08745Sheppo 	return (0);
13461ae08745Sheppo }
13471ae08745Sheppo 
13481ae08745Sheppo static int
13491ae08745Sheppo vd_process_dring_reg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
13501ae08745Sheppo {
13511ae08745Sheppo 	int			status;
13521ae08745Sheppo 	size_t			expected;
13531ae08745Sheppo 	ldc_mem_info_t		dring_minfo;
13541ae08745Sheppo 	vio_dring_reg_msg_t	*reg_msg = (vio_dring_reg_msg_t *)msg;
13551ae08745Sheppo 
13561ae08745Sheppo 
13571ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
13581ae08745Sheppo 
13591ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
13601ae08745Sheppo 		VIO_DRING_REG)) {
1361d10e4ef2Snarayan 		PR0("Message is not a register-dring message");
1362d10e4ef2Snarayan 		return (ENOMSG);
13631ae08745Sheppo 	}
13641ae08745Sheppo 
13651ae08745Sheppo 	if (msglen < sizeof (*reg_msg)) {
13663af08d82Slm66018 		PR0("Expected at least %lu-byte register-dring message; "
13671ae08745Sheppo 		    "received %lu bytes", sizeof (*reg_msg), msglen);
13681ae08745Sheppo 		return (EBADMSG);
13691ae08745Sheppo 	}
13701ae08745Sheppo 
13711ae08745Sheppo 	expected = sizeof (*reg_msg) +
13721ae08745Sheppo 	    (reg_msg->ncookies - 1)*(sizeof (reg_msg->cookie[0]));
13731ae08745Sheppo 	if (msglen != expected) {
13743af08d82Slm66018 		PR0("Expected %lu-byte register-dring message; "
13751ae08745Sheppo 		    "received %lu bytes", expected, msglen);
13761ae08745Sheppo 		return (EBADMSG);
13771ae08745Sheppo 	}
13781ae08745Sheppo 
13791ae08745Sheppo 	if (vd->initialized & VD_DRING) {
13803af08d82Slm66018 		PR0("A dring was previously registered; only support one");
13811ae08745Sheppo 		return (EBADMSG);
13821ae08745Sheppo 	}
13831ae08745Sheppo 
1384d10e4ef2Snarayan 	if (reg_msg->num_descriptors > INT32_MAX) {
13853af08d82Slm66018 		PR0("reg_msg->num_descriptors = %u; must be <= %u (%s)",
1386d10e4ef2Snarayan 		    reg_msg->ncookies, INT32_MAX, STRINGIZE(INT32_MAX));
1387d10e4ef2Snarayan 		return (EBADMSG);
1388d10e4ef2Snarayan 	}
1389d10e4ef2Snarayan 
13901ae08745Sheppo 	if (reg_msg->ncookies != 1) {
13911ae08745Sheppo 		/*
13921ae08745Sheppo 		 * In addition to fixing the assertion in the success case
13931ae08745Sheppo 		 * below, supporting drings which require more than one
13941ae08745Sheppo 		 * "cookie" requires increasing the value of vd->max_msglen
13951ae08745Sheppo 		 * somewhere in the code path prior to receiving the message
13961ae08745Sheppo 		 * which results in calling this function.  Note that without
13971ae08745Sheppo 		 * making this change, the larger message size required to
13981ae08745Sheppo 		 * accommodate multiple cookies cannot be successfully
13991ae08745Sheppo 		 * received, so this function will not even get called.
14001ae08745Sheppo 		 * Gracefully accommodating more dring cookies might
14011ae08745Sheppo 		 * reasonably demand exchanging an additional attribute or
14021ae08745Sheppo 		 * making a minor protocol adjustment
14031ae08745Sheppo 		 */
14043af08d82Slm66018 		PR0("reg_msg->ncookies = %u != 1", reg_msg->ncookies);
14051ae08745Sheppo 		return (EBADMSG);
14061ae08745Sheppo 	}
14071ae08745Sheppo 
14081ae08745Sheppo 	status = ldc_mem_dring_map(vd->ldc_handle, reg_msg->cookie,
14091ae08745Sheppo 	    reg_msg->ncookies, reg_msg->num_descriptors,
14104bac2208Snarayan 	    reg_msg->descriptor_size, LDC_DIRECT_MAP, &vd->dring_handle);
14111ae08745Sheppo 	if (status != 0) {
14123af08d82Slm66018 		PR0("ldc_mem_dring_map() returned errno %d", status);
14131ae08745Sheppo 		return (status);
14141ae08745Sheppo 	}
14151ae08745Sheppo 
14161ae08745Sheppo 	/*
14171ae08745Sheppo 	 * To remove the need for this assertion, must call
14181ae08745Sheppo 	 * ldc_mem_dring_nextcookie() successfully ncookies-1 times after a
14191ae08745Sheppo 	 * successful call to ldc_mem_dring_map()
14201ae08745Sheppo 	 */
14211ae08745Sheppo 	ASSERT(reg_msg->ncookies == 1);
14221ae08745Sheppo 
14231ae08745Sheppo 	if ((status =
14241ae08745Sheppo 		ldc_mem_dring_info(vd->dring_handle, &dring_minfo)) != 0) {
14253af08d82Slm66018 		PR0("ldc_mem_dring_info() returned errno %d", status);
14261ae08745Sheppo 		if ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0)
14273af08d82Slm66018 			PR0("ldc_mem_dring_unmap() returned errno %d", status);
14281ae08745Sheppo 		return (status);
14291ae08745Sheppo 	}
14301ae08745Sheppo 
14311ae08745Sheppo 	if (dring_minfo.vaddr == NULL) {
14323af08d82Slm66018 		PR0("Descriptor ring virtual address is NULL");
14330a55fbb7Slm66018 		return (ENXIO);
14341ae08745Sheppo 	}
14351ae08745Sheppo 
14361ae08745Sheppo 
1437d10e4ef2Snarayan 	/* Initialize for valid message and mapped dring */
14381ae08745Sheppo 	PR1("descriptor size = %u, dring length = %u",
14391ae08745Sheppo 	    vd->descriptor_size, vd->dring_len);
14401ae08745Sheppo 	vd->initialized |= VD_DRING;
14411ae08745Sheppo 	vd->dring_ident = 1;	/* "There Can Be Only One" */
14421ae08745Sheppo 	vd->dring = dring_minfo.vaddr;
14431ae08745Sheppo 	vd->descriptor_size = reg_msg->descriptor_size;
14441ae08745Sheppo 	vd->dring_len = reg_msg->num_descriptors;
14451ae08745Sheppo 	reg_msg->dring_ident = vd->dring_ident;
1446d10e4ef2Snarayan 
1447d10e4ef2Snarayan 	/*
1448d10e4ef2Snarayan 	 * Allocate and initialize a "shadow" array of data structures for
1449d10e4ef2Snarayan 	 * tasks to process I/O requests in dring elements
1450d10e4ef2Snarayan 	 */
1451d10e4ef2Snarayan 	vd->dring_task =
1452d10e4ef2Snarayan 	    kmem_zalloc((sizeof (*vd->dring_task)) * vd->dring_len, KM_SLEEP);
1453d10e4ef2Snarayan 	for (int i = 0; i < vd->dring_len; i++) {
1454d10e4ef2Snarayan 		vd->dring_task[i].vd		= vd;
1455d10e4ef2Snarayan 		vd->dring_task[i].index		= i;
1456d10e4ef2Snarayan 		vd->dring_task[i].request	= &VD_DRING_ELEM(i)->payload;
14574bac2208Snarayan 
14584bac2208Snarayan 		status = ldc_mem_alloc_handle(vd->ldc_handle,
14594bac2208Snarayan 		    &(vd->dring_task[i].mhdl));
14604bac2208Snarayan 		if (status) {
14613af08d82Slm66018 			PR0("ldc_mem_alloc_handle() returned err %d ", status);
14624bac2208Snarayan 			return (ENXIO);
14634bac2208Snarayan 		}
14643af08d82Slm66018 
14653af08d82Slm66018 		vd->dring_task[i].msg = kmem_alloc(vd->max_msglen, KM_SLEEP);
1466d10e4ef2Snarayan 	}
1467d10e4ef2Snarayan 
14681ae08745Sheppo 	return (0);
14691ae08745Sheppo }
14701ae08745Sheppo 
14711ae08745Sheppo static int
14721ae08745Sheppo vd_process_dring_unreg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
14731ae08745Sheppo {
14741ae08745Sheppo 	vio_dring_unreg_msg_t	*unreg_msg = (vio_dring_unreg_msg_t *)msg;
14751ae08745Sheppo 
14761ae08745Sheppo 
14771ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
14781ae08745Sheppo 
14791ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
14801ae08745Sheppo 		VIO_DRING_UNREG)) {
1481d10e4ef2Snarayan 		PR0("Message is not an unregister-dring message");
1482d10e4ef2Snarayan 		return (ENOMSG);
14831ae08745Sheppo 	}
14841ae08745Sheppo 
14851ae08745Sheppo 	if (msglen != sizeof (*unreg_msg)) {
14863af08d82Slm66018 		PR0("Expected %lu-byte unregister-dring message; "
14871ae08745Sheppo 		    "received %lu bytes", sizeof (*unreg_msg), msglen);
14881ae08745Sheppo 		return (EBADMSG);
14891ae08745Sheppo 	}
14901ae08745Sheppo 
14911ae08745Sheppo 	if (unreg_msg->dring_ident != vd->dring_ident) {
14923af08d82Slm66018 		PR0("Expected dring ident %lu; received %lu",
14931ae08745Sheppo 		    vd->dring_ident, unreg_msg->dring_ident);
14941ae08745Sheppo 		return (EBADMSG);
14951ae08745Sheppo 	}
14961ae08745Sheppo 
14971ae08745Sheppo 	return (0);
14981ae08745Sheppo }
14991ae08745Sheppo 
15001ae08745Sheppo static int
15011ae08745Sheppo process_rdx_msg(vio_msg_t *msg, size_t msglen)
15021ae08745Sheppo {
15031ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
15041ae08745Sheppo 
1505d10e4ef2Snarayan 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX)) {
1506d10e4ef2Snarayan 		PR0("Message is not an RDX message");
1507d10e4ef2Snarayan 		return (ENOMSG);
1508d10e4ef2Snarayan 	}
15091ae08745Sheppo 
15101ae08745Sheppo 	if (msglen != sizeof (vio_rdx_msg_t)) {
15113af08d82Slm66018 		PR0("Expected %lu-byte RDX message; received %lu bytes",
15121ae08745Sheppo 		    sizeof (vio_rdx_msg_t), msglen);
15131ae08745Sheppo 		return (EBADMSG);
15141ae08745Sheppo 	}
15151ae08745Sheppo 
1516d10e4ef2Snarayan 	PR0("Valid RDX message");
15171ae08745Sheppo 	return (0);
15181ae08745Sheppo }
15191ae08745Sheppo 
15201ae08745Sheppo static int
15211ae08745Sheppo vd_check_seq_num(vd_t *vd, uint64_t seq_num)
15221ae08745Sheppo {
15231ae08745Sheppo 	if ((vd->initialized & VD_SEQ_NUM) && (seq_num != vd->seq_num + 1)) {
15243af08d82Slm66018 		PR0("Received seq_num %lu; expected %lu",
15251ae08745Sheppo 		    seq_num, (vd->seq_num + 1));
15263af08d82Slm66018 		PR0("initiating soft reset");
1527d10e4ef2Snarayan 		vd_need_reset(vd, B_FALSE);
15281ae08745Sheppo 		return (1);
15291ae08745Sheppo 	}
15301ae08745Sheppo 
15311ae08745Sheppo 	vd->seq_num = seq_num;
15321ae08745Sheppo 	vd->initialized |= VD_SEQ_NUM;	/* superfluous after first time... */
15331ae08745Sheppo 	return (0);
15341ae08745Sheppo }
15351ae08745Sheppo 
15361ae08745Sheppo /*
15371ae08745Sheppo  * Return the expected size of an inband-descriptor message with all the
15381ae08745Sheppo  * cookies it claims to include
15391ae08745Sheppo  */
15401ae08745Sheppo static size_t
15411ae08745Sheppo expected_inband_size(vd_dring_inband_msg_t *msg)
15421ae08745Sheppo {
15431ae08745Sheppo 	return ((sizeof (*msg)) +
15441ae08745Sheppo 	    (msg->payload.ncookies - 1)*(sizeof (msg->payload.cookie[0])));
15451ae08745Sheppo }
15461ae08745Sheppo 
15471ae08745Sheppo /*
15481ae08745Sheppo  * Process an in-band descriptor message:  used with clients like OBP, with
15491ae08745Sheppo  * which vds exchanges descriptors within VIO message payloads, rather than
15501ae08745Sheppo  * operating on them within a descriptor ring
15511ae08745Sheppo  */
15521ae08745Sheppo static int
15533af08d82Slm66018 vd_process_desc_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
15541ae08745Sheppo {
15551ae08745Sheppo 	size_t			expected;
15561ae08745Sheppo 	vd_dring_inband_msg_t	*desc_msg = (vd_dring_inband_msg_t *)msg;
15571ae08745Sheppo 
15581ae08745Sheppo 
15591ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
15601ae08745Sheppo 
15611ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
1562d10e4ef2Snarayan 		VIO_DESC_DATA)) {
1563d10e4ef2Snarayan 		PR1("Message is not an in-band-descriptor message");
1564d10e4ef2Snarayan 		return (ENOMSG);
1565d10e4ef2Snarayan 	}
15661ae08745Sheppo 
15671ae08745Sheppo 	if (msglen < sizeof (*desc_msg)) {
15683af08d82Slm66018 		PR0("Expected at least %lu-byte descriptor message; "
15691ae08745Sheppo 		    "received %lu bytes", sizeof (*desc_msg), msglen);
15701ae08745Sheppo 		return (EBADMSG);
15711ae08745Sheppo 	}
15721ae08745Sheppo 
15731ae08745Sheppo 	if (msglen != (expected = expected_inband_size(desc_msg))) {
15743af08d82Slm66018 		PR0("Expected %lu-byte descriptor message; "
15751ae08745Sheppo 		    "received %lu bytes", expected, msglen);
15761ae08745Sheppo 		return (EBADMSG);
15771ae08745Sheppo 	}
15781ae08745Sheppo 
1579d10e4ef2Snarayan 	if (vd_check_seq_num(vd, desc_msg->hdr.seq_num) != 0)
15801ae08745Sheppo 		return (EBADMSG);
15811ae08745Sheppo 
1582d10e4ef2Snarayan 	/*
1583d10e4ef2Snarayan 	 * Valid message:  Set up the in-band descriptor task and process the
1584d10e4ef2Snarayan 	 * request.  Arrange to acknowledge the client's message, unless an
1585d10e4ef2Snarayan 	 * error processing the descriptor task results in setting
1586d10e4ef2Snarayan 	 * VIO_SUBTYPE_NACK
1587d10e4ef2Snarayan 	 */
1588d10e4ef2Snarayan 	PR1("Valid in-band-descriptor message");
1589d10e4ef2Snarayan 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
15903af08d82Slm66018 
15913af08d82Slm66018 	ASSERT(vd->inband_task.msg != NULL);
15923af08d82Slm66018 
15933af08d82Slm66018 	bcopy(msg, vd->inband_task.msg, msglen);
1594d10e4ef2Snarayan 	vd->inband_task.msglen	= msglen;
15953af08d82Slm66018 
15963af08d82Slm66018 	/*
15973af08d82Slm66018 	 * The task request is now the payload of the message
15983af08d82Slm66018 	 * that was just copied into the body of the task.
15993af08d82Slm66018 	 */
16003af08d82Slm66018 	desc_msg = (vd_dring_inband_msg_t *)vd->inband_task.msg;
1601d10e4ef2Snarayan 	vd->inband_task.request	= &desc_msg->payload;
16023af08d82Slm66018 
1603d10e4ef2Snarayan 	return (vd_process_task(&vd->inband_task));
16041ae08745Sheppo }
16051ae08745Sheppo 
16061ae08745Sheppo static int
1607d10e4ef2Snarayan vd_process_element(vd_t *vd, vd_task_type_t type, uint32_t idx,
16083af08d82Slm66018     vio_msg_t *msg, size_t msglen)
16091ae08745Sheppo {
16101ae08745Sheppo 	int			status;
1611d10e4ef2Snarayan 	boolean_t		ready;
1612d10e4ef2Snarayan 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
16131ae08745Sheppo 
16141ae08745Sheppo 
1615d10e4ef2Snarayan 	/* Accept the updated dring element */
1616d10e4ef2Snarayan 	if ((status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) {
16173af08d82Slm66018 		PR0("ldc_mem_dring_acquire() returned errno %d", status);
16181ae08745Sheppo 		return (status);
16191ae08745Sheppo 	}
1620d10e4ef2Snarayan 	ready = (elem->hdr.dstate == VIO_DESC_READY);
1621d10e4ef2Snarayan 	if (ready) {
1622d10e4ef2Snarayan 		elem->hdr.dstate = VIO_DESC_ACCEPTED;
1623d10e4ef2Snarayan 	} else {
16243af08d82Slm66018 		PR0("descriptor %u not ready", idx);
1625d10e4ef2Snarayan 		VD_DUMP_DRING_ELEM(elem);
1626d10e4ef2Snarayan 	}
1627d10e4ef2Snarayan 	if ((status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) {
16283af08d82Slm66018 		PR0("ldc_mem_dring_release() returned errno %d", status);
16291ae08745Sheppo 		return (status);
16301ae08745Sheppo 	}
1631d10e4ef2Snarayan 	if (!ready)
1632d10e4ef2Snarayan 		return (EBUSY);
16331ae08745Sheppo 
16341ae08745Sheppo 
1635d10e4ef2Snarayan 	/* Initialize a task and process the accepted element */
1636d10e4ef2Snarayan 	PR1("Processing dring element %u", idx);
1637d10e4ef2Snarayan 	vd->dring_task[idx].type	= type;
16383af08d82Slm66018 
16393af08d82Slm66018 	/* duplicate msg buf for cookies etc. */
16403af08d82Slm66018 	bcopy(msg, vd->dring_task[idx].msg, msglen);
16413af08d82Slm66018 
1642d10e4ef2Snarayan 	vd->dring_task[idx].msglen	= msglen;
1643d10e4ef2Snarayan 	if ((status = vd_process_task(&vd->dring_task[idx])) != EINPROGRESS)
1644d10e4ef2Snarayan 		status = vd_mark_elem_done(vd, idx, elem->payload.status);
16451ae08745Sheppo 
16461ae08745Sheppo 	return (status);
16471ae08745Sheppo }
16481ae08745Sheppo 
16491ae08745Sheppo static int
1650d10e4ef2Snarayan vd_process_element_range(vd_t *vd, int start, int end,
16513af08d82Slm66018     vio_msg_t *msg, size_t msglen)
1652d10e4ef2Snarayan {
1653d10e4ef2Snarayan 	int		i, n, nelem, status = 0;
1654d10e4ef2Snarayan 	boolean_t	inprogress = B_FALSE;
1655d10e4ef2Snarayan 	vd_task_type_t	type;
1656d10e4ef2Snarayan 
1657d10e4ef2Snarayan 
1658d10e4ef2Snarayan 	ASSERT(start >= 0);
1659d10e4ef2Snarayan 	ASSERT(end >= 0);
1660d10e4ef2Snarayan 
1661d10e4ef2Snarayan 	/*
1662d10e4ef2Snarayan 	 * Arrange to acknowledge the client's message, unless an error
1663d10e4ef2Snarayan 	 * processing one of the dring elements results in setting
1664d10e4ef2Snarayan 	 * VIO_SUBTYPE_NACK
1665d10e4ef2Snarayan 	 */
1666d10e4ef2Snarayan 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
1667d10e4ef2Snarayan 
1668d10e4ef2Snarayan 	/*
1669d10e4ef2Snarayan 	 * Process the dring elements in the range
1670d10e4ef2Snarayan 	 */
1671d10e4ef2Snarayan 	nelem = ((end < start) ? end + vd->dring_len : end) - start + 1;
1672d10e4ef2Snarayan 	for (i = start, n = nelem; n > 0; i = (i + 1) % vd->dring_len, n--) {
1673d10e4ef2Snarayan 		((vio_dring_msg_t *)msg)->end_idx = i;
1674d10e4ef2Snarayan 		type = (n == 1) ? VD_FINAL_RANGE_TASK : VD_NONFINAL_RANGE_TASK;
16753af08d82Slm66018 		status = vd_process_element(vd, type, i, msg, msglen);
1676d10e4ef2Snarayan 		if (status == EINPROGRESS)
1677d10e4ef2Snarayan 			inprogress = B_TRUE;
1678d10e4ef2Snarayan 		else if (status != 0)
1679d10e4ef2Snarayan 			break;
1680d10e4ef2Snarayan 	}
1681d10e4ef2Snarayan 
1682d10e4ef2Snarayan 	/*
1683d10e4ef2Snarayan 	 * If some, but not all, operations of a multi-element range are in
1684d10e4ef2Snarayan 	 * progress, wait for other operations to complete before returning
1685d10e4ef2Snarayan 	 * (which will result in "ack" or "nack" of the message).  Note that
1686d10e4ef2Snarayan 	 * all outstanding operations will need to complete, not just the ones
1687d10e4ef2Snarayan 	 * corresponding to the current range of dring elements; howevever, as
1688d10e4ef2Snarayan 	 * this situation is an error case, performance is less critical.
1689d10e4ef2Snarayan 	 */
1690d10e4ef2Snarayan 	if ((nelem > 1) && (status != EINPROGRESS) && inprogress)
1691d10e4ef2Snarayan 		ddi_taskq_wait(vd->completionq);
1692d10e4ef2Snarayan 
1693d10e4ef2Snarayan 	return (status);
1694d10e4ef2Snarayan }
1695d10e4ef2Snarayan 
1696d10e4ef2Snarayan static int
16973af08d82Slm66018 vd_process_dring_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
16981ae08745Sheppo {
16991ae08745Sheppo 	vio_dring_msg_t	*dring_msg = (vio_dring_msg_t *)msg;
17001ae08745Sheppo 
17011ae08745Sheppo 
17021ae08745Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
17031ae08745Sheppo 
17041ae08745Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
17051ae08745Sheppo 		VIO_DRING_DATA)) {
1706d10e4ef2Snarayan 		PR1("Message is not a dring-data message");
1707d10e4ef2Snarayan 		return (ENOMSG);
17081ae08745Sheppo 	}
17091ae08745Sheppo 
17101ae08745Sheppo 	if (msglen != sizeof (*dring_msg)) {
17113af08d82Slm66018 		PR0("Expected %lu-byte dring message; received %lu bytes",
17121ae08745Sheppo 		    sizeof (*dring_msg), msglen);
17131ae08745Sheppo 		return (EBADMSG);
17141ae08745Sheppo 	}
17151ae08745Sheppo 
1716d10e4ef2Snarayan 	if (vd_check_seq_num(vd, dring_msg->seq_num) != 0)
17171ae08745Sheppo 		return (EBADMSG);
17181ae08745Sheppo 
17191ae08745Sheppo 	if (dring_msg->dring_ident != vd->dring_ident) {
17203af08d82Slm66018 		PR0("Expected dring ident %lu; received ident %lu",
17211ae08745Sheppo 		    vd->dring_ident, dring_msg->dring_ident);
17221ae08745Sheppo 		return (EBADMSG);
17231ae08745Sheppo 	}
17241ae08745Sheppo 
1725d10e4ef2Snarayan 	if (dring_msg->start_idx >= vd->dring_len) {
17263af08d82Slm66018 		PR0("\"start_idx\" = %u; must be less than %u",
1727d10e4ef2Snarayan 		    dring_msg->start_idx, vd->dring_len);
1728d10e4ef2Snarayan 		return (EBADMSG);
1729d10e4ef2Snarayan 	}
17301ae08745Sheppo 
1731d10e4ef2Snarayan 	if ((dring_msg->end_idx < 0) ||
1732d10e4ef2Snarayan 	    (dring_msg->end_idx >= vd->dring_len)) {
17333af08d82Slm66018 		PR0("\"end_idx\" = %u; must be >= 0 and less than %u",
1734d10e4ef2Snarayan 		    dring_msg->end_idx, vd->dring_len);
1735d10e4ef2Snarayan 		return (EBADMSG);
1736d10e4ef2Snarayan 	}
1737d10e4ef2Snarayan 
1738d10e4ef2Snarayan 	/* Valid message; process range of updated dring elements */
1739d10e4ef2Snarayan 	PR1("Processing descriptor range, start = %u, end = %u",
1740d10e4ef2Snarayan 	    dring_msg->start_idx, dring_msg->end_idx);
1741d10e4ef2Snarayan 	return (vd_process_element_range(vd, dring_msg->start_idx,
17423af08d82Slm66018 		dring_msg->end_idx, msg, msglen));
17431ae08745Sheppo }
17441ae08745Sheppo 
17451ae08745Sheppo static int
17461ae08745Sheppo recv_msg(ldc_handle_t ldc_handle, void *msg, size_t *nbytes)
17471ae08745Sheppo {
17481ae08745Sheppo 	int	retry, status;
17491ae08745Sheppo 	size_t	size = *nbytes;
17501ae08745Sheppo 
17511ae08745Sheppo 
17521ae08745Sheppo 	for (retry = 0, status = ETIMEDOUT;
17531ae08745Sheppo 	    retry < vds_ldc_retries && status == ETIMEDOUT;
17541ae08745Sheppo 	    retry++) {
17551ae08745Sheppo 		PR1("ldc_read() attempt %d", (retry + 1));
17561ae08745Sheppo 		*nbytes = size;
17571ae08745Sheppo 		status = ldc_read(ldc_handle, msg, nbytes);
17581ae08745Sheppo 	}
17591ae08745Sheppo 
17603af08d82Slm66018 	if (status) {
17613af08d82Slm66018 		PR0("ldc_read() returned errno %d", status);
17623af08d82Slm66018 		if (status != ECONNRESET)
17633af08d82Slm66018 			return (ENOMSG);
17641ae08745Sheppo 		return (status);
17651ae08745Sheppo 	} else if (*nbytes == 0) {
17661ae08745Sheppo 		PR1("ldc_read() returned 0 and no message read");
17671ae08745Sheppo 		return (ENOMSG);
17681ae08745Sheppo 	}
17691ae08745Sheppo 
17701ae08745Sheppo 	PR1("RCVD %lu-byte message", *nbytes);
17711ae08745Sheppo 	return (0);
17721ae08745Sheppo }
17731ae08745Sheppo 
17741ae08745Sheppo static int
17753af08d82Slm66018 vd_do_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
17761ae08745Sheppo {
17771ae08745Sheppo 	int		status;
17781ae08745Sheppo 
17791ae08745Sheppo 
17801ae08745Sheppo 	PR1("Processing (%x/%x/%x) message", msg->tag.vio_msgtype,
17811ae08745Sheppo 	    msg->tag.vio_subtype, msg->tag.vio_subtype_env);
17823af08d82Slm66018 #ifdef	DEBUG
17833af08d82Slm66018 	vd_decode_tag(msg);
17843af08d82Slm66018 #endif
17851ae08745Sheppo 
17861ae08745Sheppo 	/*
17871ae08745Sheppo 	 * Validate session ID up front, since it applies to all messages
17881ae08745Sheppo 	 * once set
17891ae08745Sheppo 	 */
17901ae08745Sheppo 	if ((msg->tag.vio_sid != vd->sid) && (vd->initialized & VD_SID)) {
17913af08d82Slm66018 		PR0("Expected SID %u, received %u", vd->sid,
17921ae08745Sheppo 		    msg->tag.vio_sid);
17931ae08745Sheppo 		return (EBADMSG);
17941ae08745Sheppo 	}
17951ae08745Sheppo 
17963af08d82Slm66018 	PR1("\tWhile in state %d (%s)", vd->state, vd_decode_state(vd->state));
17971ae08745Sheppo 
17981ae08745Sheppo 	/*
17991ae08745Sheppo 	 * Process the received message based on connection state
18001ae08745Sheppo 	 */
18011ae08745Sheppo 	switch (vd->state) {
18021ae08745Sheppo 	case VD_STATE_INIT:	/* expect version message */
18030a55fbb7Slm66018 		if ((status = vd_process_ver_msg(vd, msg, msglen)) != 0)
18041ae08745Sheppo 			return (status);
18051ae08745Sheppo 
18061ae08745Sheppo 		/* Version negotiated, move to that state */
18071ae08745Sheppo 		vd->state = VD_STATE_VER;
18081ae08745Sheppo 		return (0);
18091ae08745Sheppo 
18101ae08745Sheppo 	case VD_STATE_VER:	/* expect attribute message */
18111ae08745Sheppo 		if ((status = vd_process_attr_msg(vd, msg, msglen)) != 0)
18121ae08745Sheppo 			return (status);
18131ae08745Sheppo 
18141ae08745Sheppo 		/* Attributes exchanged, move to that state */
18151ae08745Sheppo 		vd->state = VD_STATE_ATTR;
18161ae08745Sheppo 		return (0);
18171ae08745Sheppo 
18181ae08745Sheppo 	case VD_STATE_ATTR:
18191ae08745Sheppo 		switch (vd->xfer_mode) {
18201ae08745Sheppo 		case VIO_DESC_MODE:	/* expect RDX message */
18211ae08745Sheppo 			if ((status = process_rdx_msg(msg, msglen)) != 0)
18221ae08745Sheppo 				return (status);
18231ae08745Sheppo 
18241ae08745Sheppo 			/* Ready to receive in-band descriptors */
18251ae08745Sheppo 			vd->state = VD_STATE_DATA;
18261ae08745Sheppo 			return (0);
18271ae08745Sheppo 
18281ae08745Sheppo 		case VIO_DRING_MODE:	/* expect register-dring message */
18291ae08745Sheppo 			if ((status =
18301ae08745Sheppo 				vd_process_dring_reg_msg(vd, msg, msglen)) != 0)
18311ae08745Sheppo 				return (status);
18321ae08745Sheppo 
18331ae08745Sheppo 			/* One dring negotiated, move to that state */
18341ae08745Sheppo 			vd->state = VD_STATE_DRING;
18351ae08745Sheppo 			return (0);
18361ae08745Sheppo 
18371ae08745Sheppo 		default:
18381ae08745Sheppo 			ASSERT("Unsupported transfer mode");
18393af08d82Slm66018 			PR0("Unsupported transfer mode");
18401ae08745Sheppo 			return (ENOTSUP);
18411ae08745Sheppo 		}
18421ae08745Sheppo 
18431ae08745Sheppo 	case VD_STATE_DRING:	/* expect RDX, register-dring, or unreg-dring */
18441ae08745Sheppo 		if ((status = process_rdx_msg(msg, msglen)) == 0) {
18451ae08745Sheppo 			/* Ready to receive data */
18461ae08745Sheppo 			vd->state = VD_STATE_DATA;
18471ae08745Sheppo 			return (0);
18481ae08745Sheppo 		} else if (status != ENOMSG) {
18491ae08745Sheppo 			return (status);
18501ae08745Sheppo 		}
18511ae08745Sheppo 
18521ae08745Sheppo 
18531ae08745Sheppo 		/*
18541ae08745Sheppo 		 * If another register-dring message is received, stay in
18551ae08745Sheppo 		 * dring state in case the client sends RDX; although the
18561ae08745Sheppo 		 * protocol allows multiple drings, this server does not
18571ae08745Sheppo 		 * support using more than one
18581ae08745Sheppo 		 */
18591ae08745Sheppo 		if ((status =
18601ae08745Sheppo 			vd_process_dring_reg_msg(vd, msg, msglen)) != ENOMSG)
18611ae08745Sheppo 			return (status);
18621ae08745Sheppo 
18631ae08745Sheppo 		/*
18641ae08745Sheppo 		 * Acknowledge an unregister-dring message, but reset the
18651ae08745Sheppo 		 * connection anyway:  Although the protocol allows
18661ae08745Sheppo 		 * unregistering drings, this server cannot serve a vdisk
18671ae08745Sheppo 		 * without its only dring
18681ae08745Sheppo 		 */
18691ae08745Sheppo 		status = vd_process_dring_unreg_msg(vd, msg, msglen);
18701ae08745Sheppo 		return ((status == 0) ? ENOTSUP : status);
18711ae08745Sheppo 
18721ae08745Sheppo 	case VD_STATE_DATA:
18731ae08745Sheppo 		switch (vd->xfer_mode) {
18741ae08745Sheppo 		case VIO_DESC_MODE:	/* expect in-band-descriptor message */
18753af08d82Slm66018 			return (vd_process_desc_msg(vd, msg, msglen));
18761ae08745Sheppo 
18771ae08745Sheppo 		case VIO_DRING_MODE:	/* expect dring-data or unreg-dring */
18781ae08745Sheppo 			/*
18791ae08745Sheppo 			 * Typically expect dring-data messages, so handle
18801ae08745Sheppo 			 * them first
18811ae08745Sheppo 			 */
18821ae08745Sheppo 			if ((status = vd_process_dring_msg(vd, msg,
18833af08d82Slm66018 				    msglen)) != ENOMSG)
18841ae08745Sheppo 				return (status);
18851ae08745Sheppo 
18861ae08745Sheppo 			/*
18871ae08745Sheppo 			 * Acknowledge an unregister-dring message, but reset
18881ae08745Sheppo 			 * the connection anyway:  Although the protocol
18891ae08745Sheppo 			 * allows unregistering drings, this server cannot
18901ae08745Sheppo 			 * serve a vdisk without its only dring
18911ae08745Sheppo 			 */
18921ae08745Sheppo 			status = vd_process_dring_unreg_msg(vd, msg, msglen);
18931ae08745Sheppo 			return ((status == 0) ? ENOTSUP : status);
18941ae08745Sheppo 
18951ae08745Sheppo 		default:
18961ae08745Sheppo 			ASSERT("Unsupported transfer mode");
18973af08d82Slm66018 			PR0("Unsupported transfer mode");
18981ae08745Sheppo 			return (ENOTSUP);
18991ae08745Sheppo 		}
19001ae08745Sheppo 
19011ae08745Sheppo 	default:
19021ae08745Sheppo 		ASSERT("Invalid client connection state");
19033af08d82Slm66018 		PR0("Invalid client connection state");
19041ae08745Sheppo 		return (ENOTSUP);
19051ae08745Sheppo 	}
19061ae08745Sheppo }
19071ae08745Sheppo 
1908d10e4ef2Snarayan static int
19093af08d82Slm66018 vd_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
19101ae08745Sheppo {
19111ae08745Sheppo 	int		status;
19121ae08745Sheppo 	boolean_t	reset_ldc = B_FALSE;
19131ae08745Sheppo 
19141ae08745Sheppo 
19151ae08745Sheppo 	/*
19161ae08745Sheppo 	 * Check that the message is at least big enough for a "tag", so that
19171ae08745Sheppo 	 * message processing can proceed based on tag-specified message type
19181ae08745Sheppo 	 */
19191ae08745Sheppo 	if (msglen < sizeof (vio_msg_tag_t)) {
19203af08d82Slm66018 		PR0("Received short (%lu-byte) message", msglen);
19211ae08745Sheppo 		/* Can't "nack" short message, so drop the big hammer */
19223af08d82Slm66018 		PR0("initiating full reset");
1923d10e4ef2Snarayan 		vd_need_reset(vd, B_TRUE);
1924d10e4ef2Snarayan 		return (EBADMSG);
19251ae08745Sheppo 	}
19261ae08745Sheppo 
19271ae08745Sheppo 	/*
19281ae08745Sheppo 	 * Process the message
19291ae08745Sheppo 	 */
19303af08d82Slm66018 	switch (status = vd_do_process_msg(vd, msg, msglen)) {
19311ae08745Sheppo 	case 0:
19321ae08745Sheppo 		/* "ack" valid, successfully-processed messages */
19331ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
19341ae08745Sheppo 		break;
19351ae08745Sheppo 
1936d10e4ef2Snarayan 	case EINPROGRESS:
1937d10e4ef2Snarayan 		/* The completion handler will "ack" or "nack" the message */
1938d10e4ef2Snarayan 		return (EINPROGRESS);
19391ae08745Sheppo 	case ENOMSG:
19403af08d82Slm66018 		PR0("Received unexpected message");
19411ae08745Sheppo 		_NOTE(FALLTHROUGH);
19421ae08745Sheppo 	case EBADMSG:
19431ae08745Sheppo 	case ENOTSUP:
19441ae08745Sheppo 		/* "nack" invalid messages */
19451ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
19461ae08745Sheppo 		break;
19471ae08745Sheppo 
19481ae08745Sheppo 	default:
19491ae08745Sheppo 		/* "nack" failed messages */
19501ae08745Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
19511ae08745Sheppo 		/* An LDC error probably occurred, so try resetting it */
19521ae08745Sheppo 		reset_ldc = B_TRUE;
19531ae08745Sheppo 		break;
19541ae08745Sheppo 	}
19551ae08745Sheppo 
19563af08d82Slm66018 	PR1("\tResulting in state %d (%s)", vd->state,
19573af08d82Slm66018 		vd_decode_state(vd->state));
19583af08d82Slm66018 
1959d10e4ef2Snarayan 	/* Send the "ack" or "nack" to the client */
19601ae08745Sheppo 	PR1("Sending %s",
19611ae08745Sheppo 	    (msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK");
19621ae08745Sheppo 	if (send_msg(vd->ldc_handle, msg, msglen) != 0)
19631ae08745Sheppo 		reset_ldc = B_TRUE;
19641ae08745Sheppo 
1965d10e4ef2Snarayan 	/* Arrange to reset the connection for nack'ed or failed messages */
19663af08d82Slm66018 	if ((status != 0) || reset_ldc) {
19673af08d82Slm66018 		PR0("initiating %s reset",
19683af08d82Slm66018 		    (reset_ldc) ? "full" : "soft");
1969d10e4ef2Snarayan 		vd_need_reset(vd, reset_ldc);
19703af08d82Slm66018 	}
1971d10e4ef2Snarayan 
1972d10e4ef2Snarayan 	return (status);
1973d10e4ef2Snarayan }
1974d10e4ef2Snarayan 
1975d10e4ef2Snarayan static boolean_t
1976d10e4ef2Snarayan vd_enabled(vd_t *vd)
1977d10e4ef2Snarayan {
1978d10e4ef2Snarayan 	boolean_t	enabled;
1979d10e4ef2Snarayan 
1980d10e4ef2Snarayan 
1981d10e4ef2Snarayan 	mutex_enter(&vd->lock);
1982d10e4ef2Snarayan 	enabled = vd->enabled;
1983d10e4ef2Snarayan 	mutex_exit(&vd->lock);
1984d10e4ef2Snarayan 	return (enabled);
19851ae08745Sheppo }
19861ae08745Sheppo 
19871ae08745Sheppo static void
19880a55fbb7Slm66018 vd_recv_msg(void *arg)
19891ae08745Sheppo {
19901ae08745Sheppo 	vd_t	*vd = (vd_t *)arg;
19913af08d82Slm66018 	int	rv = 0, status = 0;
19921ae08745Sheppo 
19931ae08745Sheppo 	ASSERT(vd != NULL);
19943af08d82Slm66018 
1995d10e4ef2Snarayan 	PR2("New task to receive incoming message(s)");
19963af08d82Slm66018 
19973af08d82Slm66018 
1998d10e4ef2Snarayan 	while (vd_enabled(vd) && status == 0) {
1999d10e4ef2Snarayan 		size_t		msglen, msgsize;
20003af08d82Slm66018 		ldc_status_t	lstatus;
2001d10e4ef2Snarayan 
20020a55fbb7Slm66018 		/*
2003d10e4ef2Snarayan 		 * Receive and process a message
20040a55fbb7Slm66018 		 */
2005d10e4ef2Snarayan 		vd_reset_if_needed(vd);	/* can change vd->max_msglen */
20063af08d82Slm66018 
20073af08d82Slm66018 		/*
20083af08d82Slm66018 		 * check if channel is UP - else break out of loop
20093af08d82Slm66018 		 */
20103af08d82Slm66018 		status = ldc_status(vd->ldc_handle, &lstatus);
20113af08d82Slm66018 		if (lstatus != LDC_UP) {
20123af08d82Slm66018 			PR0("channel not up (status=%d), exiting recv loop\n",
20133af08d82Slm66018 			    lstatus);
20143af08d82Slm66018 			break;
20153af08d82Slm66018 		}
20163af08d82Slm66018 
20173af08d82Slm66018 		ASSERT(vd->max_msglen != 0);
20183af08d82Slm66018 
2019d10e4ef2Snarayan 		msgsize = vd->max_msglen; /* stable copy for alloc/free */
20203af08d82Slm66018 		msglen	= msgsize;	  /* actual len after recv_msg() */
20213af08d82Slm66018 
20223af08d82Slm66018 		status = recv_msg(vd->ldc_handle, vd->vio_msgp, &msglen);
20233af08d82Slm66018 		switch (status) {
20243af08d82Slm66018 		case 0:
20253af08d82Slm66018 			rv = vd_process_msg(vd, (vio_msg_t *)vd->vio_msgp,
20263af08d82Slm66018 				msglen);
20273af08d82Slm66018 			/* check if max_msglen changed */
20283af08d82Slm66018 			if (msgsize != vd->max_msglen) {
20293af08d82Slm66018 				PR0("max_msglen changed 0x%lx to 0x%lx bytes\n",
20303af08d82Slm66018 				    msgsize, vd->max_msglen);
20313af08d82Slm66018 				kmem_free(vd->vio_msgp, msgsize);
20323af08d82Slm66018 				vd->vio_msgp =
20333af08d82Slm66018 					kmem_alloc(vd->max_msglen, KM_SLEEP);
20343af08d82Slm66018 			}
20353af08d82Slm66018 			if (rv == EINPROGRESS)
20363af08d82Slm66018 				continue;
20373af08d82Slm66018 			break;
20383af08d82Slm66018 
20393af08d82Slm66018 		case ENOMSG:
20403af08d82Slm66018 			break;
20413af08d82Slm66018 
20423af08d82Slm66018 		case ECONNRESET:
20433af08d82Slm66018 			PR0("initiating soft reset (ECONNRESET)\n");
20443af08d82Slm66018 			vd_need_reset(vd, B_FALSE);
20453af08d82Slm66018 			status = 0;
20463af08d82Slm66018 			break;
20473af08d82Slm66018 
20483af08d82Slm66018 		default:
2049d10e4ef2Snarayan 			/* Probably an LDC failure; arrange to reset it */
20503af08d82Slm66018 			PR0("initiating full reset (status=0x%x)", status);
2051d10e4ef2Snarayan 			vd_need_reset(vd, B_TRUE);
20523af08d82Slm66018 			break;
20530a55fbb7Slm66018 		}
20541ae08745Sheppo 	}
20553af08d82Slm66018 
2056d10e4ef2Snarayan 	PR2("Task finished");
20570a55fbb7Slm66018 }
20580a55fbb7Slm66018 
20590a55fbb7Slm66018 static uint_t
20601ae08745Sheppo vd_handle_ldc_events(uint64_t event, caddr_t arg)
20611ae08745Sheppo {
20621ae08745Sheppo 	vd_t	*vd = (vd_t *)(void *)arg;
20633af08d82Slm66018 	int	status;
20641ae08745Sheppo 
20651ae08745Sheppo 	ASSERT(vd != NULL);
2066d10e4ef2Snarayan 
2067d10e4ef2Snarayan 	if (!vd_enabled(vd))
2068d10e4ef2Snarayan 		return (LDC_SUCCESS);
2069d10e4ef2Snarayan 
20703af08d82Slm66018 	if (event & LDC_EVT_DOWN) {
2071*34683adeSsg70180 		PR0("LDC_EVT_DOWN: LDC channel went down");
20723af08d82Slm66018 
20733af08d82Slm66018 		vd_need_reset(vd, B_TRUE);
20743af08d82Slm66018 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
20753af08d82Slm66018 		    DDI_SLEEP);
20763af08d82Slm66018 		if (status == DDI_FAILURE) {
20773af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
20783af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
20793af08d82Slm66018 		}
20803af08d82Slm66018 	}
20813af08d82Slm66018 
2082d10e4ef2Snarayan 	if (event & LDC_EVT_RESET) {
20833af08d82Slm66018 		PR0("LDC_EVT_RESET: LDC channel was reset");
20843af08d82Slm66018 
20853af08d82Slm66018 		if (vd->state != VD_STATE_INIT) {
20863af08d82Slm66018 			PR0("scheduling full reset");
20873af08d82Slm66018 			vd_need_reset(vd, B_FALSE);
20883af08d82Slm66018 			status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
20893af08d82Slm66018 			    vd, DDI_SLEEP);
20903af08d82Slm66018 			if (status == DDI_FAILURE) {
20913af08d82Slm66018 				PR0("cannot schedule task to recv msg\n");
20923af08d82Slm66018 				vd_need_reset(vd, B_TRUE);
20933af08d82Slm66018 			}
20943af08d82Slm66018 
20953af08d82Slm66018 		} else {
20963af08d82Slm66018 			PR0("channel already reset, ignoring...\n");
20973af08d82Slm66018 			PR0("doing ldc up...\n");
20983af08d82Slm66018 			(void) ldc_up(vd->ldc_handle);
20993af08d82Slm66018 		}
21003af08d82Slm66018 
2101d10e4ef2Snarayan 		return (LDC_SUCCESS);
2102d10e4ef2Snarayan 	}
2103d10e4ef2Snarayan 
2104d10e4ef2Snarayan 	if (event & LDC_EVT_UP) {
21053af08d82Slm66018 		PR0("EVT_UP: LDC is up\nResetting client connection state");
21063af08d82Slm66018 		PR0("initiating soft reset");
2107d10e4ef2Snarayan 		vd_need_reset(vd, B_FALSE);
21083af08d82Slm66018 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
21093af08d82Slm66018 		    vd, DDI_SLEEP);
21103af08d82Slm66018 		if (status == DDI_FAILURE) {
21113af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
21123af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
21133af08d82Slm66018 			return (LDC_SUCCESS);
21143af08d82Slm66018 		}
2115d10e4ef2Snarayan 	}
2116d10e4ef2Snarayan 
2117d10e4ef2Snarayan 	if (event & LDC_EVT_READ) {
2118d10e4ef2Snarayan 		int	status;
2119d10e4ef2Snarayan 
2120d10e4ef2Snarayan 		PR1("New data available");
2121d10e4ef2Snarayan 		/* Queue a task to receive the new data */
2122d10e4ef2Snarayan 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
2123d10e4ef2Snarayan 		    DDI_SLEEP);
21243af08d82Slm66018 
21253af08d82Slm66018 		if (status == DDI_FAILURE) {
21263af08d82Slm66018 			PR0("cannot schedule task to recv msg\n");
21273af08d82Slm66018 			vd_need_reset(vd, B_TRUE);
21283af08d82Slm66018 		}
2129d10e4ef2Snarayan 	}
2130d10e4ef2Snarayan 
2131d10e4ef2Snarayan 	return (LDC_SUCCESS);
21321ae08745Sheppo }
21331ae08745Sheppo 
21341ae08745Sheppo static uint_t
21351ae08745Sheppo vds_check_for_vd(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
21361ae08745Sheppo {
21371ae08745Sheppo 	_NOTE(ARGUNUSED(key, val))
21381ae08745Sheppo 	(*((uint_t *)arg))++;
21391ae08745Sheppo 	return (MH_WALK_TERMINATE);
21401ae08745Sheppo }
21411ae08745Sheppo 
21421ae08745Sheppo 
21431ae08745Sheppo static int
21441ae08745Sheppo vds_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
21451ae08745Sheppo {
21461ae08745Sheppo 	uint_t	vd_present = 0;
21471ae08745Sheppo 	minor_t	instance;
21481ae08745Sheppo 	vds_t	*vds;
21491ae08745Sheppo 
21501ae08745Sheppo 
21511ae08745Sheppo 	switch (cmd) {
21521ae08745Sheppo 	case DDI_DETACH:
21531ae08745Sheppo 		/* the real work happens below */
21541ae08745Sheppo 		break;
21551ae08745Sheppo 	case DDI_SUSPEND:
2156d10e4ef2Snarayan 		PR0("No action required for DDI_SUSPEND");
21571ae08745Sheppo 		return (DDI_SUCCESS);
21581ae08745Sheppo 	default:
21593af08d82Slm66018 		PR0("Unrecognized \"cmd\"");
21601ae08745Sheppo 		return (DDI_FAILURE);
21611ae08745Sheppo 	}
21621ae08745Sheppo 
21631ae08745Sheppo 	ASSERT(cmd == DDI_DETACH);
21641ae08745Sheppo 	instance = ddi_get_instance(dip);
21651ae08745Sheppo 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
21663af08d82Slm66018 		PR0("Could not get state for instance %u", instance);
21671ae08745Sheppo 		ddi_soft_state_free(vds_state, instance);
21681ae08745Sheppo 		return (DDI_FAILURE);
21691ae08745Sheppo 	}
21701ae08745Sheppo 
21711ae08745Sheppo 	/* Do no detach when serving any vdisks */
21721ae08745Sheppo 	mod_hash_walk(vds->vd_table, vds_check_for_vd, &vd_present);
21731ae08745Sheppo 	if (vd_present) {
21741ae08745Sheppo 		PR0("Not detaching because serving vdisks");
21751ae08745Sheppo 		return (DDI_FAILURE);
21761ae08745Sheppo 	}
21771ae08745Sheppo 
21781ae08745Sheppo 	PR0("Detaching");
21791ae08745Sheppo 	if (vds->initialized & VDS_MDEG)
21801ae08745Sheppo 		(void) mdeg_unregister(vds->mdeg);
21811ae08745Sheppo 	if (vds->initialized & VDS_LDI)
21821ae08745Sheppo 		(void) ldi_ident_release(vds->ldi_ident);
21831ae08745Sheppo 	mod_hash_destroy_hash(vds->vd_table);
21841ae08745Sheppo 	ddi_soft_state_free(vds_state, instance);
21851ae08745Sheppo 	return (DDI_SUCCESS);
21861ae08745Sheppo }
21871ae08745Sheppo 
21881ae08745Sheppo static boolean_t
21891ae08745Sheppo is_pseudo_device(dev_info_t *dip)
21901ae08745Sheppo {
21911ae08745Sheppo 	dev_info_t	*parent, *root = ddi_root_node();
21921ae08745Sheppo 
21931ae08745Sheppo 
21941ae08745Sheppo 	for (parent = ddi_get_parent(dip); (parent != NULL) && (parent != root);
21951ae08745Sheppo 	    parent = ddi_get_parent(parent)) {
21961ae08745Sheppo 		if (strcmp(ddi_get_name(parent), DEVI_PSEUDO_NEXNAME) == 0)
21971ae08745Sheppo 			return (B_TRUE);
21981ae08745Sheppo 	}
21991ae08745Sheppo 
22001ae08745Sheppo 	return (B_FALSE);
22011ae08745Sheppo }
22021ae08745Sheppo 
22031ae08745Sheppo static int
22040a55fbb7Slm66018 vd_setup_full_disk(vd_t *vd)
22050a55fbb7Slm66018 {
22060a55fbb7Slm66018 	int		rval, status;
22070a55fbb7Slm66018 	major_t		major = getmajor(vd->dev[0]);
22080a55fbb7Slm66018 	minor_t		minor = getminor(vd->dev[0]) - VD_ENTIRE_DISK_SLICE;
22094bac2208Snarayan 	struct dk_minfo	dk_minfo;
22100a55fbb7Slm66018 
22114bac2208Snarayan 	/*
22124bac2208Snarayan 	 * At this point, vdisk_size is set to the size of partition 2 but
22134bac2208Snarayan 	 * this does not represent the size of the disk because partition 2
22144bac2208Snarayan 	 * may not cover the entire disk and its size does not include reserved
22154bac2208Snarayan 	 * blocks. So we update vdisk_size to be the size of the entire disk.
22164bac2208Snarayan 	 */
22174bac2208Snarayan 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCGMEDIAINFO,
22184bac2208Snarayan 	    (intptr_t)&dk_minfo, (vd_open_flags | FKIOCTL),
22194bac2208Snarayan 	    kcred, &rval)) != 0) {
2220*34683adeSsg70180 		PR0("ldi_ioctl(DKIOCGMEDIAINFO) returned errno %d",
22214bac2208Snarayan 		    status);
22220a55fbb7Slm66018 		return (status);
22230a55fbb7Slm66018 	}
22244bac2208Snarayan 	vd->vdisk_size = dk_minfo.dki_capacity;
22250a55fbb7Slm66018 
22260a55fbb7Slm66018 	/* Set full-disk parameters */
22270a55fbb7Slm66018 	vd->vdisk_type	= VD_DISK_TYPE_DISK;
22280a55fbb7Slm66018 	vd->nslices	= (sizeof (vd->dev))/(sizeof (vd->dev[0]));
22290a55fbb7Slm66018 
22300a55fbb7Slm66018 	/* Move dev number and LDI handle to entire-disk-slice array elements */
22310a55fbb7Slm66018 	vd->dev[VD_ENTIRE_DISK_SLICE]		= vd->dev[0];
22320a55fbb7Slm66018 	vd->dev[0]				= 0;
22330a55fbb7Slm66018 	vd->ldi_handle[VD_ENTIRE_DISK_SLICE]	= vd->ldi_handle[0];
22340a55fbb7Slm66018 	vd->ldi_handle[0]			= NULL;
22350a55fbb7Slm66018 
22360a55fbb7Slm66018 	/* Initialize device numbers for remaining slices and open them */
22370a55fbb7Slm66018 	for (int slice = 0; slice < vd->nslices; slice++) {
22380a55fbb7Slm66018 		/*
22390a55fbb7Slm66018 		 * Skip the entire-disk slice, as it's already open and its
22400a55fbb7Slm66018 		 * device known
22410a55fbb7Slm66018 		 */
22420a55fbb7Slm66018 		if (slice == VD_ENTIRE_DISK_SLICE)
22430a55fbb7Slm66018 			continue;
22440a55fbb7Slm66018 		ASSERT(vd->dev[slice] == 0);
22450a55fbb7Slm66018 		ASSERT(vd->ldi_handle[slice] == NULL);
22460a55fbb7Slm66018 
22470a55fbb7Slm66018 		/*
22480a55fbb7Slm66018 		 * Construct the device number for the current slice
22490a55fbb7Slm66018 		 */
22500a55fbb7Slm66018 		vd->dev[slice] = makedevice(major, (minor + slice));
22510a55fbb7Slm66018 
22520a55fbb7Slm66018 		/*
2253*34683adeSsg70180 		 * Open all slices of the disk to serve them to the client.
2254*34683adeSsg70180 		 * Slices are opened exclusively to prevent other threads or
2255*34683adeSsg70180 		 * processes in the service domain from performing I/O to
2256*34683adeSsg70180 		 * slices being accessed by a client.  Failure to open a slice
2257*34683adeSsg70180 		 * results in vds not serving this disk, as the client could
2258*34683adeSsg70180 		 * attempt (and should be able) to access any slice immediately.
2259*34683adeSsg70180 		 * Any slices successfully opened before a failure will get
2260*34683adeSsg70180 		 * closed by vds_destroy_vd() as a result of the error returned
2261*34683adeSsg70180 		 * by this function.
2262*34683adeSsg70180 		 *
2263*34683adeSsg70180 		 * We need to do the open with FNDELAY so that opening an empty
2264*34683adeSsg70180 		 * slice does not fail.
22650a55fbb7Slm66018 		 */
22660a55fbb7Slm66018 		PR0("Opening device major %u, minor %u = slice %u",
22670a55fbb7Slm66018 		    major, minor, slice);
22680a55fbb7Slm66018 		if ((status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK,
2269*34683adeSsg70180 		    vd_open_flags | FNDELAY, kcred, &vd->ldi_handle[slice],
22700a55fbb7Slm66018 		    vd->vds->ldi_ident)) != 0) {
2271*34683adeSsg70180 			PR0("ldi_open_by_dev() returned errno %d "
22720a55fbb7Slm66018 			    "for slice %u", status, slice);
22730a55fbb7Slm66018 			/* vds_destroy_vd() will close any open slices */
22740a55fbb7Slm66018 			return (status);
22750a55fbb7Slm66018 		}
22760a55fbb7Slm66018 	}
22770a55fbb7Slm66018 
22780a55fbb7Slm66018 	return (0);
22790a55fbb7Slm66018 }
22800a55fbb7Slm66018 
22810a55fbb7Slm66018 static int
22824bac2208Snarayan vd_setup_partition_efi(vd_t *vd)
22834bac2208Snarayan {
22844bac2208Snarayan 	efi_gpt_t *gpt;
22854bac2208Snarayan 	efi_gpe_t *gpe;
22864bac2208Snarayan 	struct uuid uuid = EFI_RESERVED;
22874bac2208Snarayan 	uint32_t crc;
22884bac2208Snarayan 	int length;
22894bac2208Snarayan 
22904bac2208Snarayan 	length = sizeof (efi_gpt_t) + sizeof (efi_gpe_t);
22914bac2208Snarayan 
22924bac2208Snarayan 	gpt = kmem_zalloc(length, KM_SLEEP);
22934bac2208Snarayan 	gpe = (efi_gpe_t *)(gpt + 1);
22944bac2208Snarayan 
22954bac2208Snarayan 	gpt->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
22964bac2208Snarayan 	gpt->efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
22974bac2208Snarayan 	gpt->efi_gpt_HeaderSize = LE_32(sizeof (efi_gpt_t));
22984bac2208Snarayan 	gpt->efi_gpt_FirstUsableLBA = LE_64(0ULL);
22994bac2208Snarayan 	gpt->efi_gpt_LastUsableLBA = LE_64(vd->vdisk_size - 1);
23004bac2208Snarayan 	gpt->efi_gpt_NumberOfPartitionEntries = LE_32(1);
23014bac2208Snarayan 	gpt->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (efi_gpe_t));
23024bac2208Snarayan 
23034bac2208Snarayan 	UUID_LE_CONVERT(gpe->efi_gpe_PartitionTypeGUID, uuid);
23044bac2208Snarayan 	gpe->efi_gpe_StartingLBA = gpt->efi_gpt_FirstUsableLBA;
23054bac2208Snarayan 	gpe->efi_gpe_EndingLBA = gpt->efi_gpt_LastUsableLBA;
23064bac2208Snarayan 
23074bac2208Snarayan 	CRC32(crc, gpe, sizeof (efi_gpe_t), -1U, crc32_table);
23084bac2208Snarayan 	gpt->efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
23094bac2208Snarayan 
23104bac2208Snarayan 	CRC32(crc, gpt, sizeof (efi_gpt_t), -1U, crc32_table);
23114bac2208Snarayan 	gpt->efi_gpt_HeaderCRC32 = LE_32(~crc);
23124bac2208Snarayan 
23134bac2208Snarayan 	vd->dk_efi.dki_lba = 0;
23144bac2208Snarayan 	vd->dk_efi.dki_length = length;
23154bac2208Snarayan 	vd->dk_efi.dki_data = gpt;
23164bac2208Snarayan 
23174bac2208Snarayan 	return (0);
23184bac2208Snarayan }
23194bac2208Snarayan 
23204bac2208Snarayan static int
2321e1ebb9ecSlm66018 vd_setup_vd(char *device_path, vd_t *vd)
23221ae08745Sheppo {
2323e1ebb9ecSlm66018 	int		rval, status;
23241ae08745Sheppo 	dev_info_t	*dip;
23251ae08745Sheppo 	struct dk_cinfo	dk_cinfo;
23261ae08745Sheppo 
23274bac2208Snarayan 	/*
23284bac2208Snarayan 	 * We need to open with FNDELAY so that opening an empty partition
23294bac2208Snarayan 	 * does not fail.
23304bac2208Snarayan 	 */
23314bac2208Snarayan 	if ((status = ldi_open_by_name(device_path, vd_open_flags | FNDELAY,
23324bac2208Snarayan 	    kcred, &vd->ldi_handle[0], vd->vds->ldi_ident)) != 0) {
2333e1ebb9ecSlm66018 		PRN("ldi_open_by_name(%s) = errno %d", device_path, status);
23340a55fbb7Slm66018 		return (status);
23350a55fbb7Slm66018 	}
23360a55fbb7Slm66018 
23374bac2208Snarayan 	/*
23384bac2208Snarayan 	 * nslices must be updated now so that vds_destroy_vd() will close
23394bac2208Snarayan 	 * the slice we have just opened in case of an error.
23404bac2208Snarayan 	 */
23414bac2208Snarayan 	vd->nslices = 1;
23424bac2208Snarayan 
2343e1ebb9ecSlm66018 	/* Get device number and size of backing device */
23440a55fbb7Slm66018 	if ((status = ldi_get_dev(vd->ldi_handle[0], &vd->dev[0])) != 0) {
23451ae08745Sheppo 		PRN("ldi_get_dev() returned errno %d for %s",
2346e1ebb9ecSlm66018 		    status, device_path);
23471ae08745Sheppo 		return (status);
23481ae08745Sheppo 	}
23490a55fbb7Slm66018 	if (ldi_get_size(vd->ldi_handle[0], &vd->vdisk_size) != DDI_SUCCESS) {
2350e1ebb9ecSlm66018 		PRN("ldi_get_size() failed for %s", device_path);
23511ae08745Sheppo 		return (EIO);
23521ae08745Sheppo 	}
2353e1ebb9ecSlm66018 	vd->vdisk_size = lbtodb(vd->vdisk_size);	/* convert to blocks */
23541ae08745Sheppo 
2355e1ebb9ecSlm66018 	/* Verify backing device supports dk_cinfo, dk_geom, and vtoc */
2356e1ebb9ecSlm66018 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO,
2357e1ebb9ecSlm66018 		    (intptr_t)&dk_cinfo, (vd_open_flags | FKIOCTL), kcred,
2358e1ebb9ecSlm66018 		    &rval)) != 0) {
2359e1ebb9ecSlm66018 		PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
2360e1ebb9ecSlm66018 		    status, device_path);
2361e1ebb9ecSlm66018 		return (status);
2362e1ebb9ecSlm66018 	}
2363e1ebb9ecSlm66018 	if (dk_cinfo.dki_partition >= V_NUMPAR) {
2364e1ebb9ecSlm66018 		PRN("slice %u >= maximum slice %u for %s",
2365e1ebb9ecSlm66018 		    dk_cinfo.dki_partition, V_NUMPAR, device_path);
2366e1ebb9ecSlm66018 		return (EIO);
2367e1ebb9ecSlm66018 	}
23684bac2208Snarayan 
23694bac2208Snarayan 	status = vd_read_vtoc(vd->ldi_handle[0], &vd->vtoc, &vd->vdisk_label);
23704bac2208Snarayan 
23714bac2208Snarayan 	if (status != 0) {
23724bac2208Snarayan 		PRN("vd_read_vtoc returned errno %d for %s",
2373e1ebb9ecSlm66018 		    status, device_path);
2374e1ebb9ecSlm66018 		return (status);
2375e1ebb9ecSlm66018 	}
23764bac2208Snarayan 
23774bac2208Snarayan 	if (vd->vdisk_label == VD_DISK_LABEL_VTOC &&
23784bac2208Snarayan 	    (status = ldi_ioctl(vd->ldi_handle[0], DKIOCGGEOM,
23794bac2208Snarayan 	    (intptr_t)&vd->dk_geom, (vd_open_flags | FKIOCTL),
23804bac2208Snarayan 	    kcred, &rval)) != 0) {
23814bac2208Snarayan 		    PRN("ldi_ioctl(DKIOCGEOM) returned errno %d for %s",
2382e1ebb9ecSlm66018 			status, device_path);
2383e1ebb9ecSlm66018 		    return (status);
2384e1ebb9ecSlm66018 	}
2385e1ebb9ecSlm66018 
2386e1ebb9ecSlm66018 	/* Store the device's max transfer size for return to the client */
2387e1ebb9ecSlm66018 	vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
2388e1ebb9ecSlm66018 
2389e1ebb9ecSlm66018 
2390e1ebb9ecSlm66018 	/* Determine if backing device is a pseudo device */
23911ae08745Sheppo 	if ((dip = ddi_hold_devi_by_instance(getmajor(vd->dev[0]),
23921ae08745Sheppo 		    dev_to_instance(vd->dev[0]), 0))  == NULL) {
2393e1ebb9ecSlm66018 		PRN("%s is no longer accessible", device_path);
23941ae08745Sheppo 		return (EIO);
23951ae08745Sheppo 	}
23961ae08745Sheppo 	vd->pseudo = is_pseudo_device(dip);
23971ae08745Sheppo 	ddi_release_devi(dip);
23981ae08745Sheppo 	if (vd->pseudo) {
23991ae08745Sheppo 		vd->vdisk_type	= VD_DISK_TYPE_SLICE;
24001ae08745Sheppo 		vd->nslices	= 1;
24011ae08745Sheppo 		return (0);	/* ...and we're done */
24021ae08745Sheppo 	}
24031ae08745Sheppo 
24041ae08745Sheppo 
24050a55fbb7Slm66018 	/* If slice is entire-disk slice, initialize for full disk */
24060a55fbb7Slm66018 	if (dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE)
24070a55fbb7Slm66018 		return (vd_setup_full_disk(vd));
24081ae08745Sheppo 
24090a55fbb7Slm66018 
2410e1ebb9ecSlm66018 	/* Otherwise, we have a non-entire slice of a device */
24111ae08745Sheppo 	vd->vdisk_type	= VD_DISK_TYPE_SLICE;
24121ae08745Sheppo 	vd->nslices	= 1;
24131ae08745Sheppo 
24144bac2208Snarayan 	if (vd->vdisk_label == VD_DISK_LABEL_EFI) {
24154bac2208Snarayan 		status = vd_setup_partition_efi(vd);
24164bac2208Snarayan 		return (status);
24174bac2208Snarayan 	}
24181ae08745Sheppo 
2419e1ebb9ecSlm66018 	/* Initialize dk_geom structure for single-slice device */
24201ae08745Sheppo 	if (vd->dk_geom.dkg_nsect == 0) {
24213af08d82Slm66018 		PR0("%s geometry claims 0 sectors per track", device_path);
24221ae08745Sheppo 		return (EIO);
24231ae08745Sheppo 	}
24241ae08745Sheppo 	if (vd->dk_geom.dkg_nhead == 0) {
24253af08d82Slm66018 		PR0("%s geometry claims 0 heads", device_path);
24261ae08745Sheppo 		return (EIO);
24271ae08745Sheppo 	}
24281ae08745Sheppo 	vd->dk_geom.dkg_ncyl =
2429e1ebb9ecSlm66018 	    vd->vdisk_size/vd->dk_geom.dkg_nsect/vd->dk_geom.dkg_nhead;
24301ae08745Sheppo 	vd->dk_geom.dkg_acyl = 0;
24311ae08745Sheppo 	vd->dk_geom.dkg_pcyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl;
24321ae08745Sheppo 
24331ae08745Sheppo 
2434e1ebb9ecSlm66018 	/* Initialize vtoc structure for single-slice device */
24351ae08745Sheppo 	bcopy(VD_VOLUME_NAME, vd->vtoc.v_volume,
24361ae08745Sheppo 	    MIN(sizeof (VD_VOLUME_NAME), sizeof (vd->vtoc.v_volume)));
24371ae08745Sheppo 	bzero(vd->vtoc.v_part, sizeof (vd->vtoc.v_part));
24381ae08745Sheppo 	vd->vtoc.v_nparts = 1;
24391ae08745Sheppo 	vd->vtoc.v_part[0].p_tag = V_UNASSIGNED;
24401ae08745Sheppo 	vd->vtoc.v_part[0].p_flag = 0;
24411ae08745Sheppo 	vd->vtoc.v_part[0].p_start = 0;
2442e1ebb9ecSlm66018 	vd->vtoc.v_part[0].p_size = vd->vdisk_size;
24431ae08745Sheppo 	bcopy(VD_ASCIILABEL, vd->vtoc.v_asciilabel,
24441ae08745Sheppo 	    MIN(sizeof (VD_ASCIILABEL), sizeof (vd->vtoc.v_asciilabel)));
24451ae08745Sheppo 
24461ae08745Sheppo 
24471ae08745Sheppo 	return (0);
24481ae08745Sheppo }
24491ae08745Sheppo 
24501ae08745Sheppo static int
2451e1ebb9ecSlm66018 vds_do_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t ldc_id,
24521ae08745Sheppo     vd_t **vdp)
24531ae08745Sheppo {
24541ae08745Sheppo 	char			tq_name[TASKQ_NAMELEN];
24550a55fbb7Slm66018 	int			status;
24561ae08745Sheppo 	ddi_iblock_cookie_t	iblock = NULL;
24571ae08745Sheppo 	ldc_attr_t		ldc_attr;
24581ae08745Sheppo 	vd_t			*vd;
24591ae08745Sheppo 
24601ae08745Sheppo 
24611ae08745Sheppo 	ASSERT(vds != NULL);
2462e1ebb9ecSlm66018 	ASSERT(device_path != NULL);
24631ae08745Sheppo 	ASSERT(vdp != NULL);
2464e1ebb9ecSlm66018 	PR0("Adding vdisk for %s", device_path);
24651ae08745Sheppo 
24661ae08745Sheppo 	if ((vd = kmem_zalloc(sizeof (*vd), KM_NOSLEEP)) == NULL) {
24671ae08745Sheppo 		PRN("No memory for virtual disk");
24681ae08745Sheppo 		return (EAGAIN);
24691ae08745Sheppo 	}
24701ae08745Sheppo 	*vdp = vd;	/* assign here so vds_destroy_vd() can cleanup later */
24711ae08745Sheppo 	vd->vds = vds;
24721ae08745Sheppo 
24731ae08745Sheppo 
24740a55fbb7Slm66018 	/* Open vdisk and initialize parameters */
2475e1ebb9ecSlm66018 	if ((status = vd_setup_vd(device_path, vd)) != 0)
24761ae08745Sheppo 		return (status);
24771ae08745Sheppo 	ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
24781ae08745Sheppo 	PR0("vdisk_type = %s, pseudo = %s, nslices = %u",
24791ae08745Sheppo 	    ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
24801ae08745Sheppo 	    (vd->pseudo ? "yes" : "no"), vd->nslices);
24811ae08745Sheppo 
24821ae08745Sheppo 
24831ae08745Sheppo 	/* Initialize locking */
24841ae08745Sheppo 	if (ddi_get_soft_iblock_cookie(vds->dip, DDI_SOFTINT_MED,
24851ae08745Sheppo 		&iblock) != DDI_SUCCESS) {
24861ae08745Sheppo 		PRN("Could not get iblock cookie.");
24871ae08745Sheppo 		return (EIO);
24881ae08745Sheppo 	}
24891ae08745Sheppo 
24901ae08745Sheppo 	mutex_init(&vd->lock, NULL, MUTEX_DRIVER, iblock);
24911ae08745Sheppo 	vd->initialized |= VD_LOCKING;
24921ae08745Sheppo 
24931ae08745Sheppo 
2494d10e4ef2Snarayan 	/* Create start and completion task queues for the vdisk */
2495d10e4ef2Snarayan 	(void) snprintf(tq_name, sizeof (tq_name), "vd_startq%lu", id);
24961ae08745Sheppo 	PR1("tq_name = %s", tq_name);
2497d10e4ef2Snarayan 	if ((vd->startq = ddi_taskq_create(vds->dip, tq_name, 1,
24981ae08745Sheppo 		    TASKQ_DEFAULTPRI, 0)) == NULL) {
24991ae08745Sheppo 		PRN("Could not create task queue");
25001ae08745Sheppo 		return (EIO);
25011ae08745Sheppo 	}
2502d10e4ef2Snarayan 	(void) snprintf(tq_name, sizeof (tq_name), "vd_completionq%lu", id);
2503d10e4ef2Snarayan 	PR1("tq_name = %s", tq_name);
2504d10e4ef2Snarayan 	if ((vd->completionq = ddi_taskq_create(vds->dip, tq_name, 1,
2505d10e4ef2Snarayan 		    TASKQ_DEFAULTPRI, 0)) == NULL) {
2506d10e4ef2Snarayan 		PRN("Could not create task queue");
2507d10e4ef2Snarayan 		return (EIO);
2508d10e4ef2Snarayan 	}
2509d10e4ef2Snarayan 	vd->enabled = 1;	/* before callback can dispatch to startq */
25101ae08745Sheppo 
25111ae08745Sheppo 
25121ae08745Sheppo 	/* Bring up LDC */
25131ae08745Sheppo 	ldc_attr.devclass	= LDC_DEV_BLK_SVC;
25141ae08745Sheppo 	ldc_attr.instance	= ddi_get_instance(vds->dip);
25151ae08745Sheppo 	ldc_attr.mode		= LDC_MODE_UNRELIABLE;
2516e1ebb9ecSlm66018 	ldc_attr.mtu		= VD_LDC_MTU;
25171ae08745Sheppo 	if ((status = ldc_init(ldc_id, &ldc_attr, &vd->ldc_handle)) != 0) {
25183af08d82Slm66018 		PR0("ldc_init(%lu) = errno %d", ldc_id, status);
25191ae08745Sheppo 		return (status);
25201ae08745Sheppo 	}
25211ae08745Sheppo 	vd->initialized |= VD_LDC;
25221ae08745Sheppo 
25231ae08745Sheppo 	if ((status = ldc_reg_callback(vd->ldc_handle, vd_handle_ldc_events,
25241ae08745Sheppo 		(caddr_t)vd)) != 0) {
25253af08d82Slm66018 		PR0("ldc_reg_callback() returned errno %d", status);
25261ae08745Sheppo 		return (status);
25271ae08745Sheppo 	}
25281ae08745Sheppo 
25291ae08745Sheppo 	if ((status = ldc_open(vd->ldc_handle)) != 0) {
25303af08d82Slm66018 		PR0("ldc_open() returned errno %d", status);
25311ae08745Sheppo 		return (status);
25321ae08745Sheppo 	}
25331ae08745Sheppo 
25343af08d82Slm66018 	if ((status = ldc_up(vd->ldc_handle)) != 0) {
2535*34683adeSsg70180 		PR0("ldc_up() returned errno %d", status);
25363af08d82Slm66018 	}
25373af08d82Slm66018 
25384bac2208Snarayan 	/* Allocate the inband task memory handle */
25394bac2208Snarayan 	status = ldc_mem_alloc_handle(vd->ldc_handle, &(vd->inband_task.mhdl));
25404bac2208Snarayan 	if (status) {
2541*34683adeSsg70180 		PR0("ldc_mem_alloc_handle() returned err %d ", status);
25424bac2208Snarayan 		return (ENXIO);
25434bac2208Snarayan 	}
25441ae08745Sheppo 
25451ae08745Sheppo 	/* Add the successfully-initialized vdisk to the server's table */
25461ae08745Sheppo 	if (mod_hash_insert(vds->vd_table, (mod_hash_key_t)id, vd) != 0) {
25471ae08745Sheppo 		PRN("Error adding vdisk ID %lu to table", id);
25481ae08745Sheppo 		return (EIO);
25491ae08745Sheppo 	}
25501ae08745Sheppo 
25513af08d82Slm66018 	/* Allocate the staging buffer */
25523af08d82Slm66018 	vd->max_msglen	= sizeof (vio_msg_t);	/* baseline vio message size */
25533af08d82Slm66018 	vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
25543af08d82Slm66018 
25553af08d82Slm66018 	/* store initial state */
25563af08d82Slm66018 	vd->state = VD_STATE_INIT;
25573af08d82Slm66018 
25581ae08745Sheppo 	return (0);
25591ae08745Sheppo }
25601ae08745Sheppo 
25613af08d82Slm66018 static void
25623af08d82Slm66018 vd_free_dring_task(vd_t *vdp)
25633af08d82Slm66018 {
25643af08d82Slm66018 	if (vdp->dring_task != NULL) {
25653af08d82Slm66018 		ASSERT(vdp->dring_len != 0);
25663af08d82Slm66018 		/* Free all dring_task memory handles */
25673af08d82Slm66018 		for (int i = 0; i < vdp->dring_len; i++) {
25683af08d82Slm66018 			(void) ldc_mem_free_handle(vdp->dring_task[i].mhdl);
25693af08d82Slm66018 			kmem_free(vdp->dring_task[i].msg, vdp->max_msglen);
25703af08d82Slm66018 			vdp->dring_task[i].msg = NULL;
25713af08d82Slm66018 		}
25723af08d82Slm66018 		kmem_free(vdp->dring_task,
25733af08d82Slm66018 		    (sizeof (*vdp->dring_task)) * vdp->dring_len);
25743af08d82Slm66018 		vdp->dring_task = NULL;
25753af08d82Slm66018 	}
25763af08d82Slm66018 }
25773af08d82Slm66018 
25781ae08745Sheppo /*
25791ae08745Sheppo  * Destroy the state associated with a virtual disk
25801ae08745Sheppo  */
25811ae08745Sheppo static void
25821ae08745Sheppo vds_destroy_vd(void *arg)
25831ae08745Sheppo {
25841ae08745Sheppo 	vd_t	*vd = (vd_t *)arg;
2585*34683adeSsg70180 	int	retry = 0, rv;
25861ae08745Sheppo 
25871ae08745Sheppo 	if (vd == NULL)
25881ae08745Sheppo 		return;
25891ae08745Sheppo 
2590d10e4ef2Snarayan 	PR0("Destroying vdisk state");
2591d10e4ef2Snarayan 
25924bac2208Snarayan 	if (vd->dk_efi.dki_data != NULL)
25934bac2208Snarayan 		kmem_free(vd->dk_efi.dki_data, vd->dk_efi.dki_length);
25944bac2208Snarayan 
25951ae08745Sheppo 	/* Disable queuing requests for the vdisk */
25961ae08745Sheppo 	if (vd->initialized & VD_LOCKING) {
25971ae08745Sheppo 		mutex_enter(&vd->lock);
25981ae08745Sheppo 		vd->enabled = 0;
25991ae08745Sheppo 		mutex_exit(&vd->lock);
26001ae08745Sheppo 	}
26011ae08745Sheppo 
2602d10e4ef2Snarayan 	/* Drain and destroy start queue (*before* destroying completionq) */
2603d10e4ef2Snarayan 	if (vd->startq != NULL)
2604d10e4ef2Snarayan 		ddi_taskq_destroy(vd->startq);	/* waits for queued tasks */
2605d10e4ef2Snarayan 
2606d10e4ef2Snarayan 	/* Drain and destroy completion queue (*before* shutting down LDC) */
2607d10e4ef2Snarayan 	if (vd->completionq != NULL)
2608d10e4ef2Snarayan 		ddi_taskq_destroy(vd->completionq);	/* waits for tasks */
2609d10e4ef2Snarayan 
26103af08d82Slm66018 	vd_free_dring_task(vd);
26113af08d82Slm66018 
2612*34683adeSsg70180 	/* Free the inband task memory handle */
2613*34683adeSsg70180 	(void) ldc_mem_free_handle(vd->inband_task.mhdl);
2614*34683adeSsg70180 
2615*34683adeSsg70180 	/* Shut down LDC */
2616*34683adeSsg70180 	if (vd->initialized & VD_LDC) {
2617*34683adeSsg70180 		/* unmap the dring */
2618*34683adeSsg70180 		if (vd->initialized & VD_DRING)
2619*34683adeSsg70180 			(void) ldc_mem_dring_unmap(vd->dring_handle);
2620*34683adeSsg70180 
2621*34683adeSsg70180 		/* close LDC channel - retry on EAGAIN */
2622*34683adeSsg70180 		while ((rv = ldc_close(vd->ldc_handle)) == EAGAIN) {
2623*34683adeSsg70180 			if (++retry > vds_ldc_retries) {
2624*34683adeSsg70180 				PR0("Timed out closing channel");
2625*34683adeSsg70180 				break;
2626*34683adeSsg70180 			}
2627*34683adeSsg70180 			drv_usecwait(vds_ldc_delay);
2628*34683adeSsg70180 		}
2629*34683adeSsg70180 		if (rv == 0) {
2630*34683adeSsg70180 			(void) ldc_unreg_callback(vd->ldc_handle);
2631*34683adeSsg70180 			(void) ldc_fini(vd->ldc_handle);
2632*34683adeSsg70180 		} else {
2633*34683adeSsg70180 			/*
2634*34683adeSsg70180 			 * Closing the LDC channel has failed. Ideally we should
2635*34683adeSsg70180 			 * fail here but there is no Zeus level infrastructure
2636*34683adeSsg70180 			 * to handle this. The MD has already been changed and
2637*34683adeSsg70180 			 * we have to do the close. So we try to do as much
2638*34683adeSsg70180 			 * clean up as we can.
2639*34683adeSsg70180 			 */
2640*34683adeSsg70180 			(void) ldc_set_cb_mode(vd->ldc_handle, LDC_CB_DISABLE);
2641*34683adeSsg70180 			while (ldc_unreg_callback(vd->ldc_handle) == EAGAIN)
2642*34683adeSsg70180 				drv_usecwait(vds_ldc_delay);
2643*34683adeSsg70180 		}
2644*34683adeSsg70180 	}
2645*34683adeSsg70180 
26463af08d82Slm66018 	/* Free the staging buffer for msgs */
26473af08d82Slm66018 	if (vd->vio_msgp != NULL) {
26483af08d82Slm66018 		kmem_free(vd->vio_msgp, vd->max_msglen);
26493af08d82Slm66018 		vd->vio_msgp = NULL;
26503af08d82Slm66018 	}
26513af08d82Slm66018 
26523af08d82Slm66018 	/* Free the inband message buffer */
26533af08d82Slm66018 	if (vd->inband_task.msg != NULL) {
26543af08d82Slm66018 		kmem_free(vd->inband_task.msg, vd->max_msglen);
26553af08d82Slm66018 		vd->inband_task.msg = NULL;
2656d10e4ef2Snarayan 	}
26571ae08745Sheppo 
26581ae08745Sheppo 	/* Close any open backing-device slices */
26591ae08745Sheppo 	for (uint_t slice = 0; slice < vd->nslices; slice++) {
26601ae08745Sheppo 		if (vd->ldi_handle[slice] != NULL) {
26611ae08745Sheppo 			PR0("Closing slice %u", slice);
26621ae08745Sheppo 			(void) ldi_close(vd->ldi_handle[slice],
26634bac2208Snarayan 			    vd_open_flags | FNDELAY, kcred);
26641ae08745Sheppo 		}
26651ae08745Sheppo 	}
26661ae08745Sheppo 
26671ae08745Sheppo 	/* Free lock */
26681ae08745Sheppo 	if (vd->initialized & VD_LOCKING)
26691ae08745Sheppo 		mutex_destroy(&vd->lock);
26701ae08745Sheppo 
26711ae08745Sheppo 	/* Finally, free the vdisk structure itself */
26721ae08745Sheppo 	kmem_free(vd, sizeof (*vd));
26731ae08745Sheppo }
26741ae08745Sheppo 
26751ae08745Sheppo static int
2676e1ebb9ecSlm66018 vds_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t ldc_id)
26771ae08745Sheppo {
26781ae08745Sheppo 	int	status;
26791ae08745Sheppo 	vd_t	*vd = NULL;
26801ae08745Sheppo 
26811ae08745Sheppo 
2682e1ebb9ecSlm66018 	if ((status = vds_do_init_vd(vds, id, device_path, ldc_id, &vd)) != 0)
26831ae08745Sheppo 		vds_destroy_vd(vd);
26841ae08745Sheppo 
26851ae08745Sheppo 	return (status);
26861ae08745Sheppo }
26871ae08745Sheppo 
26881ae08745Sheppo static int
26891ae08745Sheppo vds_do_get_ldc_id(md_t *md, mde_cookie_t vd_node, mde_cookie_t *channel,
26901ae08745Sheppo     uint64_t *ldc_id)
26911ae08745Sheppo {
26921ae08745Sheppo 	int	num_channels;
26931ae08745Sheppo 
26941ae08745Sheppo 
26951ae08745Sheppo 	/* Look for channel endpoint child(ren) of the vdisk MD node */
26961ae08745Sheppo 	if ((num_channels = md_scan_dag(md, vd_node,
26971ae08745Sheppo 		    md_find_name(md, VD_CHANNEL_ENDPOINT),
26981ae08745Sheppo 		    md_find_name(md, "fwd"), channel)) <= 0) {
26991ae08745Sheppo 		PRN("No \"%s\" found for virtual disk", VD_CHANNEL_ENDPOINT);
27001ae08745Sheppo 		return (-1);
27011ae08745Sheppo 	}
27021ae08745Sheppo 
27031ae08745Sheppo 	/* Get the "id" value for the first channel endpoint node */
27041ae08745Sheppo 	if (md_get_prop_val(md, channel[0], VD_ID_PROP, ldc_id) != 0) {
27051ae08745Sheppo 		PRN("No \"%s\" property found for \"%s\" of vdisk",
27061ae08745Sheppo 		    VD_ID_PROP, VD_CHANNEL_ENDPOINT);
27071ae08745Sheppo 		return (-1);
27081ae08745Sheppo 	}
27091ae08745Sheppo 
27101ae08745Sheppo 	if (num_channels > 1) {
27111ae08745Sheppo 		PRN("Using ID of first of multiple channels for this vdisk");
27121ae08745Sheppo 	}
27131ae08745Sheppo 
27141ae08745Sheppo 	return (0);
27151ae08745Sheppo }
27161ae08745Sheppo 
27171ae08745Sheppo static int
27181ae08745Sheppo vds_get_ldc_id(md_t *md, mde_cookie_t vd_node, uint64_t *ldc_id)
27191ae08745Sheppo {
27201ae08745Sheppo 	int		num_nodes, status;
27211ae08745Sheppo 	size_t		size;
27221ae08745Sheppo 	mde_cookie_t	*channel;
27231ae08745Sheppo 
27241ae08745Sheppo 
27251ae08745Sheppo 	if ((num_nodes = md_node_count(md)) <= 0) {
27261ae08745Sheppo 		PRN("Invalid node count in Machine Description subtree");
27271ae08745Sheppo 		return (-1);
27281ae08745Sheppo 	}
27291ae08745Sheppo 	size = num_nodes*(sizeof (*channel));
27301ae08745Sheppo 	channel = kmem_zalloc(size, KM_SLEEP);
27311ae08745Sheppo 	status = vds_do_get_ldc_id(md, vd_node, channel, ldc_id);
27321ae08745Sheppo 	kmem_free(channel, size);
27331ae08745Sheppo 
27341ae08745Sheppo 	return (status);
27351ae08745Sheppo }
27361ae08745Sheppo 
27371ae08745Sheppo static void
27381ae08745Sheppo vds_add_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
27391ae08745Sheppo {
2740e1ebb9ecSlm66018 	char		*device_path = NULL;
27411ae08745Sheppo 	uint64_t	id = 0, ldc_id = 0;
27421ae08745Sheppo 
27431ae08745Sheppo 
27441ae08745Sheppo 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
27451ae08745Sheppo 		PRN("Error getting vdisk \"%s\"", VD_ID_PROP);
27461ae08745Sheppo 		return;
27471ae08745Sheppo 	}
27481ae08745Sheppo 	PR0("Adding vdisk ID %lu", id);
27491ae08745Sheppo 	if (md_get_prop_str(md, vd_node, VD_BLOCK_DEVICE_PROP,
2750e1ebb9ecSlm66018 		&device_path) != 0) {
27511ae08745Sheppo 		PRN("Error getting vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
27521ae08745Sheppo 		return;
27531ae08745Sheppo 	}
27541ae08745Sheppo 
27551ae08745Sheppo 	if (vds_get_ldc_id(md, vd_node, &ldc_id) != 0) {
27561ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", id);
27571ae08745Sheppo 		return;
27581ae08745Sheppo 	}
27591ae08745Sheppo 
2760e1ebb9ecSlm66018 	if (vds_init_vd(vds, id, device_path, ldc_id) != 0) {
27611ae08745Sheppo 		PRN("Failed to add vdisk ID %lu", id);
27621ae08745Sheppo 		return;
27631ae08745Sheppo 	}
27641ae08745Sheppo }
27651ae08745Sheppo 
27661ae08745Sheppo static void
27671ae08745Sheppo vds_remove_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
27681ae08745Sheppo {
27691ae08745Sheppo 	uint64_t	id = 0;
27701ae08745Sheppo 
27711ae08745Sheppo 
27721ae08745Sheppo 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
27731ae08745Sheppo 		PRN("Unable to get \"%s\" property from vdisk's MD node",
27741ae08745Sheppo 		    VD_ID_PROP);
27751ae08745Sheppo 		return;
27761ae08745Sheppo 	}
27771ae08745Sheppo 	PR0("Removing vdisk ID %lu", id);
27781ae08745Sheppo 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0)
27791ae08745Sheppo 		PRN("No vdisk entry found for vdisk ID %lu", id);
27801ae08745Sheppo }
27811ae08745Sheppo 
27821ae08745Sheppo static void
27831ae08745Sheppo vds_change_vd(vds_t *vds, md_t *prev_md, mde_cookie_t prev_vd_node,
27841ae08745Sheppo     md_t *curr_md, mde_cookie_t curr_vd_node)
27851ae08745Sheppo {
27861ae08745Sheppo 	char		*curr_dev, *prev_dev;
27871ae08745Sheppo 	uint64_t	curr_id = 0, curr_ldc_id = 0;
27881ae08745Sheppo 	uint64_t	prev_id = 0, prev_ldc_id = 0;
27891ae08745Sheppo 	size_t		len;
27901ae08745Sheppo 
27911ae08745Sheppo 
27921ae08745Sheppo 	/* Validate that vdisk ID has not changed */
27931ae08745Sheppo 	if (md_get_prop_val(prev_md, prev_vd_node, VD_ID_PROP, &prev_id) != 0) {
27941ae08745Sheppo 		PRN("Error getting previous vdisk \"%s\" property",
27951ae08745Sheppo 		    VD_ID_PROP);
27961ae08745Sheppo 		return;
27971ae08745Sheppo 	}
27981ae08745Sheppo 	if (md_get_prop_val(curr_md, curr_vd_node, VD_ID_PROP, &curr_id) != 0) {
27991ae08745Sheppo 		PRN("Error getting current vdisk \"%s\" property", VD_ID_PROP);
28001ae08745Sheppo 		return;
28011ae08745Sheppo 	}
28021ae08745Sheppo 	if (curr_id != prev_id) {
28031ae08745Sheppo 		PRN("Not changing vdisk:  ID changed from %lu to %lu",
28041ae08745Sheppo 		    prev_id, curr_id);
28051ae08745Sheppo 		return;
28061ae08745Sheppo 	}
28071ae08745Sheppo 
28081ae08745Sheppo 	/* Validate that LDC ID has not changed */
28091ae08745Sheppo 	if (vds_get_ldc_id(prev_md, prev_vd_node, &prev_ldc_id) != 0) {
28101ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", prev_id);
28111ae08745Sheppo 		return;
28121ae08745Sheppo 	}
28131ae08745Sheppo 
28141ae08745Sheppo 	if (vds_get_ldc_id(curr_md, curr_vd_node, &curr_ldc_id) != 0) {
28151ae08745Sheppo 		PRN("Error getting LDC ID for vdisk %lu", curr_id);
28161ae08745Sheppo 		return;
28171ae08745Sheppo 	}
28181ae08745Sheppo 	if (curr_ldc_id != prev_ldc_id) {
28190a55fbb7Slm66018 		_NOTE(NOTREACHED);	/* lint is confused */
28201ae08745Sheppo 		PRN("Not changing vdisk:  "
28211ae08745Sheppo 		    "LDC ID changed from %lu to %lu", prev_ldc_id, curr_ldc_id);
28221ae08745Sheppo 		return;
28231ae08745Sheppo 	}
28241ae08745Sheppo 
28251ae08745Sheppo 	/* Determine whether device path has changed */
28261ae08745Sheppo 	if (md_get_prop_str(prev_md, prev_vd_node, VD_BLOCK_DEVICE_PROP,
28271ae08745Sheppo 		&prev_dev) != 0) {
28281ae08745Sheppo 		PRN("Error getting previous vdisk \"%s\"",
28291ae08745Sheppo 		    VD_BLOCK_DEVICE_PROP);
28301ae08745Sheppo 		return;
28311ae08745Sheppo 	}
28321ae08745Sheppo 	if (md_get_prop_str(curr_md, curr_vd_node, VD_BLOCK_DEVICE_PROP,
28331ae08745Sheppo 		&curr_dev) != 0) {
28341ae08745Sheppo 		PRN("Error getting current vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
28351ae08745Sheppo 		return;
28361ae08745Sheppo 	}
28371ae08745Sheppo 	if (((len = strlen(curr_dev)) == strlen(prev_dev)) &&
28381ae08745Sheppo 	    (strncmp(curr_dev, prev_dev, len) == 0))
28391ae08745Sheppo 		return;	/* no relevant (supported) change */
28401ae08745Sheppo 
28411ae08745Sheppo 	PR0("Changing vdisk ID %lu", prev_id);
28423af08d82Slm66018 
28431ae08745Sheppo 	/* Remove old state, which will close vdisk and reset */
28441ae08745Sheppo 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)prev_id) != 0)
28451ae08745Sheppo 		PRN("No entry found for vdisk ID %lu", prev_id);
28463af08d82Slm66018 
28471ae08745Sheppo 	/* Re-initialize vdisk with new state */
28481ae08745Sheppo 	if (vds_init_vd(vds, curr_id, curr_dev, curr_ldc_id) != 0) {
28491ae08745Sheppo 		PRN("Failed to change vdisk ID %lu", curr_id);
28501ae08745Sheppo 		return;
28511ae08745Sheppo 	}
28521ae08745Sheppo }
28531ae08745Sheppo 
28541ae08745Sheppo static int
28551ae08745Sheppo vds_process_md(void *arg, mdeg_result_t *md)
28561ae08745Sheppo {
28571ae08745Sheppo 	int	i;
28581ae08745Sheppo 	vds_t	*vds = arg;
28591ae08745Sheppo 
28601ae08745Sheppo 
28611ae08745Sheppo 	if (md == NULL)
28621ae08745Sheppo 		return (MDEG_FAILURE);
28631ae08745Sheppo 	ASSERT(vds != NULL);
28641ae08745Sheppo 
28651ae08745Sheppo 	for (i = 0; i < md->removed.nelem; i++)
28661ae08745Sheppo 		vds_remove_vd(vds, md->removed.mdp, md->removed.mdep[i]);
28671ae08745Sheppo 	for (i = 0; i < md->match_curr.nelem; i++)
28681ae08745Sheppo 		vds_change_vd(vds, md->match_prev.mdp, md->match_prev.mdep[i],
28691ae08745Sheppo 		    md->match_curr.mdp, md->match_curr.mdep[i]);
28701ae08745Sheppo 	for (i = 0; i < md->added.nelem; i++)
28711ae08745Sheppo 		vds_add_vd(vds, md->added.mdp, md->added.mdep[i]);
28721ae08745Sheppo 
28731ae08745Sheppo 	return (MDEG_SUCCESS);
28741ae08745Sheppo }
28751ae08745Sheppo 
28761ae08745Sheppo static int
28771ae08745Sheppo vds_do_attach(dev_info_t *dip)
28781ae08745Sheppo {
28791ae08745Sheppo 	static char	reg_prop[] = "reg";	/* devinfo ID prop */
28801ae08745Sheppo 
28811ae08745Sheppo 	/* MDEG specification for a (particular) vds node */
28821ae08745Sheppo 	static mdeg_prop_spec_t	vds_prop_spec[] = {
28831ae08745Sheppo 		{MDET_PROP_STR, "name", {VDS_NAME}},
28841ae08745Sheppo 		{MDET_PROP_VAL, "cfg-handle", {0}},
28851ae08745Sheppo 		{MDET_LIST_END, NULL, {0}}};
28861ae08745Sheppo 	static mdeg_node_spec_t	vds_spec = {"virtual-device", vds_prop_spec};
28871ae08745Sheppo 
28881ae08745Sheppo 	/* MDEG specification for matching a vd node */
28891ae08745Sheppo 	static md_prop_match_t	vd_prop_spec[] = {
28901ae08745Sheppo 		{MDET_PROP_VAL, VD_ID_PROP},
28911ae08745Sheppo 		{MDET_LIST_END, NULL}};
28921ae08745Sheppo 	static mdeg_node_match_t vd_spec = {"virtual-device-port",
28931ae08745Sheppo 					    vd_prop_spec};
28941ae08745Sheppo 
28951ae08745Sheppo 	int			status;
28961ae08745Sheppo 	uint64_t		cfg_handle;
28971ae08745Sheppo 	minor_t			instance = ddi_get_instance(dip);
28981ae08745Sheppo 	vds_t			*vds;
28991ae08745Sheppo 
29001ae08745Sheppo 
29011ae08745Sheppo 	/*
29021ae08745Sheppo 	 * The "cfg-handle" property of a vds node in an MD contains the MD's
29031ae08745Sheppo 	 * notion of "instance", or unique identifier, for that node; OBP
29041ae08745Sheppo 	 * stores the value of the "cfg-handle" MD property as the value of
29051ae08745Sheppo 	 * the "reg" property on the node in the device tree it builds from
29061ae08745Sheppo 	 * the MD and passes to Solaris.  Thus, we look up the devinfo node's
29071ae08745Sheppo 	 * "reg" property value to uniquely identify this device instance when
29081ae08745Sheppo 	 * registering with the MD event-generation framework.  If the "reg"
29091ae08745Sheppo 	 * property cannot be found, the device tree state is presumably so
29101ae08745Sheppo 	 * broken that there is no point in continuing.
29111ae08745Sheppo 	 */
29121ae08745Sheppo 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, reg_prop)) {
29131ae08745Sheppo 		PRN("vds \"%s\" property does not exist", reg_prop);
29141ae08745Sheppo 		return (DDI_FAILURE);
29151ae08745Sheppo 	}
29161ae08745Sheppo 
29171ae08745Sheppo 	/* Get the MD instance for later MDEG registration */
29181ae08745Sheppo 	cfg_handle = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
29191ae08745Sheppo 	    reg_prop, -1);
29201ae08745Sheppo 
29211ae08745Sheppo 	if (ddi_soft_state_zalloc(vds_state, instance) != DDI_SUCCESS) {
29221ae08745Sheppo 		PRN("Could not allocate state for instance %u", instance);
29231ae08745Sheppo 		return (DDI_FAILURE);
29241ae08745Sheppo 	}
29251ae08745Sheppo 
29261ae08745Sheppo 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
29271ae08745Sheppo 		PRN("Could not get state for instance %u", instance);
29281ae08745Sheppo 		ddi_soft_state_free(vds_state, instance);
29291ae08745Sheppo 		return (DDI_FAILURE);
29301ae08745Sheppo 	}
29311ae08745Sheppo 
29321ae08745Sheppo 
29331ae08745Sheppo 	vds->dip	= dip;
29341ae08745Sheppo 	vds->vd_table	= mod_hash_create_ptrhash("vds_vd_table", VDS_NCHAINS,
29351ae08745Sheppo 							vds_destroy_vd,
29361ae08745Sheppo 							sizeof (void *));
29371ae08745Sheppo 	ASSERT(vds->vd_table != NULL);
29381ae08745Sheppo 
29391ae08745Sheppo 	if ((status = ldi_ident_from_dip(dip, &vds->ldi_ident)) != 0) {
29401ae08745Sheppo 		PRN("ldi_ident_from_dip() returned errno %d", status);
29411ae08745Sheppo 		return (DDI_FAILURE);
29421ae08745Sheppo 	}
29431ae08745Sheppo 	vds->initialized |= VDS_LDI;
29441ae08745Sheppo 
29451ae08745Sheppo 	/* Register for MD updates */
29461ae08745Sheppo 	vds_prop_spec[1].ps_val = cfg_handle;
29471ae08745Sheppo 	if (mdeg_register(&vds_spec, &vd_spec, vds_process_md, vds,
29481ae08745Sheppo 		&vds->mdeg) != MDEG_SUCCESS) {
29491ae08745Sheppo 		PRN("Unable to register for MD updates");
29501ae08745Sheppo 		return (DDI_FAILURE);
29511ae08745Sheppo 	}
29521ae08745Sheppo 	vds->initialized |= VDS_MDEG;
29531ae08745Sheppo 
29540a55fbb7Slm66018 	/* Prevent auto-detaching so driver is available whenever MD changes */
29550a55fbb7Slm66018 	if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1) !=
29560a55fbb7Slm66018 	    DDI_PROP_SUCCESS) {
29570a55fbb7Slm66018 		PRN("failed to set \"%s\" property for instance %u",
29580a55fbb7Slm66018 		    DDI_NO_AUTODETACH, instance);
29590a55fbb7Slm66018 	}
29600a55fbb7Slm66018 
29611ae08745Sheppo 	ddi_report_dev(dip);
29621ae08745Sheppo 	return (DDI_SUCCESS);
29631ae08745Sheppo }
29641ae08745Sheppo 
29651ae08745Sheppo static int
29661ae08745Sheppo vds_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
29671ae08745Sheppo {
29681ae08745Sheppo 	int	status;
29691ae08745Sheppo 
29701ae08745Sheppo 	switch (cmd) {
29711ae08745Sheppo 	case DDI_ATTACH:
2972d10e4ef2Snarayan 		PR0("Attaching");
29731ae08745Sheppo 		if ((status = vds_do_attach(dip)) != DDI_SUCCESS)
29741ae08745Sheppo 			(void) vds_detach(dip, DDI_DETACH);
29751ae08745Sheppo 		return (status);
29761ae08745Sheppo 	case DDI_RESUME:
2977d10e4ef2Snarayan 		PR0("No action required for DDI_RESUME");
29781ae08745Sheppo 		return (DDI_SUCCESS);
29791ae08745Sheppo 	default:
29801ae08745Sheppo 		return (DDI_FAILURE);
29811ae08745Sheppo 	}
29821ae08745Sheppo }
29831ae08745Sheppo 
29841ae08745Sheppo static struct dev_ops vds_ops = {
29851ae08745Sheppo 	DEVO_REV,	/* devo_rev */
29861ae08745Sheppo 	0,		/* devo_refcnt */
29871ae08745Sheppo 	ddi_no_info,	/* devo_getinfo */
29881ae08745Sheppo 	nulldev,	/* devo_identify */
29891ae08745Sheppo 	nulldev,	/* devo_probe */
29901ae08745Sheppo 	vds_attach,	/* devo_attach */
29911ae08745Sheppo 	vds_detach,	/* devo_detach */
29921ae08745Sheppo 	nodev,		/* devo_reset */
29931ae08745Sheppo 	NULL,		/* devo_cb_ops */
29941ae08745Sheppo 	NULL,		/* devo_bus_ops */
29951ae08745Sheppo 	nulldev		/* devo_power */
29961ae08745Sheppo };
29971ae08745Sheppo 
29981ae08745Sheppo static struct modldrv modldrv = {
29991ae08745Sheppo 	&mod_driverops,
30001ae08745Sheppo 	"virtual disk server v%I%",
30011ae08745Sheppo 	&vds_ops,
30021ae08745Sheppo };
30031ae08745Sheppo 
30041ae08745Sheppo static struct modlinkage modlinkage = {
30051ae08745Sheppo 	MODREV_1,
30061ae08745Sheppo 	&modldrv,
30071ae08745Sheppo 	NULL
30081ae08745Sheppo };
30091ae08745Sheppo 
30101ae08745Sheppo 
30111ae08745Sheppo int
30121ae08745Sheppo _init(void)
30131ae08745Sheppo {
30141ae08745Sheppo 	int		i, status;
30151ae08745Sheppo 
3016d10e4ef2Snarayan 
30171ae08745Sheppo 	if ((status = ddi_soft_state_init(&vds_state, sizeof (vds_t), 1)) != 0)
30181ae08745Sheppo 		return (status);
30191ae08745Sheppo 	if ((status = mod_install(&modlinkage)) != 0) {
30201ae08745Sheppo 		ddi_soft_state_fini(&vds_state);
30211ae08745Sheppo 		return (status);
30221ae08745Sheppo 	}
30231ae08745Sheppo 
30241ae08745Sheppo 	/* Fill in the bit-mask of server-supported operations */
30251ae08745Sheppo 	for (i = 0; i < vds_noperations; i++)
30261ae08745Sheppo 		vds_operations |= 1 << (vds_operation[i].operation - 1);
30271ae08745Sheppo 
30281ae08745Sheppo 	return (0);
30291ae08745Sheppo }
30301ae08745Sheppo 
30311ae08745Sheppo int
30321ae08745Sheppo _info(struct modinfo *modinfop)
30331ae08745Sheppo {
30341ae08745Sheppo 	return (mod_info(&modlinkage, modinfop));
30351ae08745Sheppo }
30361ae08745Sheppo 
30371ae08745Sheppo int
30381ae08745Sheppo _fini(void)
30391ae08745Sheppo {
30401ae08745Sheppo 	int	status;
30411ae08745Sheppo 
3042d10e4ef2Snarayan 
30431ae08745Sheppo 	if ((status = mod_remove(&modlinkage)) != 0)
30441ae08745Sheppo 		return (status);
30451ae08745Sheppo 	ddi_soft_state_fini(&vds_state);
30461ae08745Sheppo 	return (0);
30471ae08745Sheppo }
3048