xref: /titanic_53/usr/src/uts/sun4v/io/vdc.c (revision 8cd108911b492c7a96794e47d7064a4c4c064338)
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 /*
23edcc0754Sachartre  * Copyright 2008 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>
68366a92acSlm66018 #include <sys/kstat.h>
691ae08745Sheppo #include <sys/mach_descrip.h>
701ae08745Sheppo #include <sys/modctl.h>
711ae08745Sheppo #include <sys/mdeg.h>
721ae08745Sheppo #include <sys/note.h>
731ae08745Sheppo #include <sys/open.h>
74d10e4ef2Snarayan #include <sys/sdt.h>
751ae08745Sheppo #include <sys/stat.h>
761ae08745Sheppo #include <sys/sunddi.h>
771ae08745Sheppo #include <sys/types.h>
781ae08745Sheppo #include <sys/promif.h>
792f5224aeSachartre #include <sys/var.h>
801ae08745Sheppo #include <sys/vtoc.h>
811ae08745Sheppo #include <sys/archsystm.h>
821ae08745Sheppo #include <sys/sysmacros.h>
831ae08745Sheppo 
841ae08745Sheppo #include <sys/cdio.h>
851ae08745Sheppo #include <sys/dktp/fdisk.h>
8687a7269eSachartre #include <sys/dktp/dadkio.h>
872f5224aeSachartre #include <sys/mhd.h>
881ae08745Sheppo #include <sys/scsi/generic/sense.h>
892f5224aeSachartre #include <sys/scsi/impl/uscsi.h>
902f5224aeSachartre #include <sys/scsi/impl/services.h>
912f5224aeSachartre #include <sys/scsi/targets/sddef.h>
921ae08745Sheppo 
931ae08745Sheppo #include <sys/ldoms.h>
941ae08745Sheppo #include <sys/ldc.h>
951ae08745Sheppo #include <sys/vio_common.h>
961ae08745Sheppo #include <sys/vio_mailbox.h>
9717cadca8Slm66018 #include <sys/vio_util.h>
981ae08745Sheppo #include <sys/vdsk_common.h>
991ae08745Sheppo #include <sys/vdsk_mailbox.h>
1001ae08745Sheppo #include <sys/vdc.h>
1011ae08745Sheppo 
1021ae08745Sheppo /*
1031ae08745Sheppo  * function prototypes
1041ae08745Sheppo  */
1051ae08745Sheppo 
1061ae08745Sheppo /* standard driver functions */
1071ae08745Sheppo static int	vdc_open(dev_t *dev, int flag, int otyp, cred_t *cred);
1081ae08745Sheppo static int	vdc_close(dev_t dev, int flag, int otyp, cred_t *cred);
1091ae08745Sheppo static int	vdc_strategy(struct buf *buf);
1101ae08745Sheppo static int	vdc_print(dev_t dev, char *str);
1111ae08745Sheppo static int	vdc_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
1121ae08745Sheppo static int	vdc_read(dev_t dev, struct uio *uio, cred_t *cred);
1131ae08745Sheppo static int	vdc_write(dev_t dev, struct uio *uio, cred_t *cred);
1141ae08745Sheppo static int	vdc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
1151ae08745Sheppo 			cred_t *credp, int *rvalp);
1161ae08745Sheppo static int	vdc_aread(dev_t dev, struct aio_req *aio, cred_t *cred);
1171ae08745Sheppo static int	vdc_awrite(dev_t dev, struct aio_req *aio, cred_t *cred);
1181ae08745Sheppo 
1191ae08745Sheppo static int	vdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,
1201ae08745Sheppo 			void *arg, void **resultp);
1211ae08745Sheppo static int	vdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
1221ae08745Sheppo static int	vdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
1231ae08745Sheppo 
1241ae08745Sheppo /* setup */
1250d0c8d4bSnarayan static void	vdc_min(struct buf *bufp);
1260a55fbb7Slm66018 static int	vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen);
127*8cd10891Snarayan static int	vdc_do_ldc_init(vdc_t *vdc, vdc_server_t *srvr);
1281ae08745Sheppo static int	vdc_start_ldc_connection(vdc_t *vdc);
1291ae08745Sheppo static int	vdc_create_device_nodes(vdc_t *vdc);
1304bac2208Snarayan static int	vdc_create_device_nodes_efi(vdc_t *vdc);
1314bac2208Snarayan static int	vdc_create_device_nodes_vtoc(vdc_t *vdc);
1321ae08745Sheppo static int	vdc_create_device_nodes_props(vdc_t *vdc);
133366a92acSlm66018 static void	vdc_create_io_kstats(vdc_t *vdc);
134366a92acSlm66018 static void	vdc_create_err_kstats(vdc_t *vdc);
135366a92acSlm66018 static void	vdc_set_err_kstats(vdc_t *vdc);
136655fd6a9Sachartre static int	vdc_get_md_node(dev_info_t *dip, md_t **mdpp,
137*8cd10891Snarayan 		    mde_cookie_t *vd_nodep);
138*8cd10891Snarayan static int	vdc_init_ports(vdc_t *vdc, md_t *mdp, mde_cookie_t vd_nodep);
139*8cd10891Snarayan static void	vdc_fini_ports(vdc_t *vdc);
140*8cd10891Snarayan static void	vdc_switch_server(vdc_t *vdcp);
1410a55fbb7Slm66018 static int	vdc_do_ldc_up(vdc_t *vdc);
142*8cd10891Snarayan static void	vdc_terminate_ldc(vdc_t *vdc, vdc_server_t *srvr);
1431ae08745Sheppo static int	vdc_init_descriptor_ring(vdc_t *vdc);
1441ae08745Sheppo static void	vdc_destroy_descriptor_ring(vdc_t *vdc);
1454bac2208Snarayan static int	vdc_setup_devid(vdc_t *vdc);
146edcc0754Sachartre static void	vdc_store_label_efi(vdc_t *, efi_gpt_t *, efi_gpe_t *);
14778fcd0a1Sachartre static void	vdc_store_label_vtoc(vdc_t *, struct dk_geom *, struct vtoc *);
14878fcd0a1Sachartre static void	vdc_store_label_unk(vdc_t *vdc);
14978fcd0a1Sachartre static boolean_t vdc_is_opened(vdc_t *vdc);
1501ae08745Sheppo 
1511ae08745Sheppo /* handshake with vds */
1520a55fbb7Slm66018 static int		vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver);
1533af08d82Slm66018 static int		vdc_ver_negotiation(vdc_t *vdcp);
1541ae08745Sheppo static int		vdc_init_attr_negotiation(vdc_t *vdc);
1553af08d82Slm66018 static int		vdc_attr_negotiation(vdc_t *vdcp);
1561ae08745Sheppo static int		vdc_init_dring_negotiate(vdc_t *vdc);
1573af08d82Slm66018 static int		vdc_dring_negotiation(vdc_t *vdcp);
1583af08d82Slm66018 static int		vdc_send_rdx(vdc_t *vdcp);
1593af08d82Slm66018 static int		vdc_rdx_exchange(vdc_t *vdcp);
1600a55fbb7Slm66018 static boolean_t	vdc_is_supported_version(vio_ver_msg_t *ver_msg);
1611ae08745Sheppo 
1620a55fbb7Slm66018 /* processing incoming messages from vDisk server */
1631ae08745Sheppo static void	vdc_process_msg_thread(vdc_t *vdc);
1643af08d82Slm66018 static int	vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp);
1653af08d82Slm66018 
1660a55fbb7Slm66018 static uint_t	vdc_handle_cb(uint64_t event, caddr_t arg);
1673af08d82Slm66018 static int	vdc_process_data_msg(vdc_t *vdc, vio_msg_t *msg);
1680a55fbb7Slm66018 static int	vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg);
1690a55fbb7Slm66018 static int	vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg);
1700a55fbb7Slm66018 static int	vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *msg);
1713af08d82Slm66018 static int 	vdc_send_request(vdc_t *vdcp, int operation,
1723af08d82Slm66018 		    caddr_t addr, size_t nbytes, int slice, diskaddr_t offset,
1733af08d82Slm66018 		    int cb_type, void *cb_arg, vio_desc_direction_t dir);
1743af08d82Slm66018 static int	vdc_map_to_shared_dring(vdc_t *vdcp, int idx);
1753af08d82Slm66018 static int 	vdc_populate_descriptor(vdc_t *vdcp, int operation,
1763af08d82Slm66018 		    caddr_t addr, size_t nbytes, int slice, diskaddr_t offset,
1773af08d82Slm66018 		    int cb_type, void *cb_arg, vio_desc_direction_t dir);
1782f5224aeSachartre static int 	vdc_do_sync_op(vdc_t *vdcp, int operation, caddr_t addr,
1792f5224aeSachartre 		    size_t nbytes, int slice, diskaddr_t offset, int cb_type,
1802f5224aeSachartre 		    void *cb_arg, vio_desc_direction_t dir, boolean_t);
1813af08d82Slm66018 
1823af08d82Slm66018 static int	vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp);
1833af08d82Slm66018 static int	vdc_drain_response(vdc_t *vdcp);
1841ae08745Sheppo static int	vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx);
1853af08d82Slm66018 static int	vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep);
186e1ebb9ecSlm66018 static int	vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg);
1871ae08745Sheppo 
1881ae08745Sheppo /* dkio */
1892f5224aeSachartre static int	vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode,
1902f5224aeSachartre 		    int *rvalp);
191edcc0754Sachartre static int	vd_process_efi_ioctl(void *vdisk, int cmd, uintptr_t arg);
19278fcd0a1Sachartre static void	vdc_create_fake_geometry(vdc_t *vdc);
19378fcd0a1Sachartre static int	vdc_validate_geometry(vdc_t *vdc);
19478fcd0a1Sachartre static void	vdc_validate(vdc_t *vdc);
19578fcd0a1Sachartre static void	vdc_validate_task(void *arg);
196d10e4ef2Snarayan static int	vdc_null_copy_func(vdc_t *vdc, void *from, void *to,
197d10e4ef2Snarayan 		    int mode, int dir);
1984bac2208Snarayan static int	vdc_get_wce_convert(vdc_t *vdc, void *from, void *to,
1994bac2208Snarayan 		    int mode, int dir);
2004bac2208Snarayan static int	vdc_set_wce_convert(vdc_t *vdc, void *from, void *to,
2014bac2208Snarayan 		    int mode, int dir);
202d10e4ef2Snarayan static int	vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to,
203d10e4ef2Snarayan 		    int mode, int dir);
204d10e4ef2Snarayan static int	vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to,
205d10e4ef2Snarayan 		    int mode, int dir);
206d10e4ef2Snarayan static int	vdc_get_geom_convert(vdc_t *vdc, void *from, void *to,
207d10e4ef2Snarayan 		    int mode, int dir);
208d10e4ef2Snarayan static int	vdc_set_geom_convert(vdc_t *vdc, void *from, void *to,
209d10e4ef2Snarayan 		    int mode, int dir);
2104bac2208Snarayan static int	vdc_get_efi_convert(vdc_t *vdc, void *from, void *to,
2114bac2208Snarayan 		    int mode, int dir);
2124bac2208Snarayan static int	vdc_set_efi_convert(vdc_t *vdc, void *from, void *to,
2134bac2208Snarayan 		    int mode, int dir);
2141ae08745Sheppo 
2152f5224aeSachartre static void 	vdc_ownership_update(vdc_t *vdc, int ownership_flags);
2162f5224aeSachartre static int	vdc_access_set(vdc_t *vdc, uint64_t flags, int mode);
2172f5224aeSachartre static vdc_io_t	*vdc_failfast_io_queue(vdc_t *vdc, struct buf *buf);
2182f5224aeSachartre static int	vdc_failfast_check_resv(vdc_t *vdc);
2192f5224aeSachartre 
2201ae08745Sheppo /*
2211ae08745Sheppo  * Module variables
2221ae08745Sheppo  */
223e1ebb9ecSlm66018 
224e1ebb9ecSlm66018 /*
225e1ebb9ecSlm66018  * Tunable variables to control how long vdc waits before timing out on
226e1ebb9ecSlm66018  * various operations
227e1ebb9ecSlm66018  */
2283c96341aSnarayan static int	vdc_hshake_retries = 3;
229e1ebb9ecSlm66018 
230655fd6a9Sachartre static int	vdc_timeout = 0; /* units: seconds */
231*8cd10891Snarayan static int 	vdc_ldcup_timeout = 1; /* units: seconds */
232655fd6a9Sachartre 
2333af08d82Slm66018 static uint64_t vdc_hz_min_ldc_delay;
2343af08d82Slm66018 static uint64_t vdc_min_timeout_ldc = 1 * MILLISEC;
2353af08d82Slm66018 static uint64_t vdc_hz_max_ldc_delay;
2363af08d82Slm66018 static uint64_t vdc_max_timeout_ldc = 100 * MILLISEC;
2373af08d82Slm66018 
2383af08d82Slm66018 static uint64_t vdc_ldc_read_init_delay = 1 * MILLISEC;
2393af08d82Slm66018 static uint64_t vdc_ldc_read_max_delay = 100 * MILLISEC;
240e1ebb9ecSlm66018 
241e1ebb9ecSlm66018 /* values for dumping - need to run in a tighter loop */
242e1ebb9ecSlm66018 static uint64_t	vdc_usec_timeout_dump = 100 * MILLISEC;	/* 0.1s units: ns */
243e1ebb9ecSlm66018 static int	vdc_dump_retries = 100;
244e1ebb9ecSlm66018 
2452f5224aeSachartre static uint16_t	vdc_scsi_timeout = 60;	/* 60s units: seconds  */
2462f5224aeSachartre 
2472f5224aeSachartre static uint64_t vdc_ownership_delay = 6 * MICROSEC; /* 6s units: usec */
2482f5224aeSachartre 
249e1ebb9ecSlm66018 /* Count of the number of vdc instances attached */
250e1ebb9ecSlm66018 static volatile uint32_t	vdc_instance_count = 0;
2511ae08745Sheppo 
2522f5224aeSachartre /* Tunable to log all SCSI errors */
2532f5224aeSachartre static boolean_t vdc_scsi_log_error = B_FALSE;
2542f5224aeSachartre 
2551ae08745Sheppo /* Soft state pointer */
2561ae08745Sheppo static void	*vdc_state;
2571ae08745Sheppo 
2583af08d82Slm66018 /*
2593af08d82Slm66018  * Controlling the verbosity of the error/debug messages
2603af08d82Slm66018  *
2613af08d82Slm66018  * vdc_msglevel - controls level of messages
2623af08d82Slm66018  * vdc_matchinst - 64-bit variable where each bit corresponds
2633af08d82Slm66018  *                 to the vdc instance the vdc_msglevel applies.
2643af08d82Slm66018  */
2653af08d82Slm66018 int		vdc_msglevel = 0x0;
2663af08d82Slm66018 uint64_t	vdc_matchinst = 0ull;
2671ae08745Sheppo 
2680a55fbb7Slm66018 /*
2690a55fbb7Slm66018  * Supported vDisk protocol version pairs.
2700a55fbb7Slm66018  *
2710a55fbb7Slm66018  * The first array entry is the latest and preferred version.
2720a55fbb7Slm66018  */
27317cadca8Slm66018 static const vio_ver_t	vdc_version[] = {{1, 1}};
2741ae08745Sheppo 
2751ae08745Sheppo static struct cb_ops vdc_cb_ops = {
2761ae08745Sheppo 	vdc_open,	/* cb_open */
2771ae08745Sheppo 	vdc_close,	/* cb_close */
2781ae08745Sheppo 	vdc_strategy,	/* cb_strategy */
2791ae08745Sheppo 	vdc_print,	/* cb_print */
2801ae08745Sheppo 	vdc_dump,	/* cb_dump */
2811ae08745Sheppo 	vdc_read,	/* cb_read */
2821ae08745Sheppo 	vdc_write,	/* cb_write */
2831ae08745Sheppo 	vdc_ioctl,	/* cb_ioctl */
2841ae08745Sheppo 	nodev,		/* cb_devmap */
2851ae08745Sheppo 	nodev,		/* cb_mmap */
2861ae08745Sheppo 	nodev,		/* cb_segmap */
2871ae08745Sheppo 	nochpoll,	/* cb_chpoll */
2881ae08745Sheppo 	ddi_prop_op,	/* cb_prop_op */
2891ae08745Sheppo 	NULL,		/* cb_str */
2901ae08745Sheppo 	D_MP | D_64BIT,	/* cb_flag */
2911ae08745Sheppo 	CB_REV,		/* cb_rev */
2921ae08745Sheppo 	vdc_aread,	/* cb_aread */
2931ae08745Sheppo 	vdc_awrite	/* cb_awrite */
2941ae08745Sheppo };
2951ae08745Sheppo 
2961ae08745Sheppo static struct dev_ops vdc_ops = {
2971ae08745Sheppo 	DEVO_REV,	/* devo_rev */
2981ae08745Sheppo 	0,		/* devo_refcnt */
2991ae08745Sheppo 	vdc_getinfo,	/* devo_getinfo */
3001ae08745Sheppo 	nulldev,	/* devo_identify */
3011ae08745Sheppo 	nulldev,	/* devo_probe */
3021ae08745Sheppo 	vdc_attach,	/* devo_attach */
3031ae08745Sheppo 	vdc_detach,	/* devo_detach */
3041ae08745Sheppo 	nodev,		/* devo_reset */
3051ae08745Sheppo 	&vdc_cb_ops,	/* devo_cb_ops */
3061ae08745Sheppo 	NULL,		/* devo_bus_ops */
3071ae08745Sheppo 	nulldev		/* devo_power */
3081ae08745Sheppo };
3091ae08745Sheppo 
3101ae08745Sheppo static struct modldrv modldrv = {
3111ae08745Sheppo 	&mod_driverops,
312205eeb1aSlm66018 	"virtual disk client",
3131ae08745Sheppo 	&vdc_ops,
3141ae08745Sheppo };
3151ae08745Sheppo 
3161ae08745Sheppo static struct modlinkage modlinkage = {
3171ae08745Sheppo 	MODREV_1,
3181ae08745Sheppo 	&modldrv,
3191ae08745Sheppo 	NULL
3201ae08745Sheppo };
3211ae08745Sheppo 
3221ae08745Sheppo /* -------------------------------------------------------------------------- */
3231ae08745Sheppo 
3241ae08745Sheppo /*
3251ae08745Sheppo  * Device Driver housekeeping and setup
3261ae08745Sheppo  */
3271ae08745Sheppo 
3281ae08745Sheppo int
3291ae08745Sheppo _init(void)
3301ae08745Sheppo {
3311ae08745Sheppo 	int	status;
3321ae08745Sheppo 
3331ae08745Sheppo 	if ((status = ddi_soft_state_init(&vdc_state, sizeof (vdc_t), 1)) != 0)
3341ae08745Sheppo 		return (status);
3351ae08745Sheppo 	if ((status = mod_install(&modlinkage)) != 0)
3361ae08745Sheppo 		ddi_soft_state_fini(&vdc_state);
3371ae08745Sheppo 	return (status);
3381ae08745Sheppo }
3391ae08745Sheppo 
3401ae08745Sheppo int
3411ae08745Sheppo _info(struct modinfo *modinfop)
3421ae08745Sheppo {
3431ae08745Sheppo 	return (mod_info(&modlinkage, modinfop));
3441ae08745Sheppo }
3451ae08745Sheppo 
3461ae08745Sheppo int
3471ae08745Sheppo _fini(void)
3481ae08745Sheppo {
3491ae08745Sheppo 	int	status;
3501ae08745Sheppo 
3511ae08745Sheppo 	if ((status = mod_remove(&modlinkage)) != 0)
3521ae08745Sheppo 		return (status);
3531ae08745Sheppo 	ddi_soft_state_fini(&vdc_state);
3541ae08745Sheppo 	return (0);
3551ae08745Sheppo }
3561ae08745Sheppo 
3571ae08745Sheppo static int
3581ae08745Sheppo vdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,  void *arg, void **resultp)
3591ae08745Sheppo {
3601ae08745Sheppo 	_NOTE(ARGUNUSED(dip))
3611ae08745Sheppo 
3620d0c8d4bSnarayan 	int	instance = VDCUNIT((dev_t)arg);
3631ae08745Sheppo 	vdc_t	*vdc = NULL;
3641ae08745Sheppo 
3651ae08745Sheppo 	switch (cmd) {
3661ae08745Sheppo 	case DDI_INFO_DEVT2DEVINFO:
3671ae08745Sheppo 		if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
3681ae08745Sheppo 			*resultp = NULL;
3691ae08745Sheppo 			return (DDI_FAILURE);
3701ae08745Sheppo 		}
3711ae08745Sheppo 		*resultp = vdc->dip;
3721ae08745Sheppo 		return (DDI_SUCCESS);
3731ae08745Sheppo 	case DDI_INFO_DEVT2INSTANCE:
3741ae08745Sheppo 		*resultp = (void *)(uintptr_t)instance;
3751ae08745Sheppo 		return (DDI_SUCCESS);
3761ae08745Sheppo 	default:
3771ae08745Sheppo 		*resultp = NULL;
3781ae08745Sheppo 		return (DDI_FAILURE);
3791ae08745Sheppo 	}
3801ae08745Sheppo }
3811ae08745Sheppo 
3821ae08745Sheppo static int
3831ae08745Sheppo vdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3841ae08745Sheppo {
3852f5224aeSachartre 	kt_did_t failfast_tid, ownership_tid;
3861ae08745Sheppo 	int	instance;
3871ae08745Sheppo 	int	rv;
3881ae08745Sheppo 	vdc_t	*vdc = NULL;
3891ae08745Sheppo 
3901ae08745Sheppo 	switch (cmd) {
3911ae08745Sheppo 	case DDI_DETACH:
3921ae08745Sheppo 		/* the real work happens below */
3931ae08745Sheppo 		break;
3941ae08745Sheppo 	case DDI_SUSPEND:
3951ae08745Sheppo 		/* nothing to do for this non-device */
3961ae08745Sheppo 		return (DDI_SUCCESS);
3971ae08745Sheppo 	default:
3981ae08745Sheppo 		return (DDI_FAILURE);
3991ae08745Sheppo 	}
4001ae08745Sheppo 
4011ae08745Sheppo 	ASSERT(cmd == DDI_DETACH);
4021ae08745Sheppo 	instance = ddi_get_instance(dip);
4033af08d82Slm66018 	DMSGX(1, "[%d] Entered\n", instance);
4041ae08745Sheppo 
4051ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
406e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
4071ae08745Sheppo 		return (DDI_FAILURE);
4081ae08745Sheppo 	}
4091ae08745Sheppo 
4102f5224aeSachartre 	/*
4112f5224aeSachartre 	 * This function is called when vdc is detached or if it has failed to
4122f5224aeSachartre 	 * attach. In that case, the attach may have fail before the vdisk type
4132f5224aeSachartre 	 * has been set so we can't call vdc_is_opened(). However as the attach
4142f5224aeSachartre 	 * has failed, we know that the vdisk is not opened and we can safely
4152f5224aeSachartre 	 * detach.
4162f5224aeSachartre 	 */
4172f5224aeSachartre 	if (vdc->vdisk_type != VD_DISK_TYPE_UNK && vdc_is_opened(vdc)) {
4183af08d82Slm66018 		DMSG(vdc, 0, "[%d] Cannot detach: device is open", instance);
4191ae08745Sheppo 		return (DDI_FAILURE);
4201ae08745Sheppo 	}
4211ae08745Sheppo 
42278fcd0a1Sachartre 	if (vdc->dkio_flush_pending) {
42378fcd0a1Sachartre 		DMSG(vdc, 0,
42478fcd0a1Sachartre 		    "[%d] Cannot detach: %d outstanding DKIO flushes\n",
42578fcd0a1Sachartre 		    instance, vdc->dkio_flush_pending);
42678fcd0a1Sachartre 		return (DDI_FAILURE);
42778fcd0a1Sachartre 	}
42878fcd0a1Sachartre 
42978fcd0a1Sachartre 	if (vdc->validate_pending) {
43078fcd0a1Sachartre 		DMSG(vdc, 0,
43178fcd0a1Sachartre 		    "[%d] Cannot detach: %d outstanding validate request\n",
43278fcd0a1Sachartre 		    instance, vdc->validate_pending);
43378fcd0a1Sachartre 		return (DDI_FAILURE);
43478fcd0a1Sachartre 	}
43578fcd0a1Sachartre 
4363af08d82Slm66018 	DMSG(vdc, 0, "[%d] proceeding...\n", instance);
4373af08d82Slm66018 
4382f5224aeSachartre 	/* If we took ownership, release ownership */
4392f5224aeSachartre 	mutex_enter(&vdc->ownership_lock);
4402f5224aeSachartre 	if (vdc->ownership & VDC_OWNERSHIP_GRANTED) {
4412f5224aeSachartre 		rv = vdc_access_set(vdc, VD_ACCESS_SET_CLEAR, FKIOCTL);
4422f5224aeSachartre 		if (rv == 0) {
4432f5224aeSachartre 			vdc_ownership_update(vdc, VDC_OWNERSHIP_NONE);
4442f5224aeSachartre 		}
4452f5224aeSachartre 	}
4462f5224aeSachartre 	mutex_exit(&vdc->ownership_lock);
4472f5224aeSachartre 
4483af08d82Slm66018 	/* mark instance as detaching */
4493af08d82Slm66018 	vdc->lifecycle	= VDC_LC_DETACHING;
4501ae08745Sheppo 
4511ae08745Sheppo 	/*
4521ae08745Sheppo 	 * try and disable callbacks to prevent another handshake
4531ae08745Sheppo 	 */
454*8cd10891Snarayan 	if (vdc->curr_server != NULL) {
455*8cd10891Snarayan 		rv = ldc_set_cb_mode(vdc->curr_server->ldc_handle,
456*8cd10891Snarayan 		    LDC_CB_DISABLE);
4573af08d82Slm66018 		DMSG(vdc, 0, "callback disabled (rv=%d)\n", rv);
458*8cd10891Snarayan 	}
4591ae08745Sheppo 
4601ae08745Sheppo 	if (vdc->initialized & VDC_THREAD) {
4613af08d82Slm66018 		mutex_enter(&vdc->read_lock);
4623af08d82Slm66018 		if ((vdc->read_state == VDC_READ_WAITING) ||
4633af08d82Slm66018 		    (vdc->read_state == VDC_READ_RESET)) {
4643af08d82Slm66018 			vdc->read_state = VDC_READ_RESET;
4653af08d82Slm66018 			cv_signal(&vdc->read_cv);
4661ae08745Sheppo 		}
4673af08d82Slm66018 
4683af08d82Slm66018 		mutex_exit(&vdc->read_lock);
4693af08d82Slm66018 
4703af08d82Slm66018 		/* wake up any thread waiting for connection to come online */
4713af08d82Slm66018 		mutex_enter(&vdc->lock);
4723af08d82Slm66018 		if (vdc->state == VDC_STATE_INIT_WAITING) {
4733af08d82Slm66018 			DMSG(vdc, 0,
4743af08d82Slm66018 			    "[%d] write reset - move to resetting state...\n",
4753af08d82Slm66018 			    instance);
4763af08d82Slm66018 			vdc->state = VDC_STATE_RESETTING;
4773af08d82Slm66018 			cv_signal(&vdc->initwait_cv);
4783af08d82Slm66018 		}
4793af08d82Slm66018 		mutex_exit(&vdc->lock);
4803af08d82Slm66018 
4813af08d82Slm66018 		/* now wait until state transitions to VDC_STATE_DETACH */
4823af08d82Slm66018 		thread_join(vdc->msg_proc_thr->t_did);
4833af08d82Slm66018 		ASSERT(vdc->state == VDC_STATE_DETACH);
4843af08d82Slm66018 		DMSG(vdc, 0, "[%d] Reset thread exit and join ..\n",
4853af08d82Slm66018 		    vdc->instance);
4861ae08745Sheppo 	}
4871ae08745Sheppo 
4881ae08745Sheppo 	mutex_enter(&vdc->lock);
4891ae08745Sheppo 
4901ae08745Sheppo 	if (vdc->initialized & VDC_DRING)
4911ae08745Sheppo 		vdc_destroy_descriptor_ring(vdc);
4921ae08745Sheppo 
493*8cd10891Snarayan 	vdc_fini_ports(vdc);
4941ae08745Sheppo 
4952f5224aeSachartre 	if (vdc->failfast_thread) {
4962f5224aeSachartre 		failfast_tid = vdc->failfast_thread->t_did;
4972f5224aeSachartre 		vdc->failfast_interval = 0;
4982f5224aeSachartre 		cv_signal(&vdc->failfast_cv);
4992f5224aeSachartre 	} else {
5002f5224aeSachartre 		failfast_tid = 0;
5012f5224aeSachartre 	}
5022f5224aeSachartre 
5032f5224aeSachartre 	if (vdc->ownership & VDC_OWNERSHIP_WANTED) {
5042f5224aeSachartre 		ownership_tid = vdc->ownership_thread->t_did;
5052f5224aeSachartre 		vdc->ownership = VDC_OWNERSHIP_NONE;
5062f5224aeSachartre 		cv_signal(&vdc->ownership_cv);
5072f5224aeSachartre 	} else {
5082f5224aeSachartre 		ownership_tid = 0;
5092f5224aeSachartre 	}
5102f5224aeSachartre 
5111ae08745Sheppo 	mutex_exit(&vdc->lock);
5121ae08745Sheppo 
5132f5224aeSachartre 	if (failfast_tid != 0)
5142f5224aeSachartre 		thread_join(failfast_tid);
5152f5224aeSachartre 
5162f5224aeSachartre 	if (ownership_tid != 0)
5172f5224aeSachartre 		thread_join(ownership_tid);
5182f5224aeSachartre 
5191ae08745Sheppo 	if (vdc->initialized & VDC_MINOR) {
5201ae08745Sheppo 		ddi_prop_remove_all(dip);
5211ae08745Sheppo 		ddi_remove_minor_node(dip, NULL);
5221ae08745Sheppo 	}
5231ae08745Sheppo 
524366a92acSlm66018 	if (vdc->io_stats) {
525366a92acSlm66018 		kstat_delete(vdc->io_stats);
526366a92acSlm66018 		vdc->io_stats = NULL;
527366a92acSlm66018 	}
528366a92acSlm66018 
529366a92acSlm66018 	if (vdc->err_stats) {
530366a92acSlm66018 		kstat_delete(vdc->err_stats);
531366a92acSlm66018 		vdc->err_stats = NULL;
532366a92acSlm66018 	}
533366a92acSlm66018 
5341ae08745Sheppo 	if (vdc->initialized & VDC_LOCKS) {
5351ae08745Sheppo 		mutex_destroy(&vdc->lock);
5363af08d82Slm66018 		mutex_destroy(&vdc->read_lock);
5372f5224aeSachartre 		mutex_destroy(&vdc->ownership_lock);
5383af08d82Slm66018 		cv_destroy(&vdc->initwait_cv);
5393af08d82Slm66018 		cv_destroy(&vdc->dring_free_cv);
5403af08d82Slm66018 		cv_destroy(&vdc->membind_cv);
5413af08d82Slm66018 		cv_destroy(&vdc->sync_pending_cv);
5423af08d82Slm66018 		cv_destroy(&vdc->sync_blocked_cv);
5433af08d82Slm66018 		cv_destroy(&vdc->read_cv);
5443af08d82Slm66018 		cv_destroy(&vdc->running_cv);
5452f5224aeSachartre 		cv_destroy(&vdc->ownership_cv);
5462f5224aeSachartre 		cv_destroy(&vdc->failfast_cv);
5472f5224aeSachartre 		cv_destroy(&vdc->failfast_io_cv);
5481ae08745Sheppo 	}
5491ae08745Sheppo 
5501ae08745Sheppo 	if (vdc->minfo)
5511ae08745Sheppo 		kmem_free(vdc->minfo, sizeof (struct dk_minfo));
5521ae08745Sheppo 
5531ae08745Sheppo 	if (vdc->cinfo)
5541ae08745Sheppo 		kmem_free(vdc->cinfo, sizeof (struct dk_cinfo));
5551ae08745Sheppo 
5561ae08745Sheppo 	if (vdc->vtoc)
5571ae08745Sheppo 		kmem_free(vdc->vtoc, sizeof (struct vtoc));
5581ae08745Sheppo 
55978fcd0a1Sachartre 	if (vdc->geom)
56078fcd0a1Sachartre 		kmem_free(vdc->geom, sizeof (struct dk_geom));
5610a55fbb7Slm66018 
5624bac2208Snarayan 	if (vdc->devid) {
5634bac2208Snarayan 		ddi_devid_unregister(dip);
5644bac2208Snarayan 		ddi_devid_free(vdc->devid);
5654bac2208Snarayan 	}
5664bac2208Snarayan 
5671ae08745Sheppo 	if (vdc->initialized & VDC_SOFT_STATE)
5681ae08745Sheppo 		ddi_soft_state_free(vdc_state, instance);
5691ae08745Sheppo 
5703af08d82Slm66018 	DMSG(vdc, 0, "[%d] End %p\n", instance, (void *)vdc);
5711ae08745Sheppo 
5721ae08745Sheppo 	return (DDI_SUCCESS);
5731ae08745Sheppo }
5741ae08745Sheppo 
5751ae08745Sheppo 
5761ae08745Sheppo static int
5771ae08745Sheppo vdc_do_attach(dev_info_t *dip)
5781ae08745Sheppo {
5791ae08745Sheppo 	int		instance;
5801ae08745Sheppo 	vdc_t		*vdc = NULL;
5811ae08745Sheppo 	int		status;
582655fd6a9Sachartre 	md_t		*mdp;
583*8cd10891Snarayan 	mde_cookie_t	vd_node;
5841ae08745Sheppo 
5851ae08745Sheppo 	ASSERT(dip != NULL);
5861ae08745Sheppo 
5871ae08745Sheppo 	instance = ddi_get_instance(dip);
5881ae08745Sheppo 	if (ddi_soft_state_zalloc(vdc_state, instance) != DDI_SUCCESS) {
589e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't alloc state structure",
590e1ebb9ecSlm66018 		    instance);
5911ae08745Sheppo 		return (DDI_FAILURE);
5921ae08745Sheppo 	}
5931ae08745Sheppo 
5941ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
595e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
5961ae08745Sheppo 		return (DDI_FAILURE);
5971ae08745Sheppo 	}
5981ae08745Sheppo 
5991ae08745Sheppo 	/*
6001ae08745Sheppo 	 * We assign the value to initialized in this case to zero out the
6011ae08745Sheppo 	 * variable and then set bits in it to indicate what has been done
6021ae08745Sheppo 	 */
6031ae08745Sheppo 	vdc->initialized = VDC_SOFT_STATE;
6041ae08745Sheppo 
6053af08d82Slm66018 	vdc_hz_min_ldc_delay = drv_usectohz(vdc_min_timeout_ldc);
6063af08d82Slm66018 	vdc_hz_max_ldc_delay = drv_usectohz(vdc_max_timeout_ldc);
6071ae08745Sheppo 
6081ae08745Sheppo 	vdc->dip	= dip;
6091ae08745Sheppo 	vdc->instance	= instance;
6101ae08745Sheppo 	vdc->vdisk_type	= VD_DISK_TYPE_UNK;
6114bac2208Snarayan 	vdc->vdisk_label = VD_DISK_LABEL_UNK;
6123af08d82Slm66018 	vdc->state	= VDC_STATE_INIT;
6133af08d82Slm66018 	vdc->lifecycle	= VDC_LC_ATTACHING;
6141ae08745Sheppo 	vdc->session_id = 0;
6151ae08745Sheppo 	vdc->block_size = DEV_BSIZE;
6168e6a2a04Slm66018 	vdc->max_xfer_sz = maxphys / DEV_BSIZE;
6171ae08745Sheppo 
61817cadca8Slm66018 	/*
61917cadca8Slm66018 	 * We assume, for now, that the vDisk server will export 'read'
62017cadca8Slm66018 	 * operations to us at a minimum (this is needed because of checks
62117cadca8Slm66018 	 * in vdc for supported operations early in the handshake process).
62217cadca8Slm66018 	 * The vDisk server will return ENOTSUP if this is not the case.
62317cadca8Slm66018 	 * The value will be overwritten during the attribute exchange with
62417cadca8Slm66018 	 * the bitmask of operations exported by server.
62517cadca8Slm66018 	 */
62617cadca8Slm66018 	vdc->operations = VD_OP_MASK_READ;
62717cadca8Slm66018 
6281ae08745Sheppo 	vdc->vtoc = NULL;
62978fcd0a1Sachartre 	vdc->geom = NULL;
6301ae08745Sheppo 	vdc->cinfo = NULL;
6311ae08745Sheppo 	vdc->minfo = NULL;
6321ae08745Sheppo 
6331ae08745Sheppo 	mutex_init(&vdc->lock, NULL, MUTEX_DRIVER, NULL);
6343af08d82Slm66018 	cv_init(&vdc->initwait_cv, NULL, CV_DRIVER, NULL);
6353af08d82Slm66018 	cv_init(&vdc->dring_free_cv, NULL, CV_DRIVER, NULL);
6363af08d82Slm66018 	cv_init(&vdc->membind_cv, NULL, CV_DRIVER, NULL);
6373af08d82Slm66018 	cv_init(&vdc->running_cv, NULL, CV_DRIVER, NULL);
6383af08d82Slm66018 
6393af08d82Slm66018 	vdc->threads_pending = 0;
6403af08d82Slm66018 	vdc->sync_op_pending = B_FALSE;
6413af08d82Slm66018 	vdc->sync_op_blocked = B_FALSE;
6423af08d82Slm66018 	cv_init(&vdc->sync_pending_cv, NULL, CV_DRIVER, NULL);
6433af08d82Slm66018 	cv_init(&vdc->sync_blocked_cv, NULL, CV_DRIVER, NULL);
6443af08d82Slm66018 
6452f5224aeSachartre 	mutex_init(&vdc->ownership_lock, NULL, MUTEX_DRIVER, NULL);
6462f5224aeSachartre 	cv_init(&vdc->ownership_cv, NULL, CV_DRIVER, NULL);
6472f5224aeSachartre 	cv_init(&vdc->failfast_cv, NULL, CV_DRIVER, NULL);
6482f5224aeSachartre 	cv_init(&vdc->failfast_io_cv, NULL, CV_DRIVER, NULL);
6492f5224aeSachartre 
6503af08d82Slm66018 	/* init blocking msg read functionality */
6513af08d82Slm66018 	mutex_init(&vdc->read_lock, NULL, MUTEX_DRIVER, NULL);
6523af08d82Slm66018 	cv_init(&vdc->read_cv, NULL, CV_DRIVER, NULL);
6533af08d82Slm66018 	vdc->read_state = VDC_READ_IDLE;
6543af08d82Slm66018 
6551ae08745Sheppo 	vdc->initialized |= VDC_LOCKS;
6561ae08745Sheppo 
657655fd6a9Sachartre 	/* get device and port MD node for this disk instance */
658*8cd10891Snarayan 	if (vdc_get_md_node(dip, &mdp, &vd_node) != 0) {
659655fd6a9Sachartre 		cmn_err(CE_NOTE, "[%d] Could not get machine description node",
660655fd6a9Sachartre 		    instance);
661655fd6a9Sachartre 		return (DDI_FAILURE);
662655fd6a9Sachartre 	}
663655fd6a9Sachartre 
664*8cd10891Snarayan 	if (vdc_init_ports(vdc, mdp, vd_node) != 0) {
665*8cd10891Snarayan 		cmn_err(CE_NOTE, "[%d] Error initialising ports", instance);
666*8cd10891Snarayan 		return (DDI_FAILURE);
667655fd6a9Sachartre 	}
668655fd6a9Sachartre 
669655fd6a9Sachartre 	(void) md_fini_handle(mdp);
670655fd6a9Sachartre 
6713af08d82Slm66018 	/* initialize the thread responsible for managing state with server */
6723af08d82Slm66018 	vdc->msg_proc_thr = thread_create(NULL, 0, vdc_process_msg_thread,
6731ae08745Sheppo 	    vdc, 0, &p0, TS_RUN, minclsyspri);
6743af08d82Slm66018 	if (vdc->msg_proc_thr == NULL) {
6751ae08745Sheppo 		cmn_err(CE_NOTE, "[%d] Failed to create msg processing thread",
6761ae08745Sheppo 		    instance);
6771ae08745Sheppo 		return (DDI_FAILURE);
6781ae08745Sheppo 	}
6793af08d82Slm66018 
6801ae08745Sheppo 	vdc->initialized |= VDC_THREAD;
6811ae08745Sheppo 
682366a92acSlm66018 	/* Create the kstats for saving the I/O statistics used by iostat(1M) */
683366a92acSlm66018 	vdc_create_io_kstats(vdc);
684366a92acSlm66018 	vdc_create_err_kstats(vdc);
685366a92acSlm66018 
686e1ebb9ecSlm66018 	atomic_inc_32(&vdc_instance_count);
6871ae08745Sheppo 
6880a55fbb7Slm66018 	/*
68978fcd0a1Sachartre 	 * Check the disk label. This will send requests and do the handshake.
69078fcd0a1Sachartre 	 * We don't really care about the disk label now. What we really need is
69178fcd0a1Sachartre 	 * the handshake do be done so that we know the type of the disk (slice
69278fcd0a1Sachartre 	 * or full disk) and the appropriate device nodes can be created.
6930a55fbb7Slm66018 	 */
69478fcd0a1Sachartre 	vdc->vdisk_label = VD_DISK_LABEL_UNK;
69578fcd0a1Sachartre 	vdc->vtoc = kmem_zalloc(sizeof (struct vtoc), KM_SLEEP);
69678fcd0a1Sachartre 	vdc->geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
69717cadca8Slm66018 	vdc->minfo = kmem_zalloc(sizeof (struct dk_minfo), KM_SLEEP);
69878fcd0a1Sachartre 
69978fcd0a1Sachartre 	mutex_enter(&vdc->lock);
70078fcd0a1Sachartre 	(void) vdc_validate_geometry(vdc);
70178fcd0a1Sachartre 	mutex_exit(&vdc->lock);
7021ae08745Sheppo 
7031ae08745Sheppo 	/*
7041ae08745Sheppo 	 * Now that we have the device info we can create the
7051ae08745Sheppo 	 * device nodes and properties
7061ae08745Sheppo 	 */
7071ae08745Sheppo 	status = vdc_create_device_nodes(vdc);
7081ae08745Sheppo 	if (status) {
7093af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to create device nodes",
7101ae08745Sheppo 		    instance);
7113af08d82Slm66018 		goto return_status;
7121ae08745Sheppo 	}
7131ae08745Sheppo 	status = vdc_create_device_nodes_props(vdc);
7141ae08745Sheppo 	if (status) {
7153af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to create device nodes"
7160a55fbb7Slm66018 		    " properties (%d)", instance, status);
7173af08d82Slm66018 		goto return_status;
7181ae08745Sheppo 	}
7191ae08745Sheppo 
7204bac2208Snarayan 	/*
7214bac2208Snarayan 	 * Setup devid
7224bac2208Snarayan 	 */
7234bac2208Snarayan 	if (vdc_setup_devid(vdc)) {
7243af08d82Slm66018 		DMSG(vdc, 0, "[%d] No device id available\n", instance);
7254bac2208Snarayan 	}
7264bac2208Snarayan 
727366a92acSlm66018 	/*
728366a92acSlm66018 	 * Fill in the fields of the error statistics kstat that were not
729366a92acSlm66018 	 * available when creating the kstat
730366a92acSlm66018 	 */
731366a92acSlm66018 	vdc_set_err_kstats(vdc);
732366a92acSlm66018 
7331ae08745Sheppo 	ddi_report_dev(dip);
7343af08d82Slm66018 	vdc->lifecycle	= VDC_LC_ONLINE;
7353af08d82Slm66018 	DMSG(vdc, 0, "[%d] Attach tasks successful\n", instance);
7361ae08745Sheppo 
7373af08d82Slm66018 return_status:
7383af08d82Slm66018 	DMSG(vdc, 0, "[%d] Attach completed\n", instance);
7391ae08745Sheppo 	return (status);
7401ae08745Sheppo }
7411ae08745Sheppo 
7421ae08745Sheppo static int
7431ae08745Sheppo vdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
7441ae08745Sheppo {
7451ae08745Sheppo 	int	status;
7461ae08745Sheppo 
7471ae08745Sheppo 	switch (cmd) {
7481ae08745Sheppo 	case DDI_ATTACH:
7491ae08745Sheppo 		if ((status = vdc_do_attach(dip)) != 0)
7501ae08745Sheppo 			(void) vdc_detach(dip, DDI_DETACH);
7511ae08745Sheppo 		return (status);
7521ae08745Sheppo 	case DDI_RESUME:
7531ae08745Sheppo 		/* nothing to do for this non-device */
7541ae08745Sheppo 		return (DDI_SUCCESS);
7551ae08745Sheppo 	default:
7561ae08745Sheppo 		return (DDI_FAILURE);
7571ae08745Sheppo 	}
7581ae08745Sheppo }
7591ae08745Sheppo 
7601ae08745Sheppo static int
761*8cd10891Snarayan vdc_do_ldc_init(vdc_t *vdc, vdc_server_t *srvr)
7621ae08745Sheppo {
7631ae08745Sheppo 	int			status = 0;
7641ae08745Sheppo 	ldc_status_t		ldc_state;
7651ae08745Sheppo 	ldc_attr_t		ldc_attr;
7661ae08745Sheppo 
7671ae08745Sheppo 	ASSERT(vdc != NULL);
768*8cd10891Snarayan 	ASSERT(srvr != NULL);
7691ae08745Sheppo 
7701ae08745Sheppo 	ldc_attr.devclass = LDC_DEV_BLK;
7711ae08745Sheppo 	ldc_attr.instance = vdc->instance;
7721ae08745Sheppo 	ldc_attr.mode = LDC_MODE_UNRELIABLE;	/* unreliable transport */
773e1ebb9ecSlm66018 	ldc_attr.mtu = VD_LDC_MTU;
7741ae08745Sheppo 
775*8cd10891Snarayan 	if ((srvr->state & VDC_LDC_INIT) == 0) {
776*8cd10891Snarayan 		status = ldc_init(srvr->ldc_id, &ldc_attr,
777*8cd10891Snarayan 		    &srvr->ldc_handle);
7781ae08745Sheppo 		if (status != 0) {
7793af08d82Slm66018 			DMSG(vdc, 0, "[%d] ldc_init(chan %ld) returned %d",
780*8cd10891Snarayan 			    vdc->instance, srvr->ldc_id, status);
7811ae08745Sheppo 			return (status);
7821ae08745Sheppo 		}
783*8cd10891Snarayan 		srvr->state |= VDC_LDC_INIT;
7841ae08745Sheppo 	}
785*8cd10891Snarayan 	status = ldc_status(srvr->ldc_handle, &ldc_state);
7861ae08745Sheppo 	if (status != 0) {
7873af08d82Slm66018 		DMSG(vdc, 0, "[%d] Cannot discover LDC status [err=%d]",
788e1ebb9ecSlm66018 		    vdc->instance, status);
789*8cd10891Snarayan 		goto init_exit;
7901ae08745Sheppo 	}
791*8cd10891Snarayan 	srvr->ldc_state = ldc_state;
7921ae08745Sheppo 
793*8cd10891Snarayan 	if ((srvr->state & VDC_LDC_CB) == 0) {
794*8cd10891Snarayan 		status = ldc_reg_callback(srvr->ldc_handle, vdc_handle_cb,
795*8cd10891Snarayan 		    (caddr_t)srvr);
7961ae08745Sheppo 		if (status != 0) {
7973af08d82Slm66018 			DMSG(vdc, 0, "[%d] LDC callback reg. failed (%d)",
798e1ebb9ecSlm66018 			    vdc->instance, status);
799*8cd10891Snarayan 			goto init_exit;
8001ae08745Sheppo 		}
801*8cd10891Snarayan 		srvr->state |= VDC_LDC_CB;
8021ae08745Sheppo 	}
8031ae08745Sheppo 
8041ae08745Sheppo 	/*
8051ae08745Sheppo 	 * At this stage we have initialised LDC, we will now try and open
8061ae08745Sheppo 	 * the connection.
8071ae08745Sheppo 	 */
808*8cd10891Snarayan 	if (srvr->ldc_state == LDC_INIT) {
809*8cd10891Snarayan 		status = ldc_open(srvr->ldc_handle);
8101ae08745Sheppo 		if (status != 0) {
8113af08d82Slm66018 			DMSG(vdc, 0, "[%d] ldc_open(chan %ld) returned %d",
812*8cd10891Snarayan 			    vdc->instance, srvr->ldc_id, status);
813*8cd10891Snarayan 			goto init_exit;
8141ae08745Sheppo 		}
815*8cd10891Snarayan 		srvr->state |= VDC_LDC_OPEN;
816*8cd10891Snarayan 	}
817*8cd10891Snarayan 
818*8cd10891Snarayan init_exit:
819*8cd10891Snarayan 	if (status) {
820*8cd10891Snarayan 		vdc_terminate_ldc(vdc, srvr);
8211ae08745Sheppo 	}
8221ae08745Sheppo 
8231ae08745Sheppo 	return (status);
8241ae08745Sheppo }
8251ae08745Sheppo 
8261ae08745Sheppo static int
8271ae08745Sheppo vdc_start_ldc_connection(vdc_t *vdc)
8281ae08745Sheppo {
8291ae08745Sheppo 	int		status = 0;
8301ae08745Sheppo 
8311ae08745Sheppo 	ASSERT(vdc != NULL);
8321ae08745Sheppo 
8333af08d82Slm66018 	ASSERT(MUTEX_HELD(&vdc->lock));
8341ae08745Sheppo 
8350a55fbb7Slm66018 	status = vdc_do_ldc_up(vdc);
8361ae08745Sheppo 
8373af08d82Slm66018 	DMSG(vdc, 0, "[%d] Finished bringing up LDC\n", vdc->instance);
8381ae08745Sheppo 
8393af08d82Slm66018 	return (status);
8403af08d82Slm66018 }
8413af08d82Slm66018 
8423af08d82Slm66018 static int
8433af08d82Slm66018 vdc_stop_ldc_connection(vdc_t *vdcp)
8443af08d82Slm66018 {
8453af08d82Slm66018 	int	status;
8463af08d82Slm66018 
847*8cd10891Snarayan 	ASSERT(vdcp != NULL);
848*8cd10891Snarayan 
849*8cd10891Snarayan 	ASSERT(MUTEX_HELD(&vdcp->lock));
850*8cd10891Snarayan 
8513af08d82Slm66018 	DMSG(vdcp, 0, ": Resetting connection to vDisk server : state %d\n",
8523af08d82Slm66018 	    vdcp->state);
8533af08d82Slm66018 
854*8cd10891Snarayan 	status = ldc_down(vdcp->curr_server->ldc_handle);
8553af08d82Slm66018 	DMSG(vdcp, 0, "ldc_down() = %d\n", status);
8563af08d82Slm66018 
8573af08d82Slm66018 	vdcp->initialized &= ~VDC_HANDSHAKE;
8583af08d82Slm66018 	DMSG(vdcp, 0, "initialized=%x\n", vdcp->initialized);
8591ae08745Sheppo 
8601ae08745Sheppo 	return (status);
8611ae08745Sheppo }
8621ae08745Sheppo 
863366a92acSlm66018 static void
864366a92acSlm66018 vdc_create_io_kstats(vdc_t *vdc)
865366a92acSlm66018 {
866366a92acSlm66018 	if (vdc->io_stats != NULL) {
867366a92acSlm66018 		DMSG(vdc, 0, "[%d] I/O kstat already exists\n", vdc->instance);
868366a92acSlm66018 		return;
869366a92acSlm66018 	}
870366a92acSlm66018 
871366a92acSlm66018 	vdc->io_stats = kstat_create(VDC_DRIVER_NAME, vdc->instance, NULL,
872366a92acSlm66018 	    "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
873366a92acSlm66018 	if (vdc->io_stats != NULL) {
874366a92acSlm66018 		vdc->io_stats->ks_lock = &vdc->lock;
875366a92acSlm66018 		kstat_install(vdc->io_stats);
876366a92acSlm66018 	} else {
877366a92acSlm66018 		cmn_err(CE_NOTE, "[%d] Failed to create kstat: I/O statistics"
878366a92acSlm66018 		    " will not be gathered", vdc->instance);
879366a92acSlm66018 	}
880366a92acSlm66018 }
881366a92acSlm66018 
882366a92acSlm66018 static void
883366a92acSlm66018 vdc_create_err_kstats(vdc_t *vdc)
884366a92acSlm66018 {
885366a92acSlm66018 	vd_err_stats_t	*stp;
886366a92acSlm66018 	char	kstatmodule_err[KSTAT_STRLEN];
887366a92acSlm66018 	char	kstatname[KSTAT_STRLEN];
888366a92acSlm66018 	int	ndata = (sizeof (vd_err_stats_t) / sizeof (kstat_named_t));
889366a92acSlm66018 	int	instance = vdc->instance;
890366a92acSlm66018 
891366a92acSlm66018 	if (vdc->err_stats != NULL) {
892366a92acSlm66018 		DMSG(vdc, 0, "[%d] ERR kstat already exists\n", vdc->instance);
893366a92acSlm66018 		return;
894366a92acSlm66018 	}
895366a92acSlm66018 
896366a92acSlm66018 	(void) snprintf(kstatmodule_err, sizeof (kstatmodule_err),
897366a92acSlm66018 	    "%serr", VDC_DRIVER_NAME);
898366a92acSlm66018 	(void) snprintf(kstatname, sizeof (kstatname),
899366a92acSlm66018 	    "%s%d,err", VDC_DRIVER_NAME, instance);
900366a92acSlm66018 
901366a92acSlm66018 	vdc->err_stats = kstat_create(kstatmodule_err, instance, kstatname,
902366a92acSlm66018 	    "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT);
903366a92acSlm66018 
904366a92acSlm66018 	if (vdc->err_stats == NULL) {
905366a92acSlm66018 		cmn_err(CE_NOTE, "[%d] Failed to create kstat: Error statistics"
906366a92acSlm66018 		    " will not be gathered", instance);
907366a92acSlm66018 		return;
908366a92acSlm66018 	}
909366a92acSlm66018 
910366a92acSlm66018 	stp = (vd_err_stats_t *)vdc->err_stats->ks_data;
911366a92acSlm66018 	kstat_named_init(&stp->vd_softerrs,	"Soft Errors",
912366a92acSlm66018 	    KSTAT_DATA_UINT32);
913366a92acSlm66018 	kstat_named_init(&stp->vd_transerrs,	"Transport Errors",
914366a92acSlm66018 	    KSTAT_DATA_UINT32);
915366a92acSlm66018 	kstat_named_init(&stp->vd_protoerrs,	"Protocol Errors",
916366a92acSlm66018 	    KSTAT_DATA_UINT32);
917366a92acSlm66018 	kstat_named_init(&stp->vd_vid,		"Vendor",
918366a92acSlm66018 	    KSTAT_DATA_CHAR);
919366a92acSlm66018 	kstat_named_init(&stp->vd_pid,		"Product",
920366a92acSlm66018 	    KSTAT_DATA_CHAR);
921366a92acSlm66018 	kstat_named_init(&stp->vd_capacity,	"Size",
922366a92acSlm66018 	    KSTAT_DATA_ULONGLONG);
923366a92acSlm66018 
924366a92acSlm66018 	vdc->err_stats->ks_update  = nulldev;
925366a92acSlm66018 
926366a92acSlm66018 	kstat_install(vdc->err_stats);
927366a92acSlm66018 }
928366a92acSlm66018 
929366a92acSlm66018 static void
930366a92acSlm66018 vdc_set_err_kstats(vdc_t *vdc)
931366a92acSlm66018 {
932366a92acSlm66018 	vd_err_stats_t  *stp;
933366a92acSlm66018 
934366a92acSlm66018 	if (vdc->err_stats == NULL)
935366a92acSlm66018 		return;
936366a92acSlm66018 
937366a92acSlm66018 	mutex_enter(&vdc->lock);
938366a92acSlm66018 
939366a92acSlm66018 	stp = (vd_err_stats_t *)vdc->err_stats->ks_data;
940366a92acSlm66018 	ASSERT(stp != NULL);
941366a92acSlm66018 
942366a92acSlm66018 	stp->vd_capacity.value.ui64 = vdc->vdisk_size * vdc->block_size;
943366a92acSlm66018 	(void) strcpy(stp->vd_vid.value.c, "SUN");
944366a92acSlm66018 	(void) strcpy(stp->vd_pid.value.c, "VDSK");
945366a92acSlm66018 
946366a92acSlm66018 	mutex_exit(&vdc->lock);
947366a92acSlm66018 }
948366a92acSlm66018 
9494bac2208Snarayan static int
9504bac2208Snarayan vdc_create_device_nodes_efi(vdc_t *vdc)
9514bac2208Snarayan {
9524bac2208Snarayan 	ddi_remove_minor_node(vdc->dip, "h");
9534bac2208Snarayan 	ddi_remove_minor_node(vdc->dip, "h,raw");
9544bac2208Snarayan 
9554bac2208Snarayan 	if (ddi_create_minor_node(vdc->dip, "wd", S_IFBLK,
9564bac2208Snarayan 	    VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
9574bac2208Snarayan 	    DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
9584bac2208Snarayan 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd'",
9594bac2208Snarayan 		    vdc->instance);
9604bac2208Snarayan 		return (EIO);
9614bac2208Snarayan 	}
9624bac2208Snarayan 
9634bac2208Snarayan 	/* if any device node is created we set this flag */
9644bac2208Snarayan 	vdc->initialized |= VDC_MINOR;
9654bac2208Snarayan 
9664bac2208Snarayan 	if (ddi_create_minor_node(vdc->dip, "wd,raw", S_IFCHR,
9674bac2208Snarayan 	    VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
9684bac2208Snarayan 	    DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
9694bac2208Snarayan 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd,raw'",
9704bac2208Snarayan 		    vdc->instance);
9714bac2208Snarayan 		return (EIO);
9724bac2208Snarayan 	}
9734bac2208Snarayan 
9744bac2208Snarayan 	return (0);
9754bac2208Snarayan }
9764bac2208Snarayan 
9774bac2208Snarayan static int
9784bac2208Snarayan vdc_create_device_nodes_vtoc(vdc_t *vdc)
9794bac2208Snarayan {
9804bac2208Snarayan 	ddi_remove_minor_node(vdc->dip, "wd");
9814bac2208Snarayan 	ddi_remove_minor_node(vdc->dip, "wd,raw");
9824bac2208Snarayan 
9834bac2208Snarayan 	if (ddi_create_minor_node(vdc->dip, "h", S_IFBLK,
9844bac2208Snarayan 	    VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
9854bac2208Snarayan 	    DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
9864bac2208Snarayan 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h'",
9874bac2208Snarayan 		    vdc->instance);
9884bac2208Snarayan 		return (EIO);
9894bac2208Snarayan 	}
9904bac2208Snarayan 
9914bac2208Snarayan 	/* if any device node is created we set this flag */
9924bac2208Snarayan 	vdc->initialized |= VDC_MINOR;
9934bac2208Snarayan 
9944bac2208Snarayan 	if (ddi_create_minor_node(vdc->dip, "h,raw", S_IFCHR,
9954bac2208Snarayan 	    VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
9964bac2208Snarayan 	    DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
9974bac2208Snarayan 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h,raw'",
9984bac2208Snarayan 		    vdc->instance);
9994bac2208Snarayan 		return (EIO);
10004bac2208Snarayan 	}
10014bac2208Snarayan 
10024bac2208Snarayan 	return (0);
10034bac2208Snarayan }
10041ae08745Sheppo 
10051ae08745Sheppo /*
10061ae08745Sheppo  * Function:
10071ae08745Sheppo  *	vdc_create_device_nodes
10081ae08745Sheppo  *
10091ae08745Sheppo  * Description:
10101ae08745Sheppo  *	This function creates the block and character device nodes under
10111ae08745Sheppo  *	/devices along with the node properties. It is called as part of
10121ae08745Sheppo  *	the attach(9E) of the instance during the handshake with vds after
10131ae08745Sheppo  *	vds has sent the attributes to vdc.
10141ae08745Sheppo  *
10151ae08745Sheppo  *	If the device is of type VD_DISK_TYPE_SLICE then the minor node
10161ae08745Sheppo  *	of 2 is used in keeping with the Solaris convention that slice 2
10171ae08745Sheppo  *	refers to a whole disk. Slices start at 'a'
10181ae08745Sheppo  *
10191ae08745Sheppo  * Parameters:
10201ae08745Sheppo  *	vdc 		- soft state pointer
10211ae08745Sheppo  *
10221ae08745Sheppo  * Return Values
10231ae08745Sheppo  *	0		- Success
10241ae08745Sheppo  *	EIO		- Failed to create node
10251ae08745Sheppo  *	EINVAL		- Unknown type of disk exported
10261ae08745Sheppo  */
10271ae08745Sheppo static int
10281ae08745Sheppo vdc_create_device_nodes(vdc_t *vdc)
10291ae08745Sheppo {
10304bac2208Snarayan 	char		name[sizeof ("s,raw")];
10311ae08745Sheppo 	dev_info_t	*dip = NULL;
10324bac2208Snarayan 	int		instance, status;
10331ae08745Sheppo 	int		num_slices = 1;
10341ae08745Sheppo 	int		i;
10351ae08745Sheppo 
10361ae08745Sheppo 	ASSERT(vdc != NULL);
10371ae08745Sheppo 
10381ae08745Sheppo 	instance = vdc->instance;
10391ae08745Sheppo 	dip = vdc->dip;
10401ae08745Sheppo 
10411ae08745Sheppo 	switch (vdc->vdisk_type) {
10421ae08745Sheppo 	case VD_DISK_TYPE_DISK:
10431ae08745Sheppo 		num_slices = V_NUMPAR;
10441ae08745Sheppo 		break;
10451ae08745Sheppo 	case VD_DISK_TYPE_SLICE:
10461ae08745Sheppo 		num_slices = 1;
10471ae08745Sheppo 		break;
10481ae08745Sheppo 	case VD_DISK_TYPE_UNK:
10491ae08745Sheppo 	default:
10501ae08745Sheppo 		return (EINVAL);
10511ae08745Sheppo 	}
10521ae08745Sheppo 
10534bac2208Snarayan 	/*
10544bac2208Snarayan 	 * Minor nodes are different for EFI disks: EFI disks do not have
10554bac2208Snarayan 	 * a minor node 'g' for the minor number corresponding to slice
10564bac2208Snarayan 	 * VD_EFI_WD_SLICE (slice 7) instead they have a minor node 'wd'
10574bac2208Snarayan 	 * representing the whole disk.
10584bac2208Snarayan 	 */
10591ae08745Sheppo 	for (i = 0; i < num_slices; i++) {
10604bac2208Snarayan 
10614bac2208Snarayan 		if (i == VD_EFI_WD_SLICE) {
10624bac2208Snarayan 			if (vdc->vdisk_label == VD_DISK_LABEL_EFI)
10634bac2208Snarayan 				status = vdc_create_device_nodes_efi(vdc);
10644bac2208Snarayan 			else
10654bac2208Snarayan 				status = vdc_create_device_nodes_vtoc(vdc);
10664bac2208Snarayan 			if (status != 0)
10674bac2208Snarayan 				return (status);
10684bac2208Snarayan 			continue;
10694bac2208Snarayan 		}
10704bac2208Snarayan 
10711ae08745Sheppo 		(void) snprintf(name, sizeof (name), "%c", 'a' + i);
10721ae08745Sheppo 		if (ddi_create_minor_node(dip, name, S_IFBLK,
10731ae08745Sheppo 		    VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
1074e1ebb9ecSlm66018 			cmn_err(CE_NOTE, "[%d] Couldn't add block node '%s'",
1075e1ebb9ecSlm66018 			    instance, name);
10761ae08745Sheppo 			return (EIO);
10771ae08745Sheppo 		}
10781ae08745Sheppo 
10791ae08745Sheppo 		/* if any device node is created we set this flag */
10801ae08745Sheppo 		vdc->initialized |= VDC_MINOR;
10811ae08745Sheppo 
108287a7269eSachartre 		(void) snprintf(name, sizeof (name), "%c%s", 'a' + i, ",raw");
108387a7269eSachartre 
10841ae08745Sheppo 		if (ddi_create_minor_node(dip, name, S_IFCHR,
10851ae08745Sheppo 		    VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
1086e1ebb9ecSlm66018 			cmn_err(CE_NOTE, "[%d] Couldn't add raw node '%s'",
1087e1ebb9ecSlm66018 			    instance, name);
10881ae08745Sheppo 			return (EIO);
10891ae08745Sheppo 		}
10901ae08745Sheppo 	}
10911ae08745Sheppo 
10921ae08745Sheppo 	return (0);
10931ae08745Sheppo }
10941ae08745Sheppo 
10951ae08745Sheppo /*
10961ae08745Sheppo  * Function:
10971ae08745Sheppo  *	vdc_create_device_nodes_props
10981ae08745Sheppo  *
10991ae08745Sheppo  * Description:
11001ae08745Sheppo  *	This function creates the block and character device nodes under
11011ae08745Sheppo  *	/devices along with the node properties. It is called as part of
11021ae08745Sheppo  *	the attach(9E) of the instance during the handshake with vds after
11031ae08745Sheppo  *	vds has sent the attributes to vdc.
11041ae08745Sheppo  *
11051ae08745Sheppo  * Parameters:
11061ae08745Sheppo  *	vdc 		- soft state pointer
11071ae08745Sheppo  *
11081ae08745Sheppo  * Return Values
11091ae08745Sheppo  *	0		- Success
11101ae08745Sheppo  *	EIO		- Failed to create device node property
11111ae08745Sheppo  *	EINVAL		- Unknown type of disk exported
11121ae08745Sheppo  */
11131ae08745Sheppo static int
11141ae08745Sheppo vdc_create_device_nodes_props(vdc_t *vdc)
11151ae08745Sheppo {
11161ae08745Sheppo 	dev_info_t	*dip = NULL;
11171ae08745Sheppo 	int		instance;
11181ae08745Sheppo 	int		num_slices = 1;
11191ae08745Sheppo 	int64_t		size = 0;
11201ae08745Sheppo 	dev_t		dev;
11211ae08745Sheppo 	int		rv;
11221ae08745Sheppo 	int		i;
11231ae08745Sheppo 
11241ae08745Sheppo 	ASSERT(vdc != NULL);
11251ae08745Sheppo 
11261ae08745Sheppo 	instance = vdc->instance;
11271ae08745Sheppo 	dip = vdc->dip;
11281ae08745Sheppo 
11291ae08745Sheppo 	switch (vdc->vdisk_type) {
11301ae08745Sheppo 	case VD_DISK_TYPE_DISK:
11311ae08745Sheppo 		num_slices = V_NUMPAR;
11321ae08745Sheppo 		break;
11331ae08745Sheppo 	case VD_DISK_TYPE_SLICE:
11341ae08745Sheppo 		num_slices = 1;
11351ae08745Sheppo 		break;
11361ae08745Sheppo 	case VD_DISK_TYPE_UNK:
11371ae08745Sheppo 	default:
11381ae08745Sheppo 		return (EINVAL);
11391ae08745Sheppo 	}
11401ae08745Sheppo 
114178fcd0a1Sachartre 	if (vdc->vdisk_label == VD_DISK_LABEL_UNK) {
114278fcd0a1Sachartre 		/* remove all properties */
114378fcd0a1Sachartre 		for (i = 0; i < num_slices; i++) {
114478fcd0a1Sachartre 			dev = makedevice(ddi_driver_major(dip),
114578fcd0a1Sachartre 			    VD_MAKE_DEV(instance, i));
114678fcd0a1Sachartre 			(void) ddi_prop_remove(dev, dip, VDC_SIZE_PROP_NAME);
114778fcd0a1Sachartre 			(void) ddi_prop_remove(dev, dip, VDC_NBLOCKS_PROP_NAME);
114878fcd0a1Sachartre 		}
114978fcd0a1Sachartre 		return (0);
115078fcd0a1Sachartre 	}
115178fcd0a1Sachartre 
11521ae08745Sheppo 	for (i = 0; i < num_slices; i++) {
11531ae08745Sheppo 		dev = makedevice(ddi_driver_major(dip),
11541ae08745Sheppo 		    VD_MAKE_DEV(instance, i));
11551ae08745Sheppo 
1156edcc0754Sachartre 		size = vdc->slice[i].nblocks * vdc->block_size;
11573af08d82Slm66018 		DMSG(vdc, 0, "[%d] sz %ld (%ld Mb)  p_size %lx\n",
1158e1ebb9ecSlm66018 		    instance, size, size / (1024 * 1024),
1159edcc0754Sachartre 		    vdc->slice[i].nblocks);
11601ae08745Sheppo 
11611ae08745Sheppo 		rv = ddi_prop_update_int64(dev, dip, VDC_SIZE_PROP_NAME, size);
11621ae08745Sheppo 		if (rv != DDI_PROP_SUCCESS) {
1163e1ebb9ecSlm66018 			cmn_err(CE_NOTE, "[%d] Couldn't add '%s' prop of [%ld]",
1164e1ebb9ecSlm66018 			    instance, VDC_SIZE_PROP_NAME, size);
11651ae08745Sheppo 			return (EIO);
11661ae08745Sheppo 		}
11671ae08745Sheppo 
11681ae08745Sheppo 		rv = ddi_prop_update_int64(dev, dip, VDC_NBLOCKS_PROP_NAME,
11691ae08745Sheppo 		    lbtodb(size));
11701ae08745Sheppo 		if (rv != DDI_PROP_SUCCESS) {
1171e1ebb9ecSlm66018 			cmn_err(CE_NOTE, "[%d] Couldn't add '%s' prop [%llu]",
11721ae08745Sheppo 			    instance, VDC_NBLOCKS_PROP_NAME, lbtodb(size));
11731ae08745Sheppo 			return (EIO);
11741ae08745Sheppo 		}
11751ae08745Sheppo 	}
11761ae08745Sheppo 
11771ae08745Sheppo 	return (0);
11781ae08745Sheppo }
11791ae08745Sheppo 
118078fcd0a1Sachartre /*
118178fcd0a1Sachartre  * Function:
118278fcd0a1Sachartre  *	vdc_is_opened
118378fcd0a1Sachartre  *
118478fcd0a1Sachartre  * Description:
118578fcd0a1Sachartre  *	This function checks if any slice of a given virtual disk is
118678fcd0a1Sachartre  *	currently opened.
118778fcd0a1Sachartre  *
118878fcd0a1Sachartre  * Parameters:
118978fcd0a1Sachartre  *	vdc 		- soft state pointer
119078fcd0a1Sachartre  *
119178fcd0a1Sachartre  * Return Values
119278fcd0a1Sachartre  *	B_TRUE		- at least one slice is opened.
119378fcd0a1Sachartre  *	B_FALSE		- no slice is opened.
119478fcd0a1Sachartre  */
119578fcd0a1Sachartre static boolean_t
119678fcd0a1Sachartre vdc_is_opened(vdc_t *vdc)
119778fcd0a1Sachartre {
119878fcd0a1Sachartre 	int i, nslices;
119978fcd0a1Sachartre 
120078fcd0a1Sachartre 	switch (vdc->vdisk_type) {
120178fcd0a1Sachartre 	case VD_DISK_TYPE_DISK:
120278fcd0a1Sachartre 		nslices = V_NUMPAR;
120378fcd0a1Sachartre 		break;
120478fcd0a1Sachartre 	case VD_DISK_TYPE_SLICE:
120578fcd0a1Sachartre 		nslices = 1;
120678fcd0a1Sachartre 		break;
120778fcd0a1Sachartre 	case VD_DISK_TYPE_UNK:
120878fcd0a1Sachartre 	default:
120978fcd0a1Sachartre 		ASSERT(0);
121078fcd0a1Sachartre 	}
121178fcd0a1Sachartre 
121278fcd0a1Sachartre 	/* check if there's any layered open */
121378fcd0a1Sachartre 	for (i = 0; i < nslices; i++) {
121478fcd0a1Sachartre 		if (vdc->open_lyr[i] > 0)
121578fcd0a1Sachartre 			return (B_TRUE);
121678fcd0a1Sachartre 	}
121778fcd0a1Sachartre 
121878fcd0a1Sachartre 	/* check if there is any other kind of open */
121978fcd0a1Sachartre 	for (i = 0; i < OTYPCNT; i++) {
122078fcd0a1Sachartre 		if (vdc->open[i] != 0)
122178fcd0a1Sachartre 			return (B_TRUE);
122278fcd0a1Sachartre 	}
122378fcd0a1Sachartre 
122478fcd0a1Sachartre 	return (B_FALSE);
122578fcd0a1Sachartre }
122678fcd0a1Sachartre 
122778fcd0a1Sachartre static int
122878fcd0a1Sachartre vdc_mark_opened(vdc_t *vdc, int slice, int flag, int otyp)
122978fcd0a1Sachartre {
123078fcd0a1Sachartre 	uint8_t slicemask;
123178fcd0a1Sachartre 	int i;
123278fcd0a1Sachartre 
123378fcd0a1Sachartre 	ASSERT(otyp < OTYPCNT);
123478fcd0a1Sachartre 	ASSERT(slice < V_NUMPAR);
123578fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
123678fcd0a1Sachartre 
123778fcd0a1Sachartre 	slicemask = 1 << slice;
123878fcd0a1Sachartre 
123978fcd0a1Sachartre 	/* check if slice is already exclusively opened */
124078fcd0a1Sachartre 	if (vdc->open_excl & slicemask)
124178fcd0a1Sachartre 		return (EBUSY);
124278fcd0a1Sachartre 
124378fcd0a1Sachartre 	/* if open exclusive, check if slice is already opened */
124478fcd0a1Sachartre 	if (flag & FEXCL) {
124578fcd0a1Sachartre 		if (vdc->open_lyr[slice] > 0)
124678fcd0a1Sachartre 			return (EBUSY);
124778fcd0a1Sachartre 		for (i = 0; i < OTYPCNT; i++) {
124878fcd0a1Sachartre 			if (vdc->open[i] & slicemask)
124978fcd0a1Sachartre 				return (EBUSY);
125078fcd0a1Sachartre 		}
125178fcd0a1Sachartre 		vdc->open_excl |= slicemask;
125278fcd0a1Sachartre 	}
125378fcd0a1Sachartre 
125478fcd0a1Sachartre 	/* mark slice as opened */
125578fcd0a1Sachartre 	if (otyp == OTYP_LYR) {
125678fcd0a1Sachartre 		vdc->open_lyr[slice]++;
125778fcd0a1Sachartre 	} else {
125878fcd0a1Sachartre 		vdc->open[otyp] |= slicemask;
125978fcd0a1Sachartre 	}
126078fcd0a1Sachartre 
126178fcd0a1Sachartre 	return (0);
126278fcd0a1Sachartre }
126378fcd0a1Sachartre 
126478fcd0a1Sachartre static void
126578fcd0a1Sachartre vdc_mark_closed(vdc_t *vdc, int slice, int flag, int otyp)
126678fcd0a1Sachartre {
126778fcd0a1Sachartre 	uint8_t slicemask;
126878fcd0a1Sachartre 
126978fcd0a1Sachartre 	ASSERT(otyp < OTYPCNT);
127078fcd0a1Sachartre 	ASSERT(slice < V_NUMPAR);
127178fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
127278fcd0a1Sachartre 
127378fcd0a1Sachartre 	slicemask = 1 << slice;
127478fcd0a1Sachartre 
127578fcd0a1Sachartre 	if (otyp == OTYP_LYR) {
127678fcd0a1Sachartre 		ASSERT(vdc->open_lyr[slice] > 0);
127778fcd0a1Sachartre 		vdc->open_lyr[slice]--;
127878fcd0a1Sachartre 	} else {
127978fcd0a1Sachartre 		vdc->open[otyp] &= ~slicemask;
128078fcd0a1Sachartre 	}
128178fcd0a1Sachartre 
128278fcd0a1Sachartre 	if (flag & FEXCL)
128378fcd0a1Sachartre 		vdc->open_excl &= ~slicemask;
128478fcd0a1Sachartre }
128578fcd0a1Sachartre 
12861ae08745Sheppo static int
12871ae08745Sheppo vdc_open(dev_t *dev, int flag, int otyp, cred_t *cred)
12881ae08745Sheppo {
12891ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
12901ae08745Sheppo 
1291179e09c2Sachartre 	int	instance, nodelay;
129278fcd0a1Sachartre 	int	slice, status = 0;
12931ae08745Sheppo 	vdc_t	*vdc;
12941ae08745Sheppo 
12951ae08745Sheppo 	ASSERT(dev != NULL);
12960d0c8d4bSnarayan 	instance = VDCUNIT(*dev);
12971ae08745Sheppo 
129878fcd0a1Sachartre 	if (otyp >= OTYPCNT)
12991ae08745Sheppo 		return (EINVAL);
13001ae08745Sheppo 
13011ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1302e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
13031ae08745Sheppo 		return (ENXIO);
13041ae08745Sheppo 	}
13051ae08745Sheppo 
13063af08d82Slm66018 	DMSG(vdc, 0, "minor = %d flag = %x, otyp = %x\n",
13073af08d82Slm66018 	    getminor(*dev), flag, otyp);
13081ae08745Sheppo 
130978fcd0a1Sachartre 	slice = VDCPART(*dev);
131078fcd0a1Sachartre 
1311179e09c2Sachartre 	nodelay = flag & (FNDELAY | FNONBLOCK);
1312179e09c2Sachartre 
1313179e09c2Sachartre 	if ((flag & FWRITE) && (!nodelay) &&
1314179e09c2Sachartre 	    !(VD_OP_SUPPORTED(vdc->operations, VD_OP_BWRITE))) {
1315179e09c2Sachartre 		return (EROFS);
1316179e09c2Sachartre 	}
1317179e09c2Sachartre 
13181ae08745Sheppo 	mutex_enter(&vdc->lock);
131978fcd0a1Sachartre 
132078fcd0a1Sachartre 	status = vdc_mark_opened(vdc, slice, flag, otyp);
132178fcd0a1Sachartre 
132278fcd0a1Sachartre 	if (status != 0) {
132378fcd0a1Sachartre 		mutex_exit(&vdc->lock);
132478fcd0a1Sachartre 		return (status);
132578fcd0a1Sachartre 	}
132678fcd0a1Sachartre 
1327179e09c2Sachartre 	if (nodelay) {
132878fcd0a1Sachartre 
132978fcd0a1Sachartre 		/* don't resubmit a validate request if there's already one */
133078fcd0a1Sachartre 		if (vdc->validate_pending > 0) {
133178fcd0a1Sachartre 			mutex_exit(&vdc->lock);
133278fcd0a1Sachartre 			return (0);
133378fcd0a1Sachartre 		}
133478fcd0a1Sachartre 
133578fcd0a1Sachartre 		/* call vdc_validate() asynchronously to avoid blocking */
133678fcd0a1Sachartre 		if (taskq_dispatch(system_taskq, vdc_validate_task,
133778fcd0a1Sachartre 		    (void *)vdc, TQ_NOSLEEP) == NULL) {
133878fcd0a1Sachartre 			vdc_mark_closed(vdc, slice, flag, otyp);
133978fcd0a1Sachartre 			mutex_exit(&vdc->lock);
134078fcd0a1Sachartre 			return (ENXIO);
134178fcd0a1Sachartre 		}
134278fcd0a1Sachartre 
134378fcd0a1Sachartre 		vdc->validate_pending++;
134478fcd0a1Sachartre 		mutex_exit(&vdc->lock);
134578fcd0a1Sachartre 		return (0);
134678fcd0a1Sachartre 	}
134778fcd0a1Sachartre 
13481ae08745Sheppo 	mutex_exit(&vdc->lock);
13491ae08745Sheppo 
135078fcd0a1Sachartre 	vdc_validate(vdc);
135178fcd0a1Sachartre 
135278fcd0a1Sachartre 	mutex_enter(&vdc->lock);
135378fcd0a1Sachartre 
135478fcd0a1Sachartre 	if (vdc->vdisk_label == VD_DISK_LABEL_UNK ||
1355edcc0754Sachartre 	    vdc->slice[slice].nblocks == 0) {
135678fcd0a1Sachartre 		vdc_mark_closed(vdc, slice, flag, otyp);
135778fcd0a1Sachartre 		status = EIO;
135878fcd0a1Sachartre 	}
135978fcd0a1Sachartre 
136078fcd0a1Sachartre 	mutex_exit(&vdc->lock);
136178fcd0a1Sachartre 
136278fcd0a1Sachartre 	return (status);
13631ae08745Sheppo }
13641ae08745Sheppo 
13651ae08745Sheppo static int
13661ae08745Sheppo vdc_close(dev_t dev, int flag, int otyp, cred_t *cred)
13671ae08745Sheppo {
13681ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
13691ae08745Sheppo 
13701ae08745Sheppo 	int	instance;
137178fcd0a1Sachartre 	int	slice;
13722f5224aeSachartre 	int	rv, rval;
13731ae08745Sheppo 	vdc_t	*vdc;
13741ae08745Sheppo 
13750d0c8d4bSnarayan 	instance = VDCUNIT(dev);
13761ae08745Sheppo 
137778fcd0a1Sachartre 	if (otyp >= OTYPCNT)
13781ae08745Sheppo 		return (EINVAL);
13791ae08745Sheppo 
13801ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1381e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
13821ae08745Sheppo 		return (ENXIO);
13831ae08745Sheppo 	}
13841ae08745Sheppo 
13853af08d82Slm66018 	DMSG(vdc, 0, "[%d] flag = %x, otyp = %x\n", instance, flag, otyp);
13861ae08745Sheppo 
138778fcd0a1Sachartre 	slice = VDCPART(dev);
138878fcd0a1Sachartre 
13898259acd8Szk194757 	/*
13908259acd8Szk194757 	 * Attempt to flush the W$ on a close operation. If this is
13918259acd8Szk194757 	 * not a supported IOCTL command or the backing device is read-only
13928259acd8Szk194757 	 * do not fail the close operation.
13938259acd8Szk194757 	 */
13942f5224aeSachartre 	rv = vd_process_ioctl(dev, DKIOCFLUSHWRITECACHE, NULL, FKIOCTL, &rval);
13958259acd8Szk194757 
13968259acd8Szk194757 	if (rv != 0 && rv != ENOTSUP && rv != ENOTTY && rv != EROFS) {
13978259acd8Szk194757 		DMSG(vdc, 0, "[%d] flush failed with error %d on close\n",
13988259acd8Szk194757 		    instance, rv);
13998259acd8Szk194757 		return (EIO);
14008259acd8Szk194757 	}
14018259acd8Szk194757 
14021ae08745Sheppo 	mutex_enter(&vdc->lock);
140378fcd0a1Sachartre 	vdc_mark_closed(vdc, slice, flag, otyp);
14041ae08745Sheppo 	mutex_exit(&vdc->lock);
14051ae08745Sheppo 
14061ae08745Sheppo 	return (0);
14071ae08745Sheppo }
14081ae08745Sheppo 
14091ae08745Sheppo static int
14101ae08745Sheppo vdc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
14111ae08745Sheppo {
14121ae08745Sheppo 	_NOTE(ARGUNUSED(credp))
14131ae08745Sheppo 
14142f5224aeSachartre 	return (vd_process_ioctl(dev, cmd, (caddr_t)arg, mode, rvalp));
14151ae08745Sheppo }
14161ae08745Sheppo 
14171ae08745Sheppo static int
14181ae08745Sheppo vdc_print(dev_t dev, char *str)
14191ae08745Sheppo {
14200d0c8d4bSnarayan 	cmn_err(CE_NOTE, "vdc%d:  %s", VDCUNIT(dev), str);
14211ae08745Sheppo 	return (0);
14221ae08745Sheppo }
14231ae08745Sheppo 
14241ae08745Sheppo static int
14251ae08745Sheppo vdc_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
14261ae08745Sheppo {
1427d10e4ef2Snarayan 	int	rv;
1428d10e4ef2Snarayan 	size_t	nbytes = nblk * DEV_BSIZE;
14290d0c8d4bSnarayan 	int	instance = VDCUNIT(dev);
1430d10e4ef2Snarayan 	vdc_t	*vdc = NULL;
14311ae08745Sheppo 
14321ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1433e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
14341ae08745Sheppo 		return (ENXIO);
14351ae08745Sheppo 	}
14361ae08745Sheppo 
14373af08d82Slm66018 	DMSG(vdc, 2, "[%d] dump %ld bytes at block 0x%lx : addr=0x%p\n",
14383af08d82Slm66018 	    instance, nbytes, blkno, (void *)addr);
14393af08d82Slm66018 	rv = vdc_send_request(vdc, VD_OP_BWRITE, addr, nbytes,
14400d0c8d4bSnarayan 	    VDCPART(dev), blkno, CB_STRATEGY, 0, VIO_write_dir);
14413af08d82Slm66018 	if (rv) {
14423af08d82Slm66018 		DMSG(vdc, 0, "Failed to do a disk dump (err=%d)\n", rv);
14431ae08745Sheppo 		return (rv);
14441ae08745Sheppo 	}
14451ae08745Sheppo 
14463af08d82Slm66018 	if (ddi_in_panic())
14473af08d82Slm66018 		(void) vdc_drain_response(vdc);
14483af08d82Slm66018 
14493af08d82Slm66018 	DMSG(vdc, 0, "[%d] End\n", instance);
14503af08d82Slm66018 
14513af08d82Slm66018 	return (0);
14523af08d82Slm66018 }
14533af08d82Slm66018 
14541ae08745Sheppo /* -------------------------------------------------------------------------- */
14551ae08745Sheppo 
14561ae08745Sheppo /*
14571ae08745Sheppo  * Disk access routines
14581ae08745Sheppo  *
14591ae08745Sheppo  */
14601ae08745Sheppo 
14611ae08745Sheppo /*
14621ae08745Sheppo  * vdc_strategy()
14631ae08745Sheppo  *
14641ae08745Sheppo  * Return Value:
14651ae08745Sheppo  *	0:	As per strategy(9E), the strategy() function must return 0
14661ae08745Sheppo  *		[ bioerror(9f) sets b_flags to the proper error code ]
14671ae08745Sheppo  */
14681ae08745Sheppo static int
14691ae08745Sheppo vdc_strategy(struct buf *buf)
14701ae08745Sheppo {
14711ae08745Sheppo 	int	rv = -1;
14721ae08745Sheppo 	vdc_t	*vdc = NULL;
14730d0c8d4bSnarayan 	int	instance = VDCUNIT(buf->b_edev);
14741ae08745Sheppo 	int	op = (buf->b_flags & B_READ) ? VD_OP_BREAD : VD_OP_BWRITE;
147587a7269eSachartre 	int	slice;
14761ae08745Sheppo 
14771ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1478e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
14791ae08745Sheppo 		bioerror(buf, ENXIO);
14801ae08745Sheppo 		biodone(buf);
14811ae08745Sheppo 		return (0);
14821ae08745Sheppo 	}
14831ae08745Sheppo 
14843af08d82Slm66018 	DMSG(vdc, 2, "[%d] %s %ld bytes at block %llx : b_addr=0x%p\n",
14853af08d82Slm66018 	    instance, (buf->b_flags & B_READ) ? "Read" : "Write",
14863af08d82Slm66018 	    buf->b_bcount, buf->b_lblkno, (void *)buf->b_un.b_addr);
1487d10e4ef2Snarayan 
14881ae08745Sheppo 	bp_mapin(buf);
14891ae08745Sheppo 
149087a7269eSachartre 	if ((long)buf->b_private == VD_SLICE_NONE) {
149187a7269eSachartre 		/* I/O using an absolute disk offset */
149287a7269eSachartre 		slice = VD_SLICE_NONE;
149387a7269eSachartre 	} else {
149487a7269eSachartre 		slice = VDCPART(buf->b_edev);
149587a7269eSachartre 	}
149687a7269eSachartre 
14973af08d82Slm66018 	rv = vdc_send_request(vdc, op, (caddr_t)buf->b_un.b_addr,
149887a7269eSachartre 	    buf->b_bcount, slice, buf->b_lblkno,
14993af08d82Slm66018 	    CB_STRATEGY, buf, (op == VD_OP_BREAD) ? VIO_read_dir :
15003af08d82Slm66018 	    VIO_write_dir);
15013af08d82Slm66018 
1502d10e4ef2Snarayan 	/*
1503d10e4ef2Snarayan 	 * If the request was successfully sent, the strategy call returns and
1504d10e4ef2Snarayan 	 * the ACK handler calls the bioxxx functions when the vDisk server is
1505366a92acSlm66018 	 * done otherwise we handle the error here.
1506d10e4ef2Snarayan 	 */
1507d10e4ef2Snarayan 	if (rv) {
15083af08d82Slm66018 		DMSG(vdc, 0, "Failed to read/write (err=%d)\n", rv);
15091ae08745Sheppo 		bioerror(buf, rv);
15101ae08745Sheppo 		biodone(buf);
1511d10e4ef2Snarayan 	}
1512d10e4ef2Snarayan 
15131ae08745Sheppo 	return (0);
15141ae08745Sheppo }
15151ae08745Sheppo 
15160d0c8d4bSnarayan /*
15170d0c8d4bSnarayan  * Function:
15180d0c8d4bSnarayan  *	vdc_min
15190d0c8d4bSnarayan  *
15200d0c8d4bSnarayan  * Description:
15210d0c8d4bSnarayan  *	Routine to limit the size of a data transfer. Used in
15220d0c8d4bSnarayan  *	conjunction with physio(9F).
15230d0c8d4bSnarayan  *
15240d0c8d4bSnarayan  * Arguments:
15250d0c8d4bSnarayan  *	bp - pointer to the indicated buf(9S) struct.
15260d0c8d4bSnarayan  *
15270d0c8d4bSnarayan  */
15280d0c8d4bSnarayan static void
15290d0c8d4bSnarayan vdc_min(struct buf *bufp)
15300d0c8d4bSnarayan {
15310d0c8d4bSnarayan 	vdc_t	*vdc = NULL;
15320d0c8d4bSnarayan 	int	instance = VDCUNIT(bufp->b_edev);
15330d0c8d4bSnarayan 
15340d0c8d4bSnarayan 	vdc = ddi_get_soft_state(vdc_state, instance);
15350d0c8d4bSnarayan 	VERIFY(vdc != NULL);
15360d0c8d4bSnarayan 
15370d0c8d4bSnarayan 	if (bufp->b_bcount > (vdc->max_xfer_sz * vdc->block_size)) {
15380d0c8d4bSnarayan 		bufp->b_bcount = vdc->max_xfer_sz * vdc->block_size;
15390d0c8d4bSnarayan 	}
15400d0c8d4bSnarayan }
15411ae08745Sheppo 
15421ae08745Sheppo static int
15431ae08745Sheppo vdc_read(dev_t dev, struct uio *uio, cred_t *cred)
15441ae08745Sheppo {
15451ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
15461ae08745Sheppo 
15470d0c8d4bSnarayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
15480d0c8d4bSnarayan 	return (physio(vdc_strategy, NULL, dev, B_READ, vdc_min, uio));
15491ae08745Sheppo }
15501ae08745Sheppo 
15511ae08745Sheppo static int
15521ae08745Sheppo vdc_write(dev_t dev, struct uio *uio, cred_t *cred)
15531ae08745Sheppo {
15541ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
15551ae08745Sheppo 
15560d0c8d4bSnarayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
15570d0c8d4bSnarayan 	return (physio(vdc_strategy, NULL, dev, B_WRITE, vdc_min, uio));
15581ae08745Sheppo }
15591ae08745Sheppo 
15601ae08745Sheppo static int
15611ae08745Sheppo vdc_aread(dev_t dev, struct aio_req *aio, cred_t *cred)
15621ae08745Sheppo {
15631ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
15641ae08745Sheppo 
15650d0c8d4bSnarayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
15660d0c8d4bSnarayan 	return (aphysio(vdc_strategy, anocancel, dev, B_READ, vdc_min, aio));
15671ae08745Sheppo }
15681ae08745Sheppo 
15691ae08745Sheppo static int
15701ae08745Sheppo vdc_awrite(dev_t dev, struct aio_req *aio, cred_t *cred)
15711ae08745Sheppo {
15721ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
15731ae08745Sheppo 
15740d0c8d4bSnarayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
15750d0c8d4bSnarayan 	return (aphysio(vdc_strategy, anocancel, dev, B_WRITE, vdc_min, aio));
15761ae08745Sheppo }
15771ae08745Sheppo 
15781ae08745Sheppo 
15791ae08745Sheppo /* -------------------------------------------------------------------------- */
15801ae08745Sheppo 
15811ae08745Sheppo /*
15821ae08745Sheppo  * Handshake support
15831ae08745Sheppo  */
15841ae08745Sheppo 
15851ae08745Sheppo 
15860a55fbb7Slm66018 /*
15870a55fbb7Slm66018  * Function:
15880a55fbb7Slm66018  *	vdc_init_ver_negotiation()
15890a55fbb7Slm66018  *
15900a55fbb7Slm66018  * Description:
15910a55fbb7Slm66018  *
15920a55fbb7Slm66018  * Arguments:
15930a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
15940a55fbb7Slm66018  *
15950a55fbb7Slm66018  * Return Code:
15960a55fbb7Slm66018  *	0	- Success
15970a55fbb7Slm66018  */
15981ae08745Sheppo static int
15990a55fbb7Slm66018 vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver)
16001ae08745Sheppo {
16011ae08745Sheppo 	vio_ver_msg_t	pkt;
16021ae08745Sheppo 	size_t		msglen = sizeof (pkt);
16031ae08745Sheppo 	int		status = -1;
16041ae08745Sheppo 
16051ae08745Sheppo 	ASSERT(vdc != NULL);
16061ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
16071ae08745Sheppo 
16083af08d82Slm66018 	DMSG(vdc, 0, "[%d] Entered.\n", vdc->instance);
1609e1ebb9ecSlm66018 
16101ae08745Sheppo 	/*
16111ae08745Sheppo 	 * set the Session ID to a unique value
16121ae08745Sheppo 	 * (the lower 32 bits of the clock tick)
16131ae08745Sheppo 	 */
16141ae08745Sheppo 	vdc->session_id = ((uint32_t)gettick() & 0xffffffff);
16153af08d82Slm66018 	DMSG(vdc, 0, "[%d] Set SID to 0x%lx\n", vdc->instance, vdc->session_id);
16161ae08745Sheppo 
16171ae08745Sheppo 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
16181ae08745Sheppo 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
16191ae08745Sheppo 	pkt.tag.vio_subtype_env = VIO_VER_INFO;
16201ae08745Sheppo 	pkt.tag.vio_sid = vdc->session_id;
16211ae08745Sheppo 	pkt.dev_class = VDEV_DISK;
16220a55fbb7Slm66018 	pkt.ver_major = ver.major;
16230a55fbb7Slm66018 	pkt.ver_minor = ver.minor;
16241ae08745Sheppo 
16250a55fbb7Slm66018 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
16263af08d82Slm66018 	DMSG(vdc, 0, "[%d] Ver info sent (status = %d)\n",
16273af08d82Slm66018 	    vdc->instance, status);
16281ae08745Sheppo 	if ((status != 0) || (msglen != sizeof (vio_ver_msg_t))) {
16293af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to send Ver negotiation info: "
1630*8cd10891Snarayan 		    "id(%lx) rv(%d) size(%ld)", vdc->instance,
1631*8cd10891Snarayan 		    vdc->curr_server->ldc_handle, status, msglen);
16321ae08745Sheppo 		if (msglen != sizeof (vio_ver_msg_t))
16331ae08745Sheppo 			status = ENOMSG;
16341ae08745Sheppo 	}
16351ae08745Sheppo 
16361ae08745Sheppo 	return (status);
16371ae08745Sheppo }
16381ae08745Sheppo 
16390a55fbb7Slm66018 /*
16400a55fbb7Slm66018  * Function:
16413af08d82Slm66018  *	vdc_ver_negotiation()
16423af08d82Slm66018  *
16433af08d82Slm66018  * Description:
16443af08d82Slm66018  *
16453af08d82Slm66018  * Arguments:
16463af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
16473af08d82Slm66018  *
16483af08d82Slm66018  * Return Code:
16493af08d82Slm66018  *	0	- Success
16503af08d82Slm66018  */
16513af08d82Slm66018 static int
16523af08d82Slm66018 vdc_ver_negotiation(vdc_t *vdcp)
16533af08d82Slm66018 {
16543af08d82Slm66018 	vio_msg_t vio_msg;
16553af08d82Slm66018 	int status;
16563af08d82Slm66018 
16573af08d82Slm66018 	if (status = vdc_init_ver_negotiation(vdcp, vdc_version[0]))
16583af08d82Slm66018 		return (status);
16593af08d82Slm66018 
16603af08d82Slm66018 	/* release lock and wait for response */
16613af08d82Slm66018 	mutex_exit(&vdcp->lock);
16623af08d82Slm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
16633af08d82Slm66018 	mutex_enter(&vdcp->lock);
16643af08d82Slm66018 	if (status) {
16653af08d82Slm66018 		DMSG(vdcp, 0,
16663af08d82Slm66018 		    "[%d] Failed waiting for Ver negotiation response, rv(%d)",
16673af08d82Slm66018 		    vdcp->instance, status);
16683af08d82Slm66018 		return (status);
16693af08d82Slm66018 	}
16703af08d82Slm66018 
16713af08d82Slm66018 	/* check type and sub_type ... */
16723af08d82Slm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
16733af08d82Slm66018 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
16743af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Invalid ver negotiation response\n",
16753af08d82Slm66018 		    vdcp->instance);
16763af08d82Slm66018 		return (EPROTO);
16773af08d82Slm66018 	}
16783af08d82Slm66018 
16793af08d82Slm66018 	return (vdc_handle_ver_msg(vdcp, (vio_ver_msg_t *)&vio_msg));
16803af08d82Slm66018 }
16813af08d82Slm66018 
16823af08d82Slm66018 /*
16833af08d82Slm66018  * Function:
16840a55fbb7Slm66018  *	vdc_init_attr_negotiation()
16850a55fbb7Slm66018  *
16860a55fbb7Slm66018  * Description:
16870a55fbb7Slm66018  *
16880a55fbb7Slm66018  * Arguments:
16890a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
16900a55fbb7Slm66018  *
16910a55fbb7Slm66018  * Return Code:
16920a55fbb7Slm66018  *	0	- Success
16930a55fbb7Slm66018  */
16941ae08745Sheppo static int
16951ae08745Sheppo vdc_init_attr_negotiation(vdc_t *vdc)
16961ae08745Sheppo {
16971ae08745Sheppo 	vd_attr_msg_t	pkt;
16981ae08745Sheppo 	size_t		msglen = sizeof (pkt);
16991ae08745Sheppo 	int		status;
17001ae08745Sheppo 
17011ae08745Sheppo 	ASSERT(vdc != NULL);
17021ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
17031ae08745Sheppo 
17043af08d82Slm66018 	DMSG(vdc, 0, "[%d] entered\n", vdc->instance);
17051ae08745Sheppo 
17061ae08745Sheppo 	/* fill in tag */
17071ae08745Sheppo 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
17081ae08745Sheppo 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
17091ae08745Sheppo 	pkt.tag.vio_subtype_env = VIO_ATTR_INFO;
17101ae08745Sheppo 	pkt.tag.vio_sid = vdc->session_id;
17111ae08745Sheppo 	/* fill in payload */
17121ae08745Sheppo 	pkt.max_xfer_sz = vdc->max_xfer_sz;
17131ae08745Sheppo 	pkt.vdisk_block_size = vdc->block_size;
1714f0ca1d9aSsb155480 	pkt.xfer_mode = VIO_DRING_MODE_V1_0;
17151ae08745Sheppo 	pkt.operations = 0;	/* server will set bits of valid operations */
17161ae08745Sheppo 	pkt.vdisk_type = 0;	/* server will set to valid device type */
171717cadca8Slm66018 	pkt.vdisk_media = 0;	/* server will set to valid media type */
17181ae08745Sheppo 	pkt.vdisk_size = 0;	/* server will set to valid size */
17191ae08745Sheppo 
17200a55fbb7Slm66018 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
17213af08d82Slm66018 	DMSG(vdc, 0, "Attr info sent (status = %d)\n", status);
17221ae08745Sheppo 
17231ae08745Sheppo 	if ((status != 0) || (msglen != sizeof (vio_ver_msg_t))) {
17243af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to send Attr negotiation info: "
1725*8cd10891Snarayan 		    "id(%lx) rv(%d) size(%ld)", vdc->instance,
1726*8cd10891Snarayan 		    vdc->curr_server->ldc_handle, status, msglen);
17271ae08745Sheppo 		if (msglen != sizeof (vio_ver_msg_t))
17281ae08745Sheppo 			status = ENOMSG;
17291ae08745Sheppo 	}
17301ae08745Sheppo 
17311ae08745Sheppo 	return (status);
17321ae08745Sheppo }
17331ae08745Sheppo 
17340a55fbb7Slm66018 /*
17350a55fbb7Slm66018  * Function:
17363af08d82Slm66018  *	vdc_attr_negotiation()
17373af08d82Slm66018  *
17383af08d82Slm66018  * Description:
17393af08d82Slm66018  *
17403af08d82Slm66018  * Arguments:
17413af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
17423af08d82Slm66018  *
17433af08d82Slm66018  * Return Code:
17443af08d82Slm66018  *	0	- Success
17453af08d82Slm66018  */
17463af08d82Slm66018 static int
17473af08d82Slm66018 vdc_attr_negotiation(vdc_t *vdcp)
17483af08d82Slm66018 {
17493af08d82Slm66018 	int status;
17503af08d82Slm66018 	vio_msg_t vio_msg;
17513af08d82Slm66018 
17523af08d82Slm66018 	if (status = vdc_init_attr_negotiation(vdcp))
17533af08d82Slm66018 		return (status);
17543af08d82Slm66018 
17553af08d82Slm66018 	/* release lock and wait for response */
17563af08d82Slm66018 	mutex_exit(&vdcp->lock);
17573af08d82Slm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
17583af08d82Slm66018 	mutex_enter(&vdcp->lock);
17593af08d82Slm66018 	if (status) {
17603af08d82Slm66018 		DMSG(vdcp, 0,
17613af08d82Slm66018 		    "[%d] Failed waiting for Attr negotiation response, rv(%d)",
17623af08d82Slm66018 		    vdcp->instance, status);
17633af08d82Slm66018 		return (status);
17643af08d82Slm66018 	}
17653af08d82Slm66018 
17663af08d82Slm66018 	/* check type and sub_type ... */
17673af08d82Slm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
17683af08d82Slm66018 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
17693af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Invalid attr negotiation response\n",
17703af08d82Slm66018 		    vdcp->instance);
17713af08d82Slm66018 		return (EPROTO);
17723af08d82Slm66018 	}
17733af08d82Slm66018 
17743af08d82Slm66018 	return (vdc_handle_attr_msg(vdcp, (vd_attr_msg_t *)&vio_msg));
17753af08d82Slm66018 }
17763af08d82Slm66018 
17773af08d82Slm66018 
17783af08d82Slm66018 /*
17793af08d82Slm66018  * Function:
17800a55fbb7Slm66018  *	vdc_init_dring_negotiate()
17810a55fbb7Slm66018  *
17820a55fbb7Slm66018  * Description:
17830a55fbb7Slm66018  *
17840a55fbb7Slm66018  * Arguments:
17850a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
17860a55fbb7Slm66018  *
17870a55fbb7Slm66018  * Return Code:
17880a55fbb7Slm66018  *	0	- Success
17890a55fbb7Slm66018  */
17901ae08745Sheppo static int
17911ae08745Sheppo vdc_init_dring_negotiate(vdc_t *vdc)
17921ae08745Sheppo {
17931ae08745Sheppo 	vio_dring_reg_msg_t	pkt;
17941ae08745Sheppo 	size_t			msglen = sizeof (pkt);
17951ae08745Sheppo 	int			status = -1;
17963af08d82Slm66018 	int			retry;
17973af08d82Slm66018 	int			nretries = 10;
17981ae08745Sheppo 
17991ae08745Sheppo 	ASSERT(vdc != NULL);
18001ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
18011ae08745Sheppo 
18023af08d82Slm66018 	for (retry = 0; retry < nretries; retry++) {
18031ae08745Sheppo 		status = vdc_init_descriptor_ring(vdc);
18043af08d82Slm66018 		if (status != EAGAIN)
18053af08d82Slm66018 			break;
18063af08d82Slm66018 		drv_usecwait(vdc_min_timeout_ldc);
18073af08d82Slm66018 	}
18083af08d82Slm66018 
18091ae08745Sheppo 	if (status != 0) {
18103af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to init DRing (status = %d)\n",
18111ae08745Sheppo 		    vdc->instance, status);
18121ae08745Sheppo 		return (status);
18131ae08745Sheppo 	}
18143af08d82Slm66018 
18153af08d82Slm66018 	DMSG(vdc, 0, "[%d] Init of descriptor ring completed (status = %d)\n",
1816e1ebb9ecSlm66018 	    vdc->instance, status);
18171ae08745Sheppo 
18181ae08745Sheppo 	/* fill in tag */
18191ae08745Sheppo 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
18201ae08745Sheppo 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
18211ae08745Sheppo 	pkt.tag.vio_subtype_env = VIO_DRING_REG;
18221ae08745Sheppo 	pkt.tag.vio_sid = vdc->session_id;
18231ae08745Sheppo 	/* fill in payload */
18241ae08745Sheppo 	pkt.dring_ident = 0;
1825e1ebb9ecSlm66018 	pkt.num_descriptors = vdc->dring_len;
1826e1ebb9ecSlm66018 	pkt.descriptor_size = vdc->dring_entry_size;
18271ae08745Sheppo 	pkt.options = (VIO_TX_DRING | VIO_RX_DRING);
18281ae08745Sheppo 	pkt.ncookies = vdc->dring_cookie_count;
18291ae08745Sheppo 	pkt.cookie[0] = vdc->dring_cookie[0];	/* for now just one cookie */
18301ae08745Sheppo 
18310a55fbb7Slm66018 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
18321ae08745Sheppo 	if (status != 0) {
18333af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to register DRing (err = %d)",
1834e1ebb9ecSlm66018 		    vdc->instance, status);
18351ae08745Sheppo 	}
18361ae08745Sheppo 
18371ae08745Sheppo 	return (status);
18381ae08745Sheppo }
18391ae08745Sheppo 
18401ae08745Sheppo 
18413af08d82Slm66018 /*
18423af08d82Slm66018  * Function:
18433af08d82Slm66018  *	vdc_dring_negotiation()
18443af08d82Slm66018  *
18453af08d82Slm66018  * Description:
18463af08d82Slm66018  *
18473af08d82Slm66018  * Arguments:
18483af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
18493af08d82Slm66018  *
18503af08d82Slm66018  * Return Code:
18513af08d82Slm66018  *	0	- Success
18523af08d82Slm66018  */
18533af08d82Slm66018 static int
18543af08d82Slm66018 vdc_dring_negotiation(vdc_t *vdcp)
18553af08d82Slm66018 {
18563af08d82Slm66018 	int status;
18573af08d82Slm66018 	vio_msg_t vio_msg;
18583af08d82Slm66018 
18593af08d82Slm66018 	if (status = vdc_init_dring_negotiate(vdcp))
18603af08d82Slm66018 		return (status);
18613af08d82Slm66018 
18623af08d82Slm66018 	/* release lock and wait for response */
18633af08d82Slm66018 	mutex_exit(&vdcp->lock);
18643af08d82Slm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
18653af08d82Slm66018 	mutex_enter(&vdcp->lock);
18663af08d82Slm66018 	if (status) {
18673af08d82Slm66018 		DMSG(vdcp, 0,
18683af08d82Slm66018 		    "[%d] Failed waiting for Dring negotiation response,"
18693af08d82Slm66018 		    " rv(%d)", vdcp->instance, status);
18703af08d82Slm66018 		return (status);
18713af08d82Slm66018 	}
18723af08d82Slm66018 
18733af08d82Slm66018 	/* check type and sub_type ... */
18743af08d82Slm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
18753af08d82Slm66018 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
18763af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Invalid Dring negotiation response\n",
18773af08d82Slm66018 		    vdcp->instance);
18783af08d82Slm66018 		return (EPROTO);
18793af08d82Slm66018 	}
18803af08d82Slm66018 
18813af08d82Slm66018 	return (vdc_handle_dring_reg_msg(vdcp,
18823af08d82Slm66018 	    (vio_dring_reg_msg_t *)&vio_msg));
18833af08d82Slm66018 }
18843af08d82Slm66018 
18853af08d82Slm66018 
18863af08d82Slm66018 /*
18873af08d82Slm66018  * Function:
18883af08d82Slm66018  *	vdc_send_rdx()
18893af08d82Slm66018  *
18903af08d82Slm66018  * Description:
18913af08d82Slm66018  *
18923af08d82Slm66018  * Arguments:
18933af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
18943af08d82Slm66018  *
18953af08d82Slm66018  * Return Code:
18963af08d82Slm66018  *	0	- Success
18973af08d82Slm66018  */
18983af08d82Slm66018 static int
18993af08d82Slm66018 vdc_send_rdx(vdc_t *vdcp)
19003af08d82Slm66018 {
19013af08d82Slm66018 	vio_msg_t	msg;
19023af08d82Slm66018 	size_t		msglen = sizeof (vio_msg_t);
19033af08d82Slm66018 	int		status;
19043af08d82Slm66018 
19053af08d82Slm66018 	/*
19063af08d82Slm66018 	 * Send an RDX message to vds to indicate we are ready
19073af08d82Slm66018 	 * to send data
19083af08d82Slm66018 	 */
19093af08d82Slm66018 	msg.tag.vio_msgtype = VIO_TYPE_CTRL;
19103af08d82Slm66018 	msg.tag.vio_subtype = VIO_SUBTYPE_INFO;
19113af08d82Slm66018 	msg.tag.vio_subtype_env = VIO_RDX;
19123af08d82Slm66018 	msg.tag.vio_sid = vdcp->session_id;
19133af08d82Slm66018 	status = vdc_send(vdcp, (caddr_t)&msg, &msglen);
19143af08d82Slm66018 	if (status != 0) {
19153af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Failed to send RDX message (%d)",
19163af08d82Slm66018 		    vdcp->instance, status);
19173af08d82Slm66018 	}
19183af08d82Slm66018 
19193af08d82Slm66018 	return (status);
19203af08d82Slm66018 }
19213af08d82Slm66018 
19223af08d82Slm66018 /*
19233af08d82Slm66018  * Function:
19243af08d82Slm66018  *	vdc_handle_rdx()
19253af08d82Slm66018  *
19263af08d82Slm66018  * Description:
19273af08d82Slm66018  *
19283af08d82Slm66018  * Arguments:
19293af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
19303af08d82Slm66018  *	msgp	- received msg
19313af08d82Slm66018  *
19323af08d82Slm66018  * Return Code:
19333af08d82Slm66018  *	0	- Success
19343af08d82Slm66018  */
19353af08d82Slm66018 static int
19363af08d82Slm66018 vdc_handle_rdx(vdc_t *vdcp, vio_rdx_msg_t *msgp)
19373af08d82Slm66018 {
19383af08d82Slm66018 	_NOTE(ARGUNUSED(vdcp))
19393af08d82Slm66018 	_NOTE(ARGUNUSED(msgp))
19403af08d82Slm66018 
19413af08d82Slm66018 	ASSERT(msgp->tag.vio_msgtype == VIO_TYPE_CTRL);
19423af08d82Slm66018 	ASSERT(msgp->tag.vio_subtype == VIO_SUBTYPE_ACK);
19433af08d82Slm66018 	ASSERT(msgp->tag.vio_subtype_env == VIO_RDX);
19443af08d82Slm66018 
19453af08d82Slm66018 	DMSG(vdcp, 1, "[%d] Got an RDX msg", vdcp->instance);
19463af08d82Slm66018 
19473af08d82Slm66018 	return (0);
19483af08d82Slm66018 }
19493af08d82Slm66018 
19503af08d82Slm66018 /*
19513af08d82Slm66018  * Function:
19523af08d82Slm66018  *	vdc_rdx_exchange()
19533af08d82Slm66018  *
19543af08d82Slm66018  * Description:
19553af08d82Slm66018  *
19563af08d82Slm66018  * Arguments:
19573af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
19583af08d82Slm66018  *
19593af08d82Slm66018  * Return Code:
19603af08d82Slm66018  *	0	- Success
19613af08d82Slm66018  */
19623af08d82Slm66018 static int
19633af08d82Slm66018 vdc_rdx_exchange(vdc_t *vdcp)
19643af08d82Slm66018 {
19653af08d82Slm66018 	int status;
19663af08d82Slm66018 	vio_msg_t vio_msg;
19673af08d82Slm66018 
19683af08d82Slm66018 	if (status = vdc_send_rdx(vdcp))
19693af08d82Slm66018 		return (status);
19703af08d82Slm66018 
19713af08d82Slm66018 	/* release lock and wait for response */
19723af08d82Slm66018 	mutex_exit(&vdcp->lock);
19733af08d82Slm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
19743af08d82Slm66018 	mutex_enter(&vdcp->lock);
19753af08d82Slm66018 	if (status) {
197687a7269eSachartre 		DMSG(vdcp, 0, "[%d] Failed waiting for RDX response, rv(%d)",
197787a7269eSachartre 		    vdcp->instance, status);
19783af08d82Slm66018 		return (status);
19793af08d82Slm66018 	}
19803af08d82Slm66018 
19813af08d82Slm66018 	/* check type and sub_type ... */
19823af08d82Slm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
19833af08d82Slm66018 	    vio_msg.tag.vio_subtype != VIO_SUBTYPE_ACK) {
198487a7269eSachartre 		DMSG(vdcp, 0, "[%d] Invalid RDX response\n", vdcp->instance);
19853af08d82Slm66018 		return (EPROTO);
19863af08d82Slm66018 	}
19873af08d82Slm66018 
19883af08d82Slm66018 	return (vdc_handle_rdx(vdcp, (vio_rdx_msg_t *)&vio_msg));
19893af08d82Slm66018 }
19903af08d82Slm66018 
19913af08d82Slm66018 
19921ae08745Sheppo /* -------------------------------------------------------------------------- */
19931ae08745Sheppo 
19941ae08745Sheppo /*
19951ae08745Sheppo  * LDC helper routines
19961ae08745Sheppo  */
19971ae08745Sheppo 
19983af08d82Slm66018 static int
19993af08d82Slm66018 vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp)
20003af08d82Slm66018 {
20013af08d82Slm66018 	int		status;
20023af08d82Slm66018 	boolean_t	q_has_pkts = B_FALSE;
200317cadca8Slm66018 	uint64_t	delay_time;
20043af08d82Slm66018 	size_t		len;
20053af08d82Slm66018 
20063af08d82Slm66018 	mutex_enter(&vdc->read_lock);
20073af08d82Slm66018 
20083af08d82Slm66018 	if (vdc->read_state == VDC_READ_IDLE)
20093af08d82Slm66018 		vdc->read_state = VDC_READ_WAITING;
20103af08d82Slm66018 
20113af08d82Slm66018 	while (vdc->read_state != VDC_READ_PENDING) {
20123af08d82Slm66018 
20133af08d82Slm66018 		/* detect if the connection has been reset */
20143af08d82Slm66018 		if (vdc->read_state == VDC_READ_RESET) {
20153af08d82Slm66018 			status = ECONNRESET;
20163af08d82Slm66018 			goto done;
20173af08d82Slm66018 		}
20183af08d82Slm66018 
20193af08d82Slm66018 		cv_wait(&vdc->read_cv, &vdc->read_lock);
20203af08d82Slm66018 	}
20213af08d82Slm66018 
20223af08d82Slm66018 	/*
20233af08d82Slm66018 	 * Until we get a blocking ldc read we have to retry
20243af08d82Slm66018 	 * until the entire LDC message has arrived before
20253af08d82Slm66018 	 * ldc_read() will succeed. Note we also bail out if
2026eff7243fSlm66018 	 * the channel is reset or goes away.
20273af08d82Slm66018 	 */
20283af08d82Slm66018 	delay_time = vdc_ldc_read_init_delay;
20293af08d82Slm66018 loop:
20303af08d82Slm66018 	len = *nbytesp;
2031*8cd10891Snarayan 	status = ldc_read(vdc->curr_server->ldc_handle, (caddr_t)msgp, &len);
20323af08d82Slm66018 	switch (status) {
20333af08d82Slm66018 	case EAGAIN:
20343af08d82Slm66018 		delay_time *= 2;
20353af08d82Slm66018 		if (delay_time >= vdc_ldc_read_max_delay)
20363af08d82Slm66018 			delay_time = vdc_ldc_read_max_delay;
20373af08d82Slm66018 		delay(delay_time);
20383af08d82Slm66018 		goto loop;
20393af08d82Slm66018 
20403af08d82Slm66018 	case 0:
20413af08d82Slm66018 		if (len == 0) {
204217cadca8Slm66018 			DMSG(vdc, 1, "[%d] ldc_read returned 0 bytes with "
20433af08d82Slm66018 			    "no error!\n", vdc->instance);
20443af08d82Slm66018 			goto loop;
20453af08d82Slm66018 		}
20463af08d82Slm66018 
20473af08d82Slm66018 		*nbytesp = len;
20483af08d82Slm66018 
20493af08d82Slm66018 		/*
20503af08d82Slm66018 		 * If there are pending messages, leave the
20513af08d82Slm66018 		 * read state as pending. Otherwise, set the state
20523af08d82Slm66018 		 * back to idle.
20533af08d82Slm66018 		 */
2054*8cd10891Snarayan 		status = ldc_chkq(vdc->curr_server->ldc_handle, &q_has_pkts);
20553af08d82Slm66018 		if (status == 0 && !q_has_pkts)
20563af08d82Slm66018 			vdc->read_state = VDC_READ_IDLE;
20573af08d82Slm66018 
20583af08d82Slm66018 		break;
20593af08d82Slm66018 	default:
20603af08d82Slm66018 		DMSG(vdc, 0, "ldc_read returned %d\n", status);
20613af08d82Slm66018 		break;
20623af08d82Slm66018 	}
20633af08d82Slm66018 
20643af08d82Slm66018 done:
20653af08d82Slm66018 	mutex_exit(&vdc->read_lock);
20663af08d82Slm66018 
20673af08d82Slm66018 	return (status);
20683af08d82Slm66018 }
20693af08d82Slm66018 
20703af08d82Slm66018 
20713af08d82Slm66018 
20723af08d82Slm66018 #ifdef DEBUG
20733af08d82Slm66018 void
20743af08d82Slm66018 vdc_decode_tag(vdc_t *vdcp, vio_msg_t *msg)
20753af08d82Slm66018 {
20763af08d82Slm66018 	char *ms, *ss, *ses;
20773af08d82Slm66018 	switch (msg->tag.vio_msgtype) {
20783af08d82Slm66018 #define	Q(_s)	case _s : ms = #_s; break;
20793af08d82Slm66018 	Q(VIO_TYPE_CTRL)
20803af08d82Slm66018 	Q(VIO_TYPE_DATA)
20813af08d82Slm66018 	Q(VIO_TYPE_ERR)
20823af08d82Slm66018 #undef Q
20833af08d82Slm66018 	default: ms = "unknown"; break;
20843af08d82Slm66018 	}
20853af08d82Slm66018 
20863af08d82Slm66018 	switch (msg->tag.vio_subtype) {
20873af08d82Slm66018 #define	Q(_s)	case _s : ss = #_s; break;
20883af08d82Slm66018 	Q(VIO_SUBTYPE_INFO)
20893af08d82Slm66018 	Q(VIO_SUBTYPE_ACK)
20903af08d82Slm66018 	Q(VIO_SUBTYPE_NACK)
20913af08d82Slm66018 #undef Q
20923af08d82Slm66018 	default: ss = "unknown"; break;
20933af08d82Slm66018 	}
20943af08d82Slm66018 
20953af08d82Slm66018 	switch (msg->tag.vio_subtype_env) {
20963af08d82Slm66018 #define	Q(_s)	case _s : ses = #_s; break;
20973af08d82Slm66018 	Q(VIO_VER_INFO)
20983af08d82Slm66018 	Q(VIO_ATTR_INFO)
20993af08d82Slm66018 	Q(VIO_DRING_REG)
21003af08d82Slm66018 	Q(VIO_DRING_UNREG)
21013af08d82Slm66018 	Q(VIO_RDX)
21023af08d82Slm66018 	Q(VIO_PKT_DATA)
21033af08d82Slm66018 	Q(VIO_DESC_DATA)
21043af08d82Slm66018 	Q(VIO_DRING_DATA)
21053af08d82Slm66018 #undef Q
21063af08d82Slm66018 	default: ses = "unknown"; break;
21073af08d82Slm66018 	}
21083af08d82Slm66018 
21093af08d82Slm66018 	DMSG(vdcp, 3, "(%x/%x/%x) message : (%s/%s/%s)\n",
21103af08d82Slm66018 	    msg->tag.vio_msgtype, msg->tag.vio_subtype,
21113af08d82Slm66018 	    msg->tag.vio_subtype_env, ms, ss, ses);
21123af08d82Slm66018 }
21133af08d82Slm66018 #endif
21143af08d82Slm66018 
21151ae08745Sheppo /*
21161ae08745Sheppo  * Function:
21171ae08745Sheppo  *	vdc_send()
21181ae08745Sheppo  *
21191ae08745Sheppo  * Description:
21201ae08745Sheppo  *	The function encapsulates the call to write a message using LDC.
21211ae08745Sheppo  *	If LDC indicates that the call failed due to the queue being full,
212217cadca8Slm66018  *	we retry the ldc_write(), otherwise we return the error returned by LDC.
21231ae08745Sheppo  *
21241ae08745Sheppo  * Arguments:
21251ae08745Sheppo  *	ldc_handle	- LDC handle for the channel this instance of vdc uses
21261ae08745Sheppo  *	pkt		- address of LDC message to be sent
21271ae08745Sheppo  *	msglen		- the size of the message being sent. When the function
21281ae08745Sheppo  *			  returns, this contains the number of bytes written.
21291ae08745Sheppo  *
21301ae08745Sheppo  * Return Code:
21311ae08745Sheppo  *	0		- Success.
21321ae08745Sheppo  *	EINVAL		- pkt or msglen were NULL
21331ae08745Sheppo  *	ECONNRESET	- The connection was not up.
21341ae08745Sheppo  *	EWOULDBLOCK	- LDC queue is full
21351ae08745Sheppo  *	xxx		- other error codes returned by ldc_write
21361ae08745Sheppo  */
21371ae08745Sheppo static int
21380a55fbb7Slm66018 vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen)
21391ae08745Sheppo {
21401ae08745Sheppo 	size_t	size = 0;
21411ae08745Sheppo 	int	status = 0;
21423af08d82Slm66018 	clock_t delay_ticks;
21431ae08745Sheppo 
21440a55fbb7Slm66018 	ASSERT(vdc != NULL);
21450a55fbb7Slm66018 	ASSERT(mutex_owned(&vdc->lock));
21461ae08745Sheppo 	ASSERT(msglen != NULL);
21471ae08745Sheppo 	ASSERT(*msglen != 0);
21481ae08745Sheppo 
21493af08d82Slm66018 #ifdef DEBUG
215017cadca8Slm66018 	vdc_decode_tag(vdc, (vio_msg_t *)(uintptr_t)pkt);
21513af08d82Slm66018 #endif
21523af08d82Slm66018 	/*
21533af08d82Slm66018 	 * Wait indefinitely to send if channel
21543af08d82Slm66018 	 * is busy, but bail out if we succeed or
21553af08d82Slm66018 	 * if the channel closes or is reset.
21563af08d82Slm66018 	 */
21573af08d82Slm66018 	delay_ticks = vdc_hz_min_ldc_delay;
21581ae08745Sheppo 	do {
21591ae08745Sheppo 		size = *msglen;
2160*8cd10891Snarayan 		status = ldc_write(vdc->curr_server->ldc_handle, pkt, &size);
21613af08d82Slm66018 		if (status == EWOULDBLOCK) {
21623af08d82Slm66018 			delay(delay_ticks);
21633af08d82Slm66018 			/* geometric backoff */
21643af08d82Slm66018 			delay_ticks *= 2;
21653af08d82Slm66018 			if (delay_ticks > vdc_hz_max_ldc_delay)
21663af08d82Slm66018 				delay_ticks = vdc_hz_max_ldc_delay;
21673af08d82Slm66018 		}
21683af08d82Slm66018 	} while (status == EWOULDBLOCK);
21691ae08745Sheppo 
21700a55fbb7Slm66018 	/* if LDC had serious issues --- reset vdc state */
21710a55fbb7Slm66018 	if (status == EIO || status == ECONNRESET) {
21723af08d82Slm66018 		/* LDC had serious issues --- reset vdc state */
21733af08d82Slm66018 		mutex_enter(&vdc->read_lock);
21743af08d82Slm66018 		if ((vdc->read_state == VDC_READ_WAITING) ||
21753af08d82Slm66018 		    (vdc->read_state == VDC_READ_RESET))
21763af08d82Slm66018 			cv_signal(&vdc->read_cv);
21773af08d82Slm66018 		vdc->read_state = VDC_READ_RESET;
21783af08d82Slm66018 		mutex_exit(&vdc->read_lock);
21793af08d82Slm66018 
21803af08d82Slm66018 		/* wake up any waiters in the reset thread */
21813af08d82Slm66018 		if (vdc->state == VDC_STATE_INIT_WAITING) {
21823af08d82Slm66018 			DMSG(vdc, 0, "[%d] write reset - "
21833af08d82Slm66018 			    "vdc is resetting ..\n", vdc->instance);
21843af08d82Slm66018 			vdc->state = VDC_STATE_RESETTING;
21853af08d82Slm66018 			cv_signal(&vdc->initwait_cv);
21863af08d82Slm66018 		}
21873af08d82Slm66018 
21883af08d82Slm66018 		return (ECONNRESET);
21890a55fbb7Slm66018 	}
21900a55fbb7Slm66018 
21911ae08745Sheppo 	/* return the last size written */
21921ae08745Sheppo 	*msglen = size;
21931ae08745Sheppo 
21941ae08745Sheppo 	return (status);
21951ae08745Sheppo }
21961ae08745Sheppo 
21971ae08745Sheppo /*
21981ae08745Sheppo  * Function:
2199655fd6a9Sachartre  *	vdc_get_md_node
22001ae08745Sheppo  *
22011ae08745Sheppo  * Description:
2202*8cd10891Snarayan  *	Get the MD, the device node for the given disk instance. The
2203*8cd10891Snarayan  *	caller is responsible for cleaning up the reference to the
2204*8cd10891Snarayan  *	returned MD (mdpp) by calling md_fini_handle().
22051ae08745Sheppo  *
22061ae08745Sheppo  * Arguments:
22071ae08745Sheppo  *	dip	- dev info pointer for this instance of the device driver.
2208655fd6a9Sachartre  *	mdpp	- the returned MD.
2209655fd6a9Sachartre  *	vd_nodep - the returned device node.
22101ae08745Sheppo  *
22111ae08745Sheppo  * Return Code:
22121ae08745Sheppo  *	0	- Success.
22131ae08745Sheppo  *	ENOENT	- Expected node or property did not exist.
22141ae08745Sheppo  *	ENXIO	- Unexpected error communicating with MD framework
22151ae08745Sheppo  */
22161ae08745Sheppo static int
2217*8cd10891Snarayan vdc_get_md_node(dev_info_t *dip, md_t **mdpp, mde_cookie_t *vd_nodep)
22181ae08745Sheppo {
22191ae08745Sheppo 	int		status = ENOENT;
22201ae08745Sheppo 	char		*node_name = NULL;
22211ae08745Sheppo 	md_t		*mdp = NULL;
22221ae08745Sheppo 	int		num_nodes;
22231ae08745Sheppo 	int		num_vdevs;
22241ae08745Sheppo 	mde_cookie_t	rootnode;
22251ae08745Sheppo 	mde_cookie_t	*listp = NULL;
22261ae08745Sheppo 	boolean_t	found_inst = B_FALSE;
22271ae08745Sheppo 	int		listsz;
22281ae08745Sheppo 	int		idx;
22291ae08745Sheppo 	uint64_t	md_inst;
22301ae08745Sheppo 	int		obp_inst;
22311ae08745Sheppo 	int		instance = ddi_get_instance(dip);
22321ae08745Sheppo 
22331ae08745Sheppo 	/*
22341ae08745Sheppo 	 * Get the OBP instance number for comparison with the MD instance
22351ae08745Sheppo 	 *
22361ae08745Sheppo 	 * The "cfg-handle" property of a vdc node in an MD contains the MD's
22371ae08745Sheppo 	 * notion of "instance", or unique identifier, for that node; OBP
22381ae08745Sheppo 	 * stores the value of the "cfg-handle" MD property as the value of
22391ae08745Sheppo 	 * the "reg" property on the node in the device tree it builds from
22401ae08745Sheppo 	 * the MD and passes to Solaris.  Thus, we look up the devinfo node's
22411ae08745Sheppo 	 * "reg" property value to uniquely identify this device instance.
22421ae08745Sheppo 	 * If the "reg" property cannot be found, the device tree state is
22431ae08745Sheppo 	 * presumably so broken that there is no point in continuing.
22441ae08745Sheppo 	 */
22451ae08745Sheppo 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, OBP_REG)) {
22461ae08745Sheppo 		cmn_err(CE_WARN, "'%s' property does not exist", OBP_REG);
22471ae08745Sheppo 		return (ENOENT);
22481ae08745Sheppo 	}
22491ae08745Sheppo 	obp_inst = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
22501ae08745Sheppo 	    OBP_REG, -1);
22513af08d82Slm66018 	DMSGX(1, "[%d] OBP inst=%d\n", instance, obp_inst);
22521ae08745Sheppo 
22531ae08745Sheppo 	/*
2254655fd6a9Sachartre 	 * We now walk the MD nodes to find the node for this vdisk.
22551ae08745Sheppo 	 */
22561ae08745Sheppo 	if ((mdp = md_get_handle()) == NULL) {
22571ae08745Sheppo 		cmn_err(CE_WARN, "unable to init machine description");
22581ae08745Sheppo 		return (ENXIO);
22591ae08745Sheppo 	}
22601ae08745Sheppo 
22611ae08745Sheppo 	num_nodes = md_node_count(mdp);
22621ae08745Sheppo 	ASSERT(num_nodes > 0);
22631ae08745Sheppo 
22641ae08745Sheppo 	listsz = num_nodes * sizeof (mde_cookie_t);
22651ae08745Sheppo 
22661ae08745Sheppo 	/* allocate memory for nodes */
22671ae08745Sheppo 	listp = kmem_zalloc(listsz, KM_SLEEP);
22681ae08745Sheppo 
22691ae08745Sheppo 	rootnode = md_root_node(mdp);
22701ae08745Sheppo 	ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE);
22711ae08745Sheppo 
22721ae08745Sheppo 	/*
22731ae08745Sheppo 	 * Search for all the virtual devices, we will then check to see which
22741ae08745Sheppo 	 * ones are disk nodes.
22751ae08745Sheppo 	 */
22761ae08745Sheppo 	num_vdevs = md_scan_dag(mdp, rootnode,
22771ae08745Sheppo 	    md_find_name(mdp, VDC_MD_VDEV_NAME),
22781ae08745Sheppo 	    md_find_name(mdp, "fwd"), listp);
22791ae08745Sheppo 
22801ae08745Sheppo 	if (num_vdevs <= 0) {
22811ae08745Sheppo 		cmn_err(CE_NOTE, "No '%s' node found", VDC_MD_VDEV_NAME);
22821ae08745Sheppo 		status = ENOENT;
22831ae08745Sheppo 		goto done;
22841ae08745Sheppo 	}
22851ae08745Sheppo 
22863af08d82Slm66018 	DMSGX(1, "[%d] num_vdevs=%d\n", instance, num_vdevs);
22871ae08745Sheppo 	for (idx = 0; idx < num_vdevs; idx++) {
22881ae08745Sheppo 		status = md_get_prop_str(mdp, listp[idx], "name", &node_name);
22891ae08745Sheppo 		if ((status != 0) || (node_name == NULL)) {
22901ae08745Sheppo 			cmn_err(CE_NOTE, "Unable to get name of node type '%s'"
22911ae08745Sheppo 			    ": err %d", VDC_MD_VDEV_NAME, status);
22921ae08745Sheppo 			continue;
22931ae08745Sheppo 		}
22941ae08745Sheppo 
22953af08d82Slm66018 		DMSGX(1, "[%d] Found node '%s'\n", instance, node_name);
22961ae08745Sheppo 		if (strcmp(VDC_MD_DISK_NAME, node_name) == 0) {
22971ae08745Sheppo 			status = md_get_prop_val(mdp, listp[idx],
22981ae08745Sheppo 			    VDC_MD_CFG_HDL, &md_inst);
22993af08d82Slm66018 			DMSGX(1, "[%d] vdc inst in MD=%lx\n",
23003af08d82Slm66018 			    instance, md_inst);
23011ae08745Sheppo 			if ((status == 0) && (md_inst == obp_inst)) {
23021ae08745Sheppo 				found_inst = B_TRUE;
23031ae08745Sheppo 				break;
23041ae08745Sheppo 			}
23051ae08745Sheppo 		}
23061ae08745Sheppo 	}
23071ae08745Sheppo 
23080a55fbb7Slm66018 	if (!found_inst) {
23093af08d82Slm66018 		DMSGX(0, "Unable to find correct '%s' node", VDC_MD_DISK_NAME);
23101ae08745Sheppo 		status = ENOENT;
23111ae08745Sheppo 		goto done;
23121ae08745Sheppo 	}
23133af08d82Slm66018 	DMSGX(0, "[%d] MD inst=%lx\n", instance, md_inst);
23141ae08745Sheppo 
2315655fd6a9Sachartre 	*vd_nodep = listp[idx];
2316655fd6a9Sachartre 	*mdpp = mdp;
2317655fd6a9Sachartre done:
2318655fd6a9Sachartre 	kmem_free(listp, listsz);
2319655fd6a9Sachartre 	return (status);
2320655fd6a9Sachartre }
2321655fd6a9Sachartre 
2322655fd6a9Sachartre /*
2323655fd6a9Sachartre  * Function:
2324*8cd10891Snarayan  *	vdc_init_ports
2325655fd6a9Sachartre  *
2326655fd6a9Sachartre  * Description:
2327*8cd10891Snarayan  *	Initialize all the ports for this vdisk instance.
2328655fd6a9Sachartre  *
2329655fd6a9Sachartre  * Arguments:
2330*8cd10891Snarayan  *	vdc	- soft state pointer for this instance of the device driver.
2331*8cd10891Snarayan  *	mdp	- md pointer
2332*8cd10891Snarayan  *	vd_nodep - device md node.
2333655fd6a9Sachartre  *
2334655fd6a9Sachartre  * Return Code:
2335655fd6a9Sachartre  *	0	- Success.
2336655fd6a9Sachartre  *	ENOENT	- Expected node or property did not exist.
2337655fd6a9Sachartre  */
2338655fd6a9Sachartre static int
2339*8cd10891Snarayan vdc_init_ports(vdc_t *vdc, md_t *mdp, mde_cookie_t vd_nodep)
2340655fd6a9Sachartre {
2341655fd6a9Sachartre 	int		status = 0;
2342*8cd10891Snarayan 	int		idx;
2343*8cd10891Snarayan 	int		num_nodes;
2344*8cd10891Snarayan 	int		num_vports;
2345*8cd10891Snarayan 	int		num_chans;
2346*8cd10891Snarayan 	int		listsz;
2347*8cd10891Snarayan 	mde_cookie_t	vd_port;
2348*8cd10891Snarayan 	mde_cookie_t	*chanp = NULL;
2349*8cd10891Snarayan 	mde_cookie_t	*portp = NULL;
2350*8cd10891Snarayan 	vdc_server_t	*srvr;
2351*8cd10891Snarayan 	vdc_server_t	*prev_srvr = NULL;
2352655fd6a9Sachartre 
2353*8cd10891Snarayan 	/*
2354*8cd10891Snarayan 	 * We now walk the MD nodes to find the port nodes for this vdisk.
2355*8cd10891Snarayan 	 */
2356655fd6a9Sachartre 	num_nodes = md_node_count(mdp);
2357655fd6a9Sachartre 	ASSERT(num_nodes > 0);
2358655fd6a9Sachartre 
2359655fd6a9Sachartre 	listsz = num_nodes * sizeof (mde_cookie_t);
2360655fd6a9Sachartre 
2361655fd6a9Sachartre 	/* allocate memory for nodes */
2362*8cd10891Snarayan 	portp = kmem_zalloc(listsz, KM_SLEEP);
2363655fd6a9Sachartre 	chanp = kmem_zalloc(listsz, KM_SLEEP);
2364655fd6a9Sachartre 
2365*8cd10891Snarayan 	num_vports = md_scan_dag(mdp, vd_nodep,
2366*8cd10891Snarayan 	    md_find_name(mdp, VDC_MD_PORT_NAME),
2367*8cd10891Snarayan 	    md_find_name(mdp, "fwd"), portp);
2368*8cd10891Snarayan 	if (num_vports == 0) {
2369*8cd10891Snarayan 		DMSGX(0, "Found no '%s' node for '%s' port\n",
2370*8cd10891Snarayan 		    VDC_MD_PORT_NAME, VDC_MD_VDEV_NAME);
2371*8cd10891Snarayan 		status = ENOENT;
2372*8cd10891Snarayan 		goto done;
2373*8cd10891Snarayan 	}
2374*8cd10891Snarayan 
2375*8cd10891Snarayan 	DMSGX(1, "Found %d '%s' node(s) for '%s' port\n",
2376*8cd10891Snarayan 	    num_vports, VDC_MD_PORT_NAME, VDC_MD_VDEV_NAME);
2377*8cd10891Snarayan 
2378*8cd10891Snarayan 	vdc->num_servers = 0;
2379*8cd10891Snarayan 	for (idx = 0; idx < num_vports; idx++) {
2380*8cd10891Snarayan 
2381*8cd10891Snarayan 		/* initialize this port */
2382*8cd10891Snarayan 		vd_port = portp[idx];
2383*8cd10891Snarayan 		srvr = kmem_zalloc(sizeof (vdc_server_t), KM_SLEEP);
2384*8cd10891Snarayan 		srvr->vdcp = vdc;
2385*8cd10891Snarayan 
2386*8cd10891Snarayan 		/* get port id */
2387*8cd10891Snarayan 		if (md_get_prop_val(mdp, vd_port, VDC_MD_ID, &srvr->id) != 0) {
2388*8cd10891Snarayan 			cmn_err(CE_NOTE, "vDisk port '%s' property not found",
2389*8cd10891Snarayan 			    VDC_MD_ID);
2390*8cd10891Snarayan 			kmem_free(srvr, sizeof (vdc_server_t));
2391*8cd10891Snarayan 			continue;
2392*8cd10891Snarayan 		}
2393*8cd10891Snarayan 
2394*8cd10891Snarayan 		/* set the connection timeout */
2395*8cd10891Snarayan 		if (md_get_prop_val(mdp, vd_port, VDC_MD_TIMEOUT,
2396*8cd10891Snarayan 		    &srvr->ctimeout) != 0) {
2397*8cd10891Snarayan 			srvr->ctimeout = 0;
2398*8cd10891Snarayan 		}
2399*8cd10891Snarayan 
2400*8cd10891Snarayan 		/* get the ldc id */
2401*8cd10891Snarayan 		num_chans = md_scan_dag(mdp, vd_port,
24021ae08745Sheppo 		    md_find_name(mdp, VDC_MD_CHAN_NAME),
24031ae08745Sheppo 		    md_find_name(mdp, "fwd"), chanp);
24041ae08745Sheppo 
24051ae08745Sheppo 		/* expecting at least one channel */
24061ae08745Sheppo 		if (num_chans <= 0) {
24071ae08745Sheppo 			cmn_err(CE_NOTE, "No '%s' node for '%s' port",
24081ae08745Sheppo 			    VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME);
2409*8cd10891Snarayan 			kmem_free(srvr, sizeof (vdc_server_t));
2410*8cd10891Snarayan 			continue;
24111ae08745Sheppo 		} else if (num_chans != 1) {
2412*8cd10891Snarayan 			DMSGX(0, "Expected 1 '%s' node for '%s' port, "
2413*8cd10891Snarayan 			    "found %d\n", VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME,
2414*8cd10891Snarayan 			    num_chans);
24151ae08745Sheppo 		}
24161ae08745Sheppo 
24171ae08745Sheppo 		/*
24181ae08745Sheppo 		 * We use the first channel found (index 0), irrespective of how
24191ae08745Sheppo 		 * many are there in total.
24201ae08745Sheppo 		 */
2421*8cd10891Snarayan 		if (md_get_prop_val(mdp, chanp[0], VDC_MD_ID,
2422*8cd10891Snarayan 		    &srvr->ldc_id) != 0) {
2423*8cd10891Snarayan 			cmn_err(CE_NOTE, "Channel '%s' property not found",
2424*8cd10891Snarayan 			    VDC_MD_ID);
2425*8cd10891Snarayan 			kmem_free(srvr, sizeof (vdc_server_t));
2426*8cd10891Snarayan 			continue;
2427*8cd10891Snarayan 		}
2428*8cd10891Snarayan 
2429*8cd10891Snarayan 		/*
2430*8cd10891Snarayan 		 * now initialise LDC channel which will be used to
2431*8cd10891Snarayan 		 * communicate with this server
2432*8cd10891Snarayan 		 */
2433*8cd10891Snarayan 		if (vdc_do_ldc_init(vdc, srvr) != 0) {
2434*8cd10891Snarayan 			kmem_free(srvr, sizeof (vdc_server_t));
2435*8cd10891Snarayan 			continue;
2436*8cd10891Snarayan 		}
2437*8cd10891Snarayan 
2438*8cd10891Snarayan 		/* add server to list */
2439*8cd10891Snarayan 		if (prev_srvr) {
2440*8cd10891Snarayan 			prev_srvr->next = srvr;
2441*8cd10891Snarayan 		} else {
2442*8cd10891Snarayan 			vdc->server_list = srvr;
2443*8cd10891Snarayan 			prev_srvr = srvr;
2444*8cd10891Snarayan 		}
2445*8cd10891Snarayan 
2446*8cd10891Snarayan 		/* inc numbers of servers */
2447*8cd10891Snarayan 		vdc->num_servers++;
2448*8cd10891Snarayan 	}
2449*8cd10891Snarayan 
2450*8cd10891Snarayan 	/*
2451*8cd10891Snarayan 	 * Adjust the max number of handshake retries to match
2452*8cd10891Snarayan 	 * the number of vdisk servers.
2453*8cd10891Snarayan 	 */
2454*8cd10891Snarayan 	if (vdc_hshake_retries < vdc->num_servers)
2455*8cd10891Snarayan 		vdc_hshake_retries = vdc->num_servers;
2456*8cd10891Snarayan 
2457*8cd10891Snarayan 	/* pick first server as current server */
2458*8cd10891Snarayan 	if (vdc->server_list != NULL) {
2459*8cd10891Snarayan 		vdc->curr_server = vdc->server_list;
2460*8cd10891Snarayan 		status = 0;
2461*8cd10891Snarayan 	} else {
24621ae08745Sheppo 		status = ENOENT;
24631ae08745Sheppo 	}
24641ae08745Sheppo 
24651ae08745Sheppo done:
24661ae08745Sheppo 	kmem_free(chanp, listsz);
2467*8cd10891Snarayan 	kmem_free(portp, listsz);
24681ae08745Sheppo 	return (status);
24691ae08745Sheppo }
24701ae08745Sheppo 
2471*8cd10891Snarayan 
2472*8cd10891Snarayan /*
2473*8cd10891Snarayan  * Function:
2474*8cd10891Snarayan  *	vdc_do_ldc_up
2475*8cd10891Snarayan  *
2476*8cd10891Snarayan  * Description:
2477*8cd10891Snarayan  *	Bring the channel for the current server up.
2478*8cd10891Snarayan  *
2479*8cd10891Snarayan  * Arguments:
2480*8cd10891Snarayan  *	vdc	- soft state pointer for this instance of the device driver.
2481*8cd10891Snarayan  *
2482*8cd10891Snarayan  * Return Code:
2483*8cd10891Snarayan  *	0		- Success.
2484*8cd10891Snarayan  *	EINVAL		- Driver is detaching / LDC error
2485*8cd10891Snarayan  *	ECONNREFUSED	- Other end is not listening
2486*8cd10891Snarayan  */
24870a55fbb7Slm66018 static int
24880a55fbb7Slm66018 vdc_do_ldc_up(vdc_t *vdc)
24890a55fbb7Slm66018 {
24900a55fbb7Slm66018 	int		status;
24913af08d82Slm66018 	ldc_status_t	ldc_state;
24920a55fbb7Slm66018 
2493*8cd10891Snarayan 	ASSERT(MUTEX_HELD(&vdc->lock));
2494*8cd10891Snarayan 
24953af08d82Slm66018 	DMSG(vdc, 0, "[%d] Bringing up channel %lx\n",
2496*8cd10891Snarayan 	    vdc->instance, vdc->curr_server->ldc_id);
24973af08d82Slm66018 
24983af08d82Slm66018 	if (vdc->lifecycle == VDC_LC_DETACHING)
24993af08d82Slm66018 		return (EINVAL);
25000a55fbb7Slm66018 
2501*8cd10891Snarayan 	if ((status = ldc_up(vdc->curr_server->ldc_handle)) != 0) {
25020a55fbb7Slm66018 		switch (status) {
25030a55fbb7Slm66018 		case ECONNREFUSED:	/* listener not ready at other end */
25043af08d82Slm66018 			DMSG(vdc, 0, "[%d] ldc_up(%lx,...) return %d\n",
2505*8cd10891Snarayan 			    vdc->instance, vdc->curr_server->ldc_id, status);
25060a55fbb7Slm66018 			status = 0;
25070a55fbb7Slm66018 			break;
25080a55fbb7Slm66018 		default:
25093af08d82Slm66018 			DMSG(vdc, 0, "[%d] Failed to bring up LDC: "
2510*8cd10891Snarayan 			    "channel=%ld, err=%d", vdc->instance,
2511*8cd10891Snarayan 			    vdc->curr_server->ldc_id, status);
25123af08d82Slm66018 			break;
25133af08d82Slm66018 		}
25143af08d82Slm66018 	}
25153af08d82Slm66018 
2516*8cd10891Snarayan 	if (ldc_status(vdc->curr_server->ldc_handle, &ldc_state) == 0) {
2517*8cd10891Snarayan 		vdc->curr_server->ldc_state = ldc_state;
25183af08d82Slm66018 		if (ldc_state == LDC_UP) {
25193af08d82Slm66018 			DMSG(vdc, 0, "[%d] LDC channel already up\n",
25203af08d82Slm66018 			    vdc->instance);
25213af08d82Slm66018 			vdc->seq_num = 1;
25223af08d82Slm66018 			vdc->seq_num_reply = 0;
25230a55fbb7Slm66018 		}
25240a55fbb7Slm66018 	}
25250a55fbb7Slm66018 
25260a55fbb7Slm66018 	return (status);
25270a55fbb7Slm66018 }
25280a55fbb7Slm66018 
25290a55fbb7Slm66018 /*
25300a55fbb7Slm66018  * Function:
25310a55fbb7Slm66018  *	vdc_terminate_ldc()
25320a55fbb7Slm66018  *
25330a55fbb7Slm66018  * Description:
25340a55fbb7Slm66018  *
25350a55fbb7Slm66018  * Arguments:
25360a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
2537*8cd10891Snarayan  *	srvr	- vdc per-server info structure
25380a55fbb7Slm66018  *
25390a55fbb7Slm66018  * Return Code:
25400a55fbb7Slm66018  *	None
25410a55fbb7Slm66018  */
25421ae08745Sheppo static void
2543*8cd10891Snarayan vdc_terminate_ldc(vdc_t *vdc, vdc_server_t *srvr)
25441ae08745Sheppo {
25451ae08745Sheppo 	int	instance = ddi_get_instance(vdc->dip);
25461ae08745Sheppo 
2547*8cd10891Snarayan 	if (srvr->state & VDC_LDC_OPEN) {
2548*8cd10891Snarayan 		DMSG(vdc, 0, "[%d] ldc_close()\n", instance);
2549*8cd10891Snarayan 		(void) ldc_close(srvr->ldc_handle);
2550*8cd10891Snarayan 	}
2551*8cd10891Snarayan 	if (srvr->state & VDC_LDC_CB) {
2552*8cd10891Snarayan 		DMSG(vdc, 0, "[%d] ldc_unreg_callback()\n", instance);
2553*8cd10891Snarayan 		(void) ldc_unreg_callback(srvr->ldc_handle);
2554*8cd10891Snarayan 	}
2555*8cd10891Snarayan 	if (srvr->state & VDC_LDC_INIT) {
2556*8cd10891Snarayan 		DMSG(vdc, 0, "[%d] ldc_fini()\n", instance);
2557*8cd10891Snarayan 		(void) ldc_fini(srvr->ldc_handle);
2558*8cd10891Snarayan 		srvr->ldc_handle = NULL;
2559*8cd10891Snarayan 	}
2560*8cd10891Snarayan 
2561*8cd10891Snarayan 	srvr->state &= ~(VDC_LDC_INIT | VDC_LDC_CB | VDC_LDC_OPEN);
2562*8cd10891Snarayan }
2563*8cd10891Snarayan 
2564*8cd10891Snarayan /*
2565*8cd10891Snarayan  * Function:
2566*8cd10891Snarayan  *	vdc_fini_ports()
2567*8cd10891Snarayan  *
2568*8cd10891Snarayan  * Description:
2569*8cd10891Snarayan  *	Finalize all ports by closing the channel associated with each
2570*8cd10891Snarayan  *	port and also freeing the server structure.
2571*8cd10891Snarayan  *
2572*8cd10891Snarayan  * Arguments:
2573*8cd10891Snarayan  *	vdc	- soft state pointer for this instance of the device driver.
2574*8cd10891Snarayan  *
2575*8cd10891Snarayan  * Return Code:
2576*8cd10891Snarayan  *	None
2577*8cd10891Snarayan  */
2578*8cd10891Snarayan static void
2579*8cd10891Snarayan vdc_fini_ports(vdc_t *vdc)
2580*8cd10891Snarayan {
2581*8cd10891Snarayan 	int		instance = ddi_get_instance(vdc->dip);
2582*8cd10891Snarayan 	vdc_server_t	*srvr, *prev_srvr;
2583*8cd10891Snarayan 
25841ae08745Sheppo 	ASSERT(vdc != NULL);
25851ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
25861ae08745Sheppo 
25873af08d82Slm66018 	DMSG(vdc, 0, "[%d] initialized=%x\n", instance, vdc->initialized);
25881ae08745Sheppo 
2589*8cd10891Snarayan 	srvr = vdc->server_list;
2590*8cd10891Snarayan 
2591*8cd10891Snarayan 	while (srvr) {
2592*8cd10891Snarayan 
2593*8cd10891Snarayan 		vdc_terminate_ldc(vdc, srvr);
2594*8cd10891Snarayan 
2595*8cd10891Snarayan 		/* next server */
2596*8cd10891Snarayan 		prev_srvr = srvr;
2597*8cd10891Snarayan 		srvr = srvr->next;
2598*8cd10891Snarayan 
2599*8cd10891Snarayan 		/* free server */
2600*8cd10891Snarayan 		kmem_free(prev_srvr, sizeof (vdc_server_t));
26011ae08745Sheppo 	}
26021ae08745Sheppo 
2603*8cd10891Snarayan 	vdc->server_list = NULL;
26041ae08745Sheppo }
26051ae08745Sheppo 
26061ae08745Sheppo /* -------------------------------------------------------------------------- */
26071ae08745Sheppo 
26081ae08745Sheppo /*
26091ae08745Sheppo  * Descriptor Ring helper routines
26101ae08745Sheppo  */
26111ae08745Sheppo 
26120a55fbb7Slm66018 /*
26130a55fbb7Slm66018  * Function:
26140a55fbb7Slm66018  *	vdc_init_descriptor_ring()
26150a55fbb7Slm66018  *
26160a55fbb7Slm66018  * Description:
26170a55fbb7Slm66018  *
26180a55fbb7Slm66018  * Arguments:
26190a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
26200a55fbb7Slm66018  *
26210a55fbb7Slm66018  * Return Code:
26220a55fbb7Slm66018  *	0	- Success
26230a55fbb7Slm66018  */
26241ae08745Sheppo static int
26251ae08745Sheppo vdc_init_descriptor_ring(vdc_t *vdc)
26261ae08745Sheppo {
26271ae08745Sheppo 	vd_dring_entry_t	*dep = NULL;	/* DRing Entry pointer */
26280a55fbb7Slm66018 	int	status = 0;
26291ae08745Sheppo 	int	i;
26301ae08745Sheppo 
26313af08d82Slm66018 	DMSG(vdc, 0, "[%d] initialized=%x\n", vdc->instance, vdc->initialized);
26321ae08745Sheppo 
26331ae08745Sheppo 	ASSERT(vdc != NULL);
26341ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
26351ae08745Sheppo 
2636e1ebb9ecSlm66018 	/* ensure we have enough room to store max sized block */
2637e1ebb9ecSlm66018 	ASSERT(maxphys <= VD_MAX_BLOCK_SIZE);
2638e1ebb9ecSlm66018 
26390a55fbb7Slm66018 	if ((vdc->initialized & VDC_DRING_INIT) == 0) {
26403af08d82Slm66018 		DMSG(vdc, 0, "[%d] ldc_mem_dring_create\n", vdc->instance);
2641e1ebb9ecSlm66018 		/*
2642e1ebb9ecSlm66018 		 * Calculate the maximum block size we can transmit using one
2643e1ebb9ecSlm66018 		 * Descriptor Ring entry from the attributes returned by the
2644e1ebb9ecSlm66018 		 * vDisk server. This is subject to a minimum of 'maxphys'
2645e1ebb9ecSlm66018 		 * as we do not have the capability to split requests over
2646e1ebb9ecSlm66018 		 * multiple DRing entries.
2647e1ebb9ecSlm66018 		 */
2648e1ebb9ecSlm66018 		if ((vdc->max_xfer_sz * vdc->block_size) < maxphys) {
26493af08d82Slm66018 			DMSG(vdc, 0, "[%d] using minimum DRing size\n",
2650e1ebb9ecSlm66018 			    vdc->instance);
2651e1ebb9ecSlm66018 			vdc->dring_max_cookies = maxphys / PAGESIZE;
2652e1ebb9ecSlm66018 		} else {
2653e1ebb9ecSlm66018 			vdc->dring_max_cookies =
2654e1ebb9ecSlm66018 			    (vdc->max_xfer_sz * vdc->block_size) / PAGESIZE;
2655e1ebb9ecSlm66018 		}
2656e1ebb9ecSlm66018 		vdc->dring_entry_size = (sizeof (vd_dring_entry_t) +
2657e1ebb9ecSlm66018 		    (sizeof (ldc_mem_cookie_t) *
2658e1ebb9ecSlm66018 		    (vdc->dring_max_cookies - 1)));
2659e1ebb9ecSlm66018 		vdc->dring_len = VD_DRING_LEN;
2660e1ebb9ecSlm66018 
2661e1ebb9ecSlm66018 		status = ldc_mem_dring_create(vdc->dring_len,
2662*8cd10891Snarayan 		    vdc->dring_entry_size, &vdc->dring_hdl);
2663*8cd10891Snarayan 		if ((vdc->dring_hdl == NULL) || (status != 0)) {
26643af08d82Slm66018 			DMSG(vdc, 0, "[%d] Descriptor ring creation failed",
2665e1ebb9ecSlm66018 			    vdc->instance);
26661ae08745Sheppo 			return (status);
26671ae08745Sheppo 		}
26680a55fbb7Slm66018 		vdc->initialized |= VDC_DRING_INIT;
26690a55fbb7Slm66018 	}
26701ae08745Sheppo 
26710a55fbb7Slm66018 	if ((vdc->initialized & VDC_DRING_BOUND) == 0) {
26723af08d82Slm66018 		DMSG(vdc, 0, "[%d] ldc_mem_dring_bind\n", vdc->instance);
26730a55fbb7Slm66018 		vdc->dring_cookie =
26740a55fbb7Slm66018 		    kmem_zalloc(sizeof (ldc_mem_cookie_t), KM_SLEEP);
26751ae08745Sheppo 
2676*8cd10891Snarayan 		status = ldc_mem_dring_bind(vdc->curr_server->ldc_handle,
2677*8cd10891Snarayan 		    vdc->dring_hdl,
26784bac2208Snarayan 		    LDC_SHADOW_MAP|LDC_DIRECT_MAP, LDC_MEM_RW,
26790a55fbb7Slm66018 		    &vdc->dring_cookie[0],
26801ae08745Sheppo 		    &vdc->dring_cookie_count);
26811ae08745Sheppo 		if (status != 0) {
26823af08d82Slm66018 			DMSG(vdc, 0, "[%d] Failed to bind descriptor ring "
26833af08d82Slm66018 			    "(%lx) to channel (%lx) status=%d\n",
2684*8cd10891Snarayan 			    vdc->instance, vdc->dring_hdl,
2685*8cd10891Snarayan 			    vdc->curr_server->ldc_handle, status);
26861ae08745Sheppo 			return (status);
26871ae08745Sheppo 		}
26881ae08745Sheppo 		ASSERT(vdc->dring_cookie_count == 1);
26891ae08745Sheppo 		vdc->initialized |= VDC_DRING_BOUND;
26900a55fbb7Slm66018 	}
26911ae08745Sheppo 
2692*8cd10891Snarayan 	status = ldc_mem_dring_info(vdc->dring_hdl, &vdc->dring_mem_info);
26931ae08745Sheppo 	if (status != 0) {
26943af08d82Slm66018 		DMSG(vdc, 0,
26953af08d82Slm66018 		    "[%d] Failed to get info for descriptor ring (%lx)\n",
2696*8cd10891Snarayan 		    vdc->instance, vdc->dring_hdl);
26971ae08745Sheppo 		return (status);
26981ae08745Sheppo 	}
26991ae08745Sheppo 
27000a55fbb7Slm66018 	if ((vdc->initialized & VDC_DRING_LOCAL) == 0) {
27013af08d82Slm66018 		DMSG(vdc, 0, "[%d] local dring\n", vdc->instance);
27020a55fbb7Slm66018 
27031ae08745Sheppo 		/* Allocate the local copy of this dring */
27040a55fbb7Slm66018 		vdc->local_dring =
2705e1ebb9ecSlm66018 		    kmem_zalloc(vdc->dring_len * sizeof (vdc_local_desc_t),
27061ae08745Sheppo 		    KM_SLEEP);
27071ae08745Sheppo 		vdc->initialized |= VDC_DRING_LOCAL;
27080a55fbb7Slm66018 	}
27091ae08745Sheppo 
27101ae08745Sheppo 	/*
27110a55fbb7Slm66018 	 * Mark all DRing entries as free and initialize the private
27120a55fbb7Slm66018 	 * descriptor's memory handles. If any entry is initialized,
27130a55fbb7Slm66018 	 * we need to free it later so we set the bit in 'initialized'
27140a55fbb7Slm66018 	 * at the start.
27151ae08745Sheppo 	 */
27161ae08745Sheppo 	vdc->initialized |= VDC_DRING_ENTRY;
2717e1ebb9ecSlm66018 	for (i = 0; i < vdc->dring_len; i++) {
27181ae08745Sheppo 		dep = VDC_GET_DRING_ENTRY_PTR(vdc, i);
27191ae08745Sheppo 		dep->hdr.dstate = VIO_DESC_FREE;
27201ae08745Sheppo 
2721*8cd10891Snarayan 		status = ldc_mem_alloc_handle(vdc->curr_server->ldc_handle,
27221ae08745Sheppo 		    &vdc->local_dring[i].desc_mhdl);
27231ae08745Sheppo 		if (status != 0) {
27243af08d82Slm66018 			DMSG(vdc, 0, "![%d] Failed to alloc mem handle for"
27251ae08745Sheppo 			    " descriptor %d", vdc->instance, i);
27261ae08745Sheppo 			return (status);
27271ae08745Sheppo 		}
27283af08d82Slm66018 		vdc->local_dring[i].is_free = B_TRUE;
27291ae08745Sheppo 		vdc->local_dring[i].dep = dep;
27301ae08745Sheppo 	}
27311ae08745Sheppo 
27323af08d82Slm66018 	/* Initialize the starting index */
27333af08d82Slm66018 	vdc->dring_curr_idx = 0;
27341ae08745Sheppo 
27351ae08745Sheppo 	return (status);
27361ae08745Sheppo }
27371ae08745Sheppo 
27380a55fbb7Slm66018 /*
27390a55fbb7Slm66018  * Function:
27400a55fbb7Slm66018  *	vdc_destroy_descriptor_ring()
27410a55fbb7Slm66018  *
27420a55fbb7Slm66018  * Description:
27430a55fbb7Slm66018  *
27440a55fbb7Slm66018  * Arguments:
27450a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
27460a55fbb7Slm66018  *
27470a55fbb7Slm66018  * Return Code:
27480a55fbb7Slm66018  *	None
27490a55fbb7Slm66018  */
27501ae08745Sheppo static void
27511ae08745Sheppo vdc_destroy_descriptor_ring(vdc_t *vdc)
27521ae08745Sheppo {
27530a55fbb7Slm66018 	vdc_local_desc_t	*ldep = NULL;	/* Local Dring Entry Pointer */
27541ae08745Sheppo 	ldc_mem_handle_t	mhdl = NULL;
27553af08d82Slm66018 	ldc_mem_info_t		minfo;
27561ae08745Sheppo 	int			status = -1;
27571ae08745Sheppo 	int			i;	/* loop */
27581ae08745Sheppo 
27591ae08745Sheppo 	ASSERT(vdc != NULL);
27601ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
27611ae08745Sheppo 
27623af08d82Slm66018 	DMSG(vdc, 0, "[%d] Entered\n", vdc->instance);
27631ae08745Sheppo 
27641ae08745Sheppo 	if (vdc->initialized & VDC_DRING_ENTRY) {
27653af08d82Slm66018 		DMSG(vdc, 0,
27663af08d82Slm66018 		    "[%d] Removing Local DRing entries\n", vdc->instance);
2767e1ebb9ecSlm66018 		for (i = 0; i < vdc->dring_len; i++) {
27680a55fbb7Slm66018 			ldep = &vdc->local_dring[i];
27690a55fbb7Slm66018 			mhdl = ldep->desc_mhdl;
27701ae08745Sheppo 
27710a55fbb7Slm66018 			if (mhdl == NULL)
27720a55fbb7Slm66018 				continue;
27730a55fbb7Slm66018 
27743af08d82Slm66018 			if ((status = ldc_mem_info(mhdl, &minfo)) != 0) {
27753af08d82Slm66018 				DMSG(vdc, 0,
27763af08d82Slm66018 				    "ldc_mem_info returned an error: %d\n",
27773af08d82Slm66018 				    status);
27783af08d82Slm66018 
27793af08d82Slm66018 				/*
27803af08d82Slm66018 				 * This must mean that the mem handle
27813af08d82Slm66018 				 * is not valid. Clear it out so that
27823af08d82Slm66018 				 * no one tries to use it.
27833af08d82Slm66018 				 */
27843af08d82Slm66018 				ldep->desc_mhdl = NULL;
27853af08d82Slm66018 				continue;
27863af08d82Slm66018 			}
27873af08d82Slm66018 
27883af08d82Slm66018 			if (minfo.status == LDC_BOUND) {
27893af08d82Slm66018 				(void) ldc_mem_unbind_handle(mhdl);
27903af08d82Slm66018 			}
27913af08d82Slm66018 
27921ae08745Sheppo 			(void) ldc_mem_free_handle(mhdl);
27933af08d82Slm66018 
27943af08d82Slm66018 			ldep->desc_mhdl = NULL;
27951ae08745Sheppo 		}
27961ae08745Sheppo 		vdc->initialized &= ~VDC_DRING_ENTRY;
27971ae08745Sheppo 	}
27981ae08745Sheppo 
27991ae08745Sheppo 	if (vdc->initialized & VDC_DRING_LOCAL) {
28003af08d82Slm66018 		DMSG(vdc, 0, "[%d] Freeing Local DRing\n", vdc->instance);
28011ae08745Sheppo 		kmem_free(vdc->local_dring,
2802e1ebb9ecSlm66018 		    vdc->dring_len * sizeof (vdc_local_desc_t));
28031ae08745Sheppo 		vdc->initialized &= ~VDC_DRING_LOCAL;
28041ae08745Sheppo 	}
28051ae08745Sheppo 
28061ae08745Sheppo 	if (vdc->initialized & VDC_DRING_BOUND) {
28073af08d82Slm66018 		DMSG(vdc, 0, "[%d] Unbinding DRing\n", vdc->instance);
2808*8cd10891Snarayan 		status = ldc_mem_dring_unbind(vdc->dring_hdl);
28091ae08745Sheppo 		if (status == 0) {
28101ae08745Sheppo 			vdc->initialized &= ~VDC_DRING_BOUND;
28111ae08745Sheppo 		} else {
28123af08d82Slm66018 			DMSG(vdc, 0, "[%d] Error %d unbinding DRing %lx",
2813*8cd10891Snarayan 			    vdc->instance, status, vdc->dring_hdl);
28141ae08745Sheppo 		}
28153af08d82Slm66018 		kmem_free(vdc->dring_cookie, sizeof (ldc_mem_cookie_t));
28161ae08745Sheppo 	}
28171ae08745Sheppo 
28181ae08745Sheppo 	if (vdc->initialized & VDC_DRING_INIT) {
28193af08d82Slm66018 		DMSG(vdc, 0, "[%d] Destroying DRing\n", vdc->instance);
2820*8cd10891Snarayan 		status = ldc_mem_dring_destroy(vdc->dring_hdl);
28211ae08745Sheppo 		if (status == 0) {
2822*8cd10891Snarayan 			vdc->dring_hdl = NULL;
28231ae08745Sheppo 			bzero(&vdc->dring_mem_info, sizeof (ldc_mem_info_t));
28241ae08745Sheppo 			vdc->initialized &= ~VDC_DRING_INIT;
28251ae08745Sheppo 		} else {
28263af08d82Slm66018 			DMSG(vdc, 0, "[%d] Error %d destroying DRing (%lx)",
2827*8cd10891Snarayan 			    vdc->instance, status, vdc->dring_hdl);
28281ae08745Sheppo 		}
28291ae08745Sheppo 	}
28301ae08745Sheppo }
28311ae08745Sheppo 
28321ae08745Sheppo /*
28333af08d82Slm66018  * Function:
283490e2f9dcSlm66018  *	vdc_map_to_shared_dring()
28351ae08745Sheppo  *
28361ae08745Sheppo  * Description:
28373af08d82Slm66018  *	Copy contents of the local descriptor to the shared
28383af08d82Slm66018  *	memory descriptor.
28391ae08745Sheppo  *
28403af08d82Slm66018  * Arguments:
28413af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
28423af08d82Slm66018  *	idx	- descriptor ring index
28433af08d82Slm66018  *
28443af08d82Slm66018  * Return Code:
28453af08d82Slm66018  *	None
28461ae08745Sheppo  */
28471ae08745Sheppo static int
28483af08d82Slm66018 vdc_map_to_shared_dring(vdc_t *vdcp, int idx)
28491ae08745Sheppo {
28503af08d82Slm66018 	vdc_local_desc_t	*ldep;
28513af08d82Slm66018 	vd_dring_entry_t	*dep;
28523af08d82Slm66018 	int			rv;
28531ae08745Sheppo 
28543af08d82Slm66018 	ldep = &(vdcp->local_dring[idx]);
28551ae08745Sheppo 
28563af08d82Slm66018 	/* for now leave in the old pop_mem_hdl stuff */
28573af08d82Slm66018 	if (ldep->nbytes > 0) {
28583af08d82Slm66018 		rv = vdc_populate_mem_hdl(vdcp, ldep);
28593af08d82Slm66018 		if (rv) {
28603af08d82Slm66018 			DMSG(vdcp, 0, "[%d] Cannot populate mem handle\n",
28613af08d82Slm66018 			    vdcp->instance);
28623af08d82Slm66018 			return (rv);
28633af08d82Slm66018 		}
28643af08d82Slm66018 	}
28651ae08745Sheppo 
28663af08d82Slm66018 	/*
28673af08d82Slm66018 	 * fill in the data details into the DRing
28683af08d82Slm66018 	 */
2869d10e4ef2Snarayan 	dep = ldep->dep;
28701ae08745Sheppo 	ASSERT(dep != NULL);
28711ae08745Sheppo 
28723af08d82Slm66018 	dep->payload.req_id = VDC_GET_NEXT_REQ_ID(vdcp);
28733af08d82Slm66018 	dep->payload.operation = ldep->operation;
28743af08d82Slm66018 	dep->payload.addr = ldep->offset;
28753af08d82Slm66018 	dep->payload.nbytes = ldep->nbytes;
2876055d7c80Scarlsonj 	dep->payload.status = (uint32_t)-1;	/* vds will set valid value */
28773af08d82Slm66018 	dep->payload.slice = ldep->slice;
28783af08d82Slm66018 	dep->hdr.dstate = VIO_DESC_READY;
28793af08d82Slm66018 	dep->hdr.ack = 1;		/* request an ACK for every message */
28801ae08745Sheppo 
28813af08d82Slm66018 	return (0);
28821ae08745Sheppo }
28831ae08745Sheppo 
28841ae08745Sheppo /*
28851ae08745Sheppo  * Function:
28863af08d82Slm66018  *	vdc_send_request
28873af08d82Slm66018  *
28883af08d82Slm66018  * Description:
28893af08d82Slm66018  *	This routine writes the data to be transmitted to vds into the
28903af08d82Slm66018  *	descriptor, notifies vds that the ring has been updated and
28913af08d82Slm66018  *	then waits for the request to be processed.
28923af08d82Slm66018  *
28933af08d82Slm66018  * Arguments:
28943af08d82Slm66018  *	vdcp	  - the soft state pointer
28953af08d82Slm66018  *	operation - operation we want vds to perform (VD_OP_XXX)
28963af08d82Slm66018  *	addr	  - address of data buf to be read/written.
28973af08d82Slm66018  *	nbytes	  - number of bytes to read/write
28983af08d82Slm66018  *	slice	  - the disk slice this request is for
28993af08d82Slm66018  *	offset	  - relative disk offset
29003af08d82Slm66018  *	cb_type   - type of call - STRATEGY or SYNC
29013af08d82Slm66018  *	cb_arg	  - parameter to be sent to server (depends on VD_OP_XXX type)
29023af08d82Slm66018  *			. mode for ioctl(9e)
29033af08d82Slm66018  *			. LP64 diskaddr_t (block I/O)
29043af08d82Slm66018  *	dir	  - direction of operation (READ/WRITE/BOTH)
29053af08d82Slm66018  *
29063af08d82Slm66018  * Return Codes:
29073af08d82Slm66018  *	0
29083af08d82Slm66018  *	ENXIO
29093af08d82Slm66018  */
29103af08d82Slm66018 static int
29113af08d82Slm66018 vdc_send_request(vdc_t *vdcp, int operation, caddr_t addr,
29123af08d82Slm66018     size_t nbytes, int slice, diskaddr_t offset, int cb_type,
29133af08d82Slm66018     void *cb_arg, vio_desc_direction_t dir)
29143af08d82Slm66018 {
2915366a92acSlm66018 	int	rv = 0;
2916366a92acSlm66018 
29173af08d82Slm66018 	ASSERT(vdcp != NULL);
291887a7269eSachartre 	ASSERT(slice == VD_SLICE_NONE || slice < V_NUMPAR);
29193af08d82Slm66018 
29203af08d82Slm66018 	mutex_enter(&vdcp->lock);
29213af08d82Slm66018 
2922366a92acSlm66018 	/*
2923366a92acSlm66018 	 * If this is a block read/write operation we update the I/O statistics
2924366a92acSlm66018 	 * to indicate that the request is being put on the waitq to be
2925366a92acSlm66018 	 * serviced.
2926366a92acSlm66018 	 *
2927366a92acSlm66018 	 * We do it here (a common routine for both synchronous and strategy
2928366a92acSlm66018 	 * calls) for performance reasons - we are already holding vdc->lock
2929366a92acSlm66018 	 * so there is no extra locking overhead. We would have to explicitly
2930366a92acSlm66018 	 * grab the 'lock' mutex to update the stats if we were to do this
2931366a92acSlm66018 	 * higher up the stack in vdc_strategy() et. al.
2932366a92acSlm66018 	 */
2933366a92acSlm66018 	if ((operation == VD_OP_BREAD) || (operation == VD_OP_BWRITE)) {
2934366a92acSlm66018 		DTRACE_IO1(start, buf_t *, cb_arg);
293590e2f9dcSlm66018 		VD_KSTAT_WAITQ_ENTER(vdcp);
2936366a92acSlm66018 	}
2937366a92acSlm66018 
29383af08d82Slm66018 	do {
29393c96341aSnarayan 		while (vdcp->state != VDC_STATE_RUNNING) {
29403af08d82Slm66018 
29413c96341aSnarayan 			/* return error if detaching */
29423c96341aSnarayan 			if (vdcp->state == VDC_STATE_DETACH) {
2943366a92acSlm66018 				rv = ENXIO;
2944366a92acSlm66018 				goto done;
29453c96341aSnarayan 			}
2946655fd6a9Sachartre 
2947655fd6a9Sachartre 			/* fail request if connection timeout is reached */
2948655fd6a9Sachartre 			if (vdcp->ctimeout_reached) {
2949366a92acSlm66018 				rv = EIO;
2950366a92acSlm66018 				goto done;
2951655fd6a9Sachartre 			}
2952655fd6a9Sachartre 
29532f5224aeSachartre 			/*
29542f5224aeSachartre 			 * If we are panicking and the disk is not ready then
29552f5224aeSachartre 			 * we can't send any request because we can't complete
29562f5224aeSachartre 			 * the handshake now.
29572f5224aeSachartre 			 */
29582f5224aeSachartre 			if (ddi_in_panic()) {
2959366a92acSlm66018 				rv = EIO;
2960366a92acSlm66018 				goto done;
29612f5224aeSachartre 			}
29622f5224aeSachartre 
2963655fd6a9Sachartre 			cv_wait(&vdcp->running_cv, &vdcp->lock);
29643c96341aSnarayan 		}
29653c96341aSnarayan 
29663af08d82Slm66018 	} while (vdc_populate_descriptor(vdcp, operation, addr,
29673af08d82Slm66018 	    nbytes, slice, offset, cb_type, cb_arg, dir));
29683af08d82Slm66018 
2969366a92acSlm66018 done:
2970366a92acSlm66018 	/*
2971366a92acSlm66018 	 * If this is a block read/write we update the I/O statistics kstat
2972366a92acSlm66018 	 * to indicate that this request has been placed on the queue for
2973366a92acSlm66018 	 * processing (i.e sent to the vDisk server) - iostat(1M) will
2974366a92acSlm66018 	 * report the time waiting for the vDisk server under the %b column
2975366a92acSlm66018 	 * In the case of an error we simply take it off the wait queue.
2976366a92acSlm66018 	 */
2977366a92acSlm66018 	if ((operation == VD_OP_BREAD) || (operation == VD_OP_BWRITE)) {
2978366a92acSlm66018 		if (rv == 0) {
297990e2f9dcSlm66018 			VD_KSTAT_WAITQ_TO_RUNQ(vdcp);
2980366a92acSlm66018 			DTRACE_PROBE1(send, buf_t *, cb_arg);
2981366a92acSlm66018 		} else {
2982366a92acSlm66018 			VD_UPDATE_ERR_STATS(vdcp, vd_transerrs);
298390e2f9dcSlm66018 			VD_KSTAT_WAITQ_EXIT(vdcp);
2984366a92acSlm66018 			DTRACE_IO1(done, buf_t *, cb_arg);
2985366a92acSlm66018 		}
2986366a92acSlm66018 	}
2987366a92acSlm66018 
29883af08d82Slm66018 	mutex_exit(&vdcp->lock);
2989366a92acSlm66018 
2990366a92acSlm66018 	return (rv);
29913af08d82Slm66018 }
29923af08d82Slm66018 
29933af08d82Slm66018 
29943af08d82Slm66018 /*
29953af08d82Slm66018  * Function:
29961ae08745Sheppo  *	vdc_populate_descriptor
29971ae08745Sheppo  *
29981ae08745Sheppo  * Description:
29991ae08745Sheppo  *	This routine writes the data to be transmitted to vds into the
30001ae08745Sheppo  *	descriptor, notifies vds that the ring has been updated and
30011ae08745Sheppo  *	then waits for the request to be processed.
30021ae08745Sheppo  *
30031ae08745Sheppo  * Arguments:
30043af08d82Slm66018  *	vdcp	  - the soft state pointer
30051ae08745Sheppo  *	operation - operation we want vds to perform (VD_OP_XXX)
30063af08d82Slm66018  *	addr	  - address of data buf to be read/written.
30073af08d82Slm66018  *	nbytes	  - number of bytes to read/write
30083af08d82Slm66018  *	slice	  - the disk slice this request is for
30093af08d82Slm66018  *	offset	  - relative disk offset
30103af08d82Slm66018  *	cb_type   - type of call - STRATEGY or SYNC
30113af08d82Slm66018  *	cb_arg	  - parameter to be sent to server (depends on VD_OP_XXX type)
30121ae08745Sheppo  *			. mode for ioctl(9e)
30131ae08745Sheppo  *			. LP64 diskaddr_t (block I/O)
30143af08d82Slm66018  *	dir	  - direction of operation (READ/WRITE/BOTH)
30151ae08745Sheppo  *
30161ae08745Sheppo  * Return Codes:
30171ae08745Sheppo  *	0
30181ae08745Sheppo  *	EAGAIN
301917cadca8Slm66018  *	ECONNRESET
30201ae08745Sheppo  *	ENXIO
30211ae08745Sheppo  */
30221ae08745Sheppo static int
30233af08d82Slm66018 vdc_populate_descriptor(vdc_t *vdcp, int operation, caddr_t addr,
30243af08d82Slm66018     size_t nbytes, int slice, diskaddr_t offset, int cb_type,
30253af08d82Slm66018     void *cb_arg, vio_desc_direction_t dir)
30261ae08745Sheppo {
30273af08d82Slm66018 	vdc_local_desc_t	*local_dep = NULL; /* Local Dring Pointer */
30283af08d82Slm66018 	int			idx;		/* Index of DRing entry used */
30293af08d82Slm66018 	int			next_idx;
30301ae08745Sheppo 	vio_dring_msg_t		dmsg;
30313af08d82Slm66018 	size_t			msglen;
30328e6a2a04Slm66018 	int			rv;
30331ae08745Sheppo 
30343af08d82Slm66018 	ASSERT(MUTEX_HELD(&vdcp->lock));
30353af08d82Slm66018 	vdcp->threads_pending++;
30363af08d82Slm66018 loop:
30373af08d82Slm66018 	DMSG(vdcp, 2, ": dring_curr_idx = %d\n", vdcp->dring_curr_idx);
30381ae08745Sheppo 
30393af08d82Slm66018 	/* Get next available D-Ring entry */
30403af08d82Slm66018 	idx = vdcp->dring_curr_idx;
30413af08d82Slm66018 	local_dep = &(vdcp->local_dring[idx]);
30421ae08745Sheppo 
30433af08d82Slm66018 	if (!local_dep->is_free) {
30443af08d82Slm66018 		DMSG(vdcp, 2, "[%d]: dring full - waiting for space\n",
30453af08d82Slm66018 		    vdcp->instance);
30463af08d82Slm66018 		cv_wait(&vdcp->dring_free_cv, &vdcp->lock);
30473af08d82Slm66018 		if (vdcp->state == VDC_STATE_RUNNING ||
30483af08d82Slm66018 		    vdcp->state == VDC_STATE_HANDLE_PENDING) {
30493af08d82Slm66018 			goto loop;
30503af08d82Slm66018 		}
30513af08d82Slm66018 		vdcp->threads_pending--;
30523af08d82Slm66018 		return (ECONNRESET);
30531ae08745Sheppo 	}
30541ae08745Sheppo 
30553af08d82Slm66018 	next_idx = idx + 1;
30563af08d82Slm66018 	if (next_idx >= vdcp->dring_len)
30573af08d82Slm66018 		next_idx = 0;
30583af08d82Slm66018 	vdcp->dring_curr_idx = next_idx;
30591ae08745Sheppo 
30603af08d82Slm66018 	ASSERT(local_dep->is_free);
30611ae08745Sheppo 
30623af08d82Slm66018 	local_dep->operation = operation;
3063d10e4ef2Snarayan 	local_dep->addr = addr;
30643af08d82Slm66018 	local_dep->nbytes = nbytes;
30653af08d82Slm66018 	local_dep->slice = slice;
30663af08d82Slm66018 	local_dep->offset = offset;
30673af08d82Slm66018 	local_dep->cb_type = cb_type;
30683af08d82Slm66018 	local_dep->cb_arg = cb_arg;
30693af08d82Slm66018 	local_dep->dir = dir;
30703af08d82Slm66018 
30713af08d82Slm66018 	local_dep->is_free = B_FALSE;
30723af08d82Slm66018 
30733af08d82Slm66018 	rv = vdc_map_to_shared_dring(vdcp, idx);
30743af08d82Slm66018 	if (rv) {
30753af08d82Slm66018 		DMSG(vdcp, 0, "[%d]: cannot bind memory - waiting ..\n",
30763af08d82Slm66018 		    vdcp->instance);
30773af08d82Slm66018 		/* free the descriptor */
30783af08d82Slm66018 		local_dep->is_free = B_TRUE;
30793af08d82Slm66018 		vdcp->dring_curr_idx = idx;
30803af08d82Slm66018 		cv_wait(&vdcp->membind_cv, &vdcp->lock);
30813af08d82Slm66018 		if (vdcp->state == VDC_STATE_RUNNING ||
30823af08d82Slm66018 		    vdcp->state == VDC_STATE_HANDLE_PENDING) {
30833af08d82Slm66018 			goto loop;
30841ae08745Sheppo 		}
30853af08d82Slm66018 		vdcp->threads_pending--;
30863af08d82Slm66018 		return (ECONNRESET);
30871ae08745Sheppo 	}
30881ae08745Sheppo 
30891ae08745Sheppo 	/*
30901ae08745Sheppo 	 * Send a msg with the DRing details to vds
30911ae08745Sheppo 	 */
30921ae08745Sheppo 	VIO_INIT_DRING_DATA_TAG(dmsg);
30933af08d82Slm66018 	VDC_INIT_DRING_DATA_MSG_IDS(dmsg, vdcp);
30943af08d82Slm66018 	dmsg.dring_ident = vdcp->dring_ident;
30951ae08745Sheppo 	dmsg.start_idx = idx;
30961ae08745Sheppo 	dmsg.end_idx = idx;
30973af08d82Slm66018 	vdcp->seq_num++;
30981ae08745Sheppo 
3099366a92acSlm66018 	DTRACE_PROBE2(populate, int, vdcp->instance,
3100366a92acSlm66018 	    vdc_local_desc_t *, local_dep);
31013af08d82Slm66018 	DMSG(vdcp, 2, "ident=0x%lx, st=%u, end=%u, seq=%ld\n",
31023af08d82Slm66018 	    vdcp->dring_ident, dmsg.start_idx, dmsg.end_idx, dmsg.seq_num);
31031ae08745Sheppo 
31043af08d82Slm66018 	/*
31053af08d82Slm66018 	 * note we're still holding the lock here to
31063af08d82Slm66018 	 * make sure the message goes out in order !!!...
31073af08d82Slm66018 	 */
31083af08d82Slm66018 	msglen = sizeof (dmsg);
31093af08d82Slm66018 	rv = vdc_send(vdcp, (caddr_t)&dmsg, &msglen);
31103af08d82Slm66018 	switch (rv) {
31113af08d82Slm66018 	case ECONNRESET:
31123af08d82Slm66018 		/*
31133af08d82Slm66018 		 * vdc_send initiates the reset on failure.
31143af08d82Slm66018 		 * Since the transaction has already been put
31153af08d82Slm66018 		 * on the local dring, it will automatically get
31163af08d82Slm66018 		 * retried when the channel is reset. Given that,
31173af08d82Slm66018 		 * it is ok to just return success even though the
31183af08d82Slm66018 		 * send failed.
31193af08d82Slm66018 		 */
31203af08d82Slm66018 		rv = 0;
31213af08d82Slm66018 		break;
3122d10e4ef2Snarayan 
31233af08d82Slm66018 	case 0: /* EOK */
31243af08d82Slm66018 		DMSG(vdcp, 1, "sent via LDC: rv=%d\n", rv);
31253af08d82Slm66018 		break;
3126d10e4ef2Snarayan 
31273af08d82Slm66018 	default:
31283af08d82Slm66018 		goto cleanup_and_exit;
31293af08d82Slm66018 	}
3130e1ebb9ecSlm66018 
31313af08d82Slm66018 	vdcp->threads_pending--;
31323af08d82Slm66018 	return (rv);
31333af08d82Slm66018 
31343af08d82Slm66018 cleanup_and_exit:
31353af08d82Slm66018 	DMSG(vdcp, 0, "unexpected error, rv=%d\n", rv);
31363af08d82Slm66018 	return (ENXIO);
31371ae08745Sheppo }
31381ae08745Sheppo 
31391ae08745Sheppo /*
31403af08d82Slm66018  * Function:
31413af08d82Slm66018  *	vdc_do_sync_op
31423af08d82Slm66018  *
31433af08d82Slm66018  * Description:
31443af08d82Slm66018  * 	Wrapper around vdc_populate_descriptor that blocks until the
31453af08d82Slm66018  * 	response to the message is available.
31463af08d82Slm66018  *
31473af08d82Slm66018  * Arguments:
31483af08d82Slm66018  *	vdcp	  - the soft state pointer
31493af08d82Slm66018  *	operation - operation we want vds to perform (VD_OP_XXX)
31503af08d82Slm66018  *	addr	  - address of data buf to be read/written.
31513af08d82Slm66018  *	nbytes	  - number of bytes to read/write
31523af08d82Slm66018  *	slice	  - the disk slice this request is for
31533af08d82Slm66018  *	offset	  - relative disk offset
31543af08d82Slm66018  *	cb_type   - type of call - STRATEGY or SYNC
31553af08d82Slm66018  *	cb_arg	  - parameter to be sent to server (depends on VD_OP_XXX type)
31563af08d82Slm66018  *			. mode for ioctl(9e)
31573af08d82Slm66018  *			. LP64 diskaddr_t (block I/O)
31583af08d82Slm66018  *	dir	  - direction of operation (READ/WRITE/BOTH)
31592f5224aeSachartre  *	rconflict - check for reservation conflict in case of failure
31602f5224aeSachartre  *
31612f5224aeSachartre  * rconflict should be set to B_TRUE by most callers. Callers invoking the
31622f5224aeSachartre  * VD_OP_SCSICMD operation can set rconflict to B_FALSE if they check the
31632f5224aeSachartre  * result of a successful operation with vd_scsi_status().
31643af08d82Slm66018  *
31653af08d82Slm66018  * Return Codes:
31663af08d82Slm66018  *	0
31673af08d82Slm66018  *	EAGAIN
31683af08d82Slm66018  *	EFAULT
31693af08d82Slm66018  *	ENXIO
31703af08d82Slm66018  *	EIO
31710a55fbb7Slm66018  */
31723af08d82Slm66018 static int
31733af08d82Slm66018 vdc_do_sync_op(vdc_t *vdcp, int operation, caddr_t addr, size_t nbytes,
31743af08d82Slm66018     int slice, diskaddr_t offset, int cb_type, void *cb_arg,
31752f5224aeSachartre     vio_desc_direction_t dir, boolean_t rconflict)
31763af08d82Slm66018 {
31773af08d82Slm66018 	int status;
31782f5224aeSachartre 	vdc_io_t *vio;
31792f5224aeSachartre 	boolean_t check_resv_conflict = B_FALSE;
31803af08d82Slm66018 
31813af08d82Slm66018 	ASSERT(cb_type == CB_SYNC);
31821ae08745Sheppo 
31831ae08745Sheppo 	/*
31843af08d82Slm66018 	 * Grab the lock, if blocked wait until the server
31853af08d82Slm66018 	 * response causes us to wake up again.
31863af08d82Slm66018 	 */
31873af08d82Slm66018 	mutex_enter(&vdcp->lock);
31883af08d82Slm66018 	vdcp->sync_op_cnt++;
31893af08d82Slm66018 	while (vdcp->sync_op_blocked && vdcp->state != VDC_STATE_DETACH)
31903af08d82Slm66018 		cv_wait(&vdcp->sync_blocked_cv, &vdcp->lock);
31913af08d82Slm66018 
31923af08d82Slm66018 	if (vdcp->state == VDC_STATE_DETACH) {
31933af08d82Slm66018 		cv_broadcast(&vdcp->sync_blocked_cv);
31943af08d82Slm66018 		vdcp->sync_op_cnt--;
31953af08d82Slm66018 		mutex_exit(&vdcp->lock);
31963af08d82Slm66018 		return (ENXIO);
31973af08d82Slm66018 	}
31983af08d82Slm66018 
31993af08d82Slm66018 	/* now block anyone other thread entering after us */
32003af08d82Slm66018 	vdcp->sync_op_blocked = B_TRUE;
32013af08d82Slm66018 	vdcp->sync_op_pending = B_TRUE;
32023af08d82Slm66018 	mutex_exit(&vdcp->lock);
32033af08d82Slm66018 
3204655fd6a9Sachartre 	status = vdc_send_request(vdcp, operation, addr,
32053af08d82Slm66018 	    nbytes, slice, offset, cb_type, cb_arg, dir);
32063af08d82Slm66018 
3207655fd6a9Sachartre 	mutex_enter(&vdcp->lock);
3208655fd6a9Sachartre 
3209655fd6a9Sachartre 	if (status != 0) {
3210655fd6a9Sachartre 		vdcp->sync_op_pending = B_FALSE;
3211655fd6a9Sachartre 	} else {
32123af08d82Slm66018 		/*
32133af08d82Slm66018 		 * block until our transaction completes.
32143af08d82Slm66018 		 * Also anyone else waiting also gets to go next.
32153af08d82Slm66018 		 */
32163af08d82Slm66018 		while (vdcp->sync_op_pending && vdcp->state != VDC_STATE_DETACH)
32173af08d82Slm66018 			cv_wait(&vdcp->sync_pending_cv, &vdcp->lock);
32183af08d82Slm66018 
3219655fd6a9Sachartre 		DMSG(vdcp, 2, ": operation returned %d\n",
3220655fd6a9Sachartre 		    vdcp->sync_op_status);
32213c96341aSnarayan 		if (vdcp->state == VDC_STATE_DETACH) {
32223c96341aSnarayan 			vdcp->sync_op_pending = B_FALSE;
32233af08d82Slm66018 			status = ENXIO;
32243c96341aSnarayan 		} else {
32253af08d82Slm66018 			status = vdcp->sync_op_status;
32262f5224aeSachartre 			if (status != 0 && vdcp->failfast_interval != 0) {
32272f5224aeSachartre 				/*
32282f5224aeSachartre 				 * Operation has failed and failfast is enabled.
32292f5224aeSachartre 				 * We need to check if the failure is due to a
32302f5224aeSachartre 				 * reservation conflict if this was requested.
32312f5224aeSachartre 				 */
32322f5224aeSachartre 				check_resv_conflict = rconflict;
32332f5224aeSachartre 			}
32342f5224aeSachartre 
32353c96341aSnarayan 		}
3236655fd6a9Sachartre 	}
32373c96341aSnarayan 
32383af08d82Slm66018 	vdcp->sync_op_status = 0;
32393af08d82Slm66018 	vdcp->sync_op_blocked = B_FALSE;
32403af08d82Slm66018 	vdcp->sync_op_cnt--;
32413af08d82Slm66018 
32423af08d82Slm66018 	/* signal the next waiting thread */
32433af08d82Slm66018 	cv_signal(&vdcp->sync_blocked_cv);
32442f5224aeSachartre 
32452f5224aeSachartre 	/*
32462f5224aeSachartre 	 * We have to check for reservation conflict after unblocking sync
32472f5224aeSachartre 	 * operations because some sync operations will be used to do this
32482f5224aeSachartre 	 * check.
32492f5224aeSachartre 	 */
32502f5224aeSachartre 	if (check_resv_conflict) {
32512f5224aeSachartre 		vio = vdc_failfast_io_queue(vdcp, NULL);
32522f5224aeSachartre 		while (vio->vio_qtime != 0)
32532f5224aeSachartre 			cv_wait(&vdcp->failfast_io_cv, &vdcp->lock);
32542f5224aeSachartre 		kmem_free(vio, sizeof (vdc_io_t));
32552f5224aeSachartre 	}
32562f5224aeSachartre 
32573af08d82Slm66018 	mutex_exit(&vdcp->lock);
32583af08d82Slm66018 
32593af08d82Slm66018 	return (status);
32603af08d82Slm66018 }
32613af08d82Slm66018 
32623af08d82Slm66018 
32633af08d82Slm66018 /*
32643af08d82Slm66018  * Function:
32653af08d82Slm66018  *	vdc_drain_response()
32663af08d82Slm66018  *
32673af08d82Slm66018  * Description:
32681ae08745Sheppo  * 	When a guest is panicking, the completion of requests needs to be
32691ae08745Sheppo  * 	handled differently because interrupts are disabled and vdc
32701ae08745Sheppo  * 	will not get messages. We have to poll for the messages instead.
32713af08d82Slm66018  *
3272366a92acSlm66018  *	Note: since we don't have a buf_t available we cannot implement
3273366a92acSlm66018  *	the io:::done DTrace probe in this specific case.
3274366a92acSlm66018  *
32753af08d82Slm66018  * Arguments:
32763af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
32773af08d82Slm66018  *
32783af08d82Slm66018  * Return Code:
32793af08d82Slm66018  *	0	- Success
32801ae08745Sheppo  */
32813af08d82Slm66018 static int
32823af08d82Slm66018 vdc_drain_response(vdc_t *vdc)
32833af08d82Slm66018 {
32843af08d82Slm66018 	int 			rv, idx, retries;
32853af08d82Slm66018 	size_t			msglen;
32863af08d82Slm66018 	vdc_local_desc_t 	*ldep = NULL;	/* Local Dring Entry Pointer */
32873af08d82Slm66018 	vio_dring_msg_t		dmsg;
32883af08d82Slm66018 
32893af08d82Slm66018 	mutex_enter(&vdc->lock);
32903af08d82Slm66018 
32911ae08745Sheppo 	retries = 0;
32921ae08745Sheppo 	for (;;) {
32931ae08745Sheppo 		msglen = sizeof (dmsg);
3294*8cd10891Snarayan 		rv = ldc_read(vdc->curr_server->ldc_handle, (caddr_t)&dmsg,
3295*8cd10891Snarayan 		    &msglen);
32968e6a2a04Slm66018 		if (rv) {
32978e6a2a04Slm66018 			rv = EINVAL;
32981ae08745Sheppo 			break;
32991ae08745Sheppo 		}
33001ae08745Sheppo 
33011ae08745Sheppo 		/*
33021ae08745Sheppo 		 * if there are no packets wait and check again
33031ae08745Sheppo 		 */
33048e6a2a04Slm66018 		if ((rv == 0) && (msglen == 0)) {
33051ae08745Sheppo 			if (retries++ > vdc_dump_retries) {
33068e6a2a04Slm66018 				rv = EAGAIN;
33071ae08745Sheppo 				break;
33081ae08745Sheppo 			}
33091ae08745Sheppo 
3310d10e4ef2Snarayan 			drv_usecwait(vdc_usec_timeout_dump);
33111ae08745Sheppo 			continue;
33121ae08745Sheppo 		}
33131ae08745Sheppo 
33141ae08745Sheppo 		/*
33151ae08745Sheppo 		 * Ignore all messages that are not ACKs/NACKs to
33161ae08745Sheppo 		 * DRing requests.
33171ae08745Sheppo 		 */
33181ae08745Sheppo 		if ((dmsg.tag.vio_msgtype != VIO_TYPE_DATA) ||
33191ae08745Sheppo 		    (dmsg.tag.vio_subtype_env != VIO_DRING_DATA)) {
33203af08d82Slm66018 			DMSG(vdc, 0, "discard pkt: type=%d sub=%d env=%d\n",
33211ae08745Sheppo 			    dmsg.tag.vio_msgtype,
33221ae08745Sheppo 			    dmsg.tag.vio_subtype,
33231ae08745Sheppo 			    dmsg.tag.vio_subtype_env);
33241ae08745Sheppo 			continue;
33251ae08745Sheppo 		}
33261ae08745Sheppo 
33271ae08745Sheppo 		/*
33283af08d82Slm66018 		 * set the appropriate return value for the current request.
33291ae08745Sheppo 		 */
33301ae08745Sheppo 		switch (dmsg.tag.vio_subtype) {
33311ae08745Sheppo 		case VIO_SUBTYPE_ACK:
33328e6a2a04Slm66018 			rv = 0;
33331ae08745Sheppo 			break;
33341ae08745Sheppo 		case VIO_SUBTYPE_NACK:
33358e6a2a04Slm66018 			rv = EAGAIN;
33361ae08745Sheppo 			break;
33371ae08745Sheppo 		default:
33381ae08745Sheppo 			continue;
33391ae08745Sheppo 		}
33401ae08745Sheppo 
33413af08d82Slm66018 		idx = dmsg.start_idx;
33423af08d82Slm66018 		if (idx >= vdc->dring_len) {
33433af08d82Slm66018 			DMSG(vdc, 0, "[%d] Bogus ack data : start %d\n",
3344e1ebb9ecSlm66018 			    vdc->instance, idx);
33453af08d82Slm66018 			continue;
33461ae08745Sheppo 		}
33473af08d82Slm66018 		ldep = &vdc->local_dring[idx];
33483af08d82Slm66018 		if (ldep->dep->hdr.dstate != VIO_DESC_DONE) {
33493af08d82Slm66018 			DMSG(vdc, 0, "[%d] Entry @ %d - state !DONE %d\n",
33503af08d82Slm66018 			    vdc->instance, idx, ldep->dep->hdr.dstate);
33511ae08745Sheppo 			continue;
33521ae08745Sheppo 		}
33531ae08745Sheppo 
33543af08d82Slm66018 		DMSG(vdc, 1, "[%d] Depopulating idx=%d state=%d\n",
33553af08d82Slm66018 		    vdc->instance, idx, ldep->dep->hdr.dstate);
3356366a92acSlm66018 
33573af08d82Slm66018 		rv = vdc_depopulate_descriptor(vdc, idx);
33583af08d82Slm66018 		if (rv) {
33593af08d82Slm66018 			DMSG(vdc, 0,
33603af08d82Slm66018 			    "[%d] Entry @ %d - depopulate failed ..\n",
33613af08d82Slm66018 			    vdc->instance, idx);
33621ae08745Sheppo 		}
33631ae08745Sheppo 
33643af08d82Slm66018 		/* if this is the last descriptor - break out of loop */
33653af08d82Slm66018 		if ((idx + 1) % vdc->dring_len == vdc->dring_curr_idx)
33663af08d82Slm66018 			break;
33673af08d82Slm66018 	}
33683af08d82Slm66018 
33693af08d82Slm66018 	mutex_exit(&vdc->lock);
33703af08d82Slm66018 	DMSG(vdc, 0, "End idx=%d\n", idx);
33713af08d82Slm66018 
33723af08d82Slm66018 	return (rv);
33731ae08745Sheppo }
33741ae08745Sheppo 
33751ae08745Sheppo 
33760a55fbb7Slm66018 /*
33770a55fbb7Slm66018  * Function:
33780a55fbb7Slm66018  *	vdc_depopulate_descriptor()
33790a55fbb7Slm66018  *
33800a55fbb7Slm66018  * Description:
33810a55fbb7Slm66018  *
33820a55fbb7Slm66018  * Arguments:
33830a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
33840a55fbb7Slm66018  *	idx	- Index of the Descriptor Ring entry being modified
33850a55fbb7Slm66018  *
33860a55fbb7Slm66018  * Return Code:
33870a55fbb7Slm66018  *	0	- Success
33880a55fbb7Slm66018  */
33891ae08745Sheppo static int
33901ae08745Sheppo vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx)
33911ae08745Sheppo {
33921ae08745Sheppo 	vd_dring_entry_t *dep = NULL;		/* Dring Entry Pointer */
33931ae08745Sheppo 	vdc_local_desc_t *ldep = NULL;		/* Local Dring Entry Pointer */
33941ae08745Sheppo 	int		status = ENXIO;
33958e6a2a04Slm66018 	int		rv = 0;
33961ae08745Sheppo 
33971ae08745Sheppo 	ASSERT(vdc != NULL);
3398e1ebb9ecSlm66018 	ASSERT(idx < vdc->dring_len);
33991ae08745Sheppo 	ldep = &vdc->local_dring[idx];
34001ae08745Sheppo 	ASSERT(ldep != NULL);
34013af08d82Slm66018 	ASSERT(MUTEX_HELD(&vdc->lock));
34023af08d82Slm66018 
3403366a92acSlm66018 	DTRACE_PROBE2(depopulate, int, vdc->instance, vdc_local_desc_t *, ldep);
34043af08d82Slm66018 	DMSG(vdc, 2, ": idx = %d\n", idx);
3405366a92acSlm66018 
34061ae08745Sheppo 	dep = ldep->dep;
34071ae08745Sheppo 	ASSERT(dep != NULL);
3408e1ebb9ecSlm66018 	ASSERT((dep->hdr.dstate == VIO_DESC_DONE) ||
3409e1ebb9ecSlm66018 	    (dep->payload.status == ECANCELED));
34101ae08745Sheppo 
3411e1ebb9ecSlm66018 	VDC_MARK_DRING_ENTRY_FREE(vdc, idx);
34123af08d82Slm66018 
34133af08d82Slm66018 	ldep->is_free = B_TRUE;
34141ae08745Sheppo 	status = dep->payload.status;
3415205eeb1aSlm66018 	DMSG(vdc, 2, ": is_free = %d : status = %d\n", ldep->is_free, status);
34161ae08745Sheppo 
3417eff7243fSlm66018 	/*
3418eff7243fSlm66018 	 * If no buffers were used to transfer information to the server when
3419eff7243fSlm66018 	 * populating the descriptor then no memory handles need to be unbound
3420eff7243fSlm66018 	 * and we can return now.
3421eff7243fSlm66018 	 */
3422eff7243fSlm66018 	if (ldep->nbytes == 0) {
3423eff7243fSlm66018 		cv_signal(&vdc->dring_free_cv);
34248e6a2a04Slm66018 		return (status);
3425eff7243fSlm66018 	}
34268e6a2a04Slm66018 
34271ae08745Sheppo 	/*
34281ae08745Sheppo 	 * If the upper layer passed in a misaligned address we copied the
34291ae08745Sheppo 	 * data into an aligned buffer before sending it to LDC - we now
34301ae08745Sheppo 	 * copy it back to the original buffer.
34311ae08745Sheppo 	 */
34321ae08745Sheppo 	if (ldep->align_addr) {
34331ae08745Sheppo 		ASSERT(ldep->addr != NULL);
34341ae08745Sheppo 
34353c96341aSnarayan 		if (dep->payload.nbytes > 0)
34363c96341aSnarayan 			bcopy(ldep->align_addr, ldep->addr,
34373c96341aSnarayan 			    dep->payload.nbytes);
34381ae08745Sheppo 		kmem_free(ldep->align_addr,
34393c96341aSnarayan 		    sizeof (caddr_t) * P2ROUNDUP(ldep->nbytes, 8));
34401ae08745Sheppo 		ldep->align_addr = NULL;
34411ae08745Sheppo 	}
34421ae08745Sheppo 
34438e6a2a04Slm66018 	rv = ldc_mem_unbind_handle(ldep->desc_mhdl);
34448e6a2a04Slm66018 	if (rv != 0) {
34453af08d82Slm66018 		DMSG(vdc, 0, "?[%d] unbind mhdl 0x%lx @ idx %d failed (%d)",
34468e6a2a04Slm66018 		    vdc->instance, ldep->desc_mhdl, idx, rv);
34478e6a2a04Slm66018 		/*
34488e6a2a04Slm66018 		 * The error returned by the vDisk server is more informative
34498e6a2a04Slm66018 		 * and thus has a higher priority but if it isn't set we ensure
34508e6a2a04Slm66018 		 * that this function returns an error.
34518e6a2a04Slm66018 		 */
34528e6a2a04Slm66018 		if (status == 0)
34538e6a2a04Slm66018 			status = EINVAL;
34541ae08745Sheppo 	}
34551ae08745Sheppo 
34563af08d82Slm66018 	cv_signal(&vdc->membind_cv);
34573af08d82Slm66018 	cv_signal(&vdc->dring_free_cv);
34583af08d82Slm66018 
34591ae08745Sheppo 	return (status);
34601ae08745Sheppo }
34611ae08745Sheppo 
34620a55fbb7Slm66018 /*
34630a55fbb7Slm66018  * Function:
34640a55fbb7Slm66018  *	vdc_populate_mem_hdl()
34650a55fbb7Slm66018  *
34660a55fbb7Slm66018  * Description:
34670a55fbb7Slm66018  *
34680a55fbb7Slm66018  * Arguments:
34690a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
34700a55fbb7Slm66018  *	idx	- Index of the Descriptor Ring entry being modified
34710a55fbb7Slm66018  *	addr	- virtual address being mapped in
34720a55fbb7Slm66018  *	nybtes	- number of bytes in 'addr'
34730a55fbb7Slm66018  *	operation - the vDisk operation being performed (VD_OP_xxx)
34740a55fbb7Slm66018  *
34750a55fbb7Slm66018  * Return Code:
34760a55fbb7Slm66018  *	0	- Success
34770a55fbb7Slm66018  */
34781ae08745Sheppo static int
34793af08d82Slm66018 vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep)
34801ae08745Sheppo {
34811ae08745Sheppo 	vd_dring_entry_t	*dep = NULL;
34821ae08745Sheppo 	ldc_mem_handle_t	mhdl;
34831ae08745Sheppo 	caddr_t			vaddr;
34843af08d82Slm66018 	size_t			nbytes;
34854bac2208Snarayan 	uint8_t			perm = LDC_MEM_RW;
34864bac2208Snarayan 	uint8_t			maptype;
34871ae08745Sheppo 	int			rv = 0;
34881ae08745Sheppo 	int			i;
34891ae08745Sheppo 
34903af08d82Slm66018 	ASSERT(vdcp != NULL);
34911ae08745Sheppo 
34923af08d82Slm66018 	dep = ldep->dep;
34931ae08745Sheppo 	mhdl = ldep->desc_mhdl;
34941ae08745Sheppo 
34953af08d82Slm66018 	switch (ldep->dir) {
34963af08d82Slm66018 	case VIO_read_dir:
34971ae08745Sheppo 		perm = LDC_MEM_W;
34981ae08745Sheppo 		break;
34991ae08745Sheppo 
35003af08d82Slm66018 	case VIO_write_dir:
35011ae08745Sheppo 		perm = LDC_MEM_R;
35021ae08745Sheppo 		break;
35031ae08745Sheppo 
35043af08d82Slm66018 	case VIO_both_dir:
35051ae08745Sheppo 		perm = LDC_MEM_RW;
35061ae08745Sheppo 		break;
35071ae08745Sheppo 
35081ae08745Sheppo 	default:
35091ae08745Sheppo 		ASSERT(0);	/* catch bad programming in vdc */
35101ae08745Sheppo 	}
35111ae08745Sheppo 
35121ae08745Sheppo 	/*
35131ae08745Sheppo 	 * LDC expects any addresses passed in to be 8-byte aligned. We need
35141ae08745Sheppo 	 * to copy the contents of any misaligned buffers to a newly allocated
35151ae08745Sheppo 	 * buffer and bind it instead (and copy the the contents back to the
35161ae08745Sheppo 	 * original buffer passed in when depopulating the descriptor)
35171ae08745Sheppo 	 */
35183af08d82Slm66018 	vaddr = ldep->addr;
35193af08d82Slm66018 	nbytes = ldep->nbytes;
35203af08d82Slm66018 	if (((uint64_t)vaddr & 0x7) != 0) {
3521d10e4ef2Snarayan 		ASSERT(ldep->align_addr == NULL);
35221ae08745Sheppo 		ldep->align_addr =
35233af08d82Slm66018 		    kmem_alloc(sizeof (caddr_t) *
35243af08d82Slm66018 		    P2ROUNDUP(nbytes, 8), KM_SLEEP);
35253af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Misaligned address %p reallocating "
35263af08d82Slm66018 		    "(buf=%p nb=%ld op=%d)\n",
35273af08d82Slm66018 		    vdcp->instance, (void *)vaddr, (void *)ldep->align_addr,
35283af08d82Slm66018 		    nbytes, ldep->operation);
35293af08d82Slm66018 		if (perm != LDC_MEM_W)
35303af08d82Slm66018 			bcopy(vaddr, ldep->align_addr, nbytes);
35311ae08745Sheppo 		vaddr = ldep->align_addr;
35321ae08745Sheppo 	}
35331ae08745Sheppo 
35344bac2208Snarayan 	maptype = LDC_IO_MAP|LDC_SHADOW_MAP|LDC_DIRECT_MAP;
35351ae08745Sheppo 	rv = ldc_mem_bind_handle(mhdl, vaddr, P2ROUNDUP(nbytes, 8),
353687a7269eSachartre 	    maptype, perm, &dep->payload.cookie[0], &dep->payload.ncookies);
35373af08d82Slm66018 	DMSG(vdcp, 2, "[%d] bound mem handle; ncookies=%d\n",
35383af08d82Slm66018 	    vdcp->instance, dep->payload.ncookies);
35391ae08745Sheppo 	if (rv != 0) {
35403af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Failed to bind LDC memory handle "
35413af08d82Slm66018 		    "(mhdl=%p, buf=%p, err=%d)\n",
35423af08d82Slm66018 		    vdcp->instance, (void *)mhdl, (void *)vaddr, rv);
35431ae08745Sheppo 		if (ldep->align_addr) {
35441ae08745Sheppo 			kmem_free(ldep->align_addr,
3545d10e4ef2Snarayan 			    sizeof (caddr_t) * P2ROUNDUP(nbytes, 8));
35461ae08745Sheppo 			ldep->align_addr = NULL;
35471ae08745Sheppo 		}
35481ae08745Sheppo 		return (EAGAIN);
35491ae08745Sheppo 	}
35501ae08745Sheppo 
35511ae08745Sheppo 	/*
35521ae08745Sheppo 	 * Get the other cookies (if any).
35531ae08745Sheppo 	 */
35541ae08745Sheppo 	for (i = 1; i < dep->payload.ncookies; i++) {
35551ae08745Sheppo 		rv = ldc_mem_nextcookie(mhdl, &dep->payload.cookie[i]);
35561ae08745Sheppo 		if (rv != 0) {
35571ae08745Sheppo 			(void) ldc_mem_unbind_handle(mhdl);
35583af08d82Slm66018 			DMSG(vdcp, 0, "?[%d] Failed to get next cookie "
3559e1ebb9ecSlm66018 			    "(mhdl=%lx cnum=%d), err=%d",
35603af08d82Slm66018 			    vdcp->instance, mhdl, i, rv);
35611ae08745Sheppo 			if (ldep->align_addr) {
35621ae08745Sheppo 				kmem_free(ldep->align_addr,
35633c96341aSnarayan 				    sizeof (caddr_t) * ldep->nbytes);
35641ae08745Sheppo 				ldep->align_addr = NULL;
35651ae08745Sheppo 			}
35661ae08745Sheppo 			return (EAGAIN);
35671ae08745Sheppo 		}
35681ae08745Sheppo 	}
35691ae08745Sheppo 
35701ae08745Sheppo 	return (rv);
35711ae08745Sheppo }
35721ae08745Sheppo 
35731ae08745Sheppo /*
35741ae08745Sheppo  * Interrupt handlers for messages from LDC
35751ae08745Sheppo  */
35761ae08745Sheppo 
35770a55fbb7Slm66018 /*
35780a55fbb7Slm66018  * Function:
35790a55fbb7Slm66018  *	vdc_handle_cb()
35800a55fbb7Slm66018  *
35810a55fbb7Slm66018  * Description:
35820a55fbb7Slm66018  *
35830a55fbb7Slm66018  * Arguments:
35840a55fbb7Slm66018  *	event	- Type of event (LDC_EVT_xxx) that triggered the callback
35850a55fbb7Slm66018  *	arg	- soft state pointer for this instance of the device driver.
35860a55fbb7Slm66018  *
35870a55fbb7Slm66018  * Return Code:
35880a55fbb7Slm66018  *	0	- Success
35890a55fbb7Slm66018  */
35901ae08745Sheppo static uint_t
35911ae08745Sheppo vdc_handle_cb(uint64_t event, caddr_t arg)
35921ae08745Sheppo {
35931ae08745Sheppo 	ldc_status_t	ldc_state;
35941ae08745Sheppo 	int		rv = 0;
3595*8cd10891Snarayan 	vdc_server_t	*srvr = (vdc_server_t *)(void *)arg;
3596*8cd10891Snarayan 	vdc_t		*vdc = srvr->vdcp;
35971ae08745Sheppo 
35981ae08745Sheppo 	ASSERT(vdc != NULL);
35991ae08745Sheppo 
36003af08d82Slm66018 	DMSG(vdc, 1, "evt=%lx seqID=%ld\n", event, vdc->seq_num);
36011ae08745Sheppo 
3602*8cd10891Snarayan 	/* If callback is not for the current server, ignore it */
3603*8cd10891Snarayan 	mutex_enter(&vdc->lock);
3604*8cd10891Snarayan 
3605*8cd10891Snarayan 	if (vdc->curr_server != srvr) {
3606*8cd10891Snarayan 		DMSG(vdc, 0, "[%d] Ignoring event 0x%lx for port@%ld\n",
3607*8cd10891Snarayan 		    vdc->instance, event, srvr->id);
3608*8cd10891Snarayan 		mutex_exit(&vdc->lock);
3609*8cd10891Snarayan 		return (LDC_SUCCESS);
3610*8cd10891Snarayan 	}
3611*8cd10891Snarayan 
36121ae08745Sheppo 	/*
36131ae08745Sheppo 	 * Depending on the type of event that triggered this callback,
36143af08d82Slm66018 	 * we modify the handshake state or read the data.
36151ae08745Sheppo 	 *
36161ae08745Sheppo 	 * NOTE: not done as a switch() as event could be triggered by
36171ae08745Sheppo 	 * a state change and a read request. Also the ordering	of the
36181ae08745Sheppo 	 * check for the event types is deliberate.
36191ae08745Sheppo 	 */
36201ae08745Sheppo 	if (event & LDC_EVT_UP) {
36213af08d82Slm66018 		DMSG(vdc, 0, "[%d] Received LDC_EVT_UP\n", vdc->instance);
36223af08d82Slm66018 
36231ae08745Sheppo 		/* get LDC state */
3624*8cd10891Snarayan 		rv = ldc_status(srvr->ldc_handle, &ldc_state);
36251ae08745Sheppo 		if (rv != 0) {
36263af08d82Slm66018 			DMSG(vdc, 0, "[%d] Couldn't get LDC status %d",
36271ae08745Sheppo 			    vdc->instance, rv);
3628*8cd10891Snarayan 			mutex_exit(&vdc->lock);
36291ae08745Sheppo 			return (LDC_SUCCESS);
36301ae08745Sheppo 		}
3631*8cd10891Snarayan 		if (srvr->ldc_state != LDC_UP &&
3632*8cd10891Snarayan 		    ldc_state == LDC_UP) {
36331ae08745Sheppo 			/*
36343af08d82Slm66018 			 * Reset the transaction sequence numbers when
36353af08d82Slm66018 			 * LDC comes up. We then kick off the handshake
36363af08d82Slm66018 			 * negotiation with the vDisk server.
36371ae08745Sheppo 			 */
36380a55fbb7Slm66018 			vdc->seq_num = 1;
36391ae08745Sheppo 			vdc->seq_num_reply = 0;
3640*8cd10891Snarayan 			srvr->ldc_state = ldc_state;
36413af08d82Slm66018 			cv_signal(&vdc->initwait_cv);
36423af08d82Slm66018 		}
36431ae08745Sheppo 	}
36441ae08745Sheppo 
36451ae08745Sheppo 	if (event & LDC_EVT_READ) {
364617cadca8Slm66018 		DMSG(vdc, 1, "[%d] Received LDC_EVT_READ\n", vdc->instance);
36473af08d82Slm66018 		mutex_enter(&vdc->read_lock);
36483af08d82Slm66018 		cv_signal(&vdc->read_cv);
36493af08d82Slm66018 		vdc->read_state = VDC_READ_PENDING;
36503af08d82Slm66018 		mutex_exit(&vdc->read_lock);
3651*8cd10891Snarayan 		mutex_exit(&vdc->lock);
36521ae08745Sheppo 
36531ae08745Sheppo 		/* that's all we have to do - no need to handle DOWN/RESET */
36541ae08745Sheppo 		return (LDC_SUCCESS);
36551ae08745Sheppo 	}
36561ae08745Sheppo 
36573af08d82Slm66018 	if (event & (LDC_EVT_RESET|LDC_EVT_DOWN)) {
36580a55fbb7Slm66018 
36593af08d82Slm66018 		DMSG(vdc, 0, "[%d] Received LDC RESET event\n", vdc->instance);
36603af08d82Slm66018 
36613af08d82Slm66018 		/*
36623af08d82Slm66018 		 * Need to wake up any readers so they will
36633af08d82Slm66018 		 * detect that a reset has occurred.
36643af08d82Slm66018 		 */
36653af08d82Slm66018 		mutex_enter(&vdc->read_lock);
36663af08d82Slm66018 		if ((vdc->read_state == VDC_READ_WAITING) ||
36673af08d82Slm66018 		    (vdc->read_state == VDC_READ_RESET))
36683af08d82Slm66018 			cv_signal(&vdc->read_cv);
36693af08d82Slm66018 		vdc->read_state = VDC_READ_RESET;
36703af08d82Slm66018 		mutex_exit(&vdc->read_lock);
36710a55fbb7Slm66018 
36723af08d82Slm66018 		/* wake up any threads waiting for connection to come up */
36733af08d82Slm66018 		if (vdc->state == VDC_STATE_INIT_WAITING) {
36743af08d82Slm66018 			vdc->state = VDC_STATE_RESETTING;
36753af08d82Slm66018 			cv_signal(&vdc->initwait_cv);
36761ae08745Sheppo 		}
36771ae08745Sheppo 
36781ae08745Sheppo 	}
36791ae08745Sheppo 
3680*8cd10891Snarayan 	mutex_exit(&vdc->lock);
3681*8cd10891Snarayan 
36821ae08745Sheppo 	if (event & ~(LDC_EVT_UP | LDC_EVT_RESET | LDC_EVT_DOWN | LDC_EVT_READ))
36833af08d82Slm66018 		DMSG(vdc, 0, "![%d] Unexpected LDC event (%lx) received",
36841ae08745Sheppo 		    vdc->instance, event);
36851ae08745Sheppo 
36861ae08745Sheppo 	return (LDC_SUCCESS);
36871ae08745Sheppo }
36881ae08745Sheppo 
36893af08d82Slm66018 /*
36903af08d82Slm66018  * Function:
36913af08d82Slm66018  *	vdc_wait_for_response()
36923af08d82Slm66018  *
36933af08d82Slm66018  * Description:
36943af08d82Slm66018  *	Block waiting for a response from the server. If there is
36953af08d82Slm66018  *	no data the thread block on the read_cv that is signalled
36963af08d82Slm66018  *	by the callback when an EVT_READ occurs.
36973af08d82Slm66018  *
36983af08d82Slm66018  * Arguments:
36993af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
37003af08d82Slm66018  *
37013af08d82Slm66018  * Return Code:
37023af08d82Slm66018  *	0	- Success
37033af08d82Slm66018  */
37043af08d82Slm66018 static int
37053af08d82Slm66018 vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp)
37063af08d82Slm66018 {
37073af08d82Slm66018 	size_t		nbytes = sizeof (*msgp);
37083af08d82Slm66018 	int		status;
37093af08d82Slm66018 
37103af08d82Slm66018 	ASSERT(vdcp != NULL);
37113af08d82Slm66018 
37123af08d82Slm66018 	DMSG(vdcp, 1, "[%d] Entered\n", vdcp->instance);
37133af08d82Slm66018 
37143af08d82Slm66018 	status = vdc_recv(vdcp, msgp, &nbytes);
37153af08d82Slm66018 	DMSG(vdcp, 3, "vdc_read() done.. status=0x%x size=0x%x\n",
37163af08d82Slm66018 	    status, (int)nbytes);
37173af08d82Slm66018 	if (status) {
37183af08d82Slm66018 		DMSG(vdcp, 0, "?[%d] Error %d reading LDC msg\n",
37193af08d82Slm66018 		    vdcp->instance, status);
37203af08d82Slm66018 		return (status);
37213af08d82Slm66018 	}
37223af08d82Slm66018 
37233af08d82Slm66018 	if (nbytes < sizeof (vio_msg_tag_t)) {
37243af08d82Slm66018 		DMSG(vdcp, 0, "?[%d] Expect %lu bytes; recv'd %lu\n",
37253af08d82Slm66018 		    vdcp->instance, sizeof (vio_msg_tag_t), nbytes);
37263af08d82Slm66018 		return (ENOMSG);
37273af08d82Slm66018 	}
37283af08d82Slm66018 
37293af08d82Slm66018 	DMSG(vdcp, 2, "[%d] (%x/%x/%x)\n", vdcp->instance,
37303af08d82Slm66018 	    msgp->tag.vio_msgtype,
37313af08d82Slm66018 	    msgp->tag.vio_subtype,
37323af08d82Slm66018 	    msgp->tag.vio_subtype_env);
37333af08d82Slm66018 
37343af08d82Slm66018 	/*
37353af08d82Slm66018 	 * Verify the Session ID of the message
37363af08d82Slm66018 	 *
37373af08d82Slm66018 	 * Every message after the Version has been negotiated should
37383af08d82Slm66018 	 * have the correct session ID set.
37393af08d82Slm66018 	 */
37403af08d82Slm66018 	if ((msgp->tag.vio_sid != vdcp->session_id) &&
37413af08d82Slm66018 	    (msgp->tag.vio_subtype_env != VIO_VER_INFO)) {
37423af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Invalid SID: received 0x%x, "
37433af08d82Slm66018 		    "expected 0x%lx [seq num %lx @ %d]",
37443af08d82Slm66018 		    vdcp->instance, msgp->tag.vio_sid,
37453af08d82Slm66018 		    vdcp->session_id,
37463af08d82Slm66018 		    ((vio_dring_msg_t *)msgp)->seq_num,
37473af08d82Slm66018 		    ((vio_dring_msg_t *)msgp)->start_idx);
37483af08d82Slm66018 		return (ENOMSG);
37493af08d82Slm66018 	}
37503af08d82Slm66018 	return (0);
37513af08d82Slm66018 }
37523af08d82Slm66018 
37533af08d82Slm66018 
37543af08d82Slm66018 /*
37553af08d82Slm66018  * Function:
37563af08d82Slm66018  *	vdc_resubmit_backup_dring()
37573af08d82Slm66018  *
37583af08d82Slm66018  * Description:
37593af08d82Slm66018  *	Resubmit each descriptor in the backed up dring to
37603af08d82Slm66018  * 	vDisk server. The Dring was backed up during connection
37613af08d82Slm66018  *	reset.
37623af08d82Slm66018  *
37633af08d82Slm66018  * Arguments:
37643af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
37653af08d82Slm66018  *
37663af08d82Slm66018  * Return Code:
37673af08d82Slm66018  *	0	- Success
37683af08d82Slm66018  */
37693af08d82Slm66018 static int
37703af08d82Slm66018 vdc_resubmit_backup_dring(vdc_t *vdcp)
37713af08d82Slm66018 {
377290e2f9dcSlm66018 	int		processed = 0;
37733af08d82Slm66018 	int		count;
37743af08d82Slm66018 	int		b_idx;
377590e2f9dcSlm66018 	int		rv = 0;
37763af08d82Slm66018 	int		dring_size;
377790e2f9dcSlm66018 	int		op;
37783af08d82Slm66018 	vio_msg_t	vio_msg;
37793af08d82Slm66018 	vdc_local_desc_t	*curr_ldep;
37803af08d82Slm66018 
37813af08d82Slm66018 	ASSERT(MUTEX_NOT_HELD(&vdcp->lock));
37823af08d82Slm66018 	ASSERT(vdcp->state == VDC_STATE_HANDLE_PENDING);
37833af08d82Slm66018 
3784655fd6a9Sachartre 	if (vdcp->local_dring_backup == NULL) {
3785655fd6a9Sachartre 		/* the pending requests have already been processed */
3786655fd6a9Sachartre 		return (0);
3787655fd6a9Sachartre 	}
3788655fd6a9Sachartre 
37893af08d82Slm66018 	DMSG(vdcp, 1, "restoring pending dring entries (len=%d, tail=%d)\n",
37903af08d82Slm66018 	    vdcp->local_dring_backup_len, vdcp->local_dring_backup_tail);
37913af08d82Slm66018 
37923af08d82Slm66018 	/*
37933af08d82Slm66018 	 * Walk the backup copy of the local descriptor ring and
37943af08d82Slm66018 	 * resubmit all the outstanding transactions.
37953af08d82Slm66018 	 */
37963af08d82Slm66018 	b_idx = vdcp->local_dring_backup_tail;
37973af08d82Slm66018 	for (count = 0; count < vdcp->local_dring_backup_len; count++) {
37983af08d82Slm66018 
37993af08d82Slm66018 		curr_ldep = &(vdcp->local_dring_backup[b_idx]);
38003af08d82Slm66018 
3801eff7243fSlm66018 		/* only resubmit outstanding transactions */
38023af08d82Slm66018 		if (!curr_ldep->is_free) {
380390e2f9dcSlm66018 			/*
380490e2f9dcSlm66018 			 * If we are retrying a block read/write operation we
380590e2f9dcSlm66018 			 * need to update the I/O statistics to indicate that
380690e2f9dcSlm66018 			 * the request is being put back on the waitq to be
380790e2f9dcSlm66018 			 * serviced (it will have been taken off after the
380890e2f9dcSlm66018 			 * error was reported).
380990e2f9dcSlm66018 			 */
381090e2f9dcSlm66018 			mutex_enter(&vdcp->lock);
381190e2f9dcSlm66018 			op = curr_ldep->operation;
381290e2f9dcSlm66018 			if ((op == VD_OP_BREAD) || (op == VD_OP_BWRITE)) {
381390e2f9dcSlm66018 				DTRACE_IO1(start, buf_t *, curr_ldep->cb_arg);
381490e2f9dcSlm66018 				VD_KSTAT_WAITQ_ENTER(vdcp);
381590e2f9dcSlm66018 			}
38163af08d82Slm66018 
38173af08d82Slm66018 			DMSG(vdcp, 1, "resubmitting entry idx=%x\n", b_idx);
381890e2f9dcSlm66018 			rv = vdc_populate_descriptor(vdcp, op,
38193af08d82Slm66018 			    curr_ldep->addr, curr_ldep->nbytes,
38203af08d82Slm66018 			    curr_ldep->slice, curr_ldep->offset,
38213af08d82Slm66018 			    curr_ldep->cb_type, curr_ldep->cb_arg,
38223af08d82Slm66018 			    curr_ldep->dir);
382390e2f9dcSlm66018 
38243af08d82Slm66018 			if (rv) {
382590e2f9dcSlm66018 				if (op == VD_OP_BREAD || op == VD_OP_BWRITE) {
382690e2f9dcSlm66018 					VD_UPDATE_ERR_STATS(vdcp, vd_transerrs);
382790e2f9dcSlm66018 					VD_KSTAT_WAITQ_EXIT(vdcp);
382890e2f9dcSlm66018 					DTRACE_IO1(done, buf_t *,
382990e2f9dcSlm66018 					    curr_ldep->cb_arg);
383090e2f9dcSlm66018 				}
38313af08d82Slm66018 				DMSG(vdcp, 1, "[%d] cannot resubmit entry %d\n",
38323af08d82Slm66018 				    vdcp->instance, b_idx);
383390e2f9dcSlm66018 				mutex_exit(&vdcp->lock);
383490e2f9dcSlm66018 				goto done;
38353af08d82Slm66018 			}
38363af08d82Slm66018 
383790e2f9dcSlm66018 			/*
383890e2f9dcSlm66018 			 * If this is a block read/write we update the I/O
383990e2f9dcSlm66018 			 * statistics kstat to indicate that the request
384090e2f9dcSlm66018 			 * has been sent back to the vDisk server and should
384190e2f9dcSlm66018 			 * now be put on the run queue.
384290e2f9dcSlm66018 			 */
384390e2f9dcSlm66018 			if ((op == VD_OP_BREAD) || (op == VD_OP_BWRITE)) {
384490e2f9dcSlm66018 				DTRACE_PROBE1(send, buf_t *, curr_ldep->cb_arg);
384590e2f9dcSlm66018 				VD_KSTAT_WAITQ_TO_RUNQ(vdcp);
384690e2f9dcSlm66018 			}
384790e2f9dcSlm66018 			mutex_exit(&vdcp->lock);
384890e2f9dcSlm66018 
38493af08d82Slm66018 			/* Wait for the response message. */
38503af08d82Slm66018 			DMSG(vdcp, 1, "waiting for response to idx=%x\n",
38513af08d82Slm66018 			    b_idx);
385290e2f9dcSlm66018 			rv = vdc_wait_for_response(vdcp, &vio_msg);
385390e2f9dcSlm66018 			if (rv) {
385490e2f9dcSlm66018 				/*
385590e2f9dcSlm66018 				 * If this is a block read/write we update
385690e2f9dcSlm66018 				 * the I/O statistics kstat to take it
385790e2f9dcSlm66018 				 * off the run queue.
385890e2f9dcSlm66018 				 */
385990e2f9dcSlm66018 				mutex_enter(&vdcp->lock);
386090e2f9dcSlm66018 				if (op == VD_OP_BREAD || op == VD_OP_BWRITE) {
386190e2f9dcSlm66018 					VD_UPDATE_ERR_STATS(vdcp, vd_transerrs);
386290e2f9dcSlm66018 					VD_KSTAT_RUNQ_EXIT(vdcp);
386390e2f9dcSlm66018 					DTRACE_IO1(done, buf_t *,
386490e2f9dcSlm66018 					    curr_ldep->cb_arg);
386590e2f9dcSlm66018 				}
38663af08d82Slm66018 				DMSG(vdcp, 1, "[%d] wait_for_response "
38673af08d82Slm66018 				    "returned err=%d\n", vdcp->instance,
386890e2f9dcSlm66018 				    rv);
386990e2f9dcSlm66018 				mutex_exit(&vdcp->lock);
387090e2f9dcSlm66018 				goto done;
38713af08d82Slm66018 			}
38723af08d82Slm66018 
38733af08d82Slm66018 			DMSG(vdcp, 1, "processing msg for idx=%x\n", b_idx);
387490e2f9dcSlm66018 			rv = vdc_process_data_msg(vdcp, &vio_msg);
387590e2f9dcSlm66018 			if (rv) {
38763af08d82Slm66018 				DMSG(vdcp, 1, "[%d] process_data_msg "
38773af08d82Slm66018 				    "returned err=%d\n", vdcp->instance,
387890e2f9dcSlm66018 				    rv);
387990e2f9dcSlm66018 				goto done;
38803af08d82Slm66018 			}
388190e2f9dcSlm66018 			processed++;
38823af08d82Slm66018 		}
38833af08d82Slm66018 
38843af08d82Slm66018 		/* get the next element to submit */
38853af08d82Slm66018 		if (++b_idx >= vdcp->local_dring_backup_len)
38863af08d82Slm66018 			b_idx = 0;
38873af08d82Slm66018 	}
38883af08d82Slm66018 
38893af08d82Slm66018 	/* all done - now clear up pending dring copy */
38903af08d82Slm66018 	dring_size = vdcp->local_dring_backup_len *
38913af08d82Slm66018 	    sizeof (vdcp->local_dring_backup[0]);
38923af08d82Slm66018 
38933af08d82Slm66018 	(void) kmem_free(vdcp->local_dring_backup, dring_size);
38943af08d82Slm66018 
38953af08d82Slm66018 	vdcp->local_dring_backup = NULL;
38963af08d82Slm66018 
389790e2f9dcSlm66018 done:
389890e2f9dcSlm66018 	DTRACE_PROBE2(processed, int, processed, vdc_t *, vdcp);
389990e2f9dcSlm66018 
390090e2f9dcSlm66018 	return (rv);
39013af08d82Slm66018 }
39023af08d82Slm66018 
39033af08d82Slm66018 /*
39043af08d82Slm66018  * Function:
3905655fd6a9Sachartre  *	vdc_cancel_backup_dring
3906655fd6a9Sachartre  *
3907655fd6a9Sachartre  * Description:
3908655fd6a9Sachartre  *	Cancel each descriptor in the backed up dring to vDisk server.
3909655fd6a9Sachartre  *	The Dring was backed up during connection reset.
3910655fd6a9Sachartre  *
3911655fd6a9Sachartre  * Arguments:
3912655fd6a9Sachartre  *	vdcp	- soft state pointer for this instance of the device driver.
3913655fd6a9Sachartre  *
3914655fd6a9Sachartre  * Return Code:
3915655fd6a9Sachartre  *	None
3916655fd6a9Sachartre  */
3917655fd6a9Sachartre void
391890e2f9dcSlm66018 vdc_cancel_backup_dring(vdc_t *vdcp)
3919655fd6a9Sachartre {
3920655fd6a9Sachartre 	vdc_local_desc_t *ldep;
3921655fd6a9Sachartre 	struct buf 	*bufp;
3922655fd6a9Sachartre 	int		count;
3923655fd6a9Sachartre 	int		b_idx;
3924655fd6a9Sachartre 	int		dring_size;
392590e2f9dcSlm66018 	int		cancelled = 0;
3926655fd6a9Sachartre 
3927655fd6a9Sachartre 	ASSERT(MUTEX_HELD(&vdcp->lock));
3928655fd6a9Sachartre 	ASSERT(vdcp->state == VDC_STATE_INIT ||
3929655fd6a9Sachartre 	    vdcp->state == VDC_STATE_INIT_WAITING ||
3930655fd6a9Sachartre 	    vdcp->state == VDC_STATE_NEGOTIATE ||
3931655fd6a9Sachartre 	    vdcp->state == VDC_STATE_RESETTING);
3932655fd6a9Sachartre 
3933655fd6a9Sachartre 	if (vdcp->local_dring_backup == NULL) {
3934655fd6a9Sachartre 		/* the pending requests have already been processed */
3935655fd6a9Sachartre 		return;
3936655fd6a9Sachartre 	}
3937655fd6a9Sachartre 
3938655fd6a9Sachartre 	DMSG(vdcp, 1, "cancelling pending dring entries (len=%d, tail=%d)\n",
3939655fd6a9Sachartre 	    vdcp->local_dring_backup_len, vdcp->local_dring_backup_tail);
3940655fd6a9Sachartre 
3941655fd6a9Sachartre 	/*
3942655fd6a9Sachartre 	 * Walk the backup copy of the local descriptor ring and
3943655fd6a9Sachartre 	 * cancel all the outstanding transactions.
3944655fd6a9Sachartre 	 */
3945655fd6a9Sachartre 	b_idx = vdcp->local_dring_backup_tail;
3946655fd6a9Sachartre 	for (count = 0; count < vdcp->local_dring_backup_len; count++) {
3947655fd6a9Sachartre 
3948655fd6a9Sachartre 		ldep = &(vdcp->local_dring_backup[b_idx]);
3949655fd6a9Sachartre 
3950655fd6a9Sachartre 		/* only cancel outstanding transactions */
3951655fd6a9Sachartre 		if (!ldep->is_free) {
3952655fd6a9Sachartre 
3953655fd6a9Sachartre 			DMSG(vdcp, 1, "cancelling entry idx=%x\n", b_idx);
395490e2f9dcSlm66018 			cancelled++;
3955655fd6a9Sachartre 
3956655fd6a9Sachartre 			/*
3957655fd6a9Sachartre 			 * All requests have already been cleared from the
3958655fd6a9Sachartre 			 * local descriptor ring and the LDC channel has been
3959655fd6a9Sachartre 			 * reset so we will never get any reply for these
3960655fd6a9Sachartre 			 * requests. Now we just have to notify threads waiting
3961655fd6a9Sachartre 			 * for replies that the request has failed.
3962655fd6a9Sachartre 			 */
3963655fd6a9Sachartre 			switch (ldep->cb_type) {
3964655fd6a9Sachartre 			case CB_SYNC:
3965655fd6a9Sachartre 				ASSERT(vdcp->sync_op_pending);
3966655fd6a9Sachartre 				vdcp->sync_op_status = EIO;
3967655fd6a9Sachartre 				vdcp->sync_op_pending = B_FALSE;
3968655fd6a9Sachartre 				cv_signal(&vdcp->sync_pending_cv);
3969655fd6a9Sachartre 				break;
3970655fd6a9Sachartre 
3971655fd6a9Sachartre 			case CB_STRATEGY:
3972655fd6a9Sachartre 				bufp = ldep->cb_arg;
3973655fd6a9Sachartre 				ASSERT(bufp != NULL);
3974655fd6a9Sachartre 				bufp->b_resid = bufp->b_bcount;
3975366a92acSlm66018 				VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
397690e2f9dcSlm66018 				VD_KSTAT_RUNQ_EXIT(vdcp);
3977366a92acSlm66018 				DTRACE_IO1(done, buf_t *, bufp);
3978655fd6a9Sachartre 				bioerror(bufp, EIO);
3979655fd6a9Sachartre 				biodone(bufp);
3980655fd6a9Sachartre 				break;
3981655fd6a9Sachartre 
3982655fd6a9Sachartre 			default:
3983655fd6a9Sachartre 				ASSERT(0);
3984655fd6a9Sachartre 			}
3985655fd6a9Sachartre 
3986655fd6a9Sachartre 		}
3987655fd6a9Sachartre 
3988655fd6a9Sachartre 		/* get the next element to cancel */
3989655fd6a9Sachartre 		if (++b_idx >= vdcp->local_dring_backup_len)
3990655fd6a9Sachartre 			b_idx = 0;
3991655fd6a9Sachartre 	}
3992655fd6a9Sachartre 
3993655fd6a9Sachartre 	/* all done - now clear up pending dring copy */
3994655fd6a9Sachartre 	dring_size = vdcp->local_dring_backup_len *
3995655fd6a9Sachartre 	    sizeof (vdcp->local_dring_backup[0]);
3996655fd6a9Sachartre 
3997655fd6a9Sachartre 	(void) kmem_free(vdcp->local_dring_backup, dring_size);
3998655fd6a9Sachartre 
3999655fd6a9Sachartre 	vdcp->local_dring_backup = NULL;
4000655fd6a9Sachartre 
400190e2f9dcSlm66018 	DTRACE_PROBE2(cancelled, int, cancelled, vdc_t *, vdcp);
4002655fd6a9Sachartre }
4003655fd6a9Sachartre 
4004655fd6a9Sachartre /*
4005655fd6a9Sachartre  * Function:
4006655fd6a9Sachartre  *	vdc_connection_timeout
4007655fd6a9Sachartre  *
4008655fd6a9Sachartre  * Description:
4009655fd6a9Sachartre  *	This function is invoked if the timeout set to establish the connection
4010655fd6a9Sachartre  *	with vds expires. This will happen if we spend too much time in the
4011655fd6a9Sachartre  *	VDC_STATE_INIT_WAITING or VDC_STATE_NEGOTIATE states. Then we will
4012655fd6a9Sachartre  *	cancel any pending request and mark them as failed.
4013655fd6a9Sachartre  *
4014655fd6a9Sachartre  *	If the timeout does not expire, it will be cancelled when we reach the
4015655fd6a9Sachartre  *	VDC_STATE_HANDLE_PENDING or VDC_STATE_RESETTING state. This function can
4016655fd6a9Sachartre  *	be invoked while we are in the VDC_STATE_HANDLE_PENDING or
4017655fd6a9Sachartre  *	VDC_STATE_RESETTING state in which case we do nothing because the
4018655fd6a9Sachartre  *	timeout is being cancelled.
4019655fd6a9Sachartre  *
4020655fd6a9Sachartre  * Arguments:
4021655fd6a9Sachartre  *	arg	- argument of the timeout function actually a soft state
4022655fd6a9Sachartre  *		  pointer for the instance of the device driver.
4023655fd6a9Sachartre  *
4024655fd6a9Sachartre  * Return Code:
4025655fd6a9Sachartre  *	None
4026655fd6a9Sachartre  */
4027655fd6a9Sachartre void
4028655fd6a9Sachartre vdc_connection_timeout(void *arg)
4029655fd6a9Sachartre {
4030655fd6a9Sachartre 	vdc_t 		*vdcp = (vdc_t *)arg;
4031655fd6a9Sachartre 
4032655fd6a9Sachartre 	mutex_enter(&vdcp->lock);
4033655fd6a9Sachartre 
4034655fd6a9Sachartre 	if (vdcp->state == VDC_STATE_HANDLE_PENDING ||
4035655fd6a9Sachartre 	    vdcp->state == VDC_STATE_DETACH) {
4036655fd6a9Sachartre 		/*
4037655fd6a9Sachartre 		 * The connection has just been re-established or
4038655fd6a9Sachartre 		 * we are detaching.
4039655fd6a9Sachartre 		 */
4040655fd6a9Sachartre 		vdcp->ctimeout_reached = B_FALSE;
4041655fd6a9Sachartre 		mutex_exit(&vdcp->lock);
4042655fd6a9Sachartre 		return;
4043655fd6a9Sachartre 	}
4044655fd6a9Sachartre 
4045655fd6a9Sachartre 	vdcp->ctimeout_reached = B_TRUE;
4046655fd6a9Sachartre 
4047655fd6a9Sachartre 	/* notify requests waiting for sending */
4048655fd6a9Sachartre 	cv_broadcast(&vdcp->running_cv);
4049655fd6a9Sachartre 
4050655fd6a9Sachartre 	/* cancel requests waiting for a result */
405190e2f9dcSlm66018 	vdc_cancel_backup_dring(vdcp);
4052655fd6a9Sachartre 
4053655fd6a9Sachartre 	mutex_exit(&vdcp->lock);
4054655fd6a9Sachartre 
4055655fd6a9Sachartre 	cmn_err(CE_NOTE, "[%d] connection to service domain timeout",
4056655fd6a9Sachartre 	    vdcp->instance);
4057655fd6a9Sachartre }
4058655fd6a9Sachartre 
4059655fd6a9Sachartre /*
4060655fd6a9Sachartre  * Function:
40613af08d82Slm66018  *	vdc_backup_local_dring()
40623af08d82Slm66018  *
40633af08d82Slm66018  * Description:
40643af08d82Slm66018  *	Backup the current dring in the event of a reset. The Dring
40653af08d82Slm66018  *	transactions will be resubmitted to the server when the
40663af08d82Slm66018  *	connection is restored.
40673af08d82Slm66018  *
40683af08d82Slm66018  * Arguments:
40693af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
40703af08d82Slm66018  *
40713af08d82Slm66018  * Return Code:
40723af08d82Slm66018  *	NONE
40733af08d82Slm66018  */
40743af08d82Slm66018 static void
40753af08d82Slm66018 vdc_backup_local_dring(vdc_t *vdcp)
40763af08d82Slm66018 {
40773af08d82Slm66018 	int dring_size;
40783af08d82Slm66018 
4079655fd6a9Sachartre 	ASSERT(MUTEX_HELD(&vdcp->lock));
40803af08d82Slm66018 	ASSERT(vdcp->state == VDC_STATE_RESETTING);
40813af08d82Slm66018 
40823af08d82Slm66018 	/*
40833af08d82Slm66018 	 * If the backup dring is stil around, it means
40843af08d82Slm66018 	 * that the last restore did not complete. However,
40853af08d82Slm66018 	 * since we never got back into the running state,
40863af08d82Slm66018 	 * the backup copy we have is still valid.
40873af08d82Slm66018 	 */
40883af08d82Slm66018 	if (vdcp->local_dring_backup != NULL) {
40893af08d82Slm66018 		DMSG(vdcp, 1, "reusing local descriptor ring backup "
40903af08d82Slm66018 		    "(len=%d, tail=%d)\n", vdcp->local_dring_backup_len,
40913af08d82Slm66018 		    vdcp->local_dring_backup_tail);
40923af08d82Slm66018 		return;
40933af08d82Slm66018 	}
40943af08d82Slm66018 
4095655fd6a9Sachartre 	/*
4096655fd6a9Sachartre 	 * The backup dring can be NULL and the local dring may not be
4097655fd6a9Sachartre 	 * initialized. This can happen if we had a reset while establishing
4098655fd6a9Sachartre 	 * a new connection but after the connection has timed out. In that
4099655fd6a9Sachartre 	 * case the backup dring is NULL because the requests have been
4100655fd6a9Sachartre 	 * cancelled and the request occured before the local dring is
4101655fd6a9Sachartre 	 * initialized.
4102655fd6a9Sachartre 	 */
4103655fd6a9Sachartre 	if (!(vdcp->initialized & VDC_DRING_LOCAL))
4104655fd6a9Sachartre 		return;
4105655fd6a9Sachartre 
41063af08d82Slm66018 	DMSG(vdcp, 1, "backing up the local descriptor ring (len=%d, "
41073af08d82Slm66018 	    "tail=%d)\n", vdcp->dring_len, vdcp->dring_curr_idx);
41083af08d82Slm66018 
41093af08d82Slm66018 	dring_size = vdcp->dring_len * sizeof (vdcp->local_dring[0]);
41103af08d82Slm66018 
41113af08d82Slm66018 	vdcp->local_dring_backup = kmem_alloc(dring_size, KM_SLEEP);
41123af08d82Slm66018 	bcopy(vdcp->local_dring, vdcp->local_dring_backup, dring_size);
41133af08d82Slm66018 
41143af08d82Slm66018 	vdcp->local_dring_backup_tail = vdcp->dring_curr_idx;
41153af08d82Slm66018 	vdcp->local_dring_backup_len = vdcp->dring_len;
41163af08d82Slm66018 }
41173af08d82Slm66018 
4118*8cd10891Snarayan static void
4119*8cd10891Snarayan vdc_switch_server(vdc_t *vdcp)
4120*8cd10891Snarayan {
4121*8cd10891Snarayan 	int		rv;
4122*8cd10891Snarayan 	vdc_server_t 	*curr_server, *new_server;
4123*8cd10891Snarayan 
4124*8cd10891Snarayan 	ASSERT(MUTEX_HELD(&vdcp->lock));
4125*8cd10891Snarayan 
4126*8cd10891Snarayan 	/* if there is only one server return back */
4127*8cd10891Snarayan 	if (vdcp->num_servers == 1) {
4128*8cd10891Snarayan 		return;
4129*8cd10891Snarayan 	}
4130*8cd10891Snarayan 
4131*8cd10891Snarayan 	/* Get current and next server */
4132*8cd10891Snarayan 	curr_server = vdcp->curr_server;
4133*8cd10891Snarayan 	new_server =
4134*8cd10891Snarayan 	    (curr_server->next) ? curr_server->next : vdcp->server_list;
4135*8cd10891Snarayan 	ASSERT(curr_server != new_server);
4136*8cd10891Snarayan 
4137*8cd10891Snarayan 	/* bring current server's channel down */
4138*8cd10891Snarayan 	rv = ldc_down(curr_server->ldc_handle);
4139*8cd10891Snarayan 	if (rv) {
4140*8cd10891Snarayan 		DMSG(vdcp, 0, "[%d] Cannot bring channel down, port %ld\n",
4141*8cd10891Snarayan 		    vdcp->instance, curr_server->id);
4142*8cd10891Snarayan 		return;
4143*8cd10891Snarayan 	}
4144*8cd10891Snarayan 
4145*8cd10891Snarayan 	/* switch the server */
4146*8cd10891Snarayan 	vdcp->curr_server = new_server;
4147*8cd10891Snarayan 
4148*8cd10891Snarayan 	cmn_err(CE_NOTE, "Successfully failed over from VDS on port@%ld to "
4149*8cd10891Snarayan 	    "VDS on port@%ld.\n", curr_server->id, new_server->id);
4150*8cd10891Snarayan 	DMSG(vdcp, 0, "[%d] Switched to next vdisk server, port@%ld, ldc@%ld\n",
4151*8cd10891Snarayan 	    vdcp->instance, vdcp->curr_server->id, vdcp->curr_server->ldc_id);
4152*8cd10891Snarayan }
4153*8cd10891Snarayan 
41541ae08745Sheppo /* -------------------------------------------------------------------------- */
41551ae08745Sheppo 
41561ae08745Sheppo /*
41571ae08745Sheppo  * The following functions process the incoming messages from vds
41581ae08745Sheppo  */
41591ae08745Sheppo 
41600a55fbb7Slm66018 /*
41610a55fbb7Slm66018  * Function:
41620a55fbb7Slm66018  *      vdc_process_msg_thread()
41630a55fbb7Slm66018  *
41640a55fbb7Slm66018  * Description:
41650a55fbb7Slm66018  *
41663af08d82Slm66018  *	Main VDC message processing thread. Each vDisk instance
41673af08d82Slm66018  * 	consists of a copy of this thread. This thread triggers
41683af08d82Slm66018  * 	all the handshakes and data exchange with the server. It
41693af08d82Slm66018  * 	also handles all channel resets
41703af08d82Slm66018  *
41710a55fbb7Slm66018  * Arguments:
41720a55fbb7Slm66018  *      vdc     - soft state pointer for this instance of the device driver.
41730a55fbb7Slm66018  *
41740a55fbb7Slm66018  * Return Code:
41750a55fbb7Slm66018  *      None
41760a55fbb7Slm66018  */
41771ae08745Sheppo static void
41783af08d82Slm66018 vdc_process_msg_thread(vdc_t *vdcp)
41791ae08745Sheppo {
41801ae08745Sheppo 	int		status;
4181655fd6a9Sachartre 	int		ctimeout;
4182655fd6a9Sachartre 	timeout_id_t	tmid = 0;
4183*8cd10891Snarayan 	clock_t		ldcup_timeout = 0;
41841ae08745Sheppo 
41853af08d82Slm66018 	mutex_enter(&vdcp->lock);
41861ae08745Sheppo 
41871ae08745Sheppo 	for (;;) {
41881ae08745Sheppo 
41893af08d82Slm66018 #define	Q(_s)	(vdcp->state == _s) ? #_s :
41903af08d82Slm66018 		DMSG(vdcp, 3, "state = %d (%s)\n", vdcp->state,
41913af08d82Slm66018 		    Q(VDC_STATE_INIT)
41923af08d82Slm66018 		    Q(VDC_STATE_INIT_WAITING)
41933af08d82Slm66018 		    Q(VDC_STATE_NEGOTIATE)
41943af08d82Slm66018 		    Q(VDC_STATE_HANDLE_PENDING)
41953af08d82Slm66018 		    Q(VDC_STATE_RUNNING)
41963af08d82Slm66018 		    Q(VDC_STATE_RESETTING)
41973af08d82Slm66018 		    Q(VDC_STATE_DETACH)
41983af08d82Slm66018 		    "UNKNOWN");
41991ae08745Sheppo 
42003af08d82Slm66018 		switch (vdcp->state) {
42013af08d82Slm66018 		case VDC_STATE_INIT:
42023af08d82Slm66018 
4203655fd6a9Sachartre 			/*
4204655fd6a9Sachartre 			 * If requested, start a timeout to check if the
4205655fd6a9Sachartre 			 * connection with vds is established in the
4206655fd6a9Sachartre 			 * specified delay. If the timeout expires, we
4207655fd6a9Sachartre 			 * will cancel any pending request.
4208655fd6a9Sachartre 			 *
4209655fd6a9Sachartre 			 * If some reset have occurred while establishing
4210655fd6a9Sachartre 			 * the connection, we already have a timeout armed
4211655fd6a9Sachartre 			 * and in that case we don't need to arm a new one.
4212*8cd10891Snarayan 			 *
4213*8cd10891Snarayan 			 * The same rule applies when there are multiple vds'.
4214*8cd10891Snarayan 			 * If either a connection cannot be established or
4215*8cd10891Snarayan 			 * the handshake times out, the connection thread will
4216*8cd10891Snarayan 			 * try another server. The 'ctimeout' will report
4217*8cd10891Snarayan 			 * back an error after it expires irrespective of
4218*8cd10891Snarayan 			 * whether the vdisk is trying to connect to just
4219*8cd10891Snarayan 			 * one or multiple servers.
4220655fd6a9Sachartre 			 */
4221655fd6a9Sachartre 			ctimeout = (vdc_timeout != 0)?
4222*8cd10891Snarayan 			    vdc_timeout : vdcp->curr_server->ctimeout;
4223655fd6a9Sachartre 
4224655fd6a9Sachartre 			if (ctimeout != 0 && tmid == 0) {
4225655fd6a9Sachartre 				tmid = timeout(vdc_connection_timeout, vdcp,
4226*8cd10891Snarayan 				    ctimeout * drv_usectohz(MICROSEC));
4227655fd6a9Sachartre 			}
4228655fd6a9Sachartre 
4229*8cd10891Snarayan 			/* Check if we are re-initializing repeatedly */
4230*8cd10891Snarayan 			if (vdcp->hshake_cnt > vdc_hshake_retries &&
4231655fd6a9Sachartre 			    vdcp->lifecycle != VDC_LC_ONLINE) {
4232*8cd10891Snarayan 
4233*8cd10891Snarayan 				DMSG(vdcp, 0, "[%d] too many handshakes,cnt=%d",
4234*8cd10891Snarayan 				    vdcp->instance, vdcp->hshake_cnt);
42353c96341aSnarayan 				cmn_err(CE_NOTE, "[%d] disk access failed.\n",
42363c96341aSnarayan 				    vdcp->instance);
42373af08d82Slm66018 				vdcp->state = VDC_STATE_DETACH;
42383af08d82Slm66018 				break;
42393af08d82Slm66018 			}
42403af08d82Slm66018 
4241*8cd10891Snarayan 			/* Switch to STATE_DETACH if drv is detaching */
4242*8cd10891Snarayan 			if (vdcp->lifecycle == VDC_LC_DETACHING) {
4243*8cd10891Snarayan 				vdcp->state = VDC_STATE_DETACH;
4244*8cd10891Snarayan 				break;
4245*8cd10891Snarayan 			}
4246*8cd10891Snarayan 
4247*8cd10891Snarayan 			/* Switch server */
4248*8cd10891Snarayan 			if (vdcp->hshake_cnt > 0)
4249*8cd10891Snarayan 				vdc_switch_server(vdcp);
4250*8cd10891Snarayan 			vdcp->hshake_cnt++;
4251*8cd10891Snarayan 
42523af08d82Slm66018 			/* Bring up connection with vds via LDC */
42533af08d82Slm66018 			status = vdc_start_ldc_connection(vdcp);
4254*8cd10891Snarayan 			if (status != EINVAL) {
42553af08d82Slm66018 				vdcp->state = VDC_STATE_INIT_WAITING;
42563af08d82Slm66018 			}
42573af08d82Slm66018 			break;
42583af08d82Slm66018 
42593af08d82Slm66018 		case VDC_STATE_INIT_WAITING:
42603af08d82Slm66018 
4261*8cd10891Snarayan 			/* if channel is UP, start negotiation */
4262*8cd10891Snarayan 			if (vdcp->curr_server->ldc_state == LDC_UP) {
4263*8cd10891Snarayan 				vdcp->state = VDC_STATE_NEGOTIATE;
4264*8cd10891Snarayan 				break;
4265*8cd10891Snarayan 			}
4266*8cd10891Snarayan 
4267*8cd10891Snarayan 			/* check if only one server exists */
4268*8cd10891Snarayan 			if (vdcp->num_servers == 1) {
42693af08d82Slm66018 				cv_wait(&vdcp->initwait_cv, &vdcp->lock);
4270*8cd10891Snarayan 			} else {
4271*8cd10891Snarayan 				/*
4272*8cd10891Snarayan 				 * wait for LDC_UP, if it times out, switch
4273*8cd10891Snarayan 				 * to another server.
4274*8cd10891Snarayan 				 */
4275*8cd10891Snarayan 				ldcup_timeout = ddi_get_lbolt() +
4276*8cd10891Snarayan 				    (vdc_ldcup_timeout *
4277*8cd10891Snarayan 				    drv_usectohz(MICROSEC));
4278*8cd10891Snarayan 				status = cv_timedwait(&vdcp->initwait_cv,
4279*8cd10891Snarayan 				    &vdcp->lock, ldcup_timeout);
4280*8cd10891Snarayan 				if (status == -1 &&
4281*8cd10891Snarayan 				    vdcp->state == VDC_STATE_INIT_WAITING &&
4282*8cd10891Snarayan 				    vdcp->curr_server->ldc_state != LDC_UP) {
4283*8cd10891Snarayan 					/* timed out & still waiting */
4284*8cd10891Snarayan 					vdcp->state = VDC_STATE_INIT;
4285*8cd10891Snarayan 					break;
4286*8cd10891Snarayan 				}
4287*8cd10891Snarayan 			}
4288*8cd10891Snarayan 
42893af08d82Slm66018 			if (vdcp->state != VDC_STATE_INIT_WAITING) {
42903af08d82Slm66018 				DMSG(vdcp, 0,
42913af08d82Slm66018 				    "state moved to %d out from under us...\n",
42923af08d82Slm66018 				    vdcp->state);
42933af08d82Slm66018 			}
42943af08d82Slm66018 			break;
42953af08d82Slm66018 
42963af08d82Slm66018 		case VDC_STATE_NEGOTIATE:
42973af08d82Slm66018 			switch (status = vdc_ver_negotiation(vdcp)) {
42983af08d82Slm66018 			case 0:
42993af08d82Slm66018 				break;
43003af08d82Slm66018 			default:
43013af08d82Slm66018 				DMSG(vdcp, 0, "ver negotiate failed (%d)..\n",
43023af08d82Slm66018 				    status);
43033af08d82Slm66018 				goto reset;
43043af08d82Slm66018 			}
43053af08d82Slm66018 
43063af08d82Slm66018 			switch (status = vdc_attr_negotiation(vdcp)) {
43073af08d82Slm66018 			case 0:
43083af08d82Slm66018 				break;
43093af08d82Slm66018 			default:
43103af08d82Slm66018 				DMSG(vdcp, 0, "attr negotiate failed (%d)..\n",
43113af08d82Slm66018 				    status);
43123af08d82Slm66018 				goto reset;
43133af08d82Slm66018 			}
43143af08d82Slm66018 
43153af08d82Slm66018 			switch (status = vdc_dring_negotiation(vdcp)) {
43163af08d82Slm66018 			case 0:
43173af08d82Slm66018 				break;
43183af08d82Slm66018 			default:
43193af08d82Slm66018 				DMSG(vdcp, 0, "dring negotiate failed (%d)..\n",
43203af08d82Slm66018 				    status);
43213af08d82Slm66018 				goto reset;
43223af08d82Slm66018 			}
43233af08d82Slm66018 
43243af08d82Slm66018 			switch (status = vdc_rdx_exchange(vdcp)) {
43253af08d82Slm66018 			case 0:
43263af08d82Slm66018 				vdcp->state = VDC_STATE_HANDLE_PENDING;
43273af08d82Slm66018 				goto done;
43283af08d82Slm66018 			default:
43293af08d82Slm66018 				DMSG(vdcp, 0, "RDX xchg failed ..(%d)\n",
43303af08d82Slm66018 				    status);
43313af08d82Slm66018 				goto reset;
43323af08d82Slm66018 			}
43333af08d82Slm66018 reset:
43343af08d82Slm66018 			DMSG(vdcp, 0, "negotiation failed: resetting (%d)\n",
43353af08d82Slm66018 			    status);
43363af08d82Slm66018 			vdcp->state = VDC_STATE_RESETTING;
4337655fd6a9Sachartre 			vdcp->self_reset = B_TRUE;
43383af08d82Slm66018 done:
43393af08d82Slm66018 			DMSG(vdcp, 0, "negotiation complete (state=0x%x)...\n",
43403af08d82Slm66018 			    vdcp->state);
43413af08d82Slm66018 			break;
43423af08d82Slm66018 
43433af08d82Slm66018 		case VDC_STATE_HANDLE_PENDING:
43443af08d82Slm66018 
4345655fd6a9Sachartre 			if (vdcp->ctimeout_reached) {
4346655fd6a9Sachartre 				/*
4347655fd6a9Sachartre 				 * The connection timeout had been reached so
4348655fd6a9Sachartre 				 * pending requests have been cancelled. Now
4349655fd6a9Sachartre 				 * that the connection is back we can reset
4350655fd6a9Sachartre 				 * the timeout.
4351655fd6a9Sachartre 				 */
4352655fd6a9Sachartre 				ASSERT(vdcp->local_dring_backup == NULL);
4353655fd6a9Sachartre 				ASSERT(tmid != 0);
4354655fd6a9Sachartre 				tmid = 0;
4355655fd6a9Sachartre 				vdcp->ctimeout_reached = B_FALSE;
4356655fd6a9Sachartre 				vdcp->state = VDC_STATE_RUNNING;
4357655fd6a9Sachartre 				DMSG(vdcp, 0, "[%d] connection to service "
4358655fd6a9Sachartre 				    "domain is up", vdcp->instance);
4359655fd6a9Sachartre 				break;
4360655fd6a9Sachartre 			}
4361655fd6a9Sachartre 
43623af08d82Slm66018 			mutex_exit(&vdcp->lock);
4363655fd6a9Sachartre 			if (tmid != 0) {
4364655fd6a9Sachartre 				(void) untimeout(tmid);
4365655fd6a9Sachartre 				tmid = 0;
4366655fd6a9Sachartre 			}
43673af08d82Slm66018 			status = vdc_resubmit_backup_dring(vdcp);
43683af08d82Slm66018 			mutex_enter(&vdcp->lock);
43693af08d82Slm66018 
43703af08d82Slm66018 			if (status)
43713af08d82Slm66018 				vdcp->state = VDC_STATE_RESETTING;
43723af08d82Slm66018 			else
43733af08d82Slm66018 				vdcp->state = VDC_STATE_RUNNING;
43743af08d82Slm66018 
43753af08d82Slm66018 			break;
43763af08d82Slm66018 
43773af08d82Slm66018 		/* enter running state */
43783af08d82Slm66018 		case VDC_STATE_RUNNING:
43793af08d82Slm66018 			/*
43803af08d82Slm66018 			 * Signal anyone waiting for the connection
43813af08d82Slm66018 			 * to come on line.
43823af08d82Slm66018 			 */
43833af08d82Slm66018 			vdcp->hshake_cnt = 0;
43843af08d82Slm66018 			cv_broadcast(&vdcp->running_cv);
43852f5224aeSachartre 
43862f5224aeSachartre 			/* failfast has to been checked after reset */
43872f5224aeSachartre 			cv_signal(&vdcp->failfast_cv);
43882f5224aeSachartre 
43892f5224aeSachartre 			/* ownership is lost during reset */
43902f5224aeSachartre 			if (vdcp->ownership & VDC_OWNERSHIP_WANTED)
43912f5224aeSachartre 				vdcp->ownership |= VDC_OWNERSHIP_RESET;
43922f5224aeSachartre 			cv_signal(&vdcp->ownership_cv);
43932f5224aeSachartre 
43943af08d82Slm66018 			mutex_exit(&vdcp->lock);
43953af08d82Slm66018 
43963af08d82Slm66018 			for (;;) {
43973af08d82Slm66018 				vio_msg_t msg;
43983af08d82Slm66018 				status = vdc_wait_for_response(vdcp, &msg);
43993af08d82Slm66018 				if (status) break;
44003af08d82Slm66018 
44013af08d82Slm66018 				DMSG(vdcp, 1, "[%d] new pkt(s) available\n",
44023af08d82Slm66018 				    vdcp->instance);
44033af08d82Slm66018 				status = vdc_process_data_msg(vdcp, &msg);
44041ae08745Sheppo 				if (status) {
44053af08d82Slm66018 					DMSG(vdcp, 1, "[%d] process_data_msg "
44063af08d82Slm66018 					    "returned err=%d\n", vdcp->instance,
44073af08d82Slm66018 					    status);
44081ae08745Sheppo 					break;
44091ae08745Sheppo 				}
44101ae08745Sheppo 
44113af08d82Slm66018 			}
4412e1ebb9ecSlm66018 
44133af08d82Slm66018 			mutex_enter(&vdcp->lock);
44143af08d82Slm66018 
44153af08d82Slm66018 			vdcp->state = VDC_STATE_RESETTING;
4416690555a1Sachartre 			vdcp->self_reset = B_TRUE;
44173af08d82Slm66018 			break;
44183af08d82Slm66018 
44193af08d82Slm66018 		case VDC_STATE_RESETTING:
4420655fd6a9Sachartre 			/*
4421655fd6a9Sachartre 			 * When we reach this state, we either come from the
4422655fd6a9Sachartre 			 * VDC_STATE_RUNNING state and we can have pending
4423655fd6a9Sachartre 			 * request but no timeout is armed; or we come from
4424655fd6a9Sachartre 			 * the VDC_STATE_INIT_WAITING, VDC_NEGOTIATE or
4425655fd6a9Sachartre 			 * VDC_HANDLE_PENDING state and there is no pending
4426655fd6a9Sachartre 			 * request or pending requests have already been copied
4427655fd6a9Sachartre 			 * into the backup dring. So we can safely keep the
4428655fd6a9Sachartre 			 * connection timeout armed while we are in this state.
4429655fd6a9Sachartre 			 */
4430655fd6a9Sachartre 
44313af08d82Slm66018 			DMSG(vdcp, 0, "Initiating channel reset "
44323af08d82Slm66018 			    "(pending = %d)\n", (int)vdcp->threads_pending);
44333af08d82Slm66018 
44343af08d82Slm66018 			if (vdcp->self_reset) {
44353af08d82Slm66018 				DMSG(vdcp, 0,
44363af08d82Slm66018 				    "[%d] calling stop_ldc_connection.\n",
44373af08d82Slm66018 				    vdcp->instance);
44383af08d82Slm66018 				status = vdc_stop_ldc_connection(vdcp);
44393af08d82Slm66018 				vdcp->self_reset = B_FALSE;
44401ae08745Sheppo 			}
44411ae08745Sheppo 
44421ae08745Sheppo 			/*
44433af08d82Slm66018 			 * Wait for all threads currently waiting
44443af08d82Slm66018 			 * for a free dring entry to use.
44451ae08745Sheppo 			 */
44463af08d82Slm66018 			while (vdcp->threads_pending) {
44473af08d82Slm66018 				cv_broadcast(&vdcp->membind_cv);
44483af08d82Slm66018 				cv_broadcast(&vdcp->dring_free_cv);
44493af08d82Slm66018 				mutex_exit(&vdcp->lock);
4450205eeb1aSlm66018 				/* give the waiters enough time to wake up */
4451205eeb1aSlm66018 				delay(vdc_hz_min_ldc_delay);
44523af08d82Slm66018 				mutex_enter(&vdcp->lock);
44531ae08745Sheppo 			}
44541ae08745Sheppo 
44553af08d82Slm66018 			ASSERT(vdcp->threads_pending == 0);
44561ae08745Sheppo 
44573af08d82Slm66018 			/* Sanity check that no thread is receiving */
44583af08d82Slm66018 			ASSERT(vdcp->read_state != VDC_READ_WAITING);
44590a55fbb7Slm66018 
44603af08d82Slm66018 			vdcp->read_state = VDC_READ_IDLE;
44613af08d82Slm66018 
44623af08d82Slm66018 			vdc_backup_local_dring(vdcp);
44633af08d82Slm66018 
44643af08d82Slm66018 			/* cleanup the old d-ring */
44653af08d82Slm66018 			vdc_destroy_descriptor_ring(vdcp);
44663af08d82Slm66018 
44673af08d82Slm66018 			/* go and start again */
44683af08d82Slm66018 			vdcp->state = VDC_STATE_INIT;
44693af08d82Slm66018 
44700a55fbb7Slm66018 			break;
44710a55fbb7Slm66018 
44723af08d82Slm66018 		case VDC_STATE_DETACH:
44733af08d82Slm66018 			DMSG(vdcp, 0, "[%d] Reset thread exit cleanup ..\n",
44743af08d82Slm66018 			    vdcp->instance);
44753af08d82Slm66018 
4476655fd6a9Sachartre 			/* cancel any pending timeout */
4477655fd6a9Sachartre 			mutex_exit(&vdcp->lock);
4478655fd6a9Sachartre 			if (tmid != 0) {
4479655fd6a9Sachartre 				(void) untimeout(tmid);
4480655fd6a9Sachartre 				tmid = 0;
4481655fd6a9Sachartre 			}
4482655fd6a9Sachartre 			mutex_enter(&vdcp->lock);
4483655fd6a9Sachartre 
44843c96341aSnarayan 			/*
44853c96341aSnarayan 			 * Signal anyone waiting for connection
44863c96341aSnarayan 			 * to come online
44873c96341aSnarayan 			 */
44883c96341aSnarayan 			cv_broadcast(&vdcp->running_cv);
44893c96341aSnarayan 
44903af08d82Slm66018 			while (vdcp->sync_op_pending) {
44913af08d82Slm66018 				cv_signal(&vdcp->sync_pending_cv);
44923af08d82Slm66018 				cv_signal(&vdcp->sync_blocked_cv);
44933af08d82Slm66018 				mutex_exit(&vdcp->lock);
4494205eeb1aSlm66018 				/* give the waiters enough time to wake up */
4495205eeb1aSlm66018 				delay(vdc_hz_min_ldc_delay);
44963af08d82Slm66018 				mutex_enter(&vdcp->lock);
44970a55fbb7Slm66018 			}
44981ae08745Sheppo 
44993af08d82Slm66018 			mutex_exit(&vdcp->lock);
45003af08d82Slm66018 
45013af08d82Slm66018 			DMSG(vdcp, 0, "[%d] Msg processing thread exiting ..\n",
45023af08d82Slm66018 			    vdcp->instance);
45033af08d82Slm66018 			thread_exit();
45043af08d82Slm66018 			break;
45053af08d82Slm66018 		}
45063af08d82Slm66018 	}
45070a55fbb7Slm66018 }
45080a55fbb7Slm66018 
45090a55fbb7Slm66018 
45100a55fbb7Slm66018 /*
45110a55fbb7Slm66018  * Function:
45120a55fbb7Slm66018  *	vdc_process_data_msg()
45130a55fbb7Slm66018  *
45140a55fbb7Slm66018  * Description:
45150a55fbb7Slm66018  *	This function is called by the message processing thread each time
45160a55fbb7Slm66018  *	a message with a msgtype of VIO_TYPE_DATA is received. It will either
45170a55fbb7Slm66018  *	be an ACK or NACK from vds[1] which vdc handles as follows.
45180a55fbb7Slm66018  *		ACK	- wake up the waiting thread
45190a55fbb7Slm66018  *		NACK	- resend any messages necessary
45200a55fbb7Slm66018  *
45210a55fbb7Slm66018  *	[1] Although the message format allows it, vds should not send a
45220a55fbb7Slm66018  *	    VIO_SUBTYPE_INFO message to vdc asking it to read data; if for
45230a55fbb7Slm66018  *	    some bizarre reason it does, vdc will reset the connection.
45240a55fbb7Slm66018  *
45250a55fbb7Slm66018  * Arguments:
45260a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
45270a55fbb7Slm66018  *	msg	- the LDC message sent by vds
45280a55fbb7Slm66018  *
45290a55fbb7Slm66018  * Return Code:
45300a55fbb7Slm66018  *	0	- Success.
45310a55fbb7Slm66018  *	> 0	- error value returned by LDC
45320a55fbb7Slm66018  */
45330a55fbb7Slm66018 static int
45343af08d82Slm66018 vdc_process_data_msg(vdc_t *vdcp, vio_msg_t *msg)
45350a55fbb7Slm66018 {
45360a55fbb7Slm66018 	int			status = 0;
45373af08d82Slm66018 	vio_dring_msg_t		*dring_msg;
4538d10e4ef2Snarayan 	vdc_local_desc_t	*ldep = NULL;
45393af08d82Slm66018 	int			start, end;
45403af08d82Slm66018 	int			idx;
454190e2f9dcSlm66018 	int			op;
45420a55fbb7Slm66018 
45433af08d82Slm66018 	dring_msg = (vio_dring_msg_t *)msg;
45440a55fbb7Slm66018 
45453af08d82Slm66018 	ASSERT(msg->tag.vio_msgtype == VIO_TYPE_DATA);
45463af08d82Slm66018 	ASSERT(vdcp != NULL);
45473af08d82Slm66018 
45483af08d82Slm66018 	mutex_enter(&vdcp->lock);
45490a55fbb7Slm66018 
45500a55fbb7Slm66018 	/*
45510a55fbb7Slm66018 	 * Check to see if the message has bogus data
45520a55fbb7Slm66018 	 */
4553e1ebb9ecSlm66018 	idx = start = dring_msg->start_idx;
45540a55fbb7Slm66018 	end = dring_msg->end_idx;
45553af08d82Slm66018 	if ((start >= vdcp->dring_len) ||
45563af08d82Slm66018 	    (end >= vdcp->dring_len) || (end < -1)) {
455790e2f9dcSlm66018 		/*
455890e2f9dcSlm66018 		 * Update the I/O statistics to indicate that an error ocurred.
455990e2f9dcSlm66018 		 * No need to update the wait/run queues as no specific read or
456090e2f9dcSlm66018 		 * write request is being completed in response to this 'msg'.
456190e2f9dcSlm66018 		 */
456290e2f9dcSlm66018 		VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
45633af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Bogus ACK data : start %d, end %d\n",
45643af08d82Slm66018 		    vdcp->instance, start, end);
45653af08d82Slm66018 		mutex_exit(&vdcp->lock);
4566e1ebb9ecSlm66018 		return (EINVAL);
45670a55fbb7Slm66018 	}
45680a55fbb7Slm66018 
45690a55fbb7Slm66018 	/*
45700a55fbb7Slm66018 	 * Verify that the sequence number is what vdc expects.
45710a55fbb7Slm66018 	 */
45723af08d82Slm66018 	switch (vdc_verify_seq_num(vdcp, dring_msg)) {
4573e1ebb9ecSlm66018 	case VDC_SEQ_NUM_TODO:
4574e1ebb9ecSlm66018 		break;	/* keep processing this message */
4575e1ebb9ecSlm66018 	case VDC_SEQ_NUM_SKIP:
45763af08d82Slm66018 		mutex_exit(&vdcp->lock);
4577e1ebb9ecSlm66018 		return (0);
4578e1ebb9ecSlm66018 	case VDC_SEQ_NUM_INVALID:
457990e2f9dcSlm66018 		/*
458090e2f9dcSlm66018 		 * Update the I/O statistics to indicate that an error ocurred.
458190e2f9dcSlm66018 		 * No need to update the wait/run queues as no specific read or
458290e2f9dcSlm66018 		 * write request is being completed in response to this 'msg'.
458390e2f9dcSlm66018 		 */
4584366a92acSlm66018 		VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
458590e2f9dcSlm66018 		DMSG(vdcp, 0, "[%d] invalid seqno\n", vdcp->instance);
4586366a92acSlm66018 		mutex_exit(&vdcp->lock);
45870a55fbb7Slm66018 		return (ENXIO);
45880a55fbb7Slm66018 	}
45890a55fbb7Slm66018 
45903af08d82Slm66018 	if (msg->tag.vio_subtype == VIO_SUBTYPE_NACK) {
459190e2f9dcSlm66018 		/*
459290e2f9dcSlm66018 		 * Update the I/O statistics to indicate that an error ocurred.
459390e2f9dcSlm66018 		 *
459490e2f9dcSlm66018 		 * We need to update the run queue if a read or write request
459590e2f9dcSlm66018 		 * is being NACKed - otherwise there will appear to be an
459690e2f9dcSlm66018 		 * indefinite outstanding request and statistics reported by
459790e2f9dcSlm66018 		 * iostat(1M) will be incorrect. The transaction will be
459890e2f9dcSlm66018 		 * resubmitted from the backup DRing following the reset
459990e2f9dcSlm66018 		 * and the wait/run queues will be entered again.
460090e2f9dcSlm66018 		 */
460190e2f9dcSlm66018 		ldep = &vdcp->local_dring[idx];
460290e2f9dcSlm66018 		op = ldep->operation;
460390e2f9dcSlm66018 		if ((op == VD_OP_BREAD) || (op == VD_OP_BWRITE)) {
460490e2f9dcSlm66018 			DTRACE_IO1(done, buf_t *, ldep->cb_arg);
460590e2f9dcSlm66018 			VD_KSTAT_RUNQ_EXIT(vdcp);
460690e2f9dcSlm66018 		}
4607366a92acSlm66018 		VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
460890e2f9dcSlm66018 		VDC_DUMP_DRING_MSG(dring_msg);
460990e2f9dcSlm66018 		DMSG(vdcp, 0, "[%d] DATA NACK\n", vdcp->instance);
46103af08d82Slm66018 		mutex_exit(&vdcp->lock);
4611e1ebb9ecSlm66018 		return (EIO);
46120a55fbb7Slm66018 
46133af08d82Slm66018 	} else if (msg->tag.vio_subtype == VIO_SUBTYPE_INFO) {
461490e2f9dcSlm66018 		/*
461590e2f9dcSlm66018 		 * Update the I/O statistics to indicate that an error occurred.
461690e2f9dcSlm66018 		 * No need to update the wait/run queues as no specific read or
461790e2f9dcSlm66018 		 * write request is being completed in response to this 'msg'.
461890e2f9dcSlm66018 		 */
4619366a92acSlm66018 		VD_UPDATE_ERR_STATS(vdcp, vd_protoerrs);
46203af08d82Slm66018 		mutex_exit(&vdcp->lock);
4621e1ebb9ecSlm66018 		return (EPROTO);
4622e1ebb9ecSlm66018 	}
4623e1ebb9ecSlm66018 
46243af08d82Slm66018 	DMSG(vdcp, 1, ": start %d end %d\n", start, end);
46253af08d82Slm66018 	ASSERT(start == end);
46263af08d82Slm66018 
46273af08d82Slm66018 	ldep = &vdcp->local_dring[idx];
46283af08d82Slm66018 
46293af08d82Slm66018 	DMSG(vdcp, 1, ": state 0x%x - cb_type 0x%x\n",
46303af08d82Slm66018 	    ldep->dep->hdr.dstate, ldep->cb_type);
46313af08d82Slm66018 
4632e1ebb9ecSlm66018 	if (ldep->dep->hdr.dstate == VIO_DESC_DONE) {
46333af08d82Slm66018 		struct buf *bufp;
4634e1ebb9ecSlm66018 
46353af08d82Slm66018 		switch (ldep->cb_type) {
46363af08d82Slm66018 		case CB_SYNC:
46373af08d82Slm66018 			ASSERT(vdcp->sync_op_pending);
4638d10e4ef2Snarayan 
46393af08d82Slm66018 			status = vdc_depopulate_descriptor(vdcp, idx);
46403af08d82Slm66018 			vdcp->sync_op_status = status;
46413af08d82Slm66018 			vdcp->sync_op_pending = B_FALSE;
46423af08d82Slm66018 			cv_signal(&vdcp->sync_pending_cv);
46433af08d82Slm66018 			break;
46444bac2208Snarayan 
46453af08d82Slm66018 		case CB_STRATEGY:
46463af08d82Slm66018 			bufp = ldep->cb_arg;
46473af08d82Slm66018 			ASSERT(bufp != NULL);
46483c96341aSnarayan 			bufp->b_resid =
46493c96341aSnarayan 			    bufp->b_bcount - ldep->dep->payload.nbytes;
46503af08d82Slm66018 			status = ldep->dep->payload.status; /* Future:ntoh */
46513af08d82Slm66018 			if (status != 0) {
46523af08d82Slm66018 				DMSG(vdcp, 1, "strategy status=%d\n", status);
4653366a92acSlm66018 				VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
46543af08d82Slm66018 				bioerror(bufp, status);
4655d10e4ef2Snarayan 			}
46562f5224aeSachartre 
46572f5224aeSachartre 			(void) vdc_depopulate_descriptor(vdcp, idx);
46583c96341aSnarayan 
46593c96341aSnarayan 			DMSG(vdcp, 1,
46603c96341aSnarayan 			    "strategy complete req=%ld bytes resp=%ld bytes\n",
46613c96341aSnarayan 			    bufp->b_bcount, ldep->dep->payload.nbytes);
46622f5224aeSachartre 
46632f5224aeSachartre 			if (status != 0 && vdcp->failfast_interval != 0) {
46642f5224aeSachartre 				/*
46652f5224aeSachartre 				 * The I/O has failed and failfast is enabled.
46662f5224aeSachartre 				 * We need the failfast thread to check if the
46672f5224aeSachartre 				 * failure is due to a reservation conflict.
46682f5224aeSachartre 				 */
46692f5224aeSachartre 				(void) vdc_failfast_io_queue(vdcp, bufp);
46702f5224aeSachartre 			} else {
4671366a92acSlm66018 				if (status == 0) {
467290e2f9dcSlm66018 					op = (bufp->b_flags & B_READ) ?
4673366a92acSlm66018 					    VD_OP_BREAD : VD_OP_BWRITE;
4674366a92acSlm66018 					VD_UPDATE_IO_STATS(vdcp, op,
4675366a92acSlm66018 					    ldep->dep->payload.nbytes);
4676366a92acSlm66018 				}
467790e2f9dcSlm66018 				VD_KSTAT_RUNQ_EXIT(vdcp);
4678366a92acSlm66018 				DTRACE_IO1(done, buf_t *, bufp);
46792f5224aeSachartre 				biodone(bufp);
46802f5224aeSachartre 			}
46813af08d82Slm66018 			break;
46823af08d82Slm66018 
46833af08d82Slm66018 		default:
46843af08d82Slm66018 			ASSERT(0);
46850a55fbb7Slm66018 		}
46863af08d82Slm66018 	}
46873af08d82Slm66018 
46883af08d82Slm66018 	/* let the arrival signal propogate */
46893af08d82Slm66018 	mutex_exit(&vdcp->lock);
46900a55fbb7Slm66018 
4691e1ebb9ecSlm66018 	/* probe gives the count of how many entries were processed */
4692366a92acSlm66018 	DTRACE_PROBE2(processed, int, 1, vdc_t *, vdcp);
46930a55fbb7Slm66018 
46943af08d82Slm66018 	return (0);
46950a55fbb7Slm66018 }
46960a55fbb7Slm66018 
46970a55fbb7Slm66018 
46980a55fbb7Slm66018 /*
46990a55fbb7Slm66018  * Function:
47000a55fbb7Slm66018  *	vdc_handle_ver_msg()
47010a55fbb7Slm66018  *
47020a55fbb7Slm66018  * Description:
47030a55fbb7Slm66018  *
47040a55fbb7Slm66018  * Arguments:
47050a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
47060a55fbb7Slm66018  *	ver_msg	- LDC message sent by vDisk server
47070a55fbb7Slm66018  *
47080a55fbb7Slm66018  * Return Code:
47090a55fbb7Slm66018  *	0	- Success
47100a55fbb7Slm66018  */
47110a55fbb7Slm66018 static int
47120a55fbb7Slm66018 vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg)
47130a55fbb7Slm66018 {
47140a55fbb7Slm66018 	int status = 0;
47150a55fbb7Slm66018 
47160a55fbb7Slm66018 	ASSERT(vdc != NULL);
47170a55fbb7Slm66018 	ASSERT(mutex_owned(&vdc->lock));
47180a55fbb7Slm66018 
47190a55fbb7Slm66018 	if (ver_msg->tag.vio_subtype_env != VIO_VER_INFO) {
47200a55fbb7Slm66018 		return (EPROTO);
47210a55fbb7Slm66018 	}
47220a55fbb7Slm66018 
47230a55fbb7Slm66018 	if (ver_msg->dev_class != VDEV_DISK_SERVER) {
47240a55fbb7Slm66018 		return (EINVAL);
47250a55fbb7Slm66018 	}
47260a55fbb7Slm66018 
47270a55fbb7Slm66018 	switch (ver_msg->tag.vio_subtype) {
47280a55fbb7Slm66018 	case VIO_SUBTYPE_ACK:
47290a55fbb7Slm66018 		/*
47300a55fbb7Slm66018 		 * We check to see if the version returned is indeed supported
47310a55fbb7Slm66018 		 * (The server may have also adjusted the minor number downwards
47320a55fbb7Slm66018 		 * and if so 'ver_msg' will contain the actual version agreed)
47330a55fbb7Slm66018 		 */
47340a55fbb7Slm66018 		if (vdc_is_supported_version(ver_msg)) {
47350a55fbb7Slm66018 			vdc->ver.major = ver_msg->ver_major;
47360a55fbb7Slm66018 			vdc->ver.minor = ver_msg->ver_minor;
47370a55fbb7Slm66018 			ASSERT(vdc->ver.major > 0);
47380a55fbb7Slm66018 		} else {
47390a55fbb7Slm66018 			status = EPROTO;
47400a55fbb7Slm66018 		}
47410a55fbb7Slm66018 		break;
47420a55fbb7Slm66018 
47430a55fbb7Slm66018 	case VIO_SUBTYPE_NACK:
47440a55fbb7Slm66018 		/*
47450a55fbb7Slm66018 		 * call vdc_is_supported_version() which will return the next
47460a55fbb7Slm66018 		 * supported version (if any) in 'ver_msg'
47470a55fbb7Slm66018 		 */
47480a55fbb7Slm66018 		(void) vdc_is_supported_version(ver_msg);
47490a55fbb7Slm66018 		if (ver_msg->ver_major > 0) {
47500a55fbb7Slm66018 			size_t len = sizeof (*ver_msg);
47510a55fbb7Slm66018 
47520a55fbb7Slm66018 			ASSERT(vdc->ver.major > 0);
47530a55fbb7Slm66018 
47540a55fbb7Slm66018 			/* reset the necessary fields and resend */
47550a55fbb7Slm66018 			ver_msg->tag.vio_subtype = VIO_SUBTYPE_INFO;
47560a55fbb7Slm66018 			ver_msg->dev_class = VDEV_DISK;
47570a55fbb7Slm66018 
47580a55fbb7Slm66018 			status = vdc_send(vdc, (caddr_t)ver_msg, &len);
47593af08d82Slm66018 			DMSG(vdc, 0, "[%d] Resend VER info (LDC status = %d)\n",
47600a55fbb7Slm66018 			    vdc->instance, status);
47610a55fbb7Slm66018 			if (len != sizeof (*ver_msg))
47620a55fbb7Slm66018 				status = EBADMSG;
47630a55fbb7Slm66018 		} else {
476487a7269eSachartre 			DMSG(vdc, 0, "[%d] No common version with vDisk server",
476587a7269eSachartre 			    vdc->instance);
47660a55fbb7Slm66018 			status = ENOTSUP;
47670a55fbb7Slm66018 		}
47680a55fbb7Slm66018 
47690a55fbb7Slm66018 		break;
47701ae08745Sheppo 	case VIO_SUBTYPE_INFO:
47711ae08745Sheppo 		/*
47721ae08745Sheppo 		 * Handle the case where vds starts handshake
4773eff7243fSlm66018 		 * (for now only vdc is the instigator)
47741ae08745Sheppo 		 */
47751ae08745Sheppo 		status = ENOTSUP;
47761ae08745Sheppo 		break;
47771ae08745Sheppo 
47781ae08745Sheppo 	default:
47790a55fbb7Slm66018 		status = EINVAL;
47801ae08745Sheppo 		break;
47811ae08745Sheppo 	}
47821ae08745Sheppo 
47830a55fbb7Slm66018 	return (status);
47840a55fbb7Slm66018 }
47850a55fbb7Slm66018 
47860a55fbb7Slm66018 /*
47870a55fbb7Slm66018  * Function:
47880a55fbb7Slm66018  *	vdc_handle_attr_msg()
47890a55fbb7Slm66018  *
47900a55fbb7Slm66018  * Description:
47910a55fbb7Slm66018  *
47920a55fbb7Slm66018  * Arguments:
47930a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
47940a55fbb7Slm66018  *	attr_msg	- LDC message sent by vDisk server
47950a55fbb7Slm66018  *
47960a55fbb7Slm66018  * Return Code:
47970a55fbb7Slm66018  *	0	- Success
47980a55fbb7Slm66018  */
47990a55fbb7Slm66018 static int
48000a55fbb7Slm66018 vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg)
48010a55fbb7Slm66018 {
48020a55fbb7Slm66018 	int status = 0;
48030a55fbb7Slm66018 
48040a55fbb7Slm66018 	ASSERT(vdc != NULL);
48050a55fbb7Slm66018 	ASSERT(mutex_owned(&vdc->lock));
48060a55fbb7Slm66018 
48070a55fbb7Slm66018 	if (attr_msg->tag.vio_subtype_env != VIO_ATTR_INFO) {
48080a55fbb7Slm66018 		return (EPROTO);
48090a55fbb7Slm66018 	}
48100a55fbb7Slm66018 
48110a55fbb7Slm66018 	switch (attr_msg->tag.vio_subtype) {
48121ae08745Sheppo 	case VIO_SUBTYPE_ACK:
48131ae08745Sheppo 		/*
48141ae08745Sheppo 		 * We now verify the attributes sent by vds.
48151ae08745Sheppo 		 */
481678fcd0a1Sachartre 		if (attr_msg->vdisk_size == 0) {
481778fcd0a1Sachartre 			DMSG(vdc, 0, "[%d] Invalid disk size from vds",
481878fcd0a1Sachartre 			    vdc->instance);
481978fcd0a1Sachartre 			status = EINVAL;
482078fcd0a1Sachartre 			break;
482178fcd0a1Sachartre 		}
482278fcd0a1Sachartre 
482378fcd0a1Sachartre 		if (attr_msg->max_xfer_sz == 0) {
482478fcd0a1Sachartre 			DMSG(vdc, 0, "[%d] Invalid transfer size from vds",
482578fcd0a1Sachartre 			    vdc->instance);
482678fcd0a1Sachartre 			status = EINVAL;
482778fcd0a1Sachartre 			break;
482878fcd0a1Sachartre 		}
482978fcd0a1Sachartre 
48302f5224aeSachartre 		if (attr_msg->vdisk_size == VD_SIZE_UNKNOWN) {
48312f5224aeSachartre 			DMSG(vdc, 0, "[%d] Unknown disk size from vds",
48322f5224aeSachartre 			    vdc->instance);
48332f5224aeSachartre 			attr_msg->vdisk_size = 0;
48342f5224aeSachartre 		}
48352f5224aeSachartre 
483678fcd0a1Sachartre 		/*
483778fcd0a1Sachartre 		 * If the disk size is already set check that it hasn't changed.
483878fcd0a1Sachartre 		 */
48392f5224aeSachartre 		if ((vdc->vdisk_size != 0) && (attr_msg->vdisk_size != 0) &&
484078fcd0a1Sachartre 		    (vdc->vdisk_size != attr_msg->vdisk_size)) {
484178fcd0a1Sachartre 			DMSG(vdc, 0, "[%d] Different disk size from vds "
484278fcd0a1Sachartre 			    "(old=0x%lx - new=0x%lx", vdc->instance,
484378fcd0a1Sachartre 			    vdc->vdisk_size, attr_msg->vdisk_size)
484478fcd0a1Sachartre 			status = EINVAL;
484578fcd0a1Sachartre 			break;
484678fcd0a1Sachartre 		}
484778fcd0a1Sachartre 
48481ae08745Sheppo 		vdc->vdisk_size = attr_msg->vdisk_size;
48491ae08745Sheppo 		vdc->vdisk_type = attr_msg->vdisk_type;
485017cadca8Slm66018 		vdc->operations = attr_msg->operations;
485117cadca8Slm66018 		if (vio_ver_is_supported(vdc->ver, 1, 1))
485217cadca8Slm66018 			vdc->vdisk_media = attr_msg->vdisk_media;
485317cadca8Slm66018 		else
485417cadca8Slm66018 			vdc->vdisk_media = 0;
48551ae08745Sheppo 
48563af08d82Slm66018 		DMSG(vdc, 0, "[%d] max_xfer_sz: sent %lx acked %lx\n",
4857e1ebb9ecSlm66018 		    vdc->instance, vdc->max_xfer_sz, attr_msg->max_xfer_sz);
48583af08d82Slm66018 		DMSG(vdc, 0, "[%d] vdisk_block_size: sent %lx acked %x\n",
4859e1ebb9ecSlm66018 		    vdc->instance, vdc->block_size,
4860e1ebb9ecSlm66018 		    attr_msg->vdisk_block_size);
4861e1ebb9ecSlm66018 
48621ae08745Sheppo 		/*
4863e1ebb9ecSlm66018 		 * We don't know at compile time what the vDisk server will
486417cadca8Slm66018 		 * think are good values but we apply a large (arbitrary)
4865e1ebb9ecSlm66018 		 * upper bound to prevent memory exhaustion in vdc if it was
4866e1ebb9ecSlm66018 		 * allocating a DRing based of huge values sent by the server.
4867e1ebb9ecSlm66018 		 * We probably will never exceed this except if the message
4868e1ebb9ecSlm66018 		 * was garbage.
48691ae08745Sheppo 		 */
4870e1ebb9ecSlm66018 		if ((attr_msg->max_xfer_sz * attr_msg->vdisk_block_size) <=
4871e1ebb9ecSlm66018 		    (PAGESIZE * DEV_BSIZE)) {
4872e1ebb9ecSlm66018 			vdc->max_xfer_sz = attr_msg->max_xfer_sz;
4873e1ebb9ecSlm66018 			vdc->block_size = attr_msg->vdisk_block_size;
4874e1ebb9ecSlm66018 		} else {
48753af08d82Slm66018 			DMSG(vdc, 0, "[%d] vds block transfer size too big;"
4876e1ebb9ecSlm66018 			    " using max supported by vdc", vdc->instance);
48771ae08745Sheppo 		}
48781ae08745Sheppo 
4879f0ca1d9aSsb155480 		if ((attr_msg->xfer_mode != VIO_DRING_MODE_V1_0) ||
48801ae08745Sheppo 		    (attr_msg->vdisk_size > INT64_MAX) ||
488117cadca8Slm66018 		    (attr_msg->operations == 0) ||
48821ae08745Sheppo 		    (attr_msg->vdisk_type > VD_DISK_TYPE_DISK)) {
48833af08d82Slm66018 			DMSG(vdc, 0, "[%d] Invalid attributes from vds",
4884e1ebb9ecSlm66018 			    vdc->instance);
48851ae08745Sheppo 			status = EINVAL;
48861ae08745Sheppo 			break;
48871ae08745Sheppo 		}
48881ae08745Sheppo 
488978fcd0a1Sachartre 		/*
489078fcd0a1Sachartre 		 * Now that we have received all attributes we can create a
489178fcd0a1Sachartre 		 * fake geometry for the disk.
489278fcd0a1Sachartre 		 */
489378fcd0a1Sachartre 		vdc_create_fake_geometry(vdc);
48941ae08745Sheppo 		break;
48951ae08745Sheppo 
48961ae08745Sheppo 	case VIO_SUBTYPE_NACK:
48971ae08745Sheppo 		/*
48981ae08745Sheppo 		 * vds could not handle the attributes we sent so we
48991ae08745Sheppo 		 * stop negotiating.
49001ae08745Sheppo 		 */
49011ae08745Sheppo 		status = EPROTO;
49021ae08745Sheppo 		break;
49031ae08745Sheppo 
49041ae08745Sheppo 	case VIO_SUBTYPE_INFO:
49051ae08745Sheppo 		/*
49061ae08745Sheppo 		 * Handle the case where vds starts the handshake
49071ae08745Sheppo 		 * (for now; vdc is the only supported instigatior)
49081ae08745Sheppo 		 */
49091ae08745Sheppo 		status = ENOTSUP;
49101ae08745Sheppo 		break;
49111ae08745Sheppo 
49121ae08745Sheppo 	default:
49131ae08745Sheppo 		status = ENOTSUP;
49141ae08745Sheppo 		break;
49151ae08745Sheppo 	}
49161ae08745Sheppo 
49170a55fbb7Slm66018 	return (status);
49181ae08745Sheppo }
49191ae08745Sheppo 
49200a55fbb7Slm66018 /*
49210a55fbb7Slm66018  * Function:
49220a55fbb7Slm66018  *	vdc_handle_dring_reg_msg()
49230a55fbb7Slm66018  *
49240a55fbb7Slm66018  * Description:
49250a55fbb7Slm66018  *
49260a55fbb7Slm66018  * Arguments:
49270a55fbb7Slm66018  *	vdc		- soft state pointer for this instance of the driver.
49280a55fbb7Slm66018  *	dring_msg	- LDC message sent by vDisk server
49290a55fbb7Slm66018  *
49300a55fbb7Slm66018  * Return Code:
49310a55fbb7Slm66018  *	0	- Success
49320a55fbb7Slm66018  */
49330a55fbb7Slm66018 static int
49340a55fbb7Slm66018 vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *dring_msg)
49350a55fbb7Slm66018 {
49360a55fbb7Slm66018 	int		status = 0;
49371ae08745Sheppo 
49380a55fbb7Slm66018 	ASSERT(vdc != NULL);
49390a55fbb7Slm66018 	ASSERT(mutex_owned(&vdc->lock));
49400a55fbb7Slm66018 
49410a55fbb7Slm66018 	if (dring_msg->tag.vio_subtype_env != VIO_DRING_REG) {
49420a55fbb7Slm66018 		return (EPROTO);
49430a55fbb7Slm66018 	}
49440a55fbb7Slm66018 
49450a55fbb7Slm66018 	switch (dring_msg->tag.vio_subtype) {
49460a55fbb7Slm66018 	case VIO_SUBTYPE_ACK:
49471ae08745Sheppo 		/* save the received dring_ident */
49481ae08745Sheppo 		vdc->dring_ident = dring_msg->dring_ident;
49493af08d82Slm66018 		DMSG(vdc, 0, "[%d] Received dring ident=0x%lx\n",
4950e1ebb9ecSlm66018 		    vdc->instance, vdc->dring_ident);
49511ae08745Sheppo 		break;
49521ae08745Sheppo 
49531ae08745Sheppo 	case VIO_SUBTYPE_NACK:
49541ae08745Sheppo 		/*
49551ae08745Sheppo 		 * vds could not handle the DRing info we sent so we
49561ae08745Sheppo 		 * stop negotiating.
49571ae08745Sheppo 		 */
49583af08d82Slm66018 		DMSG(vdc, 0, "[%d] server could not register DRing\n",
49593af08d82Slm66018 		    vdc->instance);
49601ae08745Sheppo 		status = EPROTO;
49611ae08745Sheppo 		break;
49621ae08745Sheppo 
49631ae08745Sheppo 	case VIO_SUBTYPE_INFO:
49641ae08745Sheppo 		/*
49651ae08745Sheppo 		 * Handle the case where vds starts handshake
49661ae08745Sheppo 		 * (for now only vdc is the instigatior)
49671ae08745Sheppo 		 */
49681ae08745Sheppo 		status = ENOTSUP;
49691ae08745Sheppo 		break;
49701ae08745Sheppo 	default:
49711ae08745Sheppo 		status = ENOTSUP;
49721ae08745Sheppo 	}
49731ae08745Sheppo 
49741ae08745Sheppo 	return (status);
49751ae08745Sheppo }
49761ae08745Sheppo 
49771ae08745Sheppo /*
49781ae08745Sheppo  * Function:
49791ae08745Sheppo  *	vdc_verify_seq_num()
49801ae08745Sheppo  *
49811ae08745Sheppo  * Description:
4982e1ebb9ecSlm66018  *	This functions verifies that the sequence number sent back by the vDisk
4983e1ebb9ecSlm66018  *	server with the latest message is what is expected (i.e. it is greater
4984e1ebb9ecSlm66018  *	than the last seq num sent by the vDisk server and less than or equal
4985e1ebb9ecSlm66018  *	to the last seq num generated by vdc).
4986e1ebb9ecSlm66018  *
4987e1ebb9ecSlm66018  *	It then checks the request ID to see if any requests need processing
4988e1ebb9ecSlm66018  *	in the DRing.
49891ae08745Sheppo  *
49901ae08745Sheppo  * Arguments:
49911ae08745Sheppo  *	vdc		- soft state pointer for this instance of the driver.
49921ae08745Sheppo  *	dring_msg	- pointer to the LDC message sent by vds
49931ae08745Sheppo  *
49941ae08745Sheppo  * Return Code:
4995e1ebb9ecSlm66018  *	VDC_SEQ_NUM_TODO	- Message needs to be processed
4996e1ebb9ecSlm66018  *	VDC_SEQ_NUM_SKIP	- Message has already been processed
4997e1ebb9ecSlm66018  *	VDC_SEQ_NUM_INVALID	- The seq numbers are so out of sync,
4998e1ebb9ecSlm66018  *				  vdc cannot deal with them
49991ae08745Sheppo  */
5000e1ebb9ecSlm66018 static int
5001e1ebb9ecSlm66018 vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg)
50021ae08745Sheppo {
50031ae08745Sheppo 	ASSERT(vdc != NULL);
50041ae08745Sheppo 	ASSERT(dring_msg != NULL);
5005d10e4ef2Snarayan 	ASSERT(mutex_owned(&vdc->lock));
50061ae08745Sheppo 
50071ae08745Sheppo 	/*
50081ae08745Sheppo 	 * Check to see if the messages were responded to in the correct
5009e1ebb9ecSlm66018 	 * order by vds.
50101ae08745Sheppo 	 */
5011e1ebb9ecSlm66018 	if ((dring_msg->seq_num <= vdc->seq_num_reply) ||
5012e1ebb9ecSlm66018 	    (dring_msg->seq_num > vdc->seq_num)) {
50133af08d82Slm66018 		DMSG(vdc, 0, "?[%d] Bogus sequence_number %lu: "
5014e1ebb9ecSlm66018 		    "%lu > expected <= %lu (last proc req %lu sent %lu)\n",
5015e1ebb9ecSlm66018 		    vdc->instance, dring_msg->seq_num,
5016e1ebb9ecSlm66018 		    vdc->seq_num_reply, vdc->seq_num,
5017e1ebb9ecSlm66018 		    vdc->req_id_proc, vdc->req_id);
5018e1ebb9ecSlm66018 		return (VDC_SEQ_NUM_INVALID);
50191ae08745Sheppo 	}
5020e1ebb9ecSlm66018 	vdc->seq_num_reply = dring_msg->seq_num;
50211ae08745Sheppo 
5022e1ebb9ecSlm66018 	if (vdc->req_id_proc < vdc->req_id)
5023e1ebb9ecSlm66018 		return (VDC_SEQ_NUM_TODO);
5024e1ebb9ecSlm66018 	else
5025e1ebb9ecSlm66018 		return (VDC_SEQ_NUM_SKIP);
50261ae08745Sheppo }
50271ae08745Sheppo 
50280a55fbb7Slm66018 
50290a55fbb7Slm66018 /*
50300a55fbb7Slm66018  * Function:
50310a55fbb7Slm66018  *	vdc_is_supported_version()
50320a55fbb7Slm66018  *
50330a55fbb7Slm66018  * Description:
50340a55fbb7Slm66018  *	This routine checks if the major/minor version numbers specified in
50350a55fbb7Slm66018  *	'ver_msg' are supported. If not it finds the next version that is
50360a55fbb7Slm66018  *	in the supported version list 'vdc_version[]' and sets the fields in
50370a55fbb7Slm66018  *	'ver_msg' to those values
50380a55fbb7Slm66018  *
50390a55fbb7Slm66018  * Arguments:
50400a55fbb7Slm66018  *	ver_msg	- LDC message sent by vDisk server
50410a55fbb7Slm66018  *
50420a55fbb7Slm66018  * Return Code:
50430a55fbb7Slm66018  *	B_TRUE	- Success
50440a55fbb7Slm66018  *	B_FALSE	- Version not supported
50450a55fbb7Slm66018  */
50460a55fbb7Slm66018 static boolean_t
50470a55fbb7Slm66018 vdc_is_supported_version(vio_ver_msg_t *ver_msg)
50480a55fbb7Slm66018 {
50490a55fbb7Slm66018 	int vdc_num_versions = sizeof (vdc_version) / sizeof (vdc_version[0]);
50500a55fbb7Slm66018 
50510a55fbb7Slm66018 	for (int i = 0; i < vdc_num_versions; i++) {
50520a55fbb7Slm66018 		ASSERT(vdc_version[i].major > 0);
50530a55fbb7Slm66018 		ASSERT((i == 0) ||
50540a55fbb7Slm66018 		    (vdc_version[i].major < vdc_version[i-1].major));
50550a55fbb7Slm66018 
50560a55fbb7Slm66018 		/*
50570a55fbb7Slm66018 		 * If the major versions match, adjust the minor version, if
50580a55fbb7Slm66018 		 * necessary, down to the highest value supported by this
50590a55fbb7Slm66018 		 * client. The server should support all minor versions lower
50600a55fbb7Slm66018 		 * than the value it sent
50610a55fbb7Slm66018 		 */
50620a55fbb7Slm66018 		if (ver_msg->ver_major == vdc_version[i].major) {
50630a55fbb7Slm66018 			if (ver_msg->ver_minor > vdc_version[i].minor) {
50643af08d82Slm66018 				DMSGX(0,
50653af08d82Slm66018 				    "Adjusting minor version from %u to %u",
50660a55fbb7Slm66018 				    ver_msg->ver_minor, vdc_version[i].minor);
50670a55fbb7Slm66018 				ver_msg->ver_minor = vdc_version[i].minor;
50680a55fbb7Slm66018 			}
50690a55fbb7Slm66018 			return (B_TRUE);
50700a55fbb7Slm66018 		}
50710a55fbb7Slm66018 
50720a55fbb7Slm66018 		/*
50730a55fbb7Slm66018 		 * If the message contains a higher major version number, set
50740a55fbb7Slm66018 		 * the message's major/minor versions to the current values
50750a55fbb7Slm66018 		 * and return false, so this message will get resent with
50760a55fbb7Slm66018 		 * these values, and the server will potentially try again
50770a55fbb7Slm66018 		 * with the same or a lower version
50780a55fbb7Slm66018 		 */
50790a55fbb7Slm66018 		if (ver_msg->ver_major > vdc_version[i].major) {
50800a55fbb7Slm66018 			ver_msg->ver_major = vdc_version[i].major;
50810a55fbb7Slm66018 			ver_msg->ver_minor = vdc_version[i].minor;
50823af08d82Slm66018 			DMSGX(0, "Suggesting major/minor (0x%x/0x%x)\n",
50830a55fbb7Slm66018 			    ver_msg->ver_major, ver_msg->ver_minor);
50840a55fbb7Slm66018 
50850a55fbb7Slm66018 			return (B_FALSE);
50860a55fbb7Slm66018 		}
50870a55fbb7Slm66018 
50880a55fbb7Slm66018 		/*
50890a55fbb7Slm66018 		 * Otherwise, the message's major version is less than the
50900a55fbb7Slm66018 		 * current major version, so continue the loop to the next
50910a55fbb7Slm66018 		 * (lower) supported version
50920a55fbb7Slm66018 		 */
50930a55fbb7Slm66018 	}
50940a55fbb7Slm66018 
50950a55fbb7Slm66018 	/*
50960a55fbb7Slm66018 	 * No common version was found; "ground" the version pair in the
50970a55fbb7Slm66018 	 * message to terminate negotiation
50980a55fbb7Slm66018 	 */
50990a55fbb7Slm66018 	ver_msg->ver_major = 0;
51000a55fbb7Slm66018 	ver_msg->ver_minor = 0;
51010a55fbb7Slm66018 
51020a55fbb7Slm66018 	return (B_FALSE);
51030a55fbb7Slm66018 }
51041ae08745Sheppo /* -------------------------------------------------------------------------- */
51051ae08745Sheppo 
51061ae08745Sheppo /*
51071ae08745Sheppo  * DKIO(7) support
51081ae08745Sheppo  */
51091ae08745Sheppo 
51101ae08745Sheppo typedef struct vdc_dk_arg {
51111ae08745Sheppo 	struct dk_callback	dkc;
51121ae08745Sheppo 	int			mode;
51131ae08745Sheppo 	dev_t			dev;
51141ae08745Sheppo 	vdc_t			*vdc;
51151ae08745Sheppo } vdc_dk_arg_t;
51161ae08745Sheppo 
51171ae08745Sheppo /*
51181ae08745Sheppo  * Function:
51191ae08745Sheppo  * 	vdc_dkio_flush_cb()
51201ae08745Sheppo  *
51211ae08745Sheppo  * Description:
51221ae08745Sheppo  *	This routine is a callback for DKIOCFLUSHWRITECACHE which can be called
51231ae08745Sheppo  *	by kernel code.
51241ae08745Sheppo  *
51251ae08745Sheppo  * Arguments:
51261ae08745Sheppo  *	arg	- a pointer to a vdc_dk_arg_t structure.
51271ae08745Sheppo  */
51281ae08745Sheppo void
51291ae08745Sheppo vdc_dkio_flush_cb(void *arg)
51301ae08745Sheppo {
51311ae08745Sheppo 	struct vdc_dk_arg	*dk_arg = (struct vdc_dk_arg *)arg;
51321ae08745Sheppo 	struct dk_callback	*dkc = NULL;
51331ae08745Sheppo 	vdc_t			*vdc = NULL;
51341ae08745Sheppo 	int			rv;
51351ae08745Sheppo 
51361ae08745Sheppo 	if (dk_arg == NULL) {
51373af08d82Slm66018 		cmn_err(CE_NOTE, "?[Unk] DKIOCFLUSHWRITECACHE arg is NULL\n");
51381ae08745Sheppo 		return;
51391ae08745Sheppo 	}
51401ae08745Sheppo 	dkc = &dk_arg->dkc;
51411ae08745Sheppo 	vdc = dk_arg->vdc;
51421ae08745Sheppo 	ASSERT(vdc != NULL);
51431ae08745Sheppo 
51443af08d82Slm66018 	rv = vdc_do_sync_op(vdc, VD_OP_FLUSH, NULL, 0,
51452f5224aeSachartre 	    VDCPART(dk_arg->dev), 0, CB_SYNC, 0, VIO_both_dir, B_TRUE);
51461ae08745Sheppo 	if (rv != 0) {
51473af08d82Slm66018 		DMSG(vdc, 0, "[%d] DKIOCFLUSHWRITECACHE failed %d : model %x\n",
5148e1ebb9ecSlm66018 		    vdc->instance, rv,
51491ae08745Sheppo 		    ddi_model_convert_from(dk_arg->mode & FMODELS));
51501ae08745Sheppo 	}
51511ae08745Sheppo 
51521ae08745Sheppo 	/*
51531ae08745Sheppo 	 * Trigger the call back to notify the caller the the ioctl call has
51541ae08745Sheppo 	 * been completed.
51551ae08745Sheppo 	 */
51561ae08745Sheppo 	if ((dk_arg->mode & FKIOCTL) &&
51571ae08745Sheppo 	    (dkc != NULL) &&
51581ae08745Sheppo 	    (dkc->dkc_callback != NULL)) {
51591ae08745Sheppo 		ASSERT(dkc->dkc_cookie != NULL);
51608e6a2a04Slm66018 		(*dkc->dkc_callback)(dkc->dkc_cookie, rv);
51611ae08745Sheppo 	}
51621ae08745Sheppo 
51631ae08745Sheppo 	/* Indicate that one less DKIO write flush is outstanding */
51641ae08745Sheppo 	mutex_enter(&vdc->lock);
51651ae08745Sheppo 	vdc->dkio_flush_pending--;
51661ae08745Sheppo 	ASSERT(vdc->dkio_flush_pending >= 0);
51671ae08745Sheppo 	mutex_exit(&vdc->lock);
51688e6a2a04Slm66018 
51698e6a2a04Slm66018 	/* free the mem that was allocated when the callback was dispatched */
51708e6a2a04Slm66018 	kmem_free(arg, sizeof (vdc_dk_arg_t));
51711ae08745Sheppo }
51721ae08745Sheppo 
51731ae08745Sheppo /*
517487a7269eSachartre  * Function:
51759642afceSachartre  * 	vdc_dkio_gapart()
517687a7269eSachartre  *
517787a7269eSachartre  * Description:
517887a7269eSachartre  *	This function implements the DKIOCGAPART ioctl.
517987a7269eSachartre  *
518087a7269eSachartre  * Arguments:
518178fcd0a1Sachartre  *	vdc	- soft state pointer
518287a7269eSachartre  *	arg	- a pointer to a dk_map[NDKMAP] or dk_map32[NDKMAP] structure
518387a7269eSachartre  *	flag	- ioctl flags
518487a7269eSachartre  */
518587a7269eSachartre static int
51869642afceSachartre vdc_dkio_gapart(vdc_t *vdc, caddr_t arg, int flag)
518787a7269eSachartre {
518878fcd0a1Sachartre 	struct dk_geom *geom;
518978fcd0a1Sachartre 	struct vtoc *vtoc;
519087a7269eSachartre 	union {
519187a7269eSachartre 		struct dk_map map[NDKMAP];
519287a7269eSachartre 		struct dk_map32 map32[NDKMAP];
519387a7269eSachartre 	} data;
519487a7269eSachartre 	int i, rv, size;
519587a7269eSachartre 
519678fcd0a1Sachartre 	mutex_enter(&vdc->lock);
519787a7269eSachartre 
519878fcd0a1Sachartre 	if ((rv = vdc_validate_geometry(vdc)) != 0) {
519978fcd0a1Sachartre 		mutex_exit(&vdc->lock);
520087a7269eSachartre 		return (rv);
520178fcd0a1Sachartre 	}
520287a7269eSachartre 
520378fcd0a1Sachartre 	vtoc = vdc->vtoc;
520478fcd0a1Sachartre 	geom = vdc->geom;
520587a7269eSachartre 
520687a7269eSachartre 	if (ddi_model_convert_from(flag & FMODELS) == DDI_MODEL_ILP32) {
520787a7269eSachartre 
520878fcd0a1Sachartre 		for (i = 0; i < vtoc->v_nparts; i++) {
520978fcd0a1Sachartre 			data.map32[i].dkl_cylno = vtoc->v_part[i].p_start /
521078fcd0a1Sachartre 			    (geom->dkg_nhead * geom->dkg_nsect);
521178fcd0a1Sachartre 			data.map32[i].dkl_nblk = vtoc->v_part[i].p_size;
521287a7269eSachartre 		}
521387a7269eSachartre 		size = NDKMAP * sizeof (struct dk_map32);
521487a7269eSachartre 
521587a7269eSachartre 	} else {
521687a7269eSachartre 
521778fcd0a1Sachartre 		for (i = 0; i < vtoc->v_nparts; i++) {
521878fcd0a1Sachartre 			data.map[i].dkl_cylno = vtoc->v_part[i].p_start /
521978fcd0a1Sachartre 			    (geom->dkg_nhead * geom->dkg_nsect);
522078fcd0a1Sachartre 			data.map[i].dkl_nblk = vtoc->v_part[i].p_size;
522187a7269eSachartre 		}
522287a7269eSachartre 		size = NDKMAP * sizeof (struct dk_map);
522387a7269eSachartre 
522487a7269eSachartre 	}
522587a7269eSachartre 
522678fcd0a1Sachartre 	mutex_exit(&vdc->lock);
522778fcd0a1Sachartre 
522887a7269eSachartre 	if (ddi_copyout(&data, arg, size, flag) != 0)
522987a7269eSachartre 		return (EFAULT);
523087a7269eSachartre 
523187a7269eSachartre 	return (0);
523287a7269eSachartre }
523387a7269eSachartre 
523487a7269eSachartre /*
523587a7269eSachartre  * Function:
52369642afceSachartre  * 	vdc_dkio_partition()
52379642afceSachartre  *
52389642afceSachartre  * Description:
52399642afceSachartre  *	This function implements the DKIOCPARTITION ioctl.
52409642afceSachartre  *
52419642afceSachartre  * Arguments:
52429642afceSachartre  *	vdc	- soft state pointer
52439642afceSachartre  *	arg	- a pointer to a struct partition64 structure
52449642afceSachartre  *	flag	- ioctl flags
52459642afceSachartre  */
52469642afceSachartre static int
52479642afceSachartre vdc_dkio_partition(vdc_t *vdc, caddr_t arg, int flag)
52489642afceSachartre {
52499642afceSachartre 	struct partition64 p64;
52509642afceSachartre 	efi_gpt_t *gpt;
52519642afceSachartre 	efi_gpe_t *gpe;
52529642afceSachartre 	vd_efi_dev_t edev;
52539642afceSachartre 	uint_t partno;
52549642afceSachartre 	int rv;
52559642afceSachartre 
52569642afceSachartre 	if (ddi_copyin(arg, &p64, sizeof (struct partition64), flag)) {
52579642afceSachartre 		return (EFAULT);
52589642afceSachartre 	}
52599642afceSachartre 
52609642afceSachartre 	VD_EFI_DEV_SET(edev, vdc, vd_process_efi_ioctl);
52619642afceSachartre 
52629642afceSachartre 	if ((rv = vd_efi_alloc_and_read(&edev, &gpt, &gpe)) != 0) {
52639642afceSachartre 		return (rv);
52649642afceSachartre 	}
52659642afceSachartre 
52669642afceSachartre 	partno = p64.p_partno;
52679642afceSachartre 
52689642afceSachartre 	if (partno >= gpt->efi_gpt_NumberOfPartitionEntries) {
52699642afceSachartre 		vd_efi_free(&edev, gpt, gpe);
52709642afceSachartre 		return (ESRCH);
52719642afceSachartre 	}
52729642afceSachartre 
52739642afceSachartre 	bcopy(&gpe[partno].efi_gpe_PartitionTypeGUID, &p64.p_type,
52749642afceSachartre 	    sizeof (struct uuid));
52759642afceSachartre 	p64.p_start = gpe[partno].efi_gpe_StartingLBA;
52769642afceSachartre 	p64.p_size = gpe[partno].efi_gpe_EndingLBA - p64.p_start + 1;
52779642afceSachartre 
52789642afceSachartre 	if (ddi_copyout(&p64, arg, sizeof (struct partition64), flag)) {
52799642afceSachartre 		vd_efi_free(&edev, gpt, gpe);
52809642afceSachartre 		return (EFAULT);
52819642afceSachartre 	}
52829642afceSachartre 
52839642afceSachartre 	vd_efi_free(&edev, gpt, gpe);
52849642afceSachartre 	return (0);
52859642afceSachartre }
52869642afceSachartre 
52879642afceSachartre /*
52889642afceSachartre  * Function:
528987a7269eSachartre  * 	vdc_dioctl_rwcmd()
529087a7269eSachartre  *
529187a7269eSachartre  * Description:
529287a7269eSachartre  *	This function implements the DIOCTL_RWCMD ioctl. This ioctl is used
529387a7269eSachartre  *	for DKC_DIRECT disks to read or write at an absolute disk offset.
529487a7269eSachartre  *
529587a7269eSachartre  * Arguments:
529687a7269eSachartre  *	dev	- device
529787a7269eSachartre  *	arg	- a pointer to a dadkio_rwcmd or dadkio_rwcmd32 structure
529887a7269eSachartre  *	flag	- ioctl flags
529987a7269eSachartre  */
530087a7269eSachartre static int
530187a7269eSachartre vdc_dioctl_rwcmd(dev_t dev, caddr_t arg, int flag)
530287a7269eSachartre {
530387a7269eSachartre 	struct dadkio_rwcmd32 rwcmd32;
530487a7269eSachartre 	struct dadkio_rwcmd rwcmd;
530587a7269eSachartre 	struct iovec aiov;
530687a7269eSachartre 	struct uio auio;
530787a7269eSachartre 	int rw, status;
530887a7269eSachartre 	struct buf *buf;
530987a7269eSachartre 
531087a7269eSachartre 	if (ddi_model_convert_from(flag & FMODELS) == DDI_MODEL_ILP32) {
531187a7269eSachartre 		if (ddi_copyin((caddr_t)arg, (caddr_t)&rwcmd32,
531287a7269eSachartre 		    sizeof (struct dadkio_rwcmd32), flag)) {
531387a7269eSachartre 			return (EFAULT);
531487a7269eSachartre 		}
531587a7269eSachartre 		rwcmd.cmd = rwcmd32.cmd;
531687a7269eSachartre 		rwcmd.flags = rwcmd32.flags;
531787a7269eSachartre 		rwcmd.blkaddr = (daddr_t)rwcmd32.blkaddr;
531887a7269eSachartre 		rwcmd.buflen = rwcmd32.buflen;
531987a7269eSachartre 		rwcmd.bufaddr = (caddr_t)(uintptr_t)rwcmd32.bufaddr;
532087a7269eSachartre 	} else {
532187a7269eSachartre 		if (ddi_copyin((caddr_t)arg, (caddr_t)&rwcmd,
532287a7269eSachartre 		    sizeof (struct dadkio_rwcmd), flag)) {
532387a7269eSachartre 			return (EFAULT);
532487a7269eSachartre 		}
532587a7269eSachartre 	}
532687a7269eSachartre 
532787a7269eSachartre 	switch (rwcmd.cmd) {
532887a7269eSachartre 	case DADKIO_RWCMD_READ:
532987a7269eSachartre 		rw = B_READ;
533087a7269eSachartre 		break;
533187a7269eSachartre 	case DADKIO_RWCMD_WRITE:
533287a7269eSachartre 		rw = B_WRITE;
533387a7269eSachartre 		break;
533487a7269eSachartre 	default:
533587a7269eSachartre 		return (EINVAL);
533687a7269eSachartre 	}
533787a7269eSachartre 
533887a7269eSachartre 	bzero((caddr_t)&aiov, sizeof (struct iovec));
533987a7269eSachartre 	aiov.iov_base   = rwcmd.bufaddr;
534087a7269eSachartre 	aiov.iov_len    = rwcmd.buflen;
534187a7269eSachartre 
534287a7269eSachartre 	bzero((caddr_t)&auio, sizeof (struct uio));
534387a7269eSachartre 	auio.uio_iov    = &aiov;
534487a7269eSachartre 	auio.uio_iovcnt = 1;
534587a7269eSachartre 	auio.uio_loffset = rwcmd.blkaddr * DEV_BSIZE;
534687a7269eSachartre 	auio.uio_resid  = rwcmd.buflen;
534787a7269eSachartre 	auio.uio_segflg = flag & FKIOCTL ? UIO_SYSSPACE : UIO_USERSPACE;
534887a7269eSachartre 
534987a7269eSachartre 	buf = kmem_alloc(sizeof (buf_t), KM_SLEEP);
535087a7269eSachartre 	bioinit(buf);
535187a7269eSachartre 	/*
535287a7269eSachartre 	 * We use the private field of buf to specify that this is an
535387a7269eSachartre 	 * I/O using an absolute offset.
535487a7269eSachartre 	 */
535587a7269eSachartre 	buf->b_private = (void *)VD_SLICE_NONE;
535687a7269eSachartre 
535787a7269eSachartre 	status = physio(vdc_strategy, buf, dev, rw, vdc_min, &auio);
535887a7269eSachartre 
535987a7269eSachartre 	biofini(buf);
536087a7269eSachartre 	kmem_free(buf, sizeof (buf_t));
536187a7269eSachartre 
536287a7269eSachartre 	return (status);
536387a7269eSachartre }
536487a7269eSachartre 
536587a7269eSachartre /*
53662f5224aeSachartre  * Allocate a buffer for a VD_OP_SCSICMD operation. The size of the allocated
53672f5224aeSachartre  * buffer is returned in alloc_len.
53682f5224aeSachartre  */
53692f5224aeSachartre static vd_scsi_t *
53702f5224aeSachartre vdc_scsi_alloc(int cdb_len, int sense_len, int datain_len, int dataout_len,
53712f5224aeSachartre     int *alloc_len)
53722f5224aeSachartre {
53732f5224aeSachartre 	vd_scsi_t *vd_scsi;
53742f5224aeSachartre 	int vd_scsi_len = VD_SCSI_SIZE;
53752f5224aeSachartre 
53762f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(cdb_len, sizeof (uint64_t));
53772f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(sense_len, sizeof (uint64_t));
53782f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(datain_len, sizeof (uint64_t));
53792f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(dataout_len, sizeof (uint64_t));
53802f5224aeSachartre 
53812f5224aeSachartre 	ASSERT(vd_scsi_len % sizeof (uint64_t) == 0);
53822f5224aeSachartre 
53832f5224aeSachartre 	vd_scsi = kmem_zalloc(vd_scsi_len, KM_SLEEP);
53842f5224aeSachartre 
53852f5224aeSachartre 	vd_scsi->cdb_len = cdb_len;
53862f5224aeSachartre 	vd_scsi->sense_len = sense_len;
53872f5224aeSachartre 	vd_scsi->datain_len = datain_len;
53882f5224aeSachartre 	vd_scsi->dataout_len = dataout_len;
53892f5224aeSachartre 
53902f5224aeSachartre 	*alloc_len = vd_scsi_len;
53912f5224aeSachartre 
53922f5224aeSachartre 	return (vd_scsi);
53932f5224aeSachartre }
53942f5224aeSachartre 
53952f5224aeSachartre /*
53962f5224aeSachartre  * Convert the status of a SCSI command to a Solaris return code.
53972f5224aeSachartre  *
53982f5224aeSachartre  * Arguments:
53992f5224aeSachartre  *	vd_scsi		- The SCSI operation buffer.
54002f5224aeSachartre  *	log_error	- indicate if an error message should be logged.
54012f5224aeSachartre  *
54022f5224aeSachartre  * Note that our SCSI error messages are rather primitive for the moment
54032f5224aeSachartre  * and could be improved by decoding some data like the SCSI command and
54042f5224aeSachartre  * the sense key.
54052f5224aeSachartre  *
54062f5224aeSachartre  * Return value:
54072f5224aeSachartre  *	0		- Status is good.
54082f5224aeSachartre  *	EACCES		- Status reports a reservation conflict.
54092f5224aeSachartre  *	ENOTSUP		- Status reports a check condition and sense key
54102f5224aeSachartre  *			  reports an illegal request.
54112f5224aeSachartre  *	EIO		- Any other status.
54122f5224aeSachartre  */
54132f5224aeSachartre static int
54142f5224aeSachartre vdc_scsi_status(vdc_t *vdc, vd_scsi_t *vd_scsi, boolean_t log_error)
54152f5224aeSachartre {
54162f5224aeSachartre 	int rv;
54172f5224aeSachartre 	char path_str[MAXPATHLEN];
54182f5224aeSachartre 	char panic_str[VDC_RESV_CONFLICT_FMT_LEN + MAXPATHLEN];
54192f5224aeSachartre 	union scsi_cdb *cdb;
54202f5224aeSachartre 	struct scsi_extended_sense *sense;
54212f5224aeSachartre 
54222f5224aeSachartre 	if (vd_scsi->cmd_status == STATUS_GOOD)
54232f5224aeSachartre 		/* no error */
54242f5224aeSachartre 		return (0);
54252f5224aeSachartre 
54262f5224aeSachartre 	/* when the tunable vdc_scsi_log_error is true we log all errors */
54272f5224aeSachartre 	if (vdc_scsi_log_error)
54282f5224aeSachartre 		log_error = B_TRUE;
54292f5224aeSachartre 
54302f5224aeSachartre 	if (log_error) {
54312f5224aeSachartre 		cmn_err(CE_WARN, "%s (vdc%d):\tError for Command: 0x%x)\n",
54322f5224aeSachartre 		    ddi_pathname(vdc->dip, path_str), vdc->instance,
54332f5224aeSachartre 		    GETCMD(VD_SCSI_DATA_CDB(vd_scsi)));
54342f5224aeSachartre 	}
54352f5224aeSachartre 
54362f5224aeSachartre 	/* default returned value */
54372f5224aeSachartre 	rv = EIO;
54382f5224aeSachartre 
54392f5224aeSachartre 	switch (vd_scsi->cmd_status) {
54402f5224aeSachartre 
54412f5224aeSachartre 	case STATUS_CHECK:
54422f5224aeSachartre 	case STATUS_TERMINATED:
54432f5224aeSachartre 		if (log_error)
54442f5224aeSachartre 			cmn_err(CE_CONT, "\tCheck Condition Error\n");
54452f5224aeSachartre 
54462f5224aeSachartre 		/* check sense buffer */
54472f5224aeSachartre 		if (vd_scsi->sense_len == 0 ||
54482f5224aeSachartre 		    vd_scsi->sense_status != STATUS_GOOD) {
54492f5224aeSachartre 			if (log_error)
54502f5224aeSachartre 				cmn_err(CE_CONT, "\tNo Sense Data Available\n");
54512f5224aeSachartre 			break;
54522f5224aeSachartre 		}
54532f5224aeSachartre 
54542f5224aeSachartre 		sense = VD_SCSI_DATA_SENSE(vd_scsi);
54552f5224aeSachartre 
54562f5224aeSachartre 		if (log_error) {
54572f5224aeSachartre 			cmn_err(CE_CONT, "\tSense Key:  0x%x\n"
54582f5224aeSachartre 			    "\tASC: 0x%x, ASCQ: 0x%x\n",
54592f5224aeSachartre 			    scsi_sense_key((uint8_t *)sense),
54602f5224aeSachartre 			    scsi_sense_asc((uint8_t *)sense),
54612f5224aeSachartre 			    scsi_sense_ascq((uint8_t *)sense));
54622f5224aeSachartre 		}
54632f5224aeSachartre 
54642f5224aeSachartre 		if (scsi_sense_key((uint8_t *)sense) == KEY_ILLEGAL_REQUEST)
54652f5224aeSachartre 			rv = ENOTSUP;
54662f5224aeSachartre 		break;
54672f5224aeSachartre 
54682f5224aeSachartre 	case STATUS_BUSY:
54692f5224aeSachartre 		if (log_error)
54702f5224aeSachartre 			cmn_err(CE_NOTE, "\tDevice Busy\n");
54712f5224aeSachartre 		break;
54722f5224aeSachartre 
54732f5224aeSachartre 	case STATUS_RESERVATION_CONFLICT:
54742f5224aeSachartre 		/*
54752f5224aeSachartre 		 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then
54762f5224aeSachartre 		 * reservation conflict could be due to various reasons like
54772f5224aeSachartre 		 * incorrect keys, not registered or not reserved etc. So,
54782f5224aeSachartre 		 * we should not panic in that case.
54792f5224aeSachartre 		 */
54802f5224aeSachartre 		cdb = VD_SCSI_DATA_CDB(vd_scsi);
54812f5224aeSachartre 		if (vdc->failfast_interval != 0 &&
54822f5224aeSachartre 		    cdb->scc_cmd != SCMD_PERSISTENT_RESERVE_IN &&
54832f5224aeSachartre 		    cdb->scc_cmd != SCMD_PERSISTENT_RESERVE_OUT) {
54842f5224aeSachartre 			/* failfast is enabled so we have to panic */
54852f5224aeSachartre 			(void) snprintf(panic_str, sizeof (panic_str),
54862f5224aeSachartre 			    VDC_RESV_CONFLICT_FMT_STR "%s",
54872f5224aeSachartre 			    ddi_pathname(vdc->dip, path_str));
54882f5224aeSachartre 			panic(panic_str);
54892f5224aeSachartre 		}
54902f5224aeSachartre 		if (log_error)
54912f5224aeSachartre 			cmn_err(CE_NOTE, "\tReservation Conflict\n");
54922f5224aeSachartre 		rv = EACCES;
54932f5224aeSachartre 		break;
54942f5224aeSachartre 
54952f5224aeSachartre 	case STATUS_QFULL:
54962f5224aeSachartre 		if (log_error)
54972f5224aeSachartre 			cmn_err(CE_NOTE, "\tQueue Full\n");
54982f5224aeSachartre 		break;
54992f5224aeSachartre 
55002f5224aeSachartre 	case STATUS_MET:
55012f5224aeSachartre 	case STATUS_INTERMEDIATE:
55022f5224aeSachartre 	case STATUS_SCSI2:
55032f5224aeSachartre 	case STATUS_INTERMEDIATE_MET:
55042f5224aeSachartre 	case STATUS_ACA_ACTIVE:
55052f5224aeSachartre 		if (log_error)
55062f5224aeSachartre 			cmn_err(CE_CONT,
55072f5224aeSachartre 			    "\tUnexpected SCSI status received: 0x%x\n",
55082f5224aeSachartre 			    vd_scsi->cmd_status);
55092f5224aeSachartre 		break;
55102f5224aeSachartre 
55112f5224aeSachartre 	default:
55122f5224aeSachartre 		if (log_error)
55132f5224aeSachartre 			cmn_err(CE_CONT,
55142f5224aeSachartre 			    "\tInvalid SCSI status received: 0x%x\n",
55152f5224aeSachartre 			    vd_scsi->cmd_status);
55162f5224aeSachartre 		break;
55172f5224aeSachartre 	}
55182f5224aeSachartre 
55192f5224aeSachartre 	return (rv);
55202f5224aeSachartre }
55212f5224aeSachartre 
55222f5224aeSachartre /*
55232f5224aeSachartre  * Implemented the USCSICMD uscsi(7I) ioctl. This ioctl is converted to
55242f5224aeSachartre  * a VD_OP_SCSICMD operation which is sent to the vdisk server. If a SCSI
55252f5224aeSachartre  * reset is requested (i.e. a flag USCSI_RESET* is set) then the ioctl is
55262f5224aeSachartre  * converted to a VD_OP_RESET operation.
55272f5224aeSachartre  */
55282f5224aeSachartre static int
55292f5224aeSachartre vdc_uscsi_cmd(vdc_t *vdc, caddr_t arg, int mode)
55302f5224aeSachartre {
55312f5224aeSachartre 	struct uscsi_cmd 	uscsi;
55322f5224aeSachartre 	struct uscsi_cmd32	uscsi32;
55332f5224aeSachartre 	vd_scsi_t 		*vd_scsi;
55342f5224aeSachartre 	int 			vd_scsi_len;
55352f5224aeSachartre 	union scsi_cdb		*cdb;
55362f5224aeSachartre 	struct scsi_extended_sense *sense;
55372f5224aeSachartre 	char 			*datain, *dataout;
55382f5224aeSachartre 	size_t			cdb_len, datain_len, dataout_len, sense_len;
55392f5224aeSachartre 	int 			rv;
55402f5224aeSachartre 
55412f5224aeSachartre 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
55422f5224aeSachartre 		if (ddi_copyin(arg, &uscsi32, sizeof (struct uscsi_cmd32),
55432f5224aeSachartre 		    mode) != 0)
55442f5224aeSachartre 			return (EFAULT);
55452f5224aeSachartre 		uscsi_cmd32touscsi_cmd((&uscsi32), (&uscsi));
55462f5224aeSachartre 	} else {
55472f5224aeSachartre 		if (ddi_copyin(arg, &uscsi, sizeof (struct uscsi_cmd),
55482f5224aeSachartre 		    mode) != 0)
55492f5224aeSachartre 			return (EFAULT);
55502f5224aeSachartre 	}
55512f5224aeSachartre 
55522f5224aeSachartre 	/* a uscsi reset is converted to a VD_OP_RESET operation */
55532f5224aeSachartre 	if (uscsi.uscsi_flags & (USCSI_RESET | USCSI_RESET_LUN |
55542f5224aeSachartre 	    USCSI_RESET_ALL)) {
55552f5224aeSachartre 		rv = vdc_do_sync_op(vdc, VD_OP_RESET, NULL, 0, 0, 0, CB_SYNC,
55562f5224aeSachartre 		    (void *)(uint64_t)mode, VIO_both_dir, B_TRUE);
55572f5224aeSachartre 		return (rv);
55582f5224aeSachartre 	}
55592f5224aeSachartre 
55602f5224aeSachartre 	/* cdb buffer length */
55612f5224aeSachartre 	cdb_len = uscsi.uscsi_cdblen;
55622f5224aeSachartre 
55632f5224aeSachartre 	/* data in and out buffers length */
55642f5224aeSachartre 	if (uscsi.uscsi_flags & USCSI_READ) {
55652f5224aeSachartre 		datain_len = uscsi.uscsi_buflen;
55662f5224aeSachartre 		dataout_len = 0;
55672f5224aeSachartre 	} else {
55682f5224aeSachartre 		datain_len = 0;
55692f5224aeSachartre 		dataout_len = uscsi.uscsi_buflen;
55702f5224aeSachartre 	}
55712f5224aeSachartre 
55722f5224aeSachartre 	/* sense buffer length */
55732f5224aeSachartre 	if (uscsi.uscsi_flags & USCSI_RQENABLE)
55742f5224aeSachartre 		sense_len = uscsi.uscsi_rqlen;
55752f5224aeSachartre 	else
55762f5224aeSachartre 		sense_len = 0;
55772f5224aeSachartre 
55782f5224aeSachartre 	/* allocate buffer for the VD_SCSICMD_OP operation */
55792f5224aeSachartre 	vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, datain_len, dataout_len,
55802f5224aeSachartre 	    &vd_scsi_len);
55812f5224aeSachartre 
55822f5224aeSachartre 	/*
55832f5224aeSachartre 	 * The documentation of USCSI_ISOLATE and USCSI_DIAGNOSE is very vague,
55842f5224aeSachartre 	 * but basically they prevent a SCSI command from being retried in case
55852f5224aeSachartre 	 * of an error.
55862f5224aeSachartre 	 */
55872f5224aeSachartre 	if ((uscsi.uscsi_flags & USCSI_ISOLATE) ||
55882f5224aeSachartre 	    (uscsi.uscsi_flags & USCSI_DIAGNOSE))
55892f5224aeSachartre 		vd_scsi->options |= VD_SCSI_OPT_NORETRY;
55902f5224aeSachartre 
55912f5224aeSachartre 	/* set task attribute */
55922f5224aeSachartre 	if (uscsi.uscsi_flags & USCSI_NOTAG) {
55932f5224aeSachartre 		vd_scsi->task_attribute = 0;
55942f5224aeSachartre 	} else {
55952f5224aeSachartre 		if (uscsi.uscsi_flags & USCSI_HEAD)
55962f5224aeSachartre 			vd_scsi->task_attribute = VD_SCSI_TASK_ACA;
55972f5224aeSachartre 		else if (uscsi.uscsi_flags & USCSI_HTAG)
55982f5224aeSachartre 			vd_scsi->task_attribute = VD_SCSI_TASK_HQUEUE;
55992f5224aeSachartre 		else if (uscsi.uscsi_flags & USCSI_OTAG)
56002f5224aeSachartre 			vd_scsi->task_attribute = VD_SCSI_TASK_ORDERED;
56012f5224aeSachartre 		else
56022f5224aeSachartre 			vd_scsi->task_attribute = 0;
56032f5224aeSachartre 	}
56042f5224aeSachartre 
56052f5224aeSachartre 	/* set timeout */
56062f5224aeSachartre 	vd_scsi->timeout = uscsi.uscsi_timeout;
56072f5224aeSachartre 
56082f5224aeSachartre 	/* copy-in cdb data */
56092f5224aeSachartre 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
56102f5224aeSachartre 	if (ddi_copyin(uscsi.uscsi_cdb, cdb, cdb_len, mode) != 0) {
56112f5224aeSachartre 		rv = EFAULT;
56122f5224aeSachartre 		goto done;
56132f5224aeSachartre 	}
56142f5224aeSachartre 
56152f5224aeSachartre 	/* keep a pointer to the sense buffer */
56162f5224aeSachartre 	sense = VD_SCSI_DATA_SENSE(vd_scsi);
56172f5224aeSachartre 
56182f5224aeSachartre 	/* keep a pointer to the data-in buffer */
56192f5224aeSachartre 	datain = (char *)VD_SCSI_DATA_IN(vd_scsi);
56202f5224aeSachartre 
56212f5224aeSachartre 	/* copy-in request data to the data-out buffer */
56222f5224aeSachartre 	dataout = (char *)VD_SCSI_DATA_OUT(vd_scsi);
56232f5224aeSachartre 	if (!(uscsi.uscsi_flags & USCSI_READ)) {
56242f5224aeSachartre 		if (ddi_copyin(uscsi.uscsi_bufaddr, dataout, dataout_len,
56252f5224aeSachartre 		    mode)) {
56262f5224aeSachartre 			rv = EFAULT;
56272f5224aeSachartre 			goto done;
56282f5224aeSachartre 		}
56292f5224aeSachartre 	}
56302f5224aeSachartre 
56312f5224aeSachartre 	/* submit the request */
56322f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
56332f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
56342f5224aeSachartre 
56352f5224aeSachartre 	if (rv != 0)
56362f5224aeSachartre 		goto done;
56372f5224aeSachartre 
56382f5224aeSachartre 	/* update scsi status */
56392f5224aeSachartre 	uscsi.uscsi_status = vd_scsi->cmd_status;
56402f5224aeSachartre 
56412f5224aeSachartre 	/* update sense data */
56422f5224aeSachartre 	if ((uscsi.uscsi_flags & USCSI_RQENABLE) &&
56432f5224aeSachartre 	    (uscsi.uscsi_status == STATUS_CHECK ||
56442f5224aeSachartre 	    uscsi.uscsi_status == STATUS_TERMINATED)) {
56452f5224aeSachartre 
56462f5224aeSachartre 		uscsi.uscsi_rqstatus = vd_scsi->sense_status;
56472f5224aeSachartre 
56482f5224aeSachartre 		if (uscsi.uscsi_rqstatus == STATUS_GOOD) {
56492f5224aeSachartre 			uscsi.uscsi_rqresid = uscsi.uscsi_rqlen -
56502f5224aeSachartre 			    vd_scsi->sense_len;
56512f5224aeSachartre 			if (ddi_copyout(sense, uscsi.uscsi_rqbuf,
56522f5224aeSachartre 			    vd_scsi->sense_len, mode) != 0) {
56532f5224aeSachartre 				rv = EFAULT;
56542f5224aeSachartre 				goto done;
56552f5224aeSachartre 			}
56562f5224aeSachartre 		}
56572f5224aeSachartre 	}
56582f5224aeSachartre 
56592f5224aeSachartre 	/* update request data */
56602f5224aeSachartre 	if (uscsi.uscsi_status == STATUS_GOOD) {
56612f5224aeSachartre 		if (uscsi.uscsi_flags & USCSI_READ) {
56622f5224aeSachartre 			uscsi.uscsi_resid = uscsi.uscsi_buflen -
56632f5224aeSachartre 			    vd_scsi->datain_len;
56642f5224aeSachartre 			if (ddi_copyout(datain, uscsi.uscsi_bufaddr,
56652f5224aeSachartre 			    vd_scsi->datain_len, mode) != 0) {
56662f5224aeSachartre 				rv = EFAULT;
56672f5224aeSachartre 				goto done;
56682f5224aeSachartre 			}
56692f5224aeSachartre 		} else {
56702f5224aeSachartre 			uscsi.uscsi_resid = uscsi.uscsi_buflen -
56712f5224aeSachartre 			    vd_scsi->dataout_len;
56722f5224aeSachartre 		}
56732f5224aeSachartre 	}
56742f5224aeSachartre 
56752f5224aeSachartre 	/* copy-out result */
56762f5224aeSachartre 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
56772f5224aeSachartre 		uscsi_cmdtouscsi_cmd32((&uscsi), (&uscsi32));
56782f5224aeSachartre 		if (ddi_copyout(&uscsi32, arg, sizeof (struct uscsi_cmd32),
56792f5224aeSachartre 		    mode) != 0) {
56802f5224aeSachartre 			rv = EFAULT;
56812f5224aeSachartre 			goto done;
56822f5224aeSachartre 		}
56832f5224aeSachartre 	} else {
56842f5224aeSachartre 		if (ddi_copyout(&uscsi, arg, sizeof (struct uscsi_cmd),
56852f5224aeSachartre 		    mode) != 0) {
56862f5224aeSachartre 			rv = EFAULT;
56872f5224aeSachartre 			goto done;
56882f5224aeSachartre 		}
56892f5224aeSachartre 	}
56902f5224aeSachartre 
56912f5224aeSachartre 	/* get the return code from the SCSI command status */
56922f5224aeSachartre 	rv = vdc_scsi_status(vdc, vd_scsi,
56932f5224aeSachartre 	    !(uscsi.uscsi_flags & USCSI_SILENT));
56942f5224aeSachartre 
56952f5224aeSachartre done:
56962f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
56972f5224aeSachartre 	return (rv);
56982f5224aeSachartre }
56992f5224aeSachartre 
57002f5224aeSachartre /*
57012f5224aeSachartre  * Create a VD_OP_SCSICMD buffer for a SCSI PERSISTENT IN command.
57022f5224aeSachartre  *
57032f5224aeSachartre  * Arguments:
57042f5224aeSachartre  *	cmd		- SCSI PERSISTENT IN command
57052f5224aeSachartre  *	len		- length of the SCSI input buffer
57062f5224aeSachartre  *	vd_scsi_len	- return the length of the allocated buffer
57072f5224aeSachartre  *
57082f5224aeSachartre  * Returned Value:
57092f5224aeSachartre  *	a pointer to the allocated VD_OP_SCSICMD buffer.
57102f5224aeSachartre  */
57112f5224aeSachartre static vd_scsi_t *
57122f5224aeSachartre vdc_scsi_alloc_persistent_in(uchar_t cmd, int len, int *vd_scsi_len)
57132f5224aeSachartre {
57142f5224aeSachartre 	int cdb_len, sense_len, datain_len, dataout_len;
57152f5224aeSachartre 	vd_scsi_t *vd_scsi;
57162f5224aeSachartre 	union scsi_cdb *cdb;
57172f5224aeSachartre 
57182f5224aeSachartre 	cdb_len = CDB_GROUP1;
57192f5224aeSachartre 	sense_len = sizeof (struct scsi_extended_sense);
57202f5224aeSachartre 	datain_len = len;
57212f5224aeSachartre 	dataout_len = 0;
57222f5224aeSachartre 
57232f5224aeSachartre 	vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, datain_len, dataout_len,
57242f5224aeSachartre 	    vd_scsi_len);
57252f5224aeSachartre 
57262f5224aeSachartre 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
57272f5224aeSachartre 
57282f5224aeSachartre 	/* set cdb */
57292f5224aeSachartre 	cdb->scc_cmd = SCMD_PERSISTENT_RESERVE_IN;
57302f5224aeSachartre 	cdb->cdb_opaque[1] = cmd;
57312f5224aeSachartre 	FORMG1COUNT(cdb, datain_len);
57322f5224aeSachartre 
57332f5224aeSachartre 	vd_scsi->timeout = vdc_scsi_timeout;
57342f5224aeSachartre 
57352f5224aeSachartre 	return (vd_scsi);
57362f5224aeSachartre }
57372f5224aeSachartre 
57382f5224aeSachartre /*
57392f5224aeSachartre  * Create a VD_OP_SCSICMD buffer for a SCSI PERSISTENT OUT command.
57402f5224aeSachartre  *
57412f5224aeSachartre  * Arguments:
57422f5224aeSachartre  *	cmd		- SCSI PERSISTENT OUT command
57432f5224aeSachartre  *	len		- length of the SCSI output buffer
57442f5224aeSachartre  *	vd_scsi_len	- return the length of the allocated buffer
57452f5224aeSachartre  *
57462f5224aeSachartre  * Returned Code:
57472f5224aeSachartre  *	a pointer to the allocated VD_OP_SCSICMD buffer.
57482f5224aeSachartre  */
57492f5224aeSachartre static vd_scsi_t *
57502f5224aeSachartre vdc_scsi_alloc_persistent_out(uchar_t cmd, int len, int *vd_scsi_len)
57512f5224aeSachartre {
57522f5224aeSachartre 	int cdb_len, sense_len, datain_len, dataout_len;
57532f5224aeSachartre 	vd_scsi_t *vd_scsi;
57542f5224aeSachartre 	union scsi_cdb *cdb;
57552f5224aeSachartre 
57562f5224aeSachartre 	cdb_len = CDB_GROUP1;
57572f5224aeSachartre 	sense_len = sizeof (struct scsi_extended_sense);
57582f5224aeSachartre 	datain_len = 0;
57592f5224aeSachartre 	dataout_len = len;
57602f5224aeSachartre 
57612f5224aeSachartre 	vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, datain_len, dataout_len,
57622f5224aeSachartre 	    vd_scsi_len);
57632f5224aeSachartre 
57642f5224aeSachartre 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
57652f5224aeSachartre 
57662f5224aeSachartre 	/* set cdb */
57672f5224aeSachartre 	cdb->scc_cmd = SCMD_PERSISTENT_RESERVE_OUT;
57682f5224aeSachartre 	cdb->cdb_opaque[1] = cmd;
57692f5224aeSachartre 	FORMG1COUNT(cdb, dataout_len);
57702f5224aeSachartre 
57712f5224aeSachartre 	vd_scsi->timeout = vdc_scsi_timeout;
57722f5224aeSachartre 
57732f5224aeSachartre 	return (vd_scsi);
57742f5224aeSachartre }
57752f5224aeSachartre 
57762f5224aeSachartre /*
57772f5224aeSachartre  * Implement the MHIOCGRP_INKEYS mhd(7i) ioctl. The ioctl is converted
57782f5224aeSachartre  * to a SCSI PERSISTENT IN READ KEYS command which is sent to the vdisk
57792f5224aeSachartre  * server with a VD_OP_SCSICMD operation.
57802f5224aeSachartre  */
57812f5224aeSachartre static int
57822f5224aeSachartre vdc_mhd_inkeys(vdc_t *vdc, caddr_t arg, int mode)
57832f5224aeSachartre {
57842f5224aeSachartre 	vd_scsi_t *vd_scsi;
57852f5224aeSachartre 	mhioc_inkeys_t inkeys;
57862f5224aeSachartre 	mhioc_key_list_t klist;
57872f5224aeSachartre 	struct mhioc_inkeys32 inkeys32;
57882f5224aeSachartre 	struct mhioc_key_list32 klist32;
57892f5224aeSachartre 	sd_prin_readkeys_t *scsi_keys;
57902f5224aeSachartre 	void *user_keys;
57912f5224aeSachartre 	int vd_scsi_len;
57922f5224aeSachartre 	int listsize, listlen, rv;
57932f5224aeSachartre 
57942f5224aeSachartre 	/* copyin arguments */
57952f5224aeSachartre 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
57962f5224aeSachartre 		rv = ddi_copyin(arg, &inkeys32, sizeof (inkeys32), mode);
57972f5224aeSachartre 		if (rv != 0)
57982f5224aeSachartre 			return (EFAULT);
57992f5224aeSachartre 
58002f5224aeSachartre 		rv = ddi_copyin((caddr_t)(uintptr_t)inkeys32.li, &klist32,
58012f5224aeSachartre 		    sizeof (klist32), mode);
58022f5224aeSachartre 		if (rv != 0)
58032f5224aeSachartre 			return (EFAULT);
58042f5224aeSachartre 
58052f5224aeSachartre 		listsize = klist32.listsize;
58062f5224aeSachartre 	} else {
58072f5224aeSachartre 		rv = ddi_copyin(arg, &inkeys, sizeof (inkeys), mode);
58082f5224aeSachartre 		if (rv != 0)
58092f5224aeSachartre 			return (EFAULT);
58102f5224aeSachartre 
58112f5224aeSachartre 		rv = ddi_copyin(inkeys.li, &klist, sizeof (klist), mode);
58122f5224aeSachartre 		if (rv != 0)
58132f5224aeSachartre 			return (EFAULT);
58142f5224aeSachartre 
58152f5224aeSachartre 		listsize = klist.listsize;
58162f5224aeSachartre 	}
58172f5224aeSachartre 
58182f5224aeSachartre 	/* build SCSI VD_OP request */
58192f5224aeSachartre 	vd_scsi = vdc_scsi_alloc_persistent_in(SD_READ_KEYS,
58202f5224aeSachartre 	    sizeof (sd_prin_readkeys_t) - sizeof (caddr_t) +
58212f5224aeSachartre 	    (sizeof (mhioc_resv_key_t) * listsize), &vd_scsi_len);
58222f5224aeSachartre 
58232f5224aeSachartre 	scsi_keys = (sd_prin_readkeys_t *)VD_SCSI_DATA_IN(vd_scsi);
58242f5224aeSachartre 
58252f5224aeSachartre 	/* submit the request */
58262f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
58272f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
58282f5224aeSachartre 
58292f5224aeSachartre 	if (rv != 0)
58302f5224aeSachartre 		goto done;
58312f5224aeSachartre 
58322f5224aeSachartre 	listlen = scsi_keys->len / MHIOC_RESV_KEY_SIZE;
58332f5224aeSachartre 
58342f5224aeSachartre 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
58352f5224aeSachartre 		inkeys32.generation = scsi_keys->generation;
58362f5224aeSachartre 		rv = ddi_copyout(&inkeys32, arg, sizeof (inkeys32), mode);
58372f5224aeSachartre 		if (rv != 0) {
58382f5224aeSachartre 			rv = EFAULT;
58392f5224aeSachartre 			goto done;
58402f5224aeSachartre 		}
58412f5224aeSachartre 
58422f5224aeSachartre 		klist32.listlen = listlen;
58432f5224aeSachartre 		rv = ddi_copyout(&klist32, (caddr_t)(uintptr_t)inkeys32.li,
58442f5224aeSachartre 		    sizeof (klist32), mode);
58452f5224aeSachartre 		if (rv != 0) {
58462f5224aeSachartre 			rv = EFAULT;
58472f5224aeSachartre 			goto done;
58482f5224aeSachartre 		}
58492f5224aeSachartre 
58502f5224aeSachartre 		user_keys = (caddr_t)(uintptr_t)klist32.list;
58512f5224aeSachartre 	} else {
58522f5224aeSachartre 		inkeys.generation = scsi_keys->generation;
58532f5224aeSachartre 		rv = ddi_copyout(&inkeys, arg, sizeof (inkeys), mode);
58542f5224aeSachartre 		if (rv != 0) {
58552f5224aeSachartre 			rv = EFAULT;
58562f5224aeSachartre 			goto done;
58572f5224aeSachartre 		}
58582f5224aeSachartre 
58592f5224aeSachartre 		klist.listlen = listlen;
58602f5224aeSachartre 		rv = ddi_copyout(&klist, inkeys.li, sizeof (klist), mode);
58612f5224aeSachartre 		if (rv != 0) {
58622f5224aeSachartre 			rv = EFAULT;
58632f5224aeSachartre 			goto done;
58642f5224aeSachartre 		}
58652f5224aeSachartre 
58662f5224aeSachartre 		user_keys = klist.list;
58672f5224aeSachartre 	}
58682f5224aeSachartre 
58692f5224aeSachartre 	/* copy out keys */
58702f5224aeSachartre 	if (listlen > 0 && listsize > 0) {
58712f5224aeSachartre 		if (listsize < listlen)
58722f5224aeSachartre 			listlen = listsize;
58732f5224aeSachartre 		rv = ddi_copyout(&scsi_keys->keylist, user_keys,
58742f5224aeSachartre 		    listlen * MHIOC_RESV_KEY_SIZE, mode);
58752f5224aeSachartre 		if (rv != 0)
58762f5224aeSachartre 			rv = EFAULT;
58772f5224aeSachartre 	}
58782f5224aeSachartre 
58792f5224aeSachartre 	if (rv == 0)
58802f5224aeSachartre 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
58812f5224aeSachartre 
58822f5224aeSachartre done:
58832f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
58842f5224aeSachartre 
58852f5224aeSachartre 	return (rv);
58862f5224aeSachartre }
58872f5224aeSachartre 
58882f5224aeSachartre /*
58892f5224aeSachartre  * Implement the MHIOCGRP_INRESV mhd(7i) ioctl. The ioctl is converted
58902f5224aeSachartre  * to a SCSI PERSISTENT IN READ RESERVATION command which is sent to
58912f5224aeSachartre  * the vdisk server with a VD_OP_SCSICMD operation.
58922f5224aeSachartre  */
58932f5224aeSachartre static int
58942f5224aeSachartre vdc_mhd_inresv(vdc_t *vdc, caddr_t arg, int mode)
58952f5224aeSachartre {
58962f5224aeSachartre 	vd_scsi_t *vd_scsi;
58972f5224aeSachartre 	mhioc_inresvs_t inresv;
58982f5224aeSachartre 	mhioc_resv_desc_list_t rlist;
58992f5224aeSachartre 	struct mhioc_inresvs32 inresv32;
59002f5224aeSachartre 	struct mhioc_resv_desc_list32 rlist32;
59012f5224aeSachartre 	mhioc_resv_desc_t mhd_resv;
59022f5224aeSachartre 	sd_prin_readresv_t *scsi_resv;
59032f5224aeSachartre 	sd_readresv_desc_t *resv;
59042f5224aeSachartre 	mhioc_resv_desc_t *user_resv;
59052f5224aeSachartre 	int vd_scsi_len;
59062f5224aeSachartre 	int listsize, listlen, i, rv;
59072f5224aeSachartre 
59082f5224aeSachartre 	/* copyin arguments */
59092f5224aeSachartre 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
59102f5224aeSachartre 		rv = ddi_copyin(arg, &inresv32, sizeof (inresv32), mode);
59112f5224aeSachartre 		if (rv != 0)
59122f5224aeSachartre 			return (EFAULT);
59132f5224aeSachartre 
59142f5224aeSachartre 		rv = ddi_copyin((caddr_t)(uintptr_t)inresv32.li, &rlist32,
59152f5224aeSachartre 		    sizeof (rlist32), mode);
59162f5224aeSachartre 		if (rv != 0)
59172f5224aeSachartre 			return (EFAULT);
59182f5224aeSachartre 
59192f5224aeSachartre 		listsize = rlist32.listsize;
59202f5224aeSachartre 	} else {
59212f5224aeSachartre 		rv = ddi_copyin(arg, &inresv, sizeof (inresv), mode);
59222f5224aeSachartre 		if (rv != 0)
59232f5224aeSachartre 			return (EFAULT);
59242f5224aeSachartre 
59252f5224aeSachartre 		rv = ddi_copyin(inresv.li, &rlist, sizeof (rlist), mode);
59262f5224aeSachartre 		if (rv != 0)
59272f5224aeSachartre 			return (EFAULT);
59282f5224aeSachartre 
59292f5224aeSachartre 		listsize = rlist.listsize;
59302f5224aeSachartre 	}
59312f5224aeSachartre 
59322f5224aeSachartre 	/* build SCSI VD_OP request */
59332f5224aeSachartre 	vd_scsi = vdc_scsi_alloc_persistent_in(SD_READ_RESV,
59342f5224aeSachartre 	    sizeof (sd_prin_readresv_t) - sizeof (caddr_t) +
59352f5224aeSachartre 	    (SCSI3_RESV_DESC_LEN * listsize), &vd_scsi_len);
59362f5224aeSachartre 
59372f5224aeSachartre 	scsi_resv = (sd_prin_readresv_t *)VD_SCSI_DATA_IN(vd_scsi);
59382f5224aeSachartre 
59392f5224aeSachartre 	/* submit the request */
59402f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
59412f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
59422f5224aeSachartre 
59432f5224aeSachartre 	if (rv != 0)
59442f5224aeSachartre 		goto done;
59452f5224aeSachartre 
59462f5224aeSachartre 	listlen = scsi_resv->len / SCSI3_RESV_DESC_LEN;
59472f5224aeSachartre 
59482f5224aeSachartre 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
59492f5224aeSachartre 		inresv32.generation = scsi_resv->generation;
59502f5224aeSachartre 		rv = ddi_copyout(&inresv32, arg, sizeof (inresv32), mode);
59512f5224aeSachartre 		if (rv != 0) {
59522f5224aeSachartre 			rv = EFAULT;
59532f5224aeSachartre 			goto done;
59542f5224aeSachartre 		}
59552f5224aeSachartre 
59562f5224aeSachartre 		rlist32.listlen = listlen;
59572f5224aeSachartre 		rv = ddi_copyout(&rlist32, (caddr_t)(uintptr_t)inresv32.li,
59582f5224aeSachartre 		    sizeof (rlist32), mode);
59592f5224aeSachartre 		if (rv != 0) {
59602f5224aeSachartre 			rv = EFAULT;
59612f5224aeSachartre 			goto done;
59622f5224aeSachartre 		}
59632f5224aeSachartre 
59642f5224aeSachartre 		user_resv = (mhioc_resv_desc_t *)(uintptr_t)rlist32.list;
59652f5224aeSachartre 	} else {
59662f5224aeSachartre 		inresv.generation = scsi_resv->generation;
59672f5224aeSachartre 		rv = ddi_copyout(&inresv, arg, sizeof (inresv), mode);
59682f5224aeSachartre 		if (rv != 0) {
59692f5224aeSachartre 			rv = EFAULT;
59702f5224aeSachartre 			goto done;
59712f5224aeSachartre 		}
59722f5224aeSachartre 
59732f5224aeSachartre 		rlist.listlen = listlen;
59742f5224aeSachartre 		rv = ddi_copyout(&rlist, inresv.li, sizeof (rlist), mode);
59752f5224aeSachartre 		if (rv != 0) {
59762f5224aeSachartre 			rv = EFAULT;
59772f5224aeSachartre 			goto done;
59782f5224aeSachartre 		}
59792f5224aeSachartre 
59802f5224aeSachartre 		user_resv = rlist.list;
59812f5224aeSachartre 	}
59822f5224aeSachartre 
59832f5224aeSachartre 	/* copy out reservations */
59842f5224aeSachartre 	if (listsize > 0 && listlen > 0) {
59852f5224aeSachartre 		if (listsize < listlen)
59862f5224aeSachartre 			listlen = listsize;
59872f5224aeSachartre 		resv = (sd_readresv_desc_t *)&scsi_resv->readresv_desc;
59882f5224aeSachartre 
59892f5224aeSachartre 		for (i = 0; i < listlen; i++) {
59902f5224aeSachartre 			mhd_resv.type = resv->type;
59912f5224aeSachartre 			mhd_resv.scope = resv->scope;
59922f5224aeSachartre 			mhd_resv.scope_specific_addr =
59932f5224aeSachartre 			    BE_32(resv->scope_specific_addr);
59942f5224aeSachartre 			bcopy(&resv->resvkey, &mhd_resv.key,
59952f5224aeSachartre 			    MHIOC_RESV_KEY_SIZE);
59962f5224aeSachartre 
59972f5224aeSachartre 			rv = ddi_copyout(&mhd_resv, user_resv,
59982f5224aeSachartre 			    sizeof (mhd_resv), mode);
59992f5224aeSachartre 			if (rv != 0) {
60002f5224aeSachartre 				rv = EFAULT;
60012f5224aeSachartre 				goto done;
60022f5224aeSachartre 			}
60032f5224aeSachartre 			resv++;
60042f5224aeSachartre 			user_resv++;
60052f5224aeSachartre 		}
60062f5224aeSachartre 	}
60072f5224aeSachartre 
60082f5224aeSachartre 	if (rv == 0)
60092f5224aeSachartre 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
60102f5224aeSachartre 
60112f5224aeSachartre done:
60122f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
60132f5224aeSachartre 	return (rv);
60142f5224aeSachartre }
60152f5224aeSachartre 
60162f5224aeSachartre /*
60172f5224aeSachartre  * Implement the MHIOCGRP_REGISTER mhd(7i) ioctl. The ioctl is converted
60182f5224aeSachartre  * to a SCSI PERSISTENT OUT REGISTER command which is sent to the vdisk
60192f5224aeSachartre  * server with a VD_OP_SCSICMD operation.
60202f5224aeSachartre  */
60212f5224aeSachartre static int
60222f5224aeSachartre vdc_mhd_register(vdc_t *vdc, caddr_t arg, int mode)
60232f5224aeSachartre {
60242f5224aeSachartre 	vd_scsi_t *vd_scsi;
60252f5224aeSachartre 	sd_prout_t *scsi_prout;
60262f5224aeSachartre 	mhioc_register_t mhd_reg;
60272f5224aeSachartre 	int vd_scsi_len, rv;
60282f5224aeSachartre 
60292f5224aeSachartre 	/* copyin arguments */
60302f5224aeSachartre 	rv = ddi_copyin(arg, &mhd_reg, sizeof (mhd_reg), mode);
60312f5224aeSachartre 	if (rv != 0)
60322f5224aeSachartre 		return (EFAULT);
60332f5224aeSachartre 
60342f5224aeSachartre 	/* build SCSI VD_OP request */
60352f5224aeSachartre 	vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_REGISTER,
60362f5224aeSachartre 	    sizeof (sd_prout_t), &vd_scsi_len);
60372f5224aeSachartre 
60382f5224aeSachartre 	/* set parameters */
60392f5224aeSachartre 	scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi);
60402f5224aeSachartre 	bcopy(mhd_reg.oldkey.key, scsi_prout->res_key, MHIOC_RESV_KEY_SIZE);
60412f5224aeSachartre 	bcopy(mhd_reg.newkey.key, scsi_prout->service_key, MHIOC_RESV_KEY_SIZE);
60422f5224aeSachartre 	scsi_prout->aptpl = (uchar_t)mhd_reg.aptpl;
60432f5224aeSachartre 
60442f5224aeSachartre 	/* submit the request */
60452f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
60462f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
60472f5224aeSachartre 
60482f5224aeSachartre 	if (rv == 0)
60492f5224aeSachartre 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
60502f5224aeSachartre 
60512f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
60522f5224aeSachartre 	return (rv);
60532f5224aeSachartre }
60542f5224aeSachartre 
60552f5224aeSachartre /*
60562f5224aeSachartre  * Implement the MHIOCGRP_RESERVE mhd(7i) ioctl. The ioctl is converted
60572f5224aeSachartre  * to a SCSI PERSISTENT OUT RESERVE command which is sent to the vdisk
60582f5224aeSachartre  * server with a VD_OP_SCSICMD operation.
60592f5224aeSachartre  */
60602f5224aeSachartre static int
60612f5224aeSachartre vdc_mhd_reserve(vdc_t *vdc, caddr_t arg, int mode)
60622f5224aeSachartre {
60632f5224aeSachartre 	union scsi_cdb *cdb;
60642f5224aeSachartre 	vd_scsi_t *vd_scsi;
60652f5224aeSachartre 	sd_prout_t *scsi_prout;
60662f5224aeSachartre 	mhioc_resv_desc_t mhd_resv;
60672f5224aeSachartre 	int vd_scsi_len, rv;
60682f5224aeSachartre 
60692f5224aeSachartre 	/* copyin arguments */
60702f5224aeSachartre 	rv = ddi_copyin(arg, &mhd_resv, sizeof (mhd_resv), mode);
60712f5224aeSachartre 	if (rv != 0)
60722f5224aeSachartre 		return (EFAULT);
60732f5224aeSachartre 
60742f5224aeSachartre 	/* build SCSI VD_OP request */
60752f5224aeSachartre 	vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_RESERVE,
60762f5224aeSachartre 	    sizeof (sd_prout_t), &vd_scsi_len);
60772f5224aeSachartre 
60782f5224aeSachartre 	/* set parameters */
60792f5224aeSachartre 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
60802f5224aeSachartre 	scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi);
60812f5224aeSachartre 	bcopy(mhd_resv.key.key, scsi_prout->res_key, MHIOC_RESV_KEY_SIZE);
60822f5224aeSachartre 	scsi_prout->scope_address = mhd_resv.scope_specific_addr;
60832f5224aeSachartre 	cdb->cdb_opaque[2] = mhd_resv.type;
60842f5224aeSachartre 
60852f5224aeSachartre 	/* submit the request */
60862f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
60872f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
60882f5224aeSachartre 
60892f5224aeSachartre 	if (rv == 0)
60902f5224aeSachartre 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
60912f5224aeSachartre 
60922f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
60932f5224aeSachartre 	return (rv);
60942f5224aeSachartre }
60952f5224aeSachartre 
60962f5224aeSachartre /*
60972f5224aeSachartre  * Implement the MHIOCGRP_PREEMPTANDABORT mhd(7i) ioctl. The ioctl is
60982f5224aeSachartre  * converted to a SCSI PERSISTENT OUT PREEMPT AND ABORT command which
60992f5224aeSachartre  * is sent to the vdisk server with a VD_OP_SCSICMD operation.
61002f5224aeSachartre  */
61012f5224aeSachartre static int
61022f5224aeSachartre vdc_mhd_preemptabort(vdc_t *vdc, caddr_t arg, int mode)
61032f5224aeSachartre {
61042f5224aeSachartre 	union scsi_cdb *cdb;
61052f5224aeSachartre 	vd_scsi_t *vd_scsi;
61062f5224aeSachartre 	sd_prout_t *scsi_prout;
61072f5224aeSachartre 	mhioc_preemptandabort_t mhd_preempt;
61082f5224aeSachartre 	int vd_scsi_len, rv;
61092f5224aeSachartre 
61102f5224aeSachartre 	/* copyin arguments */
61112f5224aeSachartre 	rv = ddi_copyin(arg, &mhd_preempt, sizeof (mhd_preempt), mode);
61122f5224aeSachartre 	if (rv != 0)
61132f5224aeSachartre 		return (EFAULT);
61142f5224aeSachartre 
61152f5224aeSachartre 	/* build SCSI VD_OP request */
61162f5224aeSachartre 	vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_PREEMPTANDABORT,
61172f5224aeSachartre 	    sizeof (sd_prout_t), &vd_scsi_len);
61182f5224aeSachartre 
61192f5224aeSachartre 	/* set parameters */
61202f5224aeSachartre 	vd_scsi->task_attribute = VD_SCSI_TASK_ACA;
61212f5224aeSachartre 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
61222f5224aeSachartre 	scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi);
61232f5224aeSachartre 	bcopy(mhd_preempt.resvdesc.key.key, scsi_prout->res_key,
61242f5224aeSachartre 	    MHIOC_RESV_KEY_SIZE);
61252f5224aeSachartre 	bcopy(mhd_preempt.victim_key.key, scsi_prout->service_key,
61262f5224aeSachartre 	    MHIOC_RESV_KEY_SIZE);
61272f5224aeSachartre 	scsi_prout->scope_address = mhd_preempt.resvdesc.scope_specific_addr;
61282f5224aeSachartre 	cdb->cdb_opaque[2] = mhd_preempt.resvdesc.type;
61292f5224aeSachartre 
61302f5224aeSachartre 	/* submit the request */
61312f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
61322f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
61332f5224aeSachartre 
61342f5224aeSachartre 	if (rv == 0)
61352f5224aeSachartre 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
61362f5224aeSachartre 
61372f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
61382f5224aeSachartre 	return (rv);
61392f5224aeSachartre }
61402f5224aeSachartre 
61412f5224aeSachartre /*
61422f5224aeSachartre  * Implement the MHIOCGRP_REGISTERANDIGNOREKEY mhd(7i) ioctl. The ioctl
61432f5224aeSachartre  * is converted to a SCSI PERSISTENT OUT REGISTER AND IGNORE EXISTING KEY
61442f5224aeSachartre  * command which is sent to the vdisk server with a VD_OP_SCSICMD operation.
61452f5224aeSachartre  */
61462f5224aeSachartre static int
61472f5224aeSachartre vdc_mhd_registerignore(vdc_t *vdc, caddr_t arg, int mode)
61482f5224aeSachartre {
61492f5224aeSachartre 	vd_scsi_t *vd_scsi;
61502f5224aeSachartre 	sd_prout_t *scsi_prout;
61512f5224aeSachartre 	mhioc_registerandignorekey_t mhd_regi;
61522f5224aeSachartre 	int vd_scsi_len, rv;
61532f5224aeSachartre 
61542f5224aeSachartre 	/* copyin arguments */
61552f5224aeSachartre 	rv = ddi_copyin(arg, &mhd_regi, sizeof (mhd_regi), mode);
61562f5224aeSachartre 	if (rv != 0)
61572f5224aeSachartre 		return (EFAULT);
61582f5224aeSachartre 
61592f5224aeSachartre 	/* build SCSI VD_OP request */
61602f5224aeSachartre 	vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_REGISTERANDIGNOREKEY,
61612f5224aeSachartre 	    sizeof (sd_prout_t), &vd_scsi_len);
61622f5224aeSachartre 
61632f5224aeSachartre 	/* set parameters */
61642f5224aeSachartre 	scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi);
61652f5224aeSachartre 	bcopy(mhd_regi.newkey.key, scsi_prout->service_key,
61662f5224aeSachartre 	    MHIOC_RESV_KEY_SIZE);
61672f5224aeSachartre 	scsi_prout->aptpl = (uchar_t)mhd_regi.aptpl;
61682f5224aeSachartre 
61692f5224aeSachartre 	/* submit the request */
61702f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
61712f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
61722f5224aeSachartre 
61732f5224aeSachartre 	if (rv == 0)
61742f5224aeSachartre 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
61752f5224aeSachartre 
61762f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
61772f5224aeSachartre 	return (rv);
61782f5224aeSachartre }
61792f5224aeSachartre 
61802f5224aeSachartre /*
61812f5224aeSachartre  * This function is used by the failfast mechanism to send a SCSI command
61822f5224aeSachartre  * to check for reservation conflict.
61832f5224aeSachartre  */
61842f5224aeSachartre static int
61852f5224aeSachartre vdc_failfast_scsi_cmd(vdc_t *vdc, uchar_t scmd)
61862f5224aeSachartre {
61872f5224aeSachartre 	int cdb_len, sense_len, vd_scsi_len;
61882f5224aeSachartre 	vd_scsi_t *vd_scsi;
61892f5224aeSachartre 	union scsi_cdb *cdb;
61902f5224aeSachartre 	int rv;
61912f5224aeSachartre 
61922f5224aeSachartre 	ASSERT(scmd == SCMD_TEST_UNIT_READY || scmd == SCMD_WRITE_G1);
61932f5224aeSachartre 
61942f5224aeSachartre 	if (scmd == SCMD_WRITE_G1)
61952f5224aeSachartre 		cdb_len = CDB_GROUP1;
61962f5224aeSachartre 	else
61972f5224aeSachartre 		cdb_len = CDB_GROUP0;
61982f5224aeSachartre 
61992f5224aeSachartre 	sense_len = sizeof (struct scsi_extended_sense);
62002f5224aeSachartre 
62012f5224aeSachartre 	vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, 0, 0, &vd_scsi_len);
62022f5224aeSachartre 
62032f5224aeSachartre 	/* set cdb */
62042f5224aeSachartre 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
62052f5224aeSachartre 	cdb->scc_cmd = scmd;
62062f5224aeSachartre 
62072f5224aeSachartre 	vd_scsi->timeout = vdc_scsi_timeout;
62082f5224aeSachartre 
62092f5224aeSachartre 	/*
62102f5224aeSachartre 	 * Submit the request. The last argument has to be B_FALSE so that
62112f5224aeSachartre 	 * vdc_do_sync_op does not loop checking for reservation conflict if
62122f5224aeSachartre 	 * the operation returns an error.
62132f5224aeSachartre 	 */
62142f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
62152f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)FKIOCTL, VIO_both_dir, B_FALSE);
62162f5224aeSachartre 
62172f5224aeSachartre 	if (rv == 0)
62182f5224aeSachartre 		(void) vdc_scsi_status(vdc, vd_scsi, B_FALSE);
62192f5224aeSachartre 
62202f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
62212f5224aeSachartre 	return (rv);
62222f5224aeSachartre }
62232f5224aeSachartre 
62242f5224aeSachartre /*
62252f5224aeSachartre  * This function is used by the failfast mechanism to check for reservation
62262f5224aeSachartre  * conflict. It sends some SCSI commands which will fail with a reservation
62272f5224aeSachartre  * conflict error if the system does not have access to the disk and this
62282f5224aeSachartre  * will panic the system.
62292f5224aeSachartre  *
62302f5224aeSachartre  * Returned Code:
62312f5224aeSachartre  *	0	- disk is accessible without reservation conflict error
62322f5224aeSachartre  *	!= 0	- unable to check if disk is accessible
62332f5224aeSachartre  */
62342f5224aeSachartre int
62352f5224aeSachartre vdc_failfast_check_resv(vdc_t *vdc)
62362f5224aeSachartre {
62372f5224aeSachartre 	int failure = 0;
62382f5224aeSachartre 
62392f5224aeSachartre 	/*
62402f5224aeSachartre 	 * Send a TEST UNIT READY command. The command will panic
62412f5224aeSachartre 	 * the system if it fails with a reservation conflict.
62422f5224aeSachartre 	 */
62432f5224aeSachartre 	if (vdc_failfast_scsi_cmd(vdc, SCMD_TEST_UNIT_READY) != 0)
62442f5224aeSachartre 		failure++;
62452f5224aeSachartre 
62462f5224aeSachartre 	/*
62472f5224aeSachartre 	 * With SPC-3 compliant devices TEST UNIT READY will succeed on
62482f5224aeSachartre 	 * a reserved device, so we also do a WRITE(10) of zero byte in
62492f5224aeSachartre 	 * order to provoke a Reservation Conflict status on those newer
62502f5224aeSachartre 	 * devices.
62512f5224aeSachartre 	 */
62522f5224aeSachartre 	if (vdc_failfast_scsi_cmd(vdc, SCMD_WRITE_G1) != 0)
62532f5224aeSachartre 		failure++;
62542f5224aeSachartre 
62552f5224aeSachartre 	return (failure);
62562f5224aeSachartre }
62572f5224aeSachartre 
62582f5224aeSachartre /*
62592f5224aeSachartre  * Add a pending I/O to the failfast I/O queue. An I/O is added to this
62602f5224aeSachartre  * queue when it has failed and failfast is enabled. Then we have to check
62612f5224aeSachartre  * if it has failed because of a reservation conflict in which case we have
62622f5224aeSachartre  * to panic the system.
62632f5224aeSachartre  *
62642f5224aeSachartre  * Async I/O should be queued with their block I/O data transfer structure
62652f5224aeSachartre  * (buf). Sync I/O should be queued with buf = NULL.
62662f5224aeSachartre  */
62672f5224aeSachartre static vdc_io_t *
62682f5224aeSachartre vdc_failfast_io_queue(vdc_t *vdc, struct buf *buf)
62692f5224aeSachartre {
62702f5224aeSachartre 	vdc_io_t *vio;
62712f5224aeSachartre 
62722f5224aeSachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
62732f5224aeSachartre 
62742f5224aeSachartre 	vio = kmem_alloc(sizeof (vdc_io_t), KM_SLEEP);
62752f5224aeSachartre 	vio->vio_next = vdc->failfast_io_queue;
62762f5224aeSachartre 	vio->vio_buf = buf;
62772f5224aeSachartre 	vio->vio_qtime = ddi_get_lbolt();
62782f5224aeSachartre 
62792f5224aeSachartre 	vdc->failfast_io_queue = vio;
62802f5224aeSachartre 
62812f5224aeSachartre 	/* notify the failfast thread that a new I/O is queued */
62822f5224aeSachartre 	cv_signal(&vdc->failfast_cv);
62832f5224aeSachartre 
62842f5224aeSachartre 	return (vio);
62852f5224aeSachartre }
62862f5224aeSachartre 
62872f5224aeSachartre /*
62882f5224aeSachartre  * Remove and complete I/O in the failfast I/O queue which have been
62892f5224aeSachartre  * added after the indicated deadline. A deadline of 0 means that all
62902f5224aeSachartre  * I/O have to be unqueued and marked as completed.
62912f5224aeSachartre  */
62922f5224aeSachartre static void
62932f5224aeSachartre vdc_failfast_io_unqueue(vdc_t *vdc, clock_t deadline)
62942f5224aeSachartre {
62952f5224aeSachartre 	vdc_io_t *vio, *vio_tmp;
62962f5224aeSachartre 
62972f5224aeSachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
62982f5224aeSachartre 
62992f5224aeSachartre 	vio_tmp = NULL;
63002f5224aeSachartre 	vio = vdc->failfast_io_queue;
63012f5224aeSachartre 
63022f5224aeSachartre 	if (deadline != 0) {
63032f5224aeSachartre 		/*
63042f5224aeSachartre 		 * Skip any io queued after the deadline. The failfast
63052f5224aeSachartre 		 * I/O queue is ordered starting with the last I/O added
63062f5224aeSachartre 		 * to the queue.
63072f5224aeSachartre 		 */
63082f5224aeSachartre 		while (vio != NULL && vio->vio_qtime > deadline) {
63092f5224aeSachartre 			vio_tmp = vio;
63102f5224aeSachartre 			vio = vio->vio_next;
63112f5224aeSachartre 		}
63122f5224aeSachartre 	}
63132f5224aeSachartre 
63142f5224aeSachartre 	if (vio == NULL)
63152f5224aeSachartre 		/* nothing to unqueue */
63162f5224aeSachartre 		return;
63172f5224aeSachartre 
63182f5224aeSachartre 	/* update the queue */
63192f5224aeSachartre 	if (vio_tmp == NULL)
63202f5224aeSachartre 		vdc->failfast_io_queue = NULL;
63212f5224aeSachartre 	else
63222f5224aeSachartre 		vio_tmp->vio_next = NULL;
63232f5224aeSachartre 
63242f5224aeSachartre 	/*
63252f5224aeSachartre 	 * Complete unqueued I/O. Async I/O have a block I/O data transfer
63262f5224aeSachartre 	 * structure (buf) and they are completed by calling biodone(). Sync
63272f5224aeSachartre 	 * I/O do not have a buf and they are completed by setting the
63282f5224aeSachartre 	 * vio_qtime to zero and signaling failfast_io_cv. In that case, the
63292f5224aeSachartre 	 * thread waiting for the I/O to complete is responsible for freeing
63302f5224aeSachartre 	 * the vio structure.
63312f5224aeSachartre 	 */
63322f5224aeSachartre 	while (vio != NULL) {
63332f5224aeSachartre 		vio_tmp = vio->vio_next;
63342f5224aeSachartre 		if (vio->vio_buf != NULL) {
633590e2f9dcSlm66018 			VD_KSTAT_RUNQ_EXIT(vdc);
6336366a92acSlm66018 			DTRACE_IO1(done, buf_t *, vio->vio_buf);
63372f5224aeSachartre 			biodone(vio->vio_buf);
63382f5224aeSachartre 			kmem_free(vio, sizeof (vdc_io_t));
63392f5224aeSachartre 		} else {
63402f5224aeSachartre 			vio->vio_qtime = 0;
63412f5224aeSachartre 		}
63422f5224aeSachartre 		vio = vio_tmp;
63432f5224aeSachartre 	}
63442f5224aeSachartre 
63452f5224aeSachartre 	cv_broadcast(&vdc->failfast_io_cv);
63462f5224aeSachartre }
63472f5224aeSachartre 
63482f5224aeSachartre /*
63492f5224aeSachartre  * Failfast Thread.
63502f5224aeSachartre  *
63512f5224aeSachartre  * While failfast is enabled, the failfast thread sends a TEST UNIT READY
63522f5224aeSachartre  * and a zero size WRITE(10) SCSI commands on a regular basis to check that
63532f5224aeSachartre  * we still have access to the disk. If a command fails with a RESERVATION
63542f5224aeSachartre  * CONFLICT error then the system will immediatly panic.
63552f5224aeSachartre  *
63562f5224aeSachartre  * The failfast thread is also woken up when an I/O has failed. It then check
63572f5224aeSachartre  * the access to the disk to ensure that the I/O failure was not due to a
63582f5224aeSachartre  * reservation conflict.
63592f5224aeSachartre  *
63602f5224aeSachartre  * There is one failfast thread for each virtual disk for which failfast is
63612f5224aeSachartre  * enabled. We could have only one thread sending requests for all disks but
63622f5224aeSachartre  * this would need vdc to send asynchronous requests and to have callbacks to
63632f5224aeSachartre  * process replies.
63642f5224aeSachartre  */
63652f5224aeSachartre static void
63662f5224aeSachartre vdc_failfast_thread(void *arg)
63672f5224aeSachartre {
63682f5224aeSachartre 	int status;
63692f5224aeSachartre 	vdc_t *vdc = (vdc_t *)arg;
63702f5224aeSachartre 	clock_t timeout, starttime;
63712f5224aeSachartre 
63722f5224aeSachartre 	mutex_enter(&vdc->lock);
63732f5224aeSachartre 
63742f5224aeSachartre 	while (vdc->failfast_interval != 0) {
63752f5224aeSachartre 
63762f5224aeSachartre 		starttime = ddi_get_lbolt();
63772f5224aeSachartre 
63782f5224aeSachartre 		mutex_exit(&vdc->lock);
63792f5224aeSachartre 
63802f5224aeSachartre 		/* check for reservation conflict */
63812f5224aeSachartre 		status = vdc_failfast_check_resv(vdc);
63822f5224aeSachartre 
63832f5224aeSachartre 		mutex_enter(&vdc->lock);
63842f5224aeSachartre 		/*
63852f5224aeSachartre 		 * We have dropped the lock to send the SCSI command so we have
63862f5224aeSachartre 		 * to check that failfast is still enabled.
63872f5224aeSachartre 		 */
63882f5224aeSachartre 		if (vdc->failfast_interval == 0)
63892f5224aeSachartre 			break;
63902f5224aeSachartre 
63912f5224aeSachartre 		/*
63922f5224aeSachartre 		 * If we have successfully check the disk access and there was
63932f5224aeSachartre 		 * no reservation conflict then we can complete any I/O queued
63942f5224aeSachartre 		 * before the last check.
63952f5224aeSachartre 		 */
63962f5224aeSachartre 		if (status == 0)
63972f5224aeSachartre 			vdc_failfast_io_unqueue(vdc, starttime);
63982f5224aeSachartre 
63992f5224aeSachartre 		/* proceed again if some I/O are still in the queue */
64002f5224aeSachartre 		if (vdc->failfast_io_queue != NULL)
64012f5224aeSachartre 			continue;
64022f5224aeSachartre 
64032f5224aeSachartre 		timeout = ddi_get_lbolt() +
64042f5224aeSachartre 		    drv_usectohz(vdc->failfast_interval);
64052f5224aeSachartre 		(void) cv_timedwait(&vdc->failfast_cv, &vdc->lock, timeout);
64062f5224aeSachartre 	}
64072f5224aeSachartre 
64082f5224aeSachartre 	/*
64092f5224aeSachartre 	 * Failfast is being stop so we can complete any queued I/O.
64102f5224aeSachartre 	 */
64112f5224aeSachartre 	vdc_failfast_io_unqueue(vdc, 0);
64122f5224aeSachartre 	vdc->failfast_thread = NULL;
64132f5224aeSachartre 	mutex_exit(&vdc->lock);
64142f5224aeSachartre 	thread_exit();
64152f5224aeSachartre }
64162f5224aeSachartre 
64172f5224aeSachartre /*
64182f5224aeSachartre  * Implement the MHIOCENFAILFAST mhd(7i) ioctl.
64192f5224aeSachartre  */
64202f5224aeSachartre static int
64212f5224aeSachartre vdc_failfast(vdc_t *vdc, caddr_t arg, int mode)
64222f5224aeSachartre {
64232f5224aeSachartre 	unsigned int mh_time;
64242f5224aeSachartre 
64252f5224aeSachartre 	if (ddi_copyin((void *)arg, &mh_time, sizeof (int), mode))
64262f5224aeSachartre 		return (EFAULT);
64272f5224aeSachartre 
64282f5224aeSachartre 	mutex_enter(&vdc->lock);
64292f5224aeSachartre 	if (mh_time != 0 && vdc->failfast_thread == NULL) {
64302f5224aeSachartre 		vdc->failfast_thread = thread_create(NULL, 0,
64312f5224aeSachartre 		    vdc_failfast_thread, vdc, 0, &p0, TS_RUN,
64322f5224aeSachartre 		    v.v_maxsyspri - 2);
64332f5224aeSachartre 	}
64342f5224aeSachartre 
64352f5224aeSachartre 	vdc->failfast_interval = mh_time * 1000;
64362f5224aeSachartre 	cv_signal(&vdc->failfast_cv);
64372f5224aeSachartre 	mutex_exit(&vdc->lock);
64382f5224aeSachartre 
64392f5224aeSachartre 	return (0);
64402f5224aeSachartre }
64412f5224aeSachartre 
64422f5224aeSachartre /*
64432f5224aeSachartre  * Implement the MHIOCTKOWN and MHIOCRELEASE mhd(7i) ioctls. These ioctls are
64442f5224aeSachartre  * converted to VD_OP_SET_ACCESS operations.
64452f5224aeSachartre  */
64462f5224aeSachartre static int
64472f5224aeSachartre vdc_access_set(vdc_t *vdc, uint64_t flags, int mode)
64482f5224aeSachartre {
64492f5224aeSachartre 	int rv;
64502f5224aeSachartre 
64512f5224aeSachartre 	/* submit owership command request */
64522f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SET_ACCESS, (caddr_t)&flags,
64532f5224aeSachartre 	    sizeof (uint64_t), 0, 0, CB_SYNC, (void *)(uint64_t)mode,
64542f5224aeSachartre 	    VIO_both_dir, B_TRUE);
64552f5224aeSachartre 
64562f5224aeSachartre 	return (rv);
64572f5224aeSachartre }
64582f5224aeSachartre 
64592f5224aeSachartre /*
64602f5224aeSachartre  * Implement the MHIOCSTATUS mhd(7i) ioctl. This ioctl is converted to a
64612f5224aeSachartre  * VD_OP_GET_ACCESS operation.
64622f5224aeSachartre  */
64632f5224aeSachartre static int
64642f5224aeSachartre vdc_access_get(vdc_t *vdc, uint64_t *status, int mode)
64652f5224aeSachartre {
64662f5224aeSachartre 	int rv;
64672f5224aeSachartre 
64682f5224aeSachartre 	/* submit owership command request */
64692f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_GET_ACCESS, (caddr_t)status,
64702f5224aeSachartre 	    sizeof (uint64_t), 0, 0, CB_SYNC, (void *)(uint64_t)mode,
64712f5224aeSachartre 	    VIO_both_dir, B_TRUE);
64722f5224aeSachartre 
64732f5224aeSachartre 	return (rv);
64742f5224aeSachartre }
64752f5224aeSachartre 
64762f5224aeSachartre /*
64772f5224aeSachartre  * Disk Ownership Thread.
64782f5224aeSachartre  *
64792f5224aeSachartre  * When we have taken the ownership of a disk, this thread waits to be
64802f5224aeSachartre  * notified when the LDC channel is reset so that it can recover the
64812f5224aeSachartre  * ownership.
64822f5224aeSachartre  *
64832f5224aeSachartre  * Note that the thread handling the LDC reset (vdc_process_msg_thread())
64842f5224aeSachartre  * can not be used to do the ownership recovery because it has to be
64852f5224aeSachartre  * running to handle the reply message to the ownership operation.
64862f5224aeSachartre  */
64872f5224aeSachartre static void
64882f5224aeSachartre vdc_ownership_thread(void *arg)
64892f5224aeSachartre {
64902f5224aeSachartre 	vdc_t *vdc = (vdc_t *)arg;
64912f5224aeSachartre 	clock_t timeout;
64922f5224aeSachartre 	uint64_t status;
64932f5224aeSachartre 
64942f5224aeSachartre 	mutex_enter(&vdc->ownership_lock);
64952f5224aeSachartre 	mutex_enter(&vdc->lock);
64962f5224aeSachartre 
64972f5224aeSachartre 	while (vdc->ownership & VDC_OWNERSHIP_WANTED) {
64982f5224aeSachartre 
64992f5224aeSachartre 		if ((vdc->ownership & VDC_OWNERSHIP_RESET) ||
65002f5224aeSachartre 		    !(vdc->ownership & VDC_OWNERSHIP_GRANTED)) {
65012f5224aeSachartre 			/*
65022f5224aeSachartre 			 * There was a reset so the ownership has been lost,
65032f5224aeSachartre 			 * try to recover. We do this without using the preempt
65042f5224aeSachartre 			 * option so that we don't steal the ownership from
65052f5224aeSachartre 			 * someone who has preempted us.
65062f5224aeSachartre 			 */
65072f5224aeSachartre 			DMSG(vdc, 0, "[%d] Ownership lost, recovering",
65082f5224aeSachartre 			    vdc->instance);
65092f5224aeSachartre 
65102f5224aeSachartre 			vdc->ownership &= ~(VDC_OWNERSHIP_RESET |
65112f5224aeSachartre 			    VDC_OWNERSHIP_GRANTED);
65122f5224aeSachartre 
65132f5224aeSachartre 			mutex_exit(&vdc->lock);
65142f5224aeSachartre 
65152f5224aeSachartre 			status = vdc_access_set(vdc, VD_ACCESS_SET_EXCLUSIVE |
65162f5224aeSachartre 			    VD_ACCESS_SET_PRESERVE, FKIOCTL);
65172f5224aeSachartre 
65182f5224aeSachartre 			mutex_enter(&vdc->lock);
65192f5224aeSachartre 
65202f5224aeSachartre 			if (status == 0) {
65212f5224aeSachartre 				DMSG(vdc, 0, "[%d] Ownership recovered",
65222f5224aeSachartre 				    vdc->instance);
65232f5224aeSachartre 				vdc->ownership |= VDC_OWNERSHIP_GRANTED;
65242f5224aeSachartre 			} else {
65252f5224aeSachartre 				DMSG(vdc, 0, "[%d] Fail to recover ownership",
65262f5224aeSachartre 				    vdc->instance);
65272f5224aeSachartre 			}
65282f5224aeSachartre 
65292f5224aeSachartre 		}
65302f5224aeSachartre 
65312f5224aeSachartre 		/*
65322f5224aeSachartre 		 * If we have the ownership then we just wait for an event
65332f5224aeSachartre 		 * to happen (LDC reset), otherwise we will retry to recover
65342f5224aeSachartre 		 * after a delay.
65352f5224aeSachartre 		 */
65362f5224aeSachartre 		if (vdc->ownership & VDC_OWNERSHIP_GRANTED)
65372f5224aeSachartre 			timeout = 0;
65382f5224aeSachartre 		else
65392f5224aeSachartre 			timeout = ddi_get_lbolt() +
65402f5224aeSachartre 			    drv_usectohz(vdc_ownership_delay);
65412f5224aeSachartre 
65422f5224aeSachartre 		/* Release the ownership_lock and wait on the vdc lock */
65432f5224aeSachartre 		mutex_exit(&vdc->ownership_lock);
65442f5224aeSachartre 
65452f5224aeSachartre 		if (timeout == 0)
65462f5224aeSachartre 			(void) cv_wait(&vdc->ownership_cv, &vdc->lock);
65472f5224aeSachartre 		else
65482f5224aeSachartre 			(void) cv_timedwait(&vdc->ownership_cv,
65492f5224aeSachartre 			    &vdc->lock, timeout);
65502f5224aeSachartre 
65512f5224aeSachartre 		mutex_exit(&vdc->lock);
65522f5224aeSachartre 
65532f5224aeSachartre 		mutex_enter(&vdc->ownership_lock);
65542f5224aeSachartre 		mutex_enter(&vdc->lock);
65552f5224aeSachartre 	}
65562f5224aeSachartre 
65572f5224aeSachartre 	vdc->ownership_thread = NULL;
65582f5224aeSachartre 	mutex_exit(&vdc->lock);
65592f5224aeSachartre 	mutex_exit(&vdc->ownership_lock);
65602f5224aeSachartre 
65612f5224aeSachartre 	thread_exit();
65622f5224aeSachartre }
65632f5224aeSachartre 
65642f5224aeSachartre static void
65652f5224aeSachartre vdc_ownership_update(vdc_t *vdc, int ownership_flags)
65662f5224aeSachartre {
65672f5224aeSachartre 	ASSERT(MUTEX_HELD(&vdc->ownership_lock));
65682f5224aeSachartre 
65692f5224aeSachartre 	mutex_enter(&vdc->lock);
65702f5224aeSachartre 	vdc->ownership = ownership_flags;
65712f5224aeSachartre 	if ((vdc->ownership & VDC_OWNERSHIP_WANTED) &&
65722f5224aeSachartre 	    vdc->ownership_thread == NULL) {
65732f5224aeSachartre 		/* start ownership thread */
65742f5224aeSachartre 		vdc->ownership_thread = thread_create(NULL, 0,
65752f5224aeSachartre 		    vdc_ownership_thread, vdc, 0, &p0, TS_RUN,
65762f5224aeSachartre 		    v.v_maxsyspri - 2);
65772f5224aeSachartre 	} else {
65782f5224aeSachartre 		/* notify the ownership thread */
65792f5224aeSachartre 		cv_signal(&vdc->ownership_cv);
65802f5224aeSachartre 	}
65812f5224aeSachartre 	mutex_exit(&vdc->lock);
65822f5224aeSachartre }
65832f5224aeSachartre 
65842f5224aeSachartre /*
65852f5224aeSachartre  * Get the size and the block size of a virtual disk from the vdisk server.
65862f5224aeSachartre  * We need to use this operation when the vdisk_size attribute was not
65872f5224aeSachartre  * available during the handshake with the vdisk server.
65882f5224aeSachartre  */
65892f5224aeSachartre static int
65902f5224aeSachartre vdc_check_capacity(vdc_t *vdc)
65912f5224aeSachartre {
65922f5224aeSachartre 	int rv = 0;
65932f5224aeSachartre 	size_t alloc_len;
65942f5224aeSachartre 	vd_capacity_t *vd_cap;
65952f5224aeSachartre 
65962f5224aeSachartre 	if (vdc->vdisk_size != 0)
65972f5224aeSachartre 		return (0);
65982f5224aeSachartre 
65992f5224aeSachartre 	alloc_len = P2ROUNDUP(sizeof (vd_capacity_t), sizeof (uint64_t));
66002f5224aeSachartre 
66012f5224aeSachartre 	vd_cap = kmem_zalloc(alloc_len, KM_SLEEP);
66022f5224aeSachartre 
66032f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_GET_CAPACITY, (caddr_t)vd_cap, alloc_len,
66042f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)FKIOCTL, VIO_both_dir, B_TRUE);
66052f5224aeSachartre 
66062f5224aeSachartre 	if (rv == 0) {
66072f5224aeSachartre 		if (vd_cap->vdisk_block_size != vdc->block_size ||
66082f5224aeSachartre 		    vd_cap->vdisk_size == VD_SIZE_UNKNOWN ||
66092f5224aeSachartre 		    vd_cap->vdisk_size == 0)
66102f5224aeSachartre 			rv = EINVAL;
66112f5224aeSachartre 		else
66122f5224aeSachartre 			vdc->vdisk_size = vd_cap->vdisk_size;
66132f5224aeSachartre 	}
66142f5224aeSachartre 
66152f5224aeSachartre 	kmem_free(vd_cap, alloc_len);
66162f5224aeSachartre 	return (rv);
66172f5224aeSachartre }
66182f5224aeSachartre 
66192f5224aeSachartre /*
66201ae08745Sheppo  * This structure is used in the DKIO(7I) array below.
66211ae08745Sheppo  */
66221ae08745Sheppo typedef struct vdc_dk_ioctl {
66231ae08745Sheppo 	uint8_t		op;		/* VD_OP_XXX value */
66241ae08745Sheppo 	int		cmd;		/* Solaris ioctl operation number */
66251ae08745Sheppo 	size_t		nbytes;		/* size of structure to be copied */
66260a55fbb7Slm66018 
66270a55fbb7Slm66018 	/* function to convert between vDisk and Solaris structure formats */
6628d10e4ef2Snarayan 	int	(*convert)(vdc_t *vdc, void *vd_buf, void *ioctl_arg,
6629d10e4ef2Snarayan 	    int mode, int dir);
66301ae08745Sheppo } vdc_dk_ioctl_t;
66311ae08745Sheppo 
66321ae08745Sheppo /*
66331ae08745Sheppo  * Subset of DKIO(7I) operations currently supported
66341ae08745Sheppo  */
66351ae08745Sheppo static vdc_dk_ioctl_t	dk_ioctl[] = {
6636eff7243fSlm66018 	{VD_OP_FLUSH,		DKIOCFLUSHWRITECACHE,	0,
66370a55fbb7Slm66018 		vdc_null_copy_func},
66380a55fbb7Slm66018 	{VD_OP_GET_WCE,		DKIOCGETWCE,		sizeof (int),
66394bac2208Snarayan 		vdc_get_wce_convert},
66400a55fbb7Slm66018 	{VD_OP_SET_WCE,		DKIOCSETWCE,		sizeof (int),
66414bac2208Snarayan 		vdc_set_wce_convert},
66420a55fbb7Slm66018 	{VD_OP_GET_VTOC,	DKIOCGVTOC,		sizeof (vd_vtoc_t),
66430a55fbb7Slm66018 		vdc_get_vtoc_convert},
66440a55fbb7Slm66018 	{VD_OP_SET_VTOC,	DKIOCSVTOC,		sizeof (vd_vtoc_t),
66450a55fbb7Slm66018 		vdc_set_vtoc_convert},
66460a55fbb7Slm66018 	{VD_OP_GET_DISKGEOM,	DKIOCGGEOM,		sizeof (vd_geom_t),
66470a55fbb7Slm66018 		vdc_get_geom_convert},
66480a55fbb7Slm66018 	{VD_OP_GET_DISKGEOM,	DKIOCG_PHYGEOM,		sizeof (vd_geom_t),
66490a55fbb7Slm66018 		vdc_get_geom_convert},
66500a55fbb7Slm66018 	{VD_OP_GET_DISKGEOM, 	DKIOCG_VIRTGEOM,	sizeof (vd_geom_t),
66510a55fbb7Slm66018 		vdc_get_geom_convert},
66520a55fbb7Slm66018 	{VD_OP_SET_DISKGEOM,	DKIOCSGEOM,		sizeof (vd_geom_t),
66530a55fbb7Slm66018 		vdc_set_geom_convert},
66544bac2208Snarayan 	{VD_OP_GET_EFI,		DKIOCGETEFI,		0,
66554bac2208Snarayan 		vdc_get_efi_convert},
66564bac2208Snarayan 	{VD_OP_SET_EFI,		DKIOCSETEFI,		0,
66574bac2208Snarayan 		vdc_set_efi_convert},
66580a55fbb7Slm66018 
665987a7269eSachartre 	/* DIOCTL_RWCMD is converted to a read or a write */
666087a7269eSachartre 	{0, DIOCTL_RWCMD,  sizeof (struct dadkio_rwcmd), NULL},
666187a7269eSachartre 
66622f5224aeSachartre 	/* mhd(7I) non-shared multihost disks ioctls */
66632f5224aeSachartre 	{0, MHIOCTKOWN,				0, vdc_null_copy_func},
66642f5224aeSachartre 	{0, MHIOCRELEASE,			0, vdc_null_copy_func},
66652f5224aeSachartre 	{0, MHIOCSTATUS,			0, vdc_null_copy_func},
66662f5224aeSachartre 	{0, MHIOCQRESERVE,			0, vdc_null_copy_func},
66672f5224aeSachartre 
66682f5224aeSachartre 	/* mhd(7I) shared multihost disks ioctls */
66692f5224aeSachartre 	{0, MHIOCGRP_INKEYS,			0, vdc_null_copy_func},
66702f5224aeSachartre 	{0, MHIOCGRP_INRESV,			0, vdc_null_copy_func},
66712f5224aeSachartre 	{0, MHIOCGRP_REGISTER,			0, vdc_null_copy_func},
66722f5224aeSachartre 	{0, MHIOCGRP_RESERVE, 			0, vdc_null_copy_func},
66732f5224aeSachartre 	{0, MHIOCGRP_PREEMPTANDABORT,		0, vdc_null_copy_func},
66742f5224aeSachartre 	{0, MHIOCGRP_REGISTERANDIGNOREKEY,	0, vdc_null_copy_func},
66752f5224aeSachartre 
66762f5224aeSachartre 	/* mhd(7I) failfast ioctl */
66772f5224aeSachartre 	{0, MHIOCENFAILFAST,			0, vdc_null_copy_func},
66782f5224aeSachartre 
66790a55fbb7Slm66018 	/*
66800a55fbb7Slm66018 	 * These particular ioctls are not sent to the server - vdc fakes up
66810a55fbb7Slm66018 	 * the necessary info.
66820a55fbb7Slm66018 	 */
66830a55fbb7Slm66018 	{0, DKIOCINFO, sizeof (struct dk_cinfo), vdc_null_copy_func},
66840a55fbb7Slm66018 	{0, DKIOCGMEDIAINFO, sizeof (struct dk_minfo), vdc_null_copy_func},
66850a55fbb7Slm66018 	{0, USCSICMD,	sizeof (struct uscsi_cmd), vdc_null_copy_func},
66869642afceSachartre 	{0, DKIOCPARTITION, 0, vdc_null_copy_func },
668787a7269eSachartre 	{0, DKIOCGAPART, 0, vdc_null_copy_func },
66880a55fbb7Slm66018 	{0, DKIOCREMOVABLE, 0, vdc_null_copy_func},
66890a55fbb7Slm66018 	{0, CDROMREADOFFSET, 0, vdc_null_copy_func}
66901ae08745Sheppo };
66911ae08745Sheppo 
66921ae08745Sheppo /*
6693edcc0754Sachartre  * This function handles ioctl requests from the vd_efi_alloc_and_read()
6694edcc0754Sachartre  * function and forward them to the vdisk.
66952f5224aeSachartre  */
66962f5224aeSachartre static int
6697edcc0754Sachartre vd_process_efi_ioctl(void *vdisk, int cmd, uintptr_t arg)
66982f5224aeSachartre {
6699edcc0754Sachartre 	vdc_t *vdc = (vdc_t *)vdisk;
6700edcc0754Sachartre 	dev_t dev;
67012f5224aeSachartre 	int rval;
6702edcc0754Sachartre 
6703edcc0754Sachartre 	dev = makedevice(ddi_driver_major(vdc->dip),
6704edcc0754Sachartre 	    VD_MAKE_DEV(vdc->instance, 0));
6705edcc0754Sachartre 
6706edcc0754Sachartre 	return (vd_process_ioctl(dev, cmd, (caddr_t)arg, FKIOCTL, &rval));
67072f5224aeSachartre }
67082f5224aeSachartre 
67092f5224aeSachartre /*
67101ae08745Sheppo  * Function:
67111ae08745Sheppo  *	vd_process_ioctl()
67121ae08745Sheppo  *
67131ae08745Sheppo  * Description:
67140a55fbb7Slm66018  *	This routine processes disk specific ioctl calls
67151ae08745Sheppo  *
67161ae08745Sheppo  * Arguments:
67171ae08745Sheppo  *	dev	- the device number
67181ae08745Sheppo  *	cmd	- the operation [dkio(7I)] to be processed
67191ae08745Sheppo  *	arg	- pointer to user provided structure
67201ae08745Sheppo  *		  (contains data to be set or reference parameter for get)
67211ae08745Sheppo  *	mode	- bit flag, indicating open settings, 32/64 bit type, etc
67222f5224aeSachartre  *	rvalp	- pointer to return value for calling process.
67231ae08745Sheppo  *
67241ae08745Sheppo  * Return Code:
67251ae08745Sheppo  *	0
67261ae08745Sheppo  *	EFAULT
67271ae08745Sheppo  *	ENXIO
67281ae08745Sheppo  *	EIO
67291ae08745Sheppo  *	ENOTSUP
67301ae08745Sheppo  */
67311ae08745Sheppo static int
67322f5224aeSachartre vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode, int *rvalp)
67331ae08745Sheppo {
67340d0c8d4bSnarayan 	int		instance = VDCUNIT(dev);
67351ae08745Sheppo 	vdc_t		*vdc = NULL;
67361ae08745Sheppo 	int		rv = -1;
67371ae08745Sheppo 	int		idx = 0;		/* index into dk_ioctl[] */
67381ae08745Sheppo 	size_t		len = 0;		/* #bytes to send to vds */
67391ae08745Sheppo 	size_t		alloc_len = 0;		/* #bytes to allocate mem for */
67401ae08745Sheppo 	caddr_t		mem_p = NULL;
67411ae08745Sheppo 	size_t		nioctls = (sizeof (dk_ioctl)) / (sizeof (dk_ioctl[0]));
67423af08d82Slm66018 	vdc_dk_ioctl_t	*iop;
67431ae08745Sheppo 
67441ae08745Sheppo 	vdc = ddi_get_soft_state(vdc_state, instance);
67451ae08745Sheppo 	if (vdc == NULL) {
67461ae08745Sheppo 		cmn_err(CE_NOTE, "![%d] Could not get soft state structure",
67471ae08745Sheppo 		    instance);
67481ae08745Sheppo 		return (ENXIO);
67491ae08745Sheppo 	}
67501ae08745Sheppo 
67513af08d82Slm66018 	DMSG(vdc, 0, "[%d] Processing ioctl(%x) for dev %lx : model %x\n",
67523af08d82Slm66018 	    instance, cmd, dev, ddi_model_convert_from(mode & FMODELS));
67531ae08745Sheppo 
67542f5224aeSachartre 	if (rvalp != NULL) {
67552f5224aeSachartre 		/* the return value of the ioctl is 0 by default */
67562f5224aeSachartre 		*rvalp = 0;
67572f5224aeSachartre 	}
67582f5224aeSachartre 
67591ae08745Sheppo 	/*
67601ae08745Sheppo 	 * Validate the ioctl operation to be performed.
67611ae08745Sheppo 	 *
67621ae08745Sheppo 	 * If we have looped through the array without finding a match then we
67631ae08745Sheppo 	 * don't support this ioctl.
67641ae08745Sheppo 	 */
67651ae08745Sheppo 	for (idx = 0; idx < nioctls; idx++) {
67661ae08745Sheppo 		if (cmd == dk_ioctl[idx].cmd)
67671ae08745Sheppo 			break;
67681ae08745Sheppo 	}
67691ae08745Sheppo 
67701ae08745Sheppo 	if (idx >= nioctls) {
67713af08d82Slm66018 		DMSG(vdc, 0, "[%d] Unsupported ioctl (0x%x)\n",
6772e1ebb9ecSlm66018 		    vdc->instance, cmd);
67731ae08745Sheppo 		return (ENOTSUP);
67741ae08745Sheppo 	}
67751ae08745Sheppo 
67763af08d82Slm66018 	iop = &(dk_ioctl[idx]);
67773af08d82Slm66018 
67784bac2208Snarayan 	if (cmd == DKIOCGETEFI || cmd == DKIOCSETEFI) {
67794bac2208Snarayan 		/* size is not fixed for EFI ioctls, it depends on ioctl arg */
67804bac2208Snarayan 		dk_efi_t	dk_efi;
67814bac2208Snarayan 
67824bac2208Snarayan 		rv = ddi_copyin(arg, &dk_efi, sizeof (dk_efi_t), mode);
67834bac2208Snarayan 		if (rv != 0)
67844bac2208Snarayan 			return (EFAULT);
67854bac2208Snarayan 
67864bac2208Snarayan 		len = sizeof (vd_efi_t) - 1 + dk_efi.dki_length;
67874bac2208Snarayan 	} else {
67883af08d82Slm66018 		len = iop->nbytes;
67894bac2208Snarayan 	}
67901ae08745Sheppo 
67912f5224aeSachartre 	/* check if the ioctl is applicable */
67921ae08745Sheppo 	switch (cmd) {
67931ae08745Sheppo 	case CDROMREADOFFSET:
67941ae08745Sheppo 	case DKIOCREMOVABLE:
67951ae08745Sheppo 		return (ENOTTY);
67961ae08745Sheppo 
67972f5224aeSachartre 	case USCSICMD:
67982f5224aeSachartre 	case MHIOCTKOWN:
67992f5224aeSachartre 	case MHIOCSTATUS:
68002f5224aeSachartre 	case MHIOCQRESERVE:
68012f5224aeSachartre 	case MHIOCRELEASE:
68022f5224aeSachartre 	case MHIOCGRP_INKEYS:
68032f5224aeSachartre 	case MHIOCGRP_INRESV:
68042f5224aeSachartre 	case MHIOCGRP_REGISTER:
68052f5224aeSachartre 	case MHIOCGRP_RESERVE:
68062f5224aeSachartre 	case MHIOCGRP_PREEMPTANDABORT:
68072f5224aeSachartre 	case MHIOCGRP_REGISTERANDIGNOREKEY:
68082f5224aeSachartre 	case MHIOCENFAILFAST:
68092f5224aeSachartre 		if (vdc->cinfo == NULL)
68102f5224aeSachartre 			return (ENXIO);
68112f5224aeSachartre 		if (vdc->cinfo->dki_ctype != DKC_SCSI_CCS)
68122f5224aeSachartre 			return (ENOTTY);
68132f5224aeSachartre 		break;
68142f5224aeSachartre 
68152f5224aeSachartre 	case DIOCTL_RWCMD:
68162f5224aeSachartre 		if (vdc->cinfo == NULL)
68172f5224aeSachartre 			return (ENXIO);
68182f5224aeSachartre 		if (vdc->cinfo->dki_ctype != DKC_DIRECT)
68192f5224aeSachartre 			return (ENOTTY);
68202f5224aeSachartre 		break;
68212f5224aeSachartre 
68222f5224aeSachartre 	case DKIOCINFO:
68232f5224aeSachartre 		if (vdc->cinfo == NULL)
68242f5224aeSachartre 			return (ENXIO);
68252f5224aeSachartre 		break;
68262f5224aeSachartre 
68272f5224aeSachartre 	case DKIOCGMEDIAINFO:
68282f5224aeSachartre 		if (vdc->minfo == NULL)
68292f5224aeSachartre 			return (ENXIO);
68302f5224aeSachartre 		if (vdc_check_capacity(vdc) != 0)
68312f5224aeSachartre 			/* disk capacity is not available */
68322f5224aeSachartre 			return (EIO);
68332f5224aeSachartre 		break;
68342f5224aeSachartre 	}
68352f5224aeSachartre 
68362f5224aeSachartre 	/*
68372f5224aeSachartre 	 * Deal with ioctls which require a processing different than
68382f5224aeSachartre 	 * converting ioctl arguments and sending a corresponding
68392f5224aeSachartre 	 * VD operation.
68402f5224aeSachartre 	 */
68412f5224aeSachartre 	switch (cmd) {
68422f5224aeSachartre 
68432f5224aeSachartre 	case USCSICMD:
68442f5224aeSachartre 	{
68452f5224aeSachartre 		return (vdc_uscsi_cmd(vdc, arg, mode));
68462f5224aeSachartre 	}
68472f5224aeSachartre 
68482f5224aeSachartre 	case MHIOCTKOWN:
68492f5224aeSachartre 	{
68502f5224aeSachartre 		mutex_enter(&vdc->ownership_lock);
68512f5224aeSachartre 		/*
68522f5224aeSachartre 		 * We have to set VDC_OWNERSHIP_WANTED now so that the ownership
68532f5224aeSachartre 		 * can be flagged with VDC_OWNERSHIP_RESET if the LDC is reset
68542f5224aeSachartre 		 * while we are processing the ioctl.
68552f5224aeSachartre 		 */
68562f5224aeSachartre 		vdc_ownership_update(vdc, VDC_OWNERSHIP_WANTED);
68572f5224aeSachartre 
68582f5224aeSachartre 		rv = vdc_access_set(vdc, VD_ACCESS_SET_EXCLUSIVE |
68592f5224aeSachartre 		    VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE, mode);
68602f5224aeSachartre 		if (rv == 0) {
68612f5224aeSachartre 			vdc_ownership_update(vdc, VDC_OWNERSHIP_WANTED |
68622f5224aeSachartre 			    VDC_OWNERSHIP_GRANTED);
68632f5224aeSachartre 		} else {
68642f5224aeSachartre 			vdc_ownership_update(vdc, VDC_OWNERSHIP_NONE);
68652f5224aeSachartre 		}
68662f5224aeSachartre 		mutex_exit(&vdc->ownership_lock);
68672f5224aeSachartre 		return (rv);
68682f5224aeSachartre 	}
68692f5224aeSachartre 
68702f5224aeSachartre 	case MHIOCRELEASE:
68712f5224aeSachartre 	{
68722f5224aeSachartre 		mutex_enter(&vdc->ownership_lock);
68732f5224aeSachartre 		rv = vdc_access_set(vdc, VD_ACCESS_SET_CLEAR, mode);
68742f5224aeSachartre 		if (rv == 0) {
68752f5224aeSachartre 			vdc_ownership_update(vdc, VDC_OWNERSHIP_NONE);
68762f5224aeSachartre 		}
68772f5224aeSachartre 		mutex_exit(&vdc->ownership_lock);
68782f5224aeSachartre 		return (rv);
68792f5224aeSachartre 	}
68802f5224aeSachartre 
68812f5224aeSachartre 	case MHIOCSTATUS:
68822f5224aeSachartre 	{
68832f5224aeSachartre 		uint64_t status;
68842f5224aeSachartre 
68852f5224aeSachartre 		rv = vdc_access_get(vdc, &status, mode);
68862f5224aeSachartre 		if (rv == 0 && rvalp != NULL)
68872f5224aeSachartre 			*rvalp = (status & VD_ACCESS_ALLOWED)? 0 : 1;
68882f5224aeSachartre 		return (rv);
68892f5224aeSachartre 	}
68902f5224aeSachartre 
68912f5224aeSachartre 	case MHIOCQRESERVE:
68922f5224aeSachartre 	{
68932f5224aeSachartre 		rv = vdc_access_set(vdc, VD_ACCESS_SET_EXCLUSIVE, mode);
68942f5224aeSachartre 		return (rv);
68952f5224aeSachartre 	}
68962f5224aeSachartre 
68972f5224aeSachartre 	case MHIOCGRP_INKEYS:
68982f5224aeSachartre 	{
68992f5224aeSachartre 		return (vdc_mhd_inkeys(vdc, arg, mode));
69002f5224aeSachartre 	}
69012f5224aeSachartre 
69022f5224aeSachartre 	case MHIOCGRP_INRESV:
69032f5224aeSachartre 	{
69042f5224aeSachartre 		return (vdc_mhd_inresv(vdc, arg, mode));
69052f5224aeSachartre 	}
69062f5224aeSachartre 
69072f5224aeSachartre 	case MHIOCGRP_REGISTER:
69082f5224aeSachartre 	{
69092f5224aeSachartre 		return (vdc_mhd_register(vdc, arg, mode));
69102f5224aeSachartre 	}
69112f5224aeSachartre 
69122f5224aeSachartre 	case MHIOCGRP_RESERVE:
69132f5224aeSachartre 	{
69142f5224aeSachartre 		return (vdc_mhd_reserve(vdc, arg, mode));
69152f5224aeSachartre 	}
69162f5224aeSachartre 
69172f5224aeSachartre 	case MHIOCGRP_PREEMPTANDABORT:
69182f5224aeSachartre 	{
69192f5224aeSachartre 		return (vdc_mhd_preemptabort(vdc, arg, mode));
69202f5224aeSachartre 	}
69212f5224aeSachartre 
69222f5224aeSachartre 	case MHIOCGRP_REGISTERANDIGNOREKEY:
69232f5224aeSachartre 	{
69242f5224aeSachartre 		return (vdc_mhd_registerignore(vdc, arg, mode));
69252f5224aeSachartre 	}
69262f5224aeSachartre 
69272f5224aeSachartre 	case MHIOCENFAILFAST:
69282f5224aeSachartre 	{
69292f5224aeSachartre 		rv = vdc_failfast(vdc, arg, mode);
69302f5224aeSachartre 		return (rv);
69312f5224aeSachartre 	}
69322f5224aeSachartre 
693387a7269eSachartre 	case DIOCTL_RWCMD:
693487a7269eSachartre 	{
693587a7269eSachartre 		return (vdc_dioctl_rwcmd(dev, arg, mode));
693687a7269eSachartre 	}
693787a7269eSachartre 
693887a7269eSachartre 	case DKIOCGAPART:
693987a7269eSachartre 	{
69409642afceSachartre 		return (vdc_dkio_gapart(vdc, arg, mode));
69419642afceSachartre 	}
69429642afceSachartre 
69439642afceSachartre 	case DKIOCPARTITION:
69449642afceSachartre 	{
69459642afceSachartre 		return (vdc_dkio_partition(vdc, arg, mode));
694687a7269eSachartre 	}
694787a7269eSachartre 
69481ae08745Sheppo 	case DKIOCINFO:
69491ae08745Sheppo 	{
69501ae08745Sheppo 		struct dk_cinfo	cinfo;
69511ae08745Sheppo 
69521ae08745Sheppo 		bcopy(vdc->cinfo, &cinfo, sizeof (struct dk_cinfo));
69530d0c8d4bSnarayan 		cinfo.dki_partition = VDCPART(dev);
69541ae08745Sheppo 
69551ae08745Sheppo 		rv = ddi_copyout(&cinfo, (void *)arg,
69561ae08745Sheppo 		    sizeof (struct dk_cinfo), mode);
69571ae08745Sheppo 		if (rv != 0)
69581ae08745Sheppo 			return (EFAULT);
69591ae08745Sheppo 
69601ae08745Sheppo 		return (0);
69611ae08745Sheppo 	}
69621ae08745Sheppo 
69631ae08745Sheppo 	case DKIOCGMEDIAINFO:
69648e6a2a04Slm66018 	{
69652f5224aeSachartre 		ASSERT(vdc->vdisk_size != 0);
69662f5224aeSachartre 		if (vdc->minfo->dki_capacity == 0)
69672f5224aeSachartre 			vdc->minfo->dki_capacity = vdc->vdisk_size;
69681ae08745Sheppo 		rv = ddi_copyout(vdc->minfo, (void *)arg,
69691ae08745Sheppo 		    sizeof (struct dk_minfo), mode);
69701ae08745Sheppo 		if (rv != 0)
69711ae08745Sheppo 			return (EFAULT);
69721ae08745Sheppo 
69731ae08745Sheppo 		return (0);
69741ae08745Sheppo 	}
69751ae08745Sheppo 
69768e6a2a04Slm66018 	case DKIOCFLUSHWRITECACHE:
69778e6a2a04Slm66018 		{
697817cadca8Slm66018 			struct dk_callback *dkc =
697917cadca8Slm66018 			    (struct dk_callback *)(uintptr_t)arg;
69808e6a2a04Slm66018 			vdc_dk_arg_t	*dkarg = NULL;
69818e6a2a04Slm66018 
69823af08d82Slm66018 			DMSG(vdc, 1, "[%d] Flush W$: mode %x\n",
69833af08d82Slm66018 			    instance, mode);
69848e6a2a04Slm66018 
69858e6a2a04Slm66018 			/*
69868e6a2a04Slm66018 			 * If arg is NULL, then there is no callback function
69878e6a2a04Slm66018 			 * registered and the call operates synchronously; we
69888e6a2a04Slm66018 			 * break and continue with the rest of the function and
69898e6a2a04Slm66018 			 * wait for vds to return (i.e. after the request to
69908e6a2a04Slm66018 			 * vds returns successfully, all writes completed prior
69918e6a2a04Slm66018 			 * to the ioctl will have been flushed from the disk
69928e6a2a04Slm66018 			 * write cache to persistent media.
69938e6a2a04Slm66018 			 *
69948e6a2a04Slm66018 			 * If a callback function is registered, we dispatch
69958e6a2a04Slm66018 			 * the request on a task queue and return immediately.
69968e6a2a04Slm66018 			 * The callback will deal with informing the calling
69978e6a2a04Slm66018 			 * thread that the flush request is completed.
69988e6a2a04Slm66018 			 */
69998e6a2a04Slm66018 			if (dkc == NULL)
70008e6a2a04Slm66018 				break;
70018e6a2a04Slm66018 
7002eff7243fSlm66018 			/*
7003eff7243fSlm66018 			 * the asynchronous callback is only supported if
7004eff7243fSlm66018 			 * invoked from within the kernel
7005eff7243fSlm66018 			 */
7006eff7243fSlm66018 			if ((mode & FKIOCTL) == 0)
7007eff7243fSlm66018 				return (ENOTSUP);
7008eff7243fSlm66018 
70098e6a2a04Slm66018 			dkarg = kmem_zalloc(sizeof (vdc_dk_arg_t), KM_SLEEP);
70108e6a2a04Slm66018 
70118e6a2a04Slm66018 			dkarg->mode = mode;
70128e6a2a04Slm66018 			dkarg->dev = dev;
70138e6a2a04Slm66018 			bcopy(dkc, &dkarg->dkc, sizeof (*dkc));
70148e6a2a04Slm66018 
70158e6a2a04Slm66018 			mutex_enter(&vdc->lock);
70168e6a2a04Slm66018 			vdc->dkio_flush_pending++;
70178e6a2a04Slm66018 			dkarg->vdc = vdc;
70188e6a2a04Slm66018 			mutex_exit(&vdc->lock);
70198e6a2a04Slm66018 
70208e6a2a04Slm66018 			/* put the request on a task queue */
70218e6a2a04Slm66018 			rv = taskq_dispatch(system_taskq, vdc_dkio_flush_cb,
70228e6a2a04Slm66018 			    (void *)dkarg, DDI_SLEEP);
70233af08d82Slm66018 			if (rv == NULL) {
70243af08d82Slm66018 				/* clean up if dispatch fails */
70253af08d82Slm66018 				mutex_enter(&vdc->lock);
70263af08d82Slm66018 				vdc->dkio_flush_pending--;
702778fcd0a1Sachartre 				mutex_exit(&vdc->lock);
70283af08d82Slm66018 				kmem_free(dkarg, sizeof (vdc_dk_arg_t));
70293af08d82Slm66018 			}
70308e6a2a04Slm66018 
70318e6a2a04Slm66018 			return (rv == NULL ? ENOMEM : 0);
70328e6a2a04Slm66018 		}
70338e6a2a04Slm66018 	}
70348e6a2a04Slm66018 
70351ae08745Sheppo 	/* catch programming error in vdc - should be a VD_OP_XXX ioctl */
70363af08d82Slm66018 	ASSERT(iop->op != 0);
70371ae08745Sheppo 
703817cadca8Slm66018 	/* check if the vDisk server handles the operation for this vDisk */
703917cadca8Slm66018 	if (VD_OP_SUPPORTED(vdc->operations, iop->op) == B_FALSE) {
704017cadca8Slm66018 		DMSG(vdc, 0, "[%d] Unsupported VD_OP operation (0x%x)\n",
704117cadca8Slm66018 		    vdc->instance, iop->op);
704217cadca8Slm66018 		return (ENOTSUP);
704317cadca8Slm66018 	}
704417cadca8Slm66018 
70451ae08745Sheppo 	/* LDC requires that the memory being mapped is 8-byte aligned */
70461ae08745Sheppo 	alloc_len = P2ROUNDUP(len, sizeof (uint64_t));
70473af08d82Slm66018 	DMSG(vdc, 1, "[%d] struct size %ld alloc %ld\n",
70483af08d82Slm66018 	    instance, len, alloc_len);
70491ae08745Sheppo 
7050eff7243fSlm66018 	if (alloc_len > 0)
70511ae08745Sheppo 		mem_p = kmem_zalloc(alloc_len, KM_SLEEP);
70521ae08745Sheppo 
70530a55fbb7Slm66018 	/*
7054eff7243fSlm66018 	 * Call the conversion function for this ioctl which, if necessary,
70550a55fbb7Slm66018 	 * converts from the Solaris format to the format ARC'ed
70560a55fbb7Slm66018 	 * as part of the vDisk protocol (FWARC 2006/195)
70570a55fbb7Slm66018 	 */
70583af08d82Slm66018 	ASSERT(iop->convert != NULL);
70593af08d82Slm66018 	rv = (iop->convert)(vdc, arg, mem_p, mode, VD_COPYIN);
70601ae08745Sheppo 	if (rv != 0) {
70613af08d82Slm66018 		DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n",
7062e1ebb9ecSlm66018 		    instance, rv, cmd);
70631ae08745Sheppo 		if (mem_p != NULL)
70641ae08745Sheppo 			kmem_free(mem_p, alloc_len);
70650a55fbb7Slm66018 		return (rv);
70661ae08745Sheppo 	}
70671ae08745Sheppo 
70681ae08745Sheppo 	/*
70691ae08745Sheppo 	 * send request to vds to service the ioctl.
70701ae08745Sheppo 	 */
70713af08d82Slm66018 	rv = vdc_do_sync_op(vdc, iop->op, mem_p, alloc_len,
70720d0c8d4bSnarayan 	    VDCPART(dev), 0, CB_SYNC, (void *)(uint64_t)mode,
70732f5224aeSachartre 	    VIO_both_dir, B_TRUE);
707478fcd0a1Sachartre 
70751ae08745Sheppo 	if (rv != 0) {
70761ae08745Sheppo 		/*
70771ae08745Sheppo 		 * This is not necessarily an error. The ioctl could
70781ae08745Sheppo 		 * be returning a value such as ENOTTY to indicate
70791ae08745Sheppo 		 * that the ioctl is not applicable.
70801ae08745Sheppo 		 */
70813af08d82Slm66018 		DMSG(vdc, 0, "[%d] vds returned %d for ioctl 0x%x\n",
7082e1ebb9ecSlm66018 		    instance, rv, cmd);
70831ae08745Sheppo 		if (mem_p != NULL)
70841ae08745Sheppo 			kmem_free(mem_p, alloc_len);
7085d10e4ef2Snarayan 
70861ae08745Sheppo 		return (rv);
70871ae08745Sheppo 	}
70881ae08745Sheppo 
70891ae08745Sheppo 	/*
70900a55fbb7Slm66018 	 * Call the conversion function (if it exists) for this ioctl
70910a55fbb7Slm66018 	 * which converts from the format ARC'ed as part of the vDisk
70920a55fbb7Slm66018 	 * protocol (FWARC 2006/195) back to a format understood by
70930a55fbb7Slm66018 	 * the rest of Solaris.
70941ae08745Sheppo 	 */
70953af08d82Slm66018 	rv = (iop->convert)(vdc, mem_p, arg, mode, VD_COPYOUT);
70960a55fbb7Slm66018 	if (rv != 0) {
70973af08d82Slm66018 		DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n",
7098e1ebb9ecSlm66018 		    instance, rv, cmd);
70991ae08745Sheppo 		if (mem_p != NULL)
71001ae08745Sheppo 			kmem_free(mem_p, alloc_len);
71010a55fbb7Slm66018 		return (rv);
71021ae08745Sheppo 	}
71031ae08745Sheppo 
71041ae08745Sheppo 	if (mem_p != NULL)
71051ae08745Sheppo 		kmem_free(mem_p, alloc_len);
71061ae08745Sheppo 
71071ae08745Sheppo 	return (rv);
71081ae08745Sheppo }
71091ae08745Sheppo 
71101ae08745Sheppo /*
71111ae08745Sheppo  * Function:
71120a55fbb7Slm66018  *
71130a55fbb7Slm66018  * Description:
71140a55fbb7Slm66018  *	This is an empty conversion function used by ioctl calls which
71150a55fbb7Slm66018  *	do not need to convert the data being passed in/out to userland
71160a55fbb7Slm66018  */
71170a55fbb7Slm66018 static int
7118d10e4ef2Snarayan vdc_null_copy_func(vdc_t *vdc, void *from, void *to, int mode, int dir)
71190a55fbb7Slm66018 {
7120d10e4ef2Snarayan 	_NOTE(ARGUNUSED(vdc))
71210a55fbb7Slm66018 	_NOTE(ARGUNUSED(from))
71220a55fbb7Slm66018 	_NOTE(ARGUNUSED(to))
71230a55fbb7Slm66018 	_NOTE(ARGUNUSED(mode))
71240a55fbb7Slm66018 	_NOTE(ARGUNUSED(dir))
71250a55fbb7Slm66018 
71260a55fbb7Slm66018 	return (0);
71270a55fbb7Slm66018 }
71280a55fbb7Slm66018 
71294bac2208Snarayan static int
71304bac2208Snarayan vdc_get_wce_convert(vdc_t *vdc, void *from, void *to,
71314bac2208Snarayan     int mode, int dir)
71324bac2208Snarayan {
71334bac2208Snarayan 	_NOTE(ARGUNUSED(vdc))
71344bac2208Snarayan 
71354bac2208Snarayan 	if (dir == VD_COPYIN)
71364bac2208Snarayan 		return (0);		/* nothing to do */
71374bac2208Snarayan 
71384bac2208Snarayan 	if (ddi_copyout(from, to, sizeof (int), mode) != 0)
71394bac2208Snarayan 		return (EFAULT);
71404bac2208Snarayan 
71414bac2208Snarayan 	return (0);
71424bac2208Snarayan }
71434bac2208Snarayan 
71444bac2208Snarayan static int
71454bac2208Snarayan vdc_set_wce_convert(vdc_t *vdc, void *from, void *to,
71464bac2208Snarayan     int mode, int dir)
71474bac2208Snarayan {
71484bac2208Snarayan 	_NOTE(ARGUNUSED(vdc))
71494bac2208Snarayan 
71504bac2208Snarayan 	if (dir == VD_COPYOUT)
71514bac2208Snarayan 		return (0);		/* nothing to do */
71524bac2208Snarayan 
71534bac2208Snarayan 	if (ddi_copyin(from, to, sizeof (int), mode) != 0)
71544bac2208Snarayan 		return (EFAULT);
71554bac2208Snarayan 
71564bac2208Snarayan 	return (0);
71574bac2208Snarayan }
71584bac2208Snarayan 
71590a55fbb7Slm66018 /*
71600a55fbb7Slm66018  * Function:
71610a55fbb7Slm66018  *	vdc_get_vtoc_convert()
71620a55fbb7Slm66018  *
71630a55fbb7Slm66018  * Description:
7164d10e4ef2Snarayan  *	This routine performs the necessary convertions from the DKIOCGVTOC
7165d10e4ef2Snarayan  *	Solaris structure to the format defined in FWARC 2006/195.
7166d10e4ef2Snarayan  *
7167d10e4ef2Snarayan  *	In the struct vtoc definition, the timestamp field is marked as not
7168d10e4ef2Snarayan  *	supported so it is not part of vDisk protocol (FWARC 2006/195).
7169d10e4ef2Snarayan  *	However SVM uses that field to check it can write into the VTOC,
7170d10e4ef2Snarayan  *	so we fake up the info of that field.
71710a55fbb7Slm66018  *
71720a55fbb7Slm66018  * Arguments:
7173d10e4ef2Snarayan  *	vdc	- the vDisk client
71740a55fbb7Slm66018  *	from	- the buffer containing the data to be copied from
71750a55fbb7Slm66018  *	to	- the buffer to be copied to
71760a55fbb7Slm66018  *	mode	- flags passed to ioctl() call
71770a55fbb7Slm66018  *	dir	- the "direction" of the copy - VD_COPYIN or VD_COPYOUT
71780a55fbb7Slm66018  *
71790a55fbb7Slm66018  * Return Code:
71800a55fbb7Slm66018  *	0	- Success
71810a55fbb7Slm66018  *	ENXIO	- incorrect buffer passed in.
7182d10e4ef2Snarayan  *	EFAULT	- ddi_copyout routine encountered an error.
71830a55fbb7Slm66018  */
71840a55fbb7Slm66018 static int
7185d10e4ef2Snarayan vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
71860a55fbb7Slm66018 {
7187d10e4ef2Snarayan 	int		i;
71880a55fbb7Slm66018 	void		*tmp_mem = NULL;
71890a55fbb7Slm66018 	void		*tmp_memp;
71900a55fbb7Slm66018 	struct vtoc	vt;
71910a55fbb7Slm66018 	struct vtoc32	vt32;
71920a55fbb7Slm66018 	int		copy_len = 0;
71930a55fbb7Slm66018 	int		rv = 0;
71940a55fbb7Slm66018 
71950a55fbb7Slm66018 	if (dir != VD_COPYOUT)
71960a55fbb7Slm66018 		return (0);	/* nothing to do */
71970a55fbb7Slm66018 
71980a55fbb7Slm66018 	if ((from == NULL) || (to == NULL))
71990a55fbb7Slm66018 		return (ENXIO);
72000a55fbb7Slm66018 
72010a55fbb7Slm66018 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32)
72020a55fbb7Slm66018 		copy_len = sizeof (struct vtoc32);
72030a55fbb7Slm66018 	else
72040a55fbb7Slm66018 		copy_len = sizeof (struct vtoc);
72050a55fbb7Slm66018 
72060a55fbb7Slm66018 	tmp_mem = kmem_alloc(copy_len, KM_SLEEP);
72070a55fbb7Slm66018 
72080a55fbb7Slm66018 	VD_VTOC2VTOC((vd_vtoc_t *)from, &vt);
7209d10e4ef2Snarayan 
7210d10e4ef2Snarayan 	/* fake the VTOC timestamp field */
7211d10e4ef2Snarayan 	for (i = 0; i < V_NUMPAR; i++) {
7212d10e4ef2Snarayan 		vt.timestamp[i] = vdc->vtoc->timestamp[i];
7213d10e4ef2Snarayan 	}
7214d10e4ef2Snarayan 
72150a55fbb7Slm66018 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
721617cadca8Slm66018 		/* LINTED E_ASSIGN_NARROW_CONV */
72170a55fbb7Slm66018 		vtoctovtoc32(vt, vt32);
72180a55fbb7Slm66018 		tmp_memp = &vt32;
72190a55fbb7Slm66018 	} else {
72200a55fbb7Slm66018 		tmp_memp = &vt;
72210a55fbb7Slm66018 	}
72220a55fbb7Slm66018 	rv = ddi_copyout(tmp_memp, to, copy_len, mode);
72230a55fbb7Slm66018 	if (rv != 0)
72240a55fbb7Slm66018 		rv = EFAULT;
72250a55fbb7Slm66018 
72260a55fbb7Slm66018 	kmem_free(tmp_mem, copy_len);
72270a55fbb7Slm66018 	return (rv);
72280a55fbb7Slm66018 }
72290a55fbb7Slm66018 
72300a55fbb7Slm66018 /*
72310a55fbb7Slm66018  * Function:
72320a55fbb7Slm66018  *	vdc_set_vtoc_convert()
72330a55fbb7Slm66018  *
72340a55fbb7Slm66018  * Description:
7235d10e4ef2Snarayan  *	This routine performs the necessary convertions from the DKIOCSVTOC
7236d10e4ef2Snarayan  *	Solaris structure to the format defined in FWARC 2006/195.
72370a55fbb7Slm66018  *
72380a55fbb7Slm66018  * Arguments:
7239d10e4ef2Snarayan  *	vdc	- the vDisk client
72400a55fbb7Slm66018  *	from	- Buffer with data
72410a55fbb7Slm66018  *	to	- Buffer where data is to be copied to
72420a55fbb7Slm66018  *	mode	- flags passed to ioctl
72430a55fbb7Slm66018  *	dir	- direction of copy (in or out)
72440a55fbb7Slm66018  *
72450a55fbb7Slm66018  * Return Code:
72460a55fbb7Slm66018  *	0	- Success
72470a55fbb7Slm66018  *	ENXIO	- Invalid buffer passed in
72480a55fbb7Slm66018  *	EFAULT	- ddi_copyin of data failed
72490a55fbb7Slm66018  */
72500a55fbb7Slm66018 static int
7251d10e4ef2Snarayan vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
72520a55fbb7Slm66018 {
725378fcd0a1Sachartre 	_NOTE(ARGUNUSED(vdc))
725478fcd0a1Sachartre 
72552f5224aeSachartre 	void		*tmp_mem = NULL, *uvtoc;
72560a55fbb7Slm66018 	struct vtoc	vt;
72570a55fbb7Slm66018 	struct vtoc	*vtp = &vt;
72580a55fbb7Slm66018 	vd_vtoc_t	vtvd;
72590a55fbb7Slm66018 	int		copy_len = 0;
72602f5224aeSachartre 	int		i, rv = 0;
72610a55fbb7Slm66018 
72620a55fbb7Slm66018 	if ((from == NULL) || (to == NULL))
72630a55fbb7Slm66018 		return (ENXIO);
72640a55fbb7Slm66018 
72652f5224aeSachartre 	if (dir == VD_COPYIN)
72662f5224aeSachartre 		uvtoc = from;
72672f5224aeSachartre 	else
72682f5224aeSachartre 		uvtoc = to;
72692f5224aeSachartre 
72700a55fbb7Slm66018 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32)
72710a55fbb7Slm66018 		copy_len = sizeof (struct vtoc32);
72720a55fbb7Slm66018 	else
72730a55fbb7Slm66018 		copy_len = sizeof (struct vtoc);
72740a55fbb7Slm66018 
72750a55fbb7Slm66018 	tmp_mem = kmem_alloc(copy_len, KM_SLEEP);
72760a55fbb7Slm66018 
72772f5224aeSachartre 	rv = ddi_copyin(uvtoc, tmp_mem, copy_len, mode);
72780a55fbb7Slm66018 	if (rv != 0) {
72790a55fbb7Slm66018 		kmem_free(tmp_mem, copy_len);
72800a55fbb7Slm66018 		return (EFAULT);
72810a55fbb7Slm66018 	}
72820a55fbb7Slm66018 
72830a55fbb7Slm66018 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
72840a55fbb7Slm66018 		vtoc32tovtoc((*(struct vtoc32 *)tmp_mem), vt);
72850a55fbb7Slm66018 	} else {
72860a55fbb7Slm66018 		vtp = tmp_mem;
72870a55fbb7Slm66018 	}
72880a55fbb7Slm66018 
72892f5224aeSachartre 	if (dir == VD_COPYOUT) {
72902f5224aeSachartre 		/*
72912f5224aeSachartre 		 * The disk label may have changed. Revalidate the disk
72922f5224aeSachartre 		 * geometry. This will also update the device nodes and
72932f5224aeSachartre 		 * properties.
72942f5224aeSachartre 		 */
72952f5224aeSachartre 		vdc_validate(vdc);
72962f5224aeSachartre 
72972f5224aeSachartre 		/*
72982f5224aeSachartre 		 * We also need to keep track of the timestamp fields.
72992f5224aeSachartre 		 */
73002f5224aeSachartre 		for (i = 0; i < V_NUMPAR; i++) {
73012f5224aeSachartre 			vdc->vtoc->timestamp[i] = vtp->timestamp[i];
73022f5224aeSachartre 		}
73032f5224aeSachartre 
73042f5224aeSachartre 		return (0);
73052f5224aeSachartre 	}
73062f5224aeSachartre 
73070a55fbb7Slm66018 	VTOC2VD_VTOC(vtp, &vtvd);
73080a55fbb7Slm66018 	bcopy(&vtvd, to, sizeof (vd_vtoc_t));
73090a55fbb7Slm66018 	kmem_free(tmp_mem, copy_len);
73100a55fbb7Slm66018 
73110a55fbb7Slm66018 	return (0);
73120a55fbb7Slm66018 }
73130a55fbb7Slm66018 
73140a55fbb7Slm66018 /*
73150a55fbb7Slm66018  * Function:
73160a55fbb7Slm66018  *	vdc_get_geom_convert()
73170a55fbb7Slm66018  *
73180a55fbb7Slm66018  * Description:
7319d10e4ef2Snarayan  *	This routine performs the necessary convertions from the DKIOCGGEOM,
7320d10e4ef2Snarayan  *	DKIOCG_PHYSGEOM and DKIOG_VIRTGEOM Solaris structures to the format
7321d10e4ef2Snarayan  *	defined in FWARC 2006/195
73220a55fbb7Slm66018  *
73230a55fbb7Slm66018  * Arguments:
7324d10e4ef2Snarayan  *	vdc	- the vDisk client
73250a55fbb7Slm66018  *	from	- Buffer with data
73260a55fbb7Slm66018  *	to	- Buffer where data is to be copied to
73270a55fbb7Slm66018  *	mode	- flags passed to ioctl
73280a55fbb7Slm66018  *	dir	- direction of copy (in or out)
73290a55fbb7Slm66018  *
73300a55fbb7Slm66018  * Return Code:
73310a55fbb7Slm66018  *	0	- Success
73320a55fbb7Slm66018  *	ENXIO	- Invalid buffer passed in
7333d10e4ef2Snarayan  *	EFAULT	- ddi_copyout of data failed
73340a55fbb7Slm66018  */
73350a55fbb7Slm66018 static int
7336d10e4ef2Snarayan vdc_get_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
73370a55fbb7Slm66018 {
7338d10e4ef2Snarayan 	_NOTE(ARGUNUSED(vdc))
7339d10e4ef2Snarayan 
73400a55fbb7Slm66018 	struct dk_geom	geom;
73410a55fbb7Slm66018 	int	copy_len = sizeof (struct dk_geom);
73420a55fbb7Slm66018 	int	rv = 0;
73430a55fbb7Slm66018 
73440a55fbb7Slm66018 	if (dir != VD_COPYOUT)
73450a55fbb7Slm66018 		return (0);	/* nothing to do */
73460a55fbb7Slm66018 
73470a55fbb7Slm66018 	if ((from == NULL) || (to == NULL))
73480a55fbb7Slm66018 		return (ENXIO);
73490a55fbb7Slm66018 
73500a55fbb7Slm66018 	VD_GEOM2DK_GEOM((vd_geom_t *)from, &geom);
73510a55fbb7Slm66018 	rv = ddi_copyout(&geom, to, copy_len, mode);
73520a55fbb7Slm66018 	if (rv != 0)
73530a55fbb7Slm66018 		rv = EFAULT;
73540a55fbb7Slm66018 
73550a55fbb7Slm66018 	return (rv);
73560a55fbb7Slm66018 }
73570a55fbb7Slm66018 
73580a55fbb7Slm66018 /*
73590a55fbb7Slm66018  * Function:
73600a55fbb7Slm66018  *	vdc_set_geom_convert()
73610a55fbb7Slm66018  *
73620a55fbb7Slm66018  * Description:
7363d10e4ef2Snarayan  *	This routine performs the necessary convertions from the DKIOCSGEOM
7364d10e4ef2Snarayan  *	Solaris structure to the format defined in FWARC 2006/195.
73650a55fbb7Slm66018  *
73660a55fbb7Slm66018  * Arguments:
7367d10e4ef2Snarayan  *	vdc	- the vDisk client
73680a55fbb7Slm66018  *	from	- Buffer with data
73690a55fbb7Slm66018  *	to	- Buffer where data is to be copied to
73700a55fbb7Slm66018  *	mode	- flags passed to ioctl
73710a55fbb7Slm66018  *	dir	- direction of copy (in or out)
73720a55fbb7Slm66018  *
73730a55fbb7Slm66018  * Return Code:
73740a55fbb7Slm66018  *	0	- Success
73750a55fbb7Slm66018  *	ENXIO	- Invalid buffer passed in
73760a55fbb7Slm66018  *	EFAULT	- ddi_copyin of data failed
73770a55fbb7Slm66018  */
73780a55fbb7Slm66018 static int
7379d10e4ef2Snarayan vdc_set_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
73800a55fbb7Slm66018 {
7381d10e4ef2Snarayan 	_NOTE(ARGUNUSED(vdc))
7382d10e4ef2Snarayan 
73830a55fbb7Slm66018 	vd_geom_t	vdgeom;
73840a55fbb7Slm66018 	void		*tmp_mem = NULL;
73850a55fbb7Slm66018 	int		copy_len = sizeof (struct dk_geom);
73860a55fbb7Slm66018 	int		rv = 0;
73870a55fbb7Slm66018 
73880a55fbb7Slm66018 	if (dir != VD_COPYIN)
73890a55fbb7Slm66018 		return (0);	/* nothing to do */
73900a55fbb7Slm66018 
73910a55fbb7Slm66018 	if ((from == NULL) || (to == NULL))
73920a55fbb7Slm66018 		return (ENXIO);
73930a55fbb7Slm66018 
73940a55fbb7Slm66018 	tmp_mem = kmem_alloc(copy_len, KM_SLEEP);
73950a55fbb7Slm66018 
73960a55fbb7Slm66018 	rv = ddi_copyin(from, tmp_mem, copy_len, mode);
73970a55fbb7Slm66018 	if (rv != 0) {
73980a55fbb7Slm66018 		kmem_free(tmp_mem, copy_len);
73990a55fbb7Slm66018 		return (EFAULT);
74000a55fbb7Slm66018 	}
74010a55fbb7Slm66018 	DK_GEOM2VD_GEOM((struct dk_geom *)tmp_mem, &vdgeom);
74020a55fbb7Slm66018 	bcopy(&vdgeom, to, sizeof (vdgeom));
74030a55fbb7Slm66018 	kmem_free(tmp_mem, copy_len);
74040a55fbb7Slm66018 
74050a55fbb7Slm66018 	return (0);
74060a55fbb7Slm66018 }
74070a55fbb7Slm66018 
74084bac2208Snarayan static int
74094bac2208Snarayan vdc_get_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
74104bac2208Snarayan {
74114bac2208Snarayan 	_NOTE(ARGUNUSED(vdc))
74124bac2208Snarayan 
74134bac2208Snarayan 	vd_efi_t	*vd_efi;
74144bac2208Snarayan 	dk_efi_t	dk_efi;
74154bac2208Snarayan 	int		rv = 0;
74164bac2208Snarayan 	void		*uaddr;
74174bac2208Snarayan 
74184bac2208Snarayan 	if ((from == NULL) || (to == NULL))
74194bac2208Snarayan 		return (ENXIO);
74204bac2208Snarayan 
74214bac2208Snarayan 	if (dir == VD_COPYIN) {
74224bac2208Snarayan 
74234bac2208Snarayan 		vd_efi = (vd_efi_t *)to;
74244bac2208Snarayan 
74254bac2208Snarayan 		rv = ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode);
74264bac2208Snarayan 		if (rv != 0)
74274bac2208Snarayan 			return (EFAULT);
74284bac2208Snarayan 
74294bac2208Snarayan 		vd_efi->lba = dk_efi.dki_lba;
74304bac2208Snarayan 		vd_efi->length = dk_efi.dki_length;
74314bac2208Snarayan 		bzero(vd_efi->data, vd_efi->length);
74324bac2208Snarayan 
74334bac2208Snarayan 	} else {
74344bac2208Snarayan 
74354bac2208Snarayan 		rv = ddi_copyin(to, &dk_efi, sizeof (dk_efi_t), mode);
74364bac2208Snarayan 		if (rv != 0)
74374bac2208Snarayan 			return (EFAULT);
74384bac2208Snarayan 
74394bac2208Snarayan 		uaddr = dk_efi.dki_data;
74404bac2208Snarayan 
74414bac2208Snarayan 		dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP);
74424bac2208Snarayan 
74434bac2208Snarayan 		VD_EFI2DK_EFI((vd_efi_t *)from, &dk_efi);
74444bac2208Snarayan 
74454bac2208Snarayan 		rv = ddi_copyout(dk_efi.dki_data, uaddr, dk_efi.dki_length,
74464bac2208Snarayan 		    mode);
74474bac2208Snarayan 		if (rv != 0)
74484bac2208Snarayan 			return (EFAULT);
74494bac2208Snarayan 
74504bac2208Snarayan 		kmem_free(dk_efi.dki_data, dk_efi.dki_length);
74514bac2208Snarayan 	}
74524bac2208Snarayan 
74534bac2208Snarayan 	return (0);
74544bac2208Snarayan }
74554bac2208Snarayan 
74564bac2208Snarayan static int
74574bac2208Snarayan vdc_set_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
74584bac2208Snarayan {
74594bac2208Snarayan 	_NOTE(ARGUNUSED(vdc))
74604bac2208Snarayan 
74614bac2208Snarayan 	dk_efi_t	dk_efi;
74624bac2208Snarayan 	void		*uaddr;
74634bac2208Snarayan 
74642f5224aeSachartre 	if (dir == VD_COPYOUT) {
74652f5224aeSachartre 		/*
74662f5224aeSachartre 		 * The disk label may have changed. Revalidate the disk
74672f5224aeSachartre 		 * geometry. This will also update the device nodes and
74682f5224aeSachartre 		 * properties.
74692f5224aeSachartre 		 */
74702f5224aeSachartre 		vdc_validate(vdc);
74712f5224aeSachartre 		return (0);
74722f5224aeSachartre 	}
74734bac2208Snarayan 
74744bac2208Snarayan 	if ((from == NULL) || (to == NULL))
74754bac2208Snarayan 		return (ENXIO);
74764bac2208Snarayan 
74774bac2208Snarayan 	if (ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode) != 0)
74784bac2208Snarayan 		return (EFAULT);
74794bac2208Snarayan 
74804bac2208Snarayan 	uaddr = dk_efi.dki_data;
74814bac2208Snarayan 
74824bac2208Snarayan 	dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP);
74834bac2208Snarayan 
74844bac2208Snarayan 	if (ddi_copyin(uaddr, dk_efi.dki_data, dk_efi.dki_length, mode) != 0)
74854bac2208Snarayan 		return (EFAULT);
74864bac2208Snarayan 
74874bac2208Snarayan 	DK_EFI2VD_EFI(&dk_efi, (vd_efi_t *)to);
74884bac2208Snarayan 
74894bac2208Snarayan 	kmem_free(dk_efi.dki_data, dk_efi.dki_length);
74904bac2208Snarayan 
74914bac2208Snarayan 	return (0);
74924bac2208Snarayan }
74934bac2208Snarayan 
749417cadca8Slm66018 
749517cadca8Slm66018 /* -------------------------------------------------------------------------- */
749617cadca8Slm66018 
74970a55fbb7Slm66018 /*
74980a55fbb7Slm66018  * Function:
74991ae08745Sheppo  *	vdc_create_fake_geometry()
75001ae08745Sheppo  *
75011ae08745Sheppo  * Description:
750217cadca8Slm66018  *	This routine fakes up the disk info needed for some DKIO ioctls such
750317cadca8Slm66018  *	as DKIOCINFO and DKIOCGMEDIAINFO [just like lofi(7D) and ramdisk(7D) do]
75041ae08745Sheppo  *
750517cadca8Slm66018  *	Note: This function must not be called until the vDisk attributes have
750617cadca8Slm66018  *	been exchanged as part of the handshake with the vDisk server.
75071ae08745Sheppo  *
75081ae08745Sheppo  * Arguments:
75091ae08745Sheppo  *	vdc	- soft state pointer for this instance of the device driver.
75101ae08745Sheppo  *
75111ae08745Sheppo  * Return Code:
751278fcd0a1Sachartre  *	none.
75131ae08745Sheppo  */
751478fcd0a1Sachartre static void
75151ae08745Sheppo vdc_create_fake_geometry(vdc_t *vdc)
75161ae08745Sheppo {
75171ae08745Sheppo 	ASSERT(vdc != NULL);
751878fcd0a1Sachartre 	ASSERT(vdc->max_xfer_sz != 0);
75190d0c8d4bSnarayan 
75200d0c8d4bSnarayan 	/*
75211ae08745Sheppo 	 * DKIOCINFO support
75221ae08745Sheppo 	 */
752378fcd0a1Sachartre 	if (vdc->cinfo == NULL)
75241ae08745Sheppo 		vdc->cinfo = kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
75251ae08745Sheppo 
75261ae08745Sheppo 	(void) strcpy(vdc->cinfo->dki_cname, VDC_DRIVER_NAME);
75271ae08745Sheppo 	(void) strcpy(vdc->cinfo->dki_dname, VDC_DRIVER_NAME);
75288e6a2a04Slm66018 	/* max_xfer_sz is #blocks so we don't need to divide by DEV_BSIZE */
75298e6a2a04Slm66018 	vdc->cinfo->dki_maxtransfer = vdc->max_xfer_sz;
75302f5224aeSachartre 
753187a7269eSachartre 	/*
75322f5224aeSachartre 	 * We set the controller type to DKC_SCSI_CCS only if the VD_OP_SCSICMD
75332f5224aeSachartre 	 * operation is supported, otherwise the controller type is DKC_DIRECT.
75342f5224aeSachartre 	 * Version 1.0 does not support the VD_OP_SCSICMD operation, so the
75352f5224aeSachartre 	 * controller type is always DKC_DIRECT in that case.
75362f5224aeSachartre 	 *
753717cadca8Slm66018 	 * If the virtual disk is backed by a physical CD/DVD device or
753817cadca8Slm66018 	 * an ISO image, modify the controller type to indicate this
753987a7269eSachartre 	 */
754017cadca8Slm66018 	switch (vdc->vdisk_media) {
754117cadca8Slm66018 	case VD_MEDIA_CD:
754217cadca8Slm66018 	case VD_MEDIA_DVD:
754317cadca8Slm66018 		vdc->cinfo->dki_ctype = DKC_CDROM;
754417cadca8Slm66018 		break;
754517cadca8Slm66018 	case VD_MEDIA_FIXED:
75462f5224aeSachartre 		if (VD_OP_SUPPORTED(vdc->operations, VD_OP_SCSICMD))
75472f5224aeSachartre 			vdc->cinfo->dki_ctype = DKC_SCSI_CCS;
75482f5224aeSachartre 		else
754987a7269eSachartre 			vdc->cinfo->dki_ctype = DKC_DIRECT;
755017cadca8Slm66018 		break;
755117cadca8Slm66018 	default:
755217cadca8Slm66018 		/* in the case of v1.0 we default to a fixed disk */
755317cadca8Slm66018 		vdc->cinfo->dki_ctype = DKC_DIRECT;
755417cadca8Slm66018 		break;
755517cadca8Slm66018 	}
75561ae08745Sheppo 	vdc->cinfo->dki_flags = DKI_FMTVOL;
75571ae08745Sheppo 	vdc->cinfo->dki_cnum = 0;
75581ae08745Sheppo 	vdc->cinfo->dki_addr = 0;
75591ae08745Sheppo 	vdc->cinfo->dki_space = 0;
75601ae08745Sheppo 	vdc->cinfo->dki_prio = 0;
75611ae08745Sheppo 	vdc->cinfo->dki_vec = 0;
75621ae08745Sheppo 	vdc->cinfo->dki_unit = vdc->instance;
75631ae08745Sheppo 	vdc->cinfo->dki_slave = 0;
75641ae08745Sheppo 	/*
75651ae08745Sheppo 	 * The partition number will be created on the fly depending on the
75661ae08745Sheppo 	 * actual slice (i.e. minor node) that is used to request the data.
75671ae08745Sheppo 	 */
75681ae08745Sheppo 	vdc->cinfo->dki_partition = 0;
75691ae08745Sheppo 
75701ae08745Sheppo 	/*
75711ae08745Sheppo 	 * DKIOCGMEDIAINFO support
75721ae08745Sheppo 	 */
75730a55fbb7Slm66018 	if (vdc->minfo == NULL)
75741ae08745Sheppo 		vdc->minfo = kmem_zalloc(sizeof (struct dk_minfo), KM_SLEEP);
757517cadca8Slm66018 
757617cadca8Slm66018 	if (vio_ver_is_supported(vdc->ver, 1, 1)) {
757717cadca8Slm66018 		vdc->minfo->dki_media_type =
757817cadca8Slm66018 		    VD_MEDIATYPE2DK_MEDIATYPE(vdc->vdisk_media);
757917cadca8Slm66018 	} else {
75801ae08745Sheppo 		vdc->minfo->dki_media_type = DK_FIXED_DISK;
758117cadca8Slm66018 	}
758217cadca8Slm66018 
75834bac2208Snarayan 	vdc->minfo->dki_capacity = vdc->vdisk_size;
758417cadca8Slm66018 	vdc->minfo->dki_lbsize = vdc->block_size;
758578fcd0a1Sachartre }
75861ae08745Sheppo 
758778fcd0a1Sachartre static ushort_t
758878fcd0a1Sachartre vdc_lbl2cksum(struct dk_label *label)
758978fcd0a1Sachartre {
759078fcd0a1Sachartre 	int	count;
759178fcd0a1Sachartre 	ushort_t sum, *sp;
759278fcd0a1Sachartre 
759378fcd0a1Sachartre 	count =	(sizeof (struct dk_label)) / (sizeof (short)) - 1;
759478fcd0a1Sachartre 	sp = (ushort_t *)label;
759578fcd0a1Sachartre 	sum = 0;
759678fcd0a1Sachartre 	while (count--) {
759778fcd0a1Sachartre 		sum ^= *sp++;
759878fcd0a1Sachartre 	}
759978fcd0a1Sachartre 
760078fcd0a1Sachartre 	return (sum);
76010a55fbb7Slm66018 }
76020a55fbb7Slm66018 
76030a55fbb7Slm66018 /*
76040a55fbb7Slm66018  * Function:
760578fcd0a1Sachartre  *	vdc_validate_geometry
76060a55fbb7Slm66018  *
76070a55fbb7Slm66018  * Description:
760878fcd0a1Sachartre  *	This routine discovers the label and geometry of the disk. It stores
760978fcd0a1Sachartre  *	the disk label and related information in the vdc structure. If it
761078fcd0a1Sachartre  *	fails to validate the geometry or to discover the disk label then
761178fcd0a1Sachartre  *	the label is marked as unknown (VD_DISK_LABEL_UNK).
76120a55fbb7Slm66018  *
76130a55fbb7Slm66018  * Arguments:
76140a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
76150a55fbb7Slm66018  *
76160a55fbb7Slm66018  * Return Code:
761778fcd0a1Sachartre  *	0	- success.
761878fcd0a1Sachartre  *	EINVAL	- unknown disk label.
761978fcd0a1Sachartre  *	ENOTSUP	- geometry not applicable (EFI label).
762078fcd0a1Sachartre  *	EIO	- error accessing the disk.
76210a55fbb7Slm66018  */
76220a55fbb7Slm66018 static int
762378fcd0a1Sachartre vdc_validate_geometry(vdc_t *vdc)
76240a55fbb7Slm66018 {
7625d10e4ef2Snarayan 	buf_t	*buf;	/* BREAD requests need to be in a buf_t structure */
76260a55fbb7Slm66018 	dev_t	dev;
76272f5224aeSachartre 	int	rv, rval;
762878fcd0a1Sachartre 	struct dk_label label;
762978fcd0a1Sachartre 	struct dk_geom geom;
763078fcd0a1Sachartre 	struct vtoc vtoc;
7631edcc0754Sachartre 	efi_gpt_t *gpt;
7632edcc0754Sachartre 	efi_gpe_t *gpe;
7633edcc0754Sachartre 	vd_efi_dev_t edev;
76340a55fbb7Slm66018 
76350a55fbb7Slm66018 	ASSERT(vdc != NULL);
763678fcd0a1Sachartre 	ASSERT(vdc->vtoc != NULL && vdc->geom != NULL);
763778fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
76380a55fbb7Slm66018 
763978fcd0a1Sachartre 	mutex_exit(&vdc->lock);
76400a55fbb7Slm66018 
76410a55fbb7Slm66018 	dev = makedevice(ddi_driver_major(vdc->dip),
76420a55fbb7Slm66018 	    VD_MAKE_DEV(vdc->instance, 0));
76434bac2208Snarayan 
76442f5224aeSachartre 	rv = vd_process_ioctl(dev, DKIOCGGEOM, (caddr_t)&geom, FKIOCTL, &rval);
764578fcd0a1Sachartre 	if (rv == 0)
76462f5224aeSachartre 		rv = vd_process_ioctl(dev, DKIOCGVTOC, (caddr_t)&vtoc,
76472f5224aeSachartre 		    FKIOCTL, &rval);
76480d0c8d4bSnarayan 
76494bac2208Snarayan 	if (rv == ENOTSUP) {
76504bac2208Snarayan 		/*
76514bac2208Snarayan 		 * If the device does not support VTOC then we try
76524bac2208Snarayan 		 * to read an EFI label.
7653edcc0754Sachartre 		 *
7654edcc0754Sachartre 		 * We need to know the block size and the disk size to
7655edcc0754Sachartre 		 * be able to read an EFI label.
76564bac2208Snarayan 		 */
7657edcc0754Sachartre 		if (vdc->vdisk_size == 0) {
7658edcc0754Sachartre 			if ((rv = vdc_check_capacity(vdc)) != 0) {
7659edcc0754Sachartre 				mutex_enter(&vdc->lock);
7660edcc0754Sachartre 				vdc_store_label_unk(vdc);
7661edcc0754Sachartre 				return (rv);
7662edcc0754Sachartre 			}
7663edcc0754Sachartre 		}
76644bac2208Snarayan 
7665edcc0754Sachartre 		VD_EFI_DEV_SET(edev, vdc, vd_process_efi_ioctl);
7666edcc0754Sachartre 
7667edcc0754Sachartre 		rv = vd_efi_alloc_and_read(&edev, &gpt, &gpe);
76684bac2208Snarayan 
76694bac2208Snarayan 		if (rv) {
76703af08d82Slm66018 			DMSG(vdc, 0, "[%d] Failed to get EFI (err=%d)",
76714bac2208Snarayan 			    vdc->instance, rv);
767278fcd0a1Sachartre 			mutex_enter(&vdc->lock);
767378fcd0a1Sachartre 			vdc_store_label_unk(vdc);
767478fcd0a1Sachartre 			return (EIO);
767578fcd0a1Sachartre 		}
767678fcd0a1Sachartre 
767778fcd0a1Sachartre 		mutex_enter(&vdc->lock);
7678edcc0754Sachartre 		vdc_store_label_efi(vdc, gpt, gpe);
7679edcc0754Sachartre 		vd_efi_free(&edev, gpt, gpe);
768078fcd0a1Sachartre 		return (ENOTSUP);
768178fcd0a1Sachartre 	}
768278fcd0a1Sachartre 
768378fcd0a1Sachartre 	if (rv != 0) {
768478fcd0a1Sachartre 		DMSG(vdc, 0, "[%d] Failed to get VTOC (err=%d)",
768578fcd0a1Sachartre 		    vdc->instance, rv);
768678fcd0a1Sachartre 		mutex_enter(&vdc->lock);
768778fcd0a1Sachartre 		vdc_store_label_unk(vdc);
768878fcd0a1Sachartre 		if (rv != EINVAL)
768978fcd0a1Sachartre 			rv = EIO;
76904bac2208Snarayan 		return (rv);
76914bac2208Snarayan 	}
76924bac2208Snarayan 
769378fcd0a1Sachartre 	/* check that geometry and vtoc are valid */
769478fcd0a1Sachartre 	if (geom.dkg_nhead == 0 || geom.dkg_nsect == 0 ||
769578fcd0a1Sachartre 	    vtoc.v_sanity != VTOC_SANE) {
769678fcd0a1Sachartre 		mutex_enter(&vdc->lock);
769778fcd0a1Sachartre 		vdc_store_label_unk(vdc);
769878fcd0a1Sachartre 		return (EINVAL);
769978fcd0a1Sachartre 	}
77004bac2208Snarayan 
770178fcd0a1Sachartre 	/*
770278fcd0a1Sachartre 	 * We have a disk and a valid VTOC. However this does not mean
770378fcd0a1Sachartre 	 * that the disk currently have a VTOC label. The returned VTOC may
770478fcd0a1Sachartre 	 * be a default VTOC to be used for configuring the disk (this is
770578fcd0a1Sachartre 	 * what is done for disk image). So we read the label from the
770678fcd0a1Sachartre 	 * beginning of the disk to ensure we really have a VTOC label.
770778fcd0a1Sachartre 	 *
770878fcd0a1Sachartre 	 * FUTURE: This could be the default way for reading the VTOC
770978fcd0a1Sachartre 	 * from the disk as opposed to sending the VD_OP_GET_VTOC
771078fcd0a1Sachartre 	 * to the server. This will be the default if vdc is implemented
771178fcd0a1Sachartre 	 * ontop of cmlb.
771278fcd0a1Sachartre 	 */
771378fcd0a1Sachartre 
771478fcd0a1Sachartre 	/*
771578fcd0a1Sachartre 	 * Single slice disk does not support read using an absolute disk
771678fcd0a1Sachartre 	 * offset so we just rely on the DKIOCGVTOC ioctl in that case.
771778fcd0a1Sachartre 	 */
771878fcd0a1Sachartre 	if (vdc->vdisk_type == VD_DISK_TYPE_SLICE) {
771978fcd0a1Sachartre 		mutex_enter(&vdc->lock);
772078fcd0a1Sachartre 		if (vtoc.v_nparts != 1) {
772178fcd0a1Sachartre 			vdc_store_label_unk(vdc);
772278fcd0a1Sachartre 			return (EINVAL);
772378fcd0a1Sachartre 		}
772478fcd0a1Sachartre 		vdc_store_label_vtoc(vdc, &geom, &vtoc);
77254bac2208Snarayan 		return (0);
77264bac2208Snarayan 	}
77274bac2208Snarayan 
772878fcd0a1Sachartre 	if (vtoc.v_nparts != V_NUMPAR) {
772978fcd0a1Sachartre 		mutex_enter(&vdc->lock);
773078fcd0a1Sachartre 		vdc_store_label_unk(vdc);
773178fcd0a1Sachartre 		return (EINVAL);
77320a55fbb7Slm66018 	}
7733d10e4ef2Snarayan 
7734d10e4ef2Snarayan 	/*
7735d10e4ef2Snarayan 	 * Read disk label from start of disk
7736d10e4ef2Snarayan 	 */
7737d10e4ef2Snarayan 	buf = kmem_alloc(sizeof (buf_t), KM_SLEEP);
7738d10e4ef2Snarayan 	bioinit(buf);
773978fcd0a1Sachartre 	buf->b_un.b_addr = (caddr_t)&label;
7740d10e4ef2Snarayan 	buf->b_bcount = DK_LABEL_SIZE;
7741d10e4ef2Snarayan 	buf->b_flags = B_BUSY | B_READ;
774217cadca8Slm66018 	buf->b_dev = cmpdev(dev);
774378fcd0a1Sachartre 	rv = vdc_send_request(vdc, VD_OP_BREAD, (caddr_t)&label,
774478fcd0a1Sachartre 	    DK_LABEL_SIZE, VD_SLICE_NONE, 0, CB_STRATEGY, buf, VIO_read_dir);
77453af08d82Slm66018 	if (rv) {
77463af08d82Slm66018 		DMSG(vdc, 1, "[%d] Failed to read disk block 0\n",
77473af08d82Slm66018 		    vdc->instance);
774878fcd0a1Sachartre 	} else {
7749d10e4ef2Snarayan 		rv = biowait(buf);
7750d10e4ef2Snarayan 		biofini(buf);
775178fcd0a1Sachartre 	}
7752d10e4ef2Snarayan 	kmem_free(buf, sizeof (buf_t));
77530a55fbb7Slm66018 
775478fcd0a1Sachartre 	if (rv != 0 || label.dkl_magic != DKL_MAGIC ||
775578fcd0a1Sachartre 	    label.dkl_cksum != vdc_lbl2cksum(&label)) {
775678fcd0a1Sachartre 		DMSG(vdc, 1, "[%d] Got VTOC with invalid label\n",
775778fcd0a1Sachartre 		    vdc->instance);
775878fcd0a1Sachartre 		mutex_enter(&vdc->lock);
775978fcd0a1Sachartre 		vdc_store_label_unk(vdc);
776078fcd0a1Sachartre 		return (EINVAL);
776178fcd0a1Sachartre 	}
776278fcd0a1Sachartre 
776378fcd0a1Sachartre 	mutex_enter(&vdc->lock);
776478fcd0a1Sachartre 	vdc_store_label_vtoc(vdc, &geom, &vtoc);
776578fcd0a1Sachartre 	return (0);
776678fcd0a1Sachartre }
776778fcd0a1Sachartre 
776878fcd0a1Sachartre /*
776978fcd0a1Sachartre  * Function:
777078fcd0a1Sachartre  *	vdc_validate
777178fcd0a1Sachartre  *
777278fcd0a1Sachartre  * Description:
777378fcd0a1Sachartre  *	This routine discovers the label of the disk and create the
777478fcd0a1Sachartre  *	appropriate device nodes if the label has changed.
777578fcd0a1Sachartre  *
777678fcd0a1Sachartre  * Arguments:
777778fcd0a1Sachartre  *	vdc	- soft state pointer for this instance of the device driver.
777878fcd0a1Sachartre  *
777978fcd0a1Sachartre  * Return Code:
778078fcd0a1Sachartre  *	none.
778178fcd0a1Sachartre  */
778278fcd0a1Sachartre static void
778378fcd0a1Sachartre vdc_validate(vdc_t *vdc)
778478fcd0a1Sachartre {
778578fcd0a1Sachartre 	vd_disk_label_t old_label;
7786edcc0754Sachartre 	vd_slice_t old_slice[V_NUMPAR];
778778fcd0a1Sachartre 	int rv;
778878fcd0a1Sachartre 
778978fcd0a1Sachartre 	ASSERT(!MUTEX_HELD(&vdc->lock));
779078fcd0a1Sachartre 
779178fcd0a1Sachartre 	mutex_enter(&vdc->lock);
779278fcd0a1Sachartre 
779378fcd0a1Sachartre 	/* save the current label and vtoc */
779478fcd0a1Sachartre 	old_label = vdc->vdisk_label;
7795edcc0754Sachartre 	bcopy(vdc->slice, &old_slice, sizeof (vd_slice_t) * V_NUMPAR);
779678fcd0a1Sachartre 
779778fcd0a1Sachartre 	/* check the geometry */
779878fcd0a1Sachartre 	(void) vdc_validate_geometry(vdc);
779978fcd0a1Sachartre 
780078fcd0a1Sachartre 	/* if the disk label has changed, update device nodes */
780178fcd0a1Sachartre 	if (vdc->vdisk_label != old_label) {
780278fcd0a1Sachartre 
780378fcd0a1Sachartre 		if (vdc->vdisk_label == VD_DISK_LABEL_EFI)
780478fcd0a1Sachartre 			rv = vdc_create_device_nodes_efi(vdc);
780578fcd0a1Sachartre 		else
780678fcd0a1Sachartre 			rv = vdc_create_device_nodes_vtoc(vdc);
780778fcd0a1Sachartre 
780878fcd0a1Sachartre 		if (rv != 0) {
780978fcd0a1Sachartre 			DMSG(vdc, 0, "![%d] Failed to update device nodes",
781078fcd0a1Sachartre 			    vdc->instance);
781178fcd0a1Sachartre 		}
781278fcd0a1Sachartre 	}
781378fcd0a1Sachartre 
781478fcd0a1Sachartre 	/* if the vtoc has changed, update device nodes properties */
7815edcc0754Sachartre 	if (bcmp(vdc->slice, &old_slice, sizeof (vd_slice_t) * V_NUMPAR) != 0) {
781678fcd0a1Sachartre 
781778fcd0a1Sachartre 		if (vdc_create_device_nodes_props(vdc) != 0) {
781878fcd0a1Sachartre 			DMSG(vdc, 0, "![%d] Failed to update device nodes"
781978fcd0a1Sachartre 			    " properties", vdc->instance);
782078fcd0a1Sachartre 		}
782178fcd0a1Sachartre 	}
782278fcd0a1Sachartre 
782378fcd0a1Sachartre 	mutex_exit(&vdc->lock);
782478fcd0a1Sachartre }
782578fcd0a1Sachartre 
782678fcd0a1Sachartre static void
782778fcd0a1Sachartre vdc_validate_task(void *arg)
782878fcd0a1Sachartre {
782978fcd0a1Sachartre 	vdc_t *vdc = (vdc_t *)arg;
783078fcd0a1Sachartre 
783178fcd0a1Sachartre 	vdc_validate(vdc);
783278fcd0a1Sachartre 
783378fcd0a1Sachartre 	mutex_enter(&vdc->lock);
783478fcd0a1Sachartre 	ASSERT(vdc->validate_pending > 0);
783578fcd0a1Sachartre 	vdc->validate_pending--;
783678fcd0a1Sachartre 	mutex_exit(&vdc->lock);
78371ae08745Sheppo }
78384bac2208Snarayan 
78394bac2208Snarayan /*
78404bac2208Snarayan  * Function:
78414bac2208Snarayan  *	vdc_setup_devid()
78424bac2208Snarayan  *
78434bac2208Snarayan  * Description:
78444bac2208Snarayan  *	This routine discovers the devid of a vDisk. It requests the devid of
78454bac2208Snarayan  *	the underlying device from the vDisk server, builds an encapsulated
78464bac2208Snarayan  *	devid based on the retrieved devid and registers that new devid to
78474bac2208Snarayan  *	the vDisk.
78484bac2208Snarayan  *
78494bac2208Snarayan  * Arguments:
78504bac2208Snarayan  *	vdc	- soft state pointer for this instance of the device driver.
78514bac2208Snarayan  *
78524bac2208Snarayan  * Return Code:
78534bac2208Snarayan  *	0	- A devid was succesfully registered for the vDisk
78544bac2208Snarayan  */
78554bac2208Snarayan static int
78564bac2208Snarayan vdc_setup_devid(vdc_t *vdc)
78574bac2208Snarayan {
78584bac2208Snarayan 	int rv;
78594bac2208Snarayan 	vd_devid_t *vd_devid;
78604bac2208Snarayan 	size_t bufsize, bufid_len;
78614bac2208Snarayan 
78624bac2208Snarayan 	/*
78634bac2208Snarayan 	 * At first sight, we don't know the size of the devid that the
78644bac2208Snarayan 	 * server will return but this size will be encoded into the
78654bac2208Snarayan 	 * reply. So we do a first request using a default size then we
78664bac2208Snarayan 	 * check if this size was large enough. If not then we do a second
78674bac2208Snarayan 	 * request with the correct size returned by the server. Note that
78684bac2208Snarayan 	 * ldc requires size to be 8-byte aligned.
78694bac2208Snarayan 	 */
78704bac2208Snarayan 	bufsize = P2ROUNDUP(VD_DEVID_SIZE(VD_DEVID_DEFAULT_LEN),
78714bac2208Snarayan 	    sizeof (uint64_t));
78724bac2208Snarayan 	vd_devid = kmem_zalloc(bufsize, KM_SLEEP);
78734bac2208Snarayan 	bufid_len = bufsize - sizeof (vd_efi_t) - 1;
78744bac2208Snarayan 
78753af08d82Slm66018 	rv = vdc_do_sync_op(vdc, VD_OP_GET_DEVID, (caddr_t)vd_devid,
78762f5224aeSachartre 	    bufsize, 0, 0, CB_SYNC, 0, VIO_both_dir, B_TRUE);
78773af08d82Slm66018 
78783af08d82Slm66018 	DMSG(vdc, 2, "sync_op returned %d\n", rv);
78793af08d82Slm66018 
78804bac2208Snarayan 	if (rv) {
78814bac2208Snarayan 		kmem_free(vd_devid, bufsize);
78824bac2208Snarayan 		return (rv);
78834bac2208Snarayan 	}
78844bac2208Snarayan 
78854bac2208Snarayan 	if (vd_devid->length > bufid_len) {
78864bac2208Snarayan 		/*
78874bac2208Snarayan 		 * The returned devid is larger than the buffer used. Try again
78884bac2208Snarayan 		 * with a buffer with the right size.
78894bac2208Snarayan 		 */
78904bac2208Snarayan 		kmem_free(vd_devid, bufsize);
78914bac2208Snarayan 		bufsize = P2ROUNDUP(VD_DEVID_SIZE(vd_devid->length),
78924bac2208Snarayan 		    sizeof (uint64_t));
78934bac2208Snarayan 		vd_devid = kmem_zalloc(bufsize, KM_SLEEP);
78944bac2208Snarayan 		bufid_len = bufsize - sizeof (vd_efi_t) - 1;
78954bac2208Snarayan 
78963af08d82Slm66018 		rv = vdc_do_sync_op(vdc, VD_OP_GET_DEVID,
78973af08d82Slm66018 		    (caddr_t)vd_devid, bufsize, 0, 0, CB_SYNC, 0,
78982f5224aeSachartre 		    VIO_both_dir, B_TRUE);
78993af08d82Slm66018 
79004bac2208Snarayan 		if (rv) {
79014bac2208Snarayan 			kmem_free(vd_devid, bufsize);
79024bac2208Snarayan 			return (rv);
79034bac2208Snarayan 		}
79044bac2208Snarayan 	}
79054bac2208Snarayan 
79064bac2208Snarayan 	/*
79074bac2208Snarayan 	 * The virtual disk should have the same device id as the one associated
79084bac2208Snarayan 	 * with the physical disk it is mapped on, otherwise sharing a disk
79094bac2208Snarayan 	 * between a LDom and a non-LDom may not work (for example for a shared
79104bac2208Snarayan 	 * SVM disk set).
79114bac2208Snarayan 	 *
79124bac2208Snarayan 	 * The DDI framework does not allow creating a device id with any
79134bac2208Snarayan 	 * type so we first create a device id of type DEVID_ENCAP and then
79144bac2208Snarayan 	 * we restore the orignal type of the physical device.
79154bac2208Snarayan 	 */
79164bac2208Snarayan 
79173af08d82Slm66018 	DMSG(vdc, 2, ": devid length = %d\n", vd_devid->length);
79183af08d82Slm66018 
79194bac2208Snarayan 	/* build an encapsulated devid based on the returned devid */
79204bac2208Snarayan 	if (ddi_devid_init(vdc->dip, DEVID_ENCAP, vd_devid->length,
79214bac2208Snarayan 	    vd_devid->id, &vdc->devid) != DDI_SUCCESS) {
79223af08d82Slm66018 		DMSG(vdc, 1, "[%d] Fail to created devid\n", vdc->instance);
79234bac2208Snarayan 		kmem_free(vd_devid, bufsize);
79244bac2208Snarayan 		return (1);
79254bac2208Snarayan 	}
79264bac2208Snarayan 
79274bac2208Snarayan 	DEVID_FORMTYPE((impl_devid_t *)vdc->devid, vd_devid->type);
79284bac2208Snarayan 
79294bac2208Snarayan 	ASSERT(ddi_devid_valid(vdc->devid) == DDI_SUCCESS);
79304bac2208Snarayan 
79314bac2208Snarayan 	kmem_free(vd_devid, bufsize);
79324bac2208Snarayan 
79334bac2208Snarayan 	if (ddi_devid_register(vdc->dip, vdc->devid) != DDI_SUCCESS) {
79343af08d82Slm66018 		DMSG(vdc, 1, "[%d] Fail to register devid\n", vdc->instance);
79354bac2208Snarayan 		return (1);
79364bac2208Snarayan 	}
79374bac2208Snarayan 
79384bac2208Snarayan 	return (0);
79394bac2208Snarayan }
79404bac2208Snarayan 
79414bac2208Snarayan static void
7942edcc0754Sachartre vdc_store_label_efi(vdc_t *vdc, efi_gpt_t *gpt, efi_gpe_t *gpe)
79434bac2208Snarayan {
7944edcc0754Sachartre 	int i, nparts;
79454bac2208Snarayan 
794678fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
794778fcd0a1Sachartre 
794878fcd0a1Sachartre 	vdc->vdisk_label = VD_DISK_LABEL_EFI;
7949edcc0754Sachartre 	bzero(vdc->vtoc, sizeof (struct vtoc));
795078fcd0a1Sachartre 	bzero(vdc->geom, sizeof (struct dk_geom));
7951edcc0754Sachartre 	bzero(vdc->slice, sizeof (vd_slice_t) * V_NUMPAR);
7952edcc0754Sachartre 
7953edcc0754Sachartre 	nparts = gpt->efi_gpt_NumberOfPartitionEntries;
7954edcc0754Sachartre 
7955edcc0754Sachartre 	for (i = 0; i < nparts && i < VD_EFI_WD_SLICE; i++) {
7956edcc0754Sachartre 
7957edcc0754Sachartre 		if (gpe[i].efi_gpe_StartingLBA == 0 ||
7958edcc0754Sachartre 		    gpe[i].efi_gpe_EndingLBA == 0) {
7959edcc0754Sachartre 			continue;
79604bac2208Snarayan 		}
7961edcc0754Sachartre 
7962edcc0754Sachartre 		vdc->slice[i].start = gpe[i].efi_gpe_StartingLBA;
7963edcc0754Sachartre 		vdc->slice[i].nblocks = gpe[i].efi_gpe_EndingLBA -
7964edcc0754Sachartre 		    gpe[i].efi_gpe_StartingLBA + 1;
7965edcc0754Sachartre 	}
7966edcc0754Sachartre 
7967edcc0754Sachartre 	ASSERT(vdc->vdisk_size != 0);
7968edcc0754Sachartre 	vdc->slice[VD_EFI_WD_SLICE].start = 0;
7969edcc0754Sachartre 	vdc->slice[VD_EFI_WD_SLICE].nblocks = vdc->vdisk_size;
7970edcc0754Sachartre 
79714bac2208Snarayan }
797278fcd0a1Sachartre 
797378fcd0a1Sachartre static void
797478fcd0a1Sachartre vdc_store_label_vtoc(vdc_t *vdc, struct dk_geom *geom, struct vtoc *vtoc)
797578fcd0a1Sachartre {
7976edcc0754Sachartre 	int i;
7977edcc0754Sachartre 
797878fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
7979edcc0754Sachartre 	ASSERT(vdc->block_size == vtoc->v_sectorsz);
798078fcd0a1Sachartre 
798178fcd0a1Sachartre 	vdc->vdisk_label = VD_DISK_LABEL_VTOC;
798278fcd0a1Sachartre 	bcopy(vtoc, vdc->vtoc, sizeof (struct vtoc));
798378fcd0a1Sachartre 	bcopy(geom, vdc->geom, sizeof (struct dk_geom));
7984edcc0754Sachartre 	bzero(vdc->slice, sizeof (vd_slice_t) * V_NUMPAR);
7985edcc0754Sachartre 
7986edcc0754Sachartre 	for (i = 0; i < vtoc->v_nparts; i++) {
7987edcc0754Sachartre 		vdc->slice[i].start = vtoc->v_part[i].p_start;
7988edcc0754Sachartre 		vdc->slice[i].nblocks = vtoc->v_part[i].p_size;
7989edcc0754Sachartre 	}
799078fcd0a1Sachartre }
799178fcd0a1Sachartre 
799278fcd0a1Sachartre static void
799378fcd0a1Sachartre vdc_store_label_unk(vdc_t *vdc)
799478fcd0a1Sachartre {
799578fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
799678fcd0a1Sachartre 
799778fcd0a1Sachartre 	vdc->vdisk_label = VD_DISK_LABEL_UNK;
799878fcd0a1Sachartre 	bzero(vdc->vtoc, sizeof (struct vtoc));
799978fcd0a1Sachartre 	bzero(vdc->geom, sizeof (struct dk_geom));
8000edcc0754Sachartre 	bzero(vdc->slice, sizeof (vd_slice_t) * V_NUMPAR);
800178fcd0a1Sachartre }
8002