xref: /titanic_52/usr/src/uts/sun4v/io/vdc.c (revision 8259acd8641e32a891a4ad20a6479c529bb052c3)
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>
8487a7269eSachartre #include <sys/dktp/dadkio.h>
851ae08745Sheppo #include <sys/scsi/generic/sense.h>
861ae08745Sheppo #include <sys/scsi/impl/uscsi.h>	/* Needed for defn of USCSICMD ioctl */
871ae08745Sheppo 
881ae08745Sheppo #include <sys/ldoms.h>
891ae08745Sheppo #include <sys/ldc.h>
901ae08745Sheppo #include <sys/vio_common.h>
911ae08745Sheppo #include <sys/vio_mailbox.h>
921ae08745Sheppo #include <sys/vdsk_common.h>
931ae08745Sheppo #include <sys/vdsk_mailbox.h>
941ae08745Sheppo #include <sys/vdc.h>
951ae08745Sheppo 
961ae08745Sheppo /*
971ae08745Sheppo  * function prototypes
981ae08745Sheppo  */
991ae08745Sheppo 
1001ae08745Sheppo /* standard driver functions */
1011ae08745Sheppo static int	vdc_open(dev_t *dev, int flag, int otyp, cred_t *cred);
1021ae08745Sheppo static int	vdc_close(dev_t dev, int flag, int otyp, cred_t *cred);
1031ae08745Sheppo static int	vdc_strategy(struct buf *buf);
1041ae08745Sheppo static int	vdc_print(dev_t dev, char *str);
1051ae08745Sheppo static int	vdc_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
1061ae08745Sheppo static int	vdc_read(dev_t dev, struct uio *uio, cred_t *cred);
1071ae08745Sheppo static int	vdc_write(dev_t dev, struct uio *uio, cred_t *cred);
1081ae08745Sheppo static int	vdc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
1091ae08745Sheppo 			cred_t *credp, int *rvalp);
1101ae08745Sheppo static int	vdc_aread(dev_t dev, struct aio_req *aio, cred_t *cred);
1111ae08745Sheppo static int	vdc_awrite(dev_t dev, struct aio_req *aio, cred_t *cred);
1121ae08745Sheppo 
1131ae08745Sheppo static int	vdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,
1141ae08745Sheppo 			void *arg, void **resultp);
1151ae08745Sheppo static int	vdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
1161ae08745Sheppo static int	vdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
1171ae08745Sheppo 
1181ae08745Sheppo /* setup */
1190d0c8d4bSnarayan static void	vdc_min(struct buf *bufp);
1200a55fbb7Slm66018 static int	vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen);
121655fd6a9Sachartre static int	vdc_do_ldc_init(vdc_t *vdc, md_t *mdp, mde_cookie_t vd_node);
1221ae08745Sheppo static int	vdc_start_ldc_connection(vdc_t *vdc);
1231ae08745Sheppo static int	vdc_create_device_nodes(vdc_t *vdc);
1244bac2208Snarayan static int	vdc_create_device_nodes_efi(vdc_t *vdc);
1254bac2208Snarayan static int	vdc_create_device_nodes_vtoc(vdc_t *vdc);
1261ae08745Sheppo static int	vdc_create_device_nodes_props(vdc_t *vdc);
127655fd6a9Sachartre static int	vdc_get_md_node(dev_info_t *dip, md_t **mdpp,
128655fd6a9Sachartre 		    mde_cookie_t *vd_nodep, mde_cookie_t *vd_portp);
129655fd6a9Sachartre static int	vdc_get_ldc_id(md_t *, mde_cookie_t, uint64_t *);
1300a55fbb7Slm66018 static int	vdc_do_ldc_up(vdc_t *vdc);
1311ae08745Sheppo static void	vdc_terminate_ldc(vdc_t *vdc);
1321ae08745Sheppo static int	vdc_init_descriptor_ring(vdc_t *vdc);
1331ae08745Sheppo static void	vdc_destroy_descriptor_ring(vdc_t *vdc);
1344bac2208Snarayan static int	vdc_setup_devid(vdc_t *vdc);
13578fcd0a1Sachartre static void	vdc_store_label_efi(vdc_t *vdc, struct dk_gpt *efi);
13678fcd0a1Sachartre static void	vdc_store_label_vtoc(vdc_t *, struct dk_geom *, struct vtoc *);
13778fcd0a1Sachartre static void	vdc_store_label_unk(vdc_t *vdc);
13878fcd0a1Sachartre static boolean_t vdc_is_opened(vdc_t *vdc);
1391ae08745Sheppo 
1401ae08745Sheppo /* handshake with vds */
1410a55fbb7Slm66018 static int		vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver);
1423af08d82Slm66018 static int		vdc_ver_negotiation(vdc_t *vdcp);
1431ae08745Sheppo static int		vdc_init_attr_negotiation(vdc_t *vdc);
1443af08d82Slm66018 static int		vdc_attr_negotiation(vdc_t *vdcp);
1451ae08745Sheppo static int		vdc_init_dring_negotiate(vdc_t *vdc);
1463af08d82Slm66018 static int		vdc_dring_negotiation(vdc_t *vdcp);
1473af08d82Slm66018 static int		vdc_send_rdx(vdc_t *vdcp);
1483af08d82Slm66018 static int		vdc_rdx_exchange(vdc_t *vdcp);
1490a55fbb7Slm66018 static boolean_t	vdc_is_supported_version(vio_ver_msg_t *ver_msg);
1501ae08745Sheppo 
1510a55fbb7Slm66018 /* processing incoming messages from vDisk server */
1521ae08745Sheppo static void	vdc_process_msg_thread(vdc_t *vdc);
1533af08d82Slm66018 static int	vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp);
1543af08d82Slm66018 
1550a55fbb7Slm66018 static uint_t	vdc_handle_cb(uint64_t event, caddr_t arg);
1563af08d82Slm66018 static int	vdc_process_data_msg(vdc_t *vdc, vio_msg_t *msg);
1571ae08745Sheppo static int	vdc_process_err_msg(vdc_t *vdc, vio_msg_t msg);
1580a55fbb7Slm66018 static int	vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg);
1590a55fbb7Slm66018 static int	vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg);
1600a55fbb7Slm66018 static int	vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *msg);
1613af08d82Slm66018 static int 	vdc_send_request(vdc_t *vdcp, int operation,
1623af08d82Slm66018 		    caddr_t addr, size_t nbytes, int slice, diskaddr_t offset,
1633af08d82Slm66018 		    int cb_type, void *cb_arg, vio_desc_direction_t dir);
1643af08d82Slm66018 static int	vdc_map_to_shared_dring(vdc_t *vdcp, int idx);
1653af08d82Slm66018 static int 	vdc_populate_descriptor(vdc_t *vdcp, int operation,
1663af08d82Slm66018 		    caddr_t addr, size_t nbytes, int slice, diskaddr_t offset,
1673af08d82Slm66018 		    int cb_type, void *cb_arg, vio_desc_direction_t dir);
1683af08d82Slm66018 static int 	vdc_do_sync_op(vdc_t *vdcp, int operation,
1693af08d82Slm66018 		    caddr_t addr, size_t nbytes, int slice, diskaddr_t offset,
1703af08d82Slm66018 		    int cb_type, void *cb_arg, vio_desc_direction_t dir);
1713af08d82Slm66018 
1723af08d82Slm66018 static int	vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp);
1733af08d82Slm66018 static int	vdc_drain_response(vdc_t *vdcp);
1741ae08745Sheppo static int	vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx);
1753af08d82Slm66018 static int	vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep);
176e1ebb9ecSlm66018 static int	vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg);
1771ae08745Sheppo 
1781ae08745Sheppo /* dkio */
1791ae08745Sheppo static int	vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode);
18078fcd0a1Sachartre static void	vdc_create_fake_geometry(vdc_t *vdc);
18178fcd0a1Sachartre static int	vdc_validate_geometry(vdc_t *vdc);
18278fcd0a1Sachartre static void	vdc_validate(vdc_t *vdc);
18378fcd0a1Sachartre static void	vdc_validate_task(void *arg);
184d10e4ef2Snarayan static int	vdc_null_copy_func(vdc_t *vdc, void *from, void *to,
185d10e4ef2Snarayan 		    int mode, int dir);
1864bac2208Snarayan static int	vdc_get_wce_convert(vdc_t *vdc, void *from, void *to,
1874bac2208Snarayan 		    int mode, int dir);
1884bac2208Snarayan static int	vdc_set_wce_convert(vdc_t *vdc, void *from, void *to,
1894bac2208Snarayan 		    int mode, int dir);
190d10e4ef2Snarayan static int	vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to,
191d10e4ef2Snarayan 		    int mode, int dir);
192d10e4ef2Snarayan static int	vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to,
193d10e4ef2Snarayan 		    int mode, int dir);
194d10e4ef2Snarayan static int	vdc_get_geom_convert(vdc_t *vdc, void *from, void *to,
195d10e4ef2Snarayan 		    int mode, int dir);
196d10e4ef2Snarayan static int	vdc_set_geom_convert(vdc_t *vdc, void *from, void *to,
197d10e4ef2Snarayan 		    int mode, int dir);
198d10e4ef2Snarayan static int	vdc_uscsicmd_convert(vdc_t *vdc, void *from, void *to,
199d10e4ef2Snarayan 		    int mode, int dir);
2004bac2208Snarayan static int	vdc_get_efi_convert(vdc_t *vdc, void *from, void *to,
2014bac2208Snarayan 		    int mode, int dir);
2024bac2208Snarayan static int	vdc_set_efi_convert(vdc_t *vdc, void *from, void *to,
2034bac2208Snarayan 		    int mode, int dir);
2041ae08745Sheppo 
2051ae08745Sheppo /*
2061ae08745Sheppo  * Module variables
2071ae08745Sheppo  */
208e1ebb9ecSlm66018 
209e1ebb9ecSlm66018 /*
210e1ebb9ecSlm66018  * Tunable variables to control how long vdc waits before timing out on
211e1ebb9ecSlm66018  * various operations
212e1ebb9ecSlm66018  */
213e1ebb9ecSlm66018 static int	vdc_retries = 10;
2143c96341aSnarayan static int	vdc_hshake_retries = 3;
215e1ebb9ecSlm66018 
216655fd6a9Sachartre static int	vdc_timeout = 0; /* units: seconds */
217655fd6a9Sachartre 
218e1ebb9ecSlm66018 /* calculated from 'vdc_usec_timeout' during attach */
219e1ebb9ecSlm66018 static uint64_t	vdc_hz_timeout;				/* units: Hz */
220e1ebb9ecSlm66018 static uint64_t	vdc_usec_timeout = 30 * MICROSEC;	/* 30s units: ns */
221e1ebb9ecSlm66018 
2223af08d82Slm66018 static uint64_t vdc_hz_min_ldc_delay;
2233af08d82Slm66018 static uint64_t vdc_min_timeout_ldc = 1 * MILLISEC;
2243af08d82Slm66018 static uint64_t vdc_hz_max_ldc_delay;
2253af08d82Slm66018 static uint64_t vdc_max_timeout_ldc = 100 * MILLISEC;
2263af08d82Slm66018 
2273af08d82Slm66018 static uint64_t vdc_ldc_read_init_delay = 1 * MILLISEC;
2283af08d82Slm66018 static uint64_t vdc_ldc_read_max_delay = 100 * MILLISEC;
229e1ebb9ecSlm66018 
230e1ebb9ecSlm66018 /* values for dumping - need to run in a tighter loop */
231e1ebb9ecSlm66018 static uint64_t	vdc_usec_timeout_dump = 100 * MILLISEC;	/* 0.1s units: ns */
232e1ebb9ecSlm66018 static int	vdc_dump_retries = 100;
233e1ebb9ecSlm66018 
234e1ebb9ecSlm66018 /* Count of the number of vdc instances attached */
235e1ebb9ecSlm66018 static volatile uint32_t	vdc_instance_count = 0;
2361ae08745Sheppo 
2371ae08745Sheppo /* Soft state pointer */
2381ae08745Sheppo static void	*vdc_state;
2391ae08745Sheppo 
2403af08d82Slm66018 /*
2413af08d82Slm66018  * Controlling the verbosity of the error/debug messages
2423af08d82Slm66018  *
2433af08d82Slm66018  * vdc_msglevel - controls level of messages
2443af08d82Slm66018  * vdc_matchinst - 64-bit variable where each bit corresponds
2453af08d82Slm66018  *                 to the vdc instance the vdc_msglevel applies.
2463af08d82Slm66018  */
2473af08d82Slm66018 int		vdc_msglevel = 0x0;
2483af08d82Slm66018 uint64_t	vdc_matchinst = 0ull;
2491ae08745Sheppo 
2500a55fbb7Slm66018 /*
2510a55fbb7Slm66018  * Supported vDisk protocol version pairs.
2520a55fbb7Slm66018  *
2530a55fbb7Slm66018  * The first array entry is the latest and preferred version.
2540a55fbb7Slm66018  */
2550a55fbb7Slm66018 static const vio_ver_t	vdc_version[] = {{1, 0}};
2561ae08745Sheppo 
2571ae08745Sheppo static struct cb_ops vdc_cb_ops = {
2581ae08745Sheppo 	vdc_open,	/* cb_open */
2591ae08745Sheppo 	vdc_close,	/* cb_close */
2601ae08745Sheppo 	vdc_strategy,	/* cb_strategy */
2611ae08745Sheppo 	vdc_print,	/* cb_print */
2621ae08745Sheppo 	vdc_dump,	/* cb_dump */
2631ae08745Sheppo 	vdc_read,	/* cb_read */
2641ae08745Sheppo 	vdc_write,	/* cb_write */
2651ae08745Sheppo 	vdc_ioctl,	/* cb_ioctl */
2661ae08745Sheppo 	nodev,		/* cb_devmap */
2671ae08745Sheppo 	nodev,		/* cb_mmap */
2681ae08745Sheppo 	nodev,		/* cb_segmap */
2691ae08745Sheppo 	nochpoll,	/* cb_chpoll */
2701ae08745Sheppo 	ddi_prop_op,	/* cb_prop_op */
2711ae08745Sheppo 	NULL,		/* cb_str */
2721ae08745Sheppo 	D_MP | D_64BIT,	/* cb_flag */
2731ae08745Sheppo 	CB_REV,		/* cb_rev */
2741ae08745Sheppo 	vdc_aread,	/* cb_aread */
2751ae08745Sheppo 	vdc_awrite	/* cb_awrite */
2761ae08745Sheppo };
2771ae08745Sheppo 
2781ae08745Sheppo static struct dev_ops vdc_ops = {
2791ae08745Sheppo 	DEVO_REV,	/* devo_rev */
2801ae08745Sheppo 	0,		/* devo_refcnt */
2811ae08745Sheppo 	vdc_getinfo,	/* devo_getinfo */
2821ae08745Sheppo 	nulldev,	/* devo_identify */
2831ae08745Sheppo 	nulldev,	/* devo_probe */
2841ae08745Sheppo 	vdc_attach,	/* devo_attach */
2851ae08745Sheppo 	vdc_detach,	/* devo_detach */
2861ae08745Sheppo 	nodev,		/* devo_reset */
2871ae08745Sheppo 	&vdc_cb_ops,	/* devo_cb_ops */
2881ae08745Sheppo 	NULL,		/* devo_bus_ops */
2891ae08745Sheppo 	nulldev		/* devo_power */
2901ae08745Sheppo };
2911ae08745Sheppo 
2921ae08745Sheppo static struct modldrv modldrv = {
2931ae08745Sheppo 	&mod_driverops,
294205eeb1aSlm66018 	"virtual disk client",
2951ae08745Sheppo 	&vdc_ops,
2961ae08745Sheppo };
2971ae08745Sheppo 
2981ae08745Sheppo static struct modlinkage modlinkage = {
2991ae08745Sheppo 	MODREV_1,
3001ae08745Sheppo 	&modldrv,
3011ae08745Sheppo 	NULL
3021ae08745Sheppo };
3031ae08745Sheppo 
3041ae08745Sheppo /* -------------------------------------------------------------------------- */
3051ae08745Sheppo 
3061ae08745Sheppo /*
3071ae08745Sheppo  * Device Driver housekeeping and setup
3081ae08745Sheppo  */
3091ae08745Sheppo 
3101ae08745Sheppo int
3111ae08745Sheppo _init(void)
3121ae08745Sheppo {
3131ae08745Sheppo 	int	status;
3141ae08745Sheppo 
3151ae08745Sheppo 	if ((status = ddi_soft_state_init(&vdc_state, sizeof (vdc_t), 1)) != 0)
3161ae08745Sheppo 		return (status);
3171ae08745Sheppo 	if ((status = mod_install(&modlinkage)) != 0)
3181ae08745Sheppo 		ddi_soft_state_fini(&vdc_state);
3194bac2208Snarayan 	vdc_efi_init(vd_process_ioctl);
3201ae08745Sheppo 	return (status);
3211ae08745Sheppo }
3221ae08745Sheppo 
3231ae08745Sheppo int
3241ae08745Sheppo _info(struct modinfo *modinfop)
3251ae08745Sheppo {
3261ae08745Sheppo 	return (mod_info(&modlinkage, modinfop));
3271ae08745Sheppo }
3281ae08745Sheppo 
3291ae08745Sheppo int
3301ae08745Sheppo _fini(void)
3311ae08745Sheppo {
3321ae08745Sheppo 	int	status;
3331ae08745Sheppo 
3341ae08745Sheppo 	if ((status = mod_remove(&modlinkage)) != 0)
3351ae08745Sheppo 		return (status);
3364bac2208Snarayan 	vdc_efi_fini();
3371ae08745Sheppo 	ddi_soft_state_fini(&vdc_state);
3381ae08745Sheppo 	return (0);
3391ae08745Sheppo }
3401ae08745Sheppo 
3411ae08745Sheppo static int
3421ae08745Sheppo vdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,  void *arg, void **resultp)
3431ae08745Sheppo {
3441ae08745Sheppo 	_NOTE(ARGUNUSED(dip))
3451ae08745Sheppo 
3460d0c8d4bSnarayan 	int	instance = VDCUNIT((dev_t)arg);
3471ae08745Sheppo 	vdc_t	*vdc = NULL;
3481ae08745Sheppo 
3491ae08745Sheppo 	switch (cmd) {
3501ae08745Sheppo 	case DDI_INFO_DEVT2DEVINFO:
3511ae08745Sheppo 		if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
3521ae08745Sheppo 			*resultp = NULL;
3531ae08745Sheppo 			return (DDI_FAILURE);
3541ae08745Sheppo 		}
3551ae08745Sheppo 		*resultp = vdc->dip;
3561ae08745Sheppo 		return (DDI_SUCCESS);
3571ae08745Sheppo 	case DDI_INFO_DEVT2INSTANCE:
3581ae08745Sheppo 		*resultp = (void *)(uintptr_t)instance;
3591ae08745Sheppo 		return (DDI_SUCCESS);
3601ae08745Sheppo 	default:
3611ae08745Sheppo 		*resultp = NULL;
3621ae08745Sheppo 		return (DDI_FAILURE);
3631ae08745Sheppo 	}
3641ae08745Sheppo }
3651ae08745Sheppo 
3661ae08745Sheppo static int
3671ae08745Sheppo vdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3681ae08745Sheppo {
3691ae08745Sheppo 	int	instance;
3701ae08745Sheppo 	int	rv;
3711ae08745Sheppo 	vdc_t	*vdc = NULL;
3721ae08745Sheppo 
3731ae08745Sheppo 	switch (cmd) {
3741ae08745Sheppo 	case DDI_DETACH:
3751ae08745Sheppo 		/* the real work happens below */
3761ae08745Sheppo 		break;
3771ae08745Sheppo 	case DDI_SUSPEND:
3781ae08745Sheppo 		/* nothing to do for this non-device */
3791ae08745Sheppo 		return (DDI_SUCCESS);
3801ae08745Sheppo 	default:
3811ae08745Sheppo 		return (DDI_FAILURE);
3821ae08745Sheppo 	}
3831ae08745Sheppo 
3841ae08745Sheppo 	ASSERT(cmd == DDI_DETACH);
3851ae08745Sheppo 	instance = ddi_get_instance(dip);
3863af08d82Slm66018 	DMSGX(1, "[%d] Entered\n", instance);
3871ae08745Sheppo 
3881ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
389e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
3901ae08745Sheppo 		return (DDI_FAILURE);
3911ae08745Sheppo 	}
3921ae08745Sheppo 
39378fcd0a1Sachartre 	if (vdc_is_opened(vdc)) {
3943af08d82Slm66018 		DMSG(vdc, 0, "[%d] Cannot detach: device is open", instance);
3951ae08745Sheppo 		return (DDI_FAILURE);
3961ae08745Sheppo 	}
3971ae08745Sheppo 
39878fcd0a1Sachartre 	if (vdc->dkio_flush_pending) {
39978fcd0a1Sachartre 		DMSG(vdc, 0,
40078fcd0a1Sachartre 		    "[%d] Cannot detach: %d outstanding DKIO flushes\n",
40178fcd0a1Sachartre 		    instance, vdc->dkio_flush_pending);
40278fcd0a1Sachartre 		return (DDI_FAILURE);
40378fcd0a1Sachartre 	}
40478fcd0a1Sachartre 
40578fcd0a1Sachartre 	if (vdc->validate_pending) {
40678fcd0a1Sachartre 		DMSG(vdc, 0,
40778fcd0a1Sachartre 		    "[%d] Cannot detach: %d outstanding validate request\n",
40878fcd0a1Sachartre 		    instance, vdc->validate_pending);
40978fcd0a1Sachartre 		return (DDI_FAILURE);
41078fcd0a1Sachartre 	}
41178fcd0a1Sachartre 
4123af08d82Slm66018 	DMSG(vdc, 0, "[%d] proceeding...\n", instance);
4133af08d82Slm66018 
4143af08d82Slm66018 	/* mark instance as detaching */
4153af08d82Slm66018 	vdc->lifecycle	= VDC_LC_DETACHING;
4161ae08745Sheppo 
4171ae08745Sheppo 	/*
4181ae08745Sheppo 	 * try and disable callbacks to prevent another handshake
4191ae08745Sheppo 	 */
4201ae08745Sheppo 	rv = ldc_set_cb_mode(vdc->ldc_handle, LDC_CB_DISABLE);
4213af08d82Slm66018 	DMSG(vdc, 0, "callback disabled (rv=%d)\n", rv);
4221ae08745Sheppo 
4231ae08745Sheppo 	if (vdc->initialized & VDC_THREAD) {
4243af08d82Slm66018 		mutex_enter(&vdc->read_lock);
4253af08d82Slm66018 		if ((vdc->read_state == VDC_READ_WAITING) ||
4263af08d82Slm66018 		    (vdc->read_state == VDC_READ_RESET)) {
4273af08d82Slm66018 			vdc->read_state = VDC_READ_RESET;
4283af08d82Slm66018 			cv_signal(&vdc->read_cv);
4291ae08745Sheppo 		}
4303af08d82Slm66018 
4313af08d82Slm66018 		mutex_exit(&vdc->read_lock);
4323af08d82Slm66018 
4333af08d82Slm66018 		/* wake up any thread waiting for connection to come online */
4343af08d82Slm66018 		mutex_enter(&vdc->lock);
4353af08d82Slm66018 		if (vdc->state == VDC_STATE_INIT_WAITING) {
4363af08d82Slm66018 			DMSG(vdc, 0,
4373af08d82Slm66018 			    "[%d] write reset - move to resetting state...\n",
4383af08d82Slm66018 			    instance);
4393af08d82Slm66018 			vdc->state = VDC_STATE_RESETTING;
4403af08d82Slm66018 			cv_signal(&vdc->initwait_cv);
4413af08d82Slm66018 		}
4423af08d82Slm66018 		mutex_exit(&vdc->lock);
4433af08d82Slm66018 
4443af08d82Slm66018 		/* now wait until state transitions to VDC_STATE_DETACH */
4453af08d82Slm66018 		thread_join(vdc->msg_proc_thr->t_did);
4463af08d82Slm66018 		ASSERT(vdc->state == VDC_STATE_DETACH);
4473af08d82Slm66018 		DMSG(vdc, 0, "[%d] Reset thread exit and join ..\n",
4483af08d82Slm66018 		    vdc->instance);
4491ae08745Sheppo 	}
4501ae08745Sheppo 
4511ae08745Sheppo 	mutex_enter(&vdc->lock);
4521ae08745Sheppo 
4531ae08745Sheppo 	if (vdc->initialized & VDC_DRING)
4541ae08745Sheppo 		vdc_destroy_descriptor_ring(vdc);
4551ae08745Sheppo 
4561ae08745Sheppo 	if (vdc->initialized & VDC_LDC)
4571ae08745Sheppo 		vdc_terminate_ldc(vdc);
4581ae08745Sheppo 
4591ae08745Sheppo 	mutex_exit(&vdc->lock);
4601ae08745Sheppo 
4611ae08745Sheppo 	if (vdc->initialized & VDC_MINOR) {
4621ae08745Sheppo 		ddi_prop_remove_all(dip);
4631ae08745Sheppo 		ddi_remove_minor_node(dip, NULL);
4641ae08745Sheppo 	}
4651ae08745Sheppo 
4661ae08745Sheppo 	if (vdc->initialized & VDC_LOCKS) {
4671ae08745Sheppo 		mutex_destroy(&vdc->lock);
4683af08d82Slm66018 		mutex_destroy(&vdc->read_lock);
4693af08d82Slm66018 		cv_destroy(&vdc->initwait_cv);
4703af08d82Slm66018 		cv_destroy(&vdc->dring_free_cv);
4713af08d82Slm66018 		cv_destroy(&vdc->membind_cv);
4723af08d82Slm66018 		cv_destroy(&vdc->sync_pending_cv);
4733af08d82Slm66018 		cv_destroy(&vdc->sync_blocked_cv);
4743af08d82Slm66018 		cv_destroy(&vdc->read_cv);
4753af08d82Slm66018 		cv_destroy(&vdc->running_cv);
4761ae08745Sheppo 	}
4771ae08745Sheppo 
4781ae08745Sheppo 	if (vdc->minfo)
4791ae08745Sheppo 		kmem_free(vdc->minfo, sizeof (struct dk_minfo));
4801ae08745Sheppo 
4811ae08745Sheppo 	if (vdc->cinfo)
4821ae08745Sheppo 		kmem_free(vdc->cinfo, sizeof (struct dk_cinfo));
4831ae08745Sheppo 
4841ae08745Sheppo 	if (vdc->vtoc)
4851ae08745Sheppo 		kmem_free(vdc->vtoc, sizeof (struct vtoc));
4861ae08745Sheppo 
48778fcd0a1Sachartre 	if (vdc->geom)
48878fcd0a1Sachartre 		kmem_free(vdc->geom, sizeof (struct dk_geom));
4890a55fbb7Slm66018 
4904bac2208Snarayan 	if (vdc->devid) {
4914bac2208Snarayan 		ddi_devid_unregister(dip);
4924bac2208Snarayan 		ddi_devid_free(vdc->devid);
4934bac2208Snarayan 	}
4944bac2208Snarayan 
4951ae08745Sheppo 	if (vdc->initialized & VDC_SOFT_STATE)
4961ae08745Sheppo 		ddi_soft_state_free(vdc_state, instance);
4971ae08745Sheppo 
4983af08d82Slm66018 	DMSG(vdc, 0, "[%d] End %p\n", instance, (void *)vdc);
4991ae08745Sheppo 
5001ae08745Sheppo 	return (DDI_SUCCESS);
5011ae08745Sheppo }
5021ae08745Sheppo 
5031ae08745Sheppo 
5041ae08745Sheppo static int
5051ae08745Sheppo vdc_do_attach(dev_info_t *dip)
5061ae08745Sheppo {
5071ae08745Sheppo 	int		instance;
5081ae08745Sheppo 	vdc_t		*vdc = NULL;
5091ae08745Sheppo 	int		status;
510655fd6a9Sachartre 	md_t		*mdp;
511655fd6a9Sachartre 	mde_cookie_t	vd_node, vd_port;
5121ae08745Sheppo 
5131ae08745Sheppo 	ASSERT(dip != NULL);
5141ae08745Sheppo 
5151ae08745Sheppo 	instance = ddi_get_instance(dip);
5161ae08745Sheppo 	if (ddi_soft_state_zalloc(vdc_state, instance) != DDI_SUCCESS) {
517e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't alloc state structure",
518e1ebb9ecSlm66018 		    instance);
5191ae08745Sheppo 		return (DDI_FAILURE);
5201ae08745Sheppo 	}
5211ae08745Sheppo 
5221ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
523e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
5241ae08745Sheppo 		return (DDI_FAILURE);
5251ae08745Sheppo 	}
5261ae08745Sheppo 
5271ae08745Sheppo 	/*
5281ae08745Sheppo 	 * We assign the value to initialized in this case to zero out the
5291ae08745Sheppo 	 * variable and then set bits in it to indicate what has been done
5301ae08745Sheppo 	 */
5311ae08745Sheppo 	vdc->initialized = VDC_SOFT_STATE;
5321ae08745Sheppo 
5331ae08745Sheppo 	vdc_hz_timeout = drv_usectohz(vdc_usec_timeout);
5343af08d82Slm66018 
5353af08d82Slm66018 	vdc_hz_min_ldc_delay = drv_usectohz(vdc_min_timeout_ldc);
5363af08d82Slm66018 	vdc_hz_max_ldc_delay = drv_usectohz(vdc_max_timeout_ldc);
5371ae08745Sheppo 
5381ae08745Sheppo 	vdc->dip	= dip;
5391ae08745Sheppo 	vdc->instance	= instance;
5401ae08745Sheppo 	vdc->vdisk_type	= VD_DISK_TYPE_UNK;
5414bac2208Snarayan 	vdc->vdisk_label = VD_DISK_LABEL_UNK;
5423af08d82Slm66018 	vdc->state	= VDC_STATE_INIT;
5433af08d82Slm66018 	vdc->lifecycle	= VDC_LC_ATTACHING;
5441ae08745Sheppo 	vdc->ldc_state	= 0;
5451ae08745Sheppo 	vdc->session_id = 0;
5461ae08745Sheppo 	vdc->block_size = DEV_BSIZE;
5478e6a2a04Slm66018 	vdc->max_xfer_sz = maxphys / DEV_BSIZE;
5481ae08745Sheppo 
5491ae08745Sheppo 	vdc->vtoc = NULL;
55078fcd0a1Sachartre 	vdc->geom = NULL;
5511ae08745Sheppo 	vdc->cinfo = NULL;
5521ae08745Sheppo 	vdc->minfo = NULL;
5531ae08745Sheppo 
5541ae08745Sheppo 	mutex_init(&vdc->lock, NULL, MUTEX_DRIVER, NULL);
5553af08d82Slm66018 	cv_init(&vdc->initwait_cv, NULL, CV_DRIVER, NULL);
5563af08d82Slm66018 	cv_init(&vdc->dring_free_cv, NULL, CV_DRIVER, NULL);
5573af08d82Slm66018 	cv_init(&vdc->membind_cv, NULL, CV_DRIVER, NULL);
5583af08d82Slm66018 	cv_init(&vdc->running_cv, NULL, CV_DRIVER, NULL);
5593af08d82Slm66018 
5603af08d82Slm66018 	vdc->threads_pending = 0;
5613af08d82Slm66018 	vdc->sync_op_pending = B_FALSE;
5623af08d82Slm66018 	vdc->sync_op_blocked = B_FALSE;
5633af08d82Slm66018 	cv_init(&vdc->sync_pending_cv, NULL, CV_DRIVER, NULL);
5643af08d82Slm66018 	cv_init(&vdc->sync_blocked_cv, NULL, CV_DRIVER, NULL);
5653af08d82Slm66018 
5663af08d82Slm66018 	/* init blocking msg read functionality */
5673af08d82Slm66018 	mutex_init(&vdc->read_lock, NULL, MUTEX_DRIVER, NULL);
5683af08d82Slm66018 	cv_init(&vdc->read_cv, NULL, CV_DRIVER, NULL);
5693af08d82Slm66018 	vdc->read_state = VDC_READ_IDLE;
5703af08d82Slm66018 
5711ae08745Sheppo 	vdc->initialized |= VDC_LOCKS;
5721ae08745Sheppo 
573655fd6a9Sachartre 	/* get device and port MD node for this disk instance */
574655fd6a9Sachartre 	if (vdc_get_md_node(dip, &mdp, &vd_node, &vd_port) != 0) {
575655fd6a9Sachartre 		cmn_err(CE_NOTE, "[%d] Could not get machine description node",
576655fd6a9Sachartre 		    instance);
577655fd6a9Sachartre 		return (DDI_FAILURE);
578655fd6a9Sachartre 	}
579655fd6a9Sachartre 
580655fd6a9Sachartre 	/* set the connection timeout */
581655fd6a9Sachartre 	if (vd_port == NULL || (md_get_prop_val(mdp, vd_port,
582655fd6a9Sachartre 	    VDC_MD_TIMEOUT, &vdc->ctimeout) != 0)) {
583655fd6a9Sachartre 		vdc->ctimeout = 0;
584655fd6a9Sachartre 	}
585655fd6a9Sachartre 
5863af08d82Slm66018 	/* initialise LDC channel which will be used to communicate with vds */
587655fd6a9Sachartre 	status = vdc_do_ldc_init(vdc, mdp, vd_node);
588655fd6a9Sachartre 
589655fd6a9Sachartre 	(void) md_fini_handle(mdp);
590655fd6a9Sachartre 
591655fd6a9Sachartre 	if (status != 0) {
5923af08d82Slm66018 		cmn_err(CE_NOTE, "[%d] Couldn't initialize LDC", instance);
5933af08d82Slm66018 		goto return_status;
5943af08d82Slm66018 	}
5953af08d82Slm66018 
5963af08d82Slm66018 	/* initialize the thread responsible for managing state with server */
5973af08d82Slm66018 	vdc->msg_proc_thr = thread_create(NULL, 0, vdc_process_msg_thread,
5981ae08745Sheppo 	    vdc, 0, &p0, TS_RUN, minclsyspri);
5993af08d82Slm66018 	if (vdc->msg_proc_thr == NULL) {
6001ae08745Sheppo 		cmn_err(CE_NOTE, "[%d] Failed to create msg processing thread",
6011ae08745Sheppo 		    instance);
6021ae08745Sheppo 		return (DDI_FAILURE);
6031ae08745Sheppo 	}
6043af08d82Slm66018 
6051ae08745Sheppo 	vdc->initialized |= VDC_THREAD;
6061ae08745Sheppo 
607e1ebb9ecSlm66018 	atomic_inc_32(&vdc_instance_count);
6081ae08745Sheppo 
6090a55fbb7Slm66018 	/*
61078fcd0a1Sachartre 	 * Check the disk label. This will send requests and do the handshake.
61178fcd0a1Sachartre 	 * We don't really care about the disk label now. What we really need is
61278fcd0a1Sachartre 	 * the handshake do be done so that we know the type of the disk (slice
61378fcd0a1Sachartre 	 * or full disk) and the appropriate device nodes can be created.
6140a55fbb7Slm66018 	 */
61578fcd0a1Sachartre 	vdc->vdisk_label = VD_DISK_LABEL_UNK;
61678fcd0a1Sachartre 	vdc->vtoc = kmem_zalloc(sizeof (struct vtoc), KM_SLEEP);
61778fcd0a1Sachartre 	vdc->geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
61878fcd0a1Sachartre 
61978fcd0a1Sachartre 	mutex_enter(&vdc->lock);
62078fcd0a1Sachartre 	(void) vdc_validate_geometry(vdc);
62178fcd0a1Sachartre 	mutex_exit(&vdc->lock);
6221ae08745Sheppo 
6231ae08745Sheppo 	/*
6241ae08745Sheppo 	 * Now that we have the device info we can create the
6251ae08745Sheppo 	 * device nodes and properties
6261ae08745Sheppo 	 */
6271ae08745Sheppo 	status = vdc_create_device_nodes(vdc);
6281ae08745Sheppo 	if (status) {
6293af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to create device nodes",
6301ae08745Sheppo 		    instance);
6313af08d82Slm66018 		goto return_status;
6321ae08745Sheppo 	}
6331ae08745Sheppo 	status = vdc_create_device_nodes_props(vdc);
6341ae08745Sheppo 	if (status) {
6353af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to create device nodes"
6360a55fbb7Slm66018 		    " properties (%d)", instance, status);
6373af08d82Slm66018 		goto return_status;
6381ae08745Sheppo 	}
6391ae08745Sheppo 
6404bac2208Snarayan 	/*
6414bac2208Snarayan 	 * Setup devid
6424bac2208Snarayan 	 */
6434bac2208Snarayan 	if (vdc_setup_devid(vdc)) {
6443af08d82Slm66018 		DMSG(vdc, 0, "[%d] No device id available\n", instance);
6454bac2208Snarayan 	}
6464bac2208Snarayan 
6471ae08745Sheppo 	ddi_report_dev(dip);
6483af08d82Slm66018 	vdc->lifecycle	= VDC_LC_ONLINE;
6493af08d82Slm66018 	DMSG(vdc, 0, "[%d] Attach tasks successful\n", instance);
6501ae08745Sheppo 
6513af08d82Slm66018 return_status:
6523af08d82Slm66018 	DMSG(vdc, 0, "[%d] Attach completed\n", instance);
6531ae08745Sheppo 	return (status);
6541ae08745Sheppo }
6551ae08745Sheppo 
6561ae08745Sheppo static int
6571ae08745Sheppo vdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
6581ae08745Sheppo {
6591ae08745Sheppo 	int	status;
6601ae08745Sheppo 
6611ae08745Sheppo 	switch (cmd) {
6621ae08745Sheppo 	case DDI_ATTACH:
6631ae08745Sheppo 		if ((status = vdc_do_attach(dip)) != 0)
6641ae08745Sheppo 			(void) vdc_detach(dip, DDI_DETACH);
6651ae08745Sheppo 		return (status);
6661ae08745Sheppo 	case DDI_RESUME:
6671ae08745Sheppo 		/* nothing to do for this non-device */
6681ae08745Sheppo 		return (DDI_SUCCESS);
6691ae08745Sheppo 	default:
6701ae08745Sheppo 		return (DDI_FAILURE);
6711ae08745Sheppo 	}
6721ae08745Sheppo }
6731ae08745Sheppo 
6741ae08745Sheppo static int
675655fd6a9Sachartre vdc_do_ldc_init(vdc_t *vdc, md_t *mdp, mde_cookie_t vd_node)
6761ae08745Sheppo {
6771ae08745Sheppo 	int			status = 0;
6781ae08745Sheppo 	ldc_status_t		ldc_state;
6791ae08745Sheppo 	ldc_attr_t		ldc_attr;
6801ae08745Sheppo 	uint64_t		ldc_id = 0;
6811ae08745Sheppo 
6821ae08745Sheppo 	ASSERT(vdc != NULL);
6831ae08745Sheppo 
6841ae08745Sheppo 	vdc->initialized |= VDC_LDC;
6851ae08745Sheppo 
686655fd6a9Sachartre 	if ((status = vdc_get_ldc_id(mdp, vd_node, &ldc_id)) != 0) {
6873af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to get LDC channel ID property",
688e1ebb9ecSlm66018 		    vdc->instance);
6891ae08745Sheppo 		return (EIO);
6901ae08745Sheppo 	}
691655fd6a9Sachartre 
692655fd6a9Sachartre 	DMSGX(0, "[%d] LDC id is 0x%lx\n", vdc->instance, ldc_id);
693655fd6a9Sachartre 
6941ae08745Sheppo 	vdc->ldc_id = ldc_id;
6951ae08745Sheppo 
6961ae08745Sheppo 	ldc_attr.devclass = LDC_DEV_BLK;
6971ae08745Sheppo 	ldc_attr.instance = vdc->instance;
6981ae08745Sheppo 	ldc_attr.mode = LDC_MODE_UNRELIABLE;	/* unreliable transport */
699e1ebb9ecSlm66018 	ldc_attr.mtu = VD_LDC_MTU;
7001ae08745Sheppo 
7011ae08745Sheppo 	if ((vdc->initialized & VDC_LDC_INIT) == 0) {
7021ae08745Sheppo 		status = ldc_init(ldc_id, &ldc_attr, &vdc->ldc_handle);
7031ae08745Sheppo 		if (status != 0) {
7043af08d82Slm66018 			DMSG(vdc, 0, "[%d] ldc_init(chan %ld) returned %d",
7051ae08745Sheppo 			    vdc->instance, ldc_id, status);
7061ae08745Sheppo 			return (status);
7071ae08745Sheppo 		}
7081ae08745Sheppo 		vdc->initialized |= VDC_LDC_INIT;
7091ae08745Sheppo 	}
7101ae08745Sheppo 	status = ldc_status(vdc->ldc_handle, &ldc_state);
7111ae08745Sheppo 	if (status != 0) {
7123af08d82Slm66018 		DMSG(vdc, 0, "[%d] Cannot discover LDC status [err=%d]",
713e1ebb9ecSlm66018 		    vdc->instance, status);
7141ae08745Sheppo 		return (status);
7151ae08745Sheppo 	}
7161ae08745Sheppo 	vdc->ldc_state = ldc_state;
7171ae08745Sheppo 
7181ae08745Sheppo 	if ((vdc->initialized & VDC_LDC_CB) == 0) {
7191ae08745Sheppo 		status = ldc_reg_callback(vdc->ldc_handle, vdc_handle_cb,
7201ae08745Sheppo 		    (caddr_t)vdc);
7211ae08745Sheppo 		if (status != 0) {
7223af08d82Slm66018 			DMSG(vdc, 0, "[%d] LDC callback reg. failed (%d)",
723e1ebb9ecSlm66018 			    vdc->instance, status);
7241ae08745Sheppo 			return (status);
7251ae08745Sheppo 		}
7261ae08745Sheppo 		vdc->initialized |= VDC_LDC_CB;
7271ae08745Sheppo 	}
7281ae08745Sheppo 
7291ae08745Sheppo 	vdc->initialized |= VDC_LDC;
7301ae08745Sheppo 
7311ae08745Sheppo 	/*
7321ae08745Sheppo 	 * At this stage we have initialised LDC, we will now try and open
7331ae08745Sheppo 	 * the connection.
7341ae08745Sheppo 	 */
7351ae08745Sheppo 	if (vdc->ldc_state == LDC_INIT) {
7361ae08745Sheppo 		status = ldc_open(vdc->ldc_handle);
7371ae08745Sheppo 		if (status != 0) {
7383af08d82Slm66018 			DMSG(vdc, 0, "[%d] ldc_open(chan %ld) returned %d",
7391ae08745Sheppo 			    vdc->instance, vdc->ldc_id, status);
7401ae08745Sheppo 			return (status);
7411ae08745Sheppo 		}
7421ae08745Sheppo 		vdc->initialized |= VDC_LDC_OPEN;
7431ae08745Sheppo 	}
7441ae08745Sheppo 
7451ae08745Sheppo 	return (status);
7461ae08745Sheppo }
7471ae08745Sheppo 
7481ae08745Sheppo static int
7491ae08745Sheppo vdc_start_ldc_connection(vdc_t *vdc)
7501ae08745Sheppo {
7511ae08745Sheppo 	int		status = 0;
7521ae08745Sheppo 
7531ae08745Sheppo 	ASSERT(vdc != NULL);
7541ae08745Sheppo 
7553af08d82Slm66018 	ASSERT(MUTEX_HELD(&vdc->lock));
7561ae08745Sheppo 
7570a55fbb7Slm66018 	status = vdc_do_ldc_up(vdc);
7581ae08745Sheppo 
7593af08d82Slm66018 	DMSG(vdc, 0, "[%d] Finished bringing up LDC\n", vdc->instance);
7601ae08745Sheppo 
7613af08d82Slm66018 	return (status);
7623af08d82Slm66018 }
7633af08d82Slm66018 
7643af08d82Slm66018 static int
7653af08d82Slm66018 vdc_stop_ldc_connection(vdc_t *vdcp)
7663af08d82Slm66018 {
7673af08d82Slm66018 	int	status;
7683af08d82Slm66018 
7693af08d82Slm66018 	DMSG(vdcp, 0, ": Resetting connection to vDisk server : state %d\n",
7703af08d82Slm66018 	    vdcp->state);
7713af08d82Slm66018 
7723af08d82Slm66018 	status = ldc_down(vdcp->ldc_handle);
7733af08d82Slm66018 	DMSG(vdcp, 0, "ldc_down() = %d\n", status);
7743af08d82Slm66018 
7753af08d82Slm66018 	vdcp->initialized &= ~VDC_HANDSHAKE;
7763af08d82Slm66018 	DMSG(vdcp, 0, "initialized=%x\n", vdcp->initialized);
7771ae08745Sheppo 
7781ae08745Sheppo 	return (status);
7791ae08745Sheppo }
7801ae08745Sheppo 
7814bac2208Snarayan static int
7824bac2208Snarayan vdc_create_device_nodes_efi(vdc_t *vdc)
7834bac2208Snarayan {
7844bac2208Snarayan 	ddi_remove_minor_node(vdc->dip, "h");
7854bac2208Snarayan 	ddi_remove_minor_node(vdc->dip, "h,raw");
7864bac2208Snarayan 
7874bac2208Snarayan 	if (ddi_create_minor_node(vdc->dip, "wd", S_IFBLK,
7884bac2208Snarayan 	    VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
7894bac2208Snarayan 	    DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
7904bac2208Snarayan 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd'",
7914bac2208Snarayan 		    vdc->instance);
7924bac2208Snarayan 		return (EIO);
7934bac2208Snarayan 	}
7944bac2208Snarayan 
7954bac2208Snarayan 	/* if any device node is created we set this flag */
7964bac2208Snarayan 	vdc->initialized |= VDC_MINOR;
7974bac2208Snarayan 
7984bac2208Snarayan 	if (ddi_create_minor_node(vdc->dip, "wd,raw", S_IFCHR,
7994bac2208Snarayan 	    VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
8004bac2208Snarayan 	    DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
8014bac2208Snarayan 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd,raw'",
8024bac2208Snarayan 		    vdc->instance);
8034bac2208Snarayan 		return (EIO);
8044bac2208Snarayan 	}
8054bac2208Snarayan 
8064bac2208Snarayan 	return (0);
8074bac2208Snarayan }
8084bac2208Snarayan 
8094bac2208Snarayan static int
8104bac2208Snarayan vdc_create_device_nodes_vtoc(vdc_t *vdc)
8114bac2208Snarayan {
8124bac2208Snarayan 	ddi_remove_minor_node(vdc->dip, "wd");
8134bac2208Snarayan 	ddi_remove_minor_node(vdc->dip, "wd,raw");
8144bac2208Snarayan 
8154bac2208Snarayan 	if (ddi_create_minor_node(vdc->dip, "h", S_IFBLK,
8164bac2208Snarayan 	    VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
8174bac2208Snarayan 	    DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
8184bac2208Snarayan 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h'",
8194bac2208Snarayan 		    vdc->instance);
8204bac2208Snarayan 		return (EIO);
8214bac2208Snarayan 	}
8224bac2208Snarayan 
8234bac2208Snarayan 	/* if any device node is created we set this flag */
8244bac2208Snarayan 	vdc->initialized |= VDC_MINOR;
8254bac2208Snarayan 
8264bac2208Snarayan 	if (ddi_create_minor_node(vdc->dip, "h,raw", S_IFCHR,
8274bac2208Snarayan 	    VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
8284bac2208Snarayan 	    DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
8294bac2208Snarayan 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h,raw'",
8304bac2208Snarayan 		    vdc->instance);
8314bac2208Snarayan 		return (EIO);
8324bac2208Snarayan 	}
8334bac2208Snarayan 
8344bac2208Snarayan 	return (0);
8354bac2208Snarayan }
8361ae08745Sheppo 
8371ae08745Sheppo /*
8381ae08745Sheppo  * Function:
8391ae08745Sheppo  *	vdc_create_device_nodes
8401ae08745Sheppo  *
8411ae08745Sheppo  * Description:
8421ae08745Sheppo  *	This function creates the block and character device nodes under
8431ae08745Sheppo  *	/devices along with the node properties. It is called as part of
8441ae08745Sheppo  *	the attach(9E) of the instance during the handshake with vds after
8451ae08745Sheppo  *	vds has sent the attributes to vdc.
8461ae08745Sheppo  *
8471ae08745Sheppo  *	If the device is of type VD_DISK_TYPE_SLICE then the minor node
8481ae08745Sheppo  *	of 2 is used in keeping with the Solaris convention that slice 2
8491ae08745Sheppo  *	refers to a whole disk. Slices start at 'a'
8501ae08745Sheppo  *
8511ae08745Sheppo  * Parameters:
8521ae08745Sheppo  *	vdc 		- soft state pointer
8531ae08745Sheppo  *
8541ae08745Sheppo  * Return Values
8551ae08745Sheppo  *	0		- Success
8561ae08745Sheppo  *	EIO		- Failed to create node
8571ae08745Sheppo  *	EINVAL		- Unknown type of disk exported
8581ae08745Sheppo  */
8591ae08745Sheppo static int
8601ae08745Sheppo vdc_create_device_nodes(vdc_t *vdc)
8611ae08745Sheppo {
8624bac2208Snarayan 	char		name[sizeof ("s,raw")];
8631ae08745Sheppo 	dev_info_t	*dip = NULL;
8644bac2208Snarayan 	int		instance, status;
8651ae08745Sheppo 	int		num_slices = 1;
8661ae08745Sheppo 	int		i;
8671ae08745Sheppo 
8681ae08745Sheppo 	ASSERT(vdc != NULL);
8691ae08745Sheppo 
8701ae08745Sheppo 	instance = vdc->instance;
8711ae08745Sheppo 	dip = vdc->dip;
8721ae08745Sheppo 
8731ae08745Sheppo 	switch (vdc->vdisk_type) {
8741ae08745Sheppo 	case VD_DISK_TYPE_DISK:
8751ae08745Sheppo 		num_slices = V_NUMPAR;
8761ae08745Sheppo 		break;
8771ae08745Sheppo 	case VD_DISK_TYPE_SLICE:
8781ae08745Sheppo 		num_slices = 1;
8791ae08745Sheppo 		break;
8801ae08745Sheppo 	case VD_DISK_TYPE_UNK:
8811ae08745Sheppo 	default:
8821ae08745Sheppo 		return (EINVAL);
8831ae08745Sheppo 	}
8841ae08745Sheppo 
8854bac2208Snarayan 	/*
8864bac2208Snarayan 	 * Minor nodes are different for EFI disks: EFI disks do not have
8874bac2208Snarayan 	 * a minor node 'g' for the minor number corresponding to slice
8884bac2208Snarayan 	 * VD_EFI_WD_SLICE (slice 7) instead they have a minor node 'wd'
8894bac2208Snarayan 	 * representing the whole disk.
8904bac2208Snarayan 	 */
8911ae08745Sheppo 	for (i = 0; i < num_slices; i++) {
8924bac2208Snarayan 
8934bac2208Snarayan 		if (i == VD_EFI_WD_SLICE) {
8944bac2208Snarayan 			if (vdc->vdisk_label == VD_DISK_LABEL_EFI)
8954bac2208Snarayan 				status = vdc_create_device_nodes_efi(vdc);
8964bac2208Snarayan 			else
8974bac2208Snarayan 				status = vdc_create_device_nodes_vtoc(vdc);
8984bac2208Snarayan 			if (status != 0)
8994bac2208Snarayan 				return (status);
9004bac2208Snarayan 			continue;
9014bac2208Snarayan 		}
9024bac2208Snarayan 
9031ae08745Sheppo 		(void) snprintf(name, sizeof (name), "%c", 'a' + i);
9041ae08745Sheppo 		if (ddi_create_minor_node(dip, name, S_IFBLK,
9051ae08745Sheppo 		    VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
906e1ebb9ecSlm66018 			cmn_err(CE_NOTE, "[%d] Couldn't add block node '%s'",
907e1ebb9ecSlm66018 			    instance, name);
9081ae08745Sheppo 			return (EIO);
9091ae08745Sheppo 		}
9101ae08745Sheppo 
9111ae08745Sheppo 		/* if any device node is created we set this flag */
9121ae08745Sheppo 		vdc->initialized |= VDC_MINOR;
9131ae08745Sheppo 
91487a7269eSachartre 		(void) snprintf(name, sizeof (name), "%c%s", 'a' + i, ",raw");
91587a7269eSachartre 
9161ae08745Sheppo 		if (ddi_create_minor_node(dip, name, S_IFCHR,
9171ae08745Sheppo 		    VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
918e1ebb9ecSlm66018 			cmn_err(CE_NOTE, "[%d] Couldn't add raw node '%s'",
919e1ebb9ecSlm66018 			    instance, name);
9201ae08745Sheppo 			return (EIO);
9211ae08745Sheppo 		}
9221ae08745Sheppo 	}
9231ae08745Sheppo 
9241ae08745Sheppo 	return (0);
9251ae08745Sheppo }
9261ae08745Sheppo 
9271ae08745Sheppo /*
9281ae08745Sheppo  * Function:
9291ae08745Sheppo  *	vdc_create_device_nodes_props
9301ae08745Sheppo  *
9311ae08745Sheppo  * Description:
9321ae08745Sheppo  *	This function creates the block and character device nodes under
9331ae08745Sheppo  *	/devices along with the node properties. It is called as part of
9341ae08745Sheppo  *	the attach(9E) of the instance during the handshake with vds after
9351ae08745Sheppo  *	vds has sent the attributes to vdc.
9361ae08745Sheppo  *
9371ae08745Sheppo  * Parameters:
9381ae08745Sheppo  *	vdc 		- soft state pointer
9391ae08745Sheppo  *
9401ae08745Sheppo  * Return Values
9411ae08745Sheppo  *	0		- Success
9421ae08745Sheppo  *	EIO		- Failed to create device node property
9431ae08745Sheppo  *	EINVAL		- Unknown type of disk exported
9441ae08745Sheppo  */
9451ae08745Sheppo static int
9461ae08745Sheppo vdc_create_device_nodes_props(vdc_t *vdc)
9471ae08745Sheppo {
9481ae08745Sheppo 	dev_info_t	*dip = NULL;
9491ae08745Sheppo 	int		instance;
9501ae08745Sheppo 	int		num_slices = 1;
9511ae08745Sheppo 	int64_t		size = 0;
9521ae08745Sheppo 	dev_t		dev;
9531ae08745Sheppo 	int		rv;
9541ae08745Sheppo 	int		i;
9551ae08745Sheppo 
9561ae08745Sheppo 	ASSERT(vdc != NULL);
95778fcd0a1Sachartre 	ASSERT(vdc->vtoc != NULL);
9581ae08745Sheppo 
9591ae08745Sheppo 	instance = vdc->instance;
9601ae08745Sheppo 	dip = vdc->dip;
9611ae08745Sheppo 
9621ae08745Sheppo 	switch (vdc->vdisk_type) {
9631ae08745Sheppo 	case VD_DISK_TYPE_DISK:
9641ae08745Sheppo 		num_slices = V_NUMPAR;
9651ae08745Sheppo 		break;
9661ae08745Sheppo 	case VD_DISK_TYPE_SLICE:
9671ae08745Sheppo 		num_slices = 1;
9681ae08745Sheppo 		break;
9691ae08745Sheppo 	case VD_DISK_TYPE_UNK:
9701ae08745Sheppo 	default:
9711ae08745Sheppo 		return (EINVAL);
9721ae08745Sheppo 	}
9731ae08745Sheppo 
97478fcd0a1Sachartre 	if (vdc->vdisk_label == VD_DISK_LABEL_UNK) {
97578fcd0a1Sachartre 		/* remove all properties */
97678fcd0a1Sachartre 		for (i = 0; i < num_slices; i++) {
97778fcd0a1Sachartre 			dev = makedevice(ddi_driver_major(dip),
97878fcd0a1Sachartre 			    VD_MAKE_DEV(instance, i));
97978fcd0a1Sachartre 			(void) ddi_prop_remove(dev, dip, VDC_SIZE_PROP_NAME);
98078fcd0a1Sachartre 			(void) ddi_prop_remove(dev, dip, VDC_NBLOCKS_PROP_NAME);
98178fcd0a1Sachartre 		}
98278fcd0a1Sachartre 		return (0);
98378fcd0a1Sachartre 	}
98478fcd0a1Sachartre 
9851ae08745Sheppo 	for (i = 0; i < num_slices; i++) {
9861ae08745Sheppo 		dev = makedevice(ddi_driver_major(dip),
9871ae08745Sheppo 		    VD_MAKE_DEV(instance, i));
9881ae08745Sheppo 
9891ae08745Sheppo 		size = vdc->vtoc->v_part[i].p_size * vdc->vtoc->v_sectorsz;
9903af08d82Slm66018 		DMSG(vdc, 0, "[%d] sz %ld (%ld Mb)  p_size %lx\n",
991e1ebb9ecSlm66018 		    instance, size, size / (1024 * 1024),
9921ae08745Sheppo 		    vdc->vtoc->v_part[i].p_size);
9931ae08745Sheppo 
9941ae08745Sheppo 		rv = ddi_prop_update_int64(dev, dip, VDC_SIZE_PROP_NAME, size);
9951ae08745Sheppo 		if (rv != DDI_PROP_SUCCESS) {
996e1ebb9ecSlm66018 			cmn_err(CE_NOTE, "[%d] Couldn't add '%s' prop of [%ld]",
997e1ebb9ecSlm66018 			    instance, VDC_SIZE_PROP_NAME, size);
9981ae08745Sheppo 			return (EIO);
9991ae08745Sheppo 		}
10001ae08745Sheppo 
10011ae08745Sheppo 		rv = ddi_prop_update_int64(dev, dip, VDC_NBLOCKS_PROP_NAME,
10021ae08745Sheppo 		    lbtodb(size));
10031ae08745Sheppo 		if (rv != DDI_PROP_SUCCESS) {
1004e1ebb9ecSlm66018 			cmn_err(CE_NOTE, "[%d] Couldn't add '%s' prop [%llu]",
10051ae08745Sheppo 			    instance, VDC_NBLOCKS_PROP_NAME, lbtodb(size));
10061ae08745Sheppo 			return (EIO);
10071ae08745Sheppo 		}
10081ae08745Sheppo 	}
10091ae08745Sheppo 
10101ae08745Sheppo 	return (0);
10111ae08745Sheppo }
10121ae08745Sheppo 
101378fcd0a1Sachartre /*
101478fcd0a1Sachartre  * Function:
101578fcd0a1Sachartre  *	vdc_is_opened
101678fcd0a1Sachartre  *
101778fcd0a1Sachartre  * Description:
101878fcd0a1Sachartre  *	This function checks if any slice of a given virtual disk is
101978fcd0a1Sachartre  *	currently opened.
102078fcd0a1Sachartre  *
102178fcd0a1Sachartre  * Parameters:
102278fcd0a1Sachartre  *	vdc 		- soft state pointer
102378fcd0a1Sachartre  *
102478fcd0a1Sachartre  * Return Values
102578fcd0a1Sachartre  *	B_TRUE		- at least one slice is opened.
102678fcd0a1Sachartre  *	B_FALSE		- no slice is opened.
102778fcd0a1Sachartre  */
102878fcd0a1Sachartre static boolean_t
102978fcd0a1Sachartre vdc_is_opened(vdc_t *vdc)
103078fcd0a1Sachartre {
103178fcd0a1Sachartre 	int i, nslices;
103278fcd0a1Sachartre 
103378fcd0a1Sachartre 	switch (vdc->vdisk_type) {
103478fcd0a1Sachartre 	case VD_DISK_TYPE_DISK:
103578fcd0a1Sachartre 		nslices = V_NUMPAR;
103678fcd0a1Sachartre 		break;
103778fcd0a1Sachartre 	case VD_DISK_TYPE_SLICE:
103878fcd0a1Sachartre 		nslices = 1;
103978fcd0a1Sachartre 		break;
104078fcd0a1Sachartre 	case VD_DISK_TYPE_UNK:
104178fcd0a1Sachartre 	default:
104278fcd0a1Sachartre 		ASSERT(0);
104378fcd0a1Sachartre 	}
104478fcd0a1Sachartre 
104578fcd0a1Sachartre 	/* check if there's any layered open */
104678fcd0a1Sachartre 	for (i = 0; i < nslices; i++) {
104778fcd0a1Sachartre 		if (vdc->open_lyr[i] > 0)
104878fcd0a1Sachartre 			return (B_TRUE);
104978fcd0a1Sachartre 	}
105078fcd0a1Sachartre 
105178fcd0a1Sachartre 	/* check if there is any other kind of open */
105278fcd0a1Sachartre 	for (i = 0; i < OTYPCNT; i++) {
105378fcd0a1Sachartre 		if (vdc->open[i] != 0)
105478fcd0a1Sachartre 			return (B_TRUE);
105578fcd0a1Sachartre 	}
105678fcd0a1Sachartre 
105778fcd0a1Sachartre 	return (B_FALSE);
105878fcd0a1Sachartre }
105978fcd0a1Sachartre 
106078fcd0a1Sachartre static int
106178fcd0a1Sachartre vdc_mark_opened(vdc_t *vdc, int slice, int flag, int otyp)
106278fcd0a1Sachartre {
106378fcd0a1Sachartre 	uint8_t slicemask;
106478fcd0a1Sachartre 	int i;
106578fcd0a1Sachartre 
106678fcd0a1Sachartre 	ASSERT(otyp < OTYPCNT);
106778fcd0a1Sachartre 	ASSERT(slice < V_NUMPAR);
106878fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
106978fcd0a1Sachartre 
107078fcd0a1Sachartre 	slicemask = 1 << slice;
107178fcd0a1Sachartre 
107278fcd0a1Sachartre 	/* check if slice is already exclusively opened */
107378fcd0a1Sachartre 	if (vdc->open_excl & slicemask)
107478fcd0a1Sachartre 		return (EBUSY);
107578fcd0a1Sachartre 
107678fcd0a1Sachartre 	/* if open exclusive, check if slice is already opened */
107778fcd0a1Sachartre 	if (flag & FEXCL) {
107878fcd0a1Sachartre 		if (vdc->open_lyr[slice] > 0)
107978fcd0a1Sachartre 			return (EBUSY);
108078fcd0a1Sachartre 		for (i = 0; i < OTYPCNT; i++) {
108178fcd0a1Sachartre 			if (vdc->open[i] & slicemask)
108278fcd0a1Sachartre 				return (EBUSY);
108378fcd0a1Sachartre 		}
108478fcd0a1Sachartre 		vdc->open_excl |= slicemask;
108578fcd0a1Sachartre 	}
108678fcd0a1Sachartre 
108778fcd0a1Sachartre 	/* mark slice as opened */
108878fcd0a1Sachartre 	if (otyp == OTYP_LYR) {
108978fcd0a1Sachartre 		vdc->open_lyr[slice]++;
109078fcd0a1Sachartre 	} else {
109178fcd0a1Sachartre 		vdc->open[otyp] |= slicemask;
109278fcd0a1Sachartre 	}
109378fcd0a1Sachartre 
109478fcd0a1Sachartre 	return (0);
109578fcd0a1Sachartre }
109678fcd0a1Sachartre 
109778fcd0a1Sachartre static void
109878fcd0a1Sachartre vdc_mark_closed(vdc_t *vdc, int slice, int flag, int otyp)
109978fcd0a1Sachartre {
110078fcd0a1Sachartre 	uint8_t slicemask;
110178fcd0a1Sachartre 
110278fcd0a1Sachartre 	ASSERT(otyp < OTYPCNT);
110378fcd0a1Sachartre 	ASSERT(slice < V_NUMPAR);
110478fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
110578fcd0a1Sachartre 
110678fcd0a1Sachartre 	slicemask = 1 << slice;
110778fcd0a1Sachartre 
110878fcd0a1Sachartre 	if (otyp == OTYP_LYR) {
110978fcd0a1Sachartre 		ASSERT(vdc->open_lyr[slice] > 0);
111078fcd0a1Sachartre 		vdc->open_lyr[slice]--;
111178fcd0a1Sachartre 	} else {
111278fcd0a1Sachartre 		vdc->open[otyp] &= ~slicemask;
111378fcd0a1Sachartre 	}
111478fcd0a1Sachartre 
111578fcd0a1Sachartre 	if (flag & FEXCL)
111678fcd0a1Sachartre 		vdc->open_excl &= ~slicemask;
111778fcd0a1Sachartre }
111878fcd0a1Sachartre 
11191ae08745Sheppo static int
11201ae08745Sheppo vdc_open(dev_t *dev, int flag, int otyp, cred_t *cred)
11211ae08745Sheppo {
11221ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
11231ae08745Sheppo 
11241ae08745Sheppo 	int	instance;
112578fcd0a1Sachartre 	int	slice, status = 0;
11261ae08745Sheppo 	vdc_t	*vdc;
11271ae08745Sheppo 
11281ae08745Sheppo 	ASSERT(dev != NULL);
11290d0c8d4bSnarayan 	instance = VDCUNIT(*dev);
11301ae08745Sheppo 
113178fcd0a1Sachartre 	if (otyp >= OTYPCNT)
11321ae08745Sheppo 		return (EINVAL);
11331ae08745Sheppo 
11341ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1135e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
11361ae08745Sheppo 		return (ENXIO);
11371ae08745Sheppo 	}
11381ae08745Sheppo 
11393af08d82Slm66018 	DMSG(vdc, 0, "minor = %d flag = %x, otyp = %x\n",
11403af08d82Slm66018 	    getminor(*dev), flag, otyp);
11411ae08745Sheppo 
114278fcd0a1Sachartre 	slice = VDCPART(*dev);
114378fcd0a1Sachartre 
11441ae08745Sheppo 	mutex_enter(&vdc->lock);
114578fcd0a1Sachartre 
114678fcd0a1Sachartre 	status = vdc_mark_opened(vdc, slice, flag, otyp);
114778fcd0a1Sachartre 
114878fcd0a1Sachartre 	if (status != 0) {
114978fcd0a1Sachartre 		mutex_exit(&vdc->lock);
115078fcd0a1Sachartre 		return (status);
115178fcd0a1Sachartre 	}
115278fcd0a1Sachartre 
115378fcd0a1Sachartre 	if (flag & (FNDELAY | FNONBLOCK)) {
115478fcd0a1Sachartre 
115578fcd0a1Sachartre 		/* don't resubmit a validate request if there's already one */
115678fcd0a1Sachartre 		if (vdc->validate_pending > 0) {
115778fcd0a1Sachartre 			mutex_exit(&vdc->lock);
115878fcd0a1Sachartre 			return (0);
115978fcd0a1Sachartre 		}
116078fcd0a1Sachartre 
116178fcd0a1Sachartre 		/* call vdc_validate() asynchronously to avoid blocking */
116278fcd0a1Sachartre 		if (taskq_dispatch(system_taskq, vdc_validate_task,
116378fcd0a1Sachartre 		    (void *)vdc, TQ_NOSLEEP) == NULL) {
116478fcd0a1Sachartre 			vdc_mark_closed(vdc, slice, flag, otyp);
116578fcd0a1Sachartre 			mutex_exit(&vdc->lock);
116678fcd0a1Sachartre 			return (ENXIO);
116778fcd0a1Sachartre 		}
116878fcd0a1Sachartre 
116978fcd0a1Sachartre 		vdc->validate_pending++;
117078fcd0a1Sachartre 		mutex_exit(&vdc->lock);
117178fcd0a1Sachartre 		return (0);
117278fcd0a1Sachartre 	}
117378fcd0a1Sachartre 
11741ae08745Sheppo 	mutex_exit(&vdc->lock);
11751ae08745Sheppo 
117678fcd0a1Sachartre 	vdc_validate(vdc);
117778fcd0a1Sachartre 
117878fcd0a1Sachartre 	mutex_enter(&vdc->lock);
117978fcd0a1Sachartre 
118078fcd0a1Sachartre 	if (vdc->vdisk_label == VD_DISK_LABEL_UNK ||
118178fcd0a1Sachartre 	    vdc->vtoc->v_part[slice].p_size == 0) {
118278fcd0a1Sachartre 		vdc_mark_closed(vdc, slice, flag, otyp);
118378fcd0a1Sachartre 		status = EIO;
118478fcd0a1Sachartre 	}
118578fcd0a1Sachartre 
118678fcd0a1Sachartre 	mutex_exit(&vdc->lock);
118778fcd0a1Sachartre 
118878fcd0a1Sachartre 	return (status);
11891ae08745Sheppo }
11901ae08745Sheppo 
11911ae08745Sheppo static int
11921ae08745Sheppo vdc_close(dev_t dev, int flag, int otyp, cred_t *cred)
11931ae08745Sheppo {
11941ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
11951ae08745Sheppo 
11961ae08745Sheppo 	int	instance;
119778fcd0a1Sachartre 	int	slice;
1198*8259acd8Szk194757 	int	rv;
11991ae08745Sheppo 	vdc_t	*vdc;
12001ae08745Sheppo 
12010d0c8d4bSnarayan 	instance = VDCUNIT(dev);
12021ae08745Sheppo 
120378fcd0a1Sachartre 	if (otyp >= OTYPCNT)
12041ae08745Sheppo 		return (EINVAL);
12051ae08745Sheppo 
12061ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1207e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
12081ae08745Sheppo 		return (ENXIO);
12091ae08745Sheppo 	}
12101ae08745Sheppo 
12113af08d82Slm66018 	DMSG(vdc, 0, "[%d] flag = %x, otyp = %x\n", instance, flag, otyp);
12121ae08745Sheppo 
121378fcd0a1Sachartre 	slice = VDCPART(dev);
121478fcd0a1Sachartre 
1215*8259acd8Szk194757 	/*
1216*8259acd8Szk194757 	 * Attempt to flush the W$ on a close operation. If this is
1217*8259acd8Szk194757 	 * not a supported IOCTL command or the backing device is read-only
1218*8259acd8Szk194757 	 * do not fail the close operation.
1219*8259acd8Szk194757 	 */
1220*8259acd8Szk194757 	rv = vd_process_ioctl(dev, DKIOCFLUSHWRITECACHE, NULL, FKIOCTL);
1221*8259acd8Szk194757 
1222*8259acd8Szk194757 	if (rv != 0 && rv != ENOTSUP && rv != ENOTTY && rv != EROFS) {
1223*8259acd8Szk194757 		DMSG(vdc, 0, "[%d] flush failed with error %d on close\n",
1224*8259acd8Szk194757 		    instance, rv);
1225*8259acd8Szk194757 		return (EIO);
1226*8259acd8Szk194757 	}
1227*8259acd8Szk194757 
12281ae08745Sheppo 	mutex_enter(&vdc->lock);
122978fcd0a1Sachartre 	vdc_mark_closed(vdc, slice, flag, otyp);
12301ae08745Sheppo 	mutex_exit(&vdc->lock);
12311ae08745Sheppo 
12321ae08745Sheppo 	return (0);
12331ae08745Sheppo }
12341ae08745Sheppo 
12351ae08745Sheppo static int
12361ae08745Sheppo vdc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
12371ae08745Sheppo {
12381ae08745Sheppo 	_NOTE(ARGUNUSED(credp))
12391ae08745Sheppo 	_NOTE(ARGUNUSED(rvalp))
12401ae08745Sheppo 
12411ae08745Sheppo 	return (vd_process_ioctl(dev, cmd, (caddr_t)arg, mode));
12421ae08745Sheppo }
12431ae08745Sheppo 
12441ae08745Sheppo static int
12451ae08745Sheppo vdc_print(dev_t dev, char *str)
12461ae08745Sheppo {
12470d0c8d4bSnarayan 	cmn_err(CE_NOTE, "vdc%d:  %s", VDCUNIT(dev), str);
12481ae08745Sheppo 	return (0);
12491ae08745Sheppo }
12501ae08745Sheppo 
12511ae08745Sheppo static int
12521ae08745Sheppo vdc_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
12531ae08745Sheppo {
1254d10e4ef2Snarayan 	int	rv;
1255d10e4ef2Snarayan 	size_t	nbytes = nblk * DEV_BSIZE;
12560d0c8d4bSnarayan 	int	instance = VDCUNIT(dev);
1257d10e4ef2Snarayan 	vdc_t	*vdc = NULL;
12581ae08745Sheppo 
12591ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1260e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
12611ae08745Sheppo 		return (ENXIO);
12621ae08745Sheppo 	}
12631ae08745Sheppo 
12643af08d82Slm66018 	DMSG(vdc, 2, "[%d] dump %ld bytes at block 0x%lx : addr=0x%p\n",
12653af08d82Slm66018 	    instance, nbytes, blkno, (void *)addr);
12663af08d82Slm66018 	rv = vdc_send_request(vdc, VD_OP_BWRITE, addr, nbytes,
12670d0c8d4bSnarayan 	    VDCPART(dev), blkno, CB_STRATEGY, 0, VIO_write_dir);
12683af08d82Slm66018 	if (rv) {
12693af08d82Slm66018 		DMSG(vdc, 0, "Failed to do a disk dump (err=%d)\n", rv);
12701ae08745Sheppo 		return (rv);
12711ae08745Sheppo 	}
12721ae08745Sheppo 
12733af08d82Slm66018 	if (ddi_in_panic())
12743af08d82Slm66018 		(void) vdc_drain_response(vdc);
12753af08d82Slm66018 
12763af08d82Slm66018 	DMSG(vdc, 0, "[%d] End\n", instance);
12773af08d82Slm66018 
12783af08d82Slm66018 	return (0);
12793af08d82Slm66018 }
12803af08d82Slm66018 
12811ae08745Sheppo /* -------------------------------------------------------------------------- */
12821ae08745Sheppo 
12831ae08745Sheppo /*
12841ae08745Sheppo  * Disk access routines
12851ae08745Sheppo  *
12861ae08745Sheppo  */
12871ae08745Sheppo 
12881ae08745Sheppo /*
12891ae08745Sheppo  * vdc_strategy()
12901ae08745Sheppo  *
12911ae08745Sheppo  * Return Value:
12921ae08745Sheppo  *	0:	As per strategy(9E), the strategy() function must return 0
12931ae08745Sheppo  *		[ bioerror(9f) sets b_flags to the proper error code ]
12941ae08745Sheppo  */
12951ae08745Sheppo static int
12961ae08745Sheppo vdc_strategy(struct buf *buf)
12971ae08745Sheppo {
12981ae08745Sheppo 	int	rv = -1;
12991ae08745Sheppo 	vdc_t	*vdc = NULL;
13000d0c8d4bSnarayan 	int	instance = VDCUNIT(buf->b_edev);
13011ae08745Sheppo 	int	op = (buf->b_flags & B_READ) ? VD_OP_BREAD : VD_OP_BWRITE;
130287a7269eSachartre 	int	slice;
13031ae08745Sheppo 
13041ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1305e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
13061ae08745Sheppo 		bioerror(buf, ENXIO);
13071ae08745Sheppo 		biodone(buf);
13081ae08745Sheppo 		return (0);
13091ae08745Sheppo 	}
13101ae08745Sheppo 
13113af08d82Slm66018 	DMSG(vdc, 2, "[%d] %s %ld bytes at block %llx : b_addr=0x%p\n",
13123af08d82Slm66018 	    instance, (buf->b_flags & B_READ) ? "Read" : "Write",
13133af08d82Slm66018 	    buf->b_bcount, buf->b_lblkno, (void *)buf->b_un.b_addr);
1314d10e4ef2Snarayan 	DTRACE_IO2(vstart, buf_t *, buf, vdc_t *, vdc);
1315d10e4ef2Snarayan 
13161ae08745Sheppo 	bp_mapin(buf);
13171ae08745Sheppo 
131887a7269eSachartre 	if ((long)buf->b_private == VD_SLICE_NONE) {
131987a7269eSachartre 		/* I/O using an absolute disk offset */
132087a7269eSachartre 		slice = VD_SLICE_NONE;
132187a7269eSachartre 	} else {
132287a7269eSachartre 		slice = VDCPART(buf->b_edev);
132387a7269eSachartre 	}
132487a7269eSachartre 
13253af08d82Slm66018 	rv = vdc_send_request(vdc, op, (caddr_t)buf->b_un.b_addr,
132687a7269eSachartre 	    buf->b_bcount, slice, buf->b_lblkno,
13273af08d82Slm66018 	    CB_STRATEGY, buf, (op == VD_OP_BREAD) ? VIO_read_dir :
13283af08d82Slm66018 	    VIO_write_dir);
13293af08d82Slm66018 
1330d10e4ef2Snarayan 	/*
1331d10e4ef2Snarayan 	 * If the request was successfully sent, the strategy call returns and
1332d10e4ef2Snarayan 	 * the ACK handler calls the bioxxx functions when the vDisk server is
1333d10e4ef2Snarayan 	 * done.
1334d10e4ef2Snarayan 	 */
1335d10e4ef2Snarayan 	if (rv) {
13363af08d82Slm66018 		DMSG(vdc, 0, "Failed to read/write (err=%d)\n", rv);
13371ae08745Sheppo 		bioerror(buf, rv);
13381ae08745Sheppo 		biodone(buf);
1339d10e4ef2Snarayan 	}
1340d10e4ef2Snarayan 
13411ae08745Sheppo 	return (0);
13421ae08745Sheppo }
13431ae08745Sheppo 
13440d0c8d4bSnarayan /*
13450d0c8d4bSnarayan  * Function:
13460d0c8d4bSnarayan  *	vdc_min
13470d0c8d4bSnarayan  *
13480d0c8d4bSnarayan  * Description:
13490d0c8d4bSnarayan  *	Routine to limit the size of a data transfer. Used in
13500d0c8d4bSnarayan  *	conjunction with physio(9F).
13510d0c8d4bSnarayan  *
13520d0c8d4bSnarayan  * Arguments:
13530d0c8d4bSnarayan  *	bp - pointer to the indicated buf(9S) struct.
13540d0c8d4bSnarayan  *
13550d0c8d4bSnarayan  */
13560d0c8d4bSnarayan static void
13570d0c8d4bSnarayan vdc_min(struct buf *bufp)
13580d0c8d4bSnarayan {
13590d0c8d4bSnarayan 	vdc_t	*vdc = NULL;
13600d0c8d4bSnarayan 	int	instance = VDCUNIT(bufp->b_edev);
13610d0c8d4bSnarayan 
13620d0c8d4bSnarayan 	vdc = ddi_get_soft_state(vdc_state, instance);
13630d0c8d4bSnarayan 	VERIFY(vdc != NULL);
13640d0c8d4bSnarayan 
13650d0c8d4bSnarayan 	if (bufp->b_bcount > (vdc->max_xfer_sz * vdc->block_size)) {
13660d0c8d4bSnarayan 		bufp->b_bcount = vdc->max_xfer_sz * vdc->block_size;
13670d0c8d4bSnarayan 	}
13680d0c8d4bSnarayan }
13691ae08745Sheppo 
13701ae08745Sheppo static int
13711ae08745Sheppo vdc_read(dev_t dev, struct uio *uio, cred_t *cred)
13721ae08745Sheppo {
13731ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
13741ae08745Sheppo 
13750d0c8d4bSnarayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
13760d0c8d4bSnarayan 	return (physio(vdc_strategy, NULL, dev, B_READ, vdc_min, uio));
13771ae08745Sheppo }
13781ae08745Sheppo 
13791ae08745Sheppo static int
13801ae08745Sheppo vdc_write(dev_t dev, struct uio *uio, cred_t *cred)
13811ae08745Sheppo {
13821ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
13831ae08745Sheppo 
13840d0c8d4bSnarayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
13850d0c8d4bSnarayan 	return (physio(vdc_strategy, NULL, dev, B_WRITE, vdc_min, uio));
13861ae08745Sheppo }
13871ae08745Sheppo 
13881ae08745Sheppo static int
13891ae08745Sheppo vdc_aread(dev_t dev, struct aio_req *aio, cred_t *cred)
13901ae08745Sheppo {
13911ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
13921ae08745Sheppo 
13930d0c8d4bSnarayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
13940d0c8d4bSnarayan 	return (aphysio(vdc_strategy, anocancel, dev, B_READ, vdc_min, aio));
13951ae08745Sheppo }
13961ae08745Sheppo 
13971ae08745Sheppo static int
13981ae08745Sheppo vdc_awrite(dev_t dev, struct aio_req *aio, cred_t *cred)
13991ae08745Sheppo {
14001ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
14011ae08745Sheppo 
14020d0c8d4bSnarayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
14030d0c8d4bSnarayan 	return (aphysio(vdc_strategy, anocancel, dev, B_WRITE, vdc_min, aio));
14041ae08745Sheppo }
14051ae08745Sheppo 
14061ae08745Sheppo 
14071ae08745Sheppo /* -------------------------------------------------------------------------- */
14081ae08745Sheppo 
14091ae08745Sheppo /*
14101ae08745Sheppo  * Handshake support
14111ae08745Sheppo  */
14121ae08745Sheppo 
14131ae08745Sheppo 
14140a55fbb7Slm66018 /*
14150a55fbb7Slm66018  * Function:
14160a55fbb7Slm66018  *	vdc_init_ver_negotiation()
14170a55fbb7Slm66018  *
14180a55fbb7Slm66018  * Description:
14190a55fbb7Slm66018  *
14200a55fbb7Slm66018  * Arguments:
14210a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
14220a55fbb7Slm66018  *
14230a55fbb7Slm66018  * Return Code:
14240a55fbb7Slm66018  *	0	- Success
14250a55fbb7Slm66018  */
14261ae08745Sheppo static int
14270a55fbb7Slm66018 vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver)
14281ae08745Sheppo {
14291ae08745Sheppo 	vio_ver_msg_t	pkt;
14301ae08745Sheppo 	size_t		msglen = sizeof (pkt);
14311ae08745Sheppo 	int		status = -1;
14321ae08745Sheppo 
14331ae08745Sheppo 	ASSERT(vdc != NULL);
14341ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
14351ae08745Sheppo 
14363af08d82Slm66018 	DMSG(vdc, 0, "[%d] Entered.\n", vdc->instance);
1437e1ebb9ecSlm66018 
14381ae08745Sheppo 	/*
14391ae08745Sheppo 	 * set the Session ID to a unique value
14401ae08745Sheppo 	 * (the lower 32 bits of the clock tick)
14411ae08745Sheppo 	 */
14421ae08745Sheppo 	vdc->session_id = ((uint32_t)gettick() & 0xffffffff);
14433af08d82Slm66018 	DMSG(vdc, 0, "[%d] Set SID to 0x%lx\n", vdc->instance, vdc->session_id);
14441ae08745Sheppo 
14451ae08745Sheppo 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
14461ae08745Sheppo 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
14471ae08745Sheppo 	pkt.tag.vio_subtype_env = VIO_VER_INFO;
14481ae08745Sheppo 	pkt.tag.vio_sid = vdc->session_id;
14491ae08745Sheppo 	pkt.dev_class = VDEV_DISK;
14500a55fbb7Slm66018 	pkt.ver_major = ver.major;
14510a55fbb7Slm66018 	pkt.ver_minor = ver.minor;
14521ae08745Sheppo 
14530a55fbb7Slm66018 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
14543af08d82Slm66018 	DMSG(vdc, 0, "[%d] Ver info sent (status = %d)\n",
14553af08d82Slm66018 	    vdc->instance, status);
14561ae08745Sheppo 	if ((status != 0) || (msglen != sizeof (vio_ver_msg_t))) {
14573af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to send Ver negotiation info: "
145887a7269eSachartre 		    "id(%lx) rv(%d) size(%ld)", vdc->instance, vdc->ldc_handle,
14591ae08745Sheppo 		    status, msglen);
14601ae08745Sheppo 		if (msglen != sizeof (vio_ver_msg_t))
14611ae08745Sheppo 			status = ENOMSG;
14621ae08745Sheppo 	}
14631ae08745Sheppo 
14641ae08745Sheppo 	return (status);
14651ae08745Sheppo }
14661ae08745Sheppo 
14670a55fbb7Slm66018 /*
14680a55fbb7Slm66018  * Function:
14693af08d82Slm66018  *	vdc_ver_negotiation()
14703af08d82Slm66018  *
14713af08d82Slm66018  * Description:
14723af08d82Slm66018  *
14733af08d82Slm66018  * Arguments:
14743af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
14753af08d82Slm66018  *
14763af08d82Slm66018  * Return Code:
14773af08d82Slm66018  *	0	- Success
14783af08d82Slm66018  */
14793af08d82Slm66018 static int
14803af08d82Slm66018 vdc_ver_negotiation(vdc_t *vdcp)
14813af08d82Slm66018 {
14823af08d82Slm66018 	vio_msg_t vio_msg;
14833af08d82Slm66018 	int status;
14843af08d82Slm66018 
14853af08d82Slm66018 	if (status = vdc_init_ver_negotiation(vdcp, vdc_version[0]))
14863af08d82Slm66018 		return (status);
14873af08d82Slm66018 
14883af08d82Slm66018 	/* release lock and wait for response */
14893af08d82Slm66018 	mutex_exit(&vdcp->lock);
14903af08d82Slm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
14913af08d82Slm66018 	mutex_enter(&vdcp->lock);
14923af08d82Slm66018 	if (status) {
14933af08d82Slm66018 		DMSG(vdcp, 0,
14943af08d82Slm66018 		    "[%d] Failed waiting for Ver negotiation response, rv(%d)",
14953af08d82Slm66018 		    vdcp->instance, status);
14963af08d82Slm66018 		return (status);
14973af08d82Slm66018 	}
14983af08d82Slm66018 
14993af08d82Slm66018 	/* check type and sub_type ... */
15003af08d82Slm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
15013af08d82Slm66018 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
15023af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Invalid ver negotiation response\n",
15033af08d82Slm66018 		    vdcp->instance);
15043af08d82Slm66018 		return (EPROTO);
15053af08d82Slm66018 	}
15063af08d82Slm66018 
15073af08d82Slm66018 	return (vdc_handle_ver_msg(vdcp, (vio_ver_msg_t *)&vio_msg));
15083af08d82Slm66018 }
15093af08d82Slm66018 
15103af08d82Slm66018 /*
15113af08d82Slm66018  * Function:
15120a55fbb7Slm66018  *	vdc_init_attr_negotiation()
15130a55fbb7Slm66018  *
15140a55fbb7Slm66018  * Description:
15150a55fbb7Slm66018  *
15160a55fbb7Slm66018  * Arguments:
15170a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
15180a55fbb7Slm66018  *
15190a55fbb7Slm66018  * Return Code:
15200a55fbb7Slm66018  *	0	- Success
15210a55fbb7Slm66018  */
15221ae08745Sheppo static int
15231ae08745Sheppo vdc_init_attr_negotiation(vdc_t *vdc)
15241ae08745Sheppo {
15251ae08745Sheppo 	vd_attr_msg_t	pkt;
15261ae08745Sheppo 	size_t		msglen = sizeof (pkt);
15271ae08745Sheppo 	int		status;
15281ae08745Sheppo 
15291ae08745Sheppo 	ASSERT(vdc != NULL);
15301ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
15311ae08745Sheppo 
15323af08d82Slm66018 	DMSG(vdc, 0, "[%d] entered\n", vdc->instance);
15331ae08745Sheppo 
15341ae08745Sheppo 	/* fill in tag */
15351ae08745Sheppo 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
15361ae08745Sheppo 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
15371ae08745Sheppo 	pkt.tag.vio_subtype_env = VIO_ATTR_INFO;
15381ae08745Sheppo 	pkt.tag.vio_sid = vdc->session_id;
15391ae08745Sheppo 	/* fill in payload */
15401ae08745Sheppo 	pkt.max_xfer_sz = vdc->max_xfer_sz;
15411ae08745Sheppo 	pkt.vdisk_block_size = vdc->block_size;
15421ae08745Sheppo 	pkt.xfer_mode = VIO_DRING_MODE;
15431ae08745Sheppo 	pkt.operations = 0;	/* server will set bits of valid operations */
15441ae08745Sheppo 	pkt.vdisk_type = 0;	/* server will set to valid device type */
15451ae08745Sheppo 	pkt.vdisk_size = 0;	/* server will set to valid size */
15461ae08745Sheppo 
15470a55fbb7Slm66018 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
15483af08d82Slm66018 	DMSG(vdc, 0, "Attr info sent (status = %d)\n", status);
15491ae08745Sheppo 
15501ae08745Sheppo 	if ((status != 0) || (msglen != sizeof (vio_ver_msg_t))) {
15513af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to send Attr negotiation info: "
155287a7269eSachartre 		    "id(%lx) rv(%d) size(%ld)", vdc->instance, vdc->ldc_handle,
15531ae08745Sheppo 		    status, msglen);
15541ae08745Sheppo 		if (msglen != sizeof (vio_ver_msg_t))
15551ae08745Sheppo 			status = ENOMSG;
15561ae08745Sheppo 	}
15571ae08745Sheppo 
15581ae08745Sheppo 	return (status);
15591ae08745Sheppo }
15601ae08745Sheppo 
15610a55fbb7Slm66018 /*
15620a55fbb7Slm66018  * Function:
15633af08d82Slm66018  *	vdc_attr_negotiation()
15643af08d82Slm66018  *
15653af08d82Slm66018  * Description:
15663af08d82Slm66018  *
15673af08d82Slm66018  * Arguments:
15683af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
15693af08d82Slm66018  *
15703af08d82Slm66018  * Return Code:
15713af08d82Slm66018  *	0	- Success
15723af08d82Slm66018  */
15733af08d82Slm66018 static int
15743af08d82Slm66018 vdc_attr_negotiation(vdc_t *vdcp)
15753af08d82Slm66018 {
15763af08d82Slm66018 	int status;
15773af08d82Slm66018 	vio_msg_t vio_msg;
15783af08d82Slm66018 
15793af08d82Slm66018 	if (status = vdc_init_attr_negotiation(vdcp))
15803af08d82Slm66018 		return (status);
15813af08d82Slm66018 
15823af08d82Slm66018 	/* release lock and wait for response */
15833af08d82Slm66018 	mutex_exit(&vdcp->lock);
15843af08d82Slm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
15853af08d82Slm66018 	mutex_enter(&vdcp->lock);
15863af08d82Slm66018 	if (status) {
15873af08d82Slm66018 		DMSG(vdcp, 0,
15883af08d82Slm66018 		    "[%d] Failed waiting for Attr negotiation response, rv(%d)",
15893af08d82Slm66018 		    vdcp->instance, status);
15903af08d82Slm66018 		return (status);
15913af08d82Slm66018 	}
15923af08d82Slm66018 
15933af08d82Slm66018 	/* check type and sub_type ... */
15943af08d82Slm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
15953af08d82Slm66018 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
15963af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Invalid attr negotiation response\n",
15973af08d82Slm66018 		    vdcp->instance);
15983af08d82Slm66018 		return (EPROTO);
15993af08d82Slm66018 	}
16003af08d82Slm66018 
16013af08d82Slm66018 	return (vdc_handle_attr_msg(vdcp, (vd_attr_msg_t *)&vio_msg));
16023af08d82Slm66018 }
16033af08d82Slm66018 
16043af08d82Slm66018 
16053af08d82Slm66018 /*
16063af08d82Slm66018  * Function:
16070a55fbb7Slm66018  *	vdc_init_dring_negotiate()
16080a55fbb7Slm66018  *
16090a55fbb7Slm66018  * Description:
16100a55fbb7Slm66018  *
16110a55fbb7Slm66018  * Arguments:
16120a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
16130a55fbb7Slm66018  *
16140a55fbb7Slm66018  * Return Code:
16150a55fbb7Slm66018  *	0	- Success
16160a55fbb7Slm66018  */
16171ae08745Sheppo static int
16181ae08745Sheppo vdc_init_dring_negotiate(vdc_t *vdc)
16191ae08745Sheppo {
16201ae08745Sheppo 	vio_dring_reg_msg_t	pkt;
16211ae08745Sheppo 	size_t			msglen = sizeof (pkt);
16221ae08745Sheppo 	int			status = -1;
16233af08d82Slm66018 	int			retry;
16243af08d82Slm66018 	int			nretries = 10;
16251ae08745Sheppo 
16261ae08745Sheppo 	ASSERT(vdc != NULL);
16271ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
16281ae08745Sheppo 
16293af08d82Slm66018 	for (retry = 0; retry < nretries; retry++) {
16301ae08745Sheppo 		status = vdc_init_descriptor_ring(vdc);
16313af08d82Slm66018 		if (status != EAGAIN)
16323af08d82Slm66018 			break;
16333af08d82Slm66018 		drv_usecwait(vdc_min_timeout_ldc);
16343af08d82Slm66018 	}
16353af08d82Slm66018 
16361ae08745Sheppo 	if (status != 0) {
16373af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to init DRing (status = %d)\n",
16381ae08745Sheppo 		    vdc->instance, status);
16391ae08745Sheppo 		return (status);
16401ae08745Sheppo 	}
16413af08d82Slm66018 
16423af08d82Slm66018 	DMSG(vdc, 0, "[%d] Init of descriptor ring completed (status = %d)\n",
1643e1ebb9ecSlm66018 	    vdc->instance, status);
16441ae08745Sheppo 
16451ae08745Sheppo 	/* fill in tag */
16461ae08745Sheppo 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
16471ae08745Sheppo 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
16481ae08745Sheppo 	pkt.tag.vio_subtype_env = VIO_DRING_REG;
16491ae08745Sheppo 	pkt.tag.vio_sid = vdc->session_id;
16501ae08745Sheppo 	/* fill in payload */
16511ae08745Sheppo 	pkt.dring_ident = 0;
1652e1ebb9ecSlm66018 	pkt.num_descriptors = vdc->dring_len;
1653e1ebb9ecSlm66018 	pkt.descriptor_size = vdc->dring_entry_size;
16541ae08745Sheppo 	pkt.options = (VIO_TX_DRING | VIO_RX_DRING);
16551ae08745Sheppo 	pkt.ncookies = vdc->dring_cookie_count;
16561ae08745Sheppo 	pkt.cookie[0] = vdc->dring_cookie[0];	/* for now just one cookie */
16571ae08745Sheppo 
16580a55fbb7Slm66018 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
16591ae08745Sheppo 	if (status != 0) {
16603af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to register DRing (err = %d)",
1661e1ebb9ecSlm66018 		    vdc->instance, status);
16621ae08745Sheppo 	}
16631ae08745Sheppo 
16641ae08745Sheppo 	return (status);
16651ae08745Sheppo }
16661ae08745Sheppo 
16671ae08745Sheppo 
16683af08d82Slm66018 /*
16693af08d82Slm66018  * Function:
16703af08d82Slm66018  *	vdc_dring_negotiation()
16713af08d82Slm66018  *
16723af08d82Slm66018  * Description:
16733af08d82Slm66018  *
16743af08d82Slm66018  * Arguments:
16753af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
16763af08d82Slm66018  *
16773af08d82Slm66018  * Return Code:
16783af08d82Slm66018  *	0	- Success
16793af08d82Slm66018  */
16803af08d82Slm66018 static int
16813af08d82Slm66018 vdc_dring_negotiation(vdc_t *vdcp)
16823af08d82Slm66018 {
16833af08d82Slm66018 	int status;
16843af08d82Slm66018 	vio_msg_t vio_msg;
16853af08d82Slm66018 
16863af08d82Slm66018 	if (status = vdc_init_dring_negotiate(vdcp))
16873af08d82Slm66018 		return (status);
16883af08d82Slm66018 
16893af08d82Slm66018 	/* release lock and wait for response */
16903af08d82Slm66018 	mutex_exit(&vdcp->lock);
16913af08d82Slm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
16923af08d82Slm66018 	mutex_enter(&vdcp->lock);
16933af08d82Slm66018 	if (status) {
16943af08d82Slm66018 		DMSG(vdcp, 0,
16953af08d82Slm66018 		    "[%d] Failed waiting for Dring negotiation response,"
16963af08d82Slm66018 		    " rv(%d)", vdcp->instance, status);
16973af08d82Slm66018 		return (status);
16983af08d82Slm66018 	}
16993af08d82Slm66018 
17003af08d82Slm66018 	/* check type and sub_type ... */
17013af08d82Slm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
17023af08d82Slm66018 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
17033af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Invalid Dring negotiation response\n",
17043af08d82Slm66018 		    vdcp->instance);
17053af08d82Slm66018 		return (EPROTO);
17063af08d82Slm66018 	}
17073af08d82Slm66018 
17083af08d82Slm66018 	return (vdc_handle_dring_reg_msg(vdcp,
17093af08d82Slm66018 	    (vio_dring_reg_msg_t *)&vio_msg));
17103af08d82Slm66018 }
17113af08d82Slm66018 
17123af08d82Slm66018 
17133af08d82Slm66018 /*
17143af08d82Slm66018  * Function:
17153af08d82Slm66018  *	vdc_send_rdx()
17163af08d82Slm66018  *
17173af08d82Slm66018  * Description:
17183af08d82Slm66018  *
17193af08d82Slm66018  * Arguments:
17203af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
17213af08d82Slm66018  *
17223af08d82Slm66018  * Return Code:
17233af08d82Slm66018  *	0	- Success
17243af08d82Slm66018  */
17253af08d82Slm66018 static int
17263af08d82Slm66018 vdc_send_rdx(vdc_t *vdcp)
17273af08d82Slm66018 {
17283af08d82Slm66018 	vio_msg_t	msg;
17293af08d82Slm66018 	size_t		msglen = sizeof (vio_msg_t);
17303af08d82Slm66018 	int		status;
17313af08d82Slm66018 
17323af08d82Slm66018 	/*
17333af08d82Slm66018 	 * Send an RDX message to vds to indicate we are ready
17343af08d82Slm66018 	 * to send data
17353af08d82Slm66018 	 */
17363af08d82Slm66018 	msg.tag.vio_msgtype = VIO_TYPE_CTRL;
17373af08d82Slm66018 	msg.tag.vio_subtype = VIO_SUBTYPE_INFO;
17383af08d82Slm66018 	msg.tag.vio_subtype_env = VIO_RDX;
17393af08d82Slm66018 	msg.tag.vio_sid = vdcp->session_id;
17403af08d82Slm66018 	status = vdc_send(vdcp, (caddr_t)&msg, &msglen);
17413af08d82Slm66018 	if (status != 0) {
17423af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Failed to send RDX message (%d)",
17433af08d82Slm66018 		    vdcp->instance, status);
17443af08d82Slm66018 	}
17453af08d82Slm66018 
17463af08d82Slm66018 	return (status);
17473af08d82Slm66018 }
17483af08d82Slm66018 
17493af08d82Slm66018 /*
17503af08d82Slm66018  * Function:
17513af08d82Slm66018  *	vdc_handle_rdx()
17523af08d82Slm66018  *
17533af08d82Slm66018  * Description:
17543af08d82Slm66018  *
17553af08d82Slm66018  * Arguments:
17563af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
17573af08d82Slm66018  *	msgp	- received msg
17583af08d82Slm66018  *
17593af08d82Slm66018  * Return Code:
17603af08d82Slm66018  *	0	- Success
17613af08d82Slm66018  */
17623af08d82Slm66018 static int
17633af08d82Slm66018 vdc_handle_rdx(vdc_t *vdcp, vio_rdx_msg_t *msgp)
17643af08d82Slm66018 {
17653af08d82Slm66018 	_NOTE(ARGUNUSED(vdcp))
17663af08d82Slm66018 	_NOTE(ARGUNUSED(msgp))
17673af08d82Slm66018 
17683af08d82Slm66018 	ASSERT(msgp->tag.vio_msgtype == VIO_TYPE_CTRL);
17693af08d82Slm66018 	ASSERT(msgp->tag.vio_subtype == VIO_SUBTYPE_ACK);
17703af08d82Slm66018 	ASSERT(msgp->tag.vio_subtype_env == VIO_RDX);
17713af08d82Slm66018 
17723af08d82Slm66018 	DMSG(vdcp, 1, "[%d] Got an RDX msg", vdcp->instance);
17733af08d82Slm66018 
17743af08d82Slm66018 	return (0);
17753af08d82Slm66018 }
17763af08d82Slm66018 
17773af08d82Slm66018 /*
17783af08d82Slm66018  * Function:
17793af08d82Slm66018  *	vdc_rdx_exchange()
17803af08d82Slm66018  *
17813af08d82Slm66018  * Description:
17823af08d82Slm66018  *
17833af08d82Slm66018  * Arguments:
17843af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
17853af08d82Slm66018  *
17863af08d82Slm66018  * Return Code:
17873af08d82Slm66018  *	0	- Success
17883af08d82Slm66018  */
17893af08d82Slm66018 static int
17903af08d82Slm66018 vdc_rdx_exchange(vdc_t *vdcp)
17913af08d82Slm66018 {
17923af08d82Slm66018 	int status;
17933af08d82Slm66018 	vio_msg_t vio_msg;
17943af08d82Slm66018 
17953af08d82Slm66018 	if (status = vdc_send_rdx(vdcp))
17963af08d82Slm66018 		return (status);
17973af08d82Slm66018 
17983af08d82Slm66018 	/* release lock and wait for response */
17993af08d82Slm66018 	mutex_exit(&vdcp->lock);
18003af08d82Slm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
18013af08d82Slm66018 	mutex_enter(&vdcp->lock);
18023af08d82Slm66018 	if (status) {
180387a7269eSachartre 		DMSG(vdcp, 0, "[%d] Failed waiting for RDX response, rv(%d)",
180487a7269eSachartre 		    vdcp->instance, status);
18053af08d82Slm66018 		return (status);
18063af08d82Slm66018 	}
18073af08d82Slm66018 
18083af08d82Slm66018 	/* check type and sub_type ... */
18093af08d82Slm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
18103af08d82Slm66018 	    vio_msg.tag.vio_subtype != VIO_SUBTYPE_ACK) {
181187a7269eSachartre 		DMSG(vdcp, 0, "[%d] Invalid RDX response\n", vdcp->instance);
18123af08d82Slm66018 		return (EPROTO);
18133af08d82Slm66018 	}
18143af08d82Slm66018 
18153af08d82Slm66018 	return (vdc_handle_rdx(vdcp, (vio_rdx_msg_t *)&vio_msg));
18163af08d82Slm66018 }
18173af08d82Slm66018 
18183af08d82Slm66018 
18191ae08745Sheppo /* -------------------------------------------------------------------------- */
18201ae08745Sheppo 
18211ae08745Sheppo /*
18221ae08745Sheppo  * LDC helper routines
18231ae08745Sheppo  */
18241ae08745Sheppo 
18253af08d82Slm66018 static int
18263af08d82Slm66018 vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp)
18273af08d82Slm66018 {
18283af08d82Slm66018 	int		status;
18293af08d82Slm66018 	boolean_t	q_has_pkts = B_FALSE;
18303af08d82Slm66018 	int		delay_time;
18313af08d82Slm66018 	size_t		len;
18323af08d82Slm66018 
18333af08d82Slm66018 	mutex_enter(&vdc->read_lock);
18343af08d82Slm66018 
18353af08d82Slm66018 	if (vdc->read_state == VDC_READ_IDLE)
18363af08d82Slm66018 		vdc->read_state = VDC_READ_WAITING;
18373af08d82Slm66018 
18383af08d82Slm66018 	while (vdc->read_state != VDC_READ_PENDING) {
18393af08d82Slm66018 
18403af08d82Slm66018 		/* detect if the connection has been reset */
18413af08d82Slm66018 		if (vdc->read_state == VDC_READ_RESET) {
18423af08d82Slm66018 			status = ECONNRESET;
18433af08d82Slm66018 			goto done;
18443af08d82Slm66018 		}
18453af08d82Slm66018 
18463af08d82Slm66018 		cv_wait(&vdc->read_cv, &vdc->read_lock);
18473af08d82Slm66018 	}
18483af08d82Slm66018 
18493af08d82Slm66018 	/*
18503af08d82Slm66018 	 * Until we get a blocking ldc read we have to retry
18513af08d82Slm66018 	 * until the entire LDC message has arrived before
18523af08d82Slm66018 	 * ldc_read() will succeed. Note we also bail out if
1853eff7243fSlm66018 	 * the channel is reset or goes away.
18543af08d82Slm66018 	 */
18553af08d82Slm66018 	delay_time = vdc_ldc_read_init_delay;
18563af08d82Slm66018 loop:
18573af08d82Slm66018 	len = *nbytesp;
18583af08d82Slm66018 	status = ldc_read(vdc->ldc_handle, (caddr_t)msgp, &len);
18593af08d82Slm66018 	switch (status) {
18603af08d82Slm66018 	case EAGAIN:
18613af08d82Slm66018 		delay_time *= 2;
18623af08d82Slm66018 		if (delay_time >= vdc_ldc_read_max_delay)
18633af08d82Slm66018 			delay_time = vdc_ldc_read_max_delay;
18643af08d82Slm66018 		delay(delay_time);
18653af08d82Slm66018 		goto loop;
18663af08d82Slm66018 
18673af08d82Slm66018 	case 0:
18683af08d82Slm66018 		if (len == 0) {
18693af08d82Slm66018 			DMSG(vdc, 0, "[%d] ldc_read returned 0 bytes with "
18703af08d82Slm66018 			    "no error!\n", vdc->instance);
18713af08d82Slm66018 			goto loop;
18723af08d82Slm66018 		}
18733af08d82Slm66018 
18743af08d82Slm66018 		*nbytesp = len;
18753af08d82Slm66018 
18763af08d82Slm66018 		/*
18773af08d82Slm66018 		 * If there are pending messages, leave the
18783af08d82Slm66018 		 * read state as pending. Otherwise, set the state
18793af08d82Slm66018 		 * back to idle.
18803af08d82Slm66018 		 */
18813af08d82Slm66018 		status = ldc_chkq(vdc->ldc_handle, &q_has_pkts);
18823af08d82Slm66018 		if (status == 0 && !q_has_pkts)
18833af08d82Slm66018 			vdc->read_state = VDC_READ_IDLE;
18843af08d82Slm66018 
18853af08d82Slm66018 		break;
18863af08d82Slm66018 	default:
18873af08d82Slm66018 		DMSG(vdc, 0, "ldc_read returned %d\n", status);
18883af08d82Slm66018 		break;
18893af08d82Slm66018 	}
18903af08d82Slm66018 
18913af08d82Slm66018 done:
18923af08d82Slm66018 	mutex_exit(&vdc->read_lock);
18933af08d82Slm66018 
18943af08d82Slm66018 	return (status);
18953af08d82Slm66018 }
18963af08d82Slm66018 
18973af08d82Slm66018 
18983af08d82Slm66018 
18993af08d82Slm66018 #ifdef DEBUG
19003af08d82Slm66018 void
19013af08d82Slm66018 vdc_decode_tag(vdc_t *vdcp, vio_msg_t *msg)
19023af08d82Slm66018 {
19033af08d82Slm66018 	char *ms, *ss, *ses;
19043af08d82Slm66018 	switch (msg->tag.vio_msgtype) {
19053af08d82Slm66018 #define	Q(_s)	case _s : ms = #_s; break;
19063af08d82Slm66018 	Q(VIO_TYPE_CTRL)
19073af08d82Slm66018 	Q(VIO_TYPE_DATA)
19083af08d82Slm66018 	Q(VIO_TYPE_ERR)
19093af08d82Slm66018 #undef Q
19103af08d82Slm66018 	default: ms = "unknown"; break;
19113af08d82Slm66018 	}
19123af08d82Slm66018 
19133af08d82Slm66018 	switch (msg->tag.vio_subtype) {
19143af08d82Slm66018 #define	Q(_s)	case _s : ss = #_s; break;
19153af08d82Slm66018 	Q(VIO_SUBTYPE_INFO)
19163af08d82Slm66018 	Q(VIO_SUBTYPE_ACK)
19173af08d82Slm66018 	Q(VIO_SUBTYPE_NACK)
19183af08d82Slm66018 #undef Q
19193af08d82Slm66018 	default: ss = "unknown"; break;
19203af08d82Slm66018 	}
19213af08d82Slm66018 
19223af08d82Slm66018 	switch (msg->tag.vio_subtype_env) {
19233af08d82Slm66018 #define	Q(_s)	case _s : ses = #_s; break;
19243af08d82Slm66018 	Q(VIO_VER_INFO)
19253af08d82Slm66018 	Q(VIO_ATTR_INFO)
19263af08d82Slm66018 	Q(VIO_DRING_REG)
19273af08d82Slm66018 	Q(VIO_DRING_UNREG)
19283af08d82Slm66018 	Q(VIO_RDX)
19293af08d82Slm66018 	Q(VIO_PKT_DATA)
19303af08d82Slm66018 	Q(VIO_DESC_DATA)
19313af08d82Slm66018 	Q(VIO_DRING_DATA)
19323af08d82Slm66018 #undef Q
19333af08d82Slm66018 	default: ses = "unknown"; break;
19343af08d82Slm66018 	}
19353af08d82Slm66018 
19363af08d82Slm66018 	DMSG(vdcp, 3, "(%x/%x/%x) message : (%s/%s/%s)\n",
19373af08d82Slm66018 	    msg->tag.vio_msgtype, msg->tag.vio_subtype,
19383af08d82Slm66018 	    msg->tag.vio_subtype_env, ms, ss, ses);
19393af08d82Slm66018 }
19403af08d82Slm66018 #endif
19413af08d82Slm66018 
19421ae08745Sheppo /*
19431ae08745Sheppo  * Function:
19441ae08745Sheppo  *	vdc_send()
19451ae08745Sheppo  *
19461ae08745Sheppo  * Description:
19471ae08745Sheppo  *	The function encapsulates the call to write a message using LDC.
19481ae08745Sheppo  *	If LDC indicates that the call failed due to the queue being full,
19491ae08745Sheppo  *	we retry the ldc_write() [ up to 'vdc_retries' time ], otherwise
19501ae08745Sheppo  *	we return the error returned by LDC.
19511ae08745Sheppo  *
19521ae08745Sheppo  * Arguments:
19531ae08745Sheppo  *	ldc_handle	- LDC handle for the channel this instance of vdc uses
19541ae08745Sheppo  *	pkt		- address of LDC message to be sent
19551ae08745Sheppo  *	msglen		- the size of the message being sent. When the function
19561ae08745Sheppo  *			  returns, this contains the number of bytes written.
19571ae08745Sheppo  *
19581ae08745Sheppo  * Return Code:
19591ae08745Sheppo  *	0		- Success.
19601ae08745Sheppo  *	EINVAL		- pkt or msglen were NULL
19611ae08745Sheppo  *	ECONNRESET	- The connection was not up.
19621ae08745Sheppo  *	EWOULDBLOCK	- LDC queue is full
19631ae08745Sheppo  *	xxx		- other error codes returned by ldc_write
19641ae08745Sheppo  */
19651ae08745Sheppo static int
19660a55fbb7Slm66018 vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen)
19671ae08745Sheppo {
19681ae08745Sheppo 	size_t	size = 0;
19691ae08745Sheppo 	int	status = 0;
19703af08d82Slm66018 	clock_t delay_ticks;
19711ae08745Sheppo 
19720a55fbb7Slm66018 	ASSERT(vdc != NULL);
19730a55fbb7Slm66018 	ASSERT(mutex_owned(&vdc->lock));
19741ae08745Sheppo 	ASSERT(msglen != NULL);
19751ae08745Sheppo 	ASSERT(*msglen != 0);
19761ae08745Sheppo 
19773af08d82Slm66018 #ifdef DEBUG
19783af08d82Slm66018 	vdc_decode_tag(vdc, (vio_msg_t *)pkt);
19793af08d82Slm66018 #endif
19803af08d82Slm66018 	/*
19813af08d82Slm66018 	 * Wait indefinitely to send if channel
19823af08d82Slm66018 	 * is busy, but bail out if we succeed or
19833af08d82Slm66018 	 * if the channel closes or is reset.
19843af08d82Slm66018 	 */
19853af08d82Slm66018 	delay_ticks = vdc_hz_min_ldc_delay;
19861ae08745Sheppo 	do {
19871ae08745Sheppo 		size = *msglen;
19880a55fbb7Slm66018 		status = ldc_write(vdc->ldc_handle, pkt, &size);
19893af08d82Slm66018 		if (status == EWOULDBLOCK) {
19903af08d82Slm66018 			delay(delay_ticks);
19913af08d82Slm66018 			/* geometric backoff */
19923af08d82Slm66018 			delay_ticks *= 2;
19933af08d82Slm66018 			if (delay_ticks > vdc_hz_max_ldc_delay)
19943af08d82Slm66018 				delay_ticks = vdc_hz_max_ldc_delay;
19953af08d82Slm66018 		}
19963af08d82Slm66018 	} while (status == EWOULDBLOCK);
19971ae08745Sheppo 
19980a55fbb7Slm66018 	/* if LDC had serious issues --- reset vdc state */
19990a55fbb7Slm66018 	if (status == EIO || status == ECONNRESET) {
20003af08d82Slm66018 		/* LDC had serious issues --- reset vdc state */
20013af08d82Slm66018 		mutex_enter(&vdc->read_lock);
20023af08d82Slm66018 		if ((vdc->read_state == VDC_READ_WAITING) ||
20033af08d82Slm66018 		    (vdc->read_state == VDC_READ_RESET))
20043af08d82Slm66018 			cv_signal(&vdc->read_cv);
20053af08d82Slm66018 		vdc->read_state = VDC_READ_RESET;
20063af08d82Slm66018 		mutex_exit(&vdc->read_lock);
20073af08d82Slm66018 
20083af08d82Slm66018 		/* wake up any waiters in the reset thread */
20093af08d82Slm66018 		if (vdc->state == VDC_STATE_INIT_WAITING) {
20103af08d82Slm66018 			DMSG(vdc, 0, "[%d] write reset - "
20113af08d82Slm66018 			    "vdc is resetting ..\n", vdc->instance);
20123af08d82Slm66018 			vdc->state = VDC_STATE_RESETTING;
20133af08d82Slm66018 			cv_signal(&vdc->initwait_cv);
20143af08d82Slm66018 		}
20153af08d82Slm66018 
20163af08d82Slm66018 		return (ECONNRESET);
20170a55fbb7Slm66018 	}
20180a55fbb7Slm66018 
20191ae08745Sheppo 	/* return the last size written */
20201ae08745Sheppo 	*msglen = size;
20211ae08745Sheppo 
20221ae08745Sheppo 	return (status);
20231ae08745Sheppo }
20241ae08745Sheppo 
20251ae08745Sheppo /*
20261ae08745Sheppo  * Function:
2027655fd6a9Sachartre  *	vdc_get_md_node
20281ae08745Sheppo  *
20291ae08745Sheppo  * Description:
2030655fd6a9Sachartre  *	Get the MD, the device node and the port node for the given
2031655fd6a9Sachartre  *	disk instance. The caller is responsible for cleaning up the
2032655fd6a9Sachartre  *	reference to the returned MD (mdpp) by calling md_fini_handle().
20331ae08745Sheppo  *
20341ae08745Sheppo  * Arguments:
20351ae08745Sheppo  *	dip	- dev info pointer for this instance of the device driver.
2036655fd6a9Sachartre  *	mdpp	- the returned MD.
2037655fd6a9Sachartre  *	vd_nodep - the returned device node.
2038655fd6a9Sachartre  *	vd_portp - the returned port node. The returned port node is NULL
2039655fd6a9Sachartre  *		   if no port node is found.
20401ae08745Sheppo  *
20411ae08745Sheppo  * Return Code:
20421ae08745Sheppo  *	0	- Success.
20431ae08745Sheppo  *	ENOENT	- Expected node or property did not exist.
20441ae08745Sheppo  *	ENXIO	- Unexpected error communicating with MD framework
20451ae08745Sheppo  */
20461ae08745Sheppo static int
2047655fd6a9Sachartre vdc_get_md_node(dev_info_t *dip, md_t **mdpp, mde_cookie_t *vd_nodep,
2048655fd6a9Sachartre     mde_cookie_t *vd_portp)
20491ae08745Sheppo {
20501ae08745Sheppo 	int		status = ENOENT;
20511ae08745Sheppo 	char		*node_name = NULL;
20521ae08745Sheppo 	md_t		*mdp = NULL;
20531ae08745Sheppo 	int		num_nodes;
20541ae08745Sheppo 	int		num_vdevs;
2055655fd6a9Sachartre 	int		num_vports;
20561ae08745Sheppo 	mde_cookie_t	rootnode;
20571ae08745Sheppo 	mde_cookie_t	*listp = NULL;
20581ae08745Sheppo 	boolean_t	found_inst = B_FALSE;
20591ae08745Sheppo 	int		listsz;
20601ae08745Sheppo 	int		idx;
20611ae08745Sheppo 	uint64_t	md_inst;
20621ae08745Sheppo 	int		obp_inst;
20631ae08745Sheppo 	int		instance = ddi_get_instance(dip);
20641ae08745Sheppo 
20651ae08745Sheppo 	/*
20661ae08745Sheppo 	 * Get the OBP instance number for comparison with the MD instance
20671ae08745Sheppo 	 *
20681ae08745Sheppo 	 * The "cfg-handle" property of a vdc node in an MD contains the MD's
20691ae08745Sheppo 	 * notion of "instance", or unique identifier, for that node; OBP
20701ae08745Sheppo 	 * stores the value of the "cfg-handle" MD property as the value of
20711ae08745Sheppo 	 * the "reg" property on the node in the device tree it builds from
20721ae08745Sheppo 	 * the MD and passes to Solaris.  Thus, we look up the devinfo node's
20731ae08745Sheppo 	 * "reg" property value to uniquely identify this device instance.
20741ae08745Sheppo 	 * If the "reg" property cannot be found, the device tree state is
20751ae08745Sheppo 	 * presumably so broken that there is no point in continuing.
20761ae08745Sheppo 	 */
20771ae08745Sheppo 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, OBP_REG)) {
20781ae08745Sheppo 		cmn_err(CE_WARN, "'%s' property does not exist", OBP_REG);
20791ae08745Sheppo 		return (ENOENT);
20801ae08745Sheppo 	}
20811ae08745Sheppo 	obp_inst = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
20821ae08745Sheppo 	    OBP_REG, -1);
20833af08d82Slm66018 	DMSGX(1, "[%d] OBP inst=%d\n", instance, obp_inst);
20841ae08745Sheppo 
20851ae08745Sheppo 	/*
2086655fd6a9Sachartre 	 * We now walk the MD nodes to find the node for this vdisk.
20871ae08745Sheppo 	 */
20881ae08745Sheppo 	if ((mdp = md_get_handle()) == NULL) {
20891ae08745Sheppo 		cmn_err(CE_WARN, "unable to init machine description");
20901ae08745Sheppo 		return (ENXIO);
20911ae08745Sheppo 	}
20921ae08745Sheppo 
20931ae08745Sheppo 	num_nodes = md_node_count(mdp);
20941ae08745Sheppo 	ASSERT(num_nodes > 0);
20951ae08745Sheppo 
20961ae08745Sheppo 	listsz = num_nodes * sizeof (mde_cookie_t);
20971ae08745Sheppo 
20981ae08745Sheppo 	/* allocate memory for nodes */
20991ae08745Sheppo 	listp = kmem_zalloc(listsz, KM_SLEEP);
21001ae08745Sheppo 
21011ae08745Sheppo 	rootnode = md_root_node(mdp);
21021ae08745Sheppo 	ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE);
21031ae08745Sheppo 
21041ae08745Sheppo 	/*
21051ae08745Sheppo 	 * Search for all the virtual devices, we will then check to see which
21061ae08745Sheppo 	 * ones are disk nodes.
21071ae08745Sheppo 	 */
21081ae08745Sheppo 	num_vdevs = md_scan_dag(mdp, rootnode,
21091ae08745Sheppo 	    md_find_name(mdp, VDC_MD_VDEV_NAME),
21101ae08745Sheppo 	    md_find_name(mdp, "fwd"), listp);
21111ae08745Sheppo 
21121ae08745Sheppo 	if (num_vdevs <= 0) {
21131ae08745Sheppo 		cmn_err(CE_NOTE, "No '%s' node found", VDC_MD_VDEV_NAME);
21141ae08745Sheppo 		status = ENOENT;
21151ae08745Sheppo 		goto done;
21161ae08745Sheppo 	}
21171ae08745Sheppo 
21183af08d82Slm66018 	DMSGX(1, "[%d] num_vdevs=%d\n", instance, num_vdevs);
21191ae08745Sheppo 	for (idx = 0; idx < num_vdevs; idx++) {
21201ae08745Sheppo 		status = md_get_prop_str(mdp, listp[idx], "name", &node_name);
21211ae08745Sheppo 		if ((status != 0) || (node_name == NULL)) {
21221ae08745Sheppo 			cmn_err(CE_NOTE, "Unable to get name of node type '%s'"
21231ae08745Sheppo 			    ": err %d", VDC_MD_VDEV_NAME, status);
21241ae08745Sheppo 			continue;
21251ae08745Sheppo 		}
21261ae08745Sheppo 
21273af08d82Slm66018 		DMSGX(1, "[%d] Found node '%s'\n", instance, node_name);
21281ae08745Sheppo 		if (strcmp(VDC_MD_DISK_NAME, node_name) == 0) {
21291ae08745Sheppo 			status = md_get_prop_val(mdp, listp[idx],
21301ae08745Sheppo 			    VDC_MD_CFG_HDL, &md_inst);
21313af08d82Slm66018 			DMSGX(1, "[%d] vdc inst in MD=%lx\n",
21323af08d82Slm66018 			    instance, md_inst);
21331ae08745Sheppo 			if ((status == 0) && (md_inst == obp_inst)) {
21341ae08745Sheppo 				found_inst = B_TRUE;
21351ae08745Sheppo 				break;
21361ae08745Sheppo 			}
21371ae08745Sheppo 		}
21381ae08745Sheppo 	}
21391ae08745Sheppo 
21400a55fbb7Slm66018 	if (!found_inst) {
21413af08d82Slm66018 		DMSGX(0, "Unable to find correct '%s' node", VDC_MD_DISK_NAME);
21421ae08745Sheppo 		status = ENOENT;
21431ae08745Sheppo 		goto done;
21441ae08745Sheppo 	}
21453af08d82Slm66018 	DMSGX(0, "[%d] MD inst=%lx\n", instance, md_inst);
21461ae08745Sheppo 
2147655fd6a9Sachartre 	*vd_nodep = listp[idx];
2148655fd6a9Sachartre 	*mdpp = mdp;
2149655fd6a9Sachartre 
2150655fd6a9Sachartre 	num_vports = md_scan_dag(mdp, *vd_nodep,
2151655fd6a9Sachartre 	    md_find_name(mdp, VDC_MD_PORT_NAME),
2152655fd6a9Sachartre 	    md_find_name(mdp, "fwd"), listp);
2153655fd6a9Sachartre 
2154655fd6a9Sachartre 	if (num_vports != 1) {
2155655fd6a9Sachartre 		DMSGX(0, "Expected 1 '%s' node for '%s' port, found %d\n",
2156655fd6a9Sachartre 		    VDC_MD_PORT_NAME, VDC_MD_VDEV_NAME, num_vports);
2157655fd6a9Sachartre 	}
2158655fd6a9Sachartre 
2159655fd6a9Sachartre 	*vd_portp = (num_vports == 0)? NULL: listp[0];
2160655fd6a9Sachartre 
2161655fd6a9Sachartre done:
2162655fd6a9Sachartre 	kmem_free(listp, listsz);
2163655fd6a9Sachartre 	return (status);
2164655fd6a9Sachartre }
2165655fd6a9Sachartre 
2166655fd6a9Sachartre /*
2167655fd6a9Sachartre  * Function:
2168655fd6a9Sachartre  *	vdc_get_ldc_id()
2169655fd6a9Sachartre  *
2170655fd6a9Sachartre  * Description:
2171655fd6a9Sachartre  *	This function gets the 'ldc-id' for this particular instance of vdc.
2172655fd6a9Sachartre  *	The id returned is the guest domain channel endpoint LDC uses for
2173655fd6a9Sachartre  *	communication with vds.
2174655fd6a9Sachartre  *
2175655fd6a9Sachartre  * Arguments:
2176655fd6a9Sachartre  *	mdp	- pointer to the machine description.
2177655fd6a9Sachartre  *	vd_node	- the vdisk element from the MD.
2178655fd6a9Sachartre  *	ldc_id	- pointer to variable used to return the 'ldc-id' found.
2179655fd6a9Sachartre  *
2180655fd6a9Sachartre  * Return Code:
2181655fd6a9Sachartre  *	0	- Success.
2182655fd6a9Sachartre  *	ENOENT	- Expected node or property did not exist.
2183655fd6a9Sachartre  */
2184655fd6a9Sachartre static int
2185655fd6a9Sachartre vdc_get_ldc_id(md_t *mdp, mde_cookie_t vd_node, uint64_t *ldc_id)
2186655fd6a9Sachartre {
2187655fd6a9Sachartre 	mde_cookie_t	*chanp = NULL;
2188655fd6a9Sachartre 	int		listsz;
2189655fd6a9Sachartre 	int		num_chans;
2190655fd6a9Sachartre 	int		num_nodes;
2191655fd6a9Sachartre 	int		status = 0;
2192655fd6a9Sachartre 
2193655fd6a9Sachartre 	num_nodes = md_node_count(mdp);
2194655fd6a9Sachartre 	ASSERT(num_nodes > 0);
2195655fd6a9Sachartre 
2196655fd6a9Sachartre 	listsz = num_nodes * sizeof (mde_cookie_t);
2197655fd6a9Sachartre 
2198655fd6a9Sachartre 	/* allocate memory for nodes */
2199655fd6a9Sachartre 	chanp = kmem_zalloc(listsz, KM_SLEEP);
2200655fd6a9Sachartre 
22011ae08745Sheppo 	/* get the channels for this node */
2202655fd6a9Sachartre 	num_chans = md_scan_dag(mdp, vd_node,
22031ae08745Sheppo 	    md_find_name(mdp, VDC_MD_CHAN_NAME),
22041ae08745Sheppo 	    md_find_name(mdp, "fwd"), chanp);
22051ae08745Sheppo 
22061ae08745Sheppo 	/* expecting at least one channel */
22071ae08745Sheppo 	if (num_chans <= 0) {
22081ae08745Sheppo 		cmn_err(CE_NOTE, "No '%s' node for '%s' port",
22091ae08745Sheppo 		    VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME);
22101ae08745Sheppo 		status = ENOENT;
22111ae08745Sheppo 		goto done;
22121ae08745Sheppo 
22131ae08745Sheppo 	} else if (num_chans != 1) {
2214655fd6a9Sachartre 		DMSGX(0, "Expected 1 '%s' node for '%s' port, found %d\n",
2215655fd6a9Sachartre 		    VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME, num_chans);
22161ae08745Sheppo 	}
22171ae08745Sheppo 
22181ae08745Sheppo 	/*
22191ae08745Sheppo 	 * We use the first channel found (index 0), irrespective of how
22201ae08745Sheppo 	 * many are there in total.
22211ae08745Sheppo 	 */
2222655fd6a9Sachartre 	if (md_get_prop_val(mdp, chanp[0], VDC_MD_ID, ldc_id) != 0) {
2223655fd6a9Sachartre 		cmn_err(CE_NOTE, "Channel '%s' property not found", VDC_MD_ID);
22241ae08745Sheppo 		status = ENOENT;
22251ae08745Sheppo 	}
22261ae08745Sheppo 
22271ae08745Sheppo done:
22281ae08745Sheppo 	kmem_free(chanp, listsz);
22291ae08745Sheppo 	return (status);
22301ae08745Sheppo }
22311ae08745Sheppo 
22320a55fbb7Slm66018 static int
22330a55fbb7Slm66018 vdc_do_ldc_up(vdc_t *vdc)
22340a55fbb7Slm66018 {
22350a55fbb7Slm66018 	int		status;
22363af08d82Slm66018 	ldc_status_t	ldc_state;
22370a55fbb7Slm66018 
22383af08d82Slm66018 	DMSG(vdc, 0, "[%d] Bringing up channel %lx\n",
22393af08d82Slm66018 	    vdc->instance, vdc->ldc_id);
22403af08d82Slm66018 
22413af08d82Slm66018 	if (vdc->lifecycle == VDC_LC_DETACHING)
22423af08d82Slm66018 		return (EINVAL);
22430a55fbb7Slm66018 
22440a55fbb7Slm66018 	if ((status = ldc_up(vdc->ldc_handle)) != 0) {
22450a55fbb7Slm66018 		switch (status) {
22460a55fbb7Slm66018 		case ECONNREFUSED:	/* listener not ready at other end */
22473af08d82Slm66018 			DMSG(vdc, 0, "[%d] ldc_up(%lx,...) return %d\n",
2248e1ebb9ecSlm66018 			    vdc->instance, vdc->ldc_id, status);
22490a55fbb7Slm66018 			status = 0;
22500a55fbb7Slm66018 			break;
22510a55fbb7Slm66018 		default:
22523af08d82Slm66018 			DMSG(vdc, 0, "[%d] Failed to bring up LDC: "
22533af08d82Slm66018 			    "channel=%ld, err=%d", vdc->instance, vdc->ldc_id,
22543af08d82Slm66018 			    status);
22553af08d82Slm66018 			break;
22563af08d82Slm66018 		}
22573af08d82Slm66018 	}
22583af08d82Slm66018 
22593af08d82Slm66018 	if (ldc_status(vdc->ldc_handle, &ldc_state) == 0) {
22603af08d82Slm66018 		vdc->ldc_state = ldc_state;
22613af08d82Slm66018 		if (ldc_state == LDC_UP) {
22623af08d82Slm66018 			DMSG(vdc, 0, "[%d] LDC channel already up\n",
22633af08d82Slm66018 			    vdc->instance);
22643af08d82Slm66018 			vdc->seq_num = 1;
22653af08d82Slm66018 			vdc->seq_num_reply = 0;
22660a55fbb7Slm66018 		}
22670a55fbb7Slm66018 	}
22680a55fbb7Slm66018 
22690a55fbb7Slm66018 	return (status);
22700a55fbb7Slm66018 }
22710a55fbb7Slm66018 
22720a55fbb7Slm66018 /*
22730a55fbb7Slm66018  * Function:
22740a55fbb7Slm66018  *	vdc_terminate_ldc()
22750a55fbb7Slm66018  *
22760a55fbb7Slm66018  * Description:
22770a55fbb7Slm66018  *
22780a55fbb7Slm66018  * Arguments:
22790a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
22800a55fbb7Slm66018  *
22810a55fbb7Slm66018  * Return Code:
22820a55fbb7Slm66018  *	None
22830a55fbb7Slm66018  */
22841ae08745Sheppo static void
22851ae08745Sheppo vdc_terminate_ldc(vdc_t *vdc)
22861ae08745Sheppo {
22871ae08745Sheppo 	int	instance = ddi_get_instance(vdc->dip);
22881ae08745Sheppo 
22891ae08745Sheppo 	ASSERT(vdc != NULL);
22901ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
22911ae08745Sheppo 
22923af08d82Slm66018 	DMSG(vdc, 0, "[%d] initialized=%x\n", instance, vdc->initialized);
22931ae08745Sheppo 
22941ae08745Sheppo 	if (vdc->initialized & VDC_LDC_OPEN) {
22953af08d82Slm66018 		DMSG(vdc, 0, "[%d] ldc_close()\n", instance);
22961ae08745Sheppo 		(void) ldc_close(vdc->ldc_handle);
22971ae08745Sheppo 	}
22981ae08745Sheppo 	if (vdc->initialized & VDC_LDC_CB) {
22993af08d82Slm66018 		DMSG(vdc, 0, "[%d] ldc_unreg_callback()\n", instance);
23001ae08745Sheppo 		(void) ldc_unreg_callback(vdc->ldc_handle);
23011ae08745Sheppo 	}
23021ae08745Sheppo 	if (vdc->initialized & VDC_LDC) {
23033af08d82Slm66018 		DMSG(vdc, 0, "[%d] ldc_fini()\n", instance);
23041ae08745Sheppo 		(void) ldc_fini(vdc->ldc_handle);
23051ae08745Sheppo 		vdc->ldc_handle = NULL;
23061ae08745Sheppo 	}
23071ae08745Sheppo 
23081ae08745Sheppo 	vdc->initialized &= ~(VDC_LDC | VDC_LDC_CB | VDC_LDC_OPEN);
23091ae08745Sheppo }
23101ae08745Sheppo 
23111ae08745Sheppo /* -------------------------------------------------------------------------- */
23121ae08745Sheppo 
23131ae08745Sheppo /*
23141ae08745Sheppo  * Descriptor Ring helper routines
23151ae08745Sheppo  */
23161ae08745Sheppo 
23170a55fbb7Slm66018 /*
23180a55fbb7Slm66018  * Function:
23190a55fbb7Slm66018  *	vdc_init_descriptor_ring()
23200a55fbb7Slm66018  *
23210a55fbb7Slm66018  * Description:
23220a55fbb7Slm66018  *
23230a55fbb7Slm66018  * Arguments:
23240a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
23250a55fbb7Slm66018  *
23260a55fbb7Slm66018  * Return Code:
23270a55fbb7Slm66018  *	0	- Success
23280a55fbb7Slm66018  */
23291ae08745Sheppo static int
23301ae08745Sheppo vdc_init_descriptor_ring(vdc_t *vdc)
23311ae08745Sheppo {
23321ae08745Sheppo 	vd_dring_entry_t	*dep = NULL;	/* DRing Entry pointer */
23330a55fbb7Slm66018 	int	status = 0;
23341ae08745Sheppo 	int	i;
23351ae08745Sheppo 
23363af08d82Slm66018 	DMSG(vdc, 0, "[%d] initialized=%x\n", vdc->instance, vdc->initialized);
23371ae08745Sheppo 
23381ae08745Sheppo 	ASSERT(vdc != NULL);
23391ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
23401ae08745Sheppo 	ASSERT(vdc->ldc_handle != NULL);
23411ae08745Sheppo 
2342e1ebb9ecSlm66018 	/* ensure we have enough room to store max sized block */
2343e1ebb9ecSlm66018 	ASSERT(maxphys <= VD_MAX_BLOCK_SIZE);
2344e1ebb9ecSlm66018 
23450a55fbb7Slm66018 	if ((vdc->initialized & VDC_DRING_INIT) == 0) {
23463af08d82Slm66018 		DMSG(vdc, 0, "[%d] ldc_mem_dring_create\n", vdc->instance);
2347e1ebb9ecSlm66018 		/*
2348e1ebb9ecSlm66018 		 * Calculate the maximum block size we can transmit using one
2349e1ebb9ecSlm66018 		 * Descriptor Ring entry from the attributes returned by the
2350e1ebb9ecSlm66018 		 * vDisk server. This is subject to a minimum of 'maxphys'
2351e1ebb9ecSlm66018 		 * as we do not have the capability to split requests over
2352e1ebb9ecSlm66018 		 * multiple DRing entries.
2353e1ebb9ecSlm66018 		 */
2354e1ebb9ecSlm66018 		if ((vdc->max_xfer_sz * vdc->block_size) < maxphys) {
23553af08d82Slm66018 			DMSG(vdc, 0, "[%d] using minimum DRing size\n",
2356e1ebb9ecSlm66018 			    vdc->instance);
2357e1ebb9ecSlm66018 			vdc->dring_max_cookies = maxphys / PAGESIZE;
2358e1ebb9ecSlm66018 		} else {
2359e1ebb9ecSlm66018 			vdc->dring_max_cookies =
2360e1ebb9ecSlm66018 			    (vdc->max_xfer_sz * vdc->block_size) / PAGESIZE;
2361e1ebb9ecSlm66018 		}
2362e1ebb9ecSlm66018 		vdc->dring_entry_size = (sizeof (vd_dring_entry_t) +
2363e1ebb9ecSlm66018 		    (sizeof (ldc_mem_cookie_t) *
2364e1ebb9ecSlm66018 		    (vdc->dring_max_cookies - 1)));
2365e1ebb9ecSlm66018 		vdc->dring_len = VD_DRING_LEN;
2366e1ebb9ecSlm66018 
2367e1ebb9ecSlm66018 		status = ldc_mem_dring_create(vdc->dring_len,
2368e1ebb9ecSlm66018 		    vdc->dring_entry_size, &vdc->ldc_dring_hdl);
23691ae08745Sheppo 		if ((vdc->ldc_dring_hdl == NULL) || (status != 0)) {
23703af08d82Slm66018 			DMSG(vdc, 0, "[%d] Descriptor ring creation failed",
2371e1ebb9ecSlm66018 			    vdc->instance);
23721ae08745Sheppo 			return (status);
23731ae08745Sheppo 		}
23740a55fbb7Slm66018 		vdc->initialized |= VDC_DRING_INIT;
23750a55fbb7Slm66018 	}
23761ae08745Sheppo 
23770a55fbb7Slm66018 	if ((vdc->initialized & VDC_DRING_BOUND) == 0) {
23783af08d82Slm66018 		DMSG(vdc, 0, "[%d] ldc_mem_dring_bind\n", vdc->instance);
23790a55fbb7Slm66018 		vdc->dring_cookie =
23800a55fbb7Slm66018 		    kmem_zalloc(sizeof (ldc_mem_cookie_t), KM_SLEEP);
23811ae08745Sheppo 
23821ae08745Sheppo 		status = ldc_mem_dring_bind(vdc->ldc_handle, vdc->ldc_dring_hdl,
23834bac2208Snarayan 		    LDC_SHADOW_MAP|LDC_DIRECT_MAP, LDC_MEM_RW,
23840a55fbb7Slm66018 		    &vdc->dring_cookie[0],
23851ae08745Sheppo 		    &vdc->dring_cookie_count);
23861ae08745Sheppo 		if (status != 0) {
23873af08d82Slm66018 			DMSG(vdc, 0, "[%d] Failed to bind descriptor ring "
23883af08d82Slm66018 			    "(%lx) to channel (%lx) status=%d\n",
23893af08d82Slm66018 			    vdc->instance, vdc->ldc_dring_hdl,
23903af08d82Slm66018 			    vdc->ldc_handle, status);
23911ae08745Sheppo 			return (status);
23921ae08745Sheppo 		}
23931ae08745Sheppo 		ASSERT(vdc->dring_cookie_count == 1);
23941ae08745Sheppo 		vdc->initialized |= VDC_DRING_BOUND;
23950a55fbb7Slm66018 	}
23961ae08745Sheppo 
23971ae08745Sheppo 	status = ldc_mem_dring_info(vdc->ldc_dring_hdl, &vdc->dring_mem_info);
23981ae08745Sheppo 	if (status != 0) {
23993af08d82Slm66018 		DMSG(vdc, 0,
24003af08d82Slm66018 		    "[%d] Failed to get info for descriptor ring (%lx)\n",
2401e1ebb9ecSlm66018 		    vdc->instance, vdc->ldc_dring_hdl);
24021ae08745Sheppo 		return (status);
24031ae08745Sheppo 	}
24041ae08745Sheppo 
24050a55fbb7Slm66018 	if ((vdc->initialized & VDC_DRING_LOCAL) == 0) {
24063af08d82Slm66018 		DMSG(vdc, 0, "[%d] local dring\n", vdc->instance);
24070a55fbb7Slm66018 
24081ae08745Sheppo 		/* Allocate the local copy of this dring */
24090a55fbb7Slm66018 		vdc->local_dring =
2410e1ebb9ecSlm66018 		    kmem_zalloc(vdc->dring_len * sizeof (vdc_local_desc_t),
24111ae08745Sheppo 		    KM_SLEEP);
24121ae08745Sheppo 		vdc->initialized |= VDC_DRING_LOCAL;
24130a55fbb7Slm66018 	}
24141ae08745Sheppo 
24151ae08745Sheppo 	/*
24160a55fbb7Slm66018 	 * Mark all DRing entries as free and initialize the private
24170a55fbb7Slm66018 	 * descriptor's memory handles. If any entry is initialized,
24180a55fbb7Slm66018 	 * we need to free it later so we set the bit in 'initialized'
24190a55fbb7Slm66018 	 * at the start.
24201ae08745Sheppo 	 */
24211ae08745Sheppo 	vdc->initialized |= VDC_DRING_ENTRY;
2422e1ebb9ecSlm66018 	for (i = 0; i < vdc->dring_len; i++) {
24231ae08745Sheppo 		dep = VDC_GET_DRING_ENTRY_PTR(vdc, i);
24241ae08745Sheppo 		dep->hdr.dstate = VIO_DESC_FREE;
24251ae08745Sheppo 
24261ae08745Sheppo 		status = ldc_mem_alloc_handle(vdc->ldc_handle,
24271ae08745Sheppo 		    &vdc->local_dring[i].desc_mhdl);
24281ae08745Sheppo 		if (status != 0) {
24293af08d82Slm66018 			DMSG(vdc, 0, "![%d] Failed to alloc mem handle for"
24301ae08745Sheppo 			    " descriptor %d", vdc->instance, i);
24311ae08745Sheppo 			return (status);
24321ae08745Sheppo 		}
24333af08d82Slm66018 		vdc->local_dring[i].is_free = B_TRUE;
24341ae08745Sheppo 		vdc->local_dring[i].dep = dep;
24351ae08745Sheppo 	}
24361ae08745Sheppo 
24373af08d82Slm66018 	/* Initialize the starting index */
24383af08d82Slm66018 	vdc->dring_curr_idx = 0;
24391ae08745Sheppo 
24401ae08745Sheppo 	return (status);
24411ae08745Sheppo }
24421ae08745Sheppo 
24430a55fbb7Slm66018 /*
24440a55fbb7Slm66018  * Function:
24450a55fbb7Slm66018  *	vdc_destroy_descriptor_ring()
24460a55fbb7Slm66018  *
24470a55fbb7Slm66018  * Description:
24480a55fbb7Slm66018  *
24490a55fbb7Slm66018  * Arguments:
24500a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
24510a55fbb7Slm66018  *
24520a55fbb7Slm66018  * Return Code:
24530a55fbb7Slm66018  *	None
24540a55fbb7Slm66018  */
24551ae08745Sheppo static void
24561ae08745Sheppo vdc_destroy_descriptor_ring(vdc_t *vdc)
24571ae08745Sheppo {
24580a55fbb7Slm66018 	vdc_local_desc_t	*ldep = NULL;	/* Local Dring Entry Pointer */
24591ae08745Sheppo 	ldc_mem_handle_t	mhdl = NULL;
24603af08d82Slm66018 	ldc_mem_info_t		minfo;
24611ae08745Sheppo 	int			status = -1;
24621ae08745Sheppo 	int			i;	/* loop */
24631ae08745Sheppo 
24641ae08745Sheppo 	ASSERT(vdc != NULL);
24651ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
24661ae08745Sheppo 
24673af08d82Slm66018 	DMSG(vdc, 0, "[%d] Entered\n", vdc->instance);
24681ae08745Sheppo 
24691ae08745Sheppo 	if (vdc->initialized & VDC_DRING_ENTRY) {
24703af08d82Slm66018 		DMSG(vdc, 0,
24713af08d82Slm66018 		    "[%d] Removing Local DRing entries\n", vdc->instance);
2472e1ebb9ecSlm66018 		for (i = 0; i < vdc->dring_len; i++) {
24730a55fbb7Slm66018 			ldep = &vdc->local_dring[i];
24740a55fbb7Slm66018 			mhdl = ldep->desc_mhdl;
24751ae08745Sheppo 
24760a55fbb7Slm66018 			if (mhdl == NULL)
24770a55fbb7Slm66018 				continue;
24780a55fbb7Slm66018 
24793af08d82Slm66018 			if ((status = ldc_mem_info(mhdl, &minfo)) != 0) {
24803af08d82Slm66018 				DMSG(vdc, 0,
24813af08d82Slm66018 				    "ldc_mem_info returned an error: %d\n",
24823af08d82Slm66018 				    status);
24833af08d82Slm66018 
24843af08d82Slm66018 				/*
24853af08d82Slm66018 				 * This must mean that the mem handle
24863af08d82Slm66018 				 * is not valid. Clear it out so that
24873af08d82Slm66018 				 * no one tries to use it.
24883af08d82Slm66018 				 */
24893af08d82Slm66018 				ldep->desc_mhdl = NULL;
24903af08d82Slm66018 				continue;
24913af08d82Slm66018 			}
24923af08d82Slm66018 
24933af08d82Slm66018 			if (minfo.status == LDC_BOUND) {
24943af08d82Slm66018 				(void) ldc_mem_unbind_handle(mhdl);
24953af08d82Slm66018 			}
24963af08d82Slm66018 
24971ae08745Sheppo 			(void) ldc_mem_free_handle(mhdl);
24983af08d82Slm66018 
24993af08d82Slm66018 			ldep->desc_mhdl = NULL;
25001ae08745Sheppo 		}
25011ae08745Sheppo 		vdc->initialized &= ~VDC_DRING_ENTRY;
25021ae08745Sheppo 	}
25031ae08745Sheppo 
25041ae08745Sheppo 	if (vdc->initialized & VDC_DRING_LOCAL) {
25053af08d82Slm66018 		DMSG(vdc, 0, "[%d] Freeing Local DRing\n", vdc->instance);
25061ae08745Sheppo 		kmem_free(vdc->local_dring,
2507e1ebb9ecSlm66018 		    vdc->dring_len * sizeof (vdc_local_desc_t));
25081ae08745Sheppo 		vdc->initialized &= ~VDC_DRING_LOCAL;
25091ae08745Sheppo 	}
25101ae08745Sheppo 
25111ae08745Sheppo 	if (vdc->initialized & VDC_DRING_BOUND) {
25123af08d82Slm66018 		DMSG(vdc, 0, "[%d] Unbinding DRing\n", vdc->instance);
25131ae08745Sheppo 		status = ldc_mem_dring_unbind(vdc->ldc_dring_hdl);
25141ae08745Sheppo 		if (status == 0) {
25151ae08745Sheppo 			vdc->initialized &= ~VDC_DRING_BOUND;
25161ae08745Sheppo 		} else {
25173af08d82Slm66018 			DMSG(vdc, 0, "[%d] Error %d unbinding DRing %lx",
2518e1ebb9ecSlm66018 			    vdc->instance, status, vdc->ldc_dring_hdl);
25191ae08745Sheppo 		}
25203af08d82Slm66018 		kmem_free(vdc->dring_cookie, sizeof (ldc_mem_cookie_t));
25211ae08745Sheppo 	}
25221ae08745Sheppo 
25231ae08745Sheppo 	if (vdc->initialized & VDC_DRING_INIT) {
25243af08d82Slm66018 		DMSG(vdc, 0, "[%d] Destroying DRing\n", vdc->instance);
25251ae08745Sheppo 		status = ldc_mem_dring_destroy(vdc->ldc_dring_hdl);
25261ae08745Sheppo 		if (status == 0) {
25271ae08745Sheppo 			vdc->ldc_dring_hdl = NULL;
25281ae08745Sheppo 			bzero(&vdc->dring_mem_info, sizeof (ldc_mem_info_t));
25291ae08745Sheppo 			vdc->initialized &= ~VDC_DRING_INIT;
25301ae08745Sheppo 		} else {
25313af08d82Slm66018 			DMSG(vdc, 0, "[%d] Error %d destroying DRing (%lx)",
2532e1ebb9ecSlm66018 			    vdc->instance, status, vdc->ldc_dring_hdl);
25331ae08745Sheppo 		}
25341ae08745Sheppo 	}
25351ae08745Sheppo }
25361ae08745Sheppo 
25371ae08745Sheppo /*
25383af08d82Slm66018  * Function:
25393af08d82Slm66018  *	vdc_map_to_shared_ring()
25401ae08745Sheppo  *
25411ae08745Sheppo  * Description:
25423af08d82Slm66018  *	Copy contents of the local descriptor to the shared
25433af08d82Slm66018  *	memory descriptor.
25441ae08745Sheppo  *
25453af08d82Slm66018  * Arguments:
25463af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
25473af08d82Slm66018  *	idx	- descriptor ring index
25483af08d82Slm66018  *
25493af08d82Slm66018  * Return Code:
25503af08d82Slm66018  *	None
25511ae08745Sheppo  */
25521ae08745Sheppo static int
25533af08d82Slm66018 vdc_map_to_shared_dring(vdc_t *vdcp, int idx)
25541ae08745Sheppo {
25553af08d82Slm66018 	vdc_local_desc_t	*ldep;
25563af08d82Slm66018 	vd_dring_entry_t	*dep;
25573af08d82Slm66018 	int			rv;
25581ae08745Sheppo 
25593af08d82Slm66018 	ldep = &(vdcp->local_dring[idx]);
25601ae08745Sheppo 
25613af08d82Slm66018 	/* for now leave in the old pop_mem_hdl stuff */
25623af08d82Slm66018 	if (ldep->nbytes > 0) {
25633af08d82Slm66018 		rv = vdc_populate_mem_hdl(vdcp, ldep);
25643af08d82Slm66018 		if (rv) {
25653af08d82Slm66018 			DMSG(vdcp, 0, "[%d] Cannot populate mem handle\n",
25663af08d82Slm66018 			    vdcp->instance);
25673af08d82Slm66018 			return (rv);
25683af08d82Slm66018 		}
25693af08d82Slm66018 	}
25701ae08745Sheppo 
25713af08d82Slm66018 	/*
25723af08d82Slm66018 	 * fill in the data details into the DRing
25733af08d82Slm66018 	 */
2574d10e4ef2Snarayan 	dep = ldep->dep;
25751ae08745Sheppo 	ASSERT(dep != NULL);
25761ae08745Sheppo 
25773af08d82Slm66018 	dep->payload.req_id = VDC_GET_NEXT_REQ_ID(vdcp);
25783af08d82Slm66018 	dep->payload.operation = ldep->operation;
25793af08d82Slm66018 	dep->payload.addr = ldep->offset;
25803af08d82Slm66018 	dep->payload.nbytes = ldep->nbytes;
2581055d7c80Scarlsonj 	dep->payload.status = (uint32_t)-1;	/* vds will set valid value */
25823af08d82Slm66018 	dep->payload.slice = ldep->slice;
25833af08d82Slm66018 	dep->hdr.dstate = VIO_DESC_READY;
25843af08d82Slm66018 	dep->hdr.ack = 1;		/* request an ACK for every message */
25851ae08745Sheppo 
25863af08d82Slm66018 	return (0);
25871ae08745Sheppo }
25881ae08745Sheppo 
25891ae08745Sheppo /*
25901ae08745Sheppo  * Function:
25913af08d82Slm66018  *	vdc_send_request
25923af08d82Slm66018  *
25933af08d82Slm66018  * Description:
25943af08d82Slm66018  *	This routine writes the data to be transmitted to vds into the
25953af08d82Slm66018  *	descriptor, notifies vds that the ring has been updated and
25963af08d82Slm66018  *	then waits for the request to be processed.
25973af08d82Slm66018  *
25983af08d82Slm66018  * Arguments:
25993af08d82Slm66018  *	vdcp	  - the soft state pointer
26003af08d82Slm66018  *	operation - operation we want vds to perform (VD_OP_XXX)
26013af08d82Slm66018  *	addr	  - address of data buf to be read/written.
26023af08d82Slm66018  *	nbytes	  - number of bytes to read/write
26033af08d82Slm66018  *	slice	  - the disk slice this request is for
26043af08d82Slm66018  *	offset	  - relative disk offset
26053af08d82Slm66018  *	cb_type   - type of call - STRATEGY or SYNC
26063af08d82Slm66018  *	cb_arg	  - parameter to be sent to server (depends on VD_OP_XXX type)
26073af08d82Slm66018  *			. mode for ioctl(9e)
26083af08d82Slm66018  *			. LP64 diskaddr_t (block I/O)
26093af08d82Slm66018  *	dir	  - direction of operation (READ/WRITE/BOTH)
26103af08d82Slm66018  *
26113af08d82Slm66018  * Return Codes:
26123af08d82Slm66018  *	0
26133af08d82Slm66018  *	ENXIO
26143af08d82Slm66018  */
26153af08d82Slm66018 static int
26163af08d82Slm66018 vdc_send_request(vdc_t *vdcp, int operation, caddr_t addr,
26173af08d82Slm66018     size_t nbytes, int slice, diskaddr_t offset, int cb_type,
26183af08d82Slm66018     void *cb_arg, vio_desc_direction_t dir)
26193af08d82Slm66018 {
26203af08d82Slm66018 	ASSERT(vdcp != NULL);
262187a7269eSachartre 	ASSERT(slice == VD_SLICE_NONE || slice < V_NUMPAR);
26223af08d82Slm66018 
26233af08d82Slm66018 	mutex_enter(&vdcp->lock);
26243af08d82Slm66018 
26253af08d82Slm66018 	do {
26263c96341aSnarayan 		while (vdcp->state != VDC_STATE_RUNNING) {
26273af08d82Slm66018 
26283c96341aSnarayan 			/* return error if detaching */
26293c96341aSnarayan 			if (vdcp->state == VDC_STATE_DETACH) {
26303c96341aSnarayan 				mutex_exit(&vdcp->lock);
26313c96341aSnarayan 				return (ENXIO);
26323c96341aSnarayan 			}
2633655fd6a9Sachartre 
2634655fd6a9Sachartre 			/* fail request if connection timeout is reached */
2635655fd6a9Sachartre 			if (vdcp->ctimeout_reached) {
2636655fd6a9Sachartre 				mutex_exit(&vdcp->lock);
2637655fd6a9Sachartre 				return (EIO);
2638655fd6a9Sachartre 			}
2639655fd6a9Sachartre 
2640655fd6a9Sachartre 			cv_wait(&vdcp->running_cv, &vdcp->lock);
26413c96341aSnarayan 		}
26423c96341aSnarayan 
26433af08d82Slm66018 	} while (vdc_populate_descriptor(vdcp, operation, addr,
26443af08d82Slm66018 	    nbytes, slice, offset, cb_type, cb_arg, dir));
26453af08d82Slm66018 
26463af08d82Slm66018 	mutex_exit(&vdcp->lock);
26473af08d82Slm66018 	return (0);
26483af08d82Slm66018 }
26493af08d82Slm66018 
26503af08d82Slm66018 
26513af08d82Slm66018 /*
26523af08d82Slm66018  * Function:
26531ae08745Sheppo  *	vdc_populate_descriptor
26541ae08745Sheppo  *
26551ae08745Sheppo  * Description:
26561ae08745Sheppo  *	This routine writes the data to be transmitted to vds into the
26571ae08745Sheppo  *	descriptor, notifies vds that the ring has been updated and
26581ae08745Sheppo  *	then waits for the request to be processed.
26591ae08745Sheppo  *
26601ae08745Sheppo  * Arguments:
26613af08d82Slm66018  *	vdcp	  - the soft state pointer
26621ae08745Sheppo  *	operation - operation we want vds to perform (VD_OP_XXX)
26633af08d82Slm66018  *	addr	  - address of data buf to be read/written.
26643af08d82Slm66018  *	nbytes	  - number of bytes to read/write
26653af08d82Slm66018  *	slice	  - the disk slice this request is for
26663af08d82Slm66018  *	offset	  - relative disk offset
26673af08d82Slm66018  *	cb_type   - type of call - STRATEGY or SYNC
26683af08d82Slm66018  *	cb_arg	  - parameter to be sent to server (depends on VD_OP_XXX type)
26691ae08745Sheppo  *			. mode for ioctl(9e)
26701ae08745Sheppo  *			. LP64 diskaddr_t (block I/O)
26713af08d82Slm66018  *	dir	  - direction of operation (READ/WRITE/BOTH)
26721ae08745Sheppo  *
26731ae08745Sheppo  * Return Codes:
26741ae08745Sheppo  *	0
26751ae08745Sheppo  *	EAGAIN
26761ae08745Sheppo  *		EFAULT
26771ae08745Sheppo  *		ENXIO
26781ae08745Sheppo  *		EIO
26791ae08745Sheppo  */
26801ae08745Sheppo static int
26813af08d82Slm66018 vdc_populate_descriptor(vdc_t *vdcp, int operation, caddr_t addr,
26823af08d82Slm66018     size_t nbytes, int slice, diskaddr_t offset, int cb_type,
26833af08d82Slm66018     void *cb_arg, vio_desc_direction_t dir)
26841ae08745Sheppo {
26853af08d82Slm66018 	vdc_local_desc_t	*local_dep = NULL; /* Local Dring Pointer */
26863af08d82Slm66018 	int			idx;		/* Index of DRing entry used */
26873af08d82Slm66018 	int			next_idx;
26881ae08745Sheppo 	vio_dring_msg_t		dmsg;
26893af08d82Slm66018 	size_t			msglen;
26908e6a2a04Slm66018 	int			rv;
26911ae08745Sheppo 
26923af08d82Slm66018 	ASSERT(MUTEX_HELD(&vdcp->lock));
26933af08d82Slm66018 	vdcp->threads_pending++;
26943af08d82Slm66018 loop:
26953af08d82Slm66018 	DMSG(vdcp, 2, ": dring_curr_idx = %d\n", vdcp->dring_curr_idx);
26961ae08745Sheppo 
26973af08d82Slm66018 	/* Get next available D-Ring entry */
26983af08d82Slm66018 	idx = vdcp->dring_curr_idx;
26993af08d82Slm66018 	local_dep = &(vdcp->local_dring[idx]);
27001ae08745Sheppo 
27013af08d82Slm66018 	if (!local_dep->is_free) {
27023af08d82Slm66018 		DMSG(vdcp, 2, "[%d]: dring full - waiting for space\n",
27033af08d82Slm66018 		    vdcp->instance);
27043af08d82Slm66018 		cv_wait(&vdcp->dring_free_cv, &vdcp->lock);
27053af08d82Slm66018 		if (vdcp->state == VDC_STATE_RUNNING ||
27063af08d82Slm66018 		    vdcp->state == VDC_STATE_HANDLE_PENDING) {
27073af08d82Slm66018 			goto loop;
27083af08d82Slm66018 		}
27093af08d82Slm66018 		vdcp->threads_pending--;
27103af08d82Slm66018 		return (ECONNRESET);
27111ae08745Sheppo 	}
27121ae08745Sheppo 
27133af08d82Slm66018 	next_idx = idx + 1;
27143af08d82Slm66018 	if (next_idx >= vdcp->dring_len)
27153af08d82Slm66018 		next_idx = 0;
27163af08d82Slm66018 	vdcp->dring_curr_idx = next_idx;
27171ae08745Sheppo 
27183af08d82Slm66018 	ASSERT(local_dep->is_free);
27191ae08745Sheppo 
27203af08d82Slm66018 	local_dep->operation = operation;
2721d10e4ef2Snarayan 	local_dep->addr = addr;
27223af08d82Slm66018 	local_dep->nbytes = nbytes;
27233af08d82Slm66018 	local_dep->slice = slice;
27243af08d82Slm66018 	local_dep->offset = offset;
27253af08d82Slm66018 	local_dep->cb_type = cb_type;
27263af08d82Slm66018 	local_dep->cb_arg = cb_arg;
27273af08d82Slm66018 	local_dep->dir = dir;
27283af08d82Slm66018 
27293af08d82Slm66018 	local_dep->is_free = B_FALSE;
27303af08d82Slm66018 
27313af08d82Slm66018 	rv = vdc_map_to_shared_dring(vdcp, idx);
27323af08d82Slm66018 	if (rv) {
27333af08d82Slm66018 		DMSG(vdcp, 0, "[%d]: cannot bind memory - waiting ..\n",
27343af08d82Slm66018 		    vdcp->instance);
27353af08d82Slm66018 		/* free the descriptor */
27363af08d82Slm66018 		local_dep->is_free = B_TRUE;
27373af08d82Slm66018 		vdcp->dring_curr_idx = idx;
27383af08d82Slm66018 		cv_wait(&vdcp->membind_cv, &vdcp->lock);
27393af08d82Slm66018 		if (vdcp->state == VDC_STATE_RUNNING ||
27403af08d82Slm66018 		    vdcp->state == VDC_STATE_HANDLE_PENDING) {
27413af08d82Slm66018 			goto loop;
27421ae08745Sheppo 		}
27433af08d82Slm66018 		vdcp->threads_pending--;
27443af08d82Slm66018 		return (ECONNRESET);
27451ae08745Sheppo 	}
27461ae08745Sheppo 
27471ae08745Sheppo 	/*
27481ae08745Sheppo 	 * Send a msg with the DRing details to vds
27491ae08745Sheppo 	 */
27501ae08745Sheppo 	VIO_INIT_DRING_DATA_TAG(dmsg);
27513af08d82Slm66018 	VDC_INIT_DRING_DATA_MSG_IDS(dmsg, vdcp);
27523af08d82Slm66018 	dmsg.dring_ident = vdcp->dring_ident;
27531ae08745Sheppo 	dmsg.start_idx = idx;
27541ae08745Sheppo 	dmsg.end_idx = idx;
27553af08d82Slm66018 	vdcp->seq_num++;
27561ae08745Sheppo 
27573af08d82Slm66018 	DTRACE_IO2(send, vio_dring_msg_t *, &dmsg, vdc_t *, vdcp);
2758d10e4ef2Snarayan 
27593af08d82Slm66018 	DMSG(vdcp, 2, "ident=0x%lx, st=%u, end=%u, seq=%ld\n",
27603af08d82Slm66018 	    vdcp->dring_ident, dmsg.start_idx, dmsg.end_idx, dmsg.seq_num);
27611ae08745Sheppo 
27623af08d82Slm66018 	/*
27633af08d82Slm66018 	 * note we're still holding the lock here to
27643af08d82Slm66018 	 * make sure the message goes out in order !!!...
27653af08d82Slm66018 	 */
27663af08d82Slm66018 	msglen = sizeof (dmsg);
27673af08d82Slm66018 	rv = vdc_send(vdcp, (caddr_t)&dmsg, &msglen);
27683af08d82Slm66018 	switch (rv) {
27693af08d82Slm66018 	case ECONNRESET:
27703af08d82Slm66018 		/*
27713af08d82Slm66018 		 * vdc_send initiates the reset on failure.
27723af08d82Slm66018 		 * Since the transaction has already been put
27733af08d82Slm66018 		 * on the local dring, it will automatically get
27743af08d82Slm66018 		 * retried when the channel is reset. Given that,
27753af08d82Slm66018 		 * it is ok to just return success even though the
27763af08d82Slm66018 		 * send failed.
27773af08d82Slm66018 		 */
27783af08d82Slm66018 		rv = 0;
27793af08d82Slm66018 		break;
2780d10e4ef2Snarayan 
27813af08d82Slm66018 	case 0: /* EOK */
27823af08d82Slm66018 		DMSG(vdcp, 1, "sent via LDC: rv=%d\n", rv);
27833af08d82Slm66018 		break;
2784d10e4ef2Snarayan 
27853af08d82Slm66018 	default:
27863af08d82Slm66018 		goto cleanup_and_exit;
27873af08d82Slm66018 	}
2788e1ebb9ecSlm66018 
27893af08d82Slm66018 	vdcp->threads_pending--;
27903af08d82Slm66018 	return (rv);
27913af08d82Slm66018 
27923af08d82Slm66018 cleanup_and_exit:
27933af08d82Slm66018 	DMSG(vdcp, 0, "unexpected error, rv=%d\n", rv);
27943af08d82Slm66018 	return (ENXIO);
27951ae08745Sheppo }
27961ae08745Sheppo 
27971ae08745Sheppo /*
27983af08d82Slm66018  * Function:
27993af08d82Slm66018  *	vdc_do_sync_op
28003af08d82Slm66018  *
28013af08d82Slm66018  * Description:
28023af08d82Slm66018  * 	Wrapper around vdc_populate_descriptor that blocks until the
28033af08d82Slm66018  * 	response to the message is available.
28043af08d82Slm66018  *
28053af08d82Slm66018  * Arguments:
28063af08d82Slm66018  *	vdcp	  - the soft state pointer
28073af08d82Slm66018  *	operation - operation we want vds to perform (VD_OP_XXX)
28083af08d82Slm66018  *	addr	  - address of data buf to be read/written.
28093af08d82Slm66018  *	nbytes	  - number of bytes to read/write
28103af08d82Slm66018  *	slice	  - the disk slice this request is for
28113af08d82Slm66018  *	offset	  - relative disk offset
28123af08d82Slm66018  *	cb_type   - type of call - STRATEGY or SYNC
28133af08d82Slm66018  *	cb_arg	  - parameter to be sent to server (depends on VD_OP_XXX type)
28143af08d82Slm66018  *			. mode for ioctl(9e)
28153af08d82Slm66018  *			. LP64 diskaddr_t (block I/O)
28163af08d82Slm66018  *	dir	  - direction of operation (READ/WRITE/BOTH)
28173af08d82Slm66018  *
28183af08d82Slm66018  * Return Codes:
28193af08d82Slm66018  *	0
28203af08d82Slm66018  *	EAGAIN
28213af08d82Slm66018  *		EFAULT
28223af08d82Slm66018  *		ENXIO
28233af08d82Slm66018  *		EIO
28240a55fbb7Slm66018  */
28253af08d82Slm66018 static int
28263af08d82Slm66018 vdc_do_sync_op(vdc_t *vdcp, int operation, caddr_t addr, size_t nbytes,
28273af08d82Slm66018     int slice, diskaddr_t offset, int cb_type, void *cb_arg,
28283af08d82Slm66018     vio_desc_direction_t dir)
28293af08d82Slm66018 {
28303af08d82Slm66018 	int status;
28313af08d82Slm66018 
28323af08d82Slm66018 	ASSERT(cb_type == CB_SYNC);
28331ae08745Sheppo 
28341ae08745Sheppo 	/*
28353af08d82Slm66018 	 * Grab the lock, if blocked wait until the server
28363af08d82Slm66018 	 * response causes us to wake up again.
28373af08d82Slm66018 	 */
28383af08d82Slm66018 	mutex_enter(&vdcp->lock);
28393af08d82Slm66018 	vdcp->sync_op_cnt++;
28403af08d82Slm66018 	while (vdcp->sync_op_blocked && vdcp->state != VDC_STATE_DETACH)
28413af08d82Slm66018 		cv_wait(&vdcp->sync_blocked_cv, &vdcp->lock);
28423af08d82Slm66018 
28433af08d82Slm66018 	if (vdcp->state == VDC_STATE_DETACH) {
28443af08d82Slm66018 		cv_broadcast(&vdcp->sync_blocked_cv);
28453af08d82Slm66018 		vdcp->sync_op_cnt--;
28463af08d82Slm66018 		mutex_exit(&vdcp->lock);
28473af08d82Slm66018 		return (ENXIO);
28483af08d82Slm66018 	}
28493af08d82Slm66018 
28503af08d82Slm66018 	/* now block anyone other thread entering after us */
28513af08d82Slm66018 	vdcp->sync_op_blocked = B_TRUE;
28523af08d82Slm66018 	vdcp->sync_op_pending = B_TRUE;
28533af08d82Slm66018 	mutex_exit(&vdcp->lock);
28543af08d82Slm66018 
2855655fd6a9Sachartre 	status = vdc_send_request(vdcp, operation, addr,
28563af08d82Slm66018 	    nbytes, slice, offset, cb_type, cb_arg, dir);
28573af08d82Slm66018 
2858655fd6a9Sachartre 	mutex_enter(&vdcp->lock);
2859655fd6a9Sachartre 
2860655fd6a9Sachartre 	if (status != 0) {
2861655fd6a9Sachartre 		vdcp->sync_op_pending = B_FALSE;
2862655fd6a9Sachartre 	} else {
28633af08d82Slm66018 		/*
28643af08d82Slm66018 		 * block until our transaction completes.
28653af08d82Slm66018 		 * Also anyone else waiting also gets to go next.
28663af08d82Slm66018 		 */
28673af08d82Slm66018 		while (vdcp->sync_op_pending && vdcp->state != VDC_STATE_DETACH)
28683af08d82Slm66018 			cv_wait(&vdcp->sync_pending_cv, &vdcp->lock);
28693af08d82Slm66018 
2870655fd6a9Sachartre 		DMSG(vdcp, 2, ": operation returned %d\n",
2871655fd6a9Sachartre 		    vdcp->sync_op_status);
28723c96341aSnarayan 		if (vdcp->state == VDC_STATE_DETACH) {
28733c96341aSnarayan 			vdcp->sync_op_pending = B_FALSE;
28743af08d82Slm66018 			status = ENXIO;
28753c96341aSnarayan 		} else {
28763af08d82Slm66018 			status = vdcp->sync_op_status;
28773c96341aSnarayan 		}
2878655fd6a9Sachartre 	}
28793c96341aSnarayan 
28803af08d82Slm66018 	vdcp->sync_op_status = 0;
28813af08d82Slm66018 	vdcp->sync_op_blocked = B_FALSE;
28823af08d82Slm66018 	vdcp->sync_op_cnt--;
28833af08d82Slm66018 
28843af08d82Slm66018 	/* signal the next waiting thread */
28853af08d82Slm66018 	cv_signal(&vdcp->sync_blocked_cv);
28863af08d82Slm66018 	mutex_exit(&vdcp->lock);
28873af08d82Slm66018 
28883af08d82Slm66018 	return (status);
28893af08d82Slm66018 }
28903af08d82Slm66018 
28913af08d82Slm66018 
28923af08d82Slm66018 /*
28933af08d82Slm66018  * Function:
28943af08d82Slm66018  *	vdc_drain_response()
28953af08d82Slm66018  *
28963af08d82Slm66018  * Description:
28971ae08745Sheppo  * 	When a guest is panicking, the completion of requests needs to be
28981ae08745Sheppo  * 	handled differently because interrupts are disabled and vdc
28991ae08745Sheppo  * 	will not get messages. We have to poll for the messages instead.
29003af08d82Slm66018  *
29013af08d82Slm66018  * Arguments:
29023af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
29033af08d82Slm66018  *
29043af08d82Slm66018  * Return Code:
29053af08d82Slm66018  *	0	- Success
29061ae08745Sheppo  */
29073af08d82Slm66018 static int
29083af08d82Slm66018 vdc_drain_response(vdc_t *vdc)
29093af08d82Slm66018 {
29103af08d82Slm66018 	int 			rv, idx, retries;
29113af08d82Slm66018 	size_t			msglen;
29123af08d82Slm66018 	vdc_local_desc_t 	*ldep = NULL;	/* Local Dring Entry Pointer */
29133af08d82Slm66018 	vio_dring_msg_t		dmsg;
29143af08d82Slm66018 
29153af08d82Slm66018 	mutex_enter(&vdc->lock);
29163af08d82Slm66018 
29171ae08745Sheppo 	retries = 0;
29181ae08745Sheppo 	for (;;) {
29191ae08745Sheppo 		msglen = sizeof (dmsg);
29203af08d82Slm66018 		rv = ldc_read(vdc->ldc_handle, (caddr_t)&dmsg, &msglen);
29218e6a2a04Slm66018 		if (rv) {
29228e6a2a04Slm66018 			rv = EINVAL;
29231ae08745Sheppo 			break;
29241ae08745Sheppo 		}
29251ae08745Sheppo 
29261ae08745Sheppo 		/*
29271ae08745Sheppo 		 * if there are no packets wait and check again
29281ae08745Sheppo 		 */
29298e6a2a04Slm66018 		if ((rv == 0) && (msglen == 0)) {
29301ae08745Sheppo 			if (retries++ > vdc_dump_retries) {
29318e6a2a04Slm66018 				rv = EAGAIN;
29321ae08745Sheppo 				break;
29331ae08745Sheppo 			}
29341ae08745Sheppo 
2935d10e4ef2Snarayan 			drv_usecwait(vdc_usec_timeout_dump);
29361ae08745Sheppo 			continue;
29371ae08745Sheppo 		}
29381ae08745Sheppo 
29391ae08745Sheppo 		/*
29401ae08745Sheppo 		 * Ignore all messages that are not ACKs/NACKs to
29411ae08745Sheppo 		 * DRing requests.
29421ae08745Sheppo 		 */
29431ae08745Sheppo 		if ((dmsg.tag.vio_msgtype != VIO_TYPE_DATA) ||
29441ae08745Sheppo 		    (dmsg.tag.vio_subtype_env != VIO_DRING_DATA)) {
29453af08d82Slm66018 			DMSG(vdc, 0, "discard pkt: type=%d sub=%d env=%d\n",
29461ae08745Sheppo 			    dmsg.tag.vio_msgtype,
29471ae08745Sheppo 			    dmsg.tag.vio_subtype,
29481ae08745Sheppo 			    dmsg.tag.vio_subtype_env);
29491ae08745Sheppo 			continue;
29501ae08745Sheppo 		}
29511ae08745Sheppo 
29521ae08745Sheppo 		/*
29533af08d82Slm66018 		 * set the appropriate return value for the current request.
29541ae08745Sheppo 		 */
29551ae08745Sheppo 		switch (dmsg.tag.vio_subtype) {
29561ae08745Sheppo 		case VIO_SUBTYPE_ACK:
29578e6a2a04Slm66018 			rv = 0;
29581ae08745Sheppo 			break;
29591ae08745Sheppo 		case VIO_SUBTYPE_NACK:
29608e6a2a04Slm66018 			rv = EAGAIN;
29611ae08745Sheppo 			break;
29621ae08745Sheppo 		default:
29631ae08745Sheppo 			continue;
29641ae08745Sheppo 		}
29651ae08745Sheppo 
29663af08d82Slm66018 		idx = dmsg.start_idx;
29673af08d82Slm66018 		if (idx >= vdc->dring_len) {
29683af08d82Slm66018 			DMSG(vdc, 0, "[%d] Bogus ack data : start %d\n",
2969e1ebb9ecSlm66018 			    vdc->instance, idx);
29703af08d82Slm66018 			continue;
29711ae08745Sheppo 		}
29723af08d82Slm66018 		ldep = &vdc->local_dring[idx];
29733af08d82Slm66018 		if (ldep->dep->hdr.dstate != VIO_DESC_DONE) {
29743af08d82Slm66018 			DMSG(vdc, 0, "[%d] Entry @ %d - state !DONE %d\n",
29753af08d82Slm66018 			    vdc->instance, idx, ldep->dep->hdr.dstate);
29761ae08745Sheppo 			continue;
29771ae08745Sheppo 		}
29781ae08745Sheppo 
29793af08d82Slm66018 		DMSG(vdc, 1, "[%d] Depopulating idx=%d state=%d\n",
29803af08d82Slm66018 		    vdc->instance, idx, ldep->dep->hdr.dstate);
29813af08d82Slm66018 		rv = vdc_depopulate_descriptor(vdc, idx);
29823af08d82Slm66018 		if (rv) {
29833af08d82Slm66018 			DMSG(vdc, 0,
29843af08d82Slm66018 			    "[%d] Entry @ %d - depopulate failed ..\n",
29853af08d82Slm66018 			    vdc->instance, idx);
29861ae08745Sheppo 		}
29871ae08745Sheppo 
29883af08d82Slm66018 		/* if this is the last descriptor - break out of loop */
29893af08d82Slm66018 		if ((idx + 1) % vdc->dring_len == vdc->dring_curr_idx)
29903af08d82Slm66018 			break;
29913af08d82Slm66018 	}
29923af08d82Slm66018 
29933af08d82Slm66018 	mutex_exit(&vdc->lock);
29943af08d82Slm66018 	DMSG(vdc, 0, "End idx=%d\n", idx);
29953af08d82Slm66018 
29963af08d82Slm66018 	return (rv);
29971ae08745Sheppo }
29981ae08745Sheppo 
29991ae08745Sheppo 
30000a55fbb7Slm66018 /*
30010a55fbb7Slm66018  * Function:
30020a55fbb7Slm66018  *	vdc_depopulate_descriptor()
30030a55fbb7Slm66018  *
30040a55fbb7Slm66018  * Description:
30050a55fbb7Slm66018  *
30060a55fbb7Slm66018  * Arguments:
30070a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
30080a55fbb7Slm66018  *	idx	- Index of the Descriptor Ring entry being modified
30090a55fbb7Slm66018  *
30100a55fbb7Slm66018  * Return Code:
30110a55fbb7Slm66018  *	0	- Success
30120a55fbb7Slm66018  */
30131ae08745Sheppo static int
30141ae08745Sheppo vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx)
30151ae08745Sheppo {
30161ae08745Sheppo 	vd_dring_entry_t *dep = NULL;		/* Dring Entry Pointer */
30171ae08745Sheppo 	vdc_local_desc_t *ldep = NULL;		/* Local Dring Entry Pointer */
30181ae08745Sheppo 	int		status = ENXIO;
30198e6a2a04Slm66018 	int		rv = 0;
30201ae08745Sheppo 
30211ae08745Sheppo 	ASSERT(vdc != NULL);
3022e1ebb9ecSlm66018 	ASSERT(idx < vdc->dring_len);
30231ae08745Sheppo 	ldep = &vdc->local_dring[idx];
30241ae08745Sheppo 	ASSERT(ldep != NULL);
30253af08d82Slm66018 	ASSERT(MUTEX_HELD(&vdc->lock));
30263af08d82Slm66018 
30273af08d82Slm66018 	DMSG(vdc, 2, ": idx = %d\n", idx);
30281ae08745Sheppo 	dep = ldep->dep;
30291ae08745Sheppo 	ASSERT(dep != NULL);
3030e1ebb9ecSlm66018 	ASSERT((dep->hdr.dstate == VIO_DESC_DONE) ||
3031e1ebb9ecSlm66018 	    (dep->payload.status == ECANCELED));
30321ae08745Sheppo 
3033e1ebb9ecSlm66018 	VDC_MARK_DRING_ENTRY_FREE(vdc, idx);
30343af08d82Slm66018 
30353af08d82Slm66018 	ldep->is_free = B_TRUE;
30361ae08745Sheppo 	status = dep->payload.status;
3037205eeb1aSlm66018 	DMSG(vdc, 2, ": is_free = %d : status = %d\n", ldep->is_free, status);
30381ae08745Sheppo 
3039eff7243fSlm66018 	/*
3040eff7243fSlm66018 	 * If no buffers were used to transfer information to the server when
3041eff7243fSlm66018 	 * populating the descriptor then no memory handles need to be unbound
3042eff7243fSlm66018 	 * and we can return now.
3043eff7243fSlm66018 	 */
3044eff7243fSlm66018 	if (ldep->nbytes == 0) {
3045eff7243fSlm66018 		cv_signal(&vdc->dring_free_cv);
30468e6a2a04Slm66018 		return (status);
3047eff7243fSlm66018 	}
30488e6a2a04Slm66018 
30491ae08745Sheppo 	/*
30501ae08745Sheppo 	 * If the upper layer passed in a misaligned address we copied the
30511ae08745Sheppo 	 * data into an aligned buffer before sending it to LDC - we now
30521ae08745Sheppo 	 * copy it back to the original buffer.
30531ae08745Sheppo 	 */
30541ae08745Sheppo 	if (ldep->align_addr) {
30551ae08745Sheppo 		ASSERT(ldep->addr != NULL);
30561ae08745Sheppo 
30573c96341aSnarayan 		if (dep->payload.nbytes > 0)
30583c96341aSnarayan 			bcopy(ldep->align_addr, ldep->addr,
30593c96341aSnarayan 			    dep->payload.nbytes);
30601ae08745Sheppo 		kmem_free(ldep->align_addr,
30613c96341aSnarayan 		    sizeof (caddr_t) * P2ROUNDUP(ldep->nbytes, 8));
30621ae08745Sheppo 		ldep->align_addr = NULL;
30631ae08745Sheppo 	}
30641ae08745Sheppo 
30658e6a2a04Slm66018 	rv = ldc_mem_unbind_handle(ldep->desc_mhdl);
30668e6a2a04Slm66018 	if (rv != 0) {
30673af08d82Slm66018 		DMSG(vdc, 0, "?[%d] unbind mhdl 0x%lx @ idx %d failed (%d)",
30688e6a2a04Slm66018 		    vdc->instance, ldep->desc_mhdl, idx, rv);
30698e6a2a04Slm66018 		/*
30708e6a2a04Slm66018 		 * The error returned by the vDisk server is more informative
30718e6a2a04Slm66018 		 * and thus has a higher priority but if it isn't set we ensure
30728e6a2a04Slm66018 		 * that this function returns an error.
30738e6a2a04Slm66018 		 */
30748e6a2a04Slm66018 		if (status == 0)
30758e6a2a04Slm66018 			status = EINVAL;
30761ae08745Sheppo 	}
30771ae08745Sheppo 
30783af08d82Slm66018 	cv_signal(&vdc->membind_cv);
30793af08d82Slm66018 	cv_signal(&vdc->dring_free_cv);
30803af08d82Slm66018 
30811ae08745Sheppo 	return (status);
30821ae08745Sheppo }
30831ae08745Sheppo 
30840a55fbb7Slm66018 /*
30850a55fbb7Slm66018  * Function:
30860a55fbb7Slm66018  *	vdc_populate_mem_hdl()
30870a55fbb7Slm66018  *
30880a55fbb7Slm66018  * Description:
30890a55fbb7Slm66018  *
30900a55fbb7Slm66018  * Arguments:
30910a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
30920a55fbb7Slm66018  *	idx	- Index of the Descriptor Ring entry being modified
30930a55fbb7Slm66018  *	addr	- virtual address being mapped in
30940a55fbb7Slm66018  *	nybtes	- number of bytes in 'addr'
30950a55fbb7Slm66018  *	operation - the vDisk operation being performed (VD_OP_xxx)
30960a55fbb7Slm66018  *
30970a55fbb7Slm66018  * Return Code:
30980a55fbb7Slm66018  *	0	- Success
30990a55fbb7Slm66018  */
31001ae08745Sheppo static int
31013af08d82Slm66018 vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep)
31021ae08745Sheppo {
31031ae08745Sheppo 	vd_dring_entry_t	*dep = NULL;
31041ae08745Sheppo 	ldc_mem_handle_t	mhdl;
31051ae08745Sheppo 	caddr_t			vaddr;
31063af08d82Slm66018 	size_t			nbytes;
31074bac2208Snarayan 	uint8_t			perm = LDC_MEM_RW;
31084bac2208Snarayan 	uint8_t			maptype;
31091ae08745Sheppo 	int			rv = 0;
31101ae08745Sheppo 	int			i;
31111ae08745Sheppo 
31123af08d82Slm66018 	ASSERT(vdcp != NULL);
31131ae08745Sheppo 
31143af08d82Slm66018 	dep = ldep->dep;
31151ae08745Sheppo 	mhdl = ldep->desc_mhdl;
31161ae08745Sheppo 
31173af08d82Slm66018 	switch (ldep->dir) {
31183af08d82Slm66018 	case VIO_read_dir:
31191ae08745Sheppo 		perm = LDC_MEM_W;
31201ae08745Sheppo 		break;
31211ae08745Sheppo 
31223af08d82Slm66018 	case VIO_write_dir:
31231ae08745Sheppo 		perm = LDC_MEM_R;
31241ae08745Sheppo 		break;
31251ae08745Sheppo 
31263af08d82Slm66018 	case VIO_both_dir:
31271ae08745Sheppo 		perm = LDC_MEM_RW;
31281ae08745Sheppo 		break;
31291ae08745Sheppo 
31301ae08745Sheppo 	default:
31311ae08745Sheppo 		ASSERT(0);	/* catch bad programming in vdc */
31321ae08745Sheppo 	}
31331ae08745Sheppo 
31341ae08745Sheppo 	/*
31351ae08745Sheppo 	 * LDC expects any addresses passed in to be 8-byte aligned. We need
31361ae08745Sheppo 	 * to copy the contents of any misaligned buffers to a newly allocated
31371ae08745Sheppo 	 * buffer and bind it instead (and copy the the contents back to the
31381ae08745Sheppo 	 * original buffer passed in when depopulating the descriptor)
31391ae08745Sheppo 	 */
31403af08d82Slm66018 	vaddr = ldep->addr;
31413af08d82Slm66018 	nbytes = ldep->nbytes;
31423af08d82Slm66018 	if (((uint64_t)vaddr & 0x7) != 0) {
3143d10e4ef2Snarayan 		ASSERT(ldep->align_addr == NULL);
31441ae08745Sheppo 		ldep->align_addr =
31453af08d82Slm66018 		    kmem_alloc(sizeof (caddr_t) *
31463af08d82Slm66018 		    P2ROUNDUP(nbytes, 8), KM_SLEEP);
31473af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Misaligned address %p reallocating "
31483af08d82Slm66018 		    "(buf=%p nb=%ld op=%d)\n",
31493af08d82Slm66018 		    vdcp->instance, (void *)vaddr, (void *)ldep->align_addr,
31503af08d82Slm66018 		    nbytes, ldep->operation);
31513af08d82Slm66018 		if (perm != LDC_MEM_W)
31523af08d82Slm66018 			bcopy(vaddr, ldep->align_addr, nbytes);
31531ae08745Sheppo 		vaddr = ldep->align_addr;
31541ae08745Sheppo 	}
31551ae08745Sheppo 
31564bac2208Snarayan 	maptype = LDC_IO_MAP|LDC_SHADOW_MAP|LDC_DIRECT_MAP;
31571ae08745Sheppo 	rv = ldc_mem_bind_handle(mhdl, vaddr, P2ROUNDUP(nbytes, 8),
315887a7269eSachartre 	    maptype, perm, &dep->payload.cookie[0], &dep->payload.ncookies);
31593af08d82Slm66018 	DMSG(vdcp, 2, "[%d] bound mem handle; ncookies=%d\n",
31603af08d82Slm66018 	    vdcp->instance, dep->payload.ncookies);
31611ae08745Sheppo 	if (rv != 0) {
31623af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Failed to bind LDC memory handle "
31633af08d82Slm66018 		    "(mhdl=%p, buf=%p, err=%d)\n",
31643af08d82Slm66018 		    vdcp->instance, (void *)mhdl, (void *)vaddr, rv);
31651ae08745Sheppo 		if (ldep->align_addr) {
31661ae08745Sheppo 			kmem_free(ldep->align_addr,
3167d10e4ef2Snarayan 			    sizeof (caddr_t) * P2ROUNDUP(nbytes, 8));
31681ae08745Sheppo 			ldep->align_addr = NULL;
31691ae08745Sheppo 		}
31701ae08745Sheppo 		return (EAGAIN);
31711ae08745Sheppo 	}
31721ae08745Sheppo 
31731ae08745Sheppo 	/*
31741ae08745Sheppo 	 * Get the other cookies (if any).
31751ae08745Sheppo 	 */
31761ae08745Sheppo 	for (i = 1; i < dep->payload.ncookies; i++) {
31771ae08745Sheppo 		rv = ldc_mem_nextcookie(mhdl, &dep->payload.cookie[i]);
31781ae08745Sheppo 		if (rv != 0) {
31791ae08745Sheppo 			(void) ldc_mem_unbind_handle(mhdl);
31803af08d82Slm66018 			DMSG(vdcp, 0, "?[%d] Failed to get next cookie "
3181e1ebb9ecSlm66018 			    "(mhdl=%lx cnum=%d), err=%d",
31823af08d82Slm66018 			    vdcp->instance, mhdl, i, rv);
31831ae08745Sheppo 			if (ldep->align_addr) {
31841ae08745Sheppo 				kmem_free(ldep->align_addr,
31853c96341aSnarayan 				    sizeof (caddr_t) * ldep->nbytes);
31861ae08745Sheppo 				ldep->align_addr = NULL;
31871ae08745Sheppo 			}
31881ae08745Sheppo 			return (EAGAIN);
31891ae08745Sheppo 		}
31901ae08745Sheppo 	}
31911ae08745Sheppo 
31921ae08745Sheppo 	return (rv);
31931ae08745Sheppo }
31941ae08745Sheppo 
31951ae08745Sheppo /*
31961ae08745Sheppo  * Interrupt handlers for messages from LDC
31971ae08745Sheppo  */
31981ae08745Sheppo 
31990a55fbb7Slm66018 /*
32000a55fbb7Slm66018  * Function:
32010a55fbb7Slm66018  *	vdc_handle_cb()
32020a55fbb7Slm66018  *
32030a55fbb7Slm66018  * Description:
32040a55fbb7Slm66018  *
32050a55fbb7Slm66018  * Arguments:
32060a55fbb7Slm66018  *	event	- Type of event (LDC_EVT_xxx) that triggered the callback
32070a55fbb7Slm66018  *	arg	- soft state pointer for this instance of the device driver.
32080a55fbb7Slm66018  *
32090a55fbb7Slm66018  * Return Code:
32100a55fbb7Slm66018  *	0	- Success
32110a55fbb7Slm66018  */
32121ae08745Sheppo static uint_t
32131ae08745Sheppo vdc_handle_cb(uint64_t event, caddr_t arg)
32141ae08745Sheppo {
32151ae08745Sheppo 	ldc_status_t	ldc_state;
32161ae08745Sheppo 	int		rv = 0;
32171ae08745Sheppo 
32181ae08745Sheppo 	vdc_t	*vdc = (vdc_t *)(void *)arg;
32191ae08745Sheppo 
32201ae08745Sheppo 	ASSERT(vdc != NULL);
32211ae08745Sheppo 
32223af08d82Slm66018 	DMSG(vdc, 1, "evt=%lx seqID=%ld\n", event, vdc->seq_num);
32231ae08745Sheppo 
32241ae08745Sheppo 	/*
32251ae08745Sheppo 	 * Depending on the type of event that triggered this callback,
32263af08d82Slm66018 	 * we modify the handshake state or read the data.
32271ae08745Sheppo 	 *
32281ae08745Sheppo 	 * NOTE: not done as a switch() as event could be triggered by
32291ae08745Sheppo 	 * a state change and a read request. Also the ordering	of the
32301ae08745Sheppo 	 * check for the event types is deliberate.
32311ae08745Sheppo 	 */
32321ae08745Sheppo 	if (event & LDC_EVT_UP) {
32333af08d82Slm66018 		DMSG(vdc, 0, "[%d] Received LDC_EVT_UP\n", vdc->instance);
32343af08d82Slm66018 
32353af08d82Slm66018 		mutex_enter(&vdc->lock);
32361ae08745Sheppo 
32371ae08745Sheppo 		/* get LDC state */
32381ae08745Sheppo 		rv = ldc_status(vdc->ldc_handle, &ldc_state);
32391ae08745Sheppo 		if (rv != 0) {
32403af08d82Slm66018 			DMSG(vdc, 0, "[%d] Couldn't get LDC status %d",
32411ae08745Sheppo 			    vdc->instance, rv);
32421ae08745Sheppo 			return (LDC_SUCCESS);
32431ae08745Sheppo 		}
32443af08d82Slm66018 		if (vdc->ldc_state != LDC_UP && ldc_state == LDC_UP) {
32451ae08745Sheppo 			/*
32463af08d82Slm66018 			 * Reset the transaction sequence numbers when
32473af08d82Slm66018 			 * LDC comes up. We then kick off the handshake
32483af08d82Slm66018 			 * negotiation with the vDisk server.
32491ae08745Sheppo 			 */
32500a55fbb7Slm66018 			vdc->seq_num = 1;
32511ae08745Sheppo 			vdc->seq_num_reply = 0;
32521ae08745Sheppo 			vdc->ldc_state = ldc_state;
32533af08d82Slm66018 			cv_signal(&vdc->initwait_cv);
32543af08d82Slm66018 		}
32553af08d82Slm66018 
32561ae08745Sheppo 		mutex_exit(&vdc->lock);
32571ae08745Sheppo 	}
32581ae08745Sheppo 
32591ae08745Sheppo 	if (event & LDC_EVT_READ) {
32603af08d82Slm66018 		DMSG(vdc, 0, "[%d] Received LDC_EVT_READ\n", vdc->instance);
32613af08d82Slm66018 		mutex_enter(&vdc->read_lock);
32623af08d82Slm66018 		cv_signal(&vdc->read_cv);
32633af08d82Slm66018 		vdc->read_state = VDC_READ_PENDING;
32643af08d82Slm66018 		mutex_exit(&vdc->read_lock);
32651ae08745Sheppo 
32661ae08745Sheppo 		/* that's all we have to do - no need to handle DOWN/RESET */
32671ae08745Sheppo 		return (LDC_SUCCESS);
32681ae08745Sheppo 	}
32691ae08745Sheppo 
32703af08d82Slm66018 	if (event & (LDC_EVT_RESET|LDC_EVT_DOWN)) {
32710a55fbb7Slm66018 
32723af08d82Slm66018 		DMSG(vdc, 0, "[%d] Received LDC RESET event\n", vdc->instance);
32733af08d82Slm66018 
32740a55fbb7Slm66018 		mutex_enter(&vdc->lock);
32753af08d82Slm66018 		/*
32763af08d82Slm66018 		 * Need to wake up any readers so they will
32773af08d82Slm66018 		 * detect that a reset has occurred.
32783af08d82Slm66018 		 */
32793af08d82Slm66018 		mutex_enter(&vdc->read_lock);
32803af08d82Slm66018 		if ((vdc->read_state == VDC_READ_WAITING) ||
32813af08d82Slm66018 		    (vdc->read_state == VDC_READ_RESET))
32823af08d82Slm66018 			cv_signal(&vdc->read_cv);
32833af08d82Slm66018 		vdc->read_state = VDC_READ_RESET;
32843af08d82Slm66018 		mutex_exit(&vdc->read_lock);
32850a55fbb7Slm66018 
32863af08d82Slm66018 		/* wake up any threads waiting for connection to come up */
32873af08d82Slm66018 		if (vdc->state == VDC_STATE_INIT_WAITING) {
32883af08d82Slm66018 			vdc->state = VDC_STATE_RESETTING;
32893af08d82Slm66018 			cv_signal(&vdc->initwait_cv);
32901ae08745Sheppo 		}
32911ae08745Sheppo 
32920a55fbb7Slm66018 		mutex_exit(&vdc->lock);
32931ae08745Sheppo 	}
32941ae08745Sheppo 
32951ae08745Sheppo 	if (event & ~(LDC_EVT_UP | LDC_EVT_RESET | LDC_EVT_DOWN | LDC_EVT_READ))
32963af08d82Slm66018 		DMSG(vdc, 0, "![%d] Unexpected LDC event (%lx) received",
32971ae08745Sheppo 		    vdc->instance, event);
32981ae08745Sheppo 
32991ae08745Sheppo 	return (LDC_SUCCESS);
33001ae08745Sheppo }
33011ae08745Sheppo 
33023af08d82Slm66018 /*
33033af08d82Slm66018  * Function:
33043af08d82Slm66018  *	vdc_wait_for_response()
33053af08d82Slm66018  *
33063af08d82Slm66018  * Description:
33073af08d82Slm66018  *	Block waiting for a response from the server. If there is
33083af08d82Slm66018  *	no data the thread block on the read_cv that is signalled
33093af08d82Slm66018  *	by the callback when an EVT_READ occurs.
33103af08d82Slm66018  *
33113af08d82Slm66018  * Arguments:
33123af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
33133af08d82Slm66018  *
33143af08d82Slm66018  * Return Code:
33153af08d82Slm66018  *	0	- Success
33163af08d82Slm66018  */
33173af08d82Slm66018 static int
33183af08d82Slm66018 vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp)
33193af08d82Slm66018 {
33203af08d82Slm66018 	size_t		nbytes = sizeof (*msgp);
33213af08d82Slm66018 	int		status;
33223af08d82Slm66018 
33233af08d82Slm66018 	ASSERT(vdcp != NULL);
33243af08d82Slm66018 
33253af08d82Slm66018 	DMSG(vdcp, 1, "[%d] Entered\n", vdcp->instance);
33263af08d82Slm66018 
33273af08d82Slm66018 	status = vdc_recv(vdcp, msgp, &nbytes);
33283af08d82Slm66018 	DMSG(vdcp, 3, "vdc_read() done.. status=0x%x size=0x%x\n",
33293af08d82Slm66018 	    status, (int)nbytes);
33303af08d82Slm66018 	if (status) {
33313af08d82Slm66018 		DMSG(vdcp, 0, "?[%d] Error %d reading LDC msg\n",
33323af08d82Slm66018 		    vdcp->instance, status);
33333af08d82Slm66018 		return (status);
33343af08d82Slm66018 	}
33353af08d82Slm66018 
33363af08d82Slm66018 	if (nbytes < sizeof (vio_msg_tag_t)) {
33373af08d82Slm66018 		DMSG(vdcp, 0, "?[%d] Expect %lu bytes; recv'd %lu\n",
33383af08d82Slm66018 		    vdcp->instance, sizeof (vio_msg_tag_t), nbytes);
33393af08d82Slm66018 		return (ENOMSG);
33403af08d82Slm66018 	}
33413af08d82Slm66018 
33423af08d82Slm66018 	DMSG(vdcp, 2, "[%d] (%x/%x/%x)\n", vdcp->instance,
33433af08d82Slm66018 	    msgp->tag.vio_msgtype,
33443af08d82Slm66018 	    msgp->tag.vio_subtype,
33453af08d82Slm66018 	    msgp->tag.vio_subtype_env);
33463af08d82Slm66018 
33473af08d82Slm66018 	/*
33483af08d82Slm66018 	 * Verify the Session ID of the message
33493af08d82Slm66018 	 *
33503af08d82Slm66018 	 * Every message after the Version has been negotiated should
33513af08d82Slm66018 	 * have the correct session ID set.
33523af08d82Slm66018 	 */
33533af08d82Slm66018 	if ((msgp->tag.vio_sid != vdcp->session_id) &&
33543af08d82Slm66018 	    (msgp->tag.vio_subtype_env != VIO_VER_INFO)) {
33553af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Invalid SID: received 0x%x, "
33563af08d82Slm66018 		    "expected 0x%lx [seq num %lx @ %d]",
33573af08d82Slm66018 		    vdcp->instance, msgp->tag.vio_sid,
33583af08d82Slm66018 		    vdcp->session_id,
33593af08d82Slm66018 		    ((vio_dring_msg_t *)msgp)->seq_num,
33603af08d82Slm66018 		    ((vio_dring_msg_t *)msgp)->start_idx);
33613af08d82Slm66018 		return (ENOMSG);
33623af08d82Slm66018 	}
33633af08d82Slm66018 	return (0);
33643af08d82Slm66018 }
33653af08d82Slm66018 
33663af08d82Slm66018 
33673af08d82Slm66018 /*
33683af08d82Slm66018  * Function:
33693af08d82Slm66018  *	vdc_resubmit_backup_dring()
33703af08d82Slm66018  *
33713af08d82Slm66018  * Description:
33723af08d82Slm66018  *	Resubmit each descriptor in the backed up dring to
33733af08d82Slm66018  * 	vDisk server. The Dring was backed up during connection
33743af08d82Slm66018  *	reset.
33753af08d82Slm66018  *
33763af08d82Slm66018  * Arguments:
33773af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
33783af08d82Slm66018  *
33793af08d82Slm66018  * Return Code:
33803af08d82Slm66018  *	0	- Success
33813af08d82Slm66018  */
33823af08d82Slm66018 static int
33833af08d82Slm66018 vdc_resubmit_backup_dring(vdc_t *vdcp)
33843af08d82Slm66018 {
33853af08d82Slm66018 	int		count;
33863af08d82Slm66018 	int		b_idx;
33873af08d82Slm66018 	int		rv;
33883af08d82Slm66018 	int		dring_size;
33893af08d82Slm66018 	int		status;
33903af08d82Slm66018 	vio_msg_t	vio_msg;
33913af08d82Slm66018 	vdc_local_desc_t	*curr_ldep;
33923af08d82Slm66018 
33933af08d82Slm66018 	ASSERT(MUTEX_NOT_HELD(&vdcp->lock));
33943af08d82Slm66018 	ASSERT(vdcp->state == VDC_STATE_HANDLE_PENDING);
33953af08d82Slm66018 
3396655fd6a9Sachartre 	if (vdcp->local_dring_backup == NULL) {
3397655fd6a9Sachartre 		/* the pending requests have already been processed */
3398655fd6a9Sachartre 		return (0);
3399655fd6a9Sachartre 	}
3400655fd6a9Sachartre 
34013af08d82Slm66018 	DMSG(vdcp, 1, "restoring pending dring entries (len=%d, tail=%d)\n",
34023af08d82Slm66018 	    vdcp->local_dring_backup_len, vdcp->local_dring_backup_tail);
34033af08d82Slm66018 
34043af08d82Slm66018 	/*
34053af08d82Slm66018 	 * Walk the backup copy of the local descriptor ring and
34063af08d82Slm66018 	 * resubmit all the outstanding transactions.
34073af08d82Slm66018 	 */
34083af08d82Slm66018 	b_idx = vdcp->local_dring_backup_tail;
34093af08d82Slm66018 	for (count = 0; count < vdcp->local_dring_backup_len; count++) {
34103af08d82Slm66018 
34113af08d82Slm66018 		curr_ldep = &(vdcp->local_dring_backup[b_idx]);
34123af08d82Slm66018 
3413eff7243fSlm66018 		/* only resubmit outstanding transactions */
34143af08d82Slm66018 		if (!curr_ldep->is_free) {
34153af08d82Slm66018 
34163af08d82Slm66018 			DMSG(vdcp, 1, "resubmitting entry idx=%x\n", b_idx);
34173af08d82Slm66018 			mutex_enter(&vdcp->lock);
34183af08d82Slm66018 			rv = vdc_populate_descriptor(vdcp, curr_ldep->operation,
34193af08d82Slm66018 			    curr_ldep->addr, curr_ldep->nbytes,
34203af08d82Slm66018 			    curr_ldep->slice, curr_ldep->offset,
34213af08d82Slm66018 			    curr_ldep->cb_type, curr_ldep->cb_arg,
34223af08d82Slm66018 			    curr_ldep->dir);
34233af08d82Slm66018 			mutex_exit(&vdcp->lock);
34243af08d82Slm66018 			if (rv) {
34253af08d82Slm66018 				DMSG(vdcp, 1, "[%d] cannot resubmit entry %d\n",
34263af08d82Slm66018 				    vdcp->instance, b_idx);
34273af08d82Slm66018 				return (rv);
34283af08d82Slm66018 			}
34293af08d82Slm66018 
34303af08d82Slm66018 			/* Wait for the response message. */
34313af08d82Slm66018 			DMSG(vdcp, 1, "waiting for response to idx=%x\n",
34323af08d82Slm66018 			    b_idx);
34333af08d82Slm66018 			status = vdc_wait_for_response(vdcp, &vio_msg);
34343af08d82Slm66018 			if (status) {
34353af08d82Slm66018 				DMSG(vdcp, 1, "[%d] wait_for_response "
34363af08d82Slm66018 				    "returned err=%d\n", vdcp->instance,
34373af08d82Slm66018 				    status);
34383af08d82Slm66018 				return (status);
34393af08d82Slm66018 			}
34403af08d82Slm66018 
34413af08d82Slm66018 			DMSG(vdcp, 1, "processing msg for idx=%x\n", b_idx);
34423af08d82Slm66018 			status = vdc_process_data_msg(vdcp, &vio_msg);
34433af08d82Slm66018 			if (status) {
34443af08d82Slm66018 				DMSG(vdcp, 1, "[%d] process_data_msg "
34453af08d82Slm66018 				    "returned err=%d\n", vdcp->instance,
34463af08d82Slm66018 				    status);
34473af08d82Slm66018 				return (status);
34483af08d82Slm66018 			}
34493af08d82Slm66018 		}
34503af08d82Slm66018 
34513af08d82Slm66018 		/* get the next element to submit */
34523af08d82Slm66018 		if (++b_idx >= vdcp->local_dring_backup_len)
34533af08d82Slm66018 			b_idx = 0;
34543af08d82Slm66018 	}
34553af08d82Slm66018 
34563af08d82Slm66018 	/* all done - now clear up pending dring copy */
34573af08d82Slm66018 	dring_size = vdcp->local_dring_backup_len *
34583af08d82Slm66018 	    sizeof (vdcp->local_dring_backup[0]);
34593af08d82Slm66018 
34603af08d82Slm66018 	(void) kmem_free(vdcp->local_dring_backup, dring_size);
34613af08d82Slm66018 
34623af08d82Slm66018 	vdcp->local_dring_backup = NULL;
34633af08d82Slm66018 
34643af08d82Slm66018 	return (0);
34653af08d82Slm66018 }
34663af08d82Slm66018 
34673af08d82Slm66018 /*
34683af08d82Slm66018  * Function:
3469655fd6a9Sachartre  *	vdc_cancel_backup_dring
3470655fd6a9Sachartre  *
3471655fd6a9Sachartre  * Description:
3472655fd6a9Sachartre  *	Cancel each descriptor in the backed up dring to vDisk server.
3473655fd6a9Sachartre  *	The Dring was backed up during connection reset.
3474655fd6a9Sachartre  *
3475655fd6a9Sachartre  * Arguments:
3476655fd6a9Sachartre  *	vdcp	- soft state pointer for this instance of the device driver.
3477655fd6a9Sachartre  *
3478655fd6a9Sachartre  * Return Code:
3479655fd6a9Sachartre  *	None
3480655fd6a9Sachartre  */
3481655fd6a9Sachartre void
3482655fd6a9Sachartre vdc_cancel_backup_ring(vdc_t *vdcp)
3483655fd6a9Sachartre {
3484655fd6a9Sachartre 	vdc_local_desc_t *ldep;
3485655fd6a9Sachartre 	struct buf 	*bufp;
3486655fd6a9Sachartre 	int		count;
3487655fd6a9Sachartre 	int		b_idx;
3488655fd6a9Sachartre 	int		dring_size;
3489655fd6a9Sachartre 
3490655fd6a9Sachartre 	ASSERT(MUTEX_HELD(&vdcp->lock));
3491655fd6a9Sachartre 	ASSERT(vdcp->state == VDC_STATE_INIT ||
3492655fd6a9Sachartre 	    vdcp->state == VDC_STATE_INIT_WAITING ||
3493655fd6a9Sachartre 	    vdcp->state == VDC_STATE_NEGOTIATE ||
3494655fd6a9Sachartre 	    vdcp->state == VDC_STATE_RESETTING);
3495655fd6a9Sachartre 
3496655fd6a9Sachartre 	if (vdcp->local_dring_backup == NULL) {
3497655fd6a9Sachartre 		/* the pending requests have already been processed */
3498655fd6a9Sachartre 		return;
3499655fd6a9Sachartre 	}
3500655fd6a9Sachartre 
3501655fd6a9Sachartre 	DMSG(vdcp, 1, "cancelling pending dring entries (len=%d, tail=%d)\n",
3502655fd6a9Sachartre 	    vdcp->local_dring_backup_len, vdcp->local_dring_backup_tail);
3503655fd6a9Sachartre 
3504655fd6a9Sachartre 	/*
3505655fd6a9Sachartre 	 * Walk the backup copy of the local descriptor ring and
3506655fd6a9Sachartre 	 * cancel all the outstanding transactions.
3507655fd6a9Sachartre 	 */
3508655fd6a9Sachartre 	b_idx = vdcp->local_dring_backup_tail;
3509655fd6a9Sachartre 	for (count = 0; count < vdcp->local_dring_backup_len; count++) {
3510655fd6a9Sachartre 
3511655fd6a9Sachartre 		ldep = &(vdcp->local_dring_backup[b_idx]);
3512655fd6a9Sachartre 
3513655fd6a9Sachartre 		/* only cancel outstanding transactions */
3514655fd6a9Sachartre 		if (!ldep->is_free) {
3515655fd6a9Sachartre 
3516655fd6a9Sachartre 			DMSG(vdcp, 1, "cancelling entry idx=%x\n", b_idx);
3517655fd6a9Sachartre 
3518655fd6a9Sachartre 			/*
3519655fd6a9Sachartre 			 * All requests have already been cleared from the
3520655fd6a9Sachartre 			 * local descriptor ring and the LDC channel has been
3521655fd6a9Sachartre 			 * reset so we will never get any reply for these
3522655fd6a9Sachartre 			 * requests. Now we just have to notify threads waiting
3523655fd6a9Sachartre 			 * for replies that the request has failed.
3524655fd6a9Sachartre 			 */
3525655fd6a9Sachartre 			switch (ldep->cb_type) {
3526655fd6a9Sachartre 			case CB_SYNC:
3527655fd6a9Sachartre 				ASSERT(vdcp->sync_op_pending);
3528655fd6a9Sachartre 				vdcp->sync_op_status = EIO;
3529655fd6a9Sachartre 				vdcp->sync_op_pending = B_FALSE;
3530655fd6a9Sachartre 				cv_signal(&vdcp->sync_pending_cv);
3531655fd6a9Sachartre 				break;
3532655fd6a9Sachartre 
3533655fd6a9Sachartre 			case CB_STRATEGY:
3534655fd6a9Sachartre 				bufp = ldep->cb_arg;
3535655fd6a9Sachartre 				ASSERT(bufp != NULL);
3536655fd6a9Sachartre 				bufp->b_resid = bufp->b_bcount;
3537655fd6a9Sachartre 				bioerror(bufp, EIO);
3538655fd6a9Sachartre 				biodone(bufp);
3539655fd6a9Sachartre 				break;
3540655fd6a9Sachartre 
3541655fd6a9Sachartre 			default:
3542655fd6a9Sachartre 				ASSERT(0);
3543655fd6a9Sachartre 			}
3544655fd6a9Sachartre 
3545655fd6a9Sachartre 		}
3546655fd6a9Sachartre 
3547655fd6a9Sachartre 		/* get the next element to cancel */
3548655fd6a9Sachartre 		if (++b_idx >= vdcp->local_dring_backup_len)
3549655fd6a9Sachartre 			b_idx = 0;
3550655fd6a9Sachartre 	}
3551655fd6a9Sachartre 
3552655fd6a9Sachartre 	/* all done - now clear up pending dring copy */
3553655fd6a9Sachartre 	dring_size = vdcp->local_dring_backup_len *
3554655fd6a9Sachartre 	    sizeof (vdcp->local_dring_backup[0]);
3555655fd6a9Sachartre 
3556655fd6a9Sachartre 	(void) kmem_free(vdcp->local_dring_backup, dring_size);
3557655fd6a9Sachartre 
3558655fd6a9Sachartre 	vdcp->local_dring_backup = NULL;
3559655fd6a9Sachartre 
3560655fd6a9Sachartre 	DTRACE_IO2(processed, int, count, vdc_t *, vdcp);
3561655fd6a9Sachartre }
3562655fd6a9Sachartre 
3563655fd6a9Sachartre /*
3564655fd6a9Sachartre  * Function:
3565655fd6a9Sachartre  *	vdc_connection_timeout
3566655fd6a9Sachartre  *
3567655fd6a9Sachartre  * Description:
3568655fd6a9Sachartre  *	This function is invoked if the timeout set to establish the connection
3569655fd6a9Sachartre  *	with vds expires. This will happen if we spend too much time in the
3570655fd6a9Sachartre  *	VDC_STATE_INIT_WAITING or VDC_STATE_NEGOTIATE states. Then we will
3571655fd6a9Sachartre  *	cancel any pending request and mark them as failed.
3572655fd6a9Sachartre  *
3573655fd6a9Sachartre  *	If the timeout does not expire, it will be cancelled when we reach the
3574655fd6a9Sachartre  *	VDC_STATE_HANDLE_PENDING or VDC_STATE_RESETTING state. This function can
3575655fd6a9Sachartre  *	be invoked while we are in the VDC_STATE_HANDLE_PENDING or
3576655fd6a9Sachartre  *	VDC_STATE_RESETTING state in which case we do nothing because the
3577655fd6a9Sachartre  *	timeout is being cancelled.
3578655fd6a9Sachartre  *
3579655fd6a9Sachartre  * Arguments:
3580655fd6a9Sachartre  *	arg	- argument of the timeout function actually a soft state
3581655fd6a9Sachartre  *		  pointer for the instance of the device driver.
3582655fd6a9Sachartre  *
3583655fd6a9Sachartre  * Return Code:
3584655fd6a9Sachartre  *	None
3585655fd6a9Sachartre  */
3586655fd6a9Sachartre void
3587655fd6a9Sachartre vdc_connection_timeout(void *arg)
3588655fd6a9Sachartre {
3589655fd6a9Sachartre 	vdc_t 		*vdcp = (vdc_t *)arg;
3590655fd6a9Sachartre 
3591655fd6a9Sachartre 	mutex_enter(&vdcp->lock);
3592655fd6a9Sachartre 
3593655fd6a9Sachartre 	if (vdcp->state == VDC_STATE_HANDLE_PENDING ||
3594655fd6a9Sachartre 	    vdcp->state == VDC_STATE_DETACH) {
3595655fd6a9Sachartre 		/*
3596655fd6a9Sachartre 		 * The connection has just been re-established or
3597655fd6a9Sachartre 		 * we are detaching.
3598655fd6a9Sachartre 		 */
3599655fd6a9Sachartre 		vdcp->ctimeout_reached = B_FALSE;
3600655fd6a9Sachartre 		mutex_exit(&vdcp->lock);
3601655fd6a9Sachartre 		return;
3602655fd6a9Sachartre 	}
3603655fd6a9Sachartre 
3604655fd6a9Sachartre 	vdcp->ctimeout_reached = B_TRUE;
3605655fd6a9Sachartre 
3606655fd6a9Sachartre 	/* notify requests waiting for sending */
3607655fd6a9Sachartre 	cv_broadcast(&vdcp->running_cv);
3608655fd6a9Sachartre 
3609655fd6a9Sachartre 	/* cancel requests waiting for a result */
3610655fd6a9Sachartre 	vdc_cancel_backup_ring(vdcp);
3611655fd6a9Sachartre 
3612655fd6a9Sachartre 	mutex_exit(&vdcp->lock);
3613655fd6a9Sachartre 
3614655fd6a9Sachartre 	cmn_err(CE_NOTE, "[%d] connection to service domain timeout",
3615655fd6a9Sachartre 	    vdcp->instance);
3616655fd6a9Sachartre }
3617655fd6a9Sachartre 
3618655fd6a9Sachartre /*
3619655fd6a9Sachartre  * Function:
36203af08d82Slm66018  *	vdc_backup_local_dring()
36213af08d82Slm66018  *
36223af08d82Slm66018  * Description:
36233af08d82Slm66018  *	Backup the current dring in the event of a reset. The Dring
36243af08d82Slm66018  *	transactions will be resubmitted to the server when the
36253af08d82Slm66018  *	connection is restored.
36263af08d82Slm66018  *
36273af08d82Slm66018  * Arguments:
36283af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
36293af08d82Slm66018  *
36303af08d82Slm66018  * Return Code:
36313af08d82Slm66018  *	NONE
36323af08d82Slm66018  */
36333af08d82Slm66018 static void
36343af08d82Slm66018 vdc_backup_local_dring(vdc_t *vdcp)
36353af08d82Slm66018 {
36363af08d82Slm66018 	int dring_size;
36373af08d82Slm66018 
3638655fd6a9Sachartre 	ASSERT(MUTEX_HELD(&vdcp->lock));
36393af08d82Slm66018 	ASSERT(vdcp->state == VDC_STATE_RESETTING);
36403af08d82Slm66018 
36413af08d82Slm66018 	/*
36423af08d82Slm66018 	 * If the backup dring is stil around, it means
36433af08d82Slm66018 	 * that the last restore did not complete. However,
36443af08d82Slm66018 	 * since we never got back into the running state,
36453af08d82Slm66018 	 * the backup copy we have is still valid.
36463af08d82Slm66018 	 */
36473af08d82Slm66018 	if (vdcp->local_dring_backup != NULL) {
36483af08d82Slm66018 		DMSG(vdcp, 1, "reusing local descriptor ring backup "
36493af08d82Slm66018 		    "(len=%d, tail=%d)\n", vdcp->local_dring_backup_len,
36503af08d82Slm66018 		    vdcp->local_dring_backup_tail);
36513af08d82Slm66018 		return;
36523af08d82Slm66018 	}
36533af08d82Slm66018 
3654655fd6a9Sachartre 	/*
3655655fd6a9Sachartre 	 * The backup dring can be NULL and the local dring may not be
3656655fd6a9Sachartre 	 * initialized. This can happen if we had a reset while establishing
3657655fd6a9Sachartre 	 * a new connection but after the connection has timed out. In that
3658655fd6a9Sachartre 	 * case the backup dring is NULL because the requests have been
3659655fd6a9Sachartre 	 * cancelled and the request occured before the local dring is
3660655fd6a9Sachartre 	 * initialized.
3661655fd6a9Sachartre 	 */
3662655fd6a9Sachartre 	if (!(vdcp->initialized & VDC_DRING_LOCAL))
3663655fd6a9Sachartre 		return;
3664655fd6a9Sachartre 
36653af08d82Slm66018 	DMSG(vdcp, 1, "backing up the local descriptor ring (len=%d, "
36663af08d82Slm66018 	    "tail=%d)\n", vdcp->dring_len, vdcp->dring_curr_idx);
36673af08d82Slm66018 
36683af08d82Slm66018 	dring_size = vdcp->dring_len * sizeof (vdcp->local_dring[0]);
36693af08d82Slm66018 
36703af08d82Slm66018 	vdcp->local_dring_backup = kmem_alloc(dring_size, KM_SLEEP);
36713af08d82Slm66018 	bcopy(vdcp->local_dring, vdcp->local_dring_backup, dring_size);
36723af08d82Slm66018 
36733af08d82Slm66018 	vdcp->local_dring_backup_tail = vdcp->dring_curr_idx;
36743af08d82Slm66018 	vdcp->local_dring_backup_len = vdcp->dring_len;
36753af08d82Slm66018 }
36763af08d82Slm66018 
36771ae08745Sheppo /* -------------------------------------------------------------------------- */
36781ae08745Sheppo 
36791ae08745Sheppo /*
36801ae08745Sheppo  * The following functions process the incoming messages from vds
36811ae08745Sheppo  */
36821ae08745Sheppo 
36830a55fbb7Slm66018 /*
36840a55fbb7Slm66018  * Function:
36850a55fbb7Slm66018  *      vdc_process_msg_thread()
36860a55fbb7Slm66018  *
36870a55fbb7Slm66018  * Description:
36880a55fbb7Slm66018  *
36893af08d82Slm66018  *	Main VDC message processing thread. Each vDisk instance
36903af08d82Slm66018  * 	consists of a copy of this thread. This thread triggers
36913af08d82Slm66018  * 	all the handshakes and data exchange with the server. It
36923af08d82Slm66018  * 	also handles all channel resets
36933af08d82Slm66018  *
36940a55fbb7Slm66018  * Arguments:
36950a55fbb7Slm66018  *      vdc     - soft state pointer for this instance of the device driver.
36960a55fbb7Slm66018  *
36970a55fbb7Slm66018  * Return Code:
36980a55fbb7Slm66018  *      None
36990a55fbb7Slm66018  */
37001ae08745Sheppo static void
37013af08d82Slm66018 vdc_process_msg_thread(vdc_t *vdcp)
37021ae08745Sheppo {
37031ae08745Sheppo 	int	status;
3704655fd6a9Sachartre 	int	ctimeout;
3705655fd6a9Sachartre 	timeout_id_t tmid = 0;
37061ae08745Sheppo 
37073af08d82Slm66018 	mutex_enter(&vdcp->lock);
37081ae08745Sheppo 
37091ae08745Sheppo 	for (;;) {
37101ae08745Sheppo 
37113af08d82Slm66018 #define	Q(_s)	(vdcp->state == _s) ? #_s :
37123af08d82Slm66018 		DMSG(vdcp, 3, "state = %d (%s)\n", vdcp->state,
37133af08d82Slm66018 		    Q(VDC_STATE_INIT)
37143af08d82Slm66018 		    Q(VDC_STATE_INIT_WAITING)
37153af08d82Slm66018 		    Q(VDC_STATE_NEGOTIATE)
37163af08d82Slm66018 		    Q(VDC_STATE_HANDLE_PENDING)
37173af08d82Slm66018 		    Q(VDC_STATE_RUNNING)
37183af08d82Slm66018 		    Q(VDC_STATE_RESETTING)
37193af08d82Slm66018 		    Q(VDC_STATE_DETACH)
37203af08d82Slm66018 		    "UNKNOWN");
37211ae08745Sheppo 
37223af08d82Slm66018 		switch (vdcp->state) {
37233af08d82Slm66018 		case VDC_STATE_INIT:
37243af08d82Slm66018 
3725655fd6a9Sachartre 			/*
3726655fd6a9Sachartre 			 * If requested, start a timeout to check if the
3727655fd6a9Sachartre 			 * connection with vds is established in the
3728655fd6a9Sachartre 			 * specified delay. If the timeout expires, we
3729655fd6a9Sachartre 			 * will cancel any pending request.
3730655fd6a9Sachartre 			 *
3731655fd6a9Sachartre 			 * If some reset have occurred while establishing
3732655fd6a9Sachartre 			 * the connection, we already have a timeout armed
3733655fd6a9Sachartre 			 * and in that case we don't need to arm a new one.
3734655fd6a9Sachartre 			 */
3735655fd6a9Sachartre 			ctimeout = (vdc_timeout != 0)?
3736655fd6a9Sachartre 			    vdc_timeout : vdcp->ctimeout;
3737655fd6a9Sachartre 
3738655fd6a9Sachartre 			if (ctimeout != 0 && tmid == 0) {
3739655fd6a9Sachartre 				tmid = timeout(vdc_connection_timeout, vdcp,
3740655fd6a9Sachartre 				    ctimeout * drv_usectohz(1000000));
3741655fd6a9Sachartre 			}
3742655fd6a9Sachartre 
37433af08d82Slm66018 			/* Check if have re-initializing repeatedly */
3744655fd6a9Sachartre 			if (vdcp->hshake_cnt++ > vdc_hshake_retries &&
3745655fd6a9Sachartre 			    vdcp->lifecycle != VDC_LC_ONLINE) {
37463c96341aSnarayan 				cmn_err(CE_NOTE, "[%d] disk access failed.\n",
37473c96341aSnarayan 				    vdcp->instance);
37483af08d82Slm66018 				vdcp->state = VDC_STATE_DETACH;
37493af08d82Slm66018 				break;
37503af08d82Slm66018 			}
37513af08d82Slm66018 
37523af08d82Slm66018 			/* Bring up connection with vds via LDC */
37533af08d82Slm66018 			status = vdc_start_ldc_connection(vdcp);
3754655fd6a9Sachartre 			if (status == EINVAL) {
37553af08d82Slm66018 				DMSG(vdcp, 0, "[%d] Could not start LDC",
37563af08d82Slm66018 				    vdcp->instance);
37573af08d82Slm66018 				vdcp->state = VDC_STATE_DETACH;
3758655fd6a9Sachartre 			} else {
37593af08d82Slm66018 				vdcp->state = VDC_STATE_INIT_WAITING;
37603af08d82Slm66018 			}
37613af08d82Slm66018 			break;
37623af08d82Slm66018 
37633af08d82Slm66018 		case VDC_STATE_INIT_WAITING:
37643af08d82Slm66018 
37653af08d82Slm66018 			/*
37663af08d82Slm66018 			 * Let the callback event move us on
37673af08d82Slm66018 			 * when channel is open to server
37683af08d82Slm66018 			 */
37693af08d82Slm66018 			while (vdcp->ldc_state != LDC_UP) {
37703af08d82Slm66018 				cv_wait(&vdcp->initwait_cv, &vdcp->lock);
37713af08d82Slm66018 				if (vdcp->state != VDC_STATE_INIT_WAITING) {
37723af08d82Slm66018 					DMSG(vdcp, 0,
37733af08d82Slm66018 				"state moved to %d out from under us...\n",
37743af08d82Slm66018 					    vdcp->state);
37753af08d82Slm66018 
37763af08d82Slm66018 					break;
37773af08d82Slm66018 				}
37783af08d82Slm66018 			}
37793af08d82Slm66018 			if (vdcp->state == VDC_STATE_INIT_WAITING &&
37803af08d82Slm66018 			    vdcp->ldc_state == LDC_UP) {
37813af08d82Slm66018 				vdcp->state = VDC_STATE_NEGOTIATE;
37823af08d82Slm66018 			}
37833af08d82Slm66018 			break;
37843af08d82Slm66018 
37853af08d82Slm66018 		case VDC_STATE_NEGOTIATE:
37863af08d82Slm66018 			switch (status = vdc_ver_negotiation(vdcp)) {
37873af08d82Slm66018 			case 0:
37883af08d82Slm66018 				break;
37893af08d82Slm66018 			default:
37903af08d82Slm66018 				DMSG(vdcp, 0, "ver negotiate failed (%d)..\n",
37913af08d82Slm66018 				    status);
37923af08d82Slm66018 				goto reset;
37933af08d82Slm66018 			}
37943af08d82Slm66018 
37953af08d82Slm66018 			switch (status = vdc_attr_negotiation(vdcp)) {
37963af08d82Slm66018 			case 0:
37973af08d82Slm66018 				break;
37983af08d82Slm66018 			default:
37993af08d82Slm66018 				DMSG(vdcp, 0, "attr negotiate failed (%d)..\n",
38003af08d82Slm66018 				    status);
38013af08d82Slm66018 				goto reset;
38023af08d82Slm66018 			}
38033af08d82Slm66018 
38043af08d82Slm66018 			switch (status = vdc_dring_negotiation(vdcp)) {
38053af08d82Slm66018 			case 0:
38063af08d82Slm66018 				break;
38073af08d82Slm66018 			default:
38083af08d82Slm66018 				DMSG(vdcp, 0, "dring negotiate failed (%d)..\n",
38093af08d82Slm66018 				    status);
38103af08d82Slm66018 				goto reset;
38113af08d82Slm66018 			}
38123af08d82Slm66018 
38133af08d82Slm66018 			switch (status = vdc_rdx_exchange(vdcp)) {
38143af08d82Slm66018 			case 0:
38153af08d82Slm66018 				vdcp->state = VDC_STATE_HANDLE_PENDING;
38163af08d82Slm66018 				goto done;
38173af08d82Slm66018 			default:
38183af08d82Slm66018 				DMSG(vdcp, 0, "RDX xchg failed ..(%d)\n",
38193af08d82Slm66018 				    status);
38203af08d82Slm66018 				goto reset;
38213af08d82Slm66018 			}
38223af08d82Slm66018 reset:
38233af08d82Slm66018 			DMSG(vdcp, 0, "negotiation failed: resetting (%d)\n",
38243af08d82Slm66018 			    status);
38253af08d82Slm66018 			vdcp->state = VDC_STATE_RESETTING;
3826655fd6a9Sachartre 			vdcp->self_reset = B_TRUE;
38273af08d82Slm66018 done:
38283af08d82Slm66018 			DMSG(vdcp, 0, "negotiation complete (state=0x%x)...\n",
38293af08d82Slm66018 			    vdcp->state);
38303af08d82Slm66018 			break;
38313af08d82Slm66018 
38323af08d82Slm66018 		case VDC_STATE_HANDLE_PENDING:
38333af08d82Slm66018 
3834655fd6a9Sachartre 			if (vdcp->ctimeout_reached) {
3835655fd6a9Sachartre 				/*
3836655fd6a9Sachartre 				 * The connection timeout had been reached so
3837655fd6a9Sachartre 				 * pending requests have been cancelled. Now
3838655fd6a9Sachartre 				 * that the connection is back we can reset
3839655fd6a9Sachartre 				 * the timeout.
3840655fd6a9Sachartre 				 */
3841655fd6a9Sachartre 				ASSERT(vdcp->local_dring_backup == NULL);
3842655fd6a9Sachartre 				ASSERT(tmid != 0);
3843655fd6a9Sachartre 				tmid = 0;
3844655fd6a9Sachartre 				vdcp->ctimeout_reached = B_FALSE;
3845655fd6a9Sachartre 				vdcp->state = VDC_STATE_RUNNING;
3846655fd6a9Sachartre 				DMSG(vdcp, 0, "[%d] connection to service "
3847655fd6a9Sachartre 				    "domain is up", vdcp->instance);
3848655fd6a9Sachartre 				break;
3849655fd6a9Sachartre 			}
3850655fd6a9Sachartre 
38513af08d82Slm66018 			mutex_exit(&vdcp->lock);
3852655fd6a9Sachartre 			if (tmid != 0) {
3853655fd6a9Sachartre 				(void) untimeout(tmid);
3854655fd6a9Sachartre 				tmid = 0;
3855655fd6a9Sachartre 			}
38563af08d82Slm66018 			status = vdc_resubmit_backup_dring(vdcp);
38573af08d82Slm66018 			mutex_enter(&vdcp->lock);
38583af08d82Slm66018 
38593af08d82Slm66018 			if (status)
38603af08d82Slm66018 				vdcp->state = VDC_STATE_RESETTING;
38613af08d82Slm66018 			else
38623af08d82Slm66018 				vdcp->state = VDC_STATE_RUNNING;
38633af08d82Slm66018 
38643af08d82Slm66018 			break;
38653af08d82Slm66018 
38663af08d82Slm66018 		/* enter running state */
38673af08d82Slm66018 		case VDC_STATE_RUNNING:
38683af08d82Slm66018 			/*
38693af08d82Slm66018 			 * Signal anyone waiting for the connection
38703af08d82Slm66018 			 * to come on line.
38713af08d82Slm66018 			 */
38723af08d82Slm66018 			vdcp->hshake_cnt = 0;
38733af08d82Slm66018 			cv_broadcast(&vdcp->running_cv);
38743af08d82Slm66018 			mutex_exit(&vdcp->lock);
38753af08d82Slm66018 
38763af08d82Slm66018 			for (;;) {
38773af08d82Slm66018 				vio_msg_t msg;
38783af08d82Slm66018 				status = vdc_wait_for_response(vdcp, &msg);
38793af08d82Slm66018 				if (status) break;
38803af08d82Slm66018 
38813af08d82Slm66018 				DMSG(vdcp, 1, "[%d] new pkt(s) available\n",
38823af08d82Slm66018 				    vdcp->instance);
38833af08d82Slm66018 				status = vdc_process_data_msg(vdcp, &msg);
38841ae08745Sheppo 				if (status) {
38853af08d82Slm66018 					DMSG(vdcp, 1, "[%d] process_data_msg "
38863af08d82Slm66018 					    "returned err=%d\n", vdcp->instance,
38873af08d82Slm66018 					    status);
38881ae08745Sheppo 					break;
38891ae08745Sheppo 				}
38901ae08745Sheppo 
38913af08d82Slm66018 			}
3892e1ebb9ecSlm66018 
38933af08d82Slm66018 			mutex_enter(&vdcp->lock);
38943af08d82Slm66018 
38953af08d82Slm66018 			vdcp->state = VDC_STATE_RESETTING;
3896690555a1Sachartre 			vdcp->self_reset = B_TRUE;
38973af08d82Slm66018 			break;
38983af08d82Slm66018 
38993af08d82Slm66018 		case VDC_STATE_RESETTING:
3900655fd6a9Sachartre 			/*
3901655fd6a9Sachartre 			 * When we reach this state, we either come from the
3902655fd6a9Sachartre 			 * VDC_STATE_RUNNING state and we can have pending
3903655fd6a9Sachartre 			 * request but no timeout is armed; or we come from
3904655fd6a9Sachartre 			 * the VDC_STATE_INIT_WAITING, VDC_NEGOTIATE or
3905655fd6a9Sachartre 			 * VDC_HANDLE_PENDING state and there is no pending
3906655fd6a9Sachartre 			 * request or pending requests have already been copied
3907655fd6a9Sachartre 			 * into the backup dring. So we can safely keep the
3908655fd6a9Sachartre 			 * connection timeout armed while we are in this state.
3909655fd6a9Sachartre 			 */
3910655fd6a9Sachartre 
39113af08d82Slm66018 			DMSG(vdcp, 0, "Initiating channel reset "
39123af08d82Slm66018 			    "(pending = %d)\n", (int)vdcp->threads_pending);
39133af08d82Slm66018 
39143af08d82Slm66018 			if (vdcp->self_reset) {
39153af08d82Slm66018 				DMSG(vdcp, 0,
39163af08d82Slm66018 				    "[%d] calling stop_ldc_connection.\n",
39173af08d82Slm66018 				    vdcp->instance);
39183af08d82Slm66018 				status = vdc_stop_ldc_connection(vdcp);
39193af08d82Slm66018 				vdcp->self_reset = B_FALSE;
39201ae08745Sheppo 			}
39211ae08745Sheppo 
39221ae08745Sheppo 			/*
39233af08d82Slm66018 			 * Wait for all threads currently waiting
39243af08d82Slm66018 			 * for a free dring entry to use.
39251ae08745Sheppo 			 */
39263af08d82Slm66018 			while (vdcp->threads_pending) {
39273af08d82Slm66018 				cv_broadcast(&vdcp->membind_cv);
39283af08d82Slm66018 				cv_broadcast(&vdcp->dring_free_cv);
39293af08d82Slm66018 				mutex_exit(&vdcp->lock);
3930205eeb1aSlm66018 				/* give the waiters enough time to wake up */
3931205eeb1aSlm66018 				delay(vdc_hz_min_ldc_delay);
39323af08d82Slm66018 				mutex_enter(&vdcp->lock);
39331ae08745Sheppo 			}
39341ae08745Sheppo 
39353af08d82Slm66018 			ASSERT(vdcp->threads_pending == 0);
39361ae08745Sheppo 
39373af08d82Slm66018 			/* Sanity check that no thread is receiving */
39383af08d82Slm66018 			ASSERT(vdcp->read_state != VDC_READ_WAITING);
39390a55fbb7Slm66018 
39403af08d82Slm66018 			vdcp->read_state = VDC_READ_IDLE;
39413af08d82Slm66018 
39423af08d82Slm66018 			vdc_backup_local_dring(vdcp);
39433af08d82Slm66018 
39443af08d82Slm66018 			/* cleanup the old d-ring */
39453af08d82Slm66018 			vdc_destroy_descriptor_ring(vdcp);
39463af08d82Slm66018 
39473af08d82Slm66018 			/* go and start again */
39483af08d82Slm66018 			vdcp->state = VDC_STATE_INIT;
39493af08d82Slm66018 
39500a55fbb7Slm66018 			break;
39510a55fbb7Slm66018 
39523af08d82Slm66018 		case VDC_STATE_DETACH:
39533af08d82Slm66018 			DMSG(vdcp, 0, "[%d] Reset thread exit cleanup ..\n",
39543af08d82Slm66018 			    vdcp->instance);
39553af08d82Slm66018 
3956655fd6a9Sachartre 			/* cancel any pending timeout */
3957655fd6a9Sachartre 			mutex_exit(&vdcp->lock);
3958655fd6a9Sachartre 			if (tmid != 0) {
3959655fd6a9Sachartre 				(void) untimeout(tmid);
3960655fd6a9Sachartre 				tmid = 0;
3961655fd6a9Sachartre 			}
3962655fd6a9Sachartre 			mutex_enter(&vdcp->lock);
3963655fd6a9Sachartre 
39643c96341aSnarayan 			/*
39653c96341aSnarayan 			 * Signal anyone waiting for connection
39663c96341aSnarayan 			 * to come online
39673c96341aSnarayan 			 */
39683c96341aSnarayan 			cv_broadcast(&vdcp->running_cv);
39693c96341aSnarayan 
39703af08d82Slm66018 			while (vdcp->sync_op_pending) {
39713af08d82Slm66018 				cv_signal(&vdcp->sync_pending_cv);
39723af08d82Slm66018 				cv_signal(&vdcp->sync_blocked_cv);
39733af08d82Slm66018 				mutex_exit(&vdcp->lock);
3974205eeb1aSlm66018 				/* give the waiters enough time to wake up */
3975205eeb1aSlm66018 				delay(vdc_hz_min_ldc_delay);
39763af08d82Slm66018 				mutex_enter(&vdcp->lock);
39770a55fbb7Slm66018 			}
39781ae08745Sheppo 
39793af08d82Slm66018 			mutex_exit(&vdcp->lock);
39803af08d82Slm66018 
39813af08d82Slm66018 			DMSG(vdcp, 0, "[%d] Msg processing thread exiting ..\n",
39823af08d82Slm66018 			    vdcp->instance);
39833af08d82Slm66018 			thread_exit();
39843af08d82Slm66018 			break;
39853af08d82Slm66018 		}
39863af08d82Slm66018 	}
39870a55fbb7Slm66018 }
39880a55fbb7Slm66018 
39890a55fbb7Slm66018 
39900a55fbb7Slm66018 /*
39910a55fbb7Slm66018  * Function:
39920a55fbb7Slm66018  *	vdc_process_data_msg()
39930a55fbb7Slm66018  *
39940a55fbb7Slm66018  * Description:
39950a55fbb7Slm66018  *	This function is called by the message processing thread each time
39960a55fbb7Slm66018  *	a message with a msgtype of VIO_TYPE_DATA is received. It will either
39970a55fbb7Slm66018  *	be an ACK or NACK from vds[1] which vdc handles as follows.
39980a55fbb7Slm66018  *		ACK	- wake up the waiting thread
39990a55fbb7Slm66018  *		NACK	- resend any messages necessary
40000a55fbb7Slm66018  *
40010a55fbb7Slm66018  *	[1] Although the message format allows it, vds should not send a
40020a55fbb7Slm66018  *	    VIO_SUBTYPE_INFO message to vdc asking it to read data; if for
40030a55fbb7Slm66018  *	    some bizarre reason it does, vdc will reset the connection.
40040a55fbb7Slm66018  *
40050a55fbb7Slm66018  * Arguments:
40060a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
40070a55fbb7Slm66018  *	msg	- the LDC message sent by vds
40080a55fbb7Slm66018  *
40090a55fbb7Slm66018  * Return Code:
40100a55fbb7Slm66018  *	0	- Success.
40110a55fbb7Slm66018  *	> 0	- error value returned by LDC
40120a55fbb7Slm66018  */
40130a55fbb7Slm66018 static int
40143af08d82Slm66018 vdc_process_data_msg(vdc_t *vdcp, vio_msg_t *msg)
40150a55fbb7Slm66018 {
40160a55fbb7Slm66018 	int			status = 0;
40173af08d82Slm66018 	vio_dring_msg_t		*dring_msg;
4018d10e4ef2Snarayan 	vdc_local_desc_t	*ldep = NULL;
40193af08d82Slm66018 	int			start, end;
40203af08d82Slm66018 	int			idx;
40210a55fbb7Slm66018 
40223af08d82Slm66018 	dring_msg = (vio_dring_msg_t *)msg;
40230a55fbb7Slm66018 
40243af08d82Slm66018 	ASSERT(msg->tag.vio_msgtype == VIO_TYPE_DATA);
40253af08d82Slm66018 	ASSERT(vdcp != NULL);
40263af08d82Slm66018 
40273af08d82Slm66018 	mutex_enter(&vdcp->lock);
40280a55fbb7Slm66018 
40290a55fbb7Slm66018 	/*
40300a55fbb7Slm66018 	 * Check to see if the message has bogus data
40310a55fbb7Slm66018 	 */
4032e1ebb9ecSlm66018 	idx = start = dring_msg->start_idx;
40330a55fbb7Slm66018 	end = dring_msg->end_idx;
40343af08d82Slm66018 	if ((start >= vdcp->dring_len) ||
40353af08d82Slm66018 	    (end >= vdcp->dring_len) || (end < -1)) {
40363af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Bogus ACK data : start %d, end %d\n",
40373af08d82Slm66018 		    vdcp->instance, start, end);
40383af08d82Slm66018 		mutex_exit(&vdcp->lock);
4039e1ebb9ecSlm66018 		return (EINVAL);
40400a55fbb7Slm66018 	}
40410a55fbb7Slm66018 
40420a55fbb7Slm66018 	/*
40430a55fbb7Slm66018 	 * Verify that the sequence number is what vdc expects.
40440a55fbb7Slm66018 	 */
40453af08d82Slm66018 	switch (vdc_verify_seq_num(vdcp, dring_msg)) {
4046e1ebb9ecSlm66018 	case VDC_SEQ_NUM_TODO:
4047e1ebb9ecSlm66018 		break;	/* keep processing this message */
4048e1ebb9ecSlm66018 	case VDC_SEQ_NUM_SKIP:
40493af08d82Slm66018 		mutex_exit(&vdcp->lock);
4050e1ebb9ecSlm66018 		return (0);
4051e1ebb9ecSlm66018 	case VDC_SEQ_NUM_INVALID:
40523af08d82Slm66018 		mutex_exit(&vdcp->lock);
40533af08d82Slm66018 		DMSG(vdcp, 0, "[%d] invalid seqno\n", vdcp->instance);
40540a55fbb7Slm66018 		return (ENXIO);
40550a55fbb7Slm66018 	}
40560a55fbb7Slm66018 
40573af08d82Slm66018 	if (msg->tag.vio_subtype == VIO_SUBTYPE_NACK) {
40583af08d82Slm66018 		DMSG(vdcp, 0, "[%d] DATA NACK\n", vdcp->instance);
4059e1ebb9ecSlm66018 		VDC_DUMP_DRING_MSG(dring_msg);
40603af08d82Slm66018 		mutex_exit(&vdcp->lock);
4061e1ebb9ecSlm66018 		return (EIO);
40620a55fbb7Slm66018 
40633af08d82Slm66018 	} else if (msg->tag.vio_subtype == VIO_SUBTYPE_INFO) {
40643af08d82Slm66018 		mutex_exit(&vdcp->lock);
4065e1ebb9ecSlm66018 		return (EPROTO);
4066e1ebb9ecSlm66018 	}
4067e1ebb9ecSlm66018 
40683af08d82Slm66018 	DTRACE_IO2(recv, vio_dring_msg_t, dring_msg, vdc_t *, vdcp);
40693af08d82Slm66018 	DMSG(vdcp, 1, ": start %d end %d\n", start, end);
40703af08d82Slm66018 	ASSERT(start == end);
40713af08d82Slm66018 
40723af08d82Slm66018 	ldep = &vdcp->local_dring[idx];
40733af08d82Slm66018 
40743af08d82Slm66018 	DMSG(vdcp, 1, ": state 0x%x - cb_type 0x%x\n",
40753af08d82Slm66018 	    ldep->dep->hdr.dstate, ldep->cb_type);
40763af08d82Slm66018 
4077e1ebb9ecSlm66018 	if (ldep->dep->hdr.dstate == VIO_DESC_DONE) {
40783af08d82Slm66018 		struct buf *bufp;
4079e1ebb9ecSlm66018 
40803af08d82Slm66018 		switch (ldep->cb_type) {
40813af08d82Slm66018 		case CB_SYNC:
40823af08d82Slm66018 			ASSERT(vdcp->sync_op_pending);
4083d10e4ef2Snarayan 
40843af08d82Slm66018 			status = vdc_depopulate_descriptor(vdcp, idx);
40853af08d82Slm66018 			vdcp->sync_op_status = status;
40863af08d82Slm66018 			vdcp->sync_op_pending = B_FALSE;
40873af08d82Slm66018 			cv_signal(&vdcp->sync_pending_cv);
40883af08d82Slm66018 			break;
40894bac2208Snarayan 
40903af08d82Slm66018 		case CB_STRATEGY:
40913af08d82Slm66018 			bufp = ldep->cb_arg;
40923af08d82Slm66018 			ASSERT(bufp != NULL);
40933c96341aSnarayan 			bufp->b_resid =
40943c96341aSnarayan 			    bufp->b_bcount - ldep->dep->payload.nbytes;
40953af08d82Slm66018 			status = ldep->dep->payload.status; /* Future:ntoh */
40963af08d82Slm66018 			if (status != 0) {
40973af08d82Slm66018 				DMSG(vdcp, 1, "strategy status=%d\n", status);
40983af08d82Slm66018 				bioerror(bufp, status);
4099d10e4ef2Snarayan 			}
41003af08d82Slm66018 			status = vdc_depopulate_descriptor(vdcp, idx);
41013af08d82Slm66018 			biodone(bufp);
41023c96341aSnarayan 
41033c96341aSnarayan 			DMSG(vdcp, 1,
41043c96341aSnarayan 			    "strategy complete req=%ld bytes resp=%ld bytes\n",
41053c96341aSnarayan 			    bufp->b_bcount, ldep->dep->payload.nbytes);
41063af08d82Slm66018 			break;
41073af08d82Slm66018 
41083af08d82Slm66018 		default:
41093af08d82Slm66018 			ASSERT(0);
41100a55fbb7Slm66018 		}
41113af08d82Slm66018 	}
41123af08d82Slm66018 
41133af08d82Slm66018 	/* let the arrival signal propogate */
41143af08d82Slm66018 	mutex_exit(&vdcp->lock);
41150a55fbb7Slm66018 
4116e1ebb9ecSlm66018 	/* probe gives the count of how many entries were processed */
41173af08d82Slm66018 	DTRACE_IO2(processed, int, 1, vdc_t *, vdcp);
41180a55fbb7Slm66018 
41193af08d82Slm66018 	return (0);
41200a55fbb7Slm66018 }
41210a55fbb7Slm66018 
41220a55fbb7Slm66018 /*
41230a55fbb7Slm66018  * Function:
41240a55fbb7Slm66018  *	vdc_process_err_msg()
41250a55fbb7Slm66018  *
41260a55fbb7Slm66018  * NOTE: No error messages are used as part of the vDisk protocol
41270a55fbb7Slm66018  */
41280a55fbb7Slm66018 static int
41290a55fbb7Slm66018 vdc_process_err_msg(vdc_t *vdc, vio_msg_t msg)
41300a55fbb7Slm66018 {
41310a55fbb7Slm66018 	_NOTE(ARGUNUSED(vdc))
41320a55fbb7Slm66018 	_NOTE(ARGUNUSED(msg))
41330a55fbb7Slm66018 
41340a55fbb7Slm66018 	ASSERT(msg.tag.vio_msgtype == VIO_TYPE_ERR);
41353af08d82Slm66018 	DMSG(vdc, 1, "[%d] Got an ERR msg", vdc->instance);
41360a55fbb7Slm66018 
41370a55fbb7Slm66018 	return (ENOTSUP);
41380a55fbb7Slm66018 }
41390a55fbb7Slm66018 
41400a55fbb7Slm66018 /*
41410a55fbb7Slm66018  * Function:
41420a55fbb7Slm66018  *	vdc_handle_ver_msg()
41430a55fbb7Slm66018  *
41440a55fbb7Slm66018  * Description:
41450a55fbb7Slm66018  *
41460a55fbb7Slm66018  * Arguments:
41470a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
41480a55fbb7Slm66018  *	ver_msg	- LDC message sent by vDisk server
41490a55fbb7Slm66018  *
41500a55fbb7Slm66018  * Return Code:
41510a55fbb7Slm66018  *	0	- Success
41520a55fbb7Slm66018  */
41530a55fbb7Slm66018 static int
41540a55fbb7Slm66018 vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg)
41550a55fbb7Slm66018 {
41560a55fbb7Slm66018 	int status = 0;
41570a55fbb7Slm66018 
41580a55fbb7Slm66018 	ASSERT(vdc != NULL);
41590a55fbb7Slm66018 	ASSERT(mutex_owned(&vdc->lock));
41600a55fbb7Slm66018 
41610a55fbb7Slm66018 	if (ver_msg->tag.vio_subtype_env != VIO_VER_INFO) {
41620a55fbb7Slm66018 		return (EPROTO);
41630a55fbb7Slm66018 	}
41640a55fbb7Slm66018 
41650a55fbb7Slm66018 	if (ver_msg->dev_class != VDEV_DISK_SERVER) {
41660a55fbb7Slm66018 		return (EINVAL);
41670a55fbb7Slm66018 	}
41680a55fbb7Slm66018 
41690a55fbb7Slm66018 	switch (ver_msg->tag.vio_subtype) {
41700a55fbb7Slm66018 	case VIO_SUBTYPE_ACK:
41710a55fbb7Slm66018 		/*
41720a55fbb7Slm66018 		 * We check to see if the version returned is indeed supported
41730a55fbb7Slm66018 		 * (The server may have also adjusted the minor number downwards
41740a55fbb7Slm66018 		 * and if so 'ver_msg' will contain the actual version agreed)
41750a55fbb7Slm66018 		 */
41760a55fbb7Slm66018 		if (vdc_is_supported_version(ver_msg)) {
41770a55fbb7Slm66018 			vdc->ver.major = ver_msg->ver_major;
41780a55fbb7Slm66018 			vdc->ver.minor = ver_msg->ver_minor;
41790a55fbb7Slm66018 			ASSERT(vdc->ver.major > 0);
41800a55fbb7Slm66018 		} else {
41810a55fbb7Slm66018 			status = EPROTO;
41820a55fbb7Slm66018 		}
41830a55fbb7Slm66018 		break;
41840a55fbb7Slm66018 
41850a55fbb7Slm66018 	case VIO_SUBTYPE_NACK:
41860a55fbb7Slm66018 		/*
41870a55fbb7Slm66018 		 * call vdc_is_supported_version() which will return the next
41880a55fbb7Slm66018 		 * supported version (if any) in 'ver_msg'
41890a55fbb7Slm66018 		 */
41900a55fbb7Slm66018 		(void) vdc_is_supported_version(ver_msg);
41910a55fbb7Slm66018 		if (ver_msg->ver_major > 0) {
41920a55fbb7Slm66018 			size_t len = sizeof (*ver_msg);
41930a55fbb7Slm66018 
41940a55fbb7Slm66018 			ASSERT(vdc->ver.major > 0);
41950a55fbb7Slm66018 
41960a55fbb7Slm66018 			/* reset the necessary fields and resend */
41970a55fbb7Slm66018 			ver_msg->tag.vio_subtype = VIO_SUBTYPE_INFO;
41980a55fbb7Slm66018 			ver_msg->dev_class = VDEV_DISK;
41990a55fbb7Slm66018 
42000a55fbb7Slm66018 			status = vdc_send(vdc, (caddr_t)ver_msg, &len);
42013af08d82Slm66018 			DMSG(vdc, 0, "[%d] Resend VER info (LDC status = %d)\n",
42020a55fbb7Slm66018 			    vdc->instance, status);
42030a55fbb7Slm66018 			if (len != sizeof (*ver_msg))
42040a55fbb7Slm66018 				status = EBADMSG;
42050a55fbb7Slm66018 		} else {
420687a7269eSachartre 			DMSG(vdc, 0, "[%d] No common version with vDisk server",
420787a7269eSachartre 			    vdc->instance);
42080a55fbb7Slm66018 			status = ENOTSUP;
42090a55fbb7Slm66018 		}
42100a55fbb7Slm66018 
42110a55fbb7Slm66018 		break;
42121ae08745Sheppo 	case VIO_SUBTYPE_INFO:
42131ae08745Sheppo 		/*
42141ae08745Sheppo 		 * Handle the case where vds starts handshake
4215eff7243fSlm66018 		 * (for now only vdc is the instigator)
42161ae08745Sheppo 		 */
42171ae08745Sheppo 		status = ENOTSUP;
42181ae08745Sheppo 		break;
42191ae08745Sheppo 
42201ae08745Sheppo 	default:
42210a55fbb7Slm66018 		status = EINVAL;
42221ae08745Sheppo 		break;
42231ae08745Sheppo 	}
42241ae08745Sheppo 
42250a55fbb7Slm66018 	return (status);
42260a55fbb7Slm66018 }
42270a55fbb7Slm66018 
42280a55fbb7Slm66018 /*
42290a55fbb7Slm66018  * Function:
42300a55fbb7Slm66018  *	vdc_handle_attr_msg()
42310a55fbb7Slm66018  *
42320a55fbb7Slm66018  * Description:
42330a55fbb7Slm66018  *
42340a55fbb7Slm66018  * Arguments:
42350a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
42360a55fbb7Slm66018  *	attr_msg	- LDC message sent by vDisk server
42370a55fbb7Slm66018  *
42380a55fbb7Slm66018  * Return Code:
42390a55fbb7Slm66018  *	0	- Success
42400a55fbb7Slm66018  */
42410a55fbb7Slm66018 static int
42420a55fbb7Slm66018 vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg)
42430a55fbb7Slm66018 {
42440a55fbb7Slm66018 	int status = 0;
42450a55fbb7Slm66018 
42460a55fbb7Slm66018 	ASSERT(vdc != NULL);
42470a55fbb7Slm66018 	ASSERT(mutex_owned(&vdc->lock));
42480a55fbb7Slm66018 
42490a55fbb7Slm66018 	if (attr_msg->tag.vio_subtype_env != VIO_ATTR_INFO) {
42500a55fbb7Slm66018 		return (EPROTO);
42510a55fbb7Slm66018 	}
42520a55fbb7Slm66018 
42530a55fbb7Slm66018 	switch (attr_msg->tag.vio_subtype) {
42541ae08745Sheppo 	case VIO_SUBTYPE_ACK:
42551ae08745Sheppo 		/*
42561ae08745Sheppo 		 * We now verify the attributes sent by vds.
42571ae08745Sheppo 		 */
425878fcd0a1Sachartre 		if (attr_msg->vdisk_size == 0) {
425978fcd0a1Sachartre 			DMSG(vdc, 0, "[%d] Invalid disk size from vds",
426078fcd0a1Sachartre 			    vdc->instance);
426178fcd0a1Sachartre 			status = EINVAL;
426278fcd0a1Sachartre 			break;
426378fcd0a1Sachartre 		}
426478fcd0a1Sachartre 
426578fcd0a1Sachartre 		if (attr_msg->max_xfer_sz == 0) {
426678fcd0a1Sachartre 			DMSG(vdc, 0, "[%d] Invalid transfer size from vds",
426778fcd0a1Sachartre 			    vdc->instance);
426878fcd0a1Sachartre 			status = EINVAL;
426978fcd0a1Sachartre 			break;
427078fcd0a1Sachartre 		}
427178fcd0a1Sachartre 
427278fcd0a1Sachartre 		/*
427378fcd0a1Sachartre 		 * If the disk size is already set check that it hasn't changed.
427478fcd0a1Sachartre 		 */
427578fcd0a1Sachartre 		if ((vdc->vdisk_size != 0) &&
427678fcd0a1Sachartre 		    (vdc->vdisk_size != attr_msg->vdisk_size)) {
427778fcd0a1Sachartre 			DMSG(vdc, 0, "[%d] Different disk size from vds "
427878fcd0a1Sachartre 			    "(old=0x%lx - new=0x%lx", vdc->instance,
427978fcd0a1Sachartre 			    vdc->vdisk_size, attr_msg->vdisk_size)
428078fcd0a1Sachartre 			status = EINVAL;
428178fcd0a1Sachartre 			break;
428278fcd0a1Sachartre 		}
428378fcd0a1Sachartre 
42841ae08745Sheppo 		vdc->vdisk_size = attr_msg->vdisk_size;
42851ae08745Sheppo 		vdc->vdisk_type = attr_msg->vdisk_type;
42861ae08745Sheppo 
42873af08d82Slm66018 		DMSG(vdc, 0, "[%d] max_xfer_sz: sent %lx acked %lx\n",
4288e1ebb9ecSlm66018 		    vdc->instance, vdc->max_xfer_sz, attr_msg->max_xfer_sz);
42893af08d82Slm66018 		DMSG(vdc, 0, "[%d] vdisk_block_size: sent %lx acked %x\n",
4290e1ebb9ecSlm66018 		    vdc->instance, vdc->block_size,
4291e1ebb9ecSlm66018 		    attr_msg->vdisk_block_size);
4292e1ebb9ecSlm66018 
42931ae08745Sheppo 		/*
4294e1ebb9ecSlm66018 		 * We don't know at compile time what the vDisk server will
4295e1ebb9ecSlm66018 		 * think are good values but we apply an large (arbitrary)
4296e1ebb9ecSlm66018 		 * upper bound to prevent memory exhaustion in vdc if it was
4297e1ebb9ecSlm66018 		 * allocating a DRing based of huge values sent by the server.
4298e1ebb9ecSlm66018 		 * We probably will never exceed this except if the message
4299e1ebb9ecSlm66018 		 * was garbage.
43001ae08745Sheppo 		 */
4301e1ebb9ecSlm66018 		if ((attr_msg->max_xfer_sz * attr_msg->vdisk_block_size) <=
4302e1ebb9ecSlm66018 		    (PAGESIZE * DEV_BSIZE)) {
4303e1ebb9ecSlm66018 			vdc->max_xfer_sz = attr_msg->max_xfer_sz;
4304e1ebb9ecSlm66018 			vdc->block_size = attr_msg->vdisk_block_size;
4305e1ebb9ecSlm66018 		} else {
43063af08d82Slm66018 			DMSG(vdc, 0, "[%d] vds block transfer size too big;"
4307e1ebb9ecSlm66018 			    " using max supported by vdc", vdc->instance);
43081ae08745Sheppo 		}
43091ae08745Sheppo 
43101ae08745Sheppo 		if ((attr_msg->xfer_mode != VIO_DRING_MODE) ||
43111ae08745Sheppo 		    (attr_msg->vdisk_size > INT64_MAX) ||
43121ae08745Sheppo 		    (attr_msg->vdisk_type > VD_DISK_TYPE_DISK)) {
43133af08d82Slm66018 			DMSG(vdc, 0, "[%d] Invalid attributes from vds",
4314e1ebb9ecSlm66018 			    vdc->instance);
43151ae08745Sheppo 			status = EINVAL;
43161ae08745Sheppo 			break;
43171ae08745Sheppo 		}
43181ae08745Sheppo 
431978fcd0a1Sachartre 		/*
432078fcd0a1Sachartre 		 * Now that we have received all attributes we can create a
432178fcd0a1Sachartre 		 * fake geometry for the disk.
432278fcd0a1Sachartre 		 */
432378fcd0a1Sachartre 		vdc_create_fake_geometry(vdc);
43241ae08745Sheppo 		break;
43251ae08745Sheppo 
43261ae08745Sheppo 	case VIO_SUBTYPE_NACK:
43271ae08745Sheppo 		/*
43281ae08745Sheppo 		 * vds could not handle the attributes we sent so we
43291ae08745Sheppo 		 * stop negotiating.
43301ae08745Sheppo 		 */
43311ae08745Sheppo 		status = EPROTO;
43321ae08745Sheppo 		break;
43331ae08745Sheppo 
43341ae08745Sheppo 	case VIO_SUBTYPE_INFO:
43351ae08745Sheppo 		/*
43361ae08745Sheppo 		 * Handle the case where vds starts the handshake
43371ae08745Sheppo 		 * (for now; vdc is the only supported instigatior)
43381ae08745Sheppo 		 */
43391ae08745Sheppo 		status = ENOTSUP;
43401ae08745Sheppo 		break;
43411ae08745Sheppo 
43421ae08745Sheppo 	default:
43431ae08745Sheppo 		status = ENOTSUP;
43441ae08745Sheppo 		break;
43451ae08745Sheppo 	}
43461ae08745Sheppo 
43470a55fbb7Slm66018 	return (status);
43481ae08745Sheppo }
43491ae08745Sheppo 
43500a55fbb7Slm66018 /*
43510a55fbb7Slm66018  * Function:
43520a55fbb7Slm66018  *	vdc_handle_dring_reg_msg()
43530a55fbb7Slm66018  *
43540a55fbb7Slm66018  * Description:
43550a55fbb7Slm66018  *
43560a55fbb7Slm66018  * Arguments:
43570a55fbb7Slm66018  *	vdc		- soft state pointer for this instance of the driver.
43580a55fbb7Slm66018  *	dring_msg	- LDC message sent by vDisk server
43590a55fbb7Slm66018  *
43600a55fbb7Slm66018  * Return Code:
43610a55fbb7Slm66018  *	0	- Success
43620a55fbb7Slm66018  */
43630a55fbb7Slm66018 static int
43640a55fbb7Slm66018 vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *dring_msg)
43650a55fbb7Slm66018 {
43660a55fbb7Slm66018 	int		status = 0;
43671ae08745Sheppo 
43680a55fbb7Slm66018 	ASSERT(vdc != NULL);
43690a55fbb7Slm66018 	ASSERT(mutex_owned(&vdc->lock));
43700a55fbb7Slm66018 
43710a55fbb7Slm66018 	if (dring_msg->tag.vio_subtype_env != VIO_DRING_REG) {
43720a55fbb7Slm66018 		return (EPROTO);
43730a55fbb7Slm66018 	}
43740a55fbb7Slm66018 
43750a55fbb7Slm66018 	switch (dring_msg->tag.vio_subtype) {
43760a55fbb7Slm66018 	case VIO_SUBTYPE_ACK:
43771ae08745Sheppo 		/* save the received dring_ident */
43781ae08745Sheppo 		vdc->dring_ident = dring_msg->dring_ident;
43793af08d82Slm66018 		DMSG(vdc, 0, "[%d] Received dring ident=0x%lx\n",
4380e1ebb9ecSlm66018 		    vdc->instance, vdc->dring_ident);
43811ae08745Sheppo 		break;
43821ae08745Sheppo 
43831ae08745Sheppo 	case VIO_SUBTYPE_NACK:
43841ae08745Sheppo 		/*
43851ae08745Sheppo 		 * vds could not handle the DRing info we sent so we
43861ae08745Sheppo 		 * stop negotiating.
43871ae08745Sheppo 		 */
43883af08d82Slm66018 		DMSG(vdc, 0, "[%d] server could not register DRing\n",
43893af08d82Slm66018 		    vdc->instance);
43901ae08745Sheppo 		status = EPROTO;
43911ae08745Sheppo 		break;
43921ae08745Sheppo 
43931ae08745Sheppo 	case VIO_SUBTYPE_INFO:
43941ae08745Sheppo 		/*
43951ae08745Sheppo 		 * Handle the case where vds starts handshake
43961ae08745Sheppo 		 * (for now only vdc is the instigatior)
43971ae08745Sheppo 		 */
43981ae08745Sheppo 		status = ENOTSUP;
43991ae08745Sheppo 		break;
44001ae08745Sheppo 	default:
44011ae08745Sheppo 		status = ENOTSUP;
44021ae08745Sheppo 	}
44031ae08745Sheppo 
44041ae08745Sheppo 	return (status);
44051ae08745Sheppo }
44061ae08745Sheppo 
44071ae08745Sheppo /*
44081ae08745Sheppo  * Function:
44091ae08745Sheppo  *	vdc_verify_seq_num()
44101ae08745Sheppo  *
44111ae08745Sheppo  * Description:
4412e1ebb9ecSlm66018  *	This functions verifies that the sequence number sent back by the vDisk
4413e1ebb9ecSlm66018  *	server with the latest message is what is expected (i.e. it is greater
4414e1ebb9ecSlm66018  *	than the last seq num sent by the vDisk server and less than or equal
4415e1ebb9ecSlm66018  *	to the last seq num generated by vdc).
4416e1ebb9ecSlm66018  *
4417e1ebb9ecSlm66018  *	It then checks the request ID to see if any requests need processing
4418e1ebb9ecSlm66018  *	in the DRing.
44191ae08745Sheppo  *
44201ae08745Sheppo  * Arguments:
44211ae08745Sheppo  *	vdc		- soft state pointer for this instance of the driver.
44221ae08745Sheppo  *	dring_msg	- pointer to the LDC message sent by vds
44231ae08745Sheppo  *
44241ae08745Sheppo  * Return Code:
4425e1ebb9ecSlm66018  *	VDC_SEQ_NUM_TODO	- Message needs to be processed
4426e1ebb9ecSlm66018  *	VDC_SEQ_NUM_SKIP	- Message has already been processed
4427e1ebb9ecSlm66018  *	VDC_SEQ_NUM_INVALID	- The seq numbers are so out of sync,
4428e1ebb9ecSlm66018  *				  vdc cannot deal with them
44291ae08745Sheppo  */
4430e1ebb9ecSlm66018 static int
4431e1ebb9ecSlm66018 vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg)
44321ae08745Sheppo {
44331ae08745Sheppo 	ASSERT(vdc != NULL);
44341ae08745Sheppo 	ASSERT(dring_msg != NULL);
4435d10e4ef2Snarayan 	ASSERT(mutex_owned(&vdc->lock));
44361ae08745Sheppo 
44371ae08745Sheppo 	/*
44381ae08745Sheppo 	 * Check to see if the messages were responded to in the correct
4439e1ebb9ecSlm66018 	 * order by vds.
44401ae08745Sheppo 	 */
4441e1ebb9ecSlm66018 	if ((dring_msg->seq_num <= vdc->seq_num_reply) ||
4442e1ebb9ecSlm66018 	    (dring_msg->seq_num > vdc->seq_num)) {
44433af08d82Slm66018 		DMSG(vdc, 0, "?[%d] Bogus sequence_number %lu: "
4444e1ebb9ecSlm66018 		    "%lu > expected <= %lu (last proc req %lu sent %lu)\n",
4445e1ebb9ecSlm66018 		    vdc->instance, dring_msg->seq_num,
4446e1ebb9ecSlm66018 		    vdc->seq_num_reply, vdc->seq_num,
4447e1ebb9ecSlm66018 		    vdc->req_id_proc, vdc->req_id);
4448e1ebb9ecSlm66018 		return (VDC_SEQ_NUM_INVALID);
44491ae08745Sheppo 	}
4450e1ebb9ecSlm66018 	vdc->seq_num_reply = dring_msg->seq_num;
44511ae08745Sheppo 
4452e1ebb9ecSlm66018 	if (vdc->req_id_proc < vdc->req_id)
4453e1ebb9ecSlm66018 		return (VDC_SEQ_NUM_TODO);
4454e1ebb9ecSlm66018 	else
4455e1ebb9ecSlm66018 		return (VDC_SEQ_NUM_SKIP);
44561ae08745Sheppo }
44571ae08745Sheppo 
44580a55fbb7Slm66018 
44590a55fbb7Slm66018 /*
44600a55fbb7Slm66018  * Function:
44610a55fbb7Slm66018  *	vdc_is_supported_version()
44620a55fbb7Slm66018  *
44630a55fbb7Slm66018  * Description:
44640a55fbb7Slm66018  *	This routine checks if the major/minor version numbers specified in
44650a55fbb7Slm66018  *	'ver_msg' are supported. If not it finds the next version that is
44660a55fbb7Slm66018  *	in the supported version list 'vdc_version[]' and sets the fields in
44670a55fbb7Slm66018  *	'ver_msg' to those values
44680a55fbb7Slm66018  *
44690a55fbb7Slm66018  * Arguments:
44700a55fbb7Slm66018  *	ver_msg	- LDC message sent by vDisk server
44710a55fbb7Slm66018  *
44720a55fbb7Slm66018  * Return Code:
44730a55fbb7Slm66018  *	B_TRUE	- Success
44740a55fbb7Slm66018  *	B_FALSE	- Version not supported
44750a55fbb7Slm66018  */
44760a55fbb7Slm66018 static boolean_t
44770a55fbb7Slm66018 vdc_is_supported_version(vio_ver_msg_t *ver_msg)
44780a55fbb7Slm66018 {
44790a55fbb7Slm66018 	int vdc_num_versions = sizeof (vdc_version) / sizeof (vdc_version[0]);
44800a55fbb7Slm66018 
44810a55fbb7Slm66018 	for (int i = 0; i < vdc_num_versions; i++) {
44820a55fbb7Slm66018 		ASSERT(vdc_version[i].major > 0);
44830a55fbb7Slm66018 		ASSERT((i == 0) ||
44840a55fbb7Slm66018 		    (vdc_version[i].major < vdc_version[i-1].major));
44850a55fbb7Slm66018 
44860a55fbb7Slm66018 		/*
44870a55fbb7Slm66018 		 * If the major versions match, adjust the minor version, if
44880a55fbb7Slm66018 		 * necessary, down to the highest value supported by this
44890a55fbb7Slm66018 		 * client. The server should support all minor versions lower
44900a55fbb7Slm66018 		 * than the value it sent
44910a55fbb7Slm66018 		 */
44920a55fbb7Slm66018 		if (ver_msg->ver_major == vdc_version[i].major) {
44930a55fbb7Slm66018 			if (ver_msg->ver_minor > vdc_version[i].minor) {
44943af08d82Slm66018 				DMSGX(0,
44953af08d82Slm66018 				    "Adjusting minor version from %u to %u",
44960a55fbb7Slm66018 				    ver_msg->ver_minor, vdc_version[i].minor);
44970a55fbb7Slm66018 				ver_msg->ver_minor = vdc_version[i].minor;
44980a55fbb7Slm66018 			}
44990a55fbb7Slm66018 			return (B_TRUE);
45000a55fbb7Slm66018 		}
45010a55fbb7Slm66018 
45020a55fbb7Slm66018 		/*
45030a55fbb7Slm66018 		 * If the message contains a higher major version number, set
45040a55fbb7Slm66018 		 * the message's major/minor versions to the current values
45050a55fbb7Slm66018 		 * and return false, so this message will get resent with
45060a55fbb7Slm66018 		 * these values, and the server will potentially try again
45070a55fbb7Slm66018 		 * with the same or a lower version
45080a55fbb7Slm66018 		 */
45090a55fbb7Slm66018 		if (ver_msg->ver_major > vdc_version[i].major) {
45100a55fbb7Slm66018 			ver_msg->ver_major = vdc_version[i].major;
45110a55fbb7Slm66018 			ver_msg->ver_minor = vdc_version[i].minor;
45123af08d82Slm66018 			DMSGX(0, "Suggesting major/minor (0x%x/0x%x)\n",
45130a55fbb7Slm66018 			    ver_msg->ver_major, ver_msg->ver_minor);
45140a55fbb7Slm66018 
45150a55fbb7Slm66018 			return (B_FALSE);
45160a55fbb7Slm66018 		}
45170a55fbb7Slm66018 
45180a55fbb7Slm66018 		/*
45190a55fbb7Slm66018 		 * Otherwise, the message's major version is less than the
45200a55fbb7Slm66018 		 * current major version, so continue the loop to the next
45210a55fbb7Slm66018 		 * (lower) supported version
45220a55fbb7Slm66018 		 */
45230a55fbb7Slm66018 	}
45240a55fbb7Slm66018 
45250a55fbb7Slm66018 	/*
45260a55fbb7Slm66018 	 * No common version was found; "ground" the version pair in the
45270a55fbb7Slm66018 	 * message to terminate negotiation
45280a55fbb7Slm66018 	 */
45290a55fbb7Slm66018 	ver_msg->ver_major = 0;
45300a55fbb7Slm66018 	ver_msg->ver_minor = 0;
45310a55fbb7Slm66018 
45320a55fbb7Slm66018 	return (B_FALSE);
45330a55fbb7Slm66018 }
45341ae08745Sheppo /* -------------------------------------------------------------------------- */
45351ae08745Sheppo 
45361ae08745Sheppo /*
45371ae08745Sheppo  * DKIO(7) support
45381ae08745Sheppo  */
45391ae08745Sheppo 
45401ae08745Sheppo typedef struct vdc_dk_arg {
45411ae08745Sheppo 	struct dk_callback	dkc;
45421ae08745Sheppo 	int			mode;
45431ae08745Sheppo 	dev_t			dev;
45441ae08745Sheppo 	vdc_t			*vdc;
45451ae08745Sheppo } vdc_dk_arg_t;
45461ae08745Sheppo 
45471ae08745Sheppo /*
45481ae08745Sheppo  * Function:
45491ae08745Sheppo  * 	vdc_dkio_flush_cb()
45501ae08745Sheppo  *
45511ae08745Sheppo  * Description:
45521ae08745Sheppo  *	This routine is a callback for DKIOCFLUSHWRITECACHE which can be called
45531ae08745Sheppo  *	by kernel code.
45541ae08745Sheppo  *
45551ae08745Sheppo  * Arguments:
45561ae08745Sheppo  *	arg	- a pointer to a vdc_dk_arg_t structure.
45571ae08745Sheppo  */
45581ae08745Sheppo void
45591ae08745Sheppo vdc_dkio_flush_cb(void *arg)
45601ae08745Sheppo {
45611ae08745Sheppo 	struct vdc_dk_arg	*dk_arg = (struct vdc_dk_arg *)arg;
45621ae08745Sheppo 	struct dk_callback	*dkc = NULL;
45631ae08745Sheppo 	vdc_t			*vdc = NULL;
45641ae08745Sheppo 	int			rv;
45651ae08745Sheppo 
45661ae08745Sheppo 	if (dk_arg == NULL) {
45673af08d82Slm66018 		cmn_err(CE_NOTE, "?[Unk] DKIOCFLUSHWRITECACHE arg is NULL\n");
45681ae08745Sheppo 		return;
45691ae08745Sheppo 	}
45701ae08745Sheppo 	dkc = &dk_arg->dkc;
45711ae08745Sheppo 	vdc = dk_arg->vdc;
45721ae08745Sheppo 	ASSERT(vdc != NULL);
45731ae08745Sheppo 
45743af08d82Slm66018 	rv = vdc_do_sync_op(vdc, VD_OP_FLUSH, NULL, 0,
45750d0c8d4bSnarayan 	    VDCPART(dk_arg->dev), 0, CB_SYNC, 0, VIO_both_dir);
45761ae08745Sheppo 	if (rv != 0) {
45773af08d82Slm66018 		DMSG(vdc, 0, "[%d] DKIOCFLUSHWRITECACHE failed %d : model %x\n",
4578e1ebb9ecSlm66018 		    vdc->instance, rv,
45791ae08745Sheppo 		    ddi_model_convert_from(dk_arg->mode & FMODELS));
45801ae08745Sheppo 	}
45811ae08745Sheppo 
45821ae08745Sheppo 	/*
45831ae08745Sheppo 	 * Trigger the call back to notify the caller the the ioctl call has
45841ae08745Sheppo 	 * been completed.
45851ae08745Sheppo 	 */
45861ae08745Sheppo 	if ((dk_arg->mode & FKIOCTL) &&
45871ae08745Sheppo 	    (dkc != NULL) &&
45881ae08745Sheppo 	    (dkc->dkc_callback != NULL)) {
45891ae08745Sheppo 		ASSERT(dkc->dkc_cookie != NULL);
45908e6a2a04Slm66018 		(*dkc->dkc_callback)(dkc->dkc_cookie, rv);
45911ae08745Sheppo 	}
45921ae08745Sheppo 
45931ae08745Sheppo 	/* Indicate that one less DKIO write flush is outstanding */
45941ae08745Sheppo 	mutex_enter(&vdc->lock);
45951ae08745Sheppo 	vdc->dkio_flush_pending--;
45961ae08745Sheppo 	ASSERT(vdc->dkio_flush_pending >= 0);
45971ae08745Sheppo 	mutex_exit(&vdc->lock);
45988e6a2a04Slm66018 
45998e6a2a04Slm66018 	/* free the mem that was allocated when the callback was dispatched */
46008e6a2a04Slm66018 	kmem_free(arg, sizeof (vdc_dk_arg_t));
46011ae08745Sheppo }
46021ae08745Sheppo 
46031ae08745Sheppo /*
460487a7269eSachartre  * Function:
460587a7269eSachartre  * 	vdc_dkio_get_partition()
460687a7269eSachartre  *
460787a7269eSachartre  * Description:
460887a7269eSachartre  *	This function implements the DKIOCGAPART ioctl.
460987a7269eSachartre  *
461087a7269eSachartre  * Arguments:
461178fcd0a1Sachartre  *	vdc	- soft state pointer
461287a7269eSachartre  *	arg	- a pointer to a dk_map[NDKMAP] or dk_map32[NDKMAP] structure
461387a7269eSachartre  *	flag	- ioctl flags
461487a7269eSachartre  */
461587a7269eSachartre static int
461678fcd0a1Sachartre vdc_dkio_get_partition(vdc_t *vdc, caddr_t arg, int flag)
461787a7269eSachartre {
461878fcd0a1Sachartre 	struct dk_geom *geom;
461978fcd0a1Sachartre 	struct vtoc *vtoc;
462087a7269eSachartre 	union {
462187a7269eSachartre 		struct dk_map map[NDKMAP];
462287a7269eSachartre 		struct dk_map32 map32[NDKMAP];
462387a7269eSachartre 	} data;
462487a7269eSachartre 	int i, rv, size;
462587a7269eSachartre 
462678fcd0a1Sachartre 	mutex_enter(&vdc->lock);
462787a7269eSachartre 
462878fcd0a1Sachartre 	if ((rv = vdc_validate_geometry(vdc)) != 0) {
462978fcd0a1Sachartre 		mutex_exit(&vdc->lock);
463087a7269eSachartre 		return (rv);
463178fcd0a1Sachartre 	}
463287a7269eSachartre 
463378fcd0a1Sachartre 	vtoc = vdc->vtoc;
463478fcd0a1Sachartre 	geom = vdc->geom;
463587a7269eSachartre 
463687a7269eSachartre 	if (ddi_model_convert_from(flag & FMODELS) == DDI_MODEL_ILP32) {
463787a7269eSachartre 
463878fcd0a1Sachartre 		for (i = 0; i < vtoc->v_nparts; i++) {
463978fcd0a1Sachartre 			data.map32[i].dkl_cylno = vtoc->v_part[i].p_start /
464078fcd0a1Sachartre 			    (geom->dkg_nhead * geom->dkg_nsect);
464178fcd0a1Sachartre 			data.map32[i].dkl_nblk = vtoc->v_part[i].p_size;
464287a7269eSachartre 		}
464387a7269eSachartre 		size = NDKMAP * sizeof (struct dk_map32);
464487a7269eSachartre 
464587a7269eSachartre 	} else {
464687a7269eSachartre 
464778fcd0a1Sachartre 		for (i = 0; i < vtoc->v_nparts; i++) {
464878fcd0a1Sachartre 			data.map[i].dkl_cylno = vtoc->v_part[i].p_start /
464978fcd0a1Sachartre 			    (geom->dkg_nhead * geom->dkg_nsect);
465078fcd0a1Sachartre 			data.map[i].dkl_nblk = vtoc->v_part[i].p_size;
465187a7269eSachartre 		}
465287a7269eSachartre 		size = NDKMAP * sizeof (struct dk_map);
465387a7269eSachartre 
465487a7269eSachartre 	}
465587a7269eSachartre 
465678fcd0a1Sachartre 	mutex_exit(&vdc->lock);
465778fcd0a1Sachartre 
465887a7269eSachartre 	if (ddi_copyout(&data, arg, size, flag) != 0)
465987a7269eSachartre 		return (EFAULT);
466087a7269eSachartre 
466187a7269eSachartre 	return (0);
466287a7269eSachartre }
466387a7269eSachartre 
466487a7269eSachartre /*
466587a7269eSachartre  * Function:
466687a7269eSachartre  * 	vdc_dioctl_rwcmd()
466787a7269eSachartre  *
466887a7269eSachartre  * Description:
466987a7269eSachartre  *	This function implements the DIOCTL_RWCMD ioctl. This ioctl is used
467087a7269eSachartre  *	for DKC_DIRECT disks to read or write at an absolute disk offset.
467187a7269eSachartre  *
467287a7269eSachartre  * Arguments:
467387a7269eSachartre  *	dev	- device
467487a7269eSachartre  *	arg	- a pointer to a dadkio_rwcmd or dadkio_rwcmd32 structure
467587a7269eSachartre  *	flag	- ioctl flags
467687a7269eSachartre  */
467787a7269eSachartre static int
467887a7269eSachartre vdc_dioctl_rwcmd(dev_t dev, caddr_t arg, int flag)
467987a7269eSachartre {
468087a7269eSachartre 	struct dadkio_rwcmd32 rwcmd32;
468187a7269eSachartre 	struct dadkio_rwcmd rwcmd;
468287a7269eSachartre 	struct iovec aiov;
468387a7269eSachartre 	struct uio auio;
468487a7269eSachartre 	int rw, status;
468587a7269eSachartre 	struct buf *buf;
468687a7269eSachartre 
468787a7269eSachartre 	if (ddi_model_convert_from(flag & FMODELS) == DDI_MODEL_ILP32) {
468887a7269eSachartre 		if (ddi_copyin((caddr_t)arg, (caddr_t)&rwcmd32,
468987a7269eSachartre 		    sizeof (struct dadkio_rwcmd32), flag)) {
469087a7269eSachartre 			return (EFAULT);
469187a7269eSachartre 		}
469287a7269eSachartre 		rwcmd.cmd = rwcmd32.cmd;
469387a7269eSachartre 		rwcmd.flags = rwcmd32.flags;
469487a7269eSachartre 		rwcmd.blkaddr = (daddr_t)rwcmd32.blkaddr;
469587a7269eSachartre 		rwcmd.buflen = rwcmd32.buflen;
469687a7269eSachartre 		rwcmd.bufaddr = (caddr_t)(uintptr_t)rwcmd32.bufaddr;
469787a7269eSachartre 	} else {
469887a7269eSachartre 		if (ddi_copyin((caddr_t)arg, (caddr_t)&rwcmd,
469987a7269eSachartre 		    sizeof (struct dadkio_rwcmd), flag)) {
470087a7269eSachartre 			return (EFAULT);
470187a7269eSachartre 		}
470287a7269eSachartre 	}
470387a7269eSachartre 
470487a7269eSachartre 	switch (rwcmd.cmd) {
470587a7269eSachartre 	case DADKIO_RWCMD_READ:
470687a7269eSachartre 		rw = B_READ;
470787a7269eSachartre 		break;
470887a7269eSachartre 	case DADKIO_RWCMD_WRITE:
470987a7269eSachartre 		rw = B_WRITE;
471087a7269eSachartre 		break;
471187a7269eSachartre 	default:
471287a7269eSachartre 		return (EINVAL);
471387a7269eSachartre 	}
471487a7269eSachartre 
471587a7269eSachartre 	bzero((caddr_t)&aiov, sizeof (struct iovec));
471687a7269eSachartre 	aiov.iov_base   = rwcmd.bufaddr;
471787a7269eSachartre 	aiov.iov_len    = rwcmd.buflen;
471887a7269eSachartre 
471987a7269eSachartre 	bzero((caddr_t)&auio, sizeof (struct uio));
472087a7269eSachartre 	auio.uio_iov    = &aiov;
472187a7269eSachartre 	auio.uio_iovcnt = 1;
472287a7269eSachartre 	auio.uio_loffset = rwcmd.blkaddr * DEV_BSIZE;
472387a7269eSachartre 	auio.uio_resid  = rwcmd.buflen;
472487a7269eSachartre 	auio.uio_segflg = flag & FKIOCTL ? UIO_SYSSPACE : UIO_USERSPACE;
472587a7269eSachartre 
472687a7269eSachartre 	buf = kmem_alloc(sizeof (buf_t), KM_SLEEP);
472787a7269eSachartre 	bioinit(buf);
472887a7269eSachartre 	/*
472987a7269eSachartre 	 * We use the private field of buf to specify that this is an
473087a7269eSachartre 	 * I/O using an absolute offset.
473187a7269eSachartre 	 */
473287a7269eSachartre 	buf->b_private = (void *)VD_SLICE_NONE;
473387a7269eSachartre 
473487a7269eSachartre 	status = physio(vdc_strategy, buf, dev, rw, vdc_min, &auio);
473587a7269eSachartre 
473687a7269eSachartre 	biofini(buf);
473787a7269eSachartre 	kmem_free(buf, sizeof (buf_t));
473887a7269eSachartre 
473987a7269eSachartre 	return (status);
474087a7269eSachartre }
474187a7269eSachartre 
474287a7269eSachartre /*
47431ae08745Sheppo  * This structure is used in the DKIO(7I) array below.
47441ae08745Sheppo  */
47451ae08745Sheppo typedef struct vdc_dk_ioctl {
47461ae08745Sheppo 	uint8_t		op;		/* VD_OP_XXX value */
47471ae08745Sheppo 	int		cmd;		/* Solaris ioctl operation number */
47481ae08745Sheppo 	size_t		nbytes;		/* size of structure to be copied */
47490a55fbb7Slm66018 
47500a55fbb7Slm66018 	/* function to convert between vDisk and Solaris structure formats */
4751d10e4ef2Snarayan 	int	(*convert)(vdc_t *vdc, void *vd_buf, void *ioctl_arg,
4752d10e4ef2Snarayan 	    int mode, int dir);
47531ae08745Sheppo } vdc_dk_ioctl_t;
47541ae08745Sheppo 
47551ae08745Sheppo /*
47561ae08745Sheppo  * Subset of DKIO(7I) operations currently supported
47571ae08745Sheppo  */
47581ae08745Sheppo static vdc_dk_ioctl_t	dk_ioctl[] = {
4759eff7243fSlm66018 	{VD_OP_FLUSH,		DKIOCFLUSHWRITECACHE,	0,
47600a55fbb7Slm66018 		vdc_null_copy_func},
47610a55fbb7Slm66018 	{VD_OP_GET_WCE,		DKIOCGETWCE,		sizeof (int),
47624bac2208Snarayan 		vdc_get_wce_convert},
47630a55fbb7Slm66018 	{VD_OP_SET_WCE,		DKIOCSETWCE,		sizeof (int),
47644bac2208Snarayan 		vdc_set_wce_convert},
47650a55fbb7Slm66018 	{VD_OP_GET_VTOC,	DKIOCGVTOC,		sizeof (vd_vtoc_t),
47660a55fbb7Slm66018 		vdc_get_vtoc_convert},
47670a55fbb7Slm66018 	{VD_OP_SET_VTOC,	DKIOCSVTOC,		sizeof (vd_vtoc_t),
47680a55fbb7Slm66018 		vdc_set_vtoc_convert},
47690a55fbb7Slm66018 	{VD_OP_GET_DISKGEOM,	DKIOCGGEOM,		sizeof (vd_geom_t),
47700a55fbb7Slm66018 		vdc_get_geom_convert},
47710a55fbb7Slm66018 	{VD_OP_GET_DISKGEOM,	DKIOCG_PHYGEOM,		sizeof (vd_geom_t),
47720a55fbb7Slm66018 		vdc_get_geom_convert},
47730a55fbb7Slm66018 	{VD_OP_GET_DISKGEOM, 	DKIOCG_VIRTGEOM,	sizeof (vd_geom_t),
47740a55fbb7Slm66018 		vdc_get_geom_convert},
47750a55fbb7Slm66018 	{VD_OP_SET_DISKGEOM,	DKIOCSGEOM,		sizeof (vd_geom_t),
47760a55fbb7Slm66018 		vdc_set_geom_convert},
47774bac2208Snarayan 	{VD_OP_GET_EFI,		DKIOCGETEFI,		0,
47784bac2208Snarayan 		vdc_get_efi_convert},
47794bac2208Snarayan 	{VD_OP_SET_EFI,		DKIOCSETEFI,		0,
47804bac2208Snarayan 		vdc_set_efi_convert},
47810a55fbb7Slm66018 
478287a7269eSachartre 	/* DIOCTL_RWCMD is converted to a read or a write */
478387a7269eSachartre 	{0, DIOCTL_RWCMD,  sizeof (struct dadkio_rwcmd), NULL},
478487a7269eSachartre 
47850a55fbb7Slm66018 	/*
47860a55fbb7Slm66018 	 * These particular ioctls are not sent to the server - vdc fakes up
47870a55fbb7Slm66018 	 * the necessary info.
47880a55fbb7Slm66018 	 */
47890a55fbb7Slm66018 	{0, DKIOCINFO, sizeof (struct dk_cinfo), vdc_null_copy_func},
47900a55fbb7Slm66018 	{0, DKIOCGMEDIAINFO, sizeof (struct dk_minfo), vdc_null_copy_func},
47910a55fbb7Slm66018 	{0, USCSICMD,	sizeof (struct uscsi_cmd), vdc_null_copy_func},
479287a7269eSachartre 	{0, DKIOCGAPART, 0, vdc_null_copy_func },
47930a55fbb7Slm66018 	{0, DKIOCREMOVABLE, 0, vdc_null_copy_func},
47940a55fbb7Slm66018 	{0, CDROMREADOFFSET, 0, vdc_null_copy_func}
47951ae08745Sheppo };
47961ae08745Sheppo 
47971ae08745Sheppo /*
47981ae08745Sheppo  * Function:
47991ae08745Sheppo  *	vd_process_ioctl()
48001ae08745Sheppo  *
48011ae08745Sheppo  * Description:
48020a55fbb7Slm66018  *	This routine processes disk specific ioctl calls
48031ae08745Sheppo  *
48041ae08745Sheppo  * Arguments:
48051ae08745Sheppo  *	dev	- the device number
48061ae08745Sheppo  *	cmd	- the operation [dkio(7I)] to be processed
48071ae08745Sheppo  *	arg	- pointer to user provided structure
48081ae08745Sheppo  *		  (contains data to be set or reference parameter for get)
48091ae08745Sheppo  *	mode	- bit flag, indicating open settings, 32/64 bit type, etc
48101ae08745Sheppo  *
48111ae08745Sheppo  * Return Code:
48121ae08745Sheppo  *	0
48131ae08745Sheppo  *	EFAULT
48141ae08745Sheppo  *	ENXIO
48151ae08745Sheppo  *	EIO
48161ae08745Sheppo  *	ENOTSUP
48171ae08745Sheppo  */
48181ae08745Sheppo static int
48191ae08745Sheppo vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode)
48201ae08745Sheppo {
48210d0c8d4bSnarayan 	int		instance = VDCUNIT(dev);
48221ae08745Sheppo 	vdc_t		*vdc = NULL;
48231ae08745Sheppo 	int		rv = -1;
48241ae08745Sheppo 	int		idx = 0;		/* index into dk_ioctl[] */
48251ae08745Sheppo 	size_t		len = 0;		/* #bytes to send to vds */
48261ae08745Sheppo 	size_t		alloc_len = 0;		/* #bytes to allocate mem for */
48271ae08745Sheppo 	caddr_t		mem_p = NULL;
48281ae08745Sheppo 	size_t		nioctls = (sizeof (dk_ioctl)) / (sizeof (dk_ioctl[0]));
48293af08d82Slm66018 	vdc_dk_ioctl_t	*iop;
48301ae08745Sheppo 
48311ae08745Sheppo 	vdc = ddi_get_soft_state(vdc_state, instance);
48321ae08745Sheppo 	if (vdc == NULL) {
48331ae08745Sheppo 		cmn_err(CE_NOTE, "![%d] Could not get soft state structure",
48341ae08745Sheppo 		    instance);
48351ae08745Sheppo 		return (ENXIO);
48361ae08745Sheppo 	}
48371ae08745Sheppo 
48383af08d82Slm66018 	DMSG(vdc, 0, "[%d] Processing ioctl(%x) for dev %lx : model %x\n",
48393af08d82Slm66018 	    instance, cmd, dev, ddi_model_convert_from(mode & FMODELS));
48401ae08745Sheppo 
48411ae08745Sheppo 	/*
48421ae08745Sheppo 	 * Validate the ioctl operation to be performed.
48431ae08745Sheppo 	 *
48441ae08745Sheppo 	 * If we have looped through the array without finding a match then we
48451ae08745Sheppo 	 * don't support this ioctl.
48461ae08745Sheppo 	 */
48471ae08745Sheppo 	for (idx = 0; idx < nioctls; idx++) {
48481ae08745Sheppo 		if (cmd == dk_ioctl[idx].cmd)
48491ae08745Sheppo 			break;
48501ae08745Sheppo 	}
48511ae08745Sheppo 
48521ae08745Sheppo 	if (idx >= nioctls) {
48533af08d82Slm66018 		DMSG(vdc, 0, "[%d] Unsupported ioctl (0x%x)\n",
4854e1ebb9ecSlm66018 		    vdc->instance, cmd);
48551ae08745Sheppo 		return (ENOTSUP);
48561ae08745Sheppo 	}
48571ae08745Sheppo 
48583af08d82Slm66018 	iop = &(dk_ioctl[idx]);
48593af08d82Slm66018 
48604bac2208Snarayan 	if (cmd == DKIOCGETEFI || cmd == DKIOCSETEFI) {
48614bac2208Snarayan 		/* size is not fixed for EFI ioctls, it depends on ioctl arg */
48624bac2208Snarayan 		dk_efi_t	dk_efi;
48634bac2208Snarayan 
48644bac2208Snarayan 		rv = ddi_copyin(arg, &dk_efi, sizeof (dk_efi_t), mode);
48654bac2208Snarayan 		if (rv != 0)
48664bac2208Snarayan 			return (EFAULT);
48674bac2208Snarayan 
48684bac2208Snarayan 		len = sizeof (vd_efi_t) - 1 + dk_efi.dki_length;
48694bac2208Snarayan 	} else {
48703af08d82Slm66018 		len = iop->nbytes;
48714bac2208Snarayan 	}
48721ae08745Sheppo 
48731ae08745Sheppo 	/*
48740a55fbb7Slm66018 	 * Deal with the ioctls which the server does not provide. vdc can
48750a55fbb7Slm66018 	 * fake these up and return immediately
48761ae08745Sheppo 	 */
48771ae08745Sheppo 	switch (cmd) {
48781ae08745Sheppo 	case CDROMREADOFFSET:
48791ae08745Sheppo 	case DKIOCREMOVABLE:
48800a55fbb7Slm66018 	case USCSICMD:
48811ae08745Sheppo 		return (ENOTTY);
48821ae08745Sheppo 
488387a7269eSachartre 	case DIOCTL_RWCMD:
488487a7269eSachartre 		{
488578fcd0a1Sachartre 			if (vdc->cinfo == NULL)
488678fcd0a1Sachartre 				return (ENXIO);
488778fcd0a1Sachartre 
488887a7269eSachartre 			if (vdc->cinfo->dki_ctype != DKC_DIRECT)
488987a7269eSachartre 				return (ENOTTY);
489087a7269eSachartre 
489187a7269eSachartre 			return (vdc_dioctl_rwcmd(dev, arg, mode));
489287a7269eSachartre 		}
489387a7269eSachartre 
489487a7269eSachartre 	case DKIOCGAPART:
489587a7269eSachartre 		{
489678fcd0a1Sachartre 			return (vdc_dkio_get_partition(vdc, arg, mode));
489787a7269eSachartre 		}
489887a7269eSachartre 
48991ae08745Sheppo 	case DKIOCINFO:
49001ae08745Sheppo 		{
49011ae08745Sheppo 			struct dk_cinfo	cinfo;
49021ae08745Sheppo 			if (vdc->cinfo == NULL)
49031ae08745Sheppo 				return (ENXIO);
49041ae08745Sheppo 
49051ae08745Sheppo 			bcopy(vdc->cinfo, &cinfo, sizeof (struct dk_cinfo));
49060d0c8d4bSnarayan 			cinfo.dki_partition = VDCPART(dev);
49071ae08745Sheppo 
49081ae08745Sheppo 			rv = ddi_copyout(&cinfo, (void *)arg,
49091ae08745Sheppo 			    sizeof (struct dk_cinfo), mode);
49101ae08745Sheppo 			if (rv != 0)
49111ae08745Sheppo 				return (EFAULT);
49121ae08745Sheppo 
49131ae08745Sheppo 			return (0);
49141ae08745Sheppo 		}
49151ae08745Sheppo 
49161ae08745Sheppo 	case DKIOCGMEDIAINFO:
49178e6a2a04Slm66018 		{
49181ae08745Sheppo 			if (vdc->minfo == NULL)
49191ae08745Sheppo 				return (ENXIO);
49201ae08745Sheppo 
49211ae08745Sheppo 			rv = ddi_copyout(vdc->minfo, (void *)arg,
49221ae08745Sheppo 			    sizeof (struct dk_minfo), mode);
49231ae08745Sheppo 			if (rv != 0)
49241ae08745Sheppo 				return (EFAULT);
49251ae08745Sheppo 
49261ae08745Sheppo 			return (0);
49271ae08745Sheppo 		}
49281ae08745Sheppo 
49298e6a2a04Slm66018 	case DKIOCFLUSHWRITECACHE:
49308e6a2a04Slm66018 		{
49318e6a2a04Slm66018 			struct dk_callback *dkc = (struct dk_callback *)arg;
49328e6a2a04Slm66018 			vdc_dk_arg_t	*dkarg = NULL;
49338e6a2a04Slm66018 
49343af08d82Slm66018 			DMSG(vdc, 1, "[%d] Flush W$: mode %x\n",
49353af08d82Slm66018 			    instance, mode);
49368e6a2a04Slm66018 
49378e6a2a04Slm66018 			/*
49388e6a2a04Slm66018 			 * If arg is NULL, then there is no callback function
49398e6a2a04Slm66018 			 * registered and the call operates synchronously; we
49408e6a2a04Slm66018 			 * break and continue with the rest of the function and
49418e6a2a04Slm66018 			 * wait for vds to return (i.e. after the request to
49428e6a2a04Slm66018 			 * vds returns successfully, all writes completed prior
49438e6a2a04Slm66018 			 * to the ioctl will have been flushed from the disk
49448e6a2a04Slm66018 			 * write cache to persistent media.
49458e6a2a04Slm66018 			 *
49468e6a2a04Slm66018 			 * If a callback function is registered, we dispatch
49478e6a2a04Slm66018 			 * the request on a task queue and return immediately.
49488e6a2a04Slm66018 			 * The callback will deal with informing the calling
49498e6a2a04Slm66018 			 * thread that the flush request is completed.
49508e6a2a04Slm66018 			 */
49518e6a2a04Slm66018 			if (dkc == NULL)
49528e6a2a04Slm66018 				break;
49538e6a2a04Slm66018 
4954eff7243fSlm66018 			/*
4955eff7243fSlm66018 			 * the asynchronous callback is only supported if
4956eff7243fSlm66018 			 * invoked from within the kernel
4957eff7243fSlm66018 			 */
4958eff7243fSlm66018 			if ((mode & FKIOCTL) == 0)
4959eff7243fSlm66018 				return (ENOTSUP);
4960eff7243fSlm66018 
49618e6a2a04Slm66018 			dkarg = kmem_zalloc(sizeof (vdc_dk_arg_t), KM_SLEEP);
49628e6a2a04Slm66018 
49638e6a2a04Slm66018 			dkarg->mode = mode;
49648e6a2a04Slm66018 			dkarg->dev = dev;
49658e6a2a04Slm66018 			bcopy(dkc, &dkarg->dkc, sizeof (*dkc));
49668e6a2a04Slm66018 
49678e6a2a04Slm66018 			mutex_enter(&vdc->lock);
49688e6a2a04Slm66018 			vdc->dkio_flush_pending++;
49698e6a2a04Slm66018 			dkarg->vdc = vdc;
49708e6a2a04Slm66018 			mutex_exit(&vdc->lock);
49718e6a2a04Slm66018 
49728e6a2a04Slm66018 			/* put the request on a task queue */
49738e6a2a04Slm66018 			rv = taskq_dispatch(system_taskq, vdc_dkio_flush_cb,
49748e6a2a04Slm66018 			    (void *)dkarg, DDI_SLEEP);
49753af08d82Slm66018 			if (rv == NULL) {
49763af08d82Slm66018 				/* clean up if dispatch fails */
49773af08d82Slm66018 				mutex_enter(&vdc->lock);
49783af08d82Slm66018 				vdc->dkio_flush_pending--;
497978fcd0a1Sachartre 				mutex_exit(&vdc->lock);
49803af08d82Slm66018 				kmem_free(dkarg, sizeof (vdc_dk_arg_t));
49813af08d82Slm66018 			}
49828e6a2a04Slm66018 
49838e6a2a04Slm66018 			return (rv == NULL ? ENOMEM : 0);
49848e6a2a04Slm66018 		}
49858e6a2a04Slm66018 	}
49868e6a2a04Slm66018 
49871ae08745Sheppo 	/* catch programming error in vdc - should be a VD_OP_XXX ioctl */
49883af08d82Slm66018 	ASSERT(iop->op != 0);
49891ae08745Sheppo 
49901ae08745Sheppo 	/* LDC requires that the memory being mapped is 8-byte aligned */
49911ae08745Sheppo 	alloc_len = P2ROUNDUP(len, sizeof (uint64_t));
49923af08d82Slm66018 	DMSG(vdc, 1, "[%d] struct size %ld alloc %ld\n",
49933af08d82Slm66018 	    instance, len, alloc_len);
49941ae08745Sheppo 
4995eff7243fSlm66018 	ASSERT(alloc_len >= 0); /* sanity check */
4996eff7243fSlm66018 	if (alloc_len > 0)
49971ae08745Sheppo 		mem_p = kmem_zalloc(alloc_len, KM_SLEEP);
49981ae08745Sheppo 
49990a55fbb7Slm66018 	/*
5000eff7243fSlm66018 	 * Call the conversion function for this ioctl which, if necessary,
50010a55fbb7Slm66018 	 * converts from the Solaris format to the format ARC'ed
50020a55fbb7Slm66018 	 * as part of the vDisk protocol (FWARC 2006/195)
50030a55fbb7Slm66018 	 */
50043af08d82Slm66018 	ASSERT(iop->convert != NULL);
50053af08d82Slm66018 	rv = (iop->convert)(vdc, arg, mem_p, mode, VD_COPYIN);
50061ae08745Sheppo 	if (rv != 0) {
50073af08d82Slm66018 		DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n",
5008e1ebb9ecSlm66018 		    instance, rv, cmd);
50091ae08745Sheppo 		if (mem_p != NULL)
50101ae08745Sheppo 			kmem_free(mem_p, alloc_len);
50110a55fbb7Slm66018 		return (rv);
50121ae08745Sheppo 	}
50131ae08745Sheppo 
50141ae08745Sheppo 	/*
50151ae08745Sheppo 	 * send request to vds to service the ioctl.
50161ae08745Sheppo 	 */
50173af08d82Slm66018 	rv = vdc_do_sync_op(vdc, iop->op, mem_p, alloc_len,
50180d0c8d4bSnarayan 	    VDCPART(dev), 0, CB_SYNC, (void *)(uint64_t)mode,
50193af08d82Slm66018 	    VIO_both_dir);
50203af08d82Slm66018 
502178fcd0a1Sachartre 	if (cmd == DKIOCSVTOC || cmd == DKIOCSETEFI) {
502278fcd0a1Sachartre 		/*
502378fcd0a1Sachartre 		 * The disk label may have changed. Revalidate the disk
502478fcd0a1Sachartre 		 * geometry. This will also update the device nodes and
502578fcd0a1Sachartre 		 * properties.
502678fcd0a1Sachartre 		 */
502778fcd0a1Sachartre 		vdc_validate(vdc);
502878fcd0a1Sachartre 	}
502978fcd0a1Sachartre 
50301ae08745Sheppo 	if (rv != 0) {
50311ae08745Sheppo 		/*
50321ae08745Sheppo 		 * This is not necessarily an error. The ioctl could
50331ae08745Sheppo 		 * be returning a value such as ENOTTY to indicate
50341ae08745Sheppo 		 * that the ioctl is not applicable.
50351ae08745Sheppo 		 */
50363af08d82Slm66018 		DMSG(vdc, 0, "[%d] vds returned %d for ioctl 0x%x\n",
5037e1ebb9ecSlm66018 		    instance, rv, cmd);
50381ae08745Sheppo 		if (mem_p != NULL)
50391ae08745Sheppo 			kmem_free(mem_p, alloc_len);
5040d10e4ef2Snarayan 
50411ae08745Sheppo 		return (rv);
50421ae08745Sheppo 	}
50431ae08745Sheppo 
50441ae08745Sheppo 	/*
50450a55fbb7Slm66018 	 * Call the conversion function (if it exists) for this ioctl
50460a55fbb7Slm66018 	 * which converts from the format ARC'ed as part of the vDisk
50470a55fbb7Slm66018 	 * protocol (FWARC 2006/195) back to a format understood by
50480a55fbb7Slm66018 	 * the rest of Solaris.
50491ae08745Sheppo 	 */
50503af08d82Slm66018 	rv = (iop->convert)(vdc, mem_p, arg, mode, VD_COPYOUT);
50510a55fbb7Slm66018 	if (rv != 0) {
50523af08d82Slm66018 		DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n",
5053e1ebb9ecSlm66018 		    instance, rv, cmd);
50541ae08745Sheppo 		if (mem_p != NULL)
50551ae08745Sheppo 			kmem_free(mem_p, alloc_len);
50560a55fbb7Slm66018 		return (rv);
50571ae08745Sheppo 	}
50581ae08745Sheppo 
50591ae08745Sheppo 	if (mem_p != NULL)
50601ae08745Sheppo 		kmem_free(mem_p, alloc_len);
50611ae08745Sheppo 
50621ae08745Sheppo 	return (rv);
50631ae08745Sheppo }
50641ae08745Sheppo 
50651ae08745Sheppo /*
50661ae08745Sheppo  * Function:
50670a55fbb7Slm66018  *
50680a55fbb7Slm66018  * Description:
50690a55fbb7Slm66018  *	This is an empty conversion function used by ioctl calls which
50700a55fbb7Slm66018  *	do not need to convert the data being passed in/out to userland
50710a55fbb7Slm66018  */
50720a55fbb7Slm66018 static int
5073d10e4ef2Snarayan vdc_null_copy_func(vdc_t *vdc, void *from, void *to, int mode, int dir)
50740a55fbb7Slm66018 {
5075d10e4ef2Snarayan 	_NOTE(ARGUNUSED(vdc))
50760a55fbb7Slm66018 	_NOTE(ARGUNUSED(from))
50770a55fbb7Slm66018 	_NOTE(ARGUNUSED(to))
50780a55fbb7Slm66018 	_NOTE(ARGUNUSED(mode))
50790a55fbb7Slm66018 	_NOTE(ARGUNUSED(dir))
50800a55fbb7Slm66018 
50810a55fbb7Slm66018 	return (0);
50820a55fbb7Slm66018 }
50830a55fbb7Slm66018 
50844bac2208Snarayan static int
50854bac2208Snarayan vdc_get_wce_convert(vdc_t *vdc, void *from, void *to,
50864bac2208Snarayan     int mode, int dir)
50874bac2208Snarayan {
50884bac2208Snarayan 	_NOTE(ARGUNUSED(vdc))
50894bac2208Snarayan 
50904bac2208Snarayan 	if (dir == VD_COPYIN)
50914bac2208Snarayan 		return (0);		/* nothing to do */
50924bac2208Snarayan 
50934bac2208Snarayan 	if (ddi_copyout(from, to, sizeof (int), mode) != 0)
50944bac2208Snarayan 		return (EFAULT);
50954bac2208Snarayan 
50964bac2208Snarayan 	return (0);
50974bac2208Snarayan }
50984bac2208Snarayan 
50994bac2208Snarayan static int
51004bac2208Snarayan vdc_set_wce_convert(vdc_t *vdc, void *from, void *to,
51014bac2208Snarayan     int mode, int dir)
51024bac2208Snarayan {
51034bac2208Snarayan 	_NOTE(ARGUNUSED(vdc))
51044bac2208Snarayan 
51054bac2208Snarayan 	if (dir == VD_COPYOUT)
51064bac2208Snarayan 		return (0);		/* nothing to do */
51074bac2208Snarayan 
51084bac2208Snarayan 	if (ddi_copyin(from, to, sizeof (int), mode) != 0)
51094bac2208Snarayan 		return (EFAULT);
51104bac2208Snarayan 
51114bac2208Snarayan 	return (0);
51124bac2208Snarayan }
51134bac2208Snarayan 
51140a55fbb7Slm66018 /*
51150a55fbb7Slm66018  * Function:
51160a55fbb7Slm66018  *	vdc_get_vtoc_convert()
51170a55fbb7Slm66018  *
51180a55fbb7Slm66018  * Description:
5119d10e4ef2Snarayan  *	This routine performs the necessary convertions from the DKIOCGVTOC
5120d10e4ef2Snarayan  *	Solaris structure to the format defined in FWARC 2006/195.
5121d10e4ef2Snarayan  *
5122d10e4ef2Snarayan  *	In the struct vtoc definition, the timestamp field is marked as not
5123d10e4ef2Snarayan  *	supported so it is not part of vDisk protocol (FWARC 2006/195).
5124d10e4ef2Snarayan  *	However SVM uses that field to check it can write into the VTOC,
5125d10e4ef2Snarayan  *	so we fake up the info of that field.
51260a55fbb7Slm66018  *
51270a55fbb7Slm66018  * Arguments:
5128d10e4ef2Snarayan  *	vdc	- the vDisk client
51290a55fbb7Slm66018  *	from	- the buffer containing the data to be copied from
51300a55fbb7Slm66018  *	to	- the buffer to be copied to
51310a55fbb7Slm66018  *	mode	- flags passed to ioctl() call
51320a55fbb7Slm66018  *	dir	- the "direction" of the copy - VD_COPYIN or VD_COPYOUT
51330a55fbb7Slm66018  *
51340a55fbb7Slm66018  * Return Code:
51350a55fbb7Slm66018  *	0	- Success
51360a55fbb7Slm66018  *	ENXIO	- incorrect buffer passed in.
5137d10e4ef2Snarayan  *	EFAULT	- ddi_copyout routine encountered an error.
51380a55fbb7Slm66018  */
51390a55fbb7Slm66018 static int
5140d10e4ef2Snarayan vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
51410a55fbb7Slm66018 {
5142d10e4ef2Snarayan 	int		i;
51430a55fbb7Slm66018 	void		*tmp_mem = NULL;
51440a55fbb7Slm66018 	void		*tmp_memp;
51450a55fbb7Slm66018 	struct vtoc	vt;
51460a55fbb7Slm66018 	struct vtoc32	vt32;
51470a55fbb7Slm66018 	int		copy_len = 0;
51480a55fbb7Slm66018 	int		rv = 0;
51490a55fbb7Slm66018 
51500a55fbb7Slm66018 	if (dir != VD_COPYOUT)
51510a55fbb7Slm66018 		return (0);	/* nothing to do */
51520a55fbb7Slm66018 
51530a55fbb7Slm66018 	if ((from == NULL) || (to == NULL))
51540a55fbb7Slm66018 		return (ENXIO);
51550a55fbb7Slm66018 
51560a55fbb7Slm66018 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32)
51570a55fbb7Slm66018 		copy_len = sizeof (struct vtoc32);
51580a55fbb7Slm66018 	else
51590a55fbb7Slm66018 		copy_len = sizeof (struct vtoc);
51600a55fbb7Slm66018 
51610a55fbb7Slm66018 	tmp_mem = kmem_alloc(copy_len, KM_SLEEP);
51620a55fbb7Slm66018 
51630a55fbb7Slm66018 	VD_VTOC2VTOC((vd_vtoc_t *)from, &vt);
5164d10e4ef2Snarayan 
5165d10e4ef2Snarayan 	/* fake the VTOC timestamp field */
5166d10e4ef2Snarayan 	for (i = 0; i < V_NUMPAR; i++) {
5167d10e4ef2Snarayan 		vt.timestamp[i] = vdc->vtoc->timestamp[i];
5168d10e4ef2Snarayan 	}
5169d10e4ef2Snarayan 
51700a55fbb7Slm66018 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
51710a55fbb7Slm66018 		vtoctovtoc32(vt, vt32);
51720a55fbb7Slm66018 		tmp_memp = &vt32;
51730a55fbb7Slm66018 	} else {
51740a55fbb7Slm66018 		tmp_memp = &vt;
51750a55fbb7Slm66018 	}
51760a55fbb7Slm66018 	rv = ddi_copyout(tmp_memp, to, copy_len, mode);
51770a55fbb7Slm66018 	if (rv != 0)
51780a55fbb7Slm66018 		rv = EFAULT;
51790a55fbb7Slm66018 
51800a55fbb7Slm66018 	kmem_free(tmp_mem, copy_len);
51810a55fbb7Slm66018 	return (rv);
51820a55fbb7Slm66018 }
51830a55fbb7Slm66018 
51840a55fbb7Slm66018 /*
51850a55fbb7Slm66018  * Function:
51860a55fbb7Slm66018  *	vdc_set_vtoc_convert()
51870a55fbb7Slm66018  *
51880a55fbb7Slm66018  * Description:
5189d10e4ef2Snarayan  *	This routine performs the necessary convertions from the DKIOCSVTOC
5190d10e4ef2Snarayan  *	Solaris structure to the format defined in FWARC 2006/195.
51910a55fbb7Slm66018  *
51920a55fbb7Slm66018  * Arguments:
5193d10e4ef2Snarayan  *	vdc	- the vDisk client
51940a55fbb7Slm66018  *	from	- Buffer with data
51950a55fbb7Slm66018  *	to	- Buffer where data is to be copied to
51960a55fbb7Slm66018  *	mode	- flags passed to ioctl
51970a55fbb7Slm66018  *	dir	- direction of copy (in or out)
51980a55fbb7Slm66018  *
51990a55fbb7Slm66018  * Return Code:
52000a55fbb7Slm66018  *	0	- Success
52010a55fbb7Slm66018  *	ENXIO	- Invalid buffer passed in
52020a55fbb7Slm66018  *	EFAULT	- ddi_copyin of data failed
52030a55fbb7Slm66018  */
52040a55fbb7Slm66018 static int
5205d10e4ef2Snarayan vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
52060a55fbb7Slm66018 {
520778fcd0a1Sachartre 	_NOTE(ARGUNUSED(vdc))
520878fcd0a1Sachartre 
52090a55fbb7Slm66018 	void		*tmp_mem = NULL;
52100a55fbb7Slm66018 	struct vtoc	vt;
52110a55fbb7Slm66018 	struct vtoc	*vtp = &vt;
52120a55fbb7Slm66018 	vd_vtoc_t	vtvd;
52130a55fbb7Slm66018 	int		copy_len = 0;
52140a55fbb7Slm66018 	int		rv = 0;
52150a55fbb7Slm66018 
52160a55fbb7Slm66018 	if (dir != VD_COPYIN)
52170a55fbb7Slm66018 		return (0);	/* nothing to do */
52180a55fbb7Slm66018 
52190a55fbb7Slm66018 	if ((from == NULL) || (to == NULL))
52200a55fbb7Slm66018 		return (ENXIO);
52210a55fbb7Slm66018 
52220a55fbb7Slm66018 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32)
52230a55fbb7Slm66018 		copy_len = sizeof (struct vtoc32);
52240a55fbb7Slm66018 	else
52250a55fbb7Slm66018 		copy_len = sizeof (struct vtoc);
52260a55fbb7Slm66018 
52270a55fbb7Slm66018 	tmp_mem = kmem_alloc(copy_len, KM_SLEEP);
52280a55fbb7Slm66018 
52290a55fbb7Slm66018 	rv = ddi_copyin(from, tmp_mem, copy_len, mode);
52300a55fbb7Slm66018 	if (rv != 0) {
52310a55fbb7Slm66018 		kmem_free(tmp_mem, copy_len);
52320a55fbb7Slm66018 		return (EFAULT);
52330a55fbb7Slm66018 	}
52340a55fbb7Slm66018 
52350a55fbb7Slm66018 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
52360a55fbb7Slm66018 		vtoc32tovtoc((*(struct vtoc32 *)tmp_mem), vt);
52370a55fbb7Slm66018 	} else {
52380a55fbb7Slm66018 		vtp = tmp_mem;
52390a55fbb7Slm66018 	}
52400a55fbb7Slm66018 
52410a55fbb7Slm66018 	VTOC2VD_VTOC(vtp, &vtvd);
52420a55fbb7Slm66018 	bcopy(&vtvd, to, sizeof (vd_vtoc_t));
52430a55fbb7Slm66018 	kmem_free(tmp_mem, copy_len);
52440a55fbb7Slm66018 
52450a55fbb7Slm66018 	return (0);
52460a55fbb7Slm66018 }
52470a55fbb7Slm66018 
52480a55fbb7Slm66018 /*
52490a55fbb7Slm66018  * Function:
52500a55fbb7Slm66018  *	vdc_get_geom_convert()
52510a55fbb7Slm66018  *
52520a55fbb7Slm66018  * Description:
5253d10e4ef2Snarayan  *	This routine performs the necessary convertions from the DKIOCGGEOM,
5254d10e4ef2Snarayan  *	DKIOCG_PHYSGEOM and DKIOG_VIRTGEOM Solaris structures to the format
5255d10e4ef2Snarayan  *	defined in FWARC 2006/195
52560a55fbb7Slm66018  *
52570a55fbb7Slm66018  * Arguments:
5258d10e4ef2Snarayan  *	vdc	- the vDisk client
52590a55fbb7Slm66018  *	from	- Buffer with data
52600a55fbb7Slm66018  *	to	- Buffer where data is to be copied to
52610a55fbb7Slm66018  *	mode	- flags passed to ioctl
52620a55fbb7Slm66018  *	dir	- direction of copy (in or out)
52630a55fbb7Slm66018  *
52640a55fbb7Slm66018  * Return Code:
52650a55fbb7Slm66018  *	0	- Success
52660a55fbb7Slm66018  *	ENXIO	- Invalid buffer passed in
5267d10e4ef2Snarayan  *	EFAULT	- ddi_copyout of data failed
52680a55fbb7Slm66018  */
52690a55fbb7Slm66018 static int
5270d10e4ef2Snarayan vdc_get_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
52710a55fbb7Slm66018 {
5272d10e4ef2Snarayan 	_NOTE(ARGUNUSED(vdc))
5273d10e4ef2Snarayan 
52740a55fbb7Slm66018 	struct dk_geom	geom;
52750a55fbb7Slm66018 	int	copy_len = sizeof (struct dk_geom);
52760a55fbb7Slm66018 	int	rv = 0;
52770a55fbb7Slm66018 
52780a55fbb7Slm66018 	if (dir != VD_COPYOUT)
52790a55fbb7Slm66018 		return (0);	/* nothing to do */
52800a55fbb7Slm66018 
52810a55fbb7Slm66018 	if ((from == NULL) || (to == NULL))
52820a55fbb7Slm66018 		return (ENXIO);
52830a55fbb7Slm66018 
52840a55fbb7Slm66018 	VD_GEOM2DK_GEOM((vd_geom_t *)from, &geom);
52850a55fbb7Slm66018 	rv = ddi_copyout(&geom, to, copy_len, mode);
52860a55fbb7Slm66018 	if (rv != 0)
52870a55fbb7Slm66018 		rv = EFAULT;
52880a55fbb7Slm66018 
52890a55fbb7Slm66018 	return (rv);
52900a55fbb7Slm66018 }
52910a55fbb7Slm66018 
52920a55fbb7Slm66018 /*
52930a55fbb7Slm66018  * Function:
52940a55fbb7Slm66018  *	vdc_set_geom_convert()
52950a55fbb7Slm66018  *
52960a55fbb7Slm66018  * Description:
5297d10e4ef2Snarayan  *	This routine performs the necessary convertions from the DKIOCSGEOM
5298d10e4ef2Snarayan  *	Solaris structure to the format defined in FWARC 2006/195.
52990a55fbb7Slm66018  *
53000a55fbb7Slm66018  * Arguments:
5301d10e4ef2Snarayan  *	vdc	- the vDisk client
53020a55fbb7Slm66018  *	from	- Buffer with data
53030a55fbb7Slm66018  *	to	- Buffer where data is to be copied to
53040a55fbb7Slm66018  *	mode	- flags passed to ioctl
53050a55fbb7Slm66018  *	dir	- direction of copy (in or out)
53060a55fbb7Slm66018  *
53070a55fbb7Slm66018  * Return Code:
53080a55fbb7Slm66018  *	0	- Success
53090a55fbb7Slm66018  *	ENXIO	- Invalid buffer passed in
53100a55fbb7Slm66018  *	EFAULT	- ddi_copyin of data failed
53110a55fbb7Slm66018  */
53120a55fbb7Slm66018 static int
5313d10e4ef2Snarayan vdc_set_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
53140a55fbb7Slm66018 {
5315d10e4ef2Snarayan 	_NOTE(ARGUNUSED(vdc))
5316d10e4ef2Snarayan 
53170a55fbb7Slm66018 	vd_geom_t	vdgeom;
53180a55fbb7Slm66018 	void		*tmp_mem = NULL;
53190a55fbb7Slm66018 	int		copy_len = sizeof (struct dk_geom);
53200a55fbb7Slm66018 	int		rv = 0;
53210a55fbb7Slm66018 
53220a55fbb7Slm66018 	if (dir != VD_COPYIN)
53230a55fbb7Slm66018 		return (0);	/* nothing to do */
53240a55fbb7Slm66018 
53250a55fbb7Slm66018 	if ((from == NULL) || (to == NULL))
53260a55fbb7Slm66018 		return (ENXIO);
53270a55fbb7Slm66018 
53280a55fbb7Slm66018 	tmp_mem = kmem_alloc(copy_len, KM_SLEEP);
53290a55fbb7Slm66018 
53300a55fbb7Slm66018 	rv = ddi_copyin(from, tmp_mem, copy_len, mode);
53310a55fbb7Slm66018 	if (rv != 0) {
53320a55fbb7Slm66018 		kmem_free(tmp_mem, copy_len);
53330a55fbb7Slm66018 		return (EFAULT);
53340a55fbb7Slm66018 	}
53350a55fbb7Slm66018 	DK_GEOM2VD_GEOM((struct dk_geom *)tmp_mem, &vdgeom);
53360a55fbb7Slm66018 	bcopy(&vdgeom, to, sizeof (vdgeom));
53370a55fbb7Slm66018 	kmem_free(tmp_mem, copy_len);
53380a55fbb7Slm66018 
53390a55fbb7Slm66018 	return (0);
53400a55fbb7Slm66018 }
53410a55fbb7Slm66018 
53424bac2208Snarayan static int
53434bac2208Snarayan vdc_get_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
53444bac2208Snarayan {
53454bac2208Snarayan 	_NOTE(ARGUNUSED(vdc))
53464bac2208Snarayan 
53474bac2208Snarayan 	vd_efi_t	*vd_efi;
53484bac2208Snarayan 	dk_efi_t	dk_efi;
53494bac2208Snarayan 	int		rv = 0;
53504bac2208Snarayan 	void		*uaddr;
53514bac2208Snarayan 
53524bac2208Snarayan 	if ((from == NULL) || (to == NULL))
53534bac2208Snarayan 		return (ENXIO);
53544bac2208Snarayan 
53554bac2208Snarayan 	if (dir == VD_COPYIN) {
53564bac2208Snarayan 
53574bac2208Snarayan 		vd_efi = (vd_efi_t *)to;
53584bac2208Snarayan 
53594bac2208Snarayan 		rv = ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode);
53604bac2208Snarayan 		if (rv != 0)
53614bac2208Snarayan 			return (EFAULT);
53624bac2208Snarayan 
53634bac2208Snarayan 		vd_efi->lba = dk_efi.dki_lba;
53644bac2208Snarayan 		vd_efi->length = dk_efi.dki_length;
53654bac2208Snarayan 		bzero(vd_efi->data, vd_efi->length);
53664bac2208Snarayan 
53674bac2208Snarayan 	} else {
53684bac2208Snarayan 
53694bac2208Snarayan 		rv = ddi_copyin(to, &dk_efi, sizeof (dk_efi_t), mode);
53704bac2208Snarayan 		if (rv != 0)
53714bac2208Snarayan 			return (EFAULT);
53724bac2208Snarayan 
53734bac2208Snarayan 		uaddr = dk_efi.dki_data;
53744bac2208Snarayan 
53754bac2208Snarayan 		dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP);
53764bac2208Snarayan 
53774bac2208Snarayan 		VD_EFI2DK_EFI((vd_efi_t *)from, &dk_efi);
53784bac2208Snarayan 
53794bac2208Snarayan 		rv = ddi_copyout(dk_efi.dki_data, uaddr, dk_efi.dki_length,
53804bac2208Snarayan 		    mode);
53814bac2208Snarayan 		if (rv != 0)
53824bac2208Snarayan 			return (EFAULT);
53834bac2208Snarayan 
53844bac2208Snarayan 		kmem_free(dk_efi.dki_data, dk_efi.dki_length);
53854bac2208Snarayan 	}
53864bac2208Snarayan 
53874bac2208Snarayan 	return (0);
53884bac2208Snarayan }
53894bac2208Snarayan 
53904bac2208Snarayan static int
53914bac2208Snarayan vdc_set_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
53924bac2208Snarayan {
53934bac2208Snarayan 	_NOTE(ARGUNUSED(vdc))
53944bac2208Snarayan 
53954bac2208Snarayan 	dk_efi_t	dk_efi;
53964bac2208Snarayan 	void		*uaddr;
53974bac2208Snarayan 
53984bac2208Snarayan 	if (dir == VD_COPYOUT)
53994bac2208Snarayan 		return (0);	/* nothing to do */
54004bac2208Snarayan 
54014bac2208Snarayan 	if ((from == NULL) || (to == NULL))
54024bac2208Snarayan 		return (ENXIO);
54034bac2208Snarayan 
54044bac2208Snarayan 	if (ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode) != 0)
54054bac2208Snarayan 		return (EFAULT);
54064bac2208Snarayan 
54074bac2208Snarayan 	uaddr = dk_efi.dki_data;
54084bac2208Snarayan 
54094bac2208Snarayan 	dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP);
54104bac2208Snarayan 
54114bac2208Snarayan 	if (ddi_copyin(uaddr, dk_efi.dki_data, dk_efi.dki_length, mode) != 0)
54124bac2208Snarayan 		return (EFAULT);
54134bac2208Snarayan 
54144bac2208Snarayan 	DK_EFI2VD_EFI(&dk_efi, (vd_efi_t *)to);
54154bac2208Snarayan 
54164bac2208Snarayan 	kmem_free(dk_efi.dki_data, dk_efi.dki_length);
54174bac2208Snarayan 
54184bac2208Snarayan 	return (0);
54194bac2208Snarayan }
54204bac2208Snarayan 
54210a55fbb7Slm66018 /*
54220a55fbb7Slm66018  * Function:
54231ae08745Sheppo  *	vdc_create_fake_geometry()
54241ae08745Sheppo  *
54251ae08745Sheppo  * Description:
54261ae08745Sheppo  *	This routine fakes up the disk info needed for some DKIO ioctls.
54271ae08745Sheppo  *		- DKIOCINFO
54281ae08745Sheppo  *		- DKIOCGMEDIAINFO
54291ae08745Sheppo  *
54301ae08745Sheppo  *	[ just like lofi(7D) and ramdisk(7D) ]
54311ae08745Sheppo  *
54321ae08745Sheppo  * Arguments:
54331ae08745Sheppo  *	vdc	- soft state pointer for this instance of the device driver.
54341ae08745Sheppo  *
54351ae08745Sheppo  * Return Code:
543678fcd0a1Sachartre  *	none.
54371ae08745Sheppo  */
543878fcd0a1Sachartre static void
54391ae08745Sheppo vdc_create_fake_geometry(vdc_t *vdc)
54401ae08745Sheppo {
54411ae08745Sheppo 	ASSERT(vdc != NULL);
544278fcd0a1Sachartre 	ASSERT(vdc->vdisk_size != 0);
544378fcd0a1Sachartre 	ASSERT(vdc->max_xfer_sz != 0);
54440d0c8d4bSnarayan 
54450d0c8d4bSnarayan 	/*
54461ae08745Sheppo 	 * DKIOCINFO support
54471ae08745Sheppo 	 */
544878fcd0a1Sachartre 	if (vdc->cinfo == NULL)
54491ae08745Sheppo 		vdc->cinfo = kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
54501ae08745Sheppo 
54511ae08745Sheppo 	(void) strcpy(vdc->cinfo->dki_cname, VDC_DRIVER_NAME);
54521ae08745Sheppo 	(void) strcpy(vdc->cinfo->dki_dname, VDC_DRIVER_NAME);
54538e6a2a04Slm66018 	/* max_xfer_sz is #blocks so we don't need to divide by DEV_BSIZE */
54548e6a2a04Slm66018 	vdc->cinfo->dki_maxtransfer = vdc->max_xfer_sz;
545587a7269eSachartre 	/*
545687a7269eSachartre 	 * We currently set the controller type to DKC_DIRECT for any disk.
545787a7269eSachartre 	 * When SCSI support is implemented, we will eventually change this
545887a7269eSachartre 	 * type to DKC_SCSI_CCS for disks supporting the SCSI protocol.
545987a7269eSachartre 	 */
546087a7269eSachartre 	vdc->cinfo->dki_ctype = DKC_DIRECT;
54611ae08745Sheppo 	vdc->cinfo->dki_flags = DKI_FMTVOL;
54621ae08745Sheppo 	vdc->cinfo->dki_cnum = 0;
54631ae08745Sheppo 	vdc->cinfo->dki_addr = 0;
54641ae08745Sheppo 	vdc->cinfo->dki_space = 0;
54651ae08745Sheppo 	vdc->cinfo->dki_prio = 0;
54661ae08745Sheppo 	vdc->cinfo->dki_vec = 0;
54671ae08745Sheppo 	vdc->cinfo->dki_unit = vdc->instance;
54681ae08745Sheppo 	vdc->cinfo->dki_slave = 0;
54691ae08745Sheppo 	/*
54701ae08745Sheppo 	 * The partition number will be created on the fly depending on the
54711ae08745Sheppo 	 * actual slice (i.e. minor node) that is used to request the data.
54721ae08745Sheppo 	 */
54731ae08745Sheppo 	vdc->cinfo->dki_partition = 0;
54741ae08745Sheppo 
54751ae08745Sheppo 	/*
54761ae08745Sheppo 	 * DKIOCGMEDIAINFO support
54771ae08745Sheppo 	 */
54780a55fbb7Slm66018 	if (vdc->minfo == NULL)
54791ae08745Sheppo 		vdc->minfo = kmem_zalloc(sizeof (struct dk_minfo), KM_SLEEP);
54801ae08745Sheppo 	vdc->minfo->dki_media_type = DK_FIXED_DISK;
54814bac2208Snarayan 	vdc->minfo->dki_capacity = vdc->vdisk_size;
54821ae08745Sheppo 	vdc->minfo->dki_lbsize = DEV_BSIZE;
548378fcd0a1Sachartre }
54841ae08745Sheppo 
548578fcd0a1Sachartre static ushort_t
548678fcd0a1Sachartre vdc_lbl2cksum(struct dk_label *label)
548778fcd0a1Sachartre {
548878fcd0a1Sachartre 	int	count;
548978fcd0a1Sachartre 	ushort_t sum, *sp;
549078fcd0a1Sachartre 
549178fcd0a1Sachartre 	count =	(sizeof (struct dk_label)) / (sizeof (short)) - 1;
549278fcd0a1Sachartre 	sp = (ushort_t *)label;
549378fcd0a1Sachartre 	sum = 0;
549478fcd0a1Sachartre 	while (count--) {
549578fcd0a1Sachartre 		sum ^= *sp++;
549678fcd0a1Sachartre 	}
549778fcd0a1Sachartre 
549878fcd0a1Sachartre 	return (sum);
54990a55fbb7Slm66018 }
55000a55fbb7Slm66018 
55010a55fbb7Slm66018 /*
55020a55fbb7Slm66018  * Function:
550378fcd0a1Sachartre  *	vdc_validate_geometry
55040a55fbb7Slm66018  *
55050a55fbb7Slm66018  * Description:
550678fcd0a1Sachartre  *	This routine discovers the label and geometry of the disk. It stores
550778fcd0a1Sachartre  *	the disk label and related information in the vdc structure. If it
550878fcd0a1Sachartre  *	fails to validate the geometry or to discover the disk label then
550978fcd0a1Sachartre  *	the label is marked as unknown (VD_DISK_LABEL_UNK).
55100a55fbb7Slm66018  *
55110a55fbb7Slm66018  * Arguments:
55120a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
55130a55fbb7Slm66018  *
55140a55fbb7Slm66018  * Return Code:
551578fcd0a1Sachartre  *	0	- success.
551678fcd0a1Sachartre  *	EINVAL	- unknown disk label.
551778fcd0a1Sachartre  *	ENOTSUP	- geometry not applicable (EFI label).
551878fcd0a1Sachartre  *	EIO	- error accessing the disk.
55190a55fbb7Slm66018  */
55200a55fbb7Slm66018 static int
552178fcd0a1Sachartre vdc_validate_geometry(vdc_t *vdc)
55220a55fbb7Slm66018 {
5523d10e4ef2Snarayan 	buf_t	*buf;	/* BREAD requests need to be in a buf_t structure */
55240a55fbb7Slm66018 	dev_t	dev;
552578fcd0a1Sachartre 	int	rv;
552678fcd0a1Sachartre 	struct dk_label label;
552778fcd0a1Sachartre 	struct dk_geom geom;
552878fcd0a1Sachartre 	struct vtoc vtoc;
55290a55fbb7Slm66018 
55300a55fbb7Slm66018 	ASSERT(vdc != NULL);
553178fcd0a1Sachartre 	ASSERT(vdc->vtoc != NULL && vdc->geom != NULL);
553278fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
55330a55fbb7Slm66018 
553478fcd0a1Sachartre 	mutex_exit(&vdc->lock);
55350a55fbb7Slm66018 
55360a55fbb7Slm66018 	dev = makedevice(ddi_driver_major(vdc->dip),
55370a55fbb7Slm66018 	    VD_MAKE_DEV(vdc->instance, 0));
55384bac2208Snarayan 
553978fcd0a1Sachartre 	rv = vd_process_ioctl(dev, DKIOCGGEOM, (caddr_t)&geom, FKIOCTL);
554078fcd0a1Sachartre 	if (rv == 0)
554178fcd0a1Sachartre 		rv = vd_process_ioctl(dev, DKIOCGVTOC, (caddr_t)&vtoc, FKIOCTL);
55420d0c8d4bSnarayan 
55434bac2208Snarayan 	if (rv == ENOTSUP) {
55444bac2208Snarayan 		/*
55454bac2208Snarayan 		 * If the device does not support VTOC then we try
55464bac2208Snarayan 		 * to read an EFI label.
55474bac2208Snarayan 		 */
55484bac2208Snarayan 		struct dk_gpt *efi;
55494bac2208Snarayan 		size_t efi_len;
55504bac2208Snarayan 
55514bac2208Snarayan 		rv = vdc_efi_alloc_and_read(dev, &efi, &efi_len);
55524bac2208Snarayan 
55534bac2208Snarayan 		if (rv) {
55543af08d82Slm66018 			DMSG(vdc, 0, "[%d] Failed to get EFI (err=%d)",
55554bac2208Snarayan 			    vdc->instance, rv);
555678fcd0a1Sachartre 			mutex_enter(&vdc->lock);
555778fcd0a1Sachartre 			vdc_store_label_unk(vdc);
555878fcd0a1Sachartre 			return (EIO);
555978fcd0a1Sachartre 		}
556078fcd0a1Sachartre 
556178fcd0a1Sachartre 		mutex_enter(&vdc->lock);
556278fcd0a1Sachartre 		vdc_store_label_efi(vdc, efi);
556378fcd0a1Sachartre 		vd_efi_free(efi, efi_len);
556478fcd0a1Sachartre 		return (ENOTSUP);
556578fcd0a1Sachartre 	}
556678fcd0a1Sachartre 
556778fcd0a1Sachartre 	if (rv != 0) {
556878fcd0a1Sachartre 		DMSG(vdc, 0, "[%d] Failed to get VTOC (err=%d)",
556978fcd0a1Sachartre 		    vdc->instance, rv);
557078fcd0a1Sachartre 		mutex_enter(&vdc->lock);
557178fcd0a1Sachartre 		vdc_store_label_unk(vdc);
557278fcd0a1Sachartre 		if (rv != EINVAL)
557378fcd0a1Sachartre 			rv = EIO;
55744bac2208Snarayan 		return (rv);
55754bac2208Snarayan 	}
55764bac2208Snarayan 
557778fcd0a1Sachartre 	/* check that geometry and vtoc are valid */
557878fcd0a1Sachartre 	if (geom.dkg_nhead == 0 || geom.dkg_nsect == 0 ||
557978fcd0a1Sachartre 	    vtoc.v_sanity != VTOC_SANE) {
558078fcd0a1Sachartre 		mutex_enter(&vdc->lock);
558178fcd0a1Sachartre 		vdc_store_label_unk(vdc);
558278fcd0a1Sachartre 		return (EINVAL);
558378fcd0a1Sachartre 	}
55844bac2208Snarayan 
558578fcd0a1Sachartre 	/*
558678fcd0a1Sachartre 	 * We have a disk and a valid VTOC. However this does not mean
558778fcd0a1Sachartre 	 * that the disk currently have a VTOC label. The returned VTOC may
558878fcd0a1Sachartre 	 * be a default VTOC to be used for configuring the disk (this is
558978fcd0a1Sachartre 	 * what is done for disk image). So we read the label from the
559078fcd0a1Sachartre 	 * beginning of the disk to ensure we really have a VTOC label.
559178fcd0a1Sachartre 	 *
559278fcd0a1Sachartre 	 * FUTURE: This could be the default way for reading the VTOC
559378fcd0a1Sachartre 	 * from the disk as opposed to sending the VD_OP_GET_VTOC
559478fcd0a1Sachartre 	 * to the server. This will be the default if vdc is implemented
559578fcd0a1Sachartre 	 * ontop of cmlb.
559678fcd0a1Sachartre 	 */
559778fcd0a1Sachartre 
559878fcd0a1Sachartre 	/*
559978fcd0a1Sachartre 	 * Single slice disk does not support read using an absolute disk
560078fcd0a1Sachartre 	 * offset so we just rely on the DKIOCGVTOC ioctl in that case.
560178fcd0a1Sachartre 	 */
560278fcd0a1Sachartre 	if (vdc->vdisk_type == VD_DISK_TYPE_SLICE) {
560378fcd0a1Sachartre 		mutex_enter(&vdc->lock);
560478fcd0a1Sachartre 		if (vtoc.v_nparts != 1) {
560578fcd0a1Sachartre 			vdc_store_label_unk(vdc);
560678fcd0a1Sachartre 			return (EINVAL);
560778fcd0a1Sachartre 		}
560878fcd0a1Sachartre 		vdc_store_label_vtoc(vdc, &geom, &vtoc);
56094bac2208Snarayan 		return (0);
56104bac2208Snarayan 	}
56114bac2208Snarayan 
561278fcd0a1Sachartre 	if (vtoc.v_nparts != V_NUMPAR) {
561378fcd0a1Sachartre 		mutex_enter(&vdc->lock);
561478fcd0a1Sachartre 		vdc_store_label_unk(vdc);
561578fcd0a1Sachartre 		return (EINVAL);
56160a55fbb7Slm66018 	}
5617d10e4ef2Snarayan 
5618d10e4ef2Snarayan 	/*
5619d10e4ef2Snarayan 	 * Read disk label from start of disk
5620d10e4ef2Snarayan 	 */
5621d10e4ef2Snarayan 	buf = kmem_alloc(sizeof (buf_t), KM_SLEEP);
5622d10e4ef2Snarayan 	bioinit(buf);
562378fcd0a1Sachartre 	buf->b_un.b_addr = (caddr_t)&label;
5624d10e4ef2Snarayan 	buf->b_bcount = DK_LABEL_SIZE;
5625d10e4ef2Snarayan 	buf->b_flags = B_BUSY | B_READ;
5626d10e4ef2Snarayan 	buf->b_dev = dev;
562778fcd0a1Sachartre 	rv = vdc_send_request(vdc, VD_OP_BREAD, (caddr_t)&label,
562878fcd0a1Sachartre 	    DK_LABEL_SIZE, VD_SLICE_NONE, 0, CB_STRATEGY, buf, VIO_read_dir);
56293af08d82Slm66018 	if (rv) {
56303af08d82Slm66018 		DMSG(vdc, 1, "[%d] Failed to read disk block 0\n",
56313af08d82Slm66018 		    vdc->instance);
563278fcd0a1Sachartre 	} else {
5633d10e4ef2Snarayan 		rv = biowait(buf);
5634d10e4ef2Snarayan 		biofini(buf);
563578fcd0a1Sachartre 	}
5636d10e4ef2Snarayan 	kmem_free(buf, sizeof (buf_t));
56370a55fbb7Slm66018 
563878fcd0a1Sachartre 	if (rv != 0 || label.dkl_magic != DKL_MAGIC ||
563978fcd0a1Sachartre 	    label.dkl_cksum != vdc_lbl2cksum(&label)) {
564078fcd0a1Sachartre 		DMSG(vdc, 1, "[%d] Got VTOC with invalid label\n",
564178fcd0a1Sachartre 		    vdc->instance);
564278fcd0a1Sachartre 		mutex_enter(&vdc->lock);
564378fcd0a1Sachartre 		vdc_store_label_unk(vdc);
564478fcd0a1Sachartre 		return (EINVAL);
564578fcd0a1Sachartre 	}
564678fcd0a1Sachartre 
564778fcd0a1Sachartre 	mutex_enter(&vdc->lock);
564878fcd0a1Sachartre 	vdc_store_label_vtoc(vdc, &geom, &vtoc);
564978fcd0a1Sachartre 	return (0);
565078fcd0a1Sachartre }
565178fcd0a1Sachartre 
565278fcd0a1Sachartre /*
565378fcd0a1Sachartre  * Function:
565478fcd0a1Sachartre  *	vdc_validate
565578fcd0a1Sachartre  *
565678fcd0a1Sachartre  * Description:
565778fcd0a1Sachartre  *	This routine discovers the label of the disk and create the
565878fcd0a1Sachartre  *	appropriate device nodes if the label has changed.
565978fcd0a1Sachartre  *
566078fcd0a1Sachartre  * Arguments:
566178fcd0a1Sachartre  *	vdc	- soft state pointer for this instance of the device driver.
566278fcd0a1Sachartre  *
566378fcd0a1Sachartre  * Return Code:
566478fcd0a1Sachartre  *	none.
566578fcd0a1Sachartre  */
566678fcd0a1Sachartre static void
566778fcd0a1Sachartre vdc_validate(vdc_t *vdc)
566878fcd0a1Sachartre {
566978fcd0a1Sachartre 	vd_disk_label_t old_label;
567078fcd0a1Sachartre 	struct vtoc old_vtoc;
567178fcd0a1Sachartre 	int rv;
567278fcd0a1Sachartre 
567378fcd0a1Sachartre 	ASSERT(!MUTEX_HELD(&vdc->lock));
567478fcd0a1Sachartre 
567578fcd0a1Sachartre 	mutex_enter(&vdc->lock);
567678fcd0a1Sachartre 
567778fcd0a1Sachartre 	/* save the current label and vtoc */
567878fcd0a1Sachartre 	old_label = vdc->vdisk_label;
567978fcd0a1Sachartre 	bcopy(vdc->vtoc, &old_vtoc, sizeof (struct vtoc));
568078fcd0a1Sachartre 
568178fcd0a1Sachartre 	/* check the geometry */
568278fcd0a1Sachartre 	(void) vdc_validate_geometry(vdc);
568378fcd0a1Sachartre 
568478fcd0a1Sachartre 	/* if the disk label has changed, update device nodes */
568578fcd0a1Sachartre 	if (vdc->vdisk_label != old_label) {
568678fcd0a1Sachartre 
568778fcd0a1Sachartre 		if (vdc->vdisk_label == VD_DISK_LABEL_EFI)
568878fcd0a1Sachartre 			rv = vdc_create_device_nodes_efi(vdc);
568978fcd0a1Sachartre 		else
569078fcd0a1Sachartre 			rv = vdc_create_device_nodes_vtoc(vdc);
569178fcd0a1Sachartre 
569278fcd0a1Sachartre 		if (rv != 0) {
569378fcd0a1Sachartre 			DMSG(vdc, 0, "![%d] Failed to update device nodes",
569478fcd0a1Sachartre 			    vdc->instance);
569578fcd0a1Sachartre 		}
569678fcd0a1Sachartre 	}
569778fcd0a1Sachartre 
569878fcd0a1Sachartre 	/* if the vtoc has changed, update device nodes properties */
569978fcd0a1Sachartre 	if (bcmp(vdc->vtoc, &old_vtoc, sizeof (struct vtoc)) != 0) {
570078fcd0a1Sachartre 
570178fcd0a1Sachartre 		if (vdc_create_device_nodes_props(vdc) != 0) {
570278fcd0a1Sachartre 			DMSG(vdc, 0, "![%d] Failed to update device nodes"
570378fcd0a1Sachartre 			    " properties", vdc->instance);
570478fcd0a1Sachartre 		}
570578fcd0a1Sachartre 	}
570678fcd0a1Sachartre 
570778fcd0a1Sachartre 	mutex_exit(&vdc->lock);
570878fcd0a1Sachartre }
570978fcd0a1Sachartre 
571078fcd0a1Sachartre static void
571178fcd0a1Sachartre vdc_validate_task(void *arg)
571278fcd0a1Sachartre {
571378fcd0a1Sachartre 	vdc_t *vdc = (vdc_t *)arg;
571478fcd0a1Sachartre 
571578fcd0a1Sachartre 	vdc_validate(vdc);
571678fcd0a1Sachartre 
571778fcd0a1Sachartre 	mutex_enter(&vdc->lock);
571878fcd0a1Sachartre 	ASSERT(vdc->validate_pending > 0);
571978fcd0a1Sachartre 	vdc->validate_pending--;
572078fcd0a1Sachartre 	mutex_exit(&vdc->lock);
57211ae08745Sheppo }
57224bac2208Snarayan 
57234bac2208Snarayan /*
57244bac2208Snarayan  * Function:
57254bac2208Snarayan  *	vdc_setup_devid()
57264bac2208Snarayan  *
57274bac2208Snarayan  * Description:
57284bac2208Snarayan  *	This routine discovers the devid of a vDisk. It requests the devid of
57294bac2208Snarayan  *	the underlying device from the vDisk server, builds an encapsulated
57304bac2208Snarayan  *	devid based on the retrieved devid and registers that new devid to
57314bac2208Snarayan  *	the vDisk.
57324bac2208Snarayan  *
57334bac2208Snarayan  * Arguments:
57344bac2208Snarayan  *	vdc	- soft state pointer for this instance of the device driver.
57354bac2208Snarayan  *
57364bac2208Snarayan  * Return Code:
57374bac2208Snarayan  *	0	- A devid was succesfully registered for the vDisk
57384bac2208Snarayan  */
57394bac2208Snarayan static int
57404bac2208Snarayan vdc_setup_devid(vdc_t *vdc)
57414bac2208Snarayan {
57424bac2208Snarayan 	int rv;
57434bac2208Snarayan 	vd_devid_t *vd_devid;
57444bac2208Snarayan 	size_t bufsize, bufid_len;
57454bac2208Snarayan 
57464bac2208Snarayan 	/*
57474bac2208Snarayan 	 * At first sight, we don't know the size of the devid that the
57484bac2208Snarayan 	 * server will return but this size will be encoded into the
57494bac2208Snarayan 	 * reply. So we do a first request using a default size then we
57504bac2208Snarayan 	 * check if this size was large enough. If not then we do a second
57514bac2208Snarayan 	 * request with the correct size returned by the server. Note that
57524bac2208Snarayan 	 * ldc requires size to be 8-byte aligned.
57534bac2208Snarayan 	 */
57544bac2208Snarayan 	bufsize = P2ROUNDUP(VD_DEVID_SIZE(VD_DEVID_DEFAULT_LEN),
57554bac2208Snarayan 	    sizeof (uint64_t));
57564bac2208Snarayan 	vd_devid = kmem_zalloc(bufsize, KM_SLEEP);
57574bac2208Snarayan 	bufid_len = bufsize - sizeof (vd_efi_t) - 1;
57584bac2208Snarayan 
57593af08d82Slm66018 	rv = vdc_do_sync_op(vdc, VD_OP_GET_DEVID, (caddr_t)vd_devid,
57603af08d82Slm66018 	    bufsize, 0, 0, CB_SYNC, 0, VIO_both_dir);
57613af08d82Slm66018 
57623af08d82Slm66018 	DMSG(vdc, 2, "sync_op returned %d\n", rv);
57633af08d82Slm66018 
57644bac2208Snarayan 	if (rv) {
57654bac2208Snarayan 		kmem_free(vd_devid, bufsize);
57664bac2208Snarayan 		return (rv);
57674bac2208Snarayan 	}
57684bac2208Snarayan 
57694bac2208Snarayan 	if (vd_devid->length > bufid_len) {
57704bac2208Snarayan 		/*
57714bac2208Snarayan 		 * The returned devid is larger than the buffer used. Try again
57724bac2208Snarayan 		 * with a buffer with the right size.
57734bac2208Snarayan 		 */
57744bac2208Snarayan 		kmem_free(vd_devid, bufsize);
57754bac2208Snarayan 		bufsize = P2ROUNDUP(VD_DEVID_SIZE(vd_devid->length),
57764bac2208Snarayan 		    sizeof (uint64_t));
57774bac2208Snarayan 		vd_devid = kmem_zalloc(bufsize, KM_SLEEP);
57784bac2208Snarayan 		bufid_len = bufsize - sizeof (vd_efi_t) - 1;
57794bac2208Snarayan 
57803af08d82Slm66018 		rv = vdc_do_sync_op(vdc, VD_OP_GET_DEVID,
57813af08d82Slm66018 		    (caddr_t)vd_devid, bufsize, 0, 0, CB_SYNC, 0,
57823af08d82Slm66018 		    VIO_both_dir);
57833af08d82Slm66018 
57844bac2208Snarayan 		if (rv) {
57854bac2208Snarayan 			kmem_free(vd_devid, bufsize);
57864bac2208Snarayan 			return (rv);
57874bac2208Snarayan 		}
57884bac2208Snarayan 	}
57894bac2208Snarayan 
57904bac2208Snarayan 	/*
57914bac2208Snarayan 	 * The virtual disk should have the same device id as the one associated
57924bac2208Snarayan 	 * with the physical disk it is mapped on, otherwise sharing a disk
57934bac2208Snarayan 	 * between a LDom and a non-LDom may not work (for example for a shared
57944bac2208Snarayan 	 * SVM disk set).
57954bac2208Snarayan 	 *
57964bac2208Snarayan 	 * The DDI framework does not allow creating a device id with any
57974bac2208Snarayan 	 * type so we first create a device id of type DEVID_ENCAP and then
57984bac2208Snarayan 	 * we restore the orignal type of the physical device.
57994bac2208Snarayan 	 */
58004bac2208Snarayan 
58013af08d82Slm66018 	DMSG(vdc, 2, ": devid length = %d\n", vd_devid->length);
58023af08d82Slm66018 
58034bac2208Snarayan 	/* build an encapsulated devid based on the returned devid */
58044bac2208Snarayan 	if (ddi_devid_init(vdc->dip, DEVID_ENCAP, vd_devid->length,
58054bac2208Snarayan 	    vd_devid->id, &vdc->devid) != DDI_SUCCESS) {
58063af08d82Slm66018 		DMSG(vdc, 1, "[%d] Fail to created devid\n", vdc->instance);
58074bac2208Snarayan 		kmem_free(vd_devid, bufsize);
58084bac2208Snarayan 		return (1);
58094bac2208Snarayan 	}
58104bac2208Snarayan 
58114bac2208Snarayan 	DEVID_FORMTYPE((impl_devid_t *)vdc->devid, vd_devid->type);
58124bac2208Snarayan 
58134bac2208Snarayan 	ASSERT(ddi_devid_valid(vdc->devid) == DDI_SUCCESS);
58144bac2208Snarayan 
58154bac2208Snarayan 	kmem_free(vd_devid, bufsize);
58164bac2208Snarayan 
58174bac2208Snarayan 	if (ddi_devid_register(vdc->dip, vdc->devid) != DDI_SUCCESS) {
58183af08d82Slm66018 		DMSG(vdc, 1, "[%d] Fail to register devid\n", vdc->instance);
58194bac2208Snarayan 		return (1);
58204bac2208Snarayan 	}
58214bac2208Snarayan 
58224bac2208Snarayan 	return (0);
58234bac2208Snarayan }
58244bac2208Snarayan 
58254bac2208Snarayan static void
582678fcd0a1Sachartre vdc_store_label_efi(vdc_t *vdc, struct dk_gpt *efi)
58274bac2208Snarayan {
58284bac2208Snarayan 	struct vtoc *vtoc = vdc->vtoc;
58294bac2208Snarayan 
583078fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
583178fcd0a1Sachartre 
583278fcd0a1Sachartre 	vdc->vdisk_label = VD_DISK_LABEL_EFI;
583378fcd0a1Sachartre 	bzero(vdc->geom, sizeof (struct dk_geom));
58344bac2208Snarayan 	vd_efi_to_vtoc(efi, vtoc);
58354bac2208Snarayan 	if (vdc->vdisk_type == VD_DISK_TYPE_SLICE) {
58364bac2208Snarayan 		/*
58374bac2208Snarayan 		 * vd_efi_to_vtoc() will store information about the EFI Sun
58384bac2208Snarayan 		 * reserved partition (representing the entire disk) into
58394bac2208Snarayan 		 * partition 7. However single-slice device will only have
58404bac2208Snarayan 		 * that single partition and the vdc driver expects to find
58414bac2208Snarayan 		 * information about that partition in slice 0. So we need
58424bac2208Snarayan 		 * to copy information from slice 7 to slice 0.
58434bac2208Snarayan 		 */
58444bac2208Snarayan 		vtoc->v_part[0].p_tag = vtoc->v_part[VD_EFI_WD_SLICE].p_tag;
58454bac2208Snarayan 		vtoc->v_part[0].p_flag = vtoc->v_part[VD_EFI_WD_SLICE].p_flag;
58464bac2208Snarayan 		vtoc->v_part[0].p_start = vtoc->v_part[VD_EFI_WD_SLICE].p_start;
58474bac2208Snarayan 		vtoc->v_part[0].p_size =  vtoc->v_part[VD_EFI_WD_SLICE].p_size;
58484bac2208Snarayan 	}
58494bac2208Snarayan }
585078fcd0a1Sachartre 
585178fcd0a1Sachartre static void
585278fcd0a1Sachartre vdc_store_label_vtoc(vdc_t *vdc, struct dk_geom *geom, struct vtoc *vtoc)
585378fcd0a1Sachartre {
585478fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
585578fcd0a1Sachartre 
585678fcd0a1Sachartre 	vdc->vdisk_label = VD_DISK_LABEL_VTOC;
585778fcd0a1Sachartre 	bcopy(vtoc, vdc->vtoc, sizeof (struct vtoc));
585878fcd0a1Sachartre 	bcopy(geom, vdc->geom, sizeof (struct dk_geom));
585978fcd0a1Sachartre }
586078fcd0a1Sachartre 
586178fcd0a1Sachartre static void
586278fcd0a1Sachartre vdc_store_label_unk(vdc_t *vdc)
586378fcd0a1Sachartre {
586478fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
586578fcd0a1Sachartre 
586678fcd0a1Sachartre 	vdc->vdisk_label = VD_DISK_LABEL_UNK;
586778fcd0a1Sachartre 	bzero(vdc->vtoc, sizeof (struct vtoc));
586878fcd0a1Sachartre 	bzero(vdc->geom, sizeof (struct dk_geom));
586978fcd0a1Sachartre }
5870