xref: /titanic_53/usr/src/uts/sun4v/io/vdc.c (revision 690555a1dfb3e9c3b00be26ece1988ed10a6fbe5)
11ae08745Sheppo /*
21ae08745Sheppo  * CDDL HEADER START
31ae08745Sheppo  *
41ae08745Sheppo  * The contents of this file are subject to the terms of the
51ae08745Sheppo  * Common Development and Distribution License (the "License").
61ae08745Sheppo  * You may not use this file except in compliance with the License.
71ae08745Sheppo  *
81ae08745Sheppo  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91ae08745Sheppo  * or http://www.opensolaris.org/os/licensing.
101ae08745Sheppo  * See the License for the specific language governing permissions
111ae08745Sheppo  * and limitations under the License.
121ae08745Sheppo  *
131ae08745Sheppo  * When distributing Covered Code, include this CDDL HEADER in each
141ae08745Sheppo  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151ae08745Sheppo  * If applicable, add the following below this CDDL HEADER, with the
161ae08745Sheppo  * fields enclosed by brackets "[]" replaced with your own identifying
171ae08745Sheppo  * information: Portions Copyright [yyyy] [name of copyright owner]
181ae08745Sheppo  *
191ae08745Sheppo  * CDDL HEADER END
201ae08745Sheppo  */
211ae08745Sheppo 
221ae08745Sheppo /*
236567eb0aSlh195018  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
241ae08745Sheppo  * Use is subject to license terms.
251ae08745Sheppo  */
261ae08745Sheppo 
271ae08745Sheppo #pragma ident	"%Z%%M%	%I%	%E% SMI"
281ae08745Sheppo 
291ae08745Sheppo /*
301ae08745Sheppo  * LDoms virtual disk client (vdc) device driver
311ae08745Sheppo  *
321ae08745Sheppo  * This driver runs on a guest logical domain and communicates with the virtual
331ae08745Sheppo  * disk server (vds) driver running on the service domain which is exporting
341ae08745Sheppo  * virtualized "disks" to the guest logical domain.
351ae08745Sheppo  *
361ae08745Sheppo  * The driver can be divided into four sections:
371ae08745Sheppo  *
381ae08745Sheppo  * 1) generic device driver housekeeping
391ae08745Sheppo  *	_init, _fini, attach, detach, ops structures, etc.
401ae08745Sheppo  *
411ae08745Sheppo  * 2) communication channel setup
421ae08745Sheppo  *	Setup the communications link over the LDC channel that vdc uses to
431ae08745Sheppo  *	talk to the vDisk server. Initialise the descriptor ring which
441ae08745Sheppo  *	allows the LDC clients to transfer data via memory mappings.
451ae08745Sheppo  *
461ae08745Sheppo  * 3) Support exported to upper layers (filesystems, etc)
471ae08745Sheppo  *	The upper layers call into vdc via strategy(9E) and DKIO(7I)
481ae08745Sheppo  *	ioctl calls. vdc will copy the data to be written to the descriptor
491ae08745Sheppo  *	ring or maps the buffer to store the data read by the vDisk
501ae08745Sheppo  *	server into the descriptor ring. It then sends a message to the
511ae08745Sheppo  *	vDisk server requesting it to complete the operation.
521ae08745Sheppo  *
531ae08745Sheppo  * 4) Handling responses from vDisk server.
541ae08745Sheppo  *	The vDisk server will ACK some or all of the messages vdc sends to it
551ae08745Sheppo  *	(this is configured during the handshake). Upon receipt of an ACK
561ae08745Sheppo  *	vdc will check the descriptor ring and signal to the upper layer
571ae08745Sheppo  *	code waiting on the IO.
581ae08745Sheppo  */
591ae08745Sheppo 
60e1ebb9ecSlm66018 #include <sys/atomic.h>
611ae08745Sheppo #include <sys/conf.h>
621ae08745Sheppo #include <sys/disp.h>
631ae08745Sheppo #include <sys/ddi.h>
641ae08745Sheppo #include <sys/dkio.h>
651ae08745Sheppo #include <sys/efi_partition.h>
661ae08745Sheppo #include <sys/fcntl.h>
671ae08745Sheppo #include <sys/file.h>
681ae08745Sheppo #include <sys/mach_descrip.h>
691ae08745Sheppo #include <sys/modctl.h>
701ae08745Sheppo #include <sys/mdeg.h>
711ae08745Sheppo #include <sys/note.h>
721ae08745Sheppo #include <sys/open.h>
73d10e4ef2Snarayan #include <sys/sdt.h>
741ae08745Sheppo #include <sys/stat.h>
751ae08745Sheppo #include <sys/sunddi.h>
761ae08745Sheppo #include <sys/types.h>
771ae08745Sheppo #include <sys/promif.h>
781ae08745Sheppo #include <sys/vtoc.h>
791ae08745Sheppo #include <sys/archsystm.h>
801ae08745Sheppo #include <sys/sysmacros.h>
811ae08745Sheppo 
821ae08745Sheppo #include <sys/cdio.h>
831ae08745Sheppo #include <sys/dktp/fdisk.h>
841ae08745Sheppo #include <sys/scsi/generic/sense.h>
851ae08745Sheppo #include <sys/scsi/impl/uscsi.h>	/* Needed for defn of USCSICMD ioctl */
861ae08745Sheppo 
871ae08745Sheppo #include <sys/ldoms.h>
881ae08745Sheppo #include <sys/ldc.h>
891ae08745Sheppo #include <sys/vio_common.h>
901ae08745Sheppo #include <sys/vio_mailbox.h>
911ae08745Sheppo #include <sys/vdsk_common.h>
921ae08745Sheppo #include <sys/vdsk_mailbox.h>
931ae08745Sheppo #include <sys/vdc.h>
941ae08745Sheppo 
951ae08745Sheppo /*
961ae08745Sheppo  * function prototypes
971ae08745Sheppo  */
981ae08745Sheppo 
991ae08745Sheppo /* standard driver functions */
1001ae08745Sheppo static int	vdc_open(dev_t *dev, int flag, int otyp, cred_t *cred);
1011ae08745Sheppo static int	vdc_close(dev_t dev, int flag, int otyp, cred_t *cred);
1021ae08745Sheppo static int	vdc_strategy(struct buf *buf);
1031ae08745Sheppo static int	vdc_print(dev_t dev, char *str);
1041ae08745Sheppo static int	vdc_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
1051ae08745Sheppo static int	vdc_read(dev_t dev, struct uio *uio, cred_t *cred);
1061ae08745Sheppo static int	vdc_write(dev_t dev, struct uio *uio, cred_t *cred);
1071ae08745Sheppo static int	vdc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
1081ae08745Sheppo 			cred_t *credp, int *rvalp);
1091ae08745Sheppo static int	vdc_aread(dev_t dev, struct aio_req *aio, cred_t *cred);
1101ae08745Sheppo static int	vdc_awrite(dev_t dev, struct aio_req *aio, cred_t *cred);
1111ae08745Sheppo 
1121ae08745Sheppo static int	vdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,
1131ae08745Sheppo 			void *arg, void **resultp);
1141ae08745Sheppo static int	vdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
1151ae08745Sheppo static int	vdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
1161ae08745Sheppo 
1171ae08745Sheppo /* setup */
1180d0c8d4bSnarayan static void	vdc_min(struct buf *bufp);
1190a55fbb7Slm66018 static int	vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen);
1201ae08745Sheppo static int	vdc_do_ldc_init(vdc_t *vdc);
1211ae08745Sheppo static int	vdc_start_ldc_connection(vdc_t *vdc);
1221ae08745Sheppo static int	vdc_create_device_nodes(vdc_t *vdc);
1234bac2208Snarayan static int	vdc_create_device_nodes_efi(vdc_t *vdc);
1244bac2208Snarayan static int	vdc_create_device_nodes_vtoc(vdc_t *vdc);
1251ae08745Sheppo static int	vdc_create_device_nodes_props(vdc_t *vdc);
1261ae08745Sheppo static int	vdc_get_ldc_id(dev_info_t *dip, uint64_t *ldc_id);
1270a55fbb7Slm66018 static int	vdc_do_ldc_up(vdc_t *vdc);
1281ae08745Sheppo static void	vdc_terminate_ldc(vdc_t *vdc);
1291ae08745Sheppo static int	vdc_init_descriptor_ring(vdc_t *vdc);
1301ae08745Sheppo static void	vdc_destroy_descriptor_ring(vdc_t *vdc);
1314bac2208Snarayan static int	vdc_setup_devid(vdc_t *vdc);
1324bac2208Snarayan static void	vdc_store_efi(vdc_t *vdc, struct dk_gpt *efi);
1331ae08745Sheppo 
1341ae08745Sheppo /* handshake with vds */
1350a55fbb7Slm66018 static int		vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver);
1363af08d82Slm66018 static int		vdc_ver_negotiation(vdc_t *vdcp);
1371ae08745Sheppo static int		vdc_init_attr_negotiation(vdc_t *vdc);
1383af08d82Slm66018 static int		vdc_attr_negotiation(vdc_t *vdcp);
1391ae08745Sheppo static int		vdc_init_dring_negotiate(vdc_t *vdc);
1403af08d82Slm66018 static int		vdc_dring_negotiation(vdc_t *vdcp);
1413af08d82Slm66018 static int		vdc_send_rdx(vdc_t *vdcp);
1423af08d82Slm66018 static int		vdc_rdx_exchange(vdc_t *vdcp);
1430a55fbb7Slm66018 static boolean_t	vdc_is_supported_version(vio_ver_msg_t *ver_msg);
1441ae08745Sheppo 
1450a55fbb7Slm66018 /* processing incoming messages from vDisk server */
1461ae08745Sheppo static void	vdc_process_msg_thread(vdc_t *vdc);
1473af08d82Slm66018 static int	vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp);
1483af08d82Slm66018 
1490a55fbb7Slm66018 static uint_t	vdc_handle_cb(uint64_t event, caddr_t arg);
1503af08d82Slm66018 static int	vdc_process_data_msg(vdc_t *vdc, vio_msg_t *msg);
1511ae08745Sheppo static int	vdc_process_err_msg(vdc_t *vdc, vio_msg_t msg);
1520a55fbb7Slm66018 static int	vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg);
1530a55fbb7Slm66018 static int	vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg);
1540a55fbb7Slm66018 static int	vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *msg);
1553af08d82Slm66018 static int 	vdc_send_request(vdc_t *vdcp, int operation,
1563af08d82Slm66018 		    caddr_t addr, size_t nbytes, int slice, diskaddr_t offset,
1573af08d82Slm66018 		    int cb_type, void *cb_arg, vio_desc_direction_t dir);
1583af08d82Slm66018 static int	vdc_map_to_shared_dring(vdc_t *vdcp, int idx);
1593af08d82Slm66018 static int 	vdc_populate_descriptor(vdc_t *vdcp, int operation,
1603af08d82Slm66018 		    caddr_t addr, size_t nbytes, int slice, diskaddr_t offset,
1613af08d82Slm66018 		    int cb_type, void *cb_arg, vio_desc_direction_t dir);
1623af08d82Slm66018 static int 	vdc_do_sync_op(vdc_t *vdcp, int operation,
1633af08d82Slm66018 		    caddr_t addr, size_t nbytes, int slice, diskaddr_t offset,
1643af08d82Slm66018 		    int cb_type, void *cb_arg, vio_desc_direction_t dir);
1653af08d82Slm66018 
1663af08d82Slm66018 static int	vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp);
1673af08d82Slm66018 static int	vdc_drain_response(vdc_t *vdcp);
1681ae08745Sheppo static int	vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx);
1693af08d82Slm66018 static int	vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep);
170e1ebb9ecSlm66018 static int	vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg);
1711ae08745Sheppo 
1721ae08745Sheppo /* dkio */
1731ae08745Sheppo static int	vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode);
1741ae08745Sheppo static int	vdc_create_fake_geometry(vdc_t *vdc);
1750a55fbb7Slm66018 static int	vdc_setup_disk_layout(vdc_t *vdc);
176d10e4ef2Snarayan static int	vdc_null_copy_func(vdc_t *vdc, void *from, void *to,
177d10e4ef2Snarayan 		    int mode, int dir);
1784bac2208Snarayan static int	vdc_get_wce_convert(vdc_t *vdc, void *from, void *to,
1794bac2208Snarayan 		    int mode, int dir);
1804bac2208Snarayan static int	vdc_set_wce_convert(vdc_t *vdc, void *from, void *to,
1814bac2208Snarayan 		    int mode, int dir);
182d10e4ef2Snarayan static int	vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to,
183d10e4ef2Snarayan 		    int mode, int dir);
184d10e4ef2Snarayan static int	vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to,
185d10e4ef2Snarayan 		    int mode, int dir);
186d10e4ef2Snarayan static int	vdc_get_geom_convert(vdc_t *vdc, void *from, void *to,
187d10e4ef2Snarayan 		    int mode, int dir);
188d10e4ef2Snarayan static int	vdc_set_geom_convert(vdc_t *vdc, void *from, void *to,
189d10e4ef2Snarayan 		    int mode, int dir);
190d10e4ef2Snarayan static int	vdc_uscsicmd_convert(vdc_t *vdc, void *from, void *to,
191d10e4ef2Snarayan 		    int mode, int dir);
1924bac2208Snarayan static int	vdc_get_efi_convert(vdc_t *vdc, void *from, void *to,
1934bac2208Snarayan 		    int mode, int dir);
1944bac2208Snarayan static int	vdc_set_efi_convert(vdc_t *vdc, void *from, void *to,
1954bac2208Snarayan 		    int mode, int dir);
1961ae08745Sheppo 
1971ae08745Sheppo /*
1981ae08745Sheppo  * Module variables
1991ae08745Sheppo  */
200e1ebb9ecSlm66018 
201e1ebb9ecSlm66018 /*
202e1ebb9ecSlm66018  * Tunable variables to control how long vdc waits before timing out on
203e1ebb9ecSlm66018  * various operations
204e1ebb9ecSlm66018  */
205e1ebb9ecSlm66018 static int	vdc_retries = 10;
2063c96341aSnarayan static int	vdc_hshake_retries = 3;
207e1ebb9ecSlm66018 
208e1ebb9ecSlm66018 /* calculated from 'vdc_usec_timeout' during attach */
209e1ebb9ecSlm66018 static uint64_t	vdc_hz_timeout;				/* units: Hz */
210e1ebb9ecSlm66018 static uint64_t	vdc_usec_timeout = 30 * MICROSEC;	/* 30s units: ns */
211e1ebb9ecSlm66018 
2123af08d82Slm66018 static uint64_t vdc_hz_min_ldc_delay;
2133af08d82Slm66018 static uint64_t vdc_min_timeout_ldc = 1 * MILLISEC;
2143af08d82Slm66018 static uint64_t vdc_hz_max_ldc_delay;
2153af08d82Slm66018 static uint64_t vdc_max_timeout_ldc = 100 * MILLISEC;
2163af08d82Slm66018 
2173af08d82Slm66018 static uint64_t vdc_ldc_read_init_delay = 1 * MILLISEC;
2183af08d82Slm66018 static uint64_t vdc_ldc_read_max_delay = 100 * MILLISEC;
219e1ebb9ecSlm66018 
220e1ebb9ecSlm66018 /* values for dumping - need to run in a tighter loop */
221e1ebb9ecSlm66018 static uint64_t	vdc_usec_timeout_dump = 100 * MILLISEC;	/* 0.1s units: ns */
222e1ebb9ecSlm66018 static int	vdc_dump_retries = 100;
223e1ebb9ecSlm66018 
224e1ebb9ecSlm66018 /* Count of the number of vdc instances attached */
225e1ebb9ecSlm66018 static volatile uint32_t	vdc_instance_count = 0;
2261ae08745Sheppo 
2271ae08745Sheppo /* Soft state pointer */
2281ae08745Sheppo static void	*vdc_state;
2291ae08745Sheppo 
2303af08d82Slm66018 /*
2313af08d82Slm66018  * Controlling the verbosity of the error/debug messages
2323af08d82Slm66018  *
2333af08d82Slm66018  * vdc_msglevel - controls level of messages
2343af08d82Slm66018  * vdc_matchinst - 64-bit variable where each bit corresponds
2353af08d82Slm66018  *                 to the vdc instance the vdc_msglevel applies.
2363af08d82Slm66018  */
2373af08d82Slm66018 int		vdc_msglevel = 0x0;
2383af08d82Slm66018 uint64_t	vdc_matchinst = 0ull;
2391ae08745Sheppo 
2400a55fbb7Slm66018 /*
2410a55fbb7Slm66018  * Supported vDisk protocol version pairs.
2420a55fbb7Slm66018  *
2430a55fbb7Slm66018  * The first array entry is the latest and preferred version.
2440a55fbb7Slm66018  */
2450a55fbb7Slm66018 static const vio_ver_t	vdc_version[] = {{1, 0}};
2461ae08745Sheppo 
2471ae08745Sheppo static struct cb_ops vdc_cb_ops = {
2481ae08745Sheppo 	vdc_open,	/* cb_open */
2491ae08745Sheppo 	vdc_close,	/* cb_close */
2501ae08745Sheppo 	vdc_strategy,	/* cb_strategy */
2511ae08745Sheppo 	vdc_print,	/* cb_print */
2521ae08745Sheppo 	vdc_dump,	/* cb_dump */
2531ae08745Sheppo 	vdc_read,	/* cb_read */
2541ae08745Sheppo 	vdc_write,	/* cb_write */
2551ae08745Sheppo 	vdc_ioctl,	/* cb_ioctl */
2561ae08745Sheppo 	nodev,		/* cb_devmap */
2571ae08745Sheppo 	nodev,		/* cb_mmap */
2581ae08745Sheppo 	nodev,		/* cb_segmap */
2591ae08745Sheppo 	nochpoll,	/* cb_chpoll */
2601ae08745Sheppo 	ddi_prop_op,	/* cb_prop_op */
2611ae08745Sheppo 	NULL,		/* cb_str */
2621ae08745Sheppo 	D_MP | D_64BIT,	/* cb_flag */
2631ae08745Sheppo 	CB_REV,		/* cb_rev */
2641ae08745Sheppo 	vdc_aread,	/* cb_aread */
2651ae08745Sheppo 	vdc_awrite	/* cb_awrite */
2661ae08745Sheppo };
2671ae08745Sheppo 
2681ae08745Sheppo static struct dev_ops vdc_ops = {
2691ae08745Sheppo 	DEVO_REV,	/* devo_rev */
2701ae08745Sheppo 	0,		/* devo_refcnt */
2711ae08745Sheppo 	vdc_getinfo,	/* devo_getinfo */
2721ae08745Sheppo 	nulldev,	/* devo_identify */
2731ae08745Sheppo 	nulldev,	/* devo_probe */
2741ae08745Sheppo 	vdc_attach,	/* devo_attach */
2751ae08745Sheppo 	vdc_detach,	/* devo_detach */
2761ae08745Sheppo 	nodev,		/* devo_reset */
2771ae08745Sheppo 	&vdc_cb_ops,	/* devo_cb_ops */
2781ae08745Sheppo 	NULL,		/* devo_bus_ops */
2791ae08745Sheppo 	nulldev		/* devo_power */
2801ae08745Sheppo };
2811ae08745Sheppo 
2821ae08745Sheppo static struct modldrv modldrv = {
2831ae08745Sheppo 	&mod_driverops,
2841ae08745Sheppo 	"virtual disk client %I%",
2851ae08745Sheppo 	&vdc_ops,
2861ae08745Sheppo };
2871ae08745Sheppo 
2881ae08745Sheppo static struct modlinkage modlinkage = {
2891ae08745Sheppo 	MODREV_1,
2901ae08745Sheppo 	&modldrv,
2911ae08745Sheppo 	NULL
2921ae08745Sheppo };
2931ae08745Sheppo 
2941ae08745Sheppo /* -------------------------------------------------------------------------- */
2951ae08745Sheppo 
2961ae08745Sheppo /*
2971ae08745Sheppo  * Device Driver housekeeping and setup
2981ae08745Sheppo  */
2991ae08745Sheppo 
3001ae08745Sheppo int
3011ae08745Sheppo _init(void)
3021ae08745Sheppo {
3031ae08745Sheppo 	int	status;
3041ae08745Sheppo 
3051ae08745Sheppo 	if ((status = ddi_soft_state_init(&vdc_state, sizeof (vdc_t), 1)) != 0)
3061ae08745Sheppo 		return (status);
3071ae08745Sheppo 	if ((status = mod_install(&modlinkage)) != 0)
3081ae08745Sheppo 		ddi_soft_state_fini(&vdc_state);
3094bac2208Snarayan 	vdc_efi_init(vd_process_ioctl);
3101ae08745Sheppo 	return (status);
3111ae08745Sheppo }
3121ae08745Sheppo 
3131ae08745Sheppo int
3141ae08745Sheppo _info(struct modinfo *modinfop)
3151ae08745Sheppo {
3161ae08745Sheppo 	return (mod_info(&modlinkage, modinfop));
3171ae08745Sheppo }
3181ae08745Sheppo 
3191ae08745Sheppo int
3201ae08745Sheppo _fini(void)
3211ae08745Sheppo {
3221ae08745Sheppo 	int	status;
3231ae08745Sheppo 
3241ae08745Sheppo 	if ((status = mod_remove(&modlinkage)) != 0)
3251ae08745Sheppo 		return (status);
3264bac2208Snarayan 	vdc_efi_fini();
3271ae08745Sheppo 	ddi_soft_state_fini(&vdc_state);
3281ae08745Sheppo 	return (0);
3291ae08745Sheppo }
3301ae08745Sheppo 
3311ae08745Sheppo static int
3321ae08745Sheppo vdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,  void *arg, void **resultp)
3331ae08745Sheppo {
3341ae08745Sheppo 	_NOTE(ARGUNUSED(dip))
3351ae08745Sheppo 
3360d0c8d4bSnarayan 	int	instance = VDCUNIT((dev_t)arg);
3371ae08745Sheppo 	vdc_t	*vdc = NULL;
3381ae08745Sheppo 
3391ae08745Sheppo 	switch (cmd) {
3401ae08745Sheppo 	case DDI_INFO_DEVT2DEVINFO:
3411ae08745Sheppo 		if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
3421ae08745Sheppo 			*resultp = NULL;
3431ae08745Sheppo 			return (DDI_FAILURE);
3441ae08745Sheppo 		}
3451ae08745Sheppo 		*resultp = vdc->dip;
3461ae08745Sheppo 		return (DDI_SUCCESS);
3471ae08745Sheppo 	case DDI_INFO_DEVT2INSTANCE:
3481ae08745Sheppo 		*resultp = (void *)(uintptr_t)instance;
3491ae08745Sheppo 		return (DDI_SUCCESS);
3501ae08745Sheppo 	default:
3511ae08745Sheppo 		*resultp = NULL;
3521ae08745Sheppo 		return (DDI_FAILURE);
3531ae08745Sheppo 	}
3541ae08745Sheppo }
3551ae08745Sheppo 
3561ae08745Sheppo static int
3571ae08745Sheppo vdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3581ae08745Sheppo {
3591ae08745Sheppo 	int	instance;
3601ae08745Sheppo 	int	rv;
3611ae08745Sheppo 	vdc_t	*vdc = NULL;
3621ae08745Sheppo 
3631ae08745Sheppo 	switch (cmd) {
3641ae08745Sheppo 	case DDI_DETACH:
3651ae08745Sheppo 		/* the real work happens below */
3661ae08745Sheppo 		break;
3671ae08745Sheppo 	case DDI_SUSPEND:
3681ae08745Sheppo 		/* nothing to do for this non-device */
3691ae08745Sheppo 		return (DDI_SUCCESS);
3701ae08745Sheppo 	default:
3711ae08745Sheppo 		return (DDI_FAILURE);
3721ae08745Sheppo 	}
3731ae08745Sheppo 
3741ae08745Sheppo 	ASSERT(cmd == DDI_DETACH);
3751ae08745Sheppo 	instance = ddi_get_instance(dip);
3763af08d82Slm66018 	DMSGX(1, "[%d] Entered\n", instance);
3771ae08745Sheppo 
3781ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
379e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
3801ae08745Sheppo 		return (DDI_FAILURE);
3811ae08745Sheppo 	}
3821ae08745Sheppo 
3833af08d82Slm66018 	if (vdc->open_count) {
3843af08d82Slm66018 		DMSG(vdc, 0, "[%d] Cannot detach: device is open", instance);
3851ae08745Sheppo 		return (DDI_FAILURE);
3861ae08745Sheppo 	}
3871ae08745Sheppo 
3883af08d82Slm66018 	DMSG(vdc, 0, "[%d] proceeding...\n", instance);
3893af08d82Slm66018 
3903af08d82Slm66018 	/* mark instance as detaching */
3913af08d82Slm66018 	vdc->lifecycle	= VDC_LC_DETACHING;
3921ae08745Sheppo 
3931ae08745Sheppo 	/*
3941ae08745Sheppo 	 * try and disable callbacks to prevent another handshake
3951ae08745Sheppo 	 */
3961ae08745Sheppo 	rv = ldc_set_cb_mode(vdc->ldc_handle, LDC_CB_DISABLE);
3973af08d82Slm66018 	DMSG(vdc, 0, "callback disabled (rv=%d)\n", rv);
3981ae08745Sheppo 
3991ae08745Sheppo 	if (vdc->initialized & VDC_THREAD) {
4003af08d82Slm66018 		mutex_enter(&vdc->read_lock);
4013af08d82Slm66018 		if ((vdc->read_state == VDC_READ_WAITING) ||
4023af08d82Slm66018 		    (vdc->read_state == VDC_READ_RESET)) {
4033af08d82Slm66018 			vdc->read_state = VDC_READ_RESET;
4043af08d82Slm66018 			cv_signal(&vdc->read_cv);
4051ae08745Sheppo 		}
4063af08d82Slm66018 
4073af08d82Slm66018 		mutex_exit(&vdc->read_lock);
4083af08d82Slm66018 
4093af08d82Slm66018 		/* wake up any thread waiting for connection to come online */
4103af08d82Slm66018 		mutex_enter(&vdc->lock);
4113af08d82Slm66018 		if (vdc->state == VDC_STATE_INIT_WAITING) {
4123af08d82Slm66018 			DMSG(vdc, 0,
4133af08d82Slm66018 			    "[%d] write reset - move to resetting state...\n",
4143af08d82Slm66018 			    instance);
4153af08d82Slm66018 			vdc->state = VDC_STATE_RESETTING;
4163af08d82Slm66018 			cv_signal(&vdc->initwait_cv);
4173af08d82Slm66018 		}
4183af08d82Slm66018 		mutex_exit(&vdc->lock);
4193af08d82Slm66018 
4203af08d82Slm66018 		/* now wait until state transitions to VDC_STATE_DETACH */
4213af08d82Slm66018 		thread_join(vdc->msg_proc_thr->t_did);
4223af08d82Slm66018 		ASSERT(vdc->state == VDC_STATE_DETACH);
4233af08d82Slm66018 		DMSG(vdc, 0, "[%d] Reset thread exit and join ..\n",
4243af08d82Slm66018 		    vdc->instance);
4251ae08745Sheppo 	}
4261ae08745Sheppo 
4271ae08745Sheppo 	mutex_enter(&vdc->lock);
4281ae08745Sheppo 
4291ae08745Sheppo 	if (vdc->initialized & VDC_DRING)
4301ae08745Sheppo 		vdc_destroy_descriptor_ring(vdc);
4311ae08745Sheppo 
4321ae08745Sheppo 	if (vdc->initialized & VDC_LDC)
4331ae08745Sheppo 		vdc_terminate_ldc(vdc);
4341ae08745Sheppo 
4351ae08745Sheppo 	mutex_exit(&vdc->lock);
4361ae08745Sheppo 
4371ae08745Sheppo 	if (vdc->initialized & VDC_MINOR) {
4381ae08745Sheppo 		ddi_prop_remove_all(dip);
4391ae08745Sheppo 		ddi_remove_minor_node(dip, NULL);
4401ae08745Sheppo 	}
4411ae08745Sheppo 
4421ae08745Sheppo 	if (vdc->initialized & VDC_LOCKS) {
4431ae08745Sheppo 		mutex_destroy(&vdc->lock);
4443af08d82Slm66018 		mutex_destroy(&vdc->read_lock);
4453af08d82Slm66018 		cv_destroy(&vdc->initwait_cv);
4463af08d82Slm66018 		cv_destroy(&vdc->dring_free_cv);
4473af08d82Slm66018 		cv_destroy(&vdc->membind_cv);
4483af08d82Slm66018 		cv_destroy(&vdc->sync_pending_cv);
4493af08d82Slm66018 		cv_destroy(&vdc->sync_blocked_cv);
4503af08d82Slm66018 		cv_destroy(&vdc->read_cv);
4513af08d82Slm66018 		cv_destroy(&vdc->running_cv);
4521ae08745Sheppo 	}
4531ae08745Sheppo 
4541ae08745Sheppo 	if (vdc->minfo)
4551ae08745Sheppo 		kmem_free(vdc->minfo, sizeof (struct dk_minfo));
4561ae08745Sheppo 
4571ae08745Sheppo 	if (vdc->cinfo)
4581ae08745Sheppo 		kmem_free(vdc->cinfo, sizeof (struct dk_cinfo));
4591ae08745Sheppo 
4601ae08745Sheppo 	if (vdc->vtoc)
4611ae08745Sheppo 		kmem_free(vdc->vtoc, sizeof (struct vtoc));
4621ae08745Sheppo 
4630a55fbb7Slm66018 	if (vdc->label)
4640a55fbb7Slm66018 		kmem_free(vdc->label, DK_LABEL_SIZE);
4650a55fbb7Slm66018 
4664bac2208Snarayan 	if (vdc->devid) {
4674bac2208Snarayan 		ddi_devid_unregister(dip);
4684bac2208Snarayan 		ddi_devid_free(vdc->devid);
4694bac2208Snarayan 	}
4704bac2208Snarayan 
4711ae08745Sheppo 	if (vdc->initialized & VDC_SOFT_STATE)
4721ae08745Sheppo 		ddi_soft_state_free(vdc_state, instance);
4731ae08745Sheppo 
4743af08d82Slm66018 	DMSG(vdc, 0, "[%d] End %p\n", instance, (void *)vdc);
4751ae08745Sheppo 
4761ae08745Sheppo 	return (DDI_SUCCESS);
4771ae08745Sheppo }
4781ae08745Sheppo 
4791ae08745Sheppo 
4801ae08745Sheppo static int
4811ae08745Sheppo vdc_do_attach(dev_info_t *dip)
4821ae08745Sheppo {
4831ae08745Sheppo 	int		instance;
4841ae08745Sheppo 	vdc_t		*vdc = NULL;
4851ae08745Sheppo 	int		status;
4861ae08745Sheppo 
4871ae08745Sheppo 	ASSERT(dip != NULL);
4881ae08745Sheppo 
4891ae08745Sheppo 	instance = ddi_get_instance(dip);
4901ae08745Sheppo 	if (ddi_soft_state_zalloc(vdc_state, instance) != DDI_SUCCESS) {
491e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't alloc state structure",
492e1ebb9ecSlm66018 		    instance);
4931ae08745Sheppo 		return (DDI_FAILURE);
4941ae08745Sheppo 	}
4951ae08745Sheppo 
4961ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
497e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
4981ae08745Sheppo 		return (DDI_FAILURE);
4991ae08745Sheppo 	}
5001ae08745Sheppo 
5011ae08745Sheppo 	/*
5021ae08745Sheppo 	 * We assign the value to initialized in this case to zero out the
5031ae08745Sheppo 	 * variable and then set bits in it to indicate what has been done
5041ae08745Sheppo 	 */
5051ae08745Sheppo 	vdc->initialized = VDC_SOFT_STATE;
5061ae08745Sheppo 
5071ae08745Sheppo 	vdc_hz_timeout = drv_usectohz(vdc_usec_timeout);
5083af08d82Slm66018 
5093af08d82Slm66018 	vdc_hz_min_ldc_delay = drv_usectohz(vdc_min_timeout_ldc);
5103af08d82Slm66018 	vdc_hz_max_ldc_delay = drv_usectohz(vdc_max_timeout_ldc);
5111ae08745Sheppo 
5121ae08745Sheppo 	vdc->dip	= dip;
5131ae08745Sheppo 	vdc->instance	= instance;
5143af08d82Slm66018 	vdc->open_count	= 0;
5151ae08745Sheppo 	vdc->vdisk_type	= VD_DISK_TYPE_UNK;
5164bac2208Snarayan 	vdc->vdisk_label = VD_DISK_LABEL_UNK;
5173af08d82Slm66018 	vdc->state	= VDC_STATE_INIT;
5183af08d82Slm66018 	vdc->lifecycle	= VDC_LC_ATTACHING;
5191ae08745Sheppo 	vdc->ldc_state	= 0;
5201ae08745Sheppo 	vdc->session_id = 0;
5211ae08745Sheppo 	vdc->block_size = DEV_BSIZE;
5228e6a2a04Slm66018 	vdc->max_xfer_sz = maxphys / DEV_BSIZE;
5231ae08745Sheppo 
5241ae08745Sheppo 	vdc->vtoc = NULL;
5251ae08745Sheppo 	vdc->cinfo = NULL;
5261ae08745Sheppo 	vdc->minfo = NULL;
5271ae08745Sheppo 
5281ae08745Sheppo 	mutex_init(&vdc->lock, NULL, MUTEX_DRIVER, NULL);
5293af08d82Slm66018 	cv_init(&vdc->initwait_cv, NULL, CV_DRIVER, NULL);
5303af08d82Slm66018 	cv_init(&vdc->dring_free_cv, NULL, CV_DRIVER, NULL);
5313af08d82Slm66018 	cv_init(&vdc->membind_cv, NULL, CV_DRIVER, NULL);
5323af08d82Slm66018 	cv_init(&vdc->running_cv, NULL, CV_DRIVER, NULL);
5333af08d82Slm66018 
5343af08d82Slm66018 	vdc->threads_pending = 0;
5353af08d82Slm66018 	vdc->sync_op_pending = B_FALSE;
5363af08d82Slm66018 	vdc->sync_op_blocked = B_FALSE;
5373af08d82Slm66018 	cv_init(&vdc->sync_pending_cv, NULL, CV_DRIVER, NULL);
5383af08d82Slm66018 	cv_init(&vdc->sync_blocked_cv, NULL, CV_DRIVER, NULL);
5393af08d82Slm66018 
5403af08d82Slm66018 	/* init blocking msg read functionality */
5413af08d82Slm66018 	mutex_init(&vdc->read_lock, NULL, MUTEX_DRIVER, NULL);
5423af08d82Slm66018 	cv_init(&vdc->read_cv, NULL, CV_DRIVER, NULL);
5433af08d82Slm66018 	vdc->read_state = VDC_READ_IDLE;
5443af08d82Slm66018 
5451ae08745Sheppo 	vdc->initialized |= VDC_LOCKS;
5461ae08745Sheppo 
5473af08d82Slm66018 	/* initialise LDC channel which will be used to communicate with vds */
5483af08d82Slm66018 	if ((status = vdc_do_ldc_init(vdc)) != 0) {
5493af08d82Slm66018 		cmn_err(CE_NOTE, "[%d] Couldn't initialize LDC", instance);
5503af08d82Slm66018 		goto return_status;
5513af08d82Slm66018 	}
5523af08d82Slm66018 
5533af08d82Slm66018 	/* initialize the thread responsible for managing state with server */
5543af08d82Slm66018 	vdc->msg_proc_thr = thread_create(NULL, 0, vdc_process_msg_thread,
5551ae08745Sheppo 	    vdc, 0, &p0, TS_RUN, minclsyspri);
5563af08d82Slm66018 	if (vdc->msg_proc_thr == NULL) {
5571ae08745Sheppo 		cmn_err(CE_NOTE, "[%d] Failed to create msg processing thread",
5581ae08745Sheppo 		    instance);
5591ae08745Sheppo 		return (DDI_FAILURE);
5601ae08745Sheppo 	}
5613af08d82Slm66018 
5621ae08745Sheppo 	vdc->initialized |= VDC_THREAD;
5631ae08745Sheppo 
564e1ebb9ecSlm66018 	atomic_inc_32(&vdc_instance_count);
5651ae08745Sheppo 
5660a55fbb7Slm66018 	/*
5670a55fbb7Slm66018 	 * Once the handshake is complete, we can use the DRing to send
5680a55fbb7Slm66018 	 * requests to the vDisk server to calculate the geometry and
5690a55fbb7Slm66018 	 * VTOC of the "disk"
5700a55fbb7Slm66018 	 */
5710a55fbb7Slm66018 	status = vdc_setup_disk_layout(vdc);
5720a55fbb7Slm66018 	if (status != 0) {
5733af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to discover disk layout (err%d)",
5740a55fbb7Slm66018 			vdc->instance, status);
5753af08d82Slm66018 		goto return_status;
5761ae08745Sheppo 	}
5771ae08745Sheppo 
5781ae08745Sheppo 	/*
5791ae08745Sheppo 	 * Now that we have the device info we can create the
5801ae08745Sheppo 	 * device nodes and properties
5811ae08745Sheppo 	 */
5821ae08745Sheppo 	status = vdc_create_device_nodes(vdc);
5831ae08745Sheppo 	if (status) {
5843af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to create device nodes",
5851ae08745Sheppo 				instance);
5863af08d82Slm66018 		goto return_status;
5871ae08745Sheppo 	}
5881ae08745Sheppo 	status = vdc_create_device_nodes_props(vdc);
5891ae08745Sheppo 	if (status) {
5903af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to create device nodes"
5910a55fbb7Slm66018 				" properties (%d)", instance, status);
5923af08d82Slm66018 		goto return_status;
5931ae08745Sheppo 	}
5941ae08745Sheppo 
5954bac2208Snarayan 	/*
5964bac2208Snarayan 	 * Setup devid
5974bac2208Snarayan 	 */
5984bac2208Snarayan 	if (vdc_setup_devid(vdc)) {
5993af08d82Slm66018 		DMSG(vdc, 0, "[%d] No device id available\n", instance);
6004bac2208Snarayan 	}
6014bac2208Snarayan 
6021ae08745Sheppo 	ddi_report_dev(dip);
6033af08d82Slm66018 	vdc->lifecycle	= VDC_LC_ONLINE;
6043af08d82Slm66018 	DMSG(vdc, 0, "[%d] Attach tasks successful\n", instance);
6051ae08745Sheppo 
6063af08d82Slm66018 return_status:
6073af08d82Slm66018 	DMSG(vdc, 0, "[%d] Attach completed\n", instance);
6081ae08745Sheppo 	return (status);
6091ae08745Sheppo }
6101ae08745Sheppo 
6111ae08745Sheppo static int
6121ae08745Sheppo vdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
6131ae08745Sheppo {
6141ae08745Sheppo 	int	status;
6151ae08745Sheppo 
6161ae08745Sheppo 	switch (cmd) {
6171ae08745Sheppo 	case DDI_ATTACH:
6181ae08745Sheppo 		if ((status = vdc_do_attach(dip)) != 0)
6191ae08745Sheppo 			(void) vdc_detach(dip, DDI_DETACH);
6201ae08745Sheppo 		return (status);
6211ae08745Sheppo 	case DDI_RESUME:
6221ae08745Sheppo 		/* nothing to do for this non-device */
6231ae08745Sheppo 		return (DDI_SUCCESS);
6241ae08745Sheppo 	default:
6251ae08745Sheppo 		return (DDI_FAILURE);
6261ae08745Sheppo 	}
6271ae08745Sheppo }
6281ae08745Sheppo 
6291ae08745Sheppo static int
6301ae08745Sheppo vdc_do_ldc_init(vdc_t *vdc)
6311ae08745Sheppo {
6321ae08745Sheppo 	int			status = 0;
6331ae08745Sheppo 	ldc_status_t		ldc_state;
6341ae08745Sheppo 	ldc_attr_t		ldc_attr;
6351ae08745Sheppo 	uint64_t		ldc_id = 0;
6361ae08745Sheppo 	dev_info_t		*dip = NULL;
6371ae08745Sheppo 
6381ae08745Sheppo 	ASSERT(vdc != NULL);
6391ae08745Sheppo 
6401ae08745Sheppo 	dip = vdc->dip;
6411ae08745Sheppo 	vdc->initialized |= VDC_LDC;
6421ae08745Sheppo 
6431ae08745Sheppo 	if ((status = vdc_get_ldc_id(dip, &ldc_id)) != 0) {
6443af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to get LDC channel ID property",
645e1ebb9ecSlm66018 				vdc->instance);
6461ae08745Sheppo 		return (EIO);
6471ae08745Sheppo 	}
6481ae08745Sheppo 	vdc->ldc_id = ldc_id;
6491ae08745Sheppo 
6501ae08745Sheppo 	ldc_attr.devclass = LDC_DEV_BLK;
6511ae08745Sheppo 	ldc_attr.instance = vdc->instance;
6521ae08745Sheppo 	ldc_attr.mode = LDC_MODE_UNRELIABLE;	/* unreliable transport */
653e1ebb9ecSlm66018 	ldc_attr.mtu = VD_LDC_MTU;
6541ae08745Sheppo 
6551ae08745Sheppo 	if ((vdc->initialized & VDC_LDC_INIT) == 0) {
6561ae08745Sheppo 		status = ldc_init(ldc_id, &ldc_attr, &vdc->ldc_handle);
6571ae08745Sheppo 		if (status != 0) {
6583af08d82Slm66018 			DMSG(vdc, 0, "[%d] ldc_init(chan %ld) returned %d",
6591ae08745Sheppo 					vdc->instance, ldc_id, status);
6601ae08745Sheppo 			return (status);
6611ae08745Sheppo 		}
6621ae08745Sheppo 		vdc->initialized |= VDC_LDC_INIT;
6631ae08745Sheppo 	}
6641ae08745Sheppo 	status = ldc_status(vdc->ldc_handle, &ldc_state);
6651ae08745Sheppo 	if (status != 0) {
6663af08d82Slm66018 		DMSG(vdc, 0, "[%d] Cannot discover LDC status [err=%d]",
667e1ebb9ecSlm66018 				vdc->instance, status);
6681ae08745Sheppo 		return (status);
6691ae08745Sheppo 	}
6701ae08745Sheppo 	vdc->ldc_state = ldc_state;
6711ae08745Sheppo 
6721ae08745Sheppo 	if ((vdc->initialized & VDC_LDC_CB) == 0) {
6731ae08745Sheppo 		status = ldc_reg_callback(vdc->ldc_handle, vdc_handle_cb,
6741ae08745Sheppo 		    (caddr_t)vdc);
6751ae08745Sheppo 		if (status != 0) {
6763af08d82Slm66018 			DMSG(vdc, 0, "[%d] LDC callback reg. failed (%d)",
677e1ebb9ecSlm66018 					vdc->instance, status);
6781ae08745Sheppo 			return (status);
6791ae08745Sheppo 		}
6801ae08745Sheppo 		vdc->initialized |= VDC_LDC_CB;
6811ae08745Sheppo 	}
6821ae08745Sheppo 
6831ae08745Sheppo 	vdc->initialized |= VDC_LDC;
6841ae08745Sheppo 
6851ae08745Sheppo 	/*
6861ae08745Sheppo 	 * At this stage we have initialised LDC, we will now try and open
6871ae08745Sheppo 	 * the connection.
6881ae08745Sheppo 	 */
6891ae08745Sheppo 	if (vdc->ldc_state == LDC_INIT) {
6901ae08745Sheppo 		status = ldc_open(vdc->ldc_handle);
6911ae08745Sheppo 		if (status != 0) {
6923af08d82Slm66018 			DMSG(vdc, 0, "[%d] ldc_open(chan %ld) returned %d",
6931ae08745Sheppo 					vdc->instance, vdc->ldc_id, status);
6941ae08745Sheppo 			return (status);
6951ae08745Sheppo 		}
6961ae08745Sheppo 		vdc->initialized |= VDC_LDC_OPEN;
6971ae08745Sheppo 	}
6981ae08745Sheppo 
6991ae08745Sheppo 	return (status);
7001ae08745Sheppo }
7011ae08745Sheppo 
7021ae08745Sheppo static int
7031ae08745Sheppo vdc_start_ldc_connection(vdc_t *vdc)
7041ae08745Sheppo {
7051ae08745Sheppo 	int		status = 0;
7061ae08745Sheppo 
7071ae08745Sheppo 	ASSERT(vdc != NULL);
7081ae08745Sheppo 
7093af08d82Slm66018 	ASSERT(MUTEX_HELD(&vdc->lock));
7101ae08745Sheppo 
7110a55fbb7Slm66018 	status = vdc_do_ldc_up(vdc);
7121ae08745Sheppo 
7133af08d82Slm66018 	DMSG(vdc, 0, "[%d] Finished bringing up LDC\n", vdc->instance);
7141ae08745Sheppo 
7153af08d82Slm66018 	return (status);
7163af08d82Slm66018 }
7173af08d82Slm66018 
7183af08d82Slm66018 static int
7193af08d82Slm66018 vdc_stop_ldc_connection(vdc_t *vdcp)
7203af08d82Slm66018 {
7213af08d82Slm66018 	int	status;
7223af08d82Slm66018 
7233af08d82Slm66018 	DMSG(vdcp, 0, ": Resetting connection to vDisk server : state %d\n",
7243af08d82Slm66018 		vdcp->state);
7253af08d82Slm66018 
7263af08d82Slm66018 	status = ldc_down(vdcp->ldc_handle);
7273af08d82Slm66018 	DMSG(vdcp, 0, "ldc_down() = %d\n", status);
7283af08d82Slm66018 
7293af08d82Slm66018 	vdcp->initialized &= ~VDC_HANDSHAKE;
7303af08d82Slm66018 	DMSG(vdcp, 0, "initialized=%x\n", vdcp->initialized);
7311ae08745Sheppo 
7321ae08745Sheppo 	return (status);
7331ae08745Sheppo }
7341ae08745Sheppo 
7354bac2208Snarayan static int
7364bac2208Snarayan vdc_create_device_nodes_efi(vdc_t *vdc)
7374bac2208Snarayan {
7384bac2208Snarayan 	ddi_remove_minor_node(vdc->dip, "h");
7394bac2208Snarayan 	ddi_remove_minor_node(vdc->dip, "h,raw");
7404bac2208Snarayan 
7414bac2208Snarayan 	if (ddi_create_minor_node(vdc->dip, "wd", S_IFBLK,
7424bac2208Snarayan 		VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
7434bac2208Snarayan 		DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
7444bac2208Snarayan 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd'",
7454bac2208Snarayan 		    vdc->instance);
7464bac2208Snarayan 		return (EIO);
7474bac2208Snarayan 	}
7484bac2208Snarayan 
7494bac2208Snarayan 	/* if any device node is created we set this flag */
7504bac2208Snarayan 	vdc->initialized |= VDC_MINOR;
7514bac2208Snarayan 
7524bac2208Snarayan 	if (ddi_create_minor_node(vdc->dip, "wd,raw", S_IFCHR,
7534bac2208Snarayan 		VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
7544bac2208Snarayan 		DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
7554bac2208Snarayan 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd,raw'",
7564bac2208Snarayan 		    vdc->instance);
7574bac2208Snarayan 		return (EIO);
7584bac2208Snarayan 	}
7594bac2208Snarayan 
7604bac2208Snarayan 	return (0);
7614bac2208Snarayan }
7624bac2208Snarayan 
7634bac2208Snarayan static int
7644bac2208Snarayan vdc_create_device_nodes_vtoc(vdc_t *vdc)
7654bac2208Snarayan {
7664bac2208Snarayan 	ddi_remove_minor_node(vdc->dip, "wd");
7674bac2208Snarayan 	ddi_remove_minor_node(vdc->dip, "wd,raw");
7684bac2208Snarayan 
7694bac2208Snarayan 	if (ddi_create_minor_node(vdc->dip, "h", S_IFBLK,
7704bac2208Snarayan 		VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
7714bac2208Snarayan 		DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
7724bac2208Snarayan 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h'",
7734bac2208Snarayan 		    vdc->instance);
7744bac2208Snarayan 		return (EIO);
7754bac2208Snarayan 	}
7764bac2208Snarayan 
7774bac2208Snarayan 	/* if any device node is created we set this flag */
7784bac2208Snarayan 	vdc->initialized |= VDC_MINOR;
7794bac2208Snarayan 
7804bac2208Snarayan 	if (ddi_create_minor_node(vdc->dip, "h,raw", S_IFCHR,
7814bac2208Snarayan 		VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
7824bac2208Snarayan 		DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
7834bac2208Snarayan 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h,raw'",
7844bac2208Snarayan 		    vdc->instance);
7854bac2208Snarayan 		return (EIO);
7864bac2208Snarayan 	}
7874bac2208Snarayan 
7884bac2208Snarayan 	return (0);
7894bac2208Snarayan }
7901ae08745Sheppo 
7911ae08745Sheppo /*
7921ae08745Sheppo  * Function:
7931ae08745Sheppo  *	vdc_create_device_nodes
7941ae08745Sheppo  *
7951ae08745Sheppo  * Description:
7961ae08745Sheppo  *	This function creates the block and character device nodes under
7971ae08745Sheppo  *	/devices along with the node properties. It is called as part of
7981ae08745Sheppo  *	the attach(9E) of the instance during the handshake with vds after
7991ae08745Sheppo  *	vds has sent the attributes to vdc.
8001ae08745Sheppo  *
8011ae08745Sheppo  *	If the device is of type VD_DISK_TYPE_SLICE then the minor node
8021ae08745Sheppo  *	of 2 is used in keeping with the Solaris convention that slice 2
8031ae08745Sheppo  *	refers to a whole disk. Slices start at 'a'
8041ae08745Sheppo  *
8051ae08745Sheppo  * Parameters:
8061ae08745Sheppo  *	vdc 		- soft state pointer
8071ae08745Sheppo  *
8081ae08745Sheppo  * Return Values
8091ae08745Sheppo  *	0		- Success
8101ae08745Sheppo  *	EIO		- Failed to create node
8111ae08745Sheppo  *	EINVAL		- Unknown type of disk exported
8121ae08745Sheppo  */
8131ae08745Sheppo static int
8141ae08745Sheppo vdc_create_device_nodes(vdc_t *vdc)
8151ae08745Sheppo {
8164bac2208Snarayan 	char		name[sizeof ("s,raw")];
8171ae08745Sheppo 	dev_info_t	*dip = NULL;
8184bac2208Snarayan 	int		instance, status;
8191ae08745Sheppo 	int		num_slices = 1;
8201ae08745Sheppo 	int		i;
8211ae08745Sheppo 
8221ae08745Sheppo 	ASSERT(vdc != NULL);
8231ae08745Sheppo 
8241ae08745Sheppo 	instance = vdc->instance;
8251ae08745Sheppo 	dip = vdc->dip;
8261ae08745Sheppo 
8271ae08745Sheppo 	switch (vdc->vdisk_type) {
8281ae08745Sheppo 	case VD_DISK_TYPE_DISK:
8291ae08745Sheppo 		num_slices = V_NUMPAR;
8301ae08745Sheppo 		break;
8311ae08745Sheppo 	case VD_DISK_TYPE_SLICE:
8321ae08745Sheppo 		num_slices = 1;
8331ae08745Sheppo 		break;
8341ae08745Sheppo 	case VD_DISK_TYPE_UNK:
8351ae08745Sheppo 	default:
8361ae08745Sheppo 		return (EINVAL);
8371ae08745Sheppo 	}
8381ae08745Sheppo 
8394bac2208Snarayan 	/*
8404bac2208Snarayan 	 * Minor nodes are different for EFI disks: EFI disks do not have
8414bac2208Snarayan 	 * a minor node 'g' for the minor number corresponding to slice
8424bac2208Snarayan 	 * VD_EFI_WD_SLICE (slice 7) instead they have a minor node 'wd'
8434bac2208Snarayan 	 * representing the whole disk.
8444bac2208Snarayan 	 */
8451ae08745Sheppo 	for (i = 0; i < num_slices; i++) {
8464bac2208Snarayan 
8474bac2208Snarayan 		if (i == VD_EFI_WD_SLICE) {
8484bac2208Snarayan 			if (vdc->vdisk_label == VD_DISK_LABEL_EFI)
8494bac2208Snarayan 				status = vdc_create_device_nodes_efi(vdc);
8504bac2208Snarayan 			else
8514bac2208Snarayan 				status = vdc_create_device_nodes_vtoc(vdc);
8524bac2208Snarayan 			if (status != 0)
8534bac2208Snarayan 				return (status);
8544bac2208Snarayan 			continue;
8554bac2208Snarayan 		}
8564bac2208Snarayan 
8571ae08745Sheppo 		(void) snprintf(name, sizeof (name), "%c", 'a' + i);
8581ae08745Sheppo 		if (ddi_create_minor_node(dip, name, S_IFBLK,
8591ae08745Sheppo 		    VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
860e1ebb9ecSlm66018 			cmn_err(CE_NOTE, "[%d] Couldn't add block node '%s'",
861e1ebb9ecSlm66018 				instance, name);
8621ae08745Sheppo 			return (EIO);
8631ae08745Sheppo 		}
8641ae08745Sheppo 
8651ae08745Sheppo 		/* if any device node is created we set this flag */
8661ae08745Sheppo 		vdc->initialized |= VDC_MINOR;
8671ae08745Sheppo 
8681ae08745Sheppo 		(void) snprintf(name, sizeof (name), "%c%s",
8691ae08745Sheppo 			'a' + i, ",raw");
8701ae08745Sheppo 		if (ddi_create_minor_node(dip, name, S_IFCHR,
8711ae08745Sheppo 		    VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
872e1ebb9ecSlm66018 			cmn_err(CE_NOTE, "[%d] Couldn't add raw node '%s'",
873e1ebb9ecSlm66018 				instance, name);
8741ae08745Sheppo 			return (EIO);
8751ae08745Sheppo 		}
8761ae08745Sheppo 	}
8771ae08745Sheppo 
8781ae08745Sheppo 	return (0);
8791ae08745Sheppo }
8801ae08745Sheppo 
8811ae08745Sheppo /*
8821ae08745Sheppo  * Function:
8831ae08745Sheppo  *	vdc_create_device_nodes_props
8841ae08745Sheppo  *
8851ae08745Sheppo  * Description:
8861ae08745Sheppo  *	This function creates the block and character device nodes under
8871ae08745Sheppo  *	/devices along with the node properties. It is called as part of
8881ae08745Sheppo  *	the attach(9E) of the instance during the handshake with vds after
8891ae08745Sheppo  *	vds has sent the attributes to vdc.
8901ae08745Sheppo  *
8911ae08745Sheppo  * Parameters:
8921ae08745Sheppo  *	vdc 		- soft state pointer
8931ae08745Sheppo  *
8941ae08745Sheppo  * Return Values
8951ae08745Sheppo  *	0		- Success
8961ae08745Sheppo  *	EIO		- Failed to create device node property
8971ae08745Sheppo  *	EINVAL		- Unknown type of disk exported
8981ae08745Sheppo  */
8991ae08745Sheppo static int
9001ae08745Sheppo vdc_create_device_nodes_props(vdc_t *vdc)
9011ae08745Sheppo {
9021ae08745Sheppo 	dev_info_t	*dip = NULL;
9031ae08745Sheppo 	int		instance;
9041ae08745Sheppo 	int		num_slices = 1;
9051ae08745Sheppo 	int64_t		size = 0;
9061ae08745Sheppo 	dev_t		dev;
9071ae08745Sheppo 	int		rv;
9081ae08745Sheppo 	int		i;
9091ae08745Sheppo 
9101ae08745Sheppo 	ASSERT(vdc != NULL);
9111ae08745Sheppo 
9121ae08745Sheppo 	instance = vdc->instance;
9131ae08745Sheppo 	dip = vdc->dip;
9141ae08745Sheppo 
9151ae08745Sheppo 	if ((vdc->vtoc == NULL) || (vdc->vtoc->v_sanity != VTOC_SANE)) {
9163af08d82Slm66018 		DMSG(vdc, 0, "![%d] Could not create device node property."
9171ae08745Sheppo 				" No VTOC available", instance);
9181ae08745Sheppo 		return (ENXIO);
9191ae08745Sheppo 	}
9201ae08745Sheppo 
9211ae08745Sheppo 	switch (vdc->vdisk_type) {
9221ae08745Sheppo 	case VD_DISK_TYPE_DISK:
9231ae08745Sheppo 		num_slices = V_NUMPAR;
9241ae08745Sheppo 		break;
9251ae08745Sheppo 	case VD_DISK_TYPE_SLICE:
9261ae08745Sheppo 		num_slices = 1;
9271ae08745Sheppo 		break;
9281ae08745Sheppo 	case VD_DISK_TYPE_UNK:
9291ae08745Sheppo 	default:
9301ae08745Sheppo 		return (EINVAL);
9311ae08745Sheppo 	}
9321ae08745Sheppo 
9331ae08745Sheppo 	for (i = 0; i < num_slices; i++) {
9341ae08745Sheppo 		dev = makedevice(ddi_driver_major(dip),
9351ae08745Sheppo 			VD_MAKE_DEV(instance, i));
9361ae08745Sheppo 
9371ae08745Sheppo 		size = vdc->vtoc->v_part[i].p_size * vdc->vtoc->v_sectorsz;
9383af08d82Slm66018 		DMSG(vdc, 0, "[%d] sz %ld (%ld Mb)  p_size %lx\n",
939e1ebb9ecSlm66018 				instance, size, size / (1024 * 1024),
9401ae08745Sheppo 				vdc->vtoc->v_part[i].p_size);
9411ae08745Sheppo 
9421ae08745Sheppo 		rv = ddi_prop_update_int64(dev, dip, VDC_SIZE_PROP_NAME, size);
9431ae08745Sheppo 		if (rv != DDI_PROP_SUCCESS) {
944e1ebb9ecSlm66018 			cmn_err(CE_NOTE, "[%d] Couldn't add '%s' prop of [%ld]",
945e1ebb9ecSlm66018 				instance, VDC_SIZE_PROP_NAME, size);
9461ae08745Sheppo 			return (EIO);
9471ae08745Sheppo 		}
9481ae08745Sheppo 
9491ae08745Sheppo 		rv = ddi_prop_update_int64(dev, dip, VDC_NBLOCKS_PROP_NAME,
9501ae08745Sheppo 			lbtodb(size));
9511ae08745Sheppo 		if (rv != DDI_PROP_SUCCESS) {
952e1ebb9ecSlm66018 			cmn_err(CE_NOTE, "[%d] Couldn't add '%s' prop [%llu]",
9531ae08745Sheppo 				instance, VDC_NBLOCKS_PROP_NAME, lbtodb(size));
9541ae08745Sheppo 			return (EIO);
9551ae08745Sheppo 		}
9561ae08745Sheppo 	}
9571ae08745Sheppo 
9581ae08745Sheppo 	return (0);
9591ae08745Sheppo }
9601ae08745Sheppo 
9611ae08745Sheppo static int
9621ae08745Sheppo vdc_open(dev_t *dev, int flag, int otyp, cred_t *cred)
9631ae08745Sheppo {
9641ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
9651ae08745Sheppo 
9661ae08745Sheppo 	int		instance;
9671ae08745Sheppo 	vdc_t		*vdc;
9681ae08745Sheppo 
9691ae08745Sheppo 	ASSERT(dev != NULL);
9700d0c8d4bSnarayan 	instance = VDCUNIT(*dev);
9711ae08745Sheppo 
9721ae08745Sheppo 	if ((otyp != OTYP_CHR) && (otyp != OTYP_BLK))
9731ae08745Sheppo 		return (EINVAL);
9741ae08745Sheppo 
9751ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
976e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
9771ae08745Sheppo 		return (ENXIO);
9781ae08745Sheppo 	}
9791ae08745Sheppo 
9803af08d82Slm66018 	DMSG(vdc, 0, "minor = %d flag = %x, otyp = %x\n",
9813af08d82Slm66018 			getminor(*dev), flag, otyp);
9821ae08745Sheppo 
9831ae08745Sheppo 	mutex_enter(&vdc->lock);
9843af08d82Slm66018 	vdc->open_count++;
9851ae08745Sheppo 	mutex_exit(&vdc->lock);
9861ae08745Sheppo 
9871ae08745Sheppo 	return (0);
9881ae08745Sheppo }
9891ae08745Sheppo 
9901ae08745Sheppo static int
9911ae08745Sheppo vdc_close(dev_t dev, int flag, int otyp, cred_t *cred)
9921ae08745Sheppo {
9931ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
9941ae08745Sheppo 
9951ae08745Sheppo 	int	instance;
9961ae08745Sheppo 	vdc_t	*vdc;
9971ae08745Sheppo 
9980d0c8d4bSnarayan 	instance = VDCUNIT(dev);
9991ae08745Sheppo 
10001ae08745Sheppo 	if ((otyp != OTYP_CHR) && (otyp != OTYP_BLK))
10011ae08745Sheppo 		return (EINVAL);
10021ae08745Sheppo 
10031ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1004e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
10051ae08745Sheppo 		return (ENXIO);
10061ae08745Sheppo 	}
10071ae08745Sheppo 
10083af08d82Slm66018 	DMSG(vdc, 0, "[%d] flag = %x, otyp = %x\n", instance, flag, otyp);
10091ae08745Sheppo 	if (vdc->dkio_flush_pending) {
10103af08d82Slm66018 		DMSG(vdc, 0,
10113af08d82Slm66018 		    "[%d] Cannot detach: %d outstanding DKIO flushes\n",
1012e1ebb9ecSlm66018 		    instance, vdc->dkio_flush_pending);
10131ae08745Sheppo 		return (EBUSY);
10141ae08745Sheppo 	}
10151ae08745Sheppo 
10161ae08745Sheppo 	/*
10171ae08745Sheppo 	 * Should not need the mutex here, since the framework should protect
10181ae08745Sheppo 	 * against more opens on this device, but just in case.
10191ae08745Sheppo 	 */
10201ae08745Sheppo 	mutex_enter(&vdc->lock);
10213af08d82Slm66018 	vdc->open_count--;
10221ae08745Sheppo 	mutex_exit(&vdc->lock);
10231ae08745Sheppo 
10241ae08745Sheppo 	return (0);
10251ae08745Sheppo }
10261ae08745Sheppo 
10271ae08745Sheppo static int
10281ae08745Sheppo vdc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
10291ae08745Sheppo {
10301ae08745Sheppo 	_NOTE(ARGUNUSED(credp))
10311ae08745Sheppo 	_NOTE(ARGUNUSED(rvalp))
10321ae08745Sheppo 
10331ae08745Sheppo 	return (vd_process_ioctl(dev, cmd, (caddr_t)arg, mode));
10341ae08745Sheppo }
10351ae08745Sheppo 
10361ae08745Sheppo static int
10371ae08745Sheppo vdc_print(dev_t dev, char *str)
10381ae08745Sheppo {
10390d0c8d4bSnarayan 	cmn_err(CE_NOTE, "vdc%d:  %s", VDCUNIT(dev), str);
10401ae08745Sheppo 	return (0);
10411ae08745Sheppo }
10421ae08745Sheppo 
10431ae08745Sheppo static int
10441ae08745Sheppo vdc_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
10451ae08745Sheppo {
1046d10e4ef2Snarayan 	int	rv;
1047d10e4ef2Snarayan 	size_t	nbytes = nblk * DEV_BSIZE;
10480d0c8d4bSnarayan 	int	instance = VDCUNIT(dev);
1049d10e4ef2Snarayan 	vdc_t	*vdc = NULL;
10501ae08745Sheppo 
10511ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1052e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
10531ae08745Sheppo 		return (ENXIO);
10541ae08745Sheppo 	}
10551ae08745Sheppo 
10563af08d82Slm66018 	DMSG(vdc, 2, "[%d] dump %ld bytes at block 0x%lx : addr=0x%p\n",
10573af08d82Slm66018 	    instance, nbytes, blkno, (void *)addr);
10583af08d82Slm66018 	rv = vdc_send_request(vdc, VD_OP_BWRITE, addr, nbytes,
10590d0c8d4bSnarayan 	    VDCPART(dev), blkno, CB_STRATEGY, 0, VIO_write_dir);
10603af08d82Slm66018 	if (rv) {
10613af08d82Slm66018 		DMSG(vdc, 0, "Failed to do a disk dump (err=%d)\n", rv);
10621ae08745Sheppo 		return (rv);
10631ae08745Sheppo 	}
10641ae08745Sheppo 
10653af08d82Slm66018 	if (ddi_in_panic())
10663af08d82Slm66018 		(void) vdc_drain_response(vdc);
10673af08d82Slm66018 
10683af08d82Slm66018 	DMSG(vdc, 0, "[%d] End\n", instance);
10693af08d82Slm66018 
10703af08d82Slm66018 	return (0);
10713af08d82Slm66018 }
10723af08d82Slm66018 
10731ae08745Sheppo /* -------------------------------------------------------------------------- */
10741ae08745Sheppo 
10751ae08745Sheppo /*
10761ae08745Sheppo  * Disk access routines
10771ae08745Sheppo  *
10781ae08745Sheppo  */
10791ae08745Sheppo 
10801ae08745Sheppo /*
10811ae08745Sheppo  * vdc_strategy()
10821ae08745Sheppo  *
10831ae08745Sheppo  * Return Value:
10841ae08745Sheppo  *	0:	As per strategy(9E), the strategy() function must return 0
10851ae08745Sheppo  *		[ bioerror(9f) sets b_flags to the proper error code ]
10861ae08745Sheppo  */
10871ae08745Sheppo static int
10881ae08745Sheppo vdc_strategy(struct buf *buf)
10891ae08745Sheppo {
10901ae08745Sheppo 	int	rv = -1;
10911ae08745Sheppo 	vdc_t	*vdc = NULL;
10920d0c8d4bSnarayan 	int	instance = VDCUNIT(buf->b_edev);
10931ae08745Sheppo 	int	op = (buf->b_flags & B_READ) ? VD_OP_BREAD : VD_OP_BWRITE;
10941ae08745Sheppo 
10951ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1096e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
10971ae08745Sheppo 		bioerror(buf, ENXIO);
10981ae08745Sheppo 		biodone(buf);
10991ae08745Sheppo 		return (0);
11001ae08745Sheppo 	}
11011ae08745Sheppo 
11023af08d82Slm66018 	DMSG(vdc, 2, "[%d] %s %ld bytes at block %llx : b_addr=0x%p\n",
11033af08d82Slm66018 	    instance, (buf->b_flags & B_READ) ? "Read" : "Write",
11043af08d82Slm66018 	    buf->b_bcount, buf->b_lblkno, (void *)buf->b_un.b_addr);
1105d10e4ef2Snarayan 	DTRACE_IO2(vstart, buf_t *, buf, vdc_t *, vdc);
1106d10e4ef2Snarayan 
11071ae08745Sheppo 	bp_mapin(buf);
11081ae08745Sheppo 
11093af08d82Slm66018 	rv = vdc_send_request(vdc, op, (caddr_t)buf->b_un.b_addr,
11100d0c8d4bSnarayan 	    buf->b_bcount, VDCPART(buf->b_edev), buf->b_lblkno,
11113af08d82Slm66018 	    CB_STRATEGY, buf, (op == VD_OP_BREAD) ? VIO_read_dir :
11123af08d82Slm66018 	    VIO_write_dir);
11133af08d82Slm66018 
1114d10e4ef2Snarayan 	/*
1115d10e4ef2Snarayan 	 * If the request was successfully sent, the strategy call returns and
1116d10e4ef2Snarayan 	 * the ACK handler calls the bioxxx functions when the vDisk server is
1117d10e4ef2Snarayan 	 * done.
1118d10e4ef2Snarayan 	 */
1119d10e4ef2Snarayan 	if (rv) {
11203af08d82Slm66018 		DMSG(vdc, 0, "Failed to read/write (err=%d)\n", rv);
11211ae08745Sheppo 		bioerror(buf, rv);
11221ae08745Sheppo 		biodone(buf);
1123d10e4ef2Snarayan 	}
1124d10e4ef2Snarayan 
11251ae08745Sheppo 	return (0);
11261ae08745Sheppo }
11271ae08745Sheppo 
11280d0c8d4bSnarayan /*
11290d0c8d4bSnarayan  * Function:
11300d0c8d4bSnarayan  *	vdc_min
11310d0c8d4bSnarayan  *
11320d0c8d4bSnarayan  * Description:
11330d0c8d4bSnarayan  *	Routine to limit the size of a data transfer. Used in
11340d0c8d4bSnarayan  *	conjunction with physio(9F).
11350d0c8d4bSnarayan  *
11360d0c8d4bSnarayan  * Arguments:
11370d0c8d4bSnarayan  *	bp - pointer to the indicated buf(9S) struct.
11380d0c8d4bSnarayan  *
11390d0c8d4bSnarayan  */
11400d0c8d4bSnarayan static void
11410d0c8d4bSnarayan vdc_min(struct buf *bufp)
11420d0c8d4bSnarayan {
11430d0c8d4bSnarayan 	vdc_t	*vdc = NULL;
11440d0c8d4bSnarayan 	int	instance = VDCUNIT(bufp->b_edev);
11450d0c8d4bSnarayan 
11460d0c8d4bSnarayan 	vdc = ddi_get_soft_state(vdc_state, instance);
11470d0c8d4bSnarayan 	VERIFY(vdc != NULL);
11480d0c8d4bSnarayan 
11490d0c8d4bSnarayan 	if (bufp->b_bcount > (vdc->max_xfer_sz * vdc->block_size)) {
11500d0c8d4bSnarayan 		bufp->b_bcount = vdc->max_xfer_sz * vdc->block_size;
11510d0c8d4bSnarayan 	}
11520d0c8d4bSnarayan }
11531ae08745Sheppo 
11541ae08745Sheppo static int
11551ae08745Sheppo vdc_read(dev_t dev, struct uio *uio, cred_t *cred)
11561ae08745Sheppo {
11571ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
11581ae08745Sheppo 
11590d0c8d4bSnarayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
11600d0c8d4bSnarayan 	return (physio(vdc_strategy, NULL, dev, B_READ, vdc_min, uio));
11611ae08745Sheppo }
11621ae08745Sheppo 
11631ae08745Sheppo static int
11641ae08745Sheppo vdc_write(dev_t dev, struct uio *uio, cred_t *cred)
11651ae08745Sheppo {
11661ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
11671ae08745Sheppo 
11680d0c8d4bSnarayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
11690d0c8d4bSnarayan 	return (physio(vdc_strategy, NULL, dev, B_WRITE, vdc_min, uio));
11701ae08745Sheppo }
11711ae08745Sheppo 
11721ae08745Sheppo static int
11731ae08745Sheppo vdc_aread(dev_t dev, struct aio_req *aio, cred_t *cred)
11741ae08745Sheppo {
11751ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
11761ae08745Sheppo 
11770d0c8d4bSnarayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
11780d0c8d4bSnarayan 	return (aphysio(vdc_strategy, anocancel, dev, B_READ, vdc_min, aio));
11791ae08745Sheppo }
11801ae08745Sheppo 
11811ae08745Sheppo static int
11821ae08745Sheppo vdc_awrite(dev_t dev, struct aio_req *aio, cred_t *cred)
11831ae08745Sheppo {
11841ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
11851ae08745Sheppo 
11860d0c8d4bSnarayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
11870d0c8d4bSnarayan 	return (aphysio(vdc_strategy, anocancel, dev, B_WRITE, vdc_min, aio));
11881ae08745Sheppo }
11891ae08745Sheppo 
11901ae08745Sheppo 
11911ae08745Sheppo /* -------------------------------------------------------------------------- */
11921ae08745Sheppo 
11931ae08745Sheppo /*
11941ae08745Sheppo  * Handshake support
11951ae08745Sheppo  */
11961ae08745Sheppo 
11971ae08745Sheppo 
11980a55fbb7Slm66018 /*
11990a55fbb7Slm66018  * Function:
12000a55fbb7Slm66018  *	vdc_init_ver_negotiation()
12010a55fbb7Slm66018  *
12020a55fbb7Slm66018  * Description:
12030a55fbb7Slm66018  *
12040a55fbb7Slm66018  * Arguments:
12050a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
12060a55fbb7Slm66018  *
12070a55fbb7Slm66018  * Return Code:
12080a55fbb7Slm66018  *	0	- Success
12090a55fbb7Slm66018  */
12101ae08745Sheppo static int
12110a55fbb7Slm66018 vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver)
12121ae08745Sheppo {
12131ae08745Sheppo 	vio_ver_msg_t	pkt;
12141ae08745Sheppo 	size_t		msglen = sizeof (pkt);
12151ae08745Sheppo 	int		status = -1;
12161ae08745Sheppo 
12171ae08745Sheppo 	ASSERT(vdc != NULL);
12181ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
12191ae08745Sheppo 
12203af08d82Slm66018 	DMSG(vdc, 0, "[%d] Entered.\n", vdc->instance);
1221e1ebb9ecSlm66018 
12221ae08745Sheppo 	/*
12231ae08745Sheppo 	 * set the Session ID to a unique value
12241ae08745Sheppo 	 * (the lower 32 bits of the clock tick)
12251ae08745Sheppo 	 */
12261ae08745Sheppo 	vdc->session_id = ((uint32_t)gettick() & 0xffffffff);
12273af08d82Slm66018 	DMSG(vdc, 0, "[%d] Set SID to 0x%lx\n", vdc->instance, vdc->session_id);
12281ae08745Sheppo 
12291ae08745Sheppo 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
12301ae08745Sheppo 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
12311ae08745Sheppo 	pkt.tag.vio_subtype_env = VIO_VER_INFO;
12321ae08745Sheppo 	pkt.tag.vio_sid = vdc->session_id;
12331ae08745Sheppo 	pkt.dev_class = VDEV_DISK;
12340a55fbb7Slm66018 	pkt.ver_major = ver.major;
12350a55fbb7Slm66018 	pkt.ver_minor = ver.minor;
12361ae08745Sheppo 
12370a55fbb7Slm66018 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
12383af08d82Slm66018 	DMSG(vdc, 0, "[%d] Ver info sent (status = %d)\n",
12393af08d82Slm66018 	    vdc->instance, status);
12401ae08745Sheppo 	if ((status != 0) || (msglen != sizeof (vio_ver_msg_t))) {
12413af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to send Ver negotiation info: "
1242e1ebb9ecSlm66018 				"id(%lx) rv(%d) size(%ld)",
1243e1ebb9ecSlm66018 				vdc->instance, vdc->ldc_handle,
12441ae08745Sheppo 				status, msglen);
12451ae08745Sheppo 		if (msglen != sizeof (vio_ver_msg_t))
12461ae08745Sheppo 			status = ENOMSG;
12471ae08745Sheppo 	}
12481ae08745Sheppo 
12491ae08745Sheppo 	return (status);
12501ae08745Sheppo }
12511ae08745Sheppo 
12520a55fbb7Slm66018 /*
12530a55fbb7Slm66018  * Function:
12543af08d82Slm66018  *	vdc_ver_negotiation()
12553af08d82Slm66018  *
12563af08d82Slm66018  * Description:
12573af08d82Slm66018  *
12583af08d82Slm66018  * Arguments:
12593af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
12603af08d82Slm66018  *
12613af08d82Slm66018  * Return Code:
12623af08d82Slm66018  *	0	- Success
12633af08d82Slm66018  */
12643af08d82Slm66018 static int
12653af08d82Slm66018 vdc_ver_negotiation(vdc_t *vdcp)
12663af08d82Slm66018 {
12673af08d82Slm66018 	vio_msg_t vio_msg;
12683af08d82Slm66018 	int status;
12693af08d82Slm66018 
12703af08d82Slm66018 	if (status = vdc_init_ver_negotiation(vdcp, vdc_version[0]))
12713af08d82Slm66018 		return (status);
12723af08d82Slm66018 
12733af08d82Slm66018 	/* release lock and wait for response */
12743af08d82Slm66018 	mutex_exit(&vdcp->lock);
12753af08d82Slm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
12763af08d82Slm66018 	mutex_enter(&vdcp->lock);
12773af08d82Slm66018 	if (status) {
12783af08d82Slm66018 		DMSG(vdcp, 0,
12793af08d82Slm66018 		    "[%d] Failed waiting for Ver negotiation response, rv(%d)",
12803af08d82Slm66018 		    vdcp->instance, status);
12813af08d82Slm66018 		return (status);
12823af08d82Slm66018 	}
12833af08d82Slm66018 
12843af08d82Slm66018 	/* check type and sub_type ... */
12853af08d82Slm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
12863af08d82Slm66018 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
12873af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Invalid ver negotiation response\n",
12883af08d82Slm66018 				vdcp->instance);
12893af08d82Slm66018 		return (EPROTO);
12903af08d82Slm66018 	}
12913af08d82Slm66018 
12923af08d82Slm66018 	return (vdc_handle_ver_msg(vdcp, (vio_ver_msg_t *)&vio_msg));
12933af08d82Slm66018 }
12943af08d82Slm66018 
12953af08d82Slm66018 /*
12963af08d82Slm66018  * Function:
12970a55fbb7Slm66018  *	vdc_init_attr_negotiation()
12980a55fbb7Slm66018  *
12990a55fbb7Slm66018  * Description:
13000a55fbb7Slm66018  *
13010a55fbb7Slm66018  * Arguments:
13020a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
13030a55fbb7Slm66018  *
13040a55fbb7Slm66018  * Return Code:
13050a55fbb7Slm66018  *	0	- Success
13060a55fbb7Slm66018  */
13071ae08745Sheppo static int
13081ae08745Sheppo vdc_init_attr_negotiation(vdc_t *vdc)
13091ae08745Sheppo {
13101ae08745Sheppo 	vd_attr_msg_t	pkt;
13111ae08745Sheppo 	size_t		msglen = sizeof (pkt);
13121ae08745Sheppo 	int		status;
13131ae08745Sheppo 
13141ae08745Sheppo 	ASSERT(vdc != NULL);
13151ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
13161ae08745Sheppo 
13173af08d82Slm66018 	DMSG(vdc, 0, "[%d] entered\n", vdc->instance);
13181ae08745Sheppo 
13191ae08745Sheppo 	/* fill in tag */
13201ae08745Sheppo 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
13211ae08745Sheppo 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
13221ae08745Sheppo 	pkt.tag.vio_subtype_env = VIO_ATTR_INFO;
13231ae08745Sheppo 	pkt.tag.vio_sid = vdc->session_id;
13241ae08745Sheppo 	/* fill in payload */
13251ae08745Sheppo 	pkt.max_xfer_sz = vdc->max_xfer_sz;
13261ae08745Sheppo 	pkt.vdisk_block_size = vdc->block_size;
13271ae08745Sheppo 	pkt.xfer_mode = VIO_DRING_MODE;
13281ae08745Sheppo 	pkt.operations = 0;	/* server will set bits of valid operations */
13291ae08745Sheppo 	pkt.vdisk_type = 0;	/* server will set to valid device type */
13301ae08745Sheppo 	pkt.vdisk_size = 0;	/* server will set to valid size */
13311ae08745Sheppo 
13320a55fbb7Slm66018 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
13333af08d82Slm66018 	DMSG(vdc, 0, "Attr info sent (status = %d)\n", status);
13341ae08745Sheppo 
13351ae08745Sheppo 	if ((status != 0) || (msglen != sizeof (vio_ver_msg_t))) {
13363af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to send Attr negotiation info: "
1337e1ebb9ecSlm66018 				"id(%lx) rv(%d) size(%ld)",
1338e1ebb9ecSlm66018 				vdc->instance, vdc->ldc_handle,
13391ae08745Sheppo 				status, msglen);
13401ae08745Sheppo 		if (msglen != sizeof (vio_ver_msg_t))
13411ae08745Sheppo 			status = ENOMSG;
13421ae08745Sheppo 	}
13431ae08745Sheppo 
13441ae08745Sheppo 	return (status);
13451ae08745Sheppo }
13461ae08745Sheppo 
13470a55fbb7Slm66018 /*
13480a55fbb7Slm66018  * Function:
13493af08d82Slm66018  *	vdc_attr_negotiation()
13503af08d82Slm66018  *
13513af08d82Slm66018  * Description:
13523af08d82Slm66018  *
13533af08d82Slm66018  * Arguments:
13543af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
13553af08d82Slm66018  *
13563af08d82Slm66018  * Return Code:
13573af08d82Slm66018  *	0	- Success
13583af08d82Slm66018  */
13593af08d82Slm66018 static int
13603af08d82Slm66018 vdc_attr_negotiation(vdc_t *vdcp)
13613af08d82Slm66018 {
13623af08d82Slm66018 	int status;
13633af08d82Slm66018 	vio_msg_t vio_msg;
13643af08d82Slm66018 
13653af08d82Slm66018 	if (status = vdc_init_attr_negotiation(vdcp))
13663af08d82Slm66018 		return (status);
13673af08d82Slm66018 
13683af08d82Slm66018 	/* release lock and wait for response */
13693af08d82Slm66018 	mutex_exit(&vdcp->lock);
13703af08d82Slm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
13713af08d82Slm66018 	mutex_enter(&vdcp->lock);
13723af08d82Slm66018 	if (status) {
13733af08d82Slm66018 		DMSG(vdcp, 0,
13743af08d82Slm66018 		    "[%d] Failed waiting for Attr negotiation response, rv(%d)",
13753af08d82Slm66018 		    vdcp->instance, status);
13763af08d82Slm66018 		return (status);
13773af08d82Slm66018 	}
13783af08d82Slm66018 
13793af08d82Slm66018 	/* check type and sub_type ... */
13803af08d82Slm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
13813af08d82Slm66018 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
13823af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Invalid attr negotiation response\n",
13833af08d82Slm66018 				vdcp->instance);
13843af08d82Slm66018 		return (EPROTO);
13853af08d82Slm66018 	}
13863af08d82Slm66018 
13873af08d82Slm66018 	return (vdc_handle_attr_msg(vdcp, (vd_attr_msg_t *)&vio_msg));
13883af08d82Slm66018 }
13893af08d82Slm66018 
13903af08d82Slm66018 
13913af08d82Slm66018 /*
13923af08d82Slm66018  * Function:
13930a55fbb7Slm66018  *	vdc_init_dring_negotiate()
13940a55fbb7Slm66018  *
13950a55fbb7Slm66018  * Description:
13960a55fbb7Slm66018  *
13970a55fbb7Slm66018  * Arguments:
13980a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
13990a55fbb7Slm66018  *
14000a55fbb7Slm66018  * Return Code:
14010a55fbb7Slm66018  *	0	- Success
14020a55fbb7Slm66018  */
14031ae08745Sheppo static int
14041ae08745Sheppo vdc_init_dring_negotiate(vdc_t *vdc)
14051ae08745Sheppo {
14061ae08745Sheppo 	vio_dring_reg_msg_t	pkt;
14071ae08745Sheppo 	size_t			msglen = sizeof (pkt);
14081ae08745Sheppo 	int			status = -1;
14093af08d82Slm66018 	int			retry;
14103af08d82Slm66018 	int			nretries = 10;
14111ae08745Sheppo 
14121ae08745Sheppo 	ASSERT(vdc != NULL);
14131ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
14141ae08745Sheppo 
14153af08d82Slm66018 	for (retry = 0; retry < nretries; retry++) {
14161ae08745Sheppo 		status = vdc_init_descriptor_ring(vdc);
14173af08d82Slm66018 		if (status != EAGAIN)
14183af08d82Slm66018 			break;
14193af08d82Slm66018 		drv_usecwait(vdc_min_timeout_ldc);
14203af08d82Slm66018 	}
14213af08d82Slm66018 
14221ae08745Sheppo 	if (status != 0) {
14233af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to init DRing (status = %d)\n",
14241ae08745Sheppo 				vdc->instance, status);
14251ae08745Sheppo 		return (status);
14261ae08745Sheppo 	}
14273af08d82Slm66018 
14283af08d82Slm66018 	DMSG(vdc, 0, "[%d] Init of descriptor ring completed (status = %d)\n",
1429e1ebb9ecSlm66018 			vdc->instance, status);
14301ae08745Sheppo 
14311ae08745Sheppo 	/* fill in tag */
14321ae08745Sheppo 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
14331ae08745Sheppo 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
14341ae08745Sheppo 	pkt.tag.vio_subtype_env = VIO_DRING_REG;
14351ae08745Sheppo 	pkt.tag.vio_sid = vdc->session_id;
14361ae08745Sheppo 	/* fill in payload */
14371ae08745Sheppo 	pkt.dring_ident = 0;
1438e1ebb9ecSlm66018 	pkt.num_descriptors = vdc->dring_len;
1439e1ebb9ecSlm66018 	pkt.descriptor_size = vdc->dring_entry_size;
14401ae08745Sheppo 	pkt.options = (VIO_TX_DRING | VIO_RX_DRING);
14411ae08745Sheppo 	pkt.ncookies = vdc->dring_cookie_count;
14421ae08745Sheppo 	pkt.cookie[0] = vdc->dring_cookie[0];	/* for now just one cookie */
14431ae08745Sheppo 
14440a55fbb7Slm66018 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
14451ae08745Sheppo 	if (status != 0) {
14463af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to register DRing (err = %d)",
1447e1ebb9ecSlm66018 				vdc->instance, status);
14481ae08745Sheppo 	}
14491ae08745Sheppo 
14501ae08745Sheppo 	return (status);
14511ae08745Sheppo }
14521ae08745Sheppo 
14531ae08745Sheppo 
14543af08d82Slm66018 /*
14553af08d82Slm66018  * Function:
14563af08d82Slm66018  *	vdc_dring_negotiation()
14573af08d82Slm66018  *
14583af08d82Slm66018  * Description:
14593af08d82Slm66018  *
14603af08d82Slm66018  * Arguments:
14613af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
14623af08d82Slm66018  *
14633af08d82Slm66018  * Return Code:
14643af08d82Slm66018  *	0	- Success
14653af08d82Slm66018  */
14663af08d82Slm66018 static int
14673af08d82Slm66018 vdc_dring_negotiation(vdc_t *vdcp)
14683af08d82Slm66018 {
14693af08d82Slm66018 	int status;
14703af08d82Slm66018 	vio_msg_t vio_msg;
14713af08d82Slm66018 
14723af08d82Slm66018 	if (status = vdc_init_dring_negotiate(vdcp))
14733af08d82Slm66018 		return (status);
14743af08d82Slm66018 
14753af08d82Slm66018 	/* release lock and wait for response */
14763af08d82Slm66018 	mutex_exit(&vdcp->lock);
14773af08d82Slm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
14783af08d82Slm66018 	mutex_enter(&vdcp->lock);
14793af08d82Slm66018 	if (status) {
14803af08d82Slm66018 		DMSG(vdcp, 0,
14813af08d82Slm66018 		    "[%d] Failed waiting for Dring negotiation response,"
14823af08d82Slm66018 		    " rv(%d)", vdcp->instance, status);
14833af08d82Slm66018 		return (status);
14843af08d82Slm66018 	}
14853af08d82Slm66018 
14863af08d82Slm66018 	/* check type and sub_type ... */
14873af08d82Slm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
14883af08d82Slm66018 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
14893af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Invalid Dring negotiation response\n",
14903af08d82Slm66018 				vdcp->instance);
14913af08d82Slm66018 		return (EPROTO);
14923af08d82Slm66018 	}
14933af08d82Slm66018 
14943af08d82Slm66018 	return (vdc_handle_dring_reg_msg(vdcp,
14953af08d82Slm66018 		    (vio_dring_reg_msg_t *)&vio_msg));
14963af08d82Slm66018 }
14973af08d82Slm66018 
14983af08d82Slm66018 
14993af08d82Slm66018 /*
15003af08d82Slm66018  * Function:
15013af08d82Slm66018  *	vdc_send_rdx()
15023af08d82Slm66018  *
15033af08d82Slm66018  * Description:
15043af08d82Slm66018  *
15053af08d82Slm66018  * Arguments:
15063af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
15073af08d82Slm66018  *
15083af08d82Slm66018  * Return Code:
15093af08d82Slm66018  *	0	- Success
15103af08d82Slm66018  */
15113af08d82Slm66018 static int
15123af08d82Slm66018 vdc_send_rdx(vdc_t *vdcp)
15133af08d82Slm66018 {
15143af08d82Slm66018 	vio_msg_t	msg;
15153af08d82Slm66018 	size_t		msglen = sizeof (vio_msg_t);
15163af08d82Slm66018 	int		status;
15173af08d82Slm66018 
15183af08d82Slm66018 	/*
15193af08d82Slm66018 	 * Send an RDX message to vds to indicate we are ready
15203af08d82Slm66018 	 * to send data
15213af08d82Slm66018 	 */
15223af08d82Slm66018 	msg.tag.vio_msgtype = VIO_TYPE_CTRL;
15233af08d82Slm66018 	msg.tag.vio_subtype = VIO_SUBTYPE_INFO;
15243af08d82Slm66018 	msg.tag.vio_subtype_env = VIO_RDX;
15253af08d82Slm66018 	msg.tag.vio_sid = vdcp->session_id;
15263af08d82Slm66018 	status = vdc_send(vdcp, (caddr_t)&msg, &msglen);
15273af08d82Slm66018 	if (status != 0) {
15283af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Failed to send RDX message (%d)",
15293af08d82Slm66018 		    vdcp->instance, status);
15303af08d82Slm66018 	}
15313af08d82Slm66018 
15323af08d82Slm66018 	return (status);
15333af08d82Slm66018 }
15343af08d82Slm66018 
15353af08d82Slm66018 /*
15363af08d82Slm66018  * Function:
15373af08d82Slm66018  *	vdc_handle_rdx()
15383af08d82Slm66018  *
15393af08d82Slm66018  * Description:
15403af08d82Slm66018  *
15413af08d82Slm66018  * Arguments:
15423af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
15433af08d82Slm66018  *	msgp	- received msg
15443af08d82Slm66018  *
15453af08d82Slm66018  * Return Code:
15463af08d82Slm66018  *	0	- Success
15473af08d82Slm66018  */
15483af08d82Slm66018 static int
15493af08d82Slm66018 vdc_handle_rdx(vdc_t *vdcp, vio_rdx_msg_t *msgp)
15503af08d82Slm66018 {
15513af08d82Slm66018 	_NOTE(ARGUNUSED(vdcp))
15523af08d82Slm66018 	_NOTE(ARGUNUSED(msgp))
15533af08d82Slm66018 
15543af08d82Slm66018 	ASSERT(msgp->tag.vio_msgtype == VIO_TYPE_CTRL);
15553af08d82Slm66018 	ASSERT(msgp->tag.vio_subtype == VIO_SUBTYPE_ACK);
15563af08d82Slm66018 	ASSERT(msgp->tag.vio_subtype_env == VIO_RDX);
15573af08d82Slm66018 
15583af08d82Slm66018 	DMSG(vdcp, 1, "[%d] Got an RDX msg", vdcp->instance);
15593af08d82Slm66018 
15603af08d82Slm66018 	return (0);
15613af08d82Slm66018 }
15623af08d82Slm66018 
15633af08d82Slm66018 /*
15643af08d82Slm66018  * Function:
15653af08d82Slm66018  *	vdc_rdx_exchange()
15663af08d82Slm66018  *
15673af08d82Slm66018  * Description:
15683af08d82Slm66018  *
15693af08d82Slm66018  * Arguments:
15703af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
15713af08d82Slm66018  *
15723af08d82Slm66018  * Return Code:
15733af08d82Slm66018  *	0	- Success
15743af08d82Slm66018  */
15753af08d82Slm66018 static int
15763af08d82Slm66018 vdc_rdx_exchange(vdc_t *vdcp)
15773af08d82Slm66018 {
15783af08d82Slm66018 	int status;
15793af08d82Slm66018 	vio_msg_t vio_msg;
15803af08d82Slm66018 
15813af08d82Slm66018 	if (status = vdc_send_rdx(vdcp))
15823af08d82Slm66018 		return (status);
15833af08d82Slm66018 
15843af08d82Slm66018 	/* release lock and wait for response */
15853af08d82Slm66018 	mutex_exit(&vdcp->lock);
15863af08d82Slm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
15873af08d82Slm66018 	mutex_enter(&vdcp->lock);
15883af08d82Slm66018 	if (status) {
15893af08d82Slm66018 		DMSG(vdcp, 0,
15903af08d82Slm66018 		    "[%d] Failed waiting for RDX response,"
15913af08d82Slm66018 		    " rv(%d)", vdcp->instance, status);
15923af08d82Slm66018 		return (status);
15933af08d82Slm66018 	}
15943af08d82Slm66018 
15953af08d82Slm66018 	/* check type and sub_type ... */
15963af08d82Slm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
15973af08d82Slm66018 	    vio_msg.tag.vio_subtype != VIO_SUBTYPE_ACK) {
15983af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Invalid RDX response\n",
15993af08d82Slm66018 				vdcp->instance);
16003af08d82Slm66018 		return (EPROTO);
16013af08d82Slm66018 	}
16023af08d82Slm66018 
16033af08d82Slm66018 	return (vdc_handle_rdx(vdcp, (vio_rdx_msg_t *)&vio_msg));
16043af08d82Slm66018 }
16053af08d82Slm66018 
16063af08d82Slm66018 
16071ae08745Sheppo /* -------------------------------------------------------------------------- */
16081ae08745Sheppo 
16091ae08745Sheppo /*
16101ae08745Sheppo  * LDC helper routines
16111ae08745Sheppo  */
16121ae08745Sheppo 
16133af08d82Slm66018 static int
16143af08d82Slm66018 vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp)
16153af08d82Slm66018 {
16163af08d82Slm66018 	int		status;
16173af08d82Slm66018 	boolean_t	q_has_pkts = B_FALSE;
16183af08d82Slm66018 	int		delay_time;
16193af08d82Slm66018 	size_t		len;
16203af08d82Slm66018 
16213af08d82Slm66018 	mutex_enter(&vdc->read_lock);
16223af08d82Slm66018 
16233af08d82Slm66018 	if (vdc->read_state == VDC_READ_IDLE)
16243af08d82Slm66018 		vdc->read_state = VDC_READ_WAITING;
16253af08d82Slm66018 
16263af08d82Slm66018 	while (vdc->read_state != VDC_READ_PENDING) {
16273af08d82Slm66018 
16283af08d82Slm66018 		/* detect if the connection has been reset */
16293af08d82Slm66018 		if (vdc->read_state == VDC_READ_RESET) {
16303af08d82Slm66018 			status = ECONNRESET;
16313af08d82Slm66018 			goto done;
16323af08d82Slm66018 		}
16333af08d82Slm66018 
16343af08d82Slm66018 		cv_wait(&vdc->read_cv, &vdc->read_lock);
16353af08d82Slm66018 	}
16363af08d82Slm66018 
16373af08d82Slm66018 	/*
16383af08d82Slm66018 	 * Until we get a blocking ldc read we have to retry
16393af08d82Slm66018 	 * until the entire LDC message has arrived before
16403af08d82Slm66018 	 * ldc_read() will succeed. Note we also bail out if
16413af08d82Slm66018 	 * the chanel is reset or goes away.
16423af08d82Slm66018 	 */
16433af08d82Slm66018 	delay_time = vdc_ldc_read_init_delay;
16443af08d82Slm66018 loop:
16453af08d82Slm66018 	len = *nbytesp;
16463af08d82Slm66018 	status = ldc_read(vdc->ldc_handle, (caddr_t)msgp, &len);
16473af08d82Slm66018 	switch (status) {
16483af08d82Slm66018 	case EAGAIN:
16493af08d82Slm66018 		delay_time *= 2;
16503af08d82Slm66018 		if (delay_time >= vdc_ldc_read_max_delay)
16513af08d82Slm66018 			delay_time = vdc_ldc_read_max_delay;
16523af08d82Slm66018 		delay(delay_time);
16533af08d82Slm66018 		goto loop;
16543af08d82Slm66018 
16553af08d82Slm66018 	case 0:
16563af08d82Slm66018 		if (len == 0) {
16573af08d82Slm66018 			DMSG(vdc, 0, "[%d] ldc_read returned 0 bytes with "
16583af08d82Slm66018 				"no error!\n", vdc->instance);
16593af08d82Slm66018 			goto loop;
16603af08d82Slm66018 		}
16613af08d82Slm66018 
16623af08d82Slm66018 		*nbytesp = len;
16633af08d82Slm66018 
16643af08d82Slm66018 		/*
16653af08d82Slm66018 		 * If there are pending messages, leave the
16663af08d82Slm66018 		 * read state as pending. Otherwise, set the state
16673af08d82Slm66018 		 * back to idle.
16683af08d82Slm66018 		 */
16693af08d82Slm66018 		status = ldc_chkq(vdc->ldc_handle, &q_has_pkts);
16703af08d82Slm66018 		if (status == 0 && !q_has_pkts)
16713af08d82Slm66018 			vdc->read_state = VDC_READ_IDLE;
16723af08d82Slm66018 
16733af08d82Slm66018 		break;
16743af08d82Slm66018 	default:
16753af08d82Slm66018 		DMSG(vdc, 0, "ldc_read returned %d\n", status);
16763af08d82Slm66018 		break;
16773af08d82Slm66018 	}
16783af08d82Slm66018 
16793af08d82Slm66018 done:
16803af08d82Slm66018 	mutex_exit(&vdc->read_lock);
16813af08d82Slm66018 
16823af08d82Slm66018 	return (status);
16833af08d82Slm66018 }
16843af08d82Slm66018 
16853af08d82Slm66018 
16863af08d82Slm66018 
16873af08d82Slm66018 #ifdef DEBUG
16883af08d82Slm66018 void
16893af08d82Slm66018 vdc_decode_tag(vdc_t *vdcp, vio_msg_t *msg)
16903af08d82Slm66018 {
16913af08d82Slm66018 	char *ms, *ss, *ses;
16923af08d82Slm66018 	switch (msg->tag.vio_msgtype) {
16933af08d82Slm66018 #define	Q(_s)	case _s : ms = #_s; break;
16943af08d82Slm66018 	Q(VIO_TYPE_CTRL)
16953af08d82Slm66018 	Q(VIO_TYPE_DATA)
16963af08d82Slm66018 	Q(VIO_TYPE_ERR)
16973af08d82Slm66018 #undef Q
16983af08d82Slm66018 	default: ms = "unknown"; break;
16993af08d82Slm66018 	}
17003af08d82Slm66018 
17013af08d82Slm66018 	switch (msg->tag.vio_subtype) {
17023af08d82Slm66018 #define	Q(_s)	case _s : ss = #_s; break;
17033af08d82Slm66018 	Q(VIO_SUBTYPE_INFO)
17043af08d82Slm66018 	Q(VIO_SUBTYPE_ACK)
17053af08d82Slm66018 	Q(VIO_SUBTYPE_NACK)
17063af08d82Slm66018 #undef Q
17073af08d82Slm66018 	default: ss = "unknown"; break;
17083af08d82Slm66018 	}
17093af08d82Slm66018 
17103af08d82Slm66018 	switch (msg->tag.vio_subtype_env) {
17113af08d82Slm66018 #define	Q(_s)	case _s : ses = #_s; break;
17123af08d82Slm66018 	Q(VIO_VER_INFO)
17133af08d82Slm66018 	Q(VIO_ATTR_INFO)
17143af08d82Slm66018 	Q(VIO_DRING_REG)
17153af08d82Slm66018 	Q(VIO_DRING_UNREG)
17163af08d82Slm66018 	Q(VIO_RDX)
17173af08d82Slm66018 	Q(VIO_PKT_DATA)
17183af08d82Slm66018 	Q(VIO_DESC_DATA)
17193af08d82Slm66018 	Q(VIO_DRING_DATA)
17203af08d82Slm66018 #undef Q
17213af08d82Slm66018 	default: ses = "unknown"; break;
17223af08d82Slm66018 	}
17233af08d82Slm66018 
17243af08d82Slm66018 	DMSG(vdcp, 3, "(%x/%x/%x) message : (%s/%s/%s)\n",
17253af08d82Slm66018 	    msg->tag.vio_msgtype, msg->tag.vio_subtype,
17263af08d82Slm66018 	    msg->tag.vio_subtype_env, ms, ss, ses);
17273af08d82Slm66018 }
17283af08d82Slm66018 #endif
17293af08d82Slm66018 
17301ae08745Sheppo /*
17311ae08745Sheppo  * Function:
17321ae08745Sheppo  *	vdc_send()
17331ae08745Sheppo  *
17341ae08745Sheppo  * Description:
17351ae08745Sheppo  *	The function encapsulates the call to write a message using LDC.
17361ae08745Sheppo  *	If LDC indicates that the call failed due to the queue being full,
17371ae08745Sheppo  *	we retry the ldc_write() [ up to 'vdc_retries' time ], otherwise
17381ae08745Sheppo  *	we return the error returned by LDC.
17391ae08745Sheppo  *
17401ae08745Sheppo  * Arguments:
17411ae08745Sheppo  *	ldc_handle	- LDC handle for the channel this instance of vdc uses
17421ae08745Sheppo  *	pkt		- address of LDC message to be sent
17431ae08745Sheppo  *	msglen		- the size of the message being sent. When the function
17441ae08745Sheppo  *			  returns, this contains the number of bytes written.
17451ae08745Sheppo  *
17461ae08745Sheppo  * Return Code:
17471ae08745Sheppo  *	0		- Success.
17481ae08745Sheppo  *	EINVAL		- pkt or msglen were NULL
17491ae08745Sheppo  *	ECONNRESET	- The connection was not up.
17501ae08745Sheppo  *	EWOULDBLOCK	- LDC queue is full
17511ae08745Sheppo  *	xxx		- other error codes returned by ldc_write
17521ae08745Sheppo  */
17531ae08745Sheppo static int
17540a55fbb7Slm66018 vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen)
17551ae08745Sheppo {
17561ae08745Sheppo 	size_t	size = 0;
17571ae08745Sheppo 	int	status = 0;
17583af08d82Slm66018 	clock_t delay_ticks;
17591ae08745Sheppo 
17600a55fbb7Slm66018 	ASSERT(vdc != NULL);
17610a55fbb7Slm66018 	ASSERT(mutex_owned(&vdc->lock));
17621ae08745Sheppo 	ASSERT(msglen != NULL);
17631ae08745Sheppo 	ASSERT(*msglen != 0);
17641ae08745Sheppo 
17653af08d82Slm66018 #ifdef DEBUG
17663af08d82Slm66018 	vdc_decode_tag(vdc, (vio_msg_t *)pkt);
17673af08d82Slm66018 #endif
17683af08d82Slm66018 	/*
17693af08d82Slm66018 	 * Wait indefinitely to send if channel
17703af08d82Slm66018 	 * is busy, but bail out if we succeed or
17713af08d82Slm66018 	 * if the channel closes or is reset.
17723af08d82Slm66018 	 */
17733af08d82Slm66018 	delay_ticks = vdc_hz_min_ldc_delay;
17741ae08745Sheppo 	do {
17751ae08745Sheppo 		size = *msglen;
17760a55fbb7Slm66018 		status = ldc_write(vdc->ldc_handle, pkt, &size);
17773af08d82Slm66018 		if (status == EWOULDBLOCK) {
17783af08d82Slm66018 			delay(delay_ticks);
17793af08d82Slm66018 			/* geometric backoff */
17803af08d82Slm66018 			delay_ticks *= 2;
17813af08d82Slm66018 			if (delay_ticks > vdc_hz_max_ldc_delay)
17823af08d82Slm66018 				delay_ticks = vdc_hz_max_ldc_delay;
17833af08d82Slm66018 		}
17843af08d82Slm66018 	} while (status == EWOULDBLOCK);
17851ae08745Sheppo 
17860a55fbb7Slm66018 	/* if LDC had serious issues --- reset vdc state */
17870a55fbb7Slm66018 	if (status == EIO || status == ECONNRESET) {
17883af08d82Slm66018 		/* LDC had serious issues --- reset vdc state */
17893af08d82Slm66018 		mutex_enter(&vdc->read_lock);
17903af08d82Slm66018 		if ((vdc->read_state == VDC_READ_WAITING) ||
17913af08d82Slm66018 		    (vdc->read_state == VDC_READ_RESET))
17923af08d82Slm66018 			cv_signal(&vdc->read_cv);
17933af08d82Slm66018 		vdc->read_state = VDC_READ_RESET;
17943af08d82Slm66018 		mutex_exit(&vdc->read_lock);
17953af08d82Slm66018 
17963af08d82Slm66018 		/* wake up any waiters in the reset thread */
17973af08d82Slm66018 		if (vdc->state == VDC_STATE_INIT_WAITING) {
17983af08d82Slm66018 			DMSG(vdc, 0, "[%d] write reset - "
17993af08d82Slm66018 			    "vdc is resetting ..\n", vdc->instance);
18003af08d82Slm66018 			vdc->state = VDC_STATE_RESETTING;
18013af08d82Slm66018 			cv_signal(&vdc->initwait_cv);
18023af08d82Slm66018 		}
18033af08d82Slm66018 
18043af08d82Slm66018 		return (ECONNRESET);
18050a55fbb7Slm66018 	}
18060a55fbb7Slm66018 
18071ae08745Sheppo 	/* return the last size written */
18081ae08745Sheppo 	*msglen = size;
18091ae08745Sheppo 
18101ae08745Sheppo 	return (status);
18111ae08745Sheppo }
18121ae08745Sheppo 
18131ae08745Sheppo /*
18141ae08745Sheppo  * Function:
18151ae08745Sheppo  *	vdc_get_ldc_id()
18161ae08745Sheppo  *
18171ae08745Sheppo  * Description:
18181ae08745Sheppo  *	This function gets the 'ldc-id' for this particular instance of vdc.
18191ae08745Sheppo  *	The id returned is the guest domain channel endpoint LDC uses for
18201ae08745Sheppo  *	communication with vds.
18211ae08745Sheppo  *
18221ae08745Sheppo  * Arguments:
18231ae08745Sheppo  *	dip	- dev info pointer for this instance of the device driver.
18241ae08745Sheppo  *	ldc_id	- pointer to variable used to return the 'ldc-id' found.
18251ae08745Sheppo  *
18261ae08745Sheppo  * Return Code:
18271ae08745Sheppo  *	0	- Success.
18281ae08745Sheppo  *	ENOENT	- Expected node or property did not exist.
18291ae08745Sheppo  *	ENXIO	- Unexpected error communicating with MD framework
18301ae08745Sheppo  */
18311ae08745Sheppo static int
18321ae08745Sheppo vdc_get_ldc_id(dev_info_t *dip, uint64_t *ldc_id)
18331ae08745Sheppo {
18341ae08745Sheppo 	int		status = ENOENT;
18351ae08745Sheppo 	char		*node_name = NULL;
18361ae08745Sheppo 	md_t		*mdp = NULL;
18371ae08745Sheppo 	int		num_nodes;
18381ae08745Sheppo 	int		num_vdevs;
18391ae08745Sheppo 	int		num_chans;
18401ae08745Sheppo 	mde_cookie_t	rootnode;
18411ae08745Sheppo 	mde_cookie_t	*listp = NULL;
18421ae08745Sheppo 	mde_cookie_t	*chanp = NULL;
18431ae08745Sheppo 	boolean_t	found_inst = B_FALSE;
18441ae08745Sheppo 	int		listsz;
18451ae08745Sheppo 	int		idx;
18461ae08745Sheppo 	uint64_t	md_inst;
18471ae08745Sheppo 	int		obp_inst;
18481ae08745Sheppo 	int		instance = ddi_get_instance(dip);
18491ae08745Sheppo 
18501ae08745Sheppo 	ASSERT(ldc_id != NULL);
18511ae08745Sheppo 	*ldc_id = 0;
18521ae08745Sheppo 
18531ae08745Sheppo 	/*
18541ae08745Sheppo 	 * Get the OBP instance number for comparison with the MD instance
18551ae08745Sheppo 	 *
18561ae08745Sheppo 	 * The "cfg-handle" property of a vdc node in an MD contains the MD's
18571ae08745Sheppo 	 * notion of "instance", or unique identifier, for that node; OBP
18581ae08745Sheppo 	 * stores the value of the "cfg-handle" MD property as the value of
18591ae08745Sheppo 	 * the "reg" property on the node in the device tree it builds from
18601ae08745Sheppo 	 * the MD and passes to Solaris.  Thus, we look up the devinfo node's
18611ae08745Sheppo 	 * "reg" property value to uniquely identify this device instance.
18621ae08745Sheppo 	 * If the "reg" property cannot be found, the device tree state is
18631ae08745Sheppo 	 * presumably so broken that there is no point in continuing.
18641ae08745Sheppo 	 */
18651ae08745Sheppo 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, OBP_REG)) {
18661ae08745Sheppo 		cmn_err(CE_WARN, "'%s' property does not exist", OBP_REG);
18671ae08745Sheppo 		return (ENOENT);
18681ae08745Sheppo 	}
18691ae08745Sheppo 	obp_inst = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
18701ae08745Sheppo 			OBP_REG, -1);
18713af08d82Slm66018 	DMSGX(1, "[%d] OBP inst=%d\n", instance, obp_inst);
18721ae08745Sheppo 
18731ae08745Sheppo 	/*
18741ae08745Sheppo 	 * We now walk the MD nodes and if an instance of a vdc node matches
18751ae08745Sheppo 	 * the instance got from OBP we get the ldc-id property.
18761ae08745Sheppo 	 */
18771ae08745Sheppo 	if ((mdp = md_get_handle()) == NULL) {
18781ae08745Sheppo 		cmn_err(CE_WARN, "unable to init machine description");
18791ae08745Sheppo 		return (ENXIO);
18801ae08745Sheppo 	}
18811ae08745Sheppo 
18821ae08745Sheppo 	num_nodes = md_node_count(mdp);
18831ae08745Sheppo 	ASSERT(num_nodes > 0);
18841ae08745Sheppo 
18851ae08745Sheppo 	listsz = num_nodes * sizeof (mde_cookie_t);
18861ae08745Sheppo 
18871ae08745Sheppo 	/* allocate memory for nodes */
18881ae08745Sheppo 	listp = kmem_zalloc(listsz, KM_SLEEP);
18891ae08745Sheppo 	chanp = kmem_zalloc(listsz, KM_SLEEP);
18901ae08745Sheppo 
18911ae08745Sheppo 	rootnode = md_root_node(mdp);
18921ae08745Sheppo 	ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE);
18931ae08745Sheppo 
18941ae08745Sheppo 	/*
18951ae08745Sheppo 	 * Search for all the virtual devices, we will then check to see which
18961ae08745Sheppo 	 * ones are disk nodes.
18971ae08745Sheppo 	 */
18981ae08745Sheppo 	num_vdevs = md_scan_dag(mdp, rootnode,
18991ae08745Sheppo 			md_find_name(mdp, VDC_MD_VDEV_NAME),
19001ae08745Sheppo 			md_find_name(mdp, "fwd"), listp);
19011ae08745Sheppo 
19021ae08745Sheppo 	if (num_vdevs <= 0) {
19031ae08745Sheppo 		cmn_err(CE_NOTE, "No '%s' node found", VDC_MD_VDEV_NAME);
19041ae08745Sheppo 		status = ENOENT;
19051ae08745Sheppo 		goto done;
19061ae08745Sheppo 	}
19071ae08745Sheppo 
19083af08d82Slm66018 	DMSGX(1, "[%d] num_vdevs=%d\n", instance, num_vdevs);
19091ae08745Sheppo 	for (idx = 0; idx < num_vdevs; idx++) {
19101ae08745Sheppo 		status = md_get_prop_str(mdp, listp[idx], "name", &node_name);
19111ae08745Sheppo 		if ((status != 0) || (node_name == NULL)) {
19121ae08745Sheppo 			cmn_err(CE_NOTE, "Unable to get name of node type '%s'"
19131ae08745Sheppo 					": err %d", VDC_MD_VDEV_NAME, status);
19141ae08745Sheppo 			continue;
19151ae08745Sheppo 		}
19161ae08745Sheppo 
19173af08d82Slm66018 		DMSGX(1, "[%d] Found node '%s'\n", instance, node_name);
19181ae08745Sheppo 		if (strcmp(VDC_MD_DISK_NAME, node_name) == 0) {
19191ae08745Sheppo 			status = md_get_prop_val(mdp, listp[idx],
19201ae08745Sheppo 					VDC_MD_CFG_HDL, &md_inst);
19213af08d82Slm66018 			DMSGX(1, "[%d] vdc inst in MD=%lx\n",
19223af08d82Slm66018 			    instance, md_inst);
19231ae08745Sheppo 			if ((status == 0) && (md_inst == obp_inst)) {
19241ae08745Sheppo 				found_inst = B_TRUE;
19251ae08745Sheppo 				break;
19261ae08745Sheppo 			}
19271ae08745Sheppo 		}
19281ae08745Sheppo 	}
19291ae08745Sheppo 
19300a55fbb7Slm66018 	if (!found_inst) {
19313af08d82Slm66018 		DMSGX(0, "Unable to find correct '%s' node", VDC_MD_DISK_NAME);
19321ae08745Sheppo 		status = ENOENT;
19331ae08745Sheppo 		goto done;
19341ae08745Sheppo 	}
19353af08d82Slm66018 	DMSGX(0, "[%d] MD inst=%lx\n", instance, md_inst);
19361ae08745Sheppo 
19371ae08745Sheppo 	/* get the channels for this node */
19381ae08745Sheppo 	num_chans = md_scan_dag(mdp, listp[idx],
19391ae08745Sheppo 			md_find_name(mdp, VDC_MD_CHAN_NAME),
19401ae08745Sheppo 			md_find_name(mdp, "fwd"), chanp);
19411ae08745Sheppo 
19421ae08745Sheppo 	/* expecting at least one channel */
19431ae08745Sheppo 	if (num_chans <= 0) {
19441ae08745Sheppo 		cmn_err(CE_NOTE, "No '%s' node for '%s' port",
19451ae08745Sheppo 				VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME);
19461ae08745Sheppo 		status = ENOENT;
19471ae08745Sheppo 		goto done;
19481ae08745Sheppo 
19491ae08745Sheppo 	} else if (num_chans != 1) {
19503af08d82Slm66018 		DMSGX(0, "[%d] Expected 1 '%s' node for '%s' port, found %d\n",
1951e1ebb9ecSlm66018 			instance, VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME,
19521ae08745Sheppo 			num_chans);
19531ae08745Sheppo 	}
19541ae08745Sheppo 
19551ae08745Sheppo 	/*
19561ae08745Sheppo 	 * We use the first channel found (index 0), irrespective of how
19571ae08745Sheppo 	 * many are there in total.
19581ae08745Sheppo 	 */
19591ae08745Sheppo 	if (md_get_prop_val(mdp, chanp[0], VDC_ID_PROP, ldc_id) != 0) {
19601ae08745Sheppo 		cmn_err(CE_NOTE, "Channel '%s' property not found",
19611ae08745Sheppo 				VDC_ID_PROP);
19621ae08745Sheppo 		status = ENOENT;
19631ae08745Sheppo 	}
19641ae08745Sheppo 
19653af08d82Slm66018 	DMSGX(0, "[%d] LDC id is 0x%lx\n", instance, *ldc_id);
19661ae08745Sheppo 
19671ae08745Sheppo done:
19681ae08745Sheppo 	if (chanp)
19691ae08745Sheppo 		kmem_free(chanp, listsz);
19701ae08745Sheppo 	if (listp)
19711ae08745Sheppo 		kmem_free(listp, listsz);
19721ae08745Sheppo 
19731ae08745Sheppo 	(void) md_fini_handle(mdp);
19741ae08745Sheppo 
19751ae08745Sheppo 	return (status);
19761ae08745Sheppo }
19771ae08745Sheppo 
19780a55fbb7Slm66018 static int
19790a55fbb7Slm66018 vdc_do_ldc_up(vdc_t *vdc)
19800a55fbb7Slm66018 {
19810a55fbb7Slm66018 	int		status;
19823af08d82Slm66018 	ldc_status_t	ldc_state;
19830a55fbb7Slm66018 
19843af08d82Slm66018 	DMSG(vdc, 0, "[%d] Bringing up channel %lx\n",
19853af08d82Slm66018 	    vdc->instance, vdc->ldc_id);
19863af08d82Slm66018 
19873af08d82Slm66018 	if (vdc->lifecycle == VDC_LC_DETACHING)
19883af08d82Slm66018 		return (EINVAL);
19890a55fbb7Slm66018 
19900a55fbb7Slm66018 	if ((status = ldc_up(vdc->ldc_handle)) != 0) {
19910a55fbb7Slm66018 		switch (status) {
19920a55fbb7Slm66018 		case ECONNREFUSED:	/* listener not ready at other end */
19933af08d82Slm66018 			DMSG(vdc, 0, "[%d] ldc_up(%lx,...) return %d\n",
1994e1ebb9ecSlm66018 					vdc->instance, vdc->ldc_id, status);
19950a55fbb7Slm66018 			status = 0;
19960a55fbb7Slm66018 			break;
19970a55fbb7Slm66018 		default:
19983af08d82Slm66018 			DMSG(vdc, 0, "[%d] Failed to bring up LDC: "
19993af08d82Slm66018 			    "channel=%ld, err=%d", vdc->instance, vdc->ldc_id,
20003af08d82Slm66018 			    status);
20013af08d82Slm66018 			break;
20023af08d82Slm66018 		}
20033af08d82Slm66018 	}
20043af08d82Slm66018 
20053af08d82Slm66018 	if (ldc_status(vdc->ldc_handle, &ldc_state) == 0) {
20063af08d82Slm66018 		vdc->ldc_state = ldc_state;
20073af08d82Slm66018 		if (ldc_state == LDC_UP) {
20083af08d82Slm66018 			DMSG(vdc, 0, "[%d] LDC channel already up\n",
20093af08d82Slm66018 			    vdc->instance);
20103af08d82Slm66018 			vdc->seq_num = 1;
20113af08d82Slm66018 			vdc->seq_num_reply = 0;
20120a55fbb7Slm66018 		}
20130a55fbb7Slm66018 	}
20140a55fbb7Slm66018 
20150a55fbb7Slm66018 	return (status);
20160a55fbb7Slm66018 }
20170a55fbb7Slm66018 
20180a55fbb7Slm66018 /*
20190a55fbb7Slm66018  * Function:
20200a55fbb7Slm66018  *	vdc_terminate_ldc()
20210a55fbb7Slm66018  *
20220a55fbb7Slm66018  * Description:
20230a55fbb7Slm66018  *
20240a55fbb7Slm66018  * Arguments:
20250a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
20260a55fbb7Slm66018  *
20270a55fbb7Slm66018  * Return Code:
20280a55fbb7Slm66018  *	None
20290a55fbb7Slm66018  */
20301ae08745Sheppo static void
20311ae08745Sheppo vdc_terminate_ldc(vdc_t *vdc)
20321ae08745Sheppo {
20331ae08745Sheppo 	int	instance = ddi_get_instance(vdc->dip);
20341ae08745Sheppo 
20351ae08745Sheppo 	ASSERT(vdc != NULL);
20361ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
20371ae08745Sheppo 
20383af08d82Slm66018 	DMSG(vdc, 0, "[%d] initialized=%x\n", instance, vdc->initialized);
20391ae08745Sheppo 
20401ae08745Sheppo 	if (vdc->initialized & VDC_LDC_OPEN) {
20413af08d82Slm66018 		DMSG(vdc, 0, "[%d] ldc_close()\n", instance);
20421ae08745Sheppo 		(void) ldc_close(vdc->ldc_handle);
20431ae08745Sheppo 	}
20441ae08745Sheppo 	if (vdc->initialized & VDC_LDC_CB) {
20453af08d82Slm66018 		DMSG(vdc, 0, "[%d] ldc_unreg_callback()\n", instance);
20461ae08745Sheppo 		(void) ldc_unreg_callback(vdc->ldc_handle);
20471ae08745Sheppo 	}
20481ae08745Sheppo 	if (vdc->initialized & VDC_LDC) {
20493af08d82Slm66018 		DMSG(vdc, 0, "[%d] ldc_fini()\n", instance);
20501ae08745Sheppo 		(void) ldc_fini(vdc->ldc_handle);
20511ae08745Sheppo 		vdc->ldc_handle = NULL;
20521ae08745Sheppo 	}
20531ae08745Sheppo 
20541ae08745Sheppo 	vdc->initialized &= ~(VDC_LDC | VDC_LDC_CB | VDC_LDC_OPEN);
20551ae08745Sheppo }
20561ae08745Sheppo 
20571ae08745Sheppo /* -------------------------------------------------------------------------- */
20581ae08745Sheppo 
20591ae08745Sheppo /*
20601ae08745Sheppo  * Descriptor Ring helper routines
20611ae08745Sheppo  */
20621ae08745Sheppo 
20630a55fbb7Slm66018 /*
20640a55fbb7Slm66018  * Function:
20650a55fbb7Slm66018  *	vdc_init_descriptor_ring()
20660a55fbb7Slm66018  *
20670a55fbb7Slm66018  * Description:
20680a55fbb7Slm66018  *
20690a55fbb7Slm66018  * Arguments:
20700a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
20710a55fbb7Slm66018  *
20720a55fbb7Slm66018  * Return Code:
20730a55fbb7Slm66018  *	0	- Success
20740a55fbb7Slm66018  */
20751ae08745Sheppo static int
20761ae08745Sheppo vdc_init_descriptor_ring(vdc_t *vdc)
20771ae08745Sheppo {
20781ae08745Sheppo 	vd_dring_entry_t	*dep = NULL;	/* DRing Entry pointer */
20790a55fbb7Slm66018 	int	status = 0;
20801ae08745Sheppo 	int	i;
20811ae08745Sheppo 
20823af08d82Slm66018 	DMSG(vdc, 0, "[%d] initialized=%x\n", vdc->instance, vdc->initialized);
20831ae08745Sheppo 
20841ae08745Sheppo 	ASSERT(vdc != NULL);
20851ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
20861ae08745Sheppo 	ASSERT(vdc->ldc_handle != NULL);
20871ae08745Sheppo 
2088e1ebb9ecSlm66018 	/* ensure we have enough room to store max sized block */
2089e1ebb9ecSlm66018 	ASSERT(maxphys <= VD_MAX_BLOCK_SIZE);
2090e1ebb9ecSlm66018 
20910a55fbb7Slm66018 	if ((vdc->initialized & VDC_DRING_INIT) == 0) {
20923af08d82Slm66018 		DMSG(vdc, 0, "[%d] ldc_mem_dring_create\n", vdc->instance);
2093e1ebb9ecSlm66018 		/*
2094e1ebb9ecSlm66018 		 * Calculate the maximum block size we can transmit using one
2095e1ebb9ecSlm66018 		 * Descriptor Ring entry from the attributes returned by the
2096e1ebb9ecSlm66018 		 * vDisk server. This is subject to a minimum of 'maxphys'
2097e1ebb9ecSlm66018 		 * as we do not have the capability to split requests over
2098e1ebb9ecSlm66018 		 * multiple DRing entries.
2099e1ebb9ecSlm66018 		 */
2100e1ebb9ecSlm66018 		if ((vdc->max_xfer_sz * vdc->block_size) < maxphys) {
21013af08d82Slm66018 			DMSG(vdc, 0, "[%d] using minimum DRing size\n",
2102e1ebb9ecSlm66018 					vdc->instance);
2103e1ebb9ecSlm66018 			vdc->dring_max_cookies = maxphys / PAGESIZE;
2104e1ebb9ecSlm66018 		} else {
2105e1ebb9ecSlm66018 			vdc->dring_max_cookies =
2106e1ebb9ecSlm66018 				(vdc->max_xfer_sz * vdc->block_size) / PAGESIZE;
2107e1ebb9ecSlm66018 		}
2108e1ebb9ecSlm66018 		vdc->dring_entry_size = (sizeof (vd_dring_entry_t) +
2109e1ebb9ecSlm66018 				(sizeof (ldc_mem_cookie_t) *
2110e1ebb9ecSlm66018 					(vdc->dring_max_cookies - 1)));
2111e1ebb9ecSlm66018 		vdc->dring_len = VD_DRING_LEN;
2112e1ebb9ecSlm66018 
2113e1ebb9ecSlm66018 		status = ldc_mem_dring_create(vdc->dring_len,
2114e1ebb9ecSlm66018 				vdc->dring_entry_size, &vdc->ldc_dring_hdl);
21151ae08745Sheppo 		if ((vdc->ldc_dring_hdl == NULL) || (status != 0)) {
21163af08d82Slm66018 			DMSG(vdc, 0, "[%d] Descriptor ring creation failed",
2117e1ebb9ecSlm66018 					vdc->instance);
21181ae08745Sheppo 			return (status);
21191ae08745Sheppo 		}
21200a55fbb7Slm66018 		vdc->initialized |= VDC_DRING_INIT;
21210a55fbb7Slm66018 	}
21221ae08745Sheppo 
21230a55fbb7Slm66018 	if ((vdc->initialized & VDC_DRING_BOUND) == 0) {
21243af08d82Slm66018 		DMSG(vdc, 0, "[%d] ldc_mem_dring_bind\n", vdc->instance);
21250a55fbb7Slm66018 		vdc->dring_cookie =
21260a55fbb7Slm66018 			kmem_zalloc(sizeof (ldc_mem_cookie_t), KM_SLEEP);
21271ae08745Sheppo 
21281ae08745Sheppo 		status = ldc_mem_dring_bind(vdc->ldc_handle, vdc->ldc_dring_hdl,
21294bac2208Snarayan 				LDC_SHADOW_MAP|LDC_DIRECT_MAP, LDC_MEM_RW,
21300a55fbb7Slm66018 				&vdc->dring_cookie[0],
21311ae08745Sheppo 				&vdc->dring_cookie_count);
21321ae08745Sheppo 		if (status != 0) {
21333af08d82Slm66018 			DMSG(vdc, 0, "[%d] Failed to bind descriptor ring "
21343af08d82Slm66018 				"(%lx) to channel (%lx) status=%d\n",
21353af08d82Slm66018 				vdc->instance, vdc->ldc_dring_hdl,
21363af08d82Slm66018 				vdc->ldc_handle, status);
21371ae08745Sheppo 			return (status);
21381ae08745Sheppo 		}
21391ae08745Sheppo 		ASSERT(vdc->dring_cookie_count == 1);
21401ae08745Sheppo 		vdc->initialized |= VDC_DRING_BOUND;
21410a55fbb7Slm66018 	}
21421ae08745Sheppo 
21431ae08745Sheppo 	status = ldc_mem_dring_info(vdc->ldc_dring_hdl, &vdc->dring_mem_info);
21441ae08745Sheppo 	if (status != 0) {
21453af08d82Slm66018 		DMSG(vdc, 0,
21463af08d82Slm66018 		    "[%d] Failed to get info for descriptor ring (%lx)\n",
2147e1ebb9ecSlm66018 		    vdc->instance, vdc->ldc_dring_hdl);
21481ae08745Sheppo 		return (status);
21491ae08745Sheppo 	}
21501ae08745Sheppo 
21510a55fbb7Slm66018 	if ((vdc->initialized & VDC_DRING_LOCAL) == 0) {
21523af08d82Slm66018 		DMSG(vdc, 0, "[%d] local dring\n", vdc->instance);
21530a55fbb7Slm66018 
21541ae08745Sheppo 		/* Allocate the local copy of this dring */
21550a55fbb7Slm66018 		vdc->local_dring =
2156e1ebb9ecSlm66018 			kmem_zalloc(vdc->dring_len * sizeof (vdc_local_desc_t),
21571ae08745Sheppo 						KM_SLEEP);
21581ae08745Sheppo 		vdc->initialized |= VDC_DRING_LOCAL;
21590a55fbb7Slm66018 	}
21601ae08745Sheppo 
21611ae08745Sheppo 	/*
21620a55fbb7Slm66018 	 * Mark all DRing entries as free and initialize the private
21630a55fbb7Slm66018 	 * descriptor's memory handles. If any entry is initialized,
21640a55fbb7Slm66018 	 * we need to free it later so we set the bit in 'initialized'
21650a55fbb7Slm66018 	 * at the start.
21661ae08745Sheppo 	 */
21671ae08745Sheppo 	vdc->initialized |= VDC_DRING_ENTRY;
2168e1ebb9ecSlm66018 	for (i = 0; i < vdc->dring_len; i++) {
21691ae08745Sheppo 		dep = VDC_GET_DRING_ENTRY_PTR(vdc, i);
21701ae08745Sheppo 		dep->hdr.dstate = VIO_DESC_FREE;
21711ae08745Sheppo 
21721ae08745Sheppo 		status = ldc_mem_alloc_handle(vdc->ldc_handle,
21731ae08745Sheppo 				&vdc->local_dring[i].desc_mhdl);
21741ae08745Sheppo 		if (status != 0) {
21753af08d82Slm66018 			DMSG(vdc, 0, "![%d] Failed to alloc mem handle for"
21761ae08745Sheppo 					" descriptor %d", vdc->instance, i);
21771ae08745Sheppo 			return (status);
21781ae08745Sheppo 		}
21793af08d82Slm66018 		vdc->local_dring[i].is_free = B_TRUE;
21801ae08745Sheppo 		vdc->local_dring[i].dep = dep;
21811ae08745Sheppo 	}
21821ae08745Sheppo 
21833af08d82Slm66018 	/* Initialize the starting index */
21843af08d82Slm66018 	vdc->dring_curr_idx = 0;
21851ae08745Sheppo 
21861ae08745Sheppo 	return (status);
21871ae08745Sheppo }
21881ae08745Sheppo 
21890a55fbb7Slm66018 /*
21900a55fbb7Slm66018  * Function:
21910a55fbb7Slm66018  *	vdc_destroy_descriptor_ring()
21920a55fbb7Slm66018  *
21930a55fbb7Slm66018  * Description:
21940a55fbb7Slm66018  *
21950a55fbb7Slm66018  * Arguments:
21960a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
21970a55fbb7Slm66018  *
21980a55fbb7Slm66018  * Return Code:
21990a55fbb7Slm66018  *	None
22000a55fbb7Slm66018  */
22011ae08745Sheppo static void
22021ae08745Sheppo vdc_destroy_descriptor_ring(vdc_t *vdc)
22031ae08745Sheppo {
22040a55fbb7Slm66018 	vdc_local_desc_t	*ldep = NULL;	/* Local Dring Entry Pointer */
22051ae08745Sheppo 	ldc_mem_handle_t	mhdl = NULL;
22063af08d82Slm66018 	ldc_mem_info_t		minfo;
22071ae08745Sheppo 	int			status = -1;
22081ae08745Sheppo 	int			i;	/* loop */
22091ae08745Sheppo 
22101ae08745Sheppo 	ASSERT(vdc != NULL);
22111ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
22121ae08745Sheppo 
22133af08d82Slm66018 	DMSG(vdc, 0, "[%d] Entered\n", vdc->instance);
22141ae08745Sheppo 
22151ae08745Sheppo 	if (vdc->initialized & VDC_DRING_ENTRY) {
22163af08d82Slm66018 		DMSG(vdc, 0,
22173af08d82Slm66018 		    "[%d] Removing Local DRing entries\n", vdc->instance);
2218e1ebb9ecSlm66018 		for (i = 0; i < vdc->dring_len; i++) {
22190a55fbb7Slm66018 			ldep = &vdc->local_dring[i];
22200a55fbb7Slm66018 			mhdl = ldep->desc_mhdl;
22211ae08745Sheppo 
22220a55fbb7Slm66018 			if (mhdl == NULL)
22230a55fbb7Slm66018 				continue;
22240a55fbb7Slm66018 
22253af08d82Slm66018 			if ((status = ldc_mem_info(mhdl, &minfo)) != 0) {
22263af08d82Slm66018 				DMSG(vdc, 0,
22273af08d82Slm66018 				    "ldc_mem_info returned an error: %d\n",
22283af08d82Slm66018 				    status);
22293af08d82Slm66018 
22303af08d82Slm66018 				/*
22313af08d82Slm66018 				 * This must mean that the mem handle
22323af08d82Slm66018 				 * is not valid. Clear it out so that
22333af08d82Slm66018 				 * no one tries to use it.
22343af08d82Slm66018 				 */
22353af08d82Slm66018 				ldep->desc_mhdl = NULL;
22363af08d82Slm66018 				continue;
22373af08d82Slm66018 			}
22383af08d82Slm66018 
22393af08d82Slm66018 			if (minfo.status == LDC_BOUND) {
22403af08d82Slm66018 				(void) ldc_mem_unbind_handle(mhdl);
22413af08d82Slm66018 			}
22423af08d82Slm66018 
22431ae08745Sheppo 			(void) ldc_mem_free_handle(mhdl);
22443af08d82Slm66018 
22453af08d82Slm66018 			ldep->desc_mhdl = NULL;
22461ae08745Sheppo 		}
22471ae08745Sheppo 		vdc->initialized &= ~VDC_DRING_ENTRY;
22481ae08745Sheppo 	}
22491ae08745Sheppo 
22501ae08745Sheppo 	if (vdc->initialized & VDC_DRING_LOCAL) {
22513af08d82Slm66018 		DMSG(vdc, 0, "[%d] Freeing Local DRing\n", vdc->instance);
22521ae08745Sheppo 		kmem_free(vdc->local_dring,
2253e1ebb9ecSlm66018 				vdc->dring_len * sizeof (vdc_local_desc_t));
22541ae08745Sheppo 		vdc->initialized &= ~VDC_DRING_LOCAL;
22551ae08745Sheppo 	}
22561ae08745Sheppo 
22571ae08745Sheppo 	if (vdc->initialized & VDC_DRING_BOUND) {
22583af08d82Slm66018 		DMSG(vdc, 0, "[%d] Unbinding DRing\n", vdc->instance);
22591ae08745Sheppo 		status = ldc_mem_dring_unbind(vdc->ldc_dring_hdl);
22601ae08745Sheppo 		if (status == 0) {
22611ae08745Sheppo 			vdc->initialized &= ~VDC_DRING_BOUND;
22621ae08745Sheppo 		} else {
22633af08d82Slm66018 			DMSG(vdc, 0, "[%d] Error %d unbinding DRing %lx",
2264e1ebb9ecSlm66018 				vdc->instance, status, vdc->ldc_dring_hdl);
22651ae08745Sheppo 		}
22663af08d82Slm66018 		kmem_free(vdc->dring_cookie, sizeof (ldc_mem_cookie_t));
22671ae08745Sheppo 	}
22681ae08745Sheppo 
22691ae08745Sheppo 	if (vdc->initialized & VDC_DRING_INIT) {
22703af08d82Slm66018 		DMSG(vdc, 0, "[%d] Destroying DRing\n", vdc->instance);
22711ae08745Sheppo 		status = ldc_mem_dring_destroy(vdc->ldc_dring_hdl);
22721ae08745Sheppo 		if (status == 0) {
22731ae08745Sheppo 			vdc->ldc_dring_hdl = NULL;
22741ae08745Sheppo 			bzero(&vdc->dring_mem_info, sizeof (ldc_mem_info_t));
22751ae08745Sheppo 			vdc->initialized &= ~VDC_DRING_INIT;
22761ae08745Sheppo 		} else {
22773af08d82Slm66018 			DMSG(vdc, 0, "[%d] Error %d destroying DRing (%lx)",
2278e1ebb9ecSlm66018 				vdc->instance, status, vdc->ldc_dring_hdl);
22791ae08745Sheppo 		}
22801ae08745Sheppo 	}
22811ae08745Sheppo }
22821ae08745Sheppo 
22831ae08745Sheppo /*
22843af08d82Slm66018  * Function:
22853af08d82Slm66018  *	vdc_map_to_shared_ring()
22861ae08745Sheppo  *
22871ae08745Sheppo  * Description:
22883af08d82Slm66018  *	Copy contents of the local descriptor to the shared
22893af08d82Slm66018  *	memory descriptor.
22901ae08745Sheppo  *
22913af08d82Slm66018  * Arguments:
22923af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
22933af08d82Slm66018  *	idx	- descriptor ring index
22943af08d82Slm66018  *
22953af08d82Slm66018  * Return Code:
22963af08d82Slm66018  *	None
22971ae08745Sheppo  */
22981ae08745Sheppo static int
22993af08d82Slm66018 vdc_map_to_shared_dring(vdc_t *vdcp, int idx)
23001ae08745Sheppo {
23013af08d82Slm66018 	vdc_local_desc_t	*ldep;
23023af08d82Slm66018 	vd_dring_entry_t	*dep;
23033af08d82Slm66018 	int			rv;
23041ae08745Sheppo 
23053af08d82Slm66018 	ldep = &(vdcp->local_dring[idx]);
23061ae08745Sheppo 
23073af08d82Slm66018 	/* for now leave in the old pop_mem_hdl stuff */
23083af08d82Slm66018 	if (ldep->nbytes > 0) {
23093af08d82Slm66018 		rv = vdc_populate_mem_hdl(vdcp, ldep);
23103af08d82Slm66018 		if (rv) {
23113af08d82Slm66018 			DMSG(vdcp, 0, "[%d] Cannot populate mem handle\n",
23123af08d82Slm66018 			    vdcp->instance);
23133af08d82Slm66018 			return (rv);
23143af08d82Slm66018 		}
23153af08d82Slm66018 	}
23161ae08745Sheppo 
23173af08d82Slm66018 	/*
23183af08d82Slm66018 	 * fill in the data details into the DRing
23193af08d82Slm66018 	 */
2320d10e4ef2Snarayan 	dep = ldep->dep;
23211ae08745Sheppo 	ASSERT(dep != NULL);
23221ae08745Sheppo 
23233af08d82Slm66018 	dep->payload.req_id = VDC_GET_NEXT_REQ_ID(vdcp);
23243af08d82Slm66018 	dep->payload.operation = ldep->operation;
23253af08d82Slm66018 	dep->payload.addr = ldep->offset;
23263af08d82Slm66018 	dep->payload.nbytes = ldep->nbytes;
2327055d7c80Scarlsonj 	dep->payload.status = (uint32_t)-1;	/* vds will set valid value */
23283af08d82Slm66018 	dep->payload.slice = ldep->slice;
23293af08d82Slm66018 	dep->hdr.dstate = VIO_DESC_READY;
23303af08d82Slm66018 	dep->hdr.ack = 1;		/* request an ACK for every message */
23311ae08745Sheppo 
23323af08d82Slm66018 	return (0);
23331ae08745Sheppo }
23341ae08745Sheppo 
23351ae08745Sheppo /*
23361ae08745Sheppo  * Function:
23373af08d82Slm66018  *	vdc_send_request
23383af08d82Slm66018  *
23393af08d82Slm66018  * Description:
23403af08d82Slm66018  *	This routine writes the data to be transmitted to vds into the
23413af08d82Slm66018  *	descriptor, notifies vds that the ring has been updated and
23423af08d82Slm66018  *	then waits for the request to be processed.
23433af08d82Slm66018  *
23443af08d82Slm66018  * Arguments:
23453af08d82Slm66018  *	vdcp	  - the soft state pointer
23463af08d82Slm66018  *	operation - operation we want vds to perform (VD_OP_XXX)
23473af08d82Slm66018  *	addr	  - address of data buf to be read/written.
23483af08d82Slm66018  *	nbytes	  - number of bytes to read/write
23493af08d82Slm66018  *	slice	  - the disk slice this request is for
23503af08d82Slm66018  *	offset	  - relative disk offset
23513af08d82Slm66018  *	cb_type   - type of call - STRATEGY or SYNC
23523af08d82Slm66018  *	cb_arg	  - parameter to be sent to server (depends on VD_OP_XXX type)
23533af08d82Slm66018  *			. mode for ioctl(9e)
23543af08d82Slm66018  *			. LP64 diskaddr_t (block I/O)
23553af08d82Slm66018  *	dir	  - direction of operation (READ/WRITE/BOTH)
23563af08d82Slm66018  *
23573af08d82Slm66018  * Return Codes:
23583af08d82Slm66018  *	0
23593af08d82Slm66018  *	ENXIO
23603af08d82Slm66018  */
23613af08d82Slm66018 static int
23623af08d82Slm66018 vdc_send_request(vdc_t *vdcp, int operation, caddr_t addr,
23633af08d82Slm66018     size_t nbytes, int slice, diskaddr_t offset, int cb_type,
23643af08d82Slm66018     void *cb_arg, vio_desc_direction_t dir)
23653af08d82Slm66018 {
23663af08d82Slm66018 	ASSERT(vdcp != NULL);
23673af08d82Slm66018 	ASSERT(slice < V_NUMPAR);
23683af08d82Slm66018 
23693af08d82Slm66018 	mutex_enter(&vdcp->lock);
23703af08d82Slm66018 
23713af08d82Slm66018 	do {
23723c96341aSnarayan 		while (vdcp->state != VDC_STATE_RUNNING) {
23733af08d82Slm66018 			cv_wait(&vdcp->running_cv, &vdcp->lock);
23743af08d82Slm66018 
23753c96341aSnarayan 			/* return error if detaching */
23763c96341aSnarayan 			if (vdcp->state == VDC_STATE_DETACH) {
23773c96341aSnarayan 				mutex_exit(&vdcp->lock);
23783c96341aSnarayan 				return (ENXIO);
23793c96341aSnarayan 			}
23803c96341aSnarayan 		}
23813c96341aSnarayan 
23823af08d82Slm66018 	} while (vdc_populate_descriptor(vdcp, operation, addr,
23833af08d82Slm66018 	    nbytes, slice, offset, cb_type, cb_arg, dir));
23843af08d82Slm66018 
23853af08d82Slm66018 	mutex_exit(&vdcp->lock);
23863af08d82Slm66018 	return (0);
23873af08d82Slm66018 }
23883af08d82Slm66018 
23893af08d82Slm66018 
23903af08d82Slm66018 /*
23913af08d82Slm66018  * Function:
23921ae08745Sheppo  *	vdc_populate_descriptor
23931ae08745Sheppo  *
23941ae08745Sheppo  * Description:
23951ae08745Sheppo  *	This routine writes the data to be transmitted to vds into the
23961ae08745Sheppo  *	descriptor, notifies vds that the ring has been updated and
23971ae08745Sheppo  *	then waits for the request to be processed.
23981ae08745Sheppo  *
23991ae08745Sheppo  * Arguments:
24003af08d82Slm66018  *	vdcp	  - the soft state pointer
24011ae08745Sheppo  *	operation - operation we want vds to perform (VD_OP_XXX)
24023af08d82Slm66018  *	addr	  - address of data buf to be read/written.
24033af08d82Slm66018  *	nbytes	  - number of bytes to read/write
24043af08d82Slm66018  *	slice	  - the disk slice this request is for
24053af08d82Slm66018  *	offset	  - relative disk offset
24063af08d82Slm66018  *	cb_type   - type of call - STRATEGY or SYNC
24073af08d82Slm66018  *	cb_arg	  - parameter to be sent to server (depends on VD_OP_XXX type)
24081ae08745Sheppo  *			. mode for ioctl(9e)
24091ae08745Sheppo  *			. LP64 diskaddr_t (block I/O)
24103af08d82Slm66018  *	dir	  - direction of operation (READ/WRITE/BOTH)
24111ae08745Sheppo  *
24121ae08745Sheppo  * Return Codes:
24131ae08745Sheppo  *	0
24141ae08745Sheppo  *	EAGAIN
24151ae08745Sheppo  *		EFAULT
24161ae08745Sheppo  *		ENXIO
24171ae08745Sheppo  *		EIO
24181ae08745Sheppo  */
24191ae08745Sheppo static int
24203af08d82Slm66018 vdc_populate_descriptor(vdc_t *vdcp, int operation, caddr_t addr,
24213af08d82Slm66018     size_t nbytes, int slice, diskaddr_t offset, int cb_type,
24223af08d82Slm66018     void *cb_arg, vio_desc_direction_t dir)
24231ae08745Sheppo {
24243af08d82Slm66018 	vdc_local_desc_t	*local_dep = NULL; /* Local Dring Pointer */
24253af08d82Slm66018 	int			idx;		/* Index of DRing entry used */
24263af08d82Slm66018 	int			next_idx;
24271ae08745Sheppo 	vio_dring_msg_t		dmsg;
24283af08d82Slm66018 	size_t			msglen;
24298e6a2a04Slm66018 	int			rv;
24301ae08745Sheppo 
24313af08d82Slm66018 	ASSERT(MUTEX_HELD(&vdcp->lock));
24323af08d82Slm66018 	vdcp->threads_pending++;
24333af08d82Slm66018 loop:
24343af08d82Slm66018 	DMSG(vdcp, 2, ": dring_curr_idx = %d\n", vdcp->dring_curr_idx);
24351ae08745Sheppo 
24363af08d82Slm66018 	/* Get next available D-Ring entry */
24373af08d82Slm66018 	idx = vdcp->dring_curr_idx;
24383af08d82Slm66018 	local_dep = &(vdcp->local_dring[idx]);
24391ae08745Sheppo 
24403af08d82Slm66018 	if (!local_dep->is_free) {
24413af08d82Slm66018 		DMSG(vdcp, 2, "[%d]: dring full - waiting for space\n",
24423af08d82Slm66018 		    vdcp->instance);
24433af08d82Slm66018 		cv_wait(&vdcp->dring_free_cv, &vdcp->lock);
24443af08d82Slm66018 		if (vdcp->state == VDC_STATE_RUNNING ||
24453af08d82Slm66018 		    vdcp->state == VDC_STATE_HANDLE_PENDING) {
24463af08d82Slm66018 			goto loop;
24473af08d82Slm66018 		}
24483af08d82Slm66018 		vdcp->threads_pending--;
24493af08d82Slm66018 		return (ECONNRESET);
24501ae08745Sheppo 	}
24511ae08745Sheppo 
24523af08d82Slm66018 	next_idx = idx + 1;
24533af08d82Slm66018 	if (next_idx >= vdcp->dring_len)
24543af08d82Slm66018 		next_idx = 0;
24553af08d82Slm66018 	vdcp->dring_curr_idx = next_idx;
24561ae08745Sheppo 
24573af08d82Slm66018 	ASSERT(local_dep->is_free);
24581ae08745Sheppo 
24593af08d82Slm66018 	local_dep->operation = operation;
2460d10e4ef2Snarayan 	local_dep->addr = addr;
24613af08d82Slm66018 	local_dep->nbytes = nbytes;
24623af08d82Slm66018 	local_dep->slice = slice;
24633af08d82Slm66018 	local_dep->offset = offset;
24643af08d82Slm66018 	local_dep->cb_type = cb_type;
24653af08d82Slm66018 	local_dep->cb_arg = cb_arg;
24663af08d82Slm66018 	local_dep->dir = dir;
24673af08d82Slm66018 
24683af08d82Slm66018 	local_dep->is_free = B_FALSE;
24693af08d82Slm66018 
24703af08d82Slm66018 	rv = vdc_map_to_shared_dring(vdcp, idx);
24713af08d82Slm66018 	if (rv) {
24723af08d82Slm66018 		DMSG(vdcp, 0, "[%d]: cannot bind memory - waiting ..\n",
24733af08d82Slm66018 		    vdcp->instance);
24743af08d82Slm66018 		/* free the descriptor */
24753af08d82Slm66018 		local_dep->is_free = B_TRUE;
24763af08d82Slm66018 		vdcp->dring_curr_idx = idx;
24773af08d82Slm66018 		cv_wait(&vdcp->membind_cv, &vdcp->lock);
24783af08d82Slm66018 		if (vdcp->state == VDC_STATE_RUNNING ||
24793af08d82Slm66018 		    vdcp->state == VDC_STATE_HANDLE_PENDING) {
24803af08d82Slm66018 			goto loop;
24811ae08745Sheppo 		}
24823af08d82Slm66018 		vdcp->threads_pending--;
24833af08d82Slm66018 		return (ECONNRESET);
24841ae08745Sheppo 	}
24851ae08745Sheppo 
24861ae08745Sheppo 	/*
24871ae08745Sheppo 	 * Send a msg with the DRing details to vds
24881ae08745Sheppo 	 */
24891ae08745Sheppo 	VIO_INIT_DRING_DATA_TAG(dmsg);
24903af08d82Slm66018 	VDC_INIT_DRING_DATA_MSG_IDS(dmsg, vdcp);
24913af08d82Slm66018 	dmsg.dring_ident = vdcp->dring_ident;
24921ae08745Sheppo 	dmsg.start_idx = idx;
24931ae08745Sheppo 	dmsg.end_idx = idx;
24943af08d82Slm66018 	vdcp->seq_num++;
24951ae08745Sheppo 
24963af08d82Slm66018 	DTRACE_IO2(send, vio_dring_msg_t *, &dmsg, vdc_t *, vdcp);
2497d10e4ef2Snarayan 
24983af08d82Slm66018 	DMSG(vdcp, 2, "ident=0x%lx, st=%u, end=%u, seq=%ld\n",
24993af08d82Slm66018 	    vdcp->dring_ident, dmsg.start_idx, dmsg.end_idx, dmsg.seq_num);
25001ae08745Sheppo 
25013af08d82Slm66018 	/*
25023af08d82Slm66018 	 * note we're still holding the lock here to
25033af08d82Slm66018 	 * make sure the message goes out in order !!!...
25043af08d82Slm66018 	 */
25053af08d82Slm66018 	msglen = sizeof (dmsg);
25063af08d82Slm66018 	rv = vdc_send(vdcp, (caddr_t)&dmsg, &msglen);
25073af08d82Slm66018 	switch (rv) {
25083af08d82Slm66018 	case ECONNRESET:
25093af08d82Slm66018 		/*
25103af08d82Slm66018 		 * vdc_send initiates the reset on failure.
25113af08d82Slm66018 		 * Since the transaction has already been put
25123af08d82Slm66018 		 * on the local dring, it will automatically get
25133af08d82Slm66018 		 * retried when the channel is reset. Given that,
25143af08d82Slm66018 		 * it is ok to just return success even though the
25153af08d82Slm66018 		 * send failed.
25163af08d82Slm66018 		 */
25173af08d82Slm66018 		rv = 0;
25183af08d82Slm66018 		break;
2519d10e4ef2Snarayan 
25203af08d82Slm66018 	case 0: /* EOK */
25213af08d82Slm66018 		DMSG(vdcp, 1, "sent via LDC: rv=%d\n", rv);
25223af08d82Slm66018 		break;
2523d10e4ef2Snarayan 
25243af08d82Slm66018 	default:
25253af08d82Slm66018 		goto cleanup_and_exit;
25263af08d82Slm66018 	}
2527e1ebb9ecSlm66018 
25283af08d82Slm66018 	vdcp->threads_pending--;
25293af08d82Slm66018 	return (rv);
25303af08d82Slm66018 
25313af08d82Slm66018 cleanup_and_exit:
25323af08d82Slm66018 	DMSG(vdcp, 0, "unexpected error, rv=%d\n", rv);
25333af08d82Slm66018 	return (ENXIO);
25341ae08745Sheppo }
25351ae08745Sheppo 
25361ae08745Sheppo /*
25373af08d82Slm66018  * Function:
25383af08d82Slm66018  *	vdc_do_sync_op
25393af08d82Slm66018  *
25403af08d82Slm66018  * Description:
25413af08d82Slm66018  * 	Wrapper around vdc_populate_descriptor that blocks until the
25423af08d82Slm66018  * 	response to the message is available.
25433af08d82Slm66018  *
25443af08d82Slm66018  * Arguments:
25453af08d82Slm66018  *	vdcp	  - the soft state pointer
25463af08d82Slm66018  *	operation - operation we want vds to perform (VD_OP_XXX)
25473af08d82Slm66018  *	addr	  - address of data buf to be read/written.
25483af08d82Slm66018  *	nbytes	  - number of bytes to read/write
25493af08d82Slm66018  *	slice	  - the disk slice this request is for
25503af08d82Slm66018  *	offset	  - relative disk offset
25513af08d82Slm66018  *	cb_type   - type of call - STRATEGY or SYNC
25523af08d82Slm66018  *	cb_arg	  - parameter to be sent to server (depends on VD_OP_XXX type)
25533af08d82Slm66018  *			. mode for ioctl(9e)
25543af08d82Slm66018  *			. LP64 diskaddr_t (block I/O)
25553af08d82Slm66018  *	dir	  - direction of operation (READ/WRITE/BOTH)
25563af08d82Slm66018  *
25573af08d82Slm66018  * Return Codes:
25583af08d82Slm66018  *	0
25593af08d82Slm66018  *	EAGAIN
25603af08d82Slm66018  *		EFAULT
25613af08d82Slm66018  *		ENXIO
25623af08d82Slm66018  *		EIO
25630a55fbb7Slm66018  */
25643af08d82Slm66018 static int
25653af08d82Slm66018 vdc_do_sync_op(vdc_t *vdcp, int operation, caddr_t addr, size_t nbytes,
25663af08d82Slm66018     int slice, diskaddr_t offset, int cb_type, void *cb_arg,
25673af08d82Slm66018     vio_desc_direction_t dir)
25683af08d82Slm66018 {
25693af08d82Slm66018 	int status;
25703af08d82Slm66018 
25713af08d82Slm66018 	ASSERT(cb_type == CB_SYNC);
25721ae08745Sheppo 
25731ae08745Sheppo 	/*
25743af08d82Slm66018 	 * Grab the lock, if blocked wait until the server
25753af08d82Slm66018 	 * response causes us to wake up again.
25763af08d82Slm66018 	 */
25773af08d82Slm66018 	mutex_enter(&vdcp->lock);
25783af08d82Slm66018 	vdcp->sync_op_cnt++;
25793af08d82Slm66018 	while (vdcp->sync_op_blocked && vdcp->state != VDC_STATE_DETACH)
25803af08d82Slm66018 		cv_wait(&vdcp->sync_blocked_cv, &vdcp->lock);
25813af08d82Slm66018 
25823af08d82Slm66018 	if (vdcp->state == VDC_STATE_DETACH) {
25833af08d82Slm66018 		cv_broadcast(&vdcp->sync_blocked_cv);
25843af08d82Slm66018 		vdcp->sync_op_cnt--;
25853af08d82Slm66018 		mutex_exit(&vdcp->lock);
25863af08d82Slm66018 		return (ENXIO);
25873af08d82Slm66018 	}
25883af08d82Slm66018 
25893af08d82Slm66018 	/* now block anyone other thread entering after us */
25903af08d82Slm66018 	vdcp->sync_op_blocked = B_TRUE;
25913af08d82Slm66018 	vdcp->sync_op_pending = B_TRUE;
25923af08d82Slm66018 	mutex_exit(&vdcp->lock);
25933af08d82Slm66018 
25943af08d82Slm66018 	/*
25953af08d82Slm66018 	 * No need to check return value - will return error only
25963af08d82Slm66018 	 * in the DETACH case and we can fall through
25973af08d82Slm66018 	 */
25983af08d82Slm66018 	(void) vdc_send_request(vdcp, operation, addr,
25993af08d82Slm66018 	    nbytes, slice, offset, cb_type, cb_arg, dir);
26003af08d82Slm66018 
26013af08d82Slm66018 	/*
26023af08d82Slm66018 	 * block until our transaction completes.
26033af08d82Slm66018 	 * Also anyone else waiting also gets to go next.
26043af08d82Slm66018 	 */
26053af08d82Slm66018 	mutex_enter(&vdcp->lock);
26063af08d82Slm66018 	while (vdcp->sync_op_pending && vdcp->state != VDC_STATE_DETACH)
26073af08d82Slm66018 		cv_wait(&vdcp->sync_pending_cv, &vdcp->lock);
26083af08d82Slm66018 
26093af08d82Slm66018 	DMSG(vdcp, 2, ": operation returned %d\n", vdcp->sync_op_status);
26103c96341aSnarayan 	if (vdcp->state == VDC_STATE_DETACH) {
26113c96341aSnarayan 		vdcp->sync_op_pending = B_FALSE;
26123af08d82Slm66018 		status = ENXIO;
26133c96341aSnarayan 	} else {
26143af08d82Slm66018 		status = vdcp->sync_op_status;
26153c96341aSnarayan 	}
26163c96341aSnarayan 
26173af08d82Slm66018 	vdcp->sync_op_status = 0;
26183af08d82Slm66018 	vdcp->sync_op_blocked = B_FALSE;
26193af08d82Slm66018 	vdcp->sync_op_cnt--;
26203af08d82Slm66018 
26213af08d82Slm66018 	/* signal the next waiting thread */
26223af08d82Slm66018 	cv_signal(&vdcp->sync_blocked_cv);
26233af08d82Slm66018 	mutex_exit(&vdcp->lock);
26243af08d82Slm66018 
26253af08d82Slm66018 	return (status);
26263af08d82Slm66018 }
26273af08d82Slm66018 
26283af08d82Slm66018 
26293af08d82Slm66018 /*
26303af08d82Slm66018  * Function:
26313af08d82Slm66018  *	vdc_drain_response()
26323af08d82Slm66018  *
26333af08d82Slm66018  * Description:
26341ae08745Sheppo  * 	When a guest is panicking, the completion of requests needs to be
26351ae08745Sheppo  * 	handled differently because interrupts are disabled and vdc
26361ae08745Sheppo  * 	will not get messages. We have to poll for the messages instead.
26373af08d82Slm66018  *
26383af08d82Slm66018  * Arguments:
26393af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
26403af08d82Slm66018  *
26413af08d82Slm66018  * Return Code:
26423af08d82Slm66018  *	0	- Success
26431ae08745Sheppo  */
26443af08d82Slm66018 static int
26453af08d82Slm66018 vdc_drain_response(vdc_t *vdc)
26463af08d82Slm66018 {
26473af08d82Slm66018 	int 			rv, idx, retries;
26483af08d82Slm66018 	size_t			msglen;
26493af08d82Slm66018 	vdc_local_desc_t 	*ldep = NULL;	/* Local Dring Entry Pointer */
26503af08d82Slm66018 	vio_dring_msg_t		dmsg;
26513af08d82Slm66018 
26523af08d82Slm66018 	mutex_enter(&vdc->lock);
26533af08d82Slm66018 
26541ae08745Sheppo 	retries = 0;
26551ae08745Sheppo 	for (;;) {
26561ae08745Sheppo 		msglen = sizeof (dmsg);
26573af08d82Slm66018 		rv = ldc_read(vdc->ldc_handle, (caddr_t)&dmsg, &msglen);
26588e6a2a04Slm66018 		if (rv) {
26598e6a2a04Slm66018 			rv = EINVAL;
26601ae08745Sheppo 			break;
26611ae08745Sheppo 		}
26621ae08745Sheppo 
26631ae08745Sheppo 		/*
26641ae08745Sheppo 		 * if there are no packets wait and check again
26651ae08745Sheppo 		 */
26668e6a2a04Slm66018 		if ((rv == 0) && (msglen == 0)) {
26671ae08745Sheppo 			if (retries++ > vdc_dump_retries) {
26688e6a2a04Slm66018 				rv = EAGAIN;
26691ae08745Sheppo 				break;
26701ae08745Sheppo 			}
26711ae08745Sheppo 
2672d10e4ef2Snarayan 			drv_usecwait(vdc_usec_timeout_dump);
26731ae08745Sheppo 			continue;
26741ae08745Sheppo 		}
26751ae08745Sheppo 
26761ae08745Sheppo 		/*
26771ae08745Sheppo 		 * Ignore all messages that are not ACKs/NACKs to
26781ae08745Sheppo 		 * DRing requests.
26791ae08745Sheppo 		 */
26801ae08745Sheppo 		if ((dmsg.tag.vio_msgtype != VIO_TYPE_DATA) ||
26811ae08745Sheppo 		    (dmsg.tag.vio_subtype_env != VIO_DRING_DATA)) {
26823af08d82Slm66018 			DMSG(vdc, 0, "discard pkt: type=%d sub=%d env=%d\n",
26831ae08745Sheppo 			    dmsg.tag.vio_msgtype,
26841ae08745Sheppo 			    dmsg.tag.vio_subtype,
26851ae08745Sheppo 			    dmsg.tag.vio_subtype_env);
26861ae08745Sheppo 			continue;
26871ae08745Sheppo 		}
26881ae08745Sheppo 
26891ae08745Sheppo 		/*
26903af08d82Slm66018 		 * set the appropriate return value for the current request.
26911ae08745Sheppo 		 */
26921ae08745Sheppo 		switch (dmsg.tag.vio_subtype) {
26931ae08745Sheppo 		case VIO_SUBTYPE_ACK:
26948e6a2a04Slm66018 			rv = 0;
26951ae08745Sheppo 			break;
26961ae08745Sheppo 		case VIO_SUBTYPE_NACK:
26978e6a2a04Slm66018 			rv = EAGAIN;
26981ae08745Sheppo 			break;
26991ae08745Sheppo 		default:
27001ae08745Sheppo 			continue;
27011ae08745Sheppo 		}
27021ae08745Sheppo 
27033af08d82Slm66018 		idx = dmsg.start_idx;
27043af08d82Slm66018 		if (idx >= vdc->dring_len) {
27053af08d82Slm66018 			DMSG(vdc, 0, "[%d] Bogus ack data : start %d\n",
2706e1ebb9ecSlm66018 			    vdc->instance, idx);
27073af08d82Slm66018 			continue;
27081ae08745Sheppo 		}
27093af08d82Slm66018 		ldep = &vdc->local_dring[idx];
27103af08d82Slm66018 		if (ldep->dep->hdr.dstate != VIO_DESC_DONE) {
27113af08d82Slm66018 			DMSG(vdc, 0, "[%d] Entry @ %d - state !DONE %d\n",
27123af08d82Slm66018 			    vdc->instance, idx, ldep->dep->hdr.dstate);
27131ae08745Sheppo 			continue;
27141ae08745Sheppo 		}
27151ae08745Sheppo 
27163af08d82Slm66018 		DMSG(vdc, 1, "[%d] Depopulating idx=%d state=%d\n",
27173af08d82Slm66018 		    vdc->instance, idx, ldep->dep->hdr.dstate);
27183af08d82Slm66018 		rv = vdc_depopulate_descriptor(vdc, idx);
27193af08d82Slm66018 		if (rv) {
27203af08d82Slm66018 			DMSG(vdc, 0,
27213af08d82Slm66018 			    "[%d] Entry @ %d - depopulate failed ..\n",
27223af08d82Slm66018 			    vdc->instance, idx);
27231ae08745Sheppo 		}
27241ae08745Sheppo 
27253af08d82Slm66018 		/* if this is the last descriptor - break out of loop */
27263af08d82Slm66018 		if ((idx + 1) % vdc->dring_len == vdc->dring_curr_idx)
27273af08d82Slm66018 			break;
27283af08d82Slm66018 	}
27293af08d82Slm66018 
27303af08d82Slm66018 	mutex_exit(&vdc->lock);
27313af08d82Slm66018 	DMSG(vdc, 0, "End idx=%d\n", idx);
27323af08d82Slm66018 
27333af08d82Slm66018 	return (rv);
27341ae08745Sheppo }
27351ae08745Sheppo 
27361ae08745Sheppo 
27370a55fbb7Slm66018 /*
27380a55fbb7Slm66018  * Function:
27390a55fbb7Slm66018  *	vdc_depopulate_descriptor()
27400a55fbb7Slm66018  *
27410a55fbb7Slm66018  * Description:
27420a55fbb7Slm66018  *
27430a55fbb7Slm66018  * Arguments:
27440a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
27450a55fbb7Slm66018  *	idx	- Index of the Descriptor Ring entry being modified
27460a55fbb7Slm66018  *
27470a55fbb7Slm66018  * Return Code:
27480a55fbb7Slm66018  *	0	- Success
27490a55fbb7Slm66018  */
27501ae08745Sheppo static int
27511ae08745Sheppo vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx)
27521ae08745Sheppo {
27531ae08745Sheppo 	vd_dring_entry_t *dep = NULL;		/* Dring Entry Pointer */
27541ae08745Sheppo 	vdc_local_desc_t *ldep = NULL;		/* Local Dring Entry Pointer */
27551ae08745Sheppo 	int		status = ENXIO;
27568e6a2a04Slm66018 	int		operation;
27578e6a2a04Slm66018 	int		rv = 0;
27581ae08745Sheppo 
27591ae08745Sheppo 	ASSERT(vdc != NULL);
2760e1ebb9ecSlm66018 	ASSERT(idx < vdc->dring_len);
27611ae08745Sheppo 	ldep = &vdc->local_dring[idx];
27621ae08745Sheppo 	ASSERT(ldep != NULL);
27633af08d82Slm66018 	ASSERT(MUTEX_HELD(&vdc->lock));
27643af08d82Slm66018 
27653af08d82Slm66018 	DMSG(vdc, 2, ": idx = %d\n", idx);
27661ae08745Sheppo 	dep = ldep->dep;
27671ae08745Sheppo 	ASSERT(dep != NULL);
2768e1ebb9ecSlm66018 	ASSERT((dep->hdr.dstate == VIO_DESC_DONE) ||
2769e1ebb9ecSlm66018 			(dep->payload.status == ECANCELED));
27701ae08745Sheppo 
2771e1ebb9ecSlm66018 	VDC_MARK_DRING_ENTRY_FREE(vdc, idx);
27723af08d82Slm66018 
27733af08d82Slm66018 	ldep->is_free = B_TRUE;
27743af08d82Slm66018 	DMSG(vdc, 2, ": is_free = %d\n", ldep->is_free);
27751ae08745Sheppo 	status = dep->payload.status;
27768e6a2a04Slm66018 	operation = dep->payload.operation;
27771ae08745Sheppo 
27784bac2208Snarayan 	/* the DKIO FLUSH operation never bind handles so we can return now */
27794bac2208Snarayan 	if (operation == VD_OP_FLUSH)
27808e6a2a04Slm66018 		return (status);
27818e6a2a04Slm66018 
27821ae08745Sheppo 	/*
27831ae08745Sheppo 	 * If the upper layer passed in a misaligned address we copied the
27841ae08745Sheppo 	 * data into an aligned buffer before sending it to LDC - we now
27851ae08745Sheppo 	 * copy it back to the original buffer.
27861ae08745Sheppo 	 */
27871ae08745Sheppo 	if (ldep->align_addr) {
27881ae08745Sheppo 		ASSERT(ldep->addr != NULL);
27891ae08745Sheppo 
27903c96341aSnarayan 		if (dep->payload.nbytes > 0)
27913c96341aSnarayan 			bcopy(ldep->align_addr, ldep->addr,
27923c96341aSnarayan 			    dep->payload.nbytes);
27931ae08745Sheppo 		kmem_free(ldep->align_addr,
27943c96341aSnarayan 			sizeof (caddr_t) * P2ROUNDUP(ldep->nbytes, 8));
27951ae08745Sheppo 		ldep->align_addr = NULL;
27961ae08745Sheppo 	}
27971ae08745Sheppo 
27988e6a2a04Slm66018 	rv = ldc_mem_unbind_handle(ldep->desc_mhdl);
27998e6a2a04Slm66018 	if (rv != 0) {
28003af08d82Slm66018 		DMSG(vdc, 0, "?[%d] unbind mhdl 0x%lx @ idx %d failed (%d)",
28018e6a2a04Slm66018 				vdc->instance, ldep->desc_mhdl, idx, rv);
28028e6a2a04Slm66018 		/*
28038e6a2a04Slm66018 		 * The error returned by the vDisk server is more informative
28048e6a2a04Slm66018 		 * and thus has a higher priority but if it isn't set we ensure
28058e6a2a04Slm66018 		 * that this function returns an error.
28068e6a2a04Slm66018 		 */
28078e6a2a04Slm66018 		if (status == 0)
28088e6a2a04Slm66018 			status = EINVAL;
28091ae08745Sheppo 	}
28101ae08745Sheppo 
28113af08d82Slm66018 	cv_signal(&vdc->membind_cv);
28123af08d82Slm66018 	cv_signal(&vdc->dring_free_cv);
28133af08d82Slm66018 
28141ae08745Sheppo 	return (status);
28151ae08745Sheppo }
28161ae08745Sheppo 
28170a55fbb7Slm66018 /*
28180a55fbb7Slm66018  * Function:
28190a55fbb7Slm66018  *	vdc_populate_mem_hdl()
28200a55fbb7Slm66018  *
28210a55fbb7Slm66018  * Description:
28220a55fbb7Slm66018  *
28230a55fbb7Slm66018  * Arguments:
28240a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
28250a55fbb7Slm66018  *	idx	- Index of the Descriptor Ring entry being modified
28260a55fbb7Slm66018  *	addr	- virtual address being mapped in
28270a55fbb7Slm66018  *	nybtes	- number of bytes in 'addr'
28280a55fbb7Slm66018  *	operation - the vDisk operation being performed (VD_OP_xxx)
28290a55fbb7Slm66018  *
28300a55fbb7Slm66018  * Return Code:
28310a55fbb7Slm66018  *	0	- Success
28320a55fbb7Slm66018  */
28331ae08745Sheppo static int
28343af08d82Slm66018 vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep)
28351ae08745Sheppo {
28361ae08745Sheppo 	vd_dring_entry_t	*dep = NULL;
28371ae08745Sheppo 	ldc_mem_handle_t	mhdl;
28381ae08745Sheppo 	caddr_t			vaddr;
28393af08d82Slm66018 	size_t			nbytes;
28404bac2208Snarayan 	uint8_t			perm = LDC_MEM_RW;
28414bac2208Snarayan 	uint8_t			maptype;
28421ae08745Sheppo 	int			rv = 0;
28431ae08745Sheppo 	int			i;
28441ae08745Sheppo 
28453af08d82Slm66018 	ASSERT(vdcp != NULL);
28461ae08745Sheppo 
28473af08d82Slm66018 	dep = ldep->dep;
28481ae08745Sheppo 	mhdl = ldep->desc_mhdl;
28491ae08745Sheppo 
28503af08d82Slm66018 	switch (ldep->dir) {
28513af08d82Slm66018 	case VIO_read_dir:
28521ae08745Sheppo 		perm = LDC_MEM_W;
28531ae08745Sheppo 		break;
28541ae08745Sheppo 
28553af08d82Slm66018 	case VIO_write_dir:
28561ae08745Sheppo 		perm = LDC_MEM_R;
28571ae08745Sheppo 		break;
28581ae08745Sheppo 
28593af08d82Slm66018 	case VIO_both_dir:
28601ae08745Sheppo 		perm = LDC_MEM_RW;
28611ae08745Sheppo 		break;
28621ae08745Sheppo 
28631ae08745Sheppo 	default:
28641ae08745Sheppo 		ASSERT(0);	/* catch bad programming in vdc */
28651ae08745Sheppo 	}
28661ae08745Sheppo 
28671ae08745Sheppo 	/*
28681ae08745Sheppo 	 * LDC expects any addresses passed in to be 8-byte aligned. We need
28691ae08745Sheppo 	 * to copy the contents of any misaligned buffers to a newly allocated
28701ae08745Sheppo 	 * buffer and bind it instead (and copy the the contents back to the
28711ae08745Sheppo 	 * original buffer passed in when depopulating the descriptor)
28721ae08745Sheppo 	 */
28733af08d82Slm66018 	vaddr = ldep->addr;
28743af08d82Slm66018 	nbytes = ldep->nbytes;
28753af08d82Slm66018 	if (((uint64_t)vaddr & 0x7) != 0) {
2876d10e4ef2Snarayan 		ASSERT(ldep->align_addr == NULL);
28771ae08745Sheppo 		ldep->align_addr =
28783af08d82Slm66018 			kmem_alloc(sizeof (caddr_t) *
28793af08d82Slm66018 				P2ROUNDUP(nbytes, 8), KM_SLEEP);
28803af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Misaligned address %p reallocating "
28813af08d82Slm66018 		    "(buf=%p nb=%ld op=%d)\n",
28823af08d82Slm66018 		    vdcp->instance, (void *)vaddr, (void *)ldep->align_addr,
28833af08d82Slm66018 		    nbytes, ldep->operation);
28843af08d82Slm66018 		if (perm != LDC_MEM_W)
28853af08d82Slm66018 			bcopy(vaddr, ldep->align_addr, nbytes);
28861ae08745Sheppo 		vaddr = ldep->align_addr;
28871ae08745Sheppo 	}
28881ae08745Sheppo 
28894bac2208Snarayan 	maptype = LDC_IO_MAP|LDC_SHADOW_MAP|LDC_DIRECT_MAP;
28901ae08745Sheppo 	rv = ldc_mem_bind_handle(mhdl, vaddr, P2ROUNDUP(nbytes, 8),
28914bac2208Snarayan 		maptype, perm, &dep->payload.cookie[0],
28921ae08745Sheppo 		&dep->payload.ncookies);
28933af08d82Slm66018 	DMSG(vdcp, 2, "[%d] bound mem handle; ncookies=%d\n",
28943af08d82Slm66018 			vdcp->instance, dep->payload.ncookies);
28951ae08745Sheppo 	if (rv != 0) {
28963af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Failed to bind LDC memory handle "
28973af08d82Slm66018 		    "(mhdl=%p, buf=%p, err=%d)\n",
28983af08d82Slm66018 		    vdcp->instance, (void *)mhdl, (void *)vaddr, rv);
28991ae08745Sheppo 		if (ldep->align_addr) {
29001ae08745Sheppo 			kmem_free(ldep->align_addr,
2901d10e4ef2Snarayan 				sizeof (caddr_t) * P2ROUNDUP(nbytes, 8));
29021ae08745Sheppo 			ldep->align_addr = NULL;
29031ae08745Sheppo 		}
29041ae08745Sheppo 		return (EAGAIN);
29051ae08745Sheppo 	}
29061ae08745Sheppo 
29071ae08745Sheppo 	/*
29081ae08745Sheppo 	 * Get the other cookies (if any).
29091ae08745Sheppo 	 */
29101ae08745Sheppo 	for (i = 1; i < dep->payload.ncookies; i++) {
29111ae08745Sheppo 		rv = ldc_mem_nextcookie(mhdl, &dep->payload.cookie[i]);
29121ae08745Sheppo 		if (rv != 0) {
29131ae08745Sheppo 			(void) ldc_mem_unbind_handle(mhdl);
29143af08d82Slm66018 			DMSG(vdcp, 0, "?[%d] Failed to get next cookie "
2915e1ebb9ecSlm66018 					"(mhdl=%lx cnum=%d), err=%d",
29163af08d82Slm66018 					vdcp->instance, mhdl, i, rv);
29171ae08745Sheppo 			if (ldep->align_addr) {
29181ae08745Sheppo 				kmem_free(ldep->align_addr,
29193c96341aSnarayan 					sizeof (caddr_t) * ldep->nbytes);
29201ae08745Sheppo 				ldep->align_addr = NULL;
29211ae08745Sheppo 			}
29221ae08745Sheppo 			return (EAGAIN);
29231ae08745Sheppo 		}
29241ae08745Sheppo 	}
29251ae08745Sheppo 
29261ae08745Sheppo 	return (rv);
29271ae08745Sheppo }
29281ae08745Sheppo 
29291ae08745Sheppo /*
29301ae08745Sheppo  * Interrupt handlers for messages from LDC
29311ae08745Sheppo  */
29321ae08745Sheppo 
29330a55fbb7Slm66018 /*
29340a55fbb7Slm66018  * Function:
29350a55fbb7Slm66018  *	vdc_handle_cb()
29360a55fbb7Slm66018  *
29370a55fbb7Slm66018  * Description:
29380a55fbb7Slm66018  *
29390a55fbb7Slm66018  * Arguments:
29400a55fbb7Slm66018  *	event	- Type of event (LDC_EVT_xxx) that triggered the callback
29410a55fbb7Slm66018  *	arg	- soft state pointer for this instance of the device driver.
29420a55fbb7Slm66018  *
29430a55fbb7Slm66018  * Return Code:
29440a55fbb7Slm66018  *	0	- Success
29450a55fbb7Slm66018  */
29461ae08745Sheppo static uint_t
29471ae08745Sheppo vdc_handle_cb(uint64_t event, caddr_t arg)
29481ae08745Sheppo {
29491ae08745Sheppo 	ldc_status_t	ldc_state;
29501ae08745Sheppo 	int		rv = 0;
29511ae08745Sheppo 
29521ae08745Sheppo 	vdc_t	*vdc = (vdc_t *)(void *)arg;
29531ae08745Sheppo 
29541ae08745Sheppo 	ASSERT(vdc != NULL);
29551ae08745Sheppo 
29563af08d82Slm66018 	DMSG(vdc, 1, "evt=%lx seqID=%ld\n", event, vdc->seq_num);
29571ae08745Sheppo 
29581ae08745Sheppo 	/*
29591ae08745Sheppo 	 * Depending on the type of event that triggered this callback,
29603af08d82Slm66018 	 * we modify the handshake state or read the data.
29611ae08745Sheppo 	 *
29621ae08745Sheppo 	 * NOTE: not done as a switch() as event could be triggered by
29631ae08745Sheppo 	 * a state change and a read request. Also the ordering	of the
29641ae08745Sheppo 	 * check for the event types is deliberate.
29651ae08745Sheppo 	 */
29661ae08745Sheppo 	if (event & LDC_EVT_UP) {
29673af08d82Slm66018 		DMSG(vdc, 0, "[%d] Received LDC_EVT_UP\n", vdc->instance);
29683af08d82Slm66018 
29693af08d82Slm66018 		mutex_enter(&vdc->lock);
29701ae08745Sheppo 
29711ae08745Sheppo 		/* get LDC state */
29721ae08745Sheppo 		rv = ldc_status(vdc->ldc_handle, &ldc_state);
29731ae08745Sheppo 		if (rv != 0) {
29743af08d82Slm66018 			DMSG(vdc, 0, "[%d] Couldn't get LDC status %d",
29751ae08745Sheppo 			    vdc->instance, rv);
29761ae08745Sheppo 			return (LDC_SUCCESS);
29771ae08745Sheppo 		}
29783af08d82Slm66018 		if (vdc->ldc_state != LDC_UP && ldc_state == LDC_UP) {
29791ae08745Sheppo 			/*
29803af08d82Slm66018 			 * Reset the transaction sequence numbers when
29813af08d82Slm66018 			 * LDC comes up. We then kick off the handshake
29823af08d82Slm66018 			 * negotiation with the vDisk server.
29831ae08745Sheppo 			 */
29840a55fbb7Slm66018 			vdc->seq_num = 1;
29851ae08745Sheppo 			vdc->seq_num_reply = 0;
29861ae08745Sheppo 			vdc->ldc_state = ldc_state;
29873af08d82Slm66018 			cv_signal(&vdc->initwait_cv);
29883af08d82Slm66018 		}
29893af08d82Slm66018 
29901ae08745Sheppo 		mutex_exit(&vdc->lock);
29911ae08745Sheppo 	}
29921ae08745Sheppo 
29931ae08745Sheppo 	if (event & LDC_EVT_READ) {
29943af08d82Slm66018 		DMSG(vdc, 0, "[%d] Received LDC_EVT_READ\n", vdc->instance);
29953af08d82Slm66018 		mutex_enter(&vdc->read_lock);
29963af08d82Slm66018 		cv_signal(&vdc->read_cv);
29973af08d82Slm66018 		vdc->read_state = VDC_READ_PENDING;
29983af08d82Slm66018 		mutex_exit(&vdc->read_lock);
29991ae08745Sheppo 
30001ae08745Sheppo 		/* that's all we have to do - no need to handle DOWN/RESET */
30011ae08745Sheppo 		return (LDC_SUCCESS);
30021ae08745Sheppo 	}
30031ae08745Sheppo 
30043af08d82Slm66018 	if (event & (LDC_EVT_RESET|LDC_EVT_DOWN)) {
30050a55fbb7Slm66018 
30063af08d82Slm66018 		DMSG(vdc, 0, "[%d] Received LDC RESET event\n", vdc->instance);
30073af08d82Slm66018 
30080a55fbb7Slm66018 		mutex_enter(&vdc->lock);
30093af08d82Slm66018 		/*
30103af08d82Slm66018 		 * Need to wake up any readers so they will
30113af08d82Slm66018 		 * detect that a reset has occurred.
30123af08d82Slm66018 		 */
30133af08d82Slm66018 		mutex_enter(&vdc->read_lock);
30143af08d82Slm66018 		if ((vdc->read_state == VDC_READ_WAITING) ||
30153af08d82Slm66018 		    (vdc->read_state == VDC_READ_RESET))
30163af08d82Slm66018 			cv_signal(&vdc->read_cv);
30173af08d82Slm66018 		vdc->read_state = VDC_READ_RESET;
30183af08d82Slm66018 		mutex_exit(&vdc->read_lock);
30190a55fbb7Slm66018 
30203af08d82Slm66018 		/* wake up any threads waiting for connection to come up */
30213af08d82Slm66018 		if (vdc->state == VDC_STATE_INIT_WAITING) {
30223af08d82Slm66018 			vdc->state = VDC_STATE_RESETTING;
30233af08d82Slm66018 			cv_signal(&vdc->initwait_cv);
30241ae08745Sheppo 		}
30251ae08745Sheppo 
30260a55fbb7Slm66018 		mutex_exit(&vdc->lock);
30271ae08745Sheppo 	}
30281ae08745Sheppo 
30291ae08745Sheppo 	if (event & ~(LDC_EVT_UP | LDC_EVT_RESET | LDC_EVT_DOWN | LDC_EVT_READ))
30303af08d82Slm66018 		DMSG(vdc, 0, "![%d] Unexpected LDC event (%lx) received",
30311ae08745Sheppo 				vdc->instance, event);
30321ae08745Sheppo 
30331ae08745Sheppo 	return (LDC_SUCCESS);
30341ae08745Sheppo }
30351ae08745Sheppo 
30363af08d82Slm66018 /*
30373af08d82Slm66018  * Function:
30383af08d82Slm66018  *	vdc_wait_for_response()
30393af08d82Slm66018  *
30403af08d82Slm66018  * Description:
30413af08d82Slm66018  *	Block waiting for a response from the server. If there is
30423af08d82Slm66018  *	no data the thread block on the read_cv that is signalled
30433af08d82Slm66018  *	by the callback when an EVT_READ occurs.
30443af08d82Slm66018  *
30453af08d82Slm66018  * Arguments:
30463af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
30473af08d82Slm66018  *
30483af08d82Slm66018  * Return Code:
30493af08d82Slm66018  *	0	- Success
30503af08d82Slm66018  */
30513af08d82Slm66018 static int
30523af08d82Slm66018 vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp)
30533af08d82Slm66018 {
30543af08d82Slm66018 	size_t		nbytes = sizeof (*msgp);
30553af08d82Slm66018 	int		status;
30563af08d82Slm66018 
30573af08d82Slm66018 	ASSERT(vdcp != NULL);
30583af08d82Slm66018 
30593af08d82Slm66018 	DMSG(vdcp, 1, "[%d] Entered\n", vdcp->instance);
30603af08d82Slm66018 
30613af08d82Slm66018 	status = vdc_recv(vdcp, msgp, &nbytes);
30623af08d82Slm66018 	DMSG(vdcp, 3, "vdc_read() done.. status=0x%x size=0x%x\n",
30633af08d82Slm66018 		status, (int)nbytes);
30643af08d82Slm66018 	if (status) {
30653af08d82Slm66018 		DMSG(vdcp, 0, "?[%d] Error %d reading LDC msg\n",
30663af08d82Slm66018 				vdcp->instance, status);
30673af08d82Slm66018 		return (status);
30683af08d82Slm66018 	}
30693af08d82Slm66018 
30703af08d82Slm66018 	if (nbytes < sizeof (vio_msg_tag_t)) {
30713af08d82Slm66018 		DMSG(vdcp, 0, "?[%d] Expect %lu bytes; recv'd %lu\n",
30723af08d82Slm66018 			vdcp->instance, sizeof (vio_msg_tag_t), nbytes);
30733af08d82Slm66018 		return (ENOMSG);
30743af08d82Slm66018 	}
30753af08d82Slm66018 
30763af08d82Slm66018 	DMSG(vdcp, 2, "[%d] (%x/%x/%x)\n", vdcp->instance,
30773af08d82Slm66018 	    msgp->tag.vio_msgtype,
30783af08d82Slm66018 	    msgp->tag.vio_subtype,
30793af08d82Slm66018 	    msgp->tag.vio_subtype_env);
30803af08d82Slm66018 
30813af08d82Slm66018 	/*
30823af08d82Slm66018 	 * Verify the Session ID of the message
30833af08d82Slm66018 	 *
30843af08d82Slm66018 	 * Every message after the Version has been negotiated should
30853af08d82Slm66018 	 * have the correct session ID set.
30863af08d82Slm66018 	 */
30873af08d82Slm66018 	if ((msgp->tag.vio_sid != vdcp->session_id) &&
30883af08d82Slm66018 	    (msgp->tag.vio_subtype_env != VIO_VER_INFO)) {
30893af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Invalid SID: received 0x%x, "
30903af08d82Slm66018 				"expected 0x%lx [seq num %lx @ %d]",
30913af08d82Slm66018 			vdcp->instance, msgp->tag.vio_sid,
30923af08d82Slm66018 			vdcp->session_id,
30933af08d82Slm66018 			((vio_dring_msg_t *)msgp)->seq_num,
30943af08d82Slm66018 			((vio_dring_msg_t *)msgp)->start_idx);
30953af08d82Slm66018 		return (ENOMSG);
30963af08d82Slm66018 	}
30973af08d82Slm66018 	return (0);
30983af08d82Slm66018 }
30993af08d82Slm66018 
31003af08d82Slm66018 
31013af08d82Slm66018 /*
31023af08d82Slm66018  * Function:
31033af08d82Slm66018  *	vdc_resubmit_backup_dring()
31043af08d82Slm66018  *
31053af08d82Slm66018  * Description:
31063af08d82Slm66018  *	Resubmit each descriptor in the backed up dring to
31073af08d82Slm66018  * 	vDisk server. The Dring was backed up during connection
31083af08d82Slm66018  *	reset.
31093af08d82Slm66018  *
31103af08d82Slm66018  * Arguments:
31113af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
31123af08d82Slm66018  *
31133af08d82Slm66018  * Return Code:
31143af08d82Slm66018  *	0	- Success
31153af08d82Slm66018  */
31163af08d82Slm66018 static int
31173af08d82Slm66018 vdc_resubmit_backup_dring(vdc_t *vdcp)
31183af08d82Slm66018 {
31193af08d82Slm66018 	int		count;
31203af08d82Slm66018 	int		b_idx;
31213af08d82Slm66018 	int		rv;
31223af08d82Slm66018 	int		dring_size;
31233af08d82Slm66018 	int		status;
31243af08d82Slm66018 	vio_msg_t	vio_msg;
31253af08d82Slm66018 	vdc_local_desc_t	*curr_ldep;
31263af08d82Slm66018 
31273af08d82Slm66018 	ASSERT(MUTEX_NOT_HELD(&vdcp->lock));
31283af08d82Slm66018 	ASSERT(vdcp->state == VDC_STATE_HANDLE_PENDING);
31293af08d82Slm66018 
31303af08d82Slm66018 	DMSG(vdcp, 1, "restoring pending dring entries (len=%d, tail=%d)\n",
31313af08d82Slm66018 	    vdcp->local_dring_backup_len, vdcp->local_dring_backup_tail);
31323af08d82Slm66018 
31333af08d82Slm66018 	/*
31343af08d82Slm66018 	 * Walk the backup copy of the local descriptor ring and
31353af08d82Slm66018 	 * resubmit all the outstanding transactions.
31363af08d82Slm66018 	 */
31373af08d82Slm66018 	b_idx = vdcp->local_dring_backup_tail;
31383af08d82Slm66018 	for (count = 0; count < vdcp->local_dring_backup_len; count++) {
31393af08d82Slm66018 
31403af08d82Slm66018 		curr_ldep = &(vdcp->local_dring_backup[b_idx]);
31413af08d82Slm66018 
31423af08d82Slm66018 		/* only resubmit oustanding transactions */
31433af08d82Slm66018 		if (!curr_ldep->is_free) {
31443af08d82Slm66018 
31453af08d82Slm66018 			DMSG(vdcp, 1, "resubmitting entry idx=%x\n", b_idx);
31463af08d82Slm66018 			mutex_enter(&vdcp->lock);
31473af08d82Slm66018 			rv = vdc_populate_descriptor(vdcp, curr_ldep->operation,
31483af08d82Slm66018 			    curr_ldep->addr, curr_ldep->nbytes,
31493af08d82Slm66018 			    curr_ldep->slice, curr_ldep->offset,
31503af08d82Slm66018 			    curr_ldep->cb_type, curr_ldep->cb_arg,
31513af08d82Slm66018 			    curr_ldep->dir);
31523af08d82Slm66018 			mutex_exit(&vdcp->lock);
31533af08d82Slm66018 			if (rv) {
31543af08d82Slm66018 				DMSG(vdcp, 1, "[%d] cannot resubmit entry %d\n",
31553af08d82Slm66018 				    vdcp->instance, b_idx);
31563af08d82Slm66018 				return (rv);
31573af08d82Slm66018 			}
31583af08d82Slm66018 
31593af08d82Slm66018 			/* Wait for the response message. */
31603af08d82Slm66018 			DMSG(vdcp, 1, "waiting for response to idx=%x\n",
31613af08d82Slm66018 			    b_idx);
31623af08d82Slm66018 			status = vdc_wait_for_response(vdcp, &vio_msg);
31633af08d82Slm66018 			if (status) {
31643af08d82Slm66018 				DMSG(vdcp, 1, "[%d] wait_for_response "
31653af08d82Slm66018 				    "returned err=%d\n", vdcp->instance,
31663af08d82Slm66018 				    status);
31673af08d82Slm66018 				return (status);
31683af08d82Slm66018 			}
31693af08d82Slm66018 
31703af08d82Slm66018 			DMSG(vdcp, 1, "processing msg for idx=%x\n", b_idx);
31713af08d82Slm66018 			status = vdc_process_data_msg(vdcp, &vio_msg);
31723af08d82Slm66018 			if (status) {
31733af08d82Slm66018 				DMSG(vdcp, 1, "[%d] process_data_msg "
31743af08d82Slm66018 				    "returned err=%d\n", vdcp->instance,
31753af08d82Slm66018 				    status);
31763af08d82Slm66018 				return (status);
31773af08d82Slm66018 			}
31783af08d82Slm66018 		}
31793af08d82Slm66018 
31803af08d82Slm66018 		/* get the next element to submit */
31813af08d82Slm66018 		if (++b_idx >= vdcp->local_dring_backup_len)
31823af08d82Slm66018 			b_idx = 0;
31833af08d82Slm66018 	}
31843af08d82Slm66018 
31853af08d82Slm66018 	/* all done - now clear up pending dring copy */
31863af08d82Slm66018 	dring_size = vdcp->local_dring_backup_len *
31873af08d82Slm66018 		sizeof (vdcp->local_dring_backup[0]);
31883af08d82Slm66018 
31893af08d82Slm66018 	(void) kmem_free(vdcp->local_dring_backup, dring_size);
31903af08d82Slm66018 
31913af08d82Slm66018 	vdcp->local_dring_backup = NULL;
31923af08d82Slm66018 
31933af08d82Slm66018 	return (0);
31943af08d82Slm66018 }
31953af08d82Slm66018 
31963af08d82Slm66018 /*
31973af08d82Slm66018  * Function:
31983af08d82Slm66018  *	vdc_backup_local_dring()
31993af08d82Slm66018  *
32003af08d82Slm66018  * Description:
32013af08d82Slm66018  *	Backup the current dring in the event of a reset. The Dring
32023af08d82Slm66018  *	transactions will be resubmitted to the server when the
32033af08d82Slm66018  *	connection is restored.
32043af08d82Slm66018  *
32053af08d82Slm66018  * Arguments:
32063af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
32073af08d82Slm66018  *
32083af08d82Slm66018  * Return Code:
32093af08d82Slm66018  *	NONE
32103af08d82Slm66018  */
32113af08d82Slm66018 static void
32123af08d82Slm66018 vdc_backup_local_dring(vdc_t *vdcp)
32133af08d82Slm66018 {
32143af08d82Slm66018 	int dring_size;
32153af08d82Slm66018 
32163af08d82Slm66018 	ASSERT(vdcp->state == VDC_STATE_RESETTING);
32173af08d82Slm66018 
32183af08d82Slm66018 	/*
32193af08d82Slm66018 	 * If the backup dring is stil around, it means
32203af08d82Slm66018 	 * that the last restore did not complete. However,
32213af08d82Slm66018 	 * since we never got back into the running state,
32223af08d82Slm66018 	 * the backup copy we have is still valid.
32233af08d82Slm66018 	 */
32243af08d82Slm66018 	if (vdcp->local_dring_backup != NULL) {
32253af08d82Slm66018 		DMSG(vdcp, 1, "reusing local descriptor ring backup "
32263af08d82Slm66018 		    "(len=%d, tail=%d)\n", vdcp->local_dring_backup_len,
32273af08d82Slm66018 		    vdcp->local_dring_backup_tail);
32283af08d82Slm66018 		return;
32293af08d82Slm66018 	}
32303af08d82Slm66018 
32313af08d82Slm66018 	DMSG(vdcp, 1, "backing up the local descriptor ring (len=%d, "
32323af08d82Slm66018 	    "tail=%d)\n", vdcp->dring_len, vdcp->dring_curr_idx);
32333af08d82Slm66018 
32343af08d82Slm66018 	dring_size = vdcp->dring_len * sizeof (vdcp->local_dring[0]);
32353af08d82Slm66018 
32363af08d82Slm66018 	vdcp->local_dring_backup = kmem_alloc(dring_size, KM_SLEEP);
32373af08d82Slm66018 	bcopy(vdcp->local_dring, vdcp->local_dring_backup, dring_size);
32383af08d82Slm66018 
32393af08d82Slm66018 	vdcp->local_dring_backup_tail = vdcp->dring_curr_idx;
32403af08d82Slm66018 	vdcp->local_dring_backup_len = vdcp->dring_len;
32413af08d82Slm66018 }
32423af08d82Slm66018 
32431ae08745Sheppo /* -------------------------------------------------------------------------- */
32441ae08745Sheppo 
32451ae08745Sheppo /*
32461ae08745Sheppo  * The following functions process the incoming messages from vds
32471ae08745Sheppo  */
32481ae08745Sheppo 
32490a55fbb7Slm66018 /*
32500a55fbb7Slm66018  * Function:
32510a55fbb7Slm66018  *      vdc_process_msg_thread()
32520a55fbb7Slm66018  *
32530a55fbb7Slm66018  * Description:
32540a55fbb7Slm66018  *
32553af08d82Slm66018  *	Main VDC message processing thread. Each vDisk instance
32563af08d82Slm66018  * 	consists of a copy of this thread. This thread triggers
32573af08d82Slm66018  * 	all the handshakes and data exchange with the server. It
32583af08d82Slm66018  * 	also handles all channel resets
32593af08d82Slm66018  *
32600a55fbb7Slm66018  * Arguments:
32610a55fbb7Slm66018  *      vdc     - soft state pointer for this instance of the device driver.
32620a55fbb7Slm66018  *
32630a55fbb7Slm66018  * Return Code:
32640a55fbb7Slm66018  *      None
32650a55fbb7Slm66018  */
32661ae08745Sheppo static void
32673af08d82Slm66018 vdc_process_msg_thread(vdc_t *vdcp)
32681ae08745Sheppo {
32691ae08745Sheppo 	int	status;
32701ae08745Sheppo 
32713af08d82Slm66018 	mutex_enter(&vdcp->lock);
32721ae08745Sheppo 
32731ae08745Sheppo 	for (;;) {
32741ae08745Sheppo 
32753af08d82Slm66018 #define	Q(_s)	(vdcp->state == _s) ? #_s :
32763af08d82Slm66018 		DMSG(vdcp, 3, "state = %d (%s)\n", vdcp->state,
32773af08d82Slm66018 		Q(VDC_STATE_INIT)
32783af08d82Slm66018 		Q(VDC_STATE_INIT_WAITING)
32793af08d82Slm66018 		Q(VDC_STATE_NEGOTIATE)
32803af08d82Slm66018 		Q(VDC_STATE_HANDLE_PENDING)
32813af08d82Slm66018 		Q(VDC_STATE_RUNNING)
32823af08d82Slm66018 		Q(VDC_STATE_RESETTING)
32833af08d82Slm66018 		Q(VDC_STATE_DETACH)
32843af08d82Slm66018 		"UNKNOWN");
32851ae08745Sheppo 
32863af08d82Slm66018 		switch (vdcp->state) {
32873af08d82Slm66018 		case VDC_STATE_INIT:
32883af08d82Slm66018 
32893af08d82Slm66018 			/* Check if have re-initializing repeatedly */
32903c96341aSnarayan 			if (vdcp->hshake_cnt++ > vdc_hshake_retries) {
32913c96341aSnarayan 				cmn_err(CE_NOTE, "[%d] disk access failed.\n",
32923c96341aSnarayan 				    vdcp->instance);
32933af08d82Slm66018 				vdcp->state = VDC_STATE_DETACH;
32943af08d82Slm66018 				break;
32953af08d82Slm66018 			}
32963af08d82Slm66018 
32973af08d82Slm66018 			/* Bring up connection with vds via LDC */
32983af08d82Slm66018 			status = vdc_start_ldc_connection(vdcp);
32993af08d82Slm66018 			switch (status) {
33003af08d82Slm66018 			case EINVAL:
33013af08d82Slm66018 				DMSG(vdcp, 0, "[%d] Could not start LDC",
33023af08d82Slm66018 				    vdcp->instance);
33033af08d82Slm66018 				vdcp->state = VDC_STATE_DETACH;
33043af08d82Slm66018 				break;
33053af08d82Slm66018 			case 0:
33063af08d82Slm66018 				vdcp->state = VDC_STATE_INIT_WAITING;
33073af08d82Slm66018 				break;
33083af08d82Slm66018 			default:
33093af08d82Slm66018 				vdcp->state = VDC_STATE_INIT_WAITING;
33103af08d82Slm66018 				break;
33113af08d82Slm66018 			}
33123af08d82Slm66018 			break;
33133af08d82Slm66018 
33143af08d82Slm66018 		case VDC_STATE_INIT_WAITING:
33153af08d82Slm66018 
33163af08d82Slm66018 			/*
33173af08d82Slm66018 			 * Let the callback event move us on
33183af08d82Slm66018 			 * when channel is open to server
33193af08d82Slm66018 			 */
33203af08d82Slm66018 			while (vdcp->ldc_state != LDC_UP) {
33213af08d82Slm66018 				cv_wait(&vdcp->initwait_cv, &vdcp->lock);
33223af08d82Slm66018 				if (vdcp->state != VDC_STATE_INIT_WAITING) {
33233af08d82Slm66018 					DMSG(vdcp, 0,
33243af08d82Slm66018 				"state moved to %d out from under us...\n",
33253af08d82Slm66018 					    vdcp->state);
33263af08d82Slm66018 
33273af08d82Slm66018 					break;
33283af08d82Slm66018 				}
33293af08d82Slm66018 			}
33303af08d82Slm66018 			if (vdcp->state == VDC_STATE_INIT_WAITING &&
33313af08d82Slm66018 			    vdcp->ldc_state == LDC_UP) {
33323af08d82Slm66018 				vdcp->state = VDC_STATE_NEGOTIATE;
33333af08d82Slm66018 			}
33343af08d82Slm66018 			break;
33353af08d82Slm66018 
33363af08d82Slm66018 		case VDC_STATE_NEGOTIATE:
33373af08d82Slm66018 			switch (status = vdc_ver_negotiation(vdcp)) {
33383af08d82Slm66018 			case 0:
33393af08d82Slm66018 				break;
33403af08d82Slm66018 			default:
33413af08d82Slm66018 				DMSG(vdcp, 0, "ver negotiate failed (%d)..\n",
33423af08d82Slm66018 				    status);
33433af08d82Slm66018 				goto reset;
33443af08d82Slm66018 			}
33453af08d82Slm66018 
33463af08d82Slm66018 			switch (status = vdc_attr_negotiation(vdcp)) {
33473af08d82Slm66018 			case 0:
33483af08d82Slm66018 				break;
33493af08d82Slm66018 			default:
33503af08d82Slm66018 				DMSG(vdcp, 0, "attr negotiate failed (%d)..\n",
33513af08d82Slm66018 				    status);
33523af08d82Slm66018 				goto reset;
33533af08d82Slm66018 			}
33543af08d82Slm66018 
33553af08d82Slm66018 			switch (status = vdc_dring_negotiation(vdcp)) {
33563af08d82Slm66018 			case 0:
33573af08d82Slm66018 				break;
33583af08d82Slm66018 			default:
33593af08d82Slm66018 				DMSG(vdcp, 0, "dring negotiate failed (%d)..\n",
33603af08d82Slm66018 				    status);
33613af08d82Slm66018 				goto reset;
33623af08d82Slm66018 			}
33633af08d82Slm66018 
33643af08d82Slm66018 			switch (status = vdc_rdx_exchange(vdcp)) {
33653af08d82Slm66018 			case 0:
33663af08d82Slm66018 				vdcp->state = VDC_STATE_HANDLE_PENDING;
33673af08d82Slm66018 				goto done;
33683af08d82Slm66018 			default:
33693af08d82Slm66018 				DMSG(vdcp, 0, "RDX xchg failed ..(%d)\n",
33703af08d82Slm66018 				    status);
33713af08d82Slm66018 				goto reset;
33723af08d82Slm66018 			}
33733af08d82Slm66018 reset:
33743af08d82Slm66018 			DMSG(vdcp, 0, "negotiation failed: resetting (%d)\n",
33753af08d82Slm66018 			    status);
33763af08d82Slm66018 			vdcp->state = VDC_STATE_RESETTING;
33773af08d82Slm66018 done:
33783af08d82Slm66018 			DMSG(vdcp, 0, "negotiation complete (state=0x%x)...\n",
33793af08d82Slm66018 			    vdcp->state);
33803af08d82Slm66018 			break;
33813af08d82Slm66018 
33823af08d82Slm66018 		case VDC_STATE_HANDLE_PENDING:
33833af08d82Slm66018 
33843af08d82Slm66018 			mutex_exit(&vdcp->lock);
33853af08d82Slm66018 			status = vdc_resubmit_backup_dring(vdcp);
33863af08d82Slm66018 			mutex_enter(&vdcp->lock);
33873af08d82Slm66018 
33883af08d82Slm66018 			if (status)
33893af08d82Slm66018 				vdcp->state = VDC_STATE_RESETTING;
33903af08d82Slm66018 			else
33913af08d82Slm66018 				vdcp->state = VDC_STATE_RUNNING;
33923af08d82Slm66018 
33933af08d82Slm66018 			break;
33943af08d82Slm66018 
33953af08d82Slm66018 		/* enter running state */
33963af08d82Slm66018 		case VDC_STATE_RUNNING:
33973af08d82Slm66018 			/*
33983af08d82Slm66018 			 * Signal anyone waiting for the connection
33993af08d82Slm66018 			 * to come on line.
34003af08d82Slm66018 			 */
34013af08d82Slm66018 			vdcp->hshake_cnt = 0;
34023af08d82Slm66018 			cv_broadcast(&vdcp->running_cv);
34033af08d82Slm66018 			mutex_exit(&vdcp->lock);
34043af08d82Slm66018 
34053af08d82Slm66018 			for (;;) {
34063af08d82Slm66018 				vio_msg_t msg;
34073af08d82Slm66018 				status = vdc_wait_for_response(vdcp, &msg);
34083af08d82Slm66018 				if (status) break;
34093af08d82Slm66018 
34103af08d82Slm66018 				DMSG(vdcp, 1, "[%d] new pkt(s) available\n",
34113af08d82Slm66018 					vdcp->instance);
34123af08d82Slm66018 				status = vdc_process_data_msg(vdcp, &msg);
34131ae08745Sheppo 				if (status) {
34143af08d82Slm66018 					DMSG(vdcp, 1, "[%d] process_data_msg "
34153af08d82Slm66018 					    "returned err=%d\n", vdcp->instance,
34163af08d82Slm66018 					    status);
34171ae08745Sheppo 					break;
34181ae08745Sheppo 				}
34191ae08745Sheppo 
34203af08d82Slm66018 			}
3421e1ebb9ecSlm66018 
34223af08d82Slm66018 			mutex_enter(&vdcp->lock);
34233af08d82Slm66018 
34243af08d82Slm66018 			vdcp->state = VDC_STATE_RESETTING;
3425*690555a1Sachartre 			vdcp->self_reset = B_TRUE;
34263af08d82Slm66018 			break;
34273af08d82Slm66018 
34283af08d82Slm66018 		case VDC_STATE_RESETTING:
34293af08d82Slm66018 			DMSG(vdcp, 0, "Initiating channel reset "
34303af08d82Slm66018 			    "(pending = %d)\n", (int)vdcp->threads_pending);
34313af08d82Slm66018 
34323af08d82Slm66018 			if (vdcp->self_reset) {
34333af08d82Slm66018 				DMSG(vdcp, 0,
34343af08d82Slm66018 				    "[%d] calling stop_ldc_connection.\n",
34353af08d82Slm66018 				    vdcp->instance);
34363af08d82Slm66018 				status = vdc_stop_ldc_connection(vdcp);
34373af08d82Slm66018 				vdcp->self_reset = B_FALSE;
34381ae08745Sheppo 			}
34391ae08745Sheppo 
34401ae08745Sheppo 			/*
34413af08d82Slm66018 			 * Wait for all threads currently waiting
34423af08d82Slm66018 			 * for a free dring entry to use.
34431ae08745Sheppo 			 */
34443af08d82Slm66018 			while (vdcp->threads_pending) {
34453af08d82Slm66018 				cv_broadcast(&vdcp->membind_cv);
34463af08d82Slm66018 				cv_broadcast(&vdcp->dring_free_cv);
34473af08d82Slm66018 				mutex_exit(&vdcp->lock);
34483af08d82Slm66018 				/* let them wake up */
34493af08d82Slm66018 				drv_usecwait(vdc_min_timeout_ldc);
34503af08d82Slm66018 				mutex_enter(&vdcp->lock);
34511ae08745Sheppo 			}
34521ae08745Sheppo 
34533af08d82Slm66018 			ASSERT(vdcp->threads_pending == 0);
34541ae08745Sheppo 
34553af08d82Slm66018 			/* Sanity check that no thread is receiving */
34563af08d82Slm66018 			ASSERT(vdcp->read_state != VDC_READ_WAITING);
34570a55fbb7Slm66018 
34583af08d82Slm66018 			vdcp->read_state = VDC_READ_IDLE;
34593af08d82Slm66018 
34603af08d82Slm66018 			vdc_backup_local_dring(vdcp);
34613af08d82Slm66018 
34623af08d82Slm66018 			/* cleanup the old d-ring */
34633af08d82Slm66018 			vdc_destroy_descriptor_ring(vdcp);
34643af08d82Slm66018 
34653af08d82Slm66018 			/* go and start again */
34663af08d82Slm66018 			vdcp->state = VDC_STATE_INIT;
34673af08d82Slm66018 
34680a55fbb7Slm66018 			break;
34690a55fbb7Slm66018 
34703af08d82Slm66018 		case VDC_STATE_DETACH:
34713af08d82Slm66018 			DMSG(vdcp, 0, "[%d] Reset thread exit cleanup ..\n",
34723af08d82Slm66018 			    vdcp->instance);
34733af08d82Slm66018 
34743c96341aSnarayan 			/*
34753c96341aSnarayan 			 * Signal anyone waiting for connection
34763c96341aSnarayan 			 * to come online
34773c96341aSnarayan 			 */
34783c96341aSnarayan 			cv_broadcast(&vdcp->running_cv);
34793c96341aSnarayan 
34803af08d82Slm66018 			while (vdcp->sync_op_pending) {
34813af08d82Slm66018 				cv_signal(&vdcp->sync_pending_cv);
34823af08d82Slm66018 				cv_signal(&vdcp->sync_blocked_cv);
34833af08d82Slm66018 				mutex_exit(&vdcp->lock);
34843af08d82Slm66018 				drv_usecwait(vdc_min_timeout_ldc);
34853af08d82Slm66018 				mutex_enter(&vdcp->lock);
34860a55fbb7Slm66018 			}
34871ae08745Sheppo 
34883af08d82Slm66018 			mutex_exit(&vdcp->lock);
34893af08d82Slm66018 
34903af08d82Slm66018 			DMSG(vdcp, 0, "[%d] Msg processing thread exiting ..\n",
34913af08d82Slm66018 				vdcp->instance);
34923af08d82Slm66018 			thread_exit();
34933af08d82Slm66018 			break;
34943af08d82Slm66018 		}
34953af08d82Slm66018 	}
34960a55fbb7Slm66018 }
34970a55fbb7Slm66018 
34980a55fbb7Slm66018 
34990a55fbb7Slm66018 /*
35000a55fbb7Slm66018  * Function:
35010a55fbb7Slm66018  *	vdc_process_data_msg()
35020a55fbb7Slm66018  *
35030a55fbb7Slm66018  * Description:
35040a55fbb7Slm66018  *	This function is called by the message processing thread each time
35050a55fbb7Slm66018  *	a message with a msgtype of VIO_TYPE_DATA is received. It will either
35060a55fbb7Slm66018  *	be an ACK or NACK from vds[1] which vdc handles as follows.
35070a55fbb7Slm66018  *		ACK	- wake up the waiting thread
35080a55fbb7Slm66018  *		NACK	- resend any messages necessary
35090a55fbb7Slm66018  *
35100a55fbb7Slm66018  *	[1] Although the message format allows it, vds should not send a
35110a55fbb7Slm66018  *	    VIO_SUBTYPE_INFO message to vdc asking it to read data; if for
35120a55fbb7Slm66018  *	    some bizarre reason it does, vdc will reset the connection.
35130a55fbb7Slm66018  *
35140a55fbb7Slm66018  * Arguments:
35150a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
35160a55fbb7Slm66018  *	msg	- the LDC message sent by vds
35170a55fbb7Slm66018  *
35180a55fbb7Slm66018  * Return Code:
35190a55fbb7Slm66018  *	0	- Success.
35200a55fbb7Slm66018  *	> 0	- error value returned by LDC
35210a55fbb7Slm66018  */
35220a55fbb7Slm66018 static int
35233af08d82Slm66018 vdc_process_data_msg(vdc_t *vdcp, vio_msg_t *msg)
35240a55fbb7Slm66018 {
35250a55fbb7Slm66018 	int			status = 0;
35263af08d82Slm66018 	vio_dring_msg_t		*dring_msg;
3527d10e4ef2Snarayan 	vdc_local_desc_t	*ldep = NULL;
35283af08d82Slm66018 	int			start, end;
35293af08d82Slm66018 	int			idx;
35300a55fbb7Slm66018 
35313af08d82Slm66018 	dring_msg = (vio_dring_msg_t *)msg;
35320a55fbb7Slm66018 
35333af08d82Slm66018 	ASSERT(msg->tag.vio_msgtype == VIO_TYPE_DATA);
35343af08d82Slm66018 	ASSERT(vdcp != NULL);
35353af08d82Slm66018 
35363af08d82Slm66018 	mutex_enter(&vdcp->lock);
35370a55fbb7Slm66018 
35380a55fbb7Slm66018 	/*
35390a55fbb7Slm66018 	 * Check to see if the message has bogus data
35400a55fbb7Slm66018 	 */
3541e1ebb9ecSlm66018 	idx = start = dring_msg->start_idx;
35420a55fbb7Slm66018 	end = dring_msg->end_idx;
35433af08d82Slm66018 	if ((start >= vdcp->dring_len) ||
35443af08d82Slm66018 	    (end >= vdcp->dring_len) || (end < -1)) {
35453af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Bogus ACK data : start %d, end %d\n",
35463af08d82Slm66018 			vdcp->instance, start, end);
35473af08d82Slm66018 		mutex_exit(&vdcp->lock);
3548e1ebb9ecSlm66018 		return (EINVAL);
35490a55fbb7Slm66018 	}
35500a55fbb7Slm66018 
35510a55fbb7Slm66018 	/*
35520a55fbb7Slm66018 	 * Verify that the sequence number is what vdc expects.
35530a55fbb7Slm66018 	 */
35543af08d82Slm66018 	switch (vdc_verify_seq_num(vdcp, dring_msg)) {
3555e1ebb9ecSlm66018 	case VDC_SEQ_NUM_TODO:
3556e1ebb9ecSlm66018 		break;	/* keep processing this message */
3557e1ebb9ecSlm66018 	case VDC_SEQ_NUM_SKIP:
35583af08d82Slm66018 		mutex_exit(&vdcp->lock);
3559e1ebb9ecSlm66018 		return (0);
3560e1ebb9ecSlm66018 	case VDC_SEQ_NUM_INVALID:
35613af08d82Slm66018 		mutex_exit(&vdcp->lock);
35623af08d82Slm66018 		DMSG(vdcp, 0, "[%d] invalid seqno\n", vdcp->instance);
35630a55fbb7Slm66018 		return (ENXIO);
35640a55fbb7Slm66018 	}
35650a55fbb7Slm66018 
35663af08d82Slm66018 	if (msg->tag.vio_subtype == VIO_SUBTYPE_NACK) {
35673af08d82Slm66018 		DMSG(vdcp, 0, "[%d] DATA NACK\n", vdcp->instance);
3568e1ebb9ecSlm66018 		VDC_DUMP_DRING_MSG(dring_msg);
35693af08d82Slm66018 		mutex_exit(&vdcp->lock);
3570e1ebb9ecSlm66018 		return (EIO);
35710a55fbb7Slm66018 
35723af08d82Slm66018 	} else if (msg->tag.vio_subtype == VIO_SUBTYPE_INFO) {
35733af08d82Slm66018 		mutex_exit(&vdcp->lock);
3574e1ebb9ecSlm66018 		return (EPROTO);
3575e1ebb9ecSlm66018 	}
3576e1ebb9ecSlm66018 
35773af08d82Slm66018 	DTRACE_IO2(recv, vio_dring_msg_t, dring_msg, vdc_t *, vdcp);
35783af08d82Slm66018 	DMSG(vdcp, 1, ": start %d end %d\n", start, end);
35793af08d82Slm66018 	ASSERT(start == end);
35803af08d82Slm66018 
35813af08d82Slm66018 	ldep = &vdcp->local_dring[idx];
35823af08d82Slm66018 
35833af08d82Slm66018 	DMSG(vdcp, 1, ": state 0x%x - cb_type 0x%x\n",
35843af08d82Slm66018 		ldep->dep->hdr.dstate, ldep->cb_type);
35853af08d82Slm66018 
3586e1ebb9ecSlm66018 	if (ldep->dep->hdr.dstate == VIO_DESC_DONE) {
35873af08d82Slm66018 		struct buf *bufp;
3588e1ebb9ecSlm66018 
35893af08d82Slm66018 		switch (ldep->cb_type) {
35903af08d82Slm66018 		case CB_SYNC:
35913af08d82Slm66018 			ASSERT(vdcp->sync_op_pending);
3592d10e4ef2Snarayan 
35933af08d82Slm66018 			status = vdc_depopulate_descriptor(vdcp, idx);
35943af08d82Slm66018 			vdcp->sync_op_status = status;
35953af08d82Slm66018 			vdcp->sync_op_pending = B_FALSE;
35963af08d82Slm66018 			cv_signal(&vdcp->sync_pending_cv);
35973af08d82Slm66018 			break;
35984bac2208Snarayan 
35993af08d82Slm66018 		case CB_STRATEGY:
36003af08d82Slm66018 			bufp = ldep->cb_arg;
36013af08d82Slm66018 			ASSERT(bufp != NULL);
36023c96341aSnarayan 			bufp->b_resid =
36033c96341aSnarayan 				bufp->b_bcount - ldep->dep->payload.nbytes;
36043af08d82Slm66018 			status = ldep->dep->payload.status; /* Future:ntoh */
36053af08d82Slm66018 			if (status != 0) {
36063af08d82Slm66018 				DMSG(vdcp, 1, "strategy status=%d\n", status);
36073af08d82Slm66018 				bioerror(bufp, status);
3608d10e4ef2Snarayan 			}
36093af08d82Slm66018 			status = vdc_depopulate_descriptor(vdcp, idx);
36103af08d82Slm66018 			biodone(bufp);
36113c96341aSnarayan 
36123c96341aSnarayan 			DMSG(vdcp, 1,
36133c96341aSnarayan 			    "strategy complete req=%ld bytes resp=%ld bytes\n",
36143c96341aSnarayan 			    bufp->b_bcount, ldep->dep->payload.nbytes);
36153af08d82Slm66018 			break;
36163af08d82Slm66018 
36173af08d82Slm66018 		default:
36183af08d82Slm66018 			ASSERT(0);
36190a55fbb7Slm66018 		}
36203af08d82Slm66018 	}
36213af08d82Slm66018 
36223af08d82Slm66018 	/* let the arrival signal propogate */
36233af08d82Slm66018 	mutex_exit(&vdcp->lock);
36240a55fbb7Slm66018 
3625e1ebb9ecSlm66018 	/* probe gives the count of how many entries were processed */
36263af08d82Slm66018 	DTRACE_IO2(processed, int, 1, vdc_t *, vdcp);
36270a55fbb7Slm66018 
36283af08d82Slm66018 	return (0);
36290a55fbb7Slm66018 }
36300a55fbb7Slm66018 
36310a55fbb7Slm66018 /*
36320a55fbb7Slm66018  * Function:
36330a55fbb7Slm66018  *	vdc_process_err_msg()
36340a55fbb7Slm66018  *
36350a55fbb7Slm66018  * NOTE: No error messages are used as part of the vDisk protocol
36360a55fbb7Slm66018  */
36370a55fbb7Slm66018 static int
36380a55fbb7Slm66018 vdc_process_err_msg(vdc_t *vdc, vio_msg_t msg)
36390a55fbb7Slm66018 {
36400a55fbb7Slm66018 	_NOTE(ARGUNUSED(vdc))
36410a55fbb7Slm66018 	_NOTE(ARGUNUSED(msg))
36420a55fbb7Slm66018 
36430a55fbb7Slm66018 	ASSERT(msg.tag.vio_msgtype == VIO_TYPE_ERR);
36443af08d82Slm66018 	DMSG(vdc, 1, "[%d] Got an ERR msg", vdc->instance);
36450a55fbb7Slm66018 
36460a55fbb7Slm66018 	return (ENOTSUP);
36470a55fbb7Slm66018 }
36480a55fbb7Slm66018 
36490a55fbb7Slm66018 /*
36500a55fbb7Slm66018  * Function:
36510a55fbb7Slm66018  *	vdc_handle_ver_msg()
36520a55fbb7Slm66018  *
36530a55fbb7Slm66018  * Description:
36540a55fbb7Slm66018  *
36550a55fbb7Slm66018  * Arguments:
36560a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
36570a55fbb7Slm66018  *	ver_msg	- LDC message sent by vDisk server
36580a55fbb7Slm66018  *
36590a55fbb7Slm66018  * Return Code:
36600a55fbb7Slm66018  *	0	- Success
36610a55fbb7Slm66018  */
36620a55fbb7Slm66018 static int
36630a55fbb7Slm66018 vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg)
36640a55fbb7Slm66018 {
36650a55fbb7Slm66018 	int status = 0;
36660a55fbb7Slm66018 
36670a55fbb7Slm66018 	ASSERT(vdc != NULL);
36680a55fbb7Slm66018 	ASSERT(mutex_owned(&vdc->lock));
36690a55fbb7Slm66018 
36700a55fbb7Slm66018 	if (ver_msg->tag.vio_subtype_env != VIO_VER_INFO) {
36710a55fbb7Slm66018 		return (EPROTO);
36720a55fbb7Slm66018 	}
36730a55fbb7Slm66018 
36740a55fbb7Slm66018 	if (ver_msg->dev_class != VDEV_DISK_SERVER) {
36750a55fbb7Slm66018 		return (EINVAL);
36760a55fbb7Slm66018 	}
36770a55fbb7Slm66018 
36780a55fbb7Slm66018 	switch (ver_msg->tag.vio_subtype) {
36790a55fbb7Slm66018 	case VIO_SUBTYPE_ACK:
36800a55fbb7Slm66018 		/*
36810a55fbb7Slm66018 		 * We check to see if the version returned is indeed supported
36820a55fbb7Slm66018 		 * (The server may have also adjusted the minor number downwards
36830a55fbb7Slm66018 		 * and if so 'ver_msg' will contain the actual version agreed)
36840a55fbb7Slm66018 		 */
36850a55fbb7Slm66018 		if (vdc_is_supported_version(ver_msg)) {
36860a55fbb7Slm66018 			vdc->ver.major = ver_msg->ver_major;
36870a55fbb7Slm66018 			vdc->ver.minor = ver_msg->ver_minor;
36880a55fbb7Slm66018 			ASSERT(vdc->ver.major > 0);
36890a55fbb7Slm66018 		} else {
36900a55fbb7Slm66018 			status = EPROTO;
36910a55fbb7Slm66018 		}
36920a55fbb7Slm66018 		break;
36930a55fbb7Slm66018 
36940a55fbb7Slm66018 	case VIO_SUBTYPE_NACK:
36950a55fbb7Slm66018 		/*
36960a55fbb7Slm66018 		 * call vdc_is_supported_version() which will return the next
36970a55fbb7Slm66018 		 * supported version (if any) in 'ver_msg'
36980a55fbb7Slm66018 		 */
36990a55fbb7Slm66018 		(void) vdc_is_supported_version(ver_msg);
37000a55fbb7Slm66018 		if (ver_msg->ver_major > 0) {
37010a55fbb7Slm66018 			size_t len = sizeof (*ver_msg);
37020a55fbb7Slm66018 
37030a55fbb7Slm66018 			ASSERT(vdc->ver.major > 0);
37040a55fbb7Slm66018 
37050a55fbb7Slm66018 			/* reset the necessary fields and resend */
37060a55fbb7Slm66018 			ver_msg->tag.vio_subtype = VIO_SUBTYPE_INFO;
37070a55fbb7Slm66018 			ver_msg->dev_class = VDEV_DISK;
37080a55fbb7Slm66018 
37090a55fbb7Slm66018 			status = vdc_send(vdc, (caddr_t)ver_msg, &len);
37103af08d82Slm66018 			DMSG(vdc, 0, "[%d] Resend VER info (LDC status = %d)\n",
37110a55fbb7Slm66018 					vdc->instance, status);
37120a55fbb7Slm66018 			if (len != sizeof (*ver_msg))
37130a55fbb7Slm66018 				status = EBADMSG;
37140a55fbb7Slm66018 		} else {
37153af08d82Slm66018 			DMSG(vdc, 0, "[%d] No common version with "
37160a55fbb7Slm66018 					"vDisk server", vdc->instance);
37170a55fbb7Slm66018 			status = ENOTSUP;
37180a55fbb7Slm66018 		}
37190a55fbb7Slm66018 
37200a55fbb7Slm66018 		break;
37211ae08745Sheppo 	case VIO_SUBTYPE_INFO:
37221ae08745Sheppo 		/*
37231ae08745Sheppo 		 * Handle the case where vds starts handshake
37241ae08745Sheppo 		 * (for now only vdc is the instigatior)
37251ae08745Sheppo 		 */
37261ae08745Sheppo 		status = ENOTSUP;
37271ae08745Sheppo 		break;
37281ae08745Sheppo 
37291ae08745Sheppo 	default:
37300a55fbb7Slm66018 		status = EINVAL;
37311ae08745Sheppo 		break;
37321ae08745Sheppo 	}
37331ae08745Sheppo 
37340a55fbb7Slm66018 	return (status);
37350a55fbb7Slm66018 }
37360a55fbb7Slm66018 
37370a55fbb7Slm66018 /*
37380a55fbb7Slm66018  * Function:
37390a55fbb7Slm66018  *	vdc_handle_attr_msg()
37400a55fbb7Slm66018  *
37410a55fbb7Slm66018  * Description:
37420a55fbb7Slm66018  *
37430a55fbb7Slm66018  * Arguments:
37440a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
37450a55fbb7Slm66018  *	attr_msg	- LDC message sent by vDisk server
37460a55fbb7Slm66018  *
37470a55fbb7Slm66018  * Return Code:
37480a55fbb7Slm66018  *	0	- Success
37490a55fbb7Slm66018  */
37500a55fbb7Slm66018 static int
37510a55fbb7Slm66018 vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg)
37520a55fbb7Slm66018 {
37530a55fbb7Slm66018 	int status = 0;
37540a55fbb7Slm66018 
37550a55fbb7Slm66018 	ASSERT(vdc != NULL);
37560a55fbb7Slm66018 	ASSERT(mutex_owned(&vdc->lock));
37570a55fbb7Slm66018 
37580a55fbb7Slm66018 	if (attr_msg->tag.vio_subtype_env != VIO_ATTR_INFO) {
37590a55fbb7Slm66018 		return (EPROTO);
37600a55fbb7Slm66018 	}
37610a55fbb7Slm66018 
37620a55fbb7Slm66018 	switch (attr_msg->tag.vio_subtype) {
37631ae08745Sheppo 	case VIO_SUBTYPE_ACK:
37641ae08745Sheppo 		/*
37651ae08745Sheppo 		 * We now verify the attributes sent by vds.
37661ae08745Sheppo 		 */
37671ae08745Sheppo 		vdc->vdisk_size = attr_msg->vdisk_size;
37681ae08745Sheppo 		vdc->vdisk_type = attr_msg->vdisk_type;
37691ae08745Sheppo 
37703af08d82Slm66018 		DMSG(vdc, 0, "[%d] max_xfer_sz: sent %lx acked %lx\n",
3771e1ebb9ecSlm66018 			vdc->instance, vdc->max_xfer_sz, attr_msg->max_xfer_sz);
37723af08d82Slm66018 		DMSG(vdc, 0, "[%d] vdisk_block_size: sent %lx acked %x\n",
3773e1ebb9ecSlm66018 			vdc->instance, vdc->block_size,
3774e1ebb9ecSlm66018 			attr_msg->vdisk_block_size);
3775e1ebb9ecSlm66018 
37761ae08745Sheppo 		/*
3777e1ebb9ecSlm66018 		 * We don't know at compile time what the vDisk server will
3778e1ebb9ecSlm66018 		 * think are good values but we apply an large (arbitrary)
3779e1ebb9ecSlm66018 		 * upper bound to prevent memory exhaustion in vdc if it was
3780e1ebb9ecSlm66018 		 * allocating a DRing based of huge values sent by the server.
3781e1ebb9ecSlm66018 		 * We probably will never exceed this except if the message
3782e1ebb9ecSlm66018 		 * was garbage.
37831ae08745Sheppo 		 */
3784e1ebb9ecSlm66018 		if ((attr_msg->max_xfer_sz * attr_msg->vdisk_block_size) <=
3785e1ebb9ecSlm66018 				(PAGESIZE * DEV_BSIZE)) {
3786e1ebb9ecSlm66018 			vdc->max_xfer_sz = attr_msg->max_xfer_sz;
3787e1ebb9ecSlm66018 			vdc->block_size = attr_msg->vdisk_block_size;
3788e1ebb9ecSlm66018 		} else {
37893af08d82Slm66018 			DMSG(vdc, 0, "[%d] vds block transfer size too big;"
3790e1ebb9ecSlm66018 				" using max supported by vdc", vdc->instance);
37911ae08745Sheppo 		}
37921ae08745Sheppo 
37931ae08745Sheppo 		if ((attr_msg->xfer_mode != VIO_DRING_MODE) ||
37941ae08745Sheppo 		    (attr_msg->vdisk_size > INT64_MAX) ||
37951ae08745Sheppo 		    (attr_msg->vdisk_type > VD_DISK_TYPE_DISK)) {
37963af08d82Slm66018 			DMSG(vdc, 0, "[%d] Invalid attributes from vds",
3797e1ebb9ecSlm66018 					vdc->instance);
37981ae08745Sheppo 			status = EINVAL;
37991ae08745Sheppo 			break;
38001ae08745Sheppo 		}
38011ae08745Sheppo 
38021ae08745Sheppo 		break;
38031ae08745Sheppo 
38041ae08745Sheppo 	case VIO_SUBTYPE_NACK:
38051ae08745Sheppo 		/*
38061ae08745Sheppo 		 * vds could not handle the attributes we sent so we
38071ae08745Sheppo 		 * stop negotiating.
38081ae08745Sheppo 		 */
38091ae08745Sheppo 		status = EPROTO;
38101ae08745Sheppo 		break;
38111ae08745Sheppo 
38121ae08745Sheppo 	case VIO_SUBTYPE_INFO:
38131ae08745Sheppo 		/*
38141ae08745Sheppo 		 * Handle the case where vds starts the handshake
38151ae08745Sheppo 		 * (for now; vdc is the only supported instigatior)
38161ae08745Sheppo 		 */
38171ae08745Sheppo 		status = ENOTSUP;
38181ae08745Sheppo 		break;
38191ae08745Sheppo 
38201ae08745Sheppo 	default:
38211ae08745Sheppo 		status = ENOTSUP;
38221ae08745Sheppo 		break;
38231ae08745Sheppo 	}
38241ae08745Sheppo 
38250a55fbb7Slm66018 	return (status);
38261ae08745Sheppo }
38271ae08745Sheppo 
38280a55fbb7Slm66018 /*
38290a55fbb7Slm66018  * Function:
38300a55fbb7Slm66018  *	vdc_handle_dring_reg_msg()
38310a55fbb7Slm66018  *
38320a55fbb7Slm66018  * Description:
38330a55fbb7Slm66018  *
38340a55fbb7Slm66018  * Arguments:
38350a55fbb7Slm66018  *	vdc		- soft state pointer for this instance of the driver.
38360a55fbb7Slm66018  *	dring_msg	- LDC message sent by vDisk server
38370a55fbb7Slm66018  *
38380a55fbb7Slm66018  * Return Code:
38390a55fbb7Slm66018  *	0	- Success
38400a55fbb7Slm66018  */
38410a55fbb7Slm66018 static int
38420a55fbb7Slm66018 vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *dring_msg)
38430a55fbb7Slm66018 {
38440a55fbb7Slm66018 	int		status = 0;
38451ae08745Sheppo 
38460a55fbb7Slm66018 	ASSERT(vdc != NULL);
38470a55fbb7Slm66018 	ASSERT(mutex_owned(&vdc->lock));
38480a55fbb7Slm66018 
38490a55fbb7Slm66018 	if (dring_msg->tag.vio_subtype_env != VIO_DRING_REG) {
38500a55fbb7Slm66018 		return (EPROTO);
38510a55fbb7Slm66018 	}
38520a55fbb7Slm66018 
38530a55fbb7Slm66018 	switch (dring_msg->tag.vio_subtype) {
38540a55fbb7Slm66018 	case VIO_SUBTYPE_ACK:
38551ae08745Sheppo 		/* save the received dring_ident */
38561ae08745Sheppo 		vdc->dring_ident = dring_msg->dring_ident;
38573af08d82Slm66018 		DMSG(vdc, 0, "[%d] Received dring ident=0x%lx\n",
3858e1ebb9ecSlm66018 			vdc->instance, vdc->dring_ident);
38591ae08745Sheppo 		break;
38601ae08745Sheppo 
38611ae08745Sheppo 	case VIO_SUBTYPE_NACK:
38621ae08745Sheppo 		/*
38631ae08745Sheppo 		 * vds could not handle the DRing info we sent so we
38641ae08745Sheppo 		 * stop negotiating.
38651ae08745Sheppo 		 */
38663af08d82Slm66018 		DMSG(vdc, 0, "[%d] server could not register DRing\n",
38673af08d82Slm66018 		    vdc->instance);
38681ae08745Sheppo 		status = EPROTO;
38691ae08745Sheppo 		break;
38701ae08745Sheppo 
38711ae08745Sheppo 	case VIO_SUBTYPE_INFO:
38721ae08745Sheppo 		/*
38731ae08745Sheppo 		 * Handle the case where vds starts handshake
38741ae08745Sheppo 		 * (for now only vdc is the instigatior)
38751ae08745Sheppo 		 */
38761ae08745Sheppo 		status = ENOTSUP;
38771ae08745Sheppo 		break;
38781ae08745Sheppo 	default:
38791ae08745Sheppo 		status = ENOTSUP;
38801ae08745Sheppo 	}
38811ae08745Sheppo 
38821ae08745Sheppo 	return (status);
38831ae08745Sheppo }
38841ae08745Sheppo 
38851ae08745Sheppo /*
38861ae08745Sheppo  * Function:
38871ae08745Sheppo  *	vdc_verify_seq_num()
38881ae08745Sheppo  *
38891ae08745Sheppo  * Description:
3890e1ebb9ecSlm66018  *	This functions verifies that the sequence number sent back by the vDisk
3891e1ebb9ecSlm66018  *	server with the latest message is what is expected (i.e. it is greater
3892e1ebb9ecSlm66018  *	than the last seq num sent by the vDisk server and less than or equal
3893e1ebb9ecSlm66018  *	to the last seq num generated by vdc).
3894e1ebb9ecSlm66018  *
3895e1ebb9ecSlm66018  *	It then checks the request ID to see if any requests need processing
3896e1ebb9ecSlm66018  *	in the DRing.
38971ae08745Sheppo  *
38981ae08745Sheppo  * Arguments:
38991ae08745Sheppo  *	vdc		- soft state pointer for this instance of the driver.
39001ae08745Sheppo  *	dring_msg	- pointer to the LDC message sent by vds
39011ae08745Sheppo  *
39021ae08745Sheppo  * Return Code:
3903e1ebb9ecSlm66018  *	VDC_SEQ_NUM_TODO	- Message needs to be processed
3904e1ebb9ecSlm66018  *	VDC_SEQ_NUM_SKIP	- Message has already been processed
3905e1ebb9ecSlm66018  *	VDC_SEQ_NUM_INVALID	- The seq numbers are so out of sync,
3906e1ebb9ecSlm66018  *				  vdc cannot deal with them
39071ae08745Sheppo  */
3908e1ebb9ecSlm66018 static int
3909e1ebb9ecSlm66018 vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg)
39101ae08745Sheppo {
39111ae08745Sheppo 	ASSERT(vdc != NULL);
39121ae08745Sheppo 	ASSERT(dring_msg != NULL);
3913d10e4ef2Snarayan 	ASSERT(mutex_owned(&vdc->lock));
39141ae08745Sheppo 
39151ae08745Sheppo 	/*
39161ae08745Sheppo 	 * Check to see if the messages were responded to in the correct
3917e1ebb9ecSlm66018 	 * order by vds.
39181ae08745Sheppo 	 */
3919e1ebb9ecSlm66018 	if ((dring_msg->seq_num <= vdc->seq_num_reply) ||
3920e1ebb9ecSlm66018 	    (dring_msg->seq_num > vdc->seq_num)) {
39213af08d82Slm66018 		DMSG(vdc, 0, "?[%d] Bogus sequence_number %lu: "
3922e1ebb9ecSlm66018 			"%lu > expected <= %lu (last proc req %lu sent %lu)\n",
3923e1ebb9ecSlm66018 				vdc->instance, dring_msg->seq_num,
3924e1ebb9ecSlm66018 				vdc->seq_num_reply, vdc->seq_num,
3925e1ebb9ecSlm66018 				vdc->req_id_proc, vdc->req_id);
3926e1ebb9ecSlm66018 		return (VDC_SEQ_NUM_INVALID);
39271ae08745Sheppo 	}
3928e1ebb9ecSlm66018 	vdc->seq_num_reply = dring_msg->seq_num;
39291ae08745Sheppo 
3930e1ebb9ecSlm66018 	if (vdc->req_id_proc < vdc->req_id)
3931e1ebb9ecSlm66018 		return (VDC_SEQ_NUM_TODO);
3932e1ebb9ecSlm66018 	else
3933e1ebb9ecSlm66018 		return (VDC_SEQ_NUM_SKIP);
39341ae08745Sheppo }
39351ae08745Sheppo 
39360a55fbb7Slm66018 
39370a55fbb7Slm66018 /*
39380a55fbb7Slm66018  * Function:
39390a55fbb7Slm66018  *	vdc_is_supported_version()
39400a55fbb7Slm66018  *
39410a55fbb7Slm66018  * Description:
39420a55fbb7Slm66018  *	This routine checks if the major/minor version numbers specified in
39430a55fbb7Slm66018  *	'ver_msg' are supported. If not it finds the next version that is
39440a55fbb7Slm66018  *	in the supported version list 'vdc_version[]' and sets the fields in
39450a55fbb7Slm66018  *	'ver_msg' to those values
39460a55fbb7Slm66018  *
39470a55fbb7Slm66018  * Arguments:
39480a55fbb7Slm66018  *	ver_msg	- LDC message sent by vDisk server
39490a55fbb7Slm66018  *
39500a55fbb7Slm66018  * Return Code:
39510a55fbb7Slm66018  *	B_TRUE	- Success
39520a55fbb7Slm66018  *	B_FALSE	- Version not supported
39530a55fbb7Slm66018  */
39540a55fbb7Slm66018 static boolean_t
39550a55fbb7Slm66018 vdc_is_supported_version(vio_ver_msg_t *ver_msg)
39560a55fbb7Slm66018 {
39570a55fbb7Slm66018 	int vdc_num_versions = sizeof (vdc_version) / sizeof (vdc_version[0]);
39580a55fbb7Slm66018 
39590a55fbb7Slm66018 	for (int i = 0; i < vdc_num_versions; i++) {
39600a55fbb7Slm66018 		ASSERT(vdc_version[i].major > 0);
39610a55fbb7Slm66018 		ASSERT((i == 0) ||
39620a55fbb7Slm66018 		    (vdc_version[i].major < vdc_version[i-1].major));
39630a55fbb7Slm66018 
39640a55fbb7Slm66018 		/*
39650a55fbb7Slm66018 		 * If the major versions match, adjust the minor version, if
39660a55fbb7Slm66018 		 * necessary, down to the highest value supported by this
39670a55fbb7Slm66018 		 * client. The server should support all minor versions lower
39680a55fbb7Slm66018 		 * than the value it sent
39690a55fbb7Slm66018 		 */
39700a55fbb7Slm66018 		if (ver_msg->ver_major == vdc_version[i].major) {
39710a55fbb7Slm66018 			if (ver_msg->ver_minor > vdc_version[i].minor) {
39723af08d82Slm66018 				DMSGX(0,
39733af08d82Slm66018 				    "Adjusting minor version from %u to %u",
39740a55fbb7Slm66018 				    ver_msg->ver_minor, vdc_version[i].minor);
39750a55fbb7Slm66018 				ver_msg->ver_minor = vdc_version[i].minor;
39760a55fbb7Slm66018 			}
39770a55fbb7Slm66018 			return (B_TRUE);
39780a55fbb7Slm66018 		}
39790a55fbb7Slm66018 
39800a55fbb7Slm66018 		/*
39810a55fbb7Slm66018 		 * If the message contains a higher major version number, set
39820a55fbb7Slm66018 		 * the message's major/minor versions to the current values
39830a55fbb7Slm66018 		 * and return false, so this message will get resent with
39840a55fbb7Slm66018 		 * these values, and the server will potentially try again
39850a55fbb7Slm66018 		 * with the same or a lower version
39860a55fbb7Slm66018 		 */
39870a55fbb7Slm66018 		if (ver_msg->ver_major > vdc_version[i].major) {
39880a55fbb7Slm66018 			ver_msg->ver_major = vdc_version[i].major;
39890a55fbb7Slm66018 			ver_msg->ver_minor = vdc_version[i].minor;
39903af08d82Slm66018 			DMSGX(0, "Suggesting major/minor (0x%x/0x%x)\n",
39910a55fbb7Slm66018 				ver_msg->ver_major, ver_msg->ver_minor);
39920a55fbb7Slm66018 
39930a55fbb7Slm66018 			return (B_FALSE);
39940a55fbb7Slm66018 		}
39950a55fbb7Slm66018 
39960a55fbb7Slm66018 		/*
39970a55fbb7Slm66018 		 * Otherwise, the message's major version is less than the
39980a55fbb7Slm66018 		 * current major version, so continue the loop to the next
39990a55fbb7Slm66018 		 * (lower) supported version
40000a55fbb7Slm66018 		 */
40010a55fbb7Slm66018 	}
40020a55fbb7Slm66018 
40030a55fbb7Slm66018 	/*
40040a55fbb7Slm66018 	 * No common version was found; "ground" the version pair in the
40050a55fbb7Slm66018 	 * message to terminate negotiation
40060a55fbb7Slm66018 	 */
40070a55fbb7Slm66018 	ver_msg->ver_major = 0;
40080a55fbb7Slm66018 	ver_msg->ver_minor = 0;
40090a55fbb7Slm66018 
40100a55fbb7Slm66018 	return (B_FALSE);
40110a55fbb7Slm66018 }
40121ae08745Sheppo /* -------------------------------------------------------------------------- */
40131ae08745Sheppo 
40141ae08745Sheppo /*
40151ae08745Sheppo  * DKIO(7) support
40161ae08745Sheppo  */
40171ae08745Sheppo 
40181ae08745Sheppo typedef struct vdc_dk_arg {
40191ae08745Sheppo 	struct dk_callback	dkc;
40201ae08745Sheppo 	int			mode;
40211ae08745Sheppo 	dev_t			dev;
40221ae08745Sheppo 	vdc_t			*vdc;
40231ae08745Sheppo } vdc_dk_arg_t;
40241ae08745Sheppo 
40251ae08745Sheppo /*
40261ae08745Sheppo  * Function:
40271ae08745Sheppo  * 	vdc_dkio_flush_cb()
40281ae08745Sheppo  *
40291ae08745Sheppo  * Description:
40301ae08745Sheppo  *	This routine is a callback for DKIOCFLUSHWRITECACHE which can be called
40311ae08745Sheppo  *	by kernel code.
40321ae08745Sheppo  *
40331ae08745Sheppo  * Arguments:
40341ae08745Sheppo  *	arg	- a pointer to a vdc_dk_arg_t structure.
40351ae08745Sheppo  */
40361ae08745Sheppo void
40371ae08745Sheppo vdc_dkio_flush_cb(void *arg)
40381ae08745Sheppo {
40391ae08745Sheppo 	struct vdc_dk_arg	*dk_arg = (struct vdc_dk_arg *)arg;
40401ae08745Sheppo 	struct dk_callback	*dkc = NULL;
40411ae08745Sheppo 	vdc_t			*vdc = NULL;
40421ae08745Sheppo 	int			rv;
40431ae08745Sheppo 
40441ae08745Sheppo 	if (dk_arg == NULL) {
40453af08d82Slm66018 		cmn_err(CE_NOTE, "?[Unk] DKIOCFLUSHWRITECACHE arg is NULL\n");
40461ae08745Sheppo 		return;
40471ae08745Sheppo 	}
40481ae08745Sheppo 	dkc = &dk_arg->dkc;
40491ae08745Sheppo 	vdc = dk_arg->vdc;
40501ae08745Sheppo 	ASSERT(vdc != NULL);
40511ae08745Sheppo 
40523af08d82Slm66018 	rv = vdc_do_sync_op(vdc, VD_OP_FLUSH, NULL, 0,
40530d0c8d4bSnarayan 	    VDCPART(dk_arg->dev), 0, CB_SYNC, 0, VIO_both_dir);
40541ae08745Sheppo 	if (rv != 0) {
40553af08d82Slm66018 		DMSG(vdc, 0, "[%d] DKIOCFLUSHWRITECACHE failed %d : model %x\n",
4056e1ebb9ecSlm66018 			vdc->instance, rv,
40571ae08745Sheppo 			ddi_model_convert_from(dk_arg->mode & FMODELS));
40581ae08745Sheppo 	}
40591ae08745Sheppo 
40601ae08745Sheppo 	/*
40611ae08745Sheppo 	 * Trigger the call back to notify the caller the the ioctl call has
40621ae08745Sheppo 	 * been completed.
40631ae08745Sheppo 	 */
40641ae08745Sheppo 	if ((dk_arg->mode & FKIOCTL) &&
40651ae08745Sheppo 	    (dkc != NULL) &&
40661ae08745Sheppo 	    (dkc->dkc_callback != NULL)) {
40671ae08745Sheppo 		ASSERT(dkc->dkc_cookie != NULL);
40688e6a2a04Slm66018 		(*dkc->dkc_callback)(dkc->dkc_cookie, rv);
40691ae08745Sheppo 	}
40701ae08745Sheppo 
40711ae08745Sheppo 	/* Indicate that one less DKIO write flush is outstanding */
40721ae08745Sheppo 	mutex_enter(&vdc->lock);
40731ae08745Sheppo 	vdc->dkio_flush_pending--;
40741ae08745Sheppo 	ASSERT(vdc->dkio_flush_pending >= 0);
40751ae08745Sheppo 	mutex_exit(&vdc->lock);
40768e6a2a04Slm66018 
40778e6a2a04Slm66018 	/* free the mem that was allocated when the callback was dispatched */
40788e6a2a04Slm66018 	kmem_free(arg, sizeof (vdc_dk_arg_t));
40791ae08745Sheppo }
40801ae08745Sheppo 
40811ae08745Sheppo /*
40821ae08745Sheppo  * This structure is used in the DKIO(7I) array below.
40831ae08745Sheppo  */
40841ae08745Sheppo typedef struct vdc_dk_ioctl {
40851ae08745Sheppo 	uint8_t		op;		/* VD_OP_XXX value */
40861ae08745Sheppo 	int		cmd;		/* Solaris ioctl operation number */
40871ae08745Sheppo 	size_t		nbytes;		/* size of structure to be copied */
40880a55fbb7Slm66018 
40890a55fbb7Slm66018 	/* function to convert between vDisk and Solaris structure formats */
4090d10e4ef2Snarayan 	int	(*convert)(vdc_t *vdc, void *vd_buf, void *ioctl_arg,
4091d10e4ef2Snarayan 	    int mode, int dir);
40921ae08745Sheppo } vdc_dk_ioctl_t;
40931ae08745Sheppo 
40941ae08745Sheppo /*
40951ae08745Sheppo  * Subset of DKIO(7I) operations currently supported
40961ae08745Sheppo  */
40971ae08745Sheppo static vdc_dk_ioctl_t	dk_ioctl[] = {
40980a55fbb7Slm66018 	{VD_OP_FLUSH,		DKIOCFLUSHWRITECACHE,	sizeof (int),
40990a55fbb7Slm66018 		vdc_null_copy_func},
41000a55fbb7Slm66018 	{VD_OP_GET_WCE,		DKIOCGETWCE,		sizeof (int),
41014bac2208Snarayan 		vdc_get_wce_convert},
41020a55fbb7Slm66018 	{VD_OP_SET_WCE,		DKIOCSETWCE,		sizeof (int),
41034bac2208Snarayan 		vdc_set_wce_convert},
41040a55fbb7Slm66018 	{VD_OP_GET_VTOC,	DKIOCGVTOC,		sizeof (vd_vtoc_t),
41050a55fbb7Slm66018 		vdc_get_vtoc_convert},
41060a55fbb7Slm66018 	{VD_OP_SET_VTOC,	DKIOCSVTOC,		sizeof (vd_vtoc_t),
41070a55fbb7Slm66018 		vdc_set_vtoc_convert},
41080a55fbb7Slm66018 	{VD_OP_GET_DISKGEOM,	DKIOCGGEOM,		sizeof (vd_geom_t),
41090a55fbb7Slm66018 		vdc_get_geom_convert},
41100a55fbb7Slm66018 	{VD_OP_GET_DISKGEOM,	DKIOCG_PHYGEOM,		sizeof (vd_geom_t),
41110a55fbb7Slm66018 		vdc_get_geom_convert},
41120a55fbb7Slm66018 	{VD_OP_GET_DISKGEOM, 	DKIOCG_VIRTGEOM,	sizeof (vd_geom_t),
41130a55fbb7Slm66018 		vdc_get_geom_convert},
41140a55fbb7Slm66018 	{VD_OP_SET_DISKGEOM,	DKIOCSGEOM,		sizeof (vd_geom_t),
41150a55fbb7Slm66018 		vdc_set_geom_convert},
41164bac2208Snarayan 	{VD_OP_GET_EFI,		DKIOCGETEFI,		0,
41174bac2208Snarayan 		vdc_get_efi_convert},
41184bac2208Snarayan 	{VD_OP_SET_EFI,		DKIOCSETEFI,		0,
41194bac2208Snarayan 		vdc_set_efi_convert},
41200a55fbb7Slm66018 
41210a55fbb7Slm66018 	/*
41220a55fbb7Slm66018 	 * These particular ioctls are not sent to the server - vdc fakes up
41230a55fbb7Slm66018 	 * the necessary info.
41240a55fbb7Slm66018 	 */
41250a55fbb7Slm66018 	{0, DKIOCINFO, sizeof (struct dk_cinfo), vdc_null_copy_func},
41260a55fbb7Slm66018 	{0, DKIOCGMEDIAINFO, sizeof (struct dk_minfo), vdc_null_copy_func},
41270a55fbb7Slm66018 	{0, USCSICMD,	sizeof (struct uscsi_cmd), vdc_null_copy_func},
41280a55fbb7Slm66018 	{0, DKIOCREMOVABLE, 0, vdc_null_copy_func},
41290a55fbb7Slm66018 	{0, CDROMREADOFFSET, 0, vdc_null_copy_func}
41301ae08745Sheppo };
41311ae08745Sheppo 
41321ae08745Sheppo /*
41331ae08745Sheppo  * Function:
41341ae08745Sheppo  *	vd_process_ioctl()
41351ae08745Sheppo  *
41361ae08745Sheppo  * Description:
41370a55fbb7Slm66018  *	This routine processes disk specific ioctl calls
41381ae08745Sheppo  *
41391ae08745Sheppo  * Arguments:
41401ae08745Sheppo  *	dev	- the device number
41411ae08745Sheppo  *	cmd	- the operation [dkio(7I)] to be processed
41421ae08745Sheppo  *	arg	- pointer to user provided structure
41431ae08745Sheppo  *		  (contains data to be set or reference parameter for get)
41441ae08745Sheppo  *	mode	- bit flag, indicating open settings, 32/64 bit type, etc
41451ae08745Sheppo  *
41461ae08745Sheppo  * Return Code:
41471ae08745Sheppo  *	0
41481ae08745Sheppo  *	EFAULT
41491ae08745Sheppo  *	ENXIO
41501ae08745Sheppo  *	EIO
41511ae08745Sheppo  *	ENOTSUP
41521ae08745Sheppo  */
41531ae08745Sheppo static int
41541ae08745Sheppo vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode)
41551ae08745Sheppo {
41560d0c8d4bSnarayan 	int		instance = VDCUNIT(dev);
41571ae08745Sheppo 	vdc_t		*vdc = NULL;
41581ae08745Sheppo 	int		rv = -1;
41591ae08745Sheppo 	int		idx = 0;		/* index into dk_ioctl[] */
41601ae08745Sheppo 	size_t		len = 0;		/* #bytes to send to vds */
41611ae08745Sheppo 	size_t		alloc_len = 0;		/* #bytes to allocate mem for */
41621ae08745Sheppo 	caddr_t		mem_p = NULL;
41631ae08745Sheppo 	size_t		nioctls = (sizeof (dk_ioctl)) / (sizeof (dk_ioctl[0]));
4164d10e4ef2Snarayan 	struct vtoc	vtoc_saved;
41653af08d82Slm66018 	vdc_dk_ioctl_t	*iop;
41661ae08745Sheppo 
41671ae08745Sheppo 	vdc = ddi_get_soft_state(vdc_state, instance);
41681ae08745Sheppo 	if (vdc == NULL) {
41691ae08745Sheppo 		cmn_err(CE_NOTE, "![%d] Could not get soft state structure",
41701ae08745Sheppo 		    instance);
41711ae08745Sheppo 		return (ENXIO);
41721ae08745Sheppo 	}
41731ae08745Sheppo 
41743af08d82Slm66018 	DMSG(vdc, 0, "[%d] Processing ioctl(%x) for dev %lx : model %x\n",
41753af08d82Slm66018 		instance, cmd, dev, ddi_model_convert_from(mode & FMODELS));
41761ae08745Sheppo 
41771ae08745Sheppo 	/*
41781ae08745Sheppo 	 * Validate the ioctl operation to be performed.
41791ae08745Sheppo 	 *
41801ae08745Sheppo 	 * If we have looped through the array without finding a match then we
41811ae08745Sheppo 	 * don't support this ioctl.
41821ae08745Sheppo 	 */
41831ae08745Sheppo 	for (idx = 0; idx < nioctls; idx++) {
41841ae08745Sheppo 		if (cmd == dk_ioctl[idx].cmd)
41851ae08745Sheppo 			break;
41861ae08745Sheppo 	}
41871ae08745Sheppo 
41881ae08745Sheppo 	if (idx >= nioctls) {
41893af08d82Slm66018 		DMSG(vdc, 0, "[%d] Unsupported ioctl (0x%x)\n",
4190e1ebb9ecSlm66018 		    vdc->instance, cmd);
41911ae08745Sheppo 		return (ENOTSUP);
41921ae08745Sheppo 	}
41931ae08745Sheppo 
41943af08d82Slm66018 	iop = &(dk_ioctl[idx]);
41953af08d82Slm66018 
41964bac2208Snarayan 	if (cmd == DKIOCGETEFI || cmd == DKIOCSETEFI) {
41974bac2208Snarayan 		/* size is not fixed for EFI ioctls, it depends on ioctl arg */
41984bac2208Snarayan 		dk_efi_t	dk_efi;
41994bac2208Snarayan 
42004bac2208Snarayan 		rv = ddi_copyin(arg, &dk_efi, sizeof (dk_efi_t), mode);
42014bac2208Snarayan 		if (rv != 0)
42024bac2208Snarayan 			return (EFAULT);
42034bac2208Snarayan 
42044bac2208Snarayan 		len = sizeof (vd_efi_t) - 1 + dk_efi.dki_length;
42054bac2208Snarayan 	} else {
42063af08d82Slm66018 		len = iop->nbytes;
42074bac2208Snarayan 	}
42081ae08745Sheppo 
42091ae08745Sheppo 	/*
42100a55fbb7Slm66018 	 * Deal with the ioctls which the server does not provide. vdc can
42110a55fbb7Slm66018 	 * fake these up and return immediately
42121ae08745Sheppo 	 */
42131ae08745Sheppo 	switch (cmd) {
42141ae08745Sheppo 	case CDROMREADOFFSET:
42151ae08745Sheppo 	case DKIOCREMOVABLE:
42160a55fbb7Slm66018 	case USCSICMD:
42171ae08745Sheppo 		return (ENOTTY);
42181ae08745Sheppo 
42191ae08745Sheppo 	case DKIOCINFO:
42201ae08745Sheppo 		{
42211ae08745Sheppo 			struct dk_cinfo	cinfo;
42221ae08745Sheppo 			if (vdc->cinfo == NULL)
42231ae08745Sheppo 				return (ENXIO);
42241ae08745Sheppo 
42251ae08745Sheppo 			bcopy(vdc->cinfo, &cinfo, sizeof (struct dk_cinfo));
42260d0c8d4bSnarayan 			cinfo.dki_partition = VDCPART(dev);
42271ae08745Sheppo 
42281ae08745Sheppo 			rv = ddi_copyout(&cinfo, (void *)arg,
42291ae08745Sheppo 					sizeof (struct dk_cinfo), mode);
42301ae08745Sheppo 			if (rv != 0)
42311ae08745Sheppo 				return (EFAULT);
42321ae08745Sheppo 
42331ae08745Sheppo 			return (0);
42341ae08745Sheppo 		}
42351ae08745Sheppo 
42361ae08745Sheppo 	case DKIOCGMEDIAINFO:
42378e6a2a04Slm66018 		{
42381ae08745Sheppo 			if (vdc->minfo == NULL)
42391ae08745Sheppo 				return (ENXIO);
42401ae08745Sheppo 
42411ae08745Sheppo 			rv = ddi_copyout(vdc->minfo, (void *)arg,
42421ae08745Sheppo 					sizeof (struct dk_minfo), mode);
42431ae08745Sheppo 			if (rv != 0)
42441ae08745Sheppo 				return (EFAULT);
42451ae08745Sheppo 
42461ae08745Sheppo 			return (0);
42471ae08745Sheppo 		}
42481ae08745Sheppo 
42498e6a2a04Slm66018 	case DKIOCFLUSHWRITECACHE:
42508e6a2a04Slm66018 		{
42518e6a2a04Slm66018 			struct dk_callback *dkc = (struct dk_callback *)arg;
42528e6a2a04Slm66018 			vdc_dk_arg_t	*dkarg = NULL;
42538e6a2a04Slm66018 
42543af08d82Slm66018 			DMSG(vdc, 1, "[%d] Flush W$: mode %x\n",
42553af08d82Slm66018 			    instance, mode);
42568e6a2a04Slm66018 
42578e6a2a04Slm66018 			/*
42588e6a2a04Slm66018 			 * If the backing device is not a 'real' disk then the
42598e6a2a04Slm66018 			 * W$ operation request to the vDisk server will fail
42608e6a2a04Slm66018 			 * so we might as well save the cycles and return now.
42618e6a2a04Slm66018 			 */
42628e6a2a04Slm66018 			if (vdc->vdisk_type != VD_DISK_TYPE_DISK)
42638e6a2a04Slm66018 				return (ENOTTY);
42648e6a2a04Slm66018 
42658e6a2a04Slm66018 			/*
42668e6a2a04Slm66018 			 * If arg is NULL, then there is no callback function
42678e6a2a04Slm66018 			 * registered and the call operates synchronously; we
42688e6a2a04Slm66018 			 * break and continue with the rest of the function and
42698e6a2a04Slm66018 			 * wait for vds to return (i.e. after the request to
42708e6a2a04Slm66018 			 * vds returns successfully, all writes completed prior
42718e6a2a04Slm66018 			 * to the ioctl will have been flushed from the disk
42728e6a2a04Slm66018 			 * write cache to persistent media.
42738e6a2a04Slm66018 			 *
42748e6a2a04Slm66018 			 * If a callback function is registered, we dispatch
42758e6a2a04Slm66018 			 * the request on a task queue and return immediately.
42768e6a2a04Slm66018 			 * The callback will deal with informing the calling
42778e6a2a04Slm66018 			 * thread that the flush request is completed.
42788e6a2a04Slm66018 			 */
42798e6a2a04Slm66018 			if (dkc == NULL)
42808e6a2a04Slm66018 				break;
42818e6a2a04Slm66018 
42828e6a2a04Slm66018 			dkarg = kmem_zalloc(sizeof (vdc_dk_arg_t), KM_SLEEP);
42838e6a2a04Slm66018 
42848e6a2a04Slm66018 			dkarg->mode = mode;
42858e6a2a04Slm66018 			dkarg->dev = dev;
42868e6a2a04Slm66018 			bcopy(dkc, &dkarg->dkc, sizeof (*dkc));
42878e6a2a04Slm66018 
42888e6a2a04Slm66018 			mutex_enter(&vdc->lock);
42898e6a2a04Slm66018 			vdc->dkio_flush_pending++;
42908e6a2a04Slm66018 			dkarg->vdc = vdc;
42918e6a2a04Slm66018 			mutex_exit(&vdc->lock);
42928e6a2a04Slm66018 
42938e6a2a04Slm66018 			/* put the request on a task queue */
42948e6a2a04Slm66018 			rv = taskq_dispatch(system_taskq, vdc_dkio_flush_cb,
42958e6a2a04Slm66018 				(void *)dkarg, DDI_SLEEP);
42963af08d82Slm66018 			if (rv == NULL) {
42973af08d82Slm66018 				/* clean up if dispatch fails */
42983af08d82Slm66018 				mutex_enter(&vdc->lock);
42993af08d82Slm66018 				vdc->dkio_flush_pending--;
43003af08d82Slm66018 				kmem_free(dkarg, sizeof (vdc_dk_arg_t));
43013af08d82Slm66018 			}
43028e6a2a04Slm66018 
43038e6a2a04Slm66018 			return (rv == NULL ? ENOMEM : 0);
43048e6a2a04Slm66018 		}
43058e6a2a04Slm66018 	}
43068e6a2a04Slm66018 
43071ae08745Sheppo 	/* catch programming error in vdc - should be a VD_OP_XXX ioctl */
43083af08d82Slm66018 	ASSERT(iop->op != 0);
43091ae08745Sheppo 
43101ae08745Sheppo 	/* LDC requires that the memory being mapped is 8-byte aligned */
43111ae08745Sheppo 	alloc_len = P2ROUNDUP(len, sizeof (uint64_t));
43123af08d82Slm66018 	DMSG(vdc, 1, "[%d] struct size %ld alloc %ld\n",
43133af08d82Slm66018 	    instance, len, alloc_len);
43141ae08745Sheppo 
43150a55fbb7Slm66018 	ASSERT(alloc_len != 0);	/* sanity check */
43161ae08745Sheppo 	mem_p = kmem_zalloc(alloc_len, KM_SLEEP);
43171ae08745Sheppo 
4318d10e4ef2Snarayan 	if (cmd == DKIOCSVTOC) {
4319d10e4ef2Snarayan 		/*
4320d10e4ef2Snarayan 		 * Save a copy of the current VTOC so that we can roll back
4321d10e4ef2Snarayan 		 * if the setting of the new VTOC fails.
4322d10e4ef2Snarayan 		 */
4323d10e4ef2Snarayan 		bcopy(vdc->vtoc, &vtoc_saved, sizeof (struct vtoc));
4324d10e4ef2Snarayan 	}
4325d10e4ef2Snarayan 
43260a55fbb7Slm66018 	/*
43270a55fbb7Slm66018 	 * Call the conversion function for this ioctl whhich if necessary
43280a55fbb7Slm66018 	 * converts from the Solaris format to the format ARC'ed
43290a55fbb7Slm66018 	 * as part of the vDisk protocol (FWARC 2006/195)
43300a55fbb7Slm66018 	 */
43313af08d82Slm66018 	ASSERT(iop->convert != NULL);
43323af08d82Slm66018 	rv = (iop->convert)(vdc, arg, mem_p, mode, VD_COPYIN);
43331ae08745Sheppo 	if (rv != 0) {
43343af08d82Slm66018 		DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n",
4335e1ebb9ecSlm66018 				instance, rv, cmd);
43361ae08745Sheppo 		if (mem_p != NULL)
43371ae08745Sheppo 			kmem_free(mem_p, alloc_len);
43380a55fbb7Slm66018 		return (rv);
43391ae08745Sheppo 	}
43401ae08745Sheppo 
43411ae08745Sheppo 	/*
43421ae08745Sheppo 	 * send request to vds to service the ioctl.
43431ae08745Sheppo 	 */
43443af08d82Slm66018 	rv = vdc_do_sync_op(vdc, iop->op, mem_p, alloc_len,
43450d0c8d4bSnarayan 	    VDCPART(dev), 0, CB_SYNC, (void *)(uint64_t)mode,
43463af08d82Slm66018 	    VIO_both_dir);
43473af08d82Slm66018 
43481ae08745Sheppo 	if (rv != 0) {
43491ae08745Sheppo 		/*
43501ae08745Sheppo 		 * This is not necessarily an error. The ioctl could
43511ae08745Sheppo 		 * be returning a value such as ENOTTY to indicate
43521ae08745Sheppo 		 * that the ioctl is not applicable.
43531ae08745Sheppo 		 */
43543af08d82Slm66018 		DMSG(vdc, 0, "[%d] vds returned %d for ioctl 0x%x\n",
4355e1ebb9ecSlm66018 			instance, rv, cmd);
43561ae08745Sheppo 		if (mem_p != NULL)
43571ae08745Sheppo 			kmem_free(mem_p, alloc_len);
4358d10e4ef2Snarayan 
4359d10e4ef2Snarayan 		if (cmd == DKIOCSVTOC) {
4360d10e4ef2Snarayan 			/* update of the VTOC has failed, roll back */
4361d10e4ef2Snarayan 			bcopy(&vtoc_saved, vdc->vtoc, sizeof (struct vtoc));
4362d10e4ef2Snarayan 		}
4363d10e4ef2Snarayan 
43641ae08745Sheppo 		return (rv);
43651ae08745Sheppo 	}
43661ae08745Sheppo 
43671ae08745Sheppo 	if (cmd == DKIOCSVTOC) {
4368d10e4ef2Snarayan 		/*
43694bac2208Snarayan 		 * The VTOC has been changed. We need to update the device
43704bac2208Snarayan 		 * nodes to handle the case where an EFI label has been
43714bac2208Snarayan 		 * changed to a VTOC label. We also try and update the device
4372d10e4ef2Snarayan 		 * node properties. Failing to set the properties should
4373d10e4ef2Snarayan 		 * not cause an error to be return the caller though.
4374d10e4ef2Snarayan 		 */
43754bac2208Snarayan 		vdc->vdisk_label = VD_DISK_LABEL_VTOC;
43764bac2208Snarayan 		(void) vdc_create_device_nodes_vtoc(vdc);
43774bac2208Snarayan 
43781ae08745Sheppo 		if (vdc_create_device_nodes_props(vdc)) {
43793af08d82Slm66018 			DMSG(vdc, 0, "![%d] Failed to update device nodes"
4380d10e4ef2Snarayan 			    " properties", vdc->instance);
43811ae08745Sheppo 		}
43824bac2208Snarayan 
43834bac2208Snarayan 	} else if (cmd == DKIOCSETEFI) {
43844bac2208Snarayan 		/*
43854bac2208Snarayan 		 * The EFI has been changed. We need to update the device
43864bac2208Snarayan 		 * nodes to handle the case where a VTOC label has been
43874bac2208Snarayan 		 * changed to an EFI label. We also try and update the device
43884bac2208Snarayan 		 * node properties. Failing to set the properties should
43894bac2208Snarayan 		 * not cause an error to be return the caller though.
43904bac2208Snarayan 		 */
43914bac2208Snarayan 		struct dk_gpt *efi;
43924bac2208Snarayan 		size_t efi_len;
43934bac2208Snarayan 
43944bac2208Snarayan 		vdc->vdisk_label = VD_DISK_LABEL_EFI;
43954bac2208Snarayan 		(void) vdc_create_device_nodes_efi(vdc);
43964bac2208Snarayan 
43974bac2208Snarayan 		rv = vdc_efi_alloc_and_read(dev, &efi, &efi_len);
43984bac2208Snarayan 
43994bac2208Snarayan 		if (rv == 0) {
44004bac2208Snarayan 			vdc_store_efi(vdc, efi);
44014bac2208Snarayan 			rv = vdc_create_device_nodes_props(vdc);
44024bac2208Snarayan 			vd_efi_free(efi, efi_len);
44034bac2208Snarayan 		}
44044bac2208Snarayan 
44054bac2208Snarayan 		if (rv) {
44063af08d82Slm66018 			DMSG(vdc, 0, "![%d] Failed to update device nodes"
44074bac2208Snarayan 			    " properties", vdc->instance);
44084bac2208Snarayan 		}
44091ae08745Sheppo 	}
44101ae08745Sheppo 
44111ae08745Sheppo 	/*
44120a55fbb7Slm66018 	 * Call the conversion function (if it exists) for this ioctl
44130a55fbb7Slm66018 	 * which converts from the format ARC'ed as part of the vDisk
44140a55fbb7Slm66018 	 * protocol (FWARC 2006/195) back to a format understood by
44150a55fbb7Slm66018 	 * the rest of Solaris.
44161ae08745Sheppo 	 */
44173af08d82Slm66018 	rv = (iop->convert)(vdc, mem_p, arg, mode, VD_COPYOUT);
44180a55fbb7Slm66018 	if (rv != 0) {
44193af08d82Slm66018 		DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n",
4420e1ebb9ecSlm66018 				instance, rv, cmd);
44211ae08745Sheppo 		if (mem_p != NULL)
44221ae08745Sheppo 			kmem_free(mem_p, alloc_len);
44230a55fbb7Slm66018 		return (rv);
44241ae08745Sheppo 	}
44251ae08745Sheppo 
44261ae08745Sheppo 	if (mem_p != NULL)
44271ae08745Sheppo 		kmem_free(mem_p, alloc_len);
44281ae08745Sheppo 
44291ae08745Sheppo 	return (rv);
44301ae08745Sheppo }
44311ae08745Sheppo 
44321ae08745Sheppo /*
44331ae08745Sheppo  * Function:
44340a55fbb7Slm66018  *
44350a55fbb7Slm66018  * Description:
44360a55fbb7Slm66018  *	This is an empty conversion function used by ioctl calls which
44370a55fbb7Slm66018  *	do not need to convert the data being passed in/out to userland
44380a55fbb7Slm66018  */
44390a55fbb7Slm66018 static int
4440d10e4ef2Snarayan vdc_null_copy_func(vdc_t *vdc, void *from, void *to, int mode, int dir)
44410a55fbb7Slm66018 {
4442d10e4ef2Snarayan 	_NOTE(ARGUNUSED(vdc))
44430a55fbb7Slm66018 	_NOTE(ARGUNUSED(from))
44440a55fbb7Slm66018 	_NOTE(ARGUNUSED(to))
44450a55fbb7Slm66018 	_NOTE(ARGUNUSED(mode))
44460a55fbb7Slm66018 	_NOTE(ARGUNUSED(dir))
44470a55fbb7Slm66018 
44480a55fbb7Slm66018 	return (0);
44490a55fbb7Slm66018 }
44500a55fbb7Slm66018 
44514bac2208Snarayan static int
44524bac2208Snarayan vdc_get_wce_convert(vdc_t *vdc, void *from, void *to,
44534bac2208Snarayan     int mode, int dir)
44544bac2208Snarayan {
44554bac2208Snarayan 	_NOTE(ARGUNUSED(vdc))
44564bac2208Snarayan 
44574bac2208Snarayan 	if (dir == VD_COPYIN)
44584bac2208Snarayan 		return (0);		/* nothing to do */
44594bac2208Snarayan 
44604bac2208Snarayan 	if (ddi_copyout(from, to, sizeof (int), mode) != 0)
44614bac2208Snarayan 		return (EFAULT);
44624bac2208Snarayan 
44634bac2208Snarayan 	return (0);
44644bac2208Snarayan }
44654bac2208Snarayan 
44664bac2208Snarayan static int
44674bac2208Snarayan vdc_set_wce_convert(vdc_t *vdc, void *from, void *to,
44684bac2208Snarayan     int mode, int dir)
44694bac2208Snarayan {
44704bac2208Snarayan 	_NOTE(ARGUNUSED(vdc))
44714bac2208Snarayan 
44724bac2208Snarayan 	if (dir == VD_COPYOUT)
44734bac2208Snarayan 		return (0);		/* nothing to do */
44744bac2208Snarayan 
44754bac2208Snarayan 	if (ddi_copyin(from, to, sizeof (int), mode) != 0)
44764bac2208Snarayan 		return (EFAULT);
44774bac2208Snarayan 
44784bac2208Snarayan 	return (0);
44794bac2208Snarayan }
44804bac2208Snarayan 
44810a55fbb7Slm66018 /*
44820a55fbb7Slm66018  * Function:
44830a55fbb7Slm66018  *	vdc_get_vtoc_convert()
44840a55fbb7Slm66018  *
44850a55fbb7Slm66018  * Description:
4486d10e4ef2Snarayan  *	This routine performs the necessary convertions from the DKIOCGVTOC
4487d10e4ef2Snarayan  *	Solaris structure to the format defined in FWARC 2006/195.
4488d10e4ef2Snarayan  *
4489d10e4ef2Snarayan  *	In the struct vtoc definition, the timestamp field is marked as not
4490d10e4ef2Snarayan  *	supported so it is not part of vDisk protocol (FWARC 2006/195).
4491d10e4ef2Snarayan  *	However SVM uses that field to check it can write into the VTOC,
4492d10e4ef2Snarayan  *	so we fake up the info of that field.
44930a55fbb7Slm66018  *
44940a55fbb7Slm66018  * Arguments:
4495d10e4ef2Snarayan  *	vdc	- the vDisk client
44960a55fbb7Slm66018  *	from	- the buffer containing the data to be copied from
44970a55fbb7Slm66018  *	to	- the buffer to be copied to
44980a55fbb7Slm66018  *	mode	- flags passed to ioctl() call
44990a55fbb7Slm66018  *	dir	- the "direction" of the copy - VD_COPYIN or VD_COPYOUT
45000a55fbb7Slm66018  *
45010a55fbb7Slm66018  * Return Code:
45020a55fbb7Slm66018  *	0	- Success
45030a55fbb7Slm66018  *	ENXIO	- incorrect buffer passed in.
4504d10e4ef2Snarayan  *	EFAULT	- ddi_copyout routine encountered an error.
45050a55fbb7Slm66018  */
45060a55fbb7Slm66018 static int
4507d10e4ef2Snarayan vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
45080a55fbb7Slm66018 {
4509d10e4ef2Snarayan 	int		i;
45100a55fbb7Slm66018 	void		*tmp_mem = NULL;
45110a55fbb7Slm66018 	void		*tmp_memp;
45120a55fbb7Slm66018 	struct vtoc	vt;
45130a55fbb7Slm66018 	struct vtoc32	vt32;
45140a55fbb7Slm66018 	int		copy_len = 0;
45150a55fbb7Slm66018 	int		rv = 0;
45160a55fbb7Slm66018 
45170a55fbb7Slm66018 	if (dir != VD_COPYOUT)
45180a55fbb7Slm66018 		return (0);	/* nothing to do */
45190a55fbb7Slm66018 
45200a55fbb7Slm66018 	if ((from == NULL) || (to == NULL))
45210a55fbb7Slm66018 		return (ENXIO);
45220a55fbb7Slm66018 
45230a55fbb7Slm66018 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32)
45240a55fbb7Slm66018 		copy_len = sizeof (struct vtoc32);
45250a55fbb7Slm66018 	else
45260a55fbb7Slm66018 		copy_len = sizeof (struct vtoc);
45270a55fbb7Slm66018 
45280a55fbb7Slm66018 	tmp_mem = kmem_alloc(copy_len, KM_SLEEP);
45290a55fbb7Slm66018 
45300a55fbb7Slm66018 	VD_VTOC2VTOC((vd_vtoc_t *)from, &vt);
4531d10e4ef2Snarayan 
4532d10e4ef2Snarayan 	/* fake the VTOC timestamp field */
4533d10e4ef2Snarayan 	for (i = 0; i < V_NUMPAR; i++) {
4534d10e4ef2Snarayan 		vt.timestamp[i] = vdc->vtoc->timestamp[i];
4535d10e4ef2Snarayan 	}
4536d10e4ef2Snarayan 
45370a55fbb7Slm66018 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
45380a55fbb7Slm66018 		vtoctovtoc32(vt, vt32);
45390a55fbb7Slm66018 		tmp_memp = &vt32;
45400a55fbb7Slm66018 	} else {
45410a55fbb7Slm66018 		tmp_memp = &vt;
45420a55fbb7Slm66018 	}
45430a55fbb7Slm66018 	rv = ddi_copyout(tmp_memp, to, copy_len, mode);
45440a55fbb7Slm66018 	if (rv != 0)
45450a55fbb7Slm66018 		rv = EFAULT;
45460a55fbb7Slm66018 
45470a55fbb7Slm66018 	kmem_free(tmp_mem, copy_len);
45480a55fbb7Slm66018 	return (rv);
45490a55fbb7Slm66018 }
45500a55fbb7Slm66018 
45510a55fbb7Slm66018 /*
45520a55fbb7Slm66018  * Function:
45530a55fbb7Slm66018  *	vdc_set_vtoc_convert()
45540a55fbb7Slm66018  *
45550a55fbb7Slm66018  * Description:
4556d10e4ef2Snarayan  *	This routine performs the necessary convertions from the DKIOCSVTOC
4557d10e4ef2Snarayan  *	Solaris structure to the format defined in FWARC 2006/195.
45580a55fbb7Slm66018  *
45590a55fbb7Slm66018  * Arguments:
4560d10e4ef2Snarayan  *	vdc	- the vDisk client
45610a55fbb7Slm66018  *	from	- Buffer with data
45620a55fbb7Slm66018  *	to	- Buffer where data is to be copied to
45630a55fbb7Slm66018  *	mode	- flags passed to ioctl
45640a55fbb7Slm66018  *	dir	- direction of copy (in or out)
45650a55fbb7Slm66018  *
45660a55fbb7Slm66018  * Return Code:
45670a55fbb7Slm66018  *	0	- Success
45680a55fbb7Slm66018  *	ENXIO	- Invalid buffer passed in
45690a55fbb7Slm66018  *	EFAULT	- ddi_copyin of data failed
45700a55fbb7Slm66018  */
45710a55fbb7Slm66018 static int
4572d10e4ef2Snarayan vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
45730a55fbb7Slm66018 {
45740a55fbb7Slm66018 	void		*tmp_mem = NULL;
45750a55fbb7Slm66018 	struct vtoc	vt;
45760a55fbb7Slm66018 	struct vtoc	*vtp = &vt;
45770a55fbb7Slm66018 	vd_vtoc_t	vtvd;
45780a55fbb7Slm66018 	int		copy_len = 0;
45790a55fbb7Slm66018 	int		rv = 0;
45800a55fbb7Slm66018 
45810a55fbb7Slm66018 	if (dir != VD_COPYIN)
45820a55fbb7Slm66018 		return (0);	/* nothing to do */
45830a55fbb7Slm66018 
45840a55fbb7Slm66018 	if ((from == NULL) || (to == NULL))
45850a55fbb7Slm66018 		return (ENXIO);
45860a55fbb7Slm66018 
45870a55fbb7Slm66018 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32)
45880a55fbb7Slm66018 		copy_len = sizeof (struct vtoc32);
45890a55fbb7Slm66018 	else
45900a55fbb7Slm66018 		copy_len = sizeof (struct vtoc);
45910a55fbb7Slm66018 
45920a55fbb7Slm66018 	tmp_mem = kmem_alloc(copy_len, KM_SLEEP);
45930a55fbb7Slm66018 
45940a55fbb7Slm66018 	rv = ddi_copyin(from, tmp_mem, copy_len, mode);
45950a55fbb7Slm66018 	if (rv != 0) {
45960a55fbb7Slm66018 		kmem_free(tmp_mem, copy_len);
45970a55fbb7Slm66018 		return (EFAULT);
45980a55fbb7Slm66018 	}
45990a55fbb7Slm66018 
46000a55fbb7Slm66018 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
46010a55fbb7Slm66018 		vtoc32tovtoc((*(struct vtoc32 *)tmp_mem), vt);
46020a55fbb7Slm66018 	} else {
46030a55fbb7Slm66018 		vtp = tmp_mem;
46040a55fbb7Slm66018 	}
46050a55fbb7Slm66018 
4606d10e4ef2Snarayan 	/*
4607d10e4ef2Snarayan 	 * The VTOC is being changed, then vdc needs to update the copy
4608d10e4ef2Snarayan 	 * it saved in the soft state structure.
4609d10e4ef2Snarayan 	 */
4610d10e4ef2Snarayan 	bcopy(vtp, vdc->vtoc, sizeof (struct vtoc));
4611d10e4ef2Snarayan 
46120a55fbb7Slm66018 	VTOC2VD_VTOC(vtp, &vtvd);
46130a55fbb7Slm66018 	bcopy(&vtvd, to, sizeof (vd_vtoc_t));
46140a55fbb7Slm66018 	kmem_free(tmp_mem, copy_len);
46150a55fbb7Slm66018 
46160a55fbb7Slm66018 	return (0);
46170a55fbb7Slm66018 }
46180a55fbb7Slm66018 
46190a55fbb7Slm66018 /*
46200a55fbb7Slm66018  * Function:
46210a55fbb7Slm66018  *	vdc_get_geom_convert()
46220a55fbb7Slm66018  *
46230a55fbb7Slm66018  * Description:
4624d10e4ef2Snarayan  *	This routine performs the necessary convertions from the DKIOCGGEOM,
4625d10e4ef2Snarayan  *	DKIOCG_PHYSGEOM and DKIOG_VIRTGEOM Solaris structures to the format
4626d10e4ef2Snarayan  *	defined in FWARC 2006/195
46270a55fbb7Slm66018  *
46280a55fbb7Slm66018  * Arguments:
4629d10e4ef2Snarayan  *	vdc	- the vDisk client
46300a55fbb7Slm66018  *	from	- Buffer with data
46310a55fbb7Slm66018  *	to	- Buffer where data is to be copied to
46320a55fbb7Slm66018  *	mode	- flags passed to ioctl
46330a55fbb7Slm66018  *	dir	- direction of copy (in or out)
46340a55fbb7Slm66018  *
46350a55fbb7Slm66018  * Return Code:
46360a55fbb7Slm66018  *	0	- Success
46370a55fbb7Slm66018  *	ENXIO	- Invalid buffer passed in
4638d10e4ef2Snarayan  *	EFAULT	- ddi_copyout of data failed
46390a55fbb7Slm66018  */
46400a55fbb7Slm66018 static int
4641d10e4ef2Snarayan vdc_get_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
46420a55fbb7Slm66018 {
4643d10e4ef2Snarayan 	_NOTE(ARGUNUSED(vdc))
4644d10e4ef2Snarayan 
46450a55fbb7Slm66018 	struct dk_geom	geom;
46460a55fbb7Slm66018 	int	copy_len = sizeof (struct dk_geom);
46470a55fbb7Slm66018 	int	rv = 0;
46480a55fbb7Slm66018 
46490a55fbb7Slm66018 	if (dir != VD_COPYOUT)
46500a55fbb7Slm66018 		return (0);	/* nothing to do */
46510a55fbb7Slm66018 
46520a55fbb7Slm66018 	if ((from == NULL) || (to == NULL))
46530a55fbb7Slm66018 		return (ENXIO);
46540a55fbb7Slm66018 
46550a55fbb7Slm66018 	VD_GEOM2DK_GEOM((vd_geom_t *)from, &geom);
46560a55fbb7Slm66018 	rv = ddi_copyout(&geom, to, copy_len, mode);
46570a55fbb7Slm66018 	if (rv != 0)
46580a55fbb7Slm66018 		rv = EFAULT;
46590a55fbb7Slm66018 
46600a55fbb7Slm66018 	return (rv);
46610a55fbb7Slm66018 }
46620a55fbb7Slm66018 
46630a55fbb7Slm66018 /*
46640a55fbb7Slm66018  * Function:
46650a55fbb7Slm66018  *	vdc_set_geom_convert()
46660a55fbb7Slm66018  *
46670a55fbb7Slm66018  * Description:
4668d10e4ef2Snarayan  *	This routine performs the necessary convertions from the DKIOCSGEOM
4669d10e4ef2Snarayan  *	Solaris structure to the format defined in FWARC 2006/195.
46700a55fbb7Slm66018  *
46710a55fbb7Slm66018  * Arguments:
4672d10e4ef2Snarayan  *	vdc	- the vDisk client
46730a55fbb7Slm66018  *	from	- Buffer with data
46740a55fbb7Slm66018  *	to	- Buffer where data is to be copied to
46750a55fbb7Slm66018  *	mode	- flags passed to ioctl
46760a55fbb7Slm66018  *	dir	- direction of copy (in or out)
46770a55fbb7Slm66018  *
46780a55fbb7Slm66018  * Return Code:
46790a55fbb7Slm66018  *	0	- Success
46800a55fbb7Slm66018  *	ENXIO	- Invalid buffer passed in
46810a55fbb7Slm66018  *	EFAULT	- ddi_copyin of data failed
46820a55fbb7Slm66018  */
46830a55fbb7Slm66018 static int
4684d10e4ef2Snarayan vdc_set_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
46850a55fbb7Slm66018 {
4686d10e4ef2Snarayan 	_NOTE(ARGUNUSED(vdc))
4687d10e4ef2Snarayan 
46880a55fbb7Slm66018 	vd_geom_t	vdgeom;
46890a55fbb7Slm66018 	void		*tmp_mem = NULL;
46900a55fbb7Slm66018 	int		copy_len = sizeof (struct dk_geom);
46910a55fbb7Slm66018 	int		rv = 0;
46920a55fbb7Slm66018 
46930a55fbb7Slm66018 	if (dir != VD_COPYIN)
46940a55fbb7Slm66018 		return (0);	/* nothing to do */
46950a55fbb7Slm66018 
46960a55fbb7Slm66018 	if ((from == NULL) || (to == NULL))
46970a55fbb7Slm66018 		return (ENXIO);
46980a55fbb7Slm66018 
46990a55fbb7Slm66018 	tmp_mem = kmem_alloc(copy_len, KM_SLEEP);
47000a55fbb7Slm66018 
47010a55fbb7Slm66018 	rv = ddi_copyin(from, tmp_mem, copy_len, mode);
47020a55fbb7Slm66018 	if (rv != 0) {
47030a55fbb7Slm66018 		kmem_free(tmp_mem, copy_len);
47040a55fbb7Slm66018 		return (EFAULT);
47050a55fbb7Slm66018 	}
47060a55fbb7Slm66018 	DK_GEOM2VD_GEOM((struct dk_geom *)tmp_mem, &vdgeom);
47070a55fbb7Slm66018 	bcopy(&vdgeom, to, sizeof (vdgeom));
47080a55fbb7Slm66018 	kmem_free(tmp_mem, copy_len);
47090a55fbb7Slm66018 
47100a55fbb7Slm66018 	return (0);
47110a55fbb7Slm66018 }
47120a55fbb7Slm66018 
47134bac2208Snarayan static int
47144bac2208Snarayan vdc_get_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
47154bac2208Snarayan {
47164bac2208Snarayan 	_NOTE(ARGUNUSED(vdc))
47174bac2208Snarayan 
47184bac2208Snarayan 	vd_efi_t	*vd_efi;
47194bac2208Snarayan 	dk_efi_t	dk_efi;
47204bac2208Snarayan 	int		rv = 0;
47214bac2208Snarayan 	void		*uaddr;
47224bac2208Snarayan 
47234bac2208Snarayan 	if ((from == NULL) || (to == NULL))
47244bac2208Snarayan 		return (ENXIO);
47254bac2208Snarayan 
47264bac2208Snarayan 	if (dir == VD_COPYIN) {
47274bac2208Snarayan 
47284bac2208Snarayan 		vd_efi = (vd_efi_t *)to;
47294bac2208Snarayan 
47304bac2208Snarayan 		rv = ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode);
47314bac2208Snarayan 		if (rv != 0)
47324bac2208Snarayan 			return (EFAULT);
47334bac2208Snarayan 
47344bac2208Snarayan 		vd_efi->lba = dk_efi.dki_lba;
47354bac2208Snarayan 		vd_efi->length = dk_efi.dki_length;
47364bac2208Snarayan 		bzero(vd_efi->data, vd_efi->length);
47374bac2208Snarayan 
47384bac2208Snarayan 	} else {
47394bac2208Snarayan 
47404bac2208Snarayan 		rv = ddi_copyin(to, &dk_efi, sizeof (dk_efi_t), mode);
47414bac2208Snarayan 		if (rv != 0)
47424bac2208Snarayan 			return (EFAULT);
47434bac2208Snarayan 
47444bac2208Snarayan 		uaddr = dk_efi.dki_data;
47454bac2208Snarayan 
47464bac2208Snarayan 		dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP);
47474bac2208Snarayan 
47484bac2208Snarayan 		VD_EFI2DK_EFI((vd_efi_t *)from, &dk_efi);
47494bac2208Snarayan 
47504bac2208Snarayan 		rv = ddi_copyout(dk_efi.dki_data, uaddr, dk_efi.dki_length,
47514bac2208Snarayan 		    mode);
47524bac2208Snarayan 		if (rv != 0)
47534bac2208Snarayan 			return (EFAULT);
47544bac2208Snarayan 
47554bac2208Snarayan 		kmem_free(dk_efi.dki_data, dk_efi.dki_length);
47564bac2208Snarayan 	}
47574bac2208Snarayan 
47584bac2208Snarayan 	return (0);
47594bac2208Snarayan }
47604bac2208Snarayan 
47614bac2208Snarayan static int
47624bac2208Snarayan vdc_set_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
47634bac2208Snarayan {
47644bac2208Snarayan 	_NOTE(ARGUNUSED(vdc))
47654bac2208Snarayan 
47664bac2208Snarayan 	dk_efi_t	dk_efi;
47674bac2208Snarayan 	void		*uaddr;
47684bac2208Snarayan 
47694bac2208Snarayan 	if (dir == VD_COPYOUT)
47704bac2208Snarayan 		return (0);	/* nothing to do */
47714bac2208Snarayan 
47724bac2208Snarayan 	if ((from == NULL) || (to == NULL))
47734bac2208Snarayan 		return (ENXIO);
47744bac2208Snarayan 
47754bac2208Snarayan 	if (ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode) != 0)
47764bac2208Snarayan 		return (EFAULT);
47774bac2208Snarayan 
47784bac2208Snarayan 	uaddr = dk_efi.dki_data;
47794bac2208Snarayan 
47804bac2208Snarayan 	dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP);
47814bac2208Snarayan 
47824bac2208Snarayan 	if (ddi_copyin(uaddr, dk_efi.dki_data, dk_efi.dki_length, mode) != 0)
47834bac2208Snarayan 		return (EFAULT);
47844bac2208Snarayan 
47854bac2208Snarayan 	DK_EFI2VD_EFI(&dk_efi, (vd_efi_t *)to);
47864bac2208Snarayan 
47874bac2208Snarayan 	kmem_free(dk_efi.dki_data, dk_efi.dki_length);
47884bac2208Snarayan 
47894bac2208Snarayan 	return (0);
47904bac2208Snarayan }
47914bac2208Snarayan 
47920a55fbb7Slm66018 /*
47930a55fbb7Slm66018  * Function:
47941ae08745Sheppo  *	vdc_create_fake_geometry()
47951ae08745Sheppo  *
47961ae08745Sheppo  * Description:
47971ae08745Sheppo  *	This routine fakes up the disk info needed for some DKIO ioctls.
47981ae08745Sheppo  *		- DKIOCINFO
47991ae08745Sheppo  *		- DKIOCGMEDIAINFO
48001ae08745Sheppo  *
48011ae08745Sheppo  *	[ just like lofi(7D) and ramdisk(7D) ]
48021ae08745Sheppo  *
48031ae08745Sheppo  * Arguments:
48041ae08745Sheppo  *	vdc	- soft state pointer for this instance of the device driver.
48051ae08745Sheppo  *
48061ae08745Sheppo  * Return Code:
48071ae08745Sheppo  *	0	- Success
48081ae08745Sheppo  */
48091ae08745Sheppo static int
48101ae08745Sheppo vdc_create_fake_geometry(vdc_t *vdc)
48111ae08745Sheppo {
48121ae08745Sheppo 	ASSERT(vdc != NULL);
48131ae08745Sheppo 
48141ae08745Sheppo 	/*
48150d0c8d4bSnarayan 	 * Check if max_xfer_sz and vdisk_size are valid
48160d0c8d4bSnarayan 	 */
48170d0c8d4bSnarayan 	if (vdc->vdisk_size == 0 || vdc->max_xfer_sz == 0)
48180d0c8d4bSnarayan 		return (EIO);
48190d0c8d4bSnarayan 
48200d0c8d4bSnarayan 	/*
48211ae08745Sheppo 	 * DKIOCINFO support
48221ae08745Sheppo 	 */
48231ae08745Sheppo 	vdc->cinfo = kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
48241ae08745Sheppo 
48251ae08745Sheppo 	(void) strcpy(vdc->cinfo->dki_cname, VDC_DRIVER_NAME);
48261ae08745Sheppo 	(void) strcpy(vdc->cinfo->dki_dname, VDC_DRIVER_NAME);
48278e6a2a04Slm66018 	/* max_xfer_sz is #blocks so we don't need to divide by DEV_BSIZE */
48288e6a2a04Slm66018 	vdc->cinfo->dki_maxtransfer = vdc->max_xfer_sz;
48291ae08745Sheppo 	vdc->cinfo->dki_ctype = DKC_SCSI_CCS;
48301ae08745Sheppo 	vdc->cinfo->dki_flags = DKI_FMTVOL;
48311ae08745Sheppo 	vdc->cinfo->dki_cnum = 0;
48321ae08745Sheppo 	vdc->cinfo->dki_addr = 0;
48331ae08745Sheppo 	vdc->cinfo->dki_space = 0;
48341ae08745Sheppo 	vdc->cinfo->dki_prio = 0;
48351ae08745Sheppo 	vdc->cinfo->dki_vec = 0;
48361ae08745Sheppo 	vdc->cinfo->dki_unit = vdc->instance;
48371ae08745Sheppo 	vdc->cinfo->dki_slave = 0;
48381ae08745Sheppo 	/*
48391ae08745Sheppo 	 * The partition number will be created on the fly depending on the
48401ae08745Sheppo 	 * actual slice (i.e. minor node) that is used to request the data.
48411ae08745Sheppo 	 */
48421ae08745Sheppo 	vdc->cinfo->dki_partition = 0;
48431ae08745Sheppo 
48441ae08745Sheppo 	/*
48451ae08745Sheppo 	 * DKIOCGMEDIAINFO support
48461ae08745Sheppo 	 */
48470a55fbb7Slm66018 	if (vdc->minfo == NULL)
48481ae08745Sheppo 		vdc->minfo = kmem_zalloc(sizeof (struct dk_minfo), KM_SLEEP);
48491ae08745Sheppo 	vdc->minfo->dki_media_type = DK_FIXED_DISK;
48504bac2208Snarayan 	vdc->minfo->dki_capacity = vdc->vdisk_size;
48511ae08745Sheppo 	vdc->minfo->dki_lbsize = DEV_BSIZE;
48521ae08745Sheppo 
48530d0c8d4bSnarayan 	return (0);
48540a55fbb7Slm66018 }
48550a55fbb7Slm66018 
48560a55fbb7Slm66018 /*
48570a55fbb7Slm66018  * Function:
48580a55fbb7Slm66018  *	vdc_setup_disk_layout()
48590a55fbb7Slm66018  *
48600a55fbb7Slm66018  * Description:
48610a55fbb7Slm66018  *	This routine discovers all the necessary details about the "disk"
48620a55fbb7Slm66018  *	by requesting the data that is available from the vDisk server and by
48630a55fbb7Slm66018  *	faking up the rest of the data.
48640a55fbb7Slm66018  *
48650a55fbb7Slm66018  * Arguments:
48660a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
48670a55fbb7Slm66018  *
48680a55fbb7Slm66018  * Return Code:
48690a55fbb7Slm66018  *	0	- Success
48700a55fbb7Slm66018  */
48710a55fbb7Slm66018 static int
48720a55fbb7Slm66018 vdc_setup_disk_layout(vdc_t *vdc)
48730a55fbb7Slm66018 {
4874d10e4ef2Snarayan 	buf_t	*buf;	/* BREAD requests need to be in a buf_t structure */
48750a55fbb7Slm66018 	dev_t	dev;
48760a55fbb7Slm66018 	int	slice = 0;
48770d0c8d4bSnarayan 	int	rv, error;
48780a55fbb7Slm66018 
48790a55fbb7Slm66018 	ASSERT(vdc != NULL);
48800a55fbb7Slm66018 
48810a55fbb7Slm66018 	if (vdc->vtoc == NULL)
48820a55fbb7Slm66018 		vdc->vtoc = kmem_zalloc(sizeof (struct vtoc), KM_SLEEP);
48830a55fbb7Slm66018 
48840a55fbb7Slm66018 	dev = makedevice(ddi_driver_major(vdc->dip),
48850a55fbb7Slm66018 				VD_MAKE_DEV(vdc->instance, 0));
48860a55fbb7Slm66018 	rv = vd_process_ioctl(dev, DKIOCGVTOC, (caddr_t)vdc->vtoc, FKIOCTL);
48874bac2208Snarayan 
48884bac2208Snarayan 	if (rv && rv != ENOTSUP) {
48893af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to get VTOC (err=%d)",
48900a55fbb7Slm66018 				vdc->instance, rv);
48910a55fbb7Slm66018 		return (rv);
48920a55fbb7Slm66018 	}
48930a55fbb7Slm66018 
48940d0c8d4bSnarayan 	/*
48950d0c8d4bSnarayan 	 * The process of attempting to read VTOC will initiate
48960d0c8d4bSnarayan 	 * the handshake and establish a connection. Following
48970d0c8d4bSnarayan 	 * handshake, go ahead and create geometry.
48980d0c8d4bSnarayan 	 */
48990d0c8d4bSnarayan 	error = vdc_create_fake_geometry(vdc);
49000d0c8d4bSnarayan 	if (error != 0) {
49010d0c8d4bSnarayan 		DMSG(vdc, 0, "[%d] Failed to create disk geometry (err%d)",
49020d0c8d4bSnarayan 		    vdc->instance, error);
49030d0c8d4bSnarayan 		return (error);
49040d0c8d4bSnarayan 	}
49050d0c8d4bSnarayan 
49064bac2208Snarayan 	if (rv == ENOTSUP) {
49074bac2208Snarayan 		/*
49084bac2208Snarayan 		 * If the device does not support VTOC then we try
49094bac2208Snarayan 		 * to read an EFI label.
49104bac2208Snarayan 		 */
49114bac2208Snarayan 		struct dk_gpt *efi;
49124bac2208Snarayan 		size_t efi_len;
49134bac2208Snarayan 
49144bac2208Snarayan 		rv = vdc_efi_alloc_and_read(dev, &efi, &efi_len);
49154bac2208Snarayan 
49164bac2208Snarayan 		if (rv) {
49173af08d82Slm66018 			DMSG(vdc, 0, "[%d] Failed to get EFI (err=%d)",
49184bac2208Snarayan 			    vdc->instance, rv);
49194bac2208Snarayan 			return (rv);
49204bac2208Snarayan 		}
49214bac2208Snarayan 
49224bac2208Snarayan 		vdc->vdisk_label = VD_DISK_LABEL_EFI;
49234bac2208Snarayan 		vdc_store_efi(vdc, efi);
49244bac2208Snarayan 		vd_efi_free(efi, efi_len);
49254bac2208Snarayan 
49264bac2208Snarayan 		return (0);
49274bac2208Snarayan 	}
49284bac2208Snarayan 
49294bac2208Snarayan 	vdc->vdisk_label = VD_DISK_LABEL_VTOC;
49304bac2208Snarayan 
49310a55fbb7Slm66018 	/*
49323af08d82Slm66018 	 * FUTURE: This could be default way for reading the VTOC
49333af08d82Slm66018 	 * from the disk as supposed to sending the VD_OP_GET_VTOC
49343af08d82Slm66018 	 * to the server. Currently this is a sanity check.
49353af08d82Slm66018 	 *
49360a55fbb7Slm66018 	 * find the slice that represents the entire "disk" and use that to
49370a55fbb7Slm66018 	 * read the disk label. The convention in Solaris is that slice 2
4938d10e4ef2Snarayan 	 * represents the whole disk so we check that it is, otherwise we
49390a55fbb7Slm66018 	 * default to slice 0
49400a55fbb7Slm66018 	 */
49410a55fbb7Slm66018 	if ((vdc->vdisk_type == VD_DISK_TYPE_DISK) &&
49420a55fbb7Slm66018 	    (vdc->vtoc->v_part[2].p_tag == V_BACKUP)) {
49430a55fbb7Slm66018 		slice = 2;
49440a55fbb7Slm66018 	} else {
49450a55fbb7Slm66018 		slice = 0;
49460a55fbb7Slm66018 	}
4947d10e4ef2Snarayan 
4948d10e4ef2Snarayan 	/*
4949d10e4ef2Snarayan 	 * Read disk label from start of disk
4950d10e4ef2Snarayan 	 */
4951d10e4ef2Snarayan 	vdc->label = kmem_zalloc(DK_LABEL_SIZE, KM_SLEEP);
4952d10e4ef2Snarayan 	buf = kmem_alloc(sizeof (buf_t), KM_SLEEP);
4953d10e4ef2Snarayan 	bioinit(buf);
4954d10e4ef2Snarayan 	buf->b_un.b_addr = (caddr_t)vdc->label;
4955d10e4ef2Snarayan 	buf->b_bcount = DK_LABEL_SIZE;
4956d10e4ef2Snarayan 	buf->b_flags = B_BUSY | B_READ;
4957d10e4ef2Snarayan 	buf->b_dev = dev;
49583af08d82Slm66018 	rv = vdc_send_request(vdc, VD_OP_BREAD, (caddr_t)vdc->label,
49593af08d82Slm66018 	    DK_LABEL_SIZE, slice, 0, CB_STRATEGY, buf, VIO_read_dir);
49603af08d82Slm66018 	if (rv) {
49613af08d82Slm66018 		DMSG(vdc, 1, "[%d] Failed to read disk block 0\n",
49623af08d82Slm66018 		    vdc->instance);
49633af08d82Slm66018 		kmem_free(buf, sizeof (buf_t));
49643af08d82Slm66018 		return (rv);
49653af08d82Slm66018 	}
4966d10e4ef2Snarayan 	rv = biowait(buf);
4967d10e4ef2Snarayan 	biofini(buf);
4968d10e4ef2Snarayan 	kmem_free(buf, sizeof (buf_t));
49690a55fbb7Slm66018 
49700a55fbb7Slm66018 	return (rv);
49711ae08745Sheppo }
49724bac2208Snarayan 
49734bac2208Snarayan /*
49744bac2208Snarayan  * Function:
49754bac2208Snarayan  *	vdc_setup_devid()
49764bac2208Snarayan  *
49774bac2208Snarayan  * Description:
49784bac2208Snarayan  *	This routine discovers the devid of a vDisk. It requests the devid of
49794bac2208Snarayan  *	the underlying device from the vDisk server, builds an encapsulated
49804bac2208Snarayan  *	devid based on the retrieved devid and registers that new devid to
49814bac2208Snarayan  *	the vDisk.
49824bac2208Snarayan  *
49834bac2208Snarayan  * Arguments:
49844bac2208Snarayan  *	vdc	- soft state pointer for this instance of the device driver.
49854bac2208Snarayan  *
49864bac2208Snarayan  * Return Code:
49874bac2208Snarayan  *	0	- A devid was succesfully registered for the vDisk
49884bac2208Snarayan  */
49894bac2208Snarayan static int
49904bac2208Snarayan vdc_setup_devid(vdc_t *vdc)
49914bac2208Snarayan {
49924bac2208Snarayan 	int rv;
49934bac2208Snarayan 	vd_devid_t *vd_devid;
49944bac2208Snarayan 	size_t bufsize, bufid_len;
49954bac2208Snarayan 
49964bac2208Snarayan 	/*
49974bac2208Snarayan 	 * At first sight, we don't know the size of the devid that the
49984bac2208Snarayan 	 * server will return but this size will be encoded into the
49994bac2208Snarayan 	 * reply. So we do a first request using a default size then we
50004bac2208Snarayan 	 * check if this size was large enough. If not then we do a second
50014bac2208Snarayan 	 * request with the correct size returned by the server. Note that
50024bac2208Snarayan 	 * ldc requires size to be 8-byte aligned.
50034bac2208Snarayan 	 */
50044bac2208Snarayan 	bufsize = P2ROUNDUP(VD_DEVID_SIZE(VD_DEVID_DEFAULT_LEN),
50054bac2208Snarayan 	    sizeof (uint64_t));
50064bac2208Snarayan 	vd_devid = kmem_zalloc(bufsize, KM_SLEEP);
50074bac2208Snarayan 	bufid_len = bufsize - sizeof (vd_efi_t) - 1;
50084bac2208Snarayan 
50093af08d82Slm66018 	rv = vdc_do_sync_op(vdc, VD_OP_GET_DEVID, (caddr_t)vd_devid,
50103af08d82Slm66018 	    bufsize, 0, 0, CB_SYNC, 0, VIO_both_dir);
50113af08d82Slm66018 
50123af08d82Slm66018 	DMSG(vdc, 2, "sync_op returned %d\n", rv);
50133af08d82Slm66018 
50144bac2208Snarayan 	if (rv) {
50154bac2208Snarayan 		kmem_free(vd_devid, bufsize);
50164bac2208Snarayan 		return (rv);
50174bac2208Snarayan 	}
50184bac2208Snarayan 
50194bac2208Snarayan 	if (vd_devid->length > bufid_len) {
50204bac2208Snarayan 		/*
50214bac2208Snarayan 		 * The returned devid is larger than the buffer used. Try again
50224bac2208Snarayan 		 * with a buffer with the right size.
50234bac2208Snarayan 		 */
50244bac2208Snarayan 		kmem_free(vd_devid, bufsize);
50254bac2208Snarayan 		bufsize = P2ROUNDUP(VD_DEVID_SIZE(vd_devid->length),
50264bac2208Snarayan 		    sizeof (uint64_t));
50274bac2208Snarayan 		vd_devid = kmem_zalloc(bufsize, KM_SLEEP);
50284bac2208Snarayan 		bufid_len = bufsize - sizeof (vd_efi_t) - 1;
50294bac2208Snarayan 
50303af08d82Slm66018 		rv = vdc_do_sync_op(vdc, VD_OP_GET_DEVID,
50313af08d82Slm66018 		    (caddr_t)vd_devid, bufsize, 0, 0, CB_SYNC, 0,
50323af08d82Slm66018 		    VIO_both_dir);
50333af08d82Slm66018 
50344bac2208Snarayan 		if (rv) {
50354bac2208Snarayan 			kmem_free(vd_devid, bufsize);
50364bac2208Snarayan 			return (rv);
50374bac2208Snarayan 		}
50384bac2208Snarayan 	}
50394bac2208Snarayan 
50404bac2208Snarayan 	/*
50414bac2208Snarayan 	 * The virtual disk should have the same device id as the one associated
50424bac2208Snarayan 	 * with the physical disk it is mapped on, otherwise sharing a disk
50434bac2208Snarayan 	 * between a LDom and a non-LDom may not work (for example for a shared
50444bac2208Snarayan 	 * SVM disk set).
50454bac2208Snarayan 	 *
50464bac2208Snarayan 	 * The DDI framework does not allow creating a device id with any
50474bac2208Snarayan 	 * type so we first create a device id of type DEVID_ENCAP and then
50484bac2208Snarayan 	 * we restore the orignal type of the physical device.
50494bac2208Snarayan 	 */
50504bac2208Snarayan 
50513af08d82Slm66018 	DMSG(vdc, 2, ": devid length = %d\n", vd_devid->length);
50523af08d82Slm66018 
50534bac2208Snarayan 	/* build an encapsulated devid based on the returned devid */
50544bac2208Snarayan 	if (ddi_devid_init(vdc->dip, DEVID_ENCAP, vd_devid->length,
50554bac2208Snarayan 		vd_devid->id, &vdc->devid) != DDI_SUCCESS) {
50563af08d82Slm66018 		DMSG(vdc, 1, "[%d] Fail to created devid\n", vdc->instance);
50574bac2208Snarayan 		kmem_free(vd_devid, bufsize);
50584bac2208Snarayan 		return (1);
50594bac2208Snarayan 	}
50604bac2208Snarayan 
50614bac2208Snarayan 	DEVID_FORMTYPE((impl_devid_t *)vdc->devid, vd_devid->type);
50624bac2208Snarayan 
50634bac2208Snarayan 	ASSERT(ddi_devid_valid(vdc->devid) == DDI_SUCCESS);
50644bac2208Snarayan 
50654bac2208Snarayan 	kmem_free(vd_devid, bufsize);
50664bac2208Snarayan 
50674bac2208Snarayan 	if (ddi_devid_register(vdc->dip, vdc->devid) != DDI_SUCCESS) {
50683af08d82Slm66018 		DMSG(vdc, 1, "[%d] Fail to register devid\n", vdc->instance);
50694bac2208Snarayan 		return (1);
50704bac2208Snarayan 	}
50714bac2208Snarayan 
50724bac2208Snarayan 	return (0);
50734bac2208Snarayan }
50744bac2208Snarayan 
50754bac2208Snarayan static void
50764bac2208Snarayan vdc_store_efi(vdc_t *vdc, struct dk_gpt *efi)
50774bac2208Snarayan {
50784bac2208Snarayan 	struct vtoc *vtoc = vdc->vtoc;
50794bac2208Snarayan 
50804bac2208Snarayan 	vd_efi_to_vtoc(efi, vtoc);
50814bac2208Snarayan 	if (vdc->vdisk_type == VD_DISK_TYPE_SLICE) {
50824bac2208Snarayan 		/*
50834bac2208Snarayan 		 * vd_efi_to_vtoc() will store information about the EFI Sun
50844bac2208Snarayan 		 * reserved partition (representing the entire disk) into
50854bac2208Snarayan 		 * partition 7. However single-slice device will only have
50864bac2208Snarayan 		 * that single partition and the vdc driver expects to find
50874bac2208Snarayan 		 * information about that partition in slice 0. So we need
50884bac2208Snarayan 		 * to copy information from slice 7 to slice 0.
50894bac2208Snarayan 		 */
50904bac2208Snarayan 		vtoc->v_part[0].p_tag = vtoc->v_part[VD_EFI_WD_SLICE].p_tag;
50914bac2208Snarayan 		vtoc->v_part[0].p_flag = vtoc->v_part[VD_EFI_WD_SLICE].p_flag;
50924bac2208Snarayan 		vtoc->v_part[0].p_start = vtoc->v_part[VD_EFI_WD_SLICE].p_start;
50934bac2208Snarayan 		vtoc->v_part[0].p_size =  vtoc->v_part[VD_EFI_WD_SLICE].p_size;
50944bac2208Snarayan 	}
50954bac2208Snarayan }
5096