xref: /titanic_52/usr/src/uts/sun4v/io/vdc.c (revision 9642afceed06426c959500df521be1ac1fffcc35)
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 
13001ae08745Sheppo 	int	instance;
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 
13201ae08745Sheppo 	mutex_enter(&vdc->lock);
132178fcd0a1Sachartre 
132278fcd0a1Sachartre 	status = vdc_mark_opened(vdc, slice, flag, otyp);
132378fcd0a1Sachartre 
132478fcd0a1Sachartre 	if (status != 0) {
132578fcd0a1Sachartre 		mutex_exit(&vdc->lock);
132678fcd0a1Sachartre 		return (status);
132778fcd0a1Sachartre 	}
132878fcd0a1Sachartre 
132978fcd0a1Sachartre 	if (flag & (FNDELAY | FNONBLOCK)) {
133078fcd0a1Sachartre 
133178fcd0a1Sachartre 		/* don't resubmit a validate request if there's already one */
133278fcd0a1Sachartre 		if (vdc->validate_pending > 0) {
133378fcd0a1Sachartre 			mutex_exit(&vdc->lock);
133478fcd0a1Sachartre 			return (0);
133578fcd0a1Sachartre 		}
133678fcd0a1Sachartre 
133778fcd0a1Sachartre 		/* call vdc_validate() asynchronously to avoid blocking */
133878fcd0a1Sachartre 		if (taskq_dispatch(system_taskq, vdc_validate_task,
133978fcd0a1Sachartre 		    (void *)vdc, TQ_NOSLEEP) == NULL) {
134078fcd0a1Sachartre 			vdc_mark_closed(vdc, slice, flag, otyp);
134178fcd0a1Sachartre 			mutex_exit(&vdc->lock);
134278fcd0a1Sachartre 			return (ENXIO);
134378fcd0a1Sachartre 		}
134478fcd0a1Sachartre 
134578fcd0a1Sachartre 		vdc->validate_pending++;
134678fcd0a1Sachartre 		mutex_exit(&vdc->lock);
134778fcd0a1Sachartre 		return (0);
134878fcd0a1Sachartre 	}
134978fcd0a1Sachartre 
13501ae08745Sheppo 	mutex_exit(&vdc->lock);
13511ae08745Sheppo 
135278fcd0a1Sachartre 	vdc_validate(vdc);
135378fcd0a1Sachartre 
135478fcd0a1Sachartre 	mutex_enter(&vdc->lock);
135578fcd0a1Sachartre 
135678fcd0a1Sachartre 	if (vdc->vdisk_label == VD_DISK_LABEL_UNK ||
1357edcc0754Sachartre 	    vdc->slice[slice].nblocks == 0) {
135878fcd0a1Sachartre 		vdc_mark_closed(vdc, slice, flag, otyp);
135978fcd0a1Sachartre 		status = EIO;
136078fcd0a1Sachartre 	}
136178fcd0a1Sachartre 
136278fcd0a1Sachartre 	mutex_exit(&vdc->lock);
136378fcd0a1Sachartre 
136478fcd0a1Sachartre 	return (status);
13651ae08745Sheppo }
13661ae08745Sheppo 
13671ae08745Sheppo static int
13681ae08745Sheppo vdc_close(dev_t dev, int flag, int otyp, cred_t *cred)
13691ae08745Sheppo {
13701ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
13711ae08745Sheppo 
13721ae08745Sheppo 	int	instance;
137378fcd0a1Sachartre 	int	slice;
13742f5224aeSachartre 	int	rv, rval;
13751ae08745Sheppo 	vdc_t	*vdc;
13761ae08745Sheppo 
13770d0c8d4bSnarayan 	instance = VDCUNIT(dev);
13781ae08745Sheppo 
137978fcd0a1Sachartre 	if (otyp >= OTYPCNT)
13801ae08745Sheppo 		return (EINVAL);
13811ae08745Sheppo 
13821ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1383e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
13841ae08745Sheppo 		return (ENXIO);
13851ae08745Sheppo 	}
13861ae08745Sheppo 
13873af08d82Slm66018 	DMSG(vdc, 0, "[%d] flag = %x, otyp = %x\n", instance, flag, otyp);
13881ae08745Sheppo 
138978fcd0a1Sachartre 	slice = VDCPART(dev);
139078fcd0a1Sachartre 
13918259acd8Szk194757 	/*
13928259acd8Szk194757 	 * Attempt to flush the W$ on a close operation. If this is
13938259acd8Szk194757 	 * not a supported IOCTL command or the backing device is read-only
13948259acd8Szk194757 	 * do not fail the close operation.
13958259acd8Szk194757 	 */
13962f5224aeSachartre 	rv = vd_process_ioctl(dev, DKIOCFLUSHWRITECACHE, NULL, FKIOCTL, &rval);
13978259acd8Szk194757 
13988259acd8Szk194757 	if (rv != 0 && rv != ENOTSUP && rv != ENOTTY && rv != EROFS) {
13998259acd8Szk194757 		DMSG(vdc, 0, "[%d] flush failed with error %d on close\n",
14008259acd8Szk194757 		    instance, rv);
14018259acd8Szk194757 		return (EIO);
14028259acd8Szk194757 	}
14038259acd8Szk194757 
14041ae08745Sheppo 	mutex_enter(&vdc->lock);
140578fcd0a1Sachartre 	vdc_mark_closed(vdc, slice, flag, otyp);
14061ae08745Sheppo 	mutex_exit(&vdc->lock);
14071ae08745Sheppo 
14081ae08745Sheppo 	return (0);
14091ae08745Sheppo }
14101ae08745Sheppo 
14111ae08745Sheppo static int
14121ae08745Sheppo vdc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
14131ae08745Sheppo {
14141ae08745Sheppo 	_NOTE(ARGUNUSED(credp))
14151ae08745Sheppo 
14162f5224aeSachartre 	return (vd_process_ioctl(dev, cmd, (caddr_t)arg, mode, rvalp));
14171ae08745Sheppo }
14181ae08745Sheppo 
14191ae08745Sheppo static int
14201ae08745Sheppo vdc_print(dev_t dev, char *str)
14211ae08745Sheppo {
14220d0c8d4bSnarayan 	cmn_err(CE_NOTE, "vdc%d:  %s", VDCUNIT(dev), str);
14231ae08745Sheppo 	return (0);
14241ae08745Sheppo }
14251ae08745Sheppo 
14261ae08745Sheppo static int
14271ae08745Sheppo vdc_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
14281ae08745Sheppo {
1429d10e4ef2Snarayan 	int	rv;
1430d10e4ef2Snarayan 	size_t	nbytes = nblk * DEV_BSIZE;
14310d0c8d4bSnarayan 	int	instance = VDCUNIT(dev);
1432d10e4ef2Snarayan 	vdc_t	*vdc = NULL;
14331ae08745Sheppo 
14341ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1435e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
14361ae08745Sheppo 		return (ENXIO);
14371ae08745Sheppo 	}
14381ae08745Sheppo 
14393af08d82Slm66018 	DMSG(vdc, 2, "[%d] dump %ld bytes at block 0x%lx : addr=0x%p\n",
14403af08d82Slm66018 	    instance, nbytes, blkno, (void *)addr);
14413af08d82Slm66018 	rv = vdc_send_request(vdc, VD_OP_BWRITE, addr, nbytes,
14420d0c8d4bSnarayan 	    VDCPART(dev), blkno, CB_STRATEGY, 0, VIO_write_dir);
14433af08d82Slm66018 	if (rv) {
14443af08d82Slm66018 		DMSG(vdc, 0, "Failed to do a disk dump (err=%d)\n", rv);
14451ae08745Sheppo 		return (rv);
14461ae08745Sheppo 	}
14471ae08745Sheppo 
14483af08d82Slm66018 	if (ddi_in_panic())
14493af08d82Slm66018 		(void) vdc_drain_response(vdc);
14503af08d82Slm66018 
14513af08d82Slm66018 	DMSG(vdc, 0, "[%d] End\n", instance);
14523af08d82Slm66018 
14533af08d82Slm66018 	return (0);
14543af08d82Slm66018 }
14553af08d82Slm66018 
14561ae08745Sheppo /* -------------------------------------------------------------------------- */
14571ae08745Sheppo 
14581ae08745Sheppo /*
14591ae08745Sheppo  * Disk access routines
14601ae08745Sheppo  *
14611ae08745Sheppo  */
14621ae08745Sheppo 
14631ae08745Sheppo /*
14641ae08745Sheppo  * vdc_strategy()
14651ae08745Sheppo  *
14661ae08745Sheppo  * Return Value:
14671ae08745Sheppo  *	0:	As per strategy(9E), the strategy() function must return 0
14681ae08745Sheppo  *		[ bioerror(9f) sets b_flags to the proper error code ]
14691ae08745Sheppo  */
14701ae08745Sheppo static int
14711ae08745Sheppo vdc_strategy(struct buf *buf)
14721ae08745Sheppo {
14731ae08745Sheppo 	int	rv = -1;
14741ae08745Sheppo 	vdc_t	*vdc = NULL;
14750d0c8d4bSnarayan 	int	instance = VDCUNIT(buf->b_edev);
14761ae08745Sheppo 	int	op = (buf->b_flags & B_READ) ? VD_OP_BREAD : VD_OP_BWRITE;
147787a7269eSachartre 	int	slice;
14781ae08745Sheppo 
14791ae08745Sheppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1480e1ebb9ecSlm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
14811ae08745Sheppo 		bioerror(buf, ENXIO);
14821ae08745Sheppo 		biodone(buf);
14831ae08745Sheppo 		return (0);
14841ae08745Sheppo 	}
14851ae08745Sheppo 
14863af08d82Slm66018 	DMSG(vdc, 2, "[%d] %s %ld bytes at block %llx : b_addr=0x%p\n",
14873af08d82Slm66018 	    instance, (buf->b_flags & B_READ) ? "Read" : "Write",
14883af08d82Slm66018 	    buf->b_bcount, buf->b_lblkno, (void *)buf->b_un.b_addr);
1489d10e4ef2Snarayan 
14901ae08745Sheppo 	bp_mapin(buf);
14911ae08745Sheppo 
149287a7269eSachartre 	if ((long)buf->b_private == VD_SLICE_NONE) {
149387a7269eSachartre 		/* I/O using an absolute disk offset */
149487a7269eSachartre 		slice = VD_SLICE_NONE;
149587a7269eSachartre 	} else {
149687a7269eSachartre 		slice = VDCPART(buf->b_edev);
149787a7269eSachartre 	}
149887a7269eSachartre 
14993af08d82Slm66018 	rv = vdc_send_request(vdc, op, (caddr_t)buf->b_un.b_addr,
150087a7269eSachartre 	    buf->b_bcount, slice, buf->b_lblkno,
15013af08d82Slm66018 	    CB_STRATEGY, buf, (op == VD_OP_BREAD) ? VIO_read_dir :
15023af08d82Slm66018 	    VIO_write_dir);
15033af08d82Slm66018 
1504d10e4ef2Snarayan 	/*
1505d10e4ef2Snarayan 	 * If the request was successfully sent, the strategy call returns and
1506d10e4ef2Snarayan 	 * the ACK handler calls the bioxxx functions when the vDisk server is
1507366a92acSlm66018 	 * done otherwise we handle the error here.
1508d10e4ef2Snarayan 	 */
1509d10e4ef2Snarayan 	if (rv) {
15103af08d82Slm66018 		DMSG(vdc, 0, "Failed to read/write (err=%d)\n", rv);
15111ae08745Sheppo 		bioerror(buf, rv);
15121ae08745Sheppo 		biodone(buf);
1513d10e4ef2Snarayan 	}
1514d10e4ef2Snarayan 
15151ae08745Sheppo 	return (0);
15161ae08745Sheppo }
15171ae08745Sheppo 
15180d0c8d4bSnarayan /*
15190d0c8d4bSnarayan  * Function:
15200d0c8d4bSnarayan  *	vdc_min
15210d0c8d4bSnarayan  *
15220d0c8d4bSnarayan  * Description:
15230d0c8d4bSnarayan  *	Routine to limit the size of a data transfer. Used in
15240d0c8d4bSnarayan  *	conjunction with physio(9F).
15250d0c8d4bSnarayan  *
15260d0c8d4bSnarayan  * Arguments:
15270d0c8d4bSnarayan  *	bp - pointer to the indicated buf(9S) struct.
15280d0c8d4bSnarayan  *
15290d0c8d4bSnarayan  */
15300d0c8d4bSnarayan static void
15310d0c8d4bSnarayan vdc_min(struct buf *bufp)
15320d0c8d4bSnarayan {
15330d0c8d4bSnarayan 	vdc_t	*vdc = NULL;
15340d0c8d4bSnarayan 	int	instance = VDCUNIT(bufp->b_edev);
15350d0c8d4bSnarayan 
15360d0c8d4bSnarayan 	vdc = ddi_get_soft_state(vdc_state, instance);
15370d0c8d4bSnarayan 	VERIFY(vdc != NULL);
15380d0c8d4bSnarayan 
15390d0c8d4bSnarayan 	if (bufp->b_bcount > (vdc->max_xfer_sz * vdc->block_size)) {
15400d0c8d4bSnarayan 		bufp->b_bcount = vdc->max_xfer_sz * vdc->block_size;
15410d0c8d4bSnarayan 	}
15420d0c8d4bSnarayan }
15431ae08745Sheppo 
15441ae08745Sheppo static int
15451ae08745Sheppo vdc_read(dev_t dev, struct uio *uio, cred_t *cred)
15461ae08745Sheppo {
15471ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
15481ae08745Sheppo 
15490d0c8d4bSnarayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
15500d0c8d4bSnarayan 	return (physio(vdc_strategy, NULL, dev, B_READ, vdc_min, uio));
15511ae08745Sheppo }
15521ae08745Sheppo 
15531ae08745Sheppo static int
15541ae08745Sheppo vdc_write(dev_t dev, struct uio *uio, cred_t *cred)
15551ae08745Sheppo {
15561ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
15571ae08745Sheppo 
15580d0c8d4bSnarayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
15590d0c8d4bSnarayan 	return (physio(vdc_strategy, NULL, dev, B_WRITE, vdc_min, uio));
15601ae08745Sheppo }
15611ae08745Sheppo 
15621ae08745Sheppo static int
15631ae08745Sheppo vdc_aread(dev_t dev, struct aio_req *aio, cred_t *cred)
15641ae08745Sheppo {
15651ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
15661ae08745Sheppo 
15670d0c8d4bSnarayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
15680d0c8d4bSnarayan 	return (aphysio(vdc_strategy, anocancel, dev, B_READ, vdc_min, aio));
15691ae08745Sheppo }
15701ae08745Sheppo 
15711ae08745Sheppo static int
15721ae08745Sheppo vdc_awrite(dev_t dev, struct aio_req *aio, cred_t *cred)
15731ae08745Sheppo {
15741ae08745Sheppo 	_NOTE(ARGUNUSED(cred))
15751ae08745Sheppo 
15760d0c8d4bSnarayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
15770d0c8d4bSnarayan 	return (aphysio(vdc_strategy, anocancel, dev, B_WRITE, vdc_min, aio));
15781ae08745Sheppo }
15791ae08745Sheppo 
15801ae08745Sheppo 
15811ae08745Sheppo /* -------------------------------------------------------------------------- */
15821ae08745Sheppo 
15831ae08745Sheppo /*
15841ae08745Sheppo  * Handshake support
15851ae08745Sheppo  */
15861ae08745Sheppo 
15871ae08745Sheppo 
15880a55fbb7Slm66018 /*
15890a55fbb7Slm66018  * Function:
15900a55fbb7Slm66018  *	vdc_init_ver_negotiation()
15910a55fbb7Slm66018  *
15920a55fbb7Slm66018  * Description:
15930a55fbb7Slm66018  *
15940a55fbb7Slm66018  * Arguments:
15950a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
15960a55fbb7Slm66018  *
15970a55fbb7Slm66018  * Return Code:
15980a55fbb7Slm66018  *	0	- Success
15990a55fbb7Slm66018  */
16001ae08745Sheppo static int
16010a55fbb7Slm66018 vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver)
16021ae08745Sheppo {
16031ae08745Sheppo 	vio_ver_msg_t	pkt;
16041ae08745Sheppo 	size_t		msglen = sizeof (pkt);
16051ae08745Sheppo 	int		status = -1;
16061ae08745Sheppo 
16071ae08745Sheppo 	ASSERT(vdc != NULL);
16081ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
16091ae08745Sheppo 
16103af08d82Slm66018 	DMSG(vdc, 0, "[%d] Entered.\n", vdc->instance);
1611e1ebb9ecSlm66018 
16121ae08745Sheppo 	/*
16131ae08745Sheppo 	 * set the Session ID to a unique value
16141ae08745Sheppo 	 * (the lower 32 bits of the clock tick)
16151ae08745Sheppo 	 */
16161ae08745Sheppo 	vdc->session_id = ((uint32_t)gettick() & 0xffffffff);
16173af08d82Slm66018 	DMSG(vdc, 0, "[%d] Set SID to 0x%lx\n", vdc->instance, vdc->session_id);
16181ae08745Sheppo 
16191ae08745Sheppo 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
16201ae08745Sheppo 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
16211ae08745Sheppo 	pkt.tag.vio_subtype_env = VIO_VER_INFO;
16221ae08745Sheppo 	pkt.tag.vio_sid = vdc->session_id;
16231ae08745Sheppo 	pkt.dev_class = VDEV_DISK;
16240a55fbb7Slm66018 	pkt.ver_major = ver.major;
16250a55fbb7Slm66018 	pkt.ver_minor = ver.minor;
16261ae08745Sheppo 
16270a55fbb7Slm66018 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
16283af08d82Slm66018 	DMSG(vdc, 0, "[%d] Ver info sent (status = %d)\n",
16293af08d82Slm66018 	    vdc->instance, status);
16301ae08745Sheppo 	if ((status != 0) || (msglen != sizeof (vio_ver_msg_t))) {
16313af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to send Ver negotiation info: "
163287a7269eSachartre 		    "id(%lx) rv(%d) size(%ld)", vdc->instance, vdc->ldc_handle,
16331ae08745Sheppo 		    status, msglen);
16341ae08745Sheppo 		if (msglen != sizeof (vio_ver_msg_t))
16351ae08745Sheppo 			status = ENOMSG;
16361ae08745Sheppo 	}
16371ae08745Sheppo 
16381ae08745Sheppo 	return (status);
16391ae08745Sheppo }
16401ae08745Sheppo 
16410a55fbb7Slm66018 /*
16420a55fbb7Slm66018  * Function:
16433af08d82Slm66018  *	vdc_ver_negotiation()
16443af08d82Slm66018  *
16453af08d82Slm66018  * Description:
16463af08d82Slm66018  *
16473af08d82Slm66018  * Arguments:
16483af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
16493af08d82Slm66018  *
16503af08d82Slm66018  * Return Code:
16513af08d82Slm66018  *	0	- Success
16523af08d82Slm66018  */
16533af08d82Slm66018 static int
16543af08d82Slm66018 vdc_ver_negotiation(vdc_t *vdcp)
16553af08d82Slm66018 {
16563af08d82Slm66018 	vio_msg_t vio_msg;
16573af08d82Slm66018 	int status;
16583af08d82Slm66018 
16593af08d82Slm66018 	if (status = vdc_init_ver_negotiation(vdcp, vdc_version[0]))
16603af08d82Slm66018 		return (status);
16613af08d82Slm66018 
16623af08d82Slm66018 	/* release lock and wait for response */
16633af08d82Slm66018 	mutex_exit(&vdcp->lock);
16643af08d82Slm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
16653af08d82Slm66018 	mutex_enter(&vdcp->lock);
16663af08d82Slm66018 	if (status) {
16673af08d82Slm66018 		DMSG(vdcp, 0,
16683af08d82Slm66018 		    "[%d] Failed waiting for Ver negotiation response, rv(%d)",
16693af08d82Slm66018 		    vdcp->instance, status);
16703af08d82Slm66018 		return (status);
16713af08d82Slm66018 	}
16723af08d82Slm66018 
16733af08d82Slm66018 	/* check type and sub_type ... */
16743af08d82Slm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
16753af08d82Slm66018 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
16763af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Invalid ver negotiation response\n",
16773af08d82Slm66018 		    vdcp->instance);
16783af08d82Slm66018 		return (EPROTO);
16793af08d82Slm66018 	}
16803af08d82Slm66018 
16813af08d82Slm66018 	return (vdc_handle_ver_msg(vdcp, (vio_ver_msg_t *)&vio_msg));
16823af08d82Slm66018 }
16833af08d82Slm66018 
16843af08d82Slm66018 /*
16853af08d82Slm66018  * Function:
16860a55fbb7Slm66018  *	vdc_init_attr_negotiation()
16870a55fbb7Slm66018  *
16880a55fbb7Slm66018  * Description:
16890a55fbb7Slm66018  *
16900a55fbb7Slm66018  * Arguments:
16910a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
16920a55fbb7Slm66018  *
16930a55fbb7Slm66018  * Return Code:
16940a55fbb7Slm66018  *	0	- Success
16950a55fbb7Slm66018  */
16961ae08745Sheppo static int
16971ae08745Sheppo vdc_init_attr_negotiation(vdc_t *vdc)
16981ae08745Sheppo {
16991ae08745Sheppo 	vd_attr_msg_t	pkt;
17001ae08745Sheppo 	size_t		msglen = sizeof (pkt);
17011ae08745Sheppo 	int		status;
17021ae08745Sheppo 
17031ae08745Sheppo 	ASSERT(vdc != NULL);
17041ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
17051ae08745Sheppo 
17063af08d82Slm66018 	DMSG(vdc, 0, "[%d] entered\n", vdc->instance);
17071ae08745Sheppo 
17081ae08745Sheppo 	/* fill in tag */
17091ae08745Sheppo 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
17101ae08745Sheppo 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
17111ae08745Sheppo 	pkt.tag.vio_subtype_env = VIO_ATTR_INFO;
17121ae08745Sheppo 	pkt.tag.vio_sid = vdc->session_id;
17131ae08745Sheppo 	/* fill in payload */
17141ae08745Sheppo 	pkt.max_xfer_sz = vdc->max_xfer_sz;
17151ae08745Sheppo 	pkt.vdisk_block_size = vdc->block_size;
1716f0ca1d9aSsb155480 	pkt.xfer_mode = VIO_DRING_MODE_V1_0;
17171ae08745Sheppo 	pkt.operations = 0;	/* server will set bits of valid operations */
17181ae08745Sheppo 	pkt.vdisk_type = 0;	/* server will set to valid device type */
171917cadca8Slm66018 	pkt.vdisk_media = 0;	/* server will set to valid media type */
17201ae08745Sheppo 	pkt.vdisk_size = 0;	/* server will set to valid size */
17211ae08745Sheppo 
17220a55fbb7Slm66018 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
17233af08d82Slm66018 	DMSG(vdc, 0, "Attr info sent (status = %d)\n", status);
17241ae08745Sheppo 
17251ae08745Sheppo 	if ((status != 0) || (msglen != sizeof (vio_ver_msg_t))) {
17263af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to send Attr negotiation info: "
172787a7269eSachartre 		    "id(%lx) rv(%d) size(%ld)", vdc->instance, vdc->ldc_handle,
17281ae08745Sheppo 		    status, msglen);
17291ae08745Sheppo 		if (msglen != sizeof (vio_ver_msg_t))
17301ae08745Sheppo 			status = ENOMSG;
17311ae08745Sheppo 	}
17321ae08745Sheppo 
17331ae08745Sheppo 	return (status);
17341ae08745Sheppo }
17351ae08745Sheppo 
17360a55fbb7Slm66018 /*
17370a55fbb7Slm66018  * Function:
17383af08d82Slm66018  *	vdc_attr_negotiation()
17393af08d82Slm66018  *
17403af08d82Slm66018  * Description:
17413af08d82Slm66018  *
17423af08d82Slm66018  * Arguments:
17433af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
17443af08d82Slm66018  *
17453af08d82Slm66018  * Return Code:
17463af08d82Slm66018  *	0	- Success
17473af08d82Slm66018  */
17483af08d82Slm66018 static int
17493af08d82Slm66018 vdc_attr_negotiation(vdc_t *vdcp)
17503af08d82Slm66018 {
17513af08d82Slm66018 	int status;
17523af08d82Slm66018 	vio_msg_t vio_msg;
17533af08d82Slm66018 
17543af08d82Slm66018 	if (status = vdc_init_attr_negotiation(vdcp))
17553af08d82Slm66018 		return (status);
17563af08d82Slm66018 
17573af08d82Slm66018 	/* release lock and wait for response */
17583af08d82Slm66018 	mutex_exit(&vdcp->lock);
17593af08d82Slm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
17603af08d82Slm66018 	mutex_enter(&vdcp->lock);
17613af08d82Slm66018 	if (status) {
17623af08d82Slm66018 		DMSG(vdcp, 0,
17633af08d82Slm66018 		    "[%d] Failed waiting for Attr negotiation response, rv(%d)",
17643af08d82Slm66018 		    vdcp->instance, status);
17653af08d82Slm66018 		return (status);
17663af08d82Slm66018 	}
17673af08d82Slm66018 
17683af08d82Slm66018 	/* check type and sub_type ... */
17693af08d82Slm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
17703af08d82Slm66018 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
17713af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Invalid attr negotiation response\n",
17723af08d82Slm66018 		    vdcp->instance);
17733af08d82Slm66018 		return (EPROTO);
17743af08d82Slm66018 	}
17753af08d82Slm66018 
17763af08d82Slm66018 	return (vdc_handle_attr_msg(vdcp, (vd_attr_msg_t *)&vio_msg));
17773af08d82Slm66018 }
17783af08d82Slm66018 
17793af08d82Slm66018 
17803af08d82Slm66018 /*
17813af08d82Slm66018  * Function:
17820a55fbb7Slm66018  *	vdc_init_dring_negotiate()
17830a55fbb7Slm66018  *
17840a55fbb7Slm66018  * Description:
17850a55fbb7Slm66018  *
17860a55fbb7Slm66018  * Arguments:
17870a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
17880a55fbb7Slm66018  *
17890a55fbb7Slm66018  * Return Code:
17900a55fbb7Slm66018  *	0	- Success
17910a55fbb7Slm66018  */
17921ae08745Sheppo static int
17931ae08745Sheppo vdc_init_dring_negotiate(vdc_t *vdc)
17941ae08745Sheppo {
17951ae08745Sheppo 	vio_dring_reg_msg_t	pkt;
17961ae08745Sheppo 	size_t			msglen = sizeof (pkt);
17971ae08745Sheppo 	int			status = -1;
17983af08d82Slm66018 	int			retry;
17993af08d82Slm66018 	int			nretries = 10;
18001ae08745Sheppo 
18011ae08745Sheppo 	ASSERT(vdc != NULL);
18021ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
18031ae08745Sheppo 
18043af08d82Slm66018 	for (retry = 0; retry < nretries; retry++) {
18051ae08745Sheppo 		status = vdc_init_descriptor_ring(vdc);
18063af08d82Slm66018 		if (status != EAGAIN)
18073af08d82Slm66018 			break;
18083af08d82Slm66018 		drv_usecwait(vdc_min_timeout_ldc);
18093af08d82Slm66018 	}
18103af08d82Slm66018 
18111ae08745Sheppo 	if (status != 0) {
18123af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to init DRing (status = %d)\n",
18131ae08745Sheppo 		    vdc->instance, status);
18141ae08745Sheppo 		return (status);
18151ae08745Sheppo 	}
18163af08d82Slm66018 
18173af08d82Slm66018 	DMSG(vdc, 0, "[%d] Init of descriptor ring completed (status = %d)\n",
1818e1ebb9ecSlm66018 	    vdc->instance, status);
18191ae08745Sheppo 
18201ae08745Sheppo 	/* fill in tag */
18211ae08745Sheppo 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
18221ae08745Sheppo 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
18231ae08745Sheppo 	pkt.tag.vio_subtype_env = VIO_DRING_REG;
18241ae08745Sheppo 	pkt.tag.vio_sid = vdc->session_id;
18251ae08745Sheppo 	/* fill in payload */
18261ae08745Sheppo 	pkt.dring_ident = 0;
1827e1ebb9ecSlm66018 	pkt.num_descriptors = vdc->dring_len;
1828e1ebb9ecSlm66018 	pkt.descriptor_size = vdc->dring_entry_size;
18291ae08745Sheppo 	pkt.options = (VIO_TX_DRING | VIO_RX_DRING);
18301ae08745Sheppo 	pkt.ncookies = vdc->dring_cookie_count;
18311ae08745Sheppo 	pkt.cookie[0] = vdc->dring_cookie[0];	/* for now just one cookie */
18321ae08745Sheppo 
18330a55fbb7Slm66018 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
18341ae08745Sheppo 	if (status != 0) {
18353af08d82Slm66018 		DMSG(vdc, 0, "[%d] Failed to register DRing (err = %d)",
1836e1ebb9ecSlm66018 		    vdc->instance, status);
18371ae08745Sheppo 	}
18381ae08745Sheppo 
18391ae08745Sheppo 	return (status);
18401ae08745Sheppo }
18411ae08745Sheppo 
18421ae08745Sheppo 
18433af08d82Slm66018 /*
18443af08d82Slm66018  * Function:
18453af08d82Slm66018  *	vdc_dring_negotiation()
18463af08d82Slm66018  *
18473af08d82Slm66018  * Description:
18483af08d82Slm66018  *
18493af08d82Slm66018  * Arguments:
18503af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
18513af08d82Slm66018  *
18523af08d82Slm66018  * Return Code:
18533af08d82Slm66018  *	0	- Success
18543af08d82Slm66018  */
18553af08d82Slm66018 static int
18563af08d82Slm66018 vdc_dring_negotiation(vdc_t *vdcp)
18573af08d82Slm66018 {
18583af08d82Slm66018 	int status;
18593af08d82Slm66018 	vio_msg_t vio_msg;
18603af08d82Slm66018 
18613af08d82Slm66018 	if (status = vdc_init_dring_negotiate(vdcp))
18623af08d82Slm66018 		return (status);
18633af08d82Slm66018 
18643af08d82Slm66018 	/* release lock and wait for response */
18653af08d82Slm66018 	mutex_exit(&vdcp->lock);
18663af08d82Slm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
18673af08d82Slm66018 	mutex_enter(&vdcp->lock);
18683af08d82Slm66018 	if (status) {
18693af08d82Slm66018 		DMSG(vdcp, 0,
18703af08d82Slm66018 		    "[%d] Failed waiting for Dring negotiation response,"
18713af08d82Slm66018 		    " rv(%d)", vdcp->instance, status);
18723af08d82Slm66018 		return (status);
18733af08d82Slm66018 	}
18743af08d82Slm66018 
18753af08d82Slm66018 	/* check type and sub_type ... */
18763af08d82Slm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
18773af08d82Slm66018 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
18783af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Invalid Dring negotiation response\n",
18793af08d82Slm66018 		    vdcp->instance);
18803af08d82Slm66018 		return (EPROTO);
18813af08d82Slm66018 	}
18823af08d82Slm66018 
18833af08d82Slm66018 	return (vdc_handle_dring_reg_msg(vdcp,
18843af08d82Slm66018 	    (vio_dring_reg_msg_t *)&vio_msg));
18853af08d82Slm66018 }
18863af08d82Slm66018 
18873af08d82Slm66018 
18883af08d82Slm66018 /*
18893af08d82Slm66018  * Function:
18903af08d82Slm66018  *	vdc_send_rdx()
18913af08d82Slm66018  *
18923af08d82Slm66018  * Description:
18933af08d82Slm66018  *
18943af08d82Slm66018  * Arguments:
18953af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
18963af08d82Slm66018  *
18973af08d82Slm66018  * Return Code:
18983af08d82Slm66018  *	0	- Success
18993af08d82Slm66018  */
19003af08d82Slm66018 static int
19013af08d82Slm66018 vdc_send_rdx(vdc_t *vdcp)
19023af08d82Slm66018 {
19033af08d82Slm66018 	vio_msg_t	msg;
19043af08d82Slm66018 	size_t		msglen = sizeof (vio_msg_t);
19053af08d82Slm66018 	int		status;
19063af08d82Slm66018 
19073af08d82Slm66018 	/*
19083af08d82Slm66018 	 * Send an RDX message to vds to indicate we are ready
19093af08d82Slm66018 	 * to send data
19103af08d82Slm66018 	 */
19113af08d82Slm66018 	msg.tag.vio_msgtype = VIO_TYPE_CTRL;
19123af08d82Slm66018 	msg.tag.vio_subtype = VIO_SUBTYPE_INFO;
19133af08d82Slm66018 	msg.tag.vio_subtype_env = VIO_RDX;
19143af08d82Slm66018 	msg.tag.vio_sid = vdcp->session_id;
19153af08d82Slm66018 	status = vdc_send(vdcp, (caddr_t)&msg, &msglen);
19163af08d82Slm66018 	if (status != 0) {
19173af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Failed to send RDX message (%d)",
19183af08d82Slm66018 		    vdcp->instance, status);
19193af08d82Slm66018 	}
19203af08d82Slm66018 
19213af08d82Slm66018 	return (status);
19223af08d82Slm66018 }
19233af08d82Slm66018 
19243af08d82Slm66018 /*
19253af08d82Slm66018  * Function:
19263af08d82Slm66018  *	vdc_handle_rdx()
19273af08d82Slm66018  *
19283af08d82Slm66018  * Description:
19293af08d82Slm66018  *
19303af08d82Slm66018  * Arguments:
19313af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
19323af08d82Slm66018  *	msgp	- received msg
19333af08d82Slm66018  *
19343af08d82Slm66018  * Return Code:
19353af08d82Slm66018  *	0	- Success
19363af08d82Slm66018  */
19373af08d82Slm66018 static int
19383af08d82Slm66018 vdc_handle_rdx(vdc_t *vdcp, vio_rdx_msg_t *msgp)
19393af08d82Slm66018 {
19403af08d82Slm66018 	_NOTE(ARGUNUSED(vdcp))
19413af08d82Slm66018 	_NOTE(ARGUNUSED(msgp))
19423af08d82Slm66018 
19433af08d82Slm66018 	ASSERT(msgp->tag.vio_msgtype == VIO_TYPE_CTRL);
19443af08d82Slm66018 	ASSERT(msgp->tag.vio_subtype == VIO_SUBTYPE_ACK);
19453af08d82Slm66018 	ASSERT(msgp->tag.vio_subtype_env == VIO_RDX);
19463af08d82Slm66018 
19473af08d82Slm66018 	DMSG(vdcp, 1, "[%d] Got an RDX msg", vdcp->instance);
19483af08d82Slm66018 
19493af08d82Slm66018 	return (0);
19503af08d82Slm66018 }
19513af08d82Slm66018 
19523af08d82Slm66018 /*
19533af08d82Slm66018  * Function:
19543af08d82Slm66018  *	vdc_rdx_exchange()
19553af08d82Slm66018  *
19563af08d82Slm66018  * Description:
19573af08d82Slm66018  *
19583af08d82Slm66018  * Arguments:
19593af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
19603af08d82Slm66018  *
19613af08d82Slm66018  * Return Code:
19623af08d82Slm66018  *	0	- Success
19633af08d82Slm66018  */
19643af08d82Slm66018 static int
19653af08d82Slm66018 vdc_rdx_exchange(vdc_t *vdcp)
19663af08d82Slm66018 {
19673af08d82Slm66018 	int status;
19683af08d82Slm66018 	vio_msg_t vio_msg;
19693af08d82Slm66018 
19703af08d82Slm66018 	if (status = vdc_send_rdx(vdcp))
19713af08d82Slm66018 		return (status);
19723af08d82Slm66018 
19733af08d82Slm66018 	/* release lock and wait for response */
19743af08d82Slm66018 	mutex_exit(&vdcp->lock);
19753af08d82Slm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
19763af08d82Slm66018 	mutex_enter(&vdcp->lock);
19773af08d82Slm66018 	if (status) {
197887a7269eSachartre 		DMSG(vdcp, 0, "[%d] Failed waiting for RDX response, rv(%d)",
197987a7269eSachartre 		    vdcp->instance, status);
19803af08d82Slm66018 		return (status);
19813af08d82Slm66018 	}
19823af08d82Slm66018 
19833af08d82Slm66018 	/* check type and sub_type ... */
19843af08d82Slm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
19853af08d82Slm66018 	    vio_msg.tag.vio_subtype != VIO_SUBTYPE_ACK) {
198687a7269eSachartre 		DMSG(vdcp, 0, "[%d] Invalid RDX response\n", vdcp->instance);
19873af08d82Slm66018 		return (EPROTO);
19883af08d82Slm66018 	}
19893af08d82Slm66018 
19903af08d82Slm66018 	return (vdc_handle_rdx(vdcp, (vio_rdx_msg_t *)&vio_msg));
19913af08d82Slm66018 }
19923af08d82Slm66018 
19933af08d82Slm66018 
19941ae08745Sheppo /* -------------------------------------------------------------------------- */
19951ae08745Sheppo 
19961ae08745Sheppo /*
19971ae08745Sheppo  * LDC helper routines
19981ae08745Sheppo  */
19991ae08745Sheppo 
20003af08d82Slm66018 static int
20013af08d82Slm66018 vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp)
20023af08d82Slm66018 {
20033af08d82Slm66018 	int		status;
20043af08d82Slm66018 	boolean_t	q_has_pkts = B_FALSE;
200517cadca8Slm66018 	uint64_t	delay_time;
20063af08d82Slm66018 	size_t		len;
20073af08d82Slm66018 
20083af08d82Slm66018 	mutex_enter(&vdc->read_lock);
20093af08d82Slm66018 
20103af08d82Slm66018 	if (vdc->read_state == VDC_READ_IDLE)
20113af08d82Slm66018 		vdc->read_state = VDC_READ_WAITING;
20123af08d82Slm66018 
20133af08d82Slm66018 	while (vdc->read_state != VDC_READ_PENDING) {
20143af08d82Slm66018 
20153af08d82Slm66018 		/* detect if the connection has been reset */
20163af08d82Slm66018 		if (vdc->read_state == VDC_READ_RESET) {
20173af08d82Slm66018 			status = ECONNRESET;
20183af08d82Slm66018 			goto done;
20193af08d82Slm66018 		}
20203af08d82Slm66018 
20213af08d82Slm66018 		cv_wait(&vdc->read_cv, &vdc->read_lock);
20223af08d82Slm66018 	}
20233af08d82Slm66018 
20243af08d82Slm66018 	/*
20253af08d82Slm66018 	 * Until we get a blocking ldc read we have to retry
20263af08d82Slm66018 	 * until the entire LDC message has arrived before
20273af08d82Slm66018 	 * ldc_read() will succeed. Note we also bail out if
2028eff7243fSlm66018 	 * the channel is reset or goes away.
20293af08d82Slm66018 	 */
20303af08d82Slm66018 	delay_time = vdc_ldc_read_init_delay;
20313af08d82Slm66018 loop:
20323af08d82Slm66018 	len = *nbytesp;
20333af08d82Slm66018 	status = ldc_read(vdc->ldc_handle, (caddr_t)msgp, &len);
20343af08d82Slm66018 	switch (status) {
20353af08d82Slm66018 	case EAGAIN:
20363af08d82Slm66018 		delay_time *= 2;
20373af08d82Slm66018 		if (delay_time >= vdc_ldc_read_max_delay)
20383af08d82Slm66018 			delay_time = vdc_ldc_read_max_delay;
20393af08d82Slm66018 		delay(delay_time);
20403af08d82Slm66018 		goto loop;
20413af08d82Slm66018 
20423af08d82Slm66018 	case 0:
20433af08d82Slm66018 		if (len == 0) {
204417cadca8Slm66018 			DMSG(vdc, 1, "[%d] ldc_read returned 0 bytes with "
20453af08d82Slm66018 			    "no error!\n", vdc->instance);
20463af08d82Slm66018 			goto loop;
20473af08d82Slm66018 		}
20483af08d82Slm66018 
20493af08d82Slm66018 		*nbytesp = len;
20503af08d82Slm66018 
20513af08d82Slm66018 		/*
20523af08d82Slm66018 		 * If there are pending messages, leave the
20533af08d82Slm66018 		 * read state as pending. Otherwise, set the state
20543af08d82Slm66018 		 * back to idle.
20553af08d82Slm66018 		 */
20563af08d82Slm66018 		status = ldc_chkq(vdc->ldc_handle, &q_has_pkts);
20573af08d82Slm66018 		if (status == 0 && !q_has_pkts)
20583af08d82Slm66018 			vdc->read_state = VDC_READ_IDLE;
20593af08d82Slm66018 
20603af08d82Slm66018 		break;
20613af08d82Slm66018 	default:
20623af08d82Slm66018 		DMSG(vdc, 0, "ldc_read returned %d\n", status);
20633af08d82Slm66018 		break;
20643af08d82Slm66018 	}
20653af08d82Slm66018 
20663af08d82Slm66018 done:
20673af08d82Slm66018 	mutex_exit(&vdc->read_lock);
20683af08d82Slm66018 
20693af08d82Slm66018 	return (status);
20703af08d82Slm66018 }
20713af08d82Slm66018 
20723af08d82Slm66018 
20733af08d82Slm66018 
20743af08d82Slm66018 #ifdef DEBUG
20753af08d82Slm66018 void
20763af08d82Slm66018 vdc_decode_tag(vdc_t *vdcp, vio_msg_t *msg)
20773af08d82Slm66018 {
20783af08d82Slm66018 	char *ms, *ss, *ses;
20793af08d82Slm66018 	switch (msg->tag.vio_msgtype) {
20803af08d82Slm66018 #define	Q(_s)	case _s : ms = #_s; break;
20813af08d82Slm66018 	Q(VIO_TYPE_CTRL)
20823af08d82Slm66018 	Q(VIO_TYPE_DATA)
20833af08d82Slm66018 	Q(VIO_TYPE_ERR)
20843af08d82Slm66018 #undef Q
20853af08d82Slm66018 	default: ms = "unknown"; break;
20863af08d82Slm66018 	}
20873af08d82Slm66018 
20883af08d82Slm66018 	switch (msg->tag.vio_subtype) {
20893af08d82Slm66018 #define	Q(_s)	case _s : ss = #_s; break;
20903af08d82Slm66018 	Q(VIO_SUBTYPE_INFO)
20913af08d82Slm66018 	Q(VIO_SUBTYPE_ACK)
20923af08d82Slm66018 	Q(VIO_SUBTYPE_NACK)
20933af08d82Slm66018 #undef Q
20943af08d82Slm66018 	default: ss = "unknown"; break;
20953af08d82Slm66018 	}
20963af08d82Slm66018 
20973af08d82Slm66018 	switch (msg->tag.vio_subtype_env) {
20983af08d82Slm66018 #define	Q(_s)	case _s : ses = #_s; break;
20993af08d82Slm66018 	Q(VIO_VER_INFO)
21003af08d82Slm66018 	Q(VIO_ATTR_INFO)
21013af08d82Slm66018 	Q(VIO_DRING_REG)
21023af08d82Slm66018 	Q(VIO_DRING_UNREG)
21033af08d82Slm66018 	Q(VIO_RDX)
21043af08d82Slm66018 	Q(VIO_PKT_DATA)
21053af08d82Slm66018 	Q(VIO_DESC_DATA)
21063af08d82Slm66018 	Q(VIO_DRING_DATA)
21073af08d82Slm66018 #undef Q
21083af08d82Slm66018 	default: ses = "unknown"; break;
21093af08d82Slm66018 	}
21103af08d82Slm66018 
21113af08d82Slm66018 	DMSG(vdcp, 3, "(%x/%x/%x) message : (%s/%s/%s)\n",
21123af08d82Slm66018 	    msg->tag.vio_msgtype, msg->tag.vio_subtype,
21133af08d82Slm66018 	    msg->tag.vio_subtype_env, ms, ss, ses);
21143af08d82Slm66018 }
21153af08d82Slm66018 #endif
21163af08d82Slm66018 
21171ae08745Sheppo /*
21181ae08745Sheppo  * Function:
21191ae08745Sheppo  *	vdc_send()
21201ae08745Sheppo  *
21211ae08745Sheppo  * Description:
21221ae08745Sheppo  *	The function encapsulates the call to write a message using LDC.
21231ae08745Sheppo  *	If LDC indicates that the call failed due to the queue being full,
212417cadca8Slm66018  *	we retry the ldc_write(), otherwise we return the error returned by LDC.
21251ae08745Sheppo  *
21261ae08745Sheppo  * Arguments:
21271ae08745Sheppo  *	ldc_handle	- LDC handle for the channel this instance of vdc uses
21281ae08745Sheppo  *	pkt		- address of LDC message to be sent
21291ae08745Sheppo  *	msglen		- the size of the message being sent. When the function
21301ae08745Sheppo  *			  returns, this contains the number of bytes written.
21311ae08745Sheppo  *
21321ae08745Sheppo  * Return Code:
21331ae08745Sheppo  *	0		- Success.
21341ae08745Sheppo  *	EINVAL		- pkt or msglen were NULL
21351ae08745Sheppo  *	ECONNRESET	- The connection was not up.
21361ae08745Sheppo  *	EWOULDBLOCK	- LDC queue is full
21371ae08745Sheppo  *	xxx		- other error codes returned by ldc_write
21381ae08745Sheppo  */
21391ae08745Sheppo static int
21400a55fbb7Slm66018 vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen)
21411ae08745Sheppo {
21421ae08745Sheppo 	size_t	size = 0;
21431ae08745Sheppo 	int	status = 0;
21443af08d82Slm66018 	clock_t delay_ticks;
21451ae08745Sheppo 
21460a55fbb7Slm66018 	ASSERT(vdc != NULL);
21470a55fbb7Slm66018 	ASSERT(mutex_owned(&vdc->lock));
21481ae08745Sheppo 	ASSERT(msglen != NULL);
21491ae08745Sheppo 	ASSERT(*msglen != 0);
21501ae08745Sheppo 
21513af08d82Slm66018 #ifdef DEBUG
215217cadca8Slm66018 	vdc_decode_tag(vdc, (vio_msg_t *)(uintptr_t)pkt);
21533af08d82Slm66018 #endif
21543af08d82Slm66018 	/*
21553af08d82Slm66018 	 * Wait indefinitely to send if channel
21563af08d82Slm66018 	 * is busy, but bail out if we succeed or
21573af08d82Slm66018 	 * if the channel closes or is reset.
21583af08d82Slm66018 	 */
21593af08d82Slm66018 	delay_ticks = vdc_hz_min_ldc_delay;
21601ae08745Sheppo 	do {
21611ae08745Sheppo 		size = *msglen;
21620a55fbb7Slm66018 		status = ldc_write(vdc->ldc_handle, pkt, &size);
21633af08d82Slm66018 		if (status == EWOULDBLOCK) {
21643af08d82Slm66018 			delay(delay_ticks);
21653af08d82Slm66018 			/* geometric backoff */
21663af08d82Slm66018 			delay_ticks *= 2;
21673af08d82Slm66018 			if (delay_ticks > vdc_hz_max_ldc_delay)
21683af08d82Slm66018 				delay_ticks = vdc_hz_max_ldc_delay;
21693af08d82Slm66018 		}
21703af08d82Slm66018 	} while (status == EWOULDBLOCK);
21711ae08745Sheppo 
21720a55fbb7Slm66018 	/* if LDC had serious issues --- reset vdc state */
21730a55fbb7Slm66018 	if (status == EIO || status == ECONNRESET) {
21743af08d82Slm66018 		/* LDC had serious issues --- reset vdc state */
21753af08d82Slm66018 		mutex_enter(&vdc->read_lock);
21763af08d82Slm66018 		if ((vdc->read_state == VDC_READ_WAITING) ||
21773af08d82Slm66018 		    (vdc->read_state == VDC_READ_RESET))
21783af08d82Slm66018 			cv_signal(&vdc->read_cv);
21793af08d82Slm66018 		vdc->read_state = VDC_READ_RESET;
21803af08d82Slm66018 		mutex_exit(&vdc->read_lock);
21813af08d82Slm66018 
21823af08d82Slm66018 		/* wake up any waiters in the reset thread */
21833af08d82Slm66018 		if (vdc->state == VDC_STATE_INIT_WAITING) {
21843af08d82Slm66018 			DMSG(vdc, 0, "[%d] write reset - "
21853af08d82Slm66018 			    "vdc is resetting ..\n", vdc->instance);
21863af08d82Slm66018 			vdc->state = VDC_STATE_RESETTING;
21873af08d82Slm66018 			cv_signal(&vdc->initwait_cv);
21883af08d82Slm66018 		}
21893af08d82Slm66018 
21903af08d82Slm66018 		return (ECONNRESET);
21910a55fbb7Slm66018 	}
21920a55fbb7Slm66018 
21931ae08745Sheppo 	/* return the last size written */
21941ae08745Sheppo 	*msglen = size;
21951ae08745Sheppo 
21961ae08745Sheppo 	return (status);
21971ae08745Sheppo }
21981ae08745Sheppo 
21991ae08745Sheppo /*
22001ae08745Sheppo  * Function:
2201655fd6a9Sachartre  *	vdc_get_md_node
22021ae08745Sheppo  *
22031ae08745Sheppo  * Description:
2204655fd6a9Sachartre  *	Get the MD, the device node and the port node for the given
2205655fd6a9Sachartre  *	disk instance. The caller is responsible for cleaning up the
2206655fd6a9Sachartre  *	reference to the returned MD (mdpp) by calling md_fini_handle().
22071ae08745Sheppo  *
22081ae08745Sheppo  * Arguments:
22091ae08745Sheppo  *	dip	- dev info pointer for this instance of the device driver.
2210655fd6a9Sachartre  *	mdpp	- the returned MD.
2211655fd6a9Sachartre  *	vd_nodep - the returned device node.
2212655fd6a9Sachartre  *	vd_portp - the returned port node. The returned port node is NULL
2213655fd6a9Sachartre  *		   if no port node is found.
22141ae08745Sheppo  *
22151ae08745Sheppo  * Return Code:
22161ae08745Sheppo  *	0	- Success.
22171ae08745Sheppo  *	ENOENT	- Expected node or property did not exist.
22181ae08745Sheppo  *	ENXIO	- Unexpected error communicating with MD framework
22191ae08745Sheppo  */
22201ae08745Sheppo static int
2221655fd6a9Sachartre vdc_get_md_node(dev_info_t *dip, md_t **mdpp, mde_cookie_t *vd_nodep,
2222655fd6a9Sachartre     mde_cookie_t *vd_portp)
22231ae08745Sheppo {
22241ae08745Sheppo 	int		status = ENOENT;
22251ae08745Sheppo 	char		*node_name = NULL;
22261ae08745Sheppo 	md_t		*mdp = NULL;
22271ae08745Sheppo 	int		num_nodes;
22281ae08745Sheppo 	int		num_vdevs;
2229655fd6a9Sachartre 	int		num_vports;
22301ae08745Sheppo 	mde_cookie_t	rootnode;
22311ae08745Sheppo 	mde_cookie_t	*listp = NULL;
22321ae08745Sheppo 	boolean_t	found_inst = B_FALSE;
22331ae08745Sheppo 	int		listsz;
22341ae08745Sheppo 	int		idx;
22351ae08745Sheppo 	uint64_t	md_inst;
22361ae08745Sheppo 	int		obp_inst;
22371ae08745Sheppo 	int		instance = ddi_get_instance(dip);
22381ae08745Sheppo 
22391ae08745Sheppo 	/*
22401ae08745Sheppo 	 * Get the OBP instance number for comparison with the MD instance
22411ae08745Sheppo 	 *
22421ae08745Sheppo 	 * The "cfg-handle" property of a vdc node in an MD contains the MD's
22431ae08745Sheppo 	 * notion of "instance", or unique identifier, for that node; OBP
22441ae08745Sheppo 	 * stores the value of the "cfg-handle" MD property as the value of
22451ae08745Sheppo 	 * the "reg" property on the node in the device tree it builds from
22461ae08745Sheppo 	 * the MD and passes to Solaris.  Thus, we look up the devinfo node's
22471ae08745Sheppo 	 * "reg" property value to uniquely identify this device instance.
22481ae08745Sheppo 	 * If the "reg" property cannot be found, the device tree state is
22491ae08745Sheppo 	 * presumably so broken that there is no point in continuing.
22501ae08745Sheppo 	 */
22511ae08745Sheppo 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, OBP_REG)) {
22521ae08745Sheppo 		cmn_err(CE_WARN, "'%s' property does not exist", OBP_REG);
22531ae08745Sheppo 		return (ENOENT);
22541ae08745Sheppo 	}
22551ae08745Sheppo 	obp_inst = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
22561ae08745Sheppo 	    OBP_REG, -1);
22573af08d82Slm66018 	DMSGX(1, "[%d] OBP inst=%d\n", instance, obp_inst);
22581ae08745Sheppo 
22591ae08745Sheppo 	/*
2260655fd6a9Sachartre 	 * We now walk the MD nodes to find the node for this vdisk.
22611ae08745Sheppo 	 */
22621ae08745Sheppo 	if ((mdp = md_get_handle()) == NULL) {
22631ae08745Sheppo 		cmn_err(CE_WARN, "unable to init machine description");
22641ae08745Sheppo 		return (ENXIO);
22651ae08745Sheppo 	}
22661ae08745Sheppo 
22671ae08745Sheppo 	num_nodes = md_node_count(mdp);
22681ae08745Sheppo 	ASSERT(num_nodes > 0);
22691ae08745Sheppo 
22701ae08745Sheppo 	listsz = num_nodes * sizeof (mde_cookie_t);
22711ae08745Sheppo 
22721ae08745Sheppo 	/* allocate memory for nodes */
22731ae08745Sheppo 	listp = kmem_zalloc(listsz, KM_SLEEP);
22741ae08745Sheppo 
22751ae08745Sheppo 	rootnode = md_root_node(mdp);
22761ae08745Sheppo 	ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE);
22771ae08745Sheppo 
22781ae08745Sheppo 	/*
22791ae08745Sheppo 	 * Search for all the virtual devices, we will then check to see which
22801ae08745Sheppo 	 * ones are disk nodes.
22811ae08745Sheppo 	 */
22821ae08745Sheppo 	num_vdevs = md_scan_dag(mdp, rootnode,
22831ae08745Sheppo 	    md_find_name(mdp, VDC_MD_VDEV_NAME),
22841ae08745Sheppo 	    md_find_name(mdp, "fwd"), listp);
22851ae08745Sheppo 
22861ae08745Sheppo 	if (num_vdevs <= 0) {
22871ae08745Sheppo 		cmn_err(CE_NOTE, "No '%s' node found", VDC_MD_VDEV_NAME);
22881ae08745Sheppo 		status = ENOENT;
22891ae08745Sheppo 		goto done;
22901ae08745Sheppo 	}
22911ae08745Sheppo 
22923af08d82Slm66018 	DMSGX(1, "[%d] num_vdevs=%d\n", instance, num_vdevs);
22931ae08745Sheppo 	for (idx = 0; idx < num_vdevs; idx++) {
22941ae08745Sheppo 		status = md_get_prop_str(mdp, listp[idx], "name", &node_name);
22951ae08745Sheppo 		if ((status != 0) || (node_name == NULL)) {
22961ae08745Sheppo 			cmn_err(CE_NOTE, "Unable to get name of node type '%s'"
22971ae08745Sheppo 			    ": err %d", VDC_MD_VDEV_NAME, status);
22981ae08745Sheppo 			continue;
22991ae08745Sheppo 		}
23001ae08745Sheppo 
23013af08d82Slm66018 		DMSGX(1, "[%d] Found node '%s'\n", instance, node_name);
23021ae08745Sheppo 		if (strcmp(VDC_MD_DISK_NAME, node_name) == 0) {
23031ae08745Sheppo 			status = md_get_prop_val(mdp, listp[idx],
23041ae08745Sheppo 			    VDC_MD_CFG_HDL, &md_inst);
23053af08d82Slm66018 			DMSGX(1, "[%d] vdc inst in MD=%lx\n",
23063af08d82Slm66018 			    instance, md_inst);
23071ae08745Sheppo 			if ((status == 0) && (md_inst == obp_inst)) {
23081ae08745Sheppo 				found_inst = B_TRUE;
23091ae08745Sheppo 				break;
23101ae08745Sheppo 			}
23111ae08745Sheppo 		}
23121ae08745Sheppo 	}
23131ae08745Sheppo 
23140a55fbb7Slm66018 	if (!found_inst) {
23153af08d82Slm66018 		DMSGX(0, "Unable to find correct '%s' node", VDC_MD_DISK_NAME);
23161ae08745Sheppo 		status = ENOENT;
23171ae08745Sheppo 		goto done;
23181ae08745Sheppo 	}
23193af08d82Slm66018 	DMSGX(0, "[%d] MD inst=%lx\n", instance, md_inst);
23201ae08745Sheppo 
2321655fd6a9Sachartre 	*vd_nodep = listp[idx];
2322655fd6a9Sachartre 	*mdpp = mdp;
2323655fd6a9Sachartre 
2324655fd6a9Sachartre 	num_vports = md_scan_dag(mdp, *vd_nodep,
2325655fd6a9Sachartre 	    md_find_name(mdp, VDC_MD_PORT_NAME),
2326655fd6a9Sachartre 	    md_find_name(mdp, "fwd"), listp);
2327655fd6a9Sachartre 
2328655fd6a9Sachartre 	if (num_vports != 1) {
2329655fd6a9Sachartre 		DMSGX(0, "Expected 1 '%s' node for '%s' port, found %d\n",
2330655fd6a9Sachartre 		    VDC_MD_PORT_NAME, VDC_MD_VDEV_NAME, num_vports);
2331655fd6a9Sachartre 	}
2332655fd6a9Sachartre 
2333655fd6a9Sachartre 	*vd_portp = (num_vports == 0)? NULL: listp[0];
2334655fd6a9Sachartre 
2335655fd6a9Sachartre done:
2336655fd6a9Sachartre 	kmem_free(listp, listsz);
2337655fd6a9Sachartre 	return (status);
2338655fd6a9Sachartre }
2339655fd6a9Sachartre 
2340655fd6a9Sachartre /*
2341655fd6a9Sachartre  * Function:
2342655fd6a9Sachartre  *	vdc_get_ldc_id()
2343655fd6a9Sachartre  *
2344655fd6a9Sachartre  * Description:
2345655fd6a9Sachartre  *	This function gets the 'ldc-id' for this particular instance of vdc.
2346655fd6a9Sachartre  *	The id returned is the guest domain channel endpoint LDC uses for
2347655fd6a9Sachartre  *	communication with vds.
2348655fd6a9Sachartre  *
2349655fd6a9Sachartre  * Arguments:
2350655fd6a9Sachartre  *	mdp	- pointer to the machine description.
2351655fd6a9Sachartre  *	vd_node	- the vdisk element from the MD.
2352655fd6a9Sachartre  *	ldc_id	- pointer to variable used to return the 'ldc-id' found.
2353655fd6a9Sachartre  *
2354655fd6a9Sachartre  * Return Code:
2355655fd6a9Sachartre  *	0	- Success.
2356655fd6a9Sachartre  *	ENOENT	- Expected node or property did not exist.
2357655fd6a9Sachartre  */
2358655fd6a9Sachartre static int
2359655fd6a9Sachartre vdc_get_ldc_id(md_t *mdp, mde_cookie_t vd_node, uint64_t *ldc_id)
2360655fd6a9Sachartre {
2361655fd6a9Sachartre 	mde_cookie_t	*chanp = NULL;
2362655fd6a9Sachartre 	int		listsz;
2363655fd6a9Sachartre 	int		num_chans;
2364655fd6a9Sachartre 	int		num_nodes;
2365655fd6a9Sachartre 	int		status = 0;
2366655fd6a9Sachartre 
2367655fd6a9Sachartre 	num_nodes = md_node_count(mdp);
2368655fd6a9Sachartre 	ASSERT(num_nodes > 0);
2369655fd6a9Sachartre 
2370655fd6a9Sachartre 	listsz = num_nodes * sizeof (mde_cookie_t);
2371655fd6a9Sachartre 
2372655fd6a9Sachartre 	/* allocate memory for nodes */
2373655fd6a9Sachartre 	chanp = kmem_zalloc(listsz, KM_SLEEP);
2374655fd6a9Sachartre 
23751ae08745Sheppo 	/* get the channels for this node */
2376655fd6a9Sachartre 	num_chans = md_scan_dag(mdp, vd_node,
23771ae08745Sheppo 	    md_find_name(mdp, VDC_MD_CHAN_NAME),
23781ae08745Sheppo 	    md_find_name(mdp, "fwd"), chanp);
23791ae08745Sheppo 
23801ae08745Sheppo 	/* expecting at least one channel */
23811ae08745Sheppo 	if (num_chans <= 0) {
23821ae08745Sheppo 		cmn_err(CE_NOTE, "No '%s' node for '%s' port",
23831ae08745Sheppo 		    VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME);
23841ae08745Sheppo 		status = ENOENT;
23851ae08745Sheppo 		goto done;
23861ae08745Sheppo 
23871ae08745Sheppo 	} else if (num_chans != 1) {
2388655fd6a9Sachartre 		DMSGX(0, "Expected 1 '%s' node for '%s' port, found %d\n",
2389655fd6a9Sachartre 		    VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME, num_chans);
23901ae08745Sheppo 	}
23911ae08745Sheppo 
23921ae08745Sheppo 	/*
23931ae08745Sheppo 	 * We use the first channel found (index 0), irrespective of how
23941ae08745Sheppo 	 * many are there in total.
23951ae08745Sheppo 	 */
2396655fd6a9Sachartre 	if (md_get_prop_val(mdp, chanp[0], VDC_MD_ID, ldc_id) != 0) {
2397655fd6a9Sachartre 		cmn_err(CE_NOTE, "Channel '%s' property not found", VDC_MD_ID);
23981ae08745Sheppo 		status = ENOENT;
23991ae08745Sheppo 	}
24001ae08745Sheppo 
24011ae08745Sheppo done:
24021ae08745Sheppo 	kmem_free(chanp, listsz);
24031ae08745Sheppo 	return (status);
24041ae08745Sheppo }
24051ae08745Sheppo 
24060a55fbb7Slm66018 static int
24070a55fbb7Slm66018 vdc_do_ldc_up(vdc_t *vdc)
24080a55fbb7Slm66018 {
24090a55fbb7Slm66018 	int		status;
24103af08d82Slm66018 	ldc_status_t	ldc_state;
24110a55fbb7Slm66018 
24123af08d82Slm66018 	DMSG(vdc, 0, "[%d] Bringing up channel %lx\n",
24133af08d82Slm66018 	    vdc->instance, vdc->ldc_id);
24143af08d82Slm66018 
24153af08d82Slm66018 	if (vdc->lifecycle == VDC_LC_DETACHING)
24163af08d82Slm66018 		return (EINVAL);
24170a55fbb7Slm66018 
24180a55fbb7Slm66018 	if ((status = ldc_up(vdc->ldc_handle)) != 0) {
24190a55fbb7Slm66018 		switch (status) {
24200a55fbb7Slm66018 		case ECONNREFUSED:	/* listener not ready at other end */
24213af08d82Slm66018 			DMSG(vdc, 0, "[%d] ldc_up(%lx,...) return %d\n",
2422e1ebb9ecSlm66018 			    vdc->instance, vdc->ldc_id, status);
24230a55fbb7Slm66018 			status = 0;
24240a55fbb7Slm66018 			break;
24250a55fbb7Slm66018 		default:
24263af08d82Slm66018 			DMSG(vdc, 0, "[%d] Failed to bring up LDC: "
24273af08d82Slm66018 			    "channel=%ld, err=%d", vdc->instance, vdc->ldc_id,
24283af08d82Slm66018 			    status);
24293af08d82Slm66018 			break;
24303af08d82Slm66018 		}
24313af08d82Slm66018 	}
24323af08d82Slm66018 
24333af08d82Slm66018 	if (ldc_status(vdc->ldc_handle, &ldc_state) == 0) {
24343af08d82Slm66018 		vdc->ldc_state = ldc_state;
24353af08d82Slm66018 		if (ldc_state == LDC_UP) {
24363af08d82Slm66018 			DMSG(vdc, 0, "[%d] LDC channel already up\n",
24373af08d82Slm66018 			    vdc->instance);
24383af08d82Slm66018 			vdc->seq_num = 1;
24393af08d82Slm66018 			vdc->seq_num_reply = 0;
24400a55fbb7Slm66018 		}
24410a55fbb7Slm66018 	}
24420a55fbb7Slm66018 
24430a55fbb7Slm66018 	return (status);
24440a55fbb7Slm66018 }
24450a55fbb7Slm66018 
24460a55fbb7Slm66018 /*
24470a55fbb7Slm66018  * Function:
24480a55fbb7Slm66018  *	vdc_terminate_ldc()
24490a55fbb7Slm66018  *
24500a55fbb7Slm66018  * Description:
24510a55fbb7Slm66018  *
24520a55fbb7Slm66018  * Arguments:
24530a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
24540a55fbb7Slm66018  *
24550a55fbb7Slm66018  * Return Code:
24560a55fbb7Slm66018  *	None
24570a55fbb7Slm66018  */
24581ae08745Sheppo static void
24591ae08745Sheppo vdc_terminate_ldc(vdc_t *vdc)
24601ae08745Sheppo {
24611ae08745Sheppo 	int	instance = ddi_get_instance(vdc->dip);
24621ae08745Sheppo 
24631ae08745Sheppo 	ASSERT(vdc != NULL);
24641ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
24651ae08745Sheppo 
24663af08d82Slm66018 	DMSG(vdc, 0, "[%d] initialized=%x\n", instance, vdc->initialized);
24671ae08745Sheppo 
24681ae08745Sheppo 	if (vdc->initialized & VDC_LDC_OPEN) {
24693af08d82Slm66018 		DMSG(vdc, 0, "[%d] ldc_close()\n", instance);
24701ae08745Sheppo 		(void) ldc_close(vdc->ldc_handle);
24711ae08745Sheppo 	}
24721ae08745Sheppo 	if (vdc->initialized & VDC_LDC_CB) {
24733af08d82Slm66018 		DMSG(vdc, 0, "[%d] ldc_unreg_callback()\n", instance);
24741ae08745Sheppo 		(void) ldc_unreg_callback(vdc->ldc_handle);
24751ae08745Sheppo 	}
24761ae08745Sheppo 	if (vdc->initialized & VDC_LDC) {
24773af08d82Slm66018 		DMSG(vdc, 0, "[%d] ldc_fini()\n", instance);
24781ae08745Sheppo 		(void) ldc_fini(vdc->ldc_handle);
24791ae08745Sheppo 		vdc->ldc_handle = NULL;
24801ae08745Sheppo 	}
24811ae08745Sheppo 
24821ae08745Sheppo 	vdc->initialized &= ~(VDC_LDC | VDC_LDC_CB | VDC_LDC_OPEN);
24831ae08745Sheppo }
24841ae08745Sheppo 
24851ae08745Sheppo /* -------------------------------------------------------------------------- */
24861ae08745Sheppo 
24871ae08745Sheppo /*
24881ae08745Sheppo  * Descriptor Ring helper routines
24891ae08745Sheppo  */
24901ae08745Sheppo 
24910a55fbb7Slm66018 /*
24920a55fbb7Slm66018  * Function:
24930a55fbb7Slm66018  *	vdc_init_descriptor_ring()
24940a55fbb7Slm66018  *
24950a55fbb7Slm66018  * Description:
24960a55fbb7Slm66018  *
24970a55fbb7Slm66018  * Arguments:
24980a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
24990a55fbb7Slm66018  *
25000a55fbb7Slm66018  * Return Code:
25010a55fbb7Slm66018  *	0	- Success
25020a55fbb7Slm66018  */
25031ae08745Sheppo static int
25041ae08745Sheppo vdc_init_descriptor_ring(vdc_t *vdc)
25051ae08745Sheppo {
25061ae08745Sheppo 	vd_dring_entry_t	*dep = NULL;	/* DRing Entry pointer */
25070a55fbb7Slm66018 	int	status = 0;
25081ae08745Sheppo 	int	i;
25091ae08745Sheppo 
25103af08d82Slm66018 	DMSG(vdc, 0, "[%d] initialized=%x\n", vdc->instance, vdc->initialized);
25111ae08745Sheppo 
25121ae08745Sheppo 	ASSERT(vdc != NULL);
25131ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
25141ae08745Sheppo 	ASSERT(vdc->ldc_handle != NULL);
25151ae08745Sheppo 
2516e1ebb9ecSlm66018 	/* ensure we have enough room to store max sized block */
2517e1ebb9ecSlm66018 	ASSERT(maxphys <= VD_MAX_BLOCK_SIZE);
2518e1ebb9ecSlm66018 
25190a55fbb7Slm66018 	if ((vdc->initialized & VDC_DRING_INIT) == 0) {
25203af08d82Slm66018 		DMSG(vdc, 0, "[%d] ldc_mem_dring_create\n", vdc->instance);
2521e1ebb9ecSlm66018 		/*
2522e1ebb9ecSlm66018 		 * Calculate the maximum block size we can transmit using one
2523e1ebb9ecSlm66018 		 * Descriptor Ring entry from the attributes returned by the
2524e1ebb9ecSlm66018 		 * vDisk server. This is subject to a minimum of 'maxphys'
2525e1ebb9ecSlm66018 		 * as we do not have the capability to split requests over
2526e1ebb9ecSlm66018 		 * multiple DRing entries.
2527e1ebb9ecSlm66018 		 */
2528e1ebb9ecSlm66018 		if ((vdc->max_xfer_sz * vdc->block_size) < maxphys) {
25293af08d82Slm66018 			DMSG(vdc, 0, "[%d] using minimum DRing size\n",
2530e1ebb9ecSlm66018 			    vdc->instance);
2531e1ebb9ecSlm66018 			vdc->dring_max_cookies = maxphys / PAGESIZE;
2532e1ebb9ecSlm66018 		} else {
2533e1ebb9ecSlm66018 			vdc->dring_max_cookies =
2534e1ebb9ecSlm66018 			    (vdc->max_xfer_sz * vdc->block_size) / PAGESIZE;
2535e1ebb9ecSlm66018 		}
2536e1ebb9ecSlm66018 		vdc->dring_entry_size = (sizeof (vd_dring_entry_t) +
2537e1ebb9ecSlm66018 		    (sizeof (ldc_mem_cookie_t) *
2538e1ebb9ecSlm66018 		    (vdc->dring_max_cookies - 1)));
2539e1ebb9ecSlm66018 		vdc->dring_len = VD_DRING_LEN;
2540e1ebb9ecSlm66018 
2541e1ebb9ecSlm66018 		status = ldc_mem_dring_create(vdc->dring_len,
2542e1ebb9ecSlm66018 		    vdc->dring_entry_size, &vdc->ldc_dring_hdl);
25431ae08745Sheppo 		if ((vdc->ldc_dring_hdl == NULL) || (status != 0)) {
25443af08d82Slm66018 			DMSG(vdc, 0, "[%d] Descriptor ring creation failed",
2545e1ebb9ecSlm66018 			    vdc->instance);
25461ae08745Sheppo 			return (status);
25471ae08745Sheppo 		}
25480a55fbb7Slm66018 		vdc->initialized |= VDC_DRING_INIT;
25490a55fbb7Slm66018 	}
25501ae08745Sheppo 
25510a55fbb7Slm66018 	if ((vdc->initialized & VDC_DRING_BOUND) == 0) {
25523af08d82Slm66018 		DMSG(vdc, 0, "[%d] ldc_mem_dring_bind\n", vdc->instance);
25530a55fbb7Slm66018 		vdc->dring_cookie =
25540a55fbb7Slm66018 		    kmem_zalloc(sizeof (ldc_mem_cookie_t), KM_SLEEP);
25551ae08745Sheppo 
25561ae08745Sheppo 		status = ldc_mem_dring_bind(vdc->ldc_handle, vdc->ldc_dring_hdl,
25574bac2208Snarayan 		    LDC_SHADOW_MAP|LDC_DIRECT_MAP, LDC_MEM_RW,
25580a55fbb7Slm66018 		    &vdc->dring_cookie[0],
25591ae08745Sheppo 		    &vdc->dring_cookie_count);
25601ae08745Sheppo 		if (status != 0) {
25613af08d82Slm66018 			DMSG(vdc, 0, "[%d] Failed to bind descriptor ring "
25623af08d82Slm66018 			    "(%lx) to channel (%lx) status=%d\n",
25633af08d82Slm66018 			    vdc->instance, vdc->ldc_dring_hdl,
25643af08d82Slm66018 			    vdc->ldc_handle, status);
25651ae08745Sheppo 			return (status);
25661ae08745Sheppo 		}
25671ae08745Sheppo 		ASSERT(vdc->dring_cookie_count == 1);
25681ae08745Sheppo 		vdc->initialized |= VDC_DRING_BOUND;
25690a55fbb7Slm66018 	}
25701ae08745Sheppo 
25711ae08745Sheppo 	status = ldc_mem_dring_info(vdc->ldc_dring_hdl, &vdc->dring_mem_info);
25721ae08745Sheppo 	if (status != 0) {
25733af08d82Slm66018 		DMSG(vdc, 0,
25743af08d82Slm66018 		    "[%d] Failed to get info for descriptor ring (%lx)\n",
2575e1ebb9ecSlm66018 		    vdc->instance, vdc->ldc_dring_hdl);
25761ae08745Sheppo 		return (status);
25771ae08745Sheppo 	}
25781ae08745Sheppo 
25790a55fbb7Slm66018 	if ((vdc->initialized & VDC_DRING_LOCAL) == 0) {
25803af08d82Slm66018 		DMSG(vdc, 0, "[%d] local dring\n", vdc->instance);
25810a55fbb7Slm66018 
25821ae08745Sheppo 		/* Allocate the local copy of this dring */
25830a55fbb7Slm66018 		vdc->local_dring =
2584e1ebb9ecSlm66018 		    kmem_zalloc(vdc->dring_len * sizeof (vdc_local_desc_t),
25851ae08745Sheppo 		    KM_SLEEP);
25861ae08745Sheppo 		vdc->initialized |= VDC_DRING_LOCAL;
25870a55fbb7Slm66018 	}
25881ae08745Sheppo 
25891ae08745Sheppo 	/*
25900a55fbb7Slm66018 	 * Mark all DRing entries as free and initialize the private
25910a55fbb7Slm66018 	 * descriptor's memory handles. If any entry is initialized,
25920a55fbb7Slm66018 	 * we need to free it later so we set the bit in 'initialized'
25930a55fbb7Slm66018 	 * at the start.
25941ae08745Sheppo 	 */
25951ae08745Sheppo 	vdc->initialized |= VDC_DRING_ENTRY;
2596e1ebb9ecSlm66018 	for (i = 0; i < vdc->dring_len; i++) {
25971ae08745Sheppo 		dep = VDC_GET_DRING_ENTRY_PTR(vdc, i);
25981ae08745Sheppo 		dep->hdr.dstate = VIO_DESC_FREE;
25991ae08745Sheppo 
26001ae08745Sheppo 		status = ldc_mem_alloc_handle(vdc->ldc_handle,
26011ae08745Sheppo 		    &vdc->local_dring[i].desc_mhdl);
26021ae08745Sheppo 		if (status != 0) {
26033af08d82Slm66018 			DMSG(vdc, 0, "![%d] Failed to alloc mem handle for"
26041ae08745Sheppo 			    " descriptor %d", vdc->instance, i);
26051ae08745Sheppo 			return (status);
26061ae08745Sheppo 		}
26073af08d82Slm66018 		vdc->local_dring[i].is_free = B_TRUE;
26081ae08745Sheppo 		vdc->local_dring[i].dep = dep;
26091ae08745Sheppo 	}
26101ae08745Sheppo 
26113af08d82Slm66018 	/* Initialize the starting index */
26123af08d82Slm66018 	vdc->dring_curr_idx = 0;
26131ae08745Sheppo 
26141ae08745Sheppo 	return (status);
26151ae08745Sheppo }
26161ae08745Sheppo 
26170a55fbb7Slm66018 /*
26180a55fbb7Slm66018  * Function:
26190a55fbb7Slm66018  *	vdc_destroy_descriptor_ring()
26200a55fbb7Slm66018  *
26210a55fbb7Slm66018  * Description:
26220a55fbb7Slm66018  *
26230a55fbb7Slm66018  * Arguments:
26240a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
26250a55fbb7Slm66018  *
26260a55fbb7Slm66018  * Return Code:
26270a55fbb7Slm66018  *	None
26280a55fbb7Slm66018  */
26291ae08745Sheppo static void
26301ae08745Sheppo vdc_destroy_descriptor_ring(vdc_t *vdc)
26311ae08745Sheppo {
26320a55fbb7Slm66018 	vdc_local_desc_t	*ldep = NULL;	/* Local Dring Entry Pointer */
26331ae08745Sheppo 	ldc_mem_handle_t	mhdl = NULL;
26343af08d82Slm66018 	ldc_mem_info_t		minfo;
26351ae08745Sheppo 	int			status = -1;
26361ae08745Sheppo 	int			i;	/* loop */
26371ae08745Sheppo 
26381ae08745Sheppo 	ASSERT(vdc != NULL);
26391ae08745Sheppo 	ASSERT(mutex_owned(&vdc->lock));
26401ae08745Sheppo 
26413af08d82Slm66018 	DMSG(vdc, 0, "[%d] Entered\n", vdc->instance);
26421ae08745Sheppo 
26431ae08745Sheppo 	if (vdc->initialized & VDC_DRING_ENTRY) {
26443af08d82Slm66018 		DMSG(vdc, 0,
26453af08d82Slm66018 		    "[%d] Removing Local DRing entries\n", vdc->instance);
2646e1ebb9ecSlm66018 		for (i = 0; i < vdc->dring_len; i++) {
26470a55fbb7Slm66018 			ldep = &vdc->local_dring[i];
26480a55fbb7Slm66018 			mhdl = ldep->desc_mhdl;
26491ae08745Sheppo 
26500a55fbb7Slm66018 			if (mhdl == NULL)
26510a55fbb7Slm66018 				continue;
26520a55fbb7Slm66018 
26533af08d82Slm66018 			if ((status = ldc_mem_info(mhdl, &minfo)) != 0) {
26543af08d82Slm66018 				DMSG(vdc, 0,
26553af08d82Slm66018 				    "ldc_mem_info returned an error: %d\n",
26563af08d82Slm66018 				    status);
26573af08d82Slm66018 
26583af08d82Slm66018 				/*
26593af08d82Slm66018 				 * This must mean that the mem handle
26603af08d82Slm66018 				 * is not valid. Clear it out so that
26613af08d82Slm66018 				 * no one tries to use it.
26623af08d82Slm66018 				 */
26633af08d82Slm66018 				ldep->desc_mhdl = NULL;
26643af08d82Slm66018 				continue;
26653af08d82Slm66018 			}
26663af08d82Slm66018 
26673af08d82Slm66018 			if (minfo.status == LDC_BOUND) {
26683af08d82Slm66018 				(void) ldc_mem_unbind_handle(mhdl);
26693af08d82Slm66018 			}
26703af08d82Slm66018 
26711ae08745Sheppo 			(void) ldc_mem_free_handle(mhdl);
26723af08d82Slm66018 
26733af08d82Slm66018 			ldep->desc_mhdl = NULL;
26741ae08745Sheppo 		}
26751ae08745Sheppo 		vdc->initialized &= ~VDC_DRING_ENTRY;
26761ae08745Sheppo 	}
26771ae08745Sheppo 
26781ae08745Sheppo 	if (vdc->initialized & VDC_DRING_LOCAL) {
26793af08d82Slm66018 		DMSG(vdc, 0, "[%d] Freeing Local DRing\n", vdc->instance);
26801ae08745Sheppo 		kmem_free(vdc->local_dring,
2681e1ebb9ecSlm66018 		    vdc->dring_len * sizeof (vdc_local_desc_t));
26821ae08745Sheppo 		vdc->initialized &= ~VDC_DRING_LOCAL;
26831ae08745Sheppo 	}
26841ae08745Sheppo 
26851ae08745Sheppo 	if (vdc->initialized & VDC_DRING_BOUND) {
26863af08d82Slm66018 		DMSG(vdc, 0, "[%d] Unbinding DRing\n", vdc->instance);
26871ae08745Sheppo 		status = ldc_mem_dring_unbind(vdc->ldc_dring_hdl);
26881ae08745Sheppo 		if (status == 0) {
26891ae08745Sheppo 			vdc->initialized &= ~VDC_DRING_BOUND;
26901ae08745Sheppo 		} else {
26913af08d82Slm66018 			DMSG(vdc, 0, "[%d] Error %d unbinding DRing %lx",
2692e1ebb9ecSlm66018 			    vdc->instance, status, vdc->ldc_dring_hdl);
26931ae08745Sheppo 		}
26943af08d82Slm66018 		kmem_free(vdc->dring_cookie, sizeof (ldc_mem_cookie_t));
26951ae08745Sheppo 	}
26961ae08745Sheppo 
26971ae08745Sheppo 	if (vdc->initialized & VDC_DRING_INIT) {
26983af08d82Slm66018 		DMSG(vdc, 0, "[%d] Destroying DRing\n", vdc->instance);
26991ae08745Sheppo 		status = ldc_mem_dring_destroy(vdc->ldc_dring_hdl);
27001ae08745Sheppo 		if (status == 0) {
27011ae08745Sheppo 			vdc->ldc_dring_hdl = NULL;
27021ae08745Sheppo 			bzero(&vdc->dring_mem_info, sizeof (ldc_mem_info_t));
27031ae08745Sheppo 			vdc->initialized &= ~VDC_DRING_INIT;
27041ae08745Sheppo 		} else {
27053af08d82Slm66018 			DMSG(vdc, 0, "[%d] Error %d destroying DRing (%lx)",
2706e1ebb9ecSlm66018 			    vdc->instance, status, vdc->ldc_dring_hdl);
27071ae08745Sheppo 		}
27081ae08745Sheppo 	}
27091ae08745Sheppo }
27101ae08745Sheppo 
27111ae08745Sheppo /*
27123af08d82Slm66018  * Function:
27133af08d82Slm66018  *	vdc_map_to_shared_ring()
27141ae08745Sheppo  *
27151ae08745Sheppo  * Description:
27163af08d82Slm66018  *	Copy contents of the local descriptor to the shared
27173af08d82Slm66018  *	memory descriptor.
27181ae08745Sheppo  *
27193af08d82Slm66018  * Arguments:
27203af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
27213af08d82Slm66018  *	idx	- descriptor ring index
27223af08d82Slm66018  *
27233af08d82Slm66018  * Return Code:
27243af08d82Slm66018  *	None
27251ae08745Sheppo  */
27261ae08745Sheppo static int
27273af08d82Slm66018 vdc_map_to_shared_dring(vdc_t *vdcp, int idx)
27281ae08745Sheppo {
27293af08d82Slm66018 	vdc_local_desc_t	*ldep;
27303af08d82Slm66018 	vd_dring_entry_t	*dep;
27313af08d82Slm66018 	int			rv;
27321ae08745Sheppo 
27333af08d82Slm66018 	ldep = &(vdcp->local_dring[idx]);
27341ae08745Sheppo 
27353af08d82Slm66018 	/* for now leave in the old pop_mem_hdl stuff */
27363af08d82Slm66018 	if (ldep->nbytes > 0) {
27373af08d82Slm66018 		rv = vdc_populate_mem_hdl(vdcp, ldep);
27383af08d82Slm66018 		if (rv) {
27393af08d82Slm66018 			DMSG(vdcp, 0, "[%d] Cannot populate mem handle\n",
27403af08d82Slm66018 			    vdcp->instance);
27413af08d82Slm66018 			return (rv);
27423af08d82Slm66018 		}
27433af08d82Slm66018 	}
27441ae08745Sheppo 
27453af08d82Slm66018 	/*
27463af08d82Slm66018 	 * fill in the data details into the DRing
27473af08d82Slm66018 	 */
2748d10e4ef2Snarayan 	dep = ldep->dep;
27491ae08745Sheppo 	ASSERT(dep != NULL);
27501ae08745Sheppo 
27513af08d82Slm66018 	dep->payload.req_id = VDC_GET_NEXT_REQ_ID(vdcp);
27523af08d82Slm66018 	dep->payload.operation = ldep->operation;
27533af08d82Slm66018 	dep->payload.addr = ldep->offset;
27543af08d82Slm66018 	dep->payload.nbytes = ldep->nbytes;
2755055d7c80Scarlsonj 	dep->payload.status = (uint32_t)-1;	/* vds will set valid value */
27563af08d82Slm66018 	dep->payload.slice = ldep->slice;
27573af08d82Slm66018 	dep->hdr.dstate = VIO_DESC_READY;
27583af08d82Slm66018 	dep->hdr.ack = 1;		/* request an ACK for every message */
27591ae08745Sheppo 
27603af08d82Slm66018 	return (0);
27611ae08745Sheppo }
27621ae08745Sheppo 
27631ae08745Sheppo /*
27641ae08745Sheppo  * Function:
27653af08d82Slm66018  *	vdc_send_request
27663af08d82Slm66018  *
27673af08d82Slm66018  * Description:
27683af08d82Slm66018  *	This routine writes the data to be transmitted to vds into the
27693af08d82Slm66018  *	descriptor, notifies vds that the ring has been updated and
27703af08d82Slm66018  *	then waits for the request to be processed.
27713af08d82Slm66018  *
27723af08d82Slm66018  * Arguments:
27733af08d82Slm66018  *	vdcp	  - the soft state pointer
27743af08d82Slm66018  *	operation - operation we want vds to perform (VD_OP_XXX)
27753af08d82Slm66018  *	addr	  - address of data buf to be read/written.
27763af08d82Slm66018  *	nbytes	  - number of bytes to read/write
27773af08d82Slm66018  *	slice	  - the disk slice this request is for
27783af08d82Slm66018  *	offset	  - relative disk offset
27793af08d82Slm66018  *	cb_type   - type of call - STRATEGY or SYNC
27803af08d82Slm66018  *	cb_arg	  - parameter to be sent to server (depends on VD_OP_XXX type)
27813af08d82Slm66018  *			. mode for ioctl(9e)
27823af08d82Slm66018  *			. LP64 diskaddr_t (block I/O)
27833af08d82Slm66018  *	dir	  - direction of operation (READ/WRITE/BOTH)
27843af08d82Slm66018  *
27853af08d82Slm66018  * Return Codes:
27863af08d82Slm66018  *	0
27873af08d82Slm66018  *	ENXIO
27883af08d82Slm66018  */
27893af08d82Slm66018 static int
27903af08d82Slm66018 vdc_send_request(vdc_t *vdcp, int operation, caddr_t addr,
27913af08d82Slm66018     size_t nbytes, int slice, diskaddr_t offset, int cb_type,
27923af08d82Slm66018     void *cb_arg, vio_desc_direction_t dir)
27933af08d82Slm66018 {
2794366a92acSlm66018 	int	rv = 0;
2795366a92acSlm66018 
27963af08d82Slm66018 	ASSERT(vdcp != NULL);
279787a7269eSachartre 	ASSERT(slice == VD_SLICE_NONE || slice < V_NUMPAR);
27983af08d82Slm66018 
27993af08d82Slm66018 	mutex_enter(&vdcp->lock);
28003af08d82Slm66018 
2801366a92acSlm66018 	/*
2802366a92acSlm66018 	 * If this is a block read/write operation we update the I/O statistics
2803366a92acSlm66018 	 * to indicate that the request is being put on the waitq to be
2804366a92acSlm66018 	 * serviced.
2805366a92acSlm66018 	 *
2806366a92acSlm66018 	 * We do it here (a common routine for both synchronous and strategy
2807366a92acSlm66018 	 * calls) for performance reasons - we are already holding vdc->lock
2808366a92acSlm66018 	 * so there is no extra locking overhead. We would have to explicitly
2809366a92acSlm66018 	 * grab the 'lock' mutex to update the stats if we were to do this
2810366a92acSlm66018 	 * higher up the stack in vdc_strategy() et. al.
2811366a92acSlm66018 	 */
2812366a92acSlm66018 	if ((operation == VD_OP_BREAD) || (operation == VD_OP_BWRITE)) {
2813366a92acSlm66018 		DTRACE_IO1(start, buf_t *, cb_arg);
2814366a92acSlm66018 		VD_KSTAT_WAITQ_ENTER(vdcp->io_stats);
2815366a92acSlm66018 	}
2816366a92acSlm66018 
28173af08d82Slm66018 	do {
28183c96341aSnarayan 		while (vdcp->state != VDC_STATE_RUNNING) {
28193af08d82Slm66018 
28203c96341aSnarayan 			/* return error if detaching */
28213c96341aSnarayan 			if (vdcp->state == VDC_STATE_DETACH) {
2822366a92acSlm66018 				rv = ENXIO;
2823366a92acSlm66018 				goto done;
28243c96341aSnarayan 			}
2825655fd6a9Sachartre 
2826655fd6a9Sachartre 			/* fail request if connection timeout is reached */
2827655fd6a9Sachartre 			if (vdcp->ctimeout_reached) {
2828366a92acSlm66018 				rv = EIO;
2829366a92acSlm66018 				goto done;
2830655fd6a9Sachartre 			}
2831655fd6a9Sachartre 
28322f5224aeSachartre 			/*
28332f5224aeSachartre 			 * If we are panicking and the disk is not ready then
28342f5224aeSachartre 			 * we can't send any request because we can't complete
28352f5224aeSachartre 			 * the handshake now.
28362f5224aeSachartre 			 */
28372f5224aeSachartre 			if (ddi_in_panic()) {
2838366a92acSlm66018 				rv = EIO;
2839366a92acSlm66018 				goto done;
28402f5224aeSachartre 			}
28412f5224aeSachartre 
2842655fd6a9Sachartre 			cv_wait(&vdcp->running_cv, &vdcp->lock);
28433c96341aSnarayan 		}
28443c96341aSnarayan 
28453af08d82Slm66018 	} while (vdc_populate_descriptor(vdcp, operation, addr,
28463af08d82Slm66018 	    nbytes, slice, offset, cb_type, cb_arg, dir));
28473af08d82Slm66018 
2848366a92acSlm66018 done:
2849366a92acSlm66018 	/*
2850366a92acSlm66018 	 * If this is a block read/write we update the I/O statistics kstat
2851366a92acSlm66018 	 * to indicate that this request has been placed on the queue for
2852366a92acSlm66018 	 * processing (i.e sent to the vDisk server) - iostat(1M) will
2853366a92acSlm66018 	 * report the time waiting for the vDisk server under the %b column
2854366a92acSlm66018 	 * In the case of an error we simply take it off the wait queue.
2855366a92acSlm66018 	 */
2856366a92acSlm66018 	if ((operation == VD_OP_BREAD) || (operation == VD_OP_BWRITE)) {
2857366a92acSlm66018 		if (rv == 0) {
2858366a92acSlm66018 			VD_KSTAT_WAITQ_TO_RUNQ(vdcp->io_stats);
2859366a92acSlm66018 			DTRACE_PROBE1(send, buf_t *, cb_arg);
2860366a92acSlm66018 		} else {
2861366a92acSlm66018 			VD_UPDATE_ERR_STATS(vdcp, vd_transerrs);
2862366a92acSlm66018 			VD_KSTAT_WAITQ_EXIT(vdcp->io_stats);
2863366a92acSlm66018 			DTRACE_IO1(done, buf_t *, cb_arg);
2864366a92acSlm66018 		}
2865366a92acSlm66018 	}
2866366a92acSlm66018 
28673af08d82Slm66018 	mutex_exit(&vdcp->lock);
2868366a92acSlm66018 
2869366a92acSlm66018 	return (rv);
28703af08d82Slm66018 }
28713af08d82Slm66018 
28723af08d82Slm66018 
28733af08d82Slm66018 /*
28743af08d82Slm66018  * Function:
28751ae08745Sheppo  *	vdc_populate_descriptor
28761ae08745Sheppo  *
28771ae08745Sheppo  * Description:
28781ae08745Sheppo  *	This routine writes the data to be transmitted to vds into the
28791ae08745Sheppo  *	descriptor, notifies vds that the ring has been updated and
28801ae08745Sheppo  *	then waits for the request to be processed.
28811ae08745Sheppo  *
28821ae08745Sheppo  * Arguments:
28833af08d82Slm66018  *	vdcp	  - the soft state pointer
28841ae08745Sheppo  *	operation - operation we want vds to perform (VD_OP_XXX)
28853af08d82Slm66018  *	addr	  - address of data buf to be read/written.
28863af08d82Slm66018  *	nbytes	  - number of bytes to read/write
28873af08d82Slm66018  *	slice	  - the disk slice this request is for
28883af08d82Slm66018  *	offset	  - relative disk offset
28893af08d82Slm66018  *	cb_type   - type of call - STRATEGY or SYNC
28903af08d82Slm66018  *	cb_arg	  - parameter to be sent to server (depends on VD_OP_XXX type)
28911ae08745Sheppo  *			. mode for ioctl(9e)
28921ae08745Sheppo  *			. LP64 diskaddr_t (block I/O)
28933af08d82Slm66018  *	dir	  - direction of operation (READ/WRITE/BOTH)
28941ae08745Sheppo  *
28951ae08745Sheppo  * Return Codes:
28961ae08745Sheppo  *	0
28971ae08745Sheppo  *	EAGAIN
289817cadca8Slm66018  *	ECONNRESET
28991ae08745Sheppo  *	ENXIO
29001ae08745Sheppo  */
29011ae08745Sheppo static int
29023af08d82Slm66018 vdc_populate_descriptor(vdc_t *vdcp, int operation, caddr_t addr,
29033af08d82Slm66018     size_t nbytes, int slice, diskaddr_t offset, int cb_type,
29043af08d82Slm66018     void *cb_arg, vio_desc_direction_t dir)
29051ae08745Sheppo {
29063af08d82Slm66018 	vdc_local_desc_t	*local_dep = NULL; /* Local Dring Pointer */
29073af08d82Slm66018 	int			idx;		/* Index of DRing entry used */
29083af08d82Slm66018 	int			next_idx;
29091ae08745Sheppo 	vio_dring_msg_t		dmsg;
29103af08d82Slm66018 	size_t			msglen;
29118e6a2a04Slm66018 	int			rv;
29121ae08745Sheppo 
29133af08d82Slm66018 	ASSERT(MUTEX_HELD(&vdcp->lock));
29143af08d82Slm66018 	vdcp->threads_pending++;
29153af08d82Slm66018 loop:
29163af08d82Slm66018 	DMSG(vdcp, 2, ": dring_curr_idx = %d\n", vdcp->dring_curr_idx);
29171ae08745Sheppo 
29183af08d82Slm66018 	/* Get next available D-Ring entry */
29193af08d82Slm66018 	idx = vdcp->dring_curr_idx;
29203af08d82Slm66018 	local_dep = &(vdcp->local_dring[idx]);
29211ae08745Sheppo 
29223af08d82Slm66018 	if (!local_dep->is_free) {
29233af08d82Slm66018 		DMSG(vdcp, 2, "[%d]: dring full - waiting for space\n",
29243af08d82Slm66018 		    vdcp->instance);
29253af08d82Slm66018 		cv_wait(&vdcp->dring_free_cv, &vdcp->lock);
29263af08d82Slm66018 		if (vdcp->state == VDC_STATE_RUNNING ||
29273af08d82Slm66018 		    vdcp->state == VDC_STATE_HANDLE_PENDING) {
29283af08d82Slm66018 			goto loop;
29293af08d82Slm66018 		}
29303af08d82Slm66018 		vdcp->threads_pending--;
29313af08d82Slm66018 		return (ECONNRESET);
29321ae08745Sheppo 	}
29331ae08745Sheppo 
29343af08d82Slm66018 	next_idx = idx + 1;
29353af08d82Slm66018 	if (next_idx >= vdcp->dring_len)
29363af08d82Slm66018 		next_idx = 0;
29373af08d82Slm66018 	vdcp->dring_curr_idx = next_idx;
29381ae08745Sheppo 
29393af08d82Slm66018 	ASSERT(local_dep->is_free);
29401ae08745Sheppo 
29413af08d82Slm66018 	local_dep->operation = operation;
2942d10e4ef2Snarayan 	local_dep->addr = addr;
29433af08d82Slm66018 	local_dep->nbytes = nbytes;
29443af08d82Slm66018 	local_dep->slice = slice;
29453af08d82Slm66018 	local_dep->offset = offset;
29463af08d82Slm66018 	local_dep->cb_type = cb_type;
29473af08d82Slm66018 	local_dep->cb_arg = cb_arg;
29483af08d82Slm66018 	local_dep->dir = dir;
29493af08d82Slm66018 
29503af08d82Slm66018 	local_dep->is_free = B_FALSE;
29513af08d82Slm66018 
29523af08d82Slm66018 	rv = vdc_map_to_shared_dring(vdcp, idx);
29533af08d82Slm66018 	if (rv) {
29543af08d82Slm66018 		DMSG(vdcp, 0, "[%d]: cannot bind memory - waiting ..\n",
29553af08d82Slm66018 		    vdcp->instance);
29563af08d82Slm66018 		/* free the descriptor */
29573af08d82Slm66018 		local_dep->is_free = B_TRUE;
29583af08d82Slm66018 		vdcp->dring_curr_idx = idx;
29593af08d82Slm66018 		cv_wait(&vdcp->membind_cv, &vdcp->lock);
29603af08d82Slm66018 		if (vdcp->state == VDC_STATE_RUNNING ||
29613af08d82Slm66018 		    vdcp->state == VDC_STATE_HANDLE_PENDING) {
29623af08d82Slm66018 			goto loop;
29631ae08745Sheppo 		}
29643af08d82Slm66018 		vdcp->threads_pending--;
29653af08d82Slm66018 		return (ECONNRESET);
29661ae08745Sheppo 	}
29671ae08745Sheppo 
29681ae08745Sheppo 	/*
29691ae08745Sheppo 	 * Send a msg with the DRing details to vds
29701ae08745Sheppo 	 */
29711ae08745Sheppo 	VIO_INIT_DRING_DATA_TAG(dmsg);
29723af08d82Slm66018 	VDC_INIT_DRING_DATA_MSG_IDS(dmsg, vdcp);
29733af08d82Slm66018 	dmsg.dring_ident = vdcp->dring_ident;
29741ae08745Sheppo 	dmsg.start_idx = idx;
29751ae08745Sheppo 	dmsg.end_idx = idx;
29763af08d82Slm66018 	vdcp->seq_num++;
29771ae08745Sheppo 
2978366a92acSlm66018 	DTRACE_PROBE2(populate, int, vdcp->instance,
2979366a92acSlm66018 	    vdc_local_desc_t *, local_dep);
29803af08d82Slm66018 	DMSG(vdcp, 2, "ident=0x%lx, st=%u, end=%u, seq=%ld\n",
29813af08d82Slm66018 	    vdcp->dring_ident, dmsg.start_idx, dmsg.end_idx, dmsg.seq_num);
29821ae08745Sheppo 
29833af08d82Slm66018 	/*
29843af08d82Slm66018 	 * note we're still holding the lock here to
29853af08d82Slm66018 	 * make sure the message goes out in order !!!...
29863af08d82Slm66018 	 */
29873af08d82Slm66018 	msglen = sizeof (dmsg);
29883af08d82Slm66018 	rv = vdc_send(vdcp, (caddr_t)&dmsg, &msglen);
29893af08d82Slm66018 	switch (rv) {
29903af08d82Slm66018 	case ECONNRESET:
29913af08d82Slm66018 		/*
29923af08d82Slm66018 		 * vdc_send initiates the reset on failure.
29933af08d82Slm66018 		 * Since the transaction has already been put
29943af08d82Slm66018 		 * on the local dring, it will automatically get
29953af08d82Slm66018 		 * retried when the channel is reset. Given that,
29963af08d82Slm66018 		 * it is ok to just return success even though the
29973af08d82Slm66018 		 * send failed.
29983af08d82Slm66018 		 */
29993af08d82Slm66018 		rv = 0;
30003af08d82Slm66018 		break;
3001d10e4ef2Snarayan 
30023af08d82Slm66018 	case 0: /* EOK */
30033af08d82Slm66018 		DMSG(vdcp, 1, "sent via LDC: rv=%d\n", rv);
30043af08d82Slm66018 		break;
3005d10e4ef2Snarayan 
30063af08d82Slm66018 	default:
30073af08d82Slm66018 		goto cleanup_and_exit;
30083af08d82Slm66018 	}
3009e1ebb9ecSlm66018 
30103af08d82Slm66018 	vdcp->threads_pending--;
30113af08d82Slm66018 	return (rv);
30123af08d82Slm66018 
30133af08d82Slm66018 cleanup_and_exit:
30143af08d82Slm66018 	DMSG(vdcp, 0, "unexpected error, rv=%d\n", rv);
30153af08d82Slm66018 	return (ENXIO);
30161ae08745Sheppo }
30171ae08745Sheppo 
30181ae08745Sheppo /*
30193af08d82Slm66018  * Function:
30203af08d82Slm66018  *	vdc_do_sync_op
30213af08d82Slm66018  *
30223af08d82Slm66018  * Description:
30233af08d82Slm66018  * 	Wrapper around vdc_populate_descriptor that blocks until the
30243af08d82Slm66018  * 	response to the message is available.
30253af08d82Slm66018  *
30263af08d82Slm66018  * Arguments:
30273af08d82Slm66018  *	vdcp	  - the soft state pointer
30283af08d82Slm66018  *	operation - operation we want vds to perform (VD_OP_XXX)
30293af08d82Slm66018  *	addr	  - address of data buf to be read/written.
30303af08d82Slm66018  *	nbytes	  - number of bytes to read/write
30313af08d82Slm66018  *	slice	  - the disk slice this request is for
30323af08d82Slm66018  *	offset	  - relative disk offset
30333af08d82Slm66018  *	cb_type   - type of call - STRATEGY or SYNC
30343af08d82Slm66018  *	cb_arg	  - parameter to be sent to server (depends on VD_OP_XXX type)
30353af08d82Slm66018  *			. mode for ioctl(9e)
30363af08d82Slm66018  *			. LP64 diskaddr_t (block I/O)
30373af08d82Slm66018  *	dir	  - direction of operation (READ/WRITE/BOTH)
30382f5224aeSachartre  *	rconflict - check for reservation conflict in case of failure
30392f5224aeSachartre  *
30402f5224aeSachartre  * rconflict should be set to B_TRUE by most callers. Callers invoking the
30412f5224aeSachartre  * VD_OP_SCSICMD operation can set rconflict to B_FALSE if they check the
30422f5224aeSachartre  * result of a successful operation with vd_scsi_status().
30433af08d82Slm66018  *
30443af08d82Slm66018  * Return Codes:
30453af08d82Slm66018  *	0
30463af08d82Slm66018  *	EAGAIN
30473af08d82Slm66018  *	EFAULT
30483af08d82Slm66018  *	ENXIO
30493af08d82Slm66018  *	EIO
30500a55fbb7Slm66018  */
30513af08d82Slm66018 static int
30523af08d82Slm66018 vdc_do_sync_op(vdc_t *vdcp, int operation, caddr_t addr, size_t nbytes,
30533af08d82Slm66018     int slice, diskaddr_t offset, int cb_type, void *cb_arg,
30542f5224aeSachartre     vio_desc_direction_t dir, boolean_t rconflict)
30553af08d82Slm66018 {
30563af08d82Slm66018 	int status;
30572f5224aeSachartre 	vdc_io_t *vio;
30582f5224aeSachartre 	boolean_t check_resv_conflict = B_FALSE;
30593af08d82Slm66018 
30603af08d82Slm66018 	ASSERT(cb_type == CB_SYNC);
30611ae08745Sheppo 
30621ae08745Sheppo 	/*
30633af08d82Slm66018 	 * Grab the lock, if blocked wait until the server
30643af08d82Slm66018 	 * response causes us to wake up again.
30653af08d82Slm66018 	 */
30663af08d82Slm66018 	mutex_enter(&vdcp->lock);
30673af08d82Slm66018 	vdcp->sync_op_cnt++;
30683af08d82Slm66018 	while (vdcp->sync_op_blocked && vdcp->state != VDC_STATE_DETACH)
30693af08d82Slm66018 		cv_wait(&vdcp->sync_blocked_cv, &vdcp->lock);
30703af08d82Slm66018 
30713af08d82Slm66018 	if (vdcp->state == VDC_STATE_DETACH) {
30723af08d82Slm66018 		cv_broadcast(&vdcp->sync_blocked_cv);
30733af08d82Slm66018 		vdcp->sync_op_cnt--;
30743af08d82Slm66018 		mutex_exit(&vdcp->lock);
30753af08d82Slm66018 		return (ENXIO);
30763af08d82Slm66018 	}
30773af08d82Slm66018 
30783af08d82Slm66018 	/* now block anyone other thread entering after us */
30793af08d82Slm66018 	vdcp->sync_op_blocked = B_TRUE;
30803af08d82Slm66018 	vdcp->sync_op_pending = B_TRUE;
30813af08d82Slm66018 	mutex_exit(&vdcp->lock);
30823af08d82Slm66018 
3083655fd6a9Sachartre 	status = vdc_send_request(vdcp, operation, addr,
30843af08d82Slm66018 	    nbytes, slice, offset, cb_type, cb_arg, dir);
30853af08d82Slm66018 
3086655fd6a9Sachartre 	mutex_enter(&vdcp->lock);
3087655fd6a9Sachartre 
3088655fd6a9Sachartre 	if (status != 0) {
3089655fd6a9Sachartre 		vdcp->sync_op_pending = B_FALSE;
3090655fd6a9Sachartre 	} else {
30913af08d82Slm66018 		/*
30923af08d82Slm66018 		 * block until our transaction completes.
30933af08d82Slm66018 		 * Also anyone else waiting also gets to go next.
30943af08d82Slm66018 		 */
30953af08d82Slm66018 		while (vdcp->sync_op_pending && vdcp->state != VDC_STATE_DETACH)
30963af08d82Slm66018 			cv_wait(&vdcp->sync_pending_cv, &vdcp->lock);
30973af08d82Slm66018 
3098655fd6a9Sachartre 		DMSG(vdcp, 2, ": operation returned %d\n",
3099655fd6a9Sachartre 		    vdcp->sync_op_status);
31003c96341aSnarayan 		if (vdcp->state == VDC_STATE_DETACH) {
31013c96341aSnarayan 			vdcp->sync_op_pending = B_FALSE;
31023af08d82Slm66018 			status = ENXIO;
31033c96341aSnarayan 		} else {
31043af08d82Slm66018 			status = vdcp->sync_op_status;
31052f5224aeSachartre 			if (status != 0 && vdcp->failfast_interval != 0) {
31062f5224aeSachartre 				/*
31072f5224aeSachartre 				 * Operation has failed and failfast is enabled.
31082f5224aeSachartre 				 * We need to check if the failure is due to a
31092f5224aeSachartre 				 * reservation conflict if this was requested.
31102f5224aeSachartre 				 */
31112f5224aeSachartre 				check_resv_conflict = rconflict;
31122f5224aeSachartre 			}
31132f5224aeSachartre 
31143c96341aSnarayan 		}
3115655fd6a9Sachartre 	}
31163c96341aSnarayan 
31173af08d82Slm66018 	vdcp->sync_op_status = 0;
31183af08d82Slm66018 	vdcp->sync_op_blocked = B_FALSE;
31193af08d82Slm66018 	vdcp->sync_op_cnt--;
31203af08d82Slm66018 
31213af08d82Slm66018 	/* signal the next waiting thread */
31223af08d82Slm66018 	cv_signal(&vdcp->sync_blocked_cv);
31232f5224aeSachartre 
31242f5224aeSachartre 	/*
31252f5224aeSachartre 	 * We have to check for reservation conflict after unblocking sync
31262f5224aeSachartre 	 * operations because some sync operations will be used to do this
31272f5224aeSachartre 	 * check.
31282f5224aeSachartre 	 */
31292f5224aeSachartre 	if (check_resv_conflict) {
31302f5224aeSachartre 		vio = vdc_failfast_io_queue(vdcp, NULL);
31312f5224aeSachartre 		while (vio->vio_qtime != 0)
31322f5224aeSachartre 			cv_wait(&vdcp->failfast_io_cv, &vdcp->lock);
31332f5224aeSachartre 		kmem_free(vio, sizeof (vdc_io_t));
31342f5224aeSachartre 	}
31352f5224aeSachartre 
31363af08d82Slm66018 	mutex_exit(&vdcp->lock);
31373af08d82Slm66018 
31383af08d82Slm66018 	return (status);
31393af08d82Slm66018 }
31403af08d82Slm66018 
31413af08d82Slm66018 
31423af08d82Slm66018 /*
31433af08d82Slm66018  * Function:
31443af08d82Slm66018  *	vdc_drain_response()
31453af08d82Slm66018  *
31463af08d82Slm66018  * Description:
31471ae08745Sheppo  * 	When a guest is panicking, the completion of requests needs to be
31481ae08745Sheppo  * 	handled differently because interrupts are disabled and vdc
31491ae08745Sheppo  * 	will not get messages. We have to poll for the messages instead.
31503af08d82Slm66018  *
3151366a92acSlm66018  *	Note: since we don't have a buf_t available we cannot implement
3152366a92acSlm66018  *	the io:::done DTrace probe in this specific case.
3153366a92acSlm66018  *
31543af08d82Slm66018  * Arguments:
31553af08d82Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
31563af08d82Slm66018  *
31573af08d82Slm66018  * Return Code:
31583af08d82Slm66018  *	0	- Success
31591ae08745Sheppo  */
31603af08d82Slm66018 static int
31613af08d82Slm66018 vdc_drain_response(vdc_t *vdc)
31623af08d82Slm66018 {
31633af08d82Slm66018 	int 			rv, idx, retries;
31643af08d82Slm66018 	size_t			msglen;
31653af08d82Slm66018 	vdc_local_desc_t 	*ldep = NULL;	/* Local Dring Entry Pointer */
31663af08d82Slm66018 	vio_dring_msg_t		dmsg;
31673af08d82Slm66018 
31683af08d82Slm66018 	mutex_enter(&vdc->lock);
31693af08d82Slm66018 
31701ae08745Sheppo 	retries = 0;
31711ae08745Sheppo 	for (;;) {
31721ae08745Sheppo 		msglen = sizeof (dmsg);
31733af08d82Slm66018 		rv = ldc_read(vdc->ldc_handle, (caddr_t)&dmsg, &msglen);
31748e6a2a04Slm66018 		if (rv) {
31758e6a2a04Slm66018 			rv = EINVAL;
31761ae08745Sheppo 			break;
31771ae08745Sheppo 		}
31781ae08745Sheppo 
31791ae08745Sheppo 		/*
31801ae08745Sheppo 		 * if there are no packets wait and check again
31811ae08745Sheppo 		 */
31828e6a2a04Slm66018 		if ((rv == 0) && (msglen == 0)) {
31831ae08745Sheppo 			if (retries++ > vdc_dump_retries) {
31848e6a2a04Slm66018 				rv = EAGAIN;
31851ae08745Sheppo 				break;
31861ae08745Sheppo 			}
31871ae08745Sheppo 
3188d10e4ef2Snarayan 			drv_usecwait(vdc_usec_timeout_dump);
31891ae08745Sheppo 			continue;
31901ae08745Sheppo 		}
31911ae08745Sheppo 
31921ae08745Sheppo 		/*
31931ae08745Sheppo 		 * Ignore all messages that are not ACKs/NACKs to
31941ae08745Sheppo 		 * DRing requests.
31951ae08745Sheppo 		 */
31961ae08745Sheppo 		if ((dmsg.tag.vio_msgtype != VIO_TYPE_DATA) ||
31971ae08745Sheppo 		    (dmsg.tag.vio_subtype_env != VIO_DRING_DATA)) {
31983af08d82Slm66018 			DMSG(vdc, 0, "discard pkt: type=%d sub=%d env=%d\n",
31991ae08745Sheppo 			    dmsg.tag.vio_msgtype,
32001ae08745Sheppo 			    dmsg.tag.vio_subtype,
32011ae08745Sheppo 			    dmsg.tag.vio_subtype_env);
32021ae08745Sheppo 			continue;
32031ae08745Sheppo 		}
32041ae08745Sheppo 
32051ae08745Sheppo 		/*
32063af08d82Slm66018 		 * set the appropriate return value for the current request.
32071ae08745Sheppo 		 */
32081ae08745Sheppo 		switch (dmsg.tag.vio_subtype) {
32091ae08745Sheppo 		case VIO_SUBTYPE_ACK:
32108e6a2a04Slm66018 			rv = 0;
32111ae08745Sheppo 			break;
32121ae08745Sheppo 		case VIO_SUBTYPE_NACK:
32138e6a2a04Slm66018 			rv = EAGAIN;
32141ae08745Sheppo 			break;
32151ae08745Sheppo 		default:
32161ae08745Sheppo 			continue;
32171ae08745Sheppo 		}
32181ae08745Sheppo 
32193af08d82Slm66018 		idx = dmsg.start_idx;
32203af08d82Slm66018 		if (idx >= vdc->dring_len) {
32213af08d82Slm66018 			DMSG(vdc, 0, "[%d] Bogus ack data : start %d\n",
3222e1ebb9ecSlm66018 			    vdc->instance, idx);
32233af08d82Slm66018 			continue;
32241ae08745Sheppo 		}
32253af08d82Slm66018 		ldep = &vdc->local_dring[idx];
32263af08d82Slm66018 		if (ldep->dep->hdr.dstate != VIO_DESC_DONE) {
32273af08d82Slm66018 			DMSG(vdc, 0, "[%d] Entry @ %d - state !DONE %d\n",
32283af08d82Slm66018 			    vdc->instance, idx, ldep->dep->hdr.dstate);
32291ae08745Sheppo 			continue;
32301ae08745Sheppo 		}
32311ae08745Sheppo 
32323af08d82Slm66018 		DMSG(vdc, 1, "[%d] Depopulating idx=%d state=%d\n",
32333af08d82Slm66018 		    vdc->instance, idx, ldep->dep->hdr.dstate);
3234366a92acSlm66018 
32353af08d82Slm66018 		rv = vdc_depopulate_descriptor(vdc, idx);
32363af08d82Slm66018 		if (rv) {
32373af08d82Slm66018 			DMSG(vdc, 0,
32383af08d82Slm66018 			    "[%d] Entry @ %d - depopulate failed ..\n",
32393af08d82Slm66018 			    vdc->instance, idx);
32401ae08745Sheppo 		}
32411ae08745Sheppo 
32423af08d82Slm66018 		/* if this is the last descriptor - break out of loop */
32433af08d82Slm66018 		if ((idx + 1) % vdc->dring_len == vdc->dring_curr_idx)
32443af08d82Slm66018 			break;
32453af08d82Slm66018 	}
32463af08d82Slm66018 
32473af08d82Slm66018 	mutex_exit(&vdc->lock);
32483af08d82Slm66018 	DMSG(vdc, 0, "End idx=%d\n", idx);
32493af08d82Slm66018 
32503af08d82Slm66018 	return (rv);
32511ae08745Sheppo }
32521ae08745Sheppo 
32531ae08745Sheppo 
32540a55fbb7Slm66018 /*
32550a55fbb7Slm66018  * Function:
32560a55fbb7Slm66018  *	vdc_depopulate_descriptor()
32570a55fbb7Slm66018  *
32580a55fbb7Slm66018  * Description:
32590a55fbb7Slm66018  *
32600a55fbb7Slm66018  * Arguments:
32610a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
32620a55fbb7Slm66018  *	idx	- Index of the Descriptor Ring entry being modified
32630a55fbb7Slm66018  *
32640a55fbb7Slm66018  * Return Code:
32650a55fbb7Slm66018  *	0	- Success
32660a55fbb7Slm66018  */
32671ae08745Sheppo static int
32681ae08745Sheppo vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx)
32691ae08745Sheppo {
32701ae08745Sheppo 	vd_dring_entry_t *dep = NULL;		/* Dring Entry Pointer */
32711ae08745Sheppo 	vdc_local_desc_t *ldep = NULL;		/* Local Dring Entry Pointer */
32721ae08745Sheppo 	int		status = ENXIO;
32738e6a2a04Slm66018 	int		rv = 0;
32741ae08745Sheppo 
32751ae08745Sheppo 	ASSERT(vdc != NULL);
3276e1ebb9ecSlm66018 	ASSERT(idx < vdc->dring_len);
32771ae08745Sheppo 	ldep = &vdc->local_dring[idx];
32781ae08745Sheppo 	ASSERT(ldep != NULL);
32793af08d82Slm66018 	ASSERT(MUTEX_HELD(&vdc->lock));
32803af08d82Slm66018 
3281366a92acSlm66018 	DTRACE_PROBE2(depopulate, int, vdc->instance, vdc_local_desc_t *, ldep);
32823af08d82Slm66018 	DMSG(vdc, 2, ": idx = %d\n", idx);
3283366a92acSlm66018 
32841ae08745Sheppo 	dep = ldep->dep;
32851ae08745Sheppo 	ASSERT(dep != NULL);
3286e1ebb9ecSlm66018 	ASSERT((dep->hdr.dstate == VIO_DESC_DONE) ||
3287e1ebb9ecSlm66018 	    (dep->payload.status == ECANCELED));
32881ae08745Sheppo 
3289e1ebb9ecSlm66018 	VDC_MARK_DRING_ENTRY_FREE(vdc, idx);
32903af08d82Slm66018 
32913af08d82Slm66018 	ldep->is_free = B_TRUE;
32921ae08745Sheppo 	status = dep->payload.status;
3293205eeb1aSlm66018 	DMSG(vdc, 2, ": is_free = %d : status = %d\n", ldep->is_free, status);
32941ae08745Sheppo 
3295eff7243fSlm66018 	/*
3296eff7243fSlm66018 	 * If no buffers were used to transfer information to the server when
3297eff7243fSlm66018 	 * populating the descriptor then no memory handles need to be unbound
3298eff7243fSlm66018 	 * and we can return now.
3299eff7243fSlm66018 	 */
3300eff7243fSlm66018 	if (ldep->nbytes == 0) {
3301eff7243fSlm66018 		cv_signal(&vdc->dring_free_cv);
33028e6a2a04Slm66018 		return (status);
3303eff7243fSlm66018 	}
33048e6a2a04Slm66018 
33051ae08745Sheppo 	/*
33061ae08745Sheppo 	 * If the upper layer passed in a misaligned address we copied the
33071ae08745Sheppo 	 * data into an aligned buffer before sending it to LDC - we now
33081ae08745Sheppo 	 * copy it back to the original buffer.
33091ae08745Sheppo 	 */
33101ae08745Sheppo 	if (ldep->align_addr) {
33111ae08745Sheppo 		ASSERT(ldep->addr != NULL);
33121ae08745Sheppo 
33133c96341aSnarayan 		if (dep->payload.nbytes > 0)
33143c96341aSnarayan 			bcopy(ldep->align_addr, ldep->addr,
33153c96341aSnarayan 			    dep->payload.nbytes);
33161ae08745Sheppo 		kmem_free(ldep->align_addr,
33173c96341aSnarayan 		    sizeof (caddr_t) * P2ROUNDUP(ldep->nbytes, 8));
33181ae08745Sheppo 		ldep->align_addr = NULL;
33191ae08745Sheppo 	}
33201ae08745Sheppo 
33218e6a2a04Slm66018 	rv = ldc_mem_unbind_handle(ldep->desc_mhdl);
33228e6a2a04Slm66018 	if (rv != 0) {
33233af08d82Slm66018 		DMSG(vdc, 0, "?[%d] unbind mhdl 0x%lx @ idx %d failed (%d)",
33248e6a2a04Slm66018 		    vdc->instance, ldep->desc_mhdl, idx, rv);
33258e6a2a04Slm66018 		/*
33268e6a2a04Slm66018 		 * The error returned by the vDisk server is more informative
33278e6a2a04Slm66018 		 * and thus has a higher priority but if it isn't set we ensure
33288e6a2a04Slm66018 		 * that this function returns an error.
33298e6a2a04Slm66018 		 */
33308e6a2a04Slm66018 		if (status == 0)
33318e6a2a04Slm66018 			status = EINVAL;
33321ae08745Sheppo 	}
33331ae08745Sheppo 
33343af08d82Slm66018 	cv_signal(&vdc->membind_cv);
33353af08d82Slm66018 	cv_signal(&vdc->dring_free_cv);
33363af08d82Slm66018 
33371ae08745Sheppo 	return (status);
33381ae08745Sheppo }
33391ae08745Sheppo 
33400a55fbb7Slm66018 /*
33410a55fbb7Slm66018  * Function:
33420a55fbb7Slm66018  *	vdc_populate_mem_hdl()
33430a55fbb7Slm66018  *
33440a55fbb7Slm66018  * Description:
33450a55fbb7Slm66018  *
33460a55fbb7Slm66018  * Arguments:
33470a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
33480a55fbb7Slm66018  *	idx	- Index of the Descriptor Ring entry being modified
33490a55fbb7Slm66018  *	addr	- virtual address being mapped in
33500a55fbb7Slm66018  *	nybtes	- number of bytes in 'addr'
33510a55fbb7Slm66018  *	operation - the vDisk operation being performed (VD_OP_xxx)
33520a55fbb7Slm66018  *
33530a55fbb7Slm66018  * Return Code:
33540a55fbb7Slm66018  *	0	- Success
33550a55fbb7Slm66018  */
33561ae08745Sheppo static int
33573af08d82Slm66018 vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep)
33581ae08745Sheppo {
33591ae08745Sheppo 	vd_dring_entry_t	*dep = NULL;
33601ae08745Sheppo 	ldc_mem_handle_t	mhdl;
33611ae08745Sheppo 	caddr_t			vaddr;
33623af08d82Slm66018 	size_t			nbytes;
33634bac2208Snarayan 	uint8_t			perm = LDC_MEM_RW;
33644bac2208Snarayan 	uint8_t			maptype;
33651ae08745Sheppo 	int			rv = 0;
33661ae08745Sheppo 	int			i;
33671ae08745Sheppo 
33683af08d82Slm66018 	ASSERT(vdcp != NULL);
33691ae08745Sheppo 
33703af08d82Slm66018 	dep = ldep->dep;
33711ae08745Sheppo 	mhdl = ldep->desc_mhdl;
33721ae08745Sheppo 
33733af08d82Slm66018 	switch (ldep->dir) {
33743af08d82Slm66018 	case VIO_read_dir:
33751ae08745Sheppo 		perm = LDC_MEM_W;
33761ae08745Sheppo 		break;
33771ae08745Sheppo 
33783af08d82Slm66018 	case VIO_write_dir:
33791ae08745Sheppo 		perm = LDC_MEM_R;
33801ae08745Sheppo 		break;
33811ae08745Sheppo 
33823af08d82Slm66018 	case VIO_both_dir:
33831ae08745Sheppo 		perm = LDC_MEM_RW;
33841ae08745Sheppo 		break;
33851ae08745Sheppo 
33861ae08745Sheppo 	default:
33871ae08745Sheppo 		ASSERT(0);	/* catch bad programming in vdc */
33881ae08745Sheppo 	}
33891ae08745Sheppo 
33901ae08745Sheppo 	/*
33911ae08745Sheppo 	 * LDC expects any addresses passed in to be 8-byte aligned. We need
33921ae08745Sheppo 	 * to copy the contents of any misaligned buffers to a newly allocated
33931ae08745Sheppo 	 * buffer and bind it instead (and copy the the contents back to the
33941ae08745Sheppo 	 * original buffer passed in when depopulating the descriptor)
33951ae08745Sheppo 	 */
33963af08d82Slm66018 	vaddr = ldep->addr;
33973af08d82Slm66018 	nbytes = ldep->nbytes;
33983af08d82Slm66018 	if (((uint64_t)vaddr & 0x7) != 0) {
3399d10e4ef2Snarayan 		ASSERT(ldep->align_addr == NULL);
34001ae08745Sheppo 		ldep->align_addr =
34013af08d82Slm66018 		    kmem_alloc(sizeof (caddr_t) *
34023af08d82Slm66018 		    P2ROUNDUP(nbytes, 8), KM_SLEEP);
34033af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Misaligned address %p reallocating "
34043af08d82Slm66018 		    "(buf=%p nb=%ld op=%d)\n",
34053af08d82Slm66018 		    vdcp->instance, (void *)vaddr, (void *)ldep->align_addr,
34063af08d82Slm66018 		    nbytes, ldep->operation);
34073af08d82Slm66018 		if (perm != LDC_MEM_W)
34083af08d82Slm66018 			bcopy(vaddr, ldep->align_addr, nbytes);
34091ae08745Sheppo 		vaddr = ldep->align_addr;
34101ae08745Sheppo 	}
34111ae08745Sheppo 
34124bac2208Snarayan 	maptype = LDC_IO_MAP|LDC_SHADOW_MAP|LDC_DIRECT_MAP;
34131ae08745Sheppo 	rv = ldc_mem_bind_handle(mhdl, vaddr, P2ROUNDUP(nbytes, 8),
341487a7269eSachartre 	    maptype, perm, &dep->payload.cookie[0], &dep->payload.ncookies);
34153af08d82Slm66018 	DMSG(vdcp, 2, "[%d] bound mem handle; ncookies=%d\n",
34163af08d82Slm66018 	    vdcp->instance, dep->payload.ncookies);
34171ae08745Sheppo 	if (rv != 0) {
34183af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Failed to bind LDC memory handle "
34193af08d82Slm66018 		    "(mhdl=%p, buf=%p, err=%d)\n",
34203af08d82Slm66018 		    vdcp->instance, (void *)mhdl, (void *)vaddr, rv);
34211ae08745Sheppo 		if (ldep->align_addr) {
34221ae08745Sheppo 			kmem_free(ldep->align_addr,
3423d10e4ef2Snarayan 			    sizeof (caddr_t) * P2ROUNDUP(nbytes, 8));
34241ae08745Sheppo 			ldep->align_addr = NULL;
34251ae08745Sheppo 		}
34261ae08745Sheppo 		return (EAGAIN);
34271ae08745Sheppo 	}
34281ae08745Sheppo 
34291ae08745Sheppo 	/*
34301ae08745Sheppo 	 * Get the other cookies (if any).
34311ae08745Sheppo 	 */
34321ae08745Sheppo 	for (i = 1; i < dep->payload.ncookies; i++) {
34331ae08745Sheppo 		rv = ldc_mem_nextcookie(mhdl, &dep->payload.cookie[i]);
34341ae08745Sheppo 		if (rv != 0) {
34351ae08745Sheppo 			(void) ldc_mem_unbind_handle(mhdl);
34363af08d82Slm66018 			DMSG(vdcp, 0, "?[%d] Failed to get next cookie "
3437e1ebb9ecSlm66018 			    "(mhdl=%lx cnum=%d), err=%d",
34383af08d82Slm66018 			    vdcp->instance, mhdl, i, rv);
34391ae08745Sheppo 			if (ldep->align_addr) {
34401ae08745Sheppo 				kmem_free(ldep->align_addr,
34413c96341aSnarayan 				    sizeof (caddr_t) * ldep->nbytes);
34421ae08745Sheppo 				ldep->align_addr = NULL;
34431ae08745Sheppo 			}
34441ae08745Sheppo 			return (EAGAIN);
34451ae08745Sheppo 		}
34461ae08745Sheppo 	}
34471ae08745Sheppo 
34481ae08745Sheppo 	return (rv);
34491ae08745Sheppo }
34501ae08745Sheppo 
34511ae08745Sheppo /*
34521ae08745Sheppo  * Interrupt handlers for messages from LDC
34531ae08745Sheppo  */
34541ae08745Sheppo 
34550a55fbb7Slm66018 /*
34560a55fbb7Slm66018  * Function:
34570a55fbb7Slm66018  *	vdc_handle_cb()
34580a55fbb7Slm66018  *
34590a55fbb7Slm66018  * Description:
34600a55fbb7Slm66018  *
34610a55fbb7Slm66018  * Arguments:
34620a55fbb7Slm66018  *	event	- Type of event (LDC_EVT_xxx) that triggered the callback
34630a55fbb7Slm66018  *	arg	- soft state pointer for this instance of the device driver.
34640a55fbb7Slm66018  *
34650a55fbb7Slm66018  * Return Code:
34660a55fbb7Slm66018  *	0	- Success
34670a55fbb7Slm66018  */
34681ae08745Sheppo static uint_t
34691ae08745Sheppo vdc_handle_cb(uint64_t event, caddr_t arg)
34701ae08745Sheppo {
34711ae08745Sheppo 	ldc_status_t	ldc_state;
34721ae08745Sheppo 	int		rv = 0;
34731ae08745Sheppo 
34741ae08745Sheppo 	vdc_t	*vdc = (vdc_t *)(void *)arg;
34751ae08745Sheppo 
34761ae08745Sheppo 	ASSERT(vdc != NULL);
34771ae08745Sheppo 
34783af08d82Slm66018 	DMSG(vdc, 1, "evt=%lx seqID=%ld\n", event, vdc->seq_num);
34791ae08745Sheppo 
34801ae08745Sheppo 	/*
34811ae08745Sheppo 	 * Depending on the type of event that triggered this callback,
34823af08d82Slm66018 	 * we modify the handshake state or read the data.
34831ae08745Sheppo 	 *
34841ae08745Sheppo 	 * NOTE: not done as a switch() as event could be triggered by
34851ae08745Sheppo 	 * a state change and a read request. Also the ordering	of the
34861ae08745Sheppo 	 * check for the event types is deliberate.
34871ae08745Sheppo 	 */
34881ae08745Sheppo 	if (event & LDC_EVT_UP) {
34893af08d82Slm66018 		DMSG(vdc, 0, "[%d] Received LDC_EVT_UP\n", vdc->instance);
34903af08d82Slm66018 
34913af08d82Slm66018 		mutex_enter(&vdc->lock);
34921ae08745Sheppo 
34931ae08745Sheppo 		/* get LDC state */
34941ae08745Sheppo 		rv = ldc_status(vdc->ldc_handle, &ldc_state);
34951ae08745Sheppo 		if (rv != 0) {
34963af08d82Slm66018 			DMSG(vdc, 0, "[%d] Couldn't get LDC status %d",
34971ae08745Sheppo 			    vdc->instance, rv);
34981ae08745Sheppo 			return (LDC_SUCCESS);
34991ae08745Sheppo 		}
35003af08d82Slm66018 		if (vdc->ldc_state != LDC_UP && ldc_state == LDC_UP) {
35011ae08745Sheppo 			/*
35023af08d82Slm66018 			 * Reset the transaction sequence numbers when
35033af08d82Slm66018 			 * LDC comes up. We then kick off the handshake
35043af08d82Slm66018 			 * negotiation with the vDisk server.
35051ae08745Sheppo 			 */
35060a55fbb7Slm66018 			vdc->seq_num = 1;
35071ae08745Sheppo 			vdc->seq_num_reply = 0;
35081ae08745Sheppo 			vdc->ldc_state = ldc_state;
35093af08d82Slm66018 			cv_signal(&vdc->initwait_cv);
35103af08d82Slm66018 		}
35113af08d82Slm66018 
35121ae08745Sheppo 		mutex_exit(&vdc->lock);
35131ae08745Sheppo 	}
35141ae08745Sheppo 
35151ae08745Sheppo 	if (event & LDC_EVT_READ) {
351617cadca8Slm66018 		DMSG(vdc, 1, "[%d] Received LDC_EVT_READ\n", vdc->instance);
35173af08d82Slm66018 		mutex_enter(&vdc->read_lock);
35183af08d82Slm66018 		cv_signal(&vdc->read_cv);
35193af08d82Slm66018 		vdc->read_state = VDC_READ_PENDING;
35203af08d82Slm66018 		mutex_exit(&vdc->read_lock);
35211ae08745Sheppo 
35221ae08745Sheppo 		/* that's all we have to do - no need to handle DOWN/RESET */
35231ae08745Sheppo 		return (LDC_SUCCESS);
35241ae08745Sheppo 	}
35251ae08745Sheppo 
35263af08d82Slm66018 	if (event & (LDC_EVT_RESET|LDC_EVT_DOWN)) {
35270a55fbb7Slm66018 
35283af08d82Slm66018 		DMSG(vdc, 0, "[%d] Received LDC RESET event\n", vdc->instance);
35293af08d82Slm66018 
35300a55fbb7Slm66018 		mutex_enter(&vdc->lock);
35313af08d82Slm66018 		/*
35323af08d82Slm66018 		 * Need to wake up any readers so they will
35333af08d82Slm66018 		 * detect that a reset has occurred.
35343af08d82Slm66018 		 */
35353af08d82Slm66018 		mutex_enter(&vdc->read_lock);
35363af08d82Slm66018 		if ((vdc->read_state == VDC_READ_WAITING) ||
35373af08d82Slm66018 		    (vdc->read_state == VDC_READ_RESET))
35383af08d82Slm66018 			cv_signal(&vdc->read_cv);
35393af08d82Slm66018 		vdc->read_state = VDC_READ_RESET;
35403af08d82Slm66018 		mutex_exit(&vdc->read_lock);
35410a55fbb7Slm66018 
35423af08d82Slm66018 		/* wake up any threads waiting for connection to come up */
35433af08d82Slm66018 		if (vdc->state == VDC_STATE_INIT_WAITING) {
35443af08d82Slm66018 			vdc->state = VDC_STATE_RESETTING;
35453af08d82Slm66018 			cv_signal(&vdc->initwait_cv);
35461ae08745Sheppo 		}
35471ae08745Sheppo 
35480a55fbb7Slm66018 		mutex_exit(&vdc->lock);
35491ae08745Sheppo 	}
35501ae08745Sheppo 
35511ae08745Sheppo 	if (event & ~(LDC_EVT_UP | LDC_EVT_RESET | LDC_EVT_DOWN | LDC_EVT_READ))
35523af08d82Slm66018 		DMSG(vdc, 0, "![%d] Unexpected LDC event (%lx) received",
35531ae08745Sheppo 		    vdc->instance, event);
35541ae08745Sheppo 
35551ae08745Sheppo 	return (LDC_SUCCESS);
35561ae08745Sheppo }
35571ae08745Sheppo 
35583af08d82Slm66018 /*
35593af08d82Slm66018  * Function:
35603af08d82Slm66018  *	vdc_wait_for_response()
35613af08d82Slm66018  *
35623af08d82Slm66018  * Description:
35633af08d82Slm66018  *	Block waiting for a response from the server. If there is
35643af08d82Slm66018  *	no data the thread block on the read_cv that is signalled
35653af08d82Slm66018  *	by the callback when an EVT_READ occurs.
35663af08d82Slm66018  *
35673af08d82Slm66018  * Arguments:
35683af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
35693af08d82Slm66018  *
35703af08d82Slm66018  * Return Code:
35713af08d82Slm66018  *	0	- Success
35723af08d82Slm66018  */
35733af08d82Slm66018 static int
35743af08d82Slm66018 vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp)
35753af08d82Slm66018 {
35763af08d82Slm66018 	size_t		nbytes = sizeof (*msgp);
35773af08d82Slm66018 	int		status;
35783af08d82Slm66018 
35793af08d82Slm66018 	ASSERT(vdcp != NULL);
35803af08d82Slm66018 
35813af08d82Slm66018 	DMSG(vdcp, 1, "[%d] Entered\n", vdcp->instance);
35823af08d82Slm66018 
35833af08d82Slm66018 	status = vdc_recv(vdcp, msgp, &nbytes);
35843af08d82Slm66018 	DMSG(vdcp, 3, "vdc_read() done.. status=0x%x size=0x%x\n",
35853af08d82Slm66018 	    status, (int)nbytes);
35863af08d82Slm66018 	if (status) {
35873af08d82Slm66018 		DMSG(vdcp, 0, "?[%d] Error %d reading LDC msg\n",
35883af08d82Slm66018 		    vdcp->instance, status);
35893af08d82Slm66018 		return (status);
35903af08d82Slm66018 	}
35913af08d82Slm66018 
35923af08d82Slm66018 	if (nbytes < sizeof (vio_msg_tag_t)) {
35933af08d82Slm66018 		DMSG(vdcp, 0, "?[%d] Expect %lu bytes; recv'd %lu\n",
35943af08d82Slm66018 		    vdcp->instance, sizeof (vio_msg_tag_t), nbytes);
35953af08d82Slm66018 		return (ENOMSG);
35963af08d82Slm66018 	}
35973af08d82Slm66018 
35983af08d82Slm66018 	DMSG(vdcp, 2, "[%d] (%x/%x/%x)\n", vdcp->instance,
35993af08d82Slm66018 	    msgp->tag.vio_msgtype,
36003af08d82Slm66018 	    msgp->tag.vio_subtype,
36013af08d82Slm66018 	    msgp->tag.vio_subtype_env);
36023af08d82Slm66018 
36033af08d82Slm66018 	/*
36043af08d82Slm66018 	 * Verify the Session ID of the message
36053af08d82Slm66018 	 *
36063af08d82Slm66018 	 * Every message after the Version has been negotiated should
36073af08d82Slm66018 	 * have the correct session ID set.
36083af08d82Slm66018 	 */
36093af08d82Slm66018 	if ((msgp->tag.vio_sid != vdcp->session_id) &&
36103af08d82Slm66018 	    (msgp->tag.vio_subtype_env != VIO_VER_INFO)) {
36113af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Invalid SID: received 0x%x, "
36123af08d82Slm66018 		    "expected 0x%lx [seq num %lx @ %d]",
36133af08d82Slm66018 		    vdcp->instance, msgp->tag.vio_sid,
36143af08d82Slm66018 		    vdcp->session_id,
36153af08d82Slm66018 		    ((vio_dring_msg_t *)msgp)->seq_num,
36163af08d82Slm66018 		    ((vio_dring_msg_t *)msgp)->start_idx);
36173af08d82Slm66018 		return (ENOMSG);
36183af08d82Slm66018 	}
36193af08d82Slm66018 	return (0);
36203af08d82Slm66018 }
36213af08d82Slm66018 
36223af08d82Slm66018 
36233af08d82Slm66018 /*
36243af08d82Slm66018  * Function:
36253af08d82Slm66018  *	vdc_resubmit_backup_dring()
36263af08d82Slm66018  *
36273af08d82Slm66018  * Description:
36283af08d82Slm66018  *	Resubmit each descriptor in the backed up dring to
36293af08d82Slm66018  * 	vDisk server. The Dring was backed up during connection
36303af08d82Slm66018  *	reset.
36313af08d82Slm66018  *
36323af08d82Slm66018  * Arguments:
36333af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
36343af08d82Slm66018  *
36353af08d82Slm66018  * Return Code:
36363af08d82Slm66018  *	0	- Success
36373af08d82Slm66018  */
36383af08d82Slm66018 static int
36393af08d82Slm66018 vdc_resubmit_backup_dring(vdc_t *vdcp)
36403af08d82Slm66018 {
36413af08d82Slm66018 	int		count;
36423af08d82Slm66018 	int		b_idx;
36433af08d82Slm66018 	int		rv;
36443af08d82Slm66018 	int		dring_size;
36453af08d82Slm66018 	int		status;
36463af08d82Slm66018 	vio_msg_t	vio_msg;
36473af08d82Slm66018 	vdc_local_desc_t	*curr_ldep;
36483af08d82Slm66018 
36493af08d82Slm66018 	ASSERT(MUTEX_NOT_HELD(&vdcp->lock));
36503af08d82Slm66018 	ASSERT(vdcp->state == VDC_STATE_HANDLE_PENDING);
36513af08d82Slm66018 
3652655fd6a9Sachartre 	if (vdcp->local_dring_backup == NULL) {
3653655fd6a9Sachartre 		/* the pending requests have already been processed */
3654655fd6a9Sachartre 		return (0);
3655655fd6a9Sachartre 	}
3656655fd6a9Sachartre 
36573af08d82Slm66018 	DMSG(vdcp, 1, "restoring pending dring entries (len=%d, tail=%d)\n",
36583af08d82Slm66018 	    vdcp->local_dring_backup_len, vdcp->local_dring_backup_tail);
36593af08d82Slm66018 
36603af08d82Slm66018 	/*
36613af08d82Slm66018 	 * Walk the backup copy of the local descriptor ring and
36623af08d82Slm66018 	 * resubmit all the outstanding transactions.
36633af08d82Slm66018 	 */
36643af08d82Slm66018 	b_idx = vdcp->local_dring_backup_tail;
36653af08d82Slm66018 	for (count = 0; count < vdcp->local_dring_backup_len; count++) {
36663af08d82Slm66018 
36673af08d82Slm66018 		curr_ldep = &(vdcp->local_dring_backup[b_idx]);
36683af08d82Slm66018 
3669eff7243fSlm66018 		/* only resubmit outstanding transactions */
36703af08d82Slm66018 		if (!curr_ldep->is_free) {
36713af08d82Slm66018 
36723af08d82Slm66018 			DMSG(vdcp, 1, "resubmitting entry idx=%x\n", b_idx);
36733af08d82Slm66018 			mutex_enter(&vdcp->lock);
36743af08d82Slm66018 			rv = vdc_populate_descriptor(vdcp, curr_ldep->operation,
36753af08d82Slm66018 			    curr_ldep->addr, curr_ldep->nbytes,
36763af08d82Slm66018 			    curr_ldep->slice, curr_ldep->offset,
36773af08d82Slm66018 			    curr_ldep->cb_type, curr_ldep->cb_arg,
36783af08d82Slm66018 			    curr_ldep->dir);
36793af08d82Slm66018 			mutex_exit(&vdcp->lock);
36803af08d82Slm66018 			if (rv) {
36813af08d82Slm66018 				DMSG(vdcp, 1, "[%d] cannot resubmit entry %d\n",
36823af08d82Slm66018 				    vdcp->instance, b_idx);
36833af08d82Slm66018 				return (rv);
36843af08d82Slm66018 			}
36853af08d82Slm66018 
36863af08d82Slm66018 			/* Wait for the response message. */
36873af08d82Slm66018 			DMSG(vdcp, 1, "waiting for response to idx=%x\n",
36883af08d82Slm66018 			    b_idx);
36893af08d82Slm66018 			status = vdc_wait_for_response(vdcp, &vio_msg);
36903af08d82Slm66018 			if (status) {
36913af08d82Slm66018 				DMSG(vdcp, 1, "[%d] wait_for_response "
36923af08d82Slm66018 				    "returned err=%d\n", vdcp->instance,
36933af08d82Slm66018 				    status);
36943af08d82Slm66018 				return (status);
36953af08d82Slm66018 			}
36963af08d82Slm66018 
36973af08d82Slm66018 			DMSG(vdcp, 1, "processing msg for idx=%x\n", b_idx);
36983af08d82Slm66018 			status = vdc_process_data_msg(vdcp, &vio_msg);
36993af08d82Slm66018 			if (status) {
37003af08d82Slm66018 				DMSG(vdcp, 1, "[%d] process_data_msg "
37013af08d82Slm66018 				    "returned err=%d\n", vdcp->instance,
37023af08d82Slm66018 				    status);
37033af08d82Slm66018 				return (status);
37043af08d82Slm66018 			}
37053af08d82Slm66018 		}
37063af08d82Slm66018 
37073af08d82Slm66018 		/* get the next element to submit */
37083af08d82Slm66018 		if (++b_idx >= vdcp->local_dring_backup_len)
37093af08d82Slm66018 			b_idx = 0;
37103af08d82Slm66018 	}
37113af08d82Slm66018 
37123af08d82Slm66018 	/* all done - now clear up pending dring copy */
37133af08d82Slm66018 	dring_size = vdcp->local_dring_backup_len *
37143af08d82Slm66018 	    sizeof (vdcp->local_dring_backup[0]);
37153af08d82Slm66018 
37163af08d82Slm66018 	(void) kmem_free(vdcp->local_dring_backup, dring_size);
37173af08d82Slm66018 
37183af08d82Slm66018 	vdcp->local_dring_backup = NULL;
37193af08d82Slm66018 
37203af08d82Slm66018 	return (0);
37213af08d82Slm66018 }
37223af08d82Slm66018 
37233af08d82Slm66018 /*
37243af08d82Slm66018  * Function:
3725655fd6a9Sachartre  *	vdc_cancel_backup_dring
3726655fd6a9Sachartre  *
3727655fd6a9Sachartre  * Description:
3728655fd6a9Sachartre  *	Cancel each descriptor in the backed up dring to vDisk server.
3729655fd6a9Sachartre  *	The Dring was backed up during connection reset.
3730655fd6a9Sachartre  *
3731655fd6a9Sachartre  * Arguments:
3732655fd6a9Sachartre  *	vdcp	- soft state pointer for this instance of the device driver.
3733655fd6a9Sachartre  *
3734655fd6a9Sachartre  * Return Code:
3735655fd6a9Sachartre  *	None
3736655fd6a9Sachartre  */
3737655fd6a9Sachartre void
3738655fd6a9Sachartre vdc_cancel_backup_ring(vdc_t *vdcp)
3739655fd6a9Sachartre {
3740655fd6a9Sachartre 	vdc_local_desc_t *ldep;
3741655fd6a9Sachartre 	struct buf 	*bufp;
3742655fd6a9Sachartre 	int		count;
3743655fd6a9Sachartre 	int		b_idx;
3744655fd6a9Sachartre 	int		dring_size;
3745655fd6a9Sachartre 
3746655fd6a9Sachartre 	ASSERT(MUTEX_HELD(&vdcp->lock));
3747655fd6a9Sachartre 	ASSERT(vdcp->state == VDC_STATE_INIT ||
3748655fd6a9Sachartre 	    vdcp->state == VDC_STATE_INIT_WAITING ||
3749655fd6a9Sachartre 	    vdcp->state == VDC_STATE_NEGOTIATE ||
3750655fd6a9Sachartre 	    vdcp->state == VDC_STATE_RESETTING);
3751655fd6a9Sachartre 
3752655fd6a9Sachartre 	if (vdcp->local_dring_backup == NULL) {
3753655fd6a9Sachartre 		/* the pending requests have already been processed */
3754655fd6a9Sachartre 		return;
3755655fd6a9Sachartre 	}
3756655fd6a9Sachartre 
3757655fd6a9Sachartre 	DMSG(vdcp, 1, "cancelling pending dring entries (len=%d, tail=%d)\n",
3758655fd6a9Sachartre 	    vdcp->local_dring_backup_len, vdcp->local_dring_backup_tail);
3759655fd6a9Sachartre 
3760655fd6a9Sachartre 	/*
3761655fd6a9Sachartre 	 * Walk the backup copy of the local descriptor ring and
3762655fd6a9Sachartre 	 * cancel all the outstanding transactions.
3763655fd6a9Sachartre 	 */
3764655fd6a9Sachartre 	b_idx = vdcp->local_dring_backup_tail;
3765655fd6a9Sachartre 	for (count = 0; count < vdcp->local_dring_backup_len; count++) {
3766655fd6a9Sachartre 
3767655fd6a9Sachartre 		ldep = &(vdcp->local_dring_backup[b_idx]);
3768655fd6a9Sachartre 
3769655fd6a9Sachartre 		/* only cancel outstanding transactions */
3770655fd6a9Sachartre 		if (!ldep->is_free) {
3771655fd6a9Sachartre 
3772655fd6a9Sachartre 			DMSG(vdcp, 1, "cancelling entry idx=%x\n", b_idx);
3773655fd6a9Sachartre 
3774655fd6a9Sachartre 			/*
3775655fd6a9Sachartre 			 * All requests have already been cleared from the
3776655fd6a9Sachartre 			 * local descriptor ring and the LDC channel has been
3777655fd6a9Sachartre 			 * reset so we will never get any reply for these
3778655fd6a9Sachartre 			 * requests. Now we just have to notify threads waiting
3779655fd6a9Sachartre 			 * for replies that the request has failed.
3780655fd6a9Sachartre 			 */
3781655fd6a9Sachartre 			switch (ldep->cb_type) {
3782655fd6a9Sachartre 			case CB_SYNC:
3783655fd6a9Sachartre 				ASSERT(vdcp->sync_op_pending);
3784655fd6a9Sachartre 				vdcp->sync_op_status = EIO;
3785655fd6a9Sachartre 				vdcp->sync_op_pending = B_FALSE;
3786655fd6a9Sachartre 				cv_signal(&vdcp->sync_pending_cv);
3787655fd6a9Sachartre 				break;
3788655fd6a9Sachartre 
3789655fd6a9Sachartre 			case CB_STRATEGY:
3790655fd6a9Sachartre 				bufp = ldep->cb_arg;
3791655fd6a9Sachartre 				ASSERT(bufp != NULL);
3792655fd6a9Sachartre 				bufp->b_resid = bufp->b_bcount;
3793366a92acSlm66018 				VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
3794366a92acSlm66018 				VD_KSTAT_RUNQ_EXIT(vdcp->io_stats);
3795366a92acSlm66018 				DTRACE_IO1(done, buf_t *, bufp);
3796655fd6a9Sachartre 				bioerror(bufp, EIO);
3797655fd6a9Sachartre 				biodone(bufp);
3798655fd6a9Sachartre 				break;
3799655fd6a9Sachartre 
3800655fd6a9Sachartre 			default:
3801655fd6a9Sachartre 				ASSERT(0);
3802655fd6a9Sachartre 			}
3803655fd6a9Sachartre 
3804655fd6a9Sachartre 		}
3805655fd6a9Sachartre 
3806655fd6a9Sachartre 		/* get the next element to cancel */
3807655fd6a9Sachartre 		if (++b_idx >= vdcp->local_dring_backup_len)
3808655fd6a9Sachartre 			b_idx = 0;
3809655fd6a9Sachartre 	}
3810655fd6a9Sachartre 
3811655fd6a9Sachartre 	/* all done - now clear up pending dring copy */
3812655fd6a9Sachartre 	dring_size = vdcp->local_dring_backup_len *
3813655fd6a9Sachartre 	    sizeof (vdcp->local_dring_backup[0]);
3814655fd6a9Sachartre 
3815655fd6a9Sachartre 	(void) kmem_free(vdcp->local_dring_backup, dring_size);
3816655fd6a9Sachartre 
3817655fd6a9Sachartre 	vdcp->local_dring_backup = NULL;
3818655fd6a9Sachartre 
3819366a92acSlm66018 	DTRACE_PROBE2(processed, int, count, vdc_t *, vdcp);
3820655fd6a9Sachartre }
3821655fd6a9Sachartre 
3822655fd6a9Sachartre /*
3823655fd6a9Sachartre  * Function:
3824655fd6a9Sachartre  *	vdc_connection_timeout
3825655fd6a9Sachartre  *
3826655fd6a9Sachartre  * Description:
3827655fd6a9Sachartre  *	This function is invoked if the timeout set to establish the connection
3828655fd6a9Sachartre  *	with vds expires. This will happen if we spend too much time in the
3829655fd6a9Sachartre  *	VDC_STATE_INIT_WAITING or VDC_STATE_NEGOTIATE states. Then we will
3830655fd6a9Sachartre  *	cancel any pending request and mark them as failed.
3831655fd6a9Sachartre  *
3832655fd6a9Sachartre  *	If the timeout does not expire, it will be cancelled when we reach the
3833655fd6a9Sachartre  *	VDC_STATE_HANDLE_PENDING or VDC_STATE_RESETTING state. This function can
3834655fd6a9Sachartre  *	be invoked while we are in the VDC_STATE_HANDLE_PENDING or
3835655fd6a9Sachartre  *	VDC_STATE_RESETTING state in which case we do nothing because the
3836655fd6a9Sachartre  *	timeout is being cancelled.
3837655fd6a9Sachartre  *
3838655fd6a9Sachartre  * Arguments:
3839655fd6a9Sachartre  *	arg	- argument of the timeout function actually a soft state
3840655fd6a9Sachartre  *		  pointer for the instance of the device driver.
3841655fd6a9Sachartre  *
3842655fd6a9Sachartre  * Return Code:
3843655fd6a9Sachartre  *	None
3844655fd6a9Sachartre  */
3845655fd6a9Sachartre void
3846655fd6a9Sachartre vdc_connection_timeout(void *arg)
3847655fd6a9Sachartre {
3848655fd6a9Sachartre 	vdc_t 		*vdcp = (vdc_t *)arg;
3849655fd6a9Sachartre 
3850655fd6a9Sachartre 	mutex_enter(&vdcp->lock);
3851655fd6a9Sachartre 
3852655fd6a9Sachartre 	if (vdcp->state == VDC_STATE_HANDLE_PENDING ||
3853655fd6a9Sachartre 	    vdcp->state == VDC_STATE_DETACH) {
3854655fd6a9Sachartre 		/*
3855655fd6a9Sachartre 		 * The connection has just been re-established or
3856655fd6a9Sachartre 		 * we are detaching.
3857655fd6a9Sachartre 		 */
3858655fd6a9Sachartre 		vdcp->ctimeout_reached = B_FALSE;
3859655fd6a9Sachartre 		mutex_exit(&vdcp->lock);
3860655fd6a9Sachartre 		return;
3861655fd6a9Sachartre 	}
3862655fd6a9Sachartre 
3863655fd6a9Sachartre 	vdcp->ctimeout_reached = B_TRUE;
3864655fd6a9Sachartre 
3865655fd6a9Sachartre 	/* notify requests waiting for sending */
3866655fd6a9Sachartre 	cv_broadcast(&vdcp->running_cv);
3867655fd6a9Sachartre 
3868655fd6a9Sachartre 	/* cancel requests waiting for a result */
3869655fd6a9Sachartre 	vdc_cancel_backup_ring(vdcp);
3870655fd6a9Sachartre 
3871655fd6a9Sachartre 	mutex_exit(&vdcp->lock);
3872655fd6a9Sachartre 
3873655fd6a9Sachartre 	cmn_err(CE_NOTE, "[%d] connection to service domain timeout",
3874655fd6a9Sachartre 	    vdcp->instance);
3875655fd6a9Sachartre }
3876655fd6a9Sachartre 
3877655fd6a9Sachartre /*
3878655fd6a9Sachartre  * Function:
38793af08d82Slm66018  *	vdc_backup_local_dring()
38803af08d82Slm66018  *
38813af08d82Slm66018  * Description:
38823af08d82Slm66018  *	Backup the current dring in the event of a reset. The Dring
38833af08d82Slm66018  *	transactions will be resubmitted to the server when the
38843af08d82Slm66018  *	connection is restored.
38853af08d82Slm66018  *
38863af08d82Slm66018  * Arguments:
38873af08d82Slm66018  *	vdcp	- soft state pointer for this instance of the device driver.
38883af08d82Slm66018  *
38893af08d82Slm66018  * Return Code:
38903af08d82Slm66018  *	NONE
38913af08d82Slm66018  */
38923af08d82Slm66018 static void
38933af08d82Slm66018 vdc_backup_local_dring(vdc_t *vdcp)
38943af08d82Slm66018 {
38953af08d82Slm66018 	int dring_size;
38963af08d82Slm66018 
3897655fd6a9Sachartre 	ASSERT(MUTEX_HELD(&vdcp->lock));
38983af08d82Slm66018 	ASSERT(vdcp->state == VDC_STATE_RESETTING);
38993af08d82Slm66018 
39003af08d82Slm66018 	/*
39013af08d82Slm66018 	 * If the backup dring is stil around, it means
39023af08d82Slm66018 	 * that the last restore did not complete. However,
39033af08d82Slm66018 	 * since we never got back into the running state,
39043af08d82Slm66018 	 * the backup copy we have is still valid.
39053af08d82Slm66018 	 */
39063af08d82Slm66018 	if (vdcp->local_dring_backup != NULL) {
39073af08d82Slm66018 		DMSG(vdcp, 1, "reusing local descriptor ring backup "
39083af08d82Slm66018 		    "(len=%d, tail=%d)\n", vdcp->local_dring_backup_len,
39093af08d82Slm66018 		    vdcp->local_dring_backup_tail);
39103af08d82Slm66018 		return;
39113af08d82Slm66018 	}
39123af08d82Slm66018 
3913655fd6a9Sachartre 	/*
3914655fd6a9Sachartre 	 * The backup dring can be NULL and the local dring may not be
3915655fd6a9Sachartre 	 * initialized. This can happen if we had a reset while establishing
3916655fd6a9Sachartre 	 * a new connection but after the connection has timed out. In that
3917655fd6a9Sachartre 	 * case the backup dring is NULL because the requests have been
3918655fd6a9Sachartre 	 * cancelled and the request occured before the local dring is
3919655fd6a9Sachartre 	 * initialized.
3920655fd6a9Sachartre 	 */
3921655fd6a9Sachartre 	if (!(vdcp->initialized & VDC_DRING_LOCAL))
3922655fd6a9Sachartre 		return;
3923655fd6a9Sachartre 
39243af08d82Slm66018 	DMSG(vdcp, 1, "backing up the local descriptor ring (len=%d, "
39253af08d82Slm66018 	    "tail=%d)\n", vdcp->dring_len, vdcp->dring_curr_idx);
39263af08d82Slm66018 
39273af08d82Slm66018 	dring_size = vdcp->dring_len * sizeof (vdcp->local_dring[0]);
39283af08d82Slm66018 
39293af08d82Slm66018 	vdcp->local_dring_backup = kmem_alloc(dring_size, KM_SLEEP);
39303af08d82Slm66018 	bcopy(vdcp->local_dring, vdcp->local_dring_backup, dring_size);
39313af08d82Slm66018 
39323af08d82Slm66018 	vdcp->local_dring_backup_tail = vdcp->dring_curr_idx;
39333af08d82Slm66018 	vdcp->local_dring_backup_len = vdcp->dring_len;
39343af08d82Slm66018 }
39353af08d82Slm66018 
39361ae08745Sheppo /* -------------------------------------------------------------------------- */
39371ae08745Sheppo 
39381ae08745Sheppo /*
39391ae08745Sheppo  * The following functions process the incoming messages from vds
39401ae08745Sheppo  */
39411ae08745Sheppo 
39420a55fbb7Slm66018 /*
39430a55fbb7Slm66018  * Function:
39440a55fbb7Slm66018  *      vdc_process_msg_thread()
39450a55fbb7Slm66018  *
39460a55fbb7Slm66018  * Description:
39470a55fbb7Slm66018  *
39483af08d82Slm66018  *	Main VDC message processing thread. Each vDisk instance
39493af08d82Slm66018  * 	consists of a copy of this thread. This thread triggers
39503af08d82Slm66018  * 	all the handshakes and data exchange with the server. It
39513af08d82Slm66018  * 	also handles all channel resets
39523af08d82Slm66018  *
39530a55fbb7Slm66018  * Arguments:
39540a55fbb7Slm66018  *      vdc     - soft state pointer for this instance of the device driver.
39550a55fbb7Slm66018  *
39560a55fbb7Slm66018  * Return Code:
39570a55fbb7Slm66018  *      None
39580a55fbb7Slm66018  */
39591ae08745Sheppo static void
39603af08d82Slm66018 vdc_process_msg_thread(vdc_t *vdcp)
39611ae08745Sheppo {
39621ae08745Sheppo 	int	status;
3963655fd6a9Sachartre 	int	ctimeout;
3964655fd6a9Sachartre 	timeout_id_t tmid = 0;
39651ae08745Sheppo 
39663af08d82Slm66018 	mutex_enter(&vdcp->lock);
39671ae08745Sheppo 
39681ae08745Sheppo 	for (;;) {
39691ae08745Sheppo 
39703af08d82Slm66018 #define	Q(_s)	(vdcp->state == _s) ? #_s :
39713af08d82Slm66018 		DMSG(vdcp, 3, "state = %d (%s)\n", vdcp->state,
39723af08d82Slm66018 		    Q(VDC_STATE_INIT)
39733af08d82Slm66018 		    Q(VDC_STATE_INIT_WAITING)
39743af08d82Slm66018 		    Q(VDC_STATE_NEGOTIATE)
39753af08d82Slm66018 		    Q(VDC_STATE_HANDLE_PENDING)
39763af08d82Slm66018 		    Q(VDC_STATE_RUNNING)
39773af08d82Slm66018 		    Q(VDC_STATE_RESETTING)
39783af08d82Slm66018 		    Q(VDC_STATE_DETACH)
39793af08d82Slm66018 		    "UNKNOWN");
39801ae08745Sheppo 
39813af08d82Slm66018 		switch (vdcp->state) {
39823af08d82Slm66018 		case VDC_STATE_INIT:
39833af08d82Slm66018 
3984655fd6a9Sachartre 			/*
3985655fd6a9Sachartre 			 * If requested, start a timeout to check if the
3986655fd6a9Sachartre 			 * connection with vds is established in the
3987655fd6a9Sachartre 			 * specified delay. If the timeout expires, we
3988655fd6a9Sachartre 			 * will cancel any pending request.
3989655fd6a9Sachartre 			 *
3990655fd6a9Sachartre 			 * If some reset have occurred while establishing
3991655fd6a9Sachartre 			 * the connection, we already have a timeout armed
3992655fd6a9Sachartre 			 * and in that case we don't need to arm a new one.
3993655fd6a9Sachartre 			 */
3994655fd6a9Sachartre 			ctimeout = (vdc_timeout != 0)?
3995655fd6a9Sachartre 			    vdc_timeout : vdcp->ctimeout;
3996655fd6a9Sachartre 
3997655fd6a9Sachartre 			if (ctimeout != 0 && tmid == 0) {
3998655fd6a9Sachartre 				tmid = timeout(vdc_connection_timeout, vdcp,
3999655fd6a9Sachartre 				    ctimeout * drv_usectohz(1000000));
4000655fd6a9Sachartre 			}
4001655fd6a9Sachartre 
40023af08d82Slm66018 			/* Check if have re-initializing repeatedly */
4003655fd6a9Sachartre 			if (vdcp->hshake_cnt++ > vdc_hshake_retries &&
4004655fd6a9Sachartre 			    vdcp->lifecycle != VDC_LC_ONLINE) {
40053c96341aSnarayan 				cmn_err(CE_NOTE, "[%d] disk access failed.\n",
40063c96341aSnarayan 				    vdcp->instance);
40073af08d82Slm66018 				vdcp->state = VDC_STATE_DETACH;
40083af08d82Slm66018 				break;
40093af08d82Slm66018 			}
40103af08d82Slm66018 
40113af08d82Slm66018 			/* Bring up connection with vds via LDC */
40123af08d82Slm66018 			status = vdc_start_ldc_connection(vdcp);
4013655fd6a9Sachartre 			if (status == EINVAL) {
40143af08d82Slm66018 				DMSG(vdcp, 0, "[%d] Could not start LDC",
40153af08d82Slm66018 				    vdcp->instance);
40163af08d82Slm66018 				vdcp->state = VDC_STATE_DETACH;
4017655fd6a9Sachartre 			} else {
40183af08d82Slm66018 				vdcp->state = VDC_STATE_INIT_WAITING;
40193af08d82Slm66018 			}
40203af08d82Slm66018 			break;
40213af08d82Slm66018 
40223af08d82Slm66018 		case VDC_STATE_INIT_WAITING:
40233af08d82Slm66018 
40243af08d82Slm66018 			/*
40253af08d82Slm66018 			 * Let the callback event move us on
40263af08d82Slm66018 			 * when channel is open to server
40273af08d82Slm66018 			 */
40283af08d82Slm66018 			while (vdcp->ldc_state != LDC_UP) {
40293af08d82Slm66018 				cv_wait(&vdcp->initwait_cv, &vdcp->lock);
40303af08d82Slm66018 				if (vdcp->state != VDC_STATE_INIT_WAITING) {
40313af08d82Slm66018 					DMSG(vdcp, 0,
40323af08d82Slm66018 				"state moved to %d out from under us...\n",
40333af08d82Slm66018 					    vdcp->state);
40343af08d82Slm66018 
40353af08d82Slm66018 					break;
40363af08d82Slm66018 				}
40373af08d82Slm66018 			}
40383af08d82Slm66018 			if (vdcp->state == VDC_STATE_INIT_WAITING &&
40393af08d82Slm66018 			    vdcp->ldc_state == LDC_UP) {
40403af08d82Slm66018 				vdcp->state = VDC_STATE_NEGOTIATE;
40413af08d82Slm66018 			}
40423af08d82Slm66018 			break;
40433af08d82Slm66018 
40443af08d82Slm66018 		case VDC_STATE_NEGOTIATE:
40453af08d82Slm66018 			switch (status = vdc_ver_negotiation(vdcp)) {
40463af08d82Slm66018 			case 0:
40473af08d82Slm66018 				break;
40483af08d82Slm66018 			default:
40493af08d82Slm66018 				DMSG(vdcp, 0, "ver negotiate failed (%d)..\n",
40503af08d82Slm66018 				    status);
40513af08d82Slm66018 				goto reset;
40523af08d82Slm66018 			}
40533af08d82Slm66018 
40543af08d82Slm66018 			switch (status = vdc_attr_negotiation(vdcp)) {
40553af08d82Slm66018 			case 0:
40563af08d82Slm66018 				break;
40573af08d82Slm66018 			default:
40583af08d82Slm66018 				DMSG(vdcp, 0, "attr negotiate failed (%d)..\n",
40593af08d82Slm66018 				    status);
40603af08d82Slm66018 				goto reset;
40613af08d82Slm66018 			}
40623af08d82Slm66018 
40633af08d82Slm66018 			switch (status = vdc_dring_negotiation(vdcp)) {
40643af08d82Slm66018 			case 0:
40653af08d82Slm66018 				break;
40663af08d82Slm66018 			default:
40673af08d82Slm66018 				DMSG(vdcp, 0, "dring negotiate failed (%d)..\n",
40683af08d82Slm66018 				    status);
40693af08d82Slm66018 				goto reset;
40703af08d82Slm66018 			}
40713af08d82Slm66018 
40723af08d82Slm66018 			switch (status = vdc_rdx_exchange(vdcp)) {
40733af08d82Slm66018 			case 0:
40743af08d82Slm66018 				vdcp->state = VDC_STATE_HANDLE_PENDING;
40753af08d82Slm66018 				goto done;
40763af08d82Slm66018 			default:
40773af08d82Slm66018 				DMSG(vdcp, 0, "RDX xchg failed ..(%d)\n",
40783af08d82Slm66018 				    status);
40793af08d82Slm66018 				goto reset;
40803af08d82Slm66018 			}
40813af08d82Slm66018 reset:
40823af08d82Slm66018 			DMSG(vdcp, 0, "negotiation failed: resetting (%d)\n",
40833af08d82Slm66018 			    status);
40843af08d82Slm66018 			vdcp->state = VDC_STATE_RESETTING;
4085655fd6a9Sachartre 			vdcp->self_reset = B_TRUE;
40863af08d82Slm66018 done:
40873af08d82Slm66018 			DMSG(vdcp, 0, "negotiation complete (state=0x%x)...\n",
40883af08d82Slm66018 			    vdcp->state);
40893af08d82Slm66018 			break;
40903af08d82Slm66018 
40913af08d82Slm66018 		case VDC_STATE_HANDLE_PENDING:
40923af08d82Slm66018 
4093655fd6a9Sachartre 			if (vdcp->ctimeout_reached) {
4094655fd6a9Sachartre 				/*
4095655fd6a9Sachartre 				 * The connection timeout had been reached so
4096655fd6a9Sachartre 				 * pending requests have been cancelled. Now
4097655fd6a9Sachartre 				 * that the connection is back we can reset
4098655fd6a9Sachartre 				 * the timeout.
4099655fd6a9Sachartre 				 */
4100655fd6a9Sachartre 				ASSERT(vdcp->local_dring_backup == NULL);
4101655fd6a9Sachartre 				ASSERT(tmid != 0);
4102655fd6a9Sachartre 				tmid = 0;
4103655fd6a9Sachartre 				vdcp->ctimeout_reached = B_FALSE;
4104655fd6a9Sachartre 				vdcp->state = VDC_STATE_RUNNING;
4105655fd6a9Sachartre 				DMSG(vdcp, 0, "[%d] connection to service "
4106655fd6a9Sachartre 				    "domain is up", vdcp->instance);
4107655fd6a9Sachartre 				break;
4108655fd6a9Sachartre 			}
4109655fd6a9Sachartre 
41103af08d82Slm66018 			mutex_exit(&vdcp->lock);
4111655fd6a9Sachartre 			if (tmid != 0) {
4112655fd6a9Sachartre 				(void) untimeout(tmid);
4113655fd6a9Sachartre 				tmid = 0;
4114655fd6a9Sachartre 			}
41153af08d82Slm66018 			status = vdc_resubmit_backup_dring(vdcp);
41163af08d82Slm66018 			mutex_enter(&vdcp->lock);
41173af08d82Slm66018 
41183af08d82Slm66018 			if (status)
41193af08d82Slm66018 				vdcp->state = VDC_STATE_RESETTING;
41203af08d82Slm66018 			else
41213af08d82Slm66018 				vdcp->state = VDC_STATE_RUNNING;
41223af08d82Slm66018 
41233af08d82Slm66018 			break;
41243af08d82Slm66018 
41253af08d82Slm66018 		/* enter running state */
41263af08d82Slm66018 		case VDC_STATE_RUNNING:
41273af08d82Slm66018 			/*
41283af08d82Slm66018 			 * Signal anyone waiting for the connection
41293af08d82Slm66018 			 * to come on line.
41303af08d82Slm66018 			 */
41313af08d82Slm66018 			vdcp->hshake_cnt = 0;
41323af08d82Slm66018 			cv_broadcast(&vdcp->running_cv);
41332f5224aeSachartre 
41342f5224aeSachartre 			/* failfast has to been checked after reset */
41352f5224aeSachartre 			cv_signal(&vdcp->failfast_cv);
41362f5224aeSachartre 
41372f5224aeSachartre 			/* ownership is lost during reset */
41382f5224aeSachartre 			if (vdcp->ownership & VDC_OWNERSHIP_WANTED)
41392f5224aeSachartre 				vdcp->ownership |= VDC_OWNERSHIP_RESET;
41402f5224aeSachartre 			cv_signal(&vdcp->ownership_cv);
41412f5224aeSachartre 
41423af08d82Slm66018 			mutex_exit(&vdcp->lock);
41433af08d82Slm66018 
41443af08d82Slm66018 			for (;;) {
41453af08d82Slm66018 				vio_msg_t msg;
41463af08d82Slm66018 				status = vdc_wait_for_response(vdcp, &msg);
41473af08d82Slm66018 				if (status) break;
41483af08d82Slm66018 
41493af08d82Slm66018 				DMSG(vdcp, 1, "[%d] new pkt(s) available\n",
41503af08d82Slm66018 				    vdcp->instance);
41513af08d82Slm66018 				status = vdc_process_data_msg(vdcp, &msg);
41521ae08745Sheppo 				if (status) {
41533af08d82Slm66018 					DMSG(vdcp, 1, "[%d] process_data_msg "
41543af08d82Slm66018 					    "returned err=%d\n", vdcp->instance,
41553af08d82Slm66018 					    status);
41561ae08745Sheppo 					break;
41571ae08745Sheppo 				}
41581ae08745Sheppo 
41593af08d82Slm66018 			}
4160e1ebb9ecSlm66018 
41613af08d82Slm66018 			mutex_enter(&vdcp->lock);
41623af08d82Slm66018 
41633af08d82Slm66018 			vdcp->state = VDC_STATE_RESETTING;
4164690555a1Sachartre 			vdcp->self_reset = B_TRUE;
41653af08d82Slm66018 			break;
41663af08d82Slm66018 
41673af08d82Slm66018 		case VDC_STATE_RESETTING:
4168655fd6a9Sachartre 			/*
4169655fd6a9Sachartre 			 * When we reach this state, we either come from the
4170655fd6a9Sachartre 			 * VDC_STATE_RUNNING state and we can have pending
4171655fd6a9Sachartre 			 * request but no timeout is armed; or we come from
4172655fd6a9Sachartre 			 * the VDC_STATE_INIT_WAITING, VDC_NEGOTIATE or
4173655fd6a9Sachartre 			 * VDC_HANDLE_PENDING state and there is no pending
4174655fd6a9Sachartre 			 * request or pending requests have already been copied
4175655fd6a9Sachartre 			 * into the backup dring. So we can safely keep the
4176655fd6a9Sachartre 			 * connection timeout armed while we are in this state.
4177655fd6a9Sachartre 			 */
4178655fd6a9Sachartre 
41793af08d82Slm66018 			DMSG(vdcp, 0, "Initiating channel reset "
41803af08d82Slm66018 			    "(pending = %d)\n", (int)vdcp->threads_pending);
41813af08d82Slm66018 
41823af08d82Slm66018 			if (vdcp->self_reset) {
41833af08d82Slm66018 				DMSG(vdcp, 0,
41843af08d82Slm66018 				    "[%d] calling stop_ldc_connection.\n",
41853af08d82Slm66018 				    vdcp->instance);
41863af08d82Slm66018 				status = vdc_stop_ldc_connection(vdcp);
41873af08d82Slm66018 				vdcp->self_reset = B_FALSE;
41881ae08745Sheppo 			}
41891ae08745Sheppo 
41901ae08745Sheppo 			/*
41913af08d82Slm66018 			 * Wait for all threads currently waiting
41923af08d82Slm66018 			 * for a free dring entry to use.
41931ae08745Sheppo 			 */
41943af08d82Slm66018 			while (vdcp->threads_pending) {
41953af08d82Slm66018 				cv_broadcast(&vdcp->membind_cv);
41963af08d82Slm66018 				cv_broadcast(&vdcp->dring_free_cv);
41973af08d82Slm66018 				mutex_exit(&vdcp->lock);
4198205eeb1aSlm66018 				/* give the waiters enough time to wake up */
4199205eeb1aSlm66018 				delay(vdc_hz_min_ldc_delay);
42003af08d82Slm66018 				mutex_enter(&vdcp->lock);
42011ae08745Sheppo 			}
42021ae08745Sheppo 
42033af08d82Slm66018 			ASSERT(vdcp->threads_pending == 0);
42041ae08745Sheppo 
42053af08d82Slm66018 			/* Sanity check that no thread is receiving */
42063af08d82Slm66018 			ASSERT(vdcp->read_state != VDC_READ_WAITING);
42070a55fbb7Slm66018 
42083af08d82Slm66018 			vdcp->read_state = VDC_READ_IDLE;
42093af08d82Slm66018 
42103af08d82Slm66018 			vdc_backup_local_dring(vdcp);
42113af08d82Slm66018 
42123af08d82Slm66018 			/* cleanup the old d-ring */
42133af08d82Slm66018 			vdc_destroy_descriptor_ring(vdcp);
42143af08d82Slm66018 
42153af08d82Slm66018 			/* go and start again */
42163af08d82Slm66018 			vdcp->state = VDC_STATE_INIT;
42173af08d82Slm66018 
42180a55fbb7Slm66018 			break;
42190a55fbb7Slm66018 
42203af08d82Slm66018 		case VDC_STATE_DETACH:
42213af08d82Slm66018 			DMSG(vdcp, 0, "[%d] Reset thread exit cleanup ..\n",
42223af08d82Slm66018 			    vdcp->instance);
42233af08d82Slm66018 
4224655fd6a9Sachartre 			/* cancel any pending timeout */
4225655fd6a9Sachartre 			mutex_exit(&vdcp->lock);
4226655fd6a9Sachartre 			if (tmid != 0) {
4227655fd6a9Sachartre 				(void) untimeout(tmid);
4228655fd6a9Sachartre 				tmid = 0;
4229655fd6a9Sachartre 			}
4230655fd6a9Sachartre 			mutex_enter(&vdcp->lock);
4231655fd6a9Sachartre 
42323c96341aSnarayan 			/*
42333c96341aSnarayan 			 * Signal anyone waiting for connection
42343c96341aSnarayan 			 * to come online
42353c96341aSnarayan 			 */
42363c96341aSnarayan 			cv_broadcast(&vdcp->running_cv);
42373c96341aSnarayan 
42383af08d82Slm66018 			while (vdcp->sync_op_pending) {
42393af08d82Slm66018 				cv_signal(&vdcp->sync_pending_cv);
42403af08d82Slm66018 				cv_signal(&vdcp->sync_blocked_cv);
42413af08d82Slm66018 				mutex_exit(&vdcp->lock);
4242205eeb1aSlm66018 				/* give the waiters enough time to wake up */
4243205eeb1aSlm66018 				delay(vdc_hz_min_ldc_delay);
42443af08d82Slm66018 				mutex_enter(&vdcp->lock);
42450a55fbb7Slm66018 			}
42461ae08745Sheppo 
42473af08d82Slm66018 			mutex_exit(&vdcp->lock);
42483af08d82Slm66018 
42493af08d82Slm66018 			DMSG(vdcp, 0, "[%d] Msg processing thread exiting ..\n",
42503af08d82Slm66018 			    vdcp->instance);
42513af08d82Slm66018 			thread_exit();
42523af08d82Slm66018 			break;
42533af08d82Slm66018 		}
42543af08d82Slm66018 	}
42550a55fbb7Slm66018 }
42560a55fbb7Slm66018 
42570a55fbb7Slm66018 
42580a55fbb7Slm66018 /*
42590a55fbb7Slm66018  * Function:
42600a55fbb7Slm66018  *	vdc_process_data_msg()
42610a55fbb7Slm66018  *
42620a55fbb7Slm66018  * Description:
42630a55fbb7Slm66018  *	This function is called by the message processing thread each time
42640a55fbb7Slm66018  *	a message with a msgtype of VIO_TYPE_DATA is received. It will either
42650a55fbb7Slm66018  *	be an ACK or NACK from vds[1] which vdc handles as follows.
42660a55fbb7Slm66018  *		ACK	- wake up the waiting thread
42670a55fbb7Slm66018  *		NACK	- resend any messages necessary
42680a55fbb7Slm66018  *
42690a55fbb7Slm66018  *	[1] Although the message format allows it, vds should not send a
42700a55fbb7Slm66018  *	    VIO_SUBTYPE_INFO message to vdc asking it to read data; if for
42710a55fbb7Slm66018  *	    some bizarre reason it does, vdc will reset the connection.
42720a55fbb7Slm66018  *
42730a55fbb7Slm66018  * Arguments:
42740a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
42750a55fbb7Slm66018  *	msg	- the LDC message sent by vds
42760a55fbb7Slm66018  *
42770a55fbb7Slm66018  * Return Code:
42780a55fbb7Slm66018  *	0	- Success.
42790a55fbb7Slm66018  *	> 0	- error value returned by LDC
42800a55fbb7Slm66018  */
42810a55fbb7Slm66018 static int
42823af08d82Slm66018 vdc_process_data_msg(vdc_t *vdcp, vio_msg_t *msg)
42830a55fbb7Slm66018 {
42840a55fbb7Slm66018 	int			status = 0;
42853af08d82Slm66018 	vio_dring_msg_t		*dring_msg;
4286d10e4ef2Snarayan 	vdc_local_desc_t	*ldep = NULL;
42873af08d82Slm66018 	int			start, end;
42883af08d82Slm66018 	int			idx;
42890a55fbb7Slm66018 
42903af08d82Slm66018 	dring_msg = (vio_dring_msg_t *)msg;
42910a55fbb7Slm66018 
42923af08d82Slm66018 	ASSERT(msg->tag.vio_msgtype == VIO_TYPE_DATA);
42933af08d82Slm66018 	ASSERT(vdcp != NULL);
42943af08d82Slm66018 
42953af08d82Slm66018 	mutex_enter(&vdcp->lock);
42960a55fbb7Slm66018 
42970a55fbb7Slm66018 	/*
42980a55fbb7Slm66018 	 * Check to see if the message has bogus data
42990a55fbb7Slm66018 	 */
4300e1ebb9ecSlm66018 	idx = start = dring_msg->start_idx;
43010a55fbb7Slm66018 	end = dring_msg->end_idx;
43023af08d82Slm66018 	if ((start >= vdcp->dring_len) ||
43033af08d82Slm66018 	    (end >= vdcp->dring_len) || (end < -1)) {
43043af08d82Slm66018 		DMSG(vdcp, 0, "[%d] Bogus ACK data : start %d, end %d\n",
43053af08d82Slm66018 		    vdcp->instance, start, end);
4306366a92acSlm66018 		VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
43073af08d82Slm66018 		mutex_exit(&vdcp->lock);
4308e1ebb9ecSlm66018 		return (EINVAL);
43090a55fbb7Slm66018 	}
43100a55fbb7Slm66018 
43110a55fbb7Slm66018 	/*
43120a55fbb7Slm66018 	 * Verify that the sequence number is what vdc expects.
43130a55fbb7Slm66018 	 */
43143af08d82Slm66018 	switch (vdc_verify_seq_num(vdcp, dring_msg)) {
4315e1ebb9ecSlm66018 	case VDC_SEQ_NUM_TODO:
4316e1ebb9ecSlm66018 		break;	/* keep processing this message */
4317e1ebb9ecSlm66018 	case VDC_SEQ_NUM_SKIP:
43183af08d82Slm66018 		mutex_exit(&vdcp->lock);
4319e1ebb9ecSlm66018 		return (0);
4320e1ebb9ecSlm66018 	case VDC_SEQ_NUM_INVALID:
43213af08d82Slm66018 		DMSG(vdcp, 0, "[%d] invalid seqno\n", vdcp->instance);
4322366a92acSlm66018 		VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
4323366a92acSlm66018 		mutex_exit(&vdcp->lock);
43240a55fbb7Slm66018 		return (ENXIO);
43250a55fbb7Slm66018 	}
43260a55fbb7Slm66018 
43273af08d82Slm66018 	if (msg->tag.vio_subtype == VIO_SUBTYPE_NACK) {
43283af08d82Slm66018 		DMSG(vdcp, 0, "[%d] DATA NACK\n", vdcp->instance);
4329e1ebb9ecSlm66018 		VDC_DUMP_DRING_MSG(dring_msg);
4330366a92acSlm66018 		VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
43313af08d82Slm66018 		mutex_exit(&vdcp->lock);
4332e1ebb9ecSlm66018 		return (EIO);
43330a55fbb7Slm66018 
43343af08d82Slm66018 	} else if (msg->tag.vio_subtype == VIO_SUBTYPE_INFO) {
4335366a92acSlm66018 		VD_UPDATE_ERR_STATS(vdcp, vd_protoerrs);
43363af08d82Slm66018 		mutex_exit(&vdcp->lock);
4337e1ebb9ecSlm66018 		return (EPROTO);
4338e1ebb9ecSlm66018 	}
4339e1ebb9ecSlm66018 
43403af08d82Slm66018 	DMSG(vdcp, 1, ": start %d end %d\n", start, end);
43413af08d82Slm66018 	ASSERT(start == end);
43423af08d82Slm66018 
43433af08d82Slm66018 	ldep = &vdcp->local_dring[idx];
43443af08d82Slm66018 
43453af08d82Slm66018 	DMSG(vdcp, 1, ": state 0x%x - cb_type 0x%x\n",
43463af08d82Slm66018 	    ldep->dep->hdr.dstate, ldep->cb_type);
43473af08d82Slm66018 
4348e1ebb9ecSlm66018 	if (ldep->dep->hdr.dstate == VIO_DESC_DONE) {
43493af08d82Slm66018 		struct buf *bufp;
4350e1ebb9ecSlm66018 
43513af08d82Slm66018 		switch (ldep->cb_type) {
43523af08d82Slm66018 		case CB_SYNC:
43533af08d82Slm66018 			ASSERT(vdcp->sync_op_pending);
4354d10e4ef2Snarayan 
43553af08d82Slm66018 			status = vdc_depopulate_descriptor(vdcp, idx);
43563af08d82Slm66018 			vdcp->sync_op_status = status;
43573af08d82Slm66018 			vdcp->sync_op_pending = B_FALSE;
43583af08d82Slm66018 			cv_signal(&vdcp->sync_pending_cv);
43593af08d82Slm66018 			break;
43604bac2208Snarayan 
43613af08d82Slm66018 		case CB_STRATEGY:
43623af08d82Slm66018 			bufp = ldep->cb_arg;
43633af08d82Slm66018 			ASSERT(bufp != NULL);
43643c96341aSnarayan 			bufp->b_resid =
43653c96341aSnarayan 			    bufp->b_bcount - ldep->dep->payload.nbytes;
43663af08d82Slm66018 			status = ldep->dep->payload.status; /* Future:ntoh */
43673af08d82Slm66018 			if (status != 0) {
43683af08d82Slm66018 				DMSG(vdcp, 1, "strategy status=%d\n", status);
4369366a92acSlm66018 				VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
43703af08d82Slm66018 				bioerror(bufp, status);
4371d10e4ef2Snarayan 			}
43722f5224aeSachartre 
43732f5224aeSachartre 			(void) vdc_depopulate_descriptor(vdcp, idx);
43743c96341aSnarayan 
43753c96341aSnarayan 			DMSG(vdcp, 1,
43763c96341aSnarayan 			    "strategy complete req=%ld bytes resp=%ld bytes\n",
43773c96341aSnarayan 			    bufp->b_bcount, ldep->dep->payload.nbytes);
43782f5224aeSachartre 
43792f5224aeSachartre 			if (status != 0 && vdcp->failfast_interval != 0) {
43802f5224aeSachartre 				/*
43812f5224aeSachartre 				 * The I/O has failed and failfast is enabled.
43822f5224aeSachartre 				 * We need the failfast thread to check if the
43832f5224aeSachartre 				 * failure is due to a reservation conflict.
43842f5224aeSachartre 				 */
43852f5224aeSachartre 				(void) vdc_failfast_io_queue(vdcp, bufp);
43862f5224aeSachartre 			} else {
4387366a92acSlm66018 				if (status == 0) {
4388366a92acSlm66018 					int op = (bufp->b_flags & B_READ) ?
4389366a92acSlm66018 					    VD_OP_BREAD : VD_OP_BWRITE;
4390366a92acSlm66018 					VD_UPDATE_IO_STATS(vdcp, op,
4391366a92acSlm66018 					    ldep->dep->payload.nbytes);
4392366a92acSlm66018 				}
4393366a92acSlm66018 				VD_KSTAT_RUNQ_EXIT(vdcp->io_stats);
4394366a92acSlm66018 				DTRACE_IO1(done, buf_t *, bufp);
43952f5224aeSachartre 				biodone(bufp);
43962f5224aeSachartre 			}
43973af08d82Slm66018 			break;
43983af08d82Slm66018 
43993af08d82Slm66018 		default:
44003af08d82Slm66018 			ASSERT(0);
44010a55fbb7Slm66018 		}
44023af08d82Slm66018 	}
44033af08d82Slm66018 
44043af08d82Slm66018 	/* let the arrival signal propogate */
44053af08d82Slm66018 	mutex_exit(&vdcp->lock);
44060a55fbb7Slm66018 
4407e1ebb9ecSlm66018 	/* probe gives the count of how many entries were processed */
4408366a92acSlm66018 	DTRACE_PROBE2(processed, int, 1, vdc_t *, vdcp);
44090a55fbb7Slm66018 
44103af08d82Slm66018 	return (0);
44110a55fbb7Slm66018 }
44120a55fbb7Slm66018 
44130a55fbb7Slm66018 
44140a55fbb7Slm66018 /*
44150a55fbb7Slm66018  * Function:
44160a55fbb7Slm66018  *	vdc_handle_ver_msg()
44170a55fbb7Slm66018  *
44180a55fbb7Slm66018  * Description:
44190a55fbb7Slm66018  *
44200a55fbb7Slm66018  * Arguments:
44210a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
44220a55fbb7Slm66018  *	ver_msg	- LDC message sent by vDisk server
44230a55fbb7Slm66018  *
44240a55fbb7Slm66018  * Return Code:
44250a55fbb7Slm66018  *	0	- Success
44260a55fbb7Slm66018  */
44270a55fbb7Slm66018 static int
44280a55fbb7Slm66018 vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg)
44290a55fbb7Slm66018 {
44300a55fbb7Slm66018 	int status = 0;
44310a55fbb7Slm66018 
44320a55fbb7Slm66018 	ASSERT(vdc != NULL);
44330a55fbb7Slm66018 	ASSERT(mutex_owned(&vdc->lock));
44340a55fbb7Slm66018 
44350a55fbb7Slm66018 	if (ver_msg->tag.vio_subtype_env != VIO_VER_INFO) {
44360a55fbb7Slm66018 		return (EPROTO);
44370a55fbb7Slm66018 	}
44380a55fbb7Slm66018 
44390a55fbb7Slm66018 	if (ver_msg->dev_class != VDEV_DISK_SERVER) {
44400a55fbb7Slm66018 		return (EINVAL);
44410a55fbb7Slm66018 	}
44420a55fbb7Slm66018 
44430a55fbb7Slm66018 	switch (ver_msg->tag.vio_subtype) {
44440a55fbb7Slm66018 	case VIO_SUBTYPE_ACK:
44450a55fbb7Slm66018 		/*
44460a55fbb7Slm66018 		 * We check to see if the version returned is indeed supported
44470a55fbb7Slm66018 		 * (The server may have also adjusted the minor number downwards
44480a55fbb7Slm66018 		 * and if so 'ver_msg' will contain the actual version agreed)
44490a55fbb7Slm66018 		 */
44500a55fbb7Slm66018 		if (vdc_is_supported_version(ver_msg)) {
44510a55fbb7Slm66018 			vdc->ver.major = ver_msg->ver_major;
44520a55fbb7Slm66018 			vdc->ver.minor = ver_msg->ver_minor;
44530a55fbb7Slm66018 			ASSERT(vdc->ver.major > 0);
44540a55fbb7Slm66018 		} else {
44550a55fbb7Slm66018 			status = EPROTO;
44560a55fbb7Slm66018 		}
44570a55fbb7Slm66018 		break;
44580a55fbb7Slm66018 
44590a55fbb7Slm66018 	case VIO_SUBTYPE_NACK:
44600a55fbb7Slm66018 		/*
44610a55fbb7Slm66018 		 * call vdc_is_supported_version() which will return the next
44620a55fbb7Slm66018 		 * supported version (if any) in 'ver_msg'
44630a55fbb7Slm66018 		 */
44640a55fbb7Slm66018 		(void) vdc_is_supported_version(ver_msg);
44650a55fbb7Slm66018 		if (ver_msg->ver_major > 0) {
44660a55fbb7Slm66018 			size_t len = sizeof (*ver_msg);
44670a55fbb7Slm66018 
44680a55fbb7Slm66018 			ASSERT(vdc->ver.major > 0);
44690a55fbb7Slm66018 
44700a55fbb7Slm66018 			/* reset the necessary fields and resend */
44710a55fbb7Slm66018 			ver_msg->tag.vio_subtype = VIO_SUBTYPE_INFO;
44720a55fbb7Slm66018 			ver_msg->dev_class = VDEV_DISK;
44730a55fbb7Slm66018 
44740a55fbb7Slm66018 			status = vdc_send(vdc, (caddr_t)ver_msg, &len);
44753af08d82Slm66018 			DMSG(vdc, 0, "[%d] Resend VER info (LDC status = %d)\n",
44760a55fbb7Slm66018 			    vdc->instance, status);
44770a55fbb7Slm66018 			if (len != sizeof (*ver_msg))
44780a55fbb7Slm66018 				status = EBADMSG;
44790a55fbb7Slm66018 		} else {
448087a7269eSachartre 			DMSG(vdc, 0, "[%d] No common version with vDisk server",
448187a7269eSachartre 			    vdc->instance);
44820a55fbb7Slm66018 			status = ENOTSUP;
44830a55fbb7Slm66018 		}
44840a55fbb7Slm66018 
44850a55fbb7Slm66018 		break;
44861ae08745Sheppo 	case VIO_SUBTYPE_INFO:
44871ae08745Sheppo 		/*
44881ae08745Sheppo 		 * Handle the case where vds starts handshake
4489eff7243fSlm66018 		 * (for now only vdc is the instigator)
44901ae08745Sheppo 		 */
44911ae08745Sheppo 		status = ENOTSUP;
44921ae08745Sheppo 		break;
44931ae08745Sheppo 
44941ae08745Sheppo 	default:
44950a55fbb7Slm66018 		status = EINVAL;
44961ae08745Sheppo 		break;
44971ae08745Sheppo 	}
44981ae08745Sheppo 
44990a55fbb7Slm66018 	return (status);
45000a55fbb7Slm66018 }
45010a55fbb7Slm66018 
45020a55fbb7Slm66018 /*
45030a55fbb7Slm66018  * Function:
45040a55fbb7Slm66018  *	vdc_handle_attr_msg()
45050a55fbb7Slm66018  *
45060a55fbb7Slm66018  * Description:
45070a55fbb7Slm66018  *
45080a55fbb7Slm66018  * Arguments:
45090a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
45100a55fbb7Slm66018  *	attr_msg	- LDC message sent by vDisk server
45110a55fbb7Slm66018  *
45120a55fbb7Slm66018  * Return Code:
45130a55fbb7Slm66018  *	0	- Success
45140a55fbb7Slm66018  */
45150a55fbb7Slm66018 static int
45160a55fbb7Slm66018 vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg)
45170a55fbb7Slm66018 {
45180a55fbb7Slm66018 	int status = 0;
45190a55fbb7Slm66018 
45200a55fbb7Slm66018 	ASSERT(vdc != NULL);
45210a55fbb7Slm66018 	ASSERT(mutex_owned(&vdc->lock));
45220a55fbb7Slm66018 
45230a55fbb7Slm66018 	if (attr_msg->tag.vio_subtype_env != VIO_ATTR_INFO) {
45240a55fbb7Slm66018 		return (EPROTO);
45250a55fbb7Slm66018 	}
45260a55fbb7Slm66018 
45270a55fbb7Slm66018 	switch (attr_msg->tag.vio_subtype) {
45281ae08745Sheppo 	case VIO_SUBTYPE_ACK:
45291ae08745Sheppo 		/*
45301ae08745Sheppo 		 * We now verify the attributes sent by vds.
45311ae08745Sheppo 		 */
453278fcd0a1Sachartre 		if (attr_msg->vdisk_size == 0) {
453378fcd0a1Sachartre 			DMSG(vdc, 0, "[%d] Invalid disk size from vds",
453478fcd0a1Sachartre 			    vdc->instance);
453578fcd0a1Sachartre 			status = EINVAL;
453678fcd0a1Sachartre 			break;
453778fcd0a1Sachartre 		}
453878fcd0a1Sachartre 
453978fcd0a1Sachartre 		if (attr_msg->max_xfer_sz == 0) {
454078fcd0a1Sachartre 			DMSG(vdc, 0, "[%d] Invalid transfer size from vds",
454178fcd0a1Sachartre 			    vdc->instance);
454278fcd0a1Sachartre 			status = EINVAL;
454378fcd0a1Sachartre 			break;
454478fcd0a1Sachartre 		}
454578fcd0a1Sachartre 
45462f5224aeSachartre 		if (attr_msg->vdisk_size == VD_SIZE_UNKNOWN) {
45472f5224aeSachartre 			DMSG(vdc, 0, "[%d] Unknown disk size from vds",
45482f5224aeSachartre 			    vdc->instance);
45492f5224aeSachartre 			attr_msg->vdisk_size = 0;
45502f5224aeSachartre 		}
45512f5224aeSachartre 
455278fcd0a1Sachartre 		/*
455378fcd0a1Sachartre 		 * If the disk size is already set check that it hasn't changed.
455478fcd0a1Sachartre 		 */
45552f5224aeSachartre 		if ((vdc->vdisk_size != 0) && (attr_msg->vdisk_size != 0) &&
455678fcd0a1Sachartre 		    (vdc->vdisk_size != attr_msg->vdisk_size)) {
455778fcd0a1Sachartre 			DMSG(vdc, 0, "[%d] Different disk size from vds "
455878fcd0a1Sachartre 			    "(old=0x%lx - new=0x%lx", vdc->instance,
455978fcd0a1Sachartre 			    vdc->vdisk_size, attr_msg->vdisk_size)
456078fcd0a1Sachartre 			status = EINVAL;
456178fcd0a1Sachartre 			break;
456278fcd0a1Sachartre 		}
456378fcd0a1Sachartre 
45641ae08745Sheppo 		vdc->vdisk_size = attr_msg->vdisk_size;
45651ae08745Sheppo 		vdc->vdisk_type = attr_msg->vdisk_type;
456617cadca8Slm66018 		vdc->operations = attr_msg->operations;
456717cadca8Slm66018 		if (vio_ver_is_supported(vdc->ver, 1, 1))
456817cadca8Slm66018 			vdc->vdisk_media = attr_msg->vdisk_media;
456917cadca8Slm66018 		else
457017cadca8Slm66018 			vdc->vdisk_media = 0;
45711ae08745Sheppo 
45723af08d82Slm66018 		DMSG(vdc, 0, "[%d] max_xfer_sz: sent %lx acked %lx\n",
4573e1ebb9ecSlm66018 		    vdc->instance, vdc->max_xfer_sz, attr_msg->max_xfer_sz);
45743af08d82Slm66018 		DMSG(vdc, 0, "[%d] vdisk_block_size: sent %lx acked %x\n",
4575e1ebb9ecSlm66018 		    vdc->instance, vdc->block_size,
4576e1ebb9ecSlm66018 		    attr_msg->vdisk_block_size);
4577e1ebb9ecSlm66018 
45781ae08745Sheppo 		/*
4579e1ebb9ecSlm66018 		 * We don't know at compile time what the vDisk server will
458017cadca8Slm66018 		 * think are good values but we apply a large (arbitrary)
4581e1ebb9ecSlm66018 		 * upper bound to prevent memory exhaustion in vdc if it was
4582e1ebb9ecSlm66018 		 * allocating a DRing based of huge values sent by the server.
4583e1ebb9ecSlm66018 		 * We probably will never exceed this except if the message
4584e1ebb9ecSlm66018 		 * was garbage.
45851ae08745Sheppo 		 */
4586e1ebb9ecSlm66018 		if ((attr_msg->max_xfer_sz * attr_msg->vdisk_block_size) <=
4587e1ebb9ecSlm66018 		    (PAGESIZE * DEV_BSIZE)) {
4588e1ebb9ecSlm66018 			vdc->max_xfer_sz = attr_msg->max_xfer_sz;
4589e1ebb9ecSlm66018 			vdc->block_size = attr_msg->vdisk_block_size;
4590e1ebb9ecSlm66018 		} else {
45913af08d82Slm66018 			DMSG(vdc, 0, "[%d] vds block transfer size too big;"
4592e1ebb9ecSlm66018 			    " using max supported by vdc", vdc->instance);
45931ae08745Sheppo 		}
45941ae08745Sheppo 
4595f0ca1d9aSsb155480 		if ((attr_msg->xfer_mode != VIO_DRING_MODE_V1_0) ||
45961ae08745Sheppo 		    (attr_msg->vdisk_size > INT64_MAX) ||
459717cadca8Slm66018 		    (attr_msg->operations == 0) ||
45981ae08745Sheppo 		    (attr_msg->vdisk_type > VD_DISK_TYPE_DISK)) {
45993af08d82Slm66018 			DMSG(vdc, 0, "[%d] Invalid attributes from vds",
4600e1ebb9ecSlm66018 			    vdc->instance);
46011ae08745Sheppo 			status = EINVAL;
46021ae08745Sheppo 			break;
46031ae08745Sheppo 		}
46041ae08745Sheppo 
460578fcd0a1Sachartre 		/*
460678fcd0a1Sachartre 		 * Now that we have received all attributes we can create a
460778fcd0a1Sachartre 		 * fake geometry for the disk.
460878fcd0a1Sachartre 		 */
460978fcd0a1Sachartre 		vdc_create_fake_geometry(vdc);
46101ae08745Sheppo 		break;
46111ae08745Sheppo 
46121ae08745Sheppo 	case VIO_SUBTYPE_NACK:
46131ae08745Sheppo 		/*
46141ae08745Sheppo 		 * vds could not handle the attributes we sent so we
46151ae08745Sheppo 		 * stop negotiating.
46161ae08745Sheppo 		 */
46171ae08745Sheppo 		status = EPROTO;
46181ae08745Sheppo 		break;
46191ae08745Sheppo 
46201ae08745Sheppo 	case VIO_SUBTYPE_INFO:
46211ae08745Sheppo 		/*
46221ae08745Sheppo 		 * Handle the case where vds starts the handshake
46231ae08745Sheppo 		 * (for now; vdc is the only supported instigatior)
46241ae08745Sheppo 		 */
46251ae08745Sheppo 		status = ENOTSUP;
46261ae08745Sheppo 		break;
46271ae08745Sheppo 
46281ae08745Sheppo 	default:
46291ae08745Sheppo 		status = ENOTSUP;
46301ae08745Sheppo 		break;
46311ae08745Sheppo 	}
46321ae08745Sheppo 
46330a55fbb7Slm66018 	return (status);
46341ae08745Sheppo }
46351ae08745Sheppo 
46360a55fbb7Slm66018 /*
46370a55fbb7Slm66018  * Function:
46380a55fbb7Slm66018  *	vdc_handle_dring_reg_msg()
46390a55fbb7Slm66018  *
46400a55fbb7Slm66018  * Description:
46410a55fbb7Slm66018  *
46420a55fbb7Slm66018  * Arguments:
46430a55fbb7Slm66018  *	vdc		- soft state pointer for this instance of the driver.
46440a55fbb7Slm66018  *	dring_msg	- LDC message sent by vDisk server
46450a55fbb7Slm66018  *
46460a55fbb7Slm66018  * Return Code:
46470a55fbb7Slm66018  *	0	- Success
46480a55fbb7Slm66018  */
46490a55fbb7Slm66018 static int
46500a55fbb7Slm66018 vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *dring_msg)
46510a55fbb7Slm66018 {
46520a55fbb7Slm66018 	int		status = 0;
46531ae08745Sheppo 
46540a55fbb7Slm66018 	ASSERT(vdc != NULL);
46550a55fbb7Slm66018 	ASSERT(mutex_owned(&vdc->lock));
46560a55fbb7Slm66018 
46570a55fbb7Slm66018 	if (dring_msg->tag.vio_subtype_env != VIO_DRING_REG) {
46580a55fbb7Slm66018 		return (EPROTO);
46590a55fbb7Slm66018 	}
46600a55fbb7Slm66018 
46610a55fbb7Slm66018 	switch (dring_msg->tag.vio_subtype) {
46620a55fbb7Slm66018 	case VIO_SUBTYPE_ACK:
46631ae08745Sheppo 		/* save the received dring_ident */
46641ae08745Sheppo 		vdc->dring_ident = dring_msg->dring_ident;
46653af08d82Slm66018 		DMSG(vdc, 0, "[%d] Received dring ident=0x%lx\n",
4666e1ebb9ecSlm66018 		    vdc->instance, vdc->dring_ident);
46671ae08745Sheppo 		break;
46681ae08745Sheppo 
46691ae08745Sheppo 	case VIO_SUBTYPE_NACK:
46701ae08745Sheppo 		/*
46711ae08745Sheppo 		 * vds could not handle the DRing info we sent so we
46721ae08745Sheppo 		 * stop negotiating.
46731ae08745Sheppo 		 */
46743af08d82Slm66018 		DMSG(vdc, 0, "[%d] server could not register DRing\n",
46753af08d82Slm66018 		    vdc->instance);
46761ae08745Sheppo 		status = EPROTO;
46771ae08745Sheppo 		break;
46781ae08745Sheppo 
46791ae08745Sheppo 	case VIO_SUBTYPE_INFO:
46801ae08745Sheppo 		/*
46811ae08745Sheppo 		 * Handle the case where vds starts handshake
46821ae08745Sheppo 		 * (for now only vdc is the instigatior)
46831ae08745Sheppo 		 */
46841ae08745Sheppo 		status = ENOTSUP;
46851ae08745Sheppo 		break;
46861ae08745Sheppo 	default:
46871ae08745Sheppo 		status = ENOTSUP;
46881ae08745Sheppo 	}
46891ae08745Sheppo 
46901ae08745Sheppo 	return (status);
46911ae08745Sheppo }
46921ae08745Sheppo 
46931ae08745Sheppo /*
46941ae08745Sheppo  * Function:
46951ae08745Sheppo  *	vdc_verify_seq_num()
46961ae08745Sheppo  *
46971ae08745Sheppo  * Description:
4698e1ebb9ecSlm66018  *	This functions verifies that the sequence number sent back by the vDisk
4699e1ebb9ecSlm66018  *	server with the latest message is what is expected (i.e. it is greater
4700e1ebb9ecSlm66018  *	than the last seq num sent by the vDisk server and less than or equal
4701e1ebb9ecSlm66018  *	to the last seq num generated by vdc).
4702e1ebb9ecSlm66018  *
4703e1ebb9ecSlm66018  *	It then checks the request ID to see if any requests need processing
4704e1ebb9ecSlm66018  *	in the DRing.
47051ae08745Sheppo  *
47061ae08745Sheppo  * Arguments:
47071ae08745Sheppo  *	vdc		- soft state pointer for this instance of the driver.
47081ae08745Sheppo  *	dring_msg	- pointer to the LDC message sent by vds
47091ae08745Sheppo  *
47101ae08745Sheppo  * Return Code:
4711e1ebb9ecSlm66018  *	VDC_SEQ_NUM_TODO	- Message needs to be processed
4712e1ebb9ecSlm66018  *	VDC_SEQ_NUM_SKIP	- Message has already been processed
4713e1ebb9ecSlm66018  *	VDC_SEQ_NUM_INVALID	- The seq numbers are so out of sync,
4714e1ebb9ecSlm66018  *				  vdc cannot deal with them
47151ae08745Sheppo  */
4716e1ebb9ecSlm66018 static int
4717e1ebb9ecSlm66018 vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg)
47181ae08745Sheppo {
47191ae08745Sheppo 	ASSERT(vdc != NULL);
47201ae08745Sheppo 	ASSERT(dring_msg != NULL);
4721d10e4ef2Snarayan 	ASSERT(mutex_owned(&vdc->lock));
47221ae08745Sheppo 
47231ae08745Sheppo 	/*
47241ae08745Sheppo 	 * Check to see if the messages were responded to in the correct
4725e1ebb9ecSlm66018 	 * order by vds.
47261ae08745Sheppo 	 */
4727e1ebb9ecSlm66018 	if ((dring_msg->seq_num <= vdc->seq_num_reply) ||
4728e1ebb9ecSlm66018 	    (dring_msg->seq_num > vdc->seq_num)) {
47293af08d82Slm66018 		DMSG(vdc, 0, "?[%d] Bogus sequence_number %lu: "
4730e1ebb9ecSlm66018 		    "%lu > expected <= %lu (last proc req %lu sent %lu)\n",
4731e1ebb9ecSlm66018 		    vdc->instance, dring_msg->seq_num,
4732e1ebb9ecSlm66018 		    vdc->seq_num_reply, vdc->seq_num,
4733e1ebb9ecSlm66018 		    vdc->req_id_proc, vdc->req_id);
4734e1ebb9ecSlm66018 		return (VDC_SEQ_NUM_INVALID);
47351ae08745Sheppo 	}
4736e1ebb9ecSlm66018 	vdc->seq_num_reply = dring_msg->seq_num;
47371ae08745Sheppo 
4738e1ebb9ecSlm66018 	if (vdc->req_id_proc < vdc->req_id)
4739e1ebb9ecSlm66018 		return (VDC_SEQ_NUM_TODO);
4740e1ebb9ecSlm66018 	else
4741e1ebb9ecSlm66018 		return (VDC_SEQ_NUM_SKIP);
47421ae08745Sheppo }
47431ae08745Sheppo 
47440a55fbb7Slm66018 
47450a55fbb7Slm66018 /*
47460a55fbb7Slm66018  * Function:
47470a55fbb7Slm66018  *	vdc_is_supported_version()
47480a55fbb7Slm66018  *
47490a55fbb7Slm66018  * Description:
47500a55fbb7Slm66018  *	This routine checks if the major/minor version numbers specified in
47510a55fbb7Slm66018  *	'ver_msg' are supported. If not it finds the next version that is
47520a55fbb7Slm66018  *	in the supported version list 'vdc_version[]' and sets the fields in
47530a55fbb7Slm66018  *	'ver_msg' to those values
47540a55fbb7Slm66018  *
47550a55fbb7Slm66018  * Arguments:
47560a55fbb7Slm66018  *	ver_msg	- LDC message sent by vDisk server
47570a55fbb7Slm66018  *
47580a55fbb7Slm66018  * Return Code:
47590a55fbb7Slm66018  *	B_TRUE	- Success
47600a55fbb7Slm66018  *	B_FALSE	- Version not supported
47610a55fbb7Slm66018  */
47620a55fbb7Slm66018 static boolean_t
47630a55fbb7Slm66018 vdc_is_supported_version(vio_ver_msg_t *ver_msg)
47640a55fbb7Slm66018 {
47650a55fbb7Slm66018 	int vdc_num_versions = sizeof (vdc_version) / sizeof (vdc_version[0]);
47660a55fbb7Slm66018 
47670a55fbb7Slm66018 	for (int i = 0; i < vdc_num_versions; i++) {
47680a55fbb7Slm66018 		ASSERT(vdc_version[i].major > 0);
47690a55fbb7Slm66018 		ASSERT((i == 0) ||
47700a55fbb7Slm66018 		    (vdc_version[i].major < vdc_version[i-1].major));
47710a55fbb7Slm66018 
47720a55fbb7Slm66018 		/*
47730a55fbb7Slm66018 		 * If the major versions match, adjust the minor version, if
47740a55fbb7Slm66018 		 * necessary, down to the highest value supported by this
47750a55fbb7Slm66018 		 * client. The server should support all minor versions lower
47760a55fbb7Slm66018 		 * than the value it sent
47770a55fbb7Slm66018 		 */
47780a55fbb7Slm66018 		if (ver_msg->ver_major == vdc_version[i].major) {
47790a55fbb7Slm66018 			if (ver_msg->ver_minor > vdc_version[i].minor) {
47803af08d82Slm66018 				DMSGX(0,
47813af08d82Slm66018 				    "Adjusting minor version from %u to %u",
47820a55fbb7Slm66018 				    ver_msg->ver_minor, vdc_version[i].minor);
47830a55fbb7Slm66018 				ver_msg->ver_minor = vdc_version[i].minor;
47840a55fbb7Slm66018 			}
47850a55fbb7Slm66018 			return (B_TRUE);
47860a55fbb7Slm66018 		}
47870a55fbb7Slm66018 
47880a55fbb7Slm66018 		/*
47890a55fbb7Slm66018 		 * If the message contains a higher major version number, set
47900a55fbb7Slm66018 		 * the message's major/minor versions to the current values
47910a55fbb7Slm66018 		 * and return false, so this message will get resent with
47920a55fbb7Slm66018 		 * these values, and the server will potentially try again
47930a55fbb7Slm66018 		 * with the same or a lower version
47940a55fbb7Slm66018 		 */
47950a55fbb7Slm66018 		if (ver_msg->ver_major > vdc_version[i].major) {
47960a55fbb7Slm66018 			ver_msg->ver_major = vdc_version[i].major;
47970a55fbb7Slm66018 			ver_msg->ver_minor = vdc_version[i].minor;
47983af08d82Slm66018 			DMSGX(0, "Suggesting major/minor (0x%x/0x%x)\n",
47990a55fbb7Slm66018 			    ver_msg->ver_major, ver_msg->ver_minor);
48000a55fbb7Slm66018 
48010a55fbb7Slm66018 			return (B_FALSE);
48020a55fbb7Slm66018 		}
48030a55fbb7Slm66018 
48040a55fbb7Slm66018 		/*
48050a55fbb7Slm66018 		 * Otherwise, the message's major version is less than the
48060a55fbb7Slm66018 		 * current major version, so continue the loop to the next
48070a55fbb7Slm66018 		 * (lower) supported version
48080a55fbb7Slm66018 		 */
48090a55fbb7Slm66018 	}
48100a55fbb7Slm66018 
48110a55fbb7Slm66018 	/*
48120a55fbb7Slm66018 	 * No common version was found; "ground" the version pair in the
48130a55fbb7Slm66018 	 * message to terminate negotiation
48140a55fbb7Slm66018 	 */
48150a55fbb7Slm66018 	ver_msg->ver_major = 0;
48160a55fbb7Slm66018 	ver_msg->ver_minor = 0;
48170a55fbb7Slm66018 
48180a55fbb7Slm66018 	return (B_FALSE);
48190a55fbb7Slm66018 }
48201ae08745Sheppo /* -------------------------------------------------------------------------- */
48211ae08745Sheppo 
48221ae08745Sheppo /*
48231ae08745Sheppo  * DKIO(7) support
48241ae08745Sheppo  */
48251ae08745Sheppo 
48261ae08745Sheppo typedef struct vdc_dk_arg {
48271ae08745Sheppo 	struct dk_callback	dkc;
48281ae08745Sheppo 	int			mode;
48291ae08745Sheppo 	dev_t			dev;
48301ae08745Sheppo 	vdc_t			*vdc;
48311ae08745Sheppo } vdc_dk_arg_t;
48321ae08745Sheppo 
48331ae08745Sheppo /*
48341ae08745Sheppo  * Function:
48351ae08745Sheppo  * 	vdc_dkio_flush_cb()
48361ae08745Sheppo  *
48371ae08745Sheppo  * Description:
48381ae08745Sheppo  *	This routine is a callback for DKIOCFLUSHWRITECACHE which can be called
48391ae08745Sheppo  *	by kernel code.
48401ae08745Sheppo  *
48411ae08745Sheppo  * Arguments:
48421ae08745Sheppo  *	arg	- a pointer to a vdc_dk_arg_t structure.
48431ae08745Sheppo  */
48441ae08745Sheppo void
48451ae08745Sheppo vdc_dkio_flush_cb(void *arg)
48461ae08745Sheppo {
48471ae08745Sheppo 	struct vdc_dk_arg	*dk_arg = (struct vdc_dk_arg *)arg;
48481ae08745Sheppo 	struct dk_callback	*dkc = NULL;
48491ae08745Sheppo 	vdc_t			*vdc = NULL;
48501ae08745Sheppo 	int			rv;
48511ae08745Sheppo 
48521ae08745Sheppo 	if (dk_arg == NULL) {
48533af08d82Slm66018 		cmn_err(CE_NOTE, "?[Unk] DKIOCFLUSHWRITECACHE arg is NULL\n");
48541ae08745Sheppo 		return;
48551ae08745Sheppo 	}
48561ae08745Sheppo 	dkc = &dk_arg->dkc;
48571ae08745Sheppo 	vdc = dk_arg->vdc;
48581ae08745Sheppo 	ASSERT(vdc != NULL);
48591ae08745Sheppo 
48603af08d82Slm66018 	rv = vdc_do_sync_op(vdc, VD_OP_FLUSH, NULL, 0,
48612f5224aeSachartre 	    VDCPART(dk_arg->dev), 0, CB_SYNC, 0, VIO_both_dir, B_TRUE);
48621ae08745Sheppo 	if (rv != 0) {
48633af08d82Slm66018 		DMSG(vdc, 0, "[%d] DKIOCFLUSHWRITECACHE failed %d : model %x\n",
4864e1ebb9ecSlm66018 		    vdc->instance, rv,
48651ae08745Sheppo 		    ddi_model_convert_from(dk_arg->mode & FMODELS));
48661ae08745Sheppo 	}
48671ae08745Sheppo 
48681ae08745Sheppo 	/*
48691ae08745Sheppo 	 * Trigger the call back to notify the caller the the ioctl call has
48701ae08745Sheppo 	 * been completed.
48711ae08745Sheppo 	 */
48721ae08745Sheppo 	if ((dk_arg->mode & FKIOCTL) &&
48731ae08745Sheppo 	    (dkc != NULL) &&
48741ae08745Sheppo 	    (dkc->dkc_callback != NULL)) {
48751ae08745Sheppo 		ASSERT(dkc->dkc_cookie != NULL);
48768e6a2a04Slm66018 		(*dkc->dkc_callback)(dkc->dkc_cookie, rv);
48771ae08745Sheppo 	}
48781ae08745Sheppo 
48791ae08745Sheppo 	/* Indicate that one less DKIO write flush is outstanding */
48801ae08745Sheppo 	mutex_enter(&vdc->lock);
48811ae08745Sheppo 	vdc->dkio_flush_pending--;
48821ae08745Sheppo 	ASSERT(vdc->dkio_flush_pending >= 0);
48831ae08745Sheppo 	mutex_exit(&vdc->lock);
48848e6a2a04Slm66018 
48858e6a2a04Slm66018 	/* free the mem that was allocated when the callback was dispatched */
48868e6a2a04Slm66018 	kmem_free(arg, sizeof (vdc_dk_arg_t));
48871ae08745Sheppo }
48881ae08745Sheppo 
48891ae08745Sheppo /*
489087a7269eSachartre  * Function:
4891*9642afceSachartre  * 	vdc_dkio_gapart()
489287a7269eSachartre  *
489387a7269eSachartre  * Description:
489487a7269eSachartre  *	This function implements the DKIOCGAPART ioctl.
489587a7269eSachartre  *
489687a7269eSachartre  * Arguments:
489778fcd0a1Sachartre  *	vdc	- soft state pointer
489887a7269eSachartre  *	arg	- a pointer to a dk_map[NDKMAP] or dk_map32[NDKMAP] structure
489987a7269eSachartre  *	flag	- ioctl flags
490087a7269eSachartre  */
490187a7269eSachartre static int
4902*9642afceSachartre vdc_dkio_gapart(vdc_t *vdc, caddr_t arg, int flag)
490387a7269eSachartre {
490478fcd0a1Sachartre 	struct dk_geom *geom;
490578fcd0a1Sachartre 	struct vtoc *vtoc;
490687a7269eSachartre 	union {
490787a7269eSachartre 		struct dk_map map[NDKMAP];
490887a7269eSachartre 		struct dk_map32 map32[NDKMAP];
490987a7269eSachartre 	} data;
491087a7269eSachartre 	int i, rv, size;
491187a7269eSachartre 
491278fcd0a1Sachartre 	mutex_enter(&vdc->lock);
491387a7269eSachartre 
491478fcd0a1Sachartre 	if ((rv = vdc_validate_geometry(vdc)) != 0) {
491578fcd0a1Sachartre 		mutex_exit(&vdc->lock);
491687a7269eSachartre 		return (rv);
491778fcd0a1Sachartre 	}
491887a7269eSachartre 
491978fcd0a1Sachartre 	vtoc = vdc->vtoc;
492078fcd0a1Sachartre 	geom = vdc->geom;
492187a7269eSachartre 
492287a7269eSachartre 	if (ddi_model_convert_from(flag & FMODELS) == DDI_MODEL_ILP32) {
492387a7269eSachartre 
492478fcd0a1Sachartre 		for (i = 0; i < vtoc->v_nparts; i++) {
492578fcd0a1Sachartre 			data.map32[i].dkl_cylno = vtoc->v_part[i].p_start /
492678fcd0a1Sachartre 			    (geom->dkg_nhead * geom->dkg_nsect);
492778fcd0a1Sachartre 			data.map32[i].dkl_nblk = vtoc->v_part[i].p_size;
492887a7269eSachartre 		}
492987a7269eSachartre 		size = NDKMAP * sizeof (struct dk_map32);
493087a7269eSachartre 
493187a7269eSachartre 	} else {
493287a7269eSachartre 
493378fcd0a1Sachartre 		for (i = 0; i < vtoc->v_nparts; i++) {
493478fcd0a1Sachartre 			data.map[i].dkl_cylno = vtoc->v_part[i].p_start /
493578fcd0a1Sachartre 			    (geom->dkg_nhead * geom->dkg_nsect);
493678fcd0a1Sachartre 			data.map[i].dkl_nblk = vtoc->v_part[i].p_size;
493787a7269eSachartre 		}
493887a7269eSachartre 		size = NDKMAP * sizeof (struct dk_map);
493987a7269eSachartre 
494087a7269eSachartre 	}
494187a7269eSachartre 
494278fcd0a1Sachartre 	mutex_exit(&vdc->lock);
494378fcd0a1Sachartre 
494487a7269eSachartre 	if (ddi_copyout(&data, arg, size, flag) != 0)
494587a7269eSachartre 		return (EFAULT);
494687a7269eSachartre 
494787a7269eSachartre 	return (0);
494887a7269eSachartre }
494987a7269eSachartre 
495087a7269eSachartre /*
495187a7269eSachartre  * Function:
4952*9642afceSachartre  * 	vdc_dkio_partition()
4953*9642afceSachartre  *
4954*9642afceSachartre  * Description:
4955*9642afceSachartre  *	This function implements the DKIOCPARTITION ioctl.
4956*9642afceSachartre  *
4957*9642afceSachartre  * Arguments:
4958*9642afceSachartre  *	vdc	- soft state pointer
4959*9642afceSachartre  *	arg	- a pointer to a struct partition64 structure
4960*9642afceSachartre  *	flag	- ioctl flags
4961*9642afceSachartre  */
4962*9642afceSachartre static int
4963*9642afceSachartre vdc_dkio_partition(vdc_t *vdc, caddr_t arg, int flag)
4964*9642afceSachartre {
4965*9642afceSachartre 	struct partition64 p64;
4966*9642afceSachartre 	efi_gpt_t *gpt;
4967*9642afceSachartre 	efi_gpe_t *gpe;
4968*9642afceSachartre 	vd_efi_dev_t edev;
4969*9642afceSachartre 	uint_t partno;
4970*9642afceSachartre 	int rv;
4971*9642afceSachartre 
4972*9642afceSachartre 	if (ddi_copyin(arg, &p64, sizeof (struct partition64), flag)) {
4973*9642afceSachartre 		return (EFAULT);
4974*9642afceSachartre 	}
4975*9642afceSachartre 
4976*9642afceSachartre 	VD_EFI_DEV_SET(edev, vdc, vd_process_efi_ioctl);
4977*9642afceSachartre 
4978*9642afceSachartre 	if ((rv = vd_efi_alloc_and_read(&edev, &gpt, &gpe)) != 0) {
4979*9642afceSachartre 		return (rv);
4980*9642afceSachartre 	}
4981*9642afceSachartre 
4982*9642afceSachartre 	partno = p64.p_partno;
4983*9642afceSachartre 
4984*9642afceSachartre 	if (partno >= gpt->efi_gpt_NumberOfPartitionEntries) {
4985*9642afceSachartre 		vd_efi_free(&edev, gpt, gpe);
4986*9642afceSachartre 		return (ESRCH);
4987*9642afceSachartre 	}
4988*9642afceSachartre 
4989*9642afceSachartre 	bcopy(&gpe[partno].efi_gpe_PartitionTypeGUID, &p64.p_type,
4990*9642afceSachartre 	    sizeof (struct uuid));
4991*9642afceSachartre 	p64.p_start = gpe[partno].efi_gpe_StartingLBA;
4992*9642afceSachartre 	p64.p_size = gpe[partno].efi_gpe_EndingLBA - p64.p_start + 1;
4993*9642afceSachartre 
4994*9642afceSachartre 	if (ddi_copyout(&p64, arg, sizeof (struct partition64), flag)) {
4995*9642afceSachartre 		vd_efi_free(&edev, gpt, gpe);
4996*9642afceSachartre 		return (EFAULT);
4997*9642afceSachartre 	}
4998*9642afceSachartre 
4999*9642afceSachartre 	vd_efi_free(&edev, gpt, gpe);
5000*9642afceSachartre 	return (0);
5001*9642afceSachartre }
5002*9642afceSachartre 
5003*9642afceSachartre /*
5004*9642afceSachartre  * Function:
500587a7269eSachartre  * 	vdc_dioctl_rwcmd()
500687a7269eSachartre  *
500787a7269eSachartre  * Description:
500887a7269eSachartre  *	This function implements the DIOCTL_RWCMD ioctl. This ioctl is used
500987a7269eSachartre  *	for DKC_DIRECT disks to read or write at an absolute disk offset.
501087a7269eSachartre  *
501187a7269eSachartre  * Arguments:
501287a7269eSachartre  *	dev	- device
501387a7269eSachartre  *	arg	- a pointer to a dadkio_rwcmd or dadkio_rwcmd32 structure
501487a7269eSachartre  *	flag	- ioctl flags
501587a7269eSachartre  */
501687a7269eSachartre static int
501787a7269eSachartre vdc_dioctl_rwcmd(dev_t dev, caddr_t arg, int flag)
501887a7269eSachartre {
501987a7269eSachartre 	struct dadkio_rwcmd32 rwcmd32;
502087a7269eSachartre 	struct dadkio_rwcmd rwcmd;
502187a7269eSachartre 	struct iovec aiov;
502287a7269eSachartre 	struct uio auio;
502387a7269eSachartre 	int rw, status;
502487a7269eSachartre 	struct buf *buf;
502587a7269eSachartre 
502687a7269eSachartre 	if (ddi_model_convert_from(flag & FMODELS) == DDI_MODEL_ILP32) {
502787a7269eSachartre 		if (ddi_copyin((caddr_t)arg, (caddr_t)&rwcmd32,
502887a7269eSachartre 		    sizeof (struct dadkio_rwcmd32), flag)) {
502987a7269eSachartre 			return (EFAULT);
503087a7269eSachartre 		}
503187a7269eSachartre 		rwcmd.cmd = rwcmd32.cmd;
503287a7269eSachartre 		rwcmd.flags = rwcmd32.flags;
503387a7269eSachartre 		rwcmd.blkaddr = (daddr_t)rwcmd32.blkaddr;
503487a7269eSachartre 		rwcmd.buflen = rwcmd32.buflen;
503587a7269eSachartre 		rwcmd.bufaddr = (caddr_t)(uintptr_t)rwcmd32.bufaddr;
503687a7269eSachartre 	} else {
503787a7269eSachartre 		if (ddi_copyin((caddr_t)arg, (caddr_t)&rwcmd,
503887a7269eSachartre 		    sizeof (struct dadkio_rwcmd), flag)) {
503987a7269eSachartre 			return (EFAULT);
504087a7269eSachartre 		}
504187a7269eSachartre 	}
504287a7269eSachartre 
504387a7269eSachartre 	switch (rwcmd.cmd) {
504487a7269eSachartre 	case DADKIO_RWCMD_READ:
504587a7269eSachartre 		rw = B_READ;
504687a7269eSachartre 		break;
504787a7269eSachartre 	case DADKIO_RWCMD_WRITE:
504887a7269eSachartre 		rw = B_WRITE;
504987a7269eSachartre 		break;
505087a7269eSachartre 	default:
505187a7269eSachartre 		return (EINVAL);
505287a7269eSachartre 	}
505387a7269eSachartre 
505487a7269eSachartre 	bzero((caddr_t)&aiov, sizeof (struct iovec));
505587a7269eSachartre 	aiov.iov_base   = rwcmd.bufaddr;
505687a7269eSachartre 	aiov.iov_len    = rwcmd.buflen;
505787a7269eSachartre 
505887a7269eSachartre 	bzero((caddr_t)&auio, sizeof (struct uio));
505987a7269eSachartre 	auio.uio_iov    = &aiov;
506087a7269eSachartre 	auio.uio_iovcnt = 1;
506187a7269eSachartre 	auio.uio_loffset = rwcmd.blkaddr * DEV_BSIZE;
506287a7269eSachartre 	auio.uio_resid  = rwcmd.buflen;
506387a7269eSachartre 	auio.uio_segflg = flag & FKIOCTL ? UIO_SYSSPACE : UIO_USERSPACE;
506487a7269eSachartre 
506587a7269eSachartre 	buf = kmem_alloc(sizeof (buf_t), KM_SLEEP);
506687a7269eSachartre 	bioinit(buf);
506787a7269eSachartre 	/*
506887a7269eSachartre 	 * We use the private field of buf to specify that this is an
506987a7269eSachartre 	 * I/O using an absolute offset.
507087a7269eSachartre 	 */
507187a7269eSachartre 	buf->b_private = (void *)VD_SLICE_NONE;
507287a7269eSachartre 
507387a7269eSachartre 	status = physio(vdc_strategy, buf, dev, rw, vdc_min, &auio);
507487a7269eSachartre 
507587a7269eSachartre 	biofini(buf);
507687a7269eSachartre 	kmem_free(buf, sizeof (buf_t));
507787a7269eSachartre 
507887a7269eSachartre 	return (status);
507987a7269eSachartre }
508087a7269eSachartre 
508187a7269eSachartre /*
50822f5224aeSachartre  * Allocate a buffer for a VD_OP_SCSICMD operation. The size of the allocated
50832f5224aeSachartre  * buffer is returned in alloc_len.
50842f5224aeSachartre  */
50852f5224aeSachartre static vd_scsi_t *
50862f5224aeSachartre vdc_scsi_alloc(int cdb_len, int sense_len, int datain_len, int dataout_len,
50872f5224aeSachartre     int *alloc_len)
50882f5224aeSachartre {
50892f5224aeSachartre 	vd_scsi_t *vd_scsi;
50902f5224aeSachartre 	int vd_scsi_len = VD_SCSI_SIZE;
50912f5224aeSachartre 
50922f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(cdb_len, sizeof (uint64_t));
50932f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(sense_len, sizeof (uint64_t));
50942f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(datain_len, sizeof (uint64_t));
50952f5224aeSachartre 	vd_scsi_len += P2ROUNDUP(dataout_len, sizeof (uint64_t));
50962f5224aeSachartre 
50972f5224aeSachartre 	ASSERT(vd_scsi_len % sizeof (uint64_t) == 0);
50982f5224aeSachartre 
50992f5224aeSachartre 	vd_scsi = kmem_zalloc(vd_scsi_len, KM_SLEEP);
51002f5224aeSachartre 
51012f5224aeSachartre 	vd_scsi->cdb_len = cdb_len;
51022f5224aeSachartre 	vd_scsi->sense_len = sense_len;
51032f5224aeSachartre 	vd_scsi->datain_len = datain_len;
51042f5224aeSachartre 	vd_scsi->dataout_len = dataout_len;
51052f5224aeSachartre 
51062f5224aeSachartre 	*alloc_len = vd_scsi_len;
51072f5224aeSachartre 
51082f5224aeSachartre 	return (vd_scsi);
51092f5224aeSachartre }
51102f5224aeSachartre 
51112f5224aeSachartre /*
51122f5224aeSachartre  * Convert the status of a SCSI command to a Solaris return code.
51132f5224aeSachartre  *
51142f5224aeSachartre  * Arguments:
51152f5224aeSachartre  *	vd_scsi		- The SCSI operation buffer.
51162f5224aeSachartre  *	log_error	- indicate if an error message should be logged.
51172f5224aeSachartre  *
51182f5224aeSachartre  * Note that our SCSI error messages are rather primitive for the moment
51192f5224aeSachartre  * and could be improved by decoding some data like the SCSI command and
51202f5224aeSachartre  * the sense key.
51212f5224aeSachartre  *
51222f5224aeSachartre  * Return value:
51232f5224aeSachartre  *	0		- Status is good.
51242f5224aeSachartre  *	EACCES		- Status reports a reservation conflict.
51252f5224aeSachartre  *	ENOTSUP		- Status reports a check condition and sense key
51262f5224aeSachartre  *			  reports an illegal request.
51272f5224aeSachartre  *	EIO		- Any other status.
51282f5224aeSachartre  */
51292f5224aeSachartre static int
51302f5224aeSachartre vdc_scsi_status(vdc_t *vdc, vd_scsi_t *vd_scsi, boolean_t log_error)
51312f5224aeSachartre {
51322f5224aeSachartre 	int rv;
51332f5224aeSachartre 	char path_str[MAXPATHLEN];
51342f5224aeSachartre 	char panic_str[VDC_RESV_CONFLICT_FMT_LEN + MAXPATHLEN];
51352f5224aeSachartre 	union scsi_cdb *cdb;
51362f5224aeSachartre 	struct scsi_extended_sense *sense;
51372f5224aeSachartre 
51382f5224aeSachartre 	if (vd_scsi->cmd_status == STATUS_GOOD)
51392f5224aeSachartre 		/* no error */
51402f5224aeSachartre 		return (0);
51412f5224aeSachartre 
51422f5224aeSachartre 	/* when the tunable vdc_scsi_log_error is true we log all errors */
51432f5224aeSachartre 	if (vdc_scsi_log_error)
51442f5224aeSachartre 		log_error = B_TRUE;
51452f5224aeSachartre 
51462f5224aeSachartre 	if (log_error) {
51472f5224aeSachartre 		cmn_err(CE_WARN, "%s (vdc%d):\tError for Command: 0x%x)\n",
51482f5224aeSachartre 		    ddi_pathname(vdc->dip, path_str), vdc->instance,
51492f5224aeSachartre 		    GETCMD(VD_SCSI_DATA_CDB(vd_scsi)));
51502f5224aeSachartre 	}
51512f5224aeSachartre 
51522f5224aeSachartre 	/* default returned value */
51532f5224aeSachartre 	rv = EIO;
51542f5224aeSachartre 
51552f5224aeSachartre 	switch (vd_scsi->cmd_status) {
51562f5224aeSachartre 
51572f5224aeSachartre 	case STATUS_CHECK:
51582f5224aeSachartre 	case STATUS_TERMINATED:
51592f5224aeSachartre 		if (log_error)
51602f5224aeSachartre 			cmn_err(CE_CONT, "\tCheck Condition Error\n");
51612f5224aeSachartre 
51622f5224aeSachartre 		/* check sense buffer */
51632f5224aeSachartre 		if (vd_scsi->sense_len == 0 ||
51642f5224aeSachartre 		    vd_scsi->sense_status != STATUS_GOOD) {
51652f5224aeSachartre 			if (log_error)
51662f5224aeSachartre 				cmn_err(CE_CONT, "\tNo Sense Data Available\n");
51672f5224aeSachartre 			break;
51682f5224aeSachartre 		}
51692f5224aeSachartre 
51702f5224aeSachartre 		sense = VD_SCSI_DATA_SENSE(vd_scsi);
51712f5224aeSachartre 
51722f5224aeSachartre 		if (log_error) {
51732f5224aeSachartre 			cmn_err(CE_CONT, "\tSense Key:  0x%x\n"
51742f5224aeSachartre 			    "\tASC: 0x%x, ASCQ: 0x%x\n",
51752f5224aeSachartre 			    scsi_sense_key((uint8_t *)sense),
51762f5224aeSachartre 			    scsi_sense_asc((uint8_t *)sense),
51772f5224aeSachartre 			    scsi_sense_ascq((uint8_t *)sense));
51782f5224aeSachartre 		}
51792f5224aeSachartre 
51802f5224aeSachartre 		if (scsi_sense_key((uint8_t *)sense) == KEY_ILLEGAL_REQUEST)
51812f5224aeSachartre 			rv = ENOTSUP;
51822f5224aeSachartre 		break;
51832f5224aeSachartre 
51842f5224aeSachartre 	case STATUS_BUSY:
51852f5224aeSachartre 		if (log_error)
51862f5224aeSachartre 			cmn_err(CE_NOTE, "\tDevice Busy\n");
51872f5224aeSachartre 		break;
51882f5224aeSachartre 
51892f5224aeSachartre 	case STATUS_RESERVATION_CONFLICT:
51902f5224aeSachartre 		/*
51912f5224aeSachartre 		 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then
51922f5224aeSachartre 		 * reservation conflict could be due to various reasons like
51932f5224aeSachartre 		 * incorrect keys, not registered or not reserved etc. So,
51942f5224aeSachartre 		 * we should not panic in that case.
51952f5224aeSachartre 		 */
51962f5224aeSachartre 		cdb = VD_SCSI_DATA_CDB(vd_scsi);
51972f5224aeSachartre 		if (vdc->failfast_interval != 0 &&
51982f5224aeSachartre 		    cdb->scc_cmd != SCMD_PERSISTENT_RESERVE_IN &&
51992f5224aeSachartre 		    cdb->scc_cmd != SCMD_PERSISTENT_RESERVE_OUT) {
52002f5224aeSachartre 			/* failfast is enabled so we have to panic */
52012f5224aeSachartre 			(void) snprintf(panic_str, sizeof (panic_str),
52022f5224aeSachartre 			    VDC_RESV_CONFLICT_FMT_STR "%s",
52032f5224aeSachartre 			    ddi_pathname(vdc->dip, path_str));
52042f5224aeSachartre 			panic(panic_str);
52052f5224aeSachartre 		}
52062f5224aeSachartre 		if (log_error)
52072f5224aeSachartre 			cmn_err(CE_NOTE, "\tReservation Conflict\n");
52082f5224aeSachartre 		rv = EACCES;
52092f5224aeSachartre 		break;
52102f5224aeSachartre 
52112f5224aeSachartre 	case STATUS_QFULL:
52122f5224aeSachartre 		if (log_error)
52132f5224aeSachartre 			cmn_err(CE_NOTE, "\tQueue Full\n");
52142f5224aeSachartre 		break;
52152f5224aeSachartre 
52162f5224aeSachartre 	case STATUS_MET:
52172f5224aeSachartre 	case STATUS_INTERMEDIATE:
52182f5224aeSachartre 	case STATUS_SCSI2:
52192f5224aeSachartre 	case STATUS_INTERMEDIATE_MET:
52202f5224aeSachartre 	case STATUS_ACA_ACTIVE:
52212f5224aeSachartre 		if (log_error)
52222f5224aeSachartre 			cmn_err(CE_CONT,
52232f5224aeSachartre 			    "\tUnexpected SCSI status received: 0x%x\n",
52242f5224aeSachartre 			    vd_scsi->cmd_status);
52252f5224aeSachartre 		break;
52262f5224aeSachartre 
52272f5224aeSachartre 	default:
52282f5224aeSachartre 		if (log_error)
52292f5224aeSachartre 			cmn_err(CE_CONT,
52302f5224aeSachartre 			    "\tInvalid SCSI status received: 0x%x\n",
52312f5224aeSachartre 			    vd_scsi->cmd_status);
52322f5224aeSachartre 		break;
52332f5224aeSachartre 	}
52342f5224aeSachartre 
52352f5224aeSachartre 	return (rv);
52362f5224aeSachartre }
52372f5224aeSachartre 
52382f5224aeSachartre /*
52392f5224aeSachartre  * Implemented the USCSICMD uscsi(7I) ioctl. This ioctl is converted to
52402f5224aeSachartre  * a VD_OP_SCSICMD operation which is sent to the vdisk server. If a SCSI
52412f5224aeSachartre  * reset is requested (i.e. a flag USCSI_RESET* is set) then the ioctl is
52422f5224aeSachartre  * converted to a VD_OP_RESET operation.
52432f5224aeSachartre  */
52442f5224aeSachartre static int
52452f5224aeSachartre vdc_uscsi_cmd(vdc_t *vdc, caddr_t arg, int mode)
52462f5224aeSachartre {
52472f5224aeSachartre 	struct uscsi_cmd 	uscsi;
52482f5224aeSachartre 	struct uscsi_cmd32	uscsi32;
52492f5224aeSachartre 	vd_scsi_t 		*vd_scsi;
52502f5224aeSachartre 	int 			vd_scsi_len;
52512f5224aeSachartre 	union scsi_cdb		*cdb;
52522f5224aeSachartre 	struct scsi_extended_sense *sense;
52532f5224aeSachartre 	char 			*datain, *dataout;
52542f5224aeSachartre 	size_t			cdb_len, datain_len, dataout_len, sense_len;
52552f5224aeSachartre 	int 			rv;
52562f5224aeSachartre 
52572f5224aeSachartre 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
52582f5224aeSachartre 		if (ddi_copyin(arg, &uscsi32, sizeof (struct uscsi_cmd32),
52592f5224aeSachartre 		    mode) != 0)
52602f5224aeSachartre 			return (EFAULT);
52612f5224aeSachartre 		uscsi_cmd32touscsi_cmd((&uscsi32), (&uscsi));
52622f5224aeSachartre 	} else {
52632f5224aeSachartre 		if (ddi_copyin(arg, &uscsi, sizeof (struct uscsi_cmd),
52642f5224aeSachartre 		    mode) != 0)
52652f5224aeSachartre 			return (EFAULT);
52662f5224aeSachartre 	}
52672f5224aeSachartre 
52682f5224aeSachartre 	/* a uscsi reset is converted to a VD_OP_RESET operation */
52692f5224aeSachartre 	if (uscsi.uscsi_flags & (USCSI_RESET | USCSI_RESET_LUN |
52702f5224aeSachartre 	    USCSI_RESET_ALL)) {
52712f5224aeSachartre 		rv = vdc_do_sync_op(vdc, VD_OP_RESET, NULL, 0, 0, 0, CB_SYNC,
52722f5224aeSachartre 		    (void *)(uint64_t)mode, VIO_both_dir, B_TRUE);
52732f5224aeSachartre 		return (rv);
52742f5224aeSachartre 	}
52752f5224aeSachartre 
52762f5224aeSachartre 	/* cdb buffer length */
52772f5224aeSachartre 	cdb_len = uscsi.uscsi_cdblen;
52782f5224aeSachartre 
52792f5224aeSachartre 	/* data in and out buffers length */
52802f5224aeSachartre 	if (uscsi.uscsi_flags & USCSI_READ) {
52812f5224aeSachartre 		datain_len = uscsi.uscsi_buflen;
52822f5224aeSachartre 		dataout_len = 0;
52832f5224aeSachartre 	} else {
52842f5224aeSachartre 		datain_len = 0;
52852f5224aeSachartre 		dataout_len = uscsi.uscsi_buflen;
52862f5224aeSachartre 	}
52872f5224aeSachartre 
52882f5224aeSachartre 	/* sense buffer length */
52892f5224aeSachartre 	if (uscsi.uscsi_flags & USCSI_RQENABLE)
52902f5224aeSachartre 		sense_len = uscsi.uscsi_rqlen;
52912f5224aeSachartre 	else
52922f5224aeSachartre 		sense_len = 0;
52932f5224aeSachartre 
52942f5224aeSachartre 	/* allocate buffer for the VD_SCSICMD_OP operation */
52952f5224aeSachartre 	vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, datain_len, dataout_len,
52962f5224aeSachartre 	    &vd_scsi_len);
52972f5224aeSachartre 
52982f5224aeSachartre 	/*
52992f5224aeSachartre 	 * The documentation of USCSI_ISOLATE and USCSI_DIAGNOSE is very vague,
53002f5224aeSachartre 	 * but basically they prevent a SCSI command from being retried in case
53012f5224aeSachartre 	 * of an error.
53022f5224aeSachartre 	 */
53032f5224aeSachartre 	if ((uscsi.uscsi_flags & USCSI_ISOLATE) ||
53042f5224aeSachartre 	    (uscsi.uscsi_flags & USCSI_DIAGNOSE))
53052f5224aeSachartre 		vd_scsi->options |= VD_SCSI_OPT_NORETRY;
53062f5224aeSachartre 
53072f5224aeSachartre 	/* set task attribute */
53082f5224aeSachartre 	if (uscsi.uscsi_flags & USCSI_NOTAG) {
53092f5224aeSachartre 		vd_scsi->task_attribute = 0;
53102f5224aeSachartre 	} else {
53112f5224aeSachartre 		if (uscsi.uscsi_flags & USCSI_HEAD)
53122f5224aeSachartre 			vd_scsi->task_attribute = VD_SCSI_TASK_ACA;
53132f5224aeSachartre 		else if (uscsi.uscsi_flags & USCSI_HTAG)
53142f5224aeSachartre 			vd_scsi->task_attribute = VD_SCSI_TASK_HQUEUE;
53152f5224aeSachartre 		else if (uscsi.uscsi_flags & USCSI_OTAG)
53162f5224aeSachartre 			vd_scsi->task_attribute = VD_SCSI_TASK_ORDERED;
53172f5224aeSachartre 		else
53182f5224aeSachartre 			vd_scsi->task_attribute = 0;
53192f5224aeSachartre 	}
53202f5224aeSachartre 
53212f5224aeSachartre 	/* set timeout */
53222f5224aeSachartre 	vd_scsi->timeout = uscsi.uscsi_timeout;
53232f5224aeSachartre 
53242f5224aeSachartre 	/* copy-in cdb data */
53252f5224aeSachartre 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
53262f5224aeSachartre 	if (ddi_copyin(uscsi.uscsi_cdb, cdb, cdb_len, mode) != 0) {
53272f5224aeSachartre 		rv = EFAULT;
53282f5224aeSachartre 		goto done;
53292f5224aeSachartre 	}
53302f5224aeSachartre 
53312f5224aeSachartre 	/* keep a pointer to the sense buffer */
53322f5224aeSachartre 	sense = VD_SCSI_DATA_SENSE(vd_scsi);
53332f5224aeSachartre 
53342f5224aeSachartre 	/* keep a pointer to the data-in buffer */
53352f5224aeSachartre 	datain = (char *)VD_SCSI_DATA_IN(vd_scsi);
53362f5224aeSachartre 
53372f5224aeSachartre 	/* copy-in request data to the data-out buffer */
53382f5224aeSachartre 	dataout = (char *)VD_SCSI_DATA_OUT(vd_scsi);
53392f5224aeSachartre 	if (!(uscsi.uscsi_flags & USCSI_READ)) {
53402f5224aeSachartre 		if (ddi_copyin(uscsi.uscsi_bufaddr, dataout, dataout_len,
53412f5224aeSachartre 		    mode)) {
53422f5224aeSachartre 			rv = EFAULT;
53432f5224aeSachartre 			goto done;
53442f5224aeSachartre 		}
53452f5224aeSachartre 	}
53462f5224aeSachartre 
53472f5224aeSachartre 	/* submit the request */
53482f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
53492f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
53502f5224aeSachartre 
53512f5224aeSachartre 	if (rv != 0)
53522f5224aeSachartre 		goto done;
53532f5224aeSachartre 
53542f5224aeSachartre 	/* update scsi status */
53552f5224aeSachartre 	uscsi.uscsi_status = vd_scsi->cmd_status;
53562f5224aeSachartre 
53572f5224aeSachartre 	/* update sense data */
53582f5224aeSachartre 	if ((uscsi.uscsi_flags & USCSI_RQENABLE) &&
53592f5224aeSachartre 	    (uscsi.uscsi_status == STATUS_CHECK ||
53602f5224aeSachartre 	    uscsi.uscsi_status == STATUS_TERMINATED)) {
53612f5224aeSachartre 
53622f5224aeSachartre 		uscsi.uscsi_rqstatus = vd_scsi->sense_status;
53632f5224aeSachartre 
53642f5224aeSachartre 		if (uscsi.uscsi_rqstatus == STATUS_GOOD) {
53652f5224aeSachartre 			uscsi.uscsi_rqresid = uscsi.uscsi_rqlen -
53662f5224aeSachartre 			    vd_scsi->sense_len;
53672f5224aeSachartre 			if (ddi_copyout(sense, uscsi.uscsi_rqbuf,
53682f5224aeSachartre 			    vd_scsi->sense_len, mode) != 0) {
53692f5224aeSachartre 				rv = EFAULT;
53702f5224aeSachartre 				goto done;
53712f5224aeSachartre 			}
53722f5224aeSachartre 		}
53732f5224aeSachartre 	}
53742f5224aeSachartre 
53752f5224aeSachartre 	/* update request data */
53762f5224aeSachartre 	if (uscsi.uscsi_status == STATUS_GOOD) {
53772f5224aeSachartre 		if (uscsi.uscsi_flags & USCSI_READ) {
53782f5224aeSachartre 			uscsi.uscsi_resid = uscsi.uscsi_buflen -
53792f5224aeSachartre 			    vd_scsi->datain_len;
53802f5224aeSachartre 			if (ddi_copyout(datain, uscsi.uscsi_bufaddr,
53812f5224aeSachartre 			    vd_scsi->datain_len, mode) != 0) {
53822f5224aeSachartre 				rv = EFAULT;
53832f5224aeSachartre 				goto done;
53842f5224aeSachartre 			}
53852f5224aeSachartre 		} else {
53862f5224aeSachartre 			uscsi.uscsi_resid = uscsi.uscsi_buflen -
53872f5224aeSachartre 			    vd_scsi->dataout_len;
53882f5224aeSachartre 		}
53892f5224aeSachartre 	}
53902f5224aeSachartre 
53912f5224aeSachartre 	/* copy-out result */
53922f5224aeSachartre 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
53932f5224aeSachartre 		uscsi_cmdtouscsi_cmd32((&uscsi), (&uscsi32));
53942f5224aeSachartre 		if (ddi_copyout(&uscsi32, arg, sizeof (struct uscsi_cmd32),
53952f5224aeSachartre 		    mode) != 0) {
53962f5224aeSachartre 			rv = EFAULT;
53972f5224aeSachartre 			goto done;
53982f5224aeSachartre 		}
53992f5224aeSachartre 	} else {
54002f5224aeSachartre 		if (ddi_copyout(&uscsi, arg, sizeof (struct uscsi_cmd),
54012f5224aeSachartre 		    mode) != 0) {
54022f5224aeSachartre 			rv = EFAULT;
54032f5224aeSachartre 			goto done;
54042f5224aeSachartre 		}
54052f5224aeSachartre 	}
54062f5224aeSachartre 
54072f5224aeSachartre 	/* get the return code from the SCSI command status */
54082f5224aeSachartre 	rv = vdc_scsi_status(vdc, vd_scsi,
54092f5224aeSachartre 	    !(uscsi.uscsi_flags & USCSI_SILENT));
54102f5224aeSachartre 
54112f5224aeSachartre done:
54122f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
54132f5224aeSachartre 	return (rv);
54142f5224aeSachartre }
54152f5224aeSachartre 
54162f5224aeSachartre /*
54172f5224aeSachartre  * Create a VD_OP_SCSICMD buffer for a SCSI PERSISTENT IN command.
54182f5224aeSachartre  *
54192f5224aeSachartre  * Arguments:
54202f5224aeSachartre  *	cmd		- SCSI PERSISTENT IN command
54212f5224aeSachartre  *	len		- length of the SCSI input buffer
54222f5224aeSachartre  *	vd_scsi_len	- return the length of the allocated buffer
54232f5224aeSachartre  *
54242f5224aeSachartre  * Returned Value:
54252f5224aeSachartre  *	a pointer to the allocated VD_OP_SCSICMD buffer.
54262f5224aeSachartre  */
54272f5224aeSachartre static vd_scsi_t *
54282f5224aeSachartre vdc_scsi_alloc_persistent_in(uchar_t cmd, int len, int *vd_scsi_len)
54292f5224aeSachartre {
54302f5224aeSachartre 	int cdb_len, sense_len, datain_len, dataout_len;
54312f5224aeSachartre 	vd_scsi_t *vd_scsi;
54322f5224aeSachartre 	union scsi_cdb *cdb;
54332f5224aeSachartre 
54342f5224aeSachartre 	cdb_len = CDB_GROUP1;
54352f5224aeSachartre 	sense_len = sizeof (struct scsi_extended_sense);
54362f5224aeSachartre 	datain_len = len;
54372f5224aeSachartre 	dataout_len = 0;
54382f5224aeSachartre 
54392f5224aeSachartre 	vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, datain_len, dataout_len,
54402f5224aeSachartre 	    vd_scsi_len);
54412f5224aeSachartre 
54422f5224aeSachartre 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
54432f5224aeSachartre 
54442f5224aeSachartre 	/* set cdb */
54452f5224aeSachartre 	cdb->scc_cmd = SCMD_PERSISTENT_RESERVE_IN;
54462f5224aeSachartre 	cdb->cdb_opaque[1] = cmd;
54472f5224aeSachartre 	FORMG1COUNT(cdb, datain_len);
54482f5224aeSachartre 
54492f5224aeSachartre 	vd_scsi->timeout = vdc_scsi_timeout;
54502f5224aeSachartre 
54512f5224aeSachartre 	return (vd_scsi);
54522f5224aeSachartre }
54532f5224aeSachartre 
54542f5224aeSachartre /*
54552f5224aeSachartre  * Create a VD_OP_SCSICMD buffer for a SCSI PERSISTENT OUT command.
54562f5224aeSachartre  *
54572f5224aeSachartre  * Arguments:
54582f5224aeSachartre  *	cmd		- SCSI PERSISTENT OUT command
54592f5224aeSachartre  *	len		- length of the SCSI output buffer
54602f5224aeSachartre  *	vd_scsi_len	- return the length of the allocated buffer
54612f5224aeSachartre  *
54622f5224aeSachartre  * Returned Code:
54632f5224aeSachartre  *	a pointer to the allocated VD_OP_SCSICMD buffer.
54642f5224aeSachartre  */
54652f5224aeSachartre static vd_scsi_t *
54662f5224aeSachartre vdc_scsi_alloc_persistent_out(uchar_t cmd, int len, int *vd_scsi_len)
54672f5224aeSachartre {
54682f5224aeSachartre 	int cdb_len, sense_len, datain_len, dataout_len;
54692f5224aeSachartre 	vd_scsi_t *vd_scsi;
54702f5224aeSachartre 	union scsi_cdb *cdb;
54712f5224aeSachartre 
54722f5224aeSachartre 	cdb_len = CDB_GROUP1;
54732f5224aeSachartre 	sense_len = sizeof (struct scsi_extended_sense);
54742f5224aeSachartre 	datain_len = 0;
54752f5224aeSachartre 	dataout_len = len;
54762f5224aeSachartre 
54772f5224aeSachartre 	vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, datain_len, dataout_len,
54782f5224aeSachartre 	    vd_scsi_len);
54792f5224aeSachartre 
54802f5224aeSachartre 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
54812f5224aeSachartre 
54822f5224aeSachartre 	/* set cdb */
54832f5224aeSachartre 	cdb->scc_cmd = SCMD_PERSISTENT_RESERVE_OUT;
54842f5224aeSachartre 	cdb->cdb_opaque[1] = cmd;
54852f5224aeSachartre 	FORMG1COUNT(cdb, dataout_len);
54862f5224aeSachartre 
54872f5224aeSachartre 	vd_scsi->timeout = vdc_scsi_timeout;
54882f5224aeSachartre 
54892f5224aeSachartre 	return (vd_scsi);
54902f5224aeSachartre }
54912f5224aeSachartre 
54922f5224aeSachartre /*
54932f5224aeSachartre  * Implement the MHIOCGRP_INKEYS mhd(7i) ioctl. The ioctl is converted
54942f5224aeSachartre  * to a SCSI PERSISTENT IN READ KEYS command which is sent to the vdisk
54952f5224aeSachartre  * server with a VD_OP_SCSICMD operation.
54962f5224aeSachartre  */
54972f5224aeSachartre static int
54982f5224aeSachartre vdc_mhd_inkeys(vdc_t *vdc, caddr_t arg, int mode)
54992f5224aeSachartre {
55002f5224aeSachartre 	vd_scsi_t *vd_scsi;
55012f5224aeSachartre 	mhioc_inkeys_t inkeys;
55022f5224aeSachartre 	mhioc_key_list_t klist;
55032f5224aeSachartre 	struct mhioc_inkeys32 inkeys32;
55042f5224aeSachartre 	struct mhioc_key_list32 klist32;
55052f5224aeSachartre 	sd_prin_readkeys_t *scsi_keys;
55062f5224aeSachartre 	void *user_keys;
55072f5224aeSachartre 	int vd_scsi_len;
55082f5224aeSachartre 	int listsize, listlen, rv;
55092f5224aeSachartre 
55102f5224aeSachartre 	/* copyin arguments */
55112f5224aeSachartre 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
55122f5224aeSachartre 		rv = ddi_copyin(arg, &inkeys32, sizeof (inkeys32), mode);
55132f5224aeSachartre 		if (rv != 0)
55142f5224aeSachartre 			return (EFAULT);
55152f5224aeSachartre 
55162f5224aeSachartre 		rv = ddi_copyin((caddr_t)(uintptr_t)inkeys32.li, &klist32,
55172f5224aeSachartre 		    sizeof (klist32), mode);
55182f5224aeSachartre 		if (rv != 0)
55192f5224aeSachartre 			return (EFAULT);
55202f5224aeSachartre 
55212f5224aeSachartre 		listsize = klist32.listsize;
55222f5224aeSachartre 	} else {
55232f5224aeSachartre 		rv = ddi_copyin(arg, &inkeys, sizeof (inkeys), mode);
55242f5224aeSachartre 		if (rv != 0)
55252f5224aeSachartre 			return (EFAULT);
55262f5224aeSachartre 
55272f5224aeSachartre 		rv = ddi_copyin(inkeys.li, &klist, sizeof (klist), mode);
55282f5224aeSachartre 		if (rv != 0)
55292f5224aeSachartre 			return (EFAULT);
55302f5224aeSachartre 
55312f5224aeSachartre 		listsize = klist.listsize;
55322f5224aeSachartre 	}
55332f5224aeSachartre 
55342f5224aeSachartre 	/* build SCSI VD_OP request */
55352f5224aeSachartre 	vd_scsi = vdc_scsi_alloc_persistent_in(SD_READ_KEYS,
55362f5224aeSachartre 	    sizeof (sd_prin_readkeys_t) - sizeof (caddr_t) +
55372f5224aeSachartre 	    (sizeof (mhioc_resv_key_t) * listsize), &vd_scsi_len);
55382f5224aeSachartre 
55392f5224aeSachartre 	scsi_keys = (sd_prin_readkeys_t *)VD_SCSI_DATA_IN(vd_scsi);
55402f5224aeSachartre 
55412f5224aeSachartre 	/* submit the request */
55422f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
55432f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
55442f5224aeSachartre 
55452f5224aeSachartre 	if (rv != 0)
55462f5224aeSachartre 		goto done;
55472f5224aeSachartre 
55482f5224aeSachartre 	listlen = scsi_keys->len / MHIOC_RESV_KEY_SIZE;
55492f5224aeSachartre 
55502f5224aeSachartre 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
55512f5224aeSachartre 		inkeys32.generation = scsi_keys->generation;
55522f5224aeSachartre 		rv = ddi_copyout(&inkeys32, arg, sizeof (inkeys32), mode);
55532f5224aeSachartre 		if (rv != 0) {
55542f5224aeSachartre 			rv = EFAULT;
55552f5224aeSachartre 			goto done;
55562f5224aeSachartre 		}
55572f5224aeSachartre 
55582f5224aeSachartre 		klist32.listlen = listlen;
55592f5224aeSachartre 		rv = ddi_copyout(&klist32, (caddr_t)(uintptr_t)inkeys32.li,
55602f5224aeSachartre 		    sizeof (klist32), mode);
55612f5224aeSachartre 		if (rv != 0) {
55622f5224aeSachartre 			rv = EFAULT;
55632f5224aeSachartre 			goto done;
55642f5224aeSachartre 		}
55652f5224aeSachartre 
55662f5224aeSachartre 		user_keys = (caddr_t)(uintptr_t)klist32.list;
55672f5224aeSachartre 	} else {
55682f5224aeSachartre 		inkeys.generation = scsi_keys->generation;
55692f5224aeSachartre 		rv = ddi_copyout(&inkeys, arg, sizeof (inkeys), mode);
55702f5224aeSachartre 		if (rv != 0) {
55712f5224aeSachartre 			rv = EFAULT;
55722f5224aeSachartre 			goto done;
55732f5224aeSachartre 		}
55742f5224aeSachartre 
55752f5224aeSachartre 		klist.listlen = listlen;
55762f5224aeSachartre 		rv = ddi_copyout(&klist, inkeys.li, sizeof (klist), mode);
55772f5224aeSachartre 		if (rv != 0) {
55782f5224aeSachartre 			rv = EFAULT;
55792f5224aeSachartre 			goto done;
55802f5224aeSachartre 		}
55812f5224aeSachartre 
55822f5224aeSachartre 		user_keys = klist.list;
55832f5224aeSachartre 	}
55842f5224aeSachartre 
55852f5224aeSachartre 	/* copy out keys */
55862f5224aeSachartre 	if (listlen > 0 && listsize > 0) {
55872f5224aeSachartre 		if (listsize < listlen)
55882f5224aeSachartre 			listlen = listsize;
55892f5224aeSachartre 		rv = ddi_copyout(&scsi_keys->keylist, user_keys,
55902f5224aeSachartre 		    listlen * MHIOC_RESV_KEY_SIZE, mode);
55912f5224aeSachartre 		if (rv != 0)
55922f5224aeSachartre 			rv = EFAULT;
55932f5224aeSachartre 	}
55942f5224aeSachartre 
55952f5224aeSachartre 	if (rv == 0)
55962f5224aeSachartre 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
55972f5224aeSachartre 
55982f5224aeSachartre done:
55992f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
56002f5224aeSachartre 
56012f5224aeSachartre 	return (rv);
56022f5224aeSachartre }
56032f5224aeSachartre 
56042f5224aeSachartre /*
56052f5224aeSachartre  * Implement the MHIOCGRP_INRESV mhd(7i) ioctl. The ioctl is converted
56062f5224aeSachartre  * to a SCSI PERSISTENT IN READ RESERVATION command which is sent to
56072f5224aeSachartre  * the vdisk server with a VD_OP_SCSICMD operation.
56082f5224aeSachartre  */
56092f5224aeSachartre static int
56102f5224aeSachartre vdc_mhd_inresv(vdc_t *vdc, caddr_t arg, int mode)
56112f5224aeSachartre {
56122f5224aeSachartre 	vd_scsi_t *vd_scsi;
56132f5224aeSachartre 	mhioc_inresvs_t inresv;
56142f5224aeSachartre 	mhioc_resv_desc_list_t rlist;
56152f5224aeSachartre 	struct mhioc_inresvs32 inresv32;
56162f5224aeSachartre 	struct mhioc_resv_desc_list32 rlist32;
56172f5224aeSachartre 	mhioc_resv_desc_t mhd_resv;
56182f5224aeSachartre 	sd_prin_readresv_t *scsi_resv;
56192f5224aeSachartre 	sd_readresv_desc_t *resv;
56202f5224aeSachartre 	mhioc_resv_desc_t *user_resv;
56212f5224aeSachartre 	int vd_scsi_len;
56222f5224aeSachartre 	int listsize, listlen, i, rv;
56232f5224aeSachartre 
56242f5224aeSachartre 	/* copyin arguments */
56252f5224aeSachartre 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
56262f5224aeSachartre 		rv = ddi_copyin(arg, &inresv32, sizeof (inresv32), mode);
56272f5224aeSachartre 		if (rv != 0)
56282f5224aeSachartre 			return (EFAULT);
56292f5224aeSachartre 
56302f5224aeSachartre 		rv = ddi_copyin((caddr_t)(uintptr_t)inresv32.li, &rlist32,
56312f5224aeSachartre 		    sizeof (rlist32), mode);
56322f5224aeSachartre 		if (rv != 0)
56332f5224aeSachartre 			return (EFAULT);
56342f5224aeSachartre 
56352f5224aeSachartre 		listsize = rlist32.listsize;
56362f5224aeSachartre 	} else {
56372f5224aeSachartre 		rv = ddi_copyin(arg, &inresv, sizeof (inresv), mode);
56382f5224aeSachartre 		if (rv != 0)
56392f5224aeSachartre 			return (EFAULT);
56402f5224aeSachartre 
56412f5224aeSachartre 		rv = ddi_copyin(inresv.li, &rlist, sizeof (rlist), mode);
56422f5224aeSachartre 		if (rv != 0)
56432f5224aeSachartre 			return (EFAULT);
56442f5224aeSachartre 
56452f5224aeSachartre 		listsize = rlist.listsize;
56462f5224aeSachartre 	}
56472f5224aeSachartre 
56482f5224aeSachartre 	/* build SCSI VD_OP request */
56492f5224aeSachartre 	vd_scsi = vdc_scsi_alloc_persistent_in(SD_READ_RESV,
56502f5224aeSachartre 	    sizeof (sd_prin_readresv_t) - sizeof (caddr_t) +
56512f5224aeSachartre 	    (SCSI3_RESV_DESC_LEN * listsize), &vd_scsi_len);
56522f5224aeSachartre 
56532f5224aeSachartre 	scsi_resv = (sd_prin_readresv_t *)VD_SCSI_DATA_IN(vd_scsi);
56542f5224aeSachartre 
56552f5224aeSachartre 	/* submit the request */
56562f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
56572f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
56582f5224aeSachartre 
56592f5224aeSachartre 	if (rv != 0)
56602f5224aeSachartre 		goto done;
56612f5224aeSachartre 
56622f5224aeSachartre 	listlen = scsi_resv->len / SCSI3_RESV_DESC_LEN;
56632f5224aeSachartre 
56642f5224aeSachartre 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
56652f5224aeSachartre 		inresv32.generation = scsi_resv->generation;
56662f5224aeSachartre 		rv = ddi_copyout(&inresv32, arg, sizeof (inresv32), mode);
56672f5224aeSachartre 		if (rv != 0) {
56682f5224aeSachartre 			rv = EFAULT;
56692f5224aeSachartre 			goto done;
56702f5224aeSachartre 		}
56712f5224aeSachartre 
56722f5224aeSachartre 		rlist32.listlen = listlen;
56732f5224aeSachartre 		rv = ddi_copyout(&rlist32, (caddr_t)(uintptr_t)inresv32.li,
56742f5224aeSachartre 		    sizeof (rlist32), mode);
56752f5224aeSachartre 		if (rv != 0) {
56762f5224aeSachartre 			rv = EFAULT;
56772f5224aeSachartre 			goto done;
56782f5224aeSachartre 		}
56792f5224aeSachartre 
56802f5224aeSachartre 		user_resv = (mhioc_resv_desc_t *)(uintptr_t)rlist32.list;
56812f5224aeSachartre 	} else {
56822f5224aeSachartre 		inresv.generation = scsi_resv->generation;
56832f5224aeSachartre 		rv = ddi_copyout(&inresv, arg, sizeof (inresv), mode);
56842f5224aeSachartre 		if (rv != 0) {
56852f5224aeSachartre 			rv = EFAULT;
56862f5224aeSachartre 			goto done;
56872f5224aeSachartre 		}
56882f5224aeSachartre 
56892f5224aeSachartre 		rlist.listlen = listlen;
56902f5224aeSachartre 		rv = ddi_copyout(&rlist, inresv.li, sizeof (rlist), mode);
56912f5224aeSachartre 		if (rv != 0) {
56922f5224aeSachartre 			rv = EFAULT;
56932f5224aeSachartre 			goto done;
56942f5224aeSachartre 		}
56952f5224aeSachartre 
56962f5224aeSachartre 		user_resv = rlist.list;
56972f5224aeSachartre 	}
56982f5224aeSachartre 
56992f5224aeSachartre 	/* copy out reservations */
57002f5224aeSachartre 	if (listsize > 0 && listlen > 0) {
57012f5224aeSachartre 		if (listsize < listlen)
57022f5224aeSachartre 			listlen = listsize;
57032f5224aeSachartre 		resv = (sd_readresv_desc_t *)&scsi_resv->readresv_desc;
57042f5224aeSachartre 
57052f5224aeSachartre 		for (i = 0; i < listlen; i++) {
57062f5224aeSachartre 			mhd_resv.type = resv->type;
57072f5224aeSachartre 			mhd_resv.scope = resv->scope;
57082f5224aeSachartre 			mhd_resv.scope_specific_addr =
57092f5224aeSachartre 			    BE_32(resv->scope_specific_addr);
57102f5224aeSachartre 			bcopy(&resv->resvkey, &mhd_resv.key,
57112f5224aeSachartre 			    MHIOC_RESV_KEY_SIZE);
57122f5224aeSachartre 
57132f5224aeSachartre 			rv = ddi_copyout(&mhd_resv, user_resv,
57142f5224aeSachartre 			    sizeof (mhd_resv), mode);
57152f5224aeSachartre 			if (rv != 0) {
57162f5224aeSachartre 				rv = EFAULT;
57172f5224aeSachartre 				goto done;
57182f5224aeSachartre 			}
57192f5224aeSachartre 			resv++;
57202f5224aeSachartre 			user_resv++;
57212f5224aeSachartre 		}
57222f5224aeSachartre 	}
57232f5224aeSachartre 
57242f5224aeSachartre 	if (rv == 0)
57252f5224aeSachartre 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
57262f5224aeSachartre 
57272f5224aeSachartre done:
57282f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
57292f5224aeSachartre 	return (rv);
57302f5224aeSachartre }
57312f5224aeSachartre 
57322f5224aeSachartre /*
57332f5224aeSachartre  * Implement the MHIOCGRP_REGISTER mhd(7i) ioctl. The ioctl is converted
57342f5224aeSachartre  * to a SCSI PERSISTENT OUT REGISTER command which is sent to the vdisk
57352f5224aeSachartre  * server with a VD_OP_SCSICMD operation.
57362f5224aeSachartre  */
57372f5224aeSachartre static int
57382f5224aeSachartre vdc_mhd_register(vdc_t *vdc, caddr_t arg, int mode)
57392f5224aeSachartre {
57402f5224aeSachartre 	vd_scsi_t *vd_scsi;
57412f5224aeSachartre 	sd_prout_t *scsi_prout;
57422f5224aeSachartre 	mhioc_register_t mhd_reg;
57432f5224aeSachartre 	int vd_scsi_len, rv;
57442f5224aeSachartre 
57452f5224aeSachartre 	/* copyin arguments */
57462f5224aeSachartre 	rv = ddi_copyin(arg, &mhd_reg, sizeof (mhd_reg), mode);
57472f5224aeSachartre 	if (rv != 0)
57482f5224aeSachartre 		return (EFAULT);
57492f5224aeSachartre 
57502f5224aeSachartre 	/* build SCSI VD_OP request */
57512f5224aeSachartre 	vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_REGISTER,
57522f5224aeSachartre 	    sizeof (sd_prout_t), &vd_scsi_len);
57532f5224aeSachartre 
57542f5224aeSachartre 	/* set parameters */
57552f5224aeSachartre 	scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi);
57562f5224aeSachartre 	bcopy(mhd_reg.oldkey.key, scsi_prout->res_key, MHIOC_RESV_KEY_SIZE);
57572f5224aeSachartre 	bcopy(mhd_reg.newkey.key, scsi_prout->service_key, MHIOC_RESV_KEY_SIZE);
57582f5224aeSachartre 	scsi_prout->aptpl = (uchar_t)mhd_reg.aptpl;
57592f5224aeSachartre 
57602f5224aeSachartre 	/* submit the request */
57612f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
57622f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
57632f5224aeSachartre 
57642f5224aeSachartre 	if (rv == 0)
57652f5224aeSachartre 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
57662f5224aeSachartre 
57672f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
57682f5224aeSachartre 	return (rv);
57692f5224aeSachartre }
57702f5224aeSachartre 
57712f5224aeSachartre /*
57722f5224aeSachartre  * Implement the MHIOCGRP_RESERVE mhd(7i) ioctl. The ioctl is converted
57732f5224aeSachartre  * to a SCSI PERSISTENT OUT RESERVE command which is sent to the vdisk
57742f5224aeSachartre  * server with a VD_OP_SCSICMD operation.
57752f5224aeSachartre  */
57762f5224aeSachartre static int
57772f5224aeSachartre vdc_mhd_reserve(vdc_t *vdc, caddr_t arg, int mode)
57782f5224aeSachartre {
57792f5224aeSachartre 	union scsi_cdb *cdb;
57802f5224aeSachartre 	vd_scsi_t *vd_scsi;
57812f5224aeSachartre 	sd_prout_t *scsi_prout;
57822f5224aeSachartre 	mhioc_resv_desc_t mhd_resv;
57832f5224aeSachartre 	int vd_scsi_len, rv;
57842f5224aeSachartre 
57852f5224aeSachartre 	/* copyin arguments */
57862f5224aeSachartre 	rv = ddi_copyin(arg, &mhd_resv, sizeof (mhd_resv), mode);
57872f5224aeSachartre 	if (rv != 0)
57882f5224aeSachartre 		return (EFAULT);
57892f5224aeSachartre 
57902f5224aeSachartre 	/* build SCSI VD_OP request */
57912f5224aeSachartre 	vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_RESERVE,
57922f5224aeSachartre 	    sizeof (sd_prout_t), &vd_scsi_len);
57932f5224aeSachartre 
57942f5224aeSachartre 	/* set parameters */
57952f5224aeSachartre 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
57962f5224aeSachartre 	scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi);
57972f5224aeSachartre 	bcopy(mhd_resv.key.key, scsi_prout->res_key, MHIOC_RESV_KEY_SIZE);
57982f5224aeSachartre 	scsi_prout->scope_address = mhd_resv.scope_specific_addr;
57992f5224aeSachartre 	cdb->cdb_opaque[2] = mhd_resv.type;
58002f5224aeSachartre 
58012f5224aeSachartre 	/* submit the request */
58022f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
58032f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
58042f5224aeSachartre 
58052f5224aeSachartre 	if (rv == 0)
58062f5224aeSachartre 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
58072f5224aeSachartre 
58082f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
58092f5224aeSachartre 	return (rv);
58102f5224aeSachartre }
58112f5224aeSachartre 
58122f5224aeSachartre /*
58132f5224aeSachartre  * Implement the MHIOCGRP_PREEMPTANDABORT mhd(7i) ioctl. The ioctl is
58142f5224aeSachartre  * converted to a SCSI PERSISTENT OUT PREEMPT AND ABORT command which
58152f5224aeSachartre  * is sent to the vdisk server with a VD_OP_SCSICMD operation.
58162f5224aeSachartre  */
58172f5224aeSachartre static int
58182f5224aeSachartre vdc_mhd_preemptabort(vdc_t *vdc, caddr_t arg, int mode)
58192f5224aeSachartre {
58202f5224aeSachartre 	union scsi_cdb *cdb;
58212f5224aeSachartre 	vd_scsi_t *vd_scsi;
58222f5224aeSachartre 	sd_prout_t *scsi_prout;
58232f5224aeSachartre 	mhioc_preemptandabort_t mhd_preempt;
58242f5224aeSachartre 	int vd_scsi_len, rv;
58252f5224aeSachartre 
58262f5224aeSachartre 	/* copyin arguments */
58272f5224aeSachartre 	rv = ddi_copyin(arg, &mhd_preempt, sizeof (mhd_preempt), mode);
58282f5224aeSachartre 	if (rv != 0)
58292f5224aeSachartre 		return (EFAULT);
58302f5224aeSachartre 
58312f5224aeSachartre 	/* build SCSI VD_OP request */
58322f5224aeSachartre 	vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_PREEMPTANDABORT,
58332f5224aeSachartre 	    sizeof (sd_prout_t), &vd_scsi_len);
58342f5224aeSachartre 
58352f5224aeSachartre 	/* set parameters */
58362f5224aeSachartre 	vd_scsi->task_attribute = VD_SCSI_TASK_ACA;
58372f5224aeSachartre 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
58382f5224aeSachartre 	scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi);
58392f5224aeSachartre 	bcopy(mhd_preempt.resvdesc.key.key, scsi_prout->res_key,
58402f5224aeSachartre 	    MHIOC_RESV_KEY_SIZE);
58412f5224aeSachartre 	bcopy(mhd_preempt.victim_key.key, scsi_prout->service_key,
58422f5224aeSachartre 	    MHIOC_RESV_KEY_SIZE);
58432f5224aeSachartre 	scsi_prout->scope_address = mhd_preempt.resvdesc.scope_specific_addr;
58442f5224aeSachartre 	cdb->cdb_opaque[2] = mhd_preempt.resvdesc.type;
58452f5224aeSachartre 
58462f5224aeSachartre 	/* submit the request */
58472f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
58482f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
58492f5224aeSachartre 
58502f5224aeSachartre 	if (rv == 0)
58512f5224aeSachartre 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
58522f5224aeSachartre 
58532f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
58542f5224aeSachartre 	return (rv);
58552f5224aeSachartre }
58562f5224aeSachartre 
58572f5224aeSachartre /*
58582f5224aeSachartre  * Implement the MHIOCGRP_REGISTERANDIGNOREKEY mhd(7i) ioctl. The ioctl
58592f5224aeSachartre  * is converted to a SCSI PERSISTENT OUT REGISTER AND IGNORE EXISTING KEY
58602f5224aeSachartre  * command which is sent to the vdisk server with a VD_OP_SCSICMD operation.
58612f5224aeSachartre  */
58622f5224aeSachartre static int
58632f5224aeSachartre vdc_mhd_registerignore(vdc_t *vdc, caddr_t arg, int mode)
58642f5224aeSachartre {
58652f5224aeSachartre 	vd_scsi_t *vd_scsi;
58662f5224aeSachartre 	sd_prout_t *scsi_prout;
58672f5224aeSachartre 	mhioc_registerandignorekey_t mhd_regi;
58682f5224aeSachartre 	int vd_scsi_len, rv;
58692f5224aeSachartre 
58702f5224aeSachartre 	/* copyin arguments */
58712f5224aeSachartre 	rv = ddi_copyin(arg, &mhd_regi, sizeof (mhd_regi), mode);
58722f5224aeSachartre 	if (rv != 0)
58732f5224aeSachartre 		return (EFAULT);
58742f5224aeSachartre 
58752f5224aeSachartre 	/* build SCSI VD_OP request */
58762f5224aeSachartre 	vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_REGISTERANDIGNOREKEY,
58772f5224aeSachartre 	    sizeof (sd_prout_t), &vd_scsi_len);
58782f5224aeSachartre 
58792f5224aeSachartre 	/* set parameters */
58802f5224aeSachartre 	scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi);
58812f5224aeSachartre 	bcopy(mhd_regi.newkey.key, scsi_prout->service_key,
58822f5224aeSachartre 	    MHIOC_RESV_KEY_SIZE);
58832f5224aeSachartre 	scsi_prout->aptpl = (uchar_t)mhd_regi.aptpl;
58842f5224aeSachartre 
58852f5224aeSachartre 	/* submit the request */
58862f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
58872f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)mode, VIO_both_dir, B_FALSE);
58882f5224aeSachartre 
58892f5224aeSachartre 	if (rv == 0)
58902f5224aeSachartre 		rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
58912f5224aeSachartre 
58922f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
58932f5224aeSachartre 	return (rv);
58942f5224aeSachartre }
58952f5224aeSachartre 
58962f5224aeSachartre /*
58972f5224aeSachartre  * This function is used by the failfast mechanism to send a SCSI command
58982f5224aeSachartre  * to check for reservation conflict.
58992f5224aeSachartre  */
59002f5224aeSachartre static int
59012f5224aeSachartre vdc_failfast_scsi_cmd(vdc_t *vdc, uchar_t scmd)
59022f5224aeSachartre {
59032f5224aeSachartre 	int cdb_len, sense_len, vd_scsi_len;
59042f5224aeSachartre 	vd_scsi_t *vd_scsi;
59052f5224aeSachartre 	union scsi_cdb *cdb;
59062f5224aeSachartre 	int rv;
59072f5224aeSachartre 
59082f5224aeSachartre 	ASSERT(scmd == SCMD_TEST_UNIT_READY || scmd == SCMD_WRITE_G1);
59092f5224aeSachartre 
59102f5224aeSachartre 	if (scmd == SCMD_WRITE_G1)
59112f5224aeSachartre 		cdb_len = CDB_GROUP1;
59122f5224aeSachartre 	else
59132f5224aeSachartre 		cdb_len = CDB_GROUP0;
59142f5224aeSachartre 
59152f5224aeSachartre 	sense_len = sizeof (struct scsi_extended_sense);
59162f5224aeSachartre 
59172f5224aeSachartre 	vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, 0, 0, &vd_scsi_len);
59182f5224aeSachartre 
59192f5224aeSachartre 	/* set cdb */
59202f5224aeSachartre 	cdb = VD_SCSI_DATA_CDB(vd_scsi);
59212f5224aeSachartre 	cdb->scc_cmd = scmd;
59222f5224aeSachartre 
59232f5224aeSachartre 	vd_scsi->timeout = vdc_scsi_timeout;
59242f5224aeSachartre 
59252f5224aeSachartre 	/*
59262f5224aeSachartre 	 * Submit the request. The last argument has to be B_FALSE so that
59272f5224aeSachartre 	 * vdc_do_sync_op does not loop checking for reservation conflict if
59282f5224aeSachartre 	 * the operation returns an error.
59292f5224aeSachartre 	 */
59302f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
59312f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)FKIOCTL, VIO_both_dir, B_FALSE);
59322f5224aeSachartre 
59332f5224aeSachartre 	if (rv == 0)
59342f5224aeSachartre 		(void) vdc_scsi_status(vdc, vd_scsi, B_FALSE);
59352f5224aeSachartre 
59362f5224aeSachartre 	kmem_free(vd_scsi, vd_scsi_len);
59372f5224aeSachartre 	return (rv);
59382f5224aeSachartre }
59392f5224aeSachartre 
59402f5224aeSachartre /*
59412f5224aeSachartre  * This function is used by the failfast mechanism to check for reservation
59422f5224aeSachartre  * conflict. It sends some SCSI commands which will fail with a reservation
59432f5224aeSachartre  * conflict error if the system does not have access to the disk and this
59442f5224aeSachartre  * will panic the system.
59452f5224aeSachartre  *
59462f5224aeSachartre  * Returned Code:
59472f5224aeSachartre  *	0	- disk is accessible without reservation conflict error
59482f5224aeSachartre  *	!= 0	- unable to check if disk is accessible
59492f5224aeSachartre  */
59502f5224aeSachartre int
59512f5224aeSachartre vdc_failfast_check_resv(vdc_t *vdc)
59522f5224aeSachartre {
59532f5224aeSachartre 	int failure = 0;
59542f5224aeSachartre 
59552f5224aeSachartre 	/*
59562f5224aeSachartre 	 * Send a TEST UNIT READY command. The command will panic
59572f5224aeSachartre 	 * the system if it fails with a reservation conflict.
59582f5224aeSachartre 	 */
59592f5224aeSachartre 	if (vdc_failfast_scsi_cmd(vdc, SCMD_TEST_UNIT_READY) != 0)
59602f5224aeSachartre 		failure++;
59612f5224aeSachartre 
59622f5224aeSachartre 	/*
59632f5224aeSachartre 	 * With SPC-3 compliant devices TEST UNIT READY will succeed on
59642f5224aeSachartre 	 * a reserved device, so we also do a WRITE(10) of zero byte in
59652f5224aeSachartre 	 * order to provoke a Reservation Conflict status on those newer
59662f5224aeSachartre 	 * devices.
59672f5224aeSachartre 	 */
59682f5224aeSachartre 	if (vdc_failfast_scsi_cmd(vdc, SCMD_WRITE_G1) != 0)
59692f5224aeSachartre 		failure++;
59702f5224aeSachartre 
59712f5224aeSachartre 	return (failure);
59722f5224aeSachartre }
59732f5224aeSachartre 
59742f5224aeSachartre /*
59752f5224aeSachartre  * Add a pending I/O to the failfast I/O queue. An I/O is added to this
59762f5224aeSachartre  * queue when it has failed and failfast is enabled. Then we have to check
59772f5224aeSachartre  * if it has failed because of a reservation conflict in which case we have
59782f5224aeSachartre  * to panic the system.
59792f5224aeSachartre  *
59802f5224aeSachartre  * Async I/O should be queued with their block I/O data transfer structure
59812f5224aeSachartre  * (buf). Sync I/O should be queued with buf = NULL.
59822f5224aeSachartre  */
59832f5224aeSachartre static vdc_io_t *
59842f5224aeSachartre vdc_failfast_io_queue(vdc_t *vdc, struct buf *buf)
59852f5224aeSachartre {
59862f5224aeSachartre 	vdc_io_t *vio;
59872f5224aeSachartre 
59882f5224aeSachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
59892f5224aeSachartre 
59902f5224aeSachartre 	vio = kmem_alloc(sizeof (vdc_io_t), KM_SLEEP);
59912f5224aeSachartre 	vio->vio_next = vdc->failfast_io_queue;
59922f5224aeSachartre 	vio->vio_buf = buf;
59932f5224aeSachartre 	vio->vio_qtime = ddi_get_lbolt();
59942f5224aeSachartre 
59952f5224aeSachartre 	vdc->failfast_io_queue = vio;
59962f5224aeSachartre 
59972f5224aeSachartre 	/* notify the failfast thread that a new I/O is queued */
59982f5224aeSachartre 	cv_signal(&vdc->failfast_cv);
59992f5224aeSachartre 
60002f5224aeSachartre 	return (vio);
60012f5224aeSachartre }
60022f5224aeSachartre 
60032f5224aeSachartre /*
60042f5224aeSachartre  * Remove and complete I/O in the failfast I/O queue which have been
60052f5224aeSachartre  * added after the indicated deadline. A deadline of 0 means that all
60062f5224aeSachartre  * I/O have to be unqueued and marked as completed.
60072f5224aeSachartre  */
60082f5224aeSachartre static void
60092f5224aeSachartre vdc_failfast_io_unqueue(vdc_t *vdc, clock_t deadline)
60102f5224aeSachartre {
60112f5224aeSachartre 	vdc_io_t *vio, *vio_tmp;
60122f5224aeSachartre 
60132f5224aeSachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
60142f5224aeSachartre 
60152f5224aeSachartre 	vio_tmp = NULL;
60162f5224aeSachartre 	vio = vdc->failfast_io_queue;
60172f5224aeSachartre 
60182f5224aeSachartre 	if (deadline != 0) {
60192f5224aeSachartre 		/*
60202f5224aeSachartre 		 * Skip any io queued after the deadline. The failfast
60212f5224aeSachartre 		 * I/O queue is ordered starting with the last I/O added
60222f5224aeSachartre 		 * to the queue.
60232f5224aeSachartre 		 */
60242f5224aeSachartre 		while (vio != NULL && vio->vio_qtime > deadline) {
60252f5224aeSachartre 			vio_tmp = vio;
60262f5224aeSachartre 			vio = vio->vio_next;
60272f5224aeSachartre 		}
60282f5224aeSachartre 	}
60292f5224aeSachartre 
60302f5224aeSachartre 	if (vio == NULL)
60312f5224aeSachartre 		/* nothing to unqueue */
60322f5224aeSachartre 		return;
60332f5224aeSachartre 
60342f5224aeSachartre 	/* update the queue */
60352f5224aeSachartre 	if (vio_tmp == NULL)
60362f5224aeSachartre 		vdc->failfast_io_queue = NULL;
60372f5224aeSachartre 	else
60382f5224aeSachartre 		vio_tmp->vio_next = NULL;
60392f5224aeSachartre 
60402f5224aeSachartre 	/*
60412f5224aeSachartre 	 * Complete unqueued I/O. Async I/O have a block I/O data transfer
60422f5224aeSachartre 	 * structure (buf) and they are completed by calling biodone(). Sync
60432f5224aeSachartre 	 * I/O do not have a buf and they are completed by setting the
60442f5224aeSachartre 	 * vio_qtime to zero and signaling failfast_io_cv. In that case, the
60452f5224aeSachartre 	 * thread waiting for the I/O to complete is responsible for freeing
60462f5224aeSachartre 	 * the vio structure.
60472f5224aeSachartre 	 */
60482f5224aeSachartre 	while (vio != NULL) {
60492f5224aeSachartre 		vio_tmp = vio->vio_next;
60502f5224aeSachartre 		if (vio->vio_buf != NULL) {
6051366a92acSlm66018 			VD_KSTAT_RUNQ_EXIT(vdc->io_stats);
6052366a92acSlm66018 			DTRACE_IO1(done, buf_t *, vio->vio_buf);
60532f5224aeSachartre 			biodone(vio->vio_buf);
60542f5224aeSachartre 			kmem_free(vio, sizeof (vdc_io_t));
60552f5224aeSachartre 		} else {
60562f5224aeSachartre 			vio->vio_qtime = 0;
60572f5224aeSachartre 		}
60582f5224aeSachartre 		vio = vio_tmp;
60592f5224aeSachartre 	}
60602f5224aeSachartre 
60612f5224aeSachartre 	cv_broadcast(&vdc->failfast_io_cv);
60622f5224aeSachartre }
60632f5224aeSachartre 
60642f5224aeSachartre /*
60652f5224aeSachartre  * Failfast Thread.
60662f5224aeSachartre  *
60672f5224aeSachartre  * While failfast is enabled, the failfast thread sends a TEST UNIT READY
60682f5224aeSachartre  * and a zero size WRITE(10) SCSI commands on a regular basis to check that
60692f5224aeSachartre  * we still have access to the disk. If a command fails with a RESERVATION
60702f5224aeSachartre  * CONFLICT error then the system will immediatly panic.
60712f5224aeSachartre  *
60722f5224aeSachartre  * The failfast thread is also woken up when an I/O has failed. It then check
60732f5224aeSachartre  * the access to the disk to ensure that the I/O failure was not due to a
60742f5224aeSachartre  * reservation conflict.
60752f5224aeSachartre  *
60762f5224aeSachartre  * There is one failfast thread for each virtual disk for which failfast is
60772f5224aeSachartre  * enabled. We could have only one thread sending requests for all disks but
60782f5224aeSachartre  * this would need vdc to send asynchronous requests and to have callbacks to
60792f5224aeSachartre  * process replies.
60802f5224aeSachartre  */
60812f5224aeSachartre static void
60822f5224aeSachartre vdc_failfast_thread(void *arg)
60832f5224aeSachartre {
60842f5224aeSachartre 	int status;
60852f5224aeSachartre 	vdc_t *vdc = (vdc_t *)arg;
60862f5224aeSachartre 	clock_t timeout, starttime;
60872f5224aeSachartre 
60882f5224aeSachartre 	mutex_enter(&vdc->lock);
60892f5224aeSachartre 
60902f5224aeSachartre 	while (vdc->failfast_interval != 0) {
60912f5224aeSachartre 
60922f5224aeSachartre 		starttime = ddi_get_lbolt();
60932f5224aeSachartre 
60942f5224aeSachartre 		mutex_exit(&vdc->lock);
60952f5224aeSachartre 
60962f5224aeSachartre 		/* check for reservation conflict */
60972f5224aeSachartre 		status = vdc_failfast_check_resv(vdc);
60982f5224aeSachartre 
60992f5224aeSachartre 		mutex_enter(&vdc->lock);
61002f5224aeSachartre 		/*
61012f5224aeSachartre 		 * We have dropped the lock to send the SCSI command so we have
61022f5224aeSachartre 		 * to check that failfast is still enabled.
61032f5224aeSachartre 		 */
61042f5224aeSachartre 		if (vdc->failfast_interval == 0)
61052f5224aeSachartre 			break;
61062f5224aeSachartre 
61072f5224aeSachartre 		/*
61082f5224aeSachartre 		 * If we have successfully check the disk access and there was
61092f5224aeSachartre 		 * no reservation conflict then we can complete any I/O queued
61102f5224aeSachartre 		 * before the last check.
61112f5224aeSachartre 		 */
61122f5224aeSachartre 		if (status == 0)
61132f5224aeSachartre 			vdc_failfast_io_unqueue(vdc, starttime);
61142f5224aeSachartre 
61152f5224aeSachartre 		/* proceed again if some I/O are still in the queue */
61162f5224aeSachartre 		if (vdc->failfast_io_queue != NULL)
61172f5224aeSachartre 			continue;
61182f5224aeSachartre 
61192f5224aeSachartre 		timeout = ddi_get_lbolt() +
61202f5224aeSachartre 		    drv_usectohz(vdc->failfast_interval);
61212f5224aeSachartre 		(void) cv_timedwait(&vdc->failfast_cv, &vdc->lock, timeout);
61222f5224aeSachartre 	}
61232f5224aeSachartre 
61242f5224aeSachartre 	/*
61252f5224aeSachartre 	 * Failfast is being stop so we can complete any queued I/O.
61262f5224aeSachartre 	 */
61272f5224aeSachartre 	vdc_failfast_io_unqueue(vdc, 0);
61282f5224aeSachartre 	vdc->failfast_thread = NULL;
61292f5224aeSachartre 	mutex_exit(&vdc->lock);
61302f5224aeSachartre 	thread_exit();
61312f5224aeSachartre }
61322f5224aeSachartre 
61332f5224aeSachartre /*
61342f5224aeSachartre  * Implement the MHIOCENFAILFAST mhd(7i) ioctl.
61352f5224aeSachartre  */
61362f5224aeSachartre static int
61372f5224aeSachartre vdc_failfast(vdc_t *vdc, caddr_t arg, int mode)
61382f5224aeSachartre {
61392f5224aeSachartre 	unsigned int mh_time;
61402f5224aeSachartre 
61412f5224aeSachartre 	if (ddi_copyin((void *)arg, &mh_time, sizeof (int), mode))
61422f5224aeSachartre 		return (EFAULT);
61432f5224aeSachartre 
61442f5224aeSachartre 	mutex_enter(&vdc->lock);
61452f5224aeSachartre 	if (mh_time != 0 && vdc->failfast_thread == NULL) {
61462f5224aeSachartre 		vdc->failfast_thread = thread_create(NULL, 0,
61472f5224aeSachartre 		    vdc_failfast_thread, vdc, 0, &p0, TS_RUN,
61482f5224aeSachartre 		    v.v_maxsyspri - 2);
61492f5224aeSachartre 	}
61502f5224aeSachartre 
61512f5224aeSachartre 	vdc->failfast_interval = mh_time * 1000;
61522f5224aeSachartre 	cv_signal(&vdc->failfast_cv);
61532f5224aeSachartre 	mutex_exit(&vdc->lock);
61542f5224aeSachartre 
61552f5224aeSachartre 	return (0);
61562f5224aeSachartre }
61572f5224aeSachartre 
61582f5224aeSachartre /*
61592f5224aeSachartre  * Implement the MHIOCTKOWN and MHIOCRELEASE mhd(7i) ioctls. These ioctls are
61602f5224aeSachartre  * converted to VD_OP_SET_ACCESS operations.
61612f5224aeSachartre  */
61622f5224aeSachartre static int
61632f5224aeSachartre vdc_access_set(vdc_t *vdc, uint64_t flags, int mode)
61642f5224aeSachartre {
61652f5224aeSachartre 	int rv;
61662f5224aeSachartre 
61672f5224aeSachartre 	/* submit owership command request */
61682f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_SET_ACCESS, (caddr_t)&flags,
61692f5224aeSachartre 	    sizeof (uint64_t), 0, 0, CB_SYNC, (void *)(uint64_t)mode,
61702f5224aeSachartre 	    VIO_both_dir, B_TRUE);
61712f5224aeSachartre 
61722f5224aeSachartre 	return (rv);
61732f5224aeSachartre }
61742f5224aeSachartre 
61752f5224aeSachartre /*
61762f5224aeSachartre  * Implement the MHIOCSTATUS mhd(7i) ioctl. This ioctl is converted to a
61772f5224aeSachartre  * VD_OP_GET_ACCESS operation.
61782f5224aeSachartre  */
61792f5224aeSachartre static int
61802f5224aeSachartre vdc_access_get(vdc_t *vdc, uint64_t *status, int mode)
61812f5224aeSachartre {
61822f5224aeSachartre 	int rv;
61832f5224aeSachartre 
61842f5224aeSachartre 	/* submit owership command request */
61852f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_GET_ACCESS, (caddr_t)status,
61862f5224aeSachartre 	    sizeof (uint64_t), 0, 0, CB_SYNC, (void *)(uint64_t)mode,
61872f5224aeSachartre 	    VIO_both_dir, B_TRUE);
61882f5224aeSachartre 
61892f5224aeSachartre 	return (rv);
61902f5224aeSachartre }
61912f5224aeSachartre 
61922f5224aeSachartre /*
61932f5224aeSachartre  * Disk Ownership Thread.
61942f5224aeSachartre  *
61952f5224aeSachartre  * When we have taken the ownership of a disk, this thread waits to be
61962f5224aeSachartre  * notified when the LDC channel is reset so that it can recover the
61972f5224aeSachartre  * ownership.
61982f5224aeSachartre  *
61992f5224aeSachartre  * Note that the thread handling the LDC reset (vdc_process_msg_thread())
62002f5224aeSachartre  * can not be used to do the ownership recovery because it has to be
62012f5224aeSachartre  * running to handle the reply message to the ownership operation.
62022f5224aeSachartre  */
62032f5224aeSachartre static void
62042f5224aeSachartre vdc_ownership_thread(void *arg)
62052f5224aeSachartre {
62062f5224aeSachartre 	vdc_t *vdc = (vdc_t *)arg;
62072f5224aeSachartre 	clock_t timeout;
62082f5224aeSachartre 	uint64_t status;
62092f5224aeSachartre 
62102f5224aeSachartre 	mutex_enter(&vdc->ownership_lock);
62112f5224aeSachartre 	mutex_enter(&vdc->lock);
62122f5224aeSachartre 
62132f5224aeSachartre 	while (vdc->ownership & VDC_OWNERSHIP_WANTED) {
62142f5224aeSachartre 
62152f5224aeSachartre 		if ((vdc->ownership & VDC_OWNERSHIP_RESET) ||
62162f5224aeSachartre 		    !(vdc->ownership & VDC_OWNERSHIP_GRANTED)) {
62172f5224aeSachartre 			/*
62182f5224aeSachartre 			 * There was a reset so the ownership has been lost,
62192f5224aeSachartre 			 * try to recover. We do this without using the preempt
62202f5224aeSachartre 			 * option so that we don't steal the ownership from
62212f5224aeSachartre 			 * someone who has preempted us.
62222f5224aeSachartre 			 */
62232f5224aeSachartre 			DMSG(vdc, 0, "[%d] Ownership lost, recovering",
62242f5224aeSachartre 			    vdc->instance);
62252f5224aeSachartre 
62262f5224aeSachartre 			vdc->ownership &= ~(VDC_OWNERSHIP_RESET |
62272f5224aeSachartre 			    VDC_OWNERSHIP_GRANTED);
62282f5224aeSachartre 
62292f5224aeSachartre 			mutex_exit(&vdc->lock);
62302f5224aeSachartre 
62312f5224aeSachartre 			status = vdc_access_set(vdc, VD_ACCESS_SET_EXCLUSIVE |
62322f5224aeSachartre 			    VD_ACCESS_SET_PRESERVE, FKIOCTL);
62332f5224aeSachartre 
62342f5224aeSachartre 			mutex_enter(&vdc->lock);
62352f5224aeSachartre 
62362f5224aeSachartre 			if (status == 0) {
62372f5224aeSachartre 				DMSG(vdc, 0, "[%d] Ownership recovered",
62382f5224aeSachartre 				    vdc->instance);
62392f5224aeSachartre 				vdc->ownership |= VDC_OWNERSHIP_GRANTED;
62402f5224aeSachartre 			} else {
62412f5224aeSachartre 				DMSG(vdc, 0, "[%d] Fail to recover ownership",
62422f5224aeSachartre 				    vdc->instance);
62432f5224aeSachartre 			}
62442f5224aeSachartre 
62452f5224aeSachartre 		}
62462f5224aeSachartre 
62472f5224aeSachartre 		/*
62482f5224aeSachartre 		 * If we have the ownership then we just wait for an event
62492f5224aeSachartre 		 * to happen (LDC reset), otherwise we will retry to recover
62502f5224aeSachartre 		 * after a delay.
62512f5224aeSachartre 		 */
62522f5224aeSachartre 		if (vdc->ownership & VDC_OWNERSHIP_GRANTED)
62532f5224aeSachartre 			timeout = 0;
62542f5224aeSachartre 		else
62552f5224aeSachartre 			timeout = ddi_get_lbolt() +
62562f5224aeSachartre 			    drv_usectohz(vdc_ownership_delay);
62572f5224aeSachartre 
62582f5224aeSachartre 		/* Release the ownership_lock and wait on the vdc lock */
62592f5224aeSachartre 		mutex_exit(&vdc->ownership_lock);
62602f5224aeSachartre 
62612f5224aeSachartre 		if (timeout == 0)
62622f5224aeSachartre 			(void) cv_wait(&vdc->ownership_cv, &vdc->lock);
62632f5224aeSachartre 		else
62642f5224aeSachartre 			(void) cv_timedwait(&vdc->ownership_cv,
62652f5224aeSachartre 			    &vdc->lock, timeout);
62662f5224aeSachartre 
62672f5224aeSachartre 		mutex_exit(&vdc->lock);
62682f5224aeSachartre 
62692f5224aeSachartre 		mutex_enter(&vdc->ownership_lock);
62702f5224aeSachartre 		mutex_enter(&vdc->lock);
62712f5224aeSachartre 	}
62722f5224aeSachartre 
62732f5224aeSachartre 	vdc->ownership_thread = NULL;
62742f5224aeSachartre 	mutex_exit(&vdc->lock);
62752f5224aeSachartre 	mutex_exit(&vdc->ownership_lock);
62762f5224aeSachartre 
62772f5224aeSachartre 	thread_exit();
62782f5224aeSachartre }
62792f5224aeSachartre 
62802f5224aeSachartre static void
62812f5224aeSachartre vdc_ownership_update(vdc_t *vdc, int ownership_flags)
62822f5224aeSachartre {
62832f5224aeSachartre 	ASSERT(MUTEX_HELD(&vdc->ownership_lock));
62842f5224aeSachartre 
62852f5224aeSachartre 	mutex_enter(&vdc->lock);
62862f5224aeSachartre 	vdc->ownership = ownership_flags;
62872f5224aeSachartre 	if ((vdc->ownership & VDC_OWNERSHIP_WANTED) &&
62882f5224aeSachartre 	    vdc->ownership_thread == NULL) {
62892f5224aeSachartre 		/* start ownership thread */
62902f5224aeSachartre 		vdc->ownership_thread = thread_create(NULL, 0,
62912f5224aeSachartre 		    vdc_ownership_thread, vdc, 0, &p0, TS_RUN,
62922f5224aeSachartre 		    v.v_maxsyspri - 2);
62932f5224aeSachartre 	} else {
62942f5224aeSachartre 		/* notify the ownership thread */
62952f5224aeSachartre 		cv_signal(&vdc->ownership_cv);
62962f5224aeSachartre 	}
62972f5224aeSachartre 	mutex_exit(&vdc->lock);
62982f5224aeSachartre }
62992f5224aeSachartre 
63002f5224aeSachartre /*
63012f5224aeSachartre  * Get the size and the block size of a virtual disk from the vdisk server.
63022f5224aeSachartre  * We need to use this operation when the vdisk_size attribute was not
63032f5224aeSachartre  * available during the handshake with the vdisk server.
63042f5224aeSachartre  */
63052f5224aeSachartre static int
63062f5224aeSachartre vdc_check_capacity(vdc_t *vdc)
63072f5224aeSachartre {
63082f5224aeSachartre 	int rv = 0;
63092f5224aeSachartre 	size_t alloc_len;
63102f5224aeSachartre 	vd_capacity_t *vd_cap;
63112f5224aeSachartre 
63122f5224aeSachartre 	if (vdc->vdisk_size != 0)
63132f5224aeSachartre 		return (0);
63142f5224aeSachartre 
63152f5224aeSachartre 	alloc_len = P2ROUNDUP(sizeof (vd_capacity_t), sizeof (uint64_t));
63162f5224aeSachartre 
63172f5224aeSachartre 	vd_cap = kmem_zalloc(alloc_len, KM_SLEEP);
63182f5224aeSachartre 
63192f5224aeSachartre 	rv = vdc_do_sync_op(vdc, VD_OP_GET_CAPACITY, (caddr_t)vd_cap, alloc_len,
63202f5224aeSachartre 	    0, 0, CB_SYNC, (void *)(uint64_t)FKIOCTL, VIO_both_dir, B_TRUE);
63212f5224aeSachartre 
63222f5224aeSachartre 	if (rv == 0) {
63232f5224aeSachartre 		if (vd_cap->vdisk_block_size != vdc->block_size ||
63242f5224aeSachartre 		    vd_cap->vdisk_size == VD_SIZE_UNKNOWN ||
63252f5224aeSachartre 		    vd_cap->vdisk_size == 0)
63262f5224aeSachartre 			rv = EINVAL;
63272f5224aeSachartre 		else
63282f5224aeSachartre 			vdc->vdisk_size = vd_cap->vdisk_size;
63292f5224aeSachartre 	}
63302f5224aeSachartre 
63312f5224aeSachartre 	kmem_free(vd_cap, alloc_len);
63322f5224aeSachartre 	return (rv);
63332f5224aeSachartre }
63342f5224aeSachartre 
63352f5224aeSachartre /*
63361ae08745Sheppo  * This structure is used in the DKIO(7I) array below.
63371ae08745Sheppo  */
63381ae08745Sheppo typedef struct vdc_dk_ioctl {
63391ae08745Sheppo 	uint8_t		op;		/* VD_OP_XXX value */
63401ae08745Sheppo 	int		cmd;		/* Solaris ioctl operation number */
63411ae08745Sheppo 	size_t		nbytes;		/* size of structure to be copied */
63420a55fbb7Slm66018 
63430a55fbb7Slm66018 	/* function to convert between vDisk and Solaris structure formats */
6344d10e4ef2Snarayan 	int	(*convert)(vdc_t *vdc, void *vd_buf, void *ioctl_arg,
6345d10e4ef2Snarayan 	    int mode, int dir);
63461ae08745Sheppo } vdc_dk_ioctl_t;
63471ae08745Sheppo 
63481ae08745Sheppo /*
63491ae08745Sheppo  * Subset of DKIO(7I) operations currently supported
63501ae08745Sheppo  */
63511ae08745Sheppo static vdc_dk_ioctl_t	dk_ioctl[] = {
6352eff7243fSlm66018 	{VD_OP_FLUSH,		DKIOCFLUSHWRITECACHE,	0,
63530a55fbb7Slm66018 		vdc_null_copy_func},
63540a55fbb7Slm66018 	{VD_OP_GET_WCE,		DKIOCGETWCE,		sizeof (int),
63554bac2208Snarayan 		vdc_get_wce_convert},
63560a55fbb7Slm66018 	{VD_OP_SET_WCE,		DKIOCSETWCE,		sizeof (int),
63574bac2208Snarayan 		vdc_set_wce_convert},
63580a55fbb7Slm66018 	{VD_OP_GET_VTOC,	DKIOCGVTOC,		sizeof (vd_vtoc_t),
63590a55fbb7Slm66018 		vdc_get_vtoc_convert},
63600a55fbb7Slm66018 	{VD_OP_SET_VTOC,	DKIOCSVTOC,		sizeof (vd_vtoc_t),
63610a55fbb7Slm66018 		vdc_set_vtoc_convert},
63620a55fbb7Slm66018 	{VD_OP_GET_DISKGEOM,	DKIOCGGEOM,		sizeof (vd_geom_t),
63630a55fbb7Slm66018 		vdc_get_geom_convert},
63640a55fbb7Slm66018 	{VD_OP_GET_DISKGEOM,	DKIOCG_PHYGEOM,		sizeof (vd_geom_t),
63650a55fbb7Slm66018 		vdc_get_geom_convert},
63660a55fbb7Slm66018 	{VD_OP_GET_DISKGEOM, 	DKIOCG_VIRTGEOM,	sizeof (vd_geom_t),
63670a55fbb7Slm66018 		vdc_get_geom_convert},
63680a55fbb7Slm66018 	{VD_OP_SET_DISKGEOM,	DKIOCSGEOM,		sizeof (vd_geom_t),
63690a55fbb7Slm66018 		vdc_set_geom_convert},
63704bac2208Snarayan 	{VD_OP_GET_EFI,		DKIOCGETEFI,		0,
63714bac2208Snarayan 		vdc_get_efi_convert},
63724bac2208Snarayan 	{VD_OP_SET_EFI,		DKIOCSETEFI,		0,
63734bac2208Snarayan 		vdc_set_efi_convert},
63740a55fbb7Slm66018 
637587a7269eSachartre 	/* DIOCTL_RWCMD is converted to a read or a write */
637687a7269eSachartre 	{0, DIOCTL_RWCMD,  sizeof (struct dadkio_rwcmd), NULL},
637787a7269eSachartre 
63782f5224aeSachartre 	/* mhd(7I) non-shared multihost disks ioctls */
63792f5224aeSachartre 	{0, MHIOCTKOWN,				0, vdc_null_copy_func},
63802f5224aeSachartre 	{0, MHIOCRELEASE,			0, vdc_null_copy_func},
63812f5224aeSachartre 	{0, MHIOCSTATUS,			0, vdc_null_copy_func},
63822f5224aeSachartre 	{0, MHIOCQRESERVE,			0, vdc_null_copy_func},
63832f5224aeSachartre 
63842f5224aeSachartre 	/* mhd(7I) shared multihost disks ioctls */
63852f5224aeSachartre 	{0, MHIOCGRP_INKEYS,			0, vdc_null_copy_func},
63862f5224aeSachartre 	{0, MHIOCGRP_INRESV,			0, vdc_null_copy_func},
63872f5224aeSachartre 	{0, MHIOCGRP_REGISTER,			0, vdc_null_copy_func},
63882f5224aeSachartre 	{0, MHIOCGRP_RESERVE, 			0, vdc_null_copy_func},
63892f5224aeSachartre 	{0, MHIOCGRP_PREEMPTANDABORT,		0, vdc_null_copy_func},
63902f5224aeSachartre 	{0, MHIOCGRP_REGISTERANDIGNOREKEY,	0, vdc_null_copy_func},
63912f5224aeSachartre 
63922f5224aeSachartre 	/* mhd(7I) failfast ioctl */
63932f5224aeSachartre 	{0, MHIOCENFAILFAST,			0, vdc_null_copy_func},
63942f5224aeSachartre 
63950a55fbb7Slm66018 	/*
63960a55fbb7Slm66018 	 * These particular ioctls are not sent to the server - vdc fakes up
63970a55fbb7Slm66018 	 * the necessary info.
63980a55fbb7Slm66018 	 */
63990a55fbb7Slm66018 	{0, DKIOCINFO, sizeof (struct dk_cinfo), vdc_null_copy_func},
64000a55fbb7Slm66018 	{0, DKIOCGMEDIAINFO, sizeof (struct dk_minfo), vdc_null_copy_func},
64010a55fbb7Slm66018 	{0, USCSICMD,	sizeof (struct uscsi_cmd), vdc_null_copy_func},
6402*9642afceSachartre 	{0, DKIOCPARTITION, 0, vdc_null_copy_func },
640387a7269eSachartre 	{0, DKIOCGAPART, 0, vdc_null_copy_func },
64040a55fbb7Slm66018 	{0, DKIOCREMOVABLE, 0, vdc_null_copy_func},
64050a55fbb7Slm66018 	{0, CDROMREADOFFSET, 0, vdc_null_copy_func}
64061ae08745Sheppo };
64071ae08745Sheppo 
64081ae08745Sheppo /*
6409edcc0754Sachartre  * This function handles ioctl requests from the vd_efi_alloc_and_read()
6410edcc0754Sachartre  * function and forward them to the vdisk.
64112f5224aeSachartre  */
64122f5224aeSachartre static int
6413edcc0754Sachartre vd_process_efi_ioctl(void *vdisk, int cmd, uintptr_t arg)
64142f5224aeSachartre {
6415edcc0754Sachartre 	vdc_t *vdc = (vdc_t *)vdisk;
6416edcc0754Sachartre 	dev_t dev;
64172f5224aeSachartre 	int rval;
6418edcc0754Sachartre 
6419edcc0754Sachartre 	dev = makedevice(ddi_driver_major(vdc->dip),
6420edcc0754Sachartre 	    VD_MAKE_DEV(vdc->instance, 0));
6421edcc0754Sachartre 
6422edcc0754Sachartre 	return (vd_process_ioctl(dev, cmd, (caddr_t)arg, FKIOCTL, &rval));
64232f5224aeSachartre }
64242f5224aeSachartre 
64252f5224aeSachartre /*
64261ae08745Sheppo  * Function:
64271ae08745Sheppo  *	vd_process_ioctl()
64281ae08745Sheppo  *
64291ae08745Sheppo  * Description:
64300a55fbb7Slm66018  *	This routine processes disk specific ioctl calls
64311ae08745Sheppo  *
64321ae08745Sheppo  * Arguments:
64331ae08745Sheppo  *	dev	- the device number
64341ae08745Sheppo  *	cmd	- the operation [dkio(7I)] to be processed
64351ae08745Sheppo  *	arg	- pointer to user provided structure
64361ae08745Sheppo  *		  (contains data to be set or reference parameter for get)
64371ae08745Sheppo  *	mode	- bit flag, indicating open settings, 32/64 bit type, etc
64382f5224aeSachartre  *	rvalp	- pointer to return value for calling process.
64391ae08745Sheppo  *
64401ae08745Sheppo  * Return Code:
64411ae08745Sheppo  *	0
64421ae08745Sheppo  *	EFAULT
64431ae08745Sheppo  *	ENXIO
64441ae08745Sheppo  *	EIO
64451ae08745Sheppo  *	ENOTSUP
64461ae08745Sheppo  */
64471ae08745Sheppo static int
64482f5224aeSachartre vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode, int *rvalp)
64491ae08745Sheppo {
64500d0c8d4bSnarayan 	int		instance = VDCUNIT(dev);
64511ae08745Sheppo 	vdc_t		*vdc = NULL;
64521ae08745Sheppo 	int		rv = -1;
64531ae08745Sheppo 	int		idx = 0;		/* index into dk_ioctl[] */
64541ae08745Sheppo 	size_t		len = 0;		/* #bytes to send to vds */
64551ae08745Sheppo 	size_t		alloc_len = 0;		/* #bytes to allocate mem for */
64561ae08745Sheppo 	caddr_t		mem_p = NULL;
64571ae08745Sheppo 	size_t		nioctls = (sizeof (dk_ioctl)) / (sizeof (dk_ioctl[0]));
64583af08d82Slm66018 	vdc_dk_ioctl_t	*iop;
64591ae08745Sheppo 
64601ae08745Sheppo 	vdc = ddi_get_soft_state(vdc_state, instance);
64611ae08745Sheppo 	if (vdc == NULL) {
64621ae08745Sheppo 		cmn_err(CE_NOTE, "![%d] Could not get soft state structure",
64631ae08745Sheppo 		    instance);
64641ae08745Sheppo 		return (ENXIO);
64651ae08745Sheppo 	}
64661ae08745Sheppo 
64673af08d82Slm66018 	DMSG(vdc, 0, "[%d] Processing ioctl(%x) for dev %lx : model %x\n",
64683af08d82Slm66018 	    instance, cmd, dev, ddi_model_convert_from(mode & FMODELS));
64691ae08745Sheppo 
64702f5224aeSachartre 	if (rvalp != NULL) {
64712f5224aeSachartre 		/* the return value of the ioctl is 0 by default */
64722f5224aeSachartre 		*rvalp = 0;
64732f5224aeSachartre 	}
64742f5224aeSachartre 
64751ae08745Sheppo 	/*
64761ae08745Sheppo 	 * Validate the ioctl operation to be performed.
64771ae08745Sheppo 	 *
64781ae08745Sheppo 	 * If we have looped through the array without finding a match then we
64791ae08745Sheppo 	 * don't support this ioctl.
64801ae08745Sheppo 	 */
64811ae08745Sheppo 	for (idx = 0; idx < nioctls; idx++) {
64821ae08745Sheppo 		if (cmd == dk_ioctl[idx].cmd)
64831ae08745Sheppo 			break;
64841ae08745Sheppo 	}
64851ae08745Sheppo 
64861ae08745Sheppo 	if (idx >= nioctls) {
64873af08d82Slm66018 		DMSG(vdc, 0, "[%d] Unsupported ioctl (0x%x)\n",
6488e1ebb9ecSlm66018 		    vdc->instance, cmd);
64891ae08745Sheppo 		return (ENOTSUP);
64901ae08745Sheppo 	}
64911ae08745Sheppo 
64923af08d82Slm66018 	iop = &(dk_ioctl[idx]);
64933af08d82Slm66018 
64944bac2208Snarayan 	if (cmd == DKIOCGETEFI || cmd == DKIOCSETEFI) {
64954bac2208Snarayan 		/* size is not fixed for EFI ioctls, it depends on ioctl arg */
64964bac2208Snarayan 		dk_efi_t	dk_efi;
64974bac2208Snarayan 
64984bac2208Snarayan 		rv = ddi_copyin(arg, &dk_efi, sizeof (dk_efi_t), mode);
64994bac2208Snarayan 		if (rv != 0)
65004bac2208Snarayan 			return (EFAULT);
65014bac2208Snarayan 
65024bac2208Snarayan 		len = sizeof (vd_efi_t) - 1 + dk_efi.dki_length;
65034bac2208Snarayan 	} else {
65043af08d82Slm66018 		len = iop->nbytes;
65054bac2208Snarayan 	}
65061ae08745Sheppo 
65072f5224aeSachartre 	/* check if the ioctl is applicable */
65081ae08745Sheppo 	switch (cmd) {
65091ae08745Sheppo 	case CDROMREADOFFSET:
65101ae08745Sheppo 	case DKIOCREMOVABLE:
65111ae08745Sheppo 		return (ENOTTY);
65121ae08745Sheppo 
65132f5224aeSachartre 	case USCSICMD:
65142f5224aeSachartre 	case MHIOCTKOWN:
65152f5224aeSachartre 	case MHIOCSTATUS:
65162f5224aeSachartre 	case MHIOCQRESERVE:
65172f5224aeSachartre 	case MHIOCRELEASE:
65182f5224aeSachartre 	case MHIOCGRP_INKEYS:
65192f5224aeSachartre 	case MHIOCGRP_INRESV:
65202f5224aeSachartre 	case MHIOCGRP_REGISTER:
65212f5224aeSachartre 	case MHIOCGRP_RESERVE:
65222f5224aeSachartre 	case MHIOCGRP_PREEMPTANDABORT:
65232f5224aeSachartre 	case MHIOCGRP_REGISTERANDIGNOREKEY:
65242f5224aeSachartre 	case MHIOCENFAILFAST:
65252f5224aeSachartre 		if (vdc->cinfo == NULL)
65262f5224aeSachartre 			return (ENXIO);
65272f5224aeSachartre 		if (vdc->cinfo->dki_ctype != DKC_SCSI_CCS)
65282f5224aeSachartre 			return (ENOTTY);
65292f5224aeSachartre 		break;
65302f5224aeSachartre 
65312f5224aeSachartre 	case DIOCTL_RWCMD:
65322f5224aeSachartre 		if (vdc->cinfo == NULL)
65332f5224aeSachartre 			return (ENXIO);
65342f5224aeSachartre 		if (vdc->cinfo->dki_ctype != DKC_DIRECT)
65352f5224aeSachartre 			return (ENOTTY);
65362f5224aeSachartre 		break;
65372f5224aeSachartre 
65382f5224aeSachartre 	case DKIOCINFO:
65392f5224aeSachartre 		if (vdc->cinfo == NULL)
65402f5224aeSachartre 			return (ENXIO);
65412f5224aeSachartre 		break;
65422f5224aeSachartre 
65432f5224aeSachartre 	case DKIOCGMEDIAINFO:
65442f5224aeSachartre 		if (vdc->minfo == NULL)
65452f5224aeSachartre 			return (ENXIO);
65462f5224aeSachartre 		if (vdc_check_capacity(vdc) != 0)
65472f5224aeSachartre 			/* disk capacity is not available */
65482f5224aeSachartre 			return (EIO);
65492f5224aeSachartre 		break;
65502f5224aeSachartre 	}
65512f5224aeSachartre 
65522f5224aeSachartre 	/*
65532f5224aeSachartre 	 * Deal with ioctls which require a processing different than
65542f5224aeSachartre 	 * converting ioctl arguments and sending a corresponding
65552f5224aeSachartre 	 * VD operation.
65562f5224aeSachartre 	 */
65572f5224aeSachartre 	switch (cmd) {
65582f5224aeSachartre 
65592f5224aeSachartre 	case USCSICMD:
65602f5224aeSachartre 	{
65612f5224aeSachartre 		return (vdc_uscsi_cmd(vdc, arg, mode));
65622f5224aeSachartre 	}
65632f5224aeSachartre 
65642f5224aeSachartre 	case MHIOCTKOWN:
65652f5224aeSachartre 	{
65662f5224aeSachartre 		mutex_enter(&vdc->ownership_lock);
65672f5224aeSachartre 		/*
65682f5224aeSachartre 		 * We have to set VDC_OWNERSHIP_WANTED now so that the ownership
65692f5224aeSachartre 		 * can be flagged with VDC_OWNERSHIP_RESET if the LDC is reset
65702f5224aeSachartre 		 * while we are processing the ioctl.
65712f5224aeSachartre 		 */
65722f5224aeSachartre 		vdc_ownership_update(vdc, VDC_OWNERSHIP_WANTED);
65732f5224aeSachartre 
65742f5224aeSachartre 		rv = vdc_access_set(vdc, VD_ACCESS_SET_EXCLUSIVE |
65752f5224aeSachartre 		    VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE, mode);
65762f5224aeSachartre 		if (rv == 0) {
65772f5224aeSachartre 			vdc_ownership_update(vdc, VDC_OWNERSHIP_WANTED |
65782f5224aeSachartre 			    VDC_OWNERSHIP_GRANTED);
65792f5224aeSachartre 		} else {
65802f5224aeSachartre 			vdc_ownership_update(vdc, VDC_OWNERSHIP_NONE);
65812f5224aeSachartre 		}
65822f5224aeSachartre 		mutex_exit(&vdc->ownership_lock);
65832f5224aeSachartre 		return (rv);
65842f5224aeSachartre 	}
65852f5224aeSachartre 
65862f5224aeSachartre 	case MHIOCRELEASE:
65872f5224aeSachartre 	{
65882f5224aeSachartre 		mutex_enter(&vdc->ownership_lock);
65892f5224aeSachartre 		rv = vdc_access_set(vdc, VD_ACCESS_SET_CLEAR, mode);
65902f5224aeSachartre 		if (rv == 0) {
65912f5224aeSachartre 			vdc_ownership_update(vdc, VDC_OWNERSHIP_NONE);
65922f5224aeSachartre 		}
65932f5224aeSachartre 		mutex_exit(&vdc->ownership_lock);
65942f5224aeSachartre 		return (rv);
65952f5224aeSachartre 	}
65962f5224aeSachartre 
65972f5224aeSachartre 	case MHIOCSTATUS:
65982f5224aeSachartre 	{
65992f5224aeSachartre 		uint64_t status;
66002f5224aeSachartre 
66012f5224aeSachartre 		rv = vdc_access_get(vdc, &status, mode);
66022f5224aeSachartre 		if (rv == 0 && rvalp != NULL)
66032f5224aeSachartre 			*rvalp = (status & VD_ACCESS_ALLOWED)? 0 : 1;
66042f5224aeSachartre 		return (rv);
66052f5224aeSachartre 	}
66062f5224aeSachartre 
66072f5224aeSachartre 	case MHIOCQRESERVE:
66082f5224aeSachartre 	{
66092f5224aeSachartre 		rv = vdc_access_set(vdc, VD_ACCESS_SET_EXCLUSIVE, mode);
66102f5224aeSachartre 		return (rv);
66112f5224aeSachartre 	}
66122f5224aeSachartre 
66132f5224aeSachartre 	case MHIOCGRP_INKEYS:
66142f5224aeSachartre 	{
66152f5224aeSachartre 		return (vdc_mhd_inkeys(vdc, arg, mode));
66162f5224aeSachartre 	}
66172f5224aeSachartre 
66182f5224aeSachartre 	case MHIOCGRP_INRESV:
66192f5224aeSachartre 	{
66202f5224aeSachartre 		return (vdc_mhd_inresv(vdc, arg, mode));
66212f5224aeSachartre 	}
66222f5224aeSachartre 
66232f5224aeSachartre 	case MHIOCGRP_REGISTER:
66242f5224aeSachartre 	{
66252f5224aeSachartre 		return (vdc_mhd_register(vdc, arg, mode));
66262f5224aeSachartre 	}
66272f5224aeSachartre 
66282f5224aeSachartre 	case MHIOCGRP_RESERVE:
66292f5224aeSachartre 	{
66302f5224aeSachartre 		return (vdc_mhd_reserve(vdc, arg, mode));
66312f5224aeSachartre 	}
66322f5224aeSachartre 
66332f5224aeSachartre 	case MHIOCGRP_PREEMPTANDABORT:
66342f5224aeSachartre 	{
66352f5224aeSachartre 		return (vdc_mhd_preemptabort(vdc, arg, mode));
66362f5224aeSachartre 	}
66372f5224aeSachartre 
66382f5224aeSachartre 	case MHIOCGRP_REGISTERANDIGNOREKEY:
66392f5224aeSachartre 	{
66402f5224aeSachartre 		return (vdc_mhd_registerignore(vdc, arg, mode));
66412f5224aeSachartre 	}
66422f5224aeSachartre 
66432f5224aeSachartre 	case MHIOCENFAILFAST:
66442f5224aeSachartre 	{
66452f5224aeSachartre 		rv = vdc_failfast(vdc, arg, mode);
66462f5224aeSachartre 		return (rv);
66472f5224aeSachartre 	}
66482f5224aeSachartre 
664987a7269eSachartre 	case DIOCTL_RWCMD:
665087a7269eSachartre 	{
665187a7269eSachartre 		return (vdc_dioctl_rwcmd(dev, arg, mode));
665287a7269eSachartre 	}
665387a7269eSachartre 
665487a7269eSachartre 	case DKIOCGAPART:
665587a7269eSachartre 	{
6656*9642afceSachartre 		return (vdc_dkio_gapart(vdc, arg, mode));
6657*9642afceSachartre 	}
6658*9642afceSachartre 
6659*9642afceSachartre 	case DKIOCPARTITION:
6660*9642afceSachartre 	{
6661*9642afceSachartre 		return (vdc_dkio_partition(vdc, arg, mode));
666287a7269eSachartre 	}
666387a7269eSachartre 
66641ae08745Sheppo 	case DKIOCINFO:
66651ae08745Sheppo 	{
66661ae08745Sheppo 		struct dk_cinfo	cinfo;
66671ae08745Sheppo 
66681ae08745Sheppo 		bcopy(vdc->cinfo, &cinfo, sizeof (struct dk_cinfo));
66690d0c8d4bSnarayan 		cinfo.dki_partition = VDCPART(dev);
66701ae08745Sheppo 
66711ae08745Sheppo 		rv = ddi_copyout(&cinfo, (void *)arg,
66721ae08745Sheppo 		    sizeof (struct dk_cinfo), mode);
66731ae08745Sheppo 		if (rv != 0)
66741ae08745Sheppo 			return (EFAULT);
66751ae08745Sheppo 
66761ae08745Sheppo 		return (0);
66771ae08745Sheppo 	}
66781ae08745Sheppo 
66791ae08745Sheppo 	case DKIOCGMEDIAINFO:
66808e6a2a04Slm66018 	{
66812f5224aeSachartre 		ASSERT(vdc->vdisk_size != 0);
66822f5224aeSachartre 		if (vdc->minfo->dki_capacity == 0)
66832f5224aeSachartre 			vdc->minfo->dki_capacity = vdc->vdisk_size;
66841ae08745Sheppo 		rv = ddi_copyout(vdc->minfo, (void *)arg,
66851ae08745Sheppo 		    sizeof (struct dk_minfo), mode);
66861ae08745Sheppo 		if (rv != 0)
66871ae08745Sheppo 			return (EFAULT);
66881ae08745Sheppo 
66891ae08745Sheppo 		return (0);
66901ae08745Sheppo 	}
66911ae08745Sheppo 
66928e6a2a04Slm66018 	case DKIOCFLUSHWRITECACHE:
66938e6a2a04Slm66018 		{
669417cadca8Slm66018 			struct dk_callback *dkc =
669517cadca8Slm66018 			    (struct dk_callback *)(uintptr_t)arg;
66968e6a2a04Slm66018 			vdc_dk_arg_t	*dkarg = NULL;
66978e6a2a04Slm66018 
66983af08d82Slm66018 			DMSG(vdc, 1, "[%d] Flush W$: mode %x\n",
66993af08d82Slm66018 			    instance, mode);
67008e6a2a04Slm66018 
67018e6a2a04Slm66018 			/*
67028e6a2a04Slm66018 			 * If arg is NULL, then there is no callback function
67038e6a2a04Slm66018 			 * registered and the call operates synchronously; we
67048e6a2a04Slm66018 			 * break and continue with the rest of the function and
67058e6a2a04Slm66018 			 * wait for vds to return (i.e. after the request to
67068e6a2a04Slm66018 			 * vds returns successfully, all writes completed prior
67078e6a2a04Slm66018 			 * to the ioctl will have been flushed from the disk
67088e6a2a04Slm66018 			 * write cache to persistent media.
67098e6a2a04Slm66018 			 *
67108e6a2a04Slm66018 			 * If a callback function is registered, we dispatch
67118e6a2a04Slm66018 			 * the request on a task queue and return immediately.
67128e6a2a04Slm66018 			 * The callback will deal with informing the calling
67138e6a2a04Slm66018 			 * thread that the flush request is completed.
67148e6a2a04Slm66018 			 */
67158e6a2a04Slm66018 			if (dkc == NULL)
67168e6a2a04Slm66018 				break;
67178e6a2a04Slm66018 
6718eff7243fSlm66018 			/*
6719eff7243fSlm66018 			 * the asynchronous callback is only supported if
6720eff7243fSlm66018 			 * invoked from within the kernel
6721eff7243fSlm66018 			 */
6722eff7243fSlm66018 			if ((mode & FKIOCTL) == 0)
6723eff7243fSlm66018 				return (ENOTSUP);
6724eff7243fSlm66018 
67258e6a2a04Slm66018 			dkarg = kmem_zalloc(sizeof (vdc_dk_arg_t), KM_SLEEP);
67268e6a2a04Slm66018 
67278e6a2a04Slm66018 			dkarg->mode = mode;
67288e6a2a04Slm66018 			dkarg->dev = dev;
67298e6a2a04Slm66018 			bcopy(dkc, &dkarg->dkc, sizeof (*dkc));
67308e6a2a04Slm66018 
67318e6a2a04Slm66018 			mutex_enter(&vdc->lock);
67328e6a2a04Slm66018 			vdc->dkio_flush_pending++;
67338e6a2a04Slm66018 			dkarg->vdc = vdc;
67348e6a2a04Slm66018 			mutex_exit(&vdc->lock);
67358e6a2a04Slm66018 
67368e6a2a04Slm66018 			/* put the request on a task queue */
67378e6a2a04Slm66018 			rv = taskq_dispatch(system_taskq, vdc_dkio_flush_cb,
67388e6a2a04Slm66018 			    (void *)dkarg, DDI_SLEEP);
67393af08d82Slm66018 			if (rv == NULL) {
67403af08d82Slm66018 				/* clean up if dispatch fails */
67413af08d82Slm66018 				mutex_enter(&vdc->lock);
67423af08d82Slm66018 				vdc->dkio_flush_pending--;
674378fcd0a1Sachartre 				mutex_exit(&vdc->lock);
67443af08d82Slm66018 				kmem_free(dkarg, sizeof (vdc_dk_arg_t));
67453af08d82Slm66018 			}
67468e6a2a04Slm66018 
67478e6a2a04Slm66018 			return (rv == NULL ? ENOMEM : 0);
67488e6a2a04Slm66018 		}
67498e6a2a04Slm66018 	}
67508e6a2a04Slm66018 
67511ae08745Sheppo 	/* catch programming error in vdc - should be a VD_OP_XXX ioctl */
67523af08d82Slm66018 	ASSERT(iop->op != 0);
67531ae08745Sheppo 
675417cadca8Slm66018 	/* check if the vDisk server handles the operation for this vDisk */
675517cadca8Slm66018 	if (VD_OP_SUPPORTED(vdc->operations, iop->op) == B_FALSE) {
675617cadca8Slm66018 		DMSG(vdc, 0, "[%d] Unsupported VD_OP operation (0x%x)\n",
675717cadca8Slm66018 		    vdc->instance, iop->op);
675817cadca8Slm66018 		return (ENOTSUP);
675917cadca8Slm66018 	}
676017cadca8Slm66018 
67611ae08745Sheppo 	/* LDC requires that the memory being mapped is 8-byte aligned */
67621ae08745Sheppo 	alloc_len = P2ROUNDUP(len, sizeof (uint64_t));
67633af08d82Slm66018 	DMSG(vdc, 1, "[%d] struct size %ld alloc %ld\n",
67643af08d82Slm66018 	    instance, len, alloc_len);
67651ae08745Sheppo 
6766eff7243fSlm66018 	if (alloc_len > 0)
67671ae08745Sheppo 		mem_p = kmem_zalloc(alloc_len, KM_SLEEP);
67681ae08745Sheppo 
67690a55fbb7Slm66018 	/*
6770eff7243fSlm66018 	 * Call the conversion function for this ioctl which, if necessary,
67710a55fbb7Slm66018 	 * converts from the Solaris format to the format ARC'ed
67720a55fbb7Slm66018 	 * as part of the vDisk protocol (FWARC 2006/195)
67730a55fbb7Slm66018 	 */
67743af08d82Slm66018 	ASSERT(iop->convert != NULL);
67753af08d82Slm66018 	rv = (iop->convert)(vdc, arg, mem_p, mode, VD_COPYIN);
67761ae08745Sheppo 	if (rv != 0) {
67773af08d82Slm66018 		DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n",
6778e1ebb9ecSlm66018 		    instance, rv, cmd);
67791ae08745Sheppo 		if (mem_p != NULL)
67801ae08745Sheppo 			kmem_free(mem_p, alloc_len);
67810a55fbb7Slm66018 		return (rv);
67821ae08745Sheppo 	}
67831ae08745Sheppo 
67841ae08745Sheppo 	/*
67851ae08745Sheppo 	 * send request to vds to service the ioctl.
67861ae08745Sheppo 	 */
67873af08d82Slm66018 	rv = vdc_do_sync_op(vdc, iop->op, mem_p, alloc_len,
67880d0c8d4bSnarayan 	    VDCPART(dev), 0, CB_SYNC, (void *)(uint64_t)mode,
67892f5224aeSachartre 	    VIO_both_dir, B_TRUE);
679078fcd0a1Sachartre 
67911ae08745Sheppo 	if (rv != 0) {
67921ae08745Sheppo 		/*
67931ae08745Sheppo 		 * This is not necessarily an error. The ioctl could
67941ae08745Sheppo 		 * be returning a value such as ENOTTY to indicate
67951ae08745Sheppo 		 * that the ioctl is not applicable.
67961ae08745Sheppo 		 */
67973af08d82Slm66018 		DMSG(vdc, 0, "[%d] vds returned %d for ioctl 0x%x\n",
6798e1ebb9ecSlm66018 		    instance, rv, cmd);
67991ae08745Sheppo 		if (mem_p != NULL)
68001ae08745Sheppo 			kmem_free(mem_p, alloc_len);
6801d10e4ef2Snarayan 
68021ae08745Sheppo 		return (rv);
68031ae08745Sheppo 	}
68041ae08745Sheppo 
68051ae08745Sheppo 	/*
68060a55fbb7Slm66018 	 * Call the conversion function (if it exists) for this ioctl
68070a55fbb7Slm66018 	 * which converts from the format ARC'ed as part of the vDisk
68080a55fbb7Slm66018 	 * protocol (FWARC 2006/195) back to a format understood by
68090a55fbb7Slm66018 	 * the rest of Solaris.
68101ae08745Sheppo 	 */
68113af08d82Slm66018 	rv = (iop->convert)(vdc, mem_p, arg, mode, VD_COPYOUT);
68120a55fbb7Slm66018 	if (rv != 0) {
68133af08d82Slm66018 		DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n",
6814e1ebb9ecSlm66018 		    instance, rv, cmd);
68151ae08745Sheppo 		if (mem_p != NULL)
68161ae08745Sheppo 			kmem_free(mem_p, alloc_len);
68170a55fbb7Slm66018 		return (rv);
68181ae08745Sheppo 	}
68191ae08745Sheppo 
68201ae08745Sheppo 	if (mem_p != NULL)
68211ae08745Sheppo 		kmem_free(mem_p, alloc_len);
68221ae08745Sheppo 
68231ae08745Sheppo 	return (rv);
68241ae08745Sheppo }
68251ae08745Sheppo 
68261ae08745Sheppo /*
68271ae08745Sheppo  * Function:
68280a55fbb7Slm66018  *
68290a55fbb7Slm66018  * Description:
68300a55fbb7Slm66018  *	This is an empty conversion function used by ioctl calls which
68310a55fbb7Slm66018  *	do not need to convert the data being passed in/out to userland
68320a55fbb7Slm66018  */
68330a55fbb7Slm66018 static int
6834d10e4ef2Snarayan vdc_null_copy_func(vdc_t *vdc, void *from, void *to, int mode, int dir)
68350a55fbb7Slm66018 {
6836d10e4ef2Snarayan 	_NOTE(ARGUNUSED(vdc))
68370a55fbb7Slm66018 	_NOTE(ARGUNUSED(from))
68380a55fbb7Slm66018 	_NOTE(ARGUNUSED(to))
68390a55fbb7Slm66018 	_NOTE(ARGUNUSED(mode))
68400a55fbb7Slm66018 	_NOTE(ARGUNUSED(dir))
68410a55fbb7Slm66018 
68420a55fbb7Slm66018 	return (0);
68430a55fbb7Slm66018 }
68440a55fbb7Slm66018 
68454bac2208Snarayan static int
68464bac2208Snarayan vdc_get_wce_convert(vdc_t *vdc, void *from, void *to,
68474bac2208Snarayan     int mode, int dir)
68484bac2208Snarayan {
68494bac2208Snarayan 	_NOTE(ARGUNUSED(vdc))
68504bac2208Snarayan 
68514bac2208Snarayan 	if (dir == VD_COPYIN)
68524bac2208Snarayan 		return (0);		/* nothing to do */
68534bac2208Snarayan 
68544bac2208Snarayan 	if (ddi_copyout(from, to, sizeof (int), mode) != 0)
68554bac2208Snarayan 		return (EFAULT);
68564bac2208Snarayan 
68574bac2208Snarayan 	return (0);
68584bac2208Snarayan }
68594bac2208Snarayan 
68604bac2208Snarayan static int
68614bac2208Snarayan vdc_set_wce_convert(vdc_t *vdc, void *from, void *to,
68624bac2208Snarayan     int mode, int dir)
68634bac2208Snarayan {
68644bac2208Snarayan 	_NOTE(ARGUNUSED(vdc))
68654bac2208Snarayan 
68664bac2208Snarayan 	if (dir == VD_COPYOUT)
68674bac2208Snarayan 		return (0);		/* nothing to do */
68684bac2208Snarayan 
68694bac2208Snarayan 	if (ddi_copyin(from, to, sizeof (int), mode) != 0)
68704bac2208Snarayan 		return (EFAULT);
68714bac2208Snarayan 
68724bac2208Snarayan 	return (0);
68734bac2208Snarayan }
68744bac2208Snarayan 
68750a55fbb7Slm66018 /*
68760a55fbb7Slm66018  * Function:
68770a55fbb7Slm66018  *	vdc_get_vtoc_convert()
68780a55fbb7Slm66018  *
68790a55fbb7Slm66018  * Description:
6880d10e4ef2Snarayan  *	This routine performs the necessary convertions from the DKIOCGVTOC
6881d10e4ef2Snarayan  *	Solaris structure to the format defined in FWARC 2006/195.
6882d10e4ef2Snarayan  *
6883d10e4ef2Snarayan  *	In the struct vtoc definition, the timestamp field is marked as not
6884d10e4ef2Snarayan  *	supported so it is not part of vDisk protocol (FWARC 2006/195).
6885d10e4ef2Snarayan  *	However SVM uses that field to check it can write into the VTOC,
6886d10e4ef2Snarayan  *	so we fake up the info of that field.
68870a55fbb7Slm66018  *
68880a55fbb7Slm66018  * Arguments:
6889d10e4ef2Snarayan  *	vdc	- the vDisk client
68900a55fbb7Slm66018  *	from	- the buffer containing the data to be copied from
68910a55fbb7Slm66018  *	to	- the buffer to be copied to
68920a55fbb7Slm66018  *	mode	- flags passed to ioctl() call
68930a55fbb7Slm66018  *	dir	- the "direction" of the copy - VD_COPYIN or VD_COPYOUT
68940a55fbb7Slm66018  *
68950a55fbb7Slm66018  * Return Code:
68960a55fbb7Slm66018  *	0	- Success
68970a55fbb7Slm66018  *	ENXIO	- incorrect buffer passed in.
6898d10e4ef2Snarayan  *	EFAULT	- ddi_copyout routine encountered an error.
68990a55fbb7Slm66018  */
69000a55fbb7Slm66018 static int
6901d10e4ef2Snarayan vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
69020a55fbb7Slm66018 {
6903d10e4ef2Snarayan 	int		i;
69040a55fbb7Slm66018 	void		*tmp_mem = NULL;
69050a55fbb7Slm66018 	void		*tmp_memp;
69060a55fbb7Slm66018 	struct vtoc	vt;
69070a55fbb7Slm66018 	struct vtoc32	vt32;
69080a55fbb7Slm66018 	int		copy_len = 0;
69090a55fbb7Slm66018 	int		rv = 0;
69100a55fbb7Slm66018 
69110a55fbb7Slm66018 	if (dir != VD_COPYOUT)
69120a55fbb7Slm66018 		return (0);	/* nothing to do */
69130a55fbb7Slm66018 
69140a55fbb7Slm66018 	if ((from == NULL) || (to == NULL))
69150a55fbb7Slm66018 		return (ENXIO);
69160a55fbb7Slm66018 
69170a55fbb7Slm66018 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32)
69180a55fbb7Slm66018 		copy_len = sizeof (struct vtoc32);
69190a55fbb7Slm66018 	else
69200a55fbb7Slm66018 		copy_len = sizeof (struct vtoc);
69210a55fbb7Slm66018 
69220a55fbb7Slm66018 	tmp_mem = kmem_alloc(copy_len, KM_SLEEP);
69230a55fbb7Slm66018 
69240a55fbb7Slm66018 	VD_VTOC2VTOC((vd_vtoc_t *)from, &vt);
6925d10e4ef2Snarayan 
6926d10e4ef2Snarayan 	/* fake the VTOC timestamp field */
6927d10e4ef2Snarayan 	for (i = 0; i < V_NUMPAR; i++) {
6928d10e4ef2Snarayan 		vt.timestamp[i] = vdc->vtoc->timestamp[i];
6929d10e4ef2Snarayan 	}
6930d10e4ef2Snarayan 
69310a55fbb7Slm66018 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
693217cadca8Slm66018 		/* LINTED E_ASSIGN_NARROW_CONV */
69330a55fbb7Slm66018 		vtoctovtoc32(vt, vt32);
69340a55fbb7Slm66018 		tmp_memp = &vt32;
69350a55fbb7Slm66018 	} else {
69360a55fbb7Slm66018 		tmp_memp = &vt;
69370a55fbb7Slm66018 	}
69380a55fbb7Slm66018 	rv = ddi_copyout(tmp_memp, to, copy_len, mode);
69390a55fbb7Slm66018 	if (rv != 0)
69400a55fbb7Slm66018 		rv = EFAULT;
69410a55fbb7Slm66018 
69420a55fbb7Slm66018 	kmem_free(tmp_mem, copy_len);
69430a55fbb7Slm66018 	return (rv);
69440a55fbb7Slm66018 }
69450a55fbb7Slm66018 
69460a55fbb7Slm66018 /*
69470a55fbb7Slm66018  * Function:
69480a55fbb7Slm66018  *	vdc_set_vtoc_convert()
69490a55fbb7Slm66018  *
69500a55fbb7Slm66018  * Description:
6951d10e4ef2Snarayan  *	This routine performs the necessary convertions from the DKIOCSVTOC
6952d10e4ef2Snarayan  *	Solaris structure to the format defined in FWARC 2006/195.
69530a55fbb7Slm66018  *
69540a55fbb7Slm66018  * Arguments:
6955d10e4ef2Snarayan  *	vdc	- the vDisk client
69560a55fbb7Slm66018  *	from	- Buffer with data
69570a55fbb7Slm66018  *	to	- Buffer where data is to be copied to
69580a55fbb7Slm66018  *	mode	- flags passed to ioctl
69590a55fbb7Slm66018  *	dir	- direction of copy (in or out)
69600a55fbb7Slm66018  *
69610a55fbb7Slm66018  * Return Code:
69620a55fbb7Slm66018  *	0	- Success
69630a55fbb7Slm66018  *	ENXIO	- Invalid buffer passed in
69640a55fbb7Slm66018  *	EFAULT	- ddi_copyin of data failed
69650a55fbb7Slm66018  */
69660a55fbb7Slm66018 static int
6967d10e4ef2Snarayan vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
69680a55fbb7Slm66018 {
696978fcd0a1Sachartre 	_NOTE(ARGUNUSED(vdc))
697078fcd0a1Sachartre 
69712f5224aeSachartre 	void		*tmp_mem = NULL, *uvtoc;
69720a55fbb7Slm66018 	struct vtoc	vt;
69730a55fbb7Slm66018 	struct vtoc	*vtp = &vt;
69740a55fbb7Slm66018 	vd_vtoc_t	vtvd;
69750a55fbb7Slm66018 	int		copy_len = 0;
69762f5224aeSachartre 	int		i, rv = 0;
69770a55fbb7Slm66018 
69780a55fbb7Slm66018 	if ((from == NULL) || (to == NULL))
69790a55fbb7Slm66018 		return (ENXIO);
69800a55fbb7Slm66018 
69812f5224aeSachartre 	if (dir == VD_COPYIN)
69822f5224aeSachartre 		uvtoc = from;
69832f5224aeSachartre 	else
69842f5224aeSachartre 		uvtoc = to;
69852f5224aeSachartre 
69860a55fbb7Slm66018 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32)
69870a55fbb7Slm66018 		copy_len = sizeof (struct vtoc32);
69880a55fbb7Slm66018 	else
69890a55fbb7Slm66018 		copy_len = sizeof (struct vtoc);
69900a55fbb7Slm66018 
69910a55fbb7Slm66018 	tmp_mem = kmem_alloc(copy_len, KM_SLEEP);
69920a55fbb7Slm66018 
69932f5224aeSachartre 	rv = ddi_copyin(uvtoc, tmp_mem, copy_len, mode);
69940a55fbb7Slm66018 	if (rv != 0) {
69950a55fbb7Slm66018 		kmem_free(tmp_mem, copy_len);
69960a55fbb7Slm66018 		return (EFAULT);
69970a55fbb7Slm66018 	}
69980a55fbb7Slm66018 
69990a55fbb7Slm66018 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
70000a55fbb7Slm66018 		vtoc32tovtoc((*(struct vtoc32 *)tmp_mem), vt);
70010a55fbb7Slm66018 	} else {
70020a55fbb7Slm66018 		vtp = tmp_mem;
70030a55fbb7Slm66018 	}
70040a55fbb7Slm66018 
70052f5224aeSachartre 	if (dir == VD_COPYOUT) {
70062f5224aeSachartre 		/*
70072f5224aeSachartre 		 * The disk label may have changed. Revalidate the disk
70082f5224aeSachartre 		 * geometry. This will also update the device nodes and
70092f5224aeSachartre 		 * properties.
70102f5224aeSachartre 		 */
70112f5224aeSachartre 		vdc_validate(vdc);
70122f5224aeSachartre 
70132f5224aeSachartre 		/*
70142f5224aeSachartre 		 * We also need to keep track of the timestamp fields.
70152f5224aeSachartre 		 */
70162f5224aeSachartre 		for (i = 0; i < V_NUMPAR; i++) {
70172f5224aeSachartre 			vdc->vtoc->timestamp[i] = vtp->timestamp[i];
70182f5224aeSachartre 		}
70192f5224aeSachartre 
70202f5224aeSachartre 		return (0);
70212f5224aeSachartre 	}
70222f5224aeSachartre 
70230a55fbb7Slm66018 	VTOC2VD_VTOC(vtp, &vtvd);
70240a55fbb7Slm66018 	bcopy(&vtvd, to, sizeof (vd_vtoc_t));
70250a55fbb7Slm66018 	kmem_free(tmp_mem, copy_len);
70260a55fbb7Slm66018 
70270a55fbb7Slm66018 	return (0);
70280a55fbb7Slm66018 }
70290a55fbb7Slm66018 
70300a55fbb7Slm66018 /*
70310a55fbb7Slm66018  * Function:
70320a55fbb7Slm66018  *	vdc_get_geom_convert()
70330a55fbb7Slm66018  *
70340a55fbb7Slm66018  * Description:
7035d10e4ef2Snarayan  *	This routine performs the necessary convertions from the DKIOCGGEOM,
7036d10e4ef2Snarayan  *	DKIOCG_PHYSGEOM and DKIOG_VIRTGEOM Solaris structures to the format
7037d10e4ef2Snarayan  *	defined in FWARC 2006/195
70380a55fbb7Slm66018  *
70390a55fbb7Slm66018  * Arguments:
7040d10e4ef2Snarayan  *	vdc	- the vDisk client
70410a55fbb7Slm66018  *	from	- Buffer with data
70420a55fbb7Slm66018  *	to	- Buffer where data is to be copied to
70430a55fbb7Slm66018  *	mode	- flags passed to ioctl
70440a55fbb7Slm66018  *	dir	- direction of copy (in or out)
70450a55fbb7Slm66018  *
70460a55fbb7Slm66018  * Return Code:
70470a55fbb7Slm66018  *	0	- Success
70480a55fbb7Slm66018  *	ENXIO	- Invalid buffer passed in
7049d10e4ef2Snarayan  *	EFAULT	- ddi_copyout of data failed
70500a55fbb7Slm66018  */
70510a55fbb7Slm66018 static int
7052d10e4ef2Snarayan vdc_get_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
70530a55fbb7Slm66018 {
7054d10e4ef2Snarayan 	_NOTE(ARGUNUSED(vdc))
7055d10e4ef2Snarayan 
70560a55fbb7Slm66018 	struct dk_geom	geom;
70570a55fbb7Slm66018 	int	copy_len = sizeof (struct dk_geom);
70580a55fbb7Slm66018 	int	rv = 0;
70590a55fbb7Slm66018 
70600a55fbb7Slm66018 	if (dir != VD_COPYOUT)
70610a55fbb7Slm66018 		return (0);	/* nothing to do */
70620a55fbb7Slm66018 
70630a55fbb7Slm66018 	if ((from == NULL) || (to == NULL))
70640a55fbb7Slm66018 		return (ENXIO);
70650a55fbb7Slm66018 
70660a55fbb7Slm66018 	VD_GEOM2DK_GEOM((vd_geom_t *)from, &geom);
70670a55fbb7Slm66018 	rv = ddi_copyout(&geom, to, copy_len, mode);
70680a55fbb7Slm66018 	if (rv != 0)
70690a55fbb7Slm66018 		rv = EFAULT;
70700a55fbb7Slm66018 
70710a55fbb7Slm66018 	return (rv);
70720a55fbb7Slm66018 }
70730a55fbb7Slm66018 
70740a55fbb7Slm66018 /*
70750a55fbb7Slm66018  * Function:
70760a55fbb7Slm66018  *	vdc_set_geom_convert()
70770a55fbb7Slm66018  *
70780a55fbb7Slm66018  * Description:
7079d10e4ef2Snarayan  *	This routine performs the necessary convertions from the DKIOCSGEOM
7080d10e4ef2Snarayan  *	Solaris structure to the format defined in FWARC 2006/195.
70810a55fbb7Slm66018  *
70820a55fbb7Slm66018  * Arguments:
7083d10e4ef2Snarayan  *	vdc	- the vDisk client
70840a55fbb7Slm66018  *	from	- Buffer with data
70850a55fbb7Slm66018  *	to	- Buffer where data is to be copied to
70860a55fbb7Slm66018  *	mode	- flags passed to ioctl
70870a55fbb7Slm66018  *	dir	- direction of copy (in or out)
70880a55fbb7Slm66018  *
70890a55fbb7Slm66018  * Return Code:
70900a55fbb7Slm66018  *	0	- Success
70910a55fbb7Slm66018  *	ENXIO	- Invalid buffer passed in
70920a55fbb7Slm66018  *	EFAULT	- ddi_copyin of data failed
70930a55fbb7Slm66018  */
70940a55fbb7Slm66018 static int
7095d10e4ef2Snarayan vdc_set_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
70960a55fbb7Slm66018 {
7097d10e4ef2Snarayan 	_NOTE(ARGUNUSED(vdc))
7098d10e4ef2Snarayan 
70990a55fbb7Slm66018 	vd_geom_t	vdgeom;
71000a55fbb7Slm66018 	void		*tmp_mem = NULL;
71010a55fbb7Slm66018 	int		copy_len = sizeof (struct dk_geom);
71020a55fbb7Slm66018 	int		rv = 0;
71030a55fbb7Slm66018 
71040a55fbb7Slm66018 	if (dir != VD_COPYIN)
71050a55fbb7Slm66018 		return (0);	/* nothing to do */
71060a55fbb7Slm66018 
71070a55fbb7Slm66018 	if ((from == NULL) || (to == NULL))
71080a55fbb7Slm66018 		return (ENXIO);
71090a55fbb7Slm66018 
71100a55fbb7Slm66018 	tmp_mem = kmem_alloc(copy_len, KM_SLEEP);
71110a55fbb7Slm66018 
71120a55fbb7Slm66018 	rv = ddi_copyin(from, tmp_mem, copy_len, mode);
71130a55fbb7Slm66018 	if (rv != 0) {
71140a55fbb7Slm66018 		kmem_free(tmp_mem, copy_len);
71150a55fbb7Slm66018 		return (EFAULT);
71160a55fbb7Slm66018 	}
71170a55fbb7Slm66018 	DK_GEOM2VD_GEOM((struct dk_geom *)tmp_mem, &vdgeom);
71180a55fbb7Slm66018 	bcopy(&vdgeom, to, sizeof (vdgeom));
71190a55fbb7Slm66018 	kmem_free(tmp_mem, copy_len);
71200a55fbb7Slm66018 
71210a55fbb7Slm66018 	return (0);
71220a55fbb7Slm66018 }
71230a55fbb7Slm66018 
71244bac2208Snarayan static int
71254bac2208Snarayan vdc_get_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
71264bac2208Snarayan {
71274bac2208Snarayan 	_NOTE(ARGUNUSED(vdc))
71284bac2208Snarayan 
71294bac2208Snarayan 	vd_efi_t	*vd_efi;
71304bac2208Snarayan 	dk_efi_t	dk_efi;
71314bac2208Snarayan 	int		rv = 0;
71324bac2208Snarayan 	void		*uaddr;
71334bac2208Snarayan 
71344bac2208Snarayan 	if ((from == NULL) || (to == NULL))
71354bac2208Snarayan 		return (ENXIO);
71364bac2208Snarayan 
71374bac2208Snarayan 	if (dir == VD_COPYIN) {
71384bac2208Snarayan 
71394bac2208Snarayan 		vd_efi = (vd_efi_t *)to;
71404bac2208Snarayan 
71414bac2208Snarayan 		rv = ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode);
71424bac2208Snarayan 		if (rv != 0)
71434bac2208Snarayan 			return (EFAULT);
71444bac2208Snarayan 
71454bac2208Snarayan 		vd_efi->lba = dk_efi.dki_lba;
71464bac2208Snarayan 		vd_efi->length = dk_efi.dki_length;
71474bac2208Snarayan 		bzero(vd_efi->data, vd_efi->length);
71484bac2208Snarayan 
71494bac2208Snarayan 	} else {
71504bac2208Snarayan 
71514bac2208Snarayan 		rv = ddi_copyin(to, &dk_efi, sizeof (dk_efi_t), mode);
71524bac2208Snarayan 		if (rv != 0)
71534bac2208Snarayan 			return (EFAULT);
71544bac2208Snarayan 
71554bac2208Snarayan 		uaddr = dk_efi.dki_data;
71564bac2208Snarayan 
71574bac2208Snarayan 		dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP);
71584bac2208Snarayan 
71594bac2208Snarayan 		VD_EFI2DK_EFI((vd_efi_t *)from, &dk_efi);
71604bac2208Snarayan 
71614bac2208Snarayan 		rv = ddi_copyout(dk_efi.dki_data, uaddr, dk_efi.dki_length,
71624bac2208Snarayan 		    mode);
71634bac2208Snarayan 		if (rv != 0)
71644bac2208Snarayan 			return (EFAULT);
71654bac2208Snarayan 
71664bac2208Snarayan 		kmem_free(dk_efi.dki_data, dk_efi.dki_length);
71674bac2208Snarayan 	}
71684bac2208Snarayan 
71694bac2208Snarayan 	return (0);
71704bac2208Snarayan }
71714bac2208Snarayan 
71724bac2208Snarayan static int
71734bac2208Snarayan vdc_set_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
71744bac2208Snarayan {
71754bac2208Snarayan 	_NOTE(ARGUNUSED(vdc))
71764bac2208Snarayan 
71774bac2208Snarayan 	dk_efi_t	dk_efi;
71784bac2208Snarayan 	void		*uaddr;
71794bac2208Snarayan 
71802f5224aeSachartre 	if (dir == VD_COPYOUT) {
71812f5224aeSachartre 		/*
71822f5224aeSachartre 		 * The disk label may have changed. Revalidate the disk
71832f5224aeSachartre 		 * geometry. This will also update the device nodes and
71842f5224aeSachartre 		 * properties.
71852f5224aeSachartre 		 */
71862f5224aeSachartre 		vdc_validate(vdc);
71872f5224aeSachartre 		return (0);
71882f5224aeSachartre 	}
71894bac2208Snarayan 
71904bac2208Snarayan 	if ((from == NULL) || (to == NULL))
71914bac2208Snarayan 		return (ENXIO);
71924bac2208Snarayan 
71934bac2208Snarayan 	if (ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode) != 0)
71944bac2208Snarayan 		return (EFAULT);
71954bac2208Snarayan 
71964bac2208Snarayan 	uaddr = dk_efi.dki_data;
71974bac2208Snarayan 
71984bac2208Snarayan 	dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP);
71994bac2208Snarayan 
72004bac2208Snarayan 	if (ddi_copyin(uaddr, dk_efi.dki_data, dk_efi.dki_length, mode) != 0)
72014bac2208Snarayan 		return (EFAULT);
72024bac2208Snarayan 
72034bac2208Snarayan 	DK_EFI2VD_EFI(&dk_efi, (vd_efi_t *)to);
72044bac2208Snarayan 
72054bac2208Snarayan 	kmem_free(dk_efi.dki_data, dk_efi.dki_length);
72064bac2208Snarayan 
72074bac2208Snarayan 	return (0);
72084bac2208Snarayan }
72094bac2208Snarayan 
721017cadca8Slm66018 
721117cadca8Slm66018 /* -------------------------------------------------------------------------- */
721217cadca8Slm66018 
72130a55fbb7Slm66018 /*
72140a55fbb7Slm66018  * Function:
72151ae08745Sheppo  *	vdc_create_fake_geometry()
72161ae08745Sheppo  *
72171ae08745Sheppo  * Description:
721817cadca8Slm66018  *	This routine fakes up the disk info needed for some DKIO ioctls such
721917cadca8Slm66018  *	as DKIOCINFO and DKIOCGMEDIAINFO [just like lofi(7D) and ramdisk(7D) do]
72201ae08745Sheppo  *
722117cadca8Slm66018  *	Note: This function must not be called until the vDisk attributes have
722217cadca8Slm66018  *	been exchanged as part of the handshake with the vDisk server.
72231ae08745Sheppo  *
72241ae08745Sheppo  * Arguments:
72251ae08745Sheppo  *	vdc	- soft state pointer for this instance of the device driver.
72261ae08745Sheppo  *
72271ae08745Sheppo  * Return Code:
722878fcd0a1Sachartre  *	none.
72291ae08745Sheppo  */
723078fcd0a1Sachartre static void
72311ae08745Sheppo vdc_create_fake_geometry(vdc_t *vdc)
72321ae08745Sheppo {
72331ae08745Sheppo 	ASSERT(vdc != NULL);
723478fcd0a1Sachartre 	ASSERT(vdc->max_xfer_sz != 0);
72350d0c8d4bSnarayan 
72360d0c8d4bSnarayan 	/*
72371ae08745Sheppo 	 * DKIOCINFO support
72381ae08745Sheppo 	 */
723978fcd0a1Sachartre 	if (vdc->cinfo == NULL)
72401ae08745Sheppo 		vdc->cinfo = kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
72411ae08745Sheppo 
72421ae08745Sheppo 	(void) strcpy(vdc->cinfo->dki_cname, VDC_DRIVER_NAME);
72431ae08745Sheppo 	(void) strcpy(vdc->cinfo->dki_dname, VDC_DRIVER_NAME);
72448e6a2a04Slm66018 	/* max_xfer_sz is #blocks so we don't need to divide by DEV_BSIZE */
72458e6a2a04Slm66018 	vdc->cinfo->dki_maxtransfer = vdc->max_xfer_sz;
72462f5224aeSachartre 
724787a7269eSachartre 	/*
72482f5224aeSachartre 	 * We set the controller type to DKC_SCSI_CCS only if the VD_OP_SCSICMD
72492f5224aeSachartre 	 * operation is supported, otherwise the controller type is DKC_DIRECT.
72502f5224aeSachartre 	 * Version 1.0 does not support the VD_OP_SCSICMD operation, so the
72512f5224aeSachartre 	 * controller type is always DKC_DIRECT in that case.
72522f5224aeSachartre 	 *
725317cadca8Slm66018 	 * If the virtual disk is backed by a physical CD/DVD device or
725417cadca8Slm66018 	 * an ISO image, modify the controller type to indicate this
725587a7269eSachartre 	 */
725617cadca8Slm66018 	switch (vdc->vdisk_media) {
725717cadca8Slm66018 	case VD_MEDIA_CD:
725817cadca8Slm66018 	case VD_MEDIA_DVD:
725917cadca8Slm66018 		vdc->cinfo->dki_ctype = DKC_CDROM;
726017cadca8Slm66018 		break;
726117cadca8Slm66018 	case VD_MEDIA_FIXED:
72622f5224aeSachartre 		if (VD_OP_SUPPORTED(vdc->operations, VD_OP_SCSICMD))
72632f5224aeSachartre 			vdc->cinfo->dki_ctype = DKC_SCSI_CCS;
72642f5224aeSachartre 		else
726587a7269eSachartre 			vdc->cinfo->dki_ctype = DKC_DIRECT;
726617cadca8Slm66018 		break;
726717cadca8Slm66018 	default:
726817cadca8Slm66018 		/* in the case of v1.0 we default to a fixed disk */
726917cadca8Slm66018 		vdc->cinfo->dki_ctype = DKC_DIRECT;
727017cadca8Slm66018 		break;
727117cadca8Slm66018 	}
72721ae08745Sheppo 	vdc->cinfo->dki_flags = DKI_FMTVOL;
72731ae08745Sheppo 	vdc->cinfo->dki_cnum = 0;
72741ae08745Sheppo 	vdc->cinfo->dki_addr = 0;
72751ae08745Sheppo 	vdc->cinfo->dki_space = 0;
72761ae08745Sheppo 	vdc->cinfo->dki_prio = 0;
72771ae08745Sheppo 	vdc->cinfo->dki_vec = 0;
72781ae08745Sheppo 	vdc->cinfo->dki_unit = vdc->instance;
72791ae08745Sheppo 	vdc->cinfo->dki_slave = 0;
72801ae08745Sheppo 	/*
72811ae08745Sheppo 	 * The partition number will be created on the fly depending on the
72821ae08745Sheppo 	 * actual slice (i.e. minor node) that is used to request the data.
72831ae08745Sheppo 	 */
72841ae08745Sheppo 	vdc->cinfo->dki_partition = 0;
72851ae08745Sheppo 
72861ae08745Sheppo 	/*
72871ae08745Sheppo 	 * DKIOCGMEDIAINFO support
72881ae08745Sheppo 	 */
72890a55fbb7Slm66018 	if (vdc->minfo == NULL)
72901ae08745Sheppo 		vdc->minfo = kmem_zalloc(sizeof (struct dk_minfo), KM_SLEEP);
729117cadca8Slm66018 
729217cadca8Slm66018 	if (vio_ver_is_supported(vdc->ver, 1, 1)) {
729317cadca8Slm66018 		vdc->minfo->dki_media_type =
729417cadca8Slm66018 		    VD_MEDIATYPE2DK_MEDIATYPE(vdc->vdisk_media);
729517cadca8Slm66018 	} else {
72961ae08745Sheppo 		vdc->minfo->dki_media_type = DK_FIXED_DISK;
729717cadca8Slm66018 	}
729817cadca8Slm66018 
72994bac2208Snarayan 	vdc->minfo->dki_capacity = vdc->vdisk_size;
730017cadca8Slm66018 	vdc->minfo->dki_lbsize = vdc->block_size;
730178fcd0a1Sachartre }
73021ae08745Sheppo 
730378fcd0a1Sachartre static ushort_t
730478fcd0a1Sachartre vdc_lbl2cksum(struct dk_label *label)
730578fcd0a1Sachartre {
730678fcd0a1Sachartre 	int	count;
730778fcd0a1Sachartre 	ushort_t sum, *sp;
730878fcd0a1Sachartre 
730978fcd0a1Sachartre 	count =	(sizeof (struct dk_label)) / (sizeof (short)) - 1;
731078fcd0a1Sachartre 	sp = (ushort_t *)label;
731178fcd0a1Sachartre 	sum = 0;
731278fcd0a1Sachartre 	while (count--) {
731378fcd0a1Sachartre 		sum ^= *sp++;
731478fcd0a1Sachartre 	}
731578fcd0a1Sachartre 
731678fcd0a1Sachartre 	return (sum);
73170a55fbb7Slm66018 }
73180a55fbb7Slm66018 
73190a55fbb7Slm66018 /*
73200a55fbb7Slm66018  * Function:
732178fcd0a1Sachartre  *	vdc_validate_geometry
73220a55fbb7Slm66018  *
73230a55fbb7Slm66018  * Description:
732478fcd0a1Sachartre  *	This routine discovers the label and geometry of the disk. It stores
732578fcd0a1Sachartre  *	the disk label and related information in the vdc structure. If it
732678fcd0a1Sachartre  *	fails to validate the geometry or to discover the disk label then
732778fcd0a1Sachartre  *	the label is marked as unknown (VD_DISK_LABEL_UNK).
73280a55fbb7Slm66018  *
73290a55fbb7Slm66018  * Arguments:
73300a55fbb7Slm66018  *	vdc	- soft state pointer for this instance of the device driver.
73310a55fbb7Slm66018  *
73320a55fbb7Slm66018  * Return Code:
733378fcd0a1Sachartre  *	0	- success.
733478fcd0a1Sachartre  *	EINVAL	- unknown disk label.
733578fcd0a1Sachartre  *	ENOTSUP	- geometry not applicable (EFI label).
733678fcd0a1Sachartre  *	EIO	- error accessing the disk.
73370a55fbb7Slm66018  */
73380a55fbb7Slm66018 static int
733978fcd0a1Sachartre vdc_validate_geometry(vdc_t *vdc)
73400a55fbb7Slm66018 {
7341d10e4ef2Snarayan 	buf_t	*buf;	/* BREAD requests need to be in a buf_t structure */
73420a55fbb7Slm66018 	dev_t	dev;
73432f5224aeSachartre 	int	rv, rval;
734478fcd0a1Sachartre 	struct dk_label label;
734578fcd0a1Sachartre 	struct dk_geom geom;
734678fcd0a1Sachartre 	struct vtoc vtoc;
7347edcc0754Sachartre 	efi_gpt_t *gpt;
7348edcc0754Sachartre 	efi_gpe_t *gpe;
7349edcc0754Sachartre 	vd_efi_dev_t edev;
73500a55fbb7Slm66018 
73510a55fbb7Slm66018 	ASSERT(vdc != NULL);
735278fcd0a1Sachartre 	ASSERT(vdc->vtoc != NULL && vdc->geom != NULL);
735378fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
73540a55fbb7Slm66018 
735578fcd0a1Sachartre 	mutex_exit(&vdc->lock);
73560a55fbb7Slm66018 
73570a55fbb7Slm66018 	dev = makedevice(ddi_driver_major(vdc->dip),
73580a55fbb7Slm66018 	    VD_MAKE_DEV(vdc->instance, 0));
73594bac2208Snarayan 
73602f5224aeSachartre 	rv = vd_process_ioctl(dev, DKIOCGGEOM, (caddr_t)&geom, FKIOCTL, &rval);
736178fcd0a1Sachartre 	if (rv == 0)
73622f5224aeSachartre 		rv = vd_process_ioctl(dev, DKIOCGVTOC, (caddr_t)&vtoc,
73632f5224aeSachartre 		    FKIOCTL, &rval);
73640d0c8d4bSnarayan 
73654bac2208Snarayan 	if (rv == ENOTSUP) {
73664bac2208Snarayan 		/*
73674bac2208Snarayan 		 * If the device does not support VTOC then we try
73684bac2208Snarayan 		 * to read an EFI label.
7369edcc0754Sachartre 		 *
7370edcc0754Sachartre 		 * We need to know the block size and the disk size to
7371edcc0754Sachartre 		 * be able to read an EFI label.
73724bac2208Snarayan 		 */
7373edcc0754Sachartre 		if (vdc->vdisk_size == 0) {
7374edcc0754Sachartre 			if ((rv = vdc_check_capacity(vdc)) != 0) {
7375edcc0754Sachartre 				mutex_enter(&vdc->lock);
7376edcc0754Sachartre 				vdc_store_label_unk(vdc);
7377edcc0754Sachartre 				return (rv);
7378edcc0754Sachartre 			}
7379edcc0754Sachartre 		}
73804bac2208Snarayan 
7381edcc0754Sachartre 		VD_EFI_DEV_SET(edev, vdc, vd_process_efi_ioctl);
7382edcc0754Sachartre 
7383edcc0754Sachartre 		rv = vd_efi_alloc_and_read(&edev, &gpt, &gpe);
73844bac2208Snarayan 
73854bac2208Snarayan 		if (rv) {
73863af08d82Slm66018 			DMSG(vdc, 0, "[%d] Failed to get EFI (err=%d)",
73874bac2208Snarayan 			    vdc->instance, rv);
738878fcd0a1Sachartre 			mutex_enter(&vdc->lock);
738978fcd0a1Sachartre 			vdc_store_label_unk(vdc);
739078fcd0a1Sachartre 			return (EIO);
739178fcd0a1Sachartre 		}
739278fcd0a1Sachartre 
739378fcd0a1Sachartre 		mutex_enter(&vdc->lock);
7394edcc0754Sachartre 		vdc_store_label_efi(vdc, gpt, gpe);
7395edcc0754Sachartre 		vd_efi_free(&edev, gpt, gpe);
739678fcd0a1Sachartre 		return (ENOTSUP);
739778fcd0a1Sachartre 	}
739878fcd0a1Sachartre 
739978fcd0a1Sachartre 	if (rv != 0) {
740078fcd0a1Sachartre 		DMSG(vdc, 0, "[%d] Failed to get VTOC (err=%d)",
740178fcd0a1Sachartre 		    vdc->instance, rv);
740278fcd0a1Sachartre 		mutex_enter(&vdc->lock);
740378fcd0a1Sachartre 		vdc_store_label_unk(vdc);
740478fcd0a1Sachartre 		if (rv != EINVAL)
740578fcd0a1Sachartre 			rv = EIO;
74064bac2208Snarayan 		return (rv);
74074bac2208Snarayan 	}
74084bac2208Snarayan 
740978fcd0a1Sachartre 	/* check that geometry and vtoc are valid */
741078fcd0a1Sachartre 	if (geom.dkg_nhead == 0 || geom.dkg_nsect == 0 ||
741178fcd0a1Sachartre 	    vtoc.v_sanity != VTOC_SANE) {
741278fcd0a1Sachartre 		mutex_enter(&vdc->lock);
741378fcd0a1Sachartre 		vdc_store_label_unk(vdc);
741478fcd0a1Sachartre 		return (EINVAL);
741578fcd0a1Sachartre 	}
74164bac2208Snarayan 
741778fcd0a1Sachartre 	/*
741878fcd0a1Sachartre 	 * We have a disk and a valid VTOC. However this does not mean
741978fcd0a1Sachartre 	 * that the disk currently have a VTOC label. The returned VTOC may
742078fcd0a1Sachartre 	 * be a default VTOC to be used for configuring the disk (this is
742178fcd0a1Sachartre 	 * what is done for disk image). So we read the label from the
742278fcd0a1Sachartre 	 * beginning of the disk to ensure we really have a VTOC label.
742378fcd0a1Sachartre 	 *
742478fcd0a1Sachartre 	 * FUTURE: This could be the default way for reading the VTOC
742578fcd0a1Sachartre 	 * from the disk as opposed to sending the VD_OP_GET_VTOC
742678fcd0a1Sachartre 	 * to the server. This will be the default if vdc is implemented
742778fcd0a1Sachartre 	 * ontop of cmlb.
742878fcd0a1Sachartre 	 */
742978fcd0a1Sachartre 
743078fcd0a1Sachartre 	/*
743178fcd0a1Sachartre 	 * Single slice disk does not support read using an absolute disk
743278fcd0a1Sachartre 	 * offset so we just rely on the DKIOCGVTOC ioctl in that case.
743378fcd0a1Sachartre 	 */
743478fcd0a1Sachartre 	if (vdc->vdisk_type == VD_DISK_TYPE_SLICE) {
743578fcd0a1Sachartre 		mutex_enter(&vdc->lock);
743678fcd0a1Sachartre 		if (vtoc.v_nparts != 1) {
743778fcd0a1Sachartre 			vdc_store_label_unk(vdc);
743878fcd0a1Sachartre 			return (EINVAL);
743978fcd0a1Sachartre 		}
744078fcd0a1Sachartre 		vdc_store_label_vtoc(vdc, &geom, &vtoc);
74414bac2208Snarayan 		return (0);
74424bac2208Snarayan 	}
74434bac2208Snarayan 
744478fcd0a1Sachartre 	if (vtoc.v_nparts != V_NUMPAR) {
744578fcd0a1Sachartre 		mutex_enter(&vdc->lock);
744678fcd0a1Sachartre 		vdc_store_label_unk(vdc);
744778fcd0a1Sachartre 		return (EINVAL);
74480a55fbb7Slm66018 	}
7449d10e4ef2Snarayan 
7450d10e4ef2Snarayan 	/*
7451d10e4ef2Snarayan 	 * Read disk label from start of disk
7452d10e4ef2Snarayan 	 */
7453d10e4ef2Snarayan 	buf = kmem_alloc(sizeof (buf_t), KM_SLEEP);
7454d10e4ef2Snarayan 	bioinit(buf);
745578fcd0a1Sachartre 	buf->b_un.b_addr = (caddr_t)&label;
7456d10e4ef2Snarayan 	buf->b_bcount = DK_LABEL_SIZE;
7457d10e4ef2Snarayan 	buf->b_flags = B_BUSY | B_READ;
745817cadca8Slm66018 	buf->b_dev = cmpdev(dev);
745978fcd0a1Sachartre 	rv = vdc_send_request(vdc, VD_OP_BREAD, (caddr_t)&label,
746078fcd0a1Sachartre 	    DK_LABEL_SIZE, VD_SLICE_NONE, 0, CB_STRATEGY, buf, VIO_read_dir);
74613af08d82Slm66018 	if (rv) {
74623af08d82Slm66018 		DMSG(vdc, 1, "[%d] Failed to read disk block 0\n",
74633af08d82Slm66018 		    vdc->instance);
746478fcd0a1Sachartre 	} else {
7465d10e4ef2Snarayan 		rv = biowait(buf);
7466d10e4ef2Snarayan 		biofini(buf);
746778fcd0a1Sachartre 	}
7468d10e4ef2Snarayan 	kmem_free(buf, sizeof (buf_t));
74690a55fbb7Slm66018 
747078fcd0a1Sachartre 	if (rv != 0 || label.dkl_magic != DKL_MAGIC ||
747178fcd0a1Sachartre 	    label.dkl_cksum != vdc_lbl2cksum(&label)) {
747278fcd0a1Sachartre 		DMSG(vdc, 1, "[%d] Got VTOC with invalid label\n",
747378fcd0a1Sachartre 		    vdc->instance);
747478fcd0a1Sachartre 		mutex_enter(&vdc->lock);
747578fcd0a1Sachartre 		vdc_store_label_unk(vdc);
747678fcd0a1Sachartre 		return (EINVAL);
747778fcd0a1Sachartre 	}
747878fcd0a1Sachartre 
747978fcd0a1Sachartre 	mutex_enter(&vdc->lock);
748078fcd0a1Sachartre 	vdc_store_label_vtoc(vdc, &geom, &vtoc);
748178fcd0a1Sachartre 	return (0);
748278fcd0a1Sachartre }
748378fcd0a1Sachartre 
748478fcd0a1Sachartre /*
748578fcd0a1Sachartre  * Function:
748678fcd0a1Sachartre  *	vdc_validate
748778fcd0a1Sachartre  *
748878fcd0a1Sachartre  * Description:
748978fcd0a1Sachartre  *	This routine discovers the label of the disk and create the
749078fcd0a1Sachartre  *	appropriate device nodes if the label has changed.
749178fcd0a1Sachartre  *
749278fcd0a1Sachartre  * Arguments:
749378fcd0a1Sachartre  *	vdc	- soft state pointer for this instance of the device driver.
749478fcd0a1Sachartre  *
749578fcd0a1Sachartre  * Return Code:
749678fcd0a1Sachartre  *	none.
749778fcd0a1Sachartre  */
749878fcd0a1Sachartre static void
749978fcd0a1Sachartre vdc_validate(vdc_t *vdc)
750078fcd0a1Sachartre {
750178fcd0a1Sachartre 	vd_disk_label_t old_label;
7502edcc0754Sachartre 	vd_slice_t old_slice[V_NUMPAR];
750378fcd0a1Sachartre 	int rv;
750478fcd0a1Sachartre 
750578fcd0a1Sachartre 	ASSERT(!MUTEX_HELD(&vdc->lock));
750678fcd0a1Sachartre 
750778fcd0a1Sachartre 	mutex_enter(&vdc->lock);
750878fcd0a1Sachartre 
750978fcd0a1Sachartre 	/* save the current label and vtoc */
751078fcd0a1Sachartre 	old_label = vdc->vdisk_label;
7511edcc0754Sachartre 	bcopy(vdc->slice, &old_slice, sizeof (vd_slice_t) * V_NUMPAR);
751278fcd0a1Sachartre 
751378fcd0a1Sachartre 	/* check the geometry */
751478fcd0a1Sachartre 	(void) vdc_validate_geometry(vdc);
751578fcd0a1Sachartre 
751678fcd0a1Sachartre 	/* if the disk label has changed, update device nodes */
751778fcd0a1Sachartre 	if (vdc->vdisk_label != old_label) {
751878fcd0a1Sachartre 
751978fcd0a1Sachartre 		if (vdc->vdisk_label == VD_DISK_LABEL_EFI)
752078fcd0a1Sachartre 			rv = vdc_create_device_nodes_efi(vdc);
752178fcd0a1Sachartre 		else
752278fcd0a1Sachartre 			rv = vdc_create_device_nodes_vtoc(vdc);
752378fcd0a1Sachartre 
752478fcd0a1Sachartre 		if (rv != 0) {
752578fcd0a1Sachartre 			DMSG(vdc, 0, "![%d] Failed to update device nodes",
752678fcd0a1Sachartre 			    vdc->instance);
752778fcd0a1Sachartre 		}
752878fcd0a1Sachartre 	}
752978fcd0a1Sachartre 
753078fcd0a1Sachartre 	/* if the vtoc has changed, update device nodes properties */
7531edcc0754Sachartre 	if (bcmp(vdc->slice, &old_slice, sizeof (vd_slice_t) * V_NUMPAR) != 0) {
753278fcd0a1Sachartre 
753378fcd0a1Sachartre 		if (vdc_create_device_nodes_props(vdc) != 0) {
753478fcd0a1Sachartre 			DMSG(vdc, 0, "![%d] Failed to update device nodes"
753578fcd0a1Sachartre 			    " properties", vdc->instance);
753678fcd0a1Sachartre 		}
753778fcd0a1Sachartre 	}
753878fcd0a1Sachartre 
753978fcd0a1Sachartre 	mutex_exit(&vdc->lock);
754078fcd0a1Sachartre }
754178fcd0a1Sachartre 
754278fcd0a1Sachartre static void
754378fcd0a1Sachartre vdc_validate_task(void *arg)
754478fcd0a1Sachartre {
754578fcd0a1Sachartre 	vdc_t *vdc = (vdc_t *)arg;
754678fcd0a1Sachartre 
754778fcd0a1Sachartre 	vdc_validate(vdc);
754878fcd0a1Sachartre 
754978fcd0a1Sachartre 	mutex_enter(&vdc->lock);
755078fcd0a1Sachartre 	ASSERT(vdc->validate_pending > 0);
755178fcd0a1Sachartre 	vdc->validate_pending--;
755278fcd0a1Sachartre 	mutex_exit(&vdc->lock);
75531ae08745Sheppo }
75544bac2208Snarayan 
75554bac2208Snarayan /*
75564bac2208Snarayan  * Function:
75574bac2208Snarayan  *	vdc_setup_devid()
75584bac2208Snarayan  *
75594bac2208Snarayan  * Description:
75604bac2208Snarayan  *	This routine discovers the devid of a vDisk. It requests the devid of
75614bac2208Snarayan  *	the underlying device from the vDisk server, builds an encapsulated
75624bac2208Snarayan  *	devid based on the retrieved devid and registers that new devid to
75634bac2208Snarayan  *	the vDisk.
75644bac2208Snarayan  *
75654bac2208Snarayan  * Arguments:
75664bac2208Snarayan  *	vdc	- soft state pointer for this instance of the device driver.
75674bac2208Snarayan  *
75684bac2208Snarayan  * Return Code:
75694bac2208Snarayan  *	0	- A devid was succesfully registered for the vDisk
75704bac2208Snarayan  */
75714bac2208Snarayan static int
75724bac2208Snarayan vdc_setup_devid(vdc_t *vdc)
75734bac2208Snarayan {
75744bac2208Snarayan 	int rv;
75754bac2208Snarayan 	vd_devid_t *vd_devid;
75764bac2208Snarayan 	size_t bufsize, bufid_len;
75774bac2208Snarayan 
75784bac2208Snarayan 	/*
75794bac2208Snarayan 	 * At first sight, we don't know the size of the devid that the
75804bac2208Snarayan 	 * server will return but this size will be encoded into the
75814bac2208Snarayan 	 * reply. So we do a first request using a default size then we
75824bac2208Snarayan 	 * check if this size was large enough. If not then we do a second
75834bac2208Snarayan 	 * request with the correct size returned by the server. Note that
75844bac2208Snarayan 	 * ldc requires size to be 8-byte aligned.
75854bac2208Snarayan 	 */
75864bac2208Snarayan 	bufsize = P2ROUNDUP(VD_DEVID_SIZE(VD_DEVID_DEFAULT_LEN),
75874bac2208Snarayan 	    sizeof (uint64_t));
75884bac2208Snarayan 	vd_devid = kmem_zalloc(bufsize, KM_SLEEP);
75894bac2208Snarayan 	bufid_len = bufsize - sizeof (vd_efi_t) - 1;
75904bac2208Snarayan 
75913af08d82Slm66018 	rv = vdc_do_sync_op(vdc, VD_OP_GET_DEVID, (caddr_t)vd_devid,
75922f5224aeSachartre 	    bufsize, 0, 0, CB_SYNC, 0, VIO_both_dir, B_TRUE);
75933af08d82Slm66018 
75943af08d82Slm66018 	DMSG(vdc, 2, "sync_op returned %d\n", rv);
75953af08d82Slm66018 
75964bac2208Snarayan 	if (rv) {
75974bac2208Snarayan 		kmem_free(vd_devid, bufsize);
75984bac2208Snarayan 		return (rv);
75994bac2208Snarayan 	}
76004bac2208Snarayan 
76014bac2208Snarayan 	if (vd_devid->length > bufid_len) {
76024bac2208Snarayan 		/*
76034bac2208Snarayan 		 * The returned devid is larger than the buffer used. Try again
76044bac2208Snarayan 		 * with a buffer with the right size.
76054bac2208Snarayan 		 */
76064bac2208Snarayan 		kmem_free(vd_devid, bufsize);
76074bac2208Snarayan 		bufsize = P2ROUNDUP(VD_DEVID_SIZE(vd_devid->length),
76084bac2208Snarayan 		    sizeof (uint64_t));
76094bac2208Snarayan 		vd_devid = kmem_zalloc(bufsize, KM_SLEEP);
76104bac2208Snarayan 		bufid_len = bufsize - sizeof (vd_efi_t) - 1;
76114bac2208Snarayan 
76123af08d82Slm66018 		rv = vdc_do_sync_op(vdc, VD_OP_GET_DEVID,
76133af08d82Slm66018 		    (caddr_t)vd_devid, bufsize, 0, 0, CB_SYNC, 0,
76142f5224aeSachartre 		    VIO_both_dir, B_TRUE);
76153af08d82Slm66018 
76164bac2208Snarayan 		if (rv) {
76174bac2208Snarayan 			kmem_free(vd_devid, bufsize);
76184bac2208Snarayan 			return (rv);
76194bac2208Snarayan 		}
76204bac2208Snarayan 	}
76214bac2208Snarayan 
76224bac2208Snarayan 	/*
76234bac2208Snarayan 	 * The virtual disk should have the same device id as the one associated
76244bac2208Snarayan 	 * with the physical disk it is mapped on, otherwise sharing a disk
76254bac2208Snarayan 	 * between a LDom and a non-LDom may not work (for example for a shared
76264bac2208Snarayan 	 * SVM disk set).
76274bac2208Snarayan 	 *
76284bac2208Snarayan 	 * The DDI framework does not allow creating a device id with any
76294bac2208Snarayan 	 * type so we first create a device id of type DEVID_ENCAP and then
76304bac2208Snarayan 	 * we restore the orignal type of the physical device.
76314bac2208Snarayan 	 */
76324bac2208Snarayan 
76333af08d82Slm66018 	DMSG(vdc, 2, ": devid length = %d\n", vd_devid->length);
76343af08d82Slm66018 
76354bac2208Snarayan 	/* build an encapsulated devid based on the returned devid */
76364bac2208Snarayan 	if (ddi_devid_init(vdc->dip, DEVID_ENCAP, vd_devid->length,
76374bac2208Snarayan 	    vd_devid->id, &vdc->devid) != DDI_SUCCESS) {
76383af08d82Slm66018 		DMSG(vdc, 1, "[%d] Fail to created devid\n", vdc->instance);
76394bac2208Snarayan 		kmem_free(vd_devid, bufsize);
76404bac2208Snarayan 		return (1);
76414bac2208Snarayan 	}
76424bac2208Snarayan 
76434bac2208Snarayan 	DEVID_FORMTYPE((impl_devid_t *)vdc->devid, vd_devid->type);
76444bac2208Snarayan 
76454bac2208Snarayan 	ASSERT(ddi_devid_valid(vdc->devid) == DDI_SUCCESS);
76464bac2208Snarayan 
76474bac2208Snarayan 	kmem_free(vd_devid, bufsize);
76484bac2208Snarayan 
76494bac2208Snarayan 	if (ddi_devid_register(vdc->dip, vdc->devid) != DDI_SUCCESS) {
76503af08d82Slm66018 		DMSG(vdc, 1, "[%d] Fail to register devid\n", vdc->instance);
76514bac2208Snarayan 		return (1);
76524bac2208Snarayan 	}
76534bac2208Snarayan 
76544bac2208Snarayan 	return (0);
76554bac2208Snarayan }
76564bac2208Snarayan 
76574bac2208Snarayan static void
7658edcc0754Sachartre vdc_store_label_efi(vdc_t *vdc, efi_gpt_t *gpt, efi_gpe_t *gpe)
76594bac2208Snarayan {
7660edcc0754Sachartre 	int i, nparts;
76614bac2208Snarayan 
766278fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
766378fcd0a1Sachartre 
766478fcd0a1Sachartre 	vdc->vdisk_label = VD_DISK_LABEL_EFI;
7665edcc0754Sachartre 	bzero(vdc->vtoc, sizeof (struct vtoc));
766678fcd0a1Sachartre 	bzero(vdc->geom, sizeof (struct dk_geom));
7667edcc0754Sachartre 	bzero(vdc->slice, sizeof (vd_slice_t) * V_NUMPAR);
7668edcc0754Sachartre 
7669edcc0754Sachartre 	nparts = gpt->efi_gpt_NumberOfPartitionEntries;
7670edcc0754Sachartre 
7671edcc0754Sachartre 	for (i = 0; i < nparts && i < VD_EFI_WD_SLICE; i++) {
7672edcc0754Sachartre 
7673edcc0754Sachartre 		if (gpe[i].efi_gpe_StartingLBA == 0 ||
7674edcc0754Sachartre 		    gpe[i].efi_gpe_EndingLBA == 0) {
7675edcc0754Sachartre 			continue;
76764bac2208Snarayan 		}
7677edcc0754Sachartre 
7678edcc0754Sachartre 		vdc->slice[i].start = gpe[i].efi_gpe_StartingLBA;
7679edcc0754Sachartre 		vdc->slice[i].nblocks = gpe[i].efi_gpe_EndingLBA -
7680edcc0754Sachartre 		    gpe[i].efi_gpe_StartingLBA + 1;
7681edcc0754Sachartre 	}
7682edcc0754Sachartre 
7683edcc0754Sachartre 	ASSERT(vdc->vdisk_size != 0);
7684edcc0754Sachartre 	vdc->slice[VD_EFI_WD_SLICE].start = 0;
7685edcc0754Sachartre 	vdc->slice[VD_EFI_WD_SLICE].nblocks = vdc->vdisk_size;
7686edcc0754Sachartre 
76874bac2208Snarayan }
768878fcd0a1Sachartre 
768978fcd0a1Sachartre static void
769078fcd0a1Sachartre vdc_store_label_vtoc(vdc_t *vdc, struct dk_geom *geom, struct vtoc *vtoc)
769178fcd0a1Sachartre {
7692edcc0754Sachartre 	int i;
7693edcc0754Sachartre 
769478fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
7695edcc0754Sachartre 	ASSERT(vdc->block_size == vtoc->v_sectorsz);
769678fcd0a1Sachartre 
769778fcd0a1Sachartre 	vdc->vdisk_label = VD_DISK_LABEL_VTOC;
769878fcd0a1Sachartre 	bcopy(vtoc, vdc->vtoc, sizeof (struct vtoc));
769978fcd0a1Sachartre 	bcopy(geom, vdc->geom, sizeof (struct dk_geom));
7700edcc0754Sachartre 	bzero(vdc->slice, sizeof (vd_slice_t) * V_NUMPAR);
7701edcc0754Sachartre 
7702edcc0754Sachartre 	for (i = 0; i < vtoc->v_nparts; i++) {
7703edcc0754Sachartre 		vdc->slice[i].start = vtoc->v_part[i].p_start;
7704edcc0754Sachartre 		vdc->slice[i].nblocks = vtoc->v_part[i].p_size;
7705edcc0754Sachartre 	}
770678fcd0a1Sachartre }
770778fcd0a1Sachartre 
770878fcd0a1Sachartre static void
770978fcd0a1Sachartre vdc_store_label_unk(vdc_t *vdc)
771078fcd0a1Sachartre {
771178fcd0a1Sachartre 	ASSERT(MUTEX_HELD(&vdc->lock));
771278fcd0a1Sachartre 
771378fcd0a1Sachartre 	vdc->vdisk_label = VD_DISK_LABEL_UNK;
771478fcd0a1Sachartre 	bzero(vdc->vtoc, sizeof (struct vtoc));
771578fcd0a1Sachartre 	bzero(vdc->geom, sizeof (struct dk_geom));
7716edcc0754Sachartre 	bzero(vdc->slice, sizeof (vd_slice_t) * V_NUMPAR);
771778fcd0a1Sachartre }
7718