xref: /titanic_52/usr/src/uts/sun4v/io/vdc.c (revision 90e2f9dc8deb72bb1a3fb2e78d39aff80aa005b5)
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);
127655fd6a9Sachartre static int	vdc_do_ldc_init(vdc_t *vdc, md_t *mdp, mde_cookie_t vd_node);
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,
137655fd6a9Sachartre 		    mde_cookie_t *vd_nodep, mde_cookie_t *vd_portp);
138655fd6a9Sachartre static int	vdc_get_ldc_id(md_t *, mde_cookie_t, uint64_t *);
1390a55fbb7Slm66018 static int	vdc_do_ldc_up(vdc_t *vdc);
1401ae08745Sheppo static void	vdc_terminate_ldc(vdc_t *vdc);
1411ae08745Sheppo static int	vdc_init_descriptor_ring(vdc_t *vdc);
1421ae08745Sheppo static void	vdc_destroy_descriptor_ring(vdc_t *vdc);
1434bac2208Snarayan static int	vdc_setup_devid(vdc_t *vdc);
144edcc0754Sachartre static void	vdc_store_label_efi(vdc_t *, efi_gpt_t *, efi_gpe_t *);
14578fcd0a1Sachartre static void	vdc_store_label_vtoc(vdc_t *, struct dk_geom *, struct vtoc *);
14678fcd0a1Sachartre static void	vdc_store_label_unk(vdc_t *vdc);
14778fcd0a1Sachartre static boolean_t vdc_is_opened(vdc_t *vdc);
1481ae08745Sheppo 
1491ae08745Sheppo /* handshake with vds */
1500a55fbb7Slm66018 static int		vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver);
1513af08d82Slm66018 static int		vdc_ver_negotiation(vdc_t *vdcp);
1521ae08745Sheppo static int		vdc_init_attr_negotiation(vdc_t *vdc);
1533af08d82Slm66018 static int		vdc_attr_negotiation(vdc_t *vdcp);
1541ae08745Sheppo static int		vdc_init_dring_negotiate(vdc_t *vdc);
1553af08d82Slm66018 static int		vdc_dring_negotiation(vdc_t *vdcp);
1563af08d82Slm66018 static int		vdc_send_rdx(vdc_t *vdcp);
1573af08d82Slm66018 static int		vdc_rdx_exchange(vdc_t *vdcp);
1580a55fbb7Slm66018 static boolean_t	vdc_is_supported_version(vio_ver_msg_t *ver_msg);
1591ae08745Sheppo 
1600a55fbb7Slm66018 /* processing incoming messages from vDisk server */
1611ae08745Sheppo static void	vdc_process_msg_thread(vdc_t *vdc);
1623af08d82Slm66018 static int	vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp);
1633af08d82Slm66018 
1640a55fbb7Slm66018 static uint_t	vdc_handle_cb(uint64_t event, caddr_t arg);
1653af08d82Slm66018 static int	vdc_process_data_msg(vdc_t *vdc, vio_msg_t *msg);
1660a55fbb7Slm66018 static int	vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg);
1670a55fbb7Slm66018 static int	vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg);
1680a55fbb7Slm66018 static int	vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *msg);
1693af08d82Slm66018 static int 	vdc_send_request(vdc_t *vdcp, int operation,
1703af08d82Slm66018 		    caddr_t addr, size_t nbytes, int slice, diskaddr_t offset,
1713af08d82Slm66018 		    int cb_type, void *cb_arg, vio_desc_direction_t dir);
1723af08d82Slm66018 static int	vdc_map_to_shared_dring(vdc_t *vdcp, int idx);
1733af08d82Slm66018 static int 	vdc_populate_descriptor(vdc_t *vdcp, int operation,
1743af08d82Slm66018 		    caddr_t addr, size_t nbytes, int slice, diskaddr_t offset,
1753af08d82Slm66018 		    int cb_type, void *cb_arg, vio_desc_direction_t dir);
1762f5224aeSachartre static int 	vdc_do_sync_op(vdc_t *vdcp, int operation, caddr_t addr,
1772f5224aeSachartre 		    size_t nbytes, int slice, diskaddr_t offset, int cb_type,
1782f5224aeSachartre 		    void *cb_arg, vio_desc_direction_t dir, boolean_t);
1793af08d82Slm66018 
1803af08d82Slm66018 static int	vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp);
1813af08d82Slm66018 static int	vdc_drain_response(vdc_t *vdcp);
1821ae08745Sheppo static int	vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx);
1833af08d82Slm66018 static int	vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep);
184e1ebb9ecSlm66018 static int	vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg);
1851ae08745Sheppo 
1861ae08745Sheppo /* dkio */
1872f5224aeSachartre static int	vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode,
1882f5224aeSachartre 		    int *rvalp);
189edcc0754Sachartre static int	vd_process_efi_ioctl(void *vdisk, int cmd, uintptr_t arg);
19078fcd0a1Sachartre static void	vdc_create_fake_geometry(vdc_t *vdc);
19178fcd0a1Sachartre static int	vdc_validate_geometry(vdc_t *vdc);
19278fcd0a1Sachartre static void	vdc_validate(vdc_t *vdc);
19378fcd0a1Sachartre static void	vdc_validate_task(void *arg);
194d10e4ef2Snarayan static int	vdc_null_copy_func(vdc_t *vdc, void *from, void *to,
195d10e4ef2Snarayan 		    int mode, int dir);
1964bac2208Snarayan static int	vdc_get_wce_convert(vdc_t *vdc, void *from, void *to,
1974bac2208Snarayan 		    int mode, int dir);
1984bac2208Snarayan static int	vdc_set_wce_convert(vdc_t *vdc, void *from, void *to,
1994bac2208Snarayan 		    int mode, int dir);
200d10e4ef2Snarayan static int	vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to,
201d10e4ef2Snarayan 		    int mode, int dir);
202d10e4ef2Snarayan static int	vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to,
203d10e4ef2Snarayan 		    int mode, int dir);
204d10e4ef2Snarayan static int	vdc_get_geom_convert(vdc_t *vdc, void *from, void *to,
205d10e4ef2Snarayan 		    int mode, int dir);
206d10e4ef2Snarayan static int	vdc_set_geom_convert(vdc_t *vdc, void *from, void *to,
207d10e4ef2Snarayan 		    int mode, int dir);
2084bac2208Snarayan static int	vdc_get_efi_convert(vdc_t *vdc, void *from, void *to,
2094bac2208Snarayan 		    int mode, int dir);
2104bac2208Snarayan static int	vdc_set_efi_convert(vdc_t *vdc, void *from, void *to,
2114bac2208Snarayan 		    int mode, int dir);
2121ae08745Sheppo 
2132f5224aeSachartre static void 	vdc_ownership_update(vdc_t *vdc, int ownership_flags);
2142f5224aeSachartre static int	vdc_access_set(vdc_t *vdc, uint64_t flags, int mode);
2152f5224aeSachartre static vdc_io_t	*vdc_failfast_io_queue(vdc_t *vdc, struct buf *buf);
2162f5224aeSachartre static int	vdc_failfast_check_resv(vdc_t *vdc);
2172f5224aeSachartre 
2181ae08745Sheppo /*
2191ae08745Sheppo  * Module variables
2201ae08745Sheppo  */
221e1ebb9ecSlm66018 
222e1ebb9ecSlm66018 /*
223e1ebb9ecSlm66018  * Tunable variables to control how long vdc waits before timing out on
224e1ebb9ecSlm66018  * various operations
225e1ebb9ecSlm66018  */
2263c96341aSnarayan static int	vdc_hshake_retries = 3;
227e1ebb9ecSlm66018 
228655fd6a9Sachartre static int	vdc_timeout = 0; /* units: seconds */
229655fd6a9Sachartre 
2303af08d82Slm66018 static uint64_t vdc_hz_min_ldc_delay;
2313af08d82Slm66018 static uint64_t vdc_min_timeout_ldc = 1 * MILLISEC;
2323af08d82Slm66018 static uint64_t vdc_hz_max_ldc_delay;
2333af08d82Slm66018 static uint64_t vdc_max_timeout_ldc = 100 * MILLISEC;
2343af08d82Slm66018 
2353af08d82Slm66018 static uint64_t vdc_ldc_read_init_delay = 1 * MILLISEC;
2363af08d82Slm66018 static uint64_t vdc_ldc_read_max_delay = 100 * MILLISEC;
237e1ebb9ecSlm66018 
238e1ebb9ecSlm66018 /* values for dumping - need to run in a tighter loop */
239e1ebb9ecSlm66018 static uint64_t	vdc_usec_timeout_dump = 100 * MILLISEC;	/* 0.1s units: ns */
240e1ebb9ecSlm66018 static int	vdc_dump_retries = 100;
241e1ebb9ecSlm66018 
2422f5224aeSachartre static uint16_t	vdc_scsi_timeout = 60;	/* 60s units: seconds  */
2432f5224aeSachartre 
2442f5224aeSachartre static uint64_t vdc_ownership_delay = 6 * MICROSEC; /* 6s units: usec */
2452f5224aeSachartre 
246e1ebb9ecSlm66018 /* Count of the number of vdc instances attached */
247e1ebb9ecSlm66018 static volatile uint32_t	vdc_instance_count = 0;
2481ae08745Sheppo 
2492f5224aeSachartre /* Tunable to log all SCSI errors */
2502f5224aeSachartre static boolean_t vdc_scsi_log_error = B_FALSE;
2512f5224aeSachartre 
2521ae08745Sheppo /* Soft state pointer */
2531ae08745Sheppo static void	*vdc_state;
2541ae08745Sheppo 
2553af08d82Slm66018 /*
2563af08d82Slm66018  * Controlling the verbosity of the error/debug messages
2573af08d82Slm66018  *
2583af08d82Slm66018  * vdc_msglevel - controls level of messages
2593af08d82Slm66018  * vdc_matchinst - 64-bit variable where each bit corresponds
2603af08d82Slm66018  *                 to the vdc instance the vdc_msglevel applies.
2613af08d82Slm66018  */
2623af08d82Slm66018 int		vdc_msglevel = 0x0;
2633af08d82Slm66018 uint64_t	vdc_matchinst = 0ull;
2641ae08745Sheppo 
2650a55fbb7Slm66018 /*
2660a55fbb7Slm66018  * Supported vDisk protocol version pairs.
2670a55fbb7Slm66018  *
2680a55fbb7Slm66018  * The first array entry is the latest and preferred version.
2690a55fbb7Slm66018  */
27017cadca8Slm66018 static const vio_ver_t	vdc_version[] = {{1, 1}};
2711ae08745Sheppo 
2721ae08745Sheppo static struct cb_ops vdc_cb_ops = {
2731ae08745Sheppo 	vdc_open,	/* cb_open */
2741ae08745Sheppo 	vdc_close,	/* cb_close */
2751ae08745Sheppo 	vdc_strategy,	/* cb_strategy */
2761ae08745Sheppo 	vdc_print,	/* cb_print */
2771ae08745Sheppo 	vdc_dump,	/* cb_dump */
2781ae08745Sheppo 	vdc_read,	/* cb_read */
2791ae08745Sheppo 	vdc_write,	/* cb_write */
2801ae08745Sheppo 	vdc_ioctl,	/* cb_ioctl */
2811ae08745Sheppo 	nodev,		/* cb_devmap */
2821ae08745Sheppo 	nodev,		/* cb_mmap */
2831ae08745Sheppo 	nodev,		/* cb_segmap */
2841ae08745Sheppo 	nochpoll,	/* cb_chpoll */
2851ae08745Sheppo 	ddi_prop_op,	/* cb_prop_op */
2861ae08745Sheppo 	NULL,		/* cb_str */
2871ae08745Sheppo 	D_MP | D_64BIT,	/* cb_flag */
2881ae08745Sheppo 	CB_REV,		/* cb_rev */
2891ae08745Sheppo 	vdc_aread,	/* cb_aread */
2901ae08745Sheppo 	vdc_awrite	/* cb_awrite */
2911ae08745Sheppo };
2921ae08745Sheppo 
2931ae08745Sheppo static struct dev_ops vdc_ops = {
2941ae08745Sheppo 	DEVO_REV,	/* devo_rev */
2951ae08745Sheppo 	0,		/* devo_refcnt */
2961ae08745Sheppo 	vdc_getinfo,	/* devo_getinfo */
2971ae08745Sheppo 	nulldev,	/* devo_identify */
2981ae08745Sheppo 	nulldev,	/* devo_probe */
2991ae08745Sheppo 	vdc_attach,	/* devo_attach */
3001ae08745Sheppo 	vdc_detach,	/* devo_detach */
3011ae08745Sheppo 	nodev,		/* devo_reset */
3021ae08745Sheppo 	&vdc_cb_ops,	/* devo_cb_ops */
3031ae08745Sheppo 	NULL,		/* devo_bus_ops */
3041ae08745Sheppo 	nulldev		/* devo_power */
3051ae08745Sheppo };
3061ae08745Sheppo 
3071ae08745Sheppo static struct modldrv modldrv = {
3081ae08745Sheppo 	&mod_driverops,
309205eeb1aSlm66018 	"virtual disk client",
3101ae08745Sheppo 	&vdc_ops,
3111ae08745Sheppo };
3121ae08745Sheppo 
3131ae08745Sheppo static struct modlinkage modlinkage = {
3141ae08745Sheppo 	MODREV_1,
3151ae08745Sheppo 	&modldrv,
3161ae08745Sheppo 	NULL
3171ae08745Sheppo };
3181ae08745Sheppo 
3191ae08745Sheppo /* -------------------------------------------------------------------------- */
3201ae08745Sheppo 
3211ae08745Sheppo /*
3221ae08745Sheppo  * Device Driver housekeeping and setup
3231ae08745Sheppo  */
3241ae08745Sheppo 
3251ae08745Sheppo int
3261ae08745Sheppo _init(void)
3271ae08745Sheppo {
3281ae08745Sheppo 	int	status;
3291ae08745Sheppo 
3301ae08745Sheppo 	if ((status = ddi_soft_state_init(&vdc_state, sizeof (vdc_t), 1)) != 0)
3311ae08745Sheppo 		return (status);
3321ae08745Sheppo 	if ((status = mod_install(&modlinkage)) != 0)
3331ae08745Sheppo 		ddi_soft_state_fini(&vdc_state);
3341ae08745Sheppo 	return (status);
3351ae08745Sheppo }
3361ae08745Sheppo 
3371ae08745Sheppo int
3381ae08745Sheppo _info(struct modinfo *modinfop)
3391ae08745Sheppo {
3401ae08745Sheppo 	return (mod_info(&modlinkage, modinfop));
3411ae08745Sheppo }
3421ae08745Sheppo 
3431ae08745Sheppo int
3441ae08745Sheppo _fini(void)
3451ae08745Sheppo {
3461ae08745Sheppo 	int	status;
3471ae08745Sheppo 
3481ae08745Sheppo 	if ((status = mod_remove(&modlinkage)) != 0)
3491ae08745Sheppo 		return (status);
3501ae08745Sheppo 	ddi_soft_state_fini(&vdc_state);
3511ae08745Sheppo 	return (0);
3521ae08745Sheppo }
3531ae08745Sheppo 
3541ae08745Sheppo static int
3551ae08745Sheppo vdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,  void *arg, void **resultp)
3561ae08745Sheppo {
3571ae08745Sheppo 	_NOTE(ARGUNUSED(dip))
3581ae08745Sheppo 
3590d0c8d4bSnarayan 	int	instance = VDCUNIT((dev_t)arg);
3601ae08745Sheppo 	vdc_t	*vdc = NULL;
3611ae08745Sheppo 
3621ae08745Sheppo 	switch (cmd) {
3631ae08745Sheppo 	case DDI_INFO_DEVT2DEVINFO:
3641ae08745Sheppo 		if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
3651ae08745Sheppo 			*resultp = NULL;
3661ae08745Sheppo 			return (DDI_FAILURE);
3671ae08745Sheppo 		}
3681ae08745Sheppo 		*resultp = vdc->dip;
3691ae08745Sheppo 		return (DDI_SUCCESS);
3701ae08745Sheppo 	case DDI_INFO_DEVT2INSTANCE:
3711ae08745Sheppo 		*resultp = (void *)(uintptr_t)instance;
3721ae08745Sheppo 		return (DDI_SUCCESS);
3731ae08745Sheppo 	default:
3741ae08745Sheppo 		*resultp = NULL;
3751ae08745Sheppo 		return (DDI_FAILURE);
3761ae08745Sheppo 	}
3771ae08745Sheppo }
3781ae08745Sheppo 
3791ae08745Sheppo static int
3801ae08745Sheppo vdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3811ae08745Sheppo {
3822f5224aeSachartre 	kt_did_t failfast_tid, ownership_tid;
3831ae08745Sheppo 	int	instance;
3841ae08745Sheppo 	int	rv;
3851ae08745Sheppo 	vdc_t	*vdc = NULL;
3861ae08745Sheppo 
3871ae08745Sheppo 	switch (cmd) {
3881ae08745Sheppo 	case DDI_DETACH:
3891ae08745Sheppo 		/* the real work happens below */
3901ae08745Sheppo 		break;
3911ae08745Sheppo 	case DDI_SUSPEND:
3921ae08745Sheppo 		/* nothing to do for this non-device */
3931ae08745Sheppo 		return (DDI_SUCCESS);
3941ae08745Sheppo 	default:
3951ae08745Sheppo 		return (DDI_FAILURE);
3961ae08745Sheppo 	}
3971ae08745Sheppo 
3981ae08745Sheppo 	ASSERT(cmd == DDI_DETACH);
3991ae08745Sheppo 	instance = ddi_get_instance(dip);
4003af08d82Slm66018 	DMSGX(1, "[%d] Entered\n", instance);
4011ae08745Sheppo 
4021ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
403e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
4041ae08745Sheppo 		return (DDI_FAILURE);
4051ae08745Sheppo 	}
4061ae08745Sheppo 
4072f5224aeSachartre 	/*
4082f5224aeSachartre 	 * This function is called when vdc is detached or if it has failed to
4092f5224aeSachartre 	 * attach. In that case, the attach may have fail before the vdisk type
4102f5224aeSachartre 	 * has been set so we can't call vdc_is_opened(). However as the attach
4112f5224aeSachartre 	 * has failed, we know that the vdisk is not opened and we can safely
4122f5224aeSachartre 	 * detach.
4132f5224aeSachartre 	 */
4142f5224aeSachartre 	if (vdc->vdisk_type != VD_DISK_TYPE_UNK && vdc_is_opened(vdc)) {
4153af08d82Slm66018 		DMSG(vdc, 0, "[%d] Cannot detach: device is open", instance);
4161ae08745Sheppo 		return (DDI_FAILURE);
4171ae08745Sheppo 	}
4181ae08745Sheppo 
41978fcd0a1Sachartre 	if (vdc->dkio_flush_pending) {
42078fcd0a1Sachartre 		DMSG(vdc, 0,
42178fcd0a1Sachartre 		    "[%d] Cannot detach: %d outstanding DKIO flushes\n",
42278fcd0a1Sachartre 		    instance, vdc->dkio_flush_pending);
42378fcd0a1Sachartre 		return (DDI_FAILURE);
42478fcd0a1Sachartre 	}
42578fcd0a1Sachartre 
42678fcd0a1Sachartre 	if (vdc->validate_pending) {
42778fcd0a1Sachartre 		DMSG(vdc, 0,
42878fcd0a1Sachartre 		    "[%d] Cannot detach: %d outstanding validate request\n",
42978fcd0a1Sachartre 		    instance, vdc->validate_pending);
43078fcd0a1Sachartre 		return (DDI_FAILURE);
43178fcd0a1Sachartre 	}
43278fcd0a1Sachartre 
4333af08d82Slm66018 	DMSG(vdc, 0, "[%d] proceeding...\n", instance);
4343af08d82Slm66018 
4352f5224aeSachartre 	/* If we took ownership, release ownership */
4362f5224aeSachartre 	mutex_enter(&vdc->ownership_lock);
4372f5224aeSachartre 	if (vdc->ownership & VDC_OWNERSHIP_GRANTED) {
4382f5224aeSachartre 		rv = vdc_access_set(vdc, VD_ACCESS_SET_CLEAR, FKIOCTL);
4392f5224aeSachartre 		if (rv == 0) {
4402f5224aeSachartre 			vdc_ownership_update(vdc, VDC_OWNERSHIP_NONE);
4412f5224aeSachartre 		}
4422f5224aeSachartre 	}
4432f5224aeSachartre 	mutex_exit(&vdc->ownership_lock);
4442f5224aeSachartre 
4453af08d82Slm66018 	/* mark instance as detaching */
4463af08d82Slm66018 	vdc->lifecycle	= VDC_LC_DETACHING;
4471ae08745Sheppo 
4481ae08745Sheppo 	/*
4491ae08745Sheppo 	 * try and disable callbacks to prevent another handshake
4501ae08745Sheppo 	 */
4511ae08745Sheppo 	rv = ldc_set_cb_mode(vdc->ldc_handle, LDC_CB_DISABLE);
4523af08d82Slm66018 	DMSG(vdc, 0, "callback disabled (rv=%d)\n", rv);
4531ae08745Sheppo 
4541ae08745Sheppo 	if (vdc->initialized & VDC_THREAD) {
4553af08d82Slm66018 		mutex_enter(&vdc->read_lock);
4563af08d82Slm66018 		if ((vdc->read_state == VDC_READ_WAITING) ||
4573af08d82Slm66018 		    (vdc->read_state == VDC_READ_RESET)) {
4583af08d82Slm66018 			vdc->read_state = VDC_READ_RESET;
4593af08d82Slm66018 			cv_signal(&vdc->read_cv);
4601ae08745Sheppo 		}
4613af08d82Slm66018 
4623af08d82Slm66018 		mutex_exit(&vdc->read_lock);
4633af08d82Slm66018 
4643af08d82Slm66018 		/* wake up any thread waiting for connection to come online */
4653af08d82Slm66018 		mutex_enter(&vdc->lock);
4663af08d82Slm66018 		if (vdc->state == VDC_STATE_INIT_WAITING) {
4673af08d82Slm66018 			DMSG(vdc, 0,
4683af08d82Slm66018 			    "[%d] write reset - move to resetting state...\n",
4693af08d82Slm66018 			    instance);
4703af08d82Slm66018 			vdc->state = VDC_STATE_RESETTING;
4713af08d82Slm66018 			cv_signal(&vdc->initwait_cv);
4723af08d82Slm66018 		}
4733af08d82Slm66018 		mutex_exit(&vdc->lock);
4743af08d82Slm66018 
4753af08d82Slm66018 		/* now wait until state transitions to VDC_STATE_DETACH */
4763af08d82Slm66018 		thread_join(vdc->msg_proc_thr->t_did);
4773af08d82Slm66018 		ASSERT(vdc->state == VDC_STATE_DETACH);
4783af08d82Slm66018 		DMSG(vdc, 0, "[%d] Reset thread exit and join ..\n",
4793af08d82Slm66018 		    vdc->instance);
4801ae08745Sheppo 	}
4811ae08745Sheppo 
4821ae08745Sheppo 	mutex_enter(&vdc->lock);
4831ae08745Sheppo 
4841ae08745Sheppo 	if (vdc->initialized & VDC_DRING)
4851ae08745Sheppo 		vdc_destroy_descriptor_ring(vdc);
4861ae08745Sheppo 
4871ae08745Sheppo 	if (vdc->initialized & VDC_LDC)
4881ae08745Sheppo 		vdc_terminate_ldc(vdc);
4891ae08745Sheppo 
4902f5224aeSachartre 	if (vdc->failfast_thread) {
4912f5224aeSachartre 		failfast_tid = vdc->failfast_thread->t_did;
4922f5224aeSachartre 		vdc->failfast_interval = 0;
4932f5224aeSachartre 		cv_signal(&vdc->failfast_cv);
4942f5224aeSachartre 	} else {
4952f5224aeSachartre 		failfast_tid = 0;
4962f5224aeSachartre 	}
4972f5224aeSachartre 
4982f5224aeSachartre 	if (vdc->ownership & VDC_OWNERSHIP_WANTED) {
4992f5224aeSachartre 		ownership_tid = vdc->ownership_thread->t_did;
5002f5224aeSachartre 		vdc->ownership = VDC_OWNERSHIP_NONE;
5012f5224aeSachartre 		cv_signal(&vdc->ownership_cv);
5022f5224aeSachartre 	} else {
5032f5224aeSachartre 		ownership_tid = 0;
5042f5224aeSachartre 	}
5052f5224aeSachartre 
5061ae08745Sheppo 	mutex_exit(&vdc->lock);
5071ae08745Sheppo 
5082f5224aeSachartre 	if (failfast_tid != 0)
5092f5224aeSachartre 		thread_join(failfast_tid);
5102f5224aeSachartre 
5112f5224aeSachartre 	if (ownership_tid != 0)
5122f5224aeSachartre 		thread_join(ownership_tid);
5132f5224aeSachartre 
5141ae08745Sheppo 	if (vdc->initialized & VDC_MINOR) {
5151ae08745Sheppo 		ddi_prop_remove_all(dip);
5161ae08745Sheppo 		ddi_remove_minor_node(dip, NULL);
5171ae08745Sheppo 	}
5181ae08745Sheppo 
519366a92acSlm66018 	if (vdc->io_stats) {
520366a92acSlm66018 		kstat_delete(vdc->io_stats);
521366a92acSlm66018 		vdc->io_stats = NULL;
522366a92acSlm66018 	}
523366a92acSlm66018 
524366a92acSlm66018 	if (vdc->err_stats) {
525366a92acSlm66018 		kstat_delete(vdc->err_stats);
526366a92acSlm66018 		vdc->err_stats = NULL;
527366a92acSlm66018 	}
528366a92acSlm66018 
5291ae08745Sheppo 	if (vdc->initialized & VDC_LOCKS) {
5301ae08745Sheppo 		mutex_destroy(&vdc->lock);
5313af08d82Slm66018 		mutex_destroy(&vdc->read_lock);
5322f5224aeSachartre 		mutex_destroy(&vdc->ownership_lock);
5333af08d82Slm66018 		cv_destroy(&vdc->initwait_cv);
5343af08d82Slm66018 		cv_destroy(&vdc->dring_free_cv);
5353af08d82Slm66018 		cv_destroy(&vdc->membind_cv);
5363af08d82Slm66018 		cv_destroy(&vdc->sync_pending_cv);
5373af08d82Slm66018 		cv_destroy(&vdc->sync_blocked_cv);
5383af08d82Slm66018 		cv_destroy(&vdc->read_cv);
5393af08d82Slm66018 		cv_destroy(&vdc->running_cv);
5402f5224aeSachartre 		cv_destroy(&vdc->ownership_cv);
5412f5224aeSachartre 		cv_destroy(&vdc->failfast_cv);
5422f5224aeSachartre 		cv_destroy(&vdc->failfast_io_cv);
5431ae08745Sheppo 	}
5441ae08745Sheppo 
5451ae08745Sheppo 	if (vdc->minfo)
5461ae08745Sheppo 		kmem_free(vdc->minfo, sizeof (struct dk_minfo));
5471ae08745Sheppo 
5481ae08745Sheppo 	if (vdc->cinfo)
5491ae08745Sheppo 		kmem_free(vdc->cinfo, sizeof (struct dk_cinfo));
5501ae08745Sheppo 
5511ae08745Sheppo 	if (vdc->vtoc)
5521ae08745Sheppo 		kmem_free(vdc->vtoc, sizeof (struct vtoc));
5531ae08745Sheppo 
55478fcd0a1Sachartre 	if (vdc->geom)
55578fcd0a1Sachartre 		kmem_free(vdc->geom, sizeof (struct dk_geom));
5560a55fbb7Slm66018 
5574bac2208Snarayan 	if (vdc->devid) {
5584bac2208Snarayan 		ddi_devid_unregister(dip);
5594bac2208Snarayan 		ddi_devid_free(vdc->devid);
5604bac2208Snarayan 	}
5614bac2208Snarayan 
5621ae08745Sheppo 	if (vdc->initialized & VDC_SOFT_STATE)
5631ae08745Sheppo 		ddi_soft_state_free(vdc_state, instance);
5641ae08745Sheppo 
5653af08d82Slm66018 	DMSG(vdc, 0, "[%d] End %p\n", instance, (void *)vdc);
5661ae08745Sheppo 
5671ae08745Sheppo 	return (DDI_SUCCESS);
5681ae08745Sheppo }
5691ae08745Sheppo 
5701ae08745Sheppo 
5711ae08745Sheppo static int
5721ae08745Sheppo vdc_do_attach(dev_info_t *dip)
5731ae08745Sheppo {
5741ae08745Sheppo 	int		instance;
5751ae08745Sheppo 	vdc_t		*vdc = NULL;
5761ae08745Sheppo 	int		status;
577655fd6a9Sachartre 	md_t		*mdp;
578655fd6a9Sachartre 	mde_cookie_t	vd_node, vd_port;
5791ae08745Sheppo 
5801ae08745Sheppo 	ASSERT(dip != NULL);
5811ae08745Sheppo 
5821ae08745Sheppo 	instance = ddi_get_instance(dip);
5831ae08745Sheppo 	if (ddi_soft_state_zalloc(vdc_state, instance) != DDI_SUCCESS) {
584e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't alloc state structure",
585e1ebb9ecSlm66018 		    instance);
5861ae08745Sheppo 		return (DDI_FAILURE);
5871ae08745Sheppo 	}
5881ae08745Sheppo 
5891ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
590e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
5911ae08745Sheppo 		return (DDI_FAILURE);
5921ae08745Sheppo 	}
5931ae08745Sheppo 
5941ae08745Sheppo 	/*
5951ae08745Sheppo 	 * We assign the value to initialized in this case to zero out the
5961ae08745Sheppo 	 * variable and then set bits in it to indicate what has been done
5971ae08745Sheppo 	 */
5981ae08745Sheppo 	vdc->initialized = VDC_SOFT_STATE;
5991ae08745Sheppo 
6003af08d82Slm66018 	vdc_hz_min_ldc_delay = drv_usectohz(vdc_min_timeout_ldc);
6013af08d82Slm66018 	vdc_hz_max_ldc_delay = drv_usectohz(vdc_max_timeout_ldc);
6021ae08745Sheppo 
6031ae08745Sheppo 	vdc->dip	= dip;
6041ae08745Sheppo 	vdc->instance	= instance;
6051ae08745Sheppo 	vdc->vdisk_type	= VD_DISK_TYPE_UNK;
6064bac2208Snarayan 	vdc->vdisk_label = VD_DISK_LABEL_UNK;
6073af08d82Slm66018 	vdc->state	= VDC_STATE_INIT;
6083af08d82Slm66018 	vdc->lifecycle	= VDC_LC_ATTACHING;
6091ae08745Sheppo 	vdc->ldc_state	= 0;
6101ae08745Sheppo 	vdc->session_id = 0;
6111ae08745Sheppo 	vdc->block_size = DEV_BSIZE;
6128e6a2a04Slm66018 	vdc->max_xfer_sz = maxphys / DEV_BSIZE;
6131ae08745Sheppo 
61417cadca8Slm66018 	/*
61517cadca8Slm66018 	 * We assume, for now, that the vDisk server will export 'read'
61617cadca8Slm66018 	 * operations to us at a minimum (this is needed because of checks
61717cadca8Slm66018 	 * in vdc for supported operations early in the handshake process).
61817cadca8Slm66018 	 * The vDisk server will return ENOTSUP if this is not the case.
61917cadca8Slm66018 	 * The value will be overwritten during the attribute exchange with
62017cadca8Slm66018 	 * the bitmask of operations exported by server.
62117cadca8Slm66018 	 */
62217cadca8Slm66018 	vdc->operations = VD_OP_MASK_READ;
62317cadca8Slm66018 
6241ae08745Sheppo 	vdc->vtoc = NULL;
62578fcd0a1Sachartre 	vdc->geom = NULL;
6261ae08745Sheppo 	vdc->cinfo = NULL;
6271ae08745Sheppo 	vdc->minfo = NULL;
6281ae08745Sheppo 
6291ae08745Sheppo 	mutex_init(&vdc->lock, NULL, MUTEX_DRIVER, NULL);
6303af08d82Slm66018 	cv_init(&vdc->initwait_cv, NULL, CV_DRIVER, NULL);
6313af08d82Slm66018 	cv_init(&vdc->dring_free_cv, NULL, CV_DRIVER, NULL);
6323af08d82Slm66018 	cv_init(&vdc->membind_cv, NULL, CV_DRIVER, NULL);
6333af08d82Slm66018 	cv_init(&vdc->running_cv, NULL, CV_DRIVER, NULL);
6343af08d82Slm66018 
6353af08d82Slm66018 	vdc->threads_pending = 0;
6363af08d82Slm66018 	vdc->sync_op_pending = B_FALSE;
6373af08d82Slm66018 	vdc->sync_op_blocked = B_FALSE;
6383af08d82Slm66018 	cv_init(&vdc->sync_pending_cv, NULL, CV_DRIVER, NULL);
6393af08d82Slm66018 	cv_init(&vdc->sync_blocked_cv, NULL, CV_DRIVER, NULL);
6403af08d82Slm66018 
6412f5224aeSachartre 	mutex_init(&vdc->ownership_lock, NULL, MUTEX_DRIVER, NULL);
6422f5224aeSachartre 	cv_init(&vdc->ownership_cv, NULL, CV_DRIVER, NULL);
6432f5224aeSachartre 	cv_init(&vdc->failfast_cv, NULL, CV_DRIVER, NULL);
6442f5224aeSachartre 	cv_init(&vdc->failfast_io_cv, NULL, CV_DRIVER, NULL);
6452f5224aeSachartre 
6463af08d82Slm66018 	/* init blocking msg read functionality */
6473af08d82Slm66018 	mutex_init(&vdc->read_lock, NULL, MUTEX_DRIVER, NULL);
6483af08d82Slm66018 	cv_init(&vdc->read_cv, NULL, CV_DRIVER, NULL);
6493af08d82Slm66018 	vdc->read_state = VDC_READ_IDLE;
6503af08d82Slm66018 
6511ae08745Sheppo 	vdc->initialized |= VDC_LOCKS;
6521ae08745Sheppo 
653655fd6a9Sachartre 	/* get device and port MD node for this disk instance */
654655fd6a9Sachartre 	if (vdc_get_md_node(dip, &mdp, &vd_node, &vd_port) != 0) {
655655fd6a9Sachartre 		cmn_err(CE_NOTE, "[%d] Could not get machine description node",
656655fd6a9Sachartre 		    instance);
657655fd6a9Sachartre 		return (DDI_FAILURE);
658655fd6a9Sachartre 	}
659655fd6a9Sachartre 
660655fd6a9Sachartre 	/* set the connection timeout */
661655fd6a9Sachartre 	if (vd_port == NULL || (md_get_prop_val(mdp, vd_port,
662655fd6a9Sachartre 	    VDC_MD_TIMEOUT, &vdc->ctimeout) != 0)) {
663655fd6a9Sachartre 		vdc->ctimeout = 0;
664655fd6a9Sachartre 	}
665655fd6a9Sachartre 
6663af08d82Slm66018 	/* initialise LDC channel which will be used to communicate with vds */
667655fd6a9Sachartre 	status = vdc_do_ldc_init(vdc, mdp, vd_node);
668655fd6a9Sachartre 
669655fd6a9Sachartre 	(void) md_fini_handle(mdp);
670655fd6a9Sachartre 
671655fd6a9Sachartre 	if (status != 0) {
6723af08d82Slm66018 		cmn_err(CE_NOTE, "[%d] Couldn't initialize LDC", instance);
6733af08d82Slm66018 		goto return_status;
6743af08d82Slm66018 	}
6753af08d82Slm66018 
6763af08d82Slm66018 	/* initialize the thread responsible for managing state with server */
6773af08d82Slm66018 	vdc->msg_proc_thr = thread_create(NULL, 0, vdc_process_msg_thread,
6781ae08745Sheppo 	    vdc, 0, &p0, TS_RUN, minclsyspri);
6793af08d82Slm66018 	if (vdc->msg_proc_thr == NULL) {
6801ae08745Sheppo 		cmn_err(CE_NOTE, "[%d] Failed to create msg processing thread",
6811ae08745Sheppo 		    instance);
6821ae08745Sheppo 		return (DDI_FAILURE);
6831ae08745Sheppo 	}
6843af08d82Slm66018 
6851ae08745Sheppo 	vdc->initialized |= VDC_THREAD;
6861ae08745Sheppo 
687366a92acSlm66018 	/* Create the kstats for saving the I/O statistics used by iostat(1M) */
688366a92acSlm66018 	vdc_create_io_kstats(vdc);
689366a92acSlm66018 	vdc_create_err_kstats(vdc);
690366a92acSlm66018 
691e1ebb9ecSlm66018 	atomic_inc_32(&vdc_instance_count);
6921ae08745Sheppo 
6930a55fbb7Slm66018 	/*
69478fcd0a1Sachartre 	 * Check the disk label. This will send requests and do the handshake.
69578fcd0a1Sachartre 	 * We don't really care about the disk label now. What we really need is
69678fcd0a1Sachartre 	 * the handshake do be done so that we know the type of the disk (slice
69778fcd0a1Sachartre 	 * or full disk) and the appropriate device nodes can be created.
6980a55fbb7Slm66018 	 */
69978fcd0a1Sachartre 	vdc->vdisk_label = VD_DISK_LABEL_UNK;
70078fcd0a1Sachartre 	vdc->vtoc = kmem_zalloc(sizeof (struct vtoc), KM_SLEEP);
70178fcd0a1Sachartre 	vdc->geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
70217cadca8Slm66018 	vdc->minfo = kmem_zalloc(sizeof (struct dk_minfo), KM_SLEEP);
70378fcd0a1Sachartre 
70478fcd0a1Sachartre 	mutex_enter(&vdc->lock);
70578fcd0a1Sachartre 	(void) vdc_validate_geometry(vdc);
70678fcd0a1Sachartre 	mutex_exit(&vdc->lock);
7071ae08745Sheppo 
7081ae08745Sheppo 	/*
7091ae08745Sheppo 	 * Now that we have the device info we can create the
7101ae08745Sheppo 	 * device nodes and properties
7111ae08745Sheppo 	 */
7121ae08745Sheppo 	status = vdc_create_device_nodes(vdc);
7131ae08745Sheppo 	if (status) {
7143af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to create device nodes",
7151ae08745Sheppo 		    instance);
7163af08d82Slm66018 		goto return_status;
7171ae08745Sheppo 	}
7181ae08745Sheppo 	status = vdc_create_device_nodes_props(vdc);
7191ae08745Sheppo 	if (status) {
7203af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to create device nodes"
7210a55fbb7Slm66018 		    " properties (%d)", instance, status);
7223af08d82Slm66018 		goto return_status;
7231ae08745Sheppo 	}
7241ae08745Sheppo 
7254bac2208Snarayan 	/*
7264bac2208Snarayan 	 * Setup devid
7274bac2208Snarayan 	 */
7284bac2208Snarayan 	if (vdc_setup_devid(vdc)) {
7293af08d82Slm66018 		DMSG(vdc, 0, "[%d] No device id available\n", instance);
7304bac2208Snarayan 	}
7314bac2208Snarayan 
732366a92acSlm66018 	/*
733366a92acSlm66018 	 * Fill in the fields of the error statistics kstat that were not
734366a92acSlm66018 	 * available when creating the kstat
735366a92acSlm66018 	 */
736366a92acSlm66018 	vdc_set_err_kstats(vdc);
737366a92acSlm66018 
7381ae08745Sheppo 	ddi_report_dev(dip);
7393af08d82Slm66018 	vdc->lifecycle	= VDC_LC_ONLINE;
7403af08d82Slm66018 	DMSG(vdc, 0, "[%d] Attach tasks successful\n", instance);
7411ae08745Sheppo 
7423af08d82Slm66018 return_status:
7433af08d82Slm66018 	DMSG(vdc, 0, "[%d] Attach completed\n", instance);
7441ae08745Sheppo 	return (status);
7451ae08745Sheppo }
7461ae08745Sheppo 
7471ae08745Sheppo static int
7481ae08745Sheppo vdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
7491ae08745Sheppo {
7501ae08745Sheppo 	int	status;
7511ae08745Sheppo 
7521ae08745Sheppo 	switch (cmd) {
7531ae08745Sheppo 	case DDI_ATTACH:
7541ae08745Sheppo 		if ((status = vdc_do_attach(dip)) != 0)
7551ae08745Sheppo 			(void) vdc_detach(dip, DDI_DETACH);
7561ae08745Sheppo 		return (status);
7571ae08745Sheppo 	case DDI_RESUME:
7581ae08745Sheppo 		/* nothing to do for this non-device */
7591ae08745Sheppo 		return (DDI_SUCCESS);
7601ae08745Sheppo 	default:
7611ae08745Sheppo 		return (DDI_FAILURE);
7621ae08745Sheppo 	}
7631ae08745Sheppo }
7641ae08745Sheppo 
7651ae08745Sheppo static int
766655fd6a9Sachartre vdc_do_ldc_init(vdc_t *vdc, md_t *mdp, mde_cookie_t vd_node)
7671ae08745Sheppo {
7681ae08745Sheppo 	int			status = 0;
7691ae08745Sheppo 	ldc_status_t		ldc_state;
7701ae08745Sheppo 	ldc_attr_t		ldc_attr;
7711ae08745Sheppo 	uint64_t		ldc_id = 0;
7721ae08745Sheppo 
7731ae08745Sheppo 	ASSERT(vdc != NULL);
7741ae08745Sheppo 
7751ae08745Sheppo 	vdc->initialized |= VDC_LDC;
7761ae08745Sheppo 
777655fd6a9Sachartre 	if ((status = vdc_get_ldc_id(mdp, vd_node, &ldc_id)) != 0) {
7783af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to get LDC channel ID property",
779e1ebb9ecSlm66018 		    vdc->instance);
7801ae08745Sheppo 		return (EIO);
7811ae08745Sheppo 	}
782655fd6a9Sachartre 
783655fd6a9Sachartre 	DMSGX(0, "[%d] LDC id is 0x%lx\n", vdc->instance, ldc_id);
784655fd6a9Sachartre 
7851ae08745Sheppo 	vdc->ldc_id = ldc_id;
7861ae08745Sheppo 
7871ae08745Sheppo 	ldc_attr.devclass = LDC_DEV_BLK;
7881ae08745Sheppo 	ldc_attr.instance = vdc->instance;
7891ae08745Sheppo 	ldc_attr.mode = LDC_MODE_UNRELIABLE;	/* unreliable transport */
790e1ebb9ecSlm66018 	ldc_attr.mtu = VD_LDC_MTU;
7911ae08745Sheppo 
7921ae08745Sheppo 	if ((vdc->initialized & VDC_LDC_INIT) == 0) {
7931ae08745Sheppo 		status = ldc_init(ldc_id, &ldc_attr, &vdc->ldc_handle);
7941ae08745Sheppo 		if (status != 0) {
7953af08d82Slm66018 			DMSG(vdc, 0, "[%d] ldc_init(chan %ld) returned %d",
7961ae08745Sheppo 			    vdc->instance, ldc_id, status);
7971ae08745Sheppo 			return (status);
7981ae08745Sheppo 		}
7991ae08745Sheppo 		vdc->initialized |= VDC_LDC_INIT;
8001ae08745Sheppo 	}
8011ae08745Sheppo 	status = ldc_status(vdc->ldc_handle, &ldc_state);
8021ae08745Sheppo 	if (status != 0) {
8033af08d82Slm66018 		DMSG(vdc, 0, "[%d] Cannot discover LDC status [err=%d]",
804e1ebb9ecSlm66018 		    vdc->instance, status);
8051ae08745Sheppo 		return (status);
8061ae08745Sheppo 	}
8071ae08745Sheppo 	vdc->ldc_state = ldc_state;
8081ae08745Sheppo 
8091ae08745Sheppo 	if ((vdc->initialized & VDC_LDC_CB) == 0) {
8101ae08745Sheppo 		status = ldc_reg_callback(vdc->ldc_handle, vdc_handle_cb,
8111ae08745Sheppo 		    (caddr_t)vdc);
8121ae08745Sheppo 		if (status != 0) {
8133af08d82Slm66018 			DMSG(vdc, 0, "[%d] LDC callback reg. failed (%d)",
814e1ebb9ecSlm66018 			    vdc->instance, status);
8151ae08745Sheppo 			return (status);
8161ae08745Sheppo 		}
8171ae08745Sheppo 		vdc->initialized |= VDC_LDC_CB;
8181ae08745Sheppo 	}
8191ae08745Sheppo 
8201ae08745Sheppo 	vdc->initialized |= VDC_LDC;
8211ae08745Sheppo 
8221ae08745Sheppo 	/*
8231ae08745Sheppo 	 * At this stage we have initialised LDC, we will now try and open
8241ae08745Sheppo 	 * the connection.
8251ae08745Sheppo 	 */
8261ae08745Sheppo 	if (vdc->ldc_state == LDC_INIT) {
8271ae08745Sheppo 		status = ldc_open(vdc->ldc_handle);
8281ae08745Sheppo 		if (status != 0) {
8293af08d82Slm66018 			DMSG(vdc, 0, "[%d] ldc_open(chan %ld) returned %d",
8301ae08745Sheppo 			    vdc->instance, vdc->ldc_id, status);
8311ae08745Sheppo 			return (status);
8321ae08745Sheppo 		}
8331ae08745Sheppo 		vdc->initialized |= VDC_LDC_OPEN;
8341ae08745Sheppo 	}
8351ae08745Sheppo 
8361ae08745Sheppo 	return (status);
8371ae08745Sheppo }
8381ae08745Sheppo 
8391ae08745Sheppo static int
8401ae08745Sheppo vdc_start_ldc_connection(vdc_t *vdc)
8411ae08745Sheppo {
8421ae08745Sheppo 	int		status = 0;
8431ae08745Sheppo 
8441ae08745Sheppo 	ASSERT(vdc != NULL);
8451ae08745Sheppo 
8463af08d82Slm66018 	ASSERT(MUTEX_HELD(&vdc->lock));
8471ae08745Sheppo 
8480a55fbb7Slm66018 	status = vdc_do_ldc_up(vdc);
8491ae08745Sheppo 
8503af08d82Slm66018 	DMSG(vdc, 0, "[%d] Finished bringing up LDC\n", vdc->instance);
8511ae08745Sheppo 
8523af08d82Slm66018 	return (status);
8533af08d82Slm66018 }
8543af08d82Slm66018 
8553af08d82Slm66018 static int
8563af08d82Slm66018 vdc_stop_ldc_connection(vdc_t *vdcp)
8573af08d82Slm66018 {
8583af08d82Slm66018 	int	status;
8593af08d82Slm66018 
8603af08d82Slm66018 	DMSG(vdcp, 0, ": Resetting connection to vDisk server : state %d\n",
8613af08d82Slm66018 	    vdcp->state);
8623af08d82Slm66018 
8633af08d82Slm66018 	status = ldc_down(vdcp->ldc_handle);
8643af08d82Slm66018 	DMSG(vdcp, 0, "ldc_down() = %d\n", status);
8653af08d82Slm66018 
8663af08d82Slm66018 	vdcp->initialized &= ~VDC_HANDSHAKE;
8673af08d82Slm66018 	DMSG(vdcp, 0, "initialized=%x\n", vdcp->initialized);
8681ae08745Sheppo 
8691ae08745Sheppo 	return (status);
8701ae08745Sheppo }
8711ae08745Sheppo 
872366a92acSlm66018 static void
873366a92acSlm66018 vdc_create_io_kstats(vdc_t *vdc)
874366a92acSlm66018 {
875366a92acSlm66018 	if (vdc->io_stats != NULL) {
876366a92acSlm66018 		DMSG(vdc, 0, "[%d] I/O kstat already exists\n", vdc->instance);
877366a92acSlm66018 		return;
878366a92acSlm66018 	}
879366a92acSlm66018 
880366a92acSlm66018 	vdc->io_stats = kstat_create(VDC_DRIVER_NAME, vdc->instance, NULL,
881366a92acSlm66018 	    "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
882366a92acSlm66018 	if (vdc->io_stats != NULL) {
883366a92acSlm66018 		vdc->io_stats->ks_lock = &vdc->lock;
884366a92acSlm66018 		kstat_install(vdc->io_stats);
885366a92acSlm66018 	} else {
886366a92acSlm66018 		cmn_err(CE_NOTE, "[%d] Failed to create kstat: I/O statistics"
887366a92acSlm66018 		    " will not be gathered", vdc->instance);
888366a92acSlm66018 	}
889366a92acSlm66018 }
890366a92acSlm66018 
891366a92acSlm66018 static void
892366a92acSlm66018 vdc_create_err_kstats(vdc_t *vdc)
893366a92acSlm66018 {
894366a92acSlm66018 	vd_err_stats_t	*stp;
895366a92acSlm66018 	char	kstatmodule_err[KSTAT_STRLEN];
896366a92acSlm66018 	char	kstatname[KSTAT_STRLEN];
897366a92acSlm66018 	int	ndata = (sizeof (vd_err_stats_t) / sizeof (kstat_named_t));
898366a92acSlm66018 	int	instance = vdc->instance;
899366a92acSlm66018 
900366a92acSlm66018 	if (vdc->err_stats != NULL) {
901366a92acSlm66018 		DMSG(vdc, 0, "[%d] ERR kstat already exists\n", vdc->instance);
902366a92acSlm66018 		return;
903366a92acSlm66018 	}
904366a92acSlm66018 
905366a92acSlm66018 	(void) snprintf(kstatmodule_err, sizeof (kstatmodule_err),
906366a92acSlm66018 	    "%serr", VDC_DRIVER_NAME);
907366a92acSlm66018 	(void) snprintf(kstatname, sizeof (kstatname),
908366a92acSlm66018 	    "%s%d,err", VDC_DRIVER_NAME, instance);
909366a92acSlm66018 
910366a92acSlm66018 	vdc->err_stats = kstat_create(kstatmodule_err, instance, kstatname,
911366a92acSlm66018 	    "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT);
912366a92acSlm66018 
913366a92acSlm66018 	if (vdc->err_stats == NULL) {
914366a92acSlm66018 		cmn_err(CE_NOTE, "[%d] Failed to create kstat: Error statistics"
915366a92acSlm66018 		    " will not be gathered", instance);
916366a92acSlm66018 		return;
917366a92acSlm66018 	}
918366a92acSlm66018 
919366a92acSlm66018 	stp = (vd_err_stats_t *)vdc->err_stats->ks_data;
920366a92acSlm66018 	kstat_named_init(&stp->vd_softerrs,	"Soft Errors",
921366a92acSlm66018 	    KSTAT_DATA_UINT32);
922366a92acSlm66018 	kstat_named_init(&stp->vd_transerrs,	"Transport Errors",
923366a92acSlm66018 	    KSTAT_DATA_UINT32);
924366a92acSlm66018 	kstat_named_init(&stp->vd_protoerrs,	"Protocol Errors",
925366a92acSlm66018 	    KSTAT_DATA_UINT32);
926366a92acSlm66018 	kstat_named_init(&stp->vd_vid,		"Vendor",
927366a92acSlm66018 	    KSTAT_DATA_CHAR);
928366a92acSlm66018 	kstat_named_init(&stp->vd_pid,		"Product",
929366a92acSlm66018 	    KSTAT_DATA_CHAR);
930366a92acSlm66018 	kstat_named_init(&stp->vd_capacity,	"Size",
931366a92acSlm66018 	    KSTAT_DATA_ULONGLONG);
932366a92acSlm66018 
933366a92acSlm66018 	vdc->err_stats->ks_update  = nulldev;
934366a92acSlm66018 
935366a92acSlm66018 	kstat_install(vdc->err_stats);
936366a92acSlm66018 }
937366a92acSlm66018 
938366a92acSlm66018 static void
939366a92acSlm66018 vdc_set_err_kstats(vdc_t *vdc)
940366a92acSlm66018 {
941366a92acSlm66018 	vd_err_stats_t  *stp;
942366a92acSlm66018 
943366a92acSlm66018 	if (vdc->err_stats == NULL)
944366a92acSlm66018 		return;
945366a92acSlm66018 
946366a92acSlm66018 	mutex_enter(&vdc->lock);
947366a92acSlm66018 
948366a92acSlm66018 	stp = (vd_err_stats_t *)vdc->err_stats->ks_data;
949366a92acSlm66018 	ASSERT(stp != NULL);
950366a92acSlm66018 
951366a92acSlm66018 	stp->vd_capacity.value.ui64 = vdc->vdisk_size * vdc->block_size;
952366a92acSlm66018 	(void) strcpy(stp->vd_vid.value.c, "SUN");
953366a92acSlm66018 	(void) strcpy(stp->vd_pid.value.c, "VDSK");
954366a92acSlm66018 
955366a92acSlm66018 	mutex_exit(&vdc->lock);
956366a92acSlm66018 }
957366a92acSlm66018 
9584bac2208Snarayan static int
9594bac2208Snarayan vdc_create_device_nodes_efi(vdc_t *vdc)
9604bac2208Snarayan {
9614bac2208Snarayan 	ddi_remove_minor_node(vdc->dip, "h");
9624bac2208Snarayan 	ddi_remove_minor_node(vdc->dip, "h,raw");
9634bac2208Snarayan 
9644bac2208Snarayan 	if (ddi_create_minor_node(vdc->dip, "wd", S_IFBLK,
9654bac2208Snarayan 	    VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
9664bac2208Snarayan 	    DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
9674bac2208Snarayan 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd'",
9684bac2208Snarayan 		    vdc->instance);
9694bac2208Snarayan 		return (EIO);
9704bac2208Snarayan 	}
9714bac2208Snarayan 
9724bac2208Snarayan 	/* if any device node is created we set this flag */
9734bac2208Snarayan 	vdc->initialized |= VDC_MINOR;
9744bac2208Snarayan 
9754bac2208Snarayan 	if (ddi_create_minor_node(vdc->dip, "wd,raw", S_IFCHR,
9764bac2208Snarayan 	    VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
9774bac2208Snarayan 	    DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
9784bac2208Snarayan 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd,raw'",
9794bac2208Snarayan 		    vdc->instance);
9804bac2208Snarayan 		return (EIO);
9814bac2208Snarayan 	}
9824bac2208Snarayan 
9834bac2208Snarayan 	return (0);
9844bac2208Snarayan }
9854bac2208Snarayan 
9864bac2208Snarayan static int
9874bac2208Snarayan vdc_create_device_nodes_vtoc(vdc_t *vdc)
9884bac2208Snarayan {
9894bac2208Snarayan 	ddi_remove_minor_node(vdc->dip, "wd");
9904bac2208Snarayan 	ddi_remove_minor_node(vdc->dip, "wd,raw");
9914bac2208Snarayan 
9924bac2208Snarayan 	if (ddi_create_minor_node(vdc->dip, "h", S_IFBLK,
9934bac2208Snarayan 	    VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
9944bac2208Snarayan 	    DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
9954bac2208Snarayan 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h'",
9964bac2208Snarayan 		    vdc->instance);
9974bac2208Snarayan 		return (EIO);
9984bac2208Snarayan 	}
9994bac2208Snarayan 
10004bac2208Snarayan 	/* if any device node is created we set this flag */
10014bac2208Snarayan 	vdc->initialized |= VDC_MINOR;
10024bac2208Snarayan 
10034bac2208Snarayan 	if (ddi_create_minor_node(vdc->dip, "h,raw", S_IFCHR,
10044bac2208Snarayan 	    VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
10054bac2208Snarayan 	    DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
10064bac2208Snarayan 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h,raw'",
10074bac2208Snarayan 		    vdc->instance);
10084bac2208Snarayan 		return (EIO);
10094bac2208Snarayan 	}
10104bac2208Snarayan 
10114bac2208Snarayan 	return (0);
10124bac2208Snarayan }
10131ae08745Sheppo 
10141ae08745Sheppo /*
10151ae08745Sheppo  * Function:
10161ae08745Sheppo  *	vdc_create_device_nodes
10171ae08745Sheppo  *
10181ae08745Sheppo  * Description:
10191ae08745Sheppo  *	This function creates the block and character device nodes under
10201ae08745Sheppo  *	/devices along with the node properties. It is called as part of
10211ae08745Sheppo  *	the attach(9E) of the instance during the handshake with vds after
10221ae08745Sheppo  *	vds has sent the attributes to vdc.
10231ae08745Sheppo  *
10241ae08745Sheppo  *	If the device is of type VD_DISK_TYPE_SLICE then the minor node
10251ae08745Sheppo  *	of 2 is used in keeping with the Solaris convention that slice 2
10261ae08745Sheppo  *	refers to a whole disk. Slices start at 'a'
10271ae08745Sheppo  *
10281ae08745Sheppo  * Parameters:
10291ae08745Sheppo  *	vdc 		- soft state pointer
10301ae08745Sheppo  *
10311ae08745Sheppo  * Return Values
10321ae08745Sheppo  *	0		- Success
10331ae08745Sheppo  *	EIO		- Failed to create node
10341ae08745Sheppo  *	EINVAL		- Unknown type of disk exported
10351ae08745Sheppo  */
10361ae08745Sheppo static int
10371ae08745Sheppo vdc_create_device_nodes(vdc_t *vdc)
10381ae08745Sheppo {
10394bac2208Snarayan 	char		name[sizeof ("s,raw")];
10401ae08745Sheppo 	dev_info_t	*dip = NULL;
10414bac2208Snarayan 	int		instance, status;
10421ae08745Sheppo 	int		num_slices = 1;
10431ae08745Sheppo 	int		i;
10441ae08745Sheppo 
10451ae08745Sheppo 	ASSERT(vdc != NULL);
10461ae08745Sheppo 
10471ae08745Sheppo 	instance = vdc->instance;
10481ae08745Sheppo 	dip = vdc->dip;
10491ae08745Sheppo 
10501ae08745Sheppo 	switch (vdc->vdisk_type) {
10511ae08745Sheppo 	case VD_DISK_TYPE_DISK:
10521ae08745Sheppo 		num_slices = V_NUMPAR;
10531ae08745Sheppo 		break;
10541ae08745Sheppo 	case VD_DISK_TYPE_SLICE:
10551ae08745Sheppo 		num_slices = 1;
10561ae08745Sheppo 		break;
10571ae08745Sheppo 	case VD_DISK_TYPE_UNK:
10581ae08745Sheppo 	default:
10591ae08745Sheppo 		return (EINVAL);
10601ae08745Sheppo 	}
10611ae08745Sheppo 
10624bac2208Snarayan 	/*
10634bac2208Snarayan 	 * Minor nodes are different for EFI disks: EFI disks do not have
10644bac2208Snarayan 	 * a minor node 'g' for the minor number corresponding to slice
10654bac2208Snarayan 	 * VD_EFI_WD_SLICE (slice 7) instead they have a minor node 'wd'
10664bac2208Snarayan 	 * representing the whole disk.
10674bac2208Snarayan 	 */
10681ae08745Sheppo 	for (i = 0; i < num_slices; i++) {
10694bac2208Snarayan 
10704bac2208Snarayan 		if (i == VD_EFI_WD_SLICE) {
10714bac2208Snarayan 			if (vdc->vdisk_label == VD_DISK_LABEL_EFI)
10724bac2208Snarayan 				status = vdc_create_device_nodes_efi(vdc);
10734bac2208Snarayan 			else
10744bac2208Snarayan 				status = vdc_create_device_nodes_vtoc(vdc);
10754bac2208Snarayan 			if (status != 0)
10764bac2208Snarayan 				return (status);
10774bac2208Snarayan 			continue;
10784bac2208Snarayan 		}
10794bac2208Snarayan 
10801ae08745Sheppo 		(void) snprintf(name, sizeof (name), "%c", 'a' + i);
10811ae08745Sheppo 		if (ddi_create_minor_node(dip, name, S_IFBLK,
10821ae08745Sheppo 		    VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
1083e1ebb9ecSlm66018 			cmn_err(CE_NOTE, "[%d] Couldn't add block node '%s'",
1084e1ebb9ecSlm66018 			    instance, name);
10851ae08745Sheppo 			return (EIO);
10861ae08745Sheppo 		}
10871ae08745Sheppo 
10881ae08745Sheppo 		/* if any device node is created we set this flag */
10891ae08745Sheppo 		vdc->initialized |= VDC_MINOR;
10901ae08745Sheppo 
109187a7269eSachartre 		(void) snprintf(name, sizeof (name), "%c%s", 'a' + i, ",raw");
109287a7269eSachartre 
10931ae08745Sheppo 		if (ddi_create_minor_node(dip, name, S_IFCHR,
10941ae08745Sheppo 		    VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
1095e1ebb9ecSlm66018 			cmn_err(CE_NOTE, "[%d] Couldn't add raw node '%s'",
1096e1ebb9ecSlm66018 			    instance, name);
10971ae08745Sheppo 			return (EIO);
10981ae08745Sheppo 		}
10991ae08745Sheppo 	}
11001ae08745Sheppo 
11011ae08745Sheppo 	return (0);
11021ae08745Sheppo }
11031ae08745Sheppo 
11041ae08745Sheppo /*
11051ae08745Sheppo  * Function:
11061ae08745Sheppo  *	vdc_create_device_nodes_props
11071ae08745Sheppo  *
11081ae08745Sheppo  * Description:
11091ae08745Sheppo  *	This function creates the block and character device nodes under
11101ae08745Sheppo  *	/devices along with the node properties. It is called as part of
11111ae08745Sheppo  *	the attach(9E) of the instance during the handshake with vds after
11121ae08745Sheppo  *	vds has sent the attributes to vdc.
11131ae08745Sheppo  *
11141ae08745Sheppo  * Parameters:
11151ae08745Sheppo  *	vdc 		- soft state pointer
11161ae08745Sheppo  *
11171ae08745Sheppo  * Return Values
11181ae08745Sheppo  *	0		- Success
11191ae08745Sheppo  *	EIO		- Failed to create device node property
11201ae08745Sheppo  *	EINVAL		- Unknown type of disk exported
11211ae08745Sheppo  */
11221ae08745Sheppo static int
11231ae08745Sheppo vdc_create_device_nodes_props(vdc_t *vdc)
11241ae08745Sheppo {
11251ae08745Sheppo 	dev_info_t	*dip = NULL;
11261ae08745Sheppo 	int		instance;
11271ae08745Sheppo 	int		num_slices = 1;
11281ae08745Sheppo 	int64_t		size = 0;
11291ae08745Sheppo 	dev_t		dev;
11301ae08745Sheppo 	int		rv;
11311ae08745Sheppo 	int		i;
11321ae08745Sheppo 
11331ae08745Sheppo 	ASSERT(vdc != NULL);
11341ae08745Sheppo 
11351ae08745Sheppo 	instance = vdc->instance;
11361ae08745Sheppo 	dip = vdc->dip;
11371ae08745Sheppo 
11381ae08745Sheppo 	switch (vdc->vdisk_type) {
11391ae08745Sheppo 	case VD_DISK_TYPE_DISK:
11401ae08745Sheppo 		num_slices = V_NUMPAR;
11411ae08745Sheppo 		break;
11421ae08745Sheppo 	case VD_DISK_TYPE_SLICE:
11431ae08745Sheppo 		num_slices = 1;
11441ae08745Sheppo 		break;
11451ae08745Sheppo 	case VD_DISK_TYPE_UNK:
11461ae08745Sheppo 	default:
11471ae08745Sheppo 		return (EINVAL);
11481ae08745Sheppo 	}
11491ae08745Sheppo 
115078fcd0a1Sachartre 	if (vdc->vdisk_label == VD_DISK_LABEL_UNK) {
115178fcd0a1Sachartre 		/* remove all properties */
115278fcd0a1Sachartre 		for (i = 0; i < num_slices; i++) {
115378fcd0a1Sachartre 			dev = makedevice(ddi_driver_major(dip),
115478fcd0a1Sachartre 			    VD_MAKE_DEV(instance, i));
115578fcd0a1Sachartre 			(void) ddi_prop_remove(dev, dip, VDC_SIZE_PROP_NAME);
115678fcd0a1Sachartre 			(void) ddi_prop_remove(dev, dip, VDC_NBLOCKS_PROP_NAME);
115778fcd0a1Sachartre 		}
115878fcd0a1Sachartre 		return (0);
115978fcd0a1Sachartre 	}
116078fcd0a1Sachartre 
11611ae08745Sheppo 	for (i = 0; i < num_slices; i++) {
11621ae08745Sheppo 		dev = makedevice(ddi_driver_major(dip),
11631ae08745Sheppo 		    VD_MAKE_DEV(instance, i));
11641ae08745Sheppo 
1165edcc0754Sachartre 		size = vdc->slice[i].nblocks * vdc->block_size;
11663af08d82Slm66018 		DMSG(vdc, 0, "[%d] sz %ld (%ld Mb)  p_size %lx\n",
1167e1ebb9ecSlm66018 		    instance, size, size / (1024 * 1024),
1168edcc0754Sachartre 		    vdc->slice[i].nblocks);
11691ae08745Sheppo 
11701ae08745Sheppo 		rv = ddi_prop_update_int64(dev, dip, VDC_SIZE_PROP_NAME, size);
11711ae08745Sheppo 		if (rv != DDI_PROP_SUCCESS) {
1172e1ebb9ecSlm66018 			cmn_err(CE_NOTE, "[%d] Couldn't add '%s' prop of [%ld]",
1173e1ebb9ecSlm66018 			    instance, VDC_SIZE_PROP_NAME, size);
11741ae08745Sheppo 			return (EIO);
11751ae08745Sheppo 		}
11761ae08745Sheppo 
11771ae08745Sheppo 		rv = ddi_prop_update_int64(dev, dip, VDC_NBLOCKS_PROP_NAME,
11781ae08745Sheppo 		    lbtodb(size));
11791ae08745Sheppo 		if (rv != DDI_PROP_SUCCESS) {
1180e1ebb9ecSlm66018 			cmn_err(CE_NOTE, "[%d] Couldn't add '%s' prop [%llu]",
11811ae08745Sheppo 			    instance, VDC_NBLOCKS_PROP_NAME, lbtodb(size));
11821ae08745Sheppo 			return (EIO);
11831ae08745Sheppo 		}
11841ae08745Sheppo 	}
11851ae08745Sheppo 
11861ae08745Sheppo 	return (0);
11871ae08745Sheppo }
11881ae08745Sheppo 
118978fcd0a1Sachartre /*
119078fcd0a1Sachartre  * Function:
119178fcd0a1Sachartre  *	vdc_is_opened
119278fcd0a1Sachartre  *
119378fcd0a1Sachartre  * Description:
119478fcd0a1Sachartre  *	This function checks if any slice of a given virtual disk is
119578fcd0a1Sachartre  *	currently opened.
119678fcd0a1Sachartre  *
119778fcd0a1Sachartre  * Parameters:
119878fcd0a1Sachartre  *	vdc 		- soft state pointer
119978fcd0a1Sachartre  *
120078fcd0a1Sachartre  * Return Values
120178fcd0a1Sachartre  *	B_TRUE		- at least one slice is opened.
120278fcd0a1Sachartre  *	B_FALSE		- no slice is opened.
120378fcd0a1Sachartre  */
120478fcd0a1Sachartre static boolean_t
120578fcd0a1Sachartre vdc_is_opened(vdc_t *vdc)
120678fcd0a1Sachartre {
120778fcd0a1Sachartre 	int i, nslices;
120878fcd0a1Sachartre 
120978fcd0a1Sachartre 	switch (vdc->vdisk_type) {
121078fcd0a1Sachartre 	case VD_DISK_TYPE_DISK:
121178fcd0a1Sachartre 		nslices = V_NUMPAR;
121278fcd0a1Sachartre 		break;
121378fcd0a1Sachartre 	case VD_DISK_TYPE_SLICE:
121478fcd0a1Sachartre 		nslices = 1;
121578fcd0a1Sachartre 		break;
121678fcd0a1Sachartre 	case VD_DISK_TYPE_UNK:
121778fcd0a1Sachartre 	default:
121878fcd0a1Sachartre 		ASSERT(0);
121978fcd0a1Sachartre 	}
122078fcd0a1Sachartre 
122178fcd0a1Sachartre 	/* check if there's any layered open */
122278fcd0a1Sachartre 	for (i = 0; i < nslices; i++) {
122378fcd0a1Sachartre 		if (vdc->open_lyr[i] > 0)
122478fcd0a1Sachartre 			return (B_TRUE);
122578fcd0a1Sachartre 	}
122678fcd0a1Sachartre 
122778fcd0a1Sachartre 	/* check if there is any other kind of open */
122878fcd0a1Sachartre 	for (i = 0; i < OTYPCNT; i++) {
122978fcd0a1Sachartre 		if (vdc->open[i] != 0)
123078fcd0a1Sachartre 			return (B_TRUE);
123178fcd0a1Sachartre 	}
123278fcd0a1Sachartre 
123378fcd0a1Sachartre 	return (B_FALSE);
123478fcd0a1Sachartre }
123578fcd0a1Sachartre 
123678fcd0a1Sachartre static int
123778fcd0a1Sachartre vdc_mark_opened(vdc_t *vdc, int slice, int flag, int otyp)
123878fcd0a1Sachartre {
123978fcd0a1Sachartre 	uint8_t slicemask;
124078fcd0a1Sachartre 	int i;
124178fcd0a1Sachartre 
124278fcd0a1Sachartre 	ASSERT(otyp < OTYPCNT);
124378fcd0a1Sachartre 	ASSERT(slice < V_NUMPAR);
124478fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
124578fcd0a1Sachartre 
124678fcd0a1Sachartre 	slicemask = 1 << slice;
124778fcd0a1Sachartre 
124878fcd0a1Sachartre 	/* check if slice is already exclusively opened */
124978fcd0a1Sachartre 	if (vdc->open_excl & slicemask)
125078fcd0a1Sachartre 		return (EBUSY);
125178fcd0a1Sachartre 
125278fcd0a1Sachartre 	/* if open exclusive, check if slice is already opened */
125378fcd0a1Sachartre 	if (flag & FEXCL) {
125478fcd0a1Sachartre 		if (vdc->open_lyr[slice] > 0)
125578fcd0a1Sachartre 			return (EBUSY);
125678fcd0a1Sachartre 		for (i = 0; i < OTYPCNT; i++) {
125778fcd0a1Sachartre 			if (vdc->open[i] & slicemask)
125878fcd0a1Sachartre 				return (EBUSY);
125978fcd0a1Sachartre 		}
126078fcd0a1Sachartre 		vdc->open_excl |= slicemask;
126178fcd0a1Sachartre 	}
126278fcd0a1Sachartre 
126378fcd0a1Sachartre 	/* mark slice as opened */
126478fcd0a1Sachartre 	if (otyp == OTYP_LYR) {
126578fcd0a1Sachartre 		vdc->open_lyr[slice]++;
126678fcd0a1Sachartre 	} else {
126778fcd0a1Sachartre 		vdc->open[otyp] |= slicemask;
126878fcd0a1Sachartre 	}
126978fcd0a1Sachartre 
127078fcd0a1Sachartre 	return (0);
127178fcd0a1Sachartre }
127278fcd0a1Sachartre 
127378fcd0a1Sachartre static void
127478fcd0a1Sachartre vdc_mark_closed(vdc_t *vdc, int slice, int flag, int otyp)
127578fcd0a1Sachartre {
127678fcd0a1Sachartre 	uint8_t slicemask;
127778fcd0a1Sachartre 
127878fcd0a1Sachartre 	ASSERT(otyp < OTYPCNT);
127978fcd0a1Sachartre 	ASSERT(slice < V_NUMPAR);
128078fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
128178fcd0a1Sachartre 
128278fcd0a1Sachartre 	slicemask = 1 << slice;
128378fcd0a1Sachartre 
128478fcd0a1Sachartre 	if (otyp == OTYP_LYR) {
128578fcd0a1Sachartre 		ASSERT(vdc->open_lyr[slice] > 0);
128678fcd0a1Sachartre 		vdc->open_lyr[slice]--;
128778fcd0a1Sachartre 	} else {
128878fcd0a1Sachartre 		vdc->open[otyp] &= ~slicemask;
128978fcd0a1Sachartre 	}
129078fcd0a1Sachartre 
129178fcd0a1Sachartre 	if (flag & FEXCL)
129278fcd0a1Sachartre 		vdc->open_excl &= ~slicemask;
129378fcd0a1Sachartre }
129478fcd0a1Sachartre 
12951ae08745Sheppo static int
12961ae08745Sheppo vdc_open(dev_t *dev, int flag, int otyp, cred_t *cred)
12971ae08745Sheppo {
12981ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
12991ae08745Sheppo 
1300179e09c2Sachartre 	int	instance, nodelay;
130178fcd0a1Sachartre 	int	slice, status = 0;
13021ae08745Sheppo 	vdc_t	*vdc;
13031ae08745Sheppo 
13041ae08745Sheppo 	ASSERT(dev != NULL);
13050d0c8d4bSnarayan 	instance = VDCUNIT(*dev);
13061ae08745Sheppo 
130778fcd0a1Sachartre 	if (otyp >= OTYPCNT)
13081ae08745Sheppo 		return (EINVAL);
13091ae08745Sheppo 
13101ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1311e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
13121ae08745Sheppo 		return (ENXIO);
13131ae08745Sheppo 	}
13141ae08745Sheppo 
13153af08d82Slm66018 	DMSG(vdc, 0, "minor = %d flag = %x, otyp = %x\n",
13163af08d82Slm66018 	    getminor(*dev), flag, otyp);
13171ae08745Sheppo 
131878fcd0a1Sachartre 	slice = VDCPART(*dev);
131978fcd0a1Sachartre 
1320179e09c2Sachartre 	nodelay = flag & (FNDELAY | FNONBLOCK);
1321179e09c2Sachartre 
1322179e09c2Sachartre 	if ((flag & FWRITE) && (!nodelay) &&
1323179e09c2Sachartre 	    !(VD_OP_SUPPORTED(vdc->operations, VD_OP_BWRITE))) {
1324179e09c2Sachartre 		return (EROFS);
1325179e09c2Sachartre 	}
1326179e09c2Sachartre 
13271ae08745Sheppo 	mutex_enter(&vdc->lock);
132878fcd0a1Sachartre 
132978fcd0a1Sachartre 	status = vdc_mark_opened(vdc, slice, flag, otyp);
133078fcd0a1Sachartre 
133178fcd0a1Sachartre 	if (status != 0) {
133278fcd0a1Sachartre 		mutex_exit(&vdc->lock);
133378fcd0a1Sachartre 		return (status);
133478fcd0a1Sachartre 	}
133578fcd0a1Sachartre 
1336179e09c2Sachartre 	if (nodelay) {
133778fcd0a1Sachartre 
133878fcd0a1Sachartre 		/* don't resubmit a validate request if there's already one */
133978fcd0a1Sachartre 		if (vdc->validate_pending > 0) {
134078fcd0a1Sachartre 			mutex_exit(&vdc->lock);
134178fcd0a1Sachartre 			return (0);
134278fcd0a1Sachartre 		}
134378fcd0a1Sachartre 
134478fcd0a1Sachartre 		/* call vdc_validate() asynchronously to avoid blocking */
134578fcd0a1Sachartre 		if (taskq_dispatch(system_taskq, vdc_validate_task,
134678fcd0a1Sachartre 		    (void *)vdc, TQ_NOSLEEP) == NULL) {
134778fcd0a1Sachartre 			vdc_mark_closed(vdc, slice, flag, otyp);
134878fcd0a1Sachartre 			mutex_exit(&vdc->lock);
134978fcd0a1Sachartre 			return (ENXIO);
135078fcd0a1Sachartre 		}
135178fcd0a1Sachartre 
135278fcd0a1Sachartre 		vdc->validate_pending++;
135378fcd0a1Sachartre 		mutex_exit(&vdc->lock);
135478fcd0a1Sachartre 		return (0);
135578fcd0a1Sachartre 	}
135678fcd0a1Sachartre 
13571ae08745Sheppo 	mutex_exit(&vdc->lock);
13581ae08745Sheppo 
135978fcd0a1Sachartre 	vdc_validate(vdc);
136078fcd0a1Sachartre 
136178fcd0a1Sachartre 	mutex_enter(&vdc->lock);
136278fcd0a1Sachartre 
136378fcd0a1Sachartre 	if (vdc->vdisk_label == VD_DISK_LABEL_UNK ||
1364edcc0754Sachartre 	    vdc->slice[slice].nblocks == 0) {
136578fcd0a1Sachartre 		vdc_mark_closed(vdc, slice, flag, otyp);
136678fcd0a1Sachartre 		status = EIO;
136778fcd0a1Sachartre 	}
136878fcd0a1Sachartre 
136978fcd0a1Sachartre 	mutex_exit(&vdc->lock);
137078fcd0a1Sachartre 
137178fcd0a1Sachartre 	return (status);
13721ae08745Sheppo }
13731ae08745Sheppo 
13741ae08745Sheppo static int
13751ae08745Sheppo vdc_close(dev_t dev, int flag, int otyp, cred_t *cred)
13761ae08745Sheppo {
13771ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
13781ae08745Sheppo 
13791ae08745Sheppo 	int	instance;
138078fcd0a1Sachartre 	int	slice;
13812f5224aeSachartre 	int	rv, rval;
13821ae08745Sheppo 	vdc_t	*vdc;
13831ae08745Sheppo 
13840d0c8d4bSnarayan 	instance = VDCUNIT(dev);
13851ae08745Sheppo 
138678fcd0a1Sachartre 	if (otyp >= OTYPCNT)
13871ae08745Sheppo 		return (EINVAL);
13881ae08745Sheppo 
13891ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1390e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
13911ae08745Sheppo 		return (ENXIO);
13921ae08745Sheppo 	}
13931ae08745Sheppo 
13943af08d82Slm66018 	DMSG(vdc, 0, "[%d] flag = %x, otyp = %x\n", instance, flag, otyp);
13951ae08745Sheppo 
139678fcd0a1Sachartre 	slice = VDCPART(dev);
139778fcd0a1Sachartre 
13988259acd8Szk194757 	/*
13998259acd8Szk194757 	 * Attempt to flush the W$ on a close operation. If this is
14008259acd8Szk194757 	 * not a supported IOCTL command or the backing device is read-only
14018259acd8Szk194757 	 * do not fail the close operation.
14028259acd8Szk194757 	 */
14032f5224aeSachartre 	rv = vd_process_ioctl(dev, DKIOCFLUSHWRITECACHE, NULL, FKIOCTL, &rval);
14048259acd8Szk194757 
14058259acd8Szk194757 	if (rv != 0 && rv != ENOTSUP && rv != ENOTTY && rv != EROFS) {
14068259acd8Szk194757 		DMSG(vdc, 0, "[%d] flush failed with error %d on close\n",
14078259acd8Szk194757 		    instance, rv);
14088259acd8Szk194757 		return (EIO);
14098259acd8Szk194757 	}
14108259acd8Szk194757 
14111ae08745Sheppo 	mutex_enter(&vdc->lock);
141278fcd0a1Sachartre 	vdc_mark_closed(vdc, slice, flag, otyp);
14131ae08745Sheppo 	mutex_exit(&vdc->lock);
14141ae08745Sheppo 
14151ae08745Sheppo 	return (0);
14161ae08745Sheppo }
14171ae08745Sheppo 
14181ae08745Sheppo static int
14191ae08745Sheppo vdc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
14201ae08745Sheppo {
14211ae08745Sheppo 	_NOTE(ARGUNUSED(credp))
14221ae08745Sheppo 
14232f5224aeSachartre 	return (vd_process_ioctl(dev, cmd, (caddr_t)arg, mode, rvalp));
14241ae08745Sheppo }
14251ae08745Sheppo 
14261ae08745Sheppo static int
14271ae08745Sheppo vdc_print(dev_t dev, char *str)
14281ae08745Sheppo {
14290d0c8d4bSnarayan 	cmn_err(CE_NOTE, "vdc%d:  %s", VDCUNIT(dev), str);
14301ae08745Sheppo 	return (0);
14311ae08745Sheppo }
14321ae08745Sheppo 
14331ae08745Sheppo static int
14341ae08745Sheppo vdc_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
14351ae08745Sheppo {
1436d10e4ef2Snarayan 	int	rv;
1437d10e4ef2Snarayan 	size_t	nbytes = nblk * DEV_BSIZE;
14380d0c8d4bSnarayan 	int	instance = VDCUNIT(dev);
1439d10e4ef2Snarayan 	vdc_t	*vdc = NULL;
14401ae08745Sheppo 
14411ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1442e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
14431ae08745Sheppo 		return (ENXIO);
14441ae08745Sheppo 	}
14451ae08745Sheppo 
14463af08d82Slm66018 	DMSG(vdc, 2, "[%d] dump %ld bytes at block 0x%lx : addr=0x%p\n",
14473af08d82Slm66018 	    instance, nbytes, blkno, (void *)addr);
14483af08d82Slm66018 	rv = vdc_send_request(vdc, VD_OP_BWRITE, addr, nbytes,
14490d0c8d4bSnarayan 	    VDCPART(dev), blkno, CB_STRATEGY, 0, VIO_write_dir);
14503af08d82Slm66018 	if (rv) {
14513af08d82Slm66018 		DMSG(vdc, 0, "Failed to do a disk dump (err=%d)\n", rv);
14521ae08745Sheppo 		return (rv);
14531ae08745Sheppo 	}
14541ae08745Sheppo 
14553af08d82Slm66018 	if (ddi_in_panic())
14563af08d82Slm66018 		(void) vdc_drain_response(vdc);
14573af08d82Slm66018 
14583af08d82Slm66018 	DMSG(vdc, 0, "[%d] End\n", instance);
14593af08d82Slm66018 
14603af08d82Slm66018 	return (0);
14613af08d82Slm66018 }
14623af08d82Slm66018 
14631ae08745Sheppo /* -------------------------------------------------------------------------- */
14641ae08745Sheppo 
14651ae08745Sheppo /*
14661ae08745Sheppo  * Disk access routines
14671ae08745Sheppo  *
14681ae08745Sheppo  */
14691ae08745Sheppo 
14701ae08745Sheppo /*
14711ae08745Sheppo  * vdc_strategy()
14721ae08745Sheppo  *
14731ae08745Sheppo  * Return Value:
14741ae08745Sheppo  *	0:	As per strategy(9E), the strategy() function must return 0
14751ae08745Sheppo  *		[ bioerror(9f) sets b_flags to the proper error code ]
14761ae08745Sheppo  */
14771ae08745Sheppo static int
14781ae08745Sheppo vdc_strategy(struct buf *buf)
14791ae08745Sheppo {
14801ae08745Sheppo 	int	rv = -1;
14811ae08745Sheppo 	vdc_t	*vdc = NULL;
14820d0c8d4bSnarayan 	int	instance = VDCUNIT(buf->b_edev);
14831ae08745Sheppo 	int	op = (buf->b_flags & B_READ) ? VD_OP_BREAD : VD_OP_BWRITE;
148487a7269eSachartre 	int	slice;
14851ae08745Sheppo 
14861ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1487e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
14881ae08745Sheppo 		bioerror(buf, ENXIO);
14891ae08745Sheppo 		biodone(buf);
14901ae08745Sheppo 		return (0);
14911ae08745Sheppo 	}
14921ae08745Sheppo 
14933af08d82Slm66018 	DMSG(vdc, 2, "[%d] %s %ld bytes at block %llx : b_addr=0x%p\n",
14943af08d82Slm66018 	    instance, (buf->b_flags & B_READ) ? "Read" : "Write",
14953af08d82Slm66018 	    buf->b_bcount, buf->b_lblkno, (void *)buf->b_un.b_addr);
1496d10e4ef2Snarayan 
14971ae08745Sheppo 	bp_mapin(buf);
14981ae08745Sheppo 
149987a7269eSachartre 	if ((long)buf->b_private == VD_SLICE_NONE) {
150087a7269eSachartre 		/* I/O using an absolute disk offset */
150187a7269eSachartre 		slice = VD_SLICE_NONE;
150287a7269eSachartre 	} else {
150387a7269eSachartre 		slice = VDCPART(buf->b_edev);
150487a7269eSachartre 	}
150587a7269eSachartre 
15063af08d82Slm66018 	rv = vdc_send_request(vdc, op, (caddr_t)buf->b_un.b_addr,
150787a7269eSachartre 	    buf->b_bcount, slice, buf->b_lblkno,
15083af08d82Slm66018 	    CB_STRATEGY, buf, (op == VD_OP_BREAD) ? VIO_read_dir :
15093af08d82Slm66018 	    VIO_write_dir);
15103af08d82Slm66018 
1511d10e4ef2Snarayan 	/*
1512d10e4ef2Snarayan 	 * If the request was successfully sent, the strategy call returns and
1513d10e4ef2Snarayan 	 * the ACK handler calls the bioxxx functions when the vDisk server is
1514366a92acSlm66018 	 * done otherwise we handle the error here.
1515d10e4ef2Snarayan 	 */
1516d10e4ef2Snarayan 	if (rv) {
15173af08d82Slm66018 		DMSG(vdc, 0, "Failed to read/write (err=%d)\n", rv);
15181ae08745Sheppo 		bioerror(buf, rv);
15191ae08745Sheppo 		biodone(buf);
1520d10e4ef2Snarayan 	}
1521d10e4ef2Snarayan 
15221ae08745Sheppo 	return (0);
15231ae08745Sheppo }
15241ae08745Sheppo 
15250d0c8d4bSnarayan /*
15260d0c8d4bSnarayan  * Function:
15270d0c8d4bSnarayan  *	vdc_min
15280d0c8d4bSnarayan  *
15290d0c8d4bSnarayan  * Description:
15300d0c8d4bSnarayan  *	Routine to limit the size of a data transfer. Used in
15310d0c8d4bSnarayan  *	conjunction with physio(9F).
15320d0c8d4bSnarayan  *
15330d0c8d4bSnarayan  * Arguments:
15340d0c8d4bSnarayan  *	bp - pointer to the indicated buf(9S) struct.
15350d0c8d4bSnarayan  *
15360d0c8d4bSnarayan  */
15370d0c8d4bSnarayan static void
15380d0c8d4bSnarayan vdc_min(struct buf *bufp)
15390d0c8d4bSnarayan {
15400d0c8d4bSnarayan 	vdc_t	*vdc = NULL;
15410d0c8d4bSnarayan 	int	instance = VDCUNIT(bufp->b_edev);
15420d0c8d4bSnarayan 
15430d0c8d4bSnarayan 	vdc = ddi_get_soft_state(vdc_state, instance);
15440d0c8d4bSnarayan 	VERIFY(vdc != NULL);
15450d0c8d4bSnarayan 
15460d0c8d4bSnarayan 	if (bufp->b_bcount > (vdc->max_xfer_sz * vdc->block_size)) {
15470d0c8d4bSnarayan 		bufp->b_bcount = vdc->max_xfer_sz * vdc->block_size;
15480d0c8d4bSnarayan 	}
15490d0c8d4bSnarayan }
15501ae08745Sheppo 
15511ae08745Sheppo static int
15521ae08745Sheppo vdc_read(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_READ, vdc_min, uio));
15581ae08745Sheppo }
15591ae08745Sheppo 
15601ae08745Sheppo static int
15611ae08745Sheppo vdc_write(dev_t dev, struct uio *uio, cred_t *cred)
15621ae08745Sheppo {
15631ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
15641ae08745Sheppo 
15650d0c8d4bSnarayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
15660d0c8d4bSnarayan 	return (physio(vdc_strategy, NULL, dev, B_WRITE, vdc_min, uio));
15671ae08745Sheppo }
15681ae08745Sheppo 
15691ae08745Sheppo static int
15701ae08745Sheppo vdc_aread(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_READ, vdc_min, aio));
15761ae08745Sheppo }
15771ae08745Sheppo 
15781ae08745Sheppo static int
15791ae08745Sheppo vdc_awrite(dev_t dev, struct aio_req *aio, cred_t *cred)
15801ae08745Sheppo {
15811ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
15821ae08745Sheppo 
15830d0c8d4bSnarayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
15840d0c8d4bSnarayan 	return (aphysio(vdc_strategy, anocancel, dev, B_WRITE, vdc_min, aio));
15851ae08745Sheppo }
15861ae08745Sheppo 
15871ae08745Sheppo 
15881ae08745Sheppo /* -------------------------------------------------------------------------- */
15891ae08745Sheppo 
15901ae08745Sheppo /*
15911ae08745Sheppo  * Handshake support
15921ae08745Sheppo  */
15931ae08745Sheppo 
15941ae08745Sheppo 
15950a55fbb7Slm66018 /*
15960a55fbb7Slm66018  * Function:
15970a55fbb7Slm66018  *	vdc_init_ver_negotiation()
15980a55fbb7Slm66018  *
15990a55fbb7Slm66018  * Description:
16000a55fbb7Slm66018  *
16010a55fbb7Slm66018  * Arguments:
16020a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
16030a55fbb7Slm66018  *
16040a55fbb7Slm66018  * Return Code:
16050a55fbb7Slm66018  *	0	- Success
16060a55fbb7Slm66018  */
16071ae08745Sheppo static int
16080a55fbb7Slm66018 vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver)
16091ae08745Sheppo {
16101ae08745Sheppo 	vio_ver_msg_t	pkt;
16111ae08745Sheppo 	size_t		msglen = sizeof (pkt);
16121ae08745Sheppo 	int		status = -1;
16131ae08745Sheppo 
16141ae08745Sheppo 	ASSERT(vdc != NULL);
16151ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
16161ae08745Sheppo 
16173af08d82Slm66018 	DMSG(vdc, 0, "[%d] Entered.\n", vdc->instance);
1618e1ebb9ecSlm66018 
16191ae08745Sheppo 	/*
16201ae08745Sheppo 	 * set the Session ID to a unique value
16211ae08745Sheppo 	 * (the lower 32 bits of the clock tick)
16221ae08745Sheppo 	 */
16231ae08745Sheppo 	vdc->session_id = ((uint32_t)gettick() & 0xffffffff);
16243af08d82Slm66018 	DMSG(vdc, 0, "[%d] Set SID to 0x%lx\n", vdc->instance, vdc->session_id);
16251ae08745Sheppo 
16261ae08745Sheppo 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
16271ae08745Sheppo 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
16281ae08745Sheppo 	pkt.tag.vio_subtype_env = VIO_VER_INFO;
16291ae08745Sheppo 	pkt.tag.vio_sid = vdc->session_id;
16301ae08745Sheppo 	pkt.dev_class = VDEV_DISK;
16310a55fbb7Slm66018 	pkt.ver_major = ver.major;
16320a55fbb7Slm66018 	pkt.ver_minor = ver.minor;
16331ae08745Sheppo 
16340a55fbb7Slm66018 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
16353af08d82Slm66018 	DMSG(vdc, 0, "[%d] Ver info sent (status = %d)\n",
16363af08d82Slm66018 	    vdc->instance, status);
16371ae08745Sheppo 	if ((status != 0) || (msglen != sizeof (vio_ver_msg_t))) {
16383af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to send Ver negotiation info: "
163987a7269eSachartre 		    "id(%lx) rv(%d) size(%ld)", vdc->instance, vdc->ldc_handle,
16401ae08745Sheppo 		    status, msglen);
16411ae08745Sheppo 		if (msglen != sizeof (vio_ver_msg_t))
16421ae08745Sheppo 			status = ENOMSG;
16431ae08745Sheppo 	}
16441ae08745Sheppo 
16451ae08745Sheppo 	return (status);
16461ae08745Sheppo }
16471ae08745Sheppo 
16480a55fbb7Slm66018 /*
16490a55fbb7Slm66018  * Function:
16503af08d82Slm66018  *	vdc_ver_negotiation()
16513af08d82Slm66018  *
16523af08d82Slm66018  * Description:
16533af08d82Slm66018  *
16543af08d82Slm66018  * Arguments:
16553af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
16563af08d82Slm66018  *
16573af08d82Slm66018  * Return Code:
16583af08d82Slm66018  *	0	- Success
16593af08d82Slm66018  */
16603af08d82Slm66018 static int
16613af08d82Slm66018 vdc_ver_negotiation(vdc_t *vdcp)
16623af08d82Slm66018 {
16633af08d82Slm66018 	vio_msg_t vio_msg;
16643af08d82Slm66018 	int status;
16653af08d82Slm66018 
16663af08d82Slm66018 	if (status = vdc_init_ver_negotiation(vdcp, vdc_version[0]))
16673af08d82Slm66018 		return (status);
16683af08d82Slm66018 
16693af08d82Slm66018 	/* release lock and wait for response */
16703af08d82Slm66018 	mutex_exit(&vdcp->lock);
16713af08d82Slm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
16723af08d82Slm66018 	mutex_enter(&vdcp->lock);
16733af08d82Slm66018 	if (status) {
16743af08d82Slm66018 		DMSG(vdcp, 0,
16753af08d82Slm66018 		    "[%d] Failed waiting for Ver negotiation response, rv(%d)",
16763af08d82Slm66018 		    vdcp->instance, status);
16773af08d82Slm66018 		return (status);
16783af08d82Slm66018 	}
16793af08d82Slm66018 
16803af08d82Slm66018 	/* check type and sub_type ... */
16813af08d82Slm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
16823af08d82Slm66018 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
16833af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Invalid ver negotiation response\n",
16843af08d82Slm66018 		    vdcp->instance);
16853af08d82Slm66018 		return (EPROTO);
16863af08d82Slm66018 	}
16873af08d82Slm66018 
16883af08d82Slm66018 	return (vdc_handle_ver_msg(vdcp, (vio_ver_msg_t *)&vio_msg));
16893af08d82Slm66018 }
16903af08d82Slm66018 
16913af08d82Slm66018 /*
16923af08d82Slm66018  * Function:
16930a55fbb7Slm66018  *	vdc_init_attr_negotiation()
16940a55fbb7Slm66018  *
16950a55fbb7Slm66018  * Description:
16960a55fbb7Slm66018  *
16970a55fbb7Slm66018  * Arguments:
16980a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
16990a55fbb7Slm66018  *
17000a55fbb7Slm66018  * Return Code:
17010a55fbb7Slm66018  *	0	- Success
17020a55fbb7Slm66018  */
17031ae08745Sheppo static int
17041ae08745Sheppo vdc_init_attr_negotiation(vdc_t *vdc)
17051ae08745Sheppo {
17061ae08745Sheppo 	vd_attr_msg_t	pkt;
17071ae08745Sheppo 	size_t		msglen = sizeof (pkt);
17081ae08745Sheppo 	int		status;
17091ae08745Sheppo 
17101ae08745Sheppo 	ASSERT(vdc != NULL);
17111ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
17121ae08745Sheppo 
17133af08d82Slm66018 	DMSG(vdc, 0, "[%d] entered\n", vdc->instance);
17141ae08745Sheppo 
17151ae08745Sheppo 	/* fill in tag */
17161ae08745Sheppo 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
17171ae08745Sheppo 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
17181ae08745Sheppo 	pkt.tag.vio_subtype_env = VIO_ATTR_INFO;
17191ae08745Sheppo 	pkt.tag.vio_sid = vdc->session_id;
17201ae08745Sheppo 	/* fill in payload */
17211ae08745Sheppo 	pkt.max_xfer_sz = vdc->max_xfer_sz;
17221ae08745Sheppo 	pkt.vdisk_block_size = vdc->block_size;
1723f0ca1d9aSsb155480 	pkt.xfer_mode = VIO_DRING_MODE_V1_0;
17241ae08745Sheppo 	pkt.operations = 0;	/* server will set bits of valid operations */
17251ae08745Sheppo 	pkt.vdisk_type = 0;	/* server will set to valid device type */
172617cadca8Slm66018 	pkt.vdisk_media = 0;	/* server will set to valid media type */
17271ae08745Sheppo 	pkt.vdisk_size = 0;	/* server will set to valid size */
17281ae08745Sheppo 
17290a55fbb7Slm66018 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
17303af08d82Slm66018 	DMSG(vdc, 0, "Attr info sent (status = %d)\n", status);
17311ae08745Sheppo 
17321ae08745Sheppo 	if ((status != 0) || (msglen != sizeof (vio_ver_msg_t))) {
17333af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to send Attr negotiation info: "
173487a7269eSachartre 		    "id(%lx) rv(%d) size(%ld)", vdc->instance, vdc->ldc_handle,
17351ae08745Sheppo 		    status, msglen);
17361ae08745Sheppo 		if (msglen != sizeof (vio_ver_msg_t))
17371ae08745Sheppo 			status = ENOMSG;
17381ae08745Sheppo 	}
17391ae08745Sheppo 
17401ae08745Sheppo 	return (status);
17411ae08745Sheppo }
17421ae08745Sheppo 
17430a55fbb7Slm66018 /*
17440a55fbb7Slm66018  * Function:
17453af08d82Slm66018  *	vdc_attr_negotiation()
17463af08d82Slm66018  *
17473af08d82Slm66018  * Description:
17483af08d82Slm66018  *
17493af08d82Slm66018  * Arguments:
17503af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
17513af08d82Slm66018  *
17523af08d82Slm66018  * Return Code:
17533af08d82Slm66018  *	0	- Success
17543af08d82Slm66018  */
17553af08d82Slm66018 static int
17563af08d82Slm66018 vdc_attr_negotiation(vdc_t *vdcp)
17573af08d82Slm66018 {
17583af08d82Slm66018 	int status;
17593af08d82Slm66018 	vio_msg_t vio_msg;
17603af08d82Slm66018 
17613af08d82Slm66018 	if (status = vdc_init_attr_negotiation(vdcp))
17623af08d82Slm66018 		return (status);
17633af08d82Slm66018 
17643af08d82Slm66018 	/* release lock and wait for response */
17653af08d82Slm66018 	mutex_exit(&vdcp->lock);
17663af08d82Slm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
17673af08d82Slm66018 	mutex_enter(&vdcp->lock);
17683af08d82Slm66018 	if (status) {
17693af08d82Slm66018 		DMSG(vdcp, 0,
17703af08d82Slm66018 		    "[%d] Failed waiting for Attr negotiation response, rv(%d)",
17713af08d82Slm66018 		    vdcp->instance, status);
17723af08d82Slm66018 		return (status);
17733af08d82Slm66018 	}
17743af08d82Slm66018 
17753af08d82Slm66018 	/* check type and sub_type ... */
17763af08d82Slm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
17773af08d82Slm66018 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
17783af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Invalid attr negotiation response\n",
17793af08d82Slm66018 		    vdcp->instance);
17803af08d82Slm66018 		return (EPROTO);
17813af08d82Slm66018 	}
17823af08d82Slm66018 
17833af08d82Slm66018 	return (vdc_handle_attr_msg(vdcp, (vd_attr_msg_t *)&vio_msg));
17843af08d82Slm66018 }
17853af08d82Slm66018 
17863af08d82Slm66018 
17873af08d82Slm66018 /*
17883af08d82Slm66018  * Function:
17890a55fbb7Slm66018  *	vdc_init_dring_negotiate()
17900a55fbb7Slm66018  *
17910a55fbb7Slm66018  * Description:
17920a55fbb7Slm66018  *
17930a55fbb7Slm66018  * Arguments:
17940a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
17950a55fbb7Slm66018  *
17960a55fbb7Slm66018  * Return Code:
17970a55fbb7Slm66018  *	0	- Success
17980a55fbb7Slm66018  */
17991ae08745Sheppo static int
18001ae08745Sheppo vdc_init_dring_negotiate(vdc_t *vdc)
18011ae08745Sheppo {
18021ae08745Sheppo 	vio_dring_reg_msg_t	pkt;
18031ae08745Sheppo 	size_t			msglen = sizeof (pkt);
18041ae08745Sheppo 	int			status = -1;
18053af08d82Slm66018 	int			retry;
18063af08d82Slm66018 	int			nretries = 10;
18071ae08745Sheppo 
18081ae08745Sheppo 	ASSERT(vdc != NULL);
18091ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
18101ae08745Sheppo 
18113af08d82Slm66018 	for (retry = 0; retry < nretries; retry++) {
18121ae08745Sheppo 		status = vdc_init_descriptor_ring(vdc);
18133af08d82Slm66018 		if (status != EAGAIN)
18143af08d82Slm66018 			break;
18153af08d82Slm66018 		drv_usecwait(vdc_min_timeout_ldc);
18163af08d82Slm66018 	}
18173af08d82Slm66018 
18181ae08745Sheppo 	if (status != 0) {
18193af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to init DRing (status = %d)\n",
18201ae08745Sheppo 		    vdc->instance, status);
18211ae08745Sheppo 		return (status);
18221ae08745Sheppo 	}
18233af08d82Slm66018 
18243af08d82Slm66018 	DMSG(vdc, 0, "[%d] Init of descriptor ring completed (status = %d)\n",
1825e1ebb9ecSlm66018 	    vdc->instance, status);
18261ae08745Sheppo 
18271ae08745Sheppo 	/* fill in tag */
18281ae08745Sheppo 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
18291ae08745Sheppo 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
18301ae08745Sheppo 	pkt.tag.vio_subtype_env = VIO_DRING_REG;
18311ae08745Sheppo 	pkt.tag.vio_sid = vdc->session_id;
18321ae08745Sheppo 	/* fill in payload */
18331ae08745Sheppo 	pkt.dring_ident = 0;
1834e1ebb9ecSlm66018 	pkt.num_descriptors = vdc->dring_len;
1835e1ebb9ecSlm66018 	pkt.descriptor_size = vdc->dring_entry_size;
18361ae08745Sheppo 	pkt.options = (VIO_TX_DRING | VIO_RX_DRING);
18371ae08745Sheppo 	pkt.ncookies = vdc->dring_cookie_count;
18381ae08745Sheppo 	pkt.cookie[0] = vdc->dring_cookie[0];	/* for now just one cookie */
18391ae08745Sheppo 
18400a55fbb7Slm66018 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
18411ae08745Sheppo 	if (status != 0) {
18423af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to register DRing (err = %d)",
1843e1ebb9ecSlm66018 		    vdc->instance, status);
18441ae08745Sheppo 	}
18451ae08745Sheppo 
18461ae08745Sheppo 	return (status);
18471ae08745Sheppo }
18481ae08745Sheppo 
18491ae08745Sheppo 
18503af08d82Slm66018 /*
18513af08d82Slm66018  * Function:
18523af08d82Slm66018  *	vdc_dring_negotiation()
18533af08d82Slm66018  *
18543af08d82Slm66018  * Description:
18553af08d82Slm66018  *
18563af08d82Slm66018  * Arguments:
18573af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
18583af08d82Slm66018  *
18593af08d82Slm66018  * Return Code:
18603af08d82Slm66018  *	0	- Success
18613af08d82Slm66018  */
18623af08d82Slm66018 static int
18633af08d82Slm66018 vdc_dring_negotiation(vdc_t *vdcp)
18643af08d82Slm66018 {
18653af08d82Slm66018 	int status;
18663af08d82Slm66018 	vio_msg_t vio_msg;
18673af08d82Slm66018 
18683af08d82Slm66018 	if (status = vdc_init_dring_negotiate(vdcp))
18693af08d82Slm66018 		return (status);
18703af08d82Slm66018 
18713af08d82Slm66018 	/* release lock and wait for response */
18723af08d82Slm66018 	mutex_exit(&vdcp->lock);
18733af08d82Slm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
18743af08d82Slm66018 	mutex_enter(&vdcp->lock);
18753af08d82Slm66018 	if (status) {
18763af08d82Slm66018 		DMSG(vdcp, 0,
18773af08d82Slm66018 		    "[%d] Failed waiting for Dring negotiation response,"
18783af08d82Slm66018 		    " rv(%d)", vdcp->instance, status);
18793af08d82Slm66018 		return (status);
18803af08d82Slm66018 	}
18813af08d82Slm66018 
18823af08d82Slm66018 	/* check type and sub_type ... */
18833af08d82Slm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
18843af08d82Slm66018 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
18853af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Invalid Dring negotiation response\n",
18863af08d82Slm66018 		    vdcp->instance);
18873af08d82Slm66018 		return (EPROTO);
18883af08d82Slm66018 	}
18893af08d82Slm66018 
18903af08d82Slm66018 	return (vdc_handle_dring_reg_msg(vdcp,
18913af08d82Slm66018 	    (vio_dring_reg_msg_t *)&vio_msg));
18923af08d82Slm66018 }
18933af08d82Slm66018 
18943af08d82Slm66018 
18953af08d82Slm66018 /*
18963af08d82Slm66018  * Function:
18973af08d82Slm66018  *	vdc_send_rdx()
18983af08d82Slm66018  *
18993af08d82Slm66018  * Description:
19003af08d82Slm66018  *
19013af08d82Slm66018  * Arguments:
19023af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
19033af08d82Slm66018  *
19043af08d82Slm66018  * Return Code:
19053af08d82Slm66018  *	0	- Success
19063af08d82Slm66018  */
19073af08d82Slm66018 static int
19083af08d82Slm66018 vdc_send_rdx(vdc_t *vdcp)
19093af08d82Slm66018 {
19103af08d82Slm66018 	vio_msg_t	msg;
19113af08d82Slm66018 	size_t		msglen = sizeof (vio_msg_t);
19123af08d82Slm66018 	int		status;
19133af08d82Slm66018 
19143af08d82Slm66018 	/*
19153af08d82Slm66018 	 * Send an RDX message to vds to indicate we are ready
19163af08d82Slm66018 	 * to send data
19173af08d82Slm66018 	 */
19183af08d82Slm66018 	msg.tag.vio_msgtype = VIO_TYPE_CTRL;
19193af08d82Slm66018 	msg.tag.vio_subtype = VIO_SUBTYPE_INFO;
19203af08d82Slm66018 	msg.tag.vio_subtype_env = VIO_RDX;
19213af08d82Slm66018 	msg.tag.vio_sid = vdcp->session_id;
19223af08d82Slm66018 	status = vdc_send(vdcp, (caddr_t)&msg, &msglen);
19233af08d82Slm66018 	if (status != 0) {
19243af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Failed to send RDX message (%d)",
19253af08d82Slm66018 		    vdcp->instance, status);
19263af08d82Slm66018 	}
19273af08d82Slm66018 
19283af08d82Slm66018 	return (status);
19293af08d82Slm66018 }
19303af08d82Slm66018 
19313af08d82Slm66018 /*
19323af08d82Slm66018  * Function:
19333af08d82Slm66018  *	vdc_handle_rdx()
19343af08d82Slm66018  *
19353af08d82Slm66018  * Description:
19363af08d82Slm66018  *
19373af08d82Slm66018  * Arguments:
19383af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
19393af08d82Slm66018  *	msgp	- received msg
19403af08d82Slm66018  *
19413af08d82Slm66018  * Return Code:
19423af08d82Slm66018  *	0	- Success
19433af08d82Slm66018  */
19443af08d82Slm66018 static int
19453af08d82Slm66018 vdc_handle_rdx(vdc_t *vdcp, vio_rdx_msg_t *msgp)
19463af08d82Slm66018 {
19473af08d82Slm66018 	_NOTE(ARGUNUSED(vdcp))
19483af08d82Slm66018 	_NOTE(ARGUNUSED(msgp))
19493af08d82Slm66018 
19503af08d82Slm66018 	ASSERT(msgp->tag.vio_msgtype == VIO_TYPE_CTRL);
19513af08d82Slm66018 	ASSERT(msgp->tag.vio_subtype == VIO_SUBTYPE_ACK);
19523af08d82Slm66018 	ASSERT(msgp->tag.vio_subtype_env == VIO_RDX);
19533af08d82Slm66018 
19543af08d82Slm66018 	DMSG(vdcp, 1, "[%d] Got an RDX msg", vdcp->instance);
19553af08d82Slm66018 
19563af08d82Slm66018 	return (0);
19573af08d82Slm66018 }
19583af08d82Slm66018 
19593af08d82Slm66018 /*
19603af08d82Slm66018  * Function:
19613af08d82Slm66018  *	vdc_rdx_exchange()
19623af08d82Slm66018  *
19633af08d82Slm66018  * Description:
19643af08d82Slm66018  *
19653af08d82Slm66018  * Arguments:
19663af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
19673af08d82Slm66018  *
19683af08d82Slm66018  * Return Code:
19693af08d82Slm66018  *	0	- Success
19703af08d82Slm66018  */
19713af08d82Slm66018 static int
19723af08d82Slm66018 vdc_rdx_exchange(vdc_t *vdcp)
19733af08d82Slm66018 {
19743af08d82Slm66018 	int status;
19753af08d82Slm66018 	vio_msg_t vio_msg;
19763af08d82Slm66018 
19773af08d82Slm66018 	if (status = vdc_send_rdx(vdcp))
19783af08d82Slm66018 		return (status);
19793af08d82Slm66018 
19803af08d82Slm66018 	/* release lock and wait for response */
19813af08d82Slm66018 	mutex_exit(&vdcp->lock);
19823af08d82Slm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
19833af08d82Slm66018 	mutex_enter(&vdcp->lock);
19843af08d82Slm66018 	if (status) {
198587a7269eSachartre 		DMSG(vdcp, 0, "[%d] Failed waiting for RDX response, rv(%d)",
198687a7269eSachartre 		    vdcp->instance, status);
19873af08d82Slm66018 		return (status);
19883af08d82Slm66018 	}
19893af08d82Slm66018 
19903af08d82Slm66018 	/* check type and sub_type ... */
19913af08d82Slm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
19923af08d82Slm66018 	    vio_msg.tag.vio_subtype != VIO_SUBTYPE_ACK) {
199387a7269eSachartre 		DMSG(vdcp, 0, "[%d] Invalid RDX response\n", vdcp->instance);
19943af08d82Slm66018 		return (EPROTO);
19953af08d82Slm66018 	}
19963af08d82Slm66018 
19973af08d82Slm66018 	return (vdc_handle_rdx(vdcp, (vio_rdx_msg_t *)&vio_msg));
19983af08d82Slm66018 }
19993af08d82Slm66018 
20003af08d82Slm66018 
20011ae08745Sheppo /* -------------------------------------------------------------------------- */
20021ae08745Sheppo 
20031ae08745Sheppo /*
20041ae08745Sheppo  * LDC helper routines
20051ae08745Sheppo  */
20061ae08745Sheppo 
20073af08d82Slm66018 static int
20083af08d82Slm66018 vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp)
20093af08d82Slm66018 {
20103af08d82Slm66018 	int		status;
20113af08d82Slm66018 	boolean_t	q_has_pkts = B_FALSE;
201217cadca8Slm66018 	uint64_t	delay_time;
20133af08d82Slm66018 	size_t		len;
20143af08d82Slm66018 
20153af08d82Slm66018 	mutex_enter(&vdc->read_lock);
20163af08d82Slm66018 
20173af08d82Slm66018 	if (vdc->read_state == VDC_READ_IDLE)
20183af08d82Slm66018 		vdc->read_state = VDC_READ_WAITING;
20193af08d82Slm66018 
20203af08d82Slm66018 	while (vdc->read_state != VDC_READ_PENDING) {
20213af08d82Slm66018 
20223af08d82Slm66018 		/* detect if the connection has been reset */
20233af08d82Slm66018 		if (vdc->read_state == VDC_READ_RESET) {
20243af08d82Slm66018 			status = ECONNRESET;
20253af08d82Slm66018 			goto done;
20263af08d82Slm66018 		}
20273af08d82Slm66018 
20283af08d82Slm66018 		cv_wait(&vdc->read_cv, &vdc->read_lock);
20293af08d82Slm66018 	}
20303af08d82Slm66018 
20313af08d82Slm66018 	/*
20323af08d82Slm66018 	 * Until we get a blocking ldc read we have to retry
20333af08d82Slm66018 	 * until the entire LDC message has arrived before
20343af08d82Slm66018 	 * ldc_read() will succeed. Note we also bail out if
2035eff7243fSlm66018 	 * the channel is reset or goes away.
20363af08d82Slm66018 	 */
20373af08d82Slm66018 	delay_time = vdc_ldc_read_init_delay;
20383af08d82Slm66018 loop:
20393af08d82Slm66018 	len = *nbytesp;
20403af08d82Slm66018 	status = ldc_read(vdc->ldc_handle, (caddr_t)msgp, &len);
20413af08d82Slm66018 	switch (status) {
20423af08d82Slm66018 	case EAGAIN:
20433af08d82Slm66018 		delay_time *= 2;
20443af08d82Slm66018 		if (delay_time >= vdc_ldc_read_max_delay)
20453af08d82Slm66018 			delay_time = vdc_ldc_read_max_delay;
20463af08d82Slm66018 		delay(delay_time);
20473af08d82Slm66018 		goto loop;
20483af08d82Slm66018 
20493af08d82Slm66018 	case 0:
20503af08d82Slm66018 		if (len == 0) {
205117cadca8Slm66018 			DMSG(vdc, 1, "[%d] ldc_read returned 0 bytes with "
20523af08d82Slm66018 			    "no error!\n", vdc->instance);
20533af08d82Slm66018 			goto loop;
20543af08d82Slm66018 		}
20553af08d82Slm66018 
20563af08d82Slm66018 		*nbytesp = len;
20573af08d82Slm66018 
20583af08d82Slm66018 		/*
20593af08d82Slm66018 		 * If there are pending messages, leave the
20603af08d82Slm66018 		 * read state as pending. Otherwise, set the state
20613af08d82Slm66018 		 * back to idle.
20623af08d82Slm66018 		 */
20633af08d82Slm66018 		status = ldc_chkq(vdc->ldc_handle, &q_has_pkts);
20643af08d82Slm66018 		if (status == 0 && !q_has_pkts)
20653af08d82Slm66018 			vdc->read_state = VDC_READ_IDLE;
20663af08d82Slm66018 
20673af08d82Slm66018 		break;
20683af08d82Slm66018 	default:
20693af08d82Slm66018 		DMSG(vdc, 0, "ldc_read returned %d\n", status);
20703af08d82Slm66018 		break;
20713af08d82Slm66018 	}
20723af08d82Slm66018 
20733af08d82Slm66018 done:
20743af08d82Slm66018 	mutex_exit(&vdc->read_lock);
20753af08d82Slm66018 
20763af08d82Slm66018 	return (status);
20773af08d82Slm66018 }
20783af08d82Slm66018 
20793af08d82Slm66018 
20803af08d82Slm66018 
20813af08d82Slm66018 #ifdef DEBUG
20823af08d82Slm66018 void
20833af08d82Slm66018 vdc_decode_tag(vdc_t *vdcp, vio_msg_t *msg)
20843af08d82Slm66018 {
20853af08d82Slm66018 	char *ms, *ss, *ses;
20863af08d82Slm66018 	switch (msg->tag.vio_msgtype) {
20873af08d82Slm66018 #define	Q(_s)	case _s : ms = #_s; break;
20883af08d82Slm66018 	Q(VIO_TYPE_CTRL)
20893af08d82Slm66018 	Q(VIO_TYPE_DATA)
20903af08d82Slm66018 	Q(VIO_TYPE_ERR)
20913af08d82Slm66018 #undef Q
20923af08d82Slm66018 	default: ms = "unknown"; break;
20933af08d82Slm66018 	}
20943af08d82Slm66018 
20953af08d82Slm66018 	switch (msg->tag.vio_subtype) {
20963af08d82Slm66018 #define	Q(_s)	case _s : ss = #_s; break;
20973af08d82Slm66018 	Q(VIO_SUBTYPE_INFO)
20983af08d82Slm66018 	Q(VIO_SUBTYPE_ACK)
20993af08d82Slm66018 	Q(VIO_SUBTYPE_NACK)
21003af08d82Slm66018 #undef Q
21013af08d82Slm66018 	default: ss = "unknown"; break;
21023af08d82Slm66018 	}
21033af08d82Slm66018 
21043af08d82Slm66018 	switch (msg->tag.vio_subtype_env) {
21053af08d82Slm66018 #define	Q(_s)	case _s : ses = #_s; break;
21063af08d82Slm66018 	Q(VIO_VER_INFO)
21073af08d82Slm66018 	Q(VIO_ATTR_INFO)
21083af08d82Slm66018 	Q(VIO_DRING_REG)
21093af08d82Slm66018 	Q(VIO_DRING_UNREG)
21103af08d82Slm66018 	Q(VIO_RDX)
21113af08d82Slm66018 	Q(VIO_PKT_DATA)
21123af08d82Slm66018 	Q(VIO_DESC_DATA)
21133af08d82Slm66018 	Q(VIO_DRING_DATA)
21143af08d82Slm66018 #undef Q
21153af08d82Slm66018 	default: ses = "unknown"; break;
21163af08d82Slm66018 	}
21173af08d82Slm66018 
21183af08d82Slm66018 	DMSG(vdcp, 3, "(%x/%x/%x) message : (%s/%s/%s)\n",
21193af08d82Slm66018 	    msg->tag.vio_msgtype, msg->tag.vio_subtype,
21203af08d82Slm66018 	    msg->tag.vio_subtype_env, ms, ss, ses);
21213af08d82Slm66018 }
21223af08d82Slm66018 #endif
21233af08d82Slm66018 
21241ae08745Sheppo /*
21251ae08745Sheppo  * Function:
21261ae08745Sheppo  *	vdc_send()
21271ae08745Sheppo  *
21281ae08745Sheppo  * Description:
21291ae08745Sheppo  *	The function encapsulates the call to write a message using LDC.
21301ae08745Sheppo  *	If LDC indicates that the call failed due to the queue being full,
213117cadca8Slm66018  *	we retry the ldc_write(), otherwise we return the error returned by LDC.
21321ae08745Sheppo  *
21331ae08745Sheppo  * Arguments:
21341ae08745Sheppo  *	ldc_handle	- LDC handle for the channel this instance of vdc uses
21351ae08745Sheppo  *	pkt		- address of LDC message to be sent
21361ae08745Sheppo  *	msglen		- the size of the message being sent. When the function
21371ae08745Sheppo  *			  returns, this contains the number of bytes written.
21381ae08745Sheppo  *
21391ae08745Sheppo  * Return Code:
21401ae08745Sheppo  *	0		- Success.
21411ae08745Sheppo  *	EINVAL		- pkt or msglen were NULL
21421ae08745Sheppo  *	ECONNRESET	- The connection was not up.
21431ae08745Sheppo  *	EWOULDBLOCK	- LDC queue is full
21441ae08745Sheppo  *	xxx		- other error codes returned by ldc_write
21451ae08745Sheppo  */
21461ae08745Sheppo static int
21470a55fbb7Slm66018 vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen)
21481ae08745Sheppo {
21491ae08745Sheppo 	size_t	size = 0;
21501ae08745Sheppo 	int	status = 0;
21513af08d82Slm66018 	clock_t delay_ticks;
21521ae08745Sheppo 
21530a55fbb7Slm66018 	ASSERT(vdc != NULL);
21540a55fbb7Slm66018 	ASSERT(mutex_owned(&vdc->lock));
21551ae08745Sheppo 	ASSERT(msglen != NULL);
21561ae08745Sheppo 	ASSERT(*msglen != 0);
21571ae08745Sheppo 
21583af08d82Slm66018 #ifdef DEBUG
215917cadca8Slm66018 	vdc_decode_tag(vdc, (vio_msg_t *)(uintptr_t)pkt);
21603af08d82Slm66018 #endif
21613af08d82Slm66018 	/*
21623af08d82Slm66018 	 * Wait indefinitely to send if channel
21633af08d82Slm66018 	 * is busy, but bail out if we succeed or
21643af08d82Slm66018 	 * if the channel closes or is reset.
21653af08d82Slm66018 	 */
21663af08d82Slm66018 	delay_ticks = vdc_hz_min_ldc_delay;
21671ae08745Sheppo 	do {
21681ae08745Sheppo 		size = *msglen;
21690a55fbb7Slm66018 		status = ldc_write(vdc->ldc_handle, pkt, &size);
21703af08d82Slm66018 		if (status == EWOULDBLOCK) {
21713af08d82Slm66018 			delay(delay_ticks);
21723af08d82Slm66018 			/* geometric backoff */
21733af08d82Slm66018 			delay_ticks *= 2;
21743af08d82Slm66018 			if (delay_ticks > vdc_hz_max_ldc_delay)
21753af08d82Slm66018 				delay_ticks = vdc_hz_max_ldc_delay;
21763af08d82Slm66018 		}
21773af08d82Slm66018 	} while (status == EWOULDBLOCK);
21781ae08745Sheppo 
21790a55fbb7Slm66018 	/* if LDC had serious issues --- reset vdc state */
21800a55fbb7Slm66018 	if (status == EIO || status == ECONNRESET) {
21813af08d82Slm66018 		/* LDC had serious issues --- reset vdc state */
21823af08d82Slm66018 		mutex_enter(&vdc->read_lock);
21833af08d82Slm66018 		if ((vdc->read_state == VDC_READ_WAITING) ||
21843af08d82Slm66018 		    (vdc->read_state == VDC_READ_RESET))
21853af08d82Slm66018 			cv_signal(&vdc->read_cv);
21863af08d82Slm66018 		vdc->read_state = VDC_READ_RESET;
21873af08d82Slm66018 		mutex_exit(&vdc->read_lock);
21883af08d82Slm66018 
21893af08d82Slm66018 		/* wake up any waiters in the reset thread */
21903af08d82Slm66018 		if (vdc->state == VDC_STATE_INIT_WAITING) {
21913af08d82Slm66018 			DMSG(vdc, 0, "[%d] write reset - "
21923af08d82Slm66018 			    "vdc is resetting ..\n", vdc->instance);
21933af08d82Slm66018 			vdc->state = VDC_STATE_RESETTING;
21943af08d82Slm66018 			cv_signal(&vdc->initwait_cv);
21953af08d82Slm66018 		}
21963af08d82Slm66018 
21973af08d82Slm66018 		return (ECONNRESET);
21980a55fbb7Slm66018 	}
21990a55fbb7Slm66018 
22001ae08745Sheppo 	/* return the last size written */
22011ae08745Sheppo 	*msglen = size;
22021ae08745Sheppo 
22031ae08745Sheppo 	return (status);
22041ae08745Sheppo }
22051ae08745Sheppo 
22061ae08745Sheppo /*
22071ae08745Sheppo  * Function:
2208655fd6a9Sachartre  *	vdc_get_md_node
22091ae08745Sheppo  *
22101ae08745Sheppo  * Description:
2211655fd6a9Sachartre  *	Get the MD, the device node and the port node for the given
2212655fd6a9Sachartre  *	disk instance. The caller is responsible for cleaning up the
2213655fd6a9Sachartre  *	reference to the returned MD (mdpp) by calling md_fini_handle().
22141ae08745Sheppo  *
22151ae08745Sheppo  * Arguments:
22161ae08745Sheppo  *	dip	- dev info pointer for this instance of the device driver.
2217655fd6a9Sachartre  *	mdpp	- the returned MD.
2218655fd6a9Sachartre  *	vd_nodep - the returned device node.
2219655fd6a9Sachartre  *	vd_portp - the returned port node. The returned port node is NULL
2220655fd6a9Sachartre  *		   if no port node is found.
22211ae08745Sheppo  *
22221ae08745Sheppo  * Return Code:
22231ae08745Sheppo  *	0	- Success.
22241ae08745Sheppo  *	ENOENT	- Expected node or property did not exist.
22251ae08745Sheppo  *	ENXIO	- Unexpected error communicating with MD framework
22261ae08745Sheppo  */
22271ae08745Sheppo static int
2228655fd6a9Sachartre vdc_get_md_node(dev_info_t *dip, md_t **mdpp, mde_cookie_t *vd_nodep,
2229655fd6a9Sachartre     mde_cookie_t *vd_portp)
22301ae08745Sheppo {
22311ae08745Sheppo 	int		status = ENOENT;
22321ae08745Sheppo 	char		*node_name = NULL;
22331ae08745Sheppo 	md_t		*mdp = NULL;
22341ae08745Sheppo 	int		num_nodes;
22351ae08745Sheppo 	int		num_vdevs;
2236655fd6a9Sachartre 	int		num_vports;
22371ae08745Sheppo 	mde_cookie_t	rootnode;
22381ae08745Sheppo 	mde_cookie_t	*listp = NULL;
22391ae08745Sheppo 	boolean_t	found_inst = B_FALSE;
22401ae08745Sheppo 	int		listsz;
22411ae08745Sheppo 	int		idx;
22421ae08745Sheppo 	uint64_t	md_inst;
22431ae08745Sheppo 	int		obp_inst;
22441ae08745Sheppo 	int		instance = ddi_get_instance(dip);
22451ae08745Sheppo 
22461ae08745Sheppo 	/*
22471ae08745Sheppo 	 * Get the OBP instance number for comparison with the MD instance
22481ae08745Sheppo 	 *
22491ae08745Sheppo 	 * The "cfg-handle" property of a vdc node in an MD contains the MD's
22501ae08745Sheppo 	 * notion of "instance", or unique identifier, for that node; OBP
22511ae08745Sheppo 	 * stores the value of the "cfg-handle" MD property as the value of
22521ae08745Sheppo 	 * the "reg" property on the node in the device tree it builds from
22531ae08745Sheppo 	 * the MD and passes to Solaris.  Thus, we look up the devinfo node's
22541ae08745Sheppo 	 * "reg" property value to uniquely identify this device instance.
22551ae08745Sheppo 	 * If the "reg" property cannot be found, the device tree state is
22561ae08745Sheppo 	 * presumably so broken that there is no point in continuing.
22571ae08745Sheppo 	 */
22581ae08745Sheppo 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, OBP_REG)) {
22591ae08745Sheppo 		cmn_err(CE_WARN, "'%s' property does not exist", OBP_REG);
22601ae08745Sheppo 		return (ENOENT);
22611ae08745Sheppo 	}
22621ae08745Sheppo 	obp_inst = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
22631ae08745Sheppo 	    OBP_REG, -1);
22643af08d82Slm66018 	DMSGX(1, "[%d] OBP inst=%d\n", instance, obp_inst);
22651ae08745Sheppo 
22661ae08745Sheppo 	/*
2267655fd6a9Sachartre 	 * We now walk the MD nodes to find the node for this vdisk.
22681ae08745Sheppo 	 */
22691ae08745Sheppo 	if ((mdp = md_get_handle()) == NULL) {
22701ae08745Sheppo 		cmn_err(CE_WARN, "unable to init machine description");
22711ae08745Sheppo 		return (ENXIO);
22721ae08745Sheppo 	}
22731ae08745Sheppo 
22741ae08745Sheppo 	num_nodes = md_node_count(mdp);
22751ae08745Sheppo 	ASSERT(num_nodes > 0);
22761ae08745Sheppo 
22771ae08745Sheppo 	listsz = num_nodes * sizeof (mde_cookie_t);
22781ae08745Sheppo 
22791ae08745Sheppo 	/* allocate memory for nodes */
22801ae08745Sheppo 	listp = kmem_zalloc(listsz, KM_SLEEP);
22811ae08745Sheppo 
22821ae08745Sheppo 	rootnode = md_root_node(mdp);
22831ae08745Sheppo 	ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE);
22841ae08745Sheppo 
22851ae08745Sheppo 	/*
22861ae08745Sheppo 	 * Search for all the virtual devices, we will then check to see which
22871ae08745Sheppo 	 * ones are disk nodes.
22881ae08745Sheppo 	 */
22891ae08745Sheppo 	num_vdevs = md_scan_dag(mdp, rootnode,
22901ae08745Sheppo 	    md_find_name(mdp, VDC_MD_VDEV_NAME),
22911ae08745Sheppo 	    md_find_name(mdp, "fwd"), listp);
22921ae08745Sheppo 
22931ae08745Sheppo 	if (num_vdevs <= 0) {
22941ae08745Sheppo 		cmn_err(CE_NOTE, "No '%s' node found", VDC_MD_VDEV_NAME);
22951ae08745Sheppo 		status = ENOENT;
22961ae08745Sheppo 		goto done;
22971ae08745Sheppo 	}
22981ae08745Sheppo 
22993af08d82Slm66018 	DMSGX(1, "[%d] num_vdevs=%d\n", instance, num_vdevs);
23001ae08745Sheppo 	for (idx = 0; idx < num_vdevs; idx++) {
23011ae08745Sheppo 		status = md_get_prop_str(mdp, listp[idx], "name", &node_name);
23021ae08745Sheppo 		if ((status != 0) || (node_name == NULL)) {
23031ae08745Sheppo 			cmn_err(CE_NOTE, "Unable to get name of node type '%s'"
23041ae08745Sheppo 			    ": err %d", VDC_MD_VDEV_NAME, status);
23051ae08745Sheppo 			continue;
23061ae08745Sheppo 		}
23071ae08745Sheppo 
23083af08d82Slm66018 		DMSGX(1, "[%d] Found node '%s'\n", instance, node_name);
23091ae08745Sheppo 		if (strcmp(VDC_MD_DISK_NAME, node_name) == 0) {
23101ae08745Sheppo 			status = md_get_prop_val(mdp, listp[idx],
23111ae08745Sheppo 			    VDC_MD_CFG_HDL, &md_inst);
23123af08d82Slm66018 			DMSGX(1, "[%d] vdc inst in MD=%lx\n",
23133af08d82Slm66018 			    instance, md_inst);
23141ae08745Sheppo 			if ((status == 0) && (md_inst == obp_inst)) {
23151ae08745Sheppo 				found_inst = B_TRUE;
23161ae08745Sheppo 				break;
23171ae08745Sheppo 			}
23181ae08745Sheppo 		}
23191ae08745Sheppo 	}
23201ae08745Sheppo 
23210a55fbb7Slm66018 	if (!found_inst) {
23223af08d82Slm66018 		DMSGX(0, "Unable to find correct '%s' node", VDC_MD_DISK_NAME);
23231ae08745Sheppo 		status = ENOENT;
23241ae08745Sheppo 		goto done;
23251ae08745Sheppo 	}
23263af08d82Slm66018 	DMSGX(0, "[%d] MD inst=%lx\n", instance, md_inst);
23271ae08745Sheppo 
2328655fd6a9Sachartre 	*vd_nodep = listp[idx];
2329655fd6a9Sachartre 	*mdpp = mdp;
2330655fd6a9Sachartre 
2331655fd6a9Sachartre 	num_vports = md_scan_dag(mdp, *vd_nodep,
2332655fd6a9Sachartre 	    md_find_name(mdp, VDC_MD_PORT_NAME),
2333655fd6a9Sachartre 	    md_find_name(mdp, "fwd"), listp);
2334655fd6a9Sachartre 
2335655fd6a9Sachartre 	if (num_vports != 1) {
2336655fd6a9Sachartre 		DMSGX(0, "Expected 1 '%s' node for '%s' port, found %d\n",
2337655fd6a9Sachartre 		    VDC_MD_PORT_NAME, VDC_MD_VDEV_NAME, num_vports);
2338655fd6a9Sachartre 	}
2339655fd6a9Sachartre 
2340655fd6a9Sachartre 	*vd_portp = (num_vports == 0)? NULL: listp[0];
2341655fd6a9Sachartre 
2342655fd6a9Sachartre done:
2343655fd6a9Sachartre 	kmem_free(listp, listsz);
2344655fd6a9Sachartre 	return (status);
2345655fd6a9Sachartre }
2346655fd6a9Sachartre 
2347655fd6a9Sachartre /*
2348655fd6a9Sachartre  * Function:
2349655fd6a9Sachartre  *	vdc_get_ldc_id()
2350655fd6a9Sachartre  *
2351655fd6a9Sachartre  * Description:
2352655fd6a9Sachartre  *	This function gets the 'ldc-id' for this particular instance of vdc.
2353655fd6a9Sachartre  *	The id returned is the guest domain channel endpoint LDC uses for
2354655fd6a9Sachartre  *	communication with vds.
2355655fd6a9Sachartre  *
2356655fd6a9Sachartre  * Arguments:
2357655fd6a9Sachartre  *	mdp	- pointer to the machine description.
2358655fd6a9Sachartre  *	vd_node	- the vdisk element from the MD.
2359655fd6a9Sachartre  *	ldc_id	- pointer to variable used to return the 'ldc-id' found.
2360655fd6a9Sachartre  *
2361655fd6a9Sachartre  * Return Code:
2362655fd6a9Sachartre  *	0	- Success.
2363655fd6a9Sachartre  *	ENOENT	- Expected node or property did not exist.
2364655fd6a9Sachartre  */
2365655fd6a9Sachartre static int
2366655fd6a9Sachartre vdc_get_ldc_id(md_t *mdp, mde_cookie_t vd_node, uint64_t *ldc_id)
2367655fd6a9Sachartre {
2368655fd6a9Sachartre 	mde_cookie_t	*chanp = NULL;
2369655fd6a9Sachartre 	int		listsz;
2370655fd6a9Sachartre 	int		num_chans;
2371655fd6a9Sachartre 	int		num_nodes;
2372655fd6a9Sachartre 	int		status = 0;
2373655fd6a9Sachartre 
2374655fd6a9Sachartre 	num_nodes = md_node_count(mdp);
2375655fd6a9Sachartre 	ASSERT(num_nodes > 0);
2376655fd6a9Sachartre 
2377655fd6a9Sachartre 	listsz = num_nodes * sizeof (mde_cookie_t);
2378655fd6a9Sachartre 
2379655fd6a9Sachartre 	/* allocate memory for nodes */
2380655fd6a9Sachartre 	chanp = kmem_zalloc(listsz, KM_SLEEP);
2381655fd6a9Sachartre 
23821ae08745Sheppo 	/* get the channels for this node */
2383655fd6a9Sachartre 	num_chans = md_scan_dag(mdp, vd_node,
23841ae08745Sheppo 	    md_find_name(mdp, VDC_MD_CHAN_NAME),
23851ae08745Sheppo 	    md_find_name(mdp, "fwd"), chanp);
23861ae08745Sheppo 
23871ae08745Sheppo 	/* expecting at least one channel */
23881ae08745Sheppo 	if (num_chans <= 0) {
23891ae08745Sheppo 		cmn_err(CE_NOTE, "No '%s' node for '%s' port",
23901ae08745Sheppo 		    VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME);
23911ae08745Sheppo 		status = ENOENT;
23921ae08745Sheppo 		goto done;
23931ae08745Sheppo 
23941ae08745Sheppo 	} else if (num_chans != 1) {
2395655fd6a9Sachartre 		DMSGX(0, "Expected 1 '%s' node for '%s' port, found %d\n",
2396655fd6a9Sachartre 		    VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME, num_chans);
23971ae08745Sheppo 	}
23981ae08745Sheppo 
23991ae08745Sheppo 	/*
24001ae08745Sheppo 	 * We use the first channel found (index 0), irrespective of how
24011ae08745Sheppo 	 * many are there in total.
24021ae08745Sheppo 	 */
2403655fd6a9Sachartre 	if (md_get_prop_val(mdp, chanp[0], VDC_MD_ID, ldc_id) != 0) {
2404655fd6a9Sachartre 		cmn_err(CE_NOTE, "Channel '%s' property not found", VDC_MD_ID);
24051ae08745Sheppo 		status = ENOENT;
24061ae08745Sheppo 	}
24071ae08745Sheppo 
24081ae08745Sheppo done:
24091ae08745Sheppo 	kmem_free(chanp, listsz);
24101ae08745Sheppo 	return (status);
24111ae08745Sheppo }
24121ae08745Sheppo 
24130a55fbb7Slm66018 static int
24140a55fbb7Slm66018 vdc_do_ldc_up(vdc_t *vdc)
24150a55fbb7Slm66018 {
24160a55fbb7Slm66018 	int		status;
24173af08d82Slm66018 	ldc_status_t	ldc_state;
24180a55fbb7Slm66018 
24193af08d82Slm66018 	DMSG(vdc, 0, "[%d] Bringing up channel %lx\n",
24203af08d82Slm66018 	    vdc->instance, vdc->ldc_id);
24213af08d82Slm66018 
24223af08d82Slm66018 	if (vdc->lifecycle == VDC_LC_DETACHING)
24233af08d82Slm66018 		return (EINVAL);
24240a55fbb7Slm66018 
24250a55fbb7Slm66018 	if ((status = ldc_up(vdc->ldc_handle)) != 0) {
24260a55fbb7Slm66018 		switch (status) {
24270a55fbb7Slm66018 		case ECONNREFUSED:	/* listener not ready at other end */
24283af08d82Slm66018 			DMSG(vdc, 0, "[%d] ldc_up(%lx,...) return %d\n",
2429e1ebb9ecSlm66018 			    vdc->instance, vdc->ldc_id, status);
24300a55fbb7Slm66018 			status = 0;
24310a55fbb7Slm66018 			break;
24320a55fbb7Slm66018 		default:
24333af08d82Slm66018 			DMSG(vdc, 0, "[%d] Failed to bring up LDC: "
24343af08d82Slm66018 			    "channel=%ld, err=%d", vdc->instance, vdc->ldc_id,
24353af08d82Slm66018 			    status);
24363af08d82Slm66018 			break;
24373af08d82Slm66018 		}
24383af08d82Slm66018 	}
24393af08d82Slm66018 
24403af08d82Slm66018 	if (ldc_status(vdc->ldc_handle, &ldc_state) == 0) {
24413af08d82Slm66018 		vdc->ldc_state = ldc_state;
24423af08d82Slm66018 		if (ldc_state == LDC_UP) {
24433af08d82Slm66018 			DMSG(vdc, 0, "[%d] LDC channel already up\n",
24443af08d82Slm66018 			    vdc->instance);
24453af08d82Slm66018 			vdc->seq_num = 1;
24463af08d82Slm66018 			vdc->seq_num_reply = 0;
24470a55fbb7Slm66018 		}
24480a55fbb7Slm66018 	}
24490a55fbb7Slm66018 
24500a55fbb7Slm66018 	return (status);
24510a55fbb7Slm66018 }
24520a55fbb7Slm66018 
24530a55fbb7Slm66018 /*
24540a55fbb7Slm66018  * Function:
24550a55fbb7Slm66018  *	vdc_terminate_ldc()
24560a55fbb7Slm66018  *
24570a55fbb7Slm66018  * Description:
24580a55fbb7Slm66018  *
24590a55fbb7Slm66018  * Arguments:
24600a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
24610a55fbb7Slm66018  *
24620a55fbb7Slm66018  * Return Code:
24630a55fbb7Slm66018  *	None
24640a55fbb7Slm66018  */
24651ae08745Sheppo static void
24661ae08745Sheppo vdc_terminate_ldc(vdc_t *vdc)
24671ae08745Sheppo {
24681ae08745Sheppo 	int	instance = ddi_get_instance(vdc->dip);
24691ae08745Sheppo 
24701ae08745Sheppo 	ASSERT(vdc != NULL);
24711ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
24721ae08745Sheppo 
24733af08d82Slm66018 	DMSG(vdc, 0, "[%d] initialized=%x\n", instance, vdc->initialized);
24741ae08745Sheppo 
24751ae08745Sheppo 	if (vdc->initialized & VDC_LDC_OPEN) {
24763af08d82Slm66018 		DMSG(vdc, 0, "[%d] ldc_close()\n", instance);
24771ae08745Sheppo 		(void) ldc_close(vdc->ldc_handle);
24781ae08745Sheppo 	}
24791ae08745Sheppo 	if (vdc->initialized & VDC_LDC_CB) {
24803af08d82Slm66018 		DMSG(vdc, 0, "[%d] ldc_unreg_callback()\n", instance);
24811ae08745Sheppo 		(void) ldc_unreg_callback(vdc->ldc_handle);
24821ae08745Sheppo 	}
24831ae08745Sheppo 	if (vdc->initialized & VDC_LDC) {
24843af08d82Slm66018 		DMSG(vdc, 0, "[%d] ldc_fini()\n", instance);
24851ae08745Sheppo 		(void) ldc_fini(vdc->ldc_handle);
24861ae08745Sheppo 		vdc->ldc_handle = NULL;
24871ae08745Sheppo 	}
24881ae08745Sheppo 
24891ae08745Sheppo 	vdc->initialized &= ~(VDC_LDC | VDC_LDC_CB | VDC_LDC_OPEN);
24901ae08745Sheppo }
24911ae08745Sheppo 
24921ae08745Sheppo /* -------------------------------------------------------------------------- */
24931ae08745Sheppo 
24941ae08745Sheppo /*
24951ae08745Sheppo  * Descriptor Ring helper routines
24961ae08745Sheppo  */
24971ae08745Sheppo 
24980a55fbb7Slm66018 /*
24990a55fbb7Slm66018  * Function:
25000a55fbb7Slm66018  *	vdc_init_descriptor_ring()
25010a55fbb7Slm66018  *
25020a55fbb7Slm66018  * Description:
25030a55fbb7Slm66018  *
25040a55fbb7Slm66018  * Arguments:
25050a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
25060a55fbb7Slm66018  *
25070a55fbb7Slm66018  * Return Code:
25080a55fbb7Slm66018  *	0	- Success
25090a55fbb7Slm66018  */
25101ae08745Sheppo static int
25111ae08745Sheppo vdc_init_descriptor_ring(vdc_t *vdc)
25121ae08745Sheppo {
25131ae08745Sheppo 	vd_dring_entry_t	*dep = NULL;	/* DRing Entry pointer */
25140a55fbb7Slm66018 	int	status = 0;
25151ae08745Sheppo 	int	i;
25161ae08745Sheppo 
25173af08d82Slm66018 	DMSG(vdc, 0, "[%d] initialized=%x\n", vdc->instance, vdc->initialized);
25181ae08745Sheppo 
25191ae08745Sheppo 	ASSERT(vdc != NULL);
25201ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
25211ae08745Sheppo 	ASSERT(vdc->ldc_handle != NULL);
25221ae08745Sheppo 
2523e1ebb9ecSlm66018 	/* ensure we have enough room to store max sized block */
2524e1ebb9ecSlm66018 	ASSERT(maxphys <= VD_MAX_BLOCK_SIZE);
2525e1ebb9ecSlm66018 
25260a55fbb7Slm66018 	if ((vdc->initialized & VDC_DRING_INIT) == 0) {
25273af08d82Slm66018 		DMSG(vdc, 0, "[%d] ldc_mem_dring_create\n", vdc->instance);
2528e1ebb9ecSlm66018 		/*
2529e1ebb9ecSlm66018 		 * Calculate the maximum block size we can transmit using one
2530e1ebb9ecSlm66018 		 * Descriptor Ring entry from the attributes returned by the
2531e1ebb9ecSlm66018 		 * vDisk server. This is subject to a minimum of 'maxphys'
2532e1ebb9ecSlm66018 		 * as we do not have the capability to split requests over
2533e1ebb9ecSlm66018 		 * multiple DRing entries.
2534e1ebb9ecSlm66018 		 */
2535e1ebb9ecSlm66018 		if ((vdc->max_xfer_sz * vdc->block_size) < maxphys) {
25363af08d82Slm66018 			DMSG(vdc, 0, "[%d] using minimum DRing size\n",
2537e1ebb9ecSlm66018 			    vdc->instance);
2538e1ebb9ecSlm66018 			vdc->dring_max_cookies = maxphys / PAGESIZE;
2539e1ebb9ecSlm66018 		} else {
2540e1ebb9ecSlm66018 			vdc->dring_max_cookies =
2541e1ebb9ecSlm66018 			    (vdc->max_xfer_sz * vdc->block_size) / PAGESIZE;
2542e1ebb9ecSlm66018 		}
2543e1ebb9ecSlm66018 		vdc->dring_entry_size = (sizeof (vd_dring_entry_t) +
2544e1ebb9ecSlm66018 		    (sizeof (ldc_mem_cookie_t) *
2545e1ebb9ecSlm66018 		    (vdc->dring_max_cookies - 1)));
2546e1ebb9ecSlm66018 		vdc->dring_len = VD_DRING_LEN;
2547e1ebb9ecSlm66018 
2548e1ebb9ecSlm66018 		status = ldc_mem_dring_create(vdc->dring_len,
2549e1ebb9ecSlm66018 		    vdc->dring_entry_size, &vdc->ldc_dring_hdl);
25501ae08745Sheppo 		if ((vdc->ldc_dring_hdl == NULL) || (status != 0)) {
25513af08d82Slm66018 			DMSG(vdc, 0, "[%d] Descriptor ring creation failed",
2552e1ebb9ecSlm66018 			    vdc->instance);
25531ae08745Sheppo 			return (status);
25541ae08745Sheppo 		}
25550a55fbb7Slm66018 		vdc->initialized |= VDC_DRING_INIT;
25560a55fbb7Slm66018 	}
25571ae08745Sheppo 
25580a55fbb7Slm66018 	if ((vdc->initialized & VDC_DRING_BOUND) == 0) {
25593af08d82Slm66018 		DMSG(vdc, 0, "[%d] ldc_mem_dring_bind\n", vdc->instance);
25600a55fbb7Slm66018 		vdc->dring_cookie =
25610a55fbb7Slm66018 		    kmem_zalloc(sizeof (ldc_mem_cookie_t), KM_SLEEP);
25621ae08745Sheppo 
25631ae08745Sheppo 		status = ldc_mem_dring_bind(vdc->ldc_handle, vdc->ldc_dring_hdl,
25644bac2208Snarayan 		    LDC_SHADOW_MAP|LDC_DIRECT_MAP, LDC_MEM_RW,
25650a55fbb7Slm66018 		    &vdc->dring_cookie[0],
25661ae08745Sheppo 		    &vdc->dring_cookie_count);
25671ae08745Sheppo 		if (status != 0) {
25683af08d82Slm66018 			DMSG(vdc, 0, "[%d] Failed to bind descriptor ring "
25693af08d82Slm66018 			    "(%lx) to channel (%lx) status=%d\n",
25703af08d82Slm66018 			    vdc->instance, vdc->ldc_dring_hdl,
25713af08d82Slm66018 			    vdc->ldc_handle, status);
25721ae08745Sheppo 			return (status);
25731ae08745Sheppo 		}
25741ae08745Sheppo 		ASSERT(vdc->dring_cookie_count == 1);
25751ae08745Sheppo 		vdc->initialized |= VDC_DRING_BOUND;
25760a55fbb7Slm66018 	}
25771ae08745Sheppo 
25781ae08745Sheppo 	status = ldc_mem_dring_info(vdc->ldc_dring_hdl, &vdc->dring_mem_info);
25791ae08745Sheppo 	if (status != 0) {
25803af08d82Slm66018 		DMSG(vdc, 0,
25813af08d82Slm66018 		    "[%d] Failed to get info for descriptor ring (%lx)\n",
2582e1ebb9ecSlm66018 		    vdc->instance, vdc->ldc_dring_hdl);
25831ae08745Sheppo 		return (status);
25841ae08745Sheppo 	}
25851ae08745Sheppo 
25860a55fbb7Slm66018 	if ((vdc->initialized & VDC_DRING_LOCAL) == 0) {
25873af08d82Slm66018 		DMSG(vdc, 0, "[%d] local dring\n", vdc->instance);
25880a55fbb7Slm66018 
25891ae08745Sheppo 		/* Allocate the local copy of this dring */
25900a55fbb7Slm66018 		vdc->local_dring =
2591e1ebb9ecSlm66018 		    kmem_zalloc(vdc->dring_len * sizeof (vdc_local_desc_t),
25921ae08745Sheppo 		    KM_SLEEP);
25931ae08745Sheppo 		vdc->initialized |= VDC_DRING_LOCAL;
25940a55fbb7Slm66018 	}
25951ae08745Sheppo 
25961ae08745Sheppo 	/*
25970a55fbb7Slm66018 	 * Mark all DRing entries as free and initialize the private
25980a55fbb7Slm66018 	 * descriptor's memory handles. If any entry is initialized,
25990a55fbb7Slm66018 	 * we need to free it later so we set the bit in 'initialized'
26000a55fbb7Slm66018 	 * at the start.
26011ae08745Sheppo 	 */
26021ae08745Sheppo 	vdc->initialized |= VDC_DRING_ENTRY;
2603e1ebb9ecSlm66018 	for (i = 0; i < vdc->dring_len; i++) {
26041ae08745Sheppo 		dep = VDC_GET_DRING_ENTRY_PTR(vdc, i);
26051ae08745Sheppo 		dep->hdr.dstate = VIO_DESC_FREE;
26061ae08745Sheppo 
26071ae08745Sheppo 		status = ldc_mem_alloc_handle(vdc->ldc_handle,
26081ae08745Sheppo 		    &vdc->local_dring[i].desc_mhdl);
26091ae08745Sheppo 		if (status != 0) {
26103af08d82Slm66018 			DMSG(vdc, 0, "![%d] Failed to alloc mem handle for"
26111ae08745Sheppo 			    " descriptor %d", vdc->instance, i);
26121ae08745Sheppo 			return (status);
26131ae08745Sheppo 		}
26143af08d82Slm66018 		vdc->local_dring[i].is_free = B_TRUE;
26151ae08745Sheppo 		vdc->local_dring[i].dep = dep;
26161ae08745Sheppo 	}
26171ae08745Sheppo 
26183af08d82Slm66018 	/* Initialize the starting index */
26193af08d82Slm66018 	vdc->dring_curr_idx = 0;
26201ae08745Sheppo 
26211ae08745Sheppo 	return (status);
26221ae08745Sheppo }
26231ae08745Sheppo 
26240a55fbb7Slm66018 /*
26250a55fbb7Slm66018  * Function:
26260a55fbb7Slm66018  *	vdc_destroy_descriptor_ring()
26270a55fbb7Slm66018  *
26280a55fbb7Slm66018  * Description:
26290a55fbb7Slm66018  *
26300a55fbb7Slm66018  * Arguments:
26310a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
26320a55fbb7Slm66018  *
26330a55fbb7Slm66018  * Return Code:
26340a55fbb7Slm66018  *	None
26350a55fbb7Slm66018  */
26361ae08745Sheppo static void
26371ae08745Sheppo vdc_destroy_descriptor_ring(vdc_t *vdc)
26381ae08745Sheppo {
26390a55fbb7Slm66018 	vdc_local_desc_t	*ldep = NULL;	/* Local Dring Entry Pointer */
26401ae08745Sheppo 	ldc_mem_handle_t	mhdl = NULL;
26413af08d82Slm66018 	ldc_mem_info_t		minfo;
26421ae08745Sheppo 	int			status = -1;
26431ae08745Sheppo 	int			i;	/* loop */
26441ae08745Sheppo 
26451ae08745Sheppo 	ASSERT(vdc != NULL);
26461ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
26471ae08745Sheppo 
26483af08d82Slm66018 	DMSG(vdc, 0, "[%d] Entered\n", vdc->instance);
26491ae08745Sheppo 
26501ae08745Sheppo 	if (vdc->initialized & VDC_DRING_ENTRY) {
26513af08d82Slm66018 		DMSG(vdc, 0,
26523af08d82Slm66018 		    "[%d] Removing Local DRing entries\n", vdc->instance);
2653e1ebb9ecSlm66018 		for (i = 0; i < vdc->dring_len; i++) {
26540a55fbb7Slm66018 			ldep = &vdc->local_dring[i];
26550a55fbb7Slm66018 			mhdl = ldep->desc_mhdl;
26561ae08745Sheppo 
26570a55fbb7Slm66018 			if (mhdl == NULL)
26580a55fbb7Slm66018 				continue;
26590a55fbb7Slm66018 
26603af08d82Slm66018 			if ((status = ldc_mem_info(mhdl, &minfo)) != 0) {
26613af08d82Slm66018 				DMSG(vdc, 0,
26623af08d82Slm66018 				    "ldc_mem_info returned an error: %d\n",
26633af08d82Slm66018 				    status);
26643af08d82Slm66018 
26653af08d82Slm66018 				/*
26663af08d82Slm66018 				 * This must mean that the mem handle
26673af08d82Slm66018 				 * is not valid. Clear it out so that
26683af08d82Slm66018 				 * no one tries to use it.
26693af08d82Slm66018 				 */
26703af08d82Slm66018 				ldep->desc_mhdl = NULL;
26713af08d82Slm66018 				continue;
26723af08d82Slm66018 			}
26733af08d82Slm66018 
26743af08d82Slm66018 			if (minfo.status == LDC_BOUND) {
26753af08d82Slm66018 				(void) ldc_mem_unbind_handle(mhdl);
26763af08d82Slm66018 			}
26773af08d82Slm66018 
26781ae08745Sheppo 			(void) ldc_mem_free_handle(mhdl);
26793af08d82Slm66018 
26803af08d82Slm66018 			ldep->desc_mhdl = NULL;
26811ae08745Sheppo 		}
26821ae08745Sheppo 		vdc->initialized &= ~VDC_DRING_ENTRY;
26831ae08745Sheppo 	}
26841ae08745Sheppo 
26851ae08745Sheppo 	if (vdc->initialized & VDC_DRING_LOCAL) {
26863af08d82Slm66018 		DMSG(vdc, 0, "[%d] Freeing Local DRing\n", vdc->instance);
26871ae08745Sheppo 		kmem_free(vdc->local_dring,
2688e1ebb9ecSlm66018 		    vdc->dring_len * sizeof (vdc_local_desc_t));
26891ae08745Sheppo 		vdc->initialized &= ~VDC_DRING_LOCAL;
26901ae08745Sheppo 	}
26911ae08745Sheppo 
26921ae08745Sheppo 	if (vdc->initialized & VDC_DRING_BOUND) {
26933af08d82Slm66018 		DMSG(vdc, 0, "[%d] Unbinding DRing\n", vdc->instance);
26941ae08745Sheppo 		status = ldc_mem_dring_unbind(vdc->ldc_dring_hdl);
26951ae08745Sheppo 		if (status == 0) {
26961ae08745Sheppo 			vdc->initialized &= ~VDC_DRING_BOUND;
26971ae08745Sheppo 		} else {
26983af08d82Slm66018 			DMSG(vdc, 0, "[%d] Error %d unbinding DRing %lx",
2699e1ebb9ecSlm66018 			    vdc->instance, status, vdc->ldc_dring_hdl);
27001ae08745Sheppo 		}
27013af08d82Slm66018 		kmem_free(vdc->dring_cookie, sizeof (ldc_mem_cookie_t));
27021ae08745Sheppo 	}
27031ae08745Sheppo 
27041ae08745Sheppo 	if (vdc->initialized & VDC_DRING_INIT) {
27053af08d82Slm66018 		DMSG(vdc, 0, "[%d] Destroying DRing\n", vdc->instance);
27061ae08745Sheppo 		status = ldc_mem_dring_destroy(vdc->ldc_dring_hdl);
27071ae08745Sheppo 		if (status == 0) {
27081ae08745Sheppo 			vdc->ldc_dring_hdl = NULL;
27091ae08745Sheppo 			bzero(&vdc->dring_mem_info, sizeof (ldc_mem_info_t));
27101ae08745Sheppo 			vdc->initialized &= ~VDC_DRING_INIT;
27111ae08745Sheppo 		} else {
27123af08d82Slm66018 			DMSG(vdc, 0, "[%d] Error %d destroying DRing (%lx)",
2713e1ebb9ecSlm66018 			    vdc->instance, status, vdc->ldc_dring_hdl);
27141ae08745Sheppo 		}
27151ae08745Sheppo 	}
27161ae08745Sheppo }
27171ae08745Sheppo 
27181ae08745Sheppo /*
27193af08d82Slm66018  * Function:
2720*90e2f9dcSlm66018  *	vdc_map_to_shared_dring()
27211ae08745Sheppo  *
27221ae08745Sheppo  * Description:
27233af08d82Slm66018  *	Copy contents of the local descriptor to the shared
27243af08d82Slm66018  *	memory descriptor.
27251ae08745Sheppo  *
27263af08d82Slm66018  * Arguments:
27273af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
27283af08d82Slm66018  *	idx	- descriptor ring index
27293af08d82Slm66018  *
27303af08d82Slm66018  * Return Code:
27313af08d82Slm66018  *	None
27321ae08745Sheppo  */
27331ae08745Sheppo static int
27343af08d82Slm66018 vdc_map_to_shared_dring(vdc_t *vdcp, int idx)
27351ae08745Sheppo {
27363af08d82Slm66018 	vdc_local_desc_t	*ldep;
27373af08d82Slm66018 	vd_dring_entry_t	*dep;
27383af08d82Slm66018 	int			rv;
27391ae08745Sheppo 
27403af08d82Slm66018 	ldep = &(vdcp->local_dring[idx]);
27411ae08745Sheppo 
27423af08d82Slm66018 	/* for now leave in the old pop_mem_hdl stuff */
27433af08d82Slm66018 	if (ldep->nbytes > 0) {
27443af08d82Slm66018 		rv = vdc_populate_mem_hdl(vdcp, ldep);
27453af08d82Slm66018 		if (rv) {
27463af08d82Slm66018 			DMSG(vdcp, 0, "[%d] Cannot populate mem handle\n",
27473af08d82Slm66018 			    vdcp->instance);
27483af08d82Slm66018 			return (rv);
27493af08d82Slm66018 		}
27503af08d82Slm66018 	}
27511ae08745Sheppo 
27523af08d82Slm66018 	/*
27533af08d82Slm66018 	 * fill in the data details into the DRing
27543af08d82Slm66018 	 */
2755d10e4ef2Snarayan 	dep = ldep->dep;
27561ae08745Sheppo 	ASSERT(dep != NULL);
27571ae08745Sheppo 
27583af08d82Slm66018 	dep->payload.req_id = VDC_GET_NEXT_REQ_ID(vdcp);
27593af08d82Slm66018 	dep->payload.operation = ldep->operation;
27603af08d82Slm66018 	dep->payload.addr = ldep->offset;
27613af08d82Slm66018 	dep->payload.nbytes = ldep->nbytes;
2762055d7c80Scarlsonj 	dep->payload.status = (uint32_t)-1;	/* vds will set valid value */
27633af08d82Slm66018 	dep->payload.slice = ldep->slice;
27643af08d82Slm66018 	dep->hdr.dstate = VIO_DESC_READY;
27653af08d82Slm66018 	dep->hdr.ack = 1;		/* request an ACK for every message */
27661ae08745Sheppo 
27673af08d82Slm66018 	return (0);
27681ae08745Sheppo }
27691ae08745Sheppo 
27701ae08745Sheppo /*
27711ae08745Sheppo  * Function:
27723af08d82Slm66018  *	vdc_send_request
27733af08d82Slm66018  *
27743af08d82Slm66018  * Description:
27753af08d82Slm66018  *	This routine writes the data to be transmitted to vds into the
27763af08d82Slm66018  *	descriptor, notifies vds that the ring has been updated and
27773af08d82Slm66018  *	then waits for the request to be processed.
27783af08d82Slm66018  *
27793af08d82Slm66018  * Arguments:
27803af08d82Slm66018  *	vdcp	  - the soft state pointer
27813af08d82Slm66018  *	operation - operation we want vds to perform (VD_OP_XXX)
27823af08d82Slm66018  *	addr	  - address of data buf to be read/written.
27833af08d82Slm66018  *	nbytes	  - number of bytes to read/write
27843af08d82Slm66018  *	slice	  - the disk slice this request is for
27853af08d82Slm66018  *	offset	  - relative disk offset
27863af08d82Slm66018  *	cb_type   - type of call - STRATEGY or SYNC
27873af08d82Slm66018  *	cb_arg	  - parameter to be sent to server (depends on VD_OP_XXX type)
27883af08d82Slm66018  *			. mode for ioctl(9e)
27893af08d82Slm66018  *			. LP64 diskaddr_t (block I/O)
27903af08d82Slm66018  *	dir	  - direction of operation (READ/WRITE/BOTH)
27913af08d82Slm66018  *
27923af08d82Slm66018  * Return Codes:
27933af08d82Slm66018  *	0
27943af08d82Slm66018  *	ENXIO
27953af08d82Slm66018  */
27963af08d82Slm66018 static int
27973af08d82Slm66018 vdc_send_request(vdc_t *vdcp, int operation, caddr_t addr,
27983af08d82Slm66018     size_t nbytes, int slice, diskaddr_t offset, int cb_type,
27993af08d82Slm66018     void *cb_arg, vio_desc_direction_t dir)
28003af08d82Slm66018 {
2801366a92acSlm66018 	int	rv = 0;
2802366a92acSlm66018 
28033af08d82Slm66018 	ASSERT(vdcp != NULL);
280487a7269eSachartre 	ASSERT(slice == VD_SLICE_NONE || slice < V_NUMPAR);
28053af08d82Slm66018 
28063af08d82Slm66018 	mutex_enter(&vdcp->lock);
28073af08d82Slm66018 
2808366a92acSlm66018 	/*
2809366a92acSlm66018 	 * If this is a block read/write operation we update the I/O statistics
2810366a92acSlm66018 	 * to indicate that the request is being put on the waitq to be
2811366a92acSlm66018 	 * serviced.
2812366a92acSlm66018 	 *
2813366a92acSlm66018 	 * We do it here (a common routine for both synchronous and strategy
2814366a92acSlm66018 	 * calls) for performance reasons - we are already holding vdc->lock
2815366a92acSlm66018 	 * so there is no extra locking overhead. We would have to explicitly
2816366a92acSlm66018 	 * grab the 'lock' mutex to update the stats if we were to do this
2817366a92acSlm66018 	 * higher up the stack in vdc_strategy() et. al.
2818366a92acSlm66018 	 */
2819366a92acSlm66018 	if ((operation == VD_OP_BREAD) || (operation == VD_OP_BWRITE)) {
2820366a92acSlm66018 		DTRACE_IO1(start, buf_t *, cb_arg);
2821*90e2f9dcSlm66018 		VD_KSTAT_WAITQ_ENTER(vdcp);
2822366a92acSlm66018 	}
2823366a92acSlm66018 
28243af08d82Slm66018 	do {
28253c96341aSnarayan 		while (vdcp->state != VDC_STATE_RUNNING) {
28263af08d82Slm66018 
28273c96341aSnarayan 			/* return error if detaching */
28283c96341aSnarayan 			if (vdcp->state == VDC_STATE_DETACH) {
2829366a92acSlm66018 				rv = ENXIO;
2830366a92acSlm66018 				goto done;
28313c96341aSnarayan 			}
2832655fd6a9Sachartre 
2833655fd6a9Sachartre 			/* fail request if connection timeout is reached */
2834655fd6a9Sachartre 			if (vdcp->ctimeout_reached) {
2835366a92acSlm66018 				rv = EIO;
2836366a92acSlm66018 				goto done;
2837655fd6a9Sachartre 			}
2838655fd6a9Sachartre 
28392f5224aeSachartre 			/*
28402f5224aeSachartre 			 * If we are panicking and the disk is not ready then
28412f5224aeSachartre 			 * we can't send any request because we can't complete
28422f5224aeSachartre 			 * the handshake now.
28432f5224aeSachartre 			 */
28442f5224aeSachartre 			if (ddi_in_panic()) {
2845366a92acSlm66018 				rv = EIO;
2846366a92acSlm66018 				goto done;
28472f5224aeSachartre 			}
28482f5224aeSachartre 
2849655fd6a9Sachartre 			cv_wait(&vdcp->running_cv, &vdcp->lock);
28503c96341aSnarayan 		}
28513c96341aSnarayan 
28523af08d82Slm66018 	} while (vdc_populate_descriptor(vdcp, operation, addr,
28533af08d82Slm66018 	    nbytes, slice, offset, cb_type, cb_arg, dir));
28543af08d82Slm66018 
2855366a92acSlm66018 done:
2856366a92acSlm66018 	/*
2857366a92acSlm66018 	 * If this is a block read/write we update the I/O statistics kstat
2858366a92acSlm66018 	 * to indicate that this request has been placed on the queue for
2859366a92acSlm66018 	 * processing (i.e sent to the vDisk server) - iostat(1M) will
2860366a92acSlm66018 	 * report the time waiting for the vDisk server under the %b column
2861366a92acSlm66018 	 * In the case of an error we simply take it off the wait queue.
2862366a92acSlm66018 	 */
2863366a92acSlm66018 	if ((operation == VD_OP_BREAD) || (operation == VD_OP_BWRITE)) {
2864366a92acSlm66018 		if (rv == 0) {
2865*90e2f9dcSlm66018 			VD_KSTAT_WAITQ_TO_RUNQ(vdcp);
2866366a92acSlm66018 			DTRACE_PROBE1(send, buf_t *, cb_arg);
2867366a92acSlm66018 		} else {
2868366a92acSlm66018 			VD_UPDATE_ERR_STATS(vdcp, vd_transerrs);
2869*90e2f9dcSlm66018 			VD_KSTAT_WAITQ_EXIT(vdcp);
2870366a92acSlm66018 			DTRACE_IO1(done, buf_t *, cb_arg);
2871366a92acSlm66018 		}
2872366a92acSlm66018 	}
2873366a92acSlm66018 
28743af08d82Slm66018 	mutex_exit(&vdcp->lock);
2875366a92acSlm66018 
2876366a92acSlm66018 	return (rv);
28773af08d82Slm66018 }
28783af08d82Slm66018 
28793af08d82Slm66018 
28803af08d82Slm66018 /*
28813af08d82Slm66018  * Function:
28821ae08745Sheppo  *	vdc_populate_descriptor
28831ae08745Sheppo  *
28841ae08745Sheppo  * Description:
28851ae08745Sheppo  *	This routine writes the data to be transmitted to vds into the
28861ae08745Sheppo  *	descriptor, notifies vds that the ring has been updated and
28871ae08745Sheppo  *	then waits for the request to be processed.
28881ae08745Sheppo  *
28891ae08745Sheppo  * Arguments:
28903af08d82Slm66018  *	vdcp	  - the soft state pointer
28911ae08745Sheppo  *	operation - operation we want vds to perform (VD_OP_XXX)
28923af08d82Slm66018  *	addr	  - address of data buf to be read/written.
28933af08d82Slm66018  *	nbytes	  - number of bytes to read/write
28943af08d82Slm66018  *	slice	  - the disk slice this request is for
28953af08d82Slm66018  *	offset	  - relative disk offset
28963af08d82Slm66018  *	cb_type   - type of call - STRATEGY or SYNC
28973af08d82Slm66018  *	cb_arg	  - parameter to be sent to server (depends on VD_OP_XXX type)
28981ae08745Sheppo  *			. mode for ioctl(9e)
28991ae08745Sheppo  *			. LP64 diskaddr_t (block I/O)
29003af08d82Slm66018  *	dir	  - direction of operation (READ/WRITE/BOTH)
29011ae08745Sheppo  *
29021ae08745Sheppo  * Return Codes:
29031ae08745Sheppo  *	0
29041ae08745Sheppo  *	EAGAIN
290517cadca8Slm66018  *	ECONNRESET
29061ae08745Sheppo  *	ENXIO
29071ae08745Sheppo  */
29081ae08745Sheppo static int
29093af08d82Slm66018 vdc_populate_descriptor(vdc_t *vdcp, int operation, caddr_t addr,
29103af08d82Slm66018     size_t nbytes, int slice, diskaddr_t offset, int cb_type,
29113af08d82Slm66018     void *cb_arg, vio_desc_direction_t dir)
29121ae08745Sheppo {
29133af08d82Slm66018 	vdc_local_desc_t	*local_dep = NULL; /* Local Dring Pointer */
29143af08d82Slm66018 	int			idx;		/* Index of DRing entry used */
29153af08d82Slm66018 	int			next_idx;
29161ae08745Sheppo 	vio_dring_msg_t		dmsg;
29173af08d82Slm66018 	size_t			msglen;
29188e6a2a04Slm66018 	int			rv;
29191ae08745Sheppo 
29203af08d82Slm66018 	ASSERT(MUTEX_HELD(&vdcp->lock));
29213af08d82Slm66018 	vdcp->threads_pending++;
29223af08d82Slm66018 loop:
29233af08d82Slm66018 	DMSG(vdcp, 2, ": dring_curr_idx = %d\n", vdcp->dring_curr_idx);
29241ae08745Sheppo 
29253af08d82Slm66018 	/* Get next available D-Ring entry */
29263af08d82Slm66018 	idx = vdcp->dring_curr_idx;
29273af08d82Slm66018 	local_dep = &(vdcp->local_dring[idx]);
29281ae08745Sheppo 
29293af08d82Slm66018 	if (!local_dep->is_free) {
29303af08d82Slm66018 		DMSG(vdcp, 2, "[%d]: dring full - waiting for space\n",
29313af08d82Slm66018 		    vdcp->instance);
29323af08d82Slm66018 		cv_wait(&vdcp->dring_free_cv, &vdcp->lock);
29333af08d82Slm66018 		if (vdcp->state == VDC_STATE_RUNNING ||
29343af08d82Slm66018 		    vdcp->state == VDC_STATE_HANDLE_PENDING) {
29353af08d82Slm66018 			goto loop;
29363af08d82Slm66018 		}
29373af08d82Slm66018 		vdcp->threads_pending--;
29383af08d82Slm66018 		return (ECONNRESET);
29391ae08745Sheppo 	}
29401ae08745Sheppo 
29413af08d82Slm66018 	next_idx = idx + 1;
29423af08d82Slm66018 	if (next_idx >= vdcp->dring_len)
29433af08d82Slm66018 		next_idx = 0;
29443af08d82Slm66018 	vdcp->dring_curr_idx = next_idx;
29451ae08745Sheppo 
29463af08d82Slm66018 	ASSERT(local_dep->is_free);
29471ae08745Sheppo 
29483af08d82Slm66018 	local_dep->operation = operation;
2949d10e4ef2Snarayan 	local_dep->addr = addr;
29503af08d82Slm66018 	local_dep->nbytes = nbytes;
29513af08d82Slm66018 	local_dep->slice = slice;
29523af08d82Slm66018 	local_dep->offset = offset;
29533af08d82Slm66018 	local_dep->cb_type = cb_type;
29543af08d82Slm66018 	local_dep->cb_arg = cb_arg;
29553af08d82Slm66018 	local_dep->dir = dir;
29563af08d82Slm66018 
29573af08d82Slm66018 	local_dep->is_free = B_FALSE;
29583af08d82Slm66018 
29593af08d82Slm66018 	rv = vdc_map_to_shared_dring(vdcp, idx);
29603af08d82Slm66018 	if (rv) {
29613af08d82Slm66018 		DMSG(vdcp, 0, "[%d]: cannot bind memory - waiting ..\n",
29623af08d82Slm66018 		    vdcp->instance);
29633af08d82Slm66018 		/* free the descriptor */
29643af08d82Slm66018 		local_dep->is_free = B_TRUE;
29653af08d82Slm66018 		vdcp->dring_curr_idx = idx;
29663af08d82Slm66018 		cv_wait(&vdcp->membind_cv, &vdcp->lock);
29673af08d82Slm66018 		if (vdcp->state == VDC_STATE_RUNNING ||
29683af08d82Slm66018 		    vdcp->state == VDC_STATE_HANDLE_PENDING) {
29693af08d82Slm66018 			goto loop;
29701ae08745Sheppo 		}
29713af08d82Slm66018 		vdcp->threads_pending--;
29723af08d82Slm66018 		return (ECONNRESET);
29731ae08745Sheppo 	}
29741ae08745Sheppo 
29751ae08745Sheppo 	/*
29761ae08745Sheppo 	 * Send a msg with the DRing details to vds
29771ae08745Sheppo 	 */
29781ae08745Sheppo 	VIO_INIT_DRING_DATA_TAG(dmsg);
29793af08d82Slm66018 	VDC_INIT_DRING_DATA_MSG_IDS(dmsg, vdcp);
29803af08d82Slm66018 	dmsg.dring_ident = vdcp->dring_ident;
29811ae08745Sheppo 	dmsg.start_idx = idx;
29821ae08745Sheppo 	dmsg.end_idx = idx;
29833af08d82Slm66018 	vdcp->seq_num++;
29841ae08745Sheppo 
2985366a92acSlm66018 	DTRACE_PROBE2(populate, int, vdcp->instance,
2986366a92acSlm66018 	    vdc_local_desc_t *, local_dep);
29873af08d82Slm66018 	DMSG(vdcp, 2, "ident=0x%lx, st=%u, end=%u, seq=%ld\n",
29883af08d82Slm66018 	    vdcp->dring_ident, dmsg.start_idx, dmsg.end_idx, dmsg.seq_num);
29891ae08745Sheppo 
29903af08d82Slm66018 	/*
29913af08d82Slm66018 	 * note we're still holding the lock here to
29923af08d82Slm66018 	 * make sure the message goes out in order !!!...
29933af08d82Slm66018 	 */
29943af08d82Slm66018 	msglen = sizeof (dmsg);
29953af08d82Slm66018 	rv = vdc_send(vdcp, (caddr_t)&dmsg, &msglen);
29963af08d82Slm66018 	switch (rv) {
29973af08d82Slm66018 	case ECONNRESET:
29983af08d82Slm66018 		/*
29993af08d82Slm66018 		 * vdc_send initiates the reset on failure.
30003af08d82Slm66018 		 * Since the transaction has already been put
30013af08d82Slm66018 		 * on the local dring, it will automatically get
30023af08d82Slm66018 		 * retried when the channel is reset. Given that,
30033af08d82Slm66018 		 * it is ok to just return success even though the
30043af08d82Slm66018 		 * send failed.
30053af08d82Slm66018 		 */
30063af08d82Slm66018 		rv = 0;
30073af08d82Slm66018 		break;
3008d10e4ef2Snarayan 
30093af08d82Slm66018 	case 0: /* EOK */
30103af08d82Slm66018 		DMSG(vdcp, 1, "sent via LDC: rv=%d\n", rv);
30113af08d82Slm66018 		break;
3012d10e4ef2Snarayan 
30133af08d82Slm66018 	default:
30143af08d82Slm66018 		goto cleanup_and_exit;
30153af08d82Slm66018 	}
3016e1ebb9ecSlm66018 
30173af08d82Slm66018 	vdcp->threads_pending--;
30183af08d82Slm66018 	return (rv);
30193af08d82Slm66018 
30203af08d82Slm66018 cleanup_and_exit:
30213af08d82Slm66018 	DMSG(vdcp, 0, "unexpected error, rv=%d\n", rv);
30223af08d82Slm66018 	return (ENXIO);
30231ae08745Sheppo }
30241ae08745Sheppo 
30251ae08745Sheppo /*
30263af08d82Slm66018  * Function:
30273af08d82Slm66018  *	vdc_do_sync_op
30283af08d82Slm66018  *
30293af08d82Slm66018  * Description:
30303af08d82Slm66018  * 	Wrapper around vdc_populate_descriptor that blocks until the
30313af08d82Slm66018  * 	response to the message is available.
30323af08d82Slm66018  *
30333af08d82Slm66018  * Arguments:
30343af08d82Slm66018  *	vdcp	  - the soft state pointer
30353af08d82Slm66018  *	operation - operation we want vds to perform (VD_OP_XXX)
30363af08d82Slm66018  *	addr	  - address of data buf to be read/written.
30373af08d82Slm66018  *	nbytes	  - number of bytes to read/write
30383af08d82Slm66018  *	slice	  - the disk slice this request is for
30393af08d82Slm66018  *	offset	  - relative disk offset
30403af08d82Slm66018  *	cb_type   - type of call - STRATEGY or SYNC
30413af08d82Slm66018  *	cb_arg	  - parameter to be sent to server (depends on VD_OP_XXX type)
30423af08d82Slm66018  *			. mode for ioctl(9e)
30433af08d82Slm66018  *			. LP64 diskaddr_t (block I/O)
30443af08d82Slm66018  *	dir	  - direction of operation (READ/WRITE/BOTH)
30452f5224aeSachartre  *	rconflict - check for reservation conflict in case of failure
30462f5224aeSachartre  *
30472f5224aeSachartre  * rconflict should be set to B_TRUE by most callers. Callers invoking the
30482f5224aeSachartre  * VD_OP_SCSICMD operation can set rconflict to B_FALSE if they check the
30492f5224aeSachartre  * result of a successful operation with vd_scsi_status().
30503af08d82Slm66018  *
30513af08d82Slm66018  * Return Codes:
30523af08d82Slm66018  *	0
30533af08d82Slm66018  *	EAGAIN
30543af08d82Slm66018  *	EFAULT
30553af08d82Slm66018  *	ENXIO
30563af08d82Slm66018  *	EIO
30570a55fbb7Slm66018  */
30583af08d82Slm66018 static int
30593af08d82Slm66018 vdc_do_sync_op(vdc_t *vdcp, int operation, caddr_t addr, size_t nbytes,
30603af08d82Slm66018     int slice, diskaddr_t offset, int cb_type, void *cb_arg,
30612f5224aeSachartre     vio_desc_direction_t dir, boolean_t rconflict)
30623af08d82Slm66018 {
30633af08d82Slm66018 	int status;
30642f5224aeSachartre 	vdc_io_t *vio;
30652f5224aeSachartre 	boolean_t check_resv_conflict = B_FALSE;
30663af08d82Slm66018 
30673af08d82Slm66018 	ASSERT(cb_type == CB_SYNC);
30681ae08745Sheppo 
30691ae08745Sheppo 	/*
30703af08d82Slm66018 	 * Grab the lock, if blocked wait until the server
30713af08d82Slm66018 	 * response causes us to wake up again.
30723af08d82Slm66018 	 */
30733af08d82Slm66018 	mutex_enter(&vdcp->lock);
30743af08d82Slm66018 	vdcp->sync_op_cnt++;
30753af08d82Slm66018 	while (vdcp->sync_op_blocked && vdcp->state != VDC_STATE_DETACH)
30763af08d82Slm66018 		cv_wait(&vdcp->sync_blocked_cv, &vdcp->lock);
30773af08d82Slm66018 
30783af08d82Slm66018 	if (vdcp->state == VDC_STATE_DETACH) {
30793af08d82Slm66018 		cv_broadcast(&vdcp->sync_blocked_cv);
30803af08d82Slm66018 		vdcp->sync_op_cnt--;
30813af08d82Slm66018 		mutex_exit(&vdcp->lock);
30823af08d82Slm66018 		return (ENXIO);
30833af08d82Slm66018 	}
30843af08d82Slm66018 
30853af08d82Slm66018 	/* now block anyone other thread entering after us */
30863af08d82Slm66018 	vdcp->sync_op_blocked = B_TRUE;
30873af08d82Slm66018 	vdcp->sync_op_pending = B_TRUE;
30883af08d82Slm66018 	mutex_exit(&vdcp->lock);
30893af08d82Slm66018 
3090655fd6a9Sachartre 	status = vdc_send_request(vdcp, operation, addr,
30913af08d82Slm66018 	    nbytes, slice, offset, cb_type, cb_arg, dir);
30923af08d82Slm66018 
3093655fd6a9Sachartre 	mutex_enter(&vdcp->lock);
3094655fd6a9Sachartre 
3095655fd6a9Sachartre 	if (status != 0) {
3096655fd6a9Sachartre 		vdcp->sync_op_pending = B_FALSE;
3097655fd6a9Sachartre 	} else {
30983af08d82Slm66018 		/*
30993af08d82Slm66018 		 * block until our transaction completes.
31003af08d82Slm66018 		 * Also anyone else waiting also gets to go next.
31013af08d82Slm66018 		 */
31023af08d82Slm66018 		while (vdcp->sync_op_pending && vdcp->state != VDC_STATE_DETACH)
31033af08d82Slm66018 			cv_wait(&vdcp->sync_pending_cv, &vdcp->lock);
31043af08d82Slm66018 
3105655fd6a9Sachartre 		DMSG(vdcp, 2, ": operation returned %d\n",
3106655fd6a9Sachartre 		    vdcp->sync_op_status);
31073c96341aSnarayan 		if (vdcp->state == VDC_STATE_DETACH) {
31083c96341aSnarayan 			vdcp->sync_op_pending = B_FALSE;
31093af08d82Slm66018 			status = ENXIO;
31103c96341aSnarayan 		} else {
31113af08d82Slm66018 			status = vdcp->sync_op_status;
31122f5224aeSachartre 			if (status != 0 && vdcp->failfast_interval != 0) {
31132f5224aeSachartre 				/*
31142f5224aeSachartre 				 * Operation has failed and failfast is enabled.
31152f5224aeSachartre 				 * We need to check if the failure is due to a
31162f5224aeSachartre 				 * reservation conflict if this was requested.
31172f5224aeSachartre 				 */
31182f5224aeSachartre 				check_resv_conflict = rconflict;
31192f5224aeSachartre 			}
31202f5224aeSachartre 
31213c96341aSnarayan 		}
3122655fd6a9Sachartre 	}
31233c96341aSnarayan 
31243af08d82Slm66018 	vdcp->sync_op_status = 0;
31253af08d82Slm66018 	vdcp->sync_op_blocked = B_FALSE;
31263af08d82Slm66018 	vdcp->sync_op_cnt--;
31273af08d82Slm66018 
31283af08d82Slm66018 	/* signal the next waiting thread */
31293af08d82Slm66018 	cv_signal(&vdcp->sync_blocked_cv);
31302f5224aeSachartre 
31312f5224aeSachartre 	/*
31322f5224aeSachartre 	 * We have to check for reservation conflict after unblocking sync
31332f5224aeSachartre 	 * operations because some sync operations will be used to do this
31342f5224aeSachartre 	 * check.
31352f5224aeSachartre 	 */
31362f5224aeSachartre 	if (check_resv_conflict) {
31372f5224aeSachartre 		vio = vdc_failfast_io_queue(vdcp, NULL);
31382f5224aeSachartre 		while (vio->vio_qtime != 0)
31392f5224aeSachartre 			cv_wait(&vdcp->failfast_io_cv, &vdcp->lock);
31402f5224aeSachartre 		kmem_free(vio, sizeof (vdc_io_t));
31412f5224aeSachartre 	}
31422f5224aeSachartre 
31433af08d82Slm66018 	mutex_exit(&vdcp->lock);
31443af08d82Slm66018 
31453af08d82Slm66018 	return (status);
31463af08d82Slm66018 }
31473af08d82Slm66018 
31483af08d82Slm66018 
31493af08d82Slm66018 /*
31503af08d82Slm66018  * Function:
31513af08d82Slm66018  *	vdc_drain_response()
31523af08d82Slm66018  *
31533af08d82Slm66018  * Description:
31541ae08745Sheppo  * 	When a guest is panicking, the completion of requests needs to be
31551ae08745Sheppo  * 	handled differently because interrupts are disabled and vdc
31561ae08745Sheppo  * 	will not get messages. We have to poll for the messages instead.
31573af08d82Slm66018  *
3158366a92acSlm66018  *	Note: since we don't have a buf_t available we cannot implement
3159366a92acSlm66018  *	the io:::done DTrace probe in this specific case.
3160366a92acSlm66018  *
31613af08d82Slm66018  * Arguments:
31623af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
31633af08d82Slm66018  *
31643af08d82Slm66018  * Return Code:
31653af08d82Slm66018  *	0	- Success
31661ae08745Sheppo  */
31673af08d82Slm66018 static int
31683af08d82Slm66018 vdc_drain_response(vdc_t *vdc)
31693af08d82Slm66018 {
31703af08d82Slm66018 	int 			rv, idx, retries;
31713af08d82Slm66018 	size_t			msglen;
31723af08d82Slm66018 	vdc_local_desc_t 	*ldep = NULL;	/* Local Dring Entry Pointer */
31733af08d82Slm66018 	vio_dring_msg_t		dmsg;
31743af08d82Slm66018 
31753af08d82Slm66018 	mutex_enter(&vdc->lock);
31763af08d82Slm66018 
31771ae08745Sheppo 	retries = 0;
31781ae08745Sheppo 	for (;;) {
31791ae08745Sheppo 		msglen = sizeof (dmsg);
31803af08d82Slm66018 		rv = ldc_read(vdc->ldc_handle, (caddr_t)&dmsg, &msglen);
31818e6a2a04Slm66018 		if (rv) {
31828e6a2a04Slm66018 			rv = EINVAL;
31831ae08745Sheppo 			break;
31841ae08745Sheppo 		}
31851ae08745Sheppo 
31861ae08745Sheppo 		/*
31871ae08745Sheppo 		 * if there are no packets wait and check again
31881ae08745Sheppo 		 */
31898e6a2a04Slm66018 		if ((rv == 0) && (msglen == 0)) {
31901ae08745Sheppo 			if (retries++ > vdc_dump_retries) {
31918e6a2a04Slm66018 				rv = EAGAIN;
31921ae08745Sheppo 				break;
31931ae08745Sheppo 			}
31941ae08745Sheppo 
3195d10e4ef2Snarayan 			drv_usecwait(vdc_usec_timeout_dump);
31961ae08745Sheppo 			continue;
31971ae08745Sheppo 		}
31981ae08745Sheppo 
31991ae08745Sheppo 		/*
32001ae08745Sheppo 		 * Ignore all messages that are not ACKs/NACKs to
32011ae08745Sheppo 		 * DRing requests.
32021ae08745Sheppo 		 */
32031ae08745Sheppo 		if ((dmsg.tag.vio_msgtype != VIO_TYPE_DATA) ||
32041ae08745Sheppo 		    (dmsg.tag.vio_subtype_env != VIO_DRING_DATA)) {
32053af08d82Slm66018 			DMSG(vdc, 0, "discard pkt: type=%d sub=%d env=%d\n",
32061ae08745Sheppo 			    dmsg.tag.vio_msgtype,
32071ae08745Sheppo 			    dmsg.tag.vio_subtype,
32081ae08745Sheppo 			    dmsg.tag.vio_subtype_env);
32091ae08745Sheppo 			continue;
32101ae08745Sheppo 		}
32111ae08745Sheppo 
32121ae08745Sheppo 		/*
32133af08d82Slm66018 		 * set the appropriate return value for the current request.
32141ae08745Sheppo 		 */
32151ae08745Sheppo 		switch (dmsg.tag.vio_subtype) {
32161ae08745Sheppo 		case VIO_SUBTYPE_ACK:
32178e6a2a04Slm66018 			rv = 0;
32181ae08745Sheppo 			break;
32191ae08745Sheppo 		case VIO_SUBTYPE_NACK:
32208e6a2a04Slm66018 			rv = EAGAIN;
32211ae08745Sheppo 			break;
32221ae08745Sheppo 		default:
32231ae08745Sheppo 			continue;
32241ae08745Sheppo 		}
32251ae08745Sheppo 
32263af08d82Slm66018 		idx = dmsg.start_idx;
32273af08d82Slm66018 		if (idx >= vdc->dring_len) {
32283af08d82Slm66018 			DMSG(vdc, 0, "[%d] Bogus ack data : start %d\n",
3229e1ebb9ecSlm66018 			    vdc->instance, idx);
32303af08d82Slm66018 			continue;
32311ae08745Sheppo 		}
32323af08d82Slm66018 		ldep = &vdc->local_dring[idx];
32333af08d82Slm66018 		if (ldep->dep->hdr.dstate != VIO_DESC_DONE) {
32343af08d82Slm66018 			DMSG(vdc, 0, "[%d] Entry @ %d - state !DONE %d\n",
32353af08d82Slm66018 			    vdc->instance, idx, ldep->dep->hdr.dstate);
32361ae08745Sheppo 			continue;
32371ae08745Sheppo 		}
32381ae08745Sheppo 
32393af08d82Slm66018 		DMSG(vdc, 1, "[%d] Depopulating idx=%d state=%d\n",
32403af08d82Slm66018 		    vdc->instance, idx, ldep->dep->hdr.dstate);
3241366a92acSlm66018 
32423af08d82Slm66018 		rv = vdc_depopulate_descriptor(vdc, idx);
32433af08d82Slm66018 		if (rv) {
32443af08d82Slm66018 			DMSG(vdc, 0,
32453af08d82Slm66018 			    "[%d] Entry @ %d - depopulate failed ..\n",
32463af08d82Slm66018 			    vdc->instance, idx);
32471ae08745Sheppo 		}
32481ae08745Sheppo 
32493af08d82Slm66018 		/* if this is the last descriptor - break out of loop */
32503af08d82Slm66018 		if ((idx + 1) % vdc->dring_len == vdc->dring_curr_idx)
32513af08d82Slm66018 			break;
32523af08d82Slm66018 	}
32533af08d82Slm66018 
32543af08d82Slm66018 	mutex_exit(&vdc->lock);
32553af08d82Slm66018 	DMSG(vdc, 0, "End idx=%d\n", idx);
32563af08d82Slm66018 
32573af08d82Slm66018 	return (rv);
32581ae08745Sheppo }
32591ae08745Sheppo 
32601ae08745Sheppo 
32610a55fbb7Slm66018 /*
32620a55fbb7Slm66018  * Function:
32630a55fbb7Slm66018  *	vdc_depopulate_descriptor()
32640a55fbb7Slm66018  *
32650a55fbb7Slm66018  * Description:
32660a55fbb7Slm66018  *
32670a55fbb7Slm66018  * Arguments:
32680a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
32690a55fbb7Slm66018  *	idx	- Index of the Descriptor Ring entry being modified
32700a55fbb7Slm66018  *
32710a55fbb7Slm66018  * Return Code:
32720a55fbb7Slm66018  *	0	- Success
32730a55fbb7Slm66018  */
32741ae08745Sheppo static int
32751ae08745Sheppo vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx)
32761ae08745Sheppo {
32771ae08745Sheppo 	vd_dring_entry_t *dep = NULL;		/* Dring Entry Pointer */
32781ae08745Sheppo 	vdc_local_desc_t *ldep = NULL;		/* Local Dring Entry Pointer */
32791ae08745Sheppo 	int		status = ENXIO;
32808e6a2a04Slm66018 	int		rv = 0;
32811ae08745Sheppo 
32821ae08745Sheppo 	ASSERT(vdc != NULL);
3283e1ebb9ecSlm66018 	ASSERT(idx < vdc->dring_len);
32841ae08745Sheppo 	ldep = &vdc->local_dring[idx];
32851ae08745Sheppo 	ASSERT(ldep != NULL);
32863af08d82Slm66018 	ASSERT(MUTEX_HELD(&vdc->lock));
32873af08d82Slm66018 
3288366a92acSlm66018 	DTRACE_PROBE2(depopulate, int, vdc->instance, vdc_local_desc_t *, ldep);
32893af08d82Slm66018 	DMSG(vdc, 2, ": idx = %d\n", idx);
3290366a92acSlm66018 
32911ae08745Sheppo 	dep = ldep->dep;
32921ae08745Sheppo 	ASSERT(dep != NULL);
3293e1ebb9ecSlm66018 	ASSERT((dep->hdr.dstate == VIO_DESC_DONE) ||
3294e1ebb9ecSlm66018 	    (dep->payload.status == ECANCELED));
32951ae08745Sheppo 
3296e1ebb9ecSlm66018 	VDC_MARK_DRING_ENTRY_FREE(vdc, idx);
32973af08d82Slm66018 
32983af08d82Slm66018 	ldep->is_free = B_TRUE;
32991ae08745Sheppo 	status = dep->payload.status;
3300205eeb1aSlm66018 	DMSG(vdc, 2, ": is_free = %d : status = %d\n", ldep->is_free, status);
33011ae08745Sheppo 
3302eff7243fSlm66018 	/*
3303eff7243fSlm66018 	 * If no buffers were used to transfer information to the server when
3304eff7243fSlm66018 	 * populating the descriptor then no memory handles need to be unbound
3305eff7243fSlm66018 	 * and we can return now.
3306eff7243fSlm66018 	 */
3307eff7243fSlm66018 	if (ldep->nbytes == 0) {
3308eff7243fSlm66018 		cv_signal(&vdc->dring_free_cv);
33098e6a2a04Slm66018 		return (status);
3310eff7243fSlm66018 	}
33118e6a2a04Slm66018 
33121ae08745Sheppo 	/*
33131ae08745Sheppo 	 * If the upper layer passed in a misaligned address we copied the
33141ae08745Sheppo 	 * data into an aligned buffer before sending it to LDC - we now
33151ae08745Sheppo 	 * copy it back to the original buffer.
33161ae08745Sheppo 	 */
33171ae08745Sheppo 	if (ldep->align_addr) {
33181ae08745Sheppo 		ASSERT(ldep->addr != NULL);
33191ae08745Sheppo 
33203c96341aSnarayan 		if (dep->payload.nbytes > 0)
33213c96341aSnarayan 			bcopy(ldep->align_addr, ldep->addr,
33223c96341aSnarayan 			    dep->payload.nbytes);
33231ae08745Sheppo 		kmem_free(ldep->align_addr,
33243c96341aSnarayan 		    sizeof (caddr_t) * P2ROUNDUP(ldep->nbytes, 8));
33251ae08745Sheppo 		ldep->align_addr = NULL;
33261ae08745Sheppo 	}
33271ae08745Sheppo 
33288e6a2a04Slm66018 	rv = ldc_mem_unbind_handle(ldep->desc_mhdl);
33298e6a2a04Slm66018 	if (rv != 0) {
33303af08d82Slm66018 		DMSG(vdc, 0, "?[%d] unbind mhdl 0x%lx @ idx %d failed (%d)",
33318e6a2a04Slm66018 		    vdc->instance, ldep->desc_mhdl, idx, rv);
33328e6a2a04Slm66018 		/*
33338e6a2a04Slm66018 		 * The error returned by the vDisk server is more informative
33348e6a2a04Slm66018 		 * and thus has a higher priority but if it isn't set we ensure
33358e6a2a04Slm66018 		 * that this function returns an error.
33368e6a2a04Slm66018 		 */
33378e6a2a04Slm66018 		if (status == 0)
33388e6a2a04Slm66018 			status = EINVAL;
33391ae08745Sheppo 	}
33401ae08745Sheppo 
33413af08d82Slm66018 	cv_signal(&vdc->membind_cv);
33423af08d82Slm66018 	cv_signal(&vdc->dring_free_cv);
33433af08d82Slm66018 
33441ae08745Sheppo 	return (status);
33451ae08745Sheppo }
33461ae08745Sheppo 
33470a55fbb7Slm66018 /*
33480a55fbb7Slm66018  * Function:
33490a55fbb7Slm66018  *	vdc_populate_mem_hdl()
33500a55fbb7Slm66018  *
33510a55fbb7Slm66018  * Description:
33520a55fbb7Slm66018  *
33530a55fbb7Slm66018  * Arguments:
33540a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
33550a55fbb7Slm66018  *	idx	- Index of the Descriptor Ring entry being modified
33560a55fbb7Slm66018  *	addr	- virtual address being mapped in
33570a55fbb7Slm66018  *	nybtes	- number of bytes in 'addr'
33580a55fbb7Slm66018  *	operation - the vDisk operation being performed (VD_OP_xxx)
33590a55fbb7Slm66018  *
33600a55fbb7Slm66018  * Return Code:
33610a55fbb7Slm66018  *	0	- Success
33620a55fbb7Slm66018  */
33631ae08745Sheppo static int
33643af08d82Slm66018 vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep)
33651ae08745Sheppo {
33661ae08745Sheppo 	vd_dring_entry_t	*dep = NULL;
33671ae08745Sheppo 	ldc_mem_handle_t	mhdl;
33681ae08745Sheppo 	caddr_t			vaddr;
33693af08d82Slm66018 	size_t			nbytes;
33704bac2208Snarayan 	uint8_t			perm = LDC_MEM_RW;
33714bac2208Snarayan 	uint8_t			maptype;
33721ae08745Sheppo 	int			rv = 0;
33731ae08745Sheppo 	int			i;
33741ae08745Sheppo 
33753af08d82Slm66018 	ASSERT(vdcp != NULL);
33761ae08745Sheppo 
33773af08d82Slm66018 	dep = ldep->dep;
33781ae08745Sheppo 	mhdl = ldep->desc_mhdl;
33791ae08745Sheppo 
33803af08d82Slm66018 	switch (ldep->dir) {
33813af08d82Slm66018 	case VIO_read_dir:
33821ae08745Sheppo 		perm = LDC_MEM_W;
33831ae08745Sheppo 		break;
33841ae08745Sheppo 
33853af08d82Slm66018 	case VIO_write_dir:
33861ae08745Sheppo 		perm = LDC_MEM_R;
33871ae08745Sheppo 		break;
33881ae08745Sheppo 
33893af08d82Slm66018 	case VIO_both_dir:
33901ae08745Sheppo 		perm = LDC_MEM_RW;
33911ae08745Sheppo 		break;
33921ae08745Sheppo 
33931ae08745Sheppo 	default:
33941ae08745Sheppo 		ASSERT(0);	/* catch bad programming in vdc */
33951ae08745Sheppo 	}
33961ae08745Sheppo 
33971ae08745Sheppo 	/*
33981ae08745Sheppo 	 * LDC expects any addresses passed in to be 8-byte aligned. We need
33991ae08745Sheppo 	 * to copy the contents of any misaligned buffers to a newly allocated
34001ae08745Sheppo 	 * buffer and bind it instead (and copy the the contents back to the
34011ae08745Sheppo 	 * original buffer passed in when depopulating the descriptor)
34021ae08745Sheppo 	 */
34033af08d82Slm66018 	vaddr = ldep->addr;
34043af08d82Slm66018 	nbytes = ldep->nbytes;
34053af08d82Slm66018 	if (((uint64_t)vaddr & 0x7) != 0) {
3406d10e4ef2Snarayan 		ASSERT(ldep->align_addr == NULL);
34071ae08745Sheppo 		ldep->align_addr =
34083af08d82Slm66018 		    kmem_alloc(sizeof (caddr_t) *
34093af08d82Slm66018 		    P2ROUNDUP(nbytes, 8), KM_SLEEP);
34103af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Misaligned address %p reallocating "
34113af08d82Slm66018 		    "(buf=%p nb=%ld op=%d)\n",
34123af08d82Slm66018 		    vdcp->instance, (void *)vaddr, (void *)ldep->align_addr,
34133af08d82Slm66018 		    nbytes, ldep->operation);
34143af08d82Slm66018 		if (perm != LDC_MEM_W)
34153af08d82Slm66018 			bcopy(vaddr, ldep->align_addr, nbytes);
34161ae08745Sheppo 		vaddr = ldep->align_addr;
34171ae08745Sheppo 	}
34181ae08745Sheppo 
34194bac2208Snarayan 	maptype = LDC_IO_MAP|LDC_SHADOW_MAP|LDC_DIRECT_MAP;
34201ae08745Sheppo 	rv = ldc_mem_bind_handle(mhdl, vaddr, P2ROUNDUP(nbytes, 8),
342187a7269eSachartre 	    maptype, perm, &dep->payload.cookie[0], &dep->payload.ncookies);
34223af08d82Slm66018 	DMSG(vdcp, 2, "[%d] bound mem handle; ncookies=%d\n",
34233af08d82Slm66018 	    vdcp->instance, dep->payload.ncookies);
34241ae08745Sheppo 	if (rv != 0) {
34253af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Failed to bind LDC memory handle "
34263af08d82Slm66018 		    "(mhdl=%p, buf=%p, err=%d)\n",
34273af08d82Slm66018 		    vdcp->instance, (void *)mhdl, (void *)vaddr, rv);
34281ae08745Sheppo 		if (ldep->align_addr) {
34291ae08745Sheppo 			kmem_free(ldep->align_addr,
3430d10e4ef2Snarayan 			    sizeof (caddr_t) * P2ROUNDUP(nbytes, 8));
34311ae08745Sheppo 			ldep->align_addr = NULL;
34321ae08745Sheppo 		}
34331ae08745Sheppo 		return (EAGAIN);
34341ae08745Sheppo 	}
34351ae08745Sheppo 
34361ae08745Sheppo 	/*
34371ae08745Sheppo 	 * Get the other cookies (if any).
34381ae08745Sheppo 	 */
34391ae08745Sheppo 	for (i = 1; i < dep->payload.ncookies; i++) {
34401ae08745Sheppo 		rv = ldc_mem_nextcookie(mhdl, &dep->payload.cookie[i]);
34411ae08745Sheppo 		if (rv != 0) {
34421ae08745Sheppo 			(void) ldc_mem_unbind_handle(mhdl);
34433af08d82Slm66018 			DMSG(vdcp, 0, "?[%d] Failed to get next cookie "
3444e1ebb9ecSlm66018 			    "(mhdl=%lx cnum=%d), err=%d",
34453af08d82Slm66018 			    vdcp->instance, mhdl, i, rv);
34461ae08745Sheppo 			if (ldep->align_addr) {
34471ae08745Sheppo 				kmem_free(ldep->align_addr,
34483c96341aSnarayan 				    sizeof (caddr_t) * ldep->nbytes);
34491ae08745Sheppo 				ldep->align_addr = NULL;
34501ae08745Sheppo 			}
34511ae08745Sheppo 			return (EAGAIN);
34521ae08745Sheppo 		}
34531ae08745Sheppo 	}
34541ae08745Sheppo 
34551ae08745Sheppo 	return (rv);
34561ae08745Sheppo }
34571ae08745Sheppo 
34581ae08745Sheppo /*
34591ae08745Sheppo  * Interrupt handlers for messages from LDC
34601ae08745Sheppo  */
34611ae08745Sheppo 
34620a55fbb7Slm66018 /*
34630a55fbb7Slm66018  * Function:
34640a55fbb7Slm66018  *	vdc_handle_cb()
34650a55fbb7Slm66018  *
34660a55fbb7Slm66018  * Description:
34670a55fbb7Slm66018  *
34680a55fbb7Slm66018  * Arguments:
34690a55fbb7Slm66018  *	event	- Type of event (LDC_EVT_xxx) that triggered the callback
34700a55fbb7Slm66018  *	arg	- soft state pointer for this instance of the device driver.
34710a55fbb7Slm66018  *
34720a55fbb7Slm66018  * Return Code:
34730a55fbb7Slm66018  *	0	- Success
34740a55fbb7Slm66018  */
34751ae08745Sheppo static uint_t
34761ae08745Sheppo vdc_handle_cb(uint64_t event, caddr_t arg)
34771ae08745Sheppo {
34781ae08745Sheppo 	ldc_status_t	ldc_state;
34791ae08745Sheppo 	int		rv = 0;
34801ae08745Sheppo 
34811ae08745Sheppo 	vdc_t	*vdc = (vdc_t *)(void *)arg;
34821ae08745Sheppo 
34831ae08745Sheppo 	ASSERT(vdc != NULL);
34841ae08745Sheppo 
34853af08d82Slm66018 	DMSG(vdc, 1, "evt=%lx seqID=%ld\n", event, vdc->seq_num);
34861ae08745Sheppo 
34871ae08745Sheppo 	/*
34881ae08745Sheppo 	 * Depending on the type of event that triggered this callback,
34893af08d82Slm66018 	 * we modify the handshake state or read the data.
34901ae08745Sheppo 	 *
34911ae08745Sheppo 	 * NOTE: not done as a switch() as event could be triggered by
34921ae08745Sheppo 	 * a state change and a read request. Also the ordering	of the
34931ae08745Sheppo 	 * check for the event types is deliberate.
34941ae08745Sheppo 	 */
34951ae08745Sheppo 	if (event & LDC_EVT_UP) {
34963af08d82Slm66018 		DMSG(vdc, 0, "[%d] Received LDC_EVT_UP\n", vdc->instance);
34973af08d82Slm66018 
34983af08d82Slm66018 		mutex_enter(&vdc->lock);
34991ae08745Sheppo 
35001ae08745Sheppo 		/* get LDC state */
35011ae08745Sheppo 		rv = ldc_status(vdc->ldc_handle, &ldc_state);
35021ae08745Sheppo 		if (rv != 0) {
35033af08d82Slm66018 			DMSG(vdc, 0, "[%d] Couldn't get LDC status %d",
35041ae08745Sheppo 			    vdc->instance, rv);
35051ae08745Sheppo 			return (LDC_SUCCESS);
35061ae08745Sheppo 		}
35073af08d82Slm66018 		if (vdc->ldc_state != LDC_UP && ldc_state == LDC_UP) {
35081ae08745Sheppo 			/*
35093af08d82Slm66018 			 * Reset the transaction sequence numbers when
35103af08d82Slm66018 			 * LDC comes up. We then kick off the handshake
35113af08d82Slm66018 			 * negotiation with the vDisk server.
35121ae08745Sheppo 			 */
35130a55fbb7Slm66018 			vdc->seq_num = 1;
35141ae08745Sheppo 			vdc->seq_num_reply = 0;
35151ae08745Sheppo 			vdc->ldc_state = ldc_state;
35163af08d82Slm66018 			cv_signal(&vdc->initwait_cv);
35173af08d82Slm66018 		}
35183af08d82Slm66018 
35191ae08745Sheppo 		mutex_exit(&vdc->lock);
35201ae08745Sheppo 	}
35211ae08745Sheppo 
35221ae08745Sheppo 	if (event & LDC_EVT_READ) {
352317cadca8Slm66018 		DMSG(vdc, 1, "[%d] Received LDC_EVT_READ\n", vdc->instance);
35243af08d82Slm66018 		mutex_enter(&vdc->read_lock);
35253af08d82Slm66018 		cv_signal(&vdc->read_cv);
35263af08d82Slm66018 		vdc->read_state = VDC_READ_PENDING;
35273af08d82Slm66018 		mutex_exit(&vdc->read_lock);
35281ae08745Sheppo 
35291ae08745Sheppo 		/* that's all we have to do - no need to handle DOWN/RESET */
35301ae08745Sheppo 		return (LDC_SUCCESS);
35311ae08745Sheppo 	}
35321ae08745Sheppo 
35333af08d82Slm66018 	if (event & (LDC_EVT_RESET|LDC_EVT_DOWN)) {
35340a55fbb7Slm66018 
35353af08d82Slm66018 		DMSG(vdc, 0, "[%d] Received LDC RESET event\n", vdc->instance);
35363af08d82Slm66018 
35370a55fbb7Slm66018 		mutex_enter(&vdc->lock);
35383af08d82Slm66018 		/*
35393af08d82Slm66018 		 * Need to wake up any readers so they will
35403af08d82Slm66018 		 * detect that a reset has occurred.
35413af08d82Slm66018 		 */
35423af08d82Slm66018 		mutex_enter(&vdc->read_lock);
35433af08d82Slm66018 		if ((vdc->read_state == VDC_READ_WAITING) ||
35443af08d82Slm66018 		    (vdc->read_state == VDC_READ_RESET))
35453af08d82Slm66018 			cv_signal(&vdc->read_cv);
35463af08d82Slm66018 		vdc->read_state = VDC_READ_RESET;
35473af08d82Slm66018 		mutex_exit(&vdc->read_lock);
35480a55fbb7Slm66018 
35493af08d82Slm66018 		/* wake up any threads waiting for connection to come up */
35503af08d82Slm66018 		if (vdc->state == VDC_STATE_INIT_WAITING) {
35513af08d82Slm66018 			vdc->state = VDC_STATE_RESETTING;
35523af08d82Slm66018 			cv_signal(&vdc->initwait_cv);
35531ae08745Sheppo 		}
35541ae08745Sheppo 
35550a55fbb7Slm66018 		mutex_exit(&vdc->lock);
35561ae08745Sheppo 	}
35571ae08745Sheppo 
35581ae08745Sheppo 	if (event & ~(LDC_EVT_UP | LDC_EVT_RESET | LDC_EVT_DOWN | LDC_EVT_READ))
35593af08d82Slm66018 		DMSG(vdc, 0, "![%d] Unexpected LDC event (%lx) received",
35601ae08745Sheppo 		    vdc->instance, event);
35611ae08745Sheppo 
35621ae08745Sheppo 	return (LDC_SUCCESS);
35631ae08745Sheppo }
35641ae08745Sheppo 
35653af08d82Slm66018 /*
35663af08d82Slm66018  * Function:
35673af08d82Slm66018  *	vdc_wait_for_response()
35683af08d82Slm66018  *
35693af08d82Slm66018  * Description:
35703af08d82Slm66018  *	Block waiting for a response from the server. If there is
35713af08d82Slm66018  *	no data the thread block on the read_cv that is signalled
35723af08d82Slm66018  *	by the callback when an EVT_READ occurs.
35733af08d82Slm66018  *
35743af08d82Slm66018  * Arguments:
35753af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
35763af08d82Slm66018  *
35773af08d82Slm66018  * Return Code:
35783af08d82Slm66018  *	0	- Success
35793af08d82Slm66018  */
35803af08d82Slm66018 static int
35813af08d82Slm66018 vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp)
35823af08d82Slm66018 {
35833af08d82Slm66018 	size_t		nbytes = sizeof (*msgp);
35843af08d82Slm66018 	int		status;
35853af08d82Slm66018 
35863af08d82Slm66018 	ASSERT(vdcp != NULL);
35873af08d82Slm66018 
35883af08d82Slm66018 	DMSG(vdcp, 1, "[%d] Entered\n", vdcp->instance);
35893af08d82Slm66018 
35903af08d82Slm66018 	status = vdc_recv(vdcp, msgp, &nbytes);
35913af08d82Slm66018 	DMSG(vdcp, 3, "vdc_read() done.. status=0x%x size=0x%x\n",
35923af08d82Slm66018 	    status, (int)nbytes);
35933af08d82Slm66018 	if (status) {
35943af08d82Slm66018 		DMSG(vdcp, 0, "?[%d] Error %d reading LDC msg\n",
35953af08d82Slm66018 		    vdcp->instance, status);
35963af08d82Slm66018 		return (status);
35973af08d82Slm66018 	}
35983af08d82Slm66018 
35993af08d82Slm66018 	if (nbytes < sizeof (vio_msg_tag_t)) {
36003af08d82Slm66018 		DMSG(vdcp, 0, "?[%d] Expect %lu bytes; recv'd %lu\n",
36013af08d82Slm66018 		    vdcp->instance, sizeof (vio_msg_tag_t), nbytes);
36023af08d82Slm66018 		return (ENOMSG);
36033af08d82Slm66018 	}
36043af08d82Slm66018 
36053af08d82Slm66018 	DMSG(vdcp, 2, "[%d] (%x/%x/%x)\n", vdcp->instance,
36063af08d82Slm66018 	    msgp->tag.vio_msgtype,
36073af08d82Slm66018 	    msgp->tag.vio_subtype,
36083af08d82Slm66018 	    msgp->tag.vio_subtype_env);
36093af08d82Slm66018 
36103af08d82Slm66018 	/*
36113af08d82Slm66018 	 * Verify the Session ID of the message
36123af08d82Slm66018 	 *
36133af08d82Slm66018 	 * Every message after the Version has been negotiated should
36143af08d82Slm66018 	 * have the correct session ID set.
36153af08d82Slm66018 	 */
36163af08d82Slm66018 	if ((msgp->tag.vio_sid != vdcp->session_id) &&
36173af08d82Slm66018 	    (msgp->tag.vio_subtype_env != VIO_VER_INFO)) {
36183af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Invalid SID: received 0x%x, "
36193af08d82Slm66018 		    "expected 0x%lx [seq num %lx @ %d]",
36203af08d82Slm66018 		    vdcp->instance, msgp->tag.vio_sid,
36213af08d82Slm66018 		    vdcp->session_id,
36223af08d82Slm66018 		    ((vio_dring_msg_t *)msgp)->seq_num,
36233af08d82Slm66018 		    ((vio_dring_msg_t *)msgp)->start_idx);
36243af08d82Slm66018 		return (ENOMSG);
36253af08d82Slm66018 	}
36263af08d82Slm66018 	return (0);
36273af08d82Slm66018 }
36283af08d82Slm66018 
36293af08d82Slm66018 
36303af08d82Slm66018 /*
36313af08d82Slm66018  * Function:
36323af08d82Slm66018  *	vdc_resubmit_backup_dring()
36333af08d82Slm66018  *
36343af08d82Slm66018  * Description:
36353af08d82Slm66018  *	Resubmit each descriptor in the backed up dring to
36363af08d82Slm66018  * 	vDisk server. The Dring was backed up during connection
36373af08d82Slm66018  *	reset.
36383af08d82Slm66018  *
36393af08d82Slm66018  * Arguments:
36403af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
36413af08d82Slm66018  *
36423af08d82Slm66018  * Return Code:
36433af08d82Slm66018  *	0	- Success
36443af08d82Slm66018  */
36453af08d82Slm66018 static int
36463af08d82Slm66018 vdc_resubmit_backup_dring(vdc_t *vdcp)
36473af08d82Slm66018 {
3648*90e2f9dcSlm66018 	int		processed = 0;
36493af08d82Slm66018 	int		count;
36503af08d82Slm66018 	int		b_idx;
3651*90e2f9dcSlm66018 	int		rv = 0;
36523af08d82Slm66018 	int		dring_size;
3653*90e2f9dcSlm66018 	int		op;
36543af08d82Slm66018 	vio_msg_t	vio_msg;
36553af08d82Slm66018 	vdc_local_desc_t	*curr_ldep;
36563af08d82Slm66018 
36573af08d82Slm66018 	ASSERT(MUTEX_NOT_HELD(&vdcp->lock));
36583af08d82Slm66018 	ASSERT(vdcp->state == VDC_STATE_HANDLE_PENDING);
36593af08d82Slm66018 
3660655fd6a9Sachartre 	if (vdcp->local_dring_backup == NULL) {
3661655fd6a9Sachartre 		/* the pending requests have already been processed */
3662655fd6a9Sachartre 		return (0);
3663655fd6a9Sachartre 	}
3664655fd6a9Sachartre 
36653af08d82Slm66018 	DMSG(vdcp, 1, "restoring pending dring entries (len=%d, tail=%d)\n",
36663af08d82Slm66018 	    vdcp->local_dring_backup_len, vdcp->local_dring_backup_tail);
36673af08d82Slm66018 
36683af08d82Slm66018 	/*
36693af08d82Slm66018 	 * Walk the backup copy of the local descriptor ring and
36703af08d82Slm66018 	 * resubmit all the outstanding transactions.
36713af08d82Slm66018 	 */
36723af08d82Slm66018 	b_idx = vdcp->local_dring_backup_tail;
36733af08d82Slm66018 	for (count = 0; count < vdcp->local_dring_backup_len; count++) {
36743af08d82Slm66018 
36753af08d82Slm66018 		curr_ldep = &(vdcp->local_dring_backup[b_idx]);
36763af08d82Slm66018 
3677eff7243fSlm66018 		/* only resubmit outstanding transactions */
36783af08d82Slm66018 		if (!curr_ldep->is_free) {
3679*90e2f9dcSlm66018 			/*
3680*90e2f9dcSlm66018 			 * If we are retrying a block read/write operation we
3681*90e2f9dcSlm66018 			 * need to update the I/O statistics to indicate that
3682*90e2f9dcSlm66018 			 * the request is being put back on the waitq to be
3683*90e2f9dcSlm66018 			 * serviced (it will have been taken off after the
3684*90e2f9dcSlm66018 			 * error was reported).
3685*90e2f9dcSlm66018 			 */
3686*90e2f9dcSlm66018 			mutex_enter(&vdcp->lock);
3687*90e2f9dcSlm66018 			op = curr_ldep->operation;
3688*90e2f9dcSlm66018 			if ((op == VD_OP_BREAD) || (op == VD_OP_BWRITE)) {
3689*90e2f9dcSlm66018 				DTRACE_IO1(start, buf_t *, curr_ldep->cb_arg);
3690*90e2f9dcSlm66018 				VD_KSTAT_WAITQ_ENTER(vdcp);
3691*90e2f9dcSlm66018 			}
36923af08d82Slm66018 
36933af08d82Slm66018 			DMSG(vdcp, 1, "resubmitting entry idx=%x\n", b_idx);
3694*90e2f9dcSlm66018 			rv = vdc_populate_descriptor(vdcp, op,
36953af08d82Slm66018 			    curr_ldep->addr, curr_ldep->nbytes,
36963af08d82Slm66018 			    curr_ldep->slice, curr_ldep->offset,
36973af08d82Slm66018 			    curr_ldep->cb_type, curr_ldep->cb_arg,
36983af08d82Slm66018 			    curr_ldep->dir);
3699*90e2f9dcSlm66018 
37003af08d82Slm66018 			if (rv) {
3701*90e2f9dcSlm66018 				if (op == VD_OP_BREAD || op == VD_OP_BWRITE) {
3702*90e2f9dcSlm66018 					VD_UPDATE_ERR_STATS(vdcp, vd_transerrs);
3703*90e2f9dcSlm66018 					VD_KSTAT_WAITQ_EXIT(vdcp);
3704*90e2f9dcSlm66018 					DTRACE_IO1(done, buf_t *,
3705*90e2f9dcSlm66018 					    curr_ldep->cb_arg);
3706*90e2f9dcSlm66018 				}
37073af08d82Slm66018 				DMSG(vdcp, 1, "[%d] cannot resubmit entry %d\n",
37083af08d82Slm66018 				    vdcp->instance, b_idx);
3709*90e2f9dcSlm66018 				mutex_exit(&vdcp->lock);
3710*90e2f9dcSlm66018 				goto done;
37113af08d82Slm66018 			}
37123af08d82Slm66018 
3713*90e2f9dcSlm66018 			/*
3714*90e2f9dcSlm66018 			 * If this is a block read/write we update the I/O
3715*90e2f9dcSlm66018 			 * statistics kstat to indicate that the request
3716*90e2f9dcSlm66018 			 * has been sent back to the vDisk server and should
3717*90e2f9dcSlm66018 			 * now be put on the run queue.
3718*90e2f9dcSlm66018 			 */
3719*90e2f9dcSlm66018 			if ((op == VD_OP_BREAD) || (op == VD_OP_BWRITE)) {
3720*90e2f9dcSlm66018 				DTRACE_PROBE1(send, buf_t *, curr_ldep->cb_arg);
3721*90e2f9dcSlm66018 				VD_KSTAT_WAITQ_TO_RUNQ(vdcp);
3722*90e2f9dcSlm66018 			}
3723*90e2f9dcSlm66018 			mutex_exit(&vdcp->lock);
3724*90e2f9dcSlm66018 
37253af08d82Slm66018 			/* Wait for the response message. */
37263af08d82Slm66018 			DMSG(vdcp, 1, "waiting for response to idx=%x\n",
37273af08d82Slm66018 			    b_idx);
3728*90e2f9dcSlm66018 			rv = vdc_wait_for_response(vdcp, &vio_msg);
3729*90e2f9dcSlm66018 			if (rv) {
3730*90e2f9dcSlm66018 				/*
3731*90e2f9dcSlm66018 				 * If this is a block read/write we update
3732*90e2f9dcSlm66018 				 * the I/O statistics kstat to take it
3733*90e2f9dcSlm66018 				 * off the run queue.
3734*90e2f9dcSlm66018 				 */
3735*90e2f9dcSlm66018 				mutex_enter(&vdcp->lock);
3736*90e2f9dcSlm66018 				if (op == VD_OP_BREAD || op == VD_OP_BWRITE) {
3737*90e2f9dcSlm66018 					VD_UPDATE_ERR_STATS(vdcp, vd_transerrs);
3738*90e2f9dcSlm66018 					VD_KSTAT_RUNQ_EXIT(vdcp);
3739*90e2f9dcSlm66018 					DTRACE_IO1(done, buf_t *,
3740*90e2f9dcSlm66018 					    curr_ldep->cb_arg);
3741*90e2f9dcSlm66018 				}
37423af08d82Slm66018 				DMSG(vdcp, 1, "[%d] wait_for_response "
37433af08d82Slm66018 				    "returned err=%d\n", vdcp->instance,
3744*90e2f9dcSlm66018 				    rv);
3745*90e2f9dcSlm66018 				mutex_exit(&vdcp->lock);
3746*90e2f9dcSlm66018 				goto done;
37473af08d82Slm66018 			}
37483af08d82Slm66018 
37493af08d82Slm66018 			DMSG(vdcp, 1, "processing msg for idx=%x\n", b_idx);
3750*90e2f9dcSlm66018 			rv = vdc_process_data_msg(vdcp, &vio_msg);
3751*90e2f9dcSlm66018 			if (rv) {
37523af08d82Slm66018 				DMSG(vdcp, 1, "[%d] process_data_msg "
37533af08d82Slm66018 				    "returned err=%d\n", vdcp->instance,
3754*90e2f9dcSlm66018 				    rv);
3755*90e2f9dcSlm66018 				goto done;
37563af08d82Slm66018 			}
3757*90e2f9dcSlm66018 			processed++;
37583af08d82Slm66018 		}
37593af08d82Slm66018 
37603af08d82Slm66018 		/* get the next element to submit */
37613af08d82Slm66018 		if (++b_idx >= vdcp->local_dring_backup_len)
37623af08d82Slm66018 			b_idx = 0;
37633af08d82Slm66018 	}
37643af08d82Slm66018 
37653af08d82Slm66018 	/* all done - now clear up pending dring copy */
37663af08d82Slm66018 	dring_size = vdcp->local_dring_backup_len *
37673af08d82Slm66018 	    sizeof (vdcp->local_dring_backup[0]);
37683af08d82Slm66018 
37693af08d82Slm66018 	(void) kmem_free(vdcp->local_dring_backup, dring_size);
37703af08d82Slm66018 
37713af08d82Slm66018 	vdcp->local_dring_backup = NULL;
37723af08d82Slm66018 
3773*90e2f9dcSlm66018 done:
3774*90e2f9dcSlm66018 	DTRACE_PROBE2(processed, int, processed, vdc_t *, vdcp);
3775*90e2f9dcSlm66018 
3776*90e2f9dcSlm66018 	return (rv);
37773af08d82Slm66018 }
37783af08d82Slm66018 
37793af08d82Slm66018 /*
37803af08d82Slm66018  * Function:
3781655fd6a9Sachartre  *	vdc_cancel_backup_dring
3782655fd6a9Sachartre  *
3783655fd6a9Sachartre  * Description:
3784655fd6a9Sachartre  *	Cancel each descriptor in the backed up dring to vDisk server.
3785655fd6a9Sachartre  *	The Dring was backed up during connection reset.
3786655fd6a9Sachartre  *
3787655fd6a9Sachartre  * Arguments:
3788655fd6a9Sachartre  *	vdcp	- soft state pointer for this instance of the device driver.
3789655fd6a9Sachartre  *
3790655fd6a9Sachartre  * Return Code:
3791655fd6a9Sachartre  *	None
3792655fd6a9Sachartre  */
3793655fd6a9Sachartre void
3794*90e2f9dcSlm66018 vdc_cancel_backup_dring(vdc_t *vdcp)
3795655fd6a9Sachartre {
3796655fd6a9Sachartre 	vdc_local_desc_t *ldep;
3797655fd6a9Sachartre 	struct buf 	*bufp;
3798655fd6a9Sachartre 	int		count;
3799655fd6a9Sachartre 	int		b_idx;
3800655fd6a9Sachartre 	int		dring_size;
3801*90e2f9dcSlm66018 	int		cancelled = 0;
3802655fd6a9Sachartre 
3803655fd6a9Sachartre 	ASSERT(MUTEX_HELD(&vdcp->lock));
3804655fd6a9Sachartre 	ASSERT(vdcp->state == VDC_STATE_INIT ||
3805655fd6a9Sachartre 	    vdcp->state == VDC_STATE_INIT_WAITING ||
3806655fd6a9Sachartre 	    vdcp->state == VDC_STATE_NEGOTIATE ||
3807655fd6a9Sachartre 	    vdcp->state == VDC_STATE_RESETTING);
3808655fd6a9Sachartre 
3809655fd6a9Sachartre 	if (vdcp->local_dring_backup == NULL) {
3810655fd6a9Sachartre 		/* the pending requests have already been processed */
3811655fd6a9Sachartre 		return;
3812655fd6a9Sachartre 	}
3813655fd6a9Sachartre 
3814655fd6a9Sachartre 	DMSG(vdcp, 1, "cancelling pending dring entries (len=%d, tail=%d)\n",
3815655fd6a9Sachartre 	    vdcp->local_dring_backup_len, vdcp->local_dring_backup_tail);
3816655fd6a9Sachartre 
3817655fd6a9Sachartre 	/*
3818655fd6a9Sachartre 	 * Walk the backup copy of the local descriptor ring and
3819655fd6a9Sachartre 	 * cancel all the outstanding transactions.
3820655fd6a9Sachartre 	 */
3821655fd6a9Sachartre 	b_idx = vdcp->local_dring_backup_tail;
3822655fd6a9Sachartre 	for (count = 0; count < vdcp->local_dring_backup_len; count++) {
3823655fd6a9Sachartre 
3824655fd6a9Sachartre 		ldep = &(vdcp->local_dring_backup[b_idx]);
3825655fd6a9Sachartre 
3826655fd6a9Sachartre 		/* only cancel outstanding transactions */
3827655fd6a9Sachartre 		if (!ldep->is_free) {
3828655fd6a9Sachartre 
3829655fd6a9Sachartre 			DMSG(vdcp, 1, "cancelling entry idx=%x\n", b_idx);
3830*90e2f9dcSlm66018 			cancelled++;
3831655fd6a9Sachartre 
3832655fd6a9Sachartre 			/*
3833655fd6a9Sachartre 			 * All requests have already been cleared from the
3834655fd6a9Sachartre 			 * local descriptor ring and the LDC channel has been
3835655fd6a9Sachartre 			 * reset so we will never get any reply for these
3836655fd6a9Sachartre 			 * requests. Now we just have to notify threads waiting
3837655fd6a9Sachartre 			 * for replies that the request has failed.
3838655fd6a9Sachartre 			 */
3839655fd6a9Sachartre 			switch (ldep->cb_type) {
3840655fd6a9Sachartre 			case CB_SYNC:
3841655fd6a9Sachartre 				ASSERT(vdcp->sync_op_pending);
3842655fd6a9Sachartre 				vdcp->sync_op_status = EIO;
3843655fd6a9Sachartre 				vdcp->sync_op_pending = B_FALSE;
3844655fd6a9Sachartre 				cv_signal(&vdcp->sync_pending_cv);
3845655fd6a9Sachartre 				break;
3846655fd6a9Sachartre 
3847655fd6a9Sachartre 			case CB_STRATEGY:
3848655fd6a9Sachartre 				bufp = ldep->cb_arg;
3849655fd6a9Sachartre 				ASSERT(bufp != NULL);
3850655fd6a9Sachartre 				bufp->b_resid = bufp->b_bcount;
3851366a92acSlm66018 				VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
3852*90e2f9dcSlm66018 				VD_KSTAT_RUNQ_EXIT(vdcp);
3853366a92acSlm66018 				DTRACE_IO1(done, buf_t *, bufp);
3854655fd6a9Sachartre 				bioerror(bufp, EIO);
3855655fd6a9Sachartre 				biodone(bufp);
3856655fd6a9Sachartre 				break;
3857655fd6a9Sachartre 
3858655fd6a9Sachartre 			default:
3859655fd6a9Sachartre 				ASSERT(0);
3860655fd6a9Sachartre 			}
3861655fd6a9Sachartre 
3862655fd6a9Sachartre 		}
3863655fd6a9Sachartre 
3864655fd6a9Sachartre 		/* get the next element to cancel */
3865655fd6a9Sachartre 		if (++b_idx >= vdcp->local_dring_backup_len)
3866655fd6a9Sachartre 			b_idx = 0;
3867655fd6a9Sachartre 	}
3868655fd6a9Sachartre 
3869655fd6a9Sachartre 	/* all done - now clear up pending dring copy */
3870655fd6a9Sachartre 	dring_size = vdcp->local_dring_backup_len *
3871655fd6a9Sachartre 	    sizeof (vdcp->local_dring_backup[0]);
3872655fd6a9Sachartre 
3873655fd6a9Sachartre 	(void) kmem_free(vdcp->local_dring_backup, dring_size);
3874655fd6a9Sachartre 
3875655fd6a9Sachartre 	vdcp->local_dring_backup = NULL;
3876655fd6a9Sachartre 
3877*90e2f9dcSlm66018 	DTRACE_PROBE2(cancelled, int, cancelled, vdc_t *, vdcp);
3878655fd6a9Sachartre }
3879655fd6a9Sachartre 
3880655fd6a9Sachartre /*
3881655fd6a9Sachartre  * Function:
3882655fd6a9Sachartre  *	vdc_connection_timeout
3883655fd6a9Sachartre  *
3884655fd6a9Sachartre  * Description:
3885655fd6a9Sachartre  *	This function is invoked if the timeout set to establish the connection
3886655fd6a9Sachartre  *	with vds expires. This will happen if we spend too much time in the
3887655fd6a9Sachartre  *	VDC_STATE_INIT_WAITING or VDC_STATE_NEGOTIATE states. Then we will
3888655fd6a9Sachartre  *	cancel any pending request and mark them as failed.
3889655fd6a9Sachartre  *
3890655fd6a9Sachartre  *	If the timeout does not expire, it will be cancelled when we reach the
3891655fd6a9Sachartre  *	VDC_STATE_HANDLE_PENDING or VDC_STATE_RESETTING state. This function can
3892655fd6a9Sachartre  *	be invoked while we are in the VDC_STATE_HANDLE_PENDING or
3893655fd6a9Sachartre  *	VDC_STATE_RESETTING state in which case we do nothing because the
3894655fd6a9Sachartre  *	timeout is being cancelled.
3895655fd6a9Sachartre  *
3896655fd6a9Sachartre  * Arguments:
3897655fd6a9Sachartre  *	arg	- argument of the timeout function actually a soft state
3898655fd6a9Sachartre  *		  pointer for the instance of the device driver.
3899655fd6a9Sachartre  *
3900655fd6a9Sachartre  * Return Code:
3901655fd6a9Sachartre  *	None
3902655fd6a9Sachartre  */
3903655fd6a9Sachartre void
3904655fd6a9Sachartre vdc_connection_timeout(void *arg)
3905655fd6a9Sachartre {
3906655fd6a9Sachartre 	vdc_t 		*vdcp = (vdc_t *)arg;
3907655fd6a9Sachartre 
3908655fd6a9Sachartre 	mutex_enter(&vdcp->lock);
3909655fd6a9Sachartre 
3910655fd6a9Sachartre 	if (vdcp->state == VDC_STATE_HANDLE_PENDING ||
3911655fd6a9Sachartre 	    vdcp->state == VDC_STATE_DETACH) {
3912655fd6a9Sachartre 		/*
3913655fd6a9Sachartre 		 * The connection has just been re-established or
3914655fd6a9Sachartre 		 * we are detaching.
3915655fd6a9Sachartre 		 */
3916655fd6a9Sachartre 		vdcp->ctimeout_reached = B_FALSE;
3917655fd6a9Sachartre 		mutex_exit(&vdcp->lock);
3918655fd6a9Sachartre 		return;
3919655fd6a9Sachartre 	}
3920655fd6a9Sachartre 
3921655fd6a9Sachartre 	vdcp->ctimeout_reached = B_TRUE;
3922655fd6a9Sachartre 
3923655fd6a9Sachartre 	/* notify requests waiting for sending */
3924655fd6a9Sachartre 	cv_broadcast(&vdcp->running_cv);
3925655fd6a9Sachartre 
3926655fd6a9Sachartre 	/* cancel requests waiting for a result */
3927*90e2f9dcSlm66018 	vdc_cancel_backup_dring(vdcp);
3928655fd6a9Sachartre 
3929655fd6a9Sachartre 	mutex_exit(&vdcp->lock);
3930655fd6a9Sachartre 
3931655fd6a9Sachartre 	cmn_err(CE_NOTE, "[%d] connection to service domain timeout",
3932655fd6a9Sachartre 	    vdcp->instance);
3933655fd6a9Sachartre }
3934655fd6a9Sachartre 
3935655fd6a9Sachartre /*
3936655fd6a9Sachartre  * Function:
39373af08d82Slm66018  *	vdc_backup_local_dring()
39383af08d82Slm66018  *
39393af08d82Slm66018  * Description:
39403af08d82Slm66018  *	Backup the current dring in the event of a reset. The Dring
39413af08d82Slm66018  *	transactions will be resubmitted to the server when the
39423af08d82Slm66018  *	connection is restored.
39433af08d82Slm66018  *
39443af08d82Slm66018  * Arguments:
39453af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
39463af08d82Slm66018  *
39473af08d82Slm66018  * Return Code:
39483af08d82Slm66018  *	NONE
39493af08d82Slm66018  */
39503af08d82Slm66018 static void
39513af08d82Slm66018 vdc_backup_local_dring(vdc_t *vdcp)
39523af08d82Slm66018 {
39533af08d82Slm66018 	int dring_size;
39543af08d82Slm66018 
3955655fd6a9Sachartre 	ASSERT(MUTEX_HELD(&vdcp->lock));
39563af08d82Slm66018 	ASSERT(vdcp->state == VDC_STATE_RESETTING);
39573af08d82Slm66018 
39583af08d82Slm66018 	/*
39593af08d82Slm66018 	 * If the backup dring is stil around, it means
39603af08d82Slm66018 	 * that the last restore did not complete. However,
39613af08d82Slm66018 	 * since we never got back into the running state,
39623af08d82Slm66018 	 * the backup copy we have is still valid.
39633af08d82Slm66018 	 */
39643af08d82Slm66018 	if (vdcp->local_dring_backup != NULL) {
39653af08d82Slm66018 		DMSG(vdcp, 1, "reusing local descriptor ring backup "
39663af08d82Slm66018 		    "(len=%d, tail=%d)\n", vdcp->local_dring_backup_len,
39673af08d82Slm66018 		    vdcp->local_dring_backup_tail);
39683af08d82Slm66018 		return;
39693af08d82Slm66018 	}
39703af08d82Slm66018 
3971655fd6a9Sachartre 	/*
3972655fd6a9Sachartre 	 * The backup dring can be NULL and the local dring may not be
3973655fd6a9Sachartre 	 * initialized. This can happen if we had a reset while establishing
3974655fd6a9Sachartre 	 * a new connection but after the connection has timed out. In that
3975655fd6a9Sachartre 	 * case the backup dring is NULL because the requests have been
3976655fd6a9Sachartre 	 * cancelled and the request occured before the local dring is
3977655fd6a9Sachartre 	 * initialized.
3978655fd6a9Sachartre 	 */
3979655fd6a9Sachartre 	if (!(vdcp->initialized & VDC_DRING_LOCAL))
3980655fd6a9Sachartre 		return;
3981655fd6a9Sachartre 
39823af08d82Slm66018 	DMSG(vdcp, 1, "backing up the local descriptor ring (len=%d, "
39833af08d82Slm66018 	    "tail=%d)\n", vdcp->dring_len, vdcp->dring_curr_idx);
39843af08d82Slm66018 
39853af08d82Slm66018 	dring_size = vdcp->dring_len * sizeof (vdcp->local_dring[0]);
39863af08d82Slm66018 
39873af08d82Slm66018 	vdcp->local_dring_backup = kmem_alloc(dring_size, KM_SLEEP);
39883af08d82Slm66018 	bcopy(vdcp->local_dring, vdcp->local_dring_backup, dring_size);
39893af08d82Slm66018 
39903af08d82Slm66018 	vdcp->local_dring_backup_tail = vdcp->dring_curr_idx;
39913af08d82Slm66018 	vdcp->local_dring_backup_len = vdcp->dring_len;
39923af08d82Slm66018 }
39933af08d82Slm66018 
39941ae08745Sheppo /* -------------------------------------------------------------------------- */
39951ae08745Sheppo 
39961ae08745Sheppo /*
39971ae08745Sheppo  * The following functions process the incoming messages from vds
39981ae08745Sheppo  */
39991ae08745Sheppo 
40000a55fbb7Slm66018 /*
40010a55fbb7Slm66018  * Function:
40020a55fbb7Slm66018  *      vdc_process_msg_thread()
40030a55fbb7Slm66018  *
40040a55fbb7Slm66018  * Description:
40050a55fbb7Slm66018  *
40063af08d82Slm66018  *	Main VDC message processing thread. Each vDisk instance
40073af08d82Slm66018  * 	consists of a copy of this thread. This thread triggers
40083af08d82Slm66018  * 	all the handshakes and data exchange with the server. It
40093af08d82Slm66018  * 	also handles all channel resets
40103af08d82Slm66018  *
40110a55fbb7Slm66018  * Arguments:
40120a55fbb7Slm66018  *      vdc     - soft state pointer for this instance of the device driver.
40130a55fbb7Slm66018  *
40140a55fbb7Slm66018  * Return Code:
40150a55fbb7Slm66018  *      None
40160a55fbb7Slm66018  */
40171ae08745Sheppo static void
40183af08d82Slm66018 vdc_process_msg_thread(vdc_t *vdcp)
40191ae08745Sheppo {
40201ae08745Sheppo 	int	status;
4021655fd6a9Sachartre 	int	ctimeout;
4022655fd6a9Sachartre 	timeout_id_t tmid = 0;
40231ae08745Sheppo 
40243af08d82Slm66018 	mutex_enter(&vdcp->lock);
40251ae08745Sheppo 
40261ae08745Sheppo 	for (;;) {
40271ae08745Sheppo 
40283af08d82Slm66018 #define	Q(_s)	(vdcp->state == _s) ? #_s :
40293af08d82Slm66018 		DMSG(vdcp, 3, "state = %d (%s)\n", vdcp->state,
40303af08d82Slm66018 		    Q(VDC_STATE_INIT)
40313af08d82Slm66018 		    Q(VDC_STATE_INIT_WAITING)
40323af08d82Slm66018 		    Q(VDC_STATE_NEGOTIATE)
40333af08d82Slm66018 		    Q(VDC_STATE_HANDLE_PENDING)
40343af08d82Slm66018 		    Q(VDC_STATE_RUNNING)
40353af08d82Slm66018 		    Q(VDC_STATE_RESETTING)
40363af08d82Slm66018 		    Q(VDC_STATE_DETACH)
40373af08d82Slm66018 		    "UNKNOWN");
40381ae08745Sheppo 
40393af08d82Slm66018 		switch (vdcp->state) {
40403af08d82Slm66018 		case VDC_STATE_INIT:
40413af08d82Slm66018 
4042655fd6a9Sachartre 			/*
4043655fd6a9Sachartre 			 * If requested, start a timeout to check if the
4044655fd6a9Sachartre 			 * connection with vds is established in the
4045655fd6a9Sachartre 			 * specified delay. If the timeout expires, we
4046655fd6a9Sachartre 			 * will cancel any pending request.
4047655fd6a9Sachartre 			 *
4048655fd6a9Sachartre 			 * If some reset have occurred while establishing
4049655fd6a9Sachartre 			 * the connection, we already have a timeout armed
4050655fd6a9Sachartre 			 * and in that case we don't need to arm a new one.
4051655fd6a9Sachartre 			 */
4052655fd6a9Sachartre 			ctimeout = (vdc_timeout != 0)?
4053655fd6a9Sachartre 			    vdc_timeout : vdcp->ctimeout;
4054655fd6a9Sachartre 
4055655fd6a9Sachartre 			if (ctimeout != 0 && tmid == 0) {
4056655fd6a9Sachartre 				tmid = timeout(vdc_connection_timeout, vdcp,
4057655fd6a9Sachartre 				    ctimeout * drv_usectohz(1000000));
4058655fd6a9Sachartre 			}
4059655fd6a9Sachartre 
40603af08d82Slm66018 			/* Check if have re-initializing repeatedly */
4061655fd6a9Sachartre 			if (vdcp->hshake_cnt++ > vdc_hshake_retries &&
4062655fd6a9Sachartre 			    vdcp->lifecycle != VDC_LC_ONLINE) {
40633c96341aSnarayan 				cmn_err(CE_NOTE, "[%d] disk access failed.\n",
40643c96341aSnarayan 				    vdcp->instance);
40653af08d82Slm66018 				vdcp->state = VDC_STATE_DETACH;
40663af08d82Slm66018 				break;
40673af08d82Slm66018 			}
40683af08d82Slm66018 
40693af08d82Slm66018 			/* Bring up connection with vds via LDC */
40703af08d82Slm66018 			status = vdc_start_ldc_connection(vdcp);
4071655fd6a9Sachartre 			if (status == EINVAL) {
40723af08d82Slm66018 				DMSG(vdcp, 0, "[%d] Could not start LDC",
40733af08d82Slm66018 				    vdcp->instance);
40743af08d82Slm66018 				vdcp->state = VDC_STATE_DETACH;
4075655fd6a9Sachartre 			} else {
40763af08d82Slm66018 				vdcp->state = VDC_STATE_INIT_WAITING;
40773af08d82Slm66018 			}
40783af08d82Slm66018 			break;
40793af08d82Slm66018 
40803af08d82Slm66018 		case VDC_STATE_INIT_WAITING:
40813af08d82Slm66018 
40823af08d82Slm66018 			/*
40833af08d82Slm66018 			 * Let the callback event move us on
40843af08d82Slm66018 			 * when channel is open to server
40853af08d82Slm66018 			 */
40863af08d82Slm66018 			while (vdcp->ldc_state != LDC_UP) {
40873af08d82Slm66018 				cv_wait(&vdcp->initwait_cv, &vdcp->lock);
40883af08d82Slm66018 				if (vdcp->state != VDC_STATE_INIT_WAITING) {
40893af08d82Slm66018 					DMSG(vdcp, 0,
40903af08d82Slm66018 				"state moved to %d out from under us...\n",
40913af08d82Slm66018 					    vdcp->state);
40923af08d82Slm66018 
40933af08d82Slm66018 					break;
40943af08d82Slm66018 				}
40953af08d82Slm66018 			}
40963af08d82Slm66018 			if (vdcp->state == VDC_STATE_INIT_WAITING &&
40973af08d82Slm66018 			    vdcp->ldc_state == LDC_UP) {
40983af08d82Slm66018 				vdcp->state = VDC_STATE_NEGOTIATE;
40993af08d82Slm66018 			}
41003af08d82Slm66018 			break;
41013af08d82Slm66018 
41023af08d82Slm66018 		case VDC_STATE_NEGOTIATE:
41033af08d82Slm66018 			switch (status = vdc_ver_negotiation(vdcp)) {
41043af08d82Slm66018 			case 0:
41053af08d82Slm66018 				break;
41063af08d82Slm66018 			default:
41073af08d82Slm66018 				DMSG(vdcp, 0, "ver negotiate failed (%d)..\n",
41083af08d82Slm66018 				    status);
41093af08d82Slm66018 				goto reset;
41103af08d82Slm66018 			}
41113af08d82Slm66018 
41123af08d82Slm66018 			switch (status = vdc_attr_negotiation(vdcp)) {
41133af08d82Slm66018 			case 0:
41143af08d82Slm66018 				break;
41153af08d82Slm66018 			default:
41163af08d82Slm66018 				DMSG(vdcp, 0, "attr negotiate failed (%d)..\n",
41173af08d82Slm66018 				    status);
41183af08d82Slm66018 				goto reset;
41193af08d82Slm66018 			}
41203af08d82Slm66018 
41213af08d82Slm66018 			switch (status = vdc_dring_negotiation(vdcp)) {
41223af08d82Slm66018 			case 0:
41233af08d82Slm66018 				break;
41243af08d82Slm66018 			default:
41253af08d82Slm66018 				DMSG(vdcp, 0, "dring negotiate failed (%d)..\n",
41263af08d82Slm66018 				    status);
41273af08d82Slm66018 				goto reset;
41283af08d82Slm66018 			}
41293af08d82Slm66018 
41303af08d82Slm66018 			switch (status = vdc_rdx_exchange(vdcp)) {
41313af08d82Slm66018 			case 0:
41323af08d82Slm66018 				vdcp->state = VDC_STATE_HANDLE_PENDING;
41333af08d82Slm66018 				goto done;
41343af08d82Slm66018 			default:
41353af08d82Slm66018 				DMSG(vdcp, 0, "RDX xchg failed ..(%d)\n",
41363af08d82Slm66018 				    status);
41373af08d82Slm66018 				goto reset;
41383af08d82Slm66018 			}
41393af08d82Slm66018 reset:
41403af08d82Slm66018 			DMSG(vdcp, 0, "negotiation failed: resetting (%d)\n",
41413af08d82Slm66018 			    status);
41423af08d82Slm66018 			vdcp->state = VDC_STATE_RESETTING;
4143655fd6a9Sachartre 			vdcp->self_reset = B_TRUE;
41443af08d82Slm66018 done:
41453af08d82Slm66018 			DMSG(vdcp, 0, "negotiation complete (state=0x%x)...\n",
41463af08d82Slm66018 			    vdcp->state);
41473af08d82Slm66018 			break;
41483af08d82Slm66018 
41493af08d82Slm66018 		case VDC_STATE_HANDLE_PENDING:
41503af08d82Slm66018 
4151655fd6a9Sachartre 			if (vdcp->ctimeout_reached) {
4152655fd6a9Sachartre 				/*
4153655fd6a9Sachartre 				 * The connection timeout had been reached so
4154655fd6a9Sachartre 				 * pending requests have been cancelled. Now
4155655fd6a9Sachartre 				 * that the connection is back we can reset
4156655fd6a9Sachartre 				 * the timeout.
4157655fd6a9Sachartre 				 */
4158655fd6a9Sachartre 				ASSERT(vdcp->local_dring_backup == NULL);
4159655fd6a9Sachartre 				ASSERT(tmid != 0);
4160655fd6a9Sachartre 				tmid = 0;
4161655fd6a9Sachartre 				vdcp->ctimeout_reached = B_FALSE;
4162655fd6a9Sachartre 				vdcp->state = VDC_STATE_RUNNING;
4163655fd6a9Sachartre 				DMSG(vdcp, 0, "[%d] connection to service "
4164655fd6a9Sachartre 				    "domain is up", vdcp->instance);
4165655fd6a9Sachartre 				break;
4166655fd6a9Sachartre 			}
4167655fd6a9Sachartre 
41683af08d82Slm66018 			mutex_exit(&vdcp->lock);
4169655fd6a9Sachartre 			if (tmid != 0) {
4170655fd6a9Sachartre 				(void) untimeout(tmid);
4171655fd6a9Sachartre 				tmid = 0;
4172655fd6a9Sachartre 			}
41733af08d82Slm66018 			status = vdc_resubmit_backup_dring(vdcp);
41743af08d82Slm66018 			mutex_enter(&vdcp->lock);
41753af08d82Slm66018 
41763af08d82Slm66018 			if (status)
41773af08d82Slm66018 				vdcp->state = VDC_STATE_RESETTING;
41783af08d82Slm66018 			else
41793af08d82Slm66018 				vdcp->state = VDC_STATE_RUNNING;
41803af08d82Slm66018 
41813af08d82Slm66018 			break;
41823af08d82Slm66018 
41833af08d82Slm66018 		/* enter running state */
41843af08d82Slm66018 		case VDC_STATE_RUNNING:
41853af08d82Slm66018 			/*
41863af08d82Slm66018 			 * Signal anyone waiting for the connection
41873af08d82Slm66018 			 * to come on line.
41883af08d82Slm66018 			 */
41893af08d82Slm66018 			vdcp->hshake_cnt = 0;
41903af08d82Slm66018 			cv_broadcast(&vdcp->running_cv);
41912f5224aeSachartre 
41922f5224aeSachartre 			/* failfast has to been checked after reset */
41932f5224aeSachartre 			cv_signal(&vdcp->failfast_cv);
41942f5224aeSachartre 
41952f5224aeSachartre 			/* ownership is lost during reset */
41962f5224aeSachartre 			if (vdcp->ownership & VDC_OWNERSHIP_WANTED)
41972f5224aeSachartre 				vdcp->ownership |= VDC_OWNERSHIP_RESET;
41982f5224aeSachartre 			cv_signal(&vdcp->ownership_cv);
41992f5224aeSachartre 
42003af08d82Slm66018 			mutex_exit(&vdcp->lock);
42013af08d82Slm66018 
42023af08d82Slm66018 			for (;;) {
42033af08d82Slm66018 				vio_msg_t msg;
42043af08d82Slm66018 				status = vdc_wait_for_response(vdcp, &msg);
42053af08d82Slm66018 				if (status) break;
42063af08d82Slm66018 
42073af08d82Slm66018 				DMSG(vdcp, 1, "[%d] new pkt(s) available\n",
42083af08d82Slm66018 				    vdcp->instance);
42093af08d82Slm66018 				status = vdc_process_data_msg(vdcp, &msg);
42101ae08745Sheppo 				if (status) {
42113af08d82Slm66018 					DMSG(vdcp, 1, "[%d] process_data_msg "
42123af08d82Slm66018 					    "returned err=%d\n", vdcp->instance,
42133af08d82Slm66018 					    status);
42141ae08745Sheppo 					break;
42151ae08745Sheppo 				}
42161ae08745Sheppo 
42173af08d82Slm66018 			}
4218e1ebb9ecSlm66018 
42193af08d82Slm66018 			mutex_enter(&vdcp->lock);
42203af08d82Slm66018 
42213af08d82Slm66018 			vdcp->state = VDC_STATE_RESETTING;
4222690555a1Sachartre 			vdcp->self_reset = B_TRUE;
42233af08d82Slm66018 			break;
42243af08d82Slm66018 
42253af08d82Slm66018 		case VDC_STATE_RESETTING:
4226655fd6a9Sachartre 			/*
4227655fd6a9Sachartre 			 * When we reach this state, we either come from the
4228655fd6a9Sachartre 			 * VDC_STATE_RUNNING state and we can have pending
4229655fd6a9Sachartre 			 * request but no timeout is armed; or we come from
4230655fd6a9Sachartre 			 * the VDC_STATE_INIT_WAITING, VDC_NEGOTIATE or
4231655fd6a9Sachartre 			 * VDC_HANDLE_PENDING state and there is no pending
4232655fd6a9Sachartre 			 * request or pending requests have already been copied
4233655fd6a9Sachartre 			 * into the backup dring. So we can safely keep the
4234655fd6a9Sachartre 			 * connection timeout armed while we are in this state.
4235655fd6a9Sachartre 			 */
4236655fd6a9Sachartre 
42373af08d82Slm66018 			DMSG(vdcp, 0, "Initiating channel reset "
42383af08d82Slm66018 			    "(pending = %d)\n", (int)vdcp->threads_pending);
42393af08d82Slm66018 
42403af08d82Slm66018 			if (vdcp->self_reset) {
42413af08d82Slm66018 				DMSG(vdcp, 0,
42423af08d82Slm66018 				    "[%d] calling stop_ldc_connection.\n",
42433af08d82Slm66018 				    vdcp->instance);
42443af08d82Slm66018 				status = vdc_stop_ldc_connection(vdcp);
42453af08d82Slm66018 				vdcp->self_reset = B_FALSE;
42461ae08745Sheppo 			}
42471ae08745Sheppo 
42481ae08745Sheppo 			/*
42493af08d82Slm66018 			 * Wait for all threads currently waiting
42503af08d82Slm66018 			 * for a free dring entry to use.
42511ae08745Sheppo 			 */
42523af08d82Slm66018 			while (vdcp->threads_pending) {
42533af08d82Slm66018 				cv_broadcast(&vdcp->membind_cv);
42543af08d82Slm66018 				cv_broadcast(&vdcp->dring_free_cv);
42553af08d82Slm66018 				mutex_exit(&vdcp->lock);
4256205eeb1aSlm66018 				/* give the waiters enough time to wake up */
4257205eeb1aSlm66018 				delay(vdc_hz_min_ldc_delay);
42583af08d82Slm66018 				mutex_enter(&vdcp->lock);
42591ae08745Sheppo 			}
42601ae08745Sheppo 
42613af08d82Slm66018 			ASSERT(vdcp->threads_pending == 0);
42621ae08745Sheppo 
42633af08d82Slm66018 			/* Sanity check that no thread is receiving */
42643af08d82Slm66018 			ASSERT(vdcp->read_state != VDC_READ_WAITING);
42650a55fbb7Slm66018 
42663af08d82Slm66018 			vdcp->read_state = VDC_READ_IDLE;
42673af08d82Slm66018 
42683af08d82Slm66018 			vdc_backup_local_dring(vdcp);
42693af08d82Slm66018 
42703af08d82Slm66018 			/* cleanup the old d-ring */
42713af08d82Slm66018 			vdc_destroy_descriptor_ring(vdcp);
42723af08d82Slm66018 
42733af08d82Slm66018 			/* go and start again */
42743af08d82Slm66018 			vdcp->state = VDC_STATE_INIT;
42753af08d82Slm66018 
42760a55fbb7Slm66018 			break;
42770a55fbb7Slm66018 
42783af08d82Slm66018 		case VDC_STATE_DETACH:
42793af08d82Slm66018 			DMSG(vdcp, 0, "[%d] Reset thread exit cleanup ..\n",
42803af08d82Slm66018 			    vdcp->instance);
42813af08d82Slm66018 
4282655fd6a9Sachartre 			/* cancel any pending timeout */
4283655fd6a9Sachartre 			mutex_exit(&vdcp->lock);
4284655fd6a9Sachartre 			if (tmid != 0) {
4285655fd6a9Sachartre 				(void) untimeout(tmid);
4286655fd6a9Sachartre 				tmid = 0;
4287655fd6a9Sachartre 			}
4288655fd6a9Sachartre 			mutex_enter(&vdcp->lock);
4289655fd6a9Sachartre 
42903c96341aSnarayan 			/*
42913c96341aSnarayan 			 * Signal anyone waiting for connection
42923c96341aSnarayan 			 * to come online
42933c96341aSnarayan 			 */
42943c96341aSnarayan 			cv_broadcast(&vdcp->running_cv);
42953c96341aSnarayan 
42963af08d82Slm66018 			while (vdcp->sync_op_pending) {
42973af08d82Slm66018 				cv_signal(&vdcp->sync_pending_cv);
42983af08d82Slm66018 				cv_signal(&vdcp->sync_blocked_cv);
42993af08d82Slm66018 				mutex_exit(&vdcp->lock);
4300205eeb1aSlm66018 				/* give the waiters enough time to wake up */
4301205eeb1aSlm66018 				delay(vdc_hz_min_ldc_delay);
43023af08d82Slm66018 				mutex_enter(&vdcp->lock);
43030a55fbb7Slm66018 			}
43041ae08745Sheppo 
43053af08d82Slm66018 			mutex_exit(&vdcp->lock);
43063af08d82Slm66018 
43073af08d82Slm66018 			DMSG(vdcp, 0, "[%d] Msg processing thread exiting ..\n",
43083af08d82Slm66018 			    vdcp->instance);
43093af08d82Slm66018 			thread_exit();
43103af08d82Slm66018 			break;
43113af08d82Slm66018 		}
43123af08d82Slm66018 	}
43130a55fbb7Slm66018 }
43140a55fbb7Slm66018 
43150a55fbb7Slm66018 
43160a55fbb7Slm66018 /*
43170a55fbb7Slm66018  * Function:
43180a55fbb7Slm66018  *	vdc_process_data_msg()
43190a55fbb7Slm66018  *
43200a55fbb7Slm66018  * Description:
43210a55fbb7Slm66018  *	This function is called by the message processing thread each time
43220a55fbb7Slm66018  *	a message with a msgtype of VIO_TYPE_DATA is received. It will either
43230a55fbb7Slm66018  *	be an ACK or NACK from vds[1] which vdc handles as follows.
43240a55fbb7Slm66018  *		ACK	- wake up the waiting thread
43250a55fbb7Slm66018  *		NACK	- resend any messages necessary
43260a55fbb7Slm66018  *
43270a55fbb7Slm66018  *	[1] Although the message format allows it, vds should not send a
43280a55fbb7Slm66018  *	    VIO_SUBTYPE_INFO message to vdc asking it to read data; if for
43290a55fbb7Slm66018  *	    some bizarre reason it does, vdc will reset the connection.
43300a55fbb7Slm66018  *
43310a55fbb7Slm66018  * Arguments:
43320a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
43330a55fbb7Slm66018  *	msg	- the LDC message sent by vds
43340a55fbb7Slm66018  *
43350a55fbb7Slm66018  * Return Code:
43360a55fbb7Slm66018  *	0	- Success.
43370a55fbb7Slm66018  *	> 0	- error value returned by LDC
43380a55fbb7Slm66018  */
43390a55fbb7Slm66018 static int
43403af08d82Slm66018 vdc_process_data_msg(vdc_t *vdcp, vio_msg_t *msg)
43410a55fbb7Slm66018 {
43420a55fbb7Slm66018 	int			status = 0;
43433af08d82Slm66018 	vio_dring_msg_t		*dring_msg;
4344d10e4ef2Snarayan 	vdc_local_desc_t	*ldep = NULL;
43453af08d82Slm66018 	int			start, end;
43463af08d82Slm66018 	int			idx;
4347*90e2f9dcSlm66018 	int			op;
43480a55fbb7Slm66018 
43493af08d82Slm66018 	dring_msg = (vio_dring_msg_t *)msg;
43500a55fbb7Slm66018 
43513af08d82Slm66018 	ASSERT(msg->tag.vio_msgtype == VIO_TYPE_DATA);
43523af08d82Slm66018 	ASSERT(vdcp != NULL);
43533af08d82Slm66018 
43543af08d82Slm66018 	mutex_enter(&vdcp->lock);
43550a55fbb7Slm66018 
43560a55fbb7Slm66018 	/*
43570a55fbb7Slm66018 	 * Check to see if the message has bogus data
43580a55fbb7Slm66018 	 */
4359e1ebb9ecSlm66018 	idx = start = dring_msg->start_idx;
43600a55fbb7Slm66018 	end = dring_msg->end_idx;
43613af08d82Slm66018 	if ((start >= vdcp->dring_len) ||
43623af08d82Slm66018 	    (end >= vdcp->dring_len) || (end < -1)) {
4363*90e2f9dcSlm66018 		/*
4364*90e2f9dcSlm66018 		 * Update the I/O statistics to indicate that an error ocurred.
4365*90e2f9dcSlm66018 		 * No need to update the wait/run queues as no specific read or
4366*90e2f9dcSlm66018 		 * write request is being completed in response to this 'msg'.
4367*90e2f9dcSlm66018 		 */
4368*90e2f9dcSlm66018 		VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
43693af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Bogus ACK data : start %d, end %d\n",
43703af08d82Slm66018 		    vdcp->instance, start, end);
43713af08d82Slm66018 		mutex_exit(&vdcp->lock);
4372e1ebb9ecSlm66018 		return (EINVAL);
43730a55fbb7Slm66018 	}
43740a55fbb7Slm66018 
43750a55fbb7Slm66018 	/*
43760a55fbb7Slm66018 	 * Verify that the sequence number is what vdc expects.
43770a55fbb7Slm66018 	 */
43783af08d82Slm66018 	switch (vdc_verify_seq_num(vdcp, dring_msg)) {
4379e1ebb9ecSlm66018 	case VDC_SEQ_NUM_TODO:
4380e1ebb9ecSlm66018 		break;	/* keep processing this message */
4381e1ebb9ecSlm66018 	case VDC_SEQ_NUM_SKIP:
43823af08d82Slm66018 		mutex_exit(&vdcp->lock);
4383e1ebb9ecSlm66018 		return (0);
4384e1ebb9ecSlm66018 	case VDC_SEQ_NUM_INVALID:
4385*90e2f9dcSlm66018 		/*
4386*90e2f9dcSlm66018 		 * Update the I/O statistics to indicate that an error ocurred.
4387*90e2f9dcSlm66018 		 * No need to update the wait/run queues as no specific read or
4388*90e2f9dcSlm66018 		 * write request is being completed in response to this 'msg'.
4389*90e2f9dcSlm66018 		 */
4390366a92acSlm66018 		VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
4391*90e2f9dcSlm66018 		DMSG(vdcp, 0, "[%d] invalid seqno\n", vdcp->instance);
4392366a92acSlm66018 		mutex_exit(&vdcp->lock);
43930a55fbb7Slm66018 		return (ENXIO);
43940a55fbb7Slm66018 	}
43950a55fbb7Slm66018 
43963af08d82Slm66018 	if (msg->tag.vio_subtype == VIO_SUBTYPE_NACK) {
4397*90e2f9dcSlm66018 		/*
4398*90e2f9dcSlm66018 		 * Update the I/O statistics to indicate that an error ocurred.
4399*90e2f9dcSlm66018 		 *
4400*90e2f9dcSlm66018 		 * We need to update the run queue if a read or write request
4401*90e2f9dcSlm66018 		 * is being NACKed - otherwise there will appear to be an
4402*90e2f9dcSlm66018 		 * indefinite outstanding request and statistics reported by
4403*90e2f9dcSlm66018 		 * iostat(1M) will be incorrect. The transaction will be
4404*90e2f9dcSlm66018 		 * resubmitted from the backup DRing following the reset
4405*90e2f9dcSlm66018 		 * and the wait/run queues will be entered again.
4406*90e2f9dcSlm66018 		 */
4407*90e2f9dcSlm66018 		ldep = &vdcp->local_dring[idx];
4408*90e2f9dcSlm66018 		op = ldep->operation;
4409*90e2f9dcSlm66018 		if ((op == VD_OP_BREAD) || (op == VD_OP_BWRITE)) {
4410*90e2f9dcSlm66018 			DTRACE_IO1(done, buf_t *, ldep->cb_arg);
4411*90e2f9dcSlm66018 			VD_KSTAT_RUNQ_EXIT(vdcp);
4412*90e2f9dcSlm66018 		}
4413366a92acSlm66018 		VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
4414*90e2f9dcSlm66018 		VDC_DUMP_DRING_MSG(dring_msg);
4415*90e2f9dcSlm66018 		DMSG(vdcp, 0, "[%d] DATA NACK\n", vdcp->instance);
44163af08d82Slm66018 		mutex_exit(&vdcp->lock);
4417e1ebb9ecSlm66018 		return (EIO);
44180a55fbb7Slm66018 
44193af08d82Slm66018 	} else if (msg->tag.vio_subtype == VIO_SUBTYPE_INFO) {
4420*90e2f9dcSlm66018 		/*
4421*90e2f9dcSlm66018 		 * Update the I/O statistics to indicate that an error occurred.
4422*90e2f9dcSlm66018 		 * No need to update the wait/run queues as no specific read or
4423*90e2f9dcSlm66018 		 * write request is being completed in response to this 'msg'.
4424*90e2f9dcSlm66018 		 */
4425366a92acSlm66018 		VD_UPDATE_ERR_STATS(vdcp, vd_protoerrs);
44263af08d82Slm66018 		mutex_exit(&vdcp->lock);
4427e1ebb9ecSlm66018 		return (EPROTO);
4428e1ebb9ecSlm66018 	}
4429e1ebb9ecSlm66018 
44303af08d82Slm66018 	DMSG(vdcp, 1, ": start %d end %d\n", start, end);
44313af08d82Slm66018 	ASSERT(start == end);
44323af08d82Slm66018 
44333af08d82Slm66018 	ldep = &vdcp->local_dring[idx];
44343af08d82Slm66018 
44353af08d82Slm66018 	DMSG(vdcp, 1, ": state 0x%x - cb_type 0x%x\n",
44363af08d82Slm66018 	    ldep->dep->hdr.dstate, ldep->cb_type);
44373af08d82Slm66018 
4438e1ebb9ecSlm66018 	if (ldep->dep->hdr.dstate == VIO_DESC_DONE) {
44393af08d82Slm66018 		struct buf *bufp;
4440e1ebb9ecSlm66018 
44413af08d82Slm66018 		switch (ldep->cb_type) {
44423af08d82Slm66018 		case CB_SYNC:
44433af08d82Slm66018 			ASSERT(vdcp->sync_op_pending);
4444d10e4ef2Snarayan 
44453af08d82Slm66018 			status = vdc_depopulate_descriptor(vdcp, idx);
44463af08d82Slm66018 			vdcp->sync_op_status = status;
44473af08d82Slm66018 			vdcp->sync_op_pending = B_FALSE;
44483af08d82Slm66018 			cv_signal(&vdcp->sync_pending_cv);
44493af08d82Slm66018 			break;
44504bac2208Snarayan 
44513af08d82Slm66018 		case CB_STRATEGY:
44523af08d82Slm66018 			bufp = ldep->cb_arg;
44533af08d82Slm66018 			ASSERT(bufp != NULL);
44543c96341aSnarayan 			bufp->b_resid =
44553c96341aSnarayan 			    bufp->b_bcount - ldep->dep->payload.nbytes;
44563af08d82Slm66018 			status = ldep->dep->payload.status; /* Future:ntoh */
44573af08d82Slm66018 			if (status != 0) {
44583af08d82Slm66018 				DMSG(vdcp, 1, "strategy status=%d\n", status);
4459366a92acSlm66018 				VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
44603af08d82Slm66018 				bioerror(bufp, status);
4461d10e4ef2Snarayan 			}
44622f5224aeSachartre 
44632f5224aeSachartre 			(void) vdc_depopulate_descriptor(vdcp, idx);
44643c96341aSnarayan 
44653c96341aSnarayan 			DMSG(vdcp, 1,
44663c96341aSnarayan 			    "strategy complete req=%ld bytes resp=%ld bytes\n",
44673c96341aSnarayan 			    bufp->b_bcount, ldep->dep->payload.nbytes);
44682f5224aeSachartre 
44692f5224aeSachartre 			if (status != 0 && vdcp->failfast_interval != 0) {
44702f5224aeSachartre 				/*
44712f5224aeSachartre 				 * The I/O has failed and failfast is enabled.
44722f5224aeSachartre 				 * We need the failfast thread to check if the
44732f5224aeSachartre 				 * failure is due to a reservation conflict.
44742f5224aeSachartre 				 */
44752f5224aeSachartre 				(void) vdc_failfast_io_queue(vdcp, bufp);
44762f5224aeSachartre 			} else {
4477366a92acSlm66018 				if (status == 0) {
4478*90e2f9dcSlm66018 					op = (bufp->b_flags & B_READ) ?
4479366a92acSlm66018 					    VD_OP_BREAD : VD_OP_BWRITE;
4480366a92acSlm66018 					VD_UPDATE_IO_STATS(vdcp, op,
4481366a92acSlm66018 					    ldep->dep->payload.nbytes);
4482366a92acSlm66018 				}
4483*90e2f9dcSlm66018 				VD_KSTAT_RUNQ_EXIT(vdcp);
4484366a92acSlm66018 				DTRACE_IO1(done, buf_t *, bufp);
44852f5224aeSachartre 				biodone(bufp);
44862f5224aeSachartre 			}
44873af08d82Slm66018 			break;
44883af08d82Slm66018 
44893af08d82Slm66018 		default:
44903af08d82Slm66018 			ASSERT(0);
44910a55fbb7Slm66018 		}
44923af08d82Slm66018 	}
44933af08d82Slm66018 
44943af08d82Slm66018 	/* let the arrival signal propogate */
44953af08d82Slm66018 	mutex_exit(&vdcp->lock);
44960a55fbb7Slm66018 
4497e1ebb9ecSlm66018 	/* probe gives the count of how many entries were processed */
4498366a92acSlm66018 	DTRACE_PROBE2(processed, int, 1, vdc_t *, vdcp);
44990a55fbb7Slm66018 
45003af08d82Slm66018 	return (0);
45010a55fbb7Slm66018 }
45020a55fbb7Slm66018 
45030a55fbb7Slm66018 
45040a55fbb7Slm66018 /*
45050a55fbb7Slm66018  * Function:
45060a55fbb7Slm66018  *	vdc_handle_ver_msg()
45070a55fbb7Slm66018  *
45080a55fbb7Slm66018  * Description:
45090a55fbb7Slm66018  *
45100a55fbb7Slm66018  * Arguments:
45110a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
45120a55fbb7Slm66018  *	ver_msg	- LDC message sent by vDisk server
45130a55fbb7Slm66018  *
45140a55fbb7Slm66018  * Return Code:
45150a55fbb7Slm66018  *	0	- Success
45160a55fbb7Slm66018  */
45170a55fbb7Slm66018 static int
45180a55fbb7Slm66018 vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg)
45190a55fbb7Slm66018 {
45200a55fbb7Slm66018 	int status = 0;
45210a55fbb7Slm66018 
45220a55fbb7Slm66018 	ASSERT(vdc != NULL);
45230a55fbb7Slm66018 	ASSERT(mutex_owned(&vdc->lock));
45240a55fbb7Slm66018 
45250a55fbb7Slm66018 	if (ver_msg->tag.vio_subtype_env != VIO_VER_INFO) {
45260a55fbb7Slm66018 		return (EPROTO);
45270a55fbb7Slm66018 	}
45280a55fbb7Slm66018 
45290a55fbb7Slm66018 	if (ver_msg->dev_class != VDEV_DISK_SERVER) {
45300a55fbb7Slm66018 		return (EINVAL);
45310a55fbb7Slm66018 	}
45320a55fbb7Slm66018 
45330a55fbb7Slm66018 	switch (ver_msg->tag.vio_subtype) {
45340a55fbb7Slm66018 	case VIO_SUBTYPE_ACK:
45350a55fbb7Slm66018 		/*
45360a55fbb7Slm66018 		 * We check to see if the version returned is indeed supported
45370a55fbb7Slm66018 		 * (The server may have also adjusted the minor number downwards
45380a55fbb7Slm66018 		 * and if so 'ver_msg' will contain the actual version agreed)
45390a55fbb7Slm66018 		 */
45400a55fbb7Slm66018 		if (vdc_is_supported_version(ver_msg)) {
45410a55fbb7Slm66018 			vdc->ver.major = ver_msg->ver_major;
45420a55fbb7Slm66018 			vdc->ver.minor = ver_msg->ver_minor;
45430a55fbb7Slm66018 			ASSERT(vdc->ver.major > 0);
45440a55fbb7Slm66018 		} else {
45450a55fbb7Slm66018 			status = EPROTO;
45460a55fbb7Slm66018 		}
45470a55fbb7Slm66018 		break;
45480a55fbb7Slm66018 
45490a55fbb7Slm66018 	case VIO_SUBTYPE_NACK:
45500a55fbb7Slm66018 		/*
45510a55fbb7Slm66018 		 * call vdc_is_supported_version() which will return the next
45520a55fbb7Slm66018 		 * supported version (if any) in 'ver_msg'
45530a55fbb7Slm66018 		 */
45540a55fbb7Slm66018 		(void) vdc_is_supported_version(ver_msg);
45550a55fbb7Slm66018 		if (ver_msg->ver_major > 0) {
45560a55fbb7Slm66018 			size_t len = sizeof (*ver_msg);
45570a55fbb7Slm66018 
45580a55fbb7Slm66018 			ASSERT(vdc->ver.major > 0);
45590a55fbb7Slm66018 
45600a55fbb7Slm66018 			/* reset the necessary fields and resend */
45610a55fbb7Slm66018 			ver_msg->tag.vio_subtype = VIO_SUBTYPE_INFO;
45620a55fbb7Slm66018 			ver_msg->dev_class = VDEV_DISK;
45630a55fbb7Slm66018 
45640a55fbb7Slm66018 			status = vdc_send(vdc, (caddr_t)ver_msg, &len);
45653af08d82Slm66018 			DMSG(vdc, 0, "[%d] Resend VER info (LDC status = %d)\n",
45660a55fbb7Slm66018 			    vdc->instance, status);
45670a55fbb7Slm66018 			if (len != sizeof (*ver_msg))
45680a55fbb7Slm66018 				status = EBADMSG;
45690a55fbb7Slm66018 		} else {
457087a7269eSachartre 			DMSG(vdc, 0, "[%d] No common version with vDisk server",
457187a7269eSachartre 			    vdc->instance);
45720a55fbb7Slm66018 			status = ENOTSUP;
45730a55fbb7Slm66018 		}
45740a55fbb7Slm66018 
45750a55fbb7Slm66018 		break;
45761ae08745Sheppo 	case VIO_SUBTYPE_INFO:
45771ae08745Sheppo 		/*
45781ae08745Sheppo 		 * Handle the case where vds starts handshake
4579eff7243fSlm66018 		 * (for now only vdc is the instigator)
45801ae08745Sheppo 		 */
45811ae08745Sheppo 		status = ENOTSUP;
45821ae08745Sheppo 		break;
45831ae08745Sheppo 
45841ae08745Sheppo 	default:
45850a55fbb7Slm66018 		status = EINVAL;
45861ae08745Sheppo 		break;
45871ae08745Sheppo 	}
45881ae08745Sheppo 
45890a55fbb7Slm66018 	return (status);
45900a55fbb7Slm66018 }
45910a55fbb7Slm66018 
45920a55fbb7Slm66018 /*
45930a55fbb7Slm66018  * Function:
45940a55fbb7Slm66018  *	vdc_handle_attr_msg()
45950a55fbb7Slm66018  *
45960a55fbb7Slm66018  * Description:
45970a55fbb7Slm66018  *
45980a55fbb7Slm66018  * Arguments:
45990a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
46000a55fbb7Slm66018  *	attr_msg	- LDC message sent by vDisk server
46010a55fbb7Slm66018  *
46020a55fbb7Slm66018  * Return Code:
46030a55fbb7Slm66018  *	0	- Success
46040a55fbb7Slm66018  */
46050a55fbb7Slm66018 static int
46060a55fbb7Slm66018 vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg)
46070a55fbb7Slm66018 {
46080a55fbb7Slm66018 	int status = 0;
46090a55fbb7Slm66018 
46100a55fbb7Slm66018 	ASSERT(vdc != NULL);
46110a55fbb7Slm66018 	ASSERT(mutex_owned(&vdc->lock));
46120a55fbb7Slm66018 
46130a55fbb7Slm66018 	if (attr_msg->tag.vio_subtype_env != VIO_ATTR_INFO) {
46140a55fbb7Slm66018 		return (EPROTO);
46150a55fbb7Slm66018 	}
46160a55fbb7Slm66018 
46170a55fbb7Slm66018 	switch (attr_msg->tag.vio_subtype) {
46181ae08745Sheppo 	case VIO_SUBTYPE_ACK:
46191ae08745Sheppo 		/*
46201ae08745Sheppo 		 * We now verify the attributes sent by vds.
46211ae08745Sheppo 		 */
462278fcd0a1Sachartre 		if (attr_msg->vdisk_size == 0) {
462378fcd0a1Sachartre 			DMSG(vdc, 0, "[%d] Invalid disk size from vds",
462478fcd0a1Sachartre 			    vdc->instance);
462578fcd0a1Sachartre 			status = EINVAL;
462678fcd0a1Sachartre 			break;
462778fcd0a1Sachartre 		}
462878fcd0a1Sachartre 
462978fcd0a1Sachartre 		if (attr_msg->max_xfer_sz == 0) {
463078fcd0a1Sachartre 			DMSG(vdc, 0, "[%d] Invalid transfer size from vds",
463178fcd0a1Sachartre 			    vdc->instance);
463278fcd0a1Sachartre 			status = EINVAL;
463378fcd0a1Sachartre 			break;
463478fcd0a1Sachartre 		}
463578fcd0a1Sachartre 
46362f5224aeSachartre 		if (attr_msg->vdisk_size == VD_SIZE_UNKNOWN) {
46372f5224aeSachartre 			DMSG(vdc, 0, "[%d] Unknown disk size from vds",
46382f5224aeSachartre 			    vdc->instance);
46392f5224aeSachartre 			attr_msg->vdisk_size = 0;
46402f5224aeSachartre 		}
46412f5224aeSachartre 
464278fcd0a1Sachartre 		/*
464378fcd0a1Sachartre 		 * If the disk size is already set check that it hasn't changed.
464478fcd0a1Sachartre 		 */
46452f5224aeSachartre 		if ((vdc->vdisk_size != 0) && (attr_msg->vdisk_size != 0) &&
464678fcd0a1Sachartre 		    (vdc->vdisk_size != attr_msg->vdisk_size)) {
464778fcd0a1Sachartre 			DMSG(vdc, 0, "[%d] Different disk size from vds "
464878fcd0a1Sachartre 			    "(old=0x%lx - new=0x%lx", vdc->instance,
464978fcd0a1Sachartre 			    vdc->vdisk_size, attr_msg->vdisk_size)
465078fcd0a1Sachartre 			status = EINVAL;
465178fcd0a1Sachartre 			break;
465278fcd0a1Sachartre 		}
465378fcd0a1Sachartre 
46541ae08745Sheppo 		vdc->vdisk_size = attr_msg->vdisk_size;
46551ae08745Sheppo 		vdc->vdisk_type = attr_msg->vdisk_type;
465617cadca8Slm66018 		vdc->operations = attr_msg->operations;
465717cadca8Slm66018 		if (vio_ver_is_supported(vdc->ver, 1, 1))
465817cadca8Slm66018 			vdc->vdisk_media = attr_msg->vdisk_media;
465917cadca8Slm66018 		else
466017cadca8Slm66018 			vdc->vdisk_media = 0;
46611ae08745Sheppo 
46623af08d82Slm66018 		DMSG(vdc, 0, "[%d] max_xfer_sz: sent %lx acked %lx\n",
4663e1ebb9ecSlm66018 		    vdc->instance, vdc->max_xfer_sz, attr_msg->max_xfer_sz);
46643af08d82Slm66018 		DMSG(vdc, 0, "[%d] vdisk_block_size: sent %lx acked %x\n",
4665e1ebb9ecSlm66018 		    vdc->instance, vdc->block_size,
4666e1ebb9ecSlm66018 		    attr_msg->vdisk_block_size);
4667e1ebb9ecSlm66018 
46681ae08745Sheppo 		/*
4669e1ebb9ecSlm66018 		 * We don't know at compile time what the vDisk server will
467017cadca8Slm66018 		 * think are good values but we apply a large (arbitrary)
4671e1ebb9ecSlm66018 		 * upper bound to prevent memory exhaustion in vdc if it was
4672e1ebb9ecSlm66018 		 * allocating a DRing based of huge values sent by the server.
4673e1ebb9ecSlm66018 		 * We probably will never exceed this except if the message
4674e1ebb9ecSlm66018 		 * was garbage.
46751ae08745Sheppo 		 */
4676e1ebb9ecSlm66018 		if ((attr_msg->max_xfer_sz * attr_msg->vdisk_block_size) <=
4677e1ebb9ecSlm66018 		    (PAGESIZE * DEV_BSIZE)) {
4678e1ebb9ecSlm66018 			vdc->max_xfer_sz = attr_msg->max_xfer_sz;
4679e1ebb9ecSlm66018 			vdc->block_size = attr_msg->vdisk_block_size;
4680e1ebb9ecSlm66018 		} else {
46813af08d82Slm66018 			DMSG(vdc, 0, "[%d] vds block transfer size too big;"
4682e1ebb9ecSlm66018 			    " using max supported by vdc", vdc->instance);
46831ae08745Sheppo 		}
46841ae08745Sheppo 
4685f0ca1d9aSsb155480 		if ((attr_msg->xfer_mode != VIO_DRING_MODE_V1_0) ||
46861ae08745Sheppo 		    (attr_msg->vdisk_size > INT64_MAX) ||
468717cadca8Slm66018 		    (attr_msg->operations == 0) ||
46881ae08745Sheppo 		    (attr_msg->vdisk_type > VD_DISK_TYPE_DISK)) {
46893af08d82Slm66018 			DMSG(vdc, 0, "[%d] Invalid attributes from vds",
4690e1ebb9ecSlm66018 			    vdc->instance);
46911ae08745Sheppo 			status = EINVAL;
46921ae08745Sheppo 			break;
46931ae08745Sheppo 		}
46941ae08745Sheppo 
469578fcd0a1Sachartre 		/*
469678fcd0a1Sachartre 		 * Now that we have received all attributes we can create a
469778fcd0a1Sachartre 		 * fake geometry for the disk.
469878fcd0a1Sachartre 		 */
469978fcd0a1Sachartre 		vdc_create_fake_geometry(vdc);
47001ae08745Sheppo 		break;
47011ae08745Sheppo 
47021ae08745Sheppo 	case VIO_SUBTYPE_NACK:
47031ae08745Sheppo 		/*
47041ae08745Sheppo 		 * vds could not handle the attributes we sent so we
47051ae08745Sheppo 		 * stop negotiating.
47061ae08745Sheppo 		 */
47071ae08745Sheppo 		status = EPROTO;
47081ae08745Sheppo 		break;
47091ae08745Sheppo 
47101ae08745Sheppo 	case VIO_SUBTYPE_INFO:
47111ae08745Sheppo 		/*
47121ae08745Sheppo 		 * Handle the case where vds starts the handshake
47131ae08745Sheppo 		 * (for now; vdc is the only supported instigatior)
47141ae08745Sheppo 		 */
47151ae08745Sheppo 		status = ENOTSUP;
47161ae08745Sheppo 		break;
47171ae08745Sheppo 
47181ae08745Sheppo 	default:
47191ae08745Sheppo 		status = ENOTSUP;
47201ae08745Sheppo 		break;
47211ae08745Sheppo 	}
47221ae08745Sheppo 
47230a55fbb7Slm66018 	return (status);
47241ae08745Sheppo }
47251ae08745Sheppo 
47260a55fbb7Slm66018 /*
47270a55fbb7Slm66018  * Function:
47280a55fbb7Slm66018  *	vdc_handle_dring_reg_msg()
47290a55fbb7Slm66018  *
47300a55fbb7Slm66018  * Description:
47310a55fbb7Slm66018  *
47320a55fbb7Slm66018  * Arguments:
47330a55fbb7Slm66018  *	vdc		- soft state pointer for this instance of the driver.
47340a55fbb7Slm66018  *	dring_msg	- LDC message sent by vDisk server
47350a55fbb7Slm66018  *
47360a55fbb7Slm66018  * Return Code:
47370a55fbb7Slm66018  *	0	- Success
47380a55fbb7Slm66018  */
47390a55fbb7Slm66018 static int
47400a55fbb7Slm66018 vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *dring_msg)
47410a55fbb7Slm66018 {
47420a55fbb7Slm66018 	int		status = 0;
47431ae08745Sheppo 
47440a55fbb7Slm66018 	ASSERT(vdc != NULL);
47450a55fbb7Slm66018 	ASSERT(mutex_owned(&vdc->lock));
47460a55fbb7Slm66018 
47470a55fbb7Slm66018 	if (dring_msg->tag.vio_subtype_env != VIO_DRING_REG) {
47480a55fbb7Slm66018 		return (EPROTO);
47490a55fbb7Slm66018 	}
47500a55fbb7Slm66018 
47510a55fbb7Slm66018 	switch (dring_msg->tag.vio_subtype) {
47520a55fbb7Slm66018 	case VIO_SUBTYPE_ACK:
47531ae08745Sheppo 		/* save the received dring_ident */
47541ae08745Sheppo 		vdc->dring_ident = dring_msg->dring_ident;
47553af08d82Slm66018 		DMSG(vdc, 0, "[%d] Received dring ident=0x%lx\n",
4756e1ebb9ecSlm66018 		    vdc->instance, vdc->dring_ident);
47571ae08745Sheppo 		break;
47581ae08745Sheppo 
47591ae08745Sheppo 	case VIO_SUBTYPE_NACK:
47601ae08745Sheppo 		/*
47611ae08745Sheppo 		 * vds could not handle the DRing info we sent so we
47621ae08745Sheppo 		 * stop negotiating.
47631ae08745Sheppo 		 */
47643af08d82Slm66018 		DMSG(vdc, 0, "[%d] server could not register DRing\n",
47653af08d82Slm66018 		    vdc->instance);
47661ae08745Sheppo 		status = EPROTO;
47671ae08745Sheppo 		break;
47681ae08745Sheppo 
47691ae08745Sheppo 	case VIO_SUBTYPE_INFO:
47701ae08745Sheppo 		/*
47711ae08745Sheppo 		 * Handle the case where vds starts handshake
47721ae08745Sheppo 		 * (for now only vdc is the instigatior)
47731ae08745Sheppo 		 */
47741ae08745Sheppo 		status = ENOTSUP;
47751ae08745Sheppo 		break;
47761ae08745Sheppo 	default:
47771ae08745Sheppo 		status = ENOTSUP;
47781ae08745Sheppo 	}
47791ae08745Sheppo 
47801ae08745Sheppo 	return (status);
47811ae08745Sheppo }
47821ae08745Sheppo 
47831ae08745Sheppo /*
47841ae08745Sheppo  * Function:
47851ae08745Sheppo  *	vdc_verify_seq_num()
47861ae08745Sheppo  *
47871ae08745Sheppo  * Description:
4788e1ebb9ecSlm66018  *	This functions verifies that the sequence number sent back by the vDisk
4789e1ebb9ecSlm66018  *	server with the latest message is what is expected (i.e. it is greater
4790e1ebb9ecSlm66018  *	than the last seq num sent by the vDisk server and less than or equal
4791e1ebb9ecSlm66018  *	to the last seq num generated by vdc).
4792e1ebb9ecSlm66018  *
4793e1ebb9ecSlm66018  *	It then checks the request ID to see if any requests need processing
4794e1ebb9ecSlm66018  *	in the DRing.
47951ae08745Sheppo  *
47961ae08745Sheppo  * Arguments:
47971ae08745Sheppo  *	vdc		- soft state pointer for this instance of the driver.
47981ae08745Sheppo  *	dring_msg	- pointer to the LDC message sent by vds
47991ae08745Sheppo  *
48001ae08745Sheppo  * Return Code:
4801e1ebb9ecSlm66018  *	VDC_SEQ_NUM_TODO	- Message needs to be processed
4802e1ebb9ecSlm66018  *	VDC_SEQ_NUM_SKIP	- Message has already been processed
4803e1ebb9ecSlm66018  *	VDC_SEQ_NUM_INVALID	- The seq numbers are so out of sync,
4804e1ebb9ecSlm66018  *				  vdc cannot deal with them
48051ae08745Sheppo  */
4806e1ebb9ecSlm66018 static int
4807e1ebb9ecSlm66018 vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg)
48081ae08745Sheppo {
48091ae08745Sheppo 	ASSERT(vdc != NULL);
48101ae08745Sheppo 	ASSERT(dring_msg != NULL);
4811d10e4ef2Snarayan 	ASSERT(mutex_owned(&vdc->lock));
48121ae08745Sheppo 
48131ae08745Sheppo 	/*
48141ae08745Sheppo 	 * Check to see if the messages were responded to in the correct
4815e1ebb9ecSlm66018 	 * order by vds.
48161ae08745Sheppo 	 */
4817e1ebb9ecSlm66018 	if ((dring_msg->seq_num <= vdc->seq_num_reply) ||
4818e1ebb9ecSlm66018 	    (dring_msg->seq_num > vdc->seq_num)) {
48193af08d82Slm66018 		DMSG(vdc, 0, "?[%d] Bogus sequence_number %lu: "
4820e1ebb9ecSlm66018 		    "%lu > expected <= %lu (last proc req %lu sent %lu)\n",
4821e1ebb9ecSlm66018 		    vdc->instance, dring_msg->seq_num,
4822e1ebb9ecSlm66018 		    vdc->seq_num_reply, vdc->seq_num,
4823e1ebb9ecSlm66018 		    vdc->req_id_proc, vdc->req_id);
4824e1ebb9ecSlm66018 		return (VDC_SEQ_NUM_INVALID);
48251ae08745Sheppo 	}
4826e1ebb9ecSlm66018 	vdc->seq_num_reply = dring_msg->seq_num;
48271ae08745Sheppo 
4828e1ebb9ecSlm66018 	if (vdc->req_id_proc < vdc->req_id)
4829e1ebb9ecSlm66018 		return (VDC_SEQ_NUM_TODO);
4830e1ebb9ecSlm66018 	else
4831e1ebb9ecSlm66018 		return (VDC_SEQ_NUM_SKIP);
48321ae08745Sheppo }
48331ae08745Sheppo 
48340a55fbb7Slm66018 
48350a55fbb7Slm66018 /*
48360a55fbb7Slm66018  * Function:
48370a55fbb7Slm66018  *	vdc_is_supported_version()
48380a55fbb7Slm66018  *
48390a55fbb7Slm66018  * Description:
48400a55fbb7Slm66018  *	This routine checks if the major/minor version numbers specified in
48410a55fbb7Slm66018  *	'ver_msg' are supported. If not it finds the next version that is
48420a55fbb7Slm66018  *	in the supported version list 'vdc_version[]' and sets the fields in
48430a55fbb7Slm66018  *	'ver_msg' to those values
48440a55fbb7Slm66018  *
48450a55fbb7Slm66018  * Arguments:
48460a55fbb7Slm66018  *	ver_msg	- LDC message sent by vDisk server
48470a55fbb7Slm66018  *
48480a55fbb7Slm66018  * Return Code:
48490a55fbb7Slm66018  *	B_TRUE	- Success
48500a55fbb7Slm66018  *	B_FALSE	- Version not supported
48510a55fbb7Slm66018  */
48520a55fbb7Slm66018 static boolean_t
48530a55fbb7Slm66018 vdc_is_supported_version(vio_ver_msg_t *ver_msg)
48540a55fbb7Slm66018 {
48550a55fbb7Slm66018 	int vdc_num_versions = sizeof (vdc_version) / sizeof (vdc_version[0]);
48560a55fbb7Slm66018 
48570a55fbb7Slm66018 	for (int i = 0; i < vdc_num_versions; i++) {
48580a55fbb7Slm66018 		ASSERT(vdc_version[i].major > 0);
48590a55fbb7Slm66018 		ASSERT((i == 0) ||
48600a55fbb7Slm66018 		    (vdc_version[i].major < vdc_version[i-1].major));
48610a55fbb7Slm66018 
48620a55fbb7Slm66018 		/*
48630a55fbb7Slm66018 		 * If the major versions match, adjust the minor version, if
48640a55fbb7Slm66018 		 * necessary, down to the highest value supported by this
48650a55fbb7Slm66018 		 * client. The server should support all minor versions lower
48660a55fbb7Slm66018 		 * than the value it sent
48670a55fbb7Slm66018 		 */
48680a55fbb7Slm66018 		if (ver_msg->ver_major == vdc_version[i].major) {
48690a55fbb7Slm66018 			if (ver_msg->ver_minor > vdc_version[i].minor) {
48703af08d82Slm66018 				DMSGX(0,
48713af08d82Slm66018 				    "Adjusting minor version from %u to %u",
48720a55fbb7Slm66018 				    ver_msg->ver_minor, vdc_version[i].minor);
48730a55fbb7Slm66018 				ver_msg->ver_minor = vdc_version[i].minor;
48740a55fbb7Slm66018 			}
48750a55fbb7Slm66018 			return (B_TRUE);
48760a55fbb7Slm66018 		}
48770a55fbb7Slm66018 
48780a55fbb7Slm66018 		/*
48790a55fbb7Slm66018 		 * If the message contains a higher major version number, set
48800a55fbb7Slm66018 		 * the message's major/minor versions to the current values
48810a55fbb7Slm66018 		 * and return false, so this message will get resent with
48820a55fbb7Slm66018 		 * these values, and the server will potentially try again
48830a55fbb7Slm66018 		 * with the same or a lower version
48840a55fbb7Slm66018 		 */
48850a55fbb7Slm66018 		if (ver_msg->ver_major > vdc_version[i].major) {
48860a55fbb7Slm66018 			ver_msg->ver_major = vdc_version[i].major;
48870a55fbb7Slm66018 			ver_msg->ver_minor = vdc_version[i].minor;
48883af08d82Slm66018 			DMSGX(0, "Suggesting major/minor (0x%x/0x%x)\n",
48890a55fbb7Slm66018 			    ver_msg->ver_major, ver_msg->ver_minor);
48900a55fbb7Slm66018 
48910a55fbb7Slm66018 			return (B_FALSE);
48920a55fbb7Slm66018 		}
48930a55fbb7Slm66018 
48940a55fbb7Slm66018 		/*
48950a55fbb7Slm66018 		 * Otherwise, the message's major version is less than the
48960a55fbb7Slm66018 		 * current major version, so continue the loop to the next
48970a55fbb7Slm66018 		 * (lower) supported version
48980a55fbb7Slm66018 		 */
48990a55fbb7Slm66018 	}
49000a55fbb7Slm66018 
49010a55fbb7Slm66018 	/*
49020a55fbb7Slm66018 	 * No common version was found; "ground" the version pair in the
49030a55fbb7Slm66018 	 * message to terminate negotiation
49040a55fbb7Slm66018 	 */
49050a55fbb7Slm66018 	ver_msg->ver_major = 0;
49060a55fbb7Slm66018 	ver_msg->ver_minor = 0;
49070a55fbb7Slm66018 
49080a55fbb7Slm66018 	return (B_FALSE);
49090a55fbb7Slm66018 }
49101ae08745Sheppo /* -------------------------------------------------------------------------- */
49111ae08745Sheppo 
49121ae08745Sheppo /*
49131ae08745Sheppo  * DKIO(7) support
49141ae08745Sheppo  */
49151ae08745Sheppo 
49161ae08745Sheppo typedef struct vdc_dk_arg {
49171ae08745Sheppo 	struct dk_callback	dkc;
49181ae08745Sheppo 	int			mode;
49191ae08745Sheppo 	dev_t			dev;
49201ae08745Sheppo 	vdc_t			*vdc;
49211ae08745Sheppo } vdc_dk_arg_t;
49221ae08745Sheppo 
49231ae08745Sheppo /*
49241ae08745Sheppo  * Function:
49251ae08745Sheppo  * 	vdc_dkio_flush_cb()
49261ae08745Sheppo  *
49271ae08745Sheppo  * Description:
49281ae08745Sheppo  *	This routine is a callback for DKIOCFLUSHWRITECACHE which can be called
49291ae08745Sheppo  *	by kernel code.
49301ae08745Sheppo  *
49311ae08745Sheppo  * Arguments:
49321ae08745Sheppo  *	arg	- a pointer to a vdc_dk_arg_t structure.
49331ae08745Sheppo  */
49341ae08745Sheppo void
49351ae08745Sheppo vdc_dkio_flush_cb(void *arg)
49361ae08745Sheppo {
49371ae08745Sheppo 	struct vdc_dk_arg	*dk_arg = (struct vdc_dk_arg *)arg;
49381ae08745Sheppo 	struct dk_callback	*dkc = NULL;
49391ae08745Sheppo 	vdc_t			*vdc = NULL;
49401ae08745Sheppo 	int			rv;
49411ae08745Sheppo 
49421ae08745Sheppo 	if (dk_arg == NULL) {
49433af08d82Slm66018 		cmn_err(CE_NOTE, "?[Unk] DKIOCFLUSHWRITECACHE arg is NULL\n");
49441ae08745Sheppo 		return;
49451ae08745Sheppo 	}
49461ae08745Sheppo 	dkc = &dk_arg->dkc;
49471ae08745Sheppo 	vdc = dk_arg->vdc;
49481ae08745Sheppo 	ASSERT(vdc != NULL);
49491ae08745Sheppo 
49503af08d82Slm66018 	rv = vdc_do_sync_op(vdc, VD_OP_FLUSH, NULL, 0,
49512f5224aeSachartre 	    VDCPART(dk_arg->dev), 0, CB_SYNC, 0, VIO_both_dir, B_TRUE);
49521ae08745Sheppo 	if (rv != 0) {
49533af08d82Slm66018 		DMSG(vdc, 0, "[%d] DKIOCFLUSHWRITECACHE failed %d : model %x\n",
4954e1ebb9ecSlm66018 		    vdc->instance, rv,
49551ae08745Sheppo 		    ddi_model_convert_from(dk_arg->mode & FMODELS));
49561ae08745Sheppo 	}
49571ae08745Sheppo 
49581ae08745Sheppo 	/*
49591ae08745Sheppo 	 * Trigger the call back to notify the caller the the ioctl call has
49601ae08745Sheppo 	 * been completed.
49611ae08745Sheppo 	 */
49621ae08745Sheppo 	if ((dk_arg->mode & FKIOCTL) &&
49631ae08745Sheppo 	    (dkc != NULL) &&
49641ae08745Sheppo 	    (dkc->dkc_callback != NULL)) {
49651ae08745Sheppo 		ASSERT(dkc->dkc_cookie != NULL);
49668e6a2a04Slm66018 		(*dkc->dkc_callback)(dkc->dkc_cookie, rv);
49671ae08745Sheppo 	}
49681ae08745Sheppo 
49691ae08745Sheppo 	/* Indicate that one less DKIO write flush is outstanding */
49701ae08745Sheppo 	mutex_enter(&vdc->lock);
49711ae08745Sheppo 	vdc->dkio_flush_pending--;
49721ae08745Sheppo 	ASSERT(vdc->dkio_flush_pending >= 0);
49731ae08745Sheppo 	mutex_exit(&vdc->lock);
49748e6a2a04Slm66018 
49758e6a2a04Slm66018 	/* free the mem that was allocated when the callback was dispatched */
49768e6a2a04Slm66018 	kmem_free(arg, sizeof (vdc_dk_arg_t));
49771ae08745Sheppo }
49781ae08745Sheppo 
49791ae08745Sheppo /*
498087a7269eSachartre  * Function:
49819642afceSachartre  * 	vdc_dkio_gapart()
498287a7269eSachartre  *
498387a7269eSachartre  * Description:
498487a7269eSachartre  *	This function implements the DKIOCGAPART ioctl.
498587a7269eSachartre  *
498687a7269eSachartre  * Arguments:
498778fcd0a1Sachartre  *	vdc	- soft state pointer
498887a7269eSachartre  *	arg	- a pointer to a dk_map[NDKMAP] or dk_map32[NDKMAP] structure
498987a7269eSachartre  *	flag	- ioctl flags
499087a7269eSachartre  */
499187a7269eSachartre static int
49929642afceSachartre vdc_dkio_gapart(vdc_t *vdc, caddr_t arg, int flag)
499387a7269eSachartre {
499478fcd0a1Sachartre 	struct dk_geom *geom;
499578fcd0a1Sachartre 	struct vtoc *vtoc;
499687a7269eSachartre 	union {
499787a7269eSachartre 		struct dk_map map[NDKMAP];
499887a7269eSachartre 		struct dk_map32 map32[NDKMAP];
499987a7269eSachartre 	} data;
500087a7269eSachartre 	int i, rv, size;
500187a7269eSachartre 
500278fcd0a1Sachartre 	mutex_enter(&vdc->lock);
500387a7269eSachartre 
500478fcd0a1Sachartre 	if ((rv = vdc_validate_geometry(vdc)) != 0) {
500578fcd0a1Sachartre 		mutex_exit(&vdc->lock);
500687a7269eSachartre 		return (rv);
500778fcd0a1Sachartre 	}
500887a7269eSachartre 
500978fcd0a1Sachartre 	vtoc = vdc->vtoc;
501078fcd0a1Sachartre 	geom = vdc->geom;
501187a7269eSachartre 
501287a7269eSachartre 	if (ddi_model_convert_from(flag & FMODELS) == DDI_MODEL_ILP32) {
501387a7269eSachartre 
501478fcd0a1Sachartre 		for (i = 0; i < vtoc->v_nparts; i++) {
501578fcd0a1Sachartre 			data.map32[i].dkl_cylno = vtoc->v_part[i].p_start /
501678fcd0a1Sachartre 			    (geom->dkg_nhead * geom->dkg_nsect);
501778fcd0a1Sachartre 			data.map32[i].dkl_nblk = vtoc->v_part[i].p_size;
501887a7269eSachartre 		}
501987a7269eSachartre 		size = NDKMAP * sizeof (struct dk_map32);
502087a7269eSachartre 
502187a7269eSachartre 	} else {
502287a7269eSachartre 
502378fcd0a1Sachartre 		for (i = 0; i < vtoc->v_nparts; i++) {
502478fcd0a1Sachartre 			data.map[i].dkl_cylno = vtoc->v_part[i].p_start /
502578fcd0a1Sachartre 			    (geom->dkg_nhead * geom->dkg_nsect);
502678fcd0a1Sachartre 			data.map[i].dkl_nblk = vtoc->v_part[i].p_size;
502787a7269eSachartre 		}
502887a7269eSachartre 		size = NDKMAP * sizeof (struct dk_map);
502987a7269eSachartre 
503087a7269eSachartre 	}
503187a7269eSachartre 
503278fcd0a1Sachartre 	mutex_exit(&vdc->lock);
503378fcd0a1Sachartre 
503487a7269eSachartre 	if (ddi_copyout(&data, arg, size, flag) != 0)
503587a7269eSachartre 		return (EFAULT);
503687a7269eSachartre 
503787a7269eSachartre 	return (0);
503887a7269eSachartre }
503987a7269eSachartre 
504087a7269eSachartre /*
504187a7269eSachartre  * Function:
50429642afceSachartre  * 	vdc_dkio_partition()
50439642afceSachartre  *
50449642afceSachartre  * Description:
50459642afceSachartre  *	This function implements the DKIOCPARTITION ioctl.
50469642afceSachartre  *
50479642afceSachartre  * Arguments:
50489642afceSachartre  *	vdc	- soft state pointer
50499642afceSachartre  *	arg	- a pointer to a struct partition64 structure
50509642afceSachartre  *	flag	- ioctl flags
50519642afceSachartre  */
50529642afceSachartre static int
50539642afceSachartre vdc_dkio_partition(vdc_t *vdc, caddr_t arg, int flag)
50549642afceSachartre {
50559642afceSachartre 	struct partition64 p64;
50569642afceSachartre 	efi_gpt_t *gpt;
50579642afceSachartre 	efi_gpe_t *gpe;
50589642afceSachartre 	vd_efi_dev_t edev;
50599642afceSachartre 	uint_t partno;
50609642afceSachartre 	int rv;
50619642afceSachartre 
50629642afceSachartre 	if (ddi_copyin(arg, &p64, sizeof (struct partition64), flag)) {
50639642afceSachartre 		return (EFAULT);
50649642afceSachartre 	}
50659642afceSachartre 
50669642afceSachartre 	VD_EFI_DEV_SET(edev, vdc, vd_process_efi_ioctl);
50679642afceSachartre 
50689642afceSachartre 	if ((rv = vd_efi_alloc_and_read(&edev, &gpt, &gpe)) != 0) {
50699642afceSachartre 		return (rv);
50709642afceSachartre 	}
50719642afceSachartre 
50729642afceSachartre 	partno = p64.p_partno;
50739642afceSachartre 
50749642afceSachartre 	if (partno >= gpt->efi_gpt_NumberOfPartitionEntries) {
50759642afceSachartre 		vd_efi_free(&edev, gpt, gpe);
50769642afceSachartre 		return (ESRCH);
50779642afceSachartre 	}
50789642afceSachartre 
50799642afceSachartre 	bcopy(&gpe[partno].efi_gpe_PartitionTypeGUID, &p64.p_type,
50809642afceSachartre 	    sizeof (struct uuid));
50819642afceSachartre 	p64.p_start = gpe[partno].efi_gpe_StartingLBA;
50829642afceSachartre 	p64.p_size = gpe[partno].efi_gpe_EndingLBA - p64.p_start + 1;
50839642afceSachartre 
50849642afceSachartre 	if (ddi_copyout(&p64, arg, sizeof (struct partition64), flag)) {
50859642afceSachartre 		vd_efi_free(&edev, gpt, gpe);
50869642afceSachartre 		return (EFAULT);
50879642afceSachartre 	}
50889642afceSachartre 
50899642afceSachartre 	vd_efi_free(&edev, gpt, gpe);
50909642afceSachartre 	return (0);
50919642afceSachartre }
50929642afceSachartre 
50939642afceSachartre /*
50949642afceSachartre  * Function:
509587a7269eSachartre  * 	vdc_dioctl_rwcmd()
509687a7269eSachartre  *
509787a7269eSachartre  * Description:
509887a7269eSachartre  *	This function implements the DIOCTL_RWCMD ioctl. This ioctl is used
509987a7269eSachartre  *	for DKC_DIRECT disks to read or write at an absolute disk offset.
510087a7269eSachartre  *
510187a7269eSachartre  * Arguments:
510287a7269eSachartre  *	dev	- device
510387a7269eSachartre  *	arg	- a pointer to a dadkio_rwcmd or dadkio_rwcmd32 structure
510487a7269eSachartre  *	flag	- ioctl flags
510587a7269eSachartre  */
510687a7269eSachartre static int
510787a7269eSachartre vdc_dioctl_rwcmd(dev_t dev, caddr_t arg, int flag)
510887a7269eSachartre {
510987a7269eSachartre 	struct dadkio_rwcmd32 rwcmd32;
511087a7269eSachartre 	struct dadkio_rwcmd rwcmd;
511187a7269eSachartre 	struct iovec aiov;
511287a7269eSachartre 	struct uio auio;
511387a7269eSachartre 	int rw, status;
511487a7269eSachartre 	struct buf *buf;
511587a7269eSachartre 
511687a7269eSachartre 	if (ddi_model_convert_from(flag & FMODELS) == DDI_MODEL_ILP32) {
511787a7269eSachartre 		if (ddi_copyin((caddr_t)arg, (caddr_t)&rwcmd32,
511887a7269eSachartre 		    sizeof (struct dadkio_rwcmd32), flag)) {
511987a7269eSachartre 			return (EFAULT);
512087a7269eSachartre 		}
512187a7269eSachartre 		rwcmd.cmd = rwcmd32.cmd;
512287a7269eSachartre 		rwcmd.flags = rwcmd32.flags;
512387a7269eSachartre 		rwcmd.blkaddr = (daddr_t)rwcmd32.blkaddr;
512487a7269eSachartre 		rwcmd.buflen = rwcmd32.buflen;
512587a7269eSachartre 		rwcmd.bufaddr = (caddr_t)(uintptr_t)rwcmd32.bufaddr;
512687a7269eSachartre 	} else {
512787a7269eSachartre 		if (ddi_copyin((caddr_t)arg, (caddr_t)&rwcmd,
512887a7269eSachartre 		    sizeof (struct dadkio_rwcmd), flag)) {
512987a7269eSachartre 			return (EFAULT);
513087a7269eSachartre 		}
513187a7269eSachartre 	}
513287a7269eSachartre 
513387a7269eSachartre 	switch (rwcmd.cmd) {
513487a7269eSachartre 	case DADKIO_RWCMD_READ:
513587a7269eSachartre 		rw = B_READ;
513687a7269eSachartre 		break;
513787a7269eSachartre 	case DADKIO_RWCMD_WRITE:
513887a7269eSachartre 		rw = B_WRITE;
513987a7269eSachartre 		break;
514087a7269eSachartre 	default:
514187a7269eSachartre 		return (EINVAL);
514287a7269eSachartre 	}
514387a7269eSachartre 
514487a7269eSachartre 	bzero((caddr_t)&aiov, sizeof (struct iovec));
514587a7269eSachartre 	aiov.iov_base   = rwcmd.bufaddr;
514687a7269eSachartre 	aiov.iov_len    = rwcmd.buflen;
514787a7269eSachartre 
514887a7269eSachartre 	bzero((caddr_t)&auio, sizeof (struct uio));
514987a7269eSachartre 	auio.uio_iov    = &aiov;
515087a7269eSachartre 	auio.uio_iovcnt = 1;
515187a7269eSachartre 	auio.uio_loffset = rwcmd.blkaddr * DEV_BSIZE;
515287a7269eSachartre 	auio.uio_resid  = rwcmd.buflen;
515387a7269eSachartre 	auio.uio_segflg = flag & FKIOCTL ? UIO_SYSSPACE : UIO_USERSPACE;
515487a7269eSachartre 
515587a7269eSachartre 	buf = kmem_alloc(sizeof (buf_t), KM_SLEEP);
515687a7269eSachartre 	bioinit(buf);
515787a7269eSachartre 	/*
515887a7269eSachartre 	 * We use the private field of buf to specify that this is an
515987a7269eSachartre 	 * I/O using an absolute offset.
516087a7269eSachartre 	 */
516187a7269eSachartre 	buf->b_private = (void *)VD_SLICE_NONE;
516287a7269eSachartre 
516387a7269eSachartre 	status = physio(vdc_strategy, buf, dev, rw, vdc_min, &auio);
516487a7269eSachartre 
516587a7269eSachartre 	biofini(buf);
516687a7269eSachartre 	kmem_free(buf, sizeof (buf_t));
516787a7269eSachartre 
516887a7269eSachartre 	return (status);
516987a7269eSachartre }
517087a7269eSachartre 
517187a7269eSachartre /*
51722f5224aeSachartre  * Allocate a buffer for a VD_OP_SCSICMD operation. The size of the allocated
51732f5224aeSachartre  * buffer is returned in alloc_len.
51742f5224aeSachartre  */
51752f5224aeSachartre static vd_scsi_t *
51762f5224aeSachartre vdc_scsi_alloc(int cdb_len, int sense_len, int datain_len, int dataout_len,
51772f5224aeSachartre     int *alloc_len)
51782f5224aeSachartre {
51792f5224aeSachartre 	vd_scsi_t *vd_scsi;
51802f5224aeSachartre 	int vd_scsi_len = VD_SCSI_SIZE;
51812f5224aeSachartre 
51822f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(cdb_len, sizeof (uint64_t));
51832f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(sense_len, sizeof (uint64_t));
51842f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(datain_len, sizeof (uint64_t));
51852f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(dataout_len, sizeof (uint64_t));
51862f5224aeSachartre 
51872f5224aeSachartre 	ASSERT(vd_scsi_len % sizeof (uint64_t) == 0);
51882f5224aeSachartre 
51892f5224aeSachartre 	vd_scsi = kmem_zalloc(vd_scsi_len, KM_SLEEP);
51902f5224aeSachartre 
51912f5224aeSachartre 	vd_scsi->cdb_len = cdb_len;
51922f5224aeSachartre 	vd_scsi->sense_len = sense_len;
51932f5224aeSachartre 	vd_scsi->datain_len = datain_len;
51942f5224aeSachartre 	vd_scsi->dataout_len = dataout_len;
51952f5224aeSachartre 
51962f5224aeSachartre 	*alloc_len = vd_scsi_len;
51972f5224aeSachartre 
51982f5224aeSachartre 	return (vd_scsi);
51992f5224aeSachartre }
52002f5224aeSachartre 
52012f5224aeSachartre /*
52022f5224aeSachartre  * Convert the status of a SCSI command to a Solaris return code.
52032f5224aeSachartre  *
52042f5224aeSachartre  * Arguments:
52052f5224aeSachartre  *	vd_scsi		- The SCSI operation buffer.
52062f5224aeSachartre  *	log_error	- indicate if an error message should be logged.
52072f5224aeSachartre  *
52082f5224aeSachartre  * Note that our SCSI error messages are rather primitive for the moment
52092f5224aeSachartre  * and could be improved by decoding some data like the SCSI command and
52102f5224aeSachartre  * the sense key.
52112f5224aeSachartre  *
52122f5224aeSachartre  * Return value:
52132f5224aeSachartre  *	0		- Status is good.
52142f5224aeSachartre  *	EACCES		- Status reports a reservation conflict.
52152f5224aeSachartre  *	ENOTSUP		- Status reports a check condition and sense key
52162f5224aeSachartre  *			  reports an illegal request.
52172f5224aeSachartre  *	EIO		- Any other status.
52182f5224aeSachartre  */
52192f5224aeSachartre static int
52202f5224aeSachartre vdc_scsi_status(vdc_t *vdc, vd_scsi_t *vd_scsi, boolean_t log_error)
52212f5224aeSachartre {
52222f5224aeSachartre 	int rv;
52232f5224aeSachartre 	char path_str[MAXPATHLEN];
52242f5224aeSachartre 	char panic_str[VDC_RESV_CONFLICT_FMT_LEN + MAXPATHLEN];
52252f5224aeSachartre 	union scsi_cdb *cdb;
52262f5224aeSachartre 	struct scsi_extended_sense *sense;
52272f5224aeSachartre 
52282f5224aeSachartre 	if (vd_scsi->cmd_status == STATUS_GOOD)
52292f5224aeSachartre 		/* no error */
52302f5224aeSachartre 		return (0);
52312f5224aeSachartre 
52322f5224aeSachartre 	/* when the tunable vdc_scsi_log_error is true we log all errors */
52332f5224aeSachartre 	if (vdc_scsi_log_error)
52342f5224aeSachartre 		log_error = B_TRUE;
52352f5224aeSachartre 
52362f5224aeSachartre 	if (log_error) {
52372f5224aeSachartre 		cmn_err(CE_WARN, "%s (vdc%d):\tError for Command: 0x%x)\n",
52382f5224aeSachartre 		    ddi_pathname(vdc->dip, path_str), vdc->instance,
52392f5224aeSachartre 		    GETCMD(VD_SCSI_DATA_CDB(vd_scsi)));
52402f5224aeSachartre 	}
52412f5224aeSachartre 
52422f5224aeSachartre 	/* default returned value */
52432f5224aeSachartre 	rv = EIO;
52442f5224aeSachartre 
52452f5224aeSachartre 	switch (vd_scsi->cmd_status) {
52462f5224aeSachartre 
52472f5224aeSachartre 	case STATUS_CHECK:
52482f5224aeSachartre 	case STATUS_TERMINATED:
52492f5224aeSachartre 		if (log_error)
52502f5224aeSachartre 			cmn_err(CE_CONT, "\tCheck Condition Error\n");
52512f5224aeSachartre 
52522f5224aeSachartre 		/* check sense buffer */
52532f5224aeSachartre 		if (vd_scsi->sense_len == 0 ||
52542f5224aeSachartre 		    vd_scsi->sense_status != STATUS_GOOD) {
52552f5224aeSachartre 			if (log_error)
52562f5224aeSachartre 				cmn_err(CE_CONT, "\tNo Sense Data Available\n");
52572f5224aeSachartre 			break;
52582f5224aeSachartre 		}
52592f5224aeSachartre 
52602f5224aeSachartre 		sense = VD_SCSI_DATA_SENSE(vd_scsi);
52612f5224aeSachartre 
52622f5224aeSachartre 		if (log_error) {
52632f5224aeSachartre 			cmn_err(CE_CONT, "\tSense Key:  0x%x\n"
52642f5224aeSachartre 			    "\tASC: 0x%x, ASCQ: 0x%x\n",
52652f5224aeSachartre 			    scsi_sense_key((uint8_t *)sense),
52662f5224aeSachartre 			    scsi_sense_asc((uint8_t *)sense),
52672f5224aeSachartre 			    scsi_sense_ascq((uint8_t *)sense));
52682f5224aeSachartre 		}
52692f5224aeSachartre 
52702f5224aeSachartre 		if (scsi_sense_key((uint8_t *)sense) == KEY_ILLEGAL_REQUEST)
52712f5224aeSachartre 			rv = ENOTSUP;
52722f5224aeSachartre 		break;
52732f5224aeSachartre 
52742f5224aeSachartre 	case STATUS_BUSY:
52752f5224aeSachartre 		if (log_error)
52762f5224aeSachartre 			cmn_err(CE_NOTE, "\tDevice Busy\n");
52772f5224aeSachartre 		break;
52782f5224aeSachartre 
52792f5224aeSachartre 	case STATUS_RESERVATION_CONFLICT:
52802f5224aeSachartre 		/*
52812f5224aeSachartre 		 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then
52822f5224aeSachartre 		 * reservation conflict could be due to various reasons like
52832f5224aeSachartre 		 * incorrect keys, not registered or not reserved etc. So,
52842f5224aeSachartre 		 * we should not panic in that case.
52852f5224aeSachartre 		 */
52862f5224aeSachartre 		cdb = VD_SCSI_DATA_CDB(vd_scsi);
52872f5224aeSachartre 		if (vdc->failfast_interval != 0 &&
52882f5224aeSachartre 		    cdb->scc_cmd != SCMD_PERSISTENT_RESERVE_IN &&
52892f5224aeSachartre 		    cdb->scc_cmd != SCMD_PERSISTENT_RESERVE_OUT) {
52902f5224aeSachartre 			/* failfast is enabled so we have to panic */
52912f5224aeSachartre 			(void) snprintf(panic_str, sizeof (panic_str),
52922f5224aeSachartre 			    VDC_RESV_CONFLICT_FMT_STR "%s",
52932f5224aeSachartre 			    ddi_pathname(vdc->dip, path_str));
52942f5224aeSachartre 			panic(panic_str);
52952f5224aeSachartre 		}
52962f5224aeSachartre 		if (log_error)
52972f5224aeSachartre 			cmn_err(CE_NOTE, "\tReservation Conflict\n");
52982f5224aeSachartre 		rv = EACCES;
52992f5224aeSachartre 		break;
53002f5224aeSachartre 
53012f5224aeSachartre 	case STATUS_QFULL:
53022f5224aeSachartre 		if (log_error)
53032f5224aeSachartre 			cmn_err(CE_NOTE, "\tQueue Full\n");
53042f5224aeSachartre 		break;
53052f5224aeSachartre 
53062f5224aeSachartre 	case STATUS_MET:
53072f5224aeSachartre 	case STATUS_INTERMEDIATE:
53082f5224aeSachartre 	case STATUS_SCSI2:
53092f5224aeSachartre 	case STATUS_INTERMEDIATE_MET:
53102f5224aeSachartre 	case STATUS_ACA_ACTIVE:
53112f5224aeSachartre 		if (log_error)
53122f5224aeSachartre 			cmn_err(CE_CONT,
53132f5224aeSachartre 			    "\tUnexpected SCSI status received: 0x%x\n",
53142f5224aeSachartre 			    vd_scsi->cmd_status);
53152f5224aeSachartre 		break;
53162f5224aeSachartre 
53172f5224aeSachartre 	default:
53182f5224aeSachartre 		if (log_error)
53192f5224aeSachartre 			cmn_err(CE_CONT,
53202f5224aeSachartre 			    "\tInvalid SCSI status received: 0x%x\n",
53212f5224aeSachartre 			    vd_scsi->cmd_status);
53222f5224aeSachartre 		break;
53232f5224aeSachartre 	}
53242f5224aeSachartre 
53252f5224aeSachartre 	return (rv);
53262f5224aeSachartre }
53272f5224aeSachartre 
53282f5224aeSachartre /*
53292f5224aeSachartre  * Implemented the USCSICMD uscsi(7I) ioctl. This ioctl is converted to
53302f5224aeSachartre  * a VD_OP_SCSICMD operation which is sent to the vdisk server. If a SCSI
53312f5224aeSachartre  * reset is requested (i.e. a flag USCSI_RESET* is set) then the ioctl is
53322f5224aeSachartre  * converted to a VD_OP_RESET operation.
53332f5224aeSachartre  */
53342f5224aeSachartre static int
53352f5224aeSachartre vdc_uscsi_cmd(vdc_t *vdc, caddr_t arg, int mode)
53362f5224aeSachartre {
53372f5224aeSachartre 	struct uscsi_cmd 	uscsi;
53382f5224aeSachartre 	struct uscsi_cmd32	uscsi32;
53392f5224aeSachartre 	vd_scsi_t 		*vd_scsi;
53402f5224aeSachartre 	int 			vd_scsi_len;
53412f5224aeSachartre 	union scsi_cdb		*cdb;
53422f5224aeSachartre 	struct scsi_extended_sense *sense;
53432f5224aeSachartre 	char 			*datain, *dataout;
53442f5224aeSachartre 	size_t			cdb_len, datain_len, dataout_len, sense_len;
53452f5224aeSachartre 	int 			rv;
53462f5224aeSachartre 
53472f5224aeSachartre 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
53482f5224aeSachartre 		if (ddi_copyin(arg, &uscsi32, sizeof (struct uscsi_cmd32),
53492f5224aeSachartre 		    mode) != 0)
53502f5224aeSachartre 			return (EFAULT);
53512f5224aeSachartre 		uscsi_cmd32touscsi_cmd((&uscsi32), (&uscsi));
53522f5224aeSachartre 	} else {
53532f5224aeSachartre 		if (ddi_copyin(arg, &uscsi, sizeof (struct uscsi_cmd),
53542f5224aeSachartre 		    mode) != 0)
53552f5224aeSachartre 			return (EFAULT);
53562f5224aeSachartre 	}
53572f5224aeSachartre 
53582f5224aeSachartre 	/* a uscsi reset is converted to a VD_OP_RESET operation */
53592f5224aeSachartre 	if (uscsi.uscsi_flags & (USCSI_RESET | USCSI_RESET_LUN |
53602f5224aeSachartre 	    USCSI_RESET_ALL)) {
53612f5224aeSachartre 		rv = vdc_do_sync_op(vdc, VD_OP_RESET, NULL, 0, 0, 0, CB_SYNC,
53622f5224aeSachartre 		    (void *)(uint64_t)mode, VIO_both_dir, B_TRUE);
53632f5224aeSachartre 		return (rv);
53642f5224aeSachartre 	}
53652f5224aeSachartre 
53662f5224aeSachartre 	/* cdb buffer length */
53672f5224aeSachartre 	cdb_len = uscsi.uscsi_cdblen;
53682f5224aeSachartre 
53692f5224aeSachartre 	/* data in and out buffers length */
53702f5224aeSachartre 	if (uscsi.uscsi_flags & USCSI_READ) {
53712f5224aeSachartre 		datain_len = uscsi.uscsi_buflen;
53722f5224aeSachartre 		dataout_len = 0;
53732f5224aeSachartre 	} else {
53742f5224aeSachartre 		datain_len = 0;
53752f5224aeSachartre 		dataout_len = uscsi.uscsi_buflen;
53762f5224aeSachartre 	}
53772f5224aeSachartre 
53782f5224aeSachartre 	/* sense buffer length */
53792f5224aeSachartre 	if (uscsi.uscsi_flags & USCSI_RQENABLE)
53802f5224aeSachartre 		sense_len = uscsi.uscsi_rqlen;
53812f5224aeSachartre 	else
53822f5224aeSachartre 		sense_len = 0;
53832f5224aeSachartre 
53842f5224aeSachartre 	/* allocate buffer for the VD_SCSICMD_OP operation */
53852f5224aeSachartre 	vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, datain_len, dataout_len,
53862f5224aeSachartre 	    &vd_scsi_len);
53872f5224aeSachartre 
53882f5224aeSachartre 	/*
53892f5224aeSachartre 	 * The documentation of USCSI_ISOLATE and USCSI_DIAGNOSE is very vague,
53902f5224aeSachartre 	 * but basically they prevent a SCSI command from being retried in case
53912f5224aeSachartre 	 * of an error.
53922f5224aeSachartre 	 */
53932f5224aeSachartre 	if ((uscsi.uscsi_flags & USCSI_ISOLATE) ||
53942f5224aeSachartre 	    (uscsi.uscsi_flags & USCSI_DIAGNOSE))
53952f5224aeSachartre 		vd_scsi->options |= VD_SCSI_OPT_NORETRY;
53962f5224aeSachartre 
53972f5224aeSachartre 	/* set task attribute */
53982f5224aeSachartre 	if (uscsi.uscsi_flags & USCSI_NOTAG) {
53992f5224aeSachartre 		vd_scsi->task_attribute = 0;
54002f5224aeSachartre 	} else {
54012f5224aeSachartre 		if (uscsi.uscsi_flags & USCSI_HEAD)
54022f5224aeSachartre 			vd_scsi->task_attribute = VD_SCSI_TASK_ACA;
54032f5224aeSachartre 		else if (uscsi.uscsi_flags & USCSI_HTAG)
54042f5224aeSachartre 			vd_scsi->task_attribute = VD_SCSI_TASK_HQUEUE;
54052f5224aeSachartre 		else if (uscsi.uscsi_flags & USCSI_OTAG)
54062f5224aeSachartre 			vd_scsi->task_attribute = VD_SCSI_TASK_ORDERED;
54072f5224aeSachartre 		else
54082f5224aeSachartre 			vd_scsi->task_attribute = 0;
54092f5224aeSachartre 	}
54102f5224aeSachartre 
54112f5224aeSachartre 	/* set timeout */
54122f5224aeSachartre 	vd_scsi->timeout = uscsi.uscsi_timeout;
54132f5224aeSachartre 
54142f5224aeSachartre 	/* copy-in cdb data */
54152f5224aeSachartre 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
54162f5224aeSachartre 	if (ddi_copyin(uscsi.uscsi_cdb, cdb, cdb_len, mode) != 0) {
54172f5224aeSachartre 		rv = EFAULT;
54182f5224aeSachartre 		goto done;
54192f5224aeSachartre 	}
54202f5224aeSachartre 
54212f5224aeSachartre 	/* keep a pointer to the sense buffer */
54222f5224aeSachartre 	sense = VD_SCSI_DATA_SENSE(vd_scsi);
54232f5224aeSachartre 
54242f5224aeSachartre 	/* keep a pointer to the data-in buffer */
54252f5224aeSachartre 	datain = (char *)VD_SCSI_DATA_IN(vd_scsi);
54262f5224aeSachartre 
54272f5224aeSachartre 	/* copy-in request data to the data-out buffer */
54282f5224aeSachartre 	dataout = (char *)VD_SCSI_DATA_OUT(vd_scsi);
54292f5224aeSachartre 	if (!(uscsi.uscsi_flags & USCSI_READ)) {
54302f5224aeSachartre 		if (ddi_copyin(uscsi.uscsi_bufaddr, dataout, dataout_len,
54312f5224aeSachartre 		    mode)) {
54322f5224aeSachartre 			rv = EFAULT;
54332f5224aeSachartre 			goto done;
54342f5224aeSachartre 		}
54352f5224aeSachartre 	}
54362f5224aeSachartre 
54372f5224aeSachartre 	/* submit the request */
54382f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
54392f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
54402f5224aeSachartre 
54412f5224aeSachartre 	if (rv != 0)
54422f5224aeSachartre 		goto done;
54432f5224aeSachartre 
54442f5224aeSachartre 	/* update scsi status */
54452f5224aeSachartre 	uscsi.uscsi_status = vd_scsi->cmd_status;
54462f5224aeSachartre 
54472f5224aeSachartre 	/* update sense data */
54482f5224aeSachartre 	if ((uscsi.uscsi_flags & USCSI_RQENABLE) &&
54492f5224aeSachartre 	    (uscsi.uscsi_status == STATUS_CHECK ||
54502f5224aeSachartre 	    uscsi.uscsi_status == STATUS_TERMINATED)) {
54512f5224aeSachartre 
54522f5224aeSachartre 		uscsi.uscsi_rqstatus = vd_scsi->sense_status;
54532f5224aeSachartre 
54542f5224aeSachartre 		if (uscsi.uscsi_rqstatus == STATUS_GOOD) {
54552f5224aeSachartre 			uscsi.uscsi_rqresid = uscsi.uscsi_rqlen -
54562f5224aeSachartre 			    vd_scsi->sense_len;
54572f5224aeSachartre 			if (ddi_copyout(sense, uscsi.uscsi_rqbuf,
54582f5224aeSachartre 			    vd_scsi->sense_len, mode) != 0) {
54592f5224aeSachartre 				rv = EFAULT;
54602f5224aeSachartre 				goto done;
54612f5224aeSachartre 			}
54622f5224aeSachartre 		}
54632f5224aeSachartre 	}
54642f5224aeSachartre 
54652f5224aeSachartre 	/* update request data */
54662f5224aeSachartre 	if (uscsi.uscsi_status == STATUS_GOOD) {
54672f5224aeSachartre 		if (uscsi.uscsi_flags & USCSI_READ) {
54682f5224aeSachartre 			uscsi.uscsi_resid = uscsi.uscsi_buflen -
54692f5224aeSachartre 			    vd_scsi->datain_len;
54702f5224aeSachartre 			if (ddi_copyout(datain, uscsi.uscsi_bufaddr,
54712f5224aeSachartre 			    vd_scsi->datain_len, mode) != 0) {
54722f5224aeSachartre 				rv = EFAULT;
54732f5224aeSachartre 				goto done;
54742f5224aeSachartre 			}
54752f5224aeSachartre 		} else {
54762f5224aeSachartre 			uscsi.uscsi_resid = uscsi.uscsi_buflen -
54772f5224aeSachartre 			    vd_scsi->dataout_len;
54782f5224aeSachartre 		}
54792f5224aeSachartre 	}
54802f5224aeSachartre 
54812f5224aeSachartre 	/* copy-out result */
54822f5224aeSachartre 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
54832f5224aeSachartre 		uscsi_cmdtouscsi_cmd32((&uscsi), (&uscsi32));
54842f5224aeSachartre 		if (ddi_copyout(&uscsi32, arg, sizeof (struct uscsi_cmd32),
54852f5224aeSachartre 		    mode) != 0) {
54862f5224aeSachartre 			rv = EFAULT;
54872f5224aeSachartre 			goto done;
54882f5224aeSachartre 		}
54892f5224aeSachartre 	} else {
54902f5224aeSachartre 		if (ddi_copyout(&uscsi, arg, sizeof (struct uscsi_cmd),
54912f5224aeSachartre 		    mode) != 0) {
54922f5224aeSachartre 			rv = EFAULT;
54932f5224aeSachartre 			goto done;
54942f5224aeSachartre 		}
54952f5224aeSachartre 	}
54962f5224aeSachartre 
54972f5224aeSachartre 	/* get the return code from the SCSI command status */
54982f5224aeSachartre 	rv = vdc_scsi_status(vdc, vd_scsi,
54992f5224aeSachartre 	    !(uscsi.uscsi_flags & USCSI_SILENT));
55002f5224aeSachartre 
55012f5224aeSachartre done:
55022f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
55032f5224aeSachartre 	return (rv);
55042f5224aeSachartre }
55052f5224aeSachartre 
55062f5224aeSachartre /*
55072f5224aeSachartre  * Create a VD_OP_SCSICMD buffer for a SCSI PERSISTENT IN command.
55082f5224aeSachartre  *
55092f5224aeSachartre  * Arguments:
55102f5224aeSachartre  *	cmd		- SCSI PERSISTENT IN command
55112f5224aeSachartre  *	len		- length of the SCSI input buffer
55122f5224aeSachartre  *	vd_scsi_len	- return the length of the allocated buffer
55132f5224aeSachartre  *
55142f5224aeSachartre  * Returned Value:
55152f5224aeSachartre  *	a pointer to the allocated VD_OP_SCSICMD buffer.
55162f5224aeSachartre  */
55172f5224aeSachartre static vd_scsi_t *
55182f5224aeSachartre vdc_scsi_alloc_persistent_in(uchar_t cmd, int len, int *vd_scsi_len)
55192f5224aeSachartre {
55202f5224aeSachartre 	int cdb_len, sense_len, datain_len, dataout_len;
55212f5224aeSachartre 	vd_scsi_t *vd_scsi;
55222f5224aeSachartre 	union scsi_cdb *cdb;
55232f5224aeSachartre 
55242f5224aeSachartre 	cdb_len = CDB_GROUP1;
55252f5224aeSachartre 	sense_len = sizeof (struct scsi_extended_sense);
55262f5224aeSachartre 	datain_len = len;
55272f5224aeSachartre 	dataout_len = 0;
55282f5224aeSachartre 
55292f5224aeSachartre 	vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, datain_len, dataout_len,
55302f5224aeSachartre 	    vd_scsi_len);
55312f5224aeSachartre 
55322f5224aeSachartre 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
55332f5224aeSachartre 
55342f5224aeSachartre 	/* set cdb */
55352f5224aeSachartre 	cdb->scc_cmd = SCMD_PERSISTENT_RESERVE_IN;
55362f5224aeSachartre 	cdb->cdb_opaque[1] = cmd;
55372f5224aeSachartre 	FORMG1COUNT(cdb, datain_len);
55382f5224aeSachartre 
55392f5224aeSachartre 	vd_scsi->timeout = vdc_scsi_timeout;
55402f5224aeSachartre 
55412f5224aeSachartre 	return (vd_scsi);
55422f5224aeSachartre }
55432f5224aeSachartre 
55442f5224aeSachartre /*
55452f5224aeSachartre  * Create a VD_OP_SCSICMD buffer for a SCSI PERSISTENT OUT command.
55462f5224aeSachartre  *
55472f5224aeSachartre  * Arguments:
55482f5224aeSachartre  *	cmd		- SCSI PERSISTENT OUT command
55492f5224aeSachartre  *	len		- length of the SCSI output buffer
55502f5224aeSachartre  *	vd_scsi_len	- return the length of the allocated buffer
55512f5224aeSachartre  *
55522f5224aeSachartre  * Returned Code:
55532f5224aeSachartre  *	a pointer to the allocated VD_OP_SCSICMD buffer.
55542f5224aeSachartre  */
55552f5224aeSachartre static vd_scsi_t *
55562f5224aeSachartre vdc_scsi_alloc_persistent_out(uchar_t cmd, int len, int *vd_scsi_len)
55572f5224aeSachartre {
55582f5224aeSachartre 	int cdb_len, sense_len, datain_len, dataout_len;
55592f5224aeSachartre 	vd_scsi_t *vd_scsi;
55602f5224aeSachartre 	union scsi_cdb *cdb;
55612f5224aeSachartre 
55622f5224aeSachartre 	cdb_len = CDB_GROUP1;
55632f5224aeSachartre 	sense_len = sizeof (struct scsi_extended_sense);
55642f5224aeSachartre 	datain_len = 0;
55652f5224aeSachartre 	dataout_len = len;
55662f5224aeSachartre 
55672f5224aeSachartre 	vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, datain_len, dataout_len,
55682f5224aeSachartre 	    vd_scsi_len);
55692f5224aeSachartre 
55702f5224aeSachartre 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
55712f5224aeSachartre 
55722f5224aeSachartre 	/* set cdb */
55732f5224aeSachartre 	cdb->scc_cmd = SCMD_PERSISTENT_RESERVE_OUT;
55742f5224aeSachartre 	cdb->cdb_opaque[1] = cmd;
55752f5224aeSachartre 	FORMG1COUNT(cdb, dataout_len);
55762f5224aeSachartre 
55772f5224aeSachartre 	vd_scsi->timeout = vdc_scsi_timeout;
55782f5224aeSachartre 
55792f5224aeSachartre 	return (vd_scsi);
55802f5224aeSachartre }
55812f5224aeSachartre 
55822f5224aeSachartre /*
55832f5224aeSachartre  * Implement the MHIOCGRP_INKEYS mhd(7i) ioctl. The ioctl is converted
55842f5224aeSachartre  * to a SCSI PERSISTENT IN READ KEYS command which is sent to the vdisk
55852f5224aeSachartre  * server with a VD_OP_SCSICMD operation.
55862f5224aeSachartre  */
55872f5224aeSachartre static int
55882f5224aeSachartre vdc_mhd_inkeys(vdc_t *vdc, caddr_t arg, int mode)
55892f5224aeSachartre {
55902f5224aeSachartre 	vd_scsi_t *vd_scsi;
55912f5224aeSachartre 	mhioc_inkeys_t inkeys;
55922f5224aeSachartre 	mhioc_key_list_t klist;
55932f5224aeSachartre 	struct mhioc_inkeys32 inkeys32;
55942f5224aeSachartre 	struct mhioc_key_list32 klist32;
55952f5224aeSachartre 	sd_prin_readkeys_t *scsi_keys;
55962f5224aeSachartre 	void *user_keys;
55972f5224aeSachartre 	int vd_scsi_len;
55982f5224aeSachartre 	int listsize, listlen, rv;
55992f5224aeSachartre 
56002f5224aeSachartre 	/* copyin arguments */
56012f5224aeSachartre 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
56022f5224aeSachartre 		rv = ddi_copyin(arg, &inkeys32, sizeof (inkeys32), mode);
56032f5224aeSachartre 		if (rv != 0)
56042f5224aeSachartre 			return (EFAULT);
56052f5224aeSachartre 
56062f5224aeSachartre 		rv = ddi_copyin((caddr_t)(uintptr_t)inkeys32.li, &klist32,
56072f5224aeSachartre 		    sizeof (klist32), mode);
56082f5224aeSachartre 		if (rv != 0)
56092f5224aeSachartre 			return (EFAULT);
56102f5224aeSachartre 
56112f5224aeSachartre 		listsize = klist32.listsize;
56122f5224aeSachartre 	} else {
56132f5224aeSachartre 		rv = ddi_copyin(arg, &inkeys, sizeof (inkeys), mode);
56142f5224aeSachartre 		if (rv != 0)
56152f5224aeSachartre 			return (EFAULT);
56162f5224aeSachartre 
56172f5224aeSachartre 		rv = ddi_copyin(inkeys.li, &klist, sizeof (klist), mode);
56182f5224aeSachartre 		if (rv != 0)
56192f5224aeSachartre 			return (EFAULT);
56202f5224aeSachartre 
56212f5224aeSachartre 		listsize = klist.listsize;
56222f5224aeSachartre 	}
56232f5224aeSachartre 
56242f5224aeSachartre 	/* build SCSI VD_OP request */
56252f5224aeSachartre 	vd_scsi = vdc_scsi_alloc_persistent_in(SD_READ_KEYS,
56262f5224aeSachartre 	    sizeof (sd_prin_readkeys_t) - sizeof (caddr_t) +
56272f5224aeSachartre 	    (sizeof (mhioc_resv_key_t) * listsize), &vd_scsi_len);
56282f5224aeSachartre 
56292f5224aeSachartre 	scsi_keys = (sd_prin_readkeys_t *)VD_SCSI_DATA_IN(vd_scsi);
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 	listlen = scsi_keys->len / MHIOC_RESV_KEY_SIZE;
56392f5224aeSachartre 
56402f5224aeSachartre 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
56412f5224aeSachartre 		inkeys32.generation = scsi_keys->generation;
56422f5224aeSachartre 		rv = ddi_copyout(&inkeys32, arg, sizeof (inkeys32), mode);
56432f5224aeSachartre 		if (rv != 0) {
56442f5224aeSachartre 			rv = EFAULT;
56452f5224aeSachartre 			goto done;
56462f5224aeSachartre 		}
56472f5224aeSachartre 
56482f5224aeSachartre 		klist32.listlen = listlen;
56492f5224aeSachartre 		rv = ddi_copyout(&klist32, (caddr_t)(uintptr_t)inkeys32.li,
56502f5224aeSachartre 		    sizeof (klist32), mode);
56512f5224aeSachartre 		if (rv != 0) {
56522f5224aeSachartre 			rv = EFAULT;
56532f5224aeSachartre 			goto done;
56542f5224aeSachartre 		}
56552f5224aeSachartre 
56562f5224aeSachartre 		user_keys = (caddr_t)(uintptr_t)klist32.list;
56572f5224aeSachartre 	} else {
56582f5224aeSachartre 		inkeys.generation = scsi_keys->generation;
56592f5224aeSachartre 		rv = ddi_copyout(&inkeys, arg, sizeof (inkeys), mode);
56602f5224aeSachartre 		if (rv != 0) {
56612f5224aeSachartre 			rv = EFAULT;
56622f5224aeSachartre 			goto done;
56632f5224aeSachartre 		}
56642f5224aeSachartre 
56652f5224aeSachartre 		klist.listlen = listlen;
56662f5224aeSachartre 		rv = ddi_copyout(&klist, inkeys.li, sizeof (klist), mode);
56672f5224aeSachartre 		if (rv != 0) {
56682f5224aeSachartre 			rv = EFAULT;
56692f5224aeSachartre 			goto done;
56702f5224aeSachartre 		}
56712f5224aeSachartre 
56722f5224aeSachartre 		user_keys = klist.list;
56732f5224aeSachartre 	}
56742f5224aeSachartre 
56752f5224aeSachartre 	/* copy out keys */
56762f5224aeSachartre 	if (listlen > 0 && listsize > 0) {
56772f5224aeSachartre 		if (listsize < listlen)
56782f5224aeSachartre 			listlen = listsize;
56792f5224aeSachartre 		rv = ddi_copyout(&scsi_keys->keylist, user_keys,
56802f5224aeSachartre 		    listlen * MHIOC_RESV_KEY_SIZE, mode);
56812f5224aeSachartre 		if (rv != 0)
56822f5224aeSachartre 			rv = EFAULT;
56832f5224aeSachartre 	}
56842f5224aeSachartre 
56852f5224aeSachartre 	if (rv == 0)
56862f5224aeSachartre 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
56872f5224aeSachartre 
56882f5224aeSachartre done:
56892f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
56902f5224aeSachartre 
56912f5224aeSachartre 	return (rv);
56922f5224aeSachartre }
56932f5224aeSachartre 
56942f5224aeSachartre /*
56952f5224aeSachartre  * Implement the MHIOCGRP_INRESV mhd(7i) ioctl. The ioctl is converted
56962f5224aeSachartre  * to a SCSI PERSISTENT IN READ RESERVATION command which is sent to
56972f5224aeSachartre  * the vdisk server with a VD_OP_SCSICMD operation.
56982f5224aeSachartre  */
56992f5224aeSachartre static int
57002f5224aeSachartre vdc_mhd_inresv(vdc_t *vdc, caddr_t arg, int mode)
57012f5224aeSachartre {
57022f5224aeSachartre 	vd_scsi_t *vd_scsi;
57032f5224aeSachartre 	mhioc_inresvs_t inresv;
57042f5224aeSachartre 	mhioc_resv_desc_list_t rlist;
57052f5224aeSachartre 	struct mhioc_inresvs32 inresv32;
57062f5224aeSachartre 	struct mhioc_resv_desc_list32 rlist32;
57072f5224aeSachartre 	mhioc_resv_desc_t mhd_resv;
57082f5224aeSachartre 	sd_prin_readresv_t *scsi_resv;
57092f5224aeSachartre 	sd_readresv_desc_t *resv;
57102f5224aeSachartre 	mhioc_resv_desc_t *user_resv;
57112f5224aeSachartre 	int vd_scsi_len;
57122f5224aeSachartre 	int listsize, listlen, i, rv;
57132f5224aeSachartre 
57142f5224aeSachartre 	/* copyin arguments */
57152f5224aeSachartre 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
57162f5224aeSachartre 		rv = ddi_copyin(arg, &inresv32, sizeof (inresv32), mode);
57172f5224aeSachartre 		if (rv != 0)
57182f5224aeSachartre 			return (EFAULT);
57192f5224aeSachartre 
57202f5224aeSachartre 		rv = ddi_copyin((caddr_t)(uintptr_t)inresv32.li, &rlist32,
57212f5224aeSachartre 		    sizeof (rlist32), mode);
57222f5224aeSachartre 		if (rv != 0)
57232f5224aeSachartre 			return (EFAULT);
57242f5224aeSachartre 
57252f5224aeSachartre 		listsize = rlist32.listsize;
57262f5224aeSachartre 	} else {
57272f5224aeSachartre 		rv = ddi_copyin(arg, &inresv, sizeof (inresv), mode);
57282f5224aeSachartre 		if (rv != 0)
57292f5224aeSachartre 			return (EFAULT);
57302f5224aeSachartre 
57312f5224aeSachartre 		rv = ddi_copyin(inresv.li, &rlist, sizeof (rlist), mode);
57322f5224aeSachartre 		if (rv != 0)
57332f5224aeSachartre 			return (EFAULT);
57342f5224aeSachartre 
57352f5224aeSachartre 		listsize = rlist.listsize;
57362f5224aeSachartre 	}
57372f5224aeSachartre 
57382f5224aeSachartre 	/* build SCSI VD_OP request */
57392f5224aeSachartre 	vd_scsi = vdc_scsi_alloc_persistent_in(SD_READ_RESV,
57402f5224aeSachartre 	    sizeof (sd_prin_readresv_t) - sizeof (caddr_t) +
57412f5224aeSachartre 	    (SCSI3_RESV_DESC_LEN * listsize), &vd_scsi_len);
57422f5224aeSachartre 
57432f5224aeSachartre 	scsi_resv = (sd_prin_readresv_t *)VD_SCSI_DATA_IN(vd_scsi);
57442f5224aeSachartre 
57452f5224aeSachartre 	/* submit the request */
57462f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
57472f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
57482f5224aeSachartre 
57492f5224aeSachartre 	if (rv != 0)
57502f5224aeSachartre 		goto done;
57512f5224aeSachartre 
57522f5224aeSachartre 	listlen = scsi_resv->len / SCSI3_RESV_DESC_LEN;
57532f5224aeSachartre 
57542f5224aeSachartre 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
57552f5224aeSachartre 		inresv32.generation = scsi_resv->generation;
57562f5224aeSachartre 		rv = ddi_copyout(&inresv32, arg, sizeof (inresv32), mode);
57572f5224aeSachartre 		if (rv != 0) {
57582f5224aeSachartre 			rv = EFAULT;
57592f5224aeSachartre 			goto done;
57602f5224aeSachartre 		}
57612f5224aeSachartre 
57622f5224aeSachartre 		rlist32.listlen = listlen;
57632f5224aeSachartre 		rv = ddi_copyout(&rlist32, (caddr_t)(uintptr_t)inresv32.li,
57642f5224aeSachartre 		    sizeof (rlist32), mode);
57652f5224aeSachartre 		if (rv != 0) {
57662f5224aeSachartre 			rv = EFAULT;
57672f5224aeSachartre 			goto done;
57682f5224aeSachartre 		}
57692f5224aeSachartre 
57702f5224aeSachartre 		user_resv = (mhioc_resv_desc_t *)(uintptr_t)rlist32.list;
57712f5224aeSachartre 	} else {
57722f5224aeSachartre 		inresv.generation = scsi_resv->generation;
57732f5224aeSachartre 		rv = ddi_copyout(&inresv, arg, sizeof (inresv), mode);
57742f5224aeSachartre 		if (rv != 0) {
57752f5224aeSachartre 			rv = EFAULT;
57762f5224aeSachartre 			goto done;
57772f5224aeSachartre 		}
57782f5224aeSachartre 
57792f5224aeSachartre 		rlist.listlen = listlen;
57802f5224aeSachartre 		rv = ddi_copyout(&rlist, inresv.li, sizeof (rlist), mode);
57812f5224aeSachartre 		if (rv != 0) {
57822f5224aeSachartre 			rv = EFAULT;
57832f5224aeSachartre 			goto done;
57842f5224aeSachartre 		}
57852f5224aeSachartre 
57862f5224aeSachartre 		user_resv = rlist.list;
57872f5224aeSachartre 	}
57882f5224aeSachartre 
57892f5224aeSachartre 	/* copy out reservations */
57902f5224aeSachartre 	if (listsize > 0 && listlen > 0) {
57912f5224aeSachartre 		if (listsize < listlen)
57922f5224aeSachartre 			listlen = listsize;
57932f5224aeSachartre 		resv = (sd_readresv_desc_t *)&scsi_resv->readresv_desc;
57942f5224aeSachartre 
57952f5224aeSachartre 		for (i = 0; i < listlen; i++) {
57962f5224aeSachartre 			mhd_resv.type = resv->type;
57972f5224aeSachartre 			mhd_resv.scope = resv->scope;
57982f5224aeSachartre 			mhd_resv.scope_specific_addr =
57992f5224aeSachartre 			    BE_32(resv->scope_specific_addr);
58002f5224aeSachartre 			bcopy(&resv->resvkey, &mhd_resv.key,
58012f5224aeSachartre 			    MHIOC_RESV_KEY_SIZE);
58022f5224aeSachartre 
58032f5224aeSachartre 			rv = ddi_copyout(&mhd_resv, user_resv,
58042f5224aeSachartre 			    sizeof (mhd_resv), mode);
58052f5224aeSachartre 			if (rv != 0) {
58062f5224aeSachartre 				rv = EFAULT;
58072f5224aeSachartre 				goto done;
58082f5224aeSachartre 			}
58092f5224aeSachartre 			resv++;
58102f5224aeSachartre 			user_resv++;
58112f5224aeSachartre 		}
58122f5224aeSachartre 	}
58132f5224aeSachartre 
58142f5224aeSachartre 	if (rv == 0)
58152f5224aeSachartre 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
58162f5224aeSachartre 
58172f5224aeSachartre done:
58182f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
58192f5224aeSachartre 	return (rv);
58202f5224aeSachartre }
58212f5224aeSachartre 
58222f5224aeSachartre /*
58232f5224aeSachartre  * Implement the MHIOCGRP_REGISTER mhd(7i) ioctl. The ioctl is converted
58242f5224aeSachartre  * to a SCSI PERSISTENT OUT REGISTER command which is sent to the vdisk
58252f5224aeSachartre  * server with a VD_OP_SCSICMD operation.
58262f5224aeSachartre  */
58272f5224aeSachartre static int
58282f5224aeSachartre vdc_mhd_register(vdc_t *vdc, caddr_t arg, int mode)
58292f5224aeSachartre {
58302f5224aeSachartre 	vd_scsi_t *vd_scsi;
58312f5224aeSachartre 	sd_prout_t *scsi_prout;
58322f5224aeSachartre 	mhioc_register_t mhd_reg;
58332f5224aeSachartre 	int vd_scsi_len, rv;
58342f5224aeSachartre 
58352f5224aeSachartre 	/* copyin arguments */
58362f5224aeSachartre 	rv = ddi_copyin(arg, &mhd_reg, sizeof (mhd_reg), mode);
58372f5224aeSachartre 	if (rv != 0)
58382f5224aeSachartre 		return (EFAULT);
58392f5224aeSachartre 
58402f5224aeSachartre 	/* build SCSI VD_OP request */
58412f5224aeSachartre 	vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_REGISTER,
58422f5224aeSachartre 	    sizeof (sd_prout_t), &vd_scsi_len);
58432f5224aeSachartre 
58442f5224aeSachartre 	/* set parameters */
58452f5224aeSachartre 	scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi);
58462f5224aeSachartre 	bcopy(mhd_reg.oldkey.key, scsi_prout->res_key, MHIOC_RESV_KEY_SIZE);
58472f5224aeSachartre 	bcopy(mhd_reg.newkey.key, scsi_prout->service_key, MHIOC_RESV_KEY_SIZE);
58482f5224aeSachartre 	scsi_prout->aptpl = (uchar_t)mhd_reg.aptpl;
58492f5224aeSachartre 
58502f5224aeSachartre 	/* submit the request */
58512f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
58522f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
58532f5224aeSachartre 
58542f5224aeSachartre 	if (rv == 0)
58552f5224aeSachartre 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
58562f5224aeSachartre 
58572f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
58582f5224aeSachartre 	return (rv);
58592f5224aeSachartre }
58602f5224aeSachartre 
58612f5224aeSachartre /*
58622f5224aeSachartre  * Implement the MHIOCGRP_RESERVE mhd(7i) ioctl. The ioctl is converted
58632f5224aeSachartre  * to a SCSI PERSISTENT OUT RESERVE command which is sent to the vdisk
58642f5224aeSachartre  * server with a VD_OP_SCSICMD operation.
58652f5224aeSachartre  */
58662f5224aeSachartre static int
58672f5224aeSachartre vdc_mhd_reserve(vdc_t *vdc, caddr_t arg, int mode)
58682f5224aeSachartre {
58692f5224aeSachartre 	union scsi_cdb *cdb;
58702f5224aeSachartre 	vd_scsi_t *vd_scsi;
58712f5224aeSachartre 	sd_prout_t *scsi_prout;
58722f5224aeSachartre 	mhioc_resv_desc_t mhd_resv;
58732f5224aeSachartre 	int vd_scsi_len, rv;
58742f5224aeSachartre 
58752f5224aeSachartre 	/* copyin arguments */
58762f5224aeSachartre 	rv = ddi_copyin(arg, &mhd_resv, sizeof (mhd_resv), mode);
58772f5224aeSachartre 	if (rv != 0)
58782f5224aeSachartre 		return (EFAULT);
58792f5224aeSachartre 
58802f5224aeSachartre 	/* build SCSI VD_OP request */
58812f5224aeSachartre 	vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_RESERVE,
58822f5224aeSachartre 	    sizeof (sd_prout_t), &vd_scsi_len);
58832f5224aeSachartre 
58842f5224aeSachartre 	/* set parameters */
58852f5224aeSachartre 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
58862f5224aeSachartre 	scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi);
58872f5224aeSachartre 	bcopy(mhd_resv.key.key, scsi_prout->res_key, MHIOC_RESV_KEY_SIZE);
58882f5224aeSachartre 	scsi_prout->scope_address = mhd_resv.scope_specific_addr;
58892f5224aeSachartre 	cdb->cdb_opaque[2] = mhd_resv.type;
58902f5224aeSachartre 
58912f5224aeSachartre 	/* submit the request */
58922f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
58932f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
58942f5224aeSachartre 
58952f5224aeSachartre 	if (rv == 0)
58962f5224aeSachartre 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
58972f5224aeSachartre 
58982f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
58992f5224aeSachartre 	return (rv);
59002f5224aeSachartre }
59012f5224aeSachartre 
59022f5224aeSachartre /*
59032f5224aeSachartre  * Implement the MHIOCGRP_PREEMPTANDABORT mhd(7i) ioctl. The ioctl is
59042f5224aeSachartre  * converted to a SCSI PERSISTENT OUT PREEMPT AND ABORT command which
59052f5224aeSachartre  * is sent to the vdisk server with a VD_OP_SCSICMD operation.
59062f5224aeSachartre  */
59072f5224aeSachartre static int
59082f5224aeSachartre vdc_mhd_preemptabort(vdc_t *vdc, caddr_t arg, int mode)
59092f5224aeSachartre {
59102f5224aeSachartre 	union scsi_cdb *cdb;
59112f5224aeSachartre 	vd_scsi_t *vd_scsi;
59122f5224aeSachartre 	sd_prout_t *scsi_prout;
59132f5224aeSachartre 	mhioc_preemptandabort_t mhd_preempt;
59142f5224aeSachartre 	int vd_scsi_len, rv;
59152f5224aeSachartre 
59162f5224aeSachartre 	/* copyin arguments */
59172f5224aeSachartre 	rv = ddi_copyin(arg, &mhd_preempt, sizeof (mhd_preempt), mode);
59182f5224aeSachartre 	if (rv != 0)
59192f5224aeSachartre 		return (EFAULT);
59202f5224aeSachartre 
59212f5224aeSachartre 	/* build SCSI VD_OP request */
59222f5224aeSachartre 	vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_PREEMPTANDABORT,
59232f5224aeSachartre 	    sizeof (sd_prout_t), &vd_scsi_len);
59242f5224aeSachartre 
59252f5224aeSachartre 	/* set parameters */
59262f5224aeSachartre 	vd_scsi->task_attribute = VD_SCSI_TASK_ACA;
59272f5224aeSachartre 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
59282f5224aeSachartre 	scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi);
59292f5224aeSachartre 	bcopy(mhd_preempt.resvdesc.key.key, scsi_prout->res_key,
59302f5224aeSachartre 	    MHIOC_RESV_KEY_SIZE);
59312f5224aeSachartre 	bcopy(mhd_preempt.victim_key.key, scsi_prout->service_key,
59322f5224aeSachartre 	    MHIOC_RESV_KEY_SIZE);
59332f5224aeSachartre 	scsi_prout->scope_address = mhd_preempt.resvdesc.scope_specific_addr;
59342f5224aeSachartre 	cdb->cdb_opaque[2] = mhd_preempt.resvdesc.type;
59352f5224aeSachartre 
59362f5224aeSachartre 	/* submit the request */
59372f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
59382f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
59392f5224aeSachartre 
59402f5224aeSachartre 	if (rv == 0)
59412f5224aeSachartre 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
59422f5224aeSachartre 
59432f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
59442f5224aeSachartre 	return (rv);
59452f5224aeSachartre }
59462f5224aeSachartre 
59472f5224aeSachartre /*
59482f5224aeSachartre  * Implement the MHIOCGRP_REGISTERANDIGNOREKEY mhd(7i) ioctl. The ioctl
59492f5224aeSachartre  * is converted to a SCSI PERSISTENT OUT REGISTER AND IGNORE EXISTING KEY
59502f5224aeSachartre  * command which is sent to the vdisk server with a VD_OP_SCSICMD operation.
59512f5224aeSachartre  */
59522f5224aeSachartre static int
59532f5224aeSachartre vdc_mhd_registerignore(vdc_t *vdc, caddr_t arg, int mode)
59542f5224aeSachartre {
59552f5224aeSachartre 	vd_scsi_t *vd_scsi;
59562f5224aeSachartre 	sd_prout_t *scsi_prout;
59572f5224aeSachartre 	mhioc_registerandignorekey_t mhd_regi;
59582f5224aeSachartre 	int vd_scsi_len, rv;
59592f5224aeSachartre 
59602f5224aeSachartre 	/* copyin arguments */
59612f5224aeSachartre 	rv = ddi_copyin(arg, &mhd_regi, sizeof (mhd_regi), mode);
59622f5224aeSachartre 	if (rv != 0)
59632f5224aeSachartre 		return (EFAULT);
59642f5224aeSachartre 
59652f5224aeSachartre 	/* build SCSI VD_OP request */
59662f5224aeSachartre 	vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_REGISTERANDIGNOREKEY,
59672f5224aeSachartre 	    sizeof (sd_prout_t), &vd_scsi_len);
59682f5224aeSachartre 
59692f5224aeSachartre 	/* set parameters */
59702f5224aeSachartre 	scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi);
59712f5224aeSachartre 	bcopy(mhd_regi.newkey.key, scsi_prout->service_key,
59722f5224aeSachartre 	    MHIOC_RESV_KEY_SIZE);
59732f5224aeSachartre 	scsi_prout->aptpl = (uchar_t)mhd_regi.aptpl;
59742f5224aeSachartre 
59752f5224aeSachartre 	/* submit the request */
59762f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
59772f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
59782f5224aeSachartre 
59792f5224aeSachartre 	if (rv == 0)
59802f5224aeSachartre 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
59812f5224aeSachartre 
59822f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
59832f5224aeSachartre 	return (rv);
59842f5224aeSachartre }
59852f5224aeSachartre 
59862f5224aeSachartre /*
59872f5224aeSachartre  * This function is used by the failfast mechanism to send a SCSI command
59882f5224aeSachartre  * to check for reservation conflict.
59892f5224aeSachartre  */
59902f5224aeSachartre static int
59912f5224aeSachartre vdc_failfast_scsi_cmd(vdc_t *vdc, uchar_t scmd)
59922f5224aeSachartre {
59932f5224aeSachartre 	int cdb_len, sense_len, vd_scsi_len;
59942f5224aeSachartre 	vd_scsi_t *vd_scsi;
59952f5224aeSachartre 	union scsi_cdb *cdb;
59962f5224aeSachartre 	int rv;
59972f5224aeSachartre 
59982f5224aeSachartre 	ASSERT(scmd == SCMD_TEST_UNIT_READY || scmd == SCMD_WRITE_G1);
59992f5224aeSachartre 
60002f5224aeSachartre 	if (scmd == SCMD_WRITE_G1)
60012f5224aeSachartre 		cdb_len = CDB_GROUP1;
60022f5224aeSachartre 	else
60032f5224aeSachartre 		cdb_len = CDB_GROUP0;
60042f5224aeSachartre 
60052f5224aeSachartre 	sense_len = sizeof (struct scsi_extended_sense);
60062f5224aeSachartre 
60072f5224aeSachartre 	vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, 0, 0, &vd_scsi_len);
60082f5224aeSachartre 
60092f5224aeSachartre 	/* set cdb */
60102f5224aeSachartre 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
60112f5224aeSachartre 	cdb->scc_cmd = scmd;
60122f5224aeSachartre 
60132f5224aeSachartre 	vd_scsi->timeout = vdc_scsi_timeout;
60142f5224aeSachartre 
60152f5224aeSachartre 	/*
60162f5224aeSachartre 	 * Submit the request. The last argument has to be B_FALSE so that
60172f5224aeSachartre 	 * vdc_do_sync_op does not loop checking for reservation conflict if
60182f5224aeSachartre 	 * the operation returns an error.
60192f5224aeSachartre 	 */
60202f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
60212f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)FKIOCTL, VIO_both_dir, B_FALSE);
60222f5224aeSachartre 
60232f5224aeSachartre 	if (rv == 0)
60242f5224aeSachartre 		(void) vdc_scsi_status(vdc, vd_scsi, B_FALSE);
60252f5224aeSachartre 
60262f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
60272f5224aeSachartre 	return (rv);
60282f5224aeSachartre }
60292f5224aeSachartre 
60302f5224aeSachartre /*
60312f5224aeSachartre  * This function is used by the failfast mechanism to check for reservation
60322f5224aeSachartre  * conflict. It sends some SCSI commands which will fail with a reservation
60332f5224aeSachartre  * conflict error if the system does not have access to the disk and this
60342f5224aeSachartre  * will panic the system.
60352f5224aeSachartre  *
60362f5224aeSachartre  * Returned Code:
60372f5224aeSachartre  *	0	- disk is accessible without reservation conflict error
60382f5224aeSachartre  *	!= 0	- unable to check if disk is accessible
60392f5224aeSachartre  */
60402f5224aeSachartre int
60412f5224aeSachartre vdc_failfast_check_resv(vdc_t *vdc)
60422f5224aeSachartre {
60432f5224aeSachartre 	int failure = 0;
60442f5224aeSachartre 
60452f5224aeSachartre 	/*
60462f5224aeSachartre 	 * Send a TEST UNIT READY command. The command will panic
60472f5224aeSachartre 	 * the system if it fails with a reservation conflict.
60482f5224aeSachartre 	 */
60492f5224aeSachartre 	if (vdc_failfast_scsi_cmd(vdc, SCMD_TEST_UNIT_READY) != 0)
60502f5224aeSachartre 		failure++;
60512f5224aeSachartre 
60522f5224aeSachartre 	/*
60532f5224aeSachartre 	 * With SPC-3 compliant devices TEST UNIT READY will succeed on
60542f5224aeSachartre 	 * a reserved device, so we also do a WRITE(10) of zero byte in
60552f5224aeSachartre 	 * order to provoke a Reservation Conflict status on those newer
60562f5224aeSachartre 	 * devices.
60572f5224aeSachartre 	 */
60582f5224aeSachartre 	if (vdc_failfast_scsi_cmd(vdc, SCMD_WRITE_G1) != 0)
60592f5224aeSachartre 		failure++;
60602f5224aeSachartre 
60612f5224aeSachartre 	return (failure);
60622f5224aeSachartre }
60632f5224aeSachartre 
60642f5224aeSachartre /*
60652f5224aeSachartre  * Add a pending I/O to the failfast I/O queue. An I/O is added to this
60662f5224aeSachartre  * queue when it has failed and failfast is enabled. Then we have to check
60672f5224aeSachartre  * if it has failed because of a reservation conflict in which case we have
60682f5224aeSachartre  * to panic the system.
60692f5224aeSachartre  *
60702f5224aeSachartre  * Async I/O should be queued with their block I/O data transfer structure
60712f5224aeSachartre  * (buf). Sync I/O should be queued with buf = NULL.
60722f5224aeSachartre  */
60732f5224aeSachartre static vdc_io_t *
60742f5224aeSachartre vdc_failfast_io_queue(vdc_t *vdc, struct buf *buf)
60752f5224aeSachartre {
60762f5224aeSachartre 	vdc_io_t *vio;
60772f5224aeSachartre 
60782f5224aeSachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
60792f5224aeSachartre 
60802f5224aeSachartre 	vio = kmem_alloc(sizeof (vdc_io_t), KM_SLEEP);
60812f5224aeSachartre 	vio->vio_next = vdc->failfast_io_queue;
60822f5224aeSachartre 	vio->vio_buf = buf;
60832f5224aeSachartre 	vio->vio_qtime = ddi_get_lbolt();
60842f5224aeSachartre 
60852f5224aeSachartre 	vdc->failfast_io_queue = vio;
60862f5224aeSachartre 
60872f5224aeSachartre 	/* notify the failfast thread that a new I/O is queued */
60882f5224aeSachartre 	cv_signal(&vdc->failfast_cv);
60892f5224aeSachartre 
60902f5224aeSachartre 	return (vio);
60912f5224aeSachartre }
60922f5224aeSachartre 
60932f5224aeSachartre /*
60942f5224aeSachartre  * Remove and complete I/O in the failfast I/O queue which have been
60952f5224aeSachartre  * added after the indicated deadline. A deadline of 0 means that all
60962f5224aeSachartre  * I/O have to be unqueued and marked as completed.
60972f5224aeSachartre  */
60982f5224aeSachartre static void
60992f5224aeSachartre vdc_failfast_io_unqueue(vdc_t *vdc, clock_t deadline)
61002f5224aeSachartre {
61012f5224aeSachartre 	vdc_io_t *vio, *vio_tmp;
61022f5224aeSachartre 
61032f5224aeSachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
61042f5224aeSachartre 
61052f5224aeSachartre 	vio_tmp = NULL;
61062f5224aeSachartre 	vio = vdc->failfast_io_queue;
61072f5224aeSachartre 
61082f5224aeSachartre 	if (deadline != 0) {
61092f5224aeSachartre 		/*
61102f5224aeSachartre 		 * Skip any io queued after the deadline. The failfast
61112f5224aeSachartre 		 * I/O queue is ordered starting with the last I/O added
61122f5224aeSachartre 		 * to the queue.
61132f5224aeSachartre 		 */
61142f5224aeSachartre 		while (vio != NULL && vio->vio_qtime > deadline) {
61152f5224aeSachartre 			vio_tmp = vio;
61162f5224aeSachartre 			vio = vio->vio_next;
61172f5224aeSachartre 		}
61182f5224aeSachartre 	}
61192f5224aeSachartre 
61202f5224aeSachartre 	if (vio == NULL)
61212f5224aeSachartre 		/* nothing to unqueue */
61222f5224aeSachartre 		return;
61232f5224aeSachartre 
61242f5224aeSachartre 	/* update the queue */
61252f5224aeSachartre 	if (vio_tmp == NULL)
61262f5224aeSachartre 		vdc->failfast_io_queue = NULL;
61272f5224aeSachartre 	else
61282f5224aeSachartre 		vio_tmp->vio_next = NULL;
61292f5224aeSachartre 
61302f5224aeSachartre 	/*
61312f5224aeSachartre 	 * Complete unqueued I/O. Async I/O have a block I/O data transfer
61322f5224aeSachartre 	 * structure (buf) and they are completed by calling biodone(). Sync
61332f5224aeSachartre 	 * I/O do not have a buf and they are completed by setting the
61342f5224aeSachartre 	 * vio_qtime to zero and signaling failfast_io_cv. In that case, the
61352f5224aeSachartre 	 * thread waiting for the I/O to complete is responsible for freeing
61362f5224aeSachartre 	 * the vio structure.
61372f5224aeSachartre 	 */
61382f5224aeSachartre 	while (vio != NULL) {
61392f5224aeSachartre 		vio_tmp = vio->vio_next;
61402f5224aeSachartre 		if (vio->vio_buf != NULL) {
6141*90e2f9dcSlm66018 			VD_KSTAT_RUNQ_EXIT(vdc);
6142366a92acSlm66018 			DTRACE_IO1(done, buf_t *, vio->vio_buf);
61432f5224aeSachartre 			biodone(vio->vio_buf);
61442f5224aeSachartre 			kmem_free(vio, sizeof (vdc_io_t));
61452f5224aeSachartre 		} else {
61462f5224aeSachartre 			vio->vio_qtime = 0;
61472f5224aeSachartre 		}
61482f5224aeSachartre 		vio = vio_tmp;
61492f5224aeSachartre 	}
61502f5224aeSachartre 
61512f5224aeSachartre 	cv_broadcast(&vdc->failfast_io_cv);
61522f5224aeSachartre }
61532f5224aeSachartre 
61542f5224aeSachartre /*
61552f5224aeSachartre  * Failfast Thread.
61562f5224aeSachartre  *
61572f5224aeSachartre  * While failfast is enabled, the failfast thread sends a TEST UNIT READY
61582f5224aeSachartre  * and a zero size WRITE(10) SCSI commands on a regular basis to check that
61592f5224aeSachartre  * we still have access to the disk. If a command fails with a RESERVATION
61602f5224aeSachartre  * CONFLICT error then the system will immediatly panic.
61612f5224aeSachartre  *
61622f5224aeSachartre  * The failfast thread is also woken up when an I/O has failed. It then check
61632f5224aeSachartre  * the access to the disk to ensure that the I/O failure was not due to a
61642f5224aeSachartre  * reservation conflict.
61652f5224aeSachartre  *
61662f5224aeSachartre  * There is one failfast thread for each virtual disk for which failfast is
61672f5224aeSachartre  * enabled. We could have only one thread sending requests for all disks but
61682f5224aeSachartre  * this would need vdc to send asynchronous requests and to have callbacks to
61692f5224aeSachartre  * process replies.
61702f5224aeSachartre  */
61712f5224aeSachartre static void
61722f5224aeSachartre vdc_failfast_thread(void *arg)
61732f5224aeSachartre {
61742f5224aeSachartre 	int status;
61752f5224aeSachartre 	vdc_t *vdc = (vdc_t *)arg;
61762f5224aeSachartre 	clock_t timeout, starttime;
61772f5224aeSachartre 
61782f5224aeSachartre 	mutex_enter(&vdc->lock);
61792f5224aeSachartre 
61802f5224aeSachartre 	while (vdc->failfast_interval != 0) {
61812f5224aeSachartre 
61822f5224aeSachartre 		starttime = ddi_get_lbolt();
61832f5224aeSachartre 
61842f5224aeSachartre 		mutex_exit(&vdc->lock);
61852f5224aeSachartre 
61862f5224aeSachartre 		/* check for reservation conflict */
61872f5224aeSachartre 		status = vdc_failfast_check_resv(vdc);
61882f5224aeSachartre 
61892f5224aeSachartre 		mutex_enter(&vdc->lock);
61902f5224aeSachartre 		/*
61912f5224aeSachartre 		 * We have dropped the lock to send the SCSI command so we have
61922f5224aeSachartre 		 * to check that failfast is still enabled.
61932f5224aeSachartre 		 */
61942f5224aeSachartre 		if (vdc->failfast_interval == 0)
61952f5224aeSachartre 			break;
61962f5224aeSachartre 
61972f5224aeSachartre 		/*
61982f5224aeSachartre 		 * If we have successfully check the disk access and there was
61992f5224aeSachartre 		 * no reservation conflict then we can complete any I/O queued
62002f5224aeSachartre 		 * before the last check.
62012f5224aeSachartre 		 */
62022f5224aeSachartre 		if (status == 0)
62032f5224aeSachartre 			vdc_failfast_io_unqueue(vdc, starttime);
62042f5224aeSachartre 
62052f5224aeSachartre 		/* proceed again if some I/O are still in the queue */
62062f5224aeSachartre 		if (vdc->failfast_io_queue != NULL)
62072f5224aeSachartre 			continue;
62082f5224aeSachartre 
62092f5224aeSachartre 		timeout = ddi_get_lbolt() +
62102f5224aeSachartre 		    drv_usectohz(vdc->failfast_interval);
62112f5224aeSachartre 		(void) cv_timedwait(&vdc->failfast_cv, &vdc->lock, timeout);
62122f5224aeSachartre 	}
62132f5224aeSachartre 
62142f5224aeSachartre 	/*
62152f5224aeSachartre 	 * Failfast is being stop so we can complete any queued I/O.
62162f5224aeSachartre 	 */
62172f5224aeSachartre 	vdc_failfast_io_unqueue(vdc, 0);
62182f5224aeSachartre 	vdc->failfast_thread = NULL;
62192f5224aeSachartre 	mutex_exit(&vdc->lock);
62202f5224aeSachartre 	thread_exit();
62212f5224aeSachartre }
62222f5224aeSachartre 
62232f5224aeSachartre /*
62242f5224aeSachartre  * Implement the MHIOCENFAILFAST mhd(7i) ioctl.
62252f5224aeSachartre  */
62262f5224aeSachartre static int
62272f5224aeSachartre vdc_failfast(vdc_t *vdc, caddr_t arg, int mode)
62282f5224aeSachartre {
62292f5224aeSachartre 	unsigned int mh_time;
62302f5224aeSachartre 
62312f5224aeSachartre 	if (ddi_copyin((void *)arg, &mh_time, sizeof (int), mode))
62322f5224aeSachartre 		return (EFAULT);
62332f5224aeSachartre 
62342f5224aeSachartre 	mutex_enter(&vdc->lock);
62352f5224aeSachartre 	if (mh_time != 0 && vdc->failfast_thread == NULL) {
62362f5224aeSachartre 		vdc->failfast_thread = thread_create(NULL, 0,
62372f5224aeSachartre 		    vdc_failfast_thread, vdc, 0, &p0, TS_RUN,
62382f5224aeSachartre 		    v.v_maxsyspri - 2);
62392f5224aeSachartre 	}
62402f5224aeSachartre 
62412f5224aeSachartre 	vdc->failfast_interval = mh_time * 1000;
62422f5224aeSachartre 	cv_signal(&vdc->failfast_cv);
62432f5224aeSachartre 	mutex_exit(&vdc->lock);
62442f5224aeSachartre 
62452f5224aeSachartre 	return (0);
62462f5224aeSachartre }
62472f5224aeSachartre 
62482f5224aeSachartre /*
62492f5224aeSachartre  * Implement the MHIOCTKOWN and MHIOCRELEASE mhd(7i) ioctls. These ioctls are
62502f5224aeSachartre  * converted to VD_OP_SET_ACCESS operations.
62512f5224aeSachartre  */
62522f5224aeSachartre static int
62532f5224aeSachartre vdc_access_set(vdc_t *vdc, uint64_t flags, int mode)
62542f5224aeSachartre {
62552f5224aeSachartre 	int rv;
62562f5224aeSachartre 
62572f5224aeSachartre 	/* submit owership command request */
62582f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SET_ACCESS, (caddr_t)&flags,
62592f5224aeSachartre 	    sizeof (uint64_t), 0, 0, CB_SYNC, (void *)(uint64_t)mode,
62602f5224aeSachartre 	    VIO_both_dir, B_TRUE);
62612f5224aeSachartre 
62622f5224aeSachartre 	return (rv);
62632f5224aeSachartre }
62642f5224aeSachartre 
62652f5224aeSachartre /*
62662f5224aeSachartre  * Implement the MHIOCSTATUS mhd(7i) ioctl. This ioctl is converted to a
62672f5224aeSachartre  * VD_OP_GET_ACCESS operation.
62682f5224aeSachartre  */
62692f5224aeSachartre static int
62702f5224aeSachartre vdc_access_get(vdc_t *vdc, uint64_t *status, int mode)
62712f5224aeSachartre {
62722f5224aeSachartre 	int rv;
62732f5224aeSachartre 
62742f5224aeSachartre 	/* submit owership command request */
62752f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_GET_ACCESS, (caddr_t)status,
62762f5224aeSachartre 	    sizeof (uint64_t), 0, 0, CB_SYNC, (void *)(uint64_t)mode,
62772f5224aeSachartre 	    VIO_both_dir, B_TRUE);
62782f5224aeSachartre 
62792f5224aeSachartre 	return (rv);
62802f5224aeSachartre }
62812f5224aeSachartre 
62822f5224aeSachartre /*
62832f5224aeSachartre  * Disk Ownership Thread.
62842f5224aeSachartre  *
62852f5224aeSachartre  * When we have taken the ownership of a disk, this thread waits to be
62862f5224aeSachartre  * notified when the LDC channel is reset so that it can recover the
62872f5224aeSachartre  * ownership.
62882f5224aeSachartre  *
62892f5224aeSachartre  * Note that the thread handling the LDC reset (vdc_process_msg_thread())
62902f5224aeSachartre  * can not be used to do the ownership recovery because it has to be
62912f5224aeSachartre  * running to handle the reply message to the ownership operation.
62922f5224aeSachartre  */
62932f5224aeSachartre static void
62942f5224aeSachartre vdc_ownership_thread(void *arg)
62952f5224aeSachartre {
62962f5224aeSachartre 	vdc_t *vdc = (vdc_t *)arg;
62972f5224aeSachartre 	clock_t timeout;
62982f5224aeSachartre 	uint64_t status;
62992f5224aeSachartre 
63002f5224aeSachartre 	mutex_enter(&vdc->ownership_lock);
63012f5224aeSachartre 	mutex_enter(&vdc->lock);
63022f5224aeSachartre 
63032f5224aeSachartre 	while (vdc->ownership & VDC_OWNERSHIP_WANTED) {
63042f5224aeSachartre 
63052f5224aeSachartre 		if ((vdc->ownership & VDC_OWNERSHIP_RESET) ||
63062f5224aeSachartre 		    !(vdc->ownership & VDC_OWNERSHIP_GRANTED)) {
63072f5224aeSachartre 			/*
63082f5224aeSachartre 			 * There was a reset so the ownership has been lost,
63092f5224aeSachartre 			 * try to recover. We do this without using the preempt
63102f5224aeSachartre 			 * option so that we don't steal the ownership from
63112f5224aeSachartre 			 * someone who has preempted us.
63122f5224aeSachartre 			 */
63132f5224aeSachartre 			DMSG(vdc, 0, "[%d] Ownership lost, recovering",
63142f5224aeSachartre 			    vdc->instance);
63152f5224aeSachartre 
63162f5224aeSachartre 			vdc->ownership &= ~(VDC_OWNERSHIP_RESET |
63172f5224aeSachartre 			    VDC_OWNERSHIP_GRANTED);
63182f5224aeSachartre 
63192f5224aeSachartre 			mutex_exit(&vdc->lock);
63202f5224aeSachartre 
63212f5224aeSachartre 			status = vdc_access_set(vdc, VD_ACCESS_SET_EXCLUSIVE |
63222f5224aeSachartre 			    VD_ACCESS_SET_PRESERVE, FKIOCTL);
63232f5224aeSachartre 
63242f5224aeSachartre 			mutex_enter(&vdc->lock);
63252f5224aeSachartre 
63262f5224aeSachartre 			if (status == 0) {
63272f5224aeSachartre 				DMSG(vdc, 0, "[%d] Ownership recovered",
63282f5224aeSachartre 				    vdc->instance);
63292f5224aeSachartre 				vdc->ownership |= VDC_OWNERSHIP_GRANTED;
63302f5224aeSachartre 			} else {
63312f5224aeSachartre 				DMSG(vdc, 0, "[%d] Fail to recover ownership",
63322f5224aeSachartre 				    vdc->instance);
63332f5224aeSachartre 			}
63342f5224aeSachartre 
63352f5224aeSachartre 		}
63362f5224aeSachartre 
63372f5224aeSachartre 		/*
63382f5224aeSachartre 		 * If we have the ownership then we just wait for an event
63392f5224aeSachartre 		 * to happen (LDC reset), otherwise we will retry to recover
63402f5224aeSachartre 		 * after a delay.
63412f5224aeSachartre 		 */
63422f5224aeSachartre 		if (vdc->ownership & VDC_OWNERSHIP_GRANTED)
63432f5224aeSachartre 			timeout = 0;
63442f5224aeSachartre 		else
63452f5224aeSachartre 			timeout = ddi_get_lbolt() +
63462f5224aeSachartre 			    drv_usectohz(vdc_ownership_delay);
63472f5224aeSachartre 
63482f5224aeSachartre 		/* Release the ownership_lock and wait on the vdc lock */
63492f5224aeSachartre 		mutex_exit(&vdc->ownership_lock);
63502f5224aeSachartre 
63512f5224aeSachartre 		if (timeout == 0)
63522f5224aeSachartre 			(void) cv_wait(&vdc->ownership_cv, &vdc->lock);
63532f5224aeSachartre 		else
63542f5224aeSachartre 			(void) cv_timedwait(&vdc->ownership_cv,
63552f5224aeSachartre 			    &vdc->lock, timeout);
63562f5224aeSachartre 
63572f5224aeSachartre 		mutex_exit(&vdc->lock);
63582f5224aeSachartre 
63592f5224aeSachartre 		mutex_enter(&vdc->ownership_lock);
63602f5224aeSachartre 		mutex_enter(&vdc->lock);
63612f5224aeSachartre 	}
63622f5224aeSachartre 
63632f5224aeSachartre 	vdc->ownership_thread = NULL;
63642f5224aeSachartre 	mutex_exit(&vdc->lock);
63652f5224aeSachartre 	mutex_exit(&vdc->ownership_lock);
63662f5224aeSachartre 
63672f5224aeSachartre 	thread_exit();
63682f5224aeSachartre }
63692f5224aeSachartre 
63702f5224aeSachartre static void
63712f5224aeSachartre vdc_ownership_update(vdc_t *vdc, int ownership_flags)
63722f5224aeSachartre {
63732f5224aeSachartre 	ASSERT(MUTEX_HELD(&vdc->ownership_lock));
63742f5224aeSachartre 
63752f5224aeSachartre 	mutex_enter(&vdc->lock);
63762f5224aeSachartre 	vdc->ownership = ownership_flags;
63772f5224aeSachartre 	if ((vdc->ownership & VDC_OWNERSHIP_WANTED) &&
63782f5224aeSachartre 	    vdc->ownership_thread == NULL) {
63792f5224aeSachartre 		/* start ownership thread */
63802f5224aeSachartre 		vdc->ownership_thread = thread_create(NULL, 0,
63812f5224aeSachartre 		    vdc_ownership_thread, vdc, 0, &p0, TS_RUN,
63822f5224aeSachartre 		    v.v_maxsyspri - 2);
63832f5224aeSachartre 	} else {
63842f5224aeSachartre 		/* notify the ownership thread */
63852f5224aeSachartre 		cv_signal(&vdc->ownership_cv);
63862f5224aeSachartre 	}
63872f5224aeSachartre 	mutex_exit(&vdc->lock);
63882f5224aeSachartre }
63892f5224aeSachartre 
63902f5224aeSachartre /*
63912f5224aeSachartre  * Get the size and the block size of a virtual disk from the vdisk server.
63922f5224aeSachartre  * We need to use this operation when the vdisk_size attribute was not
63932f5224aeSachartre  * available during the handshake with the vdisk server.
63942f5224aeSachartre  */
63952f5224aeSachartre static int
63962f5224aeSachartre vdc_check_capacity(vdc_t *vdc)
63972f5224aeSachartre {
63982f5224aeSachartre 	int rv = 0;
63992f5224aeSachartre 	size_t alloc_len;
64002f5224aeSachartre 	vd_capacity_t *vd_cap;
64012f5224aeSachartre 
64022f5224aeSachartre 	if (vdc->vdisk_size != 0)
64032f5224aeSachartre 		return (0);
64042f5224aeSachartre 
64052f5224aeSachartre 	alloc_len = P2ROUNDUP(sizeof (vd_capacity_t), sizeof (uint64_t));
64062f5224aeSachartre 
64072f5224aeSachartre 	vd_cap = kmem_zalloc(alloc_len, KM_SLEEP);
64082f5224aeSachartre 
64092f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_GET_CAPACITY, (caddr_t)vd_cap, alloc_len,
64102f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)FKIOCTL, VIO_both_dir, B_TRUE);
64112f5224aeSachartre 
64122f5224aeSachartre 	if (rv == 0) {
64132f5224aeSachartre 		if (vd_cap->vdisk_block_size != vdc->block_size ||
64142f5224aeSachartre 		    vd_cap->vdisk_size == VD_SIZE_UNKNOWN ||
64152f5224aeSachartre 		    vd_cap->vdisk_size == 0)
64162f5224aeSachartre 			rv = EINVAL;
64172f5224aeSachartre 		else
64182f5224aeSachartre 			vdc->vdisk_size = vd_cap->vdisk_size;
64192f5224aeSachartre 	}
64202f5224aeSachartre 
64212f5224aeSachartre 	kmem_free(vd_cap, alloc_len);
64222f5224aeSachartre 	return (rv);
64232f5224aeSachartre }
64242f5224aeSachartre 
64252f5224aeSachartre /*
64261ae08745Sheppo  * This structure is used in the DKIO(7I) array below.
64271ae08745Sheppo  */
64281ae08745Sheppo typedef struct vdc_dk_ioctl {
64291ae08745Sheppo 	uint8_t		op;		/* VD_OP_XXX value */
64301ae08745Sheppo 	int		cmd;		/* Solaris ioctl operation number */
64311ae08745Sheppo 	size_t		nbytes;		/* size of structure to be copied */
64320a55fbb7Slm66018 
64330a55fbb7Slm66018 	/* function to convert between vDisk and Solaris structure formats */
6434d10e4ef2Snarayan 	int	(*convert)(vdc_t *vdc, void *vd_buf, void *ioctl_arg,
6435d10e4ef2Snarayan 	    int mode, int dir);
64361ae08745Sheppo } vdc_dk_ioctl_t;
64371ae08745Sheppo 
64381ae08745Sheppo /*
64391ae08745Sheppo  * Subset of DKIO(7I) operations currently supported
64401ae08745Sheppo  */
64411ae08745Sheppo static vdc_dk_ioctl_t	dk_ioctl[] = {
6442eff7243fSlm66018 	{VD_OP_FLUSH,		DKIOCFLUSHWRITECACHE,	0,
64430a55fbb7Slm66018 		vdc_null_copy_func},
64440a55fbb7Slm66018 	{VD_OP_GET_WCE,		DKIOCGETWCE,		sizeof (int),
64454bac2208Snarayan 		vdc_get_wce_convert},
64460a55fbb7Slm66018 	{VD_OP_SET_WCE,		DKIOCSETWCE,		sizeof (int),
64474bac2208Snarayan 		vdc_set_wce_convert},
64480a55fbb7Slm66018 	{VD_OP_GET_VTOC,	DKIOCGVTOC,		sizeof (vd_vtoc_t),
64490a55fbb7Slm66018 		vdc_get_vtoc_convert},
64500a55fbb7Slm66018 	{VD_OP_SET_VTOC,	DKIOCSVTOC,		sizeof (vd_vtoc_t),
64510a55fbb7Slm66018 		vdc_set_vtoc_convert},
64520a55fbb7Slm66018 	{VD_OP_GET_DISKGEOM,	DKIOCGGEOM,		sizeof (vd_geom_t),
64530a55fbb7Slm66018 		vdc_get_geom_convert},
64540a55fbb7Slm66018 	{VD_OP_GET_DISKGEOM,	DKIOCG_PHYGEOM,		sizeof (vd_geom_t),
64550a55fbb7Slm66018 		vdc_get_geom_convert},
64560a55fbb7Slm66018 	{VD_OP_GET_DISKGEOM, 	DKIOCG_VIRTGEOM,	sizeof (vd_geom_t),
64570a55fbb7Slm66018 		vdc_get_geom_convert},
64580a55fbb7Slm66018 	{VD_OP_SET_DISKGEOM,	DKIOCSGEOM,		sizeof (vd_geom_t),
64590a55fbb7Slm66018 		vdc_set_geom_convert},
64604bac2208Snarayan 	{VD_OP_GET_EFI,		DKIOCGETEFI,		0,
64614bac2208Snarayan 		vdc_get_efi_convert},
64624bac2208Snarayan 	{VD_OP_SET_EFI,		DKIOCSETEFI,		0,
64634bac2208Snarayan 		vdc_set_efi_convert},
64640a55fbb7Slm66018 
646587a7269eSachartre 	/* DIOCTL_RWCMD is converted to a read or a write */
646687a7269eSachartre 	{0, DIOCTL_RWCMD,  sizeof (struct dadkio_rwcmd), NULL},
646787a7269eSachartre 
64682f5224aeSachartre 	/* mhd(7I) non-shared multihost disks ioctls */
64692f5224aeSachartre 	{0, MHIOCTKOWN,				0, vdc_null_copy_func},
64702f5224aeSachartre 	{0, MHIOCRELEASE,			0, vdc_null_copy_func},
64712f5224aeSachartre 	{0, MHIOCSTATUS,			0, vdc_null_copy_func},
64722f5224aeSachartre 	{0, MHIOCQRESERVE,			0, vdc_null_copy_func},
64732f5224aeSachartre 
64742f5224aeSachartre 	/* mhd(7I) shared multihost disks ioctls */
64752f5224aeSachartre 	{0, MHIOCGRP_INKEYS,			0, vdc_null_copy_func},
64762f5224aeSachartre 	{0, MHIOCGRP_INRESV,			0, vdc_null_copy_func},
64772f5224aeSachartre 	{0, MHIOCGRP_REGISTER,			0, vdc_null_copy_func},
64782f5224aeSachartre 	{0, MHIOCGRP_RESERVE, 			0, vdc_null_copy_func},
64792f5224aeSachartre 	{0, MHIOCGRP_PREEMPTANDABORT,		0, vdc_null_copy_func},
64802f5224aeSachartre 	{0, MHIOCGRP_REGISTERANDIGNOREKEY,	0, vdc_null_copy_func},
64812f5224aeSachartre 
64822f5224aeSachartre 	/* mhd(7I) failfast ioctl */
64832f5224aeSachartre 	{0, MHIOCENFAILFAST,			0, vdc_null_copy_func},
64842f5224aeSachartre 
64850a55fbb7Slm66018 	/*
64860a55fbb7Slm66018 	 * These particular ioctls are not sent to the server - vdc fakes up
64870a55fbb7Slm66018 	 * the necessary info.
64880a55fbb7Slm66018 	 */
64890a55fbb7Slm66018 	{0, DKIOCINFO, sizeof (struct dk_cinfo), vdc_null_copy_func},
64900a55fbb7Slm66018 	{0, DKIOCGMEDIAINFO, sizeof (struct dk_minfo), vdc_null_copy_func},
64910a55fbb7Slm66018 	{0, USCSICMD,	sizeof (struct uscsi_cmd), vdc_null_copy_func},
64929642afceSachartre 	{0, DKIOCPARTITION, 0, vdc_null_copy_func },
649387a7269eSachartre 	{0, DKIOCGAPART, 0, vdc_null_copy_func },
64940a55fbb7Slm66018 	{0, DKIOCREMOVABLE, 0, vdc_null_copy_func},
64950a55fbb7Slm66018 	{0, CDROMREADOFFSET, 0, vdc_null_copy_func}
64961ae08745Sheppo };
64971ae08745Sheppo 
64981ae08745Sheppo /*
6499edcc0754Sachartre  * This function handles ioctl requests from the vd_efi_alloc_and_read()
6500edcc0754Sachartre  * function and forward them to the vdisk.
65012f5224aeSachartre  */
65022f5224aeSachartre static int
6503edcc0754Sachartre vd_process_efi_ioctl(void *vdisk, int cmd, uintptr_t arg)
65042f5224aeSachartre {
6505edcc0754Sachartre 	vdc_t *vdc = (vdc_t *)vdisk;
6506edcc0754Sachartre 	dev_t dev;
65072f5224aeSachartre 	int rval;
6508edcc0754Sachartre 
6509edcc0754Sachartre 	dev = makedevice(ddi_driver_major(vdc->dip),
6510edcc0754Sachartre 	    VD_MAKE_DEV(vdc->instance, 0));
6511edcc0754Sachartre 
6512edcc0754Sachartre 	return (vd_process_ioctl(dev, cmd, (caddr_t)arg, FKIOCTL, &rval));
65132f5224aeSachartre }
65142f5224aeSachartre 
65152f5224aeSachartre /*
65161ae08745Sheppo  * Function:
65171ae08745Sheppo  *	vd_process_ioctl()
65181ae08745Sheppo  *
65191ae08745Sheppo  * Description:
65200a55fbb7Slm66018  *	This routine processes disk specific ioctl calls
65211ae08745Sheppo  *
65221ae08745Sheppo  * Arguments:
65231ae08745Sheppo  *	dev	- the device number
65241ae08745Sheppo  *	cmd	- the operation [dkio(7I)] to be processed
65251ae08745Sheppo  *	arg	- pointer to user provided structure
65261ae08745Sheppo  *		  (contains data to be set or reference parameter for get)
65271ae08745Sheppo  *	mode	- bit flag, indicating open settings, 32/64 bit type, etc
65282f5224aeSachartre  *	rvalp	- pointer to return value for calling process.
65291ae08745Sheppo  *
65301ae08745Sheppo  * Return Code:
65311ae08745Sheppo  *	0
65321ae08745Sheppo  *	EFAULT
65331ae08745Sheppo  *	ENXIO
65341ae08745Sheppo  *	EIO
65351ae08745Sheppo  *	ENOTSUP
65361ae08745Sheppo  */
65371ae08745Sheppo static int
65382f5224aeSachartre vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode, int *rvalp)
65391ae08745Sheppo {
65400d0c8d4bSnarayan 	int		instance = VDCUNIT(dev);
65411ae08745Sheppo 	vdc_t		*vdc = NULL;
65421ae08745Sheppo 	int		rv = -1;
65431ae08745Sheppo 	int		idx = 0;		/* index into dk_ioctl[] */
65441ae08745Sheppo 	size_t		len = 0;		/* #bytes to send to vds */
65451ae08745Sheppo 	size_t		alloc_len = 0;		/* #bytes to allocate mem for */
65461ae08745Sheppo 	caddr_t		mem_p = NULL;
65471ae08745Sheppo 	size_t		nioctls = (sizeof (dk_ioctl)) / (sizeof (dk_ioctl[0]));
65483af08d82Slm66018 	vdc_dk_ioctl_t	*iop;
65491ae08745Sheppo 
65501ae08745Sheppo 	vdc = ddi_get_soft_state(vdc_state, instance);
65511ae08745Sheppo 	if (vdc == NULL) {
65521ae08745Sheppo 		cmn_err(CE_NOTE, "![%d] Could not get soft state structure",
65531ae08745Sheppo 		    instance);
65541ae08745Sheppo 		return (ENXIO);
65551ae08745Sheppo 	}
65561ae08745Sheppo 
65573af08d82Slm66018 	DMSG(vdc, 0, "[%d] Processing ioctl(%x) for dev %lx : model %x\n",
65583af08d82Slm66018 	    instance, cmd, dev, ddi_model_convert_from(mode & FMODELS));
65591ae08745Sheppo 
65602f5224aeSachartre 	if (rvalp != NULL) {
65612f5224aeSachartre 		/* the return value of the ioctl is 0 by default */
65622f5224aeSachartre 		*rvalp = 0;
65632f5224aeSachartre 	}
65642f5224aeSachartre 
65651ae08745Sheppo 	/*
65661ae08745Sheppo 	 * Validate the ioctl operation to be performed.
65671ae08745Sheppo 	 *
65681ae08745Sheppo 	 * If we have looped through the array without finding a match then we
65691ae08745Sheppo 	 * don't support this ioctl.
65701ae08745Sheppo 	 */
65711ae08745Sheppo 	for (idx = 0; idx < nioctls; idx++) {
65721ae08745Sheppo 		if (cmd == dk_ioctl[idx].cmd)
65731ae08745Sheppo 			break;
65741ae08745Sheppo 	}
65751ae08745Sheppo 
65761ae08745Sheppo 	if (idx >= nioctls) {
65773af08d82Slm66018 		DMSG(vdc, 0, "[%d] Unsupported ioctl (0x%x)\n",
6578e1ebb9ecSlm66018 		    vdc->instance, cmd);
65791ae08745Sheppo 		return (ENOTSUP);
65801ae08745Sheppo 	}
65811ae08745Sheppo 
65823af08d82Slm66018 	iop = &(dk_ioctl[idx]);
65833af08d82Slm66018 
65844bac2208Snarayan 	if (cmd == DKIOCGETEFI || cmd == DKIOCSETEFI) {
65854bac2208Snarayan 		/* size is not fixed for EFI ioctls, it depends on ioctl arg */
65864bac2208Snarayan 		dk_efi_t	dk_efi;
65874bac2208Snarayan 
65884bac2208Snarayan 		rv = ddi_copyin(arg, &dk_efi, sizeof (dk_efi_t), mode);
65894bac2208Snarayan 		if (rv != 0)
65904bac2208Snarayan 			return (EFAULT);
65914bac2208Snarayan 
65924bac2208Snarayan 		len = sizeof (vd_efi_t) - 1 + dk_efi.dki_length;
65934bac2208Snarayan 	} else {
65943af08d82Slm66018 		len = iop->nbytes;
65954bac2208Snarayan 	}
65961ae08745Sheppo 
65972f5224aeSachartre 	/* check if the ioctl is applicable */
65981ae08745Sheppo 	switch (cmd) {
65991ae08745Sheppo 	case CDROMREADOFFSET:
66001ae08745Sheppo 	case DKIOCREMOVABLE:
66011ae08745Sheppo 		return (ENOTTY);
66021ae08745Sheppo 
66032f5224aeSachartre 	case USCSICMD:
66042f5224aeSachartre 	case MHIOCTKOWN:
66052f5224aeSachartre 	case MHIOCSTATUS:
66062f5224aeSachartre 	case MHIOCQRESERVE:
66072f5224aeSachartre 	case MHIOCRELEASE:
66082f5224aeSachartre 	case MHIOCGRP_INKEYS:
66092f5224aeSachartre 	case MHIOCGRP_INRESV:
66102f5224aeSachartre 	case MHIOCGRP_REGISTER:
66112f5224aeSachartre 	case MHIOCGRP_RESERVE:
66122f5224aeSachartre 	case MHIOCGRP_PREEMPTANDABORT:
66132f5224aeSachartre 	case MHIOCGRP_REGISTERANDIGNOREKEY:
66142f5224aeSachartre 	case MHIOCENFAILFAST:
66152f5224aeSachartre 		if (vdc->cinfo == NULL)
66162f5224aeSachartre 			return (ENXIO);
66172f5224aeSachartre 		if (vdc->cinfo->dki_ctype != DKC_SCSI_CCS)
66182f5224aeSachartre 			return (ENOTTY);
66192f5224aeSachartre 		break;
66202f5224aeSachartre 
66212f5224aeSachartre 	case DIOCTL_RWCMD:
66222f5224aeSachartre 		if (vdc->cinfo == NULL)
66232f5224aeSachartre 			return (ENXIO);
66242f5224aeSachartre 		if (vdc->cinfo->dki_ctype != DKC_DIRECT)
66252f5224aeSachartre 			return (ENOTTY);
66262f5224aeSachartre 		break;
66272f5224aeSachartre 
66282f5224aeSachartre 	case DKIOCINFO:
66292f5224aeSachartre 		if (vdc->cinfo == NULL)
66302f5224aeSachartre 			return (ENXIO);
66312f5224aeSachartre 		break;
66322f5224aeSachartre 
66332f5224aeSachartre 	case DKIOCGMEDIAINFO:
66342f5224aeSachartre 		if (vdc->minfo == NULL)
66352f5224aeSachartre 			return (ENXIO);
66362f5224aeSachartre 		if (vdc_check_capacity(vdc) != 0)
66372f5224aeSachartre 			/* disk capacity is not available */
66382f5224aeSachartre 			return (EIO);
66392f5224aeSachartre 		break;
66402f5224aeSachartre 	}
66412f5224aeSachartre 
66422f5224aeSachartre 	/*
66432f5224aeSachartre 	 * Deal with ioctls which require a processing different than
66442f5224aeSachartre 	 * converting ioctl arguments and sending a corresponding
66452f5224aeSachartre 	 * VD operation.
66462f5224aeSachartre 	 */
66472f5224aeSachartre 	switch (cmd) {
66482f5224aeSachartre 
66492f5224aeSachartre 	case USCSICMD:
66502f5224aeSachartre 	{
66512f5224aeSachartre 		return (vdc_uscsi_cmd(vdc, arg, mode));
66522f5224aeSachartre 	}
66532f5224aeSachartre 
66542f5224aeSachartre 	case MHIOCTKOWN:
66552f5224aeSachartre 	{
66562f5224aeSachartre 		mutex_enter(&vdc->ownership_lock);
66572f5224aeSachartre 		/*
66582f5224aeSachartre 		 * We have to set VDC_OWNERSHIP_WANTED now so that the ownership
66592f5224aeSachartre 		 * can be flagged with VDC_OWNERSHIP_RESET if the LDC is reset
66602f5224aeSachartre 		 * while we are processing the ioctl.
66612f5224aeSachartre 		 */
66622f5224aeSachartre 		vdc_ownership_update(vdc, VDC_OWNERSHIP_WANTED);
66632f5224aeSachartre 
66642f5224aeSachartre 		rv = vdc_access_set(vdc, VD_ACCESS_SET_EXCLUSIVE |
66652f5224aeSachartre 		    VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE, mode);
66662f5224aeSachartre 		if (rv == 0) {
66672f5224aeSachartre 			vdc_ownership_update(vdc, VDC_OWNERSHIP_WANTED |
66682f5224aeSachartre 			    VDC_OWNERSHIP_GRANTED);
66692f5224aeSachartre 		} else {
66702f5224aeSachartre 			vdc_ownership_update(vdc, VDC_OWNERSHIP_NONE);
66712f5224aeSachartre 		}
66722f5224aeSachartre 		mutex_exit(&vdc->ownership_lock);
66732f5224aeSachartre 		return (rv);
66742f5224aeSachartre 	}
66752f5224aeSachartre 
66762f5224aeSachartre 	case MHIOCRELEASE:
66772f5224aeSachartre 	{
66782f5224aeSachartre 		mutex_enter(&vdc->ownership_lock);
66792f5224aeSachartre 		rv = vdc_access_set(vdc, VD_ACCESS_SET_CLEAR, mode);
66802f5224aeSachartre 		if (rv == 0) {
66812f5224aeSachartre 			vdc_ownership_update(vdc, VDC_OWNERSHIP_NONE);
66822f5224aeSachartre 		}
66832f5224aeSachartre 		mutex_exit(&vdc->ownership_lock);
66842f5224aeSachartre 		return (rv);
66852f5224aeSachartre 	}
66862f5224aeSachartre 
66872f5224aeSachartre 	case MHIOCSTATUS:
66882f5224aeSachartre 	{
66892f5224aeSachartre 		uint64_t status;
66902f5224aeSachartre 
66912f5224aeSachartre 		rv = vdc_access_get(vdc, &status, mode);
66922f5224aeSachartre 		if (rv == 0 && rvalp != NULL)
66932f5224aeSachartre 			*rvalp = (status & VD_ACCESS_ALLOWED)? 0 : 1;
66942f5224aeSachartre 		return (rv);
66952f5224aeSachartre 	}
66962f5224aeSachartre 
66972f5224aeSachartre 	case MHIOCQRESERVE:
66982f5224aeSachartre 	{
66992f5224aeSachartre 		rv = vdc_access_set(vdc, VD_ACCESS_SET_EXCLUSIVE, mode);
67002f5224aeSachartre 		return (rv);
67012f5224aeSachartre 	}
67022f5224aeSachartre 
67032f5224aeSachartre 	case MHIOCGRP_INKEYS:
67042f5224aeSachartre 	{
67052f5224aeSachartre 		return (vdc_mhd_inkeys(vdc, arg, mode));
67062f5224aeSachartre 	}
67072f5224aeSachartre 
67082f5224aeSachartre 	case MHIOCGRP_INRESV:
67092f5224aeSachartre 	{
67102f5224aeSachartre 		return (vdc_mhd_inresv(vdc, arg, mode));
67112f5224aeSachartre 	}
67122f5224aeSachartre 
67132f5224aeSachartre 	case MHIOCGRP_REGISTER:
67142f5224aeSachartre 	{
67152f5224aeSachartre 		return (vdc_mhd_register(vdc, arg, mode));
67162f5224aeSachartre 	}
67172f5224aeSachartre 
67182f5224aeSachartre 	case MHIOCGRP_RESERVE:
67192f5224aeSachartre 	{
67202f5224aeSachartre 		return (vdc_mhd_reserve(vdc, arg, mode));
67212f5224aeSachartre 	}
67222f5224aeSachartre 
67232f5224aeSachartre 	case MHIOCGRP_PREEMPTANDABORT:
67242f5224aeSachartre 	{
67252f5224aeSachartre 		return (vdc_mhd_preemptabort(vdc, arg, mode));
67262f5224aeSachartre 	}
67272f5224aeSachartre 
67282f5224aeSachartre 	case MHIOCGRP_REGISTERANDIGNOREKEY:
67292f5224aeSachartre 	{
67302f5224aeSachartre 		return (vdc_mhd_registerignore(vdc, arg, mode));
67312f5224aeSachartre 	}
67322f5224aeSachartre 
67332f5224aeSachartre 	case MHIOCENFAILFAST:
67342f5224aeSachartre 	{
67352f5224aeSachartre 		rv = vdc_failfast(vdc, arg, mode);
67362f5224aeSachartre 		return (rv);
67372f5224aeSachartre 	}
67382f5224aeSachartre 
673987a7269eSachartre 	case DIOCTL_RWCMD:
674087a7269eSachartre 	{
674187a7269eSachartre 		return (vdc_dioctl_rwcmd(dev, arg, mode));
674287a7269eSachartre 	}
674387a7269eSachartre 
674487a7269eSachartre 	case DKIOCGAPART:
674587a7269eSachartre 	{
67469642afceSachartre 		return (vdc_dkio_gapart(vdc, arg, mode));
67479642afceSachartre 	}
67489642afceSachartre 
67499642afceSachartre 	case DKIOCPARTITION:
67509642afceSachartre 	{
67519642afceSachartre 		return (vdc_dkio_partition(vdc, arg, mode));
675287a7269eSachartre 	}
675387a7269eSachartre 
67541ae08745Sheppo 	case DKIOCINFO:
67551ae08745Sheppo 	{
67561ae08745Sheppo 		struct dk_cinfo	cinfo;
67571ae08745Sheppo 
67581ae08745Sheppo 		bcopy(vdc->cinfo, &cinfo, sizeof (struct dk_cinfo));
67590d0c8d4bSnarayan 		cinfo.dki_partition = VDCPART(dev);
67601ae08745Sheppo 
67611ae08745Sheppo 		rv = ddi_copyout(&cinfo, (void *)arg,
67621ae08745Sheppo 		    sizeof (struct dk_cinfo), mode);
67631ae08745Sheppo 		if (rv != 0)
67641ae08745Sheppo 			return (EFAULT);
67651ae08745Sheppo 
67661ae08745Sheppo 		return (0);
67671ae08745Sheppo 	}
67681ae08745Sheppo 
67691ae08745Sheppo 	case DKIOCGMEDIAINFO:
67708e6a2a04Slm66018 	{
67712f5224aeSachartre 		ASSERT(vdc->vdisk_size != 0);
67722f5224aeSachartre 		if (vdc->minfo->dki_capacity == 0)
67732f5224aeSachartre 			vdc->minfo->dki_capacity = vdc->vdisk_size;
67741ae08745Sheppo 		rv = ddi_copyout(vdc->minfo, (void *)arg,
67751ae08745Sheppo 		    sizeof (struct dk_minfo), mode);
67761ae08745Sheppo 		if (rv != 0)
67771ae08745Sheppo 			return (EFAULT);
67781ae08745Sheppo 
67791ae08745Sheppo 		return (0);
67801ae08745Sheppo 	}
67811ae08745Sheppo 
67828e6a2a04Slm66018 	case DKIOCFLUSHWRITECACHE:
67838e6a2a04Slm66018 		{
678417cadca8Slm66018 			struct dk_callback *dkc =
678517cadca8Slm66018 			    (struct dk_callback *)(uintptr_t)arg;
67868e6a2a04Slm66018 			vdc_dk_arg_t	*dkarg = NULL;
67878e6a2a04Slm66018 
67883af08d82Slm66018 			DMSG(vdc, 1, "[%d] Flush W$: mode %x\n",
67893af08d82Slm66018 			    instance, mode);
67908e6a2a04Slm66018 
67918e6a2a04Slm66018 			/*
67928e6a2a04Slm66018 			 * If arg is NULL, then there is no callback function
67938e6a2a04Slm66018 			 * registered and the call operates synchronously; we
67948e6a2a04Slm66018 			 * break and continue with the rest of the function and
67958e6a2a04Slm66018 			 * wait for vds to return (i.e. after the request to
67968e6a2a04Slm66018 			 * vds returns successfully, all writes completed prior
67978e6a2a04Slm66018 			 * to the ioctl will have been flushed from the disk
67988e6a2a04Slm66018 			 * write cache to persistent media.
67998e6a2a04Slm66018 			 *
68008e6a2a04Slm66018 			 * If a callback function is registered, we dispatch
68018e6a2a04Slm66018 			 * the request on a task queue and return immediately.
68028e6a2a04Slm66018 			 * The callback will deal with informing the calling
68038e6a2a04Slm66018 			 * thread that the flush request is completed.
68048e6a2a04Slm66018 			 */
68058e6a2a04Slm66018 			if (dkc == NULL)
68068e6a2a04Slm66018 				break;
68078e6a2a04Slm66018 
6808eff7243fSlm66018 			/*
6809eff7243fSlm66018 			 * the asynchronous callback is only supported if
6810eff7243fSlm66018 			 * invoked from within the kernel
6811eff7243fSlm66018 			 */
6812eff7243fSlm66018 			if ((mode & FKIOCTL) == 0)
6813eff7243fSlm66018 				return (ENOTSUP);
6814eff7243fSlm66018 
68158e6a2a04Slm66018 			dkarg = kmem_zalloc(sizeof (vdc_dk_arg_t), KM_SLEEP);
68168e6a2a04Slm66018 
68178e6a2a04Slm66018 			dkarg->mode = mode;
68188e6a2a04Slm66018 			dkarg->dev = dev;
68198e6a2a04Slm66018 			bcopy(dkc, &dkarg->dkc, sizeof (*dkc));
68208e6a2a04Slm66018 
68218e6a2a04Slm66018 			mutex_enter(&vdc->lock);
68228e6a2a04Slm66018 			vdc->dkio_flush_pending++;
68238e6a2a04Slm66018 			dkarg->vdc = vdc;
68248e6a2a04Slm66018 			mutex_exit(&vdc->lock);
68258e6a2a04Slm66018 
68268e6a2a04Slm66018 			/* put the request on a task queue */
68278e6a2a04Slm66018 			rv = taskq_dispatch(system_taskq, vdc_dkio_flush_cb,
68288e6a2a04Slm66018 			    (void *)dkarg, DDI_SLEEP);
68293af08d82Slm66018 			if (rv == NULL) {
68303af08d82Slm66018 				/* clean up if dispatch fails */
68313af08d82Slm66018 				mutex_enter(&vdc->lock);
68323af08d82Slm66018 				vdc->dkio_flush_pending--;
683378fcd0a1Sachartre 				mutex_exit(&vdc->lock);
68343af08d82Slm66018 				kmem_free(dkarg, sizeof (vdc_dk_arg_t));
68353af08d82Slm66018 			}
68368e6a2a04Slm66018 
68378e6a2a04Slm66018 			return (rv == NULL ? ENOMEM : 0);
68388e6a2a04Slm66018 		}
68398e6a2a04Slm66018 	}
68408e6a2a04Slm66018 
68411ae08745Sheppo 	/* catch programming error in vdc - should be a VD_OP_XXX ioctl */
68423af08d82Slm66018 	ASSERT(iop->op != 0);
68431ae08745Sheppo 
684417cadca8Slm66018 	/* check if the vDisk server handles the operation for this vDisk */
684517cadca8Slm66018 	if (VD_OP_SUPPORTED(vdc->operations, iop->op) == B_FALSE) {
684617cadca8Slm66018 		DMSG(vdc, 0, "[%d] Unsupported VD_OP operation (0x%x)\n",
684717cadca8Slm66018 		    vdc->instance, iop->op);
684817cadca8Slm66018 		return (ENOTSUP);
684917cadca8Slm66018 	}
685017cadca8Slm66018 
68511ae08745Sheppo 	/* LDC requires that the memory being mapped is 8-byte aligned */
68521ae08745Sheppo 	alloc_len = P2ROUNDUP(len, sizeof (uint64_t));
68533af08d82Slm66018 	DMSG(vdc, 1, "[%d] struct size %ld alloc %ld\n",
68543af08d82Slm66018 	    instance, len, alloc_len);
68551ae08745Sheppo 
6856eff7243fSlm66018 	if (alloc_len > 0)
68571ae08745Sheppo 		mem_p = kmem_zalloc(alloc_len, KM_SLEEP);
68581ae08745Sheppo 
68590a55fbb7Slm66018 	/*
6860eff7243fSlm66018 	 * Call the conversion function for this ioctl which, if necessary,
68610a55fbb7Slm66018 	 * converts from the Solaris format to the format ARC'ed
68620a55fbb7Slm66018 	 * as part of the vDisk protocol (FWARC 2006/195)
68630a55fbb7Slm66018 	 */
68643af08d82Slm66018 	ASSERT(iop->convert != NULL);
68653af08d82Slm66018 	rv = (iop->convert)(vdc, arg, mem_p, mode, VD_COPYIN);
68661ae08745Sheppo 	if (rv != 0) {
68673af08d82Slm66018 		DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n",
6868e1ebb9ecSlm66018 		    instance, rv, cmd);
68691ae08745Sheppo 		if (mem_p != NULL)
68701ae08745Sheppo 			kmem_free(mem_p, alloc_len);
68710a55fbb7Slm66018 		return (rv);
68721ae08745Sheppo 	}
68731ae08745Sheppo 
68741ae08745Sheppo 	/*
68751ae08745Sheppo 	 * send request to vds to service the ioctl.
68761ae08745Sheppo 	 */
68773af08d82Slm66018 	rv = vdc_do_sync_op(vdc, iop->op, mem_p, alloc_len,
68780d0c8d4bSnarayan 	    VDCPART(dev), 0, CB_SYNC, (void *)(uint64_t)mode,
68792f5224aeSachartre 	    VIO_both_dir, B_TRUE);
688078fcd0a1Sachartre 
68811ae08745Sheppo 	if (rv != 0) {
68821ae08745Sheppo 		/*
68831ae08745Sheppo 		 * This is not necessarily an error. The ioctl could
68841ae08745Sheppo 		 * be returning a value such as ENOTTY to indicate
68851ae08745Sheppo 		 * that the ioctl is not applicable.
68861ae08745Sheppo 		 */
68873af08d82Slm66018 		DMSG(vdc, 0, "[%d] vds returned %d for ioctl 0x%x\n",
6888e1ebb9ecSlm66018 		    instance, rv, cmd);
68891ae08745Sheppo 		if (mem_p != NULL)
68901ae08745Sheppo 			kmem_free(mem_p, alloc_len);
6891d10e4ef2Snarayan 
68921ae08745Sheppo 		return (rv);
68931ae08745Sheppo 	}
68941ae08745Sheppo 
68951ae08745Sheppo 	/*
68960a55fbb7Slm66018 	 * Call the conversion function (if it exists) for this ioctl
68970a55fbb7Slm66018 	 * which converts from the format ARC'ed as part of the vDisk
68980a55fbb7Slm66018 	 * protocol (FWARC 2006/195) back to a format understood by
68990a55fbb7Slm66018 	 * the rest of Solaris.
69001ae08745Sheppo 	 */
69013af08d82Slm66018 	rv = (iop->convert)(vdc, mem_p, arg, mode, VD_COPYOUT);
69020a55fbb7Slm66018 	if (rv != 0) {
69033af08d82Slm66018 		DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n",
6904e1ebb9ecSlm66018 		    instance, rv, cmd);
69051ae08745Sheppo 		if (mem_p != NULL)
69061ae08745Sheppo 			kmem_free(mem_p, alloc_len);
69070a55fbb7Slm66018 		return (rv);
69081ae08745Sheppo 	}
69091ae08745Sheppo 
69101ae08745Sheppo 	if (mem_p != NULL)
69111ae08745Sheppo 		kmem_free(mem_p, alloc_len);
69121ae08745Sheppo 
69131ae08745Sheppo 	return (rv);
69141ae08745Sheppo }
69151ae08745Sheppo 
69161ae08745Sheppo /*
69171ae08745Sheppo  * Function:
69180a55fbb7Slm66018  *
69190a55fbb7Slm66018  * Description:
69200a55fbb7Slm66018  *	This is an empty conversion function used by ioctl calls which
69210a55fbb7Slm66018  *	do not need to convert the data being passed in/out to userland
69220a55fbb7Slm66018  */
69230a55fbb7Slm66018 static int
6924d10e4ef2Snarayan vdc_null_copy_func(vdc_t *vdc, void *from, void *to, int mode, int dir)
69250a55fbb7Slm66018 {
6926d10e4ef2Snarayan 	_NOTE(ARGUNUSED(vdc))
69270a55fbb7Slm66018 	_NOTE(ARGUNUSED(from))
69280a55fbb7Slm66018 	_NOTE(ARGUNUSED(to))
69290a55fbb7Slm66018 	_NOTE(ARGUNUSED(mode))
69300a55fbb7Slm66018 	_NOTE(ARGUNUSED(dir))
69310a55fbb7Slm66018 
69320a55fbb7Slm66018 	return (0);
69330a55fbb7Slm66018 }
69340a55fbb7Slm66018 
69354bac2208Snarayan static int
69364bac2208Snarayan vdc_get_wce_convert(vdc_t *vdc, void *from, void *to,
69374bac2208Snarayan     int mode, int dir)
69384bac2208Snarayan {
69394bac2208Snarayan 	_NOTE(ARGUNUSED(vdc))
69404bac2208Snarayan 
69414bac2208Snarayan 	if (dir == VD_COPYIN)
69424bac2208Snarayan 		return (0);		/* nothing to do */
69434bac2208Snarayan 
69444bac2208Snarayan 	if (ddi_copyout(from, to, sizeof (int), mode) != 0)
69454bac2208Snarayan 		return (EFAULT);
69464bac2208Snarayan 
69474bac2208Snarayan 	return (0);
69484bac2208Snarayan }
69494bac2208Snarayan 
69504bac2208Snarayan static int
69514bac2208Snarayan vdc_set_wce_convert(vdc_t *vdc, void *from, void *to,
69524bac2208Snarayan     int mode, int dir)
69534bac2208Snarayan {
69544bac2208Snarayan 	_NOTE(ARGUNUSED(vdc))
69554bac2208Snarayan 
69564bac2208Snarayan 	if (dir == VD_COPYOUT)
69574bac2208Snarayan 		return (0);		/* nothing to do */
69584bac2208Snarayan 
69594bac2208Snarayan 	if (ddi_copyin(from, to, sizeof (int), mode) != 0)
69604bac2208Snarayan 		return (EFAULT);
69614bac2208Snarayan 
69624bac2208Snarayan 	return (0);
69634bac2208Snarayan }
69644bac2208Snarayan 
69650a55fbb7Slm66018 /*
69660a55fbb7Slm66018  * Function:
69670a55fbb7Slm66018  *	vdc_get_vtoc_convert()
69680a55fbb7Slm66018  *
69690a55fbb7Slm66018  * Description:
6970d10e4ef2Snarayan  *	This routine performs the necessary convertions from the DKIOCGVTOC
6971d10e4ef2Snarayan  *	Solaris structure to the format defined in FWARC 2006/195.
6972d10e4ef2Snarayan  *
6973d10e4ef2Snarayan  *	In the struct vtoc definition, the timestamp field is marked as not
6974d10e4ef2Snarayan  *	supported so it is not part of vDisk protocol (FWARC 2006/195).
6975d10e4ef2Snarayan  *	However SVM uses that field to check it can write into the VTOC,
6976d10e4ef2Snarayan  *	so we fake up the info of that field.
69770a55fbb7Slm66018  *
69780a55fbb7Slm66018  * Arguments:
6979d10e4ef2Snarayan  *	vdc	- the vDisk client
69800a55fbb7Slm66018  *	from	- the buffer containing the data to be copied from
69810a55fbb7Slm66018  *	to	- the buffer to be copied to
69820a55fbb7Slm66018  *	mode	- flags passed to ioctl() call
69830a55fbb7Slm66018  *	dir	- the "direction" of the copy - VD_COPYIN or VD_COPYOUT
69840a55fbb7Slm66018  *
69850a55fbb7Slm66018  * Return Code:
69860a55fbb7Slm66018  *	0	- Success
69870a55fbb7Slm66018  *	ENXIO	- incorrect buffer passed in.
6988d10e4ef2Snarayan  *	EFAULT	- ddi_copyout routine encountered an error.
69890a55fbb7Slm66018  */
69900a55fbb7Slm66018 static int
6991d10e4ef2Snarayan vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
69920a55fbb7Slm66018 {
6993d10e4ef2Snarayan 	int		i;
69940a55fbb7Slm66018 	void		*tmp_mem = NULL;
69950a55fbb7Slm66018 	void		*tmp_memp;
69960a55fbb7Slm66018 	struct vtoc	vt;
69970a55fbb7Slm66018 	struct vtoc32	vt32;
69980a55fbb7Slm66018 	int		copy_len = 0;
69990a55fbb7Slm66018 	int		rv = 0;
70000a55fbb7Slm66018 
70010a55fbb7Slm66018 	if (dir != VD_COPYOUT)
70020a55fbb7Slm66018 		return (0);	/* nothing to do */
70030a55fbb7Slm66018 
70040a55fbb7Slm66018 	if ((from == NULL) || (to == NULL))
70050a55fbb7Slm66018 		return (ENXIO);
70060a55fbb7Slm66018 
70070a55fbb7Slm66018 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32)
70080a55fbb7Slm66018 		copy_len = sizeof (struct vtoc32);
70090a55fbb7Slm66018 	else
70100a55fbb7Slm66018 		copy_len = sizeof (struct vtoc);
70110a55fbb7Slm66018 
70120a55fbb7Slm66018 	tmp_mem = kmem_alloc(copy_len, KM_SLEEP);
70130a55fbb7Slm66018 
70140a55fbb7Slm66018 	VD_VTOC2VTOC((vd_vtoc_t *)from, &vt);
7015d10e4ef2Snarayan 
7016d10e4ef2Snarayan 	/* fake the VTOC timestamp field */
7017d10e4ef2Snarayan 	for (i = 0; i < V_NUMPAR; i++) {
7018d10e4ef2Snarayan 		vt.timestamp[i] = vdc->vtoc->timestamp[i];
7019d10e4ef2Snarayan 	}
7020d10e4ef2Snarayan 
70210a55fbb7Slm66018 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
702217cadca8Slm66018 		/* LINTED E_ASSIGN_NARROW_CONV */
70230a55fbb7Slm66018 		vtoctovtoc32(vt, vt32);
70240a55fbb7Slm66018 		tmp_memp = &vt32;
70250a55fbb7Slm66018 	} else {
70260a55fbb7Slm66018 		tmp_memp = &vt;
70270a55fbb7Slm66018 	}
70280a55fbb7Slm66018 	rv = ddi_copyout(tmp_memp, to, copy_len, mode);
70290a55fbb7Slm66018 	if (rv != 0)
70300a55fbb7Slm66018 		rv = EFAULT;
70310a55fbb7Slm66018 
70320a55fbb7Slm66018 	kmem_free(tmp_mem, copy_len);
70330a55fbb7Slm66018 	return (rv);
70340a55fbb7Slm66018 }
70350a55fbb7Slm66018 
70360a55fbb7Slm66018 /*
70370a55fbb7Slm66018  * Function:
70380a55fbb7Slm66018  *	vdc_set_vtoc_convert()
70390a55fbb7Slm66018  *
70400a55fbb7Slm66018  * Description:
7041d10e4ef2Snarayan  *	This routine performs the necessary convertions from the DKIOCSVTOC
7042d10e4ef2Snarayan  *	Solaris structure to the format defined in FWARC 2006/195.
70430a55fbb7Slm66018  *
70440a55fbb7Slm66018  * Arguments:
7045d10e4ef2Snarayan  *	vdc	- the vDisk client
70460a55fbb7Slm66018  *	from	- Buffer with data
70470a55fbb7Slm66018  *	to	- Buffer where data is to be copied to
70480a55fbb7Slm66018  *	mode	- flags passed to ioctl
70490a55fbb7Slm66018  *	dir	- direction of copy (in or out)
70500a55fbb7Slm66018  *
70510a55fbb7Slm66018  * Return Code:
70520a55fbb7Slm66018  *	0	- Success
70530a55fbb7Slm66018  *	ENXIO	- Invalid buffer passed in
70540a55fbb7Slm66018  *	EFAULT	- ddi_copyin of data failed
70550a55fbb7Slm66018  */
70560a55fbb7Slm66018 static int
7057d10e4ef2Snarayan vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
70580a55fbb7Slm66018 {
705978fcd0a1Sachartre 	_NOTE(ARGUNUSED(vdc))
706078fcd0a1Sachartre 
70612f5224aeSachartre 	void		*tmp_mem = NULL, *uvtoc;
70620a55fbb7Slm66018 	struct vtoc	vt;
70630a55fbb7Slm66018 	struct vtoc	*vtp = &vt;
70640a55fbb7Slm66018 	vd_vtoc_t	vtvd;
70650a55fbb7Slm66018 	int		copy_len = 0;
70662f5224aeSachartre 	int		i, rv = 0;
70670a55fbb7Slm66018 
70680a55fbb7Slm66018 	if ((from == NULL) || (to == NULL))
70690a55fbb7Slm66018 		return (ENXIO);
70700a55fbb7Slm66018 
70712f5224aeSachartre 	if (dir == VD_COPYIN)
70722f5224aeSachartre 		uvtoc = from;
70732f5224aeSachartre 	else
70742f5224aeSachartre 		uvtoc = to;
70752f5224aeSachartre 
70760a55fbb7Slm66018 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32)
70770a55fbb7Slm66018 		copy_len = sizeof (struct vtoc32);
70780a55fbb7Slm66018 	else
70790a55fbb7Slm66018 		copy_len = sizeof (struct vtoc);
70800a55fbb7Slm66018 
70810a55fbb7Slm66018 	tmp_mem = kmem_alloc(copy_len, KM_SLEEP);
70820a55fbb7Slm66018 
70832f5224aeSachartre 	rv = ddi_copyin(uvtoc, tmp_mem, copy_len, mode);
70840a55fbb7Slm66018 	if (rv != 0) {
70850a55fbb7Slm66018 		kmem_free(tmp_mem, copy_len);
70860a55fbb7Slm66018 		return (EFAULT);
70870a55fbb7Slm66018 	}
70880a55fbb7Slm66018 
70890a55fbb7Slm66018 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
70900a55fbb7Slm66018 		vtoc32tovtoc((*(struct vtoc32 *)tmp_mem), vt);
70910a55fbb7Slm66018 	} else {
70920a55fbb7Slm66018 		vtp = tmp_mem;
70930a55fbb7Slm66018 	}
70940a55fbb7Slm66018 
70952f5224aeSachartre 	if (dir == VD_COPYOUT) {
70962f5224aeSachartre 		/*
70972f5224aeSachartre 		 * The disk label may have changed. Revalidate the disk
70982f5224aeSachartre 		 * geometry. This will also update the device nodes and
70992f5224aeSachartre 		 * properties.
71002f5224aeSachartre 		 */
71012f5224aeSachartre 		vdc_validate(vdc);
71022f5224aeSachartre 
71032f5224aeSachartre 		/*
71042f5224aeSachartre 		 * We also need to keep track of the timestamp fields.
71052f5224aeSachartre 		 */
71062f5224aeSachartre 		for (i = 0; i < V_NUMPAR; i++) {
71072f5224aeSachartre 			vdc->vtoc->timestamp[i] = vtp->timestamp[i];
71082f5224aeSachartre 		}
71092f5224aeSachartre 
71102f5224aeSachartre 		return (0);
71112f5224aeSachartre 	}
71122f5224aeSachartre 
71130a55fbb7Slm66018 	VTOC2VD_VTOC(vtp, &vtvd);
71140a55fbb7Slm66018 	bcopy(&vtvd, to, sizeof (vd_vtoc_t));
71150a55fbb7Slm66018 	kmem_free(tmp_mem, copy_len);
71160a55fbb7Slm66018 
71170a55fbb7Slm66018 	return (0);
71180a55fbb7Slm66018 }
71190a55fbb7Slm66018 
71200a55fbb7Slm66018 /*
71210a55fbb7Slm66018  * Function:
71220a55fbb7Slm66018  *	vdc_get_geom_convert()
71230a55fbb7Slm66018  *
71240a55fbb7Slm66018  * Description:
7125d10e4ef2Snarayan  *	This routine performs the necessary convertions from the DKIOCGGEOM,
7126d10e4ef2Snarayan  *	DKIOCG_PHYSGEOM and DKIOG_VIRTGEOM Solaris structures to the format
7127d10e4ef2Snarayan  *	defined in FWARC 2006/195
71280a55fbb7Slm66018  *
71290a55fbb7Slm66018  * Arguments:
7130d10e4ef2Snarayan  *	vdc	- the vDisk client
71310a55fbb7Slm66018  *	from	- Buffer with data
71320a55fbb7Slm66018  *	to	- Buffer where data is to be copied to
71330a55fbb7Slm66018  *	mode	- flags passed to ioctl
71340a55fbb7Slm66018  *	dir	- direction of copy (in or out)
71350a55fbb7Slm66018  *
71360a55fbb7Slm66018  * Return Code:
71370a55fbb7Slm66018  *	0	- Success
71380a55fbb7Slm66018  *	ENXIO	- Invalid buffer passed in
7139d10e4ef2Snarayan  *	EFAULT	- ddi_copyout of data failed
71400a55fbb7Slm66018  */
71410a55fbb7Slm66018 static int
7142d10e4ef2Snarayan vdc_get_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
71430a55fbb7Slm66018 {
7144d10e4ef2Snarayan 	_NOTE(ARGUNUSED(vdc))
7145d10e4ef2Snarayan 
71460a55fbb7Slm66018 	struct dk_geom	geom;
71470a55fbb7Slm66018 	int	copy_len = sizeof (struct dk_geom);
71480a55fbb7Slm66018 	int	rv = 0;
71490a55fbb7Slm66018 
71500a55fbb7Slm66018 	if (dir != VD_COPYOUT)
71510a55fbb7Slm66018 		return (0);	/* nothing to do */
71520a55fbb7Slm66018 
71530a55fbb7Slm66018 	if ((from == NULL) || (to == NULL))
71540a55fbb7Slm66018 		return (ENXIO);
71550a55fbb7Slm66018 
71560a55fbb7Slm66018 	VD_GEOM2DK_GEOM((vd_geom_t *)from, &geom);
71570a55fbb7Slm66018 	rv = ddi_copyout(&geom, to, copy_len, mode);
71580a55fbb7Slm66018 	if (rv != 0)
71590a55fbb7Slm66018 		rv = EFAULT;
71600a55fbb7Slm66018 
71610a55fbb7Slm66018 	return (rv);
71620a55fbb7Slm66018 }
71630a55fbb7Slm66018 
71640a55fbb7Slm66018 /*
71650a55fbb7Slm66018  * Function:
71660a55fbb7Slm66018  *	vdc_set_geom_convert()
71670a55fbb7Slm66018  *
71680a55fbb7Slm66018  * Description:
7169d10e4ef2Snarayan  *	This routine performs the necessary convertions from the DKIOCSGEOM
7170d10e4ef2Snarayan  *	Solaris structure to the format defined in FWARC 2006/195.
71710a55fbb7Slm66018  *
71720a55fbb7Slm66018  * Arguments:
7173d10e4ef2Snarayan  *	vdc	- the vDisk client
71740a55fbb7Slm66018  *	from	- Buffer with data
71750a55fbb7Slm66018  *	to	- Buffer where data is to be copied to
71760a55fbb7Slm66018  *	mode	- flags passed to ioctl
71770a55fbb7Slm66018  *	dir	- direction of copy (in or out)
71780a55fbb7Slm66018  *
71790a55fbb7Slm66018  * Return Code:
71800a55fbb7Slm66018  *	0	- Success
71810a55fbb7Slm66018  *	ENXIO	- Invalid buffer passed in
71820a55fbb7Slm66018  *	EFAULT	- ddi_copyin of data failed
71830a55fbb7Slm66018  */
71840a55fbb7Slm66018 static int
7185d10e4ef2Snarayan vdc_set_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
71860a55fbb7Slm66018 {
7187d10e4ef2Snarayan 	_NOTE(ARGUNUSED(vdc))
7188d10e4ef2Snarayan 
71890a55fbb7Slm66018 	vd_geom_t	vdgeom;
71900a55fbb7Slm66018 	void		*tmp_mem = NULL;
71910a55fbb7Slm66018 	int		copy_len = sizeof (struct dk_geom);
71920a55fbb7Slm66018 	int		rv = 0;
71930a55fbb7Slm66018 
71940a55fbb7Slm66018 	if (dir != VD_COPYIN)
71950a55fbb7Slm66018 		return (0);	/* nothing to do */
71960a55fbb7Slm66018 
71970a55fbb7Slm66018 	if ((from == NULL) || (to == NULL))
71980a55fbb7Slm66018 		return (ENXIO);
71990a55fbb7Slm66018 
72000a55fbb7Slm66018 	tmp_mem = kmem_alloc(copy_len, KM_SLEEP);
72010a55fbb7Slm66018 
72020a55fbb7Slm66018 	rv = ddi_copyin(from, tmp_mem, copy_len, mode);
72030a55fbb7Slm66018 	if (rv != 0) {
72040a55fbb7Slm66018 		kmem_free(tmp_mem, copy_len);
72050a55fbb7Slm66018 		return (EFAULT);
72060a55fbb7Slm66018 	}
72070a55fbb7Slm66018 	DK_GEOM2VD_GEOM((struct dk_geom *)tmp_mem, &vdgeom);
72080a55fbb7Slm66018 	bcopy(&vdgeom, to, sizeof (vdgeom));
72090a55fbb7Slm66018 	kmem_free(tmp_mem, copy_len);
72100a55fbb7Slm66018 
72110a55fbb7Slm66018 	return (0);
72120a55fbb7Slm66018 }
72130a55fbb7Slm66018 
72144bac2208Snarayan static int
72154bac2208Snarayan vdc_get_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
72164bac2208Snarayan {
72174bac2208Snarayan 	_NOTE(ARGUNUSED(vdc))
72184bac2208Snarayan 
72194bac2208Snarayan 	vd_efi_t	*vd_efi;
72204bac2208Snarayan 	dk_efi_t	dk_efi;
72214bac2208Snarayan 	int		rv = 0;
72224bac2208Snarayan 	void		*uaddr;
72234bac2208Snarayan 
72244bac2208Snarayan 	if ((from == NULL) || (to == NULL))
72254bac2208Snarayan 		return (ENXIO);
72264bac2208Snarayan 
72274bac2208Snarayan 	if (dir == VD_COPYIN) {
72284bac2208Snarayan 
72294bac2208Snarayan 		vd_efi = (vd_efi_t *)to;
72304bac2208Snarayan 
72314bac2208Snarayan 		rv = ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode);
72324bac2208Snarayan 		if (rv != 0)
72334bac2208Snarayan 			return (EFAULT);
72344bac2208Snarayan 
72354bac2208Snarayan 		vd_efi->lba = dk_efi.dki_lba;
72364bac2208Snarayan 		vd_efi->length = dk_efi.dki_length;
72374bac2208Snarayan 		bzero(vd_efi->data, vd_efi->length);
72384bac2208Snarayan 
72394bac2208Snarayan 	} else {
72404bac2208Snarayan 
72414bac2208Snarayan 		rv = ddi_copyin(to, &dk_efi, sizeof (dk_efi_t), mode);
72424bac2208Snarayan 		if (rv != 0)
72434bac2208Snarayan 			return (EFAULT);
72444bac2208Snarayan 
72454bac2208Snarayan 		uaddr = dk_efi.dki_data;
72464bac2208Snarayan 
72474bac2208Snarayan 		dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP);
72484bac2208Snarayan 
72494bac2208Snarayan 		VD_EFI2DK_EFI((vd_efi_t *)from, &dk_efi);
72504bac2208Snarayan 
72514bac2208Snarayan 		rv = ddi_copyout(dk_efi.dki_data, uaddr, dk_efi.dki_length,
72524bac2208Snarayan 		    mode);
72534bac2208Snarayan 		if (rv != 0)
72544bac2208Snarayan 			return (EFAULT);
72554bac2208Snarayan 
72564bac2208Snarayan 		kmem_free(dk_efi.dki_data, dk_efi.dki_length);
72574bac2208Snarayan 	}
72584bac2208Snarayan 
72594bac2208Snarayan 	return (0);
72604bac2208Snarayan }
72614bac2208Snarayan 
72624bac2208Snarayan static int
72634bac2208Snarayan vdc_set_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
72644bac2208Snarayan {
72654bac2208Snarayan 	_NOTE(ARGUNUSED(vdc))
72664bac2208Snarayan 
72674bac2208Snarayan 	dk_efi_t	dk_efi;
72684bac2208Snarayan 	void		*uaddr;
72694bac2208Snarayan 
72702f5224aeSachartre 	if (dir == VD_COPYOUT) {
72712f5224aeSachartre 		/*
72722f5224aeSachartre 		 * The disk label may have changed. Revalidate the disk
72732f5224aeSachartre 		 * geometry. This will also update the device nodes and
72742f5224aeSachartre 		 * properties.
72752f5224aeSachartre 		 */
72762f5224aeSachartre 		vdc_validate(vdc);
72772f5224aeSachartre 		return (0);
72782f5224aeSachartre 	}
72794bac2208Snarayan 
72804bac2208Snarayan 	if ((from == NULL) || (to == NULL))
72814bac2208Snarayan 		return (ENXIO);
72824bac2208Snarayan 
72834bac2208Snarayan 	if (ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode) != 0)
72844bac2208Snarayan 		return (EFAULT);
72854bac2208Snarayan 
72864bac2208Snarayan 	uaddr = dk_efi.dki_data;
72874bac2208Snarayan 
72884bac2208Snarayan 	dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP);
72894bac2208Snarayan 
72904bac2208Snarayan 	if (ddi_copyin(uaddr, dk_efi.dki_data, dk_efi.dki_length, mode) != 0)
72914bac2208Snarayan 		return (EFAULT);
72924bac2208Snarayan 
72934bac2208Snarayan 	DK_EFI2VD_EFI(&dk_efi, (vd_efi_t *)to);
72944bac2208Snarayan 
72954bac2208Snarayan 	kmem_free(dk_efi.dki_data, dk_efi.dki_length);
72964bac2208Snarayan 
72974bac2208Snarayan 	return (0);
72984bac2208Snarayan }
72994bac2208Snarayan 
730017cadca8Slm66018 
730117cadca8Slm66018 /* -------------------------------------------------------------------------- */
730217cadca8Slm66018 
73030a55fbb7Slm66018 /*
73040a55fbb7Slm66018  * Function:
73051ae08745Sheppo  *	vdc_create_fake_geometry()
73061ae08745Sheppo  *
73071ae08745Sheppo  * Description:
730817cadca8Slm66018  *	This routine fakes up the disk info needed for some DKIO ioctls such
730917cadca8Slm66018  *	as DKIOCINFO and DKIOCGMEDIAINFO [just like lofi(7D) and ramdisk(7D) do]
73101ae08745Sheppo  *
731117cadca8Slm66018  *	Note: This function must not be called until the vDisk attributes have
731217cadca8Slm66018  *	been exchanged as part of the handshake with the vDisk server.
73131ae08745Sheppo  *
73141ae08745Sheppo  * Arguments:
73151ae08745Sheppo  *	vdc	- soft state pointer for this instance of the device driver.
73161ae08745Sheppo  *
73171ae08745Sheppo  * Return Code:
731878fcd0a1Sachartre  *	none.
73191ae08745Sheppo  */
732078fcd0a1Sachartre static void
73211ae08745Sheppo vdc_create_fake_geometry(vdc_t *vdc)
73221ae08745Sheppo {
73231ae08745Sheppo 	ASSERT(vdc != NULL);
732478fcd0a1Sachartre 	ASSERT(vdc->max_xfer_sz != 0);
73250d0c8d4bSnarayan 
73260d0c8d4bSnarayan 	/*
73271ae08745Sheppo 	 * DKIOCINFO support
73281ae08745Sheppo 	 */
732978fcd0a1Sachartre 	if (vdc->cinfo == NULL)
73301ae08745Sheppo 		vdc->cinfo = kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
73311ae08745Sheppo 
73321ae08745Sheppo 	(void) strcpy(vdc->cinfo->dki_cname, VDC_DRIVER_NAME);
73331ae08745Sheppo 	(void) strcpy(vdc->cinfo->dki_dname, VDC_DRIVER_NAME);
73348e6a2a04Slm66018 	/* max_xfer_sz is #blocks so we don't need to divide by DEV_BSIZE */
73358e6a2a04Slm66018 	vdc->cinfo->dki_maxtransfer = vdc->max_xfer_sz;
73362f5224aeSachartre 
733787a7269eSachartre 	/*
73382f5224aeSachartre 	 * We set the controller type to DKC_SCSI_CCS only if the VD_OP_SCSICMD
73392f5224aeSachartre 	 * operation is supported, otherwise the controller type is DKC_DIRECT.
73402f5224aeSachartre 	 * Version 1.0 does not support the VD_OP_SCSICMD operation, so the
73412f5224aeSachartre 	 * controller type is always DKC_DIRECT in that case.
73422f5224aeSachartre 	 *
734317cadca8Slm66018 	 * If the virtual disk is backed by a physical CD/DVD device or
734417cadca8Slm66018 	 * an ISO image, modify the controller type to indicate this
734587a7269eSachartre 	 */
734617cadca8Slm66018 	switch (vdc->vdisk_media) {
734717cadca8Slm66018 	case VD_MEDIA_CD:
734817cadca8Slm66018 	case VD_MEDIA_DVD:
734917cadca8Slm66018 		vdc->cinfo->dki_ctype = DKC_CDROM;
735017cadca8Slm66018 		break;
735117cadca8Slm66018 	case VD_MEDIA_FIXED:
73522f5224aeSachartre 		if (VD_OP_SUPPORTED(vdc->operations, VD_OP_SCSICMD))
73532f5224aeSachartre 			vdc->cinfo->dki_ctype = DKC_SCSI_CCS;
73542f5224aeSachartre 		else
735587a7269eSachartre 			vdc->cinfo->dki_ctype = DKC_DIRECT;
735617cadca8Slm66018 		break;
735717cadca8Slm66018 	default:
735817cadca8Slm66018 		/* in the case of v1.0 we default to a fixed disk */
735917cadca8Slm66018 		vdc->cinfo->dki_ctype = DKC_DIRECT;
736017cadca8Slm66018 		break;
736117cadca8Slm66018 	}
73621ae08745Sheppo 	vdc->cinfo->dki_flags = DKI_FMTVOL;
73631ae08745Sheppo 	vdc->cinfo->dki_cnum = 0;
73641ae08745Sheppo 	vdc->cinfo->dki_addr = 0;
73651ae08745Sheppo 	vdc->cinfo->dki_space = 0;
73661ae08745Sheppo 	vdc->cinfo->dki_prio = 0;
73671ae08745Sheppo 	vdc->cinfo->dki_vec = 0;
73681ae08745Sheppo 	vdc->cinfo->dki_unit = vdc->instance;
73691ae08745Sheppo 	vdc->cinfo->dki_slave = 0;
73701ae08745Sheppo 	/*
73711ae08745Sheppo 	 * The partition number will be created on the fly depending on the
73721ae08745Sheppo 	 * actual slice (i.e. minor node) that is used to request the data.
73731ae08745Sheppo 	 */
73741ae08745Sheppo 	vdc->cinfo->dki_partition = 0;
73751ae08745Sheppo 
73761ae08745Sheppo 	/*
73771ae08745Sheppo 	 * DKIOCGMEDIAINFO support
73781ae08745Sheppo 	 */
73790a55fbb7Slm66018 	if (vdc->minfo == NULL)
73801ae08745Sheppo 		vdc->minfo = kmem_zalloc(sizeof (struct dk_minfo), KM_SLEEP);
738117cadca8Slm66018 
738217cadca8Slm66018 	if (vio_ver_is_supported(vdc->ver, 1, 1)) {
738317cadca8Slm66018 		vdc->minfo->dki_media_type =
738417cadca8Slm66018 		    VD_MEDIATYPE2DK_MEDIATYPE(vdc->vdisk_media);
738517cadca8Slm66018 	} else {
73861ae08745Sheppo 		vdc->minfo->dki_media_type = DK_FIXED_DISK;
738717cadca8Slm66018 	}
738817cadca8Slm66018 
73894bac2208Snarayan 	vdc->minfo->dki_capacity = vdc->vdisk_size;
739017cadca8Slm66018 	vdc->minfo->dki_lbsize = vdc->block_size;
739178fcd0a1Sachartre }
73921ae08745Sheppo 
739378fcd0a1Sachartre static ushort_t
739478fcd0a1Sachartre vdc_lbl2cksum(struct dk_label *label)
739578fcd0a1Sachartre {
739678fcd0a1Sachartre 	int	count;
739778fcd0a1Sachartre 	ushort_t sum, *sp;
739878fcd0a1Sachartre 
739978fcd0a1Sachartre 	count =	(sizeof (struct dk_label)) / (sizeof (short)) - 1;
740078fcd0a1Sachartre 	sp = (ushort_t *)label;
740178fcd0a1Sachartre 	sum = 0;
740278fcd0a1Sachartre 	while (count--) {
740378fcd0a1Sachartre 		sum ^= *sp++;
740478fcd0a1Sachartre 	}
740578fcd0a1Sachartre 
740678fcd0a1Sachartre 	return (sum);
74070a55fbb7Slm66018 }
74080a55fbb7Slm66018 
74090a55fbb7Slm66018 /*
74100a55fbb7Slm66018  * Function:
741178fcd0a1Sachartre  *	vdc_validate_geometry
74120a55fbb7Slm66018  *
74130a55fbb7Slm66018  * Description:
741478fcd0a1Sachartre  *	This routine discovers the label and geometry of the disk. It stores
741578fcd0a1Sachartre  *	the disk label and related information in the vdc structure. If it
741678fcd0a1Sachartre  *	fails to validate the geometry or to discover the disk label then
741778fcd0a1Sachartre  *	the label is marked as unknown (VD_DISK_LABEL_UNK).
74180a55fbb7Slm66018  *
74190a55fbb7Slm66018  * Arguments:
74200a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
74210a55fbb7Slm66018  *
74220a55fbb7Slm66018  * Return Code:
742378fcd0a1Sachartre  *	0	- success.
742478fcd0a1Sachartre  *	EINVAL	- unknown disk label.
742578fcd0a1Sachartre  *	ENOTSUP	- geometry not applicable (EFI label).
742678fcd0a1Sachartre  *	EIO	- error accessing the disk.
74270a55fbb7Slm66018  */
74280a55fbb7Slm66018 static int
742978fcd0a1Sachartre vdc_validate_geometry(vdc_t *vdc)
74300a55fbb7Slm66018 {
7431d10e4ef2Snarayan 	buf_t	*buf;	/* BREAD requests need to be in a buf_t structure */
74320a55fbb7Slm66018 	dev_t	dev;
74332f5224aeSachartre 	int	rv, rval;
743478fcd0a1Sachartre 	struct dk_label label;
743578fcd0a1Sachartre 	struct dk_geom geom;
743678fcd0a1Sachartre 	struct vtoc vtoc;
7437edcc0754Sachartre 	efi_gpt_t *gpt;
7438edcc0754Sachartre 	efi_gpe_t *gpe;
7439edcc0754Sachartre 	vd_efi_dev_t edev;
74400a55fbb7Slm66018 
74410a55fbb7Slm66018 	ASSERT(vdc != NULL);
744278fcd0a1Sachartre 	ASSERT(vdc->vtoc != NULL && vdc->geom != NULL);
744378fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
74440a55fbb7Slm66018 
744578fcd0a1Sachartre 	mutex_exit(&vdc->lock);
74460a55fbb7Slm66018 
74470a55fbb7Slm66018 	dev = makedevice(ddi_driver_major(vdc->dip),
74480a55fbb7Slm66018 	    VD_MAKE_DEV(vdc->instance, 0));
74494bac2208Snarayan 
74502f5224aeSachartre 	rv = vd_process_ioctl(dev, DKIOCGGEOM, (caddr_t)&geom, FKIOCTL, &rval);
745178fcd0a1Sachartre 	if (rv == 0)
74522f5224aeSachartre 		rv = vd_process_ioctl(dev, DKIOCGVTOC, (caddr_t)&vtoc,
74532f5224aeSachartre 		    FKIOCTL, &rval);
74540d0c8d4bSnarayan 
74554bac2208Snarayan 	if (rv == ENOTSUP) {
74564bac2208Snarayan 		/*
74574bac2208Snarayan 		 * If the device does not support VTOC then we try
74584bac2208Snarayan 		 * to read an EFI label.
7459edcc0754Sachartre 		 *
7460edcc0754Sachartre 		 * We need to know the block size and the disk size to
7461edcc0754Sachartre 		 * be able to read an EFI label.
74624bac2208Snarayan 		 */
7463edcc0754Sachartre 		if (vdc->vdisk_size == 0) {
7464edcc0754Sachartre 			if ((rv = vdc_check_capacity(vdc)) != 0) {
7465edcc0754Sachartre 				mutex_enter(&vdc->lock);
7466edcc0754Sachartre 				vdc_store_label_unk(vdc);
7467edcc0754Sachartre 				return (rv);
7468edcc0754Sachartre 			}
7469edcc0754Sachartre 		}
74704bac2208Snarayan 
7471edcc0754Sachartre 		VD_EFI_DEV_SET(edev, vdc, vd_process_efi_ioctl);
7472edcc0754Sachartre 
7473edcc0754Sachartre 		rv = vd_efi_alloc_and_read(&edev, &gpt, &gpe);
74744bac2208Snarayan 
74754bac2208Snarayan 		if (rv) {
74763af08d82Slm66018 			DMSG(vdc, 0, "[%d] Failed to get EFI (err=%d)",
74774bac2208Snarayan 			    vdc->instance, rv);
747878fcd0a1Sachartre 			mutex_enter(&vdc->lock);
747978fcd0a1Sachartre 			vdc_store_label_unk(vdc);
748078fcd0a1Sachartre 			return (EIO);
748178fcd0a1Sachartre 		}
748278fcd0a1Sachartre 
748378fcd0a1Sachartre 		mutex_enter(&vdc->lock);
7484edcc0754Sachartre 		vdc_store_label_efi(vdc, gpt, gpe);
7485edcc0754Sachartre 		vd_efi_free(&edev, gpt, gpe);
748678fcd0a1Sachartre 		return (ENOTSUP);
748778fcd0a1Sachartre 	}
748878fcd0a1Sachartre 
748978fcd0a1Sachartre 	if (rv != 0) {
749078fcd0a1Sachartre 		DMSG(vdc, 0, "[%d] Failed to get VTOC (err=%d)",
749178fcd0a1Sachartre 		    vdc->instance, rv);
749278fcd0a1Sachartre 		mutex_enter(&vdc->lock);
749378fcd0a1Sachartre 		vdc_store_label_unk(vdc);
749478fcd0a1Sachartre 		if (rv != EINVAL)
749578fcd0a1Sachartre 			rv = EIO;
74964bac2208Snarayan 		return (rv);
74974bac2208Snarayan 	}
74984bac2208Snarayan 
749978fcd0a1Sachartre 	/* check that geometry and vtoc are valid */
750078fcd0a1Sachartre 	if (geom.dkg_nhead == 0 || geom.dkg_nsect == 0 ||
750178fcd0a1Sachartre 	    vtoc.v_sanity != VTOC_SANE) {
750278fcd0a1Sachartre 		mutex_enter(&vdc->lock);
750378fcd0a1Sachartre 		vdc_store_label_unk(vdc);
750478fcd0a1Sachartre 		return (EINVAL);
750578fcd0a1Sachartre 	}
75064bac2208Snarayan 
750778fcd0a1Sachartre 	/*
750878fcd0a1Sachartre 	 * We have a disk and a valid VTOC. However this does not mean
750978fcd0a1Sachartre 	 * that the disk currently have a VTOC label. The returned VTOC may
751078fcd0a1Sachartre 	 * be a default VTOC to be used for configuring the disk (this is
751178fcd0a1Sachartre 	 * what is done for disk image). So we read the label from the
751278fcd0a1Sachartre 	 * beginning of the disk to ensure we really have a VTOC label.
751378fcd0a1Sachartre 	 *
751478fcd0a1Sachartre 	 * FUTURE: This could be the default way for reading the VTOC
751578fcd0a1Sachartre 	 * from the disk as opposed to sending the VD_OP_GET_VTOC
751678fcd0a1Sachartre 	 * to the server. This will be the default if vdc is implemented
751778fcd0a1Sachartre 	 * ontop of cmlb.
751878fcd0a1Sachartre 	 */
751978fcd0a1Sachartre 
752078fcd0a1Sachartre 	/*
752178fcd0a1Sachartre 	 * Single slice disk does not support read using an absolute disk
752278fcd0a1Sachartre 	 * offset so we just rely on the DKIOCGVTOC ioctl in that case.
752378fcd0a1Sachartre 	 */
752478fcd0a1Sachartre 	if (vdc->vdisk_type == VD_DISK_TYPE_SLICE) {
752578fcd0a1Sachartre 		mutex_enter(&vdc->lock);
752678fcd0a1Sachartre 		if (vtoc.v_nparts != 1) {
752778fcd0a1Sachartre 			vdc_store_label_unk(vdc);
752878fcd0a1Sachartre 			return (EINVAL);
752978fcd0a1Sachartre 		}
753078fcd0a1Sachartre 		vdc_store_label_vtoc(vdc, &geom, &vtoc);
75314bac2208Snarayan 		return (0);
75324bac2208Snarayan 	}
75334bac2208Snarayan 
753478fcd0a1Sachartre 	if (vtoc.v_nparts != V_NUMPAR) {
753578fcd0a1Sachartre 		mutex_enter(&vdc->lock);
753678fcd0a1Sachartre 		vdc_store_label_unk(vdc);
753778fcd0a1Sachartre 		return (EINVAL);
75380a55fbb7Slm66018 	}
7539d10e4ef2Snarayan 
7540d10e4ef2Snarayan 	/*
7541d10e4ef2Snarayan 	 * Read disk label from start of disk
7542d10e4ef2Snarayan 	 */
7543d10e4ef2Snarayan 	buf = kmem_alloc(sizeof (buf_t), KM_SLEEP);
7544d10e4ef2Snarayan 	bioinit(buf);
754578fcd0a1Sachartre 	buf->b_un.b_addr = (caddr_t)&label;
7546d10e4ef2Snarayan 	buf->b_bcount = DK_LABEL_SIZE;
7547d10e4ef2Snarayan 	buf->b_flags = B_BUSY | B_READ;
754817cadca8Slm66018 	buf->b_dev = cmpdev(dev);
754978fcd0a1Sachartre 	rv = vdc_send_request(vdc, VD_OP_BREAD, (caddr_t)&label,
755078fcd0a1Sachartre 	    DK_LABEL_SIZE, VD_SLICE_NONE, 0, CB_STRATEGY, buf, VIO_read_dir);
75513af08d82Slm66018 	if (rv) {
75523af08d82Slm66018 		DMSG(vdc, 1, "[%d] Failed to read disk block 0\n",
75533af08d82Slm66018 		    vdc->instance);
755478fcd0a1Sachartre 	} else {
7555d10e4ef2Snarayan 		rv = biowait(buf);
7556d10e4ef2Snarayan 		biofini(buf);
755778fcd0a1Sachartre 	}
7558d10e4ef2Snarayan 	kmem_free(buf, sizeof (buf_t));
75590a55fbb7Slm66018 
756078fcd0a1Sachartre 	if (rv != 0 || label.dkl_magic != DKL_MAGIC ||
756178fcd0a1Sachartre 	    label.dkl_cksum != vdc_lbl2cksum(&label)) {
756278fcd0a1Sachartre 		DMSG(vdc, 1, "[%d] Got VTOC with invalid label\n",
756378fcd0a1Sachartre 		    vdc->instance);
756478fcd0a1Sachartre 		mutex_enter(&vdc->lock);
756578fcd0a1Sachartre 		vdc_store_label_unk(vdc);
756678fcd0a1Sachartre 		return (EINVAL);
756778fcd0a1Sachartre 	}
756878fcd0a1Sachartre 
756978fcd0a1Sachartre 	mutex_enter(&vdc->lock);
757078fcd0a1Sachartre 	vdc_store_label_vtoc(vdc, &geom, &vtoc);
757178fcd0a1Sachartre 	return (0);
757278fcd0a1Sachartre }
757378fcd0a1Sachartre 
757478fcd0a1Sachartre /*
757578fcd0a1Sachartre  * Function:
757678fcd0a1Sachartre  *	vdc_validate
757778fcd0a1Sachartre  *
757878fcd0a1Sachartre  * Description:
757978fcd0a1Sachartre  *	This routine discovers the label of the disk and create the
758078fcd0a1Sachartre  *	appropriate device nodes if the label has changed.
758178fcd0a1Sachartre  *
758278fcd0a1Sachartre  * Arguments:
758378fcd0a1Sachartre  *	vdc	- soft state pointer for this instance of the device driver.
758478fcd0a1Sachartre  *
758578fcd0a1Sachartre  * Return Code:
758678fcd0a1Sachartre  *	none.
758778fcd0a1Sachartre  */
758878fcd0a1Sachartre static void
758978fcd0a1Sachartre vdc_validate(vdc_t *vdc)
759078fcd0a1Sachartre {
759178fcd0a1Sachartre 	vd_disk_label_t old_label;
7592edcc0754Sachartre 	vd_slice_t old_slice[V_NUMPAR];
759378fcd0a1Sachartre 	int rv;
759478fcd0a1Sachartre 
759578fcd0a1Sachartre 	ASSERT(!MUTEX_HELD(&vdc->lock));
759678fcd0a1Sachartre 
759778fcd0a1Sachartre 	mutex_enter(&vdc->lock);
759878fcd0a1Sachartre 
759978fcd0a1Sachartre 	/* save the current label and vtoc */
760078fcd0a1Sachartre 	old_label = vdc->vdisk_label;
7601edcc0754Sachartre 	bcopy(vdc->slice, &old_slice, sizeof (vd_slice_t) * V_NUMPAR);
760278fcd0a1Sachartre 
760378fcd0a1Sachartre 	/* check the geometry */
760478fcd0a1Sachartre 	(void) vdc_validate_geometry(vdc);
760578fcd0a1Sachartre 
760678fcd0a1Sachartre 	/* if the disk label has changed, update device nodes */
760778fcd0a1Sachartre 	if (vdc->vdisk_label != old_label) {
760878fcd0a1Sachartre 
760978fcd0a1Sachartre 		if (vdc->vdisk_label == VD_DISK_LABEL_EFI)
761078fcd0a1Sachartre 			rv = vdc_create_device_nodes_efi(vdc);
761178fcd0a1Sachartre 		else
761278fcd0a1Sachartre 			rv = vdc_create_device_nodes_vtoc(vdc);
761378fcd0a1Sachartre 
761478fcd0a1Sachartre 		if (rv != 0) {
761578fcd0a1Sachartre 			DMSG(vdc, 0, "![%d] Failed to update device nodes",
761678fcd0a1Sachartre 			    vdc->instance);
761778fcd0a1Sachartre 		}
761878fcd0a1Sachartre 	}
761978fcd0a1Sachartre 
762078fcd0a1Sachartre 	/* if the vtoc has changed, update device nodes properties */
7621edcc0754Sachartre 	if (bcmp(vdc->slice, &old_slice, sizeof (vd_slice_t) * V_NUMPAR) != 0) {
762278fcd0a1Sachartre 
762378fcd0a1Sachartre 		if (vdc_create_device_nodes_props(vdc) != 0) {
762478fcd0a1Sachartre 			DMSG(vdc, 0, "![%d] Failed to update device nodes"
762578fcd0a1Sachartre 			    " properties", vdc->instance);
762678fcd0a1Sachartre 		}
762778fcd0a1Sachartre 	}
762878fcd0a1Sachartre 
762978fcd0a1Sachartre 	mutex_exit(&vdc->lock);
763078fcd0a1Sachartre }
763178fcd0a1Sachartre 
763278fcd0a1Sachartre static void
763378fcd0a1Sachartre vdc_validate_task(void *arg)
763478fcd0a1Sachartre {
763578fcd0a1Sachartre 	vdc_t *vdc = (vdc_t *)arg;
763678fcd0a1Sachartre 
763778fcd0a1Sachartre 	vdc_validate(vdc);
763878fcd0a1Sachartre 
763978fcd0a1Sachartre 	mutex_enter(&vdc->lock);
764078fcd0a1Sachartre 	ASSERT(vdc->validate_pending > 0);
764178fcd0a1Sachartre 	vdc->validate_pending--;
764278fcd0a1Sachartre 	mutex_exit(&vdc->lock);
76431ae08745Sheppo }
76444bac2208Snarayan 
76454bac2208Snarayan /*
76464bac2208Snarayan  * Function:
76474bac2208Snarayan  *	vdc_setup_devid()
76484bac2208Snarayan  *
76494bac2208Snarayan  * Description:
76504bac2208Snarayan  *	This routine discovers the devid of a vDisk. It requests the devid of
76514bac2208Snarayan  *	the underlying device from the vDisk server, builds an encapsulated
76524bac2208Snarayan  *	devid based on the retrieved devid and registers that new devid to
76534bac2208Snarayan  *	the vDisk.
76544bac2208Snarayan  *
76554bac2208Snarayan  * Arguments:
76564bac2208Snarayan  *	vdc	- soft state pointer for this instance of the device driver.
76574bac2208Snarayan  *
76584bac2208Snarayan  * Return Code:
76594bac2208Snarayan  *	0	- A devid was succesfully registered for the vDisk
76604bac2208Snarayan  */
76614bac2208Snarayan static int
76624bac2208Snarayan vdc_setup_devid(vdc_t *vdc)
76634bac2208Snarayan {
76644bac2208Snarayan 	int rv;
76654bac2208Snarayan 	vd_devid_t *vd_devid;
76664bac2208Snarayan 	size_t bufsize, bufid_len;
76674bac2208Snarayan 
76684bac2208Snarayan 	/*
76694bac2208Snarayan 	 * At first sight, we don't know the size of the devid that the
76704bac2208Snarayan 	 * server will return but this size will be encoded into the
76714bac2208Snarayan 	 * reply. So we do a first request using a default size then we
76724bac2208Snarayan 	 * check if this size was large enough. If not then we do a second
76734bac2208Snarayan 	 * request with the correct size returned by the server. Note that
76744bac2208Snarayan 	 * ldc requires size to be 8-byte aligned.
76754bac2208Snarayan 	 */
76764bac2208Snarayan 	bufsize = P2ROUNDUP(VD_DEVID_SIZE(VD_DEVID_DEFAULT_LEN),
76774bac2208Snarayan 	    sizeof (uint64_t));
76784bac2208Snarayan 	vd_devid = kmem_zalloc(bufsize, KM_SLEEP);
76794bac2208Snarayan 	bufid_len = bufsize - sizeof (vd_efi_t) - 1;
76804bac2208Snarayan 
76813af08d82Slm66018 	rv = vdc_do_sync_op(vdc, VD_OP_GET_DEVID, (caddr_t)vd_devid,
76822f5224aeSachartre 	    bufsize, 0, 0, CB_SYNC, 0, VIO_both_dir, B_TRUE);
76833af08d82Slm66018 
76843af08d82Slm66018 	DMSG(vdc, 2, "sync_op returned %d\n", rv);
76853af08d82Slm66018 
76864bac2208Snarayan 	if (rv) {
76874bac2208Snarayan 		kmem_free(vd_devid, bufsize);
76884bac2208Snarayan 		return (rv);
76894bac2208Snarayan 	}
76904bac2208Snarayan 
76914bac2208Snarayan 	if (vd_devid->length > bufid_len) {
76924bac2208Snarayan 		/*
76934bac2208Snarayan 		 * The returned devid is larger than the buffer used. Try again
76944bac2208Snarayan 		 * with a buffer with the right size.
76954bac2208Snarayan 		 */
76964bac2208Snarayan 		kmem_free(vd_devid, bufsize);
76974bac2208Snarayan 		bufsize = P2ROUNDUP(VD_DEVID_SIZE(vd_devid->length),
76984bac2208Snarayan 		    sizeof (uint64_t));
76994bac2208Snarayan 		vd_devid = kmem_zalloc(bufsize, KM_SLEEP);
77004bac2208Snarayan 		bufid_len = bufsize - sizeof (vd_efi_t) - 1;
77014bac2208Snarayan 
77023af08d82Slm66018 		rv = vdc_do_sync_op(vdc, VD_OP_GET_DEVID,
77033af08d82Slm66018 		    (caddr_t)vd_devid, bufsize, 0, 0, CB_SYNC, 0,
77042f5224aeSachartre 		    VIO_both_dir, B_TRUE);
77053af08d82Slm66018 
77064bac2208Snarayan 		if (rv) {
77074bac2208Snarayan 			kmem_free(vd_devid, bufsize);
77084bac2208Snarayan 			return (rv);
77094bac2208Snarayan 		}
77104bac2208Snarayan 	}
77114bac2208Snarayan 
77124bac2208Snarayan 	/*
77134bac2208Snarayan 	 * The virtual disk should have the same device id as the one associated
77144bac2208Snarayan 	 * with the physical disk it is mapped on, otherwise sharing a disk
77154bac2208Snarayan 	 * between a LDom and a non-LDom may not work (for example for a shared
77164bac2208Snarayan 	 * SVM disk set).
77174bac2208Snarayan 	 *
77184bac2208Snarayan 	 * The DDI framework does not allow creating a device id with any
77194bac2208Snarayan 	 * type so we first create a device id of type DEVID_ENCAP and then
77204bac2208Snarayan 	 * we restore the orignal type of the physical device.
77214bac2208Snarayan 	 */
77224bac2208Snarayan 
77233af08d82Slm66018 	DMSG(vdc, 2, ": devid length = %d\n", vd_devid->length);
77243af08d82Slm66018 
77254bac2208Snarayan 	/* build an encapsulated devid based on the returned devid */
77264bac2208Snarayan 	if (ddi_devid_init(vdc->dip, DEVID_ENCAP, vd_devid->length,
77274bac2208Snarayan 	    vd_devid->id, &vdc->devid) != DDI_SUCCESS) {
77283af08d82Slm66018 		DMSG(vdc, 1, "[%d] Fail to created devid\n", vdc->instance);
77294bac2208Snarayan 		kmem_free(vd_devid, bufsize);
77304bac2208Snarayan 		return (1);
77314bac2208Snarayan 	}
77324bac2208Snarayan 
77334bac2208Snarayan 	DEVID_FORMTYPE((impl_devid_t *)vdc->devid, vd_devid->type);
77344bac2208Snarayan 
77354bac2208Snarayan 	ASSERT(ddi_devid_valid(vdc->devid) == DDI_SUCCESS);
77364bac2208Snarayan 
77374bac2208Snarayan 	kmem_free(vd_devid, bufsize);
77384bac2208Snarayan 
77394bac2208Snarayan 	if (ddi_devid_register(vdc->dip, vdc->devid) != DDI_SUCCESS) {
77403af08d82Slm66018 		DMSG(vdc, 1, "[%d] Fail to register devid\n", vdc->instance);
77414bac2208Snarayan 		return (1);
77424bac2208Snarayan 	}
77434bac2208Snarayan 
77444bac2208Snarayan 	return (0);
77454bac2208Snarayan }
77464bac2208Snarayan 
77474bac2208Snarayan static void
7748edcc0754Sachartre vdc_store_label_efi(vdc_t *vdc, efi_gpt_t *gpt, efi_gpe_t *gpe)
77494bac2208Snarayan {
7750edcc0754Sachartre 	int i, nparts;
77514bac2208Snarayan 
775278fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
775378fcd0a1Sachartre 
775478fcd0a1Sachartre 	vdc->vdisk_label = VD_DISK_LABEL_EFI;
7755edcc0754Sachartre 	bzero(vdc->vtoc, sizeof (struct vtoc));
775678fcd0a1Sachartre 	bzero(vdc->geom, sizeof (struct dk_geom));
7757edcc0754Sachartre 	bzero(vdc->slice, sizeof (vd_slice_t) * V_NUMPAR);
7758edcc0754Sachartre 
7759edcc0754Sachartre 	nparts = gpt->efi_gpt_NumberOfPartitionEntries;
7760edcc0754Sachartre 
7761edcc0754Sachartre 	for (i = 0; i < nparts && i < VD_EFI_WD_SLICE; i++) {
7762edcc0754Sachartre 
7763edcc0754Sachartre 		if (gpe[i].efi_gpe_StartingLBA == 0 ||
7764edcc0754Sachartre 		    gpe[i].efi_gpe_EndingLBA == 0) {
7765edcc0754Sachartre 			continue;
77664bac2208Snarayan 		}
7767edcc0754Sachartre 
7768edcc0754Sachartre 		vdc->slice[i].start = gpe[i].efi_gpe_StartingLBA;
7769edcc0754Sachartre 		vdc->slice[i].nblocks = gpe[i].efi_gpe_EndingLBA -
7770edcc0754Sachartre 		    gpe[i].efi_gpe_StartingLBA + 1;
7771edcc0754Sachartre 	}
7772edcc0754Sachartre 
7773edcc0754Sachartre 	ASSERT(vdc->vdisk_size != 0);
7774edcc0754Sachartre 	vdc->slice[VD_EFI_WD_SLICE].start = 0;
7775edcc0754Sachartre 	vdc->slice[VD_EFI_WD_SLICE].nblocks = vdc->vdisk_size;
7776edcc0754Sachartre 
77774bac2208Snarayan }
777878fcd0a1Sachartre 
777978fcd0a1Sachartre static void
778078fcd0a1Sachartre vdc_store_label_vtoc(vdc_t *vdc, struct dk_geom *geom, struct vtoc *vtoc)
778178fcd0a1Sachartre {
7782edcc0754Sachartre 	int i;
7783edcc0754Sachartre 
778478fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
7785edcc0754Sachartre 	ASSERT(vdc->block_size == vtoc->v_sectorsz);
778678fcd0a1Sachartre 
778778fcd0a1Sachartre 	vdc->vdisk_label = VD_DISK_LABEL_VTOC;
778878fcd0a1Sachartre 	bcopy(vtoc, vdc->vtoc, sizeof (struct vtoc));
778978fcd0a1Sachartre 	bcopy(geom, vdc->geom, sizeof (struct dk_geom));
7790edcc0754Sachartre 	bzero(vdc->slice, sizeof (vd_slice_t) * V_NUMPAR);
7791edcc0754Sachartre 
7792edcc0754Sachartre 	for (i = 0; i < vtoc->v_nparts; i++) {
7793edcc0754Sachartre 		vdc->slice[i].start = vtoc->v_part[i].p_start;
7794edcc0754Sachartre 		vdc->slice[i].nblocks = vtoc->v_part[i].p_size;
7795edcc0754Sachartre 	}
779678fcd0a1Sachartre }
779778fcd0a1Sachartre 
779878fcd0a1Sachartre static void
779978fcd0a1Sachartre vdc_store_label_unk(vdc_t *vdc)
780078fcd0a1Sachartre {
780178fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
780278fcd0a1Sachartre 
780378fcd0a1Sachartre 	vdc->vdisk_label = VD_DISK_LABEL_UNK;
780478fcd0a1Sachartre 	bzero(vdc->vtoc, sizeof (struct vtoc));
780578fcd0a1Sachartre 	bzero(vdc->geom, sizeof (struct dk_geom));
7806edcc0754Sachartre 	bzero(vdc->slice, sizeof (vd_slice_t) * V_NUMPAR);
780778fcd0a1Sachartre }
7808