xref: /onnv-gate/usr/src/uts/sun4v/io/vds.c (revision 3782:d53b92e6d144)
11991Sheppo /*
21991Sheppo  * CDDL HEADER START
31991Sheppo  *
41991Sheppo  * The contents of this file are subject to the terms of the
51991Sheppo  * Common Development and Distribution License (the "License").
61991Sheppo  * You may not use this file except in compliance with the License.
71991Sheppo  *
81991Sheppo  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91991Sheppo  * or http://www.opensolaris.org/os/licensing.
101991Sheppo  * See the License for the specific language governing permissions
111991Sheppo  * and limitations under the License.
121991Sheppo  *
131991Sheppo  * When distributing Covered Code, include this CDDL HEADER in each
141991Sheppo  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151991Sheppo  * If applicable, add the following below this CDDL HEADER, with the
161991Sheppo  * fields enclosed by brackets "[]" replaced with your own identifying
171991Sheppo  * information: Portions Copyright [yyyy] [name of copyright owner]
181991Sheppo  *
191991Sheppo  * CDDL HEADER END
201991Sheppo  */
211991Sheppo 
221991Sheppo /*
233401Snarayan  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
241991Sheppo  * Use is subject to license terms.
251991Sheppo  */
261991Sheppo 
271991Sheppo #pragma ident	"%Z%%M%	%I%	%E% SMI"
281991Sheppo 
291991Sheppo /*
301991Sheppo  * Virtual disk server
311991Sheppo  */
321991Sheppo 
331991Sheppo 
341991Sheppo #include <sys/types.h>
351991Sheppo #include <sys/conf.h>
362531Snarayan #include <sys/crc32.h>
371991Sheppo #include <sys/ddi.h>
381991Sheppo #include <sys/dkio.h>
391991Sheppo #include <sys/file.h>
401991Sheppo #include <sys/mdeg.h>
411991Sheppo #include <sys/modhash.h>
421991Sheppo #include <sys/note.h>
431991Sheppo #include <sys/pathname.h>
441991Sheppo #include <sys/sunddi.h>
451991Sheppo #include <sys/sunldi.h>
461991Sheppo #include <sys/sysmacros.h>
471991Sheppo #include <sys/vio_common.h>
481991Sheppo #include <sys/vdsk_mailbox.h>
491991Sheppo #include <sys/vdsk_common.h>
501991Sheppo #include <sys/vtoc.h>
513401Snarayan #include <sys/vfs.h>
523401Snarayan #include <sys/stat.h>
53*3782Sachartre #include <vm/seg_map.h>
541991Sheppo 
551991Sheppo /* Virtual disk server initialization flags */
562336Snarayan #define	VDS_LDI			0x01
572336Snarayan #define	VDS_MDEG		0x02
581991Sheppo 
591991Sheppo /* Virtual disk server tunable parameters */
603401Snarayan #define	VDS_RETRIES		5
613401Snarayan #define	VDS_LDC_DELAY		1000 /* 1 msecs */
623401Snarayan #define	VDS_DEV_DELAY		10000000 /* 10 secs */
631991Sheppo #define	VDS_NCHAINS		32
641991Sheppo 
651991Sheppo /* Identification parameters for MD, synthetic dkio(7i) structures, etc. */
661991Sheppo #define	VDS_NAME		"virtual-disk-server"
671991Sheppo 
681991Sheppo #define	VD_NAME			"vd"
691991Sheppo #define	VD_VOLUME_NAME		"vdisk"
701991Sheppo #define	VD_ASCIILABEL		"Virtual Disk"
711991Sheppo 
721991Sheppo #define	VD_CHANNEL_ENDPOINT	"channel-endpoint"
731991Sheppo #define	VD_ID_PROP		"id"
741991Sheppo #define	VD_BLOCK_DEVICE_PROP	"vds-block-device"
753297Ssb155480 #define	VD_REG_PROP		"reg"
761991Sheppo 
771991Sheppo /* Virtual disk initialization flags */
783401Snarayan #define	VD_DISK_READY		0x01
793401Snarayan #define	VD_LOCKING		0x02
803401Snarayan #define	VD_LDC			0x04
813401Snarayan #define	VD_DRING		0x08
823401Snarayan #define	VD_SID			0x10
833401Snarayan #define	VD_SEQ_NUM		0x20
841991Sheppo 
851991Sheppo /* Flags for opening/closing backing devices via LDI */
861991Sheppo #define	VD_OPEN_FLAGS		(FEXCL | FREAD | FWRITE)
871991Sheppo 
881991Sheppo /*
891991Sheppo  * By Solaris convention, slice/partition 2 represents the entire disk;
901991Sheppo  * unfortunately, this convention does not appear to be codified.
911991Sheppo  */
921991Sheppo #define	VD_ENTIRE_DISK_SLICE	2
931991Sheppo 
941991Sheppo /* Return a cpp token as a string */
951991Sheppo #define	STRINGIZE(token)	#token
961991Sheppo 
971991Sheppo /*
981991Sheppo  * Print a message prefixed with the current function name to the message log
991991Sheppo  * (and optionally to the console for verbose boots); these macros use cpp's
1001991Sheppo  * concatenation of string literals and C99 variable-length-argument-list
1011991Sheppo  * macros
1021991Sheppo  */
1031991Sheppo #define	PRN(...)	_PRN("?%s():  "__VA_ARGS__, "")
1041991Sheppo #define	_PRN(format, ...)					\
1051991Sheppo 	cmn_err(CE_CONT, format"%s", __func__, __VA_ARGS__)
1061991Sheppo 
1071991Sheppo /* Return a pointer to the "i"th vdisk dring element */
1081991Sheppo #define	VD_DRING_ELEM(i)	((vd_dring_entry_t *)(void *)	\
1091991Sheppo 	    (vd->dring + (i)*vd->descriptor_size))
1101991Sheppo 
1111991Sheppo /* Return the virtual disk client's type as a string (for use in messages) */
1121991Sheppo #define	VD_CLIENT(vd)							\
1131991Sheppo 	(((vd)->xfer_mode == VIO_DESC_MODE) ? "in-band client" :	\
1141991Sheppo 	    (((vd)->xfer_mode == VIO_DRING_MODE) ? "dring client" :	\
1151991Sheppo 		(((vd)->xfer_mode == 0) ? "null client" :		\
1161991Sheppo 		    "unsupported client")))
1171991Sheppo 
118*3782Sachartre /* For IO to raw disk on file */
119*3782Sachartre #define	VD_FILE_SLICE_NONE	-1
120*3782Sachartre 
121*3782Sachartre /* Read disk label from a disk on file */
122*3782Sachartre #define	VD_FILE_LABEL_READ(vd, labelp) \
123*3782Sachartre 	vd_file_rw(vd, VD_FILE_SLICE_NONE, VD_OP_BREAD, (caddr_t)labelp, \
124*3782Sachartre 	    0, sizeof (struct dk_label))
125*3782Sachartre 
126*3782Sachartre /* Write disk label to a disk on file */
127*3782Sachartre #define	VD_FILE_LABEL_WRITE(vd, labelp)	\
128*3782Sachartre 	vd_file_rw(vd, VD_FILE_SLICE_NONE, VD_OP_BWRITE, (caddr_t)labelp, \
129*3782Sachartre 	    0, sizeof (struct dk_label))
130*3782Sachartre 
1313297Ssb155480 /*
1323297Ssb155480  * Specification of an MD node passed to the MDEG to filter any
1333297Ssb155480  * 'vport' nodes that do not belong to the specified node. This
1343297Ssb155480  * template is copied for each vds instance and filled in with
1353297Ssb155480  * the appropriate 'cfg-handle' value before being passed to the MDEG.
1363297Ssb155480  */
1373297Ssb155480 static mdeg_prop_spec_t	vds_prop_template[] = {
1383297Ssb155480 	{ MDET_PROP_STR,	"name",		VDS_NAME },
1393297Ssb155480 	{ MDET_PROP_VAL,	"cfg-handle",	NULL },
1403297Ssb155480 	{ MDET_LIST_END,	NULL, 		NULL }
1413297Ssb155480 };
1423297Ssb155480 
1433297Ssb155480 #define	VDS_SET_MDEG_PROP_INST(specp, val) (specp)[1].ps_val = (val);
1443297Ssb155480 
1453297Ssb155480 /*
1463297Ssb155480  * Matching criteria passed to the MDEG to register interest
1473297Ssb155480  * in changes to 'virtual-device-port' nodes identified by their
1483297Ssb155480  * 'id' property.
1493297Ssb155480  */
1503297Ssb155480 static md_prop_match_t	vd_prop_match[] = {
1513297Ssb155480 	{ MDET_PROP_VAL,	VD_ID_PROP },
1523297Ssb155480 	{ MDET_LIST_END,	NULL }
1533297Ssb155480 };
1543297Ssb155480 
1553297Ssb155480 static mdeg_node_match_t vd_match = {"virtual-device-port",
1563297Ssb155480 				    vd_prop_match};
1573297Ssb155480 
1581991Sheppo /* Debugging macros */
1591991Sheppo #ifdef DEBUG
1602793Slm66018 
1612793Slm66018 static int	vd_msglevel = 0;
1622793Slm66018 
1631991Sheppo #define	PR0 if (vd_msglevel > 0)	PRN
1641991Sheppo #define	PR1 if (vd_msglevel > 1)	PRN
1651991Sheppo #define	PR2 if (vd_msglevel > 2)	PRN
1661991Sheppo 
1671991Sheppo #define	VD_DUMP_DRING_ELEM(elem)					\
1683401Snarayan 	PR0("dst:%x op:%x st:%u nb:%lx addr:%lx ncook:%u\n",		\
1691991Sheppo 	    elem->hdr.dstate,						\
1701991Sheppo 	    elem->payload.operation,					\
1711991Sheppo 	    elem->payload.status,					\
1721991Sheppo 	    elem->payload.nbytes,					\
1731991Sheppo 	    elem->payload.addr,						\
1741991Sheppo 	    elem->payload.ncookies);
1751991Sheppo 
1762793Slm66018 char *
1772793Slm66018 vd_decode_state(int state)
1782793Slm66018 {
1792793Slm66018 	char *str;
1802793Slm66018 
1812793Slm66018 #define	CASE_STATE(_s)	case _s: str = #_s; break;
1822793Slm66018 
1832793Slm66018 	switch (state) {
1842793Slm66018 	CASE_STATE(VD_STATE_INIT)
1852793Slm66018 	CASE_STATE(VD_STATE_VER)
1862793Slm66018 	CASE_STATE(VD_STATE_ATTR)
1872793Slm66018 	CASE_STATE(VD_STATE_DRING)
1882793Slm66018 	CASE_STATE(VD_STATE_RDX)
1892793Slm66018 	CASE_STATE(VD_STATE_DATA)
1902793Slm66018 	default: str = "unknown"; break;
1912793Slm66018 	}
1922793Slm66018 
1932793Slm66018 #undef CASE_STATE
1942793Slm66018 
1952793Slm66018 	return (str);
1962793Slm66018 }
1972793Slm66018 
1982793Slm66018 void
1992793Slm66018 vd_decode_tag(vio_msg_t *msg)
2002793Slm66018 {
2012793Slm66018 	char *tstr, *sstr, *estr;
2022793Slm66018 
2032793Slm66018 #define	CASE_TYPE(_s)	case _s: tstr = #_s; break;
2042793Slm66018 
2052793Slm66018 	switch (msg->tag.vio_msgtype) {
2062793Slm66018 	CASE_TYPE(VIO_TYPE_CTRL)
2072793Slm66018 	CASE_TYPE(VIO_TYPE_DATA)
2082793Slm66018 	CASE_TYPE(VIO_TYPE_ERR)
2092793Slm66018 	default: tstr = "unknown"; break;
2102793Slm66018 	}
2112793Slm66018 
2122793Slm66018 #undef CASE_TYPE
2132793Slm66018 
2142793Slm66018 #define	CASE_SUBTYPE(_s) case _s: sstr = #_s; break;
2152793Slm66018 
2162793Slm66018 	switch (msg->tag.vio_subtype) {
2172793Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_INFO)
2182793Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_ACK)
2192793Slm66018 	CASE_SUBTYPE(VIO_SUBTYPE_NACK)
2202793Slm66018 	default: sstr = "unknown"; break;
2212793Slm66018 	}
2222793Slm66018 
2232793Slm66018 #undef CASE_SUBTYPE
2242793Slm66018 
2252793Slm66018 #define	CASE_ENV(_s)	case _s: estr = #_s; break;
2262793Slm66018 
2272793Slm66018 	switch (msg->tag.vio_subtype_env) {
2282793Slm66018 	CASE_ENV(VIO_VER_INFO)
2292793Slm66018 	CASE_ENV(VIO_ATTR_INFO)
2302793Slm66018 	CASE_ENV(VIO_DRING_REG)
2312793Slm66018 	CASE_ENV(VIO_DRING_UNREG)
2322793Slm66018 	CASE_ENV(VIO_RDX)
2332793Slm66018 	CASE_ENV(VIO_PKT_DATA)
2342793Slm66018 	CASE_ENV(VIO_DESC_DATA)
2352793Slm66018 	CASE_ENV(VIO_DRING_DATA)
2362793Slm66018 	default: estr = "unknown"; break;
2372793Slm66018 	}
2382793Slm66018 
2392793Slm66018 #undef CASE_ENV
2402793Slm66018 
2412793Slm66018 	PR1("(%x/%x/%x) message : (%s/%s/%s)",
2422793Slm66018 	    msg->tag.vio_msgtype, msg->tag.vio_subtype,
2432793Slm66018 	    msg->tag.vio_subtype_env, tstr, sstr, estr);
2442793Slm66018 }
2452793Slm66018 
2461991Sheppo #else	/* !DEBUG */
2472793Slm66018 
2481991Sheppo #define	PR0(...)
2491991Sheppo #define	PR1(...)
2501991Sheppo #define	PR2(...)
2511991Sheppo 
2521991Sheppo #define	VD_DUMP_DRING_ELEM(elem)
2531991Sheppo 
2542793Slm66018 #define	vd_decode_state(_s)	(NULL)
2552793Slm66018 #define	vd_decode_tag(_s)	(NULL)
2562793Slm66018 
2571991Sheppo #endif	/* DEBUG */
2581991Sheppo 
2591991Sheppo 
2602336Snarayan /*
2612336Snarayan  * Soft state structure for a vds instance
2622336Snarayan  */
2631991Sheppo typedef struct vds {
2641991Sheppo 	uint_t		initialized;	/* driver inst initialization flags */
2651991Sheppo 	dev_info_t	*dip;		/* driver inst devinfo pointer */
2661991Sheppo 	ldi_ident_t	ldi_ident;	/* driver's identifier for LDI */
2671991Sheppo 	mod_hash_t	*vd_table;	/* table of virtual disks served */
2683297Ssb155480 	mdeg_node_spec_t *ispecp;	/* mdeg node specification */
2691991Sheppo 	mdeg_handle_t	mdeg;		/* handle for MDEG operations  */
2701991Sheppo } vds_t;
2711991Sheppo 
2722336Snarayan /*
2732336Snarayan  * Types of descriptor-processing tasks
2742336Snarayan  */
2752336Snarayan typedef enum vd_task_type {
2762336Snarayan 	VD_NONFINAL_RANGE_TASK,	/* task for intermediate descriptor in range */
2772336Snarayan 	VD_FINAL_RANGE_TASK,	/* task for last in a range of descriptors */
2782336Snarayan } vd_task_type_t;
2792336Snarayan 
2802336Snarayan /*
2812336Snarayan  * Structure describing the task for processing a descriptor
2822336Snarayan  */
2832336Snarayan typedef struct vd_task {
2842336Snarayan 	struct vd		*vd;		/* vd instance task is for */
2852336Snarayan 	vd_task_type_t		type;		/* type of descriptor task */
2862336Snarayan 	int			index;		/* dring elem index for task */
2872336Snarayan 	vio_msg_t		*msg;		/* VIO message task is for */
2882336Snarayan 	size_t			msglen;		/* length of message content */
2892336Snarayan 	vd_dring_payload_t	*request;	/* request task will perform */
2902336Snarayan 	struct buf		buf;		/* buf(9s) for I/O request */
2912531Snarayan 	ldc_mem_handle_t	mhdl;		/* task memory handle */
2922336Snarayan } vd_task_t;
2932336Snarayan 
2942336Snarayan /*
2952336Snarayan  * Soft state structure for a virtual disk instance
2962336Snarayan  */
2971991Sheppo typedef struct vd {
2981991Sheppo 	uint_t			initialized;	/* vdisk initialization flags */
2991991Sheppo 	vds_t			*vds;		/* server for this vdisk */
3002336Snarayan 	ddi_taskq_t		*startq;	/* queue for I/O start tasks */
3012336Snarayan 	ddi_taskq_t		*completionq;	/* queue for completion tasks */
3021991Sheppo 	ldi_handle_t		ldi_handle[V_NUMPAR];	/* LDI slice handles */
3033401Snarayan 	char			device_path[MAXPATHLEN + 1]; /* vdisk device */
3041991Sheppo 	dev_t			dev[V_NUMPAR];	/* dev numbers for slices */
3052410Slm66018 	uint_t			nslices;	/* number of slices */
3061991Sheppo 	size_t			vdisk_size;	/* number of blocks in vdisk */
3071991Sheppo 	vd_disk_type_t		vdisk_type;	/* slice or entire disk */
3082531Snarayan 	vd_disk_label_t		vdisk_label;	/* EFI or VTOC label */
3092410Slm66018 	ushort_t		max_xfer_sz;	/* max xfer size in DEV_BSIZE */
3101991Sheppo 	boolean_t		pseudo;		/* underlying pseudo dev */
3113401Snarayan 	boolean_t		file;		/* underlying file */
3123401Snarayan 	vnode_t			*file_vnode;	/* file vnode */
3133401Snarayan 	size_t			file_size;	/* file size */
3142531Snarayan 	struct dk_efi		dk_efi;		/* synthetic for slice type */
3151991Sheppo 	struct dk_geom		dk_geom;	/* synthetic for slice type */
3161991Sheppo 	struct vtoc		vtoc;		/* synthetic for slice type */
3171991Sheppo 	ldc_status_t		ldc_state;	/* LDC connection state */
3181991Sheppo 	ldc_handle_t		ldc_handle;	/* handle for LDC comm */
3191991Sheppo 	size_t			max_msglen;	/* largest LDC message len */
3201991Sheppo 	vd_state_t		state;		/* client handshake state */
3211991Sheppo 	uint8_t			xfer_mode;	/* transfer mode with client */
3221991Sheppo 	uint32_t		sid;		/* client's session ID */
3231991Sheppo 	uint64_t		seq_num;	/* message sequence number */
3241991Sheppo 	uint64_t		dring_ident;	/* identifier of dring */
3251991Sheppo 	ldc_dring_handle_t	dring_handle;	/* handle for dring ops */
3261991Sheppo 	uint32_t		descriptor_size;	/* num bytes in desc */
3271991Sheppo 	uint32_t		dring_len;	/* number of dring elements */
3281991Sheppo 	caddr_t			dring;		/* address of dring */
3292793Slm66018 	caddr_t			vio_msgp;	/* vio msg staging buffer */
3302336Snarayan 	vd_task_t		inband_task;	/* task for inband descriptor */
3312336Snarayan 	vd_task_t		*dring_task;	/* tasks dring elements */
3322336Snarayan 
3332336Snarayan 	kmutex_t		lock;		/* protects variables below */
3342336Snarayan 	boolean_t		enabled;	/* is vdisk enabled? */
3352336Snarayan 	boolean_t		reset_state;	/* reset connection state? */
3362336Snarayan 	boolean_t		reset_ldc;	/* reset LDC channel? */
3371991Sheppo } vd_t;
3381991Sheppo 
3391991Sheppo typedef struct vds_operation {
3402793Slm66018 	char	*namep;
3411991Sheppo 	uint8_t	operation;
3422336Snarayan 	int	(*start)(vd_task_t *task);
3432336Snarayan 	void	(*complete)(void *arg);
3441991Sheppo } vds_operation_t;
3451991Sheppo 
3462032Slm66018 typedef struct vd_ioctl {
3472032Slm66018 	uint8_t		operation;		/* vdisk operation */
3482032Slm66018 	const char	*operation_name;	/* vdisk operation name */
3492032Slm66018 	size_t		nbytes;			/* size of operation buffer */
3502032Slm66018 	int		cmd;			/* corresponding ioctl cmd */
3512032Slm66018 	const char	*cmd_name;		/* ioctl cmd name */
3522032Slm66018 	void		*arg;			/* ioctl cmd argument */
3532032Slm66018 	/* convert input vd_buf to output ioctl_arg */
3542032Slm66018 	void		(*copyin)(void *vd_buf, void *ioctl_arg);
3552032Slm66018 	/* convert input ioctl_arg to output vd_buf */
3562032Slm66018 	void		(*copyout)(void *ioctl_arg, void *vd_buf);
3572032Slm66018 } vd_ioctl_t;
3582032Slm66018 
3592032Slm66018 /* Define trivial copyin/copyout conversion function flag */
3602032Slm66018 #define	VD_IDENTITY	((void (*)(void *, void *))-1)
3611991Sheppo 
3621991Sheppo 
3633401Snarayan static int	vds_ldc_retries = VDS_RETRIES;
3642793Slm66018 static int	vds_ldc_delay = VDS_LDC_DELAY;
3653401Snarayan static int	vds_dev_retries = VDS_RETRIES;
3663401Snarayan static int	vds_dev_delay = VDS_DEV_DELAY;
3671991Sheppo static void	*vds_state;
3681991Sheppo static uint64_t	vds_operations;	/* see vds_operation[] definition below */
3691991Sheppo 
3701991Sheppo static int	vd_open_flags = VD_OPEN_FLAGS;
3711991Sheppo 
3722032Slm66018 /*
3732032Slm66018  * Supported protocol version pairs, from highest (newest) to lowest (oldest)
3742032Slm66018  *
3752032Slm66018  * Each supported major version should appear only once, paired with (and only
3762032Slm66018  * with) its highest supported minor version number (as the protocol requires
3772032Slm66018  * supporting all lower minor version numbers as well)
3782032Slm66018  */
3792032Slm66018 static const vio_ver_t	vds_version[] = {{1, 0}};
3802032Slm66018 static const size_t	vds_num_versions =
3812032Slm66018     sizeof (vds_version)/sizeof (vds_version[0]);
3822032Slm66018 
3832793Slm66018 static void vd_free_dring_task(vd_t *vdp);
3843401Snarayan static int vd_setup_vd(vd_t *vd);
3853401Snarayan static boolean_t vd_enabled(vd_t *vd);
3861991Sheppo 
387*3782Sachartre /*
388*3782Sachartre  * Function:
389*3782Sachartre  *	vd_file_rw
390*3782Sachartre  *
391*3782Sachartre  * Description:
392*3782Sachartre  * 	Read or write to a disk on file.
393*3782Sachartre  *
394*3782Sachartre  * Parameters:
395*3782Sachartre  *	vd		- disk on which the operation is performed.
396*3782Sachartre  *	slice		- slice on which the operation is performed,
397*3782Sachartre  *			  VD_FILE_SLICE_NONE indicates that the operation
398*3782Sachartre  *			  is done on the raw disk.
399*3782Sachartre  *	operation	- operation to execute: read (VD_OP_BREAD) or
400*3782Sachartre  *			  write (VD_OP_BWRITE).
401*3782Sachartre  *	data		- buffer where data are read to or written from.
402*3782Sachartre  *	blk		- starting block for the operation.
403*3782Sachartre  *	len		- number of bytes to read or write.
404*3782Sachartre  *
405*3782Sachartre  * Return Code:
406*3782Sachartre  *	n >= 0		- success, n indicates the number of bytes read
407*3782Sachartre  *			  or written.
408*3782Sachartre  *	-1		- error.
409*3782Sachartre  */
410*3782Sachartre static ssize_t
411*3782Sachartre vd_file_rw(vd_t *vd, int slice, int operation, caddr_t data, size_t blk,
412*3782Sachartre     size_t len)
413*3782Sachartre {
414*3782Sachartre 	caddr_t	maddr;
415*3782Sachartre 	size_t offset, maxlen, moffset, mlen, n;
416*3782Sachartre 	uint_t smflags;
417*3782Sachartre 	enum seg_rw srw;
418*3782Sachartre 
419*3782Sachartre 	ASSERT(vd->file);
420*3782Sachartre 	ASSERT(len > 0);
421*3782Sachartre 
422*3782Sachartre 	if (slice == VD_FILE_SLICE_NONE) {
423*3782Sachartre 		/* raw disk access */
424*3782Sachartre 		offset = blk * DEV_BSIZE;
425*3782Sachartre 	} else {
426*3782Sachartre 		ASSERT(slice >= 0 && slice < V_NUMPAR);
427*3782Sachartre 		if (blk >= vd->vtoc.v_part[slice].p_size) {
428*3782Sachartre 			/* address past the end of the slice */
429*3782Sachartre 			PR0("req_addr (0x%lx) > psize (0x%lx)",
430*3782Sachartre 			    blk, vd->vtoc.v_part[slice].p_size);
431*3782Sachartre 			return (0);
432*3782Sachartre 		}
433*3782Sachartre 
434*3782Sachartre 		offset = (vd->vtoc.v_part[slice].p_start + blk) * DEV_BSIZE;
435*3782Sachartre 
436*3782Sachartre 		/*
437*3782Sachartre 		 * If the requested size is greater than the size
438*3782Sachartre 		 * of the partition, truncate the read/write.
439*3782Sachartre 		 */
440*3782Sachartre 		maxlen = (vd->vtoc.v_part[slice].p_size - blk) * DEV_BSIZE;
441*3782Sachartre 
442*3782Sachartre 		if (len > maxlen) {
443*3782Sachartre 			PR0("I/O size truncated to %lu bytes from %lu bytes",
444*3782Sachartre 			    maxlen, len);
445*3782Sachartre 			len = maxlen;
446*3782Sachartre 		}
447*3782Sachartre 	}
448*3782Sachartre 
449*3782Sachartre 	/*
450*3782Sachartre 	 * We have to ensure that we are reading/writing into the mmap
451*3782Sachartre 	 * range. If we have a partial disk image (e.g. an image of
452*3782Sachartre 	 * s0 instead s2) the system can try to access slices that
453*3782Sachartre 	 * are not included into the disk image.
454*3782Sachartre 	 */
455*3782Sachartre 	if ((offset + len) >= vd->file_size) {
456*3782Sachartre 		PR0("offset + nbytes (0x%lx + 0x%lx) >= "
457*3782Sachartre 		    "file_size (0x%lx)", offset, len, vd->file_size);
458*3782Sachartre 		return (-1);
459*3782Sachartre 	}
460*3782Sachartre 
461*3782Sachartre 	srw = (operation == VD_OP_BREAD)? S_READ : S_WRITE;
462*3782Sachartre 	smflags = (operation == VD_OP_BREAD)? 0 : SM_WRITE;
463*3782Sachartre 	n = len;
464*3782Sachartre 
465*3782Sachartre 	do {
466*3782Sachartre 		/*
467*3782Sachartre 		 * segmap_getmapflt() returns a MAXBSIZE chunk which is
468*3782Sachartre 		 * MAXBSIZE aligned.
469*3782Sachartre 		 */
470*3782Sachartre 		moffset = offset & MAXBOFFSET;
471*3782Sachartre 		mlen = MIN(MAXBSIZE - moffset, n);
472*3782Sachartre 		maddr = segmap_getmapflt(segkmap, vd->file_vnode, offset,
473*3782Sachartre 		    mlen, 1, srw);
474*3782Sachartre 		/*
475*3782Sachartre 		 * Fault in the pages so we can check for error and ensure
476*3782Sachartre 		 * that we can safely used the mapped address.
477*3782Sachartre 		 */
478*3782Sachartre 		if (segmap_fault(kas.a_hat, segkmap, maddr, mlen,
479*3782Sachartre 		    F_SOFTLOCK, srw) != 0) {
480*3782Sachartre 			(void) segmap_release(segkmap, maddr, 0);
481*3782Sachartre 			return (-1);
482*3782Sachartre 		}
483*3782Sachartre 
484*3782Sachartre 		if (operation == VD_OP_BREAD)
485*3782Sachartre 			bcopy(maddr + moffset, data, mlen);
486*3782Sachartre 		else
487*3782Sachartre 			bcopy(data, maddr + moffset, mlen);
488*3782Sachartre 
489*3782Sachartre 		if (segmap_fault(kas.a_hat, segkmap, maddr, mlen,
490*3782Sachartre 		    F_SOFTUNLOCK, srw) != 0) {
491*3782Sachartre 			(void) segmap_release(segkmap, maddr, 0);
492*3782Sachartre 			return (-1);
493*3782Sachartre 		}
494*3782Sachartre 		if (segmap_release(segkmap, maddr, smflags) != 0)
495*3782Sachartre 			return (-1);
496*3782Sachartre 		n -= mlen;
497*3782Sachartre 		offset += mlen;
498*3782Sachartre 		data += mlen;
499*3782Sachartre 
500*3782Sachartre 	} while (n > 0);
501*3782Sachartre 
502*3782Sachartre 	return (len);
503*3782Sachartre }
504*3782Sachartre 
5051991Sheppo static int
5062336Snarayan vd_start_bio(vd_task_t *task)
5071991Sheppo {
5082531Snarayan 	int			rv, status = 0;
5092336Snarayan 	vd_t			*vd		= task->vd;
5102336Snarayan 	vd_dring_payload_t	*request	= task->request;
5112336Snarayan 	struct buf		*buf		= &task->buf;
5122531Snarayan 	uint8_t			mtype;
5133401Snarayan 	int 			slice;
5142336Snarayan 
5152336Snarayan 	ASSERT(vd != NULL);
5162336Snarayan 	ASSERT(request != NULL);
5173401Snarayan 
5183401Snarayan 	slice = request->slice;
5193401Snarayan 
5203401Snarayan 	ASSERT(slice < vd->nslices);
5212336Snarayan 	ASSERT((request->operation == VD_OP_BREAD) ||
5222336Snarayan 	    (request->operation == VD_OP_BWRITE));
5232336Snarayan 
5241991Sheppo 	if (request->nbytes == 0)
5251991Sheppo 		return (EINVAL);	/* no service for trivial requests */
5262336Snarayan 
5272336Snarayan 	PR1("%s %lu bytes at block %lu",
5282336Snarayan 	    (request->operation == VD_OP_BREAD) ? "Read" : "Write",
5292336Snarayan 	    request->nbytes, request->addr);
5302336Snarayan 
5312336Snarayan 	bioinit(buf);
5322336Snarayan 	buf->b_flags		= B_BUSY;
5332336Snarayan 	buf->b_bcount		= request->nbytes;
5342336Snarayan 	buf->b_lblkno		= request->addr;
5353401Snarayan 	buf->b_edev		= vd->dev[slice];
5362336Snarayan 
5372531Snarayan 	mtype = (&vd->inband_task == task) ? LDC_SHADOW_MAP : LDC_DIRECT_MAP;
5382531Snarayan 
5392531Snarayan 	/* Map memory exported by client */
5402531Snarayan 	status = ldc_mem_map(task->mhdl, request->cookie, request->ncookies,
5412531Snarayan 	    mtype, (request->operation == VD_OP_BREAD) ? LDC_MEM_W : LDC_MEM_R,
5422531Snarayan 	    &(buf->b_un.b_addr), NULL);
5432531Snarayan 	if (status != 0) {
5442793Slm66018 		PR0("ldc_mem_map() returned err %d ", status);
5452531Snarayan 		biofini(buf);
5462531Snarayan 		return (status);
5472336Snarayan 	}
5482336Snarayan 
5492531Snarayan 	status = ldc_mem_acquire(task->mhdl, 0, buf->b_bcount);
5502531Snarayan 	if (status != 0) {
5512531Snarayan 		(void) ldc_mem_unmap(task->mhdl);
5522793Slm66018 		PR0("ldc_mem_acquire() returned err %d ", status);
5532531Snarayan 		biofini(buf);
5542531Snarayan 		return (status);
5552531Snarayan 	}
5562531Snarayan 
5572531Snarayan 	buf->b_flags |= (request->operation == VD_OP_BREAD) ? B_READ : B_WRITE;
5582531Snarayan 
5592336Snarayan 	/* Start the block I/O */
5603401Snarayan 	if (vd->file) {
561*3782Sachartre 		rv = vd_file_rw(vd, slice, request->operation, buf->b_un.b_addr,
562*3782Sachartre 		    request->addr, request->nbytes);
563*3782Sachartre 		if (rv < 0) {
5643401Snarayan 			request->nbytes = 0;
5653401Snarayan 			status = EIO;
566*3782Sachartre 		} else {
567*3782Sachartre 			request->nbytes = rv;
568*3782Sachartre 			status = 0;
5693401Snarayan 		}
5703401Snarayan 	} else {
5713401Snarayan 		status = ldi_strategy(vd->ldi_handle[slice], buf);
5723401Snarayan 		if (status == 0)
5733401Snarayan 			return (EINPROGRESS); /* will complete on completionq */
5743401Snarayan 	}
5753401Snarayan 
5762336Snarayan 	/* Clean up after error */
5772531Snarayan 	rv = ldc_mem_release(task->mhdl, 0, buf->b_bcount);
5782531Snarayan 	if (rv) {
5792793Slm66018 		PR0("ldc_mem_release() returned err %d ", rv);
5802531Snarayan 	}
5812531Snarayan 	rv = ldc_mem_unmap(task->mhdl);
5822531Snarayan 	if (rv) {
5832793Slm66018 		PR0("ldc_mem_unmap() returned err %d ", status);
5842531Snarayan 	}
5852531Snarayan 
5862336Snarayan 	biofini(buf);
5872336Snarayan 	return (status);
5882336Snarayan }
5892336Snarayan 
5902336Snarayan static int
5912336Snarayan send_msg(ldc_handle_t ldc_handle, void *msg, size_t msglen)
5922336Snarayan {
5932793Slm66018 	int	status;
5942336Snarayan 	size_t	nbytes;
5952336Snarayan 
5962793Slm66018 	do {
5972336Snarayan 		nbytes = msglen;
5982336Snarayan 		status = ldc_write(ldc_handle, msg, &nbytes);
5992793Slm66018 		if (status != EWOULDBLOCK)
6002793Slm66018 			break;
6012793Slm66018 		drv_usecwait(vds_ldc_delay);
6022793Slm66018 	} while (status == EWOULDBLOCK);
6032336Snarayan 
6042336Snarayan 	if (status != 0) {
6052793Slm66018 		if (status != ECONNRESET)
6062793Slm66018 			PR0("ldc_write() returned errno %d", status);
6072336Snarayan 		return (status);
6082336Snarayan 	} else if (nbytes != msglen) {
6092793Slm66018 		PR0("ldc_write() performed only partial write");
6102336Snarayan 		return (EIO);
6112336Snarayan 	}
6122336Snarayan 
6132336Snarayan 	PR1("SENT %lu bytes", msglen);
6142336Snarayan 	return (0);
6152336Snarayan }
6162336Snarayan 
6172336Snarayan static void
6182336Snarayan vd_need_reset(vd_t *vd, boolean_t reset_ldc)
6192336Snarayan {
6202336Snarayan 	mutex_enter(&vd->lock);
6212336Snarayan 	vd->reset_state	= B_TRUE;
6222336Snarayan 	vd->reset_ldc	= reset_ldc;
6232336Snarayan 	mutex_exit(&vd->lock);
6242336Snarayan }
6252336Snarayan 
6262336Snarayan /*
6272336Snarayan  * Reset the state of the connection with a client, if needed; reset the LDC
6282336Snarayan  * transport as well, if needed.  This function should only be called from the
6292793Slm66018  * "vd_recv_msg", as it waits for tasks - otherwise a deadlock can occur.
6302336Snarayan  */
6312336Snarayan static void
6322336Snarayan vd_reset_if_needed(vd_t *vd)
6332336Snarayan {
6342793Slm66018 	int	status = 0;
6352336Snarayan 
6362336Snarayan 	mutex_enter(&vd->lock);
6372336Snarayan 	if (!vd->reset_state) {
6382336Snarayan 		ASSERT(!vd->reset_ldc);
6392336Snarayan 		mutex_exit(&vd->lock);
6402336Snarayan 		return;
6412336Snarayan 	}
6422336Snarayan 	mutex_exit(&vd->lock);
6432336Snarayan 
6442336Snarayan 	PR0("Resetting connection state with %s", VD_CLIENT(vd));
6452336Snarayan 
6462336Snarayan 	/*
6472336Snarayan 	 * Let any asynchronous I/O complete before possibly pulling the rug
6482336Snarayan 	 * out from under it; defer checking vd->reset_ldc, as one of the
6492336Snarayan 	 * asynchronous tasks might set it
6502336Snarayan 	 */
6512336Snarayan 	ddi_taskq_wait(vd->completionq);
6522336Snarayan 
6533401Snarayan 	if (vd->file) {
6543401Snarayan 		status = VOP_FSYNC(vd->file_vnode, FSYNC, kcred);
6553401Snarayan 		if (status) {
6563401Snarayan 			PR0("VOP_FSYNC returned errno %d", status);
6573401Snarayan 		}
6583401Snarayan 	}
6593401Snarayan 
6602336Snarayan 	if ((vd->initialized & VD_DRING) &&
6612336Snarayan 	    ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0))
6622793Slm66018 		PR0("ldc_mem_dring_unmap() returned errno %d", status);
6632793Slm66018 
6642793Slm66018 	vd_free_dring_task(vd);
6652793Slm66018 
6662793Slm66018 	/* Free the staging buffer for msgs */
6672793Slm66018 	if (vd->vio_msgp != NULL) {
6682793Slm66018 		kmem_free(vd->vio_msgp, vd->max_msglen);
6692793Slm66018 		vd->vio_msgp = NULL;
6702336Snarayan 	}
6712336Snarayan 
6722793Slm66018 	/* Free the inband message buffer */
6732793Slm66018 	if (vd->inband_task.msg != NULL) {
6742793Slm66018 		kmem_free(vd->inband_task.msg, vd->max_msglen);
6752793Slm66018 		vd->inband_task.msg = NULL;
6762793Slm66018 	}
6772336Snarayan 
6782336Snarayan 	mutex_enter(&vd->lock);
6792793Slm66018 
6802793Slm66018 	if (vd->reset_ldc)
6812793Slm66018 		PR0("taking down LDC channel");
6822410Slm66018 	if (vd->reset_ldc && ((status = ldc_down(vd->ldc_handle)) != 0))
6832793Slm66018 		PR0("ldc_down() returned errno %d", status);
6842336Snarayan 
6852336Snarayan 	vd->initialized	&= ~(VD_SID | VD_SEQ_NUM | VD_DRING);
6862336Snarayan 	vd->state	= VD_STATE_INIT;
6872336Snarayan 	vd->max_msglen	= sizeof (vio_msg_t);	/* baseline vio message size */
6882336Snarayan 
6892793Slm66018 	/* Allocate the staging buffer */
6902793Slm66018 	vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
6912793Slm66018 
6923010Slm66018 	PR0("calling ldc_up\n");
6933010Slm66018 	(void) ldc_up(vd->ldc_handle);
6942793Slm66018 
6952336Snarayan 	vd->reset_state	= B_FALSE;
6962336Snarayan 	vd->reset_ldc	= B_FALSE;
6972793Slm66018 
6982336Snarayan 	mutex_exit(&vd->lock);
6992336Snarayan }
7002336Snarayan 
7012793Slm66018 static void vd_recv_msg(void *arg);
7022793Slm66018 
7032793Slm66018 static void
7042793Slm66018 vd_mark_in_reset(vd_t *vd)
7052793Slm66018 {
7062793Slm66018 	int status;
7072793Slm66018 
7082793Slm66018 	PR0("vd_mark_in_reset: marking vd in reset\n");
7092793Slm66018 
7102793Slm66018 	vd_need_reset(vd, B_FALSE);
7112793Slm66018 	status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, DDI_SLEEP);
7122793Slm66018 	if (status == DDI_FAILURE) {
7132793Slm66018 		PR0("cannot schedule task to recv msg\n");
7142793Slm66018 		vd_need_reset(vd, B_TRUE);
7152793Slm66018 		return;
7162793Slm66018 	}
7172793Slm66018 }
7182793Slm66018 
7192336Snarayan static int
7203401Snarayan vd_mark_elem_done(vd_t *vd, int idx, int elem_status, int elem_nbytes)
7212336Snarayan {
7222336Snarayan 	boolean_t		accepted;
7232336Snarayan 	int			status;
7242336Snarayan 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
7252336Snarayan 
7262793Slm66018 	if (vd->reset_state)
7272793Slm66018 		return (0);
7282336Snarayan 
7292336Snarayan 	/* Acquire the element */
7302793Slm66018 	if (!vd->reset_state &&
7312793Slm66018 	    (status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) {
7322793Slm66018 		if (status == ECONNRESET) {
7332793Slm66018 			vd_mark_in_reset(vd);
7342793Slm66018 			return (0);
7352793Slm66018 		} else {
7362793Slm66018 			PR0("ldc_mem_dring_acquire() returned errno %d",
7372793Slm66018 			    status);
7382793Slm66018 			return (status);
7392793Slm66018 		}
7402336Snarayan 	}
7412336Snarayan 
7422336Snarayan 	/* Set the element's status and mark it done */
7432336Snarayan 	accepted = (elem->hdr.dstate == VIO_DESC_ACCEPTED);
7442336Snarayan 	if (accepted) {
7453401Snarayan 		elem->payload.nbytes	= elem_nbytes;
7462336Snarayan 		elem->payload.status	= elem_status;
7472336Snarayan 		elem->hdr.dstate	= VIO_DESC_DONE;
7482336Snarayan 	} else {
7492336Snarayan 		/* Perhaps client timed out waiting for I/O... */
7502793Slm66018 		PR0("element %u no longer \"accepted\"", idx);
7512336Snarayan 		VD_DUMP_DRING_ELEM(elem);
7522336Snarayan 	}
7532336Snarayan 	/* Release the element */
7542793Slm66018 	if (!vd->reset_state &&
7552793Slm66018 	    (status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) {
7562793Slm66018 		if (status == ECONNRESET) {
7572793Slm66018 			vd_mark_in_reset(vd);
7582793Slm66018 			return (0);
7592793Slm66018 		} else {
7602793Slm66018 			PR0("ldc_mem_dring_release() returned errno %d",
7612793Slm66018 			    status);
7622793Slm66018 			return (status);
7632793Slm66018 		}
7642336Snarayan 	}
7652336Snarayan 
7662336Snarayan 	return (accepted ? 0 : EINVAL);
7672336Snarayan }
7682336Snarayan 
7692336Snarayan static void
7702336Snarayan vd_complete_bio(void *arg)
7712336Snarayan {
7722336Snarayan 	int			status		= 0;
7732336Snarayan 	vd_task_t		*task		= (vd_task_t *)arg;
7742336Snarayan 	vd_t			*vd		= task->vd;
7752336Snarayan 	vd_dring_payload_t	*request	= task->request;
7762336Snarayan 	struct buf		*buf		= &task->buf;
7772336Snarayan 
7782336Snarayan 
7792336Snarayan 	ASSERT(vd != NULL);
7802336Snarayan 	ASSERT(request != NULL);
7812336Snarayan 	ASSERT(task->msg != NULL);
7822336Snarayan 	ASSERT(task->msglen >= sizeof (*task->msg));
7833401Snarayan 	ASSERT(!vd->file);
7842336Snarayan 
7852336Snarayan 	/* Wait for the I/O to complete */
7862336Snarayan 	request->status = biowait(buf);
7872336Snarayan 
7883401Snarayan 	/* return back the number of bytes read/written */
7893401Snarayan 	request->nbytes = buf->b_bcount - buf->b_resid;
7903401Snarayan 
7912531Snarayan 	/* Release the buffer */
7922793Slm66018 	if (!vd->reset_state)
7932793Slm66018 		status = ldc_mem_release(task->mhdl, 0, buf->b_bcount);
7942531Snarayan 	if (status) {
7952793Slm66018 		PR0("ldc_mem_release() returned errno %d copying to "
7962793Slm66018 		    "client", status);
7972793Slm66018 		if (status == ECONNRESET) {
7982793Slm66018 			vd_mark_in_reset(vd);
7992793Slm66018 		}
8001991Sheppo 	}
8012336Snarayan 
8022793Slm66018 	/* Unmap the memory, even if in reset */
8032531Snarayan 	status = ldc_mem_unmap(task->mhdl);
8042531Snarayan 	if (status) {
8052793Slm66018 		PR0("ldc_mem_unmap() returned errno %d copying to client",
8062531Snarayan 		    status);
8072793Slm66018 		if (status == ECONNRESET) {
8082793Slm66018 			vd_mark_in_reset(vd);
8092793Slm66018 		}
8102531Snarayan 	}
8112531Snarayan 
8122336Snarayan 	biofini(buf);
8132336Snarayan 
8142336Snarayan 	/* Update the dring element for a dring client */
8152793Slm66018 	if (!vd->reset_state && (status == 0) &&
8162793Slm66018 	    (vd->xfer_mode == VIO_DRING_MODE)) {
8173401Snarayan 		status = vd_mark_elem_done(vd, task->index,
8183401Snarayan 		    request->status, request->nbytes);
8192793Slm66018 		if (status == ECONNRESET)
8202793Slm66018 			vd_mark_in_reset(vd);
8212793Slm66018 	}
8222336Snarayan 
8232336Snarayan 	/*
8242336Snarayan 	 * If a transport error occurred, arrange to "nack" the message when
8252336Snarayan 	 * the final task in the descriptor element range completes
8262336Snarayan 	 */
8272336Snarayan 	if (status != 0)
8282336Snarayan 		task->msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
8292336Snarayan 
8302336Snarayan 	/*
8312336Snarayan 	 * Only the final task for a range of elements will respond to and
8322336Snarayan 	 * free the message
8332336Snarayan 	 */
8342793Slm66018 	if (task->type == VD_NONFINAL_RANGE_TASK) {
8352336Snarayan 		return;
8362793Slm66018 	}
8372336Snarayan 
8382336Snarayan 	/*
8392336Snarayan 	 * Send the "ack" or "nack" back to the client; if sending the message
8402336Snarayan 	 * via LDC fails, arrange to reset both the connection state and LDC
8412336Snarayan 	 * itself
8422336Snarayan 	 */
8432336Snarayan 	PR1("Sending %s",
8442336Snarayan 	    (task->msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK");
8452793Slm66018 	if (!vd->reset_state) {
8462793Slm66018 		status = send_msg(vd->ldc_handle, task->msg, task->msglen);
8472793Slm66018 		switch (status) {
8482793Slm66018 		case 0:
8492793Slm66018 			break;
8502793Slm66018 		case ECONNRESET:
8512793Slm66018 			vd_mark_in_reset(vd);
8522793Slm66018 			break;
8532793Slm66018 		default:
8542793Slm66018 			PR0("initiating full reset");
8552793Slm66018 			vd_need_reset(vd, B_TRUE);
8562793Slm66018 			break;
8572793Slm66018 		}
8582793Slm66018 	}
8591991Sheppo }
8601991Sheppo 
8612032Slm66018 static void
8622032Slm66018 vd_geom2dk_geom(void *vd_buf, void *ioctl_arg)
8632032Slm66018 {
8642032Slm66018 	VD_GEOM2DK_GEOM((vd_geom_t *)vd_buf, (struct dk_geom *)ioctl_arg);
8652032Slm66018 }
8662032Slm66018 
8672032Slm66018 static void
8682032Slm66018 vd_vtoc2vtoc(void *vd_buf, void *ioctl_arg)
8692032Slm66018 {
8702032Slm66018 	VD_VTOC2VTOC((vd_vtoc_t *)vd_buf, (struct vtoc *)ioctl_arg);
8712032Slm66018 }
8722032Slm66018 
8732032Slm66018 static void
8742032Slm66018 dk_geom2vd_geom(void *ioctl_arg, void *vd_buf)
8752032Slm66018 {
8762032Slm66018 	DK_GEOM2VD_GEOM((struct dk_geom *)ioctl_arg, (vd_geom_t *)vd_buf);
8772032Slm66018 }
8782032Slm66018 
8792032Slm66018 static void
8802032Slm66018 vtoc2vd_vtoc(void *ioctl_arg, void *vd_buf)
8812032Slm66018 {
8822032Slm66018 	VTOC2VD_VTOC((struct vtoc *)ioctl_arg, (vd_vtoc_t *)vd_buf);
8832032Slm66018 }
8842032Slm66018 
8852531Snarayan static void
8862531Snarayan vd_get_efi_in(void *vd_buf, void *ioctl_arg)
8872531Snarayan {
8882531Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
8892531Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
8902531Snarayan 
8912531Snarayan 	dk_efi->dki_lba = vd_efi->lba;
8922531Snarayan 	dk_efi->dki_length = vd_efi->length;
8932531Snarayan 	dk_efi->dki_data = kmem_zalloc(vd_efi->length, KM_SLEEP);
8942531Snarayan }
8952531Snarayan 
8962531Snarayan static void
8972531Snarayan vd_get_efi_out(void *ioctl_arg, void *vd_buf)
8982531Snarayan {
8992531Snarayan 	int len;
9002531Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
9012531Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
9022531Snarayan 
9032531Snarayan 	len = vd_efi->length;
9042531Snarayan 	DK_EFI2VD_EFI(dk_efi, vd_efi);
9052531Snarayan 	kmem_free(dk_efi->dki_data, len);
9062531Snarayan }
9072531Snarayan 
9082531Snarayan static void
9092531Snarayan vd_set_efi_in(void *vd_buf, void *ioctl_arg)
9102531Snarayan {
9112531Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
9122531Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
9132531Snarayan 
9142531Snarayan 	dk_efi->dki_data = kmem_alloc(vd_efi->length, KM_SLEEP);
9152531Snarayan 	VD_EFI2DK_EFI(vd_efi, dk_efi);
9162531Snarayan }
9172531Snarayan 
9182531Snarayan static void
9192531Snarayan vd_set_efi_out(void *ioctl_arg, void *vd_buf)
9202531Snarayan {
9212531Snarayan 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
9222531Snarayan 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
9232531Snarayan 
9242531Snarayan 	kmem_free(dk_efi->dki_data, vd_efi->length);
9252531Snarayan }
9262531Snarayan 
9272531Snarayan static int
9282531Snarayan vd_read_vtoc(ldi_handle_t handle, struct vtoc *vtoc, vd_disk_label_t *label)
9292531Snarayan {
9302531Snarayan 	int status, rval;
9312531Snarayan 	struct dk_gpt *efi;
9322531Snarayan 	size_t efi_len;
9332531Snarayan 
9342531Snarayan 	*label = VD_DISK_LABEL_UNK;
9352531Snarayan 
9362531Snarayan 	status = ldi_ioctl(handle, DKIOCGVTOC, (intptr_t)vtoc,
9372531Snarayan 	    (vd_open_flags | FKIOCTL), kcred, &rval);
9382531Snarayan 
9392531Snarayan 	if (status == 0) {
9402531Snarayan 		*label = VD_DISK_LABEL_VTOC;
9412531Snarayan 		return (0);
9422531Snarayan 	} else if (status != ENOTSUP) {
9432793Slm66018 		PR0("ldi_ioctl(DKIOCGVTOC) returned error %d", status);
9442531Snarayan 		return (status);
9452531Snarayan 	}
9462531Snarayan 
9472531Snarayan 	status = vds_efi_alloc_and_read(handle, &efi, &efi_len);
9482531Snarayan 
9492531Snarayan 	if (status) {
9502793Slm66018 		PR0("vds_efi_alloc_and_read returned error %d", status);
9512531Snarayan 		return (status);
9522531Snarayan 	}
9532531Snarayan 
9542531Snarayan 	*label = VD_DISK_LABEL_EFI;
9552531Snarayan 	vd_efi_to_vtoc(efi, vtoc);
9562531Snarayan 	vd_efi_free(efi, efi_len);
9572531Snarayan 
9582531Snarayan 	return (0);
9592531Snarayan }
9602531Snarayan 
961*3782Sachartre static ushort_t
9623401Snarayan vd_lbl2cksum(struct dk_label *label)
9633401Snarayan {
9643401Snarayan 	int	count;
965*3782Sachartre 	ushort_t sum, *sp;
9663401Snarayan 
9673401Snarayan 	count =	(sizeof (struct dk_label)) / (sizeof (short)) - 1;
968*3782Sachartre 	sp = (ushort_t *)label;
9693401Snarayan 	sum = 0;
9703401Snarayan 	while (count--) {
9713401Snarayan 		sum ^= *sp++;
9723401Snarayan 	}
9733401Snarayan 
9743401Snarayan 	return (sum);
9753401Snarayan }
9763401Snarayan 
9771991Sheppo static int
9782032Slm66018 vd_do_slice_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
9791991Sheppo {
9802531Snarayan 	dk_efi_t *dk_ioc;
981*3782Sachartre 	struct dk_label label;
982*3782Sachartre 	struct vtoc *vtoc;
9833401Snarayan 	int i;
9842531Snarayan 
9852531Snarayan 	switch (vd->vdisk_label) {
9862531Snarayan 
9872531Snarayan 	case VD_DISK_LABEL_VTOC:
9882531Snarayan 
9892531Snarayan 		switch (cmd) {
9902531Snarayan 		case DKIOCGGEOM:
9912531Snarayan 			ASSERT(ioctl_arg != NULL);
9922531Snarayan 			bcopy(&vd->dk_geom, ioctl_arg, sizeof (vd->dk_geom));
9932531Snarayan 			return (0);
9942531Snarayan 		case DKIOCGVTOC:
9952531Snarayan 			ASSERT(ioctl_arg != NULL);
9962531Snarayan 			bcopy(&vd->vtoc, ioctl_arg, sizeof (vd->vtoc));
9972531Snarayan 			return (0);
9983401Snarayan 		case DKIOCSVTOC:
9993401Snarayan 			if (!vd->file)
10003401Snarayan 				return (ENOTSUP);
10013401Snarayan 			ASSERT(ioctl_arg != NULL);
1002*3782Sachartre 			vtoc = (struct vtoc *)ioctl_arg;
1003*3782Sachartre 
1004*3782Sachartre 			if (vtoc->v_sanity != VTOC_SANE ||
1005*3782Sachartre 			    vtoc->v_sectorsz != DEV_BSIZE ||
1006*3782Sachartre 			    vtoc->v_nparts != V_NUMPAR)
1007*3782Sachartre 				return (EINVAL);
1008*3782Sachartre 
1009*3782Sachartre 			bzero(&label, sizeof (label));
1010*3782Sachartre 			label.dkl_ncyl = vd->dk_geom.dkg_ncyl;
1011*3782Sachartre 			label.dkl_acyl = vd->dk_geom.dkg_acyl;
1012*3782Sachartre 			label.dkl_pcyl = vd->dk_geom.dkg_pcyl;
1013*3782Sachartre 			label.dkl_nhead = vd->dk_geom.dkg_nhead;
1014*3782Sachartre 			label.dkl_nsect = vd->dk_geom.dkg_nsect;
1015*3782Sachartre 			label.dkl_intrlv = vd->dk_geom.dkg_intrlv;
1016*3782Sachartre 			label.dkl_apc = vd->dk_geom.dkg_apc;
1017*3782Sachartre 			label.dkl_rpm = vd->dk_geom.dkg_rpm;
1018*3782Sachartre 			label.dkl_write_reinstruct =
1019*3782Sachartre 				vd->dk_geom.dkg_write_reinstruct;
1020*3782Sachartre 			label.dkl_read_reinstruct =
1021*3782Sachartre 				vd->dk_geom.dkg_read_reinstruct;
1022*3782Sachartre 
1023*3782Sachartre 			label.dkl_vtoc.v_nparts = vtoc->v_nparts;
1024*3782Sachartre 			label.dkl_vtoc.v_sanity = vtoc->v_sanity;
1025*3782Sachartre 			label.dkl_vtoc.v_version = vtoc->v_version;
1026*3782Sachartre 			for (i = 0; i < vtoc->v_nparts; i++) {
1027*3782Sachartre 				label.dkl_vtoc.v_timestamp[i] =
1028*3782Sachartre 				    vtoc->timestamp[i];
1029*3782Sachartre 				label.dkl_vtoc.v_part[i].p_tag =
1030*3782Sachartre 				    vtoc->v_part[i].p_tag;
1031*3782Sachartre 				label.dkl_vtoc.v_part[i].p_flag =
1032*3782Sachartre 				    vtoc->v_part[i].p_flag;
1033*3782Sachartre 				label.dkl_map[i].dkl_cylno =
1034*3782Sachartre 					vtoc->v_part[i].p_start /
1035*3782Sachartre 					(label.dkl_nhead * label.dkl_nsect);
1036*3782Sachartre 				label.dkl_map[i].dkl_nblk =
1037*3782Sachartre 				    vtoc->v_part[i].p_size;
1038*3782Sachartre 			}
1039*3782Sachartre 			bcopy(vtoc->v_asciilabel, label.dkl_asciilabel,
1040*3782Sachartre 			    LEN_DKL_ASCII);
1041*3782Sachartre 			bcopy(vtoc->v_volume, label.dkl_vtoc.v_volume,
10423401Snarayan 			    LEN_DKL_VVOL);
1043*3782Sachartre 			bcopy(vtoc->v_bootinfo, label.dkl_vtoc.v_bootinfo,
1044*3782Sachartre 			    sizeof (vtoc->v_bootinfo));
10453401Snarayan 
10463401Snarayan 			/* re-compute checksum */
1047*3782Sachartre 			label.dkl_magic = DKL_MAGIC;
1048*3782Sachartre 			label.dkl_cksum = vd_lbl2cksum(&label);
1049*3782Sachartre 
1050*3782Sachartre 			/* write label to file */
1051*3782Sachartre 			if (VD_FILE_LABEL_WRITE(vd, &label) < 0)
1052*3782Sachartre 				return (EIO);
1053*3782Sachartre 
1054*3782Sachartre 			/* update the cached vdisk VTOC */
1055*3782Sachartre 			bcopy(vtoc, &vd->vtoc, sizeof (vd->vtoc));
10563401Snarayan 
10573401Snarayan 			return (0);
10582531Snarayan 		default:
10592531Snarayan 			return (ENOTSUP);
10602531Snarayan 		}
10612531Snarayan 
10622531Snarayan 	case VD_DISK_LABEL_EFI:
10632531Snarayan 
10642531Snarayan 		switch (cmd) {
10652531Snarayan 		case DKIOCGETEFI:
10662531Snarayan 			ASSERT(ioctl_arg != NULL);
10672531Snarayan 			dk_ioc = (dk_efi_t *)ioctl_arg;
10682531Snarayan 			if (dk_ioc->dki_length < vd->dk_efi.dki_length)
10692531Snarayan 				return (EINVAL);
10702531Snarayan 			bcopy(vd->dk_efi.dki_data, dk_ioc->dki_data,
10712531Snarayan 			    vd->dk_efi.dki_length);
10722531Snarayan 			return (0);
10732531Snarayan 		default:
10742531Snarayan 			return (ENOTSUP);
10752531Snarayan 		}
10762531Snarayan 
10771991Sheppo 	default:
10781991Sheppo 		return (ENOTSUP);
10791991Sheppo 	}
10801991Sheppo }
10811991Sheppo 
10821991Sheppo static int
10832032Slm66018 vd_do_ioctl(vd_t *vd, vd_dring_payload_t *request, void* buf, vd_ioctl_t *ioctl)
10841991Sheppo {
10851991Sheppo 	int	rval = 0, status;
10861991Sheppo 	size_t	nbytes = request->nbytes;	/* modifiable copy */
10871991Sheppo 
10881991Sheppo 
10891991Sheppo 	ASSERT(request->slice < vd->nslices);
10901991Sheppo 	PR0("Performing %s", ioctl->operation_name);
10911991Sheppo 
10922032Slm66018 	/* Get data from client and convert, if necessary */
10932032Slm66018 	if (ioctl->copyin != NULL)  {
10941991Sheppo 		ASSERT(nbytes != 0 && buf != NULL);
10951991Sheppo 		PR1("Getting \"arg\" data from client");
10961991Sheppo 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
10971991Sheppo 			    request->cookie, request->ncookies,
10981991Sheppo 			    LDC_COPY_IN)) != 0) {
10992793Slm66018 			PR0("ldc_mem_copy() returned errno %d "
11001991Sheppo 			    "copying from client", status);
11011991Sheppo 			return (status);
11021991Sheppo 		}
11032032Slm66018 
11042032Slm66018 		/* Convert client's data, if necessary */
11052032Slm66018 		if (ioctl->copyin == VD_IDENTITY)	/* use client buffer */
11062032Slm66018 			ioctl->arg = buf;
11072032Slm66018 		else	/* convert client vdisk operation data to ioctl data */
11082032Slm66018 			(ioctl->copyin)(buf, (void *)ioctl->arg);
11091991Sheppo 	}
11101991Sheppo 
11111991Sheppo 	/*
11121991Sheppo 	 * Handle single-slice block devices internally; otherwise, have the
11131991Sheppo 	 * real driver perform the ioctl()
11141991Sheppo 	 */
11153401Snarayan 	if (vd->file || (vd->vdisk_type == VD_DISK_TYPE_SLICE && !vd->pseudo)) {
11162032Slm66018 		if ((status = vd_do_slice_ioctl(vd, ioctl->cmd,
11172032Slm66018 			    (void *)ioctl->arg)) != 0)
11181991Sheppo 			return (status);
11191991Sheppo 	} else if ((status = ldi_ioctl(vd->ldi_handle[request->slice],
11202336Snarayan 		    ioctl->cmd, (intptr_t)ioctl->arg, (vd_open_flags | FKIOCTL),
11212336Snarayan 		    kcred, &rval)) != 0) {
11221991Sheppo 		PR0("ldi_ioctl(%s) = errno %d", ioctl->cmd_name, status);
11231991Sheppo 		return (status);
11241991Sheppo 	}
11251991Sheppo #ifdef DEBUG
11261991Sheppo 	if (rval != 0) {
11272793Slm66018 		PR0("%s set rval = %d, which is not being returned to client",
11281991Sheppo 		    ioctl->cmd_name, rval);
11291991Sheppo 	}
11301991Sheppo #endif /* DEBUG */
11311991Sheppo 
11322032Slm66018 	/* Convert data and send to client, if necessary */
11332032Slm66018 	if (ioctl->copyout != NULL)  {
11341991Sheppo 		ASSERT(nbytes != 0 && buf != NULL);
11351991Sheppo 		PR1("Sending \"arg\" data to client");
11362032Slm66018 
11372032Slm66018 		/* Convert ioctl data to vdisk operation data, if necessary */
11382032Slm66018 		if (ioctl->copyout != VD_IDENTITY)
11392032Slm66018 			(ioctl->copyout)((void *)ioctl->arg, buf);
11402032Slm66018 
11411991Sheppo 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
11421991Sheppo 			    request->cookie, request->ncookies,
11431991Sheppo 			    LDC_COPY_OUT)) != 0) {
11442793Slm66018 			PR0("ldc_mem_copy() returned errno %d "
11451991Sheppo 			    "copying to client", status);
11461991Sheppo 			return (status);
11471991Sheppo 		}
11481991Sheppo 	}
11491991Sheppo 
11501991Sheppo 	return (status);
11511991Sheppo }
11521991Sheppo 
11531991Sheppo #define	RNDSIZE(expr) P2ROUNDUP(sizeof (expr), sizeof (uint64_t))
11541991Sheppo static int
11552336Snarayan vd_ioctl(vd_task_t *task)
11561991Sheppo {
11573166Ssg70180 	int			i, status, rc;
11582336Snarayan 	void			*buf = NULL;
11592336Snarayan 	struct dk_geom		dk_geom = {0};
11602336Snarayan 	struct vtoc		vtoc = {0};
11612531Snarayan 	struct dk_efi		dk_efi = {0};
11622336Snarayan 	vd_t			*vd		= task->vd;
11632336Snarayan 	vd_dring_payload_t	*request	= task->request;
11642336Snarayan 	vd_ioctl_t		ioctl[] = {
11651991Sheppo 		/* Command (no-copy) operations */
11662032Slm66018 		{VD_OP_FLUSH, STRINGIZE(VD_OP_FLUSH), 0,
11672032Slm66018 		    DKIOCFLUSHWRITECACHE, STRINGIZE(DKIOCFLUSHWRITECACHE),
11682032Slm66018 		    NULL, NULL, NULL},
11691991Sheppo 
11701991Sheppo 		/* "Get" (copy-out) operations */
11712032Slm66018 		{VD_OP_GET_WCE, STRINGIZE(VD_OP_GET_WCE), RNDSIZE(int),
11722032Slm66018 		    DKIOCGETWCE, STRINGIZE(DKIOCGETWCE),
11732531Snarayan 		    NULL, VD_IDENTITY, VD_IDENTITY},
11742032Slm66018 		{VD_OP_GET_DISKGEOM, STRINGIZE(VD_OP_GET_DISKGEOM),
11752032Slm66018 		    RNDSIZE(vd_geom_t),
11762032Slm66018 		    DKIOCGGEOM, STRINGIZE(DKIOCGGEOM),
11772032Slm66018 		    &dk_geom, NULL, dk_geom2vd_geom},
11782032Slm66018 		{VD_OP_GET_VTOC, STRINGIZE(VD_OP_GET_VTOC), RNDSIZE(vd_vtoc_t),
11792032Slm66018 		    DKIOCGVTOC, STRINGIZE(DKIOCGVTOC),
11802032Slm66018 		    &vtoc, NULL, vtoc2vd_vtoc},
11812531Snarayan 		{VD_OP_GET_EFI, STRINGIZE(VD_OP_GET_EFI), RNDSIZE(vd_efi_t),
11822531Snarayan 		    DKIOCGETEFI, STRINGIZE(DKIOCGETEFI),
11832531Snarayan 		    &dk_efi, vd_get_efi_in, vd_get_efi_out},
11841991Sheppo 
11851991Sheppo 		/* "Set" (copy-in) operations */
11862032Slm66018 		{VD_OP_SET_WCE, STRINGIZE(VD_OP_SET_WCE), RNDSIZE(int),
11872032Slm66018 		    DKIOCSETWCE, STRINGIZE(DKIOCSETWCE),
11882531Snarayan 		    NULL, VD_IDENTITY, VD_IDENTITY},
11892032Slm66018 		{VD_OP_SET_DISKGEOM, STRINGIZE(VD_OP_SET_DISKGEOM),
11902032Slm66018 		    RNDSIZE(vd_geom_t),
11912032Slm66018 		    DKIOCSGEOM, STRINGIZE(DKIOCSGEOM),
11922032Slm66018 		    &dk_geom, vd_geom2dk_geom, NULL},
11932032Slm66018 		{VD_OP_SET_VTOC, STRINGIZE(VD_OP_SET_VTOC), RNDSIZE(vd_vtoc_t),
11942032Slm66018 		    DKIOCSVTOC, STRINGIZE(DKIOCSVTOC),
11952032Slm66018 		    &vtoc, vd_vtoc2vtoc, NULL},
11962531Snarayan 		{VD_OP_SET_EFI, STRINGIZE(VD_OP_SET_EFI), RNDSIZE(vd_efi_t),
11972531Snarayan 		    DKIOCSETEFI, STRINGIZE(DKIOCSETEFI),
11982531Snarayan 		    &dk_efi, vd_set_efi_in, vd_set_efi_out},
11991991Sheppo 	};
12001991Sheppo 	size_t		nioctls = (sizeof (ioctl))/(sizeof (ioctl[0]));
12011991Sheppo 
12021991Sheppo 
12032336Snarayan 	ASSERT(vd != NULL);
12042336Snarayan 	ASSERT(request != NULL);
12051991Sheppo 	ASSERT(request->slice < vd->nslices);
12061991Sheppo 
12071991Sheppo 	/*
12081991Sheppo 	 * Determine ioctl corresponding to caller's "operation" and
12091991Sheppo 	 * validate caller's "nbytes"
12101991Sheppo 	 */
12111991Sheppo 	for (i = 0; i < nioctls; i++) {
12121991Sheppo 		if (request->operation == ioctl[i].operation) {
12132032Slm66018 			/* LDC memory operations require 8-byte multiples */
12142032Slm66018 			ASSERT(ioctl[i].nbytes % sizeof (uint64_t) == 0);
12152032Slm66018 
12162531Snarayan 			if (request->operation == VD_OP_GET_EFI ||
12172531Snarayan 			    request->operation == VD_OP_SET_EFI) {
12182531Snarayan 				if (request->nbytes >= ioctl[i].nbytes)
12192531Snarayan 					break;
12202793Slm66018 				PR0("%s:  Expected at least nbytes = %lu, "
12212531Snarayan 				    "got %lu", ioctl[i].operation_name,
12222531Snarayan 				    ioctl[i].nbytes, request->nbytes);
12232531Snarayan 				return (EINVAL);
12242531Snarayan 			}
12252531Snarayan 
12262032Slm66018 			if (request->nbytes != ioctl[i].nbytes) {
12272793Slm66018 				PR0("%s:  Expected nbytes = %lu, got %lu",
12282032Slm66018 				    ioctl[i].operation_name, ioctl[i].nbytes,
12292032Slm66018 				    request->nbytes);
12301991Sheppo 				return (EINVAL);
12311991Sheppo 			}
12321991Sheppo 
12331991Sheppo 			break;
12341991Sheppo 		}
12351991Sheppo 	}
12361991Sheppo 	ASSERT(i < nioctls);	/* because "operation" already validated */
12371991Sheppo 
12381991Sheppo 	if (request->nbytes)
12391991Sheppo 		buf = kmem_zalloc(request->nbytes, KM_SLEEP);
12401991Sheppo 	status = vd_do_ioctl(vd, request, buf, &ioctl[i]);
12411991Sheppo 	if (request->nbytes)
12421991Sheppo 		kmem_free(buf, request->nbytes);
12433401Snarayan 	if (!vd->file && vd->vdisk_type == VD_DISK_TYPE_DISK &&
12442531Snarayan 	    (request->operation == VD_OP_SET_VTOC ||
12453166Ssg70180 	    request->operation == VD_OP_SET_EFI)) {
12463166Ssg70180 		/* update disk information */
12473166Ssg70180 		rc = vd_read_vtoc(vd->ldi_handle[0], &vd->vtoc,
12483166Ssg70180 		    &vd->vdisk_label);
12493166Ssg70180 		if (rc != 0)
12503166Ssg70180 			PR0("vd_read_vtoc return error %d", rc);
12513166Ssg70180 	}
12522336Snarayan 	PR0("Returning %d", status);
12531991Sheppo 	return (status);
12541991Sheppo }
12551991Sheppo 
12562531Snarayan static int
12572531Snarayan vd_get_devid(vd_task_t *task)
12582531Snarayan {
12592531Snarayan 	vd_t *vd = task->vd;
12602531Snarayan 	vd_dring_payload_t *request = task->request;
12612531Snarayan 	vd_devid_t *vd_devid;
12622531Snarayan 	impl_devid_t *devid;
12632531Snarayan 	int status, bufid_len, devid_len, len;
12642793Slm66018 	int bufbytes;
12652793Slm66018 
12662793Slm66018 	PR1("Get Device ID, nbytes=%ld", request->nbytes);
12672531Snarayan 
12683401Snarayan 	if (vd->file) {
12693401Snarayan 		/* no devid for disk on file */
12703401Snarayan 		return (ENOENT);
12713401Snarayan 	}
12723401Snarayan 
12732531Snarayan 	if (ddi_lyr_get_devid(vd->dev[request->slice],
12742531Snarayan 	    (ddi_devid_t *)&devid) != DDI_SUCCESS) {
12752531Snarayan 		/* the most common failure is that no devid is available */
12762793Slm66018 		PR2("No Device ID");
12772531Snarayan 		return (ENOENT);
12782531Snarayan 	}
12792531Snarayan 
12802531Snarayan 	bufid_len = request->nbytes - sizeof (vd_devid_t) + 1;
12812531Snarayan 	devid_len = DEVID_GETLEN(devid);
12822531Snarayan 
12832793Slm66018 	/*
12842793Slm66018 	 * Save the buffer size here for use in deallocation.
12852793Slm66018 	 * The actual number of bytes copied is returned in
12862793Slm66018 	 * the 'nbytes' field of the request structure.
12872793Slm66018 	 */
12882793Slm66018 	bufbytes = request->nbytes;
12892793Slm66018 
12902793Slm66018 	vd_devid = kmem_zalloc(bufbytes, KM_SLEEP);
12912531Snarayan 	vd_devid->length = devid_len;
12922531Snarayan 	vd_devid->type = DEVID_GETTYPE(devid);
12932531Snarayan 
12942531Snarayan 	len = (devid_len > bufid_len)? bufid_len : devid_len;
12952531Snarayan 
12962531Snarayan 	bcopy(devid->did_id, vd_devid->id, len);
12972531Snarayan 
12982531Snarayan 	/* LDC memory operations require 8-byte multiples */
12992531Snarayan 	ASSERT(request->nbytes % sizeof (uint64_t) == 0);
13002531Snarayan 
13012531Snarayan 	if ((status = ldc_mem_copy(vd->ldc_handle, (caddr_t)vd_devid, 0,
13022531Snarayan 	    &request->nbytes, request->cookie, request->ncookies,
13032531Snarayan 	    LDC_COPY_OUT)) != 0) {
13042793Slm66018 		PR0("ldc_mem_copy() returned errno %d copying to client",
13052531Snarayan 		    status);
13062531Snarayan 	}
13072793Slm66018 	PR1("post mem_copy: nbytes=%ld", request->nbytes);
13082793Slm66018 
13092793Slm66018 	kmem_free(vd_devid, bufbytes);
13102531Snarayan 	ddi_devid_free((ddi_devid_t)devid);
13112531Snarayan 
13122531Snarayan 	return (status);
13132531Snarayan }
13142531Snarayan 
13151991Sheppo /*
13161991Sheppo  * Define the supported operations once the functions for performing them have
13171991Sheppo  * been defined
13181991Sheppo  */
13191991Sheppo static const vds_operation_t	vds_operation[] = {
13202793Slm66018 #define	X(_s)	#_s, _s
13212793Slm66018 	{X(VD_OP_BREAD),	vd_start_bio,	vd_complete_bio},
13222793Slm66018 	{X(VD_OP_BWRITE),	vd_start_bio,	vd_complete_bio},
13232793Slm66018 	{X(VD_OP_FLUSH),	vd_ioctl,	NULL},
13242793Slm66018 	{X(VD_OP_GET_WCE),	vd_ioctl,	NULL},
13252793Slm66018 	{X(VD_OP_SET_WCE),	vd_ioctl,	NULL},
13262793Slm66018 	{X(VD_OP_GET_VTOC),	vd_ioctl,	NULL},
13272793Slm66018 	{X(VD_OP_SET_VTOC),	vd_ioctl,	NULL},
13282793Slm66018 	{X(VD_OP_GET_DISKGEOM),	vd_ioctl,	NULL},
13292793Slm66018 	{X(VD_OP_SET_DISKGEOM),	vd_ioctl,	NULL},
13302793Slm66018 	{X(VD_OP_GET_EFI),	vd_ioctl,	NULL},
13312793Slm66018 	{X(VD_OP_SET_EFI),	vd_ioctl,	NULL},
13322793Slm66018 	{X(VD_OP_GET_DEVID),	vd_get_devid,	NULL},
13332793Slm66018 #undef	X
13341991Sheppo };
13351991Sheppo 
13361991Sheppo static const size_t	vds_noperations =
13371991Sheppo 	(sizeof (vds_operation))/(sizeof (vds_operation[0]));
13381991Sheppo 
13391991Sheppo /*
13402336Snarayan  * Process a task specifying a client I/O request
13411991Sheppo  */
13421991Sheppo static int
13432336Snarayan vd_process_task(vd_task_t *task)
13441991Sheppo {
13452336Snarayan 	int			i, status;
13462336Snarayan 	vd_t			*vd		= task->vd;
13472336Snarayan 	vd_dring_payload_t	*request	= task->request;
13482336Snarayan 
13492336Snarayan 
13502336Snarayan 	ASSERT(vd != NULL);
13512336Snarayan 	ASSERT(request != NULL);
13521991Sheppo 
13532336Snarayan 	/* Find the requested operation */
13541991Sheppo 	for (i = 0; i < vds_noperations; i++)
13551991Sheppo 		if (request->operation == vds_operation[i].operation)
13562336Snarayan 			break;
13572336Snarayan 	if (i == vds_noperations) {
13582793Slm66018 		PR0("Unsupported operation %u", request->operation);
13592336Snarayan 		return (ENOTSUP);
13602336Snarayan 	}
13612336Snarayan 
13622748Slm66018 	/* Handle client using absolute disk offsets */
13632748Slm66018 	if ((vd->vdisk_type == VD_DISK_TYPE_DISK) &&
13642748Slm66018 	    (request->slice == UINT8_MAX))
13652748Slm66018 		request->slice = VD_ENTIRE_DISK_SLICE;
13662748Slm66018 
13672748Slm66018 	/* Range-check slice */
13682748Slm66018 	if (request->slice >= vd->nslices) {
13692793Slm66018 		PR0("Invalid \"slice\" %u (max %u) for virtual disk",
13702748Slm66018 		    request->slice, (vd->nslices - 1));
13712748Slm66018 		return (EINVAL);
13722748Slm66018 	}
13732748Slm66018 
13742793Slm66018 	PR1("operation : %s", vds_operation[i].namep);
13752793Slm66018 
13762336Snarayan 	/* Start the operation */
13772336Snarayan 	if ((status = vds_operation[i].start(task)) != EINPROGRESS) {
13782793Slm66018 		PR0("operation : %s returned status %d",
13792793Slm66018 			vds_operation[i].namep, status);
13802336Snarayan 		request->status = status;	/* op succeeded or failed */
13812336Snarayan 		return (0);			/* but request completed */
13821991Sheppo 	}
13831991Sheppo 
13842336Snarayan 	ASSERT(vds_operation[i].complete != NULL);	/* debug case */
13852336Snarayan 	if (vds_operation[i].complete == NULL) {	/* non-debug case */
13862793Slm66018 		PR0("Unexpected return of EINPROGRESS "
13872336Snarayan 		    "with no I/O completion handler");
13882336Snarayan 		request->status = EIO;	/* operation failed */
13892336Snarayan 		return (0);		/* but request completed */
13901991Sheppo 	}
13911991Sheppo 
13922793Slm66018 	PR1("operation : kick off taskq entry for %s", vds_operation[i].namep);
13932793Slm66018 
13942336Snarayan 	/* Queue a task to complete the operation */
13952336Snarayan 	status = ddi_taskq_dispatch(vd->completionq, vds_operation[i].complete,
13962336Snarayan 	    task, DDI_SLEEP);
13972336Snarayan 	/* ddi_taskq_dispatch(9f) guarantees success with DDI_SLEEP */
13982336Snarayan 	ASSERT(status == DDI_SUCCESS);
13992336Snarayan 
14002336Snarayan 	PR1("Operation in progress");
14012336Snarayan 	return (EINPROGRESS);	/* completion handler will finish request */
14021991Sheppo }
14031991Sheppo 
14041991Sheppo /*
14052032Slm66018  * Return true if the "type", "subtype", and "env" fields of the "tag" first
14062032Slm66018  * argument match the corresponding remaining arguments; otherwise, return false
14071991Sheppo  */
14082032Slm66018 boolean_t
14091991Sheppo vd_msgtype(vio_msg_tag_t *tag, int type, int subtype, int env)
14101991Sheppo {
14111991Sheppo 	return ((tag->vio_msgtype == type) &&
14121991Sheppo 		(tag->vio_subtype == subtype) &&
14132032Slm66018 		(tag->vio_subtype_env == env)) ? B_TRUE : B_FALSE;
14141991Sheppo }
14151991Sheppo 
14162032Slm66018 /*
14172032Slm66018  * Check whether the major/minor version specified in "ver_msg" is supported
14182032Slm66018  * by this server.
14192032Slm66018  */
14202032Slm66018 static boolean_t
14212032Slm66018 vds_supported_version(vio_ver_msg_t *ver_msg)
14222032Slm66018 {
14232032Slm66018 	for (int i = 0; i < vds_num_versions; i++) {
14242032Slm66018 		ASSERT(vds_version[i].major > 0);
14252032Slm66018 		ASSERT((i == 0) ||
14262032Slm66018 		    (vds_version[i].major < vds_version[i-1].major));
14272032Slm66018 
14282032Slm66018 		/*
14292032Slm66018 		 * If the major versions match, adjust the minor version, if
14302032Slm66018 		 * necessary, down to the highest value supported by this
14312032Slm66018 		 * server and return true so this message will get "ack"ed;
14322032Slm66018 		 * the client should also support all minor versions lower
14332032Slm66018 		 * than the value it sent
14342032Slm66018 		 */
14352032Slm66018 		if (ver_msg->ver_major == vds_version[i].major) {
14362032Slm66018 			if (ver_msg->ver_minor > vds_version[i].minor) {
14372032Slm66018 				PR0("Adjusting minor version from %u to %u",
14382032Slm66018 				    ver_msg->ver_minor, vds_version[i].minor);
14392032Slm66018 				ver_msg->ver_minor = vds_version[i].minor;
14402032Slm66018 			}
14412032Slm66018 			return (B_TRUE);
14422032Slm66018 		}
14432032Slm66018 
14442032Slm66018 		/*
14452032Slm66018 		 * If the message contains a higher major version number, set
14462032Slm66018 		 * the message's major/minor versions to the current values
14472032Slm66018 		 * and return false, so this message will get "nack"ed with
14482032Slm66018 		 * these values, and the client will potentially try again
14492032Slm66018 		 * with the same or a lower version
14502032Slm66018 		 */
14512032Slm66018 		if (ver_msg->ver_major > vds_version[i].major) {
14522032Slm66018 			ver_msg->ver_major = vds_version[i].major;
14532032Slm66018 			ver_msg->ver_minor = vds_version[i].minor;
14542032Slm66018 			return (B_FALSE);
14552032Slm66018 		}
14562032Slm66018 
14572032Slm66018 		/*
14582032Slm66018 		 * Otherwise, the message's major version is less than the
14592032Slm66018 		 * current major version, so continue the loop to the next
14602032Slm66018 		 * (lower) supported version
14612032Slm66018 		 */
14622032Slm66018 	}
14632032Slm66018 
14642032Slm66018 	/*
14652032Slm66018 	 * No common version was found; "ground" the version pair in the
14662032Slm66018 	 * message to terminate negotiation
14672032Slm66018 	 */
14682032Slm66018 	ver_msg->ver_major = 0;
14692032Slm66018 	ver_msg->ver_minor = 0;
14702032Slm66018 	return (B_FALSE);
14712032Slm66018 }
14722032Slm66018 
14732032Slm66018 /*
14742032Slm66018  * Process a version message from a client.  vds expects to receive version
14752032Slm66018  * messages from clients seeking service, but never issues version messages
14762032Slm66018  * itself; therefore, vds can ACK or NACK client version messages, but does
14772032Slm66018  * not expect to receive version-message ACKs or NACKs (and will treat such
14782032Slm66018  * messages as invalid).
14792032Slm66018  */
14801991Sheppo static int
14812032Slm66018 vd_process_ver_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
14821991Sheppo {
14831991Sheppo 	vio_ver_msg_t	*ver_msg = (vio_ver_msg_t *)msg;
14841991Sheppo 
14851991Sheppo 
14861991Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
14871991Sheppo 
14881991Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
14891991Sheppo 		VIO_VER_INFO)) {
14901991Sheppo 		return (ENOMSG);	/* not a version message */
14911991Sheppo 	}
14921991Sheppo 
14931991Sheppo 	if (msglen != sizeof (*ver_msg)) {
14942793Slm66018 		PR0("Expected %lu-byte version message; "
14951991Sheppo 		    "received %lu bytes", sizeof (*ver_msg), msglen);
14961991Sheppo 		return (EBADMSG);
14971991Sheppo 	}
14981991Sheppo 
14991991Sheppo 	if (ver_msg->dev_class != VDEV_DISK) {
15002793Slm66018 		PR0("Expected device class %u (disk); received %u",
15011991Sheppo 		    VDEV_DISK, ver_msg->dev_class);
15021991Sheppo 		return (EBADMSG);
15031991Sheppo 	}
15041991Sheppo 
15052032Slm66018 	/*
15062032Slm66018 	 * We're talking to the expected kind of client; set our device class
15072032Slm66018 	 * for "ack/nack" back to the client
15082032Slm66018 	 */
15092032Slm66018 	ver_msg->dev_class = VDEV_DISK_SERVER;
15102032Slm66018 
15112032Slm66018 	/*
15122032Slm66018 	 * Check whether the (valid) version message specifies a version
15132032Slm66018 	 * supported by this server.  If the version is not supported, return
15142032Slm66018 	 * EBADMSG so the message will get "nack"ed; vds_supported_version()
15152032Slm66018 	 * will have updated the message with a supported version for the
15162032Slm66018 	 * client to consider
15172032Slm66018 	 */
15182032Slm66018 	if (!vds_supported_version(ver_msg))
15191991Sheppo 		return (EBADMSG);
15202032Slm66018 
15212032Slm66018 
15222032Slm66018 	/*
15232032Slm66018 	 * A version has been agreed upon; use the client's SID for
15242032Slm66018 	 * communication on this channel now
15252032Slm66018 	 */
15262032Slm66018 	ASSERT(!(vd->initialized & VD_SID));
15272032Slm66018 	vd->sid = ver_msg->tag.vio_sid;
15282032Slm66018 	vd->initialized |= VD_SID;
15291991Sheppo 
15302032Slm66018 	/*
15312032Slm66018 	 * When multiple versions are supported, this function should store
15322032Slm66018 	 * the negotiated major and minor version values in the "vd" data
15332032Slm66018 	 * structure to govern further communication; in particular, note that
15342032Slm66018 	 * the client might have specified a lower minor version for the
15352032Slm66018 	 * agreed major version than specifed in the vds_version[] array.  The
15362032Slm66018 	 * following assertions should help remind future maintainers to make
15372032Slm66018 	 * the appropriate changes to support multiple versions.
15382032Slm66018 	 */
15392032Slm66018 	ASSERT(vds_num_versions == 1);
15402032Slm66018 	ASSERT(ver_msg->ver_major == vds_version[0].major);
15412032Slm66018 	ASSERT(ver_msg->ver_minor == vds_version[0].minor);
15422032Slm66018 
15432032Slm66018 	PR0("Using major version %u, minor version %u",
15442032Slm66018 	    ver_msg->ver_major, ver_msg->ver_minor);
15451991Sheppo 	return (0);
15461991Sheppo }
15471991Sheppo 
15481991Sheppo static int
15491991Sheppo vd_process_attr_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
15501991Sheppo {
15511991Sheppo 	vd_attr_msg_t	*attr_msg = (vd_attr_msg_t *)msg;
15523401Snarayan 	int		status, retry = 0;
15531991Sheppo 
15541991Sheppo 
15551991Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
15561991Sheppo 
15571991Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
15581991Sheppo 		VIO_ATTR_INFO)) {
15592336Snarayan 		PR0("Message is not an attribute message");
15602336Snarayan 		return (ENOMSG);
15611991Sheppo 	}
15621991Sheppo 
15631991Sheppo 	if (msglen != sizeof (*attr_msg)) {
15642793Slm66018 		PR0("Expected %lu-byte attribute message; "
15651991Sheppo 		    "received %lu bytes", sizeof (*attr_msg), msglen);
15661991Sheppo 		return (EBADMSG);
15671991Sheppo 	}
15681991Sheppo 
15691991Sheppo 	if (attr_msg->max_xfer_sz == 0) {
15702793Slm66018 		PR0("Received maximum transfer size of 0 from client");
15711991Sheppo 		return (EBADMSG);
15721991Sheppo 	}
15731991Sheppo 
15741991Sheppo 	if ((attr_msg->xfer_mode != VIO_DESC_MODE) &&
15751991Sheppo 	    (attr_msg->xfer_mode != VIO_DRING_MODE)) {
15762793Slm66018 		PR0("Client requested unsupported transfer mode");
15771991Sheppo 		return (EBADMSG);
15781991Sheppo 	}
15791991Sheppo 
15803401Snarayan 	/*
15813401Snarayan 	 * check if the underlying disk is ready, if not try accessing
15823401Snarayan 	 * the device again. Open the vdisk device and extract info
15833401Snarayan 	 * about it, as this is needed to respond to the attr info msg
15843401Snarayan 	 */
15853401Snarayan 	if ((vd->initialized & VD_DISK_READY) == 0) {
15863401Snarayan 		PR0("Retry setting up disk (%s)", vd->device_path);
15873401Snarayan 		do {
15883401Snarayan 			status = vd_setup_vd(vd);
15893401Snarayan 			if (status != EAGAIN || ++retry > vds_dev_retries)
15903401Snarayan 				break;
15913401Snarayan 
15923401Snarayan 			/* incremental delay */
15933401Snarayan 			delay(drv_usectohz(vds_dev_delay));
15943401Snarayan 
15953401Snarayan 			/* if vdisk is no longer enabled - return error */
15963401Snarayan 			if (!vd_enabled(vd))
15973401Snarayan 				return (ENXIO);
15983401Snarayan 
15993401Snarayan 		} while (status == EAGAIN);
16003401Snarayan 
16013401Snarayan 		if (status)
16023401Snarayan 			return (ENXIO);
16033401Snarayan 
16043401Snarayan 		vd->initialized |= VD_DISK_READY;
16053401Snarayan 		ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
16063401Snarayan 		PR0("vdisk_type = %s, pseudo = %s, file = %s, nslices = %u",
16073401Snarayan 		    ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
16083401Snarayan 		    (vd->pseudo ? "yes" : "no"),
16093401Snarayan 		    (vd->file ? "yes" : "no"),
16103401Snarayan 		    vd->nslices);
16113401Snarayan 	}
16123401Snarayan 
16131991Sheppo 	/* Success:  valid message and transfer mode */
16141991Sheppo 	vd->xfer_mode = attr_msg->xfer_mode;
16152793Slm66018 
16161991Sheppo 	if (vd->xfer_mode == VIO_DESC_MODE) {
16172793Slm66018 
16181991Sheppo 		/*
16191991Sheppo 		 * The vd_dring_inband_msg_t contains one cookie; need room
16201991Sheppo 		 * for up to n-1 more cookies, where "n" is the number of full
16211991Sheppo 		 * pages plus possibly one partial page required to cover
16221991Sheppo 		 * "max_xfer_sz".  Add room for one more cookie if
16231991Sheppo 		 * "max_xfer_sz" isn't an integral multiple of the page size.
16241991Sheppo 		 * Must first get the maximum transfer size in bytes.
16251991Sheppo 		 */
16261991Sheppo 		size_t	max_xfer_bytes = attr_msg->vdisk_block_size ?
16271991Sheppo 		    attr_msg->vdisk_block_size*attr_msg->max_xfer_sz :
16281991Sheppo 		    attr_msg->max_xfer_sz;
16291991Sheppo 		size_t	max_inband_msglen =
16301991Sheppo 		    sizeof (vd_dring_inband_msg_t) +
16311991Sheppo 		    ((max_xfer_bytes/PAGESIZE +
16321991Sheppo 			((max_xfer_bytes % PAGESIZE) ? 1 : 0))*
16331991Sheppo 			(sizeof (ldc_mem_cookie_t)));
16341991Sheppo 
16351991Sheppo 		/*
16361991Sheppo 		 * Set the maximum expected message length to
16371991Sheppo 		 * accommodate in-band-descriptor messages with all
16381991Sheppo 		 * their cookies
16391991Sheppo 		 */
16401991Sheppo 		vd->max_msglen = MAX(vd->max_msglen, max_inband_msglen);
16412336Snarayan 
16422336Snarayan 		/*
16432336Snarayan 		 * Initialize the data structure for processing in-band I/O
16442336Snarayan 		 * request descriptors
16452336Snarayan 		 */
16462336Snarayan 		vd->inband_task.vd	= vd;
16472793Slm66018 		vd->inband_task.msg	= kmem_alloc(vd->max_msglen, KM_SLEEP);
16482336Snarayan 		vd->inband_task.index	= 0;
16492336Snarayan 		vd->inband_task.type	= VD_FINAL_RANGE_TASK;	/* range == 1 */
16501991Sheppo 	}
16511991Sheppo 
16522410Slm66018 	/* Return the device's block size and max transfer size to the client */
16532410Slm66018 	attr_msg->vdisk_block_size	= DEV_BSIZE;
16542410Slm66018 	attr_msg->max_xfer_sz		= vd->max_xfer_sz;
16552410Slm66018 
16561991Sheppo 	attr_msg->vdisk_size = vd->vdisk_size;
16571991Sheppo 	attr_msg->vdisk_type = vd->vdisk_type;
16581991Sheppo 	attr_msg->operations = vds_operations;
16591991Sheppo 	PR0("%s", VD_CLIENT(vd));
16602793Slm66018 
16612793Slm66018 	ASSERT(vd->dring_task == NULL);
16622793Slm66018 
16631991Sheppo 	return (0);
16641991Sheppo }
16651991Sheppo 
16661991Sheppo static int
16671991Sheppo vd_process_dring_reg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
16681991Sheppo {
16691991Sheppo 	int			status;
16701991Sheppo 	size_t			expected;
16711991Sheppo 	ldc_mem_info_t		dring_minfo;
16721991Sheppo 	vio_dring_reg_msg_t	*reg_msg = (vio_dring_reg_msg_t *)msg;
16731991Sheppo 
16741991Sheppo 
16751991Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
16761991Sheppo 
16771991Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
16781991Sheppo 		VIO_DRING_REG)) {
16792336Snarayan 		PR0("Message is not a register-dring message");
16802336Snarayan 		return (ENOMSG);
16811991Sheppo 	}
16821991Sheppo 
16831991Sheppo 	if (msglen < sizeof (*reg_msg)) {
16842793Slm66018 		PR0("Expected at least %lu-byte register-dring message; "
16851991Sheppo 		    "received %lu bytes", sizeof (*reg_msg), msglen);
16861991Sheppo 		return (EBADMSG);
16871991Sheppo 	}
16881991Sheppo 
16891991Sheppo 	expected = sizeof (*reg_msg) +
16901991Sheppo 	    (reg_msg->ncookies - 1)*(sizeof (reg_msg->cookie[0]));
16911991Sheppo 	if (msglen != expected) {
16922793Slm66018 		PR0("Expected %lu-byte register-dring message; "
16931991Sheppo 		    "received %lu bytes", expected, msglen);
16941991Sheppo 		return (EBADMSG);
16951991Sheppo 	}
16961991Sheppo 
16971991Sheppo 	if (vd->initialized & VD_DRING) {
16982793Slm66018 		PR0("A dring was previously registered; only support one");
16991991Sheppo 		return (EBADMSG);
17001991Sheppo 	}
17011991Sheppo 
17022336Snarayan 	if (reg_msg->num_descriptors > INT32_MAX) {
17032793Slm66018 		PR0("reg_msg->num_descriptors = %u; must be <= %u (%s)",
17042336Snarayan 		    reg_msg->ncookies, INT32_MAX, STRINGIZE(INT32_MAX));
17052336Snarayan 		return (EBADMSG);
17062336Snarayan 	}
17072336Snarayan 
17081991Sheppo 	if (reg_msg->ncookies != 1) {
17091991Sheppo 		/*
17101991Sheppo 		 * In addition to fixing the assertion in the success case
17111991Sheppo 		 * below, supporting drings which require more than one
17121991Sheppo 		 * "cookie" requires increasing the value of vd->max_msglen
17131991Sheppo 		 * somewhere in the code path prior to receiving the message
17141991Sheppo 		 * which results in calling this function.  Note that without
17151991Sheppo 		 * making this change, the larger message size required to
17161991Sheppo 		 * accommodate multiple cookies cannot be successfully
17171991Sheppo 		 * received, so this function will not even get called.
17181991Sheppo 		 * Gracefully accommodating more dring cookies might
17191991Sheppo 		 * reasonably demand exchanging an additional attribute or
17201991Sheppo 		 * making a minor protocol adjustment
17211991Sheppo 		 */
17222793Slm66018 		PR0("reg_msg->ncookies = %u != 1", reg_msg->ncookies);
17231991Sheppo 		return (EBADMSG);
17241991Sheppo 	}
17251991Sheppo 
17261991Sheppo 	status = ldc_mem_dring_map(vd->ldc_handle, reg_msg->cookie,
17271991Sheppo 	    reg_msg->ncookies, reg_msg->num_descriptors,
17282531Snarayan 	    reg_msg->descriptor_size, LDC_DIRECT_MAP, &vd->dring_handle);
17291991Sheppo 	if (status != 0) {
17302793Slm66018 		PR0("ldc_mem_dring_map() returned errno %d", status);
17311991Sheppo 		return (status);
17321991Sheppo 	}
17331991Sheppo 
17341991Sheppo 	/*
17351991Sheppo 	 * To remove the need for this assertion, must call
17361991Sheppo 	 * ldc_mem_dring_nextcookie() successfully ncookies-1 times after a
17371991Sheppo 	 * successful call to ldc_mem_dring_map()
17381991Sheppo 	 */
17391991Sheppo 	ASSERT(reg_msg->ncookies == 1);
17401991Sheppo 
17411991Sheppo 	if ((status =
17421991Sheppo 		ldc_mem_dring_info(vd->dring_handle, &dring_minfo)) != 0) {
17432793Slm66018 		PR0("ldc_mem_dring_info() returned errno %d", status);
17441991Sheppo 		if ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0)
17452793Slm66018 			PR0("ldc_mem_dring_unmap() returned errno %d", status);
17461991Sheppo 		return (status);
17471991Sheppo 	}
17481991Sheppo 
17491991Sheppo 	if (dring_minfo.vaddr == NULL) {
17502793Slm66018 		PR0("Descriptor ring virtual address is NULL");
17512032Slm66018 		return (ENXIO);
17521991Sheppo 	}
17531991Sheppo 
17541991Sheppo 
17552336Snarayan 	/* Initialize for valid message and mapped dring */
17561991Sheppo 	PR1("descriptor size = %u, dring length = %u",
17571991Sheppo 	    vd->descriptor_size, vd->dring_len);
17581991Sheppo 	vd->initialized |= VD_DRING;
17591991Sheppo 	vd->dring_ident = 1;	/* "There Can Be Only One" */
17601991Sheppo 	vd->dring = dring_minfo.vaddr;
17611991Sheppo 	vd->descriptor_size = reg_msg->descriptor_size;
17621991Sheppo 	vd->dring_len = reg_msg->num_descriptors;
17631991Sheppo 	reg_msg->dring_ident = vd->dring_ident;
17642336Snarayan 
17652336Snarayan 	/*
17662336Snarayan 	 * Allocate and initialize a "shadow" array of data structures for
17672336Snarayan 	 * tasks to process I/O requests in dring elements
17682336Snarayan 	 */
17692336Snarayan 	vd->dring_task =
17702336Snarayan 	    kmem_zalloc((sizeof (*vd->dring_task)) * vd->dring_len, KM_SLEEP);
17712336Snarayan 	for (int i = 0; i < vd->dring_len; i++) {
17722336Snarayan 		vd->dring_task[i].vd		= vd;
17732336Snarayan 		vd->dring_task[i].index		= i;
17742336Snarayan 		vd->dring_task[i].request	= &VD_DRING_ELEM(i)->payload;
17752531Snarayan 
17762531Snarayan 		status = ldc_mem_alloc_handle(vd->ldc_handle,
17772531Snarayan 		    &(vd->dring_task[i].mhdl));
17782531Snarayan 		if (status) {
17792793Slm66018 			PR0("ldc_mem_alloc_handle() returned err %d ", status);
17802531Snarayan 			return (ENXIO);
17812531Snarayan 		}
17822793Slm66018 
17832793Slm66018 		vd->dring_task[i].msg = kmem_alloc(vd->max_msglen, KM_SLEEP);
17842336Snarayan 	}
17852336Snarayan 
17861991Sheppo 	return (0);
17871991Sheppo }
17881991Sheppo 
17891991Sheppo static int
17901991Sheppo vd_process_dring_unreg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
17911991Sheppo {
17921991Sheppo 	vio_dring_unreg_msg_t	*unreg_msg = (vio_dring_unreg_msg_t *)msg;
17931991Sheppo 
17941991Sheppo 
17951991Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
17961991Sheppo 
17971991Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
17981991Sheppo 		VIO_DRING_UNREG)) {
17992336Snarayan 		PR0("Message is not an unregister-dring message");
18002336Snarayan 		return (ENOMSG);
18011991Sheppo 	}
18021991Sheppo 
18031991Sheppo 	if (msglen != sizeof (*unreg_msg)) {
18042793Slm66018 		PR0("Expected %lu-byte unregister-dring message; "
18051991Sheppo 		    "received %lu bytes", sizeof (*unreg_msg), msglen);
18061991Sheppo 		return (EBADMSG);
18071991Sheppo 	}
18081991Sheppo 
18091991Sheppo 	if (unreg_msg->dring_ident != vd->dring_ident) {
18102793Slm66018 		PR0("Expected dring ident %lu; received %lu",
18111991Sheppo 		    vd->dring_ident, unreg_msg->dring_ident);
18121991Sheppo 		return (EBADMSG);
18131991Sheppo 	}
18141991Sheppo 
18151991Sheppo 	return (0);
18161991Sheppo }
18171991Sheppo 
18181991Sheppo static int
18191991Sheppo process_rdx_msg(vio_msg_t *msg, size_t msglen)
18201991Sheppo {
18211991Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
18221991Sheppo 
18232336Snarayan 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX)) {
18242336Snarayan 		PR0("Message is not an RDX message");
18252336Snarayan 		return (ENOMSG);
18262336Snarayan 	}
18271991Sheppo 
18281991Sheppo 	if (msglen != sizeof (vio_rdx_msg_t)) {
18292793Slm66018 		PR0("Expected %lu-byte RDX message; received %lu bytes",
18301991Sheppo 		    sizeof (vio_rdx_msg_t), msglen);
18311991Sheppo 		return (EBADMSG);
18321991Sheppo 	}
18331991Sheppo 
18342336Snarayan 	PR0("Valid RDX message");
18351991Sheppo 	return (0);
18361991Sheppo }
18371991Sheppo 
18381991Sheppo static int
18391991Sheppo vd_check_seq_num(vd_t *vd, uint64_t seq_num)
18401991Sheppo {
18411991Sheppo 	if ((vd->initialized & VD_SEQ_NUM) && (seq_num != vd->seq_num + 1)) {
18422793Slm66018 		PR0("Received seq_num %lu; expected %lu",
18431991Sheppo 		    seq_num, (vd->seq_num + 1));
18442793Slm66018 		PR0("initiating soft reset");
18452336Snarayan 		vd_need_reset(vd, B_FALSE);
18461991Sheppo 		return (1);
18471991Sheppo 	}
18481991Sheppo 
18491991Sheppo 	vd->seq_num = seq_num;
18501991Sheppo 	vd->initialized |= VD_SEQ_NUM;	/* superfluous after first time... */
18511991Sheppo 	return (0);
18521991Sheppo }
18531991Sheppo 
18541991Sheppo /*
18551991Sheppo  * Return the expected size of an inband-descriptor message with all the
18561991Sheppo  * cookies it claims to include
18571991Sheppo  */
18581991Sheppo static size_t
18591991Sheppo expected_inband_size(vd_dring_inband_msg_t *msg)
18601991Sheppo {
18611991Sheppo 	return ((sizeof (*msg)) +
18621991Sheppo 	    (msg->payload.ncookies - 1)*(sizeof (msg->payload.cookie[0])));
18631991Sheppo }
18641991Sheppo 
18651991Sheppo /*
18661991Sheppo  * Process an in-band descriptor message:  used with clients like OBP, with
18671991Sheppo  * which vds exchanges descriptors within VIO message payloads, rather than
18681991Sheppo  * operating on them within a descriptor ring
18691991Sheppo  */
18701991Sheppo static int
18712793Slm66018 vd_process_desc_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
18721991Sheppo {
18731991Sheppo 	size_t			expected;
18741991Sheppo 	vd_dring_inband_msg_t	*desc_msg = (vd_dring_inband_msg_t *)msg;
18751991Sheppo 
18761991Sheppo 
18771991Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
18781991Sheppo 
18791991Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
18802336Snarayan 		VIO_DESC_DATA)) {
18812336Snarayan 		PR1("Message is not an in-band-descriptor message");
18822336Snarayan 		return (ENOMSG);
18832336Snarayan 	}
18841991Sheppo 
18851991Sheppo 	if (msglen < sizeof (*desc_msg)) {
18862793Slm66018 		PR0("Expected at least %lu-byte descriptor message; "
18871991Sheppo 		    "received %lu bytes", sizeof (*desc_msg), msglen);
18881991Sheppo 		return (EBADMSG);
18891991Sheppo 	}
18901991Sheppo 
18911991Sheppo 	if (msglen != (expected = expected_inband_size(desc_msg))) {
18922793Slm66018 		PR0("Expected %lu-byte descriptor message; "
18931991Sheppo 		    "received %lu bytes", expected, msglen);
18941991Sheppo 		return (EBADMSG);
18951991Sheppo 	}
18961991Sheppo 
18972336Snarayan 	if (vd_check_seq_num(vd, desc_msg->hdr.seq_num) != 0)
18981991Sheppo 		return (EBADMSG);
18992336Snarayan 
19002336Snarayan 	/*
19012336Snarayan 	 * Valid message:  Set up the in-band descriptor task and process the
19022336Snarayan 	 * request.  Arrange to acknowledge the client's message, unless an
19032336Snarayan 	 * error processing the descriptor task results in setting
19042336Snarayan 	 * VIO_SUBTYPE_NACK
19052336Snarayan 	 */
19062336Snarayan 	PR1("Valid in-band-descriptor message");
19072336Snarayan 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
19082793Slm66018 
19092793Slm66018 	ASSERT(vd->inband_task.msg != NULL);
19102793Slm66018 
19112793Slm66018 	bcopy(msg, vd->inband_task.msg, msglen);
19122336Snarayan 	vd->inband_task.msglen	= msglen;
19132793Slm66018 
19142793Slm66018 	/*
19152793Slm66018 	 * The task request is now the payload of the message
19162793Slm66018 	 * that was just copied into the body of the task.
19172793Slm66018 	 */
19182793Slm66018 	desc_msg = (vd_dring_inband_msg_t *)vd->inband_task.msg;
19192336Snarayan 	vd->inband_task.request	= &desc_msg->payload;
19202793Slm66018 
19212336Snarayan 	return (vd_process_task(&vd->inband_task));
19221991Sheppo }
19231991Sheppo 
19241991Sheppo static int
19252336Snarayan vd_process_element(vd_t *vd, vd_task_type_t type, uint32_t idx,
19262793Slm66018     vio_msg_t *msg, size_t msglen)
19271991Sheppo {
19282336Snarayan 	int			status;
19292336Snarayan 	boolean_t		ready;
19302336Snarayan 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
19312336Snarayan 
19322336Snarayan 
19332336Snarayan 	/* Accept the updated dring element */
19342336Snarayan 	if ((status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) {
19352793Slm66018 		PR0("ldc_mem_dring_acquire() returned errno %d", status);
19361991Sheppo 		return (status);
19371991Sheppo 	}
19382336Snarayan 	ready = (elem->hdr.dstate == VIO_DESC_READY);
19392336Snarayan 	if (ready) {
19402336Snarayan 		elem->hdr.dstate = VIO_DESC_ACCEPTED;
19412336Snarayan 	} else {
19422793Slm66018 		PR0("descriptor %u not ready", idx);
19432336Snarayan 		VD_DUMP_DRING_ELEM(elem);
19442336Snarayan 	}
19452336Snarayan 	if ((status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) {
19462793Slm66018 		PR0("ldc_mem_dring_release() returned errno %d", status);
19471991Sheppo 		return (status);
19481991Sheppo 	}
19492336Snarayan 	if (!ready)
19502336Snarayan 		return (EBUSY);
19512336Snarayan 
19522336Snarayan 
19532336Snarayan 	/* Initialize a task and process the accepted element */
19542336Snarayan 	PR1("Processing dring element %u", idx);
19552336Snarayan 	vd->dring_task[idx].type	= type;
19562793Slm66018 
19572793Slm66018 	/* duplicate msg buf for cookies etc. */
19582793Slm66018 	bcopy(msg, vd->dring_task[idx].msg, msglen);
19592793Slm66018 
19602336Snarayan 	vd->dring_task[idx].msglen	= msglen;
19612336Snarayan 	if ((status = vd_process_task(&vd->dring_task[idx])) != EINPROGRESS)
19623401Snarayan 		status = vd_mark_elem_done(vd, idx,
19633401Snarayan 		    vd->dring_task[idx].request->status,
19643401Snarayan 		    vd->dring_task[idx].request->nbytes);
19652336Snarayan 
19662336Snarayan 	return (status);
19671991Sheppo }
19681991Sheppo 
19691991Sheppo static int
19702336Snarayan vd_process_element_range(vd_t *vd, int start, int end,
19712793Slm66018     vio_msg_t *msg, size_t msglen)
19722336Snarayan {
19732336Snarayan 	int		i, n, nelem, status = 0;
19742336Snarayan 	boolean_t	inprogress = B_FALSE;
19752336Snarayan 	vd_task_type_t	type;
19762336Snarayan 
19772336Snarayan 
19782336Snarayan 	ASSERT(start >= 0);
19792336Snarayan 	ASSERT(end >= 0);
19802336Snarayan 
19812336Snarayan 	/*
19822336Snarayan 	 * Arrange to acknowledge the client's message, unless an error
19832336Snarayan 	 * processing one of the dring elements results in setting
19842336Snarayan 	 * VIO_SUBTYPE_NACK
19852336Snarayan 	 */
19862336Snarayan 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
19872336Snarayan 
19882336Snarayan 	/*
19892336Snarayan 	 * Process the dring elements in the range
19902336Snarayan 	 */
19912336Snarayan 	nelem = ((end < start) ? end + vd->dring_len : end) - start + 1;
19922336Snarayan 	for (i = start, n = nelem; n > 0; i = (i + 1) % vd->dring_len, n--) {
19932336Snarayan 		((vio_dring_msg_t *)msg)->end_idx = i;
19942336Snarayan 		type = (n == 1) ? VD_FINAL_RANGE_TASK : VD_NONFINAL_RANGE_TASK;
19952793Slm66018 		status = vd_process_element(vd, type, i, msg, msglen);
19962336Snarayan 		if (status == EINPROGRESS)
19972336Snarayan 			inprogress = B_TRUE;
19982336Snarayan 		else if (status != 0)
19992336Snarayan 			break;
20002336Snarayan 	}
20012336Snarayan 
20022336Snarayan 	/*
20032336Snarayan 	 * If some, but not all, operations of a multi-element range are in
20042336Snarayan 	 * progress, wait for other operations to complete before returning
20052336Snarayan 	 * (which will result in "ack" or "nack" of the message).  Note that
20062336Snarayan 	 * all outstanding operations will need to complete, not just the ones
20072336Snarayan 	 * corresponding to the current range of dring elements; howevever, as
20082336Snarayan 	 * this situation is an error case, performance is less critical.
20092336Snarayan 	 */
20102336Snarayan 	if ((nelem > 1) && (status != EINPROGRESS) && inprogress)
20112336Snarayan 		ddi_taskq_wait(vd->completionq);
20122336Snarayan 
20132336Snarayan 	return (status);
20142336Snarayan }
20152336Snarayan 
20162336Snarayan static int
20172793Slm66018 vd_process_dring_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
20181991Sheppo {
20191991Sheppo 	vio_dring_msg_t	*dring_msg = (vio_dring_msg_t *)msg;
20201991Sheppo 
20211991Sheppo 
20221991Sheppo 	ASSERT(msglen >= sizeof (msg->tag));
20231991Sheppo 
20241991Sheppo 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
20251991Sheppo 		VIO_DRING_DATA)) {
20262336Snarayan 		PR1("Message is not a dring-data message");
20272336Snarayan 		return (ENOMSG);
20281991Sheppo 	}
20291991Sheppo 
20301991Sheppo 	if (msglen != sizeof (*dring_msg)) {
20312793Slm66018 		PR0("Expected %lu-byte dring message; received %lu bytes",
20321991Sheppo 		    sizeof (*dring_msg), msglen);
20331991Sheppo 		return (EBADMSG);
20341991Sheppo 	}
20351991Sheppo 
20362336Snarayan 	if (vd_check_seq_num(vd, dring_msg->seq_num) != 0)
20371991Sheppo 		return (EBADMSG);
20381991Sheppo 
20391991Sheppo 	if (dring_msg->dring_ident != vd->dring_ident) {
20402793Slm66018 		PR0("Expected dring ident %lu; received ident %lu",
20411991Sheppo 		    vd->dring_ident, dring_msg->dring_ident);
20421991Sheppo 		return (EBADMSG);
20431991Sheppo 	}
20441991Sheppo 
20452336Snarayan 	if (dring_msg->start_idx >= vd->dring_len) {
20462793Slm66018 		PR0("\"start_idx\" = %u; must be less than %u",
20472336Snarayan 		    dring_msg->start_idx, vd->dring_len);
20482336Snarayan 		return (EBADMSG);
20492336Snarayan 	}
20502336Snarayan 
20512336Snarayan 	if ((dring_msg->end_idx < 0) ||
20522336Snarayan 	    (dring_msg->end_idx >= vd->dring_len)) {
20532793Slm66018 		PR0("\"end_idx\" = %u; must be >= 0 and less than %u",
20542336Snarayan 		    dring_msg->end_idx, vd->dring_len);
20552336Snarayan 		return (EBADMSG);
20562336Snarayan 	}
20572336Snarayan 
20582336Snarayan 	/* Valid message; process range of updated dring elements */
20592336Snarayan 	PR1("Processing descriptor range, start = %u, end = %u",
20602336Snarayan 	    dring_msg->start_idx, dring_msg->end_idx);
20612336Snarayan 	return (vd_process_element_range(vd, dring_msg->start_idx,
20622793Slm66018 		dring_msg->end_idx, msg, msglen));
20631991Sheppo }
20641991Sheppo 
20651991Sheppo static int
20661991Sheppo recv_msg(ldc_handle_t ldc_handle, void *msg, size_t *nbytes)
20671991Sheppo {
20681991Sheppo 	int	retry, status;
20691991Sheppo 	size_t	size = *nbytes;
20701991Sheppo 
20711991Sheppo 
20721991Sheppo 	for (retry = 0, status = ETIMEDOUT;
20731991Sheppo 	    retry < vds_ldc_retries && status == ETIMEDOUT;
20741991Sheppo 	    retry++) {
20751991Sheppo 		PR1("ldc_read() attempt %d", (retry + 1));
20761991Sheppo 		*nbytes = size;
20771991Sheppo 		status = ldc_read(ldc_handle, msg, nbytes);
20781991Sheppo 	}
20791991Sheppo 
20802793Slm66018 	if (status) {
20812793Slm66018 		PR0("ldc_read() returned errno %d", status);
20822793Slm66018 		if (status != ECONNRESET)
20832793Slm66018 			return (ENOMSG);
20841991Sheppo 		return (status);
20851991Sheppo 	} else if (*nbytes == 0) {
20861991Sheppo 		PR1("ldc_read() returned 0 and no message read");
20871991Sheppo 		return (ENOMSG);
20881991Sheppo 	}
20891991Sheppo 
20901991Sheppo 	PR1("RCVD %lu-byte message", *nbytes);
20911991Sheppo 	return (0);
20921991Sheppo }
20931991Sheppo 
20941991Sheppo static int
20952793Slm66018 vd_do_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
20961991Sheppo {
20971991Sheppo 	int		status;
20981991Sheppo 
20991991Sheppo 
21001991Sheppo 	PR1("Processing (%x/%x/%x) message", msg->tag.vio_msgtype,
21011991Sheppo 	    msg->tag.vio_subtype, msg->tag.vio_subtype_env);
21022793Slm66018 #ifdef	DEBUG
21032793Slm66018 	vd_decode_tag(msg);
21042793Slm66018 #endif
21051991Sheppo 
21061991Sheppo 	/*
21071991Sheppo 	 * Validate session ID up front, since it applies to all messages
21081991Sheppo 	 * once set
21091991Sheppo 	 */
21101991Sheppo 	if ((msg->tag.vio_sid != vd->sid) && (vd->initialized & VD_SID)) {
21112793Slm66018 		PR0("Expected SID %u, received %u", vd->sid,
21121991Sheppo 		    msg->tag.vio_sid);
21131991Sheppo 		return (EBADMSG);
21141991Sheppo 	}
21151991Sheppo 
21162793Slm66018 	PR1("\tWhile in state %d (%s)", vd->state, vd_decode_state(vd->state));
21171991Sheppo 
21181991Sheppo 	/*
21191991Sheppo 	 * Process the received message based on connection state
21201991Sheppo 	 */
21211991Sheppo 	switch (vd->state) {
21221991Sheppo 	case VD_STATE_INIT:	/* expect version message */
21232032Slm66018 		if ((status = vd_process_ver_msg(vd, msg, msglen)) != 0)
21241991Sheppo 			return (status);
21251991Sheppo 
21261991Sheppo 		/* Version negotiated, move to that state */
21271991Sheppo 		vd->state = VD_STATE_VER;
21281991Sheppo 		return (0);
21291991Sheppo 
21301991Sheppo 	case VD_STATE_VER:	/* expect attribute message */
21311991Sheppo 		if ((status = vd_process_attr_msg(vd, msg, msglen)) != 0)
21321991Sheppo 			return (status);
21331991Sheppo 
21341991Sheppo 		/* Attributes exchanged, move to that state */
21351991Sheppo 		vd->state = VD_STATE_ATTR;
21361991Sheppo 		return (0);
21371991Sheppo 
21381991Sheppo 	case VD_STATE_ATTR:
21391991Sheppo 		switch (vd->xfer_mode) {
21401991Sheppo 		case VIO_DESC_MODE:	/* expect RDX message */
21411991Sheppo 			if ((status = process_rdx_msg(msg, msglen)) != 0)
21421991Sheppo 				return (status);
21431991Sheppo 
21441991Sheppo 			/* Ready to receive in-band descriptors */
21451991Sheppo 			vd->state = VD_STATE_DATA;
21461991Sheppo 			return (0);
21471991Sheppo 
21481991Sheppo 		case VIO_DRING_MODE:	/* expect register-dring message */
21491991Sheppo 			if ((status =
21501991Sheppo 				vd_process_dring_reg_msg(vd, msg, msglen)) != 0)
21511991Sheppo 				return (status);
21521991Sheppo 
21531991Sheppo 			/* One dring negotiated, move to that state */
21541991Sheppo 			vd->state = VD_STATE_DRING;
21551991Sheppo 			return (0);
21561991Sheppo 
21571991Sheppo 		default:
21581991Sheppo 			ASSERT("Unsupported transfer mode");
21592793Slm66018 			PR0("Unsupported transfer mode");
21601991Sheppo 			return (ENOTSUP);
21611991Sheppo 		}
21621991Sheppo 
21631991Sheppo 	case VD_STATE_DRING:	/* expect RDX, register-dring, or unreg-dring */
21641991Sheppo 		if ((status = process_rdx_msg(msg, msglen)) == 0) {
21651991Sheppo 			/* Ready to receive data */
21661991Sheppo 			vd->state = VD_STATE_DATA;
21671991Sheppo 			return (0);
21681991Sheppo 		} else if (status != ENOMSG) {
21691991Sheppo 			return (status);
21701991Sheppo 		}
21711991Sheppo 
21721991Sheppo 
21731991Sheppo 		/*
21741991Sheppo 		 * If another register-dring message is received, stay in
21751991Sheppo 		 * dring state in case the client sends RDX; although the
21761991Sheppo 		 * protocol allows multiple drings, this server does not
21771991Sheppo 		 * support using more than one
21781991Sheppo 		 */
21791991Sheppo 		if ((status =
21801991Sheppo 			vd_process_dring_reg_msg(vd, msg, msglen)) != ENOMSG)
21811991Sheppo 			return (status);
21821991Sheppo 
21831991Sheppo 		/*
21841991Sheppo 		 * Acknowledge an unregister-dring message, but reset the
21851991Sheppo 		 * connection anyway:  Although the protocol allows
21861991Sheppo 		 * unregistering drings, this server cannot serve a vdisk
21871991Sheppo 		 * without its only dring
21881991Sheppo 		 */
21891991Sheppo 		status = vd_process_dring_unreg_msg(vd, msg, msglen);
21901991Sheppo 		return ((status == 0) ? ENOTSUP : status);
21911991Sheppo 
21921991Sheppo 	case VD_STATE_DATA:
21931991Sheppo 		switch (vd->xfer_mode) {
21941991Sheppo 		case VIO_DESC_MODE:	/* expect in-band-descriptor message */
21952793Slm66018 			return (vd_process_desc_msg(vd, msg, msglen));
21961991Sheppo 
21971991Sheppo 		case VIO_DRING_MODE:	/* expect dring-data or unreg-dring */
21981991Sheppo 			/*
21991991Sheppo 			 * Typically expect dring-data messages, so handle
22001991Sheppo 			 * them first
22011991Sheppo 			 */
22021991Sheppo 			if ((status = vd_process_dring_msg(vd, msg,
22032793Slm66018 				    msglen)) != ENOMSG)
22041991Sheppo 				return (status);
22051991Sheppo 
22061991Sheppo 			/*
22071991Sheppo 			 * Acknowledge an unregister-dring message, but reset
22081991Sheppo 			 * the connection anyway:  Although the protocol
22091991Sheppo 			 * allows unregistering drings, this server cannot
22101991Sheppo 			 * serve a vdisk without its only dring
22111991Sheppo 			 */
22121991Sheppo 			status = vd_process_dring_unreg_msg(vd, msg, msglen);
22131991Sheppo 			return ((status == 0) ? ENOTSUP : status);
22141991Sheppo 
22151991Sheppo 		default:
22161991Sheppo 			ASSERT("Unsupported transfer mode");
22172793Slm66018 			PR0("Unsupported transfer mode");
22181991Sheppo 			return (ENOTSUP);
22191991Sheppo 		}
22201991Sheppo 
22211991Sheppo 	default:
22221991Sheppo 		ASSERT("Invalid client connection state");
22232793Slm66018 		PR0("Invalid client connection state");
22241991Sheppo 		return (ENOTSUP);
22251991Sheppo 	}
22261991Sheppo }
22271991Sheppo 
22282336Snarayan static int
22292793Slm66018 vd_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
22301991Sheppo {
22311991Sheppo 	int		status;
22321991Sheppo 	boolean_t	reset_ldc = B_FALSE;
22331991Sheppo 
22341991Sheppo 
22351991Sheppo 	/*
22361991Sheppo 	 * Check that the message is at least big enough for a "tag", so that
22371991Sheppo 	 * message processing can proceed based on tag-specified message type
22381991Sheppo 	 */
22391991Sheppo 	if (msglen < sizeof (vio_msg_tag_t)) {
22402793Slm66018 		PR0("Received short (%lu-byte) message", msglen);
22411991Sheppo 		/* Can't "nack" short message, so drop the big hammer */
22422793Slm66018 		PR0("initiating full reset");
22432336Snarayan 		vd_need_reset(vd, B_TRUE);
22442336Snarayan 		return (EBADMSG);
22451991Sheppo 	}
22461991Sheppo 
22471991Sheppo 	/*
22481991Sheppo 	 * Process the message
22491991Sheppo 	 */
22502793Slm66018 	switch (status = vd_do_process_msg(vd, msg, msglen)) {
22511991Sheppo 	case 0:
22521991Sheppo 		/* "ack" valid, successfully-processed messages */
22531991Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
22541991Sheppo 		break;
22551991Sheppo 
22562336Snarayan 	case EINPROGRESS:
22572336Snarayan 		/* The completion handler will "ack" or "nack" the message */
22582336Snarayan 		return (EINPROGRESS);
22591991Sheppo 	case ENOMSG:
22602793Slm66018 		PR0("Received unexpected message");
22611991Sheppo 		_NOTE(FALLTHROUGH);
22621991Sheppo 	case EBADMSG:
22631991Sheppo 	case ENOTSUP:
22641991Sheppo 		/* "nack" invalid messages */
22651991Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
22661991Sheppo 		break;
22671991Sheppo 
22681991Sheppo 	default:
22691991Sheppo 		/* "nack" failed messages */
22701991Sheppo 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
22711991Sheppo 		/* An LDC error probably occurred, so try resetting it */
22721991Sheppo 		reset_ldc = B_TRUE;
22731991Sheppo 		break;
22741991Sheppo 	}
22751991Sheppo 
22762793Slm66018 	PR1("\tResulting in state %d (%s)", vd->state,
22772793Slm66018 		vd_decode_state(vd->state));
22782793Slm66018 
22792336Snarayan 	/* Send the "ack" or "nack" to the client */
22801991Sheppo 	PR1("Sending %s",
22811991Sheppo 	    (msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK");
22821991Sheppo 	if (send_msg(vd->ldc_handle, msg, msglen) != 0)
22831991Sheppo 		reset_ldc = B_TRUE;
22841991Sheppo 
22852336Snarayan 	/* Arrange to reset the connection for nack'ed or failed messages */
22862793Slm66018 	if ((status != 0) || reset_ldc) {
22872793Slm66018 		PR0("initiating %s reset",
22882793Slm66018 		    (reset_ldc) ? "full" : "soft");
22892336Snarayan 		vd_need_reset(vd, reset_ldc);
22902793Slm66018 	}
22912336Snarayan 
22922336Snarayan 	return (status);
22932336Snarayan }
22942336Snarayan 
22952336Snarayan static boolean_t
22962336Snarayan vd_enabled(vd_t *vd)
22972336Snarayan {
22982336Snarayan 	boolean_t	enabled;
22992336Snarayan 
23002336Snarayan 
23012336Snarayan 	mutex_enter(&vd->lock);
23022336Snarayan 	enabled = vd->enabled;
23032336Snarayan 	mutex_exit(&vd->lock);
23042336Snarayan 	return (enabled);
23051991Sheppo }
23061991Sheppo 
23071991Sheppo static void
23082032Slm66018 vd_recv_msg(void *arg)
23091991Sheppo {
23102032Slm66018 	vd_t	*vd = (vd_t *)arg;
23112793Slm66018 	int	rv = 0, status = 0;
23121991Sheppo 
23131991Sheppo 	ASSERT(vd != NULL);
23142793Slm66018 
23152336Snarayan 	PR2("New task to receive incoming message(s)");
23162793Slm66018 
23172793Slm66018 
23182336Snarayan 	while (vd_enabled(vd) && status == 0) {
23192336Snarayan 		size_t		msglen, msgsize;
23202793Slm66018 		ldc_status_t	lstatus;
23212336Snarayan 
23222336Snarayan 		/*
23232336Snarayan 		 * Receive and process a message
23242336Snarayan 		 */
23252336Snarayan 		vd_reset_if_needed(vd);	/* can change vd->max_msglen */
23262793Slm66018 
23272793Slm66018 		/*
23282793Slm66018 		 * check if channel is UP - else break out of loop
23292793Slm66018 		 */
23302793Slm66018 		status = ldc_status(vd->ldc_handle, &lstatus);
23312793Slm66018 		if (lstatus != LDC_UP) {
23322793Slm66018 			PR0("channel not up (status=%d), exiting recv loop\n",
23332793Slm66018 			    lstatus);
23342793Slm66018 			break;
23352793Slm66018 		}
23362793Slm66018 
23372793Slm66018 		ASSERT(vd->max_msglen != 0);
23382793Slm66018 
23392793Slm66018 		msgsize = vd->max_msglen; /* stable copy for alloc/free */
23402793Slm66018 		msglen	= msgsize;	  /* actual len after recv_msg() */
23412793Slm66018 
23422793Slm66018 		status = recv_msg(vd->ldc_handle, vd->vio_msgp, &msglen);
23432793Slm66018 		switch (status) {
23442793Slm66018 		case 0:
23452793Slm66018 			rv = vd_process_msg(vd, (vio_msg_t *)vd->vio_msgp,
23462793Slm66018 				msglen);
23472793Slm66018 			/* check if max_msglen changed */
23482793Slm66018 			if (msgsize != vd->max_msglen) {
23492793Slm66018 				PR0("max_msglen changed 0x%lx to 0x%lx bytes\n",
23502793Slm66018 				    msgsize, vd->max_msglen);
23512793Slm66018 				kmem_free(vd->vio_msgp, msgsize);
23522793Slm66018 				vd->vio_msgp =
23532793Slm66018 					kmem_alloc(vd->max_msglen, KM_SLEEP);
23542793Slm66018 			}
23552793Slm66018 			if (rv == EINPROGRESS)
23562793Slm66018 				continue;
23572793Slm66018 			break;
23582793Slm66018 
23592793Slm66018 		case ENOMSG:
23602793Slm66018 			break;
23612793Slm66018 
23622793Slm66018 		case ECONNRESET:
23632793Slm66018 			PR0("initiating soft reset (ECONNRESET)\n");
23642793Slm66018 			vd_need_reset(vd, B_FALSE);
23652793Slm66018 			status = 0;
23662793Slm66018 			break;
23672793Slm66018 
23682793Slm66018 		default:
23692336Snarayan 			/* Probably an LDC failure; arrange to reset it */
23702793Slm66018 			PR0("initiating full reset (status=0x%x)", status);
23712336Snarayan 			vd_need_reset(vd, B_TRUE);
23722793Slm66018 			break;
23732336Snarayan 		}
23742032Slm66018 	}
23752793Slm66018 
23762336Snarayan 	PR2("Task finished");
23772032Slm66018 }
23782032Slm66018 
23792032Slm66018 static uint_t
23801991Sheppo vd_handle_ldc_events(uint64_t event, caddr_t arg)
23811991Sheppo {
23821991Sheppo 	vd_t	*vd = (vd_t *)(void *)arg;
23832793Slm66018 	int	status;
23841991Sheppo 
23851991Sheppo 	ASSERT(vd != NULL);
23862336Snarayan 
23872336Snarayan 	if (!vd_enabled(vd))
23882336Snarayan 		return (LDC_SUCCESS);
23892336Snarayan 
23902793Slm66018 	if (event & LDC_EVT_DOWN) {
23913166Ssg70180 		PR0("LDC_EVT_DOWN: LDC channel went down");
23922793Slm66018 
23932793Slm66018 		vd_need_reset(vd, B_TRUE);
23942793Slm66018 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
23952793Slm66018 		    DDI_SLEEP);
23962793Slm66018 		if (status == DDI_FAILURE) {
23972793Slm66018 			PR0("cannot schedule task to recv msg\n");
23982793Slm66018 			vd_need_reset(vd, B_TRUE);
23992793Slm66018 		}
24002793Slm66018 	}
24012793Slm66018 
24022336Snarayan 	if (event & LDC_EVT_RESET) {
24032793Slm66018 		PR0("LDC_EVT_RESET: LDC channel was reset");
24042793Slm66018 
24052793Slm66018 		if (vd->state != VD_STATE_INIT) {
24062793Slm66018 			PR0("scheduling full reset");
24072793Slm66018 			vd_need_reset(vd, B_FALSE);
24082793Slm66018 			status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
24092793Slm66018 			    vd, DDI_SLEEP);
24102793Slm66018 			if (status == DDI_FAILURE) {
24112793Slm66018 				PR0("cannot schedule task to recv msg\n");
24122793Slm66018 				vd_need_reset(vd, B_TRUE);
24132793Slm66018 			}
24142793Slm66018 
24152793Slm66018 		} else {
24162793Slm66018 			PR0("channel already reset, ignoring...\n");
24172793Slm66018 			PR0("doing ldc up...\n");
24182793Slm66018 			(void) ldc_up(vd->ldc_handle);
24192793Slm66018 		}
24202793Slm66018 
24212336Snarayan 		return (LDC_SUCCESS);
24222336Snarayan 	}
24232336Snarayan 
24242336Snarayan 	if (event & LDC_EVT_UP) {
24252793Slm66018 		PR0("EVT_UP: LDC is up\nResetting client connection state");
24262793Slm66018 		PR0("initiating soft reset");
24272336Snarayan 		vd_need_reset(vd, B_FALSE);
24282793Slm66018 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
24292793Slm66018 		    vd, DDI_SLEEP);
24302793Slm66018 		if (status == DDI_FAILURE) {
24312793Slm66018 			PR0("cannot schedule task to recv msg\n");
24322793Slm66018 			vd_need_reset(vd, B_TRUE);
24332793Slm66018 			return (LDC_SUCCESS);
24342793Slm66018 		}
24352336Snarayan 	}
24362336Snarayan 
24372336Snarayan 	if (event & LDC_EVT_READ) {
24382336Snarayan 		int	status;
24392336Snarayan 
24402336Snarayan 		PR1("New data available");
24412336Snarayan 		/* Queue a task to receive the new data */
24422336Snarayan 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
24432336Snarayan 		    DDI_SLEEP);
24442793Slm66018 
24452793Slm66018 		if (status == DDI_FAILURE) {
24462793Slm66018 			PR0("cannot schedule task to recv msg\n");
24472793Slm66018 			vd_need_reset(vd, B_TRUE);
24482793Slm66018 		}
24492336Snarayan 	}
24502336Snarayan 
24512336Snarayan 	return (LDC_SUCCESS);
24521991Sheppo }
24531991Sheppo 
24541991Sheppo static uint_t
24551991Sheppo vds_check_for_vd(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
24561991Sheppo {
24571991Sheppo 	_NOTE(ARGUNUSED(key, val))
24581991Sheppo 	(*((uint_t *)arg))++;
24591991Sheppo 	return (MH_WALK_TERMINATE);
24601991Sheppo }
24611991Sheppo 
24621991Sheppo 
24631991Sheppo static int
24641991Sheppo vds_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
24651991Sheppo {
24661991Sheppo 	uint_t	vd_present = 0;
24671991Sheppo 	minor_t	instance;
24681991Sheppo 	vds_t	*vds;
24691991Sheppo 
24701991Sheppo 
24711991Sheppo 	switch (cmd) {
24721991Sheppo 	case DDI_DETACH:
24731991Sheppo 		/* the real work happens below */
24741991Sheppo 		break;
24751991Sheppo 	case DDI_SUSPEND:
24762336Snarayan 		PR0("No action required for DDI_SUSPEND");
24771991Sheppo 		return (DDI_SUCCESS);
24781991Sheppo 	default:
24792793Slm66018 		PR0("Unrecognized \"cmd\"");
24801991Sheppo 		return (DDI_FAILURE);
24811991Sheppo 	}
24821991Sheppo 
24831991Sheppo 	ASSERT(cmd == DDI_DETACH);
24841991Sheppo 	instance = ddi_get_instance(dip);
24851991Sheppo 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
24862793Slm66018 		PR0("Could not get state for instance %u", instance);
24871991Sheppo 		ddi_soft_state_free(vds_state, instance);
24881991Sheppo 		return (DDI_FAILURE);
24891991Sheppo 	}
24901991Sheppo 
24911991Sheppo 	/* Do no detach when serving any vdisks */
24921991Sheppo 	mod_hash_walk(vds->vd_table, vds_check_for_vd, &vd_present);
24931991Sheppo 	if (vd_present) {
24941991Sheppo 		PR0("Not detaching because serving vdisks");
24951991Sheppo 		return (DDI_FAILURE);
24961991Sheppo 	}
24971991Sheppo 
24981991Sheppo 	PR0("Detaching");
24993297Ssb155480 	if (vds->initialized & VDS_MDEG) {
25001991Sheppo 		(void) mdeg_unregister(vds->mdeg);
25013297Ssb155480 		kmem_free(vds->ispecp->specp, sizeof (vds_prop_template));
25023297Ssb155480 		kmem_free(vds->ispecp, sizeof (mdeg_node_spec_t));
25033297Ssb155480 		vds->ispecp = NULL;
25043297Ssb155480 		vds->mdeg = NULL;
25053297Ssb155480 	}
25063297Ssb155480 
25071991Sheppo 	if (vds->initialized & VDS_LDI)
25081991Sheppo 		(void) ldi_ident_release(vds->ldi_ident);
25091991Sheppo 	mod_hash_destroy_hash(vds->vd_table);
25101991Sheppo 	ddi_soft_state_free(vds_state, instance);
25111991Sheppo 	return (DDI_SUCCESS);
25121991Sheppo }
25131991Sheppo 
25141991Sheppo static boolean_t
25151991Sheppo is_pseudo_device(dev_info_t *dip)
25161991Sheppo {
25171991Sheppo 	dev_info_t	*parent, *root = ddi_root_node();
25181991Sheppo 
25191991Sheppo 
25201991Sheppo 	for (parent = ddi_get_parent(dip); (parent != NULL) && (parent != root);
25211991Sheppo 	    parent = ddi_get_parent(parent)) {
25221991Sheppo 		if (strcmp(ddi_get_name(parent), DEVI_PSEUDO_NEXNAME) == 0)
25231991Sheppo 			return (B_TRUE);
25241991Sheppo 	}
25251991Sheppo 
25261991Sheppo 	return (B_FALSE);
25271991Sheppo }
25281991Sheppo 
25291991Sheppo static int
25302032Slm66018 vd_setup_full_disk(vd_t *vd)
25312032Slm66018 {
25322032Slm66018 	int		rval, status;
25332032Slm66018 	major_t		major = getmajor(vd->dev[0]);
25342032Slm66018 	minor_t		minor = getminor(vd->dev[0]) - VD_ENTIRE_DISK_SLICE;
25352531Snarayan 	struct dk_minfo	dk_minfo;
25362531Snarayan 
25372531Snarayan 	/*
25382531Snarayan 	 * At this point, vdisk_size is set to the size of partition 2 but
25392531Snarayan 	 * this does not represent the size of the disk because partition 2
25402531Snarayan 	 * may not cover the entire disk and its size does not include reserved
25412531Snarayan 	 * blocks. So we update vdisk_size to be the size of the entire disk.
25422531Snarayan 	 */
25432531Snarayan 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCGMEDIAINFO,
25442531Snarayan 	    (intptr_t)&dk_minfo, (vd_open_flags | FKIOCTL),
25452531Snarayan 	    kcred, &rval)) != 0) {
2546*3782Sachartre 		PRN("ldi_ioctl(DKIOCGMEDIAINFO) returned errno %d",
25472531Snarayan 		    status);
25482032Slm66018 		return (status);
25492032Slm66018 	}
25502531Snarayan 	vd->vdisk_size = dk_minfo.dki_capacity;
25512032Slm66018 
25522032Slm66018 	/* Set full-disk parameters */
25532032Slm66018 	vd->vdisk_type	= VD_DISK_TYPE_DISK;
25542032Slm66018 	vd->nslices	= (sizeof (vd->dev))/(sizeof (vd->dev[0]));
25552032Slm66018 
25562032Slm66018 	/* Move dev number and LDI handle to entire-disk-slice array elements */
25572032Slm66018 	vd->dev[VD_ENTIRE_DISK_SLICE]		= vd->dev[0];
25582032Slm66018 	vd->dev[0]				= 0;
25592032Slm66018 	vd->ldi_handle[VD_ENTIRE_DISK_SLICE]	= vd->ldi_handle[0];
25602032Slm66018 	vd->ldi_handle[0]			= NULL;
25612032Slm66018 
25622032Slm66018 	/* Initialize device numbers for remaining slices and open them */
25632032Slm66018 	for (int slice = 0; slice < vd->nslices; slice++) {
25642032Slm66018 		/*
25652032Slm66018 		 * Skip the entire-disk slice, as it's already open and its
25662032Slm66018 		 * device known
25672032Slm66018 		 */
25682032Slm66018 		if (slice == VD_ENTIRE_DISK_SLICE)
25692032Slm66018 			continue;
25702032Slm66018 		ASSERT(vd->dev[slice] == 0);
25712032Slm66018 		ASSERT(vd->ldi_handle[slice] == NULL);
25722032Slm66018 
25732032Slm66018 		/*
25742032Slm66018 		 * Construct the device number for the current slice
25752032Slm66018 		 */
25762032Slm66018 		vd->dev[slice] = makedevice(major, (minor + slice));
25772032Slm66018 
25782032Slm66018 		/*
25793166Ssg70180 		 * Open all slices of the disk to serve them to the client.
25803166Ssg70180 		 * Slices are opened exclusively to prevent other threads or
25813166Ssg70180 		 * processes in the service domain from performing I/O to
25823166Ssg70180 		 * slices being accessed by a client.  Failure to open a slice
25833166Ssg70180 		 * results in vds not serving this disk, as the client could
25843166Ssg70180 		 * attempt (and should be able) to access any slice immediately.
25853166Ssg70180 		 * Any slices successfully opened before a failure will get
25863166Ssg70180 		 * closed by vds_destroy_vd() as a result of the error returned
25873166Ssg70180 		 * by this function.
25883166Ssg70180 		 *
25893166Ssg70180 		 * We need to do the open with FNDELAY so that opening an empty
25903166Ssg70180 		 * slice does not fail.
25912032Slm66018 		 */
25922032Slm66018 		PR0("Opening device major %u, minor %u = slice %u",
25932032Slm66018 		    major, minor, slice);
25942032Slm66018 		if ((status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK,
25953166Ssg70180 		    vd_open_flags | FNDELAY, kcred, &vd->ldi_handle[slice],
25963166Ssg70180 		    vd->vds->ldi_ident)) != 0) {
2597*3782Sachartre 			PRN("ldi_open_by_dev() returned errno %d "
25982032Slm66018 			    "for slice %u", status, slice);
25992032Slm66018 			/* vds_destroy_vd() will close any open slices */
2600*3782Sachartre 			vd->ldi_handle[slice] = NULL;
26012032Slm66018 			return (status);
26022032Slm66018 		}
26032032Slm66018 	}
26042032Slm66018 
26052032Slm66018 	return (0);
26062032Slm66018 }
26072032Slm66018 
26082032Slm66018 static int
26092531Snarayan vd_setup_partition_efi(vd_t *vd)
26102531Snarayan {
26112531Snarayan 	efi_gpt_t *gpt;
26122531Snarayan 	efi_gpe_t *gpe;
26132531Snarayan 	struct uuid uuid = EFI_RESERVED;
26142531Snarayan 	uint32_t crc;
26152531Snarayan 	int length;
26162531Snarayan 
26172531Snarayan 	length = sizeof (efi_gpt_t) + sizeof (efi_gpe_t);
26182531Snarayan 
26192531Snarayan 	gpt = kmem_zalloc(length, KM_SLEEP);
26202531Snarayan 	gpe = (efi_gpe_t *)(gpt + 1);
26212531Snarayan 
26222531Snarayan 	gpt->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
26232531Snarayan 	gpt->efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
26242531Snarayan 	gpt->efi_gpt_HeaderSize = LE_32(sizeof (efi_gpt_t));
26252531Snarayan 	gpt->efi_gpt_FirstUsableLBA = LE_64(0ULL);
26262531Snarayan 	gpt->efi_gpt_LastUsableLBA = LE_64(vd->vdisk_size - 1);
26272531Snarayan 	gpt->efi_gpt_NumberOfPartitionEntries = LE_32(1);
26282531Snarayan 	gpt->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (efi_gpe_t));
26292531Snarayan 
26302531Snarayan 	UUID_LE_CONVERT(gpe->efi_gpe_PartitionTypeGUID, uuid);
26312531Snarayan 	gpe->efi_gpe_StartingLBA = gpt->efi_gpt_FirstUsableLBA;
26322531Snarayan 	gpe->efi_gpe_EndingLBA = gpt->efi_gpt_LastUsableLBA;
26332531Snarayan 
26342531Snarayan 	CRC32(crc, gpe, sizeof (efi_gpe_t), -1U, crc32_table);
26352531Snarayan 	gpt->efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
26362531Snarayan 
26372531Snarayan 	CRC32(crc, gpt, sizeof (efi_gpt_t), -1U, crc32_table);
26382531Snarayan 	gpt->efi_gpt_HeaderCRC32 = LE_32(~crc);
26392531Snarayan 
26402531Snarayan 	vd->dk_efi.dki_lba = 0;
26412531Snarayan 	vd->dk_efi.dki_length = length;
26422531Snarayan 	vd->dk_efi.dki_data = gpt;
26432531Snarayan 
26442531Snarayan 	return (0);
26452531Snarayan }
26462531Snarayan 
26472531Snarayan static int
26483401Snarayan vd_setup_file(vd_t *vd)
26493401Snarayan {
26503401Snarayan 	int 		i, rval, status;
2651*3782Sachartre 	ushort_t	sum;
26523401Snarayan 	vattr_t		vattr;
26533401Snarayan 	dev_t		dev;
26543401Snarayan 	char		*file_path = vd->device_path;
26553401Snarayan 	char		dev_path[MAXPATHLEN + 1];
26563401Snarayan 	ldi_handle_t	lhandle;
26573401Snarayan 	struct dk_cinfo	dk_cinfo;
2658*3782Sachartre 	struct dk_label label;
26593401Snarayan 
26603401Snarayan 	/* make sure the file is valid */
26613401Snarayan 	if ((status = lookupname(file_path, UIO_SYSSPACE, FOLLOW,
26623401Snarayan 		NULLVPP, &vd->file_vnode)) != 0) {
2663*3782Sachartre 		PRN("Cannot lookup file(%s) errno %d", file_path, status);
26643401Snarayan 		return (status);
26653401Snarayan 	}
26663401Snarayan 
26673401Snarayan 	if (vd->file_vnode->v_type != VREG) {
2668*3782Sachartre 		PRN("Invalid file type (%s)\n", file_path);
26693401Snarayan 		VN_RELE(vd->file_vnode);
26703401Snarayan 		return (EBADF);
26713401Snarayan 	}
26723401Snarayan 	VN_RELE(vd->file_vnode);
26733401Snarayan 
26743401Snarayan 	if ((status = vn_open(file_path, UIO_SYSSPACE, vd_open_flags | FOFFMAX,
26753401Snarayan 	    0, &vd->file_vnode, 0, 0)) != 0) {
2676*3782Sachartre 		PRN("vn_open(%s) = errno %d", file_path, status);
26773401Snarayan 		return (status);
26783401Snarayan 	}
26793401Snarayan 
2680*3782Sachartre 	/*
2681*3782Sachartre 	 * We set vd->file now so that vds_destroy_vd will take care of
2682*3782Sachartre 	 * closing the file and releasing the vnode in case of an error.
2683*3782Sachartre 	 */
2684*3782Sachartre 	vd->file = B_TRUE;
2685*3782Sachartre 	vd->pseudo = B_FALSE;
2686*3782Sachartre 
26873401Snarayan 	vattr.va_mask = AT_SIZE;
26883401Snarayan 	if ((status = VOP_GETATTR(vd->file_vnode, &vattr, 0, kcred)) != 0) {
2689*3782Sachartre 		PRN("VOP_GETATTR(%s) = errno %d", file_path, status);
26903401Snarayan 		return (EIO);
26913401Snarayan 	}
26923401Snarayan 
26933401Snarayan 	vd->file_size = vattr.va_size;
26943401Snarayan 	/* size should be at least sizeof(dk_label) */
26953401Snarayan 	if (vd->file_size < sizeof (struct dk_label)) {
26963401Snarayan 		PRN("Size of file has to be at least %ld bytes",
26973401Snarayan 		    sizeof (struct dk_label));
26983401Snarayan 		return (EIO);
26993401Snarayan 	}
27003401Snarayan 
2701*3782Sachartre 	if (vd->file_vnode->v_flag & VNOMAP) {
2702*3782Sachartre 		PRN("File %s cannot be mapped", file_path);
27033401Snarayan 		return (EIO);
27043401Snarayan 	}
27053401Snarayan 
2706*3782Sachartre 	/* read label from file */
2707*3782Sachartre 	if (VD_FILE_LABEL_READ(vd, &label) < 0) {
2708*3782Sachartre 		PRN("Can't read label from %s", file_path);
2709*3782Sachartre 		return (EIO);
2710*3782Sachartre 	}
27113401Snarayan 
27123401Snarayan 	/* label checksum */
2713*3782Sachartre 	sum = vd_lbl2cksum(&label);
2714*3782Sachartre 
2715*3782Sachartre 	if (label.dkl_magic != DKL_MAGIC || label.dkl_cksum != sum) {
27163401Snarayan 		PR0("%s has an invalid disk label "
27173401Snarayan 		    "(magic=%x cksum=%x (expect %x))",
2718*3782Sachartre 		    file_path, label.dkl_magic, label.dkl_cksum, sum);
27193401Snarayan 
27203401Snarayan 		/* default label */
2721*3782Sachartre 		bzero(&label, sizeof (struct dk_label));
27223401Snarayan 
27233401Snarayan 		/*
27243401Snarayan 		 * We must have a resonable number of cylinders and sectors so
27253401Snarayan 		 * that newfs can run using default values.
27263401Snarayan 		 *
27273401Snarayan 		 * if (disk_size < 2MB)
27283401Snarayan 		 * 	phys_cylinders = disk_size / 100K
27293401Snarayan 		 * else
27303401Snarayan 		 * 	phys_cylinders = disk_size / 300K
27313401Snarayan 		 *
27323401Snarayan 		 * phys_cylinders = (phys_cylinders == 0) ? 1 : phys_cylinders
27333401Snarayan 		 * alt_cylinders = (phys_cylinders > 2) ? 2 : 0;
27343401Snarayan 		 * data_cylinders = phys_cylinders - alt_cylinders
27353401Snarayan 		 *
27363401Snarayan 		 * sectors = disk_size / (phys_cylinders * blk_size)
27373401Snarayan 		 */
27383401Snarayan 		if (vd->file_size < (2 * 1024 * 1024))
2739*3782Sachartre 			label.dkl_pcyl = vd->file_size / (100 * 1024);
27403401Snarayan 		else
2741*3782Sachartre 			label.dkl_pcyl = vd->file_size / (300 * 1024);
2742*3782Sachartre 
2743*3782Sachartre 		if (label.dkl_pcyl == 0)
2744*3782Sachartre 			label.dkl_pcyl = 1;
2745*3782Sachartre 
2746*3782Sachartre 		if (label.dkl_pcyl > 2)
2747*3782Sachartre 			label.dkl_acyl = 2;
27483401Snarayan 		else
2749*3782Sachartre 			label.dkl_acyl = 0;
2750*3782Sachartre 
2751*3782Sachartre 		label.dkl_nsect = vd->file_size /
2752*3782Sachartre 			(DEV_BSIZE * label.dkl_pcyl);
2753*3782Sachartre 		label.dkl_ncyl = label.dkl_pcyl - label.dkl_acyl;
2754*3782Sachartre 		label.dkl_nhead = 1;
2755*3782Sachartre 		label.dkl_write_reinstruct = 0;
2756*3782Sachartre 		label.dkl_read_reinstruct = 0;
2757*3782Sachartre 		label.dkl_rpm = 7200;
2758*3782Sachartre 		label.dkl_apc = 0;
2759*3782Sachartre 		label.dkl_intrlv = 0;
2760*3782Sachartre 		label.dkl_magic = DKL_MAGIC;
27613401Snarayan 
27623401Snarayan 		PR0("requested disk size: %ld bytes\n", vd->file_size);
2763*3782Sachartre 		PR0("setup: ncyl=%d nhead=%d nsec=%d\n", label.dkl_pcyl,
2764*3782Sachartre 		    label.dkl_nhead, label.dkl_nsect);
27653401Snarayan 		PR0("provided disk size: %ld bytes\n", (uint64_t)
2766*3782Sachartre 		    (label.dkl_pcyl *
2767*3782Sachartre 			label.dkl_nhead * label.dkl_nsect * DEV_BSIZE));
27683401Snarayan 
27693401Snarayan 		/*
27703401Snarayan 		 * We must have a correct label name otherwise format(1m) will
27713401Snarayan 		 * not recognized the disk as labeled.
27723401Snarayan 		 */
2773*3782Sachartre 		(void) snprintf(label.dkl_asciilabel, LEN_DKL_ASCII,
27743401Snarayan 		    "SUNVDSK cyl %d alt %d hd %d sec %d",
2775*3782Sachartre 		    label.dkl_ncyl, label.dkl_acyl, label.dkl_nhead,
2776*3782Sachartre 		    label.dkl_nsect);
27773401Snarayan 
27783401Snarayan 		/* default VTOC */
2779*3782Sachartre 		label.dkl_vtoc.v_version = V_VERSION;
2780*3782Sachartre 		label.dkl_vtoc.v_nparts = V_NUMPAR;
2781*3782Sachartre 		label.dkl_vtoc.v_sanity = VTOC_SANE;
2782*3782Sachartre 		label.dkl_vtoc.v_part[2].p_tag = V_BACKUP;
2783*3782Sachartre 		label.dkl_map[2].dkl_cylno = 0;
2784*3782Sachartre 		label.dkl_map[2].dkl_nblk = label.dkl_ncyl *
2785*3782Sachartre 			label.dkl_nhead * label.dkl_nsect;
2786*3782Sachartre 		label.dkl_map[0] = label.dkl_map[2];
2787*3782Sachartre 		label.dkl_map[0] = label.dkl_map[2];
2788*3782Sachartre 		label.dkl_cksum = vd_lbl2cksum(&label);
2789*3782Sachartre 
2790*3782Sachartre 		/* write default label to file */
2791*3782Sachartre 		if (VD_FILE_LABEL_WRITE(vd, &label) < 0) {
2792*3782Sachartre 			PRN("Can't write label to %s", file_path);
2793*3782Sachartre 			return (EIO);
2794*3782Sachartre 		}
27953401Snarayan 	}
27963401Snarayan 
2797*3782Sachartre 	vd->nslices = label.dkl_vtoc.v_nparts;
27983401Snarayan 
27993401Snarayan 	/* sector size = block size = DEV_BSIZE */
2800*3782Sachartre 	vd->vdisk_size = (label.dkl_pcyl *
2801*3782Sachartre 	    label.dkl_nhead * label.dkl_nsect) / DEV_BSIZE;
28023401Snarayan 	vd->vdisk_type = VD_DISK_TYPE_DISK;
28033401Snarayan 	vd->vdisk_label = VD_DISK_LABEL_VTOC;
28043401Snarayan 	vd->max_xfer_sz = maxphys / DEV_BSIZE; /* default transfer size */
28053401Snarayan 
28063401Snarayan 	/* Get max_xfer_sz from the device where the file is */
28073401Snarayan 	dev = vd->file_vnode->v_vfsp->vfs_dev;
28083401Snarayan 	dev_path[0] = NULL;
28093401Snarayan 	if (ddi_dev_pathname(dev, S_IFBLK, dev_path) == DDI_SUCCESS) {
28103401Snarayan 		PR0("underlying device = %s\n", dev_path);
28113401Snarayan 	}
28123401Snarayan 
28133401Snarayan 	if ((status = ldi_open_by_dev(&dev, OTYP_BLK, FREAD,
28143401Snarayan 		kcred, &lhandle, vd->vds->ldi_ident)) != 0) {
28153401Snarayan 		PR0("ldi_open_by_dev() returned errno %d for device %s",
28163401Snarayan 		    status, dev_path);
28173401Snarayan 	} else {
28183401Snarayan 		if ((status = ldi_ioctl(lhandle, DKIOCINFO,
28193401Snarayan 		    (intptr_t)&dk_cinfo, (vd_open_flags | FKIOCTL), kcred,
28203401Snarayan 		    &rval)) != 0) {
28213401Snarayan 			PR0("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
28223401Snarayan 			    status, dev_path);
28233401Snarayan 		} else {
28243401Snarayan 			/*
28253401Snarayan 			 * Store the device's max transfer size for
28263401Snarayan 			 * return to the client
28273401Snarayan 			 */
28283401Snarayan 			vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
28293401Snarayan 		}
28303401Snarayan 
28313401Snarayan 		PR0("close the device %s", dev_path);
28323401Snarayan 		(void) ldi_close(lhandle, FREAD, kcred);
28333401Snarayan 	}
28343401Snarayan 
28353401Snarayan 	PR0("using for file %s, dev %s, max_xfer = %u blks",
28363401Snarayan 	    file_path, dev_path, vd->max_xfer_sz);
28373401Snarayan 
2838*3782Sachartre 	vd->dk_geom.dkg_ncyl = label.dkl_ncyl;
2839*3782Sachartre 	vd->dk_geom.dkg_acyl = label.dkl_acyl;
2840*3782Sachartre 	vd->dk_geom.dkg_pcyl = label.dkl_pcyl;
2841*3782Sachartre 	vd->dk_geom.dkg_nhead = label.dkl_nhead;
2842*3782Sachartre 	vd->dk_geom.dkg_nsect = label.dkl_nsect;
2843*3782Sachartre 	vd->dk_geom.dkg_intrlv = label.dkl_intrlv;
2844*3782Sachartre 	vd->dk_geom.dkg_apc = label.dkl_apc;
2845*3782Sachartre 	vd->dk_geom.dkg_rpm = label.dkl_rpm;
2846*3782Sachartre 	vd->dk_geom.dkg_write_reinstruct = label.dkl_write_reinstruct;
2847*3782Sachartre 	vd->dk_geom.dkg_read_reinstruct = label.dkl_read_reinstruct;
2848*3782Sachartre 
2849*3782Sachartre 	vd->vtoc.v_sanity = label.dkl_vtoc.v_sanity;
2850*3782Sachartre 	vd->vtoc.v_version = label.dkl_vtoc.v_version;
28513401Snarayan 	vd->vtoc.v_sectorsz = DEV_BSIZE;
2852*3782Sachartre 	vd->vtoc.v_nparts = label.dkl_vtoc.v_nparts;
2853*3782Sachartre 
2854*3782Sachartre 	bcopy(label.dkl_vtoc.v_volume, vd->vtoc.v_volume,
28553401Snarayan 	    LEN_DKL_VVOL);
2856*3782Sachartre 	bcopy(label.dkl_asciilabel, vd->vtoc.v_asciilabel,
28573401Snarayan 	    LEN_DKL_ASCII);
28583401Snarayan 
28593401Snarayan 	for (i = 0; i < vd->nslices; i++) {
2860*3782Sachartre 		vd->vtoc.timestamp[i] = label.dkl_vtoc.v_timestamp[i];
2861*3782Sachartre 		vd->vtoc.v_part[i].p_tag = label.dkl_vtoc.v_part[i].p_tag;
2862*3782Sachartre 		vd->vtoc.v_part[i].p_flag = label.dkl_vtoc.v_part[i].p_flag;
2863*3782Sachartre 		vd->vtoc.v_part[i].p_start = label.dkl_map[i].dkl_cylno *
2864*3782Sachartre 			label.dkl_nhead * label.dkl_nsect;
2865*3782Sachartre 		vd->vtoc.v_part[i].p_size = label.dkl_map[i].dkl_nblk;
28663401Snarayan 		vd->ldi_handle[i] = NULL;
28673401Snarayan 		vd->dev[i] = NULL;
28683401Snarayan 	}
28693401Snarayan 
28703401Snarayan 	return (0);
28713401Snarayan }
28723401Snarayan 
28733401Snarayan static int
28743401Snarayan vd_setup_vd(vd_t *vd)
28751991Sheppo {
28762410Slm66018 	int		rval, status;
28771991Sheppo 	dev_info_t	*dip;
28781991Sheppo 	struct dk_cinfo	dk_cinfo;
28793401Snarayan 	char		*device_path = vd->device_path;
28801991Sheppo 
28812531Snarayan 	/*
28822531Snarayan 	 * We need to open with FNDELAY so that opening an empty partition
28832531Snarayan 	 * does not fail.
28842531Snarayan 	 */
28852531Snarayan 	if ((status = ldi_open_by_name(device_path, vd_open_flags | FNDELAY,
28862531Snarayan 	    kcred, &vd->ldi_handle[0], vd->vds->ldi_ident)) != 0) {
28873401Snarayan 		PR0("ldi_open_by_name(%s) = errno %d", device_path, status);
2888*3782Sachartre 		vd->ldi_handle[0] = NULL;
28893401Snarayan 
28903401Snarayan 		/* this may not be a device try opening as a file */
28913401Snarayan 		if (status == ENXIO || status == ENODEV)
28923401Snarayan 			status = vd_setup_file(vd);
28933401Snarayan 		if (status) {
2894*3782Sachartre 			PRN("Cannot use device/file (%s), errno=%d\n",
28953401Snarayan 			    device_path, status);
28963401Snarayan 			if (status == ENXIO || status == ENODEV ||
28973401Snarayan 			    status == ENOENT) {
28983401Snarayan 				return (EAGAIN);
28993401Snarayan 			}
29003401Snarayan 		}
29012032Slm66018 		return (status);
29022032Slm66018 	}
29032032Slm66018 
29042531Snarayan 	/*
29052531Snarayan 	 * nslices must be updated now so that vds_destroy_vd() will close
29062531Snarayan 	 * the slice we have just opened in case of an error.
29072531Snarayan 	 */
29082531Snarayan 	vd->nslices = 1;
29093401Snarayan 	vd->file = B_FALSE;
29102531Snarayan 
29112410Slm66018 	/* Get device number and size of backing device */
29122032Slm66018 	if ((status = ldi_get_dev(vd->ldi_handle[0], &vd->dev[0])) != 0) {
29131991Sheppo 		PRN("ldi_get_dev() returned errno %d for %s",
29142410Slm66018 		    status, device_path);
29151991Sheppo 		return (status);
29161991Sheppo 	}
29172410Slm66018 	if (ldi_get_size(vd->ldi_handle[0], &vd->vdisk_size) != DDI_SUCCESS) {
29182410Slm66018 		PRN("ldi_get_size() failed for %s", device_path);
29192410Slm66018 		return (EIO);
29202410Slm66018 	}
29212410Slm66018 	vd->vdisk_size = lbtodb(vd->vdisk_size);	/* convert to blocks */
29222410Slm66018 
29232410Slm66018 	/* Verify backing device supports dk_cinfo, dk_geom, and vtoc */
29242410Slm66018 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO,
29252410Slm66018 		    (intptr_t)&dk_cinfo, (vd_open_flags | FKIOCTL), kcred,
29262410Slm66018 		    &rval)) != 0) {
29272410Slm66018 		PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
29282410Slm66018 		    status, device_path);
29291991Sheppo 		return (status);
29301991Sheppo 	}
29312410Slm66018 	if (dk_cinfo.dki_partition >= V_NUMPAR) {
29322410Slm66018 		PRN("slice %u >= maximum slice %u for %s",
29332410Slm66018 		    dk_cinfo.dki_partition, V_NUMPAR, device_path);
29341991Sheppo 		return (EIO);
29351991Sheppo 	}
29362531Snarayan 
29372531Snarayan 	status = vd_read_vtoc(vd->ldi_handle[0], &vd->vtoc, &vd->vdisk_label);
29382531Snarayan 
29392531Snarayan 	if (status != 0) {
29402531Snarayan 		PRN("vd_read_vtoc returned errno %d for %s",
29412410Slm66018 		    status, device_path);
29422410Slm66018 		return (status);
29432410Slm66018 	}
29442531Snarayan 
29452531Snarayan 	if (vd->vdisk_label == VD_DISK_LABEL_VTOC &&
29462531Snarayan 	    (status = ldi_ioctl(vd->ldi_handle[0], DKIOCGGEOM,
29472531Snarayan 	    (intptr_t)&vd->dk_geom, (vd_open_flags | FKIOCTL),
29482531Snarayan 	    kcred, &rval)) != 0) {
29492531Snarayan 		    PRN("ldi_ioctl(DKIOCGEOM) returned errno %d for %s",
29502531Snarayan 			status, device_path);
29512531Snarayan 		    return (status);
29522410Slm66018 	}
29532410Slm66018 
29542410Slm66018 	/* Store the device's max transfer size for return to the client */
29552410Slm66018 	vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
29562410Slm66018 
29572410Slm66018 	/* Determine if backing device is a pseudo device */
29581991Sheppo 	if ((dip = ddi_hold_devi_by_instance(getmajor(vd->dev[0]),
29591991Sheppo 		    dev_to_instance(vd->dev[0]), 0))  == NULL) {
29602410Slm66018 		PRN("%s is no longer accessible", device_path);
29611991Sheppo 		return (EIO);
29621991Sheppo 	}
29631991Sheppo 	vd->pseudo = is_pseudo_device(dip);
29641991Sheppo 	ddi_release_devi(dip);
29651991Sheppo 	if (vd->pseudo) {
29661991Sheppo 		vd->vdisk_type	= VD_DISK_TYPE_SLICE;
29671991Sheppo 		vd->nslices	= 1;
29681991Sheppo 		return (0);	/* ...and we're done */
29691991Sheppo 	}
29701991Sheppo 
29712032Slm66018 	/* If slice is entire-disk slice, initialize for full disk */
29722032Slm66018 	if (dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE)
29732032Slm66018 		return (vd_setup_full_disk(vd));
29741991Sheppo 
29752032Slm66018 
29762410Slm66018 	/* Otherwise, we have a non-entire slice of a device */
29771991Sheppo 	vd->vdisk_type	= VD_DISK_TYPE_SLICE;
29781991Sheppo 	vd->nslices	= 1;
29791991Sheppo 
29802531Snarayan 	if (vd->vdisk_label == VD_DISK_LABEL_EFI) {
29812531Snarayan 		status = vd_setup_partition_efi(vd);
29822531Snarayan 		return (status);
29832531Snarayan 	}
29841991Sheppo 
29852410Slm66018 	/* Initialize dk_geom structure for single-slice device */
29861991Sheppo 	if (vd->dk_geom.dkg_nsect == 0) {
2987*3782Sachartre 		PRN("%s geometry claims 0 sectors per track", device_path);
29881991Sheppo 		return (EIO);
29891991Sheppo 	}
29901991Sheppo 	if (vd->dk_geom.dkg_nhead == 0) {
2991*3782Sachartre 		PRN("%s geometry claims 0 heads", device_path);
29921991Sheppo 		return (EIO);
29931991Sheppo 	}
29941991Sheppo 	vd->dk_geom.dkg_ncyl =
29952410Slm66018 	    vd->vdisk_size/vd->dk_geom.dkg_nsect/vd->dk_geom.dkg_nhead;
29961991Sheppo 	vd->dk_geom.dkg_acyl = 0;
29971991Sheppo 	vd->dk_geom.dkg_pcyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl;
29981991Sheppo 
29991991Sheppo 
30002410Slm66018 	/* Initialize vtoc structure for single-slice device */
30011991Sheppo 	bcopy(VD_VOLUME_NAME, vd->vtoc.v_volume,
30021991Sheppo 	    MIN(sizeof (VD_VOLUME_NAME), sizeof (vd->vtoc.v_volume)));
30031991Sheppo 	bzero(vd->vtoc.v_part, sizeof (vd->vtoc.v_part));
30041991Sheppo 	vd->vtoc.v_nparts = 1;
30051991Sheppo 	vd->vtoc.v_part[0].p_tag = V_UNASSIGNED;
30061991Sheppo 	vd->vtoc.v_part[0].p_flag = 0;
30071991Sheppo 	vd->vtoc.v_part[0].p_start = 0;
30082410Slm66018 	vd->vtoc.v_part[0].p_size = vd->vdisk_size;
30091991Sheppo 	bcopy(VD_ASCIILABEL, vd->vtoc.v_asciilabel,
30101991Sheppo 	    MIN(sizeof (VD_ASCIILABEL), sizeof (vd->vtoc.v_asciilabel)));
30111991Sheppo 
30121991Sheppo 
30131991Sheppo 	return (0);
30141991Sheppo }
30151991Sheppo 
30161991Sheppo static int
30172410Slm66018 vds_do_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t ldc_id,
30181991Sheppo     vd_t **vdp)
30191991Sheppo {
30201991Sheppo 	char			tq_name[TASKQ_NAMELEN];
30212032Slm66018 	int			status;
30221991Sheppo 	ddi_iblock_cookie_t	iblock = NULL;
30231991Sheppo 	ldc_attr_t		ldc_attr;
30241991Sheppo 	vd_t			*vd;
30251991Sheppo 
30261991Sheppo 
30271991Sheppo 	ASSERT(vds != NULL);
30282410Slm66018 	ASSERT(device_path != NULL);
30291991Sheppo 	ASSERT(vdp != NULL);
30302410Slm66018 	PR0("Adding vdisk for %s", device_path);
30311991Sheppo 
30321991Sheppo 	if ((vd = kmem_zalloc(sizeof (*vd), KM_NOSLEEP)) == NULL) {
30331991Sheppo 		PRN("No memory for virtual disk");
30341991Sheppo 		return (EAGAIN);
30351991Sheppo 	}
30361991Sheppo 	*vdp = vd;	/* assign here so vds_destroy_vd() can cleanup later */
30371991Sheppo 	vd->vds = vds;
30383401Snarayan 	(void) strncpy(vd->device_path, device_path, MAXPATHLEN);
30391991Sheppo 
30402032Slm66018 	/* Open vdisk and initialize parameters */
30413401Snarayan 	if ((status = vd_setup_vd(vd)) == 0) {
30423401Snarayan 		vd->initialized |= VD_DISK_READY;
30433401Snarayan 
30443401Snarayan 		ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
30453401Snarayan 		PR0("vdisk_type = %s, pseudo = %s, file = %s, nslices = %u",
30463401Snarayan 		    ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
30473401Snarayan 		    (vd->pseudo ? "yes" : "no"), (vd->file ? "yes" : "no"),
30483401Snarayan 		    vd->nslices);
30493401Snarayan 	} else {
30503401Snarayan 		if (status != EAGAIN)
30513401Snarayan 			return (status);
30523401Snarayan 	}
30531991Sheppo 
30541991Sheppo 	/* Initialize locking */
30551991Sheppo 	if (ddi_get_soft_iblock_cookie(vds->dip, DDI_SOFTINT_MED,
30561991Sheppo 		&iblock) != DDI_SUCCESS) {
30571991Sheppo 		PRN("Could not get iblock cookie.");
30581991Sheppo 		return (EIO);
30591991Sheppo 	}
30601991Sheppo 
30611991Sheppo 	mutex_init(&vd->lock, NULL, MUTEX_DRIVER, iblock);
30621991Sheppo 	vd->initialized |= VD_LOCKING;
30631991Sheppo 
30641991Sheppo 
30652336Snarayan 	/* Create start and completion task queues for the vdisk */
30662336Snarayan 	(void) snprintf(tq_name, sizeof (tq_name), "vd_startq%lu", id);
30671991Sheppo 	PR1("tq_name = %s", tq_name);
30682336Snarayan 	if ((vd->startq = ddi_taskq_create(vds->dip, tq_name, 1,
30691991Sheppo 		    TASKQ_DEFAULTPRI, 0)) == NULL) {
30701991Sheppo 		PRN("Could not create task queue");
30711991Sheppo 		return (EIO);
30721991Sheppo 	}
30732336Snarayan 	(void) snprintf(tq_name, sizeof (tq_name), "vd_completionq%lu", id);
30742336Snarayan 	PR1("tq_name = %s", tq_name);
30752336Snarayan 	if ((vd->completionq = ddi_taskq_create(vds->dip, tq_name, 1,
30762336Snarayan 		    TASKQ_DEFAULTPRI, 0)) == NULL) {
30772336Snarayan 		PRN("Could not create task queue");
30782336Snarayan 		return (EIO);
30792336Snarayan 	}
30802336Snarayan 	vd->enabled = 1;	/* before callback can dispatch to startq */
30811991Sheppo 
30821991Sheppo 
30831991Sheppo 	/* Bring up LDC */
30841991Sheppo 	ldc_attr.devclass	= LDC_DEV_BLK_SVC;
30851991Sheppo 	ldc_attr.instance	= ddi_get_instance(vds->dip);
30861991Sheppo 	ldc_attr.mode		= LDC_MODE_UNRELIABLE;
30872410Slm66018 	ldc_attr.mtu		= VD_LDC_MTU;
30881991Sheppo 	if ((status = ldc_init(ldc_id, &ldc_attr, &vd->ldc_handle)) != 0) {
3089*3782Sachartre 		PRN("Could not initialize LDC channel %lu, "
3090*3782Sachartre 		    "init failed with error %d", ldc_id, status);
30911991Sheppo 		return (status);
30921991Sheppo 	}
30931991Sheppo 	vd->initialized |= VD_LDC;
30941991Sheppo 
30951991Sheppo 	if ((status = ldc_reg_callback(vd->ldc_handle, vd_handle_ldc_events,
30961991Sheppo 		(caddr_t)vd)) != 0) {
3097*3782Sachartre 		PRN("Could not initialize LDC channel %lu,"
3098*3782Sachartre 		    "reg_callback failed with error %d", ldc_id, status);
30991991Sheppo 		return (status);
31001991Sheppo 	}
31011991Sheppo 
31021991Sheppo 	if ((status = ldc_open(vd->ldc_handle)) != 0) {
3103*3782Sachartre 		PRN("Could not initialize LDC channel %lu,"
3104*3782Sachartre 		    "open failed with error %d", ldc_id, status);
31051991Sheppo 		return (status);
31061991Sheppo 	}
31071991Sheppo 
31082793Slm66018 	if ((status = ldc_up(vd->ldc_handle)) != 0) {
31093166Ssg70180 		PR0("ldc_up() returned errno %d", status);
31102793Slm66018 	}
31112793Slm66018 
31122531Snarayan 	/* Allocate the inband task memory handle */
31132531Snarayan 	status = ldc_mem_alloc_handle(vd->ldc_handle, &(vd->inband_task.mhdl));
31142531Snarayan 	if (status) {
3115*3782Sachartre 		PRN("Could not initialize LDC channel %lu,"
3116*3782Sachartre 		    "alloc_handle failed with error %d", ldc_id, status);
31172531Snarayan 		return (ENXIO);
31182531Snarayan 	}
31191991Sheppo 
31201991Sheppo 	/* Add the successfully-initialized vdisk to the server's table */
31211991Sheppo 	if (mod_hash_insert(vds->vd_table, (mod_hash_key_t)id, vd) != 0) {
31221991Sheppo 		PRN("Error adding vdisk ID %lu to table", id);
31231991Sheppo 		return (EIO);
31241991Sheppo 	}
31251991Sheppo 
31262793Slm66018 	/* Allocate the staging buffer */
31272793Slm66018 	vd->max_msglen	= sizeof (vio_msg_t);	/* baseline vio message size */
31282793Slm66018 	vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
31292793Slm66018 
31302793Slm66018 	/* store initial state */
31312793Slm66018 	vd->state = VD_STATE_INIT;
31322793Slm66018 
31331991Sheppo 	return (0);
31341991Sheppo }
31351991Sheppo 
31362793Slm66018 static void
31372793Slm66018 vd_free_dring_task(vd_t *vdp)
31382793Slm66018 {
31392793Slm66018 	if (vdp->dring_task != NULL) {
31402793Slm66018 		ASSERT(vdp->dring_len != 0);
31412793Slm66018 		/* Free all dring_task memory handles */
31422793Slm66018 		for (int i = 0; i < vdp->dring_len; i++) {
31432793Slm66018 			(void) ldc_mem_free_handle(vdp->dring_task[i].mhdl);
31442793Slm66018 			kmem_free(vdp->dring_task[i].msg, vdp->max_msglen);
31452793Slm66018 			vdp->dring_task[i].msg = NULL;
31462793Slm66018 		}
31472793Slm66018 		kmem_free(vdp->dring_task,
31482793Slm66018 		    (sizeof (*vdp->dring_task)) * vdp->dring_len);
31492793Slm66018 		vdp->dring_task = NULL;
31502793Slm66018 	}
31512793Slm66018 }
31522793Slm66018 
31531991Sheppo /*
31541991Sheppo  * Destroy the state associated with a virtual disk
31551991Sheppo  */
31561991Sheppo static void
31571991Sheppo vds_destroy_vd(void *arg)
31581991Sheppo {
31591991Sheppo 	vd_t	*vd = (vd_t *)arg;
31603166Ssg70180 	int	retry = 0, rv;
31611991Sheppo 
31621991Sheppo 	if (vd == NULL)
31631991Sheppo 		return;
31641991Sheppo 
31652336Snarayan 	PR0("Destroying vdisk state");
31662336Snarayan 
31672531Snarayan 	if (vd->dk_efi.dki_data != NULL)
31682531Snarayan 		kmem_free(vd->dk_efi.dki_data, vd->dk_efi.dki_length);
31692531Snarayan 
31701991Sheppo 	/* Disable queuing requests for the vdisk */
31711991Sheppo 	if (vd->initialized & VD_LOCKING) {
31721991Sheppo 		mutex_enter(&vd->lock);
31731991Sheppo 		vd->enabled = 0;
31741991Sheppo 		mutex_exit(&vd->lock);
31751991Sheppo 	}
31761991Sheppo 
31772336Snarayan 	/* Drain and destroy start queue (*before* destroying completionq) */
31782336Snarayan 	if (vd->startq != NULL)
31792336Snarayan 		ddi_taskq_destroy(vd->startq);	/* waits for queued tasks */
31802336Snarayan 
31812336Snarayan 	/* Drain and destroy completion queue (*before* shutting down LDC) */
31822336Snarayan 	if (vd->completionq != NULL)
31832336Snarayan 		ddi_taskq_destroy(vd->completionq);	/* waits for tasks */
31842336Snarayan 
31852793Slm66018 	vd_free_dring_task(vd);
31862793Slm66018 
31873166Ssg70180 	/* Free the inband task memory handle */
31883166Ssg70180 	(void) ldc_mem_free_handle(vd->inband_task.mhdl);
31893166Ssg70180 
31903166Ssg70180 	/* Shut down LDC */
31913166Ssg70180 	if (vd->initialized & VD_LDC) {
31923166Ssg70180 		/* unmap the dring */
31933166Ssg70180 		if (vd->initialized & VD_DRING)
31943166Ssg70180 			(void) ldc_mem_dring_unmap(vd->dring_handle);
31953166Ssg70180 
31963166Ssg70180 		/* close LDC channel - retry on EAGAIN */
31973166Ssg70180 		while ((rv = ldc_close(vd->ldc_handle)) == EAGAIN) {
31983166Ssg70180 			if (++retry > vds_ldc_retries) {
31993166Ssg70180 				PR0("Timed out closing channel");
32003166Ssg70180 				break;
32013166Ssg70180 			}
32023166Ssg70180 			drv_usecwait(vds_ldc_delay);
32033166Ssg70180 		}
32043166Ssg70180 		if (rv == 0) {
32053166Ssg70180 			(void) ldc_unreg_callback(vd->ldc_handle);
32063166Ssg70180 			(void) ldc_fini(vd->ldc_handle);
32073166Ssg70180 		} else {
32083166Ssg70180 			/*
32093166Ssg70180 			 * Closing the LDC channel has failed. Ideally we should
32103166Ssg70180 			 * fail here but there is no Zeus level infrastructure
32113166Ssg70180 			 * to handle this. The MD has already been changed and
32123166Ssg70180 			 * we have to do the close. So we try to do as much
32133166Ssg70180 			 * clean up as we can.
32143166Ssg70180 			 */
32153166Ssg70180 			(void) ldc_set_cb_mode(vd->ldc_handle, LDC_CB_DISABLE);
32163166Ssg70180 			while (ldc_unreg_callback(vd->ldc_handle) == EAGAIN)
32173166Ssg70180 				drv_usecwait(vds_ldc_delay);
32183166Ssg70180 		}
32193166Ssg70180 	}
32203166Ssg70180 
32212793Slm66018 	/* Free the staging buffer for msgs */
32222793Slm66018 	if (vd->vio_msgp != NULL) {
32232793Slm66018 		kmem_free(vd->vio_msgp, vd->max_msglen);
32242793Slm66018 		vd->vio_msgp = NULL;
32252793Slm66018 	}
32262793Slm66018 
32272793Slm66018 	/* Free the inband message buffer */
32282793Slm66018 	if (vd->inband_task.msg != NULL) {
32292793Slm66018 		kmem_free(vd->inband_task.msg, vd->max_msglen);
32302793Slm66018 		vd->inband_task.msg = NULL;
32312336Snarayan 	}
3232*3782Sachartre 	if (vd->file) {
3233*3782Sachartre 		/* Close file */
3234*3782Sachartre 		(void) VOP_CLOSE(vd->file_vnode, vd_open_flags, 1,
3235*3782Sachartre 		    0, kcred);
3236*3782Sachartre 		VN_RELE(vd->file_vnode);
3237*3782Sachartre 	} else {
3238*3782Sachartre 		/* Close any open backing-device slices */
3239*3782Sachartre 		for (uint_t slice = 0; slice < vd->nslices; slice++) {
3240*3782Sachartre 			if (vd->ldi_handle[slice] != NULL) {
3241*3782Sachartre 				PR0("Closing slice %u", slice);
3242*3782Sachartre 				(void) ldi_close(vd->ldi_handle[slice],
3243*3782Sachartre 				    vd_open_flags | FNDELAY, kcred);
32443401Snarayan 			}
32451991Sheppo 		}
32461991Sheppo 	}
32471991Sheppo 
32481991Sheppo 	/* Free lock */
32491991Sheppo 	if (vd->initialized & VD_LOCKING)
32501991Sheppo 		mutex_destroy(&vd->lock);
32511991Sheppo 
32521991Sheppo 	/* Finally, free the vdisk structure itself */
32531991Sheppo 	kmem_free(vd, sizeof (*vd));
32541991Sheppo }
32551991Sheppo 
32561991Sheppo static int
32572410Slm66018 vds_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t ldc_id)
32581991Sheppo {
32591991Sheppo 	int	status;
32601991Sheppo 	vd_t	*vd = NULL;
32611991Sheppo 
32621991Sheppo 
32632410Slm66018 	if ((status = vds_do_init_vd(vds, id, device_path, ldc_id, &vd)) != 0)
32641991Sheppo 		vds_destroy_vd(vd);
32651991Sheppo 
32661991Sheppo 	return (status);
32671991Sheppo }
32681991Sheppo 
32691991Sheppo static int
32701991Sheppo vds_do_get_ldc_id(md_t *md, mde_cookie_t vd_node, mde_cookie_t *channel,
32711991Sheppo     uint64_t *ldc_id)
32721991Sheppo {
32731991Sheppo 	int	num_channels;
32741991Sheppo 
32751991Sheppo 
32761991Sheppo 	/* Look for channel endpoint child(ren) of the vdisk MD node */
32771991Sheppo 	if ((num_channels = md_scan_dag(md, vd_node,
32781991Sheppo 		    md_find_name(md, VD_CHANNEL_ENDPOINT),
32791991Sheppo 		    md_find_name(md, "fwd"), channel)) <= 0) {
32801991Sheppo 		PRN("No \"%s\" found for virtual disk", VD_CHANNEL_ENDPOINT);
32811991Sheppo 		return (-1);
32821991Sheppo 	}
32831991Sheppo 
32841991Sheppo 	/* Get the "id" value for the first channel endpoint node */
32851991Sheppo 	if (md_get_prop_val(md, channel[0], VD_ID_PROP, ldc_id) != 0) {
32861991Sheppo 		PRN("No \"%s\" property found for \"%s\" of vdisk",
32871991Sheppo 		    VD_ID_PROP, VD_CHANNEL_ENDPOINT);
32881991Sheppo 		return (-1);
32891991Sheppo 	}
32901991Sheppo 
32911991Sheppo 	if (num_channels > 1) {
32921991Sheppo 		PRN("Using ID of first of multiple channels for this vdisk");
32931991Sheppo 	}
32941991Sheppo 
32951991Sheppo 	return (0);
32961991Sheppo }
32971991Sheppo 
32981991Sheppo static int
32991991Sheppo vds_get_ldc_id(md_t *md, mde_cookie_t vd_node, uint64_t *ldc_id)
33001991Sheppo {
33011991Sheppo 	int		num_nodes, status;
33021991Sheppo 	size_t		size;
33031991Sheppo 	mde_cookie_t	*channel;
33041991Sheppo 
33051991Sheppo 
33061991Sheppo 	if ((num_nodes = md_node_count(md)) <= 0) {
33071991Sheppo 		PRN("Invalid node count in Machine Description subtree");
33081991Sheppo 		return (-1);
33091991Sheppo 	}
33101991Sheppo 	size = num_nodes*(sizeof (*channel));
33111991Sheppo 	channel = kmem_zalloc(size, KM_SLEEP);
33121991Sheppo 	status = vds_do_get_ldc_id(md, vd_node, channel, ldc_id);
33131991Sheppo 	kmem_free(channel, size);
33141991Sheppo 
33151991Sheppo 	return (status);
33161991Sheppo }
33171991Sheppo 
33181991Sheppo static void
33191991Sheppo vds_add_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
33201991Sheppo {
33212410Slm66018 	char		*device_path = NULL;
33221991Sheppo 	uint64_t	id = 0, ldc_id = 0;
33231991Sheppo 
33241991Sheppo 
33251991Sheppo 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
33261991Sheppo 		PRN("Error getting vdisk \"%s\"", VD_ID_PROP);
33271991Sheppo 		return;
33281991Sheppo 	}
33291991Sheppo 	PR0("Adding vdisk ID %lu", id);
33301991Sheppo 	if (md_get_prop_str(md, vd_node, VD_BLOCK_DEVICE_PROP,
33312410Slm66018 		&device_path) != 0) {
33321991Sheppo 		PRN("Error getting vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
33331991Sheppo 		return;
33341991Sheppo 	}
33351991Sheppo 
33361991Sheppo 	if (vds_get_ldc_id(md, vd_node, &ldc_id) != 0) {
33371991Sheppo 		PRN("Error getting LDC ID for vdisk %lu", id);
33381991Sheppo 		return;
33391991Sheppo 	}
33401991Sheppo 
33412410Slm66018 	if (vds_init_vd(vds, id, device_path, ldc_id) != 0) {
33421991Sheppo 		PRN("Failed to add vdisk ID %lu", id);
33431991Sheppo 		return;
33441991Sheppo 	}
33451991Sheppo }
33461991Sheppo 
33471991Sheppo static void
33481991Sheppo vds_remove_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
33491991Sheppo {
33501991Sheppo 	uint64_t	id = 0;
33511991Sheppo 
33521991Sheppo 
33531991Sheppo 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
33541991Sheppo 		PRN("Unable to get \"%s\" property from vdisk's MD node",
33551991Sheppo 		    VD_ID_PROP);
33561991Sheppo 		return;
33571991Sheppo 	}
33581991Sheppo 	PR0("Removing vdisk ID %lu", id);
33591991Sheppo 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0)
33601991Sheppo 		PRN("No vdisk entry found for vdisk ID %lu", id);
33611991Sheppo }
33621991Sheppo 
33631991Sheppo static void
33641991Sheppo vds_change_vd(vds_t *vds, md_t *prev_md, mde_cookie_t prev_vd_node,
33651991Sheppo     md_t *curr_md, mde_cookie_t curr_vd_node)
33661991Sheppo {
33671991Sheppo 	char		*curr_dev, *prev_dev;
33681991Sheppo 	uint64_t	curr_id = 0, curr_ldc_id = 0;
33691991Sheppo 	uint64_t	prev_id = 0, prev_ldc_id = 0;
33701991Sheppo 	size_t		len;
33711991Sheppo 
33721991Sheppo 
33731991Sheppo 	/* Validate that vdisk ID has not changed */
33741991Sheppo 	if (md_get_prop_val(prev_md, prev_vd_node, VD_ID_PROP, &prev_id) != 0) {
33751991Sheppo 		PRN("Error getting previous vdisk \"%s\" property",
33761991Sheppo 		    VD_ID_PROP);
33771991Sheppo 		return;
33781991Sheppo 	}
33791991Sheppo 	if (md_get_prop_val(curr_md, curr_vd_node, VD_ID_PROP, &curr_id) != 0) {
33801991Sheppo 		PRN("Error getting current vdisk \"%s\" property", VD_ID_PROP);
33811991Sheppo 		return;
33821991Sheppo 	}
33831991Sheppo 	if (curr_id != prev_id) {
33841991Sheppo 		PRN("Not changing vdisk:  ID changed from %lu to %lu",
33851991Sheppo 		    prev_id, curr_id);
33861991Sheppo 		return;
33871991Sheppo 	}
33881991Sheppo 
33891991Sheppo 	/* Validate that LDC ID has not changed */
33901991Sheppo 	if (vds_get_ldc_id(prev_md, prev_vd_node, &prev_ldc_id) != 0) {
33911991Sheppo 		PRN("Error getting LDC ID for vdisk %lu", prev_id);
33921991Sheppo 		return;
33931991Sheppo 	}
33941991Sheppo 
33951991Sheppo 	if (vds_get_ldc_id(curr_md, curr_vd_node, &curr_ldc_id) != 0) {
33961991Sheppo 		PRN("Error getting LDC ID for vdisk %lu", curr_id);
33971991Sheppo 		return;
33981991Sheppo 	}
33991991Sheppo 	if (curr_ldc_id != prev_ldc_id) {
34002032Slm66018 		_NOTE(NOTREACHED);	/* lint is confused */
34011991Sheppo 		PRN("Not changing vdisk:  "
34021991Sheppo 		    "LDC ID changed from %lu to %lu", prev_ldc_id, curr_ldc_id);
34031991Sheppo 		return;
34041991Sheppo 	}
34051991Sheppo 
34061991Sheppo 	/* Determine whether device path has changed */
34071991Sheppo 	if (md_get_prop_str(prev_md, prev_vd_node, VD_BLOCK_DEVICE_PROP,
34081991Sheppo 		&prev_dev) != 0) {
34091991Sheppo 		PRN("Error getting previous vdisk \"%s\"",
34101991Sheppo 		    VD_BLOCK_DEVICE_PROP);
34111991Sheppo 		return;
34121991Sheppo 	}
34131991Sheppo 	if (md_get_prop_str(curr_md, curr_vd_node, VD_BLOCK_DEVICE_PROP,
34141991Sheppo 		&curr_dev) != 0) {
34151991Sheppo 		PRN("Error getting current vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
34161991Sheppo 		return;
34171991Sheppo 	}
34181991Sheppo 	if (((len = strlen(curr_dev)) == strlen(prev_dev)) &&
34191991Sheppo 	    (strncmp(curr_dev, prev_dev, len) == 0))
34201991Sheppo 		return;	/* no relevant (supported) change */
34211991Sheppo 
34221991Sheppo 	PR0("Changing vdisk ID %lu", prev_id);
34232793Slm66018 
34241991Sheppo 	/* Remove old state, which will close vdisk and reset */
34251991Sheppo 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)prev_id) != 0)
34261991Sheppo 		PRN("No entry found for vdisk ID %lu", prev_id);
34272793Slm66018 
34281991Sheppo 	/* Re-initialize vdisk with new state */
34291991Sheppo 	if (vds_init_vd(vds, curr_id, curr_dev, curr_ldc_id) != 0) {
34301991Sheppo 		PRN("Failed to change vdisk ID %lu", curr_id);
34311991Sheppo 		return;
34321991Sheppo 	}
34331991Sheppo }
34341991Sheppo 
34351991Sheppo static int
34361991Sheppo vds_process_md(void *arg, mdeg_result_t *md)
34371991Sheppo {
34381991Sheppo 	int	i;
34391991Sheppo 	vds_t	*vds = arg;
34401991Sheppo 
34411991Sheppo 
34421991Sheppo 	if (md == NULL)
34431991Sheppo 		return (MDEG_FAILURE);
34441991Sheppo 	ASSERT(vds != NULL);
34451991Sheppo 
34461991Sheppo 	for (i = 0; i < md->removed.nelem; i++)
34471991Sheppo 		vds_remove_vd(vds, md->removed.mdp, md->removed.mdep[i]);
34481991Sheppo 	for (i = 0; i < md->match_curr.nelem; i++)
34491991Sheppo 		vds_change_vd(vds, md->match_prev.mdp, md->match_prev.mdep[i],
34501991Sheppo 		    md->match_curr.mdp, md->match_curr.mdep[i]);
34511991Sheppo 	for (i = 0; i < md->added.nelem; i++)
34521991Sheppo 		vds_add_vd(vds, md->added.mdp, md->added.mdep[i]);
34531991Sheppo 
34541991Sheppo 	return (MDEG_SUCCESS);
34551991Sheppo }
34561991Sheppo 
34573401Snarayan 
34581991Sheppo static int
34591991Sheppo vds_do_attach(dev_info_t *dip)
34601991Sheppo {
34613297Ssb155480 	int			status, sz;
34623297Ssb155480 	int			cfg_handle;
34631991Sheppo 	minor_t			instance = ddi_get_instance(dip);
34641991Sheppo 	vds_t			*vds;
34653297Ssb155480 	mdeg_prop_spec_t	*pspecp;
34663297Ssb155480 	mdeg_node_spec_t	*ispecp;
34671991Sheppo 
34681991Sheppo 	/*
34691991Sheppo 	 * The "cfg-handle" property of a vds node in an MD contains the MD's
34701991Sheppo 	 * notion of "instance", or unique identifier, for that node; OBP
34711991Sheppo 	 * stores the value of the "cfg-handle" MD property as the value of
34721991Sheppo 	 * the "reg" property on the node in the device tree it builds from
34731991Sheppo 	 * the MD and passes to Solaris.  Thus, we look up the devinfo node's
34741991Sheppo 	 * "reg" property value to uniquely identify this device instance when
34751991Sheppo 	 * registering with the MD event-generation framework.  If the "reg"
34761991Sheppo 	 * property cannot be found, the device tree state is presumably so
34771991Sheppo 	 * broken that there is no point in continuing.
34781991Sheppo 	 */
34793297Ssb155480 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
34803297Ssb155480 		VD_REG_PROP)) {
34813297Ssb155480 		PRN("vds \"%s\" property does not exist", VD_REG_PROP);
34821991Sheppo 		return (DDI_FAILURE);
34831991Sheppo 	}
34841991Sheppo 
34851991Sheppo 	/* Get the MD instance for later MDEG registration */
34861991Sheppo 	cfg_handle = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
34873297Ssb155480 	    VD_REG_PROP, -1);
34881991Sheppo 
34891991Sheppo 	if (ddi_soft_state_zalloc(vds_state, instance) != DDI_SUCCESS) {
34901991Sheppo 		PRN("Could not allocate state for instance %u", instance);
34911991Sheppo 		return (DDI_FAILURE);
34921991Sheppo 	}
34931991Sheppo 
34941991Sheppo 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
34951991Sheppo 		PRN("Could not get state for instance %u", instance);
34961991Sheppo 		ddi_soft_state_free(vds_state, instance);
34971991Sheppo 		return (DDI_FAILURE);
34981991Sheppo 	}
34991991Sheppo 
35003401Snarayan 
35011991Sheppo 	vds->dip	= dip;
35021991Sheppo 	vds->vd_table	= mod_hash_create_ptrhash("vds_vd_table", VDS_NCHAINS,
35031991Sheppo 							vds_destroy_vd,
35041991Sheppo 							sizeof (void *));
35051991Sheppo 	ASSERT(vds->vd_table != NULL);
35061991Sheppo 
35071991Sheppo 	if ((status = ldi_ident_from_dip(dip, &vds->ldi_ident)) != 0) {
35081991Sheppo 		PRN("ldi_ident_from_dip() returned errno %d", status);
35091991Sheppo 		return (DDI_FAILURE);
35101991Sheppo 	}
35111991Sheppo 	vds->initialized |= VDS_LDI;
35121991Sheppo 
35131991Sheppo 	/* Register for MD updates */
35143297Ssb155480 	sz = sizeof (vds_prop_template);
35153297Ssb155480 	pspecp = kmem_alloc(sz, KM_SLEEP);
35163297Ssb155480 	bcopy(vds_prop_template, pspecp, sz);
35173297Ssb155480 
35183297Ssb155480 	VDS_SET_MDEG_PROP_INST(pspecp, cfg_handle);
35193297Ssb155480 
35203297Ssb155480 	/* initialize the complete prop spec structure */
35213297Ssb155480 	ispecp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP);
35223297Ssb155480 	ispecp->namep = "virtual-device";
35233297Ssb155480 	ispecp->specp = pspecp;
35243297Ssb155480 
35253297Ssb155480 	if (mdeg_register(ispecp, &vd_match, vds_process_md, vds,
35261991Sheppo 		&vds->mdeg) != MDEG_SUCCESS) {
35271991Sheppo 		PRN("Unable to register for MD updates");
35283297Ssb155480 		kmem_free(ispecp, sizeof (mdeg_node_spec_t));
35293297Ssb155480 		kmem_free(pspecp, sz);
35301991Sheppo 		return (DDI_FAILURE);
35311991Sheppo 	}
35323297Ssb155480 
35333297Ssb155480 	vds->ispecp = ispecp;
35341991Sheppo 	vds->initialized |= VDS_MDEG;
35351991Sheppo 
35362032Slm66018 	/* Prevent auto-detaching so driver is available whenever MD changes */
35372032Slm66018 	if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1) !=
35382032Slm66018 	    DDI_PROP_SUCCESS) {
35392032Slm66018 		PRN("failed to set \"%s\" property for instance %u",
35402032Slm66018 		    DDI_NO_AUTODETACH, instance);
35412032Slm66018 	}
35422032Slm66018 
35431991Sheppo 	ddi_report_dev(dip);
35441991Sheppo 	return (DDI_SUCCESS);
35451991Sheppo }
35461991Sheppo 
35471991Sheppo static int
35481991Sheppo vds_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
35491991Sheppo {
35501991Sheppo 	int	status;
35511991Sheppo 
35521991Sheppo 	switch (cmd) {
35531991Sheppo 	case DDI_ATTACH:
35542336Snarayan 		PR0("Attaching");
35551991Sheppo 		if ((status = vds_do_attach(dip)) != DDI_SUCCESS)
35561991Sheppo 			(void) vds_detach(dip, DDI_DETACH);
35571991Sheppo 		return (status);
35581991Sheppo 	case DDI_RESUME:
35592336Snarayan 		PR0("No action required for DDI_RESUME");
35601991Sheppo 		return (DDI_SUCCESS);
35611991Sheppo 	default:
35621991Sheppo 		return (DDI_FAILURE);
35631991Sheppo 	}
35641991Sheppo }
35651991Sheppo 
35661991Sheppo static struct dev_ops vds_ops = {
35671991Sheppo 	DEVO_REV,	/* devo_rev */
35681991Sheppo 	0,		/* devo_refcnt */
35691991Sheppo 	ddi_no_info,	/* devo_getinfo */
35701991Sheppo 	nulldev,	/* devo_identify */
35711991Sheppo 	nulldev,	/* devo_probe */
35721991Sheppo 	vds_attach,	/* devo_attach */
35731991Sheppo 	vds_detach,	/* devo_detach */
35741991Sheppo 	nodev,		/* devo_reset */
35751991Sheppo 	NULL,		/* devo_cb_ops */
35761991Sheppo 	NULL,		/* devo_bus_ops */
35771991Sheppo 	nulldev		/* devo_power */
35781991Sheppo };
35791991Sheppo 
35801991Sheppo static struct modldrv modldrv = {
35811991Sheppo 	&mod_driverops,
35821991Sheppo 	"virtual disk server v%I%",
35831991Sheppo 	&vds_ops,
35841991Sheppo };
35851991Sheppo 
35861991Sheppo static struct modlinkage modlinkage = {
35871991Sheppo 	MODREV_1,
35881991Sheppo 	&modldrv,
35891991Sheppo 	NULL
35901991Sheppo };
35911991Sheppo 
35921991Sheppo 
35931991Sheppo int
35941991Sheppo _init(void)
35951991Sheppo {
35961991Sheppo 	int		i, status;
35971991Sheppo 
35982336Snarayan 
35991991Sheppo 	if ((status = ddi_soft_state_init(&vds_state, sizeof (vds_t), 1)) != 0)
36001991Sheppo 		return (status);
36011991Sheppo 	if ((status = mod_install(&modlinkage)) != 0) {
36021991Sheppo 		ddi_soft_state_fini(&vds_state);
36031991Sheppo 		return (status);
36041991Sheppo 	}
36051991Sheppo 
36061991Sheppo 	/* Fill in the bit-mask of server-supported operations */
36071991Sheppo 	for (i = 0; i < vds_noperations; i++)
36081991Sheppo 		vds_operations |= 1 << (vds_operation[i].operation - 1);
36091991Sheppo 
36101991Sheppo 	return (0);
36111991Sheppo }
36121991Sheppo 
36131991Sheppo int
36141991Sheppo _info(struct modinfo *modinfop)
36151991Sheppo {
36161991Sheppo 	return (mod_info(&modlinkage, modinfop));
36171991Sheppo }
36181991Sheppo 
36191991Sheppo int
36201991Sheppo _fini(void)
36211991Sheppo {
36221991Sheppo 	int	status;
36231991Sheppo 
36242336Snarayan 
36251991Sheppo 	if ((status = mod_remove(&modlinkage)) != 0)
36261991Sheppo 		return (status);
36271991Sheppo 	ddi_soft_state_fini(&vds_state);
36281991Sheppo 	return (0);
36291991Sheppo }
3630